RunMat
GitHub

expm1 — Accurate element-wise computation of exp(x) - 1 for scalars, vectors, matrices, or N-D tensors.

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 expm1 works in 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) - 1 using 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.

How expm1 runs on the GPU

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.

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-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 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]

Applying expm1 to character data

C = 'ABC';
Y = expm1(C)

Expected output:

Y = [1.6949e+28 4.6072e+28 1.2524e+29]

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.

These functions work well alongside expm1. Each page has runnable examples you can try in the browser.

exp, log1p, sin, gpuArray, gather, abs, angle, conj, double, factorial, gamma, hypot, imag, ldivide, log, log10, log2, minus, plus, pow2, power, rdivide, real, sign, single, sqrt, times

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how expm1 works, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.