/*  This header file contains several member functions which returns
    values of some special functions, at certain argument values.  These
    functions are used in Test.h, particularly for the classes 
    SvarMeanTest, DvarMeanTest, and VarTest.  These functions are also used
    in Generate.h, particularly for the member function poisson.
*/    


#include <math.h>
#include <iostream.h>

class SpecFunc {
public:

     SpecFunc();
     ~SpecFunc();
     float Gammln(float xx);			// Natural Log of gamma function.
     float Betai(float a,float b,float x);	// Incomplete Beta Function.
     float Sqr(float x);			// Square
     
     
private:
     int j;
     double x,y,tmp,ser;
     double* cof; 
     float bt;
     int MAXIT;
     float EPS;
     float FPMIN;
     void error(char ErrorText[]);
     int m,m2;
     float aa,c,d,h,qab,qam,qap;
     float Betacf(float a,float b,float x);   	// Function needed only for Betai.

};

SpecFunc::SpecFunc() {

     cof=new double [6];
     cof[0]=76.1800917294714;
     cof[1]=-86.50532032941677;
     cof[2]=24.01409824083091;
     cof[3]=-1.231739572450155;
     cof[4]=0.1208650973866179e-2;
     cof[5]=-0.5395239384953e-5;
     MAXIT=100;
     EPS=3.0e-7;
     FPMIN=1.0e-30;     
}     

SpecFunc::~SpecFunc() {delete [] cof;}

// Note this code for calculating these functions are in "Numerical Recipes in C
// The Art of Scientific Computing Second Edition"

float SpecFunc::Gammln(float xx) {    	// In "Numerical Recipes" Ch. 6-1, p. 214
					
     y=x=xx;
     tmp=x + 5.5;
     tmp -= (x+.5)*log(tmp);
     ser=1.000000000190015;
     for (j=0;j<=5;j++) ser += cof[j]/++y;
          	
     return -tmp + log(2.5066282746210005*ser/x);
}     

float SpecFunc::Betai(float a,float b,float x) {    // In "Numerical Recipes" Ch. 6-4, p. 227

     if(x < 0.0 || x > 1.0) error("Bad x in routine betai");

     if(x == 0.0 || x == 1.0) {bt=0.0;}

     else {bt=exp(Gammln(a+b) - Gammln(a) - Gammln(b)
           + a*log(x) + b*log(1.0 - x));}
     
     if(x < (a+1.0)/(a+b+2.0)) {return bt*Betacf(a,b,x)/a;}
     
     else {return 1.0 - bt*Betacf(b,a,1.0 - x)/b;}
}

float SpecFunc::Sqr(float x) {return x*x;}

float SpecFunc::Betacf(float a,float b,float x) {   // In "Numerical Recipes" Ch. 6-4, p. 227 - 228
					    // It is used for the calculation of the
     qab=a+b;				    // above function Betai only.
     qap=a+1.0;
     qam=a-1.0;
     c=1.0;
     d=1.0-qab*x/qap;
     
     if(fabs(d) < FPMIN) d=FPMIN;
     
     d=1.0/d;
     h=d;
     for (m=1;m<=MAXIT;m++) {
     
          m2=2*m;
          aa=m*(b-m)*x/((qam+m2)*(a+m2));          
          d=1.0 + aa*d;
          if (fabs(d) < FPMIN) d=FPMIN;
          c=1.0 + aa/c;
          if (fabs(c) < FPMIN) c=FPMIN;
          d=1.0/d;
          h *= d*c;
          aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
          d=1.0+aa*d;
          if (fabs(d) < FPMIN) d=FPMIN;
          c=1.0 + aa/c;
          if (fabs(c) < FPMIN) c=FPMIN;
          d=1.0/d;
          h *= d*c;
          if (fabs(d*c - 1.0) < EPS) break;
     }
      
     if (m > MAXIT) error("a or b too big, or MAXIT too small in betacf");
      
     return h;
} 

void SpecFunc::error(char ErrorText[]) {       

     cerr<<endl<<ErrorText<<endl;
     cout.flush();
     abort();
} 

    
               
	     
	     
	     