Tue, 02 Sep 2025 15:11:35 -0500
nalgebra horror
| 5 | 1 | /*! |
| 2 | Integration with nalgebra. | |
| 3 | ||
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
4 | This module mainly implements [`Euclidean`], [`Norm`], [`Linear`], etc. for [`nalgebra`] |
| 5 | 5 | matrices and vectors. |
| 6 | It also provides [`ToNalgebraRealField`] as a vomit-inducingly ugly workaround to nalgebra | |
| 7 | force-feeding its own versions of the same basic mathematical methods on `f32` and `f64` as | |
| 8 | [`num_traits`] does. | |
| 9 | */ | |
| 0 | 10 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
11 | use crate::euclidean::*; |
| 156 | 12 | use crate::instance::{Decomposition, Instance, Ownable, Space}; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
13 | use crate::linops::*; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
14 | use crate::norms::*; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
15 | use crate::types::Float; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
16 | use nalgebra::base::allocator::Allocator; |
| 158 | 17 | use nalgebra::base::constraint::{DimEq, SameNumberOfColumns, SameNumberOfRows, ShapeConstraint}; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
18 | use nalgebra::base::dimension::*; |
| 0 | 19 | use nalgebra::{ |
| 156 | 20 | ClosedAddAssign, ClosedMulAssign, DefaultAllocator, Dim, LpNorm, Matrix, MatrixView, OMatrix, |
| 158 | 21 | OVector, RawStorage, RealField, Scalar, SimdComplexField, Storage, StorageMut, UniformNorm, |
| 159 | 22 | Vector, U1, |
| 0 | 23 | }; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
24 | use num_traits::identities::{One, Zero}; |
| 0 | 25 | use std::ops::Mul; |
| 26 | ||
| 150 | 27 | impl<S, M, N, E> Ownable for Matrix<E, M, N, S> |
| 28 | where | |
| 29 | S: Storage<E, M, N>, | |
| 30 | M: Dim, | |
| 31 | N: Dim, | |
| 32 | E: Scalar + Zero + One, | |
| 33 | DefaultAllocator: Allocator<M, N>, | |
| 34 | { | |
| 35 | type OwnedVariant = OMatrix<E, M, N>; | |
| 36 | ||
| 37 | #[inline] | |
| 38 | fn into_owned(self) -> Self::OwnedVariant { | |
| 39 | Matrix::into_owned(self) | |
| 40 | } | |
| 41 | ||
| 42 | /// Returns an owned instance of a reference. | |
| 43 | fn clone_owned(&self) -> Self::OwnedVariant { | |
| 44 | Matrix::clone_owned(self) | |
| 45 | } | |
| 46 | } | |
| 47 | ||
| 159 | 48 | trait StridesOk<E, N, M = U1, S = <DefaultAllocator as Allocator<N, M>>::Buffer<E>>: |
| 49 | DimEq<Dyn, S::RStride> | |
| 50 | + DimEq<Dyn, S::CStride> | |
| 51 | + DimEq<Dyn, <<DefaultAllocator as Allocator<N, M>>::Buffer<E> as RawStorage<E, N, M>>::RStride> | |
| 52 | + DimEq<Dyn, <<DefaultAllocator as Allocator<N, M>>::Buffer<E> as RawStorage<E, N, M>>::CStride> | |
| 53 | where | |
| 54 | S: RawStorage<E, N, M>, | |
| 55 | E: Scalar, | |
| 56 | N: Dim, | |
| 57 | M: Dim, | |
| 58 | DefaultAllocator: Allocator<N, M>, | |
| 59 | { | |
| 60 | } | |
| 61 | ||
| 62 | impl<S, E, N, M> StridesOk<E, N, M, S> for ShapeConstraint | |
| 63 | where | |
| 64 | ShapeConstraint: DimEq<Dyn, S::RStride> | |
| 65 | + DimEq<Dyn, S::CStride> | |
| 66 | + DimEq< | |
| 67 | Dyn, | |
| 68 | <<DefaultAllocator as Allocator<N, M>>::Buffer<E> as RawStorage<E, N, M>>::RStride, | |
| 69 | > + DimEq< | |
| 70 | Dyn, | |
| 71 | <<DefaultAllocator as Allocator<N, M>>::Buffer<E> as RawStorage<E, N, M>>::CStride, | |
| 72 | >, | |
| 73 | S: Storage<E, N, M>, | |
| 74 | E: Scalar, | |
| 75 | N: Dim, | |
| 76 | M: Dim, | |
| 77 | DefaultAllocator: Allocator<N, M>, | |
| 78 | { | |
| 79 | } | |
| 80 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
81 | impl<SM, N, M, E> Space for Matrix<E, N, M, SM> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
82 | where |
| 150 | 83 | SM: Storage<E, N, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
84 | N: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
85 | M: Dim, |
| 150 | 86 | E: Scalar + Zero + One, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
87 | DefaultAllocator: Allocator<N, M>, |
| 159 | 88 | ShapeConstraint: StridesOk<E, N, M, SM> + StridesOk<E, N, M>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
89 | { |
| 150 | 90 | type OwnedSpace = OMatrix<E, N, M>; |
| 156 | 91 | type Decomp = MatrixDecomposition; |
| 92 | } | |
| 93 | ||
| 94 | #[derive(Copy, Clone, Debug)] | |
| 95 | pub struct MatrixDecomposition; | |
| 96 | ||
| 159 | 97 | /// nalgebra [`Storage`] that's convertible to dynamic strides (practically all, but we need this |
| 98 | /// to work around the type system). | |
| 99 | trait ViewableStorage<E, M, K = U1>: Storage<E, M, K> | |
| 100 | where | |
| 101 | M: Dim, | |
| 102 | K: Dim, | |
| 103 | E: Scalar, | |
| 104 | ShapeConstraint: StridesOk<E, M, K, Self>, | |
| 105 | DefaultAllocator: Allocator<M, K>, | |
| 106 | { | |
| 107 | } | |
| 108 | ||
| 109 | impl<M, K, E, S> ViewableStorage<E, M, K> for S | |
| 156 | 110 | where |
| 158 | 111 | S: Storage<E, M, K>, |
| 156 | 112 | M: Dim, |
| 113 | K: Dim, | |
| 159 | 114 | E: Scalar, |
| 115 | ShapeConstraint: StridesOk<E, M, K, Self>, | |
| 116 | DefaultAllocator: Allocator<M, K>, | |
| 117 | { | |
| 118 | } | |
| 119 | ||
| 120 | /// nalgebra [`StorageMut`] that's convertible to dynamic strides (practically all, but we need this | |
| 121 | /// to work around the type system). | |
| 122 | trait ViewableStorageMut<E, M, K = U1>: StorageMut<E, M, K> | |
| 123 | where | |
| 124 | M: Dim, | |
| 125 | K: Dim, | |
| 126 | E: Scalar, | |
| 127 | ShapeConstraint: StridesOk<E, M, K, Self>, | |
| 128 | DefaultAllocator: Allocator<M, K>, | |
| 129 | { | |
| 130 | } | |
| 131 | ||
| 132 | impl<M, K, E, S> ViewableStorageMut<E, M, K> for S | |
| 133 | where | |
| 134 | S: StorageMut<E, M, K>, | |
| 135 | M: Dim, | |
| 136 | K: Dim, | |
| 137 | E: Scalar, | |
| 138 | ShapeConstraint: StridesOk<E, M, K, Self>, | |
| 139 | DefaultAllocator: Allocator<M, K>, | |
| 140 | { | |
| 141 | } | |
| 142 | ||
| 143 | impl<E, M, K, S> Decomposition<Matrix<E, M, K, S>> for MatrixDecomposition | |
| 144 | where | |
| 145 | S: ViewableStorage<E, M, K>, | |
| 146 | M: Dim, | |
| 147 | K: Dim, | |
| 156 | 148 | E: Scalar + Zero + One, |
| 149 | DefaultAllocator: Allocator<M, K>, | |
| 159 | 150 | ShapeConstraint: StridesOk<E, M, K, S> + StridesOk<E, M, K>, |
| 156 | 151 | { |
| 152 | type OwnedInstance = OMatrix<E, M, K>; | |
| 153 | ||
| 154 | type Decomposition<'b> | |
| 155 | = OMatrix<E, M, K> | |
| 156 | where | |
| 157 | Matrix<E, M, K, S>: 'b; | |
| 158 | type Reference<'b> | |
| 158 | 159 | = MatrixView<'b, E, M, K, Dyn, Dyn> |
| 156 | 160 | where |
| 161 | Matrix<E, M, K, S>: 'b; | |
| 162 | ||
| 163 | #[inline] | |
| 157 | 164 | fn lift<'b>(r: Self::Reference<'b>) -> Self::Decomposition<'b> |
| 165 | where | |
| 166 | S: 'b, | |
| 167 | { | |
| 156 | 168 | r.into_owned() |
| 169 | } | |
| 170 | } | |
| 171 | ||
| 159 | 172 | impl<S1, S2, M, K, E> Instance<Matrix<E, M, K, S1>, MatrixDecomposition> for Matrix<E, M, K, S2> |
| 173 | where | |
| 174 | S1: ViewableStorage<E, M, K>, | |
| 175 | S2: ViewableStorage<E, M, K>, | |
| 176 | M: Dim, | |
| 177 | K: Dim, | |
| 178 | E: Scalar + Zero + One, | |
| 179 | DefaultAllocator: Allocator<M, K>, | |
| 180 | ShapeConstraint: StridesOk<E, M, K, S1> + StridesOk<E, M, K, S2>, | |
| 181 | { | |
| 182 | fn eval_decompose<'b, R>(self, f: impl FnOnce(OMatrix<E, M, K>) -> R) -> R | |
| 183 | where | |
| 184 | Self: 'b, | |
| 185 | { | |
| 186 | f(self.into_owned()) | |
| 187 | } | |
| 158 | 188 | |
| 159 | 189 | fn eval_ref_decompose<'b, R>( |
| 190 | &'b self, | |
| 191 | f: impl FnOnce(<MatrixDecomposition as Decomposition<Matrix<E, M, K, S1>>>::Reference<'b>) -> R, | |
| 192 | ) -> R | |
| 193 | where | |
| 194 | Self: 'b, | |
| 195 | Matrix<E, M, K, S1>: 'b, | |
| 196 | { | |
| 197 | f(self.as_view::<M, K, Dyn, Dyn>()) | |
| 198 | } | |
| 156 | 199 | |
| 159 | 200 | #[inline] |
| 201 | fn own(self) -> OMatrix<E, M, K> { | |
| 202 | self.into_owned() | |
| 203 | } | |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
204 | } |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
205 | |
| 159 | 206 | impl<'a, S1, S2, M, K, E> Instance<Matrix<E, M, K, S1>, MatrixDecomposition> |
| 207 | for &'a Matrix<E, M, K, S2> | |
| 208 | where | |
| 209 | S1: ViewableStorage<E, M, K>, | |
| 210 | S2: ViewableStorage<E, M, K>, | |
| 211 | M: Dim, | |
| 212 | K: Dim, | |
| 213 | E: Scalar + Zero + One, | |
| 214 | DefaultAllocator: Allocator<M, K>, | |
| 215 | ShapeConstraint: StridesOk<E, M, K, S1> + StridesOk<E, M, K, S2>, | |
| 216 | { | |
| 217 | fn eval_decompose<'b, R>(self, f: impl FnOnce(OMatrix<E, M, K>) -> R) -> R | |
| 218 | where | |
| 219 | Self: 'b, | |
| 220 | { | |
| 221 | f(self.into_owned()) | |
| 222 | } | |
| 223 | ||
| 224 | fn eval_ref_decompose<'b, R>( | |
| 225 | &'b self, | |
| 226 | f: impl FnOnce(<MatrixDecomposition as Decomposition<Matrix<E, M, K, S1>>>::Reference<'b>) -> R, | |
| 227 | ) -> R | |
| 228 | where | |
| 229 | Self: 'b, | |
| 230 | Matrix<E, M, K, S1>: 'b, | |
| 231 | { | |
| 232 | f((*self).as_view::<M, K, Dyn, Dyn>()) | |
| 233 | } | |
| 234 | ||
| 235 | #[inline] | |
| 236 | fn own(self) -> OMatrix<E, M, K> { | |
| 237 | self.into_owned() | |
| 238 | } | |
| 239 | } | |
| 158 | 240 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
241 | impl<SM, SV, N, M, K, E> Mapping<Matrix<E, M, K, SV>> for Matrix<E, N, M, SM> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
242 | where |
| 159 | 243 | SM: ViewableStorage<E, N, M>, |
| 244 | SV: ViewableStorage<E, M, K> + Clone, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
245 | N: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
246 | M: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
247 | K: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
248 | E: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign, |
| 159 | 249 | DefaultAllocator: Allocator<N, K> + Allocator<M, K> + Allocator<N, M> + Allocator<M, N>, |
| 250 | ShapeConstraint: StridesOk<E, N, M, SM> + StridesOk<E, M, K, SV> + StridesOk<E, N, K>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
251 | { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
252 | type Codomain = OMatrix<E, N, K>; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
253 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
254 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
255 | fn apply<I: Instance<Matrix<E, M, K, SV>>>(&self, x: I) -> Self::Codomain { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
256 | x.either(|owned| self.mul(owned), |refr| self.mul(refr)) |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
257 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
258 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
259 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
260 | impl<'a, SM, SV, N, M, K, E> Linear<Matrix<E, M, K, SV>> for Matrix<E, N, M, SM> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
261 | where |
| 159 | 262 | SM: ViewableStorage<E, N, M>, |
| 263 | SV: ViewableStorage<E, M, K> + Clone, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
264 | N: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
265 | M: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
266 | K: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
267 | E: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign, |
| 159 | 268 | DefaultAllocator: Allocator<N, K> + Allocator<M, K> + Allocator<N, M> + Allocator<M, N>, |
| 269 | ShapeConstraint: StridesOk<E, N, M, SM> + StridesOk<E, M, K, SV> + StridesOk<E, N, K>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
270 | { |
| 0 | 271 | } |
| 272 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
273 | impl<SM, SV1, SV2, N, M, K, E> GEMV<E, Matrix<E, M, K, SV1>, Matrix<E, N, K, SV2>> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
274 | for Matrix<E, N, M, SM> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
275 | where |
| 159 | 276 | SM: ViewableStorage<E, N, M>, |
| 277 | SV1: ViewableStorage<E, M, K> + Clone, | |
| 278 | SV2: ViewableStorageMut<E, N, K>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
279 | N: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
280 | M: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
281 | K: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
282 | E: Scalar + Zero + One + Float, |
| 159 | 283 | DefaultAllocator: Allocator<N, K> + Allocator<M, K> + Allocator<N, M> + Allocator<M, N>, |
| 284 | ShapeConstraint: StridesOk<E, N, M, SM> + StridesOk<E, M, K, SV1> + StridesOk<E, N, K, SV2>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
285 | { |
| 0 | 286 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
287 | fn gemv<I: Instance<Matrix<E, M, K, SV1>>>( |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
288 | &self, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
289 | y: &mut Matrix<E, N, K, SV2>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
290 | α: E, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
291 | x: I, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
292 | β: E, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
293 | ) { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
294 | x.eval(|x̃| Matrix::gemm(y, α, self, x̃, β)) |
| 0 | 295 | } |
| 296 | ||
| 297 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
298 | fn apply_mut<'a, I: Instance<Matrix<E, M, K, SV1>>>(&self, y: &mut Matrix<E, N, K, SV2>, x: I) { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
299 | x.eval(|x̃| self.mul_to(x̃, y)) |
| 0 | 300 | } |
| 301 | } | |
| 302 | ||
| 150 | 303 | impl<S, M, N, E> VectorSpace for Matrix<E, M, N, S> |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
304 | where |
| 159 | 305 | S: ViewableStorage<E, M, N>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
306 | M: Dim, |
|
148
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
307 | N: Dim, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
308 | E: Scalar + Zero + One + Float, |
|
148
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
309 | DefaultAllocator: Allocator<M, N>, |
| 159 | 310 | ShapeConstraint: StridesOk<E, M, N, S>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
311 | { |
|
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
|
312 | type Field = E; |
|
148
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
313 | type Owned = OMatrix<E, M, N>; |
| 0 | 314 | |
| 315 | #[inline] | |
| 150 | 316 | fn similar_origin(&self) -> Self::Owned { |
| 317 | let (n, m) = self.shape_generic(); | |
| 318 | OMatrix::zeros_generic(n, m) | |
| 319 | } | |
| 320 | } | |
| 321 | ||
| 322 | impl<SM, SV1, M, N, E> AXPY<Matrix<E, M, N, SV1>> for Matrix<E, M, N, SM> | |
| 323 | where | |
| 159 | 324 | SM: ViewableStorageMut<E, M, N>, |
| 325 | SV1: ViewableStorage<E, M, N>, | |
| 150 | 326 | M: Dim, |
| 327 | N: Dim, | |
| 328 | E: Scalar + Zero + One + Float, | |
| 329 | DefaultAllocator: Allocator<M, N>, | |
| 159 | 330 | ShapeConstraint: StridesOk<E, M, N, SM> + StridesOk<E, M, N, SV1>, |
| 150 | 331 | { |
| 332 | #[inline] | |
|
148
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
333 | fn axpy<I: Instance<Matrix<E, M, N, SV1>>>(&mut self, α: E, x: I, β: E) { |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
334 | x.eval(|x̃| { |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
335 | assert_eq!(self.ncols(), x̃.ncols()); |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
336 | // nalgebra does not implement axpy for matrices, and flattenining |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
337 | // also seems difficult, so loop over columns. |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
338 | for (mut y, ỹ) in self.column_iter_mut().zip(x̃.column_iter()) { |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
339 | Vector::axpy(&mut y, α, &ỹ, β) |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
340 | } |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
341 | }) |
| 0 | 342 | } |
| 343 | ||
| 344 | #[inline] | |
|
148
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
345 | fn copy_from<I: Instance<Matrix<E, M, N, SV1>>>(&mut self, y: I) { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
346 | y.eval(|ỹ| Matrix::copy_from(self, ỹ)) |
| 0 | 347 | } |
|
62
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
348 | |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
349 | #[inline] |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
350 | fn set_zero(&mut self) { |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
351 | self.iter_mut().for_each(|e| *e = E::ZERO); |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
352 | } |
| 0 | 353 | } |
| 354 | ||
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
355 | /* Implemented automatically as Euclidean. |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
356 | impl<SM,M,E> Projection<E, L2> for Vector<E,M,SM> |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
357 | where SM: StorageMut<E,M> + Clone, |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
358 | M : Dim, E : Scalar + Zero + One + Float + RealField, |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
359 | DefaultAllocator : Allocator<M> { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
360 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
361 | fn proj_ball_mut(&mut self, ρ : E, _ : L2) { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
362 | let n = self.norm(L2); |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
363 | if n > ρ { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
364 | self.iter_mut().for_each(|v| *v *= ρ/n) |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
365 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
366 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
367 | }*/ |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
368 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
369 | impl<SM, M, E> Projection<E, Linfinity> for Vector<E, M, SM> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
370 | where |
| 159 | 371 | SM: ViewableStorageMut<E, M> + Clone, |
| 150 | 372 | M: Dim, |
| 373 | E: Scalar + Zero + One + Float + RealField, | |
| 374 | DefaultAllocator: Allocator<M>, | |
| 159 | 375 | ShapeConstraint: StridesOk<E, M, U1, SM>, |
| 150 | 376 | { |
| 377 | #[inline] | |
| 378 | fn proj_ball(self, ρ: E, exp: Linfinity) -> <Self as Space>::OwnedSpace { | |
| 379 | let mut owned = self.into_owned(); | |
| 380 | owned.proj_ball_mut(ρ, exp); | |
| 381 | owned | |
| 382 | } | |
| 383 | } | |
| 384 | ||
| 385 | impl<SM, M, E> ProjectionMut<E, Linfinity> for Vector<E, M, SM> | |
| 386 | where | |
| 159 | 387 | SM: ViewableStorageMut<E, M> + Clone, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
388 | M: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
389 | E: Scalar + Zero + One + Float + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
390 | DefaultAllocator: Allocator<M>, |
| 159 | 391 | ShapeConstraint: StridesOk<E, M, U1, SM>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
392 | { |
| 0 | 393 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
394 | fn proj_ball_mut(&mut self, ρ: E, _: Linfinity) { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
395 | self.iter_mut() |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
396 | .for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ)) |
| 0 | 397 | } |
| 398 | } | |
| 399 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
400 | impl<'own, SV1, SV2, SM, N, M, K, E> Adjointable<Matrix<E, M, K, SV1>, Matrix<E, N, K, SV2>> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
401 | for Matrix<E, N, M, SM> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
402 | where |
| 159 | 403 | SM: ViewableStorage<E, N, M>, |
| 404 | SV1: ViewableStorage<E, M, K> + Clone, | |
| 405 | SV2: ViewableStorage<E, N, K> + Clone, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
406 | N: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
407 | M: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
408 | K: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
409 | E: Scalar + Zero + One + SimdComplexField, |
| 159 | 410 | DefaultAllocator: Allocator<N, K> + Allocator<M, K> + Allocator<N, M> + Allocator<M, N>, |
| 411 | ShapeConstraint: StridesOk<E, N, M, SM> | |
| 412 | + StridesOk<E, M, K, SV1> | |
| 413 | + StridesOk<E, N, K, SV2> | |
| 414 | + StridesOk<E, M, N>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
415 | { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
416 | type AdjointCodomain = OMatrix<E, M, K>; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
417 | type Adjoint<'a> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
418 | = OMatrix<E, M, N> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
419 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
420 | SM: 'a; |
| 0 | 421 | |
| 422 | #[inline] | |
| 423 | fn adjoint(&self) -> Self::Adjoint<'_> { | |
| 424 | Matrix::adjoint(self) | |
| 425 | } | |
| 426 | } | |
| 427 | ||
| 428 | /// This function is [`nalgebra::EuclideanNorm::metric_distance`] without the `sqrt`. | |
| 429 | #[inline] | |
| 430 | fn metric_distance_squared<T, R1, C1, S1, R2, C2, S2>( | |
| 431 | /*ed: &EuclideanNorm,*/ | |
| 432 | m1: &Matrix<T, R1, C1, S1>, | |
| 433 | m2: &Matrix<T, R2, C2, S2>, | |
| 434 | ) -> T::SimdRealField | |
| 435 | where | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
436 | T: SimdComplexField, |
| 0 | 437 | R1: Dim, |
| 438 | C1: Dim, | |
| 439 | S1: Storage<T, R1, C1>, | |
| 440 | R2: Dim, | |
| 441 | C2: Dim, | |
| 442 | S2: Storage<T, R2, C2>, | |
| 443 | ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>, | |
| 444 | { | |
| 445 | m1.zip_fold(m2, T::SimdRealField::zero(), |acc, a, b| { | |
| 446 | let diff = a - b; | |
| 447 | acc + diff.simd_modulus_squared() | |
| 448 | }) | |
| 449 | } | |
| 450 | ||
| 451 | // TODO: should allow different input storages in `Euclidean`. | |
| 452 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
453 | impl<E, M, S> Euclidean<E> for Vector<E, M, S> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
454 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
455 | M: Dim, |
| 159 | 456 | S: ViewableStorage<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
457 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
458 | DefaultAllocator: Allocator<M>, |
| 159 | 459 | ShapeConstraint: StridesOk<E, M, U1, S>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
460 | { |
| 151 | 461 | type OwnedEuclidean = OVector<E, M>; |
| 462 | ||
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
463 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
464 | fn dot<I: Instance<Self>>(&self, other: I) -> E { |
| 158 | 465 | other.eval_ref_decompose(|ref r| Vector::<E, M, S>::dot(self, r)) |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
466 | } |
|
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
467 | |
| 0 | 468 | #[inline] |
| 469 | fn norm2_squared(&self) -> E { | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
470 | Vector::<E, M, S>::norm_squared(self) |
| 0 | 471 | } |
| 472 | ||
| 473 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
474 | fn dist2_squared<I: Instance<Self>>(&self, other: I) -> E { |
| 158 | 475 | other.eval_ref_decompose(|ref r| metric_distance_squared(self, r)) |
| 0 | 476 | } |
| 477 | } | |
| 478 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
479 | impl<E, M, S> StaticEuclidean<E> for Vector<E, M, S> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
480 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
481 | M: DimName, |
| 159 | 482 | S: ViewableStorage<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
483 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
484 | DefaultAllocator: Allocator<M>, |
| 159 | 485 | ShapeConstraint: StridesOk<E, M, U1, S>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
486 | { |
| 0 | 487 | #[inline] |
| 488 | fn origin() -> OVector<E, M> { | |
| 489 | OVector::zeros() | |
| 490 | } | |
| 491 | } | |
| 492 | ||
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
493 | /// The default norm for `Vector` is [`L2`]. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
494 | impl<E, M, S> Normed<E> for Vector<E, M, S> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
495 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
496 | M: Dim, |
| 159 | 497 | S: ViewableStorage<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
498 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
499 | DefaultAllocator: Allocator<M>, |
| 159 | 500 | ShapeConstraint: StridesOk<E, M, U1, S>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
501 | { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
502 | type NormExp = L2; |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
503 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
504 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
505 | fn norm_exponent(&self) -> Self::NormExp { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
506 | L2 |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
507 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
508 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
509 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
510 | fn is_zero(&self) -> bool { |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
511 | Vector::<E, M, S>::norm_squared(self) == E::ZERO |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
512 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
513 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
514 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
515 | impl<E, M, S> HasDual<E> for Vector<E, M, S> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
516 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
517 | M: Dim, |
| 159 | 518 | S: ViewableStorage<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
519 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
520 | DefaultAllocator: Allocator<M>, |
| 159 | 521 | ShapeConstraint: StridesOk<E, M, U1, S>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
522 | { |
| 151 | 523 | type DualSpace = OVector<E, M>; |
| 138 | 524 | |
| 525 | fn dual_origin(&self) -> OVector<E, M> { | |
| 526 | OVector::zeros_generic(M::from_usize(self.len()), Const) | |
| 527 | } | |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
528 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
529 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
530 | impl<E, M, S> Norm<L1, E> for Vector<E, M, S> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
531 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
532 | M: Dim, |
| 159 | 533 | S: ViewableStorage<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
534 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
535 | DefaultAllocator: Allocator<M>, |
| 159 | 536 | ShapeConstraint: StridesOk<E, M, U1, S>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
537 | { |
| 0 | 538 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
539 | fn norm(&self, _: L1) -> E { |
| 70 | 540 | nalgebra::Norm::norm(&LpNorm(1), self) |
| 0 | 541 | } |
| 542 | } | |
| 543 | ||
|
145
0b9aecd7bb76
Dist argument order changed to reflect other changes
Tuomo Valkonen <tuomov@iki.fi>
parents:
138
diff
changeset
|
544 | impl<E, M, S> Dist<L1, E> for Vector<E, M, S> |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
545 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
546 | M: Dim, |
| 159 | 547 | S: ViewableStorage<E, M> + Clone, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
548 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
549 | DefaultAllocator: Allocator<M>, |
| 159 | 550 | ShapeConstraint: StridesOk<E, M, U1, S>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
551 | { |
| 0 | 552 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
553 | fn dist<I: Instance<Self>>(&self, other: I, _: L1) -> E { |
| 158 | 554 | other.eval_ref_decompose(|ref r| nalgebra::Norm::metric_distance(&LpNorm(1), self, r)) |
| 0 | 555 | } |
| 556 | } | |
| 557 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
558 | impl<E, M, S> Norm<L2, E> for Vector<E, M, S> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
559 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
560 | M: Dim, |
| 159 | 561 | S: ViewableStorage<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
562 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
563 | DefaultAllocator: Allocator<M>, |
| 159 | 564 | ShapeConstraint: StridesOk<E, M, U1, S>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
565 | { |
| 0 | 566 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
567 | fn norm(&self, _: L2) -> E { |
| 70 | 568 | nalgebra::Norm::norm(&LpNorm(2), self) |
| 0 | 569 | } |
| 570 | } | |
| 571 | ||
|
145
0b9aecd7bb76
Dist argument order changed to reflect other changes
Tuomo Valkonen <tuomov@iki.fi>
parents:
138
diff
changeset
|
572 | impl<E, M, S> Dist<L2, E> for Vector<E, M, S> |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
573 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
574 | M: Dim, |
| 159 | 575 | S: ViewableStorage<E, M> + Clone, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
576 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
577 | DefaultAllocator: Allocator<M>, |
| 159 | 578 | ShapeConstraint: StridesOk<E, M, U1, S>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
579 | { |
| 0 | 580 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
581 | fn dist<I: Instance<Self>>(&self, other: I, _: L2) -> E { |
| 158 | 582 | other.eval_ref_decompose(|ref r| nalgebra::Norm::metric_distance(&LpNorm(2), self, r)) |
| 0 | 583 | } |
| 584 | } | |
| 585 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
586 | impl<E, M, S> Norm<Linfinity, E> for Vector<E, M, S> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
587 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
588 | M: Dim, |
| 159 | 589 | S: ViewableStorage<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
590 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
591 | DefaultAllocator: Allocator<M>, |
| 159 | 592 | ShapeConstraint: StridesOk<E, M, U1, S>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
593 | { |
| 0 | 594 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
595 | fn norm(&self, _: Linfinity) -> E { |
| 70 | 596 | nalgebra::Norm::norm(&UniformNorm, self) |
| 0 | 597 | } |
| 598 | } | |
| 599 | ||
|
145
0b9aecd7bb76
Dist argument order changed to reflect other changes
Tuomo Valkonen <tuomov@iki.fi>
parents:
138
diff
changeset
|
600 | impl<E, M, S> Dist<Linfinity, E> for Vector<E, M, S> |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
601 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
602 | M: Dim, |
| 159 | 603 | S: ViewableStorage<E, M> + Clone, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
604 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
605 | DefaultAllocator: Allocator<M>, |
| 159 | 606 | ShapeConstraint: StridesOk<E, M, U1, S>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
607 | { |
| 0 | 608 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
609 | fn dist<I: Instance<Self>>(&self, other: I, _: Linfinity) -> E { |
| 158 | 610 | other.eval_ref_decompose(|ref r| nalgebra::Norm::metric_distance(&UniformNorm, self, r)) |
| 0 | 611 | } |
| 612 | } | |
| 613 | ||
| 5 | 614 | /// Helper trait to hide the symbols of [`nalgebra::RealField`]. |
| 615 | /// | |
| 616 | /// By assuming `ToNalgebraRealField` intead of `nalgebra::RealField` as a trait bound, | |
| 617 | /// functions can piggyback `nalgebra::RealField` without exponsing themselves to it. | |
| 618 | /// Thus methods from [`num_traits`] can be used directly without similarly named methods | |
| 619 | /// from [`nalgebra`] conflicting with them. Only when absolutely necessary to work with | |
| 620 | /// nalgebra, one can convert to the nalgebra view of the same type using the methods of | |
| 621 | /// this trait. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
622 | pub trait ToNalgebraRealField: Float { |
| 5 | 623 | /// The nalgebra type corresponding to this type. Usually same as `Self`. |
| 624 | /// | |
| 625 | /// This type only carries `nalgebra` traits. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
626 | type NalgebraType: RealField; |
| 5 | 627 | /// The “mixed” type corresponding to this type. Usually same as `Self`. |
| 628 | /// | |
| 629 | /// This type carries both `num_traits` and `nalgebra` traits. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
630 | type MixedType: RealField + Float; |
| 0 | 631 | |
| 5 | 632 | /// Convert to the nalgebra view of `self`. |
| 0 | 633 | fn to_nalgebra(self) -> Self::NalgebraType; |
| 5 | 634 | |
| 635 | /// Convert to the mixed (nalgebra and num_traits) view of `self`. | |
| 0 | 636 | fn to_nalgebra_mixed(self) -> Self::MixedType; |
| 637 | ||
| 5 | 638 | /// Convert from the nalgebra view of `self`. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
639 | fn from_nalgebra(t: Self::NalgebraType) -> Self; |
| 5 | 640 | |
| 641 | /// Convert from the mixed (nalgebra and num_traits) view to `self`. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
642 | fn from_nalgebra_mixed(t: Self::MixedType) -> Self; |
| 0 | 643 | } |
| 644 | ||
| 645 | impl ToNalgebraRealField for f32 { | |
| 646 | type NalgebraType = f32; | |
| 647 | type MixedType = f32; | |
| 648 | ||
| 649 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
650 | fn to_nalgebra(self) -> Self::NalgebraType { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
651 | self |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
652 | } |
| 0 | 653 | |
| 654 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
655 | fn to_nalgebra_mixed(self) -> Self::MixedType { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
656 | self |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
657 | } |
| 0 | 658 | |
| 659 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
660 | fn from_nalgebra(t: Self::NalgebraType) -> Self { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
661 | t |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
662 | } |
| 0 | 663 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
664 | #[inline] |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
665 | fn from_nalgebra_mixed(t: Self::MixedType) -> Self { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
666 | t |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
667 | } |
| 0 | 668 | } |
| 669 | ||
| 670 | impl ToNalgebraRealField for f64 { | |
| 671 | type NalgebraType = f64; | |
| 672 | type MixedType = f64; | |
| 673 | ||
| 674 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
675 | fn to_nalgebra(self) -> Self::NalgebraType { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
676 | self |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
677 | } |
| 0 | 678 | |
| 679 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
680 | fn to_nalgebra_mixed(self) -> Self::MixedType { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
681 | self |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
682 | } |
| 0 | 683 | |
| 684 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
685 | fn from_nalgebra(t: Self::NalgebraType) -> Self { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
686 | t |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
687 | } |
| 0 | 688 | |
| 689 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
690 | fn from_nalgebra_mixed(t: Self::MixedType) -> Self { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
691 | t |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
692 | } |
| 0 | 693 | } |