TExcelTAD
Home › Math for Machine Learning › Eigendecomposition and Diagonalization

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 A=PDP−1A = PDP^{-1} makes powers, exponentials and inverses of AA trivial, explains the long-run behaviour of linear recurrences, and for symmetric matrices gives an orthogonal decomposition that PCA is built on.

Diagonalization

A∈Rn×nA \in \mathbb{R}^{n \times n} is diagonalizable if it has nn linearly independent eigenvectors p1,…,pn\mathbf{p}_1, \ldots, \mathbf{p}_n. Put them in the columns of PP and the eigenvalues in D=diag⁡(λ1,…,λn)D = \operatorname{diag}(\lambda_1, \ldots, \lambda_n). Then AP=PDAP = PD, so

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

Read right to left: change to eigen-coordinates, scale each by λi\lambda_i, change back. Not every matrix is diagonalizable; [1101]\begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix} has only one independent eigenvector. Distinct eigenvalues guarantee diagonalizability.

Powers become easy

Ak=PDkP−1A^k = PD^kP^{-1} and Dk=diag⁡(λ1k,…,λnk)D^k = \operatorname{diag}(\lambda_1^k, \ldots, \lambda_n^k). For large kk, the largest ∣λ∣\lvert\lambda\rvert 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.

python · try it yourself
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 0.70.7 dies out as 0.7k0.7^k, so AkA^k 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 AA the eigenvectors can be chosen orthonormal, so P=QP = Q is orthogonal and P−1=QTP^{-1} = Q^T:

A=QΛQT=∑i=1nλi qiqiTA = Q\Lambda Q^T = \sum_{i=1}^{n} \lambda_i\,\mathbf{q}_i\mathbf{q}_i^T

The right-hand side writes AA as a weighted sum of rank-one projections onto the eigenvectors. For a covariance matrix, λi\lambda_i is the variance of the data along qi\mathbf{q}_i.

python · try it yourself
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, exp⁡(A)\exp(A) and whitening Σ−1/2\Sigma^{-1/2}, which makes the covariance of data the identity.

In software

  • np.linalg.eigh returns Λ\Lambda and QQ for symmetric input; scipy.linalg.expm computes matrix exponentials.
  • Whitening (ZCA, PCA whitening) multiplies centred data by Σ−1/2\Sigma^{-1/2}.
  • In RNNs, ht=Wht−1h_t = Wh_{t-1} behaves like WtW^t: eigenvalues above 1 explode, below 1 vanish (Vanishing and Exploding Gradients).

Exercises

Exercise 1: Diagonalize by hand

Diagonalize A=[2005]A = \begin{bmatrix} 2 & 0 \\ 0 & 5 \end{bmatrix} and compute A10A^{10}.

Answer

P=IP = I, D=AD = A; A10=diag⁡(1024,9 765 625)A^{10} = \operatorname{diag}(1024, 9\,765\,625).

Solution

A diagonal matrix is already diagonalized. Powers act entry by entry: 210=10242^{10} = 1024 and 510=9 765 6255^{10} = 9\,765\,625.

Exercise 2: Not diagonalizable

Show that [1101]\begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix} is not diagonalizable.

Answer

Its only eigenvalue is 1, with a one-dimensional eigenspace.

Solution

det⁡(A−λI)=(1−λ)2\det(A - \lambda I) = (1 - \lambda)^2. For λ=1\lambda = 1, A−I=[0100]A - I = \begin{bmatrix} 0 & 1 \\ 0 & 0 \end{bmatrix} has null space spanned by (1,0)(1, 0) only. Two independent eigenvectors would be needed.

Exercise 3: Matrix square root (code)

Compute the symmetric square root of [5445]\begin{bmatrix} 5 & 4 \\ 4 & 5 \end{bmatrix} through its eigendecomposition.

Answer

[2112]\begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix}.

Solution

Eigenvalues 9 and 1 with eigenvectors (1,1)/2(1, 1)/\sqrt{2} and (1,−1)/2(1, -1)/\sqrt{2}; their square roots are 3 and 1.

python · try it yourself
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 A=PDP−1A = PDP^{-1}, then AkA^k equals…

  1. PkDkP−kP^kD^kP^{-k}
  2. PDkP−1PD^kP^{-1}
  3. DkD^k
  4. kPDP−1kPDP^{-1}

For a symmetric matrix, the eigenvector matrix can be chosen…

  1. orthogonal, so P−1=PTP^{-1} = P^T
  2. triangular
  3. equal to AA
  4. singular

A sufficient condition for diagonalizability is…

  1. det⁡A≠0\det A \ne 0
  2. nn distinct eigenvalues
  3. tr⁡A=0\operatorname{tr} A = 0
  4. all entries positive