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