Struct mtrx::Matrix[][src]

pub struct Matrix<T: MatrixCell<T>, const R: usize, const C: usize> {
    pub inner: [[T; C]; R],
}

Uses const generics to represent a mathematical matrix

Fields

inner: [[T; C]; R]

Implementations

impl<T: MatrixCell<T>, const R: usize, const C: usize> Matrix<T, R, C>[src]

pub fn new(inner: [[T; C]; R]) -> Self[src]

Creates a new Matrix of the given size

Arguments

  • ‘inner’ - The initial value of the matrix, defines the dimensions of the matrix

Examples

use mtrx::Matrix;

let matrix = Matrix::new([[1, 2], [3, 4]]); 

pub fn inner(&self) -> [[T; C]; R][src]

Returns a 2D array representation of the matrix

pub const fn is_square(&self) -> bool[src]

Returns true if the matrix is a square matrix

Examples

use mtrx::Matrix;
 
let a = Matrix::new([[1, 2], [3, 4]]);
let b = Matrix::new([[1, 2, 3], [3, 4, 5]]);
assert!(a.is_square());
assert!(!b.is_square());
 

pub fn multiply_scalar(&self, scalar: T) -> Matrix<T, R, C>[src]

Multiples the matrix by a scalar value, and returns a new matrix with the scaled values

Arguments

  • ‘scalar’ - Value to multiply the matrix by

Returns a matrix with dimensions R×C (unchanged)

Examples

use mtrx::Matrix;
 
let matrix = Matrix::new(
    [[1, 1], [2, 2]]
);
 
let result = matrix.multiply_scalar(2);
assert_eq!(result.inner(), [[2, 2], [4, 4]]); 

pub fn multiply_matrix<const K: usize>(
    &self,
    matrix: Matrix<T, C, K>
) -> Matrix<T, R, K>
[src]

Performs matrix multiplication with the given matrix, returns the resultant matrix

Arguments

  • ‘matrix’ Matrix of dimensions C×K

Returns a matrix with dimensions R×K

Examples

use mtrx::Matrix;
 
let matrix_a = Matrix::new(
    [[1, 2, 3], 
    [4, 5, 6]]
);

let matrix_b = Matrix::new(
    [[7,  8],
     [9,  10], 
     [11, 12]]
);

let result = matrix_a.multiply_matrix(matrix_b);
assert_eq!(result.inner, [[58, 64], [139, 154]]); 
  

pub fn transpose(&self) -> Matrix<T, C, R>[src]

Returns the transposed matrix.

Matrix transposition is the process of “rotating” the matrix 90 degrees, essentially swapping rows and columns. For example,

| 1 2 | | 3 4 | | 5 6 |

becomes

| 1 3 5 | | 2 4 6 |

Returns the transposed Matrix<C, R>

Examples

use mtrx::Matrix;
 
let matrix = Matrix::new(
    [[1, 2, 3], 
    [4, 5, 6]]
);
 
let transposed = matrix.transpose();
 
assert_eq!(transposed.inner, [[1, 4], [2, 5], [3, 6]])
 

pub fn add_matrix(&self, other: Matrix<T, R, C>) -> Matrix<T, R, C>[src]

Adds two matrices of the same size and returns the sum matrix (also the same size). Additionally you can also use the + operator to add matrices together;

Arguments

  • ‘other’ - Same same sized matrix to add

Examples

use mtrx::Matrix;
 
let matrix_a = Matrix::new([[1, 2], [3, 4]]);
let matrix_b = Matrix::new([[3, 2], [1, 0]]);
 
let sum = matrix_a.add_matrix(matrix_b);
assert_eq!(sum.inner, [[4, 4], [4, 4]]);
 
let sum = matrix_a + matrix_b;
assert_eq!(sum.inner, [[4, 4], [4, 4]]);
 

pub fn add_value(&self, other: T) -> Matrix<T, R, C>[src]

Adds a single value to all cells in the matrix and returns the sum matrix. Additionally, you can use the plus operator to add a value to the matrix

Arguments

  • ‘other’ - The value T to add to all the cell

Examples

use mtrx::Matrix;
let matrix_a = Matrix::new([[1, 2], [3, 4]]);
 
let sum = matrix_a.add_value(10);
assert_eq!(sum.inner, [[11, 12], [13, 14]]);
 
let sum = matrix_a + 10;
assert_eq!(sum.inner, [[11, 12], [13, 14]]);
 

pub fn sub_matrix(&self, other: Matrix<T, R, C>) -> Matrix<T, R, C>[src]

Subtracts two matrices of the same size and returns the difference matrix (also the same size). Additionally you can also use the - operator to subtract matrices.

Arguments

  • ‘other’ - Same same sized matrix to subtract

Examples

use mtrx::Matrix;
 
let matrix_a = Matrix::new([[1, 2], [3, 4]]);
let matrix_b = Matrix::new([[0, 1], [2, 3]]);
 
let difference = matrix_a.sub_matrix(matrix_b);
assert_eq!(difference.inner, [[1, 1], [1, 1]]);
 
let difference = matrix_a - matrix_b;
assert_eq!(difference.inner, [[1, 1], [1, 1]]);
 

pub fn sub_value(&self, other: T) -> Matrix<T, R, C>[src]

Subtracts a single value to all cells in the matrix and returns the difference matrix. Additionally, you can use the - operator to add a value to the matrix

Arguments

  • ‘other’ - The value T to subtract from each cell

Examples

use mtrx::Matrix;
let matrix_a = Matrix::new([[1, 2], [3, 4]]);
 
let sum = matrix_a.sub_value(1);
assert_eq!(sum.inner, [[0, 1], [2, 3]]);
 
let sum = matrix_a - 1;
assert_eq!(sum.inner, [[0, 1], [2, 3]]);
 

pub fn vector_product(&self, other: [T; C]) -> [T; R][src]

Multiplies a matrix by a mathematical vector (const-sized array) and returns the matrix vector product.

Arguments

  • ‘other’ - Mathematical vector to multiply the matrix by

Examples

use mtrx::Matrix;
 
let matrix = Matrix::new([
    [1, -1, 2],
    [0, -3, 1]
]);
 
let vector = [2, 1, 0];
 
let product = matrix.vector_product(vector);
assert_eq!(product, [1, -3]);
 
let product = matrix * vector;
assert_eq!(product, [1, -3])
 
 

pub const fn get(&self, row: usize, col: usize) -> Option<&T>[src]

Returns a non-mutable reference to the cell at the specified row and column

Note: typical mathematical notation for matrices is for 1-indexing. However, in order to be consistent, this function is zero-indexed.

Arguments

  • ‘row’ - Must be within 0..R
  • ‘col’ - Must be within 0..C

Examples

use mtrx::Matrix;
 
let matrix = Matrix::new([
    [1, 2], 
    [3, 4]
]);
 
assert_eq!(matrix.get(0, 0), Some(&1));
assert_eq!(matrix.get(0, 1), Some(&2));
assert_eq!(matrix.get(1, 0), Some(&3));
assert_eq!(matrix.get(1, 1), Some(&4));
 
assert_eq!(matrix.get(2, 2), None);
 

pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut T>[src]

Returns a mutable reference to the cell at the specified row and column.

Note: typical mathematical notation for matrices is for 1-indexing. However, in order to be consistent, this function is zero-indexed.

Arguments

  • ‘row’ - Must be within 0..R
  • ‘col’ - Must be within 0..C

Examples

use mtrx::Matrix;
 
let mut matrix = Matrix::new([
    [1, 2], 
    [3, 4]
]);
 
 
assert_eq!(matrix.get_mut(0, 0), Some(&mut 1));
assert_eq!(matrix.get_mut(0, 1), Some(&mut 2));
assert_eq!(matrix.get_mut(1, 0), Some(&mut 3));
assert_eq!(matrix.get_mut(1, 1), Some(&mut 4));
 
assert_eq!(matrix.get_mut(2, 2), None);
 

pub fn set(&mut self, row: usize, col: usize, value: T) -> bool[src]

Sets the value of the cell at the given dimensions

Note: typical mathematical notation for matrices is for 1-indexing. However, in order to be consistent, this function is zero-indexed.

Arguments

  • ‘row’ - Must be within 0..R
  • ‘col’ - Must be within 0..C
  • ‘value’ - The value to set at row, col

Examples

use mtrx::Matrix;
 
let mut matrix = Matrix::new([
    [1, 2], 
    [3, 4]
]);
 
matrix.set(0, 0, 0);
assert_eq!(matrix.get(0, 0), Some(&0));
 

impl<T: MatrixCell<T>, const R: usize> Matrix<T, R, R>[src]

Some matrix operations are only valid for square matrices

pub fn identity() -> Matrix<T, R, R>[src]

Returns the identity matrix for a RxR matrix. The identity matrix is the matrix with 1 in a diagonal line down the matrix, and a zero everywhere else. For example, the 3x3 identity matrix is:

1 0 0

0 1 0

0 0 1

Examples

use mtrx::Matrix;
 
let identity: Matrix<i8, 2, 2> = Matrix::identity();
assert_eq!(identity.inner, [[1, 0], [0, 1]]);
 

Identity matrices cannot be created with non-square const generic sizes:

use mtrx::Matrix;
 
let identity: Matrix<i8, 3, 2> = Matrix::identity(); // Compiler Error!
 

pub fn pow(&self, exp: usize) -> Matrix<T, R, R>[src]

Raises a square matrix to a power (essentially, multiplying itself exp times). Raising a matrix to the zeroth power returns Matrix::identity

Arguments

  • ‘exp’ - Exponent

Examples

use mtrx::Matrix;
 
let matrix = Matrix::new([[1, -3], [2, 5]]);
let result = matrix.pow(2);
 
assert_eq!(result.inner, [[-5, -18], [12, 19]]);
 
let result = matrix.pow(0);
assert_eq!(result.inner, [[1, 0], [0, 1]]);
 
 

Trait Implementations

impl<T: MatrixCell<T>, const R: usize, const C: usize> Add<Matrix<T, R, C>> for Matrix<T, R, C>[src]

Trait Implementations See method descriptions above for more details

type Output = Matrix<T, R, C>

The resulting type after applying the + operator.

impl<T: MatrixCell<T>, const R: usize, const C: usize> Add<T> for Matrix<T, R, C>[src]

type Output = Matrix<T, R, C>

The resulting type after applying the + operator.

impl<T: Clone + MatrixCell<T>, const R: usize, const C: usize> Clone for Matrix<T, R, C>[src]

impl<T: Copy + MatrixCell<T>, const R: usize, const C: usize> Copy for Matrix<T, R, C>[src]

impl<T: MatrixCell<T>, const R: usize, const C: usize> Mul<[T; C]> for Matrix<T, R, C>[src]

type Output = [T; R]

The resulting type after applying the * operator.

impl<T: MatrixCell<T>, const R: usize, const C: usize, const K: usize> Mul<Matrix<T, C, K>> for Matrix<T, R, C>[src]

type Output = Matrix<T, R, K>

The resulting type after applying the * operator.

impl<T: MatrixCell<T>, const R: usize, const C: usize> Mul<T> for Matrix<T, R, C>[src]

type Output = Matrix<T, R, C>

The resulting type after applying the * operator.

impl<T: MatrixCell<T>, const R: usize, const C: usize> Sub<Matrix<T, R, C>> for Matrix<T, R, C>[src]

type Output = Matrix<T, R, C>

The resulting type after applying the - operator.

impl<T: MatrixCell<T>, const R: usize, const C: usize> Sub<T> for Matrix<T, R, C>[src]

type Output = Matrix<T, R, C>

The resulting type after applying the - operator.

Auto Trait Implementations

impl<T, const R: usize, const C: usize> RefUnwindSafe for Matrix<T, R, C> where
    T: RefUnwindSafe

impl<T, const R: usize, const C: usize> Send for Matrix<T, R, C> where
    T: Send

impl<T, const R: usize, const C: usize> Sync for Matrix<T, R, C> where
    T: Sync

impl<T, const R: usize, const C: usize> Unpin for Matrix<T, R, C> where
    T: Unpin

impl<T, const R: usize, const C: usize> UnwindSafe for Matrix<T, R, C> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.