pub struct Matrix<T, const M: usize, const N: usize, const LEN: usize> { /* private fields */ }
Expand description
A matrix of M
rows and N
columns.
LEN
is the length of the internal array data: [T; LEN]
that stores all the elements (i.e. LEN
= M
* N
).
Implementations§
Source§impl<T, const M: usize, const N: usize, const LEN: usize> Matrix<T, M, N, LEN>
impl<T, const M: usize, const N: usize, const LEN: usize> Matrix<T, M, N, LEN>
Sourcepub fn new(data: [T; LEN]) -> Result<Self, NewMatrixError>
pub fn new(data: [T; LEN]) -> Result<Self, NewMatrixError>
Sourcepub fn as_flat_array(&self) -> &[T; LEN]
pub fn as_flat_array(&self) -> &[T; LEN]
Returns an immutable reference to the underlying 1-dimensional data.
Flattened such that the matrix
[a, b, c]
[d, e, f]
becomes [a, b, c, d, e, f]
.
Sourcepub fn vol(&self) -> usize
pub fn vol(&self) -> usize
The number of elements in the matrix (i.e. the number of rows times the number of cols).
Sourcepub fn iter(&self) -> Iter<'_, T, M, N, LEN> ⓘ
pub fn iter(&self) -> Iter<'_, T, M, N, LEN> ⓘ
Iterates over immutable references to all of the elements of a matrix.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
TODO: implement myself.
Currently just passes the iter_mut
call to the underlying array.
Sourcepub fn into_iter_row(self) -> IntoIterRow<T, M, N, LEN> ⓘ
pub fn into_iter_row(self) -> IntoIterRow<T, M, N, LEN> ⓘ
Creates an iterator for the matrix’s rows by moving matrix ownership.
Sourcepub fn iter_row(&self) -> IterRow<'_, T, M, N, LEN> ⓘ
pub fn iter_row(&self) -> IterRow<'_, T, M, N, LEN> ⓘ
Iterates over rows, using immutable references to the original array’s data.
Sourcepub fn iter_row_mut(&mut self)
pub fn iter_row_mut(&mut self)
Unimplemented method. Iterates over rows, using mutable references to the original array’s data.
Sourcepub fn into_iter_col(self) -> IntoIterCol<T, M, N, LEN> ⓘ
pub fn into_iter_col(self) -> IntoIterCol<T, M, N, LEN> ⓘ
Creates an iterator for the matrix’s columns by moving matrix ownership.
Sourcepub fn iter_col(&self) -> IterCol<'_, T, M, N, LEN> ⓘ
pub fn iter_col(&self) -> IterCol<'_, T, M, N, LEN> ⓘ
Iterates over columns, using immutable references to the original array’s data.
Sourcepub fn iter_col_mut(&mut self)
pub fn iter_col_mut(&mut self)
Iterates over columns, using mutable references to the original array’s data. Unimplemented method.
Source§impl<T, const M: usize, const N: usize, const LEN: usize> Matrix<T, M, N, LEN>
impl<T, const M: usize, const N: usize, const LEN: usize> Matrix<T, M, N, LEN>
Sourcepub fn empty() -> Result<Self, NewMatrixError>
pub fn empty() -> Result<Self, NewMatrixError>
Sourcepub fn get_row(&self, row: usize) -> Matrix<T, N, 1, N>
pub fn get_row(&self, row: usize) -> Matrix<T, N, 1, N>
Gets a specific row of the matrix.
§Panics
- When it fails to make an empty matrix.
§Examples
use qmat::prelude::*;
let mat = Matrix::<_, 2, 2, 4>::new([0, 1, 2, 3]).unwrap(); // [[0, 1], [2, 3]]
assert_eq!(mat.get_row(0)[[0, 0]], 0);
assert_eq!(mat.get_row(0)[[0, 1]], 1);
assert_eq!(mat.get_row(1)[[0, 0]], 2);
assert_eq!(mat.get_row(1)[[0, 1]], 3);
Sourcepub fn get_col(&self, col: usize) -> Matrix<T, M, 1, M>
pub fn get_col(&self, col: usize) -> Matrix<T, M, 1, M>
Gets a specific column of the matrix.
§Panics
- When it fails to make an empty matrix.
§Examples
use qmat::prelude::*;
let mat = Matrix::<_, 2, 2, 4>::new([0, 1, 2, 3]).unwrap(); // [[0, 1], [2, 3]]
assert_eq!(mat.get_col(0)[[0, 0]], 0);
assert_eq!(mat.get_col(0)[[0, 1]], 2);
assert_eq!(mat.get_col(1)[[0, 0]], 1);
assert_eq!(mat.get_col(1)[[0, 1]], 3);
Source§impl<T, const M: usize, const N: usize, const LEN: usize> Matrix<T, M, N, LEN>
impl<T, const M: usize, const N: usize, const LEN: usize> Matrix<T, M, N, LEN>
Sourcepub fn multiply<const O: usize, const Q: usize, const RES_LEN: usize>(
&self,
other: &Matrix<T, N, O, Q>,
) -> Matrix<T, M, O, RES_LEN>
pub fn multiply<const O: usize, const Q: usize, const RES_LEN: usize>( &self, other: &Matrix<T, N, O, Q>, ) -> Matrix<T, M, O, RES_LEN>
Turbofish ::<O, Q, RES_LEN>
where
O
is the number of columns in the other matrix,Q
is the array length in the other matrix,RES_LEN
is the number of elements in the resulting matrix (M
*O
) whereM
is rows inself
§Panics
- When it fails to make an empty matrix.
§Examples
use qmat::prelude::*;
let a = Matrix::<_, 2, 2, 4>::new([3, 4, 2, 1]).unwrap();
let b = Matrix::<_, 2, 2, 4>::new([1, 5, 3, 7]).unwrap();
let output = a.multiply::<2, 4, 4>(&b);
assert_eq!(output[[0, 0]], 15);
assert_eq!(output[[0, 1]], 43);
assert_eq!(output[[1, 0]], 5);
assert_eq!(output[[1, 1]], 17);
Source§impl<T, const M: usize, const N: usize, const LEN: usize> Matrix<T, M, N, LEN>
impl<T, const M: usize, const N: usize, const LEN: usize> Matrix<T, M, N, LEN>
Sourcepub fn mul_scalar(&self, scalar: T) -> Self
pub fn mul_scalar(&self, scalar: T) -> Self
Multiplies a matrix with and scalar value. Iterates over all elements in the matrix and multiplies it by the given scalar.
§Examples
use qmat::prelude::*;
let mat = matrix!{[[0, 1, 2]]};
let res = mat.mul_scalar(3);
assert_eq!(res[[0, 0]], 0);
assert_eq!(res[[0, 1]], 3);
assert_eq!(res[[0, 2]], 6);
§Panics
- When it fails to make an empty matrix.
Source§impl<T, const M: usize, const LEN: usize> Matrix<T, M, M, LEN>
impl<T, const M: usize, const LEN: usize> Matrix<T, M, M, LEN>
Sourcepub fn diag(val: T) -> Self
pub fn diag(val: T) -> Self
Creates a new matrix such that every value in the diagonal from the top left ([0, 0]
) to the bottom left ([M, M]
) are equal to val
.
§Examples
use qmat::prelude::Matrix;
let mat: Matrix<i32, 3, 3, 9> = Matrix::diag(3);
assert_eq!(mat[[0, 0]], 3);
assert_eq!(mat[[1, 1]], 3);
assert_eq!(mat[[2, 2]], 3);
§Panics
- If it fails to create an empty matrix.
Source§impl<T, const M: usize, const N: usize, const LEN: usize> Matrix<T, M, N, LEN>
impl<T, const M: usize, const N: usize, const LEN: usize> Matrix<T, M, N, LEN>
Sourcepub fn det(&self) -> T
pub fn det(&self) -> T
Returns the determinant of a matrix.
Trait Implementations§
Source§impl<T, const M: usize, const N: usize, const LEN: usize> Add for Matrix<T, M, N, LEN>
impl<T, const M: usize, const N: usize, const LEN: usize> Add for Matrix<T, M, N, LEN>
Source§fn add(self, rhs: Self) -> Self::Output
fn add(self, rhs: Self) -> Self::Output
Returns a matrix where element i
is lhs[i] + rhs[i]
.
§Examples
use qmat::prelude::*;
let lhs = matrix!([[3, 17], [128, 5]]);
let rhs = matrix!([[63, 12], [4, 3]]);
let added = lhs + rhs;
assert_eq!(added[[0, 0]], 66); // 3 + 63
assert_eq!(added[[0, 1]], 29); // 17 + 12
assert_eq!(added[[1, 0]], 132); // 128 + 4
assert_eq!(added[[1, 1]], 8); // 5 + 3
Source§impl<T: Clone, const M: usize, const N: usize, const LEN: usize> Clone for Matrix<T, M, N, LEN>
impl<T: Clone, const M: usize, const N: usize, const LEN: usize> Clone for Matrix<T, M, N, LEN>
Source§impl<T: Debug, const M: usize, const N: usize, const LEN: usize> Debug for Matrix<T, M, N, LEN>
impl<T: Debug, const M: usize, const N: usize, const LEN: usize> Debug for Matrix<T, M, N, LEN>
Source§impl<T: Default + Copy + Identity, const M: usize, const LEN: usize> Identity for Matrix<T, M, M, LEN>
impl<T: Default + Copy + Identity, const M: usize, const LEN: usize> Identity for Matrix<T, M, M, LEN>
Source§impl<T, const M: usize, const N: usize, const LEN: usize> Index<[usize; 2]> for Matrix<T, M, N, LEN>
impl<T, const M: usize, const N: usize, const LEN: usize> Index<[usize; 2]> for Matrix<T, M, N, LEN>
Source§fn index(&self, pos: [usize; 2]) -> &Self::Output
fn index(&self, pos: [usize; 2]) -> &Self::Output
§Examples
use qmat::prelude::*;
let mat = Matrix::<_, 2, 3, 6>::new([0, 1, 2, 3, 4, 5]).unwrap();
assert_eq!(mat[[0, 0]], 0); // [0, 0] => 0*3 + 0 = 0
assert_eq!(mat[[0, 1]], 1); // [0, 1] => 0*3 + 1 = 1
assert_eq!(mat[[0, 2]], 2); // [0, 2] => 0*3 + 2 = 2
assert_eq!(mat[[1, 0]], 3); // [0, 0] => 1*3 + 0 = 3
assert_eq!(mat[[1, 1]], 4); // [1, 1] => 1*3 + 1 = 4
assert_eq!(mat[[1, 2]], 5); // [2, 2] => 1*3 + 2 = 5
Source§impl<T, const M: usize, const N: usize, const LEN: usize> Index<Position> for Matrix<T, M, N, LEN>
impl<T, const M: usize, const N: usize, const LEN: usize> Index<Position> for Matrix<T, M, N, LEN>
Source§impl<T, const M: usize, const N: usize, const LEN: usize> IndexMut<[usize; 2]> for Matrix<T, M, N, LEN>
impl<T, const M: usize, const N: usize, const LEN: usize> IndexMut<[usize; 2]> for Matrix<T, M, N, LEN>
Source§impl<T, const M: usize, const N: usize, const LEN: usize> IndexMut<Position> for Matrix<T, M, N, LEN>
impl<T, const M: usize, const N: usize, const LEN: usize> IndexMut<Position> for Matrix<T, M, N, LEN>
Source§impl<T, const M: usize, const N: usize, const LEN: usize> IntoIterator for Matrix<T, M, N, LEN>where
T: Clone,
impl<T, const M: usize, const N: usize, const LEN: usize> IntoIterator for Matrix<T, M, N, LEN>where
T: Clone,
Source§impl<T: PartialEq, const M: usize, const N: usize, const LEN: usize> PartialEq for Matrix<T, M, N, LEN>
impl<T: PartialEq, const M: usize, const N: usize, const LEN: usize> PartialEq for Matrix<T, M, N, LEN>
Source§impl<T, const M: usize, const N: usize, const LEN: usize> Sub for Matrix<T, M, N, LEN>
impl<T, const M: usize, const N: usize, const LEN: usize> Sub for Matrix<T, M, N, LEN>
Source§fn sub(self, rhs: Self) -> Self::Output
fn sub(self, rhs: Self) -> Self::Output
§Examples
use qmat::prelude::*;
let lhs = matrix!([[3, 17], [128, 5]]);
let rhs = matrix!([[63, 12], [4, 3]]);
let subbed = lhs - rhs;
assert_eq!(subbed[[0, 0]], -60); // 3 - 63
assert_eq!(subbed[[0, 1]], 5); // 17 - 12
assert_eq!(subbed[[1, 0]], 124); // 128 - 4
assert_eq!(subbed[[1, 1]], 2); // 5 - 3