Sparse Matrices
When a matrix is overwhelmingly zeros, sparse storage formats reduce memory from O(n²) to O(nnz) — enabling computations on graphs, meshes, and networks that would otherwise be impossible.
Mostly Zeros
COO — Coordinate
- Memory: 3 × nnz words
- SpMV: simple but random row access
- Best for: matrix construction
CSR — Compressed Row
Three arrays: val[], col_ind[], row_ptr[]. Sequential row access → cache-friendly. The workhorse for iterative solvers (CG, GMRES). Memory: nnz + nnz + (m+1) words.
CSC — Compressed Column
- Mirrors CSR but column-major: col_ptr[], row_ind[], val[]
- Native in MATLAB — all sparse() matrices use CSC internally
- Direct solvers (Cholesky, LU) prefer column access
- Aᵀv is cheap in CSC = Av in CSR
- Row access expensive — scan entire matrix
SpMV and Fill-In
Graphs and Networks
- PageRank: power iteration on sparse column-stochastic matrix
- Spectral clustering: eigenvectors of sparse Laplacian L = D − A
- GNNs: message passing = sparse à H W per layer
- Shortest paths: Bellman-Ford as SpMV in (min, +) semiring
- Triangle counting: SpGEMM diagonal of A³
FEM and Stiffness Matrices
FEM on a 2D mesh with n nodes generates a sparse symmetric positive definite stiffness matrix K with nnz ≈ 7n. Basis functions have local support — each equation couples only neighboring nodes. Assembly: COO triples per element → convert to CSR → iterative solve (CG + multigrid).
Choosing the Sparse Solver
- Direct (UMFPACK, CHOLMOD): robust, optimal for multiple RHS, limited by fill-in in 3D
- Iterative (CG, GMRES): O(nnz) per step, scales to n > 10⁷, needs preconditioner
- ILU preconditioner: sparse approx. factorization → fewer CG iterations
- Multigrid: O(n) total for elliptic PDEs — gold standard in 3D
- Matrix-free: only iterative possible if A defined implicitly
Sparse by Design
COO for assembly → CSR for arithmetic → CSC for column operations. SpMV is the critical primitive — memory-bandwidth bound. Fill-in during factorization requires reordering. Graphs, FEM, and Markov chains all generate naturally sparse matrices. Direct solvers for robustness and multiple RHS; iterative + preconditioner for large 3D problems.