pub struct Matrix2x2 { /* private fields */ }Expand description
A 2×2 f32 matrix stored in row-major order.
use embedded_matrix::Matrix2x2;
let m = Matrix2x2::new([[1.0, 2.0], [3.0, 4.0]]);
assert!((m.det() - (-2.0)).abs() < 1e-5);
assert_eq!(m[(1, 0)], 3.0);Implementations§
Source§impl Matrix2x2
impl Matrix2x2
Sourcepub const fn from_diagonal(d: [f32; 2]) -> Matrix2x2
pub const fn from_diagonal(d: [f32; 2]) -> Matrix2x2
Diagonal matrix.
Sourcepub fn get(&self, row: usize, col: usize) -> f32
pub fn get(&self, row: usize, col: usize) -> f32
Returns the element at row row and column col (zero-indexed).
§Panics
Panics if row or col is 2 or greater.
Sourcepub fn solve(&self, b: [f32; 2]) -> Result<[f32; 2], MatrixError>
pub fn solve(&self, b: [f32; 2]) -> Result<[f32; 2], MatrixError>
Solves the system A·x = b (Cramer’s rule).
More direct than inv() followed by a multiplication. For multiple
right-hand sides with the same matrix, compute inv() only once.
use embedded_matrix::Matrix2x2;
let a = Matrix2x2::new([[2.0, 1.0], [1.0, 3.0]]);
let x = a.solve([3.0, 5.0]).unwrap();
assert!((x[0] - 0.8).abs() < 1e-6 && (x[1] - 1.4).abs() < 1e-6);§Errors
MatrixError::SingularMatrix: same criterion asinv.MatrixError::NonFiniteValue: NaN / ±∞ in the matrix orb, or overflow.
Sourcepub fn approx_eq(&self, other: &Matrix2x2, tol: f32) -> bool
pub fn approx_eq(&self, other: &Matrix2x2, tol: f32) -> bool
Compares two matrices element-wise with an absolute tolerance.
Returns false if an element is NaN.
Sourcepub fn inv(&self) -> Result<Matrix2x2, MatrixError>
pub fn inv(&self) -> Result<Matrix2x2, MatrixError>
Matrix inverse.
§Errors
MatrixError::SingularMatrixif|det|is negligible compared to|ad| + |bc|(relative threshold; see the crate documentation).MatrixError::NonFiniteValueif the matrix contains NaN / ±∞ or the calculation overflows.
Trait Implementations§
Source§impl AddAssign for Matrix2x2
impl AddAssign for Matrix2x2
Source§fn add_assign(&mut self, rhs: Matrix2x2)
fn add_assign(&mut self, rhs: Matrix2x2)
Performs the
+= operation. Read moreimpl Copy for Matrix2x2
Source§impl MulAssign for Matrix2x2
impl MulAssign for Matrix2x2
Source§fn mul_assign(&mut self, rhs: Matrix2x2)
fn mul_assign(&mut self, rhs: Matrix2x2)
Performs the
*= operation. Read moreSource§impl MulAssign<f32> for Matrix2x2
impl MulAssign<f32> for Matrix2x2
Source§fn mul_assign(&mut self, s: f32)
fn mul_assign(&mut self, s: f32)
Performs the
*= operation. Read moreimpl StructuralPartialEq for Matrix2x2
Auto Trait Implementations§
impl Forget for Matrix2x2
impl Freeze for Matrix2x2
impl Move for Matrix2x2
impl RefUnwindSafe for Matrix2x2
impl Send for Matrix2x2
impl Sync for Matrix2x2
impl Unpin for Matrix2x2
impl UnsafeUnpin for Matrix2x2
impl UnwindSafe for Matrix2x2
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more