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-05 Low Rank

Last time

  • Solving least squares problems

  • Geometry of the SVD

Today

  • Reflection on algorithm choices

  • Low-rank structure

  • Primer on interpolation

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

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, aspect_ratio=:equal)
end
Aplot (generic function with 1 method)

Condition number via SVD

U[σmax⁡⋱σmin⁡]⏞ΣVT=AU \overbrace{\begin{bmatrix} \sigma_{\max} && \\ & \ddots & \\ && \sigma_{\min} \end{bmatrix}}^{\Sigma} V^T = A
∥A∥=σmax⁡κ(A)=σmax⁡σmin⁡=cond(A)\begin{align} \lVert A \rVert &= \sigma_{\max} & \kappa(A) &= \frac{\sigma_{\max}}{\sigma_{\min}} = \texttt{cond}(A) \end{align}
A = randn(2,2) ## nonsymmetric
#A = A + A'
2×2 Matrix{Float64}: -0.7476 1.08969 0.487512 1.0334
@show svdvals(A)
U, S, V = svd(A)
@show norm(U - U')
Aplot(A)
svdvals(A) = [1.5227644790588872, 0.8562092695757885]
norm(U - U') = 1.5700924586837752e-16
Loading...

Example: autonomous vehicles

  • Need to solve least squares problems in real time

  • Weight/cost/size increase with compute

  • What algorithm to choose?

  • What precision to use?

Factors

  • How many objects?

  • Speed (of robot and objects)

  • Aerial, wheeled, walking

  • Fog, light -- longer memory?

  • Tolerences (how accurate does the solution need to be?)

  • Consequences of being wrong, who bears those consequences?

A = rand(5000, 500)
A_32 = Float32.(A)
@show cond(A)
@time qr(A);       # Householder; backward stable
@time qr_chol(A);  # Unstable
@time qr(A_32);
cond(A) = 56.40146543951136
  0.202298 seconds (42.99 k allocations: 21.522 MiB, 56.51% gc time, 13.47% compilation time)
  0.126691 seconds (271.75 k allocations: 37.191 MiB, 8.83% gc time, 64.42% compilation time)
  0.103200 seconds (110.47 k allocations: 15.324 MiB, 7.64% gc time, 42.06% compilation time)
V = vander(LinRange(-1, 1, 20))
@show cond(V)
Q, R = qr(Float32.(V))
@show norm(Q' * Q - I)
Q, R = qr_chol(V)
@show norm(Q' * Q - I)
cond(V) = 2.7224082312417406e8
norm(Q' * Q - I) = 1.6641898f-6
norm(Q' * Q - I) = 0.1749736012761826
0.1749736012761826

Best low rank approximation

The SVD can be truncated to yield the best rank-kk approximation of a matrix.

n, k = 2, 1
A = randn(n, n)
Aplot(A)
Loading...
U, S, V = svd(A)
@show S[1:k+1]
Uhat = U[:, 1:k]
Shat = S[1:k]
Vhat = V[:, 1:k]
Ahat = Uhat * diagm(Shat) * Vhat'
@show norm(A - Ahat)
Aplot(A - Ahat)
S[1:k + 1] = [1.556636743765386, 0.49815301474129786]
norm(A - Ahat) = 0.4981530147412979
Loading...

Example: Galaxies

Suppose we have two galaxies of size n1=100n_1 = 100 and n2=200n_2 = 200, each randomly distributed around their respective centers.

galaxy(center, sigma, n) = reshape(center, 1, 3) .+ sigma*randn(n, 3)
g1 = galaxy([0 0 0], 1, 100)
g2 = galaxy([10 0 0], 1, 100)

scatter(g1[:,1], g1[:,2], aspect_ratio=:equal)
scatter!(g2[:,1], g2[:,2])
Loading...

Forces between stars

Consider the gravitational force from a star at position x2x_2 acting on a star at position x1x_1,

F1,2=Gm1m2∥x2−x1∥3(x2−x1)F_{1,2} = G \frac{m_1 m_2}{\lVert x_2 - x_1 \rVert^3} (x_2 - x_1)

where m1m_1 and m2m_2 are the masses of each star respectively.

function gravity(g1, g2)
    m = size(g1, 1)
    n = size(g2, 1)
    F = zeros(3*m, n)
    for i in 0:m-1
        for j in 1:n
            r = g2[j,:] - g1[1+i,:]
            F[1+3*i:3*(i+1),j] = r / norm(r)^3
        end
    end
    F
end
gravity(g1, g2)
1500×500 Matrix{Float64}: 0.00747727 0.00722756 0.0083756 … 0.00775603 0.0068401 -0.0012211 -0.00230897 0.000268541 -0.000871993 -0.00144419 0.000446229 0.000405469 -0.000449184 0.000336211 -7.05191e-6 0.00871837 0.00887671 0.0091893 0.00890791 0.00811223 -0.000380768 -0.00182582 0.00155944 0.000101437 -0.00080111 0.000833388 0.000826998 -0.000219516 … 0.000698882 0.000241846 0.0104935 0.0113772 0.0100072 0.0104856 0.00985168 0.000835297 -0.00106463 0.00320398 0.0014789 0.000143484 0.000736 0.000755218 -0.000626626 0.000536052 -7.82414e-6 0.00971377 0.00994979 0.00981067 0.00986389 0.00888553 -0.000269764 -0.00196191 0.00193167 … 0.000296485 -0.000763921 0.000162495 0.000109169 -0.0010993 -1.80245e-5 -0.000436521 0.00872849 0.00915971 0.00806927 0.00870042 0.00788611 ⋮ ⋱ 0.00793605 0.0082235 0.0076998 0.0079794 0.00724555 -1.03954e-5 -0.00126155 0.00156054 0.000409223 -0.000399816 -0.000595944 -0.000688082 -0.00150121 … -0.000738435 -0.000955779 0.00779674 0.00770449 0.00814241 0.0079814 0.00704792 -0.000837513 -0.0020259 0.000748112 -0.000444603 -0.00111036 -0.000358136 -0.000411559 -0.00133802 -0.000503885 -0.000730035 0.011988 0.0132281 0.0106149 0.0118269 0.0110202 0.00131926 -0.000985346 0.00392575 … 0.00208286 0.000433687 -0.00040786 -0.000566637 -0.00192742 -0.000653743 -0.00116159 0.00645454 0.00674391 0.00612724 0.00644798 0.00592067 0.000154844 -0.000763251 0.00128632 0.000464253 -0.000153861 -0.00086043 -0.000964611 -0.00149562 -0.00096427 -0.00108576

Spectrum

g1 = galaxy([0 0 0], 1, 500)
g2 = galaxy([30 0 0], 1, 500)
F = gravity(g1, g2)
U, S, V = svd(F)
scatter(S, yscale=:log10, ylims=(1e-15, 100), xlims=(0, 200))
Loading...
k = 10
Uhat = U[:,1:k]
Shat = S[1:k]
Vhat = V[:,1:k]
Fhat = Uhat * diagm(Shat) * Vhat'
@show norm(F)
@show norm(F - Fhat)
size(F)
norm(F) = 1.2634785388849465
norm(F - Fhat) = 2.0501158287246048e-5
(1500, 500)