Wed, 04 Oct 2023 08:59:29 -0500
Add Mapping codomain slicing and RealVectorField
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 | ||
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
138 | impl<F> Loc<F, 1> { |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
139 | #[inline] |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
140 | pub fn flatten1d(self) -> F { |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
141 | let Loc([v]) = self; |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
142 | v |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
143 | } |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
144 | } |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
145 | |
0 | 146 | impl<F, const N : usize> From<Loc<F, N>> for [F; N] { |
147 | #[inline] | |
148 | fn from(other : Loc<F, N>) -> [F; N] { | |
149 | other.0 | |
150 | } | |
151 | } | |
152 | ||
153 | /*impl<F : Copy, const N : usize> From<&Loc<F, N>> for [F; N] { | |
154 | #[inline] | |
155 | fn from(other : &Loc<F, N>) -> [F; N] { | |
156 | other.0 | |
157 | } | |
158 | }*/ | |
159 | ||
160 | ||
161 | impl<F, const N : usize> IntoIterator for Loc<F, N> { | |
162 | type Item = <[F; N] as IntoIterator>::Item; | |
163 | type IntoIter = <[F; N] as IntoIterator>::IntoIter; | |
164 | ||
165 | #[inline] | |
166 | fn into_iter(self) -> Self::IntoIter { | |
167 | self.0.into_iter() | |
168 | } | |
169 | } | |
170 | ||
171 | // Indexing | |
172 | ||
173 | impl<F, Ix, const N : usize> Index<Ix> for Loc<F,N> | |
174 | where [F; N] : Index<Ix> { | |
175 | type Output = <[F; N] as Index<Ix>>::Output; | |
176 | ||
177 | #[inline] | |
178 | fn index(&self, ix : Ix) -> &Self::Output { | |
179 | self.0.index(ix) | |
180 | } | |
181 | } | |
182 | ||
183 | impl<F, Ix, const N : usize> IndexMut<Ix> for Loc<F,N> | |
184 | where [F; N] : IndexMut<Ix> { | |
185 | #[inline] | |
186 | fn index_mut(&mut self, ix : Ix) -> &mut Self::Output { | |
187 | self.0.index_mut(ix) | |
188 | } | |
189 | } | |
190 | ||
191 | // Arithmetic | |
192 | ||
193 | macro_rules! make_binop { | |
194 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
195 | impl<F : Num, const N : usize> $trait<Loc<F,N>> for Loc<F, N> { | |
196 | type Output = Loc<F, N>; | |
197 | #[inline] | |
198 | fn $fn(mut self, other : Loc<F, N>) -> Self::Output { | |
199 | self.$fn_assign(other); | |
200 | self | |
201 | } | |
202 | } | |
203 | ||
204 | impl<'a, F : Num, const N : usize> $trait<&'a Loc<F,N>> for Loc<F, N> { | |
205 | type Output = Loc<F, N>; | |
206 | #[inline] | |
207 | fn $fn(mut self, other : &'a Loc<F, N>) -> Self::Output { | |
208 | self.$fn_assign(other); | |
209 | self | |
210 | } | |
211 | } | |
212 | ||
213 | impl<'b, F : Num, const N : usize> $trait<Loc<F,N>> for &'b Loc<F, N> { | |
214 | type Output = Loc<F, N>; | |
215 | #[inline] | |
216 | fn $fn(self, other : Loc<F, N>) -> Self::Output { | |
217 | self.map2(&other, |a, b| a.$fn(b)) | |
218 | } | |
219 | } | |
220 | ||
221 | impl<'a, 'b, F : Num, const N : usize> $trait<&'a Loc<F,N>> for &'b Loc<F, N> { | |
222 | type Output = Loc<F, N>; | |
223 | #[inline] | |
224 | fn $fn(self, other : &'a Loc<F, N>) -> Self::Output { | |
225 | self.map2(other, |a, b| a.$fn(b)) | |
226 | } | |
227 | } | |
228 | ||
229 | impl<F : Num, const N : usize> $trait_assign<Loc<F,N>> for Loc<F, N> { | |
230 | #[inline] | |
231 | fn $fn_assign(&mut self, other : Loc<F, N>) { | |
232 | self.map2_mut(&other, |a, b| a.$fn_assign(b)) | |
233 | } | |
234 | } | |
235 | ||
236 | impl<'a, F : Num, const N : usize> $trait_assign<&'a Loc<F,N>> for Loc<F, N> { | |
237 | #[inline] | |
238 | fn $fn_assign(&mut self, other : &'a Loc<F, N>) { | |
239 | self.map2_mut(other, |a, b| a.$fn_assign(b)) | |
240 | } | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | make_binop!(Add, add, AddAssign, add_assign); | |
246 | make_binop!(Sub, sub, SubAssign, sub_assign); | |
247 | ||
28
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
248 | 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
|
249 | 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
|
250 | match iter.next() { |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
251 | None => Self::ORIGIN, |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
252 | Some(mut v) => { |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
253 | for w in iter { |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
254 | v += w |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
255 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
256 | v |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
257 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
258 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
259 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
260 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
261 | |
0 | 262 | macro_rules! make_scalarop_rhs { |
263 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
264 | impl<F : Num, const N : usize> $trait<F> for Loc<F, N> { | |
265 | type Output = Loc<F, N>; | |
266 | #[inline] | |
267 | fn $fn(self, b : F) -> Self::Output { | |
268 | self.map(|a| a.$fn(b)) | |
269 | } | |
270 | } | |
271 | ||
272 | impl<'a, F : Num, const N : usize> $trait<&'a F> for Loc<F, N> { | |
273 | type Output = Loc<F, N>; | |
274 | #[inline] | |
275 | fn $fn(self, b : &'a F) -> Self::Output { | |
276 | self.map(|a| a.$fn(*b)) | |
277 | } | |
278 | } | |
279 | ||
280 | impl<'b, F : Num, const N : usize> $trait<F> for &'b Loc<F, N> { | |
281 | type Output = Loc<F, N>; | |
282 | #[inline] | |
283 | fn $fn(self, b : F) -> Self::Output { | |
284 | self.map(|a| a.$fn(b)) | |
285 | } | |
286 | } | |
287 | ||
288 | impl<'a, 'b, F : Float, const N : usize> $trait<&'a F> for &'b Loc<F, N> { | |
289 | type Output = Loc<F, N>; | |
290 | #[inline] | |
291 | fn $fn(self, b : &'a F) -> Self::Output { | |
292 | self.map(|a| a.$fn(*b)) | |
293 | } | |
294 | } | |
295 | ||
296 | impl<F : Num, const N : usize> $trait_assign<F> for Loc<F, N> { | |
297 | #[inline] | |
298 | fn $fn_assign(&mut self, b : F) { | |
299 | self.map_mut(|a| a.$fn_assign(b)); | |
300 | } | |
301 | } | |
302 | ||
303 | impl<'a, F : Num, const N : usize> $trait_assign<&'a F> for Loc<F, N> { | |
304 | #[inline] | |
305 | fn $fn_assign(&mut self, b : &'a F) { | |
306 | self.map_mut(|a| a.$fn_assign(*b)); | |
307 | } | |
308 | } | |
309 | } | |
310 | } | |
311 | ||
312 | ||
313 | make_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
314 | make_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
315 | ||
316 | macro_rules! make_unaryop { | |
317 | ($trait:ident, $fn:ident) => { | |
318 | impl<F : SignedNum, const N : usize> $trait for Loc<F, N> { | |
319 | type Output = Loc<F, N>; | |
320 | #[inline] | |
321 | fn $fn(mut self) -> Self::Output { | |
322 | self.map_mut(|a| *a = (*a).$fn()); | |
323 | self | |
324 | } | |
325 | } | |
326 | ||
327 | impl<'a, F : SignedNum, const N : usize> $trait for &'a Loc<F, N> { | |
328 | type Output = Loc<F, N>; | |
329 | #[inline] | |
330 | fn $fn(self) -> Self::Output { | |
331 | self.map(|a| a.$fn()) | |
332 | } | |
333 | } | |
334 | } | |
335 | } | |
336 | ||
337 | make_unaryop!(Neg, neg); | |
338 | ||
339 | macro_rules! make_scalarop_lhs { | |
340 | ($trait:ident, $fn:ident; $($f:ident)+) => { $( | |
341 | impl<const N : usize> $trait<Loc<$f,N>> for $f { | |
342 | type Output = Loc<$f, N>; | |
343 | #[inline] | |
344 | fn $fn(self, v : Loc<$f,N>) -> Self::Output { | |
345 | v.map(|b| self.$fn(b)) | |
346 | } | |
347 | } | |
348 | ||
349 | impl<'a, const N : usize> $trait<&'a Loc<$f,N>> for $f { | |
350 | type Output = Loc<$f, N>; | |
351 | #[inline] | |
352 | fn $fn(self, v : &'a Loc<$f,N>) -> Self::Output { | |
353 | v.map(|b| self.$fn(b)) | |
354 | } | |
355 | } | |
356 | ||
357 | impl<'b, const N : usize> $trait<Loc<$f,N>> for &'b $f { | |
358 | type Output = Loc<$f, N>; | |
359 | #[inline] | |
360 | fn $fn(self, v : Loc<$f,N>) -> Self::Output { | |
361 | v.map(|b| self.$fn(b)) | |
362 | } | |
363 | } | |
364 | ||
365 | impl<'a, 'b, const N : usize> $trait<&'a Loc<$f,N>> for &'b $f { | |
366 | type Output = Loc<$f, N>; | |
367 | #[inline] | |
368 | fn $fn(self, v : &'a Loc<$f, N>) -> Self::Output { | |
369 | v.map(|b| self.$fn(b)) | |
370 | } | |
371 | } | |
372 | )+ } | |
373 | } | |
374 | ||
375 | make_scalarop_lhs!(Mul, mul; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); | |
376 | make_scalarop_lhs!(Div, div; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); | |
377 | ||
378 | // Norms | |
379 | ||
380 | macro_rules! domination { | |
381 | ($norm:ident, $dominates:ident) => { | |
382 | impl<F : Float, const N : usize> Dominated<F, $dominates, Loc<F, N>> for $norm { | |
383 | #[inline] | |
384 | fn norm_factor(&self, _p : $dominates) -> F { | |
385 | F::ONE | |
386 | } | |
387 | #[inline] | |
388 | fn from_norm(&self, p_norm : F, _p : $dominates) -> F { | |
389 | p_norm | |
390 | } | |
391 | } | |
392 | }; | |
393 | ($norm:ident, $dominates:ident, $fn:path) => { | |
394 | impl<F : Float, const N : usize> Dominated<F, $dominates, Loc<F, N>> for $norm { | |
395 | #[inline] | |
396 | fn norm_factor(&self, _p : $dominates) -> F { | |
397 | $fn(F::cast_from(N)) | |
398 | } | |
399 | } | |
400 | }; | |
401 | } | |
402 | ||
403 | domination!(L1, L1); | |
404 | domination!(L2, L2); | |
405 | domination!(Linfinity, Linfinity); | |
406 | ||
407 | domination!(L1, L2, F::sqrt); | |
408 | domination!(L2, Linfinity, F::sqrt); | |
409 | domination!(L1, Linfinity, std::convert::identity); | |
410 | ||
411 | domination!(Linfinity, L1); | |
412 | domination!(Linfinity, L2); | |
413 | domination!(L2, L1); | |
414 | ||
415 | impl<F : Num,const N : usize> Dot<Loc<F, N>,F> for Loc<F, N> { | |
416 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
417 | /// Use [`nalgebra`] for larger vectors. | |
418 | #[inline] | |
419 | fn dot(&self, other : &Loc<F, N>) -> F { | |
420 | self.0.iter() | |
421 | .zip(other.0.iter()) | |
422 | .fold(F::ZERO, |m, (&v, &w)| m + v * w) | |
423 | } | |
424 | } | |
425 | ||
426 | impl<F : Float,const N : usize> Euclidean<F> for Loc<F, N> { | |
427 | type Output = Self; | |
428 | ||
429 | #[inline] | |
430 | fn similar_origin(&self) -> Self { | |
431 | Self::ORIGIN | |
432 | } | |
433 | ||
434 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
435 | /// Use [`nalgebra`] for larger vectors. | |
436 | #[inline] | |
437 | fn norm2_squared(&self) -> F { | |
438 | self.iter().fold(F::ZERO, |m, &v| m + v * v) | |
439 | } | |
440 | ||
441 | fn dist2_squared(&self, other : &Self) -> F { | |
442 | self.iter() | |
443 | .zip(other.iter()) | |
444 | .fold(F::ZERO, |m, (&v, &w)| { let d = v - w; m + d * d }) | |
445 | } | |
446 | ||
447 | #[inline] | |
448 | fn norm2(&self) -> F { | |
449 | // Optimisation for N==1 that avoids squaring and square rooting. | |
450 | if N==1 { | |
451 | unsafe { self.0.get_unchecked(0) }.abs() | |
452 | } else { | |
453 | self.norm2_squared().sqrt() | |
454 | } | |
455 | } | |
456 | ||
457 | #[inline] | |
458 | fn dist2(&self, other : &Self) -> F { | |
459 | // Optimisation for N==1 that avoids squaring and square rooting. | |
460 | if N==1 { | |
461 | unsafe { *self.0.get_unchecked(0) - *other.0.get_unchecked(0) }.abs() | |
462 | } else { | |
463 | self.dist2_squared(other).sqrt() | |
464 | } | |
465 | } | |
466 | } | |
467 | ||
468 | impl<F : Num, const N : usize> Loc<F, N> { | |
469 | pub const ORIGIN : Self = Loc([F::ZERO; N]); | |
470 | } | |
471 | ||
472 | impl<F : Float,const N : usize> StaticEuclidean<F> for Loc<F, N> { | |
473 | #[inline] | |
474 | fn origin() -> Self { | |
475 | Self::ORIGIN | |
476 | } | |
477 | } | |
478 | ||
479 | impl<F : Float, const N : usize> Norm<F, L2> for Loc<F, N> { | |
480 | #[inline] | |
481 | fn norm(&self, _ : L2) -> F { self.norm2() } | |
482 | } | |
483 | ||
484 | impl<F : Float, const N : usize> Dist<F, L2> for Loc<F, N> { | |
485 | #[inline] | |
486 | fn dist(&self, other : &Self, _ : L2) -> F { self.dist2(other) } | |
487 | } | |
488 | ||
489 | impl<F : Float, const N : usize> Norm<F, L1> for Loc<F, N> { | |
490 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
491 | /// Use [`nalgebra`] for larger vectors. | |
492 | #[inline] | |
493 | fn norm(&self, _ : L1) -> F { | |
494 | self.iter().fold(F::ZERO, |m, v| m + v.abs()) | |
495 | } | |
496 | } | |
497 | ||
498 | impl<F : Float, const N : usize> Dist<F, L1> for Loc<F, N> { | |
499 | #[inline] | |
500 | fn dist(&self, other : &Self, _ : L1) -> F { | |
501 | self.iter() | |
502 | .zip(other.iter()) | |
503 | .fold(F::ZERO, |m, (&v, &w)| m + (v-w).abs() ) | |
504 | } | |
505 | } | |
506 | ||
507 | impl<F : Float, const N : usize> Projection<F, Linfinity> for Loc<F, N> { | |
508 | #[inline] | |
509 | fn proj_ball_mut(&mut self, ρ : F, _ : Linfinity) { | |
510 | self.iter_mut().for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ)) | |
511 | } | |
512 | } | |
513 | ||
514 | impl<F : Float, const N : usize> Norm<F, Linfinity> for Loc<F, N> { | |
515 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
516 | /// Use [`nalgebra`] for larger vectors. | |
517 | #[inline] | |
518 | fn norm(&self, _ : Linfinity) -> F { | |
519 | self.iter().fold(F::ZERO, |m, v| m.max(v.abs())) | |
520 | } | |
521 | } | |
522 | ||
523 | impl<F : Float, const N : usize> Dist<F, Linfinity> for Loc<F, N> { | |
524 | #[inline] | |
525 | fn dist(&self, other : &Self, _ : Linfinity) -> F { | |
526 | self.iter() | |
527 | .zip(other.iter()) | |
528 | .fold(F::ZERO, |m, (&v, &w)| m.max((v-w).abs())) | |
529 | } | |
530 | } | |
531 | ||
532 | ||
533 | // Misc. | |
534 | ||
535 | impl<A, const N : usize> FixedLength<N> for Loc<A,N> { | |
536 | type Iter = std::array::IntoIter<A, N>; | |
537 | type Elem = A; | |
538 | #[inline] | |
539 | fn fl_iter(self) -> Self::Iter { | |
540 | self.into_iter() | |
541 | } | |
542 | } | |
543 | ||
544 | impl<A, const N : usize> FixedLengthMut<N> for Loc<A,N> { | |
545 | type IterMut<'a> = std::slice::IterMut<'a, A> where A : 'a; | |
546 | #[inline] | |
547 | fn fl_iter_mut(&mut self) -> Self::IterMut<'_> { | |
548 | self.iter_mut() | |
549 | } | |
550 | } | |
551 | ||
552 | impl<'a, A, const N : usize> FixedLength<N> for &'a Loc<A,N> { | |
553 | type Iter = std::slice::Iter<'a, A>; | |
554 | type Elem = &'a A; | |
555 | #[inline] | |
556 | fn fl_iter(self) -> Self::Iter { | |
557 | self.iter() | |
558 | } | |
559 | } | |
560 | ||
561 | impl<F : Num, const N : usize> AXPY<F, Loc<F, N>> for Loc<F, N> { | |
562 | ||
563 | #[inline] | |
564 | fn axpy(&mut self, α : F, x : &Loc<F, N>, β : F) { | |
565 | if β == F::ZERO { | |
566 | map2_mut(self, x, |yi, xi| { *yi = α * (*xi) }) | |
567 | } else { | |
568 | map2_mut(self, x, |yi, xi| { *yi = β * (*yi) + α * (*xi) }) | |
569 | } | |
570 | } | |
571 | ||
572 | #[inline] | |
573 | fn copy_from(&mut self, x : &Loc<F, N>) { | |
574 | map2_mut(self, x, |yi, xi| *yi = *xi ) | |
575 | } | |
576 | } |