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>

source

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⌋

source

pub fn height(&self) -> usize

Return the height of a matrix.

source

pub fn width(&self) -> usize

Return the width of a matrix.

source

pub fn transpose(&self) -> Selfwhere T: Copy,

Return the transpose of a matrix.

source

pub fn rows(&self) -> &Vec<Vec<T>>

Return a reference to the rows of a matrix as &Vec<Vec<T>>.

source

pub fn columns(&self) -> Vec<Vec<T>>where T: Copy,

Return the columns of a matrix as Vec<Vec<T>>.

source

pub fn is_square(&self) -> bool

Return true if a matrix is square and false otherwise.

source

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

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

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

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

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.

source

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

pub fn zero(height: usize, width: usize) -> Selfwhere T: Zero,

Creates a zero matrix of a given size.

source

pub fn identity(size: usize) -> Selfwhere T: Zero + One,

Creates an identity matrix of a given size.

Trait Implementations§

source§

impl<T: Add<Output = T> + Sub + Mul + Copy + Zero> Add<Matrix<T>> for Matrix<T>

§

type Output = Matrix<T>

The resulting type after applying the + operator.
source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
source§

impl<T: Clone + Mul + Add + Sub> Clone for Matrix<T>

source§

fn clone(&self) -> Matrix<T>

Returns a copy 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 + Mul + Add + Sub> Debug for Matrix<T>

source§

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

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

impl<T: Debug + Mul + Add + Sub> Display for Matrix<T>

source§

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

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

impl<T: Mul<Output = T> + Add + Sub + Copy + Zero> Mul<Matrix<T>> for Matrix<T>

§

type Output = Matrix<T>

The resulting type after applying the * operator.
source§

fn mul(self, other: Self) -> Self

Performs the * operation. Read more
source§

impl<T: PartialEq + Mul + Add + Sub> PartialEq<Matrix<T>> for Matrix<T>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Add + Sub<Output = T> + Mul + Copy + Zero> Sub<Matrix<T>> for Matrix<T>

§

type Output = Matrix<T>

The resulting type after applying the - operator.
source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.