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