Struct Matrix

Source
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

Source

pub fn cols(&self) -> usize

Returns the cols of this Matrix.

Source

pub fn rows(&self) -> usize

Returns the rows of this Matrix.

Source

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}
Source

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}
Source

pub fn array_mut(&mut self) -> &mut Vec<f64>

Source§

impl Matrix

Source

pub fn row(&self, index: usize) -> &[f64]

Get a slice to the indexth row.

§Panics

If index >= rows, this function will panic.

Source

pub fn row_mut(&mut self, index: usize) -> &mut [f64]

Get a mutable slice to the indexth row.

§Panics

If index >= rows, this function will panic.

Source

pub fn row_chunks(&self) -> Chunks<'_, f64>

Source

pub fn col(&self, index: usize) -> Vec<f64>

Get an owned Vec of the indexth column.

§Panics

If index >= cols, this function will panic.

Source

pub fn col_mut(&mut self, index: usize) -> Vec<&mut f64>

Get a Vec of mutable entries of the indexth column.

Source§

impl Matrix

Source

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}
Source

pub fn with_value(rows: usize, cols: usize, value: f64) -> Self

Source

pub fn zero(rows: usize, cols: usize) -> Self

Source

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}
Source

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}
Source

pub fn diagonal(array: &[f64]) -> Self

Source§

impl Matrix

Source

pub fn transpose(&mut self)

Transpose a Matrix in place.

Source

pub fn t(&self) -> Self

Return a transposed Matrix. This is done by cloning the original and returning the transposed clone.

Source§

impl Matrix

Source

pub fn append_col(&mut self, col: &[f64])

Source

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

Source

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 Add<f64> for Matrix

Source§

type Output = Matrix

The resulting type after applying the + operator.
Source§

fn add(self, rhs: f64) -> 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<f64> for Matrix

Source§

fn add_assign(&mut self, rhs: f64)

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§

const 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<f64> for Matrix

Source§

type Output = Matrix

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl Div for Matrix

Source§

type Output = Matrix

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl DivAssign<f64> for Matrix

Source§

fn div_assign(&mut self, rhs: f64)

Performs the /= operation. Read more
Source§

impl Index<(usize, usize)> for Matrix

Source§

fn index(&self, index: (usize, usize)) -> &Self::Output

Get entry by (row, col).

Source§

type Output = f64

The returned type after indexing.
Source§

impl IndexMut<(usize, usize)> for Matrix

Source§

fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output

Get mutable entry by (row, col).

Source§

impl Mul<f64> for Matrix

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul for Matrix

Source§

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

§Note

This is a entry by entry multiplication, not a dot product or cross product.

Source§

type Output = Matrix

The resulting type after applying the * operator.
Source§

impl MulAssign<f64> for Matrix

Source§

fn mul_assign(&mut self, rhs: f64)

Performs the *= operation. Read more
Source§

impl PartialEq for Matrix

Source§

fn eq(&self, other: &Matrix) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub<f64> for Matrix

Source§

type Output = Matrix

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: f64) -> 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<f64> for Matrix

Source§

fn sub_assign(&mut self, rhs: f64)

Performs the -= operation. Read more
Source§

impl 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> 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.