spence.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* spence.c
  2. *
  3. * Dilogarithm
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, y, spence();
  10. *
  11. * y = spence( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Computes the integral
  18. *
  19. * x
  20. * -
  21. * | | log t
  22. * spence(x) = - | ----- dt
  23. * | | t - 1
  24. * -
  25. * 1
  26. *
  27. * for x >= 0. A rational approximation gives the integral in
  28. * the interval (0.5, 1.5). Transformation formulas for 1/x
  29. * and 1-x are employed outside the basic expansion range.
  30. *
  31. *
  32. *
  33. * ACCURACY:
  34. *
  35. * Relative error:
  36. * arithmetic domain # trials peak rms
  37. * IEEE 0,4 30000 3.9e-15 5.4e-16
  38. * DEC 0,4 3000 2.5e-16 4.5e-17
  39. *
  40. *
  41. */
  42. /* spence.c */
  43. /*
  44. Cephes Math Library Release 2.8: June, 2000
  45. Copyright 1985, 1987, 1989, 2000 by Stephen L. Moshier
  46. */
  47. #include <math.h>
  48. #ifdef UNK
  49. static double A[8] = {
  50. 4.65128586073990045278E-5,
  51. 7.31589045238094711071E-3,
  52. 1.33847639578309018650E-1,
  53. 8.79691311754530315341E-1,
  54. 2.71149851196553469920E0,
  55. 4.25697156008121755724E0,
  56. 3.29771340985225106936E0,
  57. 1.00000000000000000126E0,
  58. };
  59. static double B[8] = {
  60. 6.90990488912553276999E-4,
  61. 2.54043763932544379113E-2,
  62. 2.82974860602568089943E-1,
  63. 1.41172597751831069617E0,
  64. 3.63800533345137075418E0,
  65. 5.03278880143316990390E0,
  66. 3.54771340985225096217E0,
  67. 9.99999999999999998740E-1,
  68. };
  69. #endif
  70. #ifdef DEC
  71. static unsigned short A[32] = {
  72. 0034503,0013315,0034120,0157771,
  73. 0036357,0135043,0016766,0150637,
  74. 0037411,0007533,0005212,0161475,
  75. 0040141,0031563,0023217,0120331,
  76. 0040455,0104461,0007002,0155522,
  77. 0040610,0034434,0065721,0120465,
  78. 0040523,0006674,0105671,0054427,
  79. 0040200,0000000,0000000,0000000,
  80. };
  81. static unsigned short B[32] = {
  82. 0035465,0021626,0032367,0144157,
  83. 0036720,0016326,0134431,0000406,
  84. 0037620,0161024,0133701,0120766,
  85. 0040264,0131557,0152055,0064512,
  86. 0040550,0152424,0051166,0034272,
  87. 0040641,0006233,0014672,0111572,
  88. 0040543,0006674,0105671,0054425,
  89. 0040200,0000000,0000000,0000000,
  90. };
  91. #endif
  92. #ifdef IBMPC
  93. static unsigned short A[32] = {
  94. 0x1bff,0xa70a,0x62d9,0x3f08,
  95. 0xda34,0x63be,0xf744,0x3f7d,
  96. 0x5c68,0x6151,0x21eb,0x3fc1,
  97. 0xf41b,0x64d1,0x266e,0x3fec,
  98. 0x5b6a,0x21c0,0xb126,0x4005,
  99. 0x3427,0x8d7a,0x0723,0x4011,
  100. 0x2b23,0x9177,0x61b7,0x400a,
  101. 0x0000,0x0000,0x0000,0x3ff0,
  102. };
  103. static unsigned short B[32] = {
  104. 0xf90e,0xc69e,0xa472,0x3f46,
  105. 0x2021,0xd723,0x039a,0x3f9a,
  106. 0x343f,0x96f8,0x1c42,0x3fd2,
  107. 0xad29,0xfa85,0x966d,0x3ff6,
  108. 0xc717,0x8a4e,0x1aa2,0x400d,
  109. 0x526f,0x6337,0x2193,0x4014,
  110. 0x2b23,0x9177,0x61b7,0x400c,
  111. 0x0000,0x0000,0x0000,0x3ff0,
  112. };
  113. #endif
  114. #ifdef MIEEE
  115. static unsigned short A[32] = {
  116. 0x3f08,0x62d9,0xa70a,0x1bff,
  117. 0x3f7d,0xf744,0x63be,0xda34,
  118. 0x3fc1,0x21eb,0x6151,0x5c68,
  119. 0x3fec,0x266e,0x64d1,0xf41b,
  120. 0x4005,0xb126,0x21c0,0x5b6a,
  121. 0x4011,0x0723,0x8d7a,0x3427,
  122. 0x400a,0x61b7,0x9177,0x2b23,
  123. 0x3ff0,0x0000,0x0000,0x0000,
  124. };
  125. static unsigned short B[32] = {
  126. 0x3f46,0xa472,0xc69e,0xf90e,
  127. 0x3f9a,0x039a,0xd723,0x2021,
  128. 0x3fd2,0x1c42,0x96f8,0x343f,
  129. 0x3ff6,0x966d,0xfa85,0xad29,
  130. 0x400d,0x1aa2,0x8a4e,0xc717,
  131. 0x4014,0x2193,0x6337,0x526f,
  132. 0x400c,0x61b7,0x9177,0x2b23,
  133. 0x3ff0,0x0000,0x0000,0x0000,
  134. };
  135. #endif
  136. #ifdef ANSIPROT
  137. extern double fabs ( double );
  138. extern double log ( double );
  139. extern double polevl ( double, void *, int );
  140. #else
  141. double fabs(), log(), polevl();
  142. #endif
  143. extern double PI, MACHEP;
  144. double spence(x)
  145. double x;
  146. {
  147. double w, y, z;
  148. int flag;
  149. if( x < 0.0 )
  150. {
  151. mtherr( "spence", DOMAIN );
  152. return(0.0);
  153. }
  154. if( x == 1.0 )
  155. return( 0.0 );
  156. if( x == 0.0 )
  157. return( PI*PI/6.0 );
  158. flag = 0;
  159. if( x > 2.0 )
  160. {
  161. x = 1.0/x;
  162. flag |= 2;
  163. }
  164. if( x > 1.5 )
  165. {
  166. w = (1.0/x) - 1.0;
  167. flag |= 2;
  168. }
  169. else if( x < 0.5 )
  170. {
  171. w = -x;
  172. flag |= 1;
  173. }
  174. else
  175. w = x - 1.0;
  176. y = -w * polevl( w, A, 7) / polevl( w, B, 7 );
  177. if( flag & 1 )
  178. y = (PI * PI)/6.0 - log(x) * log(1.0-x) - y;
  179. if( flag & 2 )
  180. {
  181. z = log(x);
  182. y = -0.5 * z * z - y;
  183. }
  184. return( y );
  185. }