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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use num_traits::Float;

use crate::Fill;
use serde::{Deserialize, Serialize};

/// ndhistogram bin value type for filling unweighted values.
/// Analogous to [WeightedSum](crate::value::WeightedSum). Methods returning variance and standard
/// deviation assume Poisson statistics.
#[derive(
    Copy, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize, Deserialize,
)]
pub struct Sum<T = f64> {
    sum: T,
}

impl<T: Copy> Sum<T> {
    /// Factory method to create an unfilled (or zero valued) Sum.
    pub fn new() -> Self
    where
        Self: Default,
    {
        Self::default()
    }

    /// Get the current value of the sum.
    pub fn get(&self) -> T {
        self.sum()
    }

    /// Get the current value.
    pub fn sum(&self) -> T {
        self.sum
    }

    /// Estimate of the variance of value assuming Poisson statistics.
    pub fn variance(&self) -> T {
        self.sum
    }

    /// Square root of the variance.
    pub fn standard_deviation<O: Float>(&self) -> O
    where
        T: Into<O>,
        O: Float,
    {
        self.variance().into().sqrt()
    }
}

impl<T: Copy + Fill> Fill for Sum<T> {
    fn fill(&mut self) {
        self.sum.fill();
    }
}