1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#![allow(dead_code)]

use std::{
    fmt::Display,
    ops::{Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Sub, SubAssign},
};

use crate::algebra::{vec2d, Mat2f, Mat3d, Mat4d, Vec2d};

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Mat2d {
    x_axis: Vec2d,
    y_axis: Vec2d,
}

pub fn mat2d(m00: f64, m01: f64, m10: f64, m11: f64) -> Mat2d {
    Mat2d::from_array([m00, m01, m10, m11])
}

impl Display for Mat2d {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Mat2d({}, {} | {}, {})",
            self.x_axis.x(),
            self.x_axis.y(),
            self.y_axis.x(),
            self.y_axis.y()
        )
    }
}

impl Default for Mat2d {
    fn default() -> Self {
        Self {
            x_axis: Vec2d::default(),
            y_axis: Vec2d::default(),
        }
    }
}

impl Add<Mat2d> for Mat2d {
    type Output = Mat2d;

    fn add(self, rhs: Mat2d) -> Self::Output {
        Mat2d::new(self.x_axis + rhs.x_axis, self.y_axis + rhs.y_axis)
    }
}

impl Add<f64> for Mat2d {
    type Output = Mat2d;

    fn add(self, rhs: f64) -> Self::Output {
        Mat2d::new(self.x_axis + rhs, self.y_axis + rhs)
    }
}

impl Add<Mat2d> for f64 {
    type Output = Mat2d;

    fn add(self, rhs: Mat2d) -> Self::Output {
        Mat2d::new(self + rhs.x_axis, self + rhs.y_axis)
    }
}

impl AddAssign<Mat2d> for Mat2d {
    fn add_assign(&mut self, rhs: Mat2d) {
        *self = *self + rhs;
    }
}

impl AddAssign<f64> for Mat2d {
    fn add_assign(&mut self, rhs: f64) {
        *self = *self + rhs;
    }
}

impl Sub<Mat2d> for Mat2d {
    type Output = Mat2d;

    fn sub(self, rhs: Mat2d) -> Self::Output {
        Mat2d::new(self.x_axis - rhs.x_axis, self.y_axis - rhs.y_axis)
    }
}

impl Sub<f64> for Mat2d {
    type Output = Mat2d;

    fn sub(self, rhs: f64) -> Self::Output {
        Mat2d::new(self.x_axis - rhs, self.y_axis - rhs)
    }
}

impl Sub<Mat2d> for f64 {
    type Output = Mat2d;

    fn sub(self, rhs: Mat2d) -> Self::Output {
        Mat2d::new(self - rhs.x_axis, self - rhs.y_axis)
    }
}

impl SubAssign<Mat2d> for Mat2d {
    fn sub_assign(&mut self, rhs: Mat2d) {
        *self = *self - rhs;
    }
}

impl SubAssign<f64> for Mat2d {
    fn sub_assign(&mut self, rhs: f64) {
        *self = *self - rhs;
    }
}

impl Mul<Mat2d> for Mat2d {
    type Output = Mat2d;

    fn mul(self, rhs: Mat2d) -> Self::Output {
        let m00 = self.x_axis.dot(vec2d(rhs[0][0], rhs[1][0]));
        let m01 = self.x_axis.dot(vec2d(rhs[0][1], rhs[1][1]));
        let m10 = self.y_axis.dot(vec2d(rhs[0][0], rhs[1][0]));
        let m11 = self.y_axis.dot(vec2d(rhs[0][1], rhs[1][1]));
        Mat2d::new(vec2d(m00, m01), vec2d(m10, m11))
    }
}

impl Mul<Vec2d> for Mat2d {
    type Output = Vec2d;

    fn mul(self, rhs: Vec2d) -> Self::Output {
        let v0 = self.x_axis.dot(rhs);
        let v1 = self.y_axis.dot(rhs);
        vec2d(v0, v1)
    }
}

impl Mul<f64> for Mat2d {
    type Output = Mat2d;

    fn mul(self, rhs: f64) -> Self::Output {
        Mat2d::new(self.x_axis * rhs, self.y_axis * rhs)
    }
}

impl Mul<Mat2d> for f64 {
    type Output = Mat2d;

    fn mul(self, rhs: Mat2d) -> Self::Output {
        Mat2d::new(self * rhs.x_axis, self * rhs.y_axis)
    }
}

impl MulAssign<Mat2d> for Mat2d {
    fn mul_assign(&mut self, rhs: Mat2d) {
        *self = *self * rhs;
    }
}

impl MulAssign<f64> for Mat2d {
    fn mul_assign(&mut self, rhs: f64) {
        *self = *self * rhs;
    }
}

impl Div<f64> for Mat2d {
    type Output = Mat2d;

    fn div(self, rhs: f64) -> Self::Output {
        Mat2d::new(self.x_axis / rhs, self.y_axis / rhs)
    }
}

impl DivAssign<f64> for Mat2d {
    fn div_assign(&mut self, rhs: f64) {
        *self = *self / rhs;
    }
}

impl Index<usize> for Mat2d {
    type Output = Vec2d;

    fn index(&self, index: usize) -> &Self::Output {
        match index {
            0 => &self.x_axis,
            1 => &self.y_axis,
            _ => panic!("`rmath::algebra::Mat2d::index`: index out of bounds."),
        }
    }
}

impl IndexMut<usize> for Mat2d {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        match index {
            0 => &mut self.x_axis,
            1 => &mut self.y_axis,
            _ => panic!("`rmath::algebra::Mat2d::index_mut`: index out of bounds."),
        }
    }
}

impl Mat2d {
    pub fn new(x_axis: Vec2d, y_axis: Vec2d) -> Self {
        Self { x_axis, y_axis }
    }

    pub fn zero() -> Self {
        Self::new(vec2d(0.0, 0.0), vec2d(0.0, 0.0))
    }

    pub fn identity() -> Self {
        Self::new(vec2d(1.0, 0.0), vec2d(0.0, 1.0))
    }
}

impl Mat2d {
    pub fn col(&self, index: usize) -> Vec2d {
        match index {
            0 => vec2d(self[0][0], self[1][0]),
            1 => vec2d(self[0][1], self[1][1]),
            _ => panic!("`rmath::algebra::Mat2d::col`: index out of bounds."),
        }
    }

    pub fn row(&self, index: usize) -> Vec2d {
        match index {
            0 => self.x_axis,
            1 => self.y_axis,
            _ => panic!("`rmath::algebra::Mat2d::row`: index out of bounds."),
        }
    }

    pub fn is_nan(&self) -> bool {
        self.x_axis.is_nan() || self.y_axis.is_nan()
    }

    pub fn is_infinite(&self) -> bool {
        self.x_axis.is_infinite() || self.y_axis.is_infinite()
    }

    pub fn is_finite(&self) -> bool {
        self.x_axis.is_finite() && self.y_axis.is_finite()
    }

    pub fn transpose(&self) -> Self {
        let (m00, m01) = self.x_axis.to_tuple();
        let (m10, m11) = self.y_axis.to_tuple();
        Self::new(vec2d(m00, m10), vec2d(m01, m11))
    }

    pub fn determinant(&self) -> f64 {
        let (m00, m01) = self.x_axis.to_tuple();
        let (m10, m11) = self.y_axis.to_tuple();
        m00 * m11 - m01 * m10
    }

    pub fn inverse(&self) -> Self {
        let det = self.determinant();
        ruby_assert!(!(det.abs() < f64::EPSILON));

        let inv_det = det.recip();
        let a00 = self[1][1];
        let a01 = -self[1][0];
        let a10 = -self[0][1];
        let a11 = self[0][0];

        Self::new(vec2d(a00, a10), vec2d(a01, a11)).mul(inv_det)
    }

    pub fn try_inverse(&self) -> Option<Self> {
        let det = self.determinant();
        if det.abs() < f64::EPSILON {
            return None;
        }

        let inv_det = det.recip();
        let a00 = self[1][1];
        let a01 = -self[1][0];
        let a10 = -self[0][1];
        let a11 = self[0][0];

        Some(Self::new(vec2d(a00, a10), vec2d(a01, a11)).mul(inv_det))
    }
}

impl From<Mat3d> for Mat2d {
    fn from(m: Mat3d) -> Self {
        mat2d(m[0][0], m[0][1], m[1][0], m[1][1])
    }
}

impl From<Mat4d> for Mat2d {
    fn from(m: Mat4d) -> Self {
        mat2d(m[0][0], m[0][1], m[1][0], m[1][1])
    }
}

impl Mat2d {
    pub fn from_array(m: [f64; 4]) -> Self {
        Self::new(vec2d(m[0], m[1]), vec2d(m[2], m[3]))
    }

    pub fn from_array_2d(m: [[f64; 2]; 2]) -> Self {
        Self::new(vec2d(m[0][0], m[0][1]), vec2d(m[1][0], m[1][1]))
    }

    pub fn from_diagonal(diagonal: Vec2d) -> Self {
        Self::new(vec2d(diagonal.x(), 0.0), vec2d(0.0, diagonal.y()))
    }
}

impl Mat2d {
    pub fn to_array(self) -> [f64; 4] {
        [self[0][0], self[0][1], self[1][0], self[1][1]]
    }

    pub fn to_array_2d(self) -> [[f64; 2]; 2] {
        [[self[0][0], self[0][1]], [self[1][0], self[1][1]]]
    }

    pub fn to_mat3d(self) -> Mat3d {
        Mat3d::from(self)
    }

    pub fn to_mat4d(self) -> Mat4d {
        Mat4d::from(self)
    }

    pub fn to_mat2f(self) -> Mat2f {
        Mat2f::new(self.x_axis.to_vec2f(), self.y_axis.to_vec2f())
    }
}