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