diffsol/matrix/
utils.rs

1macro_rules! impl_matrix_common {
2    ($mat:ty, $vec:ty, $con:ty, $in:ty) => {
3        impl<T: Scalar> MatrixCommon for $mat {
4            type T = T;
5            type V = $vec;
6            type C = $con;
7            type Inner = $in;
8
9            fn nrows(&self) -> IndexType {
10                self.data.nrows()
11            }
12            fn ncols(&self) -> IndexType {
13                self.data.ncols()
14            }
15            fn inner(&self) -> &Self::Inner {
16                &self.data
17            }
18        }
19    };
20}
21
22pub(crate) use impl_matrix_common;
23
24macro_rules! impl_matrix_common_ref {
25    ($mat:ty, $vec:ty, $con:ty, $in:ty) => {
26        impl<'a, T: Scalar> MatrixCommon for $mat {
27            type T = T;
28            type V = $vec;
29            type C = $con;
30            type Inner = $in;
31
32            fn nrows(&self) -> IndexType {
33                self.data.nrows()
34            }
35            fn ncols(&self) -> IndexType {
36                self.data.ncols()
37            }
38            fn inner(&self) -> &Self::Inner {
39                &self.data
40            }
41        }
42    };
43}
44
45pub(crate) use impl_matrix_common_ref;
46
47macro_rules! impl_add {
48    ($lhs:ty, $rhs:ty, $out:ty) => {
49        impl<T: Scalar> Add<$rhs> for $lhs {
50            type Output = $out;
51
52            fn add(self, rhs: $rhs) -> Self::Output {
53                Self::Output {
54                    data: self.data + &rhs.data,
55                    context: self.context,
56                }
57            }
58        }
59    };
60}
61pub(crate) use impl_add;
62
63macro_rules! impl_sub {
64    ($lhs:ty, $rhs:ty, $out:ty) => {
65        impl<T: Scalar> Sub<$rhs> for $lhs {
66            type Output = $out;
67
68            fn sub(self, rhs: $rhs) -> Self::Output {
69                Self::Output {
70                    data: self.data - &rhs.data,
71                    context: self.context,
72                }
73            }
74        }
75    };
76}
77pub(crate) use impl_sub;
78
79macro_rules! impl_add_assign {
80    ($lhs:ty, $rhs:ty) => {
81        impl<T: Scalar> AddAssign<$rhs> for $lhs {
82            fn add_assign(&mut self, rhs: $rhs) {
83                self.data += &rhs.data;
84            }
85        }
86    };
87}
88pub(crate) use impl_add_assign;
89
90macro_rules! impl_sub_assign {
91    ($lhs:ty, $rhs:ty) => {
92        impl<T: Scalar> SubAssign<$rhs> for $lhs {
93            fn sub_assign(&mut self, rhs: $rhs) {
94                self.data -= &rhs.data;
95            }
96        }
97    };
98}
99pub(crate) use impl_sub_assign;
100
101macro_rules! impl_index {
102    ($lhs:ty) => {
103        impl<T: Scalar> Index<(IndexType, IndexType)> for $lhs {
104            type Output = T;
105            fn index(&self, index: (IndexType, IndexType)) -> &T {
106                &self.data[index]
107            }
108        }
109    };
110}
111pub(crate) use impl_index;
112
113macro_rules! impl_index_mut {
114    ($lhs:ty) => {
115        impl<T: Scalar> IndexMut<(IndexType, IndexType)> for $lhs {
116            fn index_mut(&mut self, index: (IndexType, IndexType)) -> &mut T {
117                &mut self.data[index]
118            }
119        }
120    };
121}
122pub(crate) use impl_index_mut;