logspace — Generate logarithmically spaced row vectors that mirror MATLAB behavior.
logspace(a, b, n) returns n points that are equally spaced on a logarithmic scale between 10^a and 10^b. When n is omitted, MATLAB (and RunMat) default to 50 points. The result is always a 1 × n row vector.
How logspace works in RunMat
logspace(a, b)is equivalent tologspace(a, b, 50).logspace(a, b, 0)returns a1 × 0empty double row vector.logspace(a, b, 1)returns10^b.logspace(a, b, 2)returns[10^a 10^b].- Endpoints may be real or complex scalars. Complex inputs produce complex outputs using the analytic definition
10^z = e^{z ln 10}. - Scalar inputs may be numeric literals,
1×1tensors, or gathered GPU scalars (gpuArrayhandles). Non-scalar inputs raise an error. - The third argument must be a finite, non-negative integer.
How logspace runs on the GPU
When either endpoint already resides on the GPU, RunMat attempts to keep the sequence on the device. Providers that implement linspace, scalar multiplication, and unary_exp can build the vector entirely in device memory. If any hook is unavailable, RunMat generates the sequence on the host and performs a single upload, ensuring downstream kernels still receive a GPU tensor without surprising performance cliffs.
GPU memory and residency
You usually do **not** need to call gpuArray yourself in RunMat. When an acceleration provider is active and either endpoint already lives on the GPU, RunMat keeps the generated sequence on the device whenever the provider exposes the necessary hooks. If the hooks are missing, RunMat generates the vector on the host and uploads it once. This strategy preserves residency for subsequent kernels while avoiding partial GPU execution.
Examples
Creating decades between 10 and 1000
x = logspace(1, 3, 3)Expected output:
x = [10 100 1000]Generating logarithmic spacing with the default 50 points
f = logspace(1, 4)Expected output:
% f is a 1x50 row vector spanning [10, 10^4]
disp(f(1));
disp(f(end))Requesting a single logarithmic sample
edge = logspace(-3, 2, 1)Expected output:
edge = 100Building a swept frequency vector on the GPU
lo = gpuArray(0);
hi = gpuArray(3);
freq = logspace(lo, hi, 256);
resp = sin(2*pi*freq)Expected output:
% freq and resp remain on the GPU when an acceleration provider is active.
disp(gather(freq(1:5)))Producing complex logarithmic spacing
z = logspace(1+1i, 2+2i, 4)Expected output:
% Complex row vector with magnitudes and phases progressing logarithmically
disp(z)Matching MATLAB's legacy logspace(a, pi, n) syntax
theta = logspace(-1, pi, 5)Expected output:
% Interprets pi literally: theta(end) == 10^pi
disp(theta(end))FAQ
What base does logspace use?
RunMat follows MATLAB and always computes powers of ten. To use another base, build your own sequence (base .^ linspace(...)) or multiply the output by a constant factor.
Does logspace include both endpoints?
Yes. The first element is exactly 10^a and the final element is exactly 10^b, even when rounding errors could otherwise accumulate.
Can I request zero points?
Yes. logspace(a, b, 0) returns a 1 × 0 empty row vector.
What happens when a equals b?
Every element equals 10^a. For example, logspace(2, 2, 4) returns [100 100 100 100].
Does logspace support complex endpoints?
Yes. Real and imaginary components are interpolated independently, and the final values are computed using 10^z = e^{z ln 10}.
Is the third argument required to be an integer?
Yes. Non-integer or negative counts raise an error, matching MATLAB semantics.
Does the function support GPU scalars?
Yes. GPU scalars are gathered once to determine the sequence, and the resulting vector is uploaded back to the device when profitable.
How accurate is the last element?
The implementation explicitly overwrites the final element with 10^b to match MATLAB’s behavior and avoid floating-point drift.
How does logspace differ from linspace?
linspace spaces points linearly between two values, whereas logspace spaces points logarithmically between powers of ten.
Can I generate descending values?
Yes. logspace(3, 1, 4) returns [1000 464.1589 215.4435 100], decreasing logarithmically.
Related functions to explore
These functions work well alongside logspace. Each page has runnable examples you can try in the browser.
linspace, zeros, ones, gpuArray, gather, colon, eye, false, fill, magic, meshgrid, rand, randi, randn, randperm, range, true
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how logspace works, line by line, in Rust.
- View logspace.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.