[][src]Struct const_linear::matrix::Matrix

pub struct Matrix<T: Scalar, const M: usize, const N: usize> { /* fields omitted */ }

An M * N column-major Matrix of scalar values backed by an array. Scalar refers to anything that implements the Scalar trait, which is any value that is Copy and has a notion of arithmetic. However, many operations, such as the determinant and Gaussian elimination may not be possible with integer values.

Methods

impl<T: Scalar, const M: usize, const N: usize> Matrix<T, { M }, { N }>[src]

pub const fn zero() -> Self[src]

Constructs a matrix filled with all zero values.

Examples

use const_linear::Matrix;

let array = [[0.0, 0.0], [0.0, 0.0]];
let a = Matrix::<f64, 2, 2>::zero();
let b = Matrix::from_array(array);

assert_eq!(a, b);

pub const fn from_val(t: T) -> Self[src]

Constructs a matrix filled with all values equal to t.

Examples

use const_linear::Matrix;

let array = [[1.0, 1.0], [1.0, 1.0]];
let a = Matrix::<_, 2, 2>::from_val(1.0);
let b = Matrix::from_array(array);

assert_eq!(a, b);

pub const fn from_array(array: [[T; M]; N]) -> Self[src]

Constructs a matrix from a given 2 dimensional array.

Examples

use const_linear::Matrix;

let array = [[1.0, 2.0], [3.0, 4.0]];
let expected = [1.0, 2.0, 3.0, 4.0];
let m = Matrix::from_array(array);

for (x, y) in m.column_iter().zip(expected.iter()) {
    assert_eq!(x, y)
}

pub const fn as_array(&self) -> &[[T; M]; N][src]

Returns a reference to the backing array of the matrix.

Examples

use const_linear::Matrix;

let m = Matrix::<_, 2, 2>::from_val(1.0);
let slice = m.as_array();

for a in slice {
    println!("{:?}", &a[..]);
}

pub const fn as_mut_array(&mut self) -> &mut [[T; M]; N][src]

Returns a mutable reference to the backing array of the matrix.

Examples

use const_linear::Matrix;

let mut m = Matrix::<_, 2, 2>::from_val(1.0);
let slice = m.as_mut_array();

for arr in slice {
    for elem in arr {
        *elem = 2.0;
    }
}

assert_eq!(m, Matrix::from_val(2.0));

pub const fn as_ptr(&self) -> *const T[src]

Returns a raw pointer to the first element in the array. Keep in mind that since the matrix is represented in column-major order, offsetting this pointer by mem::size_of::<T> bytes will cause it to point to the next element in the column.

Examples

use const_linear::Matrix;

let m = Matrix::<_, 2, 2>::from_val(1.0);
let ptr = m.as_ptr();

unsafe {
    assert_eq!(*ptr, 1.0);
}

pub const fn as_mut_ptr(&mut self) -> *mut T[src]

Returns a mutable raw pointer to the first element in the array. Keep in mind that since the matrix is represented in column-major order, offsetting this pointer by mem::size_of::<T> bytes will cause it to point to the next element in the column.

Examples

use const_linear::Matrix;

let mut m = Matrix::<_, 2, 2>::from_val(1.0);
let ptr = m.as_mut_ptr();

unsafe {
    *ptr = 2.0;
    assert_ne!(*ptr, 1.0);
}

pub const fn dimensions(&self) -> (usize, usize)[src]

Returns the dimensions of the matrix, in the order (M, N).

Examples

use const_linear::Matrix;

let m = Matrix::<_, 2, 3>::from_val(1.0);

assert_eq!(m.dimensions(), (2, 3))

pub const fn column_iter(&self) -> ColumnIter<T, { M }, { N }>[src]

Returns a column-wise iterator of all elements in the matrix.

Examples

use const_linear::Matrix;

let array = [[1.0, 2.0], [3.0, 4.0]];
let expected = [1.0, 2.0, 3.0, 4.0];
let m = Matrix::from_array(array);

for (x, y) in m.column_iter().zip(expected.iter()) {
    assert_eq!(x, y)
}

pub const fn column_iter_mut(&mut self) -> ColumnIterMut<T, { M }, { N }>[src]

Returns a column-wise iterator of mutable references to all elements in the matrix.

Examples

use const_linear::Matrix;

let array = [[1.0, 2.0], [3.0, 4.0]];
let expected = [2.0, 4.0, 6.0, 8.0];
let mut m = Matrix::from_array(array);

for elem in m.column_iter_mut() {
    *elem *= 2.0;
}

for (x, y) in m.column_iter().zip(expected.iter()) {
    assert_eq!(x, y)
}

pub const fn row_iter(&self) -> RowIter<T, { M }, { N }>[src]

Returns a row-wise iterator of all elements in the matrix.

Examples

use const_linear::Matrix;

let array = [[1.0, 2.0], [3.0, 4.0]];
let expected = [1.0, 3.0, 2.0, 4.0];
let m = Matrix::from_array(array);

for (x, y) in m.row_iter().zip(expected.iter()) {
    assert_eq!(x, y)
}

pub const fn row_iter_mut(&mut self) -> RowIterMut<T, { M }, { N }>[src]

Returns a row-wise iterator of mutable references to all elements in the matrix.

Examples

use const_linear::Matrix;

let array = [[1.0, 2.0], [3.0, 4.0]];
let expected = [2.0, 6.0, 4.0, 8.0];
let mut m = Matrix::from_array(array);

for elem in m.row_iter_mut() {
    *elem *= 2.0;
}

for (x, y) in m.row_iter().zip(expected.iter()) {
    assert_eq!(x, y)
}

pub const fn column(&self, column: usize, n: usize) -> Column<T, { M }, { N }>[src]

Returns an iterator of elements over the specified column in the matrix, starting from the nth element.

Examples

use const_linear::Matrix;

let array = [[1.0, 2.0], [3.0, 4.0]];
let expected = [1.0, 2.0];
let m = Matrix::from_array(array);

for (x, y) in m.column(0, 0).zip(expected.iter()) {
    assert_eq!(x, y)
}

pub const fn column_mut(
    &mut self,
    column: usize,
    n: usize
) -> ColumnMut<T, { M }, { N }>
[src]

Returns an iterator of mutable references over the specified column in the matrix, starting from the nth element.

Examples

use const_linear::Matrix;

let array = [[1.0, 2.0], [3.0, 4.0]];
let expected = [2.0, 4.0, 3.0, 4.0];
let mut m = Matrix::from_array(array);

for elem in m.column_mut(0, 0) {
    *elem *= 2.0;
}

for (x, y) in m.column_iter().zip(expected.iter()) {
    assert_eq!(x, y)
}

pub const fn row(&self, row: usize, n: usize) -> Row<T, { M }, { N }>[src]

Returns an iterator of elements over the specified row in the matrix, starting from the nth element.

Examples

use const_linear::Matrix;

let array = [[1.0, 2.0], [3.0, 4.0]];
let expected = [1.0, 3.0];
let m = Matrix::from_array(array);

for (x, y) in m.row(0, 0).zip(expected.iter()) {
    assert_eq!(x, y)
}

pub const fn row_mut(&mut self, row: usize, n: usize) -> RowMut<T, { M }, { N }>[src]

Returns an iterator of mutable references over the specified row in the matrix, starting from the nth element.

Examples

use const_linear::Matrix;

let array = [[1.0, 2.0], [3.0, 4.0]];
let expected = [2.0, 6.0, 2.0, 4.0];
let mut m = Matrix::from_array(array);

for elem in m.row_mut(0, 0) {
    *elem *= 2.0;
}

for (x, y) in m.row_iter().zip(expected.iter()) {
    assert_eq!(x, y)
}

pub fn into_f64(self) -> Matrix<f64, { M }, { N }>[src]

Converts the provided matrix into one which holds f64 values. This is required for some operations, such as computing the determinant of a matrix or putting the matrix into row echelon form.

Examples

use const_linear::Matrix;     
      
let array = [[1, 2], [3, 4]];
let expected = [1.0, 2.0, 3.0, 4.0];
let m = Matrix::from_array(array).into_f64();

for (&x, &y) in m.column_iter().zip(expected.iter()) {
    assert_eq!(x, y as f64);
}

pub fn into_f32(self) -> Matrix<f32, { M }, { N }>[src]

Converts the provided matrix into one which holds f32 values. This is required for some operations, such as computing the determinant of a matrix or putting the matrix into row echelon form.

Examples

use const_linear::Matrix;     
      
let array = [[1, 2], [3, 4]];
let expected = [1.0, 2.0, 3.0, 4.0];
let m = Matrix::from_array(array).into_f32();

for (&x, &y) in m.column_iter().zip(expected.iter()) {
    assert_eq!(x, y as f32);
}

pub fn transpose(&self) -> Matrix<T, { N }, { M }>[src]

Returns the transpose of a matrix. This is a non-consuming operation, since the matrix needs to be copied anyways.

Examples

use const_linear::{matrix, Matrix};

let m = matrix![
    1.0, 2.0;
    3.0, 4.0;
    5.0, 6.0;
];

let m_t = m.transpose();

for (x, y) in m.column_iter().zip(m_t.row_iter()) {
    assert_eq!(x, y);
}

impl<T: Scalar, const N: usize> Matrix<T, { N }, { N }>[src]

pub const fn id() -> Self[src]

Returns the N x N identity matrix.

Examples

use const_linear::Matrix;

let array = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];

let m = Matrix::<usize, 3, 3>::id();

assert_eq!(m, Matrix::from_array(array));

pub fn det(&self) -> f64[src]

Returns the determinant of a given matrix. For N < 5, this is done manually, as matrices with dimensions of less than 5x5 are the most common ones encountered. For N >= 5, the matrix is first row reduced, and then the product of the main diagonal is returned. Note that this operation may not be 100% correct, as the matrix must first be converted into floating point representation, which may result in a loss of precision.

Examples

use const_linear::{matrix, Matrix};

let m = matrix![
    3, 3, 3;
    -2, -1, -2;
    1, -2, -3;
];

assert_eq!(m.det(), -12.0);

pub fn det_f32(&self) -> f32[src]

Returns the determinant of a given matrix. For N < 5, this is done manually, as matrices with dimensions of less than 5x5 are the most common ones encountered. For N >= 5, the matrix is first row reduced, and then the product of the main diagonal is returned. Note that this operation may not be 100% correct, as the matrix must first be converted into floating point representation, which may result in a loss of precision.

Examples

use const_linear::{matrix, Matrix};

let m = matrix![
    3, 3, 3;
    -2, -1, -2;
    1, -2, -3;
];

assert_eq!(m.det_f32(), -12.0);

impl<T: Real, const M: usize, const N: usize> Matrix<T, { M }, { N }>[src]

pub fn gauss(self) -> Self[src]

Reduces the provided matrix to row echelon form, consuming the original matrix in the process.

Examples

use const_linear::{matrix, Matrix};

let m = matrix![
    3, 3, 3;
    -2, -1, -2;
    1, -2, -3;
].into_f64().gauss();

let rows = m.dimensions().0;

for i in 0..rows {
    println!("{:?}", m[(i, i)]);
}

pub fn gauss_in_place(&mut self)[src]

Reduces the provided matrix to row echelon form without consuming the original matrix.

Examples

use const_linear::{matrix, Matrix};

let mut m = matrix![
    3, 3, 3;
    -2, -1, -2;
    1, -2, -3;
].into_f64();

m.gauss_in_place();
let rows = m.dimensions().0;

for i in 0..rows {
    println!("{:?}", m[(i, i)]);
}

impl<T: Scalar, const N: usize> Matrix<T, N, 1usize>[src]

pub fn length(&self) -> f64[src]

Returns the length of the vector as an f64.

Examples

use const_linear::{vector, Vector};

let v = vector![1, 1, 1];

assert_eq!(v.length(), f64::sqrt(3.0));

pub fn length_f32(&self) -> f32[src]

Returns the length of the vector as an f32.

Examples

use const_linear::{vector, Vector};

let v = vector![1, 1, 1];

assert_eq!(v.length(), f64::sqrt(3.0));

pub fn normalize(self) -> VectorF64<{ N }>[src]

Consumes and normalizes the vector, returning the unit vector of f64 values in the same direction as the original vector.

Examples

use const_linear::{vector, Vector};

let v = vector![1, 1, 1].normalize();

assert_eq!(v.length(), 1.0);

pub fn normalize_f32(self) -> VectorF32<{ N }>[src]

Consumes and normalizes the vector, returning the unit vector of f32 values in the same direction as the original vector.

Examples

use const_linear::{
    traits::Real,
    vector, Vector,
};

let v = vector![1, 1, 1].normalize_f32();

// Relative equality is needed here, since
// the value doesn't come out as *exactly*
// 1.0f32.
let epsilon = std::f32::EPSILON;
let relative = 1.0E-7f32;

assert!(v.length_f32().approx_eq(1.0f32, epsilon, relative));

pub fn dot(&self, rhs: &Self) -> T[src]

impl<T: Scalar> Matrix<T, N, 1usize>[src]

pub fn cross(&self, rhs: &Self) -> Self[src]

Trait Implementations

impl<T: Scalar, const M: usize, const N: usize> Add<Matrix<T, M, N>> for Matrix<T, { M }, { N }>[src]

type Output = Self

The resulting type after applying the + operator.

impl<T: Clone + Scalar, const M: usize, const N: usize> Clone for Matrix<T, M, N>[src]

impl<T: Scalar, const M: usize, const N: usize> Debug for Matrix<T, { M }, { N }>[src]

impl<T: Scalar + Eq, const M: usize, const N: usize> Eq for Matrix<T, { M }, { N }>[src]

impl<T: Scalar, const M: usize, const N: usize> Index<(usize, usize)> for Matrix<T, { M }, { N }>[src]

type Output = T

The returned type after indexing.

impl<T: Scalar, const M: usize, const N: usize> IndexMut<(usize, usize)> for Matrix<T, { M }, { N }>[src]

impl<T: Scalar, const M: usize, const N: usize, const P: usize> Mul<Matrix<T, N, P>> for Matrix<T, { M }, { N }>[src]

type Output = Matrix<T, { M }, { P }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<f32, M, N>> for f32[src]

type Output = Matrix<f32, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<f64, M, N>> for f64[src]

type Output = Matrix<f64, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<i128, M, N>> for i128[src]

type Output = Matrix<i128, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<i16, M, N>> for i16[src]

type Output = Matrix<i16, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<i32, M, N>> for i32[src]

type Output = Matrix<i32, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<i64, M, N>> for i64[src]

type Output = Matrix<i64, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<i8, M, N>> for i8[src]

type Output = Matrix<i8, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<isize, M, N>> for isize[src]

type Output = Matrix<isize, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<u128, M, N>> for u128[src]

type Output = Matrix<u128, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<u16, M, N>> for u16[src]

type Output = Matrix<u16, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<u32, M, N>> for u32[src]

type Output = Matrix<u32, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<u64, M, N>> for u64[src]

type Output = Matrix<u64, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<u8, M, N>> for u8[src]

type Output = Matrix<u8, { M }, { N }>

The resulting type after applying the * operator.

impl<const M: usize, const N: usize> Mul<Matrix<usize, M, N>> for usize[src]

type Output = Matrix<usize, { M }, { N }>

The resulting type after applying the * operator.

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

type Output = Self

The resulting type after applying the * operator.

impl<T: Scalar, const M: usize, const N: usize> PartialEq<Matrix<T, M, N>> for Matrix<T, { M }, { N }>[src]

impl<T: Scalar, const M: usize, const N: usize> Sub<Matrix<T, M, N>> for Matrix<T, { M }, { N }>[src]

type Output = Self

The resulting type after applying the - operator.

Auto Trait Implementations

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

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

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

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

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