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
// Adds common vector trait implementations.
// The traits here should be supported for all types of $t and all sizes of vector.
macro_rules! impl_matn_common_traits {
    ($t:ty, $matn:ident, $vecn:ident) => {
        impl Default for $matn {
            #[inline(always)]
            fn default() -> Self {
                Self::IDENTITY
            }
        }

        impl Add<$matn> for $matn {
            type Output = Self;
            #[inline(always)]
            fn add(self, other: Self) -> Self::Output {
                Self(self.0.add_matrix(&other.0))
            }
        }

        impl AddAssign<$matn> for $matn {
            #[inline(always)]
            fn add_assign(&mut self, other: Self) {
                self.0 = self.0.add_matrix(&other.0);
            }
        }

        impl Sub<$matn> for $matn {
            type Output = Self;
            #[inline(always)]
            fn sub(self, other: Self) -> Self::Output {
                Self(self.0.sub_matrix(&other.0))
            }
        }

        impl SubAssign<$matn> for $matn {
            #[inline(always)]
            fn sub_assign(&mut self, other: Self) {
                self.0 = self.0.sub_matrix(&other.0);
            }
        }

        impl Mul<$matn> for $matn {
            type Output = Self;
            #[inline(always)]
            fn mul(self, other: Self) -> Self::Output {
                Self(self.0.mul_matrix(&other.0))
            }
        }

        impl MulAssign<$matn> for $matn {
            #[inline(always)]
            fn mul_assign(&mut self, other: Self) {
                self.0 = self.0.mul_matrix(&other.0);
            }
        }

        impl Mul<$vecn> for $matn {
            type Output = $vecn;
            #[inline(always)]
            fn mul(self, other: $vecn) -> Self::Output {
                $vecn(self.0.mul_vector(other.0))
            }
        }

        impl Mul<$matn> for $t {
            type Output = $matn;
            #[inline(always)]
            fn mul(self, other: $matn) -> Self::Output {
                $matn(other.0.mul_scalar(self))
            }
        }

        impl Mul<$t> for $matn {
            type Output = Self;
            #[inline(always)]
            fn mul(self, other: $t) -> Self::Output {
                Self(self.0.mul_scalar(other))
            }
        }

        impl MulAssign<$t> for $matn {
            #[inline(always)]
            fn mul_assign(&mut self, other: $t) {
                self.0 = self.0.mul_scalar(other);
            }
        }

        #[cfg(feature = "std")]
        impl<'a> Sum<&'a Self> for $matn {
            fn sum<I>(iter: I) -> Self
            where
                I: Iterator<Item = &'a Self>,
            {
                iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
            }
        }

        #[cfg(feature = "std")]
        impl<'a> Product<&'a Self> for $matn {
            fn product<I>(iter: I) -> Self
            where
                I: Iterator<Item = &'a Self>,
            {
                iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
            }
        }
    };
}