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