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