Eigendecomposition and Diagonalization
If a matrix has enough eigenvectors, you can use them as a basis, and in that basis the matrix is diagonal: it just scales each coordinate. Writing makes powers, exponentials and inverses of trivial, explains the long-run behaviour of linear recurrences, and for symmetric matrices gives an orthogonal decomposition that PCA is built on.
Diagonalization
is diagonalizable if it has linearly independent eigenvectors . Put them in the columns of and the eigenvalues in . Then , so
Read right to left: change to eigen-coordinates, scale each by , change back. Not every matrix is diagonalizable; has only one independent eigenvector. Distinct eigenvalues guarantee diagonalizability.
Powers become easy
and . For large , the largest dominates: that is why repeated multiplication blows up (or dies out) along the top eigenvector, the mechanism behind exploding and vanishing gradients in recurrent networks.
import numpy as np
A = np.array([[0.9, 0.2], [0.1, 0.8]]) # e.g. a 2-state Markov transition (columns sum to 1)
lam, P = np.linalg.eig(A)
D = np.diag(lam)
print("eigenvalues:", np.round(lam, 6))
print("A = P D P^-1:", np.allclose(P @ D @ np.linalg.inv(P), A))
k = 50
Ak = P @ np.diag(lam**k) @ np.linalg.inv(P)
print("A^50 via eigen:", np.round(Ak, 6).tolist())
print("matches matrix_power:", np.allclose(Ak, np.linalg.matrix_power(A, k)))
stat = P[:, np.argmax(lam)] / P[:, np.argmax(lam)].sum()
print("stationary distribution (eigenvalue 1):", np.round(stat, 6))eigenvalues: [1. 0.7]
A = P D P^-1: True
A^50 via eigen: [[0.666667, 0.666667], [0.333333, 0.333333]]
matches matrix_power: True
stationary distribution (eigenvalue 1): [0.666667 0.333333]
The eigenvalue dies out as , so converges to a matrix whose columns are the eigenvector for eigenvalue 1: the long-run distribution of the chain.
Symmetric matrices: the spectral decomposition
For symmetric the eigenvectors can be chosen orthonormal, so is orthogonal and :
The right-hand side writes as a weighted sum of rank-one projections onto the eigenvectors. For a covariance matrix, is the variance of the data along .
import numpy as np
A = np.array([[3.0, 1.0, 0.0], [1.0, 3.0, 0.0], [0.0, 0.0, 2.0]])
lam, Q = np.linalg.eigh(A)
rebuilt = sum(lam[i] * np.outer(Q[:, i], Q[:, i]) for i in range(3))
print("eigenvalues:", np.round(lam, 6))
print("sum of rank-one pieces = A:", np.allclose(rebuilt, A))
A_inv = Q @ np.diag(1 / lam) @ Q.T
A_sqrt = Q @ np.diag(np.sqrt(lam)) @ Q.T
print("inverse ok:", np.allclose(A_inv @ A, np.eye(3)), " square root ok:", np.allclose(A_sqrt @ A_sqrt, A))eigenvalues: [2. 2. 4.]
sum of rank-one pieces = A: True
inverse ok: True square root ok: True
Any function of a symmetric matrix is computed the same way: apply it to the eigenvalues. That gives inverse, square root, and whitening , which makes the covariance of data the identity.
In software
np.linalg.eighreturns and for symmetric input;scipy.linalg.expmcomputes matrix exponentials.- Whitening (ZCA, PCA whitening) multiplies centred data by .
- In RNNs, behaves like : eigenvalues above 1 explode, below 1 vanish (Vanishing and Exploding Gradients).
Exercises
Exercise 1: Diagonalize by hand
Diagonalize and compute .
Answer
, ; .
Solution
A diagonal matrix is already diagonalized. Powers act entry by entry: and .
Exercise 2: Not diagonalizable
Show that is not diagonalizable.
Answer
Its only eigenvalue is 1, with a one-dimensional eigenspace.
Solution
. For , has null space spanned by only. Two independent eigenvectors would be needed.
Exercise 3: Matrix square root (code)
Compute the symmetric square root of through its eigendecomposition.
Answer
.
Solution
Eigenvalues 9 and 1 with eigenvectors and ; their square roots are 3 and 1.
import numpy as np
A = np.array([[5.0, 4.0], [4.0, 5.0]])
lam, Q = np.linalg.eigh(A)
print(np.round(Q @ np.diag(np.sqrt(lam)) @ Q.T, 10))[[2. 1.]
[1. 2.]]
Test yourself
If , then equals…
For a symmetric matrix, the eigenvector matrix can be chosen…
- orthogonal, so
- triangular
- equal to
- singular
A sufficient condition for diagonalizability is…
- distinct eigenvalues
- all entries positive