Tue, 31 Dec 2024 09:12:43 -0500
Try to have Field as member type in Mappings etc.
5 | 1 | /*! |
2 | Integration with nalgebra. | |
3 | ||
4 | This module mainly implements [`Euclidean`], [`Norm`], [`Dot`], [`Linear`], etc. for [`nalgebra`] | |
5 | matrices and vectors. | |
6 | It also provides [`ToNalgebraRealField`] as a vomit-inducingly ugly workaround to nalgebra | |
7 | force-feeding its own versions of the same basic mathematical methods on `f32` and `f64` as | |
8 | [`num_traits`] does. | |
9 | */ | |
0 | 10 | |
11 | use nalgebra::{ | |
12 | Matrix, Storage, StorageMut, OMatrix, Dim, DefaultAllocator, Scalar, | |
13 | ClosedMul, ClosedAdd, SimdComplexField, Vector, OVector, RealField, | |
14 | LpNorm, UniformNorm | |
15 | }; | |
16 | use nalgebra::Norm as NalgebraNorm; | |
17 | use nalgebra::base::constraint::{ | |
18 | ShapeConstraint, SameNumberOfRows, SameNumberOfColumns | |
19 | }; | |
20 | use nalgebra::base::dimension::*; | |
21 | use nalgebra::base::allocator::Allocator; | |
22 | use std::ops::Mul; | |
23 | use num_traits::identities::{Zero, One}; | |
24 | use crate::linops::*; | |
5 | 25 | use crate::euclidean::*; |
0 | 26 | use crate::types::Float; |
27 | use crate::norms::*; | |
28 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
29 | impl<SM,SV,N,M,K,E> Apply<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
30 | where SM: Storage<E,N,M>, SV: Storage<E,M,K>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
31 | N : Dim, M : Dim, K : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
32 | DefaultAllocator : Allocator<E,N,K>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
33 | DefaultAllocator : Allocator<E,M,K>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
34 | DefaultAllocator : Allocator<E,N,M>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
35 | DefaultAllocator : Allocator<E,M,N> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
36 | type Output = OMatrix<E,N,K>; |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
37 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
38 | #[inline] |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
39 | fn apply(&self, x : Matrix<E,M,K,SV>) -> Self::Output { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
40 | self.mul(x) |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
41 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
42 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
43 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
44 | impl<'a, SM,SV,N,M,K,E> Apply<&'a Matrix<E,M,K,SV>> for Matrix<E,N,M,SM> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
45 | where SM: Storage<E,N,M>, SV: Storage<E,M,K>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
46 | N : Dim, M : Dim, K : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
47 | DefaultAllocator : Allocator<E,N,K>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
48 | DefaultAllocator : Allocator<E,M,K>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
49 | DefaultAllocator : Allocator<E,N,M>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
50 | DefaultAllocator : Allocator<E,M,N> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
51 | type Output = OMatrix<E,N,K>; |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
52 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
53 | #[inline] |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
54 | fn apply(&self, x : &'a Matrix<E,M,K,SV>) -> Self::Output { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
55 | self.mul(x) |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
56 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
57 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
58 | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
59 | impl<SM,SV,N,M,K,E> Linear<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM> |
0 | 60 | where SM: Storage<E,N,M>, SV: Storage<E,M,K>, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
61 | N : Dim, M : Dim, K : Dim, E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One, |
0 | 62 | DefaultAllocator : Allocator<E,N,K>, |
63 | DefaultAllocator : Allocator<E,M,K>, | |
64 | DefaultAllocator : Allocator<E,N,M>, | |
65 | DefaultAllocator : Allocator<E,M,N> { | |
66 | type Codomain = OMatrix<E,N,K>; | |
67 | } | |
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> | |
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, | |
72 | DefaultAllocator : Allocator<E,N,K>, | |
73 | DefaultAllocator : Allocator<E,M,K>, | |
74 | DefaultAllocator : Allocator<E,N,M>, | |
75 | DefaultAllocator : Allocator<E,M,N> { | |
76 | ||
77 | #[inline] | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
78 | fn gemv(&self, y : &mut Matrix<E,N,K,SV2>, α : E, x : Matrix<E,M,K,SV1>, β : E) { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
79 | Matrix::gemm(y, α, self, &x, β) |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
80 | } |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
81 | |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
82 | #[inline] |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
83 | fn apply_mut(&self, y : &mut Matrix<E,N,K,SV2>, x : Matrix<E,M,K,SV1>) { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
84 | self.mul_to(&x, y) |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
85 | } |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
86 | } |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
87 | |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
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> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
89 | where SM: Storage<E,N,M>, SV1: Storage<E,M,K>, SV2: StorageMut<E,N,K>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
90 | N : Dim, M : Dim, K : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
91 | DefaultAllocator : Allocator<E,N,K>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
92 | DefaultAllocator : Allocator<E,M,K>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
93 | DefaultAllocator : Allocator<E,N,M>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
94 | DefaultAllocator : Allocator<E,M,N> { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
95 | |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
96 | #[inline] |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
97 | fn gemv(&self, y : &mut Matrix<E,N,K,SV2>, α : E, x : &'a Matrix<E,M,K,SV1>, β : E) { |
0 | 98 | Matrix::gemm(y, α, self, x, β) |
99 | } | |
100 | ||
101 | #[inline] | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
102 | fn apply_mut(&self, y : &mut Matrix<E,N,K,SV2>, x : &'a Matrix<E,M,K,SV1>) { |
0 | 103 | self.mul_to(x, y) |
104 | } | |
105 | } | |
106 | ||
107 | impl<SM,SV1,M,E> AXPY<E, Vector<E,M,SV1>> for Vector<E,M,SM> | |
108 | where SM: StorageMut<E,M>, SV1: Storage<E,M>, | |
109 | M : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float, | |
110 | DefaultAllocator : Allocator<E,M> { | |
111 | ||
112 | #[inline] | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
113 | fn axpy(&mut self, α : E, x : Vector<E,M,SV1>, β : E) { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
114 | Matrix::axpy(self, α, &x, β) |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
115 | } |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
116 | |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
117 | #[inline] |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
118 | fn copy_from(&mut self, y : Vector<E,M,SV1>) { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
119 | Matrix::copy_from(self, &y) |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
120 | } |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
121 | } |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
122 | |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
123 | impl<'a, SM,SV1,M,E> AXPY<E, &'a Vector<E,M,SV1>> for Vector<E,M,SM> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
124 | where SM: StorageMut<E,M>, SV1: Storage<E,M>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
125 | M : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
126 | DefaultAllocator : Allocator<E,M> { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
127 | |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
128 | #[inline] |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
129 | fn axpy(&mut self, α : E, x : &'a Vector<E,M,SV1>, β : E) { |
0 | 130 | Matrix::axpy(self, α, x, β) |
131 | } | |
132 | ||
133 | #[inline] | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
134 | fn copy_from(&mut self, y : &'a Vector<E,M,SV1>) { |
0 | 135 | Matrix::copy_from(self, y) |
136 | } | |
137 | } | |
138 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
139 | impl<SM,M,E> Projection<Linfinity> for Vector<E,M,SM> |
0 | 140 | where SM: StorageMut<E,M>, |
141 | M : Dim, E : Scalar + ClosedMul + ClosedAdd + Zero + One + Float + RealField, | |
142 | DefaultAllocator : Allocator<E,M> { | |
143 | #[inline] | |
144 | fn proj_ball_mut(&mut self, ρ : E, _ : Linfinity) { | |
145 | self.iter_mut().for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ)) | |
146 | } | |
147 | } | |
148 | ||
149 | impl<'own,SV1,SV2,SM,N,M,K,E> Adjointable<Matrix<E,M,K,SV1>,Matrix<E,N,K,SV2>> | |
150 | for Matrix<E,N,M,SM> | |
151 | where SM: Storage<E,N,M>, SV1: Storage<E,M,K>, SV2: Storage<E,N,K>, | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
152 | N : Dim, M : Dim, K : Dim, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
153 | E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + SimdComplexField, |
0 | 154 | DefaultAllocator : Allocator<E,N,K>, |
155 | DefaultAllocator : Allocator<E,M,K>, | |
156 | DefaultAllocator : Allocator<E,N,M>, | |
157 | DefaultAllocator : Allocator<E,M,N> { | |
158 | type AdjointCodomain = OMatrix<E,M,K>; | |
159 | type Adjoint<'a> = OMatrix<E,M,N> where SM : 'a; | |
160 | ||
161 | #[inline] | |
162 | fn adjoint(&self) -> Self::Adjoint<'_> { | |
163 | Matrix::adjoint(self) | |
164 | } | |
165 | } | |
166 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
167 | impl<E,M,S,Si> Dot<Vector<E,M,Si>> |
0 | 168 | for Vector<E,M,S> |
169 | where M : Dim, | |
170 | E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One, | |
171 | S : Storage<E,M>, | |
172 | Si : Storage<E,M>, | |
173 | DefaultAllocator : Allocator<E,M> { | |
174 | ||
175 | #[inline] | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
176 | fn dot(&self, other : Vector<E,M,Si>) -> E { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
177 | Vector::<E,M,S>::dot(self, &other) |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
178 | } |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
179 | } |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
180 | |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
181 | impl<'a, E,M,S,Si> Dot<&'a Vector<E,M,Si>> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
182 | for Vector<E,M,S> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
183 | where M : Dim, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
184 | E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
185 | S : Storage<E,M>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
186 | Si : Storage<E,M>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
187 | DefaultAllocator : Allocator<E,M> { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
188 | |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
189 | #[inline] |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
190 | fn dot(&self, other : &'a Vector<E,M,Si>) -> E { |
0 | 191 | Vector::<E,M,S>::dot(self, other) |
192 | } | |
193 | } | |
194 | ||
195 | /// This function is [`nalgebra::EuclideanNorm::metric_distance`] without the `sqrt`. | |
196 | #[inline] | |
197 | fn metric_distance_squared<T, R1, C1, S1, R2, C2, S2>( | |
198 | /*ed: &EuclideanNorm,*/ | |
199 | m1: &Matrix<T, R1, C1, S1>, | |
200 | m2: &Matrix<T, R2, C2, S2>, | |
201 | ) -> T::SimdRealField | |
202 | where | |
203 | T: SimdComplexField, | |
204 | R1: Dim, | |
205 | C1: Dim, | |
206 | S1: Storage<T, R1, C1>, | |
207 | R2: Dim, | |
208 | C2: Dim, | |
209 | S2: Storage<T, R2, C2>, | |
210 | ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>, | |
211 | { | |
212 | m1.zip_fold(m2, T::SimdRealField::zero(), |acc, a, b| { | |
213 | let diff = a - b; | |
214 | acc + diff.simd_modulus_squared() | |
215 | }) | |
216 | } | |
217 | ||
218 | // TODO: should allow different input storages in `Euclidean`. | |
219 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
220 | impl<E,M,K,S> HasScalarField |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
221 | for Matrix<E,M,K,S> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
222 | where M : Dim, K : Dim, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
223 | S : Storage<E,M,K>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
224 | E : Float + Scalar, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
225 | DefaultAllocator : Allocator<E,M,K> { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
226 | type Field = E; |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
227 | } |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
228 | |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
229 | impl<E,M,S> Euclidean |
0 | 230 | for Vector<E,M,S> |
231 | where M : Dim, | |
232 | S : StorageMut<E,M>, | |
233 | E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, | |
234 | DefaultAllocator : Allocator<E,M> { | |
235 | ||
236 | type Output = OVector<E, M>; | |
237 | ||
238 | #[inline] | |
239 | fn similar_origin(&self) -> OVector<E, M> { | |
240 | OVector::zeros_generic(M::from_usize(self.len()), Const) | |
241 | } | |
242 | ||
243 | #[inline] | |
244 | fn norm2_squared(&self) -> E { | |
245 | Vector::<E,M,S>::norm_squared(self) | |
246 | } | |
247 | ||
248 | #[inline] | |
249 | fn dist2_squared(&self, other : &Self) -> E { | |
250 | metric_distance_squared(self, other) | |
251 | } | |
252 | } | |
253 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
254 | impl<E,M,S> StaticEuclidean |
0 | 255 | for Vector<E,M,S> |
256 | where M : DimName, | |
257 | S : StorageMut<E,M>, | |
258 | E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, | |
259 | DefaultAllocator : Allocator<E,M> { | |
260 | ||
261 | #[inline] | |
262 | fn origin() -> OVector<E, M> { | |
263 | OVector::zeros() | |
264 | } | |
265 | } | |
266 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
267 | impl<E,M,S> Norm<L1> |
0 | 268 | for Vector<E,M,S> |
269 | where M : Dim, | |
270 | S : StorageMut<E,M>, | |
271 | E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, | |
272 | DefaultAllocator : Allocator<E,M> { | |
273 | ||
274 | #[inline] | |
275 | fn norm(&self, _ : L1) -> E { | |
276 | LpNorm(1).norm(self) | |
277 | } | |
278 | } | |
279 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
280 | impl<E,M,S> Dist<L1> |
0 | 281 | for Vector<E,M,S> |
282 | where M : Dim, | |
283 | S : StorageMut<E,M>, | |
284 | E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, | |
285 | DefaultAllocator : Allocator<E,M> { | |
286 | #[inline] | |
287 | fn dist(&self, other : &Self, _ : L1) -> E { | |
288 | LpNorm(1).metric_distance(self, other) | |
289 | } | |
290 | } | |
291 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
292 | impl<E,M,S> Norm<L2> |
0 | 293 | for Vector<E,M,S> |
294 | where M : Dim, | |
295 | S : StorageMut<E,M>, | |
296 | E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, | |
297 | DefaultAllocator : Allocator<E,M> { | |
298 | ||
299 | #[inline] | |
300 | fn norm(&self, _ : L2) -> E { | |
301 | LpNorm(2).norm(self) | |
302 | } | |
303 | } | |
304 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
305 | impl<E,M,S> Dist<L2> |
0 | 306 | for Vector<E,M,S> |
307 | where M : Dim, | |
308 | S : StorageMut<E,M>, | |
309 | E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, | |
310 | DefaultAllocator : Allocator<E,M> { | |
311 | #[inline] | |
312 | fn dist(&self, other : &Self, _ : L2) -> E { | |
313 | LpNorm(2).metric_distance(self, other) | |
314 | } | |
315 | } | |
316 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
317 | impl<E,M,S> Norm<Linfinity> |
0 | 318 | for Vector<E,M,S> |
319 | where M : Dim, | |
320 | S : StorageMut<E,M>, | |
321 | E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, | |
322 | DefaultAllocator : Allocator<E,M> { | |
323 | ||
324 | #[inline] | |
325 | fn norm(&self, _ : Linfinity) -> E { | |
326 | UniformNorm.norm(self) | |
327 | } | |
328 | } | |
329 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
330 | impl<E,M,S> Dist<Linfinity> |
0 | 331 | for Vector<E,M,S> |
332 | where M : Dim, | |
333 | S : StorageMut<E,M>, | |
334 | E : Float + Scalar + ClosedMul + ClosedAdd + Zero + One + RealField, | |
335 | DefaultAllocator : Allocator<E,M> { | |
336 | #[inline] | |
337 | fn dist(&self, other : &Self, _ : Linfinity) -> E { | |
338 | UniformNorm.metric_distance(self, other) | |
339 | } | |
340 | } | |
341 | ||
5 | 342 | /// Helper trait to hide the symbols of [`nalgebra::RealField`]. |
343 | /// | |
344 | /// By assuming `ToNalgebraRealField` intead of `nalgebra::RealField` as a trait bound, | |
345 | /// functions can piggyback `nalgebra::RealField` without exponsing themselves to it. | |
346 | /// Thus methods from [`num_traits`] can be used directly without similarly named methods | |
347 | /// from [`nalgebra`] conflicting with them. Only when absolutely necessary to work with | |
348 | /// nalgebra, one can convert to the nalgebra view of the same type using the methods of | |
349 | /// this trait. | |
0 | 350 | pub trait ToNalgebraRealField : Float { |
5 | 351 | /// The nalgebra type corresponding to this type. Usually same as `Self`. |
352 | /// | |
353 | /// This type only carries `nalgebra` traits. | |
0 | 354 | type NalgebraType : RealField; |
5 | 355 | /// The “mixed” type corresponding to this type. Usually same as `Self`. |
356 | /// | |
357 | /// This type carries both `num_traits` and `nalgebra` traits. | |
0 | 358 | type MixedType : RealField + Float; |
359 | ||
5 | 360 | /// Convert to the nalgebra view of `self`. |
0 | 361 | fn to_nalgebra(self) -> Self::NalgebraType; |
5 | 362 | |
363 | /// Convert to the mixed (nalgebra and num_traits) view of `self`. | |
0 | 364 | fn to_nalgebra_mixed(self) -> Self::MixedType; |
365 | ||
5 | 366 | /// Convert from the nalgebra view of `self`. |
0 | 367 | fn from_nalgebra(t : Self::NalgebraType) -> Self; |
5 | 368 | |
369 | /// Convert from the mixed (nalgebra and num_traits) view to `self`. | |
0 | 370 | fn from_nalgebra_mixed(t : Self::MixedType) -> Self; |
371 | } | |
372 | ||
373 | impl ToNalgebraRealField for f32 { | |
374 | type NalgebraType = f32; | |
375 | type MixedType = f32; | |
376 | ||
377 | #[inline] | |
378 | fn to_nalgebra(self) -> Self::NalgebraType { self } | |
379 | ||
380 | #[inline] | |
381 | fn to_nalgebra_mixed(self) -> Self::MixedType { self } | |
382 | ||
383 | #[inline] | |
384 | fn from_nalgebra(t : Self::NalgebraType) -> Self { t } | |
385 | ||
386 | #[inline] | |
387 | fn from_nalgebra_mixed(t : Self::MixedType) -> Self { t } | |
388 | ||
389 | } | |
390 | ||
391 | impl ToNalgebraRealField for f64 { | |
392 | type NalgebraType = f64; | |
393 | type MixedType = f64; | |
394 | ||
395 | #[inline] | |
396 | fn to_nalgebra(self) -> Self::NalgebraType { self } | |
397 | ||
398 | #[inline] | |
399 | fn to_nalgebra_mixed(self) -> Self::MixedType { self } | |
400 | ||
401 | #[inline] | |
402 | fn from_nalgebra(t : Self::NalgebraType) -> Self { t } | |
403 | ||
404 | #[inline] | |
405 | fn from_nalgebra_mixed(t : Self::MixedType) -> Self { t } | |
406 | } | |
407 |