scipy.sparse.linalg.matrix_power#

scipy.sparse.linalg.matrix_power(A, power)[source]#

Raise a square matrix to the integer power, power.

For non-negative integers, A**power is computed using repeated matrix multiplications. Negative integers are not supported.

Parameters:
A(M, M) square sparse array or matrix

sparse array that will be raised to power power

powerint

Exponent used to raise sparse array A

Returns:
A**power(M, M) sparse array or matrix

The output matrix will be the same shape as A, and will preserve the class of A, but the format of the output may be changed.

Notes

This uses a recursive implementation of the matrix power. For computing the matrix power using a reasonably large power, this may be less efficient than computing the product directly, using A @ A @ … @ A. This is contingent upon the number of nonzero entries in the matrix.

Added in version 1.12.0.

Examples

>>> from scipy import sparse
>>> A = sparse.csc_array([[0,1,0],[1,0,1],[0,1,0]])
>>> A.todense()
array([[0, 1, 0],
       [1, 0, 1],
       [0, 1, 0]])
>>> (A @ A).todense()
array([[1, 0, 1],
       [0, 2, 0],
       [1, 0, 1]])
>>> A2 = sparse.linalg.matrix_power(A, 2)
>>> A2.todense()
array([[1, 0, 1],
       [0, 2, 0],
       [1, 0, 1]])
>>> A4 = sparse.linalg.matrix_power(A, 4)
>>> A4.todense()
array([[2, 0, 2],
       [0, 4, 0],
       [2, 0, 2]])