exp — Element-wise exponential of scalars, vectors, matrices, or N-D tensors.
Y = exp(X) raises *e* to the power of every element in X. Results follow MATLAB semantics for scalars, vectors, matrices, logical arrays, character arrays, and complex inputs.
How exp works in RunMat
exp(X)applies the exponential element-by-element with MATLAB broadcasting rules.- Logical inputs convert to double (
true → 1.0,false → 0.0) before exponentiation. - Character arrays are treated as their numeric code points and return dense double tensors.
- Complex values follow MATLAB's definition:
exp(a + bi) = exp(a) * (cos(b) + i·sin(b)). - Inputs already living on the GPU stay there when the provider implements
unary_exp; otherwise RunMat gathers the data, computes on the host, and returns the correct residency.
How exp runs on the GPU
RunMat Accelerate keeps GPU tensors resident whenever the selected provider implements unary_exp. When the hook is missing or returns an error, RunMat automatically gathers the tensor, computes on the CPU, and re-wraps the result, ensuring correctness without surprising users.
GPU memory and residency
You typically do **not** need to call gpuArray manually in RunMat. The acceleration planner and fusion engine keep tensors on the GPU automatically when profitable. Users can still call gpuArray for explicit residency or to mirror MathWorks MATLAB workflows.
Examples
Calculate the exponential of a scalar value
y = exp(1)Expected output:
y = 2.7183Apply the exponential function to a vector of growth rates
rates = [-1 -0.5 0 0.5 1];
factor = exp(rates)Expected output:
factor = [0.3679 0.6065 1 1.6487 2.7183]Exponentiate every element of a matrix
A = [0 1 2; 3 4 5];
B = exp(A)Expected output:
B = [1.0000 2.7183 7.3891; 20.0855 54.5982 148.4132]Compute the exponential of complex numbers
z = [1+2i, -1+pi*i];
w = exp(z)Expected output:
w = [-1.1312 + 2.4717i, -0.3679 + 0.0000i]Run element-wise exponential on GPU data
G = gpuArray([0 1; 2 3]);
out = exp(G);
result = gather(out)Expected output:
result = [1.0000 2.7183; 7.3891 20.0855]Convert character codes to exponentials
C = 'ABC';
Y = exp(C)Expected output:
Y = [1.6949e+28 4.6072e+28 1.2524e+29]FAQ
When should I use the exp function?
Use exp whenever you need the natural exponential of a value or array, such as modelling growth, discounting continuous compounding, or preparing inputs for activation functions.
Does exp preserve tensor shapes?
Yes. exp returns a tensor with the same shape as the input, applying broadcasting rules where applicable.
How are logical arrays handled?
Logical arrays convert to doubles before exponentiation, matching MATLAB behavior: exp([true false]) returns [e 1].
What about complex inputs?
Complex scalars and tensors use MATLAB's complex exponential formula, producing complex outputs.
What happens when the GPU provider lacks unary_exp?
RunMat automatically gathers the data to the host, computes the result, and returns a dense tensor. Future GPU operations can still fuse because residency metadata is updated accordingly.
Can I expect double precision?
Yes. RunMat stores dense numeric tensors as double precision (f64). Providers may internally use single precision when configured, but results are converted back to double.
Related functions to explore
These functions work well alongside exp. Each page has runnable examples you can try in the browser.
logspace, abs, sin, gpuArray, gather, angle, conj, double, expm1, factorial, gamma, hypot, imag, ldivide, log, log10, log1p, 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 exp works, line by line, in Rust.
- View exp.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.