]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_nstdsums.cpp
Remove dependence on depreacted std::auto_ptr<T>.
[ginac.git] / ginac / inifcns_nstdsums.cpp
1 /** @file inifcns_nstdsums.cpp
2  *
3  *  Implementation of some special functions that have a representation as nested sums.
4  *
5  *  The functions are:
6  *    classical polylogarithm              Li(n,x)
7  *    multiple polylogarithm               Li(lst(m_1,...,m_k),lst(x_1,...,x_k))
8  *                                         G(lst(a_1,...,a_k),y) or G(lst(a_1,...,a_k),lst(s_1,...,s_k),y)
9  *    Nielsen's generalized polylogarithm  S(n,p,x)
10  *    harmonic polylogarithm               H(m,x) or H(lst(m_1,...,m_k),x)
11  *    multiple zeta value                  zeta(m) or zeta(lst(m_1,...,m_k))
12  *    alternating Euler sum                zeta(m,s) or zeta(lst(m_1,...,m_k),lst(s_1,...,s_k))
13  *
14  *  Some remarks:
15  *
16  *    - All formulae used can be looked up in the following publications:
17  *      [Kol] Nielsen's Generalized Polylogarithms, K.S.Kolbig, SIAM J.Math.Anal. 17 (1986), pp. 1232-1258.
18  *      [Cra] Fast Evaluation of Multiple Zeta Sums, R.E.Crandall, Math.Comp. 67 (1998), pp. 1163-1172.
19  *      [ReV] Harmonic Polylogarithms, E.Remiddi, J.A.M.Vermaseren, Int.J.Mod.Phys. A15 (2000), pp. 725-754
20  *      [BBB] Special Values of Multiple Polylogarithms, J.Borwein, D.Bradley, D.Broadhurst, P.Lisonek, Trans.Amer.Math.Soc. 353/3 (2001), pp. 907-941
21  *      [VSW] Numerical evaluation of multiple polylogarithms, J.Vollinga, S.Weinzierl, hep-ph/0410259
22  *
23  *    - The order of parameters and arguments of Li and zeta is defined according to the nested sums
24  *      representation. The parameters for H are understood as in [ReV]. They can be in expanded --- only
25  *      0, 1 and -1 --- or in compactified --- a string with zeros in front of 1 or -1 is written as a single
26  *      number --- notation.
27  *
28  *    - All functions can be numerically evaluated with arguments in the whole complex plane. The parameters
29  *      for Li, zeta and S must be positive integers. If you want to have an alternating Euler sum, you have
30  *      to give the signs of the parameters as a second argument s to zeta(m,s) containing 1 and -1.
31  *
32  *    - The calculation of classical polylogarithms is speeded up by using Bernoulli numbers and 
33  *      look-up tables. S uses look-up tables as well. The zeta function applies the algorithms in
34  *      [Cra] and [BBB] for speed up. Multiple polylogarithms use Hoelder convolution [BBB].
35  *
36  *    - The functions have no means to do a series expansion into nested sums. To do this, you have to convert
37  *      these functions into the appropriate objects from the nestedsums library, do the expansion and convert
38  *      the result back.
39  *
40  *    - Numerical testing of this implementation has been performed by doing a comparison of results
41  *      between this software and the commercial M.......... 4.1. Multiple zeta values have been checked
42  *      by means of evaluations into simple zeta values. Harmonic polylogarithms have been checked by
43  *      comparison to S(n,p,x) for corresponding parameter combinations and by continuity checks
44  *      around |x|=1 along with comparisons to corresponding zeta functions. Multiple polylogarithms were
45  *      checked against H and zeta and by means of shuffle and quasi-shuffle relations.
46  *
47  */
48
49 /*
50  *  GiNaC Copyright (C) 1999-2015 Johannes Gutenberg University Mainz, Germany
51  *
52  *  This program is free software; you can redistribute it and/or modify
53  *  it under the terms of the GNU General Public License as published by
54  *  the Free Software Foundation; either version 2 of the License, or
55  *  (at your option) any later version.
56  *
57  *  This program is distributed in the hope that it will be useful,
58  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
59  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
60  *  GNU General Public License for more details.
61  *
62  *  You should have received a copy of the GNU General Public License
63  *  along with this program; if not, write to the Free Software
64  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
65  */
66
67 #include "inifcns.h"
68
69 #include "add.h"
70 #include "constant.h"
71 #include "lst.h"
72 #include "mul.h"
73 #include "numeric.h"
74 #include "operators.h"
75 #include "power.h"
76 #include "pseries.h"
77 #include "relational.h"
78 #include "symbol.h"
79 #include "utils.h"
80 #include "wildcard.h"
81
82 #include <cln/cln.h>
83 #include <sstream>
84 #include <stdexcept>
85 #include <vector>
86
87 namespace GiNaC {
88
89
90 //////////////////////////////////////////////////////////////////////
91 //
92 // Classical polylogarithm  Li(n,x)
93 //
94 // helper functions
95 //
96 //////////////////////////////////////////////////////////////////////
97
98
99 // anonymous namespace for helper functions
100 namespace {
101
102
103 // lookup table for factors built from Bernoulli numbers
104 // see fill_Xn()
105 std::vector<std::vector<cln::cl_N> > Xn;
106 // initial size of Xn that should suffice for 32bit machines (must be even)
107 const int xninitsizestep = 26;
108 int xninitsize = xninitsizestep;
109 int xnsize = 0;
110
111
112 // This function calculates the X_n. The X_n are needed for speed up of classical polylogarithms.
113 // With these numbers the polylogs can be calculated as follows:
114 //   Li_p (x)  =  \sum_{n=0}^\infty X_{p-2}(n) u^{n+1}/(n+1)! with  u = -log(1-x)
115 //   X_0(n) = B_n (Bernoulli numbers)
116 //   X_p(n) = \sum_{k=0}^n binomial(n,k) B_{n-k} / (k+1) * X_{p-1}(k)
117 // The calculation of Xn depends on X0 and X{n-1}.
118 // X_0 is special, it holds only the non-zero Bernoulli numbers with index 2 or greater.
119 // This results in a slightly more complicated algorithm for the X_n.
120 // The first index in Xn corresponds to the index of the polylog minus 2.
121 // The second index in Xn corresponds to the index from the actual sum.
122 void fill_Xn(int n)
123 {
124         if (n>1) {
125                 // calculate X_2 and higher (corresponding to Li_4 and higher)
126                 std::vector<cln::cl_N> buf(xninitsize);
127                 std::vector<cln::cl_N>::iterator it = buf.begin();
128                 cln::cl_N result;
129                 *it = -(cln::expt(cln::cl_I(2),n+1) - 1) / cln::expt(cln::cl_I(2),n+1); // i == 1
130                 it++;
131                 for (int i=2; i<=xninitsize; i++) {
132                         if (i&1) {
133                                 result = 0; // k == 0
134                         } else {
135                                 result = Xn[0][i/2-1]; // k == 0
136                         }
137                         for (int k=1; k<i-1; k++) {
138                                 if ( !(((i-k) & 1) && ((i-k) > 1)) ) {
139                                         result = result + cln::binomial(i,k) * Xn[0][(i-k)/2-1] * Xn[n-1][k-1] / (k+1);
140                                 }
141                         }
142                         result = result - cln::binomial(i,i-1) * Xn[n-1][i-2] / 2 / i; // k == i-1
143                         result = result + Xn[n-1][i-1] / (i+1); // k == i
144                         
145                         *it = result;
146                         it++;
147                 }
148                 Xn.push_back(buf);
149         } else if (n==1) {
150                 // special case to handle the X_0 correct
151                 std::vector<cln::cl_N> buf(xninitsize);
152                 std::vector<cln::cl_N>::iterator it = buf.begin();
153                 cln::cl_N result;
154                 *it = cln::cl_I(-3)/cln::cl_I(4); // i == 1
155                 it++;
156                 *it = cln::cl_I(17)/cln::cl_I(36); // i == 2
157                 it++;
158                 for (int i=3; i<=xninitsize; i++) {
159                         if (i & 1) {
160                                 result = -Xn[0][(i-3)/2]/2;
161                                 *it = (cln::binomial(i,1)/cln::cl_I(2) + cln::binomial(i,i-1)/cln::cl_I(i))*result;
162                                 it++;
163                         } else {
164                                 result = Xn[0][i/2-1] + Xn[0][i/2-1]/(i+1);
165                                 for (int k=1; k<i/2; k++) {
166                                         result = result + cln::binomial(i,k*2) * Xn[0][k-1] * Xn[0][i/2-k-1] / (k*2+1);
167                                 }
168                                 *it = result;
169                                 it++;
170                         }
171                 }
172                 Xn.push_back(buf);
173         } else {
174                 // calculate X_0
175                 std::vector<cln::cl_N> buf(xninitsize/2);
176                 std::vector<cln::cl_N>::iterator it = buf.begin();
177                 for (int i=1; i<=xninitsize/2; i++) {
178                         *it = bernoulli(i*2).to_cl_N();
179                         it++;
180                 }
181                 Xn.push_back(buf);
182         }
183
184         xnsize++;
185 }
186
187 // doubles the number of entries in each Xn[]
188 void double_Xn()
189 {
190         const int pos0 = xninitsize / 2;
191         // X_0
192         for (int i=1; i<=xninitsizestep/2; ++i) {
193                 Xn[0].push_back(bernoulli((i+pos0)*2).to_cl_N());
194         }
195         if (Xn.size() > 1) {
196                 int xend = xninitsize + xninitsizestep;
197                 cln::cl_N result;
198                 // X_1
199                 for (int i=xninitsize+1; i<=xend; ++i) {
200                         if (i & 1) {
201                                 result = -Xn[0][(i-3)/2]/2;
202                                 Xn[1].push_back((cln::binomial(i,1)/cln::cl_I(2) + cln::binomial(i,i-1)/cln::cl_I(i))*result);
203                         } else {
204                                 result = Xn[0][i/2-1] + Xn[0][i/2-1]/(i+1);
205                                 for (int k=1; k<i/2; k++) {
206                                         result = result + cln::binomial(i,k*2) * Xn[0][k-1] * Xn[0][i/2-k-1] / (k*2+1);
207                                 }
208                                 Xn[1].push_back(result);
209                         }
210                 }
211                 // X_n
212                 for (size_t n=2; n<Xn.size(); ++n) {
213                         for (int i=xninitsize+1; i<=xend; ++i) {
214                                 if (i & 1) {
215                                         result = 0; // k == 0
216                                 } else {
217                                         result = Xn[0][i/2-1]; // k == 0
218                                 }
219                                 for (int k=1; k<i-1; ++k) {
220                                         if ( !(((i-k) & 1) && ((i-k) > 1)) ) {
221                                                 result = result + cln::binomial(i,k) * Xn[0][(i-k)/2-1] * Xn[n-1][k-1] / (k+1);
222                                         }
223                                 }
224                                 result = result - cln::binomial(i,i-1) * Xn[n-1][i-2] / 2 / i; // k == i-1
225                                 result = result + Xn[n-1][i-1] / (i+1); // k == i
226                                 Xn[n].push_back(result);
227                         }
228                 }
229         }
230         xninitsize += xninitsizestep;
231 }
232
233
234 // calculates Li(2,x) without Xn
235 cln::cl_N Li2_do_sum(const cln::cl_N& x)
236 {
237         cln::cl_N res = x;
238         cln::cl_N resbuf;
239         cln::cl_N num = x * cln::cl_float(1, cln::float_format(Digits));
240         cln::cl_I den = 1; // n^2 = 1
241         unsigned i = 3;
242         do {
243                 resbuf = res;
244                 num = num * x;
245                 den = den + i;  // n^2 = 4, 9, 16, ...
246                 i += 2;
247                 res = res + num / den;
248         } while (res != resbuf);
249         return res;
250 }
251
252
253 // calculates Li(2,x) with Xn
254 cln::cl_N Li2_do_sum_Xn(const cln::cl_N& x)
255 {
256         std::vector<cln::cl_N>::const_iterator it = Xn[0].begin();
257         std::vector<cln::cl_N>::const_iterator xend = Xn[0].end();
258         cln::cl_N u = -cln::log(1-x);
259         cln::cl_N factor = u * cln::cl_float(1, cln::float_format(Digits));
260         cln::cl_N uu = cln::square(u);
261         cln::cl_N res = u - uu/4;
262         cln::cl_N resbuf;
263         unsigned i = 1;
264         do {
265                 resbuf = res;
266                 factor = factor * uu / (2*i * (2*i+1));
267                 res = res + (*it) * factor;
268                 i++;
269                 if (++it == xend) {
270                         double_Xn();
271                         it = Xn[0].begin() + (i-1);
272                         xend = Xn[0].end();
273                 }
274         } while (res != resbuf);
275         return res;
276 }
277
278
279 // calculates Li(n,x), n>2 without Xn
280 cln::cl_N Lin_do_sum(int n, const cln::cl_N& x)
281 {
282         cln::cl_N factor = x * cln::cl_float(1, cln::float_format(Digits));
283         cln::cl_N res = x;
284         cln::cl_N resbuf;
285         int i=2;
286         do {
287                 resbuf = res;
288                 factor = factor * x;
289                 res = res + factor / cln::expt(cln::cl_I(i),n);
290                 i++;
291         } while (res != resbuf);
292         return res;
293 }
294
295
296 // calculates Li(n,x), n>2 with Xn
297 cln::cl_N Lin_do_sum_Xn(int n, const cln::cl_N& x)
298 {
299         std::vector<cln::cl_N>::const_iterator it = Xn[n-2].begin();
300         std::vector<cln::cl_N>::const_iterator xend = Xn[n-2].end();
301         cln::cl_N u = -cln::log(1-x);
302         cln::cl_N factor = u * cln::cl_float(1, cln::float_format(Digits));
303         cln::cl_N res = u;
304         cln::cl_N resbuf;
305         unsigned i=2;
306         do {
307                 resbuf = res;
308                 factor = factor * u / i;
309                 res = res + (*it) * factor;
310                 i++;
311                 if (++it == xend) {
312                         double_Xn();
313                         it = Xn[n-2].begin() + (i-2);
314                         xend = Xn[n-2].end();
315                 }
316         } while (res != resbuf);
317         return res;
318 }
319
320
321 // forward declaration needed by function Li_projection and C below
322 const cln::cl_N S_num(int n, int p, const cln::cl_N& x);
323
324
325 // helper function for classical polylog Li
326 cln::cl_N Li_projection(int n, const cln::cl_N& x, const cln::float_format_t& prec)
327 {
328         // treat n=2 as special case
329         if (n == 2) {
330                 // check if precalculated X0 exists
331                 if (xnsize == 0) {
332                         fill_Xn(0);
333                 }
334
335                 if (cln::realpart(x) < 0.5) {
336                         // choose the faster algorithm
337                         // the switching point was empirically determined. the optimal point
338                         // depends on hardware, Digits, ... so an approx value is okay.
339                         // it solves also the problem with precision due to the u=-log(1-x) transformation
340                         if (cln::abs(cln::realpart(x)) < 0.25) {
341                                 
342                                 return Li2_do_sum(x);
343                         } else {
344                                 return Li2_do_sum_Xn(x);
345                         }
346                 } else {
347                         // choose the faster algorithm
348                         if (cln::abs(cln::realpart(x)) > 0.75) {
349                                 if ( x == 1 ) {
350                                         return cln::zeta(2);
351                                 } else {
352                                         return -Li2_do_sum(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
353                                 }
354                         } else {
355                                 return -Li2_do_sum_Xn(1-x) - cln::log(x) * cln::log(1-x) + cln::zeta(2);
356                         }
357                 }
358         } else {
359                 // check if precalculated Xn exist
360                 if (n > xnsize+1) {
361                         for (int i=xnsize; i<n-1; i++) {
362                                 fill_Xn(i);
363                         }
364                 }
365
366                 if (cln::realpart(x) < 0.5) {
367                         // choose the faster algorithm
368                         // with n>=12 the "normal" summation always wins against the method with Xn
369                         if ((cln::abs(cln::realpart(x)) < 0.3) || (n >= 12)) {
370                                 return Lin_do_sum(n, x);
371                         } else {
372                                 return Lin_do_sum_Xn(n, x);
373                         }
374                 } else {
375                         cln::cl_N result = 0;
376                         if ( x != 1 ) result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
377                         for (int j=0; j<n-1; j++) {
378                                 result = result + (S_num(n-j-1, 1, 1) - S_num(1, n-j-1, 1-x))
379                                                   * cln::expt(cln::log(x), j) / cln::factorial(j);
380                         }
381                         return result;
382                 }
383         }
384 }
385
386 // helper function for classical polylog Li
387 const cln::cl_N Lin_numeric(const int n, const cln::cl_N& x)
388 {
389         if (n == 1) {
390                 // just a log
391                 return -cln::log(1-x);
392         }
393         if (zerop(x)) {
394                 return 0;
395         }
396         if (x == 1) {
397                 // [Kol] (2.22)
398                 return cln::zeta(n);
399         }
400         else if (x == -1) {
401                 // [Kol] (2.22)
402                 return -(1-cln::expt(cln::cl_I(2),1-n)) * cln::zeta(n);
403         }
404         if (cln::abs(realpart(x)) < 0.4 && cln::abs(cln::abs(x)-1) < 0.01) {
405                 cln::cl_N result = -cln::expt(cln::log(x), n-1) * cln::log(1-x) / cln::factorial(n-1);
406                 for (int j=0; j<n-1; j++) {
407                         result = result + (S_num(n-j-1, 1, 1) - S_num(1, n-j-1, 1-x))
408                                 * cln::expt(cln::log(x), j) / cln::factorial(j);
409                 }
410                 return result;
411         }
412
413         // what is the desired float format?
414         // first guess: default format
415         cln::float_format_t prec = cln::default_float_format;
416         const cln::cl_N value = x;
417         // second guess: the argument's format
418         if (!instanceof(realpart(x), cln::cl_RA_ring))
419                 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
420         else if (!instanceof(imagpart(x), cln::cl_RA_ring))
421                 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
422         
423         // [Kol] (5.15)
424         if (cln::abs(value) > 1) {
425                 cln::cl_N result = -cln::expt(cln::log(-value),n) / cln::factorial(n);
426                 // check if argument is complex. if it is real, the new polylog has to be conjugated.
427                 if (cln::zerop(cln::imagpart(value))) {
428                         if (n & 1) {
429                                 result = result + conjugate(Li_projection(n, cln::recip(value), prec));
430                         }
431                         else {
432                                 result = result - conjugate(Li_projection(n, cln::recip(value), prec));
433                         }
434                 }
435                 else {
436                         if (n & 1) {
437                                 result = result + Li_projection(n, cln::recip(value), prec);
438                         }
439                         else {
440                                 result = result - Li_projection(n, cln::recip(value), prec);
441                         }
442                 }
443                 cln::cl_N add;
444                 for (int j=0; j<n-1; j++) {
445                         add = add + (1+cln::expt(cln::cl_I(-1),n-j)) * (1-cln::expt(cln::cl_I(2),1-n+j))
446                                     * Lin_numeric(n-j,1) * cln::expt(cln::log(-value),j) / cln::factorial(j);
447                 }
448                 result = result - add;
449                 return result;
450         }
451         else {
452                 return Li_projection(n, value, prec);
453         }
454 }
455
456
457 } // end of anonymous namespace
458
459
460 //////////////////////////////////////////////////////////////////////
461 //
462 // Multiple polylogarithm  Li(n,x)
463 //
464 // helper function
465 //
466 //////////////////////////////////////////////////////////////////////
467
468
469 // anonymous namespace for helper function
470 namespace {
471
472
473 // performs the actual series summation for multiple polylogarithms
474 cln::cl_N multipleLi_do_sum(const std::vector<int>& s, const std::vector<cln::cl_N>& x)
475 {
476         // ensure all x <> 0.
477         for (std::vector<cln::cl_N>::const_iterator it = x.begin(); it != x.end(); ++it) {
478                 if ( *it == 0 ) return cln::cl_float(0, cln::float_format(Digits));
479         }
480
481         const int j = s.size();
482         bool flag_accidental_zero = false;
483
484         std::vector<cln::cl_N> t(j);
485         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
486
487         cln::cl_N t0buf;
488         int q = 0;
489         do {
490                 t0buf = t[0];
491                 q++;
492                 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
493                 for (int k=j-2; k>=0; k--) {
494                         t[k] = t[k] + t[k+1] * cln::expt(x[k], q+j-1-k) / cln::expt(cln::cl_I(q+j-1-k), s[k]);
495                 }
496                 q++;
497                 t[j-1] = t[j-1] + cln::expt(x[j-1], q) / cln::expt(cln::cl_I(q),s[j-1]) * one;
498                 for (int k=j-2; k>=0; k--) {
499                         flag_accidental_zero = cln::zerop(t[k+1]);
500                         t[k] = t[k] + t[k+1] * cln::expt(x[k], q+j-1-k) / cln::expt(cln::cl_I(q+j-1-k), s[k]);
501                 }
502         } while ( (t[0] != t0buf) || cln::zerop(t[0]) || flag_accidental_zero );
503
504         return t[0];
505 }
506
507
508 // forward declaration for Li_eval()
509 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf);
510
511
512 // type used by the transformation functions for G
513 typedef std::vector<int> Gparameter;
514
515
516 // G_eval1-function for G transformations
517 ex G_eval1(int a, int scale, const exvector& gsyms)
518 {
519         if (a != 0) {
520                 const ex& scs = gsyms[std::abs(scale)];
521                 const ex& as = gsyms[std::abs(a)];
522                 if (as != scs) {
523                         return -log(1 - scs/as);
524                 } else {
525                         return -zeta(1);
526                 }
527         } else {
528                 return log(gsyms[std::abs(scale)]);
529         }
530 }
531
532
533 // G_eval-function for G transformations
534 ex G_eval(const Gparameter& a, int scale, const exvector& gsyms)
535 {
536         // check for properties of G
537         ex sc = gsyms[std::abs(scale)];
538         lst newa;
539         bool all_zero = true;
540         bool all_ones = true;
541         int count_ones = 0;
542         for (Gparameter::const_iterator it = a.begin(); it != a.end(); ++it) {
543                 if (*it != 0) {
544                         const ex sym = gsyms[std::abs(*it)];
545                         newa.append(sym);
546                         all_zero = false;
547                         if (sym != sc) {
548                                 all_ones = false;
549                         }
550                         if (all_ones) {
551                                 ++count_ones;
552                         }
553                 } else {
554                         all_ones = false;
555                 }
556         }
557
558         // care about divergent G: shuffle to separate divergencies that will be canceled
559         // later on in the transformation
560         if (newa.nops() > 1 && newa.op(0) == sc && !all_ones && a.front()!=0) {
561                 // do shuffle
562                 Gparameter short_a;
563                 Gparameter::const_iterator it = a.begin();
564                 ++it;
565                 for (; it != a.end(); ++it) {
566                         short_a.push_back(*it);
567                 }
568                 ex result = G_eval1(a.front(), scale, gsyms) * G_eval(short_a, scale, gsyms);
569                 it = short_a.begin();
570                 for (int i=1; i<count_ones; ++i) {
571                         ++it;
572                 }
573                 for (; it != short_a.end(); ++it) {
574
575                         Gparameter newa;
576                         Gparameter::const_iterator it2 = short_a.begin();
577                         for (; it2 != it; ++it2) {
578                                 newa.push_back(*it2);
579                         }
580                         newa.push_back(*it);
581                         newa.push_back(a[0]);
582                         it2 = it;
583                         ++it2;
584                         for (; it2 != short_a.end(); ++it2) {
585                                 newa.push_back(*it2);   
586                         }
587                         result -= G_eval(newa, scale, gsyms);
588                 }
589                 return result / count_ones;
590         }
591
592         // G({1,...,1};y) -> G({1};y)^k / k!
593         if (all_ones && a.size() > 1) {
594                 return pow(G_eval1(a.front(),scale, gsyms), count_ones) / factorial(count_ones);
595         }
596
597         // G({0,...,0};y) -> log(y)^k / k!
598         if (all_zero) {
599                 return pow(log(gsyms[std::abs(scale)]), a.size()) / factorial(a.size());
600         }
601
602         // no special cases anymore -> convert it into Li
603         lst m;
604         lst x;
605         ex argbuf = gsyms[std::abs(scale)];
606         ex mval = _ex1;
607         for (Gparameter::const_iterator it=a.begin(); it!=a.end(); ++it) {
608                 if (*it != 0) {
609                         const ex& sym = gsyms[std::abs(*it)];
610                         x.append(argbuf / sym);
611                         m.append(mval);
612                         mval = _ex1;
613                         argbuf = sym;
614                 } else {
615                         ++mval;
616                 }
617         }
618         return pow(-1, x.nops()) * Li(m, x);
619 }
620
621
622 // converts data for G: pending_integrals -> a
623 Gparameter convert_pending_integrals_G(const Gparameter& pending_integrals)
624 {
625         GINAC_ASSERT(pending_integrals.size() != 1);
626
627         if (pending_integrals.size() > 0) {
628                 // get rid of the first element, which would stand for the new upper limit
629                 Gparameter new_a(pending_integrals.begin()+1, pending_integrals.end());
630                 return new_a;
631         } else {
632                 // just return empty parameter list
633                 Gparameter new_a;
634                 return new_a;
635         }
636 }
637
638
639 // check the parameters a and scale for G and return information about convergence, depth, etc.
640 // convergent     : true if G(a,scale) is convergent
641 // depth          : depth of G(a,scale)
642 // trailing_zeros : number of trailing zeros of a
643 // min_it         : iterator of a pointing on the smallest element in a
644 Gparameter::const_iterator check_parameter_G(const Gparameter& a, int scale,
645                 bool& convergent, int& depth, int& trailing_zeros, Gparameter::const_iterator& min_it)
646 {
647         convergent = true;
648         depth = 0;
649         trailing_zeros = 0;
650         min_it = a.end();
651         Gparameter::const_iterator lastnonzero = a.end();
652         for (Gparameter::const_iterator it = a.begin(); it != a.end(); ++it) {
653                 if (std::abs(*it) > 0) {
654                         ++depth;
655                         trailing_zeros = 0;
656                         lastnonzero = it;
657                         if (std::abs(*it) < scale) {
658                                 convergent = false;
659                                 if ((min_it == a.end()) || (std::abs(*it) < std::abs(*min_it))) {
660                                         min_it = it;
661                                 }
662                         }
663                 } else {
664                         ++trailing_zeros;
665                 }
666         }
667         if (lastnonzero == a.end())
668                 return a.end();
669         return ++lastnonzero;
670 }
671
672
673 // add scale to pending_integrals if pending_integrals is empty
674 Gparameter prepare_pending_integrals(const Gparameter& pending_integrals, int scale)
675 {
676         GINAC_ASSERT(pending_integrals.size() != 1);
677
678         if (pending_integrals.size() > 0) {
679                 return pending_integrals;
680         } else {
681                 Gparameter new_pending_integrals;
682                 new_pending_integrals.push_back(scale);
683                 return new_pending_integrals;
684         }
685 }
686
687
688 // handles trailing zeroes for an otherwise convergent integral
689 ex trailing_zeros_G(const Gparameter& a, int scale, const exvector& gsyms)
690 {
691         bool convergent;
692         int depth, trailing_zeros;
693         Gparameter::const_iterator last, dummyit;
694         last = check_parameter_G(a, scale, convergent, depth, trailing_zeros, dummyit);
695
696         GINAC_ASSERT(convergent);
697
698         if ((trailing_zeros > 0) && (depth > 0)) {
699                 ex result;
700                 Gparameter new_a(a.begin(), a.end()-1);
701                 result += G_eval1(0, scale, gsyms) * trailing_zeros_G(new_a, scale, gsyms);
702                 for (Gparameter::const_iterator it = a.begin(); it != last; ++it) {
703                         Gparameter new_a(a.begin(), it);
704                         new_a.push_back(0);
705                         new_a.insert(new_a.end(), it, a.end()-1);
706                         result -= trailing_zeros_G(new_a, scale, gsyms);
707                 }
708
709                 return result / trailing_zeros;
710         } else {
711                 return G_eval(a, scale, gsyms);
712         }
713 }
714
715
716 // G transformation [VSW] (57),(58)
717 ex depth_one_trafo_G(const Gparameter& pending_integrals, const Gparameter& a, int scale, const exvector& gsyms)
718 {
719         // pendint = ( y1, b1, ..., br )
720         //       a = ( 0, ..., 0, amin )
721         //   scale = y2
722         //
723         // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(0, ..., 0, sr; y2)
724         // where sr replaces amin
725
726         GINAC_ASSERT(a.back() != 0);
727         GINAC_ASSERT(a.size() > 0);
728
729         ex result;
730         Gparameter new_pending_integrals = prepare_pending_integrals(pending_integrals, std::abs(a.back()));
731         const int psize = pending_integrals.size();
732
733         // length == 1
734         // G(sr_{+-}; y2 ) = G(y2_{-+}; sr) - G(0; sr) + ln(-y2_{-+})
735
736         if (a.size() == 1) {
737
738           // ln(-y2_{-+})
739           result += log(gsyms[ex_to<numeric>(scale).to_int()]);
740                 if (a.back() > 0) {
741                         new_pending_integrals.push_back(-scale);
742                         result += I*Pi;
743                 } else {
744                         new_pending_integrals.push_back(scale);
745                         result -= I*Pi;
746                 }
747                 if (psize) {
748                         result *= trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
749                                                    pending_integrals.front(),
750                                                    gsyms);
751                 }
752                 
753                 // G(y2_{-+}; sr)
754                 result += trailing_zeros_G(convert_pending_integrals_G(new_pending_integrals),
755                                            new_pending_integrals.front(),
756                                            gsyms);
757                 
758                 // G(0; sr)
759                 new_pending_integrals.back() = 0;
760                 result -= trailing_zeros_G(convert_pending_integrals_G(new_pending_integrals),
761                                            new_pending_integrals.front(),
762                                            gsyms);
763
764                 return result;
765         }
766
767         // length > 1
768         // G_m(sr_{+-}; y2) = -zeta_m + int_0^y2 dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
769         //                            - int_0^sr dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
770
771         //term zeta_m
772         result -= zeta(a.size());
773         if (psize) {
774                 result *= trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
775                                            pending_integrals.front(),
776                                            gsyms);
777         }
778         
779         // term int_0^sr dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
780         //    = int_0^sr dt/t G_{m-1}( t_{+-}; y2 )
781         Gparameter new_a(a.begin()+1, a.end());
782         new_pending_integrals.push_back(0);
783         result -= depth_one_trafo_G(new_pending_integrals, new_a, scale, gsyms);
784         
785         // term int_0^y2 dt/t G_{m-1}( (1/y2)_{+-}; 1/t )
786         //    = int_0^y2 dt/t G_{m-1}( t_{+-}; y2 )
787         Gparameter new_pending_integrals_2;
788         new_pending_integrals_2.push_back(scale);
789         new_pending_integrals_2.push_back(0);
790         if (psize) {
791                 result += trailing_zeros_G(convert_pending_integrals_G(pending_integrals),
792                                            pending_integrals.front(),
793                                            gsyms)
794                           * depth_one_trafo_G(new_pending_integrals_2, new_a, scale, gsyms);
795         } else {
796                 result += depth_one_trafo_G(new_pending_integrals_2, new_a, scale, gsyms);
797         }
798
799         return result;
800 }
801
802
803 // forward declaration
804 ex shuffle_G(const Gparameter & a0, const Gparameter & a1, const Gparameter & a2,
805              const Gparameter& pendint, const Gparameter& a_old, int scale,
806              const exvector& gsyms, bool flag_trailing_zeros_only);
807
808
809 // G transformation [VSW]
810 ex G_transform(const Gparameter& pendint, const Gparameter& a, int scale,
811                const exvector& gsyms, bool flag_trailing_zeros_only)
812 {
813         // main recursion routine
814         //
815         // pendint = ( y1, b1, ..., br )
816         //       a = ( a1, ..., amin, ..., aw )
817         //   scale = y2
818         //
819         // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,sr,...,aw,y2)
820         // where sr replaces amin
821
822         // find smallest alpha, determine depth and trailing zeros, and check for convergence
823         bool convergent;
824         int depth, trailing_zeros;
825         Gparameter::const_iterator min_it;
826         Gparameter::const_iterator firstzero = 
827                 check_parameter_G(a, scale, convergent, depth, trailing_zeros, min_it);
828         int min_it_pos = min_it - a.begin();
829
830         // special case: all a's are zero
831         if (depth == 0) {
832                 ex result;
833
834                 if (a.size() == 0) {
835                   result = 1;
836                 } else {
837                   result = G_eval(a, scale, gsyms);
838                 }
839                 if (pendint.size() > 0) {
840                   result *= trailing_zeros_G(convert_pending_integrals_G(pendint),
841                                              pendint.front(),
842                                              gsyms);
843                 } 
844                 return result;
845         }
846
847         // handle trailing zeros
848         if (trailing_zeros > 0) {
849                 ex result;
850                 Gparameter new_a(a.begin(), a.end()-1);
851                 result += G_eval1(0, scale, gsyms) * G_transform(pendint, new_a, scale, gsyms, flag_trailing_zeros_only);
852                 for (Gparameter::const_iterator it = a.begin(); it != firstzero; ++it) {
853                         Gparameter new_a(a.begin(), it);
854                         new_a.push_back(0);
855                         new_a.insert(new_a.end(), it, a.end()-1);
856                         result -= G_transform(pendint, new_a, scale, gsyms, flag_trailing_zeros_only);
857                 }
858                 return result / trailing_zeros;
859         }
860
861         // convergence case or flag_trailing_zeros_only
862         if (convergent || flag_trailing_zeros_only) {
863                 if (pendint.size() > 0) {
864                         return G_eval(convert_pending_integrals_G(pendint),
865                                       pendint.front(), gsyms)*
866                                 G_eval(a, scale, gsyms);
867                 } else {
868                         return G_eval(a, scale, gsyms);
869                 }
870         }
871
872         // call basic transformation for depth equal one
873         if (depth == 1) {
874                 return depth_one_trafo_G(pendint, a, scale, gsyms);
875         }
876
877         // do recursion
878         // int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,sr,...,aw,y2)
879         //  =  int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) G(a1,...,0,...,aw,y2)
880         //   + int_0^y1 ds1/(s1-b1) ... int dsr/(sr-br) int_0^{sr} ds_{r+1} d/ds_{r+1} G(a1,...,s_{r+1},...,aw,y2)
881
882         // smallest element in last place
883         if (min_it + 1 == a.end()) {
884                 do { --min_it; } while (*min_it == 0);
885                 Gparameter empty;
886                 Gparameter a1(a.begin(),min_it+1);
887                 Gparameter a2(min_it+1,a.end());
888
889                 ex result = G_transform(pendint, a2, scale, gsyms, flag_trailing_zeros_only)*
890                             G_transform(empty, a1, scale, gsyms, flag_trailing_zeros_only);
891
892                 result -= shuffle_G(empty, a1, a2, pendint, a, scale, gsyms, flag_trailing_zeros_only);
893                 return result;
894         }
895
896         Gparameter empty;
897         Gparameter::iterator changeit;
898
899         // first term G(a_1,..,0,...,a_w;a_0)
900         Gparameter new_pendint = prepare_pending_integrals(pendint, a[min_it_pos]);
901         Gparameter new_a = a;
902         new_a[min_it_pos] = 0;
903         ex result = G_transform(empty, new_a, scale, gsyms, flag_trailing_zeros_only);
904         if (pendint.size() > 0) {
905                 result *= trailing_zeros_G(convert_pending_integrals_G(pendint),
906                                            pendint.front(), gsyms);
907         }
908
909         // other terms
910         changeit = new_a.begin() + min_it_pos;
911         changeit = new_a.erase(changeit);
912         if (changeit != new_a.begin()) {
913                 // smallest in the middle
914                 new_pendint.push_back(*changeit);
915                 result -= trailing_zeros_G(convert_pending_integrals_G(new_pendint),
916                                            new_pendint.front(), gsyms)*
917                           G_transform(empty, new_a, scale, gsyms, flag_trailing_zeros_only);
918                 int buffer = *changeit;
919                 *changeit = *min_it;
920                 result += G_transform(new_pendint, new_a, scale, gsyms, flag_trailing_zeros_only);
921                 *changeit = buffer;
922                 new_pendint.pop_back();
923                 --changeit;
924                 new_pendint.push_back(*changeit);
925                 result += trailing_zeros_G(convert_pending_integrals_G(new_pendint),
926                                            new_pendint.front(), gsyms)*
927                           G_transform(empty, new_a, scale, gsyms, flag_trailing_zeros_only);
928                 *changeit = *min_it;
929                 result -= G_transform(new_pendint, new_a, scale, gsyms, flag_trailing_zeros_only);
930         } else {
931                 // smallest at the front
932                 new_pendint.push_back(scale);
933                 result += trailing_zeros_G(convert_pending_integrals_G(new_pendint),
934                                            new_pendint.front(), gsyms)*
935                           G_transform(empty, new_a, scale, gsyms, flag_trailing_zeros_only);
936                 new_pendint.back() =  *changeit;
937                 result -= trailing_zeros_G(convert_pending_integrals_G(new_pendint),
938                                            new_pendint.front(), gsyms)*
939                           G_transform(empty, new_a, scale, gsyms, flag_trailing_zeros_only);
940                 *changeit = *min_it;
941                 result += G_transform(new_pendint, new_a, scale, gsyms, flag_trailing_zeros_only);
942         }
943         return result;
944 }
945
946
947 // shuffles the two parameter list a1 and a2 and calls G_transform for every term except
948 // for the one that is equal to a_old
949 ex shuffle_G(const Gparameter & a0, const Gparameter & a1, const Gparameter & a2,
950              const Gparameter& pendint, const Gparameter& a_old, int scale,
951              const exvector& gsyms, bool flag_trailing_zeros_only) 
952 {
953         if (a1.size()==0 && a2.size()==0) {
954                 // veto the one configuration we don't want
955                 if ( a0 == a_old ) return 0;
956
957                 return G_transform(pendint, a0, scale, gsyms, flag_trailing_zeros_only);
958         }
959
960         if (a2.size()==0) {
961                 Gparameter empty;
962                 Gparameter aa0 = a0;
963                 aa0.insert(aa0.end(),a1.begin(),a1.end());
964                 return shuffle_G(aa0, empty, empty, pendint, a_old, scale, gsyms, flag_trailing_zeros_only);
965         }
966
967         if (a1.size()==0) {
968                 Gparameter empty;
969                 Gparameter aa0 = a0;
970                 aa0.insert(aa0.end(),a2.begin(),a2.end());
971                 return shuffle_G(aa0, empty, empty, pendint, a_old, scale, gsyms, flag_trailing_zeros_only);
972         }
973
974         Gparameter a1_removed(a1.begin()+1,a1.end());
975         Gparameter a2_removed(a2.begin()+1,a2.end());
976
977         Gparameter a01 = a0;
978         Gparameter a02 = a0;
979
980         a01.push_back( a1[0] );
981         a02.push_back( a2[0] );
982
983         return shuffle_G(a01, a1_removed, a2, pendint, a_old, scale, gsyms, flag_trailing_zeros_only)
984              + shuffle_G(a02, a1, a2_removed, pendint, a_old, scale, gsyms, flag_trailing_zeros_only);
985 }
986
987 // handles the transformations and the numerical evaluation of G
988 // the parameter x, s and y must only contain numerics
989 static cln::cl_N
990 G_numeric(const std::vector<cln::cl_N>& x, const std::vector<int>& s,
991           const cln::cl_N& y);
992
993 // do acceleration transformation (hoelder convolution [BBB])
994 // the parameter x, s and y must only contain numerics
995 static cln::cl_N
996 G_do_hoelder(std::vector<cln::cl_N> x, /* yes, it's passed by value */
997              const std::vector<int>& s, const cln::cl_N& y)
998 {
999         cln::cl_N result;
1000         const std::size_t size = x.size();
1001         for (std::size_t i = 0; i < size; ++i)
1002                 x[i] = x[i]/y;
1003
1004         for (std::size_t r = 0; r <= size; ++r) {
1005                 cln::cl_N buffer(1 & r ? -1 : 1);
1006                 cln::cl_RA p(2);
1007                 bool adjustp;
1008                 do {
1009                         adjustp = false;
1010                         for (std::size_t i = 0; i < size; ++i) {
1011                                 if (x[i] == cln::cl_RA(1)/p) {
1012                                         p = p/2 + cln::cl_RA(3)/2;
1013                                         adjustp = true;
1014                                         continue;
1015                                 }
1016                         }
1017                 } while (adjustp);
1018                 cln::cl_RA q = p/(p-1);
1019                 std::vector<cln::cl_N> qlstx;
1020                 std::vector<int> qlsts;
1021                 for (std::size_t j = r; j >= 1; --j) {
1022                         qlstx.push_back(cln::cl_N(1) - x[j-1]);
1023                         if (instanceof(x[j-1], cln::cl_R_ring) && realpart(x[j-1]) > 1) {
1024                                 qlsts.push_back(1);
1025                         } else {
1026                                 qlsts.push_back(-s[j-1]);
1027                         }
1028                 }
1029                 if (qlstx.size() > 0) {
1030                         buffer = buffer*G_numeric(qlstx, qlsts, 1/q);
1031                 }
1032                 std::vector<cln::cl_N> plstx;
1033                 std::vector<int> plsts;
1034                 for (std::size_t j = r+1; j <= size; ++j) {
1035                         plstx.push_back(x[j-1]);
1036                         plsts.push_back(s[j-1]);
1037                 }
1038                 if (plstx.size() > 0) {
1039                         buffer = buffer*G_numeric(plstx, plsts, 1/p);
1040                 }
1041                 result = result + buffer;
1042         }
1043         return result;
1044 }
1045
1046 class less_object_for_cl_N
1047 {
1048 public:
1049         bool operator() (const cln::cl_N & a, const cln::cl_N & b) const
1050         {
1051                 // absolute value?
1052                 if (abs(a) != abs(b))
1053                         return (abs(a) < abs(b)) ? true : false;
1054
1055                 // complex phase?
1056                 if (phase(a) != phase(b))
1057                         return (phase(a) < phase(b)) ? true : false;
1058
1059                 // equal, therefore "less" is not true
1060                 return false;
1061         }
1062 };
1063
1064
1065 // convergence transformation, used for numerical evaluation of G function.
1066 // the parameter x, s and y must only contain numerics
1067 static cln::cl_N
1068 G_do_trafo(const std::vector<cln::cl_N>& x, const std::vector<int>& s,
1069            const cln::cl_N& y, bool flag_trailing_zeros_only)
1070 {
1071         // sort (|x|<->position) to determine indices
1072         typedef std::multimap<cln::cl_N, std::size_t, less_object_for_cl_N> sortmap_t;
1073         sortmap_t sortmap;
1074         std::size_t size = 0;
1075         for (std::size_t i = 0; i < x.size(); ++i) {
1076                 if (!zerop(x[i])) {
1077                         sortmap.insert(std::make_pair(x[i], i));
1078                         ++size;
1079                 }
1080         }
1081         // include upper limit (scale)
1082         sortmap.insert(std::make_pair(y, x.size()));
1083
1084         // generate missing dummy-symbols
1085         int i = 1;
1086         // holding dummy-symbols for the G/Li transformations
1087         exvector gsyms;
1088         gsyms.push_back(symbol("GSYMS_ERROR"));
1089         cln::cl_N lastentry(0);
1090         for (sortmap_t::const_iterator it = sortmap.begin(); it != sortmap.end(); ++it) {
1091                 if (it != sortmap.begin()) {
1092                         if (it->second < x.size()) {
1093                                 if (x[it->second] == lastentry) {
1094                                         gsyms.push_back(gsyms.back());
1095                                         continue;
1096                                 }
1097                         } else {
1098                                 if (y == lastentry) {
1099                                         gsyms.push_back(gsyms.back());
1100                                         continue;
1101                                 }
1102                         }
1103                 }
1104                 std::ostringstream os;
1105                 os << "a" << i;
1106                 gsyms.push_back(symbol(os.str()));
1107                 ++i;
1108                 if (it->second < x.size()) {
1109                         lastentry = x[it->second];
1110                 } else {
1111                         lastentry = y;
1112                 }
1113         }
1114
1115         // fill position data according to sorted indices and prepare substitution list
1116         Gparameter a(x.size());
1117         exmap subslst;
1118         std::size_t pos = 1;
1119         int scale = pos;
1120         for (sortmap_t::const_iterator it = sortmap.begin(); it != sortmap.end(); ++it) {
1121                 if (it->second < x.size()) {
1122                         if (s[it->second] > 0) {
1123                                 a[it->second] = pos;
1124                         } else {
1125                                 a[it->second] = -int(pos);
1126                         }
1127                         subslst[gsyms[pos]] = numeric(x[it->second]);
1128                 } else {
1129                         scale = pos;
1130                         subslst[gsyms[pos]] = numeric(y);
1131                 }
1132                 ++pos;
1133         }
1134
1135         // do transformation
1136         Gparameter pendint;
1137         ex result = G_transform(pendint, a, scale, gsyms, flag_trailing_zeros_only);
1138         // replace dummy symbols with their values
1139         result = result.eval().expand();
1140         result = result.subs(subslst).evalf();
1141         if (!is_a<numeric>(result))
1142                 throw std::logic_error("G_do_trafo: G_transform returned non-numeric result");
1143         
1144         cln::cl_N ret = ex_to<numeric>(result).to_cl_N();
1145         return ret;
1146 }
1147
1148 // handles the transformations and the numerical evaluation of G
1149 // the parameter x, s and y must only contain numerics
1150 static cln::cl_N
1151 G_numeric(const std::vector<cln::cl_N>& x, const std::vector<int>& s,
1152           const cln::cl_N& y)
1153 {
1154         // check for convergence and necessary accelerations
1155         bool need_trafo = false;
1156         bool need_hoelder = false;
1157         bool have_trailing_zero = false;
1158         std::size_t depth = 0;
1159         for (std::size_t i = 0; i < x.size(); ++i) {
1160                 if (!zerop(x[i])) {
1161                         ++depth;
1162                         const cln::cl_N x_y = abs(x[i]) - y;
1163                         if (instanceof(x_y, cln::cl_R_ring) &&
1164                             realpart(x_y) < cln::least_negative_float(cln::float_format(Digits - 2)))
1165                                 need_trafo = true;
1166
1167                         if (abs(abs(x[i]/y) - 1) < 0.01)
1168                                 need_hoelder = true;
1169                 }
1170         }
1171         if (zerop(x.back())) {
1172                 have_trailing_zero = true;
1173                 need_trafo = true;
1174         }
1175
1176         if (depth == 1 && x.size() == 2 && !need_trafo)
1177                 return - Li_projection(2, y/x[1], cln::float_format(Digits));
1178         
1179         // do acceleration transformation (hoelder convolution [BBB])
1180         if (need_hoelder && !have_trailing_zero)
1181                 return G_do_hoelder(x, s, y);
1182         
1183         // convergence transformation
1184         if (need_trafo)
1185                 return G_do_trafo(x, s, y, have_trailing_zero);
1186
1187         // do summation
1188         std::vector<cln::cl_N> newx;
1189         newx.reserve(x.size());
1190         std::vector<int> m;
1191         m.reserve(x.size());
1192         int mcount = 1;
1193         int sign = 1;
1194         cln::cl_N factor = y;
1195         for (std::size_t i = 0; i < x.size(); ++i) {
1196                 if (zerop(x[i])) {
1197                         ++mcount;
1198                 } else {
1199                         newx.push_back(factor/x[i]);
1200                         factor = x[i];
1201                         m.push_back(mcount);
1202                         mcount = 1;
1203                         sign = -sign;
1204                 }
1205         }
1206
1207         return sign*multipleLi_do_sum(m, newx);
1208 }
1209
1210
1211 ex mLi_numeric(const lst& m, const lst& x)
1212 {
1213         // let G_numeric do the transformation
1214         std::vector<cln::cl_N> newx;
1215         newx.reserve(x.nops());
1216         std::vector<int> s;
1217         s.reserve(x.nops());
1218         cln::cl_N factor(1);
1219         for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1220                 for (int i = 1; i < *itm; ++i) {
1221                         newx.push_back(cln::cl_N(0));
1222                         s.push_back(1);
1223                 }
1224                 const cln::cl_N xi = ex_to<numeric>(*itx).to_cl_N();
1225                 factor = factor/xi;
1226                 newx.push_back(factor);
1227                 if ( !instanceof(factor, cln::cl_R_ring) && imagpart(factor) < 0 ) {
1228                         s.push_back(-1);
1229                 }
1230                 else {
1231                         s.push_back(1);
1232                 }
1233         }
1234         return numeric(cln::cl_N(1 & m.nops() ? - 1 : 1)*G_numeric(newx, s, cln::cl_N(1)));
1235 }
1236
1237
1238 } // end of anonymous namespace
1239
1240
1241 //////////////////////////////////////////////////////////////////////
1242 //
1243 // Generalized multiple polylogarithm  G(x, y) and G(x, s, y)
1244 //
1245 // GiNaC function
1246 //
1247 //////////////////////////////////////////////////////////////////////
1248
1249
1250 static ex G2_evalf(const ex& x_, const ex& y)
1251 {
1252         if ((!y.info(info_flags::numeric)) || (!y.info(info_flags::positive))) {
1253                 return G(x_, y).hold();
1254         }
1255         lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1256         if (x.nops() == 0) {
1257                 return _ex1;
1258         }
1259         if (x.op(0) == y) {
1260                 return G(x_, y).hold();
1261         }
1262         std::vector<int> s;
1263         s.reserve(x.nops());
1264         bool all_zero = true;
1265         for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1266                 if (!(*it).info(info_flags::numeric)) {
1267                         return G(x_, y).hold();
1268                 }
1269                 if (*it != _ex0) {
1270                         all_zero = false;
1271                 }
1272                 if ( !ex_to<numeric>(*it).is_real() && ex_to<numeric>(*it).imag() < 0 ) {
1273                         s.push_back(-1);
1274                 }
1275                 else {
1276                         s.push_back(1);
1277                 }
1278         }
1279         if (all_zero) {
1280                 return pow(log(y), x.nops()) / factorial(x.nops());
1281         }
1282         std::vector<cln::cl_N> xv;
1283         xv.reserve(x.nops());
1284         for (lst::const_iterator it = x.begin(); it != x.end(); ++it)
1285                 xv.push_back(ex_to<numeric>(*it).to_cl_N());
1286         cln::cl_N result = G_numeric(xv, s, ex_to<numeric>(y).to_cl_N());
1287         return numeric(result);
1288 }
1289
1290
1291 static ex G2_eval(const ex& x_, const ex& y)
1292 {
1293         //TODO eval to MZV or H or S or Lin
1294
1295         if ((!y.info(info_flags::numeric)) || (!y.info(info_flags::positive))) {
1296                 return G(x_, y).hold();
1297         }
1298         lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1299         if (x.nops() == 0) {
1300                 return _ex1;
1301         }
1302         if (x.op(0) == y) {
1303                 return G(x_, y).hold();
1304         }
1305         std::vector<int> s;
1306         s.reserve(x.nops());
1307         bool all_zero = true;
1308         bool crational = true;
1309         for (lst::const_iterator it = x.begin(); it != x.end(); ++it) {
1310                 if (!(*it).info(info_flags::numeric)) {
1311                         return G(x_, y).hold();
1312                 }
1313                 if (!(*it).info(info_flags::crational)) {
1314                         crational = false;
1315                 }
1316                 if (*it != _ex0) {
1317                         all_zero = false;
1318                 }
1319                 if ( !ex_to<numeric>(*it).is_real() && ex_to<numeric>(*it).imag() < 0 ) {
1320                         s.push_back(-1);
1321                 }
1322                 else {
1323                         s.push_back(+1);
1324                 }
1325         }
1326         if (all_zero) {
1327                 return pow(log(y), x.nops()) / factorial(x.nops());
1328         }
1329         if (!y.info(info_flags::crational)) {
1330                 crational = false;
1331         }
1332         if (crational) {
1333                 return G(x_, y).hold();
1334         }
1335         std::vector<cln::cl_N> xv;
1336         xv.reserve(x.nops());
1337         for (lst::const_iterator it = x.begin(); it != x.end(); ++it)
1338                 xv.push_back(ex_to<numeric>(*it).to_cl_N());
1339         cln::cl_N result = G_numeric(xv, s, ex_to<numeric>(y).to_cl_N());
1340         return numeric(result);
1341 }
1342
1343
1344 // option do_not_evalf_params() removed.
1345 unsigned G2_SERIAL::serial = function::register_new(function_options("G", 2).
1346                                 evalf_func(G2_evalf).
1347                                 eval_func(G2_eval).
1348                                 overloaded(2));
1349 //TODO
1350 //                                derivative_func(G2_deriv).
1351 //                                print_func<print_latex>(G2_print_latex).
1352
1353
1354 static ex G3_evalf(const ex& x_, const ex& s_, const ex& y)
1355 {
1356         if ((!y.info(info_flags::numeric)) || (!y.info(info_flags::positive))) {
1357                 return G(x_, s_, y).hold();
1358         }
1359         lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1360         lst s = is_a<lst>(s_) ? ex_to<lst>(s_) : lst(s_);
1361         if (x.nops() != s.nops()) {
1362                 return G(x_, s_, y).hold();
1363         }
1364         if (x.nops() == 0) {
1365                 return _ex1;
1366         }
1367         if (x.op(0) == y) {
1368                 return G(x_, s_, y).hold();
1369         }
1370         std::vector<int> sn;
1371         sn.reserve(s.nops());
1372         bool all_zero = true;
1373         for (lst::const_iterator itx = x.begin(), its = s.begin(); itx != x.end(); ++itx, ++its) {
1374                 if (!(*itx).info(info_flags::numeric)) {
1375                         return G(x_, y).hold();
1376                 }
1377                 if (!(*its).info(info_flags::real)) {
1378                         return G(x_, y).hold();
1379                 }
1380                 if (*itx != _ex0) {
1381                         all_zero = false;
1382                 }
1383                 if ( ex_to<numeric>(*itx).is_real() ) {
1384                         if ( ex_to<numeric>(*itx).is_positive() ) {
1385                                 if ( *its >= 0 ) {
1386                                         sn.push_back(1);
1387                                 }
1388                                 else {
1389                                         sn.push_back(-1);
1390                                 }
1391                         } else {
1392                                 sn.push_back(1);
1393                         }
1394                 }
1395                 else {
1396                         if ( ex_to<numeric>(*itx).imag() > 0 ) {
1397                                 sn.push_back(1);
1398                         }
1399                         else {
1400                                 sn.push_back(-1);
1401                         }
1402                 }
1403         }
1404         if (all_zero) {
1405                 return pow(log(y), x.nops()) / factorial(x.nops());
1406         }
1407         std::vector<cln::cl_N> xn;
1408         xn.reserve(x.nops());
1409         for (lst::const_iterator it = x.begin(); it != x.end(); ++it)
1410                 xn.push_back(ex_to<numeric>(*it).to_cl_N());
1411         cln::cl_N result = G_numeric(xn, sn, ex_to<numeric>(y).to_cl_N());
1412         return numeric(result);
1413 }
1414
1415
1416 static ex G3_eval(const ex& x_, const ex& s_, const ex& y)
1417 {
1418         //TODO eval to MZV or H or S or Lin
1419
1420         if ((!y.info(info_flags::numeric)) || (!y.info(info_flags::positive))) {
1421                 return G(x_, s_, y).hold();
1422         }
1423         lst x = is_a<lst>(x_) ? ex_to<lst>(x_) : lst(x_);
1424         lst s = is_a<lst>(s_) ? ex_to<lst>(s_) : lst(s_);
1425         if (x.nops() != s.nops()) {
1426                 return G(x_, s_, y).hold();
1427         }
1428         if (x.nops() == 0) {
1429                 return _ex1;
1430         }
1431         if (x.op(0) == y) {
1432                 return G(x_, s_, y).hold();
1433         }
1434         std::vector<int> sn;
1435         sn.reserve(s.nops());
1436         bool all_zero = true;
1437         bool crational = true;
1438         for (lst::const_iterator itx = x.begin(), its = s.begin(); itx != x.end(); ++itx, ++its) {
1439                 if (!(*itx).info(info_flags::numeric)) {
1440                         return G(x_, s_, y).hold();
1441                 }
1442                 if (!(*its).info(info_flags::real)) {
1443                         return G(x_, s_, y).hold();
1444                 }
1445                 if (!(*itx).info(info_flags::crational)) {
1446                         crational = false;
1447                 }
1448                 if (*itx != _ex0) {
1449                         all_zero = false;
1450                 }
1451                 if ( ex_to<numeric>(*itx).is_real() ) {
1452                         if ( ex_to<numeric>(*itx).is_positive() ) {
1453                                 if ( *its >= 0 ) {
1454                                         sn.push_back(1);
1455                                 }
1456                                 else {
1457                                         sn.push_back(-1);
1458                                 }
1459                         } else {
1460                                 sn.push_back(1);
1461                         }
1462                 }
1463                 else {
1464                         if ( ex_to<numeric>(*itx).imag() > 0 ) {
1465                                 sn.push_back(1);
1466                         }
1467                         else {
1468                                 sn.push_back(-1);
1469                         }
1470                 }
1471         }
1472         if (all_zero) {
1473                 return pow(log(y), x.nops()) / factorial(x.nops());
1474         }
1475         if (!y.info(info_flags::crational)) {
1476                 crational = false;
1477         }
1478         if (crational) {
1479                 return G(x_, s_, y).hold();
1480         }
1481         std::vector<cln::cl_N> xn;
1482         xn.reserve(x.nops());
1483         for (lst::const_iterator it = x.begin(); it != x.end(); ++it)
1484                 xn.push_back(ex_to<numeric>(*it).to_cl_N());
1485         cln::cl_N result = G_numeric(xn, sn, ex_to<numeric>(y).to_cl_N());
1486         return numeric(result);
1487 }
1488
1489
1490 // option do_not_evalf_params() removed.
1491 // This is safe: in the code above it only matters if s_ > 0 or s_ < 0,
1492 // s_ is allowed to be of floating type.
1493 unsigned G3_SERIAL::serial = function::register_new(function_options("G", 3).
1494                                 evalf_func(G3_evalf).
1495                                 eval_func(G3_eval).
1496                                 overloaded(2));
1497 //TODO
1498 //                                derivative_func(G3_deriv).
1499 //                                print_func<print_latex>(G3_print_latex).
1500
1501
1502 //////////////////////////////////////////////////////////////////////
1503 //
1504 // Classical polylogarithm and multiple polylogarithm  Li(m,x)
1505 //
1506 // GiNaC function
1507 //
1508 //////////////////////////////////////////////////////////////////////
1509
1510
1511 static ex Li_evalf(const ex& m_, const ex& x_)
1512 {
1513         // classical polylogs
1514         if (m_.info(info_flags::posint)) {
1515                 if (x_.info(info_flags::numeric)) {
1516                         int m__ = ex_to<numeric>(m_).to_int();
1517                         const cln::cl_N x__ = ex_to<numeric>(x_).to_cl_N();
1518                         const cln::cl_N result = Lin_numeric(m__, x__);
1519                         return numeric(result);
1520                 } else {
1521                         // try to numerically evaluate second argument
1522                         ex x_val = x_.evalf();
1523                         if (x_val.info(info_flags::numeric)) {
1524                                 int m__ = ex_to<numeric>(m_).to_int();
1525                                 const cln::cl_N x__ = ex_to<numeric>(x_val).to_cl_N();
1526                                 const cln::cl_N result = Lin_numeric(m__, x__);
1527                                 return numeric(result);
1528                         }
1529                 }
1530         }
1531         // multiple polylogs
1532         if (is_a<lst>(m_) && is_a<lst>(x_)) {
1533
1534                 const lst& m = ex_to<lst>(m_);
1535                 const lst& x = ex_to<lst>(x_);
1536                 if (m.nops() != x.nops()) {
1537                         return Li(m_,x_).hold();
1538                 }
1539                 if (x.nops() == 0) {
1540                         return _ex1;
1541                 }
1542                 if ((m.op(0) == _ex1) && (x.op(0) == _ex1)) {
1543                         return Li(m_,x_).hold();
1544                 }
1545
1546                 for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1547                         if (!(*itm).info(info_flags::posint)) {
1548                                 return Li(m_, x_).hold();
1549                         }
1550                         if (!(*itx).info(info_flags::numeric)) {
1551                                 return Li(m_, x_).hold();
1552                         }
1553                         if (*itx == _ex0) {
1554                                 return _ex0;
1555                         }
1556                 }
1557
1558                 return mLi_numeric(m, x);
1559         }
1560
1561         return Li(m_,x_).hold();
1562 }
1563
1564
1565 static ex Li_eval(const ex& m_, const ex& x_)
1566 {
1567         if (is_a<lst>(m_)) {
1568                 if (is_a<lst>(x_)) {
1569                         // multiple polylogs
1570                         const lst& m = ex_to<lst>(m_);
1571                         const lst& x = ex_to<lst>(x_);
1572                         if (m.nops() != x.nops()) {
1573                                 return Li(m_,x_).hold();
1574                         }
1575                         if (x.nops() == 0) {
1576                                 return _ex1;
1577                         }
1578                         bool is_H = true;
1579                         bool is_zeta = true;
1580                         bool do_evalf = true;
1581                         bool crational = true;
1582                         for (lst::const_iterator itm = m.begin(), itx = x.begin(); itm != m.end(); ++itm, ++itx) {
1583                                 if (!(*itm).info(info_flags::posint)) {
1584                                         return Li(m_,x_).hold();
1585                                 }
1586                                 if ((*itx != _ex1) && (*itx != _ex_1)) {
1587                                         if (itx != x.begin()) {
1588                                                 is_H = false;
1589                                         }
1590                                         is_zeta = false;
1591                                 }
1592                                 if (*itx == _ex0) {
1593                                         return _ex0;
1594                                 }
1595                                 if (!(*itx).info(info_flags::numeric)) {
1596                                         do_evalf = false;
1597                                 }
1598                                 if (!(*itx).info(info_flags::crational)) {
1599                                         crational = false;
1600                                 }
1601                         }
1602                         if (is_zeta) {
1603                                 lst newx;
1604                                 for (lst::const_iterator itx = x.begin(); itx != x.end(); ++itx) {
1605                                         GINAC_ASSERT((*itx == _ex1) || (*itx == _ex_1));
1606                                         // XXX: 1 + 0.0*I is considered equal to 1. However
1607                                         // the former is a not automatically converted
1608                                         // to a real number. Do the conversion explicitly
1609                                         // to avoid the "numeric::operator>(): complex inequality"
1610                                         // exception (and similar problems).
1611                                         newx.append(*itx != _ex_1 ? _ex1 : _ex_1);
1612                                 }
1613                                 return zeta(m_, newx);
1614                         }
1615                         if (is_H) {
1616                                 ex prefactor;
1617                                 lst newm = convert_parameter_Li_to_H(m, x, prefactor);
1618                                 return prefactor * H(newm, x[0]);
1619                         }
1620                         if (do_evalf && !crational) {
1621                                 return mLi_numeric(m,x);
1622                         }
1623                 }
1624                 return Li(m_, x_).hold();
1625         } else if (is_a<lst>(x_)) {
1626                 return Li(m_, x_).hold();
1627         }
1628
1629         // classical polylogs
1630         if (x_ == _ex0) {
1631                 return _ex0;
1632         }
1633         if (x_ == _ex1) {
1634                 return zeta(m_);
1635         }
1636         if (x_ == _ex_1) {
1637                 return (pow(2,1-m_)-1) * zeta(m_);
1638         }
1639         if (m_ == _ex1) {
1640                 return -log(1-x_);
1641         }
1642         if (m_ == _ex2) {
1643                 if (x_.is_equal(I)) {
1644                         return power(Pi,_ex2)/_ex_48 + Catalan*I;
1645                 }
1646                 if (x_.is_equal(-I)) {
1647                         return power(Pi,_ex2)/_ex_48 - Catalan*I;
1648                 }
1649         }
1650         if (m_.info(info_flags::posint) && x_.info(info_flags::numeric) && !x_.info(info_flags::crational)) {
1651                 int m__ = ex_to<numeric>(m_).to_int();
1652                 const cln::cl_N x__ = ex_to<numeric>(x_).to_cl_N();
1653                 const cln::cl_N result = Lin_numeric(m__, x__);
1654                 return numeric(result);
1655         }
1656
1657         return Li(m_, x_).hold();
1658 }
1659
1660
1661 static ex Li_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
1662 {
1663         if (is_a<lst>(m) || is_a<lst>(x)) {
1664                 // multiple polylog
1665                 epvector seq;
1666                 seq.push_back(expair(Li(m, x), 0));
1667                 return pseries(rel, seq);
1668         }
1669         
1670         // classical polylog
1671         const ex x_pt = x.subs(rel, subs_options::no_pattern);
1672         if (m.info(info_flags::numeric) && x_pt.info(info_flags::numeric)) {
1673                 // First special case: x==0 (derivatives have poles)
1674                 if (x_pt.is_zero()) {
1675                         const symbol s;
1676                         ex ser;
1677                         // manually construct the primitive expansion
1678                         for (int i=1; i<order; ++i)
1679                                 ser += pow(s,i) / pow(numeric(i), m);
1680                         // substitute the argument's series expansion
1681                         ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
1682                         // maybe that was terminating, so add a proper order term
1683                         epvector nseq;
1684                         nseq.push_back(expair(Order(_ex1), order));
1685                         ser += pseries(rel, nseq);
1686                         // reexpanding it will collapse the series again
1687                         return ser.series(rel, order);
1688                 }
1689                 // TODO special cases: x==1 (branch point) and x real, >=1 (branch cut)
1690                 throw std::runtime_error("Li_series: don't know how to do the series expansion at this point!");
1691         }
1692         // all other cases should be safe, by now:
1693         throw do_taylor();  // caught by function::series()
1694 }
1695
1696
1697 static ex Li_deriv(const ex& m_, const ex& x_, unsigned deriv_param)
1698 {
1699         GINAC_ASSERT(deriv_param < 2);
1700         if (deriv_param == 0) {
1701                 return _ex0;
1702         }
1703         if (m_.nops() > 1) {
1704                 throw std::runtime_error("don't know how to derivate multiple polylogarithm!");
1705         }
1706         ex m;
1707         if (is_a<lst>(m_)) {
1708                 m = m_.op(0);
1709         } else {
1710                 m = m_;
1711         }
1712         ex x;
1713         if (is_a<lst>(x_)) {
1714                 x = x_.op(0);
1715         } else {
1716                 x = x_;
1717         }
1718         if (m > 0) {
1719                 return Li(m-1, x) / x;
1720         } else {
1721                 return 1/(1-x);
1722         }
1723 }
1724
1725
1726 static void Li_print_latex(const ex& m_, const ex& x_, const print_context& c)
1727 {
1728         lst m;
1729         if (is_a<lst>(m_)) {
1730                 m = ex_to<lst>(m_);
1731         } else {
1732                 m = lst(m_);
1733         }
1734         lst x;
1735         if (is_a<lst>(x_)) {
1736                 x = ex_to<lst>(x_);
1737         } else {
1738                 x = lst(x_);
1739         }
1740         c.s << "\\mathrm{Li}_{";
1741         lst::const_iterator itm = m.begin();
1742         (*itm).print(c);
1743         itm++;
1744         for (; itm != m.end(); itm++) {
1745                 c.s << ",";
1746                 (*itm).print(c);
1747         }
1748         c.s << "}(";
1749         lst::const_iterator itx = x.begin();
1750         (*itx).print(c);
1751         itx++;
1752         for (; itx != x.end(); itx++) {
1753                 c.s << ",";
1754                 (*itx).print(c);
1755         }
1756         c.s << ")";
1757 }
1758
1759
1760 REGISTER_FUNCTION(Li,
1761                   evalf_func(Li_evalf).
1762                   eval_func(Li_eval).
1763                   series_func(Li_series).
1764                   derivative_func(Li_deriv).
1765                   print_func<print_latex>(Li_print_latex).
1766                   do_not_evalf_params());
1767
1768
1769 //////////////////////////////////////////////////////////////////////
1770 //
1771 // Nielsen's generalized polylogarithm  S(n,p,x)
1772 //
1773 // helper functions
1774 //
1775 //////////////////////////////////////////////////////////////////////
1776
1777
1778 // anonymous namespace for helper functions
1779 namespace {
1780
1781
1782 // lookup table for special Euler-Zagier-Sums (used for S_n,p(x))
1783 // see fill_Yn()
1784 std::vector<std::vector<cln::cl_N> > Yn;
1785 int ynsize = 0; // number of Yn[]
1786 int ynlength = 100; // initial length of all Yn[i]
1787
1788
1789 // This function calculates the Y_n. The Y_n are needed for the evaluation of S_{n,p}(x).
1790 // The Y_n are basically Euler-Zagier sums with all m_i=1. They are subsums in the Z-sum
1791 // representing S_{n,p}(x).
1792 // The first index in Y_n corresponds to the parameter p minus one, i.e. the depth of the
1793 // equivalent Z-sum.
1794 // The second index in Y_n corresponds to the running index of the outermost sum in the full Z-sum
1795 // representing S_{n,p}(x).
1796 // The calculation of Y_n uses the values from Y_{n-1}.
1797 void fill_Yn(int n, const cln::float_format_t& prec)
1798 {
1799         const int initsize = ynlength;
1800         //const int initsize = initsize_Yn;
1801         cln::cl_N one = cln::cl_float(1, prec);
1802
1803         if (n) {
1804                 std::vector<cln::cl_N> buf(initsize);
1805                 std::vector<cln::cl_N>::iterator it = buf.begin();
1806                 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
1807                 *it = (*itprev) / cln::cl_N(n+1) * one;
1808                 it++;
1809                 itprev++;
1810                 // sums with an index smaller than the depth are zero and need not to be calculated.
1811                 // calculation starts with depth, which is n+2)
1812                 for (int i=n+2; i<=initsize+n; i++) {
1813                         *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
1814                         it++;
1815                         itprev++;
1816                 }
1817                 Yn.push_back(buf);
1818         } else {
1819                 std::vector<cln::cl_N> buf(initsize);
1820                 std::vector<cln::cl_N>::iterator it = buf.begin();
1821                 *it = 1 * one;
1822                 it++;
1823                 for (int i=2; i<=initsize; i++) {
1824                         *it = *(it-1) + 1 / cln::cl_N(i) * one;
1825                         it++;
1826                 }
1827                 Yn.push_back(buf);
1828         }
1829         ynsize++;
1830 }
1831
1832
1833 // make Yn longer ... 
1834 void make_Yn_longer(int newsize, const cln::float_format_t& prec)
1835 {
1836
1837         cln::cl_N one = cln::cl_float(1, prec);
1838
1839         Yn[0].resize(newsize);
1840         std::vector<cln::cl_N>::iterator it = Yn[0].begin();
1841         it += ynlength;
1842         for (int i=ynlength+1; i<=newsize; i++) {
1843                 *it = *(it-1) + 1 / cln::cl_N(i) * one;
1844                 it++;
1845         }
1846
1847         for (int n=1; n<ynsize; n++) {
1848                 Yn[n].resize(newsize);
1849                 std::vector<cln::cl_N>::iterator it = Yn[n].begin();
1850                 std::vector<cln::cl_N>::iterator itprev = Yn[n-1].begin();
1851                 it += ynlength;
1852                 itprev += ynlength;
1853                 for (int i=ynlength+n+1; i<=newsize+n; i++) {
1854                         *it = *(it-1) + (*itprev) / cln::cl_N(i) * one;
1855                         it++;
1856                         itprev++;
1857                 }
1858         }
1859         
1860         ynlength = newsize;
1861 }
1862
1863
1864 // helper function for S(n,p,x)
1865 // [Kol] (7.2)
1866 cln::cl_N C(int n, int p)
1867 {
1868         cln::cl_N result;
1869
1870         for (int k=0; k<p; k++) {
1871                 for (int j=0; j<=(n+k-1)/2; j++) {
1872                         if (k == 0) {
1873                                 if (n & 1) {
1874                                         if (j & 1) {
1875                                                 result = result - 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1) / cln::factorial(2*j);
1876                                         }
1877                                         else {
1878                                                 result = result + 2 * cln::expt(cln::pi(),2*j) * S_num(n-2*j,p,1) / cln::factorial(2*j);
1879                                         }
1880                                 }
1881                         }
1882                         else {
1883                                 if (k & 1) {
1884                                         if (j & 1) {
1885                                                 result = result + cln::factorial(n+k-1)
1886                                                                   * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1887                                                                   / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1888                                         }
1889                                         else {
1890                                                 result = result - cln::factorial(n+k-1)
1891                                                                   * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1892                                                                   / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1893                                         }
1894                                 }
1895                                 else {
1896                                         if (j & 1) {
1897                                                 result = result - cln::factorial(n+k-1) * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1898                                                                   / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1899                                         }
1900                                         else {
1901                                                 result = result + cln::factorial(n+k-1)
1902                                                                   * cln::expt(cln::pi(),2*j) * S_num(n+k-2*j,p-k,1)
1903                                                                   / (cln::factorial(k) * cln::factorial(n-1) * cln::factorial(2*j));
1904                                         }
1905                                 }
1906                         }
1907                 }
1908         }
1909         int np = n+p;
1910         if ((np-1) & 1) {
1911                 if (((np)/2+n) & 1) {
1912                         result = -result - cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
1913                 }
1914                 else {
1915                         result = -result + cln::expt(cln::pi(),np) / (np * cln::factorial(n-1) * cln::factorial(p));
1916                 }
1917         }
1918
1919         return result;
1920 }
1921
1922
1923 // helper function for S(n,p,x)
1924 // [Kol] remark to (9.1)
1925 cln::cl_N a_k(int k)
1926 {
1927         cln::cl_N result;
1928
1929         if (k == 0) {
1930                 return 1;
1931         }
1932
1933         result = result;
1934         for (int m=2; m<=k; m++) {
1935                 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * a_k(k-m);
1936         }
1937
1938         return -result / k;
1939 }
1940
1941
1942 // helper function for S(n,p,x)
1943 // [Kol] remark to (9.1)
1944 cln::cl_N b_k(int k)
1945 {
1946         cln::cl_N result;
1947
1948         if (k == 0) {
1949                 return 1;
1950         }
1951
1952         result = result;
1953         for (int m=2; m<=k; m++) {
1954                 result = result + cln::expt(cln::cl_N(-1),m) * cln::zeta(m) * b_k(k-m);
1955         }
1956
1957         return result / k;
1958 }
1959
1960
1961 // helper function for S(n,p,x)
1962 cln::cl_N S_do_sum(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
1963 {
1964         static cln::float_format_t oldprec = cln::default_float_format;
1965
1966         if (p==1) {
1967                 return Li_projection(n+1, x, prec);
1968         }
1969
1970         // precision has changed, we need to clear lookup table Yn
1971         if ( oldprec != prec ) {
1972                 Yn.clear();
1973                 ynsize = 0;
1974                 ynlength = 100;
1975                 oldprec = prec;
1976         }
1977                 
1978         // check if precalculated values are sufficient
1979         if (p > ynsize+1) {
1980                 for (int i=ynsize; i<p-1; i++) {
1981                         fill_Yn(i, prec);
1982                 }
1983         }
1984
1985         // should be done otherwise
1986         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
1987         cln::cl_N xf = x * one;
1988         //cln::cl_N xf = x * cln::cl_float(1, prec);
1989
1990         cln::cl_N res;
1991         cln::cl_N resbuf;
1992         cln::cl_N factor = cln::expt(xf, p);
1993         int i = p;
1994         do {
1995                 resbuf = res;
1996                 if (i-p >= ynlength) {
1997                         // make Yn longer
1998                         make_Yn_longer(ynlength*2, prec);
1999                 }
2000                 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? ...
2001                 //res = res + factor / cln::expt(cln::cl_I(i),n+1) * (*it); // should we check it? or rely on magic number? ...
2002                 factor = factor * xf;
2003                 i++;
2004         } while (res != resbuf);
2005         
2006         return res;
2007 }
2008
2009
2010 // helper function for S(n,p,x)
2011 cln::cl_N S_projection(int n, int p, const cln::cl_N& x, const cln::float_format_t& prec)
2012 {
2013         // [Kol] (5.3)
2014         if (cln::abs(cln::realpart(x)) > cln::cl_F("0.5")) {
2015
2016                 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(x),n)
2017                                    * cln::expt(cln::log(1-x),p) / cln::factorial(n) / cln::factorial(p);
2018
2019                 for (int s=0; s<n; s++) {
2020                         cln::cl_N res2;
2021                         for (int r=0; r<p; r++) {
2022                                 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-x),r)
2023                                               * S_do_sum(p-r,n-s,1-x,prec) / cln::factorial(r);
2024                         }
2025                         result = result + cln::expt(cln::log(x),s) * (S_num(n-s,p,1) - res2) / cln::factorial(s);
2026                 }
2027
2028                 return result;
2029         }
2030         
2031         return S_do_sum(n, p, x, prec);
2032 }
2033
2034
2035 // helper function for S(n,p,x)
2036 const cln::cl_N S_num(int n, int p, const cln::cl_N& x)
2037 {
2038         if (x == 1) {
2039                 if (n == 1) {
2040                     // [Kol] (2.22) with (2.21)
2041                         return cln::zeta(p+1);
2042                 }
2043
2044                 if (p == 1) {
2045                     // [Kol] (2.22)
2046                         return cln::zeta(n+1);
2047                 }
2048
2049                 // [Kol] (9.1)
2050                 cln::cl_N result;
2051                 for (int nu=0; nu<n; nu++) {
2052                         for (int rho=0; rho<=p; rho++) {
2053                                 result = result + b_k(n-nu-1) * b_k(p-rho) * a_k(nu+rho+1)
2054                                                   * cln::factorial(nu+rho+1) / cln::factorial(rho) / cln::factorial(nu+1);
2055                         }
2056                 }
2057                 result = result * cln::expt(cln::cl_I(-1),n+p-1);
2058
2059                 return result;
2060         }
2061         else if (x == -1) {
2062                 // [Kol] (2.22)
2063                 if (p == 1) {
2064                         return -(1-cln::expt(cln::cl_I(2),-n)) * cln::zeta(n+1);
2065                 }
2066 //              throw std::runtime_error("don't know how to evaluate this function!");
2067         }
2068
2069         // what is the desired float format?
2070         // first guess: default format
2071         cln::float_format_t prec = cln::default_float_format;
2072         const cln::cl_N value = x;
2073         // second guess: the argument's format
2074         if (!instanceof(realpart(value), cln::cl_RA_ring))
2075                 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
2076         else if (!instanceof(imagpart(value), cln::cl_RA_ring))
2077                 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
2078
2079         // [Kol] (5.3)
2080         // the condition abs(1-value)>1 avoids an infinite recursion in the region abs(value)<=1 && abs(value)>0.95 && abs(1-value)<=1 && abs(1-value)>0.95
2081         // we don't care here about abs(value)<1 && real(value)>0.5, this will be taken care of in S_projection
2082         if ((cln::realpart(value) < -0.5) || (n == 0) || ((cln::abs(value) <= 1) && (cln::abs(value) > 0.95) && (cln::abs(1-value) > 1) )) {
2083
2084                 cln::cl_N result = cln::expt(cln::cl_I(-1),p) * cln::expt(cln::log(value),n)
2085                                    * cln::expt(cln::log(1-value),p) / cln::factorial(n) / cln::factorial(p);
2086
2087                 for (int s=0; s<n; s++) {
2088                         cln::cl_N res2;
2089                         for (int r=0; r<p; r++) {
2090                                 res2 = res2 + cln::expt(cln::cl_I(-1),r) * cln::expt(cln::log(1-value),r)
2091                                               * S_num(p-r,n-s,1-value) / cln::factorial(r);
2092                         }
2093                         result = result + cln::expt(cln::log(value),s) * (S_num(n-s,p,1) - res2) / cln::factorial(s);
2094                 }
2095
2096                 return result;
2097                 
2098         }
2099         // [Kol] (5.12)
2100         if (cln::abs(value) > 1) {
2101                 
2102                 cln::cl_N result;
2103
2104                 for (int s=0; s<p; s++) {
2105                         for (int r=0; r<=s; r++) {
2106                                 result = result + cln::expt(cln::cl_I(-1),s) * cln::expt(cln::log(-value),r) * cln::factorial(n+s-r-1)
2107                                                   / cln::factorial(r) / cln::factorial(s-r) / cln::factorial(n-1)
2108                                                   * S_num(n+s-r,p-s,cln::recip(value));
2109                         }
2110                 }
2111                 result = result * cln::expt(cln::cl_I(-1),n);
2112
2113                 cln::cl_N res2;
2114                 for (int r=0; r<n; r++) {
2115                         res2 = res2 + cln::expt(cln::log(-value),r) * C(n-r,p) / cln::factorial(r);
2116                 }
2117                 res2 = res2 + cln::expt(cln::log(-value),n+p) / cln::factorial(n+p);
2118
2119                 result = result + cln::expt(cln::cl_I(-1),p) * res2;
2120
2121                 return result;
2122         }
2123
2124         if ((cln::abs(value) > 0.95) && (cln::abs(value-9.53) < 9.47)) {
2125                 lst m;
2126                 m.append(n+1);
2127                 for (int s=0; s<p-1; s++)
2128                         m.append(1);
2129
2130                 ex res = H(m,numeric(value)).evalf();
2131                 return ex_to<numeric>(res).to_cl_N();
2132         }
2133         else {
2134                 return S_projection(n, p, value, prec);
2135         }
2136 }
2137
2138
2139 } // end of anonymous namespace
2140
2141
2142 //////////////////////////////////////////////////////////////////////
2143 //
2144 // Nielsen's generalized polylogarithm  S(n,p,x)
2145 //
2146 // GiNaC function
2147 //
2148 //////////////////////////////////////////////////////////////////////
2149
2150
2151 static ex S_evalf(const ex& n, const ex& p, const ex& x)
2152 {
2153         if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
2154                 const int n_ = ex_to<numeric>(n).to_int();
2155                 const int p_ = ex_to<numeric>(p).to_int();
2156                 if (is_a<numeric>(x)) {
2157                         const cln::cl_N x_ = ex_to<numeric>(x).to_cl_N();
2158                         const cln::cl_N result = S_num(n_, p_, x_);
2159                         return numeric(result);
2160                 } else {
2161                         ex x_val = x.evalf();
2162                         if (is_a<numeric>(x_val)) {
2163                                 const cln::cl_N x_val_ = ex_to<numeric>(x_val).to_cl_N();
2164                                 const cln::cl_N result = S_num(n_, p_, x_val_);
2165                                 return numeric(result);
2166                         }
2167                 }
2168         }
2169         return S(n, p, x).hold();
2170 }
2171
2172
2173 static ex S_eval(const ex& n, const ex& p, const ex& x)
2174 {
2175         if (n.info(info_flags::posint) && p.info(info_flags::posint)) {
2176                 if (x == 0) {
2177                         return _ex0;
2178                 }
2179                 if (x == 1) {
2180                         lst m(n+1);
2181                         for (int i=ex_to<numeric>(p).to_int()-1; i>0; i--) {
2182                                 m.append(1);
2183                         }
2184                         return zeta(m);
2185                 }
2186                 if (p == 1) {
2187                         return Li(n+1, x);
2188                 }
2189                 if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
2190                         int n_ = ex_to<numeric>(n).to_int();
2191                         int p_ = ex_to<numeric>(p).to_int();
2192                         const cln::cl_N x_ = ex_to<numeric>(x).to_cl_N();
2193                         const cln::cl_N result = S_num(n_, p_, x_);
2194                         return numeric(result);
2195                 }
2196         }
2197         if (n.is_zero()) {
2198                 // [Kol] (5.3)
2199                 return pow(-log(1-x), p) / factorial(p);
2200         }
2201         return S(n, p, x).hold();
2202 }
2203
2204
2205 static ex S_series(const ex& n, const ex& p, const ex& x, const relational& rel, int order, unsigned options)
2206 {
2207         if (p == _ex1) {
2208                 return Li(n+1, x).series(rel, order, options);
2209         }
2210
2211         const ex x_pt = x.subs(rel, subs_options::no_pattern);
2212         if (n.info(info_flags::posint) && p.info(info_flags::posint) && x_pt.info(info_flags::numeric)) {
2213                 // First special case: x==0 (derivatives have poles)
2214                 if (x_pt.is_zero()) {
2215                         const symbol s;
2216                         ex ser;
2217                         // manually construct the primitive expansion
2218                         // subsum = Euler-Zagier-Sum is needed
2219                         // dirty hack (slow ...) calculation of subsum:
2220                         std::vector<ex> presubsum, subsum;
2221                         subsum.push_back(0);
2222                         for (int i=1; i<order-1; ++i) {
2223                                 subsum.push_back(subsum[i-1] + numeric(1, i));
2224                         }
2225                         for (int depth=2; depth<p; ++depth) {
2226                                 presubsum = subsum;
2227                                 for (int i=1; i<order-1; ++i) {
2228                                         subsum[i] = subsum[i-1] + numeric(1, i) * presubsum[i-1];
2229                                 }
2230                         }
2231                                 
2232                         for (int i=1; i<order; ++i) {
2233                                 ser += pow(s,i) / pow(numeric(i), n+1) * subsum[i-1];
2234                         }
2235                         // substitute the argument's series expansion
2236                         ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
2237                         // maybe that was terminating, so add a proper order term
2238                         epvector nseq;
2239                         nseq.push_back(expair(Order(_ex1), order));
2240                         ser += pseries(rel, nseq);
2241                         // reexpanding it will collapse the series again
2242                         return ser.series(rel, order);
2243                 }
2244                 // TODO special cases: x==1 (branch point) and x real, >=1 (branch cut)
2245                 throw std::runtime_error("S_series: don't know how to do the series expansion at this point!");
2246         }
2247         // all other cases should be safe, by now:
2248         throw do_taylor();  // caught by function::series()
2249 }
2250
2251
2252 static ex S_deriv(const ex& n, const ex& p, const ex& x, unsigned deriv_param)
2253 {
2254         GINAC_ASSERT(deriv_param < 3);
2255         if (deriv_param < 2) {
2256                 return _ex0;
2257         }
2258         if (n > 0) {
2259                 return S(n-1, p, x) / x;
2260         } else {
2261                 return S(n, p-1, x) / (1-x);
2262         }
2263 }
2264
2265
2266 static void S_print_latex(const ex& n, const ex& p, const ex& x, const print_context& c)
2267 {
2268         c.s << "\\mathrm{S}_{";
2269         n.print(c);
2270         c.s << ",";
2271         p.print(c);
2272         c.s << "}(";
2273         x.print(c);
2274         c.s << ")";
2275 }
2276
2277
2278 REGISTER_FUNCTION(S,
2279                   evalf_func(S_evalf).
2280                   eval_func(S_eval).
2281                   series_func(S_series).
2282                   derivative_func(S_deriv).
2283                   print_func<print_latex>(S_print_latex).
2284                   do_not_evalf_params());
2285
2286
2287 //////////////////////////////////////////////////////////////////////
2288 //
2289 // Harmonic polylogarithm  H(m,x)
2290 //
2291 // helper functions
2292 //
2293 //////////////////////////////////////////////////////////////////////
2294
2295
2296 // anonymous namespace for helper functions
2297 namespace {
2298
2299         
2300 // regulates the pole (used by 1/x-transformation)
2301 symbol H_polesign("IMSIGN");
2302
2303
2304 // convert parameters from H to Li representation
2305 // parameters are expected to be in expanded form, i.e. only 0, 1 and -1
2306 // returns true if some parameters are negative
2307 bool convert_parameter_H_to_Li(const lst& l, lst& m, lst& s, ex& pf)
2308 {
2309         // expand parameter list
2310         lst mexp;
2311         for (lst::const_iterator it = l.begin(); it != l.end(); it++) {
2312                 if (*it > 1) {
2313                         for (ex count=*it-1; count > 0; count--) {
2314                                 mexp.append(0);
2315                         }
2316                         mexp.append(1);
2317                 } else if (*it < -1) {
2318                         for (ex count=*it+1; count < 0; count++) {
2319                                 mexp.append(0);
2320                         }
2321                         mexp.append(-1);
2322                 } else {
2323                         mexp.append(*it);
2324                 }
2325         }
2326         
2327         ex signum = 1;
2328         pf = 1;
2329         bool has_negative_parameters = false;
2330         ex acc = 1;
2331         for (lst::const_iterator it = mexp.begin(); it != mexp.end(); it++) {
2332                 if (*it == 0) {
2333                         acc++;
2334                         continue;
2335                 }
2336                 if (*it > 0) {
2337                         m.append((*it+acc-1) * signum);
2338                 } else {
2339                         m.append((*it-acc+1) * signum);
2340                 }
2341                 acc = 1;
2342                 signum = *it;
2343                 pf *= *it;
2344                 if (pf < 0) {
2345                         has_negative_parameters = true;
2346                 }
2347         }
2348         if (has_negative_parameters) {
2349                 for (std::size_t i=0; i<m.nops(); i++) {
2350                         if (m.op(i) < 0) {
2351                                 m.let_op(i) = -m.op(i);
2352                                 s.append(-1);
2353                         } else {
2354                                 s.append(1);
2355                         }
2356                 }
2357         }
2358         
2359         return has_negative_parameters;
2360 }
2361
2362
2363 // recursivly transforms H to corresponding multiple polylogarithms
2364 struct map_trafo_H_convert_to_Li : public map_function
2365 {
2366         ex operator()(const ex& e)
2367         {
2368                 if (is_a<add>(e) || is_a<mul>(e)) {
2369                         return e.map(*this);
2370                 }
2371                 if (is_a<function>(e)) {
2372                         std::string name = ex_to<function>(e).get_name();
2373                         if (name == "H") {
2374                                 lst parameter;
2375                                 if (is_a<lst>(e.op(0))) {
2376                                                 parameter = ex_to<lst>(e.op(0));
2377                                 } else {
2378                                         parameter = lst(e.op(0));
2379                                 }
2380                                 ex arg = e.op(1);
2381
2382                                 lst m;
2383                                 lst s;
2384                                 ex pf;
2385                                 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
2386                                         s.let_op(0) = s.op(0) * arg;
2387                                         return pf * Li(m, s).hold();
2388                                 } else {
2389                                         for (std::size_t i=0; i<m.nops(); i++) {
2390                                                 s.append(1);
2391                                         }
2392                                         s.let_op(0) = s.op(0) * arg;
2393                                         return Li(m, s).hold();
2394                                 }
2395                         }
2396                 }
2397                 return e;
2398         }
2399 };
2400
2401
2402 // recursivly transforms H to corresponding zetas
2403 struct map_trafo_H_convert_to_zeta : public map_function
2404 {
2405         ex operator()(const ex& e)
2406         {
2407                 if (is_a<add>(e) || is_a<mul>(e)) {
2408                         return e.map(*this);
2409                 }
2410                 if (is_a<function>(e)) {
2411                         std::string name = ex_to<function>(e).get_name();
2412                         if (name == "H") {
2413                                 lst parameter;
2414                                 if (is_a<lst>(e.op(0))) {
2415                                                 parameter = ex_to<lst>(e.op(0));
2416                                 } else {
2417                                         parameter = lst(e.op(0));
2418                                 }
2419
2420                                 lst m;
2421                                 lst s;
2422                                 ex pf;
2423                                 if (convert_parameter_H_to_Li(parameter, m, s, pf)) {
2424                                         return pf * zeta(m, s);
2425                                 } else {
2426                                         return zeta(m);
2427                                 }
2428                         }
2429                 }
2430                 return e;
2431         }
2432 };
2433
2434
2435 // remove trailing zeros from H-parameters
2436 struct map_trafo_H_reduce_trailing_zeros : public map_function
2437 {
2438         ex operator()(const ex& e)
2439         {
2440                 if (is_a<add>(e) || is_a<mul>(e)) {
2441                         return e.map(*this);
2442                 }
2443                 if (is_a<function>(e)) {
2444                         std::string name = ex_to<function>(e).get_name();
2445                         if (name == "H") {
2446                                 lst parameter;
2447                                 if (is_a<lst>(e.op(0))) {
2448                                         parameter = ex_to<lst>(e.op(0));
2449                                 } else {
2450                                         parameter = lst(e.op(0));
2451                                 }
2452                                 ex arg = e.op(1);
2453                                 if (parameter.op(parameter.nops()-1) == 0) {
2454                                         
2455                                         //
2456                                         if (parameter.nops() == 1) {
2457                                                 return log(arg);
2458                                         }
2459                                         
2460                                         //
2461                                         lst::const_iterator it = parameter.begin();
2462                                         while ((it != parameter.end()) && (*it == 0)) {
2463                                                 it++;
2464                                         }
2465                                         if (it == parameter.end()) {
2466                                                 return pow(log(arg),parameter.nops()) / factorial(parameter.nops());
2467                                         }
2468                                         
2469                                         //
2470                                         parameter.remove_last();
2471                                         std::size_t lastentry = parameter.nops();
2472                                         while ((lastentry > 0) && (parameter[lastentry-1] == 0)) {
2473                                                 lastentry--;
2474                                         }
2475                                         
2476                                         //
2477                                         ex result = log(arg) * H(parameter,arg).hold();
2478                                         ex acc = 0;
2479                                         for (ex i=0; i<lastentry; i++) {
2480                                                 if (parameter[i] > 0) {
2481                                                         parameter[i]++;
2482                                                         result -= (acc + parameter[i]-1) * H(parameter, arg).hold();
2483                                                         parameter[i]--;
2484                                                         acc = 0;
2485                                                 } else if (parameter[i] < 0) {
2486                                                         parameter[i]--;
2487                                                         result -= (acc + abs(parameter[i]+1)) * H(parameter, arg).hold();
2488                                                         parameter[i]++;
2489                                                         acc = 0;
2490                                                 } else {
2491                                                         acc++;
2492                                                 }
2493                                         }
2494                                         
2495                                         if (lastentry < parameter.nops()) {
2496                                                 result = result / (parameter.nops()-lastentry+1);
2497                                                 return result.map(*this);
2498                                         } else {
2499                                                 return result;
2500                                         }
2501                                 }
2502                         }
2503                 }
2504                 return e;
2505         }
2506 };
2507
2508
2509 // returns an expression with zeta functions corresponding to the parameter list for H
2510 ex convert_H_to_zeta(const lst& m)
2511 {
2512         symbol xtemp("xtemp");
2513         map_trafo_H_reduce_trailing_zeros filter;
2514         map_trafo_H_convert_to_zeta filter2;
2515         return filter2(filter(H(m, xtemp).hold())).subs(xtemp == 1);
2516 }
2517
2518
2519 // convert signs form Li to H representation
2520 lst convert_parameter_Li_to_H(const lst& m, const lst& x, ex& pf)
2521 {
2522         lst res;
2523         lst::const_iterator itm = m.begin();
2524         lst::const_iterator itx = ++x.begin();
2525         int signum = 1;
2526         pf = _ex1;
2527         res.append(*itm);
2528         itm++;
2529         while (itx != x.end()) {
2530                 GINAC_ASSERT((*itx == _ex1) || (*itx == _ex_1));
2531                 // XXX: 1 + 0.0*I is considered equal to 1. However the former
2532                 // is not automatically converted to a real number.
2533                 // Do the conversion explicitly to avoid the
2534                 // "numeric::operator>(): complex inequality" exception.
2535                 signum *= (*itx != _ex_1) ? 1 : -1;
2536                 pf *= signum;
2537                 res.append((*itm) * signum);
2538                 itm++;
2539                 itx++;
2540         }
2541         return res;
2542 }
2543
2544
2545 // multiplies an one-dimensional H with another H
2546 // [ReV] (18)
2547 ex trafo_H_mult(const ex& h1, const ex& h2)
2548 {
2549         ex res;
2550         ex hshort;
2551         lst hlong;
2552         ex h1nops = h1.op(0).nops();
2553         ex h2nops = h2.op(0).nops();
2554         if (h1nops > 1) {
2555                 hshort = h2.op(0).op(0);
2556                 hlong = ex_to<lst>(h1.op(0));
2557         } else {
2558                 hshort = h1.op(0).op(0);
2559                 if (h2nops > 1) {
2560                         hlong = ex_to<lst>(h2.op(0));
2561                 } else {
2562                         hlong = h2.op(0).op(0);
2563                 }
2564         }
2565         for (std::size_t i=0; i<=hlong.nops(); i++) {
2566                 lst newparameter;
2567                 std::size_t j=0;
2568                 for (; j<i; j++) {
2569                         newparameter.append(hlong[j]);
2570                 }
2571                 newparameter.append(hshort);
2572                 for (; j<hlong.nops(); j++) {
2573                         newparameter.append(hlong[j]);
2574                 }
2575                 res += H(newparameter, h1.op(1)).hold();
2576         }
2577         return res;
2578 }
2579
2580
2581 // applies trafo_H_mult recursively on expressions
2582 struct map_trafo_H_mult : public map_function
2583 {
2584         ex operator()(const ex& e)
2585         {
2586                 if (is_a<add>(e)) {
2587                         return e.map(*this);
2588                 }
2589
2590                 if (is_a<mul>(e)) {
2591
2592                         ex result = 1;
2593                         ex firstH;
2594                         lst Hlst;
2595                         for (std::size_t pos=0; pos<e.nops(); pos++) {
2596                                 if (is_a<power>(e.op(pos)) && is_a<function>(e.op(pos).op(0))) {
2597                                         std::string name = ex_to<function>(e.op(pos).op(0)).get_name();
2598                                         if (name == "H") {
2599                                                 for (ex i=0; i<e.op(pos).op(1); i++) {
2600                                                         Hlst.append(e.op(pos).op(0));
2601                                                 }
2602                                                 continue;
2603                                         }
2604                                 } else if (is_a<function>(e.op(pos))) {
2605                                         std::string name = ex_to<function>(e.op(pos)).get_name();
2606                                         if (name == "H") {
2607                                                 if (e.op(pos).op(0).nops() > 1) {
2608                                                         firstH = e.op(pos);
2609                                                 } else {
2610                                                         Hlst.append(e.op(pos));
2611                                                 }
2612                                                 continue;
2613                                         }
2614                                 }
2615                                 result *= e.op(pos);
2616                         }
2617                         if (firstH == 0) {
2618                                 if (Hlst.nops() > 0) {
2619                                         firstH = Hlst[Hlst.nops()-1];
2620                                         Hlst.remove_last();
2621                                 } else {
2622                                         return e;
2623                                 }
2624                         }
2625
2626                         if (Hlst.nops() > 0) {
2627                                 ex buffer = trafo_H_mult(firstH, Hlst.op(0));
2628                                 result *= buffer;
2629                                 for (std::size_t i=1; i<Hlst.nops(); i++) {
2630                                         result *= Hlst.op(i);
2631                                 }
2632                                 result = result.expand();
2633                                 map_trafo_H_mult recursion;
2634                                 return recursion(result);
2635                         } else {
2636                                 return e;
2637                         }
2638
2639                 }
2640                 return e;
2641         }
2642 };
2643
2644
2645 // do integration [ReV] (55)
2646 // put parameter 0 in front of existing parameters
2647 ex trafo_H_1tx_prepend_zero(const ex& e, const ex& arg)
2648 {
2649         ex h;
2650         std::string name;
2651         if (is_a<function>(e)) {
2652                 name = ex_to<function>(e).get_name();
2653         }
2654         if (name == "H") {
2655                 h = e;
2656         } else {
2657                 for (std::size_t i=0; i<e.nops(); i++) {
2658                         if (is_a<function>(e.op(i))) {
2659                                 std::string name = ex_to<function>(e.op(i)).get_name();
2660                                 if (name == "H") {
2661                                         h = e.op(i);
2662                                 }
2663                         }
2664                 }
2665         }
2666         if (h != 0) {
2667                 lst newparameter = ex_to<lst>(h.op(0));
2668                 newparameter.prepend(0);
2669                 ex addzeta = convert_H_to_zeta(newparameter);
2670                 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
2671         } else {
2672                 return e * (-H(lst(ex(0)),1/arg).hold());
2673         }
2674 }
2675
2676
2677 // do integration [ReV] (49)
2678 // put parameter 1 in front of existing parameters
2679 ex trafo_H_prepend_one(const ex& e, const ex& arg)
2680 {
2681         ex h;
2682         std::string name;
2683         if (is_a<function>(e)) {
2684                 name = ex_to<function>(e).get_name();
2685         }
2686         if (name == "H") {
2687                 h = e;
2688         } else {
2689                 for (std::size_t i=0; i<e.nops(); i++) {
2690                         if (is_a<function>(e.op(i))) {
2691                                 std::string name = ex_to<function>(e.op(i)).get_name();
2692                                 if (name == "H") {
2693                                         h = e.op(i);
2694                                 }
2695                         }
2696                 }
2697         }
2698         if (h != 0) {
2699                 lst newparameter = ex_to<lst>(h.op(0));
2700                 newparameter.prepend(1);
2701                 return e.subs(h == H(newparameter, h.op(1)).hold());
2702         } else {
2703                 return e * H(lst(ex(1)),1-arg).hold();
2704         }
2705 }
2706
2707
2708 // do integration [ReV] (55)
2709 // put parameter -1 in front of existing parameters
2710 ex trafo_H_1tx_prepend_minusone(const ex& e, const ex& arg)
2711 {
2712         ex h;
2713         std::string name;
2714         if (is_a<function>(e)) {
2715                 name = ex_to<function>(e).get_name();
2716         }
2717         if (name == "H") {
2718                 h = e;
2719         } else {
2720                 for (std::size_t i=0; i<e.nops(); i++) {
2721                         if (is_a<function>(e.op(i))) {
2722                                 std::string name = ex_to<function>(e.op(i)).get_name();
2723                                 if (name == "H") {
2724                                         h = e.op(i);
2725                                 }
2726                         }
2727                 }
2728         }
2729         if (h != 0) {
2730                 lst newparameter = ex_to<lst>(h.op(0));
2731                 newparameter.prepend(-1);
2732                 ex addzeta = convert_H_to_zeta(newparameter);
2733                 return e.subs(h == (addzeta-H(newparameter, h.op(1)).hold())).expand();
2734         } else {
2735                 ex addzeta = convert_H_to_zeta(lst(ex(-1)));
2736                 return (e * (addzeta - H(lst(ex(-1)),1/arg).hold())).expand();
2737         }
2738 }
2739
2740
2741 // do integration [ReV] (55)
2742 // put parameter -1 in front of existing parameters
2743 ex trafo_H_1mxt1px_prepend_minusone(const ex& e, const ex& arg)
2744 {
2745         ex h;
2746         std::string name;
2747         if (is_a<function>(e)) {
2748                 name = ex_to<function>(e).get_name();
2749         }
2750         if (name == "H") {
2751                 h = e;
2752         } else {
2753                 for (std::size_t i = 0; i < e.nops(); i++) {
2754                         if (is_a<function>(e.op(i))) {
2755                                 std::string name = ex_to<function>(e.op(i)).get_name();
2756                                 if (name == "H") {
2757                                         h = e.op(i);
2758                                 }
2759                         }
2760                 }
2761         }
2762         if (h != 0) {
2763                 lst newparameter = ex_to<lst>(h.op(0));
2764                 newparameter.prepend(-1);
2765                 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
2766         } else {
2767                 return (e * H(lst(ex(-1)),(1-arg)/(1+arg)).hold()).expand();
2768         }
2769 }
2770
2771
2772 // do integration [ReV] (55)
2773 // put parameter 1 in front of existing parameters
2774 ex trafo_H_1mxt1px_prepend_one(const ex& e, const ex& arg)
2775 {
2776         ex h;
2777         std::string name;
2778         if (is_a<function>(e)) {
2779                 name = ex_to<function>(e).get_name();
2780         }
2781         if (name == "H") {
2782                 h = e;
2783         } else {
2784                 for (std::size_t i = 0; i < e.nops(); i++) {
2785                         if (is_a<function>(e.op(i))) {
2786                                 std::string name = ex_to<function>(e.op(i)).get_name();
2787                                 if (name == "H") {
2788                                         h = e.op(i);
2789                                 }
2790                         }
2791                 }
2792         }
2793         if (h != 0) {
2794                 lst newparameter = ex_to<lst>(h.op(0));
2795                 newparameter.prepend(1);
2796                 return e.subs(h == H(newparameter, h.op(1)).hold()).expand();
2797         } else {
2798                 return (e * H(lst(ex(1)),(1-arg)/(1+arg)).hold()).expand();
2799         }
2800 }
2801
2802
2803 // do x -> 1-x transformation
2804 struct map_trafo_H_1mx : public map_function
2805 {
2806         ex operator()(const ex& e)
2807         {
2808                 if (is_a<add>(e) || is_a<mul>(e)) {
2809                         return e.map(*this);
2810                 }
2811                 
2812                 if (is_a<function>(e)) {
2813                         std::string name = ex_to<function>(e).get_name();
2814                         if (name == "H") {
2815
2816                                 lst parameter = ex_to<lst>(e.op(0));
2817                                 ex arg = e.op(1);
2818
2819                                 // special cases if all parameters are either 0, 1 or -1
2820                                 bool allthesame = true;
2821                                 if (parameter.op(0) == 0) {
2822                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
2823                                                 if (parameter.op(i) != 0) {
2824                                                         allthesame = false;
2825                                                         break;
2826                                                 }
2827                                         }
2828                                         if (allthesame) {
2829                                                 lst newparameter;
2830                                                 for (int i=parameter.nops(); i>0; i--) {
2831                                                         newparameter.append(1);
2832                                                 }
2833                                                 return pow(-1, parameter.nops()) * H(newparameter, 1-arg).hold();
2834                                         }
2835                                 } else if (parameter.op(0) == -1) {
2836                                         throw std::runtime_error("map_trafo_H_1mx: cannot handle weights equal -1!");
2837                                 } else {
2838                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
2839                                                 if (parameter.op(i) != 1) {
2840                                                         allthesame = false;
2841                                                         break;
2842                                                 }
2843                                         }
2844                                         if (allthesame) {
2845                                                 lst newparameter;
2846                                                 for (int i=parameter.nops(); i>0; i--) {
2847                                                         newparameter.append(0);
2848                                                 }
2849                                                 return pow(-1, parameter.nops()) * H(newparameter, 1-arg).hold();
2850                                         }
2851                                 }
2852
2853                                 lst newparameter = parameter;
2854                                 newparameter.remove_first();
2855
2856                                 if (parameter.op(0) == 0) {
2857
2858                                         // leading zero
2859                                         ex res = convert_H_to_zeta(parameter);
2860                                         //ex res = convert_from_RV(parameter, 1).subs(H(wild(1),wild(2))==zeta(wild(1)));
2861                                         map_trafo_H_1mx recursion;
2862                                         ex buffer = recursion(H(newparameter, arg).hold());
2863                                         if (is_a<add>(buffer)) {
2864                                                 for (std::size_t i = 0; i < buffer.nops(); i++) {
2865                                                         res -= trafo_H_prepend_one(buffer.op(i), arg);
2866                                                 }
2867                                         } else {
2868                                                 res -= trafo_H_prepend_one(buffer, arg);
2869                                         }
2870                                         return res;
2871
2872                                 } else {
2873
2874                                         // leading one
2875                                         map_trafo_H_1mx recursion;
2876                                         map_trafo_H_mult unify;
2877                                         ex res = H(lst(ex(1)), arg).hold() * H(newparameter, arg).hold();
2878                                         std::size_t firstzero = 0;
2879                                         while (parameter.op(firstzero) == 1) {
2880                                                 firstzero++;
2881                                         }
2882                                         for (std::size_t i = firstzero-1; i < parameter.nops()-1; i++) {
2883                                                 lst newparameter;
2884                                                 std::size_t j=0;
2885                                                 for (; j<=i; j++) {
2886                                                         newparameter.append(parameter[j+1]);
2887                                                 }
2888                                                 newparameter.append(1);
2889                                                 for (; j<parameter.nops()-1; j++) {
2890                                                         newparameter.append(parameter[j+1]);
2891                                                 }
2892                                                 res -= H(newparameter, arg).hold();
2893                                         }
2894                                         res = recursion(res).expand() / firstzero;
2895                                         return unify(res);
2896                                 }
2897                         }
2898                 }
2899                 return e;
2900         }
2901 };
2902
2903
2904 // do x -> 1/x transformation
2905 struct map_trafo_H_1overx : public map_function
2906 {
2907         ex operator()(const ex& e)
2908         {
2909                 if (is_a<add>(e) || is_a<mul>(e)) {
2910                         return e.map(*this);
2911                 }
2912
2913                 if (is_a<function>(e)) {
2914                         std::string name = ex_to<function>(e).get_name();
2915                         if (name == "H") {
2916
2917                                 lst parameter = ex_to<lst>(e.op(0));
2918                                 ex arg = e.op(1);
2919
2920                                 // special cases if all parameters are either 0, 1 or -1
2921                                 bool allthesame = true;
2922                                 if (parameter.op(0) == 0) {
2923                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
2924                                                 if (parameter.op(i) != 0) {
2925                                                         allthesame = false;
2926                                                         break;
2927                                                 }
2928                                         }
2929                                         if (allthesame) {
2930                                                 return pow(-1, parameter.nops()) * H(parameter, 1/arg).hold();
2931                                         }
2932                                 } else if (parameter.op(0) == -1) {
2933                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
2934                                                 if (parameter.op(i) != -1) {
2935                                                         allthesame = false;
2936                                                         break;
2937                                                 }
2938                                         }
2939                                         if (allthesame) {
2940                                                 map_trafo_H_mult unify;
2941                                                 return unify((pow(H(lst(ex(-1)),1/arg).hold() - H(lst(ex(0)),1/arg).hold(), parameter.nops())
2942                                                        / factorial(parameter.nops())).expand());
2943                                         }
2944                                 } else {
2945                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
2946                                                 if (parameter.op(i) != 1) {
2947                                                         allthesame = false;
2948                                                         break;
2949                                                 }
2950                                         }
2951                                         if (allthesame) {
2952                                                 map_trafo_H_mult unify;
2953                                                 return unify((pow(H(lst(ex(1)),1/arg).hold() + H(lst(ex(0)),1/arg).hold() + H_polesign, parameter.nops())
2954                                                        / factorial(parameter.nops())).expand());
2955                                         }
2956                                 }
2957
2958                                 lst newparameter = parameter;
2959                                 newparameter.remove_first();
2960
2961                                 if (parameter.op(0) == 0) {
2962                                         
2963                                         // leading zero
2964                                         ex res = convert_H_to_zeta(parameter);
2965                                         map_trafo_H_1overx recursion;
2966                                         ex buffer = recursion(H(newparameter, arg).hold());
2967                                         if (is_a<add>(buffer)) {
2968                                                 for (std::size_t i = 0; i < buffer.nops(); i++) {
2969                                                         res += trafo_H_1tx_prepend_zero(buffer.op(i), arg);
2970                                                 }
2971                                         } else {
2972                                                 res += trafo_H_1tx_prepend_zero(buffer, arg);
2973                                         }
2974                                         return res;
2975
2976                                 } else if (parameter.op(0) == -1) {
2977
2978                                         // leading negative one
2979                                         ex res = convert_H_to_zeta(parameter);
2980                                         map_trafo_H_1overx recursion;
2981                                         ex buffer = recursion(H(newparameter, arg).hold());
2982                                         if (is_a<add>(buffer)) {
2983                                                 for (std::size_t i = 0; i < buffer.nops(); i++) {
2984                                                         res += trafo_H_1tx_prepend_zero(buffer.op(i), arg) - trafo_H_1tx_prepend_minusone(buffer.op(i), arg);
2985                                                 }
2986                                         } else {
2987                                                 res += trafo_H_1tx_prepend_zero(buffer, arg) - trafo_H_1tx_prepend_minusone(buffer, arg);
2988                                         }
2989                                         return res;
2990
2991                                 } else {
2992
2993                                         // leading one
2994                                         map_trafo_H_1overx recursion;
2995                                         map_trafo_H_mult unify;
2996                                         ex res = H(lst(ex(1)), arg).hold() * H(newparameter, arg).hold();
2997                                         std::size_t firstzero = 0;
2998                                         while (parameter.op(firstzero) == 1) {
2999                                                 firstzero++;
3000                                         }
3001                                         for (std::size_t i = firstzero-1; i < parameter.nops() - 1; i++) {
3002                                                 lst newparameter;
3003                                                 std::size_t j = 0;
3004                                                 for (; j<=i; j++) {
3005                                                         newparameter.append(parameter[j+1]);
3006                                                 }
3007                                                 newparameter.append(1);
3008                                                 for (; j<parameter.nops()-1; j++) {
3009                                                         newparameter.append(parameter[j+1]);
3010                                                 }
3011                                                 res -= H(newparameter, arg).hold();
3012                                         }
3013                                         res = recursion(res).expand() / firstzero;
3014                                         return unify(res);
3015
3016                                 }
3017
3018                         }
3019                 }
3020                 return e;
3021         }
3022 };
3023
3024
3025 // do x -> (1-x)/(1+x) transformation
3026 struct map_trafo_H_1mxt1px : public map_function
3027 {
3028         ex operator()(const ex& e)
3029         {
3030                 if (is_a<add>(e) || is_a<mul>(e)) {
3031                         return e.map(*this);
3032                 }
3033
3034                 if (is_a<function>(e)) {
3035                         std::string name = ex_to<function>(e).get_name();
3036                         if (name == "H") {
3037
3038                                 lst parameter = ex_to<lst>(e.op(0));
3039                                 ex arg = e.op(1);
3040
3041                                 // special cases if all parameters are either 0, 1 or -1
3042                                 bool allthesame = true;
3043                                 if (parameter.op(0) == 0) {
3044                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
3045                                                 if (parameter.op(i) != 0) {
3046                                                         allthesame = false;
3047                                                         break;
3048                                                 }
3049                                         }
3050                                         if (allthesame) {
3051                                                 map_trafo_H_mult unify;
3052                                                 return unify((pow(-H(lst(ex(1)),(1-arg)/(1+arg)).hold() - H(lst(ex(-1)),(1-arg)/(1+arg)).hold(), parameter.nops())
3053                                                        / factorial(parameter.nops())).expand());
3054                                         }
3055                                 } else if (parameter.op(0) == -1) {
3056                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
3057                                                 if (parameter.op(i) != -1) {
3058                                                         allthesame = false;
3059                                                         break;
3060                                                 }
3061                                         }
3062                                         if (allthesame) {
3063                                                 map_trafo_H_mult unify;
3064                                                 return unify((pow(log(2) - H(lst(ex(-1)),(1-arg)/(1+arg)).hold(), parameter.nops())
3065                                                        / factorial(parameter.nops())).expand());
3066                                         }
3067                                 } else {
3068                                         for (std::size_t i = 1; i < parameter.nops(); i++) {
3069                                                 if (parameter.op(i) != 1) {
3070                                                         allthesame = false;
3071                                                         break;
3072                                                 }
3073                                         }
3074                                         if (allthesame) {
3075                                                 map_trafo_H_mult unify;
3076                                                 return unify((pow(-log(2) - H(lst(ex(0)),(1-arg)/(1+arg)).hold() + H(lst(ex(-1)),(1-arg)/(1+arg)).hold(), parameter.nops())
3077                                                        / factorial(parameter.nops())).expand());
3078                                         }
3079                                 }
3080
3081                                 lst newparameter = parameter;
3082                                 newparameter.remove_first();
3083
3084                                 if (parameter.op(0) == 0) {
3085
3086                                         // leading zero
3087                                         ex res = convert_H_to_zeta(parameter);
3088                                         map_trafo_H_1mxt1px recursion;
3089                                         ex buffer = recursion(H(newparameter, arg).hold());
3090                                         if (is_a<add>(buffer)) {
3091                                                 for (std::size_t i = 0; i < buffer.nops(); i++) {
3092                                                         res -= trafo_H_1mxt1px_prepend_one(buffer.op(i), arg) + trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
3093                                                 }
3094                                         } else {
3095                                                 res -= trafo_H_1mxt1px_prepend_one(buffer, arg) + trafo_H_1mxt1px_prepend_minusone(buffer, arg);
3096                                         }
3097                                         return res;
3098
3099                                 } else if (parameter.op(0) == -1) {
3100
3101                                         // leading negative one
3102                                         ex res = convert_H_to_zeta(parameter);
3103                                         map_trafo_H_1mxt1px recursion;
3104                                         ex buffer = recursion(H(newparameter, arg).hold());
3105                                         if (is_a<add>(buffer)) {
3106                                                 for (std::size_t i = 0; i < buffer.nops(); i++) {
3107                                                         res -= trafo_H_1mxt1px_prepend_minusone(buffer.op(i), arg);
3108                                                 }
3109                                         } else {
3110                                                 res -= trafo_H_1mxt1px_prepend_minusone(buffer, arg);
3111                                         }
3112                                         return res;
3113
3114                                 } else {
3115
3116                                         // leading one
3117                                         map_trafo_H_1mxt1px recursion;
3118                                         map_trafo_H_mult unify;
3119                                         ex res = H(lst(ex(1)), arg).hold() * H(newparameter, arg).hold();
3120                                         std::size_t firstzero = 0;
3121                                         while (parameter.op(firstzero) == 1) {
3122                                                 firstzero++;
3123                                         }
3124                                         for (std::size_t i = firstzero - 1; i < parameter.nops() - 1; i++) {
3125                                                 lst newparameter;
3126                                                 std::size_t j=0;
3127                                                 for (; j<=i; j++) {
3128                                                         newparameter.append(parameter[j+1]);
3129                                                 }
3130                                                 newparameter.append(1);
3131                                                 for (; j<parameter.nops()-1; j++) {
3132                                                         newparameter.append(parameter[j+1]);
3133                                                 }
3134                                                 res -= H(newparameter, arg).hold();
3135                                         }
3136                                         res = recursion(res).expand() / firstzero;
3137                                         return unify(res);
3138
3139                                 }
3140
3141                         }
3142                 }
3143                 return e;
3144         }
3145 };
3146
3147
3148 // do the actual summation.
3149 cln::cl_N H_do_sum(const std::vector<int>& m, const cln::cl_N& x)
3150 {
3151         const int j = m.size();
3152
3153         std::vector<cln::cl_N> t(j);
3154
3155         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3156         cln::cl_N factor = cln::expt(x, j) * one;
3157         cln::cl_N t0buf;
3158         int q = 0;
3159         do {
3160                 t0buf = t[0];
3161                 q++;
3162                 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),m[j-1]);
3163                 for (int k=j-2; k>=1; k--) {
3164                         t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), m[k]);
3165                 }
3166                 t[0] = t[0] + t[1] * factor / cln::expt(cln::cl_I(q+j-1), m[0]);
3167                 factor = factor * x;
3168         } while (t[0] != t0buf);
3169
3170         return t[0];
3171 }
3172
3173
3174 } // end of anonymous namespace
3175
3176
3177 //////////////////////////////////////////////////////////////////////
3178 //
3179 // Harmonic polylogarithm  H(m,x)
3180 //
3181 // GiNaC function
3182 //
3183 //////////////////////////////////////////////////////////////////////
3184
3185
3186 static ex H_evalf(const ex& x1, const ex& x2)
3187 {
3188         if (is_a<lst>(x1)) {
3189                 
3190                 cln::cl_N x;
3191                 if (is_a<numeric>(x2)) {
3192                         x = ex_to<numeric>(x2).to_cl_N();
3193                 } else {
3194                         ex x2_val = x2.evalf();
3195                         if (is_a<numeric>(x2_val)) {
3196                                 x = ex_to<numeric>(x2_val).to_cl_N();
3197                         }
3198                 }
3199
3200                 for (std::size_t i = 0; i < x1.nops(); i++) {
3201                         if (!x1.op(i).info(info_flags::integer)) {
3202                                 return H(x1, x2).hold();
3203                         }
3204                 }
3205                 if (x1.nops() < 1) {
3206                         return H(x1, x2).hold();
3207                 }
3208
3209                 const lst& morg = ex_to<lst>(x1);
3210                 // remove trailing zeros ...
3211                 if (*(--morg.end()) == 0) {
3212                         symbol xtemp("xtemp");
3213                         map_trafo_H_reduce_trailing_zeros filter;
3214                         return filter(H(x1, xtemp).hold()).subs(xtemp==x2).evalf();
3215                 }
3216                 // ... and expand parameter notation
3217                 bool has_minus_one = false;
3218                 lst m;
3219                 for (lst::const_iterator it = morg.begin(); it != morg.end(); it++) {
3220                         if (*it > 1) {
3221                                 for (ex count=*it-1; count > 0; count--) {
3222                                         m.append(0);
3223                                 }
3224                                 m.append(1);
3225                         } else if (*it <= -1) {
3226                                 for (ex count=*it+1; count < 0; count++) {
3227                                         m.append(0);
3228                                 }
3229                                 m.append(-1);
3230                                 has_minus_one = true;
3231                         } else {
3232                                 m.append(*it);
3233                         }
3234                 }
3235
3236                 // do summation
3237                 if (cln::abs(x) < 0.95) {
3238                         lst m_lst;
3239                         lst s_lst;
3240                         ex pf;
3241                         if (convert_parameter_H_to_Li(m, m_lst, s_lst, pf)) {
3242                                 // negative parameters -> s_lst is filled
3243                                 std::vector<int> m_int;
3244                                 std::vector<cln::cl_N> x_cln;
3245                                 for (lst::const_iterator it_int = m_lst.begin(), it_cln = s_lst.begin(); 
3246                                      it_int != m_lst.end(); it_int++, it_cln++) {
3247                                         m_int.push_back(ex_to<numeric>(*it_int).to_int());
3248                                         x_cln.push_back(ex_to<numeric>(*it_cln).to_cl_N());
3249                                 }
3250                                 x_cln.front() = x_cln.front() * x;
3251                                 return pf * numeric(multipleLi_do_sum(m_int, x_cln));
3252                         } else {
3253                                 // only positive parameters
3254                                 //TODO
3255                                 if (m_lst.nops() == 1) {
3256                                         return Li(m_lst.op(0), x2).evalf();
3257                                 }
3258                                 std::vector<int> m_int;
3259                                 for (lst::const_iterator it = m_lst.begin(); it != m_lst.end(); it++) {
3260                                         m_int.push_back(ex_to<numeric>(*it).to_int());
3261                                 }
3262                                 return numeric(H_do_sum(m_int, x));
3263                         }
3264                 }
3265
3266                 symbol xtemp("xtemp");
3267                 ex res = 1;     
3268                 
3269                 // ensure that the realpart of the argument is positive
3270                 if (cln::realpart(x) < 0) {
3271                         x = -x;
3272                         for (std::size_t i = 0; i < m.nops(); i++) {
3273                                 if (m.op(i) != 0) {
3274                                         m.let_op(i) = -m.op(i);
3275                                         res *= -1;
3276                                 }
3277                         }
3278                 }
3279
3280                 // x -> 1/x
3281                 if (cln::abs(x) >= 2.0) {
3282                         map_trafo_H_1overx trafo;
3283                         res *= trafo(H(m, xtemp).hold());
3284                         if (cln::imagpart(x) <= 0) {
3285                                 res = res.subs(H_polesign == -I*Pi);
3286                         } else {
3287                                 res = res.subs(H_polesign == I*Pi);
3288                         }
3289                         return res.subs(xtemp == numeric(x)).evalf();
3290                 }
3291                 
3292                 // check transformations for 0.95 <= |x| < 2.0
3293                 
3294                 // |(1-x)/(1+x)| < 0.9 -> circular area with center=9.53+0i and radius=9.47
3295                 if (cln::abs(x-9.53) <= 9.47) {
3296                         // x -> (1-x)/(1+x)
3297                         map_trafo_H_1mxt1px trafo;
3298                         res *= trafo(H(m, xtemp).hold());
3299                 } else {
3300                         // x -> 1-x
3301                         if (has_minus_one) {
3302                                 map_trafo_H_convert_to_Li filter;
3303                                 return filter(H(m, numeric(x)).hold()).evalf();
3304                         }
3305                         map_trafo_H_1mx trafo;
3306                         res *= trafo(H(m, xtemp).hold());
3307                 }
3308
3309                 return res.subs(xtemp == numeric(x)).evalf();
3310         }
3311
3312         return H(x1,x2).hold();
3313 }
3314
3315
3316 static ex H_eval(const ex& m_, const ex& x)
3317 {
3318         lst m;
3319         if (is_a<lst>(m_)) {
3320                 m = ex_to<lst>(m_);
3321         } else {
3322                 m = lst(m_);
3323         }
3324         if (m.nops() == 0) {
3325                 return _ex1;
3326         }
3327         ex pos1;
3328         ex pos2;
3329         ex n;
3330         ex p;
3331         int step = 0;
3332         if (*m.begin() > _ex1) {
3333                 step++;
3334                 pos1 = _ex0;
3335                 pos2 = _ex1;
3336                 n = *m.begin()-1;
3337                 p = _ex1;
3338         } else if (*m.begin() < _ex_1) {
3339                 step++;
3340                 pos1 = _ex0;
3341                 pos2 = _ex_1;
3342                 n = -*m.begin()-1;
3343                 p = _ex1;
3344         } else if (*m.begin() == _ex0) {
3345                 pos1 = _ex0;
3346                 n = _ex1;
3347         } else {
3348                 pos1 = *m.begin();
3349                 p = _ex1;
3350         }
3351         for (lst::const_iterator it = ++m.begin(); it != m.end(); it++) {
3352                 if ((*it).info(info_flags::integer)) {
3353                         if (step == 0) {
3354                                 if (*it > _ex1) {
3355                                         if (pos1 == _ex0) {
3356                                                 step = 1;
3357                                                 pos2 = _ex1;
3358                                                 n += *it-1;
3359                                                 p = _ex1;
3360                                         } else {
3361                                                 step = 2;
3362                                         }
3363                                 } else if (*it < _ex_1) {
3364                                         if (pos1 == _ex0) {
3365                                                 step = 1;
3366                                                 pos2 = _ex_1;
3367                                                 n += -*it-1;
3368                                                 p = _ex1;
3369                                         } else {
3370                                                 step = 2;
3371                                         }
3372                                 } else {
3373                                         if (*it != pos1) {
3374                                                 step = 1;
3375                                                 pos2 = *it;
3376                                         }
3377                                         if (*it == _ex0) {
3378                                                 n++;
3379                                         } else {
3380                                                 p++;
3381                                         }
3382                                 }
3383                         } else if (step == 1) {
3384                                 if (*it != pos2) {
3385                                         step = 2;
3386                                 } else {
3387                                         if (*it == _ex0) {
3388                                                 n++;
3389                                         } else {
3390                                                 p++;
3391                                         }
3392                                 }
3393                         }
3394                 } else {
3395                         // if some m_i is not an integer
3396                         return H(m_, x).hold();
3397                 }
3398         }
3399         if ((x == _ex1) && (*(--m.end()) != _ex0)) {
3400                 return convert_H_to_zeta(m);
3401         }
3402         if (step == 0) {
3403                 if (pos1 == _ex0) {
3404                         // all zero
3405                         if (x == _ex0) {
3406                                 return H(m_, x).hold();
3407                         }
3408                         return pow(log(x), m.nops()) / factorial(m.nops());
3409                 } else {
3410                         // all (minus) one
3411                         return pow(-pos1*log(1-pos1*x), m.nops()) / factorial(m.nops());
3412                 }
3413         } else if ((step == 1) && (pos1 == _ex0)){
3414                 // convertible to S
3415                 if (pos2 == _ex1) {
3416                         return S(n, p, x);
3417                 } else {
3418                         return pow(-1, p) * S(n, p, -x);
3419                 }
3420         }
3421         if (x == _ex0) {
3422                 return _ex0;
3423         }
3424         if (x.info(info_flags::numeric) && (!x.info(info_flags::crational))) {
3425                 return H(m_, x).evalf();
3426         }
3427         return H(m_, x).hold();
3428 }
3429
3430
3431 static ex H_series(const ex& m, const ex& x, const relational& rel, int order, unsigned options)
3432 {
3433         epvector seq;
3434         seq.push_back(expair(H(m, x), 0));
3435         return pseries(rel, seq);
3436 }
3437
3438
3439 static ex H_deriv(const ex& m_, const ex& x, unsigned deriv_param)
3440 {
3441         GINAC_ASSERT(deriv_param < 2);
3442         if (deriv_param == 0) {
3443                 return _ex0;
3444         }
3445         lst m;
3446         if (is_a<lst>(m_)) {
3447                 m = ex_to<lst>(m_);
3448         } else {
3449                 m = lst(m_);
3450         }
3451         ex mb = *m.begin();
3452         if (mb > _ex1) {
3453                 m[0]--;
3454                 return H(m, x) / x;
3455         }
3456         if (mb < _ex_1) {
3457                 m[0]++;
3458                 return H(m, x) / x;
3459         }
3460         m.remove_first();
3461         if (mb == _ex1) {
3462                 return 1/(1-x) * H(m, x);
3463         } else if (mb == _ex_1) {
3464                 return 1/(1+x) * H(m, x);
3465         } else {
3466                 return H(m, x) / x;
3467         }
3468 }
3469
3470
3471 static void H_print_latex(const ex& m_, const ex& x, const print_context& c)
3472 {
3473         lst m;
3474         if (is_a<lst>(m_)) {
3475                 m = ex_to<lst>(m_);
3476         } else {
3477                 m = lst(m_);
3478         }
3479         c.s << "\\mathrm{H}_{";
3480         lst::const_iterator itm = m.begin();
3481         (*itm).print(c);
3482         itm++;
3483         for (; itm != m.end(); itm++) {
3484                 c.s << ",";
3485                 (*itm).print(c);
3486         }
3487         c.s << "}(";
3488         x.print(c);
3489         c.s << ")";
3490 }
3491
3492
3493 REGISTER_FUNCTION(H,
3494                   evalf_func(H_evalf).
3495                   eval_func(H_eval).
3496                   series_func(H_series).
3497                   derivative_func(H_deriv).
3498                   print_func<print_latex>(H_print_latex).
3499                   do_not_evalf_params());
3500
3501
3502 // takes a parameter list for H and returns an expression with corresponding multiple polylogarithms
3503 ex convert_H_to_Li(const ex& m, const ex& x)
3504 {
3505         map_trafo_H_reduce_trailing_zeros filter;
3506         map_trafo_H_convert_to_Li filter2;
3507         if (is_a<lst>(m)) {
3508                 return filter2(filter(H(m, x).hold()));
3509         } else {
3510                 return filter2(filter(H(lst(m), x).hold()));
3511         }
3512 }
3513
3514
3515 //////////////////////////////////////////////////////////////////////
3516 //
3517 // Multiple zeta values  zeta(x) and zeta(x,s)
3518 //
3519 // helper functions
3520 //
3521 //////////////////////////////////////////////////////////////////////
3522
3523
3524 // anonymous namespace for helper functions
3525 namespace {
3526
3527
3528 // parameters and data for [Cra] algorithm
3529 const cln::cl_N lambda = cln::cl_N("319/320");
3530
3531 void halfcyclic_convolute(const std::vector<cln::cl_N>& a, const std::vector<cln::cl_N>& b, std::vector<cln::cl_N>& c)
3532 {
3533         const int size = a.size();
3534         for (int n=0; n<size; n++) {
3535                 c[n] = 0;
3536                 for (int m=0; m<=n; m++) {
3537                         c[n] = c[n] + a[m]*b[n-m];
3538                 }
3539         }
3540 }
3541
3542
3543 // [Cra] section 4
3544 static void initcX(std::vector<cln::cl_N>& crX,
3545                    const std::vector<int>& s,
3546                    const int L2)
3547 {
3548         std::vector<cln::cl_N> crB(L2 + 1);
3549         for (int i=0; i<=L2; i++)
3550                 crB[i] = bernoulli(i).to_cl_N() / cln::factorial(i);
3551
3552         int Sm = 0;
3553         int Smp1 = 0;
3554         std::vector<std::vector<cln::cl_N> > crG(s.size() - 1, std::vector<cln::cl_N>(L2 + 1));
3555         for (int m=0; m < (int)s.size() - 1; m++) {
3556                 Sm += s[m];
3557                 Smp1 = Sm + s[m+1];
3558                 for (int i = 0; i <= L2; i++)
3559                         crG[m][i] = cln::factorial(i + Sm - m - 2) / cln::factorial(i + Smp1 - m - 2);
3560         }
3561
3562         crX = crB;
3563
3564         for (std::size_t m = 0; m < s.size() - 1; m++) {
3565                 std::vector<cln::cl_N> Xbuf(L2 + 1);
3566                 for (int i = 0; i <= L2; i++)
3567                         Xbuf[i] = crX[i] * crG[m][i];
3568
3569                 halfcyclic_convolute(Xbuf, crB, crX);
3570         }
3571 }
3572
3573
3574 // [Cra] section 4
3575 static cln::cl_N crandall_Y_loop(const cln::cl_N& Sqk,
3576                                  const std::vector<cln::cl_N>& crX)
3577 {
3578         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3579         cln::cl_N factor = cln::expt(lambda, Sqk);
3580         cln::cl_N res = factor / Sqk * crX[0] * one;
3581         cln::cl_N resbuf;
3582         int N = 0;
3583         do {
3584                 resbuf = res;
3585                 factor = factor * lambda;
3586                 N++;
3587                 res = res + crX[N] * factor / (N+Sqk);
3588         } while ((res != resbuf) || cln::zerop(crX[N]));
3589         return res;
3590 }
3591
3592
3593 // [Cra] section 4
3594 static void calc_f(std::vector<std::vector<cln::cl_N> >& f_kj,
3595                    const int maxr, const int L1)
3596 {
3597         cln::cl_N t0, t1, t2, t3, t4;
3598         int i, j, k;
3599         std::vector<std::vector<cln::cl_N> >::iterator it = f_kj.begin();
3600         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3601         
3602         t0 = cln::exp(-lambda);
3603         t2 = 1;
3604         for (k=1; k<=L1; k++) {
3605                 t1 = k * lambda;
3606                 t2 = t0 * t2;
3607                 for (j=1; j<=maxr; j++) {
3608                         t3 = 1;
3609                         t4 = 1;
3610                         for (i=2; i<=j; i++) {
3611                                 t4 = t4 * (j-i+1);
3612                                 t3 = t1 * t3 + t4;
3613                         }
3614                         (*it).push_back(t2 * t3 * cln::expt(cln::cl_I(k),-j) * one);
3615                 }
3616                 it++;
3617         }
3618 }
3619
3620
3621 // [Cra] (3.1)
3622 static cln::cl_N crandall_Z(const std::vector<int>& s,
3623                             const std::vector<std::vector<cln::cl_N> >& f_kj)
3624 {
3625         const int j = s.size();
3626
3627         if (j == 1) {   
3628                 cln::cl_N t0;
3629                 cln::cl_N t0buf;
3630                 int q = 0;
3631                 do {
3632                         t0buf = t0;
3633                         q++;
3634                         t0 = t0 + f_kj[q+j-2][s[0]-1];
3635                 } while (t0 != t0buf);
3636                 
3637                 return t0 / cln::factorial(s[0]-1);
3638         }
3639
3640         std::vector<cln::cl_N> t(j);
3641
3642         cln::cl_N t0buf;
3643         int q = 0;
3644         do {
3645                 t0buf = t[0];
3646                 q++;
3647                 t[j-1] = t[j-1] + 1 / cln::expt(cln::cl_I(q),s[j-1]);
3648                 for (int k=j-2; k>=1; k--) {
3649                         t[k] = t[k] + t[k+1] / cln::expt(cln::cl_I(q+j-1-k), s[k]);
3650                 }
3651                 t[0] = t[0] + t[1] * f_kj[q+j-2][s[0]-1];
3652         } while (t[0] != t0buf);
3653         
3654         return t[0] / cln::factorial(s[0]-1);
3655 }
3656
3657
3658 // [Cra] (2.4)
3659 cln::cl_N zeta_do_sum_Crandall(const std::vector<int>& s)
3660 {
3661         std::vector<int> r = s;
3662         const int j = r.size();
3663
3664         std::size_t L1;
3665
3666         // decide on maximal size of f_kj for crandall_Z
3667         if (Digits < 50) {
3668                 L1 = 150;
3669         } else {
3670                 L1 = Digits * 3 + j*2;
3671         }
3672
3673         std::size_t L2;
3674         // decide on maximal size of crX for crandall_Y
3675         if (Digits < 38) {
3676                 L2 = 63;
3677         } else if (Digits < 86) {
3678                 L2 = 127;
3679         } else if (Digits < 192) {
3680                 L2 = 255;
3681         } else if (Digits < 394) {
3682                 L2 = 511;
3683         } else if (Digits < 808) {
3684                 L2 = 1023;
3685         } else {
3686                 L2 = 2047;
3687         }
3688
3689         cln::cl_N res;
3690
3691         int maxr = 0;
3692         int S = 0;
3693         for (int i=0; i<j; i++) {
3694                 S += r[i];
3695                 if (r[i] > maxr) {
3696                         maxr = r[i];
3697                 }
3698         }
3699
3700         std::vector<std::vector<cln::cl_N> > f_kj(L1);
3701         calc_f(f_kj, maxr, L1);
3702
3703         const cln::cl_N r0factorial = cln::factorial(r[0]-1);
3704
3705         std::vector<int> rz;
3706         int skp1buf;
3707         int Srun = S;
3708         for (int k=r.size()-1; k>0; k--) {
3709
3710                 rz.insert(rz.begin(), r.back());
3711                 skp1buf = rz.front();
3712                 Srun -= skp1buf;
3713                 r.pop_back();
3714
3715                 std::vector<cln::cl_N> crX;
3716                 initcX(crX, r, L2);
3717                 
3718                 for (int q=0; q<skp1buf; q++) {
3719                         
3720                         cln::cl_N pp1 = crandall_Y_loop(Srun+q-k, crX);
3721                         cln::cl_N pp2 = crandall_Z(rz, f_kj);
3722
3723                         rz.front()--;
3724                         
3725                         if (q & 1) {
3726                                 res = res - pp1 * pp2 / cln::factorial(q);
3727                         } else {
3728                                 res = res + pp1 * pp2 / cln::factorial(q);
3729                         }
3730                 }
3731                 rz.front() = skp1buf;
3732         }
3733         rz.insert(rz.begin(), r.back());
3734
3735         std::vector<cln::cl_N> crX;
3736         initcX(crX, rz, L2);
3737
3738         res = (res + crandall_Y_loop(S-j, crX)) / r0factorial
3739                 + crandall_Z(rz, f_kj);
3740
3741         return res;
3742 }
3743
3744
3745 cln::cl_N zeta_do_sum_simple(const std::vector<int>& r)
3746 {
3747         const int j = r.size();
3748
3749         // buffer for subsums
3750         std::vector<cln::cl_N> t(j);
3751         cln::cl_F one = cln::cl_float(1, cln::float_format(Digits));
3752
3753         cln::cl_N t0buf;
3754         int q = 0;
3755         do {
3756                 t0buf = t[0];
3757                 q++;
3758                 t[j-1] = t[j-1] + one / cln::expt(cln::cl_I(q),r[j-1]);
3759                 for (int k=j-2; k>=0; k--) {
3760                         t[k] = t[k] + one * t[k+1] / cln::expt(cln::cl_I(q+j-1-k), r[k]);
3761                 }
3762         } while (t[0] != t0buf);
3763
3764         return t[0];
3765 }
3766
3767
3768 // does Hoelder convolution. see [BBB] (7.0)
3769 cln::cl_N zeta_do_Hoelder_convolution(const std::vector<int>& m_, const std::vector<int>& s_)
3770 {
3771         // prepare parameters
3772         // holds Li arguments in [BBB] notation
3773         std::vector<int> s = s_;
3774         std::vector<int> m_p = m_;
3775         std::vector<int> m_q;
3776         // holds Li arguments in nested sums notation
3777         std::vector<cln::cl_N> s_p(s.size(), cln::cl_N(1));
3778         s_p[0] = s_p[0] * cln::cl_N("1/2");
3779         // convert notations
3780         int sig = 1;
3781         for (std::size_t i = 0; i < s_.size(); i++) {
3782                 if (s_[i] < 0) {
3783                         sig = -sig;
3784                         s_p[i] = -s_p[i];
3785                 }
3786                 s[i] = sig * std::abs(s[i]);
3787         }
3788         std::vector<cln::cl_N> s_q;
3789         cln::cl_N signum = 1;
3790
3791         // first term
3792         cln::cl_N res = multipleLi_do_sum(m_p, s_p);
3793
3794         // middle terms
3795         do {
3796
3797                 // change parameters
3798                 if (s.front() > 0) {
3799                         if (m_p.front() == 1) {
3800                                 m_p.erase(m_p.begin());
3801                                 s_p.erase(s_p.begin());
3802                                 if (s_p.size() > 0) {
3803                                         s_p.front() = s_p.front() * cln::cl_N("1/2");
3804                                 }
3805                                 s.erase(s.begin());
3806                                 m_q.front()++;
3807                         } else {
3808                                 m_p.front()--;
3809                                 m_q.insert(m_q.begin(), 1);
3810                                 if (s_q.size() > 0) {
3811                                         s_q.front() = s_q.front() * 2;
3812                                 }
3813                                 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
3814                         }
3815                 } else {
3816                         if (m_p.front() == 1) {
3817                                 m_p.erase(m_p.begin());
3818                                 cln::cl_N spbuf = s_p.front();
3819                                 s_p.erase(s_p.begin());
3820                                 if (s_p.size() > 0) {
3821                                         s_p.front() = s_p.front() * spbuf;
3822                                 }
3823                                 s.erase(s.begin());
3824                                 m_q.insert(m_q.begin(), 1);
3825                                 if (s_q.size() > 0) {
3826                                         s_q.front() = s_q.front() * 4;
3827                                 }
3828                                 s_q.insert(s_q.begin(), cln::cl_N("1/4"));
3829                                 signum = -signum;
3830                         } else {
3831                                 m_p.front()--;
3832                                 m_q.insert(m_q.begin(), 1);
3833                                 if (s_q.size() > 0) {
3834                                         s_q.front() = s_q.front() * 2;
3835                                 }
3836                                 s_q.insert(s_q.begin(), cln::cl_N("1/2"));
3837                         }
3838                 }
3839
3840                 // exiting the loop
3841                 if (m_p.size() == 0) break;
3842
3843                 res = res + signum * multipleLi_do_sum(m_p, s_p) * multipleLi_do_sum(m_q, s_q);
3844
3845         } while (true);
3846
3847         // last term
3848         res = res + signum * multipleLi_do_sum(m_q, s_q);
3849
3850         return res;
3851 }
3852
3853
3854 } // end of anonymous namespace
3855
3856
3857 //////////////////////////////////////////////////////////////////////
3858 //
3859 // Multiple zeta values  zeta(x)
3860 //
3861 // GiNaC function
3862 //
3863 //////////////////////////////////////////////////////////////////////
3864
3865
3866 static ex zeta1_evalf(const ex& x)
3867 {
3868         if (is_exactly_a<lst>(x) && (x.nops()>1)) {
3869
3870                 // multiple zeta value
3871                 const int count = x.nops();
3872                 const lst& xlst = ex_to<lst>(x);
3873                 std::vector<int> r(count);
3874
3875                 // check parameters and convert them
3876                 lst::const_iterator it1 = xlst.begin();
3877                 std::vector<int>::iterator it2 = r.begin();
3878                 do {
3879                         if (!(*it1).info(info_flags::posint)) {
3880                                 return zeta(x).hold();
3881                         }
3882                         *it2 = ex_to<numeric>(*it1).to_int();
3883                         it1++;
3884                         it2++;
3885                 } while (it2 != r.end());
3886
3887                 // check for divergence
3888                 if (r[0] == 1) {
3889                         return zeta(x).hold();
3890                 }
3891
3892                 // decide on summation algorithm
3893                 // this is still a bit clumsy
3894                 int limit = (Digits>17) ? 10 : 6;
3895                 if ((r[0] < limit) || ((count > 3) && (r[1] < limit/2))) {
3896                         return numeric(zeta_do_sum_Crandall(r));
3897                 } else {
3898                         return numeric(zeta_do_sum_simple(r));
3899                 }
3900         }
3901
3902         // single zeta value
3903         if (is_exactly_a<numeric>(x) && (x != 1)) {
3904                 try {
3905                         return zeta(ex_to<numeric>(x));
3906                 } catch (const dunno &e) { }
3907         }
3908
3909         return zeta(x).hold();
3910 }
3911
3912
3913 static ex zeta1_eval(const ex& m)
3914 {
3915         if (is_exactly_a<lst>(m)) {
3916                 if (m.nops() == 1) {
3917                         return zeta(m.op(0));
3918                 }
3919                 return zeta(m).hold();
3920         }
3921
3922         if (m.info(info_flags::numeric)) {
3923                 const numeric& y = ex_to<numeric>(m);
3924                 // trap integer arguments:
3925                 if (y.is_integer()) {
3926                         if (y.is_zero()) {
3927                                 return _ex_1_2;
3928                         }
3929                         if (y.is_equal(*_num1_p)) {
3930                                 return zeta(m).hold();
3931                         }
3932                         if (y.info(info_flags::posint)) {
3933                                 if (y.info(info_flags::odd)) {
3934                                         return zeta(m).hold();
3935                                 } else {
3936                                         return abs(bernoulli(y)) * pow(Pi, y) * pow(*_num2_p, y-(*_num1_p)) / factorial(y);
3937                                 }
3938                         } else {
3939                                 if (y.info(info_flags::odd)) {
3940                                         return -bernoulli((*_num1_p)-y) / ((*_num1_p)-y);
3941                                 } else {
3942                                         return _ex0;
3943                                 }
3944                         }
3945                 }
3946                 // zeta(float)
3947                 if (y.info(info_flags::numeric) && !y.info(info_flags::crational)) {
3948                         return zeta1_evalf(m);
3949                 }
3950         }
3951         return zeta(m).hold();
3952 }
3953
3954
3955 static ex zeta1_deriv(const ex& m, unsigned deriv_param)
3956 {
3957         GINAC_ASSERT(deriv_param==0);
3958
3959         if (is_exactly_a<lst>(m)) {
3960                 return _ex0;
3961         } else {
3962                 return zetaderiv(_ex1, m);
3963         }
3964 }
3965
3966
3967 static void zeta1_print_latex(const ex& m_, const print_context& c)
3968 {
3969         c.s << "\\zeta(";
3970         if (is_a<lst>(m_)) {
3971                 const lst& m = ex_to<lst>(m_);
3972                 lst::const_iterator it = m.begin();
3973                 (*it).print(c);
3974                 it++;
3975                 for (; it != m.end(); it++) {
3976                         c.s << ",";
3977                         (*it).print(c);
3978                 }
3979         } else {
3980                 m_.print(c);
3981         }
3982         c.s << ")";
3983 }
3984
3985
3986 unsigned zeta1_SERIAL::serial = function::register_new(function_options("zeta", 1).
3987                                 evalf_func(zeta1_evalf).
3988                                 eval_func(zeta1_eval).
3989                                 derivative_func(zeta1_deriv).
3990                                 print_func<print_latex>(zeta1_print_latex).
3991                                 do_not_evalf_params().
3992                                 overloaded(2));
3993
3994
3995 //////////////////////////////////////////////////////////////////////
3996 //
3997 // Alternating Euler sum  zeta(x,s)
3998 //
3999 // GiNaC function
4000 //
4001 //////////////////////////////////////////////////////////////////////
4002
4003
4004 static ex zeta2_evalf(const ex& x, const ex& s)
4005 {
4006         if (is_exactly_a<lst>(x)) {
4007
4008                 // alternating Euler sum
4009                 const int count = x.nops();
4010                 const lst& xlst = ex_to<lst>(x);
4011                 const lst& slst = ex_to<lst>(s);
4012                 std::vector<int> xi(count);
4013                 std::vector<int> si(count);
4014
4015                 // check parameters and convert them
4016                 lst::const_iterator it_xread = xlst.begin();
4017                 lst::const_iterator it_sread = slst.begin();
4018                 std::vector<int>::iterator it_xwrite = xi.begin();
4019                 std::vector<int>::iterator it_swrite = si.begin();
4020                 do {
4021                         if (!(*it_xread).info(info_flags::posint)) {
4022                                 return zeta(x, s).hold();
4023                         }
4024                         *it_xwrite = ex_to<numeric>(*it_xread).to_int();
4025                         if (*it_sread > 0) {
4026                                 *it_swrite = 1;
4027                         } else {
4028                                 *it_swrite = -1;
4029                         }
4030                         it_xread++;
4031                         it_sread++;
4032                         it_xwrite++;
4033                         it_swrite++;
4034                 } while (it_xwrite != xi.end());
4035
4036                 // check for divergence
4037                 if ((xi[0] == 1) && (si[0] == 1)) {
4038                         return zeta(x, s).hold();
4039                 }
4040
4041                 // use Hoelder convolution
4042                 return numeric(zeta_do_Hoelder_convolution(xi, si));
4043         }
4044
4045         return zeta(x, s).hold();
4046 }
4047
4048
4049 static ex zeta2_eval(const ex& m, const ex& s_)
4050 {
4051         if (is_exactly_a<lst>(s_)) {
4052                 const lst& s = ex_to<lst>(s_);
4053                 for (lst::const_iterator it = s.begin(); it != s.end(); it++) {
4054                         if ((*it).info(info_flags::positive)) {
4055                                 continue;
4056                         }
4057                         return zeta(m, s_).hold();
4058                 }
4059                 return zeta(m);
4060         } else if (s_.info(info_flags::positive)) {
4061                 return zeta(m);
4062         }
4063
4064         return zeta(m, s_).hold();
4065 }
4066
4067
4068 static ex zeta2_deriv(const ex& m, const ex& s, unsigned deriv_param)
4069 {
4070         GINAC_ASSERT(deriv_param==0);
4071
4072         if (is_exactly_a<lst>(m)) {
4073                 return _ex0;
4074         } else {
4075                 if ((is_exactly_a<lst>(s) && s.op(0).info(info_flags::positive)) || s.info(info_flags::positive)) {
4076                         return zetaderiv(_ex1, m);
4077                 }
4078                 return _ex0;
4079         }
4080 }
4081
4082
4083 static void zeta2_print_latex(const ex& m_, const ex& s_, const print_context& c)
4084 {
4085         lst m;
4086         if (is_a<lst>(m_)) {
4087                 m = ex_to<lst>(m_);
4088         } else {
4089                 m = lst(m_);
4090         }
4091         lst s;
4092         if (is_a<lst>(s_)) {
4093                 s = ex_to<lst>(s_);
4094         } else {
4095                 s = lst(s_);
4096         }
4097         c.s << "\\zeta(";
4098         lst::const_iterator itm = m.begin();
4099         lst::const_iterator its = s.begin();
4100         if (*its < 0) {
4101                 c.s << "\\overline{";
4102                 (*itm).print(c);
4103                 c.s << "}";
4104         } else {
4105                 (*itm).print(c);
4106         }
4107         its++;
4108         itm++;
4109         for (; itm != m.end(); itm++, its++) {
4110                 c.s << ",";
4111                 if (*its < 0) {
4112                         c.s << "\\overline{";
4113                         (*itm).print(c);
4114                         c.s << "}";
4115                 } else {
4116                         (*itm).print(c);
4117                 }
4118         }
4119         c.s << ")";
4120 }
4121
4122
4123 unsigned zeta2_SERIAL::serial = function::register_new(function_options("zeta", 2).
4124                                 evalf_func(zeta2_evalf).
4125                                 eval_func(zeta2_eval).
4126                                 derivative_func(zeta2_deriv).
4127                                 print_func<print_latex>(zeta2_print_latex).
4128                                 do_not_evalf_params().
4129                                 overloaded(2));
4130
4131
4132 } // namespace GiNaC
4133