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
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Formula implementation.

use crate::math::is_ascending;
use ndarray::Array1;

/// Mathematical formulae accepting a single scalar argument.
#[derive(Debug, Clone)]
pub enum Formula {
    /// Constant value. = c
    Constant {
        /// Constant.
        c: f64,
    },
    /// Line formula. = mx + c
    Line {
        /// Offset.
        c: f64,
        /// Gradient.
        m: f64,
    },
    /// Bifurcation formula. = if x < t { under } else { over }.
    Bifurcation {
        /// Threshold value.
        t: f64,
        /// Under value.
        under: f64,
        /// Over value.
        over: f64,
    },
    /// Constant value spline.
    ConstantSpline {
        /// X change points.
        xs: Array1<f64>,
        /// Y values.
        ys: Array1<f64>,
    },
    /// Linear spline.
    LinearSpline {
        /// X change points.
        xs: Array1<f64>,
        /// Y values.
        ys: Array1<f64>,
        /// Gradient between points.
        grads: Array1<f64>,
    },
    /// Quadratic spline.
    QuadraticSpline {
        /// X change points.
        xs: Array1<f64>,
        /// Y values.
        ys: Array1<f64>,
        /// Gradient between points.
        grads: Array1<f64>,
        /// Second order term between points.
        quads: Array1<f64>,
    },
}

impl Formula {
    /// Construct a constant spline instance.
    #[inline]
    #[must_use]
    pub fn new_constant_spline(xs: Array1<f64>, ys: Array1<f64>) -> Self {
        debug_assert!(xs.len() >= 2);
        debug_assert!(is_ascending(xs.as_slice().unwrap()));
        debug_assert!(ys.len() == xs.len());

        Self::ConstantSpline { xs, ys }
    }

    /// Construct a linear spline instance.
    #[inline]
    #[must_use]
    pub fn new_linear_spline(xs: Array1<f64>, ys: Array1<f64>, grads: Array1<f64>) -> Self {
        debug_assert!(xs.len() >= 2);
        debug_assert!(is_ascending(xs.as_slice().unwrap()));
        debug_assert!(ys.len() == xs.len());
        debug_assert!((grads.len() + 1) == xs.len());

        Self::LinearSpline { xs, ys, grads }
    }

    /// Construct a linear spline instance.
    #[inline]
    #[must_use]
    pub fn new_linear_spline_auto(xs: Array1<f64>, ys: Array1<f64>) -> Self {
        debug_assert!(xs.len() >= 2);
        debug_assert!(is_ascending(xs.as_slice().unwrap()));
        debug_assert!(ys.len() == xs.len());

        let mut grads = Vec::with_capacity(xs.len() - 1);
        for ((x_curr, x_next), (y_curr, y_next)) in xs
            .iter()
            .zip(xs.iter().skip(1))
            .zip(ys.iter().zip(ys.iter().skip(1)))
        {
            grads.push((y_next - y_curr) / (x_next - x_curr));
        }

        Self::new_linear_spline(xs, ys, Array1::from(grads))
    }

    /// Construct a quadratic spline instance.
    #[inline]
    #[must_use]
    pub fn new_quadratic_spline(
        xs: Array1<f64>,
        ys: Array1<f64>,
        grads: Array1<f64>,
        quads: Array1<f64>,
    ) -> Self {
        debug_assert!(xs.len() >= 2);
        debug_assert!(is_ascending(xs.as_slice().unwrap()));
        debug_assert!(ys.len() == xs.len());
        debug_assert!((grads.len() + 1) == xs.len());
        debug_assert!((quads.len() + 1) == xs.len());

        Self::QuadraticSpline {
            xs,
            ys,
            grads,
            quads,
        }
    }

    /// Determine the corresponding output value for the given input.
    #[inline]
    #[must_use]
    pub fn y(&self, x: f64) -> f64 {
        match *self {
            Self::Constant { ref c } => *c,
            Self::Line { c, m } => x.mul_add(m, c),
            Self::Bifurcation {
                ref t,
                ref under,
                ref over,
            } => {
                if x < *t {
                    *under
                } else {
                    *over
                }
            }
            Self::ConstantSpline { ref xs, ref ys } => {
                debug_assert!(x >= xs[0]);
                debug_assert!(x <= xs[xs.len() - 1]);

                for (index, xn) in xs.iter().enumerate() {
                    if *xn > x {
                        return ys[index - 1];
                    }
                }
                ys[ys.len() - 1]
            }
            Self::LinearSpline {
                ref xs,
                ref ys,
                ref grads,
            } => {
                debug_assert!(x >= xs[0]);
                debug_assert!(x <= xs[xs.len() - 1]);

                for (index, xn) in xs.iter().enumerate() {
                    if *xn > x {
                        let dx = x - xs[index - 1];
                        return grads[index - 1].mul_add(dx, ys[index - 1]);
                    }
                }
                ys[ys.len() - 1]
            }
            Self::QuadraticSpline {
                ref xs,
                ref ys,
                ref grads,
                ref quads,
            } => {
                debug_assert!(x >= xs[0]);
                debug_assert!(x <= xs[xs.len() - 1]);

                for (index, xn) in xs.iter().enumerate() {
                    if *xn > x {
                        let dx = x - xs[index - 1];
                        let y = ys[index - 1];
                        let g = grads[index - 1];
                        let q = quads[index - 1];
                        return q.mul_add(dx * dx, g.mul_add(dx, y));
                    }
                }
                ys[ys.len() - 1]
            }
        }
    }
}