home *** CD-ROM | disk | FTP | other *** search
-
-
- #include <math.h>
- #include <stdio.h>
- #include "miscio.h"
-
- # define NearZero 1.0E-20
- # define Tolerance 1.0E-10
-
- void DisplayErrorMessage(int ErrorFlag)
- {
-
- switch ( ErrorFlag ) {
- case 0:
- printf("No Errors Found.");
- break;
- case 1:
- printf( "ERROR!! Argument must be a nonzero number");
- break;
- case 2:
- printf("ERROR!! Argument must not be a negative");
- break;
- case 3:
- printf( "ERROR!! Order of polynomial must be greater than zero");
- break;
- case 4:
- printf( "ERROR!! Argument must be greater than -1");
- break;
- }
- }
-
- float CalcPoly(float XIn,float CoefVector[],int order)
- {
- float _fret = 4;
- int I;
- float YOut;
-
- YOut = CoefVector[order];
- for ( I = order - 1; I >= 0; --I ) {
- YOut = YOut * XIn + CoefVector[I];
- }
- _fret = YOut;
- return(_fret);
- }
-
- float CalcPower(float x,float a)
- {
- float _fret;
-
- _fret = exp(a * log(x));
- return(_fret);
- }
-
- float LogGamma(float x)
- {
- float _fret;
- float coef[16];
- float ser;
- float temp;
- float xx;
- float stp;
- int I;
- coef[0] = 7.618009173E1;
- coef[1] = -8.650532033E1;
- coef[2] = 2.401409822E1;
- coef[3] = -1.231739516;
- coef[4] = 1.20858003E-3;
- coef[5] = -5.36382E-6;
- stp = 2.50662827565;
- xx = x - 1.0;
- temp = xx + 5.5;
- temp = (xx + 0.5) * log(temp) - temp;
- ser = 1.0;
- for ( I = 0; I <= 5; ++I ) {
- xx = xx + 1.0;
- ser = ser + coef[I] / xx;
- }
- _fret = temp + log(stp * ser);
-
- return(_fret);
- }
-
- float StandGamma(float y)
- {
- float _fret;
- typedef float BigArray[21];
- float temp;
- float f;
- int I;
- float BigArry[21];
- BigArry[0] = 0.5772156649015329;
- BigArry[1] = -0.6558780715202538;
- BigArry[2] = -0.0420026350340952;
- BigArry[3] = 0.1665386113822915;
- BigArry[4] = -0.0421977345555443;
- BigArry[5] = -0.009621971527877;
- BigArry[6] = 0.0072189432466630;
- BigArry[7] = -0.0011651675918591;
- BigArry[8] = -0.0002152416741149;
- BigArry[9] = 0.0001280502823882;
- BigArry[10] = -0.0000201348547807;
- BigArry[11] = -0.0000012504934821;
- BigArry[12] = 0.0000011330272320;
- BigArry[13] = -0.0000002056338417;
- BigArry[14] = 0.0000000061160950;
- BigArry[15] = 0.0000000050020075;
- BigArry[16] = -0.0000000011812746;
- BigArry[17] = 0.0000000001043427;
- BigArry[18] = 0.0000000000077823;
- BigArry[19] = -0.0000000000036968;
- BigArry[20] = 0.00000000000005100;
- f = y;
- temp = f;
- for ( I = 0; I <= 20; ++I ) {
- f = f * y;
- temp = temp + f * BigArry[I];
- }
- _fret = 1.0 / temp;
- return(_fret);
- }
-
- float Gamma(float x,int *ErrorFlag)
- {
- float _fret;
- float y;
- float f;
- if ( (fabs(x) < NearZero) ) {
- (*ErrorFlag) = 1;
- }
- else {
- if ( ((x < 0) && (fabs(x - ceil(0.5+x)) < NearZero)) ) {
- (*ErrorFlag) = 2;
- }
- else {
- if ( fabs(x-1.0) < NearZero ) {
- _fret = 1.0;
- }
- else {
- if ( x < 0.0 ) {
- _fret = -(M_PI / (x * sin(M_PI * x) * Gamma(-x,ErrorFlag)));
- }
- else {
- y = x;
- f = 1.0;
- while ( y > 1.0 ) {
-
- y = y - 1.0;
- f = f * y;
- }
- if ( fabs(y-1.0) < NearZero ) {
- _fret = f;
- }
- else {
- if ( y > (1.0 / 2.0) ) {
- _fret = f * M_PI / (sin(M_PI * y)* Gamma(1.0 - y,ErrorFlag));
- }
- else {
- _fret = f * StandGamma(y);
- }
- }
- }
- }
- }
- }
- return(_fret);
- }
-
- float IncGamma(float a,float x)
- {
- float _fret;
- float ir;
- float dif;
- float s1;
- float s2;
- float gln;
- float result;
-
- if ( (a < 0.0) || (x <= 0.0) ) {
- result = 0.0;
- }
- else {
- gln = LogGamma(a);
- ir = a;
- s1 = 1.0 / a;
- dif = s1;
- while ( (fabs(dif) > fabs(s1) * Tolerance) ) {
- ir = ir + 1.0;
- dif = dif * x / ir;
- s1 = s1 + dif;
- }
- result = s1 * exp(-x + a * log(x) - gln);
- }
- _fret = result;
- return(_fret);
- }
-
- float IncGammaComp(float a,float x)
- {
- float _fret;
-
- _fret = 1.0 - IncGamma(a,x);
- return(_fret);
- }
-
- float Bessel(float order,float x,int *ErrorFlag)
- {
- float _fret;
- float j;
- float d;
- float f;
- float sum;
- int c;
-
- if ( order < NearZero ) {
- (*ErrorFlag) = 3;
- }
- else {
- if ( ((x < 0.0) && (fabs(x - ceil(0.5 + x)) < NearZero)) ) {
- (*ErrorFlag) = 2;
- }
- else {
- if ( fabs(x) < NearZero ) {
- if ( fabs(order) < NearZero ) {
- _fret = 1.0;
- }
- else {
- _fret = 0;
- }
- }
- else {
- if ( x <= 15.0 ) {
- f = 1.0 / Gamma(order + 1,ErrorFlag);
- j = -((x*x) / 4.0);
- d = order + 1.0;
- sum = f;
- c = 1;
- do {
- f = f * j / (c * d);
- d = d + 1.0;
- c = c + 1;
- sum = sum + f;
- } while ( ! ((fabs(f) < Tolerance)) );
- if ( x > 0.0 ) {
- _fret = sum * exp(order * log(x / 2.0));
- }
- else {
- _fret = (1.0 - 2.0 * (fmod(floor(order),2.0))
- * sum * exp(order * log(fabs(x / 2.0) )));
- }
- }
- else {
- _fret = sqrt(2.0 / (M_PI * x)) *
- cos(x - M_PI / 4.0 - order * M_PI * 2.0);
- }
- }
- }
- }
- return(_fret);
- }
-
- float Tan(float x)
- {
- float _fret;
-
- _fret = sin(x) / cos(x);
- return(_fret);
- }
-
- float Cosh(float z)
- {
- float _fret;
-
- _fret = (exp(z) + exp(-z)) / 2.0;
- return(_fret);
- }
-
- float Sinh(float z)
- {
- float _fret;
-
- _fret = (exp(z) - exp(-z)) / 2.0;
- return(_fret);
- }
-
- float Sech(float z)
- {
- float _fret;
-
- _fret = 1.0 / Cosh(z);
- return(_fret);
- }
-
- float ArcTanh(float x)
- {
- float _fret;
-
- _fret = 0.5 * log((1.0 + x) / (1.0 - x));
- return(_fret);
- }
-
- float ErrFuncIter(float x)
- {
- float _fret;
- float a;
- float s1;
- float s2;
- float t;
- float result;
- int I;
- int j;
-
- a = x*x;
- s1 = x;
- t = x;
- I = 1;
- while ( t > (Tolerance * s1) ) {
- s2 = s1;
- t = 2.0 * t * (a / (1.0 + 2.0 * I));
- s1 = t + s2;
- I = I + 1;
- }
- result = 2.0 * s1 * (exp(-a)) / sqrt(M_PI);
- _fret = result;
- return(_fret);
- }
-
- float ErrFunc(float x,int *ErrorFlag)
- {
- float _fret;
- float PolyCoef[16];
- float result;
- float temp;
- float PosNeg;
- float p;
- float ANS;
- int I;
-
- if ( fabs(x) < NearZero ) {
- (*ErrorFlag) = 1;
- }
- else {
- if ( x > 4.0 ) {
- result = 1.0;
- }
- else {
- if ( x > 1.5 ) {
- result = ErrFuncIter(x);
- }
- else {
- p = x * x;
- PolyCoef[0] = 0.0;
- PolyCoef[0] = 0.0;
- PolyCoef[1] = 0.6666667;
- PolyCoef[2] = 0.2666667;
- PolyCoef[3] = 0.07619048;
- PolyCoef[4] = 0.01693122;
- PolyCoef[5] = 3.078403E-3;
- PolyCoef[6] = 4.736005E-4;
- PolyCoef[7] = 6.314673E-5;
- PolyCoef[8] = 7.429027E-6;
- PolyCoef[9] = 7.820028E-7;
- PolyCoef[10] = 7.447646E-8;
- PolyCoef[11] = 6.476214E-9;
- temp = x + x * CalcPoly(p,PolyCoef,11);
- result = 2.0 * temp * exp(-p) / sqrt(M_PI);
- }
- }
- }
- _fret = result;
- return(_fret);
- }
-
- float ErrFuncComp(float x,int *ErrorFlag)
- {
- float _fret;
-
- _fret = 1.0 - ErrFunc(x,ErrorFlag);
- return(_fret);
- }
-
- float ErrFuncR(float x,float y, int *ErrorFlag)
- {
- float _fret;
- float jy;
- float xy2;
- float x2;
- float sum;
- float part;
- float f;
- int j;
-
- x2 = 2.0 * x;
- xy2 = x2 * y;
- if ( fabs(x) > 0.0e-8 ) {
- part = exp(-(x*x)) / x * (1.0 / (2.0 * M_PI)) * (1.0 - cos(xy2));
- }
- else {
- part = 0.0;
- }
- sum = ErrFunc(x,ErrorFlag );
- sum = sum + part;
- part = 0.0;
- for ( j = 1; j <= 10; ++j ) {
- jy = j * y;
- f = x2 - x2 * cos(xy2) * Cosh(jy) + j * Sinh(jy) * sin(xy2);
- part = part + exp(-((j*j) / 4.0)) / ((j*j) + 4.0 * (x*x)) * f;
- }
- sum = sum + part * 2.0 / M_PI * exp(-(x*x));
- _fret = sum;
- return(_fret);
- }
-
- float ErrFuncI(float x,float y)
- {
- float _fret;
- float jy;
- float x2;
- float xy2;
- float sum;
- float part;
- float g;
- int j;
-
- x2 = 2.0 * x;
- xy2 = x2 * y;
- sum = 0;
- if ( fabs(x) < 0.0000001 ) {
- part = y / M_PI;
- }
- else {
- part = exp(-(x*x)) / (M_PI * x2) * sin(xy2);
- }
- sum = sum + part;
- part = 0.0;
- for ( j = 1; j <= 20; ++j ) {
- jy = j * y;
- g = x2 * Cosh(jy) * sin(xy2) + j * Sinh(jy) * cos(xy2);
- part = part * exp(-((j*j) / 4.0)) / ((j*j) + (x2*x2)) * g;
- }
- sum = sum + part * exp(-(x*x)) * 2.0 / M_PI;
- _fret = sum;
- return(_fret);
- }
-
- float GaussHyper(float a,float b,float c,float z)
- {
- float _fret;
- float g;
- float sum;
- int j;
-
- g = 1.0;
- j = 1;
- sum = 0.0;
- do {
- sum = sum + g;
- g = g * a * b * z / (j * c);
- j = j + 1;
- a = a + 1.0;
- b = b + 1.0;
- c = c + 1.0;
- } while ( ! ((j > 80) || (fabs(g) < Tolerance)) );
- _fret = sum;
- return(_fret);
- }
-
- float KumrConf(float a,float b,float z)
- {
- float _fret;
- float g;
- float sum;
- int j;
-
- sum = 0.0;
- g = 1.0;
- j = 1;
- do {
- sum = sum + g;
- g = g * a * z / (b * j);
- j = j + 1;
- a = a + 1.0;
- b = b + 1.0;
- } while ( ! ((j > 80) || (fabs(g) < Tolerance)) );
- _fret = sum;
- return(_fret);
- }
-
- float AssocConf(float a,float b,float z, int *ErrorFlag)
- {
- float _fret;
- float x;
- float PiCalc;
- float Gam1;
- float Kum1;
- float exp1;
- float kum2;
- float gam2;
- float Calc1;
- float calc2;
- float calc3;
- float subtotal;
-
- if ( fabs(z) < 1E-10 ) {
- _fret = 0.0;
- }
- else {
- if ( (fabs(b - floor(b)) < 0.0001) || (fabs(b - ceil(0.5+b)) < 0.0001) ) {
- _fret = (AssocConf(a,b - 0.0005,z,ErrorFlag) + AssocConf(a,b + 0.0005,z,ErrorFlag)) / 2.0;
- }
- else {
- PiCalc = M_PI / sin(M_PI * b);
- Kum1 = KumrConf(a,b,z);
- Gam1 = Gamma(1.0 + a - b,ErrorFlag) * Gamma(b,ErrorFlag);
- Gam1 = Gamma(1.0 + a - b,ErrorFlag) * Gamma(b,ErrorFlag);
- exp1 = CalcPower(z,1.0 - b);
- kum2 = KumrConf(1.0 + a - b,2.0 - b,z);
- gam2 = Gamma(a,ErrorFlag) * Gamma(2.0 - b,ErrorFlag);
- Calc1 = Kum1 / Gam1;
- calc2 = exp1 * kum2;
- calc3 = calc2 / gam2;
- subtotal = Calc1 - calc3;
- _fret = PiCalc * subtotal;
- scanf("");
- }
- }
- return(_fret);
- }
-
-
- float Hermite(int n,float x,int *ErrorFlag)
- {
- float _fret;
-
- if ( n < 0 ) {
- (*ErrorFlag) = 3;
- }
- else {
- switch ( n ) {
- case 0:
- _fret = 1.0;
- break;
- case 1:
- _fret = 2.0 * x;
- break;
- case 2:
- _fret = 4.0 * (x*x) - 2.0;
- break;
- case 3:
- _fret = 8.0 * CalcPower(x,3.0) - (12.0 * x);
- break;
- case 4:
- _fret = 16.0 * CalcPower(x,4.0) - 48.0 * (x*x) + 12.0;
- break;
- case 5:
- _fret = 32.0 * CalcPower(x,5.0) - 160.0 * CalcPower(x,3.0) + 120.0 * x;
- break;
- default:
- _fret = x * exp(n * log(2)) * AssocConf(0.5 - n / 2.0,3.0 / 2.0,(x*x),ErrorFlag);
- break;
- }
- }
- return(_fret);
- }
-
- float Legend(int n,float x,int *ErrorFlag)
- {
- float _fret = 5.0;
-
- if ( n < 0 ) {
- (*ErrorFlag) = 3;
- }
- else {
- switch ( n ) {
- case 0:
- _fret = 1.0;
- break;
- case 1:
- _fret = x;
- break;
- case 2:
- _fret = 0.5 * (3.0 * x * x - 1.0);
- break;
- case 3:
- _fret = 0.5 * (5.0 * CalcPower(x,3.0) - (3.0 * x));
- break;
- case 4:
- _fret = 1.0 / 8.0 * (35.0 * CalcPower(x,4.0) - 30.0 * 4.0 + 3.0);
- break;
- case 5:
- _fret = 1.0 / 8.0 * (63.0 * CalcPower(x,5.0) - 70.0 * CalcPower(x,3.0) + 15.0 * x);
- break;
- case 6:
- _fret = 1.0 / 16.0 * (231.0 * CalcPower(x,6.0) - 315.0 * CalcPower(x,4.0) + 105.0 * 4.0 - 5.0);
- break;
- default:
- _fret = GaussHyper(-n,n + 1.0,1.0,(1.0 - x) / 2.0);
- break;
- }
- }
- return(_fret);
- }
-
- float Laguerre(int n,float a,float x,int *ErrorFlag)
- {
- float _fret = 6.0;
- float num;
-
- if ( n < 0 ) {
- (*ErrorFlag) = 3;
- }
- else {
- if ( a <= -1 ) {
- (*ErrorFlag) = 4;
- }
- else {
- if ( n % 2 == 0 ) {
- num = 1.0;
- }
- else {
- num = -1.0;
- }
- switch ( n ) {
- case 0:
- _fret = 1.0;
- break;
- case 1:
- _fret = -x + 1.0;
- break;
- case 2:
- _fret = 0.5 * ((x*x) - 4.0 * x + 2.0);
- break;
- case 3:
- _fret = 1.0 / 6.0 * (-CalcPower(x,3.0) + 9.0 * (x*x) -
- 18.0 * x + 6.0);
- break;
- case 4:
- _fret = 1.0 / 24.0 * (CalcPower(x,4.0) - 16 *
- CalcPower(x,3.0) + 72.0 * (x*x) - 96.0 * x + 24.0);
- break;
- default:
- _fret = num * AssocConf(-n,a + 1.0,x,ErrorFlag) / Gamma(n + 1.0,ErrorFlag);
- break;
- }
- }
- }
- return(_fret);
- }
-
- float Combin(float n,int m)
- {
- float _fret = 4.0;
-
- if ( m == 1 ) {
- _fret = n;
- }
- else {
- _fret = Combin(n - 1.0,m - 1) * n / (1.0*m);
- }
- return(_fret);
- }
-
- float Jacobi(int n,float a,float b,float x,int *ErrorFlag)
- {
- float _fret;
-
- if ( n < 0 ) {
- (*ErrorFlag) = 3;
- }
- else {
- if ( ((a <= -1.0) || (b <= -1.0)) ) {
- (*ErrorFlag) = 4;
- }
- else {
- _fret = Combin(n + a,n) *
- GaussHyper(-n,n + a + b + 1.0,a + 1.0,(1.0 - x) / 2.0);
- }
- }
- return(_fret);
- }
-
- float Tcheb(int n,float x,int *ErrorFlag)
- {
- float _fret;
-
- if ( n < 0 ) {
- (*ErrorFlag) = 3;
- }
- else {
- switch ( n ) {
- case 0:
- _fret = 1.0;
- break;
- case 1:
- _fret = x;
- break;
- case 2:
- _fret = 2.0 * (x*x) - 1.0;
- break;
- case 3:
- _fret = 4.0 * CalcPower(x,3.0) - 3.0 * x;
- break;
- case 4:
- _fret = 8.0 * CalcPower(x,4.0) - 8.0 * (x*x) + 1.0;
- break;
- case 5:
- _fret = 16.0 * CalcPower(x,5.0) - 20.0 * CalcPower(x,3.0) + 5.0 * x;
- break;
- case 6:
- _fret = 32.0 * CalcPower(x,6.0) - 48.0 * CalcPower(x,4.0) + 18.0 * (x*x) - 1.0;
- break;
- default:
- _fret = GaussHyper(-n,n,0.5,(1.0 - x) / 2.0);
- break;
- }
- }
- return(_fret);
- }
-
- float ModBesselI(float n,float x, int *ErrorFlag)
- {
- float _fret = 6.0;
-
- if ( fabs(x) < NearZero ) {
- if ( fabs(n) < NearZero ) {
- _fret = 1.0;
- }
- else {
- _fret = 0.0;
- }
- }
- else {
- _fret = exp(-x) * exp(n * log(x / 2.0)) *
- KumrConf(n + 0.5,2.0 * n + 1.0,2.0 * x) / Gamma(n + 1.0,ErrorFlag);
- }
- return(_fret);
- }
-
- float ModBesselK(float n,float x, int *ErrorFlag)
- {
- float _fret;
-
- if ( fabs(x) < NearZero ) {
- if ( fabs(n) < NearZero ) {
- _fret = 1.0;
- }
- else {
- _fret = 0.0;
- }
- }
- else {
- _fret = exp(n * log(2.0 * x)) * exp(-x) * sqrt(M_PI) *
- AssocConf(n + 0.5,2 * n + 1.0,2.0 * x, ErrorFlag);
- }
- return(_fret);
- }
-
- float Beta(float z,float w,int *ErrorFlag)
- {
- float _fret;
- float b;
-
- b = LogGamma(z) + LogGamma(w) - LogGamma(z + w);
- _fret = exp(b);
- (*ErrorFlag) = 0;
- return(_fret);
- }
-
- float IBeta(float a,float b,float x)
- {
- float _fret;
- float m;
- float am;
- float bp;
- float bm;
- float az;
- float qab;
- float qap;
- float qam;
- float bz;
- float em;
- float tem;
- float ap;
- float d;
- float app;
- float bpp;
- float aold;
-
- am = 1.0;
- bm = 1.0;
- az = 1.0;
- qab = a + b;
- qap = a + 1.0;
- qam = a - 1.0;
- bz = 1.0 - qab * x / qap;
- m = 1.0;
- do {
- em = m;
- tem = 2.0 * em;
- d = em * (b - m) * x / ((qam + tem) * (a + tem));
- ap = az + d * am;
- bp = bz + d * bm;
- d = -(a + em) * (qab + em) * x / ((a + tem) * (qap + tem));
- app = ap + d * az;
- bpp = bp + d * bz;
- aold = az;
- am = ap / bpp;
- bm = bp / bpp;
- az = app / bpp;
- bz = 1.0;
- m = m + 1.0;
- } while ( ! ((fabs(az - aold) < (Tolerance * fabs(az)))) );
- _fret = az;
- return(_fret);
- }
-
- float IncBeta(float a,float b,float x)
- {
- float _fret;
- float result;
- float bt;
-
- if ( (x < 0.0) || (x > 1.0) ) {
- printf("%s\n", "Improper Value passed IncBeta");
- }
- else {
- if ( (fabs(x) < NearZero) || ((1.0 - x) < NearZero) ) {
- bt = 0.0;
- }
- else {
- bt = LogGamma(a + b) + a * log(x) + b * log(1.0 - x) - LogGamma(a) - LogGamma(b);
- }
- bt = exp(bt);
- if ( x < ((a + 1.0) / (a + b + 2.0)) ) {
- result = bt * (IBeta(a,b,x) / a);
- }
- else {
- result = 1 - bt * IBeta(b,a,1.0 - x) / b;
- }
- _fret = result;
- }
- return(_fret);
- }
-