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