pub struct CMatrix<const R: usize, const C: usize>(/* private fields */);
Expand description
A const
matrix type, with dimensions checked at compile time
for all operations.
Implementations§
Source§impl<const R: usize, const C: usize> CMatrix<R, C>
impl<const R: usize, const C: usize> CMatrix<R, C>
Sourcepub const fn new(vals: [[f64; C]; R]) -> Self
pub const fn new(vals: [[f64; C]; R]) -> Self
Create a CMatrix
from a 2D array of f64
.
const ARRAY: [[f64; 2]; 2] = [
[4.0, 7.0],
[2.0, 6.0]
];
const CMATRIX: CMatrix::<2, 2> = CMatrix::new(ARRAY);
Sourcepub const fn new_from_soft(vals: [[Sf64; C]; R]) -> Self
pub const fn new_from_soft(vals: [[Sf64; C]; R]) -> Self
Equivalent to CMatrix::new
using const_soft_float::SoftF64
instead of f64.
Sourcepub const fn identity() -> Self
pub const fn identity() -> Self
Create an identity CMatrix
.
const LEFT: CMatrix<4, 3> = CMatrix::new([
[1.0, 0.0, 1.0],
[2.0, 1.0, 1.0],
[0.0, 1.0, 1.0],
[1.0, 1.0, 2.0],
]);
const RIGHT: CMatrix<3, 3> = CMatrix::identity();
const EXPECTED: [[f64; 3]; 4] = LEFT.finish();
const RESULT: [[f64; 3]; 4] = LEFT.mul(RIGHT).finish();
assert_eq!(EXPECTED, RESULT);
Sourcepub const fn finish(self) -> [[f64; C]; R]
pub const fn finish(self) -> [[f64; C]; R]
Converts a CMatrix
back into a two-dimensional array.
const ARRAY: [[f64; 2]; 2] = [
[4.0, 7.0],
[2.0, 6.0]
];
const CMATRIX: CMatrix::<2, 2> = CMatrix::new(ARRAY);
const RESULT: [[f64; 2]; 2] = CMATRIX.finish();
assert_eq!(ARRAY, RESULT)
Sourcepub const fn finish_soft(self) -> [[Sf64; C]; R]
pub const fn finish_soft(self) -> [[Sf64; C]; R]
CMatrix::finish
, but returns const_soft_float::SoftF64
Sourcepub const fn mul<const OC: usize>(self, rhs: CMatrix<C, OC>) -> CMatrix<R, OC>
pub const fn mul<const OC: usize>(self, rhs: CMatrix<C, OC>) -> CMatrix<R, OC>
Multiply two CMatrix
and return the result. Columns
of self and rows of multiplier must agree in number.
const LEFT: CMatrix<4, 3> = CMatrix::new([
[1.0, 0.0, 1.0],
[2.0, 1.0, 1.0],
[0.0, 1.0, 1.0],
[1.0, 1.0, 2.0],
]);
const RIGHT: CMatrix<3, 3> = CMatrix::new([
[1.0, 2.0, 1.0],
[2.0, 3.0, 1.0],
[4.0, 2.0, 2.0]
]);
const EXPECTED: [[f64; 3]; 4] = [
[5.0, 4.0, 3.0],
[8.0, 9.0, 5.0],
[6.0, 5.0, 3.0],
[11.0, 9.0, 6.0],
];
const RESULT: [[f64; 3]; 4] = LEFT.mul(RIGHT).finish();
assert_eq!(EXPECTED, RESULT);
Sourcepub const fn add(self, rhs: Self) -> Self
pub const fn add(self, rhs: Self) -> Self
Add two CMatrix
and return the result.
const LEFT: CMatrix<3, 3> = CMatrix::new([
[1.0, 0.0, 1.0],
[2.0, 1.0, 1.0],
[0.0, 1.0, 1.0]]
);
const RIGHT: CMatrix<3, 3> = CMatrix::new([
[1.0, 2.0, 1.0],
[2.0, 3.0, 1.0],
[4.0, 2.0, 2.0]]
);
const EXPECTED: [[f64; 3]; 3] = [
[2.0, 2.0, 2.0],
[4.0, 4.0, 2.0],
[4.0, 3.0, 3.0]
];
const RESULT: [[f64; 3]; 3] = LEFT.add(RIGHT).finish();
assert_eq!(EXPECTED, RESULT);
Sourcepub const fn sub(self, rhs: Self) -> Self
pub const fn sub(self, rhs: Self) -> Self
Subtract two CMatrix
and return the result.
const LEFT: CMatrix<3, 3> = CMatrix::new([
[1.0, 2.0, 1.0],
[2.0, 3.0, 1.0],
[4.0, 2.0, 2.0]]
);
const RIGHT: CMatrix<3, 3> = CMatrix::new([
[1.0, 0.0, 1.0],
[2.0, 1.0, 1.0],
[0.0, 1.0, 1.0]]
);
const EXPECTED: [[f64; 3]; 3] = [
[0.0, 2.0, 0.0],
[0.0, 2.0, 0.0],
[4.0, 1.0, 1.0]
];
const RESULT: [[f64; 3]; 3] = LEFT.sub(RIGHT).finish();
assert_eq!(EXPECTED, RESULT);
Sourcepub const fn apply_each(self, op: Operation) -> Self
pub const fn apply_each(self, op: Operation) -> Self
Apply an operation to each member of the matrix separately. Especially useful for scaling vectors
const BASE: CMatrix<1, 3> = CMatrix::new([[1.0, 2.0, 3.0]]);
const MUL: CMatrix<1, 3> = BASE.apply_each(Operation::Mul(3.0));
assert_eq!([[3.0, 6.0, 9.0]], MUL.finish());
Sourcepub const fn transpose(self) -> CMatrix<C, R>
pub const fn transpose(self) -> CMatrix<C, R>
Return the transpose of a CMatrix
.
const START: [[f64; 2]; 2] = [
[4.0, 7.0],
[2.0, 6.0]
];
const EXPECTED: [[f64; 2]; 2] = [
[4.0, 2.0],
[7.0, 6.0]
];
const RESULT: [[f64; 2]; 2] =
CMatrix::new(START).transpose().finish();
assert_eq!(EXPECTED, RESULT)
pub const fn givens_l(self, m: usize, a: Sf64, b: Sf64) -> Self
pub const fn givens_r(self, m: usize, a: Sf64, b: Sf64) -> Self
pub const fn pinv(self, epsilon: f64) -> CMatrix<C, R>
Source§impl<const N: usize> CMatrix<1, N>
impl<const N: usize> CMatrix<1, N>
Sourcepub const fn new_vector(vals: [f64; N]) -> CVector<N>
pub const fn new_vector(vals: [f64; N]) -> CVector<N>
Special case of CMatrix::new
for constructing a CVector
Always returns a row vector, follow with transpose
to build
a column vector
const ARRAY: [f64; 2] = [4.0, 7.0];
const ROWVECTOR: CVector::<2> = CVector::new_vector(ARRAY);
pub const fn new_vector_from_soft(vals: [Sf64; N]) -> Self
Sourcepub const fn dot(self, other: Self) -> f64
pub const fn dot(self, other: Self) -> f64
Dot product of two CVector
of the same size.
const LEFT: CVector<3> = CVector::new_vector([1.0, 3.0, -5.0]);
const RIGHT: CVector<3> = CVector::new_vector([4.0, -2.0, -1.0]);
const EXPECTED: f64 = 3.0;
const RESULT: f64 = LEFT.dot(RIGHT);
assert_eq!(EXPECTED, RESULT)
Sourcepub const fn finish_vector(self) -> [f64; N]
pub const fn finish_vector(self) -> [f64; N]
Special case of CMatrix::finish
for use with a CVector,
returns [f64 ; N]
instead of [[f64 ; N]; 1]
const ARRAY: [f64; 2] = [4.0, 7.0];
const CVECTOR: CVector::<2> = CVector::new_vector(ARRAY);
const RESULT: [f64; 2] = CVECTOR.finish_vector();
assert_eq!(ARRAY, RESULT)
Sourcepub const fn finish_vector_soft(self) -> [Sf64; N]
pub const fn finish_vector_soft(self) -> [Sf64; N]
CVector::finish_vector
, but returns soft floats