5 Linear Algebra
5.2 LU Decomposition
Peroxide uses complete pivoting for LU decomposition - Very stable
Since there are lots of causes to generate error, you should use
Option
lu
returnsOption<PQLU>
PQLU
has four field -p
,q
,l
,u
p
means row permutationsq
means column permutationsl
means lower triangular matrixu
menas upper triangular matrix
The structure of
PQLU
is as follows:#[derive(Debug, Clone)] pub struct PQLU { pub p: Perms, pub q: Perms, pub l: Matrix, pub u: Matrix, } pub type Perms = Vec<(usize, usize)>;
Example of LU decomposition:
fn main() {
let a = matrix(c!(1,2,3,4), 2, 2, Row);
let pqlu = a.lu().unwrap(); // unwrap because of Option
let (p,q,l,u) = (pqlu.p, pqlu.q, pqlu.l, pqlu.u);
assert_eq!(p, vec![(0,1)]); // swap 0 & 1 (Row)
assert_eq!(q, vec![(0,1)]); // swap 0 & 1 (Col)
assert_eq!(l, matrix(c!(1,0,0.5,1),2,2,Row));
// c[0] c[1]
// r[0] 1 0
// r[1] 0.5 1
assert_eq!(u, matrix(c!(4,3,0,-0.5),2,2,Row));
// c[0] c[1]
// r[0] 4 3
// r[1] 0 -0.5
}