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