Expand description
This crate provides a stack-allocated, constant-size Matrix<T, M, N>
type implemented using const generics.
ยง๐ Getting started
Add this crate to your Cargo manifest.
cargo add vectrix
no_std
is also supported by disabling the default std feature.
cargo add vectrix --no-default-features --features=macro
ยง๐คธ Usage
ยงTypes
The base Matrix<T, M, N>
type represents a matrix with M
rows and N
columns. This type is a backed by an array of arrays. The data is stored in
column-major order. Some convenient aliases are provided for common
matrices, like vectors.
Matrix<T, M, N>
โ a generic matrix type withM
rows andN
columns.Vector<T, M>
โ a column vector withM
rows.RowVector<T, N>
โ a row vector withN
columns.
ยงMacros
Macros are provided for easy construction of the provided types. These
macros will also work in const
contexts.
-
The
matrix!
macro can be used to construct a newMatrix
of any size.let m = matrix![ 1, 3, 5; 2, 4, 6; ];
In the above example
matrix
is aMatrix<_, 2, 3>
type, having 2 rows and 3 columns. -
The
vector!
androw_vector!
macros can be used to to construct column and row vectors respectively.let v = vector![1, 3, 3, 7]; // ^ type `Vector<_, 4>` assert_eq!(v, matrix![1; 3; 3; 7]); let v = row_vector![1, 3, 3, 7]; // ^^^^^^ type `RowVector<_, 4>` assert_eq!(v, matrix![1, 3, 3, 7]);
ยงConstructors
Commonly used constructors are listed below.
::zero()
โ constructs a new matrix filled withT::zero()
.::identity()
โ constructs a new identity matrix.::repeat(..)
โ constructs a new matrix filled with the provided value.::repeat_with(..)
โ constructs a new matrix filled with values computed by the provided closure.::from_iter(..)
โ constructs a new matrix from an iterator.::new(..)
โ constructs a new vector using the provided components.
ยงAccessing elements
Three types of element access are available.
-
usize
indexing selects the nth element in the matrix as viewed in column-major order.let m = matrix![ 1, 2, 3; 4, 5, 6; ]; assert_eq!(m[1], 4);
-
(usize, usize)
indexing selects the element at a particular row and column position.let m = matrix![ 1, 2, 3; 4, 5, 6; ]; assert_eq!(m[(1, 0)], 4);
-
Component accessors are available for small vectors using traditional names.
let mut v = vector![1, 2, 3, 4, 0, 0]; v.y = 3; v.w = 7; assert_eq!(v.x, 1); assert_eq!(v.y, 3); assert_eq!(v.z, 3); assert_eq!(v.w, 7); assert_eq!(v.a, 0); assert_eq!(v.b, 0);
ยงAccessing a row or column
You can get a reference to particular row or column using the
.row()
or .column()
methods. You
can get a mutable reference using the _mut
variants.
let mut m = matrix![
1, 2, 3;
4, 7, 6;
];
let row = m.row_mut(1);
row[1] = 5;
assert_eq!(m.column(1), &[2, 5]);
ยงIteration
Element-wise, column-major order iteration is provided using the following methods.
.into_iter()
โ consumes the matrix and returns an owned iterator over each element..iter()
โ returns an iterator over a reference to each element..iter_mut()
โ returns an iterator over a mutable reference to each element.
Iteration over rows and columns is provide using the following methods.
.iter_rows()
โ returns an iterator over a reference to each row..iter_rows_mut()
โ returns an iterator over mutable reference to each row..iter_columns()
โ returns an iterator over a reference to each column..iter_columns_mut()
โ returns an iterator over a mutable reference to each column.
ยงSlice representation
A slice view of the underlying data is provided using
.as_slice()
and
.as_mut_slice()
.
let mut m = matrix![
1, 3, 5;
2, 3, 6;
];
m.as_mut_slice()[3] = 4;
assert_eq!(m.as_slice(), &[1, 2, 3, 4, 5, 6]);
ยงDebug
The Debug
implementation will print out vectors as
lists and matrices as a list of lists in column-major order.
let v = vector![1.1, 2.0];
let m = matrix![1, 2; 3, 4];
println!("vector: {:.2?}", v);
println!("matrix: {:?}", m);
This will output:
vector: [1.10, 2.00]
matrix: [[1, 3], [2, 4]]
ยงDisplay
The Display
implementation will print out the
matrix in the traditional box bracket format. Precision is supported as well
as most of the other formatting traits like
LowerHex
.
let cv = vector![1.1, 2.0];
let rv = row_vector![1.1, 2.0];
let m = matrix![1, 2; 3, 4];
println!("column vector: {:.2}", cv);
println!("row vector: {:.1}", rv);
println!("matrix: {:b}", m);
This will output:
column vector:
โ โ
โ 1.10 โ
โ 2.00 โ
โ โ
row vector:
โ โ
โ 1.1 2.0 โ
โ โ
matrix:
โ โ
โ 1 10 โ
โ 11 100 โ
โ โ
ยงOperations
Matrix
implements many built-in operators. With scalar operands almost
all operators are implemented and they simply apply the operation to each
element in the matrix. Unary operators will do the equivalent. In the
following example each element in the matrix is multiplied by 2.
let m = matrix![
1, -3;
3, -7;
];
let exp = matrix![
2, -6;
6, -14;
];
assert_eq!(m * 2, exp);
Matrix
supports addition and subtraction with same size matrices for
element-wise addition and subtraction. In the following example a matrix
is added to itself.
let m = matrix![
1, -3;
3, -7;
];
let exp = matrix![
2, -6;
6, -14;
];
assert_eq!(m + m, exp);
Macrosยง
- matrix
- A macro for composing matrices.
- row_
vector - A macro for composing row vectors.
- vector
- A macro for composing vectors.
Structsยง
- Column
- A column in a
Matrix
. - Into
Iter - An iterator that moves out of a matrix.
- Iter
Columns - An iterator over the columns in a matrix.
- Iter
Columns Mut - A mutable iterator over the columns in a matrix.
- Iter
Rows - An iterator over the rows in a matrix.
- Iter
Rows Mut - A mutable iterator over the rows in a matrix.
- Matrix
- Represents a matrix with constant
M
rows and constantN
columns. - Row
- A row in a
Matrix
.
Traitsยง
- Abs
- Defines the absolute value for a type.
- Matrix
Index - A helper trait used for indexing operations.
- One
- Defines a multiplicative identity element for a type.
- Zero
- Defines a additive identity element for a type.