Struct matrix_basic::Matrix
source · pub struct Matrix<T: Mul + Add + Sub> { /* private fields */ }
Expand description
A generic matrix struct (over any type with addition, substraction
and multiplication defined on it).
Look at from
to see examples.
Implementations§
source§impl<T: Mul + Add + Sub> Matrix<T>
impl<T: Mul + Add + Sub> Matrix<T>
sourcepub fn from(entries: Vec<Vec<T>>) -> Result<Matrix<T>, &'static str>
pub fn from(entries: Vec<Vec<T>>) -> Result<Matrix<T>, &'static str>
Creates a matrix from given 2D “array” in a Vec<Vec<T>>
form.
It’ll throw an error if all the given rows aren’t of the same size.
Example
use matrix_basic::Matrix;
let m = Matrix::from(vec![vec![1,2,3], vec![4,5,6]]);
will create the following matrix:
⌈1,2,3⌉
⌊4,5,6⌋
sourcepub fn columns(&self) -> Vec<Vec<T>>where
T: Copy,
pub fn columns(&self) -> Vec<Vec<T>>where T: Copy,
Return the columns of a matrix as Vec<Vec<T>>
.
sourcepub fn submatrix(&self, row: usize, col: usize) -> Selfwhere
T: Copy,
pub fn submatrix(&self, row: usize, col: usize) -> Selfwhere T: Copy,
Return a matrix after removing the provided row and column from it. Note: Row and column numbers are 0-indexed.
Example
use matrix_basic::Matrix;
let m = Matrix::from(vec![vec![1,2,3],vec![4,5,6]]).unwrap();
let n = Matrix::from(vec![vec![5,6]]).unwrap();
assert_eq!(m.submatrix(0,0),n);
sourcepub fn det(&self) -> Result<T, &'static str>where
T: Copy + Mul<Output = T> + Sub<Output = T> + Zero,
pub fn det(&self) -> Result<T, &'static str>where T: Copy + Mul<Output = T> + Sub<Output = T> + Zero,
Return the determinant of a square matrix. This method additionally requires Zero
,
One
and Copy
traits. Also, we need that the Mul
and Add
operations
return the same type T
.
It’ll throw an error if the provided matrix isn’t square.
Example
use matrix_basic::Matrix;
let m = Matrix::from(vec![vec![1,2],vec![3,4]]).unwrap();
assert_eq!(m.det(),Ok(-2));
Trait Implementations§
source§impl<T: PartialEq + Mul + Add + Sub> PartialEq<Matrix<T>> for Matrix<T>
impl<T: PartialEq + Mul + Add + Sub> PartialEq<Matrix<T>> for Matrix<T>
impl<T: Mul + Add + Sub> StructuralPartialEq for Matrix<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for Matrix<T>where T: RefUnwindSafe,
impl<T> Send for Matrix<T>where T: Send,
impl<T> Sync for Matrix<T>where T: Sync,
impl<T> Unpin for Matrix<T>where T: Unpin,
impl<T> UnwindSafe for Matrix<T>where T: UnwindSafe,
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