Type at least 2 letters. Enter opens the first result, Esc closes.

    Linear Algebra · Lesson 8 · The SVD and PCA

    The SVD, Eckart–Young–Mirsky, polar decomposition and the pseudoinverse

    Intermediate 95 min 5 interactive figures 5 videos Colab notebook

    Before this lesson: lesson 06 (the spectral theorem), lesson 07 (A⊤AA^\top A is PSD) and lesson 02 (the four subspaces). Time: about 95 minutes, plus the notebook.


    Why this matters #

    The spectral theorem only works for symmetric matrices. The SVD works for every matrix, of every shape. It gives the rank in floating point, orthonormal bases for all four subspaces, the best low-rank approximation, the pseudoinverse and the nearest orthogonal matrix. In ML it is behind low-rank compression, PCA (lesson 09), the spectral norm of a layer and the condition number σmax⁡/σmin⁡\sigma_{\max}/\sigma_{\min} (lesson 13). In science, the SVD of a space–time data matrix is called POD (proper orthogonal decomposition) in fluid dynamics and EOF analysis (empirical orthogonal functions) in climate science.

    1. Predict first #

    2. The picture #

    Start with a diagonal matrix, A=diag⁡(3,−2)A = \operatorname{diag}(3, -2). It stretches the x1x_1-axis by 3, and it stretches the x2x_2-axis by 2 and flips it. The SVD separates the stretch from the flip: the singular values are 33 and 22 (always ≥0\ge 0), and the sign goes into an orthogonal factor.

    Now take any 2×22 \times 2 matrix AA. It maps the unit circle to an ellipse. Call the semi-axes of the ellipse σ1u1\sigma_1 u_1 and σ2u2\sigma_2 u_2, with orthogonal unit vectors u1,u2u_1, u_2 and σ1≥σ2≥0\sigma_1 \ge \sigma_2 \ge 0. The unit vectors v1,v2v_1, v_2 that land on these axes are also orthogonal. So an orthonormal input basis goes to an orthogonal output set:

    Avi=σiui.A v_i = \sigma_i u_i .

    Put the viv_i as columns of VV and the uiu_i as columns of UU. Then AV=UΣAV = U\Sigma, so A=UΣV⊤A = U\Sigma V^\top. Read it from right to left:

    1. V⊤V^\top writes the input in coordinates along the viv_i.
    2. Σ\Sigma stretches coordinate ii by σi\sigma_i. If AA is not square, Σ\Sigma also changes the dimension from nn to mm.
    3. UU places the result along the uiu_i.

    UU and VV are orthogonal, so each is a rotation or a reflection. For square AA with det⁡A<0\det A < 0, at least one of them must contain a reflection, because det⁡A=det⁡U⋅det⁡Σ⋅det⁡V\det A = \det U \cdot \det \Sigma \cdot \det V and det⁡Σ≥0\det \Sigma \ge 0.

    Interactive figure: loading…

    Press Play, or step through the three factors. V⊤V^\top turns the orange v1v_1 and the blue v2v_2 onto the axes, Σ\Sigma stretches the axes by σ1\sigma_1 and σ2\sigma_2, and UU turns the ellipse into place: it ends on the dashed violet ellipse {Ax:∥x∥=1}\{Ax : \|x\| = 1\}. Try diag(3, −2) and det < 0: the letter F comes out mirrored. The figure puts the mirror into UU (it could also go into VV), never into Σ\Sigma. With rank 1 the circle is flattened to a segment.

    Why such a pair always exists #

    The claim "some orthonormal pair v1,v2v_1, v_2 has orthogonal images" is the heart of the SVD. Here is why it is true for every 2×22 \times 2 matrix, step by step.

    1. Turn a pair. Every orthonormal pair can be written as v1=(cos⁡t,sin⁡t)v_1 = (\cos t, \sin t) and v2=(−sin⁡t,cos⁡t)v_2 = (-\sin t, \cos t) for an angle tt, up to the sign of v2v_2.
    2. Measure the angle of the images. Let f(t)=(Av1)⋅(Av2)=v1⊤A⊤A v2f(t) = (Av_1) \cdot (Av_2) = v_1^\top A^\top A\, v_2. The images are orthogonal exactly when f(t)=0f(t) = 0.
    3. Turn by 90°90°. Then v1v_1 becomes v2v_2, and v2v_2 becomes −v1-v_1. So f(t+90°)=(Av2)⋅(−Av1)=−f(t)f(t + 90°) = (Av_2) \cdot (-Av_1) = -f(t): the sign of ff flips.
    4. So ff has a zero. ff is a continuous function of tt. It changes sign between tt and t+90°t + 90°, so it is zero somewhere in between (intermediate value theorem).
    5. What the zero means. f(t)=0f(t) = 0 says that A⊤Av1A^\top A v_1 has no component along v2v_2. In the plane, that leaves only the direction v1v_1: A⊤Av1=λ1v1A^\top A v_1 = \lambda_1 v_1. So v1v_1, and in the same way v2v_2, is an eigenvector of the symmetric matrix A⊤AA^\top A.
    6. Largest and smallest stretch. Since ddtv1=v2\frac{d}{dt} v_1 = v_2, we get ddt∥Av1∥2=2 v2⊤A⊤A v1=2f(t)\frac{d}{dt}\|Av_1\|^2 = 2\, v_2^\top A^\top A\, v_1 = 2f(t). So f=0f = 0 exactly where ∥Av1∥\|Av_1\| is largest or smallest. These extreme lengths are σ1\sigma_1 and σ2\sigma_2.

    In nn dimensions the same search is the spectral theorem for A⊤AA^\top A (lesson 06). The proof in section 3 uses it directly.

    Interactive figure: loading…

    Turn the pair with the slider, or drag the tip of v1v_1. The magenta number is the angle between Av1Av_1 and Av2Av_2. Find the turn where it is 90°90°, then press Find the orthogonal images to check. Notice in the lower chart: at that same turn, ∥Av1∥\|Av_1\| reaches its maximum σ1\sigma_1 (or its minimum σ2\sigma_2). For the shear the answer is t≈58.3°t \approx 58.3°, not the eigenvector (1,0)(1, 0) of AA. Try 2 × rotation: every pair works, because A⊤A=4IA^\top A = 4I.

    Quick check

    You turn the orthonormal pair until Av1⊥Av2Av_1 \perp Av_2. Which statement is now true?

    The same factorization is a sum of rank-1 layers, sorted by importance:

    A=σ1u1v1⊤+σ2u2v2⊤+⋯+σrurvr⊤.A = \sigma_1 u_1 v_1^\top + \sigma_2 u_2 v_2^\top + \dots + \sigma_r u_r v_r^\top .

    3. Definitions and main results #

    The SVD #

    Let A∈Rm×nA \in \mathbb{R}^{m \times n} have rank rr, and let p=min⁡(m,n)p = \min(m, n).

    • Full SVD: A=UΣV⊤A = U\Sigma V^\top with U∈Rm×mU \in \mathbb{R}^{m\times m} and V∈Rn×nV \in \mathbb{R}^{n \times n} orthogonal, and Σ∈Rm×n\Sigma \in \mathbb{R}^{m \times n} zero except for the diagonal σ1≥⋯≥σp≥0\sigma_1 \ge \dots \ge \sigma_p \ge 0. Exactly rr of the σi\sigma_i are positive.
    • Thin (reduced) SVD (NumPy's full_matrices=False): keep pp columns of UU and VV and the p×pp \times p block of Σ\Sigma.
    • Compact (rank-rr) SVD: keep only rr columns: A=UrΣrVr⊤A = U_r \Sigma_r V_r^\top with Ur∈Rm×rU_r \in \mathbb{R}^{m \times r}, Σr∈Rr×r\Sigma_r \in \mathbb{R}^{r\times r}, Vr∈Rn×rV_r \in \mathbb{R}^{n \times r}.
    • The singular values σi\sigma_i are unique. The left singular vectors uiu_i and the right singular vectors viv_i are unique only up to sign (for distinct nonzero σi\sigma_i), and a sign flip of uiu_i needs the same flip of viv_i.
    • Column by column: Avi=σiuiAv_i = \sigma_i u_i and A⊤ui=σiviA^\top u_i = \sigma_i v_i.
    • The four subspaces (lesson 02) get orthonormal bases: u1,…,uru_1, \dots, u_r span C(A)C(A); ur+1,…,umu_{r+1}, \dots, u_m span N(A⊤)N(A^\top); v1,…,vrv_1, \dots, v_r span C(A⊤)C(A^\top); vr+1,…,vnv_{r+1}, \dots, v_n span N(A)N(A).
    • Norms: ∥A∥2=σ1\|A\|_2 = \sigma_1 and ∥A∥F=σ12+⋯+σr2\|A\|_F = \sqrt{\sigma_1^2 + \dots + \sigma_r^2}, because orthogonal factors do not change these norms.

    The three shapes for one example. Take m=4m = 4, n=3n = 3 and rank r=2r = 2, so p=3p = 3 and σ3=0\sigma_3 = 0.

    Form UU Σ\Sigma VV What is dropped
    full 4×44 \times 4 4×34 \times 3 3×33 \times 3 nothing
    thin 4×34 \times 3 3×33 \times 3, diagonal (σ1,σ2,0)(\sigma_1, \sigma_2, 0) 3×33 \times 3 u4u_4, which only meets a zero row of Σ\Sigma
    compact 4×24 \times 2 2×22 \times 2 3×23 \times 2 also u3u_3, v3v_3, which only meet σ3=0\sigma_3 = 0

    All three give the same product AA. The dropped columns are multiplied by zeros of Σ\Sigma, so they do not change the product. They are still useful: u3,u4u_3, u_4 span N(A⊤)N(A^\top) and v3v_3 spans N(A)N(A).

    Quick check

    AA is 4×34 \times 3 with rank 2. What is the shape of UU in the thin SVD (full_matrices=False)?

    Theorem (SVD existence) #

    Every real m×nm\times n matrix AA has an SVD.

    Proof.

    1. A⊤AA^\top A is symmetric and PSD: x⊤A⊤Ax=∥Ax∥2≥0x^\top A^\top A x = \|Ax\|^2 \ge 0 (lesson 07).
    2. Spectral theorem: A⊤A=VΛV⊤A^\top A = V \Lambda V^\top with VV orthogonal and λ1≥⋯≥λn≥0\lambda_1 \ge \dots \ge \lambda_n \ge 0. Since N(A⊤A)=N(A)N(A^\top A) = N(A), exactly rr eigenvalues are positive.
    3. For i≤ri \le r, set σi=λi\sigma_i = \sqrt{\lambda_i} and ui=Avi/σiu_i = A v_i / \sigma_i.
    4. The uiu_i are orthonormal: ui⊤uj=vi⊤A⊤Avj/(σiσj)=λjvi⊤vj/(σiσj)=δiju_i^\top u_j = v_i^\top A^\top A v_j/(\sigma_i\sigma_j) = \lambda_j v_i^\top v_j/(\sigma_i \sigma_j) = \delta_{ij}.
    5. For i>ri > r: ∥Avi∥2=λi=0\|Av_i\|^2 = \lambda_i = 0, so Avi=0Av_i = 0.
    6. Extend u1,…,uru_1, \dots, u_r to an orthonormal basis u1,…,umu_1, \dots, u_m of Rm\mathbb{R}^m (the new vectors span N(A⊤)N(A^\top)).
    7. Now AV=UΣAV = U\Sigma column by column (Avi=σiuiAv_i = \sigma_i u_i for i≤ri \le r, Avi=0Av_i = 0 for i>ri > r). Multiply by V⊤V^\top on the right: A=UΣV⊤A = U\Sigma V^\top. ■\blacksquare

    Steps 5 and 6 on a small example. Take the rank-1 matrix A=(111100)A = \begin{pmatrix} 1 & 1 \\ 1 & 1 \\ 0 & 0 \end{pmatrix} (m=3m = 3, n=2n = 2, r=1r = 1).

    • A⊤A=(2222)A^\top A = \begin{pmatrix} 2 & 2 \\ 2 & 2 \end{pmatrix} has eigenvalues 44 and 00, with v1=(1,1)/2v_1 = (1, 1)/\sqrt2 and v2=(1,−1)/2v_2 = (1, -1)/\sqrt2.
    • Step 3: σ1=2\sigma_1 = 2 and u1=Av1/2=(2,2,0)/2=(1,1,0)/2u_1 = Av_1/2 = (\sqrt2, \sqrt2, 0)/2 = (1, 1, 0)/\sqrt2.
    • Step 5: Av2=(0,0,0)Av_2 = (0, 0, 0). There is no σ2>0\sigma_2 > 0 to divide by, so step 3 cannot give u2u_2.
    • Step 6: N(A⊤)N(A^\top) is the set of yy with y1+y2=0y_1 + y_2 = 0, a plane. Pick any orthonormal basis of it, for example u2=(1,−1,0)/2u_2 = (1, -1, 0)/\sqrt2 and u3=(0,0,1)u_3 = (0, 0, 1). Both are orthogonal to u1u_1.
    • Result: U=[u1 u2 u3]U = [u_1\ u_2\ u_3], Σ=(200000)\Sigma = \begin{pmatrix} 2 & 0 \\ 0 & 0 \\ 0 & 0 \end{pmatrix}, V=[v1 v2]V = [v_1\ v_2], and UΣV⊤=2 u1v1⊤=AU\Sigma V^\top = 2\,u_1 v_1^\top = A.

    The choice in step 6 is not unique: any orthonormal basis of the plane N(A⊤)N(A^\top) works. It does not matter, because u2u_2 and u3u_3 meet only zeros of Σ\Sigma.

    Two consequences. First, A⊤A=VΣ⊤ΣV⊤A^\top A = V\Sigma^\top\Sigma V^\top and AA⊤=UΣΣ⊤U⊤AA^\top = U\Sigma\Sigma^\top U^\top have the same nonzero eigenvalues σi2\sigma_i^2. Second, step 3 is the safe way to pair UU with VV. If you eigendecompose AA⊤AA^\top separately, each uiu_i comes with an arbitrary sign (or an arbitrary basis of a repeated eigenspace), and then UΣV⊤≠AU\Sigma V^\top \ne A.

    In floating point, do not compute the SVD from A⊤AA^\top A. Forming it squares the condition number, and singular values below about ε σ1≈10−8σ1\sqrt{\varepsilon}\,\sigma_1 \approx 10^{-8}\sigma_1 drown in rounding error (section 5). Library SVDs work on AA directly (lesson 14).

    Theorem (Eckart–Young–Mirsky) #

    Let Ak=∑i≤kσiuivi⊤A_k = \sum_{i \le k} \sigma_i u_i v_i^\top (keep the kk largest terms, k<rk < r). This is the truncated SVD. For every BB with rank⁡B≤k\operatorname{rank} B \le k:

    ∥A−B∥2≥∥A−Ak∥2=σk+1,∥A−B∥F≥∥A−Ak∥F=σk+12+⋯+σr2.\|A - B\|_2 \ge \|A - A_k\|_2 = \sigma_{k+1}, \qquad \|A - B\|_F \ge \|A - A_k\|_F = \sqrt{\sigma_{k+1}^2 + \dots + \sigma_r^2}.

    Why this is true, in words (n=3n = 3, k=1k = 1). Let BB have rank 1. We show that BB misses AA by at least σ2\sigma_2 somewhere.

    1. BB has rank 1, so by rank–nullity its null space N(B)N(B) has dimension 3−1=23 - 1 = 2: a plane.
    2. W=span⁡(v1,v2)W = \operatorname{span}(v_1, v_2) is also a plane. On WW, AA stretches every unit vector by at least σ2\sigma_2.
    3. Two planes through 00 in R3\mathbb{R}^3 always share a line, because 2+2>32 + 2 > 3. Take a unit vector zz on that line.
    4. Bz=0Bz = 0, so (A−B)z=Az(A - B)z = Az, and ∥Az∥≥σ2\|Az\| \ge \sigma_2 because z∈Wz \in W.

    This is the same dimension-counting idea as Courant–Fischer in lesson 06, which has a figure of two planes that share a line. The general proof replaces "plane" by the right dimensions.

    Proof (spectral norm).

    1. A−Ak=∑i>kσiuivi⊤A - A_k = \sum_{i > k} \sigma_i u_i v_i^\top is itself an SVD, with largest singular value σk+1\sigma_{k+1}. So ∥A−Ak∥2=σk+1\|A - A_k\|_2 = \sigma_{k+1}.
    2. Let rank⁡B≤k\operatorname{rank} B \le k. By rank–nullity, dim⁡N(B)≥n−k\dim N(B) \ge n - k.
    3. W=span⁡(v1,…,vk+1)W = \operatorname{span}(v_1, \dots, v_{k+1}) has dimension k+1k+1. Since (n−k)+(k+1)>n(n-k) + (k+1) > n, the two subspaces share a unit vector z∈N(B)∩Wz \in N(B) \cap W.
    4. (A−B)z=Az=∑i≤k+1σi(vi⊤z)ui(A - B)z = Az = \sum_{i \le k+1} \sigma_i (v_i^\top z) u_i, so ∥(A−B)z∥2=∑i≤k+1σi2(vi⊤z)2≥σk+12∑i≤k+1(vi⊤z)2=σk+12\|(A-B)z\|^2 = \sum_{i \le k+1} \sigma_i^2 (v_i^\top z)^2 \ge \sigma_{k+1}^2 \sum_{i\le k+1}(v_i^\top z)^2 = \sigma_{k+1}^2.
    5. Hence ∥A−B∥2≥∥(A−B)z∥≥σk+1\|A - B\|_2 \ge \|(A-B)z\| \ge \sigma_{k+1}. ■\blacksquare
    Proof of the Frobenius case (uses Weyl's inequality for singular values)

    This proof uses one result that this track does not prove in full: Weyl's inequality for singular values,

    σi+j−1(X+Y)≤σi(X)+σj(Y)(i,j≥1, i+j−1≤min⁡(m,n)).\sigma_{i+j-1}(X + Y) \le \sigma_i(X) + \sigma_j(Y) \qquad (i, j \ge 1,\ i + j - 1 \le \min(m, n)).

    See Horn and Johnson, Topics in Matrix Analysis (Cambridge University Press, 1991), Theorem 3.3.16. The special case j=1j = 1 gives ∣σk(A+E)−σk(A)∣≤∥E∥2|\sigma_k(A + E) - \sigma_k(A)| \le \|E\|_2, which lesson 17 proves.

    1. Let rank⁡B≤k\operatorname{rank} B \le k. Then σk+1(B)=0\sigma_{k+1}(B) = 0.
    2. Use Weyl with X=A−BX = A - B, Y=BY = B and j=k+1j = k + 1. Since X+Y=AX + Y = A: σi+k(A)≤σi(A−B)+σk+1(B)=σi(A−B)\sigma_{i+k}(A) \le \sigma_i(A - B) + \sigma_{k+1}(B) = \sigma_i(A - B).
    3. Square and sum over i=1,…,p−ki = 1, \dots, p - k: ∑i>kσi(A)2≤∑i≤p−kσi(A−B)2≤∥A−B∥F2\sum_{i > k}\sigma_i(A)^2 \le \sum_{i \le p-k} \sigma_i(A - B)^2 \le \|A - B\|_F^2.
    4. The left side is ∥A−Ak∥F2\|A - A_k\|_F^2. ■\blacksquare

    Reading the formulas. The relative Frobenius error is ∑i>kσi2/∑iσi2\sqrt{\sum_{i>k}\sigma_i^2 / \sum_i \sigma_i^2}: the root of the fraction of "energy" that you drop. For an error budget, pick the smallest kk that meets it.

    Other norms, and when the best approximation is unique
    • Mirsky extended the theorem to every unitarily invariant norm (a norm with ∥Q1AQ2∥=∥A∥\|Q_1 A Q_2\| = \|A\| for orthogonal Q1,Q2Q_1, Q_2). For example, in the nuclear norm ∥A∥∗=∑iσi\|A\|_* = \sum_i \sigma_i the error of AkA_k is σk+1+⋯+σr\sigma_{k+1} + \dots + \sigma_r, and it is again optimal.
    • The Frobenius minimizer is unique when σk>σk+1\sigma_k > \sigma_{k+1}. The spectral-norm minimizer is usually not unique. A simple case: for A=diag⁡(3,1)A = \operatorname{diag}(3, 1) and k=1k = 1, both diag⁡(3,0)\operatorname{diag}(3, 0) and diag⁡(3.5,0)\operatorname{diag}(3.5, 0) have rank 1 and spectral error ∥A−B∥2=1=σ2\|A - B\|_2 = 1 = \sigma_2.
    Interactive figure: loading…

    Move the rank slider. The middle image is AkA_k, the sum of the first kk layers σiuivi⊤\sigma_i u_i v_i^\top. The chart shows the singular values you keep, the grey ones you drop, and the relative error, which is exactly ∑i>kσi2/∑iσi2\sqrt{\sum_{i>k}\sigma_i^2/\sum_i\sigma_i^2}. Switch to axis-aligned blocks: a background and three rectangles need only rank 4, and the error drops to rounding level. The rotated square has the same kind of content but needs far more terms, because its edges are not aligned with the rows and columns. Set the third panel to k-th layer to see single rank-1 terms.

    Quick check

    AA has singular values 5,3,2,15, 3, 2, 1. What is ∥A−A2∥F\|A - A_2\|_F, the Frobenius error of the best rank-2 approximation?

    Polar decomposition #

    For square AA, insert V⊤V=IV^\top V = I into the SVD:

    A=UΣV⊤=(UV⊤)(VΣV⊤)=QP,A = U\Sigma V^\top = (UV^\top)(V\Sigma V^\top) = QP,
    Q=UV⊤,P=VΣV⊤=(A⊤A)1/2.Q = UV^\top, \qquad P = V\Sigma V^\top = (A^\top A)^{1/2}.

    For m>nm > n, use the thin SVD; then QQ is m×nm \times n with orthonormal columns.

    • Meaning. PP is symmetric PSD: it stretches by σi\sigma_i along the orthogonal axes viv_i. QQ is orthogonal: it rotates or reflects. This is the matrix version of z=eiθ∣z∣z = e^{i\theta}|z| for a complex number.
    • Uniqueness. PP is always unique (the PSD square root of A⊤AA^\top A is unique). If AA is invertible, Q=AP−1Q = AP^{-1} is unique too.
    • Rotation or not. For invertible square AA, det⁡Q=sign⁡(det⁡A)\det Q = \operatorname{sign}(\det A). So QQ is a rotation only when det⁡A>0\det A > 0. Example: the swap A=(0110)A = \begin{pmatrix}0&1\\1&0\end{pmatrix} has P=IP = I and Q=AQ = A, a reflection. In the same way, UU and VV in the SVD are orthogonal, not always rotations (lesson 04 explains the difference).
    • Nearest orthogonal matrix. Q=UV⊤Q = UV^\top minimizes ∥A−W∥F\|A - W\|_F over all orthogonal WW.
    Why UV⊤UV^\top is the nearest orthogonal matrix
    1. For orthogonal WW, ∥W∥F2=n\|W\|_F^2 = n. Expand: ∥A−W∥F2=∥A∥F2+n−2tr⁡(W⊤A)\|A - W\|_F^2 = \|A\|_F^2 + n - 2\operatorname{tr}(W^\top A). So minimizing the distance means maximizing tr⁡(W⊤A)\operatorname{tr}(W^\top A).
    2. Insert the SVD: tr⁡(W⊤A)=tr⁡(W⊤UΣV⊤)=tr⁡(V⊤W⊤U Σ)=tr⁡(ZΣ)\operatorname{tr}(W^\top A) = \operatorname{tr}(W^\top U\Sigma V^\top) = \operatorname{tr}(V^\top W^\top U\,\Sigma) = \operatorname{tr}(Z\Sigma), with Z=V⊤W⊤UZ = V^\top W^\top U (the trace does not change under cyclic shifts).
    3. ZZ is a product of orthogonal matrices, so it is orthogonal and every entry has ∣zii∣≤1|z_{ii}| \le 1.
    4. tr⁡(ZΣ)=∑iziiσi≤∑iσi\operatorname{tr}(Z\Sigma) = \sum_i z_{ii}\sigma_i \le \sum_i \sigma_i, with equality at Z=IZ = I, that is, W=UV⊤W = UV^\top. ■\blacksquare
    Interactive figure: loading…

    The same factors, read as A=QPA = QP. P=VΣV⊤P = V\Sigma V^\top stretches along v1v_1 and v2v_2 without turning them; then Q=UV⊤Q = UV^\top moves the ellipse into place. Here det⁡A<0\det A < 0, so QQ is a reflection: the F is mirrored. Pick shear or symmetric to get a rotation instead.

    Pseudoinverse #

    A+=VΣ+U⊤=∑i=1r1σi viui⊤∈Rn×m,A^+ = V \Sigma^+ U^\top = \sum_{i=1}^{r} \frac{1}{\sigma_i}\, v_i u_i^\top \in \mathbb{R}^{n\times m},

    where Σ+\Sigma^+ (n×mn \times m) has 1/σi1/\sigma_i for each nonzero σi\sigma_i and zeros everywhere else. This is the (Moore–Penrose) pseudoinverse. A+A^+ undoes AA from the column space back to the row space, and it sends N(A⊤)N(A^\top) to zero:

    • AA+=UrUr⊤AA^+ = U_rU_r^\top is the orthogonal projector onto C(A)C(A);
    • A+A=VrVr⊤A^+A = V_rV_r^\top is the orthogonal projector onto C(A⊤)C(A^\top).

    Special cases: A+=(A⊤A)−1A⊤A^+ = (A^\top A)^{-1}A^\top for full column rank, A+=A⊤(AA⊤)−1A^+ = A^\top(AA^\top)^{-1} for full row rank, and A+=A−1A^+ = A^{-1} for invertible AA.

    Theorem. x+=A+bx^+ = A^+ b minimizes ∥Ax−b∥2\|Ax - b\|_2, and among all minimizers it has the smallest ∥x∥2\|x\|_2. It is the minimum-norm least-squares solution.

    Proof. Let c=U⊤bc = U^\top b and y=V⊤xy = V^\top x. Orthogonal maps keep 2-norms, so ∥x∥=∥y∥\|x\| = \|y\|.

    1. ∥Ax−b∥2=∥UΣV⊤x−b∥2=∥Σy−c∥2=∑i≤r(σiyi−ci)2+∑i>rci2\|Ax - b\|^2 = \|U\Sigma V^\top x - b\|^2 = \|\Sigma y - c\|^2 = \sum_{i \le r} (\sigma_i y_i - c_i)^2 + \sum_{i > r} c_i^2.
    2. The second sum does not depend on xx. The first sum is zero exactly when yi=ci/σiy_i = c_i/\sigma_i for i≤ri \le r; the yiy_i with i>ri > r are free. These are all the least-squares solutions.
    3. ∥x∥2=∥y∥2=∑i≤r(ci/σi)2+∑i>ryi2\|x\|^2 = \|y\|^2 = \sum_{i\le r} (c_i/\sigma_i)^2 + \sum_{i>r} y_i^2 is smallest when the free yiy_i are 00.
    4. That yy is Σ+c\Sigma^+ c, so x=VΣ+U⊤b=A+bx = V\Sigma^+U^\top b = A^+ b. ■\blacksquare

    Geometrically, Ax+Ax^+ is the projection of bb onto C(A)C(A), and x+x^+ lies in the row space. Any other solution is x++wx^+ + w with w∈N(A)w \in N(A), and w⊥x+w \perp x^+, so ∥x++w∥2=∥x+∥2+∥w∥2\|x^+ + w\|^2 = \|x^+\|^2 + \|w\|^2.

    Numerics. A+A^+ is not a continuous function of AA: diag⁡(1,ε)+=diag⁡(1,1/ε)\operatorname{diag}(1, \varepsilon)^+ = \operatorname{diag}(1, 1/\varepsilon) blows up as ε→0\varepsilon \to 0, but diag⁡(1,0)+=diag⁡(1,0)\operatorname{diag}(1, 0)^+ = \operatorname{diag}(1, 0). So libraries treat σi≤rcond⋅σ1\sigma_i \le \text{rcond}\cdot\sigma_1 as zero. The number of singular values above this cut-off is the numerical rank. This truncation is a form of regularization, like ridge.

    Interactive figure: loading…

    Two planes: the input space of xx and the output space of bb. With σ2=0\sigma_2 = 0 the matrix has rank 1. Drag bb: Ax+Ax^+ is its projection onto the line C(A)C(A), and the dashed magenta residual is perpendicular to it. In the input space, every point of the thick violet line is a least-squares solution; x+x^+ is the one in the row space, closest to 0. Drag xx along the line and compare ∥x∥\|x\|. Then raise σ2\sigma_2 a little (0.05): AA becomes invertible and x+x^+ jumps far away. Switch on the cut-off: σ2≤0.1 σ1\sigma_2 \le 0.1\,\sigma_1 is treated as 0, as rcond does, and x+x^+ stays small at the price of a nonzero residual.

    Quick check

    What is the pseudoinverse of A=diag⁡(2,0)A = \operatorname{diag}(2, 0)?

    Answers to section 1. (1) No. A⊤A=(1112)A^\top A = \begin{pmatrix}1&1\\1&2\end{pmatrix} has eigenvalues (3±5)/2(3 \pm \sqrt5)/2, so σ=(5±1)/2≈1.618, 0.618\sigma = (\sqrt5 \pm 1)/2 \approx 1.618,\ 0.618. Eigenvalues describe directions that stay on their own line; singular values describe stretching. The products agree: σ1σ2=∣det⁡A∣=1\sigma_1\sigma_2 = |\det A| = 1. (2) By Eckart–Young, 12+0.12=1.01≈1.005\sqrt{1^2 + 0.1^2} = \sqrt{1.01} \approx 1.005.

    4. Worked example #

    A=(11111−1)(3×2, rank 2)A = \begin{pmatrix} 1 & 1 \\ 1 & 1 \\ 1 & -1 \end{pmatrix} \qquad (3 \times 2, \text{ rank } 2)

    SVD. A⊤A=(3113)A^\top A = \begin{pmatrix} 3 & 1 \\ 1 & 3\end{pmatrix} has eigenvalues 44 and 22, with eigenvectors v1=(1,1)/2v_1 = (1,1)/\sqrt2 and v2=(1,−1)/2v_2 = (1,-1)/\sqrt2. So σ1=2\sigma_1 = 2, σ2=2\sigma_2 = \sqrt2.

    • u1=Av1/σ1=(2,2,0)/(22)=(1,1,0)/2u_1 = Av_1/\sigma_1 = (2, 2, 0)/(2\sqrt2) = (1, 1, 0)/\sqrt2.
    • u2=Av2/σ2=(0,0,2)/(2⋅2)=(0,0,1)u_2 = Av_2/\sigma_2 = (0, 0, 2)/(\sqrt2\cdot\sqrt2) = (0, 0, 1).
    • For the full SVD, u3=(1,−1,0)/2u_3 = (1, -1, 0)/\sqrt2 spans N(A⊤)N(A^\top) (check: A⊤u3=0A^\top u_3 = 0).
    Quick check

    For this AA, what is the spectral norm ∥A∥2\|A\|_2?

    Best rank-1 approximation. A1=σ1u1v1⊤=(111100)A_1 = \sigma_1 u_1 v_1^\top = \begin{pmatrix} 1 & 1 \\ 1 & 1 \\ 0 & 0\end{pmatrix} and A−A1=(00001−1)A - A_1 = \begin{pmatrix} 0&0\\0&0\\1&-1\end{pmatrix}. Both errors are 2=σ2\sqrt2 = \sigma_2, as the theorem says (only one singular value is discarded, so the two norms agree).

    Polar. Q=u1v1⊤+u2v2⊤=(1/21/21/21/21/2−1/2)Q = u_1v_1^\top + u_2v_2^\top = \begin{pmatrix} 1/2 & 1/2 \\ 1/2 & 1/2 \\ 1/\sqrt2 & -1/\sqrt2\end{pmatrix} (orthonormal columns) and P=2 v1v1⊤+2 v2v2⊤=(1+121−121−121+12)P = 2\,v_1v_1^\top + \sqrt2\, v_2v_2^\top = \begin{pmatrix} 1 + \tfrac{1}{\sqrt2} & 1 - \tfrac{1}{\sqrt2} \\ 1 - \tfrac{1}{\sqrt2} & 1 + \tfrac{1}{\sqrt2}\end{pmatrix}. Check: P2=(3113)=A⊤AP^2 = \begin{pmatrix}3&1\\1&3\end{pmatrix} = A^\top A and QP=AQP = A.

    Pseudoinverse and least squares. A+=12v1u1⊤+12v2u2⊤=(1/41/41/21/41/4−1/2)A^+ = \tfrac12 v_1u_1^\top + \tfrac{1}{\sqrt2} v_2u_2^\top = \begin{pmatrix} 1/4 & 1/4 & 1/2 \\ 1/4 & 1/4 & -1/2\end{pmatrix}, the same as (A⊤A)−1A⊤(A^\top A)^{-1}A^\top (full column rank). For b=(2,4,1)b = (2, 4, 1): x+=A+b=(2,1)x^+ = A^+b = (2, 1), Ax+=(3,3,1)Ax^+ = (3, 3, 1), and the residual b−Ax+=(−1,1,0)b - Ax^+ = (-1, 1, 0) is a multiple of u3u_3, so it is orthogonal to C(A)C(A). ✓

    Minimum norm. Now solve with the rank-1 matrix A1A_1. Its pseudoinverse is A1+=12v1u1⊤=14(110110)A_1^+ = \tfrac12 v_1u_1^\top = \tfrac14\begin{pmatrix} 1&1&0\\1&1&0\end{pmatrix}, and A1+b=(3/2,3/2)A_1^+ b = (3/2, 3/2). Since A1x=(x1+x2)(1,1,0)A_1x = (x_1 + x_2)(1, 1, 0) and the best fit of (2,4)(2, 4) by one number is 33, every xx with x1+x2=3x_1 + x_2 = 3 is a least-squares solution. The pseudoinverse picks the one in the row space span⁡{(1,1)}\operatorname{span}\{(1,1)\}, which has the smallest norm.

    5. In code #

    The libraries check the hand computations. The notebook builds each of these from the SVD (and the SVD itself from eigh).

    import numpy as np
    from scipy.linalg import polar
    
    A = np.array([[1., 1.], [1., 1.], [1., -1.]])
    U, s, Vt = np.linalg.svd(A)                # full SVD: U (3, 3), s (2,), Vt (2, 2)
    print(s)                                   # [2.         1.41421356]
    print(np.linalg.svd(A, full_matrices=False).U.shape)    # thin U: (3, 2)
    
    A1 = np.array([[1., 1.], [1., 1.], [0., 0.]])           # rank-1 answer of section 4
    print(np.linalg.norm(A - A1, 2), np.linalg.norm(A - A1, "fro"))   # both 1.414...
    
    Q, P = polar(A)                            # A = Q P
    print(Q.round(4))                          # [[0.5 0.5] [0.5 0.5] [0.7071 -0.7071]]
    print(P.round(4))                          # [[1.7071 0.2929] [0.2929 1.7071]]
    
    b = np.array([2., 4., 1.])
    print(np.linalg.pinv(A) @ b)               # [2. 1.]
    print(np.linalg.pinv(A1) @ b)              # [1.5 1.5]: the minimum-norm solution
    print(np.linalg.lstsq(A1, b, rcond=None)[0])   # the same
    
    print(np.linalg.svd(np.array([[1., 1.], [0., 1.]]), compute_uv=False))  # [1.618 0.618]
    
    # Why not the SVD from eig(AᵀA): a 50 x 2 matrix with σ = 1 and 1e-12
    rng = np.random.default_rng(0)
    Qa, _ = np.linalg.qr(rng.standard_normal((50, 2)))
    Qb, _ = np.linalg.qr(rng.standard_normal((2, 2)))
    B = Qa @ np.diag([1.0, 1e-12]) @ Qb.T
    print(np.linalg.svd(B, compute_uv=False)[1])           # 1.0000084e-12: correct to about ε·σ₁
    print(np.sqrt(abs(np.linalg.eigvalsh(B.T @ B)[0])))    # 1.077e-09: rounding noise
    

    np.linalg.svd never forms A⊤AA^\top A. It calls LAPACK's gesdd: Householder bidiagonalization of AA, then a divide-and-conquer SVD of the bidiagonal matrix (lesson 14). The QR-iteration driver is gesvd; SciPy offers it as scipy.linalg.svd(..., lapack_driver="gesvd").

    6. Where it shows up #

    • Low-rank compression with an error budget. A weight matrix W∈Rm×nW \in \mathbb{R}^{m\times n} stored as UkΣkVk⊤U_k\Sigma_k V_k^\top needs k(m+n+1)k(m + n + 1) numbers instead of mnmn, with Frobenius error exactly ∑i>kσi2\sqrt{\sum_{i>k}\sigma_i^2}, and no matrix of rank ≤k\le k does better. LoRA (lesson 15) also restricts an update to rank ≤r\le r.
    • POD and EOFs in science. Stack snapshots of a flow or a climate field as columns. The left singular vectors are the spatial modes, σi2\sigma_i^2 is the energy of mode ii, and truncation gives the basis of a reduced-order model or a surrogate. PCA (lesson 09) is the same on centered data.
    • Least squares and inverse problems. np.linalg.lstsq uses the SVD and returns the minimum-norm solution for collinear features. Truncated SVD and ridge damp the small σi\sigma_i that amplify noise in ill-posed problems (deblurring, tomography).
    • Orthogonalization. Orthogonal Procrustes (aligning molecules, point clouds or embeddings): the orthogonal WW that minimizes ∥WX−Y∥F\|WX - Y\|_F is UV⊤UV^\top from the SVD of YX⊤YX^\top. The Muon optimizer (2024) replaces each momentum matrix by an approximation of its polar factor UV⊤UV^\top, computed with a Newton–Schulz iteration.
    • Spectral norm. σ1(W)\sigma_1(W) is the Lipschitz constant of x↦Wxx \mapsto Wx. Spectral normalization divides WW by an estimate of σ1\sigma_1 from power iteration.

    7. Common mistakes #

    • Getting UU and VV from two separate eigendecompositions. The signs (or the bases of repeated eigenspaces) do not match. Use ui=Avi/σiu_i = Av_i/\sigma_i.
    • Treating singular values as $|eigenvalueseigenvalues|$. They agree only for normal matrices (A⊤A=AA⊤A^\top A = AA^\top, for example symmetric or orthogonal). The shear has eigenvalues 1,11, 1 and singular values 1.618,0.6181.618, 0.618.
    • "UU and VV are rotations." They are orthogonal. A reflection is allowed, and it is forced when det⁡A<0\det A < 0.
    • Shapes. The full UU is m×mm\times m, the thin one m×pm \times p, the compact one m×rm \times r. NumPy returns V⊤V^\top (Vt), not VV.
    • The wrong Eckart–Young error. The spectral error is the single number σk+1\sigma_{k+1}; the Frobenius error is the root of the sum of all discarded σi2\sigma_i^2.
    • Inverting tiny singular values. 1/σ1/\sigma for σ≈10−14\sigma \approx 10^{-14} multiplies noise by 101410^{14}. Truncate with rcond.
    • Writing A+=(A⊤A)−1A⊤A^+ = (A^\top A)^{-1}A^\top for every AA. It needs full column rank, and even then it is numerically worse than the SVD.

    8. Check yourself #

    1. A symmetric matrix has eigenvalues 33 and −2-2. What are its singular values, and how are uiu_i and viv_i related?
      Answer33 and 22. u1=v1u_1 = v_1 for λ=3\lambda = 3 and u2=−v2u_2 = -v_2 for λ=−2\lambda = -2: σi=∣λi∣\sigma_i = |\lambda_i|, and the sign goes into UU.
    2. AA is 1000×5001000 \times 500 with rank 5050. Give the shapes of the factors of the full SVD and of the rank-rr SVD, and the storage of the rank-50 form.
      AnswerFull: UU 1000×10001000\times1000, Σ\Sigma 1000×5001000\times500, VV 500×500500\times500. Rank-rr: 1000×501000\times50, 50×5050\times50, 500×50500\times50. Storage: 50(1000+500+1)=75,05050(1000 + 500 + 1) = 75{,}050 numbers instead of 500,000500{,}000.
    3. The singular values are 6,3,2,16, 3, 2, 1. What is the smallest kk with a relative Frobenius error of at most 20%? What is the spectral error then?
      Answer∥A∥F2=50\|A\|_F^2 = 50. k=2k = 2: 5/50≈0.316\sqrt{5/50} \approx 0.316, too big. k=3k = 3: 1/50≈0.141\sqrt{1/50} \approx 0.141. So k=3k = 3, with spectral error σ4=1\sigma_4 = 1 (relative 1/61/6).
    4. Compute the pseudoinverse of a nonzero column vector a∈Rma \in \mathbb{R}^m and interpret a+ba^+ b.
      AnswerThe SVD is a=(a/∥a∥) ∥a∥ (1)a = (a/\|a\|)\,\|a\|\,(1), so a+=a⊤/∥a∥2a^+ = a^\top/\|a\|^2. a+b=a⊤b/∥a∥2a^+b = a^\top b/\|a\|^2 is the coefficient of the projection of bb onto the line through aa.
    5. Find the polar decomposition of A=(0−210)A = \begin{pmatrix}0 & -2\\ 1 & 0\end{pmatrix}. Is QQ a rotation?
      AnswerA⊤A=diag⁡(1,4)A^\top A = \operatorname{diag}(1, 4), so P=diag⁡(1,2)P = \operatorname{diag}(1, 2) and Q=AP−1=(0−110)Q = AP^{-1} = \begin{pmatrix}0&-1\\1&0\end{pmatrix}, a rotation by 90°90° (consistent with det⁡A=2>0\det A = 2 > 0).
    6. Why does the spectral-norm proof of Eckart–Young use k+1k+1 right singular vectors, not kk?
      AnswerTo force an intersection with N(B)N(B): dim⁡N(B)≥n−k\dim N(B) \ge n - k and (n−k)+(k+1)>n(n - k) + (k + 1) > n. On span⁡(v1,…,vk+1)\operatorname{span}(v_1, \dots, v_{k+1}), AA stretches every unit vector by at least σk+1\sigma_{k+1}.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. State the SVD of an m×nm \times n matrix AA.

    2. How are the singular values of AA related to A⊤AA^\top A?

    3. You know viv_i and σi>0\sigma_i > 0. How do you get uiu_i, and why not from a separate eigendecomposition of AA⊤AA^\top?

    4. Why does every 2×22 \times 2 matrix have an orthonormal pair v1,v2v_1, v_2 with Av1⊥Av2Av_1 \perp Av_2?

    5. Eckart–Young in the spectral norm: what is the smallest ∥A−B∥2\|A - B\|_2 over rank⁡B≤k\operatorname{rank} B \le k?

    6. Eckart–Young in the Frobenius norm: what is the smallest ∥A−B∥F\|A - B\|_F over rank⁡B≤k\operatorname{rank} B \le k?

    7. AA has singular values 4,2,14, 2, 1. What are the best rank-1 errors in the spectral and the Frobenius norm?

    8. How do you get the polar decomposition A=QPA = QP from the SVD?

    9. Write the pseudoinverse with the SVD. What is special about A+bA^+b?

    10. Why should you not compute the SVD from eig(AᵀA) in floating point?

    Open the review →

    9. Interview angle #

    • "Explain the SVD in one sentence." — Every matrix maps some orthonormal basis viv_i to orthogonal vectors σiui\sigma_i u_i: rotate or reflect, stretch, rotate or reflect.
    • "How do singular values relate to eigenvalues?" — σi2\sigma_i^2 are the eigenvalues of A⊤AA^\top A. For symmetric AA, σi=∣λi∣\sigma_i = |\lambda_i|. For square AA in general, max⁡i∣λi∣≤σ1\max_i|\lambda_i| \le \sigma_1 and ∏i∣λi∣=∏iσi=∣det⁡A∣\prod_i|\lambda_i| = \prod_i \sigma_i = |\det A|.
    • "How would you compress a layer to a given error?" — Compute the SVD, choose the smallest kk with ∑i>kσi2\sqrt{\sum_{i>k}\sigma_i^2} within the budget, and store k(m+n+1)k(m+n+1) numbers. Eckart–Young says this is optimal for that rank.
    • "Why not compute the SVD with eig(AᵀA)?" — It squares the condition number, so small singular values drown in rounding error.

    10. Practice #

    The notebook for this lesson runs in Colab. It has four exercises, each with a check cell and a folded solution:

    1. svd_via_eig: build the thin SVD from np.linalg.eigh(A.T @ A) and ui=Avi/σiu_i = Av_i/\sigma_i, the existence proof as code; see where it fails for a tiny singular value.
    2. truncated_svd and eckart_young_error on a generated image: compare the measured errors with σk+1\sigma_{k+1} and ∑i>kσi2\sqrt{\sum_{i>k}\sigma_i^2}, and find the smallest kk for a 5% budget.
    3. polar: Q=UV⊤Q = UV^\top, P=VΣV⊤P = V\Sigma V^\top; check that QQ is the nearest orthogonal matrix and use it to undo a rotation of a point cloud (orthogonal Procrustes).
    4. pinv: the pseudoinverse with an rcond cut-off; check the minimum-norm property on a rank-deficient least-squares problem.

    11. Watch and read #

    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Open on YouTube · the player loads from youtube-nocookie.com when you press play