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