pub struct Matrix3x3 { /* private fields */ }Expand description
A 3×3 f32 matrix stored in row-major order.
use embedded_matrix::Matrix3x3;
let m = Matrix3x3::identity();
assert!((m.det() - 1.0).abs() < 1e-5);
assert!((m.trace() - 3.0).abs() < 1e-5);Implementations§
Source§impl Matrix3x3
impl Matrix3x3
Sourcepub const fn from_diagonal(d: [f32; 3]) -> Self
pub const fn from_diagonal(d: [f32; 3]) -> Self
Diagonal matrix.
Sourcepub fn rotation_x(angle: f32) -> Self
pub fn rotation_x(angle: f32) -> Self
3D rotation around the X axis (radians, right-hand rule).
Available with the trig feature (uses embedded-trig-f32).
Sourcepub fn rotation_y(angle: f32) -> Self
pub fn rotation_y(angle: f32) -> Self
3D rotation around the Y axis (radians, right-hand rule).
Available with the trig feature.
Sourcepub fn rotation_z(angle: f32) -> Self
pub fn rotation_z(angle: f32) -> Self
3D rotation around the Z axis (radians, right-hand rule).
Available with the trig feature.
use embedded_matrix::Matrix3x3;
use embedded_trig_f32::consts::FRAC_PI_2;
let r = Matrix3x3::rotation_z(FRAC_PI_2);
let p = r * [1.0, 0.0, 5.0];
assert!(p[0].abs() < 1e-6 && (p[1] - 1.0).abs() < 1e-6 && p[2] == 5.0);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 3 or greater.
Sourcepub fn solve(&self, b: [f32; 3]) -> Result<[f32; 3], MatrixError>
pub fn solve(&self, b: [f32; 3]) -> Result<[f32; 3], MatrixError>
Solves the system A·x = b.
Computed as A⁻¹·b: same cost and singularity criterion as
inv. For multiple right-hand sides with the same
matrix, compute inv() only once.
use embedded_matrix::Matrix3x3;
let a = Matrix3x3::new([[1.0, 2.0, 3.0], [0.0, 1.0, 4.0], [5.0, 6.0, 0.0]]);
let x = a.solve([1.0, 2.0, 3.0]).unwrap();
assert!((x[0] - 27.0).abs() < 1e-3);
assert!((x[1] + 22.0).abs() < 1e-3);
assert!((x[2] - 6.0).abs() < 1e-3);§Errors
MatrixError::SingularMatrix: same criterion asinv.MatrixError::NonFiniteValue: NaN / ±∞ in the matrix orb, or overflow.
Sourcepub fn approx_eq(&self, other: &Self, tol: f32) -> bool
pub fn approx_eq(&self, other: &Self, tol: f32) -> bool
Compares two matrices element-wise with an absolute tolerance.
Returns false if an element is NaN.
Sourcepub fn inv(&self) -> Result<Self, MatrixError>
pub fn inv(&self) -> Result<Self, MatrixError>
Inverse using the cofactor method (adjugate matrix / determinant).
§Errors
MatrixError::SingularMatrixif|det|is negligible compared to the sum of the absolute values of the determinant terms (relative threshold; see the crate documentation).MatrixError::NonFiniteValueif the matrix contains NaN / ±∞ or the calculation overflows.
Trait Implementations§
Source§impl AddAssign for Matrix3x3
impl AddAssign for Matrix3x3
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreimpl Copy for Matrix3x3
Source§impl MulAssign for Matrix3x3
impl MulAssign for Matrix3x3
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl MulAssign<f32> for Matrix3x3
impl MulAssign<f32> for Matrix3x3
Source§fn mul_assign(&mut self, s: f32)
fn mul_assign(&mut self, s: f32)
*= operation. Read more