Building and using the singular value decomposition. PCA is closely tied to it.
Open the interactive lessons →Singular Value Decomposition past questions
Contents
- Singular values and the Gram matrix
- Left and right singular vectors
- Assembling and rebuilding
- Reading rank, subspaces and action
1. Singular values and the Gram matrix
Find how much the matrix stretches along its best directions.
where singular values come from and what shape Σ has
The question this lesson answers. How much can a matrix stretch a vector, even when it is not square?
A matrix turns the unit circle into an ellipse. The half lengths of the ellipse are the singular values.
For a real $m\times n$ matrix $A$, the singular values are $\sigma_i=\sqrt{\lambda_i}$, where $\lambda_i$ are the eigenvalues of $A^TA$. They are real and never negative.
Why it works this way. The squared length of $Av$ is $v^T(A^TA)v$, so the stretching is governed by the eigenvalues of $A^TA$. Their square roots are the singular values.
Why this works: $A^TA$ is symmetric, and $x^TA^TAx=\lVert Ax\rVert^2\ge0$. So its eigenvalues are real and at least zero. If $v$ is a unit eigenvector with eigenvalue $\lambda$, then $\lVert Av\rVert^2=v^TA^TAv=\lambda$. So $\sigma=\lVert Av\rVert$ is exactly how long the unit vector $v$ becomes.
The two Gram matrices $A^TA$ ($n\times n$) and $AA^T$ ($m\times m$) have the same non-zero eigenvalues. So for a wide $2\times3$ matrix use the $2\times2$ matrix $AA^T$; for a tall $3\times2$ matrix use $A^TA$. The larger one only has extra zeros.
Shape of Σ. $\Sigma$ is $m\times n$, the same shape as $A$. The singular values sit on its main diagonal in decreasing order, and every other entry is 0. A $2\times3$ matrix has two singular values (one of them may be 0). It does not have three.
Two quick checks: $\sum\sigma_i^2=\sum a_{jk}^2$, and for a square matrix $\prod\sigma_i=\lvert\det A\rvert$. For a symmetric matrix, the singular values are the absolute values of the eigenvalues.
$$\kappa(A)=\frac{\sigma_{\max}}{\sigma_{\min}}$$ Condition: $A$ square and invertible, so $\sigma_{\min}>0$. It is at least 1. It is not in general the ratio of the eigenvalues of $A$, unless $A$ is symmetric.
Worked example: Wide matrix
Problem.
Find the singular values of $A=\begin{bmatrix}2&0&0\\0&1&2\end{bmatrix}$.
Idea.
$AA^T$ is the smaller Gram matrix.
- $AA^T=\begin{bmatrix}4&0\\0&5\end{bmatrix}$.
- Eigenvalues 5 and 4.
- Singular values $\sqrt5$ and $2$.
Answer.
$\sigma_1=\sqrt5$, $\sigma_2=2$.
Common mistake.
Listing them as $2,\sqrt5$, in the wrong order.
Worked example: Symmetric with a negative eigenvalue
Problem.
Find the singular values and condition number of $\begin{bmatrix}1&3\\3&1\end{bmatrix}$.
Idea.
Symmetric: take absolute values of eigenvalues.
- Eigenvalues 4 and $-2$.
- Singular values 4 and 2.
- Condition number $4/2=2$.
Answer.
$4, 2$; $\kappa=2$.
Common mistake.
Reporting $-2$ as a singular value.
Worked example: Zero eigenvalues, large stretch
Problem.
Find the eigenvalues and singular values of $\begin{bmatrix}0&5\\0&0\end{bmatrix}$.
Idea.
Singular values come from $A^TA$.
- Both eigenvalues are 0.
- $A^TA=\mathrm{diag}(0,25)$.
Answer.
Eigenvalues $0,0$; singular values $5,0$.
Common mistake.
Taking singular values equal to eigenvalues.
- Pick the smaller of $A^TA$ and $AA^T$: the non-zero eigenvalues are the same.
- Check: the sum of $\sigma_i^2$ equals the sum of the squares of all entries of $A$.
- For a square matrix the product of the singular values equals $|\det A|$.
Square roots of the Gram eigenvalues.
Method for any exam question
| Step | Do this |
|---|---|
| 1 | Form the smaller of $A^TA$ and $AA^T$. |
| 2 | Find its eigenvalues. |
| 3 | Take non-negative square roots. |
| 4 | Order them from largest to smallest. |
2. Left and right singular vectors
One set of axes for inputs, one for outputs, matched pair by pair.
the two orthonormal bases and how they are linked
The question this lesson answers. Which input directions become which output directions?
Each input arrow $v_i$ is sent to one output arrow $u_i$, stretched by $\sigma_i$.
$$Av_i=\sigma_iu_i,\qquad A^Tu_i=\sigma_iv_i$$ $v_i$ are unit eigenvectors of $A^TA$; $u_i$ are unit eigenvectors of $AA^T$. They are numbered in the same decreasing order of $\sigma_i$. When $\sigma_i>0$, one vector of the pair determines the other, including its sign.
Why it works this way. Multiplying $A^TAv=\sigma^2v$ by $A$ shows that $Av$ is an eigenvector of $AA^T$ with the same value, so one side determines the other.
Method for a $2\times3$ matrix:
- Compute $AA^T$ and its eigen-pairs. Normalise to get $u_1,u_2$.
- For each $\sigma_i>0$ set $v_i=A^Tu_i/\sigma_i$. It is automatically a unit vector.
- For the missing $v_3$, find a unit vector orthogonal to $v_1,v_2$. It satisfies $Av_3=0$.
Do not compute $u_i$ and $v_i$ separately and hope the signs agree. In $A=Q_1\Sigma Q_2^T$, the matrix $Q_2^T$ lists the $v_i$ as its rows.
Testing an option. First test orthogonality: each column must have length 1 and different columns must be perpendicular. A matrix with a repeated column, or a column of length not 1, is rejected at once. Only then test the link with $A$.
The vectors are not unique. Flipping the signs of $u_i$ and $v_i$ together leaves the product unchanged. When two singular values are equal, any orthonormal basis of that plane will do, provided the other side is computed from it by the linking equation.
In three dimensions the cross product gives it at once: $$a\times b=(a_2b_3-a_3b_2,\;\;a_3b_1-a_1b_3,\;\;a_1b_2-a_2b_1).$$ Divide by its length to get a unit vector. Its negative is equally valid. This shortcut is for real three-component vectors only.
Worked example: All the vectors when $AA^T$ is not diagonal
Problem.
Find $U$, $\Sigma$ and $V$ for $A=\begin{bmatrix}3&2&2\\2&3&-2\end{bmatrix}$.
Idea.
Work on the smaller product $AA^T$ ($2\times2$). Get each $v_i$ from its $u_i$, then fill the one direction still missing.
- Multiply rows with rows: $AA^T=\begin{bmatrix}17&8\\8&17\end{bmatrix}$.
- Trace 34 and determinant $289-64=225$ give $\lambda^2-34\lambda+225=0$, so $\lambda=25,9$. Hence $\sigma_1=5$, $\sigma_2=3$.
- For 25: $-8x+8y=0$, so $u_1=\tfrac1{\sqrt2}(1,1)$. For 9: $8x+8y=0$, so $u_2=\tfrac1{\sqrt2}(1,-1)$. Divide by the length now.
- $A^Tu_1=\tfrac1{\sqrt2}(5,5,0)$. Divide by $\sigma_1=5$: $v_1=\tfrac1{\sqrt2}(1,1,0)$.
- $A^Tu_2=\tfrac1{\sqrt2}(1,-1,4)$. Divide by $\sigma_2=3$: $v_2=\tfrac1{\sqrt{18}}(1,-1,4)$. Check the length: $\sqrt{18}/\sqrt{18}=1$.
- $v_3$ has no $u$ partner, so take it perpendicular to both. $(1,1,0)\times(1,-1,4)=(4,-4,-2)$. Divide by its length 6: $v_3=\tfrac13(2,-2,-1)$.
- Check: $Av_3=\tfrac13(6-4-2,\;4-6+2)=(0,0)$.
Answer.
$U=[u_1\,u_2]$, $\Sigma=\begin{bmatrix}5&0&0\\0&3&0\end{bmatrix}$, $V=[v_1\,v_2\,v_3]$, and $A=U\Sigma V^T$.
Common mistake.
Finding $u_i$ and $v_i$ separately, so the signs may not match; or writing the $v_i$ as columns of $V^T$ instead of its rows.
Worked example: Repeated singular value
Problem.
For $M=\begin{bmatrix}1&1&0\\1&-1&0\end{bmatrix}$, may we take $V=I_3$?
Idea.
Check whether $M^TM$ is diagonal.
- $M^TM=\operatorname{diag}(2,2,0)$, already diagonal.
- So $e_1,e_2,e_3$ are right singular vectors.
- $u_i=Me_i/\sqrt2$ gives $(1,1)/\sqrt2$ and $(1,-1)/\sqrt2$.
Answer.
Yes, with $U=\tfrac1{\sqrt2}\begin{bmatrix}1&1\\1&-1\end{bmatrix}$. Choosing $U=I$ too would be wrong.
Common mistake.
Taking $U=I$ and $V=I$ together, which rebuilds Σ, not $M$.
An option for $Q_2^T$ shows the right singular vectors as rows. An option for $Q_2$ shows them as columns.
Worked example: A negative eigenvalue
Problem.
What are the singular values of $\mathrm{diag}(3,-3)$?
Idea.
Singular values are never negative.
- $A^TA=\mathrm{diag}(9,9)$.
- The square roots are 3 and 3.
Answer.
$3,3$
Common mistake.
Answering $3,-3$.
- Find one side by eigenvectors and get the other from $u_i=Av_i/\sigma_i$. This keeps the signs matched.
- In $A=Q_1\Sigma Q_2^T$ the right singular vectors are the rows of $Q_2^T$.
- To test an option, multiply $A v_i$ and see whether you get $\sigma_i u_i$.
Find one side, compute the other.
Method for any exam question
| Step | Do this |
|---|---|
| 1 | Find unit eigenvectors on one side. |
| 2 | Compute the partner by multiplying by $A$ or $A^T$. |
| 3 | Divide by $\sigma$. |
| 4 | Check the length is 1. |
3. Assembling and rebuilding
Check each matched pair; rebuild the matrix from its outputs.
putting $U$, Σ and $V^T$ together and going backwards
The question this lesson answers. How is the whole matrix rebuilt from its pieces?
Think of $A$ as an image built from transparent sheets. Each sheet $\sigma_iu_iv_i^T$ has rank 1. The sheet with the largest $\sigma_i$ shows the most; the last ones add fine detail. The analogy stops here: the sheets are matrices and are added, not stacked visually.
$$A=U\Sigma V^T=\sum_{i=1}^{r}\sigma_i\,u_iv_i^T$$ Shapes: $U$ is $m\times m$ orthogonal, Σ is $m\times n$, $V$ is $n\times n$ orthogonal, and $r$ is the number of non-zero singular values. This exists for every real matrix.
Why it works this way. The $v_i$ form a basis, and $A$ and the sum of layers agree on every one of them, so they are the same matrix.
Testing a full SVD option, cheapest first:
- Σ: are its non-zero entries the square roots of the Gram eigenvalues?
- $U,V$: orthonormal?
- Pairs: is $Av_i=\sigma_iu_i$ for each row $v_i^T$ of the right factor?
Going backwards. Suppose you know orthonormal $q_1,\dots,q_n$ and the outputs $Aq_i$. Let $Q=[q_1\dots q_n]$. Then $AQ=[Aq_1\dots Aq_n]$, and since $Q^{-1}=Q^T$, $$A=\big[Aq_1\ \cdots\ Aq_n\big]\,Q^T.$$ No square roots and no $U$ are needed.
The sum form shows $A$ as a stack of rank-one layers. Keeping only the largest layers gives the best low-rank approximation in the sum-of-squares sense (the Eckart–Young theorem, stated here without proof).
Worked example: Rank-one SVD
Problem.
Write $A=\begin{bmatrix}3&4\\0&0\end{bmatrix}$ as $\sigma_1u_1v_1^T$.
Idea.
One independent row means rank one.
- $AA^T=\operatorname{diag}(25,0)$, so $\sigma_1=5$ and $u_1=(1,0)$.
- $v_1=A^Tu_1/5=(0.6,0.8)$.
Answer.
$A=5\,u_1v_1^T$.
Common mistake.
Forgetting to divide $v_1$ by 5.
Worked example: Rebuilding from outputs
Problem.
$q_1=(1,1)/\sqrt2$, $q_2=(1,-1)/\sqrt2$, $Aq_1=(2,2)/\sqrt2$, $Aq_2=(4,0)/\sqrt2$. Find $A$.
Idea.
$A=[Aq_1\,Aq_2]\,Q^T$.
- The product of the two $1/\sqrt2$ factors is $1/2$.
- $\tfrac12\begin{bmatrix}2&4\\2&0\end{bmatrix}\begin{bmatrix}1&1\\1&-1\end{bmatrix}=\tfrac12\begin{bmatrix}6&-2\\2&2\end{bmatrix}$.
Answer.
$\begin{bmatrix}3&-1\\1&1\end{bmatrix}$.
Common mistake.
Multiplying by $Q$ on the left.
In "find $A$" options, test one product: apply each option to $q_1$ and compare with the given $Aq_1$. If two survive, test $q_2$.
The recipe
| Step | Do this | Check before moving on |
|---|---|---|
| 1 | Find the eigenvalues of the smaller of $A^TA$ and $AA^T$, then take square roots. | No singular value is negative. |
| 2 | Find unit eigenvectors on that side. | Each has length 1 and they are perpendicular. |
| 3 | Get the partner vector with $u_i=Av_i/\sigma_i$ or $v_i=A^Tu_i/\sigma_i$. | Never choose both sides separately. |
| 4 | Add the layers $\sigma_iu_iv_i^T$. | One entry of the sum matches $A$. |
Why keep the largest layers? The layers are perpendicular to one another in the sum-of-squares sense, so $\lVert A\rVert_F^2=\sigma_1^2+\sigma_2^2+\cdots$. Dropping layers loses exactly the squares of their singular values. To lose as little as possible, drop the smallest ones.
Worked example: Best rank-one approximation
Problem.
For $A=\begin{bmatrix}0&2\\1&0\end{bmatrix}$ find the best rank-one approximation and its squared error.
Idea.
Keep only the largest layer.
- $A^TA=\mathrm{diag}(1,4)$, so $\sigma_1=2$ and $\sigma_2=1$.
- The larger value belongs to $v_1=(0,1)$, and $u_1=Av_1/2=(1,0)$.
- $B=2\,u_1v_1^T=\begin{bmatrix}0&2\\0&0\end{bmatrix}$.
- $A-B$ has a single entry 1, so the squared error is $1=\sigma_2^2$.
Answer.
$B=\begin{bmatrix}0&2\\0&0\end{bmatrix}$, squared error $1$.
Common mistake.
Taking the first coordinate direction as $v_1$ just because it is listed first.
The sum of the squares of all entries of $A$ must equal the sum of the squares of your singular values.
A matrix is a stack of rank-one layers, largest first.
4. Reading rank, subspaces and action
Count the non-zero stretches; follow each input axis.
what the three factors tell you without multiplying them
The question this lesson answers. What do the three factors tell you about rank and the four subspaces?
To apply $A=U\Sigma V^T$ to a vector: first read its coordinates along the $v_i$, then stretch each coordinate by $\sigma_i$ (a zero erases it), then rebuild along the $u_i$. Rank counts the coordinates that survive.
Why it works this way. Only directions with a non-zero singular value survive. The survivors span the row space and the column space; the others span the two null spaces.
With rank $r$
| Subspace | Spanned by | Dimension |
|---|---|---|
| column space of $A$ | $u_1,\dots,u_r$ | $r$ |
| null space of $A$ | $v_{r+1},\dots,v_n$ | $n-r$ |
| row space of $A$ | $v_1,\dots,v_r$ | $r$ |
| left null space ($A^Tx=0$) | $u_{r+1},\dots,u_m$ | $m-r$ |
The rank is the number of non-zero singular values, not the size of Σ.
A span does not care about scale: if $u_1=(4,5,-2)/(3\sqrt5)$, then $(4,5,-2)$ spans the same line. So options usually drop the square roots.
Action on a vector. Write $x=\sum c_iv_i$. Then $$Ax=\sum_{i\le r}c_i\sigma_iu_i,\qquad\lVert Ax\rVert^2=\sum_{i\le r}(c_i\sigma_i)^2.$$ The second formula holds because the $u_i$ are orthonormal: there are no cross terms. Any part of $x$ along a null direction contributes nothing.
From the same formula, over unit vectors $x$, the largest value of $\lVert Ax\rVert$ is $\sigma_1$, reached at $v_1$.
Worked example: Rank and null space
Problem.
$\Sigma=\operatorname{diag}(6,2,0)$ for a $3\times3$ matrix. Find rank and nullity.
Idea.
Count non-zero entries.
- Two non-zero values: rank 2.
- Nullity $3-2=1$, spanned by $v_3$.
Answer.
2 and 1.
Common mistake.
Answering rank 3 because Σ is $3\times3$.
Worked example: Length of an image
Problem.
$\sigma=3,2,0$ and $x=v_1+2v_2+5v_3$. Find $Ax$ and $\lVert Ax\rVert^2$.
Idea.
Multiply each coordinate by its singular value.
- $Ax=3u_1+4u_2$.
- $\lVert Ax\rVert^2=9+16=25$.
Answer.
$3u_1+4u_2$; 25.
Common mistake.
Adding $5^2$ for the null direction.
The recipe
| Step | Do this | Check before moving on |
|---|---|---|
| 1 | Count the non-zero singular values. That is $r$. | $r\le\min(m,n)$. |
| 2 | Split the $v_i$: the first $r$ span the row space, the rest the null space. | These live in $\mathbb R^n$. |
| 3 | Split the $u_i$: the first $r$ span the column space, the rest the left null space. | These live in $\mathbb R^m$. |
| 4 | Compare dimensions. | $r+(n-r)=n$ and $r+(m-r)=m$. |
A memory hook. $v$ is for the input side and $u$ is for the output side. If a question asks about vectors that $A$ accepts, look at the $v_i$. If it asks about vectors that $A$ produces, look at the $u_i$.
Worked example: All four subspaces
Problem.
$A=\begin{bmatrix}1&2\\2&4\\0&0\end{bmatrix}$. Give the rank, $\sigma_1$ and a basis for each of the four subspaces.
Idea.
Every column is a multiple of one vector.
- The second column is twice the first, so $r=1$.
- $\sigma_1^2$ is the sum of all squared entries, $1+4+4+16=25$, so $\sigma_1=5$.
- Row space: $(1,2)$. Null space: $(-2,1)$.
- Column space: $(1,2,0)$. Left null space: $(-2,1,0)$ and $(0,0,1)$.
Answer.
Rank 1, $\sigma_1=5$, dimensions $1,1,1,2$.
Common mistake.
Giving only one vector for the left null space, which has dimension $m-r=2$.
A null space vector must give zero when multiplied by $A$, and must be perpendicular to every row.
$v$ for inputs, $u$ for outputs, zeros for null spaces.