1 |
27 |
unneback |
//===========================================================================
|
2 |
|
|
//
|
3 |
|
|
// e_jn.c
|
4 |
|
|
//
|
5 |
|
|
// Part of the standard mathematical function library
|
6 |
|
|
//
|
7 |
|
|
//===========================================================================
|
8 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
16 |
|
|
//
|
17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
18 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
22 |
|
|
// You should have received a copy of the GNU General Public License along
|
23 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
25 |
|
|
//
|
26 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
27 |
|
|
// or inline functions from this file, or you compile this file and link it
|
28 |
|
|
// with other works to produce a work based on this file, this file does not
|
29 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
30 |
|
|
// License. However the source code for this file must still be made available
|
31 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
32 |
|
|
//
|
33 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
34 |
|
|
// this file might be covered by the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
37 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
38 |
|
|
// -------------------------------------------
|
39 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
40 |
|
|
//===========================================================================
|
41 |
|
|
//#####DESCRIPTIONBEGIN####
|
42 |
|
|
//
|
43 |
|
|
// Author(s): jlarmour
|
44 |
|
|
// Contributors: jlarmour
|
45 |
|
|
// Date: 1998-02-13
|
46 |
|
|
// Purpose:
|
47 |
|
|
// Description:
|
48 |
|
|
// Usage:
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//===========================================================================
|
53 |
|
|
|
54 |
|
|
// CONFIGURATION
|
55 |
|
|
|
56 |
|
|
#include <pkgconf/libm.h> // Configuration header
|
57 |
|
|
|
58 |
|
|
// Include the Math library?
|
59 |
|
|
#ifdef CYGPKG_LIBM
|
60 |
|
|
|
61 |
|
|
// Derived from code with the following copyright
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
/* @(#)e_jn.c 1.4 95/01/18 */
|
65 |
|
|
/*
|
66 |
|
|
* ====================================================
|
67 |
|
|
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
68 |
|
|
*
|
69 |
|
|
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
70 |
|
|
* Permission to use, copy, modify, and distribute this
|
71 |
|
|
* software is freely granted, provided that this notice
|
72 |
|
|
* is preserved.
|
73 |
|
|
* ====================================================
|
74 |
|
|
*/
|
75 |
|
|
|
76 |
|
|
/*
|
77 |
|
|
* __ieee754_jn(n, x), __ieee754_yn(n, x)
|
78 |
|
|
* floating point Bessel's function of the 1st and 2nd kind
|
79 |
|
|
* of order n
|
80 |
|
|
*
|
81 |
|
|
* Special cases:
|
82 |
|
|
* y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
|
83 |
|
|
* y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
|
84 |
|
|
* Note 2. About jn(n,x), yn(n,x)
|
85 |
|
|
* For n=0, j0(x) is called,
|
86 |
|
|
* for n=1, j1(x) is called,
|
87 |
|
|
* for n<x, forward recursion us used starting
|
88 |
|
|
* from values of j0(x) and j1(x).
|
89 |
|
|
* for n>x, a continued fraction approximation to
|
90 |
|
|
* j(n,x)/j(n-1,x) is evaluated and then backward
|
91 |
|
|
* recursion is used starting from a supposed value
|
92 |
|
|
* for j(n,x). The resulting value of j(0,x) is
|
93 |
|
|
* compared with the actual value to correct the
|
94 |
|
|
* supposed value of j(n,x).
|
95 |
|
|
*
|
96 |
|
|
* yn(n,x) is similar in all respects, except
|
97 |
|
|
* that forward recursion is used for all
|
98 |
|
|
* values of n>1.
|
99 |
|
|
*
|
100 |
|
|
*/
|
101 |
|
|
|
102 |
|
|
#include "mathincl/fdlibm.h"
|
103 |
|
|
|
104 |
|
|
static const double
|
105 |
|
|
invsqrtpi= 5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */
|
106 |
|
|
two = 2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */
|
107 |
|
|
one = 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
|
108 |
|
|
|
109 |
|
|
static double zero = 0.00000000000000000000e+00;
|
110 |
|
|
|
111 |
|
|
double __ieee754_jn(int n, double x)
|
112 |
|
|
{
|
113 |
|
|
int i,hx,ix,lx, sgn;
|
114 |
|
|
double a, b, temp, di;
|
115 |
|
|
double z, w;
|
116 |
|
|
|
117 |
|
|
/* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
|
118 |
|
|
* Thus, J(-n,x) = J(n,-x)
|
119 |
|
|
*/
|
120 |
|
|
hx = CYG_LIBM_HI(x);
|
121 |
|
|
ix = 0x7fffffff&hx;
|
122 |
|
|
lx = CYG_LIBM_LO(x);
|
123 |
|
|
/* if J(n,NaN) is NaN */
|
124 |
|
|
if((ix|((unsigned)(lx|-lx))>>31)>0x7ff00000) return x+x;
|
125 |
|
|
if(n<0){
|
126 |
|
|
n = -n;
|
127 |
|
|
x = -x;
|
128 |
|
|
hx ^= 0x80000000;
|
129 |
|
|
}
|
130 |
|
|
if(n==0) return(__ieee754_j0(x));
|
131 |
|
|
if(n==1) return(__ieee754_j1(x));
|
132 |
|
|
sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */
|
133 |
|
|
x = fabs(x);
|
134 |
|
|
if((ix|lx)==0||ix>=0x7ff00000) /* if x is 0 or inf */
|
135 |
|
|
b = zero;
|
136 |
|
|
else if((double)n<=x) {
|
137 |
|
|
/* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
|
138 |
|
|
if(ix>=0x52D00000) { /* x > 2**302 */
|
139 |
|
|
/* (x >> n**2)
|
140 |
|
|
* Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
|
141 |
|
|
* Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
|
142 |
|
|
* Let s=sin(x), c=cos(x),
|
143 |
|
|
* xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
|
144 |
|
|
*
|
145 |
|
|
* n sin(xn)*sqt2 cos(xn)*sqt2
|
146 |
|
|
* ----------------------------------
|
147 |
|
|
* 0 s-c c+s
|
148 |
|
|
* 1 -s-c -c+s
|
149 |
|
|
* 2 -s+c -c-s
|
150 |
|
|
* 3 s+c c-s
|
151 |
|
|
*/
|
152 |
|
|
switch(n&3) {
|
153 |
|
|
case 0: temp = cos(x)+sin(x); break;
|
154 |
|
|
case 1: temp = -cos(x)+sin(x); break;
|
155 |
|
|
case 2: temp = -cos(x)-sin(x); break;
|
156 |
|
|
case 3: temp = cos(x)-sin(x); break;
|
157 |
|
|
default: temp = 0.0; break; /* not used - purely to
|
158 |
|
|
* placate compiler */
|
159 |
|
|
}
|
160 |
|
|
b = invsqrtpi*temp/sqrt(x);
|
161 |
|
|
} else {
|
162 |
|
|
a = __ieee754_j0(x);
|
163 |
|
|
b = __ieee754_j1(x);
|
164 |
|
|
for(i=1;i<n;i++){
|
165 |
|
|
temp = b;
|
166 |
|
|
b = b*((double)(i+i)/x) - a; /* avoid underflow */
|
167 |
|
|
a = temp;
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
} else {
|
171 |
|
|
if(ix<0x3e100000) { /* x < 2**-29 */
|
172 |
|
|
/* x is tiny, return the first Taylor expansion of J(n,x)
|
173 |
|
|
* J(n,x) = 1/n!*(x/2)^n - ...
|
174 |
|
|
*/
|
175 |
|
|
if(n>33) /* underflow */
|
176 |
|
|
b = zero;
|
177 |
|
|
else {
|
178 |
|
|
temp = x*0.5; b = temp;
|
179 |
|
|
for (a=one,i=2;i<=n;i++) {
|
180 |
|
|
a *= (double)i; /* a = n! */
|
181 |
|
|
b *= temp; /* b = (x/2)^n */
|
182 |
|
|
}
|
183 |
|
|
b = b/a;
|
184 |
|
|
}
|
185 |
|
|
} else {
|
186 |
|
|
/* use backward recurrence */
|
187 |
|
|
/* x x^2 x^2
|
188 |
|
|
* J(n,x)/J(n-1,x) = ---- ------ ------ .....
|
189 |
|
|
* 2n - 2(n+1) - 2(n+2)
|
190 |
|
|
*
|
191 |
|
|
* 1 1 1
|
192 |
|
|
* (for large x) = ---- ------ ------ .....
|
193 |
|
|
* 2n 2(n+1) 2(n+2)
|
194 |
|
|
* -- - ------ - ------ -
|
195 |
|
|
* x x x
|
196 |
|
|
*
|
197 |
|
|
* Let w = 2n/x and h=2/x, then the above quotient
|
198 |
|
|
* is equal to the continued fraction:
|
199 |
|
|
* 1
|
200 |
|
|
* = -----------------------
|
201 |
|
|
* 1
|
202 |
|
|
* w - -----------------
|
203 |
|
|
* 1
|
204 |
|
|
* w+h - ---------
|
205 |
|
|
* w+2h - ...
|
206 |
|
|
*
|
207 |
|
|
* To determine how many terms needed, let
|
208 |
|
|
* Q(0) = w, Q(1) = w(w+h) - 1,
|
209 |
|
|
* Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
|
210 |
|
|
* When Q(k) > 1e4 good for single
|
211 |
|
|
* When Q(k) > 1e9 good for double
|
212 |
|
|
* When Q(k) > 1e17 good for quadruple
|
213 |
|
|
*/
|
214 |
|
|
/* determine k */
|
215 |
|
|
double t,v;
|
216 |
|
|
double q0,q1,h,tmp; int k,m;
|
217 |
|
|
w = (n+n)/(double)x; h = 2.0/(double)x;
|
218 |
|
|
q0 = w; z = w+h; q1 = w*z - 1.0; k=1;
|
219 |
|
|
while(q1<1.0e9) {
|
220 |
|
|
k += 1; z += h;
|
221 |
|
|
tmp = z*q1 - q0;
|
222 |
|
|
q0 = q1;
|
223 |
|
|
q1 = tmp;
|
224 |
|
|
}
|
225 |
|
|
m = n+n;
|
226 |
|
|
for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
|
227 |
|
|
a = t;
|
228 |
|
|
b = one;
|
229 |
|
|
/* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
|
230 |
|
|
* Hence, if n*(log(2n/x)) > ...
|
231 |
|
|
* single 8.8722839355e+01
|
232 |
|
|
* double 7.09782712893383973096e+02
|
233 |
|
|
* long double 1.1356523406294143949491931077970765006170e+04
|
234 |
|
|
* then recurrent value may overflow and the result is
|
235 |
|
|
* likely underflow to zero
|
236 |
|
|
*/
|
237 |
|
|
tmp = n;
|
238 |
|
|
v = two/x;
|
239 |
|
|
tmp = tmp*__ieee754_log(fabs(v*tmp));
|
240 |
|
|
if(tmp<7.09782712893383973096e+02) {
|
241 |
|
|
for(i=n-1,di=(double)(i+i);i>0;i--){
|
242 |
|
|
temp = b;
|
243 |
|
|
b *= di;
|
244 |
|
|
b = b/x - a;
|
245 |
|
|
a = temp;
|
246 |
|
|
di -= two;
|
247 |
|
|
}
|
248 |
|
|
} else {
|
249 |
|
|
for(i=n-1,di=(double)(i+i);i>0;i--){
|
250 |
|
|
temp = b;
|
251 |
|
|
b *= di;
|
252 |
|
|
b = b/x - a;
|
253 |
|
|
a = temp;
|
254 |
|
|
di -= two;
|
255 |
|
|
/* scale b to avoid spurious overflow */
|
256 |
|
|
if(b>1e100) {
|
257 |
|
|
a /= b;
|
258 |
|
|
t /= b;
|
259 |
|
|
b = one;
|
260 |
|
|
}
|
261 |
|
|
}
|
262 |
|
|
}
|
263 |
|
|
b = (t*__ieee754_j0(x)/b);
|
264 |
|
|
}
|
265 |
|
|
}
|
266 |
|
|
if(sgn==1) return -b; else return b;
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
double __ieee754_yn(int n, double x)
|
270 |
|
|
{
|
271 |
|
|
int i,hx,ix,lx;
|
272 |
|
|
int sign;
|
273 |
|
|
double a, b, temp;
|
274 |
|
|
|
275 |
|
|
hx = CYG_LIBM_HI(x);
|
276 |
|
|
ix = 0x7fffffff&hx;
|
277 |
|
|
lx = CYG_LIBM_LO(x);
|
278 |
|
|
/* if Y(n,NaN) is NaN */
|
279 |
|
|
if((ix|((unsigned)(lx|-lx))>>31)>0x7ff00000) return x+x;
|
280 |
|
|
if((ix|lx)==0) return -one/zero;
|
281 |
|
|
if(hx<0) return zero/zero;
|
282 |
|
|
sign = 1;
|
283 |
|
|
if(n<0){
|
284 |
|
|
n = -n;
|
285 |
|
|
sign = 1 - ((n&1)<<1);
|
286 |
|
|
}
|
287 |
|
|
if(n==0) return(__ieee754_y0(x));
|
288 |
|
|
if(n==1) return(sign*__ieee754_y1(x));
|
289 |
|
|
if(ix==0x7ff00000) return zero;
|
290 |
|
|
if(ix>=0x52D00000) { /* x > 2**302 */
|
291 |
|
|
/* (x >> n**2)
|
292 |
|
|
* Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
|
293 |
|
|
* Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
|
294 |
|
|
* Let s=sin(x), c=cos(x),
|
295 |
|
|
* xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
|
296 |
|
|
*
|
297 |
|
|
* n sin(xn)*sqt2 cos(xn)*sqt2
|
298 |
|
|
* ----------------------------------
|
299 |
|
|
* 0 s-c c+s
|
300 |
|
|
* 1 -s-c -c+s
|
301 |
|
|
* 2 -s+c -c-s
|
302 |
|
|
* 3 s+c c-s
|
303 |
|
|
*/
|
304 |
|
|
switch(n&3) {
|
305 |
|
|
case 0: temp = sin(x)-cos(x); break;
|
306 |
|
|
case 1: temp = -sin(x)-cos(x); break;
|
307 |
|
|
case 2: temp = -sin(x)+cos(x); break;
|
308 |
|
|
case 3: temp = sin(x)+cos(x); break;
|
309 |
|
|
default: temp = 0.0; break; /* not used - purely to
|
310 |
|
|
* placate compiler */
|
311 |
|
|
}
|
312 |
|
|
b = invsqrtpi*temp/sqrt(x);
|
313 |
|
|
} else {
|
314 |
|
|
a = __ieee754_y0(x);
|
315 |
|
|
b = __ieee754_y1(x);
|
316 |
|
|
/* quit if b is -inf */
|
317 |
|
|
for(i=1;i<n&&((unsigned)CYG_LIBM_HI(b) != 0xfff00000);i++){
|
318 |
|
|
temp = b;
|
319 |
|
|
b = ((double)(i+i)/x)*b - a;
|
320 |
|
|
a = temp;
|
321 |
|
|
}
|
322 |
|
|
}
|
323 |
|
|
if(sign>0) return b; else return -b;
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
#endif // ifdef CYGPKG_LIBM
|
327 |
|
|
|
328 |
|
|
// EOF e_jn.c
|