AppDividend
Latest Code Tutorials

Numpy linalg matrix_power: How to Calculate Power of Matrix

0

Numpy linalg.matrix_rank() is used to calculate the power n of a square matrix. What does that mean is that if we have a square matrix M and we have an integer n, then this function is used to calculate Mn?

Numpy linalg matrix_power()

To calculate the power of matrix m, use the np matrix_power() function. The matrix_power() method raises a square matrix to the (integer) power n.

If the value of n=0, then it calculates on the same matrix, and if the value of is n<0, then this function first inverts the matrix and then calculates the power of abs(n).

Syntax

numpy.linalg.matrix_power(M,n)

Parameters

The matrix_power() function takes two parameters:

  • M: This is the square matrix on which we want to operate.
  • n: This is the power value of the matrix.

Return Value

The method returns an integer after calculating the power.

Programming example

Program when the value of N is greater than 0

# Program when the value of N is greater than 0
from numpy import linalg as LA
import numpy as np

arr1 = np.array([[1, 2, 3], [3, 2, 4], [1, 0, 1]])
print("The array is:\n", arr1)
# When n=2
print("Matrix Power is:\n", LA.matrix_power(arr1, 2))

arr2 = np.array([[1, 2], [2, 1]])
print("The array is:\n", arr2)
# When n=1
print("Matrix Power is:\n", LA.matrix_power(arr2, 1))

Output

The array is:
 [[1 2 3]
 [3 2 4]
 [1 0 1]]
Matrix Power is:
 [[10  6 14]
 [13 10 21]
 [ 2  2  4]]
The array is:
 [[1 2]
 [2 1]]
Matrix Power is:
 [[1 2]
 [2 1]]

Explanation

In this example, we have first imported numpy and linalg. Then we have created a matrix of size 3×3, and then we have calculated matrix_power when the value of n is 2.

After that, we have declared one more matrix of size 2×2 and called matrix_power when n=1. We got an array of the same shape after calling the function, the matrix’s power.

Program when the value of N is 0 or less than 0

See the following code.

# Program when the value of N is greater than 0
from numpy import linalg as LA
import numpy as np

arr1 = np.array([[1, 2, 3], [3, 2, 4], [1, 0, 1]])
print("The array is:\n", arr1)
# When n=0
print("Matrix Power is:\n", LA.matrix_power(arr1, 0))

arr2 = np.array([[1, 2], [2, 1]])
print("The array is:\n", arr2)
# When n=-2
print("Matrix Power is:\n", LA.matrix_power(arr2, -2))

Output

The array is:
 [[1 2 3]
 [3 2 4]
 [1 0 1]]
Matrix Power is:
 [[1 0 0]
 [0 1 0]
 [0 0 1]]
The array is:
 [[1 2]
 [2 1]]
Matrix Power is:
 [[ 0.55555556 -0.44444444]
 [-0.44444444  0.55555556]]

Explanation

In this example, we have first imported numpy and linalg. Then we have created a matrix of size 3×3, and then we have calculated matrix_power when the value of n is 0.

In this case, we got a matrix as a result where only one diagonal has value 1, and the rest are 0.
After that, we have declared one more matrix of size 2×2 and called matrix_power when n= -2.

In this case, we got a matrix of the same shape as the given matrix has after calculating the power of n.

That is it for the numpy matrix_power() function.

See also

Numpy linalg cond()

Numpy linalg norm()

Numpy linalg multi_dot()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.