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