pub struct Matrix3x3 { /* private fields */ }Expand description
Represents a 3x3 linear_algebra with f32 values in row-major order.
Internal representation is [[f32; 3]; 3].
Implementations§
Source§impl Matrix3x3
impl Matrix3x3
Sourcepub fn new(data: [[f32; 3]; 3]) -> Self
pub fn new(data: [[f32; 3]; 3]) -> Self
Creates a new 3x3 linear_algebra from the provided 2D array data.
The data should be in row-major order: data[row][column].
§Example
use crate::bb_geometry::linear_algebra::matrix3x3::*;
let linear_algebra = Matrix3x3::new([
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0],
]);
assert_eq!(linear_algebra.get(0, 1), 2.0);Sourcepub fn identity() -> Self
pub fn identity() -> Self
Creates an identity linear_algebra (1.0 on the diagonal, 0.0 elsewhere).
§Example
use crate::bb_geometry::linear_algebra::matrix3x3::*;
let identity = Matrix3x3::identity();
assert_eq!(identity.get(0, 0), 1.0);
assert_eq!(identity.get(1, 1), 1.0);
assert_eq!(identity.get(0, 1), 0.0);Sourcepub fn zero() -> Self
pub fn zero() -> Self
Creates a zero linear_algebra (all elements are 0.0).
§Example
use crate::bb_geometry::linear_algebra::matrix3x3::*;
let zero = Matrix3x3::zero();
assert_eq!(zero.get(1, 2), 0.0);Sourcepub fn get(&self, row: usize, col: usize) -> f32
pub fn get(&self, row: usize, col: usize) -> f32
Gets the value at the specified row and column using a method call.
Prefer using the index operator linear_algebra[(row, col)] for more idiomatic access.
§Panics
Panics if row or col are out of bounds (>= 3).
§Example
use crate::bb_geometry::linear_algebra::matrix3x3::*;
let linear_algebra = Matrix3x3::identity();
assert_eq!(linear_algebra.get(1, 1), 1.0);Sourcepub fn set(&mut self, row: usize, col: usize, value: f32)
pub fn set(&mut self, row: usize, col: usize, value: f32)
Sets the value at the specified row and column using a method call.
Prefer using the mutable index operator linear_algebra[(row, col)] = value
for more idiomatic modification.
§Panics
Panics if row or col are out of bounds (>= 3).
§Example
use crate::bb_geometry::linear_algebra::matrix3x3::*;
let mut linear_algebra = Matrix3x3::zero();
linear_algebra.set(2, 1, 5.0);
assert_eq!(linear_algebra.get(2, 1), 5.0);Sourcepub fn transpose(&self) -> Self
pub fn transpose(&self) -> Self
Returns the transpose of the linear_algebra.
The element at (row, col) in the original linear_algebra becomes
the element at (col, row) in the transposed linear_algebra.
Does not modify the original linear_algebra.
§Example
use crate::bb_geometry::linear_algebra::matrix3x3::*;
let linear_algebra = Matrix3x3::new([
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0],
]);
let transposed = linear_algebra.transpose();
assert_eq!(transposed.get(0, 1), 4.0); // linear_algebra.get(1, 0)
assert_eq!(transposed.get(1, 0), 2.0); // linear_algebra.get(0, 1)
assert_eq!(transposed.get(2, 1), 6.0); // linear_algebra.get(1, 2)Sourcepub fn multiply(&self, other: &Self) -> Self
pub fn multiply(&self, other: &Self) -> Self
Performs linear_algebra multiplication: self * other.
This method delegates to the * operator implementation provided by Mul.
Does not modify the original matrices.
§Example
use crate::bb_geometry::linear_algebra::matrix3x3::*;
let a = Matrix3x3::new([
[1.0, 2.0, 0.0],
[3.0, 4.0, 0.0],
[0.0, 0.0, 1.0],
]);
let b = Matrix3x3::new([
[5.0, 6.0, 0.0],
[7.0, 8.0, 0.0],
[0.0, 0.0, 1.0],
]);
let result = a.multiply(&b);
// 1*5 + 2*7 = 19
assert_eq!(result.get(0, 0), 19.0);
// 1*6 + 2*8 = 22
assert_eq!(result.get(0, 1), 22.0);
// 3*5 + 4*7 = 15 + 28 = 43
assert_eq!(result.get(1, 0), 43.0);
// 3*6 + 4*8 = 18 + 32 = 50
assert_eq!(result.get(1, 1), 50.0);Sourcepub fn is_almost_zero(&self) -> bool
pub fn is_almost_zero(&self) -> bool
Checks if a linear_algebra has components not exceeding EPSILON.
§Example
use crate::bb_geometry::linear_algebra::matrix3x3::*;
use crate::bb_geometry::linear_algebra::EPSILON;
let zero_matrix = Matrix3x3::zero();
let matrix_with_epsilon_entries = Matrix3x3::new([[EPSILON, EPSILON, 0.0], [0.0, 0.0, 0.0], [0.0, EPSILON, 0.0]]);
let matrix_with_entry_exceeding_epsilon = Matrix3x3::new([[EPSILON, 1.1* EPSILON, 0.0], [0.0, 0.0, 0.0], [0.0, EPSILON, 0.0]]);
let zero_matrix_is_almost_zero = zero_matrix.is_almost_zero();
let matrix_with_epsilon_entries_almost_zero = matrix_with_epsilon_entries.is_almost_zero();
let matrix_with_entry_exceeding_epsilon_is_not_almost_zero = !matrix_with_entry_exceeding_epsilon.is_almost_zero();
assert!(zero_matrix_is_almost_zero);
assert!(matrix_with_epsilon_entries_almost_zero);
assert!(matrix_with_entry_exceeding_epsilon_is_not_almost_zero);Trait Implementations§
Source§impl Index<(usize, usize)> for Matrix3x3
Allows accessing linear_algebra elements using tuple indexing linear_algebra[(row, col)].
impl Index<(usize, usize)> for Matrix3x3
Allows accessing linear_algebra elements using tuple indexing linear_algebra[(row, col)].
Source§impl IndexMut<(usize, usize)> for Matrix3x3
Allows mutating linear_algebra elements using tuple indexing linear_algebra[(row, col)] = value.
impl IndexMut<(usize, usize)> for Matrix3x3
Allows mutating linear_algebra elements using tuple indexing linear_algebra[(row, col)] = value.
Source§impl Mul<&Matrix3x3> for &Matrix3x3
Implements linear_algebra multiplication using the * operator (&Matrix3x3 * &Matrix3x3).
impl Mul<&Matrix3x3> for &Matrix3x3
Implements linear_algebra multiplication using the * operator (&Matrix3x3 * &Matrix3x3).
This is often the most convenient way to perform linear_algebra multiplication.
§Example
use crate::bb_geometry::linear_algebra::matrix3x3::*;
let a = Matrix3x3::identity();
let b = Matrix3x3::new([
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0],
]);
let result = &a * &b; // Multiplying by identity returns the original
assert_eq!(result, b);
let c = Matrix3x3::new([
[2.0, 0.0, 0.0],
[0.0, 3.0, 0.0],
[0.0, 0.0, 1.0],
]);
let scaled_b = &b * &c; // Scale columns of b
assert_eq!(scaled_b[(0, 0)], 2.0); // 1*2
assert_eq!(scaled_b[(1, 1)], 15.0); // 5*3
assert_eq!(scaled_b[(2, 2)], 9.0); // 9*1Source§impl Mul<&Vector3> for &Matrix3x3
Implements Matrix * Vector multiplication (&Matrix3x3 * &Vector3).
Treats the vector as a column vector.
impl Mul<&Vector3> for &Matrix3x3
Implements Matrix * Vector multiplication (&Matrix3x3 * &Vector3).
Treats the vector as a column vector.