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