Struct mini_matrix::Matrix

source ·
pub struct Matrix<T, const M: usize, const N: usize> {
    pub store: [[T; N]; M],
}
Expand description

A generic matrix type with M rows and N columns.

§Examples

use mini_matrix::Matrix;

let matrix = Matrix::<f64, 2, 2>::from([[1.0, 2.0], [3.0, 4.0]]);
assert_eq!(matrix.size(), (2, 2));

Fields§

§store: [[T; N]; M]

The underlying storage for the matrix elements.

Implementations§

source§

impl<T, const M: usize, const N: usize> Matrix<T, M, N>
where T: Copy + Default,

source

pub fn from(data: [[T; N]; M]) -> Self

Creates a new Matrix from the given 2D array.

§Examples
use mini_matrix::Matrix;

let matrix = Matrix::<i32, 2, 3>::from([[1, 2, 3], [4, 5, 6]]);
source

pub const fn size(&self) -> (usize, usize)

Returns the dimensions of the matrix as a tuple (rows, columns).

§Examples
use mini_matrix::Matrix;

let matrix = Matrix::<f64, 3, 4>::zero();
assert_eq!(matrix.size(), (3, 4));
source

pub fn zero() -> Self

Creates a new Matrix with all elements set to the default value of type T.

§Examples
use mini_matrix::Matrix;

let matrix = Matrix::<f64, 2, 2>::zero();
assert_eq!(matrix.store, [[0.0, 0.0], [0.0, 0.0]]);
source

pub fn from_vecs(vecs: Vec<Vec<T>>) -> Self

source§

impl<T, const M: usize, const N: usize> Matrix<T, M, N>

source

pub fn add(&mut self, other: &Self)

Adds another matrix to this matrix in-place.

§Examples
use mini_matrix::Matrix;

let mut a = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
let b = Matrix::<i32, 2, 2>::from([[5, 6], [7, 8]]);
a.add(&b);
assert_eq!(a.store, [[6, 8], [10, 12]]);
source

pub fn sub(&mut self, other: &Self)

Subtracts another matrix from this matrix in-place.

§Examples
use mini_matrix::Matrix;

let mut a = Matrix::<i32, 2, 2>::from([[5, 6], [7, 8]]);
let b = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
a.sub(&b);
assert_eq!(a.store, [[4, 4], [4, 4]]);
source

pub fn scl(&mut self, scalar: T)

Multiplies this matrix by a scalar value in-place.

§Examples
use mini_matrix::Matrix;

let mut a = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
a.scl(2);
assert_eq!(a.store, [[2, 4], [6, 8]]);
source§

impl<T, const M: usize, const N: usize> Matrix<T, M, N>
where T: Num + Copy + AddAssign + Default,

source

pub fn mul_vec(&mut self, vec: &Vector<T, N>) -> Vector<T, N>

Multiplies the matrix by a vector.

§Arguments
  • vec - The vector to multiply with the matrix.
§Returns

The resulting vector of the multiplication.

source

pub fn mul_mat(&mut self, mat: &Matrix<T, M, N>) -> Matrix<T, M, N>

Multiplies the matrix by another matrix.

§Arguments
  • mat - The matrix to multiply with.
§Returns

The resulting matrix of the multiplication.

source§

impl<T, const M: usize, const N: usize> Matrix<T, M, N>
where T: Num + Copy + AddAssign + Default,

source

pub fn trace(&self) -> T

Calculates the trace of the matrix.

The trace is defined as the sum of the elements on the main diagonal.

§Panics

Panics if the matrix is not square (i.e., if M != N).

§Examples
use mini_matrix::Matrix;

let mut a = Matrix::<i32, 3, 3>::from([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
assert_eq!(a.trace(), 15);
source§

impl<T, const M: usize, const N: usize> Matrix<T, M, N>
where T: Copy + Default,

source

pub fn transpose(&mut self) -> Matrix<T, N, M>

Computes the transpose of the matrix.

§Examples
use mini_matrix::Matrix;

let mut a = Matrix::<i32, 2, 3>::from([[1, 2, 3], [4, 5, 6]]);
let b = a.transpose();
assert_eq!(b.store, [[1, 4], [2, 5], [3, 6]]);
source§

impl<T, const M: usize, const N: usize> Matrix<T, M, N>
where T: Copy + Default + Num,

source

pub fn identity() -> Matrix<T, M, N>

Creates an identity matrix.

§Examples
use mini_matrix::Matrix;

let a = Matrix::<i32, 3, 3>::identity();
assert_eq!(a.store, [[1, 0, 0], [0, 1, 0], [0, 0, 1]]);
source§

impl<T, const M: usize, const N: usize> Matrix<T, M, N>
where T: Copy + Default + Mul<Output = T> + PartialEq + Num + Div<Output = T> + Sub<Output = T>,

source

pub fn row_echelon(&self) -> Matrix<T, M, N>

Computes the row echelon form of the matrix.

§Examples
use mini_matrix::Matrix;

let a = Matrix::<f64, 3, 4>::from([
    [1.0, 2.0, 3.0, 4.0],
    [5.0, 6.0, 7.0, 8.0],
    [9.0, 10.0, 11.0, 12.0]
]);
let b = a.row_echelon();
// Check the result (approximate due to floating-point arithmetic)
source§

impl<T, const M: usize, const N: usize> Matrix<T, M, N>
where T: Copy + Default + Mul + Num + Neg<Output = T> + AddAssign + Debug,

source

pub fn determinant(&self) -> T

Calculates the determinant of the matrix.

This method supports matrices up to 4x4 in size.

§Panics

Panics if the matrix is larger than 4x4.

§Examples
use mini_matrix::Matrix;

let a = Matrix::<i32, 3, 3>::from([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
assert_eq!(a.determinant(), 0);
source§

impl<T, const M: usize, const N: usize> Matrix<T, M, N>
where T: Copy + Default + Mul + Num + Neg<Output = T> + AddAssign + Debug + Float,

source

pub fn inverse(&self) -> Result<Self, &'static str>

Calculates the inverse of the matrix.

This method supports matrices up to 3x3 in size.

§Returns

Returns Ok(Matrix) if the inverse exists, or an Err with a descriptive message if not.

§Examples
use mini_matrix::Matrix;

let a = Matrix::<f64, 2, 2>::from([[1.0, 2.0], [3.0, 4.0]]);
let inv = a.inverse().unwrap();
// Check the result (approximate due to floating-point arithmetic)
source§

impl<T, const M: usize, const N: usize> Matrix<T, M, N>
where T: Copy + Default + Mul + Num + Neg<Output = T> + AddAssign + PartialEq,

source

pub fn rank(&self) -> usize

Calculates the rank of the matrix.

The rank is determined by computing the row echelon form and counting non-zero rows.

§Examples
use mini_matrix::Matrix;

let a = Matrix::<i32, 3, 3>::from([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
assert_eq!(a.rank(), 2);
source§

impl<T, const M: usize, const N: usize> Matrix<T, M, N>
where T: Copy + Default + Mul + Num + Neg<Output = T> + AddAssign + PartialEq,

source

pub fn cofactor2x2(&self, row: usize, col: usize) -> Matrix<T, 2, 2>

source

pub fn cofactor1x1(&self, row: usize, col: usize) -> Matrix<T, 1, 1>

Methods from Deref<Target = [[T; N]; M]>§

source

pub fn as_ascii(&self) -> Option<&[AsciiChar; N]>

🔬This is a nightly-only experimental API. (ascii_char)

Converts this array of bytes into an array of ASCII characters, or returns None if any of the characters is non-ASCII.

§Examples
#![feature(ascii_char)]
#![feature(const_option)]

const HEX_DIGITS: [std::ascii::Char; 16] =
    *b"0123456789abcdef".as_ascii().unwrap();

assert_eq!(HEX_DIGITS[1].as_str(), "1");
assert_eq!(HEX_DIGITS[10].as_str(), "a");
source

pub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar; N]

🔬This is a nightly-only experimental API. (ascii_char)

Converts this array of bytes into an array of ASCII characters, without checking whether they’re valid.

§Safety

Every byte in the array must be in 0..=127, or else this is UB.

1.57.0 · source

pub fn as_slice(&self) -> &[T]

Returns a slice containing the entire array. Equivalent to &s[..].

1.57.0 · source

pub fn as_mut_slice(&mut self) -> &mut [T]

Returns a mutable slice containing the entire array. Equivalent to &mut s[..].

1.77.0 · source

pub fn each_ref(&self) -> [&T; N]

Borrows each element and returns an array of references with the same size as self.

§Example
let floats = [3.1, 2.7, -1.0];
let float_refs: [&f64; 3] = floats.each_ref();
assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);

This method is particularly useful if combined with other methods, like map. This way, you can avoid moving the original array if its elements are not Copy.

let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
let is_ascii = strings.each_ref().map(|s| s.is_ascii());
assert_eq!(is_ascii, [true, false, true]);

// We can still access the original array: it has not been moved.
assert_eq!(strings.len(), 3);
1.77.0 · source

pub fn each_mut(&mut self) -> [&mut T; N]

Borrows each element mutably and returns an array of mutable references with the same size as self.

§Example

let mut floats = [3.1, 2.7, -1.0];
let float_refs: [&mut f64; 3] = floats.each_mut();
*float_refs[0] = 0.0;
assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]);
assert_eq!(floats, [0.0, 2.7, -1.0]);
source

pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])

🔬This is a nightly-only experimental API. (split_array)

Divides one array reference into two at an index.

The first will contain all indices from [0, M) (excluding the index M itself) and the second will contain all indices from [M, N) (excluding the index N itself).

§Panics

Panics if M > N.

§Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.split_array_ref::<0>();
   assert_eq!(left, &[]);
   assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<2>();
    assert_eq!(left, &[1, 2]);
    assert_eq!(right, &[3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<6>();
    assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
    assert_eq!(right, &[]);
}
source

pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])

🔬This is a nightly-only experimental API. (split_array)

Divides one mutable array reference into two at an index.

The first will contain all indices from [0, M) (excluding the index M itself) and the second will contain all indices from [M, N) (excluding the index N itself).

§Panics

Panics if M > N.

§Examples
#![feature(split_array)]

let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.split_array_mut::<2>();
assert_eq!(left, &mut [1, 0][..]);
assert_eq!(right, &mut [3, 0, 5, 6]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);
source

pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])

🔬This is a nightly-only experimental API. (split_array)

Divides one array reference into two at an index from the end.

The first will contain all indices from [0, N - M) (excluding the index N - M itself) and the second will contain all indices from [N - M, N) (excluding the index N itself).

§Panics

Panics if M > N.

§Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.rsplit_array_ref::<0>();
   assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
   assert_eq!(right, &[]);
}

{
    let (left, right) = v.rsplit_array_ref::<2>();
    assert_eq!(left, &[1, 2, 3, 4]);
    assert_eq!(right, &[5, 6]);
}

{
    let (left, right) = v.rsplit_array_ref::<6>();
    assert_eq!(left, &[]);
    assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
source

pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])

🔬This is a nightly-only experimental API. (split_array)

Divides one mutable array reference into two at an index from the end.

The first will contain all indices from [0, N - M) (excluding the index N - M itself) and the second will contain all indices from [N - M, N) (excluding the index N itself).

§Panics

Panics if M > N.

§Examples
#![feature(split_array)]

let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.rsplit_array_mut::<4>();
assert_eq!(left, &mut [1, 0]);
assert_eq!(right, &mut [3, 0, 5, 6][..]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);

Trait Implementations§

source§

impl<T, const M: usize, const N: usize> Add for Matrix<T, M, N>
where T: AddAssign + Copy + Num,

source§

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

Adds two matrices element-wise.

§Examples
use mini_matrix::Matrix;

let a = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
let b = Matrix::<i32, 2, 2>::from([[5, 6], [7, 8]]);
let c = a + b;
assert_eq!(c.store, [[6, 8], [10, 12]]);
§

type Output = Matrix<T, M, N>

The resulting type after applying the + operator.
source§

impl<T: Clone, const M: usize, const N: usize> Clone for Matrix<T, M, N>

source§

fn clone(&self) -> Matrix<T, M, N>

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, const M: usize, const N: usize> Debug for Matrix<T, M, N>

source§

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

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

impl<T, const M: usize, const N: usize> Deref for Matrix<T, M, N>

source§

fn deref(&self) -> &Self::Target

Dereferences the matrix, allowing it to be treated as a slice.

§Note

This implementation is currently unfinished.

§Examples
use mini_matrix::Matrix;

let matrix = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
// Usage example will be available once implementation is complete
§

type Target = [[T; N]; M]

The resulting type after dereferencing.
source§

impl<T, const M: usize, const N: usize> DerefMut for Matrix<T, M, N>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the matrix, allowing it to be treated as a mutable slice.

§Note

This implementation is currently unfinished.

§Examples
use mini_matrix::Matrix;

let mut matrix = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
// Usage example will be available once implementation is complete
source§

impl<T, const M: usize, const N: usize> Display for Matrix<T, M, N>

source§

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

Formats the matrix for display.

Each row of the matrix is displayed on a new line, with elements separated by commas. Elements are formatted with one decimal place precision.

§Examples
use mini_matrix::Matrix;

let a = Matrix::<f32, 2, 2>::from([[1.0, 2.5], [3.7, 4.2]]);
println!("{}", a);
// Output:
// // [1.0, 2.5]
// // [3.7, 4.2]
source§

impl<T, const M: usize, const N: usize> Index<(usize, usize)> for Matrix<T, M, N>

source§

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

Immutably indexes into the matrix, allowing read access to its elements.

§Arguments
  • index - A tuple (row, column) specifying the element to access.
§Examples
use mini_matrix::Matrix;

let matrix = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
assert_eq!(matrix[(1, 0)], 3);
§

type Output = T

The returned type after indexing.
source§

impl<T, const M: usize, const N: usize> IndexMut<(usize, usize)> for Matrix<T, M, N>

source§

fn index_mut(&mut self, index: (usize, usize)) -> &mut T

Mutably indexes into the matrix, allowing modification of its elements.

§Arguments
  • index - A tuple (row, column) specifying the element to access.
§Examples
use mini_matrix::Matrix;

let mut matrix = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
matrix[(0, 1)] = 5;
assert_eq!(matrix.store, [[1, 5], [3, 4]]);
source§

impl<T, const M: usize, const N: usize> Mul<Matrix<T, N, N>> for Matrix<T, M, N>
where T: MulAssign + AddAssign + Copy + Num + Default,

source§

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

Multiplies two matrices.

§Examples
use mini_matrix::Matrix;

let a = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
let b = Matrix::<i32, 2, 2>::from([[5, 6], [7, 8]]);
let c = a * b;
assert_eq!(c.store, [[17, 23], [39, 53]]);
§

type Output = Matrix<T, M, N>

The resulting type after applying the * operator.
source§

impl<T, const M: usize, const N: usize> Mul<T> for Matrix<T, M, N>
where T: MulAssign + Copy + Num,

source§

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

Multiplies a matrix by a scalar value.

§Examples
use mini_matrix::Matrix;

let a = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
let b = a * 2;
assert_eq!(b.store, [[2, 4], [6, 8]]);
§

type Output = Matrix<T, M, N>

The resulting type after applying the * operator.
source§

impl<T, const M: usize, const N: usize> Mul<Vector<T, N>> for Matrix<T, M, N>
where T: MulAssign + AddAssign + Copy + Num + Default,

source§

fn mul(self, rhs: Vector<T, N>) -> Self::Output

Multiplies a matrix by a vector.

§Examples
use mini_matrix::{Matrix, Vector};

let a = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
let v = Vector::<i32, 2>::from([5, 6]);
let result = a * v;
assert_eq!(result.store, [17, 39]);
§

type Output = Vector<T, M>

The resulting type after applying the * operator.
source§

impl<T, const M: usize, const N: usize> Neg for Matrix<T, M, N>
where T: Neg<Output = T> + Copy,

source§

fn neg(self) -> Self::Output

Negates all elements of the matrix.

§Examples
use mini_matrix::Matrix;

let a = Matrix::<i32, 2, 2>::from([[1, -2], [-3, 4]]);
let b = -a;
assert_eq!(b.store, [[-1, 2], [3, -4]]);
§

type Output = Matrix<T, M, N>

The resulting type after applying the - operator.
source§

impl<T: PartialEq, const M: usize, const N: usize> PartialEq for Matrix<T, M, N>

source§

fn eq(&self, other: &Matrix<T, M, N>) -> 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.
source§

impl<T, const M: usize, const N: usize> Sub for Matrix<T, M, N>
where T: SubAssign + Copy + Num,

source§

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

Subtracts one matrix from another element-wise.

§Examples
use mini_matrix::Matrix;

let a = Matrix::<i32, 2, 2>::from([[5, 6], [7, 8]]);
let b = Matrix::<i32, 2, 2>::from([[1, 2], [3, 4]]);
let c = a - b;
assert_eq!(c.store, [[4, 4], [4, 4]]);
§

type Output = Matrix<T, M, N>

The resulting type after applying the - operator.
source§

impl<T: Copy, const M: usize, const N: usize> Copy for Matrix<T, M, N>

source§

impl<T, const M: usize, const N: usize> StructuralPartialEq for Matrix<T, M, N>

Auto Trait Implementations§

§

impl<T, const M: usize, const N: usize> Freeze for Matrix<T, M, N>
where T: Freeze,

§

impl<T, const M: usize, const N: usize> RefUnwindSafe for Matrix<T, M, N>
where T: RefUnwindSafe,

§

impl<T, const M: usize, const N: usize> Send for Matrix<T, M, N>
where T: Send,

§

impl<T, const M: usize, const N: usize> Sync for Matrix<T, M, N>
where T: Sync,

§

impl<T, const M: usize, const N: usize> Unpin for Matrix<T, M, N>
where T: Unpin,

§

impl<T, const M: usize, const N: usize> UnwindSafe for Matrix<T, M, N>
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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

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

§

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§

default 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>,

§

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

§

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.