Tue, 31 Dec 2024 09:34:24 -0500
Early transport sketches
0 | 1 | //! This module implementes discrete measures. |
2 | ||
3 | use std::ops::{ | |
4 | Div,Mul,DivAssign,MulAssign,Neg, | |
5 | Add,Sub,AddAssign,SubAssign, | |
6 | Index,IndexMut, | |
7 | }; | |
8 | use std::iter::Sum; | |
9 | use serde::ser::{Serializer, Serialize, SerializeSeq}; | |
10 | use nalgebra::DVector; | |
11 | ||
12 | use alg_tools::norms::Norm; | |
13 | use alg_tools::tabledump::TableDump; | |
14 | use alg_tools::linops::{Apply, Linear}; | |
15 | use alg_tools::iter::{MapF,Mappable}; | |
16 | use alg_tools::nalgebra_support::ToNalgebraRealField; | |
17 | ||
18 | use crate::types::*; | |
19 | use super::base::*; | |
20 | use super::delta::*; | |
21 | ||
22 | /// Representation of a discrete measure. | |
23 | /// | |
24 | /// This is the measure $μ = ∑_{k=1}^n α_k δ_{x_k}$, consisting of several | |
25 | /// [`DeltaMeasure`], i.e., “spikes” $α_k δ_{x_k}$ with weights $\alpha_k$ in `F` at locations | |
26 | /// $x_k$ in `Domain`. | |
27 | #[derive(Clone,Debug)] | |
28 | pub struct DiscreteMeasure<Domain, F : Num> { | |
29 | pub(super) spikes : Vec<DeltaMeasure<Domain, F>>, | |
30 | } | |
31 | ||
32 | /// Iterator over the [`DeltaMeasure`] spikes of a [`DiscreteMeasure`]. | |
33 | pub type SpikeIter<'a, Domain, F> = std::slice::Iter<'a, DeltaMeasure<Domain, F>>; | |
34 | ||
35 | /// Iterator over mutable [`DeltaMeasure`] spikes of a [`DiscreteMeasure`]. | |
36 | pub type SpikeIterMut<'a, Domain, F> = std::slice::IterMut<'a, DeltaMeasure<Domain, F>>; | |
37 | ||
38 | /// Iterator over the locations of the spikes of a [`DiscreteMeasure`]. | |
39 | pub type LocationIter<'a, Domain, F> | |
40 | = std::iter::Map<SpikeIter<'a, Domain, F>, fn(&'a DeltaMeasure<Domain, F>) -> &'a Domain>; | |
41 | ||
42 | /// Iterator over the masses of the spikes of a [`DiscreteMeasure`]. | |
43 | pub type MassIter<'a, Domain, F> | |
44 | = std::iter::Map<SpikeIter<'a, Domain, F>, fn(&'a DeltaMeasure<Domain, F>) -> F>; | |
45 | ||
46 | /// Iterator over the mutable locations of the spikes of a [`DiscreteMeasure`]. | |
47 | pub type MassIterMut<'a, Domain, F> | |
48 | = std::iter::Map<SpikeIterMut<'a, Domain, F>, for<'r> fn(&'r mut DeltaMeasure<Domain, F>) -> &'r mut F>; | |
49 | ||
50 | impl<Domain, F : Num> DiscreteMeasure<Domain, F> { | |
51 | /// Create a new zero measure (empty spike set). | |
52 | pub fn new() -> Self { | |
53 | DiscreteMeasure{ spikes : Vec::new() } | |
54 | } | |
55 | ||
56 | /// Number of [`DeltaMeasure`] spikes in the measure | |
57 | #[inline] | |
58 | pub fn len(&self) -> usize { | |
59 | self.spikes.len() | |
60 | } | |
61 | ||
32 | 62 | /// Replace with the zero measure. |
63 | #[inline] | |
64 | pub fn clear(&mut self) { | |
65 | self.spikes.clear() | |
66 | } | |
67 | ||
68 | /// Remove `i`:th spike, not maintaining order. | |
69 | /// | |
70 | /// Panics if indiex is out of bounds. | |
71 | #[inline] | |
72 | pub fn swap_remove(&mut self, i : usize) -> DeltaMeasure<Domain, F>{ | |
73 | self.spikes.swap_remove(i) | |
74 | } | |
75 | ||
0 | 76 | /// Iterate over (references to) the [`DeltaMeasure`] spikes in this measure |
77 | #[inline] | |
78 | pub fn iter_spikes(&self) -> SpikeIter<'_, Domain, F> { | |
79 | self.spikes.iter() | |
80 | } | |
81 | ||
82 | /// Iterate over mutable references to the [`DeltaMeasure`] spikes in this measure | |
83 | #[inline] | |
84 | pub fn iter_spikes_mut(&mut self) -> SpikeIterMut<'_, Domain, F> { | |
85 | self.spikes.iter_mut() | |
86 | } | |
87 | ||
88 | /// Iterate over the location of the spikes in this measure | |
89 | #[inline] | |
90 | pub fn iter_locations(&self) -> LocationIter<'_, Domain, F> { | |
91 | self.iter_spikes().map(DeltaMeasure::get_location) | |
92 | } | |
93 | ||
94 | /// Iterate over the masses of the spikes in this measure | |
95 | #[inline] | |
96 | pub fn iter_masses(&self) -> MassIter<'_, Domain, F> { | |
97 | self.iter_spikes().map(DeltaMeasure::get_mass) | |
98 | } | |
99 | ||
100 | /// Iterate over the masses of the spikes in this measure | |
101 | #[inline] | |
102 | pub fn iter_masses_mut(&mut self) -> MassIterMut<'_, Domain, F> { | |
103 | self.iter_spikes_mut().map(DeltaMeasure::get_mass_mut) | |
104 | } | |
105 | ||
106 | /// Update the masses of all the spikes to those produced by an iterator. | |
107 | #[inline] | |
108 | pub fn set_masses<I : Iterator<Item=F>>(&mut self, iter : I) { | |
109 | self.spikes.iter_mut().zip(iter).for_each(|(δ, α)| δ.set_mass(α)); | |
110 | } | |
111 | ||
112 | // /// Map the masses of all the spikes using a function and an iterator | |
113 | // #[inline] | |
114 | // pub fn zipmap_masses< | |
115 | // I : Iterator<Item=F>, | |
116 | // G : Fn(F, I::Item) -> F | |
117 | // > (&mut self, iter : I, g : G) { | |
118 | // self.spikes.iter_mut().zip(iter).for_each(|(δ, v)| δ.set_mass(g(δ.get_mass(), v))); | |
119 | // } | |
120 | ||
121 | /// Prune all spikes with zero mass. | |
122 | #[inline] | |
123 | pub fn prune(&mut self) { | |
124 | self.spikes.retain(|δ| δ.α != F::ZERO); | |
125 | } | |
32 | 126 | |
127 | /// Add the spikes produced by `iter` to this measure. | |
128 | #[inline] | |
129 | pub fn extend<I : Iterator<Item=DeltaMeasure<Domain, F>>>( | |
130 | &mut self, | |
131 | iter : I | |
132 | ) { | |
133 | self.spikes.extend(iter); | |
134 | } | |
135 | ||
136 | /// Add a spike to the measure | |
137 | #[inline] | |
138 | pub fn push(&mut self, δ : DeltaMeasure<Domain, F>) { | |
139 | self.spikes.push(δ); | |
140 | } | |
141 | } | |
142 | ||
143 | impl<Domain, F : Num> IntoIterator for DiscreteMeasure<Domain, F> { | |
144 | type Item = DeltaMeasure<Domain, F>; | |
145 | type IntoIter = <Vec<DeltaMeasure<Domain, F>> as IntoIterator>::IntoIter; | |
146 | ||
147 | #[inline] | |
148 | fn into_iter(self) -> Self::IntoIter { | |
149 | self.spikes.into_iter() | |
150 | } | |
0 | 151 | } |
152 | ||
153 | impl<Domain : Clone, F : Float> DiscreteMeasure<Domain, F> { | |
154 | /// Computes `μ1 ← θ * μ1 - ζ * μ2`, pruning entries where both `μ1` (`self`) and `μ2` have | |
155 | // zero weight. `μ2` will contain copy of pruned original `μ1` without arithmetic performed. | |
156 | /// **This expects `self` and `μ2` to have matching coordinates in each index**. | |
157 | // `μ2` can be than `self`, but not longer. | |
158 | pub fn pruning_sub(&mut self, θ : F, ζ : F, μ2 : &mut Self) { | |
159 | let mut μ2_get = 0; | |
160 | let mut μ2_insert = 0; | |
32 | 161 | self.spikes.retain_mut(|&mut DeltaMeasure{ α : ref mut α_ref, ref x }| { |
0 | 162 | // Get weight of spike in μ2, zero if out of bounds. |
163 | let β = μ2.spikes.get(μ2_get).map_or(F::ZERO, DeltaMeasure::get_mass); | |
164 | μ2_get += 1; | |
165 | ||
166 | if *α_ref == F::ZERO && β == F::ZERO { | |
167 | // Prune | |
168 | true | |
169 | } else { | |
170 | // Save self weight | |
171 | let α = *α_ref; | |
172 | // Modify self | |
173 | *α_ref = θ * α - ζ * β; | |
174 | // Make copy of old self weight in μ2 | |
175 | let δ = DeltaMeasure{ α, x : x.clone() }; | |
176 | match μ2.spikes.get_mut(μ2_insert) { | |
177 | Some(replace) => { | |
178 | *replace = δ; | |
179 | }, | |
180 | None => { | |
181 | debug_assert_eq!(μ2.len(), μ2_insert); | |
182 | μ2.spikes.push(δ); | |
183 | }, | |
184 | } | |
185 | μ2_insert += 1; | |
186 | // Keep | |
187 | false | |
188 | } | |
189 | }); | |
190 | // Truncate μ2 to same length as self. | |
191 | μ2.spikes.truncate(μ2_insert); | |
192 | debug_assert_eq!(μ2.len(), self.len()); | |
193 | } | |
194 | } | |
195 | ||
196 | impl<Domain, F : Float> DiscreteMeasure<Domain, F> { | |
197 | /// Prune all spikes with mass absolute value less than the given `tolerance`. | |
198 | #[inline] | |
199 | pub fn prune_approx(&mut self, tolerance : F) { | |
200 | self.spikes.retain(|δ| δ.α.abs() > tolerance); | |
201 | } | |
202 | } | |
203 | ||
204 | impl<Domain, F : Float + ToNalgebraRealField> DiscreteMeasure<Domain, F> { | |
205 | /// Extracts the masses of the spikes as a [`DVector`]. | |
206 | pub fn masses_dvector(&self) -> DVector<F::MixedType> { | |
207 | DVector::from_iterator(self.len(), | |
208 | self.iter_masses() | |
209 | .map(|α| α.to_nalgebra_mixed())) | |
210 | } | |
211 | ||
212 | /// Sets the masses of the spikes from the values of a [`DVector`]. | |
213 | pub fn set_masses_dvector(&mut self, x : &DVector<F::MixedType>) { | |
214 | self.set_masses(x.iter().map(|&α| F::from_nalgebra_mixed(α))); | |
215 | } | |
216 | } | |
217 | ||
32 | 218 | // impl<Domain, F :Num> Index<usize> for DiscreteMeasure<Domain, F> { |
219 | // type Output = DeltaMeasure<Domain, F>; | |
220 | // #[inline] | |
221 | // fn index(&self, i : usize) -> &Self::Output { | |
222 | // self.spikes.index(i) | |
223 | // } | |
224 | // } | |
225 | ||
226 | // impl<Domain, F :Num> IndexMut<usize> for DiscreteMeasure<Domain, F> { | |
227 | // #[inline] | |
228 | // fn index_mut(&mut self, i : usize) -> &mut Self::Output { | |
229 | // self.spikes.index_mut(i) | |
230 | // } | |
231 | // } | |
232 | ||
233 | impl< | |
234 | Domain, | |
235 | F : Num, | |
236 | I : std::slice::SliceIndex<[DeltaMeasure<Domain, F>]> | |
237 | > Index<I> | |
238 | for DiscreteMeasure<Domain, F> { | |
239 | type Output = <I as std::slice::SliceIndex<[DeltaMeasure<Domain, F>]>>::Output; | |
0 | 240 | #[inline] |
32 | 241 | fn index(&self, i : I) -> &Self::Output { |
0 | 242 | self.spikes.index(i) |
243 | } | |
244 | } | |
245 | ||
32 | 246 | impl< |
247 | Domain, | |
248 | F : Num, | |
249 | I : std::slice::SliceIndex<[DeltaMeasure<Domain, F>]> | |
250 | > IndexMut<I> | |
251 | for DiscreteMeasure<Domain, F> { | |
0 | 252 | #[inline] |
32 | 253 | fn index_mut(&mut self, i : I) -> &mut Self::Output { |
0 | 254 | self.spikes.index_mut(i) |
255 | } | |
256 | } | |
257 | ||
32 | 258 | |
0 | 259 | impl<Domain, F : Num, D : Into<DeltaMeasure<Domain, F>>, const K : usize> From<[D; K]> |
260 | for DiscreteMeasure<Domain, F> { | |
261 | #[inline] | |
262 | fn from(list : [D; K]) -> Self { | |
263 | list.into_iter().collect() | |
264 | } | |
265 | } | |
266 | ||
267 | impl<Domain, F : Num, D : Into<DeltaMeasure<Domain, F>>> FromIterator<D> | |
268 | for DiscreteMeasure<Domain, F> { | |
269 | #[inline] | |
270 | fn from_iter<T>(iter : T) -> Self | |
271 | where T : IntoIterator<Item=D> { | |
272 | DiscreteMeasure{ | |
273 | spikes : iter.into_iter().map(|m| m.into()).collect() | |
274 | } | |
275 | } | |
276 | } | |
277 | ||
278 | impl<'a, F : Num, const N : usize> TableDump<'a> | |
279 | for DiscreteMeasure<Loc<F, N>,F> | |
280 | where DeltaMeasure<Loc<F, N>, F> : Serialize + 'a { | |
281 | type Iter = std::slice::Iter<'a, DeltaMeasure<Loc<F, N>, F>>; | |
282 | ||
283 | // fn tabledump_headers(&'a self) -> Vec<String> { | |
284 | // let mut v : Vec<String> = (0..N).map(|i| format!("x{}", i)).collect(); | |
285 | // v.push("weight".into()); | |
286 | // v | |
287 | // } | |
288 | ||
289 | fn tabledump_entries(&'a self) -> Self::Iter { | |
290 | // Ensure order matching the headers above | |
291 | self.spikes.iter() | |
292 | } | |
293 | } | |
294 | ||
295 | // Need to manually implement serialisation for DeltaMeasure<Loc<F, N>, F> [`csv`] writer fails on | |
296 | // structs with nested arrays as well as with #[serde(flatten)]. | |
297 | // Then derive no longer works for DiscreteMeasure | |
298 | impl<F : Num, const N : usize> Serialize for DiscreteMeasure<Loc<F, N>, F> | |
299 | where | |
300 | F: Serialize, | |
301 | { | |
302 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | |
303 | where | |
304 | S: Serializer, | |
305 | { | |
306 | let mut s = serializer.serialize_seq(Some(self.spikes.len()))?; | |
307 | for δ in self.spikes.iter() { | |
308 | s.serialize_element(δ)?; | |
309 | } | |
310 | s.end() | |
311 | } | |
312 | } | |
313 | ||
314 | impl<Domain : PartialEq, F : Float> Measure<F> for DiscreteMeasure<Domain, F> { | |
315 | type Domain = Domain; | |
316 | } | |
317 | ||
318 | impl<Domain : PartialEq, F : Float> Norm<F, Radon> for DiscreteMeasure<Domain, F> | |
319 | where DeltaMeasure<Domain, F> : Norm<F, Radon> { | |
320 | #[inline] | |
321 | fn norm(&self, _ : Radon) -> F { | |
322 | self.spikes.iter().map(|m| m.norm(Radon)).sum() | |
323 | } | |
324 | } | |
325 | ||
326 | impl<Domain, G, F : Num, Y : Sum + Mul<F, Output=Y>> Apply<G> for DiscreteMeasure<Domain, F> | |
327 | where G: for<'a> Apply<&'a Domain, Output = Y> { | |
328 | type Output = Y; | |
329 | #[inline] | |
330 | fn apply(&self, g : G) -> Y { | |
331 | self.spikes.iter().map(|m| g.apply(&m.x) * m.α).sum() | |
332 | } | |
333 | } | |
334 | ||
335 | impl<Domain, G, F : Num, Y : Sum + Mul<F, Output=Y>> Linear<G> for DiscreteMeasure<Domain, F> | |
336 | where G : for<'a> Apply<&'a Domain, Output = Y> { | |
337 | type Codomain = Y; | |
338 | } | |
339 | ||
340 | ||
341 | /// Helper trait for constructing arithmetic operations for combinations | |
342 | /// of [`DiscreteMeasure`] and [`DeltaMeasure`], and their references. | |
343 | trait Lift<F : Num, Domain> { | |
344 | type Producer : Iterator<Item=DeltaMeasure<Domain, F>>; | |
345 | ||
346 | /// Lifts `self` into a [`DiscreteMeasure`]. | |
347 | fn lift(self) -> DiscreteMeasure<Domain, F>; | |
348 | ||
349 | /// Lifts `self` into a [`DiscreteMeasure`], apply either `f` or `f_mut` whether the type | |
350 | /// this method is implemented for is a reference or or not. | |
351 | fn lift_with(self, | |
352 | f : impl Fn(&DeltaMeasure<Domain, F>) -> DeltaMeasure<Domain, F>, | |
353 | f_mut : impl FnMut(&mut DeltaMeasure<Domain, F>)) | |
354 | -> DiscreteMeasure<Domain, F>; | |
355 | ||
356 | /// Extend `self` into a [`DiscreteMeasure`] with the spikes produced by `iter`. | |
357 | fn lift_extend<I : Iterator<Item=DeltaMeasure<Domain, F>>>( | |
358 | self, | |
359 | iter : I | |
360 | ) -> DiscreteMeasure<Domain, F>; | |
361 | ||
362 | /// Returns an iterator for producing copies of the spikes of `self`. | |
363 | fn produce(self) -> Self::Producer; | |
364 | } | |
365 | ||
366 | impl<F : Num, Domain> Lift<F, Domain> for DiscreteMeasure<Domain, F> { | |
367 | type Producer = std::vec::IntoIter<DeltaMeasure<Domain, F>>; | |
368 | ||
369 | #[inline] | |
370 | fn lift(self) -> DiscreteMeasure<Domain, F> { self } | |
371 | ||
372 | fn lift_with(mut self, | |
373 | _f : impl Fn(&DeltaMeasure<Domain, F>) -> DeltaMeasure<Domain, F>, | |
374 | f_mut : impl FnMut(&mut DeltaMeasure<Domain, F>)) | |
375 | -> DiscreteMeasure<Domain, F> { | |
376 | self.spikes.iter_mut().for_each(f_mut); | |
377 | self | |
378 | } | |
379 | ||
380 | #[inline] | |
381 | fn lift_extend<I : Iterator<Item=DeltaMeasure<Domain, F>>>( | |
382 | mut self, | |
383 | iter : I | |
384 | ) -> DiscreteMeasure<Domain, F> { | |
385 | self.spikes.extend(iter); | |
386 | self | |
387 | } | |
388 | ||
389 | #[inline] | |
390 | fn produce(self) -> Self::Producer { | |
391 | self.spikes.into_iter() | |
392 | } | |
393 | } | |
394 | ||
395 | impl<'a, F : Num, Domain : Clone> Lift<F, Domain> for &'a DiscreteMeasure<Domain, F> { | |
396 | type Producer = MapF<std::slice::Iter<'a, DeltaMeasure<Domain, F>>, DeltaMeasure<Domain, F>>; | |
397 | ||
398 | #[inline] | |
399 | fn lift(self) -> DiscreteMeasure<Domain, F> { self.clone() } | |
400 | ||
401 | fn lift_with(self, | |
402 | f : impl Fn(&DeltaMeasure<Domain, F>) -> DeltaMeasure<Domain, F>, | |
403 | _f_mut : impl FnMut(&mut DeltaMeasure<Domain, F>)) | |
404 | -> DiscreteMeasure<Domain, F> { | |
405 | DiscreteMeasure{ spikes : self.spikes.iter().map(f).collect() } | |
406 | } | |
407 | ||
408 | #[inline] | |
409 | fn lift_extend<I : Iterator<Item=DeltaMeasure<Domain, F>>>( | |
410 | self, | |
411 | iter : I | |
412 | ) -> DiscreteMeasure<Domain, F> { | |
413 | let mut res = self.clone(); | |
414 | res.spikes.extend(iter); | |
415 | res | |
416 | } | |
417 | ||
418 | #[inline] | |
419 | fn produce(self) -> Self::Producer { | |
420 | // TODO: maybe not optimal to clone here and would benefit from | |
421 | // a reference version of lift_extend. | |
422 | self.spikes.iter().mapF(Clone::clone) | |
423 | } | |
424 | } | |
425 | ||
426 | impl<F : Num, Domain> Lift<F, Domain> for DeltaMeasure<Domain, F> { | |
427 | type Producer = std::iter::Once<DeltaMeasure<Domain, F>>; | |
428 | ||
429 | #[inline] | |
430 | fn lift(self) -> DiscreteMeasure<Domain, F> { DiscreteMeasure { spikes : vec![self] } } | |
431 | ||
432 | #[inline] | |
433 | fn lift_with(mut self, | |
434 | _f : impl Fn(&DeltaMeasure<Domain, F>) -> DeltaMeasure<Domain, F>, | |
435 | mut f_mut : impl FnMut(&mut DeltaMeasure<Domain, F>)) | |
436 | -> DiscreteMeasure<Domain, F> { | |
437 | f_mut(&mut self); | |
438 | DiscreteMeasure{ spikes : vec![self] } | |
439 | } | |
440 | ||
441 | #[inline] | |
442 | fn lift_extend<I : Iterator<Item=DeltaMeasure<Domain, F>>>( | |
443 | self, | |
444 | iter : I | |
445 | ) -> DiscreteMeasure<Domain, F> { | |
446 | let mut spikes = vec![self]; | |
447 | spikes.extend(iter); | |
448 | DiscreteMeasure{ spikes : spikes } | |
449 | } | |
450 | ||
451 | #[inline] | |
452 | fn produce(self) -> Self::Producer { | |
453 | std::iter::once(self) | |
454 | } | |
455 | } | |
456 | ||
457 | impl<'a, F : Num, Domain : Clone> Lift<F, Domain> for &'a DeltaMeasure<Domain, F> { | |
458 | type Producer = std::iter::Once<DeltaMeasure<Domain, F>>; | |
459 | ||
460 | #[inline] | |
461 | fn lift(self) -> DiscreteMeasure<Domain, F> { DiscreteMeasure { spikes : vec![self.clone()] } } | |
462 | ||
463 | #[inline] | |
464 | fn lift_with(self, | |
465 | f : impl Fn(&DeltaMeasure<Domain, F>) -> DeltaMeasure<Domain, F>, | |
466 | _f_mut : impl FnMut(&mut DeltaMeasure<Domain, F>)) | |
467 | -> DiscreteMeasure<Domain, F> { | |
468 | DiscreteMeasure{ spikes : vec![f(self)] } | |
469 | } | |
470 | ||
471 | #[inline] | |
472 | fn lift_extend<I : Iterator<Item=DeltaMeasure<Domain, F>>>( | |
473 | self, | |
474 | iter : I | |
475 | ) -> DiscreteMeasure<Domain, F> { | |
476 | let mut spikes = vec![self.clone()]; | |
477 | spikes.extend(iter); | |
478 | DiscreteMeasure{ spikes : spikes } | |
479 | } | |
480 | ||
481 | #[inline] | |
482 | fn produce(self) -> Self::Producer { | |
483 | std::iter::once(self.clone()) | |
484 | } | |
485 | } | |
486 | ||
487 | macro_rules! make_discrete_addsub_assign { | |
488 | ($rhs:ty) => { | |
489 | // Discrete += (&)Discrete | |
490 | impl<'a, F : Num, Domain : Clone> AddAssign<$rhs> | |
491 | for DiscreteMeasure<Domain, F> { | |
492 | fn add_assign(&mut self, other : $rhs) { | |
493 | self.spikes.extend(other.produce()); | |
494 | } | |
495 | } | |
496 | ||
497 | impl<'a, F : Num + Neg<Output=F>, Domain : Clone> SubAssign<$rhs> | |
498 | for DiscreteMeasure<Domain, F> { | |
499 | fn sub_assign(&mut self, other : $rhs) { | |
500 | self.spikes.extend(other.produce().map(|δ| -δ)); | |
501 | } | |
502 | } | |
503 | } | |
504 | } | |
505 | ||
506 | make_discrete_addsub_assign!(DiscreteMeasure<Domain, F>); | |
507 | make_discrete_addsub_assign!(&'a DiscreteMeasure<Domain, F>); | |
508 | make_discrete_addsub_assign!(DeltaMeasure<Domain, F>); | |
509 | make_discrete_addsub_assign!(&'a DeltaMeasure<Domain, F>); | |
510 | ||
511 | macro_rules! make_discrete_addsub { | |
512 | ($lhs:ty, $rhs:ty, $alt_order:expr) => { | |
513 | impl<'a, 'b, F : Num, Domain : Clone> Add<$rhs> for $lhs { | |
514 | type Output = DiscreteMeasure<Domain, F>; | |
515 | fn add(self, other : $rhs) -> DiscreteMeasure<Domain, F> { | |
516 | if !$alt_order { | |
517 | self.lift_extend(other.produce()) | |
518 | } else { | |
519 | other.lift_extend(self.produce()) | |
520 | } | |
521 | } | |
522 | } | |
523 | ||
524 | impl<'a, 'b, F : Num + Neg<Output=F>, Domain : Clone> Sub<$rhs> for $lhs { | |
525 | type Output = DiscreteMeasure<Domain, F>; | |
526 | fn sub(self, other : $rhs) -> DiscreteMeasure<Domain, F> { | |
527 | self.lift_extend(other.produce().map(|δ| -δ)) | |
528 | } | |
529 | } | |
530 | }; | |
531 | } | |
532 | ||
533 | make_discrete_addsub!(DiscreteMeasure<Domain, F>, DiscreteMeasure<Domain, F>, false); | |
534 | make_discrete_addsub!(DiscreteMeasure<Domain, F>, &'b DiscreteMeasure<Domain, F>, false); | |
535 | make_discrete_addsub!(&'a DiscreteMeasure<Domain, F>, DiscreteMeasure<Domain, F>, true); | |
536 | make_discrete_addsub!(&'a DiscreteMeasure<Domain, F>, &'b DiscreteMeasure<Domain, F>, false); | |
537 | make_discrete_addsub!(DeltaMeasure<Domain, F>, DiscreteMeasure<Domain, F>, false); | |
538 | make_discrete_addsub!(DeltaMeasure<Domain, F>, &'b DiscreteMeasure<Domain, F>, false); | |
539 | make_discrete_addsub!(&'a DeltaMeasure<Domain, F>, DiscreteMeasure<Domain, F>, true); | |
540 | make_discrete_addsub!(&'a DeltaMeasure<Domain, F>, &'b DiscreteMeasure<Domain, F>, false); | |
541 | make_discrete_addsub!(DiscreteMeasure<Domain, F>, DeltaMeasure<Domain, F>, false); | |
542 | make_discrete_addsub!(DiscreteMeasure<Domain, F>, &'b DeltaMeasure<Domain, F>, false); | |
543 | make_discrete_addsub!(&'a DiscreteMeasure<Domain, F>, DeltaMeasure<Domain, F>, false); | |
544 | make_discrete_addsub!(&'a DiscreteMeasure<Domain, F>, &'b DeltaMeasure<Domain, F>, false); | |
545 | make_discrete_addsub!(DeltaMeasure<Domain, F>, DeltaMeasure<Domain, F>, false); | |
546 | make_discrete_addsub!(DeltaMeasure<Domain, F>, &'b DeltaMeasure<Domain, F>, false); | |
547 | make_discrete_addsub!(&'a DeltaMeasure<Domain, F>, DeltaMeasure<Domain, F>, false); | |
548 | make_discrete_addsub!(&'a DeltaMeasure<Domain, F>, &'b DeltaMeasure<Domain, F>, false); | |
549 | ||
550 | macro_rules! make_discrete_scalarop_rhs { | |
551 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
552 | make_discrete_scalarop_rhs!(@assign DiscreteMeasure<Domain, F>, F, $trait_assign, $fn_assign); | |
553 | make_discrete_scalarop_rhs!(@assign DiscreteMeasure<Domain, F>, &'a F, $trait_assign, $fn_assign); | |
554 | make_discrete_scalarop_rhs!(@new DiscreteMeasure<Domain, F>, F, $trait, $fn, $fn_assign); | |
555 | make_discrete_scalarop_rhs!(@new DiscreteMeasure<Domain, F>, &'a F, $trait, $fn, $fn_assign); | |
556 | make_discrete_scalarop_rhs!(@new &'b DiscreteMeasure<Domain, F>, F, $trait, $fn, $fn_assign); | |
557 | make_discrete_scalarop_rhs!(@new &'b DiscreteMeasure<Domain, F>, &'a F, $trait, $fn, $fn_assign); | |
558 | }; | |
559 | ||
560 | (@assign $lhs:ty, $rhs:ty, $trait_assign:ident, $fn_assign:ident) => { | |
561 | impl<'a, 'b, F : Num, Domain> $trait_assign<$rhs> for $lhs { | |
562 | fn $fn_assign(&mut self, b : $rhs) { | |
563 | self.spikes.iter_mut().for_each(|δ| δ.$fn_assign(b)); | |
564 | } | |
565 | } | |
566 | }; | |
567 | (@new $lhs:ty, $rhs:ty, $trait:ident, $fn:ident, $fn_assign:ident) => { | |
568 | impl<'a, 'b, F : Num, Domain : Clone> $trait<$rhs> for $lhs { | |
569 | type Output = DiscreteMeasure<Domain, F>; | |
570 | fn $fn(self, b : $rhs) -> Self::Output { | |
571 | self.lift_with(|δ| δ.$fn(b), |δ| δ.$fn_assign(b)) | |
572 | } | |
573 | } | |
574 | }; | |
575 | } | |
576 | ||
577 | make_discrete_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
578 | make_discrete_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
579 | ||
580 | macro_rules! make_discrete_unary { | |
581 | ($trait:ident, $fn:ident, $type:ty) => { | |
582 | impl<'a, F : Num + Neg<Output=F>, Domain : Clone> Neg for $type { | |
583 | type Output = DiscreteMeasure<Domain, F>; | |
584 | fn $fn(self) -> Self::Output { | |
585 | self.lift_with(|δ| δ.$fn(), |δ| δ.α = δ.α.$fn()) | |
586 | } | |
587 | } | |
588 | } | |
589 | } | |
590 | ||
591 | make_discrete_unary!(Neg, neg, DiscreteMeasure<Domain, F>); | |
592 | make_discrete_unary!(Neg, neg, &'a DiscreteMeasure<Domain, F>); | |
593 | ||
594 | // impl<F : Num, Domain> Neg for DiscreteMeasure<Domain, F> { | |
595 | // type Output = Self; | |
596 | // fn $fn(mut self, b : F) -> Self { | |
597 | // self.lift().spikes.iter_mut().for_each(|δ| δ.neg(b)); | |
598 | // self | |
599 | // } | |
600 | // } | |
601 | ||
602 | macro_rules! make_discrete_scalarop_lhs { | |
603 | ($trait:ident, $fn:ident; $($f:ident)+) => { $( | |
604 | impl<Domain> $trait<DiscreteMeasure<Domain, $f>> for $f { | |
605 | type Output = DiscreteMeasure<Domain, $f>; | |
606 | fn $fn(self, mut v : DiscreteMeasure<Domain, $f>) -> Self::Output { | |
607 | v.spikes.iter_mut().for_each(|δ| δ.α = self.$fn(δ.α)); | |
608 | v | |
609 | } | |
610 | } | |
611 | ||
612 | impl<'a, Domain : Copy> $trait<&'a DiscreteMeasure<Domain, $f>> for $f { | |
613 | type Output = DiscreteMeasure<Domain, $f>; | |
614 | fn $fn(self, v : &'a DiscreteMeasure<Domain, $f>) -> Self::Output { | |
615 | DiscreteMeasure{ | |
616 | spikes : v.spikes.iter().map(|δ| self.$fn(δ)).collect() | |
617 | } | |
618 | } | |
619 | } | |
620 | ||
621 | impl<'b, Domain> $trait<DiscreteMeasure<Domain, $f>> for &'b $f { | |
622 | type Output = DiscreteMeasure<Domain, $f>; | |
623 | fn $fn(self, mut v : DiscreteMeasure<Domain, $f>) -> Self::Output { | |
624 | v.spikes.iter_mut().for_each(|δ| δ.α = self.$fn(δ.α)); | |
625 | v | |
626 | } | |
627 | } | |
628 | ||
629 | impl<'a, 'b, Domain : Copy> $trait<&'a DiscreteMeasure<Domain, $f>> for &'b $f { | |
630 | type Output = DiscreteMeasure<Domain, $f>; | |
631 | fn $fn(self, v : &'a DiscreteMeasure<Domain, $f>) -> Self::Output { | |
632 | DiscreteMeasure{ | |
633 | spikes : v.spikes.iter().map(|δ| self.$fn(δ)).collect() | |
634 | } | |
635 | } | |
636 | } | |
637 | )+ } | |
638 | } | |
639 | ||
640 | make_discrete_scalarop_lhs!(Mul, mul; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); | |
641 | make_discrete_scalarop_lhs!(Div, div; f32 f64 i8 i16 i32 i64 isize u8 u16 u32 u64 usize); |