Skip to main content

glam/f64/
dmat2.rs

1// Generated from mat.rs.tera template. Edit the template, not the generated file.
2
3use crate::{f64::math, swizzles::*, DMat3, DVec2, Mat2};
4use core::fmt;
5use core::iter::{Product, Sum};
6use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
7
8#[cfg(feature = "zerocopy-08")]
9use zerocopy_derive_08::*;
10
11/// Creates a 2x2 matrix from two column vectors.
12#[inline(always)]
13#[must_use]
14pub const fn dmat2(x_axis: DVec2, y_axis: DVec2) -> DMat2 {
15    DMat2::from_cols(x_axis, y_axis)
16}
17
18/// A 2x2 column major matrix.
19#[derive(Clone, Copy)]
20#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
21#[cfg_attr(
22    feature = "zerocopy-08",
23    derive(FromBytes, Immutable, IntoBytes, KnownLayout)
24)]
25#[cfg_attr(feature = "cuda", repr(align(16)))]
26#[repr(C)]
27pub struct DMat2 {
28    pub x_axis: DVec2,
29    pub y_axis: DVec2,
30}
31
32impl DMat2 {
33    /// A 2x2 matrix with all elements set to `0.0`.
34    pub const ZERO: Self = Self::from_cols(DVec2::ZERO, DVec2::ZERO);
35
36    /// A 2x2 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
37    pub const IDENTITY: Self = Self::from_cols(DVec2::X, DVec2::Y);
38
39    /// All NAN:s.
40    pub const NAN: Self = Self::from_cols(DVec2::NAN, DVec2::NAN);
41
42    #[allow(clippy::too_many_arguments)]
43    #[inline(always)]
44    #[must_use]
45    const fn new(m00: f64, m01: f64, m10: f64, m11: f64) -> Self {
46        Self {
47            x_axis: DVec2::new(m00, m01),
48            y_axis: DVec2::new(m10, m11),
49        }
50    }
51
52    /// Creates a 2x2 matrix from two column vectors.
53    ///
54    /// See also [`Self::from_rows`] when the data is in row major order.
55    #[inline(always)]
56    #[must_use]
57    pub const fn from_cols(x_axis: DVec2, y_axis: DVec2) -> Self {
58        Self { x_axis, y_axis }
59    }
60
61    /// Creates a 2x2 matrix from two row vectors.
62    ///
63    /// Matrices are stored in column major order, so the given rows are permuted into
64    /// the matrix layout. Use [`Self::from_cols`] instead when the data is already in
65    /// column major order.
66    #[inline(always)]
67    #[must_use]
68    pub const fn from_rows(row0: DVec2, row1: DVec2) -> Self {
69        let [m00, m01] = row0.to_array();
70        let [m10, m11] = row1.to_array();
71        Self::new(m00, m10, m01, m11)
72    }
73
74    /// Creates a 2x2 matrix from a `[f64; 4]` array stored in column major order.
75    ///
76    /// If the data is in row major order use [`Self::from_rows_array`] instead.
77    #[inline]
78    #[must_use]
79    pub const fn from_cols_array(m: &[f64; 4]) -> Self {
80        Self::new(m[0], m[1], m[2], m[3])
81    }
82
83    /// Creates a `[f64; 4]` array storing data in column major order.
84    ///
85    /// If you require the data in row major order use [`Self::to_rows_array`] instead.
86    #[inline]
87    #[must_use]
88    pub const fn to_cols_array(&self) -> [f64; 4] {
89        [self.x_axis.x, self.x_axis.y, self.y_axis.x, self.y_axis.y]
90    }
91
92    /// Creates a 2x2 matrix from a `[[f64; 2]; 2]` 2D array stored in column major order.
93    ///
94    /// If the data is in row major order `transpose` the returned matrix.
95    #[inline]
96    #[must_use]
97    pub const fn from_cols_array_2d(m: &[[f64; 2]; 2]) -> Self {
98        Self::from_cols(DVec2::from_array(m[0]), DVec2::from_array(m[1]))
99    }
100
101    /// Creates a `[[f64; 2]; 2]` 2D array storing data in column major order.
102    ///
103    /// If you require row major order `transpose` the matrix first.
104    #[inline]
105    #[must_use]
106    pub const fn to_cols_array_2d(&self) -> [[f64; 2]; 2] {
107        [self.x_axis.to_array(), self.y_axis.to_array()]
108    }
109
110    /// Creates a 2x2 matrix from a `[f64; 4]` array stored in row major order.
111    ///
112    /// Matrices are stored in column major order, so the array is permuted into the
113    /// matrix layout. Use [`Self::from_cols_array`] instead when the data is already in
114    /// column major order.
115    #[inline]
116    #[must_use]
117    pub const fn from_rows_array(m: &[f64; 4]) -> Self {
118        Self::new(m[0], m[2], m[1], m[3])
119    }
120
121    /// Creates a `[f64; 4]` array storing data in row major order.
122    ///
123    /// Matrices are stored in column major order, so the array is permuted out of the
124    /// column major storage. Use [`Self::to_cols_array`] instead when you want data in
125    /// column major order.
126    #[inline]
127    #[must_use]
128    pub const fn to_rows_array(&self) -> [f64; 4] {
129        let m = self.to_cols_array();
130        [m[0], m[2], m[1], m[3]]
131    }
132
133    /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0.
134    #[doc(alias = "scale")]
135    #[inline]
136    #[must_use]
137    pub const fn from_diagonal(diagonal: DVec2) -> Self {
138        Self::new(diagonal.x, 0.0, 0.0, diagonal.y)
139    }
140
141    /// Creates a 2x2 matrix containing the combining non-uniform `scale` and rotation of
142    /// `angle` (in radians).
143    #[inline]
144    #[must_use]
145    pub fn from_scale_angle(scale: DVec2, angle: f64) -> Self {
146        let (sin, cos) = math::sin_cos(angle);
147        Self::new(cos * scale.x, sin * scale.x, -sin * scale.y, cos * scale.y)
148    }
149
150    /// Creates a 2x2 matrix containing a rotation of `angle` (in radians).
151    #[inline]
152    #[must_use]
153    pub fn from_angle(angle: f64) -> Self {
154        let (sin, cos) = math::sin_cos(angle);
155        Self::new(cos, sin, -sin, cos)
156    }
157
158    /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
159    #[inline]
160    #[must_use]
161    pub fn from_mat3(m: DMat3) -> Self {
162        Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
163    }
164
165    /// Creates a 2x2 matrix from the minor of the given 3x3 matrix, discarding the `i`th column
166    /// and `j`th row.
167    ///
168    /// # Panics
169    ///
170    /// Panics if `i` or `j` is greater than 2.
171    #[inline]
172    #[must_use]
173    #[track_caller]
174    pub fn from_mat3_minor(m: DMat3, i: usize, j: usize) -> Self {
175        match (i, j) {
176            (0, 0) => Self::from_cols(m.y_axis.yz(), m.z_axis.yz()),
177            (0, 1) => Self::from_cols(m.y_axis.xz(), m.z_axis.xz()),
178            (0, 2) => Self::from_cols(m.y_axis.xy(), m.z_axis.xy()),
179            (1, 0) => Self::from_cols(m.x_axis.yz(), m.z_axis.yz()),
180            (1, 1) => Self::from_cols(m.x_axis.xz(), m.z_axis.xz()),
181            (1, 2) => Self::from_cols(m.x_axis.xy(), m.z_axis.xy()),
182            (2, 0) => Self::from_cols(m.x_axis.yz(), m.y_axis.yz()),
183            (2, 1) => Self::from_cols(m.x_axis.xz(), m.y_axis.xz()),
184            (2, 2) => Self::from_cols(m.x_axis.xy(), m.y_axis.xy()),
185            _ => panic!("index out of bounds"),
186        }
187    }
188
189    /// Creates a 2x2 matrix from the first 4 values in `slice`.
190    ///
191    /// See also [`Self::from_rows_slice`] when the slice is in row major order.
192    ///
193    /// # Panics
194    ///
195    /// Panics if `slice` is less than 4 elements long.
196    #[inline]
197    #[must_use]
198    #[track_caller]
199    pub const fn from_cols_slice(slice: &[f64]) -> Self {
200        Self::new(slice[0], slice[1], slice[2], slice[3])
201    }
202
203    /// Writes the columns of `self` to the first 4 elements in `slice`.
204    ///
205    /// # Panics
206    ///
207    /// Panics if `slice` is less than 4 elements long.
208    #[inline]
209    #[track_caller]
210    pub fn write_cols_to_slice(&self, slice: &mut [f64]) {
211        slice[0] = self.x_axis.x;
212        slice[1] = self.x_axis.y;
213        slice[2] = self.y_axis.x;
214        slice[3] = self.y_axis.y;
215    }
216
217    /// Creates a 2x2 matrix from the first 4 values in `slice`, stored in row
218    /// major order.
219    ///
220    /// Matrices are stored in column major order, so the slice is permuted into the
221    /// matrix layout. Use [`Self::from_cols_slice`] instead when the slice is already in
222    /// column major order.
223    ///
224    /// # Panics
225    ///
226    /// Panics if `slice` is less than 4 elements long.
227    #[inline]
228    #[must_use]
229    #[track_caller]
230    pub const fn from_rows_slice(slice: &[f64]) -> Self {
231        Self::new(slice[0], slice[2], slice[1], slice[3])
232    }
233
234    /// Returns the matrix column for the given `index`.
235    ///
236    /// # Panics
237    ///
238    /// Panics if `index` is greater than 1.
239    #[inline]
240    #[must_use]
241    #[track_caller]
242    pub fn col(&self, index: usize) -> DVec2 {
243        match index {
244            0 => self.x_axis,
245            1 => self.y_axis,
246            _ => panic!("index out of bounds"),
247        }
248    }
249
250    /// Returns a mutable reference to the matrix column for the given `index`.
251    ///
252    /// # Panics
253    ///
254    /// Panics if `index` is greater than 1.
255    #[inline]
256    #[track_caller]
257    pub fn col_mut(&mut self, index: usize) -> &mut DVec2 {
258        match index {
259            0 => &mut self.x_axis,
260            1 => &mut self.y_axis,
261            _ => panic!("index out of bounds"),
262        }
263    }
264
265    /// Returns the matrix row for the given `index`.
266    ///
267    /// See also [`Self::set_row`] when you need to change the row.
268    ///
269    /// # Panics
270    ///
271    /// Panics if `index` is greater than 1.
272    #[inline]
273    #[must_use]
274    #[track_caller]
275    pub fn row(&self, index: usize) -> DVec2 {
276        match index {
277            0 => DVec2::new(self.x_axis.x, self.y_axis.x),
278            1 => DVec2::new(self.x_axis.y, self.y_axis.y),
279            _ => panic!("index out of bounds"),
280        }
281    }
282
283    /// Sets the matrix row for the given `index`.
284    ///
285    /// Matrices are stored in column major order, so the row is spread across all
286    /// 2 columns and writing it touches every column. Use [`Self::col_mut`]
287    /// instead when you can work with columns. See also [`Self::row`].
288    ///
289    /// # Panics
290    ///
291    /// Panics if `index` is greater than 1.
292    #[inline]
293    #[track_caller]
294    pub fn set_row(&mut self, index: usize, row: DVec2) {
295        match index {
296            0 => {
297                self.x_axis.x = row.x;
298                self.y_axis.x = row.y;
299            }
300            1 => {
301                self.x_axis.y = row.x;
302                self.y_axis.y = row.y;
303            }
304            _ => panic!("index out of bounds"),
305        }
306    }
307
308    /// Returns `true` if, and only if, all elements are finite.
309    /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
310    #[inline]
311    #[must_use]
312    pub fn is_finite(&self) -> bool {
313        self.x_axis.is_finite() && self.y_axis.is_finite()
314    }
315
316    /// Returns `true` if any elements are `NaN`.
317    #[inline]
318    #[must_use]
319    pub fn is_nan(&self) -> bool {
320        self.x_axis.is_nan() || self.y_axis.is_nan()
321    }
322
323    /// Returns the transpose of `self`.
324    #[inline]
325    #[must_use]
326    pub fn transpose(&self) -> Self {
327        Self {
328            x_axis: DVec2::new(self.x_axis.x, self.y_axis.x),
329            y_axis: DVec2::new(self.x_axis.y, self.y_axis.y),
330        }
331    }
332
333    /// Returns the diagonal of `self`.
334    #[inline]
335    #[must_use]
336    pub fn diagonal(&self) -> DVec2 {
337        DVec2::new(self.x_axis.x, self.y_axis.y)
338    }
339
340    /// Returns the determinant of `self`.
341    #[inline]
342    #[must_use]
343    pub fn determinant(&self) -> f64 {
344        self.x_axis.x * self.y_axis.y - self.x_axis.y * self.y_axis.x
345    }
346
347    /// If `CHECKED` is true then if the determinant is zero this function will return a tuple
348    /// containing a zero matrix and false. If the determinant is non zero a tuple containing the
349    /// inverted matrix and true is returned.
350    ///
351    /// If `CHECKED` is false then the determinant is not checked and if it is zero the resulting
352    /// inverted matrix will be invalid. Will panic if the resulting inverted matrix is not finite
353    /// when `glam_assert` is enabled.
354    ///
355    /// A tuple containing the inverted matrix and a bool is used instead of an option here as
356    /// regular Rust enums put the discriminant first which can result in a lot of padding if the
357    /// matrix is aligned.
358    #[inline(always)]
359    #[must_use]
360    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
361    fn inverse_checked<const CHECKED: bool>(&self) -> (Self, bool) {
362        let inv_det = 1.0 / self.determinant();
363        let m = Self::new(
364            self.y_axis.y * inv_det,
365            self.x_axis.y * -inv_det,
366            self.y_axis.x * -inv_det,
367            self.x_axis.x * inv_det,
368        );
369        if CHECKED {
370            if !m.is_finite() {
371                return (Self::ZERO, false);
372            }
373        } else {
374            glam_assert!(m.is_finite());
375        }
376        (m, true)
377    }
378
379    /// Returns the inverse of `self`.
380    ///
381    /// If the matrix is not invertible the returned matrix will be invalid. The
382    /// returned matrix will also be invalid if the inverse is not finite, which can
383    /// happen when `self` contains very large or very small values. Use
384    /// [`Self::try_inverse`] or [`Self::inverse_or_zero`] to detect these cases.
385    ///
386    /// # Panics
387    ///
388    /// Will panic if the resulting inverted matrix is not finite when `glam_assert`
389    /// is enabled.
390    #[inline]
391    #[must_use]
392    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
393    pub fn inverse(&self) -> Self {
394        self.inverse_checked::<false>().0
395    }
396
397    /// Returns the inverse of `self` or `None` if the matrix is not invertible, or if
398    /// the inverse is not finite.
399    #[inline]
400    #[must_use]
401    pub fn try_inverse(&self) -> Option<Self> {
402        let (m, is_valid) = self.inverse_checked::<true>();
403        if is_valid {
404            Some(m)
405        } else {
406            None
407        }
408    }
409
410    /// Returns the inverse of `self` or `DMat2::ZERO` if the matrix is not
411    /// invertible, or if the inverse is not finite.
412    #[inline]
413    #[must_use]
414    pub fn inverse_or_zero(&self) -> Self {
415        self.inverse_checked::<true>().0
416    }
417
418    /// Transforms a 2D vector.
419    #[inline]
420    #[must_use]
421    pub fn mul_vec2(&self, rhs: DVec2) -> DVec2 {
422        #[allow(clippy::suspicious_operation_groupings)]
423        DVec2::new(
424            (self.x_axis.x * rhs.x) + (self.y_axis.x * rhs.y),
425            (self.x_axis.y * rhs.x) + (self.y_axis.y * rhs.y),
426        )
427    }
428
429    /// Transforms a 2D vector by the transpose of `self`.
430    #[inline]
431    #[must_use]
432    pub fn mul_transpose_vec2(&self, rhs: DVec2) -> DVec2 {
433        DVec2::new(self.x_axis.dot(rhs), self.y_axis.dot(rhs))
434    }
435
436    /// Multiplies two 2x2 matrices.
437    #[inline]
438    #[must_use]
439    pub fn mul_mat2(&self, rhs: &Self) -> Self {
440        self.mul(rhs)
441    }
442
443    /// Adds two 2x2 matrices.
444    #[inline]
445    #[must_use]
446    pub fn add_mat2(&self, rhs: &Self) -> Self {
447        self.add(rhs)
448    }
449
450    /// Subtracts two 2x2 matrices.
451    #[inline]
452    #[must_use]
453    pub fn sub_mat2(&self, rhs: &Self) -> Self {
454        self.sub(rhs)
455    }
456
457    /// Multiplies a 2x2 matrix by a scalar.
458    #[inline]
459    #[must_use]
460    pub fn mul_scalar(&self, rhs: f64) -> Self {
461        Self::from_cols(self.x_axis.mul(rhs), self.y_axis.mul(rhs))
462    }
463
464    /// Multiply `self` by a scaling vector `scale`.
465    /// This is faster than creating a whole diagonal scaling matrix and then multiplying that.
466    /// This operation is commutative.
467    #[inline]
468    #[must_use]
469    pub fn mul_diagonal_scale(&self, scale: DVec2) -> Self {
470        Self::from_cols(self.x_axis * scale.x, self.y_axis * scale.y)
471    }
472
473    /// Divides a 2x2 matrix by a scalar.
474    #[inline]
475    #[must_use]
476    pub fn div_scalar(&self, rhs: f64) -> Self {
477        let rhs = DVec2::splat(rhs);
478        Self::from_cols(self.x_axis.div(rhs), self.y_axis.div(rhs))
479    }
480
481    /// Returns a matrix containing the reciprocal `1.0/n` of each element of `self`.
482    #[inline]
483    #[must_use]
484    pub fn recip(&self) -> Self {
485        Self::from_cols(self.x_axis.recip(), self.y_axis.recip())
486    }
487
488    /// Returns true if the absolute difference of all elements between `self` and `rhs`
489    /// is less than or equal to `max_abs_diff`.
490    ///
491    /// This can be used to compare if two matrices contain similar elements. It works best
492    /// when comparing with a known value. The `max_abs_diff` that should be used used
493    /// depends on the values being compared against.
494    ///
495    /// For more see
496    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
497    #[inline]
498    #[must_use]
499    pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f64) -> bool {
500        self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
501            && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
502    }
503
504    /// Takes the absolute value of each element in `self`
505    #[inline]
506    #[must_use]
507    pub fn abs(&self) -> Self {
508        Self::from_cols(self.x_axis.abs(), self.y_axis.abs())
509    }
510
511    #[cfg(feature = "f64")]
512    #[inline]
513    #[must_use]
514    pub fn as_mat2(&self) -> Mat2 {
515        Mat2::from_cols(self.x_axis.as_vec2(), self.y_axis.as_vec2())
516    }
517}
518
519impl Default for DMat2 {
520    #[inline]
521    fn default() -> Self {
522        Self::IDENTITY
523    }
524}
525
526impl Add for DMat2 {
527    type Output = Self;
528    #[inline]
529    fn add(self, rhs: Self) -> Self {
530        Self::from_cols(self.x_axis.add(rhs.x_axis), self.y_axis.add(rhs.y_axis))
531    }
532}
533
534impl Add<&Self> for DMat2 {
535    type Output = Self;
536    #[inline]
537    fn add(self, rhs: &Self) -> Self {
538        self.add(*rhs)
539    }
540}
541
542impl Add<&DMat2> for &DMat2 {
543    type Output = DMat2;
544    #[inline]
545    fn add(self, rhs: &DMat2) -> DMat2 {
546        (*self).add(*rhs)
547    }
548}
549
550impl Add<DMat2> for &DMat2 {
551    type Output = DMat2;
552    #[inline]
553    fn add(self, rhs: DMat2) -> DMat2 {
554        (*self).add(rhs)
555    }
556}
557
558impl AddAssign for DMat2 {
559    #[inline]
560    fn add_assign(&mut self, rhs: Self) {
561        *self = self.add(rhs);
562    }
563}
564
565impl AddAssign<&Self> for DMat2 {
566    #[inline]
567    fn add_assign(&mut self, rhs: &Self) {
568        self.add_assign(*rhs);
569    }
570}
571
572impl Sub for DMat2 {
573    type Output = Self;
574    #[inline]
575    fn sub(self, rhs: Self) -> Self {
576        Self::from_cols(self.x_axis.sub(rhs.x_axis), self.y_axis.sub(rhs.y_axis))
577    }
578}
579
580impl Sub<&Self> for DMat2 {
581    type Output = Self;
582    #[inline]
583    fn sub(self, rhs: &Self) -> Self {
584        self.sub(*rhs)
585    }
586}
587
588impl Sub<&DMat2> for &DMat2 {
589    type Output = DMat2;
590    #[inline]
591    fn sub(self, rhs: &DMat2) -> DMat2 {
592        (*self).sub(*rhs)
593    }
594}
595
596impl Sub<DMat2> for &DMat2 {
597    type Output = DMat2;
598    #[inline]
599    fn sub(self, rhs: DMat2) -> DMat2 {
600        (*self).sub(rhs)
601    }
602}
603
604impl SubAssign for DMat2 {
605    #[inline]
606    fn sub_assign(&mut self, rhs: Self) {
607        *self = self.sub(rhs);
608    }
609}
610
611impl SubAssign<&Self> for DMat2 {
612    #[inline]
613    fn sub_assign(&mut self, rhs: &Self) {
614        self.sub_assign(*rhs);
615    }
616}
617
618impl Neg for DMat2 {
619    type Output = Self;
620    #[inline]
621    fn neg(self) -> Self::Output {
622        Self::from_cols(self.x_axis.neg(), self.y_axis.neg())
623    }
624}
625
626impl Neg for &DMat2 {
627    type Output = DMat2;
628    #[inline]
629    fn neg(self) -> DMat2 {
630        (*self).neg()
631    }
632}
633
634impl Mul for DMat2 {
635    type Output = Self;
636    #[inline]
637    fn mul(self, rhs: Self) -> Self {
638        Self::from_cols(self.mul(rhs.x_axis), self.mul(rhs.y_axis))
639    }
640}
641
642impl Mul<&Self> for DMat2 {
643    type Output = Self;
644    #[inline]
645    fn mul(self, rhs: &Self) -> Self {
646        self.mul(*rhs)
647    }
648}
649
650impl Mul<&DMat2> for &DMat2 {
651    type Output = DMat2;
652    #[inline]
653    fn mul(self, rhs: &DMat2) -> DMat2 {
654        (*self).mul(*rhs)
655    }
656}
657
658impl Mul<DMat2> for &DMat2 {
659    type Output = DMat2;
660    #[inline]
661    fn mul(self, rhs: DMat2) -> DMat2 {
662        (*self).mul(rhs)
663    }
664}
665
666impl MulAssign for DMat2 {
667    #[inline]
668    fn mul_assign(&mut self, rhs: Self) {
669        *self = self.mul(rhs);
670    }
671}
672
673impl MulAssign<&Self> for DMat2 {
674    #[inline]
675    fn mul_assign(&mut self, rhs: &Self) {
676        self.mul_assign(*rhs);
677    }
678}
679
680impl Mul<DVec2> for DMat2 {
681    type Output = DVec2;
682    #[inline]
683    fn mul(self, rhs: DVec2) -> Self::Output {
684        self.mul_vec2(rhs)
685    }
686}
687
688impl Mul<&DVec2> for DMat2 {
689    type Output = DVec2;
690    #[inline]
691    fn mul(self, rhs: &DVec2) -> DVec2 {
692        self.mul(*rhs)
693    }
694}
695
696impl Mul<&DVec2> for &DMat2 {
697    type Output = DVec2;
698    #[inline]
699    fn mul(self, rhs: &DVec2) -> DVec2 {
700        (*self).mul(*rhs)
701    }
702}
703
704impl Mul<DVec2> for &DMat2 {
705    type Output = DVec2;
706    #[inline]
707    fn mul(self, rhs: DVec2) -> DVec2 {
708        (*self).mul(rhs)
709    }
710}
711
712impl Mul<DMat2> for f64 {
713    type Output = DMat2;
714    #[inline]
715    fn mul(self, rhs: DMat2) -> Self::Output {
716        rhs.mul_scalar(self)
717    }
718}
719
720impl Mul<&DMat2> for f64 {
721    type Output = DMat2;
722    #[inline]
723    fn mul(self, rhs: &DMat2) -> DMat2 {
724        self.mul(*rhs)
725    }
726}
727
728impl Mul<&DMat2> for &f64 {
729    type Output = DMat2;
730    #[inline]
731    fn mul(self, rhs: &DMat2) -> DMat2 {
732        (*self).mul(*rhs)
733    }
734}
735
736impl Mul<DMat2> for &f64 {
737    type Output = DMat2;
738    #[inline]
739    fn mul(self, rhs: DMat2) -> DMat2 {
740        (*self).mul(rhs)
741    }
742}
743
744impl Mul<f64> for DMat2 {
745    type Output = Self;
746    #[inline]
747    fn mul(self, rhs: f64) -> Self {
748        self.mul_scalar(rhs)
749    }
750}
751
752impl Mul<&f64> for DMat2 {
753    type Output = Self;
754    #[inline]
755    fn mul(self, rhs: &f64) -> Self {
756        self.mul(*rhs)
757    }
758}
759
760impl Mul<&f64> for &DMat2 {
761    type Output = DMat2;
762    #[inline]
763    fn mul(self, rhs: &f64) -> DMat2 {
764        (*self).mul(*rhs)
765    }
766}
767
768impl Mul<f64> for &DMat2 {
769    type Output = DMat2;
770    #[inline]
771    fn mul(self, rhs: f64) -> DMat2 {
772        (*self).mul(rhs)
773    }
774}
775
776impl MulAssign<f64> for DMat2 {
777    #[inline]
778    fn mul_assign(&mut self, rhs: f64) {
779        *self = self.mul(rhs);
780    }
781}
782
783impl MulAssign<&f64> for DMat2 {
784    #[inline]
785    fn mul_assign(&mut self, rhs: &f64) {
786        self.mul_assign(*rhs);
787    }
788}
789
790impl Div<DMat2> for f64 {
791    type Output = DMat2;
792    #[inline]
793    fn div(self, rhs: DMat2) -> Self::Output {
794        DMat2::from_cols(self.div(rhs.x_axis), self.div(rhs.y_axis))
795    }
796}
797
798impl Div<&DMat2> for f64 {
799    type Output = DMat2;
800    #[inline]
801    fn div(self, rhs: &DMat2) -> DMat2 {
802        self.div(*rhs)
803    }
804}
805
806impl Div<&DMat2> for &f64 {
807    type Output = DMat2;
808    #[inline]
809    fn div(self, rhs: &DMat2) -> DMat2 {
810        (*self).div(*rhs)
811    }
812}
813
814impl Div<DMat2> for &f64 {
815    type Output = DMat2;
816    #[inline]
817    fn div(self, rhs: DMat2) -> DMat2 {
818        (*self).div(rhs)
819    }
820}
821
822impl Div<f64> for DMat2 {
823    type Output = Self;
824    #[inline]
825    fn div(self, rhs: f64) -> Self {
826        self.div_scalar(rhs)
827    }
828}
829
830impl Div<&f64> for DMat2 {
831    type Output = Self;
832    #[inline]
833    fn div(self, rhs: &f64) -> Self {
834        self.div(*rhs)
835    }
836}
837
838impl Div<&f64> for &DMat2 {
839    type Output = DMat2;
840    #[inline]
841    fn div(self, rhs: &f64) -> DMat2 {
842        (*self).div(*rhs)
843    }
844}
845
846impl Div<f64> for &DMat2 {
847    type Output = DMat2;
848    #[inline]
849    fn div(self, rhs: f64) -> DMat2 {
850        (*self).div(rhs)
851    }
852}
853
854impl DivAssign<f64> for DMat2 {
855    #[inline]
856    fn div_assign(&mut self, rhs: f64) {
857        *self = self.div(rhs);
858    }
859}
860
861impl DivAssign<&f64> for DMat2 {
862    #[inline]
863    fn div_assign(&mut self, rhs: &f64) {
864        self.div_assign(*rhs);
865    }
866}
867
868impl Sum<Self> for DMat2 {
869    fn sum<I>(iter: I) -> Self
870    where
871        I: Iterator<Item = Self>,
872    {
873        iter.fold(Self::ZERO, Self::add)
874    }
875}
876
877impl<'a> Sum<&'a Self> for DMat2 {
878    fn sum<I>(iter: I) -> Self
879    where
880        I: Iterator<Item = &'a Self>,
881    {
882        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
883    }
884}
885
886impl Product for DMat2 {
887    fn product<I>(iter: I) -> Self
888    where
889        I: Iterator<Item = Self>,
890    {
891        iter.fold(Self::IDENTITY, Self::mul)
892    }
893}
894
895impl<'a> Product<&'a Self> for DMat2 {
896    fn product<I>(iter: I) -> Self
897    where
898        I: Iterator<Item = &'a Self>,
899    {
900        iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
901    }
902}
903
904impl PartialEq for DMat2 {
905    #[inline]
906    fn eq(&self, rhs: &Self) -> bool {
907        self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
908    }
909}
910
911impl AsRef<[f64; 4]> for DMat2 {
912    #[inline]
913    fn as_ref(&self) -> &[f64; 4] {
914        unsafe { &*(self as *const Self as *const [f64; 4]) }
915    }
916}
917
918impl AsMut<[f64; 4]> for DMat2 {
919    #[inline]
920    fn as_mut(&mut self) -> &mut [f64; 4] {
921        unsafe { &mut *(self as *mut Self as *mut [f64; 4]) }
922    }
923}
924
925impl fmt::Debug for DMat2 {
926    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
927        fmt.debug_struct(stringify!(DMat2))
928            .field("x_axis", &self.x_axis)
929            .field("y_axis", &self.y_axis)
930            .finish()
931    }
932}
933
934impl fmt::Display for DMat2 {
935    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
936        if let Some(p) = f.precision() {
937            write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
938        } else {
939            write!(f, "[{}, {}]", self.x_axis, self.y_axis)
940        }
941    }
942}