Skip to main content

Module lu

Module lu 

Source
Expand description

LU, Cholesky, matrix inversion, and determinant computation utilities

use mdarray_linalg::prelude::*;
use mdarray_linalg_backend::Backend;

let bd = Backend::default();

let a = Array::from_fn([3, 3], |i| {
    (i[0] + 1) as f64 + 2. * (i[1] + 1) as f64 + if i[0] == i[1] { 3. } else { 0. }
}); // invertible matrix

// ----- LU decomposition -----
// P * A = L * U  where P is a permutation matrix.
let (l, u, p) = bd.lu(&mut a.clone());

// ----- Determinant and inverse -----
let d = bd.det(&mut a.clone());
let a_inv = bd.inv(&mut a.clone()).expect("Can't compute inverse");

// ----- Cholesky decomposition -----
// For a symmetric positive-definite matrix: A = L * L^T
let s = a.clone() + a.permute([1, 0]); // symmetric matrix
let l = bd.cholesky(&mut s).unwrap();
// Reconstruct: A ≈ L * L^T
let a_reconstructed = l.dot(&l.transpose());

Enums§

InvError
Error types related to matrix inversion

Traits§

LU
LU decomposition and matrix inversion