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