Eigenvalues and Eigenvectors — Explained with Applications
- Av = λv: eigenvector v is unchanged in direction by A, scaled by eigenvalue λ.
- Eigenvalues from characteristic polynomial det(A-λI)=0. Eigenvectors from null space of (A-λI).
- PCA: eigenvectors of covariance matrix = principal components. Eigenvalues = variance explained.
- Eigenvector of a matrix A: a nonzero vector v such that Av = λv (only scaled, not rotated)
- Eigenvalue λ: the scalar factor by which the eigenvector is stretched or shrunk
- Compute: solve det(A — λI) = 0 for λ, then (A - λI)v = 0 for v
- Real symmetric matrices: always have real eigenvalues and orthogonal eigenvectors — use numpy.linalg.eigh
- Power iteration: repeated Av / ‖Av‖ converges to dominant eigenvector — original PageRank algorithm
- Production trap: near-zero eigenvalues cause division‑by‑zero in PCA whitening and numerical instability in any downstream inversion
Eigenvalue/Eigenvector Debug Cheat Sheet
Numpy returns complex eigenvalues for a real symmetric matrix
np.linalg.eigh(A) # assumes symmetric, returns real eigenvaluesnp.max(np.abs(A - A.T)) # check asymmetry magnitudeEigenvalues include tiny negative values (e.g., -1e-15) for a positive semi-definite matrix
eigvals = np.clip(eigvals, 0, None) # set negative to zerocondition_number = np.max(eigvals) / np.min(eigvals[eigvals > 1e-12])Production Incident
Production Debug GuideCommon failures in eigenvalue-related algorithms and their immediate fixes
Eigenvalues and eigenvectors are perhaps the most fundamental concept in applied linear algebra. Av = λv — a matrix A, when applied to its eigenvector v, produces the same vector scaled by eigenvalue λ. Finding the directions that a transformation preserves (up to scaling) reveals the essential structure of the transformation.
Google's original PageRank was the dominant eigenvector of the web's link matrix. PCA computes the eigenvectors of a covariance matrix to find principal components. Quantum states are eigenvectors of Hamiltonian operators. Structural resonant frequencies are eigenvalues of stiffness matrices. Understanding eigenvectors is understanding how to extract the 'essential directions' from a linear system.
Computing Eigenvalues and Eigenvectors
Eigenvalues: solve det(A - λI) = 0 (characteristic polynomial). Eigenvectors: for each λ, solve (A - λI)v = 0.
The characteristic polynomial of an n×n matrix is degree n. For large n, numerical methods (QR algorithm, power iteration) are used. Python's numpy.linalg.eig uses the LAPACK library, which applies the QR algorithm with shifts.
import numpy as np A = np.array([[4,2],[1,3]], dtype=float) # NumPy eigendecomposition eigenvalues, eigenvectors = np.linalg.eig(A) print('Eigenvalues:', eigenvalues) # [5, 2] print('Eigenvectors (columns):\n', eigenvectors) # Verify: Av = λv for i in range(len(eigenvalues)): v = eigenvectors[:, i] lam = eigenvalues[i] print(f'λ={lam:.1f}: Av={A@v}, λv={lam*v}') print(f' Match: {np.allclose(A@v, lam*v)}')
Eigenvectors (columns):
[[0.894 0.707]
[0.447 -0.707]]
λ=5.0: Av=[4.472 2.236], λv=[4.472 2.236]
Match: True
λ=2.0: Av=[1.414 -1.414], λv=[1.414 -1.414]
Match: True
Power Iteration — Finding the Dominant Eigenvector
Power iteration finds the largest eigenvalue by repeatedly multiplying by A and normalising. This is the algorithm behind original PageRank.
Algorithm: initialize random v, then loop v = A v / ‖A v‖. Convergence rate depends on the ratio |λ₂/λ₁| — closer to 1 means slower convergence. Rayleigh quotient λ = vᵀAv / vᵀv gives eigenvalue estimate.
def power_iteration(A, n_iter=100, tol=1e-10): """Find dominant eigenvector (largest eigenvalue).""" import numpy as np n = len(A) v = np.random.rand(n) v /= np.linalg.norm(v) for _ in range(n_iter): v_new = A @ v eigenvalue = np.dot(v_new, v) # Rayleigh quotient v_new /= np.linalg.norm(v_new) if np.linalg.norm(v_new - v) < tol: return eigenvalue, v_new v = v_new return eigenvalue, v A = np.array([[4,2],[1,3]], dtype=float) lam, v = power_iteration(A) print(f'Dominant eigenvalue: {lam:.4f}') # 5.0 print(f'Dominant eigenvector: {v}')
Dominant eigenvector: [0.894 0.447]
Geometric Intuition: What Does Av = λv Mean?
An eigenvector points in a direction that is invariant under the transformation A — it only gets stretched (or shrunk, or reversed) by λ. This is the 'natural axis' of the matrix.
Example: A = [[2,0],[0,3]]. The eigenvectors are [1,0]ᵀ (λ=2) and [0,1]ᵀ (λ=3). Multiply any point: x-coordinate doubles, y-coordinate triples — the axes are scaled independently. If you pick any other direction, the point rotates AND scales.
For a symmetric matrix, eigenvectors are orthogonal — they form a perfect coordinate system. For a non‑symmetric matrix, eigenvectors are not orthogonal, and the transformation can shear.
import numpy as np import matplotlib.pyplot as plt # Diagonal matrix – eigenvectors are axes A = np.array([[2,0],[0,3]]) v1 = np.array([1,0]) v2 = np.array([0,1]) print("Eigenvectors are standard axes:", A@v1, A@v2) # [2,0] and [0,3] # Shear matrix – eigenvectors not orthogonal B = np.array([[1,1],[0,1]]) eigvals, eigvecs = np.linalg.eig(B) print("Shear eigenvalues (both 1):", eigvals) print("Only one eigenvector (not span):", eigvecs[:,0])
Real‑World Applications: Where Eigenvalues Matter in Production
Eigenvalues and eigenvectors are not just theoretical. They power critical algorithms:
- PCA (Principal Component Analysis): eigenvectors of the covariance matrix = principal components; eigenvalues = variance explained. Used for dimensionality reduction, noise filtering, and anomaly detection.
- PageRank: the dominant eigenvector of the web's transition matrix gives page importance. Google solved this for billions of pages using iterative methods.
- Structural Engineering: eigenvalues of the stiffness matrix = natural frequencies. If a bridge's frequency matches wind gust frequency, resonance can collapse it (Tacoma Narrows).
- Quantum Mechanics: eigenstates of the Hamiltonian operator are the possible energy levels. The Schrödinger equation is an eigenvalue problem.
- Graph Theory: eigenvalues of the adjacency matrix indicate community structure, connectivity, and node centrality (spectral clustering).
- Eigenvalues are the roots of the characteristic polynomial — they capture the 'stretch factors' along invariant directions.
- The spectral radius (largest |λ|) determines stability of dynamical systems: if > 1, the system diverges.
- The condition number (max λ / min λ) tells you how close the matrix is to singular.
- For symmetric matrices, eigenvalues are real and the eigenvectors form an orthonormal basis — the matrix can be 'diagonalized' perfectly.
Numerical Stability and Production Gotchas
Eigendecomposition is numerically tricky. Common pitfalls:
- Near‑zero eigenvalues cause division by zero in whitening or matrix functions.
- Round‑off asymmetry: a matrix that is theoretically symmetric may have tiny asymmetry due to floating‑point ops, leading to complex eigenvalues.
- Defective matrices: not all matrices have a full set of eigenvectors — numpy.linalg.eig still returns a result, but the eigenvectors may be linearly dependent or wildly inaccurate.
- Condition number: if the condition number (max|λ|/min|λ|) is huge (e.g., > 10¹²), small changes in A cause large changes in eigenvectors. The problem is ill‑posed.
- Always check residual ‖Av
- λv‖.
- Use numpy.linalg.eigh when symmetry is guaranteed.
- For ill‑conditioned cases, switch to SVD (singular value decomposition), which is more stable for any matrix.
- When computing matrix functions (sqrt, inverse, exp) via eigendecomposition, use scipy.linalg.sqrtm, inv, expm which handle stability internally.
import numpy as np from io.thecodeforge.linalg import safe_eigen # hypothetical robust wrapper # Ill‑conditioned matrix (nearly singular) A = np.array([[1, 1], [1, 1 + 1e-12]]) eigvals, eigvecs = np.linalg.eig(A) print("Eigenvalues (should be ~2 and ~1e-12):", eigvals) # Residual check v = eigvecs[:,0] lam = eigvals[0] print("Residual norm:", np.linalg.norm(A@v - lam*v)) # Use safe decomposition instead U, S, Vt = np.linalg.svd(A) print("Singular values:", S) # More stable
| Property | Eigendecomposition (A = QΛQ⁻¹) | SVD (A = UΣVᵀ) |
|---|---|---|
| Applicable matrices | Square only | Any matrix (rectangular too) |
| Requires diagonalizability | Yes – A must have n linearly independent eigenvectors | Always exists |
| Numerical stability | Lower – sensitive to near‑zero eigenvalues | Higher – more robust for ill‑conditioned matrices |
| Orthogonal factors | Only if A is symmetric (Q orthogonal) | Always U and V are orthogonal |
| Performance | Faster for symmetric (eigh) | Slightly slower but more general |
| Inverse / pseudo‑inverse | A⁻¹ = QΛ⁻¹Q⁻¹ (fragile) | A⁺ = VΣ⁺Uᵀ (stable via threshold) |
| Common use case | PCA, PageRank, vibration modes (square matrices) | Recommendation systems, data compression, any linear least squares |
🎯 Key Takeaways
- Av = λv: eigenvector v is unchanged in direction by A, scaled by eigenvalue λ.
- Eigenvalues from characteristic polynomial det(A-λI)=0. Eigenvectors from null space of (A-λI).
- PCA: eigenvectors of covariance matrix = principal components. Eigenvalues = variance explained.
- Power iteration: repeated A×v/‖Av‖ converges to dominant eigenvector — PageRank's original algorithm.
- Symmetric matrices always have real eigenvalues and orthogonal eigenvectors — numpy.linalg.eigh for symmetric.
- Never use eigendecomposition for inversion without checking condition number — use SVD or pinv instead.
⚠ Common Mistakes to Avoid
Interview Questions on This Topic
- QWhat is an eigenvector geometrically?JuniorReveal
- QHow is PageRank related to eigenvectors?Mid-levelReveal
- QWhy do symmetric matrices have real eigenvalues?SeniorReveal
- QHow does PCA use eigenvectors?Mid-levelReveal
- QWhat's the difference between eigendecomposition and SVD?SeniorReveal
Frequently Asked Questions
What is eigendecomposition vs SVD?
Eigendecomposition A = QΛQ⁻¹ requires A to be square and diagonalisable. SVD A = UΣVᵀ works for any matrix (including non‑square, rank‑deficient). SVD is more numerically stable and general. For rectangular matrices (overdetermined systems, data matrices in ML), always use SVD.
Can a matrix have zero as an eigenvalue?
Yes. Zero eigenvalues appear when a matrix is singular (determinant zero). In PCA, a zero eigenvalue means that dimension has no variance. In physical systems, zero eigenvalues correspond to rigid‑body modes (no restoring force). Be careful when using such matrices in inversion — they are not invertible.
How are eigenvalues of a large matrix computed in practice?
The QR algorithm is the workhorse behind LAPACK's dgeev for dense matrices (O(n³)). For sparse matrices (up to millions of dimensions), use iterative methods: Arnoldi (scipy.sparse.linalg.eigs) or Lanczos (for symmetric). These find a few extreme eigenvalues efficiently. For PageRank, a tailored power iteration is used because the matrix is huge but we only need the dominant eigenvector.
What does it mean if eigenvalue λ = 1?
It means the eigenvector is a fixed point: the transformation does not change it at all. PageRank's dominant eigenvalue is exactly 1 (because the transition matrix is stochastic). In Markov chains, λ = 1 corresponds to the stationary distribution.
Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.