00001 #include <cstdlib>
00002 #include <cmath>
00003
00004 using namespace std;
00005
00006 #include "SprChiCdf.hh"
00007
00008
00009
00010 void SprChiCdf::cumchi ( double *x, double *df, double *cum, double *ccum )
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 {
00031 static double a;
00032 static double xx;
00033
00034 a = *df * 0.5;
00035 xx = *x * 0.5;
00036 cumgam ( &xx, &a, cum, ccum );
00037 return;
00038 }
00039
00040
00041 void SprChiCdf::cumgam ( double *x, double *a, double *cum, double *ccum )
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 {
00071 static int K1 = 0;
00072
00073 if(!(*x <= 0.0e0)) goto S10;
00074 *cum = 0.0e0;
00075 *ccum = 1.0e0;
00076 return;
00077 S10:
00078 gamma_inc ( a, x, cum, ccum, &K1 );
00079
00080
00081
00082 return;
00083 }
00084
00085
00086 double SprChiCdf::dpmpar ( int *i )
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 {
00118 static int K1 = 4;
00119 static int K2 = 8;
00120 static int K3 = 9;
00121 static int K4 = 10;
00122 static double value,b,binv,bm1,one,w,z;
00123 static int emax,emin,ibeta,m;
00124
00125 if(*i > 1) goto S10;
00126 b = ipmpar(&K1);
00127 m = ipmpar(&K2);
00128 value = pow(b,(double)(1-m));
00129 return value;
00130 S10:
00131 if(*i > 2) goto S20;
00132 b = ipmpar(&K1);
00133 emin = ipmpar(&K3);
00134 one = 1.0;
00135 binv = one/b;
00136 w = pow(b,(double)(emin+2));
00137 value = w*binv*binv*binv;
00138 return value;
00139 S20:
00140 ibeta = ipmpar(&K1);
00141 m = ipmpar(&K2);
00142 emax = ipmpar(&K4);
00143 b = ibeta;
00144 bm1 = ibeta-1;
00145 one = 1.0;
00146 z = pow(b,(double)(m-1));
00147 w = ((z-one)*b+bm1)/(b*z);
00148 z = pow(b,(double)(emax-2));
00149 value = w*z*b*b;
00150 return value;
00151 }
00152
00153
00154 double SprChiCdf::erf1 ( double *x )
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 {
00169 static double c = .564189583547756e0;
00170 static double a[5] = {
00171 .771058495001320e-04,-.133733772997339e-02,.323076579225834e-01,
00172 .479137145607681e-01,.128379167095513e+00
00173 };
00174 static double b[3] = {
00175 .301048631703895e-02,.538971687740286e-01,.375795757275549e+00
00176 };
00177 static double p[8] = {
00178 -1.36864857382717e-07,5.64195517478974e-01,7.21175825088309e+00,
00179 4.31622272220567e+01,1.52989285046940e+02,3.39320816734344e+02,
00180 4.51918953711873e+02,3.00459261020162e+02
00181 };
00182 static double q[8] = {
00183 1.00000000000000e+00,1.27827273196294e+01,7.70001529352295e+01,
00184 2.77585444743988e+02,6.38980264465631e+02,9.31354094850610e+02,
00185 7.90950925327898e+02,3.00459260956983e+02
00186 };
00187 static double r[5] = {
00188 2.10144126479064e+00,2.62370141675169e+01,2.13688200555087e+01,
00189 4.65807828718470e+00,2.82094791773523e-01
00190 };
00191 static double s[4] = {
00192 9.41537750555460e+01,1.87114811799590e+02,9.90191814623914e+01,
00193 1.80124575948747e+01
00194 };
00195 static double erf1,ax,bot,t,top,x2;
00196
00197 ax = fabs(*x);
00198 if(ax > 0.5e0) goto S10;
00199 t = *x**x;
00200 top = (((a[0]*t+a[1])*t+a[2])*t+a[3])*t+a[4]+1.0e0;
00201 bot = ((b[0]*t+b[1])*t+b[2])*t+1.0e0;
00202 erf1 = *x*(top/bot);
00203 return erf1;
00204 S10:
00205 if(ax > 4.0e0) goto S20;
00206 top = ((((((p[0]*ax+p[1])*ax+p[2])*ax+p[3])*ax+p[4])*ax+p[5])*ax+p[6])*ax+p[
00207 7];
00208 bot = ((((((q[0]*ax+q[1])*ax+q[2])*ax+q[3])*ax+q[4])*ax+q[5])*ax+q[6])*ax+q[
00209 7];
00210 erf1 = 0.5e0+(0.5e0-exp(-(*x**x))*top/bot);
00211 if(*x < 0.0e0) erf1 = -erf1;
00212 return erf1;
00213 S20:
00214 if(ax >= 5.8e0) goto S30;
00215 x2 = *x**x;
00216 t = 1.0e0/x2;
00217 top = (((r[0]*t+r[1])*t+r[2])*t+r[3])*t+r[4];
00218 bot = (((s[0]*t+s[1])*t+s[2])*t+s[3])*t+1.0e0;
00219 erf1 = (c-top/(x2*bot))/ax;
00220 erf1 = 0.5e0+(0.5e0-exp(-x2)*erf1);
00221 if(*x < 0.0e0) erf1 = -erf1;
00222 return erf1;
00223 S30:
00224 erf1 = fifdsign(1.0e0,*x);
00225 return erf1;
00226 }
00227
00228
00229 double SprChiCdf::erfc1 ( int *ind, double *x )
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 {
00253 static double c = .564189583547756e0;
00254 static double a[5] = {
00255 .771058495001320e-04,-.133733772997339e-02,.323076579225834e-01,
00256 .479137145607681e-01,.128379167095513e+00
00257 };
00258 static double b[3] = {
00259 .301048631703895e-02,.538971687740286e-01,.375795757275549e+00
00260 };
00261 static double p[8] = {
00262 -1.36864857382717e-07,5.64195517478974e-01,7.21175825088309e+00,
00263 4.31622272220567e+01,1.52989285046940e+02,3.39320816734344e+02,
00264 4.51918953711873e+02,3.00459261020162e+02
00265 };
00266 static double q[8] = {
00267 1.00000000000000e+00,1.27827273196294e+01,7.70001529352295e+01,
00268 2.77585444743988e+02,6.38980264465631e+02,9.31354094850610e+02,
00269 7.90950925327898e+02,3.00459260956983e+02
00270 };
00271 static double r[5] = {
00272 2.10144126479064e+00,2.62370141675169e+01,2.13688200555087e+01,
00273 4.65807828718470e+00,2.82094791773523e-01
00274 };
00275 static double s[4] = {
00276 9.41537750555460e+01,1.87114811799590e+02,9.90191814623914e+01,
00277 1.80124575948747e+01
00278 };
00279 static int K1 = 1;
00280 static double erfc1,ax,bot,e,t,top,w;
00281
00282
00283
00284
00285 ax = fabs(*x);
00286 if(ax > 0.5e0) goto S10;
00287 t = *x**x;
00288 top = (((a[0]*t+a[1])*t+a[2])*t+a[3])*t+a[4]+1.0e0;
00289 bot = ((b[0]*t+b[1])*t+b[2])*t+1.0e0;
00290 erfc1 = 0.5e0+(0.5e0-*x*(top/bot));
00291 if(*ind != 0) erfc1 = exp(t)*erfc1;
00292 return erfc1;
00293 S10:
00294
00295
00296
00297 if(ax > 4.0e0) goto S20;
00298 top = ((((((p[0]*ax+p[1])*ax+p[2])*ax+p[3])*ax+p[4])*ax+p[5])*ax+p[6])*ax+p[
00299 7];
00300 bot = ((((((q[0]*ax+q[1])*ax+q[2])*ax+q[3])*ax+q[4])*ax+q[5])*ax+q[6])*ax+q[
00301 7];
00302 erfc1 = top/bot;
00303 goto S40;
00304 S20:
00305
00306
00307
00308 if(*x <= -5.6e0) goto S60;
00309 if(*ind != 0) goto S30;
00310 if(*x > 100.0e0) goto S70;
00311 if(*x**x > -exparg(&K1)) goto S70;
00312 S30:
00313 t = pow(1.0e0/ *x,2.0);
00314 top = (((r[0]*t+r[1])*t+r[2])*t+r[3])*t+r[4];
00315 bot = (((s[0]*t+s[1])*t+s[2])*t+s[3])*t+1.0e0;
00316 erfc1 = (c-t*top/bot)/ax;
00317 S40:
00318
00319
00320
00321 if(*ind == 0) goto S50;
00322 if(*x < 0.0e0) erfc1 = 2.0e0*exp(*x**x)-erfc1;
00323 return erfc1;
00324 S50:
00325 w = *x**x;
00326 t = w;
00327 e = w-t;
00328 erfc1 = (0.5e0+(0.5e0-e))*exp(-t)*erfc1;
00329 if(*x < 0.0e0) erfc1 = 2.0e0-erfc1;
00330 return erfc1;
00331 S60:
00332
00333
00334
00335 erfc1 = 2.0e0;
00336 if(*ind != 0) erfc1 = 2.0e0*exp(*x**x);
00337 return erfc1;
00338 S70:
00339
00340
00341
00342
00343 erfc1 = 0.0e0;
00344 return erfc1;
00345 }
00346
00347
00348 double SprChiCdf::exparg ( int *l )
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373 {
00374 static int K1 = 4;
00375 static int K2 = 9;
00376 static int K3 = 10;
00377 static double exparg,lnb;
00378 static int b,m;
00379
00380 b = ipmpar(&K1);
00381 if(b != 2) goto S10;
00382 lnb = .69314718055995e0;
00383 goto S40;
00384 S10:
00385 if(b != 8) goto S20;
00386 lnb = 2.0794415416798e0;
00387 goto S40;
00388 S20:
00389 if(b != 16) goto S30;
00390 lnb = 2.7725887222398e0;
00391 goto S40;
00392 S30:
00393 lnb = log((double)b);
00394 S40:
00395 if(*l == 0) goto S50;
00396 m = ipmpar(&K2)-1;
00397 exparg = 0.99999e0*((double)m*lnb);
00398 return exparg;
00399 S50:
00400 m = ipmpar(&K3);
00401 exparg = 0.99999e0*((double)m*lnb);
00402 return exparg;
00403 }
00404
00405
00406 double SprChiCdf::fifdmax1 ( double a, double b )
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419 {
00420 if ( a < b )
00421 {
00422 return b;
00423 }
00424 else
00425 {
00426 return a;
00427 }
00428 }
00429
00430
00431 double SprChiCdf::fifdsign ( double mag, double sign )
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444 {
00445 if (mag < 0) mag = -mag;
00446 if (sign < 0) mag = -mag;
00447 return mag;
00448
00449 }
00450
00451
00452 long SprChiCdf::fifidint ( double a )
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464 {
00465 if ( a < 1.0 )
00466 {
00467 return (long) 0;
00468 }
00469 else
00470 {
00471 return ( long ) a;
00472 }
00473 }
00474
00475
00476 long SprChiCdf::fifmod ( long a, long b )
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489 {
00490 return ( a % b );
00491 }
00492
00493
00494 double SprChiCdf::gam1 ( double *a )
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508 {
00509 static double s1 = .273076135303957e+00;
00510 static double s2 = .559398236957378e-01;
00511 static double p[7] = {
00512 .577215664901533e+00,-.409078193005776e+00,-.230975380857675e+00,
00513 .597275330452234e-01,.766968181649490e-02,-.514889771323592e-02,
00514 .589597428611429e-03
00515 };
00516 static double q[5] = {
00517 .100000000000000e+01,.427569613095214e+00,.158451672430138e+00,
00518 .261132021441447e-01,.423244297896961e-02
00519 };
00520 static double r[9] = {
00521 -.422784335098468e+00,-.771330383816272e+00,-.244757765222226e+00,
00522 .118378989872749e+00,.930357293360349e-03,-.118290993445146e-01,
00523 .223047661158249e-02,.266505979058923e-03,-.132674909766242e-03
00524 };
00525 static double gam1,bot,d,t,top,w,T1;
00526
00527 t = *a;
00528 d = *a-0.5e0;
00529 if(d > 0.0e0) t = d-0.5e0;
00530 T1 = t;
00531 if(T1 < 0) goto S40;
00532 else if(T1 == 0) goto S10;
00533 else goto S20;
00534 S10:
00535 gam1 = 0.0e0;
00536 return gam1;
00537 S20:
00538 top = (((((p[6]*t+p[5])*t+p[4])*t+p[3])*t+p[2])*t+p[1])*t+p[0];
00539 bot = (((q[4]*t+q[3])*t+q[2])*t+q[1])*t+1.0e0;
00540 w = top/bot;
00541 if(d > 0.0e0) goto S30;
00542 gam1 = *a*w;
00543 return gam1;
00544 S30:
00545 gam1 = t/ *a*(w-0.5e0-0.5e0);
00546 return gam1;
00547 S40:
00548 top = (((((((r[8]*t+r[7])*t+r[6])*t+r[5])*t+r[4])*t+r[3])*t+r[2])*t+r[1])*t+
00549 r[0];
00550 bot = (s2*t+s1)*t+1.0e0;
00551 w = top/bot;
00552 if(d > 0.0e0) goto S50;
00553 gam1 = *a*(w+0.5e0+0.5e0);
00554 return gam1;
00555 S50:
00556 gam1 = t*w/ *a;
00557 return gam1;
00558 }
00559
00560
00561 void SprChiCdf::gamma_inc ( double *a, double *x, double *ans, double *qans, int *ind )
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596 {
00597 static double alog10 = 2.30258509299405e0;
00598 static double d10 = -.185185185185185e-02;
00599 static double d20 = .413359788359788e-02;
00600 static double d30 = .649434156378601e-03;
00601 static double d40 = -.861888290916712e-03;
00602 static double d50 = -.336798553366358e-03;
00603 static double d60 = .531307936463992e-03;
00604 static double d70 = .344367606892378e-03;
00605 static double rt2pin = .398942280401433e0;
00606 static double rtpi = 1.77245385090552e0;
00607 static double third = .333333333333333e0;
00608 static double acc0[3] = {
00609 5.e-15,5.e-7,5.e-4
00610 };
00611 static double big[3] = {
00612 20.0e0,14.0e0,10.0e0
00613 };
00614 static double d0[13] = {
00615 .833333333333333e-01,-.148148148148148e-01,.115740740740741e-02,
00616 .352733686067019e-03,-.178755144032922e-03,.391926317852244e-04,
00617 -.218544851067999e-05,-.185406221071516e-05,.829671134095309e-06,
00618 -.176659527368261e-06,.670785354340150e-08,.102618097842403e-07,
00619 -.438203601845335e-08
00620 };
00621 static double d1[12] = {
00622 -.347222222222222e-02,.264550264550265e-02,-.990226337448560e-03,
00623 .205761316872428e-03,-.401877572016461e-06,-.180985503344900e-04,
00624 .764916091608111e-05,-.161209008945634e-05,.464712780280743e-08,
00625 .137863344691572e-06,-.575254560351770e-07,.119516285997781e-07
00626 };
00627 static double d2[10] = {
00628 -.268132716049383e-02,.771604938271605e-03,.200938786008230e-05,
00629 -.107366532263652e-03,.529234488291201e-04,-.127606351886187e-04,
00630 .342357873409614e-07,.137219573090629e-05,-.629899213838006e-06,
00631 .142806142060642e-06
00632 };
00633 static double d3[8] = {
00634 .229472093621399e-03,-.469189494395256e-03,.267720632062839e-03,
00635 -.756180167188398e-04,-.239650511386730e-06,.110826541153473e-04,
00636 -.567495282699160e-05,.142309007324359e-05
00637 };
00638 static double d4[6] = {
00639 .784039221720067e-03,-.299072480303190e-03,-.146384525788434e-05,
00640 .664149821546512e-04,-.396836504717943e-04,.113757269706784e-04
00641 };
00642 static double d5[4] = {
00643 -.697281375836586e-04,.277275324495939e-03,-.199325705161888e-03,
00644 .679778047793721e-04
00645 };
00646 static double d6[2] = {
00647 -.592166437353694e-03,.270878209671804e-03
00648 };
00649 static double e00[3] = {
00650 .25e-3,.25e-1,.14e0
00651 };
00652 static double x00[3] = {
00653 31.0e0,17.0e0,9.7e0
00654 };
00655 static int K1 = 1;
00656 static int K2 = 0;
00657 static double a2n,a2nm1,acc,am0,amn,an,an0,apn,b2n,b2nm1,c,c0,c1,c2,c3,c4,c5,c6,
00658 cma,e,e0,g,h,j,l,r,rta,rtx,s,sum,t,t1,tol,twoa,u,w,x0,y,z;
00659 static int i,iop,m,max,n;
00660 static double wk[20],T3;
00661 static int T4,T5;
00662 static double T6,T7;
00663
00664
00665
00666
00667
00668 e = dpmpar(&K1);
00669 if(*a < 0.0e0 || *x < 0.0e0) goto S430;
00670 if(*a == 0.0e0 && *x == 0.0e0) goto S430;
00671 if(*a**x == 0.0e0) goto S420;
00672 iop = *ind+1;
00673 if(iop != 1 && iop != 2) iop = 3;
00674 acc = fifdmax1(acc0[iop-1],e);
00675 e0 = e00[iop-1];
00676 x0 = x00[iop-1];
00677
00678
00679
00680 if(*a >= 1.0e0) goto S10;
00681 if(*a == 0.5e0) goto S390;
00682 if(*x < 1.1e0) goto S160;
00683 t1 = *a*log(*x)-*x;
00684 u = *a*exp(t1);
00685 if(u == 0.0e0) goto S380;
00686 r = u*(1.0e0+gam1(a));
00687 goto S250;
00688 S10:
00689 if(*a >= big[iop-1]) goto S30;
00690 if(*a > *x || *x >= x0) goto S20;
00691 twoa = *a+*a;
00692 m = fifidint(twoa);
00693 if(twoa != (double)m) goto S20;
00694 i = m/2;
00695 if(*a == (double)i) goto S210;
00696 goto S220;
00697 S20:
00698 t1 = *a*log(*x)-*x;
00699 r = exp(t1)/ gamma_x(a);
00700 goto S40;
00701 S30:
00702 l = *x/ *a;
00703 if(l == 0.0e0) goto S370;
00704 s = 0.5e0+(0.5e0-l);
00705 z = rlog(&l);
00706 if(z >= 700.0e0/ *a) goto S410;
00707 y = *a*z;
00708 rta = sqrt(*a);
00709 if(fabs(s) <= e0/rta) goto S330;
00710 if(fabs(s) <= 0.4e0) goto S270;
00711 t = pow(1.0e0/ *a,2.0);
00712 t1 = (((0.75e0*t-1.0e0)*t+3.5e0)*t-105.0e0)/(*a*1260.0e0);
00713 t1 -= y;
00714 r = rt2pin*rta*exp(t1);
00715 S40:
00716 if(r == 0.0e0) goto S420;
00717 if(*x <= fifdmax1(*a,alog10)) goto S50;
00718 if(*x < x0) goto S250;
00719 goto S100;
00720 S50:
00721
00722
00723
00724 apn = *a+1.0e0;
00725 t = *x/apn;
00726 wk[0] = t;
00727 for ( n = 2; n <= 20; n++ )
00728 {
00729 apn += 1.0e0;
00730 t *= (*x/apn);
00731 if(t <= 1.e-3) goto S70;
00732 wk[n-1] = t;
00733 }
00734 n = 20;
00735 S70:
00736 sum = t;
00737 tol = 0.5e0*acc;
00738 S80:
00739 apn += 1.0e0;
00740 t *= (*x/apn);
00741 sum += t;
00742 if(t > tol) goto S80;
00743 max = n-1;
00744 for ( m = 1; m <= max; m++ )
00745 {
00746 n -= 1;
00747 sum += wk[n-1];
00748 }
00749 *ans = r/ *a*(1.0e0+sum);
00750 *qans = 0.5e0+(0.5e0-*ans);
00751 return;
00752 S100:
00753
00754
00755
00756 amn = *a-1.0e0;
00757 t = amn/ *x;
00758 wk[0] = t;
00759 for ( n = 2; n <= 20; n++ )
00760 {
00761 amn -= 1.0e0;
00762 t *= (amn/ *x);
00763 if(fabs(t) <= 1.e-3) goto S120;
00764 wk[n-1] = t;
00765 }
00766 n = 20;
00767 S120:
00768 sum = t;
00769 S130:
00770 if(fabs(t) <= acc) goto S140;
00771 amn -= 1.0e0;
00772 t *= (amn/ *x);
00773 sum += t;
00774 goto S130;
00775 S140:
00776 max = n-1;
00777 for ( m = 1; m <= max; m++ )
00778 {
00779 n -= 1;
00780 sum += wk[n-1];
00781 }
00782 *qans = r/ *x*(1.0e0+sum);
00783 *ans = 0.5e0+(0.5e0-*qans);
00784 return;
00785 S160:
00786
00787
00788
00789 an = 3.0e0;
00790 c = *x;
00791 sum = *x/(*a+3.0e0);
00792 tol = 3.0e0*acc/(*a+1.0e0);
00793 S170:
00794 an += 1.0e0;
00795 c = -(c*(*x/an));
00796 t = c/(*a+an);
00797 sum += t;
00798 if(fabs(t) > tol) goto S170;
00799 j = *a**x*((sum/6.0e0-0.5e0/(*a+2.0e0))**x+1.0e0/(*a+1.0e0));
00800 z = *a*log(*x);
00801 h = gam1(a);
00802 g = 1.0e0+h;
00803 if(*x < 0.25e0) goto S180;
00804 if(*a < *x/2.59e0) goto S200;
00805 goto S190;
00806 S180:
00807 if(z > -.13394e0) goto S200;
00808 S190:
00809 w = exp(z);
00810 *ans = w*g*(0.5e0+(0.5e0-j));
00811 *qans = 0.5e0+(0.5e0-*ans);
00812 return;
00813 S200:
00814 l = rexp(&z);
00815 w = 0.5e0+(0.5e0+l);
00816 *qans = (w*j-l)*g-h;
00817 if(*qans < 0.0e0) goto S380;
00818 *ans = 0.5e0+(0.5e0-*qans);
00819 return;
00820 S210:
00821
00822
00823
00824 sum = exp(-*x);
00825 t = sum;
00826 n = 1;
00827 c = 0.0e0;
00828 goto S230;
00829 S220:
00830 rtx = sqrt(*x);
00831 sum = erfc1(&K2,&rtx);
00832 t = exp(-*x)/(rtpi*rtx);
00833 n = 0;
00834 c = -0.5e0;
00835 S230:
00836 if(n == i) goto S240;
00837 n += 1;
00838 c += 1.0e0;
00839 t = *x*t/c;
00840 sum += t;
00841 goto S230;
00842 S240:
00843 *qans = sum;
00844 *ans = 0.5e0+(0.5e0-*qans);
00845 return;
00846 S250:
00847
00848
00849
00850 tol = fifdmax1(5.0e0*e,acc);
00851 a2nm1 = a2n = 1.0e0;
00852 b2nm1 = *x;
00853 b2n = *x+(1.0e0-*a);
00854 c = 1.0e0;
00855 S260:
00856 a2nm1 = *x*a2n+c*a2nm1;
00857 b2nm1 = *x*b2n+c*b2nm1;
00858 am0 = a2nm1/b2nm1;
00859 c += 1.0e0;
00860 cma = c-*a;
00861 a2n = a2nm1+cma*a2n;
00862 b2n = b2nm1+cma*b2n;
00863 an0 = a2n/b2n;
00864 if(fabs(an0-am0) >= tol*an0) goto S260;
00865 *qans = r*an0;
00866 *ans = 0.5e0+(0.5e0-*qans);
00867 return;
00868 S270:
00869
00870
00871
00872 if(fabs(s) <= 2.0e0*e && *a*e*e > 3.28e-3) goto S430;
00873 c = exp(-y);
00874 T3 = sqrt(y);
00875 w = 0.5e0*erfc1(&K1,&T3);
00876 u = 1.0e0/ *a;
00877 z = sqrt(z+z);
00878 if(l < 1.0e0) z = -z;
00879 T4 = iop-2;
00880 if(T4 < 0) goto S280;
00881 else if(T4 == 0) goto S290;
00882 else goto S300;
00883 S280:
00884 if(fabs(s) <= 1.e-3) goto S340;
00885 c0 = ((((((((((((d0[12]*z+d0[11])*z+d0[10])*z+d0[9])*z+d0[8])*z+d0[7])*z+d0[
00886 6])*z+d0[5])*z+d0[4])*z+d0[3])*z+d0[2])*z+d0[1])*z+d0[0])*z-third;
00887 c1 = (((((((((((d1[11]*z+d1[10])*z+d1[9])*z+d1[8])*z+d1[7])*z+d1[6])*z+d1[5]
00888 )*z+d1[4])*z+d1[3])*z+d1[2])*z+d1[1])*z+d1[0])*z+d10;
00889 c2 = (((((((((d2[9]*z+d2[8])*z+d2[7])*z+d2[6])*z+d2[5])*z+d2[4])*z+d2[3])*z+
00890 d2[2])*z+d2[1])*z+d2[0])*z+d20;
00891 c3 = (((((((d3[7]*z+d3[6])*z+d3[5])*z+d3[4])*z+d3[3])*z+d3[2])*z+d3[1])*z+
00892 d3[0])*z+d30;
00893 c4 = (((((d4[5]*z+d4[4])*z+d4[3])*z+d4[2])*z+d4[1])*z+d4[0])*z+d40;
00894 c5 = (((d5[3]*z+d5[2])*z+d5[1])*z+d5[0])*z+d50;
00895 c6 = (d6[1]*z+d6[0])*z+d60;
00896 t = ((((((d70*u+c6)*u+c5)*u+c4)*u+c3)*u+c2)*u+c1)*u+c0;
00897 goto S310;
00898 S290:
00899 c0 = (((((d0[5]*z+d0[4])*z+d0[3])*z+d0[2])*z+d0[1])*z+d0[0])*z-third;
00900 c1 = (((d1[3]*z+d1[2])*z+d1[1])*z+d1[0])*z+d10;
00901 c2 = d2[0]*z+d20;
00902 t = (c2*u+c1)*u+c0;
00903 goto S310;
00904 S300:
00905 t = ((d0[2]*z+d0[1])*z+d0[0])*z-third;
00906 S310:
00907 if(l < 1.0e0) goto S320;
00908 *qans = c*(w+rt2pin*t/rta);
00909 *ans = 0.5e0+(0.5e0-*qans);
00910 return;
00911 S320:
00912 *ans = c*(w-rt2pin*t/rta);
00913 *qans = 0.5e0+(0.5e0-*ans);
00914 return;
00915 S330:
00916
00917
00918
00919 if(*a*e*e > 3.28e-3) goto S430;
00920 c = 0.5e0+(0.5e0-y);
00921 w = (0.5e0-sqrt(y)*(0.5e0+(0.5e0-y/3.0e0))/rtpi)/c;
00922 u = 1.0e0/ *a;
00923 z = sqrt(z+z);
00924 if(l < 1.0e0) z = -z;
00925 T5 = iop-2;
00926 if(T5 < 0) goto S340;
00927 else if(T5 == 0) goto S350;
00928 else goto S360;
00929 S340:
00930 c0 = ((((((d0[6]*z+d0[5])*z+d0[4])*z+d0[3])*z+d0[2])*z+d0[1])*z+d0[0])*z-
00931 third;
00932 c1 = (((((d1[5]*z+d1[4])*z+d1[3])*z+d1[2])*z+d1[1])*z+d1[0])*z+d10;
00933 c2 = ((((d2[4]*z+d2[3])*z+d2[2])*z+d2[1])*z+d2[0])*z+d20;
00934 c3 = (((d3[3]*z+d3[2])*z+d3[1])*z+d3[0])*z+d30;
00935 c4 = (d4[1]*z+d4[0])*z+d40;
00936 c5 = (d5[1]*z+d5[0])*z+d50;
00937 c6 = d6[0]*z+d60;
00938 t = ((((((d70*u+c6)*u+c5)*u+c4)*u+c3)*u+c2)*u+c1)*u+c0;
00939 goto S310;
00940 S350:
00941 c0 = (d0[1]*z+d0[0])*z-third;
00942 c1 = d1[0]*z+d10;
00943 t = (d20*u+c1)*u+c0;
00944 goto S310;
00945 S360:
00946 t = d0[0]*z-third;
00947 goto S310;
00948 S370:
00949
00950
00951
00952 *ans = 0.0e0;
00953 *qans = 1.0e0;
00954 return;
00955 S380:
00956 *ans = 1.0e0;
00957 *qans = 0.0e0;
00958 return;
00959 S390:
00960 if(*x >= 0.25e0) goto S400;
00961 T6 = sqrt(*x);
00962 *ans = erf1(&T6);
00963 *qans = 0.5e0+(0.5e0-*ans);
00964 return;
00965 S400:
00966 T7 = sqrt(*x);
00967 *qans = erfc1(&K2,&T7);
00968 *ans = 0.5e0+(0.5e0-*qans);
00969 return;
00970 S410:
00971 if(fabs(s) <= 2.0e0*e) goto S430;
00972 S420:
00973 if(*x <= *a) goto S370;
00974 goto S380;
00975 S430:
00976
00977
00978
00979 *ans = 2.0e0;
00980 return;
00981 }
00982
00983
00984 double SprChiCdf::gamma_x ( double *a )
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009 {
01010 static double d = .41893853320467274178e0;
01011 static double pi = 3.1415926535898e0;
01012 static double r1 = .820756370353826e-03;
01013 static double r2 = -.595156336428591e-03;
01014 static double r3 = .793650663183693e-03;
01015 static double r4 = -.277777777770481e-02;
01016 static double r5 = .833333333333333e-01;
01017 static double p[7] = {
01018 .539637273585445e-03,.261939260042690e-02,.204493667594920e-01,
01019 .730981088720487e-01,.279648642639792e+00,.553413866010467e+00,1.0e0
01020 };
01021 static double q[7] = {
01022 -.832979206704073e-03,.470059485860584e-02,.225211131035340e-01,
01023 -.170458969313360e+00,-.567902761974940e-01,.113062953091122e+01,1.0e0
01024 };
01025 static int K2 = 3;
01026 static int K3 = 0;
01027 static double Xgamm,bot,g,lnx,s,t,top,w,x,z;
01028 static int i,j,m,n,T1;
01029
01030 Xgamm = 0.0e0;
01031 x = *a;
01032 if(fabs(*a) >= 15.0e0) goto S110;
01033
01034
01035
01036 t = 1.0e0;
01037 m = fifidint(*a)-1;
01038
01039
01040
01041 T1 = m;
01042 if(T1 < 0) goto S40;
01043 else if(T1 == 0) goto S30;
01044 else goto S10;
01045 S10:
01046 for ( j = 1; j <= m; j++ )
01047 {
01048 x -= 1.0e0;
01049 t = x*t;
01050 }
01051 S30:
01052 x -= 1.0e0;
01053 goto S80;
01054 S40:
01055
01056
01057
01058 t = *a;
01059 if(*a > 0.0e0) goto S70;
01060 m = -m-1;
01061 if(m == 0) goto S60;
01062 for ( j = 1; j <= m; j++ )
01063 {
01064 x += 1.0e0;
01065 t = x*t;
01066 }
01067 S60:
01068 x += (0.5e0+0.5e0);
01069 t = x*t;
01070 if(t == 0.0e0) return Xgamm;
01071 S70:
01072
01073
01074
01075
01076 if(fabs(t) >= 1.e-30) goto S80;
01077 if(fabs(t)*dpmpar(&K2) <= 1.0001e0) return Xgamm;
01078 Xgamm = 1.0e0/t;
01079 return Xgamm;
01080 S80:
01081
01082
01083
01084 top = p[0];
01085 bot = q[0];
01086 for ( i = 1; i < 7; i++ )
01087 {
01088 top = p[i]+x*top;
01089 bot = q[i]+x*bot;
01090 }
01091 Xgamm = top/bot;
01092
01093
01094
01095 if(*a < 1.0e0) goto S100;
01096 Xgamm *= t;
01097 return Xgamm;
01098 S100:
01099 Xgamm /= t;
01100 return Xgamm;
01101 S110:
01102
01103
01104
01105 if(fabs(*a) >= 1.e3) return Xgamm;
01106 if(*a > 0.0e0) goto S120;
01107 x = -*a;
01108 n = x;
01109 t = x-(double)n;
01110 if(t > 0.9e0) t = 1.0e0-t;
01111 s = sin(pi*t)/pi;
01112 if(fifmod(n,2) == 0) s = -s;
01113 if(s == 0.0e0) return Xgamm;
01114 S120:
01115
01116
01117
01118 t = 1.0e0/(x*x);
01119 g = ((((r1*t+r2)*t+r3)*t+r4)*t+r5)/x;
01120
01121
01122
01123
01124 lnx = log(x);
01125
01126
01127
01128 z = x;
01129 g = d+g+(z-0.5e0)*(lnx-1.e0);
01130 w = g;
01131 t = g-w;
01132 if(w > 0.99999e0*exparg(&K3)) return Xgamm;
01133 Xgamm = exp(w)*(1.0e0+t);
01134 if(*a < 0.0e0) Xgamm = 1.0e0/(Xgamm*s)/x;
01135 return Xgamm;
01136 }
01137
01138
01139 int SprChiCdf::ipmpar ( int *i )
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203 {
01204 static int imach[11];
01205 static int ipmpar;
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384
01385
01386
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504
01505
01506
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521 imach[1] = 2;
01522 imach[2] = 31;
01523 imach[3] = 2147483647;
01524 imach[4] = 2;
01525 imach[5] = 24;
01526 imach[6] = -125;
01527 imach[7] = 128;
01528 imach[8] = 53;
01529 imach[9] = -1021;
01530 imach[10] = 1024;
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556
01557
01558 ipmpar = imach[*i];
01559 return ipmpar;
01560 }
01561
01562
01563 double SprChiCdf::rexp ( double *x )
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581 {
01582 static double p1 = .914041914819518e-09;
01583 static double p2 = .238082361044469e-01;
01584 static double q1 = -.499999999085958e+00;
01585 static double q2 = .107141568980644e+00;
01586 static double q3 = -.119041179760821e-01;
01587 static double q4 = .595130811860248e-03;
01588 static double rexp,w;
01589
01590 if(fabs(*x) > 0.15e0) goto S10;
01591 rexp = *x*(((p2**x+p1)**x+1.0e0)/((((q4**x+q3)**x+q2)**x+q1)**x+1.0e0));
01592 return rexp;
01593 S10:
01594 w = exp(*x);
01595 if(*x > 0.0e0) goto S20;
01596 rexp = w-0.5e0-0.5e0;
01597 return rexp;
01598 S20:
01599 rexp = w*(0.5e0+(0.5e0-1.0e0/w));
01600 return rexp;
01601 }
01602
01603
01604 double SprChiCdf::rlog ( double *x )
01605
01606
01607
01608
01609
01610
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621
01622 {
01623 static double a = .566749439387324e-01;
01624 static double b = .456512608815524e-01;
01625 static double p0 = .333333333333333e+00;
01626 static double p1 = -.224696413112536e+00;
01627 static double p2 = .620886815375787e-02;
01628 static double q1 = -.127408923933623e+01;
01629 static double q2 = .354508718369557e+00;
01630 static double rlog,r,t,u,w,w1;
01631
01632 if(*x < 0.61e0 || *x > 1.57e0) goto S40;
01633 if(*x < 0.82e0) goto S10;
01634 if(*x > 1.18e0) goto S20;
01635
01636
01637
01638 u = *x-0.5e0-0.5e0;
01639 w1 = 0.0e0;
01640 goto S30;
01641 S10:
01642 u = *x-0.7e0;
01643 u /= 0.7e0;
01644 w1 = a-u*0.3e0;
01645 goto S30;
01646 S20:
01647 u = 0.75e0**x-1.e0;
01648 w1 = b+u/3.0e0;
01649 S30:
01650
01651
01652
01653 r = u/(u+2.0e0);
01654 t = r*r;
01655 w = ((p2*t+p1)*t+p0)/((q2*t+q1)*t+1.0e0);
01656 rlog = 2.0e0*t*(1.0e0/(1.0e0-r)-r*w)+w1;
01657 return rlog;
01658 S40:
01659 r = *x-0.5e0-0.5e0;
01660 rlog = r-log(*x);
01661 return rlog;
01662 }
01663