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>, MatrixError>
pub fn from(entries: Vec<Vec<T>>) -> Result<Matrix<T>, MatrixError>
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, MatrixError>
pub fn det(&self) -> Result<T, MatrixError>
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, MatrixError>
pub fn det_in_field(&self) -> Result<T, MatrixError>
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) -> Self
pub fn row_echelon(&self) -> Self
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) -> Self
pub fn column_echelon(&self) -> Self
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) -> Self
pub fn reduced_row_echelon(&self) -> Self
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, MatrixError>
pub fn trace(self) -> Result<T, MatrixError>
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, MatrixError>
pub fn inverse(&self) -> Result<Self, MatrixError>
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: ToMatrix, S: ToMatrix + From<T>> MatrixFrom<T> for Matrix<S>
Blanket implementation of MatrixFrom<T>
for converting Matrix<S>
to Matrix<T>
whenever
S
implements [From(T)
]. Look at matrix_into
.
impl<T: ToMatrix, S: ToMatrix + From<T>> MatrixFrom<T> for Matrix<S>
Blanket implementation of MatrixFrom<T>
for converting Matrix<S>
to Matrix<T>
whenever
S
implements [From(T)
]. Look at matrix_into
.
Source§impl<T: MatrixFrom<S>, S: ToMatrix> MatrixInto<T> for Matrix<S>
Blanket implementation of MatrixInto<T>
for Matrix<S>
whenever T
(which is actually some)Matrix<U>
implements MatrixFrom<S>
.
impl<T: MatrixFrom<S>, S: ToMatrix> MatrixInto<T> for Matrix<S>
Blanket implementation of MatrixInto<T>
for Matrix<S>
whenever T
(which is actually some)Matrix<U>
implements MatrixFrom<S>
.