zetaf.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* zetaf.c
  2. *
  3. * Riemann zeta function of two arguments
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, q, y, zetaf();
  10. *
  11. * y = zetaf( 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. * Relative error:
  49. * arithmetic domain # trials peak rms
  50. * IEEE 0,25 10000 6.9e-7 1.0e-7
  51. *
  52. * Large arguments may produce underflow in powf(), in which
  53. * case the results are inaccurate.
  54. *
  55. * REFERENCE:
  56. *
  57. * Gradshteyn, I. S., and I. M. Ryzhik, Tables of Integrals,
  58. * Series, and Products, p. 1073; Academic Press, 1980.
  59. *
  60. */
  61. /*
  62. Cephes Math Library Release 2.2: July, 1992
  63. Copyright 1984, 1987, 1992 by Stephen L. Moshier
  64. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  65. */
  66. #include <math.h>
  67. extern float MAXNUMF, MACHEPF;
  68. /* Expansion coefficients
  69. * for Euler-Maclaurin summation formula
  70. * (2k)! / B2k
  71. * where B2k are Bernoulli numbers
  72. */
  73. static float A[] = {
  74. 12.0,
  75. -720.0,
  76. 30240.0,
  77. -1209600.0,
  78. 47900160.0,
  79. -1.8924375803183791606e9, /*1.307674368e12/691*/
  80. 7.47242496e10,
  81. -2.950130727918164224e12, /*1.067062284288e16/3617*/
  82. 1.1646782814350067249e14, /*5.109094217170944e18/43867*/
  83. -4.5979787224074726105e15, /*8.028576626982912e20/174611*/
  84. 1.8152105401943546773e17, /*1.5511210043330985984e23/854513*/
  85. -7.1661652561756670113e18 /*1.6938241367317436694528e27/236364091*/
  86. };
  87. /* 30 Nov 86 -- error in third coefficient fixed */
  88. #define fabsf(x) ( (x) < 0 ? -(x) : (x) )
  89. float powf( float, float );
  90. float zetaf(float xx, float qq)
  91. {
  92. int i;
  93. float x, q, a, b, k, s, w, t;
  94. x = xx;
  95. q = qq;
  96. if( x == 1.0 )
  97. return( MAXNUMF );
  98. if( x < 1.0 )
  99. {
  100. mtherr( "zetaf", DOMAIN );
  101. return(0.0);
  102. }
  103. /* Euler-Maclaurin summation formula */
  104. /*
  105. if( x < 25.0 )
  106. {
  107. */
  108. w = 9.0;
  109. s = powf( q, -x );
  110. a = q;
  111. for( i=0; i<9; i++ )
  112. {
  113. a += 1.0;
  114. b = powf( a, -x );
  115. s += b;
  116. if( b/s < MACHEPF )
  117. goto done;
  118. }
  119. w = a;
  120. s += b*w/(x-1.0);
  121. s -= 0.5 * b;
  122. a = 1.0;
  123. k = 0.0;
  124. for( i=0; i<12; i++ )
  125. {
  126. a *= x + k;
  127. b /= w;
  128. t = a*b/A[i];
  129. s = s + t;
  130. t = fabsf(t/s);
  131. if( t < MACHEPF )
  132. goto done;
  133. k += 1.0;
  134. a *= x + k;
  135. b /= w;
  136. k += 1.0;
  137. }
  138. done:
  139. return(s);
  140. /*
  141. }
  142. */
  143. /* Basic sum of inverse powers */
  144. /*
  145. pseres:
  146. s = powf( q, -x );
  147. a = q;
  148. do
  149. {
  150. a += 2.0;
  151. b = powf( a, -x );
  152. s += b;
  153. }
  154. while( b/s > MACHEPF );
  155. b = powf( 2.0, -x );
  156. s = (s + b)/(1.0-b);
  157. return(s);
  158. */
  159. }