src/nalgebra_support.rs

branch
dev
changeset 59
9226980e45a7
parent 56
5e3c1874797d
child 60
848ecc05becf
equal deleted inserted replaced
58:1a38447a89fa 59:9226980e45a7
21 use nalgebra::base::allocator::Allocator; 21 use nalgebra::base::allocator::Allocator;
22 use std::ops::Mul; 22 use std::ops::Mul;
23 use num_traits::identities::{Zero, One}; 23 use num_traits::identities::{Zero, One};
24 use crate::linops::*; 24 use crate::linops::*;
25 use crate::euclidean::*; 25 use crate::euclidean::*;
26 use crate::mapping::{Space, BasicDecomposition};
26 use crate::types::Float; 27 use crate::types::Float;
27 use crate::norms::*; 28 use crate::norms::*;
28 29 use crate::instance::Instance;
29 impl<SM,SV,N,M,K,E> Apply<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM> 30
30 where SM: Storage<E,N,M>, SV: Storage<E,M,K>, 31 impl<SM,N,M,E> Space for Matrix<E,N,M,SM>
31 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign, 32 where
32 DefaultAllocator : Allocator<N,K>, 33 SM: Storage<E,N,M> + Clone,
33 DefaultAllocator : Allocator<M,K>, 34 N : Dim, M : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
34 DefaultAllocator : Allocator<N,M>, 35 DefaultAllocator : Allocator<N,M>,
35 DefaultAllocator : Allocator<M,N> { 36 {
36 type Output = OMatrix<E,N,K>; 37 type Decomp = BasicDecomposition;
37 38 }
38 #[inline] 39
39 fn apply(&self, x : Matrix<E,M,K,SV>) -> Self::Output { 40 impl<SM,SV,N,M,K,E> Mapping<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM>
40 self.mul(x) 41 where SM: Storage<E,N,M>, SV: Storage<E,M,K> + Clone,
41 }
42 }
43
44 impl<'a, SM,SV,N,M,K,E> Apply<&'a Matrix<E,M,K,SV>> for Matrix<E,N,M,SM>
45 where SM: Storage<E,N,M>, SV: Storage<E,M,K>,
46 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
47 DefaultAllocator : Allocator<N,K>,
48 DefaultAllocator : Allocator<M,K>,
49 DefaultAllocator : Allocator<N,M>,
50 DefaultAllocator : Allocator<M,N> {
51 type Output = OMatrix<E,N,K>;
52
53 #[inline]
54 fn apply(&self, x : &'a Matrix<E,M,K,SV>) -> Self::Output {
55 self.mul(x)
56 }
57 }
58
59 impl<'a, SM,SV,N,M,K,E> Linear<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM>
60 where SM: Storage<E,N,M>, SV: Storage<E,M,K>,
61 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign, 42 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
62 DefaultAllocator : Allocator<N,K>, 43 DefaultAllocator : Allocator<N,K>,
63 DefaultAllocator : Allocator<M,K>, 44 DefaultAllocator : Allocator<M,K>,
64 DefaultAllocator : Allocator<N,M>, 45 DefaultAllocator : Allocator<N,M>,
65 DefaultAllocator : Allocator<M,N> { 46 DefaultAllocator : Allocator<M,N> {
66 type Codomain = OMatrix<E,N,K>; 47 type Codomain = OMatrix<E,N,K>;
48
49 #[inline]
50 fn apply<I : Instance<Matrix<E,M,K,SV>>>(
51 &self, x : I
52 ) -> Self::Codomain {
53 x.either(|owned| self.mul(owned), |refr| self.mul(refr))
54 }
55 }
56
57
58 impl<'a, SM,SV,N,M,K,E> Linear<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM>
59 where SM: Storage<E,N,M>, SV: Storage<E,M,K> + Clone,
60 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
61 DefaultAllocator : Allocator<N,K>,
62 DefaultAllocator : Allocator<M,K>,
63 DefaultAllocator : Allocator<N,M>,
64 DefaultAllocator : Allocator<M,N> {
67 } 65 }
68 66
69 impl<SM,SV1,SV2,N,M,K,E> GEMV<E, Matrix<E,M,K,SV1>, Matrix<E,N,K,SV2>> for Matrix<E,N,M,SM> 67 impl<SM,SV1,SV2,N,M,K,E> GEMV<E, Matrix<E,M,K,SV1>, Matrix<E,N,K,SV2>> for Matrix<E,N,M,SM>
70 where SM: Storage<E,N,M>, SV1: Storage<E,M,K>, SV2: StorageMut<E,N,K>, 68 where SM: Storage<E,N,M>, SV1: Storage<E,M,K> + Clone, SV2: StorageMut<E,N,K>,
71 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + Float, 69 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + Float,
72 DefaultAllocator : Allocator<N,K>, 70 DefaultAllocator : Allocator<N,K>,
73 DefaultAllocator : Allocator<M,K>, 71 DefaultAllocator : Allocator<M,K>,
74 DefaultAllocator : Allocator<N,M>, 72 DefaultAllocator : Allocator<N,M>,
75 DefaultAllocator : Allocator<M,N> { 73 DefaultAllocator : Allocator<M,N> {
76 74
77 #[inline] 75 #[inline]
78 fn gemv(&self, y : &mut Matrix<E,N,K,SV2>, α : E, x : &Matrix<E,M,K,SV1>, β : E) { 76 fn gemv<I : Instance<Matrix<E,M,K,SV1>>>(
79 Matrix::gemm(y, α, self, x, β) 77 &self, y : &mut Matrix<E,N,K,SV2>, α : E, x : I, β : E
80 } 78 ) {
81 79 x.eval(|x̃| Matrix::gemm(y, α, self, x̃, β))
82 #[inline] 80 }
83 fn apply_mut<'a>(&self, y : &mut Matrix<E,N,K,SV2>, x : &Matrix<E,M,K,SV1>) { 81
84 self.mul_to(x, y) 82 #[inline]
83 fn apply_mut<'a, I : Instance<Matrix<E,M,K,SV1>>>(&self, y : &mut Matrix<E,N,K,SV2>, x : I) {
84 x.eval(|x̃| self.mul_to(x̃, y))
85 } 85 }
86 } 86 }
87 87
88 impl<SM,SV1,M,E> AXPY<E, Vector<E,M,SV1>> for Vector<E,M,SM> 88 impl<SM,SV1,M,E> AXPY<E, Vector<E,M,SV1>> for Vector<E,M,SM>
89 where SM: StorageMut<E,M>, SV1: Storage<E,M>, 89 where SM: StorageMut<E,M> + Clone, SV1: Storage<E,M> + Clone,
90 M : Dim, E : Scalar + Zero + One + Float, 90 M : Dim, E : Scalar + Zero + One + Float,
91 DefaultAllocator : Allocator<M> { 91 DefaultAllocator : Allocator<M> {
92 92
93 #[inline] 93 #[inline]
94 fn axpy(&mut self, α : E, x : &Vector<E,M,SV1>, β : E) { 94 fn axpy<I : Instance<Vector<E,M,SV1>>>(&mut self, α : E, x : I, β : E) {
95 Matrix::axpy(self, α, x, β) 95 x.eval(|x̃| Matrix::axpy(self, α, x̃, β))
96 } 96 }
97 97
98 #[inline] 98 #[inline]
99 fn copy_from(&mut self, y : &Vector<E,M,SV1>) { 99 fn copy_from<I : Instance<Vector<E,M,SV1>>>(&mut self, y : I) {
100 Matrix::copy_from(self, y) 100 y.eval(|ỹ| Matrix::copy_from(self, ỹ))
101 } 101 }
102 } 102 }
103 103
104 impl<SM,M,E> Projection<E, Linfinity> for Vector<E,M,SM> 104 impl<SM,M,E> Projection<E, Linfinity> for Vector<E,M,SM>
105 where SM: StorageMut<E,M>, 105 where SM: StorageMut<E,M> + Clone,
106 M : Dim, E : Scalar + Zero + One + Float + RealField, 106 M : Dim, E : Scalar + Zero + One + Float + RealField,
107 DefaultAllocator : Allocator<M> { 107 DefaultAllocator : Allocator<M> {
108 #[inline] 108 #[inline]
109 fn proj_ball_mut(&mut self, ρ : E, _ : Linfinity) { 109 fn proj_ball_mut(&mut self, ρ : E, _ : Linfinity) {
110 self.iter_mut().for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ)) 110 self.iter_mut().for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ))
111 } 111 }
112 } 112 }
113 113
114 impl<'own,SV1,SV2,SM,N,M,K,E> Adjointable<Matrix<E,M,K,SV1>,Matrix<E,N,K,SV2>> 114 impl<'own,SV1,SV2,SM,N,M,K,E> Adjointable<Matrix<E,M,K,SV1>, Matrix<E,N,K,SV2>>
115 for Matrix<E,N,M,SM> 115 for Matrix<E,N,M,SM>
116 where SM: Storage<E,N,M>, SV1: Storage<E,M,K>, SV2: Storage<E,N,K>, 116 where SM: Storage<E,N,M>, SV1: Storage<E,M,K> + Clone, SV2: Storage<E,N,K> + Clone,
117 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + SimdComplexField, 117 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + SimdComplexField,
118 DefaultAllocator : Allocator<N,K>, 118 DefaultAllocator : Allocator<N,K>,
119 DefaultAllocator : Allocator<M,K>, 119 DefaultAllocator : Allocator<M,K>,
120 DefaultAllocator : Allocator<N,M>, 120 DefaultAllocator : Allocator<N,M>,
121 DefaultAllocator : Allocator<M,N> { 121 DefaultAllocator : Allocator<M,N> {
168 // TODO: should allow different input storages in `Euclidean`. 168 // TODO: should allow different input storages in `Euclidean`.
169 169
170 impl<E,M,S> Euclidean<E> 170 impl<E,M,S> Euclidean<E>
171 for Vector<E,M,S> 171 for Vector<E,M,S>
172 where M : Dim, 172 where M : Dim,
173 S : StorageMut<E,M>, 173 S : StorageMut<E,M> + Clone,
174 E : Float + Scalar + Zero + One + RealField, 174 E : Float + Scalar + Zero + One + RealField,
175 DefaultAllocator : Allocator<M> { 175 DefaultAllocator : Allocator<M> {
176 176
177 type Output = OVector<E, M>; 177 type Output = OVector<E, M>;
178 178
193 } 193 }
194 194
195 impl<E,M,S> StaticEuclidean<E> 195 impl<E,M,S> StaticEuclidean<E>
196 for Vector<E,M,S> 196 for Vector<E,M,S>
197 where M : DimName, 197 where M : DimName,
198 S : StorageMut<E,M>, 198 S : StorageMut<E,M> + Clone,
199 E : Float + Scalar + Zero + One + RealField, 199 E : Float + Scalar + Zero + One + RealField,
200 DefaultAllocator : Allocator<M> { 200 DefaultAllocator : Allocator<M> {
201 201
202 #[inline] 202 #[inline]
203 fn origin() -> OVector<E, M> { 203 fn origin() -> OVector<E, M> {

mercurial