cert/
uncertainty.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//! Defines the [`Uncertainty`] trait.
//!
//! The uncertainty trait defines common behaviours for quantities with an associated uncertainty.

use super::{AbsUncertainty, RelUncertainty};
use num_traits::{Float, One, Zero};
use std::ops;

/// Used to define behaviour for values which have associated uncertainty.
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
{
    /// The underlying float type for the uncertainty
    type Float: Float + Zero;

    fn new(value: Self::Float, uncertainty: Self::Float) -> Self;

    /// Returns the mean of the value
    fn mean(&self) -> Self::Float;
    /// The standard deviation of the value
    fn standard_deviation(&self) -> Self::Float;
    /// The coefficient of variation is the ratio of the standard deviation and the mean.
    ///
    /// It describes the relative error.
    fn coefficient_of_variation(&self) -> Self::Float;

    /// The actual uncertainty of the concrete type. This will depend on whether the implementor
    /// represent a relative or absolute uncertainty.
    fn uncertainty(&self) -> Self::Float;

    /// Returns true if the uncertainty is zero.
    ///
    /// This method is useful when the type may be used in a method which divides by the
    /// uncertainties.
    fn is_certain(&self) -> bool {
        self.uncertainty() == Self::Float::zero()
    }

    /// Raise the value to the nth power.
    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)
    }
}