|
| 1 | +use ndarray::{Array, Dimension}; |
1 | 2 | use num_traits::real::Real; |
2 | 3 |
|
3 | | -/// Modified Bessel function of order 0. |
4 | | -/// |
5 | | -/// ## Notes |
6 | | -/// * The range is partitioned into the two intervals [0, 8] and (8, infinity). |
7 | | -/// * [Scipy has this as a |
8 | | -/// ufunc](<https://docs.scipy.org/doc/scipy/reference/special.html#special-functions-scipy-special>), |
9 | | -/// as a supposed wrapper over the Cephes routine. |
10 | | -pub fn i0<F>(x: F) -> F |
| 4 | +#[cfg(feature = "alloc")] |
| 5 | +use alloc::vec::Vec; |
| 6 | + |
| 7 | +/// All [functions located in the `Faster versions of common Bessel |
| 8 | +/// functions.`](<https://docs.scipy.org/doc/scipy/reference/special.html#faster-versions-of-common-bessel-functions>) |
| 9 | +pub trait Bessel { |
| 10 | + /// Modified Bessel function of order 0. |
| 11 | + /// |
| 12 | + /// ## Notes |
| 13 | + /// * The range is partitioned into the two intervals [0, 8] and (8, infinity). |
| 14 | + /// * [Scipy has this as a |
| 15 | + /// ufunc](<https://docs.scipy.org/doc/scipy/reference/special.html#special-functions-scipy-special>), |
| 16 | + /// as a supposed wrapper over the Cephes routine. We try to define it over reasonable types in |
| 17 | + /// the impl. |
| 18 | + fn i0(&self) -> Self; |
| 19 | + |
| 20 | + /// Exponentially scaled modified Bessel function of order 0. |
| 21 | + /// |
| 22 | + /// ## Notes |
| 23 | + /// * The range is partitioned into the two intervals [0, 8] and (8, infinity). |
| 24 | + /// * [Scipy has this as a |
| 25 | + /// ufunc](<https://docs.scipy.org/doc/scipy/reference/special.html#special-functions-scipy-special>), |
| 26 | + /// as a supposed wrapper over the Cephes routine. We try to define it over reasonable types in |
| 27 | + /// the impl. |
| 28 | + fn i0e(&self) -> Self; |
| 29 | +} |
| 30 | + |
| 31 | +impl Bessel for f32 { |
| 32 | + // Known to yield wrong result. |
| 33 | + fn i0(&self) -> Self { |
| 34 | + return unsafe { special_fun::unsafe_cephes_single::i0f(*self) }; |
| 35 | + } |
| 36 | + |
| 37 | + fn i0e(&self) -> Self { |
| 38 | + return unsafe { special_fun::unsafe_cephes_single::i0ef(*self) }; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl Bessel for f64 { |
| 43 | + fn i0(&self) -> Self { |
| 44 | + return unsafe { special_fun::unsafe_cephes_double::i0(*self) }; |
| 45 | + } |
| 46 | + |
| 47 | + fn i0e(&self) -> Self { |
| 48 | + return unsafe { special_fun::unsafe_cephes_double::i0e(*self) }; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[cfg(feature = "alloc")] |
| 53 | +impl<F> Bessel for Vec<F> |
11 | 54 | where |
12 | | - F: Real, |
13 | | - f64: From<F>, |
| 55 | + F: Real + super::Bessel, |
14 | 56 | { |
15 | | - return F::from(unsafe { special_fun::unsafe_cephes_double::i0(f64::from(x)) }).unwrap(); |
| 57 | + fn i0(&self) -> Self { |
| 58 | + self.iter().map(|f| f.i0()).collect() |
| 59 | + } |
| 60 | + |
| 61 | + fn i0e(&self) -> Self { |
| 62 | + self.iter().map(|f| f.i0e()).collect() |
| 63 | + } |
16 | 64 | } |
17 | 65 |
|
18 | | -/// Exponentially scaled modified Bessel function of order 0. |
19 | | -/// |
20 | | -/// ## Notes |
21 | | -/// * The range is partitioned into the two intervals [0, 8] and (8, infinity). |
22 | | -/// * [Scipy has this as a |
23 | | -/// ufunc](<https://docs.scipy.org/doc/scipy/reference/special.html#special-functions-scipy-special>), |
24 | | -/// as a supposed wrapper over the Cephes routine. |
25 | | -pub fn i0e<F>(x: F) -> F |
| 66 | +#[cfg(feature = "alloc")] |
| 67 | +impl<F, D> Bessel for Array<F, D> |
26 | 68 | where |
27 | | - F: Real, |
28 | | - f64: From<F>, |
| 69 | + F: Real + super::Bessel, |
| 70 | + D: Dimension, |
29 | 71 | { |
30 | | - return F::from(unsafe { special_fun::unsafe_cephes_double::i0e(f64::from(x)) }).unwrap(); |
| 72 | + fn i0(&self) -> Self { |
| 73 | + self.map(|f| f.i0()) |
| 74 | + } |
| 75 | + |
| 76 | + fn i0e(&self) -> Self { |
| 77 | + self.map(|f| f.i0e()) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#[cfg(test)] |
| 82 | +mod tests { |
| 83 | + use super::*; |
| 84 | + use approx::assert_abs_diff_eq; |
| 85 | + |
| 86 | + #[test] |
| 87 | + #[ignore = "upstream"] |
| 88 | + fn i0single() { |
| 89 | + let inp: f32 = 0.213; |
| 90 | + // upstream (https://www.moshier.net/#Cephes) has to fix |
| 91 | + assert_abs_diff_eq!(1.0113744522192416, inp.i0()); |
| 92 | + assert_abs_diff_eq!(0.8173484705849442, inp.i0e()); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn i0double() { |
| 97 | + let inp: f64 = 0.213; |
| 98 | + assert_abs_diff_eq!(1.0113744522192416, inp.i0()); |
| 99 | + assert_abs_diff_eq!(0.8173484705849442, inp.i0e()); |
| 100 | + } |
31 | 101 | } |
0 commit comments