SVD of finite differences

This example illustrates the SVD of a first-order finite-difference matrix using the Julia language. This demo was inspired by Gilbert Strang's 2006 article.

This page comes from a single Julia file: svd-diff.jl.

You can access the source code for such Julia documentation using the 'Edit on GitHub' link in the top right. You can view the corresponding notebook in nbviewer here: svd-diff.ipynb, or open it in binder here: svd-diff.ipynb.

Setup

Add the Julia packages used in this demo. Change false to true in the following code block if you are using any of the following packages for the first time.

if false
    import Pkg
    Pkg.add([
        "InteractiveUtils"
        "LaTeXStrings"
        "LinearAlgebra"
        "MIRTjim"
        "Plots"
    ])
end

Tell Julia to use the following packages. Run Pkg.add() in the preceding code block first, if needed.

using InteractiveUtils: versioninfo
using LaTeXStrings
using LinearAlgebra: norm, I, diag, diagm, Diagonal
using MIRTjim: jim, prompt
using Plots: default, plot, scatter
default(); default(label="", markerstrokecolor=:auto, color=:blue, widen=:true)

The following line is helpful when running this jl-file as a script; this way it will prompt user to hit a key after each image is displayed.

isinteractive() && jim(:prompt, true);

First-order finite-difference matrix

N = 16
Δ = diagm(0 => -ones(Int,N-1), 1 => ones(Int,N-1))[1:(N-1),:] # (N-1,N) diff
jim(Δ'; title="$(N-1) × $N finite-difference matrix Δ", color=:cividis)
Example block output

Right singular vectors are cos functions

h = π / N
v = k -> cos.(((1:N)*2 .- 1) * k * h / 2) / sqrt(N/2)

plot(v(5), line=:stem, marker=:circle, title="5th right singular vector",
 xaxis = (L"i", (1,N), 1:N), yaxis=(L"v_5[i]", (-0.5,0.5), (-1:1)*0.5))
Example block output
prompt()

Left singular vectors are -sin functions

u = k -> -sin.((1:(N-1)) * k * h) / sqrt(N/2) # "derivative of cos is -sin"

plot(u(5), line=:stem, marker=:circle, title="5th left singular vector",
 xaxis = (L"i", (1,N), 1:N-1), yaxis=(L"u_5[i]", (-0.5,0.5), (-1:1)*0.5))
Example block output
prompt()

Singular values

(Caution: not in descending order)

σ = k -> 2*sin(k*h/2)
k = 1:(N-1)

scatter(k, σ.(k), title="$(N-1) singular values (unordered)",
 color=:red, xaxis=(L"k", (1,N-1), 1:N-1), yaxis=(L"σ_k", (0,2), 0:2))
Example block output
prompt()

SVD components

V = hcat([v(k) for k in 1:(N-1)]...) # (N,N-1) "V_{N-1}" DCT
U = hcat([u(k) for k in 1:(N-1)]...) # (N-1,N-1) DST
Σ = Diagonal(σ.(1:(N-1))) # (N-1,N_1) Σ_N

jim(
 jim(U', "U: Left singular vectors"; color=:cividis),
 jim(V', "V: Right singular vectors"; color=:cividis),
 jim(Σ', "Σ: Singular values"; color=:cividis),
)
Example block output

Verify correctness of SVD

@assert all(>(0), diag(Σ)) # singular values are nonnegative
@assert Δ * V ≈ U * Σ # "derivative of cos is -sin"
@assert V'V ≈ I(N-1) # V is semi-unitary
@assert U'U ≈ I(N-1) && U*U' ≈ I(N-1) # U is unitary
@assert Δ ≈ U * Σ * V' # SVD of Δ

Reproducibility

This page was generated with the following version of Julia:

using InteractiveUtils: versioninfo
io = IOBuffer(); versioninfo(io); split(String(take!(io)), '\n')
12-element Vector{SubString{String}}:
 "Julia Version 1.10.1"
 "Commit 7790d6f0641 (2024-02-13 20:41 UTC)"
 "Build Info:"
 "  Official https://julialang.org/ release"
 "Platform Info:"
 "  OS: Linux (x86_64-linux-gnu)"
 "  CPU: 4 × AMD EPYC 7763 64-Core Processor"
 "  WORD_SIZE: 64"
 "  LIBM: libopenlibm"
 "  LLVM: libLLVM-15.0.7 (ORCJIT, znver3)"
 "Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)"
 ""

And with the following package versions

import Pkg; Pkg.status()
Status `~/work/book-la-demo/book-la-demo/docs/Project.toml`
  [6e4b80f9] BenchmarkTools v1.5.0
  [aaaa29a8] Clustering v0.15.7
  [35d6a980] ColorSchemes v3.24.0
  [3da002f7] ColorTypes v0.11.4
⌅ [c3611d14] ColorVectorSpace v0.9.10
  [717857b8] DSP v0.7.9
  [72c85766] Demos v0.1.0 `~/work/book-la-demo/book-la-demo`
  [e30172f5] Documenter v1.2.1
  [4f61f5a4] FFTViews v0.3.2
  [7a1cc6ca] FFTW v1.8.0
  [587475ba] Flux v0.14.12
⌅ [a09fc81d] ImageCore v0.9.4
  [71a99df6] ImagePhantoms v0.7.2
  [b964fa9f] LaTeXStrings v1.3.1
  [7031d0ef] LazyGrids v0.5.0
  [599c1a8e] LinearMapsAA v0.11.0
  [98b081ad] Literate v2.16.1
  [7035ae7a] MIRT v0.17.0
  [170b2178] MIRTjim v0.23.0
  [eb30cadb] MLDatasets v0.7.14
  [efe261a4] NFFT v0.13.3
  [6ef6ca0d] NMF v1.0.2
  [15e1cf62] NPZ v0.4.3
  [0b1bfda6] OneHotArrays v0.2.5
  [429524aa] Optim v1.9.2
  [91a5bcdd] Plots v1.40.1
  [f27b6e38] Polynomials v4.0.6
  [2913bbd2] StatsBase v0.34.2
  [d6d074c3] VideoIO v1.0.9
  [b77e0a4c] InteractiveUtils
  [37e2e46d] LinearAlgebra
  [44cfe95a] Pkg v1.10.0
  [9a3f8284] Random
Info Packages marked with ⌅ have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated`

This page was generated using Literate.jl.