src/nalgebra_support.rs

branch
dev
changeset 159
279b1f5b8608
parent 158
9c720f822c79
child 160
e7920e205785
equal deleted inserted replaced
158:9c720f822c79 159:279b1f5b8608
17 use nalgebra::base::constraint::{DimEq, SameNumberOfColumns, SameNumberOfRows, ShapeConstraint}; 17 use nalgebra::base::constraint::{DimEq, SameNumberOfColumns, SameNumberOfRows, ShapeConstraint};
18 use nalgebra::base::dimension::*; 18 use nalgebra::base::dimension::*;
19 use nalgebra::{ 19 use nalgebra::{
20 ClosedAddAssign, ClosedMulAssign, DefaultAllocator, Dim, LpNorm, Matrix, MatrixView, OMatrix, 20 ClosedAddAssign, ClosedMulAssign, DefaultAllocator, Dim, LpNorm, Matrix, MatrixView, OMatrix,
21 OVector, RawStorage, RealField, Scalar, SimdComplexField, Storage, StorageMut, UniformNorm, 21 OVector, RawStorage, RealField, Scalar, SimdComplexField, Storage, StorageMut, UniformNorm,
22 Vector, 22 Vector, U1,
23 }; 23 };
24 use num_traits::identities::{One, Zero}; 24 use num_traits::identities::{One, Zero};
25 use std::ops::Mul; 25 use std::ops::Mul;
26 26
27 impl<S, M, N, E> Ownable for Matrix<E, M, N, S> 27 impl<S, M, N, E> Ownable for Matrix<E, M, N, S>
43 fn clone_owned(&self) -> Self::OwnedVariant { 43 fn clone_owned(&self) -> Self::OwnedVariant {
44 Matrix::clone_owned(self) 44 Matrix::clone_owned(self)
45 } 45 }
46 } 46 }
47 47
48 trait StridesOk<E, N, M = U1, S = <DefaultAllocator as Allocator<N, M>>::Buffer<E>>:
49 DimEq<Dyn, S::RStride>
50 + DimEq<Dyn, S::CStride>
51 + DimEq<Dyn, <<DefaultAllocator as Allocator<N, M>>::Buffer<E> as RawStorage<E, N, M>>::RStride>
52 + DimEq<Dyn, <<DefaultAllocator as Allocator<N, M>>::Buffer<E> as RawStorage<E, N, M>>::CStride>
53 where
54 S: RawStorage<E, N, M>,
55 E: Scalar,
56 N: Dim,
57 M: Dim,
58 DefaultAllocator: Allocator<N, M>,
59 {
60 }
61
62 impl<S, E, N, M> StridesOk<E, N, M, S> for ShapeConstraint
63 where
64 ShapeConstraint: DimEq<Dyn, S::RStride>
65 + DimEq<Dyn, S::CStride>
66 + DimEq<
67 Dyn,
68 <<DefaultAllocator as Allocator<N, M>>::Buffer<E> as RawStorage<E, N, M>>::RStride,
69 > + DimEq<
70 Dyn,
71 <<DefaultAllocator as Allocator<N, M>>::Buffer<E> as RawStorage<E, N, M>>::CStride,
72 >,
73 S: Storage<E, N, M>,
74 E: Scalar,
75 N: Dim,
76 M: Dim,
77 DefaultAllocator: Allocator<N, M>,
78 {
79 }
80
48 impl<SM, N, M, E> Space for Matrix<E, N, M, SM> 81 impl<SM, N, M, E> Space for Matrix<E, N, M, SM>
49 where 82 where
50 SM: Storage<E, N, M>, 83 SM: Storage<E, N, M>,
51 N: Dim, 84 N: Dim,
52 M: Dim, 85 M: Dim,
53 E: Scalar + Zero + One, 86 E: Scalar + Zero + One,
54 DefaultAllocator: Allocator<N, M>, 87 DefaultAllocator: Allocator<N, M>,
88 ShapeConstraint: StridesOk<E, N, M, SM> + StridesOk<E, N, M>,
55 { 89 {
56 type OwnedSpace = OMatrix<E, N, M>; 90 type OwnedSpace = OMatrix<E, N, M>;
57 type Decomp = MatrixDecomposition; 91 type Decomp = MatrixDecomposition;
58 } 92 }
59 93
60 #[derive(Copy, Clone, Debug)] 94 #[derive(Copy, Clone, Debug)]
61 pub struct MatrixDecomposition; 95 pub struct MatrixDecomposition;
62 96
97 /// nalgebra [`Storage`] that's convertible to dynamic strides (practically all, but we need this
98 /// to work around the type system).
99 trait ViewableStorage<E, M, K = U1>: Storage<E, M, K>
100 where
101 M: Dim,
102 K: Dim,
103 E: Scalar,
104 ShapeConstraint: StridesOk<E, M, K, Self>,
105 DefaultAllocator: Allocator<M, K>,
106 {
107 }
108
109 impl<M, K, E, S> ViewableStorage<E, M, K> for S
110 where
111 S: Storage<E, M, K>,
112 M: Dim,
113 K: Dim,
114 E: Scalar,
115 ShapeConstraint: StridesOk<E, M, K, Self>,
116 DefaultAllocator: Allocator<M, K>,
117 {
118 }
119
120 /// nalgebra [`StorageMut`] that's convertible to dynamic strides (practically all, but we need this
121 /// to work around the type system).
122 trait ViewableStorageMut<E, M, K = U1>: StorageMut<E, M, K>
123 where
124 M: Dim,
125 K: Dim,
126 E: Scalar,
127 ShapeConstraint: StridesOk<E, M, K, Self>,
128 DefaultAllocator: Allocator<M, K>,
129 {
130 }
131
132 impl<M, K, E, S> ViewableStorageMut<E, M, K> for S
133 where
134 S: StorageMut<E, M, K>,
135 M: Dim,
136 K: Dim,
137 E: Scalar,
138 ShapeConstraint: StridesOk<E, M, K, Self>,
139 DefaultAllocator: Allocator<M, K>,
140 {
141 }
142
63 impl<E, M, K, S> Decomposition<Matrix<E, M, K, S>> for MatrixDecomposition 143 impl<E, M, K, S> Decomposition<Matrix<E, M, K, S>> for MatrixDecomposition
64 where 144 where
65 S: Storage<E, M, K>, 145 S: ViewableStorage<E, M, K>,
66 M: Dim, 146 M: Dim,
67 K: Dim, 147 K: Dim,
68 E: Scalar + Zero + One, 148 E: Scalar + Zero + One,
69 DefaultAllocator: Allocator<M, K>, 149 DefaultAllocator: Allocator<M, K>,
150 ShapeConstraint: StridesOk<E, M, K, S> + StridesOk<E, M, K>,
70 { 151 {
71 type OwnedInstance = OMatrix<E, M, K>; 152 type OwnedInstance = OMatrix<E, M, K>;
72 153
73 type Decomposition<'b> 154 type Decomposition<'b>
74 = OMatrix<E, M, K> 155 = OMatrix<E, M, K>
86 { 167 {
87 r.into_owned() 168 r.into_owned()
88 } 169 }
89 } 170 }
90 171
91 macro_rules! impl_instances { 172 impl<S1, S2, M, K, E> Instance<Matrix<E, M, K, S1>, MatrixDecomposition> for Matrix<E, M, K, S2>
92 ($rstride:ty, $cstride:ty where $($qual:tt)*) => { 173 where
93 impl<$($qual)* S1, S2, M, K, E> Instance<Matrix<E, M, K, S1>, MatrixDecomposition> for Matrix<E, M, K, S2> 174 S1: ViewableStorage<E, M, K>,
94 where 175 S2: ViewableStorage<E, M, K>,
95 S1: Storage<E, M, K>, 176 M: Dim,
96 S2: RawStorage<E, M, K, RStride=$rstride, CStride=$cstride>, 177 K: Dim,
97 //ShapeConstraint: DimEq<Dyn, <S2 as RawStorage<E, M, K>>::RStride> 178 E: Scalar + Zero + One,
98 // + DimEq<Dyn, <S2 as RawStorage<E, M, K>>::CStride>, 179 DefaultAllocator: Allocator<M, K>,
99 M: Dim, 180 ShapeConstraint: StridesOk<E, M, K, S1> + StridesOk<E, M, K, S2>,
100 K: Dim, 181 {
101 E: Scalar + Zero + One, 182 fn eval_decompose<'b, R>(self, f: impl FnOnce(OMatrix<E, M, K>) -> R) -> R
102 DefaultAllocator: Allocator<M, K>, 183 where
103 { 184 Self: 'b,
104 fn eval_decompose<'b, R>(self, f: impl FnOnce(OMatrix<E, M, K>) -> R) -> R 185 {
105 where 186 f(self.into_owned())
106 Self: 'b, 187 }
107 { 188
108 f(self.into_owned()) 189 fn eval_ref_decompose<'b, R>(
109 } 190 &'b self,
110 191 f: impl FnOnce(<MatrixDecomposition as Decomposition<Matrix<E, M, K, S1>>>::Reference<'b>) -> R,
111 fn eval_ref_decompose<'b, R>( 192 ) -> R
112 &'b self, 193 where
113 f: impl FnOnce(<MatrixDecomposition as Decomposition<Matrix<E, M, K, S1>>>::Reference<'b>) -> R, 194 Self: 'b,
114 ) -> R 195 Matrix<E, M, K, S1>: 'b,
115 where 196 {
116 Self: 'b, 197 f(self.as_view::<M, K, Dyn, Dyn>())
117 Matrix<E, M, K, S1>: 'b, 198 }
118 { 199
119 f(self.as_view::<M, K, Dyn, Dyn>()) 200 #[inline]
120 } 201 fn own(self) -> OMatrix<E, M, K> {
121 202 self.into_owned()
122 #[inline] 203 }
123 fn own(self) -> OMatrix<E, M, K> { 204 }
124 self.into_owned() 205
125 } 206 impl<'a, S1, S2, M, K, E> Instance<Matrix<E, M, K, S1>, MatrixDecomposition>
126 } 207 for &'a Matrix<E, M, K, S2>
127 208 where
128 impl<'a, $($qual)* S1, S2, M, K, E> Instance<Matrix<E, M, K, S1>, MatrixDecomposition> 209 S1: ViewableStorage<E, M, K>,
129 for &'a Matrix<E, M, K, S2> 210 S2: ViewableStorage<E, M, K>,
130 where 211 M: Dim,
131 S1: Storage<E, M, K>, 212 K: Dim,
132 S2: RawStorage<E, M, K, RStride=$rstride, CStride=$cstride>, 213 E: Scalar + Zero + One,
133 M: Dim, 214 DefaultAllocator: Allocator<M, K>,
134 K: Dim, 215 ShapeConstraint: StridesOk<E, M, K, S1> + StridesOk<E, M, K, S2>,
135 E: Scalar + Zero + One, 216 {
136 DefaultAllocator: Allocator<M, K>, 217 fn eval_decompose<'b, R>(self, f: impl FnOnce(OMatrix<E, M, K>) -> R) -> R
137 { 218 where
138 fn eval_decompose<'b, R>(self, f: impl FnOnce(OMatrix<E, M, K>) -> R) -> R 219 Self: 'b,
139 where 220 {
140 Self: 'b, 221 f(self.into_owned())
141 { 222 }
142 f(self.into_owned()) 223
143 } 224 fn eval_ref_decompose<'b, R>(
144 225 &'b self,
145 fn eval_ref_decompose<'b, R>( 226 f: impl FnOnce(<MatrixDecomposition as Decomposition<Matrix<E, M, K, S1>>>::Reference<'b>) -> R,
146 &'b self, 227 ) -> R
147 f: impl FnOnce(<MatrixDecomposition as Decomposition<Matrix<E, M, K, S1>>>::Reference<'b>) -> R, 228 where
148 ) -> R 229 Self: 'b,
149 where 230 Matrix<E, M, K, S1>: 'b,
150 Self: 'b, 231 {
151 Matrix<E, M, K, S1>: 'b, 232 f((*self).as_view::<M, K, Dyn, Dyn>())
152 { 233 }
153 f((*self).as_view::<M, K, Dyn, Dyn>()) 234
154 } 235 #[inline]
155 236 fn own(self) -> OMatrix<E, M, K> {
156 #[inline] 237 self.into_owned()
157 fn own(self) -> OMatrix<E, M, K> { 238 }
158 self.into_owned() 239 }
159 }
160 }
161 };
162 }
163
164 impl_instances!(Dyn, Dyn where );
165 impl_instances!(Const<RS>, Dyn where const RS : usize,);
166 impl_instances!(Dyn, Const<CS> where const CS : usize,);
167 impl_instances!(Const<RS>, Const<CS> where const RS : usize, const CS : usize,);
168 240
169 impl<SM, SV, N, M, K, E> Mapping<Matrix<E, M, K, SV>> for Matrix<E, N, M, SM> 241 impl<SM, SV, N, M, K, E> Mapping<Matrix<E, M, K, SV>> for Matrix<E, N, M, SM>
170 where 242 where
171 SM: Storage<E, N, M>, 243 SM: ViewableStorage<E, N, M>,
172 SV: Storage<E, M, K> + Clone, 244 SV: ViewableStorage<E, M, K> + Clone,
173 N: Dim, 245 N: Dim,
174 M: Dim, 246 M: Dim,
175 K: Dim, 247 K: Dim,
176 E: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign, 248 E: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
177 DefaultAllocator: Allocator<N, K>, 249 DefaultAllocator: Allocator<N, K> + Allocator<M, K> + Allocator<N, M> + Allocator<M, N>,
178 DefaultAllocator: Allocator<M, K>, 250 ShapeConstraint: StridesOk<E, N, M, SM> + StridesOk<E, M, K, SV> + StridesOk<E, N, K>,
179 DefaultAllocator: Allocator<N, M>,
180 DefaultAllocator: Allocator<M, N>,
181 { 251 {
182 type Codomain = OMatrix<E, N, K>; 252 type Codomain = OMatrix<E, N, K>;
183 253
184 #[inline] 254 #[inline]
185 fn apply<I: Instance<Matrix<E, M, K, SV>>>(&self, x: I) -> Self::Codomain { 255 fn apply<I: Instance<Matrix<E, M, K, SV>>>(&self, x: I) -> Self::Codomain {
187 } 257 }
188 } 258 }
189 259
190 impl<'a, SM, SV, N, M, K, E> Linear<Matrix<E, M, K, SV>> for Matrix<E, N, M, SM> 260 impl<'a, SM, SV, N, M, K, E> Linear<Matrix<E, M, K, SV>> for Matrix<E, N, M, SM>
191 where 261 where
192 SM: Storage<E, N, M>, 262 SM: ViewableStorage<E, N, M>,
193 SV: Storage<E, M, K> + Clone, 263 SV: ViewableStorage<E, M, K> + Clone,
194 N: Dim, 264 N: Dim,
195 M: Dim, 265 M: Dim,
196 K: Dim, 266 K: Dim,
197 E: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign, 267 E: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
198 DefaultAllocator: Allocator<N, K>, 268 DefaultAllocator: Allocator<N, K> + Allocator<M, K> + Allocator<N, M> + Allocator<M, N>,
199 DefaultAllocator: Allocator<M, K>, 269 ShapeConstraint: StridesOk<E, N, M, SM> + StridesOk<E, M, K, SV> + StridesOk<E, N, K>,
200 DefaultAllocator: Allocator<N, M>,
201 DefaultAllocator: Allocator<M, N>,
202 { 270 {
203 } 271 }
204 272
205 impl<SM, SV1, SV2, N, M, K, E> GEMV<E, Matrix<E, M, K, SV1>, Matrix<E, N, K, SV2>> 273 impl<SM, SV1, SV2, N, M, K, E> GEMV<E, Matrix<E, M, K, SV1>, Matrix<E, N, K, SV2>>
206 for Matrix<E, N, M, SM> 274 for Matrix<E, N, M, SM>
207 where 275 where
208 SM: Storage<E, N, M>, 276 SM: ViewableStorage<E, N, M>,
209 SV1: Storage<E, M, K> + Clone, 277 SV1: ViewableStorage<E, M, K> + Clone,
210 SV2: StorageMut<E, N, K>, 278 SV2: ViewableStorageMut<E, N, K>,
211 N: Dim, 279 N: Dim,
212 M: Dim, 280 M: Dim,
213 K: Dim, 281 K: Dim,
214 E: Scalar + Zero + One + Float, 282 E: Scalar + Zero + One + Float,
215 DefaultAllocator: Allocator<N, K>, 283 DefaultAllocator: Allocator<N, K> + Allocator<M, K> + Allocator<N, M> + Allocator<M, N>,
216 DefaultAllocator: Allocator<M, K>, 284 ShapeConstraint: StridesOk<E, N, M, SM> + StridesOk<E, M, K, SV1> + StridesOk<E, N, K, SV2>,
217 DefaultAllocator: Allocator<N, M>,
218 DefaultAllocator: Allocator<M, N>,
219 { 285 {
220 #[inline] 286 #[inline]
221 fn gemv<I: Instance<Matrix<E, M, K, SV1>>>( 287 fn gemv<I: Instance<Matrix<E, M, K, SV1>>>(
222 &self, 288 &self,
223 y: &mut Matrix<E, N, K, SV2>, 289 y: &mut Matrix<E, N, K, SV2>,
234 } 300 }
235 } 301 }
236 302
237 impl<S, M, N, E> VectorSpace for Matrix<E, M, N, S> 303 impl<S, M, N, E> VectorSpace for Matrix<E, M, N, S>
238 where 304 where
239 S: Storage<E, M, N>, 305 S: ViewableStorage<E, M, N>,
240 M: Dim, 306 M: Dim,
241 N: Dim, 307 N: Dim,
242 E: Scalar + Zero + One + Float, 308 E: Scalar + Zero + One + Float,
243 DefaultAllocator: Allocator<M, N>, 309 DefaultAllocator: Allocator<M, N>,
310 ShapeConstraint: StridesOk<E, M, N, S>,
244 { 311 {
245 type Field = E; 312 type Field = E;
246 type Owned = OMatrix<E, M, N>; 313 type Owned = OMatrix<E, M, N>;
247 314
248 #[inline] 315 #[inline]
252 } 319 }
253 } 320 }
254 321
255 impl<SM, SV1, M, N, E> AXPY<Matrix<E, M, N, SV1>> for Matrix<E, M, N, SM> 322 impl<SM, SV1, M, N, E> AXPY<Matrix<E, M, N, SV1>> for Matrix<E, M, N, SM>
256 where 323 where
257 SM: StorageMut<E, M, N>, 324 SM: ViewableStorageMut<E, M, N>,
258 SV1: Storage<E, M, N>, 325 SV1: ViewableStorage<E, M, N>,
259 M: Dim, 326 M: Dim,
260 N: Dim, 327 N: Dim,
261 E: Scalar + Zero + One + Float, 328 E: Scalar + Zero + One + Float,
262 DefaultAllocator: Allocator<M, N>, 329 DefaultAllocator: Allocator<M, N>,
330 ShapeConstraint: StridesOk<E, M, N, SM> + StridesOk<E, M, N, SV1>,
263 { 331 {
264 #[inline] 332 #[inline]
265 fn axpy<I: Instance<Matrix<E, M, N, SV1>>>(&mut self, α: E, x: I, β: E) { 333 fn axpy<I: Instance<Matrix<E, M, N, SV1>>>(&mut self, α: E, x: I, β: E) {
266 x.eval(|x̃| { 334 x.eval(|x̃| {
267 assert_eq!(self.ncols(), x̃.ncols()); 335 assert_eq!(self.ncols(), x̃.ncols());
298 } 366 }
299 }*/ 367 }*/
300 368
301 impl<SM, M, E> Projection<E, Linfinity> for Vector<E, M, SM> 369 impl<SM, M, E> Projection<E, Linfinity> for Vector<E, M, SM>
302 where 370 where
303 SM: Storage<E, M> + Clone, 371 SM: ViewableStorageMut<E, M> + Clone,
304 M: Dim, 372 M: Dim,
305 E: Scalar + Zero + One + Float + RealField, 373 E: Scalar + Zero + One + Float + RealField,
306 DefaultAllocator: Allocator<M>, 374 DefaultAllocator: Allocator<M>,
375 ShapeConstraint: StridesOk<E, M, U1, SM>,
307 { 376 {
308 #[inline] 377 #[inline]
309 fn proj_ball(self, ρ: E, exp: Linfinity) -> <Self as Space>::OwnedSpace { 378 fn proj_ball(self, ρ: E, exp: Linfinity) -> <Self as Space>::OwnedSpace {
310 let mut owned = self.into_owned(); 379 let mut owned = self.into_owned();
311 owned.proj_ball_mut(ρ, exp); 380 owned.proj_ball_mut(ρ, exp);
313 } 382 }
314 } 383 }
315 384
316 impl<SM, M, E> ProjectionMut<E, Linfinity> for Vector<E, M, SM> 385 impl<SM, M, E> ProjectionMut<E, Linfinity> for Vector<E, M, SM>
317 where 386 where
318 SM: StorageMut<E, M> + Clone, 387 SM: ViewableStorageMut<E, M> + Clone,
319 M: Dim, 388 M: Dim,
320 E: Scalar + Zero + One + Float + RealField, 389 E: Scalar + Zero + One + Float + RealField,
321 DefaultAllocator: Allocator<M>, 390 DefaultAllocator: Allocator<M>,
391 ShapeConstraint: StridesOk<E, M, U1, SM>,
322 { 392 {
323 #[inline] 393 #[inline]
324 fn proj_ball_mut(&mut self, ρ: E, _: Linfinity) { 394 fn proj_ball_mut(&mut self, ρ: E, _: Linfinity) {
325 self.iter_mut() 395 self.iter_mut()
326 .for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ)) 396 .for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ))
328 } 398 }
329 399
330 impl<'own, SV1, SV2, SM, N, M, K, E> Adjointable<Matrix<E, M, K, SV1>, Matrix<E, N, K, SV2>> 400 impl<'own, SV1, SV2, SM, N, M, K, E> Adjointable<Matrix<E, M, K, SV1>, Matrix<E, N, K, SV2>>
331 for Matrix<E, N, M, SM> 401 for Matrix<E, N, M, SM>
332 where 402 where
333 SM: Storage<E, N, M>, 403 SM: ViewableStorage<E, N, M>,
334 SV1: Storage<E, M, K> + Clone, 404 SV1: ViewableStorage<E, M, K> + Clone,
335 SV2: Storage<E, N, K> + Clone, 405 SV2: ViewableStorage<E, N, K> + Clone,
336 N: Dim, 406 N: Dim,
337 M: Dim, 407 M: Dim,
338 K: Dim, 408 K: Dim,
339 E: Scalar + Zero + One + SimdComplexField, 409 E: Scalar + Zero + One + SimdComplexField,
340 DefaultAllocator: Allocator<N, K>, 410 DefaultAllocator: Allocator<N, K> + Allocator<M, K> + Allocator<N, M> + Allocator<M, N>,
341 DefaultAllocator: Allocator<M, K>, 411 ShapeConstraint: StridesOk<E, N, M, SM>
342 DefaultAllocator: Allocator<N, M>, 412 + StridesOk<E, M, K, SV1>
343 DefaultAllocator: Allocator<M, N>, 413 + StridesOk<E, N, K, SV2>
414 + StridesOk<E, M, N>,
344 { 415 {
345 type AdjointCodomain = OMatrix<E, M, K>; 416 type AdjointCodomain = OMatrix<E, M, K>;
346 type Adjoint<'a> 417 type Adjoint<'a>
347 = OMatrix<E, M, N> 418 = OMatrix<E, M, N>
348 where 419 where
380 // TODO: should allow different input storages in `Euclidean`. 451 // TODO: should allow different input storages in `Euclidean`.
381 452
382 impl<E, M, S> Euclidean<E> for Vector<E, M, S> 453 impl<E, M, S> Euclidean<E> for Vector<E, M, S>
383 where 454 where
384 M: Dim, 455 M: Dim,
385 S: Storage<E, M>, 456 S: ViewableStorage<E, M>,
386 E: Float + Scalar + Zero + One + RealField, 457 E: Float + Scalar + Zero + One + RealField,
387 DefaultAllocator: Allocator<M>, 458 DefaultAllocator: Allocator<M>,
459 ShapeConstraint: StridesOk<E, M, U1, S>,
388 { 460 {
389 type OwnedEuclidean = OVector<E, M>; 461 type OwnedEuclidean = OVector<E, M>;
390 462
391 #[inline] 463 #[inline]
392 fn dot<I: Instance<Self>>(&self, other: I) -> E { 464 fn dot<I: Instance<Self>>(&self, other: I) -> E {
405 } 477 }
406 478
407 impl<E, M, S> StaticEuclidean<E> for Vector<E, M, S> 479 impl<E, M, S> StaticEuclidean<E> for Vector<E, M, S>
408 where 480 where
409 M: DimName, 481 M: DimName,
410 S: Storage<E, M>, 482 S: ViewableStorage<E, M>,
411 E: Float + Scalar + Zero + One + RealField, 483 E: Float + Scalar + Zero + One + RealField,
412 DefaultAllocator: Allocator<M>, 484 DefaultAllocator: Allocator<M>,
485 ShapeConstraint: StridesOk<E, M, U1, S>,
413 { 486 {
414 #[inline] 487 #[inline]
415 fn origin() -> OVector<E, M> { 488 fn origin() -> OVector<E, M> {
416 OVector::zeros() 489 OVector::zeros()
417 } 490 }
419 492
420 /// The default norm for `Vector` is [`L2`]. 493 /// The default norm for `Vector` is [`L2`].
421 impl<E, M, S> Normed<E> for Vector<E, M, S> 494 impl<E, M, S> Normed<E> for Vector<E, M, S>
422 where 495 where
423 M: Dim, 496 M: Dim,
424 S: Storage<E, M>, 497 S: ViewableStorage<E, M>,
425 E: Float + Scalar + Zero + One + RealField, 498 E: Float + Scalar + Zero + One + RealField,
426 DefaultAllocator: Allocator<M>, 499 DefaultAllocator: Allocator<M>,
500 ShapeConstraint: StridesOk<E, M, U1, S>,
427 { 501 {
428 type NormExp = L2; 502 type NormExp = L2;
429 503
430 #[inline] 504 #[inline]
431 fn norm_exponent(&self) -> Self::NormExp { 505 fn norm_exponent(&self) -> Self::NormExp {
439 } 513 }
440 514
441 impl<E, M, S> HasDual<E> for Vector<E, M, S> 515 impl<E, M, S> HasDual<E> for Vector<E, M, S>
442 where 516 where
443 M: Dim, 517 M: Dim,
444 S: Storage<E, M>, 518 S: ViewableStorage<E, M>,
445 E: Float + Scalar + Zero + One + RealField, 519 E: Float + Scalar + Zero + One + RealField,
446 DefaultAllocator: Allocator<M>, 520 DefaultAllocator: Allocator<M>,
521 ShapeConstraint: StridesOk<E, M, U1, S>,
447 { 522 {
448 type DualSpace = OVector<E, M>; 523 type DualSpace = OVector<E, M>;
449 524
450 fn dual_origin(&self) -> OVector<E, M> { 525 fn dual_origin(&self) -> OVector<E, M> {
451 OVector::zeros_generic(M::from_usize(self.len()), Const) 526 OVector::zeros_generic(M::from_usize(self.len()), Const)
453 } 528 }
454 529
455 impl<E, M, S> Norm<L1, E> for Vector<E, M, S> 530 impl<E, M, S> Norm<L1, E> for Vector<E, M, S>
456 where 531 where
457 M: Dim, 532 M: Dim,
458 S: Storage<E, M>, 533 S: ViewableStorage<E, M>,
459 E: Float + Scalar + Zero + One + RealField, 534 E: Float + Scalar + Zero + One + RealField,
460 DefaultAllocator: Allocator<M>, 535 DefaultAllocator: Allocator<M>,
536 ShapeConstraint: StridesOk<E, M, U1, S>,
461 { 537 {
462 #[inline] 538 #[inline]
463 fn norm(&self, _: L1) -> E { 539 fn norm(&self, _: L1) -> E {
464 nalgebra::Norm::norm(&LpNorm(1), self) 540 nalgebra::Norm::norm(&LpNorm(1), self)
465 } 541 }
466 } 542 }
467 543
468 impl<E, M, S> Dist<L1, E> for Vector<E, M, S> 544 impl<E, M, S> Dist<L1, E> for Vector<E, M, S>
469 where 545 where
470 M: Dim, 546 M: Dim,
471 S: Storage<E, M> + Clone, 547 S: ViewableStorage<E, M> + Clone,
472 E: Float + Scalar + Zero + One + RealField, 548 E: Float + Scalar + Zero + One + RealField,
473 DefaultAllocator: Allocator<M>, 549 DefaultAllocator: Allocator<M>,
550 ShapeConstraint: StridesOk<E, M, U1, S>,
474 { 551 {
475 #[inline] 552 #[inline]
476 fn dist<I: Instance<Self>>(&self, other: I, _: L1) -> E { 553 fn dist<I: Instance<Self>>(&self, other: I, _: L1) -> E {
477 other.eval_ref_decompose(|ref r| nalgebra::Norm::metric_distance(&LpNorm(1), self, r)) 554 other.eval_ref_decompose(|ref r| nalgebra::Norm::metric_distance(&LpNorm(1), self, r))
478 } 555 }
479 } 556 }
480 557
481 impl<E, M, S> Norm<L2, E> for Vector<E, M, S> 558 impl<E, M, S> Norm<L2, E> for Vector<E, M, S>
482 where 559 where
483 M: Dim, 560 M: Dim,
484 S: Storage<E, M>, 561 S: ViewableStorage<E, M>,
485 E: Float + Scalar + Zero + One + RealField, 562 E: Float + Scalar + Zero + One + RealField,
486 DefaultAllocator: Allocator<M>, 563 DefaultAllocator: Allocator<M>,
564 ShapeConstraint: StridesOk<E, M, U1, S>,
487 { 565 {
488 #[inline] 566 #[inline]
489 fn norm(&self, _: L2) -> E { 567 fn norm(&self, _: L2) -> E {
490 nalgebra::Norm::norm(&LpNorm(2), self) 568 nalgebra::Norm::norm(&LpNorm(2), self)
491 } 569 }
492 } 570 }
493 571
494 impl<E, M, S> Dist<L2, E> for Vector<E, M, S> 572 impl<E, M, S> Dist<L2, E> for Vector<E, M, S>
495 where 573 where
496 M: Dim, 574 M: Dim,
497 S: Storage<E, M> + Clone, 575 S: ViewableStorage<E, M> + Clone,
498 E: Float + Scalar + Zero + One + RealField, 576 E: Float + Scalar + Zero + One + RealField,
499 DefaultAllocator: Allocator<M>, 577 DefaultAllocator: Allocator<M>,
578 ShapeConstraint: StridesOk<E, M, U1, S>,
500 { 579 {
501 #[inline] 580 #[inline]
502 fn dist<I: Instance<Self>>(&self, other: I, _: L2) -> E { 581 fn dist<I: Instance<Self>>(&self, other: I, _: L2) -> E {
503 other.eval_ref_decompose(|ref r| nalgebra::Norm::metric_distance(&LpNorm(2), self, r)) 582 other.eval_ref_decompose(|ref r| nalgebra::Norm::metric_distance(&LpNorm(2), self, r))
504 } 583 }
505 } 584 }
506 585
507 impl<E, M, S> Norm<Linfinity, E> for Vector<E, M, S> 586 impl<E, M, S> Norm<Linfinity, E> for Vector<E, M, S>
508 where 587 where
509 M: Dim, 588 M: Dim,
510 S: Storage<E, M>, 589 S: ViewableStorage<E, M>,
511 E: Float + Scalar + Zero + One + RealField, 590 E: Float + Scalar + Zero + One + RealField,
512 DefaultAllocator: Allocator<M>, 591 DefaultAllocator: Allocator<M>,
592 ShapeConstraint: StridesOk<E, M, U1, S>,
513 { 593 {
514 #[inline] 594 #[inline]
515 fn norm(&self, _: Linfinity) -> E { 595 fn norm(&self, _: Linfinity) -> E {
516 nalgebra::Norm::norm(&UniformNorm, self) 596 nalgebra::Norm::norm(&UniformNorm, self)
517 } 597 }
518 } 598 }
519 599
520 impl<E, M, S> Dist<Linfinity, E> for Vector<E, M, S> 600 impl<E, M, S> Dist<Linfinity, E> for Vector<E, M, S>
521 where 601 where
522 M: Dim, 602 M: Dim,
523 S: Storage<E, M> + Clone, 603 S: ViewableStorage<E, M> + Clone,
524 E: Float + Scalar + Zero + One + RealField, 604 E: Float + Scalar + Zero + One + RealField,
525 DefaultAllocator: Allocator<M>, 605 DefaultAllocator: Allocator<M>,
606 ShapeConstraint: StridesOk<E, M, U1, S>,
526 { 607 {
527 #[inline] 608 #[inline]
528 fn dist<I: Instance<Self>>(&self, other: I, _: Linfinity) -> E { 609 fn dist<I: Instance<Self>>(&self, other: I, _: Linfinity) -> E {
529 other.eval_ref_decompose(|ref r| nalgebra::Norm::metric_distance(&UniformNorm, self, r)) 610 other.eval_ref_decompose(|ref r| nalgebra::Norm::metric_distance(&UniformNorm, self, r))
530 } 611 }

mercurial