| 1 |
/* |
| 2 |
* // Copyright (c) Radzivon Bartoshyk 7/2025. All rights reserved. |
| 3 |
* // |
| 4 |
* // Redistribution and use in source and binary forms, with or without modification, |
| 5 |
* // are permitted provided that the following conditions are met: |
| 6 |
* // |
| 7 |
* // 1. Redistributions of source code must retain the above copyright notice, this |
| 8 |
* // list of conditions and the following disclaimer. |
| 9 |
* // |
| 10 |
* // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
* // this list of conditions and the following disclaimer in the documentation |
| 12 |
* // and/or other materials provided with the distribution. |
| 13 |
* // |
| 14 |
* // 3. Neither the name of the copyright holder nor the names of its |
| 15 |
* // contributors may be used to endorse or promote products derived from |
| 16 |
* // this software without specific prior written permission. |
| 17 |
* // |
| 18 |
* // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
* // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
* // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 |
* // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 22 |
* // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 |
* // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 |
* // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 |
* // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 |
* // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 |
* // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
use crate::bessel::j0f::j1f_rsqrt; |
| 30 |
use crate::common::f_fmla; |
| 31 |
use crate::exponents::core_expf; |
| 32 |
use crate::polyeval::{f_estrin_polyeval7, f_estrin_polyeval9, f_polyeval10}; |
| 33 |
|
| 34 |
/// Modified exponentially scaled Bessel of the first kind of order 1 |
| 35 |
/// |
| 36 |
/// Computes exp(-|x|)*I1(x) |
| 37 |
/// |
| 38 |
/// Max ULP 0.5 |
| 39 |
pub fn f_i1ef(x: f32) -> f32 { |
| 40 |
let ux = x.to_bits().wrapping_shl(1); |
| 41 |
if ux >= 0xffu32 << 24 || ux == 0 { |
| 42 |
// |x| == 0, |x| == inf, x == NaN |
| 43 |
if ux == 0 { |
| 44 |
// |x| == 0 |
| 45 |
return 0.; |
| 46 |
} |
| 47 |
if x.is_infinite() { |
| 48 |
return if x.is_sign_positive() { 0. } else { -0. }; |
| 49 |
} |
| 50 |
return x + f32::NAN; // |x| == NaN |
| 51 |
} |
| 52 |
|
| 53 |
let xb = x.to_bits() & 0x7fff_ffff; |
| 54 |
|
| 55 |
static SIGN: [f64; 2] = [1., -1.]; |
| 56 |
|
| 57 |
let sign_scale = SIGN[x.is_sign_negative() as usize]; |
| 58 |
|
| 59 |
if xb <= 0x40f80000u32 { |
| 60 |
// |x| <= 7.75 |
| 61 |
let core_expf = core_expf(-f32::from_bits(xb)); |
| 62 |
if xb <= 0x34000000u32 { |
| 63 |
// |x| <= f32::EPSILON |
| 64 |
// taylor series for I1(x)/Exp(x) ~ x/2 - x^2/2 + O(x^3) |
| 65 |
#[cfg(any( |
| 66 |
all( |
| 67 |
any(target_arch = "x86", target_arch = "x86_64"), |
| 68 |
target_feature = "fma" |
| 69 |
), |
| 70 |
target_arch = "aarch64" |
| 71 |
))] |
| 72 |
{ |
| 73 |
use crate::common::f_fmlaf; |
| 74 |
let half_x = 0.5 * x; |
| 75 |
return f_fmlaf(x, -half_x, half_x); |
| 76 |
} |
| 77 |
#[cfg(not(any( |
| 78 |
all( |
| 79 |
any(target_arch = "x86", target_arch = "x86_64"), |
| 80 |
target_feature = "fma" |
| 81 |
), |
| 82 |
target_arch = "aarch64" |
| 83 |
)))] |
| 84 |
{ |
| 85 |
let dx = x as f64; |
| 86 |
let half_x = 0.5 * dx; |
| 87 |
return f_fmla(dx, -half_x, half_x) as f32; |
| 88 |
} |
| 89 |
} |
| 90 |
return i1ef_small(f32::from_bits(xb), sign_scale, core_expf) as f32; |
| 91 |
} |
| 92 |
|
| 93 |
i1ef_asympt(f32::from_bits(xb), sign_scale) |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
Computes |
| 98 |
I1(x) = x/2 * (1 + 1 * (x/2)^2 + (x/2)^4 * P((x/2)^2)) |
| 99 |
|
| 100 |
Generated by Woflram Mathematica: |
| 101 |
|
| 102 |
```text |
| 103 |
<<FunctionApproximations` |
| 104 |
ClearAll["Global`*"] |
| 105 |
f[x_]:=(BesselI[1,x]*2/x-1-1/2(x/2)^2)/(x/2)^4 |
| 106 |
g[z_]:=f[2 Sqrt[z]] |
| 107 |
{err, approx}=MiniMaxApproximation[g[z],{z,{0.000000001,7.75},6,6},WorkingPrecision->60] |
| 108 |
poly=Numerator[approx][[1]]; |
| 109 |
coeffs=CoefficientList[poly,z]; |
| 110 |
TableForm[Table[Row[{"'",NumberForm[coeffs[[i+1]],{50,50}, ExponentFunction->(Null&)],"',"}],{i,0,Length[coeffs]-1}]] |
| 111 |
poly=Denominator[approx][[1]]; |
| 112 |
coeffs=CoefficientList[poly,z]; |
| 113 |
TableForm[Table[Row[{"'",NumberForm[coeffs[[i+1]],{50,50}, ExponentFunction->(Null&)],"',"}],{i,0,Length[coeffs]-1}]] |
| 114 |
``` |
| 115 |
**/ |
| 116 |
#[inline] |
| 117 |
fn i1ef_small(x: f32, sign_scale: f64, core_expf: f64) -> f64 { |
| 118 |
let dx = x as f64; |
| 119 |
let x_over_two = dx * 0.5; |
| 120 |
let x_over_two_sqr = x_over_two * x_over_two; |
| 121 |
let x_over_two_p4 = x_over_two_sqr * x_over_two_sqr; |
| 122 |
|
| 123 |
let p_num = f_estrin_polyeval7( |
| 124 |
x_over_two_sqr, |
| 125 |
f64::from_bits(0x3fb5555555555555), |
| 126 |
f64::from_bits(0x3f706cdccca396c4), |
| 127 |
f64::from_bits(0x3f23f9e12bdbba92), |
| 128 |
f64::from_bits(0x3ec8e39208e926b2), |
| 129 |
f64::from_bits(0x3e62e53b433c42ff), |
| 130 |
f64::from_bits(0x3def7cb16d10fb46), |
| 131 |
f64::from_bits(0x3d6747cd73d9d783), |
| 132 |
); |
| 133 |
let p_den = f_estrin_polyeval7( |
| 134 |
x_over_two_sqr, |
| 135 |
f64::from_bits(0x3ff0000000000000), |
| 136 |
f64::from_bits(0xbfa2075f77b54885), |
| 137 |
f64::from_bits(0x3f438c6d797c29f5), |
| 138 |
f64::from_bits(0xbeda57e2a258c6da), |
| 139 |
f64::from_bits(0x3e677e777c569432), |
| 140 |
f64::from_bits(0xbdea9212a96babc1), |
| 141 |
f64::from_bits(0x3d5e183186d5d782), |
| 142 |
); |
| 143 |
let p = p_num / p_den; |
| 144 |
|
| 145 |
let p1 = f_fmla(0.5, x_over_two_sqr, 1.); |
| 146 |
let p2 = f_fmla(x_over_two_p4, p, p1); |
| 147 |
p2 * x_over_two * sign_scale * core_expf |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
Asymptotic expansion for I1. |
| 152 |
|
| 153 |
Computes: |
| 154 |
sqrt(x) * exp(-x) * I1(x) = Pn(1/x)/Qn(1/x) |
| 155 |
hence: |
| 156 |
I1(x)*exp(-x) = Pn(1/x)/Qm(1/x)/sqrt(x) |
| 157 |
|
| 158 |
Generated by Wolfram Mathematica: |
| 159 |
```text |
| 160 |
<<FunctionApproximations` |
| 161 |
ClearAll["Global`*"] |
| 162 |
f[x_]:=Sqrt[x] Exp[-x] BesselI[1,x] |
| 163 |
g[z_]:=f[1/z] |
| 164 |
{err,approx}=MiniMaxApproximation[g[z],{z,{2^-33,1/7.75},9,8},WorkingPrecision->60] |
| 165 |
poly=Numerator[approx][[1]]; |
| 166 |
coeffs=CoefficientList[poly,z]; |
| 167 |
TableForm[Table[Row[{"'",NumberForm[coeffs[[i+1]],{50,50},ExponentFunction->(Null&)],"',"}],{i,0,Length[coeffs]-1}]] |
| 168 |
poly=Denominator[approx][[1]]; |
| 169 |
coeffs=CoefficientList[poly,z]; |
| 170 |
TableForm[Table[Row[{"'",NumberForm[coeffs[[i+1]],{50,50},ExponentFunction->(Null&)],"',"}],{i,0,Length[coeffs]-1}]] |
| 171 |
``` |
| 172 |
**/ |
| 173 |
#[inline] |
| 174 |
fn i1ef_asympt(x: f32, sign_scale: f64) -> f32 { |
| 175 |
let dx = x as f64; |
| 176 |
let recip = 1. / dx; |
| 177 |
let p_num = f_polyeval10( |
| 178 |
recip, |
| 179 |
f64::from_bits(0x3fd9884533d43652), |
| 180 |
f64::from_bits(0xc030686a3694d13c), |
| 181 |
f64::from_bits(0x407344697f45c2ee), |
| 182 |
f64::from_bits(0xc0aa037ee36a8967), |
| 183 |
f64::from_bits(0x40d5b2eab8cf5b17), |
| 184 |
f64::from_bits(0xc0f65addf81dbee8), |
| 185 |
f64::from_bits(0x410afc22ec1f9b8b), |
| 186 |
f64::from_bits(0xc110821dd0fc12b4), |
| 187 |
f64::from_bits(0x40feb3452c93aada), |
| 188 |
f64::from_bits(0xc0c6d04e8c5d02f3), |
| 189 |
); |
| 190 |
let p_den = f_estrin_polyeval9( |
| 191 |
recip, |
| 192 |
f64::from_bits(0x3ff0000000000000), |
| 193 |
f64::from_bits(0xc04460707a9ceed4), |
| 194 |
f64::from_bits(0x4087ac89fcf51e9b), |
| 195 |
f64::from_bits(0xc0bf830689f31b42), |
| 196 |
f64::from_bits(0x40e9c281c367fab2), |
| 197 |
f64::from_bits(0xc109b59ade76eb8c), |
| 198 |
f64::from_bits(0x411d553a9f5673c5), |
| 199 |
f64::from_bits(0xc11f9dbe0665523b), |
| 200 |
f64::from_bits(0x4103b62a329b60d7), |
| 201 |
); |
| 202 |
let z = p_num / p_den; |
| 203 |
let r_sqrt = j1f_rsqrt(dx); |
| 204 |
(z * r_sqrt * sign_scale) as f32 |
| 205 |
} |
| 206 |
|
| 207 |
#[cfg(test)] |
| 208 |
mod tests { |
| 209 |
use super::*; |
| 210 |
|
| 211 |
#[test] |
| 212 |
fn test_i1ef() { |
| 213 |
assert!(f_i1ef(f32::NAN).is_nan()); |
| 214 |
assert_eq!(f_i1ef(f32::INFINITY), 0.0); |
| 215 |
assert_eq!(f_i1ef(f32::NEG_INFINITY), 0.0); |
| 216 |
assert_eq!(f_i1ef(0.), 0.); |
| 217 |
assert_eq!(f_i1ef(1.), 0.20791042); |
| 218 |
assert_eq!(f_i1ef(-1.), -0.20791042); |
| 219 |
assert_eq!(f_i1ef(9.), 0.12722498); |
| 220 |
assert_eq!(f_i1ef(-9.), -0.12722498); |
| 221 |
assert_eq!(f_i1ef(0.000000000543453), 2.717265e-10); |
| 222 |
assert_eq!(f_i1ef(-0.000000000543453), -2.717265e-10); |
| 223 |
} |
| 224 |
} |