src/measures/discrete.rs

branch
dev
changeset 39
6316d68b58af
parent 37
c5d8bd1a7728
--- a/src/measures/discrete.rs	Thu Jan 23 23:35:28 2025 +0100
+++ b/src/measures/discrete.rs	Thu Jan 23 23:34:05 2025 +0100
@@ -265,44 +265,29 @@
 
 impl<Domain : Clone, F : Float> DiscreteMeasure<Domain, F> {
     /// Computes `μ1 ← θ * μ1 - ζ * μ2`, pruning entries where both `μ1` (`self`) and `μ2` have
-    // zero weight. `μ2` will contain copy of pruned original `μ1` without arithmetic performed.
-    /// **This expects `self` and `μ2` to have matching coordinates in each index**.
+    // zero weight. `μ2` will contain a pruned copy of pruned original `μ1` without arithmetic
+    /// performed. **This expects `self` and `μ2` to have matching coordinates in each index**.
     // `μ2` can be than `self`, but not longer.
     pub fn pruning_sub(&mut self, θ : F, ζ : F, μ2 : &mut Self) {
-        let mut μ2_get = 0;
-        let mut μ2_insert = 0;
-        self.spikes.retain_mut(|&mut DeltaMeasure{ α : ref mut α_ref, ref x }| {
-            // Get weight of spike in μ2, zero if out of bounds.
-            let β = μ2.spikes.get(μ2_get).map_or(F::ZERO, DeltaMeasure::get_mass);
-            μ2_get += 1;
-
-            if *α_ref == F::ZERO && β == F::ZERO {
-                // Prune
-                true
+        for δ in &self[μ2.len()..] {
+            μ2.push(DeltaMeasure{ x : δ.x.clone(), α : F::ZERO});
+        }
+        debug_assert_eq!(self.len(), μ2.len());
+        let mut dest = 0;
+        for i in 0..self.len() {
+            let α = self[i].α;
+            let α_new = θ * α - ζ * μ2[i].α;
+            if dest < i {
+                μ2[dest] = DeltaMeasure{ x : self[i].x.clone(), α };
+                self[dest] = DeltaMeasure{ x : self[i].x.clone(), α : α_new };
             } else {
-                // Save self weight
-                let α = *α_ref;
-                // Modify self
-                *α_ref = θ * α - ζ * β;
-                // Make copy of old self weight in μ2
-                let δ = DeltaMeasure{ α, x : x.clone() };
-                match μ2.spikes.get_mut(μ2_insert) {
-                    Some(replace) => {
-                        *replace = δ;
-                    },
-                    None => {
-                        debug_assert_eq!(μ2.len(), μ2_insert);
-                        μ2.spikes.push(δ);
-                    },
-                }
-                μ2_insert += 1;
-                // Keep
-                false
+                μ2[i].α = α;
+                self[i].α = α_new;
             }
-        });
-        // Truncate μ2 to same length as self.
-        μ2.spikes.truncate(μ2_insert);
-        debug_assert_eq!(μ2.len(), self.len());
+            dest += 1;
+        }
+        self.spikes.truncate(dest);
+        μ2.spikes.truncate(dest);
     }
 }
 
@@ -327,17 +312,17 @@
         self.set_masses(x.iter().map(|&α| F::from_nalgebra_mixed(α)));
     }
 
-    /// Extracts the masses of the spikes as a [`Vec`].
-    pub fn masses_vec(&self) -> Vec<F::MixedType> {
-        self.iter_masses()
-            .map(|α| α.to_nalgebra_mixed())
-            .collect()
-    }
+    // /// Extracts the masses of the spikes as a [`Vec`].
+    // pub fn masses_vec(&self) -> Vec<F::MixedType> {
+    //     self.iter_masses()
+    //         .map(|α| α.to_nalgebra_mixed())
+    //         .collect()
+    // }
 
-    /// Sets the masses of the spikes from the values of a [`Vec`].
-    pub fn set_masses_vec(&mut self, x : &Vec<F::MixedType>) {
-        self.set_masses(x.iter().map(|&α| F::from_nalgebra_mixed(α)));
-    }
+    // /// Sets the masses of the spikes from the values of a [`Vec`].
+    // pub fn set_masses_vec(&mut self, x : &Vec<F::MixedType>) {
+    //     self.set_masses(x.iter().map(|&α| F::from_nalgebra_mixed(α)));
+    // }
 }
 
 // impl<Domain, F :Num> Index<usize> for DiscreteMeasure<Domain, F> {

mercurial