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-2011 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) {
352 return -Li2_do_sum(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
355 return -Li2_do_sum_Xn(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
359 // check if precalculated Xn exist
361 for (int i=xnsize; i<n-1; i++) {
366 if (cln::realpart(x) < 0.5) {
367 // choose the faster algorithm
368 // with n>=12 the "normal" summation always wins against the method with Xn
369 if ((cln::abs(cln::realpart(x)) < 0.3) || (n >= 12)) {
370 return Lin_do_sum(n, x);
372 return Lin_do_sum_Xn(n, x);
375 cln::cl_N result = 0;
376 if ( x != 1 ) result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
377 for (int j=0; j<n-1; j++) {
378 result = result + (S_num(n-j-1, 1, 1) - S_num(1, n-j-1, 1-x))
379 * cln::expt(cln::log(x), j) / cln::factorial(j);
386 // helper function for classical polylog Li
387 const cln::cl_N Lin_numeric(const int n, const cln::cl_N& x)
391 return -cln::log(1-x);
402 return -(1-cln::expt(cln::cl_I(2),1-n)) * cln::zeta(n);
404 if (cln::abs(realpart(x)) < 0.4 && cln::abs(cln::abs(x)-1) < 0.01) {
405 cln::cl_N result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
406 for (int j=0; j<n-1; j++) {
407 result = result + (S_num(n-j-1, 1, 1) - S_num(1, n-j-1, 1-x))
408 * cln::expt(cln::log(x), j) / cln::factorial(j);
413 // what is the desired float format?
414 // first guess: default format
415 cln::float_format_t prec = cln::default_float_format;
416 const cln::cl_N value = x;
417 // second guess: the argument's format
418 if (!instanceof(realpart(x), cln::cl_RA_ring))
419 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
420 else if (!instanceof(imagpart(x), cln::cl_RA_ring))
421 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
424 if (cln::abs(value) > 1) {
425 cln::cl_N result = -cln::expt(cln::log(-value),n) / cln::factorial(n);
426 // check if argument is complex. if it is real, the new polylog has to be conjugated.
427 if (cln::zerop(cln::imagpart(value))) {
429 result = result + conjugate(Li_projection(n, cln::recip(value), prec));
432 result = result - conjugate(Li_projection(n, cln::recip(value), prec));
437 result = result + Li_projection(n, cln::recip(value), prec);
440 result = result - Li_projection(n, cln::recip(value), prec);
444 for (int j=0; j<n-1; j++) {
445 add = add + (1+cln::expt(cln::cl_I(-1),n-j)) * (1-cln::expt(cln::cl_I(2),1-n+j))
446 * Lin_numeric(n-j,1) * cln::expt(cln::log(-value),j) / cln::factorial(j);
448 result = result - add;
452 return Li_projection(n, value, prec);
457 } // end of anonymous namespace
460 //////////////////////////////////////////////////////////////////////
462 // Multiple polylogarithm Li(n,x)
466 //////////////////////////////////////////////////////////////////////
469 // anonymous namespace for helper function
473 // performs the actual series summation for multiple polylogarithms
474 cln::cl_N multipleLi_do_sum(const std::vector<int>& s, const std::vector<cln::cl_N>& x)
476 // ensure all x <> 0.
477 for (std::vector<cln::cl_N>::const_iterator it = x.begin(); it != x.end(); ++it) {
478 if ( *it == 0 ) return cln::cl_float(0, cln::float_format(Digits));
481 const int j = s.size();
482 bool flag_accidental_zero = false;
484 std::vector<cln::cl_N> t(j);
485 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
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 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 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
498 for (int k=j-2; k>=0; k--) {
499 flag_accidental_zero = cln::zerop(t[k+1]);
500 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]);
502 } while ( (t[0] != t0buf) || cln::zerop(t[0]) || flag_accidental_zero );
508 // forward declaration for Li_eval()
509 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf);
512 // type used by the transformation functions for G
513 typedef std::vector<int> Gparameter;
516 // G_eval1-function for G transformations
517 ex G_eval1(int a, int scale, const exvector& gsyms)
520 const ex& scs = gsyms[std::abs(scale)];
521 const ex& as = gsyms[std::abs(a)];
523 return -log(1 - scs/as);
528 return log(gsyms[std::abs(scale)]);
533 // G_eval-function for G transformations
534 ex G_eval(const Gparameter& a, int scale, const exvector& gsyms)
536 // check for properties of G
537 ex sc = gsyms[std::abs(scale)];
539 bool all_zero = true;
540 bool all_ones = true;
542 for (Gparameter::const_iterator it = a.begin(); it != a.end(); ++it) {
544 const ex sym = gsyms[std::abs(*it)];
558 // care about divergent G: shuffle to separate divergencies that will be canceled
559 // later on in the transformation
560 if (newa.nops() > 1 && newa.op(0) == sc && !all_ones && a.front()!=0) {
563 Gparameter::const_iterator it = a.begin();
565 for (; it != a.end(); ++it) {
566 short_a.push_back(*it);
568 ex result = G_eval1(a.front(), scale, gsyms) * G_eval(short_a, scale, gsyms);
569 it = short_a.begin();
570 for (int i=1; i<count_ones; ++i) {
573 for (; it != short_a.end(); ++it) {
576 Gparameter::const_iterator it2 = short_a.begin();
577 for (; it2 != it; ++it2) {
578 newa.push_back(*it2);
581 newa.push_back(a[0]);
584 for (; it2 != short_a.end(); ++it2) {
585 newa.push_back(*it2);
587 result -= G_eval(newa, scale, gsyms);
589 return result / count_ones;
592 // G({1,...,1};y) -> G({1};y)^k / k!
593 if (all_ones && a.size() > 1) {
594 return pow(G_eval1(a.front(),scale, gsyms), count_ones) / factorial(count_ones);
597 // G({0,...,0};y) -> log(y)^k / k!
599 return pow(log(gsyms[std::abs(scale)]), a.size()) / factorial(a.size());
602 // no special cases anymore -> convert it into Li
605 ex argbuf = gsyms[std::abs(scale)];
607 for (Gparameter::const_iterator it=a.begin(); it!=a.end(); ++it) {
609 const ex& sym = gsyms[std::abs(*it)];
610 x.append(argbuf / sym);
618 return pow(-1, x.nops()) * Li(m, x);
622 // converts data for G: pending_integrals -> a
623 Gparameter convert_pending_integrals_G(const Gparameter& pending_integrals)
625 GINAC_ASSERT(pending_integrals.size() != 1);
627 if (pending_integrals.size() > 0) {
628 // get rid of the first element, which would stand for the new upper limit
629 Gparameter new_a(pending_integrals.begin()+1, pending_integrals.end());
632 // just return empty parameter list
639 // check the parameters a and scale for G and return information about convergence, depth, etc.
640 // convergent : true if G(a,scale) is convergent
641 // depth : depth of G(a,scale)
642 // trailing_zeros : number of trailing zeros of a
643 // min_it : iterator of a pointing on the smallest element in a
644 Gparameter::const_iterator check_parameter_G(const Gparameter& a, int scale,
645 bool& convergent, int& depth, int& trailing_zeros, Gparameter::const_iterator& min_it)
651 Gparameter::const_iterator lastnonzero = a.end();
652 for (Gparameter::const_iterator it = a.begin(); it != a.end(); ++it) {
653 if (std::abs(*it) > 0) {
657 if (std::abs(*it) < scale) {
659 if ((min_it == a.end()) || (std::abs(*it) < std::abs(*min_it))) {
667 if (lastnonzero == a.end())
669 return ++lastnonzero;
673 // add scale to pending_integrals if pending_integrals is empty
674 Gparameter prepare_pending_integrals(const Gparameter& pending_integrals, int scale)
676 GINAC_ASSERT(pending_integrals.size() != 1);
678 if (pending_integrals.size() > 0) {
679 return pending_integrals;
681 Gparameter new_pending_integrals;
682 new_pending_integrals.push_back(scale);
683 return new_pending_integrals;
688 // handles trailing zeroes for an otherwise convergent integral
689 ex trailing_zeros_G(const Gparameter& a, int scale, const exvector& gsyms)
692 int depth, trailing_zeros;
693 Gparameter::const_iterator last, dummyit;
694 last = check_parameter_G(a, scale, convergent, depth, trailing_zeros, dummyit);
696 GINAC_ASSERT(convergent);
698 if ((trailing_zeros > 0) && (depth > 0)) {
700 Gparameter new_a(a.begin(), a.end()-1);
701 result += G_eval1(0, scale, gsyms) * trailing_zeros_G(new_a, scale, gsyms);
702 for (Gparameter::const_iterator it = a.begin(); it != last; ++it) {
703 Gparameter new_a(a.begin(), it);
705 new_a.insert(new_a.end(), it, a.end()-1);
706 result -= trailing_zeros_G(new_a, scale, gsyms);
709 return result / trailing_zeros;
711 return G_eval(a, scale, gsyms);
716 // G transformation [VSW] (57),(58)
717 ex depth_one_trafo_G(const Gparameter& pending_integrals, const Gparameter& a, int scale, const exvector& gsyms)
719 // pendint = ( y1, b1, ..., br )
720 // a = ( 0, ..., 0, amin )
723 // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(0, ..., 0, sr; y2)
724 // where sr replaces amin
726 GINAC_ASSERT(a.back() != 0);
727 GINAC_ASSERT(a.size() > 0);
730 Gparameter new_pending_integrals = prepare_pending_integrals(pending_integrals, std::abs(a.back()));
731 const int psize = pending_integrals.size();
734 // G(sr_{+-}; y2 ) = G(y2_{-+}; sr) - G(0; sr) + ln(-y2_{-+})
739 result += log(gsyms[ex_to<numeric>(scale).to_int()]);
741 new_pending_integrals.push_back(-scale);
744 new_pending_integrals.push_back(scale);
748 result *= trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
749 pending_integrals.front(),
754 result += trailing_zeros_G(convert_pending_integrals_G(new_pending_integrals),
755 new_pending_integrals.front(),
759 new_pending_integrals.back() = 0;
760 result -= trailing_zeros_G(convert_pending_integrals_G(new_pending_integrals),
761 new_pending_integrals.front(),
768 // G_m(sr_{+-}; y2) = -zeta_m + int_0^y2 dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
769 // - int_0^sr dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
772 result -= zeta(a.size());
774 result *= trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
775 pending_integrals.front(),
779 // term int_0^sr dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
780 // = int_0^sr dt/t G_{m-1}( t_{+-}; y2 )
781 Gparameter new_a(a.begin()+1, a.end());
782 new_pending_integrals.push_back(0);
783 result -= depth_one_trafo_G(new_pending_integrals, new_a, scale, gsyms);
785 // term int_0^y2 dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
786 // = int_0^y2 dt/t G_{m-1}( t_{+-}; y2 )
787 Gparameter new_pending_integrals_2;
788 new_pending_integrals_2.push_back(scale);
789 new_pending_integrals_2.push_back(0);
791 result += trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
792 pending_integrals.front(),
794 * depth_one_trafo_G(new_pending_integrals_2, new_a, scale, gsyms);
796 result += depth_one_trafo_G(new_pending_integrals_2, new_a, scale, gsyms);
803 // forward declaration
804 ex shuffle_G(const Gparameter & a0, const Gparameter & a1, const Gparameter & a2,
805 const Gparameter& pendint, const Gparameter& a_old, int scale,
806 const exvector& gsyms);
809 // G transformation [VSW]
810 ex G_transform(const Gparameter& pendint, const Gparameter& a, int scale,
811 const exvector& gsyms)
813 // main recursion routine
815 // pendint = ( y1, b1, ..., br )
816 // a = ( a1, ..., amin, ..., aw )
819 // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,sr,...,aw,y2)
820 // where sr replaces amin
822 // find smallest alpha, determine depth and trailing zeros, and check for convergence
824 int depth, trailing_zeros;
825 Gparameter::const_iterator min_it;
826 Gparameter::const_iterator firstzero =
827 check_parameter_G(a, scale, convergent, depth, trailing_zeros, min_it);
828 int min_it_pos = min_it - a.begin();
830 // special case: all a's are zero
837 result = G_eval(a, scale, gsyms);
839 if (pendint.size() > 0) {
840 result *= trailing_zeros_G(convert_pending_integrals_G(pendint),
847 // handle trailing zeros
848 if (trailing_zeros > 0) {
850 Gparameter new_a(a.begin(), a.end()-1);
851 result += G_eval1(0, scale, gsyms) * G_transform(pendint, new_a, scale, gsyms);
852 for (Gparameter::const_iterator it = a.begin(); it != firstzero; ++it) {
853 Gparameter new_a(a.begin(), it);
855 new_a.insert(new_a.end(), it, a.end()-1);
856 result -= G_transform(pendint, new_a, scale, gsyms);
858 return result / trailing_zeros;
863 if (pendint.size() > 0) {
864 return G_eval(convert_pending_integrals_G(pendint),
865 pendint.front(), gsyms)*
866 G_eval(a, scale, gsyms);
868 return G_eval(a, scale, gsyms);
872 // call basic transformation for depth equal one
874 return depth_one_trafo_G(pendint, a, scale, gsyms);
878 // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,sr,...,aw,y2)
879 // = int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,0,...,aw,y2)
880 // + 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)
882 // smallest element in last place
883 if (min_it + 1 == a.end()) {
884 do { --min_it; } while (*min_it == 0);
886 Gparameter a1(a.begin(),min_it+1);
887 Gparameter a2(min_it+1,a.end());
889 ex result = G_transform(pendint, a2, scale, gsyms)*
890 G_transform(empty, a1, scale, gsyms);
892 result -= shuffle_G(empty, a1, a2, pendint, a, scale, gsyms);
897 Gparameter::iterator changeit;
899 // first term G(a_1,..,0,...,a_w;a_0)
900 Gparameter new_pendint = prepare_pending_integrals(pendint, a[min_it_pos]);
901 Gparameter new_a = a;
902 new_a[min_it_pos] = 0;
903 ex result = G_transform(empty, new_a, scale, gsyms);
904 if (pendint.size() > 0) {
905 result *= trailing_zeros_G(convert_pending_integrals_G(pendint),
906 pendint.front(), gsyms);
910 changeit = new_a.begin() + min_it_pos;
911 changeit = new_a.erase(changeit);
912 if (changeit != new_a.begin()) {
913 // smallest in the middle
914 new_pendint.push_back(*changeit);
915 result -= trailing_zeros_G(convert_pending_integrals_G(new_pendint),
916 new_pendint.front(), gsyms)*
917 G_transform(empty, new_a, scale, gsyms);
918 int buffer = *changeit;
920 result += G_transform(new_pendint, new_a, scale, gsyms);
922 new_pendint.pop_back();
924 new_pendint.push_back(*changeit);
925 result += trailing_zeros_G(convert_pending_integrals_G(new_pendint),
926 new_pendint.front(), gsyms)*
927 G_transform(empty, new_a, scale, gsyms);
929 result -= G_transform(new_pendint, new_a, scale, gsyms);
931 // smallest at the front
932 new_pendint.push_back(scale);
933 result += trailing_zeros_G(convert_pending_integrals_G(new_pendint),
934 new_pendint.front(), gsyms)*
935 G_transform(empty, new_a, scale, gsyms);
936 new_pendint.back() = *changeit;
937 result -= trailing_zeros_G(convert_pending_integrals_G(new_pendint),
938 new_pendint.front(), gsyms)*
939 G_transform(empty, new_a, scale, gsyms);
941 result += G_transform(new_pendint, new_a, scale, gsyms);
947 // shuffles the two parameter list a1 and a2 and calls G_transform for every term except
948 // for the one that is equal to a_old
949 ex shuffle_G(const Gparameter & a0, const Gparameter & a1, const Gparameter & a2,
950 const Gparameter& pendint, const Gparameter& a_old, int scale,
951 const exvector& gsyms)
953 if (a1.size()==0 && a2.size()==0) {
954 // veto the one configuration we don't want
955 if ( a0 == a_old ) return 0;
957 return G_transform(pendint, a0, scale, gsyms);
963 aa0.insert(aa0.end(),a1.begin(),a1.end());
964 return shuffle_G(aa0, empty, empty, pendint, a_old, scale, gsyms);
970 aa0.insert(aa0.end(),a2.begin(),a2.end());
971 return shuffle_G(aa0, empty, empty, pendint, a_old, scale, gsyms);
974 Gparameter a1_removed(a1.begin()+1,a1.end());
975 Gparameter a2_removed(a2.begin()+1,a2.end());
980 a01.push_back( a1[0] );
981 a02.push_back( a2[0] );
983 return shuffle_G(a01, a1_removed, a2, pendint, a_old, scale, gsyms)
984 + shuffle_G(a02, a1, a2_removed, pendint, a_old, scale, gsyms);
987 // handles the transformations and the numerical evaluation of G
988 // the parameter x, s and y must only contain numerics
990 G_numeric(const std::vector<cln::cl_N>& x, const std::vector<int>& s,
993 // do acceleration transformation (hoelder convolution [BBB])
994 // the parameter x, s and y must only contain numerics
996 G_do_hoelder(std::vector<cln::cl_N> x, /* yes, it's passed by value */
997 const std::vector<int>& s, const cln::cl_N& y)
1000 const std::size_t size = x.size();
1001 for (std::size_t i = 0; i < size; ++i)
1004 for (std::size_t r = 0; r <= size; ++r) {
1005 cln::cl_N buffer(1 & r ? -1 : 1);
1010 for (std::size_t i = 0; i < size; ++i) {
1011 if (x[i] == cln::cl_RA(1)/p) {
1012 p = p/2 + cln::cl_RA(3)/2;
1018 cln::cl_RA q = p/(p-1);
1019 std::vector<cln::cl_N> qlstx;
1020 std::vector<int> qlsts;
1021 for (std::size_t j = r; j >= 1; --j) {
1022 qlstx.push_back(cln::cl_N(1) - x[j-1]);
1023 if (instanceof(x[j-1], cln::cl_R_ring) &&
1024 realpart(x[j-1]) > 1 && realpart(x[j-1]) <= 2) {
1025 qlsts.push_back(s[j-1]);
1027 qlsts.push_back(-s[j-1]);
1030 if (qlstx.size() > 0) {
1031 buffer = buffer*G_numeric(qlstx, qlsts, 1/q);
1033 std::vector<cln::cl_N> plstx;
1034 std::vector<int> plsts;
1035 for (std::size_t j = r+1; j <= size; ++j) {
1036 plstx.push_back(x[j-1]);
1037 plsts.push_back(s[j-1]);
1039 if (plstx.size() > 0) {
1040 buffer = buffer*G_numeric(plstx, plsts, 1/p);
1042 result = result + buffer;
1047 // convergence transformation, used for numerical evaluation of G function.
1048 // the parameter x, s and y must only contain numerics
1050 G_do_trafo(const std::vector<cln::cl_N>& x, const std::vector<int>& s,
1053 // sort (|x|<->position) to determine indices
1054 typedef std::multimap<cln::cl_R, std::size_t> sortmap_t;
1056 std::size_t size = 0;
1057 for (std::size_t i = 0; i < x.size(); ++i) {
1059 sortmap.insert(std::make_pair(abs(x[i]), i));
1063 // include upper limit (scale)
1064 sortmap.insert(std::make_pair(abs(y), x.size()));
1066 // generate missing dummy-symbols
1068 // holding dummy-symbols for the G/Li transformations
1070 gsyms.push_back(symbol("GSYMS_ERROR"));
1071 cln::cl_N lastentry(0);
1072 for (sortmap_t::const_iterator it = sortmap.begin(); it != sortmap.end(); ++it) {
1073 if (it != sortmap.begin()) {
1074 if (it->second < x.size()) {
1075 if (x[it->second] == lastentry) {
1076 gsyms.push_back(gsyms.back());
1080 if (y == lastentry) {
1081 gsyms.push_back(gsyms.back());
1086 std::ostringstream os;
1088 gsyms.push_back(symbol(os.str()));
1090 if (it->second < x.size()) {
1091 lastentry = x[it->second];
1097 // fill position data according to sorted indices and prepare substitution list
1098 Gparameter a(x.size());
1100 std::size_t pos = 1;
1102 for (sortmap_t::const_iterator it = sortmap.begin(); it != sortmap.end(); ++it) {
1103 if (it->second < x.size()) {
1104 if (s[it->second] > 0) {
1105 a[it->second] = pos;
1107 a[it->second] = -int(pos);
1109 subslst[gsyms[pos]] = numeric(x[it->second]);
1112 subslst[gsyms[pos]] = numeric(y);
1117 // do transformation
1119 ex result = G_transform(pendint, a, scale, gsyms);
1120 // replace dummy symbols with their values
1121 result = result.eval().expand();
1122 result = result.subs(subslst).evalf();
1123 if (!is_a<numeric>(result))
1124 throw std::logic_error("G_do_trafo: G_transform returned non-numeric result");
1126 cln::cl_N ret = ex_to<numeric>(result).to_cl_N();
1130 // handles the transformations and the numerical evaluation of G
1131 // the parameter x, s and y must only contain numerics
1133 G_numeric(const std::vector<cln::cl_N>& x, const std::vector<int>& s,
1136 // check for convergence and necessary accelerations
1137 bool need_trafo = false;
1138 bool need_hoelder = false;
1139 std::size_t depth = 0;
1140 for (std::size_t i = 0; i < x.size(); ++i) {
1143 const cln::cl_N x_y = abs(x[i]) - y;
1144 if (instanceof(x_y, cln::cl_R_ring) &&
1145 realpart(x_y) < cln::least_negative_float(cln::float_format(Digits - 2)))
1148 if (abs(abs(x[i]/y) - 1) < 0.01)
1149 need_hoelder = true;
1152 if (zerop(x[x.size() - 1]))
1155 if (depth == 1 && x.size() == 2 && !need_trafo)
1156 return - Li_projection(2, y/x[1], cln::float_format(Digits));
1158 // do acceleration transformation (hoelder convolution [BBB])
1160 return G_do_hoelder(x, s, y);
1162 // convergence transformation
1164 return G_do_trafo(x, s, y);
1167 std::vector<cln::cl_N> newx;
1168 newx.reserve(x.size());
1170 m.reserve(x.size());
1173 cln::cl_N factor = y;
1174 for (std::size_t i = 0; i < x.size(); ++i) {
1178 newx.push_back(factor/x[i]);
1180 m.push_back(mcount);
1186 return sign*multipleLi_do_sum(m, newx);
1190 ex mLi_numeric(const lst& m, const lst& x)
1192 // let G_numeric do the transformation
1193 std::vector<cln::cl_N> newx;
1194 newx.reserve(x.nops());
1196 s.reserve(x.nops());
1197 cln::cl_N factor(1);
1198 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1199 for (int i = 1; i < *itm; ++i) {
1200 newx.push_back(cln::cl_N(0));
1203 const cln::cl_N xi = ex_to<numeric>(*itx).to_cl_N();
1205 newx.push_back(factor);
1206 if ( !instanceof(factor, cln::cl_R_ring) && imagpart(factor) < 0 ) {
1213 return numeric(cln::cl_N(1 & m.nops() ? - 1 : 1)*G_numeric(newx, s, cln::cl_N(1)));
1217 } // end of anonymous namespace
1220 //////////////////////////////////////////////////////////////////////
1222 // Generalized multiple polylogarithm G(x, y) and G(x, s, y)
1226 //////////////////////////////////////////////////////////////////////
1229 static ex G2_evalf(const ex& x_, const ex& y)
1231 if (!y.info(info_flags::positive)) {
1232 return G(x_, y).hold();
1234 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1235 if (x.nops() == 0) {
1239 return G(x_, y).hold();
1242 s.reserve(x.nops());
1243 bool all_zero = true;
1244 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1245 if (!(*it).info(info_flags::numeric)) {
1246 return G(x_, y).hold();
1251 if ( !ex_to<numeric>(*it).is_real() && ex_to<numeric>(*it).imag() < 0 ) {
1259 return pow(log(y), x.nops()) / factorial(x.nops());
1261 std::vector<cln::cl_N> xv;
1262 xv.reserve(x.nops());
1263 for (lst::const_iterator it = x.begin(); it != x.end(); ++it)
1264 xv.push_back(ex_to<numeric>(*it).to_cl_N());
1265 cln::cl_N result = G_numeric(xv, s, ex_to<numeric>(y).to_cl_N());
1266 return numeric(result);
1270 static ex G2_eval(const ex& x_, const ex& y)
1272 //TODO eval to MZV or H or S or Lin
1274 if (!y.info(info_flags::positive)) {
1275 return G(x_, y).hold();
1277 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1278 if (x.nops() == 0) {
1282 return G(x_, y).hold();
1285 s.reserve(x.nops());
1286 bool all_zero = true;
1287 bool crational = true;
1288 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1289 if (!(*it).info(info_flags::numeric)) {
1290 return G(x_, y).hold();
1292 if (!(*it).info(info_flags::crational)) {
1298 if ( !ex_to<numeric>(*it).is_real() && ex_to<numeric>(*it).imag() < 0 ) {
1306 return pow(log(y), x.nops()) / factorial(x.nops());
1308 if (!y.info(info_flags::crational)) {
1312 return G(x_, y).hold();
1314 std::vector<cln::cl_N> xv;
1315 xv.reserve(x.nops());
1316 for (lst::const_iterator it = x.begin(); it != x.end(); ++it)
1317 xv.push_back(ex_to<numeric>(*it).to_cl_N());
1318 cln::cl_N result = G_numeric(xv, s, ex_to<numeric>(y).to_cl_N());
1319 return numeric(result);
1323 unsigned G2_SERIAL::serial = function::register_new(function_options("G", 2).
1324 evalf_func(G2_evalf).
1326 do_not_evalf_params().
1329 // derivative_func(G2_deriv).
1330 // print_func<print_latex>(G2_print_latex).
1333 static ex G3_evalf(const ex& x_, const ex& s_, const ex& y)
1335 if (!y.info(info_flags::positive)) {
1336 return G(x_, s_, y).hold();
1338 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1339 lst s = is_a<lst>(s_) ? ex_to<lst>(s_) : lst(s_);
1340 if (x.nops() != s.nops()) {
1341 return G(x_, s_, y).hold();
1343 if (x.nops() == 0) {
1347 return G(x_, s_, y).hold();
1349 std::vector<int> sn;
1350 sn.reserve(s.nops());
1351 bool all_zero = true;
1352 for (lst::const_iterator itx = x.begin(), its = s.begin(); itx != x.end(); ++itx, ++its) {
1353 if (!(*itx).info(info_flags::numeric)) {
1354 return G(x_, y).hold();
1356 if (!(*its).info(info_flags::real)) {
1357 return G(x_, y).hold();
1362 if ( ex_to<numeric>(*itx).is_real() ) {
1363 if ( ex_to<numeric>(*itx).is_positive() ) {
1375 if ( ex_to<numeric>(*itx).imag() > 0 ) {
1384 return pow(log(y), x.nops()) / factorial(x.nops());
1386 std::vector<cln::cl_N> xn;
1387 xn.reserve(x.nops());
1388 for (lst::const_iterator it = x.begin(); it != x.end(); ++it)
1389 xn.push_back(ex_to<numeric>(*it).to_cl_N());
1390 cln::cl_N result = G_numeric(xn, sn, ex_to<numeric>(y).to_cl_N());
1391 return numeric(result);
1395 static ex G3_eval(const ex& x_, const ex& s_, const ex& y)
1397 //TODO eval to MZV or H or S or Lin
1399 if (!y.info(info_flags::positive)) {
1400 return G(x_, s_, y).hold();
1402 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1403 lst s = is_a<lst>(s_) ? ex_to<lst>(s_) : lst(s_);
1404 if (x.nops() != s.nops()) {
1405 return G(x_, s_, y).hold();
1407 if (x.nops() == 0) {
1411 return G(x_, s_, y).hold();
1413 std::vector<int> sn;
1414 sn.reserve(s.nops());
1415 bool all_zero = true;
1416 bool crational = true;
1417 for (lst::const_iterator itx = x.begin(), its = s.begin(); itx != x.end(); ++itx, ++its) {
1418 if (!(*itx).info(info_flags::numeric)) {
1419 return G(x_, s_, y).hold();
1421 if (!(*its).info(info_flags::real)) {
1422 return G(x_, s_, y).hold();
1424 if (!(*itx).info(info_flags::crational)) {
1430 if ( ex_to<numeric>(*itx).is_real() ) {
1431 if ( ex_to<numeric>(*itx).is_positive() ) {
1443 if ( ex_to<numeric>(*itx).imag() > 0 ) {
1452 return pow(log(y), x.nops()) / factorial(x.nops());
1454 if (!y.info(info_flags::crational)) {
1458 return G(x_, s_, y).hold();
1460 std::vector<cln::cl_N> xn;
1461 xn.reserve(x.nops());
1462 for (lst::const_iterator it = x.begin(); it != x.end(); ++it)
1463 xn.push_back(ex_to<numeric>(*it).to_cl_N());
1464 cln::cl_N result = G_numeric(xn, sn, ex_to<numeric>(y).to_cl_N());
1465 return numeric(result);
1469 unsigned G3_SERIAL::serial = function::register_new(function_options("G", 3).
1470 evalf_func(G3_evalf).
1472 do_not_evalf_params().
1475 // derivative_func(G3_deriv).
1476 // print_func<print_latex>(G3_print_latex).
1479 //////////////////////////////////////////////////////////////////////
1481 // Classical polylogarithm and multiple polylogarithm Li(m,x)
1485 //////////////////////////////////////////////////////////////////////
1488 static ex Li_evalf(const ex& m_, const ex& x_)
1490 // classical polylogs
1491 if (m_.info(info_flags::posint)) {
1492 if (x_.info(info_flags::numeric)) {
1493 int m__ = ex_to<numeric>(m_).to_int();
1494 const cln::cl_N x__ = ex_to<numeric>(x_).to_cl_N();
1495 const cln::cl_N result = Lin_numeric(m__, x__);
1496 return numeric(result);
1498 // try to numerically evaluate second argument
1499 ex x_val = x_.evalf();
1500 if (x_val.info(info_flags::numeric)) {
1501 int m__ = ex_to<numeric>(m_).to_int();
1502 const cln::cl_N x__ = ex_to<numeric>(x_val).to_cl_N();
1503 const cln::cl_N result = Lin_numeric(m__, x__);
1504 return numeric(result);
1508 // multiple polylogs
1509 if (is_a<lst>(m_) && is_a<lst>(x_)) {
1511 const lst& m = ex_to<lst>(m_);
1512 const lst& x = ex_to<lst>(x_);
1513 if (m.nops() != x.nops()) {
1514 return Li(m_,x_).hold();
1516 if (x.nops() == 0) {
1519 if ((m.op(0) == _ex1) && (x.op(0) == _ex1)) {
1520 return Li(m_,x_).hold();
1523 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1524 if (!(*itm).info(info_flags::posint)) {
1525 return Li(m_, x_).hold();
1527 if (!(*itx).info(info_flags::numeric)) {
1528 return Li(m_, x_).hold();
1535 return mLi_numeric(m, x);
1538 return Li(m_,x_).hold();
1542 static ex Li_eval(const ex& m_, const ex& x_)
1544 if (is_a<lst>(m_)) {
1545 if (is_a<lst>(x_)) {
1546 // multiple polylogs
1547 const lst& m = ex_to<lst>(m_);
1548 const lst& x = ex_to<lst>(x_);
1549 if (m.nops() != x.nops()) {
1550 return Li(m_,x_).hold();
1552 if (x.nops() == 0) {
1556 bool is_zeta = true;
1557 bool do_evalf = true;
1558 bool crational = true;
1559 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1560 if (!(*itm).info(info_flags::posint)) {
1561 return Li(m_,x_).hold();
1563 if ((*itx != _ex1) && (*itx != _ex_1)) {
1564 if (itx != x.begin()) {
1572 if (!(*itx).info(info_flags::numeric)) {
1575 if (!(*itx).info(info_flags::crational)) {
1584 lst newm = convert_parameter_Li_to_H(m, x, prefactor);
1585 return prefactor * H(newm, x[0]);
1587 if (do_evalf && !crational) {
1588 return mLi_numeric(m,x);
1591 return Li(m_, x_).hold();
1592 } else if (is_a<lst>(x_)) {
1593 return Li(m_, x_).hold();
1596 // classical polylogs
1604 return (pow(2,1-m_)-1) * zeta(m_);
1610 if (x_.is_equal(I)) {
1611 return power(Pi,_ex2)/_ex_48 + Catalan*I;
1613 if (x_.is_equal(-I)) {
1614 return power(Pi,_ex2)/_ex_48 - Catalan*I;
1617 if (m_.info(info_flags::posint) && x_.info(info_flags::numeric) && !x_.info(info_flags::crational)) {
1618 int m__ = ex_to<numeric>(m_).to_int();
1619 const cln::cl_N x__ = ex_to<numeric>(x_).to_cl_N();
1620 const cln::cl_N result = Lin_numeric(m__, x__);
1621 return numeric(result);
1624 return Li(m_, x_).hold();
1628 static ex Li_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
1630 if (is_a<lst>(m) || is_a<lst>(x)) {
1633 seq.push_back(expair(Li(m, x), 0));
1634 return pseries(rel, seq);
1637 // classical polylog
1638 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1639 if (m.info(info_flags::numeric) && x_pt.info(info_flags::numeric)) {
1640 // First special case: x==0 (derivatives have poles)
1641 if (x_pt.is_zero()) {
1644 // manually construct the primitive expansion
1645 for (int i=1; i<order; ++i)
1646 ser += pow(s,i) / pow(numeric(i), m);
1647 // substitute the argument's series expansion
1648 ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
1649 // maybe that was terminating, so add a proper order term
1651 nseq.push_back(expair(Order(_ex1), order));
1652 ser += pseries(rel, nseq);
1653 // reexpanding it will collapse the series again
1654 return ser.series(rel, order);
1656 // TODO special cases: x==1 (branch point) and x real, >=1 (branch cut)
1657 throw std::runtime_error("Li_series: don't know how to do the series expansion at this point!");
1659 // all other cases should be safe, by now:
1660 throw do_taylor(); // caught by function::series()
1664 static ex Li_deriv(const ex& m_, const ex& x_, unsigned deriv_param)
1666 GINAC_ASSERT(deriv_param < 2);
1667 if (deriv_param == 0) {
1670 if (m_.nops() > 1) {
1671 throw std::runtime_error("don't know how to derivate multiple polylogarithm!");
1674 if (is_a<lst>(m_)) {
1680 if (is_a<lst>(x_)) {
1686 return Li(m-1, x) / x;
1693 static void Li_print_latex(const ex& m_, const ex& x_, const print_context& c)
1696 if (is_a<lst>(m_)) {
1702 if (is_a<lst>(x_)) {
1707 c.s << "\\mathrm{Li}_{";
1708 lst::const_iterator itm = m.begin();
1711 for (; itm != m.end(); itm++) {
1716 lst::const_iterator itx = x.begin();
1719 for (; itx != x.end(); itx++) {
1727 REGISTER_FUNCTION(Li,
1728 evalf_func(Li_evalf).
1730 series_func(Li_series).
1731 derivative_func(Li_deriv).
1732 print_func<print_latex>(Li_print_latex).
1733 do_not_evalf_params());
1736 //////////////////////////////////////////////////////////////////////
1738 // Nielsen's generalized polylogarithm S(n,p,x)
1742 //////////////////////////////////////////////////////////////////////
1745 // anonymous namespace for helper functions
1749 // lookup table for special Euler-Zagier-Sums (used for S_n,p(x))
1751 std::vector<std::vector<cln::cl_N> > Yn;
1752 int ynsize = 0; // number of Yn[]
1753 int ynlength = 100; // initial length of all Yn[i]
1756 // This function calculates the Y_n. The Y_n are needed for the evaluation of S_{n,p}(x).
1757 // The Y_n are basically Euler-Zagier sums with all m_i=1. They are subsums in the Z-sum
1758 // representing S_{n,p}(x).
1759 // The first index in Y_n corresponds to the parameter p minus one, i.e. the depth of the
1760 // equivalent Z-sum.
1761 // The second index in Y_n corresponds to the running index of the outermost sum in the full Z-sum
1762 // representing S_{n,p}(x).
1763 // The calculation of Y_n uses the values from Y_{n-1}.
1764 void fill_Yn(int n, const cln::float_format_t& prec)
1766 const int initsize = ynlength;
1767 //const int initsize = initsize_Yn;
1768 cln::cl_N one = cln::cl_float(1, prec);
1771 std::vector<cln::cl_N> buf(initsize);
1772 std::vector<cln::cl_N>::iterator it = buf.begin();
1773 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
1774 *it = (*itprev) / cln::cl_N(n+1) * one;
1777 // sums with an index smaller than the depth are zero and need not to be calculated.
1778 // calculation starts with depth, which is n+2)
1779 for (int i=n+2; i<=initsize+n; i++) {
1780 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
1786 std::vector<cln::cl_N> buf(initsize);
1787 std::vector<cln::cl_N>::iterator it = buf.begin();
1790 for (int i=2; i<=initsize; i++) {
1791 *it = *(it-1) + 1 / cln::cl_N(i) * one;
1800 // make Yn longer ...
1801 void make_Yn_longer(int newsize, const cln::float_format_t& prec)
1804 cln::cl_N one = cln::cl_float(1, prec);
1806 Yn[0].resize(newsize);
1807 std::vector<cln::cl_N>::iterator it = Yn[0].begin();
1809 for (int i=ynlength+1; i<=newsize; i++) {
1810 *it = *(it-1) + 1 / cln::cl_N(i) * one;
1814 for (int n=1; n<ynsize; n++) {
1815 Yn[n].resize(newsize);
1816 std::vector<cln::cl_N>::iterator it = Yn[n].begin();
1817 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
1820 for (int i=ynlength+n+1; i<=newsize+n; i++) {
1821 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
1831 // helper function for S(n,p,x)
1833 cln::cl_N C(int n, int p)
1837 for (int k=0; k<p; k++) {
1838 for (int j=0; j<=(n+k-1)/2; j++) {
1842 result = result - 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1) / cln::factorial(2*j);
1845 result = result + 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1) / cln::factorial(2*j);
1852 result = result + cln::factorial(n+k-1)
1853 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1854 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1857 result = result - cln::factorial(n+k-1)
1858 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1859 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1864 result = result - cln::factorial(n+k-1) * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1865 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1868 result = result + cln::factorial(n+k-1)
1869 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1870 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1878 if (((np)/2+n) & 1) {
1879 result = -result - cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
1882 result = -result + cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
1890 // helper function for S(n,p,x)
1891 // [Kol] remark to (9.1)
1892 cln::cl_N a_k(int k)
1901 for (int m=2; m<=k; m++) {
1902 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * a_k(k-m);
1909 // helper function for S(n,p,x)
1910 // [Kol] remark to (9.1)
1911 cln::cl_N b_k(int k)
1920 for (int m=2; m<=k; m++) {
1921 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * b_k(k-m);
1928 // helper function for S(n,p,x)
1929 cln::cl_N S_do_sum(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
1931 static cln::float_format_t oldprec = cln::default_float_format;
1934 return Li_projection(n+1, x, prec);
1937 // precision has changed, we need to clear lookup table Yn
1938 if ( oldprec != prec ) {
1945 // check if precalculated values are sufficient
1947 for (int i=ynsize; i<p-1; i++) {
1952 // should be done otherwise
1953 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
1954 cln::cl_N xf = x * one;
1955 //cln::cl_N xf = x * cln::cl_float(1, prec);
1959 cln::cl_N factor = cln::expt(xf, p);
1963 if (i-p >= ynlength) {
1965 make_Yn_longer(ynlength*2, prec);
1967 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? ...
1968 //res = res + factor / cln::expt(cln::cl_I(i),n+1) * (*it); // should we check it? or rely on magic number? ...
1969 factor = factor * xf;
1971 } while (res != resbuf);
1977 // helper function for S(n,p,x)
1978 cln::cl_N S_projection(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
1981 if (cln::abs(cln::realpart(x)) > cln::cl_F("0.5")) {
1983 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(x),n)
1984 * cln::expt(cln::log(1-x),p) / cln::factorial(n) / cln::factorial(p);
1986 for (int s=0; s<n; s++) {
1988 for (int r=0; r<p; r++) {
1989 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-x),r)
1990 * S_do_sum(p-r,n-s,1-x,prec) / cln::factorial(r);
1992 result = result + cln::expt(cln::log(x),s) * (S_num(n-s,p,1) - res2) / cln::factorial(s);
1998 return S_do_sum(n, p, x, prec);
2002 // helper function for S(n,p,x)
2003 const cln::cl_N S_num(int n, int p, const cln::cl_N& x)
2007 // [Kol] (2.22) with (2.21)
2008 return cln::zeta(p+1);
2013 return cln::zeta(n+1);
2018 for (int nu=0; nu<n; nu++) {
2019 for (int rho=0; rho<=p; rho++) {
2020 result = result + b_k(n-nu-1) * b_k(p-rho) * a_k(nu+rho+1)
2021 * cln::factorial(nu+rho+1) / cln::factorial(rho) / cln::factorial(nu+1);
2024 result = result * cln::expt(cln::cl_I(-1),n+p-1);
2031 return -(1-cln::expt(cln::cl_I(2),-n)) * cln::zeta(n+1);
2033 // throw std::runtime_error("don't know how to evaluate this function!");
2036 // what is the desired float format?
2037 // first guess: default format
2038 cln::float_format_t prec = cln::default_float_format;
2039 const cln::cl_N value = x;
2040 // second guess: the argument's format
2041 if (!instanceof(realpart(value), cln::cl_RA_ring))
2042 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
2043 else if (!instanceof(imagpart(value), cln::cl_RA_ring))
2044 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
2047 if ((cln::realpart(value) < -0.5) || (n == 0) || ((cln::abs(value) <= 1) && (cln::abs(value) > 0.95))) {
2049 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(value),n)
2050 * cln::expt(cln::log(1-value),p) / cln::factorial(n) / cln::factorial(p);
2052 for (int s=0; s<n; s++) {
2054 for (int r=0; r<p; r++) {
2055 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-value),r)
2056 * S_num(p-r,n-s,1-value) / cln::factorial(r);
2058 result = result + cln::expt(cln::log(value),s) * (S_num(n-s,p,1) - res2) / cln::factorial(s);
2065 if (cln::abs(value) > 1) {
2069 for (int s=0; s<p; s++) {
2070 for (int r=0; r<=s; r++) {
2071 result = result + cln::expt(cln::cl_I(-1),s) * cln::expt(cln::log(-value),r) * cln::factorial(n+s-r-1)
2072 / cln::factorial(r) / cln::factorial(s-r) / cln::factorial(n-1)
2073 * S_num(n+s-r,p-s,cln::recip(value));
2076 result = result * cln::expt(cln::cl_I(-1),n);
2079 for (int r=0; r<n; r++) {
2080 res2 = res2 + cln::expt(cln::log(-value),r) * C(n-r,p) / cln::factorial(r);
2082 res2 = res2 + cln::expt(cln::log(-value),n+p) / cln::factorial(n+p);
2084 result = result + cln::expt(cln::cl_I(-1),p) * res2;
2089 return S_projection(n, p, value, prec);
2094 } // end of anonymous namespace
2097 //////////////////////////////////////////////////////////////////////
2099 // Nielsen's generalized polylogarithm S(n,p,x)
2103 //////////////////////////////////////////////////////////////////////
2106 static ex S_evalf(const ex& n, const ex& p, const ex& x)
2108 if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
2109 const int n_ = ex_to<numeric>(n).to_int();
2110 const int p_ = ex_to<numeric>(p).to_int();
2111 if (is_a<numeric>(x)) {
2112 const cln::cl_N x_ = ex_to<numeric>(x).to_cl_N();
2113 const cln::cl_N result = S_num(n_, p_, x_);
2114 return numeric(result);
2116 ex x_val = x.evalf();
2117 if (is_a<numeric>(x_val)) {
2118 const cln::cl_N x_val_ = ex_to<numeric>(x_val).to_cl_N();
2119 const cln::cl_N result = S_num(n_, p_, x_val_);
2120 return numeric(result);
2124 return S(n, p, x).hold();
2128 static ex S_eval(const ex& n, const ex& p, const ex& x)
2130 if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
2136 for (int i=ex_to<numeric>(p).to_int()-1; i>0; i--) {
2144 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
2145 int n_ = ex_to<numeric>(n).to_int();
2146 int p_ = ex_to<numeric>(p).to_int();
2147 const cln::cl_N x_ = ex_to<numeric>(x).to_cl_N();
2148 const cln::cl_N result = S_num(n_, p_, x_);
2149 return numeric(result);
2154 return pow(-log(1-x), p) / factorial(p);
2156 return S(n, p, x).hold();
2160 static ex S_series(const ex& n, const ex& p, const ex& x, const relational& rel, int order, unsigned options)
2163 return Li(n+1, x).series(rel, order, options);
2166 const ex x_pt = x.subs(rel, subs_options::no_pattern);
2167 if (n.info(info_flags::posint) && p.info(info_flags::posint) && x_pt.info(info_flags::numeric)) {
2168 // First special case: x==0 (derivatives have poles)
2169 if (x_pt.is_zero()) {
2172 // manually construct the primitive expansion
2173 // subsum = Euler-Zagier-Sum is needed
2174 // dirty hack (slow ...) calculation of subsum:
2175 std::vector<ex> presubsum, subsum;
2176 subsum.push_back(0);
2177 for (int i=1; i<order-1; ++i) {
2178 subsum.push_back(subsum[i-1] + numeric(1, i));
2180 for (int depth=2; depth<p; ++depth) {
2182 for (int i=1; i<order-1; ++i) {
2183 subsum[i] = subsum[i-1] + numeric(1, i) * presubsum[i-1];
2187 for (int i=1; i<order; ++i) {
2188 ser += pow(s,i) / pow(numeric(i), n+1) * subsum[i-1];
2190 // substitute the argument's series expansion
2191 ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
2192 // maybe that was terminating, so add a proper order term
2194 nseq.push_back(expair(Order(_ex1), order));
2195 ser += pseries(rel, nseq);
2196 // reexpanding it will collapse the series again
2197 return ser.series(rel, order);
2199 // TODO special cases: x==1 (branch point) and x real, >=1 (branch cut)
2200 throw std::runtime_error("S_series: don't know how to do the series expansion at this point!");
2202 // all other cases should be safe, by now:
2203 throw do_taylor(); // caught by function::series()
2207 static ex S_deriv(const ex& n, const ex& p, const ex& x, unsigned deriv_param)
2209 GINAC_ASSERT(deriv_param < 3);
2210 if (deriv_param < 2) {
2214 return S(n-1, p, x) / x;
2216 return S(n, p-1, x) / (1-x);
2221 static void S_print_latex(const ex& n, const ex& p, const ex& x, const print_context& c)
2223 c.s << "\\mathrm{S}_{";
2233 REGISTER_FUNCTION(S,
2234 evalf_func(S_evalf).
2236 series_func(S_series).
2237 derivative_func(S_deriv).
2238 print_func<print_latex>(S_print_latex).
2239 do_not_evalf_params());
2242 //////////////////////////////////////////////////////////////////////
2244 // Harmonic polylogarithm H(m,x)
2248 //////////////////////////////////////////////////////////////////////
2251 // anonymous namespace for helper functions
2255 // regulates the pole (used by 1/x-transformation)
2256 symbol H_polesign("IMSIGN");
2259 // convert parameters from H to Li representation
2260 // parameters are expected to be in expanded form, i.e. only 0, 1 and -1
2261 // returns true if some parameters are negative
2262 bool convert_parameter_H_to_Li(const lst& l, lst& m, lst& s, ex& pf)
2264 // expand parameter list
2266 for (lst::const_iterator it = l.begin(); it != l.end(); it++) {
2268 for (ex count=*it-1; count > 0; count--) {
2272 } else if (*it < -1) {
2273 for (ex count=*it+1; count < 0; count++) {
2284 bool has_negative_parameters = false;
2286 for (lst::const_iterator it = mexp.begin(); it != mexp.end(); it++) {
2292 m.append((*it+acc-1) * signum);
2294 m.append((*it-acc+1) * signum);
2300 has_negative_parameters = true;
2303 if (has_negative_parameters) {
2304 for (std::size_t i=0; i<m.nops(); i++) {
2306 m.let_op(i) = -m.op(i);
2314 return has_negative_parameters;
2318 // recursivly transforms H to corresponding multiple polylogarithms
2319 struct map_trafo_H_convert_to_Li : public map_function
2321 ex operator()(const ex& e)
2323 if (is_a<add>(e) || is_a<mul>(e)) {
2324 return e.map(*this);
2326 if (is_a<function>(e)) {
2327 std::string name = ex_to<function>(e).get_name();
2330 if (is_a<lst>(e.op(0))) {
2331 parameter = ex_to<lst>(e.op(0));
2333 parameter = lst(e.op(0));
2340 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
2341 s.let_op(0) = s.op(0) * arg;
2342 return pf * Li(m, s).hold();
2344 for (std::size_t i=0; i<m.nops(); i++) {
2347 s.let_op(0) = s.op(0) * arg;
2348 return Li(m, s).hold();
2357 // recursivly transforms H to corresponding zetas
2358 struct map_trafo_H_convert_to_zeta : public map_function
2360 ex operator()(const ex& e)
2362 if (is_a<add>(e) || is_a<mul>(e)) {
2363 return e.map(*this);
2365 if (is_a<function>(e)) {
2366 std::string name = ex_to<function>(e).get_name();
2369 if (is_a<lst>(e.op(0))) {
2370 parameter = ex_to<lst>(e.op(0));
2372 parameter = lst(e.op(0));
2378 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
2379 return pf * zeta(m, s);
2390 // remove trailing zeros from H-parameters
2391 struct map_trafo_H_reduce_trailing_zeros : public map_function
2393 ex operator()(const ex& e)
2395 if (is_a<add>(e) || is_a<mul>(e)) {
2396 return e.map(*this);
2398 if (is_a<function>(e)) {
2399 std::string name = ex_to<function>(e).get_name();
2402 if (is_a<lst>(e.op(0))) {
2403 parameter = ex_to<lst>(e.op(0));
2405 parameter = lst(e.op(0));
2408 if (parameter.op(parameter.nops()-1) == 0) {
2411 if (parameter.nops() == 1) {
2416 lst::const_iterator it = parameter.begin();
2417 while ((it != parameter.end()) && (*it == 0)) {
2420 if (it == parameter.end()) {
2421 return pow(log(arg),parameter.nops()) / factorial(parameter.nops());
2425 parameter.remove_last();
2426 std::size_t lastentry = parameter.nops();
2427 while ((lastentry > 0) && (parameter[lastentry-1] == 0)) {
2432 ex result = log(arg) * H(parameter,arg).hold();
2434 for (ex i=0; i<lastentry; i++) {
2435 if (parameter[i] > 0) {
2437 result -= (acc + parameter[i]-1) * H(parameter, arg).hold();
2440 } else if (parameter[i] < 0) {
2442 result -= (acc + abs(parameter[i]+1)) * H(parameter, arg).hold();
2450 if (lastentry < parameter.nops()) {
2451 result = result / (parameter.nops()-lastentry+1);
2452 return result.map(*this);
2464 // returns an expression with zeta functions corresponding to the parameter list for H
2465 ex convert_H_to_zeta(const lst& m)
2467 symbol xtemp("xtemp");
2468 map_trafo_H_reduce_trailing_zeros filter;
2469 map_trafo_H_convert_to_zeta filter2;
2470 return filter2(filter(H(m, xtemp).hold())).subs(xtemp == 1);
2474 // convert signs form Li to H representation
2475 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf)
2478 lst::const_iterator itm = m.begin();
2479 lst::const_iterator itx = ++x.begin();
2484 while (itx != x.end()) {
2485 signum *= (*itx > 0) ? 1 : -1;
2487 res.append((*itm) * signum);
2495 // multiplies an one-dimensional H with another H
2497 ex trafo_H_mult(const ex& h1, const ex& h2)
2502 ex h1nops = h1.op(0).nops();
2503 ex h2nops = h2.op(0).nops();
2505 hshort = h2.op(0).op(0);
2506 hlong = ex_to<lst>(h1.op(0));
2508 hshort = h1.op(0).op(0);
2510 hlong = ex_to<lst>(h2.op(0));
2512 hlong = h2.op(0).op(0);
2515 for (std::size_t i=0; i<=hlong.nops(); i++) {
2519 newparameter.append(hlong[j]);
2521 newparameter.append(hshort);
2522 for (; j<hlong.nops(); j++) {
2523 newparameter.append(hlong[j]);
2525 res += H(newparameter, h1.op(1)).hold();
2531 // applies trafo_H_mult recursively on expressions
2532 struct map_trafo_H_mult : public map_function
2534 ex operator()(const ex& e)
2537 return e.map(*this);
2545 for (std::size_t pos=0; pos<e.nops(); pos++) {
2546 if (is_a<power>(e.op(pos)) && is_a<function>(e.op(pos).op(0))) {
2547 std::string name = ex_to<function>(e.op(pos).op(0)).get_name();
2549 for (ex i=0; i<e.op(pos).op(1); i++) {
2550 Hlst.append(e.op(pos).op(0));
2554 } else if (is_a<function>(e.op(pos))) {
2555 std::string name = ex_to<function>(e.op(pos)).get_name();
2557 if (e.op(pos).op(0).nops() > 1) {
2560 Hlst.append(e.op(pos));
2565 result *= e.op(pos);
2568 if (Hlst.nops() > 0) {
2569 firstH = Hlst[Hlst.nops()-1];
2576 if (Hlst.nops() > 0) {
2577 ex buffer = trafo_H_mult(firstH, Hlst.op(0));
2579 for (std::size_t i=1; i<Hlst.nops(); i++) {
2580 result *= Hlst.op(i);
2582 result = result.expand();
2583 map_trafo_H_mult recursion;
2584 return recursion(result);
2595 // do integration [ReV] (55)
2596 // put parameter 0 in front of existing parameters
2597 ex trafo_H_1tx_prepend_zero(const ex& e, const ex& arg)
2601 if (is_a<function>(e)) {
2602 name = ex_to<function>(e).get_name();
2607 for (std::size_t i=0; i<e.nops(); i++) {
2608 if (is_a<function>(e.op(i))) {
2609 std::string name = ex_to<function>(e.op(i)).get_name();
2617 lst newparameter = ex_to<lst>(h.op(0));
2618 newparameter.prepend(0);
2619 ex addzeta = convert_H_to_zeta(newparameter);
2620 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
2622 return e * (-H(lst(ex(0)),1/arg).hold());
2627 // do integration [ReV] (49)
2628 // put parameter 1 in front of existing parameters
2629 ex trafo_H_prepend_one(const ex& e, const ex& arg)
2633 if (is_a<function>(e)) {
2634 name = ex_to<function>(e).get_name();
2639 for (std::size_t i=0; i<e.nops(); i++) {
2640 if (is_a<function>(e.op(i))) {
2641 std::string name = ex_to<function>(e.op(i)).get_name();
2649 lst newparameter = ex_to<lst>(h.op(0));
2650 newparameter.prepend(1);
2651 return e.subs(h == H(newparameter, h.op(1)).hold());
2653 return e * H(lst(ex(1)),1-arg).hold();
2658 // do integration [ReV] (55)
2659 // put parameter -1 in front of existing parameters
2660 ex trafo_H_1tx_prepend_minusone(const ex& e, const ex& arg)
2664 if (is_a<function>(e)) {
2665 name = ex_to<function>(e).get_name();
2670 for (std::size_t i=0; i<e.nops(); i++) {
2671 if (is_a<function>(e.op(i))) {
2672 std::string name = ex_to<function>(e.op(i)).get_name();
2680 lst newparameter = ex_to<lst>(h.op(0));
2681 newparameter.prepend(-1);
2682 ex addzeta = convert_H_to_zeta(newparameter);
2683 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
2685 ex addzeta = convert_H_to_zeta(lst(ex(-1)));
2686 return (e * (addzeta - H(lst(ex(-1)),1/arg).hold())).expand();
2691 // do integration [ReV] (55)
2692 // put parameter -1 in front of existing parameters
2693 ex trafo_H_1mxt1px_prepend_minusone(const ex& e, const ex& arg)
2697 if (is_a<function>(e)) {
2698 name = ex_to<function>(e).get_name();
2703 for (std::size_t i = 0; i < e.nops(); i++) {
2704 if (is_a<function>(e.op(i))) {
2705 std::string name = ex_to<function>(e.op(i)).get_name();
2713 lst newparameter = ex_to<lst>(h.op(0));
2714 newparameter.prepend(-1);
2715 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
2717 return (e * H(lst(ex(-1)),(1-arg)/(1+arg)).hold()).expand();
2722 // do integration [ReV] (55)
2723 // put parameter 1 in front of existing parameters
2724 ex trafo_H_1mxt1px_prepend_one(const ex& e, const ex& arg)
2728 if (is_a<function>(e)) {
2729 name = ex_to<function>(e).get_name();
2734 for (std::size_t i = 0; i < e.nops(); i++) {
2735 if (is_a<function>(e.op(i))) {
2736 std::string name = ex_to<function>(e.op(i)).get_name();
2744 lst newparameter = ex_to<lst>(h.op(0));
2745 newparameter.prepend(1);
2746 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
2748 return (e * H(lst(ex(1)),(1-arg)/(1+arg)).hold()).expand();
2753 // do x -> 1-x transformation
2754 struct map_trafo_H_1mx : public map_function
2756 ex operator()(const ex& e)
2758 if (is_a<add>(e) || is_a<mul>(e)) {
2759 return e.map(*this);
2762 if (is_a<function>(e)) {
2763 std::string name = ex_to<function>(e).get_name();
2766 lst parameter = ex_to<lst>(e.op(0));
2769 // special cases if all parameters are either 0, 1 or -1
2770 bool allthesame = true;
2771 if (parameter.op(0) == 0) {
2772 for (std::size_t i = 1; i < parameter.nops(); i++) {
2773 if (parameter.op(i) != 0) {
2780 for (int i=parameter.nops(); i>0; i--) {
2781 newparameter.append(1);
2783 return pow(-1, parameter.nops()) * H(newparameter, 1-arg).hold();
2785 } else if (parameter.op(0) == -1) {
2786 throw std::runtime_error("map_trafo_H_1mx: cannot handle weights equal -1!");
2788 for (std::size_t i = 1; i < parameter.nops(); i++) {
2789 if (parameter.op(i) != 1) {
2796 for (int i=parameter.nops(); i>0; i--) {
2797 newparameter.append(0);
2799 return pow(-1, parameter.nops()) * H(newparameter, 1-arg).hold();
2803 lst newparameter = parameter;
2804 newparameter.remove_first();
2806 if (parameter.op(0) == 0) {
2809 ex res = convert_H_to_zeta(parameter);
2810 //ex res = convert_from_RV(parameter, 1).subs(H(wild(1),wild(2))==zeta(wild(1)));
2811 map_trafo_H_1mx recursion;
2812 ex buffer = recursion(H(newparameter, arg).hold());
2813 if (is_a<add>(buffer)) {
2814 for (std::size_t i = 0; i < buffer.nops(); i++) {
2815 res -= trafo_H_prepend_one(buffer.op(i), arg);
2818 res -= trafo_H_prepend_one(buffer, arg);
2825 map_trafo_H_1mx recursion;
2826 map_trafo_H_mult unify;
2827 ex res = H(lst(ex(1)), arg).hold() * H(newparameter, arg).hold();
2828 std::size_t firstzero = 0;
2829 while (parameter.op(firstzero) == 1) {
2832 for (std::size_t i = firstzero-1; i < parameter.nops()-1; i++) {
2836 newparameter.append(parameter[j+1]);
2838 newparameter.append(1);
2839 for (; j<parameter.nops()-1; j++) {
2840 newparameter.append(parameter[j+1]);
2842 res -= H(newparameter, arg).hold();
2844 res = recursion(res).expand() / firstzero;
2854 // do x -> 1/x transformation
2855 struct map_trafo_H_1overx : public map_function
2857 ex operator()(const ex& e)
2859 if (is_a<add>(e) || is_a<mul>(e)) {
2860 return e.map(*this);
2863 if (is_a<function>(e)) {
2864 std::string name = ex_to<function>(e).get_name();
2867 lst parameter = ex_to<lst>(e.op(0));
2870 // special cases if all parameters are either 0, 1 or -1
2871 bool allthesame = true;
2872 if (parameter.op(0) == 0) {
2873 for (std::size_t i = 1; i < parameter.nops(); i++) {
2874 if (parameter.op(i) != 0) {
2880 return pow(-1, parameter.nops()) * H(parameter, 1/arg).hold();
2882 } else if (parameter.op(0) == -1) {
2883 for (std::size_t i = 1; i < parameter.nops(); i++) {
2884 if (parameter.op(i) != -1) {
2890 map_trafo_H_mult unify;
2891 return unify((pow(H(lst(ex(-1)),1/arg).hold() - H(lst(ex(0)),1/arg).hold(), parameter.nops())
2892 / factorial(parameter.nops())).expand());
2895 for (std::size_t i = 1; i < parameter.nops(); i++) {
2896 if (parameter.op(i) != 1) {
2902 map_trafo_H_mult unify;
2903 return unify((pow(H(lst(ex(1)),1/arg).hold() + H(lst(ex(0)),1/arg).hold() + H_polesign, parameter.nops())
2904 / factorial(parameter.nops())).expand());
2908 lst newparameter = parameter;
2909 newparameter.remove_first();
2911 if (parameter.op(0) == 0) {
2914 ex res = convert_H_to_zeta(parameter);
2915 map_trafo_H_1overx recursion;
2916 ex buffer = recursion(H(newparameter, arg).hold());
2917 if (is_a<add>(buffer)) {
2918 for (std::size_t i = 0; i < buffer.nops(); i++) {
2919 res += trafo_H_1tx_prepend_zero(buffer.op(i), arg);
2922 res += trafo_H_1tx_prepend_zero(buffer, arg);
2926 } else if (parameter.op(0) == -1) {
2928 // leading negative one
2929 ex res = convert_H_to_zeta(parameter);
2930 map_trafo_H_1overx recursion;
2931 ex buffer = recursion(H(newparameter, arg).hold());
2932 if (is_a<add>(buffer)) {
2933 for (std::size_t i = 0; i < buffer.nops(); i++) {
2934 res += trafo_H_1tx_prepend_zero(buffer.op(i), arg) - trafo_H_1tx_prepend_minusone(buffer.op(i), arg);
2937 res += trafo_H_1tx_prepend_zero(buffer, arg) - trafo_H_1tx_prepend_minusone(buffer, arg);
2944 map_trafo_H_1overx recursion;
2945 map_trafo_H_mult unify;
2946 ex res = H(lst(ex(1)), arg).hold() * H(newparameter, arg).hold();
2947 std::size_t firstzero = 0;
2948 while (parameter.op(firstzero) == 1) {
2951 for (std::size_t i = firstzero-1; i < parameter.nops() - 1; i++) {
2955 newparameter.append(parameter[j+1]);
2957 newparameter.append(1);
2958 for (; j<parameter.nops()-1; j++) {
2959 newparameter.append(parameter[j+1]);
2961 res -= H(newparameter, arg).hold();
2963 res = recursion(res).expand() / firstzero;
2975 // do x -> (1-x)/(1+x) transformation
2976 struct map_trafo_H_1mxt1px : public map_function
2978 ex operator()(const ex& e)
2980 if (is_a<add>(e) || is_a<mul>(e)) {
2981 return e.map(*this);
2984 if (is_a<function>(e)) {
2985 std::string name = ex_to<function>(e).get_name();
2988 lst parameter = ex_to<lst>(e.op(0));
2991 // special cases if all parameters are either 0, 1 or -1
2992 bool allthesame = true;
2993 if (parameter.op(0) == 0) {
2994 for (std::size_t i = 1; i < parameter.nops(); i++) {
2995 if (parameter.op(i) != 0) {
3001 map_trafo_H_mult unify;
3002 return unify((pow(-H(lst(ex(1)),(1-arg)/(1+arg)).hold() - H(lst(ex(-1)),(1-arg)/(1+arg)).hold(), parameter.nops())
3003 / factorial(parameter.nops())).expand());
3005 } else if (parameter.op(0) == -1) {
3006 for (std::size_t i = 1; i < parameter.nops(); i++) {
3007 if (parameter.op(i) != -1) {
3013 map_trafo_H_mult unify;
3014 return unify((pow(log(2) - H(lst(ex(-1)),(1-arg)/(1+arg)).hold(), parameter.nops())
3015 / factorial(parameter.nops())).expand());
3018 for (std::size_t i = 1; i < parameter.nops(); i++) {
3019 if (parameter.op(i) != 1) {
3025 map_trafo_H_mult unify;
3026 return unify((pow(-log(2) - H(lst(ex(0)),(1-arg)/(1+arg)).hold() + H(lst(ex(-1)),(1-arg)/(1+arg)).hold(), parameter.nops())
3027 / factorial(parameter.nops())).expand());
3031 lst newparameter = parameter;
3032 newparameter.remove_first();
3034 if (parameter.op(0) == 0) {
3037 ex res = convert_H_to_zeta(parameter);
3038 map_trafo_H_1mxt1px recursion;
3039 ex buffer = recursion(H(newparameter, arg).hold());
3040 if (is_a<add>(buffer)) {
3041 for (std::size_t i = 0; i < buffer.nops(); i++) {
3042 res -= trafo_H_1mxt1px_prepend_one(buffer.op(i), arg) + trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
3045 res -= trafo_H_1mxt1px_prepend_one(buffer, arg) + trafo_H_1mxt1px_prepend_minusone(buffer, arg);
3049 } else if (parameter.op(0) == -1) {
3051 // leading negative one
3052 ex res = convert_H_to_zeta(parameter);
3053 map_trafo_H_1mxt1px recursion;
3054 ex buffer = recursion(H(newparameter, arg).hold());
3055 if (is_a<add>(buffer)) {
3056 for (std::size_t i = 0; i < buffer.nops(); i++) {
3057 res -= trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
3060 res -= trafo_H_1mxt1px_prepend_minusone(buffer, arg);
3067 map_trafo_H_1mxt1px recursion;
3068 map_trafo_H_mult unify;
3069 ex res = H(lst(ex(1)), arg).hold() * H(newparameter, arg).hold();
3070 std::size_t firstzero = 0;
3071 while (parameter.op(firstzero) == 1) {
3074 for (std::size_t i = firstzero - 1; i < parameter.nops() - 1; i++) {
3078 newparameter.append(parameter[j+1]);
3080 newparameter.append(1);
3081 for (; j<parameter.nops()-1; j++) {
3082 newparameter.append(parameter[j+1]);
3084 res -= H(newparameter, arg).hold();
3086 res = recursion(res).expand() / firstzero;
3098 // do the actual summation.
3099 cln::cl_N H_do_sum(const std::vector<int>& m, const cln::cl_N& x)
3101 const int j = m.size();
3103 std::vector<cln::cl_N> t(j);
3105 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3106 cln::cl_N factor = cln::expt(x, j) * one;
3112 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),m[j-1]);
3113 for (int k=j-2; k>=1; k--) {
3114 t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), m[k]);
3116 t[0] = t[0] + t[1] * factor / cln::expt(cln::cl_I(q+j-1), m[0]);
3117 factor = factor * x;
3118 } while (t[0] != t0buf);
3124 } // end of anonymous namespace
3127 //////////////////////////////////////////////////////////////////////
3129 // Harmonic polylogarithm H(m,x)
3133 //////////////////////////////////////////////////////////////////////
3136 static ex H_evalf(const ex& x1, const ex& x2)
3138 if (is_a<lst>(x1)) {
3141 if (is_a<numeric>(x2)) {
3142 x = ex_to<numeric>(x2).to_cl_N();
3144 ex x2_val = x2.evalf();
3145 if (is_a<numeric>(x2_val)) {
3146 x = ex_to<numeric>(x2_val).to_cl_N();
3150 for (std::size_t i = 0; i < x1.nops(); i++) {
3151 if (!x1.op(i).info(info_flags::integer)) {
3152 return H(x1, x2).hold();
3155 if (x1.nops() < 1) {
3156 return H(x1, x2).hold();
3159 const lst& morg = ex_to<lst>(x1);
3160 // remove trailing zeros ...
3161 if (*(--morg.end()) == 0) {
3162 symbol xtemp("xtemp");
3163 map_trafo_H_reduce_trailing_zeros filter;
3164 return filter(H(x1, xtemp).hold()).subs(xtemp==x2).evalf();
3166 // ... and expand parameter notation
3167 bool has_minus_one = false;
3169 for (lst::const_iterator it = morg.begin(); it != morg.end(); it++) {
3171 for (ex count=*it-1; count > 0; count--) {
3175 } else if (*it <= -1) {
3176 for (ex count=*it+1; count < 0; count++) {
3180 has_minus_one = true;
3187 if (cln::abs(x) < 0.95) {
3191 if (convert_parameter_H_to_Li(m, m_lst, s_lst, pf)) {
3192 // negative parameters -> s_lst is filled
3193 std::vector<int> m_int;
3194 std::vector<cln::cl_N> x_cln;
3195 for (lst::const_iterator it_int = m_lst.begin(), it_cln = s_lst.begin();
3196 it_int != m_lst.end(); it_int++, it_cln++) {
3197 m_int.push_back(ex_to<numeric>(*it_int).to_int());
3198 x_cln.push_back(ex_to<numeric>(*it_cln).to_cl_N());
3200 x_cln.front() = x_cln.front() * x;
3201 return pf * numeric(multipleLi_do_sum(m_int, x_cln));
3203 // only positive parameters
3205 if (m_lst.nops() == 1) {
3206 return Li(m_lst.op(0), x2).evalf();
3208 std::vector<int> m_int;
3209 for (lst::const_iterator it = m_lst.begin(); it != m_lst.end(); it++) {
3210 m_int.push_back(ex_to<numeric>(*it).to_int());
3212 return numeric(H_do_sum(m_int, x));
3216 symbol xtemp("xtemp");
3219 // ensure that the realpart of the argument is positive
3220 if (cln::realpart(x) < 0) {
3222 for (std::size_t i = 0; i < m.nops(); i++) {
3224 m.let_op(i) = -m.op(i);
3231 if (cln::abs(x) >= 2.0) {
3232 map_trafo_H_1overx trafo;
3233 res *= trafo(H(m, xtemp));
3234 if (cln::imagpart(x) <= 0) {
3235 res = res.subs(H_polesign == -I*Pi);
3237 res = res.subs(H_polesign == I*Pi);
3239 return res.subs(xtemp == numeric(x)).evalf();
3242 // check transformations for 0.95 <= |x| < 2.0
3244 // |(1-x)/(1+x)| < 0.9 -> circular area with center=9.53+0i and radius=9.47
3245 if (cln::abs(x-9.53) <= 9.47) {
3247 map_trafo_H_1mxt1px trafo;
3248 res *= trafo(H(m, xtemp));
3251 if (has_minus_one) {
3252 map_trafo_H_convert_to_Li filter;
3253 return filter(H(m, numeric(x)).hold()).evalf();
3255 map_trafo_H_1mx trafo;
3256 res *= trafo(H(m, xtemp));
3259 return res.subs(xtemp == numeric(x)).evalf();
3262 return H(x1,x2).hold();
3266 static ex H_eval(const ex& m_, const ex& x)
3269 if (is_a<lst>(m_)) {
3274 if (m.nops() == 0) {
3282 if (*m.begin() > _ex1) {
3288 } else if (*m.begin() < _ex_1) {
3294 } else if (*m.begin() == _ex0) {
3301 for (lst::const_iterator it = ++m.begin(); it != m.end(); it++) {
3302 if ((*it).info(info_flags::integer)) {
3313 } else if (*it < _ex_1) {
3333 } else if (step == 1) {
3345 // if some m_i is not an integer
3346 return H(m_, x).hold();
3349 if ((x == _ex1) && (*(--m.end()) != _ex0)) {
3350 return convert_H_to_zeta(m);
3356 return H(m_, x).hold();
3358 return pow(log(x), m.nops()) / factorial(m.nops());
3361 return pow(-pos1*log(1-pos1*x), m.nops()) / factorial(m.nops());
3363 } else if ((step == 1) && (pos1 == _ex0)){
3368 return pow(-1, p) * S(n, p, -x);
3374 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
3375 return H(m_, x).evalf();
3377 return H(m_, x).hold();
3381 static ex H_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
3384 seq.push_back(expair(H(m, x), 0));
3385 return pseries(rel, seq);
3389 static ex H_deriv(const ex& m_, const ex& x, unsigned deriv_param)
3391 GINAC_ASSERT(deriv_param < 2);
3392 if (deriv_param == 0) {
3396 if (is_a<lst>(m_)) {
3412 return 1/(1-x) * H(m, x);
3413 } else if (mb == _ex_1) {
3414 return 1/(1+x) * H(m, x);
3421 static void H_print_latex(const ex& m_, const ex& x, const print_context& c)
3424 if (is_a<lst>(m_)) {
3429 c.s << "\\mathrm{H}_{";
3430 lst::const_iterator itm = m.begin();
3433 for (; itm != m.end(); itm++) {
3443 REGISTER_FUNCTION(H,
3444 evalf_func(H_evalf).
3446 series_func(H_series).
3447 derivative_func(H_deriv).
3448 print_func<print_latex>(H_print_latex).
3449 do_not_evalf_params());
3452 // takes a parameter list for H and returns an expression with corresponding multiple polylogarithms
3453 ex convert_H_to_Li(const ex& m, const ex& x)
3455 map_trafo_H_reduce_trailing_zeros filter;
3456 map_trafo_H_convert_to_Li filter2;
3458 return filter2(filter(H(m, x).hold()));
3460 return filter2(filter(H(lst(m), x).hold()));
3465 //////////////////////////////////////////////////////////////////////
3467 // Multiple zeta values zeta(x) and zeta(x,s)
3471 //////////////////////////////////////////////////////////////////////
3474 // anonymous namespace for helper functions
3478 // parameters and data for [Cra] algorithm
3479 const cln::cl_N lambda = cln::cl_N("319/320");
3481 void halfcyclic_convolute(const std::vector<cln::cl_N>& a, const std::vector<cln::cl_N>& b, std::vector<cln::cl_N>& c)
3483 const int size = a.size();
3484 for (int n=0; n<size; n++) {
3486 for (int m=0; m<=n; m++) {
3487 c[n] = c[n] + a[m]*b[n-m];
3494 static void initcX(std::vector<cln::cl_N>& crX,
3495 const std::vector<int>& s,
3498 std::vector<cln::cl_N> crB(L2 + 1);
3499 for (int i=0; i<=L2; i++)
3500 crB[i] = bernoulli(i).to_cl_N() / cln::factorial(i);
3504 std::vector<std::vector<cln::cl_N> > crG(s.size() - 1, std::vector<cln::cl_N>(L2 + 1));
3505 for (int m=0; m < (int)s.size() - 1; m++) {
3508 for (int i = 0; i <= L2; i++)
3509 crG[m][i] = cln::factorial(i + Sm - m - 2) / cln::factorial(i + Smp1 - m - 2);
3514 for (std::size_t m = 0; m < s.size() - 1; m++) {
3515 std::vector<cln::cl_N> Xbuf(L2 + 1);
3516 for (int i = 0; i <= L2; i++)
3517 Xbuf[i] = crX[i] * crG[m][i];
3519 halfcyclic_convolute(Xbuf, crB, crX);
3525 static cln::cl_N crandall_Y_loop(const cln::cl_N& Sqk,
3526 const std::vector<cln::cl_N>& crX)
3528 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3529 cln::cl_N factor = cln::expt(lambda, Sqk);
3530 cln::cl_N res = factor / Sqk * crX[0] * one;
3535 factor = factor * lambda;
3537 res = res + crX[N] * factor / (N+Sqk);
3538 } while ((res != resbuf) || cln::zerop(crX[N]));
3544 static void calc_f(std::vector<std::vector<cln::cl_N> >& f_kj,
3545 const int maxr, const int L1)
3547 cln::cl_N t0, t1, t2, t3, t4;
3549 std::vector<std::vector<cln::cl_N> >::iterator it = f_kj.begin();
3550 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3552 t0 = cln::exp(-lambda);
3554 for (k=1; k<=L1; k++) {
3557 for (j=1; j<=maxr; j++) {
3560 for (i=2; i<=j; i++) {
3564 (*it).push_back(t2 * t3 * cln::expt(cln::cl_I(k),-j) * one);
3572 static cln::cl_N crandall_Z(const std::vector<int>& s,
3573 const std::vector<std::vector<cln::cl_N> >& f_kj)
3575 const int j = s.size();
3584 t0 = t0 + f_kj[q+j-2][s[0]-1];
3585 } while (t0 != t0buf);
3587 return t0 / cln::factorial(s[0]-1);
3590 std::vector<cln::cl_N> t(j);
3597 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),s[j-1]);
3598 for (int k=j-2; k>=1; k--) {
3599 t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), s[k]);
3601 t[0] = t[0] + t[1] * f_kj[q+j-2][s[0]-1];
3602 } while (t[0] != t0buf);
3604 return t[0] / cln::factorial(s[0]-1);
3609 cln::cl_N zeta_do_sum_Crandall(const std::vector<int>& s)
3611 std::vector<int> r = s;
3612 const int j = r.size();
3616 // decide on maximal size of f_kj for crandall_Z
3620 L1 = Digits * 3 + j*2;
3624 // decide on maximal size of crX for crandall_Y
3627 } else if (Digits < 86) {
3629 } else if (Digits < 192) {
3631 } else if (Digits < 394) {
3633 } else if (Digits < 808) {
3643 for (int i=0; i<j; i++) {
3650 std::vector<std::vector<cln::cl_N> > f_kj(L1);
3651 calc_f(f_kj, maxr, L1);
3653 const cln::cl_N r0factorial = cln::factorial(r[0]-1);
3655 std::vector<int> rz;
3658 for (int k=r.size()-1; k>0; k--) {
3660 rz.insert(rz.begin(), r.back());
3661 skp1buf = rz.front();
3665 std::vector<cln::cl_N> crX;
3668 for (int q=0; q<skp1buf; q++) {
3670 cln::cl_N pp1 = crandall_Y_loop(Srun+q-k, crX);
3671 cln::cl_N pp2 = crandall_Z(rz, f_kj);
3676 res = res - pp1 * pp2 / cln::factorial(q);
3678 res = res + pp1 * pp2 / cln::factorial(q);
3681 rz.front() = skp1buf;
3683 rz.insert(rz.begin(), r.back());
3685 std::vector<cln::cl_N> crX;
3686 initcX(crX, rz, L2);
3688 res = (res + crandall_Y_loop(S-j, crX)) / r0factorial
3689 + crandall_Z(rz, f_kj);
3695 cln::cl_N zeta_do_sum_simple(const std::vector<int>& r)
3697 const int j = r.size();
3699 // buffer for subsums
3700 std::vector<cln::cl_N> t(j);
3701 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3708 t[j-1] = t[j-1] + one / cln::expt(cln::cl_I(q),r[j-1]);
3709 for (int k=j-2; k>=0; k--) {
3710 t[k] = t[k] + one * t[k+1] / cln::expt(cln::cl_I(q+j-1-k), r[k]);
3712 } while (t[0] != t0buf);
3718 // does Hoelder convolution. see [BBB] (7.0)
3719 cln::cl_N zeta_do_Hoelder_convolution(const std::vector<int>& m_, const std::vector<int>& s_)
3721 // prepare parameters
3722 // holds Li arguments in [BBB] notation
3723 std::vector<int> s = s_;
3724 std::vector<int> m_p = m_;
3725 std::vector<int> m_q;
3726 // holds Li arguments in nested sums notation
3727 std::vector<cln::cl_N> s_p(s.size(), cln::cl_N(1));
3728 s_p[0] = s_p[0] * cln::cl_N("1/2");
3729 // convert notations
3731 for (std::size_t i = 0; i < s_.size(); i++) {
3736 s[i] = sig * std::abs(s[i]);
3738 std::vector<cln::cl_N> s_q;
3739 cln::cl_N signum = 1;
3742 cln::cl_N res = multipleLi_do_sum(m_p, s_p);
3747 // change parameters
3748 if (s.front() > 0) {
3749 if (m_p.front() == 1) {
3750 m_p.erase(m_p.begin());
3751 s_p.erase(s_p.begin());
3752 if (s_p.size() > 0) {
3753 s_p.front() = s_p.front() * cln::cl_N("1/2");
3759 m_q.insert(m_q.begin(), 1);
3760 if (s_q.size() > 0) {
3761 s_q.front() = s_q.front() * 2;
3763 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
3766 if (m_p.front() == 1) {
3767 m_p.erase(m_p.begin());
3768 cln::cl_N spbuf = s_p.front();
3769 s_p.erase(s_p.begin());
3770 if (s_p.size() > 0) {
3771 s_p.front() = s_p.front() * spbuf;
3774 m_q.insert(m_q.begin(), 1);
3775 if (s_q.size() > 0) {
3776 s_q.front() = s_q.front() * 4;
3778 s_q.insert(s_q.begin(), cln::cl_N("1/4"));
3782 m_q.insert(m_q.begin(), 1);
3783 if (s_q.size() > 0) {
3784 s_q.front() = s_q.front() * 2;
3786 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
3791 if (m_p.size() == 0) break;
3793 res = res + signum * multipleLi_do_sum(m_p, s_p) * multipleLi_do_sum(m_q, s_q);
3798 res = res + signum * multipleLi_do_sum(m_q, s_q);
3804 } // end of anonymous namespace
3807 //////////////////////////////////////////////////////////////////////
3809 // Multiple zeta values zeta(x)
3813 //////////////////////////////////////////////////////////////////////
3816 static ex zeta1_evalf(const ex& x)
3818 if (is_exactly_a<lst>(x) && (x.nops()>1)) {
3820 // multiple zeta value
3821 const int count = x.nops();
3822 const lst& xlst = ex_to<lst>(x);
3823 std::vector<int> r(count);
3825 // check parameters and convert them
3826 lst::const_iterator it1 = xlst.begin();
3827 std::vector<int>::iterator it2 = r.begin();
3829 if (!(*it1).info(info_flags::posint)) {
3830 return zeta(x).hold();
3832 *it2 = ex_to<numeric>(*it1).to_int();
3835 } while (it2 != r.end());
3837 // check for divergence
3839 return zeta(x).hold();
3842 // decide on summation algorithm
3843 // this is still a bit clumsy
3844 int limit = (Digits>17) ? 10 : 6;
3845 if ((r[0] < limit) || ((count > 3) && (r[1] < limit/2))) {
3846 return numeric(zeta_do_sum_Crandall(r));
3848 return numeric(zeta_do_sum_simple(r));
3852 // single zeta value
3853 if (is_exactly_a<numeric>(x) && (x != 1)) {
3855 return zeta(ex_to<numeric>(x));
3856 } catch (const dunno &e) { }
3859 return zeta(x).hold();
3863 static ex zeta1_eval(const ex& m)
3865 if (is_exactly_a<lst>(m)) {
3866 if (m.nops() == 1) {
3867 return zeta(m.op(0));
3869 return zeta(m).hold();
3872 if (m.info(info_flags::numeric)) {
3873 const numeric& y = ex_to<numeric>(m);
3874 // trap integer arguments:
3875 if (y.is_integer()) {
3879 if (y.is_equal(*_num1_p)) {
3880 return zeta(m).hold();
3882 if (y.info(info_flags::posint)) {
3883 if (y.info(info_flags::odd)) {
3884 return zeta(m).hold();
3886 return abs(bernoulli(y)) * pow(Pi, y) * pow(*_num2_p, y-(*_num1_p)) / factorial(y);
3889 if (y.info(info_flags::odd)) {
3890 return -bernoulli((*_num1_p)-y) / ((*_num1_p)-y);
3897 if (y.info(info_flags::numeric) && !y.info(info_flags::crational)) {
3898 return zeta1_evalf(m);
3901 return zeta(m).hold();
3905 static ex zeta1_deriv(const ex& m, unsigned deriv_param)
3907 GINAC_ASSERT(deriv_param==0);
3909 if (is_exactly_a<lst>(m)) {
3912 return zetaderiv(_ex1, m);
3917 static void zeta1_print_latex(const ex& m_, const print_context& c)
3920 if (is_a<lst>(m_)) {
3921 const lst& m = ex_to<lst>(m_);
3922 lst::const_iterator it = m.begin();
3925 for (; it != m.end(); it++) {
3936 unsigned zeta1_SERIAL::serial = function::register_new(function_options("zeta", 1).
3937 evalf_func(zeta1_evalf).
3938 eval_func(zeta1_eval).
3939 derivative_func(zeta1_deriv).
3940 print_func<print_latex>(zeta1_print_latex).
3941 do_not_evalf_params().
3945 //////////////////////////////////////////////////////////////////////
3947 // Alternating Euler sum zeta(x,s)
3951 //////////////////////////////////////////////////////////////////////
3954 static ex zeta2_evalf(const ex& x, const ex& s)
3956 if (is_exactly_a<lst>(x)) {
3958 // alternating Euler sum
3959 const int count = x.nops();
3960 const lst& xlst = ex_to<lst>(x);
3961 const lst& slst = ex_to<lst>(s);
3962 std::vector<int> xi(count);
3963 std::vector<int> si(count);
3965 // check parameters and convert them
3966 lst::const_iterator it_xread = xlst.begin();
3967 lst::const_iterator it_sread = slst.begin();
3968 std::vector<int>::iterator it_xwrite = xi.begin();
3969 std::vector<int>::iterator it_swrite = si.begin();
3971 if (!(*it_xread).info(info_flags::posint)) {
3972 return zeta(x, s).hold();
3974 *it_xwrite = ex_to<numeric>(*it_xread).to_int();
3975 if (*it_sread > 0) {
3984 } while (it_xwrite != xi.end());
3986 // check for divergence
3987 if ((xi[0] == 1) && (si[0] == 1)) {
3988 return zeta(x, s).hold();
3991 // use Hoelder convolution
3992 return numeric(zeta_do_Hoelder_convolution(xi, si));
3995 return zeta(x, s).hold();
3999 static ex zeta2_eval(const ex& m, const ex& s_)
4001 if (is_exactly_a<lst>(s_)) {
4002 const lst& s = ex_to<lst>(s_);
4003 for (lst::const_iterator it = s.begin(); it != s.end(); it++) {
4004 if ((*it).info(info_flags::positive)) {
4007 return zeta(m, s_).hold();
4010 } else if (s_.info(info_flags::positive)) {
4014 return zeta(m, s_).hold();
4018 static ex zeta2_deriv(const ex& m, const ex& s, unsigned deriv_param)
4020 GINAC_ASSERT(deriv_param==0);
4022 if (is_exactly_a<lst>(m)) {
4025 if ((is_exactly_a<lst>(s) && s.op(0).info(info_flags::positive)) || s.info(info_flags::positive)) {
4026 return zetaderiv(_ex1, m);
4033 static void zeta2_print_latex(const ex& m_, const ex& s_, const print_context& c)
4036 if (is_a<lst>(m_)) {
4042 if (is_a<lst>(s_)) {
4048 lst::const_iterator itm = m.begin();
4049 lst::const_iterator its = s.begin();
4051 c.s << "\\overline{";
4059 for (; itm != m.end(); itm++, its++) {
4062 c.s << "\\overline{";
4073 unsigned zeta2_SERIAL::serial = function::register_new(function_options("zeta", 2).
4074 evalf_func(zeta2_evalf).
4075 eval_func(zeta2_eval).
4076 derivative_func(zeta2_deriv).
4077 print_func<print_latex>(zeta2_print_latex).
4078 do_not_evalf_params().
4082 } // namespace GiNaC