Moments: Refactor add to be in-place

This commit is contained in:
Vinzent Steinberg 2018-01-11 19:00:59 +01:00
parent 608a9733cd
commit 3fd3bc4ac9

View File

@ -114,11 +114,10 @@ impl Moments {
#[inline] #[inline]
pub fn add(&mut self, x: f64) { pub fn add(&mut self, x: f64) {
let mut result = Moments::new(); self.n += 1;
result.n = self.n + 1;
let delta = x - self.avg; let delta = x - self.avg;
let n = f64::approx_from(result.n).unwrap(); let n = f64::approx_from(self.n).unwrap();
result.avg = self.avg + delta / n; self.avg += delta / n;
let mut coeff_delta = delta; let mut coeff_delta = delta;
let over_n = 1. / n; let over_n = 1. / n;
@ -129,20 +128,22 @@ impl Moments {
let factor_coeff = -delta * over_n; let factor_coeff = -delta * over_n;
let prev_m = self.m;
for p in 2..=MAX_P { for p in 2..=MAX_P {
term1 *= factor1; term1 *= factor1;
term2 *= factor2; term2 *= factor2;
coeff_delta *= delta; 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.; let mut coeff = 1.;
for k in 1..=(p - 2) { for k in 1..=(p - 2) {
coeff *= factor_coeff; coeff *= factor_coeff;
result.m[p - 2] += f64::approx_from(binomial(p, k)).unwrap() * println!("writing m[{}], accessing m[{}]", p - 2, p - 2 - k);
self.m[p - 2 - k] * coeff; self.m[p - 2] += f64::approx_from(binomial(p, k)).unwrap() *
prev_m[p - 2 - k] * coeff;
} }
} }
*self = result; println!();
} }
#[inline] #[inline]