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