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