From 3fd3bc4ac998fca433cce532759f752d98e0588e Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Thu, 11 Jan 2018 19:00:59 +0100 Subject: [PATCH] Moments: Refactor add to be in-place --- src/moments/mod.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/moments/mod.rs b/src/moments/mod.rs index c962574..46e2bd4 100644 --- a/src/moments/mod.rs +++ b/src/moments/mod.rs @@ -114,11 +114,10 @@ impl Moments { #[inline] pub fn add(&mut self, x: f64) { - let mut result = Moments::new(); - result.n = self.n + 1; + self.n += 1; let delta = x - self.avg; - let n = f64::approx_from(result.n).unwrap(); - result.avg = self.avg + delta / n; + let n = f64::approx_from(self.n).unwrap(); + self.avg += delta / n; let mut coeff_delta = delta; let over_n = 1. / n; @@ -129,20 +128,22 @@ impl Moments { let factor_coeff = -delta * over_n; + let prev_m = self.m; for p in 2..=MAX_P { term1 *= factor1; term2 *= factor2; coeff_delta *= delta; - result.m[p - 2] = self.m[p - 2] + (term1 + term2) * coeff_delta; + self.m[p - 2] += (term1 + term2) * coeff_delta; let mut coeff = 1.; for k in 1..=(p - 2) { coeff *= factor_coeff; - result.m[p - 2] += f64::approx_from(binomial(p, k)).unwrap() * - self.m[p - 2 - k] * coeff; + println!("writing m[{}], accessing m[{}]", p - 2, p - 2 - k); + self.m[p - 2] += f64::approx_from(binomial(p, k)).unwrap() * + prev_m[p - 2 - k] * coeff; } } - *self = result; + println!(); } #[inline]