NumPy linalg.matrix_power: Computing the power of a square matrix

In this tutorial, we are going to learn how to raise a matrix to a given power in linear algebra by using the linalg.matrix_power method in Python present in the NumPy module.

The numpy.linalg.matrix_power() method is used to raise a square matrix to an integer power n. 

Let us see the syntax of the function first.

Also check: Numpy linalg.eig – Compute the eigenvalues and right eigenvectors of a square array


Syntax of numpy.linalg.matrix_power

numpy.linalg.matrix_power(a, n)
  • Parameters:
    • a, an MxM array. Input matrix to be raised to a power.
    • n, integer, the power or exponent. It can be positive, negative or zero.
  • Returns: a**n. The returned matrix is of the same shape as a. If n is positive or zero, then the return type is integer. If n is negative, then the return type is float.
  • Raises: LinAlgError for matrices that are not square or (for negative powers) whose inverse cannot be calculated.

Note: If the determinant of a matrix is zero, then its inverse cannot be calculated.

This function is very similar to the numpy.power(n, p) function which takes two parameters, a number n and a power p and raises n to the power p.


Examples of numpy.linalg.matrix_power

Let’s now start with a few examples of the numpy linalg matrix power method.

Using numpy.linalg.matrix_power with positive power

import numpy as np

matrix = [[2, 5], [1, 3]]
# calculating the matrix power
mat_power_2 = np.linalg.matrix_power(matrix, 2)
mat_power_3 = np.linalg.matrix_power(matrix, 3)


print("Matrix = \n", matrix,
      "\nMatrix power 2 = \n", mat_power_2,
      "\nMatrix power 3 = \n", mat_power_3)

Output:

Matrix = 
 [[2, 5], [1, 3]] 
Matrix power 2 = 
 [[ 9 25]
 [ 5 14]] 
Matrix power 3 = 
 [[ 43 120]
 [ 24  67]]

Matrix raised to power 2 is calculated by multiplying the matrix by itself as follows:

Image
Step 1
Image 1
Step 2
Image 2
Step 3
Image 3
Step 4 – Final result of matrix power 2

The above matrix is the result of matrix power 2. Now, to calculate matrix power 3, we can multiply matrix power 2 by the given matrix. That is,

Image 5
Step 1
Image 6
Step 2
Image 7
Step 3
Image 8
Step 4 – Final result of matrix power 3

Using numpy.linalg.power() with negative power

When we pass a negative power, n, to the function, it first calculates the inverse of the matrix and then raises the inverse to the power abs(n).

For a 2×2 matrix like:

Image 9

The inverse is calculated as:

Image 10
import numpy as np

matrix = [[2, 5], [1, 3]]
# calculating the matrix power
mat_power = np.linalg.matrix_power(matrix, -2)

print("Matrix = \n", matrix, "\nMatrix power -2 = \n", mat_power)

Output:

Matrix = 
 [[2, 5], [1, 3]] 
Matrix power -2 = 
 [[ 14. -25.]
 [ -5.   9.]]

In this example,

Image 11

Its inverse is calculated as follows:

Image 12
Image 13

Now, raise this inverse of the matrix to the power abs(-2) i.e. 2 as shown below:

Image 14
Image 15
Image 16
Image 17
The final result of matrix power -2

Using numpy.linalg.matrix_power with 0

When zero is passed as the power to the numpy.linalg.matrix_power function, an identity matrix of the same shape as the input matrix is returned.

import numpy as np

matrix_1 = [[2, 5], [1, 3]]
matrix_2 = [[4, 2, 5], [1, 8, 3], [6, 0, 2]]
# calculating the matrix power
mat_1_power_0 = np.linalg.matrix_power(matrix_1, 0)
mat_2_power_0 = np.linalg.matrix_power(matrix_2, 0)

print("Matrix 1 = \n", matrix_1, 
      "\nMatrix 1 power 0 = \n", mat_1_power_0, 
      "\nMatrix 2 = \n", matrix_2,
      "\nMatrix 2 power 0 = \n", mat_2_power_0)

Output:

Matrix 1 = 
 [[2, 5], [1, 3]] 
Matrix 1 power 0 = 
 [[1 0]
 [0 1]] 
Matrix 2 = 
 [[4, 2, 5], [1, 8, 3], [6, 0, 2]] 
Matrix 2 power 0 = 
 [[1 0 0]
 [0 1 0]
 [0 0 1]]

As matrix 1 is a 2×2 matrix, the output is a 2×2 identity matrix and similarly, the output of matrix 2 raised to 0 is a 3×3 identity matrix.


Conclusion

So, in this tutorial, we learned about numpy.linalg.matrix_power function which is used in linear algebra to compute the power of a square matrix. We also saw various examples of the possible inputs and outputs.

If you want to learn more about NumPy, feel free to go through our NumPy tutorials.


Reference

Tanvi Bugdani
Tanvi Bugdani
Articles: 63