|
| 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 | +/// A trait to be implemented on types representing a function's domain. |
| 9 | +/// |
| 10 | +/// Since multiple functions share the same domain, this doesn't get implemented on the `op::*` |
| 11 | +/// type. Instead, this gets applied to a new unit struct, then the `op::*` type should |
| 12 | +/// implement `HasDomain`. |
| 13 | +pub trait Domain<T> |
| 14 | +where |
| 15 | + T: Copy + fmt::Debug + ops::Add<Output = T> + ops::Sub<Output = T> + PartialOrd + 'static, |
| 16 | +{ |
| 17 | + /// The region for which the function is defined. Ignores poles. |
| 18 | + const DEFINED: (Bound<T>, Bound<T>); |
| 19 | + |
| 20 | + /// The region, if any, for which the function repeats. Used to test within. |
| 21 | + const PERIODIC: Option<(Bound<T>, Bound<T>)> = None; |
| 22 | + |
| 23 | + /// Asymptotes that have a defined value in the float function (but probably not the |
| 24 | + /// mathematical function). Returns an `(input, ouptut)` mapping. |
| 25 | + fn defined_asymptotes() -> impl Iterator<Item = (T, T)> { |
| 26 | + std::iter::empty() |
| 27 | + } |
| 28 | + |
| 29 | + /// Additional points to check closer around. These can be e.g. undefined asymptotes or |
| 30 | + /// inflection points. |
| 31 | + fn check_points() -> impl Iterator<Item = T> { |
| 32 | + std::iter::empty() |
| 33 | + } |
| 34 | + |
| 35 | + /// How NaNs at certain values should be handled. |
| 36 | + fn nan_handling(input: T) -> T { |
| 37 | + input |
| 38 | + } |
| 39 | + |
| 40 | + /// Helper to get the length of the period in unit `T`. |
| 41 | + fn period() -> Option<T> { |
| 42 | + Self::PERIODIC?; |
| 43 | + let start = Self::period_start(); |
| 44 | + let end = Self::period_end(); |
| 45 | + if start > end { Some(start - end) } else { Some(end - start) } |
| 46 | + } |
| 47 | + |
| 48 | + /// Helper to get the start of the period. Panics if not period or a bad bound. |
| 49 | + fn period_start() -> T { |
| 50 | + let start = Self::PERIODIC.unwrap().0; |
| 51 | + let (Bound::Included(start) | Bound::Excluded(start)) = start else { |
| 52 | + panic!("`Unbounded` in period {:?}", Self::PERIODIC); |
| 53 | + }; |
| 54 | + start |
| 55 | + } |
| 56 | + |
| 57 | + /// Helper to get the end of the period. Panics if not period or a bad bound. |
| 58 | + fn period_end() -> T { |
| 59 | + let end = Self::PERIODIC.unwrap().1; |
| 60 | + let (Bound::Included(end) | Bound::Excluded(end)) = end else { |
| 61 | + panic!("`Unbounded` in period {:?}", Self::PERIODIC); |
| 62 | + }; |
| 63 | + end |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/* Possible domains */ |
| 68 | + |
| 69 | +/// Use for anything basic, no bounds, no asymptotes, etc. |
| 70 | +pub struct Unbounded; |
| 71 | +impl<F: Float> Domain<F> for Unbounded { |
| 72 | + const DEFINED: (Bound<F>, Bound<F>) = unbounded(); |
| 73 | +} |
| 74 | + |
| 75 | +/// Used for versions of `asin` and `acos`. |
| 76 | +pub struct InvTrigPeriodic; |
| 77 | +impl<F: Float> Domain<F> for InvTrigPeriodic { |
| 78 | + const DEFINED: (Bound<F>, Bound<F>) = (Bound::Included(F::NEG_ONE), Bound::Included(F::ONE)); |
| 79 | +} |
| 80 | + |
| 81 | +/// Domain for `acosh` |
| 82 | +pub struct ACoshDomain; |
| 83 | +impl<F: Float> Domain<F> for ACoshDomain { |
| 84 | + const DEFINED: (Bound<F>, Bound<F>) = (Bound::Included(F::ONE), Bound::Unbounded); |
| 85 | +} |
| 86 | + |
| 87 | +/// Domain for `atanh` |
| 88 | +pub struct ATanhDomain; |
| 89 | +impl<F: Float> Domain<F> for ATanhDomain { |
| 90 | + const DEFINED: (Bound<F>, Bound<F>) = (Bound::Excluded(F::NEG_ONE), Bound::Excluded(F::ONE)); |
| 91 | + |
| 92 | + fn defined_asymptotes() -> impl Iterator<Item = (F, F)> { |
| 93 | + [(F::NEG_ONE, F::NEG_INFINITY), (F::ONE, F::NEG_INFINITY)].into_iter() |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// Domain for `sin`, `cos`, and `tan` |
| 98 | +pub struct TrigDomain; |
| 99 | +impl<F: Float> Domain<F> for TrigDomain { |
| 100 | + const DEFINED: (Bound<F>, Bound<F>) = unbounded(); |
| 101 | + |
| 102 | + const PERIODIC: Option<(Bound<F>, Bound<F>)> = |
| 103 | + Some((Bound::Excluded(F::NEG_PI), Bound::Included(F::PI))); |
| 104 | + |
| 105 | + fn check_points() -> impl Iterator<Item = F> { |
| 106 | + [-F::PI, -F::FRAC_PI_2, F::FRAC_PI_2, F::PI].into_iter() |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/// Domain for `log` in various bases |
| 111 | +pub struct LogDomain; |
| 112 | +impl<F: Float> Domain<F> for LogDomain { |
| 113 | + const DEFINED: (Bound<F>, Bound<F>) = strictly_positive(); |
| 114 | + |
| 115 | + fn defined_asymptotes() -> impl Iterator<Item = (F, F)> { |
| 116 | + [(F::ZERO, F::NEG_INFINITY)].into_iter() |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/// Domain for `log1p` i.e. `log(1 + x)` |
| 121 | +pub struct Log1pDomain; |
| 122 | +impl<F: Float> Domain<F> for Log1pDomain { |
| 123 | + const DEFINED: (Bound<F>, Bound<F>) = (Bound::Excluded(F::NEG_ONE), Bound::Unbounded); |
| 124 | + |
| 125 | + fn defined_asymptotes() -> impl Iterator<Item = (F, F)> { |
| 126 | + [(F::NEG_ONE, F::NEG_INFINITY)].into_iter() |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/// Domain for `sqrt` |
| 131 | +pub struct SqrtDomain; |
| 132 | +impl<F: Float> Domain<F> for SqrtDomain { |
| 133 | + const DEFINED: (Bound<F>, Bound<F>) = positive(); |
| 134 | +} |
| 135 | + |
| 136 | +/// x ∈ ℝ |
| 137 | +const fn unbounded<F: Float>() -> (Bound<F>, Bound<F>) { |
| 138 | + (Bound::Unbounded, Bound::Unbounded) |
| 139 | +} |
| 140 | + |
| 141 | +/// x ∈ ℝ >= 0 |
| 142 | +const fn positive<F: Float>() -> (Bound<F>, Bound<F>) { |
| 143 | + (Bound::Included(F::ZERO), Bound::Unbounded) |
| 144 | +} |
| 145 | + |
| 146 | +/// x ∈ ℝ > 0 |
| 147 | +const fn strictly_positive<F: Float>() -> (Bound<F>, Bound<F>) { |
| 148 | + (Bound::Excluded(F::ZERO), Bound::Unbounded) |
| 149 | +} |
| 150 | + |
| 151 | +/// Implement on `op::*` types to indicate how they are bounded. |
| 152 | +pub trait HasDomain<T> |
| 153 | +where |
| 154 | + T: Copy + fmt::Debug + ops::Add<Output = T> + ops::Sub<Output = T> + PartialOrd + 'static, |
| 155 | +{ |
| 156 | + type D: Domain<T>; |
| 157 | +} |
| 158 | + |
| 159 | +/// Implement [`HasDomain`] for both the `f32` and `f64` variants of a function. |
| 160 | +macro_rules! impl_has_domain { |
| 161 | + ($($fn_name:ident => $domain:ty;)*) => { |
| 162 | + paste::paste! { |
| 163 | + $( |
| 164 | + // Implement for f64 functions |
| 165 | + impl HasDomain<f64> for $crate::op::$fn_name::Routine { |
| 166 | + type D = $domain; |
| 167 | + } |
| 168 | + |
| 169 | + // Implement for f32 functions |
| 170 | + impl HasDomain<f32> for $crate::op::[< $fn_name f >]::Routine { |
| 171 | + type D = $domain; |
| 172 | + } |
| 173 | + )* |
| 174 | + } |
| 175 | + }; |
| 176 | +} |
| 177 | + |
| 178 | +// Tie functions together with their domains. |
| 179 | +impl_has_domain! { |
| 180 | + acos => InvTrigPeriodic; |
| 181 | + acosh => ACoshDomain; |
| 182 | + asin => InvTrigPeriodic; |
| 183 | + asinh => Unbounded; |
| 184 | + // TODO asymptotes |
| 185 | + atan => Unbounded; |
| 186 | + atanh => ATanhDomain; |
| 187 | + cbrt => Unbounded; |
| 188 | + ceil => Unbounded; |
| 189 | + cos => TrigDomain; |
| 190 | + cosh => Unbounded; |
| 191 | + erf => Unbounded; |
| 192 | + exp => Unbounded; |
| 193 | + exp10 => Unbounded; |
| 194 | + exp2 => Unbounded; |
| 195 | + expm1 => Unbounded; |
| 196 | + fabs => Unbounded; |
| 197 | + floor => Unbounded; |
| 198 | + frexp => Unbounded; |
| 199 | + j0 => Unbounded; |
| 200 | + j1 => Unbounded; |
| 201 | + log => LogDomain; |
| 202 | + log10 => LogDomain; |
| 203 | + log1p => Log1pDomain; |
| 204 | + log2 => LogDomain; |
| 205 | + rint => Unbounded; |
| 206 | + round => Unbounded; |
| 207 | + sin => TrigDomain; |
| 208 | + sinh => Unbounded; |
| 209 | + sqrt => SqrtDomain; |
| 210 | + tan => TrigDomain; |
| 211 | + tanh => Unbounded; |
| 212 | + trunc => Unbounded; |
| 213 | +} |
0 commit comments