1 /** @file inifcns_nstdsums.cpp
3 * Implementation of some special functions that have a representation as nested sums.
5 * classical polylogarithm Li(n,x)
6 * multiple polylogarithm Li(lst(n_1,...,n_k),lst(x_1,...,x_k))
7 * nielsen's generalized polylogarithm S(n,p,x)
8 * harmonic polylogarithm H(lst(m_1,...,m_k),x)
9 * multiple zeta value mZeta(lst(m_1,...,m_k))
12 * - All formulae used can be looked up in the following publications:
13 * [Kol] Nielsen's Generalized Polylogarithms, K.S.Kolbig, SIAM J.Math.Anal. 17 (1986), pp. 1232-1258.
14 * [Cra] Fast Evaluation of Multiple Zeta Sums, R.E.Crandall, Math.Comp. 67 (1998), pp. 1163-1172.
15 * - Classical polylogarithms (Li) and nielsen's generalized polylogarithms (S) can be numerically
16 * evaluated in the whole complex plane.
17 * - The calculation of classical polylogarithms is speed up by using Euler-Maclaurin summation (EuMac).
18 * - The remaining functions can only be numerically evaluated with arguments lying in the unit sphere
20 * - The functions have no series expansion. To do it, you have to convert these functions
21 * into the appropriate objects from the nestedsums library, do the expansion and convert the
23 * - Numerical testing of this implementation has been performed by doing a comparison of results
24 * between this software and the commercial M.......... 4.1.
29 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation; either version 2 of the License, or
34 * (at your option) any later version.
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
41 * You should have received a copy of the GNU General Public License
42 * along with this program; if not, write to the Free Software
43 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
53 #include "operators.h"
54 #include "relational.h"
61 //////////////////////////////////////////////////////////////////////
63 // Classical polylogarithm Li
67 //////////////////////////////////////////////////////////////////////
71 // lookup table for Euler-Maclaurin optimization
73 std::vector<std::vector<cln::cl_N> > Xn;
78 // This function calculates the X_n. The X_n are needed for the Euler-Maclaurin summation (EMS) of
79 // classical polylogarithms.
80 // With EMS the polylogs can be calculated as follows:
81 // Li_p (x) = \sum_{n=0}^\infty X_{p-2}(n) u^{n+1}/(n+1)! with u = -log(1-x)
82 // X_0(n) = B_n (Bernoulli numbers)
83 // X_p(n) = \sum_{k=0}^n binomial(n,k) B_{n-k} / (k+1) * X_{p-1}(k)
84 // The calculation of Xn depends on X0 and X{n-1}.
85 // X_0 is special, it holds only the non-zero Bernoulli numbers with index 2 or greater.
86 // This results in a slightly more complicated algorithm for the X_n.
87 // The first index in Xn corresponds to the index of the polylog minus 2.
88 // The second index in Xn corresponds to the index from the EMS.
89 static void fill_Xn(int n)
91 // rule of thumb. needs to be improved. TODO
92 const int initsize = Digits * 3 / 2;
95 // calculate X_2 and higher (corresponding to Li_4 and higher)
96 std::vector<cln::cl_N> buf(initsize);
97 std::vector<cln::cl_N>::iterator it = buf.begin();
99 *it = -(cln::expt(cln::cl_I(2),n+1) - 1) / cln::expt(cln::cl_I(2),n+1); // i == 1
101 for (int i=2; i<=initsize; i++) {
103 result = 0; // k == 0
105 result = Xn[0][i/2-1]; // k == 0
107 for (int k=1; k<i-1; k++) {
108 if ( !(((i-k) & 1) && ((i-k) > 1)) ) {
109 result = result + cln::binomial(i,k) * Xn[0][(i-k)/2-1] * Xn[n-1][k-1] / (k+1);
112 result = result - cln::binomial(i,i-1) * Xn[n-1][i-2] / 2 / i; // k == i-1
113 result = result + Xn[n-1][i-1] / (i+1); // k == i
120 // special case to handle the X_0 correct
121 std::vector<cln::cl_N> buf(initsize);
122 std::vector<cln::cl_N>::iterator it = buf.begin();
124 *it = cln::cl_I(-3)/cln::cl_I(4); // i == 1
126 *it = cln::cl_I(17)/cln::cl_I(36); // i == 2
128 for (int i=3; i<=initsize; i++) {
130 result = -Xn[0][(i-3)/2]/2;
131 *it = (cln::binomial(i,1)/cln::cl_I(2) + cln::binomial(i,i-1)/cln::cl_I(i))*result;
134 result = Xn[0][i/2-1] + Xn[0][i/2-1]/(i+1);
135 for (int k=1; k<i/2; k++) {
136 result = result + cln::binomial(i,k*2) * Xn[0][k-1] * Xn[0][i/2-k-1] / (k*2+1);
145 std::vector<cln::cl_N> buf(initsize/2);
146 std::vector<cln::cl_N>::iterator it = buf.begin();
147 for (int i=1; i<=initsize/2; i++) {
148 *it = bernoulli(i*2).to_cl_N();
158 // calculates Li(2,x) without EuMac
159 static cln::cl_N Li2_do_sum(const cln::cl_N& x)
164 cln::cl_I den = 1; // n^2 = 1
169 den = den + i; // n^2 = 4, 9, 16, ...
171 res = res + num / den;
172 } while (res != resbuf);
177 // calculates Li(2,x) with EuMac
178 static cln::cl_N Li2_do_sum_EuMac(const cln::cl_N& x)
180 std::vector<cln::cl_N>::const_iterator it = Xn[0].begin();
181 cln::cl_N u = -cln::log(1-x);
182 cln::cl_N factor = u;
183 cln::cl_N res = u - u*u/4;
188 factor = factor * u*u / (2*i * (2*i+1));
189 res = res + (*it) * factor;
190 it++; // should we check it? or rely on initsize? ...
192 } while (res != resbuf);
197 // calculates Li(n,x), n>2 without EuMac
198 static cln::cl_N Lin_do_sum(int n, const cln::cl_N& x)
200 cln::cl_N factor = x;
207 res = res + factor / cln::expt(cln::cl_I(i),n);
209 } while (res != resbuf);
214 // calculates Li(n,x), n>2 with EuMac
215 static cln::cl_N Lin_do_sum_EuMac(int n, const cln::cl_N& x)
217 std::vector<cln::cl_N>::const_iterator it = Xn[n-2].begin();
218 cln::cl_N u = -cln::log(1-x);
219 cln::cl_N factor = u;
225 factor = factor * u / i;
226 res = res + (*it) * factor;
227 it++; // should we check it? or rely on initsize? ...
229 } while (res != resbuf);
234 // forward declaration needed by function Li_projection and C below
235 static numeric S_num(int n, int p, const numeric& x);
238 // helper function for classical polylog Li
239 static cln::cl_N Li_projection(int n, const cln::cl_N& x, const cln::float_format_t& prec)
241 // treat n=2 as special case
243 // check if precalculated X0 exists
248 if (cln::realpart(x) < 0.5) {
249 // choose the faster algorithm
250 // the switching point was empirically determined. the optimal point
251 // depends on hardware, Digits, ... so an approx value is okay.
252 // it solves also the problem with precision due to the u=-log(1-x) transformation
253 if (cln::abs(cln::realpart(x)) < 0.25) {
255 return Li2_do_sum(x);
257 return Li2_do_sum_EuMac(x);
260 // choose the faster algorithm
261 if (cln::abs(cln::realpart(x)) > 0.75) {
262 return -Li2_do_sum(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
264 return -Li2_do_sum_EuMac(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
268 // check if precalculated Xn exist
270 for (int i=xnsize; i<n-1; i++) {
275 if (cln::realpart(x) < 0.5) {
276 // choose the faster algorithm
277 // with n>=12 the "normal" summation always wins against EuMac
278 if ((cln::abs(cln::realpart(x)) < 0.3) || (n >= 12)) {
279 return Lin_do_sum(n, x);
281 return Lin_do_sum_EuMac(n, x);
284 cln::cl_N result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
285 for (int j=0; j<n-1; j++) {
286 result = result + (S_num(n-j-1, 1, 1).to_cl_N() - S_num(1, n-j-1, 1-x).to_cl_N())
287 * cln::expt(cln::log(x), j) / cln::factorial(j);
295 // helper function for classical polylog Li
296 static numeric Li_num(int n, const numeric& x)
300 return -cln::log(1-x.to_cl_N());
311 return -(1-cln::expt(cln::cl_I(2),1-n)) * cln::zeta(n);
314 // what is the desired float format?
315 // first guess: default format
316 cln::float_format_t prec = cln::default_float_format;
317 const cln::cl_N value = x.to_cl_N();
318 // second guess: the argument's format
319 if (!x.real().is_rational())
320 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
321 else if (!x.imag().is_rational())
322 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
325 if (cln::abs(value) > 1) {
326 cln::cl_N result = -cln::expt(cln::log(-value),n) / cln::factorial(n);
327 // check if argument is complex. if it is real, the new polylog has to be conjugated.
328 if (cln::zerop(cln::imagpart(value))) {
330 result = result + conjugate(Li_projection(n, cln::recip(value), prec));
333 result = result - conjugate(Li_projection(n, cln::recip(value), prec));
338 result = result + Li_projection(n, cln::recip(value), prec);
341 result = result - Li_projection(n, cln::recip(value), prec);
345 for (int j=0; j<n-1; j++) {
346 add = add + (1+cln::expt(cln::cl_I(-1),n-j)) * (1-cln::expt(cln::cl_I(2),1-n+j))
347 * Li_num(n-j,1).to_cl_N() * cln::expt(cln::log(-value),j) / cln::factorial(j);
349 result = result - add;
353 return Li_projection(n, value, prec);
358 //////////////////////////////////////////////////////////////////////
360 // Multiple polylogarithm Li
364 //////////////////////////////////////////////////////////////////////
367 static cln::cl_N multipleLi_do_sum(const std::vector<int>& s, const std::vector<cln::cl_N>& x)
369 const int j = s.size();
371 std::vector<cln::cl_N> t(j);
372 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
379 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
380 for (int k=j-2; k>=0; k--) {
381 t[k] = t[k] + t[k+1] * cln::expt(x[k], q+j-1-k) / cln::expt(cln::cl_I(q+j-1-k), s[k]);
383 } while (t[0] != t0buf);
389 //////////////////////////////////////////////////////////////////////
391 // Classical polylogarithm and multiple polylogarithm Li
395 //////////////////////////////////////////////////////////////////////
398 static ex Li_eval(const ex& x1, const ex& x2)
404 if (x2.info(info_flags::numeric) && (!x2.info(info_flags::crational)))
405 return Li_num(ex_to<numeric>(x1).to_int(), ex_to<numeric>(x2));
407 for (int i=0; i<x2.nops(); i++) {
408 if (!is_a<numeric>(x2.op(i))) {
409 return Li(x1,x2).hold();
412 return Li(x1,x2).evalf();
414 return Li(x1,x2).hold();
419 static ex Li_evalf(const ex& x1, const ex& x2)
421 // classical polylogs
422 if (is_a<numeric>(x1) && is_a<numeric>(x2)) {
423 return Li_num(ex_to<numeric>(x1).to_int(), ex_to<numeric>(x2));
426 else if (is_a<lst>(x1) && is_a<lst>(x2)) {
427 for (int i=0; i<x1.nops(); i++) {
428 if (!x1.op(i).info(info_flags::posint)) {
429 return Li(x1,x2).hold();
431 if (!is_a<numeric>(x2.op(i))) {
432 return Li(x1,x2).hold();
435 return Li(x1,x2).hold();
440 std::vector<cln::cl_N> x;
441 for (int i=ex_to<numeric>(x1.nops()).to_int()-1; i>=0; i--) {
442 m.push_back(ex_to<numeric>(x1.op(i)).to_int());
443 x.push_back(ex_to<numeric>(x2.op(i)).to_cl_N());
446 return numeric(multipleLi_do_sum(m, x));
449 return Li(x1,x2).hold();
453 static ex Li_series(const ex& x1, const ex& x2, const relational& rel, int order, unsigned options)
456 seq.push_back(expair(Li(x1,x2), 0));
457 return pseries(rel,seq);
461 static ex Li_deriv(const ex& x1, const ex& x2, unsigned deriv_param)
463 GINAC_ASSERT(deriv_param < 2);
464 if (deriv_param == 0) {
468 return Li(x1-1, x2) / x2;
475 REGISTER_FUNCTION(Li,
477 evalf_func(Li_evalf).
478 do_not_evalf_params().
479 series_func(Li_series).
480 derivative_func(Li_deriv));
483 //////////////////////////////////////////////////////////////////////
485 // Nielsen's generalized polylogarithm S
489 //////////////////////////////////////////////////////////////////////
493 // lookup table for special Euler-Zagier-Sums (used for S_n,p(x))
495 std::vector<std::vector<cln::cl_N> > Yn;
496 int ynsize = 0; // number of Yn[]
497 int ynlength = 100; // initial length of all Yn[i]
501 // This function calculates the Y_n. The Y_n are needed for the evaluation of S_{n,p}(x).
502 // The Y_n are basically Euler-Zagier sums with all m_i=1. They are subsums in the Z-sum
503 // representing S_{n,p}(x).
504 // The first index in Y_n corresponds to the parameter p minus one, i.e. the depth of the
506 // The second index in Y_n corresponds to the running index of the outermost sum in the full Z-sum
507 // representing S_{n,p}(x).
508 // The calculation of Y_n uses the values from Y_{n-1}.
509 static void fill_Yn(int n, const cln::float_format_t& prec)
511 const int initsize = ynlength;
512 //const int initsize = initsize_Yn;
513 cln::cl_N one = cln::cl_float(1, prec);
516 std::vector<cln::cl_N> buf(initsize);
517 std::vector<cln::cl_N>::iterator it = buf.begin();
518 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
519 *it = (*itprev) / cln::cl_N(n+1) * one;
522 // sums with an index smaller than the depth are zero and need not to be calculated.
523 // calculation starts with depth, which is n+2)
524 for (int i=n+2; i<=initsize+n; i++) {
525 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
531 std::vector<cln::cl_N> buf(initsize);
532 std::vector<cln::cl_N>::iterator it = buf.begin();
535 for (int i=2; i<=initsize; i++) {
536 *it = *(it-1) + 1 / cln::cl_N(i) * one;
545 // make Yn longer ...
546 static void make_Yn_longer(int newsize, const cln::float_format_t& prec)
549 cln::cl_N one = cln::cl_float(1, prec);
551 Yn[0].resize(newsize);
552 std::vector<cln::cl_N>::iterator it = Yn[0].begin();
554 for (int i=ynlength+1; i<=newsize; i++) {
555 *it = *(it-1) + 1 / cln::cl_N(i) * one;
559 for (int n=1; n<ynsize; n++) {
560 Yn[n].resize(newsize);
561 std::vector<cln::cl_N>::iterator it = Yn[n].begin();
562 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
565 for (int i=ynlength+n+1; i<=newsize+n; i++) {
566 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
576 // helper function for S(n,p,x)
578 static cln::cl_N C(int n, int p)
582 for (int k=0; k<p; k++) {
583 for (int j=0; j<=(n+k-1)/2; j++) {
587 result = result - 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1).to_cl_N() / cln::factorial(2*j);
590 result = result + 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1).to_cl_N() / cln::factorial(2*j);
597 result = result + cln::factorial(n+k-1)
598 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
599 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
602 result = result - cln::factorial(n+k-1)
603 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
604 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
609 result = result - cln::factorial(n+k-1) * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
610 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
613 result = result + cln::factorial(n+k-1)
614 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
615 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
623 if (((np)/2+n) & 1) {
624 result = -result - cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
627 result = -result + cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
635 // helper function for S(n,p,x)
636 // [Kol] remark to (9.1)
637 static cln::cl_N a_k(int k)
646 for (int m=2; m<=k; m++) {
647 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * a_k(k-m);
654 // helper function for S(n,p,x)
655 // [Kol] remark to (9.1)
656 static cln::cl_N b_k(int k)
665 for (int m=2; m<=k; m++) {
666 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * b_k(k-m);
673 // helper function for S(n,p,x)
674 static cln::cl_N S_do_sum(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
677 return Li_projection(n+1, x, prec);
680 // check if precalculated values are sufficient
682 for (int i=ynsize; i<p-1; i++) {
687 // should be done otherwise
688 cln::cl_N xf = x * cln::cl_float(1, prec);
692 cln::cl_N factor = cln::expt(xf, p);
696 if (i-p >= ynlength) {
698 make_Yn_longer(ynlength*2, prec);
700 res = res + factor / cln::expt(cln::cl_I(i),n+1) * Yn[p-2][i-p]; // should we check it? or rely on magic number? ...
701 //res = res + factor / cln::expt(cln::cl_I(i),n+1) * (*it); // should we check it? or rely on magic number? ...
702 factor = factor * xf;
704 } while (res != resbuf);
710 // helper function for S(n,p,x)
711 static cln::cl_N S_projection(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
714 if (cln::abs(cln::realpart(x)) > cln::cl_F("0.5")) {
716 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(x),n)
717 * cln::expt(cln::log(1-x),p) / cln::factorial(n) / cln::factorial(p);
719 for (int s=0; s<n; s++) {
721 for (int r=0; r<p; r++) {
722 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-x),r)
723 * S_do_sum(p-r,n-s,1-x,prec) / cln::factorial(r);
725 result = result + cln::expt(cln::log(x),s) * (S_num(n-s,p,1).to_cl_N() - res2) / cln::factorial(s);
731 return S_do_sum(n, p, x, prec);
735 // helper function for S(n,p,x)
736 static numeric S_num(int n, int p, const numeric& x)
740 // [Kol] (2.22) with (2.21)
741 return cln::zeta(p+1);
746 return cln::zeta(n+1);
751 for (int nu=0; nu<n; nu++) {
752 for (int rho=0; rho<=p; rho++) {
753 result = result + b_k(n-nu-1) * b_k(p-rho) * a_k(nu+rho+1)
754 * cln::factorial(nu+rho+1) / cln::factorial(rho) / cln::factorial(nu+1);
757 result = result * cln::expt(cln::cl_I(-1),n+p-1);
764 return -(1-cln::expt(cln::cl_I(2),-n)) * cln::zeta(n+1);
766 // throw std::runtime_error("don't know how to evaluate this function!");
769 // what is the desired float format?
770 // first guess: default format
771 cln::float_format_t prec = cln::default_float_format;
772 const cln::cl_N value = x.to_cl_N();
773 // second guess: the argument's format
774 if (!x.real().is_rational())
775 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
776 else if (!x.imag().is_rational())
777 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
781 if (cln::realpart(value) < -0.5) {
783 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(value),n)
784 * cln::expt(cln::log(1-value),p) / cln::factorial(n) / cln::factorial(p);
786 for (int s=0; s<n; s++) {
788 for (int r=0; r<p; r++) {
789 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-value),r)
790 * S_num(p-r,n-s,1-value).to_cl_N() / cln::factorial(r);
792 result = result + cln::expt(cln::log(value),s) * (S_num(n-s,p,1).to_cl_N() - res2) / cln::factorial(s);
799 if (cln::abs(value) > 1) {
803 for (int s=0; s<p; s++) {
804 for (int r=0; r<=s; r++) {
805 result = result + cln::expt(cln::cl_I(-1),s) * cln::expt(cln::log(-value),r) * cln::factorial(n+s-r-1)
806 / cln::factorial(r) / cln::factorial(s-r) / cln::factorial(n-1)
807 * S_num(n+s-r,p-s,cln::recip(value)).to_cl_N();
810 result = result * cln::expt(cln::cl_I(-1),n);
813 for (int r=0; r<n; r++) {
814 res2 = res2 + cln::expt(cln::log(-value),r) * C(n-r,p) / cln::factorial(r);
816 res2 = res2 + cln::expt(cln::log(-value),n+p) / cln::factorial(n+p);
818 result = result + cln::expt(cln::cl_I(-1),p) * res2;
823 return S_projection(n, p, value, prec);
828 //////////////////////////////////////////////////////////////////////
830 // Nielsen's generalized polylogarithm S
834 //////////////////////////////////////////////////////////////////////
837 static ex S_eval(const ex& x1, const ex& x2, const ex& x3)
842 if (x3.info(info_flags::numeric) && (!x3.info(info_flags::crational)) &&
843 x1.info(info_flags::posint) && x2.info(info_flags::posint)) {
844 return S_num(ex_to<numeric>(x1).to_int(), ex_to<numeric>(x2).to_int(), ex_to<numeric>(x3));
846 return S(x1,x2,x3).hold();
850 static ex S_evalf(const ex& x1, const ex& x2, const ex& x3)
852 if (is_a<numeric>(x1) && is_a<numeric>(x2) && is_a<numeric>(x3)) {
853 return S_num(ex_to<numeric>(x1).to_int(), ex_to<numeric>(x2).to_int(), ex_to<numeric>(x3));
855 return S(x1,x2,x3).hold();
859 static ex S_series(const ex& x1, const ex& x2, const ex& x3, const relational& rel, int order, unsigned options)
862 seq.push_back(expair(S(x1,x2,x3), 0));
863 return pseries(rel,seq);
867 static ex S_deriv(const ex& x1, const ex& x2, const ex& x3, unsigned deriv_param)
869 GINAC_ASSERT(deriv_param < 3);
870 if (deriv_param < 2) {
874 return S(x1-1, x2, x3) / x3;
876 return S(x1, x2-1, x3) / (1-x3);
884 do_not_evalf_params().
885 series_func(S_series).
886 derivative_func(S_deriv));
889 //////////////////////////////////////////////////////////////////////
891 // Harmonic polylogarithm H
895 //////////////////////////////////////////////////////////////////////
898 static cln::cl_N H_do_sum(const std::vector<int>& s, const cln::cl_N& x)
900 const int j = s.size();
902 std::vector<cln::cl_N> t(j);
904 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
905 cln::cl_N factor = cln::expt(x, j) * one;
911 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),s[j-1]);
912 for (int k=j-2; k>=1; k--) {
913 t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), s[k]);
915 t[0] = t[0] + t[1] * factor / cln::expt(cln::cl_I(q+j-1), s[0]);
917 } while (t[0] != t0buf);
923 //////////////////////////////////////////////////////////////////////
925 // Harmonic polylogarithm H
929 //////////////////////////////////////////////////////////////////////
932 static ex H_eval(const ex& x1, const ex& x2)
934 if (x2.info(info_flags::numeric) && (!x2.info(info_flags::crational))) {
935 return H(x1,x2).evalf();
937 return H(x1,x2).hold();
941 static ex H_evalf(const ex& x1, const ex& x2)
943 if (is_a<lst>(x1) && is_a<numeric>(x2)) {
944 for (int i=0; i<x1.nops(); i++) {
945 if (!x1.op(i).info(info_flags::posint)) {
946 return H(x1,x2).hold();
952 cln::cl_N x = ex_to<numeric>(x2).to_cl_N();
955 for (int i=x1.nops()-1; i>=0; i--) {
956 x1rev.append(x1.op(i));
958 return mZeta(x1rev).evalf();
960 const int j = x1.nops();
961 if (x2 > 1 || j < 2) {
962 return H(x1,x2).hold();
965 std::vector<int> r(j);
966 for (int i=0; i<j; i++) {
967 r[i] = ex_to<numeric>(x1.op(i)).to_int();
970 return numeric(H_do_sum(r,x));
973 return H(x1,x2).hold();
977 static ex H_series(const ex& x1, const ex& x2, const relational& rel, int order, unsigned options)
980 seq.push_back(expair(H(x1,x2), 0));
981 return pseries(rel,seq);
985 static ex H_deriv(const ex& x1, const ex& x2, unsigned deriv_param)
987 GINAC_ASSERT(deriv_param < 2);
988 if (deriv_param == 0) {
992 lst newparameter = ex_to<lst>(x1);
994 newparameter.remove_first();
995 return 1/(1-x2) * H(newparameter, x2);
998 return H(newparameter, x2).hold() / x2;
1004 return H(x1-1, x2).hold() / x2;
1010 REGISTER_FUNCTION(H,
1012 evalf_func(H_evalf).
1013 do_not_evalf_params().
1014 series_func(H_series).
1015 derivative_func(H_deriv));
1018 //////////////////////////////////////////////////////////////////////
1020 // Multiple zeta values mZeta
1024 //////////////////////////////////////////////////////////////////////
1028 const cln::cl_N lambda = cln::cl_N("319/320");
1031 std::vector<std::vector<cln::cl_N> > f_kj;
1032 std::vector<cln::cl_N> crB;
1033 std::vector<std::vector<cln::cl_N> > crG;
1034 std::vector<cln::cl_N> crX;
1038 static void halfcyclic_convolute(const std::vector<cln::cl_N>& a, const std::vector<cln::cl_N>& b, std::vector<cln::cl_N>& c)
1040 const int size = a.size();
1041 for (int n=0; n<size; n++) {
1043 for (int m=0; m<=n; m++) {
1044 c[n] = c[n] + a[m]*b[n-m];
1051 static void initcX(const std::vector<int>& s)
1053 const int k = s.size();
1059 for (int i=0; i<=L2; i++) {
1060 crB.push_back(bernoulli(i).to_cl_N() / cln::factorial(i));
1065 for (int m=0; m<k-1; m++) {
1066 std::vector<cln::cl_N> crGbuf;
1069 for (int i=0; i<=L2; i++) {
1070 crGbuf.push_back(cln::factorial(i + Sm - m - 2) / cln::factorial(i + Smp1 - m - 2));
1072 crG.push_back(crGbuf);
1077 for (int m=0; m<k-1; m++) {
1078 std::vector<cln::cl_N> Xbuf;
1079 for (int i=0; i<=L2; i++) {
1080 Xbuf.push_back(crX[i] * crG[m][i]);
1082 halfcyclic_convolute(Xbuf, crB, crX);
1088 static cln::cl_N crandall_Y_loop(const cln::cl_N& Sqk)
1090 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
1091 cln::cl_N factor = cln::expt(lambda, Sqk);
1092 cln::cl_N res = factor / Sqk * crX[0] * one;
1097 factor = factor * lambda;
1099 res = res + crX[N] * factor / (N+Sqk);
1100 } while ((res != resbuf) || cln::zerop(crX[N]));
1106 static void calc_f(int maxr)
1111 cln::cl_N t0, t1, t2, t3, t4;
1113 std::vector<std::vector<cln::cl_N> >::iterator it = f_kj.begin();
1114 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
1116 t0 = cln::exp(-lambda);
1118 for (k=1; k<=L1; k++) {
1121 for (j=1; j<=maxr; j++) {
1124 for (i=2; i<=j; i++) {
1128 (*it).push_back(t2 * t3 * cln::expt(cln::cl_I(k),-j) * one);
1136 static cln::cl_N crandall_Z(const std::vector<int>& s)
1138 const int j = s.size();
1147 t0 = t0 + f_kj[q+j-2][s[0]-1];
1148 } while (t0 != t0buf);
1150 return t0 / cln::factorial(s[0]-1);
1153 std::vector<cln::cl_N> t(j);
1160 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),s[j-1]);
1161 for (int k=j-2; k>=1; k--) {
1162 t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), s[k]);
1164 t[0] = t[0] + t[1] * f_kj[q+j-2][s[0]-1];
1165 } while (t[0] != t0buf);
1167 return t[0] / cln::factorial(s[0]-1);
1172 static cln::cl_N mZeta_do_sum_Crandall(const std::vector<int>& s)
1174 std::vector<int> r = s;
1175 const int j = r.size();
1177 // decide on maximal size of f_kj for crandall_Z
1181 L1 = Digits * 3 + j*2;
1184 // decide on maximal size of crX for crandall_Y
1187 } else if (Digits < 86) {
1189 } else if (Digits < 192) {
1191 } else if (Digits < 394) {
1193 } else if (Digits < 808) {
1203 for (int i=0; i<j; i++) {
1212 const cln::cl_N r0factorial = cln::factorial(r[0]-1);
1214 std::vector<int> rz;
1217 for (int k=r.size()-1; k>0; k--) {
1219 rz.insert(rz.begin(), r.back());
1220 skp1buf = rz.front();
1226 for (int q=0; q<skp1buf; q++) {
1228 cln::cl_N pp1 = crandall_Y_loop(Srun+q-k);
1229 cln::cl_N pp2 = crandall_Z(rz);
1234 res = res - pp1 * pp2 / cln::factorial(q);
1236 res = res + pp1 * pp2 / cln::factorial(q);
1239 rz.front() = skp1buf;
1241 rz.insert(rz.begin(), r.back());
1245 res = (res + crandall_Y_loop(S-j)) / r0factorial + crandall_Z(rz);
1251 static cln::cl_N mZeta_do_sum_simple(const std::vector<int>& r)
1253 const int j = r.size();
1255 // buffer for subsums
1256 std::vector<cln::cl_N> t(j);
1257 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
1264 t[j-1] = t[j-1] + one / cln::expt(cln::cl_I(q),r[j-1]);
1265 for (int k=j-2; k>=0; k--) {
1266 t[k] = t[k] + one * t[k+1] / cln::expt(cln::cl_I(q+j-1-k), r[k]);
1268 } while (t[0] != t0buf);
1274 //////////////////////////////////////////////////////////////////////
1276 // Multiple zeta values mZeta
1280 //////////////////////////////////////////////////////////////////////
1283 static ex mZeta_eval(const ex& x1)
1285 return mZeta(x1).hold();
1289 static ex mZeta_evalf(const ex& x1)
1291 if (is_a<lst>(x1)) {
1292 for (int i=0; i<x1.nops(); i++) {
1293 if (!x1.op(i).info(info_flags::posint))
1294 return mZeta(x1).hold();
1297 const int j = x1.nops();
1299 std::vector<int> r(j);
1300 for (int i=0; i<j; i++) {
1301 r[j-1-i] = ex_to<numeric>(x1.op(i)).to_int();
1304 // check for divergence
1306 return mZeta(x1).hold();
1309 // if only one argument, use cln::zeta
1311 return numeric(cln::zeta(r[0]));
1314 // decide on summation algorithm
1315 // this is still a bit clumsy
1316 int limit = (Digits>17) ? 10 : 6;
1317 if (r[0]<limit || (j>3 && r[1]<limit/2)) {
1318 return numeric(mZeta_do_sum_Crandall(r));
1320 return numeric(mZeta_do_sum_simple(r));
1322 } else if (x1.info(info_flags::posint) && (x1 != 1)) {
1323 return numeric(cln::zeta(ex_to<numeric>(x1).to_int()));
1326 return mZeta(x1).hold();
1330 static ex mZeta_series(const ex& x1, const relational& rel, int order, unsigned options)
1333 seq.push_back(expair(mZeta(x1), 0));
1334 return pseries(rel,seq);
1338 static ex mZeta_deriv(const ex& x, unsigned deriv_param)
1344 REGISTER_FUNCTION(mZeta,
1345 eval_func(mZeta_eval).
1346 evalf_func(mZeta_evalf).
1347 do_not_evalf_params().series_func(mZeta_series).
1348 derivative_func(mZeta_deriv));
1351 } // namespace GiNaC