ndhistogram/value/sum.rs
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 55 56 57 58 59 60 61 62 63 64 65
use num_traits::Float;
use crate::Fill;
use crate::FillWith;
/// 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)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::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>(&self) -> O
where
T: Into<O>,
O: Float,
{
self.variance().into().sqrt()
}
}
impl<T: Copy + Fill> Fill for Sum<T> {
#[inline]
fn fill(&mut self) {
self.sum.fill();
}
}
impl<T, W> FillWith<W> for Sum<T>
where
T: FillWith<W> + Copy,
W: Copy,
{
#[inline]
fn fill_with(&mut self, weight: W) {
self.sum.fill_with(weight);
}
}