Struct 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>, 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⌋

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, 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));
Source

pub fn det_in_field(&self) -> Result<T, MatrixError>
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) -> Self
where 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) -> Self
where 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) -> Self
where 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) -> Self
where T: One,

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

Source

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));
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, MatrixError>
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 for Matrix<T>

Source§

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 duplicate of the value. Read more
1.0.0 · Source§

const 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: 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§

fn matrix_from(input: Matrix<T>) -> Self

Method for getting a matrix of a new type from a matrix of type Matrix<T>. Read more
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>.

Source§

fn matrix_into(self) -> T

Method for converting a matrix Matrix<T> to another type. Read more
Source§

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

Source§

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>

Source§

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 for Matrix<T>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

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> Freeze for Matrix<T>

§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

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

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

Source§

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

Source§

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.