1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use num::Float;
use std::ops::Deref;
#[inline]
pub fn mean<R, N>(data: &R) -> N where R: Deref<Target=[N]>, N: Float {
data.iter().fold(N::zero(), |a, b| a+*b) / N::from(data.as_ref().len()).unwrap()
}
pub fn std_dev<R, N>(data: &R) -> N where R: Deref<Target=[N]>, N: Float {
let mean = mean(data);
let sum = data.as_ref()
.iter()
.map(|e| (*e- N::from(mean).unwrap()).powi(2))
.fold(N::zero(), |a, b| a+b);
(sum/ N::from(data.len()).unwrap()).sqrt()
}
pub fn znorm<R, N>(data: &R) -> Vec<N> where R: Deref<Target=[N]>, N: Float {
let mean = mean(data);
let std_dev = std_dev(data);
data.iter().map(|e| (*e-mean) / std_dev).collect()
}
pub fn gaussian<N>(q: &[N], c: &[N]) -> N where N: Float {
let sum = q
.iter()
.zip(c)
.map(|(qi, ci)| (*qi - *ci).powi(2))
.fold(N::zero(), |acc, x| acc + x);
sum.sqrt()
}