statistics: covariance(): avoid allocation

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-06-09 13:57:24 +02:00
parent 239e49f927
commit cdde66d277
1 changed files with 2 additions and 2 deletions

View File

@ -70,11 +70,11 @@ where
let mean_x = mean(x)?; let mean_x = mean(x)?;
let mean_y = mean(y)?; let mean_y = mean(y)?;
let covariance = sum(&(0..len_x).map(|i| { let covariance: f64 = (0..len_x).map(|i| {
let x = x[i].to_f64().unwrap_or(0.0); let x = x[i].to_f64().unwrap_or(0.0);
let y = y[i].to_f64().unwrap_or(0.0); let y = y[i].to_f64().unwrap_or(0.0);
(x - mean_x)*(y - mean_y) (x - mean_x)*(y - mean_y)
}).collect::<Vec<f64>>()); }).sum();
Some(covariance/(len_x as f64)) Some(covariance/(len_x as f64))
} }