1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 */
25 /*
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
28 */
29
30 #pragma weak jn = __jn
31 #pragma weak yn = __yn
32
33 /*
34 * floating point Bessel's function of the 1st and 2nd kind
35 * of order n: jn(n,x),yn(n,x);
36 *
37 * Special cases:
38 * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
39 * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
40 * Note 2. About jn(n,x), yn(n,x)
41 * For n=0, j0(x) is called,
42 * for n=1, j1(x) is called,
43 * for n<x, forward recursion us used starting
44 * from values of j0(x) and j1(x).
45 * for n>x, a continued fraction approximation to
46 * j(n,x)/j(n-1,x) is evaluated and then backward
47 * recursion is used starting from a supposed value
48 * for j(n,x). The resulting value of j(0,x) is
49 * compared with the actual value to correct the
50 * supposed value of j(n,x).
51 *
52 * yn(n,x) is similar in all respects, except
53 * that forward recursion is used for all
54 * values of n>1.
55 *
56 */
57
58 #include "libm.h"
59 #include <float.h> /* DBL_MIN */
60 #include <values.h> /* X_TLOSS */
61 #include "xpg6.h" /* __xpg6 */
62
63 #define GENERIC double
64
65 static const GENERIC
66 invsqrtpi = 5.641895835477562869480794515607725858441e-0001,
67 two = 2.0,
68 zero = 0.0,
69 one = 1.0;
70
71 GENERIC
72 jn(int n, GENERIC x) {
73 int i, sgn;
74 GENERIC a, b, temp = 0;
75 GENERIC z, w, ox, on;
76
77 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
78 * Thus, J(-n,x) = J(n,-x)
79 */
80 ox = x; on = (GENERIC)n;
81 if(n<0){
82 n = -n;
83 x = -x;
84 }
85 if(isnan(x)) return x*x; /* + -> * for Cheetah */
86 if (!((int) _lib_version == libm_ieee ||
87 (__xpg6 & _C99SUSv3_math_errexcept) != 0)) {
88 if(fabs(x) > X_TLOSS) return _SVID_libm_err(on,ox,38);
89 }
90 if(n==0) return(j0(x));
91 if(n==1) return(j1(x));
92 if((n&1)==0)
93 sgn=0; /* even n */
94 else
95 sgn = signbit(x); /* old n */
96 x = fabs(x);
97 if(x == zero||!finite(x)) b = zero;
98 else if((GENERIC)n<=x) { /* Safe to use
99 J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
100 */
101 if(x>1.0e91) { /* x >> n**2
102 Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
103 Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
104 Let s=sin(x), c=cos(x),
105 xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
106
107 n sin(xn)*sqt2 cos(xn)*sqt2
108 ----------------------------------
109 0 s-c c+s
110 1 -s-c -c+s
111 2 -s+c -c-s
112 3 s+c c-s
113 */
114 switch(n&3) {
115 case 0: temp = cos(x)+sin(x); break;
116 case 1: temp = -cos(x)+sin(x); break;
117 case 2: temp = -cos(x)-sin(x); break;
118 case 3: temp = cos(x)-sin(x); break;
119 }
120 b = invsqrtpi*temp/sqrt(x);
121 } else {
122 a = j0(x);
123 b = j1(x);
124 for(i=1;i<n;i++){
125 temp = b;
126 b = b*((GENERIC)(i+i)/x) - a; /* avoid underflow */
127 a = temp;
128 }
129 }
130 } else {
131 if(x<1e-9) { /* use J(n,x) = 1/n!*(x/2)^n */
132 b = pow(0.5*x,(GENERIC) n);
133 if (b!=zero) {
134 for(a=one,i=1;i<=n;i++) a *= (GENERIC)i;
135 b = b/a;
136 }
137 } else {
138 /* use backward recurrence */
139 /* x x^2 x^2
140 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
141 * 2n - 2(n+1) - 2(n+2)
142 *
143 * 1 1 1
144 * (for large x) = ---- ------ ------ .....
145 * 2n 2(n+1) 2(n+2)
146 * -- - ------ - ------ -
147 * x x x
148 *
149 * Let w = 2n/x and h=2/x, then the above quotient
150 * is equal to the continued fraction:
151 * 1
152 * = -----------------------
153 * 1
154 * w - -----------------
155 * 1
156 * w+h - ---------
157 * w+2h - ...
158 *
159 * To determine how many terms needed, let
160 * Q(0) = w, Q(1) = w(w+h) - 1,
161 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
162 * When Q(k) > 1e4 good for single
163 * When Q(k) > 1e9 good for double
164 * When Q(k) > 1e17 good for quaduple
165 */
166 /* determin k */
167 GENERIC t,v;
168 double q0,q1,h,tmp; int k,m;
169 w = (n+n)/(double)x; h = 2.0/(double)x;
170 q0 = w; z = w+h; q1 = w*z - 1.0; k=1;
171 while(q1<1.0e9) {
172 k += 1; z += h;
173 tmp = z*q1 - q0;
174 q0 = q1;
175 q1 = tmp;
176 }
177 m = n+n;
178 for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
179 a = t;
180 b = one;
181 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
182 hence, if n*(log(2n/x)) > ...
183 single 8.8722839355e+01
184 double 7.09782712893383973096e+02
185 long double 1.1356523406294143949491931077970765006170e+04
186 then recurrent value may overflow and the result is
187 likely underflow to zero
188 */
189 tmp = n;
190 v = two/x;
191 tmp = tmp*log(fabs(v*tmp));
192 if(tmp<7.09782712893383973096e+02) {
193 for(i=n-1;i>0;i--){
194 temp = b;
195 b = ((i+i)/x)*b - a;
196 a = temp;
197 }
198 } else {
199 for(i=n-1;i>0;i--){
200 temp = b;
201 b = ((i+i)/x)*b - a;
202 a = temp;
203 if(b>1e100) {
204 a /= b;
205 t /= b;
206 b = 1.0;
207 }
208 }
209 }
210 b = (t*j0(x)/b);
211 }
212 }
213 if(sgn==1) return -b; else return b;
214 }
215
216 GENERIC
217 yn(int n, GENERIC x) {
218 int i;
219 int sign;
220 GENERIC a, b, temp = 0, ox, on;
221
222 ox = x; on = (GENERIC)n;
223 if(isnan(x)) return x*x; /* + -> * for Cheetah */
224 if (x <= zero) {
225 if(x==zero) {
226 /* return -one/zero; */
227 return _SVID_libm_err((GENERIC)n,x,12);
228 } else {
229 /* return zero/zero; */
230 return _SVID_libm_err((GENERIC)n,x,13);
231 }
232 }
233 if (!((int) _lib_version == libm_ieee ||
234 (__xpg6 & _C99SUSv3_math_errexcept) != 0)) {
235 if(x > X_TLOSS) return _SVID_libm_err(on,ox,39);
236 }
237 sign = 1;
238 if(n<0){
239 n = -n;
240 if((n&1) == 1) sign = -1;
241 }
242 if(n==0) return(y0(x));
243 if(n==1) return(sign*y1(x));
244 if(!finite(x)) return zero;
245
246 if(x>1.0e91) { /* x >> n**2
247 Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
248 Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
249 Let s=sin(x), c=cos(x),
250 xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
251
252 n sin(xn)*sqt2 cos(xn)*sqt2
253 ----------------------------------
254 0 s-c c+s
255 1 -s-c -c+s
256 2 -s+c -c-s
257 3 s+c c-s
258 */
259 switch(n&3) {
260 case 0: temp = sin(x)-cos(x); break;
261 case 1: temp = -sin(x)-cos(x); break;
262 case 2: temp = -sin(x)+cos(x); break;
263 case 3: temp = sin(x)+cos(x); break;
264 }
265 b = invsqrtpi*temp/sqrt(x);
266 } else {
267 a = y0(x);
268 b = y1(x);
269 /*
270 * fix 1262058 and take care of non-default rounding
271 */
272 for (i = 1; i < n; i++) {
273 temp = b;
274 b *= (GENERIC) (i + i) / x;
275 if (b <= -DBL_MAX)
276 break;
277 b -= a;
278 a = temp;
279 }
280 }
281 if(sign>0) return b; else return -b;
282 }