Coverage Report

Created: 2025-12-08 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/libm-0.2.15/src/math/jn.rs
Line
Count
Source
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
37
use super::{cos, fabs, get_high_word, get_low_word, j0, j1, log, sin, sqrt, y0, y1};
38
39
const 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)]
43
0
pub 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
0
    ix = get_high_word(x);
54
0
    lx = get_low_word(x);
55
0
    sign = (ix >> 31) != 0;
56
0
    ix &= 0x7fffffff;
57
58
    // -lx == !lx + 1
59
0
    if ix | ((lx | (!lx).wrapping_add(1)) >> 31) > 0x7ff00000 {
60
        /* nan */
61
0
        return x;
62
0
    }
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
0
    if n == 0 {
69
0
        return j0(x);
70
0
    }
71
0
    if n < 0 {
72
0
        nm1 = -(n + 1);
73
0
        x = -x;
74
0
        sign = !sign;
75
0
    } else {
76
0
        nm1 = n - 1;
77
0
    }
78
0
    if nm1 == 0 {
79
0
        return j1(x);
80
0
    }
81
82
0
    sign &= (n & 1) != 0; /* even n: 0, odd n: signbit(x) */
83
0
    x = fabs(x);
84
0
    if (ix | lx) == 0 || ix == 0x7ff00000 {
85
0
        /* if x is 0 or inf */
86
0
        b = 0.0;
87
0
    } else if (nm1 as f64) < x {
88
        /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
89
0
        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
0
            temp = match nm1 & 3 {
105
0
                0 => -cos(x) + sin(x),
106
0
                1 => -cos(x) - sin(x),
107
0
                2 => cos(x) - sin(x),
108
                // 3
109
0
                _ => cos(x) + sin(x),
110
            };
111
0
            b = INVSQRTPI * temp / sqrt(x);
112
        } else {
113
0
            a = j0(x);
114
0
            b = j1(x);
115
0
            i = 0;
116
0
            while i < nm1 {
117
0
                i += 1;
118
0
                temp = b;
119
0
                b = b * (2.0 * (i as f64) / x) - a; /* avoid underflow */
120
0
                a = temp;
121
0
            }
122
        }
123
0
    } 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
0
        if nm1 > 32 {
129
0
            /* underflow */
130
0
            b = 0.0;
131
0
        } else {
132
0
            temp = x * 0.5;
133
0
            b = temp;
134
0
            a = 1.0;
135
0
            i = 2;
136
0
            while i <= nm1 + 1 {
137
0
                a *= i as f64; /* a = n! */
138
0
                b *= temp; /* b = (x/2)^n */
139
0
                i += 1;
140
0
            }
141
0
            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
0
        nf = (nm1 as f64) + 1.0;
185
0
        w = 2.0 * nf / x;
186
0
        h = 2.0 / x;
187
0
        z = w + h;
188
0
        q0 = w;
189
0
        q1 = w * z - 1.0;
190
0
        k = 1;
191
0
        while q1 < 1.0e9 {
192
0
            k += 1;
193
0
            z += h;
194
0
            tmp = z * q1 - q0;
195
0
            q0 = q1;
196
0
            q1 = tmp;
197
0
        }
198
0
        t = 0.0;
199
0
        i = k;
200
0
        while i >= 0 {
201
0
            t = 1.0 / (2.0 * ((i as f64) + nf) / x - t);
202
0
            i -= 1;
203
0
        }
204
0
        a = t;
205
0
        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
0
        tmp = nf * log(fabs(w));
215
0
        if tmp < 7.09782712893383973096e+02 {
216
0
            i = nm1;
217
0
            while i > 0 {
218
0
                temp = b;
219
0
                b = b * (2.0 * (i as f64)) / x - a;
220
0
                a = temp;
221
0
                i -= 1;
222
0
            }
223
        } else {
224
0
            i = nm1;
225
0
            while i > 0 {
226
0
                temp = b;
227
0
                b = b * (2.0 * (i as f64)) / x - a;
228
0
                a = temp;
229
                /* scale b to avoid spurious overflow */
230
0
                let x1p500 = f64::from_bits(0x5f30000000000000); // 0x1p500 == 2^500
231
0
                if b > x1p500 {
232
0
                    a /= b;
233
0
                    t /= b;
234
0
                    b = 1.0;
235
0
                }
236
0
                i -= 1;
237
            }
238
        }
239
0
        z = j0(x);
240
0
        w = j1(x);
241
0
        if fabs(z) >= fabs(w) {
242
0
            b = t * z / b;
243
0
        } else {
244
0
            b = t * w / a;
245
0
        }
246
    }
247
248
0
    if sign { -b } else { b }
249
0
}
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)]
253
0
pub 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
0
    ix = get_high_word(x);
265
0
    lx = get_low_word(x);
266
0
    sign = (ix >> 31) != 0;
267
0
    ix &= 0x7fffffff;
268
269
    // -lx == !lx + 1
270
0
    if ix | ((lx | (!lx).wrapping_add(1)) >> 31) > 0x7ff00000 {
271
        /* nan */
272
0
        return x;
273
0
    }
274
0
    if sign && (ix | lx) != 0 {
275
        /* x < 0 */
276
0
        return 0.0 / 0.0;
277
0
    }
278
0
    if ix == 0x7ff00000 {
279
0
        return 0.0;
280
0
    }
281
282
0
    if n == 0 {
283
0
        return y0(x);
284
0
    }
285
0
    if n < 0 {
286
0
        nm1 = -(n + 1);
287
0
        sign = (n & 1) != 0;
288
0
    } else {
289
0
        nm1 = n - 1;
290
0
        sign = false;
291
0
    }
292
0
    if nm1 == 0 {
293
0
        if sign {
294
0
            return -y1(x);
295
        } else {
296
0
            return y1(x);
297
        }
298
0
    }
299
300
0
    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
0
        temp = match nm1 & 3 {
316
0
            0 => -sin(x) - cos(x),
317
0
            1 => -sin(x) + cos(x),
318
0
            2 => sin(x) + cos(x),
319
            // 3
320
0
            _ => sin(x) - cos(x),
321
        };
322
0
        b = INVSQRTPI * temp / sqrt(x);
323
    } else {
324
0
        a = y0(x);
325
0
        b = y1(x);
326
        /* quit if b is -inf */
327
0
        ib = get_high_word(b);
328
0
        i = 0;
329
0
        while i < nm1 && ib != 0xfff00000 {
330
0
            i += 1;
331
0
            temp = b;
332
0
            b = (2.0 * (i as f64) / x) * b - a;
333
0
            ib = get_high_word(b);
334
0
            a = temp;
335
0
        }
336
    }
337
338
0
    if sign { -b } else { b }
339
0
}