20. Diagonalization, when a matrix is diagonalizable

📖 Reading · 9 min
💡 Every code box below is live — edit it and hit Run.

§19.0 found eigenvalues and eigenvectors one at a time. This lesson collects them into a single change of basis (§17.3) that turns A into a diagonal matrix — the concrete payoff promised back in §18.4, and the single most useful computational trick in this course.

The diagonalization formula

Suppose A (an n\times n matrix) has n linearly independent eigenvectors \vec v_1,\dots,\vec v_n with eigenvalues \lambda_1,\dots,\lambda_n (not necessarily distinct). Let P=[\vec v_1\ \cdots\ \vec v_n] (eigenvectors as columns) and D=\operatorname{diag}(\lambda_1,\dots,\lambda_n). Then

A=PDP^{-1}

Derivation: AP=A[\vec v_1\ \cdots\ \vec v_n]=[A\vec v_1\ \cdots\ A\vec v_n]=[\lambda_1\vec v_1\ \cdots\ \lambda_n\vec v_n]=PD (§16.2's column-by-column view of multiplication, applied twice), so AP=PD\Rightarrow A=PDP^{-1}, using that P is invertible because its columns — the eigenvectors — are independent by assumption.

This is exactly §17.3's change-of-basis machinery. D=P^{-1}AP says A's matrix, expressed relative to the eigenvector basis instead of the standard one, is diagonal — the eigenvectors are the "right" coordinate system in which A's action becomes nothing but independent scaling along each axis, with no mixing between coordinates at all.

When is A diagonalizable?

A is diagonalizable exactly when it has n linearly independent eigenvectors. Two sufficient (but not necessary) conditions worth knowing immediately:

  • n distinct eigenvalues \Rightarrow diagonalizable. Eigenvectors for distinct eigenvalues are automatically independent — proved properly in §19.3, taken as a working fact here.
  • Symmetric matrices are always diagonalizable (§21.0's Spectral Theorem), even with repeated eigenvalues.

A matrix can fail to be diagonalizable when an eigenvalue is repeated and doesn't have enough independent eigenvectors to match — precisely the algebraic-vs-geometric-multiplicity gap §19.2 studies next, with §19.4's Jordan form as the fallback when diagonalization is genuinely impossible.

Why bother: computing A^k for free

Diagonalization's most immediate payoff: powers of A become powers of D, which is trivial (a diagonal matrix's power is just each diagonal entry raised to that power):

A^k=(PDP^{-1})(PDP^{-1})\cdots(PDP^{-1})=PD^kP^{-1}

— every P^{-1}P in the middle cancels to I, leaving only P, D^k, and P^{-1} at the ends. Computing A^{100} directly means 99 matrix multiplications; computing PD^{100}P^{-1} means one diagonalization and raising n numbers to the 100th power. This exact trick reappears whenever a system evolves by repeated applications of the same matrix — §24.0's Markov chains and §24.4's linear ODE systems both use it directly.

Doing it in Python

import sympy as sp

A = sp.Matrix([[4, 1], [2, 3]])
P, D = A.diagonalize()

print("P =")
for row in P.tolist():
    print(row)
print("D =")
for row in D.tolist():
    print(row)

reconstructed = P * D * P.inv()
print("\nP D P^-1 == A:", reconstructed == A)
P =
[-1, 1]
[2, 1]
D =
[2, 0]
[0, 5]

P D P^-1 == A: True

Computing A^{10} two ways — directly, and via diagonalization — to confirm they agree, and to see the cost difference:

import sympy as sp

A = sp.Matrix([[4, 1], [2, 3]])
P, D = A.diagonalize()

direct = A**10
via_diag = P * D**10 * P.inv()

print("A^10 via repeated multiplication:")
for row in direct.tolist():
    print(row)
print("A^10 via P D^10 P^-1:")
for row in via_diag.tolist():
    print(row)
print("match:", direct == via_diag)
A^10 via repeated multiplication:
[6510758, 3254867]
[6509734, 3255891]
A^10 via P D^10 P^-1:
[6510758, 3254867]
[6509734, 3255891]
match: True

Worked example

Diagonalize A=\begin{pmatrix}2&0\\1&3\end{pmatrix} and use it to find A^3.

A is lower-triangular, so its eigenvalues are its diagonal entries (triangular matrices share this property with diagonal ones, since \det(A-\lambda I) is a product of the diagonal entries directly): \lambda=2,3.

\lambda=2: (A-2I)\vec v=\vec0: \begin{pmatrix}0&0\\1&1\end{pmatrix}\vec v=\vec0\Rightarrow v_1=-v_2. \vec v_1=(1,-1).

\lambda=3: (A-3I)\vec v=\vec0: \begin{pmatrix}-1&0\\1&0\end{pmatrix}\vec v=\vec0\Rightarrow v_1=0. \vec v_2=(0,1).

P=\begin{pmatrix}1&0\\-1&1\end{pmatrix},\quad D=\begin{pmatrix}2&0\\0&3\end{pmatrix},\quad P^{-1}=\begin{pmatrix}1&0\\1&1\end{pmatrix}

A^3=PD^3P^{-1}=\begin{pmatrix}1&0\\-1&1\end{pmatrix}\begin{pmatrix}8&0\\0&27\end{pmatrix}\begin{pmatrix}1&0\\1&1\end{pmatrix}=\begin{pmatrix}8&0\\-8&27\end{pmatrix}\begin{pmatrix}1&0\\1&1\end{pmatrix}=\begin{pmatrix}8&0\\19&27\end{pmatrix}

\boxed{A^3=\begin{pmatrix}8&0\\19&27\end{pmatrix}}

Sanity check, by direct multiplication: A^2=\begin{pmatrix}4&0\\5&9\end{pmatrix} (row 2: 1(2)+3(1)=5, 1(0)+3(3)=9), and A^3=A\cdot A^2=\begin{pmatrix}2(4)&0\\1(4)+3(5)&3(9)\end{pmatrix}=\begin{pmatrix}8&0\\19&27\end{pmatrix} ✓ — matches the diagonalization route exactly, confirming both the eigendecomposition and the power formula.

Your turn

1. Is A=\begin{pmatrix}5&0\\0&5\end{pmatrix} diagonalizable? What are P and D?

2. A 3\times3 matrix has eigenvalues 1,2,3, all distinct. Is it guaranteed diagonalizable? Why?

3. True or false: A and D=P^{-1}AP have the same determinant.

Solutions

1. Yes, trivially: A is already diagonal, so D=A works with P=I (or, since every nonzero vector is an eigenvector with eigenvalue 5 here — a repeated eigenvalue with a full 2-dimensional eigenspace — any invertible P works). This is the extreme case of diagonalizability: a matrix that's already its own diagonalization.

2. Yes. Three distinct eigenvalues for a 3\times3 matrix guarantees 3 independent eigenvectors (the "distinct eigenvalues \Rightarrow diagonalizable" sufficient condition above), which is exactly enough to build an invertible P.

3. True. \det D=\det(P^{-1}AP)=\det(P^{-1})\det A\det P=\frac1{\det P}\det A\det P=\det A, using §16.4's multiplicativity and \det(P^{-1})=1/\det P. (This is a preview of §19.3: similar matrices always share the same determinant, trace, and eigenvalues — D=P^{-1}AP is a special case of the general similarity relation studied there.)

Check yourself in code

Diagonalize A=\begin{pmatrix}3&1\\0&2\end{pmatrix} and use P,D to compute A^4.

Print exactly this:

D = [2, 0, 0, 3]
A^4 via diagonalization:
[81, 65]
[0, 16]
import sympy as sp

A = sp.Matrix([[3, 1], [0, 2]])
P, D = A.diagonalize()
print("D =", list(D))
# compute A^4 as P * D**4 * P.inv() and print it row by row, labeled
import sympy as sp

A = sp.Matrix([[3, 1], [0, 2]])
P, D = A.diagonalize()
print("D =", list(D))

A4 = P * D**4 * P.inv()
print("A^4 via diagonalization:")
for row in A4.tolist():
    print(row)

Diagonalization writes A=PDP^{-1} when A has a full set of n independent eigenvectors — the eigenvectors form the columns of P, and D collects the eigenvalues on its diagonal. Distinct eigenvalues (or symmetry, per §21.0) guarantee it, and once diagonalized, any power A^k=PD^kP^{-1} costs almost nothing to compute.

Next: what exactly goes wrong when a matrix isn't diagonalizable — eigenspaces, and the gap between how many times an eigenvalue is a root of the characteristic polynomial versus how many independent eigenvectors it actually supplies.