src/measures/discrete.rs

changeset 52
f0e8704d3f0e
parent 39
6316d68b58af
equal deleted inserted replaced
31:6105b5cd8d89 52:f0e8704d3f0e
9 use serde::ser::{Serializer, Serialize, SerializeSeq}; 9 use serde::ser::{Serializer, Serialize, SerializeSeq};
10 use nalgebra::DVector; 10 use nalgebra::DVector;
11 11
12 use alg_tools::norms::Norm; 12 use alg_tools::norms::Norm;
13 use alg_tools::tabledump::TableDump; 13 use alg_tools::tabledump::TableDump;
14 use alg_tools::linops::{Apply, Linear}; 14 use alg_tools::linops::{Mapping, Linear};
15 use alg_tools::iter::{MapF,Mappable}; 15 use alg_tools::iter::{MapF,Mappable};
16 use alg_tools::nalgebra_support::ToNalgebraRealField; 16 use alg_tools::nalgebra_support::ToNalgebraRealField;
17 use alg_tools::collection::Collection;
18 use alg_tools::instance::{Instance, Decomposition, MyCow, EitherDecomp, Space};
17 19
18 use crate::types::*; 20 use crate::types::*;
19 use super::base::*; 21 use super::base::*;
20 use super::delta::*; 22 use super::delta::*;
21 23
27 #[derive(Clone,Debug)] 29 #[derive(Clone,Debug)]
28 pub struct DiscreteMeasure<Domain, F : Num> { 30 pub struct DiscreteMeasure<Domain, F : Num> {
29 pub(super) spikes : Vec<DeltaMeasure<Domain, F>>, 31 pub(super) spikes : Vec<DeltaMeasure<Domain, F>>,
30 } 32 }
31 33
34 pub type RNDM<F, const N : usize> = DiscreteMeasure<Loc<F, N>, F>;
35
32 /// Iterator over the [`DeltaMeasure`] spikes of a [`DiscreteMeasure`]. 36 /// Iterator over the [`DeltaMeasure`] spikes of a [`DiscreteMeasure`].
33 pub type SpikeIter<'a, Domain, F> = std::slice::Iter<'a, DeltaMeasure<Domain, F>>; 37 pub type SpikeIter<'a, Domain, F> = std::slice::Iter<'a, DeltaMeasure<Domain, F>>;
34 38
35 /// Iterator over mutable [`DeltaMeasure`] spikes of a [`DiscreteMeasure`]. 39 /// Iterator over mutable [`DeltaMeasure`] spikes of a [`DiscreteMeasure`].
36 pub type SpikeIterMut<'a, Domain, F> = std::slice::IterMut<'a, DeltaMeasure<Domain, F>>; 40 pub type SpikeIterMut<'a, Domain, F> = std::slice::IterMut<'a, DeltaMeasure<Domain, F>>;
57 #[inline] 61 #[inline]
58 pub fn len(&self) -> usize { 62 pub fn len(&self) -> usize {
59 self.spikes.len() 63 self.spikes.len()
60 } 64 }
61 65
66 /// Replace with the zero measure.
67 #[inline]
68 pub fn clear(&mut self) {
69 self.spikes.clear()
70 }
71
72 /// Remove `i`:th spike, not maintaining order.
73 ///
74 /// Panics if indiex is out of bounds.
75 #[inline]
76 pub fn swap_remove(&mut self, i : usize) -> DeltaMeasure<Domain, F>{
77 self.spikes.swap_remove(i)
78 }
79
62 /// Iterate over (references to) the [`DeltaMeasure`] spikes in this measure 80 /// Iterate over (references to) the [`DeltaMeasure`] spikes in this measure
63 #[inline] 81 #[inline]
64 pub fn iter_spikes(&self) -> SpikeIter<'_, Domain, F> { 82 pub fn iter_spikes(&self) -> SpikeIter<'_, Domain, F> {
65 self.spikes.iter() 83 self.spikes.iter()
66 } 84 }
91 109
92 /// Update the masses of all the spikes to those produced by an iterator. 110 /// Update the masses of all the spikes to those produced by an iterator.
93 #[inline] 111 #[inline]
94 pub fn set_masses<I : Iterator<Item=F>>(&mut self, iter : I) { 112 pub fn set_masses<I : Iterator<Item=F>>(&mut self, iter : I) {
95 self.spikes.iter_mut().zip(iter).for_each(|(δ, α)| δ.set_mass(α)); 113 self.spikes.iter_mut().zip(iter).for_each(|(δ, α)| δ.set_mass(α));
114 }
115
116 /// Update the locations of all the spikes to those produced by an iterator.
117 #[inline]
118 pub fn set_locations<'a, I : Iterator<Item=&'a Domain>>(&mut self, iter : I)
119 where Domain : 'static + Clone {
120 self.spikes.iter_mut().zip(iter.cloned()).for_each(|(δ, α)| δ.set_location(α));
96 } 121 }
97 122
98 // /// Map the masses of all the spikes using a function and an iterator 123 // /// Map the masses of all the spikes using a function and an iterator
99 // #[inline] 124 // #[inline]
100 // pub fn zipmap_masses< 125 // pub fn zipmap_masses<
105 // } 130 // }
106 131
107 /// Prune all spikes with zero mass. 132 /// Prune all spikes with zero mass.
108 #[inline] 133 #[inline]
109 pub fn prune(&mut self) { 134 pub fn prune(&mut self) {
110 self.spikes.retain(|δ| δ.α != F::ZERO); 135 self.prune_by(|δ| δ.α != F::ZERO);
136 }
137
138 /// Prune spikes by the predicate `g`.
139 #[inline]
140 pub fn prune_by<G : FnMut(&DeltaMeasure<Domain, F>) -> bool>(&mut self, g : G) {
141 self.spikes.retain(g);
142 }
143
144 /// Add the spikes produced by `iter` to this measure.
145 #[inline]
146 pub fn extend<I : Iterator<Item=DeltaMeasure<Domain, F>>>(
147 &mut self,
148 iter : I
149 ) {
150 self.spikes.extend(iter);
151 }
152
153 /// Add a spike to the measure
154 #[inline]
155 pub fn push(&mut self, δ : DeltaMeasure<Domain, F>) {
156 self.spikes.push(δ);
157 }
158
159 /// Iterate over triples of masses and locations of two discrete measures, which are assumed
160 /// to have equal locations of same spike indices.
161 pub fn both_matching<'a>(&'a self, other : &'a DiscreteMeasure<Domain, F>) ->
162 impl Iterator<Item=(F, F, &'a Domain)> {
163 let m = self.len().max(other.len());
164 self.iter_spikes().map(Some).chain(std::iter::repeat(None))
165 .zip(other.iter_spikes().map(Some).chain(std::iter::repeat(None)))
166 .take(m)
167 .map(|(oδ, orδ)| {
168 match (oδ, orδ) {
169 (Some(δ), Some(rδ)) => (δ.α, rδ.α, &δ.x), // Assumed δ.x=rδ.x
170 (Some(δ), None) => (δ.α, F::ZERO, &δ.x),
171 (None, Some(rδ)) => (F::ZERO, rδ.α, &rδ.x),
172 (None, None) => panic!("This cannot happen!"),
173 }
174 })
175 }
176
177 /// Subtract `other` from `self`, assuming equal locations of same spike indices
178 pub fn sub_matching(&self, other : &DiscreteMeasure<Domain, F>) -> DiscreteMeasure<Domain, F>
179 where Domain : Clone {
180 self.both_matching(other)
181 .map(|(α, β, x)| (x.clone(), α - β))
182 .collect()
183 }
184
185 /// Add `other` to `self`, assuming equal locations of same spike indices
186 pub fn add_matching(&self, other : &DiscreteMeasure<Domain, F>) -> DiscreteMeasure<Domain, F>
187 where Domain : Clone {
188 self.both_matching(other)
189 .map(|(α, β, x)| (x.clone(), α + β))
190 .collect()
191 }
192
193 /// Calculate the Radon-norm distance of `self` to `other`,
194 /// assuming equal locations of same spike indices.
195 pub fn dist_matching(&self, other : &DiscreteMeasure<Domain, F>) -> F where F : Float {
196 self.both_matching(other)
197 .map(|(α, β, _)| (α-β).abs())
198 .sum()
199 }
200 }
201
202 impl<Domain, F : Num> IntoIterator for DiscreteMeasure<Domain, F> {
203 type Item = DeltaMeasure<Domain, F>;
204 type IntoIter = std::vec::IntoIter<DeltaMeasure<Domain, F>>;
205
206 #[inline]
207 fn into_iter(self) -> Self::IntoIter {
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())
111 } 263 }
112 } 264 }
113 265
114 impl<Domain : Clone, F : Float> DiscreteMeasure<Domain, F> { 266 impl<Domain : Clone, F : Float> DiscreteMeasure<Domain, F> {
115 /// 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
116 // zero weight. `μ2` will contain copy of pruned original `μ1` without arithmetic performed. 268 // zero weight. `μ2` will contain a pruned copy of pruned original `μ1` without arithmetic
117 /// **This expects `self` and `μ2` to have matching coordinates in each index**. 269 /// performed. **This expects `self` and `μ2` to have matching coordinates in each index**.
118 // `μ2` can be than `self`, but not longer. 270 // `μ2` can be than `self`, but not longer.
119 pub fn pruning_sub(&mut self, θ : F, ζ : F, μ2 : &mut Self) { 271 pub fn pruning_sub(&mut self, θ : F, ζ : F, μ2 : &mut Self) {
120 let mut μ2_get = 0; 272 for δ in &self[μ2.len()..] {
121 let mut μ2_insert = 0; 273 μ2.push(DeltaMeasure{ x : δ.x.clone(), α : F::ZERO});
122 self.spikes.drain_filter(|&mut DeltaMeasure{ α : ref mut α_ref, ref x }| { 274 }
123 // Get weight of spike in μ2, zero if out of bounds. 275 debug_assert_eq!(self.len(), μ2.len());
124 let β = μ2.spikes.get(μ2_get).map_or(F::ZERO, DeltaMeasure::get_mass); 276 let mut dest = 0;
125 μ2_get += 1; 277 for i in 0..self.len() {
126 278 let α = self[i].α;
127 if *α_ref == F::ZERO && β == F::ZERO { 279 let α_new = θ * α - ζ * μ2[i].α;
128 // Prune 280 if dest < i {
129 true 281 μ2[dest] = DeltaMeasure{ x : self[i].x.clone(), α };
282 self[dest] = DeltaMeasure{ x : self[i].x.clone(), α : α_new };
130 } else { 283 } else {
131 // Save self weight 284 μ2[i].α = α;
132 let α = *α_ref; 285 self[i].α = α_new;
133 // Modify self 286 }
134 *α_ref = θ * α - ζ * β; 287 dest += 1;
135 // Make copy of old self weight in μ2 288 }
136 let δ = DeltaMeasure{ α, x : x.clone() }; 289 self.spikes.truncate(dest);
137 match μ2.spikes.get_mut(μ2_insert) { 290 μ2.spikes.truncate(dest);
138 Some(replace) => {
139 *replace = δ;
140 },
141 None => {
142 debug_assert_eq!(μ2.len(), μ2_insert);
143 μ2.spikes.push(δ);
144 },
145 }
146 μ2_insert += 1;
147 // Keep
148 false
149 }
150 });
151 // Truncate μ2 to same length as self.
152 μ2.spikes.truncate(μ2_insert);
153 debug_assert_eq!(μ2.len(), self.len());
154 } 291 }
155 } 292 }
156 293
157 impl<Domain, F : Float> DiscreteMeasure<Domain, F> { 294 impl<Domain, F : Float> DiscreteMeasure<Domain, F> {
158 /// Prune all spikes with mass absolute value less than the given `tolerance`. 295 /// Prune all spikes with mass absolute value less than the given `tolerance`.
172 309
173 /// Sets the masses of the spikes from the values of a [`DVector`]. 310 /// Sets the masses of the spikes from the values of a [`DVector`].
174 pub fn set_masses_dvector(&mut self, x : &DVector<F::MixedType>) { 311 pub fn set_masses_dvector(&mut self, x : &DVector<F::MixedType>) {
175 self.set_masses(x.iter().map(|&α| F::from_nalgebra_mixed(α))); 312 self.set_masses(x.iter().map(|&α| F::from_nalgebra_mixed(α)));
176 } 313 }
177 } 314
178 315 // /// Extracts the masses of the spikes as a [`Vec`].
179 impl<Domain, F :Num> Index<usize> for DiscreteMeasure<Domain, F> { 316 // pub fn masses_vec(&self) -> Vec<F::MixedType> {
180 type Output = DeltaMeasure<Domain, F>; 317 // self.iter_masses()
181 #[inline] 318 // .map(|α| α.to_nalgebra_mixed())
182 fn index(&self, i : usize) -> &Self::Output { 319 // .collect()
320 // }
321
322 // /// Sets the masses of the spikes from the values of a [`Vec`].
323 // pub fn set_masses_vec(&mut self, x : &Vec<F::MixedType>) {
324 // self.set_masses(x.iter().map(|&α| F::from_nalgebra_mixed(α)));
325 // }
326 }
327
328 // impl<Domain, F :Num> Index<usize> for DiscreteMeasure<Domain, F> {
329 // type Output = DeltaMeasure<Domain, F>;
330 // #[inline]
331 // fn index(&self, i : usize) -> &Self::Output {
332 // self.spikes.index(i)
333 // }
334 // }
335
336 // impl<Domain, F :Num> IndexMut<usize> for DiscreteMeasure<Domain, F> {
337 // #[inline]
338 // fn index_mut(&mut self, i : usize) -> &mut Self::Output {
339 // self.spikes.index_mut(i)
340 // }
341 // }
342
343 impl<
344 Domain,
345 F : Num,
346 I : std::slice::SliceIndex<[DeltaMeasure<Domain, F>]>
347 > Index<I>
348 for DiscreteMeasure<Domain, F> {
349 type Output = <I as std::slice::SliceIndex<[DeltaMeasure<Domain, F>]>>::Output;
350 #[inline]
351 fn index(&self, i : I) -> &Self::Output {
183 self.spikes.index(i) 352 self.spikes.index(i)
184 } 353 }
185 } 354 }
186 355
187 impl<Domain, F :Num> IndexMut<usize> for DiscreteMeasure<Domain, F> { 356 impl<
188 #[inline] 357 Domain,
189 fn index_mut(&mut self, i : usize) -> &mut Self::Output { 358 F : Num,
359 I : std::slice::SliceIndex<[DeltaMeasure<Domain, F>]>
360 > IndexMut<I>
361 for DiscreteMeasure<Domain, F> {
362 #[inline]
363 fn index_mut(&mut self, i : I) -> &mut Self::Output {
190 self.spikes.index_mut(i) 364 self.spikes.index_mut(i)
191 } 365 }
192 } 366 }
367
193 368
194 impl<Domain, F : Num, D : Into<DeltaMeasure<Domain, F>>, const K : usize> From<[D; K]> 369 impl<Domain, F : Num, D : Into<DeltaMeasure<Domain, F>>, const K : usize> From<[D; K]>
195 for DiscreteMeasure<Domain, F> { 370 for DiscreteMeasure<Domain, F> {
196 #[inline] 371 #[inline]
197 fn from(list : [D; K]) -> Self { 372 fn from(list : [D; K]) -> Self {
198 list.into_iter().collect() 373 list.into_iter().collect()
199 } 374 }
200 } 375 }
376
377 impl<Domain, F : Num> From<Vec<DeltaMeasure<Domain, F>>>
378 for DiscreteMeasure<Domain, F> {
379 #[inline]
380 fn from(spikes : Vec<DeltaMeasure<Domain, F>>) -> Self {
381 DiscreteMeasure{ spikes }
382 }
383 }
384
385 impl<'a, Domain, F : Num, D> From<&'a [D]>
386 for DiscreteMeasure<Domain, F>
387 where &'a D : Into<DeltaMeasure<Domain, F>> {
388 #[inline]
389 fn from(list : &'a [D]) -> Self {
390 list.into_iter().map(|d| d.into()).collect()
391 }
392 }
393
394
395 impl<Domain, F : Num> From<DeltaMeasure<Domain, F>>
396 for DiscreteMeasure<Domain, F> {
397 #[inline]
398 fn from(δ : DeltaMeasure<Domain, F>) -> Self {
399 DiscreteMeasure{
400 spikes : vec!(δ)
401 }
402 }
403 }
404
405 impl<'a, Domain : Clone, F : Num> From<&'a DeltaMeasure<Domain, F>>
406 for DiscreteMeasure<Domain, F> {
407 #[inline]
408 fn from(δ : &'a DeltaMeasure<Domain, F>) -> Self {
409 DiscreteMeasure{
410 spikes : vec!(δ.clone())
411 }
412 }
413 }
414
201 415
202 impl<Domain, F : Num, D : Into<DeltaMeasure<Domain, F>>> FromIterator<D> 416 impl<Domain, F : Num, D : Into<DeltaMeasure<Domain, F>>> FromIterator<D>
203 for DiscreteMeasure<Domain, F> { 417 for DiscreteMeasure<Domain, F> {
204 #[inline] 418 #[inline]
205 fn from_iter<T>(iter : T) -> Self 419 fn from_iter<T>(iter : T) -> Self
256 fn norm(&self, _ : Radon) -> F { 470 fn norm(&self, _ : Radon) -> F {
257 self.spikes.iter().map(|m| m.norm(Radon)).sum() 471 self.spikes.iter().map(|m| m.norm(Radon)).sum()
258 } 472 }
259 } 473 }
260 474
261 impl<Domain, G, F : Num, Y : Sum + Mul<F, Output=Y>> Apply<G> for DiscreteMeasure<Domain, F> 475 impl<Domain, G, F : Num> Mapping<G> for DiscreteMeasure<Domain, F>
262 where G: for<'a> Apply<&'a Domain, Output = Y> { 476 where
263 type Output = Y; 477 Domain : Space,
264 #[inline] 478 G::Codomain : Sum + Mul<F, Output=G::Codomain>,
265 fn apply(&self, g : G) -> Y { 479 G : Mapping<Domain, Codomain=F> + Clone + Space,
266 self.spikes.iter().map(|m| g.apply(&m.x) * m.α).sum() 480 for<'b> &'b Domain : Instance<Domain>,
267 } 481 {
268 } 482 type Codomain = G::Codomain;
269 483
270 impl<Domain, G, F : Num, Y : Sum + Mul<F, Output=Y>> Linear<G> for DiscreteMeasure<Domain, F> 484 #[inline]
271 where G : for<'a> Apply<&'a Domain, Output = Y> { 485 fn apply<I : Instance<G>>(&self, g : I) -> Self::Codomain {
272 type Codomain = Y; 486 g.eval(|g| self.spikes.iter().map(|m| g.apply(&m.x) * m.α).sum())
273 } 487 }
488 }
489
490 impl<Domain, G, F : Num> Linear<G> for DiscreteMeasure<Domain, F>
491 where
492 Domain : Space,
493 G::Codomain : Sum + Mul<F, Output=G::Codomain>,
494 G : Mapping<Domain, Codomain=F> + Clone + Space,
495 for<'b> &'b Domain : Instance<Domain>,
496 { }
274 497
275 498
276 /// Helper trait for constructing arithmetic operations for combinations 499 /// Helper trait for constructing arithmetic operations for combinations
277 /// of [`DiscreteMeasure`] and [`DeltaMeasure`], and their references. 500 /// of [`DiscreteMeasure`] and [`DeltaMeasure`], and their references.
278 trait Lift<F : Num, Domain> { 501 trait Lift<F : Num, Domain> {
279 type Producer : Iterator<Item=DeltaMeasure<Domain, F>>; 502 type Producer : Iterator<Item=DeltaMeasure<Domain, F>>;
280 503
504 #[allow(dead_code)]
281 /// Lifts `self` into a [`DiscreteMeasure`]. 505 /// Lifts `self` into a [`DiscreteMeasure`].
282 fn lift(self) -> DiscreteMeasure<Domain, F>; 506 fn lift(self) -> DiscreteMeasure<Domain, F>;
283 507
284 /// Lifts `self` into a [`DiscreteMeasure`], apply either `f` or `f_mut` whether the type 508 /// Lifts `self` into a [`DiscreteMeasure`], apply either `f` or `f_mut` whether the type
285 /// this method is implemented for is a reference or or not. 509 /// this method is implemented for is a reference or or not.
572 )+ } 796 )+ }
573 } 797 }
574 798
575 make_discrete_scalarop_lhs!(Mul, mul; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); 799 make_discrete_scalarop_lhs!(Mul, mul; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize);
576 make_discrete_scalarop_lhs!(Div, div; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); 800 make_discrete_scalarop_lhs!(Div, div; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize);
801
802 impl<F : Num, Domain> Collection for DiscreteMeasure<Domain, F> {
803 type Element = DeltaMeasure<Domain, F>;
804 type RefsIter<'a> = std::slice::Iter<'a, Self::Element> where Self : 'a;
805
806 #[inline]
807 fn iter_refs(&self) -> Self::RefsIter<'_> {
808 self.iter_spikes()
809 }
810 }
811
812 impl<Domain : Clone, F : Num> Space for DiscreteMeasure<Domain, F> {
813 type Decomp = MeasureDecomp;
814 }
815
816 pub type SpikeSlice<'b, Domain, F> = &'b [DeltaMeasure<Domain, F>];
817
818 pub type EitherSlice<'b, Domain, F> = EitherDecomp<
819 Vec<DeltaMeasure<Domain, F>>,
820 SpikeSlice<'b, Domain, F>
821 >;
822
823 impl<F : Num, Domain : Clone> Decomposition<DiscreteMeasure<Domain, F>> for MeasureDecomp {
824 type Decomposition<'b> = EitherSlice<'b, Domain, F> where DiscreteMeasure<Domain, F> : 'b;
825 type Reference<'b> = SpikeSlice<'b, Domain, F> where DiscreteMeasure<Domain, F> : 'b;
826
827 /// Left the lightweight reference type into a full decomposition type.
828 fn lift<'b>(r : Self::Reference<'b>) -> Self::Decomposition<'b> {
829 EitherDecomp::Borrowed(r)
830 }
831 }
832
833 impl<F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp>
834 for DiscreteMeasure<Domain, F>
835 {
836 fn decompose<'b>(self)
837 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b>
838 where Self : 'b, DiscreteMeasure<Domain, F> : 'b {
839 EitherDecomp::Owned(self.spikes)
840 }
841
842 fn ref_instance(&self)
843 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_>
844 {
845 self.spikes.as_slice()
846 }
847
848 fn cow<'b>(self) -> MyCow<'b, DiscreteMeasure<Domain, F>> where Self : 'b {
849 MyCow::Owned(self)
850 }
851
852 fn own(self) -> DiscreteMeasure<Domain, F> {
853 self
854 }
855 }
856
857 impl<'a, F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp>
858 for &'a DiscreteMeasure<Domain, F>
859 {
860 fn decompose<'b>(self)
861 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b>
862 where Self : 'b, DiscreteMeasure<Domain, F> : 'b {
863 EitherDecomp::Borrowed(self.spikes.as_slice())
864 }
865
866 fn ref_instance(&self)
867 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_>
868 {
869 self.spikes.as_slice()
870 }
871
872 fn cow<'b>(self) -> MyCow<'b, DiscreteMeasure<Domain, F>> where Self : 'b {
873 MyCow::Borrowed(self)
874 }
875
876 fn own(self) -> DiscreteMeasure<Domain, F> {
877 self.clone()
878 }
879 }
880
881 impl<'a, F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp>
882 for EitherSlice<'a, Domain, F>
883 {
884 fn decompose<'b>(self)
885 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b>
886 where Self : 'b, DiscreteMeasure<Domain, F> : 'b {
887 self
888 }
889
890 fn ref_instance(&self)
891 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_>
892 {
893 match self {
894 EitherDecomp::Owned(v) => v.as_slice(),
895 EitherDecomp::Borrowed(s) => s,
896 }
897 }
898
899 fn own(self) -> DiscreteMeasure<Domain, F> {
900 match self {
901 EitherDecomp::Owned(v) => v.into(),
902 EitherDecomp::Borrowed(s) => s.into(),
903 }
904 }
905 }
906
907 impl<'a, F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp>
908 for &'a EitherSlice<'a, Domain, F>
909 {
910 fn decompose<'b>(self)
911 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b>
912 where Self : 'b, DiscreteMeasure<Domain, F> : 'b {
913 match self {
914 EitherDecomp::Owned(v) => EitherDecomp::Borrowed(v.as_slice()),
915 EitherDecomp::Borrowed(s) => EitherDecomp::Borrowed(s),
916 }
917 }
918
919 fn ref_instance(&self)
920 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_>
921 {
922 match self {
923 EitherDecomp::Owned(v) => v.as_slice(),
924 EitherDecomp::Borrowed(s) => s,
925 }
926 }
927
928 fn own(self) -> DiscreteMeasure<Domain, F> {
929 match self {
930 EitherDecomp::Owned(v) => v.as_slice(),
931 EitherDecomp::Borrowed(s) => s
932 }.into()
933 }
934 }
935
936 impl<'a, F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp>
937 for SpikeSlice<'a, Domain, F>
938 {
939 fn decompose<'b>(self)
940 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b>
941 where Self : 'b, DiscreteMeasure<Domain, F> : 'b {
942 EitherDecomp::Borrowed(self)
943 }
944
945 fn ref_instance(&self)
946 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_>
947 {
948 self
949 }
950
951 fn own(self) -> DiscreteMeasure<Domain, F> {
952 self.into()
953 }
954 }
955
956 impl<'a, F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp>
957 for &'a SpikeSlice<'a, Domain, F>
958 {
959 fn decompose<'b>(self)
960 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b>
961 where Self : 'b, DiscreteMeasure<Domain, F> : 'b {
962 EitherDecomp::Borrowed(*self)
963 }
964
965 fn ref_instance(&self)
966 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_>
967 {
968 *self
969 }
970
971 fn own(self) -> DiscreteMeasure<Domain, F> {
972 (*self).into()
973 }
974 }
975
976 impl<F : Num, Domain : Clone > Instance<DiscreteMeasure<Domain, F>, MeasureDecomp>
977 for DeltaMeasure<Domain, F>
978 {
979 fn decompose<'b>(self)
980 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b>
981 where Self : 'b, DiscreteMeasure<Domain, F> : 'b {
982 EitherDecomp::Owned(vec![self])
983 }
984
985 fn ref_instance(&self)
986 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_>
987 {
988 std::slice::from_ref(self)
989 }
990
991 fn own(self) -> DiscreteMeasure<Domain, F> {
992 self.into()
993 }
994 }
995
996 impl<'a, F : Num, Domain : Clone> Instance<DiscreteMeasure<Domain, F>, MeasureDecomp>
997 for &'a DeltaMeasure<Domain, F>
998 {
999 fn decompose<'b>(self)
1000 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Decomposition<'b>
1001 where Self : 'b, DiscreteMeasure<Domain, F> : 'b {
1002 EitherDecomp::Borrowed(std::slice::from_ref(self))
1003 }
1004
1005 fn ref_instance(&self)
1006 -> <MeasureDecomp as Decomposition<DiscreteMeasure<Domain, F>>>::Reference<'_>
1007 {
1008 std::slice::from_ref(*self)
1009 }
1010
1011 fn own(self) -> DiscreteMeasure<Domain, F> {
1012 self.into()
1013 }
1014 }

mercurial