Skip to main content

Matrix

Struct Matrix 

Source
pub struct Matrix<const N: usize, T, A: Alignment>(/* private fields */)
where
    Length<N>: SupportedLength,
    T: Scalar;
Expand description

A square column major matrix.

Matrix is the generic form of:

Matrix is generic over:

  • N: Length (2, 3, or 4)
  • T: Scalar type (see Scalar)
  • A: Alignment (see Alignment)

To initialize matrices, use the macros mat2, mat3, mat4. To initialize a matrix of an unknown length, use Matrix::from_col_array.

§Guarantees

Matrix<N, T, A> represents N consecutive values of Vector<N, T, A> followed by optional padding due to alignment.

Padding bytes are initialized and accept any bit-pattern. It is sound to store any bit-pattern in padding, and it is unsound to assume that padding contains valid values of T unless T accepts all bit-patterns.

  • Matrix<N, T, Unaligned>: has no padding, has no additional alignment.

  • Matrix<2, T, Aligned>: has memory layout identical to Vector<4, T, Aligned>.

  • Matrix<3, T, Aligned>: may have one padding vector, may have additional alignment.

  • Matrix<4, T, Aligned>: has no padding, may have additional alignment.

Matrices of scalar types with the same Scalar::Repr are guaranteed to have compatible memory layouts, unless Repr = (). They are guaranteed to have the same size and element positions, but their alignment may differ.

Types containing Matrix are not guaranteed to have the same memory layout as types containing equivalent arrays. For example, even though Mat2U<T> and [T; 4] have the same memory layout, Option<Mat2U<T>> and Option<[T; 4]> may not.

Implementations§

Source§

impl<const N: usize, T, A: Alignment> Matrix<N, T, A>

Source

pub const fn from_col_array(array: &[Vector<N, T, A>; N]) -> Self

Creates a matrix from an array of columns.

The preferable way to create matrices is using the macros mat2, mat3, mat4.

Matrix::from_array should only be used when the length of the matrix is unknown or when directly converting from an array.

Source

pub fn from_col_fn<F>(f: F) -> Self
where F: FnMut(usize) -> Vector<N, T, A>,

Creates a matrix by calling function f for each column index.

Equivalent to [f(0), f(1), f(2), ...] where each item is a column.

Source

pub const fn from_diagonal(diagonal: Vector<N, T, A>) -> Self
where T: Zero,

Creates a matrix with its diagonal set to diagonal and all other entries set to 0.

Source

pub const fn to_alignment<A2: Alignment>(&self) -> Matrix<N, T, A2>

Converts the matrix to the specified alignment.

See Alignment for more information.

Source

pub const fn align(&self) -> Matrix<N, T, Aligned>

Converts the matrix to Aligned alignment.

See Alignment for more information.

Source

pub const fn unalign(&self) -> Matrix<N, T, Unaligned>

Converts the matrix to Unaligned alignment.

See Alignment for more information.

Source

pub const fn to_col_array(&self) -> [Vector<N, T, A>; N]

Converts the matrix to an array of columns.

Source

pub const fn as_col_array_ref(&self) -> &[Vector<N, T, A>; N]

Returns a reference to the matrix’s columns.

Source

pub const fn as_col_array_mut(&mut self) -> &mut [Vector<N, T, A>; N]

Returns a mutable reference to the matrix’s columns.

Source

pub const fn col(&self, index: usize) -> Vector<N, T, A>

Returns the column at the given index.

§Panics

Panics if the index is out of bounds.

Source

pub const fn col_mut(&mut self, index: usize) -> &mut Vector<N, T, A>

Returns a mutable reference to the column at the given index.

§Panics

Panics if the index is out of bounds.

Source

pub const fn row(&self, index: usize) -> Vector<N, T, A>

Returns the row at the given index.

§Panics

Panics if the index is out of bounds.

Source

pub const fn set_row(&mut self, index: usize, value: Vector<N, T, A>)

Sets the row at the given index to the given value.

§Panics

Panics if the index is out of bounds.

Source

pub const fn diagonal(&self) -> Vector<N, T, A>

Returns the diagonal of the matrix.

Source

pub const unsafe fn to_repr<T2>(self) -> Matrix<N, T2, A>
where T2: Scalar<Repr = T::Repr>, T::Repr: SignedInteger,

Reinterprets the bits of the matrix to a different scalar type.

The two scalar types must have compatible memory layouts. This is enforced via trait bounds in this function’s signature.

This function is used to make SIMD optimizations in implementations of Scalar.

§Safety

The components of the input must be valid for the output matrix type.

For example, when converting matrices from u8 to bool the input components must be either 0 or 1.

The optional padding does not need to be a valid value of T2.

Source§

impl<const N: usize, T, A: Alignment> Matrix<N, T, A>

Source

pub const ZERO: Self

A matrix with all cells set to 0.0.

Source§

impl<const N: usize, T, A: Alignment> Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Zero + One,

Source

pub const IDENTITY: Self

The identity matrix, where diagonal cells are 1 and all other cells are 0. Corresponds to no transformation.

Source§

impl<const N: usize, T, A: Alignment> Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Nan,

Source

pub const NAN: Self

A matrix where all cells are NaNs (Not a Number).

Trait Implementations§

Source§

impl<const N: usize, T, A: Alignment> Add<&Matrix<N, T, A>> for Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Add<Output = T>,

Source§

type Output = Matrix<N, T, A>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<const N: usize, T, A: Alignment> Add<Matrix<N, T, A>> for &Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Add<Output = T>,

Source§

type Output = Matrix<N, T, A>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<const N: usize, T, A: Alignment> Add for &Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Add<Output = T>,

Source§

type Output = Matrix<N, T, A>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<const N: usize, T, A: Alignment> Add for Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Add<Output = T>,

Source§

type Output = Matrix<N, T, A>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<const N: usize, T, A: Alignment> AddAssign<&Matrix<N, T, A>> for Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Add<Output = T>,

Source§

fn add_assign(&mut self, rhs: &Self)

Performs the += operation. Read more
Source§

impl<const N: usize, T, A: Alignment> AddAssign for Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Add<Output = T>,

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

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

Source§

fn clone(&self) -> Self

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
Source§

impl<const N: usize, T, A: Alignment> Debug for Matrix<N, T, A>

Source§

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

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

impl<T, A: Alignment> Deref for Matrix<2, T, A>
where T: Scalar,

Source§

type Target = Xy<T, A>

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T, A: Alignment> Deref for Matrix<3, T, A>
where T: Scalar,

Source§

type Target = Xyz<T, A>

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T, A: Alignment> Deref for Matrix<4, T, A>
where T: Scalar,

Source§

type Target = Xyzw<T, A>

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T, A: Alignment> DerefMut for Matrix<2, T, A>
where T: Scalar,

Source§

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

Mutably dereferences the value.
Source§

impl<T, A: Alignment> DerefMut for Matrix<3, T, A>
where T: Scalar,

Source§

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

Mutably dereferences the value.
Source§

impl<T, A: Alignment> DerefMut for Matrix<4, T, A>
where T: Scalar,

Source§

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

Mutably dereferences the value.
Source§

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

Source§

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

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

impl<T, A: Alignment> From<(Matrix<2, T, A>,)> for Matrix<2, T, A>
where T: Scalar,

Source§

fn from(value: (Matrix<2, T, A>,)) -> Self

Converts to this type from the input type.
Source§

impl<T, A: Alignment> From<(Matrix<3, T, A>,)> for Matrix<3, T, A>
where T: Scalar,

Source§

fn from(value: (Matrix<3, T, A>,)) -> Self

Converts to this type from the input type.
Source§

impl<T, A: Alignment> From<(Matrix<4, T, A>,)> for Matrix<4, T, A>
where T: Scalar,

Source§

fn from(value: (Matrix<4, T, A>,)) -> Self

Converts to this type from the input type.
Source§

impl<T, A: Alignment> From<(Vector<2, T, A>, Vector<2, T, A>)> for Matrix<2, T, A>
where T: Scalar,

Source§

fn from(value: (Vector<2, T, A>, Vector<2, T, A>)) -> Self

Converts to this type from the input type.
Source§

impl<T, A: Alignment> From<(Vector<3, T, A>, Vector<3, T, A>, Vector<3, T, A>)> for Matrix<3, T, A>
where T: Scalar,

Source§

fn from(value: (Vector<3, T, A>, Vector<3, T, A>, Vector<3, T, A>)) -> Self

Converts to this type from the input type.
Source§

impl<T, A: Alignment> From<(Vector<4, T, A>, Vector<4, T, A>, Vector<4, T, A>, Vector<4, T, A>)> for Matrix<4, T, A>
where T: Scalar,

Source§

fn from( value: (Vector<4, T, A>, Vector<4, T, A>, Vector<4, T, A>, Vector<4, T, A>), ) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize, T, A: Alignment> Neg for &Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Neg<Output = T>,

Source§

type Output = Matrix<N, T, A>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<const N: usize, T, A: Alignment> Neg for Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Neg<Output = T>,

Source§

type Output = Matrix<N, T, A>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

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

Source§

fn eq(&self, other: &Self) -> bool

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

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

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

impl<const N: usize, T, A: Alignment> Sub<&Matrix<N, T, A>> for Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Sub<Output = T>,

Source§

type Output = Matrix<N, T, A>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<const N: usize, T, A: Alignment> Sub<Matrix<N, T, A>> for &Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Sub<Output = T>,

Source§

type Output = Matrix<N, T, A>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<const N: usize, T, A: Alignment> Sub for &Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Sub<Output = T>,

Source§

type Output = Matrix<N, T, A>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<const N: usize, T, A: Alignment> Sub for Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Sub<Output = T>,

Source§

type Output = Matrix<N, T, A>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<const N: usize, T, A: Alignment> SubAssign<&Matrix<N, T, A>> for Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Sub<Output = T>,

Source§

fn sub_assign(&mut self, rhs: &Self)

Performs the -= operation. Read more
Source§

impl<const N: usize, T, A: Alignment> SubAssign for Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Sub<Output = T>,

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

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

Source§

impl<const N: usize, T, A: Alignment> Eq for Matrix<N, T, A>
where Length<N>: SupportedLength, T: Scalar + Eq,

Source§

impl<const N: usize, T, A: Alignment> RefUnwindSafe for Matrix<N, T, A>

Source§

impl<const N: usize, T, A: Alignment> Send for Matrix<N, T, A>

Source§

impl<const N: usize, T, A: Alignment> Sync for Matrix<N, T, A>

Source§

impl<const N: usize, T, A: Alignment> Unpin for Matrix<N, T, A>

Source§

impl<const N: usize, T, A: Alignment> UnwindSafe for Matrix<N, T, A>

Auto Trait Implementations§

§

impl<const N: usize, T, A> Freeze for Matrix<N, T, A>
where <<T as Scalar>::Repr as ScalarRepr>::MatrixRepr<N, T, A>: Freeze,

§

impl<const N: usize, T, A> UnsafeUnpin for Matrix<N, T, A>
where <<T as Scalar>::Repr as ScalarRepr>::MatrixRepr<N, T, A>: UnsafeUnpin,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.