4 Matrix

4.1 Declare matrix

  • You can declare matrix by various ways.
    • R’s way - Default
    • MATLAB’s way
    • Python’s way
    • Other macro

4.1.1 R’s way

  • Description: Same as R - matrix(Vector, Row, Col, Shape)
  • Type: matrix(Vec<T>, usize, usize, Shape) where T: std::convert::Into<f64> + Copy
    • Shape: Enum for matrix shape - Row & Col
    fn main() {
        let a = matrix(c!(1,2,3,4), 2, 2, Row);
        a.print();
        //       c[0] c[1]
        // r[0]     1    2
        // r[1]     3    4
    
        let b = matrix(c!(1,2,3,4), 2, 2, Col);
        b.print();
        //       c[0] c[1]
        // r[0]     1    3
        // r[1]     2    4
    }

4.1.2 MATLAB’s way

  • Description: Similar to MATLAB (But should use &str)

  • Type: ml_matrix(&str)

    fn main() {
        let a = ml_matrix("1 2; 3 4");
        a.print();
        //       c[0] c[1]
        // r[0]     1    2
        // r[1]     3    4
    }

4.1.3 Python’s way

  • Description: Declare matrix as vector of vectors.

  • Type: py_matrix(Vec<Vec<T>>) where T: std::convert::Into<f64> + Copy

    fn main() {
        let a = py_matrix(vec![vec![1, 2], vec![3, 4]]);
        a.print();
        //       c[0] c[1]
        // r[0]     1    2
        // r[1]     3    4
    }

4.1.4 Other macro

  • Description: R-like macro to declare matrix

  • For R,

    # R
    a = matrix(1:4:1, 2, 2, Row)
    print(a)
    #      [,1] [,2]
    # [1,]    1    2
    # [2,]    3    4
  • For Peroxide,

    fn main() {
        let a = matrix!(1;4;1, 2, 2, Row);
        a.print();
        //       c[0] c[1]
        // r[0]     1    2
        // r[1]     3    4
    }