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