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