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 speeded 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;
105 // initial size of Xn that should suffice for 32bit machines (must be even)
106 const int xninitsizestep = 26;
107 int xninitsize = xninitsizestep;
111 // This function calculates the X_n. The X_n are needed for speed up of classical polylogarithms.
112 // With these numbers the polylogs can be calculated as follows:
113 // Li_p (x) = \sum_{n=0}^\infty X_{p-2}(n) u^{n+1}/(n+1)! with u = -log(1-x)
114 // X_0(n) = B_n (Bernoulli numbers)
115 // X_p(n) = \sum_{k=0}^n binomial(n,k) B_{n-k} / (k+1) * X_{p-1}(k)
116 // The calculation of Xn depends on X0 and X{n-1}.
117 // X_0 is special, it holds only the non-zero Bernoulli numbers with index 2 or greater.
118 // This results in a slightly more complicated algorithm for the X_n.
119 // The first index in Xn corresponds to the index of the polylog minus 2.
120 // The second index in Xn corresponds to the index from the actual sum.
124 // calculate X_2 and higher (corresponding to Li_4 and higher)
125 std::vector<cln::cl_N> buf(xninitsize);
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<=xninitsize; 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(xninitsize);
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<=xninitsize; 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(xninitsize/2);
175 std::vector<cln::cl_N>::iterator it = buf.begin();
176 for (int i=1; i<=xninitsize/2; i++) {
177 *it = bernoulli(i*2).to_cl_N();
187 // doubles the number of entries in each Xn[]
190 const int pos0 = xninitsize / 2;
192 for (int i=1; i<=xninitsizestep/2; ++i) {
193 Xn[0].push_back(bernoulli((i+pos0)*2).to_cl_N());
196 int xend = xninitsize + xninitsizestep;
199 for (int i=xninitsize+1; i<=xend; ++i) {
201 result = -Xn[0][(i-3)/2]/2;
202 Xn[1].push_back((cln::binomial(i,1)/cln::cl_I(2) + cln::binomial(i,i-1)/cln::cl_I(i))*result);
204 result = Xn[0][i/2-1] + Xn[0][i/2-1]/(i+1);
205 for (int k=1; k<i/2; k++) {
206 result = result + cln::binomial(i,k*2) * Xn[0][k-1] * Xn[0][i/2-k-1] / (k*2+1);
208 Xn[1].push_back(result);
212 for (int n=2; n<Xn.size(); ++n) {
213 for (int i=xninitsize+1; i<=xend; ++i) {
215 result = 0; // k == 0
217 result = Xn[0][i/2-1]; // k == 0
219 for (int k=1; k<i-1; ++k) {
220 if ( !(((i-k) & 1) && ((i-k) > 1)) ) {
221 result = result + cln::binomial(i,k) * Xn[0][(i-k)/2-1] * Xn[n-1][k-1] / (k+1);
224 result = result - cln::binomial(i,i-1) * Xn[n-1][i-2] / 2 / i; // k == i-1
225 result = result + Xn[n-1][i-1] / (i+1); // k == i
226 Xn[n].push_back(result);
230 xninitsize += xninitsizestep;
234 // calculates Li(2,x) without Xn
235 cln::cl_N Li2_do_sum(const cln::cl_N& x)
239 cln::cl_N num = x * cln::cl_float(1, cln::float_format(Digits));
240 cln::cl_I den = 1; // n^2 = 1
245 den = den + i; // n^2 = 4, 9, 16, ...
247 res = res + num / den;
248 } while (res != resbuf);
253 // calculates Li(2,x) with Xn
254 cln::cl_N Li2_do_sum_Xn(const cln::cl_N& x)
256 std::vector<cln::cl_N>::const_iterator it = Xn[0].begin();
257 std::vector<cln::cl_N>::const_iterator xend = Xn[0].end();
258 cln::cl_N u = -cln::log(1-x);
259 cln::cl_N factor = u * cln::cl_float(1, cln::float_format(Digits));
260 cln::cl_N uu = cln::square(u);
261 cln::cl_N res = u - uu/4;
266 factor = factor * uu / (2*i * (2*i+1));
267 res = res + (*it) * factor;
271 it = Xn[0].begin() + (i-1);
274 } while (res != resbuf);
279 // calculates Li(n,x), n>2 without Xn
280 cln::cl_N Lin_do_sum(int n, const cln::cl_N& x)
282 cln::cl_N factor = x * cln::cl_float(1, cln::float_format(Digits));
289 res = res + factor / cln::expt(cln::cl_I(i),n);
291 } while (res != resbuf);
296 // calculates Li(n,x), n>2 with Xn
297 cln::cl_N Lin_do_sum_Xn(int n, const cln::cl_N& x)
299 std::vector<cln::cl_N>::const_iterator it = Xn[n-2].begin();
300 std::vector<cln::cl_N>::const_iterator xend = Xn[n-2].end();
301 cln::cl_N u = -cln::log(1-x);
302 cln::cl_N factor = u * cln::cl_float(1, cln::float_format(Digits));
308 factor = factor * u / i;
309 res = res + (*it) * factor;
313 it = Xn[n-2].begin() + (i-2);
314 xend = Xn[n-2].end();
316 } while (res != resbuf);
321 // forward declaration needed by function Li_projection and C below
322 numeric S_num(int n, int p, const numeric& x);
325 // helper function for classical polylog Li
326 cln::cl_N Li_projection(int n, const cln::cl_N& x, const cln::float_format_t& prec)
328 // treat n=2 as special case
330 // check if precalculated X0 exists
335 if (cln::realpart(x) < 0.5) {
336 // choose the faster algorithm
337 // the switching point was empirically determined. the optimal point
338 // depends on hardware, Digits, ... so an approx value is okay.
339 // it solves also the problem with precision due to the u=-log(1-x) transformation
340 if (cln::abs(cln::realpart(x)) < 0.25) {
342 return Li2_do_sum(x);
344 return Li2_do_sum_Xn(x);
347 // choose the faster algorithm
348 if (cln::abs(cln::realpart(x)) > 0.75) {
349 return -Li2_do_sum(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
351 return -Li2_do_sum_Xn(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
355 // check if precalculated Xn exist
357 for (int i=xnsize; i<n-1; i++) {
362 if (cln::realpart(x) < 0.5) {
363 // choose the faster algorithm
364 // with n>=12 the "normal" summation always wins against the method with Xn
365 if ((cln::abs(cln::realpart(x)) < 0.3) || (n >= 12)) {
366 return Lin_do_sum(n, x);
368 return Lin_do_sum_Xn(n, x);
371 cln::cl_N result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
372 for (int j=0; j<n-1; j++) {
373 result = result + (S_num(n-j-1, 1, 1).to_cl_N() - S_num(1, n-j-1, 1-x).to_cl_N())
374 * cln::expt(cln::log(x), j) / cln::factorial(j);
382 // helper function for classical polylog Li
383 numeric Li_num(int n, const numeric& x)
387 return -cln::log(1-x.to_cl_N());
398 return -(1-cln::expt(cln::cl_I(2),1-n)) * cln::zeta(n);
400 if (abs(x.real()) < 0.4 && abs(abs(x)-1) < 0.01) {
401 cln::cl_N x_ = ex_to<numeric>(x).to_cl_N();
402 cln::cl_N result = -cln::expt(cln::log(x_), n-1) * cln::log(1-x_) / cln::factorial(n-1);
403 for (int j=0; j<n-1; j++) {
404 result = result + (S_num(n-j-1, 1, 1).to_cl_N() - S_num(1, n-j-1, 1-x_).to_cl_N())
405 * cln::expt(cln::log(x_), j) / cln::factorial(j);
410 // what is the desired float format?
411 // first guess: default format
412 cln::float_format_t prec = cln::default_float_format;
413 const cln::cl_N value = x.to_cl_N();
414 // second guess: the argument's format
415 if (!x.real().is_rational())
416 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
417 else if (!x.imag().is_rational())
418 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
421 if (cln::abs(value) > 1) {
422 cln::cl_N result = -cln::expt(cln::log(-value),n) / cln::factorial(n);
423 // check if argument is complex. if it is real, the new polylog has to be conjugated.
424 if (cln::zerop(cln::imagpart(value))) {
426 result = result + conjugate(Li_projection(n, cln::recip(value), prec));
429 result = result - conjugate(Li_projection(n, cln::recip(value), prec));
434 result = result + Li_projection(n, cln::recip(value), prec);
437 result = result - Li_projection(n, cln::recip(value), prec);
441 for (int j=0; j<n-1; j++) {
442 add = add + (1+cln::expt(cln::cl_I(-1),n-j)) * (1-cln::expt(cln::cl_I(2),1-n+j))
443 * Li_num(n-j,1).to_cl_N() * cln::expt(cln::log(-value),j) / cln::factorial(j);
445 result = result - add;
449 return Li_projection(n, value, prec);
454 } // end of anonymous namespace
457 //////////////////////////////////////////////////////////////////////
459 // Multiple polylogarithm Li(n,x)
463 //////////////////////////////////////////////////////////////////////
466 // anonymous namespace for helper function
470 cln::cl_N multipleLi_do_sum(const std::vector<int>& s, const std::vector<cln::cl_N>& x)
472 const int j = s.size();
474 std::vector<cln::cl_N> t(j);
475 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
483 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
484 for (int k=j-2; k>=0; k--) {
485 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]);
487 // ... and do it again (to avoid premature drop out due to special arguments)
489 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
490 for (int k=j-2; k>=0; k--) {
491 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]);
493 } while (t[0] != t0buf);
498 // forward declaration for Li_eval()
499 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf);
502 } // end of anonymous namespace
505 //////////////////////////////////////////////////////////////////////
507 // Classical polylogarithm and multiple polylogarithm Li(n,x)
511 //////////////////////////////////////////////////////////////////////
514 static ex Li_evalf(const ex& x1, const ex& x2)
516 // classical polylogs
517 if (is_a<numeric>(x1) && is_a<numeric>(x2)) {
518 return Li_num(ex_to<numeric>(x1).to_int(), ex_to<numeric>(x2));
520 if (is_a<numeric>(x1) && !is_a<lst>(x2)) {
521 // try to numerically evaluate second argument
522 ex x2_val = x2.evalf();
523 if (is_a<numeric>(x2_val)) {
524 return Li_num(ex_to<numeric>(x1).to_int(), ex_to<numeric>(x2_val));
526 return Li(x1, x2).hold();
530 else if (is_a<lst>(x1) && is_a<lst>(x2)) {
532 for (int i=0; i<x1.nops(); i++) {
533 if (!x1.op(i).info(info_flags::posint)) {
534 return Li(x1, x2).hold();
536 if (!is_a<numeric>(x2.op(i))) {
537 return Li(x1, x2).hold();
540 if (abs(conv) >= 1) {
541 return Li(x1, x2).hold();
546 std::vector<cln::cl_N> x;
547 for (int i=0; i<ex_to<numeric>(x1.nops()).to_int(); i++) {
548 m.push_back(ex_to<numeric>(x1.op(i)).to_int());
549 x.push_back(ex_to<numeric>(x2.op(i)).to_cl_N());
552 return numeric(multipleLi_do_sum(m, x));
555 return Li(x1,x2).hold();
559 static ex Li_eval(const ex& m_, const ex& x_)
581 return (pow(2,1-m)-1) * zeta(m);
587 if (x_.is_equal(I)) {
588 return power(Pi,_ex2)/_ex_48 + Catalan*I;
590 if (x_.is_equal(-I)) {
591 return power(Pi,_ex2)/_ex_48 - Catalan*I;
594 if (m.info(info_flags::posint) && x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
595 return Li_num(ex_to<numeric>(m).to_int(), ex_to<numeric>(x));
601 bool doevalf = false;
602 bool doevalfveto = true;
603 const lst& m = ex_to<lst>(m_);
604 const lst& x = ex_to<lst>(x_);
605 lst::const_iterator itm = m.begin();
606 lst::const_iterator itx = x.begin();
607 for (; itm != m.end(); itm++, itx++) {
608 if (!(*itm).info(info_flags::posint)) {
609 return Li(m_, x_).hold();
611 if ((*itx != _ex1) && (*itx != _ex_1)) {
612 if (itx != x.begin()) {
620 if (!(*itx).info(info_flags::numeric)) {
623 if (!(*itx).info(info_flags::crational)) {
635 lst newm = convert_parameter_Li_to_H(m, x, pf);
636 return pf * H(newm, x[0]);
638 if (doevalfveto && doevalf) {
639 return Li(m_, x_).evalf();
642 return Li(m_, x_).hold();
646 static ex Li_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
649 seq.push_back(expair(Li(m, x), 0));
650 return pseries(rel, seq);
654 static ex Li_deriv(const ex& m_, const ex& x_, unsigned deriv_param)
656 GINAC_ASSERT(deriv_param < 2);
657 if (deriv_param == 0) {
661 throw std::runtime_error("don't know how to derivate multiple polylogarithm!");
676 return Li(m-1, x) / x;
683 static void Li_print_latex(const ex& m_, const ex& x_, const print_context& c)
697 c.s << "\\mbox{Li}_{";
698 lst::const_iterator itm = m.begin();
701 for (; itm != m.end(); itm++) {
706 lst::const_iterator itx = x.begin();
709 for (; itx != x.end(); itx++) {
717 REGISTER_FUNCTION(Li,
718 evalf_func(Li_evalf).
720 series_func(Li_series).
721 derivative_func(Li_deriv).
722 print_func<print_latex>(Li_print_latex).
723 do_not_evalf_params());
726 //////////////////////////////////////////////////////////////////////
728 // Nielsen's generalized polylogarithm S(n,p,x)
732 //////////////////////////////////////////////////////////////////////
735 // anonymous namespace for helper functions
739 // lookup table for special Euler-Zagier-Sums (used for S_n,p(x))
741 std::vector<std::vector<cln::cl_N> > Yn;
742 int ynsize = 0; // number of Yn[]
743 int ynlength = 100; // initial length of all Yn[i]
746 // This function calculates the Y_n. The Y_n are needed for the evaluation of S_{n,p}(x).
747 // The Y_n are basically Euler-Zagier sums with all m_i=1. They are subsums in the Z-sum
748 // representing S_{n,p}(x).
749 // The first index in Y_n corresponds to the parameter p minus one, i.e. the depth of the
751 // The second index in Y_n corresponds to the running index of the outermost sum in the full Z-sum
752 // representing S_{n,p}(x).
753 // The calculation of Y_n uses the values from Y_{n-1}.
754 void fill_Yn(int n, const cln::float_format_t& prec)
756 const int initsize = ynlength;
757 //const int initsize = initsize_Yn;
758 cln::cl_N one = cln::cl_float(1, prec);
761 std::vector<cln::cl_N> buf(initsize);
762 std::vector<cln::cl_N>::iterator it = buf.begin();
763 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
764 *it = (*itprev) / cln::cl_N(n+1) * one;
767 // sums with an index smaller than the depth are zero and need not to be calculated.
768 // calculation starts with depth, which is n+2)
769 for (int i=n+2; i<=initsize+n; i++) {
770 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
776 std::vector<cln::cl_N> buf(initsize);
777 std::vector<cln::cl_N>::iterator it = buf.begin();
780 for (int i=2; i<=initsize; i++) {
781 *it = *(it-1) + 1 / cln::cl_N(i) * one;
790 // make Yn longer ...
791 void make_Yn_longer(int newsize, const cln::float_format_t& prec)
794 cln::cl_N one = cln::cl_float(1, prec);
796 Yn[0].resize(newsize);
797 std::vector<cln::cl_N>::iterator it = Yn[0].begin();
799 for (int i=ynlength+1; i<=newsize; i++) {
800 *it = *(it-1) + 1 / cln::cl_N(i) * one;
804 for (int n=1; n<ynsize; n++) {
805 Yn[n].resize(newsize);
806 std::vector<cln::cl_N>::iterator it = Yn[n].begin();
807 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
810 for (int i=ynlength+n+1; i<=newsize+n; i++) {
811 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
821 // helper function for S(n,p,x)
823 cln::cl_N C(int n, int p)
827 for (int k=0; k<p; k++) {
828 for (int j=0; j<=(n+k-1)/2; j++) {
832 result = result - 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1).to_cl_N() / cln::factorial(2*j);
835 result = result + 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1).to_cl_N() / cln::factorial(2*j);
842 result = result + cln::factorial(n+k-1)
843 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
844 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
847 result = result - cln::factorial(n+k-1)
848 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
849 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
854 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()
855 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
858 result = result + cln::factorial(n+k-1)
859 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
860 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
868 if (((np)/2+n) & 1) {
869 result = -result - cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
872 result = -result + cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
880 // helper function for S(n,p,x)
881 // [Kol] remark to (9.1)
891 for (int m=2; m<=k; m++) {
892 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * a_k(k-m);
899 // helper function for S(n,p,x)
900 // [Kol] remark to (9.1)
910 for (int m=2; m<=k; m++) {
911 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * b_k(k-m);
918 // helper function for S(n,p,x)
919 cln::cl_N S_do_sum(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
922 return Li_projection(n+1, x, prec);
925 // check if precalculated values are sufficient
927 for (int i=ynsize; i<p-1; i++) {
932 // should be done otherwise
933 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
934 cln::cl_N xf = x * one;
935 //cln::cl_N xf = x * cln::cl_float(1, prec);
939 cln::cl_N factor = cln::expt(xf, p);
943 if (i-p >= ynlength) {
945 make_Yn_longer(ynlength*2, prec);
947 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? ...
948 //res = res + factor / cln::expt(cln::cl_I(i),n+1) * (*it); // should we check it? or rely on magic number? ...
949 factor = factor * xf;
951 } while (res != resbuf);
957 // helper function for S(n,p,x)
958 cln::cl_N S_projection(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
961 if (cln::abs(cln::realpart(x)) > cln::cl_F("0.5")) {
963 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(x),n)
964 * cln::expt(cln::log(1-x),p) / cln::factorial(n) / cln::factorial(p);
966 for (int s=0; s<n; s++) {
968 for (int r=0; r<p; r++) {
969 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-x),r)
970 * S_do_sum(p-r,n-s,1-x,prec) / cln::factorial(r);
972 result = result + cln::expt(cln::log(x),s) * (S_num(n-s,p,1).to_cl_N() - res2) / cln::factorial(s);
978 return S_do_sum(n, p, x, prec);
982 // helper function for S(n,p,x)
983 numeric S_num(int n, int p, const numeric& x)
987 // [Kol] (2.22) with (2.21)
988 return cln::zeta(p+1);
993 return cln::zeta(n+1);
998 for (int nu=0; nu<n; nu++) {
999 for (int rho=0; rho<=p; rho++) {
1000 result = result + b_k(n-nu-1) * b_k(p-rho) * a_k(nu+rho+1)
1001 * cln::factorial(nu+rho+1) / cln::factorial(rho) / cln::factorial(nu+1);
1004 result = result * cln::expt(cln::cl_I(-1),n+p-1);
1011 return -(1-cln::expt(cln::cl_I(2),-n)) * cln::zeta(n+1);
1013 // throw std::runtime_error("don't know how to evaluate this function!");
1016 // what is the desired float format?
1017 // first guess: default format
1018 cln::float_format_t prec = cln::default_float_format;
1019 const cln::cl_N value = x.to_cl_N();
1020 // second guess: the argument's format
1021 if (!x.real().is_rational())
1022 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
1023 else if (!x.imag().is_rational())
1024 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
1027 if ((cln::realpart(value) < -0.5) || (n == 0)) {
1029 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(value),n)
1030 * cln::expt(cln::log(1-value),p) / cln::factorial(n) / cln::factorial(p);
1032 for (int s=0; s<n; s++) {
1034 for (int r=0; r<p; r++) {
1035 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-value),r)
1036 * S_num(p-r,n-s,1-value).to_cl_N() / cln::factorial(r);
1038 result = result + cln::expt(cln::log(value),s) * (S_num(n-s,p,1).to_cl_N() - res2) / cln::factorial(s);
1045 if (cln::abs(value) > 1) {
1049 for (int s=0; s<p; s++) {
1050 for (int r=0; r<=s; r++) {
1051 result = result + cln::expt(cln::cl_I(-1),s) * cln::expt(cln::log(-value),r) * cln::factorial(n+s-r-1)
1052 / cln::factorial(r) / cln::factorial(s-r) / cln::factorial(n-1)
1053 * S_num(n+s-r,p-s,cln::recip(value)).to_cl_N();
1056 result = result * cln::expt(cln::cl_I(-1),n);
1059 for (int r=0; r<n; r++) {
1060 res2 = res2 + cln::expt(cln::log(-value),r) * C(n-r,p) / cln::factorial(r);
1062 res2 = res2 + cln::expt(cln::log(-value),n+p) / cln::factorial(n+p);
1064 result = result + cln::expt(cln::cl_I(-1),p) * res2;
1069 return S_projection(n, p, value, prec);
1074 } // end of anonymous namespace
1077 //////////////////////////////////////////////////////////////////////
1079 // Nielsen's generalized polylogarithm S(n,p,x)
1083 //////////////////////////////////////////////////////////////////////
1086 static ex S_evalf(const ex& n, const ex& p, const ex& x)
1088 if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
1089 if (is_a<numeric>(x)) {
1090 return S_num(ex_to<numeric>(n).to_int(), ex_to<numeric>(p).to_int(), ex_to<numeric>(x));
1092 ex x_val = x.evalf();
1093 if (is_a<numeric>(x_val)) {
1094 return S_num(ex_to<numeric>(n).to_int(), ex_to<numeric>(p).to_int(), ex_to<numeric>(x_val));
1098 return S(n, p, x).hold();
1102 static ex S_eval(const ex& n, const ex& p, const ex& x)
1104 if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
1110 for (int i=ex_to<numeric>(p).to_int()-1; i>0; i--) {
1118 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
1119 return S_num(ex_to<numeric>(n).to_int(), ex_to<numeric>(p).to_int(), ex_to<numeric>(x));
1124 return pow(-log(1-x), p) / factorial(p);
1126 return S(n, p, x).hold();
1130 static ex S_series(const ex& n, const ex& p, const ex& x, const relational& rel, int order, unsigned options)
1133 seq.push_back(expair(S(n, p, x), 0));
1134 return pseries(rel, seq);
1138 static ex S_deriv(const ex& n, const ex& p, const ex& x, unsigned deriv_param)
1140 GINAC_ASSERT(deriv_param < 3);
1141 if (deriv_param < 2) {
1145 return S(n-1, p, x) / x;
1147 return S(n, p-1, x) / (1-x);
1152 static void S_print_latex(const ex& n, const ex& p, const ex& x, const print_context& c)
1154 c.s << "\\mbox{S}_{";
1164 REGISTER_FUNCTION(S,
1165 evalf_func(S_evalf).
1167 series_func(S_series).
1168 derivative_func(S_deriv).
1169 print_func<print_latex>(S_print_latex).
1170 do_not_evalf_params());
1173 //////////////////////////////////////////////////////////////////////
1175 // Harmonic polylogarithm H(m,x)
1179 //////////////////////////////////////////////////////////////////////
1182 // anonymous namespace for helper functions
1186 // regulates the pole (used by 1/x-transformation)
1187 symbol H_polesign("IMSIGN");
1190 // convert parameters from H to Li representation
1191 // parameters are expected to be in expanded form, i.e. only 0, 1 and -1
1192 // returns true if some parameters are negative
1193 bool convert_parameter_H_to_Li(const lst& l, lst& m, lst& s, ex& pf)
1195 // expand parameter list
1197 for (lst::const_iterator it = l.begin(); it != l.end(); it++) {
1199 for (ex count=*it-1; count > 0; count--) {
1203 } else if (*it < -1) {
1204 for (ex count=*it+1; count < 0; count++) {
1215 bool has_negative_parameters = false;
1217 for (lst::const_iterator it = mexp.begin(); it != mexp.end(); it++) {
1223 m.append((*it+acc-1) * signum);
1225 m.append((*it-acc+1) * signum);
1231 has_negative_parameters = true;
1234 if (has_negative_parameters) {
1235 for (int i=0; i<m.nops(); i++) {
1237 m.let_op(i) = -m.op(i);
1245 return has_negative_parameters;
1249 // recursivly transforms H to corresponding multiple polylogarithms
1250 struct map_trafo_H_convert_to_Li : public map_function
1252 ex operator()(const ex& e)
1254 if (is_a<add>(e) || is_a<mul>(e)) {
1255 return e.map(*this);
1257 if (is_a<function>(e)) {
1258 std::string name = ex_to<function>(e).get_name();
1261 if (is_a<lst>(e.op(0))) {
1262 parameter = ex_to<lst>(e.op(0));
1264 parameter = lst(e.op(0));
1271 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
1272 s.let_op(0) = s.op(0) * arg;
1273 return pf * Li(m, s).hold();
1275 for (int i=0; i<m.nops(); i++) {
1278 s.let_op(0) = s.op(0) * arg;
1279 return Li(m, s).hold();
1288 // recursivly transforms H to corresponding zetas
1289 struct map_trafo_H_convert_to_zeta : public map_function
1291 ex operator()(const ex& e)
1293 if (is_a<add>(e) || is_a<mul>(e)) {
1294 return e.map(*this);
1296 if (is_a<function>(e)) {
1297 std::string name = ex_to<function>(e).get_name();
1300 if (is_a<lst>(e.op(0))) {
1301 parameter = ex_to<lst>(e.op(0));
1303 parameter = lst(e.op(0));
1309 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
1310 return pf * zeta(m, s);
1321 // remove trailing zeros from H-parameters
1322 struct map_trafo_H_reduce_trailing_zeros : public map_function
1324 ex operator()(const ex& e)
1326 if (is_a<add>(e) || is_a<mul>(e)) {
1327 return e.map(*this);
1329 if (is_a<function>(e)) {
1330 std::string name = ex_to<function>(e).get_name();
1333 if (is_a<lst>(e.op(0))) {
1334 parameter = ex_to<lst>(e.op(0));
1336 parameter = lst(e.op(0));
1339 if (parameter.op(parameter.nops()-1) == 0) {
1342 if (parameter.nops() == 1) {
1347 lst::const_iterator it = parameter.begin();
1348 while ((it != parameter.end()) && (*it == 0)) {
1351 if (it == parameter.end()) {
1352 return pow(log(arg),parameter.nops()) / factorial(parameter.nops());
1356 parameter.remove_last();
1357 int lastentry = parameter.nops();
1358 while ((lastentry > 0) && (parameter[lastentry-1] == 0)) {
1363 ex result = log(arg) * H(parameter,arg).hold();
1365 for (ex i=0; i<lastentry; i++) {
1366 if (parameter[i] > 0) {
1368 result -= (acc + parameter[i]-1) * H(parameter, arg).hold();
1371 } else if (parameter[i] < 0) {
1373 result -= (acc + abs(parameter[i]+1)) * H(parameter, arg).hold();
1381 if (lastentry < parameter.nops()) {
1382 result = result / (parameter.nops()-lastentry+1);
1383 return result.map(*this);
1395 // returns an expression with zeta functions corresponding to the parameter list for H
1396 ex convert_H_to_zeta(const lst& m)
1398 symbol xtemp("xtemp");
1399 map_trafo_H_reduce_trailing_zeros filter;
1400 map_trafo_H_convert_to_zeta filter2;
1401 return filter2(filter(H(m, xtemp).hold())).subs(xtemp == 1);
1405 // convert signs form Li to H representation
1406 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf)
1409 lst::const_iterator itm = m.begin();
1410 lst::const_iterator itx = ++x.begin();
1415 while (itx != x.end()) {
1418 res.append((*itm) * signum);
1426 // multiplies an one-dimensional H with another H
1428 ex trafo_H_mult(const ex& h1, const ex& h2)
1433 ex h1nops = h1.op(0).nops();
1434 ex h2nops = h2.op(0).nops();
1436 hshort = h2.op(0).op(0);
1437 hlong = ex_to<lst>(h1.op(0));
1439 hshort = h1.op(0).op(0);
1441 hlong = ex_to<lst>(h2.op(0));
1443 hlong = h2.op(0).op(0);
1446 for (int i=0; i<=hlong.nops(); i++) {
1450 newparameter.append(hlong[j]);
1452 newparameter.append(hshort);
1453 for (; j<hlong.nops(); j++) {
1454 newparameter.append(hlong[j]);
1456 res += H(newparameter, h1.op(1)).hold();
1462 // applies trafo_H_mult recursively on expressions
1463 struct map_trafo_H_mult : public map_function
1465 ex operator()(const ex& e)
1468 return e.map(*this);
1476 for (int pos=0; pos<e.nops(); pos++) {
1477 if (is_a<power>(e.op(pos)) && is_a<function>(e.op(pos).op(0))) {
1478 std::string name = ex_to<function>(e.op(pos).op(0)).get_name();
1480 for (ex i=0; i<e.op(pos).op(1); i++) {
1481 Hlst.append(e.op(pos).op(0));
1485 } else if (is_a<function>(e.op(pos))) {
1486 std::string name = ex_to<function>(e.op(pos)).get_name();
1488 if (e.op(pos).op(0).nops() > 1) {
1491 Hlst.append(e.op(pos));
1496 result *= e.op(pos);
1499 if (Hlst.nops() > 0) {
1500 firstH = Hlst[Hlst.nops()-1];
1507 if (Hlst.nops() > 0) {
1508 ex buffer = trafo_H_mult(firstH, Hlst.op(0));
1510 for (int i=1; i<Hlst.nops(); i++) {
1511 result *= Hlst.op(i);
1513 result = result.expand();
1514 map_trafo_H_mult recursion;
1515 return recursion(result);
1526 // do integration [ReV] (55)
1527 // put parameter 0 in front of existing parameters
1528 ex trafo_H_1tx_prepend_zero(const ex& e, const ex& arg)
1532 if (is_a<function>(e)) {
1533 name = ex_to<function>(e).get_name();
1538 for (int i=0; i<e.nops(); i++) {
1539 if (is_a<function>(e.op(i))) {
1540 std::string name = ex_to<function>(e.op(i)).get_name();
1548 lst newparameter = ex_to<lst>(h.op(0));
1549 newparameter.prepend(0);
1550 ex addzeta = convert_H_to_zeta(newparameter);
1551 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
1553 return e * (-H(lst(0),1/arg).hold());
1558 // do integration [ReV] (55)
1559 // put parameter -1 in front of existing parameters
1560 ex trafo_H_1tx_prepend_minusone(const ex& e, const ex& arg)
1564 if (is_a<function>(e)) {
1565 name = ex_to<function>(e).get_name();
1570 for (int i=0; i<e.nops(); i++) {
1571 if (is_a<function>(e.op(i))) {
1572 std::string name = ex_to<function>(e.op(i)).get_name();
1580 lst newparameter = ex_to<lst>(h.op(0));
1581 newparameter.prepend(-1);
1582 ex addzeta = convert_H_to_zeta(newparameter);
1583 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
1585 ex addzeta = convert_H_to_zeta(lst(-1));
1586 return (e * (addzeta - H(lst(-1),1/arg).hold())).expand();
1591 // do integration [ReV] (55)
1592 // put parameter -1 in front of existing parameters
1593 ex trafo_H_1mxt1px_prepend_minusone(const ex& e, const ex& arg)
1597 if (is_a<function>(e)) {
1598 name = ex_to<function>(e).get_name();
1603 for (int i=0; i<e.nops(); i++) {
1604 if (is_a<function>(e.op(i))) {
1605 std::string name = ex_to<function>(e.op(i)).get_name();
1613 lst newparameter = ex_to<lst>(h.op(0));
1614 newparameter.prepend(-1);
1615 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
1617 return (e * H(lst(-1),(1-arg)/(1+arg)).hold()).expand();
1622 // do integration [ReV] (55)
1623 // put parameter 1 in front of existing parameters
1624 ex trafo_H_1mxt1px_prepend_one(const ex& e, const ex& arg)
1628 if (is_a<function>(e)) {
1629 name = ex_to<function>(e).get_name();
1634 for (int i=0; i<e.nops(); i++) {
1635 if (is_a<function>(e.op(i))) {
1636 std::string name = ex_to<function>(e.op(i)).get_name();
1644 lst newparameter = ex_to<lst>(h.op(0));
1645 newparameter.prepend(1);
1646 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
1648 return (e * H(lst(1),(1-arg)/(1+arg)).hold()).expand();
1653 // do x -> 1/x transformation
1654 struct map_trafo_H_1overx : public map_function
1656 ex operator()(const ex& e)
1658 if (is_a<add>(e) || is_a<mul>(e)) {
1659 return e.map(*this);
1662 if (is_a<function>(e)) {
1663 std::string name = ex_to<function>(e).get_name();
1666 lst parameter = ex_to<lst>(e.op(0));
1669 // special cases if all parameters are either 0, 1 or -1
1670 bool allthesame = true;
1671 if (parameter.op(0) == 0) {
1672 for (int i=1; i<parameter.nops(); i++) {
1673 if (parameter.op(i) != 0) {
1679 return pow(-1, parameter.nops()) * H(parameter, 1/arg).hold();
1681 } else if (parameter.op(0) == -1) {
1682 for (int i=1; i<parameter.nops(); i++) {
1683 if (parameter.op(i) != -1) {
1689 map_trafo_H_mult unify;
1690 return unify((pow(H(lst(-1),1/arg).hold() - H(lst(0),1/arg).hold(), parameter.nops())
1691 / factorial(parameter.nops())).expand());
1694 for (int i=1; i<parameter.nops(); i++) {
1695 if (parameter.op(i) != 1) {
1701 map_trafo_H_mult unify;
1702 return unify((pow(H(lst(1),1/arg).hold() + H(lst(0),1/arg).hold() + H_polesign, parameter.nops())
1703 / factorial(parameter.nops())).expand());
1707 lst newparameter = parameter;
1708 newparameter.remove_first();
1710 if (parameter.op(0) == 0) {
1713 ex res = convert_H_to_zeta(parameter);
1714 map_trafo_H_1overx recursion;
1715 ex buffer = recursion(H(newparameter, arg).hold());
1716 if (is_a<add>(buffer)) {
1717 for (int i=0; i<buffer.nops(); i++) {
1718 res += trafo_H_1tx_prepend_zero(buffer.op(i), arg);
1721 res += trafo_H_1tx_prepend_zero(buffer, arg);
1725 } else if (parameter.op(0) == -1) {
1727 // leading negative one
1728 ex res = convert_H_to_zeta(parameter);
1729 map_trafo_H_1overx recursion;
1730 ex buffer = recursion(H(newparameter, arg).hold());
1731 if (is_a<add>(buffer)) {
1732 for (int i=0; i<buffer.nops(); i++) {
1733 res += trafo_H_1tx_prepend_zero(buffer.op(i), arg) - trafo_H_1tx_prepend_minusone(buffer.op(i), arg);
1736 res += trafo_H_1tx_prepend_zero(buffer, arg) - trafo_H_1tx_prepend_minusone(buffer, arg);
1743 map_trafo_H_1overx recursion;
1744 map_trafo_H_mult unify;
1745 ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
1747 while (parameter.op(firstzero) == 1) {
1750 for (int i=firstzero-1; i<parameter.nops()-1; i++) {
1754 newparameter.append(parameter[j+1]);
1756 newparameter.append(1);
1757 for (; j<parameter.nops()-1; j++) {
1758 newparameter.append(parameter[j+1]);
1760 res -= H(newparameter, arg).hold();
1762 res = recursion(res).expand() / firstzero;
1774 // do x -> (1-x)/(1+x) transformation
1775 struct map_trafo_H_1mxt1px : public map_function
1777 ex operator()(const ex& e)
1779 if (is_a<add>(e) || is_a<mul>(e)) {
1780 return e.map(*this);
1783 if (is_a<function>(e)) {
1784 std::string name = ex_to<function>(e).get_name();
1787 lst parameter = ex_to<lst>(e.op(0));
1790 // special cases if all parameters are either 0, 1 or -1
1791 bool allthesame = true;
1792 if (parameter.op(0) == 0) {
1793 for (int i=1; i<parameter.nops(); i++) {
1794 if (parameter.op(i) != 0) {
1800 map_trafo_H_mult unify;
1801 return unify((pow(-H(lst(1),(1-arg)/(1+arg)).hold() - H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
1802 / factorial(parameter.nops())).expand());
1804 } else if (parameter.op(0) == -1) {
1805 for (int i=1; i<parameter.nops(); i++) {
1806 if (parameter.op(i) != -1) {
1812 map_trafo_H_mult unify;
1813 return unify((pow(log(2) - H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
1814 / factorial(parameter.nops())).expand());
1817 for (int i=1; i<parameter.nops(); i++) {
1818 if (parameter.op(i) != 1) {
1824 map_trafo_H_mult unify;
1825 return unify((pow(-log(2) - H(lst(0),(1-arg)/(1+arg)).hold() + H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
1826 / factorial(parameter.nops())).expand());
1830 lst newparameter = parameter;
1831 newparameter.remove_first();
1833 if (parameter.op(0) == 0) {
1836 ex res = convert_H_to_zeta(parameter);
1837 map_trafo_H_1mxt1px recursion;
1838 ex buffer = recursion(H(newparameter, arg).hold());
1839 if (is_a<add>(buffer)) {
1840 for (int i=0; i<buffer.nops(); i++) {
1841 res -= trafo_H_1mxt1px_prepend_one(buffer.op(i), arg) + trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
1844 res -= trafo_H_1mxt1px_prepend_one(buffer, arg) + trafo_H_1mxt1px_prepend_minusone(buffer, arg);
1848 } else if (parameter.op(0) == -1) {
1850 // leading negative one
1851 ex res = convert_H_to_zeta(parameter);
1852 map_trafo_H_1mxt1px recursion;
1853 ex buffer = recursion(H(newparameter, arg).hold());
1854 if (is_a<add>(buffer)) {
1855 for (int i=0; i<buffer.nops(); i++) {
1856 res -= trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
1859 res -= trafo_H_1mxt1px_prepend_minusone(buffer, arg);
1866 map_trafo_H_1mxt1px recursion;
1867 map_trafo_H_mult unify;
1868 ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
1870 while (parameter.op(firstzero) == 1) {
1873 for (int i=firstzero-1; i<parameter.nops()-1; i++) {
1877 newparameter.append(parameter[j+1]);
1879 newparameter.append(1);
1880 for (; j<parameter.nops()-1; j++) {
1881 newparameter.append(parameter[j+1]);
1883 res -= H(newparameter, arg).hold();
1885 res = recursion(res).expand() / firstzero;
1897 // do the actual summation.
1898 cln::cl_N H_do_sum(const std::vector<int>& m, const cln::cl_N& x)
1900 const int j = m.size();
1902 std::vector<cln::cl_N> t(j);
1904 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
1905 cln::cl_N factor = cln::expt(x, j) * one;
1911 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),m[j-1]);
1912 for (int k=j-2; k>=1; k--) {
1913 t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), m[k]);
1915 t[0] = t[0] + t[1] * factor / cln::expt(cln::cl_I(q+j-1), m[0]);
1916 factor = factor * x;
1917 } while (t[0] != t0buf);
1923 } // end of anonymous namespace
1926 //////////////////////////////////////////////////////////////////////
1928 // Harmonic polylogarithm H(m,x)
1932 //////////////////////////////////////////////////////////////////////
1935 static ex H_evalf(const ex& x1, const ex& x2)
1937 if (is_a<lst>(x1)) {
1940 if (is_a<numeric>(x2)) {
1941 x = ex_to<numeric>(x2).to_cl_N();
1943 ex x2_val = x2.evalf();
1944 if (is_a<numeric>(x2_val)) {
1945 x = ex_to<numeric>(x2_val).to_cl_N();
1949 for (int i=0; i<x1.nops(); i++) {
1950 if (!x1.op(i).info(info_flags::integer)) {
1951 return H(x1, x2).hold();
1954 if (x1.nops() < 1) {
1955 return H(x1, x2).hold();
1958 const lst& morg = ex_to<lst>(x1);
1959 // remove trailing zeros ...
1960 if (*(--morg.end()) == 0) {
1961 symbol xtemp("xtemp");
1962 map_trafo_H_reduce_trailing_zeros filter;
1963 return filter(H(x1, xtemp).hold()).subs(xtemp==x2).evalf();
1965 // ... and expand parameter notation
1967 for (lst::const_iterator it = morg.begin(); it != morg.end(); it++) {
1969 for (ex count=*it-1; count > 0; count--) {
1973 } else if (*it < -1) {
1974 for (ex count=*it+1; count < 0; count++) {
1983 // since the transformations produce a lot of terms, they are only efficient for
1984 // argument near one.
1985 // no transformation needed -> do summation
1986 if (cln::abs(x) < 0.95) {
1990 if (convert_parameter_H_to_Li(m, m_lst, s_lst, pf)) {
1991 // negative parameters -> s_lst is filled
1992 std::vector<int> m_int;
1993 std::vector<cln::cl_N> x_cln;
1994 for (lst::const_iterator it_int = m_lst.begin(), it_cln = s_lst.begin();
1995 it_int != m_lst.end(); it_int++, it_cln++) {
1996 m_int.push_back(ex_to<numeric>(*it_int).to_int());
1997 x_cln.push_back(ex_to<numeric>(*it_cln).to_cl_N());
1999 x_cln.front() = x_cln.front() * x;
2000 return pf * numeric(multipleLi_do_sum(m_int, x_cln));
2002 // only positive parameters
2004 if (m_lst.nops() == 1) {
2005 return Li(m_lst.op(0), x2).evalf();
2007 std::vector<int> m_int;
2008 for (lst::const_iterator it = m_lst.begin(); it != m_lst.end(); it++) {
2009 m_int.push_back(ex_to<numeric>(*it).to_int());
2011 return numeric(H_do_sum(m_int, x));
2017 // ensure that the realpart of the argument is positive
2018 if (cln::realpart(x) < 0) {
2020 for (int i=0; i<m.nops(); i++) {
2022 m.let_op(i) = -m.op(i);
2028 // choose transformations
2029 symbol xtemp("xtemp");
2030 if (cln::abs(x-1) < 1.4142) {
2032 map_trafo_H_1mxt1px trafo;
2033 res *= trafo(H(m, xtemp));
2036 map_trafo_H_1overx trafo;
2037 res *= trafo(H(m, xtemp));
2038 if (cln::imagpart(x) <= 0) {
2039 res = res.subs(H_polesign == -I*Pi);
2041 res = res.subs(H_polesign == I*Pi);
2047 // map_trafo_H_convert converter;
2048 // res = converter(res).expand();
2050 // res.find(H(wild(1),wild(2)), ll);
2051 // res.find(zeta(wild(1)), ll);
2052 // res.find(zeta(wild(1),wild(2)), ll);
2053 // res = res.collect(ll);
2055 return res.subs(xtemp == numeric(x)).evalf();
2058 return H(x1,x2).hold();
2062 static ex H_eval(const ex& m_, const ex& x)
2065 if (is_a<lst>(m_)) {
2070 if (m.nops() == 0) {
2078 if (*m.begin() > _ex1) {
2084 } else if (*m.begin() < _ex_1) {
2090 } else if (*m.begin() == _ex0) {
2097 for (lst::const_iterator it = ++m.begin(); it != m.end(); it++) {
2098 if ((*it).info(info_flags::integer)) {
2109 } else if (*it < _ex_1) {
2129 } else if (step == 1) {
2141 // if some m_i is not an integer
2142 return H(m_, x).hold();
2145 if ((x == _ex1) && (*(--m.end()) != _ex0)) {
2146 return convert_H_to_zeta(m);
2152 return H(m_, x).hold();
2154 return pow(log(x), m.nops()) / factorial(m.nops());
2157 return pow(-pos1*log(1-pos1*x), m.nops()) / factorial(m.nops());
2159 } else if ((step == 1) && (pos1 == _ex0)){
2164 return pow(-1, p) * S(n, p, -x);
2170 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
2171 return H(m_, x).evalf();
2173 return H(m_, x).hold();
2177 static ex H_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
2180 seq.push_back(expair(H(m, x), 0));
2181 return pseries(rel, seq);
2185 static ex H_deriv(const ex& m_, const ex& x, unsigned deriv_param)
2187 GINAC_ASSERT(deriv_param < 2);
2188 if (deriv_param == 0) {
2192 if (is_a<lst>(m_)) {
2208 return 1/(1-x) * H(m, x);
2209 } else if (mb == _ex_1) {
2210 return 1/(1+x) * H(m, x);
2217 static void H_print_latex(const ex& m_, const ex& x, const print_context& c)
2220 if (is_a<lst>(m_)) {
2225 c.s << "\\mbox{H}_{";
2226 lst::const_iterator itm = m.begin();
2229 for (; itm != m.end(); itm++) {
2239 REGISTER_FUNCTION(H,
2240 evalf_func(H_evalf).
2242 series_func(H_series).
2243 derivative_func(H_deriv).
2244 print_func<print_latex>(H_print_latex).
2245 do_not_evalf_params());
2248 // takes a parameter list for H and returns an expression with corresponding multiple polylogarithms
2249 ex convert_H_to_Li(const ex& m, const ex& x)
2251 map_trafo_H_reduce_trailing_zeros filter;
2252 map_trafo_H_convert_to_Li filter2;
2254 return filter2(filter(H(m, x).hold()));
2256 return filter2(filter(H(lst(m), x).hold()));
2261 //////////////////////////////////////////////////////////////////////
2263 // Multiple zeta values zeta(x) and zeta(x,s)
2267 //////////////////////////////////////////////////////////////////////
2270 // anonymous namespace for helper functions
2274 // parameters and data for [Cra] algorithm
2275 const cln::cl_N lambda = cln::cl_N("319/320");
2278 std::vector<std::vector<cln::cl_N> > f_kj;
2279 std::vector<cln::cl_N> crB;
2280 std::vector<std::vector<cln::cl_N> > crG;
2281 std::vector<cln::cl_N> crX;
2284 void halfcyclic_convolute(const std::vector<cln::cl_N>& a, const std::vector<cln::cl_N>& b, std::vector<cln::cl_N>& c)
2286 const int size = a.size();
2287 for (int n=0; n<size; n++) {
2289 for (int m=0; m<=n; m++) {
2290 c[n] = c[n] + a[m]*b[n-m];
2297 void initcX(const std::vector<int>& s)
2299 const int k = s.size();
2305 for (int i=0; i<=L2; i++) {
2306 crB.push_back(bernoulli(i).to_cl_N() / cln::factorial(i));
2311 for (int m=0; m<k-1; m++) {
2312 std::vector<cln::cl_N> crGbuf;
2315 for (int i=0; i<=L2; i++) {
2316 crGbuf.push_back(cln::factorial(i + Sm - m - 2) / cln::factorial(i + Smp1 - m - 2));
2318 crG.push_back(crGbuf);
2323 for (int m=0; m<k-1; m++) {
2324 std::vector<cln::cl_N> Xbuf;
2325 for (int i=0; i<=L2; i++) {
2326 Xbuf.push_back(crX[i] * crG[m][i]);
2328 halfcyclic_convolute(Xbuf, crB, crX);
2334 cln::cl_N crandall_Y_loop(const cln::cl_N& Sqk)
2336 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
2337 cln::cl_N factor = cln::expt(lambda, Sqk);
2338 cln::cl_N res = factor / Sqk * crX[0] * one;
2343 factor = factor * lambda;
2345 res = res + crX[N] * factor / (N+Sqk);
2346 } while ((res != resbuf) || cln::zerop(crX[N]));
2352 void calc_f(int maxr)
2357 cln::cl_N t0, t1, t2, t3, t4;
2359 std::vector<std::vector<cln::cl_N> >::iterator it = f_kj.begin();
2360 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
2362 t0 = cln::exp(-lambda);
2364 for (k=1; k<=L1; k++) {
2367 for (j=1; j<=maxr; j++) {
2370 for (i=2; i<=j; i++) {
2374 (*it).push_back(t2 * t3 * cln::expt(cln::cl_I(k),-j) * one);
2382 cln::cl_N crandall_Z(const std::vector<int>& s)
2384 const int j = s.size();
2393 t0 = t0 + f_kj[q+j-2][s[0]-1];
2394 } while (t0 != t0buf);
2396 return t0 / cln::factorial(s[0]-1);
2399 std::vector<cln::cl_N> t(j);
2406 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),s[j-1]);
2407 for (int k=j-2; k>=1; k--) {
2408 t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), s[k]);
2410 t[0] = t[0] + t[1] * f_kj[q+j-2][s[0]-1];
2411 } while (t[0] != t0buf);
2413 return t[0] / cln::factorial(s[0]-1);
2418 cln::cl_N zeta_do_sum_Crandall(const std::vector<int>& s)
2420 std::vector<int> r = s;
2421 const int j = r.size();
2423 // decide on maximal size of f_kj for crandall_Z
2427 L1 = Digits * 3 + j*2;
2430 // decide on maximal size of crX for crandall_Y
2433 } else if (Digits < 86) {
2435 } else if (Digits < 192) {
2437 } else if (Digits < 394) {
2439 } else if (Digits < 808) {
2449 for (int i=0; i<j; i++) {
2458 const cln::cl_N r0factorial = cln::factorial(r[0]-1);
2460 std::vector<int> rz;
2463 for (int k=r.size()-1; k>0; k--) {
2465 rz.insert(rz.begin(), r.back());
2466 skp1buf = rz.front();
2472 for (int q=0; q<skp1buf; q++) {
2474 cln::cl_N pp1 = crandall_Y_loop(Srun+q-k);
2475 cln::cl_N pp2 = crandall_Z(rz);
2480 res = res - pp1 * pp2 / cln::factorial(q);
2482 res = res + pp1 * pp2 / cln::factorial(q);
2485 rz.front() = skp1buf;
2487 rz.insert(rz.begin(), r.back());
2491 res = (res + crandall_Y_loop(S-j)) / r0factorial + crandall_Z(rz);
2497 cln::cl_N zeta_do_sum_simple(const std::vector<int>& r)
2499 const int j = r.size();
2501 // buffer for subsums
2502 std::vector<cln::cl_N> t(j);
2503 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
2510 t[j-1] = t[j-1] + one / cln::expt(cln::cl_I(q),r[j-1]);
2511 for (int k=j-2; k>=0; k--) {
2512 t[k] = t[k] + one * t[k+1] / cln::expt(cln::cl_I(q+j-1-k), r[k]);
2514 } while (t[0] != t0buf);
2520 // does Hoelder convolution. see [BBB] (7.0)
2521 cln::cl_N zeta_do_Hoelder_convolution(const std::vector<int>& m_, const std::vector<int>& s_)
2523 // prepare parameters
2524 // holds Li arguments in [BBB] notation
2525 std::vector<int> s = s_;
2526 std::vector<int> m_p = m_;
2527 std::vector<int> m_q;
2528 // holds Li arguments in nested sums notation
2529 std::vector<cln::cl_N> s_p(s.size(), cln::cl_N(1));
2530 s_p[0] = s_p[0] * cln::cl_N("1/2");
2531 // convert notations
2533 for (int i=0; i<s_.size(); i++) {
2538 s[i] = sig * std::abs(s[i]);
2540 std::vector<cln::cl_N> s_q;
2541 cln::cl_N signum = 1;
2544 cln::cl_N res = multipleLi_do_sum(m_p, s_p);
2549 // change parameters
2550 if (s.front() > 0) {
2551 if (m_p.front() == 1) {
2552 m_p.erase(m_p.begin());
2553 s_p.erase(s_p.begin());
2554 if (s_p.size() > 0) {
2555 s_p.front() = s_p.front() * cln::cl_N("1/2");
2561 m_q.insert(m_q.begin(), 1);
2562 if (s_q.size() > 0) {
2563 s_q.front() = s_q.front() * 2;
2565 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
2568 if (m_p.front() == 1) {
2569 m_p.erase(m_p.begin());
2570 cln::cl_N spbuf = s_p.front();
2571 s_p.erase(s_p.begin());
2572 if (s_p.size() > 0) {
2573 s_p.front() = s_p.front() * spbuf;
2576 m_q.insert(m_q.begin(), 1);
2577 if (s_q.size() > 0) {
2578 s_q.front() = s_q.front() * 4;
2580 s_q.insert(s_q.begin(), cln::cl_N("1/4"));
2584 m_q.insert(m_q.begin(), 1);
2585 if (s_q.size() > 0) {
2586 s_q.front() = s_q.front() * 2;
2588 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
2593 if (m_p.size() == 0) break;
2595 res = res + signum * multipleLi_do_sum(m_p, s_p) * multipleLi_do_sum(m_q, s_q);
2600 res = res + signum * multipleLi_do_sum(m_q, s_q);
2606 } // end of anonymous namespace
2609 //////////////////////////////////////////////////////////////////////
2611 // Multiple zeta values zeta(x)
2615 //////////////////////////////////////////////////////////////////////
2618 static ex zeta1_evalf(const ex& x)
2620 if (is_exactly_a<lst>(x) && (x.nops()>1)) {
2622 // multiple zeta value
2623 const int count = x.nops();
2624 const lst& xlst = ex_to<lst>(x);
2625 std::vector<int> r(count);
2627 // check parameters and convert them
2628 lst::const_iterator it1 = xlst.begin();
2629 std::vector<int>::iterator it2 = r.begin();
2631 if (!(*it1).info(info_flags::posint)) {
2632 return zeta(x).hold();
2634 *it2 = ex_to<numeric>(*it1).to_int();
2637 } while (it2 != r.end());
2639 // check for divergence
2641 return zeta(x).hold();
2644 // decide on summation algorithm
2645 // this is still a bit clumsy
2646 int limit = (Digits>17) ? 10 : 6;
2647 if ((r[0] < limit) || ((count > 3) && (r[1] < limit/2))) {
2648 return numeric(zeta_do_sum_Crandall(r));
2650 return numeric(zeta_do_sum_simple(r));
2654 // single zeta value
2655 if (is_exactly_a<numeric>(x) && (x != 1)) {
2657 return zeta(ex_to<numeric>(x));
2658 } catch (const dunno &e) { }
2661 return zeta(x).hold();
2665 static ex zeta1_eval(const ex& m)
2667 if (is_exactly_a<lst>(m)) {
2668 if (m.nops() == 1) {
2669 return zeta(m.op(0));
2671 return zeta(m).hold();
2674 if (m.info(info_flags::numeric)) {
2675 const numeric& y = ex_to<numeric>(m);
2676 // trap integer arguments:
2677 if (y.is_integer()) {
2681 if (y.is_equal(_num1)) {
2682 return zeta(m).hold();
2684 if (y.info(info_flags::posint)) {
2685 if (y.info(info_flags::odd)) {
2686 return zeta(m).hold();
2688 return abs(bernoulli(y)) * pow(Pi, y) * pow(_num2, y-_num1) / factorial(y);
2691 if (y.info(info_flags::odd)) {
2692 return -bernoulli(_num1-y) / (_num1-y);
2699 if (y.info(info_flags::numeric) && !y.info(info_flags::crational)) {
2700 return zeta1_evalf(m);
2703 return zeta(m).hold();
2707 static ex zeta1_deriv(const ex& m, unsigned deriv_param)
2709 GINAC_ASSERT(deriv_param==0);
2711 if (is_exactly_a<lst>(m)) {
2714 return zetaderiv(_ex1, m);
2719 static void zeta1_print_latex(const ex& m_, const print_context& c)
2722 if (is_a<lst>(m_)) {
2723 const lst& m = ex_to<lst>(m_);
2724 lst::const_iterator it = m.begin();
2727 for (; it != m.end(); it++) {
2738 unsigned zeta1_SERIAL::serial = function::register_new(function_options("zeta", 1).
2739 evalf_func(zeta1_evalf).
2740 eval_func(zeta1_eval).
2741 derivative_func(zeta1_deriv).
2742 print_func<print_latex>(zeta1_print_latex).
2743 do_not_evalf_params().
2747 //////////////////////////////////////////////////////////////////////
2749 // Alternating Euler sum zeta(x,s)
2753 //////////////////////////////////////////////////////////////////////
2756 static ex zeta2_evalf(const ex& x, const ex& s)
2758 if (is_exactly_a<lst>(x)) {
2760 // alternating Euler sum
2761 const int count = x.nops();
2762 const lst& xlst = ex_to<lst>(x);
2763 const lst& slst = ex_to<lst>(s);
2764 std::vector<int> xi(count);
2765 std::vector<int> si(count);
2767 // check parameters and convert them
2768 lst::const_iterator it_xread = xlst.begin();
2769 lst::const_iterator it_sread = slst.begin();
2770 std::vector<int>::iterator it_xwrite = xi.begin();
2771 std::vector<int>::iterator it_swrite = si.begin();
2773 if (!(*it_xread).info(info_flags::posint)) {
2774 return zeta(x, s).hold();
2776 *it_xwrite = ex_to<numeric>(*it_xread).to_int();
2777 if (*it_sread > 0) {
2786 } while (it_xwrite != xi.end());
2788 // check for divergence
2789 if ((xi[0] == 1) && (si[0] == 1)) {
2790 return zeta(x, s).hold();
2793 // use Hoelder convolution
2794 return numeric(zeta_do_Hoelder_convolution(xi, si));
2797 return zeta(x, s).hold();
2801 static ex zeta2_eval(const ex& m, const ex& s_)
2803 if (is_exactly_a<lst>(s_)) {
2804 const lst& s = ex_to<lst>(s_);
2805 for (lst::const_iterator it = s.begin(); it != s.end(); it++) {
2806 if ((*it).info(info_flags::positive)) {
2809 return zeta(m, s_).hold();
2812 } else if (s_.info(info_flags::positive)) {
2816 return zeta(m, s_).hold();
2820 static ex zeta2_deriv(const ex& m, const ex& s, unsigned deriv_param)
2822 GINAC_ASSERT(deriv_param==0);
2824 if (is_exactly_a<lst>(m)) {
2827 if ((is_exactly_a<lst>(s) && s.op(0).info(info_flags::positive)) || s.info(info_flags::positive)) {
2828 return zetaderiv(_ex1, m);
2835 static void zeta2_print_latex(const ex& m_, const ex& s_, const print_context& c)
2838 if (is_a<lst>(m_)) {
2844 if (is_a<lst>(s_)) {
2850 lst::const_iterator itm = m.begin();
2851 lst::const_iterator its = s.begin();
2853 c.s << "\\overline{";
2861 for (; itm != m.end(); itm++, its++) {
2864 c.s << "\\overline{";
2875 unsigned zeta2_SERIAL::serial = function::register_new(function_options("zeta", 2).
2876 evalf_func(zeta2_evalf).
2877 eval_func(zeta2_eval).
2878 derivative_func(zeta2_deriv).
2879 print_func<print_latex>(zeta2_print_latex).
2880 do_not_evalf_params().
2884 } // namespace GiNaC