Foundations to Frontier
Tests
A. Mathematical Foundations › A8 Numerical Methods and Scientific Computing

Sparse Matrices and Iterative Solvers

Discretized PDEs, SLAM, power networks and graph problems produce enormous linear systems in which almost every entry is zero. This topic shows how to store them (COO, CSR), why dense Gaussian elimination is hopeless at that scale, and how iterative solvers work: Jacobi, Gauss–Seidel, SOR and the conjugate gradient method, with convergence governed by the condition number. You will implement CSR and CG, measure how iteration counts grow with grid size, and use matrix-free preconditioned CG to solve a spacecraft plate with 1000:1 conductivity contrast, where diagonal preconditioning cuts the work by 8×.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Mostly empty tables

Imagine a table showing which of a million people are direct neighbours. Each person has only a handful of neighbours, so almost every square in the table is “no”. Writing out the whole table would waste enormous space. Just listing the neighbours is tiny. Big engineering problems produce exactly such sparse matrices. Instead of solving them in one heroic elimination, we improve a guess again and again until it’s good enough.

Sparse systems: who reaches the answer first?
1e+01e-41e-81e-12iteration →
■ Jacobi: >300 iterations to 1e−8■ Gauss–Seidel: >300 iterations to 1e−8■ SOR ω=1.60: 55 iterations to 1e−8■ CG: 38 iterations to 1e−8
N = 144 unknowns, 672 nonzeros (3.24% dense); optimal SOR ω ≈ 1.614
The matrix is almost all zeros, so we store only the nonzeros and never form it densely. Jacobi and Gauss–Seidel damp high-frequency error quickly but crawl on smooth error: their iteration counts grow like n². SOR with a well-chosen ω cuts that to about n, and conjugate gradients (for symmetric positive definite systems) also scales like n with no parameter to tune. Multigrid and good preconditioners bring it close to constant.
Why engineers care

A thermal or structural model with a million nodes gives a million-by-million matrix: 8 terabytes if stored densely, about 40 MB if sparse. SLAM back-ends (g2o, GTSAM, Ceres) and CFD codes are fast only because they exploit sparsity. Iterative solvers with good preconditioners are how the largest simulations on Earth, and onboard estimators, actually get solved.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A sparse matrix is one where…
  2. 2. Storing only nonzeros saves…
  3. 3. Iterative solvers…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Storage formats

  • COO: a list of (row, col, value) triplets. Easy to build.
  • CSR (compressed sparse row): data and indices (the nonzeros and their columns, row by row), plus indptr (where each row starts). The matrix-vector product costs O(nnz)O(\text{nnz}).
  • Matrix-free: never store the matrix at all; just code v↦Avv\mapsto Av (e.g. a stencil).

Why not Gaussian elimination?

Dense LU costs 23N3\frac23N^3 flops and N2N^2 storage. For a 2-D grid with N=106N = 10^6 that’s about 101810^{18} flops. Sparse direct solvers (with nested-dissection ordering) do much better but suffer fill-in. Iterative methods need only matrix-vector products.

Classical iterations

Split A=D+L+UA = D + L + U:

  • Jacobi: x←x+D−1(b−Ax)x\leftarrow x + D^{-1}(b - Ax).
  • Gauss–Seidel: x←x+(D+L)−1(b−Ax)x\leftarrow x + (D + L)^{-1}(b - Ax).
  • SOR: over-relax Gauss–Seidel by ω∈(1,2)\omega\in(1, 2).
📝 Quick check (Level 1)
0/3 answered
  1. 1. CSR matrix-vector cost is proportional to…
  2. 2. Dense LU on N unknowns costs about…
  3. 3. Gauss–Seidel differs from Jacobi by…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Conjugate gradients

For symmetric positive definite AA, CG minimizes the AA-norm error over the growing Krylov space span⁡{b,Ab,A2b,… }\operatorname{span}\{b, Ab, A^2b, \dots\}. It needs one matvec and a few vector operations per iteration:

α=r⊤rp⊤Ap, x+=αp, r−=αAp, β=rnew⊤rnewr⊤r, p=r+βp\alpha = \frac{r^\top r}{p^\top Ap},\ x\mathrel{+}=\alpha p,\ r\mathrel{-}=\alpha Ap,\ \beta = \frac{r_{\text{new}}^\top r_{\text{new}}}{r^\top r},\ p = r + \beta p

Convergence and conditioning

∥ek∥A∥e0∥A≤2(κ−1κ+1)k\frac{\lVert e_k\rVert_A}{\lVert e_0\rVert_A}\le2\Big(\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1}\Big)^k

For Poisson on an n×nn\times n grid, κ∼n2\kappa\sim n^2. So CG needs O(n)O(n) iterations, while Jacobi and Gauss–Seidel need O(n2)O(n^2).

Preconditioning

Solve M−1Ax=M−1bM^{-1}Ax = M^{-1}b with M≈AM\approx A cheap to invert. Options:

  • Jacobi: M=diag⁡AM = \operatorname{diag}A.
  • Incomplete Cholesky or LU.
  • Multigrid: near O(N)O(N) total work.

For nonsymmetric systems, use GMRES or BiCGSTAB.

🐍 Exercise 1: CSR and Krylov methods
Build CSR storage and a matvec. Compare Jacobi, Gauss–Seidel and CG on 2-D Poisson. Show that CG iterations roughly double when the grid is refined 2×.
📝 Quick check (Level 2)
0/3 answered
  1. 1. CG requires A to be…
  2. 2. CG iterations scale like…
  3. 3. A preconditioner M should be…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Why CG is optimal in the Krylov space

The residuals are mutually orthogonal and the directions AA-conjugate (pi⊤Apj=0p_i^\top Ap_j = 0). So xkx_k minimizes ∥x−x∗∥A\lVert x - x_*\rVert_A over x0+Kkx_0 + \mathcal K_k. Equivalently, ek=Pk(A)e0e_k = P_k(A)e_0 for the best polynomial with Pk(0)=1P_k(0) = 1. Bounding with a shifted Chebyshev polynomial on [λmin⁡,λmax⁡][\lambda_{\min}, \lambda_{\max}] gives the κ\sqrt\kappa rate. ∎ In exact arithmetic CG terminates in at most NN steps.

Convergence of Jacobi

The error satisfies ek+1=(I−D−1A)eke_{k+1} = (I - D^{-1}A)e_k. It converges iff the spectral radius ρ(I−D−1A)<1\rho(I - D^{-1}A) < 1. For 1-D Poisson, ρ=cos⁡(πh)≈1−π2h2/2\rho = \cos(\pi h)\approx1 - \pi^2h^2/2, so it needs O(h−2)O(h^{-2}) iterations.

Condition number of 1-D Poisson

The eigenvalues of tridiag⁡(−1,2,−1)\operatorname{tridiag}(-1, 2, -1) are 4sin⁡2(kπh/2)4\sin^2(k\pi h/2). So κ=sin⁡2(nπh/2)/sin⁡2(πh/2)≈4/(πh)2\kappa = \sin^2(n\pi h/2)/\sin^2(\pi h/2)\approx4/(\pi h)^2.

Exercise 3.1: CG iteration bound for κ = 10⁴, tolerance 10⁻⁶

k≈12κln⁡(2/tol)=50⋅14.5≈726k\approx\frac12\sqrt\kappa\ln(2/\text{tol}) = 50\cdot14.5\approx726. Jacobi would need about κ·14.5 ≈ 145 000 iterations.

📝 Quick check (Level 3)
0/3 answered
  1. 1. CG directions are mutually…
  2. 2. Jacobi converges iff…
  3. 3. κ of 1-D Poisson grows like…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Matrix-free PCG for a heterogeneous plate

A radiator plate with copper heat straps (conductivity 1000× the aluminium) gives a 5-point operator whose coefficients jump by three orders of magnitude. Plain CG slows from about 150 iterations (uniform plate) to about 1700. Diagonal (Jacobi) preconditioning rescales each row by its own conductivity and brings it back to about 190. The operator is applied matrix-free as a stencil on the grid. The dense matrix would need 42 MB for this modest 48 × 48 grid. The same pattern (stencil plus Krylov plus preconditioner) runs large thermal, CFD and SLAM back-ends.

🐍 Exercise 2 (research): preconditioned CG on a heterogeneous plate
Build a matrix-free variable-coefficient diffusion operator (harmonic-mean face conductivities). Check its symmetry. Solve with CG and Jacobi-preconditioned CG, and compare iteration counts with a uniform plate.

Sparse linear algebra in AI, robotics and aerospace

  • SLAM and bundle adjustment: sparse normal equations solved by sparse Cholesky with good orderings (g2o, GTSAM, Ceres), plus Schur complements for landmarks.
  • Large-scale simulation: PETSc and hypre (CG, GMRES and algebraic multigrid) run on supercomputers for CFD and structural analysis.
  • Graph learning: graph Laplacians are sparse; spectral GNNs and PageRank-type computations use sparse matvecs.
  • Optimization: interior-point and SQP solvers (IPOPT, OSQP) factor sparse KKT systems at every iteration.

References

  1. Y. Saad (2003). Iterative Methods for Sparse Linear Systems, 2nd ed. SIAM.
  2. J. Shewchuk (1994). “An introduction to the conjugate gradient method without the agonizing pain.” CMU-CS-94-125.
  3. M. Hestenes, E. Stiefel (1952). “Methods of conjugate gradients for solving linear systems.” J. Res. Nat. Bur. Standards, 49(6).
  4. T. Davis (2006). Direct Methods for Sparse Linear Systems. SIAM.
  5. W. Briggs, V. Henson, S. McCormick (2000). A Multigrid Tutorial, 2nd ed. SIAM.
  6. F. Dellaert, M. Kaess (2017). “Factor graphs for robot perception.” Foundations and Trends in Robotics, 6(1–2).
  7. S. Balay et al. PETSc Users Manual. Argonne National Laboratory.
  8. L. N. Trefethen, D. Bau (1997). Numerical Linear Algebra. SIAM. Lectures 38–40.