Singular Value Decomposition
Everything so far has had a hypothesis. Cholesky needs symmetric positive definite. The eigendecomposition needs square and non-defective. Both hypotheses fail on ordinary data — a table of measurements is not square, so neither applies.
The singular value decomposition has no hypothesis. It exists for every real matrix of every shape, which is why Strang called it the fundamental theorem of linear algebra. And it does more than merely exist: it hands you the rank, the four fundamental subspaces, the pseudo-inverse, and — next page — the provably best low-rank approximation.
What you’ll learn
Section titled “What you’ll learn”- Theorem 4.22: the statement of the SVD, and why is rectangular with zero padding.
- The geometry: rotates in the domain, scales and changes dimension, rotates in the codomain.
- Why and are not inverses of each other, unlike and .
- §4.5.2’s construction: from , from , joined by .
- The singular value equation , and how it differs from the eigenvalue equation.
- §4.5.3: every difference between the eigendecomposition and the SVD, measured rather than asserted.
- The full, reduced and truncated conventions, so you can read any paper’s notation.
Intuition: two rooms and a doorway
Section titled “Intuition: two rooms and a doorway”The eigendecomposition works inside one room. It changes into a better basis, scales, and changes back — and because it starts and ends in the same room, the two basis changes are inverse to each other.
The SVD connects two rooms of possibly different size. finds the best axes in the domain. carries you through the doorway, stretching along those axes and either padding with zeros (if the codomain is bigger) or dropping coordinates (if it is smaller). then finds the best axes in the codomain. Both basis changes are rotations, but they rotate in different spaces, so they cannot be inverses of each other. What links them is .
flowchart LR X["x in R^n
standard basis"] -->|"Vᵀ
rotate in the domain"| XB["coordinates in
the v-basis"] XB -->|"Σ
scale by σᵢ,
pad or drop dimensions"| YB["coordinates in
the u-basis, in R^m"] YB -->|"U
rotate in the codomain"| Y["Ax in R^m
standard basis"] X -->|"A, directly"| Y
flowchart TD EVD["eigendecomposition
A = P D P⁻¹"] SVD["SVD
A = U Σ Vᵀ"] EVD --> E1["needs square"] EVD --> E2["needs non-defective"] EVD --> E3["P and P⁻¹ are inverses:
ONE room"] EVD --> E4["D may be negative
or complex"] SVD --> S1["any shape"] SVD --> S2["always exists"] SVD --> S3["U and V are DIFFERENT
rotations in TWO rooms"] SVD --> S4["Σ is real and
non-negative"] E3 --> SAME["for a symmetric matrix
the two coincide
— spectral theorem"] S3 --> SAME
The math
Section titled “The math”Three things in that statement deserve emphasis.
is the same shape as . Not square. The singular value matrix is unique, and it needs zero padding to fit. If it is diagonal down to row and then all-zero rows (Equation 4.65); if it is diagonal across to column and then all-zero columns (Equation 4.66):
Both outer matrices are orthogonal. So both are rigid motions — rotations, possibly with a reflection. Nothing in the SVD shears. All the stretching lives in the middle, in real non-negative numbers.
can be . Rank zero means the zero matrix, and its SVD is with any orthogonal , . The theorem genuinely covers everything.
The geometry, in three steps
Section titled “The geometry, in three steps”Assume with standard bases and , and a second basis of and of . Then, following the book:
| step | what it does |
|---|---|
| a basis change in the domain , from into — the right-singular vectors become aligned with the coordinate axes | |
| scales the new coordinates by the and adds or deletes dimensions; it is the matrix of with respect to and | |
| a basis change in the codomain , from into the canonical basis of |
The book’s own summary is the sentence to keep: the SVD expresses a change of basis in both the domain and the codomain, in contrast with the eigendecomposition, which operates within the same vector space, applying one basis change and then undoing it. What makes the SVD special is that the two different bases are simultaneously linked by .
Constructing it (§4.5.2)
Section titled “Constructing it (§4.5.2)”The derivation is short and it is the reason the spectral theorem was worth remembering.
Step 1 — from . Substitute the SVD into , which is symmetric whatever is:
using . Compare with the eigendecomposition : the spectral theorem guarantees one exists with an orthonormal , so the eigenvectors of are the right-singular vectors , and its eigenvalues are the squared singular values.
Step 2 — from . The same substitution the other way round (Equation 4.76) gives , so the orthonormal eigenvectors of are the left-singular vectors. And since and have the same nonzero eigenvalues, the nonzero entries of come out the same either way.
Step 3 — join them. The images of the under are already orthogonal:
for . So normalise them:
Rearranged, that is the singular value equation:
which closely resembles the eigenvalue equation — except that the vectors on the two sides are not the same vector. Collecting columns gives (Equation 4.80), and right-multiplying by gives Theorem 4.22.
The eigendecomposition against the SVD (§4.5.3)
Section titled “The eigendecomposition against the SVD (§4.5.3)”| eigendecomposition | SVD | |
|---|---|---|
| exists for | square, and only if a basis of eigenvectors exists | every |
| domain and codomain | the same space | possibly different dimensions |
| are the outer matrices orthonormal? | ’s columns are not necessarily | and always are |
| are they inverse to each other? | yes | no — they change basis in different spaces |
| middle factor | may be negative or complex | all real and non-negative |
| left factor’s columns are eigenvectors of | ||
| right factor’s columns are eigenvectors of | — | |
| for a symmetric matrix | the two are one and the same, by the spectral theorem |
That last row is the bridge. Symmetry collapses the two decompositions into each other — which is why Chapter 10 can describe PCA either way and get the same answer.
Three conventions you will meet
Section titled “Three conventions you will meet”The mathematics is invariant to these; the notation is not.
| name | shapes, for | why anyone uses it |
|---|---|---|
| full SVD — Equation 4.64, used here | is , is , is | both outer matrices are square and orthogonal; you get bases for all four subspaces |
| reduced SVD — Equation 4.89 | is , is , is | is square and diagonal, as in the eigendecomposition |
| truncated SVD — §4.6 | is , is , is | has no zeros on its diagonal at all; this is the approximation of the next page |
In NumPy, full_matrices=True (the default) gives the full SVD and full_matrices=False gives the
reduced one. The book notes that a restriction to is practically unnecessary: when ,
simply has more zero columns than rows and are zero.
Worked example by hand
Section titled “Worked example by hand”Example 4.13 — computing an SVD in three steps
Section titled “Example 4.13 — computing an SVD in three steps”Step 1 — right-singular vectors as the eigenbasis of .
Its eigendecomposition gives eigenvalues , , and the orthonormal eigenvectors
Step 2 — singular values. They come straight out of as square roots. Since there are only two nonzero ones: and . must be the same size as , so
Step 3 — left-singular vectors as normalised images.
Note the third right-singular vector : its eigenvalue is , so and it is an orthonormal basis of the kernel. There is no , because the codomain is only two-dimensional.
Verifying it, digit by digit
Section titled “Verifying it, digit by digit”import numpy as np
A = np.array([[1.0, 0.0, 1.0], [-2.0, 1.0, 0.0]])
G = A.T @ A
print("A^T A ="); print(G.astype(int))
lam, V = np.linalg.eigh(G)
order = np.argsort(-lam)
lam, V = lam[order], V[:, order]
print("eigenvalues of A^T A:", np.round(lam, 12))
print("sqrt of the top two: ", np.round(np.sqrt(lam[:2]), 6), " and sqrt(6) =", round(float(np.sqrt(6)), 6))
# The book's v_j, exactly.
book_V = np.array([[5, 0, -1], [-2, 1, -2], [1, 2, 1]], dtype=float)
book_V = book_V / np.linalg.norm(book_V, axis=0)
print("the book's V is an eigenbasis of A^T A:",
bool(np.allclose(G @ book_V, book_V * lam)))
# Step 3: u_i = A v_i / sigma_i.
for i in range(2):
u = A @ book_V[:, i] / np.sqrt(lam[i])
print(f"u_{i+1} =", np.round(u, 6), " times sqrt(5) =", np.round(u * np.sqrt(5), 6))
print("A v_3 =", np.round(A @ book_V[:, 2], 12), " -> v_3 spans the kernel")
# Reassemble and compare with numpy.
U = np.array([[1.0, 2.0], [-2.0, 1.0]]) / np.sqrt(5)
Sig = np.array([[np.sqrt(6), 0.0, 0.0], [0.0, 1.0, 0.0]])
print("U Sigma V^T - A, largest entry:", f"{float(np.abs(U @ Sig @ book_V.T - A).max()):.2e}")
print("numpy's sigma:", np.round(np.linalg.svd(A, compute_uv=False), 6))A^T A =
[[ 5 -2 1]
[-2 1 0]
[ 1 0 1]]
eigenvalues of A^T A: [ 6. 1. -0.]
sqrt of the top two: [2.44949 1. ] and sqrt(6) = 2.44949
the book's V is an eigenbasis of A^T A: True
u_1 = [ 0.447214 -0.894427] times sqrt(5) = [ 1. -2.]
u_2 = [0.894427 0.447214] times sqrt(5) = [2. 1.]
A v_3 = [0. 0.] -> v_3 spans the kernel
U Sigma V^T - A, largest entry: 1.11e-16
numpy's sigma: [2.44949 1. ]Every number the book prints, reproduced: , , , , and in the kernel.
Example 4.12 — the same matrix as the geometry figure
Section titled “Example 4.12 — the same matrix as the geometry figure”Measured: and , and every entry of the book’s and
agrees to the two decimals it prints. The detail worth checking is the bottom row of
: it is zero, so after every vector has third coordinate
exactly zero — measured as 0.00e+00, not merely small. The grid still lies in a plane. Only
tilts it out.
See it move
Section titled “See it move”The stepped construction, verified frame by frame — and note the singular values it reports for Example 4.13:
The construction of §4.5.2 in order: form the Gram matrix, diagonalise it by the spectral theorem to get V and the squared singular values, take square roots, then set u_i = A v_i / sigma_i and verify orthogonality. The last frame checks the reassembly.
Four movies by three viewers. Watch the singular value strip: two large values and one small one, which is exactly the structure the book reads as two themes plus noise.
From scratch
Section titled “From scratch”import numpy as np
def svd_from_grams(A, tol=1e-10):
"""The construction of section 4.5.2, verbatim — and the wrong way to compute one."""
A = np.asarray(A, dtype=float)
m, n = A.shape
# Step 1: V and sigma^2 from the eigendecomposition of A^T A (Equation 4.73).
lam, V = np.linalg.eigh(A.T @ A)
order = np.argsort(-lam)
lam, V = np.maximum(lam[order], 0.0), V[:, order]
sigma = np.sqrt(lam)
r = int(np.sum(sigma > tol * max(sigma[0], 1.0)))
# Step 3: u_i = A v_i / sigma_i (Equation 4.78), for the nonzero sigmas only.
U = np.zeros((m, m))
for i in range(min(r, m)):
U[:, i] = A @ V[:, i] / sigma[i]
# Complete U to an orthonormal basis of the codomain: those extra columns are
# a basis of the null space of A^T, which the SVD hands you for free.
if r < m:
Q, _ = np.linalg.qr(np.hstack([U[:, :r], np.eye(m)]))
U[:, r:] = Q[:, r:m]
Sig = np.zeros((m, n))
np.fill_diagonal(Sig, sigma[:min(m, n)])
return U, Sig, V.T, r
cases = {
"Ex 4.13 2x3": [[1.0, 0, 1], [-2, 1, 0]],
"Ex 4.12 3x2": [[1.0, -0.8], [0, 1], [1, 0]],
"Ex 4.14 4x3": [[5.0, 4, 1], [5, 5, 0], [0, 0, 5], [1, 0, 4]],
"rank 1 3x3": [[1.0, 2, 3], [2, 4, 6], [3, 6, 9]],
"the zero 2x2": [[0.0, 0], [0, 0]],
}
for name, A in cases.items():
A = np.asarray(A, dtype=float)
U, Sig, Vt, r = svd_from_grams(A)
ref = np.linalg.svd(A, compute_uv=False)
k = min(A.shape)
print(f"{name} rank {r} sigma {np.round(np.diag(Sig)[:k], 6)}")
print(f"{'':14} |U Sigma V^T - A| {float(np.abs(U @ Sig @ Vt - A).max()):.1e}"
f" |U^T U - I| {float(np.abs(U.T @ U - np.eye(A.shape[0])).max()):.1e}"
f" sigma matches numpy to {float(np.abs(np.diag(Sig)[:k] - ref).max()):.1e}")Ex 4.13 2x3 rank 2 sigma [2.44949 1. ]
|U Sigma V^T - A| 4.4e-16 |U^T U - I| 4.4e-16 sigma matches numpy to 4.4e-16
Ex 4.12 3x2 rank 2 sigma [1.624808 1. ]
|U Sigma V^T - A| 1.1e-16 |U^T U - I| 2.2e-16 sigma matches numpy to 4.4e-16
Ex 4.14 4x3 rank 3 sigma [9.643811 6.363891 0.705552]
|U Sigma V^T - A| 2.7e-15 |U^T U - I| 1.2e-14 sigma matches numpy to 4.6e-15
rank 1 3x3 rank 2 sigma [14. 0. 0.]
|U Sigma V^T - A| 2.7e-15 |U^T U - I| 1.0e+00 sigma matches numpy to 1.2e-08
the zero 2x2 rank 0 sigma [0. 0.]
|U Sigma V^T - A| 0.0e+00 |U^T U - I| 0.0e+00 sigma matches numpy to 0.0e+00Four of the five cases behave. The rank-1 case does not, and it is worth reading closely, because it fails in exactly the way the book warns about.
rank 1 3x3 is reported as rank 2, and is
1.0e+00 — is not orthogonal at all, so what came back is not an SVD. Here is the chain.
The matrix is for , so its true singular values are
, , . Forming squares them to , , — but eigh
returns for the second, which is machine precision relative to 196. Taking the
square root turns that into
The square root halves the exponent, so a rounding error at becomes a singular value at
— eight orders of magnitude too big, and comfortably above any sensible tolerance. The rank
test then says , and step 3 computes
, dividing noise by a tiny number and
producing a vector with no relationship to anything. np.linalg.svd on the same matrix reports
, which is correct.
The reconstruction gap is still 2.7e-15, which is the trap: the product
comes out right because the garbage column is
multiplied by a nearly-zero singular value. Checking only the reconstruction would have passed this.
Checking catches it.
The zero matrix, by contrast, works exactly: and every gap 0.0e+00. Theorem 4.22 says
and it means it.
On real data
Section titled “On real data”Reading the plot
Section titled “Reading the plot”From the three-stage figure. Follow the two coloured vectors, not the cloud. In the first panel they
are and , at some oblique angle to the axes. In the second they lie on the
axes — that is all does. In the third they have been stretched by and and
the whole grid now lives in , but flatly: max |x3| = 0.00e+00. The fourth panel is the
only one where the disc leaves the plane.
The number worth staring at is that exact zero. It is not ; it is zero, because ‘s third row contains no nonzero entry that could put anything there. The change of dimension in the SVD is a padding, not a computation.
From the eigen-versus-singular figure. The left panel corrects a common misconception. For the non-symmetric :
| first | second | the pair’s angle | |
|---|---|---|---|
| eigenvalues, along invariant directions | |||
| singular values, along orthogonal directions |
These are different numbers, and neither is a rounding of the other. The eigenvalues say how much the matrix stretches along its two invariant directions; the singular values say how much it stretches along its two orthogonal directions of greatest and least stretch. Those are different questions, and they have the same answer only when the invariant directions happen to be orthogonal.
The angle column is the mechanism. The singular vectors meet at exactly always — that is what ” is orthogonal” means, and it is not an accident of this matrix. The eigenvectors meet at here, so they cannot possibly be the axes of an ellipse. The right panel makes the matrix symmetric and the eigenvector angle becomes ; at that moment both spectra read and , identical to every printed digit.
One quantity survives either way: and . Both factorisations agree about volume even when they disagree about every individual factor.
From the Gram figure. Read it one row at a time; each row is one matrix shape, and the two curves are the sorted spectra of the two Gram matrices. They lie on top of each other. Across the five shapes the nonzero spectra agree to at most and their square roots match the singular values to at most .
The detail the figure adds is what happens to the extra eigenvalues, and the row shows it best: is with nine eigenvalues while is only with four. Four of them match; the other five are zero. Those five zeros are the kernel of , and their eigenvectors are the last five columns of — the free null-space basis from Equation 4.79.
Note also that the two Gram matrices are different sizes and still share a spectrum apart from zeros. That is what makes the construction work from either end: you can build from the smaller one and get the same .
Pitfalls
Section titled “Pitfalls”Compare
Section titled “Compare”| decomposition | hypothesis | outer factors | middle | what it is for |
|---|---|---|---|---|
| LU | none (with pivoting) | triangular | — | solving systems, determinants |
| Cholesky | symmetric positive definite | triangular, and each other’s transpose | — | sampling, fast determinants, §4.3 |
| QR | none | orthogonal, triangular | — | least squares without forming a Gram |
| eigendecomposition | square, non-defective | invertible, inverse to each other | may be negative or complex | matrix powers, §4.4 |
| SVD | none | orthogonal, in different spaces | real, non-negative | rank, all four subspaces, pseudo-inverse, best low-rank approximation |
-
Why can U and V not be inverses of each other, the way P and P-inverse are?
This is the structural difference the book highlights in §4.5.3. The eigendecomposition applies one basis change and then undoes it inside one space; the SVD links two different bases in two different spaces through Sigma.
pch.quizShowAnswer
B — Because they perform basis changes in different vector spaces — U is m by m in the codomain and V is n by n in the domain, so their product is not even defined in general — This is the structural difference the book highlights in §4.5.3. The eigendecomposition applies one basis change and then undoes it inside one space; the SVD links two different bases in two different spaces through Sigma.
-
For the matrix [[4,2],[1,3]] the eigenvalues are 5 and 2 while the singular values are 5.116673 and 1.954395. What went wrong?
What does survive is the product: 5.116673 times 1.954395 is exactly 10, which is the absolute determinant. Both factorisations agree about volume while disagreeing about the individual factors.
pch.quizShowAnswer
B — Nothing. The identity sigma-i equals the absolute value of lambda-i holds only for symmetric matrices; eigenvalues measure stretch along invariant directions and singular values measure stretch along orthogonal ones — What does survive is the product: 5.116673 times 1.954395 is exactly 10, which is the absolute determinant. Both factorisations agree about volume while disagreeing about the individual factors.
-
In the three-stage figure, the third coordinate after Sigma is measured as exactly 0.00e+00 rather than something near 1e-16. Why exactly zero?
That is the whole content of Equations 4.65 and 4.66. Sigma has the shape of A and pads with zeros, so it can lift into a higher dimension only by putting zeros there. Only U tilts anything out of that plane.
pch.quizShowAnswer
B — Because the bottom row of Sigma is all zeros, so there is no arithmetic that could put a nonzero value in the third coordinate — the change of dimension is a padding, not a computation — That is the whole content of Equations 4.65 and 4.66. Sigma has the shape of A and pads with zeros, so it can lift into a higher dimension only by putting zeros there. Only U tilts anything out of that plane.
-
The book states the Gram construction and then immediately warns against using it. Why?
The from-scratch implementation on this page shows a symptom of the same disease: eigh returns a tiny negative eigenvalue where zero was expected, and without clipping it np.sqrt produces nan.
pch.quizShowAnswer
B — Because forming A-transpose A squares the condition number, so at a condition number of 1e8 — routine for a design matrix — the Gram matrix is numerically singular in double precision and the small singular values are destroyed — The from-scratch implementation on this page shows a symptom of the same disease: eigh returns a tiny negative eigenvalue where zero was expected, and without clipping it np.sqrt produces nan.
-
For a 3x5 matrix of rank 3, what do the last two columns of V give you?
The book makes the point right after Equation 4.79: where the singular value equation says nothing, the vectors are still orthonormal, so the SVD hands you a basis for the null space for free.
pch.quizShowAnswer
B — An orthonormal basis of the kernel of A, since A v-i equals zero for those columns while they remain orthonormal by construction — The book makes the point right after Equation 4.79: where the singular value equation says nothing, the vectors are still orthonormal, so the SVD hands you a basis for the null space for free.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Example 4.13, the construction in three steps
Section titled “Exercise 1 – Example 4.13, the construction in three steps”Exercise 2 – Sigma has the shape of A
Section titled “Exercise 2 – Sigma has the shape of A”Exercise 3 – Eigenvalues against singular values
Section titled “Exercise 3 – Eigenvalues against singular values”Exercise 4 – The kernel, for free
Section titled “Exercise 4 – The kernel, for free”Exercise 5 – Why the Gram route is a proof and not an algorithm
Section titled “Exercise 5 – Why the Gram route is a proof and not an algorithm”Recall card
Section titled “Recall card”- The SVD has no hypothesis: A equals U Sigma V-transpose for every real matrix of every shape, which is why it is called the fundamental theorem of linear algebra.
- Both outer matrices are orthogonal, and Sigma has the shape of A — rectangular, with zero padding below or to the right, and real non-negative diagonal entries in descending order.
- U and V change basis in different spaces, so unlike P and P-inverse they are not inverses of each other; Sigma is what links the two bases.
- The geometry is rotate, scale-and-change-dimension, rotate. The change of dimension is a padding: after Sigma the extra coordinates are exactly zero, not merely small.
- V comes from the eigenvectors of A-transpose A and U from those of A A-transpose, with the squared singular values as the shared nonzero eigenvalues of both.
- The singular value equation is A v-i = sigma-i u-i, which resembles the eigenvalue equation except that the two sides involve different vectors.
- The trailing columns of V are an orthonormal basis of the kernel, so the SVD hands you the null space for free.
- Never compute an SVD through the Gram matrix: it squares the condition number — the smallest singular value is 4 percent wrong at kappa 1e8 and lost entirely at 1e12 — and the square root halves the exponent, turning a 1e-16 rounding error into a spurious 1e-8 singular value that breaks the rank test.
- Singular values are the absolute eigenvalues only for symmetric matrices. For [[4,2],[1,3]] the eigenvalues are 5 and 2 while the singular values are 5.116673 and 1.954395; what always holds is that their product is the absolute determinant.
- For a symmetric matrix the eigendecomposition and the SVD are the same thing, which follows from the spectral theorem and is why PCA can be described either way.
Next: Matrix Approximation — truncate the sum and you get the provably closest low-rank matrix, with the error known in advance.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading