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