src/nalgebra_support.rs

branch
dev
changeset 56
5e3c1874797d
parent 13
465fa2121ccb
child 59
9226980e45a7
equal deleted inserted replaced
55:7b2ee3e84c5f 56:5e3c1874797d
8 [`num_traits`] does. 8 [`num_traits`] does.
9 */ 9 */
10 10
11 use nalgebra::{ 11 use nalgebra::{
12 Matrix, Storage, StorageMut, OMatrix, Dim, DefaultAllocator, Scalar, 12 Matrix, Storage, StorageMut, OMatrix, Dim, DefaultAllocator, Scalar,
13 ClosedMul, ClosedAdd, SimdComplexField, Vector, OVector, RealField, 13 ClosedAddAssign, ClosedMulAssign, SimdComplexField, Vector, OVector, RealField,
14 LpNorm, UniformNorm 14 LpNorm, UniformNorm
15 }; 15 };
16 use nalgebra::Norm as NalgebraNorm; 16 use nalgebra::Norm as NalgebraNorm;
17 use nalgebra::base::constraint::{ 17 use nalgebra::base::constraint::{
18 ShapeConstraint, SameNumberOfRows, SameNumberOfColumns 18 ShapeConstraint, SameNumberOfRows, SameNumberOfColumns
26 use crate::types::Float; 26 use crate::types::Float;
27 use crate::norms::*; 27 use crate::norms::*;
28 28
29 impl<SM,SV,N,M,K,E> Apply<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM> 29 impl<SM,SV,N,M,K,E> Apply<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM>
30 where SM: Storage<E,N,M>, SV: Storage<E,M,K>, 30 where SM: Storage<E,N,M>, SV: Storage<E,M,K>,
31 N : Dim, M : Dim, K : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One, 31 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
32 DefaultAllocator : Allocator<E,N,K>, 32 DefaultAllocator : Allocator<N,K>,
33 DefaultAllocator : Allocator<E,M,K>, 33 DefaultAllocator : Allocator<M,K>,
34 DefaultAllocator : Allocator<E,N,M>, 34 DefaultAllocator : Allocator<N,M>,
35 DefaultAllocator : Allocator<E,M,N> { 35 DefaultAllocator : Allocator<M,N> {
36 type Output = OMatrix<E,N,K>; 36 type Output = OMatrix<E,N,K>;
37 37
38 #[inline] 38 #[inline]
39 fn apply(&self, x : Matrix<E,M,K,SV>) -> Self::Output { 39 fn apply(&self, x : Matrix<E,M,K,SV>) -> Self::Output {
40 self.mul(x) 40 self.mul(x)
41 } 41 }
42 } 42 }
43 43
44 impl<'a, SM,SV,N,M,K,E> Apply<&'a Matrix<E,M,K,SV>> for Matrix<E,N,M,SM> 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>, 45 where SM: Storage<E,N,M>, SV: Storage<E,M,K>,
46 N : Dim, M : Dim, K : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One, 46 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
47 DefaultAllocator : Allocator<E,N,K>, 47 DefaultAllocator : Allocator<N,K>,
48 DefaultAllocator : Allocator<E,M,K>, 48 DefaultAllocator : Allocator<M,K>,
49 DefaultAllocator : Allocator<E,N,M>, 49 DefaultAllocator : Allocator<N,M>,
50 DefaultAllocator : Allocator<E,M,N> { 50 DefaultAllocator : Allocator<M,N> {
51 type Output = OMatrix<E,N,K>; 51 type Output = OMatrix<E,N,K>;
52 52
53 #[inline] 53 #[inline]
54 fn apply(&self, x : &'a Matrix<E,M,K,SV>) -> Self::Output { 54 fn apply(&self, x : &'a Matrix<E,M,K,SV>) -> Self::Output {
55 self.mul(x) 55 self.mul(x)
56 } 56 }
57 } 57 }
58 58
59 impl<'a, SM,SV,N,M,K,E> Linear<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM> 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>, 60 where SM: Storage<E,N,M>, SV: Storage<E,M,K>,
61 N : Dim, M : Dim, K : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One, 61 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
62 DefaultAllocator : Allocator<E,N,K>, 62 DefaultAllocator : Allocator<N,K>,
63 DefaultAllocator : Allocator<E,M,K>, 63 DefaultAllocator : Allocator<M,K>,
64 DefaultAllocator : Allocator<E,N,M>, 64 DefaultAllocator : Allocator<N,M>,
65 DefaultAllocator : Allocator<E,M,N> { 65 DefaultAllocator : Allocator<M,N> {
66 type Codomain = OMatrix<E,N,K>; 66 type Codomain = OMatrix<E,N,K>;
67 } 67 }
68 68
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> 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>
70 where SM: Storage<E,N,M>, SV1: Storage<E,M,K>, SV2: StorageMut<E,N,K>, 70 where SM: Storage<E,N,M>, SV1: Storage<E,M,K>, SV2: StorageMut<E,N,K>,
71 N : Dim, M : Dim, K : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float, 71 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + Float,
72 DefaultAllocator : Allocator<E,N,K>, 72 DefaultAllocator : Allocator<N,K>,
73 DefaultAllocator : Allocator<E,M,K>, 73 DefaultAllocator : Allocator<M,K>,
74 DefaultAllocator : Allocator<E,N,M>, 74 DefaultAllocator : Allocator<N,M>,
75 DefaultAllocator : Allocator<E,M,N> { 75 DefaultAllocator : Allocator<M,N> {
76 76
77 #[inline] 77 #[inline]
78 fn gemv(&self, y : &mut Matrix<E,N,K,SV2>, α : E, x : &Matrix<E,M,K,SV1>, β : E) { 78 fn gemv(&self, y : &mut Matrix<E,N,K,SV2>, α : E, x : &Matrix<E,M,K,SV1>, β : E) {
79 Matrix::gemm(y, α, self, x, β) 79 Matrix::gemm(y, α, self, x, β)
80 } 80 }
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>, SV1: Storage<E,M>,
90 M : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float, 90 M : Dim, E : Scalar + Zero + One + Float,
91 DefaultAllocator : Allocator<E,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(&mut self, α : E, x : &Vector<E,M,SV1>, β : E) {
95 Matrix::axpy(self, α, x, β) 95 Matrix::axpy(self, α, x, β)
96 } 96 }
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>,
106 M : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float + RealField, 106 M : Dim, E : Scalar + Zero + One + Float + RealField,
107 DefaultAllocator : Allocator<E,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>, SV2: Storage<E,N,K>,
117 N : Dim, M : Dim, K : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + SimdComplexField, 117 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + SimdComplexField,
118 DefaultAllocator : Allocator<E,N,K>, 118 DefaultAllocator : Allocator<N,K>,
119 DefaultAllocator : Allocator<E,M,K>, 119 DefaultAllocator : Allocator<M,K>,
120 DefaultAllocator : Allocator<E,N,M>, 120 DefaultAllocator : Allocator<N,M>,
121 DefaultAllocator : Allocator<E,M,N> { 121 DefaultAllocator : Allocator<M,N> {
122 type AdjointCodomain = OMatrix<E,M,K>; 122 type AdjointCodomain = OMatrix<E,M,K>;
123 type Adjoint<'a> = OMatrix<E,M,N> where SM : 'a; 123 type Adjoint<'a> = OMatrix<E,M,N> where SM : 'a;
124 124
125 #[inline] 125 #[inline]
126 fn adjoint(&self) -> Self::Adjoint<'_> { 126 fn adjoint(&self) -> Self::Adjoint<'_> {
129 } 129 }
130 130
131 impl<E,M,S,Si> Dot<Vector<E,M,Si>,E> 131 impl<E,M,S,Si> Dot<Vector<E,M,Si>,E>
132 for Vector<E,M,S> 132 for Vector<E,M,S>
133 where M : Dim, 133 where M : Dim,
134 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One, 134 E : Float + Scalar + Zero + One,
135 S : Storage<E,M>, 135 S : Storage<E,M>,
136 Si : Storage<E,M>, 136 Si : Storage<E,M>,
137 DefaultAllocator : Allocator<E,M> { 137 DefaultAllocator : Allocator<M> {
138 138
139 #[inline] 139 #[inline]
140 fn dot(&self, other : &Vector<E,M,Si>) -> E { 140 fn dot(&self, other : &Vector<E,M,Si>) -> E {
141 Vector::<E,M,S>::dot(self, other) 141 Vector::<E,M,S>::dot(self, other)
142 } 142 }
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>,
174 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 174 E : Float + Scalar + Zero + One + RealField,
175 DefaultAllocator : Allocator<E,M> { 175 DefaultAllocator : Allocator<M> {
176 176
177 type Output = OVector<E, M>; 177 type Output = OVector<E, M>;
178 178
179 #[inline] 179 #[inline]
180 fn similar_origin(&self) -> OVector<E, M> { 180 fn similar_origin(&self) -> OVector<E, M> {
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>,
199 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 199 E : Float + Scalar + Zero + One + RealField,
200 DefaultAllocator : Allocator<E,M> { 200 DefaultAllocator : Allocator<M> {
201 201
202 #[inline] 202 #[inline]
203 fn origin() -> OVector<E, M> { 203 fn origin() -> OVector<E, M> {
204 OVector::zeros() 204 OVector::zeros()
205 } 205 }
207 207
208 impl<E,M,S> Norm<E, L1> 208 impl<E,M,S> Norm<E, L1>
209 for Vector<E,M,S> 209 for Vector<E,M,S>
210 where M : Dim, 210 where M : Dim,
211 S : StorageMut<E,M>, 211 S : StorageMut<E,M>,
212 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 212 E : Float + Scalar + Zero + One + RealField,
213 DefaultAllocator : Allocator<E,M> { 213 DefaultAllocator : Allocator<M> {
214 214
215 #[inline] 215 #[inline]
216 fn norm(&self, _ : L1) -> E { 216 fn norm(&self, _ : L1) -> E {
217 LpNorm(1).norm(self) 217 LpNorm(1).norm(self)
218 } 218 }
220 220
221 impl<E,M,S> Dist<E, L1> 221 impl<E,M,S> Dist<E, L1>
222 for Vector<E,M,S> 222 for Vector<E,M,S>
223 where M : Dim, 223 where M : Dim,
224 S : StorageMut<E,M>, 224 S : StorageMut<E,M>,
225 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 225 E : Float + Scalar + Zero + One + RealField,
226 DefaultAllocator : Allocator<E,M> { 226 DefaultAllocator : Allocator<M> {
227 #[inline] 227 #[inline]
228 fn dist(&self, other : &Self, _ : L1) -> E { 228 fn dist(&self, other : &Self, _ : L1) -> E {
229 LpNorm(1).metric_distance(self, other) 229 LpNorm(1).metric_distance(self, other)
230 } 230 }
231 } 231 }
232 232
233 impl<E,M,S> Norm<E, L2> 233 impl<E,M,S> Norm<E, L2>
234 for Vector<E,M,S> 234 for Vector<E,M,S>
235 where M : Dim, 235 where M : Dim,
236 S : StorageMut<E,M>, 236 S : StorageMut<E,M>,
237 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 237 E : Float + Scalar + Zero + One + RealField,
238 DefaultAllocator : Allocator<E,M> { 238 DefaultAllocator : Allocator<M> {
239 239
240 #[inline] 240 #[inline]
241 fn norm(&self, _ : L2) -> E { 241 fn norm(&self, _ : L2) -> E {
242 LpNorm(2).norm(self) 242 LpNorm(2).norm(self)
243 } 243 }
245 245
246 impl<E,M,S> Dist<E, L2> 246 impl<E,M,S> Dist<E, L2>
247 for Vector<E,M,S> 247 for Vector<E,M,S>
248 where M : Dim, 248 where M : Dim,
249 S : StorageMut<E,M>, 249 S : StorageMut<E,M>,
250 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 250 E : Float + Scalar + Zero + One + RealField,
251 DefaultAllocator : Allocator<E,M> { 251 DefaultAllocator : Allocator<M> {
252 #[inline] 252 #[inline]
253 fn dist(&self, other : &Self, _ : L2) -> E { 253 fn dist(&self, other : &Self, _ : L2) -> E {
254 LpNorm(2).metric_distance(self, other) 254 LpNorm(2).metric_distance(self, other)
255 } 255 }
256 } 256 }
257 257
258 impl<E,M,S> Norm<E, Linfinity> 258 impl<E,M,S> Norm<E, Linfinity>
259 for Vector<E,M,S> 259 for Vector<E,M,S>
260 where M : Dim, 260 where M : Dim,
261 S : StorageMut<E,M>, 261 S : StorageMut<E,M>,
262 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 262 E : Float + Scalar + Zero + One + RealField,
263 DefaultAllocator : Allocator<E,M> { 263 DefaultAllocator : Allocator<M> {
264 264
265 #[inline] 265 #[inline]
266 fn norm(&self, _ : Linfinity) -> E { 266 fn norm(&self, _ : Linfinity) -> E {
267 UniformNorm.norm(self) 267 UniformNorm.norm(self)
268 } 268 }
270 270
271 impl<E,M,S> Dist<E, Linfinity> 271 impl<E,M,S> Dist<E, Linfinity>
272 for Vector<E,M,S> 272 for Vector<E,M,S>
273 where M : Dim, 273 where M : Dim,
274 S : StorageMut<E,M>, 274 S : StorageMut<E,M>,
275 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 275 E : Float + Scalar + Zero + One + RealField,
276 DefaultAllocator : Allocator<E,M> { 276 DefaultAllocator : Allocator<M> {
277 #[inline] 277 #[inline]
278 fn dist(&self, other : &Self, _ : Linfinity) -> E { 278 fn dist(&self, other : &Self, _ : Linfinity) -> E {
279 UniformNorm.metric_distance(self, other) 279 UniformNorm.metric_distance(self, other)
280 } 280 }
281 } 281 }

mercurial