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