linalg
Dense linear algebra on core's Array: products, factorizations as
types (Lu, Cholesky, Ldlt, Qr, Svd, Eigh, Eig, Schur),
solvers and matrix functions. Every routine takes Matrix<T> /
Vector<T> (Array<T, 2> / Array<T, 1>, views included) for T in
f64, f32, Complex<f64>, Complex<f32> (Scalar), or the real two
(Real) where the algorithm is real only. Bad input throws a
LinalgError that says what to call instead; nothing panics except
the operators of ops, which cannot throw and panic on a shape
mismatch as core's broadcast operators do.
Pure Nabi: no BLAS or LAPACK, so every target, wasm included, gets the same results.
#Tutorial
Most numerical work comes down to matrices: a system of equations, a
least-squares fit, the modes of a structure. linalg handles dense
matrices; sparse
handles the large ones that are mostly zeros. Both work on core's
Matrix<f64> and Vector<f64> (Arrays and
numerics),
so their results feed straight into everything else.
#Matrices and vectors
A Matrix<f64> is a two-dimensional array; write one as nested lists
with the type given. With the ops feature (on by default), @ is the
matrix product, \ solves and † is the transpose (the conjugate
transpose for complex matrices). The dot operators (.+, .*) work
element by element, as in core.
use core::array::{Matrix, Vector};
use linalg::ops::*;
let a: Matrix<f64> = [[2, 1], [1, 3]];
let v: Vector<f64> = [1, 2];
println(a @ v);
println(a† @ a);
println(a .* 10.0);
println(linalg::eye::<f64>(2));
[4.0, 7.0]
[[5.0, 5.0], [5.0, 10.0]]
[[20.0, 10.0], [10.0, 30.0]]
[[1.0, 0.0], [0.0, 1.0]]
a @ v panics when the shapes do not fit, the way .+ does;
linalg::matmul(a, b) is the same product as a call that throws a
LinalgError instead.
#Solve a system
Three unknowns, three equations: a x = b. linalg::solve factors a
(LU with partial pivoting) and solves; a \ b is the same as an
operator, returning a result to unwrap with get. The residual
a x - b shows how close the answer is.
use core::array::{Matrix, Vector};
use linalg::ops::*;
let a: Matrix<f64> = [[4, 1, 0], [1, 3, 1], [0, 1, 2]];
let b: Vector<f64> = [1, 2, 3];
let x = try linalg::solve(a, b);
println(f"x = {x:.4}");
println(f"residual {linalg::norm(a @ x .- b) < 1e-12}");
let same = try (a \ b).get();
println(same.all_close(x));
x = [0.2222, 0.1111, 1.4444]
residual true
true
To solve with the same matrix many times, factor it once:
linalg::lu(a) (any square matrix) or linalg::cholesky(a) (symmetric
positive definite, twice as fast) return a factorization with solve,
det and inv.
use core::array::{Matrix, Vector};
let a: Matrix<f64> = [[4, 1, 0], [1, 3, 1], [0, 1, 2]];
let c = try linalg::cholesky(a);
let rhs: List<Vector<f64>> = [[1, 0, 0], [0, 1, 0]];
for b in rhs {
println(f"{try c.solve(b):.4}");
}
println(f"det {(try linalg::lu(a)).det():.1}");
[0.2778, -0.1111, 0.0556]
[-0.1111, 0.4444, -0.2222]
det 18.0
#Fit a line by least squares
More equations than unknowns: there is no exact solution, so least
squares finds the one that makes the squared residuals smallest. For a
line y = c0 + c1 t, each data point is a row [1, t]. linalg::lstsq
solves it through a QR factorization, which keeps its accuracy where the
normal equations aᵀa x = aᵀy lose digits.
use core::array::{Matrix, Vector};
let t: Vector<f64> = [0, 1, 2, 3, 4];
let y: Vector<f64> = [1.1, 2.9, 5.2, 7.1, 8.8];
let a: Matrix<f64> = Matrix::from_fn([5, 2], |i: [usize; 2]| -> f64 { if i[1] == 0 { 1.0 } else { t[i[0]] } });
let c = try linalg::lstsq(a, y);
println(f"y = {c[0]:.3} + {c[1]:.3} t");
y = 1.100 + 1.960 t
stats::ols does the same fit and adds standard errors and p-values for
each coefficient
(stats).
#Eigenvalues and the SVD
linalg::eigh takes a symmetric matrix and returns its eigenvalues in
rising order with orthonormal eigenvectors as columns. Here it finds the
natural frequencies of three masses on springs: the eigenvalues of the
stiffness matrix are the squared frequencies.
use core::array::Matrix;
let k: Matrix<f64> = [[2, -1, 0], [-1, 2, -1], [0, -1, 2]];
let e = try linalg::eigh(k);
println(f"squared frequencies {e.values():.4}");
println(f"frequencies {e.values().map(|v: f64| -> f64 { v.sqrt() }):.4}");
squared frequencies [0.5858, 2.0000, 3.4142]
frequencies [0.7654, 1.4142, 1.8478]
The singular value decomposition works on any matrix. Its singular
values show the rank and how well conditioned a matrix is: the ratio of
the largest to the smallest is the condition number, and linalg::cond
computes it.
use core::array::Matrix;
let m: Matrix<f64> = [[1, 2], [2, 4.001]];
let s = try linalg::svd(m);
println(f"singular values {s.s():.5}");
println(f"condition number {(try linalg::cond(m)):.0}");
singular values [5.00080, 0.00020]
condition number 25008
A condition number of about 2.5 x 10<sup>4</sup> means a solve can lose
more than four of the sixteen digits a f64 carries: the two rows are
almost the same equation.
#What next
Modules
factorThe factorizations, each a type withsolve/det/invwhere they make sense; build them with the functions at the package root (linalg::lu(a),linalg::svd(a), ...).opsOperators for matrix code (featureops, on by default): a @ b, a ⊡ b matrix product (level of*) a ⊗ b Kronecker product; outer product of two vectors (*) a \ b, a ⧵ b solve a x = b, MATLAB's left division (/) a† adjoint, the conjugate transpose (postfix, level of?) Every operator also has a named form (linalg::matmul(a, b), ...).scalarElement types:Scalaris what every routine needs (f32, f64 and their complex forms),Realadds ordering and the float functions for the iterative routines (SVD, eigen) and for norms.
Re-exports
factor::cholesky::{Cholesky, Ldlt}Source
pub use factor::cholesky::{Cholesky, Ldlt}factor::eig::{Eig, Schur}Source
pub use factor::eig::{Eig, Schur}factor::eigh::EighSource
pub use factor::eigh::EighEighA = V diag(w) V^Tof a real symmetric matrix: eigenvalueswrising, orthonormal eigenvectors as the columns ofV.
factor::lu::LuSource
pub use factor::lu::LuLuP A = L Uof a square matrix:Lunit lower,Uupper,Pa row permutation.
factor::qr::QrSource
pub use factor::qr::QrQrA P = Q Rof an m x n matrix:Qunitary (orthogonal for real input),Rupper triangular,Pa column permutation (the identity unless built withqr_pivoted).
factor::svd::SvdSource
pub use factor::svd::SvdSvdA = U diag(s) V^Tof a real m x n matrix:sfalls (k = min(m, n) values),UandVhave orthonormal columns.
scalar::{Real, Scalar}Source
pub use scalar::{Real, Scalar}Enums
LinalgErrorSource
pub enum LinalgErrorWhat went wrong in a linear-algebra call.
use linalg::LinalgError;
use core::array::Matrix;
let a: Matrix<f64> = [[1, 2], [2, 4]];
let e = try {
_ = try linalg::inv(a);
LinalgError::NotFinite
} catch e: LinalgError { e };
assert(e == LinalgError::Singular { pivot: 1 });
assert(f"{e}".contains("pinv"));
A zero pivot: the matrix is singular.
pivotis its 0-based row.A square matrix was needed.
The operands' shapes do not fit;
whatnames both.Cholesky met a leading minor that is not positive;
minoris its order (1-based).An iteration did not converge in
iterationssteps.The input holds NaN or an infinity.
Trait implementations 2
impl Display for LinalgErrorimpl Error for LinalgError
NormSource
pub enum NormWhich norm: One (sum of |x|; for a matrix the largest column sum),
Two (Euclidean; for a matrix the largest singular value), Inf
(largest |x|; for a matrix the largest row sum), Fro (Frobenius:
the 2-norm of all entries).
use core::array::Matrix;
use linalg::Norm;
let a: Matrix<f64> = [[3, 0], [0, 4]];
assert(try linalg::matrix_norm(a, Norm::Two) == 4.0);
assert(try linalg::matrix_norm(a, Norm::Fro) == 5.0);
TriangleSource
pub enum TriangleWhich triangle of a matrix holds the data.
use core::array::{Matrix, Vector};
use linalg::Triangle;
let u: Matrix<f64> = [[1, 1], [0, 2]];
let b: Vector<f64> = [3, 4];
assert(try linalg::solve_triangular(u, b, Triangle::Upper) == Vector::<f64>::from([1.0, 2.0]));
Traits
AsVectorSource
pub trait AsVector<T>Something that reads as a Vector<T>: a Vector<T> itself, a
List<T> or a [T; K]; for T = f64 also vectors, lists and fixed
arrays of f32, i64, i32 and usize, and an i64 range; for
T = Complex<f64> real f64 data. A Vector<T> and a List<T> are
shared, not copied (a function taking AsVector never writes to its
input); every other form is a converted copy.
Take it as a bound to accept all of them with one signature. Give
the result its type (let v: Vector<f64> = ..): a list of f32
reads as a Vector<f32> too, and the annotation picks the one
meant.
use linalg::AsVector;
use core::array::Vector;
fn total<X: AsVector<f64>>(x: X) -> f64 {
let v: Vector<f64> = x.as_vector();
v.sum()
}
let v: Vector<f64> = [1, 2, 3];
assert(total(v) == 6.0);
assert(total([1, 2, 3]) == 6.0);
assert(total([0.5, 1.5]) == 2.0);
assert(total([1.5f32, 2.5f32]) == 4.0);
assert(total(1..4) == 6.0);
selfas a vector (see the trait for when it is a copy).
RhsSource
pub trait Rhs<T>A right-hand side b of A x = b: a vector or a list, or a matrix
whose columns are separate right-hand sides. The answer has b's
kind.
use core::array::{Array, Matrix, Vector};
let a: Matrix<f64> = [[2, 0], [0, 4]];
let v: Vector<f64> = [2, 4];
let m: Matrix<f64> = [[2, 4], [4, 8]];
assert(try linalg::solve(a, v) == Vector::<f64>::from([1.0, 1.0]));
assert((try linalg::solve(a, m)).shape() == [2, 2]);
let xs: List<f64> = [2, 4];
assert(try linalg::solve(a, xs) == [1.0, 1.0]);
selfas a matrix of columns.A solution matrix
xinself's kind.
Functions
adjointSource
pub fn adjoint<T: Scalar>(a: Matrix<T>) -> Matrix<T> The conjugate transpose a^H: a view (a.t()) for a real matrix,
a conjugated copy for a complex one.
use core::array::Matrix;
use core::num::Complex;
let z: Matrix<Complex<f64>> = [[1.0 + 2.0i, 3.0i]];
let h = linalg::adjoint(z);
assert(h.shape() == [2, 1] && h[0, 0] == Complex::new(1.0, -2.0));
diagSource
pub fn diag<T: Scalar>(v: Vector<T>) -> Matrix<T> The square matrix with v on its diagonal.
use core::array::Vector;
let v: Vector<f64> = [1, 2];
assert(linalg::diag(v)[1, 1] == 2.0);
diagonalSource
pub fn diagonal<T: Scalar>(a: Matrix<T>) -> Vector<T> The diagonal a[i, i] of any matrix, as a copy.
use core::array::{Matrix, Vector};
let a: Matrix<f64> = [[1, 2, 3], [4, 5, 6]];
assert(linalg::diagonal(a) == Vector::<f64>::from([1.0, 5.0]));
eyeSource
pub fn eye<T: Scalar>(n: usize) -> Matrix<T> The n x n identity.
use core::array::Matrix;
let i: Matrix<f64> = linalg::eye(2);
assert(i == Matrix::<f64>::from_list([1.0, 0.0, 0.0, 1.0], [2, 2]));
traceSource
pub fn trace<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> T The sum of the diagonal of a square matrix.
use core::array::Matrix;
let a: Matrix<f64> = [[1, 2], [3, 4]];
assert(try linalg::trace(a) == 5.0);
expmSource
pub fn expm<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> Matrix<T> e^A of a square matrix.
use core::array::Matrix;
// e^[[0, t], [-t, 0]] is the rotation by t.
let a: Matrix<f64> = [[0, 1], [-1, 0]];
let e = try linalg::expm(a);
assert((e[0, 0] - 1.0.cos()).abs() < 1e-15 && (e[0, 1] - 1.0.sin()).abs() < 1e-15);
matrix_normSource
pub fn matrix_norm<T: Scalar>(a: Matrix<T>, ord: Norm) throws (LinalgError) -> T::Real The ord norm of a matrix. Two takes an SVD (O(mn^2)) and can
throw NoConvergence; the others are O(mn) and never throw for
finite input.
use core::array::Matrix;
use linalg::Norm;
let a: Matrix<f64> = [[1, -2], [3, 4]];
assert(try linalg::matrix_norm(a, Norm::One) == 6.0);
assert(try linalg::matrix_norm(a, Norm::Inf) == 7.0);
assert((try linalg::matrix_norm(a, Norm::Fro) - 30.0.sqrt()).abs() < 1e-12);
normSource
pub fn norm<T: Scalar>(v: Vector<T>) -> T::Real The Euclidean norm of v.
use core::array::Vector;
let v: Vector<f64> = [3, 4];
assert(linalg::norm(v) == 5.0);
let big: Vector<f64> = [3e200, 4e200]; // the squares would overflow
assert((linalg::norm(big) / 5e200 - 1.0).abs() < 1e-15);
let n = v .* 2.0 |> linalg::norm; // `|>` binds loosest
assert(n == 10.0);
vector_normSource
pub fn vector_norm<T: Scalar>(v: Vector<T>, ord: Norm) -> T::Real The ord norm of a vector (Fro is Two).
use core::array::Vector;
use linalg::Norm;
let v: Vector<f64> = [1, -3, 2];
assert(linalg::vector_norm(v, Norm::One) == 6.0);
assert(linalg::vector_norm(v, Norm::Inf) == 3.0);
dotSource
pub fn dot<T: Scalar>(x: Vector<T>, y: Vector<T>) throws (LinalgError) -> T sum x[i] y[i] (no conjugation; see vdot). Four partial sums.
use core::array::Vector;
let x: Vector<f64> = [1, 2, 3];
assert(try linalg::dot(x, x) == 14.0);
kronSource
pub fn kron<T: Scalar>(a: Matrix<T>, b: Matrix<T>) -> Matrix<T> The Kronecker product: the (p r) x (q s) block matrix whose block
(i, j) is a[i, j] b.
use core::array::Matrix;
let a: Matrix<f64> = [[1, 2]];
let b: Matrix<f64> = [[1], [10]];
assert(linalg::kron(a, b) == Matrix::<f64>::from_list([1.0, 2.0, 10.0, 20.0], [2, 2]));
matmulSource
pub fn matmul<T: Scalar>(a: Matrix<T>, b: Matrix<T>) throws (LinalgError) -> Matrix<T> a b for an m x k and a k x n matrix. Cache-blocked; rows of the
result are split across workers once mnk reaches 128^3. O(mnk).
use core::array::Matrix;
let a: Matrix<f64> = [[1, 2], [3, 4]];
let b: Matrix<f64> = [[0, 1], [1, 0]];
assert(try linalg::matmul(a, b) == Matrix::<f64>::from_list([2.0, 1.0, 4.0, 3.0], [2, 2]));
matrix_powerSource
pub fn matrix_power<T: Scalar>(a: Matrix<T>, k: i64) throws (LinalgError) -> Matrix<T> a to the power k by repeated squaring (O(n^3 log k)); k < 0
inverts first, k = 0 is the identity.
use core::array::Matrix;
let f: Matrix<f64> = [[1, 1], [1, 0]];
assert((try linalg::matrix_power(f, 10))[0, 1] == 55.0);
matvecSource
pub fn matvec<T: Scalar>(a: Matrix<T>, x: Vector<T>) throws (LinalgError) -> Vector<T> a x for an m x n matrix and a vector of n. O(mn).
use core::array::{Matrix, Vector};
let a: Matrix<f64> = [[1, 2], [3, 4]];
let x: Vector<f64> = [1, 1];
assert(try linalg::matvec(a, x) == Vector::<f64>::from([3.0, 7.0]));
outerSource
pub fn outer<T: Scalar>(x: Vector<T>, y: Vector<T>) -> Matrix<T> The outer product: element (i, j) is x[i] y[j].
use core::array::{Matrix, Vector};
let x: Vector<f64> = [1, 2];
let y: Vector<f64> = [3, 4, 5];
assert(linalg::outer(x, y)[1, 2] == 10.0);
vdotSource
pub fn vdot<T: Scalar>(x: Vector<T>, y: Vector<T>) throws (LinalgError) -> T sum conj(x[i]) y[i], the inner product of complex vectors (dot
for real ones).
use core::array::Vector;
use core::num::Complex;
let x: Vector<Complex<f64>> = [1.0 + 1.0i];
assert(try linalg::vdot(x, x) == Complex::new(2.0, 0.0));
vecmatSource
pub fn vecmat<T: Scalar>(x: Vector<T>, a: Matrix<T>) throws (LinalgError) -> Vector<T> x a (x as a row) for a vector of m and an m x n matrix. O(mn).
use core::array::{Matrix, Vector};
let a: Matrix<f64> = [[1, 2], [3, 4]];
let x: Vector<f64> = [1, 1];
assert(try linalg::vecmat(x, a) == Vector::<f64>::from([4.0, 6.0]));
condSource
pub fn cond<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> T::Real The 2-norm condition number s_max / s_min (infinite for a
rank-deficient matrix). log10 of it is about the digits a solve
loses.
use core::array::Matrix;
let a: Matrix<f64> = [[1, 0], [0, 1e-3]];
assert((try linalg::cond(a) - 1e3).abs() < 1e-9);
detSource
pub fn det<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> T det(a) by LU (0 for a singular matrix).
use core::array::Matrix;
let a: Matrix<f64> = [[1, 2], [3, 4]];
assert((try linalg::det(a) + 2.0).abs() < 1e-12);
invSource
pub fn inv<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> Matrix<T> a^-1 by LU. Throws Singular for a zero pivot; prefer solve to
multiplying by the inverse.
use core::array::Matrix;
let a: Matrix<f64> = [[2, 0], [0, 4]];
assert((try linalg::inv(a))[1, 1] == 0.25);
lstsqSource
pub fn lstsq<T: Scalar, B: Rhs<T>>(a: Matrix<T>, b: B) throws (LinalgError) -> B The least-squares solution of a x = b (m x n a, any shape and
rank): minimizes ||a x - b||, and among minimizers ||x||. A
pivoted QR finds the numerical rank (diagonal of R above
max(m, n) * eps * |R[0, 0]|); a full-rank problem is solved from R,
a deficient one through a complete orthogonal decomposition (LAPACK's
xGELSY). O(mn^2).
use core::array::{Matrix, Vector};
// Fit y = c0 + c1 t through (0, 1), (1, 3), (2, 5).
let a: Matrix<f64> = [[1, 0], [1, 1], [1, 2]];
let y: Vector<f64> = [1, 3, 5];
let c = try linalg::lstsq(a, y);
assert((c[0] - 1.0).abs() < 1e-12 && (c[1] - 2.0).abs() < 1e-12);
null_spaceSource
pub fn null_space<T: Real>(a: Matrix<T>) throws (LinalgError) -> Matrix<T> An orthonormal basis of the null space {x : a x = 0}, as the
columns of an n x (n - rank) matrix (from the trailing rows of V^T).
use core::array::Matrix;
let a: Matrix<f64> = [[1, 1]];
let z = try linalg::null_space(a);
assert(z.shape() == [2, 1] && (z[0, 0] + z[1, 0]).abs() < 1e-12);
pinvSource
pub fn pinv<T: Real>(a: Matrix<T>) throws (LinalgError) -> Matrix<T> The Moore-Penrose pseudo-inverse V S^+ U^T (n x m), singular
values at or below max(m, n) * eps * s_max taken as zero.
use core::array::Matrix;
let a: Matrix<f64> = [[1, 0], [0, 0]];
assert(try linalg::pinv(a) == a);
rankSource
pub fn rank<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> usize The number of singular values above max(m, n) * eps * s_max.
use core::array::Matrix;
let a: Matrix<f64> = [[1, 2], [2, 4], [3, 6]];
assert(try linalg::rank(a) == 1);
slogdetSource
pub fn slogdet<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> (T, T::Real) (sign, ln |det(a)|): det(a) = sign * exp(logabs) without
overflow. sign has modulus 1 (+-1 for a real matrix), or is 0 with
logabs = -inf for a singular one.
use core::array::Matrix;
let a: Matrix<f64> = [[1e200, 0], [0, -1e200]];
let (s, l) = try linalg::slogdet(a);
assert(s == -1.0 && (l - 400.0 * 10.0.ln()).abs() < 1e-9);
solveSource
pub fn solve<T: Scalar, B: Rhs<T>>(a: Matrix<T>, b: B) throws (LinalgError) -> B x with a x = b for a square a, by LU with partial pivoting;
b is a vector or a matrix of columns. Throws Singular for a zero
pivot. O(2n^3/3 + 2n^2 k).
use core::array::{Matrix, Vector};
let a: Matrix<f64> = [[3, 1], [1, 2]];
let b: Vector<f64> = [9, 8];
let x = try linalg::solve(a, b);
assert(x == Vector::<f64>::from([2.0, 3.0]));
let r = a @ x .- b |> linalg::norm; // the residual
assert(r == 0.0);
solve_triangularSource
pub fn solve_triangular<T: Scalar, B: Rhs<T>>(t: Matrix<T>, b: B, part: Triangle) throws (LinalgError) -> B x with t x = b for a square triangular t, by substitution; only
the given triangle of t is read. Throws Singular for a zero on
the diagonal. O(n^2 k).
use core::array::{Matrix, Vector};
use linalg::Triangle;
let l: Matrix<f64> = [[2, 0], [1, 1]];
let b: Vector<f64> = [2, 3];
assert(try linalg::solve_triangular(l, b, Triangle::Lower) == Vector::<f64>::from([1.0, 2.0]));
svdvalsSource
pub fn svdvals<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> Vector<T::Real> The singular values of a, largest first; complex matrices go
through the real form [[Re, -Im], [Im, Re]], whose singular values
are those of a, each twice.
use core::array::Matrix;
let a: Matrix<f64> = [[0, 2], [1, 0]];
let s = try linalg::svdvals(a);
assert(s[0] == 2.0 && s[1] == 1.0);
luSource
pub fn lu<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> Lu<T> P A = L U with partial pivoting (square a). See Lu.
use core::array::Matrix;
let a: Matrix<f64> = [[1, 2], [3, 4]];
let f = try linalg::lu(a);
assert((f.det() + 2.0).abs() < 1e-12);
choleskySource
pub fn cholesky<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> Cholesky<T> A = L L^H of a Hermitian positive definite matrix. See Cholesky.
use core::array::Matrix;
let a: Matrix<f64> = [[4, 2], [2, 5]];
assert((try linalg::cholesky(a)).l()[1, 1] == 2.0);
ldltSource
pub fn ldlt<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> Ldlt<T> A = L D L^H without pivoting. See Ldlt.
use core::array::Matrix;
let a: Matrix<f64> = [[2, 1], [1, -1]];
assert((try linalg::ldlt(a)).d()[1] == -1.5);
qrSource
pub fn qr<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> Qr<T> Householder A = Q R of any m x n matrix. See Qr.
use core::array::Matrix;
let a: Matrix<f64> = [[1, 1], [1, 2], [1, 3]];
let f = try linalg::qr(a);
assert(f.q().shape() == [3, 2] && f.r().shape() == [2, 2]);
qr_pivotedSource
pub fn qr_pivoted<T: Scalar>(a: Matrix<T>) throws (LinalgError) -> Qr<T> A P = Q R with column pivoting: |R[i, i]| falls, which shows
the numerical rank. See Qr.
use core::array::Matrix;
let a: Matrix<f64> = [[1, 10], [1, 20]];
assert((try linalg::qr_pivoted(a)).perm() == [1, 0]);
svdSource
pub fn svd<T: Real>(a: Matrix<T>) throws (LinalgError) -> Svd<T> The thin SVD of a real matrix (U is m x k, V^T k x n, k = min(m, n)).
See Svd.
use core::array::Matrix;
let a: Matrix<f64> = [[1, 0, 0], [0, 2, 0]];
let f = try linalg::svd(a);
assert(f.u().shape() == [2, 2] && f.vt().shape() == [2, 3]);
svd_fullSource
pub fn svd_full<T: Real>(a: Matrix<T>) throws (LinalgError) -> Svd<T> The full SVD (U is m x m, V^T n x n). See Svd.
use core::array::Matrix;
let a: Matrix<f64> = [[1, 0, 0], [0, 2, 0]];
assert((try linalg::svd_full(a)).vt().shape() == [3, 3]);
eighSource
pub fn eigh<T: Real>(a: Matrix<T>) throws (LinalgError) -> Eigh<T> Eigenvalues and eigenvectors of a real symmetric matrix. See Eigh.
use core::array::Matrix;
let a: Matrix<f64> = [[2, 0], [0, 1]];
assert((try linalg::eigh(a)).values()[0] == 1.0);
eigvalshSource
pub fn eigvalsh<T: Real>(a: Matrix<T>) throws (LinalgError) -> Vector<T> The eigenvalues of a real symmetric matrix, rising, without the
vectors (about a third of the work of eigh).
use core::array::Matrix;
let a: Matrix<f64> = [[0, 1], [1, 0]];
let w = try linalg::eigvalsh(a);
assert((w[0] + 1.0).abs() < 1e-15 && (w[1] - 1.0).abs() < 1e-15);
eigSource
pub fn eig<T: Real>(a: Matrix<T>) throws (LinalgError) -> Eig<T> Eigenvalues and eigenvectors of a general real matrix (complex in
general). See Eig.
use core::array::Matrix;
let a: Matrix<f64> = [[2, 1], [0, 3]];
assert((try linalg::eig(a)).values().len() == 2);
schurSource
pub fn schur<T: Real>(a: Matrix<T>) throws (LinalgError) -> Schur<T> The real Schur form A = Z T Z^T. See Schur.
use core::array::Matrix;
let a: Matrix<f64> = [[0, -1], [1, 0]];
assert((try linalg::schur(a)).t()[1, 0] != 0.0); // one 2 x 2 block