power — Compute element-wise exponentiation in MATLAB and RunMat.
power(A, B) (or A .^ B) raises each element of A to the corresponding element of B. Implicit expansion, complex results, and domain behavior follow MATLAB semantics.
Syntax
C = power(A, B)
C = power(A, B, "like", prototype)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Base operand. |
B | Any | Yes | — | Exponent operand. |
like | StringScalar | Yes | — | Literal string "like". |
prototype | LikePrototype | Yes | — | Output class/device prototype. |
Returns
| Name | Type | Description |
|---|---|---|
C | NumericArray | Elementwise power result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:power:InvalidArgument | Optional arguments are malformed or unsupported. | power: invalid argument |
RunMat:power:InvalidInput | Operands or prototypes cannot be converted into supported numeric/logical/complex forms. | power: invalid input |
RunMat:power:SizeMismatch | Operands are not broadcast-compatible. | power: array sizes are not compatible for broadcasting |
RunMat:power:Internal | Provider interaction, gather/upload, or internal tensor construction failed. | power: internal error |
How power works
- Supports scalars, vectors, matrices, and N-D tensors with the same shape or compatible singleton dimensions. Size mismatches raise the standard MATLAB error.
- When either operand is a scalar symbolic expression,
powerand scalar^construct a symbolic power expression that can participate in symbolic functions such aslimit. - Logical and character inputs are promoted to double precision before powering (
'A'.^2uses the Unicode code points of the characters). - Complex bases and/or exponents follow the analytic identity
z.^w = exp(w * log(z)), so negative bases with fractional exponents return complex results instead ofNaN. - Empty tensors propagate emptiness; the result uses the broadcasted size.
- Integer bases preserve their class, require nonnegative integer-valued exponents, and use saturating exponentiation; scalar-double compatibility follows the exact 64-bit rule.
- In RunMat compatibility mode, the RunMat-only optional
'like', prototypearguments mirror the prototype's numeric flavour and residency. MATLAB-compatible mode rejects this extension before dispatch.
Does RunMat run power on the GPU?
When both operands are gpuArrays with identical shapes, RunMat calls the provider's elem_pow hook. The WGPU backend uses a fused WGSL kernel, and the in-process provider executes on host data without leaving the GPU abstraction.
If only one operand lives on the GPU and the other is a scalar, RunMat materialises a device buffer for the scalar and still uses elem_pow.
Implicit expansion, complex operands, and providers that lack elem_pow automatically fall back to the host implementation. Results respect 'like' residency hints, re-uploading to the GPU when requested.
GPU memory and residency
Most workflows do not require manual gpuArray calls. RunMat's auto-offload and fusion planner keep chains of element-wise operations on the GPU whenever the provider can satisfy them. When an operation needs a fallback (implicit expansion, complex inputs, or unsupported kernels), RunMat transparently gathers to the host, computes the MATLAB-accurate result, and honours any 'like' residency hints you supplied.
Examples
Raise a scalar to a power
y = power(2, 5)Expected output:
y = 32Compute element-wise powers of a matrix
A = [1 2 3; 4 5 6];
B = power(A, 2)Expected output:
B =
1 4 9
16 25 36Broadcast exponents across rows
base = (1:3)';
exponent = [1 2 3];
result = power(base, exponent)Expected output:
result =
1 1 1
2 4 8
3 9 27Generate complex powers from negative bases
values = power([-2 -1 0 1 2], 0.5)Expected output:
values = [0.0000 + 1.4142i, 0.0000 + 1.0000i, 0, 1, 1.4142]Keep GPU results with a 'like' prototype
proto = gpuArray.zeros(1, 1, 'single');
x = [1 2 3];
y = [2 3 4];
devicePowers = power(x, y, 'like', proto);
result = gather(devicePowers)Expected output:
devicePowers =
1x3 gpuArray single
1 8 81
result =
1 8 81Convert character codes before powering
codes = power('ABC', 2)Expected output:
codes = [4225 4356 4489]Using power with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how power changes the result.
Run a small power example, explain the result, then change one input and compare the output.
FAQ
Does power support MATLAB implicit expansion?⌄
Yes. Singleton dimensions expand automatically, and size mismatches raise a dimension error with the usual MATLAB wording.
What numeric type does power return?⌄
Ordinary real inputs produce double, and results promote to complex double when exponentiation leaves the real line. Integer bases preserve their class, accept only nonnegative integer-valued exponents, and saturate overflow.
Can I mix scalars and arrays?⌄
Absolutely. Scalars broadcast to match the other operand. This includes scalar gpuArrays.
What happens if only one operand is on the GPU?⌄
If the other operand is a scalar, RunMat keeps everything on the GPU. Otherwise, it gathers the device operand, performs the computation on the host, and returns a host tensor (unless 'like' instructs the runtime to re-upload the result).
Does power modify the inputs in-place?⌄
No. The builtin always allocates a fresh tensor (or complex tensor). Fusion can eliminate temporary allocations when the expression continues with other element-wise operations.
Can I force the result to stay on the GPU?⌄
Yes—pass 'like', gpuArrayPrototype. The runtime mirrors the residency of the prototype and uploads the result when necessary.
Related Math functions
Elementwise
abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · expm1 · factorial · flintmax · gamma · gammaln · heaviside · hypot · idivide · imag · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · 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 power is executed, line by line, in Rust.
- View the source for power 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.