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