TExcelTAD
Home › Linear Algebra & Matrix Theory › Diagonalization

Diagonalization

A diagonal matrix is the easiest matrix there is: it just scales each coordinate. Diagonalizing AA means finding a coordinate system, made of eigenvectors, in which AA becomes diagonal. When that works, powers, exponentials and long-run behaviour of AA all reduce to arithmetic on its eigenvalues.

The factorization A=PDP−1A = PDP^{-1}

Suppose the n×nn \times n matrix AA has nn linearly independent eigenvectors v1,…,vn\mathbf{v}_1, \ldots, \mathbf{v}_n with eigenvalues λ1,…,λn\lambda_1, \ldots, \lambda_n. Put the eigenvectors in the columns of PP and the eigenvalues on the diagonal of DD:

P=[v1v2⋯vn]D=diag⁡(λ1,λ2,…,λn)P = \begin{bmatrix} \mathbf{v}_1 & \mathbf{v}_2 & \cdots & \mathbf{v}_n \end{bmatrix} \qquad D = \operatorname{diag}(\lambda_1, \lambda_2, \ldots, \lambda_n)

Multiplying column by column, AP=[Av1∣⋯∣Avn]=[λ1v1∣⋯∣λnvn]=PDAP = [A\mathbf{v}_1 \mid \cdots \mid A\mathbf{v}_n] = [\lambda_1\mathbf{v}_1 \mid \cdots \mid \lambda_n\mathbf{v}_n] = PD. The columns of PP are independent, so PP is invertible, and

A=PDP−1and equivalentlyD=P−1APA = PDP^{-1} \qquad \text{and equivalently} \qquad D = P^{-1}AP

A matrix is diagonalizable if it can be written this way. Read from right to left, A=PDP−1A = PDP^{-1} says: convert a vector to eigenvector coordinates (P−1P^{-1}), scale each coordinate by its eigenvalue (DD), convert back (PP).

A worked example

A=[4−211]A = \begin{bmatrix} 4 & -2 \\ 1 & 1 \end{bmatrix} has eigenvalues 2 and 3 with eigenvectors (1,1)(1, 1) and (2,1)(2, 1) (see Finding Eigenvalues):

P=[1211]D=[2003]det⁡P=1−2=−1P−1=1−1⋅[1−2−11]=[−121−1]PDP−1=[2623]⋅[−121−1]=[4−211]=A ✓\begin{aligned} &P = \begin{bmatrix} 1 & 2 \\ 1 & 1 \end{bmatrix} \qquad D = \begin{bmatrix} 2 & 0 \\ 0 & 3 \end{bmatrix} \qquad \det P = 1 - 2 = -1 \\ &P^{-1} = \frac{1}{-1} \cdot \begin{bmatrix} 1 & -2 \\ -1 & 1 \end{bmatrix} = \begin{bmatrix} -1 & 2 \\ 1 & -1 \end{bmatrix} \\ &PDP^{-1} = \begin{bmatrix} 2 & 6 \\ 2 & 3 \end{bmatrix} \cdot \begin{bmatrix} -1 & 2 \\ 1 & -1 \end{bmatrix} = \begin{bmatrix} 4 & -2 \\ 1 & 1 \end{bmatrix} = A \ \checkmark \end{aligned}

SymPy's diagonalize() returns PP and DD. It may order the eigenvalues differently from you, and scale the eigenvectors differently; any such choice is valid as long as column ii of PP belongs to the eigenvalue in position ii of DD.

python · try it yourself
from sympy import Matrix

A = Matrix([[4, -2], [1, 1]])
P, D = A.diagonalize()
print("P =", P.tolist())
print("D =", D.tolist())
print("P D P^-1 == A:", P * D * P.inv() == A)
print("P^-1 A P =", (P.inv() * A * P).tolist())
P = [[1, 2], [1, 1]]
D = [[2, 0], [0, 3]]
P D P^-1 == A: True
P^-1 A P = [[2, 0], [0, 3]]

When is a matrix diagonalizable?

The whole question is whether there are enough independent eigenvectors:

Situation Diagonalizable?
nn distinct eigenvalues always (eigenvectors of distinct eigenvalues are independent)
repeated eigenvalue, eigenspace as large as the multiplicity yes
some eigenspace smaller than the eigenvalue's multiplicity no
real symmetric matrix always, even with an orthogonal PP (Diagonalization of Real Symmetric Matrices)

In terms of Finding Eigenvalues: AA is diagonalizable exactly when, for every eigenvalue, geometric multiplicity=algebraic multiplicity\text{geometric multiplicity} = \text{algebraic multiplicity} (over the complex numbers, so that the characteristic polynomial splits into linear factors).

The standard counterexample is the shear N=[1101]N = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}: λ=1\lambda = 1 twice, but N−I=[0100]N - I = \begin{bmatrix} 0 & 1 \\ 0 & 0 \end{bmatrix} has a one-dimensional null space. There is only one eigenvector direction, so no invertible PP exists.

python · try it yourself
from sympy import Matrix

for name, M in [("[[4,1,1],[1,4,1],[1,1,4]]", Matrix([[4, 1, 1], [1, 4, 1], [1, 1, 4]])),
                ("shear [[1,1],[0,1]]", Matrix([[1, 1], [0, 1]])),
                ("[[2,1,0],[0,2,0],[0,0,3]]", Matrix([[2, 1, 0], [0, 2, 0], [0, 0, 3]]))]:
    dims = {val: (mult, len(vecs)) for val, mult, vecs in M.eigenvects()}
    print(f"{name:28} {{lambda: (alg, geo)}} = {dims}  diagonalizable: {M.is_diagonalizable()}")
[[4,1,1],[1,4,1],[1,1,4]]    {lambda: (alg, geo)} = {3: (2, 2), 6: (1, 1)}  diagonalizable: True
shear [[1,1],[0,1]]          {lambda: (alg, geo)} = {1: (2, 1)}  diagonalizable: False
[[2,1,0],[0,2,0],[0,0,3]]    {lambda: (alg, geo)} = {2: (2, 1), 3: (1, 1)}  diagonalizable: False

The first matrix has a repeated eigenvalue 3 but a two-dimensional eigenspace for it, so it is diagonalizable. The other two each have an eigenvalue whose eigenspace is too small. Matrices like these are handled by the Jordan form, which is beyond this course; in practice the matrices you meet in data and physics are very often symmetric, and those are always diagonalizable.

Change of basis

D=P−1APD = P^{-1}AP is the same linear transformation as AA, described in the basis of eigenvectors instead of the standard basis. Two matrices related by B=P−1APB = P^{-1}AP for some invertible PP are called similar. Similar matrices share their characteristic polynomial, eigenvalues, trace, determinant and rank (Properties of Eigenvectors and Eigenvalues). The general idea of "the matrix of a map in a chosen basis" is developed in The Matrix of a Linear Transformation.

In software

  • Fast repeated application: Ak=PDkP−1A^k = PD^kP^{-1} costs two matrix products plus nn scalar powers (Application: Matrix Powers).
  • Decoupling: a system x′=Ax\mathbf{x}' = A\mathbf{x} of coupled equations becomes nn independent one-variable equations in eigenvector coordinates (Application: Systems of Linear Differential Equations).
  • Spectral methods: graph clustering, PCA and the analysis of attention matrices all start from an eigen-decomposition.

Note: For numerical work prefer np.linalg.eigh on symmetric matrices. For a non-symmetric AA whose eigenvectors are almost parallel, PP is nearly singular and computing P−1P^{-1} loses many digits even if AA is technically diagonalizable.

Exercises

Exercise 1: Diagonalize a triangular matrix

Diagonalize A=[1102]A = \begin{bmatrix} 1 & 1 \\ 0 & 2 \end{bmatrix}: give PP, DD and P−1P^{-1}.

Answer

P=[1101]P = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}, D=diag⁡(1,2)D = \operatorname{diag}(1, 2), P−1=[1−101]P^{-1} = \begin{bmatrix} 1 & -1 \\ 0 & 1 \end{bmatrix}.

Solution
eigenvalues: diagonal entries 1,2λ=1: A−I=[0101]→y=0→(1,0)λ=2: A−2I=[−1100]→x=y→(1,1)P=[1101],det⁡P=1,P−1=[1−101]PDP−1=[1202]⋅[1−101]=[1102] ✓\begin{aligned} &\text{eigenvalues: diagonal entries } 1, 2 \\ &\lambda = 1: \ A - I = \begin{bmatrix} 0 & 1 \\ 0 & 1 \end{bmatrix} \to y = 0 \to (1, 0) \\ &\lambda = 2: \ A - 2I = \begin{bmatrix} -1 & 1 \\ 0 & 0 \end{bmatrix} \to x = y \to (1, 1) \\ &P = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}, \quad \det P = 1, \quad P^{-1} = \begin{bmatrix} 1 & -1 \\ 0 & 1 \end{bmatrix} \\ &PDP^{-1} = \begin{bmatrix} 1 & 2 \\ 0 & 2 \end{bmatrix} \cdot \begin{bmatrix} 1 & -1 \\ 0 & 1 \end{bmatrix} = \begin{bmatrix} 1 & 1 \\ 0 & 2 \end{bmatrix} \ \checkmark \end{aligned}

Exercise 2: Yes or no

Which of these are diagonalizable: (a) [3103]\begin{bmatrix} 3 & 1 \\ 0 & 3 \end{bmatrix}, (b) [3003]\begin{bmatrix} 3 & 0 \\ 0 & 3 \end{bmatrix}, (c) [1234]\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}?

Answer

(a) no, (b) yes, (c) yes.

Solution

(a) λ=3\lambda = 3 twice, A−3I=[0100]A - 3I = \begin{bmatrix} 0 & 1 \\ 0 & 0 \end{bmatrix} has a 1-dimensional null space: not enough eigenvectors. (b) It is already diagonal (P=IP = I). (c) tr⁡=5\operatorname{tr} = 5, det⁡=−2\det = -2, so λ2−5λ−2=0\lambda^2 - 5\lambda - 2 = 0 has discriminant 25+8=33>025 + 8 = 33 > 0: two distinct real eigenvalues, so it is diagonalizable.

Exercise 3: Build a matrix from its eigen-data

Find the 2×22 \times 2 matrix with eigenvector (1,2)(1, 2) for λ=3\lambda = 3 and eigenvector (1,−1)(1, -1) for λ=0\lambda = 0. Check it in SymPy.

Answer

A=[1122]A = \begin{bmatrix} 1 & 1 \\ 2 & 2 \end{bmatrix}.

Solution
P=[112−1],D=diag⁡(3,0),det⁡P=−3P−1=1−3⋅[−1−1−21]=13⋅[112−1]PD=[3060]A=PDP−1=13⋅[3366]=[1122]\begin{aligned} &P = \begin{bmatrix} 1 & 1 \\ 2 & -1 \end{bmatrix}, \quad D = \operatorname{diag}(3, 0), \quad \det P = -3 \\ &P^{-1} = \frac{1}{-3} \cdot \begin{bmatrix} -1 & -1 \\ -2 & 1 \end{bmatrix} = \frac{1}{3} \cdot \begin{bmatrix} 1 & 1 \\ 2 & -1 \end{bmatrix} \\ &PD = \begin{bmatrix} 3 & 0 \\ 6 & 0 \end{bmatrix} \\ &A = PDP^{-1} = \frac{1}{3} \cdot \begin{bmatrix} 3 & 3 \\ 6 & 6 \end{bmatrix} = \begin{bmatrix} 1 & 1 \\ 2 & 2 \end{bmatrix} \end{aligned}
python · try it yourself
from sympy import Matrix, diag

P = Matrix([[1, 1], [2, -1]])
A = P * diag(3, 0) * P.inv()
print(A.tolist())
print(list(A * Matrix([1, 2])), list(A * Matrix([1, -1])))
[[1, 1], [2, 2]]
[3, 6] [0, 0]

Exercise 4: Count the free parameters

A 3×33 \times 3 matrix has eigenvalues 5, 5, 2. The eigenspace of 5 is a plane. Is AA diagonalizable? What if the eigenspace of 5 were a line?

Answer

Plane: yes. Line: no.

Solution

λ=2\lambda = 2 always contributes one eigenvector. A plane of eigenvectors for 5 adds two independent ones, three in total, so PP is 3×33 \times 3 invertible. With only a line for λ=5\lambda = 5, there are two independent eigenvectors in total, one short of three.

Test yourself

In A=PDP−1A = PDP^{-1}, what are the columns of PP?

  1. The rows of AA
  2. Eigenvectors of AA
  3. The eigenvalues of AA
  4. The columns of A−1A^{-1}

Which condition guarantees that an n×nn \times n matrix is diagonalizable?

  1. It has nn distinct eigenvalues
  2. It is invertible
  3. Its determinant is 1
  4. It has a repeated eigenvalue

Similar matrices AA and P−1APP^{-1}AP always share…

  1. their eigenvectors
  2. their entries
  3. their eigenvalues
  4. their columns