Struct Matrix

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

Source

pub fn new(data: [T; LEN]) -> Result<Self, NewMatrixError>

Creates a new Matrix from given dimensions and flat data.

§Errors
  • NewMatrixError::IllegalGenerics if M * N != LEN
§Examples
use qmat::prelude::*;
let mat = Matrix::<_, 3, 2, 6>::new([4, 2, 10, 5, 5, 6]);
assert!(mat.is_ok());
Source

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

Source

pub fn rows(&self) -> usize

Returns the constant number of rows, M.

Source

pub fn cols(&self) -> usize

Returns the constant number of columns, N.

Source

pub fn vol(&self) -> usize

The number of elements in the matrix (i.e. the number of rows times the number of cols).

Source

pub fn iter(&self) -> Iter<'_, T, M, N, LEN>

Iterates over immutable references to all of the elements of a matrix.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

TODO: implement myself. Currently just passes the iter_mut call to the underlying array.

Source

pub fn into_iter_row(self) -> IntoIterRow<T, M, N, LEN>

Creates an iterator for the matrix’s rows by moving matrix ownership.

Source

pub fn iter_row(&self) -> IterRow<'_, T, M, N, LEN>

Iterates over rows, using immutable references to the original array’s data.

Source

pub fn iter_row_mut(&mut self)

Unimplemented method. Iterates over rows, using mutable references to the original array’s data.

Source

pub fn into_iter_col(self) -> IntoIterCol<T, M, N, LEN>

Creates an iterator for the matrix’s columns by moving matrix ownership.

Source

pub fn iter_col(&self) -> IterCol<'_, T, M, N, LEN>

Iterates over columns, using immutable references to the original array’s data.

Source

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>
where T: Default + Copy,

Source

pub fn empty() -> Result<Self, NewMatrixError>

§Errors
  • NewMatrixError::IllegalGenerics if M * N != LEN
§Examples
use qmat::prelude::*;
let mat = Matrix::<i32, 5, 5, 25>::empty().unwrap();
println!("{:?}", mat);
Source

pub fn from_rows(data: [[T; N]; M]) -> Result<Self, NewMatrixError>

Errors

  • Same as Matrix::new
Source

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);
Source

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>
where T: Default + Copy + Mul + Sum<<T as Mul>::Output>,

Source

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) where M is rows in self
§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>
where T: Default + Copy + Mul<Output = T>,

Source

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> Matrix<T, M, 1, M>
where T: Copy + Mul + Sum<<T as Mul>::Output>,

Source

pub fn dot(&self, other: &Self) -> T

§Examples
use qmat::prelude::*;

let vec1 = Matrix::<i32, 3, 1, 3>::new([2, 4, 3]).unwrap();
let vec2 = Matrix::<i32, 3, 1, 3>::new([1, 3, 3]).unwrap();

assert_eq!(vec1.dot(&vec2), 23);
Source§

impl<T, const M: usize, const LEN: usize> Matrix<T, M, M, LEN>
where T: Default + Copy,

Source

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>
where T: Num + Copy,

Source

pub fn det(&self) -> T

Returns the determinant of a matrix.

Source§

impl<T, const M: usize, const LEN: usize> Matrix<T, M, M, LEN>
where T: Num + Copy + Default + Identity,

Source

pub fn inverse(&self) -> Result<Self, MatrixOperationError>

§Errors
  • MatrixOperationError::InvalidDeterminant is self.det() == 0.
§Panics
  • If it fails to create an empty matrix.
  • When trying to get the inverse of a matrix that isn’t 2x2. (Currently unimplemented.)

Trait Implementations§

Source§

impl<T, const M: usize, const N: usize, const LEN: usize> Add for Matrix<T, M, N, LEN>
where T: Add<Output = T> + Default + Copy,

Source§

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§

type Output = Matrix<T, M, N, LEN>

The resulting type after applying the + operator.
Source§

impl<T: Clone, const M: usize, const N: usize, const LEN: usize> Clone for Matrix<T, M, N, LEN>

Source§

fn clone(&self) -> Matrix<T, M, N, LEN>

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<T: Debug, const M: usize, const N: usize, const LEN: usize> Debug for Matrix<T, M, N, LEN>

Source§

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

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

impl<T: Default + Copy + Identity, const M: usize, const LEN: usize> Identity for Matrix<T, M, M, LEN>

Source§

fn identity() -> Self

§Panics
  • If it fails to make an empty matrix
Source§

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

§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§

type Output = T

The returned type after indexing.
Source§

impl<T, const M: usize, const N: usize, const LEN: usize> Index<Position> for Matrix<T, M, N, LEN>

Source§

type Output = T

The returned type after indexing.
Source§

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

Performs the indexing (container[index]) operation. Read more
Source§

impl<T, const M: usize, const N: usize, const LEN: usize> IndexMut<[usize; 2]> for Matrix<T, M, N, LEN>

Source§

fn index_mut(&mut self, pos: [usize; 2]) -> &mut Self::Output

§Examples
use qmat::prelude::*;
let mut mat = Matrix::<_, 2, 3, 6>::new([0, 1, 2, 3, 4, 5]).unwrap();
mat[[0, 2]] = 12;
assert_eq!(mat[[0, 2]], 12);
Source§

impl<T, const M: usize, const N: usize, const LEN: usize> IndexMut<Position> for Matrix<T, M, N, LEN>

Source§

fn index_mut(&mut self, pos: Position) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T, const M: usize, const N: usize, const LEN: usize> IntoIterator for Matrix<T, M, N, LEN>
where T: Clone,

Source§

type IntoIter = IntoIter<T, M, N, LEN>

Which kind of iterator are we turning this into?
Source§

type Item = T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq, const M: usize, const N: usize, const LEN: usize> PartialEq for Matrix<T, M, N, LEN>

Source§

fn eq(&self, other: &Matrix<T, M, N, LEN>) -> bool

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

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<T, const M: usize, const N: usize, const LEN: usize> Serialize for Matrix<T, M, N, LEN>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T, const M: usize, const N: usize, const LEN: usize> Sub for Matrix<T, M, N, LEN>
where T: Sub<Output = T> + Default + Copy,

Source§

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

type Output = Matrix<T, M, N, LEN>

The resulting type after applying the - operator.
Source§

impl<T: Copy, const M: usize, const N: usize, const LEN: usize> Copy for Matrix<T, M, N, LEN>

Source§

impl<T, const M: usize, const N: usize, const LEN: usize> StructuralPartialEq for Matrix<T, M, N, LEN>

Auto Trait Implementations§

§

impl<T, const M: usize, const N: usize, const LEN: usize> Freeze for Matrix<T, M, N, LEN>
where T: Freeze,

§

impl<T, const M: usize, const N: usize, const LEN: usize> RefUnwindSafe for Matrix<T, M, N, LEN>
where T: RefUnwindSafe,

§

impl<T, const M: usize, const N: usize, const LEN: usize> Send for Matrix<T, M, N, LEN>
where T: Send,

§

impl<T, const M: usize, const N: usize, const LEN: usize> Sync for Matrix<T, M, N, LEN>
where T: Sync,

§

impl<T, const M: usize, const N: usize, const LEN: usize> Unpin for Matrix<T, M, N, LEN>
where T: Unpin,

§

impl<T, const M: usize, const N: usize, const LEN: usize> UnwindSafe for Matrix<T, M, N, LEN>
where T: UnwindSafe,

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.