This is a pre-release version (latest). Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy latest / numpy / linalg / matrix_power

_ArrayFunctionDispatcher

numpy.linalg:matrix_power

source: build-install/usr/lib/python3.14/site-packages/numpy/linalg/_linalg.py :656

Signature

def   matrix_power ( a n )

Summary

Raise a square matrix to the (integer) power n.

Extended Summary

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

Parameters

a : (..., M, M) array_like

Matrix to be "powered".

n : int

The exponent can be any integer or long integer, positive, negative, or zero.

Returns

a**n : (..., M, M) ndarray or matrix object

The return value is the same shape and type as M; if the exponent is positive or zero then the type of the elements is the same as those of M. If the exponent is negative the elements are floating-point.

Raises

: LinAlgError

For matrices that are not square or that (for negative powers) cannot be inverted numerically.

Examples

import numpy as np
from numpy.linalg import matrix_power
i = np.array([[0, 1], [-1, 0]]) # matrix equiv. of the imaginary unit
matrix_power(i, 3) # should = -i
matrix_power(i, 0)
matrix_power(i, -3) # should = 1/(-i) = i, but w/ f.p. elements
Somewhat more sophisticated example
q = np.zeros((4, 4))
q[0:2, 0:2] = -i
q[2:4, 2:4] = i
q # one of the three quaternion units not equal to 1
matrix_power(q, 2) # = -np.eye(4)

Aliases

  • numpy.linalg.matrix_power