Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 62 ยท Eigenvalues and Decompositions

Diagonalization and Matrix Powers

Learn diagonalization A = P D P^-1: compute matrix powers fast, get Fibonacci numbers from eigenvalues, and spot defective matrices that cannot be diagonalized.

A diagonal matrix is the friendliest matrix there is. Multiplying by it just scales each coordinate separately, and raising it to the 100th power means raising each diagonal entry to the 100th power. Diagonalization is the trick of rewriting an ordinary matrix so that, seen from the right point of view, it behaves like a diagonal one.

The point of view comes from eigenvectors, which you met in Eigenvalues and Eigenvectors. If a square matrix A has enough independent eigenvectors, you can stack them as the columns of a matrix P and put the matching eigenvalues on the diagonal of D. Then A = P D P^-1. This lesson shows why that works, uses it to compute huge matrix powers cheaply, and explains the matrices where it fails.

Diagonalize in four steps

Find eigenvalues Roots of the characteristic polynomial.
Find eigenvectors One independent vector per direction.
Build P and D Vectors as columns, values on the diagonal.
Check P D P^-1 should give A back.

If step 2 cannot find enough vectors, the matrix is defective.

1 / 2

The factorization A = P D P^-1

Suppose A is an n x n matrix with n linearly independent eigenvectors v1, ..., vn and eigenvalues l1, ..., ln. Put the eigenvectors in the columns of P and the eigenvalues on the diagonal of D. Multiplying A by P applies A to every eigenvector at once, and each one only gets scaled:

A P = P D

Because the columns of P are independent, P is invertible (Inverse Matrices), so you can multiply by P^-1 on the right and get A = P D P^-1.

You can read that product from right to left as a story. P^-1 converts a vector into eigen-coordinates (a change of basis), D scales each eigen-coordinate independently, and P converts back. The matrix A is the same transformation as D, just described in a different basis.

NumPy's np.linalg.eig hands you D (as a plain list of eigenvalues) and P in one call. The order of eigenvalues is not guaranteed, and eigenvectors may come back with either sign, so the code below checks properties instead of printing raw vectors.

Python Build P and D, then rebuild A

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Matrix powers made cheap

Look what happens when you multiply A by itself:

A^2 = (P D P^-1)(P D P^-1) = P D (P^-1 P) D P^-1 = P D^2 P^-1

The inner P^-1 P cancels to the identity, and the same cancellation happens at every step. So for any whole number k:

A^k = P D^k P^-1

And D^k is trivial: raise each diagonal entry to the power k. In NumPy that is values ** k, with no matrix multiplication at all. Once you have P and its inverse, any power costs the same, whether k is 5 or 5 million. This is also how you can define things like a matrix square root: take the square root of each eigenvalue (if they are non-negative).

Python A to the 10th power two ways

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last lines show the idea behind growth models: when you keep applying A, the eigenvalue with the largest absolute value takes over, so every entry of A^k grows by roughly that factor per step. Here the eigenvalues are 2 and 5, so the ratios settle near 5.

Example: Fibonacci from eigenvalues

The Fibonacci numbers satisfy F(n+1) = F(n) + F(n-1). Write the pair of consecutive terms as a vector, and one step becomes a matrix multiplication:

[F(n+1), F(n)] = [[1, 1], [1, 0]] @ [F(n), F(n-1)]

So after n steps the pair is M^n applied to [F(1), F(0)] = [1, 0], and F(n) is an entry of M^n. Diagonalizing M turns this into a closed formula. The eigenvalues of M solve l^2 = l + 1, which gives the golden ratio (1 + sqrt(5)) / 2 (about 1.618) and its partner (1 - sqrt(5)) / 2 (about -0.618). The second one is smaller than 1 in size, so its powers vanish, and F(n) is close to golden^n / sqrt(5).

Python Fibonacci with a diagonalized matrix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For small n the two columns match exactly. The eigenvalue version works in floating point, which keeps about 15 or 16 significant digits, so once the numbers grow past that size it can no longer be exact (the last line shows a mismatch for n = 80). Integer arithmetic stays exact, so for a real program use the loop or Python's big integers; the eigenvalue formula is valuable for understanding growth and for fast matrix powers in settings where approximate numbers are fine.

Matrix powers also solve linear recurrences of any order and describe how a Markov chain evolves. You will use that idea again in the PageRank project.

When diagonalization fails

An n x n matrix is diagonalizable exactly when it has n independent eigenvectors. Two situations are common:

  • Distinct eigenvalues. If all n eigenvalues are different, the matrix is always diagonalizable, because eigenvectors for different eigenvalues are independent.
  • Repeated eigenvalues. Now it depends. The identity matrix has the eigenvalue 1 repeated, yet every vector is an eigenvector, so it is perfectly diagonalizable (it is already diagonal). But a shear matrix like [[1, 1], [0, 1]] also has the eigenvalue 1 twice and only one line of eigenvectors. Such a matrix is called defective: there are not enough eigenvectors to form P.

A defective matrix has no factorization P D P^-1. You can see the symptom numerically: eig still returns a matrix, but its two columns point in almost the same direction, so P is nearly singular and its condition number is enormous. Always check the conditioning (or at least np.allclose(P @ D @ inv(P), A)) before trusting a diagonalization of a matrix with repeated eigenvalues.

Python A defective matrix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The shear has one genuine eigenvector direction (the x-axis), and eig returns a second column that is only a numerical echo of the first. The identity has the same repeated eigenvalue but two independent eigenvectors, so its P is perfectly conditioned.

Defective matrices are not a dead end. The Jordan form and the SVD handle them, and the next two lessons introduce the cleanest case: symmetric matrices, which are always diagonalizable, and the SVD, which works for every matrix.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In A = P D P^-1, what do the columns of P contain?

    P holds one eigenvector per column and D holds the matching eigenvalues, so A P = P D.
  2. 2. If A = P D P^-1, how do you compute A^10?

    The inner P^-1 P pairs cancel, leaving P D^10 P^-1, and D^10 just raises each diagonal entry to the 10th power.
  3. 3. Which matrix is defective?

    The shear [[1, 1], [0, 1]] has the eigenvalue 1 twice but only one line of eigenvectors, so it cannot be diagonalized.
  4. 4. A 3 x 3 matrix has three different eigenvalues. What can you say?

    Eigenvectors that belong to different eigenvalues are independent, so three distinct eigenvalues give three independent eigenvectors.

Summary

  • A square matrix with n independent eigenvectors factors as A = P D P^-1, with eigenvectors in P and eigenvalues on the diagonal of D.
  • Powers become cheap: A^k = P D^k P^-1, and D^k only powers the diagonal entries.
  • The eigenvalue of largest absolute value controls long-run growth, which explains Fibonacci growth and many dynamic models.
  • Distinct eigenvalues guarantee diagonalizability; repeated eigenvalues may give a defective matrix with too few eigenvectors.
  • A nearly singular P (huge np.linalg.cond(P)) is the numerical sign of a defective matrix.

Next, meet the best-behaved matrices of all in Symmetric Matrices and the Spectral Theorem.