MatrixPower¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
MatrixPower[m, n]
gives the n-th matrix power of the square matrix m. MatrixPower[m, n, v] gives the n-th matrix power of the matrix m applied to the vector v. When n is negative, MatrixPower finds powers of the inverse of the matrix m. MatrixPower[m, 0] gives IdentityMatrix[Length[m]]. Fractional matrix powers are not currently supported.
Examples (17)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (10)¶
In[1]:= MatrixPower[{{a, b}, {c, d}}, 2]
Out[1]= {{a^2 + b c, a b + b d}, {a c + c d, b c + d^2}}
In[2]:= MatrixPower[{{a, b}, {c, d}}, 2] == {{a, b}, {c, d}} . {{a, b}, {c, d}}
Out[2]= True
In[3]:= MatrixPower[{{a, b}, {c, d}}, -2] == Inverse[{{a, b}, {c, d}}] . Inverse[{{a, b}, {c, d}}]
Out[3]= True
In[4]:= MatrixPower[{{1, 1}, {1, 2}}, 10]
Out[4]= {{4181, 6765}, {6765, 10946}}
In[5]:= MatrixPower[{{1.2, 2.5, -3.2}, {0.7, -9.4, 5.8}, {-0.2, 0.3, 6.4}}, 5]
Out[5]= {{-1208.61, 19598.2, -12658.4}, {5784.51, -83315.1, 35420.6}, {-559.11, 1960.12, 11511.9}}
In[6]:= MatrixPower[{{2, 3, 0}, {4, 9, 0}, {0, 0, 4}}, 14]
Out[6]= {{25881337259836, 54508871401413, 0}, {72678495201884, 153068703863133, 0}, {0, 0, 268435456}}
In[7]:= MatrixPower[{{a, b}, {0, c}}, 4]
Out[7]= {{a^4, a^2 (a b + b c) + c^2 (a b + b c)}, {0, c^4}}
In[8]:= MatrixPower[{{1, 1}, {1, 2}}, 3, {1, 0}]
Out[8]= {5, 8}
In[9]:= MatrixPower[{{1, 2}, {3, 4}}, 0]
Out[9]= {{1, 0}, {0, 1}}
In[10]:= MatrixPower[{{5}}, -2]
Out[10]= {{1/25}}
Applications (7)¶
In[11]:= MatrixPower[{{1, 1}, {0, 1}}, 3]
Out[11]= {{1, 3}, {0, 1}}
In[12]:= MatrixPower[{{2, 0}, {0, 3}}, 2]
Out[12]= {{4, 0}, {0, 9}}
In[13]:= MatrixPower[{{2, 0}, {0, 4}}, -1]
Out[13]= {{1/2, 0}, {0, 1/4}}
In[14]:= MatrixPower[{{2, 0}, {0, 3}}, 2, {1, 1}]
Out[14]= {4, 9}
In[15]:= MatrixPower[{{2, 1}, {0, 2}}, -2]
Out[15]= {{1/4, -1/4}, {0, 1/4}}
In[16]:= MatrixPower[{{0, 1}, {1, 1}}, 10]
Out[16]= {{34, 55}, {55, 89}}
In[17]:= MatrixPower[{{0, 1}, {1, 1}}, 10][[1, 2]] == Fibonacci[10]
Out[17]= True
Implementation notes¶
Algorithm. builtin_matrixpower computes MatrixPower[m, e] (and MatrixPower[m, e, v]) for integer exponents by binary exponentiation (square-and-multiply). After validating that m is a non-empty square rank-2 tensor (MatrixPower::matsq) and that the exponent is an Integer or BigInt that fits in int64_t, it loops over the bits of |e|, accumulating the product and repeatedly squaring the running matrix; each matrix product goes through dot2 (the Dot kernel, via the local mat_dot wrapper) followed by a full evaluate pass so entries simplify. e == 0 returns IdentityMatrix[n]. Negative exponents first compute Inverse[m] (in inv.c) as the base; if Inverse returns unevaluated (singular), the call is abandoned. If a third vector argument is supplied, the final matrix is dotted with it.
Data structures / limits. Operates directly on Expr* List-of-List matrices; no dense numeric buffer. Fractional exponents (Rational or Real) are explicitly unsupported — they emit MatrixPower::fract and return unevaluated rather than going through an eigendecomposition; symbolic and BigInt-too-large exponents also return unevaluated. Cost is O(log|e|) matrix multiplies.
Protected.MatrixPower[m, n]effectively evaluates the product of a matrix with itselfntimes.- When
nis negative,MatrixPowerfinds powers of the inverse of the matrixm. MatrixPower[m, 0]returnsIdentityMatrix[Length[m]].MatrixPowerworks only on square matrices.- Uses binary exponentiation (repeated squaring) for efficient computation.
- A machine-real matrix stays a packed list throughout,
so each squaring is one
Dotand reaches BLASdgemm:MatrixPower[A, 4]on a 300 x 300 costs 0.69 ms, the two matrix products it is. An integer matrix is materialised first and stays exact, because the exact fourth power of an integer matrix is an integer matrix and a float64 buffer cannot hold one past 2^53. - Fractional matrix powers are not currently supported and generate a warning.
Attributes: Protected.
References¶
See also: Dot
- R. A. Horn and C. R. Johnson, Matrix Analysis, 2nd ed., Cambridge University Press, 2013 — powers of matrices.
- G. H. Golub and C. F. Van Loan, Matrix Computations, 4th ed., Johns Hopkins University Press, 2013 — repeated multiplication and inversion.
- Source:
src/linalg/matpow.c - Specification:
docs/spec/builtins/linear-algebra.md - Tests:
tests/test_compile_linalg.c - Tests:
tests/test_linalg.c - Tests:
tests/test_packed_list.c - Tests:
tests/test_parallelmixedtower.c
Notes & additional examples¶
Notes¶
MatrixPower[m, n] computes the n-th power by repeated matrix multiplication. A negative exponent raises the inverse to the corresponding positive power, so it requires a non-singular matrix; MatrixPower[m, 0] returns the identity matrix of the right size. The three-argument form MatrixPower[m, n, v] applies the matrix power to a vector — equivalent to MatrixPower[m, n] . v but without forming the full power matrix. The last example is the classic identity for the Fibonacci Q-matrix: the powers of {{0, 1}, {1, 1}} carry consecutive Fibonacci numbers, with the off-diagonal entry of the n-th power equal to Fibonacci[n]. Because the arithmetic is exact (with automatic GMP bignum promotion), these powers stay exact for arbitrarily large n. Fractional exponents are not currently supported.