Debian Codegraph @

debian/missing-sources/glam/src/f32/scalar/mat2.rs

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