use num_traits::Float;
use crate::Fill;
use crate::FillWith;
#[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> {
pub fn new() -> Self
where
Self: Default,
{
Self::default()
}
pub fn get(&self) -> T {
self.sum()
}
pub fn sum(&self) -> T {
self.sum
}
pub fn variance(&self) -> T {
self.sum
}
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);
}
}