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 #if defined(ELFOBJ)
31 #pragma weak jnl = __jnl
32 #pragma weak ynl = __ynl
33 #endif
34
35 /*
36 * floating point Bessel's function of the 1st and 2nd kind
37 * of order n: jn(n,x),yn(n,x);
38 *
39 * Special cases:
40 * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
41 * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
42 * Note 2. About jn(n,x), yn(n,x)
43 * For n=0, j0(x) is called,
44 * for n=1, j1(x) is called,
45 * for n<x, forward recursion us used starting
46 * from values of j0(x) and j1(x).
47 * for n>x, a continued fraction approximation to
48 * j(n,x)/j(n-1,x) is evaluated and then backward
49 * recursion is used starting from a supposed value
50 * for j(n,x). The resulting value of j(0,x) is
51 * compared with the actual value to correct the
52 * supposed value of j(n,x).
53 *
54 * yn(n,x) is similar in all respects, except
55 * that forward recursion is used for all
56 * values of n>1.
57 *
58 */
59
60 #include "libm.h"
61 #include "longdouble.h"
62 #include <float.h> /* LDBL_MAX */
63
64 #define GENERIC long double
65
66 static const GENERIC
67 invsqrtpi = 5.641895835477562869480794515607725858441e-0001L,
68 two = 2.0L,
69 zero = 0.0L,
70 one = 1.0L;
71
72 GENERIC
73 jnl(n, x) int n; GENERIC x; {
74 int i, sgn;
75 GENERIC a, b, temp, z, w;
76
77 /*
78 * J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
79 * Thus, J(-n,x) = J(n,-x)
80 */
81 if (n < 0) {
82 n = -n;
83 x = -x;
84 }
85 if (n == 0)
86 return (j0l(x));
87 if (n == 1)
88 return (j1l(x));
89 if (x != x)
90 return (x+x);
91 if ((n&1) == 0)
92 sgn = 0; /* even n */
93 else
94 sgn = signbitl(x); /* old n */
95 x = fabsl(x);
96 if (x == zero || !finitel(x)) b = zero;
97 else if ((GENERIC)n <= x) {
98 /*
99 * Safe to use
100 * J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
101 */
102 if (x > 1.0e91L) {
103 /*
104 * x >> n**2
105 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
106 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
107 * Let s=sin(x), c=cos(x),
108 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
109 *
110 * n sin(xn)*sqt2 cos(xn)*sqt2
111 * ----------------------------------
112 * 0 s-c c+s
113 * 1 -s-c -c+s
114 * 2 -s+c -c-s
115 * 3 s+c c-s
116 */
117 switch (n&3) {
118 case 0: temp = cosl(x)+sinl(x); break;
119 case 1: temp = -cosl(x)+sinl(x); break;
120 case 2: temp = -cosl(x)-sinl(x); break;
121 case 3: temp = cosl(x)-sinl(x); break;
122 }
123 b = invsqrtpi*temp/sqrtl(x);
124 } else {
125 a = j0l(x);
126 b = j1l(x);
127 for (i = 1; i < n; i++) {
128 temp = b;
129 b = b*((GENERIC)(i+i)/x) - a; /* avoid underflow */
130 a = temp;
131 }
132 }
133 } else {
134 if (x < 1e-17L) { /* use J(n,x) = 1/n!*(x/2)^n */
135 b = powl(0.5L*x, (GENERIC)n);
136 if (b != zero) {
137 for (a = one, i = 1; i <= n; i++) a *= (GENERIC)i;
138 b = b/a;
139 }
140 } else {
141 /* use backward recurrence */
142 /*
143 * x x^2 x^2
144 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
145 * 2n - 2(n+1) - 2(n+2)
146 *
147 * 1 1 1
148 * (for large x) = ---- ------ ------ .....
149 * 2n 2(n+1) 2(n+2)
150 * -- - ------ - ------ -
151 * x x x
152 *
153 * Let w = 2n/x and h=2/x, then the above quotient
154 * is equal to the continued fraction:
155 * 1
156 * = -----------------------
157 * 1
158 * w - -----------------
159 * 1
160 * w+h - ---------
161 * w+2h - ...
162 *
163 * To determine how many terms needed, let
164 * Q(0) = w, Q(1) = w(w+h) - 1,
165 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
166 * When Q(k) > 1e4 good for single
167 * When Q(k) > 1e9 good for double
168 * When Q(k) > 1e17 good for quaduple
169 */
170 /* determin k */
171 GENERIC t, v;
172 double q0, q1, h, tmp; int k, m;
173 w = (n+n)/(double)x; h = 2.0/(double)x;
174 q0 = w; z = w+h; q1 = w*z - 1.0; k = 1;
175 while (q1 < 1.0e17) {
176 k += 1; z += h;
177 tmp = z*q1 - q0;
178 q0 = q1;
179 q1 = tmp;
180 }
181 m = n+n;
182 for (t = zero, i = 2*(n+k); i >= m; i -= 2) t = one/(i/x-t);
183 a = t;
184 b = one;
185 /*
186 * estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
187 * hence, if n*(log(2n/x)) > ...
188 * single 8.8722839355e+01
189 * double 7.09782712893383973096e+02
190 * long double 1.1356523406294143949491931077970765006170e+04
191 * then recurrent value may overflow and the result is
192 * likely underflow to zero
193 */
194 tmp = n;
195 v = two/x;
196 tmp = tmp*logl(fabsl(v*tmp));
197 if (tmp < 1.1356523406294143949491931077970765e+04L) {
198 for (i = n-1; i > 0; i--) {
199 temp = b;
200 b = ((i+i)/x)*b - a;
201 a = temp;
202 }
203 } else {
204 for (i = n-1; i > 0; i--) {
205 temp = b;
206 b = ((i+i)/x)*b - a;
207 a = temp;
208 if (b > 1e1000L) {
209 a /= b;
210 t /= b;
211 b = 1.0;
212 }
213 }
214 }
215 b = (t*j0l(x)/b);
216 }
217 }
218 if (sgn == 1)
219 return (-b);
220 else
221 return (b);
222 }
223
224 GENERIC ynl(n, x)
225 int n; GENERIC x; {
226 int i;
227 int sign;
228 GENERIC a, b, temp;
229
230 if (x != x)
231 return (x+x);
232 if (x <= zero) {
233 if (x == zero)
234 return (-one/zero);
235 else
236 return (zero/zero);
237 }
238 sign = 1;
239 if (n < 0) {
240 n = -n;
241 if ((n&1) == 1) sign = -1;
242 }
243 if (n == 0)
244 return (y0l(x));
245 if (n == 1)
246 return (sign*y1l(x));
247 if (!finitel(x))
248 return (zero);
249
250 if (x > 1.0e91L) { /* x >> n**2
251 Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
252 Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
253 Let s = sin(x), c = cos(x),
254 xn = x-(2n+1)*pi/4, sqt2 = sqrt(2), then
255
256 n sin(xn)*sqt2 cos(xn)*sqt2
257 ----------------------------------
258 0 s-c c+s
259 1 -s-c -c+s
260 2 -s+c -c-s
261 3 s+c c-s
262 */
263 switch (n&3) {
264 case 0: temp = sinl(x)-cosl(x); break;
265 case 1: temp = -sinl(x)-cosl(x); break;
266 case 2: temp = -sinl(x)+cosl(x); break;
267 case 3: temp = sinl(x)+cosl(x); break;
268 }
269 b = invsqrtpi*temp/sqrtl(x);
270 } else {
271 a = y0l(x);
272 b = y1l(x);
273 /*
274 * fix 1262058 and take care of non-default rounding
275 */
276 for (i = 1; i < n; i++) {
277 temp = b;
278 b *= (GENERIC) (i + i) / x;
279 if (b <= -LDBL_MAX)
280 break;
281 b -= a;
282 a = temp;
283 }
284 }
285 if (sign > 0)
286 return (b);
287 else
288 return (-b);
289 }