What does the logspace function do in MATLAB / RunMat?
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 does the logspace function behave in MATLAB / 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.
logspace Function GPU Execution Behavior
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.
Examples of using the logspace function in MATLAB / RunMat
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 behavior:
% 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 = 100;
Building a swept frequency vector on the GPU
lo = gpuArray(0);
hi = gpuArray(3);
freq = logspace(lo, hi, 256);
resp = sin(2*pi*freq);
Expected behavior:
% 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 behavior:
% Interprets pi literally: theta(end) == 10^pi
disp(theta(end));
GPU residency in RunMat (Do I need gpuArray?)
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.
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.
See Also
linspace, zeros, ones, gpuArray, gather
Source & Feedback
- The full source code for the implementation of the
logspacefunction is available at:crates/runmat-runtime/src/builtins/array/creation/logspace.rs - Found a bug or behavioral difference? Please open an issue with details and a minimal repro.