TExcelTAD
Home › Linear Algebra & Matrix Theory › Diagonalization of Real Symmetric Matrices

Diagonalization of Real Symmetric Matrices

Not every matrix can be diagonalized, but every real symmetric matrix can, and in the best possible way: with real eigenvalues and an orthonormal basis of eigenvectors. This is the spectral theorem, and it is the engine behind PCA, quadratic forms and vibration analysis.

The spectral theorem

A real matrix AA is symmetric if AT=AA^T = A. In Diagonalization we wrote A=PDP−1A = PDP^{-1} when AA has nn independent eigenvectors. For symmetric matrices much more is true.

Spectral theorem. If AA is a real symmetric n×nn \times n matrix, then

  1. all eigenvalues of AA are real,
  2. eigenvectors for different eigenvalues are orthogonal,
  3. AA is orthogonally diagonalizable: A=QΛQTA = Q\Lambda Q^T with QQ orthogonal and Λ\Lambda diagonal.

Conversely, any matrix of the form QΛQTQ\Lambda Q^T is symmetric, since (QΛQT)T=QΛTQT=QΛQT(Q\Lambda Q^T)^T = Q\Lambda^TQ^T = Q\Lambda Q^T.

Why the first two parts hold

Real eigenvalues. Let Av=λvA\mathbf{v} = \lambda\mathbf{v} with v≠0\mathbf{v} \ne \mathbf{0}, allowing complex entries (see Complex Eigenvalues). Write vˉ\bar{\mathbf{v}} for the complex conjugate. Then vˉTAv=λvˉTv\bar{\mathbf{v}}^TA\mathbf{v} = \lambda \bar{\mathbf{v}}^T\mathbf{v}. Taking the conjugate transpose of the scalar vˉTAv\bar{\mathbf{v}}^TA\mathbf{v}, and using that AA is real and symmetric, gives the same scalar back, so vˉTAv\bar{\mathbf{v}}^TA\mathbf{v} is real. Since vˉTv=∑∣vi∣2\bar{\mathbf{v}}^T\mathbf{v} = \sum \lvert v_i \rvert^2 is real and positive, λ\lambda is real.

Orthogonal eigenvectors. If Au=λuA\mathbf{u} = \lambda\mathbf{u} and Av=μvA\mathbf{v} = \mu\mathbf{v} with λ≠μ\lambda \ne \mu, then

λ(u⋅v)=(Au)⋅v=uTATv=uTAv=u⋅(Av)=μ(u⋅v)\lambda (\mathbf{u} \cdot \mathbf{v}) = (A\mathbf{u}) \cdot \mathbf{v} = \mathbf{u}^TA^T\mathbf{v} = \mathbf{u}^TA\mathbf{v} = \mathbf{u} \cdot (A\mathbf{v}) = \mu (\mathbf{u} \cdot \mathbf{v})

so (λ−μ)(u⋅v)=0(\lambda - \mu)(\mathbf{u} \cdot \mathbf{v}) = 0, and u⋅v=0\mathbf{u} \cdot \mathbf{v} = 0.

The third part takes an induction on nn (find one unit eigenvector, show AA maps its orthogonal complement into itself, repeat). The practical consequence: within one eigenspace of dimension >1> 1 you may pick any basis, so pick an orthonormal one with Gram-Schmidt.

An example with a repeated eigenvalue

python · try it yourself
from sympy import Matrix, GramSchmidt, diag, simplify

A = Matrix([[2, 1, 1],
            [1, 2, 1],
            [1, 1, 2]])
cols, evals = [], []
for lam, mult, vecs in A.eigenvects():
    print("eigenvalue", lam, "multiplicity", mult, "basis", [list(v) for v in vecs])
    for q in GramSchmidt(vecs, orthonormal=True):   # orthonormalize inside the eigenspace
        cols.append(q); evals.append(lam)
Q = Matrix.hstack(*cols)
print("Q^T Q = I:", simplify(Q.T * Q) == Matrix.eye(3))
print("Q^T A Q =", simplify(Q.T * A * Q).tolist())
print("A = Q L Q^T:", simplify(Q * diag(*evals) * Q.T) == A)
eigenvalue 1 multiplicity 2 basis [[-1, 1, 0], [-1, 0, 1]]
eigenvalue 4 multiplicity 1 basis [[1, 1, 1]]
Q^T Q = I: True
Q^T A Q = [[1, 0, 0], [0, 1, 0], [0, 0, 4]]
A = Q L Q^T: True

The eigenspace for λ=1\lambda = 1 is the plane x+y+z=0x + y + z = 0. SymPy's basis (−1,1,0)(-1, 1, 0), (−1,0,1)(-1, 0, 1) is not orthogonal, but both vectors are orthogonal to (1,1,1)(1, 1, 1), exactly as the theorem says. Gram-Schmidt inside the plane fixes the rest.

The spectral decomposition

Multiplying out QΛQTQ\Lambda Q^T column by column gives

A=λ1q1q1T+λ2q2q2T+⋯+λnqnqnTA = \lambda_1 \mathbf{q}_1\mathbf{q}_1^T + \lambda_2 \mathbf{q}_2\mathbf{q}_2^T + \cdots + \lambda_n \mathbf{q}_n\mathbf{q}_n^T

Each qiqiT\mathbf{q}_i\mathbf{q}_i^T is the projection matrix onto the line through qi\mathbf{q}_i. So a symmetric matrix acts by splitting a vector into perpendicular components along its eigenvectors, scaling each by its eigenvalue, and adding them up. Functions of AA become easy too: Ak=QΛkQTA^k = Q\Lambda^kQ^T, and for positive eigenvalues A=QΛQT\sqrt{A} = Q\sqrt{\Lambda}Q^T.

For numerical work use np.linalg.eigh (h for Hermitian): it assumes symmetry, is faster and more accurate than eig, returns eigenvalues in ascending order and always returns orthonormal eigenvectors.

python · try it yourself
import numpy as np

A = np.array([[4.0, 1.0, 0.0],
              [1.0, 3.0, 1.0],
              [0.0, 1.0, 2.0]])
w, Q = np.linalg.eigh(A)
print("eigenvalues:", np.round(w, 4))
print("Q orthogonal:", np.allclose(Q.T @ Q, np.eye(3)))
S = sum(w[i] * np.outer(Q[:, i], Q[:, i]) for i in range(3))
print("sum of lambda q q^T equals A:", np.allclose(S, A))
R = Q @ np.diag(np.sqrt(w)) @ Q.T
print("sqrt(A)^2 equals A:", np.allclose(R @ R, A))
eigenvalues: [1.2679 3.     4.7321]
Q orthogonal: True
sum of lambda q q^T equals A: True
sqrt(A)^2 equals A: True

The eigenvalues are 3−33 - \sqrt{3}, 3 and 3+33 + \sqrt{3}.

Tip: If np.linalg.eig returns tiny imaginary parts for a matrix you know is symmetric, it is usually because the matrix is only symmetric up to rounding. Symmetrize with (A+AT)/2(A + A^T)/2 and call eigh.

In software

  • Statistics and ML. Covariance matrices are symmetric; their orthonormal eigenvectors are the principal components (PCA).
  • Optimization. The Hessian of a smooth function is symmetric; its eigenvalues decide whether a critical point is a minimum, maximum or saddle (Positive Definite Matrices).
  • Graphs. The Laplacian of an undirected graph is symmetric; its eigenvectors drive spectral clustering and graph layouts.
  • Physics engines. The inertia tensor of a rigid body is symmetric; diagonalizing it gives the principal axes about which the body spins cleanly.

Exercises

Exercise 1: A 2×22 \times 2 by hand

Orthogonally diagonalize A=[3113]A = \begin{bmatrix} 3 & 1 \\ 1 & 3 \end{bmatrix}.

Answer

λ=2,4\lambda = 2, 4 with q1=(1,−1)/2\mathbf{q}_1 = (1, -1)/\sqrt{2}, q2=(1,1)/2\mathbf{q}_2 = (1, 1)/\sqrt{2}: A=Qdiag⁡(2,4)QTA = Q \operatorname{diag}(2, 4) Q^T.

Solution
det⁡(A−λI)=(3−λ)2−1=0  ⟹  λ=2 or 4λ=2 ⁣:(A−2I)v=0  ⟹  v1+v2=0  ⟹  (1,−1)λ=4 ⁣:(A−4I)v=0  ⟹  v1=v2  ⟹  (1,1)\begin{aligned} &\det(A - \lambda I) = (3 - \lambda)^2 - 1 = 0 \implies \lambda = 2 \text{ or } 4 \\ &\lambda = 2\colon (A - 2I)\mathbf{v} = \mathbf{0} \implies v_1 + v_2 = 0 \implies (1, -1) \\ &\lambda = 4\colon (A - 4I)\mathbf{v} = \mathbf{0} \implies v_1 = v_2 \implies (1, 1) \end{aligned}

The eigenvectors are orthogonal; divide by 2\sqrt{2}.

Exercise 2: Which parts fail without symmetry?

B=[1102]B = \begin{bmatrix} 1 & 1 \\ 0 & 2 \end{bmatrix} has eigenvalues 1 and 2. Are its eigenvectors orthogonal?

Answer

No: (1,0)(1, 0) and (1,1)(1, 1) have dot product 1. BB is diagonalizable, but not orthogonally.

Solution

λ=1\lambda = 1 gives (B−I)v=0  ⟹  v2=0  ⟹  (1,0)(B - I)\mathbf{v} = \mathbf{0} \implies v_2 = 0 \implies (1, 0). λ=2\lambda = 2 gives −v1+v2=0  ⟹  (1,1)-v_1 + v_2 = 0 \implies (1, 1). The orthogonality argument used AT=AA^T = A, which BB does not satisfy.

Exercise 3: Build a matrix from its spectrum

Find the symmetric matrix with eigenvalue 5 on (1,2)/5(1, 2)/\sqrt{5} and eigenvalue 0 on (2,−1)/5(2, -1)/\sqrt{5}.

Answer

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

Solution

A=5⋅q1q1T+0⋅q2q2T=5⋅15[1224]=[1224]A = 5 \cdot \mathbf{q}_1\mathbf{q}_1^T + 0 \cdot \mathbf{q}_2\mathbf{q}_2^T = 5 \cdot \frac{1}{5}\begin{bmatrix} 1 & 2 \\ 2 & 4 \end{bmatrix} = \begin{bmatrix} 1 & 2 \\ 2 & 4 \end{bmatrix}. Check: A(1,2)=(5,10)=5(1,2)A(1, 2) = (5, 10) = 5(1, 2) and A(2,−1)=(0,0)A(2, -1) = (0, 0).

Exercise 4: Matrix power (code)

Use A=QΛQTA = Q\Lambda Q^T to compute A10A^{10} for A=[2112]A = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix}, and compare with repeated multiplication.

Answer

A10=[29525295242952429525]A^{10} = \begin{bmatrix} 29525 & 29524 \\ 29524 & 29525 \end{bmatrix}.

Solution

λ=1,3\lambda = 1, 3 with q=(1,−1)/2,(1,1)/2\mathbf{q} = (1, -1)/\sqrt{2}, (1, 1)/\sqrt{2}. A10=1⋅q1q1T+310⋅q2q2T=12[1+5904959049−159049−11+59049]A^{10} = 1 \cdot \mathbf{q}_1\mathbf{q}_1^T + 3^{10} \cdot \mathbf{q}_2\mathbf{q}_2^T = \frac{1}{2}\begin{bmatrix} 1 + 59049 & 59049 - 1 \\ 59049 - 1 & 1 + 59049 \end{bmatrix}.

python · try it yourself
import numpy as np
A = np.array([[2.0, 1.0], [1.0, 2.0]])
w, Q = np.linalg.eigh(A)
print(np.round(Q @ np.diag(w**10) @ Q.T).astype(np.int64).tolist())
print(np.linalg.matrix_power(A.astype(np.int64), 10).tolist())
[[29525, 29524], [29524, 29525]]
[[29525, 29524], [29524, 29525]]

Test yourself

Which statement is part of the spectral theorem for real symmetric matrices?

  1. All eigenvalues are positive
  2. A=QΛQTA = Q\Lambda Q^T with QQ orthogonal
  3. All eigenvalues are distinct
  4. A is invertible

A symmetric matrix has eigenvalue 2 with a 2-dimensional eigenspace. What must you do to build QQ?

  1. Nothing: any two eigenvectors are orthogonal
  2. Choose an orthonormal basis of that eigenspace (e.g. Gram-Schmidt)
  3. Give up: it is not diagonalizable
  4. Use the eigenvectors of ATA^T

Which NumPy function should you use for a symmetric matrix?

  1. np.linalg.eig
  2. np.linalg.svd only
  3. np.linalg.eigh
  4. np.linalg.inv