TExcelTAD
Home › Linear Algebra & Matrix Theory › The Singular Value Decomposition

The Singular Value Decomposition

Every matrix, square or not, can be written as a rotation, then a stretch along the axes, then another rotation: A=UΣVTA = U \Sigma V^T. This singular value decomposition (SVD) reveals a matrix's rank, its most important directions and how much it can amplify errors, and it powers image compression, PCA, recommender systems and least squares solvers.

The statement

Theorem (SVD). Every real m×nm \times n matrix AA can be factored as

A=UΣVTU:m×m orthogonal (left singular vectors u1,…,um)Σ:m×n diagonal with σ1≥σ2≥⋯≥0 on the diagonal (singular values)V:n×n orthogonal (right singular vectors v1,…,vn)\begin{aligned} A &= U \Sigma V^T \\ U &: m \times m \text{ orthogonal (left singular vectors } \mathbf{u}_1, \ldots, \mathbf{u}_m\text{)} \\ \Sigma &: m \times n \text{ diagonal with } \sigma_1 \ge \sigma_2 \ge \cdots \ge 0 \text{ on the diagonal (singular values)} \\ V &: n \times n \text{ orthogonal (right singular vectors } \mathbf{v}_1, \ldots, \mathbf{v}_n\text{)} \end{aligned}

Equivalently Avi=σiuiA\mathbf{v}_i = \sigma_i \mathbf{u}_i: AA maps an orthonormal basis of the input space onto an orthogonal set in the output space, scaling the ii-th vector by σi\sigma_i.

Where it comes from. ATAA^T A is symmetric and positive semidefinite, so by the spectral theorem it has orthonormal eigenvectors v1,…,vn\mathbf{v}_1, \ldots, \mathbf{v}_n with eigenvalues λi≥0\lambda_i \ge 0. Put σi=λi\sigma_i = \sqrt{\lambda_i}. For σi>0\sigma_i > 0 define ui=Avi/σi\mathbf{u}_i = A\mathbf{v}_i/\sigma_i. These are orthonormal:

ui⋅uj=(Avi)T(Avj)σiσj=viT(ATA)vjσiσj=λj vi⋅vjσiσj=0 for i≠j, 1 for i=j\mathbf{u}_i \cdot \mathbf{u}_j = \frac{(A\mathbf{v}_i)^T(A\mathbf{v}_j)}{\sigma_i\sigma_j} = \frac{\mathbf{v}_i^T(A^T A)\mathbf{v}_j}{\sigma_i\sigma_j} = \frac{\lambda_j\, \mathbf{v}_i \cdot \mathbf{v}_j}{\sigma_i\sigma_j} = 0 \text{ for } i \ne j,\ 1 \text{ for } i = j

Complete the ui\mathbf{u}_i to an orthonormal basis of Rm\mathbb{R}^m (with Gram-Schmidt) and you have UU.

The geometry: circles become ellipses

Read A=UΣVTA = U \Sigma V^T right to left on the unit circle: VTV^T rotates (or reflects) it, still a circle; Σ\Sigma stretches it into an ellipse with semi-axes σ1\sigma_1, σ2\sigma_2 along the coordinate axes; UU rotates the ellipse into place. So the image of the unit sphere under AA is an ellipsoid whose semi-axes are σiui\sigma_i \mathbf{u}_i, and the vi\mathbf{v}_i are the input directions that land on those axes.

python · try it yourself
import numpy as np

A = np.array([[3.0, 0.0],
              [4.0, 5.0]])
U, s, Vt = np.linalg.svd(A)
print("singular values:", np.round(s, 4))
print("sqrt of eigenvalues of A^T A:", np.round(np.sqrt(np.linalg.eigvalsh(A.T @ A))[::-1], 4))
print("U Σ V^T = A:", np.allclose(U @ np.diag(s) @ Vt, A))
for i in range(2):
    print(f"A v{i+1} = {np.round(A @ Vt[i], 4)} = σ{i+1} u{i+1} = {np.round(s[i] * U[:, i], 4)}")

t = np.linspace(0, 2 * np.pi, 2000)
image = A @ np.vstack([np.cos(t), np.sin(t)])
r = np.linalg.norm(image, axis=0)
print("longest / shortest image of a unit vector:", round(float(r.max()), 4), round(float(r.min()), 4))
print("|det A| = product of singular values:", abs(np.linalg.det(A)).round(4), np.prod(s).round(4))
singular values: [6.7082 2.2361]
sqrt of eigenvalues of A^T A: [6.7082 2.2361]
U Σ V^T = A: True
A v1 = [-2.1213 -6.364 ] = σ1 u1 = [-2.1213 -6.364 ]
A v2 = [-2.1213  0.7071] = σ2 u2 = [-2.1213  0.7071]
longest / shortest image of a unit vector: 6.7082 2.2361
|det A| = product of singular values: 15.0 15.0

σ1=45\sigma_1 = \sqrt{45} and σ2=5\sigma_2 = \sqrt{5}: no unit vector is stretched more than 6.7082 or less than 2.2361. In general σ1=max⁡∥Ax∥\sigma_1 = \max \lVert A\mathbf{x} \rVert over unit x\mathbf{x}, the operator norm ∥A∥2\lVert A \rVert_2.

What the SVD tells you

Quantity From the SVD
rank⁡A\operatorname{rank} A number of nonzero σi\sigma_i (numerically: σi\sigma_i above a tolerance)
column space, null space u1…ur\mathbf{u}_1 \ldots \mathbf{u}_r span col⁡(A)\operatorname{col}(A); vr+1…vn\mathbf{v}_{r+1} \ldots \mathbf{v}_n span null⁡(A)\operatorname{null}(A) (Column, Row and Null Space)
∥A∥2\lVert A \rVert_2 (largest stretch) σ1\sigma_1
Frobenius norm σ12+⋯+σr2\sqrt{\sigma_1^2 + \cdots + \sigma_r^2}
condition number σ1/σn\sigma_1 / \sigma_n (Numerical Linear Algebra)
pseudoinverse A+A^+ VΣ+UTV \Sigma^+ U^T, with Σ+\Sigma^+ inverting the nonzero σi\sigma_i
best rank-kk approximation keep the kk largest terms (Low-Rank Approximation)

The pseudoinverse gives the minimum-norm least squares solution x=A+b\mathbf{x} = A^+\mathbf{b} even when AA is rank-deficient; that is what np.linalg.lstsq and np.linalg.pinv compute.

The outer-product form and image compression

Multiplying out UΣVTU \Sigma V^T gives AA as a sum of rank-one matrices, ordered by importance:

A=σ1u1v1T+σ2u2v2T+⋯+σrurvrTA = \sigma_1 \mathbf{u}_1 \mathbf{v}_1^T + \sigma_2 \mathbf{u}_2 \mathbf{v}_2^T + \cdots + \sigma_r \mathbf{u}_r \mathbf{v}_r^T

Keeping only the first kk terms gives AkA_k, which needs k(m+n+1)k(m + n + 1) numbers instead of mnmn. A grayscale image is a matrix of brightness values, and natural images have rapidly decaying singular values, so a small kk already looks right. We build a synthetic 120×160120 \times 160 "photo" (smooth shapes plus noise) so the example needs no files:

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(5)
y, x = np.mgrid[0:120, 0:160] / 40.0
img = (np.sin(2 * x) * np.cos(1.5 * y) + ((x - 2.2)**2 + (y - 1.4)**2 < 0.5)
       + 0.5 * (y > 2.2) + 0.08 * rng.normal(size=x.shape))

U, s, Vt = np.linalg.svd(img, full_matrices=False)
m, n = img.shape
print("first singular values:", np.round(s[:6], 2))
fig, axes = plt.subplots(1, 4, figsize=(10, 2.6))
axes[0].imshow(img, cmap="gray"); axes[0].set_title("original")
for ax, k in zip(axes[1:], (1, 5, 20)):
    Ak = U[:, :k] * s[:k] @ Vt[:k]
    err = np.linalg.norm(img - Ak) / np.linalg.norm(img)
    ratio = k * (m + n + 1) / (m * n)
    print(f"k={k:2d}: storage {ratio:6.1%} of original, relative error {err:.3f}")
    ax.imshow(Ak, cmap="gray"); ax.set_title(f"rank {k}")
for ax in axes: ax.axis("off")
first singular values: [92.5  31.93 12.64 11.55  6.52  4.88]
k= 1: storage   1.5% of original, relative error 0.392
k= 5: storage   7.3% of original, relative error 0.141
k=20: storage  29.3% of original, relative error 0.093

At rank 20 the picture is visually clean; most of the remaining error is the noise we added, which is spread over all the small singular values. (Real codecs such as JPEG use fixed cosine bases instead, because they are faster and work block by block, but the "keep the big coefficients" idea is the same.)

Tip: For a big matrix where you only need the top kk singular triplets, use scipy.sparse.linalg.svds or randomized SVD (sklearn.utils.extmath.randomized_svd) instead of a full np.linalg.svd.

In software

  • PCA in scikit-learn is an SVD of the centred data (PCA).
  • Recommenders. A user×item\text{user} \times \text{item} ratings matrix is approximated by a low-rank SVD; the mini project builds one.
  • NLP. Latent semantic analysis is a truncated SVD of a term-document matrix, an ancestor of modern embeddings.
  • Robotics and graphics. The best rotation aligning two point clouds (Kabsch / Procrustes, used in ICP and motion capture) is UVTUV^T from the SVD of a 3×33 \times 3 cross-covariance matrix.
  • Deep learning. The spectrum of weight matrices is monitored for stability, and LoRA relies on updates having low rank (Low-Rank Approximation).

Go further: Principal Component Analysis computes PCA through the SVD and uses the components on real data.

Exercises

Exercise 1: Singular values of a diagonal matrix

What are the singular values of diag⁡(−3,2)\operatorname{diag}(-3, 2)?

Answer

3 and 2.

Solution

ATA=diag⁡(9,4)A^T A = \operatorname{diag}(9, 4), whose eigenvalues' square roots are 3 and 2. The sign goes into UU: diag⁡(−3,2)=diag⁡(−1,1)⋅diag⁡(3,2)⋅I\operatorname{diag}(-3, 2) = \operatorname{diag}(-1, 1) \cdot \operatorname{diag}(3, 2) \cdot I.

Exercise 2: By hand

Find the singular values of A=[110011]A = \begin{bmatrix} 1 & 1 \\ 0 & 0 \\ 1 & 1 \end{bmatrix}.

Answer

σ1=2\sigma_1 = 2, σ2=0\sigma_2 = 0 (rank 1).

Solution

ATA=[2222]A^T A = \begin{bmatrix} 2 & 2 \\ 2 & 2 \end{bmatrix}, eigenvalues 4 and 0. AA is rank one: A=2⋅u1v1TA = 2 \cdot \mathbf{u}_1 \mathbf{v}_1^T with u1=(1,0,1)/2\mathbf{u}_1 = (1, 0, 1)/\sqrt{2} and v1=(1,1)/2\mathbf{v}_1 = (1, 1)/\sqrt{2}.

Exercise 3: Storage

A 1000×8001000 \times 800 image is stored as its rank-50 SVD. What fraction of the original numbers is needed?

Answer

90 050/800 000≈11.3%90\,050 / 800\,000 \approx 11.3\%.

Solution

k(m+n+1)=50⋅(1000+800+1)=90 050k(m + n + 1) = 50 \cdot (1000 + 800 + 1) = 90\,050.

Exercise 4: Rank from the SVD (code)

Use the SVD to find the rank of [123456789]\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} and a basis vector of its null space.

Answer

Rank 2; the null space is spanned by (1,−2,1)(1, -2, 1) (up to scaling and sign).

Solution

The third singular value is 0 up to rounding, and the last row of VTV^T spans the null space.

python · try it yourself
import numpy as np
A = np.arange(1, 10, dtype=float).reshape(3, 3)
U, s, Vt = np.linalg.svd(A)
print(np.round(s, 6) + 0, "rank:", int(np.sum(s > 1e-10 * s[0])))
v = Vt[-1] / Vt[-1][0]
print(np.round(v, 6) + 0, "A v ≈ 0:", np.allclose(A @ v, 0))
[16.848103  1.06837   0.      ] rank: 2
[ 1. -2.  1.] A v ≈ 0: True

Test yourself

The singular values of AA are…

  1. the eigenvalues of AA
  2. the square roots of the eigenvalues of ATAA^T A
  3. the diagonal entries of AA
  4. the eigenvalues of A+ATA + A^T

Under AA, the unit circle is mapped to…

  1. another circle
  2. an ellipse with semi-axes σ1\sigma_1 and σ2\sigma_2
  3. a square
  4. a line always

Which matrices have an SVD?

  1. Only symmetric ones
  2. Only square invertible ones
  3. Every real matrix
  4. Only diagonalizable ones