Sat, 21 Dec 2024 23:32:20 -0500
Move origin stuff to AXPY form Euclidean
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> { |
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 | impl<E,M,S,Si> Dot<Vector<E,M,Si>,E> | |
157 | for Vector<E,M,S> | |
158 | where M : Dim, | |
56
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
159 | E : Float + Scalar + Zero + One, |
0 | 160 | S : Storage<E,M>, |
161 | Si : Storage<E,M>, | |
56
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
162 | DefaultAllocator : Allocator<M> { |
0 | 163 | |
164 | #[inline] | |
165 | fn dot(&self, other : &Vector<E,M,Si>) -> E { | |
166 | Vector::<E,M,S>::dot(self, other) | |
167 | } | |
168 | } | |
169 | ||
170 | /// This function is [`nalgebra::EuclideanNorm::metric_distance`] without the `sqrt`. | |
171 | #[inline] | |
172 | fn metric_distance_squared<T, R1, C1, S1, R2, C2, S2>( | |
173 | /*ed: &EuclideanNorm,*/ | |
174 | m1: &Matrix<T, R1, C1, S1>, | |
175 | m2: &Matrix<T, R2, C2, S2>, | |
176 | ) -> T::SimdRealField | |
177 | where | |
178 | T: SimdComplexField, | |
179 | R1: Dim, | |
180 | C1: Dim, | |
181 | S1: Storage<T, R1, C1>, | |
182 | R2: Dim, | |
183 | C2: Dim, | |
184 | S2: Storage<T, R2, C2>, | |
185 | ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>, | |
186 | { | |
187 | m1.zip_fold(m2, T::SimdRealField::zero(), |acc, a, b| { | |
188 | let diff = a - b; | |
189 | acc + diff.simd_modulus_squared() | |
190 | }) | |
191 | } | |
192 | ||
193 | // TODO: should allow different input storages in `Euclidean`. | |
194 | ||
195 | impl<E,M,S> Euclidean<E> | |
196 | for Vector<E,M,S> | |
197 | where M : Dim, | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
198 | S : StorageMut<E,M> + Clone, |
56
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
199 | E : Float + Scalar + Zero + One + RealField, |
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
200 | DefaultAllocator : Allocator<M> { |
0 | 201 | |
202 | type Output = OVector<E, M>; | |
203 | ||
204 | #[inline] | |
205 | fn norm2_squared(&self) -> E { | |
206 | Vector::<E,M,S>::norm_squared(self) | |
207 | } | |
208 | ||
209 | #[inline] | |
210 | fn dist2_squared(&self, other : &Self) -> E { | |
211 | metric_distance_squared(self, other) | |
212 | } | |
213 | } | |
214 | ||
215 | impl<E,M,S> StaticEuclidean<E> | |
216 | for Vector<E,M,S> | |
217 | where M : DimName, | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
56
diff
changeset
|
218 | S : StorageMut<E,M> + Clone, |
56
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
219 | E : Float + Scalar + Zero + One + RealField, |
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
220 | DefaultAllocator : Allocator<M> { |
0 | 221 | |
222 | #[inline] | |
223 | fn origin() -> OVector<E, M> { | |
224 | OVector::zeros() | |
225 | } | |
226 | } | |
227 | ||
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
228 | /// The default norm for `Vector` is [`L2`]. |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
229 | impl<E,M,S> Normed<E> |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
230 | for Vector<E,M,S> |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
231 | where M : Dim, |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
232 | S : Storage<E,M> + Clone, |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
233 | E : Float + Scalar + Zero + One + RealField, |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
234 | DefaultAllocator : Allocator<M> { |
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 | type NormExp = L2; |
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 | #[inline] |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
239 | fn norm_exponent(&self) -> Self::NormExp { |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
240 | L2 |
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 | #[inline] |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
244 | fn is_zero(&self) -> bool { |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
245 | Vector::<E,M,S>::norm_squared(self) == E::ZERO |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
246 | } |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
247 | } |
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 | impl<E,M,S> HasDual<E> |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
250 | for Vector<E,M,S> |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
251 | where M : Dim, |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
252 | S : Storage<E,M> + Clone, |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
253 | E : Float + Scalar + Zero + One + RealField, |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
254 | DefaultAllocator : Allocator<M> { |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
255 | // TODO: Doesn't work with different storage formats. |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
256 | type DualSpace = Self; |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
257 | } |
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
258 | |
0 | 259 | impl<E,M,S> Norm<E, L1> |
260 | for Vector<E,M,S> | |
261 | where M : Dim, | |
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
262 | S : Storage<E,M>, |
56
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
263 | E : Float + Scalar + Zero + One + RealField, |
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
264 | DefaultAllocator : Allocator<M> { |
0 | 265 | |
266 | #[inline] | |
267 | fn norm(&self, _ : L1) -> E { | |
268 | LpNorm(1).norm(self) | |
269 | } | |
270 | } | |
271 | ||
272 | impl<E,M,S> Dist<E, L1> | |
273 | for Vector<E,M,S> | |
274 | where M : Dim, | |
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
275 | S : Storage<E,M>, |
56
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
276 | E : Float + Scalar + Zero + One + RealField, |
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
277 | DefaultAllocator : Allocator<M> { |
0 | 278 | #[inline] |
279 | fn dist(&self, other : &Self, _ : L1) -> E { | |
280 | LpNorm(1).metric_distance(self, other) | |
281 | } | |
282 | } | |
283 | ||
284 | impl<E,M,S> Norm<E, L2> | |
285 | for Vector<E,M,S> | |
286 | where M : Dim, | |
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
287 | S : Storage<E,M>, |
56
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
288 | E : Float + Scalar + Zero + One + RealField, |
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
289 | DefaultAllocator : Allocator<M> { |
0 | 290 | |
291 | #[inline] | |
292 | fn norm(&self, _ : L2) -> E { | |
293 | LpNorm(2).norm(self) | |
294 | } | |
295 | } | |
296 | ||
297 | impl<E,M,S> Dist<E, L2> | |
298 | for Vector<E,M,S> | |
299 | where M : Dim, | |
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
300 | S : Storage<E,M>, |
56
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
301 | E : Float + Scalar + Zero + One + RealField, |
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
302 | DefaultAllocator : Allocator<M> { |
0 | 303 | #[inline] |
304 | fn dist(&self, other : &Self, _ : L2) -> E { | |
305 | LpNorm(2).metric_distance(self, other) | |
306 | } | |
307 | } | |
308 | ||
309 | impl<E,M,S> Norm<E, Linfinity> | |
310 | for Vector<E,M,S> | |
311 | where M : Dim, | |
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
312 | S : Storage<E,M>, |
56
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
313 | E : Float + Scalar + Zero + One + RealField, |
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
314 | DefaultAllocator : Allocator<M> { |
0 | 315 | |
316 | #[inline] | |
317 | fn norm(&self, _ : Linfinity) -> E { | |
318 | UniformNorm.norm(self) | |
319 | } | |
320 | } | |
321 | ||
322 | impl<E,M,S> Dist<E, Linfinity> | |
323 | for Vector<E,M,S> | |
324 | where M : Dim, | |
60
848ecc05becf
More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
325 | S : Storage<E,M>, |
56
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
326 | E : Float + Scalar + Zero + One + RealField, |
5e3c1874797d
Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
327 | DefaultAllocator : Allocator<M> { |
0 | 328 | #[inline] |
329 | fn dist(&self, other : &Self, _ : Linfinity) -> E { | |
330 | UniformNorm.metric_distance(self, other) | |
331 | } | |
332 | } | |
333 | ||
5 | 334 | /// Helper trait to hide the symbols of [`nalgebra::RealField`]. |
335 | /// | |
336 | /// By assuming `ToNalgebraRealField` intead of `nalgebra::RealField` as a trait bound, | |
337 | /// functions can piggyback `nalgebra::RealField` without exponsing themselves to it. | |
338 | /// Thus methods from [`num_traits`] can be used directly without similarly named methods | |
339 | /// from [`nalgebra`] conflicting with them. Only when absolutely necessary to work with | |
340 | /// nalgebra, one can convert to the nalgebra view of the same type using the methods of | |
341 | /// this trait. | |
0 | 342 | pub trait ToNalgebraRealField : Float { |
5 | 343 | /// The nalgebra type corresponding to this type. Usually same as `Self`. |
344 | /// | |
345 | /// This type only carries `nalgebra` traits. | |
0 | 346 | type NalgebraType : RealField; |
5 | 347 | /// The “mixed” type corresponding to this type. Usually same as `Self`. |
348 | /// | |
349 | /// This type carries both `num_traits` and `nalgebra` traits. | |
0 | 350 | type MixedType : RealField + Float; |
351 | ||
5 | 352 | /// Convert to the nalgebra view of `self`. |
0 | 353 | fn to_nalgebra(self) -> Self::NalgebraType; |
5 | 354 | |
355 | /// Convert to the mixed (nalgebra and num_traits) view of `self`. | |
0 | 356 | fn to_nalgebra_mixed(self) -> Self::MixedType; |
357 | ||
5 | 358 | /// Convert from the nalgebra view of `self`. |
0 | 359 | fn from_nalgebra(t : Self::NalgebraType) -> Self; |
5 | 360 | |
361 | /// Convert from the mixed (nalgebra and num_traits) view to `self`. | |
0 | 362 | fn from_nalgebra_mixed(t : Self::MixedType) -> Self; |
363 | } | |
364 | ||
365 | impl ToNalgebraRealField for f32 { | |
366 | type NalgebraType = f32; | |
367 | type MixedType = f32; | |
368 | ||
369 | #[inline] | |
370 | fn to_nalgebra(self) -> Self::NalgebraType { self } | |
371 | ||
372 | #[inline] | |
373 | fn to_nalgebra_mixed(self) -> Self::MixedType { self } | |
374 | ||
375 | #[inline] | |
376 | fn from_nalgebra(t : Self::NalgebraType) -> Self { t } | |
377 | ||
378 | #[inline] | |
379 | fn from_nalgebra_mixed(t : Self::MixedType) -> Self { t } | |
380 | ||
381 | } | |
382 | ||
383 | impl ToNalgebraRealField for f64 { | |
384 | type NalgebraType = f64; | |
385 | type MixedType = f64; | |
386 | ||
387 | #[inline] | |
388 | fn to_nalgebra(self) -> Self::NalgebraType { self } | |
389 | ||
390 | #[inline] | |
391 | fn to_nalgebra_mixed(self) -> Self::MixedType { self } | |
392 | ||
393 | #[inline] | |
394 | fn from_nalgebra(t : Self::NalgebraType) -> Self { t } | |
395 | ||
396 | #[inline] | |
397 | fn from_nalgebra_mixed(t : Self::MixedType) -> Self { t } | |
398 | } | |
399 |