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