Fri, 05 Sep 2025 00:48:59 -0500
AXPY implementation reductions
| 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 | ||
|
177
b071a1b484f8
AXPY implementation reductions
Tuomo Valkonen <tuomov@iki.fi>
parents:
176
diff
changeset
|
362 | // This can only be implemented for the “principal” OMatrix as parameter, as otherwise |
|
b071a1b484f8
AXPY implementation reductions
Tuomo Valkonen <tuomov@iki.fi>
parents:
176
diff
changeset
|
363 | // we run into problems of multiple implementations when calling the methods. |
|
b071a1b484f8
AXPY implementation reductions
Tuomo Valkonen <tuomov@iki.fi>
parents:
176
diff
changeset
|
364 | impl<M, N, E, S> AXPY<OMatrix<E, M, N>> for Matrix<E, M, N, S> |
| 150 | 365 | where |
|
177
b071a1b484f8
AXPY implementation reductions
Tuomo Valkonen <tuomov@iki.fi>
parents:
176
diff
changeset
|
366 | S: StorageMut<E, M, N>, |
| 150 | 367 | M: Dim, |
| 368 | N: Dim, | |
| 369 | E: Scalar + Zero + One + Float, | |
| 370 | DefaultAllocator: Allocator<M, N>, | |
| 176 | 371 | ShapeConstraint: StridesOk<E, M, N>, |
| 150 | 372 | { |
| 373 | #[inline] | |
|
177
b071a1b484f8
AXPY implementation reductions
Tuomo Valkonen <tuomov@iki.fi>
parents:
176
diff
changeset
|
374 | fn axpy<I: Instance<OMatrix<E, M, N>>>(&mut self, α: E, x: I, β: E) { |
|
148
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
375 | x.eval(|x̃| { |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
376 | assert_eq!(self.ncols(), x̃.ncols()); |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
377 | // 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
|
378 | // also seems difficult, so loop over columns. |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
379 | 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
|
380 | Vector::axpy(&mut y, α, &ỹ, β) |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
381 | } |
|
26ef556870fd
AXPY for nalgebra matrices, not just vectors
Tuomo Valkonen <tuomov@iki.fi>
parents:
145
diff
changeset
|
382 | }) |
| 0 | 383 | } |
| 384 | ||
| 385 | #[inline] | |
|
177
b071a1b484f8
AXPY implementation reductions
Tuomo Valkonen <tuomov@iki.fi>
parents:
176
diff
changeset
|
386 | fn copy_from<I: Instance<OMatrix<E, M, N>>>(&mut self, y: I) { |
| 171 | 387 | y.eval_ref(|ỹ| Matrix::copy_from(self, &ỹ)) |
| 0 | 388 | } |
|
62
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
389 | |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
390 | #[inline] |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
391 | fn set_zero(&mut self) { |
|
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
60
diff
changeset
|
392 | 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
|
393 | } |
| 0 | 394 | } |
| 395 | ||
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
396 | /* Implemented automatically as Euclidean. |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
397 | 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
|
398 | where SM: StorageMut<E,M> + Clone, |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
399 | M : Dim, E : Scalar + Zero + One + Float + RealField, |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
400 | DefaultAllocator : Allocator<M> { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
401 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
402 | fn proj_ball_mut(&mut self, ρ : E, _ : L2) { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
403 | let n = self.norm(L2); |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
404 | if n > ρ { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
405 | self.iter_mut().for_each(|v| *v *= ρ/n) |
|
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 | }*/ |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
409 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
410 | 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
|
411 | where |
| 174 | 412 | SM: StorageMut<E, M>, |
| 150 | 413 | M: Dim, |
| 414 | E: Scalar + Zero + One + Float + RealField, | |
| 415 | DefaultAllocator: Allocator<M>, | |
| 176 | 416 | ShapeConstraint: StridesOk<E, M>, |
| 150 | 417 | { |
| 418 | #[inline] | |
| 164 | 419 | fn proj_ball(self, ρ: E, exp: Linfinity) -> <Self as Space>::Principal { |
| 150 | 420 | let mut owned = self.into_owned(); |
| 421 | owned.proj_ball_mut(ρ, exp); | |
| 422 | owned | |
| 423 | } | |
| 424 | } | |
| 425 | ||
| 426 | impl<SM, M, E> ProjectionMut<E, Linfinity> for Vector<E, M, SM> | |
| 427 | where | |
| 174 | 428 | SM: StorageMut<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
429 | M: Dim, |
| 165 | 430 | 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
|
431 | DefaultAllocator: Allocator<M>, |
| 176 | 432 | ShapeConstraint: StridesOk<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
433 | { |
| 0 | 434 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
435 | 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
|
436 | self.iter_mut() |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
437 | .for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ)) |
| 0 | 438 | } |
| 439 | } | |
| 440 | ||
|
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
|
441 | 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
|
442 | where |
|
160
e7920e205785
Viewable hack not needed now
Tuomo Valkonen <tuomov@iki.fi>
parents:
159
diff
changeset
|
443 | SM: Storage<E, N, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
444 | N: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
445 | M: Dim, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
446 | K: Dim, |
| 165 | 447 | E: Scalar + Zero + One + Copy + SimdComplexField, |
| 159 | 448 | DefaultAllocator: Allocator<N, K> + Allocator<M, K> + Allocator<N, M> + Allocator<M, N>, |
| 176 | 449 | 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
|
450 | { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
451 | type AdjointCodomain = OMatrix<E, M, K>; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
452 | type Adjoint<'a> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
453 | = OMatrix<E, M, N> |
|
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 | SM: 'a; |
| 0 | 456 | |
| 457 | #[inline] | |
| 458 | fn adjoint(&self) -> Self::Adjoint<'_> { | |
| 459 | Matrix::adjoint(self) | |
| 460 | } | |
| 461 | } | |
| 462 | ||
| 463 | /// This function is [`nalgebra::EuclideanNorm::metric_distance`] without the `sqrt`. | |
| 464 | #[inline] | |
| 465 | fn metric_distance_squared<T, R1, C1, S1, R2, C2, S2>( | |
| 466 | /*ed: &EuclideanNorm,*/ | |
| 467 | m1: &Matrix<T, R1, C1, S1>, | |
| 468 | m2: &Matrix<T, R2, C2, S2>, | |
| 469 | ) -> T::SimdRealField | |
| 470 | where | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
471 | T: SimdComplexField, |
| 0 | 472 | R1: Dim, |
| 473 | C1: Dim, | |
| 474 | S1: Storage<T, R1, C1>, | |
| 475 | R2: Dim, | |
| 476 | C2: Dim, | |
| 477 | S2: Storage<T, R2, C2>, | |
| 478 | ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>, | |
| 479 | { | |
| 480 | m1.zip_fold(m2, T::SimdRealField::zero(), |acc, a, b| { | |
| 481 | let diff = a - b; | |
| 482 | acc + diff.simd_modulus_squared() | |
| 483 | }) | |
| 484 | } | |
| 485 | ||
| 174 | 486 | 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
|
487 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
488 | M: Dim, |
| 174 | 489 | N: Dim, |
| 490 | S: Storage<E, M, N>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
491 | E: Float + Scalar + Zero + One + RealField, |
| 174 | 492 | DefaultAllocator: Allocator<M, N>, |
| 176 | 493 | ShapeConstraint: StridesOk<E, M, N>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
494 | { |
| 174 | 495 | type PrincipalE = OMatrix<E, M, N>; |
| 151 | 496 | |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
497 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
498 | fn dot<I: Instance<Self>>(&self, other: I) -> E { |
| 174 | 499 | 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
|
500 | } |
|
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
62
diff
changeset
|
501 | |
| 0 | 502 | #[inline] |
| 503 | fn norm2_squared(&self) -> E { | |
| 174 | 504 | Matrix::<E, M, N, S>::norm_squared(self) |
| 0 | 505 | } |
| 506 | ||
| 507 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
508 | fn dist2_squared<I: Instance<Self>>(&self, other: I) -> E { |
| 171 | 509 | other.eval_ref(|ref r| metric_distance_squared(self, r)) |
| 0 | 510 | } |
| 511 | } | |
| 512 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
513 | 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
|
514 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
515 | M: DimName, |
|
160
e7920e205785
Viewable hack not needed now
Tuomo Valkonen <tuomov@iki.fi>
parents:
159
diff
changeset
|
516 | S: Storage<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
517 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
518 | DefaultAllocator: Allocator<M>, |
| 176 | 519 | ShapeConstraint: StridesOk<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
520 | { |
| 0 | 521 | #[inline] |
| 522 | fn origin() -> OVector<E, M> { | |
| 523 | OVector::zeros() | |
| 524 | } | |
| 525 | } | |
| 526 | ||
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
527 | /// The default norm for `Vector` is [`L2`]. |
| 174 | 528 | 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
|
529 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
530 | M: Dim, |
| 174 | 531 | N: Dim, |
| 532 | S: Storage<E, M, N>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
533 | E: Float + Scalar + Zero + One + RealField, |
| 174 | 534 | DefaultAllocator: Allocator<M, N>, |
| 176 | 535 | ShapeConstraint: StridesOk<E, M, N>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
536 | { |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
537 | type NormExp = L2; |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
538 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
539 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
540 | fn norm_exponent(&self) -> Self::NormExp { |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
541 | L2 |
|
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 | |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
544 | #[inline] |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
545 | fn is_zero(&self) -> bool { |
| 174 | 546 | 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
|
547 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
548 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
549 | |
| 174 | 550 | 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
|
551 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
552 | M: Dim, |
| 174 | 553 | N: Dim, |
| 554 | S: Storage<E, M, N>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
555 | E: Float + Scalar + Zero + One + RealField, |
| 174 | 556 | DefaultAllocator: Allocator<M, N>, |
| 176 | 557 | ShapeConstraint: StridesOk<E, M, N>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
558 | { |
| 174 | 559 | type DualSpace = OMatrix<E, M, N>; |
| 138 | 560 | |
| 174 | 561 | fn dual_origin(&self) -> OMatrix<E, M, N> { |
| 562 | let (m, n) = self.shape_generic(); | |
| 563 | OMatrix::zeros_generic(m, n) | |
| 138 | 564 | } |
|
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
565 | } |
|
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
566 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
567 | 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
|
568 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
569 | M: Dim, |
|
160
e7920e205785
Viewable hack not needed now
Tuomo Valkonen <tuomov@iki.fi>
parents:
159
diff
changeset
|
570 | S: Storage<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
571 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
572 | DefaultAllocator: Allocator<M>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
573 | { |
| 0 | 574 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
575 | fn norm(&self, _: L1) -> E { |
| 70 | 576 | nalgebra::Norm::norm(&LpNorm(1), self) |
| 0 | 577 | } |
| 578 | } | |
| 579 | ||
|
145
0b9aecd7bb76
Dist argument order changed to reflect other changes
Tuomo Valkonen <tuomov@iki.fi>
parents:
138
diff
changeset
|
580 | 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
|
581 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
582 | M: Dim, |
| 174 | 583 | S: Storage<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
584 | E: Float + Scalar + Zero + One + RealField, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
585 | DefaultAllocator: Allocator<M>, |
| 176 | 586 | ShapeConstraint: StridesOk<E, M>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
587 | { |
| 0 | 588 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
589 | fn dist<I: Instance<Self>>(&self, other: I, _: L1) -> E { |
| 171 | 590 | other.eval_ref(|ref r| nalgebra::Norm::metric_distance(&LpNorm(1), self, r)) |
| 0 | 591 | } |
| 592 | } | |
| 593 | ||
| 174 | 594 | 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
|
595 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
596 | M: Dim, |
| 174 | 597 | N: Dim, |
| 598 | S: Storage<E, M, N>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
599 | E: Float + Scalar + Zero + One + RealField, |
| 174 | 600 | DefaultAllocator: Allocator<M, N>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
601 | { |
| 0 | 602 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
603 | fn norm(&self, _: L2) -> E { |
| 70 | 604 | nalgebra::Norm::norm(&LpNorm(2), self) |
| 0 | 605 | } |
| 606 | } | |
| 607 | ||
| 174 | 608 | 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
|
609 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
610 | M: Dim, |
| 174 | 611 | N: Dim, |
| 612 | S: Storage<E, M, N>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
613 | E: Float + Scalar + Zero + One + RealField, |
| 174 | 614 | DefaultAllocator: Allocator<M, N>, |
| 176 | 615 | ShapeConstraint: StridesOk<E, M, N>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
616 | { |
| 0 | 617 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
618 | fn dist<I: Instance<Self>>(&self, other: I, _: L2) -> E { |
| 171 | 619 | other.eval_ref(|ref r| nalgebra::Norm::metric_distance(&LpNorm(2), self, r)) |
| 0 | 620 | } |
| 621 | } | |
| 622 | ||
| 174 | 623 | 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
|
624 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
625 | M: Dim, |
| 174 | 626 | N: Dim, |
| 627 | S: Storage<E, M, N>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
628 | E: Float + Scalar + Zero + One + RealField, |
| 174 | 629 | DefaultAllocator: Allocator<M, N>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
630 | { |
| 0 | 631 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
632 | fn norm(&self, _: Linfinity) -> E { |
| 70 | 633 | nalgebra::Norm::norm(&UniformNorm, self) |
| 0 | 634 | } |
| 635 | } | |
| 636 | ||
| 174 | 637 | 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
|
638 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
639 | M: Dim, |
| 174 | 640 | N: Dim, |
| 641 | S: Storage<E, M, N>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
642 | E: Float + Scalar + Zero + One + RealField, |
| 174 | 643 | DefaultAllocator: Allocator<M, N>, |
| 176 | 644 | ShapeConstraint: StridesOk<E, M, N>, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
645 | { |
| 0 | 646 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
647 | fn dist<I: Instance<Self>>(&self, other: I, _: Linfinity) -> E { |
| 171 | 648 | other.eval_ref(|ref r| nalgebra::Norm::metric_distance(&UniformNorm, self, r)) |
| 0 | 649 | } |
| 650 | } | |
| 651 | ||
| 5 | 652 | /// Helper trait to hide the symbols of [`nalgebra::RealField`]. |
| 653 | /// | |
| 654 | /// By assuming `ToNalgebraRealField` intead of `nalgebra::RealField` as a trait bound, | |
| 655 | /// functions can piggyback `nalgebra::RealField` without exponsing themselves to it. | |
| 656 | /// Thus methods from [`num_traits`] can be used directly without similarly named methods | |
| 657 | /// from [`nalgebra`] conflicting with them. Only when absolutely necessary to work with | |
| 658 | /// nalgebra, one can convert to the nalgebra view of the same type using the methods of | |
| 659 | /// this trait. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
660 | pub trait ToNalgebraRealField: Float { |
| 5 | 661 | /// The nalgebra type corresponding to this type. Usually same as `Self`. |
| 662 | /// | |
| 663 | /// This type only carries `nalgebra` traits. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
664 | type NalgebraType: RealField; |
| 5 | 665 | /// The “mixed” type corresponding to this type. Usually same as `Self`. |
| 666 | /// | |
| 667 | /// 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
|
668 | type MixedType: RealField + Float; |
| 0 | 669 | |
| 5 | 670 | /// Convert to the nalgebra view of `self`. |
| 0 | 671 | fn to_nalgebra(self) -> Self::NalgebraType; |
| 5 | 672 | |
| 673 | /// Convert to the mixed (nalgebra and num_traits) view of `self`. | |
| 0 | 674 | fn to_nalgebra_mixed(self) -> Self::MixedType; |
| 675 | ||
| 5 | 676 | /// 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
|
677 | fn from_nalgebra(t: Self::NalgebraType) -> Self; |
| 5 | 678 | |
| 679 | /// 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
|
680 | fn from_nalgebra_mixed(t: Self::MixedType) -> Self; |
| 0 | 681 | } |
| 682 | ||
| 683 | impl ToNalgebraRealField for f32 { | |
| 684 | type NalgebraType = f32; | |
| 685 | type MixedType = f32; | |
| 686 | ||
| 687 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
688 | fn to_nalgebra(self) -> Self::NalgebraType { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
689 | self |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
690 | } |
| 0 | 691 | |
| 692 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
693 | fn to_nalgebra_mixed(self) -> Self::MixedType { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
694 | self |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
695 | } |
| 0 | 696 | |
| 697 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
698 | fn from_nalgebra(t: Self::NalgebraType) -> Self { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
699 | t |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
700 | } |
| 0 | 701 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
702 | #[inline] |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
703 | 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
|
704 | t |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
705 | } |
| 0 | 706 | } |
| 707 | ||
| 708 | impl ToNalgebraRealField for f64 { | |
| 709 | type NalgebraType = f64; | |
| 710 | type MixedType = f64; | |
| 711 | ||
| 712 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
713 | fn to_nalgebra(self) -> Self::NalgebraType { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
714 | self |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
715 | } |
| 0 | 716 | |
| 717 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
718 | fn to_nalgebra_mixed(self) -> Self::MixedType { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
719 | self |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
720 | } |
| 0 | 721 | |
| 722 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
723 | fn from_nalgebra(t: Self::NalgebraType) -> Self { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
724 | t |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
725 | } |
| 0 | 726 | |
| 727 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
728 | 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
|
729 | t |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
70
diff
changeset
|
730 | } |
| 0 | 731 | } |