xref: /original-bsd/lib/libm/common_source/j1.c (revision 95ecee29)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)j1.c	8.2 (Berkeley) 11/30/93";
10 #endif /* not lint */
11 
12 /*
13  * 16 December 1992
14  * Minor modifications by Peter McIlroy to adapt non-IEEE architecture.
15  */
16 
17 /*
18  * ====================================================
19  * Copyright (C) 1992 by Sun Microsystems, Inc.
20  *
21  * Developed at SunPro, a Sun Microsystems, Inc. business.
22  * Permission to use, copy, modify, and distribute this
23  * software is freely granted, provided that this notice
24  * is preserved.
25  * ====================================================
26  *
27  * ******************* WARNING ********************
28  * This is an alpha version of SunPro's FDLIBM (Freely
29  * Distributable Math Library) for IEEE double precision
30  * arithmetic. FDLIBM is a basic math library written
31  * in C that runs on machines that conform to IEEE
32  * Standard 754/854. This alpha version is distributed
33  * for testing purpose. Those who use this software
34  * should report any bugs to
35  *
36  *		fdlibm-comments@sunpro.eng.sun.com
37  *
38  * -- K.C. Ng, Oct 12, 1992
39  * ************************************************
40  */
41 
42 /* double j1(double x), y1(double x)
43  * Bessel function of the first and second kinds of order zero.
44  * Method -- j1(x):
45  *	1. For tiny x, we use j1(x) = x/2 - x^3/16 + x^5/384 - ...
46  *	2. Reduce x to |x| since j1(x)=-j1(-x),  and
47  *	   for x in (0,2)
48  *		j1(x) = x/2 + x*z*R0/S0,  where z = x*x;
49  *	   (precision:  |j1/x - 1/2 - R0/S0 |<2**-61.51 )
50  *	   for x in (2,inf)
51  * 		j1(x) = sqrt(2/(pi*x))*(p1(x)*cos(x1)-q1(x)*sin(x1))
52  * 		y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1))
53  * 	   where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1)
54  *	   as follows:
55  *		cos(x1) =  cos(x)cos(3pi/4)+sin(x)sin(3pi/4)
56  *			=  1/sqrt(2) * (sin(x) - cos(x))
57  *		sin(x1) =  sin(x)cos(3pi/4)-cos(x)sin(3pi/4)
58  *			= -1/sqrt(2) * (sin(x) + cos(x))
59  * 	   (To avoid cancellation, use
60  *		sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
61  * 	    to compute the worse one.)
62  *
63  *	3 Special cases
64  *		j1(nan)= nan
65  *		j1(0) = 0
66  *		j1(inf) = 0
67  *
68  * Method -- y1(x):
69  *	1. screen out x<=0 cases: y1(0)=-inf, y1(x<0)=NaN
70  *	2. For x<2.
71  *	   Since
72  *		y1(x) = 2/pi*(j1(x)*(ln(x/2)+Euler)-1/x-x/2+5/64*x^3-...)
73  *	   therefore y1(x)-2/pi*j1(x)*ln(x)-1/x is an odd function.
74  *	   We use the following function to approximate y1,
75  *		y1(x) = x*U(z)/V(z) + (2/pi)*(j1(x)*ln(x)-1/x), z= x^2
76  *	   where for x in [0,2] (abs err less than 2**-65.89)
77  *		U(z) = u0 + u1*z + ... + u4*z^4
78  *		V(z) = 1  + v1*z + ... + v5*z^5
79  *	   Note: For tiny x, 1/x dominate y1 and hence
80  *		y1(tiny) = -2/pi/tiny, (choose tiny<2**-54)
81  *	3. For x>=2.
82  * 		y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1))
83  * 	   where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1)
84  *	   by method mentioned above.
85  */
86 
87 #include <math.h>
88 #include <float.h>
89 
90 #if defined(vax) || defined(tahoe)
91 #define _IEEE	0
92 #else
93 #define _IEEE	1
94 #define infnan(x) (0.0)
95 #endif
96 
97 static double pone(), qone();
98 
99 static double
100 huge    = 1e300,
101 zero    = 0.0,
102 one	= 1.0,
103 invsqrtpi= 5.641895835477562869480794515607725858441e-0001,
104 tpi	= 0.636619772367581343075535053490057448,
105 
106 	/* R0/S0 on [0,2] */
107 r00 =  -6.250000000000000020842322918309200910191e-0002,
108 r01 =   1.407056669551897148204830386691427791200e-0003,
109 r02 =  -1.599556310840356073980727783817809847071e-0005,
110 r03 =   4.967279996095844750387702652791615403527e-0008,
111 s01 =   1.915375995383634614394860200531091839635e-0002,
112 s02 =   1.859467855886309024045655476348872850396e-0004,
113 s03 =   1.177184640426236767593432585906758230822e-0006,
114 s04 =   5.046362570762170559046714468225101016915e-0009,
115 s05 =   1.235422744261379203512624973117299248281e-0011;
116 
117 #define two_129	6.80564733841876926e+038	/* 2^129 */
118 #define two_m54	5.55111512312578270e-017	/* 2^-54 */
119 double j1(x)
120 	double x;
121 {
122 	double z, s,c,ss,cc,r,u,v,y;
123 	y = fabs(x);
124 	if (!finite(x))			/* Inf or NaN */
125 		if (_IEEE && x != x)
126 			return(x);
127 		else
128 			return (copysign(x, zero));
129 	y = fabs(x);
130 	if (y >= 2)			/* |x| >= 2.0 */
131 	{
132 		s = sin(y);
133 		c = cos(y);
134 		ss = -s-c;
135 		cc = s-c;
136 		if (y < .5*DBL_MAX) {  	/* make sure y+y not overflow */
137 		    z = cos(y+y);
138 		    if ((s*c)<zero) cc = z/ss;
139 		    else 	    ss = z/cc;
140 		}
141 	/*
142 	 * j1(x) = 1/sqrt(pi) * (P(1,x)*cc - Q(1,x)*ss) / sqrt(x)
143 	 * y1(x) = 1/sqrt(pi) * (P(1,x)*ss + Q(1,x)*cc) / sqrt(x)
144 	 */
145 #if !defined(vax) && !defined(tahoe)
146 		if (y > two_129)	 /* x > 2^129 */
147 			z = (invsqrtpi*cc)/sqrt(y);
148 		else
149 #endif /* defined(vax) || defined(tahoe) */
150 		{
151 		    u = pone(y); v = qone(y);
152 		    z = invsqrtpi*(u*cc-v*ss)/sqrt(y);
153 		}
154 		if (x < 0) return -z;
155 		else  	 return  z;
156 	}
157 	if (y < 7.450580596923828125e-009) {	/* |x|<2**-27 */
158 	    if(huge+x>one) return 0.5*x;/* inexact if x!=0 necessary */
159 	}
160 	z = x*x;
161 	r =  z*(r00+z*(r01+z*(r02+z*r03)));
162 	s =  one+z*(s01+z*(s02+z*(s03+z*(s04+z*s05))));
163 	r *= x;
164 	return (x*0.5+r/s);
165 }
166 
167 static double u0[5] = {
168   -1.960570906462389484206891092512047539632e-0001,
169    5.044387166398112572026169863174882070274e-0002,
170   -1.912568958757635383926261729464141209569e-0003,
171    2.352526005616105109577368905595045204577e-0005,
172    -9.190991580398788465315411784276789663849e-0008,
173 };
174 static double v0[5] = {
175    1.991673182366499064031901734535479833387e-0002,
176    2.025525810251351806268483867032781294682e-0004,
177    1.356088010975162198085369545564475416398e-0006,
178    6.227414523646214811803898435084697863445e-0009,
179    1.665592462079920695971450872592458916421e-0011,
180 };
181 
182 double y1(x)
183 	double x;
184 {
185 	double z, s, c, ss, cc, u, v;
186     /* if Y1(NaN) is NaN, Y1(-inf) is NaN, Y1(inf) is 0 */
187 	if (!finite(x))
188 		if (!_IEEE) return (infnan(EDOM));
189 		else if (x < 0)
190 			return(zero/zero);
191 		else if (x > 0)
192 			return (0);
193 		else
194 			return(x);
195 	if (x <= 0) {
196 		if (_IEEE && x == 0) return -one/zero;
197 		else if(x == 0) return(infnan(-ERANGE));
198 		else if(_IEEE) return (zero/zero);
199 		else return(infnan(EDOM));
200 	}
201         if (x >= 2)			 /* |x| >= 2.0 */
202 	{
203                 s = sin(x);
204                 c = cos(x);
205                 ss = -s-c;
206                 cc = s-c;
207 		if (x < .5 * DBL_MAX)	/* make sure x+x not overflow */
208 		{
209                     z = cos(x+x);
210                     if ((s*c)>zero) cc = z/ss;
211                     else            ss = z/cc;
212                 }
213         /* y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x0)+q1(x)*cos(x0))
214          * where x0 = x-3pi/4
215          *      Better formula:
216          *              cos(x0) = cos(x)cos(3pi/4)+sin(x)sin(3pi/4)
217          *                      =  1/sqrt(2) * (sin(x) - cos(x))
218          *              sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4)
219          *                      = -1/sqrt(2) * (cos(x) + sin(x))
220          * To avoid cancellation, use
221          *              sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
222          * to compute the worse one.
223          */
224                 if (_IEEE && x>two_129)
225 			z = (invsqrtpi*ss)/sqrt(x);
226                 else {
227                     u = pone(x); v = qone(x);
228                     z = invsqrtpi*(u*ss+v*cc)/sqrt(x);
229                 }
230                 return z;
231         }
232         if (x <= two_m54) {    /* x < 2**-54 */
233             return (-tpi/x);
234         }
235         z = x*x;
236         u = u0[0]+z*(u0[1]+z*(u0[2]+z*(u0[3]+z*u0[4])));
237         v = one+z*(v0[0]+z*(v0[1]+z*(v0[2]+z*(v0[3]+z*v0[4]))));
238         return (x*(u/v) + tpi*(j1(x)*log(x)-one/x));
239 }
240 
241 /* For x >= 8, the asymptotic expansions of pone is
242  *	1 + 15/128 s^2 - 4725/2^15 s^4 - ...,	where s = 1/x.
243  * We approximate pone by
244  * 	pone(x) = 1 + (R/S)
245  * where  R = pr0 + pr1*s^2 + pr2*s^4 + ... + pr5*s^10
246  * 	  S = 1 + ps0*s^2 + ... + ps4*s^10
247  * and
248  *	| pone(x)-1-R/S | <= 2  ** ( -60.06)
249  */
250 
251 static double pr8[6] = { /* for x in [inf, 8]=1/[0,0.125] */
252    0.0,
253    1.171874999999886486643746274751925399540e-0001,
254    1.323948065930735690925827997575471527252e+0001,
255    4.120518543073785433325860184116512799375e+0002,
256    3.874745389139605254931106878336700275601e+0003,
257    7.914479540318917214253998253147871806507e+0003,
258 };
259 static double ps8[5] = {
260    1.142073703756784104235066368252692471887e+0002,
261    3.650930834208534511135396060708677099382e+0003,
262    3.695620602690334708579444954937638371808e+0004,
263    9.760279359349508334916300080109196824151e+0004,
264    3.080427206278887984185421142572315054499e+0004,
265 };
266 
267 static double pr5[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
268    1.319905195562435287967533851581013807103e-0011,
269    1.171874931906140985709584817065144884218e-0001,
270    6.802751278684328781830052995333841452280e+0000,
271    1.083081829901891089952869437126160568246e+0002,
272    5.176361395331997166796512844100442096318e+0002,
273    5.287152013633375676874794230748055786553e+0002,
274 };
275 static double ps5[5] = {
276    5.928059872211313557747989128353699746120e+0001,
277    9.914014187336144114070148769222018425781e+0002,
278    5.353266952914879348427003712029704477451e+0003,
279    7.844690317495512717451367787640014588422e+0003,
280    1.504046888103610723953792002716816255382e+0003,
281 };
282 
283 static double pr3[6] = {/* for x in [4.547,2.8571]=1/[0.2199,0.35001] */
284    3.025039161373736032825049903408701962756e-0009,
285    1.171868655672535980750284752227495879921e-0001,
286    3.932977500333156527232725812363183251138e+0000,
287    3.511940355916369600741054592597098912682e+0001,
288    9.105501107507812029367749771053045219094e+0001,
289    4.855906851973649494139275085628195457113e+0001,
290 };
291 static double ps3[5] = {
292    3.479130950012515114598605916318694946754e+0001,
293    3.367624587478257581844639171605788622549e+0002,
294    1.046871399757751279180649307467612538415e+0003,
295    8.908113463982564638443204408234739237639e+0002,
296    1.037879324396392739952487012284401031859e+0002,
297 };
298 
299 static double pr2[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */
300    1.077108301068737449490056513753865482831e-0007,
301    1.171762194626833490512746348050035171545e-0001,
302    2.368514966676087902251125130227221462134e+0000,
303    1.224261091482612280835153832574115951447e+0001,
304    1.769397112716877301904532320376586509782e+0001,
305    5.073523125888185399030700509321145995160e+0000,
306 };
307 static double ps2[5] = {
308    2.143648593638214170243114358933327983793e+0001,
309    1.252902271684027493309211410842525120355e+0002,
310    2.322764690571628159027850677565128301361e+0002,
311    1.176793732871470939654351793502076106651e+0002,
312    8.364638933716182492500902115164881195742e+0000,
313 };
314 
315 static double pone(x)
316 	double x;
317 {
318 	double *p,*q,z,r,s;
319 	if (x >= 8.0) 			   {p = pr8; q= ps8;}
320 	else if (x >= 4.54545211791992188) {p = pr5; q= ps5;}
321 	else if (x >= 2.85714149475097656) {p = pr3; q= ps3;}
322 	else /* if (x >= 2.0) */	   {p = pr2; q= ps2;}
323 	z = one/(x*x);
324 	r = p[0]+z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5]))));
325 	s = one+z*(q[0]+z*(q[1]+z*(q[2]+z*(q[3]+z*q[4]))));
326 	return (one + r/s);
327 }
328 
329 
330 /* For x >= 8, the asymptotic expansions of qone is
331  *	3/8 s - 105/1024 s^3 - ..., where s = 1/x.
332  * We approximate pone by
333  * 	qone(x) = s*(0.375 + (R/S))
334  * where  R = qr1*s^2 + qr2*s^4 + ... + qr5*s^10
335  * 	  S = 1 + qs1*s^2 + ... + qs6*s^12
336  * and
337  *	| qone(x)/s -0.375-R/S | <= 2  ** ( -61.13)
338  */
339 
340 static double qr8[6] = { /* for x in [inf, 8]=1/[0,0.125] */
341    0.0,
342   -1.025390624999927207385863635575804210817e-0001,
343   -1.627175345445899724355852152103771510209e+0001,
344   -7.596017225139501519843072766973047217159e+0002,
345   -1.184980667024295901645301570813228628541e+0004,
346   -4.843851242857503225866761992518949647041e+0004,
347 };
348 static double qs8[6] = {
349    1.613953697007229231029079421446916397904e+0002,
350    7.825385999233484705298782500926834217525e+0003,
351    1.338753362872495800748094112937868089032e+0005,
352    7.196577236832409151461363171617204036929e+0005,
353    6.666012326177764020898162762642290294625e+0005,
354   -2.944902643038346618211973470809456636830e+0005,
355 };
356 
357 static double qr5[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
358   -2.089799311417640889742251585097264715678e-0011,
359   -1.025390502413754195402736294609692303708e-0001,
360   -8.056448281239359746193011295417408828404e+0000,
361   -1.836696074748883785606784430098756513222e+0002,
362   -1.373193760655081612991329358017247355921e+0003,
363   -2.612444404532156676659706427295870995743e+0003,
364 };
365 static double qs5[6] = {
366    8.127655013843357670881559763225310973118e+0001,
367    1.991798734604859732508048816860471197220e+0003,
368    1.746848519249089131627491835267411777366e+0004,
369    4.985142709103522808438758919150738000353e+0004,
370    2.794807516389181249227113445299675335543e+0004,
371   -4.719183547951285076111596613593553911065e+0003,
372 };
373 
374 static double qr3[6] = {/* for x in [4.547,2.8571]=1/[0.2199,0.35001] */
375   -5.078312264617665927595954813341838734288e-0009,
376   -1.025378298208370901410560259001035577681e-0001,
377   -4.610115811394734131557983832055607679242e+0000,
378   -5.784722165627836421815348508816936196402e+0001,
379   -2.282445407376317023842545937526967035712e+0002,
380   -2.192101284789093123936441805496580237676e+0002,
381 };
382 static double qs3[6] = {
383    4.766515503237295155392317984171640809318e+0001,
384    6.738651126766996691330687210949984203167e+0002,
385    3.380152866795263466426219644231687474174e+0003,
386    5.547729097207227642358288160210745890345e+0003,
387    1.903119193388108072238947732674639066045e+0003,
388   -1.352011914443073322978097159157678748982e+0002,
389 };
390 
391 static double qr2[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */
392   -1.783817275109588656126772316921194887979e-0007,
393   -1.025170426079855506812435356168903694433e-0001,
394   -2.752205682781874520495702498875020485552e+0000,
395   -1.966361626437037351076756351268110418862e+0001,
396   -4.232531333728305108194363846333841480336e+0001,
397   -2.137192117037040574661406572497288723430e+0001,
398 };
399 static double qs2[6] = {
400    2.953336290605238495019307530224241335502e+0001,
401    2.529815499821905343698811319455305266409e+0002,
402    7.575028348686454070022561120722815892346e+0002,
403    7.393932053204672479746835719678434981599e+0002,
404    1.559490033366661142496448853793707126179e+0002,
405   -4.959498988226281813825263003231704397158e+0000,
406 };
407 
408 static double qone(x)
409 	double x;
410 {
411 	double *p,*q, s,r,z;
412 	if (x >= 8.0)			   {p = qr8; q= qs8;}
413 	else if (x >= 4.54545211791992188) {p = qr5; q= qs5;}
414 	else if (x >= 2.85714149475097656) {p = qr3; q= qs3;}
415 	else /* if (x >= 2.0) */	   {p = qr2; q= qs2;}
416 	z = one/(x*x);
417 	r = p[0]+z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5]))));
418 	s = one+z*(q[0]+z*(q[1]+z*(q[2]+z*(q[3]+z*(q[4]+z*q[5])))));
419 	return (.375 + r/s)/x;
420 }
421