basic/basic.rs
1use matrijs::{matrix, Matrix};
2
3// From the README.
4fn main() {
5 // The matrix! macro allows for quick initialization.
6
7 // m = | 0.0 1.0 |
8 // |-1.0 0.0 |
9 let mut m = matrix![0.0, 1.0; -1.0, 0.0];
10
11 // Scalar math.
12 m += 1.0;
13 m *= -10.0;
14
15 // You can also create a Matrix manually.
16 let m_expected = Matrix::new(2, 2, &[-10.0, -20.0, 0.0, -10.0]);
17 assert_eq!(m, m_expected);
18
19 // a = | 0.0 1.0 |
20 // | 2.0 3.0 |
21 // b = | 4.0 5.0 6.0 |
22 // | 7.0 8.0 9.0 |
23 let a = matrix![0.0, 1.0; 2.0, 3.0];
24 let b = matrix![4.0, 5.0, 6.0; 7.0, 8.0, 9.0];
25
26 // The dot product of `i` and `a` should be equal to `a` (idempotence).
27 let i = Matrix::identity(2);
28 assert_eq!(i.dot(&a), a);
29
30 assert_eq!(a.dot(&b), matrix![7.0, 8.0, 9.0; 29.0, 34.0, 39.0]);
31
32 // You can append rows and columns to expand what you're working with.
33 let mut ones = Matrix::one(2, 2);
34 ones.append_row(matrix![0.0, 0.0].array());
35
36 assert_eq!(
37 ones,
38 matrix![
39 1.0, 1.0;
40 1.0, 1.0;
41 0.0, 0.0
42 ]
43 );
44
45 // When in doubt, take a look at the shape of the matrix.
46 assert_eq!(ones.shape(), (3, 2)) // 3 rows, 2 columns
47}