| 24 /// |
24 /// |
| 25 // Just a collection of desirable attributes for a marker type |
25 // Just a collection of desirable attributes for a marker type |
| 26 pub trait NormExponent: Copy { |
26 pub trait NormExponent: Copy { |
| 27 /// Return the norm as a mappin |
27 /// Return the norm as a mappin |
| 28 fn as_mapping<F: Float>(self) -> NormMapping<F, Self> { |
28 fn as_mapping<F: Float>(self) -> NormMapping<F, Self> { |
| 29 NormMapping { |
29 NormMapping { exponent: self, _phantoms: PhantomData } |
| 30 exponent: self, |
|
| 31 _phantoms: PhantomData, |
|
| 32 } |
|
| 33 } |
30 } |
| 34 } |
31 } |
| 35 |
32 |
| 36 /// Exponent type for the 1-[`Norm`]. |
33 /// Exponent type for the 1-[`Norm`]. |
| 37 #[derive(Copy, Debug, Clone, Serialize, Eq, PartialEq)] |
34 #[derive(Copy, Debug, Clone, Serialize, Eq, PartialEq)] |
| 109 p_norm * self.norm_factor(p) |
106 p_norm * self.norm_factor(p) |
| 110 } |
107 } |
| 111 } |
108 } |
| 112 |
109 |
| 113 /// Trait for distances with respect to a norm. |
110 /// Trait for distances with respect to a norm. |
| 114 pub trait Dist<F: Num, Exponent: NormExponent>: Norm<Exponent, F> + Space { |
111 pub trait Dist<Exponent: NormExponent, F: Num = f64>: Norm<Exponent, F> + Space { |
| 115 /// Calculate the distance |
112 /// Calculate the distance |
| 116 fn dist<I: Instance<Self>>(&self, other: I, _p: Exponent) -> F; |
113 fn dist<I: Instance<Self>>(&self, other: I, _p: Exponent) -> F; |
| 117 } |
114 } |
| 118 |
115 |
| 119 /// Trait for Euclidean projections to the `Exponent`-[`Norm`]-ball. |
116 /// Trait for Euclidean projections to the `Exponent`-[`Norm`]-ball. |
| 181 fn norm(&self, huber: HuberL1<F>) -> F { |
178 fn norm(&self, huber: HuberL1<F>) -> F { |
| 182 huber.apply(self.norm2_squared()) |
179 huber.apply(self.norm2_squared()) |
| 183 } |
180 } |
| 184 } |
181 } |
| 185 |
182 |
| 186 impl<F: Float, E: Euclidean<F> + Normed<F, NormExp = L2>> Dist<F, HuberL1<F>> for E { |
183 impl<F: Float, E: Euclidean<F> + Normed<F, NormExp = L2>> Dist<HuberL1<F>, F> for E { |
| 187 fn dist<I: Instance<Self>>(&self, other: I, huber: HuberL1<F>) -> F { |
184 fn dist<I: Instance<Self>>(&self, other: I, huber: HuberL1<F>) -> F { |
| 188 huber.apply(self.dist2_squared(other)) |
185 huber.apply(self.dist2_squared(other)) |
| 189 } |
186 } |
| 190 } |
187 } |
| 191 |
188 |
| 193 // fn norm(&self, _l21 : L21) -> F { |
190 // fn norm(&self, _l21 : L21) -> F { |
| 194 // self.iter().map(|e| e.norm(L2)).sum() |
191 // self.iter().map(|e| e.norm(L2)).sum() |
| 195 // } |
192 // } |
| 196 // } |
193 // } |
| 197 |
194 |
| 198 // impl<F : Float, E : Dist<F, L2>> Dist<F, L21> for Vec<E> { |
195 // impl<F : Float, E : Dist<F, L2>> Dist<L21, F> for Vec<E> { |
| 199 // fn dist<I : Instance<Self>>(&self, other : I, _l21 : L21) -> F { |
196 // fn dist<I : Instance<Self>>(&self, other : I, _l21 : L21) -> F { |
| 200 // self.iter().zip(other.iter()).map(|(e, g)| e.dist(g, L2)).sum() |
197 // self.iter().zip(other.iter()).map(|(e, g)| e.dist(g, L2)).sum() |
| 201 // } |
198 // } |
| 202 // } |
199 // } |
| 203 |
200 |