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