Raise a square matrix to the power n in Linear Algebra in Python

To raise a square matrix to the power n in Linear Algebra, use the numpy.linalg.matrix_power() function in Python. 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 is returned. If n < 0, the inverse is computed and then raised to the abs(n).

Syntax

numpy.linalg.matrix_power(a, n)

Parameters

The function accepts the following parameters ?

  • a − A square matrix to be raised to the power
  • n − The exponent that can be any integer (positive, negative, or zero)

Return Value

Returns a matrix of the same shape and type as the input. For positive or zero exponents, the element type matches the input. For negative exponents, elements are floating-point.

Example 1: Matrix to Power 0 (Identity Matrix)

When raising any matrix to the power 0, the result is always the identity matrix ?

import numpy as np
from numpy.linalg import matrix_power

# Create a 2D array, matrix equivalent of the imaginary unit
arr = np.array([[0, 1], [-1, 0]])

print("Original Matrix:")
print(arr)

print("\nMatrix to power 0 (Identity):")
print(matrix_power(arr, 0))
Original Matrix:
[[ 0  1]
 [-1  0]]

Matrix to power 0 (Identity):
[[1 0]
 [0 1]]

Example 2: Matrix to Positive Powers

Let's see how a matrix behaves when raised to different positive powers ?

import numpy as np
from numpy.linalg import matrix_power

# Create a simple 2x2 matrix
matrix = np.array([[2, 1], [0, 3]])

print("Original Matrix:")
print(matrix)

print("\nMatrix to power 1:")
print(matrix_power(matrix, 1))

print("\nMatrix to power 2:")
print(matrix_power(matrix, 2))

print("\nMatrix to power 3:")
print(matrix_power(matrix, 3))
Original Matrix:
[[2 1]
 [0 3]]

Matrix to power 1:
[[2 1]
 [0 3]]

Matrix to power 2:
[[4 5]
 [0 9]]

Matrix to power 3:
[[ 8 17]
 [ 0 27]]

Example 3: Matrix to Negative Powers

For negative powers, the function computes the inverse first, then raises it to the absolute value of the exponent ?

import numpy as np
from numpy.linalg import matrix_power

# Create an invertible matrix
matrix = np.array([[4, 2], [1, 3]], dtype=float)

print("Original Matrix:")
print(matrix)

print("\nMatrix to power -1 (Inverse):")
print(matrix_power(matrix, -1))

print("\nMatrix to power -2:")
print(matrix_power(matrix, -2))
Original Matrix:
[[4. 2.]
 [1. 3.]]

Matrix to power -1 (Inverse):
[[ 0.3 -0.2]
 [-0.1  0.4]]

Matrix to power -2:
[[ 0.13 -0.14]
 [-0.07  0.18]]

Key Points

  • The input matrix must be square (same number of rows and columns)
  • For negative powers, the matrix must be invertible (non-singular)
  • Power 0 always returns the identity matrix
  • Negative powers result in floating-point elements

Conclusion

The numpy.linalg.matrix_power() function provides an efficient way to raise square matrices to any integer power. Use it for computing matrix powers in linear algebra operations, with automatic handling of identity matrices for power 0 and matrix inversion for negative powers.

Updated on: 2026-03-26T20:13:25+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements