use super::{AbsUncertainty, RelUncertainty};
use num_traits::{Float, One, Zero};
use std::ops;
pub trait Uncertainty:
Sized
+ Copy
+ Into<AbsUncertainty<Self::Float>>
+ Into<RelUncertainty<Self::Float>>
+ ops::Add<Self, Output = Self>
+ ops::Div<Self, Output = Self>
+ ops::Mul<Self, Output = Self>
+ ops::Sub<Self, Output = Self>
+ Zero
{
type Float: Float + Zero;
fn new(value: Self::Float, uncertainty: Self::Float) -> Self;
fn mean(&self) -> Self::Float;
fn standard_deviation(&self) -> Self::Float;
fn coefficient_of_variation(&self) -> Self::Float;
fn uncertainty(&self) -> Self::Float;
fn is_certain(&self) -> bool {
self.uncertainty() == Self::Float::zero()
}
fn powi(&self, n: i32) -> Self {
Self::new(
self.mean().powi(n),
((Self::Float::one() + Self::Float::one()).powi(n)
* self.mean().powi(2 * n - 2)
* self.uncertainty().powi(2))
.sqrt(),
)
}
}
impl Uncertainty for f64 {
type Float = f64;
fn new(value: f64, _uncertainty: f64) -> f64 {
value
}
fn mean(&self) -> Self::Float {
*self
}
fn standard_deviation(&self) -> Self::Float {
0.0
}
fn coefficient_of_variation(&self) -> Self::Float {
0.0
}
fn uncertainty(&self) -> Self::Float {
0.0
}
fn is_certain(&self) -> bool {
true
}
fn powi(&self, n: i32) -> Self {
<f64 as Float>::powi(*self, n)
}
}
impl From<f64> for AbsUncertainty<f64> {
fn from(val: f64) -> AbsUncertainty<f64> {
AbsUncertainty::new(val, 0.0)
}
}
impl From<f64> for RelUncertainty<f64> {
fn from(val: f64) -> RelUncertainty<f64> {
RelUncertainty::new(val, 0.0)
}
}
impl Uncertainty for f32 {
type Float = f32;
fn new(value: f32, _uncertainty: f32) -> f32 {
value
}
fn mean(&self) -> Self::Float {
*self
}
fn standard_deviation(&self) -> Self::Float {
0.0
}
fn coefficient_of_variation(&self) -> Self::Float {
0.0
}
fn uncertainty(&self) -> Self::Float {
0.0
}
fn is_certain(&self) -> bool {
true
}
fn powi(&self, n: i32) -> Self {
<f32 as Float>::powi(*self, n)
}
}
impl From<f32> for AbsUncertainty<f32> {
fn from(val: f32) -> AbsUncertainty<f32> {
AbsUncertainty::new(val, 0.0)
}
}
impl From<f32> for RelUncertainty<f32> {
fn from(val: f32) -> RelUncertainty<f32> {
RelUncertainty::new(val, 0.0)
}
}
impl From<AbsUncertainty<f32>> for f32 {
fn from(val: AbsUncertainty<f32>) -> f32 {
val.mean()
}
}
impl From<AbsUncertainty<f64>> for f64 {
fn from(val: AbsUncertainty<f64>) -> f64 {
val.mean()
}
}