1 /** @file inifcns_nstdsums.cpp
3 * Implementation of some special functions that have a representation as nested sums.
6 * classical polylogarithm Li(n,x)
7 * multiple polylogarithm Li(lst(m_1,...,m_k),lst(x_1,...,x_k))
8 * nielsen's generalized polylogarithm S(n,p,x)
9 * harmonic polylogarithm H(m,x) or H(lst(m_1,...,m_k),x)
10 * multiple zeta value zeta(m) or zeta(lst(m_1,...,m_k))
11 * alternating Euler sum zeta(m,s) or zeta(lst(m_1,...,m_k),lst(s_1,...,s_k))
15 * - All formulae used can be looked up in the following publications:
16 * [Kol] Nielsen's Generalized Polylogarithms, K.S.Kolbig, SIAM J.Math.Anal. 17 (1986), pp. 1232-1258.
17 * [Cra] Fast Evaluation of Multiple Zeta Sums, R.E.Crandall, Math.Comp. 67 (1998), pp. 1163-1172.
18 * [ReV] Harmonic Polylogarithms, E.Remiddi, J.A.M.Vermaseren, Int.J.Mod.Phys. A15 (2000), pp. 725-754
19 * [BBB] Special Values of Multiple Polylogarithms, J.Borwein, D.Bradley, D.Broadhurst, P.Lisonek, Trans.Amer.Math.Soc. 353/3 (2001), pp. 907-941
21 * - The order of parameters and arguments of Li and zeta is defined according to the nested sums
22 * representation. The parameters for H are understood as in [ReV]. They can be in expanded --- only
23 * 0, 1 and -1 --- or in compactified --- a string with zeros in front of 1 or -1 is written as a single
24 * number --- notation.
26 * - Except for the multiple polylogarithm all functions can be nummerically evaluated with arguments in
27 * the whole complex plane. Multiple polylogarithms evaluate only if for each argument x_i the product
28 * x_1 * x_2 * ... * x_i is smaller than one. The parameters for Li, zeta and S must be positive integers.
29 * If you want to have an alternating Euler sum, you have to give the signs of the parameters as a
30 * second argument s to zeta(m,s) containing 1 and -1.
32 * - The calculation of classical polylogarithms is speed up by using Bernoulli numbers and
33 * look-up tables. S uses look-up tables as well. The zeta function applies the algorithms in
34 * [Cra] and [BBB] for speed up.
36 * - The functions have no series expansion into nested sums. To do this, you have to convert these functions
37 * into the appropriate objects from the nestedsums library, do the expansion and convert the
40 * - Numerical testing of this implementation has been performed by doing a comparison of results
41 * between this software and the commercial M.......... 4.1. Multiple zeta values have been checked
42 * by means of evaluations into simple zeta values. Harmonic polylogarithms have been checked by
43 * comparison to S(n,p,x) for corresponding parameter combinations and by continuity checks
44 * around |x|=1 along with comparisons to corresponding zeta functions.
49 * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
51 * This program is free software; you can redistribute it and/or modify
52 * it under the terms of the GNU General Public License as published by
53 * the Free Software Foundation; either version 2 of the License, or
54 * (at your option) any later version.
56 * This program is distributed in the hope that it will be useful,
57 * but WITHOUT ANY WARRANTY; without even the implied warranty of
58 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
59 * GNU General Public License for more details.
61 * You should have received a copy of the GNU General Public License
62 * along with this program; if not, write to the Free Software
63 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
77 #include "operators.h"
80 #include "relational.h"
89 //////////////////////////////////////////////////////////////////////
91 // Classical polylogarithm Li(n,x)
95 //////////////////////////////////////////////////////////////////////
98 // anonymous namespace for helper functions
102 // lookup table for factors built from Bernoulli numbers
104 std::vector<std::vector<cln::cl_N> > Xn;
108 // This function calculates the X_n. The X_n are needed for speed up of classical polylogarithms.
109 // With these numbers the polylogs can be calculated as follows:
110 // Li_p (x) = \sum_{n=0}^\infty X_{p-2}(n) u^{n+1}/(n+1)! with u = -log(1-x)
111 // X_0(n) = B_n (Bernoulli numbers)
112 // X_p(n) = \sum_{k=0}^n binomial(n,k) B_{n-k} / (k+1) * X_{p-1}(k)
113 // The calculation of Xn depends on X0 and X{n-1}.
114 // X_0 is special, it holds only the non-zero Bernoulli numbers with index 2 or greater.
115 // This results in a slightly more complicated algorithm for the X_n.
116 // The first index in Xn corresponds to the index of the polylog minus 2.
117 // The second index in Xn corresponds to the index from the actual sum.
120 // rule of thumb. needs to be improved. TODO
121 const int initsize = Digits * 3 / 2;
124 // calculate X_2 and higher (corresponding to Li_4 and higher)
125 std::vector<cln::cl_N> buf(initsize);
126 std::vector<cln::cl_N>::iterator it = buf.begin();
128 *it = -(cln::expt(cln::cl_I(2),n+1) - 1) / cln::expt(cln::cl_I(2),n+1); // i == 1
130 for (int i=2; i<=initsize; i++) {
132 result = 0; // k == 0
134 result = Xn[0][i/2-1]; // k == 0
136 for (int k=1; k<i-1; k++) {
137 if ( !(((i-k) & 1) && ((i-k) > 1)) ) {
138 result = result + cln::binomial(i,k) * Xn[0][(i-k)/2-1] * Xn[n-1][k-1] / (k+1);
141 result = result - cln::binomial(i,i-1) * Xn[n-1][i-2] / 2 / i; // k == i-1
142 result = result + Xn[n-1][i-1] / (i+1); // k == i
149 // special case to handle the X_0 correct
150 std::vector<cln::cl_N> buf(initsize);
151 std::vector<cln::cl_N>::iterator it = buf.begin();
153 *it = cln::cl_I(-3)/cln::cl_I(4); // i == 1
155 *it = cln::cl_I(17)/cln::cl_I(36); // i == 2
157 for (int i=3; i<=initsize; i++) {
159 result = -Xn[0][(i-3)/2]/2;
160 *it = (cln::binomial(i,1)/cln::cl_I(2) + cln::binomial(i,i-1)/cln::cl_I(i))*result;
163 result = Xn[0][i/2-1] + Xn[0][i/2-1]/(i+1);
164 for (int k=1; k<i/2; k++) {
165 result = result + cln::binomial(i,k*2) * Xn[0][k-1] * Xn[0][i/2-k-1] / (k*2+1);
174 std::vector<cln::cl_N> buf(initsize/2);
175 std::vector<cln::cl_N>::iterator it = buf.begin();
176 for (int i=1; i<=initsize/2; i++) {
177 *it = bernoulli(i*2).to_cl_N();
187 // calculates Li(2,x) without Xn
188 cln::cl_N Li2_do_sum(const cln::cl_N& x)
192 cln::cl_N num = x * cln::cl_float(1, cln::float_format(Digits));
193 cln::cl_I den = 1; // n^2 = 1
198 den = den + i; // n^2 = 4, 9, 16, ...
200 res = res + num / den;
201 } while (res != resbuf);
206 // calculates Li(2,x) with Xn
207 cln::cl_N Li2_do_sum_Xn(const cln::cl_N& x)
209 std::vector<cln::cl_N>::const_iterator it = Xn[0].begin();
210 cln::cl_N u = -cln::log(1-x);
211 cln::cl_N factor = u * cln::cl_float(1, cln::float_format(Digits));
212 cln::cl_N res = u - u*u/4;
217 factor = factor * u*u / (2*i * (2*i+1));
218 res = res + (*it) * factor;
219 it++; // should we check it? or rely on initsize? ...
221 } while (res != resbuf);
226 // calculates Li(n,x), n>2 without Xn
227 cln::cl_N Lin_do_sum(int n, const cln::cl_N& x)
229 cln::cl_N factor = x * cln::cl_float(1, cln::float_format(Digits));
236 res = res + factor / cln::expt(cln::cl_I(i),n);
238 } while (res != resbuf);
243 // calculates Li(n,x), n>2 with Xn
244 cln::cl_N Lin_do_sum_Xn(int n, const cln::cl_N& x)
246 std::vector<cln::cl_N>::const_iterator it = Xn[n-2].begin();
247 cln::cl_N u = -cln::log(1-x);
248 cln::cl_N factor = u * cln::cl_float(1, cln::float_format(Digits));
254 factor = factor * u / i;
255 res = res + (*it) * factor;
256 it++; // should we check it? or rely on initsize? ...
258 } while (res != resbuf);
263 // forward declaration needed by function Li_projection and C below
264 numeric S_num(int n, int p, const numeric& x);
267 // helper function for classical polylog Li
268 cln::cl_N Li_projection(int n, const cln::cl_N& x, const cln::float_format_t& prec)
270 // treat n=2 as special case
272 // check if precalculated X0 exists
277 if (cln::realpart(x) < 0.5) {
278 // choose the faster algorithm
279 // the switching point was empirically determined. the optimal point
280 // depends on hardware, Digits, ... so an approx value is okay.
281 // it solves also the problem with precision due to the u=-log(1-x) transformation
282 if (cln::abs(cln::realpart(x)) < 0.25) {
284 return Li2_do_sum(x);
286 return Li2_do_sum_Xn(x);
289 // choose the faster algorithm
290 if (cln::abs(cln::realpart(x)) > 0.75) {
291 return -Li2_do_sum(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
293 return -Li2_do_sum_Xn(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
297 // check if precalculated Xn exist
299 for (int i=xnsize; i<n-1; i++) {
304 if (cln::realpart(x) < 0.5) {
305 // choose the faster algorithm
306 // with n>=12 the "normal" summation always wins against the method with Xn
307 if ((cln::abs(cln::realpart(x)) < 0.3) || (n >= 12)) {
308 return Lin_do_sum(n, x);
310 return Lin_do_sum_Xn(n, x);
313 cln::cl_N result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
314 for (int j=0; j<n-1; j++) {
315 result = result + (S_num(n-j-1, 1, 1).to_cl_N() - S_num(1, n-j-1, 1-x).to_cl_N())
316 * cln::expt(cln::log(x), j) / cln::factorial(j);
324 // helper function for classical polylog Li
325 numeric Li_num(int n, const numeric& x)
329 return -cln::log(1-x.to_cl_N());
340 return -(1-cln::expt(cln::cl_I(2),1-n)) * cln::zeta(n);
343 // what is the desired float format?
344 // first guess: default format
345 cln::float_format_t prec = cln::default_float_format;
346 const cln::cl_N value = x.to_cl_N();
347 // second guess: the argument's format
348 if (!x.real().is_rational())
349 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
350 else if (!x.imag().is_rational())
351 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
354 if (cln::abs(value) > 1) {
355 cln::cl_N result = -cln::expt(cln::log(-value),n) / cln::factorial(n);
356 // check if argument is complex. if it is real, the new polylog has to be conjugated.
357 if (cln::zerop(cln::imagpart(value))) {
359 result = result + conjugate(Li_projection(n, cln::recip(value), prec));
362 result = result - conjugate(Li_projection(n, cln::recip(value), prec));
367 result = result + Li_projection(n, cln::recip(value), prec);
370 result = result - Li_projection(n, cln::recip(value), prec);
374 for (int j=0; j<n-1; j++) {
375 add = add + (1+cln::expt(cln::cl_I(-1),n-j)) * (1-cln::expt(cln::cl_I(2),1-n+j))
376 * Li_num(n-j,1).to_cl_N() * cln::expt(cln::log(-value),j) / cln::factorial(j);
378 result = result - add;
382 return Li_projection(n, value, prec);
387 } // end of anonymous namespace
390 //////////////////////////////////////////////////////////////////////
392 // Multiple polylogarithm Li(n,x)
396 //////////////////////////////////////////////////////////////////////
399 // anonymous namespace for helper function
403 cln::cl_N multipleLi_do_sum(const std::vector<int>& s, const std::vector<cln::cl_N>& x)
405 const int j = s.size();
407 std::vector<cln::cl_N> t(j);
408 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
416 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
417 for (int k=j-2; k>=0; k--) {
418 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]);
420 // ... and do it again (to avoid premature drop out due to special arguments)
422 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
423 for (int k=j-2; k>=0; k--) {
424 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]);
426 } while (t[0] != t0buf);
431 // forward declaration for Li_eval()
432 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf);
435 } // end of anonymous namespace
438 //////////////////////////////////////////////////////////////////////
440 // Classical polylogarithm and multiple polylogarithm Li(n,x)
444 //////////////////////////////////////////////////////////////////////
447 static ex Li_evalf(const ex& x1, const ex& x2)
449 // classical polylogs
450 if (is_a<numeric>(x1) && is_a<numeric>(x2)) {
451 return Li_num(ex_to<numeric>(x1).to_int(), ex_to<numeric>(x2));
454 else if (is_a<lst>(x1) && is_a<lst>(x2)) {
456 for (int i=0; i<x1.nops(); i++) {
457 if (!x1.op(i).info(info_flags::posint)) {
458 return Li(x1, x2).hold();
460 if (!is_a<numeric>(x2.op(i))) {
461 return Li(x1, x2).hold();
464 if (abs(conv) >= 1) {
465 return Li(x1, x2).hold();
470 std::vector<cln::cl_N> x;
471 for (int i=0; i<ex_to<numeric>(x1.nops()).to_int(); i++) {
472 m.push_back(ex_to<numeric>(x1.op(i)).to_int());
473 x.push_back(ex_to<numeric>(x2.op(i)).to_cl_N());
476 return numeric(multipleLi_do_sum(m, x));
479 return Li(x1,x2).hold();
483 static ex Li_eval(const ex& m_, const ex& x_)
505 return (pow(2,1-m)-1) * zeta(m);
510 if (m.info(info_flags::posint) && x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
511 return Li_num(ex_to<numeric>(m).to_int(), ex_to<numeric>(x));
517 bool doevalf = false;
518 bool doevalfveto = true;
519 const lst& m = ex_to<lst>(m_);
520 const lst& x = ex_to<lst>(x_);
521 lst::const_iterator itm = m.begin();
522 lst::const_iterator itx = x.begin();
523 for (; itm != m.end(); itm++, itx++) {
524 if (!(*itm).info(info_flags::posint)) {
525 return Li(m_, x_).hold();
527 if ((*itx != _ex1) && (*itx != _ex_1)) {
528 if (itx != x.begin()) {
536 if (!(*itx).info(info_flags::numeric)) {
539 if (!(*itx).info(info_flags::crational)) {
551 lst newm = convert_parameter_Li_to_H(m, x, pf);
552 return pf * H(newm, x[0]);
554 if (doevalfveto && doevalf) {
555 return Li(m_, x_).evalf();
558 return Li(m_, x_).hold();
562 static ex Li_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
565 seq.push_back(expair(Li(m, x), 0));
566 return pseries(rel, seq);
570 static ex Li_deriv(const ex& m_, const ex& x_, unsigned deriv_param)
572 GINAC_ASSERT(deriv_param < 2);
573 if (deriv_param == 0) {
577 throw std::runtime_error("don't know how to derivate multiple polylogarithm!");
592 return Li(m-1, x) / x;
599 static void Li_print_latex(const ex& m_, const ex& x_, const print_context& c)
613 c.s << "\\mbox{Li}_{";
614 lst::const_iterator itm = m.begin();
617 for (; itm != m.end(); itm++) {
622 lst::const_iterator itx = x.begin();
625 for (; itx != x.end(); itx++) {
633 REGISTER_FUNCTION(Li,
634 evalf_func(Li_evalf).
636 series_func(Li_series).
637 derivative_func(Li_deriv).
638 print_func<print_latex>(Li_print_latex).
639 do_not_evalf_params());
642 //////////////////////////////////////////////////////////////////////
644 // Nielsen's generalized polylogarithm S(n,p,x)
648 //////////////////////////////////////////////////////////////////////
651 // anonymous namespace for helper functions
655 // lookup table for special Euler-Zagier-Sums (used for S_n,p(x))
657 std::vector<std::vector<cln::cl_N> > Yn;
658 int ynsize = 0; // number of Yn[]
659 int ynlength = 100; // initial length of all Yn[i]
662 // This function calculates the Y_n. The Y_n are needed for the evaluation of S_{n,p}(x).
663 // The Y_n are basically Euler-Zagier sums with all m_i=1. They are subsums in the Z-sum
664 // representing S_{n,p}(x).
665 // The first index in Y_n corresponds to the parameter p minus one, i.e. the depth of the
667 // The second index in Y_n corresponds to the running index of the outermost sum in the full Z-sum
668 // representing S_{n,p}(x).
669 // The calculation of Y_n uses the values from Y_{n-1}.
670 void fill_Yn(int n, const cln::float_format_t& prec)
672 const int initsize = ynlength;
673 //const int initsize = initsize_Yn;
674 cln::cl_N one = cln::cl_float(1, prec);
677 std::vector<cln::cl_N> buf(initsize);
678 std::vector<cln::cl_N>::iterator it = buf.begin();
679 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
680 *it = (*itprev) / cln::cl_N(n+1) * one;
683 // sums with an index smaller than the depth are zero and need not to be calculated.
684 // calculation starts with depth, which is n+2)
685 for (int i=n+2; i<=initsize+n; i++) {
686 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
692 std::vector<cln::cl_N> buf(initsize);
693 std::vector<cln::cl_N>::iterator it = buf.begin();
696 for (int i=2; i<=initsize; i++) {
697 *it = *(it-1) + 1 / cln::cl_N(i) * one;
706 // make Yn longer ...
707 void make_Yn_longer(int newsize, const cln::float_format_t& prec)
710 cln::cl_N one = cln::cl_float(1, prec);
712 Yn[0].resize(newsize);
713 std::vector<cln::cl_N>::iterator it = Yn[0].begin();
715 for (int i=ynlength+1; i<=newsize; i++) {
716 *it = *(it-1) + 1 / cln::cl_N(i) * one;
720 for (int n=1; n<ynsize; n++) {
721 Yn[n].resize(newsize);
722 std::vector<cln::cl_N>::iterator it = Yn[n].begin();
723 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
726 for (int i=ynlength+n+1; i<=newsize+n; i++) {
727 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
737 // helper function for S(n,p,x)
739 cln::cl_N C(int n, int p)
743 for (int k=0; k<p; k++) {
744 for (int j=0; j<=(n+k-1)/2; j++) {
748 result = result - 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1).to_cl_N() / cln::factorial(2*j);
751 result = result + 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1).to_cl_N() / cln::factorial(2*j);
758 result = result + cln::factorial(n+k-1)
759 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
760 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
763 result = result - cln::factorial(n+k-1)
764 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
765 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
770 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()
771 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
774 result = result + cln::factorial(n+k-1)
775 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
776 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
784 if (((np)/2+n) & 1) {
785 result = -result - cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
788 result = -result + cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
796 // helper function for S(n,p,x)
797 // [Kol] remark to (9.1)
807 for (int m=2; m<=k; m++) {
808 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * a_k(k-m);
815 // helper function for S(n,p,x)
816 // [Kol] remark to (9.1)
826 for (int m=2; m<=k; m++) {
827 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * b_k(k-m);
834 // helper function for S(n,p,x)
835 cln::cl_N S_do_sum(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
838 return Li_projection(n+1, x, prec);
841 // check if precalculated values are sufficient
843 for (int i=ynsize; i<p-1; i++) {
848 // should be done otherwise
849 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
850 cln::cl_N xf = x * one;
851 //cln::cl_N xf = x * cln::cl_float(1, prec);
855 cln::cl_N factor = cln::expt(xf, p);
859 if (i-p >= ynlength) {
861 make_Yn_longer(ynlength*2, prec);
863 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? ...
864 //res = res + factor / cln::expt(cln::cl_I(i),n+1) * (*it); // should we check it? or rely on magic number? ...
865 factor = factor * xf;
867 } while (res != resbuf);
873 // helper function for S(n,p,x)
874 cln::cl_N S_projection(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
877 if (cln::abs(cln::realpart(x)) > cln::cl_F("0.5")) {
879 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(x),n)
880 * cln::expt(cln::log(1-x),p) / cln::factorial(n) / cln::factorial(p);
882 for (int s=0; s<n; s++) {
884 for (int r=0; r<p; r++) {
885 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-x),r)
886 * S_do_sum(p-r,n-s,1-x,prec) / cln::factorial(r);
888 result = result + cln::expt(cln::log(x),s) * (S_num(n-s,p,1).to_cl_N() - res2) / cln::factorial(s);
894 return S_do_sum(n, p, x, prec);
898 // helper function for S(n,p,x)
899 numeric S_num(int n, int p, const numeric& x)
903 // [Kol] (2.22) with (2.21)
904 return cln::zeta(p+1);
909 return cln::zeta(n+1);
914 for (int nu=0; nu<n; nu++) {
915 for (int rho=0; rho<=p; rho++) {
916 result = result + b_k(n-nu-1) * b_k(p-rho) * a_k(nu+rho+1)
917 * cln::factorial(nu+rho+1) / cln::factorial(rho) / cln::factorial(nu+1);
920 result = result * cln::expt(cln::cl_I(-1),n+p-1);
927 return -(1-cln::expt(cln::cl_I(2),-n)) * cln::zeta(n+1);
929 // throw std::runtime_error("don't know how to evaluate this function!");
932 // what is the desired float format?
933 // first guess: default format
934 cln::float_format_t prec = cln::default_float_format;
935 const cln::cl_N value = x.to_cl_N();
936 // second guess: the argument's format
937 if (!x.real().is_rational())
938 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
939 else if (!x.imag().is_rational())
940 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
943 if ((cln::realpart(value) < -0.5) || (n == 0)) {
945 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(value),n)
946 * cln::expt(cln::log(1-value),p) / cln::factorial(n) / cln::factorial(p);
948 for (int s=0; s<n; s++) {
950 for (int r=0; r<p; r++) {
951 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-value),r)
952 * S_num(p-r,n-s,1-value).to_cl_N() / cln::factorial(r);
954 result = result + cln::expt(cln::log(value),s) * (S_num(n-s,p,1).to_cl_N() - res2) / cln::factorial(s);
961 if (cln::abs(value) > 1) {
965 for (int s=0; s<p; s++) {
966 for (int r=0; r<=s; r++) {
967 result = result + cln::expt(cln::cl_I(-1),s) * cln::expt(cln::log(-value),r) * cln::factorial(n+s-r-1)
968 / cln::factorial(r) / cln::factorial(s-r) / cln::factorial(n-1)
969 * S_num(n+s-r,p-s,cln::recip(value)).to_cl_N();
972 result = result * cln::expt(cln::cl_I(-1),n);
975 for (int r=0; r<n; r++) {
976 res2 = res2 + cln::expt(cln::log(-value),r) * C(n-r,p) / cln::factorial(r);
978 res2 = res2 + cln::expt(cln::log(-value),n+p) / cln::factorial(n+p);
980 result = result + cln::expt(cln::cl_I(-1),p) * res2;
985 return S_projection(n, p, value, prec);
990 } // end of anonymous namespace
993 //////////////////////////////////////////////////////////////////////
995 // Nielsen's generalized polylogarithm S(n,p,x)
999 //////////////////////////////////////////////////////////////////////
1002 static ex S_evalf(const ex& n, const ex& p, const ex& x)
1004 if (n.info(info_flags::posint) && p.info(info_flags::posint) && is_a<numeric>(x)) {
1005 return S_num(ex_to<numeric>(n).to_int(), ex_to<numeric>(p).to_int(), ex_to<numeric>(x));
1007 return S(n, p, x).hold();
1011 static ex S_eval(const ex& n, const ex& p, const ex& x)
1013 if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
1019 for (int i=ex_to<numeric>(p).to_int()-1; i>0; i--) {
1027 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
1028 return S_num(ex_to<numeric>(n).to_int(), ex_to<numeric>(p).to_int(), ex_to<numeric>(x));
1033 return pow(-log(1-x), p) / factorial(p);
1035 return S(n, p, x).hold();
1039 static ex S_series(const ex& n, const ex& p, const ex& x, const relational& rel, int order, unsigned options)
1042 seq.push_back(expair(S(n, p, x), 0));
1043 return pseries(rel, seq);
1047 static ex S_deriv(const ex& n, const ex& p, const ex& x, unsigned deriv_param)
1049 GINAC_ASSERT(deriv_param < 3);
1050 if (deriv_param < 2) {
1054 return S(n-1, p, x) / x;
1056 return S(n, p-1, x) / (1-x);
1061 static void S_print_latex(const ex& n, const ex& p, const ex& x, const print_context& c)
1063 c.s << "\\mbox{S}_{";
1073 REGISTER_FUNCTION(S,
1074 evalf_func(S_evalf).
1076 series_func(S_series).
1077 derivative_func(S_deriv).
1078 print_func<print_latex>(S_print_latex).
1079 do_not_evalf_params());
1082 //////////////////////////////////////////////////////////////////////
1084 // Harmonic polylogarithm H(m,x)
1088 //////////////////////////////////////////////////////////////////////
1091 // anonymous namespace for helper functions
1095 // regulates the pole (used by 1/x-transformation)
1096 symbol H_polesign("IMSIGN");
1099 // convert parameters from H to Li representation
1100 // parameters are expected to be in expanded form, i.e. only 0, 1 and -1
1101 // returns true if some parameters are negative
1102 bool convert_parameter_H_to_Li(const lst& l, lst& m, lst& s, ex& pf)
1104 // expand parameter list
1106 for (lst::const_iterator it = l.begin(); it != l.end(); it++) {
1108 for (ex count=*it-1; count > 0; count--) {
1112 } else if (*it < -1) {
1113 for (ex count=*it+1; count < 0; count++) {
1124 bool has_negative_parameters = false;
1126 for (lst::const_iterator it = mexp.begin(); it != mexp.end(); it++) {
1132 m.append((*it+acc-1) * signum);
1134 m.append((*it-acc+1) * signum);
1140 has_negative_parameters = true;
1143 if (has_negative_parameters) {
1144 for (int i=0; i<m.nops(); i++) {
1146 m.let_op(i) = -m.op(i);
1154 return has_negative_parameters;
1158 // recursivly transforms H to corresponding multiple polylogarithms
1159 struct map_trafo_H_convert_to_Li : public map_function
1161 ex operator()(const ex& e)
1163 if (is_a<add>(e) || is_a<mul>(e)) {
1164 return e.map(*this);
1166 if (is_a<function>(e)) {
1167 std::string name = ex_to<function>(e).get_name();
1170 if (is_a<lst>(e.op(0))) {
1171 parameter = ex_to<lst>(e.op(0));
1173 parameter = lst(e.op(0));
1180 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
1181 s.let_op(0) = s.op(0) * arg;
1182 return pf * Li(m, s).hold();
1184 for (int i=0; i<m.nops(); i++) {
1187 s.let_op(0) = s.op(0) * arg;
1188 return Li(m, s).hold();
1197 // recursivly transforms H to corresponding zetas
1198 struct map_trafo_H_convert_to_zeta : public map_function
1200 ex operator()(const ex& e)
1202 if (is_a<add>(e) || is_a<mul>(e)) {
1203 return e.map(*this);
1205 if (is_a<function>(e)) {
1206 std::string name = ex_to<function>(e).get_name();
1209 if (is_a<lst>(e.op(0))) {
1210 parameter = ex_to<lst>(e.op(0));
1212 parameter = lst(e.op(0));
1218 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
1219 return pf * zeta(m, s);
1230 // remove trailing zeros from H-parameters
1231 struct map_trafo_H_reduce_trailing_zeros : public map_function
1233 ex operator()(const ex& e)
1235 if (is_a<add>(e) || is_a<mul>(e)) {
1236 return e.map(*this);
1238 if (is_a<function>(e)) {
1239 std::string name = ex_to<function>(e).get_name();
1242 if (is_a<lst>(e.op(0))) {
1243 parameter = ex_to<lst>(e.op(0));
1245 parameter = lst(e.op(0));
1248 if (parameter.op(parameter.nops()-1) == 0) {
1251 if (parameter.nops() == 1) {
1256 lst::const_iterator it = parameter.begin();
1257 while ((it != parameter.end()) && (*it == 0)) {
1260 if (it == parameter.end()) {
1261 return pow(log(arg),parameter.nops()) / factorial(parameter.nops());
1265 parameter.remove_last();
1266 int lastentry = parameter.nops();
1267 while ((lastentry > 0) && (parameter[lastentry-1] == 0)) {
1272 ex result = log(arg) * H(parameter,arg).hold();
1274 for (ex i=0; i<lastentry; i++) {
1275 if (parameter[i] > 0) {
1277 result -= (acc + parameter[i]-1) * H(parameter, arg).hold();
1280 } else if (parameter[i] < 0) {
1282 result -= (acc + abs(parameter[i]+1)) * H(parameter, arg).hold();
1290 if (lastentry < parameter.nops()) {
1291 result = result / (parameter.nops()-lastentry+1);
1292 return result.map(*this);
1304 // returns an expression with zeta functions corresponding to the parameter list for H
1305 ex convert_H_to_zeta(const lst& m)
1307 symbol xtemp("xtemp");
1308 map_trafo_H_reduce_trailing_zeros filter;
1309 map_trafo_H_convert_to_zeta filter2;
1310 return filter2(filter(H(m, xtemp).hold())).subs(xtemp == 1);
1314 // convert signs form Li to H representation
1315 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf)
1318 lst::const_iterator itm = m.begin();
1319 lst::const_iterator itx = ++x.begin();
1324 while (itx != x.end()) {
1327 res.append((*itm) * signum);
1335 // multiplies an one-dimensional H with another H
1337 ex trafo_H_mult(const ex& h1, const ex& h2)
1342 ex h1nops = h1.op(0).nops();
1343 ex h2nops = h2.op(0).nops();
1345 hshort = h2.op(0).op(0);
1346 hlong = ex_to<lst>(h1.op(0));
1348 hshort = h1.op(0).op(0);
1350 hlong = ex_to<lst>(h2.op(0));
1352 hlong = h2.op(0).op(0);
1355 for (int i=0; i<=hlong.nops(); i++) {
1359 newparameter.append(hlong[j]);
1361 newparameter.append(hshort);
1362 for (; j<hlong.nops(); j++) {
1363 newparameter.append(hlong[j]);
1365 res += H(newparameter, h1.op(1)).hold();
1371 // applies trafo_H_mult recursively on expressions
1372 struct map_trafo_H_mult : public map_function
1374 ex operator()(const ex& e)
1377 return e.map(*this);
1385 for (int pos=0; pos<e.nops(); pos++) {
1386 if (is_a<power>(e.op(pos)) && is_a<function>(e.op(pos).op(0))) {
1387 std::string name = ex_to<function>(e.op(pos).op(0)).get_name();
1389 for (ex i=0; i<e.op(pos).op(1); i++) {
1390 Hlst.append(e.op(pos).op(0));
1394 } else if (is_a<function>(e.op(pos))) {
1395 std::string name = ex_to<function>(e.op(pos)).get_name();
1397 if (e.op(pos).op(0).nops() > 1) {
1400 Hlst.append(e.op(pos));
1405 result *= e.op(pos);
1408 if (Hlst.nops() > 0) {
1409 firstH = Hlst[Hlst.nops()-1];
1416 if (Hlst.nops() > 0) {
1417 ex buffer = trafo_H_mult(firstH, Hlst.op(0));
1419 for (int i=1; i<Hlst.nops(); i++) {
1420 result *= Hlst.op(i);
1422 result = result.expand();
1423 map_trafo_H_mult recursion;
1424 return recursion(result);
1435 // do integration [ReV] (55)
1436 // put parameter 0 in front of existing parameters
1437 ex trafo_H_1tx_prepend_zero(const ex& e, const ex& arg)
1441 if (is_a<function>(e)) {
1442 name = ex_to<function>(e).get_name();
1447 for (int i=0; i<e.nops(); i++) {
1448 if (is_a<function>(e.op(i))) {
1449 std::string name = ex_to<function>(e.op(i)).get_name();
1457 lst newparameter = ex_to<lst>(h.op(0));
1458 newparameter.prepend(0);
1459 ex addzeta = convert_H_to_zeta(newparameter);
1460 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
1462 return e * (-H(lst(0),1/arg).hold());
1467 // do integration [ReV] (55)
1468 // put parameter -1 in front of existing parameters
1469 ex trafo_H_1tx_prepend_minusone(const ex& e, const ex& arg)
1473 if (is_a<function>(e)) {
1474 name = ex_to<function>(e).get_name();
1479 for (int i=0; i<e.nops(); i++) {
1480 if (is_a<function>(e.op(i))) {
1481 std::string name = ex_to<function>(e.op(i)).get_name();
1489 lst newparameter = ex_to<lst>(h.op(0));
1490 newparameter.prepend(-1);
1491 ex addzeta = convert_H_to_zeta(newparameter);
1492 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
1494 ex addzeta = convert_H_to_zeta(lst(-1));
1495 return (e * (addzeta - H(lst(-1),1/arg).hold())).expand();
1500 // do integration [ReV] (55)
1501 // put parameter -1 in front of existing parameters
1502 ex trafo_H_1mxt1px_prepend_minusone(const ex& e, const ex& arg)
1506 if (is_a<function>(e)) {
1507 name = ex_to<function>(e).get_name();
1512 for (int i=0; i<e.nops(); i++) {
1513 if (is_a<function>(e.op(i))) {
1514 std::string name = ex_to<function>(e.op(i)).get_name();
1522 lst newparameter = ex_to<lst>(h.op(0));
1523 newparameter.prepend(-1);
1524 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
1526 return (e * H(lst(-1),(1-arg)/(1+arg)).hold()).expand();
1531 // do integration [ReV] (55)
1532 // put parameter 1 in front of existing parameters
1533 ex trafo_H_1mxt1px_prepend_one(const ex& e, const ex& arg)
1537 if (is_a<function>(e)) {
1538 name = ex_to<function>(e).get_name();
1543 for (int i=0; i<e.nops(); i++) {
1544 if (is_a<function>(e.op(i))) {
1545 std::string name = ex_to<function>(e.op(i)).get_name();
1553 lst newparameter = ex_to<lst>(h.op(0));
1554 newparameter.prepend(1);
1555 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
1557 return (e * H(lst(1),(1-arg)/(1+arg)).hold()).expand();
1562 // do x -> 1/x transformation
1563 struct map_trafo_H_1overx : public map_function
1565 ex operator()(const ex& e)
1567 if (is_a<add>(e) || is_a<mul>(e)) {
1568 return e.map(*this);
1571 if (is_a<function>(e)) {
1572 std::string name = ex_to<function>(e).get_name();
1575 lst parameter = ex_to<lst>(e.op(0));
1578 // special cases if all parameters are either 0, 1 or -1
1579 bool allthesame = true;
1580 if (parameter.op(0) == 0) {
1581 for (int i=1; i<parameter.nops(); i++) {
1582 if (parameter.op(i) != 0) {
1588 return pow(-1, parameter.nops()) * H(parameter, 1/arg).hold();
1590 } else if (parameter.op(0) == -1) {
1591 for (int i=1; i<parameter.nops(); i++) {
1592 if (parameter.op(i) != -1) {
1598 map_trafo_H_mult unify;
1599 return unify((pow(H(lst(-1),1/arg).hold() - H(lst(0),1/arg).hold(), parameter.nops())
1600 / factorial(parameter.nops())).expand());
1603 for (int i=1; i<parameter.nops(); i++) {
1604 if (parameter.op(i) != 1) {
1610 map_trafo_H_mult unify;
1611 return unify((pow(H(lst(1),1/arg).hold() + H(lst(0),1/arg).hold() + H_polesign, parameter.nops())
1612 / factorial(parameter.nops())).expand());
1616 lst newparameter = parameter;
1617 newparameter.remove_first();
1619 if (parameter.op(0) == 0) {
1622 ex res = convert_H_to_zeta(parameter);
1623 map_trafo_H_1overx recursion;
1624 ex buffer = recursion(H(newparameter, arg).hold());
1625 if (is_a<add>(buffer)) {
1626 for (int i=0; i<buffer.nops(); i++) {
1627 res += trafo_H_1tx_prepend_zero(buffer.op(i), arg);
1630 res += trafo_H_1tx_prepend_zero(buffer, arg);
1634 } else if (parameter.op(0) == -1) {
1636 // leading negative one
1637 ex res = convert_H_to_zeta(parameter);
1638 map_trafo_H_1overx recursion;
1639 ex buffer = recursion(H(newparameter, arg).hold());
1640 if (is_a<add>(buffer)) {
1641 for (int i=0; i<buffer.nops(); i++) {
1642 res += trafo_H_1tx_prepend_zero(buffer.op(i), arg) - trafo_H_1tx_prepend_minusone(buffer.op(i), arg);
1645 res += trafo_H_1tx_prepend_zero(buffer, arg) - trafo_H_1tx_prepend_minusone(buffer, arg);
1652 map_trafo_H_1overx recursion;
1653 map_trafo_H_mult unify;
1654 ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
1656 while (parameter.op(firstzero) == 1) {
1659 for (int i=firstzero-1; i<parameter.nops()-1; i++) {
1663 newparameter.append(parameter[j+1]);
1665 newparameter.append(1);
1666 for (; j<parameter.nops()-1; j++) {
1667 newparameter.append(parameter[j+1]);
1669 res -= H(newparameter, arg).hold();
1671 res = recursion(res).expand() / firstzero;
1683 // do x -> (1-x)/(1+x) transformation
1684 struct map_trafo_H_1mxt1px : public map_function
1686 ex operator()(const ex& e)
1688 if (is_a<add>(e) || is_a<mul>(e)) {
1689 return e.map(*this);
1692 if (is_a<function>(e)) {
1693 std::string name = ex_to<function>(e).get_name();
1696 lst parameter = ex_to<lst>(e.op(0));
1699 // special cases if all parameters are either 0, 1 or -1
1700 bool allthesame = true;
1701 if (parameter.op(0) == 0) {
1702 for (int i=1; i<parameter.nops(); i++) {
1703 if (parameter.op(i) != 0) {
1709 map_trafo_H_mult unify;
1710 return unify((pow(-H(lst(1),(1-arg)/(1+arg)).hold() - H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
1711 / factorial(parameter.nops())).expand());
1713 } else if (parameter.op(0) == -1) {
1714 for (int i=1; i<parameter.nops(); i++) {
1715 if (parameter.op(i) != -1) {
1721 map_trafo_H_mult unify;
1722 return unify((pow(log(2) - H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
1723 / factorial(parameter.nops())).expand());
1726 for (int i=1; i<parameter.nops(); i++) {
1727 if (parameter.op(i) != 1) {
1733 map_trafo_H_mult unify;
1734 return unify((pow(-log(2) - H(lst(0),(1-arg)/(1+arg)).hold() + H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
1735 / factorial(parameter.nops())).expand());
1739 lst newparameter = parameter;
1740 newparameter.remove_first();
1742 if (parameter.op(0) == 0) {
1745 ex res = convert_H_to_zeta(parameter);
1746 map_trafo_H_1mxt1px recursion;
1747 ex buffer = recursion(H(newparameter, arg).hold());
1748 if (is_a<add>(buffer)) {
1749 for (int i=0; i<buffer.nops(); i++) {
1750 res -= trafo_H_1mxt1px_prepend_one(buffer.op(i), arg) + trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
1753 res -= trafo_H_1mxt1px_prepend_one(buffer, arg) + trafo_H_1mxt1px_prepend_minusone(buffer, arg);
1757 } else if (parameter.op(0) == -1) {
1759 // leading negative one
1760 ex res = convert_H_to_zeta(parameter);
1761 map_trafo_H_1mxt1px recursion;
1762 ex buffer = recursion(H(newparameter, arg).hold());
1763 if (is_a<add>(buffer)) {
1764 for (int i=0; i<buffer.nops(); i++) {
1765 res -= trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
1768 res -= trafo_H_1mxt1px_prepend_minusone(buffer, arg);
1775 map_trafo_H_1mxt1px recursion;
1776 map_trafo_H_mult unify;
1777 ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
1779 while (parameter.op(firstzero) == 1) {
1782 for (int i=firstzero-1; i<parameter.nops()-1; i++) {
1786 newparameter.append(parameter[j+1]);
1788 newparameter.append(1);
1789 for (; j<parameter.nops()-1; j++) {
1790 newparameter.append(parameter[j+1]);
1792 res -= H(newparameter, arg).hold();
1794 res = recursion(res).expand() / firstzero;
1806 // do the actual summation.
1807 cln::cl_N H_do_sum(const std::vector<int>& m, const cln::cl_N& x)
1809 const int j = m.size();
1811 std::vector<cln::cl_N> t(j);
1813 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
1814 cln::cl_N factor = cln::expt(x, j) * one;
1820 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),m[j-1]);
1821 for (int k=j-2; k>=1; k--) {
1822 t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), m[k]);
1824 t[0] = t[0] + t[1] * factor / cln::expt(cln::cl_I(q+j-1), m[0]);
1825 factor = factor * x;
1826 } while (t[0] != t0buf);
1832 } // end of anonymous namespace
1835 //////////////////////////////////////////////////////////////////////
1837 // Harmonic polylogarithm H(m,x)
1841 //////////////////////////////////////////////////////////////////////
1844 static ex H_evalf(const ex& x1, const ex& x2)
1846 if (is_a<lst>(x1) && is_a<numeric>(x2)) {
1847 for (int i=0; i<x1.nops(); i++) {
1848 if (!x1.op(i).info(info_flags::integer)) {
1849 return H(x1,x2).hold();
1852 if (x1.nops() < 1) {
1853 return H(x1,x2).hold();
1856 cln::cl_N x = ex_to<numeric>(x2).to_cl_N();
1858 const lst& morg = ex_to<lst>(x1);
1859 // remove trailing zeros ...
1860 if (*(--morg.end()) == 0) {
1861 symbol xtemp("xtemp");
1862 map_trafo_H_reduce_trailing_zeros filter;
1863 return filter(H(x1, xtemp).hold()).subs(xtemp==x2).evalf();
1865 // ... and expand parameter notation
1867 for (lst::const_iterator it = morg.begin(); it != morg.end(); it++) {
1869 for (ex count=*it-1; count > 0; count--) {
1873 } else if (*it < -1) {
1874 for (ex count=*it+1; count < 0; count++) {
1883 // since the transformations produce a lot of terms, they are only efficient for
1884 // argument near one.
1885 // no transformation needed -> do summation
1886 if (cln::abs(x) < 0.95) {
1890 if (convert_parameter_H_to_Li(m, m_lst, s_lst, pf)) {
1891 // negative parameters -> s_lst is filled
1892 std::vector<int> m_int;
1893 std::vector<cln::cl_N> x_cln;
1894 for (lst::const_iterator it_int = m_lst.begin(), it_cln = s_lst.begin();
1895 it_int != m_lst.end(); it_int++, it_cln++) {
1896 m_int.push_back(ex_to<numeric>(*it_int).to_int());
1897 x_cln.push_back(ex_to<numeric>(*it_cln).to_cl_N());
1899 x_cln.front() = x_cln.front() * x;
1900 return pf * numeric(multipleLi_do_sum(m_int, x_cln));
1902 // only positive parameters
1904 if (m_lst.nops() == 1) {
1905 return Li(m_lst.op(0), x2).evalf();
1907 std::vector<int> m_int;
1908 for (lst::const_iterator it = m_lst.begin(); it != m_lst.end(); it++) {
1909 m_int.push_back(ex_to<numeric>(*it).to_int());
1911 return numeric(H_do_sum(m_int, x));
1917 // ensure that the realpart of the argument is positive
1918 if (cln::realpart(x) < 0) {
1920 for (int i=0; i<m.nops(); i++) {
1922 m.let_op(i) = -m.op(i);
1928 // choose transformations
1929 symbol xtemp("xtemp");
1930 if (cln::abs(x-1) < 1.4142) {
1932 map_trafo_H_1mxt1px trafo;
1933 res *= trafo(H(m, xtemp));
1936 map_trafo_H_1overx trafo;
1937 res *= trafo(H(m, xtemp));
1938 if (cln::imagpart(x) <= 0) {
1939 res = res.subs(H_polesign == -I*Pi);
1941 res = res.subs(H_polesign == I*Pi);
1947 // map_trafo_H_convert converter;
1948 // res = converter(res).expand();
1950 // res.find(H(wild(1),wild(2)), ll);
1951 // res.find(zeta(wild(1)), ll);
1952 // res.find(zeta(wild(1),wild(2)), ll);
1953 // res = res.collect(ll);
1955 return res.subs(xtemp == numeric(x)).evalf();
1958 return H(x1,x2).hold();
1962 static ex H_eval(const ex& m_, const ex& x)
1965 if (is_a<lst>(m_)) {
1970 if (m.nops() == 0) {
1978 if (*m.begin() > _ex1) {
1984 } else if (*m.begin() < _ex_1) {
1990 } else if (*m.begin() == _ex0) {
1997 for (lst::const_iterator it = ++m.begin(); it != m.end(); it++) {
1998 if ((*it).info(info_flags::integer)) {
2009 } else if (*it < _ex_1) {
2029 } else if (step == 1) {
2041 // if some m_i is not an integer
2042 return H(m_, x).hold();
2045 if ((x == _ex1) && (*(--m.end()) != _ex0)) {
2046 return convert_H_to_zeta(m);
2052 return H(m_, x).hold();
2054 return pow(log(x), m.nops()) / factorial(m.nops());
2057 return pow(-pos1*log(1-pos1*x), m.nops()) / factorial(m.nops());
2059 } else if ((step == 1) && (pos1 == _ex0)){
2064 return pow(-1, p) * S(n, p, -x);
2070 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
2071 return H(m_, x).evalf();
2073 return H(m_, x).hold();
2077 static ex H_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
2080 seq.push_back(expair(H(m, x), 0));
2081 return pseries(rel, seq);
2085 static ex H_deriv(const ex& m_, const ex& x, unsigned deriv_param)
2087 GINAC_ASSERT(deriv_param < 2);
2088 if (deriv_param == 0) {
2092 if (is_a<lst>(m_)) {
2108 return 1/(1-x) * H(m, x);
2109 } else if (mb == _ex_1) {
2110 return 1/(1+x) * H(m, x);
2117 static void H_print_latex(const ex& m_, const ex& x, const print_context& c)
2120 if (is_a<lst>(m_)) {
2125 c.s << "\\mbox{H}_{";
2126 lst::const_iterator itm = m.begin();
2129 for (; itm != m.end(); itm++) {
2139 REGISTER_FUNCTION(H,
2140 evalf_func(H_evalf).
2142 series_func(H_series).
2143 derivative_func(H_deriv).
2144 print_func<print_latex>(H_print_latex).
2145 do_not_evalf_params());
2148 // takes a parameter list for H and returns an expression with corresponding multiple polylogarithms
2149 ex convert_H_to_Li(const ex& m, const ex& x)
2151 map_trafo_H_reduce_trailing_zeros filter;
2152 map_trafo_H_convert_to_Li filter2;
2154 return filter2(filter(H(m, x).hold()));
2156 return filter2(filter(H(lst(m), x).hold()));
2161 //////////////////////////////////////////////////////////////////////
2163 // Multiple zeta values zeta(x) and zeta(x,s)
2167 //////////////////////////////////////////////////////////////////////
2170 // anonymous namespace for helper functions
2174 // parameters and data for [Cra] algorithm
2175 const cln::cl_N lambda = cln::cl_N("319/320");
2178 std::vector<std::vector<cln::cl_N> > f_kj;
2179 std::vector<cln::cl_N> crB;
2180 std::vector<std::vector<cln::cl_N> > crG;
2181 std::vector<cln::cl_N> crX;
2184 void halfcyclic_convolute(const std::vector<cln::cl_N>& a, const std::vector<cln::cl_N>& b, std::vector<cln::cl_N>& c)
2186 const int size = a.size();
2187 for (int n=0; n<size; n++) {
2189 for (int m=0; m<=n; m++) {
2190 c[n] = c[n] + a[m]*b[n-m];
2197 void initcX(const std::vector<int>& s)
2199 const int k = s.size();
2205 for (int i=0; i<=L2; i++) {
2206 crB.push_back(bernoulli(i).to_cl_N() / cln::factorial(i));
2211 for (int m=0; m<k-1; m++) {
2212 std::vector<cln::cl_N> crGbuf;
2215 for (int i=0; i<=L2; i++) {
2216 crGbuf.push_back(cln::factorial(i + Sm - m - 2) / cln::factorial(i + Smp1 - m - 2));
2218 crG.push_back(crGbuf);
2223 for (int m=0; m<k-1; m++) {
2224 std::vector<cln::cl_N> Xbuf;
2225 for (int i=0; i<=L2; i++) {
2226 Xbuf.push_back(crX[i] * crG[m][i]);
2228 halfcyclic_convolute(Xbuf, crB, crX);
2234 cln::cl_N crandall_Y_loop(const cln::cl_N& Sqk)
2236 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
2237 cln::cl_N factor = cln::expt(lambda, Sqk);
2238 cln::cl_N res = factor / Sqk * crX[0] * one;
2243 factor = factor * lambda;
2245 res = res + crX[N] * factor / (N+Sqk);
2246 } while ((res != resbuf) || cln::zerop(crX[N]));
2252 void calc_f(int maxr)
2257 cln::cl_N t0, t1, t2, t3, t4;
2259 std::vector<std::vector<cln::cl_N> >::iterator it = f_kj.begin();
2260 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
2262 t0 = cln::exp(-lambda);
2264 for (k=1; k<=L1; k++) {
2267 for (j=1; j<=maxr; j++) {
2270 for (i=2; i<=j; i++) {
2274 (*it).push_back(t2 * t3 * cln::expt(cln::cl_I(k),-j) * one);
2282 cln::cl_N crandall_Z(const std::vector<int>& s)
2284 const int j = s.size();
2293 t0 = t0 + f_kj[q+j-2][s[0]-1];
2294 } while (t0 != t0buf);
2296 return t0 / cln::factorial(s[0]-1);
2299 std::vector<cln::cl_N> t(j);
2306 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),s[j-1]);
2307 for (int k=j-2; k>=1; k--) {
2308 t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), s[k]);
2310 t[0] = t[0] + t[1] * f_kj[q+j-2][s[0]-1];
2311 } while (t[0] != t0buf);
2313 return t[0] / cln::factorial(s[0]-1);
2318 cln::cl_N zeta_do_sum_Crandall(const std::vector<int>& s)
2320 std::vector<int> r = s;
2321 const int j = r.size();
2323 // decide on maximal size of f_kj for crandall_Z
2327 L1 = Digits * 3 + j*2;
2330 // decide on maximal size of crX for crandall_Y
2333 } else if (Digits < 86) {
2335 } else if (Digits < 192) {
2337 } else if (Digits < 394) {
2339 } else if (Digits < 808) {
2349 for (int i=0; i<j; i++) {
2358 const cln::cl_N r0factorial = cln::factorial(r[0]-1);
2360 std::vector<int> rz;
2363 for (int k=r.size()-1; k>0; k--) {
2365 rz.insert(rz.begin(), r.back());
2366 skp1buf = rz.front();
2372 for (int q=0; q<skp1buf; q++) {
2374 cln::cl_N pp1 = crandall_Y_loop(Srun+q-k);
2375 cln::cl_N pp2 = crandall_Z(rz);
2380 res = res - pp1 * pp2 / cln::factorial(q);
2382 res = res + pp1 * pp2 / cln::factorial(q);
2385 rz.front() = skp1buf;
2387 rz.insert(rz.begin(), r.back());
2391 res = (res + crandall_Y_loop(S-j)) / r0factorial + crandall_Z(rz);
2397 cln::cl_N zeta_do_sum_simple(const std::vector<int>& r)
2399 const int j = r.size();
2401 // buffer for subsums
2402 std::vector<cln::cl_N> t(j);
2403 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
2410 t[j-1] = t[j-1] + one / cln::expt(cln::cl_I(q),r[j-1]);
2411 for (int k=j-2; k>=0; k--) {
2412 t[k] = t[k] + one * t[k+1] / cln::expt(cln::cl_I(q+j-1-k), r[k]);
2414 } while (t[0] != t0buf);
2420 // does Hoelder convolution. see [BBB] (7.0)
2421 cln::cl_N zeta_do_Hoelder_convolution(const std::vector<int>& m_, const std::vector<int>& s_)
2423 // prepare parameters
2424 // holds Li arguments in [BBB] notation
2425 std::vector<int> s = s_;
2426 std::vector<int> m_p = m_;
2427 std::vector<int> m_q;
2428 // holds Li arguments in nested sums notation
2429 std::vector<cln::cl_N> s_p(s.size(), cln::cl_N(1));
2430 s_p[0] = s_p[0] * cln::cl_N("1/2");
2431 // convert notations
2433 for (int i=0; i<s_.size(); i++) {
2438 s[i] = sig * std::abs(s[i]);
2440 std::vector<cln::cl_N> s_q;
2441 cln::cl_N signum = 1;
2444 cln::cl_N res = multipleLi_do_sum(m_p, s_p);
2449 // change parameters
2450 if (s.front() > 0) {
2451 if (m_p.front() == 1) {
2452 m_p.erase(m_p.begin());
2453 s_p.erase(s_p.begin());
2454 if (s_p.size() > 0) {
2455 s_p.front() = s_p.front() * cln::cl_N("1/2");
2461 m_q.insert(m_q.begin(), 1);
2462 if (s_q.size() > 0) {
2463 s_q.front() = s_q.front() * 2;
2465 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
2468 if (m_p.front() == 1) {
2469 m_p.erase(m_p.begin());
2470 cln::cl_N spbuf = s_p.front();
2471 s_p.erase(s_p.begin());
2472 if (s_p.size() > 0) {
2473 s_p.front() = s_p.front() * spbuf;
2476 m_q.insert(m_q.begin(), 1);
2477 if (s_q.size() > 0) {
2478 s_q.front() = s_q.front() * 4;
2480 s_q.insert(s_q.begin(), cln::cl_N("1/4"));
2484 m_q.insert(m_q.begin(), 1);
2485 if (s_q.size() > 0) {
2486 s_q.front() = s_q.front() * 2;
2488 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
2493 if (m_p.size() == 0) break;
2495 res = res + signum * multipleLi_do_sum(m_p, s_p) * multipleLi_do_sum(m_q, s_q);
2500 res = res + signum * multipleLi_do_sum(m_q, s_q);
2506 } // end of anonymous namespace
2509 //////////////////////////////////////////////////////////////////////
2511 // Multiple zeta values zeta(x)
2515 //////////////////////////////////////////////////////////////////////
2518 static ex zeta1_evalf(const ex& x)
2520 if (is_exactly_a<lst>(x) && (x.nops()>1)) {
2522 // multiple zeta value
2523 const int count = x.nops();
2524 const lst& xlst = ex_to<lst>(x);
2525 std::vector<int> r(count);
2527 // check parameters and convert them
2528 lst::const_iterator it1 = xlst.begin();
2529 std::vector<int>::iterator it2 = r.begin();
2531 if (!(*it1).info(info_flags::posint)) {
2532 return zeta(x).hold();
2534 *it2 = ex_to<numeric>(*it1).to_int();
2537 } while (it2 != r.end());
2539 // check for divergence
2541 return zeta(x).hold();
2544 // decide on summation algorithm
2545 // this is still a bit clumsy
2546 int limit = (Digits>17) ? 10 : 6;
2547 if ((r[0] < limit) || ((count > 3) && (r[1] < limit/2))) {
2548 return numeric(zeta_do_sum_Crandall(r));
2550 return numeric(zeta_do_sum_simple(r));
2554 // single zeta value
2555 if (is_exactly_a<numeric>(x) && (x != 1)) {
2557 return zeta(ex_to<numeric>(x));
2558 } catch (const dunno &e) { }
2561 return zeta(x).hold();
2565 static ex zeta1_eval(const ex& m)
2567 if (is_exactly_a<lst>(m)) {
2568 if (m.nops() == 1) {
2569 return zeta(m.op(0));
2571 return zeta(m).hold();
2574 if (m.info(info_flags::numeric)) {
2575 const numeric& y = ex_to<numeric>(m);
2576 // trap integer arguments:
2577 if (y.is_integer()) {
2581 if (y.is_equal(_num1)) {
2582 return zeta(m).hold();
2584 if (y.info(info_flags::posint)) {
2585 if (y.info(info_flags::odd)) {
2586 return zeta(m).hold();
2588 return abs(bernoulli(y)) * pow(Pi, y) * pow(_num2, y-_num1) / factorial(y);
2591 if (y.info(info_flags::odd)) {
2592 return -bernoulli(_num1-y) / (_num1-y);
2599 if (y.info(info_flags::numeric) && !y.info(info_flags::crational)) {
2600 return zeta1_evalf(m);
2603 return zeta(m).hold();
2607 static ex zeta1_deriv(const ex& m, unsigned deriv_param)
2609 GINAC_ASSERT(deriv_param==0);
2611 if (is_exactly_a<lst>(m)) {
2614 return zetaderiv(_ex1, m);
2619 static void zeta1_print_latex(const ex& m_, const print_context& c)
2622 if (is_a<lst>(m_)) {
2623 const lst& m = ex_to<lst>(m_);
2624 lst::const_iterator it = m.begin();
2627 for (; it != m.end(); it++) {
2638 unsigned zeta1_SERIAL::serial = function::register_new(function_options("zeta", 1).
2639 evalf_func(zeta1_evalf).
2640 eval_func(zeta1_eval).
2641 derivative_func(zeta1_deriv).
2642 print_func<print_latex>(zeta1_print_latex).
2643 do_not_evalf_params().
2647 //////////////////////////////////////////////////////////////////////
2649 // Alternating Euler sum zeta(x,s)
2653 //////////////////////////////////////////////////////////////////////
2656 static ex zeta2_evalf(const ex& x, const ex& s)
2658 if (is_exactly_a<lst>(x)) {
2660 // alternating Euler sum
2661 const int count = x.nops();
2662 const lst& xlst = ex_to<lst>(x);
2663 const lst& slst = ex_to<lst>(s);
2664 std::vector<int> xi(count);
2665 std::vector<int> si(count);
2667 // check parameters and convert them
2668 lst::const_iterator it_xread = xlst.begin();
2669 lst::const_iterator it_sread = slst.begin();
2670 std::vector<int>::iterator it_xwrite = xi.begin();
2671 std::vector<int>::iterator it_swrite = si.begin();
2673 if (!(*it_xread).info(info_flags::posint)) {
2674 return zeta(x, s).hold();
2676 *it_xwrite = ex_to<numeric>(*it_xread).to_int();
2677 if (*it_sread > 0) {
2686 } while (it_xwrite != xi.end());
2688 // check for divergence
2689 if ((xi[0] == 1) && (si[0] == 1)) {
2690 return zeta(x, s).hold();
2693 // use Hoelder convolution
2694 return numeric(zeta_do_Hoelder_convolution(xi, si));
2697 return zeta(x, s).hold();
2701 static ex zeta2_eval(const ex& m, const ex& s_)
2703 if (is_exactly_a<lst>(s_)) {
2704 const lst& s = ex_to<lst>(s_);
2705 for (lst::const_iterator it = s.begin(); it != s.end(); it++) {
2706 if ((*it).info(info_flags::positive)) {
2709 return zeta(m, s_).hold();
2712 } else if (s_.info(info_flags::positive)) {
2716 return zeta(m, s_).hold();
2720 static ex zeta2_deriv(const ex& m, const ex& s, unsigned deriv_param)
2722 GINAC_ASSERT(deriv_param==0);
2724 if (is_exactly_a<lst>(m)) {
2727 if ((is_exactly_a<lst>(s) && s.op(0).info(info_flags::positive)) || s.info(info_flags::positive)) {
2728 return zetaderiv(_ex1, m);
2735 static void zeta2_print_latex(const ex& m_, const ex& s_, const print_context& c)
2738 if (is_a<lst>(m_)) {
2744 if (is_a<lst>(s_)) {
2750 lst::const_iterator itm = m.begin();
2751 lst::const_iterator its = s.begin();
2753 c.s << "\\overline{";
2761 for (; itm != m.end(); itm++, its++) {
2764 c.s << "\\overline{";
2775 unsigned zeta2_SERIAL::serial = function::register_new(function_options("zeta", 2).
2776 evalf_func(zeta2_evalf).
2777 eval_func(zeta2_eval).
2778 derivative_func(zeta2_deriv).
2779 print_func<print_latex>(zeta2_print_latex).
2780 do_not_evalf_params().
2784 } // namespace GiNaC