Struct Matrix

pub struct Matrix<T> { /* private fields */ }
Expand description

Represents a matrix.

Implementations§

§

impl<T> Matrix<T>
where T: One + Zero + Clone + Copy,

pub fn new(rows: usize, cols: usize, init: T) -> Matrix<T>

Create a new matrix of type T with init as the default value for each entry.

§Arguments
  • rows - Row count of matrix
  • cols - Column count of matrix
  • init - The initial value of all entries
§Example
let mat = Matrix::new(3, 4, 9);
println!("{}", mat);

// Output:
// 9 9 9 9
// 9 9 9 9
// 9 9 9 9

pub fn from_vec(rows: usize, cols: usize, vec: Vec<T>) -> Matrix<T>

Create a new matrix from a vec.

§Arguments
  • rows - Row count of matrix
  • cols - Column count of matrix
  • vec - Vector of length rows x cols where vec[i * cols + j] is the entry in row i and column j
§Example
let mat = matrix!{1, 2, 3; 3, 2, 1; 2, 1, 3};
println!("{}", mat);

// Output:
// 1 2 3
// 3 2 1
// 2 1 3

pub fn row_count(&self) -> usize

Get the number of rows

pub fn col_count(&self) -> usize

Get the number of columns

pub fn one(dim: usize) -> Matrix<T>

Create an identity matrix of type T with dimensions dim x dim.

§Arguments
  • dim - The dimensions of a square matrix
§Example
let mat_a: Matrix<u32> = Matrix::one(3);
println!("{}", mat_a);

// Output:
// 1 0 0
// 0 1 0
// 0 0 1

pub fn zero(rows: usize, cols: usize) -> Matrix<T>

Create a zero-matrix of type T.

§Arguments
  • rows - Row count of matrix
  • cols - Column count of matrix
§Example
let mat = Matrix::zero(3, 8);
assert_eq!(mat, Matrix::new(3, 8, 0));

pub fn diag(dim: usize, init: T) -> Matrix<T>

Create a diagonal matrix of type T with entries init.

§Arguments
  • dim - The dimensions of a square matrix
  • init - The initial value of diagonal entries
§Examples
let mat = Matrix::diag(3, 1);
assert_eq!(mat, Matrix::one(3));

pub fn diag_with(dim: usize, entries: &[T]) -> Matrix<T>

Creates a diagonal matrix with dimensions dim x dim and initial entries specified in entries.

pub fn lupdecompose(&self) -> Option<(Matrix<f64>, Vec<usize>)>

pub fn det(&self) -> f64

Calculate the determinant of a square matrix.

§Caution

Calculation may not be exact. Be sure to use round() when calculating the determinant of a integer matrix.

§Example
let mat = matrix!{1, 2, 3; 3, 2, 1; 2, 1, 3};
assert_eq!(mat.det(), -12.0);

pub fn is_square(&self) -> bool

Returns true if the matrix is a square matrix, false otherwise.

§Example
let mat_a: Matrix<i32> = Matrix::one(3);
let mat_b: Matrix<f32> = Matrix::zero(3, 4);
assert_eq!(mat_a.is_square(), true);
assert_eq!(mat_b.is_square(), false);

pub fn transpose(&self) -> Matrix<T>

Transpose a matrix.

§Example
let mat_a = matrix!{1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12};
// 1  2  3  4
// 5  6  7  8
// 9 10 11 12
let mat_b = matrix!{1, 5, 9; 2, 6, 10; 3, 7, 11; 4, 8, 12};
// 1 5  9
// 2 6 10
// 3 7 11
// 4 8 12
assert_eq!(mat_a.transpose(), mat_b);

Trait Implementations§

§

impl<T> Add for &Matrix<T>
where T: Add<Output = T> + Zero + One + Clone + Copy,

Matrices can be added by adding their references. Both matrices need to have the same dimensions.

§Example

let mat_a = Matrix::one(3);
let mat_b = Matrix::one(3);
let mat_c = Matrix::diag(3, 2);
assert_eq!(&mat_a + &mat_b, mat_c)
§

type Output = Matrix<T>

The resulting type after applying the + operator.
§

fn add(self, rhs: &Matrix<T>) -> Self::Output

Performs the + operation. Read more
§

impl<T> AddAssign<&Matrix<T>> for Matrix<T>
where T: Add<Output = T> + Zero + One + Clone + Copy,

§

fn add_assign(&mut self, mat: &Matrix<T>)

Performs the += operation. Read more
§

impl<T: Clone> Clone for Matrix<T>

§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<T: Debug> Debug for Matrix<T>

§

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

Formats the value using the given formatter. Read more
§

impl<T> Display for Matrix<T>
where T: Display,

§

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

Formats the value using the given formatter. Read more
§

impl<T> Div<T> for &Matrix<T>
where T: Div<Output = T> + Zero + One + PartialEq + Copy,

Dividing a matrix is the same as scaling it with the inverse of the divisor.

§Example

let mat_a = Matrix::new(3, 3, 1_f32);
assert_eq!(&mat_a / 2_f32, &mat_a * 0.5_f32);
§

type Output = Matrix<T>

The resulting type after applying the / operator.
§

fn div(self, scalar: T) -> Self::Output

Performs the / operation. Read more
§

impl<T> From<Matrix<T>> for Vector<T>

§

fn from(mat: Matrix<T>) -> Vector<T>

Converts to this type from the input type.
§

impl<T> Index<usize> for Matrix<T>

Indexing matrices returns the corresponding row of the matrix as a slice.

§Example

let mat = Matrix::<u32>::one(3);
assert_eq!(mat[0], vec![1_u32, 0, 0]);
assert_eq!(mat[0][0], 1);
assert_eq!(mat[1][1], 1);
assert_eq!(mat[2][1], 0);
§

type Output = [T]

The returned type after indexing.
§

fn index(&self, idx: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
§

impl<T> IndexMut<usize> for Matrix<T>

Matrices can be manipulated by assigning a value to an indexed matrix.

§Example

let mut mat = Matrix::<u32>::zero(3, 3);
mat[0][0] = 1;
mat[1][1] = 1;
mat[2][2] = 1;
assert_eq!(mat, Matrix::<u32>::one(3));
§

fn index_mut(&mut self, idx: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
§

impl<T> Into<Matrix<T>> for Vector<T>
where T: Zero + One + Copy,

§

fn into(self) -> Matrix<T>

Converts this type into the (usually inferred) input type.
§

impl<T> Inv for Matrix<T>

§

fn inv(self) -> Self::Output

Invert a matrix.

§Example
let mat_a: Matrix<i32> = matrix!{{0,-1,2},{1,2,0},{2,1,0}};
let mat_c: Matrix<i32> = matrix!{{1, 2, 3},{3, 2, 1}}; // not a square matrix
let mat_b = matrix!{{0.0, -1.0/3.0, 2.0/3.0}, {0.0, 2.0/3.0, -1.0/3.0}, {1.0/2.0, 1.0/3.0, -1.0/6.0}};
assert_eq!(mat_a.inv(), Some(mat_b));
assert_eq!(mat_c.inv(), None);
§

type Output = Option<Matrix<f64>>

The result after applying the operator.
§

impl<T> Mul<&Matrix<T>> for &Vector<T>
where T: One + Zero + Copy + Clone,

Vectors can also be multiplied with matrices. The result will be a vector.

§Example

let mat_a = Matrix::<u32>::one(4);
let mat_b = matrix!{1, 2, 3; 4, 4, 3; 2, 1, 3; 4, 1, 2};
let vec_a = vector![4, 5, 6, 7].to_row_vector();
let vec_b = vector![64, 41, 59].to_row_vector();
assert_eq!(&vec_a * &mat_a, vec_a);
assert_eq!(&vec_a * &mat_b, vec_b);
§

type Output = Vector<T>

The resulting type after applying the * operator.
§

fn mul(self, mat: &Matrix<T>) -> Self::Output

Performs the * operation. Read more
§

impl<T> Mul<&Vector<T>> for &Matrix<T>
where T: One + Zero + Copy + Clone,

Matrices can be multiplied with Vectors by multiplying their references. The dimensions of the two objects need to match like with matrix multiplication.

§Example

let v_a = vector![1, 2, 3];
let mat_a = matrix!{1, 2, 3; 4, 5, 6; 7, 8, 9};
let v_b = vector![30, 36, 42].to_row_vector();
assert_eq!(&v_a.to_row_vector() * &mat_a, v_b);
§

type Output = Vector<T>

The resulting type after applying the * operator.
§

fn mul(self, vec: &Vector<T>) -> Self::Output

Performs the * operation. Read more
§

impl<T> Mul<T> for &Matrix<T>
where T: Mul<Output = T> + Copy,

A matrix can be scaled by scaling a reference to a matrix. Each entry will be scaled by the given factor.

§Example

let mat_a = matrix!{1, 2; 3, 4};
let mat_b = matrix!{2, 4; 6, 8};
assert_eq!(&mat_a * 2, mat_b);
§

type Output = Matrix<T>

The resulting type after applying the * operator.
§

fn mul(self, scalar: T) -> Self::Output

Performs the * operation. Read more
§

impl<T> Mul for &Matrix<T>
where T: Zero + One + Clone + Copy,

Matrices can be multiplied by multiplying their references. This is matrix multiplicaiton as described in Matrix multipication, so the left matrix needs to have the same amount of columns as the right has rows.

§Example

let mat_a = matrix!{1, 2, 3, 4; 5, 6, 7, 8};
let mat_b = matrix!{1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12};
let mat_c = matrix!{70, 80, 90; 158, 184, 210};
assert_eq!(&mat_a * &mat_b, mat_c);
§

type Output = Matrix<T>

The resulting type after applying the * operator.
§

fn mul(self, rhs: &Matrix<T>) -> Self::Output

Performs the * operation. Read more
§

impl<T> Neg for &Matrix<T>
where T: Neg + One + Zero + Copy, Vec<T>: FromIterator<<T as Neg>::Output>,

A Matrix can be negated by negating a reference to a matrix. This negates every entry of the matrix.

§Example

let mat_a: Matrix<i32> = matrix!{1, 2; 3, 4};
let mat_b: Matrix<i32> = matrix!{-1, -2; -3, -4};
assert_eq!(-&mat_a, mat_b);
§

type Output = Matrix<T>

The resulting type after applying the - operator.
§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
§

impl<T: PartialEq> PartialEq for Matrix<T>

§

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

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

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

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

impl<T> Sub for &Matrix<T>
where T: Sub<Output = T> + One + Zero + Clone + Copy,

Matrices can be subtracted by subtracting their references. Both matrices need to have the same dimensions.

§Example

let mat_a: Matrix<i32> = Matrix::one(3);
let mat_b: Matrix<i32> = Matrix::one(3);
assert_eq!(&mat_a - &mat_b, Matrix::zero(3, 3));
§

type Output = Matrix<T>

The resulting type after applying the - operator.
§

fn sub(self, rhs: &Matrix<T>) -> Self::Output

Performs the - operation. Read more
§

impl<T> SubAssign<&Matrix<T>> for Matrix<T>
where T: Sub<Output = T> + Zero + One + Copy + Clone,

§

fn sub_assign(&mut self, mat: &Matrix<T>)

Performs the -= operation. Read more
§

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