pub struct Mat2 {
pub a: f64,
pub b: f64,
pub c: f64,
pub d: f64,
}Expand description
A 2×2 matrix of f64 values.
The matrix is stored in row-major order:
| a b |
| c d |§Examples
use lars::{Mat2, Vec2};
let m = Mat2::IDENTITY;
let v = Vec2::ONE;
assert_eq!(m * v, v);Fields§
§a: f64Top-left element.
b: f64Top-right element.
c: f64Bottom-left element.
d: f64Bottom-right element.
Implementations§
Source§impl Mat2
impl Mat2
Sourcepub fn determinant(&self) -> f64
pub fn determinant(&self) -> f64
Returns the determinant of the matrix.
Computed as: [ \det(M) = ad - bc ]
§Examples
use lars::Mat2;
let m = Mat2::new(7.0, 2.0, 6.0, 2.0);
assert_eq!(m.determinant(), 2.0);Sourcepub fn inverse(&self) -> Mat2
pub fn inverse(&self) -> Mat2
Returns the inverse of the matrix, if it exists.
Computed as: [ M^{-1} = \frac{1}{\det(M)} \begin{bmatrix} d & -b \ -c & a \end{bmatrix} ]
§Panics
Panics if the matrix is singular (determinant = 0).
§Examples
use lars::Mat2;
let m = Mat2::new(7.0, 2.0, 6.0, 2.0);
assert_eq!(m.inverse(), Mat2::new(1.0, -1.0, -3.0, 3.5));Trait Implementations§
Source§impl Mul<Mat2> for f64
Implements scalar–matrix multiplication (f64 * Mat2).
impl Mul<Mat2> for f64
Implements scalar–matrix multiplication (f64 * Mat2).
§Examples
use lars::Mat2;
let m = Mat2::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(2.0 * m, Mat2::new(2.0, 4.0, 6.0, 8.0));Source§impl Mul<Vec2> for Mat2
Implements matrix–vector multiplication (Mat2 * Vec2).
impl Mul<Vec2> for Mat2
Implements matrix–vector multiplication (Mat2 * Vec2).
Performs the linear transformation of the vector by the matrix.
§[ \begin{bmatrix} a & b \ c & d \end{bmatrix} \begin{bmatrix} x \ y \end{bmatrix}
\begin{bmatrix} ax + by \ cx + dy \end{bmatrix} ]
§Examples
use lars::{Mat2, Vec2};
let m = Mat2::new(1.0, 2.0, 3.0, 4.0);
let v = Vec2::new(1.0, 1.0);
assert_eq!(m * v, Vec2::new(3.0, 7.0));Source§impl Mul<f64> for Mat2
Implements matrix–scalar multiplication (Mat2 * f64).
impl Mul<f64> for Mat2
Implements matrix–scalar multiplication (Mat2 * f64).
Each element of the matrix is scaled by the scalar.
§Examples
use lars::Mat2;
let m = Mat2::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(m * 2.0, Mat2::new(2.0, 4.0, 6.0, 8.0));Source§impl Mul for Mat2
Implements matrix–matrix multiplication (Mat2 * Mat2).
impl Mul for Mat2
Implements matrix–matrix multiplication (Mat2 * Mat2).
Standard linear algebra multiplication:
[ M_1 \times M_2 = \begin{bmatrix} a_1a_2 + b_1c_2 & a_1b_2 + b_1d_2 \ c_1a_2 + d_1c_2 & c_1b_2 + d_1d_2 \end{bmatrix} ]
§Examples
use lars::Mat2;
let a = Mat2::IDENTITY;
let b = Mat2::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(a * b, b);