102 p_norm * self.norm_factor(p) |
102 p_norm * self.norm_factor(p) |
103 } |
103 } |
104 } |
104 } |
105 |
105 |
106 /// Trait for distances with respect to a norm. |
106 /// Trait for distances with respect to a norm. |
107 pub trait Dist<F : Num, Exponent : NormExponent> : Norm<F, Exponent> { |
107 pub trait Dist<F : Num, Exponent : NormExponent> : Norm<F, Exponent> + Space { |
108 /// Calculate the distance |
108 /// Calculate the distance |
109 fn dist(&self, other : &Self, _p : Exponent) -> F; |
109 fn dist<I : Instance<Self>>(&self, other : I, _p : Exponent) -> F; |
110 } |
110 } |
111 |
111 |
112 /// Trait for Euclidean projections to the `Exponent`-[`Norm`]-ball. |
112 /// Trait for Euclidean projections to the `Exponent`-[`Norm`]-ball. |
113 /// |
113 /// |
114 /// Use as |
114 /// Use as |
169 huber.apply(self.norm2_squared()) |
169 huber.apply(self.norm2_squared()) |
170 } |
170 } |
171 } |
171 } |
172 |
172 |
173 impl<F : Float, E : Euclidean<F>> Dist<F, HuberL1<F>> for E { |
173 impl<F : Float, E : Euclidean<F>> Dist<F, HuberL1<F>> for E { |
174 fn dist(&self, other : &Self, huber : HuberL1<F>) -> F { |
174 fn dist<I : Instance<Self>>(&self, other : I, huber : HuberL1<F>) -> F { |
175 huber.apply(self.dist2_squared(other)) |
175 huber.apply(self.dist2_squared(other)) |
176 } |
176 } |
177 } |
177 } |
178 |
178 |
179 impl<F : Float, E : Norm<F, L2>> Norm<F, L21> for Vec<E> { |
179 // impl<F : Float, E : Norm<F, L2>> Norm<F, L21> for Vec<E> { |
180 fn norm(&self, _l21 : L21) -> F { |
180 // fn norm(&self, _l21 : L21) -> F { |
181 self.iter().map(|e| e.norm(L2)).sum() |
181 // self.iter().map(|e| e.norm(L2)).sum() |
182 } |
182 // } |
183 } |
183 // } |
184 |
184 |
185 impl<F : Float, E : Dist<F, L2>> Dist<F, L21> for Vec<E> { |
185 // impl<F : Float, E : Dist<F, L2>> Dist<F, L21> for Vec<E> { |
186 fn dist(&self, other : &Self, _l21 : L21) -> F { |
186 // fn dist<I : Instance<Self>>(&self, other : I, _l21 : L21) -> F { |
187 self.iter().zip(other.iter()).map(|(e, g)| e.dist(g, L2)).sum() |
187 // self.iter().zip(other.iter()).map(|(e, g)| e.dist(g, L2)).sum() |
188 } |
188 // } |
189 } |
189 // } |
190 |
190 |
191 impl<E, F, Domain> Mapping<Domain> for NormMapping<F, E> |
191 impl<E, F, Domain> Mapping<Domain> for NormMapping<F, E> |
192 where |
192 where |
193 F : Float, |
193 F : Float, |
194 E : NormExponent, |
194 E : NormExponent, |