src/nalgebra_support.rs

branch
dev
changeset 181
f159287bc191
parent 180
ec5a811eab09
child 184
b7b60b3b3eff
equal deleted inserted replaced
180:ec5a811eab09 181:f159287bc191
16 use nalgebra::base::allocator::Allocator; 16 use nalgebra::base::allocator::Allocator;
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 ArrayStorage, ClosedAddAssign, ClosedMulAssign, DefaultAllocator, Dim, LpNorm, Matrix, 20 ArrayStorage, ClosedAddAssign, ClosedMulAssign, DefaultAllocator, Dim, LpNorm, Matrix,
21 MatrixView, OMatrix, OVector, RawStorage, RealField, Scalar, SimdComplexField, Storage, 21 MatrixView, OMatrix, OVector, RawStorage, RealField, SimdComplexField, Storage, StorageMut,
22 StorageMut, UniformNorm, VecStorage, Vector, ViewStorage, ViewStorageMut, U1, 22 UniformNorm, VecStorage, Vector, ViewStorage, ViewStorageMut, U1,
23 }; 23 };
24 use num_traits::identities::{One, Zero}; 24 use num_traits::identities::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>
28 where 28 where
29 S: Storage<E, M, N>, 29 S: Storage<E, M, N>,
30 M: Dim, 30 M: Dim,
31 N: Dim, 31 N: Dim,
32 E: Scalar + Zero + One, 32 E: Float,
33 DefaultAllocator: Allocator<M, N>, 33 DefaultAllocator: Allocator<M, N>,
34 { 34 {
35 type OwnedVariant = OMatrix<E, M, N>; 35 type OwnedVariant = OMatrix<E, M, N>;
36 36
37 #[inline] 37 #[inline]
61 61
62 trait StridesOk<E, N, M = U1, S = <DefaultAllocator as Allocator<N, M>>::Buffer<E>>: 62 trait StridesOk<E, N, M = U1, S = <DefaultAllocator as Allocator<N, M>>::Buffer<E>>:
63 DimEq<Dyn, S::RStride> + DimEq<Dyn, S::CStride> 63 DimEq<Dyn, S::RStride> + DimEq<Dyn, S::CStride>
64 where 64 where
65 S: RawStorage<E, N, M>, 65 S: RawStorage<E, N, M>,
66 E: Scalar, 66 E: Float,
67 N: Dim, 67 N: Dim,
68 M: Dim, 68 M: Dim,
69 DefaultAllocator: Allocator<N, M>, 69 DefaultAllocator: Allocator<N, M>,
70 { 70 {
71 } 71 }
72 72
73 impl<E, M> StridesOk<E, Dyn, M, VecStorage<E, Dyn, M>> for ShapeConstraint 73 impl<E, M> StridesOk<E, Dyn, M, VecStorage<E, Dyn, M>> for ShapeConstraint
74 where 74 where
75 M: Dim, 75 M: Dim,
76 E: Scalar, 76 E: Float,
77 DefaultAllocator: Allocator<Dyn, M>, 77 DefaultAllocator: Allocator<Dyn, M>,
78 { 78 {
79 } 79 }
80 80
81 impl<E, const N: usize, const M: usize> StridesOk<E, Const<N>, Const<M>, ArrayStorage<E, N, M>> 81 impl<E, const N: usize, const M: usize> StridesOk<E, Const<N>, Const<M>, ArrayStorage<E, N, M>>
82 for ShapeConstraint 82 for ShapeConstraint
83 where 83 where
84 E: Scalar, 84 E: Float,
85 { 85 {
86 } 86 }
87 87
88 macro_rules! strides_ok { 88 macro_rules! strides_ok {
89 ($R:ty, $C:ty where $($qual:tt)*) => { 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 90 impl<'a, E, N, M, $($qual)*> StridesOk<E, N, M, ViewStorage<'a, E, N, M, $R, $C>> for ShapeConstraint
91 where 91 where
92 N: Dim, 92 N: Dim,
93 M: Dim, 93 M: Dim,
94 E: Scalar, 94 E: Float,
95 DefaultAllocator: Allocator<N, M>, 95 DefaultAllocator: Allocator<N, M>,
96 { 96 {
97 } 97 }
98 impl<'a, E, N, M, $($qual)*> StridesOk<E, N, M, ViewStorageMut<'a, E, N, M, $R, $C>> for ShapeConstraint 98 impl<'a, E, N, M, $($qual)*> StridesOk<E, N, M, ViewStorageMut<'a, E, N, M, $R, $C>> for ShapeConstraint
99 where 99 where
100 N: Dim, 100 N: Dim,
101 M: Dim, 101 M: Dim,
102 E: Scalar, 102 E: Float,
103 DefaultAllocator: Allocator<N, M>, 103 DefaultAllocator: Allocator<N, M>,
104 { 104 {
105 } 105 }
106 }; 106 };
107 } 107 }
108 108
114 impl<SM, N, M, E> Space for Matrix<E, N, M, SM> 114 impl<SM, N, M, E> Space for Matrix<E, N, M, SM>
115 where 115 where
116 SM: Storage<E, N, M>, 116 SM: Storage<E, N, M>,
117 N: Dim, 117 N: Dim,
118 M: Dim, 118 M: Dim,
119 E: Scalar + Zero + One + Copy, 119 E: Float,
120 DefaultAllocator: Allocator<N, M>, 120 DefaultAllocator: Allocator<N, M>,
121 ShapeConstraint: StridesOk<E, N, M>, 121 ShapeConstraint: StridesOk<E, N, M>,
122 { 122 {
123 type Principal = OMatrix<E, N, M>; 123 type Principal = OMatrix<E, N, M>;
124 type Decomp = MatrixDecomposition; 124 type Decomp = MatrixDecomposition;
130 impl<E, M, K, S> Decomposition<Matrix<E, M, K, S>> for MatrixDecomposition 130 impl<E, M, K, S> Decomposition<Matrix<E, M, K, S>> for MatrixDecomposition
131 where 131 where
132 S: Storage<E, M, K>, 132 S: Storage<E, M, K>,
133 M: Dim, 133 M: Dim,
134 K: Dim, 134 K: Dim,
135 E: Scalar + Zero + One + Copy, 135 E: Float,
136 DefaultAllocator: Allocator<M, K>, 136 DefaultAllocator: Allocator<M, K>,
137 ShapeConstraint: StridesOk<E, M, K>, 137 ShapeConstraint: StridesOk<E, M, K>,
138 { 138 {
139 type Decomposition<'b> 139 type Decomposition<'b>
140 = MyCow<'b, OMatrix<E, M, K>> 140 = MyCow<'b, OMatrix<E, M, K>>
158 where 158 where
159 S1: Storage<E, M, K>, 159 S1: Storage<E, M, K>,
160 S2: Storage<E, M, K>, 160 S2: Storage<E, M, K>,
161 M: Dim, 161 M: Dim,
162 K: Dim, 162 K: Dim,
163 E: Scalar + Zero + One + Copy, 163 E: Float,
164 DefaultAllocator: Allocator<M, K>, 164 DefaultAllocator: Allocator<M, K>,
165 ShapeConstraint: StridesOk<E, M, K, S2> + StridesOk<E, M, K>, 165 ShapeConstraint: StridesOk<E, M, K, S2> + StridesOk<E, M, K>,
166 { 166 {
167 #[inline] 167 #[inline]
168 fn eval_ref<'b, R>( 168 fn eval_ref<'b, R>(
203 where 203 where
204 S1: Storage<E, M, K>, 204 S1: Storage<E, M, K>,
205 S2: Storage<E, M, K>, 205 S2: Storage<E, M, K>,
206 M: Dim, 206 M: Dim,
207 K: Dim, 207 K: Dim,
208 E: Scalar + Zero + One + Copy, 208 E: Float,
209 DefaultAllocator: Allocator<M, K>, 209 DefaultAllocator: Allocator<M, K>,
210 ShapeConstraint: StridesOk<E, M, K, S2> + StridesOk<E, M, K>, 210 ShapeConstraint: StridesOk<E, M, K, S2> + StridesOk<E, M, K>,
211 { 211 {
212 fn eval_ref<'b, R>( 212 fn eval_ref<'b, R>(
213 &'b self, 213 &'b self,
247 where 247 where
248 S1: Storage<E, M, K>, 248 S1: Storage<E, M, K>,
249 SM: Storage<E, M, K>, 249 SM: Storage<E, M, K>,
250 M: Dim, 250 M: Dim,
251 K: Dim, 251 K: Dim,
252 E: Scalar + Zero + One + Copy, 252 E: Float,
253 DefaultAllocator: Allocator<M, K>, 253 DefaultAllocator: Allocator<M, K>,
254 ShapeConstraint: StridesOk<E, M, K, SM> + StridesOk<E, M, K>, 254 ShapeConstraint: StridesOk<E, M, K, SM> + StridesOk<E, M, K>,
255 { 255 {
256 #[inline] 256 #[inline]
257 fn eval_ref<'b, R>( 257 fn eval_ref<'b, R>(
291 where 291 where
292 SM: Storage<E, N, M>, 292 SM: Storage<E, N, M>,
293 N: Dim, 293 N: Dim,
294 M: Dim, 294 M: Dim,
295 K: Dim, 295 K: Dim,
296 E: Scalar + Zero + One + Copy + ClosedMulAssign + ClosedAddAssign, 296 E: Float,
297 DefaultAllocator: Allocator<M, K> + Allocator<N, K>, 297 DefaultAllocator: Allocator<M, K> + Allocator<N, K>,
298 ShapeConstraint: StridesOk<E, M, K> + StridesOk<E, N, K>, 298 ShapeConstraint: StridesOk<E, M, K> + StridesOk<E, N, K>,
299 { 299 {
300 type Codomain = OMatrix<E, N, K>; 300 type Codomain = OMatrix<E, N, K>;
301 301
309 where 309 where
310 SM: Storage<E, N, M>, 310 SM: Storage<E, N, M>,
311 N: Dim, 311 N: Dim,
312 M: Dim, 312 M: Dim,
313 K: Dim, 313 K: Dim,
314 E: Scalar + Zero + One + Copy + ClosedMulAssign + ClosedAddAssign, 314 E: Float + ClosedMulAssign + ClosedAddAssign,
315 DefaultAllocator: Allocator<N, K> + Allocator<M, K>, 315 DefaultAllocator: Allocator<N, K> + Allocator<M, K>,
316 ShapeConstraint: StridesOk<E, M, K> + StridesOk<E, N, K>, 316 ShapeConstraint: StridesOk<E, M, K> + StridesOk<E, N, K>,
317 { 317 {
318 } 318 }
319 319
322 SM: Storage<E, N, M>, 322 SM: Storage<E, N, M>,
323 SV2: StorageMut<E, N, K>, 323 SV2: StorageMut<E, N, K>,
324 N: Dim, 324 N: Dim,
325 M: Dim, 325 M: Dim,
326 K: Dim, 326 K: Dim,
327 E: Scalar + Zero + One + Float, 327 E: Float,
328 DefaultAllocator: Allocator<N, K> + Allocator<M, K>, 328 DefaultAllocator: Allocator<N, K> + Allocator<M, K>,
329 ShapeConstraint: StridesOk<E, M, K> + StridesOk<E, N, K>, 329 ShapeConstraint: StridesOk<E, M, K> + StridesOk<E, N, K>,
330 { 330 {
331 #[inline] 331 #[inline]
332 fn gemv<I: Instance<OMatrix<E, M, K>>>( 332 fn gemv<I: Instance<OMatrix<E, M, K>>>(
344 impl<S, M, N, E> VectorSpace for Matrix<E, M, N, S> 344 impl<S, M, N, E> VectorSpace for Matrix<E, M, N, S>
345 where 345 where
346 S: Storage<E, M, N>, 346 S: Storage<E, M, N>,
347 M: Dim, 347 M: Dim,
348 N: Dim, 348 N: Dim,
349 E: Scalar + Zero + One + Float, 349 E: Float,
350 DefaultAllocator: Allocator<M, N>, 350 DefaultAllocator: Allocator<M, N>,
351 ShapeConstraint: StridesOk<E, M, N>, 351 ShapeConstraint: StridesOk<E, M, N>,
352 { 352 {
353 type Field = E; 353 type Field = E;
354 type PrincipalV = OMatrix<E, M, N>; 354 type PrincipalV = OMatrix<E, M, N>;
365 impl<M, N, E, S> AXPY<OMatrix<E, M, N>> for Matrix<E, M, N, S> 365 impl<M, N, E, S> AXPY<OMatrix<E, M, N>> for Matrix<E, M, N, S>
366 where 366 where
367 S: StorageMut<E, M, N>, 367 S: StorageMut<E, M, N>,
368 M: Dim, 368 M: Dim,
369 N: Dim, 369 N: Dim,
370 E: Scalar + Zero + One + Float, 370 E: Float,
371 DefaultAllocator: Allocator<M, N>, 371 DefaultAllocator: Allocator<M, N>,
372 ShapeConstraint: StridesOk<E, M, N>, 372 ShapeConstraint: StridesOk<E, M, N>,
373 { 373 {
374 #[inline] 374 #[inline]
375 fn axpy<I: Instance<OMatrix<E, M, N>>>(&mut self, α: E, x: I, β: E) { 375 fn axpy<I: Instance<OMatrix<E, M, N>>>(&mut self, α: E, x: I, β: E) {
410 410
411 impl<SM, M, E> Projection<E, Linfinity> for Vector<E, M, SM> 411 impl<SM, M, E> Projection<E, Linfinity> for Vector<E, M, SM>
412 where 412 where
413 SM: StorageMut<E, M>, 413 SM: StorageMut<E, M>,
414 M: Dim, 414 M: Dim,
415 E: Scalar + Zero + One + Float + RealField, 415 E: Float + RealField,
416 DefaultAllocator: Allocator<M>, 416 DefaultAllocator: Allocator<M>,
417 ShapeConstraint: StridesOk<E, M>, 417 ShapeConstraint: StridesOk<E, M>,
418 { 418 {
419 #[inline] 419 #[inline]
420 fn proj_ball(self, ρ: E, exp: Linfinity) -> <Self as Space>::Principal { 420 fn proj_ball(self, ρ: E, exp: Linfinity) -> <Self as Space>::Principal {
426 426
427 impl<SM, M, E> ProjectionMut<E, Linfinity> for Vector<E, M, SM> 427 impl<SM, M, E> ProjectionMut<E, Linfinity> for Vector<E, M, SM>
428 where 428 where
429 SM: StorageMut<E, M>, 429 SM: StorageMut<E, M>,
430 M: Dim, 430 M: Dim,
431 E: Scalar + Zero + One + Copy + Float + RealField, 431 E: Float + RealField,
432 DefaultAllocator: Allocator<M>, 432 DefaultAllocator: Allocator<M>,
433 ShapeConstraint: StridesOk<E, M>, 433 ShapeConstraint: StridesOk<E, M>,
434 { 434 {
435 #[inline] 435 #[inline]
436 fn proj_ball_mut(&mut self, ρ: E, _: Linfinity) { 436 fn proj_ball_mut(&mut self, ρ: E, _: Linfinity) {
443 where 443 where
444 SM: Storage<E, N, M>, 444 SM: Storage<E, N, M>,
445 N: Dim, 445 N: Dim,
446 M: Dim, 446 M: Dim,
447 K: Dim, 447 K: Dim,
448 E: Scalar + Zero + One + Copy + SimdComplexField, 448 E: Float + RealField,
449 DefaultAllocator: Allocator<N, K> + Allocator<M, K> + Allocator<M, N>, 449 DefaultAllocator: Allocator<N, K> + Allocator<M, K> + Allocator<M, N>,
450 ShapeConstraint: StridesOk<E, N, K> + StridesOk<E, M, K>, 450 ShapeConstraint: StridesOk<E, N, K> + StridesOk<E, M, K>,
451 { 451 {
452 type AdjointCodomain = OMatrix<E, M, K>; 452 type AdjointCodomain = OMatrix<E, M, K>;
453 type Adjoint<'a> 453 type Adjoint<'a>
487 impl<E, M, N, S> Euclidean<E> for Matrix<E, M, N, S> 487 impl<E, M, N, S> Euclidean<E> for Matrix<E, M, N, S>
488 where 488 where
489 M: Dim, 489 M: Dim,
490 N: Dim, 490 N: Dim,
491 S: Storage<E, M, N>, 491 S: Storage<E, M, N>,
492 E: Float + Scalar + Zero + One + RealField, 492 E: Float + RealField,
493 DefaultAllocator: Allocator<M, N>, 493 DefaultAllocator: Allocator<M, N>,
494 ShapeConstraint: StridesOk<E, M, N>, 494 ShapeConstraint: StridesOk<E, M, N>,
495 { 495 {
496 type PrincipalE = OMatrix<E, M, N>; 496 type PrincipalE = OMatrix<E, M, N>;
497 497
513 513
514 impl<E, M, S> StaticEuclidean<E> for Vector<E, M, S> 514 impl<E, M, S> StaticEuclidean<E> for Vector<E, M, S>
515 where 515 where
516 M: DimName, 516 M: DimName,
517 S: Storage<E, M>, 517 S: Storage<E, M>,
518 E: Float + Scalar + Zero + One + RealField, 518 E: Float + RealField,
519 DefaultAllocator: Allocator<M>, 519 DefaultAllocator: Allocator<M>,
520 ShapeConstraint: StridesOk<E, M>, 520 ShapeConstraint: StridesOk<E, M>,
521 { 521 {
522 #[inline] 522 #[inline]
523 fn origin() -> OVector<E, M> { 523 fn origin() -> OVector<E, M> {
529 impl<E, M, N, S> Normed<E> for Matrix<E, M, N, S> 529 impl<E, M, N, S> Normed<E> for Matrix<E, M, N, S>
530 where 530 where
531 M: Dim, 531 M: Dim,
532 N: Dim, 532 N: Dim,
533 S: Storage<E, M, N>, 533 S: Storage<E, M, N>,
534 E: Float + Scalar + Zero + One + RealField, 534 E: Float + RealField,
535 DefaultAllocator: Allocator<M, N>, 535 DefaultAllocator: Allocator<M, N>,
536 ShapeConstraint: StridesOk<E, M, N>, 536 ShapeConstraint: StridesOk<E, M, N>,
537 { 537 {
538 type NormExp = L2; 538 type NormExp = L2;
539 539
551 impl<E, M, N, S> HasDual<E> for Matrix<E, M, N, S> 551 impl<E, M, N, S> HasDual<E> for Matrix<E, M, N, S>
552 where 552 where
553 M: Dim, 553 M: Dim,
554 N: Dim, 554 N: Dim,
555 S: Storage<E, M, N>, 555 S: Storage<E, M, N>,
556 E: Float + Scalar + Zero + One + RealField, 556 E: Float + RealField,
557 DefaultAllocator: Allocator<M, N>, 557 DefaultAllocator: Allocator<M, N>,
558 ShapeConstraint: StridesOk<E, M, N>, 558 ShapeConstraint: StridesOk<E, M, N>,
559 { 559 {
560 type DualSpace = OMatrix<E, M, N>; 560 type DualSpace = OMatrix<E, M, N>;
561 561
567 567
568 impl<E, M, S> Norm<L1, E> for Vector<E, M, S> 568 impl<E, M, S> Norm<L1, E> for Vector<E, M, S>
569 where 569 where
570 M: Dim, 570 M: Dim,
571 S: Storage<E, M>, 571 S: Storage<E, M>,
572 E: Float + Scalar + Zero + One + RealField, 572 E: Float + RealField,
573 { 573 {
574 #[inline] 574 #[inline]
575 fn norm(&self, _: L1) -> E { 575 fn norm(&self, _: L1) -> E {
576 nalgebra::Norm::norm(&LpNorm(1), self) 576 nalgebra::Norm::norm(&LpNorm(1), self)
577 } 577 }
579 579
580 impl<E, M, S> Dist<L1, E> for Vector<E, M, S> 580 impl<E, M, S> Dist<L1, E> for Vector<E, M, S>
581 where 581 where
582 M: Dim, 582 M: Dim,
583 S: Storage<E, M>, 583 S: Storage<E, M>,
584 E: Float + Scalar + Zero + One + RealField, 584 E: Float + RealField,
585 DefaultAllocator: Allocator<M>, 585 DefaultAllocator: Allocator<M>,
586 ShapeConstraint: StridesOk<E, M>, 586 ShapeConstraint: StridesOk<E, M>,
587 { 587 {
588 #[inline] 588 #[inline]
589 fn dist<I: Instance<Self>>(&self, other: I, _: L1) -> E { 589 fn dist<I: Instance<Self>>(&self, other: I, _: L1) -> E {
594 impl<E, M, N, S> Norm<L2, E> for Matrix<E, M, N, S> 594 impl<E, M, N, S> Norm<L2, E> for Matrix<E, M, N, S>
595 where 595 where
596 M: Dim, 596 M: Dim,
597 N: Dim, 597 N: Dim,
598 S: Storage<E, M, N>, 598 S: Storage<E, M, N>,
599 E: Float + Scalar + Zero + One + RealField, 599 E: Float + RealField,
600 { 600 {
601 #[inline] 601 #[inline]
602 fn norm(&self, _: L2) -> E { 602 fn norm(&self, _: L2) -> E {
603 nalgebra::Norm::norm(&LpNorm(2), self) 603 nalgebra::Norm::norm(&LpNorm(2), self)
604 } 604 }
607 impl<E, M, N, S> Dist<L2, E> for Matrix<E, M, N, S> 607 impl<E, M, N, S> Dist<L2, E> for Matrix<E, M, N, S>
608 where 608 where
609 M: Dim, 609 M: Dim,
610 N: Dim, 610 N: Dim,
611 S: Storage<E, M, N>, 611 S: Storage<E, M, N>,
612 E: Float + Scalar + Zero + One + RealField, 612 E: Float + RealField,
613 DefaultAllocator: Allocator<M, N>, 613 DefaultAllocator: Allocator<M, N>,
614 ShapeConstraint: StridesOk<E, M, N>, 614 ShapeConstraint: StridesOk<E, M, N>,
615 { 615 {
616 #[inline] 616 #[inline]
617 fn dist<I: Instance<Self>>(&self, other: I, _: L2) -> E { 617 fn dist<I: Instance<Self>>(&self, other: I, _: L2) -> E {
622 impl<E, M, N, S> Norm<Linfinity, E> for Matrix<E, M, N, S> 622 impl<E, M, N, S> Norm<Linfinity, E> for Matrix<E, M, N, S>
623 where 623 where
624 M: Dim, 624 M: Dim,
625 N: Dim, 625 N: Dim,
626 S: Storage<E, M, N>, 626 S: Storage<E, M, N>,
627 E: Float + Scalar + Zero + One + RealField, 627 E: Float + RealField,
628 { 628 {
629 #[inline] 629 #[inline]
630 fn norm(&self, _: Linfinity) -> E { 630 fn norm(&self, _: Linfinity) -> E {
631 nalgebra::Norm::norm(&UniformNorm, self) 631 nalgebra::Norm::norm(&UniformNorm, self)
632 } 632 }
635 impl<E, M, N, S> Dist<Linfinity, E> for Matrix<E, M, N, S> 635 impl<E, M, N, S> Dist<Linfinity, E> for Matrix<E, M, N, S>
636 where 636 where
637 M: Dim, 637 M: Dim,
638 N: Dim, 638 N: Dim,
639 S: Storage<E, M, N>, 639 S: Storage<E, M, N>,
640 E: Float + Scalar + Zero + One + RealField, 640 E: Float + RealField,
641 DefaultAllocator: Allocator<M, N>, 641 DefaultAllocator: Allocator<M, N>,
642 ShapeConstraint: StridesOk<E, M, N>, 642 ShapeConstraint: StridesOk<E, M, N>,
643 { 643 {
644 #[inline] 644 #[inline]
645 fn dist<I: Instance<Self>>(&self, other: I, _: Linfinity) -> E { 645 fn dist<I: Instance<Self>>(&self, other: I, _: Linfinity) -> E {

mercurial