zeta.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* zeta.c
  2. *
  3. * Riemann zeta function of two arguments
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, q, y, zeta();
  10. *
  11. * y = zeta( x, q );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. *
  18. *
  19. * inf.
  20. * - -x
  21. * zeta(x,q) = > (k+q)
  22. * -
  23. * k=0
  24. *
  25. * where x > 1 and q is not a negative integer or zero.
  26. * The Euler-Maclaurin summation formula is used to obtain
  27. * the expansion
  28. *
  29. * n
  30. * - -x
  31. * zeta(x,q) = > (k+q)
  32. * -
  33. * k=1
  34. *
  35. * 1-x inf. B x(x+1)...(x+2j)
  36. * (n+q) 1 - 2j
  37. * + --------- - ------- + > --------------------
  38. * x-1 x - x+2j+1
  39. * 2(n+q) j=1 (2j)! (n+q)
  40. *
  41. * where the B2j are Bernoulli numbers. Note that (see zetac.c)
  42. * zeta(x,1) = zetac(x) + 1.
  43. *
  44. *
  45. *
  46. * ACCURACY:
  47. *
  48. *
  49. *
  50. * REFERENCE:
  51. *
  52. * Gradshteyn, I. S., and I. M. Ryzhik, Tables of Integrals,
  53. * Series, and Products, p. 1073; Academic Press, 1980.
  54. *
  55. */
  56. /*
  57. Cephes Math Library Release 2.8: June, 2000
  58. Copyright 1984, 1987, 2000 by Stephen L. Moshier
  59. */
  60. #include <math.h>
  61. #ifdef ANSIPROT
  62. extern double fabs ( double );
  63. extern double pow ( double, double );
  64. extern double floor ( double );
  65. #else
  66. double fabs(), pow(), floor();
  67. #endif
  68. extern double MAXNUM, MACHEP;
  69. /* Expansion coefficients
  70. * for Euler-Maclaurin summation formula
  71. * (2k)! / B2k
  72. * where B2k are Bernoulli numbers
  73. */
  74. static double A[] = {
  75. 12.0,
  76. -720.0,
  77. 30240.0,
  78. -1209600.0,
  79. 47900160.0,
  80. -1.8924375803183791606e9, /*1.307674368e12/691*/
  81. 7.47242496e10,
  82. -2.950130727918164224e12, /*1.067062284288e16/3617*/
  83. 1.1646782814350067249e14, /*5.109094217170944e18/43867*/
  84. -4.5979787224074726105e15, /*8.028576626982912e20/174611*/
  85. 1.8152105401943546773e17, /*1.5511210043330985984e23/854513*/
  86. -7.1661652561756670113e18 /*1.6938241367317436694528e27/236364091*/
  87. };
  88. /* 30 Nov 86 -- error in third coefficient fixed */
  89. double zeta(x,q)
  90. double x,q;
  91. {
  92. int i;
  93. double a, b, k, s, t, w;
  94. if( x == 1.0 )
  95. goto retinf;
  96. if( x < 1.0 )
  97. {
  98. domerr:
  99. mtherr( "zeta", DOMAIN );
  100. return(0.0);
  101. }
  102. if( q <= 0.0 )
  103. {
  104. if(q == floor(q))
  105. {
  106. mtherr( "zeta", SING );
  107. retinf:
  108. return( MAXNUM );
  109. }
  110. if( x != floor(x) )
  111. goto domerr; /* because q^-x not defined */
  112. }
  113. /* Euler-Maclaurin summation formula */
  114. /*
  115. if( x < 25.0 )
  116. */
  117. {
  118. /* Permit negative q but continue sum until n+q > +9 .
  119. * This case should be handled by a reflection formula.
  120. * If q<0 and x is an integer, there is a relation to
  121. * the polygamma function.
  122. */
  123. s = pow( q, -x );
  124. a = q;
  125. i = 0;
  126. b = 0.0;
  127. while( (i < 9) || (a <= 9.0) )
  128. {
  129. i += 1;
  130. a += 1.0;
  131. b = pow( a, -x );
  132. s += b;
  133. if( fabs(b/s) < MACHEP )
  134. goto done;
  135. }
  136. w = a;
  137. s += b*w/(x-1.0);
  138. s -= 0.5 * b;
  139. a = 1.0;
  140. k = 0.0;
  141. for( i=0; i<12; i++ )
  142. {
  143. a *= x + k;
  144. b /= w;
  145. t = a*b/A[i];
  146. s = s + t;
  147. t = fabs(t/s);
  148. if( t < MACHEP )
  149. goto done;
  150. k += 1.0;
  151. a *= x + k;
  152. b /= w;
  153. k += 1.0;
  154. }
  155. done:
  156. return(s);
  157. }
  158. /* Basic sum of inverse powers */
  159. /*
  160. pseres:
  161. s = pow( q, -x );
  162. a = q;
  163. do
  164. {
  165. a += 2.0;
  166. b = pow( a, -x );
  167. s += b;
  168. }
  169. while( b/s > MACHEP );
  170. b = pow( 2.0, -x );
  171. s = (s + b)/(1.0-b);
  172. return(s);
  173. */
  174. }