1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
8 Bessel function of the first and second kinds of order n.
11 // The original C code and the long comment below are
12 // from FreeBSD's /usr/src/lib/msun/src/e_jn.c and
13 // came with this notice. The go code is a simplified
14 // version of the original C.
16 // ====================================================
17 // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
19 // Developed at SunPro, a Sun Microsystems, Inc. business.
20 // Permission to use, copy, modify, and distribute this
21 // software is freely granted, provided that this notice
23 // ====================================================
25 // __ieee754_jn(n, x), __ieee754_yn(n, x)
26 // floating point Bessel's function of the 1st and 2nd kind
30 // y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
31 // y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
32 // Note 2. About jn(n,x), yn(n,x)
33 // For n=0, j0(x) is called,
34 // for n=1, j1(x) is called,
35 // for n<x, forward recursion is used starting
36 // from values of j0(x) and j1(x).
37 // for n>x, a continued fraction approximation to
38 // j(n,x)/j(n-1,x) is evaluated and then backward
39 // recursion is used starting from a supposed value
40 // for j(n,x). The resulting value of j(0,x) is
41 // compared with the actual value to correct the
42 // supposed value of j(n,x).
44 // yn(n,x) is similar in all respects, except
45 // that forward recursion is used for all
48 // Jn returns the order-n Bessel function of the first kind.
53 func Jn(n
int, x
float64) float64 {
55 TwoM29
= 1.0 / (1 << 29) // 2**-29 0x3e10000000000000
56 Two302
= 1 << 302 // 2**302 0x52D0000000000000
65 // J(-n, x) = (-1)**n * J(n, x), J(n, -x) = (-1)**n * J(n, x)
66 // Thus, J(-n, x) = J(n, -x)
84 sign
= true // odd n and negative x
89 // Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
90 if x
>= Two302
{ // x > 2**302
93 // Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
94 // Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
95 // Let s=sin(x), c=cos(x),
96 // xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
98 // n sin(xn)*sqt2 cos(xn)*sqt2
99 // ----------------------------------
108 temp
= Cos(x
) + Sin(x
)
110 temp
= -Cos(x
) + Sin(x
)
112 temp
= -Cos(x
) - Sin(x
)
114 temp
= Cos(x
) - Sin(x
)
116 b
= (1 / SqrtPi
) * temp
/ Sqrt(x
)
119 for i
, a
:= 1, J0(x
); i
< n
; i
++ {
120 a
, b
= b
, b
*(float64(i
+i
)/x
)-a
// avoid underflow
124 if x
< TwoM29
{ // 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 - ...
128 if n
> 33 { // underflow
134 for i
:= 2; i
<= n
; i
++ {
135 a
*= float64(i
) // a = n!
136 b
*= temp
// b = (x/2)**n
141 // use backward recurrence
143 // J(n,x)/J(n-1,x) = ---- ------ ------ .....
144 // 2n - 2(n+1) - 2(n+2)
147 // (for large x) = ---- ------ ------ .....
149 // -- - ------ - ------ -
152 // Let w = 2n/x and h=2/x, then the above quotient
153 // is equal to the continued fraction:
155 // = -----------------------
157 // w - -----------------
162 // To determine how many terms needed, let
163 // Q(0) = w, Q(1) = w(w+h) - 1,
164 // Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
165 // When Q(k) > 1e4 good for single
166 // When Q(k) > 1e9 good for double
167 // When Q(k) > 1e17 good for quadruple
170 w
:= float64(n
+n
) / x
183 for i
:= 2 * (n
+ k
); i
>= m
; i
-= 2 {
184 t
= 1 / (float64(i
)/x
- t
)
188 // estimate log((2/x)**n*n!) = n*log(2/x)+n*ln(n)
189 // Hence, if n*(log(2n/x)) > ...
190 // single 8.8722839355e+01
191 // double 7.09782712893383973096e+02
192 // long double 1.1356523406294143949491931077970765006170e+04
193 // then recurrent value may overflow and the result is
194 // likely underflow to zero
198 tmp
= tmp
* Log(Abs(v
*tmp
))
199 if tmp
< 7.09782712893383973096e+02 {
200 for i
:= n
- 1; i
> 0; i
-- {
205 for i
:= n
- 1; i
> 0; i
-- {
208 // scale b to avoid spurious overflow
225 // Yn returns the order-n Bessel function of the second kind.
227 // Special cases are:
229 // Yn(n > 0, 0) = -Inf
230 // Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even
231 // Y1(n, x < 0) = NaN
233 func Yn(n
int, x
float64) float64 {
234 const Two302
= 1 << 302 // 2**302 0x52D0000000000000
237 case x
< 0 ||
IsNaN(x
):
247 if n
< 0 && n
&1 == 1 {
256 sign
= true // sign true if n < 0 && |n| odd
266 if x
>= Two302
{ // x > 2**302
268 // Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
269 // Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
270 // Let s=sin(x), c=cos(x),
271 // xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
273 // n sin(xn)*sqt2 cos(xn)*sqt2
274 // ----------------------------------
283 temp
= Sin(x
) - Cos(x
)
285 temp
= -Sin(x
) - Cos(x
)
287 temp
= -Sin(x
) + Cos(x
)
289 temp
= Sin(x
) + Cos(x
)
291 b
= (1 / SqrtPi
) * temp
/ Sqrt(x
)
296 for i
:= 1; i
< n
&& !IsInf(b
, -1); i
++ {
297 a
, b
= b
, (float64(i
+i
)/x
)*b
-a