Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

2026-10-02 Singular Value Decomposition

Last time

  • Intro to Krylov/GMRES

  • Matrix norms and conditioning

Today

  • Least squares problems

  • Geometry of the SVD

using LinearAlgebra
using Plots
default(linewidth=4, legendfontsize=12)

function vander(x, k=nothing)
    if isnothing(k)
        k = length(x)
    end
    m = length(x)
    V = ones(m, k)
    for j in 2:k
        V[:, j] = V[:, j-1] .* x
    end
    V
end

function gram_schmidt_classical(A)
    m, n = size(A)
    Q = zeros(m, n)
    R = zeros(n, n)
    for j in 1:n
        v = A[:,j]
        R[1:j-1,j] = Q[:,1:j-1]' * v
        v -= Q[:,1:j-1] * R[1:j-1,j]
        R[j,j] = norm(v)
        Q[:,j] = v / R[j,j]
    end
    Q, R
end

function qr_householder(A)
    m, n = size(A)
    R = copy(A)
    V = [] # list of reflectors
    for j in 1:n
        v = copy(R[j:end, j])
        v[1] += sign(v[1]) * norm(v) # <---
        v = normalize(v)
        R[j:end,j:end] -= 2 * v * v' * R[j:end,j:end]
        push!(V, v)
    end
    V, R
end

function qr_chol(A)
    R = cholesky(A' * A).U
    Q = A / R
    Q, R
end

function qr_chol2(A)
    Q, R = qr_chol(A)
    Q, R1 = qr_chol(Q)
    Q, R1 * R
end
qr_chol2 (generic function with 1 method)

Condition numbers for matrices

We may have informally referred to a matrix as “ill-conditioned” when the columns are nearly linearly dependent, but let’s make this concept for precise. Recall the definition of (relative) condition number:

κ=max⁡δx∣δf∣/∣f∣∣δx∣/∣x∣.\kappa = \max_{\delta x} \frac{|\delta f|/|f|}{|\delta x|/|x|} .

We understood this definition for scalar problems, but it also makes sense when the inputs and/or outputs are vectors (or matrices, etc.) and absolute value is replaced by vector (or matrix) norms. Consider matrix-vector multiplication, for which f(x)=Axf(x) = A x.

κ(A)=max⁡δx∥A(x+δx)−Ax∥/∥Ax∥∥δx∥/∥x∥=max⁡δx∥Aδx∥∥δx∥ ∥x∥∥Ax∥=∥A∥∥x∥∥Ax∥.\kappa(A) = \max_{\delta x} \frac{\lVert A (x+\delta x) - A x \rVert/\lVert A x \rVert}{\lVert \delta x\rVert/\lVert x \rVert} = \max_{\delta x} \frac{\lVert A \delta x \rVert}{\lVert \delta x \rVert} \, \frac{\lVert x \rVert}{\lVert A x \rVert} = \lVert A \rVert \frac{\lVert x \rVert}{\lVert A x \rVert} .

There are two problems here:

  • I wrote κ(A)\kappa(A) but my formula depends on xx.

  • What is that ∥A∥\lVert A \rVert beastie?

Matrix norms induced by vector norms

Vector norms are built into the linear space (and defined in term of the inner product). Matrix norms are induced by vector norms, according to

∥A∥=max⁡x≠0∥Ax∥∥x∥.\lVert A \rVert = \max_{x \ne 0} \frac{\lVert A x \rVert}{\lVert x \rVert} .
  • This equation makes sense for non-square matrices -- the vector norms of the input and output spaces may differ.

  • Due to linearity, all that matters is direction of xx, so it could equivalently be written

∥A∥=max⁡∥x∥=1∥Ax∥.\lVert A \rVert = \max_{\lVert x \rVert = 1} \lVert A x \rVert .

The formula makes sense, but still depends on xx.

κ(A)=∥A∥∥x∥∥Ax∥\kappa(A) = \lVert A \rVert \frac{\lVert x \rVert}{\lVert Ax \rVert}

Consider the matrix

A=[1000].A = \begin{bmatrix} 1 & 0 \\ 0 & 0 \end{bmatrix} .
  • What is the norm of this matrix?

  • What is the condition number when x=[1,0]Tx = [1,0]^T?

  • What is the condition number when x=[0,1]Tx = [0,1]^T?

Condition number of the matrix

The condition number of matrix-vector multiplication depends on the vector. The condition number of the matrix is the worst case (maximum) of the condition number for any vector, i.e.,

κ(A)=max⁡x≠0∥A∥∥x∥∥Ax∥.\kappa(A) = \max_{x \ne 0} \lVert A \rVert \frac{\lVert x \rVert}{\lVert A x \rVert} .

If AA is invertible, then we can rephrase as

κ(A)=max⁡x≠0∥A∥∥A−1(Ax)∥∥Ax∥=max⁡Ax≠0∥A∥∥A−1(Ax)∥∥Ax∥=∥A∥∥A−1∥.\kappa(A) = \max_{x \ne 0} \lVert A \rVert \frac{\lVert A^{-1} (A x) \rVert}{\lVert A x \rVert} = \max_{A x \ne 0} \lVert A \rVert \frac{\lVert A^{-1} (A x) \rVert}{\lVert A x \rVert} = \lVert A \rVert \lVert A^{-1} \rVert .

Evidently multiplying by a matrix is just as ill-conditioned of an operation as solving a linear system using that matrix.

Condition number via SVD

κ(A)=∥A∥ ∥A−1∥\kappa(A) = \lVert A \rVert \ \lVert A^{-1} \rVert

Or, in terms of the SVD

UΣVT=svd(A)U \Sigma V^T = \texttt{svd}(A)

where

Σ=[σmax⁡⋱σmin⁡],\Sigma = \begin{bmatrix} \sigma_{\max} && \\ & \ddots & \\ && \sigma_{\min} \end{bmatrix},
κ(A)=σmax⁡σmin⁡=cond(A)\kappa(A) = \frac{\sigma_{\max}}{\sigma_{\min}} = \texttt{cond}(A)

Least squares and the normal equations

A least squares problem takes the form: given an m×nm\times n matrix AA (m≥nm \ge n), find xx such that

∥Ax−b∥\lVert Ax - b \rVert

is minimized. If AA is square and full rank, then this minimizer will satisfy Ax−b=0A x - b = 0, but that is not the case in general because bb is not in the range of AA. The residual Ax−bA x - b must be orthogonal to the range of AA.

  • Is this the same as saying AT(Ax−b)=0A^T (A x - b) = 0?

  • If QR=AQR = A, is it the same as QT(Ax−b)=0Q^T (A x - b) = 0?

We showed that QQTQQ^T is an orthogonal projector onto the range of QQ. If QR=AQR = A,

QQT(Ax−b)=QQT(QRx−b)=Q(QTQ)Rx−QQTb=QRx−QQTb=Ax−QQTb.QQ^T (A x - b) = QQ^T(Q R x - b) = Q (Q^T Q) R x - QQ^T b = QR x - QQ^T b = A x - QQ^T b .

So if bb is in the range of AA, we can solve Ax=bA x = b. If not, we need only orthogonally project bb into the range of AA.

The Professional’s Way: QR (Householder)

Solve Rx=QTbR x = Q^T b.

  • QR factorization costs 2mn2−23n32 m n^2 - \frac 2 3 n^3 operations and is done once per matrix AA.

  • Computing QTbQ^T b costs 4(m−n)n+2n2=4mn−2n24 (m-n)n + 2 n^2 = 4 mn - 2n^2 (using the elementary reflectors, which are stable and lower storage than naive storage of QQ).

  • Solving with RR costs n2n^2 operations. Total cost per right hand side is thus 4mn−n24 m n - n^2.

This method is stable and accurate.

The 737 MAX Way: Cholesky/Normal Equations

fast, unregulated, and dangerous

The mathematically equivalent form (ATA)x=ATb(A^T A) x = A^T b are called the normal equations. The solution process involves factoring the symmetric and positive definite n×nn\times n matrix ATAA^T A.

  • Computing ATAA^T A costs mn2m n^2 flops, exploiting symmetry.

  • Factoring ATA=RTRA^T A = R^T R costs 13n3\frac 1 3 n^3 flops. The total factorization cost is thus mn2+13n3m n^2 + \frac 1 3 n^3.

  • Computing ATbA^T b costs 2mn2 m n.

  • Solving with RTR^T costs n2n^2.

  • Solving with RR costs n2n^2. Total cost per right hand side is thus 2mn+2n22 m n + 2 n^2.

The product ATAA^T A is ill-conditioned: κ(ATA)=κ(A)2\kappa(A^T A) = \kappa(A)^2 and can reduce the accuracy of a least squares solution.

The Prepper’s Way: Singular Value Decomposition

UΣVT=AU \Sigma V^T = A

where UU and VV have orthonormal columns and Σ\Sigma is diagonal with nonnegative entries. The entries of Σ\Sigma are called singular values and this decomposition is the singular value decomposition (SVD). It may remind you of an eigenvalue decomposition XΛX−1=AX \Lambda X^{-1} = A, but

  • the SVD exists for all matrices (including non-square and deficient matrices)

  • U,VU,V have orthogonal columns (while XX can be arbitrarily ill-conditioned). Indeed, if a matrix is symmetric and positive definite (all positive eigenvalues), then U=VU=V and Σ=Λ\Sigma = \Lambda. Computing an SVD requires a somewhat complicated iterative algorithm, but a crude estimate of the cost is 2mn2+11n32 m n^2 + 11 n^3. Note that this is similar to the cost of QRQR when m≫nm \gg n, but much more expensive for square matrices. Solving with the SVD involves

  • Compute UTbU^T b at a cost of 2mn2 m n.

  • Solve with the diagonal n×nn\times n matrix Σ\Sigma at a cost of nn.

  • Apply VV at a cost of 2n22 n^2. The total cost per right hand side is thus 2mn+2n22 m n + 2n^2.

SVD gives the unique minimum norm solution when AA is rank deficient

Demo: Geometry of the Singular Value Decomposition

default(aspect_ratio=:equal)

function peanut()
    theta = LinRange(0, 2*pi, 50)
    r = 1 .+ .4*sin.(3*theta) + .6*sin.(2*theta)
    r' .* [cos.(theta) sin.(theta)]'
end

function circle()
    theta = LinRange(0, 2*pi, 50)
    [cos.(theta) sin.(theta)]'
end

function Aplot(A)
    "Plot a transformation from X to Y"
    X = peanut()
    Y = A * X
    p = scatter(X[1,:], X[2,:], label="in")
    scatter!(p, Y[1,:], Y[2,:], label="out")
    X = circle()
    Y = A * X
    q = scatter(X[1,:], X[2,:], label="in")
    scatter!(q, Y[1,:], Y[2,:], label="out")
    plot(p, q, layout=2)
end
Aplot (generic function with 1 method)
Aplot(1.5*I)
Loading...

Diagonal matrices

Perhaps the simplest transformation is a scalar multiple of the identity.

Aplot([.4 0; 0 1.1])
Loading...

The diagonal entries can be different sizes.

A=[200.5]A = \begin{bmatrix} 2 & 0 \\ 0 & .5 \end{bmatrix}
Aplot([2 0; 0 .5])
Loading...

The circles becomes an ellipse that is aligned with the coordinate axes

Givens Rotation (as example of orthogonal matrix)

We can rotate the input using a 2×22\times 2 matrix, parametrized by θ\theta. Its transpose rotates in the opposite direction.

function givens(theta)
    s = sin(theta)
    c = cos(theta)
    [c -s; s c]
end

G = givens(pi/2)
Aplot(G)
Loading...
Aplot(G')
Loading...

Reflection

We’ve previously seen that reflectors have the form F=I−2vvTF = I - 2 v v^T where vv is a normalized vector. Reflectors satisfy FTF=IF^T F = I and F=FTF = F^T, thus F2=IF^2 = I.

function reflect(theta)
    v = [cos(theta), sin(theta)]
    I - 2 * v * v'
end

Aplot(reflect(0.3))
Loading...

Singular Value Decomposition

The SVD is A=UΣVTA = U \Sigma V^T where UU and VV have orthonormal columns and Σ\Sigma is diagonal with nonnegative entries. It exists for any matrix (non-square, singular, etc.). If we think of orthogonal matrices as reflections/rotations, this says any matrix can be represented as reflect/rotate, diagonally scale, and reflect/rotate again.

Let’s try a random symmetric matrix.

A = randn(2, 2)
#A += A' # make symmetric
@show det(A) # Positive means orientation is preserved
Aplot(A)
det(A) = -1.0746714389805787
Loading...
U, S, V = svd(A)
@show S
@show norm(U * diagm(S) * V' - A) # Should be zero
@show det(U), det(V)
Aplot(V) # Rotate/reflect in preparation for scaling]
S = [2.6065410498109953, 0.4122979145325583]
norm(U * diagm(S) * V' - A) = 6.080941944488118e-16
(det(U), det(V)) = (-1.0, 1.0000000000000002)
Loading...
  • What visual features indicate that this is a symmetric matrix?

  • Is the orthogonal matrix a reflection or rotation?

    • Does this change when the determinant is positive versus negative (rerun the cell above as needed).

Parts of the SVD

Aplot(diagm(S)) # scale along axes
Loading...
Aplot(U) # rotate/reflect back
Loading...

Putting it together

Aplot(U * diagm(S) * V')
Loading...

Observations

  • The circle always maps to an ellipse

  • The UU and VV factors may reflect even when det⁡A>0\det A > 0

What makes a matrix ill-conditioned?

A = [10 5; .9 .5]
@show cond(A)
svdvals(A)
cond(A) = 252.116033572376
2-element Vector{Float64}: 11.22755613596245 0.0445332888070338
m = 100
x = LinRange(-1, 1, m)
A = vander(x, 20)
@show cond(A)
svdvals(A)
cond(A) = 7.206778416853811e6
20-element Vector{Float64}: 11.510601491136654 8.993572449917352 5.907360126610745 3.6029154831946064 1.9769729310318853 1.098310376576986 0.5392322016751737 0.2820042230178553 0.12425066006852684 0.06183086124622112 0.024242929146349384 0.011556331531623212 0.003964435582121844 0.0018189836735690052 0.0005298822985647027 0.00023487660987952337 5.4870893944525604e-5 2.3566694052620285e-5 3.8286365250847765e-6 1.597190981232044e-6

Orthogonal transformations don’t affect singular values (or conditioning)

A = [3 0; 0 .5]
Q, _R = qr(randn(2,2))
B = Q * A * Q'
U, S, V = svd(randn(2,2))
@show S
@show S[1] / S[end]
Aplot(diagm(S))
S = [1.0729677922517786, 0.0804276181075547]
S[1] / S[end] = 13.340787872356412
Loading...