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 * G(lst(a_1,...,a_k),y) or G(lst(a_1,...,a_k),lst(s_1,...,s_k),y)
9 * Nielsen's generalized polylogarithm S(n,p,x)
10 * harmonic polylogarithm H(m,x) or H(lst(m_1,...,m_k),x)
11 * multiple zeta value zeta(m) or zeta(lst(m_1,...,m_k))
12 * alternating Euler sum zeta(m,s) or zeta(lst(m_1,...,m_k),lst(s_1,...,s_k))
16 * - All formulae used can be looked up in the following publications:
17 * [Kol] Nielsen's Generalized Polylogarithms, K.S.Kolbig, SIAM J.Math.Anal. 17 (1986), pp. 1232-1258.
18 * [Cra] Fast Evaluation of Multiple Zeta Sums, R.E.Crandall, Math.Comp. 67 (1998), pp. 1163-1172.
19 * [ReV] Harmonic Polylogarithms, E.Remiddi, J.A.M.Vermaseren, Int.J.Mod.Phys. A15 (2000), pp. 725-754
20 * [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 * [VSW] Numerical evaluation of multiple polylogarithms, J.Vollinga, S.Weinzierl, hep-ph/0410259
23 * - The order of parameters and arguments of Li and zeta is defined according to the nested sums
24 * representation. The parameters for H are understood as in [ReV]. They can be in expanded --- only
25 * 0, 1 and -1 --- or in compactified --- a string with zeros in front of 1 or -1 is written as a single
26 * number --- notation.
28 * - All functions can be nummerically evaluated with arguments in the whole complex plane. The parameters
29 * for Li, zeta and S must be positive integers. If you want to have an alternating Euler sum, you have
30 * to give the signs of the parameters as a 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. Multiple polylogarithms use Hoelder convolution [BBB].
36 * - The functions have no means to do a series expansion into nested sums. To do this, you have to convert
37 * these functions into the appropriate objects from the nestedsums library, do the expansion and convert
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. Multiple polylogarithms were
45 * checked against H and zeta and by means of shuffle and quasi-shuffle relations.
50 * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
52 * This program is free software; you can redistribute it and/or modify
53 * it under the terms of the GNU General Public License as published by
54 * the Free Software Foundation; either version 2 of the License, or
55 * (at your option) any later version.
57 * This program is distributed in the hope that it will be useful,
58 * but WITHOUT ANY WARRANTY; without even the implied warranty of
59 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60 * GNU General Public License for more details.
62 * You should have received a copy of the GNU General Public License
63 * along with this program; if not, write to the Free Software
64 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
79 #include "operators.h"
82 #include "relational.h"
91 //////////////////////////////////////////////////////////////////////
93 // Classical polylogarithm Li(n,x)
97 //////////////////////////////////////////////////////////////////////
100 // anonymous namespace for helper functions
104 // lookup table for factors built from Bernoulli numbers
106 std::vector<std::vector<cln::cl_N> > Xn;
107 // initial size of Xn that should suffice for 32bit machines (must be even)
108 const int xninitsizestep = 26;
109 int xninitsize = xninitsizestep;
113 // This function calculates the X_n. The X_n are needed for speed up of classical polylogarithms.
114 // With these numbers the polylogs can be calculated as follows:
115 // Li_p (x) = \sum_{n=0}^\infty X_{p-2}(n) u^{n+1}/(n+1)! with u = -log(1-x)
116 // X_0(n) = B_n (Bernoulli numbers)
117 // X_p(n) = \sum_{k=0}^n binomial(n,k) B_{n-k} / (k+1) * X_{p-1}(k)
118 // The calculation of Xn depends on X0 and X{n-1}.
119 // X_0 is special, it holds only the non-zero Bernoulli numbers with index 2 or greater.
120 // This results in a slightly more complicated algorithm for the X_n.
121 // The first index in Xn corresponds to the index of the polylog minus 2.
122 // The second index in Xn corresponds to the index from the actual sum.
126 // calculate X_2 and higher (corresponding to Li_4 and higher)
127 std::vector<cln::cl_N> buf(xninitsize);
128 std::vector<cln::cl_N>::iterator it = buf.begin();
130 *it = -(cln::expt(cln::cl_I(2),n+1) - 1) / cln::expt(cln::cl_I(2),n+1); // i == 1
132 for (int i=2; i<=xninitsize; i++) {
134 result = 0; // k == 0
136 result = Xn[0][i/2-1]; // k == 0
138 for (int k=1; k<i-1; k++) {
139 if ( !(((i-k) & 1) && ((i-k) > 1)) ) {
140 result = result + cln::binomial(i,k) * Xn[0][(i-k)/2-1] * Xn[n-1][k-1] / (k+1);
143 result = result - cln::binomial(i,i-1) * Xn[n-1][i-2] / 2 / i; // k == i-1
144 result = result + Xn[n-1][i-1] / (i+1); // k == i
151 // special case to handle the X_0 correct
152 std::vector<cln::cl_N> buf(xninitsize);
153 std::vector<cln::cl_N>::iterator it = buf.begin();
155 *it = cln::cl_I(-3)/cln::cl_I(4); // i == 1
157 *it = cln::cl_I(17)/cln::cl_I(36); // i == 2
159 for (int i=3; i<=xninitsize; i++) {
161 result = -Xn[0][(i-3)/2]/2;
162 *it = (cln::binomial(i,1)/cln::cl_I(2) + cln::binomial(i,i-1)/cln::cl_I(i))*result;
165 result = Xn[0][i/2-1] + Xn[0][i/2-1]/(i+1);
166 for (int k=1; k<i/2; k++) {
167 result = result + cln::binomial(i,k*2) * Xn[0][k-1] * Xn[0][i/2-k-1] / (k*2+1);
176 std::vector<cln::cl_N> buf(xninitsize/2);
177 std::vector<cln::cl_N>::iterator it = buf.begin();
178 for (int i=1; i<=xninitsize/2; i++) {
179 *it = bernoulli(i*2).to_cl_N();
188 // doubles the number of entries in each Xn[]
191 const int pos0 = xninitsize / 2;
193 for (int i=1; i<=xninitsizestep/2; ++i) {
194 Xn[0].push_back(bernoulli((i+pos0)*2).to_cl_N());
197 int xend = xninitsize + xninitsizestep;
200 for (int i=xninitsize+1; i<=xend; ++i) {
202 result = -Xn[0][(i-3)/2]/2;
203 Xn[1].push_back((cln::binomial(i,1)/cln::cl_I(2) + cln::binomial(i,i-1)/cln::cl_I(i))*result);
205 result = Xn[0][i/2-1] + Xn[0][i/2-1]/(i+1);
206 for (int k=1; k<i/2; k++) {
207 result = result + cln::binomial(i,k*2) * Xn[0][k-1] * Xn[0][i/2-k-1] / (k*2+1);
209 Xn[1].push_back(result);
213 for (int n=2; n<Xn.size(); ++n) {
214 for (int i=xninitsize+1; i<=xend; ++i) {
216 result = 0; // k == 0
218 result = Xn[0][i/2-1]; // k == 0
220 for (int k=1; k<i-1; ++k) {
221 if ( !(((i-k) & 1) && ((i-k) > 1)) ) {
222 result = result + cln::binomial(i,k) * Xn[0][(i-k)/2-1] * Xn[n-1][k-1] / (k+1);
225 result = result - cln::binomial(i,i-1) * Xn[n-1][i-2] / 2 / i; // k == i-1
226 result = result + Xn[n-1][i-1] / (i+1); // k == i
227 Xn[n].push_back(result);
231 xninitsize += xninitsizestep;
235 // calculates Li(2,x) without Xn
236 cln::cl_N Li2_do_sum(const cln::cl_N& x)
240 cln::cl_N num = x * cln::cl_float(1, cln::float_format(Digits));
241 cln::cl_I den = 1; // n^2 = 1
246 den = den + i; // n^2 = 4, 9, 16, ...
248 res = res + num / den;
249 } while (res != resbuf);
254 // calculates Li(2,x) with Xn
255 cln::cl_N Li2_do_sum_Xn(const cln::cl_N& x)
257 std::vector<cln::cl_N>::const_iterator it = Xn[0].begin();
258 std::vector<cln::cl_N>::const_iterator xend = Xn[0].end();
259 cln::cl_N u = -cln::log(1-x);
260 cln::cl_N factor = u * cln::cl_float(1, cln::float_format(Digits));
261 cln::cl_N uu = cln::square(u);
262 cln::cl_N res = u - uu/4;
267 factor = factor * uu / (2*i * (2*i+1));
268 res = res + (*it) * factor;
272 it = Xn[0].begin() + (i-1);
275 } while (res != resbuf);
280 // calculates Li(n,x), n>2 without Xn
281 cln::cl_N Lin_do_sum(int n, const cln::cl_N& x)
283 cln::cl_N factor = x * cln::cl_float(1, cln::float_format(Digits));
290 res = res + factor / cln::expt(cln::cl_I(i),n);
292 } while (res != resbuf);
297 // calculates Li(n,x), n>2 with Xn
298 cln::cl_N Lin_do_sum_Xn(int n, const cln::cl_N& x)
300 std::vector<cln::cl_N>::const_iterator it = Xn[n-2].begin();
301 std::vector<cln::cl_N>::const_iterator xend = Xn[n-2].end();
302 cln::cl_N u = -cln::log(1-x);
303 cln::cl_N factor = u * cln::cl_float(1, cln::float_format(Digits));
309 factor = factor * u / i;
310 res = res + (*it) * factor;
314 it = Xn[n-2].begin() + (i-2);
315 xend = Xn[n-2].end();
317 } while (res != resbuf);
322 // forward declaration needed by function Li_projection and C below
323 numeric S_num(int n, int p, const numeric& x);
326 // helper function for classical polylog Li
327 cln::cl_N Li_projection(int n, const cln::cl_N& x, const cln::float_format_t& prec)
329 // treat n=2 as special case
331 // check if precalculated X0 exists
336 if (cln::realpart(x) < 0.5) {
337 // choose the faster algorithm
338 // the switching point was empirically determined. the optimal point
339 // depends on hardware, Digits, ... so an approx value is okay.
340 // it solves also the problem with precision due to the u=-log(1-x) transformation
341 if (cln::abs(cln::realpart(x)) < 0.25) {
343 return Li2_do_sum(x);
345 return Li2_do_sum_Xn(x);
348 // choose the faster algorithm
349 if (cln::abs(cln::realpart(x)) > 0.75) {
350 return -Li2_do_sum(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
352 return -Li2_do_sum_Xn(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
356 // check if precalculated Xn exist
358 for (int i=xnsize; i<n-1; i++) {
363 if (cln::realpart(x) < 0.5) {
364 // choose the faster algorithm
365 // with n>=12 the "normal" summation always wins against the method with Xn
366 if ((cln::abs(cln::realpart(x)) < 0.3) || (n >= 12)) {
367 return Lin_do_sum(n, x);
369 return Lin_do_sum_Xn(n, x);
372 cln::cl_N result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
373 for (int j=0; j<n-1; j++) {
374 result = result + (S_num(n-j-1, 1, 1).to_cl_N() - S_num(1, n-j-1, 1-x).to_cl_N())
375 * cln::expt(cln::log(x), j) / cln::factorial(j);
383 // helper function for classical polylog Li
384 numeric Lin_numeric(int n, const numeric& x)
388 return -cln::log(1-x.to_cl_N());
399 return -(1-cln::expt(cln::cl_I(2),1-n)) * cln::zeta(n);
401 if (abs(x.real()) < 0.4 && abs(abs(x)-1) < 0.01) {
402 cln::cl_N x_ = ex_to<numeric>(x).to_cl_N();
403 cln::cl_N result = -cln::expt(cln::log(x_), n-1) * cln::log(1-x_) / cln::factorial(n-1);
404 for (int j=0; j<n-1; j++) {
405 result = result + (S_num(n-j-1, 1, 1).to_cl_N() - S_num(1, n-j-1, 1-x_).to_cl_N())
406 * cln::expt(cln::log(x_), j) / cln::factorial(j);
411 // what is the desired float format?
412 // first guess: default format
413 cln::float_format_t prec = cln::default_float_format;
414 const cln::cl_N value = x.to_cl_N();
415 // second guess: the argument's format
416 if (!x.real().is_rational())
417 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
418 else if (!x.imag().is_rational())
419 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
422 if (cln::abs(value) > 1) {
423 cln::cl_N result = -cln::expt(cln::log(-value),n) / cln::factorial(n);
424 // check if argument is complex. if it is real, the new polylog has to be conjugated.
425 if (cln::zerop(cln::imagpart(value))) {
427 result = result + conjugate(Li_projection(n, cln::recip(value), prec));
430 result = result - conjugate(Li_projection(n, cln::recip(value), prec));
435 result = result + Li_projection(n, cln::recip(value), prec);
438 result = result - Li_projection(n, cln::recip(value), prec);
442 for (int j=0; j<n-1; j++) {
443 add = add + (1+cln::expt(cln::cl_I(-1),n-j)) * (1-cln::expt(cln::cl_I(2),1-n+j))
444 * Lin_numeric(n-j,1).to_cl_N() * cln::expt(cln::log(-value),j) / cln::factorial(j);
446 result = result - add;
450 return Li_projection(n, value, prec);
455 } // end of anonymous namespace
458 //////////////////////////////////////////////////////////////////////
460 // Multiple polylogarithm Li(n,x)
464 //////////////////////////////////////////////////////////////////////
467 // anonymous namespace for helper function
471 // performs the actual series summation for multiple polylogarithms
472 cln::cl_N multipleLi_do_sum(const std::vector<int>& s, const std::vector<cln::cl_N>& x)
474 const int j = s.size();
476 std::vector<cln::cl_N> t(j);
477 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
485 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
486 for (int k=j-2; k>=0; k--) {
487 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]);
489 // ... and do it again (to avoid premature drop out due to special arguments)
491 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
492 for (int k=j-2; k>=0; k--) {
493 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]);
495 } while (t[0] != t0buf);
501 // converts parameter types and calls multipleLi_do_sum (convenience function for G_numeric)
502 cln::cl_N mLi_do_summation(const lst& m, const lst& x)
504 std::vector<int> m_int;
505 std::vector<cln::cl_N> x_cln;
506 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
507 m_int.push_back(ex_to<numeric>(*itm).to_int());
508 x_cln.push_back(ex_to<numeric>(*itx).to_cl_N());
510 return multipleLi_do_sum(m_int, x_cln);
514 // forward declaration for Li_eval()
515 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf);
518 // holding dummy-symbols for the G/Li transformations
519 std::vector<ex> gsyms;
522 // type used by the transformation functions for G
523 typedef std::vector<int> Gparameter;
526 // G_eval1-function for G transformations
527 ex G_eval1(int a, int scale)
530 const ex& scs = gsyms[std::abs(scale)];
531 const ex& as = gsyms[std::abs(a)];
533 return -log(1 - scs/as);
538 return log(gsyms[std::abs(scale)]);
543 // G_eval-function for G transformations
544 ex G_eval(const Gparameter& a, int scale)
546 // check for properties of G
547 ex sc = gsyms[std::abs(scale)];
549 bool all_zero = true;
550 bool all_ones = true;
552 for (Gparameter::const_iterator it = a.begin(); it != a.end(); ++it) {
554 const ex sym = gsyms[std::abs(*it)];
568 // care about divergent G: shuffle to separate divergencies that will be canceled
569 // later on in the transformation
570 if (newa.nops() > 1 && newa.op(0) == sc && !all_ones && a.front()!=0) {
573 Gparameter::const_iterator it = a.begin();
575 for (; it != a.end(); ++it) {
576 short_a.push_back(*it);
578 ex result = G_eval1(a.front(), scale) * G_eval(short_a, scale);
579 it = short_a.begin();
580 for (int i=1; i<count_ones; ++i) {
583 for (; it != short_a.end(); ++it) {
586 Gparameter::const_iterator it2 = short_a.begin();
587 for (--it2; it2 != it;) {
589 newa.push_back(*it2);
591 newa.push_back(a[0]);
593 for (; it2 != short_a.end(); ++it2) {
594 newa.push_back(*it2);
596 result -= G_eval(newa, scale);
598 return result / count_ones;
601 // G({1,...,1};y) -> G({1};y)^k / k!
602 if (all_ones && a.size() > 1) {
603 return pow(G_eval1(a.front(),scale), count_ones) / factorial(count_ones);
606 // G({0,...,0};y) -> log(y)^k / k!
608 return pow(log(gsyms[std::abs(scale)]), a.size()) / factorial(a.size());
611 // no special cases anymore -> convert it into Li
614 ex argbuf = gsyms[std::abs(scale)];
616 for (Gparameter::const_iterator it=a.begin(); it!=a.end(); ++it) {
618 const ex& sym = gsyms[std::abs(*it)];
619 x.append(argbuf / sym);
627 return pow(-1, x.nops()) * Li(m, x);
631 // converts data for G: pending_integrals -> a
632 Gparameter convert_pending_integrals_G(const Gparameter& pending_integrals)
634 GINAC_ASSERT(pending_integrals.size() != 1);
636 if (pending_integrals.size() > 0) {
637 // get rid of the first element, which would stand for the new upper limit
638 Gparameter new_a(pending_integrals.begin()+1, pending_integrals.end());
641 // just return empty parameter list
648 // check the parameters a and scale for G and return information about convergence, depth, etc.
649 // convergent : true if G(a,scale) is convergent
650 // depth : depth of G(a,scale)
651 // trailing_zeros : number of trailing zeros of a
652 // min_it : iterator of a pointing on the smallest element in a
653 Gparameter::const_iterator check_parameter_G(const Gparameter& a, int scale,
654 bool& convergent, int& depth, int& trailing_zeros, Gparameter::const_iterator& min_it)
660 Gparameter::const_iterator lastnonzero = a.end();
661 for (Gparameter::const_iterator it = a.begin(); it != a.end(); ++it) {
662 if (std::abs(*it) > 0) {
666 if (std::abs(*it) < scale) {
668 if ((min_it == a.end()) || (std::abs(*it) < std::abs(*min_it))) {
676 return ++lastnonzero;
680 // add scale to pending_integrals if pending_integrals is empty
681 Gparameter prepare_pending_integrals(const Gparameter& pending_integrals, int scale)
683 GINAC_ASSERT(pending_integrals.size() != 1);
685 if (pending_integrals.size() > 0) {
686 return pending_integrals;
688 Gparameter new_pending_integrals;
689 new_pending_integrals.push_back(scale);
690 return new_pending_integrals;
695 // handles trailing zeroes for an otherwise convergent integral
696 ex trailing_zeros_G(const Gparameter& a, int scale)
699 int depth, trailing_zeros;
700 Gparameter::const_iterator last, dummyit;
701 last = check_parameter_G(a, scale, convergent, depth, trailing_zeros, dummyit);
703 GINAC_ASSERT(convergent);
705 if ((trailing_zeros > 0) && (depth > 0)) {
707 Gparameter new_a(a.begin(), a.end()-1);
708 result += G_eval1(0, scale) * trailing_zeros_G(new_a, scale);
709 for (Gparameter::const_iterator it = a.begin(); it != last; ++it) {
710 Gparameter new_a(a.begin(), it);
712 new_a.insert(new_a.end(), it, a.end()-1);
713 result -= trailing_zeros_G(new_a, scale);
716 return result / trailing_zeros;
718 return G_eval(a, scale);
723 // G transformation [VSW] (57),(58)
724 ex depth_one_trafo_G(const Gparameter& pending_integrals, const Gparameter& a, int scale)
726 // pendint = ( y1, b1, ..., br )
727 // a = ( 0, ..., 0, amin )
730 // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(0, ..., 0, sr; y2)
731 // where sr replaces amin
733 GINAC_ASSERT(a.back() != 0);
734 GINAC_ASSERT(a.size() > 0);
737 Gparameter new_pending_integrals = prepare_pending_integrals(pending_integrals, std::abs(a.back()));
738 const int psize = pending_integrals.size();
741 // G(sr_{+-}; y2 ) = G(y2_{-+}; sr) - G(0; sr) + ln(-y2_{-+})
746 result += log(gsyms[ex_to<numeric>(scale).to_int()]);
748 new_pending_integrals.push_back(-scale);
751 new_pending_integrals.push_back(scale);
755 result *= trailing_zeros_G(convert_pending_integrals_G(pending_integrals), pending_integrals.front());
759 result += trailing_zeros_G(convert_pending_integrals_G(new_pending_integrals), new_pending_integrals.front());
762 new_pending_integrals.back() = 0;
763 result -= trailing_zeros_G(convert_pending_integrals_G(new_pending_integrals), new_pending_integrals.front());
769 // G_m(sr_{+-}; y2) = -zeta_m + int_0^y2 dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
770 // - int_0^sr dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
773 result -= zeta(a.size());
775 result *= trailing_zeros_G(convert_pending_integrals_G(pending_integrals), pending_integrals.front());
778 // term int_0^sr dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
779 // = int_0^sr dt/t G_{m-1}( t_{+-}; y2 )
780 Gparameter new_a(a.begin()+1, a.end());
781 new_pending_integrals.push_back(0);
782 result -= depth_one_trafo_G(new_pending_integrals, new_a, scale);
784 // term int_0^y2 dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
785 // = int_0^y2 dt/t G_{m-1}( t_{+-}; y2 )
786 Gparameter new_pending_integrals_2;
787 new_pending_integrals_2.push_back(scale);
788 new_pending_integrals_2.push_back(0);
790 result += trailing_zeros_G(convert_pending_integrals_G(pending_integrals), pending_integrals.front())
791 * depth_one_trafo_G(new_pending_integrals_2, new_a, scale);
793 result += depth_one_trafo_G(new_pending_integrals_2, new_a, scale);
800 // forward declaration
801 ex shuffle_G(const Gparameter & a0, const Gparameter & a1, const Gparameter & a2,
802 const Gparameter& pendint, const Gparameter& a_old, int scale);
805 // G transformation [VSW]
806 ex G_transform(const Gparameter& pendint, const Gparameter& a, int scale)
808 // main recursion routine
810 // pendint = ( y1, b1, ..., br )
811 // a = ( a1, ..., amin, ..., aw )
814 // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,sr,...,aw,y2)
815 // where sr replaces amin
817 // find smallest alpha, determine depth and trailing zeros, and check for convergence
819 int depth, trailing_zeros;
820 Gparameter::const_iterator min_it;
821 Gparameter::const_iterator firstzero =
822 check_parameter_G(a, scale, convergent, depth, trailing_zeros, min_it);
823 int min_it_pos = min_it - a.begin();
825 // special case: all a's are zero
832 result = G_eval(a, scale);
834 if (pendint.size() > 0) {
835 result *= trailing_zeros_G(convert_pending_integrals_G(pendint), pendint.front());
840 // handle trailing zeros
841 if (trailing_zeros > 0) {
843 Gparameter new_a(a.begin(), a.end()-1);
844 result += G_eval1(0, scale) * G_transform(pendint, new_a, scale);
845 for (Gparameter::const_iterator it = a.begin(); it != firstzero; ++it) {
846 Gparameter new_a(a.begin(), it);
848 new_a.insert(new_a.end(), it, a.end()-1);
849 result -= G_transform(pendint, new_a, scale);
851 return result / trailing_zeros;
856 if (pendint.size() > 0) {
857 return G_eval(convert_pending_integrals_G(pendint), pendint.front()) * G_eval(a, scale);
859 return G_eval(a, scale);
863 // call basic transformation for depth equal one
865 return depth_one_trafo_G(pendint, a, scale);
869 // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,sr,...,aw,y2)
870 // = int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,0,...,aw,y2)
871 // + int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) int_0^{sr} ds_{r+1} d/ds_{r+1} G(a1,...,s_{r+1},...,aw,y2)
873 // smallest element in last place
874 if (min_it + 1 == a.end()) {
875 do { --min_it; } while (*min_it == 0);
877 Gparameter a1(a.begin(),min_it+1);
878 Gparameter a2(min_it+1,a.end());
880 ex result = G_transform(pendint,a2,scale)*G_transform(empty,a1,scale);
882 result -= shuffle_G(empty,a1,a2,pendint,a,scale);
887 Gparameter::iterator changeit;
889 // first term G(a_1,..,0,...,a_w;a_0)
890 Gparameter new_pendint = prepare_pending_integrals(pendint, a[min_it_pos]);
891 Gparameter new_a = a;
892 new_a[min_it_pos] = 0;
893 ex result = G_transform(empty, new_a, scale);
894 if (pendint.size() > 0) {
895 result *= trailing_zeros_G(convert_pending_integrals_G(pendint), pendint.front());
899 changeit = new_a.begin() + min_it_pos;
900 changeit = new_a.erase(changeit);
901 if (changeit != new_a.begin()) {
902 // smallest in the middle
903 new_pendint.push_back(*changeit);
904 result -= trailing_zeros_G(convert_pending_integrals_G(new_pendint), new_pendint.front())
905 * G_transform(empty, new_a, scale);
906 int buffer = *changeit;
908 result += G_transform(new_pendint, new_a, scale);
910 new_pendint.pop_back();
912 new_pendint.push_back(*changeit);
913 result += trailing_zeros_G(convert_pending_integrals_G(new_pendint), new_pendint.front())
914 * G_transform(empty, new_a, scale);
916 result -= G_transform(new_pendint, new_a, scale);
918 // smallest at the front
919 new_pendint.push_back(scale);
920 result += trailing_zeros_G(convert_pending_integrals_G(new_pendint), new_pendint.front())
921 * G_transform(empty, new_a, scale);
922 new_pendint.back() = *changeit;
923 result -= trailing_zeros_G(convert_pending_integrals_G(new_pendint), new_pendint.front())
924 * G_transform(empty, new_a, scale);
926 result += G_transform(new_pendint, new_a, scale);
932 // shuffles the two parameter list a1 and a2 and calls G_transform for every term except
933 // for the one that is equal to a_old
934 ex shuffle_G(const Gparameter & a0, const Gparameter & a1, const Gparameter & a2,
935 const Gparameter& pendint, const Gparameter& a_old, int scale)
937 if (a1.size()==0 && a2.size()==0) {
938 // veto the one configuration we don't want
939 if ( a0 == a_old ) return 0;
941 return G_transform(pendint,a0,scale);
947 aa0.insert(aa0.end(),a1.begin(),a1.end());
948 return shuffle_G(aa0,empty,empty,pendint,a_old,scale);
954 aa0.insert(aa0.end(),a2.begin(),a2.end());
955 return shuffle_G(aa0,empty,empty,pendint,a_old,scale);
958 Gparameter a1_removed(a1.begin()+1,a1.end());
959 Gparameter a2_removed(a2.begin()+1,a2.end());
964 a01.push_back( a1[0] );
965 a02.push_back( a2[0] );
967 return shuffle_G(a01,a1_removed,a2,pendint,a_old,scale)
968 + shuffle_G(a02,a1,a2_removed,pendint,a_old,scale);
972 // handles the transformations and the numerical evaluation of G
973 // the parameter x, s and y must only contain numerics
974 ex G_numeric(const lst& x, const lst& s, const ex& y)
976 // check for convergence and necessary accelerations
977 bool need_trafo = false;
978 bool need_hoelder = false;
980 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
981 if (!(*it).is_zero()) {
983 if (abs(*it) - y < -pow(10,-Digits+2)) {
987 if (abs((abs(*it) - y)/y) < 0.01) {
992 if (x.op(x.nops()-1).is_zero()) {
995 if (depth == 1 && !need_trafo) {
996 return -Li(x.nops(), y / x.op(x.nops()-1)).evalf();
999 // convergence transformation
1002 // sort (|x|<->position) to determine indices
1003 std::multimap<ex,int> sortmap;
1005 for (int i=0; i<x.nops(); ++i) {
1006 if (!x[i].is_zero()) {
1007 sortmap.insert(std::pair<ex,int>(abs(x[i]), i));
1011 // include upper limit (scale)
1012 sortmap.insert(std::pair<ex,int>(abs(y), x.nops()));
1014 // generate missing dummy-symbols
1017 gsyms.push_back(symbol("GSYMS_ERROR"));
1019 for (std::multimap<ex,int>::const_iterator it = sortmap.begin(); it != sortmap.end(); ++it) {
1020 if (it != sortmap.begin()) {
1021 if (it->second < x.nops()) {
1022 if (x[it->second] == lastentry) {
1023 gsyms.push_back(gsyms.back());
1027 if (y == lastentry) {
1028 gsyms.push_back(gsyms.back());
1033 std::ostringstream os;
1035 gsyms.push_back(symbol(os.str()));
1037 if (it->second < x.nops()) {
1038 lastentry = x[it->second];
1044 // fill position data according to sorted indices and prepare substitution list
1045 Gparameter a(x.nops());
1049 for (std::multimap<ex,int>::const_iterator it = sortmap.begin(); it != sortmap.end(); ++it) {
1050 if (it->second < x.nops()) {
1051 if (s[it->second] > 0) {
1052 a[it->second] = pos;
1054 a[it->second] = -pos;
1056 subslst.append(gsyms[pos] == x[it->second]);
1059 subslst.append(gsyms[pos] == y);
1064 // do transformation
1066 ex result = G_transform(pendint, a, scale);
1067 // replace dummy symbols with their values
1068 result = result.eval().expand();
1069 result = result.subs(subslst).evalf();
1074 // do acceleration transformation (hoelder convolution [BBB])
1078 const int size = x.nops();
1080 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1081 newx.append(*it / y);
1084 for (int r=0; r<=size; ++r) {
1085 ex buffer = pow(-1, r);
1090 for (lst::const_iterator it = newx.begin(); it != newx.end(); ++it) {
1101 for (int j=r; j>=1; --j) {
1102 qlstx.append(1-newx.op(j-1));
1103 if (newx.op(j-1).info(info_flags::real) && newx.op(j-1) > 1 && newx.op(j-1) <= 2) {
1104 qlsts.append( s.op(j-1));
1106 qlsts.append( -s.op(j-1));
1109 if (qlstx.nops() > 0) {
1110 buffer *= G_numeric(qlstx, qlsts, 1/q);
1114 for (int j=r+1; j<=size; ++j) {
1115 plstx.append(newx.op(j-1));
1116 plsts.append(s.op(j-1));
1118 if (plstx.nops() > 0) {
1119 buffer *= G_numeric(plstx, plsts, 1/p);
1132 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1133 if ((*it).is_zero()) {
1136 newx.append(factor / (*it));
1144 return sign * numeric(mLi_do_summation(m, newx));
1148 ex mLi_numeric(const lst& m, const lst& x)
1150 // let G_numeric do the transformation
1154 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1155 for (int i = 1; i < *itm; ++i) {
1159 newx.append(factor / *itx);
1163 return pow(-1, m.nops()) * G_numeric(newx, s, _ex1);
1167 } // end of anonymous namespace
1170 //////////////////////////////////////////////////////////////////////
1172 // Generalized multiple polylogarithm G(x, y) and G(x, s, y)
1176 //////////////////////////////////////////////////////////////////////
1179 static ex G2_evalf(const ex& x_, const ex& y)
1181 if (!y.info(info_flags::positive)) {
1182 return G(x_, y).hold();
1184 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1185 if (x.nops() == 0) {
1189 return G(x_, y).hold();
1192 bool all_zero = true;
1193 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1194 if (!(*it).info(info_flags::numeric)) {
1195 return G(x_, y).hold();
1203 return pow(log(y), x.nops()) / factorial(x.nops());
1205 return G_numeric(x, s, y);
1209 static ex G2_eval(const ex& x_, const ex& y)
1211 //TODO eval to MZV or H or S or Lin
1213 if (!y.info(info_flags::positive)) {
1214 return G(x_, y).hold();
1216 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1217 if (x.nops() == 0) {
1221 return G(x_, y).hold();
1224 bool all_zero = true;
1225 bool crational = true;
1226 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1227 if (!(*it).info(info_flags::numeric)) {
1228 return G(x_, y).hold();
1230 if (!(*it).info(info_flags::crational)) {
1239 return pow(log(y), x.nops()) / factorial(x.nops());
1241 if (!y.info(info_flags::crational)) {
1245 return G(x_, y).hold();
1247 return G_numeric(x, s, y);
1251 unsigned G2_SERIAL::serial = function::register_new(function_options("G", 2).
1252 evalf_func(G2_evalf).
1254 do_not_evalf_params().
1257 // derivative_func(G2_deriv).
1258 // print_func<print_latex>(G2_print_latex).
1261 static ex G3_evalf(const ex& x_, const ex& s_, const ex& y)
1263 if (!y.info(info_flags::positive)) {
1264 return G(x_, s_, y).hold();
1266 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1267 lst s = is_a<lst>(s_) ? ex_to<lst>(s_) : lst(s_);
1268 if (x.nops() != s.nops()) {
1269 return G(x_, s_, y).hold();
1271 if (x.nops() == 0) {
1275 return G(x_, s_, y).hold();
1278 bool all_zero = true;
1279 for (lst::const_iterator itx = x.begin(), its = s.begin(); itx != x.end(); ++itx, ++its) {
1280 if (!(*itx).info(info_flags::numeric)) {
1281 return G(x_, y).hold();
1283 if (!(*its).info(info_flags::real)) {
1284 return G(x_, y).hold();
1296 return pow(log(y), x.nops()) / factorial(x.nops());
1298 return G_numeric(x, sn, y);
1302 static ex G3_eval(const ex& x_, const ex& s_, const ex& y)
1304 //TODO eval to MZV or H or S or Lin
1306 if (!y.info(info_flags::positive)) {
1307 return G(x_, s_, y).hold();
1309 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1310 lst s = is_a<lst>(s_) ? ex_to<lst>(s_) : lst(s_);
1311 if (x.nops() != s.nops()) {
1312 return G(x_, s_, y).hold();
1314 if (x.nops() == 0) {
1318 return G(x_, s_, y).hold();
1321 bool all_zero = true;
1322 bool crational = true;
1323 for (lst::const_iterator itx = x.begin(), its = s.begin(); itx != x.end(); ++itx, ++its) {
1324 if (!(*itx).info(info_flags::numeric)) {
1325 return G(x_, s_, y).hold();
1327 if (!(*its).info(info_flags::real)) {
1328 return G(x_, s_, y).hold();
1330 if (!(*itx).info(info_flags::crational)) {
1343 return pow(log(y), x.nops()) / factorial(x.nops());
1345 if (!y.info(info_flags::crational)) {
1349 return G(x_, s_, y).hold();
1351 return G_numeric(x, sn, y);
1355 unsigned G3_SERIAL::serial = function::register_new(function_options("G", 3).
1356 evalf_func(G3_evalf).
1358 do_not_evalf_params().
1361 // derivative_func(G3_deriv).
1362 // print_func<print_latex>(G3_print_latex).
1365 //////////////////////////////////////////////////////////////////////
1367 // Classical polylogarithm and multiple polylogarithm Li(m,x)
1371 //////////////////////////////////////////////////////////////////////
1374 static ex Li_evalf(const ex& m_, const ex& x_)
1376 // classical polylogs
1377 if (m_.info(info_flags::posint)) {
1378 if (x_.info(info_flags::numeric)) {
1379 return Lin_numeric(ex_to<numeric>(m_).to_int(), ex_to<numeric>(x_));
1381 // try to numerically evaluate second argument
1382 ex x_val = x_.evalf();
1383 if (x_val.info(info_flags::numeric)) {
1384 return Lin_numeric(ex_to<numeric>(m_).to_int(), ex_to<numeric>(x_val));
1388 // multiple polylogs
1389 if (is_a<lst>(m_) && is_a<lst>(x_)) {
1391 const lst& m = ex_to<lst>(m_);
1392 const lst& x = ex_to<lst>(x_);
1393 if (m.nops() != x.nops()) {
1394 return Li(m_,x_).hold();
1396 if (x.nops() == 0) {
1399 if ((m.op(0) == _ex1) && (x.op(0) == _ex1)) {
1400 return Li(m_,x_).hold();
1403 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1404 if (!(*itm).info(info_flags::posint)) {
1405 return Li(m_, x_).hold();
1407 if (!(*itx).info(info_flags::numeric)) {
1408 return Li(m_, x_).hold();
1415 return mLi_numeric(m, x);
1418 return Li(m_,x_).hold();
1422 static ex Li_eval(const ex& m_, const ex& x_)
1424 if (is_a<lst>(m_)) {
1425 if (is_a<lst>(x_)) {
1426 // multiple polylogs
1427 const lst& m = ex_to<lst>(m_);
1428 const lst& x = ex_to<lst>(x_);
1429 if (m.nops() != x.nops()) {
1430 return Li(m_,x_).hold();
1432 if (x.nops() == 0) {
1436 bool is_zeta = true;
1437 bool do_evalf = true;
1438 bool crational = true;
1439 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1440 if (!(*itm).info(info_flags::posint)) {
1441 return Li(m_,x_).hold();
1443 if ((*itx != _ex1) && (*itx != _ex_1)) {
1444 if (itx != x.begin()) {
1452 if (!(*itx).info(info_flags::numeric)) {
1455 if (!(*itx).info(info_flags::crational)) {
1464 lst newm = convert_parameter_Li_to_H(m, x, prefactor);
1465 return prefactor * H(newm, x[0]);
1467 if (do_evalf && !crational) {
1468 return mLi_numeric(m,x);
1471 return Li(m_, x_).hold();
1472 } else if (is_a<lst>(x_)) {
1473 return Li(m_, x_).hold();
1476 // classical polylogs
1484 return (pow(2,1-m_)-1) * zeta(m_);
1490 if (x_.is_equal(I)) {
1491 return power(Pi,_ex2)/_ex_48 + Catalan*I;
1493 if (x_.is_equal(-I)) {
1494 return power(Pi,_ex2)/_ex_48 - Catalan*I;
1497 if (m_.info(info_flags::posint) && x_.info(info_flags::numeric) && !x_.info(info_flags::crational)) {
1498 return Lin_numeric(ex_to<numeric>(m_).to_int(), ex_to<numeric>(x_));
1501 return Li(m_, x_).hold();
1505 static ex Li_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
1508 seq.push_back(expair(Li(m, x), 0));
1509 return pseries(rel, seq);
1513 static ex Li_deriv(const ex& m_, const ex& x_, unsigned deriv_param)
1515 GINAC_ASSERT(deriv_param < 2);
1516 if (deriv_param == 0) {
1519 if (m_.nops() > 1) {
1520 throw std::runtime_error("don't know how to derivate multiple polylogarithm!");
1523 if (is_a<lst>(m_)) {
1529 if (is_a<lst>(x_)) {
1535 return Li(m-1, x) / x;
1542 static void Li_print_latex(const ex& m_, const ex& x_, const print_context& c)
1545 if (is_a<lst>(m_)) {
1551 if (is_a<lst>(x_)) {
1556 c.s << "\\mbox{Li}_{";
1557 lst::const_iterator itm = m.begin();
1560 for (; itm != m.end(); itm++) {
1565 lst::const_iterator itx = x.begin();
1568 for (; itx != x.end(); itx++) {
1576 REGISTER_FUNCTION(Li,
1577 evalf_func(Li_evalf).
1579 series_func(Li_series).
1580 derivative_func(Li_deriv).
1581 print_func<print_latex>(Li_print_latex).
1582 do_not_evalf_params());
1585 //////////////////////////////////////////////////////////////////////
1587 // Nielsen's generalized polylogarithm S(n,p,x)
1591 //////////////////////////////////////////////////////////////////////
1594 // anonymous namespace for helper functions
1598 // lookup table for special Euler-Zagier-Sums (used for S_n,p(x))
1600 std::vector<std::vector<cln::cl_N> > Yn;
1601 int ynsize = 0; // number of Yn[]
1602 int ynlength = 100; // initial length of all Yn[i]
1605 // This function calculates the Y_n. The Y_n are needed for the evaluation of S_{n,p}(x).
1606 // The Y_n are basically Euler-Zagier sums with all m_i=1. They are subsums in the Z-sum
1607 // representing S_{n,p}(x).
1608 // The first index in Y_n corresponds to the parameter p minus one, i.e. the depth of the
1609 // equivalent Z-sum.
1610 // The second index in Y_n corresponds to the running index of the outermost sum in the full Z-sum
1611 // representing S_{n,p}(x).
1612 // The calculation of Y_n uses the values from Y_{n-1}.
1613 void fill_Yn(int n, const cln::float_format_t& prec)
1615 const int initsize = ynlength;
1616 //const int initsize = initsize_Yn;
1617 cln::cl_N one = cln::cl_float(1, prec);
1620 std::vector<cln::cl_N> buf(initsize);
1621 std::vector<cln::cl_N>::iterator it = buf.begin();
1622 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
1623 *it = (*itprev) / cln::cl_N(n+1) * one;
1626 // sums with an index smaller than the depth are zero and need not to be calculated.
1627 // calculation starts with depth, which is n+2)
1628 for (int i=n+2; i<=initsize+n; i++) {
1629 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
1635 std::vector<cln::cl_N> buf(initsize);
1636 std::vector<cln::cl_N>::iterator it = buf.begin();
1639 for (int i=2; i<=initsize; i++) {
1640 *it = *(it-1) + 1 / cln::cl_N(i) * one;
1649 // make Yn longer ...
1650 void make_Yn_longer(int newsize, const cln::float_format_t& prec)
1653 cln::cl_N one = cln::cl_float(1, prec);
1655 Yn[0].resize(newsize);
1656 std::vector<cln::cl_N>::iterator it = Yn[0].begin();
1658 for (int i=ynlength+1; i<=newsize; i++) {
1659 *it = *(it-1) + 1 / cln::cl_N(i) * one;
1663 for (int n=1; n<ynsize; n++) {
1664 Yn[n].resize(newsize);
1665 std::vector<cln::cl_N>::iterator it = Yn[n].begin();
1666 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
1669 for (int i=ynlength+n+1; i<=newsize+n; i++) {
1670 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
1680 // helper function for S(n,p,x)
1682 cln::cl_N C(int n, int p)
1686 for (int k=0; k<p; k++) {
1687 for (int j=0; j<=(n+k-1)/2; j++) {
1691 result = result - 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1).to_cl_N() / cln::factorial(2*j);
1694 result = result + 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1).to_cl_N() / cln::factorial(2*j);
1701 result = result + cln::factorial(n+k-1)
1702 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
1703 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1706 result = result - cln::factorial(n+k-1)
1707 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
1708 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1713 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()
1714 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1717 result = result + cln::factorial(n+k-1)
1718 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
1719 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1727 if (((np)/2+n) & 1) {
1728 result = -result - cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
1731 result = -result + cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
1739 // helper function for S(n,p,x)
1740 // [Kol] remark to (9.1)
1741 cln::cl_N a_k(int k)
1750 for (int m=2; m<=k; m++) {
1751 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * a_k(k-m);
1758 // helper function for S(n,p,x)
1759 // [Kol] remark to (9.1)
1760 cln::cl_N b_k(int k)
1769 for (int m=2; m<=k; m++) {
1770 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * b_k(k-m);
1777 // helper function for S(n,p,x)
1778 cln::cl_N S_do_sum(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
1781 return Li_projection(n+1, x, prec);
1784 // check if precalculated values are sufficient
1786 for (int i=ynsize; i<p-1; i++) {
1791 // should be done otherwise
1792 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
1793 cln::cl_N xf = x * one;
1794 //cln::cl_N xf = x * cln::cl_float(1, prec);
1798 cln::cl_N factor = cln::expt(xf, p);
1802 if (i-p >= ynlength) {
1804 make_Yn_longer(ynlength*2, prec);
1806 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? ...
1807 //res = res + factor / cln::expt(cln::cl_I(i),n+1) * (*it); // should we check it? or rely on magic number? ...
1808 factor = factor * xf;
1810 } while (res != resbuf);
1816 // helper function for S(n,p,x)
1817 cln::cl_N S_projection(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
1820 if (cln::abs(cln::realpart(x)) > cln::cl_F("0.5")) {
1822 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(x),n)
1823 * cln::expt(cln::log(1-x),p) / cln::factorial(n) / cln::factorial(p);
1825 for (int s=0; s<n; s++) {
1827 for (int r=0; r<p; r++) {
1828 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-x),r)
1829 * S_do_sum(p-r,n-s,1-x,prec) / cln::factorial(r);
1831 result = result + cln::expt(cln::log(x),s) * (S_num(n-s,p,1).to_cl_N() - res2) / cln::factorial(s);
1837 return S_do_sum(n, p, x, prec);
1841 // helper function for S(n,p,x)
1842 numeric S_num(int n, int p, const numeric& x)
1846 // [Kol] (2.22) with (2.21)
1847 return cln::zeta(p+1);
1852 return cln::zeta(n+1);
1857 for (int nu=0; nu<n; nu++) {
1858 for (int rho=0; rho<=p; rho++) {
1859 result = result + b_k(n-nu-1) * b_k(p-rho) * a_k(nu+rho+1)
1860 * cln::factorial(nu+rho+1) / cln::factorial(rho) / cln::factorial(nu+1);
1863 result = result * cln::expt(cln::cl_I(-1),n+p-1);
1870 return -(1-cln::expt(cln::cl_I(2),-n)) * cln::zeta(n+1);
1872 // throw std::runtime_error("don't know how to evaluate this function!");
1875 // what is the desired float format?
1876 // first guess: default format
1877 cln::float_format_t prec = cln::default_float_format;
1878 const cln::cl_N value = x.to_cl_N();
1879 // second guess: the argument's format
1880 if (!x.real().is_rational())
1881 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
1882 else if (!x.imag().is_rational())
1883 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
1886 if ((cln::realpart(value) < -0.5) || (n == 0)) {
1888 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(value),n)
1889 * cln::expt(cln::log(1-value),p) / cln::factorial(n) / cln::factorial(p);
1891 for (int s=0; s<n; s++) {
1893 for (int r=0; r<p; r++) {
1894 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-value),r)
1895 * S_num(p-r,n-s,1-value).to_cl_N() / cln::factorial(r);
1897 result = result + cln::expt(cln::log(value),s) * (S_num(n-s,p,1).to_cl_N() - res2) / cln::factorial(s);
1904 if (cln::abs(value) > 1) {
1908 for (int s=0; s<p; s++) {
1909 for (int r=0; r<=s; r++) {
1910 result = result + cln::expt(cln::cl_I(-1),s) * cln::expt(cln::log(-value),r) * cln::factorial(n+s-r-1)
1911 / cln::factorial(r) / cln::factorial(s-r) / cln::factorial(n-1)
1912 * S_num(n+s-r,p-s,cln::recip(value)).to_cl_N();
1915 result = result * cln::expt(cln::cl_I(-1),n);
1918 for (int r=0; r<n; r++) {
1919 res2 = res2 + cln::expt(cln::log(-value),r) * C(n-r,p) / cln::factorial(r);
1921 res2 = res2 + cln::expt(cln::log(-value),n+p) / cln::factorial(n+p);
1923 result = result + cln::expt(cln::cl_I(-1),p) * res2;
1928 return S_projection(n, p, value, prec);
1933 } // end of anonymous namespace
1936 //////////////////////////////////////////////////////////////////////
1938 // Nielsen's generalized polylogarithm S(n,p,x)
1942 //////////////////////////////////////////////////////////////////////
1945 static ex S_evalf(const ex& n, const ex& p, const ex& x)
1947 if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
1948 if (is_a<numeric>(x)) {
1949 return S_num(ex_to<numeric>(n).to_int(), ex_to<numeric>(p).to_int(), ex_to<numeric>(x));
1951 ex x_val = x.evalf();
1952 if (is_a<numeric>(x_val)) {
1953 return S_num(ex_to<numeric>(n).to_int(), ex_to<numeric>(p).to_int(), ex_to<numeric>(x_val));
1957 return S(n, p, x).hold();
1961 static ex S_eval(const ex& n, const ex& p, const ex& x)
1963 if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
1969 for (int i=ex_to<numeric>(p).to_int()-1; i>0; i--) {
1977 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
1978 return S_num(ex_to<numeric>(n).to_int(), ex_to<numeric>(p).to_int(), ex_to<numeric>(x));
1983 return pow(-log(1-x), p) / factorial(p);
1985 return S(n, p, x).hold();
1989 static ex S_series(const ex& n, const ex& p, const ex& x, const relational& rel, int order, unsigned options)
1992 seq.push_back(expair(S(n, p, x), 0));
1993 return pseries(rel, seq);
1997 static ex S_deriv(const ex& n, const ex& p, const ex& x, unsigned deriv_param)
1999 GINAC_ASSERT(deriv_param < 3);
2000 if (deriv_param < 2) {
2004 return S(n-1, p, x) / x;
2006 return S(n, p-1, x) / (1-x);
2011 static void S_print_latex(const ex& n, const ex& p, const ex& x, const print_context& c)
2013 c.s << "\\mbox{S}_{";
2023 REGISTER_FUNCTION(S,
2024 evalf_func(S_evalf).
2026 series_func(S_series).
2027 derivative_func(S_deriv).
2028 print_func<print_latex>(S_print_latex).
2029 do_not_evalf_params());
2032 //////////////////////////////////////////////////////////////////////
2034 // Harmonic polylogarithm H(m,x)
2038 //////////////////////////////////////////////////////////////////////
2041 // anonymous namespace for helper functions
2045 // regulates the pole (used by 1/x-transformation)
2046 symbol H_polesign("IMSIGN");
2049 // convert parameters from H to Li representation
2050 // parameters are expected to be in expanded form, i.e. only 0, 1 and -1
2051 // returns true if some parameters are negative
2052 bool convert_parameter_H_to_Li(const lst& l, lst& m, lst& s, ex& pf)
2054 // expand parameter list
2056 for (lst::const_iterator it = l.begin(); it != l.end(); it++) {
2058 for (ex count=*it-1; count > 0; count--) {
2062 } else if (*it < -1) {
2063 for (ex count=*it+1; count < 0; count++) {
2074 bool has_negative_parameters = false;
2076 for (lst::const_iterator it = mexp.begin(); it != mexp.end(); it++) {
2082 m.append((*it+acc-1) * signum);
2084 m.append((*it-acc+1) * signum);
2090 has_negative_parameters = true;
2093 if (has_negative_parameters) {
2094 for (int i=0; i<m.nops(); i++) {
2096 m.let_op(i) = -m.op(i);
2104 return has_negative_parameters;
2108 // recursivly transforms H to corresponding multiple polylogarithms
2109 struct map_trafo_H_convert_to_Li : public map_function
2111 ex operator()(const ex& e)
2113 if (is_a<add>(e) || is_a<mul>(e)) {
2114 return e.map(*this);
2116 if (is_a<function>(e)) {
2117 std::string name = ex_to<function>(e).get_name();
2120 if (is_a<lst>(e.op(0))) {
2121 parameter = ex_to<lst>(e.op(0));
2123 parameter = lst(e.op(0));
2130 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
2131 s.let_op(0) = s.op(0) * arg;
2132 return pf * Li(m, s).hold();
2134 for (int i=0; i<m.nops(); i++) {
2137 s.let_op(0) = s.op(0) * arg;
2138 return Li(m, s).hold();
2147 // recursivly transforms H to corresponding zetas
2148 struct map_trafo_H_convert_to_zeta : public map_function
2150 ex operator()(const ex& e)
2152 if (is_a<add>(e) || is_a<mul>(e)) {
2153 return e.map(*this);
2155 if (is_a<function>(e)) {
2156 std::string name = ex_to<function>(e).get_name();
2159 if (is_a<lst>(e.op(0))) {
2160 parameter = ex_to<lst>(e.op(0));
2162 parameter = lst(e.op(0));
2168 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
2169 return pf * zeta(m, s);
2180 // remove trailing zeros from H-parameters
2181 struct map_trafo_H_reduce_trailing_zeros : public map_function
2183 ex operator()(const ex& e)
2185 if (is_a<add>(e) || is_a<mul>(e)) {
2186 return e.map(*this);
2188 if (is_a<function>(e)) {
2189 std::string name = ex_to<function>(e).get_name();
2192 if (is_a<lst>(e.op(0))) {
2193 parameter = ex_to<lst>(e.op(0));
2195 parameter = lst(e.op(0));
2198 if (parameter.op(parameter.nops()-1) == 0) {
2201 if (parameter.nops() == 1) {
2206 lst::const_iterator it = parameter.begin();
2207 while ((it != parameter.end()) && (*it == 0)) {
2210 if (it == parameter.end()) {
2211 return pow(log(arg),parameter.nops()) / factorial(parameter.nops());
2215 parameter.remove_last();
2216 int lastentry = parameter.nops();
2217 while ((lastentry > 0) && (parameter[lastentry-1] == 0)) {
2222 ex result = log(arg) * H(parameter,arg).hold();
2224 for (ex i=0; i<lastentry; i++) {
2225 if (parameter[i] > 0) {
2227 result -= (acc + parameter[i]-1) * H(parameter, arg).hold();
2230 } else if (parameter[i] < 0) {
2232 result -= (acc + abs(parameter[i]+1)) * H(parameter, arg).hold();
2240 if (lastentry < parameter.nops()) {
2241 result = result / (parameter.nops()-lastentry+1);
2242 return result.map(*this);
2254 // returns an expression with zeta functions corresponding to the parameter list for H
2255 ex convert_H_to_zeta(const lst& m)
2257 symbol xtemp("xtemp");
2258 map_trafo_H_reduce_trailing_zeros filter;
2259 map_trafo_H_convert_to_zeta filter2;
2260 return filter2(filter(H(m, xtemp).hold())).subs(xtemp == 1);
2264 // convert signs form Li to H representation
2265 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf)
2268 lst::const_iterator itm = m.begin();
2269 lst::const_iterator itx = ++x.begin();
2274 while (itx != x.end()) {
2275 signum *= (*itx > 0) ? 1 : -1;
2277 res.append((*itm) * signum);
2285 // multiplies an one-dimensional H with another H
2287 ex trafo_H_mult(const ex& h1, const ex& h2)
2292 ex h1nops = h1.op(0).nops();
2293 ex h2nops = h2.op(0).nops();
2295 hshort = h2.op(0).op(0);
2296 hlong = ex_to<lst>(h1.op(0));
2298 hshort = h1.op(0).op(0);
2300 hlong = ex_to<lst>(h2.op(0));
2302 hlong = h2.op(0).op(0);
2305 for (int i=0; i<=hlong.nops(); i++) {
2309 newparameter.append(hlong[j]);
2311 newparameter.append(hshort);
2312 for (; j<hlong.nops(); j++) {
2313 newparameter.append(hlong[j]);
2315 res += H(newparameter, h1.op(1)).hold();
2321 // applies trafo_H_mult recursively on expressions
2322 struct map_trafo_H_mult : public map_function
2324 ex operator()(const ex& e)
2327 return e.map(*this);
2335 for (int pos=0; pos<e.nops(); pos++) {
2336 if (is_a<power>(e.op(pos)) && is_a<function>(e.op(pos).op(0))) {
2337 std::string name = ex_to<function>(e.op(pos).op(0)).get_name();
2339 for (ex i=0; i<e.op(pos).op(1); i++) {
2340 Hlst.append(e.op(pos).op(0));
2344 } else if (is_a<function>(e.op(pos))) {
2345 std::string name = ex_to<function>(e.op(pos)).get_name();
2347 if (e.op(pos).op(0).nops() > 1) {
2350 Hlst.append(e.op(pos));
2355 result *= e.op(pos);
2358 if (Hlst.nops() > 0) {
2359 firstH = Hlst[Hlst.nops()-1];
2366 if (Hlst.nops() > 0) {
2367 ex buffer = trafo_H_mult(firstH, Hlst.op(0));
2369 for (int i=1; i<Hlst.nops(); i++) {
2370 result *= Hlst.op(i);
2372 result = result.expand();
2373 map_trafo_H_mult recursion;
2374 return recursion(result);
2385 // do integration [ReV] (55)
2386 // put parameter 0 in front of existing parameters
2387 ex trafo_H_1tx_prepend_zero(const ex& e, const ex& arg)
2391 if (is_a<function>(e)) {
2392 name = ex_to<function>(e).get_name();
2397 for (int i=0; i<e.nops(); i++) {
2398 if (is_a<function>(e.op(i))) {
2399 std::string name = ex_to<function>(e.op(i)).get_name();
2407 lst newparameter = ex_to<lst>(h.op(0));
2408 newparameter.prepend(0);
2409 ex addzeta = convert_H_to_zeta(newparameter);
2410 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
2412 return e * (-H(lst(0),1/arg).hold());
2417 // do integration [ReV] (55)
2418 // put parameter -1 in front of existing parameters
2419 ex trafo_H_1tx_prepend_minusone(const ex& e, const ex& arg)
2423 if (is_a<function>(e)) {
2424 name = ex_to<function>(e).get_name();
2429 for (int i=0; i<e.nops(); i++) {
2430 if (is_a<function>(e.op(i))) {
2431 std::string name = ex_to<function>(e.op(i)).get_name();
2439 lst newparameter = ex_to<lst>(h.op(0));
2440 newparameter.prepend(-1);
2441 ex addzeta = convert_H_to_zeta(newparameter);
2442 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
2444 ex addzeta = convert_H_to_zeta(lst(-1));
2445 return (e * (addzeta - H(lst(-1),1/arg).hold())).expand();
2450 // do integration [ReV] (55)
2451 // put parameter -1 in front of existing parameters
2452 ex trafo_H_1mxt1px_prepend_minusone(const ex& e, const ex& arg)
2456 if (is_a<function>(e)) {
2457 name = ex_to<function>(e).get_name();
2462 for (int i=0; i<e.nops(); i++) {
2463 if (is_a<function>(e.op(i))) {
2464 std::string name = ex_to<function>(e.op(i)).get_name();
2472 lst newparameter = ex_to<lst>(h.op(0));
2473 newparameter.prepend(-1);
2474 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
2476 return (e * H(lst(-1),(1-arg)/(1+arg)).hold()).expand();
2481 // do integration [ReV] (55)
2482 // put parameter 1 in front of existing parameters
2483 ex trafo_H_1mxt1px_prepend_one(const ex& e, const ex& arg)
2487 if (is_a<function>(e)) {
2488 name = ex_to<function>(e).get_name();
2493 for (int i=0; i<e.nops(); i++) {
2494 if (is_a<function>(e.op(i))) {
2495 std::string name = ex_to<function>(e.op(i)).get_name();
2503 lst newparameter = ex_to<lst>(h.op(0));
2504 newparameter.prepend(1);
2505 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
2507 return (e * H(lst(1),(1-arg)/(1+arg)).hold()).expand();
2512 // do x -> 1/x transformation
2513 struct map_trafo_H_1overx : public map_function
2515 ex operator()(const ex& e)
2517 if (is_a<add>(e) || is_a<mul>(e)) {
2518 return e.map(*this);
2521 if (is_a<function>(e)) {
2522 std::string name = ex_to<function>(e).get_name();
2525 lst parameter = ex_to<lst>(e.op(0));
2528 // special cases if all parameters are either 0, 1 or -1
2529 bool allthesame = true;
2530 if (parameter.op(0) == 0) {
2531 for (int i=1; i<parameter.nops(); i++) {
2532 if (parameter.op(i) != 0) {
2538 return pow(-1, parameter.nops()) * H(parameter, 1/arg).hold();
2540 } else if (parameter.op(0) == -1) {
2541 for (int i=1; i<parameter.nops(); i++) {
2542 if (parameter.op(i) != -1) {
2548 map_trafo_H_mult unify;
2549 return unify((pow(H(lst(-1),1/arg).hold() - H(lst(0),1/arg).hold(), parameter.nops())
2550 / factorial(parameter.nops())).expand());
2553 for (int i=1; i<parameter.nops(); i++) {
2554 if (parameter.op(i) != 1) {
2560 map_trafo_H_mult unify;
2561 return unify((pow(H(lst(1),1/arg).hold() + H(lst(0),1/arg).hold() + H_polesign, parameter.nops())
2562 / factorial(parameter.nops())).expand());
2566 lst newparameter = parameter;
2567 newparameter.remove_first();
2569 if (parameter.op(0) == 0) {
2572 ex res = convert_H_to_zeta(parameter);
2573 map_trafo_H_1overx recursion;
2574 ex buffer = recursion(H(newparameter, arg).hold());
2575 if (is_a<add>(buffer)) {
2576 for (int i=0; i<buffer.nops(); i++) {
2577 res += trafo_H_1tx_prepend_zero(buffer.op(i), arg);
2580 res += trafo_H_1tx_prepend_zero(buffer, arg);
2584 } else if (parameter.op(0) == -1) {
2586 // leading negative one
2587 ex res = convert_H_to_zeta(parameter);
2588 map_trafo_H_1overx recursion;
2589 ex buffer = recursion(H(newparameter, arg).hold());
2590 if (is_a<add>(buffer)) {
2591 for (int i=0; i<buffer.nops(); i++) {
2592 res += trafo_H_1tx_prepend_zero(buffer.op(i), arg) - trafo_H_1tx_prepend_minusone(buffer.op(i), arg);
2595 res += trafo_H_1tx_prepend_zero(buffer, arg) - trafo_H_1tx_prepend_minusone(buffer, arg);
2602 map_trafo_H_1overx recursion;
2603 map_trafo_H_mult unify;
2604 ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
2606 while (parameter.op(firstzero) == 1) {
2609 for (int i=firstzero-1; i<parameter.nops()-1; i++) {
2613 newparameter.append(parameter[j+1]);
2615 newparameter.append(1);
2616 for (; j<parameter.nops()-1; j++) {
2617 newparameter.append(parameter[j+1]);
2619 res -= H(newparameter, arg).hold();
2621 res = recursion(res).expand() / firstzero;
2633 // do x -> (1-x)/(1+x) transformation
2634 struct map_trafo_H_1mxt1px : public map_function
2636 ex operator()(const ex& e)
2638 if (is_a<add>(e) || is_a<mul>(e)) {
2639 return e.map(*this);
2642 if (is_a<function>(e)) {
2643 std::string name = ex_to<function>(e).get_name();
2646 lst parameter = ex_to<lst>(e.op(0));
2649 // special cases if all parameters are either 0, 1 or -1
2650 bool allthesame = true;
2651 if (parameter.op(0) == 0) {
2652 for (int i=1; i<parameter.nops(); i++) {
2653 if (parameter.op(i) != 0) {
2659 map_trafo_H_mult unify;
2660 return unify((pow(-H(lst(1),(1-arg)/(1+arg)).hold() - H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
2661 / factorial(parameter.nops())).expand());
2663 } else if (parameter.op(0) == -1) {
2664 for (int i=1; i<parameter.nops(); i++) {
2665 if (parameter.op(i) != -1) {
2671 map_trafo_H_mult unify;
2672 return unify((pow(log(2) - H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
2673 / factorial(parameter.nops())).expand());
2676 for (int i=1; i<parameter.nops(); i++) {
2677 if (parameter.op(i) != 1) {
2683 map_trafo_H_mult unify;
2684 return unify((pow(-log(2) - H(lst(0),(1-arg)/(1+arg)).hold() + H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
2685 / factorial(parameter.nops())).expand());
2689 lst newparameter = parameter;
2690 newparameter.remove_first();
2692 if (parameter.op(0) == 0) {
2695 ex res = convert_H_to_zeta(parameter);
2696 map_trafo_H_1mxt1px recursion;
2697 ex buffer = recursion(H(newparameter, arg).hold());
2698 if (is_a<add>(buffer)) {
2699 for (int i=0; i<buffer.nops(); i++) {
2700 res -= trafo_H_1mxt1px_prepend_one(buffer.op(i), arg) + trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
2703 res -= trafo_H_1mxt1px_prepend_one(buffer, arg) + trafo_H_1mxt1px_prepend_minusone(buffer, arg);
2707 } else if (parameter.op(0) == -1) {
2709 // leading negative one
2710 ex res = convert_H_to_zeta(parameter);
2711 map_trafo_H_1mxt1px recursion;
2712 ex buffer = recursion(H(newparameter, arg).hold());
2713 if (is_a<add>(buffer)) {
2714 for (int i=0; i<buffer.nops(); i++) {
2715 res -= trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
2718 res -= trafo_H_1mxt1px_prepend_minusone(buffer, arg);
2725 map_trafo_H_1mxt1px recursion;
2726 map_trafo_H_mult unify;
2727 ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
2729 while (parameter.op(firstzero) == 1) {
2732 for (int i=firstzero-1; i<parameter.nops()-1; i++) {
2736 newparameter.append(parameter[j+1]);
2738 newparameter.append(1);
2739 for (; j<parameter.nops()-1; j++) {
2740 newparameter.append(parameter[j+1]);
2742 res -= H(newparameter, arg).hold();
2744 res = recursion(res).expand() / firstzero;
2756 // do the actual summation.
2757 cln::cl_N H_do_sum(const std::vector<int>& m, const cln::cl_N& x)
2759 const int j = m.size();
2761 std::vector<cln::cl_N> t(j);
2763 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
2764 cln::cl_N factor = cln::expt(x, j) * one;
2770 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),m[j-1]);
2771 for (int k=j-2; k>=1; k--) {
2772 t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), m[k]);
2774 t[0] = t[0] + t[1] * factor / cln::expt(cln::cl_I(q+j-1), m[0]);
2775 factor = factor * x;
2776 } while (t[0] != t0buf);
2782 } // end of anonymous namespace
2785 //////////////////////////////////////////////////////////////////////
2787 // Harmonic polylogarithm H(m,x)
2791 //////////////////////////////////////////////////////////////////////
2794 static ex H_evalf(const ex& x1, const ex& x2)
2796 if (is_a<lst>(x1)) {
2799 if (is_a<numeric>(x2)) {
2800 x = ex_to<numeric>(x2).to_cl_N();
2802 ex x2_val = x2.evalf();
2803 if (is_a<numeric>(x2_val)) {
2804 x = ex_to<numeric>(x2_val).to_cl_N();
2808 for (int i=0; i<x1.nops(); i++) {
2809 if (!x1.op(i).info(info_flags::integer)) {
2810 return H(x1, x2).hold();
2813 if (x1.nops() < 1) {
2814 return H(x1, x2).hold();
2817 const lst& morg = ex_to<lst>(x1);
2818 // remove trailing zeros ...
2819 if (*(--morg.end()) == 0) {
2820 symbol xtemp("xtemp");
2821 map_trafo_H_reduce_trailing_zeros filter;
2822 return filter(H(x1, xtemp).hold()).subs(xtemp==x2).evalf();
2824 // ... and expand parameter notation
2826 for (lst::const_iterator it = morg.begin(); it != morg.end(); it++) {
2828 for (ex count=*it-1; count > 0; count--) {
2832 } else if (*it < -1) {
2833 for (ex count=*it+1; count < 0; count++) {
2842 // since the transformations produce a lot of terms, they are only efficient for
2843 // argument near one.
2844 // no transformation needed -> do summation
2845 if (cln::abs(x) < 0.95) {
2849 if (convert_parameter_H_to_Li(m, m_lst, s_lst, pf)) {
2850 // negative parameters -> s_lst is filled
2851 std::vector<int> m_int;
2852 std::vector<cln::cl_N> x_cln;
2853 for (lst::const_iterator it_int = m_lst.begin(), it_cln = s_lst.begin();
2854 it_int != m_lst.end(); it_int++, it_cln++) {
2855 m_int.push_back(ex_to<numeric>(*it_int).to_int());
2856 x_cln.push_back(ex_to<numeric>(*it_cln).to_cl_N());
2858 x_cln.front() = x_cln.front() * x;
2859 return pf * numeric(multipleLi_do_sum(m_int, x_cln));
2861 // only positive parameters
2863 if (m_lst.nops() == 1) {
2864 return Li(m_lst.op(0), x2).evalf();
2866 std::vector<int> m_int;
2867 for (lst::const_iterator it = m_lst.begin(); it != m_lst.end(); it++) {
2868 m_int.push_back(ex_to<numeric>(*it).to_int());
2870 return numeric(H_do_sum(m_int, x));
2876 // ensure that the realpart of the argument is positive
2877 if (cln::realpart(x) < 0) {
2879 for (int i=0; i<m.nops(); i++) {
2881 m.let_op(i) = -m.op(i);
2887 // choose transformations
2888 symbol xtemp("xtemp");
2889 if (cln::abs(x-1) < 1.4142) {
2891 map_trafo_H_1mxt1px trafo;
2892 res *= trafo(H(m, xtemp));
2895 map_trafo_H_1overx trafo;
2896 res *= trafo(H(m, xtemp));
2897 if (cln::imagpart(x) <= 0) {
2898 res = res.subs(H_polesign == -I*Pi);
2900 res = res.subs(H_polesign == I*Pi);
2906 // map_trafo_H_convert converter;
2907 // res = converter(res).expand();
2909 // res.find(H(wild(1),wild(2)), ll);
2910 // res.find(zeta(wild(1)), ll);
2911 // res.find(zeta(wild(1),wild(2)), ll);
2912 // res = res.collect(ll);
2914 return res.subs(xtemp == numeric(x)).evalf();
2917 return H(x1,x2).hold();
2921 static ex H_eval(const ex& m_, const ex& x)
2924 if (is_a<lst>(m_)) {
2929 if (m.nops() == 0) {
2937 if (*m.begin() > _ex1) {
2943 } else if (*m.begin() < _ex_1) {
2949 } else if (*m.begin() == _ex0) {
2956 for (lst::const_iterator it = ++m.begin(); it != m.end(); it++) {
2957 if ((*it).info(info_flags::integer)) {
2968 } else if (*it < _ex_1) {
2988 } else if (step == 1) {
3000 // if some m_i is not an integer
3001 return H(m_, x).hold();
3004 if ((x == _ex1) && (*(--m.end()) != _ex0)) {
3005 return convert_H_to_zeta(m);
3011 return H(m_, x).hold();
3013 return pow(log(x), m.nops()) / factorial(m.nops());
3016 return pow(-pos1*log(1-pos1*x), m.nops()) / factorial(m.nops());
3018 } else if ((step == 1) && (pos1 == _ex0)){
3023 return pow(-1, p) * S(n, p, -x);
3029 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
3030 return H(m_, x).evalf();
3032 return H(m_, x).hold();
3036 static ex H_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
3039 seq.push_back(expair(H(m, x), 0));
3040 return pseries(rel, seq);
3044 static ex H_deriv(const ex& m_, const ex& x, unsigned deriv_param)
3046 GINAC_ASSERT(deriv_param < 2);
3047 if (deriv_param == 0) {
3051 if (is_a<lst>(m_)) {
3067 return 1/(1-x) * H(m, x);
3068 } else if (mb == _ex_1) {
3069 return 1/(1+x) * H(m, x);
3076 static void H_print_latex(const ex& m_, const ex& x, const print_context& c)
3079 if (is_a<lst>(m_)) {
3084 c.s << "\\mbox{H}_{";
3085 lst::const_iterator itm = m.begin();
3088 for (; itm != m.end(); itm++) {
3098 REGISTER_FUNCTION(H,
3099 evalf_func(H_evalf).
3101 series_func(H_series).
3102 derivative_func(H_deriv).
3103 print_func<print_latex>(H_print_latex).
3104 do_not_evalf_params());
3107 // takes a parameter list for H and returns an expression with corresponding multiple polylogarithms
3108 ex convert_H_to_Li(const ex& m, const ex& x)
3110 map_trafo_H_reduce_trailing_zeros filter;
3111 map_trafo_H_convert_to_Li filter2;
3113 return filter2(filter(H(m, x).hold()));
3115 return filter2(filter(H(lst(m), x).hold()));
3120 //////////////////////////////////////////////////////////////////////
3122 // Multiple zeta values zeta(x) and zeta(x,s)
3126 //////////////////////////////////////////////////////////////////////
3129 // anonymous namespace for helper functions
3133 // parameters and data for [Cra] algorithm
3134 const cln::cl_N lambda = cln::cl_N("319/320");
3137 std::vector<std::vector<cln::cl_N> > f_kj;
3138 std::vector<cln::cl_N> crB;
3139 std::vector<std::vector<cln::cl_N> > crG;
3140 std::vector<cln::cl_N> crX;
3143 void halfcyclic_convolute(const std::vector<cln::cl_N>& a, const std::vector<cln::cl_N>& b, std::vector<cln::cl_N>& c)
3145 const int size = a.size();
3146 for (int n=0; n<size; n++) {
3148 for (int m=0; m<=n; m++) {
3149 c[n] = c[n] + a[m]*b[n-m];
3156 void initcX(const std::vector<int>& s)
3158 const int k = s.size();
3164 for (int i=0; i<=L2; i++) {
3165 crB.push_back(bernoulli(i).to_cl_N() / cln::factorial(i));
3170 for (int m=0; m<k-1; m++) {
3171 std::vector<cln::cl_N> crGbuf;
3174 for (int i=0; i<=L2; i++) {
3175 crGbuf.push_back(cln::factorial(i + Sm - m - 2) / cln::factorial(i + Smp1 - m - 2));
3177 crG.push_back(crGbuf);
3182 for (int m=0; m<k-1; m++) {
3183 std::vector<cln::cl_N> Xbuf;
3184 for (int i=0; i<=L2; i++) {
3185 Xbuf.push_back(crX[i] * crG[m][i]);
3187 halfcyclic_convolute(Xbuf, crB, crX);
3193 cln::cl_N crandall_Y_loop(const cln::cl_N& Sqk)
3195 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3196 cln::cl_N factor = cln::expt(lambda, Sqk);
3197 cln::cl_N res = factor / Sqk * crX[0] * one;
3202 factor = factor * lambda;
3204 res = res + crX[N] * factor / (N+Sqk);
3205 } while ((res != resbuf) || cln::zerop(crX[N]));
3211 void calc_f(int maxr)
3216 cln::cl_N t0, t1, t2, t3, t4;
3218 std::vector<std::vector<cln::cl_N> >::iterator it = f_kj.begin();
3219 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3221 t0 = cln::exp(-lambda);
3223 for (k=1; k<=L1; k++) {
3226 for (j=1; j<=maxr; j++) {
3229 for (i=2; i<=j; i++) {
3233 (*it).push_back(t2 * t3 * cln::expt(cln::cl_I(k),-j) * one);
3241 cln::cl_N crandall_Z(const std::vector<int>& s)
3243 const int j = s.size();
3252 t0 = t0 + f_kj[q+j-2][s[0]-1];
3253 } while (t0 != t0buf);
3255 return t0 / cln::factorial(s[0]-1);
3258 std::vector<cln::cl_N> t(j);
3265 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),s[j-1]);
3266 for (int k=j-2; k>=1; k--) {
3267 t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), s[k]);
3269 t[0] = t[0] + t[1] * f_kj[q+j-2][s[0]-1];
3270 } while (t[0] != t0buf);
3272 return t[0] / cln::factorial(s[0]-1);
3277 cln::cl_N zeta_do_sum_Crandall(const std::vector<int>& s)
3279 std::vector<int> r = s;
3280 const int j = r.size();
3282 // decide on maximal size of f_kj for crandall_Z
3286 L1 = Digits * 3 + j*2;
3289 // decide on maximal size of crX for crandall_Y
3292 } else if (Digits < 86) {
3294 } else if (Digits < 192) {
3296 } else if (Digits < 394) {
3298 } else if (Digits < 808) {
3308 for (int i=0; i<j; i++) {
3317 const cln::cl_N r0factorial = cln::factorial(r[0]-1);
3319 std::vector<int> rz;
3322 for (int k=r.size()-1; k>0; k--) {
3324 rz.insert(rz.begin(), r.back());
3325 skp1buf = rz.front();
3331 for (int q=0; q<skp1buf; q++) {
3333 cln::cl_N pp1 = crandall_Y_loop(Srun+q-k);
3334 cln::cl_N pp2 = crandall_Z(rz);
3339 res = res - pp1 * pp2 / cln::factorial(q);
3341 res = res + pp1 * pp2 / cln::factorial(q);
3344 rz.front() = skp1buf;
3346 rz.insert(rz.begin(), r.back());
3350 res = (res + crandall_Y_loop(S-j)) / r0factorial + crandall_Z(rz);
3356 cln::cl_N zeta_do_sum_simple(const std::vector<int>& r)
3358 const int j = r.size();
3360 // buffer for subsums
3361 std::vector<cln::cl_N> t(j);
3362 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3369 t[j-1] = t[j-1] + one / cln::expt(cln::cl_I(q),r[j-1]);
3370 for (int k=j-2; k>=0; k--) {
3371 t[k] = t[k] + one * t[k+1] / cln::expt(cln::cl_I(q+j-1-k), r[k]);
3373 } while (t[0] != t0buf);
3379 // does Hoelder convolution. see [BBB] (7.0)
3380 cln::cl_N zeta_do_Hoelder_convolution(const std::vector<int>& m_, const std::vector<int>& s_)
3382 // prepare parameters
3383 // holds Li arguments in [BBB] notation
3384 std::vector<int> s = s_;
3385 std::vector<int> m_p = m_;
3386 std::vector<int> m_q;
3387 // holds Li arguments in nested sums notation
3388 std::vector<cln::cl_N> s_p(s.size(), cln::cl_N(1));
3389 s_p[0] = s_p[0] * cln::cl_N("1/2");
3390 // convert notations
3392 for (int i=0; i<s_.size(); i++) {
3397 s[i] = sig * std::abs(s[i]);
3399 std::vector<cln::cl_N> s_q;
3400 cln::cl_N signum = 1;
3403 cln::cl_N res = multipleLi_do_sum(m_p, s_p);
3408 // change parameters
3409 if (s.front() > 0) {
3410 if (m_p.front() == 1) {
3411 m_p.erase(m_p.begin());
3412 s_p.erase(s_p.begin());
3413 if (s_p.size() > 0) {
3414 s_p.front() = s_p.front() * cln::cl_N("1/2");
3420 m_q.insert(m_q.begin(), 1);
3421 if (s_q.size() > 0) {
3422 s_q.front() = s_q.front() * 2;
3424 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
3427 if (m_p.front() == 1) {
3428 m_p.erase(m_p.begin());
3429 cln::cl_N spbuf = s_p.front();
3430 s_p.erase(s_p.begin());
3431 if (s_p.size() > 0) {
3432 s_p.front() = s_p.front() * spbuf;
3435 m_q.insert(m_q.begin(), 1);
3436 if (s_q.size() > 0) {
3437 s_q.front() = s_q.front() * 4;
3439 s_q.insert(s_q.begin(), cln::cl_N("1/4"));
3443 m_q.insert(m_q.begin(), 1);
3444 if (s_q.size() > 0) {
3445 s_q.front() = s_q.front() * 2;
3447 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
3452 if (m_p.size() == 0) break;
3454 res = res + signum * multipleLi_do_sum(m_p, s_p) * multipleLi_do_sum(m_q, s_q);
3459 res = res + signum * multipleLi_do_sum(m_q, s_q);
3465 } // end of anonymous namespace
3468 //////////////////////////////////////////////////////////////////////
3470 // Multiple zeta values zeta(x)
3474 //////////////////////////////////////////////////////////////////////
3477 static ex zeta1_evalf(const ex& x)
3479 if (is_exactly_a<lst>(x) && (x.nops()>1)) {
3481 // multiple zeta value
3482 const int count = x.nops();
3483 const lst& xlst = ex_to<lst>(x);
3484 std::vector<int> r(count);
3486 // check parameters and convert them
3487 lst::const_iterator it1 = xlst.begin();
3488 std::vector<int>::iterator it2 = r.begin();
3490 if (!(*it1).info(info_flags::posint)) {
3491 return zeta(x).hold();
3493 *it2 = ex_to<numeric>(*it1).to_int();
3496 } while (it2 != r.end());
3498 // check for divergence
3500 return zeta(x).hold();
3503 // decide on summation algorithm
3504 // this is still a bit clumsy
3505 int limit = (Digits>17) ? 10 : 6;
3506 if ((r[0] < limit) || ((count > 3) && (r[1] < limit/2))) {
3507 return numeric(zeta_do_sum_Crandall(r));
3509 return numeric(zeta_do_sum_simple(r));
3513 // single zeta value
3514 if (is_exactly_a<numeric>(x) && (x != 1)) {
3516 return zeta(ex_to<numeric>(x));
3517 } catch (const dunno &e) { }
3520 return zeta(x).hold();
3524 static ex zeta1_eval(const ex& m)
3526 if (is_exactly_a<lst>(m)) {
3527 if (m.nops() == 1) {
3528 return zeta(m.op(0));
3530 return zeta(m).hold();
3533 if (m.info(info_flags::numeric)) {
3534 const numeric& y = ex_to<numeric>(m);
3535 // trap integer arguments:
3536 if (y.is_integer()) {
3540 if (y.is_equal(_num1)) {
3541 return zeta(m).hold();
3543 if (y.info(info_flags::posint)) {
3544 if (y.info(info_flags::odd)) {
3545 return zeta(m).hold();
3547 return abs(bernoulli(y)) * pow(Pi, y) * pow(_num2, y-_num1) / factorial(y);
3550 if (y.info(info_flags::odd)) {
3551 return -bernoulli(_num1-y) / (_num1-y);
3558 if (y.info(info_flags::numeric) && !y.info(info_flags::crational)) {
3559 return zeta1_evalf(m);
3562 return zeta(m).hold();
3566 static ex zeta1_deriv(const ex& m, unsigned deriv_param)
3568 GINAC_ASSERT(deriv_param==0);
3570 if (is_exactly_a<lst>(m)) {
3573 return zetaderiv(_ex1, m);
3578 static void zeta1_print_latex(const ex& m_, const print_context& c)
3581 if (is_a<lst>(m_)) {
3582 const lst& m = ex_to<lst>(m_);
3583 lst::const_iterator it = m.begin();
3586 for (; it != m.end(); it++) {
3597 unsigned zeta1_SERIAL::serial = function::register_new(function_options("zeta", 1).
3598 evalf_func(zeta1_evalf).
3599 eval_func(zeta1_eval).
3600 derivative_func(zeta1_deriv).
3601 print_func<print_latex>(zeta1_print_latex).
3602 do_not_evalf_params().
3606 //////////////////////////////////////////////////////////////////////
3608 // Alternating Euler sum zeta(x,s)
3612 //////////////////////////////////////////////////////////////////////
3615 static ex zeta2_evalf(const ex& x, const ex& s)
3617 if (is_exactly_a<lst>(x)) {
3619 // alternating Euler sum
3620 const int count = x.nops();
3621 const lst& xlst = ex_to<lst>(x);
3622 const lst& slst = ex_to<lst>(s);
3623 std::vector<int> xi(count);
3624 std::vector<int> si(count);
3626 // check parameters and convert them
3627 lst::const_iterator it_xread = xlst.begin();
3628 lst::const_iterator it_sread = slst.begin();
3629 std::vector<int>::iterator it_xwrite = xi.begin();
3630 std::vector<int>::iterator it_swrite = si.begin();
3632 if (!(*it_xread).info(info_flags::posint)) {
3633 return zeta(x, s).hold();
3635 *it_xwrite = ex_to<numeric>(*it_xread).to_int();
3636 if (*it_sread > 0) {
3645 } while (it_xwrite != xi.end());
3647 // check for divergence
3648 if ((xi[0] == 1) && (si[0] == 1)) {
3649 return zeta(x, s).hold();
3652 // use Hoelder convolution
3653 return numeric(zeta_do_Hoelder_convolution(xi, si));
3656 return zeta(x, s).hold();
3660 static ex zeta2_eval(const ex& m, const ex& s_)
3662 if (is_exactly_a<lst>(s_)) {
3663 const lst& s = ex_to<lst>(s_);
3664 for (lst::const_iterator it = s.begin(); it != s.end(); it++) {
3665 if ((*it).info(info_flags::positive)) {
3668 return zeta(m, s_).hold();
3671 } else if (s_.info(info_flags::positive)) {
3675 return zeta(m, s_).hold();
3679 static ex zeta2_deriv(const ex& m, const ex& s, unsigned deriv_param)
3681 GINAC_ASSERT(deriv_param==0);
3683 if (is_exactly_a<lst>(m)) {
3686 if ((is_exactly_a<lst>(s) && s.op(0).info(info_flags::positive)) || s.info(info_flags::positive)) {
3687 return zetaderiv(_ex1, m);
3694 static void zeta2_print_latex(const ex& m_, const ex& s_, const print_context& c)
3697 if (is_a<lst>(m_)) {
3703 if (is_a<lst>(s_)) {
3709 lst::const_iterator itm = m.begin();
3710 lst::const_iterator its = s.begin();
3712 c.s << "\\overline{";
3720 for (; itm != m.end(); itm++, its++) {
3723 c.s << "\\overline{";
3734 unsigned zeta2_SERIAL::serial = function::register_new(function_options("zeta", 2).
3735 evalf_func(zeta2_evalf).
3736 eval_func(zeta2_eval).
3737 derivative_func(zeta2_deriv).
3738 print_func<print_latex>(zeta2_print_latex).
3739 do_not_evalf_params().
3743 } // namespace GiNaC