src/measures/discrete.rs

branch
dev
changeset 32
56c8adc32b09
parent 0
eb3c7813b67a
child 34
efa60bc4f743
equal deleted inserted replaced
30:bd13c2ae3450 32:56c8adc32b09
57 #[inline] 57 #[inline]
58 pub fn len(&self) -> usize { 58 pub fn len(&self) -> usize {
59 self.spikes.len() 59 self.spikes.len()
60 } 60 }
61 61
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
62 /// Iterate over (references to) the [`DeltaMeasure`] spikes in this measure 76 /// Iterate over (references to) the [`DeltaMeasure`] spikes in this measure
63 #[inline] 77 #[inline]
64 pub fn iter_spikes(&self) -> SpikeIter<'_, Domain, F> { 78 pub fn iter_spikes(&self) -> SpikeIter<'_, Domain, F> {
65 self.spikes.iter() 79 self.spikes.iter()
66 } 80 }
107 /// Prune all spikes with zero mass. 121 /// Prune all spikes with zero mass.
108 #[inline] 122 #[inline]
109 pub fn prune(&mut self) { 123 pub fn prune(&mut self) {
110 self.spikes.retain(|δ| δ.α != F::ZERO); 124 self.spikes.retain(|δ| δ.α != F::ZERO);
111 } 125 }
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 }
112 } 151 }
113 152
114 impl<Domain : Clone, F : Float> DiscreteMeasure<Domain, F> { 153 impl<Domain : Clone, F : Float> DiscreteMeasure<Domain, F> {
115 /// Computes `μ1 ← θ * μ1 - ζ * μ2`, pruning entries where both `μ1` (`self`) and `μ2` have 154 /// 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. 155 // zero weight. `μ2` will contain copy of pruned original `μ1` without arithmetic performed.
117 /// **This expects `self` and `μ2` to have matching coordinates in each index**. 156 /// **This expects `self` and `μ2` to have matching coordinates in each index**.
118 // `μ2` can be than `self`, but not longer. 157 // `μ2` can be than `self`, but not longer.
119 pub fn pruning_sub(&mut self, θ : F, ζ : F, μ2 : &mut Self) { 158 pub fn pruning_sub(&mut self, θ : F, ζ : F, μ2 : &mut Self) {
120 let mut μ2_get = 0; 159 let mut μ2_get = 0;
121 let mut μ2_insert = 0; 160 let mut μ2_insert = 0;
122 self.spikes.drain_filter(|&mut DeltaMeasure{ α : ref mut α_ref, ref x }| { 161 self.spikes.retain_mut(|&mut DeltaMeasure{ α : ref mut α_ref, ref x }| {
123 // Get weight of spike in μ2, zero if out of bounds. 162 // Get weight of spike in μ2, zero if out of bounds.
124 let β = μ2.spikes.get(μ2_get).map_or(F::ZERO, DeltaMeasure::get_mass); 163 let β = μ2.spikes.get(μ2_get).map_or(F::ZERO, DeltaMeasure::get_mass);
125 μ2_get += 1; 164 μ2_get += 1;
126 165
127 if *α_ref == F::ZERO && β == F::ZERO { 166 if *α_ref == F::ZERO && β == F::ZERO {
174 pub fn set_masses_dvector(&mut self, x : &DVector<F::MixedType>) { 213 pub fn set_masses_dvector(&mut self, x : &DVector<F::MixedType>) {
175 self.set_masses(x.iter().map(|&α| F::from_nalgebra_mixed(α))); 214 self.set_masses(x.iter().map(|&α| F::from_nalgebra_mixed(α)));
176 } 215 }
177 } 216 }
178 217
179 impl<Domain, F :Num> Index<usize> for DiscreteMeasure<Domain, F> { 218 // impl<Domain, F :Num> Index<usize> for DiscreteMeasure<Domain, F> {
180 type Output = DeltaMeasure<Domain, F>; 219 // type Output = DeltaMeasure<Domain, F>;
181 #[inline] 220 // #[inline]
182 fn index(&self, i : usize) -> &Self::Output { 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;
240 #[inline]
241 fn index(&self, i : I) -> &Self::Output {
183 self.spikes.index(i) 242 self.spikes.index(i)
184 } 243 }
185 } 244 }
186 245
187 impl<Domain, F :Num> IndexMut<usize> for DiscreteMeasure<Domain, F> { 246 impl<
188 #[inline] 247 Domain,
189 fn index_mut(&mut self, i : usize) -> &mut Self::Output { 248 F : Num,
249 I : std::slice::SliceIndex<[DeltaMeasure<Domain, F>]>
250 > IndexMut<I>
251 for DiscreteMeasure<Domain, F> {
252 #[inline]
253 fn index_mut(&mut self, i : I) -> &mut Self::Output {
190 self.spikes.index_mut(i) 254 self.spikes.index_mut(i)
191 } 255 }
192 } 256 }
257
193 258
194 impl<Domain, F : Num, D : Into<DeltaMeasure<Domain, F>>, const K : usize> From<[D; K]> 259 impl<Domain, F : Num, D : Into<DeltaMeasure<Domain, F>>, const K : usize> From<[D; K]>
195 for DiscreteMeasure<Domain, F> { 260 for DiscreteMeasure<Domain, F> {
196 #[inline] 261 #[inline]
197 fn from(list : [D; K]) -> Self { 262 fn from(list : [D; K]) -> Self {

mercurial