pub struct Matrix<T, const M: usize, const N: usize> { /* private fields */ }
Expand description

An M by N matrix of Ts.

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

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);

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);

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]),
]);

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]),
]);

Swap two rows of a matrix.

Swap two columns of a matrix.

Matrix-Matrix left-division.

This performs a generalised Gauss-Jordan elimination to calculate rhs^-1 * self.

See Div<Matrix<TRhs, N, N>>.

Construct a column vector from an array.

Examples

Constructing a Vector:

use sized_matrix::{Vector, Matrix};

let vector: Vector<i32, 3> = Vector::vector([1, 2, 3]);

assert_eq!(vector, Matrix::cols([[1, 2, 3]]));

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

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.

The resulting type after applying the / operator.

Performs the / operation. Read more

Matrix-Scalar division.

The resulting type after applying the / operator.

Performs the / operation. Read more

Matrix-Matrix division.

See Div<Matrix<TRhs, N, N>>

Performs the /= operation. Read more

Matrix-Scalar division.

Performs the /= operation. Read more

Returns self · rhs.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

Initialise an instance of this type using value by applying elem to each ‘index’ of the type. Read more

Initialise an instance of this type by applying elem to each ‘index’ of the type. Read more

The result after applying the operator.

Returns the multiplicative inverse of self. Read more

The type of values being mapped over.

The generic type of the result after mapping.

Apply a function to the inner values of this type.

Matrix-Matrix multiplication.

The resulting type after applying the * operator.

Performs the * operation. Read more

Matrix-Scalar multiplication.

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the fused multiply-add.

Performs the fused multiply-add operation.

Performs the fused multiply-add operation.

Matrix-Matrix multiplication.

Performs the *= operation. Read more

Matrix-Scalar multiplication.

Performs the *= operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

Returns the multiplicative identity element of Self, 1. Read more

Sets self to the multiplicative identity element of Self, 1.

Returns true if self is equal to the multiplicative identity. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

The result after applying the operator.

Returns self to the power rhs. Read more

Get a section of type TOut starting at offset offset. Read more

Get a section of type TOut starting at offset offset. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Returns the transpose of self.

Returns the additive identity element of Self, 0. Read more

Returns true if self is equal to the additive identity.

Sets self to the additive identity element of Self, 0.

The type parameter of Self. Read more

The generic type of Self. Read more

Apply f to each pair of elements in self and rhs and return the results.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.