pub struct Matrix { /* private fields */ }
Expand description
A 2-dimensional matrix.
§Example
use matrijs::Matrix;
let mut m = Matrix::new(2, 2, &[0.0, 1.0, -1.0, 0.0]);
m += 1.0;
assert_eq!(m, Matrix::new(2, 2, &[1.0, 2.0, 0.0, 1.0]));
§Note
The implementation is row-major. That is to say that the entries are stored as contiguous rows in the internal representation.
Implementations§
Source§impl Matrix
impl Matrix
Sourcepub fn shape(&self) -> (usize, usize)
pub fn shape(&self) -> (usize, usize)
Returns the shape of this Matrix
as a tuple of (rows
, cols
).
Examples found in repository?
examples/basic.rs (line 46)
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}
Sourcepub fn array(&self) -> &[f64]
pub fn array(&self) -> &[f64]
Returns a reference to the internal array of this Matrix
.
Examples found in repository?
examples/basic.rs (line 34)
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}
pub fn array_mut(&mut self) -> &mut Vec<f64>
Source§impl Matrix
impl Matrix
Source§impl Matrix
impl Matrix
Sourcepub fn new(rows: usize, cols: usize, array: &[f64]) -> Self
pub fn new(rows: usize, cols: usize, array: &[f64]) -> Self
Examples found in repository?
examples/basic.rs (line 16)
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}
pub fn with_value(rows: usize, cols: usize, value: f64) -> Self
pub fn zero(rows: usize, cols: usize) -> Self
Sourcepub fn one(rows: usize, cols: usize) -> Self
pub fn one(rows: usize, cols: usize) -> Self
Examples found in repository?
examples/basic.rs (line 33)
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}
Sourcepub fn identity(size: usize) -> Self
pub fn identity(size: usize) -> Self
Examples found in repository?
examples/basic.rs (line 27)
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}
pub fn diagonal(array: &[f64]) -> Self
Source§impl Matrix
impl Matrix
pub fn append_col(&mut self, col: &[f64])
Sourcepub fn append_row(&mut self, row: &[f64])
pub fn append_row(&mut self, row: &[f64])
Examples found in repository?
examples/basic.rs (line 34)
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}
Source§impl Matrix
impl Matrix
Sourcepub fn dot(&self, rhs: &Self) -> Self
pub fn dot(&self, rhs: &Self) -> Self
The dot product between two matrices.
From m × n matrix A and n × p matrix B, we can calculate the dot product AB = C where C becomes an m × p matrix. The following approach is used.
c_ij = a_i1 * b_1j + ... + a_in * b_nj
n
c_ij = Σ a_ik * b_kj
k=1
§Example
use matrijs::Matrix;
let a = Matrix::new(2, 2, &[0.0, 1.0, 2.0, 3.0]);
let b = Matrix::new(2, 3, &[4.0, 5.0, 6.0, 7.0, 8.0, 9.0]);
// Multiplication of `i` and `a` should be idempotent.
let i = Matrix::identity(2);
assert_eq!(i.dot(&a), a);
assert_eq!(a.dot(&b), Matrix::new(2, 3, &[7.0, 8.0, 9.0, 29.0, 34.0, 39.0]));
§Panics
Of course, the dot product can only be calculated for matrices where the ‘inner’ size is
the same (i.e., n in m × n dot n × p
).
ⓘ
let e = Matrix::one(3, 4);
let f = Matrix::one(3, 2);
e.dot(&f);
Examples found in repository?
examples/basic.rs (line 28)
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}
Trait Implementations§
Source§impl AddAssign<f64> for Matrix
impl AddAssign<f64> for Matrix
Source§fn add_assign(&mut self, rhs: f64)
fn add_assign(&mut self, rhs: f64)
Performs the
+=
operation. Read moreSource§impl DivAssign<f64> for Matrix
impl DivAssign<f64> for Matrix
Source§fn div_assign(&mut self, rhs: f64)
fn div_assign(&mut self, rhs: f64)
Performs the
/=
operation. Read moreSource§impl MulAssign<f64> for Matrix
impl MulAssign<f64> for Matrix
Source§fn mul_assign(&mut self, rhs: f64)
fn mul_assign(&mut self, rhs: f64)
Performs the
*=
operation. Read moreSource§impl SubAssign<f64> for Matrix
impl SubAssign<f64> for Matrix
Source§fn sub_assign(&mut self, rhs: f64)
fn sub_assign(&mut self, rhs: f64)
Performs the
-=
operation. Read moreimpl StructuralPartialEq for Matrix
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more