Skip to main content

spin_sim/statistics/
stats.rs

1/// Running statistics accumulator for per-temperature observables.
2pub struct Statistics {
3    pub count: usize,
4    pub aggregate: Vec<f64>,
5    pub power: u32,
6}
7
8impl Statistics {
9    pub fn new(n_temps: usize, power: u32) -> Self {
10        Self {
11            count: 0,
12            aggregate: vec![0.0; n_temps],
13            power,
14        }
15    }
16
17    pub fn update(&mut self, values: &[f32]) {
18        self.count += 1;
19        for (agg, &v) in self.aggregate.iter_mut().zip(values.iter()) {
20            let v = v as f64;
21            *agg += if self.power == 1 {
22                v
23            } else {
24                v.powi(self.power as i32)
25            };
26        }
27    }
28
29    pub fn average(&self) -> Vec<f64> {
30        if self.count == 0 {
31            return self.aggregate.clone();
32        }
33        let c = self.count as f64;
34        self.aggregate.iter().map(|&a| a / c).collect()
35    }
36}