|
| 1 | +//! Traits and operations related to bounds of a function. |
| 2 | +
|
| 3 | +use std::fmt; |
| 4 | +use std::ops::{self, Bound}; |
| 5 | + |
| 6 | +use crate::Float; |
| 7 | + |
| 8 | +/// Representation of a function's domain. |
| 9 | +#[derive(Clone, Debug)] |
| 10 | +pub struct Domain<T> { |
| 11 | + /// Start of the region for which a function is defined (ignoring poles). |
| 12 | + pub start: Bound<T>, |
| 13 | + /// Endof the region for which a function is defined (ignoring poles). |
| 14 | + pub end: Bound<T>, |
| 15 | + /// Additional points to check closer around. These can be e.g. undefined asymptotes or |
| 16 | + /// inflection points. |
| 17 | + pub check_points: Option<fn() -> BoxIter<T>>, |
| 18 | +} |
| 19 | + |
| 20 | +type BoxIter<T> = Box<dyn Iterator<Item = T>>; |
| 21 | + |
| 22 | +impl<F: Float> Domain<F> { |
| 23 | + /// The start of this domain, saturating at negative infinity. |
| 24 | + pub fn range_start(&self) -> F { |
| 25 | + match self.start { |
| 26 | + Bound::Included(v) | Bound::Excluded(v) => v, |
| 27 | + Bound::Unbounded => F::NEG_INFINITY, |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// The end of this domain, saturating at infinity. |
| 32 | + pub fn range_end(&self) -> F { |
| 33 | + match self.end { |
| 34 | + Bound::Included(v) | Bound::Excluded(v) => v, |
| 35 | + Bound::Unbounded => F::INFINITY, |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl<F: Float> Domain<F> { |
| 41 | + /// x ∈ ℝ |
| 42 | + pub const UNBOUNDED: Self = |
| 43 | + Self { start: Bound::Unbounded, end: Bound::Unbounded, check_points: None }; |
| 44 | + |
| 45 | + /// x ∈ ℝ >= 0 |
| 46 | + pub const POSITIVE: Self = |
| 47 | + Self { start: Bound::Included(F::ZERO), end: Bound::Unbounded, check_points: None }; |
| 48 | + |
| 49 | + /// x ∈ ℝ > 0 |
| 50 | + pub const STRICTLY_POSITIVE: Self = |
| 51 | + Self { start: Bound::Excluded(F::ZERO), end: Bound::Unbounded, check_points: None }; |
| 52 | + |
| 53 | + /// Used for versions of `asin` and `acos`. |
| 54 | + pub const INVERSE_TRIG_PERIODIC: Self = Self { |
| 55 | + start: Bound::Included(F::NEG_ONE), |
| 56 | + end: Bound::Included(F::ONE), |
| 57 | + check_points: None, |
| 58 | + }; |
| 59 | + |
| 60 | + /// Domain for `acosh` |
| 61 | + pub const ACOSH: Self = |
| 62 | + Self { start: Bound::Included(F::ONE), end: Bound::Unbounded, check_points: None }; |
| 63 | + |
| 64 | + /// Domain for `atanh` |
| 65 | + pub const ATANH: Self = Self { |
| 66 | + start: Bound::Excluded(F::NEG_ONE), |
| 67 | + end: Bound::Excluded(F::ONE), |
| 68 | + check_points: None, |
| 69 | + }; |
| 70 | + |
| 71 | + /// Domain for `sin`, `cos`, and `tan` |
| 72 | + pub const TRIG: Self = Self { |
| 73 | + // TODO |
| 74 | + check_points: Some(|| Box::new([-F::PI, -F::FRAC_PI_2, F::FRAC_PI_2, F::PI].into_iter())), |
| 75 | + ..Self::UNBOUNDED |
| 76 | + }; |
| 77 | + |
| 78 | + /// Domain for `log` in various bases |
| 79 | + pub const LOG: Self = Self::STRICTLY_POSITIVE; |
| 80 | + |
| 81 | + /// Domain for `log1p` i.e. `log(1 + x)` |
| 82 | + pub const LOG1P: Self = |
| 83 | + Self { start: Bound::Excluded(F::NEG_ONE), end: Bound::Unbounded, check_points: None }; |
| 84 | + |
| 85 | + /// Domain for `sqrt` |
| 86 | + pub const SQRT: Self = Self::POSITIVE; |
| 87 | + |
| 88 | + /// Domain for `gamma` |
| 89 | + pub const GAMMA: Self = Self { |
| 90 | + check_points: Some(|| { |
| 91 | + // Negative integers are asymptotes |
| 92 | + Box::new((0..u8::MAX).map(|scale| { |
| 93 | + let mut base = F::ZERO; |
| 94 | + for _ in 0..scale { |
| 95 | + base = base - F::ONE; |
| 96 | + } |
| 97 | + base |
| 98 | + })) |
| 99 | + }), |
| 100 | + // Whether or not gamma is defined for negative numbers is implementation dependent |
| 101 | + ..Self::UNBOUNDED |
| 102 | + }; |
| 103 | + |
| 104 | + /// Domain for `loggamma` |
| 105 | + pub const LGAMMA: Self = Self::STRICTLY_POSITIVE; |
| 106 | +} |
| 107 | + |
| 108 | +/// Implement on `op::*` types to indicate how they are bounded. |
| 109 | +pub trait HasDomain<T> |
| 110 | +where |
| 111 | + T: Copy + fmt::Debug + ops::Add<Output = T> + ops::Sub<Output = T> + PartialOrd + 'static, |
| 112 | +{ |
| 113 | + const DOMAIN: Domain<T>; |
| 114 | +} |
| 115 | + |
| 116 | +/// Implement [`HasDomain`] for both the `f32` and `f64` variants of a function. |
| 117 | +macro_rules! impl_has_domain { |
| 118 | + ($($fn_name:ident => $domain:expr;)*) => { |
| 119 | + paste::paste! { |
| 120 | + $( |
| 121 | + // Implement for f64 functions |
| 122 | + impl HasDomain<f64> for $crate::op::$fn_name::Routine { |
| 123 | + const DOMAIN: Domain<f64> = Domain::<f64>::$domain; |
| 124 | + } |
| 125 | + |
| 126 | + // Implement for f32 functions |
| 127 | + impl HasDomain<f32> for $crate::op::[< $fn_name f >]::Routine { |
| 128 | + const DOMAIN: Domain<f32> = Domain::<f32>::$domain; |
| 129 | + } |
| 130 | + )* |
| 131 | + } |
| 132 | + }; |
| 133 | +} |
| 134 | + |
| 135 | +// Tie functions together with their domains. |
| 136 | +impl_has_domain! { |
| 137 | + acos => INVERSE_TRIG_PERIODIC; |
| 138 | + acosh => ACOSH; |
| 139 | + asin => INVERSE_TRIG_PERIODIC; |
| 140 | + asinh => UNBOUNDED; |
| 141 | + atan => UNBOUNDED; |
| 142 | + atanh => ATANH; |
| 143 | + cbrt => UNBOUNDED; |
| 144 | + ceil => UNBOUNDED; |
| 145 | + cos => TRIG; |
| 146 | + cosh => UNBOUNDED; |
| 147 | + erf => UNBOUNDED; |
| 148 | + exp => UNBOUNDED; |
| 149 | + exp10 => UNBOUNDED; |
| 150 | + exp2 => UNBOUNDED; |
| 151 | + expm1 => UNBOUNDED; |
| 152 | + fabs => UNBOUNDED; |
| 153 | + floor => UNBOUNDED; |
| 154 | + frexp => UNBOUNDED; |
| 155 | + ilogb => UNBOUNDED; |
| 156 | + j0 => UNBOUNDED; |
| 157 | + j1 => UNBOUNDED; |
| 158 | + lgamma => LGAMMA; |
| 159 | + log => LOG; |
| 160 | + log10 => LOG; |
| 161 | + log1p => LOG1P; |
| 162 | + log2 => LOG; |
| 163 | + modf => UNBOUNDED; |
| 164 | + rint => UNBOUNDED; |
| 165 | + round => UNBOUNDED; |
| 166 | + sin => TRIG; |
| 167 | + sincos => TRIG; |
| 168 | + sinh => UNBOUNDED; |
| 169 | + sqrt => SQRT; |
| 170 | + tan => TRIG; |
| 171 | + tanh => UNBOUNDED; |
| 172 | + tgamma => GAMMA; |
| 173 | + trunc => UNBOUNDED; |
| 174 | +} |
| 175 | + |
| 176 | +/* Manual implementations, these functions don't follow `foo`->`foof` naming */ |
| 177 | + |
| 178 | +impl HasDomain<f32> for crate::op::lgammaf_r::Routine { |
| 179 | + const DOMAIN: Domain<f32> = Domain::<f32>::LGAMMA; |
| 180 | +} |
| 181 | + |
| 182 | +impl HasDomain<f64> for crate::op::lgamma_r::Routine { |
| 183 | + const DOMAIN: Domain<f64> = Domain::<f64>::LGAMMA; |
| 184 | +} |
0 commit comments