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