reda_geometry/base/
mat2.rs

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