exp — Compute element-wise exponential values in MATLAB and RunMat.
Y = exp(X) raises *e* to each element of real or complex single/double input. Sparse input becomes dense because each implicit zero maps to one, and table or timetable variables are mapped independently.
Syntax
Y = exp(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 exponential result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:exp:InvalidInput | Input cannot be interpreted as numeric, logical, char, or complex data. | exp: invalid input |
RunMat:exp:Internal | Internal tensor construction or provider interaction failed. | exp: internal error |
How exp works
exp(X)applies the exponential element-by-element while preserving dense single/double class and shape.- 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:
exp(a + bi) = exp(a) * (cos(b) + i·sin(b)). - Sparse single/double input densifies because
exp(0) = 1; table and timetable variables are mapped only when every variable supportsexp. - GPU input returns to its original owning provider after direct execution or host fallback; complex-interleaved storage never enters a real unary provider hook.
Does RunMat run exp on the GPU?
RunMat Accelerate keeps real floating tensors resident through unary_exp. 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
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]Exponentiating character codes
C = 'ABC';
Y = exp(C)Expected output:
Y = [1.6949e+28 4.6072e+28 1.2524e+29]Using exp with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how exp changes the result.
Run a small exp example, explain the result, then change one input and compare the output.
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 double only when the RunMat-only exp-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, producing complex outputs.
What happens when the GPU provider lacks unary_exp?⌄
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.
Related Math functions
Elementwise
abs · angle · bsxfun · complex · conj · double · erf · erfcinv · expm1 · 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 exp is executed, line by line, in Rust.
- View the source for exp 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.