我要评分
获取效率
正确性
完整性
易理解

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 is computed and then raised to 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.

  • If the exponent is positive or zero, the type of the elements is the same as that of M.
  • If the exponent is negative, the elements are floating-point.

Examples

>>> 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.]])
>>>