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