CHI2.C 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /****************************************************************************
  2. * chi2.c
  3. *
  4. * This module contains the function for the chi square distribution.
  5. *
  6. * All functions in this module are taken from the Cephes math library.
  7. *
  8. * Cephes Math Library Release 2.0: April, 1987
  9. * Copyright 1984, 1987 by Stephen L. Moshier
  10. * Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  11. *
  12. * from Persistence of Vision(tm) Ray Tracer
  13. * Copyright 1996,1999 Persistence of Vision Team
  14. *---------------------------------------------------------------------------
  15. * NOTICE: This source code file is provided so that users may experiment
  16. * with enhancements to POV-Ray and to port the software to platforms other
  17. * than those supported by the POV-Ray Team. There are strict rules under
  18. * which you are permitted to use this file. The rules are in the file
  19. * named POVLEGAL.DOC which should be distributed with this file.
  20. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  21. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  22. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  23. *
  24. * This program is based on the popular DKB raytracer version 2.12.
  25. * DKBTrace was originally written by David K. Buck.
  26. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  27. *
  28. *****************************************************************************/
  29. #include "frame.h"
  30. #include "povproto.h"
  31. #include "chi2.h"
  32. /*
  33. Cephes Math Library Release 2.0: April, 1987
  34. Copyright 1984, 1987 by Stephen L. Moshier
  35. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  36. */
  37. /*****************************************************************************
  38. * Local preprocessor defines
  39. ******************************************************************************/
  40. #define MAXLGM 2.556348e305
  41. #define BIG 1.44115188075855872E+17
  42. #define MACHEP 1.38777878078144567553E-17 /* 2**-56 */
  43. #define MAXLOG 8.8029691931113054295988E1 /* log(2**127) */
  44. #define MAXNUM 1.701411834604692317316873e38 /* 2**127 */
  45. #define LOGPI 1.14472988584940017414
  46. /*****************************************************************************
  47. * Local typedefs
  48. ******************************************************************************/
  49. /*****************************************************************************
  50. * Local variables
  51. ******************************************************************************/
  52. static int sgngam = 0;
  53. /*
  54. * A[]: Stirling's formula expansion of log gamma
  55. * B[], C[]: log gamma function between 2 and 3
  56. */
  57. static DBL A[] =
  58. {
  59. 8.11614167470508450300E-4,
  60. -5.95061904284301438324E-4,
  61. 7.93650340457716943945E-4,
  62. -2.77777777730099687205E-3,
  63. 8.33333333333331927722E-2
  64. };
  65. static DBL B[] =
  66. {
  67. -1.37825152569120859100E3,
  68. -3.88016315134637840924E4,
  69. -3.31612992738871184744E5,
  70. -1.16237097492762307383E6,
  71. -1.72173700820839662146E6,
  72. -8.53555664245765465627E5
  73. };
  74. static DBL C[] =
  75. {
  76. 1.00000000000000000000E0,
  77. -3.51815701436523470549E2,
  78. -1.70642106651881159223E4,
  79. -2.20528590553854454839E5,
  80. -1.13933444367982507207E6,
  81. -2.53252307177582951285E6,
  82. -2.01889141433532773231E6
  83. };
  84. /* log(sqrt(2pi)) */
  85. static DBL LS2PI = 0.91893853320467274178;
  86. /* sqrt(2pi) */
  87. static DBL s2pi = 2.50662827463100050242E0;
  88. /* approximation for 0 <= |y - 0.5| <= 3/8 */
  89. static DBL P0[5] =
  90. {
  91. -5.99633501014107895267E1,
  92. 9.80010754185999661536E1,
  93. -5.66762857469070293439E1,
  94. 1.39312609387279679503E1,
  95. -1.23916583867381258016E0,
  96. };
  97. static DBL Q0[8] =
  98. {
  99. /* 1.00000000000000000000E0,*/
  100. 1.95448858338141759834E0,
  101. 4.67627912898881538453E0,
  102. 8.63602421390890590575E1,
  103. -2.25462687854119370527E2,
  104. 2.00260212380060660359E2,
  105. -8.20372256168333339912E1,
  106. 1.59056225126211695515E1,
  107. -1.18331621121330003142E0,
  108. };
  109. /*
  110. * Approximation for interval z = sqrt(-2 log y ) between 2 and 8
  111. * i.e., y between exp(-2) = .135 and exp(-32) = 1.27e-14.
  112. */
  113. static DBL P1[9] =
  114. {
  115. 4.05544892305962419923E0,
  116. 3.15251094599893866154E1,
  117. 5.71628192246421288162E1,
  118. 4.40805073893200834700E1,
  119. 1.46849561928858024014E1,
  120. 2.18663306850790267539E0,
  121. -1.40256079171354495875E-1,
  122. -3.50424626827848203418E-2,
  123. -8.57456785154685413611E-4,
  124. };
  125. static DBL Q1[8] =
  126. {
  127. /* 1.00000000000000000000E0,*/
  128. 1.57799883256466749731E1,
  129. 4.53907635128879210584E1,
  130. 4.13172038254672030440E1,
  131. 1.50425385692907503408E1,
  132. 2.50464946208309415979E0,
  133. -1.42182922854787788574E-1,
  134. -3.80806407691578277194E-2,
  135. -9.33259480895457427372E-4,
  136. };
  137. /*
  138. * Approximation for interval z = sqrt(-2 log y ) between 8 and 64
  139. * i.e., y between exp(-32) = 1.27e-14 and exp(-2048) = 3.67e-890.
  140. */
  141. static DBL P2[9] =
  142. {
  143. 3.23774891776946035970E0,
  144. 6.91522889068984211695E0,
  145. 3.93881025292474443415E0,
  146. 1.33303460815807542389E0,
  147. 2.01485389549179081538E-1,
  148. 1.23716634817820021358E-2,
  149. 3.01581553508235416007E-4,
  150. 2.65806974686737550832E-6,
  151. 6.23974539184983293730E-9,
  152. };
  153. static DBL Q2[8] =
  154. {
  155. /* 1.00000000000000000000E0,*/
  156. 6.02427039364742014255E0,
  157. 3.67983563856160859403E0,
  158. 1.37702099489081330271E0,
  159. 2.16236993594496635890E-1,
  160. 1.34204006088543189037E-2,
  161. 3.28014464682127739104E-4,
  162. 2.89247864745380683936E-6,
  163. 6.79019408009981274425E-9,
  164. };
  165. /*****************************************************************************
  166. * Static functions
  167. ******************************************************************************/
  168. static DBL igami (DBL a, DBL y0);
  169. static DBL lgam (DBL x);
  170. static DBL polevl (DBL x, DBL * coef, int N);
  171. static DBL p1evl (DBL x, DBL * coef, int N);
  172. static DBL igamc (DBL a, DBL x);
  173. static DBL igam (DBL a, DBL x);
  174. static DBL ndtri (DBL y0);
  175. /* chdtri()
  176. *
  177. * Inverse of complemented Chi-square distribution
  178. *
  179. *
  180. *
  181. * SYNOPSIS:
  182. *
  183. * DBL df, x, y, chdtri();
  184. *
  185. * x = chdtri( df, y );
  186. *
  187. *
  188. *
  189. *
  190. * DESCRIPTION:
  191. *
  192. * Finds the Chi-square argument x such that the integral
  193. * from x to infinity of the Chi-square density is equal
  194. * to the given cumulative probability y.
  195. *
  196. * This is accomplished using the inverse gamma integral
  197. * function and the relation
  198. *
  199. * x/2 = igami( df/2, y );
  200. *
  201. *
  202. *
  203. *
  204. * ACCURACY:
  205. *
  206. * See igami.c.
  207. *
  208. * ERROR MESSAGES:
  209. *
  210. * message condition value returned
  211. * chdtri domain y < 0 or y > 1 0.0
  212. * v < 1
  213. *
  214. */
  215. DBL chdtri(DBL df, DBL y)
  216. {
  217. DBL x;
  218. if ((y < 0.0) || (y > 1.0) || (df < 1.0))
  219. {
  220. Error("Illegal values fd=%f and y=%f in chdtri().\n", df, y);
  221. return (0.0);
  222. }
  223. x = igami(0.5 * df, y);
  224. return (2.0 * x);
  225. }
  226. /* lgam()
  227. *
  228. * Natural logarithm of gamma function
  229. *
  230. *
  231. *
  232. * SYNOPSIS:
  233. *
  234. * DBL x, y, lgam();
  235. * extern int sgngam;
  236. *
  237. * y = lgam( x );
  238. *
  239. *
  240. *
  241. * DESCRIPTION:
  242. *
  243. * Returns the base e (2.718...) logarithm of the absolute
  244. * value of the gamma function of the argument.
  245. * The sign (+1 or -1) of the gamma function is returned in a
  246. * global (extern) variable named sgngam.
  247. *
  248. * For arguments greater than 13, the logarithm of the gamma
  249. * function is approximated by the logarithmic version of
  250. * Stirling's formula using a polynomial approximation of
  251. * degree 4. Arguments between -33 and +33 are reduced by
  252. * recurrence to the interval [2,3] of a rational approximation.
  253. * The cosecant reflection formula is employed for arguments
  254. * less than -33.
  255. *
  256. * Arguments greater than MAXLGM return MAXNUM and an error
  257. * message. MAXLGM = 2.035093e36 for DEC
  258. * arithmetic or 2.556348e305 for IEEE arithmetic.
  259. *
  260. *
  261. *
  262. * ACCURACY:
  263. *
  264. *
  265. * arithmetic domain # trials peak rms
  266. * DEC 0, 3 7000 5.2e-17 1.3e-17
  267. * DEC 2.718, 2.035e36 5000 3.9e-17 9.9e-18
  268. * IEEE 0, 3 28000 5.4e-16 1.1e-16
  269. * IEEE 2.718, 2.556e305 40000 3.5e-16 8.3e-17
  270. * The error criterion was relative when the function magnitude
  271. * was greater than one but absolute when it was less than one.
  272. *
  273. * The following test used the relative error criterion, though
  274. * at certain points the relative error could be much higher than
  275. * indicated.
  276. * IEEE -200, -4 10000 4.8e-16 1.3e-16
  277. *
  278. */
  279. static DBL lgam(DBL x)
  280. {
  281. DBL p, q, w, z;
  282. int i;
  283. sgngam = 1;
  284. if (x < -34.0)
  285. {
  286. q = -x;
  287. w = lgam(q); /* note this modifies sgngam! */
  288. p = floor(q);
  289. if (p == q)
  290. {
  291. goto loverf;
  292. }
  293. i = p;
  294. if ((i & 1) == 0)
  295. {
  296. sgngam = -1;
  297. }
  298. else
  299. {
  300. sgngam = 1;
  301. }
  302. z = q - p;
  303. if (z > 0.5)
  304. {
  305. p += 1.0;
  306. z = p - q;
  307. }
  308. z = q * sin(M_PI * z);
  309. if (z == 0.0)
  310. {
  311. goto loverf;
  312. }
  313. /* z = log(M_PI) - log( z ) - w;*/
  314. z = LOGPI - log(z) - w;
  315. return (z);
  316. }
  317. if (x < 13.0)
  318. {
  319. z = 1.0;
  320. while (x >= 3.0)
  321. {
  322. x -= 1.0;
  323. z *= x;
  324. }
  325. while (x < 2.0)
  326. {
  327. if (x == 0.0)
  328. {
  329. goto loverf;
  330. }
  331. z /= x;
  332. x += 1.0;
  333. }
  334. if (z < 0.0)
  335. {
  336. sgngam = -1;
  337. z = -z;
  338. }
  339. else
  340. {
  341. sgngam = 1;
  342. }
  343. if (x == 2.0)
  344. {
  345. return (log(z));
  346. }
  347. x -= 2.0;
  348. p = x * polevl(x, B, 5) / p1evl(x, C, 6);
  349. return (log(z) + p);
  350. }
  351. if (x > MAXLGM)
  352. {
  353. loverf:
  354. /*
  355. mtherr("lgam", OVERFLOW);
  356. */
  357. return (sgngam * MAXNUM);
  358. }
  359. q = (x - 0.5) * log(x) - x + LS2PI;
  360. if (x > 1.0e8)
  361. {
  362. return (q);
  363. }
  364. p = 1.0 / (x * x);
  365. if (x >= 1000.0)
  366. {
  367. q += ((7.9365079365079365079365e-4 * p -
  368. 2.7777777777777777777778e-3) * p +
  369. 0.0833333333333333333333) / x;
  370. }
  371. else
  372. {
  373. q += polevl(p, A, 4) / x;
  374. }
  375. return (q);
  376. }
  377. /* igamc()
  378. *
  379. * Complemented incomplete gamma integral
  380. *
  381. *
  382. *
  383. * SYNOPSIS:
  384. *
  385. * DBL a, x, y, igamc();
  386. *
  387. * y = igamc( a, x );
  388. *
  389. *
  390. *
  391. * DESCRIPTION:
  392. *
  393. * The function is defined by
  394. *
  395. *
  396. * igamc(a,x) = 1 - igam(a,x)
  397. *
  398. * inf.
  399. * -
  400. * 1 | | -t a-1
  401. * = ----- | e t dt.
  402. * - | |
  403. * | (a) -
  404. * x
  405. *
  406. *
  407. * In this implementation both arguments must be positive.
  408. * The integral is evaluated by either a power series or
  409. * continued fraction expansion, depending on the relative
  410. * values of a and x.
  411. *
  412. *
  413. *
  414. * ACCURACY:
  415. *
  416. * Relative error:
  417. * arithmetic domain # trials peak rms
  418. * DEC 0,30 2000 2.7e-15 4.0e-16
  419. * IEEE 0,30 60000 1.4e-12 6.3e-15
  420. *
  421. */
  422. static DBL igamc(DBL a, DBL x)
  423. {
  424. DBL ans, c, yc, ax, y, z;
  425. DBL pk, pkm1, pkm2, qk, qkm1, qkm2;
  426. DBL r, t;
  427. if ((x <= 0) || (a <= 0))
  428. {
  429. return (1.0);
  430. }
  431. if ((x < 1.0) || (x < a))
  432. {
  433. return (1.0 - igam(a, x));
  434. }
  435. ax = a * log(x) - x - lgam(a);
  436. if (ax < -MAXLOG)
  437. {
  438. /*
  439. mtherr("igamc", UNDERFLOW);
  440. */
  441. return (0.0);
  442. }
  443. ax = exp(ax);
  444. /* continued fraction */
  445. y = 1.0 - a;
  446. z = x + y + 1.0;
  447. c = 0.0;
  448. pkm2 = 1.0;
  449. qkm2 = x;
  450. pkm1 = x + 1.0;
  451. qkm1 = z * x;
  452. ans = pkm1 / qkm1;
  453. do
  454. {
  455. c += 1.0;
  456. y += 1.0;
  457. z += 2.0;
  458. yc = y * c;
  459. pk = pkm1 * z - pkm2 * yc;
  460. qk = qkm1 * z - qkm2 * yc;
  461. if (qk != 0)
  462. {
  463. r = pk / qk;
  464. t = fabs((ans - r) / r);
  465. ans = r;
  466. }
  467. else
  468. {
  469. t = 1.0;
  470. }
  471. pkm2 = pkm1;
  472. pkm1 = pk;
  473. qkm2 = qkm1;
  474. qkm1 = qk;
  475. if (fabs(pk) > BIG)
  476. {
  477. pkm2 /= BIG;
  478. pkm1 /= BIG;
  479. qkm2 /= BIG;
  480. qkm1 /= BIG;
  481. }
  482. }
  483. while (t > MACHEP);
  484. return (ans * ax);
  485. }
  486. /* igam.c
  487. *
  488. * Incomplete gamma integral
  489. *
  490. *
  491. *
  492. * SYNOPSIS:
  493. *
  494. * DBL a, x, y, igam();
  495. *
  496. * y = igam( a, x );
  497. *
  498. *
  499. *
  500. * DESCRIPTION:
  501. *
  502. * The function is defined by
  503. *
  504. * x
  505. * -
  506. * 1 | | -t a-1
  507. * igam(a,x) = ----- | e t dt.
  508. * - | |
  509. * | (a) -
  510. * 0
  511. *
  512. *
  513. * In this implementation both arguments must be positive.
  514. * The integral is evaluated by either a power series or
  515. * continued fraction expansion, depending on the relative
  516. * values of a and x.
  517. *
  518. *
  519. *
  520. * ACCURACY:
  521. *
  522. * Relative error:
  523. * arithmetic domain # trials peak rms
  524. * DEC 0,30 4000 4.4e-15 6.3e-16
  525. * IEEE 0,30 10000 3.6e-14 5.1e-15
  526. *
  527. */
  528. /* left tail of incomplete gamma function:
  529. *
  530. * inf. k
  531. * a -x - x
  532. * x e > ----------
  533. * - -
  534. * k=0 | (a+k+1)
  535. *
  536. */
  537. static DBL igam(DBL a, DBL x)
  538. {
  539. DBL ans, ax, c, r;
  540. if ((x <= 0) || (a <= 0))
  541. {
  542. return (0.0);
  543. }
  544. if ((x > 1.0) && (x > a))
  545. {
  546. return (1.0 - igamc(a, x));
  547. }
  548. /* Compute x**a * exp(-x) / gamma(a) */
  549. ax = a * log(x) - x - lgam(a);
  550. if (ax < -MAXLOG)
  551. {
  552. /*
  553. mtherr("igam", UNDERFLOW);
  554. */
  555. return (0.0);
  556. }
  557. ax = exp(ax);
  558. /* power series */
  559. r = a;
  560. c = 1.0;
  561. ans = 1.0;
  562. do
  563. {
  564. r += 1.0;
  565. c *= x / r;
  566. ans += c;
  567. }
  568. while (c / ans > MACHEP);
  569. return (ans * ax / a);
  570. }
  571. /* igami()
  572. *
  573. * Inverse of complemented imcomplete gamma integral
  574. *
  575. *
  576. *
  577. * SYNOPSIS:
  578. *
  579. * DBL a, x, y, igami();
  580. *
  581. * x = igami( a, y );
  582. *
  583. *
  584. *
  585. * DESCRIPTION:
  586. *
  587. * Given y, the function finds x such that
  588. *
  589. * igamc( a, x ) = y.
  590. *
  591. * Starting with the approximate value
  592. *
  593. * 3
  594. * x = a t
  595. *
  596. * where
  597. *
  598. * t = 1 - d - ndtri(y) sqrt(d)
  599. *
  600. * and
  601. *
  602. * d = 1/9a,
  603. *
  604. * the routine performs up to 10 Newton iterations to find the
  605. * root of igamc(a,x) - y = 0.
  606. *
  607. *
  608. * ACCURACY:
  609. *
  610. * Tested for a ranging from 0.5 to 30 and x from 0 to 0.5.
  611. *
  612. * Relative error:
  613. * arithmetic domain # trials peak rms
  614. * DEC 0,0.5 3400 8.8e-16 1.3e-16
  615. * IEEE 0,0.5 10000 1.1e-14 1.0e-15
  616. *
  617. */
  618. static DBL igami(DBL a, DBL y0)
  619. {
  620. DBL d, y, x0, lgm;
  621. int i;
  622. /* approximation to inverse function */
  623. d = 1.0 / (9.0 * a);
  624. y = (1.0 - d - ndtri(y0) * sqrt(d));
  625. x0 = a * y * y * y;
  626. lgm = lgam(a);
  627. for (i = 0; i < 10; i++)
  628. {
  629. if (x0 <= 0.0)
  630. {
  631. /*
  632. mtherr("igami", UNDERFLOW);
  633. */
  634. return (0.0);
  635. }
  636. y = igamc(a, x0);
  637. /* compute the derivative of the function at this point */
  638. d = (a - 1.0) * log(x0) - x0 - lgm;
  639. if (d < -MAXLOG)
  640. {
  641. /*
  642. mtherr("igami", UNDERFLOW);
  643. */
  644. goto done;
  645. }
  646. d = -exp(d);
  647. /* compute the step to the next approximation of x */
  648. if (d == 0.0)
  649. {
  650. goto done;
  651. }
  652. d = (y - y0) / d;
  653. x0 = x0 - d;
  654. if (i < 3)
  655. {
  656. continue;
  657. }
  658. if (fabs(d / x0) < 2.0 * MACHEP)
  659. {
  660. goto done;
  661. }
  662. }
  663. done:
  664. return (x0);
  665. }
  666. /* ndtri.c
  667. *
  668. * Inverse of Normal distribution function
  669. *
  670. *
  671. *
  672. * SYNOPSIS:
  673. *
  674. * DBL x, y, ndtri();
  675. *
  676. * x = ndtri( y );
  677. *
  678. *
  679. *
  680. * DESCRIPTION:
  681. *
  682. * Returns the argument, x, for which the area under the
  683. * Gaussian probability density function (integrated from
  684. * minus infinity to x) is equal to y.
  685. *
  686. *
  687. * For small arguments 0 < y < exp(-2), the program computes
  688. * z = sqrt( -2.0 * log(y) ); then the approximation is
  689. * x = z - log(z)/z - (1/z) P(1/z) / Q(1/z).
  690. * There are two rational functions P/Q, one for 0 < y < exp(-32)
  691. * and the other for y up to exp(-2). For larger arguments,
  692. * w = y - 0.5, and x/sqrt(2pi) = w + w**3 R(w**2)/S(w**2)).
  693. *
  694. *
  695. * ACCURACY:
  696. *
  697. * Relative error:
  698. * arithmetic domain # trials peak rms
  699. * DEC 0.125, 1 5500 9.5e-17 2.1e-17
  700. * DEC 6e-39, 0.135 3500 5.7e-17 1.3e-17
  701. * IEEE 0.125, 1 20000 7.2e-16 1.3e-16
  702. * IEEE 3e-308, 0.135 50000 4.6e-16 9.8e-17
  703. *
  704. *
  705. * ERROR MESSAGES:
  706. *
  707. * message condition value returned
  708. * ndtri domain x <= 0 -MAXNUM
  709. * ndtri domain x >= 1 MAXNUM
  710. *
  711. */
  712. static DBL ndtri(DBL y0)
  713. {
  714. DBL x, y, z, y2, x0, x1;
  715. int code;
  716. if (y0 <= 0.0)
  717. {
  718. /*
  719. mtherr("ndtri", DOMAIN);
  720. */
  721. return (-MAXNUM);
  722. }
  723. if (y0 >= 1.0)
  724. {
  725. /*
  726. mtherr("ndtri", DOMAIN);
  727. */
  728. return (MAXNUM);
  729. }
  730. code = 1;
  731. y = y0;
  732. if (y > (1.0 - 0.13533528323661269189)) /* 0.135... = exp(-2) */
  733. {
  734. y = 1.0 - y;
  735. code = 0;
  736. }
  737. if (y > 0.13533528323661269189)
  738. {
  739. y = y - 0.5;
  740. y2 = y * y;
  741. x = y + y * (y2 * polevl(y2, P0, 4) / p1evl(y2, Q0, 8));
  742. x = x * s2pi;
  743. return (x);
  744. }
  745. x = sqrt(-2.0 * log(y));
  746. x0 = x - log(x) / x;
  747. z = 1.0 / x;
  748. if (x < 8.0) /* y > exp(-32) = 1.2664165549e-14 */
  749. {
  750. x1 = z * polevl(z, P1, 8) / p1evl(z, Q1, 8);
  751. }
  752. else
  753. {
  754. x1 = z * polevl(z, P2, 8) / p1evl(z, Q2, 8);
  755. }
  756. x = x0 - x1;
  757. if (code != 0)
  758. {
  759. x = -x;
  760. }
  761. return (x);
  762. }
  763. /* polevl.c
  764. * p1evl.c
  765. *
  766. * Evaluate polynomial
  767. *
  768. *
  769. *
  770. * SYNOPSIS:
  771. *
  772. * int N;
  773. * DBL x, y, coef[N+1], polevl[];
  774. *
  775. * y = polevl( x, coef, N );
  776. *
  777. *
  778. *
  779. * DESCRIPTION:
  780. *
  781. * Evaluates polynomial of degree N:
  782. *
  783. * 2 N
  784. * y = C + C x + C x +...+ C x
  785. * 0 1 2 N
  786. *
  787. * Coefficients are stored in reverse order:
  788. *
  789. * coef[0] = C , ..., coef[N] = C .
  790. * N 0
  791. *
  792. * The function p1evl() assumes that coef[N] = 1.0 and is
  793. * omitted from the array. Its calling arguments are
  794. * otherwise the same as polevl().
  795. *
  796. *
  797. * SPEED:
  798. *
  799. * In the interest of speed, there are no checks for out
  800. * of bounds arithmetic. This routine is used by most of
  801. * the functions in the library. Depending on available
  802. * equipment features, the user may wish to rewrite the
  803. * program in microcode or assembly language.
  804. *
  805. */
  806. static DBL polevl(DBL x, DBL coef[], int N)
  807. {
  808. DBL ans;
  809. int i;
  810. DBL *p;
  811. p = coef;
  812. ans = *p++;
  813. i = N;
  814. do
  815. {
  816. ans = ans * x + *p++;
  817. }
  818. while (--i);
  819. return (ans);
  820. }
  821. /* p1evl() */
  822. /* N
  823. * Evaluate polynomial when coefficient of x is 1.0.
  824. * Otherwise same as polevl.
  825. */
  826. static DBL p1evl(DBL x, DBL coef[], int N)
  827. {
  828. DBL ans;
  829. DBL *p;
  830. int i;
  831. p = coef;
  832. ans = x + *p++;
  833. i = N - 1;
  834. do
  835. {
  836. ans = ans * x + *p++;
  837. }
  838. while (--i);
  839. return (ans);
  840. }