]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_nstdsums.cpp
Fix bug in inifcns_nstdsums.cpp...
[ginac.git] / ginac / inifcns_nstdsums.cpp
1 /** @file inifcns_nstdsums.cpp
2  *
3  *  Implementation of some special functions that have a representation as nested sums.
4  *
5  *  The functions are:
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})
13  *
14  *  Some remarks:
15  *
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
22  *
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.
27  *
28  *    - All functions can be numerically 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.
31  *
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].
35  *
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
38  *      the result back.
39  *
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.
46  *
47  */
48
49 /*
50  *  GiNaC Copyright (C) 1999-2019 Johannes Gutenberg University Mainz, Germany
51  *
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.
56  *
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.
61  *
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
65  */
66
67 #include "inifcns.h"
68
69 #include "add.h"
70 #include "constant.h"
71 #include "lst.h"
72 #include "mul.h"
73 #include "numeric.h"
74 #include "operators.h"
75 #include "power.h"
76 #include "pseries.h"
77 #include "relational.h"
78 #include "symbol.h"
79 #include "utils.h"
80 #include "wildcard.h"
81
82 #include <cln/cln.h>
83 #include <sstream>
84 #include <stdexcept>
85 #include <vector>
86
87 namespace GiNaC {
88
89
90 //////////////////////////////////////////////////////////////////////
91 //
92 // Classical polylogarithm  Li(n,x)
93 //
94 // helper functions
95 //
96 //////////////////////////////////////////////////////////////////////
97
98
99 // anonymous namespace for helper functions
100 namespace {
101
102
103 // lookup table for factors built from Bernoulli numbers
104 // see fill_Xn()
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;
109 int xnsize = 0;
110
111
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.
122 void fill_Xn(int n)
123 {
124         if (n>1) {
125                 // calculate X_2 and higher (corresponding to Li_4 and higher)
126                 std::vector<cln::cl_N> buf(xninitsize);
127                 auto it = buf.begin();
128                 cln::cl_N result;
129                 *it = -(cln::expt(cln::cl_I(2),n+1) - 1) / cln::expt(cln::cl_I(2),n+1); // i == 1
130                 it++;
131                 for (int i=2; i<=xninitsize; i++) {
132                         if (i&1) {
133                                 result = 0; // k == 0
134                         } else {
135                                 result = Xn[0][i/2-1]; // k == 0
136                         }
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);
140                                 }
141                         }
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
144                         
145                         *it = result;
146                         it++;
147                 }
148                 Xn.push_back(buf);
149         } else if (n==1) {
150                 // special case to handle the X_0 correct
151                 std::vector<cln::cl_N> buf(xninitsize);
152                 auto it = buf.begin();
153                 cln::cl_N result;
154                 *it = cln::cl_I(-3)/cln::cl_I(4); // i == 1
155                 it++;
156                 *it = cln::cl_I(17)/cln::cl_I(36); // i == 2
157                 it++;
158                 for (int i=3; i<=xninitsize; i++) {
159                         if (i & 1) {
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;
162                                 it++;
163                         } else {
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);
167                                 }
168                                 *it = result;
169                                 it++;
170                         }
171                 }
172                 Xn.push_back(buf);
173         } else {
174                 // calculate X_0
175                 std::vector<cln::cl_N> buf(xninitsize/2);
176                 auto it = buf.begin();
177                 for (int i=1; i<=xninitsize/2; i++) {
178                         *it = bernoulli(i*2).to_cl_N();
179                         it++;
180                 }
181                 Xn.push_back(buf);
182         }
183
184         xnsize++;
185 }
186
187 // doubles the number of entries in each Xn[]
188 void double_Xn()
189 {
190         const int pos0 = xninitsize / 2;
191         // X_0
192         for (int i=1; i<=xninitsizestep/2; ++i) {
193                 Xn[0].push_back(bernoulli((i+pos0)*2).to_cl_N());
194         }
195         if (Xn.size() > 1) {
196                 int xend = xninitsize + xninitsizestep;
197                 cln::cl_N result;
198                 // X_1
199                 for (int i=xninitsize+1; i<=xend; ++i) {
200                         if (i & 1) {
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);
203                         } else {
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);
207                                 }
208                                 Xn[1].push_back(result);
209                         }
210                 }
211                 // X_n
212                 for (size_t n=2; n<Xn.size(); ++n) {
213                         for (int i=xninitsize+1; i<=xend; ++i) {
214                                 if (i & 1) {
215                                         result = 0; // k == 0
216                                 } else {
217                                         result = Xn[0][i/2-1]; // k == 0
218                                 }
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);
222                                         }
223                                 }
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);
227                         }
228                 }
229         }
230         xninitsize += xninitsizestep;
231 }
232
233
234 // calculates Li(2,x) without Xn
235 cln::cl_N Li2_do_sum(const cln::cl_N& x)
236 {
237         cln::cl_N res = x;
238         cln::cl_N resbuf;
239         cln::cl_N num = x * cln::cl_float(1, cln::float_format(Digits));
240         cln::cl_I den = 1; // n^2 = 1
241         unsigned i = 3;
242         do {
243                 resbuf = res;
244                 num = num * x;
245                 den = den + i;  // n^2 = 4, 9, 16, ...
246                 i += 2;
247                 res = res + num / den;
248         } while (res != resbuf);
249         return res;
250 }
251
252
253 // calculates Li(2,x) with Xn
254 cln::cl_N Li2_do_sum_Xn(const cln::cl_N& x)
255 {
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;
262         cln::cl_N resbuf;
263         unsigned i = 1;
264         do {
265                 resbuf = res;
266                 factor = factor * uu / (2*i * (2*i+1));
267                 res = res + (*it) * factor;
268                 i++;
269                 if (++it == xend) {
270                         double_Xn();
271                         it = Xn[0].begin() + (i-1);
272                         xend = Xn[0].end();
273                 }
274         } while (res != resbuf);
275         return res;
276 }
277
278
279 // calculates Li(n,x), n>2 without Xn
280 cln::cl_N Lin_do_sum(int n, const cln::cl_N& x)
281 {
282         cln::cl_N factor = x * cln::cl_float(1, cln::float_format(Digits));
283         cln::cl_N res = x;
284         cln::cl_N resbuf;
285         int i=2;
286         do {
287                 resbuf = res;
288                 factor = factor * x;
289                 res = res + factor / cln::expt(cln::cl_I(i),n);
290                 i++;
291         } while (res != resbuf);
292         return res;
293 }
294
295
296 // calculates Li(n,x), n>2 with Xn
297 cln::cl_N Lin_do_sum_Xn(int n, const cln::cl_N& x)
298 {
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));
303         cln::cl_N res = u;
304         cln::cl_N resbuf;
305         unsigned i=2;
306         do {
307                 resbuf = res;
308                 factor = factor * u / i;
309                 res = res + (*it) * factor;
310                 i++;
311                 if (++it == xend) {
312                         double_Xn();
313                         it = Xn[n-2].begin() + (i-2);
314                         xend = Xn[n-2].end();
315                 }
316         } while (res != resbuf);
317         return res;
318 }
319
320
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);
323
324
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)
327 {
328         // treat n=2 as special case
329         if (n == 2) {
330                 // check if precalculated X0 exists
331                 if (xnsize == 0) {
332                         fill_Xn(0);
333                 }
334
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(x) < 0.25) {
341                                 return Li2_do_sum(x);
342                         } else {
343                                 // Li2_do_sum practically doesn't converge near x == Â±I
344                                 return Li2_do_sum_Xn(x);
345                         }
346                 } else {
347                         // choose the faster algorithm
348                         if (cln::abs(cln::realpart(x)) > 0.75) {
349                                 if ( x == 1 ) {
350                                         return cln::zeta(2);
351                                 } else {
352                                         return -Li2_do_sum(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
353                                 }
354                         } else {
355                                 return -Li2_do_sum_Xn(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
356                         }
357                 }
358         } else {
359                 // check if precalculated Xn exist
360                 if (n > xnsize+1) {
361                         for (int i=xnsize; i<n-1; i++) {
362                                 fill_Xn(i);
363                         }
364                 }
365
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(x) < 0.3) || (n >= 12)) {
370                                 return Lin_do_sum(n, x);
371                         } else {
372                                 // Li2_do_sum practically doesn't converge near x == Â±I
373                                 return Lin_do_sum_Xn(n, x);
374                         }
375                 } else {
376                         cln::cl_N result = 0;
377                         if ( x != 1 ) result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
378                         for (int j=0; j<n-1; j++) {
379                                 result = result + (S_num(n-j-1, 1, 1) - S_num(1, n-j-1, 1-x))
380                                                   * cln::expt(cln::log(x), j) / cln::factorial(j);
381                         }
382                         return result;
383                 }
384         }
385 }
386
387 // helper function for classical polylog Li
388 const cln::cl_N Lin_numeric(const int n, const cln::cl_N& x)
389 {
390         if (n == 1) {
391                 // just a log
392                 return -cln::log(1-x);
393         }
394         if (zerop(x)) {
395                 return 0;
396         }
397         if (x == 1) {
398                 // [Kol] (2.22)
399                 return cln::zeta(n);
400         }
401         else if (x == -1) {
402                 // [Kol] (2.22)
403                 return -(1-cln::expt(cln::cl_I(2),1-n)) * cln::zeta(n);
404         }
405         if (cln::abs(realpart(x)) < 0.4 && cln::abs(cln::abs(x)-1) < 0.01) {
406                 cln::cl_N result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
407                 for (int j=0; j<n-1; j++) {
408                         result = result + (S_num(n-j-1, 1, 1) - S_num(1, n-j-1, 1-x))
409                                 * cln::expt(cln::log(x), j) / cln::factorial(j);
410                 }
411                 return result;
412         }
413
414         // what is the desired float format?
415         // first guess: default format
416         cln::float_format_t prec = cln::default_float_format;
417         const cln::cl_N value = x;
418         // second guess: the argument's format
419         if (!instanceof(realpart(x), cln::cl_RA_ring))
420                 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
421         else if (!instanceof(imagpart(x), cln::cl_RA_ring))
422                 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
423         
424         // [Kol] (5.15)
425         if (cln::abs(value) > 1) {
426                 cln::cl_N result = -cln::expt(cln::log(-value),n) / cln::factorial(n);
427                 // check if argument is complex. if it is real, the new polylog has to be conjugated.
428                 if (cln::zerop(cln::imagpart(value))) {
429                         if (n & 1) {
430                                 result = result + conjugate(Li_projection(n, cln::recip(value), prec));
431                         }
432                         else {
433                                 result = result - conjugate(Li_projection(n, cln::recip(value), prec));
434                         }
435                 }
436                 else {
437                         if (n & 1) {
438                                 result = result + Li_projection(n, cln::recip(value), prec);
439                         }
440                         else {
441                                 result = result - Li_projection(n, cln::recip(value), prec);
442                         }
443                 }
444                 cln::cl_N add;
445                 for (int j=0; j<n-1; j++) {
446                         add = add + (1+cln::expt(cln::cl_I(-1),n-j)) * (1-cln::expt(cln::cl_I(2),1-n+j))
447                                     * Lin_numeric(n-j,1) * cln::expt(cln::log(-value),j) / cln::factorial(j);
448                 }
449                 result = result - add;
450                 return result;
451         }
452         else {
453                 return Li_projection(n, value, prec);
454         }
455 }
456
457
458 } // end of anonymous namespace
459
460
461 //////////////////////////////////////////////////////////////////////
462 //
463 // Multiple polylogarithm  Li(n,x)
464 //
465 // helper function
466 //
467 //////////////////////////////////////////////////////////////////////
468
469
470 // anonymous namespace for helper function
471 namespace {
472
473
474 // performs the actual series summation for multiple polylogarithms
475 cln::cl_N multipleLi_do_sum(const std::vector<int>& s, const std::vector<cln::cl_N>& x)
476 {
477         // ensure all x <> 0.
478         for (const auto & it : x) {
479                 if (it == 0) return cln::cl_float(0, cln::float_format(Digits));
480         }
481
482         const int j = s.size();
483         bool flag_accidental_zero = false;
484
485         std::vector<cln::cl_N> t(j);
486         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
487
488         cln::cl_N t0buf;
489         int q = 0;
490         do {
491                 t0buf = t[0];
492                 q++;
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                         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]);
496                 }
497                 q++;
498                 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
499                 for (int k=j-2; k>=0; k--) {
500                         flag_accidental_zero = cln::zerop(t[k+1]);
501                         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                 }
503         } while ( (t[0] != t0buf) || cln::zerop(t[0]) || flag_accidental_zero );
504
505         return t[0];
506 }
507
508
509 // forward declaration for Li_eval()
510 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf);
511
512
513 // type used by the transformation functions for G
514 typedef std::vector<int> Gparameter;
515
516
517 // G_eval1-function for G transformations
518 ex G_eval1(int a, int scale, const exvector& gsyms)
519 {
520         if (a != 0) {
521                 const ex& scs = gsyms[std::abs(scale)];
522                 const ex& as = gsyms[std::abs(a)];
523                 if (as != scs) {
524                         return -log(1 - scs/as);
525                 } else {
526                         return -zeta(1);
527                 }
528         } else {
529                 return log(gsyms[std::abs(scale)]);
530         }
531 }
532
533
534 // G_eval-function for G transformations
535 ex G_eval(const Gparameter& a, int scale, const exvector& gsyms)
536 {
537         // check for properties of G
538         ex sc = gsyms[std::abs(scale)];
539         lst newa;
540         bool all_zero = true;
541         bool all_ones = true;
542         int count_ones = 0;
543         for (const auto & it : a) {
544                 if (it != 0) {
545                         const ex sym = gsyms[std::abs(it)];
546                         newa.append(sym);
547                         all_zero = false;
548                         if (sym != sc) {
549                                 all_ones = false;
550                         }
551                         if (all_ones) {
552                                 ++count_ones;
553                         }
554                 } else {
555                         all_ones = false;
556                 }
557         }
558
559         // care about divergent G: shuffle to separate divergencies that will be canceled
560         // later on in the transformation
561         if (newa.nops() > 1 && newa.op(0) == sc && !all_ones && a.front()!=0) {
562                 // do shuffle
563                 Gparameter short_a(a.begin()+1, a.end());
564                 ex result = G_eval1(a.front(), scale, gsyms) * G_eval(short_a, scale, gsyms);
565
566                 auto it = short_a.begin();
567                 advance(it, count_ones-1);
568                 for (; it != short_a.end(); ++it) {
569
570                         Gparameter newa(short_a.begin(), it);
571                         newa.push_back(*it);
572                         newa.push_back(a[0]);
573                         newa.insert(newa.end(), it+1, short_a.end());
574                         result -= G_eval(newa, scale, gsyms);
575                 }
576                 return result / count_ones;
577         }
578
579         // G({1,...,1};y) -> G({1};y)^k / k!
580         if (all_ones && a.size() > 1) {
581                 return pow(G_eval1(a.front(),scale, gsyms), count_ones) / factorial(count_ones);
582         }
583
584         // G({0,...,0};y) -> log(y)^k / k!
585         if (all_zero) {
586                 return pow(log(gsyms[std::abs(scale)]), a.size()) / factorial(a.size());
587         }
588
589         // no special cases anymore -> convert it into Li
590         lst m;
591         lst x;
592         ex argbuf = gsyms[std::abs(scale)];
593         ex mval = _ex1;
594         for (const auto & it : a) {
595                 if (it != 0) {
596                         const ex& sym = gsyms[std::abs(it)];
597                         x.append(argbuf / sym);
598                         m.append(mval);
599                         mval = _ex1;
600                         argbuf = sym;
601                 } else {
602                         ++mval;
603                 }
604         }
605         return pow(-1, x.nops()) * Li(m, x);
606 }
607
608 // convert back to standard G-function, keep information on small imaginary parts
609 ex G_eval_to_G(const Gparameter& a, int scale, const exvector& gsyms)
610 {
611         lst z;
612         lst s;
613         for (const auto & it : a) {
614                 if (it != 0) {
615                         z.append(gsyms[std::abs(it)]);
616                         if ( it<0 ) {
617                           s.append(-1);
618                         } else {
619                           s.append(1);
620                         }
621                 } else {
622                         z.append(0);
623                         s.append(1);
624                 }
625         }
626         return G(z,s,gsyms[std::abs(scale)]);
627 }
628
629
630 // converts data for G: pending_integrals -> a
631 Gparameter convert_pending_integrals_G(const Gparameter& pending_integrals)
632 {
633         GINAC_ASSERT(pending_integrals.size() != 1);
634
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());
638                 return new_a;
639         } else {
640                 // just return empty parameter list
641                 Gparameter new_a;
642                 return new_a;
643         }
644 }
645
646
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)
654 {
655         convergent = true;
656         depth = 0;
657         trailing_zeros = 0;
658         min_it = a.end();
659         auto lastnonzero = a.end();
660         for (auto it = a.begin(); it != a.end(); ++it) {
661                 if (std::abs(*it) > 0) {
662                         ++depth;
663                         trailing_zeros = 0;
664                         lastnonzero = it;
665                         if (std::abs(*it) < scale) {
666                                 convergent = false;
667                                 if ((min_it == a.end()) || (std::abs(*it) < std::abs(*min_it))) {
668                                         min_it = it;
669                                 }
670                         }
671                 } else {
672                         ++trailing_zeros;
673                 }
674         }
675         if (lastnonzero == a.end())
676                 return a.end();
677         return ++lastnonzero;
678 }
679
680
681 // add scale to pending_integrals if pending_integrals is empty
682 Gparameter prepare_pending_integrals(const Gparameter& pending_integrals, int scale)
683 {
684         GINAC_ASSERT(pending_integrals.size() != 1);
685
686         if (pending_integrals.size() > 0) {
687                 return pending_integrals;
688         } else {
689                 Gparameter new_pending_integrals;
690                 new_pending_integrals.push_back(scale);
691                 return new_pending_integrals;
692         }
693 }
694
695
696 // handles trailing zeroes for an otherwise convergent integral
697 ex trailing_zeros_G(const Gparameter& a, int scale, const exvector& gsyms)
698 {
699         bool convergent;
700         int depth, trailing_zeros;
701         Gparameter::const_iterator last, dummyit;
702         last = check_parameter_G(a, scale, convergent, depth, trailing_zeros, dummyit);
703
704         GINAC_ASSERT(convergent);
705
706         if ((trailing_zeros > 0) && (depth > 0)) {
707                 ex result;
708                 Gparameter new_a(a.begin(), a.end()-1);
709                 result += G_eval1(0, scale, gsyms) * trailing_zeros_G(new_a, scale, gsyms);
710                 for (auto it = a.begin(); it != last; ++it) {
711                         Gparameter new_a(a.begin(), it);
712                         new_a.push_back(0);
713                         new_a.insert(new_a.end(), it, a.end()-1);
714                         result -= trailing_zeros_G(new_a, scale, gsyms);
715                 }
716
717                 return result / trailing_zeros;
718         } else {
719                 return G_eval(a, scale, gsyms);
720         }
721 }
722
723
724 // G transformation [VSW] (57),(58)
725 ex depth_one_trafo_G(const Gparameter& pending_integrals, const Gparameter& a, int scale, const exvector& gsyms)
726 {
727         // pendint = ( y1, b1, ..., br )
728         //       a = ( 0, ..., 0, amin )
729         //   scale = y2
730         //
731         // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(0, ..., 0, sr; y2)
732         // where sr replaces amin
733
734         GINAC_ASSERT(a.back() != 0);
735         GINAC_ASSERT(a.size() > 0);
736
737         ex result;
738         Gparameter new_pending_integrals = prepare_pending_integrals(pending_integrals, std::abs(a.back()));
739         const int psize = pending_integrals.size();
740
741         // length == 1
742         // G(sr_{+-}; y2 ) = G(y2_{-+}; sr) - G(0; sr) + ln(-y2_{-+})
743
744         if (a.size() == 1) {
745
746           // ln(-y2_{-+})
747           result += log(gsyms[ex_to<numeric>(scale).to_int()]);
748                 if (a.back() > 0) {
749                         new_pending_integrals.push_back(-scale);
750                         result += I*Pi;
751                 } else {
752                         new_pending_integrals.push_back(scale);
753                         result -= I*Pi;
754                 }
755                 if (psize) {
756                         result *= trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
757                                                    pending_integrals.front(),
758                                                    gsyms);
759                 }
760                 
761                 // G(y2_{-+}; sr)
762                 result += trailing_zeros_G(convert_pending_integrals_G(new_pending_integrals),
763                                            new_pending_integrals.front(),
764                                            gsyms);
765                 
766                 // G(0; sr)
767                 new_pending_integrals.back() = 0;
768                 result -= trailing_zeros_G(convert_pending_integrals_G(new_pending_integrals),
769                                            new_pending_integrals.front(),
770                                            gsyms);
771
772                 return result;
773         }
774
775         // length > 1
776         // G_m(sr_{+-}; y2) = -zeta_m + int_0^y2 dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
777         //                            - int_0^sr dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
778
779         //term zeta_m
780         result -= zeta(a.size());
781         if (psize) {
782                 result *= trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
783                                            pending_integrals.front(),
784                                            gsyms);
785         }
786         
787         // term int_0^sr dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
788         //    = int_0^sr dt/t G_{m-1}( t_{+-}; y2 )
789         Gparameter new_a(a.begin()+1, a.end());
790         new_pending_integrals.push_back(0);
791         result -= depth_one_trafo_G(new_pending_integrals, new_a, scale, gsyms);
792         
793         // term int_0^y2 dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
794         //    = int_0^y2 dt/t G_{m-1}( t_{+-}; y2 )
795         Gparameter new_pending_integrals_2;
796         new_pending_integrals_2.push_back(scale);
797         new_pending_integrals_2.push_back(0);
798         if (psize) {
799                 result += trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
800                                            pending_integrals.front(),
801                                            gsyms)
802                           * depth_one_trafo_G(new_pending_integrals_2, new_a, scale, gsyms);
803         } else {
804                 result += depth_one_trafo_G(new_pending_integrals_2, new_a, scale, gsyms);
805         }
806
807         return result;
808 }
809
810
811 // forward declaration
812 ex shuffle_G(const Gparameter & a0, const Gparameter & a1, const Gparameter & a2,
813              const Gparameter& pendint, const Gparameter& a_old, int scale,
814              const exvector& gsyms, bool flag_trailing_zeros_only);
815
816
817 // G transformation [VSW]
818 ex G_transform(const Gparameter& pendint, const Gparameter& a, int scale,
819                const exvector& gsyms, bool flag_trailing_zeros_only)
820 {
821         // main recursion routine
822         //
823         // pendint = ( y1, b1, ..., br )
824         //       a = ( a1, ..., amin, ..., aw )
825         //   scale = y2
826         //
827         // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,sr,...,aw,y2)
828         // where sr replaces amin
829
830         // find smallest alpha, determine depth and trailing zeros, and check for convergence
831         bool convergent;
832         int depth, trailing_zeros;
833         Gparameter::const_iterator min_it;
834         auto firstzero = check_parameter_G(a, scale, convergent, depth, trailing_zeros, min_it);
835         int min_it_pos = distance(a.begin(), min_it);
836
837         // special case: all a's are zero
838         if (depth == 0) {
839                 ex result;
840
841                 if (a.size() == 0) {
842                         result = 1;
843                 } else {
844                         result = G_eval(a, scale, gsyms);
845                 }
846                 if (pendint.size() > 0) {
847                         result *= trailing_zeros_G(convert_pending_integrals_G(pendint),
848                                                    pendint.front(),
849                                                    gsyms);
850                 } 
851                 return result;
852         }
853
854         // handle trailing zeros
855         if (trailing_zeros > 0) {
856                 ex result;
857                 Gparameter new_a(a.begin(), a.end()-1);
858                 result += G_eval1(0, scale, gsyms) * G_transform(pendint, new_a, scale, gsyms, flag_trailing_zeros_only);
859                 for (auto it = a.begin(); it != firstzero; ++it) {
860                         Gparameter new_a(a.begin(), it);
861                         new_a.push_back(0);
862                         new_a.insert(new_a.end(), it, a.end()-1);
863                         result -= G_transform(pendint, new_a, scale, gsyms, flag_trailing_zeros_only);
864                 }
865                 return result / trailing_zeros;
866         }
867
868         // flag_trailing_zeros_only: in this case we don't have pending integrals
869         if (flag_trailing_zeros_only)
870                 return G_eval_to_G(a, scale, gsyms);
871
872         // convergence case
873         if (convergent) {
874                 if (pendint.size() > 0) {
875                         return G_eval(convert_pending_integrals_G(pendint),
876                                       pendint.front(), gsyms) *
877                                G_eval(a, scale, gsyms);
878                 } else {
879                         return G_eval(a, scale, gsyms);
880                 }
881         }
882
883         // call basic transformation for depth equal one
884         if (depth == 1) {
885                 return depth_one_trafo_G(pendint, a, scale, gsyms);
886         }
887
888         // do recursion
889         // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,sr,...,aw,y2)
890         //  =  int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,0,...,aw,y2)
891         //   + 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)
892
893         // smallest element in last place
894         if (min_it + 1 == a.end()) {
895                 do { --min_it; } while (*min_it == 0);
896                 Gparameter empty;
897                 Gparameter a1(a.begin(),min_it+1);
898                 Gparameter a2(min_it+1,a.end());
899
900                 ex result = G_transform(pendint, a2, scale, gsyms, flag_trailing_zeros_only)*
901                             G_transform(empty, a1, scale, gsyms, flag_trailing_zeros_only);
902
903                 result -= shuffle_G(empty, a1, a2, pendint, a, scale, gsyms, flag_trailing_zeros_only);
904                 return result;
905         }
906
907         Gparameter empty;
908         Gparameter::iterator changeit;
909
910         // first term G(a_1,..,0,...,a_w;a_0)
911         Gparameter new_pendint = prepare_pending_integrals(pendint, a[min_it_pos]);
912         Gparameter new_a = a;
913         new_a[min_it_pos] = 0;
914         ex result = G_transform(empty, new_a, scale, gsyms, flag_trailing_zeros_only);
915         if (pendint.size() > 0) {
916                 result *= trailing_zeros_G(convert_pending_integrals_G(pendint),
917                                            pendint.front(), gsyms);
918         }
919
920         // other terms
921         changeit = new_a.begin() + min_it_pos;
922         changeit = new_a.erase(changeit);
923         if (changeit != new_a.begin()) {
924                 // smallest in the middle
925                 new_pendint.push_back(*changeit);
926                 result -= trailing_zeros_G(convert_pending_integrals_G(new_pendint),
927                                            new_pendint.front(), gsyms)*
928                           G_transform(empty, new_a, scale, gsyms, flag_trailing_zeros_only);
929                 int buffer = *changeit;
930                 *changeit = *min_it;
931                 result += G_transform(new_pendint, new_a, scale, gsyms, flag_trailing_zeros_only);
932                 *changeit = buffer;
933                 new_pendint.pop_back();
934                 --changeit;
935                 new_pendint.push_back(*changeit);
936                 result += trailing_zeros_G(convert_pending_integrals_G(new_pendint),
937                                            new_pendint.front(), gsyms)*
938                           G_transform(empty, new_a, scale, gsyms, flag_trailing_zeros_only);
939                 *changeit = *min_it;
940                 result -= G_transform(new_pendint, new_a, scale, gsyms, flag_trailing_zeros_only);
941         } else {
942                 // smallest at the front
943                 new_pendint.push_back(scale);
944                 result += trailing_zeros_G(convert_pending_integrals_G(new_pendint),
945                                            new_pendint.front(), gsyms)*
946                           G_transform(empty, new_a, scale, gsyms, flag_trailing_zeros_only);
947                 new_pendint.back() =  *changeit;
948                 result -= trailing_zeros_G(convert_pending_integrals_G(new_pendint),
949                                            new_pendint.front(), gsyms)*
950                           G_transform(empty, new_a, scale, gsyms, flag_trailing_zeros_only);
951                 *changeit = *min_it;
952                 result += G_transform(new_pendint, new_a, scale, gsyms, flag_trailing_zeros_only);
953         }
954         return result;
955 }
956
957
958 // shuffles the two parameter list a1 and a2 and calls G_transform for every term except
959 // for the one that is equal to a_old
960 ex shuffle_G(const Gparameter & a0, const Gparameter & a1, const Gparameter & a2,
961              const Gparameter& pendint, const Gparameter& a_old, int scale,
962              const exvector& gsyms, bool flag_trailing_zeros_only)
963 {
964         if (a1.size()==0 && a2.size()==0) {
965                 // veto the one configuration we don't want
966                 if ( a0 == a_old ) return 0;
967
968                 return G_transform(pendint, a0, scale, gsyms, flag_trailing_zeros_only);
969         }
970
971         if (a2.size()==0) {
972                 Gparameter empty;
973                 Gparameter aa0 = a0;
974                 aa0.insert(aa0.end(),a1.begin(),a1.end());
975                 return shuffle_G(aa0, empty, empty, pendint, a_old, scale, gsyms, flag_trailing_zeros_only);
976         }
977
978         if (a1.size()==0) {
979                 Gparameter empty;
980                 Gparameter aa0 = a0;
981                 aa0.insert(aa0.end(),a2.begin(),a2.end());
982                 return shuffle_G(aa0, empty, empty, pendint, a_old, scale, gsyms, flag_trailing_zeros_only);
983         }
984
985         Gparameter a1_removed(a1.begin()+1,a1.end());
986         Gparameter a2_removed(a2.begin()+1,a2.end());
987
988         Gparameter a01 = a0;
989         Gparameter a02 = a0;
990
991         a01.push_back( a1[0] );
992         a02.push_back( a2[0] );
993
994         return shuffle_G(a01, a1_removed, a2, pendint, a_old, scale, gsyms, flag_trailing_zeros_only)
995              + shuffle_G(a02, a1, a2_removed, pendint, a_old, scale, gsyms, flag_trailing_zeros_only);
996 }
997
998 // handles the transformations and the numerical evaluation of G
999 // the parameter x, s and y must only contain numerics
1000 static cln::cl_N
1001 G_numeric(const std::vector<cln::cl_N>& x, const std::vector<int>& s,
1002           const cln::cl_N& y);
1003
1004 // do acceleration transformation (hoelder convolution [BBB])
1005 // the parameter x, s and y must only contain numerics
1006 static cln::cl_N
1007 G_do_hoelder(std::vector<cln::cl_N> x, /* yes, it's passed by value */
1008              const std::vector<int>& s, const cln::cl_N& y)
1009 {
1010         cln::cl_N result;
1011         const std::size_t size = x.size();
1012         for (std::size_t i = 0; i < size; ++i)
1013                 x[i] = x[i]/y;
1014
1015         for (std::size_t r = 0; r <= size; ++r) {
1016                 cln::cl_N buffer(1 & r ? -1 : 1);
1017                 cln::cl_RA p(2);
1018                 bool adjustp;
1019                 do {
1020                         adjustp = false;
1021                         for (std::size_t i = 0; i < size; ++i) {
1022                                 if (x[i] == cln::cl_RA(1)/p) {
1023                                         p = p/2 + cln::cl_RA(3)/2;
1024                                         adjustp = true;
1025                                         continue;
1026                                 }
1027                         }
1028                 } while (adjustp);
1029                 cln::cl_RA q = p/(p-1);
1030                 std::vector<cln::cl_N> qlstx;
1031                 std::vector<int> qlsts;
1032                 for (std::size_t j = r; j >= 1; --j) {
1033                         qlstx.push_back(cln::cl_N(1) - x[j-1]);
1034                         if (imagpart(x[j-1])==0 && realpart(x[j-1]) >= 1) {
1035                                 qlsts.push_back(1);
1036                         } else {
1037                                 qlsts.push_back(-s[j-1]);
1038                         }
1039                 }
1040                 if (qlstx.size() > 0) {
1041                         buffer = buffer*G_numeric(qlstx, qlsts, 1/q);
1042                 }
1043                 std::vector<cln::cl_N> plstx;
1044                 std::vector<int> plsts;
1045                 for (std::size_t j = r+1; j <= size; ++j) {
1046                         plstx.push_back(x[j-1]);
1047                         plsts.push_back(s[j-1]);
1048                 }
1049                 if (plstx.size() > 0) {
1050                         buffer = buffer*G_numeric(plstx, plsts, 1/p);
1051                 }
1052                 result = result + buffer;
1053         }
1054         return result;
1055 }
1056
1057 class less_object_for_cl_N
1058 {
1059 public:
1060         bool operator() (const cln::cl_N & a, const cln::cl_N & b) const
1061         {
1062                 // absolute value?
1063                 if (abs(a) != abs(b))
1064                         return (abs(a) < abs(b)) ? true : false;
1065
1066                 // complex phase?
1067                 if (phase(a) != phase(b))
1068                         return (phase(a) < phase(b)) ? true : false;
1069
1070                 // equal, therefore "less" is not true
1071                 return false;
1072         }
1073 };
1074
1075
1076 // convergence transformation, used for numerical evaluation of G function.
1077 // the parameter x, s and y must only contain numerics
1078 static cln::cl_N
1079 G_do_trafo(const std::vector<cln::cl_N>& x, const std::vector<int>& s,
1080            const cln::cl_N& y, bool flag_trailing_zeros_only)
1081 {
1082         // sort (|x|<->position) to determine indices
1083         typedef std::multimap<cln::cl_N, std::size_t, less_object_for_cl_N> sortmap_t;
1084         sortmap_t sortmap;
1085         std::size_t size = 0;
1086         for (std::size_t i = 0; i < x.size(); ++i) {
1087                 if (!zerop(x[i])) {
1088                         sortmap.insert(std::make_pair(x[i], i));
1089                         ++size;
1090                 }
1091         }
1092         // include upper limit (scale)
1093         sortmap.insert(std::make_pair(y, x.size()));
1094
1095         // generate missing dummy-symbols
1096         int i = 1;
1097         // holding dummy-symbols for the G/Li transformations
1098         exvector gsyms;
1099         gsyms.push_back(symbol("GSYMS_ERROR"));
1100         cln::cl_N lastentry(0);
1101         for (sortmap_t::const_iterator it = sortmap.begin(); it != sortmap.end(); ++it) {
1102                 if (it != sortmap.begin()) {
1103                         if (it->second < x.size()) {
1104                                 if (x[it->second] == lastentry) {
1105                                         gsyms.push_back(gsyms.back());
1106                                         continue;
1107                                 }
1108                         } else {
1109                                 if (y == lastentry) {
1110                                         gsyms.push_back(gsyms.back());
1111                                         continue;
1112                                 }
1113                         }
1114                 }
1115                 std::ostringstream os;
1116                 os << "a" << i;
1117                 gsyms.push_back(symbol(os.str()));
1118                 ++i;
1119                 if (it->second < x.size()) {
1120                         lastentry = x[it->second];
1121                 } else {
1122                         lastentry = y;
1123                 }
1124         }
1125
1126         // fill position data according to sorted indices and prepare substitution list
1127         Gparameter a(x.size());
1128         exmap subslst;
1129         std::size_t pos = 1;
1130         int scale = pos;
1131         for (sortmap_t::const_iterator it = sortmap.begin(); it != sortmap.end(); ++it) {
1132                 if (it->second < x.size()) {
1133                         if (s[it->second] > 0) {
1134                                 a[it->second] = pos;
1135                         } else {
1136                                 a[it->second] = -int(pos);
1137                         }
1138                         subslst[gsyms[pos]] = numeric(x[it->second]);
1139                 } else {
1140                         scale = pos;
1141                         subslst[gsyms[pos]] = numeric(y);
1142                 }
1143                 ++pos;
1144         }
1145
1146         // do transformation
1147         Gparameter pendint;
1148         ex result = G_transform(pendint, a, scale, gsyms, flag_trailing_zeros_only);
1149         // replace dummy symbols with their values
1150         result = result.expand();
1151         result = result.subs(subslst).evalf();
1152         if (!is_a<numeric>(result))
1153                 throw std::logic_error("G_do_trafo: G_transform returned non-numeric result");
1154         
1155         cln::cl_N ret = ex_to<numeric>(result).to_cl_N();
1156         return ret;
1157 }
1158
1159 // handles the transformations and the numerical evaluation of G
1160 // the parameter x, s and y must only contain numerics
1161 static cln::cl_N
1162 G_numeric(const std::vector<cln::cl_N>& x, const std::vector<int>& s,
1163           const cln::cl_N& y)
1164 {
1165         // check for convergence and necessary accelerations
1166         bool need_trafo = false;
1167         bool need_hoelder = false;
1168         bool have_trailing_zero = false;
1169         std::size_t depth = 0;
1170         for (auto & xi : x) {
1171                 if (!zerop(xi)) {
1172                         ++depth;
1173                         const cln::cl_N x_y = abs(xi) - y;
1174                         if (instanceof(x_y, cln::cl_R_ring) &&
1175                             realpart(x_y) < cln::least_negative_float(cln::float_format(Digits - 2)))
1176                                 need_trafo = true;
1177
1178                         if (abs(abs(xi/y) - 1) < 0.01)
1179                                 need_hoelder = true;
1180                 }
1181         }
1182         if (zerop(x.back())) {
1183                 have_trailing_zero = true;
1184                 need_trafo = true;
1185         }
1186
1187         if (depth == 1 && x.size() == 2 && !need_trafo)
1188                 return - Li_projection(2, y/x[1], cln::float_format(Digits));
1189         
1190         // do acceleration transformation (hoelder convolution [BBB])
1191         if (need_hoelder && !have_trailing_zero)
1192                 return G_do_hoelder(x, s, y);
1193         
1194         // convergence transformation
1195         if (need_trafo)
1196                 return G_do_trafo(x, s, y, have_trailing_zero);
1197
1198         // do summation
1199         std::vector<cln::cl_N> newx;
1200         newx.reserve(x.size());
1201         std::vector<int> m;
1202         m.reserve(x.size());
1203         int mcount = 1;
1204         int sign = 1;
1205         cln::cl_N factor = y;
1206         for (auto & xi : x) {
1207                 if (zerop(xi)) {
1208                         ++mcount;
1209                 } else {
1210                         newx.push_back(factor/xi);
1211                         factor = xi;
1212                         m.push_back(mcount);
1213                         mcount = 1;
1214                         sign = -sign;
1215                 }
1216         }
1217
1218         return sign*multipleLi_do_sum(m, newx);
1219 }
1220
1221
1222 ex mLi_numeric(const lst& m, const lst& x)
1223 {
1224         // let G_numeric do the transformation
1225         std::vector<cln::cl_N> newx;
1226         newx.reserve(x.nops());
1227         std::vector<int> s;
1228         s.reserve(x.nops());
1229         cln::cl_N factor(1);
1230         for (auto itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1231                 for (int i = 1; i < *itm; ++i) {
1232                         newx.push_back(cln::cl_N(0));
1233                         s.push_back(1);
1234                 }
1235                 const cln::cl_N xi = ex_to<numeric>(*itx).to_cl_N();
1236                 factor = factor/xi;
1237                 newx.push_back(factor);
1238                 if ( !instanceof(factor, cln::cl_R_ring) && imagpart(factor) < 0 ) {
1239                         s.push_back(-1);
1240                 }
1241                 else {
1242                         s.push_back(1);
1243                 }
1244         }
1245         return numeric(cln::cl_N(1 & m.nops() ? - 1 : 1)*G_numeric(newx, s, cln::cl_N(1)));
1246 }
1247
1248
1249 } // end of anonymous namespace
1250
1251
1252 //////////////////////////////////////////////////////////////////////
1253 //
1254 // Generalized multiple polylogarithm  G(x, y) and G(x, s, y)
1255 //
1256 // GiNaC function
1257 //
1258 //////////////////////////////////////////////////////////////////////
1259
1260
1261 static ex G2_evalf(const ex& x_, const ex& y)
1262 {
1263         if ((!y.info(info_flags::numeric)) || (!y.info(info_flags::positive))) {
1264                 return G(x_, y).hold();
1265         }
1266         lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst{x_};
1267         if (x.nops() == 0) {
1268                 return _ex1;
1269         }
1270         if (x.op(0) == y) {
1271                 return G(x_, y).hold();
1272         }
1273         std::vector<int> s;
1274         s.reserve(x.nops());
1275         bool all_zero = true;
1276         for (const auto & it : x) {
1277                 if (!it.info(info_flags::numeric)) {
1278                         return G(x_, y).hold();
1279                 }
1280                 if (it != _ex0) {
1281                         all_zero = false;
1282                 }
1283                 if ( !ex_to<numeric>(it).is_real() && ex_to<numeric>(it).imag() < 0 ) {
1284                         s.push_back(-1);
1285                 }
1286                 else {
1287                         s.push_back(1);
1288                 }
1289         }
1290         if (all_zero) {
1291                 return pow(log(y), x.nops()) / factorial(x.nops());
1292         }
1293         std::vector<cln::cl_N> xv;
1294         xv.reserve(x.nops());
1295         for (const auto & it : x)
1296                 xv.push_back(ex_to<numeric>(it).to_cl_N());
1297         cln::cl_N result = G_numeric(xv, s, ex_to<numeric>(y).to_cl_N());
1298         return numeric(result);
1299 }
1300
1301
1302 static ex G2_eval(const ex& x_, const ex& y)
1303 {
1304         //TODO eval to MZV or H or S or Lin
1305
1306         if ((!y.info(info_flags::numeric)) || (!y.info(info_flags::positive))) {
1307                 return G(x_, y).hold();
1308         }
1309         lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst{x_};
1310         if (x.nops() == 0) {
1311                 return _ex1;
1312         }
1313         if (x.op(0) == y) {
1314                 return G(x_, y).hold();
1315         }
1316         std::vector<int> s;
1317         s.reserve(x.nops());
1318         bool all_zero = true;
1319         bool crational = true;
1320         for (const auto & it : x) {
1321                 if (!it.info(info_flags::numeric)) {
1322                         return G(x_, y).hold();
1323                 }
1324                 if (!it.info(info_flags::crational)) {
1325                         crational = false;
1326                 }
1327                 if (it != _ex0) {
1328                         all_zero = false;
1329                 }
1330                 if ( !ex_to<numeric>(it).is_real() && ex_to<numeric>(it).imag() < 0 ) {
1331                         s.push_back(-1);
1332                 }
1333                 else {
1334                         s.push_back(+1);
1335                 }
1336         }
1337         if (all_zero) {
1338                 return pow(log(y), x.nops()) / factorial(x.nops());
1339         }
1340         if (!y.info(info_flags::crational)) {
1341                 crational = false;
1342         }
1343         if (crational) {
1344                 return G(x_, y).hold();
1345         }
1346         std::vector<cln::cl_N> xv;
1347         xv.reserve(x.nops());
1348         for (const auto & it : x)
1349                 xv.push_back(ex_to<numeric>(it).to_cl_N());
1350         cln::cl_N result = G_numeric(xv, s, ex_to<numeric>(y).to_cl_N());
1351         return numeric(result);
1352 }
1353
1354
1355 // option do_not_evalf_params() removed.
1356 unsigned G2_SERIAL::serial = function::register_new(function_options("G", 2).
1357                                 evalf_func(G2_evalf).
1358                                 eval_func(G2_eval).
1359                                 overloaded(2));
1360 //TODO
1361 //                                derivative_func(G2_deriv).
1362 //                                print_func<print_latex>(G2_print_latex).
1363
1364
1365 static ex G3_evalf(const ex& x_, const ex& s_, const ex& y)
1366 {
1367         if ((!y.info(info_flags::numeric)) || (!y.info(info_flags::positive))) {
1368                 return G(x_, s_, y).hold();
1369         }
1370         lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst{x_};
1371         lst s = is_a<lst>(s_) ? ex_to<lst>(s_) : lst{s_};
1372         if (x.nops() != s.nops()) {
1373                 return G(x_, s_, y).hold();
1374         }
1375         if (x.nops() == 0) {
1376                 return _ex1;
1377         }
1378         if (x.op(0) == y) {
1379                 return G(x_, s_, y).hold();
1380         }
1381         std::vector<int> sn;
1382         sn.reserve(s.nops());
1383         bool all_zero = true;
1384         for (auto itx = x.begin(), its = s.begin(); itx != x.end(); ++itx, ++its) {
1385                 if (!(*itx).info(info_flags::numeric)) {
1386                         return G(x_, y).hold();
1387                 }
1388                 if (!(*its).info(info_flags::real)) {
1389                         return G(x_, y).hold();
1390                 }
1391                 if (*itx != _ex0) {
1392                         all_zero = false;
1393                 }
1394                 if ( ex_to<numeric>(*itx).is_real() ) {
1395                         if ( ex_to<numeric>(*itx).is_positive() ) {
1396                                 if ( *its >= 0 ) {
1397                                         sn.push_back(1);
1398                                 }
1399                                 else {
1400                                         sn.push_back(-1);
1401                                 }
1402                         } else {
1403                                 sn.push_back(1);
1404                         }
1405                 }
1406                 else {
1407                         if ( ex_to<numeric>(*itx).imag() > 0 ) {
1408                                 sn.push_back(1);
1409                         }
1410                         else {
1411                                 sn.push_back(-1);
1412                         }
1413                 }
1414         }
1415         if (all_zero) {
1416                 return pow(log(y), x.nops()) / factorial(x.nops());
1417         }
1418         std::vector<cln::cl_N> xn;
1419         xn.reserve(x.nops());
1420         for (const auto & it : x)
1421                 xn.push_back(ex_to<numeric>(it).to_cl_N());
1422         cln::cl_N result = G_numeric(xn, sn, ex_to<numeric>(y).to_cl_N());
1423         return numeric(result);
1424 }
1425
1426
1427 static ex G3_eval(const ex& x_, const ex& s_, const ex& y)
1428 {
1429         //TODO eval to MZV or H or S or Lin
1430
1431         if ((!y.info(info_flags::numeric)) || (!y.info(info_flags::positive))) {
1432                 return G(x_, s_, y).hold();
1433         }
1434         lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst{x_};
1435         lst s = is_a<lst>(s_) ? ex_to<lst>(s_) : lst{s_};
1436         if (x.nops() != s.nops()) {
1437                 return G(x_, s_, y).hold();
1438         }
1439         if (x.nops() == 0) {
1440                 return _ex1;
1441         }
1442         if (x.op(0) == y) {
1443                 return G(x_, s_, y).hold();
1444         }
1445         std::vector<int> sn;
1446         sn.reserve(s.nops());
1447         bool all_zero = true;
1448         bool crational = true;
1449         for (auto itx = x.begin(), its = s.begin(); itx != x.end(); ++itx, ++its) {
1450                 if (!(*itx).info(info_flags::numeric)) {
1451                         return G(x_, s_, y).hold();
1452                 }
1453                 if (!(*its).info(info_flags::real)) {
1454                         return G(x_, s_, y).hold();
1455                 }
1456                 if (!(*itx).info(info_flags::crational)) {
1457                         crational = false;
1458                 }
1459                 if (*itx != _ex0) {
1460                         all_zero = false;
1461                 }
1462                 if ( ex_to<numeric>(*itx).is_real() ) {
1463                         if ( ex_to<numeric>(*itx).is_positive() ) {
1464                                 if ( *its >= 0 ) {
1465                                         sn.push_back(1);
1466                                 }
1467                                 else {
1468                                         sn.push_back(-1);
1469                                 }
1470                         } else {
1471                                 sn.push_back(1);
1472                         }
1473                 }
1474                 else {
1475                         if ( ex_to<numeric>(*itx).imag() > 0 ) {
1476                                 sn.push_back(1);
1477                         }
1478                         else {
1479                                 sn.push_back(-1);
1480                         }
1481                 }
1482         }
1483         if (all_zero) {
1484                 return pow(log(y), x.nops()) / factorial(x.nops());
1485         }
1486         if (!y.info(info_flags::crational)) {
1487                 crational = false;
1488         }
1489         if (crational) {
1490                 return G(x_, s_, y).hold();
1491         }
1492         std::vector<cln::cl_N> xn;
1493         xn.reserve(x.nops());
1494         for (const auto & it : x)
1495                 xn.push_back(ex_to<numeric>(it).to_cl_N());
1496         cln::cl_N result = G_numeric(xn, sn, ex_to<numeric>(y).to_cl_N());
1497         return numeric(result);
1498 }
1499
1500
1501 // option do_not_evalf_params() removed.
1502 // This is safe: in the code above it only matters if s_ > 0 or s_ < 0,
1503 // s_ is allowed to be of floating type.
1504 unsigned G3_SERIAL::serial = function::register_new(function_options("G", 3).
1505                                 evalf_func(G3_evalf).
1506                                 eval_func(G3_eval).
1507                                 overloaded(2));
1508 //TODO
1509 //                                derivative_func(G3_deriv).
1510 //                                print_func<print_latex>(G3_print_latex).
1511
1512
1513 //////////////////////////////////////////////////////////////////////
1514 //
1515 // Classical polylogarithm and multiple polylogarithm  Li(m,x)
1516 //
1517 // GiNaC function
1518 //
1519 //////////////////////////////////////////////////////////////////////
1520
1521
1522 static ex Li_evalf(const ex& m_, const ex& x_)
1523 {
1524         // classical polylogs
1525         if (m_.info(info_flags::posint)) {
1526                 if (x_.info(info_flags::numeric)) {
1527                         int m__ = ex_to<numeric>(m_).to_int();
1528                         const cln::cl_N x__ = ex_to<numeric>(x_).to_cl_N();
1529                         const cln::cl_N result = Lin_numeric(m__, x__);
1530                         return numeric(result);
1531                 } else {
1532                         // try to numerically evaluate second argument
1533                         ex x_val = x_.evalf();
1534                         if (x_val.info(info_flags::numeric)) {
1535                                 int m__ = ex_to<numeric>(m_).to_int();
1536                                 const cln::cl_N x__ = ex_to<numeric>(x_val).to_cl_N();
1537                                 const cln::cl_N result = Lin_numeric(m__, x__);
1538                                 return numeric(result);
1539                         }
1540                 }
1541         }
1542         // multiple polylogs
1543         if (is_a<lst>(m_) && is_a<lst>(x_)) {
1544
1545                 const lst& m = ex_to<lst>(m_);
1546                 const lst& x = ex_to<lst>(x_);
1547                 if (m.nops() != x.nops()) {
1548                         return Li(m_,x_).hold();
1549                 }
1550                 if (x.nops() == 0) {
1551                         return _ex1;
1552                 }
1553                 if ((m.op(0) == _ex1) && (x.op(0) == _ex1)) {
1554                         return Li(m_,x_).hold();
1555                 }
1556
1557                 for (auto itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1558                         if (!(*itm).info(info_flags::posint)) {
1559                                 return Li(m_, x_).hold();
1560                         }
1561                         if (!(*itx).info(info_flags::numeric)) {
1562                                 return Li(m_, x_).hold();
1563                         }
1564                         if (*itx == _ex0) {
1565                                 return _ex0;
1566                         }
1567                 }
1568
1569                 return mLi_numeric(m, x);
1570         }
1571
1572         return Li(m_,x_).hold();
1573 }
1574
1575
1576 static ex Li_eval(const ex& m_, const ex& x_)
1577 {
1578         if (is_a<lst>(m_)) {
1579                 if (is_a<lst>(x_)) {
1580                         // multiple polylogs
1581                         const lst& m = ex_to<lst>(m_);
1582                         const lst& x = ex_to<lst>(x_);
1583                         if (m.nops() != x.nops()) {
1584                                 return Li(m_,x_).hold();
1585                         }
1586                         if (x.nops() == 0) {
1587                                 return _ex1;
1588                         }
1589                         bool is_H = true;
1590                         bool is_zeta = true;
1591                         bool do_evalf = true;
1592                         bool crational = true;
1593                         for (auto itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1594                                 if (!(*itm).info(info_flags::posint)) {
1595                                         return Li(m_,x_).hold();
1596                                 }
1597                                 if ((*itx != _ex1) && (*itx != _ex_1)) {
1598                                         if (itx != x.begin()) {
1599                                                 is_H = false;
1600                                         }
1601                                         is_zeta = false;
1602                                 }
1603                                 if (*itx == _ex0) {
1604                                         return _ex0;
1605                                 }
1606                                 if (!(*itx).info(info_flags::numeric)) {
1607                                         do_evalf = false;
1608                                 }
1609                                 if (!(*itx).info(info_flags::crational)) {
1610                                         crational = false;
1611                                 }
1612                         }
1613                         if (is_zeta) {
1614                                 lst newx;
1615                                 for (const auto & itx : x) {
1616                                         GINAC_ASSERT((itx == _ex1) || (itx == _ex_1));
1617                                         // XXX: 1 + 0.0*I is considered equal to 1. However
1618                                         // the former is a not automatically converted
1619                                         // to a real number. Do the conversion explicitly
1620                                         // to avoid the "numeric::operator>(): complex inequality"
1621                                         // exception (and similar problems).
1622                                         newx.append(itx != _ex_1 ? _ex1 : _ex_1);
1623                                 }
1624                                 return zeta(m_, newx);
1625                         }
1626                         if (is_H) {
1627                                 ex prefactor;
1628                                 lst newm = convert_parameter_Li_to_H(m, x, prefactor);
1629                                 return prefactor * H(newm, x[0]);
1630                         }
1631                         if (do_evalf && !crational) {
1632                                 return mLi_numeric(m,x);
1633                         }
1634                 }
1635                 return Li(m_, x_).hold();
1636         } else if (is_a<lst>(x_)) {
1637                 return Li(m_, x_).hold();
1638         }
1639
1640         // classical polylogs
1641         if (x_ == _ex0) {
1642                 return _ex0;
1643         }
1644         if (x_ == _ex1) {
1645                 return zeta(m_);
1646         }
1647         if (x_ == _ex_1) {
1648                 return (pow(2,1-m_)-1) * zeta(m_);
1649         }
1650         if (m_ == _ex1) {
1651                 return -log(1-x_);
1652         }
1653         if (m_ == _ex2) {
1654                 if (x_.is_equal(I)) {
1655                         return power(Pi,_ex2)/_ex_48 + Catalan*I;
1656                 }
1657                 if (x_.is_equal(-I)) {
1658                         return power(Pi,_ex2)/_ex_48 - Catalan*I;
1659                 }
1660         }
1661         if (m_.info(info_flags::posint) && x_.info(info_flags::numeric) && !x_.info(info_flags::crational)) {
1662                 int m__ = ex_to<numeric>(m_).to_int();
1663                 const cln::cl_N x__ = ex_to<numeric>(x_).to_cl_N();
1664                 const cln::cl_N result = Lin_numeric(m__, x__);
1665                 return numeric(result);
1666         }
1667
1668         return Li(m_, x_).hold();
1669 }
1670
1671
1672 static ex Li_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
1673 {
1674         if (is_a<lst>(m) || is_a<lst>(x)) {
1675                 // multiple polylog
1676                 epvector seq { expair(Li(m, x), 0) };
1677                 return pseries(rel, std::move(seq));
1678         }
1679         
1680         // classical polylog
1681         const ex x_pt = x.subs(rel, subs_options::no_pattern);
1682         if (m.info(info_flags::numeric) && x_pt.info(info_flags::numeric)) {
1683                 // First special case: x==0 (derivatives have poles)
1684                 if (x_pt.is_zero()) {
1685                         const symbol s;
1686                         ex ser;
1687                         // manually construct the primitive expansion
1688                         for (int i=1; i<order; ++i)
1689                                 ser += pow(s,i) / pow(numeric(i), m);
1690                         // substitute the argument's series expansion
1691                         ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
1692                         // maybe that was terminating, so add a proper order term
1693                         epvector nseq { expair(Order(_ex1), order) };
1694                         ser += pseries(rel, std::move(nseq));
1695                         // reexpanding it will collapse the series again
1696                         return ser.series(rel, order);
1697                 }
1698                 // TODO special cases: x==1 (branch point) and x real, >=1 (branch cut)
1699                 throw std::runtime_error("Li_series: don't know how to do the series expansion at this point!");
1700         }
1701         // all other cases should be safe, by now:
1702         throw do_taylor();  // caught by function::series()
1703 }
1704
1705
1706 static ex Li_deriv(const ex& m_, const ex& x_, unsigned deriv_param)
1707 {
1708         GINAC_ASSERT(deriv_param < 2);
1709         if (deriv_param == 0) {
1710                 return _ex0;
1711         }
1712         if (m_.nops() > 1) {
1713                 throw std::runtime_error("don't know how to derivate multiple polylogarithm!");
1714         }
1715         ex m;
1716         if (is_a<lst>(m_)) {
1717                 m = m_.op(0);
1718         } else {
1719                 m = m_;
1720         }
1721         ex x;
1722         if (is_a<lst>(x_)) {
1723                 x = x_.op(0);
1724         } else {
1725                 x = x_;
1726         }
1727         if (m > 0) {
1728                 return Li(m-1, x) / x;
1729         } else {
1730                 return 1/(1-x);
1731         }
1732 }
1733
1734
1735 static void Li_print_latex(const ex& m_, const ex& x_, const print_context& c)
1736 {
1737         lst m;
1738         if (is_a<lst>(m_)) {
1739                 m = ex_to<lst>(m_);
1740         } else {
1741                 m = lst{m_};
1742         }
1743         lst x;
1744         if (is_a<lst>(x_)) {
1745                 x = ex_to<lst>(x_);
1746         } else {
1747                 x = lst{x_};
1748         }
1749         c.s << "\\mathrm{Li}_{";
1750         auto itm = m.begin();
1751         (*itm).print(c);
1752         itm++;
1753         for (; itm != m.end(); itm++) {
1754                 c.s << ",";
1755                 (*itm).print(c);
1756         }
1757         c.s << "}(";
1758         auto itx = x.begin();
1759         (*itx).print(c);
1760         itx++;
1761         for (; itx != x.end(); itx++) {
1762                 c.s << ",";
1763                 (*itx).print(c);
1764         }
1765         c.s << ")";
1766 }
1767
1768
1769 REGISTER_FUNCTION(Li,
1770                   evalf_func(Li_evalf).
1771                   eval_func(Li_eval).
1772                   series_func(Li_series).
1773                   derivative_func(Li_deriv).
1774                   print_func<print_latex>(Li_print_latex).
1775                   do_not_evalf_params());
1776
1777
1778 //////////////////////////////////////////////////////////////////////
1779 //
1780 // Nielsen's generalized polylogarithm  S(n,p,x)
1781 //
1782 // helper functions
1783 //
1784 //////////////////////////////////////////////////////////////////////
1785
1786
1787 // anonymous namespace for helper functions
1788 namespace {
1789
1790
1791 // lookup table for special Euler-Zagier-Sums (used for S_n,p(x))
1792 // see fill_Yn()
1793 std::vector<std::vector<cln::cl_N>> Yn;
1794 int ynsize = 0; // number of Yn[]
1795 int ynlength = 100; // initial length of all Yn[i]
1796
1797
1798 // This function calculates the Y_n. The Y_n are needed for the evaluation of S_{n,p}(x).
1799 // The Y_n are basically Euler-Zagier sums with all m_i=1. They are subsums in the Z-sum
1800 // representing S_{n,p}(x).
1801 // The first index in Y_n corresponds to the parameter p minus one, i.e. the depth of the
1802 // equivalent Z-sum.
1803 // The second index in Y_n corresponds to the running index of the outermost sum in the full Z-sum
1804 // representing S_{n,p}(x).
1805 // The calculation of Y_n uses the values from Y_{n-1}.
1806 void fill_Yn(int n, const cln::float_format_t& prec)
1807 {
1808         const int initsize = ynlength;
1809         //const int initsize = initsize_Yn;
1810         cln::cl_N one = cln::cl_float(1, prec);
1811
1812         if (n) {
1813                 std::vector<cln::cl_N> buf(initsize);
1814                 auto it = buf.begin();
1815                 auto itprev = Yn[n-1].begin();
1816                 *it = (*itprev) / cln::cl_N(n+1) * one;
1817                 it++;
1818                 itprev++;
1819                 // sums with an index smaller than the depth are zero and need not to be calculated.
1820                 // calculation starts with depth, which is n+2)
1821                 for (int i=n+2; i<=initsize+n; i++) {
1822                         *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
1823                         it++;
1824                         itprev++;
1825                 }
1826                 Yn.push_back(buf);
1827         } else {
1828                 std::vector<cln::cl_N> buf(initsize);
1829                 auto it = buf.begin();
1830                 *it = 1 * one;
1831                 it++;
1832                 for (int i=2; i<=initsize; i++) {
1833                         *it = *(it-1) + 1 / cln::cl_N(i) * one;
1834                         it++;
1835                 }
1836                 Yn.push_back(buf);
1837         }
1838         ynsize++;
1839 }
1840
1841
1842 // make Yn longer ... 
1843 void make_Yn_longer(int newsize, const cln::float_format_t& prec)
1844 {
1845
1846         cln::cl_N one = cln::cl_float(1, prec);
1847
1848         Yn[0].resize(newsize);
1849         auto it = Yn[0].begin();
1850         it += ynlength;
1851         for (int i=ynlength+1; i<=newsize; i++) {
1852                 *it = *(it-1) + 1 / cln::cl_N(i) * one;
1853                 it++;
1854         }
1855
1856         for (int n=1; n<ynsize; n++) {
1857                 Yn[n].resize(newsize);
1858                 auto it = Yn[n].begin();
1859                 auto itprev = Yn[n-1].begin();
1860                 it += ynlength;
1861                 itprev += ynlength;
1862                 for (int i=ynlength+n+1; i<=newsize+n; i++) {
1863                         *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
1864                         it++;
1865                         itprev++;
1866                 }
1867         }
1868         
1869         ynlength = newsize;
1870 }
1871
1872
1873 // helper function for S(n,p,x)
1874 // [Kol] (7.2)
1875 cln::cl_N C(int n, int p)
1876 {
1877         cln::cl_N result;
1878
1879         for (int k=0; k<p; k++) {
1880                 for (int j=0; j<=(n+k-1)/2; j++) {
1881                         if (k == 0) {
1882                                 if (n & 1) {
1883                                         if (j & 1) {
1884                                                 result = result - 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1) / cln::factorial(2*j);
1885                                         }
1886                                         else {
1887                                                 result = result + 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1) / cln::factorial(2*j);
1888                                         }
1889                                 }
1890                         }
1891                         else {
1892                                 if (k & 1) {
1893                                         if (j & 1) {
1894                                                 result = result + cln::factorial(n+k-1)
1895                                                                   * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1896                                                                   / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1897                                         }
1898                                         else {
1899                                                 result = result - cln::factorial(n+k-1)
1900                                                                   * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1901                                                                   / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1902                                         }
1903                                 }
1904                                 else {
1905                                         if (j & 1) {
1906                                                 result = result - cln::factorial(n+k-1) * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1907                                                                   / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1908                                         }
1909                                         else {
1910                                                 result = result + cln::factorial(n+k-1)
1911                                                                   * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1912                                                                   / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1913                                         }
1914                                 }
1915                         }
1916                 }
1917         }
1918         int np = n+p;
1919         if ((np-1) & 1) {
1920                 if (((np)/2+n) & 1) {
1921                         result = -result - cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
1922                 }
1923                 else {
1924                         result = -result + cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
1925                 }
1926         }
1927
1928         return result;
1929 }
1930
1931
1932 // helper function for S(n,p,x)
1933 // [Kol] remark to (9.1)
1934 cln::cl_N a_k(int k)
1935 {
1936         cln::cl_N result;
1937
1938         if (k == 0) {
1939                 return 1;
1940         }
1941
1942         result = result;
1943         for (int m=2; m<=k; m++) {
1944                 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * a_k(k-m);
1945         }
1946
1947         return -result / k;
1948 }
1949
1950
1951 // helper function for S(n,p,x)
1952 // [Kol] remark to (9.1)
1953 cln::cl_N b_k(int k)
1954 {
1955         cln::cl_N result;
1956
1957         if (k == 0) {
1958                 return 1;
1959         }
1960
1961         result = result;
1962         for (int m=2; m<=k; m++) {
1963                 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * b_k(k-m);
1964         }
1965
1966         return result / k;
1967 }
1968
1969
1970 // helper function for S(n,p,x)
1971 cln::cl_N S_do_sum(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
1972 {
1973         static cln::float_format_t oldprec = cln::default_float_format;
1974
1975         if (p==1) {
1976                 return Li_projection(n+1, x, prec);
1977         }
1978
1979         // precision has changed, we need to clear lookup table Yn
1980         if ( oldprec != prec ) {
1981                 Yn.clear();
1982                 ynsize = 0;
1983                 ynlength = 100;
1984                 oldprec = prec;
1985         }
1986                 
1987         // check if precalculated values are sufficient
1988         if (p > ynsize+1) {
1989                 for (int i=ynsize; i<p-1; i++) {
1990                         fill_Yn(i, prec);
1991                 }
1992         }
1993
1994         // should be done otherwise
1995         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
1996         cln::cl_N xf = x * one;
1997         //cln::cl_N xf = x * cln::cl_float(1, prec);
1998
1999         cln::cl_N res;
2000         cln::cl_N resbuf;
2001         cln::cl_N factor = cln::expt(xf, p);
2002         int i = p;
2003         do {
2004                 resbuf = res;
2005                 if (i-p >= ynlength) {
2006                         // make Yn longer
2007                         make_Yn_longer(ynlength*2, prec);
2008                 }
2009                 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? ...
2010                 //res = res + factor / cln::expt(cln::cl_I(i),n+1) * (*it); // should we check it? or rely on magic number? ...
2011                 factor = factor * xf;
2012                 i++;
2013         } while (res != resbuf);
2014         
2015         return res;
2016 }
2017
2018
2019 // helper function for S(n,p,x)
2020 cln::cl_N S_projection(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
2021 {
2022         // [Kol] (5.3)
2023         if (cln::abs(cln::realpart(x)) > cln::cl_F("0.5")) {
2024
2025                 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(x),n)
2026                                    * cln::expt(cln::log(1-x),p) / cln::factorial(n) / cln::factorial(p);
2027
2028                 for (int s=0; s<n; s++) {
2029                         cln::cl_N res2;
2030                         for (int r=0; r<p; r++) {
2031                                 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-x),r)
2032                                               * S_do_sum(p-r,n-s,1-x,prec) / cln::factorial(r);
2033                         }
2034                         result = result + cln::expt(cln::log(x),s) * (S_num(n-s,p,1) - res2) / cln::factorial(s);
2035                 }
2036
2037                 return result;
2038         }
2039         
2040         return S_do_sum(n, p, x, prec);
2041 }
2042
2043
2044 // helper function for S(n,p,x)
2045 const cln::cl_N S_num(int n, int p, const cln::cl_N& x)
2046 {
2047         if (x == 1) {
2048                 if (n == 1) {
2049                     // [Kol] (2.22) with (2.21)
2050                         return cln::zeta(p+1);
2051                 }
2052
2053                 if (p == 1) {
2054                     // [Kol] (2.22)
2055                         return cln::zeta(n+1);
2056                 }
2057
2058                 // [Kol] (9.1)
2059                 cln::cl_N result;
2060                 for (int nu=0; nu<n; nu++) {
2061                         for (int rho=0; rho<=p; rho++) {
2062                                 result = result + b_k(n-nu-1) * b_k(p-rho) * a_k(nu+rho+1)
2063                                                   * cln::factorial(nu+rho+1) / cln::factorial(rho) / cln::factorial(nu+1);
2064                         }
2065                 }
2066                 result = result * cln::expt(cln::cl_I(-1),n+p-1);
2067
2068                 return result;
2069         }
2070         else if (x == -1) {
2071                 // [Kol] (2.22)
2072                 if (p == 1) {
2073                         return -(1-cln::expt(cln::cl_I(2),-n)) * cln::zeta(n+1);
2074                 }
2075 //              throw std::runtime_error("don't know how to evaluate this function!");
2076         }
2077
2078         // what is the desired float format?
2079         // first guess: default format
2080         cln::float_format_t prec = cln::default_float_format;
2081         const cln::cl_N value = x;
2082         // second guess: the argument's format
2083         if (!instanceof(realpart(value), cln::cl_RA_ring))
2084                 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
2085         else if (!instanceof(imagpart(value), cln::cl_RA_ring))
2086                 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
2087
2088         // [Kol] (5.3)
2089         // the condition abs(1-value)>1 avoids an infinite recursion in the region abs(value)<=1 && abs(value)>0.95 && abs(1-value)<=1 && abs(1-value)>0.95
2090         // we don't care here about abs(value)<1 && real(value)>0.5, this will be taken care of in S_projection
2091         if ((cln::realpart(value) < -0.5) || (n == 0) || ((cln::abs(value) <= 1) && (cln::abs(value) > 0.95) && (cln::abs(1-value) > 1) )) {
2092
2093                 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(value),n)
2094                                    * cln::expt(cln::log(1-value),p) / cln::factorial(n) / cln::factorial(p);
2095
2096                 for (int s=0; s<n; s++) {
2097                         cln::cl_N res2;
2098                         for (int r=0; r<p; r++) {
2099                                 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-value),r)
2100                                               * S_num(p-r,n-s,1-value) / cln::factorial(r);
2101                         }
2102                         result = result + cln::expt(cln::log(value),s) * (S_num(n-s,p,1) - res2) / cln::factorial(s);
2103                 }
2104
2105                 return result;
2106                 
2107         }
2108         // [Kol] (5.12)
2109         if (cln::abs(value) > 1) {
2110                 
2111                 cln::cl_N result;
2112
2113                 for (int s=0; s<p; s++) {
2114                         for (int r=0; r<=s; r++) {
2115                                 result = result + cln::expt(cln::cl_I(-1),s) * cln::expt(cln::log(-value),r) * cln::factorial(n+s-r-1)
2116                                                   / cln::factorial(r) / cln::factorial(s-r) / cln::factorial(n-1)
2117                                                   * S_num(n+s-r,p-s,cln::recip(value));
2118                         }
2119                 }
2120                 result = result * cln::expt(cln::cl_I(-1),n);
2121
2122                 cln::cl_N res2;
2123                 for (int r=0; r<n; r++) {
2124                         res2 = res2 + cln::expt(cln::log(-value),r) * C(n-r,p) / cln::factorial(r);
2125                 }
2126                 res2 = res2 + cln::expt(cln::log(-value),n+p) / cln::factorial(n+p);
2127
2128                 result = result + cln::expt(cln::cl_I(-1),p) * res2;
2129
2130                 return result;
2131         }
2132
2133         if ((cln::abs(value) > 0.95) && (cln::abs(value-9.53) < 9.47)) {
2134                 lst m;
2135                 m.append(n+1);
2136                 for (int s=0; s<p-1; s++)
2137                         m.append(1);
2138
2139                 ex res = H(m,numeric(value)).evalf();
2140                 return ex_to<numeric>(res).to_cl_N();
2141         }
2142         else {
2143                 return S_projection(n, p, value, prec);
2144         }
2145 }
2146
2147
2148 } // end of anonymous namespace
2149
2150
2151 //////////////////////////////////////////////////////////////////////
2152 //
2153 // Nielsen's generalized polylogarithm  S(n,p,x)
2154 //
2155 // GiNaC function
2156 //
2157 //////////////////////////////////////////////////////////////////////
2158
2159
2160 static ex S_evalf(const ex& n, const ex& p, const ex& x)
2161 {
2162         if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
2163                 const int n_ = ex_to<numeric>(n).to_int();
2164                 const int p_ = ex_to<numeric>(p).to_int();
2165                 if (is_a<numeric>(x)) {
2166                         const cln::cl_N x_ = ex_to<numeric>(x).to_cl_N();
2167                         const cln::cl_N result = S_num(n_, p_, x_);
2168                         return numeric(result);
2169                 } else {
2170                         ex x_val = x.evalf();
2171                         if (is_a<numeric>(x_val)) {
2172                                 const cln::cl_N x_val_ = ex_to<numeric>(x_val).to_cl_N();
2173                                 const cln::cl_N result = S_num(n_, p_, x_val_);
2174                                 return numeric(result);
2175                         }
2176                 }
2177         }
2178         return S(n, p, x).hold();
2179 }
2180
2181
2182 static ex S_eval(const ex& n, const ex& p, const ex& x)
2183 {
2184         if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
2185                 if (x == 0) {
2186                         return _ex0;
2187                 }
2188                 if (x == 1) {
2189                         lst m{n+1};
2190                         for (int i=ex_to<numeric>(p).to_int()-1; i>0; i--) {
2191                                 m.append(1);
2192                         }
2193                         return zeta(m);
2194                 }
2195                 if (p == 1) {
2196                         return Li(n+1, x);
2197                 }
2198                 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
2199                         int n_ = ex_to<numeric>(n).to_int();
2200                         int p_ = ex_to<numeric>(p).to_int();
2201                         const cln::cl_N x_ = ex_to<numeric>(x).to_cl_N();
2202                         const cln::cl_N result = S_num(n_, p_, x_);
2203                         return numeric(result);
2204                 }
2205         }
2206         if (n.is_zero()) {
2207                 // [Kol] (5.3)
2208                 return pow(-log(1-x), p) / factorial(p);
2209         }
2210         return S(n, p, x).hold();
2211 }
2212
2213
2214 static ex S_series(const ex& n, const ex& p, const ex& x, const relational& rel, int order, unsigned options)
2215 {
2216         if (p == _ex1) {
2217                 return Li(n+1, x).series(rel, order, options);
2218         }
2219
2220         const ex x_pt = x.subs(rel, subs_options::no_pattern);
2221         if (n.info(info_flags::posint) && p.info(info_flags::posint) && x_pt.info(info_flags::numeric)) {
2222                 // First special case: x==0 (derivatives have poles)
2223                 if (x_pt.is_zero()) {
2224                         const symbol s;
2225                         ex ser;
2226                         // manually construct the primitive expansion
2227                         // subsum = Euler-Zagier-Sum is needed
2228                         // dirty hack (slow ...) calculation of subsum:
2229                         std::vector<ex> presubsum, subsum;
2230                         subsum.push_back(0);
2231                         for (int i=1; i<order-1; ++i) {
2232                                 subsum.push_back(subsum[i-1] + numeric(1, i));
2233                         }
2234                         for (int depth=2; depth<p; ++depth) {
2235                                 presubsum = subsum;
2236                                 for (int i=1; i<order-1; ++i) {
2237                                         subsum[i] = subsum[i-1] + numeric(1, i) * presubsum[i-1];
2238                                 }
2239                         }
2240                                 
2241                         for (int i=1; i<order; ++i) {
2242                                 ser += pow(s,i) / pow(numeric(i), n+1) * subsum[i-1];
2243                         }
2244                         // substitute the argument's series expansion
2245                         ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
2246                         // maybe that was terminating, so add a proper order term
2247                         epvector nseq { expair(Order(_ex1), order) };
2248                         ser += pseries(rel, std::move(nseq));
2249                         // reexpanding it will collapse the series again
2250                         return ser.series(rel, order);
2251                 }
2252                 // TODO special cases: x==1 (branch point) and x real, >=1 (branch cut)
2253                 throw std::runtime_error("S_series: don't know how to do the series expansion at this point!");
2254         }
2255         // all other cases should be safe, by now:
2256         throw do_taylor();  // caught by function::series()
2257 }
2258
2259
2260 static ex S_deriv(const ex& n, const ex& p, const ex& x, unsigned deriv_param)
2261 {
2262         GINAC_ASSERT(deriv_param < 3);
2263         if (deriv_param < 2) {
2264                 return _ex0;
2265         }
2266         if (n > 0) {
2267                 return S(n-1, p, x) / x;
2268         } else {
2269                 return S(n, p-1, x) / (1-x);
2270         }
2271 }
2272
2273
2274 static void S_print_latex(const ex& n, const ex& p, const ex& x, const print_context& c)
2275 {
2276         c.s << "\\mathrm{S}_{";
2277         n.print(c);
2278         c.s << ",";
2279         p.print(c);
2280         c.s << "}(";
2281         x.print(c);
2282         c.s << ")";
2283 }
2284
2285
2286 REGISTER_FUNCTION(S,
2287                   evalf_func(S_evalf).
2288                   eval_func(S_eval).
2289                   series_func(S_series).
2290                   derivative_func(S_deriv).
2291                   print_func<print_latex>(S_print_latex).
2292                   do_not_evalf_params());
2293
2294
2295 //////////////////////////////////////////////////////////////////////
2296 //
2297 // Harmonic polylogarithm  H(m,x)
2298 //
2299 // helper functions
2300 //
2301 //////////////////////////////////////////////////////////////////////
2302
2303
2304 // anonymous namespace for helper functions
2305 namespace {
2306
2307
2308 // regulates the pole (used by 1/x-transformation)
2309 symbol H_polesign("IMSIGN");
2310
2311
2312 // convert parameters from H to Li representation
2313 // parameters are expected to be in expanded form, i.e. only 0, 1 and -1
2314 // returns true if some parameters are negative
2315 bool convert_parameter_H_to_Li(const lst& l, lst& m, lst& s, ex& pf)
2316 {
2317         // expand parameter list
2318         lst mexp;
2319         for (const auto & it : l) {
2320                 if (it > 1) {
2321                         for (ex count=it-1; count > 0; count--) {
2322                                 mexp.append(0);
2323                         }
2324                         mexp.append(1);
2325                 } else if (it < -1) {
2326                         for (ex count=it+1; count < 0; count++) {
2327                                 mexp.append(0);
2328                         }
2329                         mexp.append(-1);
2330                 } else {
2331                         mexp.append(it);
2332                 }
2333         }
2334         
2335         ex signum = 1;
2336         pf = 1;
2337         bool has_negative_parameters = false;
2338         ex acc = 1;
2339         for (const auto & it : mexp) {
2340                 if (it == 0) {
2341                         acc++;
2342                         continue;
2343                 }
2344                 if (it > 0) {
2345                         m.append((it+acc-1) * signum);
2346                 } else {
2347                         m.append((it-acc+1) * signum);
2348                 }
2349                 acc = 1;
2350                 signum = it;
2351                 pf *= it;
2352                 if (pf < 0) {
2353                         has_negative_parameters = true;
2354                 }
2355         }
2356         if (has_negative_parameters) {
2357                 for (std::size_t i=0; i<m.nops(); i++) {
2358                         if (m.op(i) < 0) {
2359                                 m.let_op(i) = -m.op(i);
2360                                 s.append(-1);
2361                         } else {
2362                                 s.append(1);
2363                         }
2364                 }
2365         }
2366         
2367         return has_negative_parameters;
2368 }
2369
2370
2371 // recursivly transforms H to corresponding multiple polylogarithms
2372 struct map_trafo_H_convert_to_Li : public map_function
2373 {
2374         ex operator()(const ex& e) override
2375         {
2376                 if (is_a<add>(e) || is_a<mul>(e)) {
2377                         return e.map(*this);
2378                 }
2379                 if (is_a<function>(e)) {
2380                         std::string name = ex_to<function>(e).get_name();
2381                         if (name == "H") {
2382                                 lst parameter;
2383                                 if (is_a<lst>(e.op(0))) {
2384                                         parameter = ex_to<lst>(e.op(0));
2385                                 } else {
2386                                         parameter = lst{e.op(0)};
2387                                 }
2388                                 ex arg = e.op(1);
2389
2390                                 lst m;
2391                                 lst s;
2392                                 ex pf;
2393                                 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
2394                                         s.let_op(0) = s.op(0) * arg;
2395                                         return pf * Li(m, s).hold();
2396                                 } else {
2397                                         for (std::size_t i=0; i<m.nops(); i++) {
2398                                                 s.append(1);
2399                                         }
2400                                         s.let_op(0) = s.op(0) * arg;
2401                                         return Li(m, s).hold();
2402                                 }
2403                         }
2404                 }
2405                 return e;
2406         }
2407 };
2408
2409
2410 // recursivly transforms H to corresponding zetas
2411 struct map_trafo_H_convert_to_zeta : public map_function
2412 {
2413         ex operator()(const ex& e) override
2414         {
2415                 if (is_a<add>(e) || is_a<mul>(e)) {
2416                         return e.map(*this);
2417                 }
2418                 if (is_a<function>(e)) {
2419                         std::string name = ex_to<function>(e).get_name();
2420                         if (name == "H") {
2421                                 lst parameter;
2422                                 if (is_a<lst>(e.op(0))) {
2423                                         parameter = ex_to<lst>(e.op(0));
2424                                 } else {
2425                                         parameter = lst{e.op(0)};
2426                                 }
2427
2428                                 lst m;
2429                                 lst s;
2430                                 ex pf;
2431                                 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
2432                                         return pf * zeta(m, s);
2433                                 } else {
2434                                         return zeta(m);
2435                                 }
2436                         }
2437                 }
2438                 return e;
2439         }
2440 };
2441
2442
2443 // remove trailing zeros from H-parameters
2444 struct map_trafo_H_reduce_trailing_zeros : public map_function
2445 {
2446         ex operator()(const ex& e) override
2447         {
2448                 if (is_a<add>(e) || is_a<mul>(e)) {
2449                         return e.map(*this);
2450                 }
2451                 if (is_a<function>(e)) {
2452                         std::string name = ex_to<function>(e).get_name();
2453                         if (name == "H") {
2454                                 lst parameter;
2455                                 if (is_a<lst>(e.op(0))) {
2456                                         parameter = ex_to<lst>(e.op(0));
2457                                 } else {
2458                                         parameter = lst{e.op(0)};
2459                                 }
2460                                 ex arg = e.op(1);
2461                                 if (parameter.op(parameter.nops()-1) == 0) {
2462                                         
2463                                         //
2464                                         if (parameter.nops() == 1) {
2465                                                 return log(arg);
2466                                         }
2467                                         
2468                                         //
2469                                         auto it = parameter.begin();
2470                                         while ((it != parameter.end()) && (*it == 0)) {
2471                                                 it++;
2472                                         }
2473                                         if (it == parameter.end()) {
2474                                                 return pow(log(arg),parameter.nops()) / factorial(parameter.nops());
2475                                         }
2476                                         
2477                                         //
2478                                         parameter.remove_last();
2479                                         std::size_t lastentry = parameter.nops();
2480                                         while ((lastentry > 0) && (parameter[lastentry-1] == 0)) {
2481                                                 lastentry--;
2482                                         }
2483                                         
2484                                         //
2485                                         ex result = log(arg) * H(parameter,arg).hold();
2486                                         ex acc = 0;
2487                                         for (ex i=0; i<lastentry; i++) {
2488                                                 if (parameter[i] > 0) {
2489                                                         parameter[i]++;
2490                                                         result -= (acc + parameter[i]-1) * H(parameter, arg).hold();
2491                                                         parameter[i]--;
2492                                                         acc = 0;
2493                                                 } else if (parameter[i] < 0) {
2494                                                         parameter[i]--;
2495                                                         result -= (acc + abs(parameter[i]+1)) * H(parameter, arg).hold();
2496                                                         parameter[i]++;
2497                                                         acc = 0;
2498                                                 } else {
2499                                                         acc++;
2500                                                 }
2501                                         }
2502                                         
2503                                         if (lastentry < parameter.nops()) {
2504                                                 result = result / (parameter.nops()-lastentry+1);
2505                                                 return result.map(*this);
2506                                         } else {
2507                                                 return result;
2508                                         }
2509                                 }
2510                         }
2511                 }
2512                 return e;
2513         }
2514 };
2515
2516
2517 // returns an expression with zeta functions corresponding to the parameter list for H
2518 ex convert_H_to_zeta(const lst& m)
2519 {
2520         symbol xtemp("xtemp");
2521         map_trafo_H_reduce_trailing_zeros filter;
2522         map_trafo_H_convert_to_zeta filter2;
2523         return filter2(filter(H(m, xtemp).hold())).subs(xtemp == 1);
2524 }
2525
2526
2527 // convert signs form Li to H representation
2528 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf)
2529 {
2530         lst res;
2531         auto itm = m.begin();
2532         auto itx = ++x.begin();
2533         int signum = 1;
2534         pf = _ex1;
2535         res.append(*itm);
2536         itm++;
2537         while (itx != x.end()) {
2538                 GINAC_ASSERT((*itx == _ex1) || (*itx == _ex_1));
2539                 // XXX: 1 + 0.0*I is considered equal to 1. However the former
2540                 // is not automatically converted to a real number.
2541                 // Do the conversion explicitly to avoid the
2542                 // "numeric::operator>(): complex inequality" exception.
2543                 signum *= (*itx != _ex_1) ? 1 : -1;
2544                 pf *= signum;
2545                 res.append((*itm) * signum);
2546                 itm++;
2547                 itx++;
2548         }
2549         return res;
2550 }
2551
2552
2553 // multiplies an one-dimensional H with another H
2554 // [ReV] (18)
2555 ex trafo_H_mult(const ex& h1, const ex& h2)
2556 {
2557         ex res;
2558         ex hshort;
2559         lst hlong;
2560         ex h1nops = h1.op(0).nops();
2561         ex h2nops = h2.op(0).nops();
2562         if (h1nops > 1) {
2563                 hshort = h2.op(0).op(0);
2564                 hlong = ex_to<lst>(h1.op(0));
2565         } else {
2566                 hshort = h1.op(0).op(0);
2567                 if (h2nops > 1) {
2568                         hlong = ex_to<lst>(h2.op(0));
2569                 } else {
2570                         hlong = lst{h2.op(0).op(0)};
2571                 }
2572         }
2573         for (std::size_t i=0; i<=hlong.nops(); i++) {
2574                 lst newparameter;
2575                 std::size_t j=0;
2576                 for (; j<i; j++) {
2577                         newparameter.append(hlong[j]);
2578                 }
2579                 newparameter.append(hshort);
2580                 for (; j<hlong.nops(); j++) {
2581                         newparameter.append(hlong[j]);
2582                 }
2583                 res += H(newparameter, h1.op(1)).hold();
2584         }
2585         return res;
2586 }
2587
2588
2589 // applies trafo_H_mult recursively on expressions
2590 struct map_trafo_H_mult : public map_function
2591 {
2592         ex operator()(const ex& e) override
2593         {
2594                 if (is_a<add>(e)) {
2595                         return e.map(*this);
2596                 }
2597
2598                 if (is_a<mul>(e)) {
2599
2600                         ex result = 1;
2601                         ex firstH;
2602                         lst Hlst;
2603                         for (std::size_t pos=0; pos<e.nops(); pos++) {
2604                                 if (is_a<power>(e.op(pos)) && is_a<function>(e.op(pos).op(0))) {
2605                                         std::string name = ex_to<function>(e.op(pos).op(0)).get_name();
2606                                         if (name == "H") {
2607                                                 for (ex i=0; i<e.op(pos).op(1); i++) {
2608                                                         Hlst.append(e.op(pos).op(0));
2609                                                 }
2610                                                 continue;
2611                                         }
2612                                 } else if (is_a<function>(e.op(pos))) {
2613                                         std::string name = ex_to<function>(e.op(pos)).get_name();
2614                                         if (name == "H") {
2615                                                 if (e.op(pos).op(0).nops() > 1) {
2616                                                         firstH = e.op(pos);
2617                                                 } else {
2618                                                         Hlst.append(e.op(pos));
2619                                                 }
2620                                                 continue;
2621                                         }
2622                                 }
2623                                 result *= e.op(pos);
2624                         }
2625                         if (firstH == 0) {
2626                                 if (Hlst.nops() > 0) {
2627                                         firstH = Hlst[Hlst.nops()-1];
2628                                         Hlst.remove_last();
2629                                 } else {
2630                                         return e;
2631                                 }
2632                         }
2633
2634                         if (Hlst.nops() > 0) {
2635                                 ex buffer = trafo_H_mult(firstH, Hlst.op(0));
2636                                 result *= buffer;
2637                                 for (std::size_t i=1; i<Hlst.nops(); i++) {
2638                                         result *= Hlst.op(i);
2639                                 }
2640                                 result = result.expand();
2641                                 map_trafo_H_mult recursion;
2642                                 return recursion(result);
2643                         } else {
2644                                 return e;
2645                         }
2646
2647                 }
2648                 return e;
2649         }
2650 };
2651
2652
2653 // do integration [ReV] (55)
2654 // put parameter 0 in front of existing parameters
2655 ex trafo_H_1tx_prepend_zero(const ex& e, const ex& arg)
2656 {
2657         ex h;
2658         std::string name;
2659         if (is_a<function>(e)) {
2660                 name = ex_to<function>(e).get_name();
2661         }
2662         if (name == "H") {
2663                 h = e;
2664         } else {
2665                 for (std::size_t i=0; i<e.nops(); i++) {
2666                         if (is_a<function>(e.op(i))) {
2667                                 std::string name = ex_to<function>(e.op(i)).get_name();
2668                                 if (name == "H") {
2669                                         h = e.op(i);
2670                                 }
2671                         }
2672                 }
2673         }
2674         if (h != 0) {
2675                 lst newparameter = ex_to<lst>(h.op(0));
2676                 newparameter.prepend(0);
2677                 ex addzeta = convert_H_to_zeta(newparameter);
2678                 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
2679         } else {
2680                 return e * (-H(lst{ex(0)},1/arg).hold());
2681         }
2682 }
2683
2684
2685 // do integration [ReV] (49)
2686 // put parameter 1 in front of existing parameters
2687 ex trafo_H_prepend_one(const ex& e, const ex& arg)
2688 {
2689         ex h;
2690         std::string name;
2691         if (is_a<function>(e)) {
2692                 name = ex_to<function>(e).get_name();
2693         }
2694         if (name == "H") {
2695                 h = e;
2696         } else {
2697                 for (std::size_t i=0; i<e.nops(); i++) {
2698                         if (is_a<function>(e.op(i))) {
2699                                 std::string name = ex_to<function>(e.op(i)).get_name();
2700                                 if (name == "H") {
2701                                         h = e.op(i);
2702                                 }
2703                         }
2704                 }
2705         }
2706         if (h != 0) {
2707                 lst newparameter = ex_to<lst>(h.op(0));
2708                 newparameter.prepend(1);
2709                 return e.subs(h == H(newparameter, h.op(1)).hold());
2710         } else {
2711                 return e * H(lst{ex(1)},1-arg).hold();
2712         }
2713 }
2714
2715
2716 // do integration [ReV] (55)
2717 // put parameter -1 in front of existing parameters
2718 ex trafo_H_1tx_prepend_minusone(const ex& e, const ex& arg)
2719 {
2720         ex h;
2721         std::string name;
2722         if (is_a<function>(e)) {
2723                 name = ex_to<function>(e).get_name();
2724         }
2725         if (name == "H") {
2726                 h = e;
2727         } else {
2728                 for (std::size_t i=0; i<e.nops(); i++) {
2729                         if (is_a<function>(e.op(i))) {
2730                                 std::string name = ex_to<function>(e.op(i)).get_name();
2731                                 if (name == "H") {
2732                                         h = e.op(i);
2733                                 }
2734                         }
2735                 }
2736         }
2737         if (h != 0) {
2738                 lst newparameter = ex_to<lst>(h.op(0));
2739                 newparameter.prepend(-1);
2740                 ex addzeta = convert_H_to_zeta(newparameter);
2741                 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
2742         } else {
2743                 ex addzeta = convert_H_to_zeta(lst{ex(-1)});
2744                 return (e * (addzeta - H(lst{ex(-1)},1/arg).hold())).expand();
2745         }
2746 }
2747
2748
2749 // do integration [ReV] (55)
2750 // put parameter -1 in front of existing parameters
2751 ex trafo_H_1mxt1px_prepend_minusone(const ex& e, const ex& arg)
2752 {
2753         ex h;
2754         std::string name;
2755         if (is_a<function>(e)) {
2756                 name = ex_to<function>(e).get_name();
2757         }
2758         if (name == "H") {
2759                 h = e;
2760         } else {
2761                 for (std::size_t i = 0; i < e.nops(); i++) {
2762                         if (is_a<function>(e.op(i))) {
2763                                 std::string name = ex_to<function>(e.op(i)).get_name();
2764                                 if (name == "H") {
2765                                         h = e.op(i);
2766                                 }
2767                         }
2768                 }
2769         }
2770         if (h != 0) {
2771                 lst newparameter = ex_to<lst>(h.op(0));
2772                 newparameter.prepend(-1);
2773                 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
2774         } else {
2775                 return (e * H(lst{ex(-1)},(1-arg)/(1+arg)).hold()).expand();
2776         }
2777 }
2778
2779
2780 // do integration [ReV] (55)
2781 // put parameter 1 in front of existing parameters
2782 ex trafo_H_1mxt1px_prepend_one(const ex& e, const ex& arg)
2783 {
2784         ex h;
2785         std::string name;
2786         if (is_a<function>(e)) {
2787                 name = ex_to<function>(e).get_name();
2788         }
2789         if (name == "H") {
2790                 h = e;
2791         } else {
2792                 for (std::size_t i = 0; i < e.nops(); i++) {
2793                         if (is_a<function>(e.op(i))) {
2794                                 std::string name = ex_to<function>(e.op(i)).get_name();
2795                                 if (name == "H") {
2796                                         h = e.op(i);
2797                                 }
2798                         }
2799                 }
2800         }
2801         if (h != 0) {
2802                 lst newparameter = ex_to<lst>(h.op(0));
2803                 newparameter.prepend(1);
2804                 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
2805         } else {
2806                 return (e * H(lst{ex(1)},(1-arg)/(1+arg)).hold()).expand();
2807         }
2808 }
2809
2810
2811 // do x -> 1-x transformation
2812 struct map_trafo_H_1mx : public map_function
2813 {
2814         ex operator()(const ex& e) override
2815         {
2816                 if (is_a<add>(e) || is_a<mul>(e)) {
2817                         return e.map(*this);
2818                 }
2819                 
2820                 if (is_a<function>(e)) {
2821                         std::string name = ex_to<function>(e).get_name();
2822                         if (name == "H") {
2823
2824                                 lst parameter = ex_to<lst>(e.op(0));
2825                                 ex arg = e.op(1);
2826
2827                                 // special cases if all parameters are either 0, 1 or -1
2828                                 bool allthesame = true;
2829                                 if (parameter.op(0) == 0) {
2830                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
2831                                                 if (parameter.op(i) != 0) {
2832                                                         allthesame = false;
2833                                                         break;
2834                                                 }
2835                                         }
2836                                         if (allthesame) {
2837                                                 lst newparameter;
2838                                                 for (int i=parameter.nops(); i>0; i--) {
2839                                                         newparameter.append(1);
2840                                                 }
2841                                                 return pow(-1, parameter.nops()) * H(newparameter, 1-arg).hold();
2842                                         }
2843                                 } else if (parameter.op(0) == -1) {
2844                                         throw std::runtime_error("map_trafo_H_1mx: cannot handle weights equal -1!");
2845                                 } else {
2846                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
2847                                                 if (parameter.op(i) != 1) {
2848                                                         allthesame = false;
2849                                                         break;
2850                                                 }
2851                                         }
2852                                         if (allthesame) {
2853                                                 lst newparameter;
2854                                                 for (int i=parameter.nops(); i>0; i--) {
2855                                                         newparameter.append(0);
2856                                                 }
2857                                                 return pow(-1, parameter.nops()) * H(newparameter, 1-arg).hold();
2858                                         }
2859                                 }
2860
2861                                 lst newparameter = parameter;
2862                                 newparameter.remove_first();
2863
2864                                 if (parameter.op(0) == 0) {
2865
2866                                         // leading zero
2867                                         ex res = convert_H_to_zeta(parameter);
2868                                         //ex res = convert_from_RV(parameter, 1).subs(H(wild(1),wild(2))==zeta(wild(1)));
2869                                         map_trafo_H_1mx recursion;
2870                                         ex buffer = recursion(H(newparameter, arg).hold());
2871                                         if (is_a<add>(buffer)) {
2872                                                 for (std::size_t i = 0; i < buffer.nops(); i++) {
2873                                                         res -= trafo_H_prepend_one(buffer.op(i), arg);
2874                                                 }
2875                                         } else {
2876                                                 res -= trafo_H_prepend_one(buffer, arg);
2877                                         }
2878                                         return res;
2879
2880                                 } else {
2881
2882                                         // leading one
2883                                         map_trafo_H_1mx recursion;
2884                                         map_trafo_H_mult unify;
2885                                         ex res = H(lst{ex(1)}, arg).hold() * H(newparameter, arg).hold();
2886                                         std::size_t firstzero = 0;
2887                                         while (parameter.op(firstzero) == 1) {
2888                                                 firstzero++;
2889                                         }
2890                                         for (std::size_t i = firstzero-1; i < parameter.nops()-1; i++) {
2891                                                 lst newparameter;
2892                                                 std::size_t j=0;
2893                                                 for (; j<=i; j++) {
2894                                                         newparameter.append(parameter[j+1]);
2895                                                 }
2896                                                 newparameter.append(1);
2897                                                 for (; j<parameter.nops()-1; j++) {
2898                                                         newparameter.append(parameter[j+1]);
2899                                                 }
2900                                                 res -= H(newparameter, arg).hold();
2901                                         }
2902                                         res = recursion(res).expand() / firstzero;
2903                                         return unify(res);
2904                                 }
2905                         }
2906                 }
2907                 return e;
2908         }
2909 };
2910
2911
2912 // do x -> 1/x transformation
2913 struct map_trafo_H_1overx : public map_function
2914 {
2915         ex operator()(const ex& e) override
2916         {
2917                 if (is_a<add>(e) || is_a<mul>(e)) {
2918                         return e.map(*this);
2919                 }
2920
2921                 if (is_a<function>(e)) {
2922                         std::string name = ex_to<function>(e).get_name();
2923                         if (name == "H") {
2924
2925                                 lst parameter = ex_to<lst>(e.op(0));
2926                                 ex arg = e.op(1);
2927
2928                                 // special cases if all parameters are either 0, 1 or -1
2929                                 bool allthesame = true;
2930                                 if (parameter.op(0) == 0) {
2931                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
2932                                                 if (parameter.op(i) != 0) {
2933                                                         allthesame = false;
2934                                                         break;
2935                                                 }
2936                                         }
2937                                         if (allthesame) {
2938                                                 return pow(-1, parameter.nops()) * H(parameter, 1/arg).hold();
2939                                         }
2940                                 } else if (parameter.op(0) == -1) {
2941                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
2942                                                 if (parameter.op(i) != -1) {
2943                                                         allthesame = false;
2944                                                         break;
2945                                                 }
2946                                         }
2947                                         if (allthesame) {
2948                                                 map_trafo_H_mult unify;
2949                                                 return unify((pow(H(lst{ex(-1)},1/arg).hold() - H(lst{ex(0)},1/arg).hold(), parameter.nops())
2950                                                        / factorial(parameter.nops())).expand());
2951                                         }
2952                                 } else {
2953                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
2954                                                 if (parameter.op(i) != 1) {
2955                                                         allthesame = false;
2956                                                         break;
2957                                                 }
2958                                         }
2959                                         if (allthesame) {
2960                                                 map_trafo_H_mult unify;
2961                                                 return unify((pow(H(lst{ex(1)},1/arg).hold() + H(lst{ex(0)},1/arg).hold() + H_polesign, parameter.nops())
2962                                                        / factorial(parameter.nops())).expand());
2963                                         }
2964                                 }
2965
2966                                 lst newparameter = parameter;
2967                                 newparameter.remove_first();
2968
2969                                 if (parameter.op(0) == 0) {
2970                                         
2971                                         // leading zero
2972                                         ex res = convert_H_to_zeta(parameter);
2973                                         map_trafo_H_1overx recursion;
2974                                         ex buffer = recursion(H(newparameter, arg).hold());
2975                                         if (is_a<add>(buffer)) {
2976                                                 for (std::size_t i = 0; i < buffer.nops(); i++) {
2977                                                         res += trafo_H_1tx_prepend_zero(buffer.op(i), arg);
2978                                                 }
2979                                         } else {
2980                                                 res += trafo_H_1tx_prepend_zero(buffer, arg);
2981                                         }
2982                                         return res;
2983
2984                                 } else if (parameter.op(0) == -1) {
2985
2986                                         // leading negative one
2987                                         ex res = convert_H_to_zeta(parameter);
2988                                         map_trafo_H_1overx recursion;
2989                                         ex buffer = recursion(H(newparameter, arg).hold());
2990                                         if (is_a<add>(buffer)) {
2991                                                 for (std::size_t i = 0; i < buffer.nops(); i++) {
2992                                                         res += trafo_H_1tx_prepend_zero(buffer.op(i), arg) - trafo_H_1tx_prepend_minusone(buffer.op(i), arg);
2993                                                 }
2994                                         } else {
2995                                                 res += trafo_H_1tx_prepend_zero(buffer, arg) - trafo_H_1tx_prepend_minusone(buffer, arg);
2996                                         }
2997                                         return res;
2998
2999                                 } else {
3000
3001                                         // leading one
3002                                         map_trafo_H_1overx recursion;
3003                                         map_trafo_H_mult unify;
3004                                         ex res = H(lst{ex(1)}, arg).hold() * H(newparameter, arg).hold();
3005                                         std::size_t firstzero = 0;
3006                                         while (parameter.op(firstzero) == 1) {
3007                                                 firstzero++;
3008                                         }
3009                                         for (std::size_t i = firstzero-1; i < parameter.nops() - 1; i++) {
3010                                                 lst newparameter;
3011                                                 std::size_t j = 0;
3012                                                 for (; j<=i; j++) {
3013                                                         newparameter.append(parameter[j+1]);
3014                                                 }
3015                                                 newparameter.append(1);
3016                                                 for (; j<parameter.nops()-1; j++) {
3017                                                         newparameter.append(parameter[j+1]);
3018                                                 }
3019                                                 res -= H(newparameter, arg).hold();
3020                                         }
3021                                         res = recursion(res).expand() / firstzero;
3022                                         return unify(res);
3023
3024                                 }
3025
3026                         }
3027                 }
3028                 return e;
3029         }
3030 };
3031
3032
3033 // do x -> (1-x)/(1+x) transformation
3034 struct map_trafo_H_1mxt1px : public map_function
3035 {
3036         ex operator()(const ex& e) override
3037         {
3038                 if (is_a<add>(e) || is_a<mul>(e)) {
3039                         return e.map(*this);
3040                 }
3041
3042                 if (is_a<function>(e)) {
3043                         std::string name = ex_to<function>(e).get_name();
3044                         if (name == "H") {
3045
3046                                 lst parameter = ex_to<lst>(e.op(0));
3047                                 ex arg = e.op(1);
3048
3049                                 // special cases if all parameters are either 0, 1 or -1
3050                                 bool allthesame = true;
3051                                 if (parameter.op(0) == 0) {
3052                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
3053                                                 if (parameter.op(i) != 0) {
3054                                                         allthesame = false;
3055                                                         break;
3056                                                 }
3057                                         }
3058                                         if (allthesame) {
3059                                                 map_trafo_H_mult unify;
3060                                                 return unify((pow(-H(lst{ex(1)},(1-arg)/(1+arg)).hold() - H(lst{ex(-1)},(1-arg)/(1+arg)).hold(), parameter.nops())
3061                                                        / factorial(parameter.nops())).expand());
3062                                         }
3063                                 } else if (parameter.op(0) == -1) {
3064                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
3065                                                 if (parameter.op(i) != -1) {
3066                                                         allthesame = false;
3067                                                         break;
3068                                                 }
3069                                         }
3070                                         if (allthesame) {
3071                                                 map_trafo_H_mult unify;
3072                                                 return unify((pow(log(2) - H(lst{ex(-1)},(1-arg)/(1+arg)).hold(), parameter.nops())
3073                                                        / factorial(parameter.nops())).expand());
3074                                         }
3075                                 } else {
3076                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
3077                                                 if (parameter.op(i) != 1) {
3078                                                         allthesame = false;
3079                                                         break;
3080                                                 }
3081                                         }
3082                                         if (allthesame) {
3083                                                 map_trafo_H_mult unify;
3084                                                 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())
3085                                                        / factorial(parameter.nops())).expand());
3086                                         }
3087                                 }
3088
3089                                 lst newparameter = parameter;
3090                                 newparameter.remove_first();
3091
3092                                 if (parameter.op(0) == 0) {
3093
3094                                         // leading zero
3095                                         ex res = convert_H_to_zeta(parameter);
3096                                         map_trafo_H_1mxt1px recursion;
3097                                         ex buffer = recursion(H(newparameter, arg).hold());
3098                                         if (is_a<add>(buffer)) {
3099                                                 for (std::size_t i = 0; i < buffer.nops(); i++) {
3100                                                         res -= trafo_H_1mxt1px_prepend_one(buffer.op(i), arg) + trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
3101                                                 }
3102                                         } else {
3103                                                 res -= trafo_H_1mxt1px_prepend_one(buffer, arg) + trafo_H_1mxt1px_prepend_minusone(buffer, arg);
3104                                         }
3105                                         return res;
3106
3107                                 } else if (parameter.op(0) == -1) {
3108
3109                                         // leading negative one
3110                                         ex res = convert_H_to_zeta(parameter);
3111                                         map_trafo_H_1mxt1px recursion;
3112                                         ex buffer = recursion(H(newparameter, arg).hold());
3113                                         if (is_a<add>(buffer)) {
3114                                                 for (std::size_t i = 0; i < buffer.nops(); i++) {
3115                                                         res -= trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
3116                                                 }
3117                                         } else {
3118                                                 res -= trafo_H_1mxt1px_prepend_minusone(buffer, arg);
3119                                         }
3120                                         return res;
3121
3122                                 } else {
3123
3124                                         // leading one
3125                                         map_trafo_H_1mxt1px recursion;
3126                                         map_trafo_H_mult unify;
3127                                         ex res = H(lst{ex(1)}, arg).hold() * H(newparameter, arg).hold();
3128                                         std::size_t firstzero = 0;
3129                                         while (parameter.op(firstzero) == 1) {
3130                                                 firstzero++;
3131                                         }
3132                                         for (std::size_t i = firstzero - 1; i < parameter.nops() - 1; i++) {
3133                                                 lst newparameter;
3134                                                 std::size_t j=0;
3135                                                 for (; j<=i; j++) {
3136                                                         newparameter.append(parameter[j+1]);
3137                                                 }
3138                                                 newparameter.append(1);
3139                                                 for (; j<parameter.nops()-1; j++) {
3140                                                         newparameter.append(parameter[j+1]);
3141                                                 }
3142                                                 res -= H(newparameter, arg).hold();
3143                                         }
3144                                         res = recursion(res).expand() / firstzero;
3145                                         return unify(res);
3146
3147                                 }
3148
3149                         }
3150                 }
3151                 return e;
3152         }
3153 };
3154
3155
3156 // do the actual summation.
3157 cln::cl_N H_do_sum(const std::vector<int>& m, const cln::cl_N& x)
3158 {
3159         const int j = m.size();
3160
3161         std::vector<cln::cl_N> t(j);
3162
3163         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3164         cln::cl_N factor = cln::expt(x, j) * one;
3165         cln::cl_N t0buf;
3166         int q = 0;
3167         do {
3168                 t0buf = t[0];
3169                 q++;
3170                 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),m[j-1]);
3171                 for (int k=j-2; k>=1; k--) {
3172                         t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), m[k]);
3173                 }
3174                 t[0] = t[0] + t[1] * factor / cln::expt(cln::cl_I(q+j-1), m[0]);
3175                 factor = factor * x;
3176         } while (t[0] != t0buf);
3177
3178         return t[0];
3179 }
3180
3181
3182 } // end of anonymous namespace
3183
3184
3185 //////////////////////////////////////////////////////////////////////
3186 //
3187 // Harmonic polylogarithm  H(m,x)
3188 //
3189 // GiNaC function
3190 //
3191 //////////////////////////////////////////////////////////////////////
3192
3193
3194 static ex H_evalf(const ex& x1, const ex& x2)
3195 {
3196         if (is_a<lst>(x1)) {
3197                 
3198                 cln::cl_N x;
3199                 if (is_a<numeric>(x2)) {
3200                         x = ex_to<numeric>(x2).to_cl_N();
3201                 } else {
3202                         ex x2_val = x2.evalf();
3203                         if (is_a<numeric>(x2_val)) {
3204                                 x = ex_to<numeric>(x2_val).to_cl_N();
3205                         }
3206                 }
3207
3208                 for (std::size_t i = 0; i < x1.nops(); i++) {
3209                         if (!x1.op(i).info(info_flags::integer)) {
3210                                 return H(x1, x2).hold();
3211                         }
3212                 }
3213                 if (x1.nops() < 1) {
3214                         return H(x1, x2).hold();
3215                 }
3216
3217                 const lst& morg = ex_to<lst>(x1);
3218                 // remove trailing zeros ...
3219                 if (*(--morg.end()) == 0) {
3220                         symbol xtemp("xtemp");
3221                         map_trafo_H_reduce_trailing_zeros filter;
3222                         return filter(H(x1, xtemp).hold()).subs(xtemp==x2).evalf();
3223                 }
3224                 // ... and expand parameter notation
3225                 bool has_minus_one = false;
3226                 lst m;
3227                 for (const auto & it : morg) {
3228                         if (it > 1) {
3229                                 for (ex count=it-1; count > 0; count--) {
3230                                         m.append(0);
3231                                 }
3232                                 m.append(1);
3233                         } else if (it <= -1) {
3234                                 for (ex count=it+1; count < 0; count++) {
3235                                         m.append(0);
3236                                 }
3237                                 m.append(-1);
3238                                 has_minus_one = true;
3239                         } else {
3240                                 m.append(it);
3241                         }
3242                 }
3243
3244                 // do summation
3245                 if (cln::abs(x) < 0.95) {
3246                         lst m_lst;
3247                         lst s_lst;
3248                         ex pf;
3249                         if (convert_parameter_H_to_Li(m, m_lst, s_lst, pf)) {
3250                                 // negative parameters -> s_lst is filled
3251                                 std::vector<int> m_int;
3252                                 std::vector<cln::cl_N> x_cln;
3253                                 for (auto it_int = m_lst.begin(), it_cln = s_lst.begin();
3254                                      it_int != m_lst.end(); it_int++, it_cln++) {
3255                                         m_int.push_back(ex_to<numeric>(*it_int).to_int());
3256                                         x_cln.push_back(ex_to<numeric>(*it_cln).to_cl_N());
3257                                 }
3258                                 x_cln.front() = x_cln.front() * x;
3259                                 return pf * numeric(multipleLi_do_sum(m_int, x_cln));
3260                         } else {
3261                                 // only positive parameters
3262                                 //TODO
3263                                 if (m_lst.nops() == 1) {
3264                                         return Li(m_lst.op(0), x2).evalf();
3265                                 }
3266                                 std::vector<int> m_int;
3267                                 for (const auto & it : m_lst) {
3268                                         m_int.push_back(ex_to<numeric>(it).to_int());
3269                                 }
3270                                 return numeric(H_do_sum(m_int, x));
3271                         }
3272                 }
3273
3274                 symbol xtemp("xtemp");
3275                 ex res = 1;     
3276                 
3277                 // ensure that the realpart of the argument is positive
3278                 if (cln::realpart(x) < 0) {
3279                         x = -x;
3280                         for (std::size_t i = 0; i < m.nops(); i++) {
3281                                 if (m.op(i) != 0) {
3282                                         m.let_op(i) = -m.op(i);
3283                                         res *= -1;
3284                                 }
3285                         }
3286                 }
3287
3288                 // x -> 1/x
3289                 if (cln::abs(x) >= 2.0) {
3290                         map_trafo_H_1overx trafo;
3291                         res *= trafo(H(m, xtemp).hold());
3292                         if (cln::imagpart(x) <= 0) {
3293                                 res = res.subs(H_polesign == -I*Pi);
3294                         } else {
3295                                 res = res.subs(H_polesign == I*Pi);
3296                         }
3297                         return res.subs(xtemp == numeric(x)).evalf();
3298                 }
3299                 
3300                 // check transformations for 0.95 <= |x| < 2.0
3301                 
3302                 // |(1-x)/(1+x)| < 0.9 -> circular area with center=9.53+0i and radius=9.47
3303                 if (cln::abs(x-9.53) <= 9.47) {
3304                         // x -> (1-x)/(1+x)
3305                         map_trafo_H_1mxt1px trafo;
3306                         res *= trafo(H(m, xtemp).hold());
3307                 } else {
3308                         // x -> 1-x
3309                         if (has_minus_one) {
3310                                 map_trafo_H_convert_to_Li filter;
3311                                 return filter(H(m, numeric(x)).hold()).evalf();
3312                         }
3313                         map_trafo_H_1mx trafo;
3314                         res *= trafo(H(m, xtemp).hold());
3315                 }
3316
3317                 return res.subs(xtemp == numeric(x)).evalf();
3318         }
3319
3320         return H(x1,x2).hold();
3321 }
3322
3323
3324 static ex H_eval(const ex& m_, const ex& x)
3325 {
3326         lst m;
3327         if (is_a<lst>(m_)) {
3328                 m = ex_to<lst>(m_);
3329         } else {
3330                 m = lst{m_};
3331         }
3332         if (m.nops() == 0) {
3333                 return _ex1;
3334         }
3335         ex pos1;
3336         ex pos2;
3337         ex n;
3338         ex p;
3339         int step = 0;
3340         if (*m.begin() > _ex1) {
3341                 step++;
3342                 pos1 = _ex0;
3343                 pos2 = _ex1;
3344                 n = *m.begin()-1;
3345                 p = _ex1;
3346         } else if (*m.begin() < _ex_1) {
3347                 step++;
3348                 pos1 = _ex0;
3349                 pos2 = _ex_1;
3350                 n = -*m.begin()-1;
3351                 p = _ex1;
3352         } else if (*m.begin() == _ex0) {
3353                 pos1 = _ex0;
3354                 n = _ex1;
3355         } else {
3356                 pos1 = *m.begin();
3357                 p = _ex1;
3358         }
3359         for (auto it = ++m.begin(); it != m.end(); it++) {
3360                 if (it->info(info_flags::integer)) {
3361                         if (step == 0) {
3362                                 if (*it > _ex1) {
3363                                         if (pos1 == _ex0) {
3364                                                 step = 1;
3365                                                 pos2 = _ex1;
3366                                                 n += *it-1;
3367                                                 p = _ex1;
3368                                         } else {
3369                                                 step = 2;
3370                                         }
3371                                 } else if (*it < _ex_1) {
3372                                         if (pos1 == _ex0) {
3373                                                 step = 1;
3374                                                 pos2 = _ex_1;
3375                                                 n += -*it-1;
3376                                                 p = _ex1;
3377                                         } else {
3378                                                 step = 2;
3379                                         }
3380                                 } else {
3381                                         if (*it != pos1) {
3382                                                 step = 1;
3383                                                 pos2 = *it;
3384                                         }
3385                                         if (*it == _ex0) {
3386                                                 n++;
3387                                         } else {
3388                                                 p++;
3389                                         }
3390                                 }
3391                         } else if (step == 1) {
3392                                 if (*it != pos2) {
3393                                         step = 2;
3394                                 } else {
3395                                         if (*it == _ex0) {
3396                                                 n++;
3397                                         } else {
3398                                                 p++;
3399                                         }
3400                                 }
3401                         }
3402                 } else {
3403                         // if some m_i is not an integer
3404                         return H(m_, x).hold();
3405                 }
3406         }
3407         if ((x == _ex1) && (*(--m.end()) != _ex0)) {
3408                 return convert_H_to_zeta(m);
3409         }
3410         if (step == 0) {
3411                 if (pos1 == _ex0) {
3412                         // all zero
3413                         if (x == _ex0) {
3414                                 return H(m_, x).hold();
3415                         }
3416                         return pow(log(x), m.nops()) / factorial(m.nops());
3417                 } else {
3418                         // all (minus) one
3419                         return pow(-pos1*log(1-pos1*x), m.nops()) / factorial(m.nops());
3420                 }
3421         } else if ((step == 1) && (pos1 == _ex0)){
3422                 // convertible to S
3423                 if (pos2 == _ex1) {
3424                         return S(n, p, x);
3425                 } else {
3426                         return pow(-1, p) * S(n, p, -x);
3427                 }
3428         }
3429         if (x == _ex0) {
3430                 return _ex0;
3431         }
3432         if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
3433                 return H(m_, x).evalf();
3434         }
3435         return H(m_, x).hold();
3436 }
3437
3438
3439 static ex H_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
3440 {
3441         epvector seq { expair(H(m, x), 0) };
3442         return pseries(rel, std::move(seq));
3443 }
3444
3445
3446 static ex H_deriv(const ex& m_, const ex& x, unsigned deriv_param)
3447 {
3448         GINAC_ASSERT(deriv_param < 2);
3449         if (deriv_param == 0) {
3450                 return _ex0;
3451         }
3452         lst m;
3453         if (is_a<lst>(m_)) {
3454                 m = ex_to<lst>(m_);
3455         } else {
3456                 m = lst{m_};
3457         }
3458         ex mb = *m.begin();
3459         if (mb > _ex1) {
3460                 m[0]--;
3461                 return H(m, x) / x;
3462         }
3463         if (mb < _ex_1) {
3464                 m[0]++;
3465                 return H(m, x) / x;
3466         }
3467         m.remove_first();
3468         if (mb == _ex1) {
3469                 return 1/(1-x) * H(m, x);
3470         } else if (mb == _ex_1) {
3471                 return 1/(1+x) * H(m, x);
3472         } else {
3473                 return H(m, x) / x;
3474         }
3475 }
3476
3477
3478 static void H_print_latex(const ex& m_, const ex& x, const print_context& c)
3479 {
3480         lst m;
3481         if (is_a<lst>(m_)) {
3482                 m = ex_to<lst>(m_);
3483         } else {
3484                 m = lst{m_};
3485         }
3486         c.s << "\\mathrm{H}_{";
3487         auto itm = m.begin();
3488         (*itm).print(c);
3489         itm++;
3490         for (; itm != m.end(); itm++) {
3491                 c.s << ",";
3492                 (*itm).print(c);
3493         }
3494         c.s << "}(";
3495         x.print(c);
3496         c.s << ")";
3497 }
3498
3499
3500 REGISTER_FUNCTION(H,
3501                   evalf_func(H_evalf).
3502                   eval_func(H_eval).
3503                   series_func(H_series).
3504                   derivative_func(H_deriv).
3505                   print_func<print_latex>(H_print_latex).
3506                   do_not_evalf_params());
3507
3508
3509 // takes a parameter list for H and returns an expression with corresponding multiple polylogarithms
3510 ex convert_H_to_Li(const ex& m, const ex& x)
3511 {
3512         map_trafo_H_reduce_trailing_zeros filter;
3513         map_trafo_H_convert_to_Li filter2;
3514         if (is_a<lst>(m)) {
3515                 return filter2(filter(H(m, x).hold()));
3516         } else {
3517                 return filter2(filter(H(lst{m}, x).hold()));
3518         }
3519 }
3520
3521
3522 //////////////////////////////////////////////////////////////////////
3523 //
3524 // Multiple zeta values  zeta(x) and zeta(x,s)
3525 //
3526 // helper functions
3527 //
3528 //////////////////////////////////////////////////////////////////////
3529
3530
3531 // anonymous namespace for helper functions
3532 namespace {
3533
3534
3535 // parameters and data for [Cra] algorithm
3536 const cln::cl_N lambda = cln::cl_N("319/320");
3537
3538 void halfcyclic_convolute(const std::vector<cln::cl_N>& a, const std::vector<cln::cl_N>& b, std::vector<cln::cl_N>& c)
3539 {
3540         const int size = a.size();
3541         for (int n=0; n<size; n++) {
3542                 c[n] = 0;
3543                 for (int m=0; m<=n; m++) {
3544                         c[n] = c[n] + a[m]*b[n-m];
3545                 }
3546         }
3547 }
3548
3549
3550 // [Cra] section 4
3551 static void initcX(std::vector<cln::cl_N>& crX,
3552                    const std::vector<int>& s,
3553                    const int L2)
3554 {
3555         std::vector<cln::cl_N> crB(L2 + 1);
3556         for (int i=0; i<=L2; i++)
3557                 crB[i] = bernoulli(i).to_cl_N() / cln::factorial(i);
3558
3559         int Sm = 0;
3560         int Smp1 = 0;
3561         std::vector<std::vector<cln::cl_N>> crG(s.size() - 1, std::vector<cln::cl_N>(L2 + 1));
3562         for (int m=0; m < (int)s.size() - 1; m++) {
3563                 Sm += s[m];
3564                 Smp1 = Sm + s[m+1];
3565                 for (int i = 0; i <= L2; i++)
3566                         crG[m][i] = cln::factorial(i + Sm - m - 2) / cln::factorial(i + Smp1 - m - 2);
3567         }
3568
3569         crX = crB;
3570
3571         for (std::size_t m = 0; m < s.size() - 1; m++) {
3572                 std::vector<cln::cl_N> Xbuf(L2 + 1);
3573                 for (int i = 0; i <= L2; i++)
3574                         Xbuf[i] = crX[i] * crG[m][i];
3575
3576                 halfcyclic_convolute(Xbuf, crB, crX);
3577         }
3578 }
3579
3580
3581 // [Cra] section 4
3582 static cln::cl_N crandall_Y_loop(const cln::cl_N& Sqk,
3583                                  const std::vector<cln::cl_N>& crX)
3584 {
3585         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3586         cln::cl_N factor = cln::expt(lambda, Sqk);
3587         cln::cl_N res = factor / Sqk * crX[0] * one;
3588         cln::cl_N resbuf;
3589         int N = 0;
3590         do {
3591                 resbuf = res;
3592                 factor = factor * lambda;
3593                 N++;
3594                 res = res + crX[N] * factor / (N+Sqk);
3595         } while ((res != resbuf) || cln::zerop(crX[N]));
3596         return res;
3597 }
3598
3599
3600 // [Cra] section 4
3601 static void calc_f(std::vector<std::vector<cln::cl_N>>& f_kj,
3602                    const int maxr, const int L1)
3603 {
3604         cln::cl_N t0, t1, t2, t3, t4;
3605         int i, j, k;
3606         auto it = f_kj.begin();
3607         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3608         
3609         t0 = cln::exp(-lambda);
3610         t2 = 1;
3611         for (k=1; k<=L1; k++) {
3612                 t1 = k * lambda;
3613                 t2 = t0 * t2;
3614                 for (j=1; j<=maxr; j++) {
3615                         t3 = 1;
3616                         t4 = 1;
3617                         for (i=2; i<=j; i++) {
3618                                 t4 = t4 * (j-i+1);
3619                                 t3 = t1 * t3 + t4;
3620                         }
3621                         (*it).push_back(t2 * t3 * cln::expt(cln::cl_I(k),-j) * one);
3622                 }
3623                 it++;
3624         }
3625 }
3626
3627
3628 // [Cra] (3.1)
3629 static cln::cl_N crandall_Z(const std::vector<int>& s,
3630                             const std::vector<std::vector<cln::cl_N>>& f_kj)
3631 {
3632         const int j = s.size();
3633
3634         if (j == 1) {   
3635                 cln::cl_N t0;
3636                 cln::cl_N t0buf;
3637                 int q = 0;
3638                 do {
3639                         t0buf = t0;
3640                         q++;
3641                         t0 = t0 + f_kj[q+j-2][s[0]-1];
3642                 } while (t0 != t0buf);
3643                 
3644                 return t0 / cln::factorial(s[0]-1);
3645         }
3646
3647         std::vector<cln::cl_N> t(j);
3648
3649         cln::cl_N t0buf;
3650         int q = 0;
3651         do {
3652                 t0buf = t[0];
3653                 q++;
3654                 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),s[j-1]);
3655                 for (int k=j-2; k>=1; k--) {
3656                         t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), s[k]);
3657                 }
3658                 t[0] = t[0] + t[1] * f_kj[q+j-2][s[0]-1];
3659         } while (t[0] != t0buf);
3660         
3661         return t[0] / cln::factorial(s[0]-1);
3662 }
3663
3664
3665 // [Cra] (2.4)
3666 cln::cl_N zeta_do_sum_Crandall(const std::vector<int>& s)
3667 {
3668         std::vector<int> r = s;
3669         const int j = r.size();
3670
3671         std::size_t L1;
3672
3673         // decide on maximal size of f_kj for crandall_Z
3674         if (Digits < 50) {
3675                 L1 = 150;
3676         } else {
3677                 L1 = Digits * 3 + j*2;
3678         }
3679
3680         std::size_t L2;
3681         // decide on maximal size of crX for crandall_Y
3682         if (Digits < 38) {
3683                 L2 = 63;
3684         } else if (Digits < 86) {
3685                 L2 = 127;
3686         } else if (Digits < 192) {
3687                 L2 = 255;
3688         } else if (Digits < 394) {
3689                 L2 = 511;
3690         } else if (Digits < 808) {
3691                 L2 = 1023;
3692         } else {
3693                 L2 = 2047;
3694         }
3695
3696         cln::cl_N res;
3697
3698         int maxr = 0;
3699         int S = 0;
3700         for (int i=0; i<j; i++) {
3701                 S += r[i];
3702                 if (r[i] > maxr) {
3703                         maxr = r[i];
3704                 }
3705         }
3706
3707         std::vector<std::vector<cln::cl_N>> f_kj(L1);
3708         calc_f(f_kj, maxr, L1);
3709
3710         const cln::cl_N r0factorial = cln::factorial(r[0]-1);
3711
3712         std::vector<int> rz;
3713         int skp1buf;
3714         int Srun = S;
3715         for (int k=r.size()-1; k>0; k--) {
3716
3717                 rz.insert(rz.begin(), r.back());
3718                 skp1buf = rz.front();
3719                 Srun -= skp1buf;
3720                 r.pop_back();
3721
3722                 std::vector<cln::cl_N> crX;
3723                 initcX(crX, r, L2);
3724                 
3725                 for (int q=0; q<skp1buf; q++) {
3726                         
3727                         cln::cl_N pp1 = crandall_Y_loop(Srun+q-k, crX);
3728                         cln::cl_N pp2 = crandall_Z(rz, f_kj);
3729
3730                         rz.front()--;
3731                         
3732                         if (q & 1) {
3733                                 res = res - pp1 * pp2 / cln::factorial(q);
3734                         } else {
3735                                 res = res + pp1 * pp2 / cln::factorial(q);
3736                         }
3737                 }
3738                 rz.front() = skp1buf;
3739         }
3740         rz.insert(rz.begin(), r.back());
3741
3742         std::vector<cln::cl_N> crX;
3743         initcX(crX, rz, L2);
3744
3745         res = (res + crandall_Y_loop(S-j, crX)) / r0factorial
3746                 + crandall_Z(rz, f_kj);
3747
3748         return res;
3749 }
3750
3751
3752 cln::cl_N zeta_do_sum_simple(const std::vector<int>& r)
3753 {
3754         const int j = r.size();
3755
3756         // buffer for subsums
3757         std::vector<cln::cl_N> t(j);
3758         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3759
3760         cln::cl_N t0buf;
3761         int q = 0;
3762         do {
3763                 t0buf = t[0];
3764                 q++;
3765                 t[j-1] = t[j-1] + one / cln::expt(cln::cl_I(q),r[j-1]);
3766                 for (int k=j-2; k>=0; k--) {
3767                         t[k] = t[k] + one * t[k+1] / cln::expt(cln::cl_I(q+j-1-k), r[k]);
3768                 }
3769         } while (t[0] != t0buf);
3770
3771         return t[0];
3772 }
3773
3774
3775 // does Hoelder convolution. see [BBB] (7.0)
3776 cln::cl_N zeta_do_Hoelder_convolution(const std::vector<int>& m_, const std::vector<int>& s_)
3777 {
3778         // prepare parameters
3779         // holds Li arguments in [BBB] notation
3780         std::vector<int> s = s_;
3781         std::vector<int> m_p = m_;
3782         std::vector<int> m_q;
3783         // holds Li arguments in nested sums notation
3784         std::vector<cln::cl_N> s_p(s.size(), cln::cl_N(1));
3785         s_p[0] = s_p[0] * cln::cl_N("1/2");
3786         // convert notations
3787         int sig = 1;
3788         for (std::size_t i = 0; i < s_.size(); i++) {
3789                 if (s_[i] < 0) {
3790                         sig = -sig;
3791                         s_p[i] = -s_p[i];
3792                 }
3793                 s[i] = sig * std::abs(s[i]);
3794         }
3795         std::vector<cln::cl_N> s_q;
3796         cln::cl_N signum = 1;
3797
3798         // first term
3799         cln::cl_N res = multipleLi_do_sum(m_p, s_p);
3800
3801         // middle terms
3802         do {
3803
3804                 // change parameters
3805                 if (s.front() > 0) {
3806                         if (m_p.front() == 1) {
3807                                 m_p.erase(m_p.begin());
3808                                 s_p.erase(s_p.begin());
3809                                 if (s_p.size() > 0) {
3810                                         s_p.front() = s_p.front() * cln::cl_N("1/2");
3811                                 }
3812                                 s.erase(s.begin());
3813                                 m_q.front()++;
3814                         } else {
3815                                 m_p.front()--;
3816                                 m_q.insert(m_q.begin(), 1);
3817                                 if (s_q.size() > 0) {
3818                                         s_q.front() = s_q.front() * 2;
3819                                 }
3820                                 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
3821                         }
3822                 } else {
3823                         if (m_p.front() == 1) {
3824                                 m_p.erase(m_p.begin());
3825                                 cln::cl_N spbuf = s_p.front();
3826                                 s_p.erase(s_p.begin());
3827                                 if (s_p.size() > 0) {
3828                                         s_p.front() = s_p.front() * spbuf;
3829                                 }
3830                                 s.erase(s.begin());
3831                                 m_q.insert(m_q.begin(), 1);
3832                                 if (s_q.size() > 0) {
3833                                         s_q.front() = s_q.front() * 4;
3834                                 }
3835                                 s_q.insert(s_q.begin(), cln::cl_N("1/4"));
3836                                 signum = -signum;
3837                         } else {
3838                                 m_p.front()--;
3839                                 m_q.insert(m_q.begin(), 1);
3840                                 if (s_q.size() > 0) {
3841                                         s_q.front() = s_q.front() * 2;
3842                                 }
3843                                 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
3844                         }
3845                 }
3846
3847                 // exiting the loop
3848                 if (m_p.size() == 0) break;
3849
3850                 res = res + signum * multipleLi_do_sum(m_p, s_p) * multipleLi_do_sum(m_q, s_q);
3851
3852         } while (true);
3853
3854         // last term
3855         res = res + signum * multipleLi_do_sum(m_q, s_q);
3856
3857         return res;
3858 }
3859
3860
3861 } // end of anonymous namespace
3862
3863
3864 //////////////////////////////////////////////////////////////////////
3865 //
3866 // Multiple zeta values  zeta(x)
3867 //
3868 // GiNaC function
3869 //
3870 //////////////////////////////////////////////////////////////////////
3871
3872
3873 static ex zeta1_evalf(const ex& x)
3874 {
3875         if (is_exactly_a<lst>(x) && (x.nops()>1)) {
3876
3877                 // multiple zeta value
3878                 const int count = x.nops();
3879                 const lst& xlst = ex_to<lst>(x);
3880                 std::vector<int> r(count);
3881
3882                 // check parameters and convert them
3883                 auto it1 = xlst.begin();
3884                 auto it2 = r.begin();
3885                 do {
3886                         if (!(*it1).info(info_flags::posint)) {
3887                                 return zeta(x).hold();
3888                         }
3889                         *it2 = ex_to<numeric>(*it1).to_int();
3890                         it1++;
3891                         it2++;
3892                 } while (it2 != r.end());
3893
3894                 // check for divergence
3895                 if (r[0] == 1) {
3896                         return zeta(x).hold();
3897                 }
3898
3899                 // decide on summation algorithm
3900                 // this is still a bit clumsy
3901                 int limit = (Digits>17) ? 10 : 6;
3902                 if ((r[0] < limit) || ((count > 3) && (r[1] < limit/2))) {
3903                         return numeric(zeta_do_sum_Crandall(r));
3904                 } else {
3905                         return numeric(zeta_do_sum_simple(r));
3906                 }
3907         }
3908
3909         // single zeta value
3910         if (is_exactly_a<numeric>(x) && (x != 1)) {
3911                 try {
3912                         return zeta(ex_to<numeric>(x));
3913                 } catch (const dunno &e) { }
3914         }
3915
3916         return zeta(x).hold();
3917 }
3918
3919
3920 static ex zeta1_eval(const ex& m)
3921 {
3922         if (is_exactly_a<lst>(m)) {
3923                 if (m.nops() == 1) {
3924                         return zeta(m.op(0));
3925                 }
3926                 return zeta(m).hold();
3927         }
3928
3929         if (m.info(info_flags::numeric)) {
3930                 const numeric& y = ex_to<numeric>(m);
3931                 // trap integer arguments:
3932                 if (y.is_integer()) {
3933                         if (y.is_zero()) {
3934                                 return _ex_1_2;
3935                         }
3936                         if (y.is_equal(*_num1_p)) {
3937                                 return zeta(m).hold();
3938                         }
3939                         if (y.info(info_flags::posint)) {
3940                                 if (y.info(info_flags::odd)) {
3941                                         return zeta(m).hold();
3942                                 } else {
3943                                         return abs(bernoulli(y)) * pow(Pi, y) * pow(*_num2_p, y-(*_num1_p)) / factorial(y);
3944                                 }
3945                         } else {
3946                                 if (y.info(info_flags::odd)) {
3947                                         return -bernoulli((*_num1_p)-y) / ((*_num1_p)-y);
3948                                 } else {
3949                                         return _ex0;
3950                                 }
3951                         }
3952                 }
3953                 // zeta(float)
3954                 if (y.info(info_flags::numeric) && !y.info(info_flags::crational)) {
3955                         return zeta1_evalf(m);
3956                 }
3957         }
3958         return zeta(m).hold();
3959 }
3960
3961
3962 static ex zeta1_deriv(const ex& m, unsigned deriv_param)
3963 {
3964         GINAC_ASSERT(deriv_param==0);
3965
3966         if (is_exactly_a<lst>(m)) {
3967                 return _ex0;
3968         } else {
3969                 return zetaderiv(_ex1, m);
3970         }
3971 }
3972
3973
3974 static void zeta1_print_latex(const ex& m_, const print_context& c)
3975 {
3976         c.s << "\\zeta(";
3977         if (is_a<lst>(m_)) {
3978                 const lst& m = ex_to<lst>(m_);
3979                 auto it = m.begin();
3980                 (*it).print(c);
3981                 it++;
3982                 for (; it != m.end(); it++) {
3983                         c.s << ",";
3984                         (*it).print(c);
3985                 }
3986         } else {
3987                 m_.print(c);
3988         }
3989         c.s << ")";
3990 }
3991
3992
3993 unsigned zeta1_SERIAL::serial = function::register_new(function_options("zeta", 1).
3994                                 evalf_func(zeta1_evalf).
3995                                 eval_func(zeta1_eval).
3996                                 derivative_func(zeta1_deriv).
3997                                 print_func<print_latex>(zeta1_print_latex).
3998                                 do_not_evalf_params().
3999                                 overloaded(2));
4000
4001
4002 //////////////////////////////////////////////////////////////////////
4003 //
4004 // Alternating Euler sum  zeta(x,s)
4005 //
4006 // GiNaC function
4007 //
4008 //////////////////////////////////////////////////////////////////////
4009
4010
4011 static ex zeta2_evalf(const ex& x, const ex& s)
4012 {
4013         if (is_exactly_a<lst>(x)) {
4014
4015                 // alternating Euler sum
4016                 const int count = x.nops();
4017                 const lst& xlst = ex_to<lst>(x);
4018                 const lst& slst = ex_to<lst>(s);
4019                 std::vector<int> xi(count);
4020                 std::vector<int> si(count);
4021
4022                 // check parameters and convert them
4023                 auto it_xread = xlst.begin();
4024                 auto it_sread = slst.begin();
4025                 auto it_xwrite = xi.begin();
4026                 auto it_swrite = si.begin();
4027                 do {
4028                         if (!(*it_xread).info(info_flags::posint)) {
4029                                 return zeta(x, s).hold();
4030                         }
4031                         *it_xwrite = ex_to<numeric>(*it_xread).to_int();
4032                         if (*it_sread > 0) {
4033                                 *it_swrite = 1;
4034                         } else {
4035                                 *it_swrite = -1;
4036                         }
4037                         it_xread++;
4038                         it_sread++;
4039                         it_xwrite++;
4040                         it_swrite++;
4041                 } while (it_xwrite != xi.end());
4042
4043                 // check for divergence
4044                 if ((xi[0] == 1) && (si[0] == 1)) {
4045                         return zeta(x, s).hold();
4046                 }
4047
4048                 // use Hoelder convolution
4049                 return numeric(zeta_do_Hoelder_convolution(xi, si));
4050         }
4051
4052         return zeta(x, s).hold();
4053 }
4054
4055
4056 static ex zeta2_eval(const ex& m, const ex& s_)
4057 {
4058         if (is_exactly_a<lst>(s_)) {
4059                 const lst& s = ex_to<lst>(s_);
4060                 for (const auto & it : s) {
4061                         if (it.info(info_flags::positive)) {
4062                                 continue;
4063                         }
4064                         return zeta(m, s_).hold();
4065                 }
4066                 return zeta(m);
4067         } else if (s_.info(info_flags::positive)) {
4068                 return zeta(m);
4069         }
4070
4071         return zeta(m, s_).hold();
4072 }
4073
4074
4075 static ex zeta2_deriv(const ex& m, const ex& s, unsigned deriv_param)
4076 {
4077         GINAC_ASSERT(deriv_param==0);
4078
4079         if (is_exactly_a<lst>(m)) {
4080                 return _ex0;
4081         } else {
4082                 if ((is_exactly_a<lst>(s) && s.op(0).info(info_flags::positive)) || s.info(info_flags::positive)) {
4083                         return zetaderiv(_ex1, m);
4084                 }
4085                 return _ex0;
4086         }
4087 }
4088
4089
4090 static void zeta2_print_latex(const ex& m_, const ex& s_, const print_context& c)
4091 {
4092         lst m;
4093         if (is_a<lst>(m_)) {
4094                 m = ex_to<lst>(m_);
4095         } else {
4096                 m = lst{m_};
4097         }
4098         lst s;
4099         if (is_a<lst>(s_)) {
4100                 s = ex_to<lst>(s_);
4101         } else {
4102                 s = lst{s_};
4103         }
4104         c.s << "\\zeta(";
4105         auto itm = m.begin();
4106         auto its = s.begin();
4107         if (*its < 0) {
4108                 c.s << "\\overline{";
4109                 (*itm).print(c);
4110                 c.s << "}";
4111         } else {
4112                 (*itm).print(c);
4113         }
4114         its++;
4115         itm++;
4116         for (; itm != m.end(); itm++, its++) {
4117                 c.s << ",";
4118                 if (*its < 0) {
4119                         c.s << "\\overline{";
4120                         (*itm).print(c);
4121                         c.s << "}";
4122                 } else {
4123                         (*itm).print(c);
4124                 }
4125         }
4126         c.s << ")";
4127 }
4128
4129
4130 unsigned zeta2_SERIAL::serial = function::register_new(function_options("zeta", 2).
4131                                 evalf_func(zeta2_evalf).
4132                                 eval_func(zeta2_eval).
4133                                 derivative_func(zeta2_deriv).
4134                                 print_func<print_latex>(zeta2_print_latex).
4135                                 do_not_evalf_params().
4136                                 overloaded(2));
4137
4138
4139 } // namespace GiNaC
4140