Mon, 24 Oct 2022 10:52:19 +0300
Added type for numerical errors
0 | 1 | /*! |
2 | Norms, projections, etc. | |
3 | */ | |
4 | ||
5 | use crate::types::*; | |
6 | use std::ops::{Mul,MulAssign,Div,DivAssign,Add,Sub,AddAssign,SubAssign,Neg}; | |
7 | use serde::Serialize; | |
8 | //use std::iter::Sum; | |
9 | ||
10 | // | |
11 | // Dot products | |
12 | // | |
13 | ||
14 | /// Space with a defined dot product. | |
15 | pub trait Dot<U,F> { | |
16 | fn dot(&self, other : &U) -> F; | |
17 | } | |
18 | ||
19 | //self.iter().zip(other.iter()).map(|(&x,&y)| x*y).sum() | |
20 | ||
21 | // | |
22 | // Euclidean spaces | |
23 | // | |
24 | ||
25 | pub trait Euclidean<F : Float> : Sized + Dot<Self,F> | |
26 | + Mul<F, Output=<Self as Euclidean<F>>::Output> + MulAssign<F> | |
27 | + Div<F, Output=<Self as Euclidean<F>>::Output> + DivAssign<F> | |
28 | + Add<Self, Output=<Self as Euclidean<F>>::Output> | |
29 | + Sub<Self, Output=<Self as Euclidean<F>>::Output> | |
30 | + for<'b> Add<&'b Self, Output=<Self as Euclidean<F>>::Output> | |
31 | + for<'b> Sub<&'b Self, Output=<Self as Euclidean<F>>::Output> | |
32 | + AddAssign<Self> + for<'b> AddAssign<&'b Self> | |
33 | + SubAssign<Self> + for<'b> SubAssign<&'b Self> | |
34 | + Neg<Output=<Self as Euclidean<F>>::Output> { | |
35 | type Output : Euclidean<F>; | |
36 | ||
37 | /// Returns the origin of same dimensions as `self`. | |
38 | fn similar_origin(&self) -> <Self as Euclidean<F>>::Output; | |
39 | ||
40 | /// Calculate the square of the 2-norm. | |
41 | #[inline] | |
42 | fn norm2_squared(&self) -> F { | |
43 | self.dot(self) | |
44 | } | |
45 | ||
46 | /// Calculate the square of the 2-norm divided by 2. | |
47 | #[inline] | |
48 | fn norm2_squared_div2(&self) -> F { | |
49 | self.norm2_squared()/F::TWO | |
50 | } | |
51 | ||
52 | /// Calculate the 2-norm. | |
53 | #[inline] | |
54 | fn norm2(&self) -> F { | |
55 | self.norm2_squared().sqrt() | |
56 | } | |
57 | ||
58 | /// Calculate the 2-distance squared. | |
59 | fn dist2_squared(&self, other : &Self) -> F; | |
60 | ||
61 | /// Calculate the 2-distance. | |
62 | #[inline] | |
63 | fn dist2(&self, other : &Self) -> F { | |
64 | self.dist2_squared(other).sqrt() | |
65 | } | |
66 | ||
67 | /// Project to the 2-ball. | |
68 | #[inline] | |
69 | fn proj_ball2(mut self, ρ : F) -> Self { | |
70 | self.proj_ball2_mut(ρ); | |
71 | self | |
72 | } | |
73 | ||
74 | /// Project to the 2-ball in-place. | |
75 | #[inline] | |
76 | fn proj_ball2_mut(&mut self, ρ : F) { | |
77 | let r = self.norm2(); | |
78 | if r>ρ { | |
79 | *self *= ρ/r | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | /// Trait for [`Euclidean`] spaces with dimensions known at compile time. | |
85 | pub trait StaticEuclidean<F : Float> : Euclidean<F> { | |
86 | /// Returns the origin | |
87 | fn origin() -> <Self as Euclidean<F>>::Output; | |
88 | } | |
89 | ||
90 | // | |
91 | // Abstract norms | |
92 | // | |
93 | ||
94 | #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] | |
95 | pub struct L1; | |
96 | ||
97 | #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] | |
98 | pub struct L2; | |
99 | ||
100 | #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] | |
101 | pub struct Linfinity; | |
102 | ||
103 | #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] | |
104 | pub struct L21; | |
105 | ||
106 | #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] | |
107 | pub struct HuberL1<F : Float>(pub F); | |
108 | ||
109 | #[derive(Copy,Debug,Clone,Serialize,Eq,PartialEq)] | |
110 | pub struct HuberL21<F : Float>(pub F); | |
111 | ||
112 | pub trait Norm<F, Exponent> { | |
113 | /// Calculate the norm | |
114 | fn norm(&self, _p : Exponent) -> F; | |
115 | } | |
116 | ||
117 | /// Indicates that a [`Norm`] is dominated by another norm (`Exponent`) on `Elem` with the | |
118 | /// corresponding field `F`. | |
119 | pub trait Dominated<F : Num, Exponent, Elem> { | |
120 | /// Indicates the factor $c$ for the inequality $‖x‖ ≤ C ‖x‖_p$. | |
121 | fn norm_factor(&self, p : Exponent) -> F; | |
122 | /// Given a norm-value $‖x‖_p$, calculates $C‖x‖_p$ such that $‖x‖ ≤ C‖x‖_p$ | |
123 | #[inline] | |
124 | fn from_norm(&self, p_norm : F, p : Exponent) -> F { | |
125 | p_norm * self.norm_factor(p) | |
126 | } | |
127 | } | |
128 | ||
129 | pub trait Dist<F,Exponent> : Norm<F, Exponent> { | |
130 | /// Calculate the distance | |
131 | fn dist(&self, other : &Self, _p : Exponent) -> F; | |
132 | } | |
133 | ||
134 | pub trait Projection<F, Exponent> : Norm<F, Exponent> + Euclidean<F> where F : Float { | |
135 | /// Project to the norm-ball. | |
136 | fn proj_ball(mut self, ρ : F, q : Exponent) -> Self { | |
137 | self.proj_ball_mut(ρ, q); | |
138 | self | |
139 | } | |
140 | ||
141 | /// Project to the norm-ball in-place. | |
142 | fn proj_ball_mut(&mut self, ρ : F, _q : Exponent); | |
143 | } | |
144 | ||
145 | /*impl<F : Float, E : Euclidean<F>> Norm<F, L2> for E { | |
146 | #[inline] | |
147 | fn norm(&self, _p : L2) -> F { self.norm2() } | |
148 | ||
149 | fn dist(&self, other : &Self, _p : L2) -> F { self.dist2(other) } | |
150 | }*/ | |
151 | ||
152 | impl<F : Float, E : Euclidean<F> + Norm<F, L2>> Projection<F, L2> for E { | |
153 | #[inline] | |
154 | fn proj_ball(self, ρ : F, _p : L2) -> Self { self.proj_ball2(ρ) } | |
155 | ||
156 | #[inline] | |
157 | fn proj_ball_mut(&mut self, ρ : F, _p : L2) { self.proj_ball2_mut(ρ) } | |
158 | } | |
159 | ||
160 | impl<F : Float> HuberL1<F> { | |
161 | fn apply(self, xnsq : F) -> F { | |
162 | let HuberL1(γ) = self; | |
163 | let xn = xnsq.sqrt(); | |
164 | if γ == F::ZERO { | |
165 | xn | |
166 | } else { | |
167 | if xn > γ { | |
168 | xn-γ/F::TWO | |
169 | } else if xn<(-γ) { | |
170 | -xn-γ/F::TWO | |
171 | } else { | |
172 | xnsq/(F::TWO*γ) | |
173 | } | |
174 | } | |
175 | } | |
176 | } | |
177 | ||
178 | impl<F : Float, E : Euclidean<F>> Norm<F, HuberL1<F>> for E { | |
179 | fn norm(&self, huber : HuberL1<F>) -> F { | |
180 | huber.apply(self.norm2_squared()) | |
181 | } | |
182 | } | |
183 | ||
184 | impl<F : Float, E : Euclidean<F>> Dist<F, HuberL1<F>> for E { | |
185 | fn dist(&self, other : &Self, huber : HuberL1<F>) -> F { | |
186 | huber.apply(self.dist2_squared(other)) | |
187 | } | |
188 | } | |
189 | ||
190 | /* | |
191 | #[inline] | |
192 | pub fn mean<V>(x : V) -> V::Field where V : ValidArray { | |
193 | x.iter().sum()/x.len() | |
194 | } | |
195 | ||
196 | #[inline] | |
197 | pub fn proj_nonneg_mut<V>(x : &mut V) -> &mut V where V : ValidArray { | |
198 | x.iter_mut().for_each(|&mut p| if p < 0 { *p = 0 } ); | |
199 | x | |
200 | } | |
201 | */ | |
202 | ||
203 | ||
204 | // | |
205 | // 2,1-norm generic implementation | |
206 | // | |
207 | ||
208 | /* | |
209 | pub trait InnerVectors { | |
210 | type Item; | |
211 | type Iter : Iterator<Item=Self::Item>; | |
212 | fn inner_vectors(&self) -> &Self::Iter; | |
213 | } | |
214 | ||
215 | pub trait InnerVectorsMut : InnerVectors { | |
216 | type IterMut : Iterator<Item=Self::Item>; | |
217 | fn inner_vectors_mut(&self) -> &mut Self::Item; | |
218 | } | |
219 | ||
220 | impl<F : Float + Sum, T : InnerVectors> Norm<F, L21> for T where T::Item : Norm<F, L2> { | |
221 | fn norm(&self, _ : L21) -> F { | |
222 | self.inner_vectors().map(|t| t.norm(L2)).sum() | |
223 | } | |
224 | } | |
225 | ||
226 | impl<F : Float + Sum, T : InnerVectorsMut + Euclidean<F>> Projection<F, L21> | |
227 | for T where T::ItemMut : Projection<F, L2> { | |
228 | fn proj_ball_mut(&mut self, _ : L21) { | |
229 | self.inner_vectors_mut().for_each(|t| t.proj_ball_mut(L2)); | |
230 | } | |
231 | } | |
232 | */ | |
233 |