polylog.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* polylog.c
  2. *
  3. * Polylogarithms
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, y, polylog();
  10. * int n;
  11. *
  12. * y = polylog( n, x );
  13. *
  14. *
  15. * The polylogarithm of order n is defined by the series
  16. *
  17. *
  18. * inf k
  19. * - x
  20. * Li (x) = > --- .
  21. * n - n
  22. * k=1 k
  23. *
  24. *
  25. * For x = 1,
  26. *
  27. * inf
  28. * - 1
  29. * Li (1) = > --- = Riemann zeta function (n) .
  30. * n - n
  31. * k=1 k
  32. *
  33. *
  34. * When n = 2, the function is the dilogarithm, related to Spence's integral:
  35. *
  36. * x 1-x
  37. * - -
  38. * | | -ln(1-t) | | ln t
  39. * Li (x) = | -------- dt = | ------ dt = spence(1-x) .
  40. * 2 | | t | | 1 - t
  41. * - -
  42. * 0 1
  43. *
  44. *
  45. * See also the program cpolylog.c for the complex polylogarithm,
  46. * whose definition is extended to x > 1.
  47. *
  48. * References:
  49. *
  50. * Lewin, L., _Polylogarithms and Associated Functions_,
  51. * North Holland, 1981.
  52. *
  53. * Lewin, L., ed., _Structural Properties of Polylogarithms_,
  54. * American Mathematical Society, 1991.
  55. *
  56. *
  57. * ACCURACY:
  58. *
  59. * Relative error:
  60. * arithmetic domain n # trials peak rms
  61. * IEEE 0, 1 2 50000 6.2e-16 8.0e-17
  62. * IEEE 0, 1 3 100000 2.5e-16 6.6e-17
  63. * IEEE 0, 1 4 30000 1.7e-16 4.9e-17
  64. * IEEE 0, 1 5 30000 5.1e-16 7.8e-17
  65. *
  66. */
  67. /*
  68. Cephes Math Library Release 2.8: July, 1999
  69. Copyright 1999 by Stephen L. Moshier
  70. */
  71. #include "mconf.h"
  72. extern double PI;
  73. /* polylog(4, 1-x) = zeta(4) - x zeta(3) + x^2 A4(x)/B4(x)
  74. 0 <= x <= 0.125
  75. Theoretical peak absolute error 4.5e-18 */
  76. #if UNK
  77. static double A4[13] = {
  78. 3.056144922089490701751E-2, 3.243086484162581557457E-1,
  79. 2.877847281461875922565E-1, 7.091267785886180663385E-2,
  80. 6.466460072456621248630E-3, 2.450233019296542883275E-4,
  81. 4.031655364627704957049E-6, 2.884169163909467997099E-8,
  82. 8.680067002466594858347E-11, 1.025983405866370985438E-13,
  83. 4.233468313538272640380E-17, 4.959422035066206902317E-21,
  84. 1.059365867585275714599E-25,
  85. };
  86. static double B4[12] = {
  87. /* 1.000000000000000000000E0, */
  88. 2.821262403600310974875E0, 1.780221124881327022033E0,
  89. 3.778888211867875721773E-1, 3.193887040074337940323E-2,
  90. 1.161252418498096498304E-3, 1.867362374829870620091E-5,
  91. 1.319022779715294371091E-7, 3.942755256555603046095E-10,
  92. 4.644326968986396928092E-13, 1.913336021014307074861E-16,
  93. 2.240041814626069927477E-20, 4.784036597230791011855E-25,
  94. };
  95. #endif
  96. #if DEC
  97. static short A4[52] = {
  98. 0036772, 0056001, 0016601, 0164507, 0037646, 0005710, 0076603, 0176456,
  99. 0037623, 0054205, 0013532, 0026476, 0037221, 0035252, 0101064, 0065407,
  100. 0036323, 0162231, 0042033, 0107244, 0035200, 0073170, 0106141, 0136543,
  101. 0033607, 0043647, 0163672, 0055340, 0031767, 0137614, 0173376, 0072313,
  102. 0027676, 0160156, 0161276, 0034203, 0025347, 0003752, 0123106, 0064266,
  103. 0022503, 0035770, 0160173, 0177501, 0017273, 0056226, 0033704, 0132530,
  104. 0013403, 0022244, 0175205, 0052161,
  105. };
  106. static short B4[48] = {
  107. /*0040200,0000000,0000000,0000000, */
  108. 0040464, 0107620, 0027471, 0071672, 0040343, 0157111, 0025601, 0137255,
  109. 0037701, 0075244, 0140412, 0160220, 0037002, 0151125, 0036572, 0057163,
  110. 0035630, 0032452, 0050727, 0161653, 0034234, 0122515, 0034323, 0172615,
  111. 0032415, 0120405, 0123660, 0003160, 0030330, 0140530, 0161045, 0150177,
  112. 0026002, 0134747, 0014542, 0002510, 0023134, 0113666, 0035730, 0035732,
  113. 0017723, 0110343, 0041217, 0007764, 0014024, 0007412, 0175575, 0160230,
  114. };
  115. #endif
  116. #if IBMPC
  117. static short A4[52] = {
  118. 0x3d29, 0x23b0, 0x4b80, 0x3f9f, 0x7fa6, 0x0fb0, 0xc179, 0x3fd4, 0x45a8,
  119. 0xa2eb, 0x6b10, 0x3fd2, 0x8d61, 0x5046, 0x2755, 0x3fb2, 0x71d4, 0x2883,
  120. 0x7c93, 0x3f7a, 0x37ac, 0x118c, 0x0ecf, 0x3f30, 0x4b5c, 0xfcf7, 0xe8f4,
  121. 0x3ed0, 0xce99, 0x9edf, 0xf7f1, 0x3e5e, 0xc710, 0xdc57, 0xdc0d, 0x3dd7,
  122. 0xcd17, 0x54c8, 0xe0fd, 0x3d3c, 0x7fe8, 0x1c0f, 0x677f, 0x3c88, 0x96ab,
  123. 0xc6f8, 0x6b92, 0x3bb7, 0xaa8e, 0x9f50, 0x6494, 0x3ac0,
  124. };
  125. static short B4[48] = {
  126. /*0x0000,0x0000,0x0000,0x3ff0,*/
  127. 0x2e77, 0x05e7, 0x91f2, 0x4006, 0x37d6, 0x2570, 0x7bc9, 0x3ffc,
  128. 0x5c12, 0x9821, 0x2f54, 0x3fd8, 0x4bce, 0xa7af, 0x5a4a, 0x3fa0,
  129. 0xfc75, 0x4a3a, 0x06a5, 0x3f53, 0x7eb2, 0xa71a, 0x94a9, 0x3ef3,
  130. 0x00ce, 0xb4f6, 0xb420, 0x3e81, 0xba10, 0x1c44, 0x182b, 0x3dfb,
  131. 0x40a9, 0xe32c, 0x573c, 0x3d60, 0x077b, 0xc77b, 0x92f6, 0x3cab,
  132. 0xe1fe, 0x6851, 0x721c, 0x3bda, 0xbc13, 0x5f6f, 0x81e1, 0x3ae2,
  133. };
  134. #endif
  135. #if MIEEE
  136. static short A4[52] = {
  137. 0x3f9f, 0x4b80, 0x23b0, 0x3d29, 0x3fd4, 0xc179, 0x0fb0, 0x7fa6, 0x3fd2,
  138. 0x6b10, 0xa2eb, 0x45a8, 0x3fb2, 0x2755, 0x5046, 0x8d61, 0x3f7a, 0x7c93,
  139. 0x2883, 0x71d4, 0x3f30, 0x0ecf, 0x118c, 0x37ac, 0x3ed0, 0xe8f4, 0xfcf7,
  140. 0x4b5c, 0x3e5e, 0xf7f1, 0x9edf, 0xce99, 0x3dd7, 0xdc0d, 0xdc57, 0xc710,
  141. 0x3d3c, 0xe0fd, 0x54c8, 0xcd17, 0x3c88, 0x677f, 0x1c0f, 0x7fe8, 0x3bb7,
  142. 0x6b92, 0xc6f8, 0x96ab, 0x3ac0, 0x6494, 0x9f50, 0xaa8e,
  143. };
  144. static short B4[48] = {
  145. /*0x3ff0,0x0000,0x0000,0x0000,*/
  146. 0x4006, 0x91f2, 0x05e7, 0x2e77, 0x3ffc, 0x7bc9, 0x2570, 0x37d6,
  147. 0x3fd8, 0x2f54, 0x9821, 0x5c12, 0x3fa0, 0x5a4a, 0xa7af, 0x4bce,
  148. 0x3f53, 0x06a5, 0x4a3a, 0xfc75, 0x3ef3, 0x94a9, 0xa71a, 0x7eb2,
  149. 0x3e81, 0xb420, 0xb4f6, 0x00ce, 0x3dfb, 0x182b, 0x1c44, 0xba10,
  150. 0x3d60, 0x573c, 0xe32c, 0x40a9, 0x3cab, 0x92f6, 0xc77b, 0x077b,
  151. 0x3bda, 0x721c, 0x6851, 0xe1fe, 0x3ae2, 0x81e1, 0x5f6f, 0xbc13,
  152. };
  153. #endif
  154. #ifdef ANSIPROT
  155. extern double spence(double);
  156. extern double polevl(double, void *, int);
  157. extern double p1evl(double, void *, int);
  158. extern double zetac(double);
  159. extern double pow(double, double);
  160. extern double powi(double, int);
  161. extern double log(double);
  162. extern double fac(int i);
  163. extern double fabs(double);
  164. double polylog(int, double);
  165. #else
  166. extern double spence(), polevl(), p1evl(), zetac();
  167. extern double pow(), powi(), log();
  168. extern double fac(); /* factorial */
  169. extern double fabs();
  170. double polylog();
  171. #endif
  172. extern double MACHEP;
  173. double polylog(n, x) int n;
  174. double x;
  175. {
  176. double h, k, p, s, t, u, xc, z;
  177. int i, j;
  178. /* This recurrence provides formulas for n < 2.
  179. d 1
  180. -- Li (x) = --- Li (x) .
  181. dx n x n-1
  182. */
  183. if (n == -1) {
  184. p = 1.0 - x;
  185. u = x / p;
  186. s = u * u + u;
  187. return s;
  188. }
  189. if (n == 0) {
  190. s = x / (1.0 - x);
  191. return s;
  192. }
  193. /* Not implemented for n < -1.
  194. Not defined for x > 1. Use cpolylog if you need that. */
  195. if (x > 1.0 || n < -1) {
  196. mtherr("polylog", DOMAIN);
  197. return 0.0;
  198. }
  199. if (n == 1) {
  200. s = -log(1.0 - x);
  201. return s;
  202. }
  203. /* Argument +1 */
  204. if (x == 1.0 && n > 1) {
  205. s = zetac((double)n) + 1.0;
  206. return s;
  207. }
  208. /* Argument -1.
  209. 1-n
  210. Li (-z) = - (1 - 2 ) Li (z)
  211. n n
  212. */
  213. if (x == -1.0 && n > 1) {
  214. /* Li_n(1) = zeta(n) */
  215. s = zetac((double)n) + 1.0;
  216. s = s * (powi(2.0, 1 - n) - 1.0);
  217. return s;
  218. }
  219. /* Inversion formula:
  220. * [n/2] n-2r
  221. * n 1 n - log (z)
  222. * Li (-z) + (-1) Li (-1/z) = - --- log (z) + 2 > ----------- Li (-1)
  223. * n n n! - (n - 2r)! 2r
  224. * r=1
  225. */
  226. if (x < -1.0 && n > 1) {
  227. double q, w;
  228. int r;
  229. w = log(-x);
  230. s = 0.0;
  231. for (r = 1; r <= n / 2; r++) {
  232. j = 2 * r;
  233. p = polylog(j, -1.0);
  234. j = n - j;
  235. if (j == 0) {
  236. s = s + p;
  237. break;
  238. }
  239. q = (double)j;
  240. q = pow(w, q) * p / fac(j);
  241. s = s + q;
  242. }
  243. s = 2.0 * s;
  244. q = polylog(n, 1.0 / x);
  245. if (n & 1)
  246. q = -q;
  247. s = s - q;
  248. s = s - pow(w, (double)n) / fac(n);
  249. return s;
  250. }
  251. if (n == 2) {
  252. if (x < 0.0 || x > 1.0)
  253. return (spence(1.0 - x));
  254. }
  255. /* The power series converges slowly when x is near 1. For n = 3, this
  256. identity helps:
  257. Li (-x/(1-x)) + Li (1-x) + Li (x)
  258. 3 3 3
  259. 2 2 3
  260. = Li (1) + (pi /6) log(1-x) - (1/2) log(x) log (1-x) + (1/6) log (1-x)
  261. 3
  262. */
  263. if (n == 3) {
  264. p = x * x * x;
  265. if (x > 0.8) {
  266. /* Thanks to Oscar van Vlijmen for detecting an error here. */
  267. u = log(x);
  268. s = u * u * u / 6.0;
  269. xc = 1.0 - x;
  270. s = s - 0.5 * u * u * log(xc);
  271. s = s + PI * PI * u / 6.0;
  272. s = s - polylog(3, -xc / x);
  273. s = s - polylog(3, xc);
  274. s = s + zetac(3.0);
  275. s = s + 1.0;
  276. return s;
  277. }
  278. /* Power series */
  279. t = p / 27.0;
  280. t = t + .125 * x * x;
  281. t = t + x;
  282. s = 0.0;
  283. k = 4.0;
  284. do {
  285. p = p * x;
  286. h = p / (k * k * k);
  287. s = s + h;
  288. k += 1.0;
  289. } while (fabs(h / s) > 1.1e-16);
  290. return (s + t);
  291. }
  292. if (n == 4) {
  293. if (x >= 0.875) {
  294. u = 1.0 - x;
  295. s = polevl(u, A4, 12) / p1evl(u, B4, 12);
  296. s = s * u * u - 1.202056903159594285400 * u;
  297. s += 1.0823232337111381915160;
  298. return s;
  299. }
  300. goto pseries;
  301. }
  302. if (x < 0.75)
  303. goto pseries;
  304. /* This expansion in powers of log(x) is especially useful when
  305. x is near 1.
  306. See also the pari gp calculator.
  307. inf j
  308. - z(n-j) (log(x))
  309. polylog(n,x) = > -----------------
  310. - j!
  311. j=0
  312. where
  313. z(j) = Riemann zeta function (j), j != 1
  314. n-1
  315. -
  316. z(1) = -log(-log(x)) + > 1/k
  317. -
  318. k=1
  319. */
  320. z = log(x);
  321. h = -log(-z);
  322. for (i = 1; i < n; i++)
  323. h = h + 1.0 / i;
  324. p = 1.0;
  325. s = zetac((double)n) + 1.0;
  326. for (j = 1; j <= n + 1; j++) {
  327. p = p * z / j;
  328. if (j == n - 1)
  329. s = s + h * p;
  330. else
  331. s = s + (zetac((double)(n - j)) + 1.0) * p;
  332. }
  333. j = n + 3;
  334. z = z * z;
  335. for (;;) {
  336. p = p * z / ((j - 1) * j);
  337. h = (zetac((double)(n - j)) + 1.0);
  338. h = h * p;
  339. s = s + h;
  340. if (fabs(h / s) < MACHEP)
  341. break;
  342. j += 2;
  343. }
  344. return s;
  345. pseries:
  346. p = x * x * x;
  347. k = 3.0;
  348. s = 0.0;
  349. do {
  350. p = p * x;
  351. k += 1.0;
  352. h = p / powi(k, n);
  353. s = s + h;
  354. } while (fabs(h / s) > MACHEP);
  355. s += x * x * x / powi(3.0, n);
  356. s += x * x / powi(2.0, n);
  357. s += x;
  358. return s;
  359. }