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