lib_jsl_linalg_reference/mod.rs
1//! Readable reference implementations of the classical dense linear algebra algorithms, kept for
2//! study and as benchmark baselines.
3//!
4//! **If you just want to solve a system or decompose a matrix, use `lib_jsl_linalg` instead.**
5//! The routines here work on real `f64` matrices, favour the shape of the textbook formulas over
6//! speed, and several are included precisely to show *why* production code does something else:
7//!
8//! | Module | Algorithms | The lesson |
9//! |---|---|---|
10//! | [`gaussian_elimination`] | Elimination without and with partial pivoting | Pivoting is what makes elimination stable |
11//! | [`lu`] | Doolittle LU, triangular solves, determinant, inverse | Factor once, solve many times |
12//! | [`cholesky`] | Cholesky-Banachiewicz | Symmetry halves the work and needs no pivoting |
13//! | [`qr`] | Classical and modified Gram-Schmidt, Householder | Orthogonality is lost by Gram-Schmidt and kept by reflections |
14//! | [`power_iteration`] | Power, inverse and Rayleigh-quotient iteration | Repeated multiplication finds dominant eigenvectors; shifts make it fast |
15//! | [`jacobi_eigenvalue`] | Cyclic Jacobi rotations | Annihilating off-diagonal entries one at a time converges quadratically |
16//! | [`qr_algorithm`] | Unshifted and shifted QR iteration | The QR algorithm is subspace iteration in disguise; shifts accelerate it |
17//! | [`naive_svd`] | SVD from the eigenvalues of `AᵀA` | Forming `AᵀA` squares the condition number |
18//! | [`least_squares`] | Normal equations versus QR | The same trade-off for least squares |
19//!
20//! Run `cargo bench -p lib_jsl_linalg_reference` to compare them with the production crate.
21//!
22//! See the book chapter [Dense linear algebra](https://jwkunz.github.io/lib_jsl/book/linear_algebra/index.html).
23
24pub mod cholesky;
25pub mod gaussian_elimination;
26pub mod jacobi_eigenvalue;
27pub mod least_squares;
28pub mod lu;
29pub mod naive_svd;
30pub mod power_iteration;
31pub mod qr;
32pub mod qr_algorithm;
33
34use ndarray::Array2;
35
36/// Why a reference routine failed.
37pub type Error = &'static str;
38
39/// The identity matrix of order `n`.
40pub(crate) fn identity(n: usize) -> Array2<f64> {
41 Array2::from_shape_fn((n, n), |(i, j)| if i == j { 1.0 } else { 0.0 })
42}
43
44/// A deterministic pseudo-random matrix with entries in [−1, 1), for tests and benchmarks.
45pub fn random_matrix(rows: usize, cols: usize, seed: u64) -> Array2<f64> {
46 let mut s = seed;
47 Array2::from_shape_fn((rows, cols), |_| {
48 s = s.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
49 (s >> 11) as f64 / (1u64 << 53) as f64 * 2.0 - 1.0
50 })
51}
52
53/// A random symmetric matrix `G + Gᵀ`.
54pub fn random_symmetric(n: usize, seed: u64) -> Array2<f64> {
55 let g = random_matrix(n, n, seed);
56 &g + &g.t()
57}
58
59/// A random symmetric positive-definite matrix `G Gᵀ + n I`.
60pub fn random_positive_definite(n: usize, seed: u64) -> Array2<f64> {
61 let g = random_matrix(n, n, seed);
62 g.dot(&g.t()) + identity(n) * n as f64
63}
64
65/// The off-diagonal Frobenius norm, used to measure how far a matrix is from diagonal.
66pub fn off_diagonal_norm(a: &Array2<f64>) -> f64 {
67 a.indexed_iter().filter(|((i, j), _)| i != j).map(|(_, v)| v * v).sum::<f64>().sqrt()
68}