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-2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
74 #include "operators.h"
77 #include "relational.h"
90 //////////////////////////////////////////////////////////////////////
92 // Classical polylogarithm Li(n,x)
96 //////////////////////////////////////////////////////////////////////
99 // anonymous namespace for helper functions
103 // lookup table for factors built from Bernoulli numbers
105 std::vector<std::vector<cln::cl_N> > Xn;
106 // initial size of Xn that should suffice for 32bit machines (must be even)
107 const int xninitsizestep = 26;
108 int xninitsize = xninitsizestep;
112 // This function calculates the X_n. The X_n are needed for speed up of classical polylogarithms.
113 // With these numbers the polylogs can be calculated as follows:
114 // Li_p (x) = \sum_{n=0}^\infty X_{p-2}(n) u^{n+1}/(n+1)! with u = -log(1-x)
115 // X_0(n) = B_n (Bernoulli numbers)
116 // X_p(n) = \sum_{k=0}^n binomial(n,k) B_{n-k} / (k+1) * X_{p-1}(k)
117 // The calculation of Xn depends on X0 and X{n-1}.
118 // X_0 is special, it holds only the non-zero Bernoulli numbers with index 2 or greater.
119 // This results in a slightly more complicated algorithm for the X_n.
120 // The first index in Xn corresponds to the index of the polylog minus 2.
121 // The second index in Xn corresponds to the index from the actual sum.
125 // calculate X_2 and higher (corresponding to Li_4 and higher)
126 std::vector<cln::cl_N> buf(xninitsize);
127 std::vector<cln::cl_N>::iterator it = buf.begin();
129 *it = -(cln::expt(cln::cl_I(2),n+1) - 1) / cln::expt(cln::cl_I(2),n+1); // i == 1
131 for (int i=2; i<=xninitsize; i++) {
133 result = 0; // k == 0
135 result = Xn[0][i/2-1]; // k == 0
137 for (int k=1; k<i-1; k++) {
138 if ( !(((i-k) & 1) && ((i-k) > 1)) ) {
139 result = result + cln::binomial(i,k) * Xn[0][(i-k)/2-1] * Xn[n-1][k-1] / (k+1);
142 result = result - cln::binomial(i,i-1) * Xn[n-1][i-2] / 2 / i; // k == i-1
143 result = result + Xn[n-1][i-1] / (i+1); // k == i
150 // special case to handle the X_0 correct
151 std::vector<cln::cl_N> buf(xninitsize);
152 std::vector<cln::cl_N>::iterator it = buf.begin();
154 *it = cln::cl_I(-3)/cln::cl_I(4); // i == 1
156 *it = cln::cl_I(17)/cln::cl_I(36); // i == 2
158 for (int i=3; i<=xninitsize; i++) {
160 result = -Xn[0][(i-3)/2]/2;
161 *it = (cln::binomial(i,1)/cln::cl_I(2) + cln::binomial(i,i-1)/cln::cl_I(i))*result;
164 result = Xn[0][i/2-1] + Xn[0][i/2-1]/(i+1);
165 for (int k=1; k<i/2; k++) {
166 result = result + cln::binomial(i,k*2) * Xn[0][k-1] * Xn[0][i/2-k-1] / (k*2+1);
175 std::vector<cln::cl_N> buf(xninitsize/2);
176 std::vector<cln::cl_N>::iterator it = buf.begin();
177 for (int i=1; i<=xninitsize/2; i++) {
178 *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 (size_t 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 const cln::cl_N S_num(int n, int p, const cln::cl_N& 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) - S_num(1, n-j-1, 1-x))
374 * cln::expt(cln::log(x), j) / cln::factorial(j);
381 // helper function for classical polylog Li
382 const cln::cl_N Lin_numeric(const int n, const cln::cl_N& x)
386 return -cln::log(1-x);
397 return -(1-cln::expt(cln::cl_I(2),1-n)) * cln::zeta(n);
399 if (cln::abs(realpart(x)) < 0.4 && cln::abs(cln::abs(x)-1) < 0.01) {
400 cln::cl_N result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
401 for (int j=0; j<n-1; j++) {
402 result = result + (S_num(n-j-1, 1, 1) - S_num(1, n-j-1, 1-x))
403 * cln::expt(cln::log(x), j) / cln::factorial(j);
408 // what is the desired float format?
409 // first guess: default format
410 cln::float_format_t prec = cln::default_float_format;
411 const cln::cl_N value = x;
412 // second guess: the argument's format
413 if (!instanceof(realpart(x), cln::cl_RA_ring))
414 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
415 else if (!instanceof(imagpart(x), cln::cl_RA_ring))
416 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
419 if (cln::abs(value) > 1) {
420 cln::cl_N result = -cln::expt(cln::log(-value),n) / cln::factorial(n);
421 // check if argument is complex. if it is real, the new polylog has to be conjugated.
422 if (cln::zerop(cln::imagpart(value))) {
424 result = result + conjugate(Li_projection(n, cln::recip(value), prec));
427 result = result - conjugate(Li_projection(n, cln::recip(value), prec));
432 result = result + Li_projection(n, cln::recip(value), prec);
435 result = result - Li_projection(n, cln::recip(value), prec);
439 for (int j=0; j<n-1; j++) {
440 add = add + (1+cln::expt(cln::cl_I(-1),n-j)) * (1-cln::expt(cln::cl_I(2),1-n+j))
441 * Lin_numeric(n-j,1) * cln::expt(cln::log(-value),j) / cln::factorial(j);
443 result = result - add;
447 return Li_projection(n, value, prec);
452 } // end of anonymous namespace
455 //////////////////////////////////////////////////////////////////////
457 // Multiple polylogarithm Li(n,x)
461 //////////////////////////////////////////////////////////////////////
464 // anonymous namespace for helper function
468 // performs the actual series summation for multiple polylogarithms
469 cln::cl_N multipleLi_do_sum(const std::vector<int>& s, const std::vector<cln::cl_N>& x)
471 // ensure all x <> 0.
472 for (std::vector<cln::cl_N>::const_iterator it = x.begin(); it != x.end(); ++it) {
473 if ( *it == 0 ) return cln::cl_float(0, cln::float_format(Digits));
476 const int j = s.size();
477 bool flag_accidental_zero = false;
479 std::vector<cln::cl_N> t(j);
480 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
487 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
488 for (int k=j-2; k>=0; k--) {
489 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]);
492 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
493 for (int k=j-2; k>=0; k--) {
494 flag_accidental_zero = cln::zerop(t[k+1]);
495 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]);
497 } while ( (t[0] != t0buf) || cln::zerop(t[0]) || flag_accidental_zero );
503 // forward declaration for Li_eval()
504 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf);
507 // type used by the transformation functions for G
508 typedef std::vector<int> Gparameter;
511 // G_eval1-function for G transformations
512 ex G_eval1(int a, int scale, const exvector& gsyms)
515 const ex& scs = gsyms[std::abs(scale)];
516 const ex& as = gsyms[std::abs(a)];
518 return -log(1 - scs/as);
523 return log(gsyms[std::abs(scale)]);
528 // G_eval-function for G transformations
529 ex G_eval(const Gparameter& a, int scale, const exvector& gsyms)
531 // check for properties of G
532 ex sc = gsyms[std::abs(scale)];
534 bool all_zero = true;
535 bool all_ones = true;
537 for (Gparameter::const_iterator it = a.begin(); it != a.end(); ++it) {
539 const ex sym = gsyms[std::abs(*it)];
553 // care about divergent G: shuffle to separate divergencies that will be canceled
554 // later on in the transformation
555 if (newa.nops() > 1 && newa.op(0) == sc && !all_ones && a.front()!=0) {
558 Gparameter::const_iterator it = a.begin();
560 for (; it != a.end(); ++it) {
561 short_a.push_back(*it);
563 ex result = G_eval1(a.front(), scale, gsyms) * G_eval(short_a, scale, gsyms);
564 it = short_a.begin();
565 for (int i=1; i<count_ones; ++i) {
568 for (; it != short_a.end(); ++it) {
571 Gparameter::const_iterator it2 = short_a.begin();
572 for (; it2 != it; ++it2) {
573 newa.push_back(*it2);
576 newa.push_back(a[0]);
579 for (; it2 != short_a.end(); ++it2) {
580 newa.push_back(*it2);
582 result -= G_eval(newa, scale, gsyms);
584 return result / count_ones;
587 // G({1,...,1};y) -> G({1};y)^k / k!
588 if (all_ones && a.size() > 1) {
589 return pow(G_eval1(a.front(),scale, gsyms), count_ones) / factorial(count_ones);
592 // G({0,...,0};y) -> log(y)^k / k!
594 return pow(log(gsyms[std::abs(scale)]), a.size()) / factorial(a.size());
597 // no special cases anymore -> convert it into Li
600 ex argbuf = gsyms[std::abs(scale)];
602 for (Gparameter::const_iterator it=a.begin(); it!=a.end(); ++it) {
604 const ex& sym = gsyms[std::abs(*it)];
605 x.append(argbuf / sym);
613 return pow(-1, x.nops()) * Li(m, x);
617 // converts data for G: pending_integrals -> a
618 Gparameter convert_pending_integrals_G(const Gparameter& pending_integrals)
620 GINAC_ASSERT(pending_integrals.size() != 1);
622 if (pending_integrals.size() > 0) {
623 // get rid of the first element, which would stand for the new upper limit
624 Gparameter new_a(pending_integrals.begin()+1, pending_integrals.end());
627 // just return empty parameter list
634 // check the parameters a and scale for G and return information about convergence, depth, etc.
635 // convergent : true if G(a,scale) is convergent
636 // depth : depth of G(a,scale)
637 // trailing_zeros : number of trailing zeros of a
638 // min_it : iterator of a pointing on the smallest element in a
639 Gparameter::const_iterator check_parameter_G(const Gparameter& a, int scale,
640 bool& convergent, int& depth, int& trailing_zeros, Gparameter::const_iterator& min_it)
646 Gparameter::const_iterator lastnonzero = a.end();
647 for (Gparameter::const_iterator it = a.begin(); it != a.end(); ++it) {
648 if (std::abs(*it) > 0) {
652 if (std::abs(*it) < scale) {
654 if ((min_it == a.end()) || (std::abs(*it) < std::abs(*min_it))) {
662 return ++lastnonzero;
666 // add scale to pending_integrals if pending_integrals is empty
667 Gparameter prepare_pending_integrals(const Gparameter& pending_integrals, int scale)
669 GINAC_ASSERT(pending_integrals.size() != 1);
671 if (pending_integrals.size() > 0) {
672 return pending_integrals;
674 Gparameter new_pending_integrals;
675 new_pending_integrals.push_back(scale);
676 return new_pending_integrals;
681 // handles trailing zeroes for an otherwise convergent integral
682 ex trailing_zeros_G(const Gparameter& a, int scale, const exvector& gsyms)
685 int depth, trailing_zeros;
686 Gparameter::const_iterator last, dummyit;
687 last = check_parameter_G(a, scale, convergent, depth, trailing_zeros, dummyit);
689 GINAC_ASSERT(convergent);
691 if ((trailing_zeros > 0) && (depth > 0)) {
693 Gparameter new_a(a.begin(), a.end()-1);
694 result += G_eval1(0, scale, gsyms) * trailing_zeros_G(new_a, scale, gsyms);
695 for (Gparameter::const_iterator it = a.begin(); it != last; ++it) {
696 Gparameter new_a(a.begin(), it);
698 new_a.insert(new_a.end(), it, a.end()-1);
699 result -= trailing_zeros_G(new_a, scale, gsyms);
702 return result / trailing_zeros;
704 return G_eval(a, scale, gsyms);
709 // G transformation [VSW] (57),(58)
710 ex depth_one_trafo_G(const Gparameter& pending_integrals, const Gparameter& a, int scale, const exvector& gsyms)
712 // pendint = ( y1, b1, ..., br )
713 // a = ( 0, ..., 0, amin )
716 // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(0, ..., 0, sr; y2)
717 // where sr replaces amin
719 GINAC_ASSERT(a.back() != 0);
720 GINAC_ASSERT(a.size() > 0);
723 Gparameter new_pending_integrals = prepare_pending_integrals(pending_integrals, std::abs(a.back()));
724 const int psize = pending_integrals.size();
727 // G(sr_{+-}; y2 ) = G(y2_{-+}; sr) - G(0; sr) + ln(-y2_{-+})
732 result += log(gsyms[ex_to<numeric>(scale).to_int()]);
734 new_pending_integrals.push_back(-scale);
737 new_pending_integrals.push_back(scale);
741 result *= trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
742 pending_integrals.front(),
747 result += trailing_zeros_G(convert_pending_integrals_G(new_pending_integrals),
748 new_pending_integrals.front(),
752 new_pending_integrals.back() = 0;
753 result -= trailing_zeros_G(convert_pending_integrals_G(new_pending_integrals),
754 new_pending_integrals.front(),
761 // G_m(sr_{+-}; y2) = -zeta_m + int_0^y2 dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
762 // - int_0^sr dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
765 result -= zeta(a.size());
767 result *= trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
768 pending_integrals.front(),
772 // term int_0^sr dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
773 // = int_0^sr dt/t G_{m-1}( t_{+-}; y2 )
774 Gparameter new_a(a.begin()+1, a.end());
775 new_pending_integrals.push_back(0);
776 result -= depth_one_trafo_G(new_pending_integrals, new_a, scale, gsyms);
778 // term int_0^y2 dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
779 // = int_0^y2 dt/t G_{m-1}( t_{+-}; y2 )
780 Gparameter new_pending_integrals_2;
781 new_pending_integrals_2.push_back(scale);
782 new_pending_integrals_2.push_back(0);
784 result += trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
785 pending_integrals.front(),
787 * depth_one_trafo_G(new_pending_integrals_2, new_a, scale, gsyms);
789 result += depth_one_trafo_G(new_pending_integrals_2, new_a, scale, gsyms);
796 // forward declaration
797 ex shuffle_G(const Gparameter & a0, const Gparameter & a1, const Gparameter & a2,
798 const Gparameter& pendint, const Gparameter& a_old, int scale,
799 const exvector& gsyms);
802 // G transformation [VSW]
803 ex G_transform(const Gparameter& pendint, const Gparameter& a, int scale,
804 const exvector& gsyms)
806 // main recursion routine
808 // pendint = ( y1, b1, ..., br )
809 // a = ( a1, ..., amin, ..., aw )
812 // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,sr,...,aw,y2)
813 // where sr replaces amin
815 // find smallest alpha, determine depth and trailing zeros, and check for convergence
817 int depth, trailing_zeros;
818 Gparameter::const_iterator min_it;
819 Gparameter::const_iterator firstzero =
820 check_parameter_G(a, scale, convergent, depth, trailing_zeros, min_it);
821 int min_it_pos = min_it - a.begin();
823 // special case: all a's are zero
830 result = G_eval(a, scale, gsyms);
832 if (pendint.size() > 0) {
833 result *= trailing_zeros_G(convert_pending_integrals_G(pendint),
840 // handle trailing zeros
841 if (trailing_zeros > 0) {
843 Gparameter new_a(a.begin(), a.end()-1);
844 result += G_eval1(0, scale, gsyms) * G_transform(pendint, new_a, scale, gsyms);
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, gsyms);
851 return result / trailing_zeros;
856 if (pendint.size() > 0) {
857 return G_eval(convert_pending_integrals_G(pendint),
858 pendint.front(), gsyms)*
859 G_eval(a, scale, gsyms);
861 return G_eval(a, scale, gsyms);
865 // call basic transformation for depth equal one
867 return depth_one_trafo_G(pendint, a, scale, gsyms);
871 // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,sr,...,aw,y2)
872 // = int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,0,...,aw,y2)
873 // + 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)
875 // smallest element in last place
876 if (min_it + 1 == a.end()) {
877 do { --min_it; } while (*min_it == 0);
879 Gparameter a1(a.begin(),min_it+1);
880 Gparameter a2(min_it+1,a.end());
882 ex result = G_transform(pendint, a2, scale, gsyms)*
883 G_transform(empty, a1, scale, gsyms);
885 result -= shuffle_G(empty, a1, a2, pendint, a, scale, gsyms);
890 Gparameter::iterator changeit;
892 // first term G(a_1,..,0,...,a_w;a_0)
893 Gparameter new_pendint = prepare_pending_integrals(pendint, a[min_it_pos]);
894 Gparameter new_a = a;
895 new_a[min_it_pos] = 0;
896 ex result = G_transform(empty, new_a, scale, gsyms);
897 if (pendint.size() > 0) {
898 result *= trailing_zeros_G(convert_pending_integrals_G(pendint),
899 pendint.front(), gsyms);
903 changeit = new_a.begin() + min_it_pos;
904 changeit = new_a.erase(changeit);
905 if (changeit != new_a.begin()) {
906 // smallest in the middle
907 new_pendint.push_back(*changeit);
908 result -= trailing_zeros_G(convert_pending_integrals_G(new_pendint),
909 new_pendint.front(), gsyms)*
910 G_transform(empty, new_a, scale, gsyms);
911 int buffer = *changeit;
913 result += G_transform(new_pendint, new_a, scale, gsyms);
915 new_pendint.pop_back();
917 new_pendint.push_back(*changeit);
918 result += trailing_zeros_G(convert_pending_integrals_G(new_pendint),
919 new_pendint.front(), gsyms)*
920 G_transform(empty, new_a, scale, gsyms);
922 result -= G_transform(new_pendint, new_a, scale, gsyms);
924 // smallest at the front
925 new_pendint.push_back(scale);
926 result += trailing_zeros_G(convert_pending_integrals_G(new_pendint),
927 new_pendint.front(), gsyms)*
928 G_transform(empty, new_a, scale, gsyms);
929 new_pendint.back() = *changeit;
930 result -= trailing_zeros_G(convert_pending_integrals_G(new_pendint),
931 new_pendint.front(), gsyms)*
932 G_transform(empty, new_a, scale, gsyms);
934 result += G_transform(new_pendint, new_a, scale, gsyms);
940 // shuffles the two parameter list a1 and a2 and calls G_transform for every term except
941 // for the one that is equal to a_old
942 ex shuffle_G(const Gparameter & a0, const Gparameter & a1, const Gparameter & a2,
943 const Gparameter& pendint, const Gparameter& a_old, int scale,
944 const exvector& gsyms)
946 if (a1.size()==0 && a2.size()==0) {
947 // veto the one configuration we don't want
948 if ( a0 == a_old ) return 0;
950 return G_transform(pendint, a0, scale, gsyms);
956 aa0.insert(aa0.end(),a1.begin(),a1.end());
957 return shuffle_G(aa0, empty, empty, pendint, a_old, scale, gsyms);
963 aa0.insert(aa0.end(),a2.begin(),a2.end());
964 return shuffle_G(aa0, empty, empty, pendint, a_old, scale, gsyms);
967 Gparameter a1_removed(a1.begin()+1,a1.end());
968 Gparameter a2_removed(a2.begin()+1,a2.end());
973 a01.push_back( a1[0] );
974 a02.push_back( a2[0] );
976 return shuffle_G(a01, a1_removed, a2, pendint, a_old, scale, gsyms)
977 + shuffle_G(a02, a1, a2_removed, pendint, a_old, scale, gsyms);
980 // handles the transformations and the numerical evaluation of G
981 // the parameter x, s and y must only contain numerics
983 G_numeric(const std::vector<cln::cl_N>& x, const std::vector<int>& s,
986 // do acceleration transformation (hoelder convolution [BBB])
987 // the parameter x, s and y must only contain numerics
989 G_do_hoelder(std::vector<cln::cl_N> x, /* yes, it's passed by value */
990 const std::vector<int>& s, const cln::cl_N& y)
993 const std::size_t size = x.size();
994 for (std::size_t i = 0; i < size; ++i)
997 for (std::size_t r = 0; r <= size; ++r) {
998 cln::cl_N buffer(1 & r ? -1 : 1);
1003 for (std::size_t i = 0; i < size; ++i) {
1004 if (x[i] == cln::cl_RA(1)/p) {
1005 p = p/2 + cln::cl_RA(3)/2;
1011 cln::cl_RA q = p/(p-1);
1012 std::vector<cln::cl_N> qlstx;
1013 std::vector<int> qlsts;
1014 for (std::size_t j = r; j >= 1; --j) {
1015 qlstx.push_back(cln::cl_N(1) - x[j-1]);
1016 if (instanceof(x[j-1], cln::cl_R_ring) &&
1017 realpart(x[j-1]) > 1 && realpart(x[j-1]) <= 2) {
1018 qlsts.push_back(s[j-1]);
1020 qlsts.push_back(-s[j-1]);
1023 if (qlstx.size() > 0) {
1024 buffer = buffer*G_numeric(qlstx, qlsts, 1/q);
1026 std::vector<cln::cl_N> plstx;
1027 std::vector<int> plsts;
1028 for (std::size_t j = r+1; j <= size; ++j) {
1029 plstx.push_back(x[j-1]);
1030 plsts.push_back(s[j-1]);
1032 if (plstx.size() > 0) {
1033 buffer = buffer*G_numeric(plstx, plsts, 1/p);
1035 result = result + buffer;
1040 // convergence transformation, used for numerical evaluation of G function.
1041 // the parameter x, s and y must only contain numerics
1043 G_do_trafo(const std::vector<cln::cl_N>& x, const std::vector<int>& s,
1046 // sort (|x|<->position) to determine indices
1047 typedef std::multimap<cln::cl_R, std::size_t> sortmap_t;
1049 std::size_t size = 0;
1050 for (std::size_t i = 0; i < x.size(); ++i) {
1052 sortmap.insert(std::make_pair(abs(x[i]), i));
1056 // include upper limit (scale)
1057 sortmap.insert(std::make_pair(abs(y), x.size()));
1059 // generate missing dummy-symbols
1061 // holding dummy-symbols for the G/Li transformations
1063 gsyms.push_back(symbol("GSYMS_ERROR"));
1064 cln::cl_N lastentry(0);
1065 for (sortmap_t::const_iterator it = sortmap.begin(); it != sortmap.end(); ++it) {
1066 if (it != sortmap.begin()) {
1067 if (it->second < x.size()) {
1068 if (x[it->second] == lastentry) {
1069 gsyms.push_back(gsyms.back());
1073 if (y == lastentry) {
1074 gsyms.push_back(gsyms.back());
1079 std::ostringstream os;
1081 gsyms.push_back(symbol(os.str()));
1083 if (it->second < x.size()) {
1084 lastentry = x[it->second];
1090 // fill position data according to sorted indices and prepare substitution list
1091 Gparameter a(x.size());
1093 std::size_t pos = 1;
1095 for (sortmap_t::const_iterator it = sortmap.begin(); it != sortmap.end(); ++it) {
1096 if (it->second < x.size()) {
1097 if (s[it->second] > 0) {
1098 a[it->second] = pos;
1100 a[it->second] = -int(pos);
1102 subslst[gsyms[pos]] = numeric(x[it->second]);
1105 subslst[gsyms[pos]] = numeric(y);
1110 // do transformation
1112 ex result = G_transform(pendint, a, scale, gsyms);
1113 // replace dummy symbols with their values
1114 result = result.eval().expand();
1115 result = result.subs(subslst).evalf();
1116 if (!is_a<numeric>(result))
1117 throw std::logic_error("G_do_trafo: G_transform returned non-numeric result");
1119 cln::cl_N ret = ex_to<numeric>(result).to_cl_N();
1123 // handles the transformations and the numerical evaluation of G
1124 // the parameter x, s and y must only contain numerics
1126 G_numeric(const std::vector<cln::cl_N>& x, const std::vector<int>& s,
1129 // check for convergence and necessary accelerations
1130 bool need_trafo = false;
1131 bool need_hoelder = false;
1132 std::size_t depth = 0;
1133 for (std::size_t i = 0; i < x.size(); ++i) {
1136 const cln::cl_N x_y = abs(x[i]) - y;
1137 if (instanceof(x_y, cln::cl_R_ring) &&
1138 realpart(x_y) < cln::least_negative_float(cln::float_format(Digits - 2)))
1141 if (abs(abs(x[i]/y) - 1) < 0.01)
1142 need_hoelder = true;
1145 if (zerop(x[x.size() - 1]))
1148 if (depth == 1 && x.size() == 2 && !need_trafo)
1149 return - Li_projection(2, y/x[1], cln::float_format(Digits));
1151 // do acceleration transformation (hoelder convolution [BBB])
1153 return G_do_hoelder(x, s, y);
1155 // convergence transformation
1157 return G_do_trafo(x, s, y);
1160 std::vector<cln::cl_N> newx;
1161 newx.reserve(x.size());
1163 m.reserve(x.size());
1166 cln::cl_N factor = y;
1167 for (std::size_t i = 0; i < x.size(); ++i) {
1171 newx.push_back(factor/x[i]);
1173 m.push_back(mcount);
1179 return sign*multipleLi_do_sum(m, newx);
1183 ex mLi_numeric(const lst& m, const lst& x)
1185 // let G_numeric do the transformation
1186 std::vector<cln::cl_N> newx;
1187 newx.reserve(x.nops());
1189 s.reserve(x.nops());
1190 cln::cl_N factor(1);
1191 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1192 for (int i = 1; i < *itm; ++i) {
1193 newx.push_back(cln::cl_N(0));
1196 const cln::cl_N xi = ex_to<numeric>(*itx).to_cl_N();
1197 newx.push_back(factor/xi);
1201 return numeric(cln::cl_N(1 & m.nops() ? - 1 : 1)*G_numeric(newx, s, cln::cl_N(1)));
1205 } // end of anonymous namespace
1208 //////////////////////////////////////////////////////////////////////
1210 // Generalized multiple polylogarithm G(x, y) and G(x, s, y)
1214 //////////////////////////////////////////////////////////////////////
1217 static ex G2_evalf(const ex& x_, const ex& y)
1219 if (!y.info(info_flags::positive)) {
1220 return G(x_, y).hold();
1222 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1223 if (x.nops() == 0) {
1227 return G(x_, y).hold();
1230 s.reserve(x.nops());
1231 bool all_zero = true;
1232 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1233 if (!(*it).info(info_flags::numeric)) {
1234 return G(x_, y).hold();
1239 if ( !ex_to<numeric>(*it).is_real() && ex_to<numeric>(*it).imag() < 0 ) {
1247 return pow(log(y), x.nops()) / factorial(x.nops());
1249 std::vector<cln::cl_N> xv;
1250 xv.reserve(x.nops());
1251 for (lst::const_iterator it = x.begin(); it != x.end(); ++it)
1252 xv.push_back(ex_to<numeric>(*it).to_cl_N());
1253 cln::cl_N result = G_numeric(xv, s, ex_to<numeric>(y).to_cl_N());
1254 return numeric(result);
1258 static ex G2_eval(const ex& x_, const ex& y)
1260 //TODO eval to MZV or H or S or Lin
1262 if (!y.info(info_flags::positive)) {
1263 return G(x_, y).hold();
1265 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1266 if (x.nops() == 0) {
1270 return G(x_, y).hold();
1273 s.reserve(x.nops());
1274 bool all_zero = true;
1275 bool crational = true;
1276 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1277 if (!(*it).info(info_flags::numeric)) {
1278 return G(x_, y).hold();
1280 if (!(*it).info(info_flags::crational)) {
1286 if ( !ex_to<numeric>(*it).is_real() && ex_to<numeric>(*it).imag() < 0 ) {
1294 return pow(log(y), x.nops()) / factorial(x.nops());
1296 if (!y.info(info_flags::crational)) {
1300 return G(x_, y).hold();
1302 std::vector<cln::cl_N> xv;
1303 xv.reserve(x.nops());
1304 for (lst::const_iterator it = x.begin(); it != x.end(); ++it)
1305 xv.push_back(ex_to<numeric>(*it).to_cl_N());
1306 cln::cl_N result = G_numeric(xv, s, ex_to<numeric>(y).to_cl_N());
1307 return numeric(result);
1311 unsigned G2_SERIAL::serial = function::register_new(function_options("G", 2).
1312 evalf_func(G2_evalf).
1314 do_not_evalf_params().
1317 // derivative_func(G2_deriv).
1318 // print_func<print_latex>(G2_print_latex).
1321 static ex G3_evalf(const ex& x_, const ex& s_, const ex& y)
1323 if (!y.info(info_flags::positive)) {
1324 return G(x_, s_, y).hold();
1326 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1327 lst s = is_a<lst>(s_) ? ex_to<lst>(s_) : lst(s_);
1328 if (x.nops() != s.nops()) {
1329 return G(x_, s_, y).hold();
1331 if (x.nops() == 0) {
1335 return G(x_, s_, y).hold();
1337 std::vector<int> sn;
1338 sn.reserve(s.nops());
1339 bool all_zero = true;
1340 for (lst::const_iterator itx = x.begin(), its = s.begin(); itx != x.end(); ++itx, ++its) {
1341 if (!(*itx).info(info_flags::numeric)) {
1342 return G(x_, y).hold();
1344 if (!(*its).info(info_flags::real)) {
1345 return G(x_, y).hold();
1350 if ( ex_to<numeric>(*itx).is_real() ) {
1359 if ( ex_to<numeric>(*itx).imag() > 0 ) {
1368 return pow(log(y), x.nops()) / factorial(x.nops());
1370 std::vector<cln::cl_N> xn;
1371 xn.reserve(x.nops());
1372 for (lst::const_iterator it = x.begin(); it != x.end(); ++it)
1373 xn.push_back(ex_to<numeric>(*it).to_cl_N());
1374 cln::cl_N result = G_numeric(xn, sn, ex_to<numeric>(y).to_cl_N());
1375 return numeric(result);
1379 static ex G3_eval(const ex& x_, const ex& s_, const ex& y)
1381 //TODO eval to MZV or H or S or Lin
1383 if (!y.info(info_flags::positive)) {
1384 return G(x_, s_, y).hold();
1386 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1387 lst s = is_a<lst>(s_) ? ex_to<lst>(s_) : lst(s_);
1388 if (x.nops() != s.nops()) {
1389 return G(x_, s_, y).hold();
1391 if (x.nops() == 0) {
1395 return G(x_, s_, y).hold();
1397 std::vector<int> sn;
1398 sn.reserve(s.nops());
1399 bool all_zero = true;
1400 bool crational = true;
1401 for (lst::const_iterator itx = x.begin(), its = s.begin(); itx != x.end(); ++itx, ++its) {
1402 if (!(*itx).info(info_flags::numeric)) {
1403 return G(x_, s_, y).hold();
1405 if (!(*its).info(info_flags::real)) {
1406 return G(x_, s_, y).hold();
1408 if (!(*itx).info(info_flags::crational)) {
1414 if ( ex_to<numeric>(*itx).is_real() ) {
1423 if ( ex_to<numeric>(*itx).imag() > 0 ) {
1432 return pow(log(y), x.nops()) / factorial(x.nops());
1434 if (!y.info(info_flags::crational)) {
1438 return G(x_, s_, y).hold();
1440 std::vector<cln::cl_N> xn;
1441 xn.reserve(x.nops());
1442 for (lst::const_iterator it = x.begin(); it != x.end(); ++it)
1443 xn.push_back(ex_to<numeric>(*it).to_cl_N());
1444 cln::cl_N result = G_numeric(xn, sn, ex_to<numeric>(y).to_cl_N());
1445 return numeric(result);
1449 unsigned G3_SERIAL::serial = function::register_new(function_options("G", 3).
1450 evalf_func(G3_evalf).
1452 do_not_evalf_params().
1455 // derivative_func(G3_deriv).
1456 // print_func<print_latex>(G3_print_latex).
1459 //////////////////////////////////////////////////////////////////////
1461 // Classical polylogarithm and multiple polylogarithm Li(m,x)
1465 //////////////////////////////////////////////////////////////////////
1468 static ex Li_evalf(const ex& m_, const ex& x_)
1470 // classical polylogs
1471 if (m_.info(info_flags::posint)) {
1472 if (x_.info(info_flags::numeric)) {
1473 int m__ = ex_to<numeric>(m_).to_int();
1474 const cln::cl_N x__ = ex_to<numeric>(x_).to_cl_N();
1475 const cln::cl_N result = Lin_numeric(m__, x__);
1476 return numeric(result);
1478 // try to numerically evaluate second argument
1479 ex x_val = x_.evalf();
1480 if (x_val.info(info_flags::numeric)) {
1481 int m__ = ex_to<numeric>(m_).to_int();
1482 const cln::cl_N x__ = ex_to<numeric>(x_val).to_cl_N();
1483 const cln::cl_N result = Lin_numeric(m__, x__);
1484 return numeric(result);
1488 // multiple polylogs
1489 if (is_a<lst>(m_) && is_a<lst>(x_)) {
1491 const lst& m = ex_to<lst>(m_);
1492 const lst& x = ex_to<lst>(x_);
1493 if (m.nops() != x.nops()) {
1494 return Li(m_,x_).hold();
1496 if (x.nops() == 0) {
1499 if ((m.op(0) == _ex1) && (x.op(0) == _ex1)) {
1500 return Li(m_,x_).hold();
1503 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1504 if (!(*itm).info(info_flags::posint)) {
1505 return Li(m_, x_).hold();
1507 if (!(*itx).info(info_flags::numeric)) {
1508 return Li(m_, x_).hold();
1515 return mLi_numeric(m, x);
1518 return Li(m_,x_).hold();
1522 static ex Li_eval(const ex& m_, const ex& x_)
1524 if (is_a<lst>(m_)) {
1525 if (is_a<lst>(x_)) {
1526 // multiple polylogs
1527 const lst& m = ex_to<lst>(m_);
1528 const lst& x = ex_to<lst>(x_);
1529 if (m.nops() != x.nops()) {
1530 return Li(m_,x_).hold();
1532 if (x.nops() == 0) {
1536 bool is_zeta = true;
1537 bool do_evalf = true;
1538 bool crational = true;
1539 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1540 if (!(*itm).info(info_flags::posint)) {
1541 return Li(m_,x_).hold();
1543 if ((*itx != _ex1) && (*itx != _ex_1)) {
1544 if (itx != x.begin()) {
1552 if (!(*itx).info(info_flags::numeric)) {
1555 if (!(*itx).info(info_flags::crational)) {
1564 lst newm = convert_parameter_Li_to_H(m, x, prefactor);
1565 return prefactor * H(newm, x[0]);
1567 if (do_evalf && !crational) {
1568 return mLi_numeric(m,x);
1571 return Li(m_, x_).hold();
1572 } else if (is_a<lst>(x_)) {
1573 return Li(m_, x_).hold();
1576 // classical polylogs
1584 return (pow(2,1-m_)-1) * zeta(m_);
1590 if (x_.is_equal(I)) {
1591 return power(Pi,_ex2)/_ex_48 + Catalan*I;
1593 if (x_.is_equal(-I)) {
1594 return power(Pi,_ex2)/_ex_48 - Catalan*I;
1597 if (m_.info(info_flags::posint) && x_.info(info_flags::numeric) && !x_.info(info_flags::crational)) {
1598 int m__ = ex_to<numeric>(m_).to_int();
1599 const cln::cl_N x__ = ex_to<numeric>(x_).to_cl_N();
1600 const cln::cl_N result = Lin_numeric(m__, x__);
1601 return numeric(result);
1604 return Li(m_, x_).hold();
1608 static ex Li_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
1610 if (is_a<lst>(m) || is_a<lst>(x)) {
1613 seq.push_back(expair(Li(m, x), 0));
1614 return pseries(rel, seq);
1617 // classical polylog
1618 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1619 if (m.info(info_flags::numeric) && x_pt.info(info_flags::numeric)) {
1620 // First special case: x==0 (derivatives have poles)
1621 if (x_pt.is_zero()) {
1624 // manually construct the primitive expansion
1625 for (int i=1; i<order; ++i)
1626 ser += pow(s,i) / pow(numeric(i), m);
1627 // substitute the argument's series expansion
1628 ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
1629 // maybe that was terminating, so add a proper order term
1631 nseq.push_back(expair(Order(_ex1), order));
1632 ser += pseries(rel, nseq);
1633 // reexpanding it will collapse the series again
1634 return ser.series(rel, order);
1636 // TODO special cases: x==1 (branch point) and x real, >=1 (branch cut)
1637 throw std::runtime_error("Li_series: don't know how to do the series expansion at this point!");
1639 // all other cases should be safe, by now:
1640 throw do_taylor(); // caught by function::series()
1644 static ex Li_deriv(const ex& m_, const ex& x_, unsigned deriv_param)
1646 GINAC_ASSERT(deriv_param < 2);
1647 if (deriv_param == 0) {
1650 if (m_.nops() > 1) {
1651 throw std::runtime_error("don't know how to derivate multiple polylogarithm!");
1654 if (is_a<lst>(m_)) {
1660 if (is_a<lst>(x_)) {
1666 return Li(m-1, x) / x;
1673 static void Li_print_latex(const ex& m_, const ex& x_, const print_context& c)
1676 if (is_a<lst>(m_)) {
1682 if (is_a<lst>(x_)) {
1687 c.s << "\\mbox{Li}_{";
1688 lst::const_iterator itm = m.begin();
1691 for (; itm != m.end(); itm++) {
1696 lst::const_iterator itx = x.begin();
1699 for (; itx != x.end(); itx++) {
1707 REGISTER_FUNCTION(Li,
1708 evalf_func(Li_evalf).
1710 series_func(Li_series).
1711 derivative_func(Li_deriv).
1712 print_func<print_latex>(Li_print_latex).
1713 do_not_evalf_params());
1716 //////////////////////////////////////////////////////////////////////
1718 // Nielsen's generalized polylogarithm S(n,p,x)
1722 //////////////////////////////////////////////////////////////////////
1725 // anonymous namespace for helper functions
1729 // lookup table for special Euler-Zagier-Sums (used for S_n,p(x))
1731 std::vector<std::vector<cln::cl_N> > Yn;
1732 int ynsize = 0; // number of Yn[]
1733 int ynlength = 100; // initial length of all Yn[i]
1736 // This function calculates the Y_n. The Y_n are needed for the evaluation of S_{n,p}(x).
1737 // The Y_n are basically Euler-Zagier sums with all m_i=1. They are subsums in the Z-sum
1738 // representing S_{n,p}(x).
1739 // The first index in Y_n corresponds to the parameter p minus one, i.e. the depth of the
1740 // equivalent Z-sum.
1741 // The second index in Y_n corresponds to the running index of the outermost sum in the full Z-sum
1742 // representing S_{n,p}(x).
1743 // The calculation of Y_n uses the values from Y_{n-1}.
1744 void fill_Yn(int n, const cln::float_format_t& prec)
1746 const int initsize = ynlength;
1747 //const int initsize = initsize_Yn;
1748 cln::cl_N one = cln::cl_float(1, prec);
1751 std::vector<cln::cl_N> buf(initsize);
1752 std::vector<cln::cl_N>::iterator it = buf.begin();
1753 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
1754 *it = (*itprev) / cln::cl_N(n+1) * one;
1757 // sums with an index smaller than the depth are zero and need not to be calculated.
1758 // calculation starts with depth, which is n+2)
1759 for (int i=n+2; i<=initsize+n; i++) {
1760 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
1766 std::vector<cln::cl_N> buf(initsize);
1767 std::vector<cln::cl_N>::iterator it = buf.begin();
1770 for (int i=2; i<=initsize; i++) {
1771 *it = *(it-1) + 1 / cln::cl_N(i) * one;
1780 // make Yn longer ...
1781 void make_Yn_longer(int newsize, const cln::float_format_t& prec)
1784 cln::cl_N one = cln::cl_float(1, prec);
1786 Yn[0].resize(newsize);
1787 std::vector<cln::cl_N>::iterator it = Yn[0].begin();
1789 for (int i=ynlength+1; i<=newsize; i++) {
1790 *it = *(it-1) + 1 / cln::cl_N(i) * one;
1794 for (int n=1; n<ynsize; n++) {
1795 Yn[n].resize(newsize);
1796 std::vector<cln::cl_N>::iterator it = Yn[n].begin();
1797 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
1800 for (int i=ynlength+n+1; i<=newsize+n; i++) {
1801 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
1811 // helper function for S(n,p,x)
1813 cln::cl_N C(int n, int p)
1817 for (int k=0; k<p; k++) {
1818 for (int j=0; j<=(n+k-1)/2; j++) {
1822 result = result - 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1) / cln::factorial(2*j);
1825 result = result + 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1) / cln::factorial(2*j);
1832 result = result + cln::factorial(n+k-1)
1833 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1834 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1837 result = result - cln::factorial(n+k-1)
1838 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1839 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1844 result = result - cln::factorial(n+k-1) * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1845 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1848 result = result + cln::factorial(n+k-1)
1849 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1850 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1858 if (((np)/2+n) & 1) {
1859 result = -result - cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
1862 result = -result + cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
1870 // helper function for S(n,p,x)
1871 // [Kol] remark to (9.1)
1872 cln::cl_N a_k(int k)
1881 for (int m=2; m<=k; m++) {
1882 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * a_k(k-m);
1889 // helper function for S(n,p,x)
1890 // [Kol] remark to (9.1)
1891 cln::cl_N b_k(int k)
1900 for (int m=2; m<=k; m++) {
1901 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * b_k(k-m);
1908 // helper function for S(n,p,x)
1909 cln::cl_N S_do_sum(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
1911 static cln::float_format_t oldprec = cln::default_float_format;
1914 return Li_projection(n+1, x, prec);
1917 // precision has changed, we need to clear lookup table Yn
1918 if ( oldprec != prec ) {
1925 // check if precalculated values are sufficient
1927 for (int i=ynsize; i<p-1; i++) {
1932 // should be done otherwise
1933 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
1934 cln::cl_N xf = x * one;
1935 //cln::cl_N xf = x * cln::cl_float(1, prec);
1939 cln::cl_N factor = cln::expt(xf, p);
1943 if (i-p >= ynlength) {
1945 make_Yn_longer(ynlength*2, prec);
1947 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? ...
1948 //res = res + factor / cln::expt(cln::cl_I(i),n+1) * (*it); // should we check it? or rely on magic number? ...
1949 factor = factor * xf;
1951 } while (res != resbuf);
1957 // helper function for S(n,p,x)
1958 cln::cl_N S_projection(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
1961 if (cln::abs(cln::realpart(x)) > cln::cl_F("0.5")) {
1963 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(x),n)
1964 * cln::expt(cln::log(1-x),p) / cln::factorial(n) / cln::factorial(p);
1966 for (int s=0; s<n; s++) {
1968 for (int r=0; r<p; r++) {
1969 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-x),r)
1970 * S_do_sum(p-r,n-s,1-x,prec) / cln::factorial(r);
1972 result = result + cln::expt(cln::log(x),s) * (S_num(n-s,p,1) - res2) / cln::factorial(s);
1978 return S_do_sum(n, p, x, prec);
1982 // helper function for S(n,p,x)
1983 const cln::cl_N S_num(int n, int p, const cln::cl_N& x)
1987 // [Kol] (2.22) with (2.21)
1988 return cln::zeta(p+1);
1993 return cln::zeta(n+1);
1998 for (int nu=0; nu<n; nu++) {
1999 for (int rho=0; rho<=p; rho++) {
2000 result = result + b_k(n-nu-1) * b_k(p-rho) * a_k(nu+rho+1)
2001 * cln::factorial(nu+rho+1) / cln::factorial(rho) / cln::factorial(nu+1);
2004 result = result * cln::expt(cln::cl_I(-1),n+p-1);
2011 return -(1-cln::expt(cln::cl_I(2),-n)) * cln::zeta(n+1);
2013 // throw std::runtime_error("don't know how to evaluate this function!");
2016 // what is the desired float format?
2017 // first guess: default format
2018 cln::float_format_t prec = cln::default_float_format;
2019 const cln::cl_N value = x;
2020 // second guess: the argument's format
2021 if (!instanceof(realpart(value), cln::cl_RA_ring))
2022 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
2023 else if (!instanceof(imagpart(value), cln::cl_RA_ring))
2024 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
2027 if ((cln::realpart(value) < -0.5) || (n == 0) || ((cln::abs(value) <= 1) && (cln::abs(value) > 0.95))) {
2029 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(value),n)
2030 * cln::expt(cln::log(1-value),p) / cln::factorial(n) / cln::factorial(p);
2032 for (int s=0; s<n; s++) {
2034 for (int r=0; r<p; r++) {
2035 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-value),r)
2036 * S_num(p-r,n-s,1-value) / cln::factorial(r);
2038 result = result + cln::expt(cln::log(value),s) * (S_num(n-s,p,1) - res2) / cln::factorial(s);
2045 if (cln::abs(value) > 1) {
2049 for (int s=0; s<p; s++) {
2050 for (int r=0; r<=s; r++) {
2051 result = result + cln::expt(cln::cl_I(-1),s) * cln::expt(cln::log(-value),r) * cln::factorial(n+s-r-1)
2052 / cln::factorial(r) / cln::factorial(s-r) / cln::factorial(n-1)
2053 * S_num(n+s-r,p-s,cln::recip(value));
2056 result = result * cln::expt(cln::cl_I(-1),n);
2059 for (int r=0; r<n; r++) {
2060 res2 = res2 + cln::expt(cln::log(-value),r) * C(n-r,p) / cln::factorial(r);
2062 res2 = res2 + cln::expt(cln::log(-value),n+p) / cln::factorial(n+p);
2064 result = result + cln::expt(cln::cl_I(-1),p) * res2;
2069 return S_projection(n, p, value, prec);
2074 } // end of anonymous namespace
2077 //////////////////////////////////////////////////////////////////////
2079 // Nielsen's generalized polylogarithm S(n,p,x)
2083 //////////////////////////////////////////////////////////////////////
2086 static ex S_evalf(const ex& n, const ex& p, const ex& x)
2088 if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
2089 const int n_ = ex_to<numeric>(n).to_int();
2090 const int p_ = ex_to<numeric>(p).to_int();
2091 if (is_a<numeric>(x)) {
2092 const cln::cl_N x_ = ex_to<numeric>(x).to_cl_N();
2093 const cln::cl_N result = S_num(n_, p_, x_);
2094 return numeric(result);
2096 ex x_val = x.evalf();
2097 if (is_a<numeric>(x_val)) {
2098 const cln::cl_N x_val_ = ex_to<numeric>(x_val).to_cl_N();
2099 const cln::cl_N result = S_num(n_, p_, x_val_);
2100 return numeric(result);
2104 return S(n, p, x).hold();
2108 static ex S_eval(const ex& n, const ex& p, const ex& x)
2110 if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
2116 for (int i=ex_to<numeric>(p).to_int()-1; i>0; i--) {
2124 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
2125 int n_ = ex_to<numeric>(n).to_int();
2126 int p_ = ex_to<numeric>(p).to_int();
2127 const cln::cl_N x_ = ex_to<numeric>(x).to_cl_N();
2128 const cln::cl_N result = S_num(n_, p_, x_);
2129 return numeric(result);
2134 return pow(-log(1-x), p) / factorial(p);
2136 return S(n, p, x).hold();
2140 static ex S_series(const ex& n, const ex& p, const ex& x, const relational& rel, int order, unsigned options)
2143 return Li(n+1, x).series(rel, order, options);
2146 const ex x_pt = x.subs(rel, subs_options::no_pattern);
2147 if (n.info(info_flags::posint) && p.info(info_flags::posint) && x_pt.info(info_flags::numeric)) {
2148 // First special case: x==0 (derivatives have poles)
2149 if (x_pt.is_zero()) {
2152 // manually construct the primitive expansion
2153 // subsum = Euler-Zagier-Sum is needed
2154 // dirty hack (slow ...) calculation of subsum:
2155 std::vector<ex> presubsum, subsum;
2156 subsum.push_back(0);
2157 for (int i=1; i<order-1; ++i) {
2158 subsum.push_back(subsum[i-1] + numeric(1, i));
2160 for (int depth=2; depth<p; ++depth) {
2162 for (int i=1; i<order-1; ++i) {
2163 subsum[i] = subsum[i-1] + numeric(1, i) * presubsum[i-1];
2167 for (int i=1; i<order; ++i) {
2168 ser += pow(s,i) / pow(numeric(i), n+1) * subsum[i-1];
2170 // substitute the argument's series expansion
2171 ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
2172 // maybe that was terminating, so add a proper order term
2174 nseq.push_back(expair(Order(_ex1), order));
2175 ser += pseries(rel, nseq);
2176 // reexpanding it will collapse the series again
2177 return ser.series(rel, order);
2179 // TODO special cases: x==1 (branch point) and x real, >=1 (branch cut)
2180 throw std::runtime_error("S_series: don't know how to do the series expansion at this point!");
2182 // all other cases should be safe, by now:
2183 throw do_taylor(); // caught by function::series()
2187 static ex S_deriv(const ex& n, const ex& p, const ex& x, unsigned deriv_param)
2189 GINAC_ASSERT(deriv_param < 3);
2190 if (deriv_param < 2) {
2194 return S(n-1, p, x) / x;
2196 return S(n, p-1, x) / (1-x);
2201 static void S_print_latex(const ex& n, const ex& p, const ex& x, const print_context& c)
2203 c.s << "\\mbox{S}_{";
2213 REGISTER_FUNCTION(S,
2214 evalf_func(S_evalf).
2216 series_func(S_series).
2217 derivative_func(S_deriv).
2218 print_func<print_latex>(S_print_latex).
2219 do_not_evalf_params());
2222 //////////////////////////////////////////////////////////////////////
2224 // Harmonic polylogarithm H(m,x)
2228 //////////////////////////////////////////////////////////////////////
2231 // anonymous namespace for helper functions
2235 // regulates the pole (used by 1/x-transformation)
2236 symbol H_polesign("IMSIGN");
2239 // convert parameters from H to Li representation
2240 // parameters are expected to be in expanded form, i.e. only 0, 1 and -1
2241 // returns true if some parameters are negative
2242 bool convert_parameter_H_to_Li(const lst& l, lst& m, lst& s, ex& pf)
2244 // expand parameter list
2246 for (lst::const_iterator it = l.begin(); it != l.end(); it++) {
2248 for (ex count=*it-1; count > 0; count--) {
2252 } else if (*it < -1) {
2253 for (ex count=*it+1; count < 0; count++) {
2264 bool has_negative_parameters = false;
2266 for (lst::const_iterator it = mexp.begin(); it != mexp.end(); it++) {
2272 m.append((*it+acc-1) * signum);
2274 m.append((*it-acc+1) * signum);
2280 has_negative_parameters = true;
2283 if (has_negative_parameters) {
2284 for (std::size_t i=0; i<m.nops(); i++) {
2286 m.let_op(i) = -m.op(i);
2294 return has_negative_parameters;
2298 // recursivly transforms H to corresponding multiple polylogarithms
2299 struct map_trafo_H_convert_to_Li : public map_function
2301 ex operator()(const ex& e)
2303 if (is_a<add>(e) || is_a<mul>(e)) {
2304 return e.map(*this);
2306 if (is_a<function>(e)) {
2307 std::string name = ex_to<function>(e).get_name();
2310 if (is_a<lst>(e.op(0))) {
2311 parameter = ex_to<lst>(e.op(0));
2313 parameter = lst(e.op(0));
2320 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
2321 s.let_op(0) = s.op(0) * arg;
2322 return pf * Li(m, s).hold();
2324 for (std::size_t i=0; i<m.nops(); i++) {
2327 s.let_op(0) = s.op(0) * arg;
2328 return Li(m, s).hold();
2337 // recursivly transforms H to corresponding zetas
2338 struct map_trafo_H_convert_to_zeta : public map_function
2340 ex operator()(const ex& e)
2342 if (is_a<add>(e) || is_a<mul>(e)) {
2343 return e.map(*this);
2345 if (is_a<function>(e)) {
2346 std::string name = ex_to<function>(e).get_name();
2349 if (is_a<lst>(e.op(0))) {
2350 parameter = ex_to<lst>(e.op(0));
2352 parameter = lst(e.op(0));
2358 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
2359 return pf * zeta(m, s);
2370 // remove trailing zeros from H-parameters
2371 struct map_trafo_H_reduce_trailing_zeros : public map_function
2373 ex operator()(const ex& e)
2375 if (is_a<add>(e) || is_a<mul>(e)) {
2376 return e.map(*this);
2378 if (is_a<function>(e)) {
2379 std::string name = ex_to<function>(e).get_name();
2382 if (is_a<lst>(e.op(0))) {
2383 parameter = ex_to<lst>(e.op(0));
2385 parameter = lst(e.op(0));
2388 if (parameter.op(parameter.nops()-1) == 0) {
2391 if (parameter.nops() == 1) {
2396 lst::const_iterator it = parameter.begin();
2397 while ((it != parameter.end()) && (*it == 0)) {
2400 if (it == parameter.end()) {
2401 return pow(log(arg),parameter.nops()) / factorial(parameter.nops());
2405 parameter.remove_last();
2406 std::size_t lastentry = parameter.nops();
2407 while ((lastentry > 0) && (parameter[lastentry-1] == 0)) {
2412 ex result = log(arg) * H(parameter,arg).hold();
2414 for (ex i=0; i<lastentry; i++) {
2415 if (parameter[i] > 0) {
2417 result -= (acc + parameter[i]-1) * H(parameter, arg).hold();
2420 } else if (parameter[i] < 0) {
2422 result -= (acc + abs(parameter[i]+1)) * H(parameter, arg).hold();
2430 if (lastentry < parameter.nops()) {
2431 result = result / (parameter.nops()-lastentry+1);
2432 return result.map(*this);
2444 // returns an expression with zeta functions corresponding to the parameter list for H
2445 ex convert_H_to_zeta(const lst& m)
2447 symbol xtemp("xtemp");
2448 map_trafo_H_reduce_trailing_zeros filter;
2449 map_trafo_H_convert_to_zeta filter2;
2450 return filter2(filter(H(m, xtemp).hold())).subs(xtemp == 1);
2454 // convert signs form Li to H representation
2455 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf)
2458 lst::const_iterator itm = m.begin();
2459 lst::const_iterator itx = ++x.begin();
2464 while (itx != x.end()) {
2465 signum *= (*itx > 0) ? 1 : -1;
2467 res.append((*itm) * signum);
2475 // multiplies an one-dimensional H with another H
2477 ex trafo_H_mult(const ex& h1, const ex& h2)
2482 ex h1nops = h1.op(0).nops();
2483 ex h2nops = h2.op(0).nops();
2485 hshort = h2.op(0).op(0);
2486 hlong = ex_to<lst>(h1.op(0));
2488 hshort = h1.op(0).op(0);
2490 hlong = ex_to<lst>(h2.op(0));
2492 hlong = h2.op(0).op(0);
2495 for (std::size_t i=0; i<=hlong.nops(); i++) {
2499 newparameter.append(hlong[j]);
2501 newparameter.append(hshort);
2502 for (; j<hlong.nops(); j++) {
2503 newparameter.append(hlong[j]);
2505 res += H(newparameter, h1.op(1)).hold();
2511 // applies trafo_H_mult recursively on expressions
2512 struct map_trafo_H_mult : public map_function
2514 ex operator()(const ex& e)
2517 return e.map(*this);
2525 for (std::size_t pos=0; pos<e.nops(); pos++) {
2526 if (is_a<power>(e.op(pos)) && is_a<function>(e.op(pos).op(0))) {
2527 std::string name = ex_to<function>(e.op(pos).op(0)).get_name();
2529 for (ex i=0; i<e.op(pos).op(1); i++) {
2530 Hlst.append(e.op(pos).op(0));
2534 } else if (is_a<function>(e.op(pos))) {
2535 std::string name = ex_to<function>(e.op(pos)).get_name();
2537 if (e.op(pos).op(0).nops() > 1) {
2540 Hlst.append(e.op(pos));
2545 result *= e.op(pos);
2548 if (Hlst.nops() > 0) {
2549 firstH = Hlst[Hlst.nops()-1];
2556 if (Hlst.nops() > 0) {
2557 ex buffer = trafo_H_mult(firstH, Hlst.op(0));
2559 for (std::size_t i=1; i<Hlst.nops(); i++) {
2560 result *= Hlst.op(i);
2562 result = result.expand();
2563 map_trafo_H_mult recursion;
2564 return recursion(result);
2575 // do integration [ReV] (55)
2576 // put parameter 0 in front of existing parameters
2577 ex trafo_H_1tx_prepend_zero(const ex& e, const ex& arg)
2581 if (is_a<function>(e)) {
2582 name = ex_to<function>(e).get_name();
2587 for (std::size_t i=0; i<e.nops(); i++) {
2588 if (is_a<function>(e.op(i))) {
2589 std::string name = ex_to<function>(e.op(i)).get_name();
2597 lst newparameter = ex_to<lst>(h.op(0));
2598 newparameter.prepend(0);
2599 ex addzeta = convert_H_to_zeta(newparameter);
2600 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
2602 return e * (-H(lst(0),1/arg).hold());
2607 // do integration [ReV] (49)
2608 // put parameter 1 in front of existing parameters
2609 ex trafo_H_prepend_one(const ex& e, const ex& arg)
2613 if (is_a<function>(e)) {
2614 name = ex_to<function>(e).get_name();
2619 for (std::size_t i=0; i<e.nops(); i++) {
2620 if (is_a<function>(e.op(i))) {
2621 std::string name = ex_to<function>(e.op(i)).get_name();
2629 lst newparameter = ex_to<lst>(h.op(0));
2630 newparameter.prepend(1);
2631 return e.subs(h == H(newparameter, h.op(1)).hold());
2633 return e * H(lst(1),1-arg).hold();
2638 // do integration [ReV] (55)
2639 // put parameter -1 in front of existing parameters
2640 ex trafo_H_1tx_prepend_minusone(const ex& e, const ex& arg)
2644 if (is_a<function>(e)) {
2645 name = ex_to<function>(e).get_name();
2650 for (std::size_t i=0; i<e.nops(); i++) {
2651 if (is_a<function>(e.op(i))) {
2652 std::string name = ex_to<function>(e.op(i)).get_name();
2660 lst newparameter = ex_to<lst>(h.op(0));
2661 newparameter.prepend(-1);
2662 ex addzeta = convert_H_to_zeta(newparameter);
2663 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
2665 ex addzeta = convert_H_to_zeta(lst(-1));
2666 return (e * (addzeta - H(lst(-1),1/arg).hold())).expand();
2671 // do integration [ReV] (55)
2672 // put parameter -1 in front of existing parameters
2673 ex trafo_H_1mxt1px_prepend_minusone(const ex& e, const ex& arg)
2677 if (is_a<function>(e)) {
2678 name = ex_to<function>(e).get_name();
2683 for (std::size_t i = 0; i < e.nops(); i++) {
2684 if (is_a<function>(e.op(i))) {
2685 std::string name = ex_to<function>(e.op(i)).get_name();
2693 lst newparameter = ex_to<lst>(h.op(0));
2694 newparameter.prepend(-1);
2695 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
2697 return (e * H(lst(-1),(1-arg)/(1+arg)).hold()).expand();
2702 // do integration [ReV] (55)
2703 // put parameter 1 in front of existing parameters
2704 ex trafo_H_1mxt1px_prepend_one(const ex& e, const ex& arg)
2708 if (is_a<function>(e)) {
2709 name = ex_to<function>(e).get_name();
2714 for (std::size_t i = 0; i < e.nops(); i++) {
2715 if (is_a<function>(e.op(i))) {
2716 std::string name = ex_to<function>(e.op(i)).get_name();
2724 lst newparameter = ex_to<lst>(h.op(0));
2725 newparameter.prepend(1);
2726 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
2728 return (e * H(lst(1),(1-arg)/(1+arg)).hold()).expand();
2733 // do x -> 1-x transformation
2734 struct map_trafo_H_1mx : public map_function
2736 ex operator()(const ex& e)
2738 if (is_a<add>(e) || is_a<mul>(e)) {
2739 return e.map(*this);
2742 if (is_a<function>(e)) {
2743 std::string name = ex_to<function>(e).get_name();
2746 lst parameter = ex_to<lst>(e.op(0));
2749 // special cases if all parameters are either 0, 1 or -1
2750 bool allthesame = true;
2751 if (parameter.op(0) == 0) {
2752 for (std::size_t i = 1; i < parameter.nops(); i++) {
2753 if (parameter.op(i) != 0) {
2760 for (int i=parameter.nops(); i>0; i--) {
2761 newparameter.append(1);
2763 return pow(-1, parameter.nops()) * H(newparameter, 1-arg).hold();
2765 } else if (parameter.op(0) == -1) {
2766 throw std::runtime_error("map_trafo_H_1mx: cannot handle weights equal -1!");
2768 for (std::size_t i = 1; i < parameter.nops(); i++) {
2769 if (parameter.op(i) != 1) {
2776 for (int i=parameter.nops(); i>0; i--) {
2777 newparameter.append(0);
2779 return pow(-1, parameter.nops()) * H(newparameter, 1-arg).hold();
2783 lst newparameter = parameter;
2784 newparameter.remove_first();
2786 if (parameter.op(0) == 0) {
2789 ex res = convert_H_to_zeta(parameter);
2790 //ex res = convert_from_RV(parameter, 1).subs(H(wild(1),wild(2))==zeta(wild(1)));
2791 map_trafo_H_1mx recursion;
2792 ex buffer = recursion(H(newparameter, arg).hold());
2793 if (is_a<add>(buffer)) {
2794 for (std::size_t i = 0; i < buffer.nops(); i++) {
2795 res -= trafo_H_prepend_one(buffer.op(i), arg);
2798 res -= trafo_H_prepend_one(buffer, arg);
2805 map_trafo_H_1mx recursion;
2806 map_trafo_H_mult unify;
2807 ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
2808 std::size_t firstzero = 0;
2809 while (parameter.op(firstzero) == 1) {
2812 for (std::size_t i = firstzero-1; i < parameter.nops()-1; i++) {
2816 newparameter.append(parameter[j+1]);
2818 newparameter.append(1);
2819 for (; j<parameter.nops()-1; j++) {
2820 newparameter.append(parameter[j+1]);
2822 res -= H(newparameter, arg).hold();
2824 res = recursion(res).expand() / firstzero;
2834 // do x -> 1/x transformation
2835 struct map_trafo_H_1overx : public map_function
2837 ex operator()(const ex& e)
2839 if (is_a<add>(e) || is_a<mul>(e)) {
2840 return e.map(*this);
2843 if (is_a<function>(e)) {
2844 std::string name = ex_to<function>(e).get_name();
2847 lst parameter = ex_to<lst>(e.op(0));
2850 // special cases if all parameters are either 0, 1 or -1
2851 bool allthesame = true;
2852 if (parameter.op(0) == 0) {
2853 for (std::size_t i = 1; i < parameter.nops(); i++) {
2854 if (parameter.op(i) != 0) {
2860 return pow(-1, parameter.nops()) * H(parameter, 1/arg).hold();
2862 } else if (parameter.op(0) == -1) {
2863 for (std::size_t i = 1; i < parameter.nops(); i++) {
2864 if (parameter.op(i) != -1) {
2870 map_trafo_H_mult unify;
2871 return unify((pow(H(lst(-1),1/arg).hold() - H(lst(0),1/arg).hold(), parameter.nops())
2872 / factorial(parameter.nops())).expand());
2875 for (std::size_t i = 1; i < parameter.nops(); i++) {
2876 if (parameter.op(i) != 1) {
2882 map_trafo_H_mult unify;
2883 return unify((pow(H(lst(1),1/arg).hold() + H(lst(0),1/arg).hold() + H_polesign, parameter.nops())
2884 / factorial(parameter.nops())).expand());
2888 lst newparameter = parameter;
2889 newparameter.remove_first();
2891 if (parameter.op(0) == 0) {
2894 ex res = convert_H_to_zeta(parameter);
2895 map_trafo_H_1overx recursion;
2896 ex buffer = recursion(H(newparameter, arg).hold());
2897 if (is_a<add>(buffer)) {
2898 for (std::size_t i = 0; i < buffer.nops(); i++) {
2899 res += trafo_H_1tx_prepend_zero(buffer.op(i), arg);
2902 res += trafo_H_1tx_prepend_zero(buffer, arg);
2906 } else if (parameter.op(0) == -1) {
2908 // leading negative one
2909 ex res = convert_H_to_zeta(parameter);
2910 map_trafo_H_1overx recursion;
2911 ex buffer = recursion(H(newparameter, arg).hold());
2912 if (is_a<add>(buffer)) {
2913 for (std::size_t i = 0; i < buffer.nops(); i++) {
2914 res += trafo_H_1tx_prepend_zero(buffer.op(i), arg) - trafo_H_1tx_prepend_minusone(buffer.op(i), arg);
2917 res += trafo_H_1tx_prepend_zero(buffer, arg) - trafo_H_1tx_prepend_minusone(buffer, arg);
2924 map_trafo_H_1overx recursion;
2925 map_trafo_H_mult unify;
2926 ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
2927 std::size_t firstzero = 0;
2928 while (parameter.op(firstzero) == 1) {
2931 for (std::size_t i = firstzero-1; i < parameter.nops() - 1; i++) {
2935 newparameter.append(parameter[j+1]);
2937 newparameter.append(1);
2938 for (; j<parameter.nops()-1; j++) {
2939 newparameter.append(parameter[j+1]);
2941 res -= H(newparameter, arg).hold();
2943 res = recursion(res).expand() / firstzero;
2955 // do x -> (1-x)/(1+x) transformation
2956 struct map_trafo_H_1mxt1px : public map_function
2958 ex operator()(const ex& e)
2960 if (is_a<add>(e) || is_a<mul>(e)) {
2961 return e.map(*this);
2964 if (is_a<function>(e)) {
2965 std::string name = ex_to<function>(e).get_name();
2968 lst parameter = ex_to<lst>(e.op(0));
2971 // special cases if all parameters are either 0, 1 or -1
2972 bool allthesame = true;
2973 if (parameter.op(0) == 0) {
2974 for (std::size_t i = 1; i < parameter.nops(); i++) {
2975 if (parameter.op(i) != 0) {