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