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. This uses basic recursive algorithm using cofactor-minor.
See det_in_field for faster determinant calculation in fields.
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));sourcepub fn det_in_field(&self) -> Result<T, &'static str>where
T: Copy + Mul<Output = T> + Sub<Output = T> + Zero + One + PartialEq + Div<Output = T>,
pub fn det_in_field(&self) -> Result<T, &'static str>where T: Copy + Mul<Output = T> + Sub<Output = T> + Zero + One + PartialEq + Div<Output = T>,
Return the determinant of a square matrix over a field i.e. needs One and Div traits.
See det for determinants in rings.
This method uses row reduction as is much faster.
It’ll throw an error if the provided matrix isn’t square.
Example
use matrix_basic::Matrix;
let m = Matrix::from(vec![vec![1.0,2.0],vec![3.0,4.0]]).unwrap();
assert_eq!(m.det(),Ok(-2.0));sourcepub fn row_echelon(&self) -> Selfwhere
T: Copy + Mul<Output = T> + Sub<Output = T> + Zero + One + PartialEq + Div<Output = T>,
pub fn row_echelon(&self) -> Selfwhere T: Copy + Mul<Output = T> + Sub<Output = T> + Zero + One + PartialEq + Div<Output = T>,
Returns the row echelon form of a matrix over a field i.e. needs One and Div traits.
Example
use matrix_basic::Matrix;
let m = Matrix::from(vec![vec![1.0,2.0,3.0],vec![3.0,4.0,5.0]]).unwrap();
let n = Matrix::from(vec![vec![1.0,2.0,3.0], vec![0.0,-2.0,-4.0]]).unwrap();
assert_eq!(m.row_echelon(),n);sourcepub fn column_echelon(&self) -> Selfwhere
T: Copy + Mul<Output = T> + Sub<Output = T> + Zero + One + PartialEq + Div<Output = T>,
pub fn column_echelon(&self) -> Selfwhere T: Copy + Mul<Output = T> + Sub<Output = T> + Zero + One + PartialEq + Div<Output = T>,
Returns the column echelon form of a matrix over a field i.e. needs One and Div traits.
It’s just the transpose of the row echelon form of the transpose.
See row_echelon and transpose.
sourcepub fn reduced_row_echelon(&self) -> Selfwhere
T: Copy + Mul<Output = T> + Sub<Output = T> + Zero + One + PartialEq + Div<Output = T> + Display + Debug,
pub fn reduced_row_echelon(&self) -> Selfwhere T: Copy + Mul<Output = T> + Sub<Output = T> + Zero + One + PartialEq + Div<Output = T> + Display + Debug,
Returns the reduced row echelon form of a matrix over a field i.e. needs One and Div traits.
Example
use matrix_basic::Matrix;
let m = Matrix::from(vec![vec![1.0,2.0,3.0],vec![3.0,4.0,5.0]]).unwrap();
let n = Matrix::from(vec![vec![1.0,2.0,3.0], vec![0.0,1.0,2.0]]).unwrap();
assert_eq!(m.reduced_row_echelon(),n);