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 is symmetric if . In Diagonalization we wrote when has independent eigenvectors. For symmetric matrices much more is true.
Spectral theorem. If is a real symmetric matrix, then
- all eigenvalues of are real,
- eigenvectors for different eigenvalues are orthogonal,
- is orthogonally diagonalizable: with orthogonal and diagonal.
Conversely, any matrix of the form is symmetric, since .
Why the first two parts hold
Real eigenvalues. Let with , allowing complex entries (see Complex Eigenvalues). Write for the complex conjugate. Then . Taking the conjugate transpose of the scalar , and using that is real and symmetric, gives the same scalar back, so is real. Since is real and positive, is real.
Orthogonal eigenvectors. If and with , then
so , and .
The third part takes an induction on (find one unit eigenvector, show maps its orthogonal complement into itself, repeat). The practical consequence: within one eigenspace of dimension you may pick any basis, so pick an orthonormal one with Gram-Schmidt.
An example with a repeated eigenvalue
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 is the plane . SymPy's basis , is not orthogonal, but both vectors are orthogonal to , exactly as the theorem says. Gram-Schmidt inside the plane fixes the rest.
The spectral decomposition
Multiplying out column by column gives
Each is the projection matrix onto the line through . 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 become easy too: , and for positive eigenvalues .
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.
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 and .
Tip: If
np.linalg.eigreturns tiny imaginary parts for a matrix you know is symmetric, it is usually because the matrix is only symmetric up to rounding. Symmetrize with and calleigh.
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 by hand
Orthogonally diagonalize .
Answer
with , : .
Solution
The eigenvectors are orthogonal; divide by .
Exercise 2: Which parts fail without symmetry?
has eigenvalues 1 and 2. Are its eigenvectors orthogonal?
Answer
No: and have dot product 1. is diagonalizable, but not orthogonally.
Solution
gives . gives . The orthogonality argument used , which does not satisfy.
Exercise 3: Build a matrix from its spectrum
Find the symmetric matrix with eigenvalue 5 on and eigenvalue 0 on .
Answer
.
Solution
. Check: and .
Exercise 4: Matrix power (code)
Use to compute for , and compare with repeated multiplication.
Answer
.
Solution
with . .
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?
- All eigenvalues are positive
- with orthogonal
- All eigenvalues are distinct
- A is invertible
A symmetric matrix has eigenvalue 2 with a 2-dimensional eigenspace. What must you do to build ?
- Nothing: any two eigenvectors are orthogonal
- Choose an orthonormal basis of that eigenspace (e.g. Gram-Schmidt)
- Give up: it is not diagonalizable
- Use the eigenvectors of
Which NumPy function should you use for a symmetric matrix?
- np.linalg.eig
- np.linalg.svd only
- np.linalg.eigh
- np.linalg.inv