---
title: The singular value decomposition
heading: Every matrix is a rotation, a stretch and a rotation
eyebrow: A7 · Lesson 44 · The SVD
xp: 50
---

::: hook
A Wi-Fi router with several antennas talks to a laptop with several antennas, and every transmit antenna reaches every receive antenna through its own path: the channel is a matrix $H$. Mixed up like that, the streams interfere. But every matrix, square or not, can be written as a rotation, then a stretch along perpendicular axes, then another rotation. Precode with the first rotation and decode with the last, and the tangled channel becomes a set of separate, non-interfering channels, one per **singular value**. That decomposition, the SVD, also compresses images, removes noise, and measures how close a matrix is to singular.
:::

::: play
- The curve is the unit circle after the matrix $A = \begin{bmatrix}a & b\\c & d\end{bmatrix}$: always an ellipse.
- The arrow is the ellipse's long semi-axis, of length $\sigma_1$. The short semi-axis has length $\sigma_2$.
- Make $A$ nearly singular. What happens to $\sigma_2$? Compare $\sigma_1\sigma_2$ with $|\det A|$.
:::
:::

```sim plot
view: complex
range: 5
slider: a = -3..3 step 0.1 = 3
slider: b = -3..3 step 0.1 = 0
slider: c = -3..3 step 0.1 = 1.5
slider: d = -3..3 step 0.1 = 2
curve: (a*cos(u) + b*sin(u)) + j*(c*cos(u) + d*sin(u)) | over: u = 0..2*pi | label: A × unit circle
arrow: sqrt(((a^2 + b^2 + c^2 + d^2) + sqrt((a^2 + b^2 + c^2 + d^2)^2 - 4*(a*d - b*c)^2))/2)*(1∠(0.5*arg((a^2 + b^2 - c^2 - d^2) + j*2*(a*c + b*d))*180/pi)) | color: ch2 | label: σ₁u₁
readout: σ₁ = sqrt(((a^2 + b^2 + c^2 + d^2) + sqrt((a^2 + b^2 + c^2 + d^2)^2 - 4*(a*d - b*c)^2))/2) | digits: 4
readout: σ₂ = sqrt(((a^2 + b^2 + c^2 + d^2) - sqrt((a^2 + b^2 + c^2 + d^2)^2 - 4*(a*d - b*c)^2))/2) | digits: 4
readout: |det A| = abs(a*d - b*c) | digits: 4
```

::: predict
## A 2 × 2 matrix has singular values 6 and 0.01. What does it do to the unit circle?

- [ ] Rotates it
- [x] Squashes it into a long, very thin ellipse, almost a line segment
- [ ] Makes it a circle of radius 6
- [ ] Turns it inside out

::: reveal
It makes an ellipse with semi-axes 6 and 0.01: nearly a line. The matrix is close to singular, and the tiny $\sigma_2$ says how close. Solving systems with it amplifies errors by $\sigma_1/\sigma_2 = 600$, the **condition number** of lesson 52.
:::
:::

::: nameit
## The decomposition

Every $m \times n$ matrix $A$ of rank $r$ can be written

$$A = U\Sigma V^T = \sigma_1\mathbf{u}_1\mathbf{v}_1^T + \cdots + \sigma_r\mathbf{u}_r\mathbf{v}_r^T$$

- $V$ ($n \times n$) and $U$ ($m \times m$) are orthogonal: their columns are orthonormal.
- $\Sigma$ is $m \times n$ with the **singular values** $\sigma_1 \ge \sigma_2 \ge \cdots \ge \sigma_r > 0$ on its diagonal.
- $A\mathbf{v}_i = \sigma_i\mathbf{u}_i$: the right singular vectors $\mathbf{v}_i$ go to the left singular vectors $\mathbf{u}_i$, stretched by $\sigma_i$.

The $\sigma_i^2$ are the eigenvalues of $A^TA$ (and of $AA^T$), and the $\mathbf{v}_i$ are its eigenvectors. Unlike eigenvalues, singular values exist for every matrix, are real and never negative.

## Geometry

$V^T$ rotates, $\Sigma$ stretches along the axes, $U$ rotates: the unit sphere becomes an ellipsoid with semi-axes $\sigma_i$ along $\mathbf{u}_i$. For a square matrix, $|\det A| = \sigma_1\sigma_2\cdots\sigma_n$.

## Low-rank approximation

Keeping the first $k$ terms gives $A_k$, the **best** rank-$k$ approximation to $A$ (Eckart-Young). Its error, measured as the root of the sum of squared entries (the Frobenius norm), is

$$\|A - A_k\|_F = \sqrt{\sigma_{k+1}^2 + \cdots + \sigma_r^2}$$

A photo whose singular values fall quickly can be stored with a few terms: $k(m + n + 1)$ numbers instead of $mn$.

## MIMO channels

With $H = U\Sigma V^T$, transmit $V\mathbf{s}$ and multiply the received signal by $U^T$: you get $\Sigma\mathbf{s}$ plus noise, separate channels with gains $\sigma_i$. Weak $\sigma_i$ get little power; strong ones carry most of the data. S8 builds on this.
:::

::: example
## Find the singular values of $A = \begin{bmatrix}3 & 0\\4 & 5\end{bmatrix}$.

::: solution
1. **Form $A^TA$.**
   $$A^TA = \begin{bmatrix}25 & 20\\20 & 25\end{bmatrix}$$
2. **Its eigenvalues.** They are $25 \pm 20$: 45 and 5.
3. **Singular values.**
   $$\begin{aligned} \sigma_1 &= \sqrt{45} \\ &= 6.708 \\ \sigma_2 &= \sqrt 5 \\ &= 2.236 \end{aligned}$$
4. **Check.**
   $$\begin{aligned} \sigma_1\sigma_2 &= 15 \\ &= |\det A| \end{aligned}$$

**Answer:** $\sigma_1 = 6.708$ and $\sigma_2 = 2.236$.
:::
:::

::: example
## A 512 × 512 image is stored as its rank-20 approximation. How much smaller is it, and when is that a good idea?

::: solution
1. **Full storage.**
   $$512^2 = 262\,144\ \text{numbers}$$
2. **Rank-20 storage.**
   $$20 \times (512 + 512 + 1) = 20\,500$$
   about 13 times smaller.
3. **When it works.** It's good when $\sigma_{21}, \sigma_{22}, \ldots$ are small compared with the first ones, so the discarded part, of size $\sqrt{\sigma_{21}^2 + \cdots}$, is a small fraction of the whole.

**Answer:** about 13 times smaller (20 500 numbers against 262 144), and a good idea when the singular values after the 20th are small.
:::
:::

::: example
## Find the singular values of $A = \mathbf{u}\mathbf{v}^T$ with $\mathbf{u} = (1, 2)$ and $\mathbf{v} = (3, 4)$.

::: solution
1. **Rank.** Every column of $A$ is a multiple of $\mathbf{u}$, so its rank is 1 and it has one nonzero singular value.
2. **SVD form.** Writing it this way puts it in SVD form directly:
   $$A = |\mathbf{u}||\mathbf{v}|\,\frac{\mathbf{u}}{|\mathbf{u}|}\frac{\mathbf{v}^T}{|\mathbf{v}|}$$
3. **Read off $\sigma_1$.**
   $$\begin{aligned} \sigma_1 &= |\mathbf{u}||\mathbf{v}| \\ &= \sqrt5 \cdot 5 \\ &= 11.18 \end{aligned}$$
   and $\sigma_2 = 0$.
4. **The bigger picture.** A matrix built from one outer product is one rank-one piece of an SVD. Any matrix is a sum of such pieces, largest first.

**Answer:** $\sigma_1 = 11.18$ and $\sigma_2 = 0$.
:::
:::

::: check
given: p = 2 | 3; q = 5 | 7
answer: q

## What is the larger singular value of $\begin{bmatrix}\given{p} & 0\\0 & -\given{q}\end{bmatrix}$?

::: right
Right. For a diagonal matrix the singular values are the sizes of the diagonal entries: {answer}.
:::

::: misses
- [-q] Singular values are never negative.
- [any] Not yet. Take the sizes of the diagonal entries.
:::
:::

::: check
given: s1 = 4 | 10; det = -6 | -8
answer: abs(det)/s1
tolerance: 0.5%

## A 2 × 2 matrix has $\det A$ = {det} and largest singular value {s1}. What is $\sigma_2$?

::: right
Right. $\sigma_1\sigma_2 = |\det A|$, so $\sigma_2$ = {answer}.
:::

::: misses
- [det/s1] Singular values are never negative.
- [any] Not yet. $\sigma_1\sigma_2 = |\det A|$.
:::
:::

::: check
given: a = 3 | 1; b = 0 | 2; c = 4 | 2; d = 5 | 1
answer: sqrt(((a^2 + b^2 + c^2 + d^2) + sqrt((a^2 + b^2 + c^2 + d^2)^2 - 4*(a*d - b*c)^2))/2)
tolerance: 0.5%

## What is $\sigma_1$ for $\begin{bmatrix}\given{a} & \given{b}\\\given{c} & \given{d}\end{bmatrix}$?

::: right
Right. The larger eigenvalue of $A^TA$, square-rooted: {answer}.
:::

::: misses
- [any] Not yet. Form $A^TA$, find its larger eigenvalue, take the square root.
:::
:::

::: check
given: s1 = 10 | 20; s2 = 3 | 4; s3 = 1 | 2
answer: sqrt(s2^2 + s3^2)/sqrt(s1^2 + s2^2 + s3^2)
tolerance: 0.5%

## A matrix has singular values {s1}, {s2} and {s3}. What is the relative error $\frac{\|A - A_1\|_F}{\|A\|_F}$ of its best rank-1 approximation?

::: right
Right. $\frac{\sqrt{\sigma_2^2 + \sigma_3^2}}{\sqrt{\sigma_1^2 + \sigma_2^2 + \sigma_3^2}}$ = {answer}.
:::

::: misses
- [(s2 + s3)/(s1 + s2 + s3)] Errors add as squares, like perpendicular pieces.
- [s2/s1] That compares only the next singular value.
- [any] Not yet. Discarded singular values, squared and summed.
:::
:::

::: check
given: m = 256 | 1024; k = 10 | 40
answer: k*(2*m + 1)/m^2*100
tolerance: 0.5%

## An {m} × {m} image is stored as its rank-{k} approximation. What percent of the original numbers does that take?

::: right
Right. $k(2m + 1)$ numbers out of $m^2$: {answer}%.
:::

::: misses
- [k/m*100] Each term stores a $\mathbf{u}$, a $\mathbf{v}$ and a $\sigma$: $2m + 1$ numbers.
- [any] Not yet. $\frac{k(m + m + 1)}{m^2}$.
:::
:::

::: check
kind: python
given: k = 1..4 int

## `A` is a 6 × 5 NumPy array that's nearly low-rank. Using `np.linalg.svd`, set `Ak` to its best rank-{k} approximation and `rel_err` to $\|A - A_k\|_F / \|A\|_F$.

```python starter
import numpy as np
rng = np.random.default_rng(1)
A = rng.standard_normal((6, 2)) @ rng.standard_normal((2, 5)) + 0.01 * rng.standard_normal((6, 5))

Ak = np.zeros_like(A)
rel_err = 0.0
print(rel_err)
```

```python solution
import numpy as np
rng = np.random.default_rng(1)
A = rng.standard_normal((6, 2)) @ rng.standard_normal((2, 5)) + 0.01 * rng.standard_normal((6, 5))

U, s, Vt = np.linalg.svd(A, full_matrices=False)
Ak = U[:, :k] @ np.diag(s[:k]) @ Vt[:k, :]
rel_err = np.linalg.norm(A - Ak) / np.linalg.norm(A)
print(rel_err)
```

```python tests
import numpy as np
U, s, Vt = np.linalg.svd(A, full_matrices=False)
assert np.linalg.matrix_rank(Ak, tol=1e-8) == k, f"Ak should have rank {k}."
assert np.isclose(np.linalg.norm(A - Ak), np.sqrt(np.sum(s[k:]**2))), "The error should be the root of the sum of the discarded σ²: is Ak the best rank-k approximation?"
assert np.isclose(rel_err, np.sqrt(np.sum(s[k:]**2)) / np.sqrt(np.sum(s**2))), "rel_err should be ‖A − Ak‖ / ‖A‖ in the Frobenius norm (np.linalg.norm's default)."
```

::: right
Right. With $k = 2$ the error is tiny, because $A$ was built as rank 2 plus a little noise; the SVD separates the structure from the noise.
:::
:::

::: check
kind: parts
given: a = 1 | 2; b = 3 | 4; c = 2 | 5

## Singular values.

::: part
answer: sqrt(2*(a^2 + b^2))
tolerance: 0.5%

### What is $\sigma_1$ of $\begin{bmatrix}\given{a} & \given{a}\\\given{b} & \given{b}\end{bmatrix}$?

::: misses
- [sqrt(a^2 + b^2)] It's $\mathbf{u}\mathbf{v}^T$ with $\mathbf{v} = (1, 1)$, whose length is $\sqrt2$.
- [any] Rank one: $\sigma_1 = |\mathbf{u}||\mathbf{v}|$.
:::
:::

::: part
answer: c
tolerance: 0.5%

### What is $\sigma_1/\sigma_2$ for $\begin{bmatrix}\given{c} & 0\\0 & -1\end{bmatrix}$?

::: misses
- [-c] Singular values are never negative.
- [any] The singular values are $|c|$ and 1.
:::
:::
:::

::: check
kind: derivation
vars: x = 0.5..3, y = 0.5..3

## Find the SVD of $A = \begin{bmatrix}x & y\\0 & 0\end{bmatrix}$.

::: step
answer: x^2 + y^2

### What is the trace of $A^TA$?
:::

::: step
answer: sqrt(x^2 + y^2)

### $A$ has rank 1. What is $\sigma_1$?
:::

::: right
Right. A rank-one matrix's only singular value is the square root of the sum of its squared entries.
:::
:::

::: explain
## Why are the singular values of $A$ the square roots of the eigenvalues of $A^TA$?

::: model
From $A = U\Sigma V^T$, $A^TA = V\Sigma^TU^TU\Sigma V^T = V(\Sigma^T\Sigma)V^T$, because $U^TU = I$. That's the spectral factorization of the symmetric matrix $A^TA$: its eigenvectors are the columns of $V$ and its eigenvalues are the diagonal entries of $\Sigma^T\Sigma$, which are $\sigma_i^2$. Since $A^TA$ is positive semidefinite those eigenvalues are never negative, so their square roots are real, and they're the singular values.
:::
:::

::: practice

::: check
given: s1 = 8 | 12; s2 = 2 | 0.5
answer: s1/s2
tolerance: 0.5%

## A square matrix has singular values {s1} and {s2}. What is its condition number $\sigma_1/\sigma_2$?

::: misses
- [s2/s1] Largest over smallest.
- [any] $\sigma_{\max}/\sigma_{\min}$.
:::
:::

::: check
given: s1 = 5 | 9; s2 = 2 | 3
answer: s1*s2

## A 2 × 2 matrix has singular values {s1} and {s2}. What is $|\det A|$?

::: misses
- [any] The product of the singular values.
:::
:::

::: check
kind: choice

## What are the singular values of an orthogonal matrix $Q$?

- [x] All 1
- [ ] Its eigenvalues
- [ ] All 0
- [ ] They depend on the rotation angle

::: misses
- [2] Its eigenvalues can be complex; singular values never are.
- [any] $Q^TQ = I$.
:::
:::
:::
