full/mat/
std_ops.rs

1use crate::mat::Mat;
2use std::ops::*;
3
4impl<T> Neg for Mat<T>
5where
6    T: Neg<Output = T>,
7{
8    type Output = Self;
9
10    fn neg(self) -> Self::Output {
11        Self::Output {
12            rows: self.rows,
13            cols: self.cols,
14            // data: self.into_iter().map(|a| -a).collect(),
15            values: self.values.into_iter().map(|a| -a).collect(),
16            col_major: self.col_major,
17        }
18    }
19}
20
21// Add/Sub/Mul/Div <T> //
22
23impl<T> Add<T> for Mat<T>
24where
25    T: Add<T, Output = T> + Copy,
26{
27    type Output = Self;
28
29    fn add(self, rhs: T) -> Self::Output {
30        Self::Output {
31            rows: self.rows,
32            cols: self.cols,
33            values: self.values.into_iter().map(|a| a + rhs).collect(),
34            col_major: self.col_major,
35        }
36    }
37}
38
39impl<T> Sub<T> for Mat<T>
40where
41    T: Sub<T, Output = T> + Copy,
42{
43    type Output = Self;
44
45    fn sub(self, rhs: T) -> Self::Output {
46        Self::Output {
47            rows: self.rows,
48            cols: self.cols,
49            values: self.values.into_iter().map(|a| a - rhs).collect(),
50            col_major: self.col_major,
51        }
52    }
53}
54
55impl<T> Mul<T> for Mat<T>
56where
57    T: Mul<T, Output = T> + Copy,
58{
59    type Output = Self;
60
61    fn mul(self, rhs: T) -> Self::Output {
62        Self::Output {
63            rows: self.rows,
64            cols: self.cols,
65            values: self.values.into_iter().map(|a| a * rhs).collect(),
66            col_major: self.col_major,
67        }
68    }
69}
70
71impl<T> Div<T> for Mat<T>
72where
73    T: Div<T, Output = T> + Copy,
74{
75    type Output = Self;
76
77    fn div(self, rhs: T) -> Self::Output {
78        Self::Output {
79            rows: self.rows,
80            cols: self.cols,
81            values: self.values.into_iter().map(|a| a / rhs).collect(),
82            col_major: self.col_major,
83        }
84    }
85}
86
87// Add/Sub/Mul/Div Assign<T> //
88
89impl<T> AddAssign<T> for Mat<T>
90where
91    T: AddAssign<T> + Copy,
92{
93    fn add_assign(&mut self, rhs: T) {
94        self.values.iter_mut().for_each(|a| *a += rhs);
95    }
96}
97
98impl<T> SubAssign<T> for Mat<T>
99where
100    T: SubAssign<T> + Copy,
101{
102    fn sub_assign(&mut self, rhs: T) {
103        self.values.iter_mut().for_each(|a| *a -= rhs);
104    }
105}
106
107impl<T> MulAssign<T> for Mat<T>
108where
109    T: MulAssign<T> + Copy,
110{
111    fn mul_assign(&mut self, rhs: T) {
112        self.values.iter_mut().for_each(|a| *a *= rhs);
113    }
114}
115
116impl<T> DivAssign<T> for Mat<T>
117where
118    T: DivAssign<T> + Copy,
119{
120    fn div_assign(&mut self, rhs: T) {
121        self.values.iter_mut().for_each(|a| *a /= rhs);
122    }
123}
124
125// Add/Sub/Mul/Div Mat<T> //
126
127impl<T> Add<Mat<T>> for Mat<T>
128where
129    T: Add<T, Output = T> + Copy,
130{
131    type Output = Self;
132
133    fn add(self, rhs: Self) -> Self::Output {
134        assert_eq!(self.rows, rhs.rows);
135        assert_eq!(self.cols, rhs.cols);
136
137        Self::Output {
138            rows: self.rows,
139            cols: self.cols,
140            values: self
141                .values
142                .into_iter()
143                .zip(rhs.values.into_iter())
144                .map(|(a, b)| a + b)
145                .collect(),
146            col_major: self.col_major,
147        }
148    }
149}
150
151impl<T> Sub<Mat<T>> for Mat<T>
152where
153    T: Sub<T, Output = T> + Copy,
154{
155    type Output = Self;
156
157    fn sub(self, rhs: Self) -> Self::Output {
158        assert_eq!(self.rows, rhs.rows);
159        assert_eq!(self.cols, rhs.cols);
160
161        Self::Output {
162            rows: self.rows,
163            cols: self.cols,
164            values: self
165                .values
166                .into_iter()
167                .zip(rhs.values.into_iter())
168                .map(|(a, b)| a - b)
169                .collect(),
170            col_major: self.col_major,
171        }
172    }
173}
174
175impl<T> Mul<Mat<T>> for Mat<T>
176where
177    T: Mul<T, Output = T> + Copy,
178{
179    type Output = Self;
180
181    fn mul(self, rhs: Self) -> Self::Output {
182        assert_eq!(self.rows, rhs.rows);
183        assert_eq!(self.cols, rhs.cols);
184
185        Self::Output {
186            rows: self.rows,
187            cols: self.cols,
188            values: self
189                .values
190                .into_iter()
191                .zip(rhs.values.into_iter())
192                .map(|(a, b)| a * b)
193                .collect(),
194            col_major: self.col_major,
195        }
196    }
197}
198
199impl<T> Div<Mat<T>> for Mat<T>
200where
201    T: Div<T, Output = T> + Copy,
202{
203    type Output = Self;
204
205    fn div(self, rhs: Self) -> Self::Output {
206        assert_eq!(self.rows, rhs.rows);
207        assert_eq!(self.cols, rhs.cols);
208
209        Self::Output {
210            rows: self.rows,
211            cols: self.cols,
212            values: self
213                .values
214                .into_iter()
215                .zip(rhs.values.into_iter())
216                .map(|(a, b)| a / b)
217                .collect(),
218            col_major: self.col_major,
219        }
220    }
221}
222
223// Add/Sub/Mul/Div Assign Mat<T> //
224
225impl<T> AddAssign<Mat<T>> for Mat<T>
226where
227    T: AddAssign<T> + Copy,
228{
229    fn add_assign(&mut self, rhs: Mat<T>) {
230        assert_eq!(self.rows, rhs.rows);
231        assert_eq!(self.cols, rhs.cols);
232
233        self.values
234            .iter_mut()
235            .zip(rhs.values.into_iter())
236            .for_each(|(a, b)| *a += b);
237    }
238}
239
240impl<T> SubAssign<Mat<T>> for Mat<T>
241where
242    T: SubAssign<T> + Copy,
243{
244    fn sub_assign(&mut self, rhs: Mat<T>) {
245        assert_eq!(self.rows, rhs.rows);
246        assert_eq!(self.cols, rhs.cols);
247
248        self.values
249            .iter_mut()
250            .zip(rhs.values.into_iter())
251            .for_each(|(a, b)| *a -= b);
252    }
253}
254
255impl<T> MulAssign<Mat<T>> for Mat<T>
256where
257    T: MulAssign<T> + Copy,
258{
259    fn mul_assign(&mut self, rhs: Mat<T>) {
260        assert_eq!(self.rows, rhs.rows);
261        assert_eq!(self.cols, rhs.cols);
262
263        self.values
264            .iter_mut()
265            .zip(rhs.values.into_iter())
266            .for_each(|(a, b)| *a *= b);
267    }
268}
269
270impl<T> DivAssign<Mat<T>> for Mat<T>
271where
272    T: DivAssign<T> + Copy,
273{
274    fn div_assign(&mut self, rhs: Mat<T>) {
275        assert_eq!(self.rows, rhs.rows);
276        assert_eq!(self.cols, rhs.cols);
277
278        self.values
279            .iter_mut()
280            .zip(rhs.values.into_iter())
281            .for_each(|(a, b)| *a /= b);
282    }
283}