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-2008 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
79 #include "operators.h"
82 #include "relational.h"
91 //////////////////////////////////////////////////////////////////////
93 // Classical polylogarithm Li(n,x)
97 //////////////////////////////////////////////////////////////////////
100 // anonymous namespace for helper functions
104 // lookup table for factors built from Bernoulli numbers
106 std::vector<std::vector<cln::cl_N> > Xn;
107 // initial size of Xn that should suffice for 32bit machines (must be even)
108 const int xninitsizestep = 26;
109 int xninitsize = xninitsizestep;
113 // This function calculates the X_n. The X_n are needed for speed up of classical polylogarithms.
114 // With these numbers the polylogs can be calculated as follows:
115 // Li_p (x) = \sum_{n=0}^\infty X_{p-2}(n) u^{n+1}/(n+1)! with u = -log(1-x)
116 // X_0(n) = B_n (Bernoulli numbers)
117 // X_p(n) = \sum_{k=0}^n binomial(n,k) B_{n-k} / (k+1) * X_{p-1}(k)
118 // The calculation of Xn depends on X0 and X{n-1}.
119 // X_0 is special, it holds only the non-zero Bernoulli numbers with index 2 or greater.
120 // This results in a slightly more complicated algorithm for the X_n.
121 // The first index in Xn corresponds to the index of the polylog minus 2.
122 // The second index in Xn corresponds to the index from the actual sum.
126 // calculate X_2 and higher (corresponding to Li_4 and higher)
127 std::vector<cln::cl_N> buf(xninitsize);
128 std::vector<cln::cl_N>::iterator it = buf.begin();
130 *it = -(cln::expt(cln::cl_I(2),n+1) - 1) / cln::expt(cln::cl_I(2),n+1); // i == 1
132 for (int i=2; i<=xninitsize; i++) {
134 result = 0; // k == 0
136 result = Xn[0][i/2-1]; // k == 0
138 for (int k=1; k<i-1; k++) {
139 if ( !(((i-k) & 1) && ((i-k) > 1)) ) {
140 result = result + cln::binomial(i,k) * Xn[0][(i-k)/2-1] * Xn[n-1][k-1] / (k+1);
143 result = result - cln::binomial(i,i-1) * Xn[n-1][i-2] / 2 / i; // k == i-1
144 result = result + Xn[n-1][i-1] / (i+1); // k == i
151 // special case to handle the X_0 correct
152 std::vector<cln::cl_N> buf(xninitsize);
153 std::vector<cln::cl_N>::iterator it = buf.begin();
155 *it = cln::cl_I(-3)/cln::cl_I(4); // i == 1
157 *it = cln::cl_I(17)/cln::cl_I(36); // i == 2
159 for (int i=3; i<=xninitsize; i++) {
161 result = -Xn[0][(i-3)/2]/2;
162 *it = (cln::binomial(i,1)/cln::cl_I(2) + cln::binomial(i,i-1)/cln::cl_I(i))*result;
165 result = Xn[0][i/2-1] + Xn[0][i/2-1]/(i+1);
166 for (int k=1; k<i/2; k++) {
167 result = result + cln::binomial(i,k*2) * Xn[0][k-1] * Xn[0][i/2-k-1] / (k*2+1);
176 std::vector<cln::cl_N> buf(xninitsize/2);
177 std::vector<cln::cl_N>::iterator it = buf.begin();
178 for (int i=1; i<=xninitsize/2; i++) {
179 *it = bernoulli(i*2).to_cl_N();
188 // doubles the number of entries in each Xn[]
191 const int pos0 = xninitsize / 2;
193 for (int i=1; i<=xninitsizestep/2; ++i) {
194 Xn[0].push_back(bernoulli((i+pos0)*2).to_cl_N());
197 int xend = xninitsize + xninitsizestep;
200 for (int i=xninitsize+1; i<=xend; ++i) {
202 result = -Xn[0][(i-3)/2]/2;
203 Xn[1].push_back((cln::binomial(i,1)/cln::cl_I(2) + cln::binomial(i,i-1)/cln::cl_I(i))*result);
205 result = Xn[0][i/2-1] + Xn[0][i/2-1]/(i+1);
206 for (int k=1; k<i/2; k++) {
207 result = result + cln::binomial(i,k*2) * Xn[0][k-1] * Xn[0][i/2-k-1] / (k*2+1);
209 Xn[1].push_back(result);
213 for (int n=2; n<Xn.size(); ++n) {
214 for (int i=xninitsize+1; i<=xend; ++i) {
216 result = 0; // k == 0
218 result = Xn[0][i/2-1]; // k == 0
220 for (int k=1; k<i-1; ++k) {
221 if ( !(((i-k) & 1) && ((i-k) > 1)) ) {
222 result = result + cln::binomial(i,k) * Xn[0][(i-k)/2-1] * Xn[n-1][k-1] / (k+1);
225 result = result - cln::binomial(i,i-1) * Xn[n-1][i-2] / 2 / i; // k == i-1
226 result = result + Xn[n-1][i-1] / (i+1); // k == i
227 Xn[n].push_back(result);
231 xninitsize += xninitsizestep;
235 // calculates Li(2,x) without Xn
236 cln::cl_N Li2_do_sum(const cln::cl_N& x)
240 cln::cl_N num = x * cln::cl_float(1, cln::float_format(Digits));
241 cln::cl_I den = 1; // n^2 = 1
246 den = den + i; // n^2 = 4, 9, 16, ...
248 res = res + num / den;
249 } while (res != resbuf);
254 // calculates Li(2,x) with Xn
255 cln::cl_N Li2_do_sum_Xn(const cln::cl_N& x)
257 std::vector<cln::cl_N>::const_iterator it = Xn[0].begin();
258 std::vector<cln::cl_N>::const_iterator xend = Xn[0].end();
259 cln::cl_N u = -cln::log(1-x);
260 cln::cl_N factor = u * cln::cl_float(1, cln::float_format(Digits));
261 cln::cl_N uu = cln::square(u);
262 cln::cl_N res = u - uu/4;
267 factor = factor * uu / (2*i * (2*i+1));
268 res = res + (*it) * factor;
272 it = Xn[0].begin() + (i-1);
275 } while (res != resbuf);
280 // calculates Li(n,x), n>2 without Xn
281 cln::cl_N Lin_do_sum(int n, const cln::cl_N& x)
283 cln::cl_N factor = x * cln::cl_float(1, cln::float_format(Digits));
290 res = res + factor / cln::expt(cln::cl_I(i),n);
292 } while (res != resbuf);
297 // calculates Li(n,x), n>2 with Xn
298 cln::cl_N Lin_do_sum_Xn(int n, const cln::cl_N& x)
300 std::vector<cln::cl_N>::const_iterator it = Xn[n-2].begin();
301 std::vector<cln::cl_N>::const_iterator xend = Xn[n-2].end();
302 cln::cl_N u = -cln::log(1-x);
303 cln::cl_N factor = u * cln::cl_float(1, cln::float_format(Digits));
309 factor = factor * u / i;
310 res = res + (*it) * factor;
314 it = Xn[n-2].begin() + (i-2);
315 xend = Xn[n-2].end();
317 } while (res != resbuf);
322 // forward declaration needed by function Li_projection and C below
323 const cln::cl_N S_num(int n, int p, const cln::cl_N& x);
326 // helper function for classical polylog Li
327 cln::cl_N Li_projection(int n, const cln::cl_N& x, const cln::float_format_t& prec)
329 // treat n=2 as special case
331 // check if precalculated X0 exists
336 if (cln::realpart(x) < 0.5) {
337 // choose the faster algorithm
338 // the switching point was empirically determined. the optimal point
339 // depends on hardware, Digits, ... so an approx value is okay.
340 // it solves also the problem with precision due to the u=-log(1-x) transformation
341 if (cln::abs(cln::realpart(x)) < 0.25) {
343 return Li2_do_sum(x);
345 return Li2_do_sum_Xn(x);
348 // choose the faster algorithm
349 if (cln::abs(cln::realpart(x)) > 0.75) {
350 return -Li2_do_sum(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
352 return -Li2_do_sum_Xn(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
356 // check if precalculated Xn exist
358 for (int i=xnsize; i<n-1; i++) {
363 if (cln::realpart(x) < 0.5) {
364 // choose the faster algorithm
365 // with n>=12 the "normal" summation always wins against the method with Xn
366 if ((cln::abs(cln::realpart(x)) < 0.3) || (n >= 12)) {
367 return Lin_do_sum(n, x);
369 return Lin_do_sum_Xn(n, x);
372 cln::cl_N result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
373 for (int j=0; j<n-1; j++) {
374 result = result + (S_num(n-j-1, 1, 1) - S_num(1, n-j-1, 1-x))
375 * cln::expt(cln::log(x), j) / cln::factorial(j);
382 // helper function for classical polylog Li
383 const cln::cl_N Lin_numeric(const int n, const cln::cl_N& x)
387 return -cln::log(1-x);
398 return -(1-cln::expt(cln::cl_I(2),1-n)) * cln::zeta(n);
400 if (cln::abs(realpart(x)) < 0.4 && cln::abs(cln::abs(x)-1) < 0.01) {
401 cln::cl_N result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
402 for (int j=0; j<n-1; j++) {
403 result = result + (S_num(n-j-1, 1, 1) - S_num(1, n-j-1, 1-x))
404 * cln::expt(cln::log(x), j) / cln::factorial(j);
409 // what is the desired float format?
410 // first guess: default format
411 cln::float_format_t prec = cln::default_float_format;
412 const cln::cl_N value = x;
413 // second guess: the argument's format
414 if (!instanceof(realpart(x), cln::cl_RA_ring))
415 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
416 else if (!instanceof(imagpart(x), cln::cl_RA_ring))
417 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
420 if (cln::abs(value) > 1) {
421 cln::cl_N result = -cln::expt(cln::log(-value),n) / cln::factorial(n);
422 // check if argument is complex. if it is real, the new polylog has to be conjugated.
423 if (cln::zerop(cln::imagpart(value))) {
425 result = result + conjugate(Li_projection(n, cln::recip(value), prec));
428 result = result - conjugate(Li_projection(n, cln::recip(value), prec));
433 result = result + Li_projection(n, cln::recip(value), prec);
436 result = result - Li_projection(n, cln::recip(value), prec);
440 for (int j=0; j<n-1; j++) {
441 add = add + (1+cln::expt(cln::cl_I(-1),n-j)) * (1-cln::expt(cln::cl_I(2),1-n+j))
442 * Lin_numeric(n-j,1) * cln::expt(cln::log(-value),j) / cln::factorial(j);
444 result = result - add;
448 return Li_projection(n, value, prec);
453 } // end of anonymous namespace
456 //////////////////////////////////////////////////////////////////////
458 // Multiple polylogarithm Li(n,x)
462 //////////////////////////////////////////////////////////////////////
465 // anonymous namespace for helper function
469 // performs the actual series summation for multiple polylogarithms
470 cln::cl_N multipleLi_do_sum(const std::vector<int>& s, const std::vector<cln::cl_N>& x)
472 // ensure all x <> 0.
473 for (std::vector<cln::cl_N>::const_iterator it = x.begin(); it != x.end(); ++it) {
474 if ( *it == 0 ) return cln::cl_float(0, cln::float_format(Digits));
477 const int j = s.size();
478 bool flag_accidental_zero = false;
480 std::vector<cln::cl_N> t(j);
481 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
488 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
489 for (int k=j-2; k>=0; k--) {
490 t[k] = t[k] + t[k+1] * cln::expt(x[k], q+j-1-k) / cln::expt(cln::cl_I(q+j-1-k), s[k]);
493 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
494 for (int k=j-2; k>=0; k--) {
495 flag_accidental_zero = cln::zerop(t[k+1]);
496 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]);
498 } while ( (t[0] != t0buf) || cln::zerop(t[0]) || flag_accidental_zero );
504 // converts parameter types and calls multipleLi_do_sum (convenience function for G_numeric)
505 cln::cl_N mLi_do_summation(const lst& m, const lst& x)
507 std::vector<int> m_int;
508 std::vector<cln::cl_N> x_cln;
509 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
510 m_int.push_back(ex_to<numeric>(*itm).to_int());
511 x_cln.push_back(ex_to<numeric>(*itx).to_cl_N());
513 return multipleLi_do_sum(m_int, x_cln);
517 // forward declaration for Li_eval()
518 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf);
521 // type used by the transformation functions for G
522 typedef std::vector<int> Gparameter;
525 // G_eval1-function for G transformations
526 ex G_eval1(int a, int scale, const exvector& gsyms)
529 const ex& scs = gsyms[std::abs(scale)];
530 const ex& as = gsyms[std::abs(a)];
532 return -log(1 - scs/as);
537 return log(gsyms[std::abs(scale)]);
542 // G_eval-function for G transformations
543 ex G_eval(const Gparameter& a, int scale, const exvector& gsyms)
545 // check for properties of G
546 ex sc = gsyms[std::abs(scale)];
548 bool all_zero = true;
549 bool all_ones = true;
551 for (Gparameter::const_iterator it = a.begin(); it != a.end(); ++it) {
553 const ex sym = gsyms[std::abs(*it)];
567 // care about divergent G: shuffle to separate divergencies that will be canceled
568 // later on in the transformation
569 if (newa.nops() > 1 && newa.op(0) == sc && !all_ones && a.front()!=0) {
572 Gparameter::const_iterator it = a.begin();
574 for (; it != a.end(); ++it) {
575 short_a.push_back(*it);
577 ex result = G_eval1(a.front(), scale, gsyms) * G_eval(short_a, scale, gsyms);
578 it = short_a.begin();
579 for (int i=1; i<count_ones; ++i) {
582 for (; it != short_a.end(); ++it) {
585 Gparameter::const_iterator it2 = short_a.begin();
586 for (--it2; it2 != it;) {
588 newa.push_back(*it2);
590 newa.push_back(a[0]);
592 for (; it2 != short_a.end(); ++it2) {
593 newa.push_back(*it2);
595 result -= G_eval(newa, scale, gsyms);
597 return result / count_ones;
600 // G({1,...,1};y) -> G({1};y)^k / k!
601 if (all_ones && a.size() > 1) {
602 return pow(G_eval1(a.front(),scale, gsyms), count_ones) / factorial(count_ones);
605 // G({0,...,0};y) -> log(y)^k / k!
607 return pow(log(gsyms[std::abs(scale)]), a.size()) / factorial(a.size());
610 // no special cases anymore -> convert it into Li
613 ex argbuf = gsyms[std::abs(scale)];
615 for (Gparameter::const_iterator it=a.begin(); it!=a.end(); ++it) {
617 const ex& sym = gsyms[std::abs(*it)];
618 x.append(argbuf / sym);
626 return pow(-1, x.nops()) * Li(m, x);
630 // converts data for G: pending_integrals -> a
631 Gparameter convert_pending_integrals_G(const Gparameter& pending_integrals)
633 GINAC_ASSERT(pending_integrals.size() != 1);
635 if (pending_integrals.size() > 0) {
636 // get rid of the first element, which would stand for the new upper limit
637 Gparameter new_a(pending_integrals.begin()+1, pending_integrals.end());
640 // just return empty parameter list
647 // check the parameters a and scale for G and return information about convergence, depth, etc.
648 // convergent : true if G(a,scale) is convergent
649 // depth : depth of G(a,scale)
650 // trailing_zeros : number of trailing zeros of a
651 // min_it : iterator of a pointing on the smallest element in a
652 Gparameter::const_iterator check_parameter_G(const Gparameter& a, int scale,
653 bool& convergent, int& depth, int& trailing_zeros, Gparameter::const_iterator& min_it)
659 Gparameter::const_iterator lastnonzero = a.end();
660 for (Gparameter::const_iterator it = a.begin(); it != a.end(); ++it) {
661 if (std::abs(*it) > 0) {
665 if (std::abs(*it) < scale) {
667 if ((min_it == a.end()) || (std::abs(*it) < std::abs(*min_it))) {
675 return ++lastnonzero;
679 // add scale to pending_integrals if pending_integrals is empty
680 Gparameter prepare_pending_integrals(const Gparameter& pending_integrals, int scale)
682 GINAC_ASSERT(pending_integrals.size() != 1);
684 if (pending_integrals.size() > 0) {
685 return pending_integrals;
687 Gparameter new_pending_integrals;
688 new_pending_integrals.push_back(scale);
689 return new_pending_integrals;
694 // handles trailing zeroes for an otherwise convergent integral
695 ex trailing_zeros_G(const Gparameter& a, int scale, const exvector& gsyms)
698 int depth, trailing_zeros;
699 Gparameter::const_iterator last, dummyit;
700 last = check_parameter_G(a, scale, convergent, depth, trailing_zeros, dummyit);
702 GINAC_ASSERT(convergent);
704 if ((trailing_zeros > 0) && (depth > 0)) {
706 Gparameter new_a(a.begin(), a.end()-1);
707 result += G_eval1(0, scale, gsyms) * trailing_zeros_G(new_a, scale, gsyms);
708 for (Gparameter::const_iterator it = a.begin(); it != last; ++it) {
709 Gparameter new_a(a.begin(), it);
711 new_a.insert(new_a.end(), it, a.end()-1);
712 result -= trailing_zeros_G(new_a, scale, gsyms);
715 return result / trailing_zeros;
717 return G_eval(a, scale, gsyms);
722 // G transformation [VSW] (57),(58)
723 ex depth_one_trafo_G(const Gparameter& pending_integrals, const Gparameter& a, int scale, const exvector& gsyms)
725 // pendint = ( y1, b1, ..., br )
726 // a = ( 0, ..., 0, amin )
729 // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(0, ..., 0, sr; y2)
730 // where sr replaces amin
732 GINAC_ASSERT(a.back() != 0);
733 GINAC_ASSERT(a.size() > 0);
736 Gparameter new_pending_integrals = prepare_pending_integrals(pending_integrals, std::abs(a.back()));
737 const int psize = pending_integrals.size();
740 // G(sr_{+-}; y2 ) = G(y2_{-+}; sr) - G(0; sr) + ln(-y2_{-+})
745 result += log(gsyms[ex_to<numeric>(scale).to_int()]);
747 new_pending_integrals.push_back(-scale);
750 new_pending_integrals.push_back(scale);
754 result *= trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
755 pending_integrals.front(),
760 result += trailing_zeros_G(convert_pending_integrals_G(new_pending_integrals),
761 new_pending_integrals.front(),
765 new_pending_integrals.back() = 0;
766 result -= trailing_zeros_G(convert_pending_integrals_G(new_pending_integrals),
767 new_pending_integrals.front(),
774 // G_m(sr_{+-}; y2) = -zeta_m + int_0^y2 dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
775 // - int_0^sr dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
778 result -= zeta(a.size());
780 result *= trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
781 pending_integrals.front(),
785 // term int_0^sr dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
786 // = int_0^sr dt/t G_{m-1}( t_{+-}; y2 )
787 Gparameter new_a(a.begin()+1, a.end());
788 new_pending_integrals.push_back(0);
789 result -= depth_one_trafo_G(new_pending_integrals, new_a, scale, gsyms);
791 // term int_0^y2 dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
792 // = int_0^y2 dt/t G_{m-1}( t_{+-}; y2 )
793 Gparameter new_pending_integrals_2;
794 new_pending_integrals_2.push_back(scale);
795 new_pending_integrals_2.push_back(0);
797 result += trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
798 pending_integrals.front(),
800 * depth_one_trafo_G(new_pending_integrals_2, new_a, scale, gsyms);
802 result += depth_one_trafo_G(new_pending_integrals_2, new_a, scale, gsyms);
809 // forward declaration
810 ex shuffle_G(const Gparameter & a0, const Gparameter & a1, const Gparameter & a2,
811 const Gparameter& pendint, const Gparameter& a_old, int scale,
812 const exvector& gsyms);
815 // G transformation [VSW]
816 ex G_transform(const Gparameter& pendint, const Gparameter& a, int scale,
817 const exvector& gsyms)
819 // main recursion routine
821 // pendint = ( y1, b1, ..., br )
822 // a = ( a1, ..., amin, ..., aw )
825 // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,sr,...,aw,y2)
826 // where sr replaces amin
828 // find smallest alpha, determine depth and trailing zeros, and check for convergence
830 int depth, trailing_zeros;
831 Gparameter::const_iterator min_it;
832 Gparameter::const_iterator firstzero =
833 check_parameter_G(a, scale, convergent, depth, trailing_zeros, min_it);
834 int min_it_pos = min_it - a.begin();
836 // special case: all a's are zero
843 result = G_eval(a, scale, gsyms);
845 if (pendint.size() > 0) {
846 result *= trailing_zeros_G(convert_pending_integrals_G(pendint),
853 // handle trailing zeros
854 if (trailing_zeros > 0) {
856 Gparameter new_a(a.begin(), a.end()-1);
857 result += G_eval1(0, scale, gsyms) * G_transform(pendint, new_a, scale, gsyms);
858 for (Gparameter::const_iterator it = a.begin(); it != firstzero; ++it) {
859 Gparameter new_a(a.begin(), it);
861 new_a.insert(new_a.end(), it, a.end()-1);
862 result -= G_transform(pendint, new_a, scale, gsyms);
864 return result / trailing_zeros;
869 if (pendint.size() > 0) {
870 return G_eval(convert_pending_integrals_G(pendint),
871 pendint.front(), gsyms)*
872 G_eval(a, scale, gsyms);
874 return G_eval(a, scale, gsyms);
878 // call basic transformation for depth equal one
880 return depth_one_trafo_G(pendint, a, scale, gsyms);
884 // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,sr,...,aw,y2)
885 // = int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,0,...,aw,y2)
886 // + 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)
888 // smallest element in last place
889 if (min_it + 1 == a.end()) {
890 do { --min_it; } while (*min_it == 0);
892 Gparameter a1(a.begin(),min_it+1);
893 Gparameter a2(min_it+1,a.end());
895 ex result = G_transform(pendint, a2, scale, gsyms)*
896 G_transform(empty, a1, scale, gsyms);
898 result -= shuffle_G(empty, a1, a2, pendint, a, scale, gsyms);
903 Gparameter::iterator changeit;
905 // first term G(a_1,..,0,...,a_w;a_0)
906 Gparameter new_pendint = prepare_pending_integrals(pendint, a[min_it_pos]);
907 Gparameter new_a = a;
908 new_a[min_it_pos] = 0;
909 ex result = G_transform(empty, new_a, scale, gsyms);
910 if (pendint.size() > 0) {
911 result *= trailing_zeros_G(convert_pending_integrals_G(pendint),
912 pendint.front(), gsyms);
916 changeit = new_a.begin() + min_it_pos;
917 changeit = new_a.erase(changeit);
918 if (changeit != new_a.begin()) {
919 // smallest in the middle
920 new_pendint.push_back(*changeit);
921 result -= trailing_zeros_G(convert_pending_integrals_G(new_pendint),
922 new_pendint.front(), gsyms)*
923 G_transform(empty, new_a, scale, gsyms);
924 int buffer = *changeit;
926 result += G_transform(new_pendint, new_a, scale, gsyms);
928 new_pendint.pop_back();
930 new_pendint.push_back(*changeit);
931 result += trailing_zeros_G(convert_pending_integrals_G(new_pendint),
932 new_pendint.front(), gsyms)*
933 G_transform(empty, new_a, scale, gsyms);
935 result -= G_transform(new_pendint, new_a, scale, gsyms);
937 // smallest at the front
938 new_pendint.push_back(scale);
939 result += trailing_zeros_G(convert_pending_integrals_G(new_pendint),
940 new_pendint.front(), gsyms)*
941 G_transform(empty, new_a, scale, gsyms);
942 new_pendint.back() = *changeit;
943 result -= trailing_zeros_G(convert_pending_integrals_G(new_pendint),
944 new_pendint.front(), gsyms)*
945 G_transform(empty, new_a, scale, gsyms);
947 result += G_transform(new_pendint, new_a, scale, gsyms);
953 // shuffles the two parameter list a1 and a2 and calls G_transform for every term except
954 // for the one that is equal to a_old
955 ex shuffle_G(const Gparameter & a0, const Gparameter & a1, const Gparameter & a2,
956 const Gparameter& pendint, const Gparameter& a_old, int scale,
957 const exvector& gsyms)
959 if (a1.size()==0 && a2.size()==0) {
960 // veto the one configuration we don't want
961 if ( a0 == a_old ) return 0;
963 return G_transform(pendint, a0, scale, gsyms);
969 aa0.insert(aa0.end(),a1.begin(),a1.end());
970 return shuffle_G(aa0, empty, empty, pendint, a_old, scale, gsyms);
976 aa0.insert(aa0.end(),a2.begin(),a2.end());
977 return shuffle_G(aa0, empty, empty, pendint, a_old, scale, gsyms);
980 Gparameter a1_removed(a1.begin()+1,a1.end());
981 Gparameter a2_removed(a2.begin()+1,a2.end());
986 a01.push_back( a1[0] );
987 a02.push_back( a2[0] );
989 return shuffle_G(a01, a1_removed, a2, pendint, a_old, scale, gsyms)
990 + shuffle_G(a02, a1, a2_removed, pendint, a_old, scale, gsyms);
994 // handles the transformations and the numerical evaluation of G
995 // the parameter x, s and y must only contain numerics
996 ex G_numeric(const lst& x, const lst& s, const ex& y)
998 // check for convergence and necessary accelerations
999 bool need_trafo = false;
1000 bool need_hoelder = false;
1002 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1003 if (!(*it).is_zero()) {
1005 if (abs(*it) - y < -pow(10,-Digits+1)) {
1008 if (abs((abs(*it) - y)/y) < 0.01) {
1009 need_hoelder = true;
1013 if (x.op(x.nops()-1).is_zero()) {
1016 if (depth == 1 && x.nops() == 2 && !need_trafo) {
1017 return -Li(x.nops(), y / x.op(x.nops()-1)).evalf();
1020 // do acceleration transformation (hoelder convolution [BBB])
1024 const int size = x.nops();
1026 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1027 newx.append(*it / y);
1030 for (int r=0; r<=size; ++r) {
1031 ex buffer = pow(-1, r);
1036 for (lst::const_iterator it = newx.begin(); it != newx.end(); ++it) {
1047 for (int j=r; j>=1; --j) {
1048 qlstx.append(1-newx.op(j-1));
1049 if (newx.op(j-1).info(info_flags::real) && newx.op(j-1) > 1 && newx.op(j-1) <= 2) {
1050 qlsts.append( s.op(j-1));
1052 qlsts.append( -s.op(j-1));
1055 if (qlstx.nops() > 0) {
1056 buffer *= G_numeric(qlstx, qlsts, 1/q);
1060 for (int j=r+1; j<=size; ++j) {
1061 plstx.append(newx.op(j-1));
1062 plsts.append(s.op(j-1));
1064 if (plstx.nops() > 0) {
1065 buffer *= G_numeric(plstx, plsts, 1/p);
1072 // convergence transformation
1075 // sort (|x|<->position) to determine indices
1076 std::multimap<ex,int> sortmap;
1078 for (int i=0; i<x.nops(); ++i) {
1079 if (!x[i].is_zero()) {
1080 sortmap.insert(std::pair<ex,int>(abs(x[i]), i));
1084 // include upper limit (scale)
1085 sortmap.insert(std::pair<ex,int>(abs(y), x.nops()));
1087 // generate missing dummy-symbols
1089 // holding dummy-symbols for the G/Li transformations
1091 gsyms.push_back(symbol("GSYMS_ERROR"));
1093 for (std::multimap<ex,int>::const_iterator it = sortmap.begin(); it != sortmap.end(); ++it) {
1094 if (it != sortmap.begin()) {
1095 if (it->second < x.nops()) {
1096 if (x[it->second] == lastentry) {
1097 gsyms.push_back(gsyms.back());
1101 if (y == lastentry) {
1102 gsyms.push_back(gsyms.back());
1107 std::ostringstream os;
1109 gsyms.push_back(symbol(os.str()));
1111 if (it->second < x.nops()) {
1112 lastentry = x[it->second];
1118 // fill position data according to sorted indices and prepare substitution list
1119 Gparameter a(x.nops());
1123 for (std::multimap<ex,int>::const_iterator it = sortmap.begin(); it != sortmap.end(); ++it) {
1124 if (it->second < x.nops()) {
1125 if (s[it->second] > 0) {
1126 a[it->second] = pos;
1128 a[it->second] = -pos;
1130 subslst.append(gsyms[pos] == x[it->second]);
1133 subslst.append(gsyms[pos] == y);
1138 // do transformation
1140 ex result = G_transform(pendint, a, scale, gsyms);
1141 // replace dummy symbols with their values
1142 result = result.eval().expand();
1143 result = result.subs(subslst).evalf();
1154 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1155 if ((*it).is_zero()) {
1158 newx.append(factor / (*it));
1166 return sign * numeric(mLi_do_summation(m, newx));
1170 ex mLi_numeric(const lst& m, const lst& x)
1172 // let G_numeric do the transformation
1176 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1177 for (int i = 1; i < *itm; ++i) {
1181 newx.append(factor / *itx);
1185 return pow(-1, m.nops()) * G_numeric(newx, s, _ex1);
1189 } // end of anonymous namespace
1192 //////////////////////////////////////////////////////////////////////
1194 // Generalized multiple polylogarithm G(x, y) and G(x, s, y)
1198 //////////////////////////////////////////////////////////////////////
1201 static ex G2_evalf(const ex& x_, const ex& y)
1203 if (!y.info(info_flags::positive)) {
1204 return G(x_, y).hold();
1206 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1207 if (x.nops() == 0) {
1211 return G(x_, y).hold();
1214 bool all_zero = true;
1215 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1216 if (!(*it).info(info_flags::numeric)) {
1217 return G(x_, y).hold();
1225 return pow(log(y), x.nops()) / factorial(x.nops());
1227 return G_numeric(x, s, y);
1231 static ex G2_eval(const ex& x_, const ex& y)
1233 //TODO eval to MZV or H or S or Lin
1235 if (!y.info(info_flags::positive)) {
1236 return G(x_, y).hold();
1238 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1239 if (x.nops() == 0) {
1243 return G(x_, y).hold();
1246 bool all_zero = true;
1247 bool crational = true;
1248 for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1249 if (!(*it).info(info_flags::numeric)) {
1250 return G(x_, y).hold();
1252 if (!(*it).info(info_flags::crational)) {
1261 return pow(log(y), x.nops()) / factorial(x.nops());
1263 if (!y.info(info_flags::crational)) {
1267 return G(x_, y).hold();
1269 return G_numeric(x, s, y);
1273 unsigned G2_SERIAL::serial = function::register_new(function_options("G", 2).
1274 evalf_func(G2_evalf).
1276 do_not_evalf_params().
1279 // derivative_func(G2_deriv).
1280 // print_func<print_latex>(G2_print_latex).
1283 static ex G3_evalf(const ex& x_, const ex& s_, const ex& y)
1285 if (!y.info(info_flags::positive)) {
1286 return G(x_, s_, y).hold();
1288 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1289 lst s = is_a<lst>(s_) ? ex_to<lst>(s_) : lst(s_);
1290 if (x.nops() != s.nops()) {
1291 return G(x_, s_, y).hold();
1293 if (x.nops() == 0) {
1297 return G(x_, s_, y).hold();
1300 bool all_zero = true;
1301 for (lst::const_iterator itx = x.begin(), its = s.begin(); itx != x.end(); ++itx, ++its) {
1302 if (!(*itx).info(info_flags::numeric)) {
1303 return G(x_, y).hold();
1305 if (!(*its).info(info_flags::real)) {
1306 return G(x_, y).hold();
1318 return pow(log(y), x.nops()) / factorial(x.nops());
1320 return G_numeric(x, sn, y);
1324 static ex G3_eval(const ex& x_, const ex& s_, const ex& y)
1326 //TODO eval to MZV or H or S or Lin
1328 if (!y.info(info_flags::positive)) {
1329 return G(x_, s_, y).hold();
1331 lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1332 lst s = is_a<lst>(s_) ? ex_to<lst>(s_) : lst(s_);
1333 if (x.nops() != s.nops()) {
1334 return G(x_, s_, y).hold();
1336 if (x.nops() == 0) {
1340 return G(x_, s_, y).hold();
1343 bool all_zero = true;
1344 bool crational = true;
1345 for (lst::const_iterator itx = x.begin(), its = s.begin(); itx != x.end(); ++itx, ++its) {
1346 if (!(*itx).info(info_flags::numeric)) {
1347 return G(x_, s_, y).hold();
1349 if (!(*its).info(info_flags::real)) {
1350 return G(x_, s_, y).hold();
1352 if (!(*itx).info(info_flags::crational)) {
1365 return pow(log(y), x.nops()) / factorial(x.nops());
1367 if (!y.info(info_flags::crational)) {
1371 return G(x_, s_, y).hold();
1373 return G_numeric(x, sn, y);
1377 unsigned G3_SERIAL::serial = function::register_new(function_options("G", 3).
1378 evalf_func(G3_evalf).
1380 do_not_evalf_params().
1383 // derivative_func(G3_deriv).
1384 // print_func<print_latex>(G3_print_latex).
1387 //////////////////////////////////////////////////////////////////////
1389 // Classical polylogarithm and multiple polylogarithm Li(m,x)
1393 //////////////////////////////////////////////////////////////////////
1396 static ex Li_evalf(const ex& m_, const ex& x_)
1398 // classical polylogs
1399 if (m_.info(info_flags::posint)) {
1400 if (x_.info(info_flags::numeric)) {
1401 int m__ = ex_to<numeric>(m_).to_int();
1402 const cln::cl_N x__ = ex_to<numeric>(x_).to_cl_N();
1403 const cln::cl_N result = Lin_numeric(m__, x__);
1404 return numeric(result);
1406 // try to numerically evaluate second argument
1407 ex x_val = x_.evalf();
1408 if (x_val.info(info_flags::numeric)) {
1409 int m__ = ex_to<numeric>(m_).to_int();
1410 const cln::cl_N x__ = ex_to<numeric>(x_val).to_cl_N();
1411 const cln::cl_N result = Lin_numeric(m__, x__);
1412 return numeric(result);
1416 // multiple polylogs
1417 if (is_a<lst>(m_) && is_a<lst>(x_)) {
1419 const lst& m = ex_to<lst>(m_);
1420 const lst& x = ex_to<lst>(x_);
1421 if (m.nops() != x.nops()) {
1422 return Li(m_,x_).hold();
1424 if (x.nops() == 0) {
1427 if ((m.op(0) == _ex1) && (x.op(0) == _ex1)) {
1428 return Li(m_,x_).hold();
1431 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1432 if (!(*itm).info(info_flags::posint)) {
1433 return Li(m_, x_).hold();
1435 if (!(*itx).info(info_flags::numeric)) {
1436 return Li(m_, x_).hold();
1443 return mLi_numeric(m, x);
1446 return Li(m_,x_).hold();
1450 static ex Li_eval(const ex& m_, const ex& x_)
1452 if (is_a<lst>(m_)) {
1453 if (is_a<lst>(x_)) {
1454 // multiple polylogs
1455 const lst& m = ex_to<lst>(m_);
1456 const lst& x = ex_to<lst>(x_);
1457 if (m.nops() != x.nops()) {
1458 return Li(m_,x_).hold();
1460 if (x.nops() == 0) {
1464 bool is_zeta = true;
1465 bool do_evalf = true;
1466 bool crational = true;
1467 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1468 if (!(*itm).info(info_flags::posint)) {
1469 return Li(m_,x_).hold();
1471 if ((*itx != _ex1) && (*itx != _ex_1)) {
1472 if (itx != x.begin()) {
1480 if (!(*itx).info(info_flags::numeric)) {
1483 if (!(*itx).info(info_flags::crational)) {
1492 lst newm = convert_parameter_Li_to_H(m, x, prefactor);
1493 return prefactor * H(newm, x[0]);
1495 if (do_evalf && !crational) {
1496 return mLi_numeric(m,x);
1499 return Li(m_, x_).hold();
1500 } else if (is_a<lst>(x_)) {
1501 return Li(m_, x_).hold();
1504 // classical polylogs
1512 return (pow(2,1-m_)-1) * zeta(m_);
1518 if (x_.is_equal(I)) {
1519 return power(Pi,_ex2)/_ex_48 + Catalan*I;
1521 if (x_.is_equal(-I)) {
1522 return power(Pi,_ex2)/_ex_48 - Catalan*I;
1525 if (m_.info(info_flags::posint) && x_.info(info_flags::numeric) && !x_.info(info_flags::crational)) {
1526 int m__ = ex_to<numeric>(m_).to_int();
1527 const cln::cl_N x__ = ex_to<numeric>(x_).to_cl_N();
1528 const cln::cl_N result = Lin_numeric(m__, x__);
1529 return numeric(result);
1532 return Li(m_, x_).hold();
1536 static ex Li_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
1538 if (is_a<lst>(m) || is_a<lst>(x)) {
1541 seq.push_back(expair(Li(m, x), 0));
1542 return pseries(rel, seq);
1545 // classical polylog
1546 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1547 if (m.info(info_flags::numeric) && x_pt.info(info_flags::numeric)) {
1548 // First special case: x==0 (derivatives have poles)
1549 if (x_pt.is_zero()) {
1552 // manually construct the primitive expansion
1553 for (int i=1; i<order; ++i)
1554 ser += pow(s,i) / pow(numeric(i), m);
1555 // substitute the argument's series expansion
1556 ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
1557 // maybe that was terminating, so add a proper order term
1559 nseq.push_back(expair(Order(_ex1), order));
1560 ser += pseries(rel, nseq);
1561 // reexpanding it will collapse the series again
1562 return ser.series(rel, order);
1564 // TODO special cases: x==1 (branch point) and x real, >=1 (branch cut)
1565 throw std::runtime_error("Li_series: don't know how to do the series expansion at this point!");
1567 // all other cases should be safe, by now:
1568 throw do_taylor(); // caught by function::series()
1572 static ex Li_deriv(const ex& m_, const ex& x_, unsigned deriv_param)
1574 GINAC_ASSERT(deriv_param < 2);
1575 if (deriv_param == 0) {
1578 if (m_.nops() > 1) {
1579 throw std::runtime_error("don't know how to derivate multiple polylogarithm!");
1582 if (is_a<lst>(m_)) {
1588 if (is_a<lst>(x_)) {
1594 return Li(m-1, x) / x;
1601 static void Li_print_latex(const ex& m_, const ex& x_, const print_context& c)
1604 if (is_a<lst>(m_)) {
1610 if (is_a<lst>(x_)) {
1615 c.s << "\\mbox{Li}_{";
1616 lst::const_iterator itm = m.begin();
1619 for (; itm != m.end(); itm++) {
1624 lst::const_iterator itx = x.begin();
1627 for (; itx != x.end(); itx++) {
1635 REGISTER_FUNCTION(Li,
1636 evalf_func(Li_evalf).
1638 series_func(Li_series).
1639 derivative_func(Li_deriv).
1640 print_func<print_latex>(Li_print_latex).
1641 do_not_evalf_params());
1644 //////////////////////////////////////////////////////////////////////
1646 // Nielsen's generalized polylogarithm S(n,p,x)
1650 //////////////////////////////////////////////////////////////////////
1653 // anonymous namespace for helper functions
1657 // lookup table for special Euler-Zagier-Sums (used for S_n,p(x))
1659 std::vector<std::vector<cln::cl_N> > Yn;
1660 int ynsize = 0; // number of Yn[]
1661 int ynlength = 100; // initial length of all Yn[i]
1664 // This function calculates the Y_n. The Y_n are needed for the evaluation of S_{n,p}(x).
1665 // The Y_n are basically Euler-Zagier sums with all m_i=1. They are subsums in the Z-sum
1666 // representing S_{n,p}(x).
1667 // The first index in Y_n corresponds to the parameter p minus one, i.e. the depth of the
1668 // equivalent Z-sum.
1669 // The second index in Y_n corresponds to the running index of the outermost sum in the full Z-sum
1670 // representing S_{n,p}(x).
1671 // The calculation of Y_n uses the values from Y_{n-1}.
1672 void fill_Yn(int n, const cln::float_format_t& prec)
1674 const int initsize = ynlength;
1675 //const int initsize = initsize_Yn;
1676 cln::cl_N one = cln::cl_float(1, prec);
1679 std::vector<cln::cl_N> buf(initsize);
1680 std::vector<cln::cl_N>::iterator it = buf.begin();
1681 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
1682 *it = (*itprev) / cln::cl_N(n+1) * one;
1685 // sums with an index smaller than the depth are zero and need not to be calculated.
1686 // calculation starts with depth, which is n+2)
1687 for (int i=n+2; i<=initsize+n; i++) {
1688 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
1694 std::vector<cln::cl_N> buf(initsize);
1695 std::vector<cln::cl_N>::iterator it = buf.begin();
1698 for (int i=2; i<=initsize; i++) {
1699 *it = *(it-1) + 1 / cln::cl_N(i) * one;
1708 // make Yn longer ...
1709 void make_Yn_longer(int newsize, const cln::float_format_t& prec)
1712 cln::cl_N one = cln::cl_float(1, prec);
1714 Yn[0].resize(newsize);
1715 std::vector<cln::cl_N>::iterator it = Yn[0].begin();
1717 for (int i=ynlength+1; i<=newsize; i++) {
1718 *it = *(it-1) + 1 / cln::cl_N(i) * one;
1722 for (int n=1; n<ynsize; n++) {
1723 Yn[n].resize(newsize);
1724 std::vector<cln::cl_N>::iterator it = Yn[n].begin();
1725 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
1728 for (int i=ynlength+n+1; i<=newsize+n; i++) {
1729 *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
1739 // helper function for S(n,p,x)
1741 cln::cl_N C(int n, int p)
1745 for (int k=0; k<p; k++) {
1746 for (int j=0; j<=(n+k-1)/2; j++) {
1750 result = result - 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1) / cln::factorial(2*j);
1753 result = result + 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1) / cln::factorial(2*j);
1760 result = result + cln::factorial(n+k-1)
1761 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1762 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1765 result = result - cln::factorial(n+k-1)
1766 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1767 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1772 result = result - cln::factorial(n+k-1) * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1773 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1776 result = result + cln::factorial(n+k-1)
1777 * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1778 / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1786 if (((np)/2+n) & 1) {
1787 result = -result - cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
1790 result = -result + cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
1798 // helper function for S(n,p,x)
1799 // [Kol] remark to (9.1)
1800 cln::cl_N a_k(int k)
1809 for (int m=2; m<=k; m++) {
1810 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * a_k(k-m);
1817 // helper function for S(n,p,x)
1818 // [Kol] remark to (9.1)
1819 cln::cl_N b_k(int k)
1828 for (int m=2; m<=k; m++) {
1829 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * b_k(k-m);
1836 // helper function for S(n,p,x)
1837 cln::cl_N S_do_sum(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
1839 static cln::float_format_t oldprec = cln::default_float_format;
1842 return Li_projection(n+1, x, prec);
1845 // precision has changed, we need to clear lookup table Yn
1846 if ( oldprec != prec ) {
1853 // check if precalculated values are sufficient
1855 for (int i=ynsize; i<p-1; i++) {
1860 // should be done otherwise
1861 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
1862 cln::cl_N xf = x * one;
1863 //cln::cl_N xf = x * cln::cl_float(1, prec);
1867 cln::cl_N factor = cln::expt(xf, p);
1871 if (i-p >= ynlength) {
1873 make_Yn_longer(ynlength*2, prec);
1875 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? ...
1876 //res = res + factor / cln::expt(cln::cl_I(i),n+1) * (*it); // should we check it? or rely on magic number? ...
1877 factor = factor * xf;
1879 } while (res != resbuf);
1885 // helper function for S(n,p,x)
1886 cln::cl_N S_projection(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
1889 if (cln::abs(cln::realpart(x)) > cln::cl_F("0.5")) {
1891 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(x),n)
1892 * cln::expt(cln::log(1-x),p) / cln::factorial(n) / cln::factorial(p);
1894 for (int s=0; s<n; s++) {
1896 for (int r=0; r<p; r++) {
1897 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-x),r)
1898 * S_do_sum(p-r,n-s,1-x,prec) / cln::factorial(r);
1900 result = result + cln::expt(cln::log(x),s) * (S_num(n-s,p,1) - res2) / cln::factorial(s);
1906 return S_do_sum(n, p, x, prec);
1910 // helper function for S(n,p,x)
1911 const cln::cl_N S_num(int n, int p, const cln::cl_N& x)
1915 // [Kol] (2.22) with (2.21)
1916 return cln::zeta(p+1);
1921 return cln::zeta(n+1);
1926 for (int nu=0; nu<n; nu++) {
1927 for (int rho=0; rho<=p; rho++) {
1928 result = result + b_k(n-nu-1) * b_k(p-rho) * a_k(nu+rho+1)
1929 * cln::factorial(nu+rho+1) / cln::factorial(rho) / cln::factorial(nu+1);
1932 result = result * cln::expt(cln::cl_I(-1),n+p-1);
1939 return -(1-cln::expt(cln::cl_I(2),-n)) * cln::zeta(n+1);
1941 // throw std::runtime_error("don't know how to evaluate this function!");
1944 // what is the desired float format?
1945 // first guess: default format
1946 cln::float_format_t prec = cln::default_float_format;
1947 const cln::cl_N value = x;
1948 // second guess: the argument's format
1949 if (!instanceof(realpart(value), cln::cl_RA_ring))
1950 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
1951 else if (!instanceof(imagpart(value), cln::cl_RA_ring))
1952 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
1955 if ((cln::realpart(value) < -0.5) || (n == 0) || ((cln::abs(value) <= 1) && (cln::abs(value) > 0.95))) {
1957 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(value),n)
1958 * cln::expt(cln::log(1-value),p) / cln::factorial(n) / cln::factorial(p);
1960 for (int s=0; s<n; s++) {
1962 for (int r=0; r<p; r++) {
1963 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-value),r)
1964 * S_num(p-r,n-s,1-value) / cln::factorial(r);
1966 result = result + cln::expt(cln::log(value),s) * (S_num(n-s,p,1) - res2) / cln::factorial(s);
1973 if (cln::abs(value) > 1) {
1977 for (int s=0; s<p; s++) {
1978 for (int r=0; r<=s; r++) {
1979 result = result + cln::expt(cln::cl_I(-1),s) * cln::expt(cln::log(-value),r) * cln::factorial(n+s-r-1)
1980 / cln::factorial(r) / cln::factorial(s-r) / cln::factorial(n-1)
1981 * S_num(n+s-r,p-s,cln::recip(value));
1984 result = result * cln::expt(cln::cl_I(-1),n);
1987 for (int r=0; r<n; r++) {
1988 res2 = res2 + cln::expt(cln::log(-value),r) * C(n-r,p) / cln::factorial(r);
1990 res2 = res2 + cln::expt(cln::log(-value),n+p) / cln::factorial(n+p);
1992 result = result + cln::expt(cln::cl_I(-1),p) * res2;
1997 return S_projection(n, p, value, prec);
2002 } // end of anonymous namespace
2005 //////////////////////////////////////////////////////////////////////
2007 // Nielsen's generalized polylogarithm S(n,p,x)
2011 //////////////////////////////////////////////////////////////////////
2014 static ex S_evalf(const ex& n, const ex& p, const ex& x)
2016 if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
2017 const int n_ = ex_to<numeric>(n).to_int();
2018 const int p_ = ex_to<numeric>(p).to_int();
2019 if (is_a<numeric>(x)) {
2020 const cln::cl_N x_ = ex_to<numeric>(x).to_cl_N();
2021 const cln::cl_N result = S_num(n_, p_, x_);
2022 return numeric(result);
2024 ex x_val = x.evalf();
2025 if (is_a<numeric>(x_val)) {
2026 const cln::cl_N x_val_ = ex_to<numeric>(x_val).to_cl_N();
2027 const cln::cl_N result = S_num(n_, p_, x_val_);
2028 return numeric(result);
2032 return S(n, p, x).hold();
2036 static ex S_eval(const ex& n, const ex& p, const ex& x)
2038 if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
2044 for (int i=ex_to<numeric>(p).to_int()-1; i>0; i--) {
2052 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
2053 int n_ = ex_to<numeric>(n).to_int();
2054 int p_ = ex_to<numeric>(p).to_int();
2055 const cln::cl_N x_ = ex_to<numeric>(x).to_cl_N();
2056 const cln::cl_N result = S_num(n_, p_, x_);
2057 return numeric(result);
2062 return pow(-log(1-x), p) / factorial(p);
2064 return S(n, p, x).hold();
2068 static ex S_series(const ex& n, const ex& p, const ex& x, const relational& rel, int order, unsigned options)
2071 return Li(n+1, x).series(rel, order, options);
2074 const ex x_pt = x.subs(rel, subs_options::no_pattern);
2075 if (n.info(info_flags::posint) && p.info(info_flags::posint) && x_pt.info(info_flags::numeric)) {
2076 // First special case: x==0 (derivatives have poles)
2077 if (x_pt.is_zero()) {
2080 // manually construct the primitive expansion
2081 // subsum = Euler-Zagier-Sum is needed
2082 // dirty hack (slow ...) calculation of subsum:
2083 std::vector<ex> presubsum, subsum;
2084 subsum.push_back(0);
2085 for (int i=1; i<order-1; ++i) {
2086 subsum.push_back(subsum[i-1] + numeric(1, i));
2088 for (int depth=2; depth<p; ++depth) {
2090 for (int i=1; i<order-1; ++i) {
2091 subsum[i] = subsum[i-1] + numeric(1, i) * presubsum[i-1];
2095 for (int i=1; i<order; ++i) {
2096 ser += pow(s,i) / pow(numeric(i), n+1) * subsum[i-1];
2098 // substitute the argument's series expansion
2099 ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
2100 // maybe that was terminating, so add a proper order term
2102 nseq.push_back(expair(Order(_ex1), order));
2103 ser += pseries(rel, nseq);
2104 // reexpanding it will collapse the series again
2105 return ser.series(rel, order);
2107 // TODO special cases: x==1 (branch point) and x real, >=1 (branch cut)
2108 throw std::runtime_error("S_series: don't know how to do the series expansion at this point!");
2110 // all other cases should be safe, by now:
2111 throw do_taylor(); // caught by function::series()
2115 static ex S_deriv(const ex& n, const ex& p, const ex& x, unsigned deriv_param)
2117 GINAC_ASSERT(deriv_param < 3);
2118 if (deriv_param < 2) {
2122 return S(n-1, p, x) / x;
2124 return S(n, p-1, x) / (1-x);
2129 static void S_print_latex(const ex& n, const ex& p, const ex& x, const print_context& c)
2131 c.s << "\\mbox{S}_{";
2141 REGISTER_FUNCTION(S,
2142 evalf_func(S_evalf).
2144 series_func(S_series).
2145 derivative_func(S_deriv).
2146 print_func<print_latex>(S_print_latex).
2147 do_not_evalf_params());
2150 //////////////////////////////////////////////////////////////////////
2152 // Harmonic polylogarithm H(m,x)
2156 //////////////////////////////////////////////////////////////////////
2159 // anonymous namespace for helper functions
2163 // regulates the pole (used by 1/x-transformation)
2164 symbol H_polesign("IMSIGN");
2167 // convert parameters from H to Li representation
2168 // parameters are expected to be in expanded form, i.e. only 0, 1 and -1
2169 // returns true if some parameters are negative
2170 bool convert_parameter_H_to_Li(const lst& l, lst& m, lst& s, ex& pf)
2172 // expand parameter list
2174 for (lst::const_iterator it = l.begin(); it != l.end(); it++) {
2176 for (ex count=*it-1; count > 0; count--) {
2180 } else if (*it < -1) {
2181 for (ex count=*it+1; count < 0; count++) {
2192 bool has_negative_parameters = false;
2194 for (lst::const_iterator it = mexp.begin(); it != mexp.end(); it++) {
2200 m.append((*it+acc-1) * signum);
2202 m.append((*it-acc+1) * signum);
2208 has_negative_parameters = true;
2211 if (has_negative_parameters) {
2212 for (int i=0; i<m.nops(); i++) {
2214 m.let_op(i) = -m.op(i);
2222 return has_negative_parameters;
2226 // recursivly transforms H to corresponding multiple polylogarithms
2227 struct map_trafo_H_convert_to_Li : public map_function
2229 ex operator()(const ex& e)
2231 if (is_a<add>(e) || is_a<mul>(e)) {
2232 return e.map(*this);
2234 if (is_a<function>(e)) {
2235 std::string name = ex_to<function>(e).get_name();
2238 if (is_a<lst>(e.op(0))) {
2239 parameter = ex_to<lst>(e.op(0));
2241 parameter = lst(e.op(0));
2248 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
2249 s.let_op(0) = s.op(0) * arg;
2250 return pf * Li(m, s).hold();
2252 for (int i=0; i<m.nops(); i++) {
2255 s.let_op(0) = s.op(0) * arg;
2256 return Li(m, s).hold();
2265 // recursivly transforms H to corresponding zetas
2266 struct map_trafo_H_convert_to_zeta : public map_function
2268 ex operator()(const ex& e)
2270 if (is_a<add>(e) || is_a<mul>(e)) {
2271 return e.map(*this);
2273 if (is_a<function>(e)) {
2274 std::string name = ex_to<function>(e).get_name();
2277 if (is_a<lst>(e.op(0))) {
2278 parameter = ex_to<lst>(e.op(0));
2280 parameter = lst(e.op(0));
2286 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
2287 return pf * zeta(m, s);
2298 // remove trailing zeros from H-parameters
2299 struct map_trafo_H_reduce_trailing_zeros : 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));
2316 if (parameter.op(parameter.nops()-1) == 0) {
2319 if (parameter.nops() == 1) {
2324 lst::const_iterator it = parameter.begin();
2325 while ((it != parameter.end()) && (*it == 0)) {
2328 if (it == parameter.end()) {
2329 return pow(log(arg),parameter.nops()) / factorial(parameter.nops());
2333 parameter.remove_last();
2334 int lastentry = parameter.nops();
2335 while ((lastentry > 0) && (parameter[lastentry-1] == 0)) {
2340 ex result = log(arg) * H(parameter,arg).hold();
2342 for (ex i=0; i<lastentry; i++) {
2343 if (parameter[i] > 0) {
2345 result -= (acc + parameter[i]-1) * H(parameter, arg).hold();
2348 } else if (parameter[i] < 0) {
2350 result -= (acc + abs(parameter[i]+1)) * H(parameter, arg).hold();
2358 if (lastentry < parameter.nops()) {
2359 result = result / (parameter.nops()-lastentry+1);
2360 return result.map(*this);
2372 // returns an expression with zeta functions corresponding to the parameter list for H
2373 ex convert_H_to_zeta(const lst& m)
2375 symbol xtemp("xtemp");
2376 map_trafo_H_reduce_trailing_zeros filter;
2377 map_trafo_H_convert_to_zeta filter2;
2378 return filter2(filter(H(m, xtemp).hold())).subs(xtemp == 1);
2382 // convert signs form Li to H representation
2383 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf)
2386 lst::const_iterator itm = m.begin();
2387 lst::const_iterator itx = ++x.begin();
2392 while (itx != x.end()) {
2393 signum *= (*itx > 0) ? 1 : -1;
2395 res.append((*itm) * signum);
2403 // multiplies an one-dimensional H with another H
2405 ex trafo_H_mult(const ex& h1, const ex& h2)
2410 ex h1nops = h1.op(0).nops();
2411 ex h2nops = h2.op(0).nops();
2413 hshort = h2.op(0).op(0);
2414 hlong = ex_to<lst>(h1.op(0));
2416 hshort = h1.op(0).op(0);
2418 hlong = ex_to<lst>(h2.op(0));
2420 hlong = h2.op(0).op(0);
2423 for (int i=0; i<=hlong.nops(); i++) {
2427 newparameter.append(hlong[j]);
2429 newparameter.append(hshort);
2430 for (; j<hlong.nops(); j++) {
2431 newparameter.append(hlong[j]);
2433 res += H(newparameter, h1.op(1)).hold();
2439 // applies trafo_H_mult recursively on expressions
2440 struct map_trafo_H_mult : public map_function
2442 ex operator()(const ex& e)
2445 return e.map(*this);
2453 for (int pos=0; pos<e.nops(); pos++) {
2454 if (is_a<power>(e.op(pos)) && is_a<function>(e.op(pos).op(0))) {
2455 std::string name = ex_to<function>(e.op(pos).op(0)).get_name();
2457 for (ex i=0; i<e.op(pos).op(1); i++) {
2458 Hlst.append(e.op(pos).op(0));
2462 } else if (is_a<function>(e.op(pos))) {
2463 std::string name = ex_to<function>(e.op(pos)).get_name();
2465 if (e.op(pos).op(0).nops() > 1) {
2468 Hlst.append(e.op(pos));
2473 result *= e.op(pos);
2476 if (Hlst.nops() > 0) {
2477 firstH = Hlst[Hlst.nops()-1];
2484 if (Hlst.nops() > 0) {
2485 ex buffer = trafo_H_mult(firstH, Hlst.op(0));
2487 for (int i=1; i<Hlst.nops(); i++) {
2488 result *= Hlst.op(i);
2490 result = result.expand();
2491 map_trafo_H_mult recursion;
2492 return recursion(result);
2503 // do integration [ReV] (55)
2504 // put parameter 0 in front of existing parameters
2505 ex trafo_H_1tx_prepend_zero(const ex& e, const ex& arg)
2509 if (is_a<function>(e)) {
2510 name = ex_to<function>(e).get_name();
2515 for (int i=0; i<e.nops(); i++) {
2516 if (is_a<function>(e.op(i))) {
2517 std::string name = ex_to<function>(e.op(i)).get_name();
2525 lst newparameter = ex_to<lst>(h.op(0));
2526 newparameter.prepend(0);
2527 ex addzeta = convert_H_to_zeta(newparameter);
2528 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
2530 return e * (-H(lst(0),1/arg).hold());
2535 // do integration [ReV] (49)
2536 // put parameter 1 in front of existing parameters
2537 ex trafo_H_prepend_one(const ex& e, const ex& arg)
2541 if (is_a<function>(e)) {
2542 name = ex_to<function>(e).get_name();
2547 for (int i=0; i<e.nops(); i++) {
2548 if (is_a<function>(e.op(i))) {
2549 std::string name = ex_to<function>(e.op(i)).get_name();
2557 lst newparameter = ex_to<lst>(h.op(0));
2558 newparameter.prepend(1);
2559 return e.subs(h == H(newparameter, h.op(1)).hold());
2561 return e * H(lst(1),1-arg).hold();
2566 // do integration [ReV] (55)
2567 // put parameter -1 in front of existing parameters
2568 ex trafo_H_1tx_prepend_minusone(const ex& e, const ex& arg)
2572 if (is_a<function>(e)) {
2573 name = ex_to<function>(e).get_name();
2578 for (int i=0; i<e.nops(); i++) {
2579 if (is_a<function>(e.op(i))) {
2580 std::string name = ex_to<function>(e.op(i)).get_name();
2588 lst newparameter = ex_to<lst>(h.op(0));
2589 newparameter.prepend(-1);
2590 ex addzeta = convert_H_to_zeta(newparameter);
2591 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
2593 ex addzeta = convert_H_to_zeta(lst(-1));
2594 return (e * (addzeta - H(lst(-1),1/arg).hold())).expand();
2599 // do integration [ReV] (55)
2600 // put parameter -1 in front of existing parameters
2601 ex trafo_H_1mxt1px_prepend_minusone(const ex& e, const ex& arg)
2605 if (is_a<function>(e)) {
2606 name = ex_to<function>(e).get_name();
2611 for (int i=0; i<e.nops(); i++) {
2612 if (is_a<function>(e.op(i))) {
2613 std::string name = ex_to<function>(e.op(i)).get_name();
2621 lst newparameter = ex_to<lst>(h.op(0));
2622 newparameter.prepend(-1);
2623 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
2625 return (e * H(lst(-1),(1-arg)/(1+arg)).hold()).expand();
2630 // do integration [ReV] (55)
2631 // put parameter 1 in front of existing parameters
2632 ex trafo_H_1mxt1px_prepend_one(const ex& e, const ex& arg)
2636 if (is_a<function>(e)) {
2637 name = ex_to<function>(e).get_name();
2642 for (int i=0; i<e.nops(); i++) {
2643 if (is_a<function>(e.op(i))) {
2644 std::string name = ex_to<function>(e.op(i)).get_name();
2652 lst newparameter = ex_to<lst>(h.op(0));
2653 newparameter.prepend(1);
2654 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
2656 return (e * H(lst(1),(1-arg)/(1+arg)).hold()).expand();
2661 // do x -> 1-x transformation
2662 struct map_trafo_H_1mx : public map_function
2664 ex operator()(const ex& e)
2666 if (is_a<add>(e) || is_a<mul>(e)) {
2667 return e.map(*this);
2670 if (is_a<function>(e)) {
2671 std::string name = ex_to<function>(e).get_name();
2674 lst parameter = ex_to<lst>(e.op(0));
2677 // special cases if all parameters are either 0, 1 or -1
2678 bool allthesame = true;
2679 if (parameter.op(0) == 0) {
2680 for (int i=1; i<parameter.nops(); i++) {
2681 if (parameter.op(i) != 0) {
2688 for (int i=parameter.nops(); i>0; i--) {
2689 newparameter.append(1);
2691 return pow(-1, parameter.nops()) * H(newparameter, 1-arg).hold();
2693 } else if (parameter.op(0) == -1) {
2694 throw std::runtime_error("map_trafo_H_1mx: cannot handle weights equal -1!");
2696 for (int i=1; i<parameter.nops(); i++) {
2697 if (parameter.op(i) != 1) {
2704 for (int i=parameter.nops(); i>0; i--) {
2705 newparameter.append(0);
2707 return pow(-1, parameter.nops()) * H(newparameter, 1-arg).hold();
2711 lst newparameter = parameter;
2712 newparameter.remove_first();
2714 if (parameter.op(0) == 0) {
2717 ex res = convert_H_to_zeta(parameter);
2718 //ex res = convert_from_RV(parameter, 1).subs(H(wild(1),wild(2))==zeta(wild(1)));
2719 map_trafo_H_1mx recursion;
2720 ex buffer = recursion(H(newparameter, arg).hold());
2721 if (is_a<add>(buffer)) {
2722 for (int i=0; i<buffer.nops(); i++) {
2723 res -= trafo_H_prepend_one(buffer.op(i), arg);
2726 res -= trafo_H_prepend_one(buffer, arg);
2733 map_trafo_H_1mx recursion;
2734 map_trafo_H_mult unify;
2735 ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
2737 while (parameter.op(firstzero) == 1) {
2740 for (int i=firstzero-1; i<parameter.nops()-1; i++) {
2744 newparameter.append(parameter[j+1]);
2746 newparameter.append(1);
2747 for (; j<parameter.nops()-1; j++) {
2748 newparameter.append(parameter[j+1]);
2750 res -= H(newparameter, arg).hold();
2752 res = recursion(res).expand() / firstzero;
2762 // do x -> 1/x transformation
2763 struct map_trafo_H_1overx : public map_function
2765 ex operator()(const ex& e)
2767 if (is_a<add>(e) || is_a<mul>(e)) {
2768 return e.map(*this);
2771 if (is_a<function>(e)) {
2772 std::string name = ex_to<function>(e).get_name();
2775 lst parameter = ex_to<lst>(e.op(0));
2778 // special cases if all parameters are either 0, 1 or -1
2779 bool allthesame = true;
2780 if (parameter.op(0) == 0) {
2781 for (int i=1; i<parameter.nops(); i++) {
2782 if (parameter.op(i) != 0) {
2788 return pow(-1, parameter.nops()) * H(parameter, 1/arg).hold();
2790 } else if (parameter.op(0) == -1) {
2791 for (int i=1; i<parameter.nops(); i++) {
2792 if (parameter.op(i) != -1) {
2798 map_trafo_H_mult unify;
2799 return unify((pow(H(lst(-1),1/arg).hold() - H(lst(0),1/arg).hold(), parameter.nops())
2800 / factorial(parameter.nops())).expand());
2803 for (int i=1; i<parameter.nops(); i++) {
2804 if (parameter.op(i) != 1) {
2810 map_trafo_H_mult unify;
2811 return unify((pow(H(lst(1),1/arg).hold() + H(lst(0),1/arg).hold() + H_polesign, parameter.nops())
2812 / factorial(parameter.nops())).expand());
2816 lst newparameter = parameter;
2817 newparameter.remove_first();
2819 if (parameter.op(0) == 0) {
2822 ex res = convert_H_to_zeta(parameter);
2823 map_trafo_H_1overx recursion;
2824 ex buffer = recursion(H(newparameter, arg).hold());
2825 if (is_a<add>(buffer)) {
2826 for (int i=0; i<buffer.nops(); i++) {
2827 res += trafo_H_1tx_prepend_zero(buffer.op(i), arg);
2830 res += trafo_H_1tx_prepend_zero(buffer, arg);
2834 } else if (parameter.op(0) == -1) {
2836 // leading negative one
2837 ex res = convert_H_to_zeta(parameter);
2838 map_trafo_H_1overx recursion;
2839 ex buffer = recursion(H(newparameter, arg).hold());
2840 if (is_a<add>(buffer)) {
2841 for (int i=0; i<buffer.nops(); i++) {
2842 res += trafo_H_1tx_prepend_zero(buffer.op(i), arg) - trafo_H_1tx_prepend_minusone(buffer.op(i), arg);
2845 res += trafo_H_1tx_prepend_zero(buffer, arg) - trafo_H_1tx_prepend_minusone(buffer, arg);
2852 map_trafo_H_1overx recursion;
2853 map_trafo_H_mult unify;
2854 ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
2856 while (parameter.op(firstzero) == 1) {
2859 for (int i=firstzero-1; i<parameter.nops()-1; i++) {
2863 newparameter.append(parameter[j+1]);
2865 newparameter.append(1);
2866 for (; j<parameter.nops()-1; j++) {
2867 newparameter.append(parameter[j+1]);
2869 res -= H(newparameter, arg).hold();
2871 res = recursion(res).expand() / firstzero;
2883 // do x -> (1-x)/(1+x) transformation
2884 struct map_trafo_H_1mxt1px : public map_function
2886 ex operator()(const ex& e)
2888 if (is_a<add>(e) || is_a<mul>(e)) {
2889 return e.map(*this);
2892 if (is_a<function>(e)) {
2893 std::string name = ex_to<function>(e).get_name();
2896 lst parameter = ex_to<lst>(e.op(0));
2899 // special cases if all parameters are either 0, 1 or -1
2900 bool allthesame = true;
2901 if (parameter.op(0) == 0) {
2902 for (int i=1; i<parameter.nops(); i++) {
2903 if (parameter.op(i) != 0) {
2909 map_trafo_H_mult unify;
2910 return unify((pow(-H(lst(1),(1-arg)/(1+arg)).hold() - H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
2911 / factorial(parameter.nops())).expand());
2913 } else if (parameter.op(0) == -1) {
2914 for (int i=1; i<parameter.nops(); i++) {
2915 if (parameter.op(i) != -1) {
2921 map_trafo_H_mult unify;
2922 return unify((pow(log(2) - H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
2923 / factorial(parameter.nops())).expand());
2926 for (int i=1; i<parameter.nops(); i++) {
2927 if (parameter.op(i) != 1) {
2933 map_trafo_H_mult unify;
2934 return unify((pow(-log(2) - H(lst(0),(1-arg)/(1+arg)).hold() + H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
2935 / factorial(parameter.nops())).expand());
2939 lst newparameter = parameter;
2940 newparameter.remove_first();
2942 if (parameter.op(0) == 0) {
2945 ex res = convert_H_to_zeta(parameter);
2946 map_trafo_H_1mxt1px recursion;
2947 ex buffer = recursion(H(newparameter, arg).hold());
2948 if (is_a<add>(buffer)) {
2949 for (int i=0; i<buffer.nops(); i++) {
2950 res -= trafo_H_1mxt1px_prepend_one(buffer.op(i), arg) + trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
2953 res -= trafo_H_1mxt1px_prepend_one(buffer, arg) + trafo_H_1mxt1px_prepend_minusone(buffer, arg);
2957 } else if (parameter.op(0) == -1) {
2959 // leading negative one
2960 ex res = convert_H_to_zeta(parameter);
2961 map_trafo_H_1mxt1px recursion;
2962 ex buffer = recursion(H(newparameter, arg).hold());
2963 if (is_a<add>(buffer)) {
2964 for (int i=0; i<buffer.nops(); i++) {
2965 res -= trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
2968 res -= trafo_H_1mxt1px_prepend_minusone(buffer, arg);
2975 map_trafo_H_1mxt1px recursion;
2976 map_trafo_H_mult unify;
2977 ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
2979 while (parameter.op(firstzero) == 1) {
2982 for (int i=firstzero-1; i<parameter.nops()-1; i++) {
2986 newparameter.append(parameter[j+1]);
2988 newparameter.append(1);
2989 for (; j<parameter.nops()-1; j++) {
2990 newparameter.append(parameter[j+1]);
2992 res -= H(newparameter, arg).hold();
2994 res = recursion(res).expand() / firstzero;
3006 // do the actual summation.
3007 cln::cl_N H_do_sum(const std::vector<int>& m, const cln::cl_N& x)
3009 const int j = m.size();
3011 std::vector<cln::cl_N> t(j);
3013 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3014 cln::cl_N factor = cln::expt(x, j) * one;
3020 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),m[j-1]);
3021 for (int k=j-2; k>=1; k--) {
3022 t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), m[k]);
3024 t[0] = t[0] + t[1] * factor / cln::expt(cln::cl_I(q+j-1), m[0]);
3025 factor = factor * x;
3026 } while (t[0] != t0buf);
3032 } // end of anonymous namespace
3035 //////////////////////////////////////////////////////////////////////
3037 // Harmonic polylogarithm H(m,x)
3041 //////////////////////////////////////////////////////////////////////
3044 static ex H_evalf(const ex& x1, const ex& x2)
3046 if (is_a<lst>(x1)) {
3049 if (is_a<numeric>(x2)) {
3050 x = ex_to<numeric>(x2).to_cl_N();
3052 ex x2_val = x2.evalf();
3053 if (is_a<numeric>(x2_val)) {
3054 x = ex_to<numeric>(x2_val).to_cl_N();
3058 for (int i=0; i<x1.nops(); i++) {
3059 if (!x1.op(i).info(info_flags::integer)) {
3060 return H(x1, x2).hold();
3063 if (x1.nops() < 1) {
3064 return H(x1, x2).hold();
3067 const lst& morg = ex_to<lst>(x1);
3068 // remove trailing zeros ...
3069 if (*(--morg.end()) == 0) {
3070 symbol xtemp("xtemp");
3071 map_trafo_H_reduce_trailing_zeros filter;
3072 return filter(H(x1, xtemp).hold()).subs(xtemp==x2).evalf();
3074 // ... and expand parameter notation
3075 bool has_minus_one = false;
3077 for (lst::const_iterator it = morg.begin(); it != morg.end(); it++) {
3079 for (ex count=*it-1; count > 0; count--) {
3083 } else if (*it <= -1) {
3084 for (ex count=*it+1; count < 0; count++) {
3088 has_minus_one = true;
3095 if (cln::abs(x) < 0.95) {
3099 if (convert_parameter_H_to_Li(m, m_lst, s_lst, pf)) {
3100 // negative parameters -> s_lst is filled
3101 std::vector<int> m_int;
3102 std::vector<cln::cl_N> x_cln;
3103 for (lst::const_iterator it_int = m_lst.begin(), it_cln = s_lst.begin();
3104 it_int != m_lst.end(); it_int++, it_cln++) {
3105 m_int.push_back(ex_to<numeric>(*it_int).to_int());
3106 x_cln.push_back(ex_to<numeric>(*it_cln).to_cl_N());
3108 x_cln.front() = x_cln.front() * x;
3109 return pf * numeric(multipleLi_do_sum(m_int, x_cln));
3111 // only positive parameters
3113 if (m_lst.nops() == 1) {
3114 return Li(m_lst.op(0), x2).evalf();
3116 std::vector<int> m_int;
3117 for (lst::const_iterator it = m_lst.begin(); it != m_lst.end(); it++) {
3118 m_int.push_back(ex_to<numeric>(*it).to_int());
3120 return numeric(H_do_sum(m_int, x));
3124 symbol xtemp("xtemp");
3127 // ensure that the realpart of the argument is positive
3128 if (cln::realpart(x) < 0) {
3130 for (int i=0; i<m.nops(); i++) {
3132 m.let_op(i) = -m.op(i);
3139 if (cln::abs(x) >= 2.0) {
3140 map_trafo_H_1overx trafo;
3141 res *= trafo(H(m, xtemp));
3142 if (cln::imagpart(x) <= 0) {
3143 res = res.subs(H_polesign == -I*Pi);
3145 res = res.subs(H_polesign == I*Pi);
3147 return res.subs(xtemp == numeric(x)).evalf();
3150 // check transformations for 0.95 <= |x| < 2.0
3152 // |(1-x)/(1+x)| < 0.9 -> circular area with center=9.53+0i and radius=9.47
3153 if (cln::abs(x-9.53) <= 9.47) {
3155 map_trafo_H_1mxt1px trafo;
3156 res *= trafo(H(m, xtemp));
3159 if (has_minus_one) {
3160 map_trafo_H_convert_to_Li filter;
3161 return filter(H(m, numeric(x)).hold()).evalf();
3163 map_trafo_H_1mx trafo;
3164 res *= trafo(H(m, xtemp));
3167 return res.subs(xtemp == numeric(x)).evalf();
3170 return H(x1,x2).hold();
3174 static ex H_eval(const ex& m_, const ex& x)
3177 if (is_a<lst>(m_)) {
3182 if (m.nops() == 0) {
3190 if (*m.begin() > _ex1) {
3196 } else if (*m.begin() < _ex_1) {
3202 } else if (*m.begin() == _ex0) {
3209 for (lst::const_iterator it = ++m.begin(); it != m.end(); it++) {
3210 if ((*it).info(info_flags::integer)) {
3221 } else if (*it < _ex_1) {
3241 } else if (step == 1) {
3253 // if some m_i is not an integer
3254 return H(m_, x).hold();
3257 if ((x == _ex1) && (*(--m.end()) != _ex0)) {
3258 return convert_H_to_zeta(m);
3264 return H(m_, x).hold();
3266 return pow(log(x), m.nops()) / factorial(m.nops());
3269 return pow(-pos1*log(1-pos1*x), m.nops()) / factorial(m.nops());
3271 } else if ((step == 1) && (pos1 == _ex0)){
3276 return pow(-1, p) * S(n, p, -x);
3282 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
3283 return H(m_, x).evalf();
3285 return H(m_, x).hold();
3289 static ex H_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
3292 seq.push_back(expair(H(m, x), 0));
3293 return pseries(rel, seq);
3297 static ex H_deriv(const ex& m_, const ex& x, unsigned deriv_param)
3299 GINAC_ASSERT(deriv_param < 2);
3300 if (deriv_param == 0) {
3304 if (is_a<lst>(m_)) {
3320 return 1/(1-x) * H(m, x);
3321 } else if (mb == _ex_1) {
3322 return 1/(1+x) * H(m, x);
3329 static void H_print_latex(const ex& m_, const ex& x, const print_context& c)
3332 if (is_a<lst>(m_)) {
3337 c.s << "\\mbox{H}_{";
3338 lst::const_iterator itm = m.begin();
3341 for (; itm != m.end(); itm++) {
3351 REGISTER_FUNCTION(H,
3352 evalf_func(H_evalf).
3354 series_func(H_series).
3355 derivative_func(H_deriv).
3356 print_func<print_latex>(H_print_latex).
3357 do_not_evalf_params());
3360 // takes a parameter list for H and returns an expression with corresponding multiple polylogarithms
3361 ex convert_H_to_Li(const ex& m, const ex& x)
3363 map_trafo_H_reduce_trailing_zeros filter;
3364 map_trafo_H_convert_to_Li filter2;
3366 return filter2(filter(H(m, x).hold()));
3368 return filter2(filter(H(lst(m), x).hold()));
3373 //////////////////////////////////////////////////////////////////////
3375 // Multiple zeta values zeta(x) and zeta(x,s)
3379 //////////////////////////////////////////////////////////////////////
3382 // anonymous namespace for helper functions
3386 // parameters and data for [Cra] algorithm
3387 const cln::cl_N lambda = cln::cl_N("319/320");
3390 std::vector<std::vector<cln::cl_N> > f_kj;
3391 std::vector<cln::cl_N> crB;
3392 std::vector<std::vector<cln::cl_N> > crG;
3393 std::vector<cln::cl_N> crX;
3396 void halfcyclic_convolute(const std::vector<cln::cl_N>& a, const std::vector<cln::cl_N>& b, std::vector<cln::cl_N>& c)
3398 const int size = a.size();
3399 for (int n=0; n<size; n++) {
3401 for (int m=0; m<=n; m++) {
3402 c[n] = c[n] + a[m]*b[n-m];
3409 void initcX(const std::vector<int>& s)
3411 const int k = s.size();
3417 for (int i=0; i<=L2; i++) {
3418 crB.push_back(bernoulli(i).to_cl_N() / cln::factorial(i));
3423 for (int m=0; m<k-1; m++) {
3424 std::vector<cln::cl_N> crGbuf;
3427 for (int i=0; i<=L2; i++) {
3428 crGbuf.push_back(cln::factorial(i + Sm - m - 2) / cln::factorial(i + Smp1 - m - 2));
3430 crG.push_back(crGbuf);
3435 for (int m=0; m<k-1; m++) {
3436 std::vector<cln::cl_N> Xbuf;
3437 for (int i=0; i<=L2; i++) {
3438 Xbuf.push_back(crX[i] * crG[m][i]);
3440 halfcyclic_convolute(Xbuf, crB, crX);
3446 cln::cl_N crandall_Y_loop(const cln::cl_N& Sqk)
3448 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3449 cln::cl_N factor = cln::expt(lambda, Sqk);
3450 cln::cl_N res = factor / Sqk * crX[0] * one;
3455 factor = factor * lambda;
3457 res = res + crX[N] * factor / (N+Sqk);
3458 } while ((res != resbuf) || cln::zerop(crX[N]));
3464 void calc_f(int maxr)
3469 cln::cl_N t0, t1, t2, t3, t4;
3471 std::vector<std::vector<cln::cl_N> >::iterator it = f_kj.begin();
3472 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3474 t0 = cln::exp(-lambda);
3476 for (k=1; k<=L1; k++) {
3479 for (j=1; j<=maxr; j++) {
3482 for (i=2; i<=j; i++) {
3486 (*it).push_back(t2 * t3 * cln::expt(cln::cl_I(k),-j) * one);
3494 cln::cl_N crandall_Z(const std::vector<int>& s)
3496 const int j = s.size();
3505 t0 = t0 + f_kj[q+j-2][s[0]-1];
3506 } while (t0 != t0buf);
3508 return t0 / cln::factorial(s[0]-1);
3511 std::vector<cln::cl_N> t(j);
3518 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),s[j-1]);
3519 for (int k=j-2; k>=1; k--) {
3520 t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), s[k]);
3522 t[0] = t[0] + t[1] * f_kj[q+j-2][s[0]-1];
3523 } while (t[0] != t0buf);
3525 return t[0] / cln::factorial(s[0]-1);
3530 cln::cl_N zeta_do_sum_Crandall(const std::vector<int>& s)
3532 std::vector<int> r = s;
3533 const int j = r.size();
3535 // decide on maximal size of f_kj for crandall_Z
3539 L1 = Digits * 3 + j*2;
3542 // decide on maximal size of crX for crandall_Y
3545 } else if (Digits < 86) {
3547 } else if (Digits < 192) {
3549 } else if (Digits < 394) {
3551 } else if (Digits < 808) {
3561 for (int i=0; i<j; i++) {
3570 const cln::cl_N r0factorial = cln::factorial(r[0]-1);
3572 std::vector<int> rz;
3575 for (int k=r.size()-1; k>0; k--) {
3577 rz.insert(rz.begin(), r.back());
3578 skp1buf = rz.front();
3584 for (int q=0; q<skp1buf; q++) {
3586 cln::cl_N pp1 = crandall_Y_loop(Srun+q-k);
3587 cln::cl_N pp2 = crandall_Z(rz);
3592 res = res - pp1 * pp2 / cln::factorial(q);
3594 res = res + pp1 * pp2 / cln::factorial(q);
3597 rz.front() = skp1buf;
3599 rz.insert(rz.begin(), r.back());
3603 res = (res + crandall_Y_loop(S-j)) / r0factorial + crandall_Z(rz);
3609 cln::cl_N zeta_do_sum_simple(const std::vector<int>& r)
3611 const int j = r.size();
3613 // buffer for subsums
3614 std::vector<cln::cl_N> t(j);
3615 cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3622 t[j-1] = t[j-1] + one / cln::expt(cln::cl_I(q),r[j-1]);
3623 for (int k=j-2; k>=0; k--) {
3624 t[k] = t[k] + one * t[k+1] / cln::expt(cln::cl_I(q+j-1-k), r[k]);
3626 } while (t[0] != t0buf);
3632 // does Hoelder convolution. see [BBB] (7.0)
3633 cln::cl_N zeta_do_Hoelder_convolution(const std::vector<int>& m_, const std::vector<int>& s_)
3635 // prepare parameters
3636 // holds Li arguments in [BBB] notation
3637 std::vector<int> s = s_;
3638 std::vector<int> m_p = m_;
3639 std::vector<int> m_q;
3640 // holds Li arguments in nested sums notation
3641 std::vector<cln::cl_N> s_p(s.size(), cln::cl_N(1));
3642 s_p[0] = s_p[0] * cln::cl_N("1/2");
3643 // convert notations
3645 for (int i=0; i<s_.size(); i++) {
3650 s[i] = sig * std::abs(s[i]);
3652 std::vector<cln::cl_N> s_q;
3653 cln::cl_N signum = 1;
3656 cln::cl_N res = multipleLi_do_sum(m_p, s_p);
3661 // change parameters
3662 if (s.front() > 0) {
3663 if (m_p.front() == 1) {
3664 m_p.erase(m_p.begin());
3665 s_p.erase(s_p.begin());
3666 if (s_p.size() > 0) {
3667 s_p.front() = s_p.front() * cln::cl_N("1/2");
3673 m_q.insert(m_q.begin(), 1);
3674 if (s_q.size() > 0) {
3675 s_q.front() = s_q.front() * 2;
3677 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
3680 if (m_p.front() == 1) {
3681 m_p.erase(m_p.begin());
3682 cln::cl_N spbuf = s_p.front();
3683 s_p.erase(s_p.begin());
3684 if (s_p.size() > 0) {
3685 s_p.front() = s_p.front() * spbuf;
3688 m_q.insert(m_q.begin(), 1);
3689 if (s_q.size() > 0) {
3690 s_q.front() = s_q.front() * 4;
3692 s_q.insert(s_q.begin(), cln::cl_N("1/4"));
3696 m_q.insert(m_q.begin(), 1);
3697 if (s_q.size() > 0) {
3698 s_q.front() = s_q.front() * 2;
3700 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
3705 if (m_p.size() == 0) break;
3707 res = res + signum * multipleLi_do_sum(m_p, s_p) * multipleLi_do_sum(m_q, s_q);
3712 res = res + signum * multipleLi_do_sum(m_q, s_q);
3718 } // end of anonymous namespace
3721 //////////////////////////////////////////////////////////////////////
3723 // Multiple zeta values zeta(x)
3727 //////////////////////////////////////////////////////////////////////
3730 static ex zeta1_evalf(const ex& x)
3732 if (is_exactly_a<lst>(x) && (x.nops()>1)) {
3734 // multiple zeta value
3735 const int count = x.nops();
3736 const lst& xlst = ex_to<lst>(x);
3737 std::vector<int> r(count);
3739 // check parameters and convert them
3740 lst::const_iterator it1 = xlst.begin();
3741 std::vector<int>::iterator it2 = r.begin();
3743 if (!(*it1).info(info_flags::posint)) {
3744 return zeta(x).hold();
3746 *it2 = ex_to<numeric>(*it1).to_int();
3749 } while (it2 != r.end());
3751 // check for divergence
3753 return zeta(x).hold();
3756 // decide on summation algorithm
3757 // this is still a bit clumsy
3758 int limit = (Digits>17) ? 10 : 6;
3759 if ((r[0] < limit) || ((count > 3) && (r[1] < limit/2))) {
3760 return numeric(zeta_do_sum_Crandall(r));
3762 return numeric(zeta_do_sum_simple(r));
3766 // single zeta value
3767 if (is_exactly_a<numeric>(x) && (x != 1)) {
3769 return zeta(ex_to<numeric>(x));
3770 } catch (const dunno &e) { }
3773 return zeta(x).hold();
3777 static ex zeta1_eval(const ex& m)
3779 if (is_exactly_a<lst>(m)) {
3780 if (m.nops() == 1) {
3781 return zeta(m.op(0));
3783 return zeta(m).hold();
3786 if (m.info(info_flags::numeric)) {
3787 const numeric& y = ex_to<numeric>(m);
3788 // trap integer arguments:
3789 if (y.is_integer()) {
3793 if (y.is_equal(*_num1_p)) {
3794 return zeta(m).hold();
3796 if (y.info(info_flags::posint)) {
3797 if (y.info(info_flags::odd)) {
3798 return zeta(m).hold();
3800 return abs(bernoulli(y)) * pow(Pi, y) * pow(*_num2_p, y-(*_num1_p)) / factorial(y);
3803 if (y.info(info_flags::odd)) {
3804 return -bernoulli((*_num1_p)-y) / ((*_num1_p)-y);
3811 if (y.info(info_flags::numeric) && !y.info(info_flags::crational)) {
3812 return zeta1_evalf(m);
3815 return zeta(m).hold();
3819 static ex zeta1_deriv(const ex& m, unsigned deriv_param)
3821 GINAC_ASSERT(deriv_param==0);
3823 if (is_exactly_a<lst>(m)) {
3826 return zetaderiv(_ex1, m);
3831 static void zeta1_print_latex(const ex& m_, const print_context& c)
3834 if (is_a<lst>(m_)) {
3835 const lst& m = ex_to<lst>(m_);
3836 lst::const_iterator it = m.begin();
3839 for (; it != m.end(); it++) {
3850 unsigned zeta1_SERIAL::serial = function::register_new(function_options("zeta", 1).
3851 evalf_func(zeta1_evalf).
3852 eval_func(zeta1_eval).
3853 derivative_func(zeta1_deriv).
3854 print_func<print_latex>(zeta1_print_latex).
3855 do_not_evalf_params().
3859 //////////////////////////////////////////////////////////////////////
3861 // Alternating Euler sum zeta(x,s)
3865 //////////////////////////////////////////////////////////////////////
3868 static ex zeta2_evalf(const ex& x, const ex& s)
3870 if (is_exactly_a<lst>(x)) {
3872 // alternating Euler sum
3873 const int count = x.nops();
3874 const lst& xlst = ex_to<lst>(x);
3875 const lst& slst = ex_to<lst>(s);
3876 std::vector<int> xi(count);
3877 std::vector<int> si(count);
3879 // check parameters and convert them
3880 lst::const_iterator it_xread = xlst.begin();
3881 lst::const_iterator it_sread = slst.begin();
3882 std::vector<int>::iterator it_xwrite = xi.begin();
3883 std::vector<int>::iterator it_swrite = si.begin();
3885 if (!(*it_xread).info(info_flags::posint)) {
3886 return zeta(x, s).hold();
3888 *it_xwrite = ex_to<numeric>(*it_xread).to_int();
3889 if (*it_sread > 0) {
3898 } while (it_xwrite != xi.end());
3900 // check for divergence
3901 if ((xi[0] == 1) && (si[0] == 1)) {
3902 return zeta(x, s).hold();
3905 // use Hoelder convolution
3906 return numeric(zeta_do_Hoelder_convolution(xi, si));
3909 return zeta(x, s).hold();
3913 static ex zeta2_eval(const ex& m, const ex& s_)
3915 if (is_exactly_a<lst>(s_)) {
3916 const lst& s = ex_to<lst>(s_);
3917 for (lst::const_iterator it = s.begin(); it != s.end(); it++) {
3918 if ((*it).info(info_flags::positive)) {
3921 return zeta(m, s_).hold();
3924 } else if (s_.info(info_flags::positive)) {
3928 return zeta(m, s_).hold();
3932 static ex zeta2_deriv(const ex& m, const ex& s, unsigned deriv_param)
3934 GINAC_ASSERT(deriv_param==0);
3936 if (is_exactly_a<lst>(m)) {
3939 if ((is_exactly_a<lst>(s) && s.op(0).info(info_flags::positive)) || s.info(info_flags::positive)) {
3940 return zetaderiv(_ex1, m);
3947 static void zeta2_print_latex(const ex& m_, const ex& s_, const print_context& c)
3950 if (is_a<lst>(m_)) {
3956 if (is_a<lst>(s_)) {
3962 lst::const_iterator itm = m.begin();
3963 lst::const_iterator its = s.begin();
3965 c.s << "\\overline{";
3973 for (; itm != m.end(); itm++, its++) {
3976 c.s << "\\overline{";
3987 unsigned zeta2_SERIAL::serial = function::register_new(function_options("zeta", 2).
3988 evalf_func(zeta2_evalf).
3989 eval_func(zeta2_eval).
3990 derivative_func(zeta2_deriv).
3991 print_func<print_latex>(zeta2_print_latex).
3992 do_not_evalf_params().
3996 } // namespace GiNaC