What does the expm1 function do in MATLAB / RunMat?
Y = expm1(X) evaluates exp(X) - 1 element-wise while maintaining high accuracy for values of
X that are close to zero. It mirrors MATLAB semantics across scalars, vectors, matrices, logical
arrays, character arrays, and complex inputs.
How does the expm1 function behave in MATLAB / RunMat?
- Logical inputs are promoted to double precision (
true -> 1.0,false -> 0.0) before execution. - Character arrays are interpreted as their numeric code points and return dense double tensors.
- Complex values follow MATLAB's definition by computing
exp(z) - 1using complex arithmetic. - Existing GPU tensors remain on the device when the registered provider implements
unary_expm1; otherwise RunMat gathers the data, computes on the CPU, and returns the result.
expm1 Function GPU Execution Behaviour
RunMat Accelerate keeps tensors resident on the GPU whenever the provider exposes the
unary_expm1 hook. When the hook is missing or errors, RunMat automatically gathers the tensor,
performs the computation on the host using f64::expm1 for the real components, and returns the
result while preserving residency metadata. This ensures users always observe MATLAB-compatible
behaviour without manual residency management.
Examples of using the expm1 function in MATLAB / RunMat
Maintaining precision for tiny growth rates
x = 1e-12;
y = expm1(x);
Expected output:
y = 1.0000000000005e-12;
Applying expm1 to model percentage growth
rates = [-0.10 -0.05 0 0.05 0.10];
factors = expm1(rates);
Expected output:
factors = [-0.0952 -0.0488 0 0.0513 0.1052];
Running expm1 on GPU arrays
G = gpuArray(linspace(-1, 1, 5));
result = expm1(G);
out = gather(result);
Expected behaviour:
out = [-0.6321 -0.3935 0 0.6487 1.7183];
Using expm1 with complex numbers
z = [1+1i, -1+pi*1i];
w = expm1(z);
Expected output:
w = [0.4687 + 2.2874i, -1.3679 + 0.0000i];
Applying expm1 to character data
C = 'ABC';
Y = expm1(C);
Expected output:
Y = [1.6949e+28 4.6072e+28 1.2524e+29];
GPU residency in RunMat (Do I need gpuArray?)
In most workflows you do not need to call gpuArray manually. RunMat's auto-offload planner
and fusion engine keep data on the GPU when beneficial. You can still call gpuArray to mirror
MathWorks MATLAB workflows or to pin data on the device explicitly.
FAQ
When should I prefer expm1 over exp(x) - 1?
Use expm1 whenever x can be very close to zero. It avoids catastrophic cancellation and matches
MATLAB's high-accuracy results for tiny magnitudes.
Does expm1 change my tensor's shape?
No. The output has the same shape as the input, subject to MATLAB broadcasting semantics.
How are logical arrays handled?
Logical values convert to doubles before applying expm1, so expm1([true false]) yields a
double array [e - 1, 0].
What about complex inputs?
Complex scalars and tensors use MATLAB's complex exponential formula and subtract one from the result, keeping both real and imaginary parts accurate.
What happens if the GPU provider lacks unary_expm1?
RunMat gathers the tensor to the host, computes with double precision, and returns the correct result. Subsequent fused kernels still see accurate residency metadata.
Can I expect double precision?
Yes. RunMat stores dense numeric tensors in double precision (f64). GPU providers may choose
single precision internally but convert back to double when returning data to the runtime.
How does expm1 interact with fusion?
The fusion planner recognises expm1 as an element-wise op. Providers that support fused kernels
can materialise expm1 directly in generated WGSL.
See Also
exp, log1p, sin, gpuArray, gather
Source & Feedback
- The full source code for the implementation of the
expm1function is available at:crates/runmat-runtime/src/builtins/math/elementwise/expm1.rs - Found a bug or behavioural difference? Please open an issue with details and a minimal repro.