Fri, 28 Apr 2023 09:03:21 +0300
Implement std::iter::Sum for Loc<F, N>
0 | 1 | /*! |
5 | 2 | Array containers that support vector space operations on floats. |
3 | For working with small vectors in $ℝ^2$ or $ℝ^3$. | |
0 | 4 | */ |
5 | ||
6 | use std::ops::{Add,Sub,AddAssign,SubAssign,Mul,Div,MulAssign,DivAssign,Neg,Index,IndexMut}; | |
7 | use std::slice::{Iter,IterMut}; | |
8 | use crate::types::{Float,Num,SignedNum}; | |
9 | use crate::maputil::{FixedLength,FixedLengthMut,map1,map2,map1_mut,map2_mut}; | |
5 | 10 | use crate::euclidean::*; |
0 | 11 | use crate::norms::*; |
12 | use crate::linops::AXPY; | |
13 | use serde::ser::{Serialize, Serializer, SerializeSeq}; | |
14 | ||
5 | 15 | /// A container type for (short) `N`-dimensional vectors of element type `F`. |
16 | /// | |
17 | /// Supports basic operations of an [`Euclidean`] space, several [`Norm`]s, and | |
18 | /// fused [`AXPY`] operations, among others. | |
0 | 19 | #[derive(Copy,Clone,Debug,PartialEq,Eq)] |
5 | 20 | pub struct Loc<F, const N : usize>( |
21 | /// An array of the elements of the vector | |
22 | pub [F; N] | |
23 | ); | |
0 | 24 | |
25 | // Need to manually implement as [F; N] serialisation is provided only for some N. | |
26 | impl<F, const N : usize> Serialize for Loc<F, N> | |
27 | where | |
28 | F: Serialize, | |
29 | { | |
30 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | |
31 | where | |
32 | S: Serializer, | |
33 | { | |
34 | let mut seq = serializer.serialize_seq(Some(N))?; | |
35 | for e in self.iter() { | |
36 | seq.serialize_element(e)?; | |
37 | } | |
38 | seq.end() | |
39 | } | |
40 | } | |
41 | ||
42 | impl<F, const N : usize> Loc<F, N> { | |
5 | 43 | /// Creates a new `Loc` vector from an array. |
0 | 44 | #[inline] |
45 | pub fn new(arr : [F; N]) -> Self { | |
46 | Loc(arr) | |
47 | } | |
5 | 48 | |
49 | /// Returns an iterator over the elements of the vector | |
0 | 50 | #[inline] |
51 | pub fn iter(&self) -> Iter<'_, F> { | |
52 | self.0.iter() | |
53 | } | |
54 | ||
5 | 55 | /// Returns an iterator over mutable references to the elements of the vector |
0 | 56 | #[inline] |
57 | pub fn iter_mut(&mut self) -> IterMut<'_, F> { | |
58 | self.0.iter_mut() | |
59 | } | |
60 | } | |
61 | ||
62 | impl<F : Copy, const N : usize> Loc<F, N> { | |
5 | 63 | /// Maps `g` over the elements of the vector, returning a new [`Loc`] vector |
0 | 64 | #[inline] |
5 | 65 | pub fn map<H>(&self, g : impl Fn(F) -> H) -> Loc<H, N> { |
0 | 66 | Loc::new(map1(self, |u| g(*u))) |
67 | } | |
68 | ||
5 | 69 | /// Maps `g` over pairs of elements of two vectors, retuning a new one. |
0 | 70 | #[inline] |
5 | 71 | pub fn map2<H>(&self, other : &Self, g : impl Fn(F, F) -> H) -> Loc<H, N> { |
0 | 72 | Loc::new(map2(self, other, |u, v| g(*u, *v))) |
73 | } | |
74 | ||
5 | 75 | /// Maps `g` over mutable references to elements of the vector. |
0 | 76 | #[inline] |
5 | 77 | pub fn map_mut(&mut self, g : impl Fn(&mut F)) { |
0 | 78 | map1_mut(self, g) |
79 | } | |
80 | ||
5 | 81 | /// Maps `g` over pairs of mutable references to elements of `self, and elements |
82 | /// of `other` vector. | |
0 | 83 | #[inline] |
5 | 84 | pub fn map2_mut(&mut self, other : &Self, g : impl Fn(&mut F, F)) { |
0 | 85 | map2_mut(self, other, |u, v| g(u, *v)) |
86 | } | |
87 | ||
5 | 88 | /// Maps `g` over the elements of `self` and returns the product of the results. |
0 | 89 | #[inline] |
5 | 90 | pub fn product_map<A : Num>(&self, g : impl Fn(F) -> A) -> A { |
0 | 91 | match N { |
5 | 92 | 1 => g(unsafe { *self.0.get_unchecked(0) }), |
93 | 2 => g(unsafe { *self.0.get_unchecked(0) }) * | |
94 | g(unsafe { *self.0.get_unchecked(1) }), | |
95 | 3 => g(unsafe { *self.0.get_unchecked(0) }) * | |
96 | g(unsafe { *self.0.get_unchecked(1) }) * | |
97 | g(unsafe { *self.0.get_unchecked(2) }), | |
98 | _ => self.iter().fold(A::ONE, |m, &x| m * g(x)) | |
0 | 99 | } |
100 | } | |
101 | } | |
102 | ||
5 | 103 | /// Construct a [`Loc`]. |
104 | /// | |
105 | /// Use as | |
106 | /// ``` | |
107 | /// # use alg_tools::loc::Loc; | |
108 | /// # use alg_tools::loc; | |
109 | /// let x = loc![1.0, 2.0]; | |
110 | /// ``` | |
0 | 111 | #[macro_export] |
112 | macro_rules! loc { | |
113 | ($($x:expr),+ $(,)?) => { Loc::new([$($x),+]) } | |
114 | } | |
115 | ||
116 | ||
117 | impl<F, const N : usize> From<[F; N]> for Loc<F, N> { | |
118 | #[inline] | |
119 | fn from(other: [F; N]) -> Loc<F, N> { | |
120 | Loc(other) | |
121 | } | |
122 | } | |
123 | ||
124 | /*impl<F : Copy, const N : usize> From<&[F; N]> for Loc<F, N> { | |
125 | #[inline] | |
126 | fn from(other: &[F; N]) -> Loc<F, N> { | |
127 | Loc(*other) | |
128 | } | |
129 | }*/ | |
130 | ||
131 | impl<F> From<F> for Loc<F, 1> { | |
132 | #[inline] | |
133 | fn from(other: F) -> Loc<F, 1> { | |
134 | Loc([other]) | |
135 | } | |
136 | } | |
137 | ||
138 | impl<F, const N : usize> From<Loc<F, N>> for [F; N] { | |
139 | #[inline] | |
140 | fn from(other : Loc<F, N>) -> [F; N] { | |
141 | other.0 | |
142 | } | |
143 | } | |
144 | ||
145 | /*impl<F : Copy, const N : usize> From<&Loc<F, N>> for [F; N] { | |
146 | #[inline] | |
147 | fn from(other : &Loc<F, N>) -> [F; N] { | |
148 | other.0 | |
149 | } | |
150 | }*/ | |
151 | ||
152 | ||
153 | impl<F, const N : usize> IntoIterator for Loc<F, N> { | |
154 | type Item = <[F; N] as IntoIterator>::Item; | |
155 | type IntoIter = <[F; N] as IntoIterator>::IntoIter; | |
156 | ||
157 | #[inline] | |
158 | fn into_iter(self) -> Self::IntoIter { | |
159 | self.0.into_iter() | |
160 | } | |
161 | } | |
162 | ||
163 | // Indexing | |
164 | ||
165 | impl<F, Ix, const N : usize> Index<Ix> for Loc<F,N> | |
166 | where [F; N] : Index<Ix> { | |
167 | type Output = <[F; N] as Index<Ix>>::Output; | |
168 | ||
169 | #[inline] | |
170 | fn index(&self, ix : Ix) -> &Self::Output { | |
171 | self.0.index(ix) | |
172 | } | |
173 | } | |
174 | ||
175 | impl<F, Ix, const N : usize> IndexMut<Ix> for Loc<F,N> | |
176 | where [F; N] : IndexMut<Ix> { | |
177 | #[inline] | |
178 | fn index_mut(&mut self, ix : Ix) -> &mut Self::Output { | |
179 | self.0.index_mut(ix) | |
180 | } | |
181 | } | |
182 | ||
183 | // Arithmetic | |
184 | ||
185 | macro_rules! make_binop { | |
186 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
187 | impl<F : Num, const N : usize> $trait<Loc<F,N>> for Loc<F, N> { | |
188 | type Output = Loc<F, N>; | |
189 | #[inline] | |
190 | fn $fn(mut self, other : Loc<F, N>) -> Self::Output { | |
191 | self.$fn_assign(other); | |
192 | self | |
193 | } | |
194 | } | |
195 | ||
196 | impl<'a, F : Num, const N : usize> $trait<&'a Loc<F,N>> for Loc<F, N> { | |
197 | type Output = Loc<F, N>; | |
198 | #[inline] | |
199 | fn $fn(mut self, other : &'a Loc<F, N>) -> Self::Output { | |
200 | self.$fn_assign(other); | |
201 | self | |
202 | } | |
203 | } | |
204 | ||
205 | impl<'b, F : Num, const N : usize> $trait<Loc<F,N>> for &'b Loc<F, N> { | |
206 | type Output = Loc<F, N>; | |
207 | #[inline] | |
208 | fn $fn(self, other : Loc<F, N>) -> Self::Output { | |
209 | self.map2(&other, |a, b| a.$fn(b)) | |
210 | } | |
211 | } | |
212 | ||
213 | impl<'a, 'b, F : Num, const N : usize> $trait<&'a Loc<F,N>> for &'b Loc<F, N> { | |
214 | type Output = Loc<F, N>; | |
215 | #[inline] | |
216 | fn $fn(self, other : &'a Loc<F, N>) -> Self::Output { | |
217 | self.map2(other, |a, b| a.$fn(b)) | |
218 | } | |
219 | } | |
220 | ||
221 | impl<F : Num, const N : usize> $trait_assign<Loc<F,N>> for Loc<F, N> { | |
222 | #[inline] | |
223 | fn $fn_assign(&mut self, other : Loc<F, N>) { | |
224 | self.map2_mut(&other, |a, b| a.$fn_assign(b)) | |
225 | } | |
226 | } | |
227 | ||
228 | impl<'a, F : Num, const N : usize> $trait_assign<&'a Loc<F,N>> for Loc<F, N> { | |
229 | #[inline] | |
230 | fn $fn_assign(&mut self, other : &'a Loc<F, N>) { | |
231 | self.map2_mut(other, |a, b| a.$fn_assign(b)) | |
232 | } | |
233 | } | |
234 | } | |
235 | } | |
236 | ||
237 | make_binop!(Add, add, AddAssign, add_assign); | |
238 | make_binop!(Sub, sub, SubAssign, sub_assign); | |
239 | ||
28
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
240 | impl<F : Float, const N : usize> std::iter::Sum for Loc<F, N> { |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
241 | fn sum<I: Iterator<Item = Loc<F, N>>>(mut iter: I) -> Self { |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
242 | match iter.next() { |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
243 | None => Self::ORIGIN, |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
244 | Some(mut v) => { |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
245 | for w in iter { |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
246 | v += w |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
247 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
248 | v |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
249 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
250 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
251 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
252 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
253 | |
0 | 254 | macro_rules! make_scalarop_rhs { |
255 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
256 | impl<F : Num, const N : usize> $trait<F> for Loc<F, N> { | |
257 | type Output = Loc<F, N>; | |
258 | #[inline] | |
259 | fn $fn(self, b : F) -> Self::Output { | |
260 | self.map(|a| a.$fn(b)) | |
261 | } | |
262 | } | |
263 | ||
264 | impl<'a, F : Num, const N : usize> $trait<&'a F> for Loc<F, N> { | |
265 | type Output = Loc<F, N>; | |
266 | #[inline] | |
267 | fn $fn(self, b : &'a F) -> Self::Output { | |
268 | self.map(|a| a.$fn(*b)) | |
269 | } | |
270 | } | |
271 | ||
272 | impl<'b, F : Num, const N : usize> $trait<F> for &'b Loc<F, N> { | |
273 | type Output = Loc<F, N>; | |
274 | #[inline] | |
275 | fn $fn(self, b : F) -> Self::Output { | |
276 | self.map(|a| a.$fn(b)) | |
277 | } | |
278 | } | |
279 | ||
280 | impl<'a, 'b, F : Float, const N : usize> $trait<&'a F> for &'b Loc<F, N> { | |
281 | type Output = Loc<F, N>; | |
282 | #[inline] | |
283 | fn $fn(self, b : &'a F) -> Self::Output { | |
284 | self.map(|a| a.$fn(*b)) | |
285 | } | |
286 | } | |
287 | ||
288 | impl<F : Num, const N : usize> $trait_assign<F> for Loc<F, N> { | |
289 | #[inline] | |
290 | fn $fn_assign(&mut self, b : F) { | |
291 | self.map_mut(|a| a.$fn_assign(b)); | |
292 | } | |
293 | } | |
294 | ||
295 | impl<'a, F : Num, const N : usize> $trait_assign<&'a F> for Loc<F, N> { | |
296 | #[inline] | |
297 | fn $fn_assign(&mut self, b : &'a F) { | |
298 | self.map_mut(|a| a.$fn_assign(*b)); | |
299 | } | |
300 | } | |
301 | } | |
302 | } | |
303 | ||
304 | ||
305 | make_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
306 | make_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
307 | ||
308 | macro_rules! make_unaryop { | |
309 | ($trait:ident, $fn:ident) => { | |
310 | impl<F : SignedNum, const N : usize> $trait for Loc<F, N> { | |
311 | type Output = Loc<F, N>; | |
312 | #[inline] | |
313 | fn $fn(mut self) -> Self::Output { | |
314 | self.map_mut(|a| *a = (*a).$fn()); | |
315 | self | |
316 | } | |
317 | } | |
318 | ||
319 | impl<'a, F : SignedNum, const N : usize> $trait for &'a Loc<F, N> { | |
320 | type Output = Loc<F, N>; | |
321 | #[inline] | |
322 | fn $fn(self) -> Self::Output { | |
323 | self.map(|a| a.$fn()) | |
324 | } | |
325 | } | |
326 | } | |
327 | } | |
328 | ||
329 | make_unaryop!(Neg, neg); | |
330 | ||
331 | macro_rules! make_scalarop_lhs { | |
332 | ($trait:ident, $fn:ident; $($f:ident)+) => { $( | |
333 | impl<const N : usize> $trait<Loc<$f,N>> for $f { | |
334 | type Output = Loc<$f, N>; | |
335 | #[inline] | |
336 | fn $fn(self, v : Loc<$f,N>) -> Self::Output { | |
337 | v.map(|b| self.$fn(b)) | |
338 | } | |
339 | } | |
340 | ||
341 | impl<'a, const N : usize> $trait<&'a Loc<$f,N>> for $f { | |
342 | type Output = Loc<$f, N>; | |
343 | #[inline] | |
344 | fn $fn(self, v : &'a Loc<$f,N>) -> Self::Output { | |
345 | v.map(|b| self.$fn(b)) | |
346 | } | |
347 | } | |
348 | ||
349 | impl<'b, const N : usize> $trait<Loc<$f,N>> for &'b $f { | |
350 | type Output = Loc<$f, N>; | |
351 | #[inline] | |
352 | fn $fn(self, v : Loc<$f,N>) -> Self::Output { | |
353 | v.map(|b| self.$fn(b)) | |
354 | } | |
355 | } | |
356 | ||
357 | impl<'a, 'b, const N : usize> $trait<&'a Loc<$f,N>> for &'b $f { | |
358 | type Output = Loc<$f, N>; | |
359 | #[inline] | |
360 | fn $fn(self, v : &'a Loc<$f, N>) -> Self::Output { | |
361 | v.map(|b| self.$fn(b)) | |
362 | } | |
363 | } | |
364 | )+ } | |
365 | } | |
366 | ||
367 | make_scalarop_lhs!(Mul, mul; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); | |
368 | make_scalarop_lhs!(Div, div; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); | |
369 | ||
370 | // Norms | |
371 | ||
372 | macro_rules! domination { | |
373 | ($norm:ident, $dominates:ident) => { | |
374 | impl<F : Float, const N : usize> Dominated<F, $dominates, Loc<F, N>> for $norm { | |
375 | #[inline] | |
376 | fn norm_factor(&self, _p : $dominates) -> F { | |
377 | F::ONE | |
378 | } | |
379 | #[inline] | |
380 | fn from_norm(&self, p_norm : F, _p : $dominates) -> F { | |
381 | p_norm | |
382 | } | |
383 | } | |
384 | }; | |
385 | ($norm:ident, $dominates:ident, $fn:path) => { | |
386 | impl<F : Float, const N : usize> Dominated<F, $dominates, Loc<F, N>> for $norm { | |
387 | #[inline] | |
388 | fn norm_factor(&self, _p : $dominates) -> F { | |
389 | $fn(F::cast_from(N)) | |
390 | } | |
391 | } | |
392 | }; | |
393 | } | |
394 | ||
395 | domination!(L1, L1); | |
396 | domination!(L2, L2); | |
397 | domination!(Linfinity, Linfinity); | |
398 | ||
399 | domination!(L1, L2, F::sqrt); | |
400 | domination!(L2, Linfinity, F::sqrt); | |
401 | domination!(L1, Linfinity, std::convert::identity); | |
402 | ||
403 | domination!(Linfinity, L1); | |
404 | domination!(Linfinity, L2); | |
405 | domination!(L2, L1); | |
406 | ||
407 | impl<F : Num,const N : usize> Dot<Loc<F, N>,F> for Loc<F, N> { | |
408 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
409 | /// Use [`nalgebra`] for larger vectors. | |
410 | #[inline] | |
411 | fn dot(&self, other : &Loc<F, N>) -> F { | |
412 | self.0.iter() | |
413 | .zip(other.0.iter()) | |
414 | .fold(F::ZERO, |m, (&v, &w)| m + v * w) | |
415 | } | |
416 | } | |
417 | ||
418 | impl<F : Float,const N : usize> Euclidean<F> for Loc<F, N> { | |
419 | type Output = Self; | |
420 | ||
421 | #[inline] | |
422 | fn similar_origin(&self) -> Self { | |
423 | Self::ORIGIN | |
424 | } | |
425 | ||
426 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
427 | /// Use [`nalgebra`] for larger vectors. | |
428 | #[inline] | |
429 | fn norm2_squared(&self) -> F { | |
430 | self.iter().fold(F::ZERO, |m, &v| m + v * v) | |
431 | } | |
432 | ||
433 | fn dist2_squared(&self, other : &Self) -> F { | |
434 | self.iter() | |
435 | .zip(other.iter()) | |
436 | .fold(F::ZERO, |m, (&v, &w)| { let d = v - w; m + d * d }) | |
437 | } | |
438 | ||
439 | #[inline] | |
440 | fn norm2(&self) -> F { | |
441 | // Optimisation for N==1 that avoids squaring and square rooting. | |
442 | if N==1 { | |
443 | unsafe { self.0.get_unchecked(0) }.abs() | |
444 | } else { | |
445 | self.norm2_squared().sqrt() | |
446 | } | |
447 | } | |
448 | ||
449 | #[inline] | |
450 | fn dist2(&self, other : &Self) -> F { | |
451 | // Optimisation for N==1 that avoids squaring and square rooting. | |
452 | if N==1 { | |
453 | unsafe { *self.0.get_unchecked(0) - *other.0.get_unchecked(0) }.abs() | |
454 | } else { | |
455 | self.dist2_squared(other).sqrt() | |
456 | } | |
457 | } | |
458 | } | |
459 | ||
460 | impl<F : Num, const N : usize> Loc<F, N> { | |
461 | pub const ORIGIN : Self = Loc([F::ZERO; N]); | |
462 | } | |
463 | ||
464 | impl<F : Float,const N : usize> StaticEuclidean<F> for Loc<F, N> { | |
465 | #[inline] | |
466 | fn origin() -> Self { | |
467 | Self::ORIGIN | |
468 | } | |
469 | } | |
470 | ||
471 | impl<F : Float, const N : usize> Norm<F, L2> for Loc<F, N> { | |
472 | #[inline] | |
473 | fn norm(&self, _ : L2) -> F { self.norm2() } | |
474 | } | |
475 | ||
476 | impl<F : Float, const N : usize> Dist<F, L2> for Loc<F, N> { | |
477 | #[inline] | |
478 | fn dist(&self, other : &Self, _ : L2) -> F { self.dist2(other) } | |
479 | } | |
480 | ||
481 | impl<F : Float, const N : usize> Norm<F, L1> for Loc<F, N> { | |
482 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
483 | /// Use [`nalgebra`] for larger vectors. | |
484 | #[inline] | |
485 | fn norm(&self, _ : L1) -> F { | |
486 | self.iter().fold(F::ZERO, |m, v| m + v.abs()) | |
487 | } | |
488 | } | |
489 | ||
490 | impl<F : Float, const N : usize> Dist<F, L1> for Loc<F, N> { | |
491 | #[inline] | |
492 | fn dist(&self, other : &Self, _ : L1) -> F { | |
493 | self.iter() | |
494 | .zip(other.iter()) | |
495 | .fold(F::ZERO, |m, (&v, &w)| m + (v-w).abs() ) | |
496 | } | |
497 | } | |
498 | ||
499 | impl<F : Float, const N : usize> Projection<F, Linfinity> for Loc<F, N> { | |
500 | #[inline] | |
501 | fn proj_ball_mut(&mut self, ρ : F, _ : Linfinity) { | |
502 | self.iter_mut().for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ)) | |
503 | } | |
504 | } | |
505 | ||
506 | impl<F : Float, const N : usize> Norm<F, Linfinity> for Loc<F, N> { | |
507 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
508 | /// Use [`nalgebra`] for larger vectors. | |
509 | #[inline] | |
510 | fn norm(&self, _ : Linfinity) -> F { | |
511 | self.iter().fold(F::ZERO, |m, v| m.max(v.abs())) | |
512 | } | |
513 | } | |
514 | ||
515 | impl<F : Float, const N : usize> Dist<F, Linfinity> for Loc<F, N> { | |
516 | #[inline] | |
517 | fn dist(&self, other : &Self, _ : Linfinity) -> F { | |
518 | self.iter() | |
519 | .zip(other.iter()) | |
520 | .fold(F::ZERO, |m, (&v, &w)| m.max((v-w).abs())) | |
521 | } | |
522 | } | |
523 | ||
524 | ||
525 | // Misc. | |
526 | ||
527 | impl<A, const N : usize> FixedLength<N> for Loc<A,N> { | |
528 | type Iter = std::array::IntoIter<A, N>; | |
529 | type Elem = A; | |
530 | #[inline] | |
531 | fn fl_iter(self) -> Self::Iter { | |
532 | self.into_iter() | |
533 | } | |
534 | } | |
535 | ||
536 | impl<A, const N : usize> FixedLengthMut<N> for Loc<A,N> { | |
537 | type IterMut<'a> = std::slice::IterMut<'a, A> where A : 'a; | |
538 | #[inline] | |
539 | fn fl_iter_mut(&mut self) -> Self::IterMut<'_> { | |
540 | self.iter_mut() | |
541 | } | |
542 | } | |
543 | ||
544 | impl<'a, A, const N : usize> FixedLength<N> for &'a Loc<A,N> { | |
545 | type Iter = std::slice::Iter<'a, A>; | |
546 | type Elem = &'a A; | |
547 | #[inline] | |
548 | fn fl_iter(self) -> Self::Iter { | |
549 | self.iter() | |
550 | } | |
551 | } | |
552 | ||
553 | impl<F : Num, const N : usize> AXPY<F, Loc<F, N>> for Loc<F, N> { | |
554 | ||
555 | #[inline] | |
556 | fn axpy(&mut self, α : F, x : &Loc<F, N>, β : F) { | |
557 | if β == F::ZERO { | |
558 | map2_mut(self, x, |yi, xi| { *yi = α * (*xi) }) | |
559 | } else { | |
560 | map2_mut(self, x, |yi, xi| { *yi = β * (*yi) + α * (*xi) }) | |
561 | } | |
562 | } | |
563 | ||
564 | #[inline] | |
565 | fn copy_from(&mut self, x : &Loc<F, N>) { | |
566 | map2_mut(self, x, |yi, xi| *yi = *xi ) | |
567 | } | |
568 | } |