What does the mpower function do in MATLAB / RunMat?
C = mpower(A, B) evaluates the matrix power A^B. For scalars it behaves like ordinary exponentiation, while square matrices use repeated matrix multiplication with MATLAB-compatible rules for identity, errors, and complex promotion.
How does the mpower function behave in MATLAB / RunMat?
Amust be square when it is a matrix. IfAism × nwithm != n, RunMat throwsMatrix must be square for matrix power: m×n.- The exponent
Bmust be a scalar numeric value. Integer-valued doubles are accepted (2.0is treated as2); non-integer exponents raiseMatrix power requires integer exponent. A^0returns the identity matrix of the same size asA. Scalar inputs follow the expected rulex^0 = 1.- Positive integers are evaluated with binary exponentiation (
A^5→A * A * A * A * A). Negative exponents are currently unsupported and produce the MATLAB-style messageNegative matrix powers not supported yet. - Complex scalars are handled exactly; complex matrices are supported so long as the exponent is an integer.
mpower Function GPU Execution Behaviour
- When either operand already resides on the GPU, RunMat asks the active acceleration provider for repeated matrix multiplications using the provider’s
matmulhook. - Identity results (
A^0) are generated witheye_likewhen the provider exposes it; otherwise the runtime fabricates the identity on the host and uploads it. - If the provider declines any step (missing
matmul, unsupported precision, unimplemented identity), RunMat gathers operands to the host, evaluates the CPU reference implementation, and uploads the result back to the device when possible so downstream code can stay on the GPU.
Examples of using the mpower function in MATLAB / RunMat
Raising a matrix to an integer power
A = [1 3; 2 4];
C = mpower(A, 2);
Expected output:
C =
7 15
10 22
Returning the identity matrix when exponent is zero
A = [5 2; 7 1];
I = mpower(A, 0);
Expected output:
I =
1 0
0 1
Scalar bases behave like ordinary exponentiation
result = mpower(4, 0.5);
Expected output:
result = 2
Non-integer exponents raise an error for matrices
A = [1 2; 3 4];
C = mpower(A, 1.5);
Expected output:
Error using mpower
Matrix power requires integer exponent.
Computing matrix powers on the GPU
G = gpuArray([2 0; 0 2]);
H = mpower(G, 3);
gather(H)
Expected output:
ans =
8 0
0 8
GPU residency in RunMat (Do I need gpuArray?)
When a provider with matrix multiplication is active, RunMat keeps inputs and results on the GPU automatically—gpuArray(A)^3 never leaves device memory. If the provider cannot service the request, the runtime gathers to the host, computes the reference result, and (when the outcome is real) uploads it back so the calling code still sees a gpuArray.
FAQ
Does mpower support non-square matrices?
No. mpower requires A to be square. Use A.^B for element-wise powers on non-square arrays.
Can I raise a matrix to a non-integer exponent?
Not yet. RunMat matches MATLAB by requiring the exponent to be an integer-valued scalar for matrix powers. Non-integer exponents raise Matrix power requires integer exponent.
Are negative exponents supported?
Negative matrix exponents will return with Negative matrix powers not supported yet. Use inv(A) manually for now.
How does mpower differ from the element-wise power operator?
mpower performs matrix multiplication repeatedly. Element-wise power (.^) raises each entry independently and supports arbitrary array shapes.
Will results stay on the GPU?
Yes—provided the acceleration provider implements matmul. When it does not, RunMat computes on the host but attempts to re-upload the result so GPU pipelines continue smoothly.
Is there a limit on the exponent magnitude?
Integers up to ±(2³¹−1) are supported. Exponents outside this range trigger a descriptive error.
See Also
mtimes, power, eye, gpuArray, gather
Source & Feedback
- Implementation:
crates/runmat-runtime/src/builtins/math/linalg/ops/mpower.rs - Found a bug or behavioural difference? Please open an issue with a minimal repro.