5 Linear Algebra

5.1 Transpose

  • Caution: Transpose does not consume the original value.

    fn main() {
        let a = matrix!(1;4;1, 2, 2, Row);
        a.transpose().print();
        // Or you can use shorter one
        a.t().print();
        //      c[0] c[1]
        // r[0]    1    3
        // r[1]    2    4
    }

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 returns Option<PQLU>

    • PQLU has four field - p, q, l , u
    • p means row permutations
    • q means column permutations
    • l means lower triangular matrix
    • u 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
    }