|
| 1 | +//! Interpolation strategies. |
| 2 | +use noisy_float::types::N64; |
| 3 | +use num_traits::{Float, FromPrimitive, NumOps, ToPrimitive}; |
| 4 | + |
| 5 | +fn float_quantile_index(q: N64, len: usize) -> N64 { |
| 6 | + q * ((len - 1) as f64) |
| 7 | +} |
| 8 | + |
| 9 | +/// Returns the fraction that the quantile is between the lower and higher indices. |
| 10 | +/// |
| 11 | +/// This ranges from 0, where the quantile exactly corresponds the lower index, |
| 12 | +/// to 1, where the quantile exactly corresponds to the higher index. |
| 13 | +fn float_quantile_index_fraction(q: N64, len: usize) -> N64 { |
| 14 | + float_quantile_index(q, len).fract() |
| 15 | +} |
| 16 | + |
| 17 | +/// Returns the index of the value on the lower side of the quantile. |
| 18 | +pub(crate) fn lower_index(q: N64, len: usize) -> usize { |
| 19 | + float_quantile_index(q, len).floor().to_usize().unwrap() |
| 20 | +} |
| 21 | + |
| 22 | +/// Returns the index of the value on the higher side of the quantile. |
| 23 | +pub(crate) fn higher_index(q: N64, len: usize) -> usize { |
| 24 | + float_quantile_index(q, len).ceil().to_usize().unwrap() |
| 25 | +} |
| 26 | + |
| 27 | +/// Used to provide an interpolation strategy to [`quantile_axis_mut`]. |
| 28 | +/// |
| 29 | +/// [`quantile_axis_mut`]: ../trait.QuantileExt.html#tymethod.quantile_axis_mut |
| 30 | +pub trait Interpolate<T> { |
| 31 | + /// Returns `true` iff the lower value is needed to compute the |
| 32 | + /// interpolated value. |
| 33 | + #[doc(hidden)] |
| 34 | + fn needs_lower(q: N64, len: usize) -> bool; |
| 35 | + |
| 36 | + /// Returns `true` iff the higher value is needed to compute the |
| 37 | + /// interpolated value. |
| 38 | + #[doc(hidden)] |
| 39 | + fn needs_higher(q: N64, len: usize) -> bool; |
| 40 | + |
| 41 | + /// Computes the interpolated value. |
| 42 | + /// |
| 43 | + /// **Panics** if `None` is provided for the lower value when it's needed |
| 44 | + /// or if `None` is provided for the higher value when it's needed. |
| 45 | + #[doc(hidden)] |
| 46 | + fn interpolate(lower: Option<T>, higher: Option<T>, q: N64, len: usize) -> T; |
| 47 | +} |
| 48 | + |
| 49 | +/// Select the higher value. |
| 50 | +pub struct Higher; |
| 51 | +/// Select the lower value. |
| 52 | +pub struct Lower; |
| 53 | +/// Select the nearest value. |
| 54 | +pub struct Nearest; |
| 55 | +/// Select the midpoint of the two values (`(lower + higher) / 2`). |
| 56 | +pub struct Midpoint; |
| 57 | +/// Linearly interpolate between the two values |
| 58 | +/// (`lower + (higher - lower) * fraction`, where `fraction` is the |
| 59 | +/// fractional part of the index surrounded by `lower` and `higher`). |
| 60 | +pub struct Linear; |
| 61 | + |
| 62 | +impl<T> Interpolate<T> for Higher { |
| 63 | + fn needs_lower(_q: N64, _len: usize) -> bool { |
| 64 | + false |
| 65 | + } |
| 66 | + fn needs_higher(_q: N64, _len: usize) -> bool { |
| 67 | + true |
| 68 | + } |
| 69 | + fn interpolate(_lower: Option<T>, higher: Option<T>, _q: N64, _len: usize) -> T { |
| 70 | + higher.unwrap() |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl<T> Interpolate<T> for Lower { |
| 75 | + fn needs_lower(_q: N64, _len: usize) -> bool { |
| 76 | + true |
| 77 | + } |
| 78 | + fn needs_higher(_q: N64, _len: usize) -> bool { |
| 79 | + false |
| 80 | + } |
| 81 | + fn interpolate(lower: Option<T>, _higher: Option<T>, _q: N64, _len: usize) -> T { |
| 82 | + lower.unwrap() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl<T> Interpolate<T> for Nearest { |
| 87 | + fn needs_lower(q: N64, len: usize) -> bool { |
| 88 | + float_quantile_index_fraction(q, len) < 0.5 |
| 89 | + } |
| 90 | + fn needs_higher(q: N64, len: usize) -> bool { |
| 91 | + !<Self as Interpolate<T>>::needs_lower(q, len) |
| 92 | + } |
| 93 | + fn interpolate(lower: Option<T>, higher: Option<T>, q: N64, len: usize) -> T { |
| 94 | + if <Self as Interpolate<T>>::needs_lower(q, len) { |
| 95 | + lower.unwrap() |
| 96 | + } else { |
| 97 | + higher.unwrap() |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<T> Interpolate<T> for Midpoint |
| 103 | +where |
| 104 | + T: NumOps + Clone + FromPrimitive, |
| 105 | +{ |
| 106 | + fn needs_lower(_q: N64, _len: usize) -> bool { |
| 107 | + true |
| 108 | + } |
| 109 | + fn needs_higher(_q: N64, _len: usize) -> bool { |
| 110 | + true |
| 111 | + } |
| 112 | + fn interpolate(lower: Option<T>, higher: Option<T>, _q: N64, _len: usize) -> T { |
| 113 | + let denom = T::from_u8(2).unwrap(); |
| 114 | + let lower = lower.unwrap(); |
| 115 | + let higher = higher.unwrap(); |
| 116 | + lower.clone() + (higher.clone() - lower.clone()) / denom.clone() |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl<T> Interpolate<T> for Linear |
| 121 | +where |
| 122 | + T: NumOps + Clone + FromPrimitive + ToPrimitive, |
| 123 | +{ |
| 124 | + fn needs_lower(_q: N64, _len: usize) -> bool { |
| 125 | + true |
| 126 | + } |
| 127 | + fn needs_higher(_q: N64, _len: usize) -> bool { |
| 128 | + true |
| 129 | + } |
| 130 | + fn interpolate(lower: Option<T>, higher: Option<T>, q: N64, len: usize) -> T { |
| 131 | + let fraction = float_quantile_index_fraction(q, len).to_f64().unwrap(); |
| 132 | + let lower = lower.unwrap(); |
| 133 | + let higher = higher.unwrap(); |
| 134 | + let lower_f64 = lower.to_f64().unwrap(); |
| 135 | + let higher_f64 = higher.to_f64().unwrap(); |
| 136 | + lower.clone() + T::from_f64(fraction * (higher_f64 - lower_f64)).unwrap() |
| 137 | + } |
| 138 | +} |
0 commit comments