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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use float_cmp::{ApproxEq, F64Margin};
use std::fmt;
use std::ops::{Add, Div, Mul, Sub, Neg};

/**A Measurement 'x' is written as x = (mean +- sigma)

where 'mean' is the mean value

and 'sigma' is the uncertainty(also called error or standard deviation from the mean)*/
#[derive(Debug, Clone, Copy)]
pub struct Measurement {
    mean: f64,  //mean value
    sigma: f64, //std deviation, error or uncertainty
}

impl Measurement {
    pub fn new(mean: f64, sigma: f64) -> Measurement {
        Measurement { mean, sigma }
    }
}

impl Neg for Measurement {
    type Output = Self;

    fn neg(self) -> Self {
        Self {
            mean: -self.mean,
            sigma: self.sigma,
        }
    }
}

impl Add for Measurement {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Self {
            mean: self.mean + other.mean,
            sigma: quadrature(self.sigma, other.sigma).sqrt(),
        }
    }
}

impl Add<f64> for Measurement {
    type Output = Self;

    fn add(self, other: f64) -> Self {
        Self {
            mean: self.mean + other,
            sigma: self.sigma,
        }
    }
}

impl Sub for Measurement {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        Self {
            mean: self.mean - other.mean,
            sigma: quadrature(self.sigma, other.sigma).sqrt(),
        }
    }
}

impl Sub<f64> for Measurement {
    type Output = Self;

    fn sub(self, other: f64) -> Self {
        Self {
            mean: self.mean - other,
            sigma: self.sigma,
        }
    }
}

impl Mul for Measurement {
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        let new_mean = self.mean * other.mean;
        Self {
            mean: new_mean,
            sigma: quadrature(self.sigma / self.mean, other.sigma / other.mean).sqrt() * new_mean,
        }
    }
}

impl Mul<f64> for Measurement {
    type Output = Self;

    fn mul(self, other: f64) -> Self {
        Self {
            mean: self.mean * other,
            sigma: self.sigma * other,
        }
    }
}

impl Div for Measurement {
    type Output = Self;

    fn div(self, other: Self) -> Self {
        let new_mean = self.mean / other.mean;
        Self {
            mean: new_mean,
            sigma: quadrature(self.sigma / self.mean, other.sigma / other.mean).sqrt() * new_mean,
        }
    }
}

impl Div<f64> for Measurement {
    type Output = Self;

    fn div(self, other: f64) -> Self {
        Self {
            mean: self.mean / other,
            sigma: self.sigma / other,
        }
    }
}

impl fmt::Display for Measurement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ± {}", self.mean, self.sigma)
    }
}

impl PartialEq for Measurement {
    fn eq(&self, other: &Self) -> bool {
        self.mean == other.mean && self.sigma == other.sigma
    }
}

impl ApproxEq for Measurement {
    type Margin = F64Margin;

    fn approx_eq<T: Into<Self::Margin>>(self, other: Self, margin: T) -> bool {
        let margin = margin.into();
        self.mean.approx_eq(other.mean, margin) && self.sigma.approx_eq(other.sigma, margin)
    }
}

fn quadrature(x: f64, y: f64) -> f64 {
    x * x + y * y
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn simple_operations() {
        let x = Measurement::new(1.0, 0.01);
        let y = Measurement::new(2.0, 0.01);

        let added = Measurement::new(3.0, 0.0002f64.sqrt());
        let subtracted = Measurement::new(-1.0, 0.0002f64.sqrt());
        let multiplied = Measurement::new(2.0, 2.0 * (quadrature(0.01 / 1.0, 0.01 / 2.0)).sqrt());
        let divided = Measurement::new(0.5, 0.5 * (quadrature(0.01 / 1.0, 0.01 / 2.0)).sqrt());

        assert!(added.approx_eq(x + y, F64Margin::default()));
        assert!(subtracted.approx_eq(x - y, F64Margin::default()));
        assert!(multiplied.approx_eq(x * y, F64Margin::default()));
        assert!(divided.approx_eq(x / y, F64Margin::default()));
    }
    #[test]
    fn approximate_equality() {
        /* Tests the approximate equality due to floating point errors */
        let x = Measurement::new(1.0, 0.01);
        let y = Measurement::new(1.0, 0.001);
        let x_prime = Measurement::new(1.0, 2.0 * 0.005);

        //A number is equal to itself. Therefore it's also approximately equal to itself
        assert!(x.approx_eq(x, F64Margin::default()));
        //x and y should NOT be approximately equal
        assert_eq!(false, x.approx_eq(y, F64Margin::default()));
        //x and x_prime should be equal
        assert!(x.approx_eq(x_prime, F64Margin::default()));
    }
}