home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2330 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  54.9 KB

  1. From: glenn@qed.physics.su.OZ.AU (Glenn Geers)
  2. Newsgroups: alt.sources
  3. Subject: 80386 alternative math lib v2.0 part01/06
  4. Message-ID: <1990Dec16.205923.27650@metro.ucc.su.OZ.AU>
  5. Date: 16 Dec 90 20:59:23 GMT
  6.  
  7. Submitted-by: root@trantor
  8. Archive-name: mathlib2.0/part01
  9.  
  10. ---- Cut Here and feed the following to sh ----
  11. #!/bin/sh
  12. # This is mathlib2.0, a shell archive (shar 3.47)
  13. # made 12/14/1990 06:28 UTC by root@trantor
  14. # Source directory /usr1/src/math/dist
  15. #
  16. # existing files will NOT be overwritten unless -c is specified
  17. #
  18. # This is part 1 of a multipart archive                                    
  19. # do not concatenate these parts, unpack them in order with /bin/sh        
  20. #
  21. # This shar contains:
  22. # length  mode       name
  23. # ------ ---------- ------------------------------------------
  24. #  12774 -rw-r--r-- j0.c
  25. #   8438 -rw-r--r-- lgamma.c
  26. #   7210 -rw-r--r-- gamma.c
  27. #  12086 -rw-r--r-- j1.c
  28. #   9681 -rw-r--r-- erf.c
  29. #   1725 -rw-r--r-- nextafter.c
  30. #  12458 -rw-r--r-- j0f.c
  31. #   8280 -rw-r--r-- lgammaf.c
  32. #   6996 -rw-r--r-- gammaf.c
  33. #  11769 -rw-r--r-- j1f.c
  34. #   9376 -rw-r--r-- erff.c
  35. #   1717 -rw-r--r-- nextafterf.c
  36. #    544 -rw-r--r-- acos.s
  37. #    555 -rw-r--r-- copysign.s
  38. #    381 -rw-r--r-- drem.s
  39. #    322 -rw-r--r-- fabs.s
  40. #    377 -rw-r--r-- hypot.s
  41. #    339 -rw-r--r-- logb.s
  42. #    343 -rw-r--r-- scalb.s
  43. #    334 -rw-r--r-- tan.s
  44. #    401 -rw-r--r-- asin.s
  45. #    682 -rw-r--r-- ceil.s
  46. #    400 -rw-r--r-- exp.s
  47. #    573 -rw-r--r-- expm1.s
  48. #    447 -rw-r--r-- finite.s
  49. #    329 -rw-r--r-- log.s
  50. #    346 -rw-r--r-- log1p.s
  51. #   2128 -rw-r--r-- pow.s
  52. #    320 -rw-r--r-- sin.s
  53. #    331 -rw-r--r-- atan.s
  54. #    320 -rw-r--r-- cos.s
  55. #    404 -rw-r--r-- exp10.s
  56. #    628 -rw-r--r-- floor.s
  57. #    333 -rw-r--r-- log10.s
  58. #    326 -rw-r--r-- rint.s
  59. #    359 -rw-r--r-- sqrt.s
  60. #    387 -rw-r--r-- exp2.s
  61. #    329 -rw-r--r-- log2.s
  62. #   1942 -rw-r--r-- sinh.s
  63. #    533 -rw-r--r-- cosh.s
  64. #    493 -rw-r--r-- tanh.s
  65. #    397 -rw-r--r-- asinh.s
  66. #    398 -rw-r--r-- acosh.s
  67. #    438 -rw-r--r-- atanh.s
  68. #    931 -rw-r--r-- atan2.s
  69. #    380 -rw-r--r-- fmod.s
  70. #   1994 -rw-r--r-- ieee_ext.s
  71. #    394 -rw-r--r-- infinity.s
  72. #    605 -rw-r--r-- sqrtp.s
  73. #   1295 -rw-r--r-- ieee_values.s
  74. #    546 -rw-r--r-- acosf.s
  75. #    555 -rw-r--r-- copysignf.s
  76. #    324 -rw-r--r-- fabsf.s
  77. #    335 -rw-r--r-- log10f.s
  78. #    322 -rw-r--r-- sinf.s
  79. #    400 -rw-r--r-- acoshf.s
  80. #    322 -rw-r--r-- cosf.s
  81. #    448 -rw-r--r-- finitef.s
  82. #    348 -rw-r--r-- log1pf.s
  83. #   2071 -rw-r--r-- sinhf.s
  84. #    403 -rw-r--r-- asinf.s
  85. #    535 -rw-r--r-- coshf.s
  86. #    630 -rw-r--r-- floorf.s
  87. #    331 -rw-r--r-- log2f.s
  88. #    361 -rw-r--r-- sqrtf.s
  89. #    399 -rw-r--r-- asinhf.s
  90. #    381 -rw-r--r-- dremf.s
  91. #    382 -rw-r--r-- fmodf.s
  92. #    341 -rw-r--r-- logbf.s
  93. #    607 -rw-r--r-- sqrtpf.s
  94. #    933 -rw-r--r-- atan2f.s
  95. #    406 -rw-r--r-- exp10f.s
  96. #    379 -rw-r--r-- hypotf.s
  97. #    331 -rw-r--r-- logf.s
  98. #    336 -rw-r--r-- tanf.s
  99. #    333 -rw-r--r-- atanf.s
  100. #    389 -rw-r--r-- exp2f.s
  101. #   1738 -rw-r--r-- ieee_extf.s
  102. #   1850 -rw-r--r-- powf.s
  103. #    495 -rw-r--r-- tanhf.s
  104. #    440 -rw-r--r-- atanhf.s
  105. #    402 -rw-r--r-- expf.s
  106. #   1128 -rw-r--r-- ieee_valuesf.s
  107. #    328 -rw-r--r-- rintf.s
  108. #    684 -rw-r--r-- ceilf.s
  109. #    573 -rw-r--r-- expm1f.s
  110. #    372 -rw-r--r-- infinityf.s
  111. #    345 -rw-r--r-- scalbf.s
  112. #    879 -rw-r--r-- ieee_retro.c
  113. #    320 -rw-r--r-- _getsw.s
  114. #    377 -rw-r--r-- setcont.s
  115. #    449 -rw-r--r-- setinternal.s
  116. #  57395 -rw-r--r-- paranoia.c
  117. #   5231 -rw-r--r-- CHANGELOG
  118. #  12488 -rw-r--r-- COPYING
  119. #    504 -rw-r--r-- COPYRIGHT
  120. #   3879 -rw-r--r-- Makefile
  121. #    557 -rw-r--r-- PROBLEMS
  122. #  11151 -rw-r--r-- README
  123. #    204 -rw-r--r-- TODO
  124. #   3424 -rw-r--r-- d2dcomb.summ
  125. #   5436 -rw-r--r-- fpumath.h
  126. #
  127. if test -r _shar_seq_.tmp; then
  128.     echo 'Must unpack archives in sequence!'
  129.     echo Please unpack part `cat _shar_seq_.tmp` next
  130.     exit 1
  131. fi
  132. # ============= j0.c ==============
  133. if test -f 'j0.c' -a X"$1" != X"-c"; then
  134.     echo 'x - skipping j0.c (File already exists)'
  135.     rm -f _shar_wnt_.tmp
  136. else
  137. > _shar_wnt_.tmp
  138. echo 'x - extracting j0.c (Text)'
  139. sed 's/^X//' << 'SHAR_EOF' > 'j0.c' &&
  140. #ifndef lint
  141. static char sccsid[] = "@(#)j0.c    1.2    (ucb.beef)    10/2/89";
  142. #endif    /* !defined(lint) */
  143. /* 
  144. X *  This packet computes zero-order Bessel functions of the first and
  145. X *    second kind (j0 and y0), for real arguments x, where 0 < x <= XMAX
  146. X *    for y0, and |x| <= XMAX for j0.  It contains three function-type
  147. X *    subprograms, j0, y0 and caljy0.  The calling statements for the
  148. X *    primary entries are:
  149. X * 
  150. X *            y = j0(x)
  151. X *    and
  152. X *            y = y0(x),
  153. X * 
  154. X *    where the entry points correspond to the functions j0(x) and y0(x),
  155. X *    respectively.  The routine caljy0() is intended for internal packet
  156. X *    use only, all computations within the packet being concentrated in
  157. X *    this one routine.  The function subprograms invoke  caljy0  with
  158. X *    the statement
  159. X *
  160. X *            result = caljy0(x,jint);
  161. X *
  162. X *    where the parameter usage is as follows:
  163. X * 
  164. X *       Function                  Parameters for caljy0
  165. X *        call              x             result          jint
  166. X * 
  167. X *       j0(x)        |x| <= XMAX         j0(x)           0
  168. X *       y0(x)      0 < x <= XMAX         y0(x)           1
  169. X * 
  170. X *    The main computation uses unpublished minimax rational
  171. X *    approximations for x <= 8.0, and an approximation from the 
  172. X *    book  Computer Approximations  by Hart, et. al., Wiley and Sons, 
  173. X *    New York, 1968, for arguments larger than 8.0   Part of this
  174. X *    transportable packet is patterned after the machine-dependent
  175. X *    FUNPACK program for j0(x), but cannot match that version for
  176. X *    efficiency or accuracy.  This version uses rational functions
  177. X *    that are theoretically accurate to at least 18 significant decimal
  178. X *    digits for x <= 8, and at least 18 decimal places for x > 8.  The
  179. X *    accuracy achieved depends on the arithmetic system, the compiler,
  180. X *    the intrinsic functions, and proper selection of the machine-
  181. X *    dependent constants.
  182. X * 
  183. X *********************************************************************
  184. X * 
  185. X *  Explanation of machine-dependent constants
  186. X * 
  187. X *    XINF   = largest positive machine number
  188. X *    XMAX   = largest acceptable argument.  The functions sin(), floor()
  189. X *             and cos() must perform properly for  fabs(x) <= XMAX.
  190. X *             We recommend that XMAX be a small integer multiple of
  191. X *             sqrt(1/eps), where eps is the smallest positive number
  192. X *             such that  1+eps > 1. 
  193. X *    XSMALL = positive argument such that  1.0-(x/2)**2 = 1.0
  194. X *             to machine precision for all  fabs(x) <= XSMALL.
  195. X *             We recommend that  XSMALL < sqrt(eps)/beta, where beta
  196. X *             is the floating-point radix (usually 2 or 16).
  197. X * 
  198. X *      Approximate values for some important machines are
  199. X * 
  200. X *                           eps      XMAX     XSMALL      XINF  
  201. X * 
  202. X *   CDC 7600      (S.P.)  7.11E-15  1.34E+08  2.98E-08  1.26E+322
  203. X *   CRAY-1        (S.P.)  7.11E-15  1.34E+08  2.98E-08  5.45E+2465
  204. X *   IBM PC (8087) (S.P.)  5.96E-08  8.19E+03  1.22E-04  3.40E+38
  205. X *   IBM PC (8087) (D.P.)  1.11D-16  2.68D+08  3.72D-09  1.79D+308
  206. X *   IBM 195       (D.P.)  2.22D-16  6.87D+09  9.09D-13  7.23D+75
  207. X *   UNIVAC 1108   (D.P.)  1.73D-18  4.30D+09  2.33D-10  8.98D+307
  208. X *   VAX 11/780    (D.P.)  1.39D-17  1.07D+09  9.31D-10  1.70D+38
  209. X * 
  210. X *********************************************************************
  211. X *********************************************************************
  212. X * 
  213. X *  Error Returns
  214. X * 
  215. X *   The program returns the value zero for  x > XMAX, and returns
  216. X *     -XINF when y0 is called with a negative or zero argument.
  217. X * 
  218. X * 
  219. X *  Intrinsic functions required are:
  220. X * 
  221. X *      fabs, floor, cos, log, sin, sqrt
  222. X * 
  223. X *
  224. X *   Latest modification: June 2, 1989
  225. X * 
  226. X *   Author: W. J. Cody
  227. X *           Mathematics and Computer Science Division 
  228. X *           Argonne National Laboratory
  229. X *           Argonne, IL 60439
  230. X */
  231. #if defined(__STDC__) || defined(__GNUC__)
  232. extern double cos(double),fabs(double),floor(double),
  233. X    log(double),sin(double),sqrt(double);
  234. #else
  235. extern double cos(),fabs(),floor(),log(),sin(),sqrt();
  236. #endif
  237. X                    /* Machine-dependent constants */
  238. #if defined(vax) || defined(tahoe)
  239. #define    XMAX    (double)1.07e9
  240. #define    XSMALL    (double)9.31e-10
  241. #define    XINF    (double)1.70e38
  242. #else    /* defined(vax) || defined(tahoe) */
  243. #define    XMAX    (double)2.68e8
  244. #define    XSMALL    (double)3.72e-9
  245. #define    XINF    (double)1.79e308
  246. #endif    /* defined(vax) || defined(tahoe) */
  247. X                    /* Mathematical constants */
  248. #define    EIGHT    (double)8
  249. #define    CONS    (double)(-1.1593151565841244881e-1) /* ln(.5)+Euler's gamma */
  250. #define    FIVE5    (double)5.5
  251. #define    FOUR    (double)4
  252. #define    ONE    (double)1
  253. #define    ONEOV8    (double)0.125
  254. #define    PI2    (double)6.3661977236758134308e-1
  255. #define    P17    (double)1.716e-1
  256. #define    SIXTY4    (double)64
  257. #define    THREE    (double)3
  258. #define    TWOPI    (double)6.2831853071795864769e0
  259. #define    TWOPI1    (double)6.28125
  260. #define    TWOPI2    (double)1.9353071795864769253e-03
  261. #define    TWO56    (double)256
  262. #define    ZERO    (double)0
  263. X                    /* Zeroes of Bessel functions */
  264. #define    XJ0    (double)2.4048255576957727686e0
  265. #define    XJ1    (double)5.5200781102863106496e0
  266. #define    XY0    (double)8.9357696627916752158e-1
  267. #define    XY1    (double)3.9576784193148578684e0
  268. #define    XY2    (double)7.0860510603017726976e0
  269. #define    XJ01    (double)616
  270. #define    XJ02    (double)(-1.4244423042272313784e-3)
  271. #define    XJ11    (double)1413
  272. #define    XJ12    (double)5.4686028631064959660e-4
  273. #define    XY01    (double)228.0
  274. #define    XY02    (double)2.9519662791675215849e-3
  275. #define    XY11    (double)1013
  276. #define    XY12    (double)6.4716931485786837568e-4
  277. #define    XY21    (double)1814
  278. #define    XY22    (double)1.1356030177269762362e-4
  279. X
  280. /*
  281. X * Coefficients for rational approximation to ln(x/a)
  282. X */
  283. static double PLG[] = {
  284. X    -2.4562334077563243311e01,
  285. X     2.3642701335621505212e02,
  286. X    -5.4989956895857911039e02,
  287. X     3.5687548468071500413e02,
  288. };
  289. static double QLG[] = {
  290. X    -3.5553900764052419184e01,
  291. X     1.9400230218539473193e02,
  292. X    -3.3442903192607538956e02,
  293. X     1.7843774234035750207e02,
  294. };
  295. X
  296. /*
  297. X * Coefficients for rational approximation of
  298. X * j0(x) / (x**2 - XJ0**2),  XSMALL  <  |x|  <=  4.0
  299. X */
  300. static double PJ0[] = {
  301. X     6.6302997904833794242e06,
  302. X    -6.2140700423540120665e08,
  303. X     2.7282507878605942706e10,
  304. X    -4.1298668500990866786e11,
  305. X    -1.2117036164593528341e-01,
  306. X     1.0344222815443188943e02,
  307. X    -3.6629814655107086448e04,
  308. };
  309. static double QJ0[] = {
  310. X    4.5612696224219938200e05,
  311. X    1.3985097372263433271e08,
  312. X    2.6328198300859648632e10,
  313. X    2.3883787996332290397e12,
  314. X    9.3614022392337710626e02,
  315. };
  316. X
  317. /*
  318. X * Coefficients for rational approximation of
  319. X * j0(x) / (x**2 - XJ1**2),  4.0  <  |x|  <=  8.0
  320. X */
  321. static double PJ1[] = {
  322. X     4.4176707025325087628e03,
  323. X     1.1725046279757103576e04,
  324. X     1.0341910641583726701e04,
  325. X    -7.2879702464464618998e03,
  326. X    -1.2254078161378989535e04,
  327. X    -1.8319397969392084011e03,
  328. X     4.8591703355916499363e01,
  329. X     7.4321196680624245801e02,
  330. };
  331. static double QJ1[] = {
  332. X     3.3307310774649071172e02,
  333. X    -2.9458766545509337327e03,
  334. X     1.8680990008359188352e04,
  335. X    -8.4055062591169562211e04,
  336. X     2.4599102262586308984e05,
  337. X    -3.5783478026152301072e05,
  338. X    -2.5258076240801555057e01,
  339. };
  340. X
  341. /*
  342. X * Coefficients for rational approximation of
  343. X *   (y0(x) - 2 LN(x/XY0) j0(x)) / (x**2 - XY0**2),
  344. X *       XSMALL  <  |x|  <=  3.0
  345. X */
  346. static double PY0[] = {
  347. X     1.0102532948020907590e04,
  348. X    -2.1287548474401797963e06,
  349. X     2.0422274357376619816e08,
  350. X    -8.3716255451260504098e09,
  351. X     1.0723538782003176831e11,
  352. X    -1.8402381979244993524e01,
  353. };
  354. static double QY0[] = {
  355. X    6.6475986689240190091e02,
  356. X    2.3889393209447253406e05,
  357. X    5.5662956624278251596e07,
  358. X    8.1617187777290363573e09,
  359. X    5.8873865738997033405e11,
  360. };
  361. X
  362. /*
  363. X * Coefficients for rational approximation of
  364. X *   (y0(x) - 2 LN(x/XY1) j0(x)) / (x**2 - XY1**2),
  365. X *       3.0  <  |x|  <=  5.5
  366. X */
  367. static double PY1[] = {
  368. X    -1.4566865832663635920e04,
  369. X     4.6905288611678631510e06,
  370. X    -6.9590439394619619534e08,
  371. X     4.3600098638603061642e10,
  372. X    -5.5107435206722644429e11,
  373. X    -2.2213976967566192242e13,
  374. X     1.7427031242901594547e01,
  375. };
  376. static double QY1[] = {
  377. X    8.3030857612070288823e02,
  378. X    4.0669982352539552018e05,
  379. X    1.3960202770986831075e08,
  380. X    3.4015103849971240096e10,
  381. X    5.4266824419412347550e12,
  382. X    4.3386146580707264428e14,
  383. };
  384. X
  385. /*
  386. X * Coefficients for rational approximation of
  387. X *   (y0(x) - 2 LN(x/XY2) j0(x)) / (x**2 - XY2**2),
  388. X *       5.5  <  |x|  <=  8.0
  389. X */
  390. static double PY2[] = {
  391. X     2.1363534169313901632e04,
  392. X    -1.0085539923498211426e07,
  393. X     2.1958827170518100757e09,
  394. X    -1.9363051266772083678e11,
  395. X    -1.2829912364088687306e11,
  396. X     6.7016641869173237784e14,
  397. X    -8.0728726905150210443e15,
  398. X    -1.7439661319197499338e01,
  399. };
  400. static double QY2[] = {
  401. X    8.7903362168128450017e02,
  402. X    5.3924739209768057030e05,
  403. X    2.4727219475672302327e08,
  404. X    8.6926121104209825246e10,
  405. X    2.2598377924042897629e13,
  406. X    3.9272425569640309819e15,
  407. X    3.4563724628846457519e17,
  408. };
  409. X
  410. /*
  411. X * Coefficients for Hart,s approximation,  |x| > 8.0
  412. X */
  413. static double P0[] = {
  414. X    3.4806486443249270347e03,
  415. X    2.1170523380864944322e04,
  416. X    4.1345386639580765797e04,
  417. X    2.2779090197304684302e04,
  418. X    8.8961548424210455236e-01,
  419. X    1.5376201909008354296e02,
  420. };
  421. static double Q0[] = {
  422. X    3.5028735138235608207e03,
  423. X    2.1215350561880115730e04,
  424. X    4.1370412495510416640e04,
  425. X    2.2779090197304684318e04,
  426. X    1.5711159858080893649e02,
  427. };
  428. static double P1[] = {
  429. X    -2.2300261666214198472e01,
  430. X    -1.1183429920482737611e02,
  431. X    -1.8591953644342993800e02,
  432. X    -8.9226600200800094098e01,
  433. X    -8.8033303048680751817e-03,
  434. X    -1.2441026745835638459e00,
  435. };
  436. static double Q1[] = {
  437. X    1.4887231232283756582e03,
  438. X    7.2642780169211018836e03,
  439. X    1.1951131543434613647e04,
  440. X    5.7105024128512061905e03,
  441. X    9.0593769594993125859e01,
  442. };
  443. X
  444. static double
  445. #if defined(__STDC__) || defined(__GNUC__)
  446. caljy0(double x,int jint)
  447. #else
  448. caljy0(x,jint)
  449. double x;
  450. int jint;
  451. #endif
  452. {
  453. X    int i;
  454. X    double resj,down,up,xden,xnum,w,wsq,z,zsq;
  455. X
  456. X    if (jint && x <= ZERO)        /* Check for error conditions */
  457. X        return -XINF;
  458. #define    ax x
  459. X    else if ((ax = fabs(x)) > XMAX)
  460. X        return ZERO;
  461. /*
  462. X * Calculate j0 or y0 for |x|  >  8.0
  463. X */
  464. X    if (ax > EIGHT) {
  465. X        z = EIGHT/ax;
  466. X        w = ax/TWOPI;
  467. X        w = floor(w)+ONEOV8;
  468. X        w = (ax-w*TWOPI1)-w*TWOPI2;
  469. X        zsq = z*z;
  470. X        xnum = P0[4]*zsq+P0[5];
  471. X        xden = zsq+Q0[4];
  472. X        up = P1[4]*zsq+P1[5];
  473. X        down = zsq+Q1[4];
  474. X        for (i = 0; i <= 3; i++) {
  475. X            xnum = xnum*zsq+P0[i];
  476. X            xden = xden*zsq+Q0[i];
  477. X            up = up*zsq+P1[i];
  478. X            down = down*zsq+Q1[i];
  479. X        }
  480. #define    r0 xnum
  481. #define    r1 up
  482. X        r0 = xnum/xden;
  483. X        r1 = up/down;
  484. X        return sqrt(PI2/ax)*(jint ? r0*sin(w)+z*r1*cos(w) :
  485. X            r0*cos(w)-z*r1*sin(w));
  486. #undef    r1
  487. #undef    r0
  488. X    }
  489. X    if (ax <= XSMALL)
  490. X        return jint ? PI2*(log(ax)+CONS) : ONE;
  491. /*
  492. X * Calculate j0 for appropriate interval, preserving
  493. X *    accuracy near the zero of j0
  494. X */
  495. X    zsq = ax*ax;
  496. X    if (ax <= FOUR) {
  497. X        xnum = (PJ0[4]*zsq+PJ0[5])*zsq+PJ0[6];
  498. X        xden = zsq+QJ0[4];
  499. X        for (i = 0; i <= 3; i++) {
  500. X            xnum = xnum*zsq+PJ0[i];
  501. X            xden = xden*zsq+QJ0[i];
  502. X        }
  503. #define    prod resj
  504. X        prod = ((ax-XJ01/TWO56)-XJ02)*(ax+XJ0);
  505. X    }
  506. X    else {
  507. X        wsq = ONE-zsq/SIXTY4;
  508. X        xnum = PJ1[6]*wsq+PJ1[7];
  509. X        xden = wsq+QJ1[6];
  510. X        for (i = 0; i <= 5; i++) {
  511. X            xnum = xnum*wsq+PJ1[i];
  512. X            xden = xden*wsq+QJ1[i];
  513. X        }
  514. X        prod = (ax+XJ1)*((ax-XJ11/TWO56)-XJ12);
  515. X    }
  516. #define    result resj
  517. X    result = prod*xnum/xden;
  518. #undef    prod
  519. X    if (!jint)
  520. X        return result;
  521. /*
  522. X * Calculate y0.  First find  resj = pi/2 ln(x/xn) j0(x),
  523. X *   where xn is a zero of y0
  524. X */
  525. #define    xy z
  526. X    if (ax <= THREE) {
  527. X        up = (ax-XY01/TWO56)-XY02;
  528. X        xy = XY0;
  529. X    }
  530. X    else if (ax <= FIVE5) {
  531. X        up = (ax-XY11/TWO56)-XY12;
  532. X        xy = XY1;
  533. X    }
  534. X    else {
  535. X        up = (ax-XY21/TWO56)-XY22;
  536. X        xy = XY2;
  537. X    }
  538. X    down = ax+xy;
  539. X    if (fabs(up) < P17*down) {
  540. X        w = up/down;
  541. X        wsq = w*w;
  542. X        xnum = PLG[0];
  543. X        xden = wsq+QLG[0];
  544. X        for (i = 1; i <= 3; i++) {
  545. X            xnum = xnum*wsq+PLG[i];
  546. X            xden = xden*wsq+QLG[i];
  547. X        }
  548. X        resj = PI2*result*w*xnum/xden;
  549. X    }
  550. X    else
  551. X        resj = PI2*result*log(ax/xy);
  552. #undef    xy
  553. #undef    result
  554. /*
  555. X * Now calculate y0 for appropriate interval, preserving
  556. X *    accuracy near the zero of y0
  557. X */
  558. X    if (ax <= THREE) {
  559. X        xnum = PY0[5]*zsq+PY0[0];
  560. X        xden = zsq+QY0[0];
  561. X        for (i = 1; i <= 4; i++) {
  562. X            xnum = xnum*zsq+PY0[i];
  563. X            xden = xden*zsq+QY0[i];
  564. X        }
  565. X    }
  566. X    else if (ax <= FIVE5) {
  567. #undef    ax
  568. X        xnum = PY1[6]*zsq+PY1[0];
  569. X        xden = zsq+QY1[0];
  570. X        for (i = 1; i <= 5; i++) {
  571. X            xnum = xnum*zsq+PY1[i];
  572. X            xden = xden*zsq+QY1[i];
  573. X        }
  574. X    }
  575. X    else {
  576. X        xnum = PY2[7]*zsq+PY2[0];
  577. X        xden = zsq+QY2[0];
  578. X        for (i = 1; i <= 6; i++) {
  579. X            xnum = xnum*zsq+PY2[i];
  580. X            xden = xden*zsq+QY2[i];
  581. X        }
  582. X    }
  583. X    return resj+up*down*xnum/xden;
  584. }
  585. X
  586. /* 
  587. X *  This subprogram computes approximate values for Bessel functions
  588. X *    of the first kind of order zero for arguments  |x| <= XMAX
  589. X *    (see comments heading caljy0).
  590. X */
  591. double
  592. #if defined(__STDC__) || defined(__GNUC__)
  593. j0(double x)
  594. #else
  595. j0(x)
  596. double x;
  597. #endif
  598. {
  599. X    return caljy0(x,0);
  600. }
  601. X
  602. /* 
  603. X *  This subprogram computes approximate values for Bessel functions
  604. X *    of the second kind of order zero for arguments 0 < x <= XMAX
  605. X *    (see comments heading caljy0).
  606. X */
  607. double
  608. #if defined(__STDC__) || defined(__GNUC__)
  609. y0(double x)
  610. #else
  611. y0(x)
  612. double x;
  613. #endif
  614. {
  615. X    return caljy0(x,1);
  616. }
  617. SHAR_EOF
  618. chmod 0644 j0.c ||
  619. echo 'restore of j0.c failed'
  620. Wc_c="`wc -c < 'j0.c'`"
  621. test 12774 -eq "$Wc_c" ||
  622.     echo 'j0.c: original size 12774, current size' "$Wc_c"
  623. rm -f _shar_wnt_.tmp
  624. fi
  625. # ============= lgamma.c ==============
  626. if test -f 'lgamma.c' -a X"$1" != X"-c"; then
  627.     echo 'x - skipping lgamma.c (File already exists)'
  628.     rm -f _shar_wnt_.tmp
  629. else
  630. > _shar_wnt_.tmp
  631. echo 'x - extracting lgamma.c (Text)'
  632. sed 's/^X//' << 'SHAR_EOF' > 'lgamma.c' &&
  633. #ifndef lint
  634. static char sccsid[] = "@(#)lgamma.c    1.2    (ucb.beef)    3/1/89";
  635. #endif    /* !defined(lint) */
  636. /* 
  637. X * This routine calculates the log(GAMMA) function for a positive real
  638. X *   argument x.  Computation is based on an algorithm outlined in
  639. X *   references 1 and 2.  The program uses rational functions that
  640. X *   theoretically approximate log(GAMMA) to at least 18 significant
  641. X *   decimal digits.  The approximation for x > 12 is from reference
  642. X *   3, while approximations for x < 12.0 are similar to those in
  643. X *   reference 1, but are unpublished.  The accuracy achieved depends
  644. X *   on the arithmetic system, the compiler, the intrinsic functions,
  645. X *   and proper selection of the machine-dependent constants.
  646. X * 
  647. X **********************************************************************
  648. X **********************************************************************
  649. X * 
  650. X * Explanation of machine-dependent constants
  651. X * 
  652. X * beta   - radix for the floating-point representation
  653. X * maxexp - the smallest positive power of beta that overflows
  654. X * XBIG   - largest argument for which LN(GAMMA(x)) is representable
  655. X *          in the machine, i.e., the solution to the equation
  656. X *                  LN(GAMMA(XBIG)) = beta**maxexp
  657. X * XINF   - largest machine representable floating-point number;
  658. X *          approximately beta**maxexp.
  659. X * EPS    - The smallest positive floating-point number such that
  660. X *          1.0+EPS > 1.0
  661. X * FRTBIG - Rough estimate of the fourth root of XBIG
  662. X * 
  663. X * 
  664. X *     Approximate values for some important machines are:
  665. X * 
  666. X *                           beta      maxexp         XBIG
  667. X * 
  668. X * CRAY-1        (S.P.)        2        8191       9.62E+2461
  669. X * Cyber 180/855
  670. X *   under NOS   (S.P.)        2        1070       1.72E+319
  671. X * IEEE (IBM/XT,
  672. X *   SUN, etc.)  (S.P.)        2         128       4.08E+36
  673. X * IEEE (IBM/XT,
  674. X *   SUN, etc.)  (D.P.)        2        1024       2.55D+305
  675. X * IBM 3033      (D.P.)       16          63       4.29D+73
  676. X * VAX D-Format  (D.P.)        2         127       2.05D+36
  677. X * VAX G-Format  (D.P.)        2        1023       1.28D+305
  678. X *
  679. X * 
  680. X *                           XINF        EPS        FRTBIG
  681. X * 
  682. X * CRAY-1        (S.P.)   5.45E+2465   7.11E-15    3.13E+615
  683. X * Cyber 180/855
  684. X *   under NOS   (S.P.)   1.26E+322    3.55E-15    6.44E+79
  685. X * IEEE (IBM/XT,
  686. X *   SUN, etc.)  (S.P.)   3.40E+38     1.19E-7     1.42E+9
  687. X * IEEE (IBM/XT,
  688. X *   SUN, etc.)  (D.P.)   1.79D+308    2.22D-16    2.25D+76
  689. X * IBM 3033      (D.P.)   7.23D+75     2.22D-16    2.56D+18
  690. X * VAX D-Format  (D.P.)   1.70D+38     1.39D-17    1.20D+9
  691. X * VAX G-Format  (D.P.)   8.98D+307    1.11D-16    1.89D+76
  692. X * 
  693. X ***************************************************************
  694. X ***************************************************************
  695. X * 
  696. X * Error returns
  697. X * 
  698. X *  The program returns the value XINF for x <= 0.0 or when
  699. X *     overflow would occur.  The computation is believed to 
  700. X *     be free of underflow and overflow.
  701. X * 
  702. X * 
  703. X * Intrinsic functions required are:
  704. X * 
  705. X *      log
  706. X * 
  707. X * 
  708. X * References:
  709. X * 
  710. X *  1) W. J. Cody and K. E. Hillstrom, 'Chebyshev Approximations for
  711. X *     the Natural Logarithm of the Gamma Function,' Math. Comp. 21,
  712. X *     1967, pp. 198-203.
  713. X * 
  714. X *  2) K. E. Hillstrom, ANL/AMD Program ANLC366S, DGAMMA/DLGAMA, May,
  715. X *     1969.
  716. X * 
  717. X *  3) Hart, Et. Al., Computer Approximations, Wiley and sons, New
  718. X *     York, 1968.
  719. X * 
  720. X * 
  721. X *  Authors: W. J. Cody and L. Stoltz
  722. X *           Argonne National Laboratory
  723. X * 
  724. X *  Latest modification: June 16, 1988
  725. X */
  726. #if defined(__STDC__) || defined(__GNUC__)
  727. extern double log(double);
  728. #else
  729. extern double log();
  730. #endif
  731. X                    /* Machine dependent parameters */
  732. #if defined(vax) || defined(tahoe)
  733. #define    XBIG    (double)2.05e36
  734. #define    XINF    (double)1.70e38
  735. #define    EPS    (double)1.39e-17
  736. #define    FRTBIG    (double)1.20e9
  737. #else    /* defined(vax) || defined(tahoe) */
  738. #define    XBIG    (double)2.55e305
  739. #define    XINF    (double)1.79e308
  740. #define    EPS    (double)2.22e-16
  741. #define    FRTBIG    (double)2.25e76
  742. #endif    /* defined(vax) || defined(tahoe) */
  743. X                    /* Mathematical constants */
  744. #define    ONE    (double)1
  745. #define    HALF    (double)0.5
  746. #define    TWELVE    (double)12
  747. #define    ZERO    (double)0
  748. #define    FOUR    (double)4
  749. #define    THRHAL    (double)1.5
  750. #define    TWO    (double)2
  751. #define    PNT68    (double)0.6796875
  752. #define    SQRTPI    (double)0.9189385332046727417803297
  753. X
  754. /*
  755. X * Numerator and denominator coefficients for rational minimax Approximation
  756. X * over (0.5,1.5).
  757. X */
  758. static double D1 = -5.772156649015328605195174e-1;
  759. static double P1[] = {
  760. X    4.945235359296727046734888e0,
  761. X    2.018112620856775083915565e2,
  762. X    2.290838373831346393026739e3,
  763. X    1.131967205903380828685045e4,
  764. X    2.855724635671635335736389e4,
  765. X    3.848496228443793359990269e4,
  766. X    2.637748787624195437963534e4,
  767. X    7.225813979700288197698961e3,
  768. };
  769. static double Q1[] = {
  770. X    6.748212550303777196073036e1,
  771. X    1.113332393857199323513008e3,
  772. X    7.738757056935398733233834e3,
  773. X    2.763987074403340708898585e4,
  774. X    5.499310206226157329794414e4,
  775. X    6.161122180066002127833352e4,
  776. X    3.635127591501940507276287e4,
  777. X    8.785536302431013170870835e3,
  778. };
  779. X
  780. /*
  781. X * Numerator and denominator coefficients for rational minimax Approximation
  782. X * over (1.5,4.0).
  783. X */
  784. static double D2 = 4.227843350984671393993777e-1;
  785. static double P2[] = {
  786. X    4.974607845568932035012064e0,
  787. X    5.424138599891070494101986e2,
  788. X    1.550693864978364947665077e4,
  789. X    1.847932904445632425417223e5,
  790. X    1.088204769468828767498470e6,
  791. X    3.338152967987029735917223e6,
  792. X    5.106661678927352456275255e6,
  793. X    3.074109054850539556250927e6,
  794. };
  795. static double Q2[] = {
  796. X    1.830328399370592604055942e2,
  797. X    7.765049321445005871323047e3,
  798. X    1.331903827966074194402448e5,
  799. X    1.136705821321969608938755e6,
  800. X    5.267964117437946917577538e6,
  801. X    1.346701454311101692290052e7,
  802. X    1.782736530353274213975932e7,
  803. X    9.533095591844353613395747e6,
  804. };
  805. X
  806. /*
  807. X * Numerator and denominator coefficients for rational minimax Approximation
  808. X * over (4.0,12.0).
  809. X */
  810. static double D4 = 1.791759469228055000094023e0;
  811. static double P4[] = {
  812. X    1.474502166059939948905062e4,
  813. X    2.426813369486704502836312e6,
  814. X    1.214755574045093227939592e8,
  815. X    2.663432449630976949898078e9,
  816. X    2.940378956634553899906876e10,
  817. X    1.702665737765398868392998e11,
  818. X    4.926125793377430887588120e11,
  819. X    5.606251856223951465078242e11,
  820. };
  821. static double Q4[] = {
  822. X    2.690530175870899333379843e3,
  823. X    6.393885654300092398984238e5,
  824. X    4.135599930241388052042842e7,
  825. X    1.120872109616147941376570e9,
  826. X    1.488613728678813811542398e10,
  827. X    1.016803586272438228077304e11,
  828. X    3.417476345507377132798597e11,
  829. X    4.463158187419713286462081e11,
  830. };
  831. X
  832. /*
  833. X * Coefficients for minimax approximation over (12, INF).
  834. X */
  835. static double C[] = {
  836. X    -1.910444077728e-03,
  837. X     8.4171387781295e-04,
  838. X    -5.952379913043012e-04,
  839. X     7.93650793500350248e-04,
  840. X    -2.777777777777681622553e-03,
  841. X     8.333333333333333331554247e-02,
  842. X     5.7083835261e-03,
  843. };
  844. X
  845. double
  846. #if defined(__STDC__) || defined(__GNUC__)
  847. lgamma(double x)
  848. #else
  849. lgamma(x)
  850. double x;
  851. #endif
  852. {
  853. X    register i;
  854. X    double res,corr,xden,xnum,dtmp;
  855. X
  856. #define    y x
  857. X    if (y > ZERO && y <= XBIG) {
  858. X        if (y <= EPS)
  859. X            res = -log(y);
  860. X        else if (y <= THRHAL) {            /*  EPS < x <= 1.5 */
  861. #define    xm1 dtmp
  862. X            if (y < PNT68) {
  863. X                corr = -log(y);
  864. X                xm1 = y;
  865. X            }
  866. X            else {
  867. X                corr = ZERO;
  868. X                xm1 = y-HALF; xm1 -= HALF;
  869. X            }
  870. X            if (y <= HALF || y >= PNT68) {
  871. X                xden = ONE;
  872. X                xnum = ZERO;
  873. X                for (i = 0; i <= 7; i++) {
  874. X                    xnum = xnum*xm1+P1[i];
  875. X                    xden = xden*xm1+Q1[i];
  876. X                }
  877. X                res = xnum/xden; res = corr+xm1*(D1+xm1*res);
  878. #undef    xm1
  879. X            }
  880. X            else {
  881. #define    xm2 dtmp
  882. X                xm2 = y-HALF; xm2 -= HALF;
  883. X                xden = ONE;
  884. X                xnum = ZERO;
  885. X                for (i = 0; i <= 7; i++) {
  886. X                    xnum = xnum*xm2+P2[i];
  887. X                    xden = xden*xm2+Q2[i];
  888. X                }
  889. X                res = xnum/xden; res = corr+xm2*(D2+xm2*res);
  890. X            }
  891. X        }
  892. X        else if (y <= FOUR) {            /*  1.5 < x <= 4.0 */
  893. X            xm2 = y-TWO;
  894. X            xden = ONE;
  895. X            xnum = ZERO;
  896. X            for (i = 0; i <= 7; i++) {
  897. X                xnum = xnum*xm2+P2[i];
  898. X                xden = xden*xm2+Q2[i];
  899. X            }
  900. X            res = xnum/xden; res = xm2*(D2+xm2*res);
  901. #undef    xm2
  902. X        }
  903. X        else if (y <= TWELVE) {            /*  4.0 < x <= 12.0 */
  904. #define    xm4 dtmp
  905. X            xm4 = y-FOUR;
  906. X            xden = -ONE;
  907. X            xnum = ZERO;
  908. X            for (i = 0; i <= 7; i++) {
  909. X                xnum = xnum*xm4+P4[i];
  910. X                xden = xden*xm4+Q4[i];
  911. X            }
  912. X            res = xnum/xden; res = D4+xm4*res;
  913. #undef    xm4
  914. X        }
  915. X        else {                    /*  x >= 12.0 */
  916. X            res = ZERO;
  917. X            if (y <= FRTBIG) {
  918. X                res = C[6];
  919. #define    ysq dtmp
  920. X                ysq = y*y;
  921. X                for (i = 0; i <= 5; i++)
  922. X                    res = res/ysq+C[i];
  923. #define    ysq dtmp
  924. X            }
  925. X            res /= y;
  926. X            corr = log(y);
  927. X            res += SQRTPI; res -= HALF*corr;
  928. X            res += y*(corr-ONE);
  929. X        }
  930. #undef    y
  931. X    }
  932. X    else                    /*  Return for bad arguments */
  933. X        res = XINF;
  934. X    return res;
  935. }
  936. SHAR_EOF
  937. chmod 0644 lgamma.c ||
  938. echo 'restore of lgamma.c failed'
  939. Wc_c="`wc -c < 'lgamma.c'`"
  940. test 8438 -eq "$Wc_c" ||
  941.     echo 'lgamma.c: original size 8438, current size' "$Wc_c"
  942. rm -f _shar_wnt_.tmp
  943. fi
  944. # ============= gamma.c ==============
  945. if test -f 'gamma.c' -a X"$1" != X"-c"; then
  946.     echo 'x - skipping gamma.c (File already exists)'
  947.     rm -f _shar_wnt_.tmp
  948. else
  949. > _shar_wnt_.tmp
  950. echo 'x - extracting gamma.c (Text)'
  951. sed 's/^X//' << 'SHAR_EOF' > 'gamma.c' &&
  952. #ifndef lint
  953. static char sccsid[] = "@(#)gamma.c    1.6    (ucb.beef)    10/3/89";
  954. #endif    /* !defined(lint) */
  955. /* 
  956. X *  This routine calculates the GAMMA function for a "double" argument X.
  957. X *    Computation is based on an algorithm outlined in reference 1.
  958. X *    The program uses rational functions that approximate the GAMMA
  959. X *    function to at least 20 significant decimal digits.  Coefficients
  960. X *    for the approximation over the interval (1,2) are unpublished.
  961. X *    Those for the approximation for X .GE. 12 are from reference 2.
  962. X *    The accuracy achieved depends on the arithmetic system, the
  963. X *    compiler, the intrinsic functions, and proper selection of the
  964. X *    machine-dependent constants.
  965. X * 
  966. X * 
  967. X *********************************************************************
  968. X *********************************************************************
  969. X * 
  970. X *  Explanation of machine-dependent constants
  971. X * 
  972. X *  beta   - radix for the floating-point representation
  973. X *  maxexp - the smallest positive power of beta that overflows
  974. X *  XBIG   - the largest argument for which GAMMA(X) is representable
  975. X *           in the machine, i.e., the solution to the equation
  976. X *                   GAMMA(XBIG) = beta**maxexp
  977. X *  XINF   - the largest machine representable floating-point number;
  978. X *           approximately beta**maxexp
  979. X *  EPS    - the smallest positive floating-point number such that
  980. X *           1.0+EPS .GT. 1.0
  981. X *  XMININ - the smallest positive floating-point number such that
  982. X *           1/XMININ is machine representable
  983. X * 
  984. X *      Approximate values for some important machines are:
  985. X * 
  986. X *                             beta       maxexp        XBIG
  987. X * 
  988. X *  CRAY-1         (S.P.)        2         8191        967.095
  989. X *  Cyber 180/855
  990. X *    under NOS    (S.P.)        2         1070        177.980
  991. X *  IEEE (IBM/XT,
  992. X *    SUN, etc.)   (S.P.)        2          128        35.299
  993. X *  IEEE (IBM/XT,
  994. X *    SUN, etc.)   (D.P.)        2         1024        171.624
  995. X *  IBM 3033       (D.P.)       16           63        57.801
  996. X *  VAX D-Format   (D.P.)        2          127        34.844
  997. X *  VAX G-Format   (D.P.)        2         1023        171.668
  998. X * 
  999. X *                             XINF         EPS        XMININ
  1000. X * 
  1001. X *  CRAY-1         (S.P.)   5.45E+2465   7.11E-15    1.84E-2466
  1002. X *  Cyber 180/855
  1003. X *    under NOS    (S.P.)   1.26E+322    3.55E-15    3.14E-294
  1004. X *  IEEE (IBM/XT,
  1005. X *    SUN, etc.)   (S.P.)   3.40E+38     1.19E-7     1.18E-38
  1006. X *  IEEE (IBM/XT,
  1007. X *    SUN, etc.)   (D.P.)   1.79D+308    2.22D-16    2.23D-308
  1008. X *  IBM 3033       (D.P.)   7.23D+75     2.22D-16    1.39D-76
  1009. X *  VAX D-Format   (D.P.)   1.70D+38     1.39D-17    5.88D-39
  1010. X *  VAX G-Format   (D.P.)   8.98D+307    1.11D-16    1.12D-308
  1011. X * 
  1012. X *********************************************************************
  1013. X *********************************************************************
  1014. X * 
  1015. X *  Error returns
  1016. X * 
  1017. X *   The program returns the value XINF for singularities or
  1018. X *      when overflow would occur.  The computation is believed
  1019. X *      to be free of underflow and overflow.
  1020. X * 
  1021. X * 
  1022. X *   Intrinsic functions required are:
  1023. X * 
  1024. X *      exp, floor, log, sin
  1025. X * 
  1026. X * 
  1027. X *   References: "An Overview of Software Development for Special
  1028. X *               Functions", W. J. Cody, Lecture Notes in Mathematics,
  1029. X *               506, Numerical Analysis Dundee, 1975, G. A. Watson
  1030. X *               (ed.), Springer Verlag, Berlin, 1976.
  1031. X * 
  1032. X *               Computer Approximations, Hart, Et. Al., Wiley and
  1033. X *               sons, New York, 1968.
  1034. X * 
  1035. X *   Latest modification: May 30, 1989
  1036. X * 
  1037. X *   Authors: W. J. Cody and L. Stoltz
  1038. X *            Applied Mathematics Division
  1039. X *            Argonne National Laboratory
  1040. X *            Argonne, IL 60439
  1041. X */
  1042. #if defined(__STDC__) || defined(__GNUC__)
  1043. extern double floor(double),exp(double),log(double),sin(double);
  1044. #else
  1045. extern double floor(),exp(),log(),sin();
  1046. #endif
  1047. X                    /* Machine dependent parameters */
  1048. #if defined(vax) || defined(tahoe)
  1049. #define    XBIG    (double)34.844e0
  1050. #define    XMININ    (double)5.88e-39
  1051. #define    EPS    (double)1.39e-17
  1052. #define    XINF    (double)1.70e38
  1053. #else    /* defined(vax) || defined(tahoe) */
  1054. #define    XBIG    (double)171.624e0
  1055. #define    XMININ    (double)2.23e-308
  1056. #define    EPS    (double)2.22e-16
  1057. #define    XINF    (double)1.79e308
  1058. #endif    /* defined(vax) || defined(tahoe) */
  1059. X                    /* Mathematical constants */
  1060. #define    ONE    (double)1
  1061. #define    HALF    (double)0.5
  1062. #define    TWELVE    (double)12
  1063. #define    ZERO    (double)0
  1064. #define    SQRTPI    (double)0.9189385332046727417803297e0
  1065. #define    PI    (double)3.1415926535897932384626434e0
  1066. X
  1067. typedef int logical;
  1068. #define FALSE    (logical)0
  1069. #define    TRUE    (logical)1
  1070. X
  1071. /*
  1072. X *   Numerator and denominator coefficients for rational minimax
  1073. X *      approximation over (1,2).
  1074. X */
  1075. static double P[] = {
  1076. X    -1.71618513886549492533811e0,
  1077. X     2.47656508055759199108314e1,
  1078. X    -3.79804256470945635097577e2,
  1079. X     6.29331155312818442661052e2,
  1080. X     8.66966202790413211295064e2,
  1081. X    -3.14512729688483675254357e4,
  1082. X    -3.61444134186911729807069e4,
  1083. X     6.64561438202405440627855e4,
  1084. };
  1085. static double Q[] = {
  1086. X    -3.08402300119738975254353e1,
  1087. X     3.15350626979604161529144e2,
  1088. X    -1.01515636749021914166146e3,
  1089. X    -3.10777167157231109440444e3,
  1090. X     2.25381184209801510330112e4,
  1091. X     4.75584627752788110767815e3,
  1092. X    -1.34659959864969306392456e5,
  1093. X    -1.15132259675553483497211e5,
  1094. };
  1095. X
  1096. /*
  1097. X *   Coefficients for minimax approximation over (12, INF).
  1098. X */
  1099. static double C[] = {
  1100. X    -1.910444077728e-03,
  1101. X     8.4171387781295e-04,
  1102. X    -5.952379913043012e-04,
  1103. X     7.93650793500350248e-04,
  1104. X    -2.777777777777681622553e-03,
  1105. X     8.333333333333333331554247e-02,
  1106. X     5.7083835261e-03,
  1107. };
  1108. X
  1109. double
  1110. #if defined(__STDC__) || defined(__GNUC__)
  1111. gamma(double x)
  1112. #else
  1113. gamma(x)
  1114. double x;
  1115. #endif
  1116. {
  1117. X    register i,n;
  1118. X    logical parity;
  1119. X    double fact,res,xden,xnum,dtmp1,dtmp2;
  1120. X
  1121. X    parity = FALSE;
  1122. X    fact = ONE;
  1123. X    n = 0;
  1124. #define    y x
  1125. X    if (y <= ZERO) {            /* Arg. is negative */
  1126. X        y = -x;
  1127. #define    y1 dtmp1
  1128. X        y1 = floor(y);
  1129. X        res = y-y1;
  1130. X        if (res != ZERO) {
  1131. X            parity = floor(y1/2)*2 != y1;
  1132. X            fact = -PI/sin(PI*res);
  1133. X            y += ONE;
  1134. X        }
  1135. X        else
  1136. X            return XINF;
  1137. X    }
  1138. X                        /* Arg. is positive */
  1139. X    if (y < EPS) {                /* Arg. < EPS */
  1140. X        if (y >= XMININ)
  1141. X            res = ONE/y;
  1142. X        else
  1143. X          return XINF;
  1144. X    }
  1145. X    else if (y < TWELVE) {
  1146. #define    y1 dtmp1
  1147. #define    z dtmp2
  1148. X        y1 = y;
  1149. X        if (y < ONE) {            /* 0.0 < arg. < 1.0 */
  1150. X            z = y;
  1151. X            y += ONE;
  1152. X        }
  1153. X        else {                /* 1.0 < arg. < 12.0 */
  1154. X
  1155. X            n = (int)y; n--;
  1156. X            y -= (double)n;        /* reduce arg. if needed */
  1157. X            z = y-ONE;
  1158. X        }
  1159. X            /* Evaluate approximation for 1.0 < arg. < 2.0 */
  1160. X        xnum = ZERO;
  1161. X        xden = ONE;
  1162. X        for (i = 0; i <= 7; i++) {
  1163. X            xnum = (xnum+P[i])*z;
  1164. X            xden = xden*z+Q[i];
  1165. X        }
  1166. X        res = xnum/xden+ONE;
  1167. X        if (y1 < y)        /* Adjust res for 0.0 < arg. < 1.0 */
  1168. X            res /= y1;
  1169. X        else if (y1 > y) {    /* Adjust res for 2.0 < arg. < 12.0 */
  1170. X            for (i = 0; i <= n-1; i++) {
  1171. X                res *= y;
  1172. X                y += ONE;
  1173. X            }
  1174. X        }
  1175. #undef    z
  1176. #undef    y1
  1177. X    }
  1178. X    else {                    /* Evaluate for arg. >= 12.0 */
  1179. X        if (y <= XBIG) {
  1180. #define    ysq dtmp1
  1181. #define    sum dtmp2
  1182. X            ysq = y*y;
  1183. X            sum = C[6];
  1184. X            for (i = 0; i <= 5; i++)
  1185. X                sum = sum/ysq+C[i];
  1186. X            sum = sum/y-y; sum += SQRTPI;
  1187. X            sum += (y-HALF)*log(y);
  1188. X            res = exp(sum);
  1189. #undef    sum
  1190. #undef    ysq
  1191. X        }
  1192. X        else
  1193. X            return XINF;
  1194. X    }
  1195. X                        /* Final adjustments and return */
  1196. X    if (parity == TRUE)
  1197. X        res = -res;
  1198. X    if (fact != ONE)
  1199. X        res = fact/res;
  1200. X    return res;
  1201. #undef    y
  1202. }
  1203. SHAR_EOF
  1204. chmod 0644 gamma.c ||
  1205. echo 'restore of gamma.c failed'
  1206. Wc_c="`wc -c < 'gamma.c'`"
  1207. test 7210 -eq "$Wc_c" ||
  1208.     echo 'gamma.c: original size 7210, current size' "$Wc_c"
  1209. rm -f _shar_wnt_.tmp
  1210. fi
  1211. # ============= j1.c ==============
  1212. if test -f 'j1.c' -a X"$1" != X"-c"; then
  1213.     echo 'x - skipping j1.c (File already exists)'
  1214.     rm -f _shar_wnt_.tmp
  1215. else
  1216. > _shar_wnt_.tmp
  1217. echo 'x - extracting j1.c (Text)'
  1218. sed 's/^X//' << 'SHAR_EOF' > 'j1.c' &&
  1219. #ifndef lint
  1220. static char sccsid[] = "@(#)j1.c    1.1    (ucb.beef)    10/1/89";
  1221. #endif    /* !defined(lint) */
  1222. /* 
  1223. X *  This packet computes first-order Bessel functions of the first and
  1224. X *    second kind (j1 and y1), for real arguments x, where 0 < x <= XMAX
  1225. X *    for y1, and |x| <= XMAX for j1.  It contains three function-type
  1226. X *    subprograms, j1, y1 and caljy1.  The calling statements for the
  1227. X *    primary entries are:
  1228. X * 
  1229. X *            y = j1(x)
  1230. X *    and
  1231. X *            y = y1(x),
  1232. X * 
  1233. X *    where the entry points correspond to the functions j1(x) and y1(x),
  1234. X *    respectively.  The routine caljy1() is intended for internal packet
  1235. X *    use only, all computations within the packet being concentrated in
  1236. X *    this one routine.  The function subprograms invoke  caljy1  with
  1237. X *    the statement
  1238. X *
  1239. X *            result = caljy1(x,jint);
  1240. X *
  1241. X *    where the parameter usage is as follows:
  1242. X * 
  1243. X *       Function                  Parameters for caljy1
  1244. X *        call              x             result          jint
  1245. X * 
  1246. X *       j1(x)       |x| <= XMAX          j1(x)           0
  1247. X *       y1(x)     0 < x <= XMAX          y1(x)           1
  1248. X * 
  1249. X *    The main computation uses unpublished minimax rational
  1250. X *    approximations for x <= 8.0, and an approximation from the 
  1251. X *    book  Computer Approximations  by Hart, et. al., Wiley and Sons, 
  1252. X *    New York, 1968, for arguments larger than 8.0   Part of this
  1253. X *    transportable packet is patterned after the machine-dependent
  1254. X *    FUNPACK program for j1(x), but cannot match that version for
  1255. X *    efficiency or accuracy.  This version uses rational functions
  1256. X *    that are theoretically accurate to at least 18 significant decimal
  1257. X *    digits for x <= 8, and at least 18 decimal places for x > 8.  The
  1258. X *    accuracy achieved depends on the arithmetic system, the compiler,
  1259. X *    the intrinsic functions, and proper selection of the machine-
  1260. X *    dependent constants.
  1261. X * 
  1262. X *********************************************************************
  1263. X * 
  1264. X *  Explanation of machine-dependent constants
  1265. X * 
  1266. X *    XINF   = largest positive machine number
  1267. X *    XMAX   = largest acceptable argument.  The functions floor(), sin()
  1268. X *             and cos() must perform properly for  fabs(x) <= XMAX.
  1269. X *             We recommend that XMAX be a small integer multiple of
  1270. X *             sqrt(1/eps), where eps is the smallest positive number
  1271. X *             such that  1+eps > 1. 
  1272. X *    XSMALL = positive argument such that  1.0-(1/2)(x/2)**2 = 1.0
  1273. X *             to machine precision for all  fabs(x) <= XSMALL.
  1274. X *             We recommend that  XSMALL < sqrt(eps)/beta, where beta
  1275. X *             is the floating-point radix (usually 2 or 16).
  1276. X * 
  1277. X *      Approximate values for some important machines are
  1278. X * 
  1279. X *                           eps      XMAX     XSMALL      XINF  
  1280. X * 
  1281. X *   CDC 7600      (S.P.)  7.11E-15  1.34E+08  2.98E-08  1.26E+322
  1282. X *   CRAY-1        (S.P.)  7.11E-15  1.34E+08  2.98E-08  5.45E+2465
  1283. X *   IBM PC (8087) (S.P.)  5.96E-08  8.19E+03  1.22E-04  3.40E+38
  1284. X *   IBM PC (8087) (D.P.)  1.11e-16  2.68e08  3.72e-09  1.79e308
  1285. X *   IBM 195       (D.P.)  2.22e-16  6.87e09  9.09e-13  7.23e75
  1286. X *   UNIVAC 1108   (D.P.)  1.73e-18  4.30e09  2.33e-10  8.98e307
  1287. X *   VAX 11/780    (D.P.)  1.39e-17  1.07e09  9.31e-10  1.70e38
  1288. X * 
  1289. X *********************************************************************
  1290. X *********************************************************************
  1291. X * 
  1292. X *  Error Returns
  1293. X * 
  1294. X *   The program returns the value zero for  x > XMAX, and returns
  1295. X *     -XINF when BESLY1 is called with a negative or zero argument.
  1296. X * 
  1297. X * 
  1298. X *  Intrinsic functions required are:
  1299. X * 
  1300. X *      cos, fabs, floor, log, sin, sqrt
  1301. X * 
  1302. X * 
  1303. X *   Author: w. J. Cody
  1304. X *           Mathematics and Computer Science Division 
  1305. X *           Argonne National Laboratory
  1306. X *           Argonne, IL 60439
  1307. X * 
  1308. X *   Latest modification: November 10, 1987
  1309. X */
  1310. #if defined(__STDC__) || defined(__GNUC__)
  1311. extern double cos(double),fabs(double),floor(double),
  1312. X    log(double),sin(double),sqrt(double);
  1313. #else
  1314. extern double cos(),fabs(),floor(),log(),sin(),sqrt();
  1315. #endif
  1316. X                    /* Machine-dependent constants */
  1317. #if defined(vax) || defined(tahoe)
  1318. #define    XMAX    (double)1.07e9
  1319. #define    XSMALL    (double)9.31e-10
  1320. #define    XINF    (double)1.7e38
  1321. #else    /* defined(vax) || defined(tahoe) */
  1322. #define    XMAX    (double)2.68e08
  1323. #define    XSMALL    (double)3.72e-09
  1324. #define    XINF    (double)1.79e308
  1325. #endif    /* defined(vax) || defined(tahoe) */
  1326. X                    /* Mathematical constants */
  1327. #define    EIGHT    (double)8
  1328. #define    FOUR    (double)4
  1329. #define    HALF    (double)0.5
  1330. #define    THROV8    (double)0.375
  1331. #define    PI2    (double)6.3661977236758134308e-1
  1332. #define    P17    (double)1.716e-1
  1333. #define    TWOPI    (double)6.2831853071795864769e0
  1334. #define    ZERO    (double)0
  1335. #define    TWOPI1    (double)6.28125
  1336. #define    TWOPI2    (double)1.9353071795864769253e-03
  1337. #define    TWO56    (double)256
  1338. #define    RTPI2    (double)7.9788456080286535588e-1
  1339. X                    /* Zeroes of Bessel functions */
  1340. #define    XJ0    (double)3.8317059702075123156e0
  1341. #define    XJ1    (double)7.0155866698156187535e0
  1342. #define    XY0    (double)2.1971413260310170351e0
  1343. #define    XY1    (double)5.4296810407941351328e0
  1344. #define    XJ01    (double)981
  1345. #define    XJ02    (double)(-3.2527979248768438556e-4)
  1346. #define    XJ11    (double)1796
  1347. #define    XJ12    (double)(-3.8330184381246462950e-5)
  1348. #define    XY01    (double)562
  1349. #define    XY02    (double)1.8288260310170351490e-3
  1350. #define    XY11    (double)1390
  1351. #define    XY12    (double)(-6.4592058648672279948e-6)
  1352. X
  1353. /*
  1354. X * Coefficients for rational approximation to ln(x/a)
  1355. X */
  1356. static double PLG[] = {
  1357. X    -2.4562334077563243311e01,
  1358. X     2.3642701335621505212e02,
  1359. X    -5.4989956895857911039e02,
  1360. X     3.5687548468071500413e02,
  1361. };
  1362. static double QLG[] = {
  1363. X    -3.5553900764052419184e01,
  1364. X     1.9400230218539473193e02,
  1365. X    -3.3442903192607538956e02,
  1366. X     1.7843774234035750207e02,
  1367. };
  1368. X
  1369. /*
  1370. X * Coefficients for rational approximation of
  1371. X * j1(x) / (x * (x**2 - XJ0**2)),  XSMALL  <  |x|  <=  4.0
  1372. X */
  1373. static double PJ0[] = {
  1374. X     9.8062904098958257677e05,
  1375. X    -1.1548696764841276794e08,
  1376. X     6.6781041261492395835e09,
  1377. X    -1.4258509801366645672e11,
  1378. X    -4.4615792982775076130e03,
  1379. X     1.0650724020080236441e01,
  1380. X    -1.0767857011487300348e-02,
  1381. };
  1382. static double QJ0[] = {
  1383. X    5.9117614494174794095e05,
  1384. X    2.0228375140097033958e08,
  1385. X    4.2091902282580133541e10,
  1386. X    4.1868604460820175290e12,
  1387. X    1.0742272239517380498e03,
  1388. };
  1389. X
  1390. /*
  1391. X * Coefficients for rational approximation of
  1392. X * j1(x) / (x * (x**2 - XJ1**2)),  4.0  <  |x|  <=  8.0
  1393. X */
  1394. static double PJ1[] = {
  1395. X     4.6179191852758252280e00,
  1396. X    -7.1329006872560947377e03,
  1397. X     4.5039658105749078904e06,
  1398. X    -1.4437717718363239107e09,
  1399. X     2.3569285397217157313e11,
  1400. X    -1.6324168293282543629e13,
  1401. X     1.1357022719979468624e14,
  1402. X     1.0051899717115285432e15,
  1403. };
  1404. static double QJ1[] = {
  1405. X    1.1267125065029138050e06,
  1406. X    6.4872502899596389593e08,
  1407. X    2.7622777286244082666e11,
  1408. X    8.4899346165481429307e13,
  1409. X    1.7128800897135812012e16,
  1410. X    1.7253905888447681194e18,
  1411. X    1.3886978985861357615e03,
  1412. };
  1413. X
  1414. /*
  1415. X * Coefficients for rational approximation of
  1416. X *   (y1(x) - 2 LN(x/XY0) j1(x)) / (x**2 - XY0**2),
  1417. X *       XSMALL  <  |x|  <=  4.0
  1418. X */
  1419. static double PY0[] = {
  1420. X     2.2157953222280260820e05,
  1421. X    -5.9157479997408395984e07,
  1422. X     7.2144548214502560419e09,
  1423. X    -3.7595974497819597599e11,
  1424. X     5.4708611716525426053e12,
  1425. X     4.0535726612579544093e13,
  1426. X    -3.1714424660046133456e02,
  1427. };
  1428. static double QY0[] = {
  1429. X    8.2079908168393867438e02,
  1430. X    3.8136470753052572164e05,
  1431. X    1.2250435122182963220e08,
  1432. X    2.7800352738690585613e10,
  1433. X    4.1272286200406461981e12,
  1434. X    3.0737873921079286084e14,
  1435. };
  1436. X
  1437. /*
  1438. X * Coefficients for rational approximation of
  1439. X *   (y1(x) - 2 LN(x/XY1) j1(x)) / (x**2 - XY1**2),
  1440. X *        .0  <  |x|  <=  8.0
  1441. X */
  1442. static double PY1[] = {
  1443. X     1.9153806858264202986e06,
  1444. X    -1.1957961912070617006e09,
  1445. X     3.7453673962438488783e11,
  1446. X    -5.9530713129741981618e13,
  1447. X     4.0686275289804744814e15,
  1448. X    -2.3638408497043134724e16,
  1449. X    -5.6808094574724204577e18,
  1450. X     1.1514276357909013326e19,
  1451. X    -1.2337180442012953128e03,
  1452. };
  1453. static double QY1[] = {
  1454. X    1.2855164849321609336e03,
  1455. X    1.0453748201934079734e06,
  1456. X    6.3550318087088919566e08,
  1457. X    3.0221766852960403645e11,
  1458. X    1.1187010065856971027e14,
  1459. X    3.0837179548112881950e16,
  1460. X    5.6968198822857178911e18,
  1461. X    5.3321844313316185697e20,
  1462. };
  1463. X
  1464. /*
  1465. X * Coefficients for Hart,s approximation,  |x| > 8.0
  1466. X */
  1467. static double P0[] = {
  1468. X    -1.0982405543459346727e05,
  1469. X    -1.5235293511811373833e06,
  1470. X    -6.6033732483649391093e06,
  1471. X    -9.9422465050776411957e06,
  1472. X    -4.4357578167941278571e06,
  1473. X    -1.6116166443246101165e03,
  1474. };
  1475. static double Q0[] = {
  1476. X    -1.0726385991103820119e05,
  1477. X    -1.5118095066341608816e06,
  1478. X    -6.5853394797230870728e06,
  1479. X    -9.9341243899345856590e06,
  1480. X    -4.4357578167941278568e06,
  1481. X    -1.4550094401904961825e03,
  1482. };
  1483. static double P1[] = {
  1484. X    1.7063754290207680021e03,
  1485. X    1.8494262873223866797e04,
  1486. X    6.6178836581270835179e04,
  1487. X    8.5145160675335701966e04,
  1488. X    3.3220913409857223519e04,
  1489. X    3.5265133846636032186e01,
  1490. };
  1491. static double Q1[] = {
  1492. X    3.7890229745772202641e04,
  1493. X    4.0029443582266975117e05,
  1494. X    1.4194606696037208929e06,
  1495. X    1.8194580422439972989e06,
  1496. X    7.0871281941028743574e05,
  1497. X    8.6383677696049909675e02,
  1498. };
  1499. X
  1500. static double
  1501. #if defined(__STDC__) || defined(__GNUC__)
  1502. caljy1(double x, int jint)
  1503. #else
  1504. caljy1(x,jint)
  1505. double x;
  1506. int jint;
  1507. #endif
  1508. {
  1509. X    int i;
  1510. X    double ax,resj,down,up,xden,xnum,w,wsq,z,zsq;
  1511. X
  1512. X    ax = fabs(x);                /* Check for error conditions */
  1513. X    if (jint && (x <= ZERO || x < HALF && ax*XINF < PI2))
  1514. X        return -XINF;
  1515. X    else if (ax > XMAX)
  1516. X        return ZERO;
  1517. /*
  1518. X * Calculate j1 or y1 for |x|  >  8.0
  1519. X */
  1520. X    if (ax > EIGHT) {
  1521. X        z = EIGHT/ax;
  1522. X        w = floor(ax/TWOPI)+THROV8;
  1523. X        w = (ax-w*TWOPI1)-w*TWOPI2;
  1524. X        zsq = z*z;
  1525. X        xnum = P0[5];
  1526. X        xden = zsq+Q0[5];
  1527. X        up = P1[5];
  1528. X        down = zsq+Q1[5];
  1529. X        for (i = 0; i <= 4; i++) {
  1530. X            xnum = xnum*zsq+P0[i];
  1531. X            xden = xden*zsq+Q0[i];
  1532. X            up = up*zsq+P1[i];
  1533. X            down = down*zsq+Q1[i];
  1534. X        }
  1535. #define    r0 xnum
  1536. #define    r1 up
  1537. X        r0 = xnum/xden;
  1538. X        r1 = up/down;
  1539. X        return RTPI2/sqrt(ax)*(jint ? r0*sin(w)+z*r1*cos(w) :
  1540. X            (x < ZERO ? z*r1*sin(w)-r0*cos(w) :
  1541. X            r0*cos(w)-z*r1*sin(w)));
  1542. #undef    r1
  1543. #undef    r0
  1544. X    }
  1545. X    else if (ax <= XSMALL)
  1546. X        return jint ? -PI2/ax : x*HALF;
  1547. /*
  1548. X * Calculate j1 for appropriate interval, preserving
  1549. X *    accuracy near the zero of j1
  1550. X */
  1551. X    zsq = ax*ax;
  1552. X    if (ax <= FOUR) {
  1553. X        xnum = (PJ0[6]*zsq+PJ0[5])*zsq+PJ0[4];
  1554. X        xden = zsq+QJ0[4];
  1555. X        for (i = 0; i <= 3; i++) {
  1556. X            xnum = xnum*zsq+PJ0[i];
  1557. X            xden = xden*zsq+QJ0[i];
  1558. X        }
  1559. #define    prod resj
  1560. X        prod = x*((ax-XJ01/TWO56)-XJ02)*(ax+XJ0);
  1561. X    }
  1562. X    else {
  1563. X        xnum = PJ1[0];
  1564. X        xden = (zsq+QJ1[6])*zsq+QJ1[0];
  1565. X        for (i = 1; i <= 5; i++) {
  1566. X            xnum = xnum*zsq+PJ1[i];
  1567. X            xden = xden*zsq+QJ1[i];
  1568. X        }
  1569. X        xnum = xnum*(ax-EIGHT)*(ax+EIGHT)+PJ1[6];
  1570. X        xnum = xnum*(ax-FOUR)*(ax+FOUR)+PJ1[7];
  1571. X        prod = x*((ax-XJ11/TWO56)-XJ12)*(ax+XJ1);
  1572. X    }
  1573. #define    result resj
  1574. X    result = prod*(xnum/xden);
  1575. #undef    prod
  1576. X    if (!jint)
  1577. X        return result;
  1578. /*
  1579. X * Calculate y1.  First find  resj = pi/2 ln(x/xn) j1(x),
  1580. X *   where xn is a zero of y1
  1581. X */
  1582. #define    xy z
  1583. X    if (ax <= FOUR) {
  1584. X        up = (ax-XY01/TWO56)-XY02;
  1585. X        xy = XY0;
  1586. X    }
  1587. X    else {
  1588. X        up = (ax-XY11/TWO56)-XY12;
  1589. X        xy = XY1;
  1590. X    }
  1591. X    down = ax+xy;
  1592. X    if (fabs(up) < P17*down) {
  1593. X        w = up/down;
  1594. X        wsq = w*w;
  1595. X        xnum = PLG[0];
  1596. X        xden = wsq+QLG[0];
  1597. X        for (i = 1; i <= 3; i++) {
  1598. X            xnum = xnum*wsq+PLG[i];
  1599. X            xden = xden*wsq+QLG[i];
  1600. X        }
  1601. X        resj = PI2*result*w*xnum/xden;
  1602. X    }
  1603. X    else
  1604. X        resj = PI2*result*log(ax/xy);
  1605. #undef    xy
  1606. #undef    result
  1607. /*
  1608. X * Now calculate y1 for appropriate interval, preserving
  1609. X *    accuracy near the zero of y1
  1610. X */
  1611. X    if (ax <= FOUR) {
  1612. X        xnum = PY0[6]*zsq+PY0[0];
  1613. X        xden = zsq+QY0[0];
  1614. X        for (i = 1; i <= 5; i++) {
  1615. X            xnum = xnum*zsq+PY0[i];
  1616. X            xden = xden*zsq+QY0[i];
  1617. X        }
  1618. X    }
  1619. X    else {
  1620. X        xnum = PY1[8]*zsq+PY1[0];
  1621. X        xden = zsq+QY1[0];
  1622. X        for (i = 1; i <= 7; i++) {
  1623. X            xnum = xnum*zsq+PY1[i];
  1624. X            xden = xden*zsq+QY1[i];
  1625. X        }
  1626. X    }
  1627. X    up *= down; up /= ax; up *= xnum; up /= xden; up += resj;
  1628. X    return up;
  1629. }
  1630. X
  1631. /*
  1632. X * This subprogram computes approximate values for Bessel functions
  1633. X *   of the first kind of order zero for arguments  |x| <= XMAX
  1634. X *   (see comments heading caljy1).
  1635. X */
  1636. double
  1637. #if defined(__STDC__) || defined(__GNUC__)
  1638. j1(double x)
  1639. #else
  1640. j1(x)
  1641. double x;
  1642. #endif
  1643. {
  1644. X    return caljy1(x,0);
  1645. }
  1646. X
  1647. /*
  1648. X * This subprogram computes approximate values for Bessel functions
  1649. X *   of the second kind of order zero for arguments 0 < x <= XMAX
  1650. X *   (see comments heading caljy1).
  1651. X */
  1652. double
  1653. #if defined(__STDC__) || defined(__GNUC__)
  1654. y1(double x)
  1655. #else
  1656. y1(x)
  1657. double x;
  1658. #endif
  1659. {
  1660. X    return caljy1(x,1);
  1661. }
  1662. SHAR_EOF
  1663. chmod 0644 j1.c ||
  1664. echo 'restore of j1.c failed'
  1665. Wc_c="`wc -c < 'j1.c'`"
  1666. test 12086 -eq "$Wc_c" ||
  1667.     echo 'j1.c: original size 12086, current size' "$Wc_c"
  1668. rm -f _shar_wnt_.tmp
  1669. fi
  1670. # ============= erf.c ==============
  1671. if test -f 'erf.c' -a X"$1" != X"-c"; then
  1672.     echo 'x - skipping erf.c (File already exists)'
  1673.     rm -f _shar_wnt_.tmp
  1674. else
  1675. > _shar_wnt_.tmp
  1676. echo 'x - extracting erf.c (Text)'
  1677. sed 's/^X//' << 'SHAR_EOF' > 'erf.c' &&
  1678. #ifndef lint
  1679. static char sccsid[] = "@(#)erf.c    1.3    (ucb.beef)    10/2/89";
  1680. #endif    /* !defined(lint) */
  1681. /* 
  1682. X * This packet evaluates erf(x), erfc(x), and exp(x*x)*erfc(x)
  1683. X *   for a "double" argument x.  It contains three subprograms:
  1684. X *   erf(), erfc(), and erfcx() and * one "static" subprogram,
  1685. X *   calerf.  The calling statements for the primary entries are:
  1686. X * 
  1687. X *                   y = erf(x),
  1688. X * 
  1689. X *                   y = erfc(x),
  1690. X *   and
  1691. X *                   y = erfcx(x).
  1692. X * 
  1693. X *   The routine calerf() is intended for internal packet use only,
  1694. X *   all computations within the packet being concentrated in this
  1695. X *   routine.  The 3 primary subprograms invoke calerf with the
  1696. X *   statement
  1697. X * 
  1698. X *          result = calerf(arg,jint);
  1699. X * 
  1700. X *   where the parameter usage is as follows
  1701. X * 
  1702. X *      Function                     Parameters for calerf
  1703. X *       call              arg                  result          jint
  1704. X * 
  1705. X *     erf(arg)      ANY "double" ARGUMENT      erf(arg)          0
  1706. X *     erfc(arg)     fabs(arg) < XBIG           erfc(arg)         1
  1707. X *     erfcx(arg)    XNEG < arg < XMAX          erfcx(arg)        2
  1708. X * 
  1709. X *   The main computation evaluates near-minimax approximations
  1710. X *   from "Rational Chebyshev approximations for the error function"
  1711. X *   by W. J. Cody, Math. Comp., 1969, PP. 631-638.  This
  1712. X *   transportable program uses rational functions that theoretically
  1713. X *   approximate  erf(x)  and  erfc(x)  to at least 18 significant
  1714. X *   decimal digits.  The accuracy achieved depends on the arithmetic
  1715. X *   system, the compiler, the intrinsic functions, and proper
  1716. X *   selection of the machine-dependent constants.
  1717. X * 
  1718. X ********************************************************************
  1719. X ********************************************************************
  1720. X * 
  1721. X * Explanation of machine-dependent constants
  1722. X * 
  1723. X *   XMIN   = the smallest positive floating-point number.
  1724. X *   XINF   = the largest positive finite floating-point number.
  1725. X *   XNEG   = the largest negative argument acceptable to erfcx;
  1726. X *            the negative of the solution to the equation
  1727. X *            2*exp(x*x) = XINF.
  1728. X *   XSMALL = argument below which erf(x) may be represented by
  1729. X *            2*x/sqrt(pi)  and above which  x*x  will not underflow.
  1730. X *            A conservative value is the largest machine number x
  1731. X *            such that   1.0 + x = 1.0   to machine precision.
  1732. X *   XBIG   = largest argument acceptable to erfc;  solution to
  1733. X *            the equation:  W(x) * (1-0.5/x**2) = XMIN,  where
  1734. X *            W(x) = exp(-x*x)/[x*sqrt(pi)].
  1735. X *   XHUGE  = argument above which  1.0 - 1/(2*x*x) = 1.0  to
  1736. X *            machine precision.  A conservative value is
  1737. X *            1/[2*sqrt(XSMALL)]
  1738. X *   XMAX   = largest acceptable argument to erfcx; the minimum
  1739. X *            of XINF and 1/[sqrt(pi)*XMIN].
  1740. X * 
  1741. X *   Approximate values for some important machines are:
  1742. X * 
  1743. X *                          XMIN       XINF        XNEG     XSMALL
  1744. X * 
  1745. X *  CDC 7600      (S.P.)  3.13E-294   1.26E+322   -27.220  7.11E-15
  1746. X *  CRAY-1        (S.P.)  4.58E-2467  5.45E+2465  -75.345  7.11E-15
  1747. X *  IEEE (IBM/XT,
  1748. X *    SUN, etc.)  (S.P.)  1.18E-38    3.40E+38     -9.382  5.96E-8
  1749. X *  IEEE (IBM/XT,
  1750. X *    SUN, etc.)  (D.P.)  2.23D-308   1.79D+308   -26.628  1.11D-16
  1751. X *  IBM 195       (D.P.)  5.40D-79    7.23E+75    -13.190  1.39D-17
  1752. X *  UNIVAC 1108   (D.P.)  2.78D-309   8.98D+307   -26.615  1.73D-18
  1753. X *  VAX D-Format  (D.P.)  2.94D-39    1.70D+38     -9.345  1.39D-17
  1754. X *  VAX G-Format  (D.P.)  5.56D-309   8.98D+307   -26.615  1.11D-16
  1755. X * 
  1756. X * 
  1757. X *                          XBIG       XHUGE       XMAX
  1758. X * 
  1759. X *  CDC 7600      (S.P.)  25.922      8.39E+6     1.80X+293
  1760. X *  CRAY-1        (S.P.)  75.326      8.39E+6     5.45E+2465
  1761. X *  IEEE (IBM/XT,
  1762. X *    SUN, etc.)  (S.P.)   9.194      2.90E+3     4.79E+37
  1763. X *  IEEE (IBM/XT,
  1764. X *    SUN, etc.)  (D.P.)  26.543      6.71D+7     2.53D+307
  1765. X *  IBM 195       (D.P.)  13.306      1.90D+8     7.23E+75
  1766. X *  UNIVAC 1108   (D.P.)  26.582      5.37D+8     8.98D+307
  1767. X *  VAX D-Format  (D.P.)   9.269      1.90D+8     1.70D+38
  1768. X *  VAX G-Format  (D.P.)  26.569      6.71D+7     8.98D+307
  1769. X * 
  1770. X ********************************************************************
  1771. X ********************************************************************
  1772. X * 
  1773. X * Error returns
  1774. X * 
  1775. X *  The program returns  erfc = 0      for  arg .GE. XBIG;
  1776. X * 
  1777. X *                       erfcx = XINF  for  arg .LT. XNEG;
  1778. X *      and
  1779. X *                       erfcx = 0     for  arg .GE. XMAX.
  1780. X * 
  1781. X * 
  1782. X * Intrinsic functions required are:
  1783. X * 
  1784. X *     fabs, exp
  1785. X * 
  1786. X * 
  1787. X *  Author: W. J. Cody
  1788. X *          Mathematics and Computer Science Division
  1789. X *          Argonne National Laboratory
  1790. X *          Argonne, IL 60439
  1791. X * 
  1792. X *  Latest modification: June 16, 1988
  1793. X */
  1794. #if defined(__STDC__) || defined(__GNUC__)
  1795. extern double fabs(double),exp(double);
  1796. #else
  1797. extern double fabs(),exp();
  1798. #endif
  1799. X                    /* Machine-dependent constants */
  1800. #if defined(vax) || defined(tahoe)
  1801. #define    XINF    (double)1.70e38
  1802. #define    XNEG    (double)(-9.345e0)
  1803. #define    XSMALL    (double)1.39e-17
  1804. #define    XBIG    (double)9.269e0
  1805. #define    XHUGE    (double)1.90e8
  1806. #define    XMAX    (double)1.70e38
  1807. #else    /* defined(vax) || defined(tahoe) */
  1808. #define    XINF    (double)1.79e308
  1809. #define    XNEG    (double)(-26.628e0)
  1810. #define    XSMALL    (double)1.11e-16
  1811. #define    XBIG    (double)26.543e0
  1812. #define    XHUGE    (double)6.71e7
  1813. #define    XMAX    (double)2.53e307
  1814. #endif    /* defined(vax) || defined(tahoe) */
  1815. X                    /* Mathematical constants */
  1816. #define    FOUR    (double)4
  1817. #define    ONE    (double)1
  1818. #define    HALF    (double)0.5
  1819. #define    TWO    (double)2
  1820. #define    ZERO    (double)0
  1821. #define    SQRPI    (double)5.6418958354775628695e-1
  1822. #define    THRESH    (double)0.46875
  1823. #define    SIXTEN    (double)16
  1824. X
  1825. /*
  1826. X * Coefficients for approximation to  erf  in first interval
  1827. X */
  1828. static double A[] = {
  1829. X    3.16112374387056560e00,
  1830. X    1.13864154151050156e02,
  1831. X    3.77485237685302021e02,
  1832. X    3.20937758913846947e03,
  1833. X    1.85777706184603153e-1,
  1834. };
  1835. static double B[] = {
  1836. X    2.36012909523441209e01,
  1837. X    2.44024637934444173e02,
  1838. X    1.28261652607737228e03,
  1839. X    2.84423683343917062e03,
  1840. };
  1841. X
  1842. /*
  1843. X * Coefficients for approximation to  erfc  in second interval
  1844. X */
  1845. static double C[] = {
  1846. X    5.64188496988670089e-1,
  1847. X    8.88314979438837594e0,
  1848. X    6.61191906371416295e01,
  1849. X    2.98635138197400131e02,
  1850. X    8.81952221241769090e02,
  1851. X    1.71204761263407058e03,
  1852. X    2.05107837782607147e03,
  1853. X    1.23033935479799725e03,
  1854. X    2.15311535474403846e-8,
  1855. };
  1856. static double D[] = {
  1857. X    1.57449261107098347e01,
  1858. X    1.17693950891312499e02,
  1859. X    5.37181101862009858e02,
  1860. X    1.62138957456669019e03,
  1861. X    3.29079923573345963e03,
  1862. X    4.36261909014324716e03,
  1863. X    3.43936767414372164e03,
  1864. X    1.23033935480374942e03,
  1865. };
  1866. X
  1867. /*
  1868. X * Coefficients for approximation to  erfc  in third interval
  1869. X */
  1870. static double P[] = {
  1871. X    3.05326634961232344e-1,
  1872. X    3.60344899949804439e-1,
  1873. X    1.25781726111229246e-1,
  1874. X    1.60837851487422766e-2,
  1875. X    6.58749161529837803e-4,
  1876. X    1.63153871373020978e-2,
  1877. };
  1878. static double Q[] = {
  1879. X    2.56852019228982242e00,
  1880. X    1.87295284992346047e00,
  1881. X    5.27905102951428412e-1,
  1882. X    6.05183413124413191e-2,
  1883. X    2.33520497626869185e-3,
  1884. };
  1885. X
  1886. static double
  1887. #if defined(__STDC__) || defined(__GNUC__)
  1888. calerf(double x,int jint)
  1889. #else
  1890. calerf(x,jint)
  1891. double x;
  1892. int jint;
  1893. #endif
  1894. {
  1895. X    register i,skip;
  1896. X    double y,ysq,xnum,xden,result;
  1897. X
  1898. X    y = fabs(x);
  1899. X    if (y <= THRESH) {    /* Evaluate erf for |x| <= 0.46875 */
  1900. X        ysq = y > XSMALL ? y*y : ZERO;
  1901. X        xnum = A[4]*ysq;
  1902. X        xden = ysq;
  1903. X        for (i = 0; i <= 2; i++) {
  1904. X            xnum = (xnum+A[i])*ysq;
  1905. X            xden = (xden+B[i])*ysq;
  1906. X        }
  1907. X        result = x*(xnum+A[3])/(xden+B[3]);
  1908. X        if (jint)
  1909. X            result = ONE-result;
  1910. X        if (jint == 2)
  1911. X            result *= exp(ysq);
  1912. X        return result;
  1913. X    }
  1914. X    else if (y <= FOUR) {    /* Evaluate erfc for 0.46875 <= |x| <= 4.0 */
  1915. X        ysq = y*y;
  1916. X        xnum = C[8]*y;
  1917. X        xden = y;
  1918. X        for (i = 0; i <= 6; i++) {
  1919. X            xnum = (xnum+C[i])*y;
  1920. X            xden = (xden+D[i])*y;
  1921. X        }
  1922. X        result = (xnum+C[7])/(xden+D[7]);
  1923. X        if (jint != 2) {
  1924. X            i = (int)(y*SIXTEN); ysq = (double)i/SIXTEN;
  1925. X            result *= exp(-ysq*ysq)*exp(-(y-ysq)*(y+ysq));
  1926. X        }
  1927. X    }
  1928. X    else {            /* Evaluate erfc for |x| > 4.0 */
  1929. X        result = ZERO;
  1930. X        skip = 0;
  1931. X        if (y >= XBIG) {
  1932. X            if (jint != 2 || y >= XMAX)
  1933. X                skip++;
  1934. X            else if (y >= XHUGE) {
  1935. X                result = SQRPI/y;
  1936. X                skip++;
  1937. X            }
  1938. X        }
  1939. X        if (!skip) {
  1940. X            ysq = ONE/(y*y);
  1941. X            xnum = P[5]*ysq;
  1942. SHAR_EOF
  1943. true || echo 'restore of erf.c failed'
  1944. fi
  1945. echo 'End of mathlib2.0 part 1'
  1946. echo 'File erf.c is continued in part 2'
  1947. echo 2 > _shar_seq_.tmp
  1948. exit 0
  1949. --
  1950. Glenn Geers                       | "So when it's over, we're back to people.
  1951. Department of Theoretical Physics |  Just to prove that human touch can have
  1952. The University of Sydney          |  no equal."
  1953. Sydney NSW 2006 Australia         |  - Basia Trzetrzelewska, 'Prime Time TV'
  1954.