Dense linear algebra
Linear algebra is the foundation of scientific computing. Nearly every method in this book reduces to it in the end: Newton’s method solves a linear system at each step, least-squares fitting is a sequence of linear least-squares problems, implicit ODE solvers and finite elements solve linear systems, and Prony’s method and polynomial root finding compute eigenvalues. This part covers dense matrices, stored in full, of up to a few thousand rows.
The central questions
- Which problem? A linear system ; a least-squares problem ; an eigenvalue problem ; or the singular value decomposition .
- What structure? Symmetric (or Hermitian) positive definite matrices halve the work of a linear solve and need no pivoting; symmetric matrices have real eigenvalues and orthogonal eigenvectors, computed far more cheaply and reliably than those of a general matrix.
- How well conditioned? The condition number measures how much the solution can change relative to a change in the data. No algorithm can do better than lose about digits; a good algorithm does not lose more.
Stability and conditioning
The organising idea of numerical linear algebra is backward stability, developed by James Wilkinson in the 1950s and 1960s. An algorithm is backward stable if the answer it computes is the exact answer for a slightly perturbed problem: the computed solution of satisfies with a small multiple of the machine precision . Combined with the conditioning of the problem, this bounds the error:
The rule of thumb, forward error ≈ condition number × backward error, explains almost every numerical result in this part. Gaussian elimination with partial pivoting, Householder QR, the QR algorithm and the SVD are all backward stable (elimination in practice rather than in the worst case); the classical Gram-Schmidt process, the normal equations for least squares, and the eigenvalues of for the SVD are not, and the reference crate implements them to show the difference.
Two crates
The production crate
lib_jsl_linalg (API reference) is a pure-Rust replacement
for LAPACK, for real (f64) and complex (Complex<f64>) matrices. Its traits follow the names of the
ndarray-linalg crate (solve, inv, det, factorize, cholesky, solve_triangular, eigh,
eig, svd, least_squares), and are implemented for every ndarray two-dimensional array. The matrix exponential and the solvers for Sylvester, Lyapunov and Riccati equations are free functions
with SciPy’s names (expm, solve_sylvester, solve_discrete_are, …). The
factorisations are organised, as in LAPACK, so that most of their arithmetic is done by a fast
matrix-multiply kernel (from the matrixmultiply crate). Against OpenBLAS on one core, most
operations take between 0.4 and 1.7 times as long (the README’s benchmark tables); the exception is the general eigenproblem at the largest size, about 1.9 times at .
The reference crate
lib_jsl_linalg_reference (API reference) contains
the textbook algorithms, written to look like the formulas: Gaussian elimination with and without
pivoting, Doolittle LU, Cholesky-Banachiewicz, classical and modified Gram-Schmidt, Householder QR,
power and Rayleigh quotient iteration, cyclic Jacobi, the unshifted and shifted QR algorithm, the
SVD through , and least squares by the normal equations. They are for study, and each entry in
this part contrasts them with the production algorithm: what pivoting, reflections, shifts,
blocking and bidiagonalisation buy. cargo bench -p lib_jsl_linalg_reference races the two.
Testing against SciPy
The references come from SciPy 1.17.1’s scipy.linalg, which calls LAPACK (through OpenBLAS), on random matrices written by book/generator/data/make_linear_algebra_refs.py. Each entry is the largest difference relative to the largest entry of SciPy’s result; factors determined only up to sign are compared in absolute value:
Table 7.1. Linear algebra routines against SciPy.
| computation (SciPy reference) | lib_jsl_linalg | lib_jsl_linalg_reference |
|---|---|---|
solve, 40 x 40 (linalg.solve) | 5e-15 | 2e-14 (pivoted elimination), 2e-14 (Doolittle LU) |
inverse (linalg.inv) | 3e-15 | |
determinant (linalg.det) | 1e-14 | 2e-14 |
Cholesky factor, 30 x 30 (linalg.cholesky) | 2e-16 | 2e-16 |
symmetric eigenvalues, 30 x 30 (linalg.eigh) | 3e-15 | 7e-15 (Jacobi), 1e-14 (shifted QR) |
| their eigenvectors, up to sign | 9e-15 | |
general eigenvalues, 25 x 25, complex pairs (linalg.eigvals) | 2e-15 | |
QR, R factor up to signs, 50 x 20 (linalg.qr) | 7e-16 (Householder), 2e-16 (modified Gram-Schmidt) | |
singular values, 50 x 20 (linalg.svd) | 3e-15 | 4e-15 (through AᵀA) |
| singular vectors, up to sign | 4e-14 | |
least squares, 50 x 20 (linalg.lstsq) | 4e-15 | 3e-15 (QR), 4e-15 (normal equations) |
minimum-norm least squares, rank 12 of 20 (linalg.lstsq) | 3e-15, rank 12 (SciPy 12) | |
Hermitian eigenvalues, complex 20 x 20 (linalg.eigh) | 1e-15 | |
complex solve, 20 x 20 (linalg.solve) | 2e-15 |
Both crates agree with LAPACK to a few units of rounding error on these well-conditioned matrices. Where their algorithms differ in stability, as in the SVD through or the normal equations, the difference shows only on ill-conditioned problems, which the entries of this part examine.
Choosing a method
| Your problem | Use | Entry |
|---|---|---|
| Solve , general square | solve, or factorize for many right-hand sides | Gaussian elimination and LU |
| Solve , symmetric positive definite | cholesky and two triangular solves | Cholesky factorisation |
| Determinant or inverse | det, inv (prefer solving to inverting) | Gaussian elimination and LU |
| An orthonormal basis, or a rank-revealing factorisation | Householder QR (with column pivoting) | QR factorisation |
| Overdetermined, underdetermined or rank-deficient | least_squares | Linear least squares |
| All eigenvalues (and vectors) of a symmetric or Hermitian matrix | eigh, eigvalsh | Symmetric and general eigensolvers |
| All eigenvalues (and vectors) of a general matrix | eig, eigvals | Symmetric and general eigensolvers |
| One eigenpair, near a known value | inverse or Rayleigh quotient iteration | Eigenvalue algorithms |
| Singular values, rank, condition number, low-rank approximation | svd (svd_jacobi for graded matrices) | The singular value decomposition |
| ; discretising | expm | Matrix functions and matrix equations |
| ; Lyapunov equations, Gramians | solve_sylvester, solve_continuous_lyapunov, solve_discrete_lyapunov | Matrix functions and matrix equations |
| Optimal (LQR) gains, steady-state Kalman gains | solve_continuous_are, solve_discrete_are | Matrix functions and matrix equations |
| , , | sqrtm, logm, fractional_matrix_power | Matrix functions, subspaces and norms |
| QR with column pivoting, RQ, Hessenberg or real Schur form; symmetric indefinite ; nearest orthogonal matrix | qr, rq, hessenberg, schur_real, ldl, polar | Further factorisations |
| : symmetric-definite, or general (also with singular ) | eigh_generalized; eig_generalized, qz | Generalised eigenproblems and structured matrices |
| Toeplitz, circulant or banded ; structured test matrices | solve_toeplitz, solve_circulant, solve_banded, solveh_banded; toeplitz, hankel, circulant, hadamard, … | Generalised eigenproblems and structured matrices |
| Angles between subspaces; for a general function | subspace_angles, funm | Further factorisations |
| Pseudoinverse, rank, null space, range, norms, condition numbers | pinv, matrix_rank, null_space, orth, norm, cond | Matrix functions, subspaces and norms |
| Large sparse : direct | sparse::spsolve, sparse::SparseLu | Sparse matrices and iterative solvers |
| Large sparse or least squares: iterative | sparse::cg, minres, gmres, bicgstab, lsqr | Sparse matrices and iterative solvers |
| A few eigenvalues or singular values of a large matrix | sparse::eigsh, sparse::svds, sparse::eigsh_shift_invert (near a shift) | Sparse matrices and iterative solvers |
| A few eigenvalues of a large nonsymmetric matrix | sparse::eigs, sparse::eigs_shift_invert (near a shift) | Sparse matrices and iterative solvers |
| Precondition a large nonsymmetric system | sparse::Ilu::ilu0, sparse::Ilu::ilut | Sparse matrices and iterative solvers |
| for a large sparse | sparse::expm_multiply | Sparse matrices and iterative solvers |