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