pxfm/bessel/j1f.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::bessel::j1_coeffs::{J1_ZEROS, J1_ZEROS_VALUE};
30use crate::bessel::j1f_coeffs::J1F_COEFFS;
31use crate::bessel::trigo_bessel::sin_small;
32use crate::double_double::DoubleDouble;
33use crate::polyeval::{f_polyeval7, f_polyeval10, f_polyeval12, f_polyeval14};
34use crate::rounding::CpuCeil;
35use crate::sincos_reduce::rem2pif_any;
36
37/// Bessel of the first kind of order 1
38///
39/// Max ULP 0.5
40///
41/// Note about accuracy:
42/// - Close to zero Bessel have tiny values such that testing against MPFR must be done exactly
43/// in the same precision, since any nearest representable number have ULP > 0.5.
44/// For example `J1(0.000000000000000000000000000000000000023509886)` in single precision
45/// have an error at least 0.72 ULP for any number with extended precision,
46/// that would be represented in f32.
47pub fn f_j1f(x: f32) -> f32 {
48 let ux = x.to_bits().wrapping_shl(1);
49 if ux >= 0xffu32 << 24 || ux == 0 {
50 // |x| == 0, |x| == inf, |x| == NaN
51 if ux == 0 {
52 // |x| == 0
53 return x;
54 }
55 if x.is_infinite() {
56 return 0.;
57 }
58 return x + f32::NAN; // x == NaN
59 }
60
61 let ax = x.to_bits() & 0x7fff_ffff;
62
63 if ax < 0x429533c2u32 {
64 // |x| <= 74.60109
65 if ax < 0x3e800000u32 {
66 // |x| <= 0.25
67 if ax <= 0x34000000u32 {
68 // |x| <= f32::EPSILON
69 // taylor series for J1(x) ~ x/2 + O(x^3)
70 return x * 0.5;
71 }
72 return poly_near_zero(x);
73 }
74 return small_argument_path(x);
75 }
76
77 // Exceptional cases:
78 if ax == 0x6ef9be45 {
79 return if x.is_sign_negative() {
80 f32::from_bits(0x187d8a8f)
81 } else {
82 -f32::from_bits(0x187d8a8f)
83 };
84 } else if ax == 0x7f0e5a38 {
85 return if x.is_sign_negative() {
86 -f32::from_bits(0x131f680b)
87 } else {
88 f32::from_bits(0x131f680b)
89 };
90 }
91
92 j1f_asympt(x) as f32
93}
94
95#[inline]
96fn j1f_rsqrt(x: f64) -> f64 {
97 (1. / x) * x.sqrt()
98}
99
100/*
101 Evaluates:
102 J1 = sqrt(2/(PI*x)) * beta(x) * cos(x - 3*PI/4 - alpha(x))
103 discarding 1*PI/2 using identities gives:
104 J1 = sqrt(2/(PI*x)) * beta(x) * sin(x - PI/4 - alpha(x))
105
106 to avoid squashing small (-PI/4 - alpha(x)) into a large x actual expansion is:
107
108 J1 = sqrt(2/(PI*x)) * beta(x) * sin((x mod 2*PI) - PI/4 - alpha(x))
109*/
110#[inline]
111fn j1f_asympt(x: f32) -> f64 {
112 static SGN: [f64; 2] = [1., -1.];
113 let sign_scale = SGN[x.is_sign_negative() as usize];
114 let x = f32::from_bits(x.to_bits() & 0x7fff_ffff);
115
116 let dx = x as f64;
117
118 let alpha = j1f_asympt_alpha(dx);
119 let beta = j1f_asympt_beta(dx);
120
121 let angle = rem2pif_any(x);
122
123 const SQRT_2_OVER_PI: f64 = f64::from_bits(0x3fe9884533d43651);
124 const MPI_OVER_4: f64 = f64::from_bits(0xbfe921fb54442d18);
125
126 let x0pi34 = MPI_OVER_4 - alpha;
127 let r0 = angle + x0pi34;
128
129 let m_sin = sin_small(r0);
130
131 let z0 = beta * m_sin;
132 let scale = SQRT_2_OVER_PI * j1f_rsqrt(dx);
133
134 scale * z0 * sign_scale
135}
136
137/**
138Note expansion generation below: this is negative series expressed in Sage as positive,
139so before any real evaluation `x=1/x` should be applied.
140
141Generated by SageMath:
142```python
143def binomial_like(n, m):
144 prod = QQ(1)
145 z = QQ(4)*(n**2)
146 for k in range(1,m + 1):
147 prod *= (z - (2*k - 1)**2)
148 return prod / (QQ(2)**(2*m) * (ZZ(m).factorial()))
149
150R = LaurentSeriesRing(RealField(300), 'x',default_prec=300)
151x = R.gen()
152
153def Pn_asymptotic(n, y, terms=10):
154 # now y = 1/x
155 return sum( (-1)**m * binomial_like(n, 2*m) / (QQ(2)**(2*m)) * y**(QQ(2)*m) for m in range(terms) )
156
157def Qn_asymptotic(n, y, terms=10):
158 return sum( (-1)**m * binomial_like(n, 2*m + 1) / (QQ(2)**(2*m + 1)) * y**(QQ(2)*m + 1) for m in range(terms) )
159
160P = Pn_asymptotic(1, x, 50)
161Q = Qn_asymptotic(1, x, 50)
162
163R_series = (-Q/P)
164
165# alpha is atan(R_series) so we're doing Taylor series atan expansion on R_series
166
167arctan_series_Z = sum([QQ(-1)**k * x**(QQ(2)*k+1) / RealField(700)(RealField(700)(2)*k+1) for k in range(25)])
168alpha_series = arctan_series_Z(R_series)
169
170# see the series
171print(alpha_series)
172```
173
174See notes/bessel_asympt.ipynb for generation
175**/
176#[inline]
177pub(crate) fn j1f_asympt_alpha(x: f64) -> f64 {
178 const C: [u64; 12] = [
179 0xbfd8000000000000,
180 0x3fc5000000000000,
181 0xbfd7bccccccccccd,
182 0x4002f486db6db6db,
183 0xc03e9fbf40000000,
184 0x4084997b55945d17,
185 0xc0d4a914195269d9,
186 0x412cd1b53816aec1,
187 0xc18aa4095d419351,
188 0x41ef809305f11b9d,
189 0xc2572e6809ed618b,
190 0x42c4c5b6057839f9,
191 ];
192 let recip = 1. / x;
193 let x2 = recip * recip;
194 let p = f_polyeval12(
195 x2,
196 f64::from_bits(C[0]),
197 f64::from_bits(C[1]),
198 f64::from_bits(C[2]),
199 f64::from_bits(C[3]),
200 f64::from_bits(C[4]),
201 f64::from_bits(C[5]),
202 f64::from_bits(C[6]),
203 f64::from_bits(C[7]),
204 f64::from_bits(C[8]),
205 f64::from_bits(C[9]),
206 f64::from_bits(C[10]),
207 f64::from_bits(C[11]),
208 );
209 p * recip
210}
211
212/**
213Note expansion generation below: this is negative series expressed in Sage as positive,
214so before any real evaluation `x=1/x` should be applied
215
216Generated by SageMath:
217```python
218def binomial_like(n, m):
219 prod = QQ(1)
220 z = QQ(4)*(n**2)
221 for k in range(1,m + 1):
222 prod *= (z - (2*k - 1)**2)
223 return prod / (QQ(2)**(2*m) * (ZZ(m).factorial()))
224
225R = LaurentSeriesRing(RealField(300), 'x',default_prec=300)
226x = R.gen()
227
228def Pn_asymptotic(n, y, terms=10):
229 # now y = 1/x
230 return sum( (-1)**m * binomial_like(n, 2*m) / (QQ(2)**(2*m)) * y**(QQ(2)*m) for m in range(terms) )
231
232def Qn_asymptotic(n, y, terms=10):
233 return sum( (-1)**m * binomial_like(n, 2*m + 1) / (QQ(2)**(2*m + 1)) * y**(QQ(2)*m + 1) for m in range(terms) )
234
235P = Pn_asymptotic(1, x, 50)
236Q = Qn_asymptotic(1, x, 50)
237
238def sqrt_series(s):
239 val = S.valuation()
240 lc = S[val] # Leading coefficient
241 b = lc.sqrt() * x**(val // 2)
242
243 for _ in range(5):
244 b = (b + S / b) / 2
245 b = b
246 return b
247
248S = (P**2 + Q**2).truncate(50)
249
250b_series = sqrt_series(S).truncate(30)
251# see the beta series
252print(b_series)
253```
254
255See notes/bessel_asympt.ipynb for generation
256**/
257#[inline]
258pub(crate) fn j1f_asympt_beta(x: f64) -> f64 {
259 const C: [u64; 10] = [
260 0x3ff0000000000000,
261 0x3fc8000000000000,
262 0xbfc8c00000000000,
263 0x3fe9c50000000000,
264 0xc01ef5b680000000,
265 0x40609860dd400000,
266 0xc0abae9b7a06e000,
267 0x41008711d41c1428,
268 0xc15ab70164c8be6e,
269 0x41bc1055e24f297f,
270 ];
271 let recip = 1. / x;
272 let x2 = recip * recip;
273 f_polyeval10(
274 x2,
275 f64::from_bits(C[0]),
276 f64::from_bits(C[1]),
277 f64::from_bits(C[2]),
278 f64::from_bits(C[3]),
279 f64::from_bits(C[4]),
280 f64::from_bits(C[5]),
281 f64::from_bits(C[6]),
282 f64::from_bits(C[7]),
283 f64::from_bits(C[8]),
284 f64::from_bits(C[9]),
285 )
286}
287
288/**
289Generated in Sollya:
290```python
291pretty = proc(u) {
292 return ~(floor(u*1000)/1000);
293};
294
295bessel_j1 = library("./cmake-build-release/libbessel_sollya.dylib");
296
297f = bessel_j1(x)/x;
298d = [0, 0.921];
299w = 1;
300pf = fpminimax(f, [|0,2,4,6,8,10,12|], [|D...|], d, absolute, floating);
301
302w = 1;
303or_f = bessel_j1(x);
304pf1 = pf * x;
305err_p = -log2(dirtyinfnorm(pf1*w-or_f, d));
306print ("relative error:", pretty(err_p));
307
308for i from 0 to degree(pf) by 2 do {
309 print("'", coeff(pf, i), "',");
310};
311```
312See ./notes/bessel_sollya/bessel_j1f_at_zero.sollya
313**/
314#[inline]
315fn poly_near_zero(x: f32) -> f32 {
316 let dx = x as f64;
317 let x2 = dx * dx;
318 let p = f_polyeval7(
319 x2,
320 f64::from_bits(0x3fe0000000000000),
321 f64::from_bits(0xbfaffffffffffffc),
322 f64::from_bits(0x3f65555555554089),
323 f64::from_bits(0xbf0c71c71c2a74ae),
324 f64::from_bits(0x3ea6c16bbd1dc5c1),
325 f64::from_bits(0xbe384562afb69e7d),
326 f64::from_bits(0x3dc248d0d0221cd0),
327 );
328 (p * dx) as f32
329}
330
331/// This method on small range searches for nearest zero or extremum.
332/// Then picks stored series expansion at the point end evaluates the poly at the point.
333#[inline]
334fn small_argument_path(x: f32) -> f32 {
335 static SIGN: [f64; 2] = [1., -1.];
336 let sign_scale = SIGN[x.is_sign_negative() as usize];
337 let x_abs = f32::from_bits(x.to_bits() & 0x7fff_ffff) as f64;
338
339 // let avg_step = 74.60109 / 47.0;
340 // let inv_step = 1.0 / avg_step;
341
342 const INV_STEP: f64 = 0.6300176043004198;
343
344 let fx = x_abs * INV_STEP;
345 const J1_ZEROS_COUNT: f64 = (J1_ZEROS.len() - 1) as f64;
346 let idx0 = unsafe { fx.min(J1_ZEROS_COUNT).to_int_unchecked::<usize>() };
347 let idx1 = unsafe {
348 fx.cpu_ceil()
349 .min(J1_ZEROS_COUNT)
350 .to_int_unchecked::<usize>()
351 };
352
353 let found_zero0 = DoubleDouble::from_bit_pair(J1_ZEROS[idx0]);
354 let found_zero1 = DoubleDouble::from_bit_pair(J1_ZEROS[idx1]);
355
356 let dist0 = (found_zero0.hi - x_abs).abs();
357 let dist1 = (found_zero1.hi - x_abs).abs();
358
359 let (found_zero, idx, dist) = if dist0 < dist1 {
360 (found_zero0, idx0, dist0)
361 } else {
362 (found_zero1, idx1, dist1)
363 };
364
365 if idx == 0 {
366 return poly_near_zero(x);
367 }
368
369 // We hit exact zero, value, better to return it directly
370 if dist == 0. {
371 return (f64::from_bits(J1_ZEROS_VALUE[idx]) * sign_scale) as f32;
372 }
373
374 let c = &J1F_COEFFS[idx - 1];
375
376 let r = (x_abs - found_zero.hi) - found_zero.lo;
377
378 let p = f_polyeval14(
379 r,
380 f64::from_bits(c[0]),
381 f64::from_bits(c[1]),
382 f64::from_bits(c[2]),
383 f64::from_bits(c[3]),
384 f64::from_bits(c[4]),
385 f64::from_bits(c[5]),
386 f64::from_bits(c[6]),
387 f64::from_bits(c[7]),
388 f64::from_bits(c[8]),
389 f64::from_bits(c[9]),
390 f64::from_bits(c[10]),
391 f64::from_bits(c[11]),
392 f64::from_bits(c[12]),
393 f64::from_bits(c[13]),
394 );
395
396 (p * sign_scale) as f32
397}
398
399#[cfg(test)]
400mod tests {
401 use super::*;
402
403 #[test]
404 fn test_f_j1f() {
405 assert_eq!(
406 f_j1f(77.743162408196766932633181568235159),
407 0.09049267898021947
408 );
409 assert_eq!(
410 f_j1f(-0.000000000000000000000000000000000000008827127),
411 -0.0000000000000000000000000000000000000044135635
412 );
413 assert_eq!(
414 f_j1f(0.000000000000000000000000000000000000008827127),
415 0.0000000000000000000000000000000000000044135635
416 );
417 assert_eq!(f_j1f(5.4), -0.3453447907795863);
418 assert_eq!(
419 f_j1f(84.027189586293545175976760219782591),
420 0.0870430264022591
421 );
422 assert_eq!(f_j1f(f32::INFINITY), 0.);
423 assert_eq!(f_j1f(f32::NEG_INFINITY), 0.);
424 assert!(f_j1f(f32::NAN).is_nan());
425 assert_eq!(f_j1f(-1.7014118e38), 0.000000000000000000006856925);
426 }
427}