Struct matrix_basic::Matrix

source ·
pub struct Matrix<T: ToMatrix> { /* private fields */ }
Expand description

A generic matrix struct (over any type with Add, Sub, Mul, Zero, Neg and Copy implemented). Look at from to see examples.

Implementations§

source§

impl<T: ToMatrix> 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

Returns the height of a matrix.

source

pub fn width(&self) -> usize

Returns the width of a matrix.

source

pub fn transpose(&self) -> Self

Returns the transpose of a matrix.

source

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

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

source

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

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

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

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

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

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.

source

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

pub fn zero(height: usize, width: usize) -> Self

Creates a zero matrix of a given size.

source

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

Creates an identity matrix of a given size. It needs the One trait.

source

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

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

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

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: Mul<Output = T> + ToMatrix> Add<Matrix<T>> for Matrix<T>

§

type Output = Matrix<T>

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl<T: Clone + ToMatrix> 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 + ToMatrix> 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 + ToMatrix> Display for Matrix<T>

source§

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

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

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

Method for getting a matrix from another matrix of type Matrix<T>. Read more
source§

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.

source§

fn matrix_into(self) -> Matrix<T>

Method for converting a matrix into a matrix of type Matrix<T>. Read more
source§

impl<T: Mul<Output = T> + ToMatrix> Mul<Matrix<T>> for Matrix<T>

§

type Output = Matrix<T>

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl<T: ToMatrix> Neg for Matrix<T>

§

type Output = Matrix<T>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<T: PartialEq + ToMatrix> 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: ToMatrix> Sub<Matrix<T>> for Matrix<T>

§

type Output = Matrix<T>

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl<T: ToMatrix> 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.