linalg.matrix_power
Description
For positive integer n, the power is computed by repeated matrix squarings and matrix multiplications.
- If n == 0, the identity matrix of the same shape as M is returned.
- If n < 0, the inverse of a matrix is computed and then raised to the power of abs(n).
Mandatory Input Parameters
Parameter |
Type |
Description |
|---|---|---|
a |
(…,M,M) array_like |
Matrix to be "powered" |
n |
int |
The exponent can be any integer or long integer, positive, negative, or zero. |
Optional Input Parameters
None
Return Value
Type |
Description |
|---|---|
(…, M, M) ndarray or matrix object |
The shape and type of the return value are the same as those of M.
|
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | >>> import numpy as np >>> a = np.array([[0,1], [-1,0]]) >>> np.linalg.matrix_power(a, 3) array([[ 0, -1], [ 1, 0]]) >>> >>> np.linalg.matrix_power(a, 0) array([[1, 0], [0, 1]]) >>> >>> np.linalg.matrix_power(a, -3) array([[ 0., 1.], [-1., 0.]]) >>> >>> q = np.zeros((4,4)) >>> q[0:2, 0:2] = -a >>> q[2:4, 2:4] = a >>> a array([[ 0, 1], [-1, 0]]) >>> >>> np.linalg.matrix_power(q, 2) array([[-1., 0., 0., 0.], [ 0., -1., 0., 0.], [ 0., 0., -1., 0.], [ 0., 0., 0., -1.]]) >>> |
Parent topic: Linear Algebra Functions