Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4973 right-hear 1
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
2
/* @(#)e_jn.c 5.1 93/09/24 */
3
/*
4
 * ====================================================
5
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6
 *
7
 * Developed at SunPro, a Sun Microsystems, Inc. business.
8
 * Permission to use, copy, modify, and distribute this
9
 * software is freely granted, provided that this notice
10
 * is preserved.
11
 * ====================================================
12
 */
13
 
14
#if defined(LIBM_SCCS) && !defined(lint)
15
static char rcsid[] = "$Id: e_jn.c,v 1.6 1994/08/18 23:05:37 jtc Exp $";
16
#endif
17
 
18
/*
19
 * __ieee754_jn(n, x), __ieee754_yn(n, x)
20
 * floating point Bessel's function of the 1st and 2nd kind
21
 * of order n
22
 *
23
 * Special cases:
24
 *	y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
25
 *	y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
26
 * Note 2. About jn(n,x), yn(n,x)
27
 *	For n=0, j0(x) is called,
28
 *	for n=1, j1(x) is called,
29
 *	for n
30
 *	from values of j0(x) and j1(x).
31
 *	for n>x, a continued fraction approximation to
32
 *	j(n,x)/j(n-1,x) is evaluated and then backward
33
 *	recursion is used starting from a supposed value
34
 *	for j(n,x). The resulting value of j(0,x) is
35
 *	compared with the actual value to correct the
36
 *	supposed value of j(n,x).
37
 *
38
 *	yn(n,x) is similar in all respects, except
39
 *	that forward recursion is used for all
40
 *	values of n>1.
41
 *
42
 */
43
 
44
#include "math.h"
45
#include "math_private.h"
46
 
47
#ifdef __STDC__
48
static const double
49
#else
50
static double
51
#endif
52
invsqrtpi=  5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */
53
two   =  2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */
54
one   =  1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
55
 
56
#ifdef __STDC__
57
static const double zero  =  0.00000000000000000000e+00;
58
#else
59
static double zero  =  0.00000000000000000000e+00;
60
#endif
61
 
62
#ifdef __STDC__
63
	double __ieee754_jn(int n, double x)
64
#else
65
	double __ieee754_jn(n,x)
66
	int n; double x;
67
#endif
68
{
69
	int32_t i,hx,ix,lx, sgn;
70
	double a, b, temp, di;
71
	double z, w;
72
 
73
    /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
74
     * Thus, J(-n,x) = J(n,-x)
75
     */
76
	EXTRACT_WORDS(hx,lx,x);
77
	ix = 0x7fffffff&hx;
78
    /* if J(n,NaN) is NaN */
79
	if((ix|((u_int32_t)(lx|-lx))>>31)>0x7ff00000) return x+x;
80
	if(n<0){
81
		n = -n;
82
		x = -x;
83
		hx ^= 0x80000000;
84
	}
85
	if(n==0) return(__ieee754_j0(x));
86
	if(n==1) return(__ieee754_j1(x));
87
	sgn = (n&1)&(hx>>31);	/* even n -- 0, odd n -- sign(x) */
88
	x = fabs(x);
89
	if((ix|lx)==0||ix>=0x7ff00000) 	/* if x is 0 or inf */
90
	    b = zero;
91
	else if((double)n<=x) {
92
		/* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
93
	    if(ix>=0x52D00000) { /* x > 2**302 */
94
    /* (x >> n**2)
95
     *	    Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
96
     *	    Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
97
     *	    Let s=sin(x), c=cos(x),
98
     *		xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
99
     *
100
     *		   n	sin(xn)*sqt2	cos(xn)*sqt2
101
     *		----------------------------------
102
     *		   0	 s-c		 c+s
103
     *		   1	-s-c 		-c+s
104
     *		   2	-s+c		-c-s
105
     *		   3	 s+c		 c-s
106
     */
107
		switch(n&3) {
108
		    case 0: temp =  cos(x)+sin(x); break;
109
		    case 1: temp = -cos(x)+sin(x); break;
110
		    case 2: temp = -cos(x)-sin(x); break;
111
		    case 3: temp =  cos(x)-sin(x); break;
112
		}
113
		b = invsqrtpi*temp/sqrt(x);
114
	    } else {
115
	        a = __ieee754_j0(x);
116
	        b = __ieee754_j1(x);
117
	        for(i=1;i
118
		    temp = b;
119
		    b = b*((double)(i+i)/x) - a; /* avoid underflow */
120
		    a = temp;
121
	        }
122
	    }
123
	} else {
124
	    if(ix<0x3e100000) {	/* 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  - ...
127
     */
128
		if(n>33)	/* underflow */
129
		    b = zero;
130
		else {
131
		    temp = x*0.5; b = temp;
132
		    for (a=one,i=2;i<=n;i++) {
133
			a *= (double)i;		/* a = n! */
134
			b *= temp;		/* b = (x/2)^n */
135
		    }
136
		    b = b/a;
137
		}
138
	    } else {
139
		/* use backward recurrence */
140
		/* 			x      x^2      x^2
141
		 *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
142
		 *			2n  - 2(n+1) - 2(n+2)
143
		 *
144
		 * 			1      1        1
145
		 *  (for large x)   =  ----  ------   ------   .....
146
		 *			2n   2(n+1)   2(n+2)
147
		 *			-- - ------ - ------ -
148
		 *			 x     x         x
149
		 *
150
		 * Let w = 2n/x and h=2/x, then the above quotient
151
		 * is equal to the continued fraction:
152
		 *		    1
153
		 *	= -----------------------
154
		 *		       1
155
		 *	   w - -----------------
156
		 *			  1
157
		 * 	        w+h - ---------
158
		 *		       w+2h - ...
159
		 *
160
		 * To determine how many terms needed, let
161
		 * Q(0) = w, Q(1) = w(w+h) - 1,
162
		 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
163
		 * When Q(k) > 1e4	good for single
164
		 * When Q(k) > 1e9	good for double
165
		 * When Q(k) > 1e17	good for quadruple
166
		 */
167
	    /* determine k */
168
		double t,v;
169
		double q0,q1,h,tmp; int32_t k,m;
170
		w  = (n+n)/(double)x; h = 2.0/(double)x;
171
		q0 = w;  z = w+h; q1 = w*z - 1.0; k=1;
172
		while(q1<1.0e9) {
173
			k += 1; z += h;
174
			tmp = z*q1 - q0;
175
			q0 = q1;
176
			q1 = tmp;
177
		}
178
		m = n+n;
179
		for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
180
		a = t;
181
		b = one;
182
		/*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
183
		 *  Hence, if n*(log(2n/x)) > ...
184
		 *  single 8.8722839355e+01
185
		 *  double 7.09782712893383973096e+02
186
		 *  long double 1.1356523406294143949491931077970765006170e+04
187
		 *  then recurrent value may overflow and the result is
188
		 *  likely underflow to zero
189
		 */
190
		tmp = n;
191
		v = two/x;
192
		tmp = tmp*__ieee754_log(fabs(v*tmp));
193
		if(tmp<7.09782712893383973096e+02) {
194
	    	    for(i=n-1,di=(double)(i+i);i>0;i--){
195
		        temp = b;
196
			b *= di;
197
			b  = b/x - a;
198
		        a = temp;
199
			di -= two;
200
	     	    }
201
		} else {
202
	    	    for(i=n-1,di=(double)(i+i);i>0;i--){
203
		        temp = b;
204
			b *= di;
205
			b  = b/x - a;
206
		        a = temp;
207
			di -= two;
208
		    /* scale b to avoid spurious overflow */
209
			if(b>1e100) {
210
			    a /= b;
211
			    t /= b;
212
			    b  = one;
213
			}
214
	     	    }
215
		}
216
	    	b = (t*__ieee754_j0(x)/b);
217
	    }
218
	}
219
	if(sgn==1) return -b; else return b;
220
}
221
 
222
#ifdef __STDC__
223
	double __ieee754_yn(int n, double x)
224
#else
225
	double __ieee754_yn(n,x)
226
	int n; double x;
227
#endif
228
{
229
	int32_t i,hx,ix,lx;
230
	int32_t sign;
231
	double a, b, temp;
232
 
233
	EXTRACT_WORDS(hx,lx,x);
234
	ix = 0x7fffffff&hx;
235
    /* if Y(n,NaN) is NaN */
236
	if((ix|((u_int32_t)(lx|-lx))>>31)>0x7ff00000) return x+x;
237
	if((ix|lx)==0) return -one/zero;
238
	if(hx<0) return zero/zero;
239
	sign = 1;
240
	if(n<0){
241
		n = -n;
242
		sign = 1 - ((n&1)<<2);
243
	}
244
	if(n==0) return(__ieee754_y0(x));
245
	if(n==1) return(sign*__ieee754_y1(x));
246
	if(ix==0x7ff00000) return zero;
247
	if(ix>=0x52D00000) { /* x > 2**302 */
248
    /* (x >> n**2)
249
     *	    Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
250
     *	    Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
251
     *	    Let s=sin(x), c=cos(x),
252
     *		xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
253
     *
254
     *		   n	sin(xn)*sqt2	cos(xn)*sqt2
255
     *		----------------------------------
256
     *		   0	 s-c		 c+s
257
     *		   1	-s-c 		-c+s
258
     *		   2	-s+c		-c-s
259
     *		   3	 s+c		 c-s
260
     */
261
		switch(n&3) {
262
		    case 0: temp =  sin(x)-cos(x); break;
263
		    case 1: temp = -sin(x)-cos(x); break;
264
		    case 2: temp = -sin(x)+cos(x); break;
265
		    case 3: temp =  sin(x)+cos(x); break;
266
		}
267
		b = invsqrtpi*temp/sqrt(x);
268
	} else {
269
	    u_int32_t high;
270
	    a = __ieee754_y0(x);
271
	    b = __ieee754_y1(x);
272
	/* quit if b is -inf */
273
	    GET_HIGH_WORD(high,b);
274
	    for(i=1;i
275
		temp = b;
276
		b = ((double)(i+i)/x)*b - a;
277
		GET_HIGH_WORD(high,b);
278
		a = temp;
279
	    }
280
	}
281
	if(sign>0) return b; else return -b;
282
}