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