sparse
On this page
Sparse matrices on core's Array: compressed rows (Csr), compressed
columns (Csc) and triplets (Coo), their products and conversions,
Krylov solvers (cg, gmres, bicgstab) with preconditioners
(Jacobi, ilu0, ic0) that also take matrix-free operators, and
direct factorizations (cholesky, lu) after a fill-reducing order.
Values are f64. Bad input throws a SparseError that says what to
call instead; nothing panics except the operators, which cannot
throw and panic on a shape mismatch as core's do.
Pure Nabi, so every target, wasm included, gets the same results.
Modules
opslinalg's operators on sparse matrices (featureops, on by default): a @ x, a ⊡ x product with a vector, a dense matrix or a sparse matrix a \ b, a ⧵ b solve a x = b by sparse LU (a Result) a† transpose, as the other form sharing the arrays The tokens are linalg's (linalg::ops);use linalg::ops::*brings them into scope.
Structs
CooSource
pub struct CooA sparse matrix as (row, col, value) triplets in any order, with
repeats allowed (they add up when converted). Push entries, then
convert once with to_csr or to_csc.
use sparse::Coo;
let c = Coo::new([3, 3]);
for i in 0..3 { try c.push(i, i, 2.0); }
try c.push(0, 1, -1.0);
try c.push(0, 1, -1.0);
let a = c.to_csr();
assert(a.get(0, 1) == -2.0 && a.nnz() == 4);
An empty
shapematrix.
Sourcepub fn from_triplets(§shape: [usize; 2], rows: List<usize>, cols: List<usize>, vals: List<f64>) throws (SparseError) -> Coo The triplets
(rows[p], cols[p], vals[p]), checked againstshape.nabilet c = try sparse::Coo::from_triplets([2, 2], [0, 1], [1, 0], [1.0, 2.0]); assert(c.nnz() == 2 && c.to_dense()[1, 0] == 2.0);
Sourcepub fn push(§self, i: usize, j: usize, v: f64) throws (SparseError) Add
vat(i, j)(on top of what is there).[rows, cols].The number of triplets (repeats counted).
The row of each triplet.
The column of each triplet.
The value of each triplet.
Compressed rows, repeats summed. O(nnz + rows + cols).
Compressed columns, repeats summed.
The dense matrix, repeats summed.
CscSource
pub struct CscA sparse matrix in compressed sparse column form: column j's row
indices are indices()[indptr()[j]..indptr()[j + 1]], rising, with
their values in data(). The transpose of a Csr with the same
arrays.
use sparse::Csc;
let a = try Csc::from_triplets([2, 3], [0, 1], [2, 0], [1.0, 2.0]);
assert(a.get(0, 2) == 1.0 && a.indptr() == [0, 1, 1, 2]);
assert((try a.matvec([1, 1, 1])).to_list() == [1.0, 2.0]);
Sourcepub fn new(§shape: [usize; 2], indptr: List<usize>, indices: List<usize>, data: List<f64>) throws (SparseError) -> Csc The matrix of
shapefrom its compressed arrays (seeCsc). Unsorted columns and repeated rows are sorted and summed.
Sourcepub fn from_triplets(§shape: [usize; 2], rows: List<usize>, cols: List<usize>, vals: List<f64>) throws (SparseError) -> Csc The matrix with
vals[p]at(rows[p], cols[p]); repeated positions are summed.The entries of
mwith|x| > drop.[rows, cols].The number of stored entries.
The column pointers:
cols + 1offsets intoindicesanddata.The row index of each stored entry, column by column.
The value of each stored entry, column by column.
The element at
(i, j): 0 where nothing is stored. Panics outside the shape.
Sourcepub fn matvec<X: AsVector<f64>>(§self, x: X) throws (SparseError) -> Vector<f64> A x. O(nnz).
Sourcepub fn rmatvec<X: AsVector<f64>>(§self, x: X) throws (SparseError) -> Vector<f64> A^T x. O(nnz).
Sourcepub fn matmul(§self, b: Csc) throws (SparseError) -> Csc A Bfor a sparseB, as(B^T A^T)^Ton the row kernels.nabilet a = sparse::eye(2).to_csc().scale(2.0); assert((try a.matmul(a)).get(0, 0) == 4.0);
Sourcepub fn add(§self, b: Csc) throws (SparseError) -> Csc A + B.
Sourcepub fn sub(§self, b: Csc) throws (SparseError) -> Csc A - B.s A.The transpose as a
Csrsharing these arrays: O(1).The transpose as a new
Csc. O(nnz).The same matrix in compressed sparse row form. O(nnz).
The entries as triplets.
The dense matrix.
The main diagonal.
Sourcepub fn cols(§self, r: Range<usize>) throws (SparseError) -> Csc Columns
r, as a new matrix.nabilet a = sparse::eye(4).to_csc(); assert((try a.cols(1..2)).shape() == [4, 1]);
Sourcepub fn rows(§self, r: Range<usize>) throws (SparseError) -> Csc Rows
r, as a new matrix.
Trait implementations 2
impl PartialEq for Cscimpl Debug for Csc
CsrSource
pub struct CsrA sparse matrix in compressed sparse row form: row i's column
indices are indices()[indptr()[i]..indptr()[i + 1]], rising, with
their values in data(). Build one with from_triplets, from_dense,
eye, diags or Coo::to_csr; A x costs O(nnz).
use sparse::Csr;
let a = try Csr::from_triplets([2, 3], [0, 1, 1, 0], [0, 2, 2, 1], [1.0, 2.0, 3.0, 4.0]);
assert(a.nnz() == 3 && a.get(1, 2) == 5.0 && a.get(1, 0) == 0.0);
let y = try a.matvec([1, 1, 1]);
assert(y.to_list() == [5.0, 5.0]);
Sourcepub fn new(§shape: [usize; 2], indptr: List<usize>, indices: List<usize>, data: List<f64>) throws (SparseError) -> Csr The matrix of
shapefrom its compressed arrays (seeCsr). Unsorted rows and repeated columns are sorted and summed; arrays that do not describe a matrix throwBadStructure.nabiuse sparse::Csr; let a = try Csr::new([2, 2], [0, 1, 2], [1, 0], [5.0, 6.0]); assert(a.get(0, 1) == 5.0 && a.get(1, 0) == 6.0);
Sourcepub fn from_triplets(§shape: [usize; 2], rows: List<usize>, cols: List<usize>, vals: List<f64>) throws (SparseError) -> Csr The matrix with
vals[p]at(rows[p], cols[p]); repeated positions are summed (the finite-element assembly rule).nabiuse sparse::Csr; let a = try Csr::from_triplets([2, 2], [0, 0, 1], [0, 0, 1], [1.0, 2.0, 4.0]); assert(a.get(0, 0) == 3.0 && a.nnz() == 2);The entries of
mwith|x| > drop(default: every nonzero).nabiuse sparse::Csr; use core::array::Matrix; let m: Matrix<f64> = [[1, 0], [0, 1e-20]]; assert(Csr::from_dense(m).nnz() == 2); assert(Csr::from_dense(m, drop: 1e-12).nnz() == 1);[rows, cols].The number of stored entries.
The row pointers:
rows + 1offsets intoindicesanddata.The column index of each stored entry, row by row.
The value of each stored entry, row by row.
The element at
(i, j): 0 where nothing is stored. O(log of the row's length). Panics outside the shape, as indexing does.nabilet a = sparse::eye(3); assert(a.get(2, 2) == 1.0 && a.get(0, 2) == 0.0);
Sourcepub fn matvec<X: AsVector<f64>>(§self, x: X) throws (SparseError) -> Vector<f64> A x. O(nnz).nabilet a = try sparse::diags([[2.0, 2.0], [1.0]], [0, 1], [2, 2]); assert((try a.matvec([1, 1])).to_list() == [3.0, 2.0]);
Sourcepub fn rmatvec<X: AsVector<f64>>(§self, x: X) throws (SparseError) -> Vector<f64> A^T x. O(nnz).nabilet a = try sparse::diags([[2.0, 2.0], [1.0]], [0, 1], [2, 2]); assert((try a.rmatvec([1, 1])).to_list() == [2.0, 3.0]);
Sourcepub fn matmul_dense(§self, b: Matrix<f64>) throws (SparseError) -> Matrix<f64> A Bfor a denseB: one sparse row timesBper row of the answer. O(nnz * B's columns).nabiuse core::array::Matrix; let b: Matrix<f64> = [[1, 2], [3, 4]]; assert(try sparse::eye(2).matmul_dense(b) == b);
Sourcepub fn matmul(§self, b: Csr) throws (SparseError) -> Csr A Bfor a sparseB(Gustavson's algorithm).nabilet a = try sparse::diags([[1.0, 2.0]], [0], [2, 2]); let b = try a.matmul(a); assert(b.get(1, 1) == 4.0 && b.nnz() == 2);
Sourcepub fn add(§self, b: Csr) throws (SparseError) -> Csr A + B.nabilet a = sparse::eye(2); assert((try a.add(a)).get(1, 1) == 2.0);
Sourcepub fn sub(§self, b: Csr) throws (SparseError) -> Csr A - B.s A.nabiassert(sparse::eye(2).scale(3.0).get(0, 0) == 3.0);The transpose as a
Cscsharing these arrays: O(1).nabilet a = try sparse::Csr::from_triplets([2, 3], [0], [2], [7.0]); assert(a.t().shape() == [3, 2] && a.t().get(2, 0) == 7.0);The transpose as a new
Csr. O(nnz).The same matrix in compressed sparse column form. O(nnz).
nabilet a = sparse::eye(3); assert(a.to_csc().get(1, 1) == 1.0);The entries as triplets.
The dense matrix.
nabilet d = sparse::eye(2).to_dense(); assert(d[1, 1] == 1.0 && d[0, 1] == 0.0);The main diagonal.
Sourcepub fn rows(§self, r: Range<usize>) throws (SparseError) -> Csr Rows
r, as a new matrix.nabilet a = sparse::eye(4); let b = try a.rows(1..3); assert(b.shape() == [2, 4] && b.get(0, 1) == 1.0);
Sourcepub fn cols(§self, r: Range<usize>) throws (SparseError) -> Csr Columns
r, as a new matrix.nabilet a = sparse::eye(4); let b = try a.cols(2..4); assert(b.shape() == [4, 2] && b.get(3, 1) == 1.0);
Trait implementations 2
impl PartialEq for Csrimpl Debug for Csr
CholeskySource
pub struct CholeskyP A P^T = L L^T for a sparse symmetric positive definite A. See
cholesky.
Sourcepub fn solve<X: AsVector<f64>>(§self, b: X) throws (SparseError) -> Vector<f64> xwithA x = b: two triangular solves, O(|L|).The factor
L(lower triangular, ofP A P^T).The permutation: row and column
iofP A P^Tareperm()[i]ofA.The number of stored entries of
L(the fill shows here).ln det A,2 sum ln L_ii: no overflow for large matrices.nabilet a = try sparse::diags([[2.0, 8.0]], [0], [2, 2]); assert(((try sparse::cholesky(a)).logdet() - 16.0.ln()).abs() < 1e-14);
Trait implementations 1
impl Preconditioner for Cholesky
LuSource
pub struct LuP A Q = L U for a sparse square nonsingular A. See lu.
Sourcepub fn solve<X: AsVector<f64>>(§self, b: X) throws (SparseError) -> Vector<f64> xwithA x = b: two triangular solves, O(|L| + |U|).L: unit lower triangular, ofP A Q.U: upper triangular, ofP A Q.The row order: row
kofP Ais rowp()[k]ofA.The column order: column
kofA Qis columnq()[k]ofA.The number of stored entries of
LandUtogether.det A: the product ofU's diagonal and the signs of both permutations.
Trait implementations 1
impl Preconditioner for Lu
SolutionSource
pub struct SolutionWhat an iterative solver found: x, the steps it took (gmres:
inner steps over all restarts) and the final residual norm ||b - A x||, computed afresh.
let a = try sparse::diags([[2.0], [-1.0], [-1.0]], [0, 1, -1], [10, 10]);
let s = try sparse::cg(a, List::filled(10, 1.0));
assert(s.iterations <= 10 && s.residual <= 1e-8 * 10.0.sqrt());
println(s.x);
The solution.
The number of steps taken.
||b - A x||.
FnOperatorSource
pub struct FnOperatorA matrix-free n x n operator: f(x) is A x. See operator.
Trait implementations 1
impl Operator for FnOperator
Ic0Source
pub struct Ic0A ~ L L^T on the pattern of the lower triangle of a symmetric A.
See ic0.
Trait implementations 1
impl Preconditioner for Ic0
IdentitySource
pub struct IdentityNo preconditioning: M = I. The solvers' default.
Trait implementations 1
impl Preconditioner for Identity
Ilu0Source
pub struct Ilu0A ~ L U on the pattern of A (L unit lower, both stored in one
CSR). See ilu0.
Trait implementations 1
impl Preconditioner for Ilu0
JacobiSource
pub struct JacobiM = diag(A): divides by the diagonal. Cheap; helps when rows are
scaled very differently.
Sourcepub fn new<S: Sparse>(§a: S) throws (SparseError) -> Jacobi The Jacobi preconditioner of a square
a; a zero on the diagonal throwsSingular.
Trait implementations 1
impl Preconditioner for Jacobi
Enums
SparseErrorSource
pub enum SparseErrorWhat went wrong in a sparse call.
use sparse::SparseError;
let a = try sparse::diags([[1.0, 0.0]], [0], [2, 2]);
let e = try {
_ = try sparse::lu(a);
SparseError::NotFinite
} catch e: SparseError { e };
assert(e == SparseError::Singular { column: 1 });
assert(f"{e}".contains("lstsq"));
The operands' shapes do not fit;
whatnames both.A row or column index is outside the shape.
Compressed arrays that do not describe a matrix:
whatsays which rule they break.A square matrix was needed.
A symmetric matrix was needed;
(row, col)differs from its mirror.No usable pivot in
column(0-based, in elimination order): the matrix is singular, or numerically so.A pivot that is not positive in
column(0-based, in elimination order): the matrix is not positive definite.An iterative solver stopped after
iterationssteps with the residual normresidualabove the tolerance.An iterative solver divided by zero after
iterationssteps;whatnames the quantity.The input holds NaN or an infinity.
Trait implementations 2
impl Display for SparseErrorimpl Error for SparseError
OrderSource
pub enum OrderHow a direct factorization orders the unknowns before eliminating.
use sparse::Order;
let a = try sparse::diags([[4.0], [-1.0], [-1.0]], [0, 1, -1], [5, 5]);
let f = try sparse::cholesky(a, order: Order::Natural);
assert(f.perm() == [0, 1, 2, 3, 4]);
As given.
Reverse Cuthill-McKee: a small bandwidth, quick to compute.
Minimum degree: the least fill of the three on most problems (the default).
Traits
SparseSource
pub trait SparseA sparse matrix in any form: Csr, Csc or Coo. A function that
takes Sparse converts once to the form it works in (free when it is
already that form).
use sparse::Sparse;
let a = sparse::eye(2);
assert(a.as_csc().get(1, 1) == 1.0 && a.to_coo().as_csr() == a);
Compressed rows.
Compressed columns.
OperatorSource
pub trait OperatorA linear map x -> A x of dims() ([rows, cols]): Csr, Csc,
a dense Matrix<f64>, or a function wrapped by operator.
use sparse::Operator;
use core::array::Vector;
let a = sparse::eye(2).scale(2.0);
let x: Vector<f64> = [1, 3];
assert(a.dims() == [2, 2] && a.apply(x).to_list() == [2.0, 6.0]);
[rows, cols].A x, forxof lengthcols.
PreconditionerSource
pub trait Preconditionerr -> M^-1 r for some M close to A and cheap to solve with:
Identity (none), Jacobi, Ilu0, Ic0, or an exact Cholesky /
Lu of a nearby matrix.
use sparse::Preconditioner;
use core::array::Vector;
let a = try sparse::diags([[4.0, 2.0]], [0], [2, 2]);
let m = try sparse::Jacobi::new(a);
let r: Vector<f64> = [4, 4];
assert(m.precondition(r).to_list() == [1.0, 2.0]);
M^-1 r.
Functions
diagsSource
pub fn diags(diagonals: List<List<f64>>, offsets: List<i64>, shape: [usize; 2]) throws (SparseError) -> Csr The shape matrix with diagonals[d] on diagonal offsets[d] (0 the
main one, k > 0 above it, k < 0 below), as SciPy's diags. Each
diagonal has its full length (min(m, n - k) for k >= 0, min(m + k, n) below) or one value, repeated.
// The 1-d Laplacian: 2 on the diagonal, -1 next to it.
let a = try sparse::diags([[2.0], [-1.0], [-1.0]], [0, 1, -1], [4, 4]);
assert(a.nnz() == 10 && a.get(3, 2) == -1.0 && a.get(0, 0) == 2.0);
eyeSource
pub fn eye(n: usize) -> Csr The n x n identity.
let i = sparse::eye(3);
assert(i.nnz() == 3 && i.get(1, 1) == 1.0);
kronSource
pub fn kron(a: Csr, b: Csr) -> Csr The Kronecker product A ⊗ B: block (i, j) is a[i, j] B. The
2-d Laplacian is kron(I, T) + kron(T, I) for the 1-d one T.
let t = try sparse::diags([[2.0], [-1.0], [-1.0]], [0, 1, -1], [3, 3]);
let i = sparse::eye(3);
let l = try sparse::kron(i, t).add(sparse::kron(t, i));
assert(l.shape() == [9, 9] && l.get(4, 4) == 4.0 && l.get(4, 1) == -1.0);
choleskySource
pub fn cholesky<S: Sparse>(a: S, order: Order = Order::MinDegree) throws (SparseError) -> Cholesky The sparse Cholesky factorization of a symmetric positive definite
a after the fill-reducing order (default minimum degree). A matrix
that is not symmetric throws NotSymmetric, one that is not positive
definite NotPositiveDefinite. Cost O(flops of L), about n^1.5 on a
2-d grid with minimum degree.
use core::array::Vector;
let t = try sparse::diags([[4.0], [-1.0], [-1.0]], [0, 1, -1], [10, 10]);
let a = try sparse::kron(sparse::eye(10), t).add(sparse::kron(t, sparse::eye(10)));
let f = try sparse::cholesky(a);
let x = try f.solve(List::filled(100, 1.0));
assert((try a.matvec(x)).all_close(Vector::<f64>::ones([100])));
luSource
pub fn lu<S: Sparse>(a: S, order: Order = Order::MinDegree, pivot_tol: f64 = 0.1) throws (SparseError) -> Lu The sparse LU factorization of a square a: the columns in order
(default minimum degree on A + A^T), rows by threshold partial
pivoting. A diagonal entry is kept as the pivot while it is at least
pivot_tol times the column's largest candidate (1.0: plain partial
pivoting, smaller: sparser factors at some cost in stability). No
nonzero pivot throws Singular.
use core::array::Vector;
let a = try sparse::Csr::from_triplets([3, 3], [0, 0, 1, 2, 2], [1, 2, 0, 0, 2], [2.0, 1.0, 3.0, 1.0, 4.0]);
let f = try sparse::lu(a);
let x = try f.solve([3, 3, 5]);
assert(x.all_close(Vector::<f64>::from([1.0, 1.0, 1.0])));
assert((f.det() - (-24.0)).abs() < 1e-12);
bicgstabSource
pub fn bicgstab<A: Operator, X: AsVector<f64>>(a: A, b: X, pre: any Preconditioner = Identity {}, x0: Vector<f64> | none = none, rtol: f64 = 1e-8, atol: f64 = 0.0, max_iter: usize | none = none) throws (SparseError) -> Solution Solve A x = b for any square nonsingular A by BiCGSTAB: short
recurrences, so memory O(n) whatever the step count; each step costs
two A x and two pre. Convergence is not monotone and it can break
down (a zero inner product): that throws Breakdown, after which
gmres is the safe choice.
let a = try sparse::diags([[3.0], [-1.5], [-0.5]], [0, 1, -1], [50, 50]);
let s = try sparse::bicgstab(a, List::filled(50, 1.0), pre: try sparse::ilu0(a));
assert(s.residual <= 1e-8 * 50.0.sqrt());
cgSource
pub fn cg<A: Operator, X: AsVector<f64>>(a: A, b: X, pre: any Preconditioner = Identity {}, x0: Vector<f64> | none = none, rtol: f64 = 1e-8, atol: f64 = 0.0, max_iter: usize | none = none) throws (SparseError) -> Solution Solve A x = b for a symmetric positive definite A by conjugate
gradients, with an SPD preconditioner pre (Jacobi, ic0, ...).
Each step costs one A x, one pre and O(n); in exact arithmetic it
ends within n steps, in practice after about sqrt(cond(A))
unpreconditioned ones. A direction with p^T A p <= 0 shows A is
not positive definite and throws NotPositiveDefinite.
use core::array::Vector;
let a = try sparse::diags([[4.0], [1.0], [1.0]], [0, 1, -1], [3, 3]);
let s = try sparse::cg(a, [1, 2, 3]);
let b: Vector<f64> = [1, 2, 3];
assert((try a.matvec(s.x)).all_close(b, abs: 1e-8));
gmresSource
pub fn gmres<A: Operator, X: AsVector<f64>>(a: A, b: X, pre: any Preconditioner = Identity {}, x0: Vector<f64> | none = none, rtol: f64 = 1e-8, atol: f64 = 0.0, restart: usize = 30, max_iter: usize | none = none) throws (SparseError) -> Solution Solve A x = b for any square nonsingular A by GMRES(restart):
minimizes ||b - A x|| over a Krylov space of up to restart
vectors, then starts again from the best x. Never breaks down;
memory restart * n, step k costs O(k n) plus one A x and one
pre. Right preconditioning, so the residual tested is the true one.
// Convection-diffusion: not symmetric, so not for cg.
let a = try sparse::diags([[3.0], [-1.5], [-0.5]], [0, 1, -1], [50, 50]);
let s = try sparse::gmres(a, List::filled(50, 1.0), restart: 10);
assert(s.residual <= 1e-8 * 50.0.sqrt());
operatorSource
pub fn operator(n: usize, f: fn(Vector<f64>) -> Vector<f64>) -> FnOperator The n x n operator x -> f(x), for the iterative solvers when A
is cheaper to apply than to store.
use core::array::Vector;
// The 1-d Laplacian as a stencil.
let n = 50;
let lap = sparse::operator(n, |x: Vector<f64>| -> Vector<f64> {
Vector::<f64>::from_fn([n], |i: [usize; 1]| -> f64 {
let k = i[0];
2.0 * x[k] - (if k > 0 { x[k - 1] } else { 0.0 }) - (if k + 1 < n { x[k + 1] } else { 0.0 })
})
});
let s = try sparse::cg(lap, Vector::<f64>::ones([n]));
assert(s.residual < 1e-6);
min_degreeSource
pub fn min_degree<S: Sparse>(a: S) throws (SparseError) -> List<usize> The minimum degree order of a square a: eliminate a vertex of the
fewest neighbours in the elimination graph, then join its neighbours
(Tinney and Walker 1967, on the explicit graph, without AMD's
approximate degrees or supervariables: exact, slower on big 3-d
problems).
let t = try sparse::diags([[4.0], [-1.0], [-1.0]], [0, 1, -1], [4, 4]);
let a = try sparse::kron(sparse::eye(4), t).add(sparse::kron(t, sparse::eye(4)));
let p = try sparse::min_degree(a);
assert(p.len() == 16 && p[0] == 0); // a corner has the fewest neighbours
rcmSource
pub fn rcm<S: Sparse>(a: S) throws (SparseError) -> List<usize> The reverse Cuthill-McKee order of a square a: breadth-first from a
pseudo-peripheral vertex (George and Liu 1979) of each component,
neighbours by rising degree, then reversed. O(nnz log).
let a = try sparse::diags([[2.0], [-1.0], [-1.0]], [0, 3, -3], [6, 6]);
let p = try sparse::rcm(a);
assert(p.len() == 6);
ic0Source
pub fn ic0<S: Sparse>(a: S) throws (SparseError) -> Ic0 The IC(0) preconditioner of a symmetric positive definite a (only
the lower triangle is read), for cg. A pivot that is not positive
throws NotPositiveDefinite; IC(0) can fail on an SPD matrix that is
not diagonally dominant, so then use Jacobi or ilu0.
let t = try sparse::diags([[2.0], [-1.0], [-1.0]], [0, 1, -1], [200, 200]);
let plain = try sparse::cg(t, List::filled(200, 1.0));
let pre = try sparse::cg(t, List::filled(200, 1.0), pre: try sparse::ic0(t));
assert(pre.iterations < plain.iterations);
ilu0Source
pub fn ilu0<S: Sparse>(a: S) throws (SparseError) -> Ilu0 The ILU(0) preconditioner of a square a: Gaussian elimination that
drops every fill-in outside a's pattern (IKJ order). A zero or
missing diagonal pivot throws Singular. O(sum of row lengths
squared).
let t = try sparse::diags([[4.0], [-1.0], [-1.0]], [0, 1, -1], [100, 100]);
let s = try sparse::gmres(t, List::filled(100, 1.0), pre: try sparse::ilu0(t));
assert(s.iterations <= 2); // ILU(0) of a tridiagonal matrix is exact