pxfm/bessel/k1f.rs
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 */
29use crate::common::f_fmla;
30use crate::exponents::core_expf;
31use crate::logs::fast_logf;
32use crate::polyeval::{f_estrin_polyeval8, f_polyeval3, f_polyeval4};
33
34/// Modified Bessel of the second kind of order 1
35///
36/// Max ULP 0.5
37pub fn f_k1f(x: f32) -> f32 {
38 let ux = x.to_bits();
39 if ux >= 0xffu32 << 23 || ux == 0 {
40 // |x| == 0, |x| == inf, |x| == NaN, x < 0
41 if ux.wrapping_shl(1) == 0 {
42 return f32::INFINITY;
43 }
44 if x.is_infinite() {
45 return if x.is_sign_positive() { 0. } else { f32::NAN };
46 }
47 return x + f32::NAN;
48 }
49
50 let xb = x.to_bits();
51
52 if xb >= 0x42cbc779u32 {
53 // x > 101.889595
54 return 0.;
55 }
56
57 if xb <= 0x3f800000u32 {
58 // x <= 1.0
59 if xb <= 0x34000000u32 {
60 // |x| <= f32::EPSILON
61 let dx = x as f64;
62 let leading_term = 1. / dx;
63 if xb <= 0x3109705fu32 {
64 // |x| <= 2e-9
65 // taylor series for tiny K1(x) ~ 1/x + O(x)
66 return leading_term as f32;
67 }
68 // taylor series for small K1(x) ~ 1/x+1/4 (-1+2 EulerGamma-2 Log[2]+2 Log[x]) x + O(x^3)
69 const C: f64 = f64::from_bits(0xbff3b5b6028a83d7); // -1+2 EulerGamma-2 Log[2]
70 let log_x = fast_logf(x);
71 let r = f_fmla(log_x, 2., C);
72 let w0 = f_fmla(dx * 0.25, r, leading_term);
73 return w0 as f32;
74 }
75 return k1f_small(x);
76 }
77
78 k1f_asympt(x)
79}
80
81/**
82Computes
83I1(x) = x/2 * (1 + 1 * (x/2)^2 + (x/2)^4 * P((x/2)^2))
84
85Generated by Woflram Mathematica:
86
87```text
88<<FunctionApproximations`
89ClearAll["Global`*"]
90f[x_]:=(BesselI[1,x]*2/x-1-1/2(x/2)^2)/(x/2)^4
91g[z_]:=f[2 Sqrt[z]]
92{err, approx}=MiniMaxApproximation[g[z],{z,{0.000000001,1},3,2},WorkingPrecision->60]
93poly=Numerator[approx][[1]];
94coeffs=CoefficientList[poly,z];
95TableForm[Table[Row[{"'",NumberForm[coeffs[[i+1]],{50,50}, ExponentFunction->(Null&)],"',"}],{i,0,Length[coeffs]-1}]]
96poly=Denominator[approx][[1]];
97coeffs=CoefficientList[poly,z];
98TableForm[Table[Row[{"'",NumberForm[coeffs[[i+1]],{50,50}, ExponentFunction->(Null&)],"',"}],{i,0,Length[coeffs]-1}]]
99```
100**/
101#[inline]
102fn i1f_small(x: f32) -> f64 {
103 let dx = x as f64;
104 let x_over_two = dx * 0.5;
105 let x_over_two_sqr = x_over_two * x_over_two;
106 let x_over_two_p4 = x_over_two_sqr * x_over_two_sqr;
107
108 let p_num = f_polyeval4(
109 x_over_two_sqr,
110 f64::from_bits(0x3fb5555555555355),
111 f64::from_bits(0x3f6ebf07f0dbc49b),
112 f64::from_bits(0x3f1fdc02bf28a8d9),
113 f64::from_bits(0x3ebb5e7574c700a6),
114 );
115 let p_den = f_polyeval3(
116 x_over_two_sqr,
117 f64::from_bits(0x3ff0000000000000),
118 f64::from_bits(0xbfa39b64b6135b5a),
119 f64::from_bits(0x3f3fa729bbe951f9),
120 );
121 let p = p_num / p_den;
122
123 let p1 = f_fmla(0.5, x_over_two_sqr, 1.);
124 let p2 = f_fmla(x_over_two_p4, p, p1);
125 p2 * x_over_two
126}
127
128/**
129Series for
130f(x) := BesselK(1, x) - Log(x)*BesselI(1, x) - 1/x
131
132Generated by Wolfram Mathematica:
133```text
134<<FunctionApproximations`
135ClearAll["Global`*"]
136f[x_]:=(BesselK[1, x]-Log[x]BesselI[1,x]-1/x)/x
137g[z_]:=f[Sqrt[z]]
138{err, approx}=MiniMaxApproximation[g[z],{z,{0.000000001,1},3,3},WorkingPrecision->60]
139poly=Numerator[approx][[1]];
140coeffs=CoefficientList[poly,z];
141TableForm[Table[Row[{"'",NumberForm[coeffs[[i+1]],{50,50}, ExponentFunction->(Null&)],"',"}],{i,0,Length[coeffs]-1}]]
142poly=Denominator[approx][[1]];
143coeffs=CoefficientList[poly,z];
144TableForm[Table[Row[{"'",NumberForm[coeffs[[i+1]],{50,50}, ExponentFunction->(Null&)],"',"}],{i,0,Length[coeffs]-1}]]
145```
146**/
147#[inline]
148fn k1f_small(x: f32) -> f32 {
149 let dx = x as f64;
150 let rcp = 1. / dx;
151 let x2 = dx * dx;
152 let p_num = f_polyeval4(
153 x2,
154 f64::from_bits(0xbfd3b5b6028a83d6),
155 f64::from_bits(0xbfb3fde2c83f7cca),
156 f64::from_bits(0xbf662b2e5defbe8c),
157 f64::from_bits(0xbefa2a63cc5c4feb),
158 );
159 let p_den = f_polyeval4(
160 x2,
161 f64::from_bits(0x3ff0000000000000),
162 f64::from_bits(0xbf9833197207a7c6),
163 f64::from_bits(0x3f315663bc7330ef),
164 f64::from_bits(0xbeb9211958f6b8c3),
165 );
166 let p = p_num / p_den;
167
168 let lg = fast_logf(x);
169 let v_i = i1f_small(x);
170 let z = f_fmla(lg, v_i, rcp);
171 let z0 = f_fmla(p, dx, z);
172 z0 as f32
173}
174
175/**
176Generated by Wolfram Mathematica:
177```text
178<<FunctionApproximations`
179ClearAll["Global`*"]
180f[x_]:=Sqrt[x] Exp[x] BesselK[1,x]
181g[z_]:=f[1/z]
182{err, approx}=MiniMaxApproximation[g[z],{z,{0.000000001,1},7,7},WorkingPrecision->60]
183poly=Numerator[approx][[1]];
184coeffs=CoefficientList[poly,z];
185TableForm[Table[Row[{"'",NumberForm[coeffs[[i+1]],{50,50}, ExponentFunction->(Null&)],"',"}],{i,0,Length[coeffs]-1}]]
186poly=Denominator[approx][[1]];
187coeffs=CoefficientList[poly,z];
188TableForm[Table[Row[{"'",NumberForm[coeffs[[i+1]],{50,50}, ExponentFunction->(Null&)],"',"}],{i,0,Length[coeffs]-1}]]
189```
190**/
191#[inline]
192fn k1f_asympt(x: f32) -> f32 {
193 let dx = x as f64;
194 let recip = 1. / dx;
195 let e = core_expf(x);
196 let r_sqrt = dx.sqrt();
197 let p_num = f_estrin_polyeval8(
198 recip,
199 f64::from_bits(0x3ff40d931ff6270d),
200 f64::from_bits(0x402d250670ed7a6c),
201 f64::from_bits(0x404e517b9b494d38),
202 f64::from_bits(0x405cb02b7433a838),
203 f64::from_bits(0x405a03e606a1b871),
204 f64::from_bits(0x4045c98d4308dbcd),
205 f64::from_bits(0x401d115c4ce0540c),
206 f64::from_bits(0x3fd4213e72b24b3a),
207 );
208 let p_den = f_estrin_polyeval8(
209 recip,
210 f64::from_bits(0x3ff0000000000000),
211 f64::from_bits(0x402681096aa3a87d),
212 f64::from_bits(0x404623ab8d72ceea),
213 f64::from_bits(0x40530af06ea802b2),
214 f64::from_bits(0x404d526906fb9cec),
215 f64::from_bits(0x403281caca389f1b),
216 f64::from_bits(0x3ffdb93996948bb4),
217 f64::from_bits(0x3f9a009da07eb989),
218 );
219 let v = p_num / p_den;
220 let pp = v / (e * r_sqrt);
221 pp as f32
222}
223
224#[cfg(test)]
225mod tests {
226 use super::*;
227
228 #[test]
229 fn test_k1f() {
230 assert_eq!(f_k1f(0.3), 3.055992);
231 assert_eq!(f_k1f(1.89), 0.16180483);
232 assert_eq!(f_k1f(5.89), 0.0015156545);
233 assert_eq!(f_k1f(101.89), 0.);
234 assert_eq!(f_k1f(0.), f32::INFINITY);
235 assert_eq!(f_k1f(-0.), f32::INFINITY);
236 assert!(f_k1f(-0.5).is_nan());
237 assert!(f_k1f(f32::NEG_INFINITY).is_nan());
238 assert_eq!(f_k1f(f32::INFINITY), 0.);
239 }
240}