// cgamma.cpp -- Complex gamma function. // Algorithms and coefficient values from "Computation of Special // Functions", Zhang and Jin, John Wiley and Sons, 1996. // // (C) 2003, C. Bond. All rights reserved. // // Returns gamma function or log(gamma) for complex argument 'z'. // // OPT value function // --------- -------- // 0 complex gamma // 1 complex log(gamma) // // Returns (1e308,0) if the real part of the argument is a negative integer // or 0 or exceeds 171. // #include complex cgamma(complex z,int OPT) { complex g,z0,z1; double x0,q1,q2,x,y,th,th1,th2,g0,gr,gi,gr1,gi1; double na,t,x1,y1,sr,si; int i,j,k; static double a[] = { 8.333333333333333e-02, -2.777777777777778e-03, 7.936507936507937e-04, -5.952380952380952e-04, 8.417508417508418e-04, -1.917526917526918e-03, 6.410256410256410e-03, -2.955065359477124e-02, 1.796443723688307e-01, -1.39243221690590}; x = real(z); y = imag(z); if (x > 171) return complex(1e308,0); if ((y == 0.0) && (x == (int)x) && (x <= 0.0)) return complex(1e308,0); else if (x < 0.0) { x1 = x; y1 = y; x = -x; y = -y; } x0 = x; if (x <= 7.0) { na = (int)(7.0-x); x0 = x+na; } q1 = sqrt(x0*x0+y*y); th = atan(y/x0); gr = (x0-0.5)*log(q1)-th*y-x0+0.5*log(2.0*M_PI); gi = th*(x0-0.5)+y*log(q1)-y; for (k=0;k<10;k++){ t = pow(q1,-1.0-2.0*k); gr += (a[k]*t*cos((2.0*k+1.0)*th)); gi -= (a[k]*t*sin((2.0*k+1.0)*th)); } if (x <= 7.0) { gr1 = 0.0; gi1 = 0.0; for (j=0;j(gr,gi); return g; }