src/nalgebra_support.rs

branch
dev
changeset 81
d2acaaddd9af
parent 13
465fa2121ccb
equal deleted inserted replaced
49:edb95d2b83cc 81:d2acaaddd9af
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<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 : Float + Scalar + ClosedMul + ClosedAdd + Zero + One,
62 DefaultAllocator : Allocator<E,N,K>, 62 DefaultAllocator : Allocator<E,N,K>,
63 DefaultAllocator : Allocator<E,M,K>, 63 DefaultAllocator : Allocator<E,M,K>,
64 DefaultAllocator : Allocator<E,N,M>, 64 DefaultAllocator : Allocator<E,N,M>,
65 DefaultAllocator : Allocator<E,M,N> { 65 DefaultAllocator : Allocator<E,M,N> {
66 type Codomain = OMatrix<E,N,K>; 66 type Codomain = OMatrix<E,N,K>;
73 DefaultAllocator : Allocator<E,M,K>, 73 DefaultAllocator : Allocator<E,M,K>,
74 DefaultAllocator : Allocator<E,N,M>, 74 DefaultAllocator : Allocator<E,N,M>,
75 DefaultAllocator : Allocator<E,M,N> { 75 DefaultAllocator : Allocator<E,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, β)
80 }
81
82 #[inline]
83 fn apply_mut(&self, y : &mut Matrix<E,N,K,SV2>, x : Matrix<E,M,K,SV1>) {
84 self.mul_to(&x, y)
85 }
86 }
87
88 impl<'a, SM,SV1,SV2,N,M,K,E> GEMV<E, &'a Matrix<E,M,K,SV1>, Matrix<E,N,K,SV2>> for Matrix<E,N,M,SM>
89 where SM: Storage<E,N,M>, SV1: Storage<E,M,K>, SV2: StorageMut<E,N,K>,
90 N : Dim, M : Dim, K : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float,
91 DefaultAllocator : Allocator<E,N,K>,
92 DefaultAllocator : Allocator<E,M,K>,
93 DefaultAllocator : Allocator<E,N,M>,
94 DefaultAllocator : Allocator<E,M,N> {
95
96 #[inline]
97 fn gemv(&self, y : &mut Matrix<E,N,K,SV2>, α : E, x : &'a Matrix<E,M,K,SV1>, β : E) {
79 Matrix::gemm(y, α, self, x, β) 98 Matrix::gemm(y, α, self, x, β)
80 } 99 }
81 100
82 #[inline] 101 #[inline]
83 fn apply_mut<'a>(&self, y : &mut Matrix<E,N,K,SV2>, x : &Matrix<E,M,K,SV1>) { 102 fn apply_mut(&self, y : &mut Matrix<E,N,K,SV2>, x : &'a Matrix<E,M,K,SV1>) {
84 self.mul_to(x, y) 103 self.mul_to(x, y)
85 } 104 }
86 } 105 }
87 106
88 impl<SM,SV1,M,E> AXPY<E, Vector<E,M,SV1>> for Vector<E,M,SM> 107 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>, 108 where SM: StorageMut<E,M>, SV1: Storage<E,M>,
90 M : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float, 109 M : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float,
91 DefaultAllocator : Allocator<E,M> { 110 DefaultAllocator : Allocator<E,M> {
92 111
93 #[inline] 112 #[inline]
94 fn axpy(&mut self, α : E, x : &Vector<E,M,SV1>, β : E) { 113 fn axpy(&mut self, α : E, x : Vector<E,M,SV1>, β : E) {
114 Matrix::axpy(self, α, &x, β)
115 }
116
117 #[inline]
118 fn copy_from(&mut self, y : Vector<E,M,SV1>) {
119 Matrix::copy_from(self, &y)
120 }
121 }
122
123 impl<'a, SM,SV1,M,E> AXPY<E, &'a Vector<E,M,SV1>> for Vector<E,M,SM>
124 where SM: StorageMut<E,M>, SV1: Storage<E,M>,
125 M : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float,
126 DefaultAllocator : Allocator<E,M> {
127
128 #[inline]
129 fn axpy(&mut self, α : E, x : &'a Vector<E,M,SV1>, β : E) {
95 Matrix::axpy(self, α, x, β) 130 Matrix::axpy(self, α, x, β)
96 } 131 }
97 132
98 #[inline] 133 #[inline]
99 fn copy_from(&mut self, y : &Vector<E,M,SV1>) { 134 fn copy_from(&mut self, y : &'a Vector<E,M,SV1>) {
100 Matrix::copy_from(self, y) 135 Matrix::copy_from(self, y)
101 } 136 }
102 } 137 }
103 138
104 impl<SM,M,E> Projection<E, Linfinity> for Vector<E,M,SM> 139 impl<SM,M,E> Projection<Linfinity> for Vector<E,M,SM>
105 where SM: StorageMut<E,M>, 140 where SM: StorageMut<E,M>,
106 M : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float + RealField, 141 M : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float + RealField,
107 DefaultAllocator : Allocator<E,M> { 142 DefaultAllocator : Allocator<E,M> {
108 #[inline] 143 #[inline]
109 fn proj_ball_mut(&mut self, ρ : E, _ : Linfinity) { 144 fn proj_ball_mut(&mut self, ρ : E, _ : Linfinity) {
112 } 147 }
113 148
114 impl<'own,SV1,SV2,SM,N,M,K,E> Adjointable<Matrix<E,M,K,SV1>,Matrix<E,N,K,SV2>> 149 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> 150 for Matrix<E,N,M,SM>
116 where SM: Storage<E,N,M>, SV1: Storage<E,M,K>, SV2: Storage<E,N,K>, 151 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, 152 N : Dim, M : Dim, K : Dim,
153 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + SimdComplexField,
118 DefaultAllocator : Allocator<E,N,K>, 154 DefaultAllocator : Allocator<E,N,K>,
119 DefaultAllocator : Allocator<E,M,K>, 155 DefaultAllocator : Allocator<E,M,K>,
120 DefaultAllocator : Allocator<E,N,M>, 156 DefaultAllocator : Allocator<E,N,M>,
121 DefaultAllocator : Allocator<E,M,N> { 157 DefaultAllocator : Allocator<E,M,N> {
122 type AdjointCodomain = OMatrix<E,M,K>; 158 type AdjointCodomain = OMatrix<E,M,K>;
126 fn adjoint(&self) -> Self::Adjoint<'_> { 162 fn adjoint(&self) -> Self::Adjoint<'_> {
127 Matrix::adjoint(self) 163 Matrix::adjoint(self)
128 } 164 }
129 } 165 }
130 166
131 impl<E,M,S,Si> Dot<Vector<E,M,Si>,E> 167 impl<E,M,S,Si> Dot<Vector<E,M,Si>>
132 for Vector<E,M,S> 168 for Vector<E,M,S>
133 where M : Dim, 169 where M : Dim,
134 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One, 170 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One,
135 S : Storage<E,M>, 171 S : Storage<E,M>,
136 Si : Storage<E,M>, 172 Si : Storage<E,M>,
137 DefaultAllocator : Allocator<E,M> { 173 DefaultAllocator : Allocator<E,M> {
138 174
139 #[inline] 175 #[inline]
140 fn dot(&self, other : &Vector<E,M,Si>) -> E { 176 fn dot(&self, other : Vector<E,M,Si>) -> E {
177 Vector::<E,M,S>::dot(self, &other)
178 }
179 }
180
181 impl<'a, E,M,S,Si> Dot<&'a Vector<E,M,Si>>
182 for Vector<E,M,S>
183 where M : Dim,
184 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One,
185 S : Storage<E,M>,
186 Si : Storage<E,M>,
187 DefaultAllocator : Allocator<E,M> {
188
189 #[inline]
190 fn dot(&self, other : &'a Vector<E,M,Si>) -> E {
141 Vector::<E,M,S>::dot(self, other) 191 Vector::<E,M,S>::dot(self, other)
142 } 192 }
143 } 193 }
144 194
145 /// This function is [`nalgebra::EuclideanNorm::metric_distance`] without the `sqrt`. 195 /// This function is [`nalgebra::EuclideanNorm::metric_distance`] without the `sqrt`.
165 }) 215 })
166 } 216 }
167 217
168 // TODO: should allow different input storages in `Euclidean`. 218 // TODO: should allow different input storages in `Euclidean`.
169 219
170 impl<E,M,S> Euclidean<E> 220 impl<E,M,K,S> HasScalarField
221 for Matrix<E,M,K,S>
222 where M : Dim, K : Dim,
223 S : Storage<E,M,K>,
224 E : Float + Scalar,
225 DefaultAllocator : Allocator<E,M,K> {
226 type Field = E;
227 }
228
229 impl<E,M,S> Euclidean
171 for Vector<E,M,S> 230 for Vector<E,M,S>
172 where M : Dim, 231 where M : Dim,
173 S : StorageMut<E,M>, 232 S : StorageMut<E,M>,
174 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 233 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField,
175 DefaultAllocator : Allocator<E,M> { 234 DefaultAllocator : Allocator<E,M> {
190 fn dist2_squared(&self, other : &Self) -> E { 249 fn dist2_squared(&self, other : &Self) -> E {
191 metric_distance_squared(self, other) 250 metric_distance_squared(self, other)
192 } 251 }
193 } 252 }
194 253
195 impl<E,M,S> StaticEuclidean<E> 254 impl<E,M,S> StaticEuclidean
196 for Vector<E,M,S> 255 for Vector<E,M,S>
197 where M : DimName, 256 where M : DimName,
198 S : StorageMut<E,M>, 257 S : StorageMut<E,M>,
199 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 258 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField,
200 DefaultAllocator : Allocator<E,M> { 259 DefaultAllocator : Allocator<E,M> {
203 fn origin() -> OVector<E, M> { 262 fn origin() -> OVector<E, M> {
204 OVector::zeros() 263 OVector::zeros()
205 } 264 }
206 } 265 }
207 266
208 impl<E,M,S> Norm<E, L1> 267 impl<E,M,S> Norm<L1>
209 for Vector<E,M,S> 268 for Vector<E,M,S>
210 where M : Dim, 269 where M : Dim,
211 S : StorageMut<E,M>, 270 S : StorageMut<E,M>,
212 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 271 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField,
213 DefaultAllocator : Allocator<E,M> { 272 DefaultAllocator : Allocator<E,M> {
216 fn norm(&self, _ : L1) -> E { 275 fn norm(&self, _ : L1) -> E {
217 LpNorm(1).norm(self) 276 LpNorm(1).norm(self)
218 } 277 }
219 } 278 }
220 279
221 impl<E,M,S> Dist<E, L1> 280 impl<E,M,S> Dist<L1>
222 for Vector<E,M,S> 281 for Vector<E,M,S>
223 where M : Dim, 282 where M : Dim,
224 S : StorageMut<E,M>, 283 S : StorageMut<E,M>,
225 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 284 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField,
226 DefaultAllocator : Allocator<E,M> { 285 DefaultAllocator : Allocator<E,M> {
228 fn dist(&self, other : &Self, _ : L1) -> E { 287 fn dist(&self, other : &Self, _ : L1) -> E {
229 LpNorm(1).metric_distance(self, other) 288 LpNorm(1).metric_distance(self, other)
230 } 289 }
231 } 290 }
232 291
233 impl<E,M,S> Norm<E, L2> 292 impl<E,M,S> Norm<L2>
234 for Vector<E,M,S> 293 for Vector<E,M,S>
235 where M : Dim, 294 where M : Dim,
236 S : StorageMut<E,M>, 295 S : StorageMut<E,M>,
237 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 296 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField,
238 DefaultAllocator : Allocator<E,M> { 297 DefaultAllocator : Allocator<E,M> {
241 fn norm(&self, _ : L2) -> E { 300 fn norm(&self, _ : L2) -> E {
242 LpNorm(2).norm(self) 301 LpNorm(2).norm(self)
243 } 302 }
244 } 303 }
245 304
246 impl<E,M,S> Dist<E, L2> 305 impl<E,M,S> Dist<L2>
247 for Vector<E,M,S> 306 for Vector<E,M,S>
248 where M : Dim, 307 where M : Dim,
249 S : StorageMut<E,M>, 308 S : StorageMut<E,M>,
250 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 309 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField,
251 DefaultAllocator : Allocator<E,M> { 310 DefaultAllocator : Allocator<E,M> {
253 fn dist(&self, other : &Self, _ : L2) -> E { 312 fn dist(&self, other : &Self, _ : L2) -> E {
254 LpNorm(2).metric_distance(self, other) 313 LpNorm(2).metric_distance(self, other)
255 } 314 }
256 } 315 }
257 316
258 impl<E,M,S> Norm<E, Linfinity> 317 impl<E,M,S> Norm<Linfinity>
259 for Vector<E,M,S> 318 for Vector<E,M,S>
260 where M : Dim, 319 where M : Dim,
261 S : StorageMut<E,M>, 320 S : StorageMut<E,M>,
262 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 321 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField,
263 DefaultAllocator : Allocator<E,M> { 322 DefaultAllocator : Allocator<E,M> {
266 fn norm(&self, _ : Linfinity) -> E { 325 fn norm(&self, _ : Linfinity) -> E {
267 UniformNorm.norm(self) 326 UniformNorm.norm(self)
268 } 327 }
269 } 328 }
270 329
271 impl<E,M,S> Dist<E, Linfinity> 330 impl<E,M,S> Dist<Linfinity>
272 for Vector<E,M,S> 331 for Vector<E,M,S>
273 where M : Dim, 332 where M : Dim,
274 S : StorageMut<E,M>, 333 S : StorageMut<E,M>,
275 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, 334 E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField,
276 DefaultAllocator : Allocator<E,M> { 335 DefaultAllocator : Allocator<E,M> {

mercurial