| 188 } |
199 } |
| 189 } |
200 } |
| 190 |
201 |
| 191 impl<Domain, F : Num> IntoIterator for DiscreteMeasure<Domain, F> { |
202 impl<Domain, F : Num> IntoIterator for DiscreteMeasure<Domain, F> { |
| 192 type Item = DeltaMeasure<Domain, F>; |
203 type Item = DeltaMeasure<Domain, F>; |
| 193 type IntoIter = <Vec<DeltaMeasure<Domain, F>> as IntoIterator>::IntoIter; |
204 type IntoIter = std::vec::IntoIter<DeltaMeasure<Domain, F>>; |
| 194 |
205 |
| 195 #[inline] |
206 #[inline] |
| 196 fn into_iter(self) -> Self::IntoIter { |
207 fn into_iter(self) -> Self::IntoIter { |
| 197 self.spikes.into_iter() |
208 self.spikes.into_iter() |
| |
209 } |
| |
210 } |
| |
211 |
| |
212 impl<'a, Domain, F : Num> IntoIterator for &'a DiscreteMeasure<Domain, F> { |
| |
213 type Item = &'a DeltaMeasure<Domain, F>; |
| |
214 type IntoIter = SpikeIter<'a, Domain, F>; |
| |
215 |
| |
216 #[inline] |
| |
217 fn into_iter(self) -> Self::IntoIter { |
| |
218 self.spikes.iter() |
| |
219 } |
| |
220 } |
| |
221 |
| |
222 impl<Domain, F : Num> Sum<DeltaMeasure<Domain, F>> for DiscreteMeasure<Domain, F> { |
| |
223 // Required method |
| |
224 fn sum<I>(iter: I) -> Self |
| |
225 where |
| |
226 I : Iterator<Item = DeltaMeasure<Domain, F>> |
| |
227 { |
| |
228 Self::from_iter(iter) |
| |
229 } |
| |
230 } |
| |
231 |
| |
232 impl<'a, Domain : Clone, F : Num> Sum<&'a DeltaMeasure<Domain, F>> |
| |
233 for DiscreteMeasure<Domain, F> |
| |
234 { |
| |
235 // Required method |
| |
236 fn sum<I>(iter: I) -> Self |
| |
237 where |
| |
238 I : Iterator<Item = &'a DeltaMeasure<Domain, F>> |
| |
239 { |
| |
240 Self::from_iter(iter.cloned()) |
| |
241 } |
| |
242 } |
| |
243 |
| |
244 impl<Domain, F : Num> Sum<DiscreteMeasure<Domain, F>> for DiscreteMeasure<Domain, F> { |
| |
245 // Required method |
| |
246 fn sum<I>(iter: I) -> Self |
| |
247 where |
| |
248 I : Iterator<Item = DiscreteMeasure<Domain, F>> |
| |
249 { |
| |
250 Self::from_iter(iter.map(|μ| μ.into_iter()).flatten()) |
| |
251 } |
| |
252 } |
| |
253 |
| |
254 impl<'a, Domain : Clone, F : Num> Sum<&'a DiscreteMeasure<Domain, F>> |
| |
255 for DiscreteMeasure<Domain, F> |
| |
256 { |
| |
257 // Required method |
| |
258 fn sum<I>(iter: I) -> Self |
| |
259 where |
| |
260 I : Iterator<Item = &'a DiscreteMeasure<Domain, F>> |
| |
261 { |
| |
262 Self::from_iter(iter.map(|μ| μ.iter_spikes()).flatten().cloned()) |
| 198 } |
263 } |
| 199 } |
264 } |
| 200 |
265 |
| 201 impl<Domain : Clone, F : Float> DiscreteMeasure<Domain, F> { |
266 impl<Domain : Clone, F : Float> DiscreteMeasure<Domain, F> { |
| 202 /// Computes `μ1 ← θ * μ1 - ζ * μ2`, pruning entries where both `μ1` (`self`) and `μ2` have |
267 /// Computes `μ1 ← θ * μ1 - ζ * μ2`, pruning entries where both `μ1` (`self`) and `μ2` have |
| 310 fn from(list : [D; K]) -> Self { |
375 fn from(list : [D; K]) -> Self { |
| 311 list.into_iter().collect() |
376 list.into_iter().collect() |
| 312 } |
377 } |
| 313 } |
378 } |
| 314 |
379 |
| |
380 impl<Domain, F : Num> From<Vec<DeltaMeasure<Domain, F>>> |
| |
381 for DiscreteMeasure<Domain, F> { |
| |
382 #[inline] |
| |
383 fn from(spikes : Vec<DeltaMeasure<Domain, F>>) -> Self { |
| |
384 DiscreteMeasure{ spikes } |
| |
385 } |
| |
386 } |
| |
387 |
| |
388 impl<'a, Domain, F : Num, D> From<&'a [D]> |
| |
389 for DiscreteMeasure<Domain, F> |
| |
390 where &'a D : Into<DeltaMeasure<Domain, F>> { |
| |
391 #[inline] |
| |
392 fn from(list : &'a [D]) -> Self { |
| |
393 list.into_iter().map(|d| d.into()).collect() |
| |
394 } |
| |
395 } |
| |
396 |
| |
397 |
| |
398 impl<Domain, F : Num> From<DeltaMeasure<Domain, F>> |
| |
399 for DiscreteMeasure<Domain, F> { |
| |
400 #[inline] |
| |
401 fn from(δ : DeltaMeasure<Domain, F>) -> Self { |
| |
402 DiscreteMeasure{ |
| |
403 spikes : vec!(δ) |
| |
404 } |
| |
405 } |
| |
406 } |
| |
407 |
| |
408 impl<'a, Domain : Clone, F : Num> From<&'a DeltaMeasure<Domain, F>> |
| |
409 for DiscreteMeasure<Domain, F> { |
| |
410 #[inline] |
| |
411 fn from(δ : &'a DeltaMeasure<Domain, F>) -> Self { |
| |
412 DiscreteMeasure{ |
| |
413 spikes : vec!(δ.clone()) |
| |
414 } |
| |
415 } |
| |
416 } |
| |
417 |
| |
418 |
| 315 impl<Domain, F : Num, D : Into<DeltaMeasure<Domain, F>>> FromIterator<D> |
419 impl<Domain, F : Num, D : Into<DeltaMeasure<Domain, F>>> FromIterator<D> |
| 316 for DiscreteMeasure<Domain, F> { |
420 for DiscreteMeasure<Domain, F> { |
| 317 #[inline] |
421 #[inline] |
| 318 fn from_iter<T>(iter : T) -> Self |
422 fn from_iter<T>(iter : T) -> Self |
| 319 where T : IntoIterator<Item=D> { |
423 where T : IntoIterator<Item=D> { |
| 369 fn norm(&self, _ : Radon) -> F { |
473 fn norm(&self, _ : Radon) -> F { |
| 370 self.spikes.iter().map(|m| m.norm(Radon)).sum() |
474 self.spikes.iter().map(|m| m.norm(Radon)).sum() |
| 371 } |
475 } |
| 372 } |
476 } |
| 373 |
477 |
| 374 impl<Domain, G, F : Num, Y : Sum + Mul<F, Output=Y>> Apply<G> for DiscreteMeasure<Domain, F> |
478 impl<Domain, G, F : Num> Mapping<G> for DiscreteMeasure<Domain, F> |
| 375 where G: for<'a> Apply<&'a Domain, Output = Y> { |
479 where |
| 376 type Output = Y; |
480 Domain : Space, |
| 377 #[inline] |
481 G::Codomain : Sum + Mul<F, Output=G::Codomain>, |
| 378 fn apply(&self, g : G) -> Y { |
482 G : Mapping<Domain, Codomain=F> + Clone + Space, |
| 379 self.spikes.iter().map(|m| g.apply(&m.x) * m.α).sum() |
483 for<'b> &'b Domain : Instance<Domain>, |
| 380 } |
484 { |
| 381 } |
485 type Codomain = G::Codomain; |
| 382 |
486 |
| 383 impl<Domain, G, F : Num, Y : Sum + Mul<F, Output=Y>> Linear<G> for DiscreteMeasure<Domain, F> |
487 #[inline] |
| 384 where G : for<'a> Apply<&'a Domain, Output = Y> { |
488 fn apply<I : Instance<G>>(&self, g : I) -> Self::Codomain { |
| 385 type Codomain = Y; |
489 g.eval(|g| self.spikes.iter().map(|m| g.apply(&m.x) * m.α).sum()) |
| 386 } |
490 } |
| |
491 } |
| |
492 |
| |
493 impl<Domain, G, F : Num> Linear<G> for DiscreteMeasure<Domain, F> |
| |
494 where |
| |
495 Domain : Space, |
| |
496 G::Codomain : Sum + Mul<F, Output=G::Codomain>, |
| |
497 G : Mapping<Domain, Codomain=F> + Clone + Space, |
| |
498 for<'b> &'b Domain : Instance<Domain>, |
| |
499 { } |
| 387 |
500 |
| 388 |
501 |
| 389 /// Helper trait for constructing arithmetic operations for combinations |
502 /// Helper trait for constructing arithmetic operations for combinations |
| 390 /// of [`DiscreteMeasure`] and [`DeltaMeasure`], and their references. |
503 /// of [`DiscreteMeasure`] and [`DeltaMeasure`], and their references. |
| 391 trait Lift<F : Num, Domain> { |
504 trait Lift<F : Num, Domain> { |
| 392 type Producer : Iterator<Item=DeltaMeasure<Domain, F>>; |
505 type Producer : Iterator<Item=DeltaMeasure<Domain, F>>; |
| 393 |
506 |
| |
507 #[allow(dead_code)] |
| 394 /// Lifts `self` into a [`DiscreteMeasure`]. |
508 /// Lifts `self` into a [`DiscreteMeasure`]. |
| 395 fn lift(self) -> DiscreteMeasure<Domain, F>; |
509 fn lift(self) -> DiscreteMeasure<Domain, F>; |
| 396 |
510 |
| 397 /// Lifts `self` into a [`DiscreteMeasure`], apply either `f` or `f_mut` whether the type |
511 /// Lifts `self` into a [`DiscreteMeasure`], apply either `f` or `f_mut` whether the type |
| 398 /// this method is implemented for is a reference or or not. |
512 /// this method is implemented for is a reference or or not. |
| 685 )+ } |
799 )+ } |
| 686 } |
800 } |
| 687 |
801 |
| 688 make_discrete_scalarop_lhs!(Mul, mul; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); |
802 make_discrete_scalarop_lhs!(Mul, mul; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); |
| 689 make_discrete_scalarop_lhs!(Div, div; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); |
803 make_discrete_scalarop_lhs!(Div, div; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); |
| |
804 |
| |
805 impl<F : Num, Domain> Collection for DiscreteMeasure<Domain, F> { |
| |
806 type Element = DeltaMeasure<Domain, F>; |
| |
807 type RefsIter<'a> = std::slice::Iter<'a, Self::Element> where Self : 'a; |
| |
808 |
| |
809 #[inline] |
| |
810 fn iter_refs(&self) -> Self::RefsIter<'_> { |
| |
811 self.iter_spikes() |
| |
812 } |
| |
813 } |
| |
814 |
| |
815 impl<Domain : Clone, F : Num> Space for DiscreteMeasure<Domain, F> { |
| |
816 type Decomp = MeasureDecomp; |
| |
817 } |
| |
818 |
| |
819 pub type SpikeSlice<'b, Domain, F> = &'b [DeltaMeasure<Domain, F>]; |
| |
820 |
| |
821 pub type EitherSlice<'b, Domain, F> = EitherDecomp< |
| |
822 Vec<DeltaMeasure<Domain, F>>, |
| |
823 SpikeSlice<'b, Domain, F> |
| |
824 >; |
| |
825 |
| |
826 impl<F : Num, Domain : Clone> Decomposition<DiscreteMeasure<Domain, F>> for MeasureDecomp { |
| |
827 type Decomposition<'b> = EitherSlice<'b, Domain, F> where DiscreteMeasure<Domain, F> : 'b; |
| |
828 type Reference<'b> = SpikeSlice<'b, Domain, F> where DiscreteMeasure<Domain, F> : 'b; |
| |
829 |
| |
830 /// Left the lightweight reference type into a full decomposition type. |
| |
831 fn lift<'b>(r : Self::Reference<'b>) -> Self::Decomposition<'b> { |
| |
832 EitherDecomp::Borrowed(r) |
| |
833 } |
| |
834 } |
| |
835 |
| |
836 impl<F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp> |
| |
837 for DiscreteMeasure<Domain, F> |
| |
838 { |
| |
839 fn decompose<'b>(self) |
| |
840 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b> |
| |
841 where Self : 'b, DiscreteMeasure<Domain, F> : 'b { |
| |
842 EitherDecomp::Owned(self.spikes) |
| |
843 } |
| |
844 |
| |
845 fn ref_instance(&self) |
| |
846 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_> |
| |
847 { |
| |
848 self.spikes.as_slice() |
| |
849 } |
| |
850 |
| |
851 fn cow<'b>(self) -> MyCow<'b, DiscreteMeasure<Domain, F>> where Self : 'b { |
| |
852 MyCow::Owned(self) |
| |
853 } |
| |
854 |
| |
855 fn own(self) -> DiscreteMeasure<Domain, F> { |
| |
856 self |
| |
857 } |
| |
858 } |
| |
859 |
| |
860 impl<'a, F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp> |
| |
861 for &'a DiscreteMeasure<Domain, F> |
| |
862 { |
| |
863 fn decompose<'b>(self) |
| |
864 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b> |
| |
865 where Self : 'b, DiscreteMeasure<Domain, F> : 'b { |
| |
866 EitherDecomp::Borrowed(self.spikes.as_slice()) |
| |
867 } |
| |
868 |
| |
869 fn ref_instance(&self) |
| |
870 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_> |
| |
871 { |
| |
872 self.spikes.as_slice() |
| |
873 } |
| |
874 |
| |
875 fn cow<'b>(self) -> MyCow<'b, DiscreteMeasure<Domain, F>> where Self : 'b { |
| |
876 MyCow::Borrowed(self) |
| |
877 } |
| |
878 |
| |
879 fn own(self) -> DiscreteMeasure<Domain, F> { |
| |
880 self.clone() |
| |
881 } |
| |
882 } |
| |
883 |
| |
884 impl<'a, F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp> |
| |
885 for EitherSlice<'a, Domain, F> |
| |
886 { |
| |
887 fn decompose<'b>(self) |
| |
888 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b> |
| |
889 where Self : 'b, DiscreteMeasure<Domain, F> : 'b { |
| |
890 self |
| |
891 } |
| |
892 |
| |
893 fn ref_instance(&self) |
| |
894 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_> |
| |
895 { |
| |
896 match self { |
| |
897 EitherDecomp::Owned(v) => v.as_slice(), |
| |
898 EitherDecomp::Borrowed(s) => s, |
| |
899 } |
| |
900 } |
| |
901 |
| |
902 fn own(self) -> DiscreteMeasure<Domain, F> { |
| |
903 match self { |
| |
904 EitherDecomp::Owned(v) => v.into(), |
| |
905 EitherDecomp::Borrowed(s) => s.into(), |
| |
906 } |
| |
907 } |
| |
908 } |
| |
909 |
| |
910 impl<'a, F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp> |
| |
911 for &'a EitherSlice<'a, Domain, F> |
| |
912 { |
| |
913 fn decompose<'b>(self) |
| |
914 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b> |
| |
915 where Self : 'b, DiscreteMeasure<Domain, F> : 'b { |
| |
916 match self { |
| |
917 EitherDecomp::Owned(v) => EitherDecomp::Borrowed(v.as_slice()), |
| |
918 EitherDecomp::Borrowed(s) => EitherDecomp::Borrowed(s), |
| |
919 } |
| |
920 } |
| |
921 |
| |
922 fn ref_instance(&self) |
| |
923 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_> |
| |
924 { |
| |
925 match self { |
| |
926 EitherDecomp::Owned(v) => v.as_slice(), |
| |
927 EitherDecomp::Borrowed(s) => s, |
| |
928 } |
| |
929 } |
| |
930 |
| |
931 fn own(self) -> DiscreteMeasure<Domain, F> { |
| |
932 match self { |
| |
933 EitherDecomp::Owned(v) => v.as_slice(), |
| |
934 EitherDecomp::Borrowed(s) => s |
| |
935 }.into() |
| |
936 } |
| |
937 } |
| |
938 |
| |
939 impl<'a, F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp> |
| |
940 for SpikeSlice<'a, Domain, F> |
| |
941 { |
| |
942 fn decompose<'b>(self) |
| |
943 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b> |
| |
944 where Self : 'b, DiscreteMeasure<Domain, F> : 'b { |
| |
945 EitherDecomp::Borrowed(self) |
| |
946 } |
| |
947 |
| |
948 fn ref_instance(&self) |
| |
949 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_> |
| |
950 { |
| |
951 self |
| |
952 } |
| |
953 |
| |
954 fn own(self) -> DiscreteMeasure<Domain, F> { |
| |
955 self.into() |
| |
956 } |
| |
957 } |
| |
958 |
| |
959 impl<'a, F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp> |
| |
960 for &'a SpikeSlice<'a, Domain, F> |
| |
961 { |
| |
962 fn decompose<'b>(self) |
| |
963 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b> |
| |
964 where Self : 'b, DiscreteMeasure<Domain, F> : 'b { |
| |
965 EitherDecomp::Borrowed(*self) |
| |
966 } |
| |
967 |
| |
968 fn ref_instance(&self) |
| |
969 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_> |
| |
970 { |
| |
971 *self |
| |
972 } |
| |
973 |
| |
974 fn own(self) -> DiscreteMeasure<Domain, F> { |
| |
975 (*self).into() |
| |
976 } |
| |
977 } |
| |
978 |
| |
979 impl<F : Num, Domain : Clone > Instance<DiscreteMeasure<Domain, F>, MeasureDecomp> |
| |
980 for DeltaMeasure<Domain, F> |
| |
981 { |
| |
982 fn decompose<'b>(self) |
| |
983 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b> |
| |
984 where Self : 'b, DiscreteMeasure<Domain, F> : 'b { |
| |
985 EitherDecomp::Owned(vec![self]) |
| |
986 } |
| |
987 |
| |
988 fn ref_instance(&self) |
| |
989 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_> |
| |
990 { |
| |
991 std::slice::from_ref(self) |
| |
992 } |
| |
993 |
| |
994 fn own(self) -> DiscreteMeasure<Domain, F> { |
| |
995 self.into() |
| |
996 } |
| |
997 } |
| |
998 |
| |
999 impl<'a, F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp> |
| |
1000 for &'a DeltaMeasure<Domain, F> |
| |
1001 { |
| |
1002 fn decompose<'b>(self) |
| |
1003 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b> |
| |
1004 where Self : 'b, DiscreteMeasure<Domain, F> : 'b { |
| |
1005 EitherDecomp::Borrowed(std::slice::from_ref(self)) |
| |
1006 } |
| |
1007 |
| |
1008 fn ref_instance(&self) |
| |
1009 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_> |
| |
1010 { |
| |
1011 std::slice::from_ref(*self) |
| |
1012 } |
| |
1013 |
| |
1014 fn own(self) -> DiscreteMeasure<Domain, F> { |
| |
1015 self.into() |
| |
1016 } |
| |
1017 } |