Struct matrix_basic::Matrix
source · pub struct Matrix<T: ToMatrix> { /* private fields */ }
Expand description
Implementations§
source§impl<T: ToMatrix> Matrix<T>
impl<T: ToMatrix> 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 rows(&self) -> &Vec<Vec<T>>
pub fn rows(&self) -> &Vec<Vec<T>>
Returns a reference to the rows of a matrix as &Vec<Vec<T>>
.
sourcepub fn submatrix(&self, row: usize, col: usize) -> Self
pub fn submatrix(&self, row: usize, col: usize) -> Self
Returns 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>
pub fn det(&self) -> Result<T, &'static str>
Returns the determinant of a square matrix.
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: One + PartialEq + Div<Output = T>,
pub fn det_in_field(&self) -> Result<T, &'static str>where T: One + PartialEq + Div<Output = T>,
Returns 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_in_field(), Ok(-2.0));
sourcepub fn row_echelon(&self) -> Selfwhere
T: PartialEq + Div<Output = T>,
pub fn row_echelon(&self) -> Selfwhere T: PartialEq + Div<Output = T>,
Returns the row echelon form of a matrix over a field i.e. needs the Div
trait.
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: PartialEq + Div<Output = T>,
pub fn column_echelon(&self) -> Selfwhere T: PartialEq + Div<Output = T>,
Returns the column echelon form of a matrix over a field i.e. needs the Div
trait.
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: PartialEq + Div<Output = T>,
pub fn reduced_row_echelon(&self) -> Selfwhere T: PartialEq + Div<Output = T>,
Returns the reduced row echelon form of a matrix over a field i.e. needs the Div
] trait.
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);
sourcepub fn identity(size: usize) -> Selfwhere
T: One,
pub fn identity(size: usize) -> Selfwhere T: One,
Creates an identity matrix of a given size.
It needs the One
trait.
sourcepub fn trace(self) -> Result<T, &'static str>
pub fn trace(self) -> Result<T, &'static str>
Returns the trace of a square matrix. 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.trace(), Ok(5));
sourcepub fn diagonal_matrix(diag: Vec<T>) -> Self
pub fn diagonal_matrix(diag: Vec<T>) -> Self
Returns a diagonal matrix with a given diagonal.
Example
use matrix_basic::Matrix;
let m = Matrix::diagonal_matrix(vec![1, 2, 3]);
let n = Matrix::from(vec![vec![1, 0, 0], vec![0, 2, 0], vec![0, 0, 3]]).unwrap();
assert_eq!(m, n);
sourcepub fn mul_scalar(&mut self, scalar: T)
pub fn mul_scalar(&mut self, scalar: T)
Multiplies all entries of a matrix by a scalar. Note that it modifies the supplied matrix.
Example
use matrix_basic::Matrix;
let mut m = Matrix::from(vec![vec![1, 2, 0], vec![0, 2, 5], vec![0, 0, 3]]).unwrap();
let n = Matrix::from(vec![vec![2, 4, 0], vec![0, 4, 10], vec![0, 0, 6]]).unwrap();
m.mul_scalar(2);
assert_eq!(m, n);
sourcepub fn inverse(&self) -> Result<Self, &'static str>where
T: Div<Output = T> + One + PartialEq,
pub fn inverse(&self) -> Result<Self, &'static str>where T: Div<Output = T> + One + PartialEq,
Returns the inverse of a square matrix. Throws an error if the 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();
let n = Matrix::from(vec![vec![-2.0, 1.0], vec![1.5, -0.5]]).unwrap();
assert_eq!(m.inverse(), Ok(n));
Trait Implementations§
source§impl<T: MatrixInto<S>, S: ToMatrix> MatrixFrom<T> for Matrix<S>
impl<T: MatrixInto<S>, S: ToMatrix> MatrixFrom<T> for Matrix<S>
Blanket implementation of MatrixFrom
for Matrix<S>
whenever T
(which is actually some)Matrix<U>
implements
MatrixInto<S>
.
source§fn matrix_from(input: T) -> Self
fn matrix_from(input: T) -> Self
source§impl<T: ToMatrix, S: ToMatrix + Into<T>> MatrixInto<T> for Matrix<S>
impl<T: ToMatrix, S: ToMatrix + Into<T>> MatrixInto<T> for Matrix<S>
Blanket implementation of MatrixInto
for converting Matrix<S>
to Matrix<T>
whenever
S
implements [Into(T)
]. Look at matrix_into
.