Skip to main content

reda_geometry/base/
mat2.rs

1use crate::traits::CoordType;
2use crate::base::Vec2;
3
4/// A 2x2 matrix of the form:
5/// [[ m11, m12 ],
6///  [ m21, m22 ]]
7#[derive(Clone, Hash, PartialEq, Eq, Debug)]
8pub struct Matrix2<T: CoordType> {
9    pub(crate) m11: T,
10    pub(crate) m12: T,
11    pub(crate) m21: T,
12    pub(crate) m22: T,
13}
14
15impl<T> Matrix2<T>
16where
17    T: CoordType,
18{
19    /// Create a new 2x2 matrix with entries of the form:
20    /// ```txt
21    /// [[ m11, m12 ],
22    ///  [ m21, m22 ]]
23    /// ```
24    pub fn new(m11: T, m12: T, m21: T, m22: T) -> Self {
25        Matrix2 { m11, m12, m21, m22 }
26    }
27
28    pub fn identity() -> Self {
29        Self::new(T::one(), T::zero(), T::zero(), T::one())
30    }
31
32    pub fn scaler_mul(&self, rhs: T) -> Self {
33        Matrix2::new(
34            self.m11 * rhs,
35            self.m12 * rhs,
36            self.m21 * rhs,
37            self.m22 * rhs,
38        )
39    }
40
41    pub fn mul_vec(&self, rhs: Vec2<T>) -> Vec2<T> {
42        Vec2::new(
43            rhs.x * self.m11 + rhs.y * self.m12,
44            rhs.x * self.m21 + rhs.y * self.m22,
45        )
46    }
47
48    pub fn mat_mul(&self, rhs: &Self) -> Self {
49        let a = self;
50        let b = rhs;
51        let c11 = a.m11 * b.m11 + a.m12 * b.m21;
52        let c12 = a.m11 * b.m12 + a.m12 * b.m22;
53        let c21 = a.m21 * b.m11 + a.m22 * b.m21;
54        let c22 = a.m21 * b.m12 + a.m22 * b.m22;
55        Self::new(c11, c12, c21, c22)
56    }
57
58    pub fn transpose(&self) -> Self {
59        Self::new(self.m11, self.m21, self.m12, self.m22)
60    }
61
62    pub fn determinant(&self) -> T {
63        self.m11 * self.m22 - self.m12 * self.m21
64    }
65
66    pub fn is_identity(&self) -> bool {
67        self == &Self::identity()
68    }
69
70    pub fn is_unitary(&self) -> bool {
71        self.mat_mul(&self.transpose()).is_identity()
72    }
73
74    pub fn try_inverse(&self) -> Option<Self> {
75        let det = self.determinant();
76        if !det.is_zero() {
77            let z = T::zero();
78            Some(Self::new(
79                self.m22 / det,
80                z - self.m12 / det,
81                z - self.m21 / det,
82                self.m11 / det,
83            ))
84        } else {
85            None
86        }
87    }
88}
89
90impl<T: CoordType> Default for Matrix2<T> {
91    fn default() -> Self {
92        Self::identity()
93    }
94}
95
96#[cfg(test)]
97mod test {
98    use crate::base::Matrix2;
99
100    #[test]
101    fn test_matrix_multiplication() {
102        let a = Matrix2::new(1.0, 2.0, 3.0, 4.0);
103        let b = Matrix2::new(5.0, 6.0, 7.0, 8.0);
104        let id = Matrix2::identity();
105        assert_eq!(id.mat_mul(&id), id);
106        assert_eq!(b.mat_mul(&id), b);
107        assert_eq!(id.mat_mul(&b), b);
108        assert_eq!(
109            a.mat_mul(&b),
110            Matrix2::new(19.0, 22.0, 15.0 + 28.0, 18.0 + 32.0)
111        );
112    }
113    
114    #[test]
115    fn test_inverse() {
116        let m = Matrix2::new(2.0, 1.0, 4.0, 8.0);
117        let i = m.try_inverse().unwrap();
118        assert_eq!(m.mat_mul(&i), Matrix2::identity());
119        assert_eq!(i.mat_mul(&m), Matrix2::identity());
120    }
121    
122}