Mon, 01 Sep 2025 13:51:03 -0500
fubar
| 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> { |
| 0 | 451 | /// This implementation is not stabilised as it's meant to be used for very small vectors. |
| 452 | /// Use [`nalgebra`] for larger vectors. | |
| 453 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
454 | 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
|
455 | 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
|
456 | self.0 |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
457 | .iter() |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
458 | .zip(r.0.iter()) |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
459 | .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
|
460 | }) |
| 0 | 461 | } |
| 462 | ||
| 463 | /// This implementation is not stabilised as it's meant to be used for very small vectors. | |
| 464 | /// Use [`nalgebra`] for larger vectors. | |
| 465 | #[inline] | |
| 466 | fn norm2_squared(&self) -> F { | |
| 467 | self.iter().fold(F::ZERO, |m, &v| m + v * v) | |
| 468 | } | |
| 469 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
470 | 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
|
471 | 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
|
472 | 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
|
473 | let d = v - w; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
474 | m + d * d |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
475 | }) |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
476 | }) |
| 0 | 477 | } |
| 478 | ||
| 479 | #[inline] | |
| 480 | fn norm2(&self) -> F { | |
| 481 | // Optimisation for N==1 that avoids squaring and square rooting. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
482 | if N == 1 { |
| 0 | 483 | unsafe { self.0.get_unchecked(0) }.abs() |
| 484 | } else { | |
| 485 | self.norm2_squared().sqrt() | |
| 486 | } | |
| 487 | } | |
| 488 | ||
| 489 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
490 | fn dist2<I: Instance<Self>>(&self, other: I) -> F { |
| 0 | 491 | // 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
|
492 | if N == 1 { |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
493 | 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
|
494 | 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
|
495 | }) |
| 0 | 496 | } else { |
| 497 | self.dist2_squared(other).sqrt() | |
| 498 | } | |
| 499 | } | |
| 500 | } | |
| 501 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
502 | impl<F: Num, const N: usize> Loc<N, F> { |
| 52 | 503 | /// Origin point |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
504 | pub const ORIGIN: Self = Loc([F::ZERO; N]); |
| 0 | 505 | } |
| 506 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
507 | impl<F: Num + std::ops::Neg<Output = F>, const N: usize> Loc<N, F> { |
| 52 | 508 | /// Reflects along the given coordinate |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
509 | pub fn reflect(mut self, i: usize) -> Self { |
| 52 | 510 | self[i] = -self[i]; |
| 511 | self | |
| 512 | } | |
| 513 | ||
| 514 | /// Reflects along the given coordinate sequences | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
515 | pub fn reflect_many<I: IntoIterator<Item = usize>>(mut self, idxs: I) -> Self { |
| 52 | 516 | for i in idxs { |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
517 | self[i] = -self[i] |
| 52 | 518 | } |
| 519 | self | |
| 520 | } | |
| 521 | } | |
| 522 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
523 | impl<F: std::ops::Neg<Output = F>> Loc<2, F> { |
| 52 | 524 | /// Reflect x coordinate |
| 525 | pub fn reflect_x(self) -> Self { | |
| 526 | let Loc([x, y]) = self; | |
| 527 | [-x, y].into() | |
| 528 | } | |
| 529 | ||
| 530 | /// Reflect y coordinate | |
| 531 | pub fn reflect_y(self) -> Self { | |
| 532 | let Loc([x, y]) = self; | |
| 533 | [x, -y].into() | |
| 534 | } | |
| 535 | } | |
| 536 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
537 | impl<F: Float> Loc<2, F> { |
| 52 | 538 | /// Rotate by angle φ |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
539 | pub fn rotate(self, φ: F) -> Self { |
| 52 | 540 | let Loc([x, y]) = self; |
| 541 | let sin_φ = φ.sin(); | |
| 542 | let cos_φ = φ.cos(); | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
543 | [cos_φ * x - sin_φ * y, sin_φ * x + cos_φ * y].into() |
| 52 | 544 | } |
| 545 | } | |
| 546 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
547 | impl<F: std::ops::Neg<Output = F>> Loc<3, F> { |
| 52 | 548 | /// Reflect x coordinate |
| 549 | pub fn reflect_x(self) -> Self { | |
| 550 | let Loc([x, y, z]) = self; | |
| 551 | [-x, y, z].into() | |
| 552 | } | |
| 553 | ||
| 554 | /// Reflect y coordinate | |
| 555 | pub fn reflect_y(self) -> Self { | |
| 556 | let Loc([x, y, z]) = self; | |
| 557 | [x, -y, z].into() | |
| 558 | } | |
| 559 | ||
| 560 | /// Reflect y coordinate | |
| 561 | pub fn reflect_z(self) -> Self { | |
| 562 | let Loc([x, y, z]) = self; | |
| 563 | [x, y, -z].into() | |
| 564 | } | |
| 565 | } | |
| 566 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
567 | impl<F: Float> Loc<3, F> { |
| 52 | 568 | /// Rotate by angles (yaw, pitch, roll) |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
569 | pub fn rotate(self, Loc([φ, ψ, θ]): Self) -> Self { |
| 52 | 570 | let Loc([mut x, mut y, mut z]) = self; |
| 571 | let sin_φ = φ.sin(); | |
| 572 | let cos_φ = φ.cos(); | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
573 | [x, y, z] = [cos_φ * x - sin_φ * y, sin_φ * x + cos_φ * y, z]; |
| 52 | 574 | let sin_ψ = ψ.sin(); |
| 575 | let cos_ψ = ψ.cos(); | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
576 | [x, y, z] = [cos_ψ * x + sin_ψ * z, y, -sin_ψ * x + cos_ψ * z]; |
| 52 | 577 | let sin_θ = θ.sin(); |
| 578 | let cos_θ = θ.cos(); | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
579 | [x, y, z] = [x, cos_θ * y - sin_θ * z, sin_θ * y + cos_θ * z]; |
| 52 | 580 | [x, y, z].into() |
| 581 | } | |
| 582 | } | |
| 583 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
584 | impl<F: Float, const N: usize> StaticEuclidean<F> for Loc<N, F> { |
| 0 | 585 | #[inline] |
| 586 | fn origin() -> Self { | |
| 587 | Self::ORIGIN | |
| 588 | } | |
| 589 | } | |
| 590 | ||
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
591 | /// 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
|
592 | 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
|
593 | type NormExp = L2; |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
594 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
595 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
596 | fn norm_exponent(&self) -> Self::NormExp { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
597 | L2 |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
598 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
599 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
600 | // #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
601 | // fn similar_origin(&self) -> Self { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
602 | // [F::ZERO; N].into() |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
603 | // } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
604 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
605 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
606 | fn is_zero(&self) -> bool { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
607 | self.norm2_squared() == F::ZERO |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
608 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
609 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
610 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
611 | 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
|
612 | type DualSpace = Self; |
| 138 | 613 | |
| 614 | fn dual_origin(&self) -> Self::DualSpace { | |
| 615 | self.similar_origin() | |
| 616 | } | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
617 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
618 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
619 | impl<F: Float, const N: usize> Norm<L2, F> for Loc<N, F> { |
| 0 | 620 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
621 | fn norm(&self, _: L2) -> F { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
622 | self.norm2() |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
623 | } |
| 0 | 624 | } |
| 625 | ||
|
145
0b9aecd7bb76
Dist argument order changed to reflect other changes
Tuomo Valkonen <tuomov@iki.fi>
parents:
138
diff
changeset
|
626 | impl<F: Float, const N: usize> Dist<L2, F> for Loc<N, F> { |
| 0 | 627 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
628 | 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
|
629 | self.dist2(other) |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
630 | } |
| 0 | 631 | } |
| 632 | ||
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
633 | /* Implemented automatically as Euclidean. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
634 | impl<F : Float, const N : usize> Projection<F, L2> for Loc<N, F> { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
635 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
636 | fn proj_ball_mut(&mut self, ρ : F, nrm : L2) { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
637 | let n = self.norm(nrm); |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
638 | if n > ρ { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
639 | *self *= ρ/n; |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
640 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
641 | } |
|
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 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
644 | impl<F: Float, const N: usize> Norm<L1, F> for Loc<N, F> { |
| 0 | 645 | /// This implementation is not stabilised as it's meant to be used for very small vectors. |
| 646 | /// Use [`nalgebra`] for larger vectors. | |
| 647 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
648 | fn norm(&self, _: L1) -> F { |
| 0 | 649 | self.iter().fold(F::ZERO, |m, v| m + v.abs()) |
| 650 | } | |
| 651 | } | |
| 652 | ||
|
145
0b9aecd7bb76
Dist argument order changed to reflect other changes
Tuomo Valkonen <tuomov@iki.fi>
parents:
138
diff
changeset
|
653 | impl<F: Float, const N: usize> Dist<L1, F> for Loc<N, F> { |
| 0 | 654 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
655 | 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
|
656 | 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
|
657 | self.iter() |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
658 | .zip(r.iter()) |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
659 | .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
|
660 | }) |
| 0 | 661 | } |
| 662 | } | |
| 663 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
664 | impl<F: Float, const N: usize> Projection<F, Linfinity> for Loc<N, F> { |
| 0 | 665 | #[inline] |
| 150 | 666 | fn proj_ball(mut self, ρ: F, exp: Linfinity) -> Self { |
| 667 | self.proj_ball_mut(ρ, exp); | |
| 668 | self | |
| 669 | } | |
| 670 | } | |
| 671 | ||
| 672 | impl<F: Float, const N: usize> ProjectionMut<F, Linfinity> for Loc<N, F> { | |
| 673 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
674 | 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
|
675 | self.iter_mut() |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
676 | .for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ)) |
| 0 | 677 | } |
| 678 | } | |
| 679 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
680 | impl<F: Float, const N: usize> Norm<Linfinity, F> for Loc<N, F> { |
| 0 | 681 | /// This implementation is not stabilised as it's meant to be used for very small vectors. |
| 682 | /// Use [`nalgebra`] for larger vectors. | |
| 683 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
684 | fn norm(&self, _: Linfinity) -> F { |
| 0 | 685 | self.iter().fold(F::ZERO, |m, v| m.max(v.abs())) |
| 686 | } | |
| 687 | } | |
| 688 | ||
|
145
0b9aecd7bb76
Dist argument order changed to reflect other changes
Tuomo Valkonen <tuomov@iki.fi>
parents:
138
diff
changeset
|
689 | impl<F: Float, const N: usize> Dist<Linfinity, F> for Loc<N, F> { |
| 0 | 690 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
691 | 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
|
692 | 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
|
693 | self.iter() |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
694 | .zip(r.iter()) |
|
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
132
diff
changeset
|
695 | .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
|
696 | }) |
| 0 | 697 | } |
| 698 | } | |
| 699 | ||
| 700 | // Misc. | |
| 701 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
702 | impl<A, const N: usize> FixedLength<N> for Loc<N, A> { |
| 0 | 703 | type Iter = std::array::IntoIter<A, N>; |
| 704 | type Elem = A; | |
| 705 | #[inline] | |
| 706 | fn fl_iter(self) -> Self::Iter { | |
| 707 | self.into_iter() | |
| 708 | } | |
| 709 | } | |
| 710 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
711 | 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
|
712 | type IterMut<'a> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
713 | = std::slice::IterMut<'a, A> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
714 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
715 | A: 'a; |
| 0 | 716 | #[inline] |
| 717 | fn fl_iter_mut(&mut self) -> Self::IterMut<'_> { | |
| 718 | self.iter_mut() | |
| 719 | } | |
| 720 | } | |
| 721 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
722 | impl<'a, A, const N: usize> FixedLength<N> for &'a Loc<N, A> { |
| 0 | 723 | type Iter = std::slice::Iter<'a, A>; |
| 724 | type Elem = &'a A; | |
| 725 | #[inline] | |
| 726 | fn fl_iter(self) -> Self::Iter { | |
| 727 | self.iter() | |
| 728 | } | |
| 729 | } | |
| 730 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
731 | impl<F: Num, const N: usize> Space for Loc<N, F> { |
| 150 | 732 | type OwnedSpace = Self; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
733 | type Decomp = BasicDecomposition; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
734 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
735 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
736 | 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
|
737 | type Codomain = F; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
738 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
739 | 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
|
740 | x.eval_decompose(|x̃| self.dot(x̃)) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
741 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
742 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
743 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
744 | 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
|
745 | |
| 150 | 746 | 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
|
747 | type Field = F; |
|
62
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
748 | type Owned = Self; |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
749 | |
| 150 | 750 | // #[inline] |
| 751 | // fn make_origin_generator(&self) -> StaticEuclideanOriginGenerator { | |
| 752 | // StaticEuclideanOriginGenerator | |
| 753 | // } | |
| 754 | ||
| 755 | #[inline] | |
| 756 | fn similar_origin(&self) -> Self::Owned { | |
| 757 | Self::ORIGIN | |
| 758 | } | |
| 759 | ||
| 760 | #[inline] | |
| 761 | fn similar_origin_inst<I: Instance<Self>>(_: I) -> Self::Owned { | |
| 762 | Self::ORIGIN | |
| 763 | } | |
| 764 | ||
| 765 | // #[inline] | |
| 766 | // fn into_owned(self) -> Self::Owned { | |
| 767 | // self | |
| 768 | // } | |
| 769 | } | |
| 770 | ||
| 771 | impl<F: Float, const N: usize> AXPY<Loc<N, F>> for Loc<N, F> { | |
| 0 | 772 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
773 | 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
|
774 | x.eval(|x̃| { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
775 | if β == F::ZERO { |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
776 | 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
|
777 | } else { |
|
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 = β * (*yi) + α * (*xi)) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
779 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
52
diff
changeset
|
780 | }) |
| 0 | 781 | } |
| 782 | ||
| 783 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
64
diff
changeset
|
784 | 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
|
785 | x.eval(|x̃| map2_mut(self, x̃, |yi, xi| *yi = *xi)) |
| 0 | 786 | } |
|
62
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
787 | |
|
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
|
788 | #[inline] |
|
62
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
789 | fn set_zero(&mut self) { |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
790 | *self = Self::ORIGIN; |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
791 | } |
| 0 | 792 | } |