polylog.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 <math.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,
  79. 3.243086484162581557457E-1,
  80. 2.877847281461875922565E-1,
  81. 7.091267785886180663385E-2,
  82. 6.466460072456621248630E-3,
  83. 2.450233019296542883275E-4,
  84. 4.031655364627704957049E-6,
  85. 2.884169163909467997099E-8,
  86. 8.680067002466594858347E-11,
  87. 1.025983405866370985438E-13,
  88. 4.233468313538272640380E-17,
  89. 4.959422035066206902317E-21,
  90. 1.059365867585275714599E-25,
  91. };
  92. static double B4[12] = {
  93. /* 1.000000000000000000000E0, */
  94. 2.821262403600310974875E0,
  95. 1.780221124881327022033E0,
  96. 3.778888211867875721773E-1,
  97. 3.193887040074337940323E-2,
  98. 1.161252418498096498304E-3,
  99. 1.867362374829870620091E-5,
  100. 1.319022779715294371091E-7,
  101. 3.942755256555603046095E-10,
  102. 4.644326968986396928092E-13,
  103. 1.913336021014307074861E-16,
  104. 2.240041814626069927477E-20,
  105. 4.784036597230791011855E-25,
  106. };
  107. #endif
  108. #if DEC
  109. static short A4[52] = {
  110. 0036772,0056001,0016601,0164507,
  111. 0037646,0005710,0076603,0176456,
  112. 0037623,0054205,0013532,0026476,
  113. 0037221,0035252,0101064,0065407,
  114. 0036323,0162231,0042033,0107244,
  115. 0035200,0073170,0106141,0136543,
  116. 0033607,0043647,0163672,0055340,
  117. 0031767,0137614,0173376,0072313,
  118. 0027676,0160156,0161276,0034203,
  119. 0025347,0003752,0123106,0064266,
  120. 0022503,0035770,0160173,0177501,
  121. 0017273,0056226,0033704,0132530,
  122. 0013403,0022244,0175205,0052161,
  123. };
  124. static short B4[48] = {
  125. /*0040200,0000000,0000000,0000000, */
  126. 0040464,0107620,0027471,0071672,
  127. 0040343,0157111,0025601,0137255,
  128. 0037701,0075244,0140412,0160220,
  129. 0037002,0151125,0036572,0057163,
  130. 0035630,0032452,0050727,0161653,
  131. 0034234,0122515,0034323,0172615,
  132. 0032415,0120405,0123660,0003160,
  133. 0030330,0140530,0161045,0150177,
  134. 0026002,0134747,0014542,0002510,
  135. 0023134,0113666,0035730,0035732,
  136. 0017723,0110343,0041217,0007764,
  137. 0014024,0007412,0175575,0160230,
  138. };
  139. #endif
  140. #if IBMPC
  141. static short A4[52] = {
  142. 0x3d29,0x23b0,0x4b80,0x3f9f,
  143. 0x7fa6,0x0fb0,0xc179,0x3fd4,
  144. 0x45a8,0xa2eb,0x6b10,0x3fd2,
  145. 0x8d61,0x5046,0x2755,0x3fb2,
  146. 0x71d4,0x2883,0x7c93,0x3f7a,
  147. 0x37ac,0x118c,0x0ecf,0x3f30,
  148. 0x4b5c,0xfcf7,0xe8f4,0x3ed0,
  149. 0xce99,0x9edf,0xf7f1,0x3e5e,
  150. 0xc710,0xdc57,0xdc0d,0x3dd7,
  151. 0xcd17,0x54c8,0xe0fd,0x3d3c,
  152. 0x7fe8,0x1c0f,0x677f,0x3c88,
  153. 0x96ab,0xc6f8,0x6b92,0x3bb7,
  154. 0xaa8e,0x9f50,0x6494,0x3ac0,
  155. };
  156. static short B4[48] = {
  157. /*0x0000,0x0000,0x0000,0x3ff0,*/
  158. 0x2e77,0x05e7,0x91f2,0x4006,
  159. 0x37d6,0x2570,0x7bc9,0x3ffc,
  160. 0x5c12,0x9821,0x2f54,0x3fd8,
  161. 0x4bce,0xa7af,0x5a4a,0x3fa0,
  162. 0xfc75,0x4a3a,0x06a5,0x3f53,
  163. 0x7eb2,0xa71a,0x94a9,0x3ef3,
  164. 0x00ce,0xb4f6,0xb420,0x3e81,
  165. 0xba10,0x1c44,0x182b,0x3dfb,
  166. 0x40a9,0xe32c,0x573c,0x3d60,
  167. 0x077b,0xc77b,0x92f6,0x3cab,
  168. 0xe1fe,0x6851,0x721c,0x3bda,
  169. 0xbc13,0x5f6f,0x81e1,0x3ae2,
  170. };
  171. #endif
  172. #if MIEEE
  173. static short A4[52] = {
  174. 0x3f9f,0x4b80,0x23b0,0x3d29,
  175. 0x3fd4,0xc179,0x0fb0,0x7fa6,
  176. 0x3fd2,0x6b10,0xa2eb,0x45a8,
  177. 0x3fb2,0x2755,0x5046,0x8d61,
  178. 0x3f7a,0x7c93,0x2883,0x71d4,
  179. 0x3f30,0x0ecf,0x118c,0x37ac,
  180. 0x3ed0,0xe8f4,0xfcf7,0x4b5c,
  181. 0x3e5e,0xf7f1,0x9edf,0xce99,
  182. 0x3dd7,0xdc0d,0xdc57,0xc710,
  183. 0x3d3c,0xe0fd,0x54c8,0xcd17,
  184. 0x3c88,0x677f,0x1c0f,0x7fe8,
  185. 0x3bb7,0x6b92,0xc6f8,0x96ab,
  186. 0x3ac0,0x6494,0x9f50,0xaa8e,
  187. };
  188. static short B4[48] = {
  189. /*0x3ff0,0x0000,0x0000,0x0000,*/
  190. 0x4006,0x91f2,0x05e7,0x2e77,
  191. 0x3ffc,0x7bc9,0x2570,0x37d6,
  192. 0x3fd8,0x2f54,0x9821,0x5c12,
  193. 0x3fa0,0x5a4a,0xa7af,0x4bce,
  194. 0x3f53,0x06a5,0x4a3a,0xfc75,
  195. 0x3ef3,0x94a9,0xa71a,0x7eb2,
  196. 0x3e81,0xb420,0xb4f6,0x00ce,
  197. 0x3dfb,0x182b,0x1c44,0xba10,
  198. 0x3d60,0x573c,0xe32c,0x40a9,
  199. 0x3cab,0x92f6,0xc77b,0x077b,
  200. 0x3bda,0x721c,0x6851,0xe1fe,
  201. 0x3ae2,0x81e1,0x5f6f,0xbc13,
  202. };
  203. #endif
  204. #ifdef ANSIPROT
  205. extern double spence ( double );
  206. extern double polevl ( double, void *, int );
  207. extern double p1evl ( double, void *, int );
  208. extern double zetac ( double );
  209. extern double pow ( double, double );
  210. extern double powi ( double, int );
  211. extern double log ( double );
  212. extern double fac ( int i );
  213. extern double fabs (double);
  214. double polylog (int, double);
  215. #else
  216. extern double spence(), polevl(), p1evl(), zetac();
  217. extern double pow(), powi(), log();
  218. extern double fac(); /* factorial */
  219. extern double fabs();
  220. double polylog();
  221. #endif
  222. extern double MACHEP;
  223. double
  224. polylog (n, x)
  225. int n;
  226. double x;
  227. {
  228. double h, k, p, s, t, u, xc, z;
  229. int i, j;
  230. /* This recurrence provides formulas for n < 2.
  231. d 1
  232. -- Li (x) = --- Li (x) .
  233. dx n x n-1
  234. */
  235. if (n == -1)
  236. {
  237. p = 1.0 - x;
  238. u = x / p;
  239. s = u * u + u;
  240. return s;
  241. }
  242. if (n == 0)
  243. {
  244. s = x / (1.0 - x);
  245. return s;
  246. }
  247. /* Not implemented for n < -1.
  248. Not defined for x > 1. Use cpolylog if you need that. */
  249. if (x > 1.0 || n < -1)
  250. {
  251. mtherr("polylog", DOMAIN);
  252. return 0.0;
  253. }
  254. if (n == 1)
  255. {
  256. s = -log (1.0 - x);
  257. return s;
  258. }
  259. /* Argument +1 */
  260. if (x == 1.0 && n > 1)
  261. {
  262. s = zetac ((double) n) + 1.0;
  263. return s;
  264. }
  265. /* Argument -1.
  266. 1-n
  267. Li (-z) = - (1 - 2 ) Li (z)
  268. n n
  269. */
  270. if (x == -1.0 && n > 1)
  271. {
  272. /* Li_n(1) = zeta(n) */
  273. s = zetac ((double) n) + 1.0;
  274. s = s * (powi (2.0, 1 - n) - 1.0);
  275. return s;
  276. }
  277. /* Inversion formula:
  278. * [n/2] n-2r
  279. * n 1 n - log (z)
  280. * Li (-z) + (-1) Li (-1/z) = - --- log (z) + 2 > ----------- Li (-1)
  281. * n n n! - (n - 2r)! 2r
  282. * r=1
  283. */
  284. if (x < -1.0 && n > 1)
  285. {
  286. double q, w;
  287. int r;
  288. w = log (-x);
  289. s = 0.0;
  290. for (r = 1; r <= n / 2; r++)
  291. {
  292. j = 2 * r;
  293. p = polylog (j, -1.0);
  294. j = n - j;
  295. if (j == 0)
  296. {
  297. s = s + p;
  298. break;
  299. }
  300. q = (double) j;
  301. q = pow (w, q) * p / fac (j);
  302. s = s + q;
  303. }
  304. s = 2.0 * s;
  305. q = polylog (n, 1.0 / x);
  306. if (n & 1)
  307. q = -q;
  308. s = s - q;
  309. s = s - pow (w, (double) n) / fac (n);
  310. return s;
  311. }
  312. if (n == 2)
  313. {
  314. if (x < 0.0 || x > 1.0)
  315. return (spence (1.0 - x));
  316. }
  317. /* The power series converges slowly when x is near 1. For n = 3, this
  318. identity helps:
  319. Li (-x/(1-x)) + Li (1-x) + Li (x)
  320. 3 3 3
  321. 2 2 3
  322. = Li (1) + (pi /6) log(1-x) - (1/2) log(x) log (1-x) + (1/6) log (1-x)
  323. 3
  324. */
  325. if (n == 3)
  326. {
  327. p = x * x * x;
  328. if (x > 0.8)
  329. {
  330. u = log(x);
  331. s = p / 6.0;
  332. xc = 1.0 - x;
  333. s = s - 0.5 * u * u * log(xc);
  334. s = s + PI * PI * u / 6.0;
  335. s = s - polylog (3, -xc/x);
  336. s = s - polylog (3, xc);
  337. s = s + zetac(3.0);
  338. s = s + 1.0;
  339. return s;
  340. }
  341. /* Power series */
  342. t = p / 27.0;
  343. t = t + .125 * x * x;
  344. t = t + x;
  345. s = 0.0;
  346. k = 4.0;
  347. do
  348. {
  349. p = p * x;
  350. h = p / (k * k * k);
  351. s = s + h;
  352. k += 1.0;
  353. }
  354. while (fabs(h/s) > 1.1e-16);
  355. return (s + t);
  356. }
  357. if (n == 4)
  358. {
  359. if (x >= 0.875)
  360. {
  361. u = 1.0 - x;
  362. s = polevl(u, A4, 12) / p1evl(u, B4, 12);
  363. s = s * u * u - 1.202056903159594285400 * u;
  364. s += 1.0823232337111381915160;
  365. return s;
  366. }
  367. goto pseries;
  368. }
  369. if (x < 0.75)
  370. goto pseries;
  371. /* This expansion in powers of log(x) is especially useful when
  372. x is near 1.
  373. See also the pari gp calculator.
  374. inf j
  375. - z(n-j) (log(x))
  376. polylog(n,x) = > -----------------
  377. - j!
  378. j=0
  379. where
  380. z(j) = Riemann zeta function (j), j != 1
  381. n-1
  382. -
  383. z(1) = -log(-log(x)) + > 1/k
  384. -
  385. k=1
  386. */
  387. z = log(x);
  388. h = -log(-z);
  389. for (i = 1; i < n; i++)
  390. h = h + 1.0/i;
  391. p = 1.0;
  392. s = zetac((double)n) + 1.0;
  393. for (j=1; j<=n+1; j++)
  394. {
  395. p = p * z / j;
  396. if (j == n-1)
  397. s = s + h * p;
  398. else
  399. s = s + (zetac((double)(n-j)) + 1.0) * p;
  400. }
  401. j = n + 3;
  402. z = z * z;
  403. for(;;)
  404. {
  405. p = p * z / ((j-1)*j);
  406. h = (zetac((double)(n-j)) + 1.0);
  407. h = h * p;
  408. s = s + h;
  409. if (fabs(h/s) < MACHEP)
  410. break;
  411. j += 2;
  412. }
  413. return s;
  414. pseries:
  415. p = x * x * x;
  416. k = 3.0;
  417. s = 0.0;
  418. do
  419. {
  420. p = p * x;
  421. k += 1.0;
  422. h = p / powi(k, n);
  423. s = s + h;
  424. }
  425. while (fabs(h/s) > MACHEP);
  426. s += x * x * x / powi(3.0,n);
  427. s += x * x / powi(2.0,n);
  428. s += x;
  429. return s;
  430. }