home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / Gnuplot / Source / specfun.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-02  |  19.2 KB  |  826 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: specfun.c%v 3.38.2.86 1993/03/01 01:50:57 woo Exp woo $";
  3. #endif
  4.  
  5.  
  6. /** GNUPLOT - specfun.c
  7.  *
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley,
  9.  *                                              Jos van der Woude
  10.  *
  11.  * Permission to use, copy, and distribute this software and its
  12.  * documentation for any purpose with or without fee is hereby granted,
  13.  * provided that the above copyright notice appear in all copies and
  14.  * that both that copyright notice and this permission notice appear
  15.  * in supporting documentation.
  16.  *
  17.  * Permission to modify the software is granted, but not the right to
  18.  * distribute the modified code.  Modifications are to be distributed
  19.  * as patches to released version.
  20.  *
  21.  * This software is provided "as is" without express or implied warranty.
  22.  *
  23.  *
  24.  * AUTHORS
  25.  *
  26.  *   Original Software:
  27.  *   Jos van der Woude, jvdwoude@hut.nl
  28.  *
  29.  * Send your comments or suggestions to
  30.  *  info-gnuplot@dartmouth.edu.
  31.  * This is a mailing list; to join it send a note to
  32.  *  info-gnuplot-request@dartmouth.edu.
  33.  * Send bug reports to
  34.  *  bug-gnuplot@dartmouth.edu.
  35.  */
  36.  
  37. #include <math.h>
  38. #include <stdio.h>
  39. #include "plot.h"
  40.  
  41. #ifdef vms
  42. #include <errno.h>
  43. #else
  44. extern int errno;
  45. #endif /* vms */
  46.  
  47.  
  48. extern struct value stack[STACK_DEPTH];
  49. extern int s_p;
  50. extern double zero;
  51.  
  52. struct value *pop(), *Gcomplex(), *Ginteger();
  53.  
  54. double magnitude(), angle(), real(), imag();
  55.  
  56. #define ITMAX   100
  57. #ifdef FLT_EPSILON
  58. #define MACHEPS FLT_EPSILON /* 1.0E-08 */
  59. #else
  60. #define MACHEPS 1.0E-08
  61. #endif
  62. #ifdef FLT_MIN_EXP
  63. #define MINEXP  FLT_MIN_EXP /* -88.0 */
  64. #else
  65. #define MINEXP  -88.0
  66. #endif
  67. #ifdef FLT_MAX
  68. #define OFLOW   FLT_MAX /* 1.0E+37 */
  69. #else
  70. #define OFLOW   1.0E+37
  71. #endif
  72. #ifdef FLT_MAX_10_EXP
  73. #define XBIG    FLT_MAX_10_EXP /* 2.55E+305 */
  74. #else
  75. #define XBIG    2.55E+305
  76. #endif
  77.  
  78. /*
  79.  * Mathematical constants
  80.  */
  81. #define LNPI 1.14472988584940016
  82. #define LNSQRT2PI 0.9189385332046727
  83. #define PI 3.14159265358979323846
  84. #define PNT68 0.6796875
  85. #define SQRTPI 0.9189385332046727417803297
  86. #define SQRT_TWO 1.41421356237309504880168872420969809   /* JG */
  87.  
  88. #ifndef min /* GCC ST uses inline functions */
  89. #define min(a,b) ((a) < (b) ? (a) : (b))
  90. #endif
  91.  
  92. /* Global variables, not visible outside this file */
  93. #ifndef GAMMA
  94. static int signgam = 0;
  95. #else
  96. extern int signgam;
  97. #endif
  98. static long     Xm1 = 2147483563L;
  99. static long     Xm2 = 2147483399L;
  100. static long     Xa1 = 40014L;
  101. static long     Xa2 = 40692L;
  102.  
  103. /* Local function declarations, not visible outside this file */
  104. static double confrac();
  105. static double ibeta();
  106. static double igamma();
  107. static double ranf();
  108.  
  109. #ifndef GAMMA
  110. /* Provide GAMMA function for those who do not already have one */
  111. static double lngamma();
  112. static double lgamneg();
  113. static double lgampos();
  114.  
  115. /**
  116.  * from statlib, Thu Jan 23 15:02:27 EST 1992 ***
  117.  *
  118.  * This file contains two algorithms for the logarithm of the gamma function.
  119.  * Algorithm AS 245 is the faster (but longer) and gives an accuracy of about
  120.  * 10-12 significant decimal digits except for small regions around X = 1 and
  121.  * X = 2, where the function goes to zero.
  122.  * The second algorithm is not part of the AS algorithms.   It is slower but
  123.  * gives 14 or more significant decimal digits accuracy, except around X = 1
  124.  * and X = 2.   The Lanczos series from which this algorithm is derived is
  125.  * interesting in that it is a convergent series approximation for the gamma
  126.  * function, whereas the familiar series due to De Moivre (and usually wrongly
  127.  * called Stirling's approximation) is only an asymptotic approximation, as
  128.  * is the true and preferable approximation due to Stirling.
  129.  * 
  130.  * Uses Lanczos-type approximation to ln(gamma) for z > 0. Reference: Lanczos,
  131.  * C. 'A precision approximation of the gamma function', J. SIAM Numer.
  132.  * Anal., B, 1, 86-96, 1964. Accuracy: About 14 significant digits except for
  133.  * small regions in the vicinity of 1 and 2.
  134.  * 
  135.  * Programmer: Alan Miller CSIRO Division of Mathematics & Statistics
  136.  * 
  137.  * Latest revision - 17 April 1988
  138.  * 
  139.  * Additions: Translated from fortran to C, code added to handle values z < 0.
  140.  * The global variable signgam contains the sign of the gamma function.
  141.  * 
  142.  * IMPORTANT: The signgam variable contains garbage until AFTER the call to
  143.  * lngamma().
  144.  * 
  145.  * Permission granted to distribute freely for non-commercial purposes only
  146.  * Copyright (c) 1992 Jos van der Woude, jvdwoude@hut.nl
  147.  */
  148.  
  149. /* Local data, not visible outside this file */
  150. static double   a[] =
  151. {
  152.     0.9999999999995183E+00,
  153.     0.6765203681218835E+03,
  154.     -.1259139216722289E+04,
  155.     0.7713234287757674E+03,
  156.     -.1766150291498386E+03,
  157.     0.1250734324009056E+02,
  158.     -.1385710331296526E+00,
  159.     0.9934937113930748E-05,
  160.     0.1659470187408462E-06,
  161. };
  162.  
  163. static double   lgamneg(z)
  164. double z;
  165. {
  166.     double          tmp;
  167.  
  168.     /* Use reflection formula, then call lgampos() */
  169.     tmp = sin(z * PI);
  170.  
  171.     if (fabs(tmp) < MACHEPS) {
  172.     tmp = 0.0;
  173.     } else if (tmp < 0.0) {
  174.     tmp = -tmp;
  175.         signgam = -1;
  176.     }
  177.     return LNPI - lgampos(1.0 - z) - log(tmp);
  178.  
  179. }
  180.  
  181. static double   lgampos(z)
  182. double z;
  183. {
  184.     double          sum;
  185.     double          tmp;
  186.     int             i;
  187.  
  188.     sum = a[0];
  189.     for (i = 1, tmp = z; i < 9; i++) {
  190.         sum += a[i] / tmp;
  191.     tmp++;
  192.     }
  193.  
  194.     return log(sum) + LNSQRT2PI - z - 6.5 + (z - 0.5) * log(z + 6.5);
  195. }
  196.  
  197. static double lngamma(z)
  198. double z;
  199. {
  200.     signgam = 1.0;
  201.  
  202.     if (z <= 0.0)
  203.     return lgamneg(z);
  204.     else
  205.     return lgampos(z);
  206. }
  207.  
  208. #define GAMMA lngamma
  209. #endif /* GAMMA */
  210.  
  211. f_erf()
  212. {
  213. struct value a;
  214. double x;
  215.  
  216.         x = real(pop(&a));
  217. #ifndef ERF
  218.         x = x < 0.0 ? -igamma(0.5, x * x) : igamma(0.5, x * x);
  219. #else
  220.         x = erf(x);
  221. #endif
  222.         push( Gcomplex(&a,x,0.0) );
  223. }
  224.  
  225. f_erfc()
  226. {
  227. struct value a;
  228. double x;
  229.  
  230.         x = real(pop(&a));
  231. #ifndef ERF
  232.         x = x < 0.0 ? 1.0 + igamma(0.5, x * x) : 1.0 - igamma(0.5, x * x);
  233. #else
  234.         x = erfc(x);
  235. #endif
  236.         push( Gcomplex(&a,x,0.0) );
  237. }
  238.  
  239. f_ibeta()
  240. {
  241. struct value a;
  242. double x;
  243. double arg1;
  244. double arg2;
  245.  
  246.     x = real(pop(&a));
  247.     arg2 = real(pop(&a));
  248.     arg1 = real(pop(&a));
  249.  
  250.     x = ibeta(arg1, arg2, x);
  251.     if(x == -1.0) {
  252.         undefined = TRUE;
  253.         push( Ginteger(&a,0) );
  254.     } else
  255.         push( Gcomplex(&a,x,0.0) );
  256. }
  257.  
  258. f_igamma()
  259. {
  260. struct value a;
  261. double x;
  262. double arg1;
  263.  
  264.     x = real(pop(&a));
  265.     arg1 = real(pop(&a));
  266.  
  267.     x = igamma(arg1,x);
  268.     if(x == -1.0) {
  269.         undefined = TRUE;
  270.         push( Ginteger(&a,0) );
  271.     } else
  272.         push( Gcomplex(&a,x,0.0) );
  273. }
  274.  
  275. f_gamma()
  276. {
  277. register double y;
  278. struct value a;
  279.  
  280.         y = GAMMA(real(pop(&a)));
  281.     if (y > 88.0) {
  282.         undefined = TRUE;
  283.         push( Ginteger(&a,0) );
  284.     }
  285.     else
  286.         push( Gcomplex(&a,signgam * exp(y),0.0) );
  287. }
  288.  
  289. f_lgamma()
  290. {
  291. struct value a;
  292.  
  293.         push( Gcomplex(&a, GAMMA(real(pop(&a))),0.0) );
  294. }
  295.  
  296. #ifndef BADRAND
  297.  
  298. f_rand()
  299. {
  300. struct value a;
  301.  
  302.         push( Gcomplex(&a, ranf(real(pop(&a))),0.0) );
  303. }
  304.  
  305. #else
  306.  
  307. /* Use only to observe the effect of a "bad" random number generator. */
  308. f_rand()
  309. {
  310. struct value a;
  311.  
  312. static unsigned int y =0;
  313. unsigned int maxran = 1000;
  314.  
  315.     (void)real(pop(&a));
  316.     y = (781*y + 387) %maxran;
  317.  
  318.     push( Gcomplex(&a, (double) y /maxran,0.0) );
  319. }
  320.  
  321. #endif
  322.  
  323. /** ibeta.c
  324.  *
  325.  *   DESCRIB   Approximate the incomplete beta function Ix(a, b).
  326.  *
  327.  *                           _
  328.  *                          |(a + b)     /x  (a-1)         (b-1)
  329.  *             Ix(a, b) = -_-------_--- * |  t     * (1 - t)     dt (a,b > 0)
  330.  *                        |(a) * |(b)   /0
  331.  *
  332.  *
  333.  *
  334.  *   CALL      p = ibeta(a, b, x)
  335.  *
  336.  *             double    a    > 0
  337.  *             double    b    > 0
  338.  *             double    x    [0, 1]
  339.  *
  340.  *   WARNING   none
  341.  *
  342.  *   RETURN    double    p    [0, 1]
  343.  *                            -1.0 on error condition
  344.  *
  345.  *   XREF      lngamma()
  346.  *
  347.  *   BUGS      none
  348.  *
  349.  *   REFERENCE The continued fraction expansion as given by
  350.  *             Abramowitz and Stegun (1964) is used.
  351.  *
  352.  * Permission granted to distribute freely for non-commercial purposes only
  353.  * Copyright (c) 1992 Jos van der Woude, jvdwoude@hut.nl
  354.  */
  355.  
  356. static double          ibeta(a, b, x)
  357. double a, b, x;
  358. {
  359.     /* Test for admissibility of arguments */
  360.     if (a <= 0.0 || b <= 0.0)
  361.     return -1.0;
  362.     if (x < 0.0 || x > 1.0)
  363.     return -1.0;;
  364.  
  365.     /* If x equals 0 or 1, return x as prob */
  366.     if (x == 0.0 || x == 1.0)
  367.     return x;
  368.  
  369.     /* Swap a, b if necessarry for more efficient evaluation */
  370.     return a < x * (a + b) ? 1.0 - confrac(b, a, 1.0 - x) : confrac(a, b, x);
  371. }
  372.  
  373. static double   confrac(a, b, x)
  374. double a, b, x;
  375. {
  376.     double          Alo = 0.0;
  377.     double          Ahi;
  378.     double          Aev;
  379.     double          Aod;
  380.     double          Blo = 1.0;
  381.     double          Bhi = 1.0;
  382.     double          Bod = 1.0;
  383.     double          Bev = 1.0;
  384.     double          f;
  385.     double          fold;
  386.     double          Apb = a + b;
  387.     double          d;
  388.     int             i;
  389.     int             j;
  390.  
  391.     /* Set up continued fraction expansion evaluation. */
  392.     Ahi = exp(GAMMA(Apb) + a * log(x) + b * log(1.0 - x) -
  393.               GAMMA(a + 1.0) - GAMMA(b));
  394.  
  395.     /*
  396.      * Continued fraction loop begins here. Evaluation continues until
  397.      * maximum iterations are exceeded, or convergence achieved.
  398.      */
  399.     for (i = 0, j = 1, f = Ahi; i <= ITMAX; i++, j++) {
  400.     d = a + j + i;
  401.     Aev = -(a + i) * (Apb + i) * x / d / (d - 1.0);
  402.     Aod = j * (b - j) * x / d / (d + 1.0);
  403.     Alo = Bev * Ahi + Aev * Alo;
  404.     Blo = Bev * Bhi + Aev * Blo;
  405.     Ahi = Bod * Alo + Aod * Ahi;
  406.     Bhi = Bod * Blo + Aod * Bhi;
  407.  
  408.     if (fabs(Bhi) < MACHEPS)
  409.         Bhi = 0.0;
  410.  
  411.     if (Bhi != 0.0) {
  412.         fold = f;
  413.         f = Ahi / Bhi;
  414.         if (fabs(f - fold) < fabs(f) * MACHEPS)
  415.         return f;
  416.     }
  417.     }
  418.  
  419.     return -1.0;
  420. }
  421.  
  422. /** igamma.c
  423.  *
  424.  *   DESCRIB   Approximate the incomplete gamma function P(a, x).
  425.  *
  426.  *                         1     /x  -t   (a-1)
  427.  *             P(a, x) = -_--- * |  e  * t     dt      (a > 0)
  428.  *                       |(a)   /0
  429.  *
  430.  *   CALL      p = igamma(a, x)
  431.  *
  432.  *             double    a    >  0
  433.  *             double    x    >= 0
  434.  *
  435.  *   WARNING   none
  436.  *
  437.  *   RETURN    double    p    [0, 1]
  438.  *                            -1.0 on error condition
  439.  *
  440.  *   XREF      lngamma()
  441.  *
  442.  *   BUGS      Values 0 <= x <= 1 may lead to inaccurate results.
  443.  *
  444.  *   REFERENCE ALGORITHM AS239  APPL. STATIST. (1988) VOL. 37, NO. 3
  445.  *
  446.  * Permission granted to distribute freely for non-commercial purposes only
  447.  * Copyright (c) 1992 Jos van der Woude, jvdwoude@hut.nl
  448.  */
  449.  
  450. /* Global variables, not visible outside this file */
  451. static double   pn1, pn2, pn3, pn4, pn5, pn6;
  452.  
  453. static double          igamma(a, x)
  454. double a, x;
  455. {
  456.     double          arg;
  457.     double          aa;
  458.     double          an;
  459.     double          b;
  460.     int             i;
  461.  
  462.     /* Check that we have valid values for a and x */
  463.     if (x < 0.0 || a <= 0.0)
  464.     return -1.0;
  465.  
  466.     /* Deal with special cases */
  467.     if (x == 0.0)
  468.     return 0.0;
  469.     if (x > XBIG)
  470.     return 1.0;
  471.  
  472.     /* Check value of factor arg */
  473.     arg = a * log(x) - x - GAMMA(a + 1.0);
  474.     if (arg < MINEXP)
  475.     return -1.0;
  476.     arg = exp(arg);
  477.  
  478.     /* Choose infinite series or continued fraction. */
  479.  
  480.     if ((x > 1.0) && (x >= a + 2.0)) {
  481.     /* Use a continued fraction expansion */
  482.  
  483.     double          rn;
  484.     double          rnold;
  485.  
  486.     aa = 1.0 - a;
  487.     b = aa + x + 1.0;
  488.     pn1 = 1.0;
  489.     pn2 = x;
  490.     pn3 = x + 1.0;
  491.     pn4 = x * b;
  492.     rnold = pn3 / pn4;
  493.  
  494.     for (i = 1; i <= ITMAX; i++) {
  495.  
  496.         aa++;
  497.         b += 2.0;
  498.         an = aa * (double) i;
  499.  
  500.         pn5 = b * pn3 - an * pn1;
  501.         pn6 = b * pn4 - an * pn2;
  502.  
  503.         if (pn6 != 0.0) {
  504.  
  505.         rn = pn5 / pn6;
  506.         if (fabs(rnold - rn) <= min(MACHEPS, MACHEPS * rn))
  507.             return 1.0 - arg * rn * a;
  508.  
  509.         rnold = rn;
  510.         }
  511.         pn1 = pn3;
  512.         pn2 = pn4;
  513.         pn3 = pn5;
  514.         pn4 = pn6;
  515.  
  516.         /* Re-scale terms in continued fraction if terms are large */
  517.         if (fabs(pn5) >= OFLOW) {
  518.  
  519.         pn1 /= OFLOW;
  520.         pn2 /= OFLOW;
  521.         pn3 /= OFLOW;
  522.         pn4 /= OFLOW;
  523.         }
  524.     }
  525.     } else {
  526.     /* Use Pearson's series expansion. */
  527.  
  528.     for (i = 0, aa = a, an = b = 1.0; i <= ITMAX; i++) {
  529.  
  530.         aa++;
  531.         an *= x / aa;
  532.         b += an;
  533.         if (an < b * MACHEPS)
  534.         return arg * b;
  535.     }
  536.     }
  537.     return -1.0;
  538. }
  539.  
  540. /***********************************************************************
  541.      double ranf(double init)
  542.                 RANDom number generator as a Function
  543.      Returns a random floating point number from a uniform distribution
  544.      over 0 - 1 (endpoints of this interval are not returned) using a
  545.      large integer generator.
  546.      This is a transcription from Pascal to Fortran of routine
  547.      Uniform_01 from the paper
  548.      L'Ecuyer, P. and Cote, S. "Implementing a Random Number Package
  549.      with Splitting Facilities." ACM Transactions on Mathematical
  550.      Software, 17:98-111 (1991)
  551.  
  552.                GeNerate LarGe Integer
  553.      Returns a random integer following a uniform distribution over
  554.      (1, 2147483562) using the generator.
  555.      This is a transcription from Pascal to Fortran of routine
  556.      Random from the paper
  557.      L'Ecuyer, P. and Cote, S. "Implementing a Random Number Package
  558.      with Splitting Facilities." ACM Transactions on Mathematical
  559.      Software, 17:98-111 (1991)
  560. ***********************************************************************/
  561. static double          ranf(init)
  562. double init;
  563. {
  564. #ifndef AMIGA_SC_6_1
  565.     /* Already declared static at the top of the file! */
  566.     extern long     Xm1, Xm2, Xa1, Xa2;
  567. #endif /* !AMIGA_SC_6_1 */
  568.     long            k, z;
  569.     static int      firsttime = 1;
  570.     static long     s1, s2;
  571.  
  572.     /* (Re)-Initialize seeds if necessary */
  573.     if (init < 0.0 || firsttime == 1) {
  574.     firsttime = 0;
  575.     s1 = 1234567890L;
  576.     s2 = 1234567890L;
  577.     }
  578.     /* Generate pseudo random integers */
  579.     k = s1 / 53668L;
  580.     s1 = Xa1 * (s1 - k * 53668L) - k * 12211;
  581.     if (s1 < 0)
  582.     s1 += Xm1;
  583.     k = s2 / 52774L;
  584.     s2 = Xa2 * (s2 - k * 52774L) - k * 3791;
  585.     if (s2 < 0)
  586.     s2 += Xm2;
  587.     z = s1 - s2;
  588.     if (z < 1)
  589.     z += (Xm1 - 1);
  590.  
  591.     /*
  592.      * 4.656613057E-10 is 1/Xm1.  Xm1 is set at the top of this file and is
  593.      * currently 2147483563. If Xm1 changes, change this also.
  594.      */
  595.     return (double) 4.656613057E-10 *z;
  596. }
  597.  
  598. /* ----------------------------------------------------------------
  599.    Following to specfun.c made by John Grosh (jgrosh@arl.mil)
  600.    on 28 OCT 1992.
  601.    ---------------------------------------------------------------- */
  602.  
  603. f_normal()    /* Normal or Gaussian Probability Function */
  604. {
  605. struct value a;
  606. double x;
  607.  
  608.     /* ref. Abramowitz and Stegun 1964, "Handbook of Mathematical 
  609.        Functions", Applied Mathematics Series, vol 55,
  610.        Chapter 26, page 934, Eqn. 26.2.29 and Jos van der Woude 
  611.            code found above */
  612.  
  613.     x = real(pop(&a));
  614.  
  615. #ifndef ERF
  616.         x = 0.5 * SQRT_TWO * x;
  617.         x = 0.5 * (1.0 + (x < 0.0 ? -igamma(0.5, x * x) : igamma(0.5, x * x)));
  618. #else
  619.     x = 0.5 * (1.0 + erf(0.5 * SQRT_TWO * x));
  620. #endif
  621.         push( Gcomplex(&a,x,0.0) );
  622. }
  623.  
  624. f_inverse_normal()  /* Inverse normal distribution function */
  625. {
  626. struct value a;
  627. double x, y;
  628. double inverse_normal_func();
  629.  
  630.     x = real(pop(&a));
  631.  
  632.     if (fabs(x) >= 1.0) {
  633.         undefined = TRUE;
  634.         push(Gcomplex(&a,0.0, 0.0));
  635.     } else {
  636.         push( Gcomplex(&a,inverse_normal_func(x), 0.0) );
  637.     }
  638. }
  639.  
  640.  
  641. f_inverse_erf()  /* Inverse error function */
  642. {
  643. struct value a;
  644. double x, y;
  645. double inverse_error_func();
  646.  
  647.     x = real(pop(&a));
  648.  
  649.     if (fabs(x) >= 1.0) {
  650.         undefined = TRUE;
  651.         push(Gcomplex(&a,0.0, 0.0));
  652.     } else {
  653.         push( Gcomplex(&a,inverse_error_func(x), 0.0) );
  654.     }
  655. }
  656.  
  657. double 
  658. inverse_normal_func(p)
  659. double p;
  660. {
  661.     /* 
  662.            Source: This routine was derived (using f2c) from the 
  663.                    FORTRAN subroutine MDNRIS found in 
  664.                    ACM Algorithm 602 obtained from netlib.
  665.  
  666.                    MDNRIS code contains the 1978 Copyright 
  667.                    by IMSL, INC. .  Since MDNRIS has been 
  668.                    submitted to netlib it may be used with 
  669.                    the restriction that it may only be 
  670.                    used for noncommercial purposes and that
  671.                    IMSL be acknowledged as the copyright-holder
  672.                    of the code.
  673.         */
  674.  
  675.     /* Initialized data */
  676.     static double eps = 1e-10;
  677.     static double g0 = 1.851159e-4;
  678.     static double g1 = -.002028152;
  679.     static double g2 = -.1498384;
  680.     static double g3 = .01078639;
  681.     static double h0 = .09952975;
  682.     static double h1 = .5211733;
  683.     static double h2 = -.06888301;
  684.     static double sqrt2 = 1.414213562373095;
  685.  
  686.     /* Local variables */
  687.     static double a, w, x;
  688.     static double sd, wi, sn, y;
  689.  
  690.     double inverse_error_func();
  691.  
  692.     /* Note: 0.0 < p < 1.0 */
  693.  
  694.     /* p too small, compute y directly */
  695.     if (p <= eps) {
  696.         a = p + p;
  697.         w = sqrt(-(double)log(a + (a - a * a)));
  698.  
  699.         /* use a rational function in 1.0 / w */
  700.         wi = 1.0 / w;
  701.         sn = ((g3 * wi + g2) * wi + g1) * wi;
  702.         sd = ((wi + h2) * wi + h1) * wi + h0;
  703.         y = w + w * (g0 + sn / sd);
  704.         y = -y * sqrt2;
  705.     } else {
  706.         x = 1.0 - (p + p);
  707.         y = inverse_error_func(x);
  708.         y = -sqrt2 * y;
  709.     }
  710.     return(y);
  711.  
  712.  
  713. double 
  714. inverse_error_func(p) 
  715. double p;
  716. {
  717.     /* 
  718.            Source: This routine was derived (using f2c) from the 
  719.                    FORTRAN subroutine MERFI found in 
  720.                    ACM Algorithm 602 obtained from netlib.
  721.  
  722.                    MDNRIS code contains the 1978 Copyright 
  723.                    by IMSL, INC. .  Since MERFI has been 
  724.                    submitted to netlib, it may be used with 
  725.                    the restriction that it may only be 
  726.                    used for noncommercial purposes and that
  727.                    IMSL be acknowledged as the copyright-holder
  728.                    of the code.
  729.         */
  730.  
  731.  
  732.  
  733.     /* Initialized data */
  734.     static double a1 = -.5751703;
  735.     static double a2 = -1.896513;
  736.     static double a3 = -.05496261;
  737.     static double b0 = -.113773;
  738.     static double b1 = -3.293474;
  739.     static double b2 = -2.374996;
  740.     static double b3 = -1.187515;
  741.     static double c0 = -.1146666;
  742.     static double c1 = -.1314774;
  743.     static double c2 = -.2368201;
  744.     static double c3 = .05073975;
  745.     static double d0 = -44.27977;
  746.     static double d1 = 21.98546;
  747.     static double d2 = -7.586103;
  748.     static double e0 = -.05668422;
  749.     static double e1 = .3937021;
  750.     static double e2 = -.3166501;
  751.     static double e3 = .06208963;
  752.     static double f0 = -6.266786;
  753.     static double f1 = 4.666263;
  754.     static double f2 = -2.962883;
  755.     static double g0 = 1.851159e-4;
  756.     static double g1 = -.002028152;
  757.     static double g2 = -.1498384;
  758.     static double g3 = .01078639;
  759.     static double h0 = .09952975;
  760.     static double h1 = .5211733;
  761.     static double h2 = -.06888301;
  762.  
  763.     /* Local variables */
  764.     static double a, b, f, w, x, y, z, sigma, z2, sd, wi, sn;
  765.  
  766.     x = p;
  767.  
  768.     /* determine sign of x */
  769.     if (x > 0)
  770.         sigma = 1.0;
  771.     else
  772.         sigma = -1.0;
  773.  
  774.     /* Note: -1.0 < x < 1.0 */
  775.  
  776.     z = fabs(x);
  777.  
  778.     /* z between 0.0 and 0.85, approx. f by a 
  779.        rational function in z  */
  780.  
  781.     if (z <= 0.85) {
  782.         z2 = z * z;
  783.         f = z + z * (b0 + a1 * z2 / (b1 + z2 + a2 
  784.             / (b2 + z2 + a3 / (b3 + z2))));
  785.  
  786.     /* z greater than 0.85 */
  787.     } else {
  788.         a = 1.0 - z;
  789.         b = z;
  790.  
  791.         /* reduced argument is in (0.85,1.0), 
  792.            obtain the transformed variable */
  793.  
  794.         w = sqrt(-(double)log(a + a * b));
  795.  
  796.         /* w greater than 4.0, approx. f by a 
  797.            rational function in 1.0 / w */
  798.  
  799.         if (w >= 4.0) {
  800.             wi = 1.0 / w;
  801.             sn = ((g3 * wi + g2) * wi + g1) * wi;
  802.             sd = ((wi + h2) * wi + h1) * wi + h0;
  803.             f = w + w * (g0 + sn / sd);
  804.  
  805.         /* w between 2.5 and 4.0, approx. 
  806.            f by a rational function in w */
  807.  
  808.         } else if (w < 4.0 && w > 2.5) {
  809.             sn = ((e3 * w + e2) * w + e1) * w;
  810.             sd = ((w + f2) * w + f1) * w + f0;
  811.             f = w + w * (e0 + sn / sd);
  812.  
  813.         /* w between 1.13222 and 2.5, approx. f by 
  814.            a rational function in w */
  815.         } else if (w <= 2.5 && w > 1.13222) {
  816.             sn = ((c3 * w + c2) * w + c1) * w;
  817.             sd = ((w + d2) * w + d1) * w + d0;
  818.             f = w + w * (c0 + sn / sd);
  819.         }
  820.     }
  821.     y = sigma * f;
  822.     return(y);
  823. }
  824.  
  825.