Numpy Matrix Power
numpy.linalg.matrix_power
Raise a square matrix to the (integer) power n.
For positive integers 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 is computed and then raised to the abs(n).
matrix power numpy
import numpy as np x = np.arange(9).reshape(3,3) y = np.matrix(x) a = y**3 b = np.linalg.matrix_power(x, 3) print a print b assert np.all(a==b)
In [19]: a Out[19]: matrix([ [ 180, 234, 288], [ 558, 720, 882], [ 936, 1206, 1476] ]) In [20]: b Out[20]: array([ [ 180, 234, 288], [ 558, 720, 882], [ 936, 1206, 1476] ])
numpy raise matrix to power
from numpy.linalg import matrix_power i = np.array([[0, 1], [-1, 0]]) matrix_power(i, 3) array([ [ 0, -1], [ 1, 0] ])
matrix_power(i, 0) array([ [1, 0], [0, 1] ])