TExcelTAD
Home › Numerical Methods › Computing the SVD

Computing the SVD

The singular value decomposition A=UΣVTA = U\Sigma V^T exists for every matrix, and it answers the questions other factorizations struggle with: the numerical rank, the 2-norm and condition number, the best low-rank approximation, and least squares for rank-deficient problems. This lesson looks at how it is computed and used numerically. The theory is in Singular Value Decomposition.

The decomposition

For A∈Rm×nA \in \mathbb{R}^{m \times n} (m≥nm \ge n): UU is m×mm \times m orthogonal, VV is n×nn \times n orthogonal, Σ\Sigma is m×nm \times n diagonal with σ1≥σ2≥⋯≥σn≥0\sigma_1 \ge \sigma_2 \ge \cdots \ge \sigma_n \ge 0. Key facts:

  • ∥A∥2=σ1\lVert A\rVert_2 = \sigma_1, κ2(A)=σ1σn\kappa_2(A) = \frac{\sigma_1}{\sigma_n}.
  • The rank is the number of nonzero singular values.
  • σi2\sigma_i^2 are the eigenvalues of ATAA^TA, but computing them that way squares the condition number.

How it is computed

Like the QR algorithm, the SVD is computed in two stages:

  1. Bidiagonalization: Householder reflections from the left and the right reduce AA to an upper bidiagonal matrix B=U1TAV1B = U_1^TAV_1, about 4mn2−43n34mn^2 - \frac{4}{3}n^3 flops.
  2. Singular values of BB by an implicitly shifted QR iteration on BTBB^TB that never forms BTBB^TB (Golub-Kahan), or by divide and conquer.

Forming ATAA^TA explicitly loses the small singular values:

python · try it yourself
import numpy as np

rng = np.random.default_rng(81)
U, _ = np.linalg.qr(rng.normal(size=(50, 6)))
V, _ = np.linalg.qr(rng.normal(size=(6, 6)))
sig = np.array([1.0, 1e-2, 1e-4, 1e-6, 1e-8, 1e-10])
A = U @ np.diag(sig) @ V.T

via_svd = np.linalg.svd(A, compute_uv=False)
via_eig = np.sqrt(np.abs(np.linalg.eigvalsh(A.T @ A)))[::-1]
for s, a, b in zip(sig, via_svd, via_eig):
    print(f"true {s:.0e}: svd {a:.6e}, sqrt(eig(A^T A)) {b:.6e}")
true 1e+00: svd 1.000000e+00, sqrt(eig(A^T A)) 1.000000e+00
true 1e-02: svd 1.000000e-02, sqrt(eig(A^T A)) 1.000000e-02
true 1e-04: svd 1.000000e-04, sqrt(eig(A^T A)) 1.000000e-04
true 1e-06: svd 1.000000e-06, sqrt(eig(A^T A)) 1.000015e-06
true 1e-08: svd 1.000000e-08, sqrt(eig(A^T A)) 3.318475e-09
true 1e-10: svd 1.000000e-10, sqrt(eig(A^T A)) 5.807072e-09

Numerical rank

In floating point, a rank-deficient matrix has tiny but nonzero singular values. The numerical rank counts the singular values above a tolerance, typically σ1⋅max⁡(m,n)⋅ϵmach\sigma_1 \cdot \max(m, n) \cdot \epsilon_{\text{mach}} (NumPy's default in matrix_rank).

python · try it yourself
import numpy as np

rng = np.random.default_rng(82)
B = rng.normal(size=(100, 3)) @ rng.normal(size=(3, 8))      # rank 3 in exact arithmetic
s = np.linalg.svd(B, compute_uv=False)
print("singular values:", np.array2string(s, precision=2))
print("matrix_rank:", np.linalg.matrix_rank(B))
singular values: [3.73e+01 2.42e+01 1.42e+01 7.41e-15 5.97e-15 2.62e-15 1.75e-15 9.31e-16]
matrix_rank: 3

Best low-rank approximation

The truncated SVD Ak=∑i=1kσiuiviTA_k = \sum_{i=1}^k\sigma_i\mathbf{u}_i\mathbf{v}_i^T is the best rank-kk approximation in both the 2-norm and the Frobenius norm (Eckart-Young), with ∥A−Ak∥2=σk+1\lVert A - A_k\rVert_2 = \sigma_{k+1}. This underlies PCA, compression and denoising.

python · try it yourself
import numpy as np

x = np.linspace(0, 1, 120)
A = np.exp(-((x[:, None] - x[None, :]) ** 2) / 0.05)          # a smooth kernel matrix
U, s, Vt = np.linalg.svd(A)
for k in (2, 5, 10, 20):
    Ak = (U[:, :k] * s[:k]) @ Vt[:k]
    print(f"rank {k:2d}: ||A - A_k||_2 = {np.linalg.norm(A - Ak, 2):.2e}, sigma_(k+1) = {s[k]:.2e}")
rank  2: ||A - A_k||_2 = 2.18e+01, sigma_(k+1) = 2.18e+01
rank  5: ||A - A_k||_2 = 2.39e+00, sigma_(k+1) = 2.39e+00
rank 10: ||A - A_k||_2 = 5.12e-03, sigma_(k+1) = 5.12e-03
rank 20: ||A - A_k||_2 = 6.17e-11, sigma_(k+1) = 6.17e-11

Rank-deficient least squares

If AA lacks full column rank, the least-squares solution is not unique. The SVD picks the one of minimum norm: c=∑σi>τuiTyσivi\mathbf{c} = \sum_{\sigma_i > \tau}\frac{\mathbf{u}_i^T\mathbf{y}}{\sigma_i}\mathbf{v}_i, dropping singular values below a cutoff τ\tau. Truncating also regularizes nearly rank-deficient problems, where tiny σi\sigma_i would amplify noise.

In software

  • np.linalg.svd(A, full_matrices=False) (thin SVD), scipy.linalg.svd(lapack_driver="gesdd") (divide and conquer, default) or "gesvd" (QR iteration, slower but more robust).
  • np.linalg.lstsq and np.linalg.pinv are SVD-based with a cutoff rcond.
  • For large sparse matrices or only a few singular triplets: scipy.sparse.linalg.svds, randomized SVD (sklearn.utils.extmath.randomized_svd).

Exercises

Exercise 1: Norms from singular values

A matrix has singular values 10,2,0.510, 2, 0.5. Find ∥A∥2\lVert A\rVert_2, ∥A∥F\lVert A\rVert_F and κ2(A)\kappa_2(A).

Answer

1010, 104.25≈10.21\sqrt{104.25} \approx 10.21, 2020.

Solution

∥A∥F2=∑σi2=100+4+0.25\lVert A\rVert_F^2 = \sum\sigma_i^2 = 100 + 4 + 0.25; κ=100.5\kappa = \frac{10}{0.5}.

Exercise 2: Rank-1 error

What is the 2-norm error of the best rank-1 approximation of the matrix in exercise 1?

Answer

22.

Solution

∥A−A1∥2=σ2=2\lVert A - A_1\rVert_2 = \sigma_2 = 2.

Exercise 3: Minimum-norm solution (code)

For A=(111111)A = \begin{pmatrix} 1 & 1 \\ 1 & 1 \\ 1 & 1\end{pmatrix} (rank 1) and y=(1,2,3)\mathbf{y} = (1, 2, 3), compute the minimum-norm least-squares solution with lstsq and with pinv.

Answer

c=(1,1)\mathbf{c} = (1, 1) from both.

Solution

Any c1+c2=2c_1 + c_2 = 2 (the mean of y\mathbf{y}) minimizes the residual; the shortest such vector is (1,1)(1, 1).

python · try it yourself
import numpy as np
A = np.ones((3, 2)); y = np.array([1.0, 2, 3])
print(np.round(np.linalg.lstsq(A, y, rcond=None)[0], 10), np.round(np.linalg.pinv(A) @ y, 10))
[1. 1.] [1. 1.]

Test yourself

Why not compute singular values as square roots of the eigenvalues of ATAA^TA?

  1. It is slower only
  2. It squares the condition number and loses small singular values
  3. ATAA^TA may not be square
  4. It gives the wrong largest value

The best rank-kk approximation error in the 2-norm is…

  1. σk\sigma_k
  2. σk+1\sigma_{k+1}
  3. ∑i>kσi\sum_{i>k}\sigma_i
  4. 00

The numerical rank of a matrix is…

  1. the number of rows
  2. the number of singular values above a tolerance
  3. the determinant
  4. always full