Struct constgebra::CMatrix

source ·
pub struct CMatrix<const R: usize, const C: usize>(_);
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>

source

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

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

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

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

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

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

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

pub const fn givens_l(self, m: usize, a: Sf64, b: Sf64) -> Self

source

pub const fn givens_r(self, m: usize, a: Sf64, b: Sf64) -> Self

source

pub const fn svd(self, epsilon: f64) -> CMatrix<C, R>

Singular Value Decomposition

source

pub const fn pinv(self, epsilon: f64) -> CMatrix<C, R>

source§

impl<const N: usize> CMatrix<1, N>

source

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

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)

Trait Implementations§

source§

impl<const R: usize, const C: usize> Clone for CMatrix<R, C>

source§

fn clone(&self) -> CMatrix<R, C>

Returns a copy 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 R: usize, const C: usize> Default for CMatrix<R, C>

source§

fn default() -> Self

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

impl<const R: usize, const C: usize> PartialEq<CMatrix<R, C>> for CMatrix<R, C>

source§

fn eq(&self, other: &CMatrix<R, C>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const R: usize, const C: usize> PartialOrd<CMatrix<R, C>> for CMatrix<R, C>

source§

fn partial_cmp(&self, other: &CMatrix<R, C>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<const R: usize, const C: usize> Copy for CMatrix<R, C>

source§

impl<const R: usize, const C: usize> StructuralPartialEq for CMatrix<R, C>

Auto Trait Implementations§

§

impl<const R: usize, const C: usize> RefUnwindSafe for CMatrix<R, C>

§

impl<const R: usize, const C: usize> Send for CMatrix<R, C>

§

impl<const R: usize, const C: usize> Sync for CMatrix<R, C>

§

impl<const R: usize, const C: usize> Unpin for CMatrix<R, C>

§

impl<const R: usize, const C: usize> UnwindSafe for CMatrix<R, C>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 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 Twhere 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<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.