Fixed bug that caused evalf to crash especially on 64bit machines
[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  *    nielsen's generalized polylogarithm  S(n,p,x)
9  *    harmonic polylogarithm               H(m,x) or H(lst(m_1,...,m_k),x)
10  *    multiple zeta value                  zeta(m) or zeta(lst(m_1,...,m_k))
11  *    alternating Euler sum                zeta(m,s) or zeta(lst(m_1,...,m_k),lst(s_1,...,s_k))
12  *
13  *  Some remarks:
14  *
15  *    - All formulae used can be looked up in the following publications:
16  *      [Kol] Nielsen's Generalized Polylogarithms, K.S.Kolbig, SIAM J.Math.Anal. 17 (1986), pp. 1232-1258.
17  *      [Cra] Fast Evaluation of Multiple Zeta Sums, R.E.Crandall, Math.Comp. 67 (1998), pp. 1163-1172.
18  *      [ReV] Harmonic Polylogarithms, E.Remiddi, J.A.M.Vermaseren, Int.J.Mod.Phys. A15 (2000), pp. 725-754
19  *      [BBB] Special Values of Multiple Polylogarithms, J.Borwein, D.Bradley, D.Broadhurst, P.Lisonek, Trans.Amer.Math.Soc. 353/3 (2001), pp. 907-941
20  *
21  *    - The order of parameters and arguments of Li and zeta is defined according to the nested sums
22  *      representation. The parameters for H are understood as in [ReV]. They can be in expanded --- only
23  *      0, 1 and -1 --- or in compactified --- a string with zeros in front of 1 or -1 is written as a single
24  *      number --- notation.
25  *
26  *    - Except for the multiple polylogarithm all functions can be nummerically evaluated with arguments in
27  *      the whole complex plane. Multiple polylogarithms evaluate only if for each argument x_i the product
28  *      x_1 * x_2 * ... * x_i is smaller than one. The parameters for Li, zeta and S must be positive integers.
29  *      If you want to have an alternating Euler sum, you have to give the signs of the parameters as a
30  *      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.
35  *
36  *    - The functions have no series expansion into nested sums. To do this, you have to convert these functions
37  *      into the appropriate objects from the nestedsums library, do the expansion and convert the
38  *      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.
45  *
46  */
47
48 /*
49  *  GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
50  *
51  *  This program is free software; you can redistribute it and/or modify
52  *  it under the terms of the GNU General Public License as published by
53  *  the Free Software Foundation; either version 2 of the License, or
54  *  (at your option) any later version.
55  *
56  *  This program is distributed in the hope that it will be useful,
57  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
58  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
59  *  GNU General Public License for more details.
60  *
61  *  You should have received a copy of the GNU General Public License
62  *  along with this program; if not, write to the Free Software
63  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
64  */
65
66 #include <stdexcept>
67 #include <vector>
68 #include <cln/cln.h>
69
70 #include "inifcns.h"
71
72 #include "add.h"
73 #include "constant.h"
74 #include "lst.h"
75 #include "mul.h"
76 #include "numeric.h"
77 #include "operators.h"
78 #include "power.h"
79 #include "pseries.h"
80 #include "relational.h"
81 #include "symbol.h"
82 #include "utils.h"
83 #include "wildcard.h"
84
85
86 namespace GiNaC {
87
88
89 //////////////////////////////////////////////////////////////////////
90 //
91 // Classical polylogarithm  Li(n,x)
92 //
93 // helper functions
94 //
95 //////////////////////////////////////////////////////////////////////
96
97
98 // anonymous namespace for helper functions
99 namespace {
100
101
102 // lookup table for factors built from Bernoulli numbers
103 // see fill_Xn()
104 std::vector<std::vector<cln::cl_N> > Xn;
105 // initial size of Xn that should suffice for 32bit machines (must be even)
106 const int xninitsizestep = 26;
107 int xninitsize = xninitsizestep;
108 int xnsize = 0;
109
110
111 // This function calculates the X_n. The X_n are needed for speed up of classical polylogarithms.
112 // With these numbers the polylogs can be calculated as follows:
113 //   Li_p (x)  =  \sum_{n=0}^\infty X_{p-2}(n) u^{n+1}/(n+1)! with  u = -log(1-x)
114 //   X_0(n) = B_n (Bernoulli numbers)
115 //   X_p(n) = \sum_{k=0}^n binomial(n,k) B_{n-k} / (k+1) * X_{p-1}(k)
116 // The calculation of Xn depends on X0 and X{n-1}.
117 // X_0 is special, it holds only the non-zero Bernoulli numbers with index 2 or greater.
118 // This results in a slightly more complicated algorithm for the X_n.
119 // The first index in Xn corresponds to the index of the polylog minus 2.
120 // The second index in Xn corresponds to the index from the actual sum.
121 void fill_Xn(int n)
122 {
123         if (n>1) {
124                 // calculate X_2 and higher (corresponding to Li_4 and higher)
125                 std::vector<cln::cl_N> buf(xninitsize);
126                 std::vector<cln::cl_N>::iterator it = buf.begin();
127                 cln::cl_N result;
128                 *it = -(cln::expt(cln::cl_I(2),n+1) - 1) / cln::expt(cln::cl_I(2),n+1); // i == 1
129                 it++;
130                 for (int i=2; i<=xninitsize; i++) {
131                         if (i&1) {
132                                 result = 0; // k == 0
133                         } else {
134                                 result = Xn[0][i/2-1]; // k == 0
135                         }
136                         for (int k=1; k<i-1; k++) {
137                                 if ( !(((i-k) & 1) && ((i-k) > 1)) ) {
138                                         result = result + cln::binomial(i,k) * Xn[0][(i-k)/2-1] * Xn[n-1][k-1] / (k+1);
139                                 }
140                         }
141                         result = result - cln::binomial(i,i-1) * Xn[n-1][i-2] / 2 / i; // k == i-1
142                         result = result + Xn[n-1][i-1] / (i+1); // k == i
143                         
144                         *it = result;
145                         it++;
146                 }
147                 Xn.push_back(buf);
148         } else if (n==1) {
149                 // special case to handle the X_0 correct
150                 std::vector<cln::cl_N> buf(xninitsize);
151                 std::vector<cln::cl_N>::iterator it = buf.begin();
152                 cln::cl_N result;
153                 *it = cln::cl_I(-3)/cln::cl_I(4); // i == 1
154                 it++;
155                 *it = cln::cl_I(17)/cln::cl_I(36); // i == 2
156                 it++;
157                 for (int i=3; i<=xninitsize; i++) {
158                         if (i & 1) {
159                                 result = -Xn[0][(i-3)/2]/2;
160                                 *it = (cln::binomial(i,1)/cln::cl_I(2) + cln::binomial(i,i-1)/cln::cl_I(i))*result;
161                                 it++;
162                         } else {
163                                 result = Xn[0][i/2-1] + Xn[0][i/2-1]/(i+1);
164                                 for (int k=1; k<i/2; k++) {
165                                         result = result + cln::binomial(i,k*2) * Xn[0][k-1] * Xn[0][i/2-k-1] / (k*2+1);
166                                 }
167                                 *it = result;
168                                 it++;
169                         }
170                 }
171                 Xn.push_back(buf);
172         } else {
173                 // calculate X_0
174                 std::vector<cln::cl_N> buf(xninitsize/2);
175                 std::vector<cln::cl_N>::iterator it = buf.begin();
176                 for (int i=1; i<=xninitsize/2; i++) {
177                         *it = bernoulli(i*2).to_cl_N();
178                         it++;
179                 }
180                 Xn.push_back(buf);
181         }
182
183         xnsize++;
184 }
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() > 0) {
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 (int 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 res = u - u*u/4;
261         cln::cl_N resbuf;
262         unsigned i = 1;
263         do {
264                 resbuf = res;
265                 factor = factor * u*u / (2*i * (2*i+1));
266                 res = res + (*it) * factor;
267                 i++;
268                 if (++it == xend) {
269                         double_Xn();
270                         it = Xn[0].begin() + (i-1);
271                         xend = Xn[0].end();
272                 }
273         } while (res != resbuf);
274         return res;
275 }
276
277
278 // calculates Li(n,x), n>2 without Xn
279 cln::cl_N Lin_do_sum(int n, const cln::cl_N& x)
280 {
281         cln::cl_N factor = x * cln::cl_float(1, cln::float_format(Digits));
282         cln::cl_N res = x;
283         cln::cl_N resbuf;
284         int i=2;
285         do {
286                 resbuf = res;
287                 factor = factor * x;
288                 res = res + factor / cln::expt(cln::cl_I(i),n);
289                 i++;
290         } while (res != resbuf);
291         return res;
292 }
293
294
295 // calculates Li(n,x), n>2 with Xn
296 cln::cl_N Lin_do_sum_Xn(int n, const cln::cl_N& x)
297 {
298         std::vector<cln::cl_N>::const_iterator it = Xn[n-2].begin();
299         std::vector<cln::cl_N>::const_iterator xend = Xn[n-2].end();
300         cln::cl_N u = -cln::log(1-x);
301         cln::cl_N factor = u * cln::cl_float(1, cln::float_format(Digits));
302         cln::cl_N res = u;
303         cln::cl_N resbuf;
304         unsigned i=2;
305         do {
306                 resbuf = res;
307                 factor = factor * u / i;
308                 res = res + (*it) * factor;
309                 i++;
310                 if (++it == xend) {
311                         double_Xn();
312                         it = Xn[n-2].begin() + (i-2);
313                         xend = Xn[n-2].end();
314                 }
315         } while (res != resbuf);
316         return res;
317 }
318
319
320 // forward declaration needed by function Li_projection and C below
321 numeric S_num(int n, int p, const numeric& x);
322
323
324 // helper function for classical polylog Li
325 cln::cl_N Li_projection(int n, const cln::cl_N& x, const cln::float_format_t& prec)
326 {
327         // treat n=2 as special case
328         if (n == 2) {
329                 // check if precalculated X0 exists
330                 if (xnsize == 0) {
331                         fill_Xn(0);
332                 }
333
334                 if (cln::realpart(x) < 0.5) {
335                         // choose the faster algorithm
336                         // the switching point was empirically determined. the optimal point
337                         // depends on hardware, Digits, ... so an approx value is okay.
338                         // it solves also the problem with precision due to the u=-log(1-x) transformation
339                         if (cln::abs(cln::realpart(x)) < 0.25) {
340                                 
341                                 return Li2_do_sum(x);
342                         } else {
343                                 return Li2_do_sum_Xn(x);
344                         }
345                 } else {
346                         // choose the faster algorithm
347                         if (cln::abs(cln::realpart(x)) > 0.75) {
348                                 return -Li2_do_sum(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
349                         } else {
350                                 return -Li2_do_sum_Xn(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
351                         }
352                 }
353         } else {
354                 // check if precalculated Xn exist
355                 if (n > xnsize+1) {
356                         for (int i=xnsize; i<n-1; i++) {
357                                 fill_Xn(i);
358                         }
359                 }
360
361                 if (cln::realpart(x) < 0.5) {
362                         // choose the faster algorithm
363                         // with n>=12 the "normal" summation always wins against the method with Xn
364                         if ((cln::abs(cln::realpart(x)) < 0.3) || (n >= 12)) {
365                                 return Lin_do_sum(n, x);
366                         } else {
367                                 return Lin_do_sum_Xn(n, x);
368                         }
369                 } else {
370                         cln::cl_N result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
371                         for (int j=0; j<n-1; j++) {
372                                 result = result + (S_num(n-j-1, 1, 1).to_cl_N() - S_num(1, n-j-1, 1-x).to_cl_N())
373                                                   * cln::expt(cln::log(x), j) / cln::factorial(j);
374                         }
375                         return result;
376                 }
377         }
378 }
379
380
381 // helper function for classical polylog Li
382 numeric Li_num(int n, const numeric& x)
383 {
384         if (n == 1) {
385                 // just a log
386                 return -cln::log(1-x.to_cl_N());
387         }
388         if (x.is_zero()) {
389                 return 0;
390         }
391         if (x == 1) {
392                 // [Kol] (2.22)
393                 return cln::zeta(n);
394         }
395         else if (x == -1) {
396                 // [Kol] (2.22)
397                 return -(1-cln::expt(cln::cl_I(2),1-n)) * cln::zeta(n);
398         }
399         
400         // what is the desired float format?
401         // first guess: default format
402         cln::float_format_t prec = cln::default_float_format;
403         const cln::cl_N value = x.to_cl_N();
404         // second guess: the argument's format
405         if (!x.real().is_rational())
406                 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
407         else if (!x.imag().is_rational())
408                 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
409         
410         // [Kol] (5.15)
411         if (cln::abs(value) > 1) {
412                 cln::cl_N result = -cln::expt(cln::log(-value),n) / cln::factorial(n);
413                 // check if argument is complex. if it is real, the new polylog has to be conjugated.
414                 if (cln::zerop(cln::imagpart(value))) {
415                         if (n & 1) {
416                                 result = result + conjugate(Li_projection(n, cln::recip(value), prec));
417                         }
418                         else {
419                                 result = result - conjugate(Li_projection(n, cln::recip(value), prec));
420                         }
421                 }
422                 else {
423                         if (n & 1) {
424                                 result = result + Li_projection(n, cln::recip(value), prec);
425                         }
426                         else {
427                                 result = result - Li_projection(n, cln::recip(value), prec);
428                         }
429                 }
430                 cln::cl_N add;
431                 for (int j=0; j<n-1; j++) {
432                         add = add + (1+cln::expt(cln::cl_I(-1),n-j)) * (1-cln::expt(cln::cl_I(2),1-n+j))
433                                     * Li_num(n-j,1).to_cl_N() * cln::expt(cln::log(-value),j) / cln::factorial(j);
434                 }
435                 result = result - add;
436                 return result;
437         }
438         else {
439                 return Li_projection(n, value, prec);
440         }
441 }
442
443
444 } // end of anonymous namespace
445
446
447 //////////////////////////////////////////////////////////////////////
448 //
449 // Multiple polylogarithm  Li(n,x)
450 //
451 // helper function
452 //
453 //////////////////////////////////////////////////////////////////////
454
455
456 // anonymous namespace for helper function
457 namespace {
458
459
460 cln::cl_N multipleLi_do_sum(const std::vector<int>& s, const std::vector<cln::cl_N>& x)
461 {
462         const int j = s.size();
463
464         std::vector<cln::cl_N> t(j);
465         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
466
467         cln::cl_N t0buf;
468         int q = 0;
469         do {
470                 t0buf = t[0];
471                 // do it once ...
472                 q++;
473                 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
474                 for (int k=j-2; k>=0; k--) {
475                         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]);
476                 }
477                 // ... and do it again (to avoid premature drop out due to special arguments)
478                 q++;
479                 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
480                 for (int k=j-2; k>=0; k--) {
481                         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]);
482                 }
483         } while (t[0] != t0buf);
484
485         return t[0];
486 }
487
488 // forward declaration for Li_eval()
489 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf);
490
491
492 } // end of anonymous namespace
493
494
495 //////////////////////////////////////////////////////////////////////
496 //
497 // Classical polylogarithm and multiple polylogarithm  Li(n,x)
498 //
499 // GiNaC function
500 //
501 //////////////////////////////////////////////////////////////////////
502
503
504 static ex Li_evalf(const ex& x1, const ex& x2)
505 {
506         // classical polylogs
507         if (is_a<numeric>(x1) && is_a<numeric>(x2)) {
508                 return Li_num(ex_to<numeric>(x1).to_int(), ex_to<numeric>(x2));
509         }
510         // multiple polylogs
511         else if (is_a<lst>(x1) && is_a<lst>(x2)) {
512                 ex conv = 1;
513                 for (int i=0; i<x1.nops(); i++) {
514                         if (!x1.op(i).info(info_flags::posint)) {
515                                 return Li(x1, x2).hold();
516                         }
517                         if (!is_a<numeric>(x2.op(i))) {
518                                 return Li(x1, x2).hold();
519                         }
520                         conv *= x2.op(i);
521                         if (abs(conv) >= 1) {
522                                 return Li(x1, x2).hold();
523                         }
524                 }
525
526                 std::vector<int> m;
527                 std::vector<cln::cl_N> x;
528                 for (int i=0; i<ex_to<numeric>(x1.nops()).to_int(); i++) {
529                         m.push_back(ex_to<numeric>(x1.op(i)).to_int());
530                         x.push_back(ex_to<numeric>(x2.op(i)).to_cl_N());
531                 }
532
533                 return numeric(multipleLi_do_sum(m, x));
534         }
535
536         return Li(x1,x2).hold();
537 }
538
539
540 static ex Li_eval(const ex& m_, const ex& x_)
541 {
542         if (m_.nops() < 2) {
543                 ex m;
544                 if (is_a<lst>(m_)) {
545                         m = m_.op(0);
546                 } else {
547                         m = m_;
548                 }
549                 ex x;
550                 if (is_a<lst>(x_)) {
551                         x = x_.op(0);
552                 } else {
553                         x = x_;
554                 }
555                 if (x == _ex0) {
556                         return _ex0;
557                 }
558                 if (x == _ex1) {
559                         return zeta(m);
560                 }
561                 if (x == _ex_1) {
562                         return (pow(2,1-m)-1) * zeta(m);
563                 }
564                 if (m == _ex1) {
565                         return -log(1-x);
566                 }
567                 if (m.info(info_flags::posint) && x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
568                         return Li_num(ex_to<numeric>(m).to_int(), ex_to<numeric>(x));
569                 }
570         } else {
571                 bool ish = true;
572                 bool iszeta = true;
573                 bool iszero = false;
574                 bool doevalf = false;
575                 bool doevalfveto = true;
576                 const lst& m = ex_to<lst>(m_);
577                 const lst& x = ex_to<lst>(x_);
578                 lst::const_iterator itm = m.begin();
579                 lst::const_iterator itx = x.begin();
580                 for (; itm != m.end(); itm++, itx++) {
581                         if (!(*itm).info(info_flags::posint)) {
582                                 return Li(m_, x_).hold();
583                         }
584                         if ((*itx != _ex1) && (*itx != _ex_1)) {
585                                 if (itx != x.begin()) {
586                                         ish = false;
587                                 }
588                                 iszeta = false;
589                         }
590                         if (*itx == _ex0) {
591                                 iszero = true;
592                         }
593                         if (!(*itx).info(info_flags::numeric)) {
594                                 doevalfveto = false;
595                         }
596                         if (!(*itx).info(info_flags::crational)) {
597                                 doevalf = true;
598                         }
599                 }
600                 if (iszeta) {
601                         return zeta(m_, x_);
602                 }
603                 if (iszero) {
604                         return _ex0;
605                 }
606                 if (ish) {
607                         ex pf;
608                         lst newm = convert_parameter_Li_to_H(m, x, pf);
609                         return pf * H(newm, x[0]);
610                 }
611                 if (doevalfveto && doevalf) {
612                         return Li(m_, x_).evalf();
613                 }
614         }
615         return Li(m_, x_).hold();
616 }
617
618
619 static ex Li_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
620 {
621         epvector seq;
622         seq.push_back(expair(Li(m, x), 0));
623         return pseries(rel, seq);
624 }
625
626
627 static ex Li_deriv(const ex& m_, const ex& x_, unsigned deriv_param)
628 {
629         GINAC_ASSERT(deriv_param < 2);
630         if (deriv_param == 0) {
631                 return _ex0;
632         }
633         if (m_.nops() > 1) {
634                 throw std::runtime_error("don't know how to derivate multiple polylogarithm!");
635         }
636         ex m;
637         if (is_a<lst>(m_)) {
638                 m = m_.op(0);
639         } else {
640                 m = m_;
641         }
642         ex x;
643         if (is_a<lst>(x_)) {
644                 x = x_.op(0);
645         } else {
646                 x = x_;
647         }
648         if (m > 0) {
649                 return Li(m-1, x) / x;
650         } else {
651                 return 1/(1-x);
652         }
653 }
654
655
656 static void Li_print_latex(const ex& m_, const ex& x_, const print_context& c)
657 {
658         lst m;
659         if (is_a<lst>(m_)) {
660                 m = ex_to<lst>(m_);
661         } else {
662                 m = lst(m_);
663         }
664         lst x;
665         if (is_a<lst>(x_)) {
666                 x = ex_to<lst>(x_);
667         } else {
668                 x = lst(x_);
669         }
670         c.s << "\\mbox{Li}_{";
671         lst::const_iterator itm = m.begin();
672         (*itm).print(c);
673         itm++;
674         for (; itm != m.end(); itm++) {
675                 c.s << ",";
676                 (*itm).print(c);
677         }
678         c.s << "}(";
679         lst::const_iterator itx = x.begin();
680         (*itx).print(c);
681         itx++;
682         for (; itx != x.end(); itx++) {
683                 c.s << ",";
684                 (*itx).print(c);
685         }
686         c.s << ")";
687 }
688
689
690 REGISTER_FUNCTION(Li,
691                   evalf_func(Li_evalf).
692                   eval_func(Li_eval).
693                   series_func(Li_series).
694                   derivative_func(Li_deriv).
695                   print_func<print_latex>(Li_print_latex).
696                   do_not_evalf_params());
697
698
699 //////////////////////////////////////////////////////////////////////
700 //
701 // Nielsen's generalized polylogarithm  S(n,p,x)
702 //
703 // helper functions
704 //
705 //////////////////////////////////////////////////////////////////////
706
707
708 // anonymous namespace for helper functions
709 namespace {
710
711
712 // lookup table for special Euler-Zagier-Sums (used for S_n,p(x))
713 // see fill_Yn()
714 std::vector<std::vector<cln::cl_N> > Yn;
715 int ynsize = 0; // number of Yn[]
716 int ynlength = 100; // initial length of all Yn[i]
717
718
719 // This function calculates the Y_n. The Y_n are needed for the evaluation of S_{n,p}(x).
720 // The Y_n are basically Euler-Zagier sums with all m_i=1. They are subsums in the Z-sum
721 // representing S_{n,p}(x).
722 // The first index in Y_n corresponds to the parameter p minus one, i.e. the depth of the
723 // equivalent Z-sum.
724 // The second index in Y_n corresponds to the running index of the outermost sum in the full Z-sum
725 // representing S_{n,p}(x).
726 // The calculation of Y_n uses the values from Y_{n-1}.
727 void fill_Yn(int n, const cln::float_format_t& prec)
728 {
729         const int initsize = ynlength;
730         //const int initsize = initsize_Yn;
731         cln::cl_N one = cln::cl_float(1, prec);
732
733         if (n) {
734                 std::vector<cln::cl_N> buf(initsize);
735                 std::vector<cln::cl_N>::iterator it = buf.begin();
736                 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
737                 *it = (*itprev) / cln::cl_N(n+1) * one;
738                 it++;
739                 itprev++;
740                 // sums with an index smaller than the depth are zero and need not to be calculated.
741                 // calculation starts with depth, which is n+2)
742                 for (int i=n+2; i<=initsize+n; i++) {
743                         *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
744                         it++;
745                         itprev++;
746                 }
747                 Yn.push_back(buf);
748         } else {
749                 std::vector<cln::cl_N> buf(initsize);
750                 std::vector<cln::cl_N>::iterator it = buf.begin();
751                 *it = 1 * one;
752                 it++;
753                 for (int i=2; i<=initsize; i++) {
754                         *it = *(it-1) + 1 / cln::cl_N(i) * one;
755                         it++;
756                 }
757                 Yn.push_back(buf);
758         }
759         ynsize++;
760 }
761
762
763 // make Yn longer ... 
764 void make_Yn_longer(int newsize, const cln::float_format_t& prec)
765 {
766
767         cln::cl_N one = cln::cl_float(1, prec);
768
769         Yn[0].resize(newsize);
770         std::vector<cln::cl_N>::iterator it = Yn[0].begin();
771         it += ynlength;
772         for (int i=ynlength+1; i<=newsize; i++) {
773                 *it = *(it-1) + 1 / cln::cl_N(i) * one;
774                 it++;
775         }
776
777         for (int n=1; n<ynsize; n++) {
778                 Yn[n].resize(newsize);
779                 std::vector<cln::cl_N>::iterator it = Yn[n].begin();
780                 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
781                 it += ynlength;
782                 itprev += ynlength;
783                 for (int i=ynlength+n+1; i<=newsize+n; i++) {
784                         *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
785                         it++;
786                         itprev++;
787                 }
788         }
789         
790         ynlength = newsize;
791 }
792
793
794 // helper function for S(n,p,x)
795 // [Kol] (7.2)
796 cln::cl_N C(int n, int p)
797 {
798         cln::cl_N result;
799
800         for (int k=0; k<p; k++) {
801                 for (int j=0; j<=(n+k-1)/2; j++) {
802                         if (k == 0) {
803                                 if (n & 1) {
804                                         if (j & 1) {
805                                                 result = result - 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1).to_cl_N() / cln::factorial(2*j);
806                                         }
807                                         else {
808                                                 result = result + 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1).to_cl_N() / cln::factorial(2*j);
809                                         }
810                                 }
811                         }
812                         else {
813                                 if (k & 1) {
814                                         if (j & 1) {
815                                                 result = result + cln::factorial(n+k-1)
816                                                                   * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
817                                                                   / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
818                                         }
819                                         else {
820                                                 result = result - cln::factorial(n+k-1)
821                                                                   * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
822                                                                   / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
823                                         }
824                                 }
825                                 else {
826                                         if (j & 1) {
827                                                 result = result - cln::factorial(n+k-1) * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
828                                                                   / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
829                                         }
830                                         else {
831                                                 result = result + cln::factorial(n+k-1)
832                                                                   * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1).to_cl_N()
833                                                                   / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
834                                         }
835                                 }
836                         }
837                 }
838         }
839         int np = n+p;
840         if ((np-1) & 1) {
841                 if (((np)/2+n) & 1) {
842                         result = -result - cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
843                 }
844                 else {
845                         result = -result + cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
846                 }
847         }
848
849         return result;
850 }
851
852
853 // helper function for S(n,p,x)
854 // [Kol] remark to (9.1)
855 cln::cl_N a_k(int k)
856 {
857         cln::cl_N result;
858
859         if (k == 0) {
860                 return 1;
861         }
862
863         result = result;
864         for (int m=2; m<=k; m++) {
865                 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * a_k(k-m);
866         }
867
868         return -result / k;
869 }
870
871
872 // helper function for S(n,p,x)
873 // [Kol] remark to (9.1)
874 cln::cl_N b_k(int k)
875 {
876         cln::cl_N result;
877
878         if (k == 0) {
879                 return 1;
880         }
881
882         result = result;
883         for (int m=2; m<=k; m++) {
884                 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * b_k(k-m);
885         }
886
887         return result / k;
888 }
889
890
891 // helper function for S(n,p,x)
892 cln::cl_N S_do_sum(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
893 {
894         if (p==1) {
895                 return Li_projection(n+1, x, prec);
896         }
897         
898         // check if precalculated values are sufficient
899         if (p > ynsize+1) {
900                 for (int i=ynsize; i<p-1; i++) {
901                         fill_Yn(i, prec);
902                 }
903         }
904
905         // should be done otherwise
906         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
907         cln::cl_N xf = x * one;
908         //cln::cl_N xf = x * cln::cl_float(1, prec);
909
910         cln::cl_N res;
911         cln::cl_N resbuf;
912         cln::cl_N factor = cln::expt(xf, p);
913         int i = p;
914         do {
915                 resbuf = res;
916                 if (i-p >= ynlength) {
917                         // make Yn longer
918                         make_Yn_longer(ynlength*2, prec);
919                 }
920                 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? ...
921                 //res = res + factor / cln::expt(cln::cl_I(i),n+1) * (*it); // should we check it? or rely on magic number? ...
922                 factor = factor * xf;
923                 i++;
924         } while (res != resbuf);
925         
926         return res;
927 }
928
929
930 // helper function for S(n,p,x)
931 cln::cl_N S_projection(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
932 {
933         // [Kol] (5.3)
934         if (cln::abs(cln::realpart(x)) > cln::cl_F("0.5")) {
935
936                 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(x),n)
937                                    * cln::expt(cln::log(1-x),p) / cln::factorial(n) / cln::factorial(p);
938
939                 for (int s=0; s<n; s++) {
940                         cln::cl_N res2;
941                         for (int r=0; r<p; r++) {
942                                 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-x),r)
943                                               * S_do_sum(p-r,n-s,1-x,prec) / cln::factorial(r);
944                         }
945                         result = result + cln::expt(cln::log(x),s) * (S_num(n-s,p,1).to_cl_N() - res2) / cln::factorial(s);
946                 }
947
948                 return result;
949         }
950         
951         return S_do_sum(n, p, x, prec);
952 }
953
954
955 // helper function for S(n,p,x)
956 numeric S_num(int n, int p, const numeric& x)
957 {
958         if (x == 1) {
959                 if (n == 1) {
960                     // [Kol] (2.22) with (2.21)
961                         return cln::zeta(p+1);
962                 }
963
964                 if (p == 1) {
965                     // [Kol] (2.22)
966                         return cln::zeta(n+1);
967                 }
968
969                 // [Kol] (9.1)
970                 cln::cl_N result;
971                 for (int nu=0; nu<n; nu++) {
972                         for (int rho=0; rho<=p; rho++) {
973                                 result = result + b_k(n-nu-1) * b_k(p-rho) * a_k(nu+rho+1)
974                                                   * cln::factorial(nu+rho+1) / cln::factorial(rho) / cln::factorial(nu+1);
975                         }
976                 }
977                 result = result * cln::expt(cln::cl_I(-1),n+p-1);
978
979                 return result;
980         }
981         else if (x == -1) {
982                 // [Kol] (2.22)
983                 if (p == 1) {
984                         return -(1-cln::expt(cln::cl_I(2),-n)) * cln::zeta(n+1);
985                 }
986 //              throw std::runtime_error("don't know how to evaluate this function!");
987         }
988
989         // what is the desired float format?
990         // first guess: default format
991         cln::float_format_t prec = cln::default_float_format;
992         const cln::cl_N value = x.to_cl_N();
993         // second guess: the argument's format
994         if (!x.real().is_rational())
995                 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
996         else if (!x.imag().is_rational())
997                 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
998
999         // [Kol] (5.3)
1000         if ((cln::realpart(value) < -0.5) || (n == 0)) {
1001
1002                 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(value),n)
1003                                    * cln::expt(cln::log(1-value),p) / cln::factorial(n) / cln::factorial(p);
1004
1005                 for (int s=0; s<n; s++) {
1006                         cln::cl_N res2;
1007                         for (int r=0; r<p; r++) {
1008                                 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-value),r)
1009                                               * S_num(p-r,n-s,1-value).to_cl_N() / cln::factorial(r);
1010                         }
1011                         result = result + cln::expt(cln::log(value),s) * (S_num(n-s,p,1).to_cl_N() - res2) / cln::factorial(s);
1012                 }
1013
1014                 return result;
1015                 
1016         }
1017         // [Kol] (5.12)
1018         if (cln::abs(value) > 1) {
1019                 
1020                 cln::cl_N result;
1021
1022                 for (int s=0; s<p; s++) {
1023                         for (int r=0; r<=s; r++) {
1024                                 result = result + cln::expt(cln::cl_I(-1),s) * cln::expt(cln::log(-value),r) * cln::factorial(n+s-r-1)
1025                                                   / cln::factorial(r) / cln::factorial(s-r) / cln::factorial(n-1)
1026                                                   * S_num(n+s-r,p-s,cln::recip(value)).to_cl_N();
1027                         }
1028                 }
1029                 result = result * cln::expt(cln::cl_I(-1),n);
1030
1031                 cln::cl_N res2;
1032                 for (int r=0; r<n; r++) {
1033                         res2 = res2 + cln::expt(cln::log(-value),r) * C(n-r,p) / cln::factorial(r);
1034                 }
1035                 res2 = res2 + cln::expt(cln::log(-value),n+p) / cln::factorial(n+p);
1036
1037                 result = result + cln::expt(cln::cl_I(-1),p) * res2;
1038
1039                 return result;
1040         }
1041         else {
1042                 return S_projection(n, p, value, prec);
1043         }
1044 }
1045
1046
1047 } // end of anonymous namespace
1048
1049
1050 //////////////////////////////////////////////////////////////////////
1051 //
1052 // Nielsen's generalized polylogarithm  S(n,p,x)
1053 //
1054 // GiNaC function
1055 //
1056 //////////////////////////////////////////////////////////////////////
1057
1058
1059 static ex S_evalf(const ex& n, const ex& p, const ex& x)
1060 {
1061         if (n.info(info_flags::posint) && p.info(info_flags::posint) && is_a<numeric>(x)) {
1062                 return S_num(ex_to<numeric>(n).to_int(), ex_to<numeric>(p).to_int(), ex_to<numeric>(x));
1063         }
1064         return S(n, p, x).hold();
1065 }
1066
1067
1068 static ex S_eval(const ex& n, const ex& p, const ex& x)
1069 {
1070         if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
1071                 if (x == 0) {
1072                         return _ex0;
1073                 }
1074                 if (x == 1) {
1075                         lst m(n+1);
1076                         for (int i=ex_to<numeric>(p).to_int()-1; i>0; i--) {
1077                                 m.append(1);
1078                         }
1079                         return zeta(m);
1080                 }
1081                 if (p == 1) {
1082                         return Li(n+1, x);
1083                 }
1084                 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
1085                         return S_num(ex_to<numeric>(n).to_int(), ex_to<numeric>(p).to_int(), ex_to<numeric>(x));
1086                 }
1087         }
1088         if (n.is_zero()) {
1089                 // [Kol] (5.3)
1090                 return pow(-log(1-x), p) / factorial(p);
1091         }
1092         return S(n, p, x).hold();
1093 }
1094
1095
1096 static ex S_series(const ex& n, const ex& p, const ex& x, const relational& rel, int order, unsigned options)
1097 {
1098         epvector seq;
1099         seq.push_back(expair(S(n, p, x), 0));
1100         return pseries(rel, seq);
1101 }
1102
1103
1104 static ex S_deriv(const ex& n, const ex& p, const ex& x, unsigned deriv_param)
1105 {
1106         GINAC_ASSERT(deriv_param < 3);
1107         if (deriv_param < 2) {
1108                 return _ex0;
1109         }
1110         if (n > 0) {
1111                 return S(n-1, p, x) / x;
1112         } else {
1113                 return S(n, p-1, x) / (1-x);
1114         }
1115 }
1116
1117
1118 static void S_print_latex(const ex& n, const ex& p, const ex& x, const print_context& c)
1119 {
1120         c.s << "\\mbox{S}_{";
1121         n.print(c);
1122         c.s << ",";
1123         p.print(c);
1124         c.s << "}(";
1125         x.print(c);
1126         c.s << ")";
1127 }
1128
1129
1130 REGISTER_FUNCTION(S,
1131                   evalf_func(S_evalf).
1132                   eval_func(S_eval).
1133                   series_func(S_series).
1134                   derivative_func(S_deriv).
1135                   print_func<print_latex>(S_print_latex).
1136                   do_not_evalf_params());
1137
1138
1139 //////////////////////////////////////////////////////////////////////
1140 //
1141 // Harmonic polylogarithm  H(m,x)
1142 //
1143 // helper functions
1144 //
1145 //////////////////////////////////////////////////////////////////////
1146
1147
1148 // anonymous namespace for helper functions
1149 namespace {
1150
1151         
1152 // regulates the pole (used by 1/x-transformation)
1153 symbol H_polesign("IMSIGN");
1154
1155
1156 // convert parameters from H to Li representation
1157 // parameters are expected to be in expanded form, i.e. only 0, 1 and -1
1158 // returns true if some parameters are negative
1159 bool convert_parameter_H_to_Li(const lst& l, lst& m, lst& s, ex& pf)
1160 {
1161         // expand parameter list
1162         lst mexp;
1163         for (lst::const_iterator it = l.begin(); it != l.end(); it++) {
1164                 if (*it > 1) {
1165                         for (ex count=*it-1; count > 0; count--) {
1166                                 mexp.append(0);
1167                         }
1168                         mexp.append(1);
1169                 } else if (*it < -1) {
1170                         for (ex count=*it+1; count < 0; count++) {
1171                                 mexp.append(0);
1172                         }
1173                         mexp.append(-1);
1174                 } else {
1175                         mexp.append(*it);
1176                 }
1177         }
1178         
1179         ex signum = 1;
1180         pf = 1;
1181         bool has_negative_parameters = false;
1182         ex acc = 1;
1183         for (lst::const_iterator it = mexp.begin(); it != mexp.end(); it++) {
1184                 if (*it == 0) {
1185                         acc++;
1186                         continue;
1187                 }
1188                 if (*it > 0) {
1189                         m.append((*it+acc-1) * signum);
1190                 } else {
1191                         m.append((*it-acc+1) * signum);
1192                 }
1193                 acc = 1;
1194                 signum = *it;
1195                 pf *= *it;
1196                 if (pf < 0) {
1197                         has_negative_parameters = true;
1198                 }
1199         }
1200         if (has_negative_parameters) {
1201                 for (int i=0; i<m.nops(); i++) {
1202                         if (m.op(i) < 0) {
1203                                 m.let_op(i) = -m.op(i);
1204                                 s.append(-1);
1205                         } else {
1206                                 s.append(1);
1207                         }
1208                 }
1209         }
1210         
1211         return has_negative_parameters;
1212 }
1213
1214
1215 // recursivly transforms H to corresponding multiple polylogarithms
1216 struct map_trafo_H_convert_to_Li : public map_function
1217 {
1218         ex operator()(const ex& e)
1219         {
1220                 if (is_a<add>(e) || is_a<mul>(e)) {
1221                         return e.map(*this);
1222                 }
1223                 if (is_a<function>(e)) {
1224                         std::string name = ex_to<function>(e).get_name();
1225                         if (name == "H") {
1226                                 lst parameter;
1227                                 if (is_a<lst>(e.op(0))) {
1228                                                 parameter = ex_to<lst>(e.op(0));
1229                                 } else {
1230                                         parameter = lst(e.op(0));
1231                                 }
1232                                 ex arg = e.op(1);
1233
1234                                 lst m;
1235                                 lst s;
1236                                 ex pf;
1237                                 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
1238                                         s.let_op(0) = s.op(0) * arg;
1239                                         return pf * Li(m, s).hold();
1240                                 } else {
1241                                         for (int i=0; i<m.nops(); i++) {
1242                                                 s.append(1);
1243                                         }
1244                                         s.let_op(0) = s.op(0) * arg;
1245                                         return Li(m, s).hold();
1246                                 }
1247                         }
1248                 }
1249                 return e;
1250         }
1251 };
1252
1253
1254 // recursivly transforms H to corresponding zetas
1255 struct map_trafo_H_convert_to_zeta : public map_function
1256 {
1257         ex operator()(const ex& e)
1258         {
1259                 if (is_a<add>(e) || is_a<mul>(e)) {
1260                         return e.map(*this);
1261                 }
1262                 if (is_a<function>(e)) {
1263                         std::string name = ex_to<function>(e).get_name();
1264                         if (name == "H") {
1265                                 lst parameter;
1266                                 if (is_a<lst>(e.op(0))) {
1267                                                 parameter = ex_to<lst>(e.op(0));
1268                                 } else {
1269                                         parameter = lst(e.op(0));
1270                                 }
1271
1272                                 lst m;
1273                                 lst s;
1274                                 ex pf;
1275                                 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
1276                                         return pf * zeta(m, s);
1277                                 } else {
1278                                         return zeta(m);
1279                                 }
1280                         }
1281                 }
1282                 return e;
1283         }
1284 };
1285
1286
1287 // remove trailing zeros from H-parameters
1288 struct map_trafo_H_reduce_trailing_zeros : public map_function
1289 {
1290         ex operator()(const ex& e)
1291         {
1292                 if (is_a<add>(e) || is_a<mul>(e)) {
1293                         return e.map(*this);
1294                 }
1295                 if (is_a<function>(e)) {
1296                         std::string name = ex_to<function>(e).get_name();
1297                         if (name == "H") {
1298                                 lst parameter;
1299                                 if (is_a<lst>(e.op(0))) {
1300                                         parameter = ex_to<lst>(e.op(0));
1301                                 } else {
1302                                         parameter = lst(e.op(0));
1303                                 }
1304                                 ex arg = e.op(1);
1305                                 if (parameter.op(parameter.nops()-1) == 0) {
1306                                         
1307                                         //
1308                                         if (parameter.nops() == 1) {
1309                                                 return log(arg);
1310                                         }
1311                                         
1312                                         //
1313                                         lst::const_iterator it = parameter.begin();
1314                                         while ((it != parameter.end()) && (*it == 0)) {
1315                                                 it++;
1316                                         }
1317                                         if (it == parameter.end()) {
1318                                                 return pow(log(arg),parameter.nops()) / factorial(parameter.nops());
1319                                         }
1320                                         
1321                                         //
1322                                         parameter.remove_last();
1323                                         int lastentry = parameter.nops();
1324                                         while ((lastentry > 0) && (parameter[lastentry-1] == 0)) {
1325                                                 lastentry--;
1326                                         }
1327                                         
1328                                         //
1329                                         ex result = log(arg) * H(parameter,arg).hold();
1330                                         ex acc = 0;
1331                                         for (ex i=0; i<lastentry; i++) {
1332                                                 if (parameter[i] > 0) {
1333                                                         parameter[i]++;
1334                                                         result -= (acc + parameter[i]-1) * H(parameter, arg).hold();
1335                                                         parameter[i]--;
1336                                                         acc = 0;
1337                                                 } else if (parameter[i] < 0) {
1338                                                         parameter[i]--;
1339                                                         result -= (acc + abs(parameter[i]+1)) * H(parameter, arg).hold();
1340                                                         parameter[i]++;
1341                                                         acc = 0;
1342                                                 } else {
1343                                                         acc++;
1344                                                 }
1345                                         }
1346                                         
1347                                         if (lastentry < parameter.nops()) {
1348                                                 result = result / (parameter.nops()-lastentry+1);
1349                                                 return result.map(*this);
1350                                         } else {
1351                                                 return result;
1352                                         }
1353                                 }
1354                         }
1355                 }
1356                 return e;
1357         }
1358 };
1359
1360
1361 // returns an expression with zeta functions corresponding to the parameter list for H
1362 ex convert_H_to_zeta(const lst& m)
1363 {
1364         symbol xtemp("xtemp");
1365         map_trafo_H_reduce_trailing_zeros filter;
1366         map_trafo_H_convert_to_zeta filter2;
1367         return filter2(filter(H(m, xtemp).hold())).subs(xtemp == 1);
1368 }
1369
1370
1371 // convert signs form Li to H representation
1372 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf)
1373 {
1374         lst res;
1375         lst::const_iterator itm = m.begin();
1376         lst::const_iterator itx = ++x.begin();
1377         ex signum = _ex1;
1378         pf = _ex1;
1379         res.append(*itm);
1380         itm++;
1381         while (itx != x.end()) {
1382                 signum *= *itx;
1383                 pf *= signum;
1384                 res.append((*itm) * signum);
1385                 itm++;
1386                 itx++;
1387         }
1388         return res;
1389 }
1390
1391
1392 // multiplies an one-dimensional H with another H
1393 // [ReV] (18)
1394 ex trafo_H_mult(const ex& h1, const ex& h2)
1395 {
1396         ex res;
1397         ex hshort;
1398         lst hlong;
1399         ex h1nops = h1.op(0).nops();
1400         ex h2nops = h2.op(0).nops();
1401         if (h1nops > 1) {
1402                 hshort = h2.op(0).op(0);
1403                 hlong = ex_to<lst>(h1.op(0));
1404         } else {
1405                 hshort = h1.op(0).op(0);
1406                 if (h2nops > 1) {
1407                         hlong = ex_to<lst>(h2.op(0));
1408                 } else {
1409                         hlong = h2.op(0).op(0);
1410                 }
1411         }
1412         for (int i=0; i<=hlong.nops(); i++) {
1413                 lst newparameter;
1414                 int j=0;
1415                 for (; j<i; j++) {
1416                         newparameter.append(hlong[j]);
1417                 }
1418                 newparameter.append(hshort);
1419                 for (; j<hlong.nops(); j++) {
1420                         newparameter.append(hlong[j]);
1421                 }
1422                 res += H(newparameter, h1.op(1)).hold();
1423         }
1424         return res;
1425 }
1426
1427
1428 // applies trafo_H_mult recursively on expressions
1429 struct map_trafo_H_mult : public map_function
1430 {
1431         ex operator()(const ex& e)
1432         {
1433                 if (is_a<add>(e)) {
1434                         return e.map(*this);
1435                 }
1436
1437                 if (is_a<mul>(e)) {
1438
1439                         ex result = 1;
1440                         ex firstH;
1441                         lst Hlst;
1442                         for (int pos=0; pos<e.nops(); pos++) {
1443                                 if (is_a<power>(e.op(pos)) && is_a<function>(e.op(pos).op(0))) {
1444                                         std::string name = ex_to<function>(e.op(pos).op(0)).get_name();
1445                                         if (name == "H") {
1446                                                 for (ex i=0; i<e.op(pos).op(1); i++) {
1447                                                         Hlst.append(e.op(pos).op(0));
1448                                                 }
1449                                                 continue;
1450                                         }
1451                                 } else if (is_a<function>(e.op(pos))) {
1452                                         std::string name = ex_to<function>(e.op(pos)).get_name();
1453                                         if (name == "H") {
1454                                                 if (e.op(pos).op(0).nops() > 1) {
1455                                                         firstH = e.op(pos);
1456                                                 } else {
1457                                                         Hlst.append(e.op(pos));
1458                                                 }
1459                                                 continue;
1460                                         }
1461                                 }
1462                                 result *= e.op(pos);
1463                         }
1464                         if (firstH == 0) {
1465                                 if (Hlst.nops() > 0) {
1466                                         firstH = Hlst[Hlst.nops()-1];
1467                                         Hlst.remove_last();
1468                                 } else {
1469                                         return e;
1470                                 }
1471                         }
1472
1473                         if (Hlst.nops() > 0) {
1474                                 ex buffer = trafo_H_mult(firstH, Hlst.op(0));
1475                                 result *= buffer;
1476                                 for (int i=1; i<Hlst.nops(); i++) {
1477                                         result *= Hlst.op(i);
1478                                 }
1479                                 result = result.expand();
1480                                 map_trafo_H_mult recursion;
1481                                 return recursion(result);
1482                         } else {
1483                                 return e;
1484                         }
1485
1486                 }
1487                 return e;
1488         }
1489 };
1490
1491
1492 // do integration [ReV] (55)
1493 // put parameter 0 in front of existing parameters
1494 ex trafo_H_1tx_prepend_zero(const ex& e, const ex& arg)
1495 {
1496         ex h;
1497         std::string name;
1498         if (is_a<function>(e)) {
1499                 name = ex_to<function>(e).get_name();
1500         }
1501         if (name == "H") {
1502                 h = e;
1503         } else {
1504                 for (int i=0; i<e.nops(); i++) {
1505                         if (is_a<function>(e.op(i))) {
1506                                 std::string name = ex_to<function>(e.op(i)).get_name();
1507                                 if (name == "H") {
1508                                         h = e.op(i);
1509                                 }
1510                         }
1511                 }
1512         }
1513         if (h != 0) {
1514                 lst newparameter = ex_to<lst>(h.op(0));
1515                 newparameter.prepend(0);
1516                 ex addzeta = convert_H_to_zeta(newparameter);
1517                 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
1518         } else {
1519                 return e * (-H(lst(0),1/arg).hold());
1520         }
1521 }
1522
1523
1524 // do integration [ReV] (55)
1525 // put parameter -1 in front of existing parameters
1526 ex trafo_H_1tx_prepend_minusone(const ex& e, const ex& arg)
1527 {
1528         ex h;
1529         std::string name;
1530         if (is_a<function>(e)) {
1531                 name = ex_to<function>(e).get_name();
1532         }
1533         if (name == "H") {
1534                 h = e;
1535         } else {
1536                 for (int i=0; i<e.nops(); i++) {
1537                         if (is_a<function>(e.op(i))) {
1538                                 std::string name = ex_to<function>(e.op(i)).get_name();
1539                                 if (name == "H") {
1540                                         h = e.op(i);
1541                                 }
1542                         }
1543                 }
1544         }
1545         if (h != 0) {
1546                 lst newparameter = ex_to<lst>(h.op(0));
1547                 newparameter.prepend(-1);
1548                 ex addzeta = convert_H_to_zeta(newparameter);
1549                 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
1550         } else {
1551                 ex addzeta = convert_H_to_zeta(lst(-1));
1552                 return (e * (addzeta - H(lst(-1),1/arg).hold())).expand();
1553         }
1554 }
1555
1556
1557 // do integration [ReV] (55)
1558 // put parameter -1 in front of existing parameters
1559 ex trafo_H_1mxt1px_prepend_minusone(const ex& e, const ex& arg)
1560 {
1561         ex h;
1562         std::string name;
1563         if (is_a<function>(e)) {
1564                 name = ex_to<function>(e).get_name();
1565         }
1566         if (name == "H") {
1567                 h = e;
1568         } else {
1569                 for (int i=0; i<e.nops(); i++) {
1570                         if (is_a<function>(e.op(i))) {
1571                                 std::string name = ex_to<function>(e.op(i)).get_name();
1572                                 if (name == "H") {
1573                                         h = e.op(i);
1574                                 }
1575                         }
1576                 }
1577         }
1578         if (h != 0) {
1579                 lst newparameter = ex_to<lst>(h.op(0));
1580                 newparameter.prepend(-1);
1581                 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
1582         } else {
1583                 return (e * H(lst(-1),(1-arg)/(1+arg)).hold()).expand();
1584         }
1585 }
1586
1587
1588 // do integration [ReV] (55)
1589 // put parameter 1 in front of existing parameters
1590 ex trafo_H_1mxt1px_prepend_one(const ex& e, const ex& arg)
1591 {
1592         ex h;
1593         std::string name;
1594         if (is_a<function>(e)) {
1595                 name = ex_to<function>(e).get_name();
1596         }
1597         if (name == "H") {
1598                 h = e;
1599         } else {
1600                 for (int i=0; i<e.nops(); i++) {
1601                         if (is_a<function>(e.op(i))) {
1602                                 std::string name = ex_to<function>(e.op(i)).get_name();
1603                                 if (name == "H") {
1604                                         h = e.op(i);
1605                                 }
1606                         }
1607                 }
1608         }
1609         if (h != 0) {
1610                 lst newparameter = ex_to<lst>(h.op(0));
1611                 newparameter.prepend(1);
1612                 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
1613         } else {
1614                 return (e * H(lst(1),(1-arg)/(1+arg)).hold()).expand();
1615         }
1616 }
1617
1618
1619 // do x -> 1/x transformation
1620 struct map_trafo_H_1overx : public map_function
1621 {
1622         ex operator()(const ex& e)
1623         {
1624                 if (is_a<add>(e) || is_a<mul>(e)) {
1625                         return e.map(*this);
1626                 }
1627
1628                 if (is_a<function>(e)) {
1629                         std::string name = ex_to<function>(e).get_name();
1630                         if (name == "H") {
1631
1632                                 lst parameter = ex_to<lst>(e.op(0));
1633                                 ex arg = e.op(1);
1634
1635                                 // special cases if all parameters are either 0, 1 or -1
1636                                 bool allthesame = true;
1637                                 if (parameter.op(0) == 0) {
1638                                         for (int i=1; i<parameter.nops(); i++) {
1639                                                 if (parameter.op(i) != 0) {
1640                                                         allthesame = false;
1641                                                         break;
1642                                                 }
1643                                         }
1644                                         if (allthesame) {
1645                                                 return pow(-1, parameter.nops()) * H(parameter, 1/arg).hold();
1646                                         }
1647                                 } else if (parameter.op(0) == -1) {
1648                                         for (int i=1; i<parameter.nops(); i++) {
1649                                                 if (parameter.op(i) != -1) {
1650                                                         allthesame = false;
1651                                                         break;
1652                                                 }
1653                                         }
1654                                         if (allthesame) {
1655                                                 map_trafo_H_mult unify;
1656                                                 return unify((pow(H(lst(-1),1/arg).hold() - H(lst(0),1/arg).hold(), parameter.nops())
1657                                                        / factorial(parameter.nops())).expand());
1658                                         }
1659                                 } else {
1660                                         for (int i=1; i<parameter.nops(); i++) {
1661                                                 if (parameter.op(i) != 1) {
1662                                                         allthesame = false;
1663                                                         break;
1664                                                 }
1665                                         }
1666                                         if (allthesame) {
1667                                                 map_trafo_H_mult unify;
1668                                                 return unify((pow(H(lst(1),1/arg).hold() + H(lst(0),1/arg).hold() + H_polesign, parameter.nops())
1669                                                        / factorial(parameter.nops())).expand());
1670                                         }
1671                                 }
1672
1673                                 lst newparameter = parameter;
1674                                 newparameter.remove_first();
1675
1676                                 if (parameter.op(0) == 0) {
1677                                         
1678                                         // leading zero
1679                                         ex res = convert_H_to_zeta(parameter);
1680                                         map_trafo_H_1overx recursion;
1681                                         ex buffer = recursion(H(newparameter, arg).hold());
1682                                         if (is_a<add>(buffer)) {
1683                                                 for (int i=0; i<buffer.nops(); i++) {
1684                                                         res += trafo_H_1tx_prepend_zero(buffer.op(i), arg);
1685                                                 }
1686                                         } else {
1687                                                 res += trafo_H_1tx_prepend_zero(buffer, arg);
1688                                         }
1689                                         return res;
1690
1691                                 } else if (parameter.op(0) == -1) {
1692
1693                                         // leading negative one
1694                                         ex res = convert_H_to_zeta(parameter);
1695                                         map_trafo_H_1overx recursion;
1696                                         ex buffer = recursion(H(newparameter, arg).hold());
1697                                         if (is_a<add>(buffer)) {
1698                                                 for (int i=0; i<buffer.nops(); i++) {
1699                                                         res += trafo_H_1tx_prepend_zero(buffer.op(i), arg) - trafo_H_1tx_prepend_minusone(buffer.op(i), arg);
1700                                                 }
1701                                         } else {
1702                                                 res += trafo_H_1tx_prepend_zero(buffer, arg) - trafo_H_1tx_prepend_minusone(buffer, arg);
1703                                         }
1704                                         return res;
1705
1706                                 } else {
1707
1708                                         // leading one
1709                                         map_trafo_H_1overx recursion;
1710                                         map_trafo_H_mult unify;
1711                                         ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
1712                                         int firstzero = 0;
1713                                         while (parameter.op(firstzero) == 1) {
1714                                                 firstzero++;
1715                                         }
1716                                         for (int i=firstzero-1; i<parameter.nops()-1; i++) {
1717                                                 lst newparameter;
1718                                                 int j=0;
1719                                                 for (; j<=i; j++) {
1720                                                         newparameter.append(parameter[j+1]);
1721                                                 }
1722                                                 newparameter.append(1);
1723                                                 for (; j<parameter.nops()-1; j++) {
1724                                                         newparameter.append(parameter[j+1]);
1725                                                 }
1726                                                 res -= H(newparameter, arg).hold();
1727                                         }
1728                                         res = recursion(res).expand() / firstzero;
1729                                         return unify(res);
1730
1731                                 }
1732
1733                         }
1734                 }
1735                 return e;
1736         }
1737 };
1738
1739
1740 // do x -> (1-x)/(1+x) transformation
1741 struct map_trafo_H_1mxt1px : public map_function
1742 {
1743         ex operator()(const ex& e)
1744         {
1745                 if (is_a<add>(e) || is_a<mul>(e)) {
1746                         return e.map(*this);
1747                 }
1748
1749                 if (is_a<function>(e)) {
1750                         std::string name = ex_to<function>(e).get_name();
1751                         if (name == "H") {
1752
1753                                 lst parameter = ex_to<lst>(e.op(0));
1754                                 ex arg = e.op(1);
1755
1756                                 // special cases if all parameters are either 0, 1 or -1
1757                                 bool allthesame = true;
1758                                 if (parameter.op(0) == 0) {
1759                                         for (int i=1; i<parameter.nops(); i++) {
1760                                                 if (parameter.op(i) != 0) {
1761                                                         allthesame = false;
1762                                                         break;
1763                                                 }
1764                                         }
1765                                         if (allthesame) {
1766                                                 map_trafo_H_mult unify;
1767                                                 return unify((pow(-H(lst(1),(1-arg)/(1+arg)).hold() - H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
1768                                                        / factorial(parameter.nops())).expand());
1769                                         }
1770                                 } else if (parameter.op(0) == -1) {
1771                                         for (int i=1; i<parameter.nops(); i++) {
1772                                                 if (parameter.op(i) != -1) {
1773                                                         allthesame = false;
1774                                                         break;
1775                                                 }
1776                                         }
1777                                         if (allthesame) {
1778                                                 map_trafo_H_mult unify;
1779                                                 return unify((pow(log(2) - H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
1780                                                        / factorial(parameter.nops())).expand());
1781                                         }
1782                                 } else {
1783                                         for (int i=1; i<parameter.nops(); i++) {
1784                                                 if (parameter.op(i) != 1) {
1785                                                         allthesame = false;
1786                                                         break;
1787                                                 }
1788                                         }
1789                                         if (allthesame) {
1790                                                 map_trafo_H_mult unify;
1791                                                 return unify((pow(-log(2) - H(lst(0),(1-arg)/(1+arg)).hold() + H(lst(-1),(1-arg)/(1+arg)).hold(), parameter.nops())
1792                                                        / factorial(parameter.nops())).expand());
1793                                         }
1794                                 }
1795
1796                                 lst newparameter = parameter;
1797                                 newparameter.remove_first();
1798
1799                                 if (parameter.op(0) == 0) {
1800
1801                                         // leading zero
1802                                         ex res = convert_H_to_zeta(parameter);
1803                                         map_trafo_H_1mxt1px recursion;
1804                                         ex buffer = recursion(H(newparameter, arg).hold());
1805                                         if (is_a<add>(buffer)) {
1806                                                 for (int i=0; i<buffer.nops(); i++) {
1807                                                         res -= trafo_H_1mxt1px_prepend_one(buffer.op(i), arg) + trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
1808                                                 }
1809                                         } else {
1810                                                 res -= trafo_H_1mxt1px_prepend_one(buffer, arg) + trafo_H_1mxt1px_prepend_minusone(buffer, arg);
1811                                         }
1812                                         return res;
1813
1814                                 } else if (parameter.op(0) == -1) {
1815
1816                                         // leading negative one
1817                                         ex res = convert_H_to_zeta(parameter);
1818                                         map_trafo_H_1mxt1px recursion;
1819                                         ex buffer = recursion(H(newparameter, arg).hold());
1820                                         if (is_a<add>(buffer)) {
1821                                                 for (int i=0; i<buffer.nops(); i++) {
1822                                                         res -= trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
1823                                                 }
1824                                         } else {
1825                                                 res -= trafo_H_1mxt1px_prepend_minusone(buffer, arg);
1826                                         }
1827                                         return res;
1828
1829                                 } else {
1830
1831                                         // leading one
1832                                         map_trafo_H_1mxt1px recursion;
1833                                         map_trafo_H_mult unify;
1834                                         ex res = H(lst(1), arg).hold() * H(newparameter, arg).hold();
1835                                         int firstzero = 0;
1836                                         while (parameter.op(firstzero) == 1) {
1837                                                 firstzero++;
1838                                         }
1839                                         for (int i=firstzero-1; i<parameter.nops()-1; i++) {
1840                                                 lst newparameter;
1841                                                 int j=0;
1842                                                 for (; j<=i; j++) {
1843                                                         newparameter.append(parameter[j+1]);
1844                                                 }
1845                                                 newparameter.append(1);
1846                                                 for (; j<parameter.nops()-1; j++) {
1847                                                         newparameter.append(parameter[j+1]);
1848                                                 }
1849                                                 res -= H(newparameter, arg).hold();
1850                                         }
1851                                         res = recursion(res).expand() / firstzero;
1852                                         return unify(res);
1853
1854                                 }
1855
1856                         }
1857                 }
1858                 return e;
1859         }
1860 };
1861
1862
1863 // do the actual summation.
1864 cln::cl_N H_do_sum(const std::vector<int>& m, const cln::cl_N& x)
1865 {
1866         const int j = m.size();
1867
1868         std::vector<cln::cl_N> t(j);
1869
1870         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
1871         cln::cl_N factor = cln::expt(x, j) * one;
1872         cln::cl_N t0buf;
1873         int q = 0;
1874         do {
1875                 t0buf = t[0];
1876                 q++;
1877                 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),m[j-1]);
1878                 for (int k=j-2; k>=1; k--) {
1879                         t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), m[k]);
1880                 }
1881                 t[0] = t[0] + t[1] * factor / cln::expt(cln::cl_I(q+j-1), m[0]);
1882                 factor = factor * x;
1883         } while (t[0] != t0buf);
1884
1885         return t[0];
1886 }
1887
1888
1889 } // end of anonymous namespace
1890
1891
1892 //////////////////////////////////////////////////////////////////////
1893 //
1894 // Harmonic polylogarithm  H(m,x)
1895 //
1896 // GiNaC function
1897 //
1898 //////////////////////////////////////////////////////////////////////
1899
1900
1901 static ex H_evalf(const ex& x1, const ex& x2)
1902 {
1903         if (is_a<lst>(x1) && is_a<numeric>(x2)) {
1904                 for (int i=0; i<x1.nops(); i++) {
1905                         if (!x1.op(i).info(info_flags::integer)) {
1906                                 return H(x1,x2).hold();
1907                         }
1908                 }
1909                 if (x1.nops() < 1) {
1910                         return H(x1,x2).hold();
1911                 }
1912
1913                 cln::cl_N x = ex_to<numeric>(x2).to_cl_N();
1914                 
1915                 const lst& morg = ex_to<lst>(x1);
1916                 // remove trailing zeros ...
1917                 if (*(--morg.end()) == 0) {
1918                         symbol xtemp("xtemp");
1919                         map_trafo_H_reduce_trailing_zeros filter;
1920                         return filter(H(x1, xtemp).hold()).subs(xtemp==x2).evalf();
1921                 }
1922                 // ... and expand parameter notation
1923                 lst m;
1924                 for (lst::const_iterator it = morg.begin(); it != morg.end(); it++) {
1925                         if (*it > 1) {
1926                                 for (ex count=*it-1; count > 0; count--) {
1927                                         m.append(0);
1928                                 }
1929                                 m.append(1);
1930                         } else if (*it < -1) {
1931                                 for (ex count=*it+1; count < 0; count++) {
1932                                         m.append(0);
1933                                 }
1934                                 m.append(-1);
1935                         } else {
1936                                 m.append(*it);
1937                         }
1938                 }
1939
1940                 // since the transformations produce a lot of terms, they are only efficient for
1941                 // argument near one.
1942                 // no transformation needed -> do summation
1943                 if (cln::abs(x) < 0.95) {
1944                         lst m_lst;
1945                         lst s_lst;
1946                         ex pf;
1947                         if (convert_parameter_H_to_Li(m, m_lst, s_lst, pf)) {
1948                                 // negative parameters -> s_lst is filled
1949                                 std::vector<int> m_int;
1950                                 std::vector<cln::cl_N> x_cln;
1951                                 for (lst::const_iterator it_int = m_lst.begin(), it_cln = s_lst.begin(); 
1952                                      it_int != m_lst.end(); it_int++, it_cln++) {
1953                                         m_int.push_back(ex_to<numeric>(*it_int).to_int());
1954                                         x_cln.push_back(ex_to<numeric>(*it_cln).to_cl_N());
1955                                 }
1956                                 x_cln.front() = x_cln.front() * x;
1957                                 return pf * numeric(multipleLi_do_sum(m_int, x_cln));
1958                         } else {
1959                                 // only positive parameters
1960                                 //TODO
1961                                 if (m_lst.nops() == 1) {
1962                                         return Li(m_lst.op(0), x2).evalf();
1963                                 }
1964                                 std::vector<int> m_int;
1965                                 for (lst::const_iterator it = m_lst.begin(); it != m_lst.end(); it++) {
1966                                         m_int.push_back(ex_to<numeric>(*it).to_int());
1967                                 }
1968                                 return numeric(H_do_sum(m_int, x));
1969                         }
1970                 }
1971
1972                 ex res = 1;     
1973                 
1974                 // ensure that the realpart of the argument is positive
1975                 if (cln::realpart(x) < 0) {
1976                         x = -x;
1977                         for (int i=0; i<m.nops(); i++) {
1978                                 if (m.op(i) != 0) {
1979                                         m.let_op(i) = -m.op(i);
1980                                         res *= -1;
1981                                 }
1982                         }
1983                 }
1984
1985                 // choose transformations
1986                 symbol xtemp("xtemp");
1987                 if (cln::abs(x-1) < 1.4142) {
1988                         // x -> (1-x)/(1+x)
1989                         map_trafo_H_1mxt1px trafo;
1990                         res *= trafo(H(m, xtemp));
1991                 } else {
1992                         // x -> 1/x
1993                         map_trafo_H_1overx trafo;
1994                         res *= trafo(H(m, xtemp));
1995                         if (cln::imagpart(x) <= 0) {
1996                                 res = res.subs(H_polesign == -I*Pi);
1997                         } else {
1998                                 res = res.subs(H_polesign == I*Pi);
1999                         }
2000                 }
2001
2002                 // simplify result
2003 // TODO
2004 //              map_trafo_H_convert converter;
2005 //              res = converter(res).expand();
2006 //              lst ll;
2007 //              res.find(H(wild(1),wild(2)), ll);
2008 //              res.find(zeta(wild(1)), ll);
2009 //              res.find(zeta(wild(1),wild(2)), ll);
2010 //              res = res.collect(ll);
2011
2012                 return res.subs(xtemp == numeric(x)).evalf();
2013         }
2014
2015         return H(x1,x2).hold();
2016 }
2017
2018
2019 static ex H_eval(const ex& m_, const ex& x)
2020 {
2021         lst m;
2022         if (is_a<lst>(m_)) {
2023                 m = ex_to<lst>(m_);
2024         } else {
2025                 m = lst(m_);
2026         }
2027         if (m.nops() == 0) {
2028                 return _ex1;
2029         }
2030         ex pos1;
2031         ex pos2;
2032         ex n;
2033         ex p;
2034         int step = 0;
2035         if (*m.begin() > _ex1) {
2036                 step++;
2037                 pos1 = _ex0;
2038                 pos2 = _ex1;
2039                 n = *m.begin()-1;
2040                 p = _ex1;
2041         } else if (*m.begin() < _ex_1) {
2042                 step++;
2043                 pos1 = _ex0;
2044                 pos2 = _ex_1;
2045                 n = -*m.begin()-1;
2046                 p = _ex1;
2047         } else if (*m.begin() == _ex0) {
2048                 pos1 = _ex0;
2049                 n = _ex1;
2050         } else {
2051                 pos1 = *m.begin();
2052                 p = _ex1;
2053         }
2054         for (lst::const_iterator it = ++m.begin(); it != m.end(); it++) {
2055                 if ((*it).info(info_flags::integer)) {
2056                         if (step == 0) {
2057                                 if (*it > _ex1) {
2058                                         if (pos1 == _ex0) {
2059                                                 step = 1;
2060                                                 pos2 = _ex1;
2061                                                 n += *it-1;
2062                                                 p = _ex1;
2063                                         } else {
2064                                                 step = 2;
2065                                         }
2066                                 } else if (*it < _ex_1) {
2067                                         if (pos1 == _ex0) {
2068                                                 step = 1;
2069                                                 pos2 = _ex_1;
2070                                                 n += -*it-1;
2071                                                 p = _ex1;
2072                                         } else {
2073                                                 step = 2;
2074                                         }
2075                                 } else {
2076                                         if (*it != pos1) {
2077                                                 step = 1;
2078                                                 pos2 = *it;
2079                                         }
2080                                         if (*it == _ex0) {
2081                                                 n++;
2082                                         } else {
2083                                                 p++;
2084                                         }
2085                                 }
2086                         } else if (step == 1) {
2087                                 if (*it != pos2) {
2088                                         step = 2;
2089                                 } else {
2090                                         if (*it == _ex0) {
2091                                                 n++;
2092                                         } else {
2093                                                 p++;
2094                                         }
2095                                 }
2096                         }
2097                 } else {
2098                         // if some m_i is not an integer
2099                         return H(m_, x).hold();
2100                 }
2101         }
2102         if ((x == _ex1) && (*(--m.end()) != _ex0)) {
2103                 return convert_H_to_zeta(m);
2104         }
2105         if (step == 0) {
2106                 if (pos1 == _ex0) {
2107                         // all zero
2108                         if (x == _ex0) {
2109                                 return H(m_, x).hold();
2110                         }
2111                         return pow(log(x), m.nops()) / factorial(m.nops());
2112                 } else {
2113                         // all (minus) one
2114                         return pow(-pos1*log(1-pos1*x), m.nops()) / factorial(m.nops());
2115                 }
2116         } else if ((step == 1) && (pos1 == _ex0)){
2117                 // convertible to S
2118                 if (pos2 == _ex1) {
2119                         return S(n, p, x);
2120                 } else {
2121                         return pow(-1, p) * S(n, p, -x);
2122                 }
2123         }
2124         if (x == _ex0) {
2125                 return _ex0;
2126         }
2127         if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
2128                 return H(m_, x).evalf();
2129         }
2130         return H(m_, x).hold();
2131 }
2132
2133
2134 static ex H_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
2135 {
2136         epvector seq;
2137         seq.push_back(expair(H(m, x), 0));
2138         return pseries(rel, seq);
2139 }
2140
2141
2142 static ex H_deriv(const ex& m_, const ex& x, unsigned deriv_param)
2143 {
2144         GINAC_ASSERT(deriv_param < 2);
2145         if (deriv_param == 0) {
2146                 return _ex0;
2147         }
2148         lst m;
2149         if (is_a<lst>(m_)) {
2150                 m = ex_to<lst>(m_);
2151         } else {
2152                 m = lst(m_);
2153         }
2154         ex mb = *m.begin();
2155         if (mb > _ex1) {
2156                 m[0]--;
2157                 return H(m, x) / x;
2158         }
2159         if (mb < _ex_1) {
2160                 m[0]++;
2161                 return H(m, x) / x;
2162         }
2163         m.remove_first();
2164         if (mb == _ex1) {
2165                 return 1/(1-x) * H(m, x);
2166         } else if (mb == _ex_1) {
2167                 return 1/(1+x) * H(m, x);
2168         } else {
2169                 return H(m, x) / x;
2170         }
2171 }
2172
2173
2174 static void H_print_latex(const ex& m_, const ex& x, const print_context& c)
2175 {
2176         lst m;
2177         if (is_a<lst>(m_)) {
2178                 m = ex_to<lst>(m_);
2179         } else {
2180                 m = lst(m_);
2181         }
2182         c.s << "\\mbox{H}_{";
2183         lst::const_iterator itm = m.begin();
2184         (*itm).print(c);
2185         itm++;
2186         for (; itm != m.end(); itm++) {
2187                 c.s << ",";
2188                 (*itm).print(c);
2189         }
2190         c.s << "}(";
2191         x.print(c);
2192         c.s << ")";
2193 }
2194
2195
2196 REGISTER_FUNCTION(H,
2197                   evalf_func(H_evalf).
2198                   eval_func(H_eval).
2199                   series_func(H_series).
2200                   derivative_func(H_deriv).
2201                   print_func<print_latex>(H_print_latex).
2202                   do_not_evalf_params());
2203
2204
2205 // takes a parameter list for H and returns an expression with corresponding multiple polylogarithms
2206 ex convert_H_to_Li(const ex& m, const ex& x)
2207 {
2208         map_trafo_H_reduce_trailing_zeros filter;
2209         map_trafo_H_convert_to_Li filter2;
2210         if (is_a<lst>(m)) {
2211                 return filter2(filter(H(m, x).hold()));
2212         } else {
2213                 return filter2(filter(H(lst(m), x).hold()));
2214         }
2215 }
2216
2217
2218 //////////////////////////////////////////////////////////////////////
2219 //
2220 // Multiple zeta values  zeta(x) and zeta(x,s)
2221 //
2222 // helper functions
2223 //
2224 //////////////////////////////////////////////////////////////////////
2225
2226
2227 // anonymous namespace for helper functions
2228 namespace {
2229
2230
2231 // parameters and data for [Cra] algorithm
2232 const cln::cl_N lambda = cln::cl_N("319/320");
2233 int L1;
2234 int L2;
2235 std::vector<std::vector<cln::cl_N> > f_kj;
2236 std::vector<cln::cl_N> crB;
2237 std::vector<std::vector<cln::cl_N> > crG;
2238 std::vector<cln::cl_N> crX;
2239
2240
2241 void halfcyclic_convolute(const std::vector<cln::cl_N>& a, const std::vector<cln::cl_N>& b, std::vector<cln::cl_N>& c)
2242 {
2243         const int size = a.size();
2244         for (int n=0; n<size; n++) {
2245                 c[n] = 0;
2246                 for (int m=0; m<=n; m++) {
2247                         c[n] = c[n] + a[m]*b[n-m];
2248                 }
2249         }
2250 }
2251
2252
2253 // [Cra] section 4
2254 void initcX(const std::vector<int>& s)
2255 {
2256         const int k = s.size();
2257
2258         crX.clear();
2259         crG.clear();
2260         crB.clear();
2261
2262         for (int i=0; i<=L2; i++) {
2263                 crB.push_back(bernoulli(i).to_cl_N() / cln::factorial(i));
2264         }
2265
2266         int Sm = 0;
2267         int Smp1 = 0;
2268         for (int m=0; m<k-1; m++) {
2269                 std::vector<cln::cl_N> crGbuf;
2270                 Sm = Sm + s[m];
2271                 Smp1 = Sm + s[m+1];
2272                 for (int i=0; i<=L2; i++) {
2273                         crGbuf.push_back(cln::factorial(i + Sm - m - 2) / cln::factorial(i + Smp1 - m - 2));
2274                 }
2275                 crG.push_back(crGbuf);
2276         }
2277
2278         crX = crB;
2279
2280         for (int m=0; m<k-1; m++) {
2281                 std::vector<cln::cl_N> Xbuf;
2282                 for (int i=0; i<=L2; i++) {
2283                         Xbuf.push_back(crX[i] * crG[m][i]);
2284                 }
2285                 halfcyclic_convolute(Xbuf, crB, crX);
2286         }
2287 }
2288
2289
2290 // [Cra] section 4
2291 cln::cl_N crandall_Y_loop(const cln::cl_N& Sqk)
2292 {
2293         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
2294         cln::cl_N factor = cln::expt(lambda, Sqk);
2295         cln::cl_N res = factor / Sqk * crX[0] * one;
2296         cln::cl_N resbuf;
2297         int N = 0;
2298         do {
2299                 resbuf = res;
2300                 factor = factor * lambda;
2301                 N++;
2302                 res = res + crX[N] * factor / (N+Sqk);
2303         } while ((res != resbuf) || cln::zerop(crX[N]));
2304         return res;
2305 }
2306
2307
2308 // [Cra] section 4
2309 void calc_f(int maxr)
2310 {
2311         f_kj.clear();
2312         f_kj.resize(L1);
2313         
2314         cln::cl_N t0, t1, t2, t3, t4;
2315         int i, j, k;
2316         std::vector<std::vector<cln::cl_N> >::iterator it = f_kj.begin();
2317         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
2318         
2319         t0 = cln::exp(-lambda);
2320         t2 = 1;
2321         for (k=1; k<=L1; k++) {
2322                 t1 = k * lambda;
2323                 t2 = t0 * t2;
2324                 for (j=1; j<=maxr; j++) {
2325                         t3 = 1;
2326                         t4 = 1;
2327                         for (i=2; i<=j; i++) {
2328                                 t4 = t4 * (j-i+1);
2329                                 t3 = t1 * t3 + t4;
2330                         }
2331                         (*it).push_back(t2 * t3 * cln::expt(cln::cl_I(k),-j) * one);
2332                 }
2333                 it++;
2334         }
2335 }
2336
2337
2338 // [Cra] (3.1)
2339 cln::cl_N crandall_Z(const std::vector<int>& s)
2340 {
2341         const int j = s.size();
2342
2343         if (j == 1) {   
2344                 cln::cl_N t0;
2345                 cln::cl_N t0buf;
2346                 int q = 0;
2347                 do {
2348                         t0buf = t0;
2349                         q++;
2350                         t0 = t0 + f_kj[q+j-2][s[0]-1];
2351                 } while (t0 != t0buf);
2352                 
2353                 return t0 / cln::factorial(s[0]-1);
2354         }
2355
2356         std::vector<cln::cl_N> t(j);
2357
2358         cln::cl_N t0buf;
2359         int q = 0;
2360         do {
2361                 t0buf = t[0];
2362                 q++;
2363                 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),s[j-1]);
2364                 for (int k=j-2; k>=1; k--) {
2365                         t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), s[k]);
2366                 }
2367                 t[0] = t[0] + t[1] * f_kj[q+j-2][s[0]-1];
2368         } while (t[0] != t0buf);
2369         
2370         return t[0] / cln::factorial(s[0]-1);
2371 }
2372
2373
2374 // [Cra] (2.4)
2375 cln::cl_N zeta_do_sum_Crandall(const std::vector<int>& s)
2376 {
2377         std::vector<int> r = s;
2378         const int j = r.size();
2379
2380         // decide on maximal size of f_kj for crandall_Z
2381         if (Digits < 50) {
2382                 L1 = 150;
2383         } else {
2384                 L1 = Digits * 3 + j*2;
2385         }
2386
2387         // decide on maximal size of crX for crandall_Y
2388         if (Digits < 38) {
2389                 L2 = 63;
2390         } else if (Digits < 86) {
2391                 L2 = 127;
2392         } else if (Digits < 192) {
2393                 L2 = 255;
2394         } else if (Digits < 394) {
2395                 L2 = 511;
2396         } else if (Digits < 808) {
2397                 L2 = 1023;
2398         } else {
2399                 L2 = 2047;
2400         }
2401
2402         cln::cl_N res;
2403
2404         int maxr = 0;
2405         int S = 0;
2406         for (int i=0; i<j; i++) {
2407                 S += r[i];
2408                 if (r[i] > maxr) {
2409                         maxr = r[i];
2410                 }
2411         }
2412
2413         calc_f(maxr);
2414
2415         const cln::cl_N r0factorial = cln::factorial(r[0]-1);
2416
2417         std::vector<int> rz;
2418         int skp1buf;
2419         int Srun = S;
2420         for (int k=r.size()-1; k>0; k--) {
2421
2422                 rz.insert(rz.begin(), r.back());
2423                 skp1buf = rz.front();
2424                 Srun -= skp1buf;
2425                 r.pop_back();
2426
2427                 initcX(r);
2428                 
2429                 for (int q=0; q<skp1buf; q++) {
2430                         
2431                         cln::cl_N pp1 = crandall_Y_loop(Srun+q-k);
2432                         cln::cl_N pp2 = crandall_Z(rz);
2433
2434                         rz.front()--;
2435                         
2436                         if (q & 1) {
2437                                 res = res - pp1 * pp2 / cln::factorial(q);
2438                         } else {
2439                                 res = res + pp1 * pp2 / cln::factorial(q);
2440                         }
2441                 }
2442                 rz.front() = skp1buf;
2443         }
2444         rz.insert(rz.begin(), r.back());
2445
2446         initcX(rz);
2447
2448         res = (res + crandall_Y_loop(S-j)) / r0factorial + crandall_Z(rz);
2449
2450         return res;
2451 }
2452
2453
2454 cln::cl_N zeta_do_sum_simple(const std::vector<int>& r)
2455 {
2456         const int j = r.size();
2457
2458         // buffer for subsums
2459         std::vector<cln::cl_N> t(j);
2460         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
2461
2462         cln::cl_N t0buf;
2463         int q = 0;
2464         do {
2465                 t0buf = t[0];
2466                 q++;
2467                 t[j-1] = t[j-1] + one / cln::expt(cln::cl_I(q),r[j-1]);
2468                 for (int k=j-2; k>=0; k--) {
2469                         t[k] = t[k] + one * t[k+1] / cln::expt(cln::cl_I(q+j-1-k), r[k]);
2470                 }
2471         } while (t[0] != t0buf);
2472
2473         return t[0];
2474 }
2475
2476
2477 // does Hoelder convolution. see [BBB] (7.0)
2478 cln::cl_N zeta_do_Hoelder_convolution(const std::vector<int>& m_, const std::vector<int>& s_)
2479 {
2480         // prepare parameters
2481         // holds Li arguments in [BBB] notation
2482         std::vector<int> s = s_;
2483         std::vector<int> m_p = m_;
2484         std::vector<int> m_q;
2485         // holds Li arguments in nested sums notation
2486         std::vector<cln::cl_N> s_p(s.size(), cln::cl_N(1));
2487         s_p[0] = s_p[0] * cln::cl_N("1/2");
2488         // convert notations
2489         int sig = 1;
2490         for (int i=0; i<s_.size(); i++) {
2491                 if (s_[i] < 0) {
2492                         sig = -sig;
2493                         s_p[i] = -s_p[i];
2494                 }
2495                 s[i] = sig * std::abs(s[i]);
2496         }
2497         std::vector<cln::cl_N> s_q;
2498         cln::cl_N signum = 1;
2499
2500         // first term
2501         cln::cl_N res = multipleLi_do_sum(m_p, s_p);
2502
2503         // middle terms
2504         do {
2505
2506                 // change parameters
2507                 if (s.front() > 0) {
2508                         if (m_p.front() == 1) {
2509                                 m_p.erase(m_p.begin());
2510                                 s_p.erase(s_p.begin());
2511                                 if (s_p.size() > 0) {
2512                                         s_p.front() = s_p.front() * cln::cl_N("1/2");
2513                                 }
2514                                 s.erase(s.begin());
2515                                 m_q.front()++;
2516                         } else {
2517                                 m_p.front()--;
2518                                 m_q.insert(m_q.begin(), 1);
2519                                 if (s_q.size() > 0) {
2520                                         s_q.front() = s_q.front() * 2;
2521                                 }
2522                                 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
2523                         }
2524                 } else {
2525                         if (m_p.front() == 1) {
2526                                 m_p.erase(m_p.begin());
2527                                 cln::cl_N spbuf = s_p.front();
2528                                 s_p.erase(s_p.begin());
2529                                 if (s_p.size() > 0) {
2530                                         s_p.front() = s_p.front() * spbuf;
2531                                 }
2532                                 s.erase(s.begin());
2533                                 m_q.insert(m_q.begin(), 1);
2534                                 if (s_q.size() > 0) {
2535                                         s_q.front() = s_q.front() * 4;
2536                                 }
2537                                 s_q.insert(s_q.begin(), cln::cl_N("1/4"));
2538                                 signum = -signum;
2539                         } else {
2540                                 m_p.front()--;
2541                                 m_q.insert(m_q.begin(), 1);
2542                                 if (s_q.size() > 0) {
2543                                         s_q.front() = s_q.front() * 2;
2544                                 }
2545                                 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
2546                         }
2547                 }
2548
2549                 // exiting the loop
2550                 if (m_p.size() == 0) break;
2551
2552                 res = res + signum * multipleLi_do_sum(m_p, s_p) * multipleLi_do_sum(m_q, s_q);
2553
2554         } while (true);
2555
2556         // last term
2557         res = res + signum * multipleLi_do_sum(m_q, s_q);
2558
2559         return res;
2560 }
2561
2562
2563 } // end of anonymous namespace
2564
2565
2566 //////////////////////////////////////////////////////////////////////
2567 //
2568 // Multiple zeta values  zeta(x)
2569 //
2570 // GiNaC function
2571 //
2572 //////////////////////////////////////////////////////////////////////
2573
2574
2575 static ex zeta1_evalf(const ex& x)
2576 {
2577         if (is_exactly_a<lst>(x) && (x.nops()>1)) {
2578
2579                 // multiple zeta value
2580                 const int count = x.nops();
2581                 const lst& xlst = ex_to<lst>(x);
2582                 std::vector<int> r(count);
2583
2584                 // check parameters and convert them
2585                 lst::const_iterator it1 = xlst.begin();
2586                 std::vector<int>::iterator it2 = r.begin();
2587                 do {
2588                         if (!(*it1).info(info_flags::posint)) {
2589                                 return zeta(x).hold();
2590                         }
2591                         *it2 = ex_to<numeric>(*it1).to_int();
2592                         it1++;
2593                         it2++;
2594                 } while (it2 != r.end());
2595
2596                 // check for divergence
2597                 if (r[0] == 1) {
2598                         return zeta(x).hold();
2599                 }
2600
2601                 // decide on summation algorithm
2602                 // this is still a bit clumsy
2603                 int limit = (Digits>17) ? 10 : 6;
2604                 if ((r[0] < limit) || ((count > 3) && (r[1] < limit/2))) {
2605                         return numeric(zeta_do_sum_Crandall(r));
2606                 } else {
2607                         return numeric(zeta_do_sum_simple(r));
2608                 }
2609         }
2610
2611         // single zeta value
2612         if (is_exactly_a<numeric>(x) && (x != 1)) {
2613                 try {
2614                         return zeta(ex_to<numeric>(x));
2615                 } catch (const dunno &e) { }
2616         }
2617
2618         return zeta(x).hold();
2619 }
2620
2621
2622 static ex zeta1_eval(const ex& m)
2623 {
2624         if (is_exactly_a<lst>(m)) {
2625                 if (m.nops() == 1) {
2626                         return zeta(m.op(0));
2627                 }
2628                 return zeta(m).hold();
2629         }
2630
2631         if (m.info(info_flags::numeric)) {
2632                 const numeric& y = ex_to<numeric>(m);
2633                 // trap integer arguments:
2634                 if (y.is_integer()) {
2635                         if (y.is_zero()) {
2636                                 return _ex_1_2;
2637                         }
2638                         if (y.is_equal(_num1)) {
2639                                 return zeta(m).hold();
2640                         }
2641                         if (y.info(info_flags::posint)) {
2642                                 if (y.info(info_flags::odd)) {
2643                                         return zeta(m).hold();
2644                                 } else {
2645                                         return abs(bernoulli(y)) * pow(Pi, y) * pow(_num2, y-_num1) / factorial(y);
2646                                 }
2647                         } else {
2648                                 if (y.info(info_flags::odd)) {
2649                                         return -bernoulli(_num1-y) / (_num1-y);
2650                                 } else {
2651                                         return _ex0;
2652                                 }
2653                         }
2654                 }
2655                 // zeta(float)
2656                 if (y.info(info_flags::numeric) && !y.info(info_flags::crational)) {
2657                         return zeta1_evalf(m);
2658                 }
2659         }
2660         return zeta(m).hold();
2661 }
2662
2663
2664 static ex zeta1_deriv(const ex& m, unsigned deriv_param)
2665 {
2666         GINAC_ASSERT(deriv_param==0);
2667
2668         if (is_exactly_a<lst>(m)) {
2669                 return _ex0;
2670         } else {
2671                 return zetaderiv(_ex1, m);
2672         }
2673 }
2674
2675
2676 static void zeta1_print_latex(const ex& m_, const print_context& c)
2677 {
2678         c.s << "\\zeta(";
2679         if (is_a<lst>(m_)) {
2680                 const lst& m = ex_to<lst>(m_);
2681                 lst::const_iterator it = m.begin();
2682                 (*it).print(c);
2683                 it++;
2684                 for (; it != m.end(); it++) {
2685                         c.s << ",";
2686                         (*it).print(c);
2687                 }
2688         } else {
2689                 m_.print(c);
2690         }
2691         c.s << ")";
2692 }
2693
2694
2695 unsigned zeta1_SERIAL::serial = function::register_new(function_options("zeta", 1).
2696                                 evalf_func(zeta1_evalf).
2697                                 eval_func(zeta1_eval).
2698                                 derivative_func(zeta1_deriv).
2699                                 print_func<print_latex>(zeta1_print_latex).
2700                                 do_not_evalf_params().
2701                                 overloaded(2));
2702
2703
2704 //////////////////////////////////////////////////////////////////////
2705 //
2706 // Alternating Euler sum  zeta(x,s)
2707 //
2708 // GiNaC function
2709 //
2710 //////////////////////////////////////////////////////////////////////
2711
2712
2713 static ex zeta2_evalf(const ex& x, const ex& s)
2714 {
2715         if (is_exactly_a<lst>(x)) {
2716
2717                 // alternating Euler sum
2718                 const int count = x.nops();
2719                 const lst& xlst = ex_to<lst>(x);
2720                 const lst& slst = ex_to<lst>(s);
2721                 std::vector<int> xi(count);
2722                 std::vector<int> si(count);
2723
2724                 // check parameters and convert them
2725                 lst::const_iterator it_xread = xlst.begin();
2726                 lst::const_iterator it_sread = slst.begin();
2727                 std::vector<int>::iterator it_xwrite = xi.begin();
2728                 std::vector<int>::iterator it_swrite = si.begin();
2729                 do {
2730                         if (!(*it_xread).info(info_flags::posint)) {
2731                                 return zeta(x, s).hold();
2732                         }
2733                         *it_xwrite = ex_to<numeric>(*it_xread).to_int();
2734                         if (*it_sread > 0) {
2735                                 *it_swrite = 1;
2736                         } else {
2737                                 *it_swrite = -1;
2738                         }
2739                         it_xread++;
2740                         it_sread++;
2741                         it_xwrite++;
2742                         it_swrite++;
2743                 } while (it_xwrite != xi.end());
2744
2745                 // check for divergence
2746                 if ((xi[0] == 1) && (si[0] == 1)) {
2747                         return zeta(x, s).hold();
2748                 }
2749
2750                 // use Hoelder convolution
2751                 return numeric(zeta_do_Hoelder_convolution(xi, si));
2752         }
2753
2754         return zeta(x, s).hold();
2755 }
2756
2757
2758 static ex zeta2_eval(const ex& m, const ex& s_)
2759 {
2760         if (is_exactly_a<lst>(s_)) {
2761                 const lst& s = ex_to<lst>(s_);
2762                 for (lst::const_iterator it = s.begin(); it != s.end(); it++) {
2763                         if ((*it).info(info_flags::positive)) {
2764                                 continue;
2765                         }
2766                         return zeta(m, s_).hold();
2767                 }
2768                 return zeta(m);
2769         } else if (s_.info(info_flags::positive)) {
2770                 return zeta(m);
2771         }
2772
2773         return zeta(m, s_).hold();
2774 }
2775
2776
2777 static ex zeta2_deriv(const ex& m, const ex& s, unsigned deriv_param)
2778 {
2779         GINAC_ASSERT(deriv_param==0);
2780
2781         if (is_exactly_a<lst>(m)) {
2782                 return _ex0;
2783         } else {
2784                 if ((is_exactly_a<lst>(s) && s.op(0).info(info_flags::positive)) || s.info(info_flags::positive)) {
2785                         return zetaderiv(_ex1, m);
2786                 }
2787                 return _ex0;
2788         }
2789 }
2790
2791
2792 static void zeta2_print_latex(const ex& m_, const ex& s_, const print_context& c)
2793 {
2794         lst m;
2795         if (is_a<lst>(m_)) {
2796                 m = ex_to<lst>(m_);
2797         } else {
2798                 m = lst(m_);
2799         }
2800         lst s;
2801         if (is_a<lst>(s_)) {
2802                 s = ex_to<lst>(s_);
2803         } else {
2804                 s = lst(s_);
2805         }
2806         c.s << "\\zeta(";
2807         lst::const_iterator itm = m.begin();
2808         lst::const_iterator its = s.begin();
2809         if (*its < 0) {
2810                 c.s << "\\overline{";
2811                 (*itm).print(c);
2812                 c.s << "}";
2813         } else {
2814                 (*itm).print(c);
2815         }
2816         its++;
2817         itm++;
2818         for (; itm != m.end(); itm++, its++) {
2819                 c.s << ",";
2820                 if (*its < 0) {
2821                         c.s << "\\overline{";
2822                         (*itm).print(c);
2823                         c.s << "}";
2824                 } else {
2825                         (*itm).print(c);
2826                 }
2827         }
2828         c.s << ")";
2829 }
2830
2831
2832 unsigned zeta2_SERIAL::serial = function::register_new(function_options("zeta", 2).
2833                                 evalf_func(zeta2_evalf).
2834                                 eval_func(zeta2_eval).
2835                                 derivative_func(zeta2_deriv).
2836                                 print_func<print_latex>(zeta2_print_latex).
2837                                 do_not_evalf_params().
2838                                 overloaded(2));
2839
2840
2841 } // namespace GiNaC
2842