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