expm1 — Compute exp(x)-1 element-wise with near-zero accuracy in MATLAB and RunMat.
Y = expm1(X) evaluates exp(X) - 1 element-wise with improved numerical accuracy near zero for real or complex single/double input. Sparse input remains sparse because implicit zeros remain zero, and table or timetable variables are mapped independently.
Syntax
Y = expm1(X)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | Any | Yes | — | Single or double real/complex input; integer, logical, and character forms are RunMat-only extensions. |
Returns
| Name | Type | Description |
|---|---|---|
Y | NumericArray | Elementwise exp(x)-1 result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:expm1:InvalidInput | Input cannot be interpreted as numeric, logical, char, or complex data. | expm1: invalid input |
RunMat:expm1:Internal | Internal tensor construction or provider interaction failed. | expm1: internal error |
How expm1 works
- Dense single/double input preserves class and shape, including complex storage.
- Integer, logical, and character inputs are three independent RunMat-only extensions; each is rejected in MATLAB-compatible mode with its own
RunMat:compatibility:*Extensionidentifier. - The integer extension accepts all eight classes, returns double, and rejects values outside the inclusive exact binary64 interval
[-2^53, 2^53]before conversion. - Complex values follow MATLAB's definition by computing
exp(z) - 1using complex arithmetic. - Sparse single/double input preserves sparse storage because
expm1(0) = 0; table and timetable variables are mapped only when every variable supportsexpm1. - GPU input returns to its original owning provider after direct execution or host fallback; complex-interleaved storage never enters a real unary provider hook.
- Fusion is disabled until the fusion ABI can use the precise expm1 helper instead of the inaccurate
exp(x) - 1expression.
Does RunMat run expm1 on the GPU?
RunMat Accelerate keeps real floating tensors resident through the precise unary_expm1 hook. An explicitly unsupported hook and complex input use an owner-preserving host fallback; other provider errors and malformed or hostile handles are reported as internal errors.
GPU memory and residency
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.
Examples
Maintaining precision for tiny growth rates
x = 1e-12;
y = expm1(x)Expected output:
y = 1.0000000000005e-12Applying 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 output:
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]Computing exp(X)-1 from character codes
C = 'ABC';
Y = expm1(C)Expected output:
Y = [1.6949e+28 4.6072e+28 1.2524e+29]Using expm1 with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how expm1 changes the result.
Run a small expm1 example, explain the result, then change one input and compare the output.
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 double only when the RunMat-only expm1-logical-input extension is enabled; MATLAB-compatible mode rejects that form.
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 through the input's owning provider and computes with the required output class. It restores the result only when the owner physically supports that precision; otherwise the correctly typed result remains on the host.
Can I expect double precision?⌄
Double input returns double and single input returns single. Integer, logical, and character extension forms return double.
How does expm1 interact with fusion?⌄
Fusion is currently disabled so the runtime never replaces precise expm1 with cancellation-prone exp(x) - 1. The direct WGPU unary kernel uses a dedicated precise helper.
Related Math functions
Elementwise
abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · factorial · flintmax · gamma · gammaln · heaviside · hypot · idivide · imag · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · realsqrt · rescale · sign · single · sqrt · swapbytes · times · typecast · uint16 · uint32 · uint8
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · cospi · deg2rad · pol2cart · rad2deg · sin · sind · sinh · sinpi · tan · tand · tanh
Reduction
all · any · bounds · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · maxk · mean · median · min · mink · movmax · movmean · movmedian · movmin · movprod · movstd · movsum · movvar · nnz · prod · rms · std · sum · trapz · var
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Signal
blackman · butter · buttord · cheb2ord · conv · conv2 · deconv · downsample · envelope · filter · filtfilt · fir1 · freqz · gauspuls · hamming · hann · hilbert · periodogram · pulstran · pwelch · rectpuls · resample · sawtooth · sinc · spectrogram · square · tripuls · unwrap · upsample · zplane
Optim
coneprog · fminbnd · fminunc · fsolve · fzero · integral · linprog · lsqcurvefit · lsqnonlin · optimoptions · optimset · quad · secondordercone
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how expm1 is executed, line by line, in Rust.
- View the source for expm1 in Rust on GitHub
- Learn how the RunMat 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 blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.