Matrix

Struct Matrix 

Source
pub struct Matrix { /* private fields */ }
Expand description

A matrix structure.

Implementations§

Source§

impl Matrix

Source

pub fn new(row_count: usize, col_count: usize) -> Self

Creates a matrix with the number of rows and the number of columns.

Source

pub fn new_with_elems(row_count: usize, col_count: usize, elems: &[f32]) -> Self

Creates a matrix with the number of rows, the number of columns, and the elements.

Examples found in repository?
examples/mul.rs (line 21)
13fn create_matrix(n: usize, m: usize) -> Matrix
14{
15    let mut elems: Vec<f32> = vec![0.0f32; n * m];
16    for i in 0..n {
17        for j in 0..m {
18            elems[m * i + j] = (m * i + j) as f32;
19        }
20    }
21    Matrix::new_with_elems(n, m, elems.as_slice())
22}
More examples
Hide additional examples
examples/mul_bt.rs (line 21)
13fn create_matrix(n: usize, m: usize) -> Matrix
14{
15    let mut elems: Vec<f32> = vec![0.0f32; n * m];
16    for i in 0..n {
17        for j in 0..m {
18            elems[m * i + j] = (m * i + j) as f32;
19        }
20    }
21    Matrix::new_with_elems(n, m, elems.as_slice())
22}
examples/mad.rs (line 26)
13fn create_matrix(n: usize, m: usize, is_scalar: bool) -> Matrix
14{
15    let mut elems: Vec<f32> = vec![0.0f32; n * m];
16    let scalar = if is_scalar {
17        (n * m) as f32
18    } else {
19        1.0f32
20    };
21    for i in 0..n {
22        for j in 0..m {
23            elems[m * i + j] = ((m * i + j) as f32) * scalar;
24        }
25    }
26    Matrix::new_with_elems(n, m, elems.as_slice())
27}
Source

pub fn new_with_elem_vecs(elem_vecs: &[Vec<f32>]) -> Self

Creates a matrix with the vector of rows.

Source

pub fn row_count(&self) -> usize

Returns the number of matrix rows.

Examples found in repository?
examples/simple.rs (line 24)
10fn main()
11{
12    let a = matrix![
13        [1.0, 2.0, 3.0],
14        [4.0, 5.0, 6.0],
15        [7.0, 8.0, 9.0]
16    ];
17    let b = matrix![
18        [1.0, 4.0, 7.0],
19        [2.0, 5.0, 8.0],
20        [3.0, 6.0, 9.0]
21    ];
22    let c = a * b;
23    let elems = c.elems();
24    for i in 0..c.row_count() {
25        for j in 0..c.col_count() {
26            print!("\t{}", elems[i * c.col_count() + j]);
27        }
28        println!("");
29    }
30}
Source

pub fn col_count(&self) -> usize

Returns the number of matrix columns.

Examples found in repository?
examples/simple.rs (line 25)
10fn main()
11{
12    let a = matrix![
13        [1.0, 2.0, 3.0],
14        [4.0, 5.0, 6.0],
15        [7.0, 8.0, 9.0]
16    ];
17    let b = matrix![
18        [1.0, 4.0, 7.0],
19        [2.0, 5.0, 8.0],
20        [3.0, 6.0, 9.0]
21    ];
22    let c = a * b;
23    let elems = c.elems();
24    for i in 0..c.row_count() {
25        for j in 0..c.col_count() {
26            print!("\t{}", elems[i * c.col_count() + j]);
27        }
28        println!("");
29    }
30}
Source

pub fn is_transposed(&self) -> bool

Returns true if the matrix is transposed, otherwise false.

This method indeed returns the transpose flag of matrix that is changed by transpose.

Source

pub fn elems(&self) -> Vec<f32>

Returns the matrix elements.

Examples found in repository?
examples/simple.rs (line 23)
10fn main()
11{
12    let a = matrix![
13        [1.0, 2.0, 3.0],
14        [4.0, 5.0, 6.0],
15        [7.0, 8.0, 9.0]
16    ];
17    let b = matrix![
18        [1.0, 4.0, 7.0],
19        [2.0, 5.0, 8.0],
20        [3.0, 6.0, 9.0]
21    ];
22    let c = a * b;
23    let elems = c.elems();
24    for i in 0..c.row_count() {
25        for j in 0..c.col_count() {
26            print!("\t{}", elems[i * c.col_count() + j]);
27        }
28        println!("");
29    }
30}
More examples
Hide additional examples
examples/mul.rs (line 76)
24fn main()
25{
26    let args: Vec<String> = env::args().collect();
27    let n: usize = match args.get(1) {
28        Some(s) => {
29            match s.parse::<usize>() {
30                Ok(tmp_n) => tmp_n,
31                Err(err) => {
32                    eprintln!("{}", err);
33                    exit(1);
34                },
35            }
36        },
37        None => 100,
38    };
39    let m: usize = match args.get(2) {
40        Some(s) => {
41            match s.parse::<usize>() {
42                Ok(tmp_m) => tmp_m,
43                Err(err) => {
44                    eprintln!("{}", err);
45                    exit(1);
46                },
47            }
48        },
49        None => 100,
50    };
51    let l: usize = match args.get(3) {
52        Some(s) => {
53            match s.parse::<usize>() {
54                Ok(tmp_l) => tmp_l,
55                Err(err) => {
56                    eprintln!("{}", err);
57                    exit(1);
58                },
59            }
60        },
61        None => 100,
62    };
63    let frontend = match Frontend::new() {
64        Ok(tmp_frontend) => tmp_frontend,
65        Err(err) => {
66            eprintln!("{}", err);
67            exit(1);
68        },
69    };
70    println!("backend: {}", frontend.backend().name());
71    let a = create_matrix(n, l);
72    let b = create_matrix(l, m);
73    let now = Instant::now();
74    let c = a * b;
75    let duration = now.elapsed();
76    let elems = c.elems();
77    let sum = elems.iter().fold(0.0f32, |x, y| x + y);
78    println!("sum: {}", sum);
79    println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
80    match finalize_default_backend() {
81        Ok(()) => (),
82        Err(err) => {
83            eprintln!("{}", err);
84            exit(1);
85        },
86    }
87}
examples/mul_bt.rs (line 76)
24fn main()
25{
26    let args: Vec<String> = env::args().collect();
27    let n: usize = match args.get(1) {
28        Some(s) => {
29            match s.parse::<usize>() {
30                Ok(tmp_n) => tmp_n,
31                Err(err) => {
32                    eprintln!("{}", err);
33                    exit(1);
34                },
35            }
36        },
37        None => 100,
38    };
39    let m: usize = match args.get(2) {
40        Some(s) => {
41            match s.parse::<usize>() {
42                Ok(tmp_m) => tmp_m,
43                Err(err) => {
44                    eprintln!("{}", err);
45                    exit(1);
46                },
47            }
48        },
49        None => 100,
50    };
51    let l: usize = match args.get(3) {
52        Some(s) => {
53            match s.parse::<usize>() {
54                Ok(tmp_l) => tmp_l,
55                Err(err) => {
56                    eprintln!("{}", err);
57                    exit(1);
58                },
59            }
60        },
61        None => 100,
62    };
63    let frontend = match Frontend::new() {
64        Ok(tmp_frontend) => tmp_frontend,
65        Err(err) => {
66            eprintln!("{}", err);
67            exit(1);
68        },
69    };
70    println!("backend: {}", frontend.backend().name());
71    let a = create_matrix(n, l);
72    let b = create_matrix(m, l);
73    let now = Instant::now();
74    let c = a * b.transpose();
75    let duration = now.elapsed();
76    let elems = c.elems();
77    let sum = elems.iter().fold(0.0f32, |x, y| x + y);
78    println!("sum: {}", sum);
79    println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
80    match finalize_default_backend() {
81        Ok(()) => (),
82        Err(err) => {
83            eprintln!("{}", err);
84            exit(1);
85        },
86    }
87}
examples/mad.rs (line 82)
29fn main()
30{
31    let args: Vec<String> = env::args().collect();
32    let n: usize = match args.get(1) {
33        Some(s) => {
34            match s.parse::<usize>() {
35                Ok(tmp_n) => tmp_n,
36                Err(err) => {
37                    eprintln!("{}", err);
38                    exit(1);
39                },
40            }
41        },
42        None => 100,
43    };
44    let m: usize = match args.get(2) {
45        Some(s) => {
46            match s.parse::<usize>() {
47                Ok(tmp_m) => tmp_m,
48                Err(err) => {
49                    eprintln!("{}", err);
50                    exit(1);
51                },
52            }
53        },
54        None => 100,
55    };
56    let l: usize = match args.get(3) {
57        Some(s) => {
58            match s.parse::<usize>() {
59                Ok(tmp_l) => tmp_l,
60                Err(err) => {
61                    eprintln!("{}", err);
62                    exit(1);
63                },
64            }
65        },
66        None => 100,
67    };
68    let frontend = match Frontend::new() {
69        Ok(tmp_frontend) => tmp_frontend,
70        Err(err) => {
71            eprintln!("{}", err);
72            exit(1);
73        },
74    };
75    println!("backend: {}", frontend.backend().name());
76    let a = create_matrix(n, l, false);
77    let b = create_matrix(l, m, false);
78    let c = create_matrix(n, m, true);
79    let now = Instant::now();
80    let d = a * b + c;
81    let duration = now.elapsed();
82    let elems = d.elems();
83    let sum = elems.iter().fold(0.0f32, |x, y| x + y);
84    println!("sum: {}", sum);
85    println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
86    match finalize_default_backend() {
87        Ok(()) => (),
88        Err(err) => {
89            eprintln!("{}", err);
90            exit(1);
91        },
92    }
93}
Source

pub fn copy(&self) -> Self

Creates a matrix copy.

This method indeed copies the matrix array to a new matrix array.

Source

pub fn transpose(&self) -> Self

Transposes the matrix (AT).

This method doesn’t indeed transpose the matrix but changes the transpose flag and exchanges the number of matrix rows with the number of matrix columns.

§Examples
let a = matrix![
    [1.0, 2.0, 3.0],
    [4.0, 5.0, 6.0]
];
let b = a.transpose();
assert_eq!(3, b.row_count());
assert_eq!(2, b.col_count());
assert_eq!(true, b.is_transposed());
assert_eq!(a.elems(), b.elems());
let c = b.transpose();
assert_eq!(2, c.row_count());
assert_eq!(3, c.col_count());
assert_eq!(false, c.is_transposed());
assert_eq!(a.elems(), c.elems());
Examples found in repository?
examples/mul_bt.rs (line 74)
24fn main()
25{
26    let args: Vec<String> = env::args().collect();
27    let n: usize = match args.get(1) {
28        Some(s) => {
29            match s.parse::<usize>() {
30                Ok(tmp_n) => tmp_n,
31                Err(err) => {
32                    eprintln!("{}", err);
33                    exit(1);
34                },
35            }
36        },
37        None => 100,
38    };
39    let m: usize = match args.get(2) {
40        Some(s) => {
41            match s.parse::<usize>() {
42                Ok(tmp_m) => tmp_m,
43                Err(err) => {
44                    eprintln!("{}", err);
45                    exit(1);
46                },
47            }
48        },
49        None => 100,
50    };
51    let l: usize = match args.get(3) {
52        Some(s) => {
53            match s.parse::<usize>() {
54                Ok(tmp_l) => tmp_l,
55                Err(err) => {
56                    eprintln!("{}", err);
57                    exit(1);
58                },
59            }
60        },
61        None => 100,
62    };
63    let frontend = match Frontend::new() {
64        Ok(tmp_frontend) => tmp_frontend,
65        Err(err) => {
66            eprintln!("{}", err);
67            exit(1);
68        },
69    };
70    println!("backend: {}", frontend.backend().name());
71    let a = create_matrix(n, l);
72    let b = create_matrix(m, l);
73    let now = Instant::now();
74    let c = a * b.transpose();
75    let duration = now.elapsed();
76    let elems = c.elems();
77    let sum = elems.iter().fold(0.0f32, |x, y| x + y);
78    println!("sum: {}", sum);
79    println!("time: {}.{:06}", duration.as_secs(), duration.as_micros() % 1000000);
80    match finalize_default_backend() {
81        Ok(()) => (),
82        Err(err) => {
83            eprintln!("{}", err);
84            exit(1);
85        },
86    }
87}
Source

pub fn t(&self) -> Self

See transpose.

Source

pub fn really_transpose(&self) -> Self

Indeed transposes the matrix (AT).

This method indeed transposes the matrix without changing the transpose flag.

§Examples
let a = matrix![
    [1.0, 2.0, 3.0],
    [4.0, 5.0, 6.0]
];
let b = a.really_transpose();
assert_eq!(3, b.row_count());
assert_eq!(2, b.col_count());
assert_eq!(false, b.is_transposed());
assert_eq!(vec![1.0, 4.0, 2.0, 5.0, 3.0, 6.0], b.elems());
Source

pub fn rt(&self) -> Self

Source

pub fn mul_elems(&self, b: &Self) -> Self

Multiplies the matrix elements by the b matrix elements (aij·bij).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = matrix![
    [5.0, 6.0],
    [7.0, 8.0]
];
let c = a.mul_elems(&b);
assert_eq!(vec![1.0 * 5.0, 2.0 * 6.0, 3.0 * 7.0, 4.0 * 8.0], c.elems());
Source

pub fn div_elems(&self, b: &Self) -> Self

Divides the matrix elements by the b matrix elements (aijbij).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = matrix![
    [5.0, 6.0],
    [7.0, 8.0]
];
let c = a.div_elems(&b);
let elems = c.elems();
assert!((1.0 / 5.0 - elems[0]).abs() < 0.001);
assert!((2.0 / 6.0 - elems[1]).abs() < 0.001);
assert!((3.0 / 7.0 - elems[2]).abs() < 0.001);
assert!((4.0 / 8.0 - elems[3]).abs() < 0.001);
Source

pub fn rsub(&self, b: f32) -> Self

Subtracts the matrix from the b scalar (b-A).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.rsub(10.5);
assert_eq!(vec![10.5 - 1.0, 10.5 - 2.0, 10.5 - 3.0, 10.5 - 4.0], b.elems());
Source

pub fn rdiv(&self, b: f32) -> Self

Divides the b scalar by the matrix elements (baij).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.rdiv(10.5);
let elems = b.elems();
assert!((10.5 / 1.0 - elems[0]).abs() < 0.001);
assert!((10.5 / 2.0 - elems[1]).abs() < 0.001);
assert!((10.5 / 3.0 - elems[2]).abs() < 0.001);
assert!((10.5 / 4.0 - elems[3]).abs() < 0.001);
Source

pub fn sigmoid(&self) -> Self

Calculates sigmoid function for the matrix (sigmoid(A)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.sigmoid();
let elems = b.elems();
assert!((1.0 / (1.0 + (-1.0f32).exp()) - elems[0]).abs() < 0.001);
assert!((1.0 / (1.0 + (-2.0f32).exp()) - elems[1]).abs() < 0.001);
assert!((1.0 / (1.0 + (-3.0f32).exp()) - elems[2]).abs() < 0.001);
assert!((1.0 / (1.0 + (-4.0f32).exp()) - elems[3]).abs() < 0.001);
Source

pub fn tanh(&self) -> Self

Calculates hyperbolic tangent function for the matrix (tanh(A)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.tanh();
let elems = b.elems();
assert!((1.0f32.tanh() - elems[0]).abs() < 0.001);
assert!((2.0f32.tanh() - elems[1]).abs() < 0.001);
assert!((3.0f32.tanh() - elems[2]).abs() < 0.001);
assert!((4.0f32.tanh() - elems[3]).abs() < 0.001);
Source

pub fn swish(&self) -> Self

Calculates swish function for the matrix (swish(A)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.swish();
let elems = b.elems();
assert!((1.0 / (1.0 + (-1.0f32).exp()) - elems[0]).abs() < 0.001);
assert!((2.0 / (1.0 + (-2.0f32).exp()) - elems[1]).abs() < 0.001);
assert!((3.0 / (1.0 + (-3.0f32).exp()) - elems[2]).abs() < 0.001);
assert!((4.0 / (1.0 + (-4.0f32).exp()) - elems[3]).abs() < 0.001);
Source

pub fn softmax(&self) -> Self

Calculates softmax function for the matrix (softmax(A)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.softmax();
let elems = b.elems();
let sum1 = 1.0f32.exp() + 3.0f32.exp();
let sum2 = 2.0f32.exp() + 4.0f32.exp();
assert!((1.0f32.exp() / sum1 - elems[0]).abs() < 0.001);
assert!((2.0f32.exp() / sum2 - elems[1]).abs() < 0.001);
assert!((3.0f32.exp() / sum1 - elems[2]).abs() < 0.001);
assert!((4.0f32.exp() / sum2 - elems[3]).abs() < 0.001);
Source

pub fn sqrt(&self) -> Self

Calculates square roots of the matrix elements (aij).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.sqrt();
let elems = b.elems();
assert!((1.0f32.sqrt() - elems[0]).abs() < 0.001);
assert!((2.0f32.sqrt() - elems[1]).abs() < 0.001);
assert!((3.0f32.sqrt() - elems[2]).abs() < 0.001);
assert!((4.0f32.sqrt() - elems[3]).abs() < 0.001);
Source

pub fn repeat(&self, n: usize) -> Self

Repeats the vector as column or a row.

§Examples
let a = matrix![
    [1.0],
    [2.0]
];
let b = a.repeat(3);
assert_eq!(vec![1.0, 1.0, 1.0, 2.0, 2.0, 2.0], b.elems());
let c = matrix![[1.0, 2.0, 3.0]];
let d = c.repeat(2);
assert_eq!(vec![1.0, 2.0, 3.0, 1.0, 2.0, 3.0], d.elems());
Source

pub fn abs(&self) -> Self

Calculates absolute values of the matrix elements (|aij|).

§Examples
let a = matrix![
    [-2.0, -1.0],
    [1.0, 2.0]
];
let b = a.abs();
assert_eq!(vec![2.0, 1.0, 1.0, 2.0], b.elems());
Source

pub fn powm(&self, b: &Self) -> Self

Raises the matrix elements to the power of the b matrix elements (aijbij).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = matrix![
    [3.0, 4.0],
    [5.0, 6.0]
];
let c = a.powm(&b);
let elems = c.elems();
assert!((1.0f32.powf(3.0) - elems[0]).abs() < 0.001);
assert!((2.0f32.powf(4.0) - elems[1]).abs() < 0.001);
assert!((3.0f32.powf(5.0) - elems[2]).abs() < 0.001);
assert!((4.0f32.powf(6.0) - elems[3]).abs() < 0.001);
Source

pub fn powf(&self, b: f32) -> Self

Raises the matrix elements to the power of the b scalar (aijb).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.powf(2.5);
let elems = b.elems();
assert!((1.0f32.powf(2.5) - elems[0]).abs() < 0.001);
assert!((2.0f32.powf(2.5) - elems[1]).abs() < 0.001);
assert!((3.0f32.powf(2.5) - elems[2]).abs() < 0.001);
assert!((4.0f32.powf(2.5) - elems[3]).abs() < 0.001);
Source

pub fn rpowf(&self, b: f32) -> Self

Raises the b scalar to the power of the matrix elements (baij).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.rpowf(10.5);
let elems = b.elems();
assert!((10.5f32.powf(1.0) - elems[0]).abs() < 0.001);
assert!((10.5f32.powf(2.0) - elems[1]).abs() < 0.001);
assert!((10.5f32.powf(3.0) - elems[2]).abs() < 0.001);
assert!((10.5f32.powf(4.0) - elems[3]).abs() < 0.001);
Source

pub fn exp(&self) -> Self

Calculates exponential function for the matrix elements (eaij).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.exp();
let elems = b.elems();
assert!((1.0f32.exp() - elems[0]).abs() < 0.001);
assert!((2.0f32.exp() - elems[1]).abs() < 0.001);
assert!((3.0f32.exp() - elems[2]).abs() < 0.001);
assert!((4.0f32.exp() - elems[3]).abs() < 0.001);
Source

pub fn ln(&self) -> Self

Calculates natural logarithm of the matrix elements (lnaij).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.ln();
let elems = b.elems();
assert!((1.0f32.ln() - elems[0]).abs() < 0.001);
assert!((2.0f32.ln() - elems[1]).abs() < 0.001);
assert!((3.0f32.ln() - elems[2]).abs() < 0.001);
assert!((4.0f32.ln() - elems[3]).abs() < 0.001);
Source

pub fn log2(&self) -> Self

Calculates base 2 logarithm of the matrix elements (log2aij).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.log2();
let elems = b.elems();
assert!((1.0f32.log2() - elems[0]).abs() < 0.001);
assert!((2.0f32.log2() - elems[1]).abs() < 0.001);
assert!((3.0f32.log2() - elems[2]).abs() < 0.001);
assert!((4.0f32.log2() - elems[3]).abs() < 0.001);
Source

pub fn log10(&self) -> Self

Calculates base 10 logarithm of the matrix elements (log10aij).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.log10();
let elems = b.elems();
assert!((1.0f32.log10() - elems[0]).abs() < 0.001);
assert!((2.0f32.log10() - elems[1]).abs() < 0.001);
assert!((3.0f32.log10() - elems[2]).abs() < 0.001);
assert!((4.0f32.log10() - elems[3]).abs() < 0.001);
Source

pub fn sin(&self) -> Self

Calculates sine function for the matrix (sin(A)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.sin();
let elems = b.elems();
assert!((1.0f32.sin() - elems[0]).abs() < 0.001);
assert!((2.0f32.sin() - elems[1]).abs() < 0.001);
assert!((3.0f32.sin() - elems[2]).abs() < 0.001);
assert!((4.0f32.sin() - elems[3]).abs() < 0.001);
Source

pub fn cos(&self) -> Self

Calculates cosine function for the matrix (cos(A)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.cos();
let elems = b.elems();
assert!((1.0f32.cos() - elems[0]).abs() < 0.001);
assert!((2.0f32.cos() - elems[1]).abs() < 0.001);
assert!((3.0f32.cos() - elems[2]).abs() < 0.001);
assert!((4.0f32.cos() - elems[3]).abs() < 0.001);
Source

pub fn tan(&self) -> Self

Calculates tangent function for the matrix (tan(A)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.tan();
let elems = b.elems();
assert!((1.0f32.tan() - elems[0]).abs() < 0.001);
assert!((2.0f32.tan() - elems[1]).abs() < 0.001);
assert!((3.0f32.tan() - elems[2]).abs() < 0.001);
assert!((4.0f32.tan() - elems[3]).abs() < 0.001);
Source

pub fn asin(&self) -> Self

Calculates arcsine function for the matrix (arcsin(A)).

§Examples
let a = matrix![
    [0.25, 0.5],
    [0.75, 1.0]
];
let b = a.asin();
let elems = b.elems();
assert!((0.25f32.asin() - elems[0]).abs() < 0.001);
assert!((0.5f32.asin() - elems[1]).abs() < 0.001);
assert!((0.75f32.asin() - elems[2]).abs() < 0.001);
assert!((1.0f32.asin() - elems[3]).abs() < 0.001);
Source

pub fn acos(&self) -> Self

Calculates arccosine function for the matrix (arccos(A)).

§Examples
let a = matrix![
    [0.25, 0.5],
    [0.75, 1.0]
];
let b = a.acos();
let elems = b.elems();
assert!((0.25f32.acos() - elems[0]).abs() < 0.001);
assert!((0.5f32.acos() - elems[1]).abs() < 0.001);
assert!((0.75f32.acos() - elems[2]).abs() < 0.001);
assert!((1.0f32.acos() - elems[3]).abs() < 0.001);
Source

pub fn atan(&self) -> Self

Calculates arctangent function for the a matrix (arctan(A)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.atan();
let elems = b.elems();
assert!((1.0f32.atan() - elems[0]).abs() < 0.001);
assert!((2.0f32.atan() - elems[1]).abs() < 0.001);
assert!((3.0f32.atan() - elems[2]).abs() < 0.001);
assert!((4.0f32.atan() - elems[3]).abs() < 0.001);
Source

pub fn atan2(&self, b: &Self) -> Self

Calculates arctangent function for the matrix elements and the b matrix elements (arctan(aijbij)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = matrix![
    [5.0, 6.0],
    [7.0, 8.0]
];
let c = a.atan2(&b);
let elems = c.elems();
assert!((1.0f32.atan2(5.0) - elems[0]).abs() < 0.001);
assert!((2.0f32.atan2(6.0) - elems[1]).abs() < 0.001);
assert!((3.0f32.atan2(7.0) - elems[2]).abs() < 0.001);
assert!((4.0f32.atan2(8.0) - elems[3]).abs() < 0.001);
Source

pub fn atan2f(&self, b: f32) -> Self

Calculates arctangent function for the matrix elements and the b scalar (arctan(aijb)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.atan2f(10.5);
let elems = b.elems();
assert!((1.0f32.atan2(10.5) - elems[0]).abs() < 0.001);
assert!((2.0f32.atan2(10.5) - elems[1]).abs() < 0.001);
assert!((3.0f32.atan2(10.5) - elems[2]).abs() < 0.001);
assert!((4.0f32.atan2(10.5) - elems[3]).abs() < 0.001);
Source

pub fn ratan2f(&self, b: f32) -> Self

Calculates arctangent function for the b scalar and the matrix elements (arctan(baij)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.ratan2f(10.5);
let elems = b.elems();
assert!((10.5f32.atan2(1.0) - elems[0]).abs() < 0.001);
assert!((10.5f32.atan2(2.0) - elems[1]).abs() < 0.001);
assert!((10.5f32.atan2(3.0) - elems[2]).abs() < 0.001);
assert!((10.5f32.atan2(4.0) - elems[3]).abs() < 0.001);
Source

pub fn sinh(&self) -> Self

Calculates hyperbolic sine function for the matrix (sinh(A)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.sinh();
let elems = b.elems();
assert!((1.0f32.sinh() - elems[0]).abs() < 0.001);
assert!((2.0f32.sinh() - elems[1]).abs() < 0.001);
assert!((3.0f32.sinh() - elems[2]).abs() < 0.001);
assert!((4.0f32.sinh() - elems[3]).abs() < 0.001);
Source

pub fn cosh(&self) -> Self

Calculates hyperbolic cosine function for the matrix (cosh(A)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.cosh();
let elems = b.elems();
assert!((1.0f32.cosh() - elems[0]).abs() < 0.001);
assert!((2.0f32.cosh() - elems[1]).abs() < 0.001);
assert!((3.0f32.cosh() - elems[2]).abs() < 0.001);
assert!((4.0f32.cosh() - elems[3]).abs() < 0.001);
Source

pub fn asinh(&self) -> Self

Calculates inverse hyperbolic sine function for the matrix (arsinh(A)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.asinh();
let elems = b.elems();
assert!((1.0f32.asinh() - elems[0]).abs() < 0.001);
assert!((2.0f32.asinh() - elems[1]).abs() < 0.001);
assert!((3.0f32.asinh() - elems[2]).abs() < 0.001);
assert!((4.0f32.asinh() - elems[3]).abs() < 0.001);
Source

pub fn acosh(&self) -> Self

Calculates inverse hyperbolic cosine function for the matrix (arcosh(A)).

§Examples
let a = matrix![
    [1.0, 2.0],
    [3.0, 4.0]
];
let b = a.acosh();
let elems = b.elems();
assert!((1.0f32.acosh() - elems[0]).abs() < 0.001);
assert!((2.0f32.acosh() - elems[1]).abs() < 0.001);
assert!((3.0f32.acosh() - elems[2]).abs() < 0.001);
assert!((4.0f32.acosh() - elems[3]).abs() < 0.001);
Source

pub fn atanh(&self) -> Self

Calculates inverse hyperbolic tangent function for the matrix (artanh(A)).

§Examples
let a = matrix![
    [0.25, 0.5],
    [0.75, 1.0]
];
let b = a.atanh();
let elems = b.elems();
assert!((0.25f32.atanh() - elems[0]).abs() < 0.001);
assert!((0.5f32.atanh() - elems[1]).abs() < 0.001);
assert!((0.75f32.atanh() - elems[2]).abs() < 0.001);
assert_eq!(f32::INFINITY, elems[3]);
Source

pub fn signum(&self) -> Self

Calculates signum function for the matrix (sgn(A)).

§Examples
let a = matrix![
    [-2.0, -1.0],
    [1.0, 2.0]
];
let b = a.signum();
assert_eq!(vec![-1.0, -1.0, 1.0, 1.0], b.elems());
Source

pub fn ceil(&self) -> Self

Calculates ceil function for the matrix (ceil(A)).

§Examples
let a = matrix![
    [-2.6, -1.3],
    [1.3, 2.6]
];
let b = a.ceil();
assert_eq!(vec![-2.0, -1.0, 2.0, 3.0], b.elems());
Source

pub fn floor(&self) -> Self

Calculates floor function for the matrix (floor(A)).

§Examples
let a = matrix![
    [-2.6, -1.3],
    [1.3, 2.6]
];
let b = a.floor();
assert_eq!(vec![-3.0, -2.0, 1.0, 2.0], b.elems());
Source

pub fn round(&self) -> Self

Calculates round function for the matrix (round(A)).

§Examples
let a = matrix![
    [-2.6, -1.3],
    [1.3, 2.6]
];
let b = a.round();
assert_eq!(vec![-3.0, -1.0, 1.0, 3.0], b.elems());
Source

pub fn trunc(&self) -> Self

Calculates trunc function for the matrix (trunc(A)).

§Examples
let a = matrix![
    [-2.6, -1.3],
    [1.3, 2.6]
];
let b = a.trunc();
assert_eq!(vec![-2.0, -1.0, 1.0, 2.0], b.elems());
Source

pub fn max(&self, b: &Self) -> Self

Finds maximum values between the matrix elements and the b matrix elements (max(aij,bij)).

§Examples
let a = matrix![
    [-2.0, -1.0],
    [1.0, 2.0]
];
let b = matrix![
    [4.0, 2.0],
    [-2.0, -4.0]
];
let c = a.max(&b);
assert_eq!(vec![4.0, 2.0, 1.0, 2.0], c.elems());
Source

pub fn maxf(&self, b: f32) -> Self

Finds maximum values between the matrix elements and the b scalar (max(aij,b)).

§Examples
let a = matrix![
    [-2.0, -1.0],
    [1.0, 2.0]
];
let b = a.maxf(0.0);
assert_eq!(vec![0.0, 0.0, 1.0, 2.0], b.elems());
Source

pub fn min(&self, b: &Self) -> Self

Finds minimum values between the matrix elements and the b matrix elements (min(aij,bij)).

§Examples
let a = matrix![
    [-2.0, -1.0],
    [1.0, 2.0]
];
let b = matrix![
    [4.0, 2.0],
    [-2.0, -4.0]
];
let c = a.min(&b);
assert_eq!(vec![-2.0, -1.0, -2.0, -4.0], c.elems());
Source

pub fn minf(&self, b: f32) -> Self

Finds minimum values between the matrix elements and the b scalar (min(aij,b)).

§Examples
let a = matrix![
    [-2.0, -1.0],
    [1.0, 2.0]
];
let b = a.minf(0.0);
assert_eq!(vec![-2.0, -1.0, 0.0, 0.0], b.elems());

Trait Implementations§

Source§

impl Add<&Matrix> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Matrix) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<&Matrix> for Matrix

Source§

type Output = Matrix

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Matrix) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<&f32> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &f32) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<&f32> for Matrix

Source§

type Output = Matrix

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &f32) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<Matrix> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Matrix) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<f32> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the + operator.
Source§

fn add(self, rhs: f32) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<f32> for Matrix

Source§

type Output = Matrix

The resulting type after applying the + operator.
Source§

fn add(self, rhs: f32) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for Matrix

Source§

type Output = Matrix

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign<&Matrix> for Matrix

Source§

fn add_assign(&mut self, rhs: &Self)

Performs the += operation. Read more
Source§

impl AddAssign<&f32> for Matrix

Source§

fn add_assign(&mut self, rhs: &f32)

Performs the += operation. Read more
Source§

impl AddAssign<f32> for Matrix

Source§

fn add_assign(&mut self, rhs: f32)

Performs the += operation. Read more
Source§

impl AddAssign for Matrix

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Matrix

Source§

fn clone(&self) -> Matrix

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Matrix

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div<&f32> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &f32) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<&f32> for Matrix

Source§

type Output = Matrix

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &f32) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<f32> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f32) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<f32> for Matrix

Source§

type Output = Matrix

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f32) -> Self::Output

Performs the / operation. Read more
Source§

impl DivAssign<&f32> for Matrix

Source§

fn div_assign(&mut self, rhs: &f32)

Performs the /= operation. Read more
Source§

impl DivAssign<f32> for Matrix

Source§

fn div_assign(&mut self, rhs: f32)

Performs the /= operation. Read more
Source§

impl Mul<&Matrix> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Matrix) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<&Matrix> for Matrix

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Matrix) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<&f32> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &f32) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<&f32> for Matrix

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &f32) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Matrix> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Matrix) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<f32> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f32) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<f32> for Matrix

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f32) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for Matrix

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign<&Matrix> for Matrix

Source§

fn mul_assign(&mut self, rhs: &Self)

Performs the *= operation. Read more
Source§

impl MulAssign<&f32> for Matrix

Source§

fn mul_assign(&mut self, rhs: &f32)

Performs the *= operation. Read more
Source§

impl MulAssign<f32> for Matrix

Source§

fn mul_assign(&mut self, rhs: f32)

Performs the *= operation. Read more
Source§

impl MulAssign for Matrix

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl Neg for &Matrix

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Neg for Matrix

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Sub<&Matrix> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Matrix) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<&Matrix> for Matrix

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Matrix) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<&f32> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &f32) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<&f32> for Matrix

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &f32) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<Matrix> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Matrix) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<f32> for &Matrix

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: f32) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<f32> for Matrix

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: f32) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for Matrix

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign<&Matrix> for Matrix

Source§

fn sub_assign(&mut self, rhs: &Self)

Performs the -= operation. Read more
Source§

impl SubAssign<&f32> for Matrix

Source§

fn sub_assign(&mut self, rhs: &f32)

Performs the -= operation. Read more
Source§

impl SubAssign<f32> for Matrix

Source§

fn sub_assign(&mut self, rhs: f32)

Performs the -= operation. Read more
Source§

impl SubAssign for Matrix

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl Freeze for Matrix

§

impl RefUnwindSafe for Matrix

§

impl Send for Matrix

§

impl Sync for Matrix

§

impl Unpin for Matrix

§

impl UnwindSafe for Matrix

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.