compiler_builtins/math/libm_math/
jn.rs

1/* origin: FreeBSD /usr/src/lib/msun/src/e_jn.c */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunSoft, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 */
12/*
13 * jn(n, x), yn(n, x)
14 * floating point Bessel's function of the 1st and 2nd kind
15 * of order n
16 *
17 * Special cases:
18 *      y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
19 *      y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
20 * Note 2. About jn(n,x), yn(n,x)
21 *      For n=0, j0(x) is called,
22 *      for n=1, j1(x) is called,
23 *      for n<=x, forward recursion is used starting
24 *      from values of j0(x) and j1(x).
25 *      for n>x, a continued fraction approximation to
26 *      j(n,x)/j(n-1,x) is evaluated and then backward
27 *      recursion is used starting from a supposed value
28 *      for j(n,x). The resulting value of j(0,x) is
29 *      compared with the actual value to correct the
30 *      supposed value of j(n,x).
31 *
32 *      yn(n,x) is similar in all respects, except
33 *      that forward recursion is used for all
34 *      values of n>1.
35 */
36
37use super::{cos, fabs, get_high_word, get_low_word, j0, j1, log, sin, sqrt, y0, y1};
38
39const INVSQRTPI: f64 = 5.64189583547756279280e-01; /* 0x3FE20DD7, 0x50429B6D */
40
41/// Integer order of the [Bessel function](https://en.wikipedia.org/wiki/Bessel_function) of the first kind (f64).
42#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
43pub fn jn(n: i32, mut x: f64) -> f64 {
44    let mut ix: u32;
45    let lx: u32;
46    let nm1: i32;
47    let mut i: i32;
48    let mut sign: bool;
49    let mut a: f64;
50    let mut b: f64;
51    let mut temp: f64;
52
53    ix = get_high_word(x);
54    lx = get_low_word(x);
55    sign = (ix >> 31) != 0;
56    ix &= 0x7fffffff;
57
58    // -lx == !lx + 1
59    if ix | ((lx | (!lx).wrapping_add(1)) >> 31) > 0x7ff00000 {
60        /* nan */
61        return x;
62    }
63
64    /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
65     * Thus, J(-n,x) = J(n,-x)
66     */
67    /* nm1 = |n|-1 is used instead of |n| to handle n==INT_MIN */
68    if n == 0 {
69        return j0(x);
70    }
71    if n < 0 {
72        nm1 = -(n + 1);
73        x = -x;
74        sign = !sign;
75    } else {
76        nm1 = n - 1;
77    }
78    if nm1 == 0 {
79        return j1(x);
80    }
81
82    sign &= (n & 1) != 0; /* even n: 0, odd n: signbit(x) */
83    x = fabs(x);
84    if (ix | lx) == 0 || ix == 0x7ff00000 {
85        /* if x is 0 or inf */
86        b = 0.0;
87    } else if (nm1 as f64) < x {
88        /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
89        if ix >= 0x52d00000 {
90            /* x > 2**302 */
91            /* (x >> n**2)
92             *      Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
93             *      Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
94             *      Let s=sin(x), c=cos(x),
95             *          xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
96             *
97             *             n    sin(xn)*sqt2    cos(xn)*sqt2
98             *          ----------------------------------
99             *             0     s-c             c+s
100             *             1    -s-c            -c+s
101             *             2    -s+c            -c-s
102             *             3     s+c             c-s
103             */
104            temp = match nm1 & 3 {
105                0 => -cos(x) + sin(x),
106                1 => -cos(x) - sin(x),
107                2 => cos(x) - sin(x),
108                // 3
109                _ => cos(x) + sin(x),
110            };
111            b = INVSQRTPI * temp / sqrt(x);
112        } else {
113            a = j0(x);
114            b = j1(x);
115            i = 0;
116            while i < nm1 {
117                i += 1;
118                temp = b;
119                b = b * (2.0 * (i as f64) / x) - a; /* avoid underflow */
120                a = temp;
121            }
122        }
123    } else if ix < 0x3e100000 {
124        /* x < 2**-29 */
125        /* x is tiny, return the first Taylor expansion of J(n,x)
126         * J(n,x) = 1/n!*(x/2)^n  - ...
127         */
128        if nm1 > 32 {
129            /* underflow */
130            b = 0.0;
131        } else {
132            temp = x * 0.5;
133            b = temp;
134            a = 1.0;
135            i = 2;
136            while i <= nm1 + 1 {
137                a *= i as f64; /* a = n! */
138                b *= temp; /* b = (x/2)^n */
139                i += 1;
140            }
141            b = b / a;
142        }
143    } else {
144        /* use backward recurrence */
145        /*                      x      x^2      x^2
146         *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
147         *                      2n  - 2(n+1) - 2(n+2)
148         *
149         *                      1      1        1
150         *  (for large x)   =  ----  ------   ------   .....
151         *                      2n   2(n+1)   2(n+2)
152         *                      -- - ------ - ------ -
153         *                       x     x         x
154         *
155         * Let w = 2n/x and h=2/x, then the above quotient
156         * is equal to the continued fraction:
157         *                  1
158         *      = -----------------------
159         *                     1
160         *         w - -----------------
161         *                        1
162         *              w+h - ---------
163         *                     w+2h - ...
164         *
165         * To determine how many terms needed, let
166         * Q(0) = w, Q(1) = w(w+h) - 1,
167         * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
168         * When Q(k) > 1e4      good for single
169         * When Q(k) > 1e9      good for double
170         * When Q(k) > 1e17     good for quadruple
171         */
172        /* determine k */
173        let mut t: f64;
174        let mut q0: f64;
175        let mut q1: f64;
176        let mut w: f64;
177        let h: f64;
178        let mut z: f64;
179        let mut tmp: f64;
180        let nf: f64;
181
182        let mut k: i32;
183
184        nf = (nm1 as f64) + 1.0;
185        w = 2.0 * nf / x;
186        h = 2.0 / x;
187        z = w + h;
188        q0 = w;
189        q1 = w * z - 1.0;
190        k = 1;
191        while q1 < 1.0e9 {
192            k += 1;
193            z += h;
194            tmp = z * q1 - q0;
195            q0 = q1;
196            q1 = tmp;
197        }
198        t = 0.0;
199        i = k;
200        while i >= 0 {
201            t = 1.0 / (2.0 * ((i as f64) + nf) / x - t);
202            i -= 1;
203        }
204        a = t;
205        b = 1.0;
206        /*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
207         *  Hence, if n*(log(2n/x)) > ...
208         *  single 8.8722839355e+01
209         *  double 7.09782712893383973096e+02
210         *  long double 1.1356523406294143949491931077970765006170e+04
211         *  then recurrent value may overflow and the result is
212         *  likely underflow to zero
213         */
214        tmp = nf * log(fabs(w));
215        if tmp < 7.09782712893383973096e+02 {
216            i = nm1;
217            while i > 0 {
218                temp = b;
219                b = b * (2.0 * (i as f64)) / x - a;
220                a = temp;
221                i -= 1;
222            }
223        } else {
224            i = nm1;
225            while i > 0 {
226                temp = b;
227                b = b * (2.0 * (i as f64)) / x - a;
228                a = temp;
229                /* scale b to avoid spurious overflow */
230                let x1p500 = f64::from_bits(0x5f30000000000000); // 0x1p500 == 2^500
231                if b > x1p500 {
232                    a /= b;
233                    t /= b;
234                    b = 1.0;
235                }
236                i -= 1;
237            }
238        }
239        z = j0(x);
240        w = j1(x);
241        if fabs(z) >= fabs(w) {
242            b = t * z / b;
243        } else {
244            b = t * w / a;
245        }
246    }
247
248    if sign { -b } else { b }
249}
250
251/// Integer order of the [Bessel function](https://en.wikipedia.org/wiki/Bessel_function) of the second kind (f64).
252#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
253pub fn yn(n: i32, x: f64) -> f64 {
254    let mut ix: u32;
255    let lx: u32;
256    let mut ib: u32;
257    let nm1: i32;
258    let mut sign: bool;
259    let mut i: i32;
260    let mut a: f64;
261    let mut b: f64;
262    let mut temp: f64;
263
264    ix = get_high_word(x);
265    lx = get_low_word(x);
266    sign = (ix >> 31) != 0;
267    ix &= 0x7fffffff;
268
269    // -lx == !lx + 1
270    if ix | ((lx | (!lx).wrapping_add(1)) >> 31) > 0x7ff00000 {
271        /* nan */
272        return x;
273    }
274    if sign && (ix | lx) != 0 {
275        /* x < 0 */
276        return 0.0 / 0.0;
277    }
278    if ix == 0x7ff00000 {
279        return 0.0;
280    }
281
282    if n == 0 {
283        return y0(x);
284    }
285    if n < 0 {
286        nm1 = -(n + 1);
287        sign = (n & 1) != 0;
288    } else {
289        nm1 = n - 1;
290        sign = false;
291    }
292    if nm1 == 0 {
293        if sign {
294            return -y1(x);
295        } else {
296            return y1(x);
297        }
298    }
299
300    if ix >= 0x52d00000 {
301        /* x > 2**302 */
302        /* (x >> n**2)
303         *      Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
304         *      Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
305         *      Let s=sin(x), c=cos(x),
306         *          xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
307         *
308         *             n    sin(xn)*sqt2    cos(xn)*sqt2
309         *          ----------------------------------
310         *             0     s-c             c+s
311         *             1    -s-c            -c+s
312         *             2    -s+c            -c-s
313         *             3     s+c             c-s
314         */
315        temp = match nm1 & 3 {
316            0 => -sin(x) - cos(x),
317            1 => -sin(x) + cos(x),
318            2 => sin(x) + cos(x),
319            // 3
320            _ => sin(x) - cos(x),
321        };
322        b = INVSQRTPI * temp / sqrt(x);
323    } else {
324        a = y0(x);
325        b = y1(x);
326        /* quit if b is -inf */
327        ib = get_high_word(b);
328        i = 0;
329        while i < nm1 && ib != 0xfff00000 {
330            i += 1;
331            temp = b;
332            b = (2.0 * (i as f64) / x) * b - a;
333            ib = get_high_word(b);
334            a = temp;
335        }
336    }
337
338    if sign { -b } else { b }
339}