What does the exp function do in MATLAB / RunMat?
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 does the exp function behave in MATLAB / 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.
exp Function GPU Execution Behaviour
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.
Examples of using the exp function in MATLAB / RunMat
Calculate the exponential of a scalar value
y = exp(1);
Expected output:
y = 2.7183;
Apply 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 behavior:
result = [1.0000 2.7183; 7.3891 20.0855];
Convert character codes to exponentials
C = 'ABC';
Y = exp(C);
Expected output:
Y = [5.9874e+41 1.2946e+42 2.7992e+42];
GPU residency in RunMat (Do I need gpuArray?)
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.
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.
See Also
logspace, abs, sin, gpuArray, gather
Source & Feedback
- The full source code for the implementation of the
expfunction is available at:crates/runmat-runtime/src/builtins/math/elementwise/exp.rs - Found a bug or behavioral difference? Please open an issue with details and a minimal repro.