pub struct Matrix<const D: usize> { /* private fields */ }Expand description
Fixed-size square matrix D×D, stored inline.
Implementations§
Source§impl<const D: usize> Matrix<D>
impl<const D: usize> Matrix<D>
Sourcepub const fn from_rows(rows: [[f64; D]; D]) -> Self
pub const fn from_rows(rows: [[f64; D]; D]) -> Self
Construct from row-major storage.
§Examples
use la_stack::prelude::*;
let m = Matrix::<2>::from_rows([[1.0, 2.0], [3.0, 4.0]]);
assert_eq!(m.get(0, 1), Some(2.0));Sourcepub const fn zero() -> Self
pub const fn zero() -> Self
All-zeros matrix.
§Examples
use la_stack::prelude::*;
let z = Matrix::<2>::zero();
assert_eq!(z.get(1, 1), Some(0.0));Sourcepub const fn identity() -> Self
pub const fn identity() -> Self
Identity matrix.
§Examples
use la_stack::prelude::*;
let i = Matrix::<3>::identity();
assert_eq!(i.get(0, 0), Some(1.0));
assert_eq!(i.get(0, 1), Some(0.0));
assert_eq!(i.get(2, 2), Some(1.0));Sourcepub const fn get(&self, r: usize, c: usize) -> Option<f64>
pub const fn get(&self, r: usize, c: usize) -> Option<f64>
Get an element with bounds checking.
§Examples
use la_stack::prelude::*;
let m = Matrix::<2>::from_rows([[1.0, 2.0], [3.0, 4.0]]);
assert_eq!(m.get(1, 0), Some(3.0));
assert_eq!(m.get(2, 0), None);Sourcepub const fn set(&mut self, r: usize, c: usize, value: f64) -> bool
pub const fn set(&mut self, r: usize, c: usize, value: f64) -> bool
Set an element with bounds checking.
Returns true if the index was in-bounds.
§Examples
use la_stack::prelude::*;
let mut m = Matrix::<2>::zero();
assert!(m.set(0, 1, 2.5));
assert_eq!(m.get(0, 1), Some(2.5));
assert!(!m.set(10, 0, 1.0));Sourcepub fn inf_norm(&self) -> f64
pub fn inf_norm(&self) -> f64
Infinity norm (maximum absolute row sum).
§Examples
use la_stack::prelude::*;
let m = Matrix::<2>::from_rows([[1.0, -2.0], [3.0, 4.0]]);
assert!((m.inf_norm() - 7.0).abs() <= 1e-12);Sourcepub fn lu(self, tol: f64) -> Result<Lu<D>, LaError>
pub fn lu(self, tol: f64) -> Result<Lu<D>, LaError>
Compute an LU decomposition with partial pivoting.
§Examples
use la_stack::prelude::*;
let a = Matrix::<2>::from_rows([[1.0, 2.0], [3.0, 4.0]]);
let lu = a.lu(DEFAULT_PIVOT_TOL)?;
let b = Vector::<2>::new([5.0, 11.0]);
let x = lu.solve_vec(b)?.into_array();
assert!((x[0] - 1.0).abs() <= 1e-12);
assert!((x[1] - 2.0).abs() <= 1e-12);§Errors
Returns LaError::Singular if, for some column k, the largest-magnitude candidate pivot
in that column satisfies |pivot| <= tol (so no numerically usable pivot exists).
Returns LaError::NonFinite if NaN/∞ is detected during factorization.
Sourcepub fn ldlt(self, tol: f64) -> Result<Ldlt<D>, LaError>
pub fn ldlt(self, tol: f64) -> Result<Ldlt<D>, LaError>
Compute an LDLT factorization (A = L D Lᵀ) without pivoting.
This is intended for symmetric positive definite (SPD) and positive semi-definite (PSD) matrices such as Gram matrices.
§Examples
use la_stack::prelude::*;
let a = Matrix::<2>::from_rows([[4.0, 2.0], [2.0, 3.0]]);
let ldlt = a.ldlt(DEFAULT_SINGULAR_TOL)?;
// det(A) = 8
assert!((ldlt.det() - 8.0).abs() <= 1e-12);
// Solve A x = b
let b = Vector::<2>::new([1.0, 2.0]);
let x = ldlt.solve_vec(b)?.into_array();
assert!((x[0] - (-0.125)).abs() <= 1e-12);
assert!((x[1] - 0.75).abs() <= 1e-12);§Errors
Returns LaError::Singular if, for some step k, the required diagonal entry d = D[k,k]
is <= tol (non-positive or too small). This treats PSD degeneracy (and indefinite inputs)
as singular/degenerate.
Returns LaError::NonFinite if NaN/∞ is detected during factorization.