Sindbad~EG File Manager
/* digamma and first polygamma function
9.17.92 Adapted and CORRECTED! from: C Mathematical Function Handbook by Louis Baker
Modified by M. Beeson for use in Mathpert.
In particular, 'abs' has to be changed to 'fabs' or you compute
very wrong values.
8.20.07 Now we have polygamma as a binary function
9.25.14 removed unused function unary_polygamma.
*/
#include <math.h>
#include "special.h"
#define BADVAL 2.0E300
#define pi 3.141592653589793238462643383279
#define Egamma 0.577215664901532860606512090082402431
double pg1(int k, double x)
// k must be non-negative
{ if(k==0)
return digamma(x);
// if(k==1)
//return unary_polygamma(x);
return polygamma(k,x);
}
/*_________________________________________________________________*/
double digamma(double x)
{
static double digammin;
double q,y,z,sum;
q=x;
sum=0.;
if(q==0.)
return BADVAL;
if(digammin<=0.)
digammin=30.;
if(q<0.)
{ if( fabs((int)(q)-q) <1.e-8)
return BADVAL;
q=1.-x;sum=pi/tan(pi*x); /*note sum subtracted from final result*/
}
while(q<digammin)
{ if(q==0.)
return BADVAL;
sum+=1./q;
q++;
}
y=1/q;
z=y*y;
/* logx-1/2x- sum n=1 to inf of B[2n]z^-2n/2n*/
return log(q)-sum-.5*y-z*(1./12.-z*(1/120.-z*(1/252.-z/240.)));
}
#undef pi
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists