Struct sized_matrix::Matrix
source · [−]Expand description
An M
by N
matrix of T
s.
Examples
Multiplying two matrices of different sizes:
use sized_matrix::Matrix;
let a: Matrix<i32, 3, 4> = Matrix::rows([
[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
]);
let b: Matrix<i32, 4, 2> = Matrix::rows([
[ 0, 1],
[ 1, 2],
[ 3, 5],
[ 8, 13],
]);
let c: Matrix<i32, 3, 2> = a * b;
assert_eq!(c, Matrix::rows([
[ 43, 72],
[ 91, 156],
[139, 240],
]));
Implementations
sourceimpl<T, const M: usize, const N: usize> Matrix<T, M, N>
impl<T, const M: usize, const N: usize> Matrix<T, M, N>
sourcepub fn cols(cols: [[T; M]; N]) -> Self
pub fn cols(cols: [[T; M]; N]) -> Self
Construct a matrix from an array of columns of values.
Examples
Constructing a matrix from columns:
use sized_matrix::Matrix;
let matrix = Matrix::cols([
[1, 3],
[2, 4],
]);
assert_eq!(matrix[[0, 0]], 1);
assert_eq!(matrix[[0, 1]], 2);
assert_eq!(matrix[[1, 0]], 3);
assert_eq!(matrix[[1, 1]], 4);
sourcepub fn rows(rows: [[T; N]; M]) -> Self
pub fn rows(rows: [[T; N]; M]) -> Self
Construct a matrix from an array of rows of values.
Examples
Constructing a matrix from rows:
use sized_matrix::Matrix;
let matrix = Matrix::rows([
[1, 2],
[3, 4],
]);
assert_eq!(matrix[[0, 0]], 1);
assert_eq!(matrix[[0, 1]], 2);
assert_eq!(matrix[[1, 0]], 3);
assert_eq!(matrix[[1, 1]], 4);
sourcepub fn from_vectors(vectors: [Vector<T, M>; N]) -> Self
pub fn from_vectors(vectors: [Vector<T, M>; N]) -> Self
Construct a matrix from an array of column vectors.
Examples
Applying a transformation to the points of a square:
use sized_matrix::Matrix;
let points = [
Matrix::vector([0.0, 0.0]),
Matrix::vector([0.0, 1.0]),
Matrix::vector([1.0, 1.0]),
Matrix::vector([1.0, 0.0]),
];
let shear = Matrix::rows([
[1.0, 0.3],
[0.0, 1.0],
]);
let transformed = (shear * Matrix::from_vectors(points)).to_vectors();
assert_eq!(transformed, [
Matrix::vector([0.0, 0.0]),
Matrix::vector([0.3, 1.0]),
Matrix::vector([1.3, 1.0]),
Matrix::vector([1.0, 0.0]),
]);
sourcepub fn to_vectors(self) -> [Vector<T, M>; N]
pub fn to_vectors(self) -> [Vector<T, M>; N]
Retrieve an array of column vectors from a matrix.
Examples
Applying a transformation to the points of a square:
use sized_matrix::Matrix;
let points = [
Matrix::vector([0.0, 0.0]),
Matrix::vector([0.0, 1.0]),
Matrix::vector([1.0, 1.0]),
Matrix::vector([1.0, 0.0]),
];
let shear = Matrix::rows([
[1.0, 0.3],
[0.0, 1.0],
]);
let transformed = (shear * Matrix::from_vectors(points)).to_vectors();
assert_eq!(transformed, [
Matrix::vector([0.0, 0.0]),
Matrix::vector([0.3, 1.0]),
Matrix::vector([1.3, 1.0]),
Matrix::vector([1.0, 0.0]),
]);
Trait Implementations
sourceimpl<TLhs: Copy, TRhs: Copy, TOutput, const M: usize, const N: usize> Add<Matrix<TRhs, M, N>> for Matrix<TLhs, M, N> where
TLhs: Add<TRhs, Output = TOutput>,
impl<TLhs: Copy, TRhs: Copy, TOutput, const M: usize, const N: usize> Add<Matrix<TRhs, M, N>> for Matrix<TLhs, M, N> where
TLhs: Add<TRhs, Output = TOutput>,
sourceimpl<TLhs: Copy, TRhs: Copy, const M: usize, const N: usize> AddAssign<Matrix<TRhs, M, N>> for Matrix<TLhs, M, N> where
Self: Add<Matrix<TRhs, M, N>, Output = Self>,
impl<TLhs: Copy, TRhs: Copy, const M: usize, const N: usize> AddAssign<Matrix<TRhs, M, N>> for Matrix<TLhs, M, N> where
Self: Add<Matrix<TRhs, M, N>, Output = Self>,
sourcefn add_assign(&mut self, rhs: Matrix<TRhs, M, N>)
fn add_assign(&mut self, rhs: Matrix<TRhs, M, N>)
Performs the +=
operation. Read more
sourceimpl<TLhs: Copy, TRhs: Copy, const M: usize, const N: usize> Div<Matrix<TRhs, N, N>> for Matrix<TLhs, M, N> where
TLhs: MulAdd<TRhs, TLhs, Output = TLhs> + DivAssign<TRhs>,
TRhs: Zero + MulAdd<TRhs, TRhs, Output = TRhs> + DivAssign<TRhs> + Neg<Output = TRhs>,
impl<TLhs: Copy, TRhs: Copy, const M: usize, const N: usize> Div<Matrix<TRhs, N, N>> for Matrix<TLhs, M, N> where
TLhs: MulAdd<TRhs, TLhs, Output = TLhs> + DivAssign<TRhs>,
TRhs: Zero + MulAdd<TRhs, TRhs, Output = TRhs> + DivAssign<TRhs> + Neg<Output = TRhs>,
Matrix
-Matrix
right-division.
This performs a generalised Gauss-Jordan elimination to calculate self * rhs^-1
.
This is slightly more efficient than calculating the inverse then multiplying, as the
Gauss-Jordan method of finding an inverse involves multiplying the identity matrix by the
inverse of the matrix, so you can replace this identity matrix with another matrix to get an
extra multiplication ‘for free’. Inversion is then defined as A^-1 = 1 / A
.
sourceimpl<TLhs: Copy, TRhs: Copy, TOutput, const M: usize, const N: usize> Div<TRhs> for Matrix<TLhs, M, N> where
TLhs: Div<TRhs, Output = TOutput>,
TRhs: Scalar,
impl<TLhs: Copy, TRhs: Copy, TOutput, const M: usize, const N: usize> Div<TRhs> for Matrix<TLhs, M, N> where
TLhs: Div<TRhs, Output = TOutput>,
TRhs: Scalar,
Matrix
-Scalar
division.
sourceimpl<T: Copy, const M: usize, const N: usize> DivAssign<Matrix<T, N, N>> for Matrix<T, M, N> where
Self: Div<Matrix<T, N, N>, Output = Self>,
impl<T: Copy, const M: usize, const N: usize> DivAssign<Matrix<T, N, N>> for Matrix<T, M, N> where
Self: Div<Matrix<T, N, N>, Output = Self>,
Matrix
-Matrix
division.
sourcefn div_assign(&mut self, rhs: Matrix<T, N, N>)
fn div_assign(&mut self, rhs: Matrix<T, N, N>)
Performs the /=
operation. Read more
sourceimpl<TLhs: Copy, TRhs: Copy, const M: usize, const N: usize> DivAssign<TRhs> for Matrix<TLhs, M, N> where
Self: Div<TRhs, Output = Matrix<TLhs, M, N>>,
TRhs: Scalar,
impl<TLhs: Copy, TRhs: Copy, const M: usize, const N: usize> DivAssign<TRhs> for Matrix<TLhs, M, N> where
Self: Div<TRhs, Output = Matrix<TLhs, M, N>>,
TRhs: Scalar,
Matrix
-Scalar
division.
sourcefn div_assign(&mut self, rhs: TRhs)
fn div_assign(&mut self, rhs: TRhs)
Performs the /=
operation. Read more
sourceimpl<TLhs: Copy, TRhs: Copy, TOutput, const M: usize> Dot<Matrix<TRhs, M, 1_usize>> for Vector<TLhs, M> where
TLhs: Mul<TRhs, Output = TOutput> + MulAdd<TRhs, TOutput, Output = TOutput>,
TOutput: Zero,
impl<TLhs: Copy, TRhs: Copy, TOutput, const M: usize> Dot<Matrix<TRhs, M, 1_usize>> for Vector<TLhs, M> where
TLhs: Mul<TRhs, Output = TOutput> + MulAdd<TRhs, TOutput, Output = TOutput>,
TOutput: Zero,
sourceimpl<T, const M: usize, const N: usize> Init<T, [usize; 2], ()> for Matrix<T, M, N>
impl<T, const M: usize, const N: usize> Init<T, [usize; 2], ()> for Matrix<T, M, N>
sourceimpl<T: Copy, const N: usize> Inv for Matrix<T, N, N> where
Self: One + Div<Self, Output = Self>,
impl<T: Copy, const N: usize> Inv for Matrix<T, N, N> where
Self: One + Div<Self, Output = Self>,
sourceimpl<TLhs: Copy, TRhs: Copy, TOutput, const M: usize, const K: usize, const N: usize> Mul<Matrix<TRhs, K, N>> for Matrix<TLhs, M, K> where
TLhs: Mul<TRhs, Output = TOutput> + MulAdd<TRhs, TOutput, Output = TOutput>,
TOutput: Zero,
impl<TLhs: Copy, TRhs: Copy, TOutput, const M: usize, const K: usize, const N: usize> Mul<Matrix<TRhs, K, N>> for Matrix<TLhs, M, K> where
TLhs: Mul<TRhs, Output = TOutput> + MulAdd<TRhs, TOutput, Output = TOutput>,
TOutput: Zero,
Matrix
-Matrix
multiplication.
sourceimpl<TLhs: Copy, TRhs: Copy, TOutput, const M: usize, const N: usize> Mul<TRhs> for Matrix<TLhs, M, N> where
TLhs: Mul<TRhs, Output = TOutput>,
TRhs: Scalar,
impl<TLhs: Copy, TRhs: Copy, TOutput, const M: usize, const N: usize> Mul<TRhs> for Matrix<TLhs, M, N> where
TLhs: Mul<TRhs, Output = TOutput>,
TRhs: Scalar,
Matrix
-Scalar
multiplication.
sourceimpl<TLhs: Copy, TA: Copy, TB: Copy, const M: usize, const K: usize, const N: usize> MulAdd<Matrix<TA, K, N>, Matrix<TB, M, N>> for Matrix<TLhs, M, K> where
TLhs: MulAdd<TA, TB, Output = TB>,
impl<TLhs: Copy, TA: Copy, TB: Copy, const M: usize, const K: usize, const N: usize> MulAdd<Matrix<TA, K, N>, Matrix<TB, M, N>> for Matrix<TLhs, M, K> where
TLhs: MulAdd<TA, TB, Output = TB>,
sourceimpl<TLhs: Copy, TA: Copy, TB: Copy, const M: usize, const N: usize> MulAddAssign<Matrix<TA, N, N>, Matrix<TB, M, N>> for Matrix<TLhs, M, N> where
Self: MulAdd<Matrix<TA, N, N>, Matrix<TB, M, N>, Output = Self>,
impl<TLhs: Copy, TA: Copy, TB: Copy, const M: usize, const N: usize> MulAddAssign<Matrix<TA, N, N>, Matrix<TB, M, N>> for Matrix<TLhs, M, N> where
Self: MulAdd<Matrix<TA, N, N>, Matrix<TB, M, N>, Output = Self>,
sourcefn mul_add_assign(&mut self, a: Matrix<TA, N, N>, b: Matrix<TB, M, N>)
fn mul_add_assign(&mut self, a: Matrix<TA, N, N>, b: Matrix<TB, M, N>)
Performs the fused multiply-add operation.
sourceimpl<TLhs: Copy, TRhs: Copy, const M: usize, const N: usize> MulAssign<Matrix<TRhs, N, N>> for Matrix<TLhs, M, N> where
Self: Mul<Matrix<TRhs, N, N>, Output = Self>,
impl<TLhs: Copy, TRhs: Copy, const M: usize, const N: usize> MulAssign<Matrix<TRhs, N, N>> for Matrix<TLhs, M, N> where
Self: Mul<Matrix<TRhs, N, N>, Output = Self>,
Matrix
-Matrix
multiplication.
sourcefn mul_assign(&mut self, rhs: Matrix<TRhs, N, N>)
fn mul_assign(&mut self, rhs: Matrix<TRhs, N, N>)
Performs the *=
operation. Read more
sourceimpl<TLhs: Copy, TRhs: Copy, const M: usize, const N: usize> MulAssign<TRhs> for Matrix<TLhs, M, N> where
Self: Mul<TRhs, Output = Matrix<TLhs, M, N>>,
TRhs: Scalar,
impl<TLhs: Copy, TRhs: Copy, const M: usize, const N: usize> MulAssign<TRhs> for Matrix<TLhs, M, N> where
Self: Mul<TRhs, Output = Matrix<TLhs, M, N>>,
TRhs: Scalar,
Matrix
-Scalar
multiplication.
sourcefn mul_assign(&mut self, rhs: TRhs)
fn mul_assign(&mut self, rhs: TRhs)
Performs the *=
operation. Read more
sourceimpl<T: Copy, TOutput, const M: usize, const N: usize> Neg for Matrix<T, M, N> where
T: Neg<Output = TOutput>,
impl<T: Copy, TOutput, const M: usize, const N: usize> Neg for Matrix<T, M, N> where
T: Neg<Output = TOutput>,
sourceimpl<T: Copy, const N: usize> One for Matrix<T, N, N> where
T: Zero + One + MulAdd<Output = T>,
impl<T: Copy, const N: usize> One for Matrix<T, N, N> where
T: Zero + One + MulAdd<Output = T>,
sourceimpl<T, const M: usize, const N: usize> PartialEq<Matrix<T, M, N>> for Matrix<T, M, N> where
T: PartialEq,
impl<T, const M: usize, const N: usize> PartialEq<Matrix<T, M, N>> for Matrix<T, M, N> where
T: PartialEq,
sourceimpl<T: Copy, TRhs, const N: usize> Pow<TRhs> for Matrix<T, N, N> where
Self: Inv<Output = Self> + One + MulAssign<Self>,
TRhs: PrimInt,
impl<T: Copy, TRhs, const N: usize> Pow<TRhs> for Matrix<T, N, N> where
Self: Inv<Output = Self> + One + MulAssign<Self>,
TRhs: PrimInt,
sourceimpl<T: Copy, const M: usize, const N: usize, const M_OUT: usize, const N_OUT: usize> Section<[usize; 2], Matrix<T, M_OUT, N_OUT>> for Matrix<T, M, N>
impl<T: Copy, const M: usize, const N: usize, const M_OUT: usize, const N_OUT: usize> Section<[usize; 2], Matrix<T, M_OUT, N_OUT>> for Matrix<T, M, N>
sourceimpl<T: Copy, const M: usize, const M_OUT: usize> Section<usize, Matrix<T, M_OUT, 1_usize>> for Vector<T, M>
impl<T: Copy, const M: usize, const M_OUT: usize> Section<usize, Matrix<T, M_OUT, 1_usize>> for Vector<T, M>
sourceimpl<TLhs: Copy, TRhs: Copy, TOutput, const M: usize, const N: usize> Sub<Matrix<TRhs, M, N>> for Matrix<TLhs, M, N> where
TLhs: Sub<TRhs, Output = TOutput>,
impl<TLhs: Copy, TRhs: Copy, TOutput, const M: usize, const N: usize> Sub<Matrix<TRhs, M, N>> for Matrix<TLhs, M, N> where
TLhs: Sub<TRhs, Output = TOutput>,
sourceimpl<TLhs: Copy, TRhs: Copy, const M: usize, const N: usize> SubAssign<Matrix<TRhs, M, N>> for Matrix<TLhs, M, N> where
Self: Sub<Matrix<TRhs, M, N>, Output = Self>,
impl<TLhs: Copy, TRhs: Copy, const M: usize, const N: usize> SubAssign<Matrix<TRhs, M, N>> for Matrix<TLhs, M, N> where
Self: Sub<Matrix<TRhs, M, N>, Output = Self>,
sourcefn sub_assign(&mut self, rhs: Matrix<TRhs, M, N>)
fn sub_assign(&mut self, rhs: Matrix<TRhs, M, N>)
Performs the -=
operation. Read more
impl<T: Copy, const M: usize, const N: usize> Copy for Matrix<T, M, N>
impl<T, const M: usize, const N: usize> Eq for Matrix<T, M, N> where
T: Eq,
impl<T, const M: usize, const N: usize> !Scalar for Matrix<T, M, N>
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more