Tue, 31 Dec 2024 09:02:55 -0500
More convexity, normed spaces, etc.
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}; | |
43 | 8 | use std::fmt::{Display, Formatter}; |
0 | 9 | use crate::types::{Float,Num,SignedNum}; |
10 | use crate::maputil::{FixedLength,FixedLengthMut,map1,map2,map1_mut,map2_mut}; | |
5 | 11 | use crate::euclidean::*; |
0 | 12 | use crate::norms::*; |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
13 | use crate::linops::{AXPY, Mapping, Linear}; |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
14 | use crate::instance::{Instance, BasicDecomposition}; |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
15 | use crate::mapping::Space; |
0 | 16 | use serde::ser::{Serialize, Serializer, SerializeSeq}; |
17 | ||
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
18 | |
5 | 19 | /// A container type for (short) `N`-dimensional vectors of element type `F`. |
20 | /// | |
21 | /// Supports basic operations of an [`Euclidean`] space, several [`Norm`]s, and | |
22 | /// fused [`AXPY`] operations, among others. | |
0 | 23 | #[derive(Copy,Clone,Debug,PartialEq,Eq)] |
5 | 24 | pub struct Loc<F, const N : usize>( |
25 | /// An array of the elements of the vector | |
26 | pub [F; N] | |
27 | ); | |
0 | 28 | |
43 | 29 | impl<F : Display, const N : usize> Display for Loc<F, N>{ |
30 | // Required method | |
31 | fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | |
32 | write!(f, "[")?; | |
33 | let mut comma = ""; | |
34 | for e in self.iter() { | |
35 | write!(f, "{comma}{e}")?; | |
36 | comma = ", "; | |
37 | } | |
38 | write!(f, "]") | |
39 | } | |
40 | } | |
41 | ||
0 | 42 | // Need to manually implement as [F; N] serialisation is provided only for some N. |
43 | impl<F, const N : usize> Serialize for Loc<F, N> | |
44 | where | |
45 | F: Serialize, | |
46 | { | |
47 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | |
48 | where | |
49 | S: Serializer, | |
50 | { | |
51 | let mut seq = serializer.serialize_seq(Some(N))?; | |
52 | for e in self.iter() { | |
53 | seq.serialize_element(e)?; | |
54 | } | |
55 | seq.end() | |
56 | } | |
57 | } | |
58 | ||
59 | impl<F, const N : usize> Loc<F, N> { | |
5 | 60 | /// Creates a new `Loc` vector from an array. |
0 | 61 | #[inline] |
62 | pub fn new(arr : [F; N]) -> Self { | |
63 | Loc(arr) | |
64 | } | |
5 | 65 | |
66 | /// Returns an iterator over the elements of the vector | |
0 | 67 | #[inline] |
68 | pub fn iter(&self) -> Iter<'_, F> { | |
69 | self.0.iter() | |
70 | } | |
71 | ||
5 | 72 | /// Returns an iterator over mutable references to the elements of the vector |
0 | 73 | #[inline] |
74 | pub fn iter_mut(&mut self) -> IterMut<'_, F> { | |
75 | self.0.iter_mut() | |
76 | } | |
77 | } | |
78 | ||
79 | impl<F : Copy, const N : usize> Loc<F, N> { | |
5 | 80 | /// Maps `g` over the elements of the vector, returning a new [`Loc`] vector |
0 | 81 | #[inline] |
5 | 82 | pub fn map<H>(&self, g : impl Fn(F) -> H) -> Loc<H, N> { |
0 | 83 | Loc::new(map1(self, |u| g(*u))) |
84 | } | |
85 | ||
5 | 86 | /// Maps `g` over pairs of elements of two vectors, retuning a new one. |
0 | 87 | #[inline] |
5 | 88 | pub fn map2<H>(&self, other : &Self, g : impl Fn(F, F) -> H) -> Loc<H, N> { |
0 | 89 | Loc::new(map2(self, other, |u, v| g(*u, *v))) |
90 | } | |
91 | ||
5 | 92 | /// Maps `g` over mutable references to elements of the vector. |
0 | 93 | #[inline] |
5 | 94 | pub fn map_mut(&mut self, g : impl Fn(&mut F)) { |
0 | 95 | map1_mut(self, g) |
96 | } | |
97 | ||
5 | 98 | /// Maps `g` over pairs of mutable references to elements of `self, and elements |
99 | /// of `other` vector. | |
0 | 100 | #[inline] |
5 | 101 | pub fn map2_mut(&mut self, other : &Self, g : impl Fn(&mut F, F)) { |
0 | 102 | map2_mut(self, other, |u, v| g(u, *v)) |
103 | } | |
104 | ||
5 | 105 | /// Maps `g` over the elements of `self` and returns the product of the results. |
0 | 106 | #[inline] |
5 | 107 | pub fn product_map<A : Num>(&self, g : impl Fn(F) -> A) -> A { |
0 | 108 | match N { |
5 | 109 | 1 => g(unsafe { *self.0.get_unchecked(0) }), |
110 | 2 => g(unsafe { *self.0.get_unchecked(0) }) * | |
111 | g(unsafe { *self.0.get_unchecked(1) }), | |
112 | 3 => g(unsafe { *self.0.get_unchecked(0) }) * | |
113 | g(unsafe { *self.0.get_unchecked(1) }) * | |
114 | g(unsafe { *self.0.get_unchecked(2) }), | |
115 | _ => self.iter().fold(A::ONE, |m, &x| m * g(x)) | |
0 | 116 | } |
117 | } | |
118 | } | |
119 | ||
5 | 120 | /// Construct a [`Loc`]. |
121 | /// | |
122 | /// Use as | |
123 | /// ``` | |
124 | /// # use alg_tools::loc::Loc; | |
125 | /// # use alg_tools::loc; | |
126 | /// let x = loc![1.0, 2.0]; | |
127 | /// ``` | |
0 | 128 | #[macro_export] |
129 | macro_rules! loc { | |
130 | ($($x:expr),+ $(,)?) => { Loc::new([$($x),+]) } | |
131 | } | |
132 | ||
133 | ||
134 | impl<F, const N : usize> From<[F; N]> for Loc<F, N> { | |
135 | #[inline] | |
136 | fn from(other: [F; N]) -> Loc<F, N> { | |
137 | Loc(other) | |
138 | } | |
139 | } | |
140 | ||
141 | /*impl<F : Copy, const N : usize> From<&[F; N]> for Loc<F, N> { | |
142 | #[inline] | |
143 | fn from(other: &[F; N]) -> Loc<F, N> { | |
144 | Loc(*other) | |
145 | } | |
146 | }*/ | |
147 | ||
148 | impl<F> From<F> for Loc<F, 1> { | |
149 | #[inline] | |
150 | fn from(other: F) -> Loc<F, 1> { | |
151 | Loc([other]) | |
152 | } | |
153 | } | |
154 | ||
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
155 | impl<F> Loc<F, 1> { |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
156 | #[inline] |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
157 | pub fn flatten1d(self) -> F { |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
158 | let Loc([v]) = self; |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
159 | v |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
160 | } |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
161 | } |
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
162 | |
0 | 163 | impl<F, const N : usize> From<Loc<F, N>> for [F; N] { |
164 | #[inline] | |
165 | fn from(other : Loc<F, N>) -> [F; N] { | |
166 | other.0 | |
167 | } | |
168 | } | |
169 | ||
170 | /*impl<F : Copy, const N : usize> From<&Loc<F, N>> for [F; N] { | |
171 | #[inline] | |
172 | fn from(other : &Loc<F, N>) -> [F; N] { | |
173 | other.0 | |
174 | } | |
175 | }*/ | |
176 | ||
177 | ||
178 | impl<F, const N : usize> IntoIterator for Loc<F, N> { | |
179 | type Item = <[F; N] as IntoIterator>::Item; | |
180 | type IntoIter = <[F; N] as IntoIterator>::IntoIter; | |
181 | ||
182 | #[inline] | |
183 | fn into_iter(self) -> Self::IntoIter { | |
184 | self.0.into_iter() | |
185 | } | |
186 | } | |
187 | ||
188 | // Indexing | |
189 | ||
190 | impl<F, Ix, const N : usize> Index<Ix> for Loc<F,N> | |
191 | where [F; N] : Index<Ix> { | |
192 | type Output = <[F; N] as Index<Ix>>::Output; | |
193 | ||
194 | #[inline] | |
195 | fn index(&self, ix : Ix) -> &Self::Output { | |
196 | self.0.index(ix) | |
197 | } | |
198 | } | |
199 | ||
200 | impl<F, Ix, const N : usize> IndexMut<Ix> for Loc<F,N> | |
201 | where [F; N] : IndexMut<Ix> { | |
202 | #[inline] | |
203 | fn index_mut(&mut self, ix : Ix) -> &mut Self::Output { | |
204 | self.0.index_mut(ix) | |
205 | } | |
206 | } | |
207 | ||
208 | // Arithmetic | |
209 | ||
210 | macro_rules! make_binop { | |
211 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
212 | impl<F : Num, const N : usize> $trait<Loc<F,N>> for Loc<F, N> { | |
213 | type Output = Loc<F, N>; | |
214 | #[inline] | |
215 | fn $fn(mut self, other : Loc<F, N>) -> Self::Output { | |
216 | self.$fn_assign(other); | |
217 | self | |
218 | } | |
219 | } | |
220 | ||
221 | impl<'a, F : Num, const N : usize> $trait<&'a Loc<F,N>> for Loc<F, N> { | |
222 | type Output = Loc<F, N>; | |
223 | #[inline] | |
224 | fn $fn(mut self, other : &'a Loc<F, N>) -> Self::Output { | |
225 | self.$fn_assign(other); | |
226 | self | |
227 | } | |
228 | } | |
229 | ||
230 | impl<'b, F : Num, const N : usize> $trait<Loc<F,N>> for &'b Loc<F, N> { | |
231 | type Output = Loc<F, N>; | |
232 | #[inline] | |
233 | fn $fn(self, other : Loc<F, N>) -> Self::Output { | |
234 | self.map2(&other, |a, b| a.$fn(b)) | |
235 | } | |
236 | } | |
237 | ||
238 | impl<'a, 'b, F : Num, const N : usize> $trait<&'a Loc<F,N>> for &'b Loc<F, N> { | |
239 | type Output = Loc<F, N>; | |
240 | #[inline] | |
241 | fn $fn(self, other : &'a Loc<F, N>) -> Self::Output { | |
242 | self.map2(other, |a, b| a.$fn(b)) | |
243 | } | |
244 | } | |
245 | ||
246 | impl<F : Num, const N : usize> $trait_assign<Loc<F,N>> for Loc<F, N> { | |
247 | #[inline] | |
248 | fn $fn_assign(&mut self, other : Loc<F, N>) { | |
249 | self.map2_mut(&other, |a, b| a.$fn_assign(b)) | |
250 | } | |
251 | } | |
252 | ||
253 | impl<'a, F : Num, const N : usize> $trait_assign<&'a Loc<F,N>> for Loc<F, N> { | |
254 | #[inline] | |
255 | fn $fn_assign(&mut self, other : &'a Loc<F, N>) { | |
256 | self.map2_mut(other, |a, b| a.$fn_assign(b)) | |
257 | } | |
258 | } | |
259 | } | |
260 | } | |
261 | ||
262 | make_binop!(Add, add, AddAssign, add_assign); | |
263 | make_binop!(Sub, sub, SubAssign, sub_assign); | |
264 | ||
28
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
265 | 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
|
266 | 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
|
267 | match iter.next() { |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
268 | None => Self::ORIGIN, |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
269 | Some(mut v) => { |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
270 | for w in iter { |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
271 | v += w |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
272 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
273 | v |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
274 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
275 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
276 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
277 | } |
331345346e7b
Implement std::iter::Sum for Loc<F, N>
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
278 | |
0 | 279 | macro_rules! make_scalarop_rhs { |
280 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
281 | impl<F : Num, const N : usize> $trait<F> for Loc<F, N> { | |
282 | type Output = Loc<F, N>; | |
283 | #[inline] | |
284 | fn $fn(self, b : F) -> Self::Output { | |
285 | self.map(|a| a.$fn(b)) | |
286 | } | |
287 | } | |
288 | ||
289 | impl<'a, F : Num, const N : usize> $trait<&'a F> for Loc<F, N> { | |
290 | type Output = Loc<F, N>; | |
291 | #[inline] | |
292 | fn $fn(self, b : &'a F) -> Self::Output { | |
293 | self.map(|a| a.$fn(*b)) | |
294 | } | |
295 | } | |
296 | ||
297 | impl<'b, F : Num, const N : usize> $trait<F> for &'b Loc<F, N> { | |
298 | type Output = Loc<F, N>; | |
299 | #[inline] | |
300 | fn $fn(self, b : F) -> Self::Output { | |
301 | self.map(|a| a.$fn(b)) | |
302 | } | |
303 | } | |
304 | ||
305 | impl<'a, 'b, F : Float, const N : usize> $trait<&'a F> for &'b Loc<F, N> { | |
306 | type Output = Loc<F, N>; | |
307 | #[inline] | |
308 | fn $fn(self, b : &'a F) -> Self::Output { | |
309 | self.map(|a| a.$fn(*b)) | |
310 | } | |
311 | } | |
312 | ||
313 | impl<F : Num, const N : usize> $trait_assign<F> for Loc<F, N> { | |
314 | #[inline] | |
315 | fn $fn_assign(&mut self, b : F) { | |
316 | self.map_mut(|a| a.$fn_assign(b)); | |
317 | } | |
318 | } | |
319 | ||
320 | impl<'a, F : Num, const N : usize> $trait_assign<&'a F> for Loc<F, N> { | |
321 | #[inline] | |
322 | fn $fn_assign(&mut self, b : &'a F) { | |
323 | self.map_mut(|a| a.$fn_assign(*b)); | |
324 | } | |
325 | } | |
326 | } | |
327 | } | |
328 | ||
329 | ||
330 | make_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
331 | make_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
332 | ||
333 | macro_rules! make_unaryop { | |
334 | ($trait:ident, $fn:ident) => { | |
335 | impl<F : SignedNum, const N : usize> $trait for Loc<F, N> { | |
336 | type Output = Loc<F, N>; | |
337 | #[inline] | |
338 | fn $fn(mut self) -> Self::Output { | |
339 | self.map_mut(|a| *a = (*a).$fn()); | |
340 | self | |
341 | } | |
342 | } | |
343 | ||
344 | impl<'a, F : SignedNum, const N : usize> $trait for &'a Loc<F, N> { | |
345 | type Output = Loc<F, N>; | |
346 | #[inline] | |
347 | fn $fn(self) -> Self::Output { | |
348 | self.map(|a| a.$fn()) | |
349 | } | |
350 | } | |
351 | } | |
352 | } | |
353 | ||
354 | make_unaryop!(Neg, neg); | |
355 | ||
356 | macro_rules! make_scalarop_lhs { | |
357 | ($trait:ident, $fn:ident; $($f:ident)+) => { $( | |
358 | impl<const N : usize> $trait<Loc<$f,N>> for $f { | |
359 | type Output = Loc<$f, N>; | |
360 | #[inline] | |
361 | fn $fn(self, v : Loc<$f,N>) -> Self::Output { | |
362 | v.map(|b| self.$fn(b)) | |
363 | } | |
364 | } | |
365 | ||
366 | impl<'a, const N : usize> $trait<&'a Loc<$f,N>> for $f { | |
367 | type Output = Loc<$f, N>; | |
368 | #[inline] | |
369 | fn $fn(self, v : &'a Loc<$f,N>) -> Self::Output { | |
370 | v.map(|b| self.$fn(b)) | |
371 | } | |
372 | } | |
373 | ||
374 | impl<'b, const N : usize> $trait<Loc<$f,N>> for &'b $f { | |
375 | type Output = Loc<$f, N>; | |
376 | #[inline] | |
377 | fn $fn(self, v : Loc<$f,N>) -> Self::Output { | |
378 | v.map(|b| self.$fn(b)) | |
379 | } | |
380 | } | |
381 | ||
382 | impl<'a, 'b, const N : usize> $trait<&'a Loc<$f,N>> for &'b $f { | |
383 | type Output = Loc<$f, N>; | |
384 | #[inline] | |
385 | fn $fn(self, v : &'a Loc<$f, N>) -> Self::Output { | |
386 | v.map(|b| self.$fn(b)) | |
387 | } | |
388 | } | |
389 | )+ } | |
390 | } | |
391 | ||
392 | make_scalarop_lhs!(Mul, mul; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); | |
393 | make_scalarop_lhs!(Div, div; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); | |
394 | ||
395 | // Norms | |
396 | ||
397 | macro_rules! domination { | |
398 | ($norm:ident, $dominates:ident) => { | |
399 | impl<F : Float, const N : usize> Dominated<F, $dominates, Loc<F, N>> for $norm { | |
400 | #[inline] | |
401 | fn norm_factor(&self, _p : $dominates) -> F { | |
402 | F::ONE | |
403 | } | |
404 | #[inline] | |
405 | fn from_norm(&self, p_norm : F, _p : $dominates) -> F { | |
406 | p_norm | |
407 | } | |
408 | } | |
409 | }; | |
410 | ($norm:ident, $dominates:ident, $fn:path) => { | |
411 | impl<F : Float, const N : usize> Dominated<F, $dominates, Loc<F, N>> for $norm { | |
412 | #[inline] | |
413 | fn norm_factor(&self, _p : $dominates) -> F { | |
414 | $fn(F::cast_from(N)) | |
415 | } | |
416 | } | |
417 | }; | |
418 | } | |
419 | ||
420 | domination!(L1, L1); | |
421 | domination!(L2, L2); | |
422 | domination!(Linfinity, Linfinity); | |
423 | ||
424 | domination!(L1, L2, F::sqrt); | |
425 | domination!(L2, Linfinity, F::sqrt); | |
426 | domination!(L1, Linfinity, std::convert::identity); | |
427 | ||
428 | domination!(Linfinity, L1); | |
429 | domination!(Linfinity, L2); | |
430 | domination!(L2, L1); | |
431 | ||
432 | impl<F : Num,const N : usize> Dot<Loc<F, N>,F> for Loc<F, N> { | |
433 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
434 | /// Use [`nalgebra`] for larger vectors. | |
435 | #[inline] | |
436 | fn dot(&self, other : &Loc<F, N>) -> F { | |
437 | self.0.iter() | |
438 | .zip(other.0.iter()) | |
439 | .fold(F::ZERO, |m, (&v, &w)| m + v * w) | |
440 | } | |
441 | } | |
442 | ||
443 | impl<F : Float,const N : usize> Euclidean<F> for Loc<F, N> { | |
444 | type Output = Self; | |
445 | ||
446 | #[inline] | |
447 | fn similar_origin(&self) -> Self { | |
448 | Self::ORIGIN | |
449 | } | |
450 | ||
451 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
452 | /// Use [`nalgebra`] for larger vectors. | |
453 | #[inline] | |
454 | fn norm2_squared(&self) -> F { | |
455 | self.iter().fold(F::ZERO, |m, &v| m + v * v) | |
456 | } | |
457 | ||
458 | fn dist2_squared(&self, other : &Self) -> F { | |
459 | self.iter() | |
460 | .zip(other.iter()) | |
461 | .fold(F::ZERO, |m, (&v, &w)| { let d = v - w; m + d * d }) | |
462 | } | |
463 | ||
464 | #[inline] | |
465 | fn norm2(&self) -> F { | |
466 | // Optimisation for N==1 that avoids squaring and square rooting. | |
467 | if N==1 { | |
468 | unsafe { self.0.get_unchecked(0) }.abs() | |
469 | } else { | |
470 | self.norm2_squared().sqrt() | |
471 | } | |
472 | } | |
473 | ||
474 | #[inline] | |
475 | fn dist2(&self, other : &Self) -> F { | |
476 | // Optimisation for N==1 that avoids squaring and square rooting. | |
477 | if N==1 { | |
478 | unsafe { *self.0.get_unchecked(0) - *other.0.get_unchecked(0) }.abs() | |
479 | } else { | |
480 | self.dist2_squared(other).sqrt() | |
481 | } | |
482 | } | |
483 | } | |
484 | ||
485 | impl<F : Num, const N : usize> Loc<F, N> { | |
52 | 486 | /// Origin point |
0 | 487 | pub const ORIGIN : Self = Loc([F::ZERO; N]); |
488 | } | |
489 | ||
52 | 490 | impl<F : Num + std::ops::Neg<Output=F>, const N : usize> Loc<F, N> { |
491 | /// Reflects along the given coordinate | |
492 | pub fn reflect(mut self, i : usize) -> Self { | |
493 | self[i] = -self[i]; | |
494 | self | |
495 | } | |
496 | ||
497 | /// Reflects along the given coordinate sequences | |
498 | pub fn reflect_many<I : IntoIterator<Item=usize>>(mut self, idxs : I) -> Self { | |
499 | for i in idxs { | |
500 | self[i]=-self[i] | |
501 | } | |
502 | self | |
503 | } | |
504 | } | |
505 | ||
506 | impl<F : std::ops::Neg<Output=F>> Loc<F, 2> { | |
507 | /// Reflect x coordinate | |
508 | pub fn reflect_x(self) -> Self { | |
509 | let Loc([x, y]) = self; | |
510 | [-x, y].into() | |
511 | } | |
512 | ||
513 | /// Reflect y coordinate | |
514 | pub fn reflect_y(self) -> Self { | |
515 | let Loc([x, y]) = self; | |
516 | [x, -y].into() | |
517 | } | |
518 | } | |
519 | ||
520 | impl<F : Float> Loc<F, 2> { | |
521 | /// Rotate by angle φ | |
522 | pub fn rotate(self, φ : F) -> Self { | |
523 | let Loc([x, y]) = self; | |
524 | let sin_φ = φ.sin(); | |
525 | let cos_φ = φ.cos(); | |
526 | [cos_φ * x - sin_φ * y, | |
527 | sin_φ * x + cos_φ * y].into() | |
528 | } | |
529 | } | |
530 | ||
531 | impl<F : std::ops::Neg<Output=F>> Loc<F, 3> { | |
532 | /// Reflect x coordinate | |
533 | pub fn reflect_x(self) -> Self { | |
534 | let Loc([x, y, z]) = self; | |
535 | [-x, y, z].into() | |
536 | } | |
537 | ||
538 | /// Reflect y coordinate | |
539 | pub fn reflect_y(self) -> Self { | |
540 | let Loc([x, y, z]) = self; | |
541 | [x, -y, z].into() | |
542 | } | |
543 | ||
544 | /// Reflect y coordinate | |
545 | pub fn reflect_z(self) -> Self { | |
546 | let Loc([x, y, z]) = self; | |
547 | [x, y, -z].into() | |
548 | } | |
549 | } | |
550 | ||
551 | impl<F : Float> Loc<F, 3> { | |
552 | /// Rotate by angles (yaw, pitch, roll) | |
553 | pub fn rotate(self, Loc([φ, ψ, θ]) : Self) -> Self { | |
554 | let Loc([mut x, mut y, mut z]) = self; | |
555 | let sin_φ = φ.sin(); | |
556 | let cos_φ = φ.cos(); | |
557 | [x, y, z] = [cos_φ * x - sin_φ *y, | |
558 | sin_φ * x + cos_φ * y, | |
559 | z]; | |
560 | let sin_ψ = ψ.sin(); | |
561 | let cos_ψ = ψ.cos(); | |
562 | [x, y, z] = [cos_ψ * x + sin_ψ * z, | |
563 | y, | |
564 | -sin_ψ * x + cos_ψ * z]; | |
565 | let sin_θ = θ.sin(); | |
566 | let cos_θ = θ.cos(); | |
567 | [x, y, z] = [x, | |
568 | cos_θ * y - sin_θ * z, | |
569 | sin_θ * y + cos_θ * z]; | |
570 | [x, y, z].into() | |
571 | } | |
572 | } | |
573 | ||
0 | 574 | impl<F : Float,const N : usize> StaticEuclidean<F> for Loc<F, N> { |
575 | #[inline] | |
576 | fn origin() -> Self { | |
577 | Self::ORIGIN | |
578 | } | |
579 | } | |
580 | ||
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
581 | |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
582 | /// The default norm for `Loc` is [`L2`]. |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
583 | impl<F : Float, const N : usize> Normed<F> for Loc<F, N> { |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
584 | type NormExp = L2; |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
585 | |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
586 | #[inline] |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
587 | fn norm_exponent(&self) -> Self::NormExp { |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
588 | L2 |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
589 | } |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
590 | |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
591 | // #[inline] |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
592 | // fn similar_origin(&self) -> Self { |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
593 | // [F::ZERO; N].into() |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
594 | // } |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
595 | |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
596 | #[inline] |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
597 | fn is_zero(&self) -> bool { |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
598 | self.norm2_squared() == F::ZERO |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
599 | } |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
600 | } |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
601 | |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
602 | impl<F : Float, const N : usize> HasDual<F> for Loc<F, N> { |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
603 | type DualSpace = Self; |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
604 | } |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
605 | |
0 | 606 | impl<F : Float, const N : usize> Norm<F, L2> for Loc<F, N> { |
607 | #[inline] | |
608 | fn norm(&self, _ : L2) -> F { self.norm2() } | |
609 | } | |
610 | ||
611 | impl<F : Float, const N : usize> Dist<F, L2> for Loc<F, N> { | |
612 | #[inline] | |
613 | fn dist(&self, other : &Self, _ : L2) -> F { self.dist2(other) } | |
614 | } | |
615 | ||
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
616 | /* Implemented automatically as Euclidean. |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
617 | impl<F : Float, const N : usize> Projection<F, L2> for Loc<F, N> { |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
618 | #[inline] |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
619 | fn proj_ball_mut(&mut self, ρ : F, nrm : L2) { |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
620 | let n = self.norm(nrm); |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
621 | if n > ρ { |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
622 | *self *= ρ/n; |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
623 | } |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
624 | } |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
625 | }*/ |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
626 | |
0 | 627 | impl<F : Float, const N : usize> Norm<F, L1> for Loc<F, N> { |
628 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
629 | /// Use [`nalgebra`] for larger vectors. | |
630 | #[inline] | |
631 | fn norm(&self, _ : L1) -> F { | |
632 | self.iter().fold(F::ZERO, |m, v| m + v.abs()) | |
633 | } | |
634 | } | |
635 | ||
636 | impl<F : Float, const N : usize> Dist<F, L1> for Loc<F, N> { | |
637 | #[inline] | |
638 | fn dist(&self, other : &Self, _ : L1) -> F { | |
639 | self.iter() | |
640 | .zip(other.iter()) | |
641 | .fold(F::ZERO, |m, (&v, &w)| m + (v-w).abs() ) | |
642 | } | |
643 | } | |
644 | ||
645 | impl<F : Float, const N : usize> Projection<F, Linfinity> for Loc<F, N> { | |
646 | #[inline] | |
647 | fn proj_ball_mut(&mut self, ρ : F, _ : Linfinity) { | |
648 | self.iter_mut().for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ)) | |
649 | } | |
650 | } | |
651 | ||
652 | impl<F : Float, const N : usize> Norm<F, Linfinity> for Loc<F, N> { | |
653 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
654 | /// Use [`nalgebra`] for larger vectors. | |
655 | #[inline] | |
656 | fn norm(&self, _ : Linfinity) -> F { | |
657 | self.iter().fold(F::ZERO, |m, v| m.max(v.abs())) | |
658 | } | |
659 | } | |
660 | ||
661 | impl<F : Float, const N : usize> Dist<F, Linfinity> for Loc<F, N> { | |
662 | #[inline] | |
663 | fn dist(&self, other : &Self, _ : Linfinity) -> F { | |
664 | self.iter() | |
665 | .zip(other.iter()) | |
666 | .fold(F::ZERO, |m, (&v, &w)| m.max((v-w).abs())) | |
667 | } | |
668 | } | |
669 | ||
670 | ||
671 | // Misc. | |
672 | ||
673 | impl<A, const N : usize> FixedLength<N> for Loc<A,N> { | |
674 | type Iter = std::array::IntoIter<A, N>; | |
675 | type Elem = A; | |
676 | #[inline] | |
677 | fn fl_iter(self) -> Self::Iter { | |
678 | self.into_iter() | |
679 | } | |
680 | } | |
681 | ||
682 | impl<A, const N : usize> FixedLengthMut<N> for Loc<A,N> { | |
683 | type IterMut<'a> = std::slice::IterMut<'a, A> where A : 'a; | |
684 | #[inline] | |
685 | fn fl_iter_mut(&mut self) -> Self::IterMut<'_> { | |
686 | self.iter_mut() | |
687 | } | |
688 | } | |
689 | ||
690 | impl<'a, A, const N : usize> FixedLength<N> for &'a Loc<A,N> { | |
691 | type Iter = std::slice::Iter<'a, A>; | |
692 | type Elem = &'a A; | |
693 | #[inline] | |
694 | fn fl_iter(self) -> Self::Iter { | |
695 | self.iter() | |
696 | } | |
697 | } | |
698 | ||
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
699 | |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
700 | impl<F : Num, const N : usize> Space for Loc<F, N> { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
701 | type Decomp = BasicDecomposition; |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
702 | } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
703 | |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
704 | impl<F : Num, const N : usize> Mapping<Loc<F, N>> for Loc<F, N> { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
705 | type Codomain = F; |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
706 | |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
707 | fn apply<I : Instance<Loc<F, N>>>(&self, x : I) -> Self::Codomain { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
708 | x.eval(|x̃| self.dot(x̃)) |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
709 | } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
710 | } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
711 | |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
712 | impl<F : Num, const N : usize> Linear<Loc<F, N>> for Loc<F, N> { } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
713 | |
0 | 714 | impl<F : Num, const N : usize> AXPY<F, Loc<F, N>> for Loc<F, N> { |
715 | #[inline] | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
716 | fn axpy<I : Instance<Loc<F, N>>>(&mut self, α : F, x : I, β : F) { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
717 | x.eval(|x̃| { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
718 | if β == F::ZERO { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
719 | map2_mut(self, x̃, |yi, xi| { *yi = α * (*xi) }) |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
720 | } else { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
721 | map2_mut(self, x̃, |yi, xi| { *yi = β * (*yi) + α * (*xi) }) |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
722 | } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
723 | }) |
0 | 724 | } |
725 | ||
726 | #[inline] | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
727 | fn copy_from<I : Instance<Loc<F, N>>>(&mut self, x : I) { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
728 | x.eval(|x̃| map2_mut(self, x̃, |yi, xi| *yi = *xi )) |
0 | 729 | } |
730 | } |