Power of a matrix

5. Power of a matrix#

In many situations, we have to continually apply (i.e., multiply by) a matrix multiple times. Here we will demonstrate how to compute the power of a matrix, which we denote as \(A^n\) (multiply by \(A\) a total of \(n\) times).

Caution

For \(A^n\), we are performing matrix multiplication \(n\) times, not raising each element of \(A\) to the \(n\)th power.

Summary of commands#

In this exercise, we will demonstrate the following:

  • np.linalg.matrix_power(M, n) - Raise a square matrix M to the n power.

  • np.diag(M, k)

    • If M is a 2D array, extract the kth diagonal elements as a 1D array.

    • If M is a 1D array, insert it into the k diagonal of a zero matrix.

Demo#

Given the matrix \(A = \begin{bmatrix} 2 & 9 \\ 4 & -3 \end{bmatrix}\), calculate \(A^5\) directly, and then using the diagonalization method.

# import libraries
import numpy as np

# initialize
A = np.array([[2, 9], [4, -3]])

# direct computation
res = np.linalg.matrix_power(A, 5)   # do NOT use ** !
print(res)

# diagonalization
D, V = np.linalg.eig(A)
res = V @ np.diag(D ** 5) @ np.linalg.inv(V)
print(res)
[[  212 17019]
 [ 7564 -9243]]
[[  212. 17019.]
 [ 7564. -9243.]]