Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - language - intnum.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 25819-e703fe1174) Lines: 1478 1514 97.6 %
Date: 2020-09-18 06:10:04 Functions: 121 122 99.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : static GEN
      18             : intlin(void *E, GEN (*eval)(void*, GEN), GEN a, GEN b, GEN tab, long prec);
      19             : 
      20             : /********************************************************************/
      21             : /**                NUMERICAL INTEGRATION (Romberg)                 **/
      22             : /********************************************************************/
      23             : typedef struct {
      24             :   void *E;
      25             :   GEN (*f)(void *E, GEN);
      26             : } invfun;
      27             : 
      28             : /* 1/x^2 f(1/x) */
      29             : static GEN
      30       12474 : _invf(void *E, GEN x)
      31             : {
      32       12474 :   invfun *S = (invfun*)E;
      33       12474 :   GEN y = ginv(x);
      34       12474 :   return gmul(S->f(S->E, y), gsqr(y));
      35             : }
      36             : 
      37             : /* h and s are arrays of the same length L > D. The h[i] are (decreasing)
      38             :  * step sizes, s[i] is the computed Riemann sum for step size h[i].
      39             :  * Interpolate the last D+1 values so that s ~ polynomial in h of degree D.
      40             :  * Guess that limit_{h->0} = s(0) */
      41             : static GEN
      42         168 : interp(GEN h, GEN s, long L, long bit, long D)
      43             : {
      44         168 :   pari_sp av = avma;
      45             :   long e1,e2;
      46         168 :   GEN ss = polintspec(h + L-D, s + L-D, gen_0, D+1, &e2);
      47             : 
      48         168 :   e1 = gexpo(ss);
      49         168 :   if (DEBUGLEVEL>2)
      50             :   {
      51           0 :     err_printf("romb: iteration %ld, guess: %Ps\n", L,ss);
      52           0 :     err_printf("romb: relative error < 2^-%ld [target %ld bits]\n",e1-e2,bit);
      53             :   }
      54         168 :   if (e1-e2 <= bit && (L <= 10 || e1 >= -bit)) return gc_NULL(av);
      55          77 :   return cxtoreal(ss);
      56             : }
      57             : 
      58             : static GEN
      59          14 : qrom3(void *E, GEN (*eval)(void *, GEN), GEN a, GEN b, long bit)
      60             : {
      61          14 :   const long JMAX = 25, KLOC = 4;
      62             :   GEN ss,s,h,p1,p2,qlint,del,x,sum;
      63          14 :   long j, j1, it, sig, prec = nbits2prec(bit);
      64             : 
      65          14 :   a = gtofp(a,prec);
      66          14 :   b = gtofp(b,prec);
      67          14 :   qlint = subrr(b,a); sig = signe(qlint);
      68          14 :   if (!sig) return gen_0;
      69          14 :   if (sig < 0) { setabssign(qlint); swap(a,b); }
      70             : 
      71          14 :   s = new_chunk(JMAX+KLOC-1);
      72          14 :   h = new_chunk(JMAX+KLOC-1);
      73          14 :   gel(h,0) = real_1(prec);
      74             : 
      75          14 :   p1 = eval(E, a); if (p1 == a) p1 = rcopy(p1);
      76          14 :   p2 = eval(E, b);
      77          14 :   gel(s,0) = gmul2n(gmul(qlint,gadd(p1,p2)),-1);
      78         112 :   for (it=1,j=1; j<JMAX; j++, it<<=1) /* it = 2^(j-1) */
      79             :   {
      80             :     pari_sp av, av2;
      81         112 :     gel(h,j) = real2n(-2*j, prec); /* 2^(-2j) */
      82         112 :     av = avma; del = divru(qlint,it);
      83         112 :     x = addrr(a, shiftr(del,-1));
      84         112 :     av2 = avma;
      85       28882 :     for (sum = gen_0, j1 = 1; j1 <= it; j1++, x = addrr(x,del))
      86             :     {
      87       28770 :       sum = gadd(sum, eval(E, x));
      88       28770 :       if ((j1 & 0x1ff) == 0) gerepileall(av2, 2, &sum,&x);
      89             :     }
      90         112 :     sum = gmul(sum,del);
      91         112 :     gel(s,j) = gerepileupto(av, gmul2n(gadd(gel(s,j-1), sum), -1));
      92         112 :     if (j >= KLOC && (ss = interp(h, s, j, bit-j-6, KLOC)))
      93          14 :       return gmulsg(sig,ss);
      94             :   }
      95           0 :   pari_err_IMPL("intnumromb recovery [too many iterations]");
      96             :   return NULL; /* LCOV_EXCL_LINE */
      97             : }
      98             : 
      99             : static GEN
     100          63 : qrom2(void *E, GEN (*eval)(void *, GEN), GEN a, GEN b, long bit)
     101             : {
     102          63 :   const long JMAX = 16, KLOC = 4;
     103             :   GEN ss,s,h,p1,qlint,del,ddel,x,sum;
     104          63 :   long j, j1, it, sig, prec = nbits2prec(bit);
     105             : 
     106          63 :   a = gtofp(a, prec);
     107          63 :   b = gtofp(b, prec);
     108          63 :   qlint = subrr(b,a); sig = signe(qlint);
     109          63 :   if (!sig)  return gen_0;
     110          63 :   if (sig < 0) { setabssign(qlint); swap(a,b); }
     111             : 
     112          63 :   s = new_chunk(JMAX+KLOC-1);
     113          63 :   h = new_chunk(JMAX+KLOC-1);
     114          63 :   gel(h,0) = real_1(prec);
     115             : 
     116          63 :   p1 = shiftr(addrr(a,b),-1);
     117          63 :   gel(s,0) = gmul(qlint, eval(E, p1));
     118         287 :   for (it=1, j=1; j<JMAX; j++, it*=3) /* it = 3^(j-1) */
     119             :   {
     120             :     pari_sp av, av2;
     121         287 :     gel(h,j) = divru(gel(h,j-1), 9); /* 3^(-2j) */
     122         287 :     av = avma; del = divru(qlint,3*it); ddel = shiftr(del,1);
     123         287 :     x = addrr(a, shiftr(del,-1));
     124         287 :     av2 = avma;
     125        7910 :     for (sum = gen_0, j1 = 1; j1 <= it; j1++)
     126             :     {
     127        7623 :       sum = gadd(sum, eval(E, x)); x = addrr(x,ddel);
     128        7623 :       sum = gadd(sum, eval(E, x)); x = addrr(x,del);
     129        7623 :       if ((j1 & 0x1ff) == 0) gerepileall(av2, 2, &sum,&x);
     130             :     }
     131         287 :     sum = gmul(sum,del); p1 = gdivgs(gel(s,j-1),3);
     132         287 :     gel(s,j) = gerepileupto(av, gadd(p1,sum));
     133         287 :     if (j >= KLOC && (ss = interp(h, s, j, bit-(3*j/2)+3, KLOC)))
     134          63 :       return gmulsg(sig, ss);
     135             :   }
     136           0 :   pari_err_IMPL("intnumromb recovery [too many iterations]");
     137             :   return NULL; /* LCOV_EXCL_LINE */
     138             : }
     139             : 
     140             : /* integrate after change of variables x --> 1/x */
     141             : static GEN
     142          28 : qromi(void *E, GEN (*eval)(void*, GEN), GEN a, GEN b, long bit)
     143             : {
     144          28 :   GEN A = ginv(b), B = ginv(a);
     145             :   invfun S;
     146          28 :   S.f = eval;
     147          28 :   S.E = E; return qrom2(&S, &_invf, A, B, bit);
     148             : }
     149             : 
     150             : /* a < b, assume b "small" (< 100 say) */
     151             : static GEN
     152          28 : rom_bsmall(void *E, GEN (*eval)(void*, GEN), GEN a, GEN b, long bit)
     153             : {
     154          28 :   if (gcmpgs(a,-100) >= 0) return qrom2(E,eval,a,b,bit);
     155           7 :   if (gcmpgs(b, -1) < 0)   return qromi(E,eval,a,b,bit); /* a<-100, b<-1 */
     156             :   /* a<-100, b>=-1, split at -1 */
     157           7 :   return gadd(qromi(E,eval,a,gen_m1,bit),
     158             :               qrom2(E,eval,gen_m1,b,bit));
     159             : }
     160             : 
     161             : static GEN
     162          35 : rombint(void *E, GEN (*eval)(void*, GEN), GEN a, GEN b, long bit)
     163             : {
     164          35 :   long l = gcmp(b,a);
     165             :   GEN z;
     166             : 
     167          35 :   if (!l) return gen_0;
     168          35 :   if (l < 0) swap(a,b);
     169          35 :   if (gcmpgs(b,100) >= 0)
     170             :   {
     171          14 :     if (gcmpgs(a,1) >= 0)
     172           7 :       z = qromi(E,eval,a,b,bit);
     173             :     else /* split at 1 */
     174           7 :       z = gadd(rom_bsmall(E,eval,a,gen_1,bit), qromi(E,eval,gen_1,b,bit));
     175             :   }
     176             :   else
     177          21 :     z = rom_bsmall(E,eval,a,b,bit);
     178          35 :   if (l < 0) z = gneg(z);
     179          35 :   return z;
     180             : }
     181             : 
     182             : GEN
     183          63 : intnumromb_bitprec(void *E, GEN (*f)(void *, GEN), GEN a,GEN b, long fl, long B)
     184             : {
     185          63 :   pari_sp av = avma;
     186             :   GEN z;
     187          63 :   switch(fl)
     188             :   {
     189          14 :     case 0: z = qrom3  (E, f, a, b, B); break;
     190          35 :     case 1: z = rombint(E, f, a, b, B); break;
     191           7 :     case 2: z = qromi  (E, f, a, b, B); break;
     192           7 :     case 3: z = qrom2  (E, f, a, b, B); break;
     193             :     default: pari_err_FLAG("intnumromb"); return NULL; /* LCOV_EXCL_LINE */
     194             :   }
     195          63 :   return gerepileupto(av, z);
     196             : }
     197             : GEN
     198           0 : intnumromb(void *E, GEN (*f)(void *, GEN), GEN a, GEN b, long flag, long prec)
     199           0 : { return intnumromb_bitprec(E,f,a,b,flag,prec2nbits(prec));}
     200             : GEN
     201          63 : intnumromb0_bitprec(GEN a, GEN b, GEN code, long flag, long bit)
     202          63 : { EXPR_WRAP(code, intnumromb_bitprec(EXPR_ARG, a, b, flag, bit)); }
     203             : 
     204             : /********************************************************************/
     205             : /**             NUMERICAL INTEGRATION (Gauss-Legendre)             **/
     206             : /********************************************************************/
     207             : /* P_N(z) / P'_N(z) if flag = 0, else N! P_N(z) */
     208             : static GEN
     209       11816 : Legendreeval(long N, GEN z, GEN z2, long flag)
     210             : {
     211       11816 :   GEN u0 = z, u1 = subrs(mulur(3, z2), 1), u2;
     212             :   long n;
     213      930461 :   for (n = 2; n < N; n++)
     214             :   {
     215      918645 :     u2 = subrr(mulrr(mulur(2*n+1, z), u1), mulir(sqru(n), u0));
     216      918645 :     u0 = u1; u1 = u2;
     217             :   }
     218       11816 :   if (flag) return u1;
     219       10619 :   return divrr(mulrr(subrs(z2, 1), u1),
     220             :                mulur(N, subrr(mulrr(z, u1), mulur(N, u0))));
     221             : }
     222             : 
     223             : /* Roots of Legendre Polynomials. */
     224             : static GEN
     225        1197 : Legendreroot(long N, double dz, long bit)
     226             : {
     227        1197 :   GEN Z = cgetr(nbits2prec(bit)), z = dbltor(dz), z2;
     228        1197 :   pari_sp av = avma;
     229        1197 :   long pr, j, e = - dblexpo(1 - dz*dz), n = 1 + expu(bit + 32 - e);
     230             : 
     231        1197 :   pr = 1 + e + ((bit - e) >> n);
     232       11816 :   for (j = 1; j <= n; j++)
     233             :   {
     234       10619 :     pr = 2 * pr - e;
     235       10619 :     z = rtor(z, nbits2prec(pr));
     236       10619 :     z2 = sqrr(z);
     237       10619 :     z = subrr(z, Legendreeval(N, z, z2, 0));
     238             :   }
     239        1197 :   affrr(z, Z); return gc_const(av, Z);
     240             : }
     241             : GEN
     242          63 : intnumgaussinit(long N, long prec)
     243             : {
     244          63 :   pari_sp av = avma;
     245             :   long N2, j, k, l, bit;
     246             :   GEN V, W, F;
     247             : 
     248          63 :   prec += EXTRAPREC64;
     249          63 :   bit = prec2nbits(prec);
     250          63 :   if (N <= 0)
     251             :   {
     252          21 :     N = (long)(bit * 0.2258);
     253          21 :     if (odd(N)) N++;
     254             :   }
     255          63 :   if (N == 1) retmkvec2(mkvec(gen_0), mkvec(gen_2));
     256          56 :   if (N == 2)
     257             :   {
     258           7 :     V = mkvec(divru(sqrtr(utor(3,prec)), 3));
     259           7 :     W = mkvec(gen_1); return gerepilecopy(av, mkvec2(V, W));
     260             :   }
     261          49 :   N2 = N >> 1; l = (N+3)>> 1;
     262          49 :   V = cgetg(l, t_VEC);
     263          49 :   W = cgetg(l, t_VEC); F = sqrr(mpfactr(N-1, prec));
     264          49 :   if (!odd(N)) k = 1;
     265             :   else
     266             :   {
     267           7 :     GEN c = sqrr(divrr(sqrr(mpfactr(N2, prec)), F));
     268           7 :     shiftr_inplace(c, 2*(N-1));
     269           7 :     gel(V, 1) = gen_0;
     270           7 :     gel(W, 1) = c; k = 2;
     271             :   }
     272        1246 :   for (j = 4*N2-1; j >= 3; k++, j -= 4)
     273             :   {
     274        1197 :     GEN w, z2, z = Legendreroot(N, cos(M_PI * j / (4*N+2)), bit);
     275        1197 :     pari_sp av = avma;
     276        1197 :     gel(V, k) = z; z2 = sqrr(z);
     277        1197 :     w = divrr(subsr(1, z2), sqrr(Legendreeval(N-1, z, z2, 1)));
     278        1197 :     gel(W, k) = gerepileuptoleaf(av, w);
     279             :   }
     280          49 :   W = RgV_Rg_mul(W, divri(shiftr(F, 1), sqru(N)));
     281          49 :   return gerepilecopy(av, mkvec2(V, W));
     282             : }
     283             : 
     284             : GEN
     285          84 : intnumgauss(void *E, GEN (*eval)(void*, GEN), GEN a, GEN b, GEN tab, long prec)
     286             : {
     287          84 :   pari_sp ltop = avma;
     288             :   GEN R, W, bma, bpa, S;
     289          84 :   long n, i, prec2 = prec + EXTRAPREC64;
     290          84 :   if (!tab)
     291          14 :     tab = intnumgaussinit(0,prec);
     292          70 :   else if (typ(tab) != t_INT)
     293             :   {
     294          35 :     if (typ(tab) != t_VEC || lg(tab) != 3
     295          28 :         || typ(gel(tab,1)) != t_VEC
     296          28 :         || typ(gel(tab,2)) != t_VEC
     297          28 :         || lg(gel(tab,1)) != lg(gel(tab,2)))
     298           7 :       pari_err_TYPE("intnumgauss",tab);
     299             :   }
     300             :   else
     301          35 :     tab = intnumgaussinit(itos(tab),prec);
     302             : 
     303          77 :   R = gel(tab,1); n = lg(R)-1;
     304          77 :   W = gel(tab,2);
     305          77 :   a = gprec_wensure(a, prec2);
     306          77 :   b = gprec_wensure(b, prec2);
     307          77 :   bma = gmul2n(gsub(b,a), -1); /* (b-a)/2 */
     308          77 :   bpa = gadd(bma, a); /* (b+a)/2 */
     309          77 :   if (!signe(gel(R,1)))
     310             :   { /* R[1] = 0, use middle node only once */
     311          14 :     S = gmul(gel(W,1), eval(E, bpa));
     312          14 :     i = 2;
     313             :   }
     314             :   else
     315             :   {
     316          63 :     S = gen_0;
     317          63 :     i = 1;
     318             :   }
     319        1883 :   for (; i <= n; ++i)
     320             :   {
     321        1806 :     GEN h = gmul(bma, gel(R,i)); /* != 0 */
     322        1806 :     GEN P = eval(E, gadd(bpa, h));
     323        1806 :     GEN M = eval(E, gsub(bpa, h));
     324        1806 :     S = gadd(S, gmul(gel(W,i), gadd(P,M)));
     325        1806 :     S = gprec_wensure(S, prec2);
     326             :   }
     327          77 :   return gerepilecopy(ltop, gprec_wtrunc(gmul(bma,S), prec));
     328             : }
     329             : 
     330             : GEN
     331          84 : intnumgauss0(GEN a, GEN b, GEN code, GEN tab, long prec)
     332          84 : { EXPR_WRAP(code, intnumgauss(EXPR_ARG, a, b, tab, prec)); }
     333             : 
     334             : /********************************************************************/
     335             : /**                DOUBLE EXPONENTIAL INTEGRATION                  **/
     336             : /********************************************************************/
     337             : 
     338             : typedef struct _intdata {
     339             :   long bit;  /* bit accuracy of current precision */
     340             :   long l; /* table lengths */
     341             :   GEN tabx0; /* abscissa phi(0) for t = 0 */
     342             :   GEN tabw0; /* weight phi'(0) for t = 0 */
     343             :   GEN tabxp; /* table of abscissas phi(kh) for k > 0 */
     344             :   GEN tabwp; /* table of weights phi'(kh) for k > 0 */
     345             :   GEN tabxm; /* table of abscissas phi(kh) for k < 0, possibly empty */
     346             :   GEN tabwm; /* table of weights phi'(kh) for k < 0, possibly empty */
     347             :   GEN h; /* integration step */
     348             : } intdata;
     349             : 
     350             : static const long LGTAB = 8;
     351             : #define TABh(v) gel(v,1)
     352             : #define TABx0(v) gel(v,2)
     353             : #define TABw0(v) gel(v,3)
     354             : #define TABxp(v) gel(v,4)
     355             : #define TABwp(v) gel(v,5)
     356             : #define TABxm(v) gel(v,6)
     357             : #define TABwm(v) gel(v,7)
     358             : 
     359             : static int
     360       25989 : isinR(GEN z) { return is_real_t(typ(z)); }
     361             : static int
     362       23805 : isinC(GEN z)
     363       23805 : { return (typ(z) == t_COMPLEX)? isinR(gel(z,1)) && isinR(gel(z,2)): isinR(z); }
     364             : 
     365             : static int
     366        9708 : checktabsimp(GEN tab)
     367             : {
     368             :   long L, LN, LW;
     369        9708 :   if (!tab || typ(tab) != t_VEC) return 0;
     370        9708 :   if (lg(tab) != LGTAB) return 0;
     371        9708 :   if (typ(TABxp(tab)) != t_VEC) return 0;
     372        9708 :   if (typ(TABwp(tab)) != t_VEC) return 0;
     373        9708 :   if (typ(TABxm(tab)) != t_VEC) return 0;
     374        9708 :   if (typ(TABwm(tab)) != t_VEC) return 0;
     375        9708 :   L = lg(TABxp(tab)); if (lg(TABwp(tab)) != L) return 0;
     376        9708 :   LN = lg(TABxm(tab)); if (LN != 1 && LN != L) return 0;
     377        9708 :   LW = lg(TABwm(tab)); if (LW != 1 && LW != L) return 0;
     378        9708 :   return 1;
     379             : }
     380             : 
     381             : static int
     382         553 : checktabdoub(GEN tab)
     383             : {
     384             :   long L;
     385         553 :   if (typ(tab) != t_VEC) return 0;
     386         553 :   if (lg(tab) != LGTAB) return 0;
     387         553 :   L = lg(TABxp(tab));
     388         553 :   if (lg(TABwp(tab)) != L) return 0;
     389         553 :   if (lg(TABxm(tab)) != L) return 0;
     390         553 :   if (lg(TABwm(tab)) != L) return 0;
     391         553 :   return 1;
     392             : }
     393             : 
     394             : static int
     395        4693 : checktab(GEN tab)
     396             : {
     397        4693 :   if (typ(tab) != t_VEC) return 0;
     398        4693 :   if (lg(tab) != 3) return checktabsimp(tab);
     399           7 :   return checktabsimp(gel(tab,1))
     400           7 :       && checktabsimp(gel(tab,2));
     401             : }
     402             : 
     403             : /* the TUNE parameter is heuristic */
     404             : static void
     405        1204 : intinit_start(intdata *D, long m, double TUNE, long prec)
     406             : {
     407        1204 :   long l, n, bitprec = prec2nbits(prec);
     408        1204 :   double d = bitprec*LOG10_2;
     409        1204 :   GEN h, nh, pi = mppi(prec);
     410             : 
     411        1204 :   n = (long)ceil(d*log(d) / TUNE); /* heuristic */
     412             :   /* nh ~ log(2npi/log(n)) */
     413        1204 :   nh = logr_abs(divrr(mulur(2*n, pi), logr_abs(utor(n,prec))));
     414        1204 :   h = divru(nh, n);
     415        1204 :   if (m > 0) { h = gmul2n(h,-m); n <<= m; }
     416        1204 :   D->h = h;
     417        1204 :   D->bit = bitprec;
     418        1204 :   D->l = l = n+1;
     419        1204 :   D->tabxp = cgetg(l, t_VEC);
     420        1204 :   D->tabwp = cgetg(l, t_VEC);
     421        1204 :   D->tabxm = cgetg(l, t_VEC);
     422        1204 :   D->tabwm = cgetg(l, t_VEC);
     423        1204 : }
     424             : 
     425             : static GEN
     426        1204 : intinit_end(intdata *D, long pnt, long mnt)
     427             : {
     428        1204 :   GEN v = cgetg(LGTAB, t_VEC);
     429        1204 :   if (pnt < 0) pari_err_DOMAIN("intnuminit","table length","<",gen_0,stoi(pnt));
     430        1204 :   TABx0(v) = D->tabx0;
     431        1204 :   TABw0(v) = D->tabw0;
     432        1204 :   TABh(v) = D->h;
     433        1204 :   TABxp(v) = D->tabxp; setlg(D->tabxp, pnt+1);
     434        1204 :   TABwp(v) = D->tabwp; setlg(D->tabwp, pnt+1);
     435        1204 :   TABxm(v) = D->tabxm; setlg(D->tabxm, mnt+1);
     436        1204 :   TABwm(v) = D->tabwm; setlg(D->tabwm, mnt+1); return v;
     437             : }
     438             : 
     439             : /* divide by 2 in place */
     440             : static GEN
     441      467064 : divr2_ip(GEN x) { shiftr_inplace(x, -1); return x; }
     442             : 
     443             : /* phi(t)=tanh((Pi/2)sinh(t)): from -1 to 1, hence also from a to b compact
     444             :  * interval */
     445             : static GEN
     446         546 : inittanhsinh(long m, long prec)
     447             : {
     448         546 :   GEN e, ei, ek, eik, pi = mppi(prec);
     449         546 :   long k, nt = -1;
     450             :   intdata D;
     451             : 
     452         546 :   intinit_start(&D, m, 1.86, prec);
     453         546 :   D.tabx0 = real_0(prec);
     454         546 :   D.tabw0 = Pi2n(-1,prec);
     455         546 :   e = mpexp(D.h); ek = mulrr(pi, e);
     456         546 :   ei = invr(e); eik = mulrr(pi, ei);
     457      130718 :   for (k = 1; k < D.l; k++)
     458             :   {
     459             :     GEN xp, wp, ct, st, z;
     460             :     pari_sp av;
     461      130718 :     gel(D.tabxp,k) = cgetr(prec);
     462      130718 :     gel(D.tabwp,k) = cgetr(prec); av = avma;
     463      130718 :     ct = divr2_ip(addrr(ek, eik)); /* Pi ch(kh) */
     464      130718 :     st = subrr(ek, ct); /* Pi sh(kh) */
     465      130718 :     z = invr( addrs(mpexp(st), 1) );
     466      130718 :     shiftr_inplace(z, 1); if (expo(z) < -D.bit) { nt = k-1; break; }
     467      130172 :     xp = subsr(1, z);
     468      130172 :     wp = divr2_ip(mulrr(ct, subsr(1, sqrr(xp))));
     469      130172 :     affrr(xp, gel(D.tabxp,k)); mulrrz(ek, e, ek);
     470      130172 :     affrr(wp, gel(D.tabwp,k)); mulrrz(eik, ei, eik); set_avma(av);
     471             :   }
     472         546 :   return intinit_end(&D, nt, 0);
     473             : }
     474             : 
     475             : /* phi(t)=sinh(sinh(t)): from -oo to oo, slowly decreasing, at least
     476             :  * as 1/x^2. */
     477             : static GEN
     478          14 : initsinhsinh(long m, long prec)
     479             : {
     480             :   pari_sp av;
     481             :   GEN et, ct, st, ex;
     482          14 :   long k, nt = -1;
     483             :   intdata D;
     484             : 
     485          14 :   intinit_start(&D, m, 0.666, prec);
     486          14 :   D.tabx0 = real_0(prec);
     487          14 :   D.tabw0 = real_1(prec);
     488          14 :   et = ex = mpexp(D.h);
     489        8184 :   for (k = 1; k < D.l; k++)
     490             :   {
     491             :     GEN xp, wp, ext, exu;
     492        8184 :     gel(D.tabxp,k) = cgetr(prec);
     493        8184 :     gel(D.tabwp,k) = cgetr(prec); av = avma;
     494        8184 :     ct = divr2_ip(addrr(et, invr(et)));
     495        8184 :     st = subrr(et, ct);
     496        8184 :     ext = mpexp(st);
     497        8184 :     exu = invr(ext);
     498        8184 :     xp = divr2_ip(subrr(ext, exu));
     499        8184 :     wp = divr2_ip(mulrr(ct, addrr(ext, exu)));
     500        8184 :     if (expo(wp) - 2*expo(xp) < -D.bit) { nt = k-1; break; }
     501        8170 :     affrr(xp, gel(D.tabxp,k));
     502        8170 :     affrr(wp, gel(D.tabwp,k)); et = gerepileuptoleaf(av, mulrr(et, ex));
     503             :   }
     504          14 :   return intinit_end(&D, nt, 0);
     505             : }
     506             : 
     507             : /* phi(t)=2sinh(t): from -oo to oo, exponentially decreasing as exp(-x) */
     508             : static GEN
     509         133 : initsinh(long m, long prec)
     510             : {
     511             :   pari_sp av;
     512             :   GEN et, ex, eti, xp, wp;
     513         133 :   long k, nt = -1;
     514             :   intdata D;
     515             : 
     516         133 :   intinit_start(&D, m, 1.0, prec);
     517         133 :   D.tabx0 = real_0(prec);
     518         133 :   D.tabw0 = real2n(1, prec);
     519         133 :   et = ex = mpexp(D.h);
     520       39585 :   for (k = 1; k < D.l; k++)
     521             :   {
     522       39585 :     gel(D.tabxp,k) = cgetr(prec);
     523       39585 :     gel(D.tabwp,k) = cgetr(prec); av = avma;
     524       39585 :     eti = invr(et);
     525       39585 :     xp = subrr(et, eti);
     526       39585 :     wp = addrr(et, eti);
     527       39585 :     if (cmprs(xp, (long)(M_LN2*(expo(wp)+D.bit) + 1)) > 0) { nt = k-1; break; }
     528       39452 :     affrr(xp, gel(D.tabxp,k));
     529       39452 :     affrr(wp, gel(D.tabwp,k)); et = gerepileuptoleaf(av, mulrr(et, ex));
     530             :   }
     531         133 :   return intinit_end(&D, nt, 0);
     532             : }
     533             : 
     534             : /* phi(t)=exp(2sinh(t)): from 0 to oo, slowly decreasing at least as 1/x^2 */
     535             : static GEN
     536         259 : initexpsinh(long m, long prec)
     537             : {
     538             :   GEN et, ex;
     539         259 :   long k, nt = -1;
     540             :   intdata D;
     541             : 
     542         259 :   intinit_start(&D, m, 1.05, prec);
     543         259 :   D.tabx0 = real_1(prec);
     544         259 :   D.tabw0 = real2n(1, prec);
     545         259 :   ex = mpexp(D.h);
     546         259 :   et = real_1(prec);
     547      115949 :   for (k = 1; k < D.l; k++)
     548             :   {
     549             :     GEN t, eti, xp;
     550      115949 :     et = mulrr(et, ex);
     551      115949 :     eti = invr(et); t = addrr(et, eti);
     552      115949 :     xp = mpexp(subrr(et, eti));
     553      115949 :     gel(D.tabxp,k) = xp;
     554      115949 :     gel(D.tabwp,k) = mulrr(xp, t);
     555      115949 :     gel(D.tabxm,k) = invr(xp);
     556      115949 :     gel(D.tabwm,k) = mulrr(gel(D.tabxm,k), t);
     557      115949 :     if (expo(gel(D.tabxm,k)) < -D.bit) { nt = k-1; break; }
     558             :   }
     559         259 :   return intinit_end(&D, nt, nt);
     560             : }
     561             : 
     562             : /* phi(t)=exp(t-exp(-t)) : from 0 to +oo, exponentially decreasing. */
     563             : static GEN
     564         140 : initexpexp(long m, long prec)
     565             : {
     566             :   pari_sp av;
     567             :   GEN et, ex;
     568         140 :   long k, nt = -1;
     569             :   intdata D;
     570             : 
     571         140 :   intinit_start(&D, m, 1.76, prec);
     572         140 :   D.tabx0 = mpexp(real_m1(prec));
     573         140 :   D.tabw0 = gmul2n(D.tabx0, 1);
     574         140 :   et = ex = mpexp(negr(D.h));
     575       44408 :   for (k = 1; k < D.l; k++)
     576             :   {
     577             :     GEN xp, xm, wp, wm, eti, kh;
     578       44408 :     gel(D.tabxp,k) = cgetr(prec);
     579       44408 :     gel(D.tabwp,k) = cgetr(prec);
     580       44408 :     gel(D.tabxm,k) = cgetr(prec);
     581       44408 :     gel(D.tabwm,k) = cgetr(prec); av = avma;
     582       44408 :     eti = invr(et); kh = mulur(k,D.h);
     583       44408 :     xp = mpexp(subrr(kh, et));
     584       44408 :     xm = mpexp(negr(addrr(kh, eti)));
     585       44408 :     wp = mulrr(xp, addsr(1, et));
     586       44408 :     if (expo(xm) < -D.bit && cmprs(xp, (long)(M_LN2*(expo(wp)+D.bit) + 1)) > 0) { nt = k-1; break; }
     587       44268 :     wm = mulrr(xm, addsr(1, eti));
     588       44268 :     affrr(xp, gel(D.tabxp,k));
     589       44268 :     affrr(wp, gel(D.tabwp,k));
     590       44268 :     affrr(xm, gel(D.tabxm,k));
     591       44268 :     affrr(wm, gel(D.tabwm,k)); et = gerepileuptoleaf(av, mulrr(et, ex));
     592             :   }
     593         140 :   return intinit_end(&D, nt, nt);
     594             : }
     595             : 
     596             : /* phi(t)=(Pi/h)*t/(1-exp(-sinh(t))) from 0 to oo, sine oscillation */
     597             : static GEN
     598         112 : initnumsine(long m, long prec)
     599             : {
     600             :   pari_sp av;
     601         112 :   GEN invh, et, eti, ex, pi = mppi(prec);
     602         112 :   long exh, k, nt = -1;
     603             :   intdata D;
     604             : 
     605         112 :   intinit_start(&D, m, 0.666, prec);
     606         112 :   invh = invr(D.h);
     607         112 :   D.tabx0 = mulrr(pi, invh);
     608         112 :   D.tabw0 = gmul2n(D.tabx0,-1);
     609         112 :   exh = expo(invh); /*  expo(1/h) */
     610         112 :   et = ex = mpexp(D.h);
     611       90811 :   for (k = 1; k < D.l; k++)
     612             :   {
     613             :     GEN xp,xm, wp,wm, ct,st, extp,extp1,extp2, extm,extm1,extm2, kct, kpi;
     614       90811 :     gel(D.tabxp,k) = cgetr(prec);
     615       90811 :     gel(D.tabwp,k) = cgetr(prec);
     616       90811 :     gel(D.tabxm,k) = cgetr(prec);
     617       90811 :     gel(D.tabwm,k) = cgetr(prec); av = avma;
     618       90811 :     eti = invr(et); /* exp(-kh) */
     619       90811 :     ct = divr2_ip(addrr(et, eti)); /* ch(kh) */
     620       90811 :     st = divr2_ip(subrr(et, eti)); /* sh(kh) */
     621       90811 :     extp = mpexp(st);  extp1 = subsr(1, extp);
     622       90811 :     extp2 = invr(extp1); /* 1/(1-exp(sh(kh))) */
     623       90811 :     extm = invr(extp); extm1 = subsr(1, extm);
     624       90811 :     extm2 = invr(extm1);/* 1/(1-exp(sh(-kh))) */
     625       90811 :     kpi = mulur(k, pi);
     626       90811 :     kct = mulur(k, ct);
     627       90811 :     extm1 = mulrr(extm1, invh);
     628       90811 :     extp1 = mulrr(extp1, invh);
     629       90811 :     xp = mulrr(kpi, extm2); /* phi(kh) */
     630       90811 :     wp = mulrr(subrr(extm1, mulrr(kct, extm)), mulrr(pi, sqrr(extm2)));
     631       90811 :     xm = mulrr(negr(kpi), extp2); /* phi(-kh) */
     632       90811 :     wm = mulrr(addrr(extp1, mulrr(kct, extp)), mulrr(pi, sqrr(extp2)));
     633       90811 :     if (expo(wm) < -D.bit && expo(extm) + exh + expu(10 * k) < -D.bit) { nt = k-1; break; }
     634       90699 :     affrr(xp, gel(D.tabxp,k));
     635       90699 :     affrr(wp, gel(D.tabwp,k));
     636       90699 :     affrr(xm, gel(D.tabxm,k));
     637       90699 :     affrr(wm, gel(D.tabwm,k)); et = gerepileuptoleaf(av, mulrr(et, ex));
     638             :   }
     639         112 :   return intinit_end(&D, nt, nt);
     640             : }
     641             : 
     642             : /* End of initialization functions. These functions can be executed once
     643             :  * and for all for a given accuracy and type of integral ([a,b], [a,oo[ or
     644             :  * ]-oo,a], ]-oo,oo[) */
     645             : 
     646             : /* The numbers below can be changed, but NOT the ordering */
     647             : enum {
     648             :   f_REG     = 0, /* regular function */
     649             :   f_SER     = 1, /* power series */
     650             :   f_SINGSER = 2, /* algebraic singularity, power series endpoint */
     651             :   f_SING    = 3, /* algebraic singularity */
     652             :   f_YSLOW   = 4, /* oo, slowly decreasing, at least x^(-2)  */
     653             :   f_YVSLO   = 5, /* oo, very slowly decreasing, worse than x^(-2) */
     654             :   f_YFAST   = 6, /* oo, exponentially decreasing */
     655             :   f_YOSCS   = 7, /* oo, sine oscillating */
     656             :   f_YOSCC   = 8  /* oo, cosine oscillating */
     657             : };
     658             : /* is finite ? */
     659             : static int
     660         994 : is_fin_f(long c) { return c == f_REG || c == f_SER || c == f_SING; }
     661             : /* is oscillatory ? */
     662             : static int
     663         147 : is_osc(long c) { long a = labs(c); return a == f_YOSCC|| a == f_YOSCS; }
     664             : 
     665             : /* All inner functions such as intn, etc... must be called with a
     666             :  * valid 'tab' table. The wrapper intnum provides a higher level interface */
     667             : 
     668             : /* compute \int_a^b f(t)dt with [a,b] compact and f nonsingular. */
     669             : static GEN
     670        4063 : intn(void *E, GEN (*eval)(void*, GEN), GEN a, GEN b, GEN tab)
     671             : {
     672             :   GEN tabx0, tabw0, tabxp, tabwp;
     673             :   GEN bpa, bma, bmb, S;
     674             :   long i, prec;
     675        4063 :   pari_sp ltop = avma, av;
     676             : 
     677        4063 :   if (!checktabsimp(tab)) pari_err_TYPE("intnum",tab);
     678        4063 :   tabx0 = TABx0(tab); tabw0 = TABw0(tab); prec = gprecision(tabw0);
     679        4063 :   tabxp = TABxp(tab); tabwp = TABwp(tab);
     680        4063 :   bpa = gmul2n(gadd(b, a), -1); /* (b+a)/2 */
     681        4063 :   bma = gsub(bpa, a); /* (b-a)/2 */
     682        4063 :   av = avma;
     683        4063 :   bmb = gmul(bma, tabx0); /* (b-a)/2 phi(0) */
     684             :   /* phi'(0) f( (b+a)/2 + (b-a)/2 * phi(0) ) */
     685        4063 :   S = gmul(tabw0, eval(E, gadd(bpa, bmb)));
     686     1035141 :   for (i = lg(tabxp)-1; i > 0; i--)
     687             :   {
     688             :     GEN SP, SM;
     689     1031078 :     bmb = gmul(bma, gel(tabxp,i));
     690     1031078 :     SP = eval(E, gsub(bpa, bmb));
     691     1031078 :     SM = eval(E, gadd(bpa, bmb));
     692     1031078 :     S = gadd(S, gmul(gel(tabwp,i), gadd(SP, SM)));
     693     1031078 :     if ((i & 0x7f) == 1) S = gerepileupto(av, S);
     694     1031078 :     S = gprec_wensure(S, prec);
     695             :   }
     696        4063 :   return gerepileupto(ltop, gmul(S, gmul(bma, TABh(tab))));
     697             : }
     698             : 
     699             : /* compute \int_a^b f(t)dt with [a,b] compact, possible singularity with
     700             :  * exponent a[2] at lower extremity, b regular. Use tanh(sinh(t)). */
     701             : static GEN
     702         357 : intnsing(void *E, GEN (*eval)(void*, GEN), GEN a, GEN b, GEN tab)
     703             : {
     704             :   GEN tabx0, tabw0, tabxp, tabwp, ea, ba, S;
     705             :   long i, prec;
     706         357 :   pari_sp ltop = avma, av;
     707             : 
     708         357 :   if (!checktabsimp(tab)) pari_err_TYPE("intnum",tab);
     709         357 :   tabx0 = TABx0(tab); tabw0 = TABw0(tab); prec = gprecision(tabw0);
     710         357 :   tabxp = TABxp(tab); tabwp = TABwp(tab);
     711         357 :   ea = ginv(gaddsg(1, gel(a,2)));
     712         357 :   a = gel(a,1);
     713         357 :   ba = gdiv(gsub(b, a), gpow(gen_2, ea, prec));
     714         357 :   av = avma;
     715         357 :   S = gmul(gmul(tabw0, ba), eval(E, gadd(gmul(ba, addsr(1, tabx0)), a)));
     716       88669 :   for (i = lg(tabxp)-1; i > 0; i--)
     717             :   {
     718       88312 :     GEN p = addsr(1, gel(tabxp,i));
     719       88312 :     GEN m = subsr(1, gel(tabxp,i));
     720       88312 :     GEN bp = gmul(ba, gpow(p, ea, prec));
     721       88312 :     GEN bm = gmul(ba, gpow(m, ea, prec));
     722       88312 :     GEN SP = gmul(gdiv(bp, p), eval(E, gadd(bp, a)));
     723       88312 :     GEN SM = gmul(gdiv(bm, m), eval(E, gadd(bm, a)));
     724       88312 :     S = gadd(S, gmul(gel(tabwp,i), gadd(SP, SM)));
     725       88312 :     if ((i & 0x7f) == 1) S = gerepileupto(av, S);
     726       88312 :     S = gprec_wensure(S, prec);
     727             :   }
     728         357 :   return gerepileupto(ltop, gmul(gmul(S, TABh(tab)), ea));
     729             : }
     730             : 
     731      187922 : static GEN id(GEN x) { return x; }
     732             : 
     733             : /* compute  \int_a^oo f(t)dt if si>0 or \int_{-oo}^a f(t)dt if si<0$.
     734             :  * Use exp(2sinh(t)) for slowly decreasing functions, exp(1+t-exp(-t)) for
     735             :  * exponentially decreasing functions, and (pi/h)t/(1-exp(-sinh(t))) for
     736             :  * oscillating functions. */
     737             : static GEN
     738         553 : intninfpm(void *E, GEN (*eval)(void*, GEN), GEN a, long sb, GEN tab)
     739             : {
     740             :   GEN tabx0, tabw0, tabxp, tabwp, tabxm, tabwm;
     741             :   GEN S;
     742             :   long L, i, prec;
     743         553 :   pari_sp av = avma;
     744             : 
     745         553 :   if (!checktabdoub(tab)) pari_err_TYPE("intnum",tab);
     746         553 :   tabx0 = TABx0(tab); tabw0 = TABw0(tab); prec = gprecision(tabw0);
     747         553 :   tabxp = TABxp(tab); tabwp = TABwp(tab); L = lg(tabxp);
     748         553 :   tabxm = TABxm(tab); tabwm = TABwm(tab);
     749         553 :   if (gequal0(a))
     750             :   {
     751         294 :     GEN (*NEG)(GEN) = sb > 0? id: gneg;
     752         294 :     S = gmul(tabw0, eval(E, NEG(tabx0)));
     753      137578 :     for (i = 1; i < L; i++)
     754             :     {
     755      137284 :       GEN SP = eval(E, NEG(gel(tabxp,i)));
     756      137284 :       GEN SM = eval(E, NEG(gel(tabxm,i)));
     757      137284 :       S = gadd(S, gadd(gmul(gel(tabwp,i), SP), gmul(gel(tabwm,i), SM)));
     758      137284 :       if ((i & 0x7f) == 1) S = gerepileupto(av, S);
     759      137284 :       S = gprec_wensure(S, prec);
     760             :     }
     761             :   }
     762         259 :   else if (gexpo(a) <= 0 || is_osc(sb))
     763         119 :   { /* a small */
     764         119 :     GEN (*ADD)(GEN,GEN) = sb > 0? gadd: gsub;
     765         119 :     S = gmul(tabw0, eval(E, ADD(a, tabx0)));
     766       64232 :     for (i = 1; i < L; i++)
     767             :     {
     768       64113 :       GEN SP = eval(E, ADD(a, gel(tabxp,i)));
     769       64113 :       GEN SM = eval(E, ADD(a, gel(tabxm,i)));
     770       64113 :       S = gadd(S, gadd(gmul(gel(tabwp,i), SP), gmul(gel(tabwm,i), SM)));
     771       64113 :       if ((i & 0x7f) == 1) S = gerepileupto(av, S);
     772       64113 :       S = gprec_wensure(S, prec);
     773             :     }
     774             :   }
     775             :   else
     776             :   { /* a large, |a|*\int_sgn(a)^{oo} f(|a|*x)dx (sb > 0)*/
     777         140 :     GEN (*ADD)(long,GEN) = sb > 0? addsr: subsr;
     778         140 :     long sa = gsigne(a);
     779         140 :     GEN A = sa > 0? a: gneg(a);
     780         140 :     pari_sp av2 = avma;
     781         140 :     S = gmul(tabw0, eval(E, gmul(A, ADD(sa, tabx0))));
     782       90091 :     for (i = 1; i < L; i++)
     783             :     {
     784       89951 :       GEN SP = eval(E, gmul(A, ADD(sa, gel(tabxp,i))));
     785       89951 :       GEN SM = eval(E, gmul(A, ADD(sa, gel(tabxm,i))));
     786       89951 :       S = gadd(S, gadd(gmul(gel(tabwp,i), SP), gmul(gel(tabwm,i), SM)));
     787       89951 :       if ((i & 0x7f) == 1) S = gerepileupto(av2, S);
     788       89951 :       S = gprec_wensure(S, prec);
     789             :     }
     790         140 :     S = gmul(S,A);
     791             :   }
     792         553 :   return gerepileupto(av, gmul(S, TABh(tab)));
     793             : }
     794             : 
     795             : /* Compute  \int_{-oo}^oo f(t)dt
     796             :  * use sinh(sinh(t)) for slowly decreasing functions and sinh(t) for
     797             :  * exponentially decreasing functions.
     798             :  * HACK: in case TABwm(tab) contains something, assume function to be integrated
     799             :  * satisfies f(-x) = conj(f(x)).
     800             :  */
     801             : static GEN
     802         588 : intninfinf(void *E, GEN (*eval)(void*, GEN), GEN tab)
     803             : {
     804             :   GEN tabx0, tabw0, tabxp, tabwp, tabwm;
     805             :   GEN S;
     806             :   long L, i, prec, spf;
     807         588 :   pari_sp ltop = avma;
     808             : 
     809         588 :   if (!checktabsimp(tab)) pari_err_TYPE("intnum",tab);
     810         588 :   tabx0 = TABx0(tab); tabw0 = TABw0(tab); prec = gprecision(tabw0);
     811         588 :   tabxp = TABxp(tab); tabwp = TABwp(tab); L = lg(tabxp);
     812         588 :   tabwm = TABwm(tab);
     813         588 :   spf = (lg(tabwm) == lg(tabwp));
     814         588 :   S = gmul(tabw0, eval(E, tabx0));
     815         588 :   if (spf) S = gmul2n(real_i(S), -1);
     816      177541 :   for (i = L-1; i > 0; i--)
     817             :   {
     818      176953 :     GEN SP = eval(E, gel(tabxp,i));
     819      176953 :     if (spf)
     820      171479 :       S = gadd(S, real_i(gmul(gel(tabwp,i), SP)));
     821             :     else
     822             :     {
     823        5474 :       GEN SM = eval(E, negr(gel(tabxp,i)));
     824        5474 :       S = gadd(S, gmul(gel(tabwp,i), gadd(SP,SM)));
     825             :     }
     826      176953 :     if ((i & 0x7f) == 1) S = gerepileupto(ltop, S);
     827      176953 :     S = gprec_wensure(S, prec);
     828             :   }
     829         588 :   if (spf) S = gmul2n(S,1);
     830         588 :   return gerepileupto(ltop, gmul(S, TABh(tab)));
     831             : }
     832             : 
     833             : /* general num integration routine int_a^b f(t)dt, where a and b are as follows:
     834             :  - a scalar : the scalar, no singularity worse than logarithmic at a.
     835             :  - [a, e] : the scalar a, singularity exponent -1 < e <= 0.
     836             :  - +oo: slowly decreasing function (at least O(t^-2))
     837             :  - [[+oo], a], a nonnegative real : +oo, function behaving like exp(-a|t|)
     838             :  - [[+oo], e], e < -1 : +oo, function behaving like t^e
     839             :  - [[+oo], a*I], a > 0 real : +oo, function behaving like cos(at)
     840             :  - [[+oo], a*I], a < 0 real : +oo, function behaving like sin(at)
     841             :  and similarly at -oo */
     842             : static GEN
     843        2030 : f_getycplx(GEN a, long prec)
     844             : {
     845             :   GEN a2R, a2I;
     846             :   long s;
     847             : 
     848        2030 :   if (lg(a) == 2 || gequal0(gel(a,2))) return gen_1;
     849        1988 :   a2R = real_i(gel(a,2));
     850        1988 :   a2I = imag_i(gel(a,2));
     851        1988 :   s = gsigne(a2I); if (s < 0) a2I = gneg(a2I);
     852        1988 :   return ginv(gprec_wensure(s ? a2I : a2R, prec));
     853             : }
     854             : 
     855             : static void
     856          14 : err_code(GEN a, const char *name)
     857             : {
     858          14 :   char *s = stack_sprintf("intnum [incorrect %s]", name);
     859          14 :   pari_err_TYPE(s, a);
     860           0 : }
     861             : 
     862             : /* a = [[+/-oo], alpha]*/
     863             : static long
     864        4431 : code_aux(GEN a, const char *name)
     865             : {
     866        4431 :   GEN re, im, alpha = gel(a,2);
     867             :   long s;
     868        4431 :   if (!isinC(alpha)) err_code(a, name);
     869        4431 :   re = real_i(alpha);
     870        4431 :   im = imag_i(alpha);
     871        4431 :   s = gsigne(im);
     872        4431 :   if (s)
     873             :   {
     874         385 :     if (!gequal0(re)) err_code(a, name);
     875         378 :     return s > 0 ? f_YOSCC : f_YOSCS;
     876             :   }
     877        4046 :   if (gequal0(re) || gcmpgs(re, -2)<=0) return f_YSLOW;
     878        3640 :   if (gsigne(re) > 0) return f_YFAST;
     879         343 :   if (gcmpgs(re, -1) >= 0) err_code(a, name);
     880         343 :   return f_YVSLO;
     881             : }
     882             : 
     883             : static long
     884       24351 : transcode(GEN a, const char *name)
     885             : {
     886             :   GEN a1, a2;
     887       24351 :   switch(typ(a))
     888             :   {
     889        5957 :     case t_VEC: break;
     890         217 :     case t_INFINITY:
     891         217 :       return inf_get_sign(a) == 1 ? f_YSLOW: -f_YSLOW;
     892         259 :     case t_SER: case t_POL: case t_RFRAC:
     893         259 :       return f_SER;
     894       17918 :     default: if (!isinC(a)) err_code(a,name);
     895       17918 :       return f_REG;
     896             :   }
     897        5957 :   switch(lg(a))
     898             :   {
     899          21 :     case 2: return gsigne(gel(a,1)) > 0 ? f_YSLOW : -f_YSLOW;
     900        5929 :     case 3: break;
     901           7 :     default: err_code(a,name);
     902             :   }
     903        5929 :   a1 = gel(a,1);
     904        5929 :   a2 = gel(a,2);
     905        5929 :   switch(typ(a1))
     906             :   {
     907          21 :     case t_VEC:
     908          21 :       if (lg(a1) != 2) err_code(a,name);
     909          21 :       return gsigne(gel(a1,1)) * code_aux(a, name);
     910        4410 :     case t_INFINITY:
     911        4410 :       return inf_get_sign(a1) * code_aux(a, name);
     912          42 :     case t_SER: case t_POL: case t_RFRAC:
     913          42 :       if (!isinR(a2)) err_code(a,name);
     914          42 :       if (gcmpgs(a2, -1) <= 0)
     915           0 :         pari_err_IMPL("intnum with diverging non constant limit");
     916          42 :       return gsigne(a2) < 0 ? f_SINGSER : f_SER;
     917        1456 :     default:
     918        1456 :       if (!isinC(a1) || !isinR(a2) || gcmpgs(a2, -1) <= 0) err_code(a,name);
     919        1456 :       return gsigne(a2) < 0 ? f_SING : f_REG;
     920             :   }
     921             : }
     922             : 
     923             : /* computes the necessary tabs, knowing a, b and m */
     924             : static GEN
     925         420 : homtab(GEN tab, GEN k)
     926             : {
     927             :   GEN z;
     928         420 :   if (gequal0(k) || gequal(k, gen_1)) return tab;
     929         217 :   if (gsigne(k) < 0) k = gneg(k);
     930         217 :   z = cgetg(LGTAB, t_VEC);
     931         217 :   TABx0(z) = gmul(TABx0(tab), k);
     932         217 :   TABw0(z) = gmul(TABw0(tab), k);
     933         217 :   TABxp(z) = gmul(TABxp(tab), k);
     934         217 :   TABwp(z) = gmul(TABwp(tab), k);
     935         217 :   TABxm(z) = gmul(TABxm(tab), k);
     936         217 :   TABwm(z) = gmul(TABwm(tab), k);
     937         217 :   TABh(z) = rcopy(TABh(tab)); return z;
     938             : }
     939             : 
     940             : static GEN
     941         238 : expvec(GEN v, GEN ea, long prec)
     942             : {
     943         238 :   long lv = lg(v), i;
     944         238 :   GEN z = cgetg(lv, t_VEC);
     945      128762 :   for (i = 1; i < lv; i++) gel(z,i) = gpow(gel(v,i),ea,prec);
     946         238 :   return z;
     947             : }
     948             : 
     949             : static GEN
     950      128643 : expscalpr(GEN vnew, GEN xold, GEN wold, GEN ea)
     951             : {
     952      128643 :   pari_sp av = avma;
     953      128643 :   return gerepileupto(av, gdiv(gmul(gmul(vnew, wold), ea), xold));
     954             : }
     955             : static GEN
     956         238 : expvecpr(GEN vnew, GEN xold, GEN wold, GEN ea)
     957             : {
     958         238 :   long lv = lg(vnew), i;
     959         238 :   GEN z = cgetg(lv, t_VEC);
     960      128762 :   for (i = 1; i < lv; i++)
     961      128524 :     gel(z,i) = expscalpr(gel(vnew,i), gel(xold,i), gel(wold,i), ea);
     962         238 :   return z;
     963             : }
     964             : 
     965             : /* here k < -1 */
     966             : static GEN
     967         119 : exptab(GEN tab, GEN k, long prec)
     968             : {
     969             :   GEN v, ea;
     970             : 
     971         119 :   if (gcmpgs(k, -2) <= 0) return tab;
     972         119 :   ea = ginv(gsubsg(-1, k));
     973         119 :   v = cgetg(LGTAB, t_VEC);
     974         119 :   TABx0(v) = gpow(TABx0(tab), ea, prec);
     975         119 :   TABw0(v) = expscalpr(TABx0(v), TABx0(tab), TABw0(tab), ea);
     976         119 :   TABxp(v) = expvec(TABxp(tab), ea, prec);
     977         119 :   TABwp(v) = expvecpr(TABxp(v), TABxp(tab), TABwp(tab), ea);
     978         119 :   TABxm(v) = expvec(TABxm(tab), ea, prec);
     979         119 :   TABwm(v) = expvecpr(TABxm(v), TABxm(tab), TABwm(tab), ea);
     980         119 :   TABh(v) = rcopy(TABh(tab));
     981         119 :   return v;
     982             : }
     983             : 
     984             : static GEN
     985         861 : init_fin(GEN b, long codeb, long m, long l, long prec)
     986             : {
     987         861 :   switch(labs(codeb))
     988             :   {
     989         511 :     case f_REG:
     990         511 :     case f_SING:  return inittanhsinh(m,l);
     991         133 :     case f_YSLOW: return initexpsinh(m,l);
     992          70 :     case f_YVSLO: return exptab(initexpsinh(m,l), gel(b,2), prec);
     993          98 :     case f_YFAST: return homtab(initexpexp(m,l), f_getycplx(b,l));
     994             :     /* f_YOSCS, f_YOSCC */
     995          49 :     default: return homtab(initnumsine(m,l),f_getycplx(b,l));
     996             :   }
     997             : }
     998             : 
     999             : static GEN
    1000        1155 : intnuminit_i(GEN a, GEN b, long m, long prec)
    1001             : {
    1002             :   long codea, codeb, l;
    1003             :   GEN T, kma, kmb, tmp;
    1004             : 
    1005        1155 :   if (m > 30) pari_err_OVERFLOW("intnuminit [m]");
    1006        1155 :   if (m < 0) pari_err_DOMAIN("intnuminit", "m", "<", gen_0, stoi(m));
    1007        1148 :   l = prec+EXTRAPREC64;
    1008        1148 :   codea = transcode(a, "a"); if (codea == f_SER) codea = f_REG;
    1009        1134 :   codeb = transcode(b, "b"); if (codeb == f_SER) codeb = f_REG;
    1010        1134 :   if (codea == f_SINGSER || codeb == f_SINGSER)
    1011           7 :     pari_err_IMPL("intnuminit with singularity at non constant limit");
    1012        1127 :   if (labs(codea) > labs(codeb)) { swap(a, b); lswap(codea, codeb); }
    1013        1127 :   if (codea == f_REG)
    1014             :   {
    1015         770 :     T = init_fin(b, codeb, m,l,prec);
    1016         770 :     switch(labs(codeb))
    1017             :     {
    1018          42 :       case f_YOSCS: if (gequal0(a)) break;
    1019           7 :       case f_YOSCC: T = mkvec2(inittanhsinh(m,l), T);
    1020             :     }
    1021         770 :     return T;
    1022             :   }
    1023         357 :   if (codea == f_SING)
    1024             :   {
    1025          91 :     T = init_fin(b,codeb, m,l,prec);
    1026          91 :     T = mkvec2(labs(codeb) == f_SING? T: inittanhsinh(m,l), T);
    1027          91 :     return T;
    1028             :   }
    1029             :   /* now a and b are infinite */
    1030         266 :   if (codea * codeb > 0) return gen_0;
    1031         252 :   kma = f_getycplx(a,l); codea = labs(codea);
    1032         252 :   kmb = f_getycplx(b,l); codeb = labs(codeb);
    1033         252 :   if (codea == f_YSLOW && codeb == f_YSLOW) return initsinhsinh(m, l);
    1034         238 :   if (codea == f_YFAST && codeb == f_YFAST && gequal(kma, kmb))
    1035         133 :     return homtab(initsinh(m,l), kmb);
    1036         105 :   T = cgetg(3, t_VEC);
    1037         105 :   switch (codea)
    1038             :   {
    1039          56 :     case f_YSLOW:
    1040             :     case f_YVSLO:
    1041          56 :       tmp = initexpsinh(m,l);
    1042          56 :       gel(T,1) = codea == f_YSLOW? tmp: exptab(tmp, gel(a,2), prec);
    1043          56 :       switch (codeb)
    1044             :       {
    1045          14 :         case f_YVSLO: gel(T,2) = exptab(tmp, gel(b,2), prec); return T;
    1046          21 :         case f_YFAST: gel(T,2) = homtab(initexpexp(m,l), kmb); return T;
    1047             :         /* YOSC[CS] */
    1048          21 :         default: gel(T,2) = homtab(initnumsine(m,l), kmb); return T;
    1049             :       }
    1050             :       break;
    1051          21 :     case f_YFAST:
    1052          21 :       tmp = initexpexp(m, l);
    1053          21 :       gel(T,1) = homtab(tmp, kma);
    1054          21 :       switch (codeb)
    1055             :       {
    1056           7 :         case f_YFAST: gel(T,2) = homtab(tmp, kmb); return T;
    1057             :         /* YOSC[CS] */
    1058          14 :         default: gel(T,2) = homtab(initnumsine(m, l), kmb); return T;
    1059             :       }
    1060          28 :     default: /* YOSC[CS] */
    1061          28 :       tmp = initnumsine(m, l);
    1062          28 :       gel(T,1) = homtab(tmp,kma);
    1063          28 :       if (codea == f_YOSCC && codeb == f_YOSCC && !gequal(kma, kmb))
    1064          14 :         gel(T,2) = mkvec2(inittanhsinh(m,l), homtab(tmp,kmb));
    1065             :       else
    1066          14 :         gel(T,2) = homtab(tmp,kmb);
    1067          28 :       return T;
    1068             :   }
    1069             : }
    1070             : GEN
    1071        1008 : intnuminit(GEN a, GEN b, long m, long prec)
    1072             : {
    1073        1008 :   pari_sp av = avma;
    1074        1008 :   return gerepilecopy(av, intnuminit_i(a,b,m,prec));
    1075             : }
    1076             : 
    1077             : static GEN
    1078        5435 : intnuminit0(GEN a, GEN b, GEN tab, long prec)
    1079             : {
    1080             :   long m;
    1081        5435 :   if (!tab) m = 0;
    1082        4707 :   else if (typ(tab) != t_INT)
    1083             :   {
    1084        4693 :     if (!checktab(tab)) pari_err_TYPE("intnuminit0",tab);
    1085        4693 :     return tab;
    1086             :   }
    1087             :   else
    1088          14 :     m = itos(tab);
    1089         742 :   return intnuminit(a, b, m, prec);
    1090             : }
    1091             : 
    1092             : /* Assigns the values of the function weighted by w[k] at quadrature points x[k]
    1093             :  * [replacing the weights]. Return the index of the last non-zero coeff */
    1094             : static long
    1095         266 : weight(void *E, GEN (*eval)(void *, GEN), GEN x, GEN w)
    1096             : {
    1097         266 :   long k, l = lg(x);
    1098       79170 :   for (k = 1; k < l; k++) gel(w,k) = gmul(gel(w,k), eval(E, gel(x,k)));
    1099         266 :   k--; while (k >= 1) if (!gequal0(gel(w,k--))) break;
    1100         266 :   return k;
    1101             : }
    1102             : /* compute the necessary tabs, weights multiplied by f(t) */
    1103             : static GEN
    1104         133 : intfuncinit_i(void *E, GEN (*eval)(void*, GEN), GEN tab)
    1105             : {
    1106         133 :   GEN tabxp = TABxp(tab), tabwp = TABwp(tab);
    1107         133 :   GEN tabxm = TABxm(tab), tabwm = TABwm(tab);
    1108         133 :   long L, L0 = lg(tabxp);
    1109             : 
    1110         133 :   TABw0(tab) = gmul(TABw0(tab), eval(E, TABx0(tab)));
    1111         133 :   if (lg(tabxm) == 1)
    1112             :   {
    1113         133 :     TABxm(tab) = tabxm = gneg(tabxp);
    1114         133 :     TABwm(tab) = tabwm = leafcopy(tabwp);
    1115             :   }
    1116             :   /* update wp and wm in place */
    1117         133 :   L = minss(weight(E, eval, tabxp, tabwp), weight(E, eval, tabxm, tabwm));
    1118         133 :   if (L < L0)
    1119             :   { /* catch up functions whose growth at oo was not adequately described */
    1120         133 :     setlg(tabxp, L+1);
    1121         133 :     setlg(tabwp, L+1);
    1122         133 :     if (lg(tabxm) > 1) { setlg(tabxm, L+1); setlg(tabwm, L+1); }
    1123             :   }
    1124         133 :   return tab;
    1125             : }
    1126             : 
    1127             : GEN
    1128         147 : intfuncinit(void *E, GEN (*eval)(void*, GEN), GEN a, GEN b, long m, long prec)
    1129             : {
    1130         147 :   pari_sp av = avma;
    1131         147 :   GEN tab = intnuminit_i(a, b, m, prec);
    1132             : 
    1133         147 :   if (lg(tab) == 3)
    1134           7 :     pari_err_IMPL("intfuncinit with hard endpoint behavior");
    1135         273 :   if (is_fin_f(transcode(a,"intfuncinit")) ||
    1136         133 :       is_fin_f(transcode(b,"intfuncinit")))
    1137           7 :     pari_err_IMPL("intfuncinit with finite endpoints");
    1138         133 :   return gerepilecopy(av, intfuncinit_i(E, eval, tab));
    1139             : }
    1140             : 
    1141             : static GEN
    1142        5435 : intnum_i(void *E, GEN (*eval)(void*, GEN), GEN a, GEN b, GEN tab, long prec)
    1143             : {
    1144        5435 :   GEN S = gen_0, kma, kmb;
    1145        5435 :   long sb, sgns = 1, codea = transcode(a, "a"), codeb = transcode(b, "b");
    1146             : 
    1147        5435 :   if (codea == f_REG && typ(a) == t_VEC) a = gel(a,1);
    1148        5435 :   if (codeb == f_REG && typ(b) == t_VEC) b = gel(b,1);
    1149        5435 :   if (codea == f_REG && codeb == f_REG) return intn(E, eval, a, b, tab);
    1150        1393 :   if (codea == f_SER || codeb == f_SER) return intlin(E, eval, a, b, tab, prec);
    1151        1323 :   if (labs(codea) > labs(codeb)) { swap(a,b); lswap(codea,codeb); sgns = -1; }
    1152             :   /* now labs(codea) <= labs(codeb) */
    1153        1323 :   if (codeb == f_SING)
    1154             :   {
    1155         266 :     if (codea == f_REG)
    1156         189 :       S = intnsing(E, eval, b, a, tab), sgns = -sgns;
    1157             :     else
    1158             :     {
    1159          77 :       GEN c = gmul2n(gadd(gel(a,1), gel(b,1)), -1);
    1160          77 :       S = gsub(intnsing(E, eval, a, c, gel(tab,1)),
    1161          77 :                intnsing(E, eval, b, c, gel(tab,2)));
    1162             :     }
    1163         266 :     return (sgns < 0) ? gneg(S) : S;
    1164             :   }
    1165             :   /* now b is infinite */
    1166        1057 :   sb = codeb > 0 ? 1 : -1;
    1167        1057 :   codeb = labs(codeb);
    1168        1057 :   if (codea == f_REG && codeb != f_YOSCC
    1169         336 :       && (codeb != f_YOSCS || gequal0(a)))
    1170             :   {
    1171         336 :     S = intninfpm(E, eval, a, sb*codeb, tab);
    1172         336 :     return sgns*sb < 0 ? gneg(S) : S;
    1173             :   }
    1174         721 :   if (is_fin_f(codea))
    1175             :   { /* either codea == f_SING  or codea == f_REG and codeb = f_YOSCC
    1176             :      * or (codeb == f_YOSCS and !gequal0(a)) */
    1177          21 :     GEN S2, c = real_i(codea == f_SING? gel(a,1): a);
    1178          21 :     switch(codeb)
    1179             :     {
    1180           7 :       case f_YOSCC: case f_YOSCS:
    1181             :       {
    1182           7 :         GEN pi2p = gmul(Pi2n(1,prec), f_getycplx(b, prec));
    1183           7 :         GEN pis2p = gmul2n(pi2p, -2);
    1184           7 :         if (codeb == f_YOSCC) c = gadd(c, pis2p);
    1185           7 :         c = gdiv(c, pi2p);
    1186           7 :         c = sb > 0? addiu(gceil(c), 1): subiu(gfloor(c), 1);
    1187           7 :         c = gmul(pi2p, c);
    1188           7 :         if (codeb == f_YOSCC) c = gsub(c, pis2p);
    1189           7 :         break;
    1190             :       }
    1191          14 :       default:
    1192          14 :         c = sb > 0? addiu(gceil(c), 1): subiu(gfloor(c), 1);
    1193          14 :         break;
    1194             :     }
    1195          14 :     S = codea==f_SING? intnsing(E, eval, a, c, gel(tab,1))
    1196          21 :                      : intn    (E, eval, a, c, gel(tab,1));
    1197          21 :     S2 = intninfpm(E, eval, c, sb*codeb, gel(tab,2));
    1198          21 :     if (sb < 0) S2 = gneg(S2);
    1199          21 :     S = gadd(S, S2);
    1200          21 :     return sgns < 0 ? gneg(S) : S;
    1201             :   }
    1202             :   /* now a and b are infinite */
    1203         700 :   if (codea * sb > 0)
    1204             :   {
    1205          14 :     if (codea > 0) pari_warn(warner, "integral from oo to oo");
    1206          14 :     if (codea < 0) pari_warn(warner, "integral from -oo to -oo");
    1207          14 :     return gen_0;
    1208             :   }
    1209         686 :   if (sb < 0) sgns = -sgns;
    1210         686 :   codea = labs(codea);
    1211         686 :   kma = f_getycplx(a, prec);
    1212         686 :   kmb = f_getycplx(b, prec);
    1213         686 :   if ((codea == f_YSLOW && codeb == f_YSLOW)
    1214         679 :    || (codea == f_YFAST && codeb == f_YFAST && gequal(kma, kmb)))
    1215         588 :     S = intninfinf(E, eval, tab);
    1216             :   else
    1217             :   {
    1218          98 :     GEN pis2 = Pi2n(-1, prec);
    1219          98 :     GEN ca = (codea == f_YOSCC)? gmul(pis2, kma): gen_0;
    1220          98 :     GEN cb = (codeb == f_YOSCC)? gmul(pis2, kmb): gen_0;
    1221          98 :     GEN c = codea == f_YOSCC ? ca : cb; /*signe(a)=-sb*/
    1222          98 :     GEN SP, SN = intninfpm(E, eval, c, -sb*codea, gel(tab,1));
    1223          98 :     if (codea != f_YOSCC)
    1224          84 :       SP = intninfpm(E, eval, cb, sb*codeb, gel(tab,2));
    1225             :     /* codea = codeb = f_YOSCC */
    1226          14 :     else if (gequal(kma, kmb))
    1227           0 :       SP = intninfpm(E, eval, cb, sb*codeb, gel(tab,2));
    1228             :     else
    1229             :     {
    1230          14 :       tab = gel(tab,2);
    1231          14 :       SP = intninfpm(E, eval, cb, sb*codeb, gel(tab,2));
    1232          14 :       SP = gadd(SP, intn(E, eval, ca, cb, gel(tab,1)));
    1233             :     }
    1234          98 :     S = gadd(SN, SP);
    1235             :   }
    1236         686 :   if (sgns < 0) S = gneg(S);
    1237         686 :   return S;
    1238             : }
    1239             : 
    1240             : GEN
    1241        5463 : intnum(void *E, GEN (*eval)(void*, GEN), GEN a, GEN b, GEN tab, long prec)
    1242             : {
    1243        5463 :   pari_sp ltop = avma;
    1244        5463 :   long l = prec+EXTRAPREC64;
    1245        5463 :   GEN na = NULL, nb = NULL, S;
    1246             : 
    1247        5463 :   if (transcode(a,"a") == f_SINGSER) {
    1248          21 :     long v = gvar(gel(a,1));
    1249          21 :     if (v != NO_VARIABLE) {
    1250          21 :       na = cgetg(3,t_VEC);
    1251          21 :       gel(na,1) = polcoef(gel(a,1),0,v);
    1252          21 :       gel(na,2) = gel(a,2);
    1253             :     }
    1254          21 :     a = gel(a,1);
    1255             :   }
    1256        5463 :   if (transcode(b,"b") == f_SINGSER) {
    1257          14 :     long v = gvar(gel(b,1));
    1258          14 :     if (v != NO_VARIABLE) {
    1259          14 :       nb = cgetg(3,t_VEC);
    1260          14 :       gel(nb,1) = polcoef(gel(b,1),0,v);
    1261          14 :       gel(nb,2) = gel(b,2);
    1262             :     }
    1263          14 :     b = gel(b,1);
    1264             :   }
    1265        5463 :   if (na || nb) {
    1266          28 :     if (tab && typ(tab) != t_INT)
    1267           7 :       pari_err_IMPL("non integer tab argument");
    1268          21 :     S = intnum(E, eval, na ? na : a, nb ? nb : b, tab, prec);
    1269          21 :     if (na) S = gsub(S, intnum(E, eval, na, a, tab, prec));
    1270          21 :     if (nb) S = gsub(S, intnum(E, eval, b, nb, tab, prec));
    1271          21 :     return gerepilecopy(ltop, S);
    1272             :   }
    1273        5435 :   tab = intnuminit0(a, b, tab, prec);
    1274        5435 :   S = intnum_i(E, eval, gprec_wensure(a, l), gprec_wensure(b, l), tab, prec);
    1275        5435 :   return gerepilecopy(ltop, gprec_wtrunc(S, prec));
    1276             : }
    1277             : 
    1278             : typedef struct auxint_s {
    1279             :   GEN a, R, mult;
    1280             :   GEN (*f)(void*, GEN);
    1281             :   GEN (*w)(GEN, long);
    1282             :   long prec;
    1283             :   void *E;
    1284             : } auxint_t;
    1285             : 
    1286             : static GEN
    1287        5222 : auxcirc(void *E, GEN t)
    1288             : {
    1289        5222 :   auxint_t *D = (auxint_t*) E;
    1290             :   GEN s, c, z;
    1291        5222 :   mpsincos(mulrr(t, D->mult), &s, &c); z = mkcomplex(c,s);
    1292        5222 :   return gmul(z, D->f(D->E, gadd(D->a, gmul(D->R, z))));
    1293             : }
    1294             : 
    1295             : GEN
    1296          14 : intcirc(void *E, GEN (*eval)(void*, GEN), GEN a, GEN R, GEN tab, long prec)
    1297             : {
    1298             :   auxint_t D;
    1299             :   GEN z;
    1300             : 
    1301          14 :   D.a = a;
    1302          14 :   D.R = R;
    1303          14 :   D.mult = mppi(prec);
    1304          14 :   D.f = eval;
    1305          14 :   D.E = E;
    1306          14 :   z = intnum(&D, &auxcirc, real_m1(prec), real_1(prec), tab, prec);
    1307          14 :   return gmul2n(gmul(R, z), -1);
    1308             : }
    1309             : 
    1310             : static GEN
    1311       36750 : auxlin(void *E, GEN t)
    1312             : {
    1313       36750 :   auxint_t *D = (auxint_t*) E;
    1314       36750 :   return D->f(D->E, gadd(D->a, gmul(D->mult, t)));
    1315             : }
    1316             : 
    1317             : static GEN
    1318          70 : intlin(void *E, GEN (*eval)(void*, GEN), GEN a, GEN b, GEN tab, long prec)
    1319             : {
    1320             :   auxint_t D;
    1321             :   GEN z;
    1322             : 
    1323          70 :   if (typ(a) == t_VEC) a = gel(a,1);
    1324          70 :   if (typ(b) == t_VEC) b = gel(b,1);
    1325          70 :   z = toser_i(a); if (z) a = z;
    1326          70 :   z = toser_i(b); if (z) b = z;
    1327          70 :   D.a = a;
    1328          70 :   D.mult = gsub(b,a);
    1329          70 :   D.f = eval;
    1330          70 :   D.E = E;
    1331          70 :   z = intnum(&D, &auxlin, real_0(prec), real_1(prec), tab, prec);
    1332          70 :   return gmul(D.mult, z);
    1333             : }
    1334             : 
    1335             : GEN
    1336        4641 : intnum0(GEN a, GEN b, GEN code, GEN tab, long prec)
    1337        4641 : { EXPR_WRAP(code, intnum(EXPR_ARG, a, b, tab, prec)); }
    1338             : GEN
    1339          14 : intcirc0(GEN a, GEN R, GEN code, GEN tab, long prec)
    1340          14 : { EXPR_WRAP(code, intcirc(EXPR_ARG, a, R, tab, prec)); }
    1341             : GEN
    1342         147 : intfuncinit0(GEN a, GEN b, GEN code, long m, long prec)
    1343         147 : { EXPR_WRAP(code, intfuncinit(EXPR_ARG, a, b, m, prec)); }
    1344             : 
    1345             : #if 0
    1346             : /* Two variable integration */
    1347             : 
    1348             : typedef struct auxf_s {
    1349             :   GEN x;
    1350             :   GEN (*f)(void *, GEN, GEN);
    1351             :   void *E;
    1352             : } auxf_t;
    1353             : 
    1354             : typedef struct indi_s {
    1355             :   GEN (*c)(void*, GEN);
    1356             :   GEN (*d)(void*, GEN);
    1357             :   GEN (*f)(void *, GEN, GEN);
    1358             :   void *Ec;
    1359             :   void *Ed;
    1360             :   void *Ef;
    1361             :   GEN tabintern;
    1362             :   long prec;
    1363             : } indi_t;
    1364             : 
    1365             : static GEN
    1366             : auxf(GEN y, void *E)
    1367             : {
    1368             :   auxf_t *D = (auxf_t*) E;
    1369             :   return D->f(D->E, D->x, y);
    1370             : }
    1371             : 
    1372             : static GEN
    1373             : intnumdoubintern(GEN x, void *E)
    1374             : {
    1375             :   indi_t *D = (indi_t*) E;
    1376             :   GEN c = D->c(x, D->Ec), d = D->d(x, D->Ed);
    1377             :   auxf_t A;
    1378             : 
    1379             :   A.x = x;
    1380             :   A.f = D->f;
    1381             :   A.E = D->Ef;
    1382             :   return intnum(&A, &auxf, c, d, D->tabintern, D->prec);
    1383             : }
    1384             : 
    1385             : GEN
    1386             : intnumdoub(void *Ef, GEN (*evalf)(void *, GEN, GEN), void *Ec, GEN (*evalc)(void*, GEN), void *Ed, GEN (*evald)(void*, GEN), GEN a, GEN b, GEN tabext, GEN tabint, long prec)
    1387             : {
    1388             :   indi_t E;
    1389             : 
    1390             :   E.c = evalc;
    1391             :   E.d = evald;
    1392             :   E.f = evalf;
    1393             :   E.Ec = Ec;
    1394             :   E.Ed = Ed;
    1395             :   E.Ef = Ef;
    1396             :   E.prec = prec;
    1397             :   if (typ(tabint) == t_INT)
    1398             :   {
    1399             :     GEN C = evalc(a, Ec), D = evald(a, Ed);
    1400             :     if (typ(C) != t_VEC && typ(D) != t_VEC) { C = gen_0; D = gen_1; }
    1401             :     E.tabintern = intnuminit0(C, D, tabint, prec);
    1402             :   }
    1403             :   else E.tabintern = tabint;
    1404             :   return intnum(&E, &intnumdoubintern, a, b, tabext, prec);
    1405             : }
    1406             : 
    1407             : GEN
    1408             : intnumdoub0(GEN a, GEN b, int nc, int nd, int nf, GEN tabext, GEN tabint, long prec)
    1409             : {
    1410             :   GEN z;
    1411             :   push_lex(NULL);
    1412             :   push_lex(NULL);
    1413             :   z = intnumdoub(chf, &gp_eval2, chc, &gp_eval, chd, &gp_eval, a, b, tabext, tabint, prec);
    1414             :   pop_lex(1); pop_lex(1); return z;
    1415             : }
    1416             : #endif
    1417             : 
    1418             : /* The quotient-difference algorithm. Given a vector M, convert the series
    1419             :  * S = \sum_{n >= 0} M[n+1]z^n into a continued fraction.
    1420             :  * Compute the c[n] such that
    1421             :  * S = c[1] / (1 + c[2]z / (1+c[3]z/(1+...c[lim]z))),
    1422             :  * Compute A[n] and B[n] such that
    1423             :  * S = M[1]/ (1+A[1]*z+B[1]*z^2 / (1+A[2]*z+B[2]*z^2/ (1+...1/(1+A[lim\2]*z)))),
    1424             :  * Assume lim <= #M.
    1425             :  * Does not work for certain M. */
    1426             : 
    1427             : /* Given a continued fraction CF output by the quodif program,
    1428             : convert it into an Euler continued fraction A(n), B(n), where
    1429             : $1/(1+c[2]z/(1+c[3]z/(1+..c[lim]z)))
    1430             : =1/(1+A[1]*z+B[1]*z^2/(1+A[2]*z+B[2]*z^2/(1+...1/(1+A[lim\2]*z)))). */
    1431             : static GEN
    1432        4529 : contfrac_Euler(GEN CF)
    1433             : {
    1434        4529 :   long lima, limb, i, lim = lg(CF)-1;
    1435             :   GEN A, B;
    1436        4529 :   lima = lim/2;
    1437        4529 :   limb = (lim - 1)/2;
    1438        4529 :   A = cgetg(lima+1, t_VEC);
    1439        4529 :   B = cgetg(limb+1, t_VEC);
    1440        4529 :   gel (A, 1) = gel(CF, 2);
    1441      159628 :   for (i=2; i <= lima; ++i) gel(A,i) = gadd(gel(CF, 2*i), gel(CF, 2*i-1));
    1442      162743 :   for (i=1; i <= limb; ++i) gel(B,i) = gneg(gmul(gel(CF, 2*i+1), gel(CF, 2*i)));
    1443        4529 :   return mkvec2(A, B);
    1444             : }
    1445             : 
    1446             : static GEN
    1447        4816 : contfracinit_i(GEN M, long lim)
    1448             : {
    1449             :   pari_sp av;
    1450             :   GEN e, q, c;
    1451             :   long lim2, j, k;
    1452        4816 :   e = zerovec(lim);
    1453        4816 :   c = zerovec(lim+1); gel(c, 1) = gel(M, 1);
    1454        4816 :   q = cgetg(lim+1, t_VEC);
    1455      339528 :   for (k = 1; k <= lim; ++k) gel(q, k) = gdiv(gel(M, k+1), gel(M, k));
    1456        4816 :   lim2 = lim/2; av = avma;
    1457      171325 :   for (j = 1; j <= lim2; ++j)
    1458             :   {
    1459      166509 :     long l = lim - 2*j;
    1460      166509 :     gel(c, 2*j) = gneg(gel(q, 1));
    1461    13759361 :     for (k = 0; k <= l; ++k)
    1462    13592852 :       gel(e, k+1) = gsub(gadd(gel(e, k+2), gel(q, k+2)), gel(q, k+1));
    1463    13592852 :     for (k = 0; k < l; ++k)
    1464    13426343 :       gel(q, k+1) = gdiv(gmul(gel(q, k+2), gel(e, k+2)), gel(e, k+1));
    1465      166509 :     gel(c, 2*j+1) = gneg(gel(e, 1));
    1466      166509 :     if (gc_needed(av, 3))
    1467             :     {
    1468         111 :       if (DEBUGMEM>1) pari_warn(warnmem,"contfracinit, %ld/%ld",j,lim2);
    1469         111 :       gerepileall(av, 3, &e, &c, &q);
    1470             :     }
    1471             :   }
    1472        4816 :   if (odd(lim)) gel(c, lim+1) = gneg(gel(q, 1));
    1473        4816 :   return c;
    1474             : }
    1475             : 
    1476             : GEN
    1477        4550 : contfracinit(GEN M, long lim)
    1478             : {
    1479        4550 :   pari_sp ltop = avma;
    1480             :   GEN c;
    1481        4550 :   switch(typ(M))
    1482             :   {
    1483           7 :     case t_RFRAC:
    1484           7 :       if (lim < 0) pari_err_TYPE("contfracinit",M);
    1485           7 :       M = gtoser(M, varn(gel(M,2)), lim+3); /*fall through*/
    1486          35 :     case t_SER: M = gtovec(M); break;
    1487           7 :     case t_POL: M = RgX_to_RgC(M, degpol(M)+1); break;
    1488        4501 :     case t_VEC: case t_COL: break;
    1489           7 :     default: pari_err_TYPE("contfracinit", M);
    1490             :   }
    1491        4543 :   if (lim < 0)
    1492             :   {
    1493          28 :     lim = lg(M)-2;
    1494          28 :     if (lim < 0) retmkvec2(cgetg(1,t_VEC),cgetg(1,t_VEC));
    1495             :   }
    1496        4515 :   else if (lg(M)-1 <= lim)
    1497           0 :     pari_err_COMPONENT("contfracinit", "<", stoi(lg(M)-1), stoi(lim));
    1498        4529 :   c = contfracinit_i(M, lim);
    1499        4529 :   return gerepilecopy(ltop, contfrac_Euler(c));
    1500             : }
    1501             : 
    1502             : /* Evaluate at 1/tinv the nlim first terms of the continued fraction output by
    1503             :  * contfracinit. */
    1504             : /* Not stack clean */
    1505             : GEN
    1506     3081444 : contfraceval_inv(GEN CF, GEN tinv, long nlim)
    1507             : {
    1508             :   pari_sp btop;
    1509             :   long j;
    1510     3081444 :   GEN S = gen_0, S1, S2, A, B;
    1511     3081444 :   if (typ(CF) != t_VEC || lg(CF) != 3) pari_err_TYPE("contfraceval", CF);
    1512     3081446 :   A = gel(CF, 1); if (typ(A) != t_VEC) pari_err_TYPE("contfraceval", CF);
    1513     3081446 :   B = gel(CF, 2); if (typ(B) != t_VEC) pari_err_TYPE("contfraceval", CF);
    1514     3081438 :   if (nlim < 0)
    1515          14 :     nlim = lg(A)-1;
    1516     3081424 :   else if (lg(A) <= nlim)
    1517           7 :     pari_err_COMPONENT("contfraceval", ">", stoi(lg(A)-1), stoi(nlim));
    1518     3081406 :   if (lg(B)+1 <= nlim)
    1519           0 :     pari_err_COMPONENT("contfraceval", ">", stoi(lg(B)), stoi(nlim));
    1520     3081528 :   btop = avma;
    1521     3081528 :   if (nlim <= 1) return lg(A)==1? gen_0: gdiv(tinv, gadd(gel(A, 1), tinv));
    1522     2981046 :   switch(nlim % 3)
    1523             :   {
    1524     1071659 :     case 2:
    1525     1071659 :       S = gdiv(gel(B, nlim-1), gadd(gel(A, nlim), tinv));
    1526     1071697 :       nlim--; break;
    1527             : 
    1528     1018375 :     case 0:
    1529     1018375 :       S1 = gadd(gel(A, nlim), tinv);
    1530     1018178 :       S2 = gadd(gmul(gadd(gel(A, nlim-1), tinv), S1), gel(B, nlim-1));
    1531     1018082 :       S = gdiv(gmul(gel(B, nlim-2), S1), S2);
    1532     1018927 :       nlim -= 2; break;
    1533             :   }
    1534             :   /* nlim = 1 (mod 3) */
    1535    14026736 :   for (j = nlim; j >= 4; j -= 3)
    1536             :   {
    1537             :     GEN S3;
    1538    11047044 :     S1 = gadd(gadd(gel(A, j), tinv), S);
    1539    11013791 :     S2 = gadd(gmul(gadd(gel(A, j-1), tinv), S1), gel(B, j-1));
    1540    11009057 :     S3 = gadd(gmul(gadd(gel(A, j-2), tinv), S2), gmul(gel(B, j-2), S1));
    1541    11012138 :     S = gdiv(gmul(gel(B, j-3), S2), S3);
    1542    11045011 :     if (gc_needed(btop, 3)) S = gerepilecopy(btop, S);
    1543             :   }
    1544     2979692 :   return gdiv(tinv, gadd(gadd(gel(A, 1), tinv), S));
    1545             : }
    1546             : 
    1547             : GEN
    1548          35 : contfraceval(GEN CF, GEN t, long nlim)
    1549             : {
    1550          35 :   pari_sp ltop = avma;
    1551          35 :   return gerepileupto(ltop, contfraceval_inv(CF, ginv(t), nlim));
    1552             : }
    1553             : 
    1554             : /* MONIEN SUMMATION */
    1555             : 
    1556             : /* basic Newton, find x ~ z such that Q(x) = 0 */
    1557             : static GEN
    1558        2436 : monrefine(GEN Q, GEN QP, GEN z, long prec)
    1559             : {
    1560        2436 :   pari_sp av = avma;
    1561        2436 :   GEN pr = poleval(Q, z);
    1562             :   for(;;)
    1563        9061 :   {
    1564             :     GEN prnew;
    1565       11497 :     z = gsub(z, gdiv(pr, poleval(QP, z)));
    1566       11497 :     prnew = poleval(Q, z);
    1567       11497 :     if (gcmp(gabs(prnew, prec), gabs(pr, prec)) >= 0) break;
    1568        9061 :     pr = prnew;
    1569             :   }
    1570        2436 :   z = gprec_wensure(z, 2*prec-2);
    1571        2436 :   z = gsub(z, gdiv(poleval(Q, z), poleval(QP, z)));
    1572        2436 :   return gerepileupto(av, z);
    1573             : }
    1574             : 
    1575             : static GEN
    1576         287 : RX_realroots(GEN x, long prec)
    1577         287 : { return realroots(gprec_wtrunc(x,prec), NULL, prec); }
    1578             : 
    1579             : /* (real) roots of Q, assuming QP = Q' and that half the roots are close to
    1580             :  * k+1, ..., k+m, m = deg(Q)/2-1. N.B. All roots are real and >= 1 */
    1581             : static GEN
    1582         182 : monroots(GEN Q, GEN QP, long k, long prec)
    1583             : {
    1584         182 :   long j, n = degpol(Q), m = n/2 - 1;
    1585         182 :   GEN v2, v1 = cgetg(m+1, t_VEC);
    1586        2618 :   for (j = 1; j <= m; ++j) gel(v1, j) = monrefine(Q, QP, stoi(k+j), prec);
    1587         182 :   Q = gdivent(Q, roots_to_pol(v1, varn(Q)));
    1588         182 :   v2 = RX_realroots(Q, prec); settyp(v2, t_VEC);
    1589         182 :   return shallowconcat(v1, v2);
    1590             : }
    1591             : 
    1592             : static void
    1593         287 : Pade(GEN M, GEN *pP, GEN *pQ)
    1594             : {
    1595         287 :   pari_sp av = avma;
    1596         287 :   long n = lg(M)-2, i;
    1597         287 :   GEN v = contfracinit_i(M, n), P = pol_0(0), Q = pol_1(0);
    1598             :   /* evaluate continued fraction => Pade approximants */
    1599       16877 :   for (i = n-1; i >= 1; i--)
    1600             :   { /* S = P/Q: S -> v[i]*x / (1+S) */
    1601       16590 :     GEN R = RgX_shift_shallow(RgX_Rg_mul(Q,gel(v,i)), 1);
    1602       16590 :     Q = RgX_add(P,Q); P = R;
    1603       16590 :     if (gc_needed(av, 3))
    1604             :     {
    1605           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Pade, %ld/%ld",i,n-1);
    1606           0 :       gerepileall(av, 3, &P, &Q, &v);
    1607             :     }
    1608             :   }
    1609             :   /* S -> 1+S */
    1610         287 :   *pP = RgX_add(P,Q);
    1611         287 :   *pQ = Q;
    1612         287 : }
    1613             : 
    1614             : static GEN
    1615           7 : veczetaprime(GEN a, GEN b, long N, long prec)
    1616             : {
    1617           7 :   long newprec, fpr = prec2nbits(prec), pr = (long)ceil(fpr * 1.5);
    1618           7 :   long l = nbits2prec(pr), e = fpr / 2;
    1619             :   GEN eps, A, B;
    1620           7 :   newprec = nbits2prec(pr + BITS_IN_LONG);
    1621           7 :   a = gprec_wensure(a, newprec);
    1622           7 :   b = gprec_wensure(b, newprec);
    1623           7 :   eps = real2n(-e, l);
    1624           7 :   A = veczeta(a, gsub(b, eps), N, newprec);
    1625           7 :   B = veczeta(a, gadd(b, eps), N, newprec);
    1626           7 :   return gmul2n(RgV_sub(B, A), e-1);
    1627             : }
    1628             : 
    1629             : struct mon_w {
    1630             :   GEN w, a, b;
    1631             :   long n, j, prec;
    1632             : };
    1633             : 
    1634             : /* w(x) / x^(a*(j+k)+b), k >= 1; w a t_CLOSURE or t_INT [encodes log(x)^w] */
    1635             : static GEN
    1636       34384 : wrapmonw(void* E, GEN x)
    1637             : {
    1638       34384 :   struct mon_w *W = (struct mon_w*)E;
    1639       34384 :   long k, j = W->j, n = W->n, prec = W->prec, l = 2*n+4-j;
    1640       34384 :   GEN wx = typ(W->w) == t_CLOSURE? closure_callgen1prec(W->w, x, prec)
    1641       34384 :                                  : powgi(glog(x, prec), W->w);
    1642       34384 :   GEN v = cgetg(l, t_VEC);
    1643       34384 :   GEN xa = gpow(x, gneg(W->a), prec), w = gmul(wx, gpowgs(xa, j));
    1644       34384 :   w = gdiv(w, gpow(x,W->b,prec));
    1645     2098129 :   for (k = 1; k < l; k++) { gel(v,k) = w; w = gmul(w, xa); }
    1646       34384 :   return v;
    1647             : }
    1648             : /* w(x) / x^(a*j+b) */
    1649             : static GEN
    1650       18819 : wrapmonw2(void* E, GEN x)
    1651             : {
    1652       18819 :   struct mon_w *W = (struct mon_w*)E;
    1653       18819 :   GEN wnx = closure_callgen1prec(W->w, x, W->prec);
    1654       18819 :   return gdiv(wnx, gpow(x, gadd(gmulgs(W->a, W->j), W->b), W->prec));
    1655             : }
    1656             : static GEN
    1657          35 : M_from_wrapmon(struct mon_w *S, GEN wfast, GEN n0)
    1658             : {
    1659          35 :   long j, N = 2*S->n+2;
    1660          35 :   GEN M = cgetg(N+1, t_VEC), faj = gsub(wfast, S->b);
    1661          42 :   for (j = 1; j <= N; j++)
    1662             :   {
    1663          42 :     faj = gsub(faj, S->a);
    1664          42 :     if (gcmpgs(faj, -2) <= 0)
    1665             :     {
    1666          28 :       S->j = j; setlg(M,j);
    1667          28 :       M = shallowconcat(M, sumnum((void*)S, wrapmonw, n0, NULL, S->prec));
    1668          28 :       break;
    1669             :     }
    1670          14 :     S->j = j;
    1671          14 :     gel(M,j) = sumnum((void*)S, wrapmonw2, mkvec2(n0,faj), NULL, S->prec);
    1672             :   }
    1673          28 :   return M;
    1674             : }
    1675             : 
    1676             : static void
    1677         231 : checkmonroots(GEN vr, long n)
    1678             : {
    1679         231 :   if (lg(vr) != n+1)
    1680           0 :     pari_err_IMPL("recovery when missing roots in sumnummonieninit");
    1681         231 : }
    1682             : 
    1683             : static GEN
    1684         238 : sumnummonieninit_i(GEN a, GEN b, GEN w, GEN n0, long prec)
    1685             : {
    1686         238 :   GEN c, M, P, Q, Qp, vr, vabs, vwt, ga = gadd(a, b);
    1687         238 :   double bit = 2*prec2nbits(prec) / gtodouble(ga), D = bit*M_LN2;
    1688         238 :   double da = maxdd(1., gtodouble(a));
    1689         238 :   long n = (long)ceil(D / (da*(log(D)-1)));
    1690         238 :   long j, prec2, prec0 = prec + EXTRAPREC64;
    1691         238 :   double bit0 = ceil((2*n+1)*LOG2_10);
    1692         238 :   int neg = 1;
    1693             :   struct mon_w S;
    1694             : 
    1695             :   /* 2.05 is heuristic; with 2.03, sumnummonien(n=1,1/n^2) loses
    1696             :    * 19 decimals at \p1500 */
    1697         238 :   prec = nbits2prec(maxdd(2.05*da*bit, bit0));
    1698         238 :   prec2 = nbits2prec(maxdd(1.3*da*bit, bit0));
    1699         238 :   S.w = w;
    1700         238 :   S.a = a = gprec_wensure(a, 2*prec-2);
    1701         238 :   S.b = b = gprec_wensure(b, 2*prec-2);
    1702         238 :   S.n = n;
    1703         238 :   S.j = 1;
    1704         238 :   S.prec = prec;
    1705         238 :   if (typ(w) == t_INT)
    1706             :   { /* f(n) ~ \sum_{i > 0} f_i log(n)^k / n^(a*i + b); a > 0, a+b > 1 */
    1707         203 :     long k = itos(w);
    1708         203 :     if (k == 0)
    1709         196 :       M = veczeta(a, ga, 2*n+2, prec);
    1710           7 :     else if (k == 1)
    1711           7 :       M = veczetaprime(a, ga, 2*n+2, prec);
    1712             :     else
    1713           0 :       M = M_from_wrapmon(&S, gen_0, gen_1);
    1714         203 :     if (odd(k)) neg = 0;
    1715             :   }
    1716             :   else
    1717             :   {
    1718          35 :     GEN wfast = gen_0;
    1719          35 :     if (typ(w) == t_VEC) { wfast = gel(w,2); w = gel(w,1); }
    1720          35 :     M = M_from_wrapmon(&S, wfast, n0);
    1721             :   }
    1722             :   /* M[j] = sum(n >= n0, w(n) / n^(a*j+b) */
    1723         231 :   Pade(M, &P,&Q);
    1724         231 :   Qp = RgX_deriv(Q);
    1725         231 :   if (gequal1(a)) a = NULL;
    1726         231 :   if (!a && typ(w) == t_INT)
    1727             :   {
    1728         182 :     vabs = vr = monroots(Q, Qp, signe(w)? 1: 0, prec2);
    1729         182 :     checkmonroots(vr, n);
    1730         182 :     c = b;
    1731             :   }
    1732             :   else
    1733             :   {
    1734          49 :     vr = RX_realroots(Q, prec2); settyp(vr, t_VEC);
    1735          49 :     checkmonroots(vr, n);
    1736          49 :     if (!a) { vabs = vr; c = b; }
    1737             :     else
    1738             :     {
    1739          35 :       GEN ai = ginv(a);
    1740          35 :       vabs = cgetg(n+1, t_VEC);
    1741        1127 :       for (j = 1; j <= n; j++) gel(vabs,j) = gpow(gel(vr,j), ai, prec2);
    1742          35 :       c = gdiv(b,a);
    1743             :     }
    1744             :   }
    1745             : 
    1746         231 :   vwt = cgetg(n+1, t_VEC);
    1747         231 :   c = gsubgs(c,1); if (gequal0(c)) c = NULL;
    1748        6972 :   for (j = 1; j <= n; j++)
    1749             :   {
    1750        6741 :     GEN r = gel(vr,j), t = gdiv(poleval(P,r), poleval(Qp,r));
    1751        6741 :     if (c) t = gmul(t, gpow(r, c, prec));
    1752        6741 :     gel(vwt,j) = neg? gneg(t): t;
    1753             :   }
    1754         231 :   if (typ(w) == t_INT && !equali1(n0))
    1755             :   {
    1756          84 :     GEN h = subiu(n0,1);
    1757        2569 :     for (j = 1; j <= n; j++) gel(vabs,j) = gadd(gel(vabs,j), h);
    1758             :   }
    1759         231 :   return mkvec3(gprec_wtrunc(vabs,prec0), gprec_wtrunc(vwt,prec0), n0);
    1760             : }
    1761             : 
    1762             : GEN
    1763         168 : sumnummonieninit(GEN asymp, GEN w, GEN n0, long prec)
    1764             : {
    1765         168 :   pari_sp av = avma;
    1766         168 :   const char *fun = "sumnummonieninit";
    1767             :   GEN a, b;
    1768         168 :   if (!n0) n0 = gen_1; else if (typ(n0) != t_INT) pari_err_TYPE(fun, n0);
    1769         168 :   if (!asymp) a = b = gen_1;
    1770             :   else
    1771             :   {
    1772         140 :     if (typ(asymp) == t_VEC)
    1773             :     {
    1774          70 :       if (lg(asymp) != 3) pari_err_TYPE(fun, asymp);
    1775          70 :       a = gel(asymp,1);
    1776          70 :       b = gel(asymp,2);
    1777             :     }
    1778             :     else
    1779             :     {
    1780          70 :       a = gen_1;
    1781          70 :       b = asymp;
    1782             :     }
    1783         140 :     if (gsigne(a) <= 0) pari_err_DOMAIN(fun, "a", "<=", gen_0, a);
    1784         133 :     if (!isinR(b)) pari_err_TYPE(fun, b);
    1785         126 :     if (gcmpgs(gadd(a,b), 1) <= 0)
    1786           7 :       pari_err_DOMAIN(fun, "a+b", "<=", gen_1, mkvec2(a,b));
    1787             :   }
    1788         147 :   if (!w) w = gen_0;
    1789          42 :   else switch(typ(w))
    1790             :   {
    1791           7 :     case t_INT:
    1792           7 :       if (signe(w) < 0) pari_err_IMPL("log power < 0 in sumnummonieninit");
    1793          35 :     case t_CLOSURE: break;
    1794           7 :     case t_VEC:
    1795           7 :       if (lg(w) == 3 && typ(gel(w,1)) == t_CLOSURE) break;
    1796           0 :     default: pari_err_TYPE(fun, w);
    1797             :   }
    1798         147 :   return gerepilecopy(av, sumnummonieninit_i(a, b, w, n0, prec));
    1799             : }
    1800             : 
    1801             : GEN
    1802         238 : sumnummonien(void *E, GEN (*eval)(void*,GEN), GEN n0, GEN tab, long prec)
    1803             : {
    1804         238 :   pari_sp av = avma;
    1805             :   GEN vabs, vwt, S;
    1806             :   long l, i;
    1807         238 :   if (typ(n0) != t_INT) pari_err_TYPE("sumnummonien", n0);
    1808         238 :   if (!tab)
    1809          91 :     tab = sumnummonieninit_i(gen_1, gen_1, gen_0, n0, prec);
    1810             :   else
    1811             :   {
    1812         147 :     if (lg(tab) != 4 || typ(tab) != t_VEC) pari_err_TYPE("sumnummonien", tab);
    1813         147 :     if (!equalii(n0, gel(tab,3)))
    1814           7 :       pari_err(e_MISC, "incompatible initial value %Ps != %Ps", gel(tab,3),n0);
    1815             :   }
    1816         231 :   vabs= gel(tab,1); l = lg(vabs);
    1817         231 :   vwt = gel(tab,2);
    1818         231 :   if (typ(vabs) != t_VEC || typ(vwt) != t_VEC || lg(vwt) != l)
    1819           0 :     pari_err_TYPE("sumnummonien", tab);
    1820         231 :   S = gen_0;
    1821        6972 :   for (i = 1; i < l; i++)
    1822             :   {
    1823        6741 :     S = gadd(S, gmul(gel(vwt,i), eval(E, gel(vabs,i))));
    1824        6741 :     S = gprec_wensure(S, prec);
    1825             :   }
    1826         231 :   return gerepilecopy(av, gprec_wtrunc(S, prec));
    1827             : }
    1828             : 
    1829             : static GEN
    1830         210 : get_oo(GEN fast) { return mkvec2(mkoo(), fast); }
    1831             : 
    1832             : GEN
    1833         126 : sumnuminit(GEN fast, long prec)
    1834             : {
    1835             :   pari_sp av;
    1836         126 :   GEN s, v, d, C, res = cgetg(6, t_VEC);
    1837         126 :   long bitprec = prec2nbits(prec), N, k, k2, m;
    1838             :   double w;
    1839             : 
    1840         126 :   gel(res, 1) = d = mkfrac(gen_1, utoipos(4)); /* 1/4 */
    1841         126 :   av = avma;
    1842         126 :   w = gtodouble(glambertW(ginv(d), LOWDEFAULTPREC));
    1843         126 :   N = (long)ceil(M_LN2*bitprec/(w*(1+w))+5);
    1844         126 :   k = (long)ceil(N*w); if (k&1) k--;
    1845             : 
    1846         126 :   prec += EXTRAPREC64;
    1847         126 :   k2 = k/2;
    1848         126 :   s = RgX_to_ser(monomial(real_1(prec),1,0), k+3);
    1849         126 :   s = gmul2n(gasinh(s, prec), 2); /* asinh(x)/d, d = 1/4 */
    1850         126 :   gel(s, 2) = utoipos(4);
    1851         126 :   s = gsub(ser_inv(gexpm1(s,prec)), ser_inv(s));
    1852         126 :   C = matpascal(k-1);
    1853         126 :   v = cgetg(k2+1, t_VEC);
    1854        8617 :   for (m = 1; m <= k2; m++)
    1855             :   {
    1856        8491 :     pari_sp av = avma;
    1857        8491 :     GEN S = real_0(prec);
    1858             :     long j;
    1859      486262 :     for (j = m; j <= k2; j++)
    1860             :     { /* s[X^(2j-1)] * binomial(2*j-1, j-m) */
    1861      477771 :       GEN t = gmul(gel(s,2*j+1), gcoeff(C, 2*j,j-m+1));
    1862      477771 :       t = gmul2n(t, 1-2*j);
    1863      477771 :       S = odd(j)? gsub(S,t): gadd(S,t);
    1864             :     }
    1865        8491 :     if (odd(m)) S = gneg(S);
    1866        8491 :     gel(v,m) = gerepileupto(av, S);
    1867             :   }
    1868         126 :   v = RgC_gtofp(v,prec); settyp(v, t_VEC);
    1869         126 :   gel(res, 4) = gerepileupto(av, v);
    1870         126 :   gel(res, 2) = utoi(N);
    1871         126 :   gel(res, 3) = utoi(k);
    1872         126 :   if (!fast) fast = get_oo(gen_0);
    1873         126 :   gel(res, 5) = intnuminit(gel(res,2), fast, 0, prec - EXTRAPREC64);
    1874         126 :   return res;
    1875             : }
    1876             : 
    1877             : static int
    1878          28 : checksumtab(GEN T)
    1879             : {
    1880          28 :   if (typ(T) != t_VEC || lg(T) != 6) return 0;
    1881          21 :   return typ(gel(T,2))==t_INT && typ(gel(T,3))==t_INT && typ(gel(T,4))==t_VEC;
    1882             : }
    1883             : GEN
    1884         140 : sumnum(void *E, GEN (*eval)(void*, GEN), GEN a, GEN tab, long prec)
    1885             : {
    1886         140 :   pari_sp av = avma, av2;
    1887             :   GEN v, tabint, S, d, fast;
    1888             :   long as, N, k, m, prec2;
    1889         140 :   if (!a) { a = gen_1; fast = get_oo(gen_0); }
    1890         140 :   else switch(typ(a))
    1891             :   {
    1892          49 :   case t_VEC:
    1893          49 :     if (lg(a) != 3) pari_err_TYPE("sumnum", a);
    1894          49 :     fast = get_oo(gel(a,2));
    1895          49 :     a = gel(a,1); break;
    1896          91 :   default:
    1897          91 :     fast = get_oo(gen_0);
    1898             :   }
    1899         140 :   if (typ(a) != t_INT) pari_err_TYPE("sumnum", a);
    1900         140 :   if (!tab) tab = sumnuminit(fast, prec);
    1901          28 :   else if (!checksumtab(tab)) pari_err_TYPE("sumnum",tab);
    1902         133 :   as = itos(a);
    1903         133 :   d = gel(tab,1);
    1904         133 :   N = maxss(as, itos(gel(tab,2)));
    1905         133 :   k = itos(gel(tab,3));
    1906         133 :   v = gel(tab,4);
    1907         133 :   tabint = gel(tab,5);
    1908         133 :   prec2 = prec+EXTRAPREC64;
    1909         133 :   av2 = avma;
    1910         133 :   S = gmul(eval(E, stoi(N)), real2n(-1,prec2));
    1911       16038 :   for (m = as; m < N; m++)
    1912             :   {
    1913       15905 :     S = gadd(S, eval(E, stoi(m)));
    1914       15905 :     if (gc_needed(av, 2))
    1915             :     {
    1916           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"sumnum [1], %ld/%ld",m,N);
    1917           0 :       S = gerepileupto(av2, S);
    1918             :     }
    1919       15905 :     S = gprec_wensure(S, prec2);
    1920             :   }
    1921        9779 :   for (m = 1; m <= k/2; m++)
    1922             :   {
    1923        9646 :     GEN t = gmulsg(2*m-1, d);
    1924        9646 :     GEN s = gsub(eval(E, gsubsg(N,t)), eval(E, gaddsg(N,t)));
    1925        9646 :     S = gadd(S, gmul(gel(v,m), s));
    1926        9646 :     if (gc_needed(av2, 2))
    1927             :     {
    1928           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"sumnum [2], %ld/%ld",m,k/2);
    1929           0 :       S = gerepileupto(av2, S);
    1930             :     }
    1931        9646 :     S = gprec_wensure(S, prec2);
    1932             :   }
    1933         133 :   S = gadd(S, intnum(E, eval,stoi(N), fast, tabint, prec2));
    1934         133 :   return gerepilecopy(av, gprec_wtrunc(S, prec));
    1935             : }
    1936             : 
    1937             : GEN
    1938         182 : sumnummonien0(GEN a, GEN code, GEN tab, long prec)
    1939         182 : { EXPR_WRAP(code, sumnummonien(EXPR_ARG, a, tab, prec)); }
    1940             : GEN
    1941          98 : sumnum0(GEN a, GEN code, GEN tab, long prec)
    1942          98 : { EXPR_WRAP(code, sumnum(EXPR_ARG, a, tab, prec)); }
    1943             : 
    1944             : /* Abel-Plana summation */
    1945             : 
    1946             : static GEN
    1947          56 : intnumgauexpinit(long prec)
    1948             : {
    1949          56 :   pari_sp ltop = avma;
    1950             :   GEN V, N, E, P, Q, R, vabs, vwt;
    1951          56 :   long l, n, k, j, prec2, prec0 = prec + EXTRAPREC64, bit = prec2nbits(prec);
    1952             : 
    1953          56 :   n = (long)ceil(bit*0.226);
    1954          56 :   n |= 1; /* make n odd */
    1955          56 :   prec = nbits2prec(1.5*bit + 32);
    1956          56 :   prec2 = maxss(prec0, nbits2prec(1.15*bit + 32));
    1957          56 :   constbern(n+3);
    1958          56 :   V = cgetg(n + 4, t_VEC);
    1959        3276 :   for (k = 1; k <= n + 3; ++k)
    1960        3220 :     gel(V, k) = gtofp(gdivgs(bernfrac(2*k), odd(k)? 2*k: -2*k), prec);
    1961          56 :   Pade(V, &P, &Q);
    1962          56 :   N = RgX_recip(gsub(P, Q));
    1963          56 :   E = RgX_recip(Q);
    1964          56 :   R = gdivgs(gdiv(N, RgX_deriv(E)), 2);
    1965          56 :   vabs = RX_realroots(E,prec2);
    1966          56 :   l = lg(vabs); settyp(vabs, t_VEC);
    1967          56 :   vwt = cgetg(l, t_VEC);
    1968        1610 :   for (j = 1; j < l; ++j)
    1969             :   {
    1970        1554 :     GEN a = gel(vabs,j);
    1971        1554 :     gel(vwt, j) = gprec_wtrunc(poleval(R, a), prec0);
    1972        1554 :     gel(vabs, j) = gprec_wtrunc(sqrtr_abs(a), prec0);
    1973             :   }
    1974          56 :   return gerepilecopy(ltop, mkvec2(vabs, vwt));
    1975             : }
    1976             : 
    1977             : /* Compute \int_{-oo}^oo w(x)f(x) dx, where w(x)=x/(exp(2pi x)-1)
    1978             :  * for x>0 and w(-x)=w(x). For Abel-Plana (sumnumap). */
    1979             : static GEN
    1980          56 : intnumgauexp(void *E, GEN (*eval)(void*,GEN), GEN gN, GEN tab, long prec)
    1981             : {
    1982          56 :   pari_sp av = avma;
    1983          56 :   GEN U = mkcomplex(gN, NULL), V = mkcomplex(gN, NULL), S = gen_0;
    1984          56 :   GEN vabs = gel(tab, 1), vwt = gel(tab, 2);
    1985          56 :   long l = lg(vabs), i;
    1986          56 :   if (lg(vwt) != l || typ(vabs) != t_VEC || typ(vwt) != t_VEC)
    1987           0 :     pari_err_TYPE("sumnumap", tab);
    1988        1610 :   for (i = 1; i < l; i++)
    1989             :   { /* I * (w_i/a_i) * (f(N + I*a_i) - f(N - I*a_i)) */
    1990        1554 :     GEN x = gel(vabs,i), w = gel(vwt,i), t;
    1991        1554 :     gel(U,2) = x;
    1992        1554 :     gel(V,2) = gneg(x);
    1993        1554 :     t = mulcxI(gsub(eval(E,U), eval(E,V)));
    1994        1554 :     S = gadd(S, gmul(gdiv(w,x), cxtoreal(t)));
    1995        1554 :     S = gprec_wensure(S, prec);
    1996             :   }
    1997          56 :   return gerepilecopy(av, gprec_wtrunc(S, prec));
    1998             : }
    1999             : 
    2000             : GEN
    2001          56 : sumnumapinit(GEN fast, long prec)
    2002             : {
    2003          56 :   if (!fast) fast = mkoo();
    2004          56 :   retmkvec2(intnumgauexpinit(prec), intnuminit(gen_1, fast, 0, prec));
    2005             : }
    2006             : 
    2007             : typedef struct {
    2008             :   GEN (*f)(void *E, GEN);
    2009             :   void *E;
    2010             :   long N;
    2011             : } expfn;
    2012             : 
    2013             : /* f(Nx) */
    2014             : static GEN
    2015       33922 : _exfn(void *E, GEN x)
    2016             : {
    2017       33922 :   expfn *S = (expfn*)E;
    2018       33922 :   return S->f(S->E, gmulsg(S->N, x));
    2019             : }
    2020             : 
    2021             : GEN
    2022          63 : sumnumap(void *E, GEN (*eval)(void*,GEN), GEN a, GEN tab, long prec)
    2023             : {
    2024          63 :   pari_sp av = avma;
    2025             :   expfn T;
    2026             :   GEN S, fast, gN;
    2027             :   long as, m, N;
    2028          63 :   if (!a) { a = gen_1; fast = get_oo(gen_0); }
    2029          63 :   else switch(typ(a))
    2030             :   {
    2031          21 :     case t_VEC:
    2032          21 :       if (lg(a) != 3) pari_err_TYPE("sumnumap", a);
    2033          21 :       fast = get_oo(gel(a,2));
    2034          21 :       a = gel(a,1); break;
    2035          42 :     default:
    2036          42 :       fast = get_oo(gen_0);
    2037             :   }
    2038          63 :   if (typ(a) != t_INT) pari_err_TYPE("sumnumap", a);
    2039          63 :   if (!tab) tab = sumnumapinit(fast, prec);
    2040          14 :   else if (typ(tab) != t_VEC || lg(tab) != 3) pari_err_TYPE("sumnumap",tab);
    2041          56 :   as = itos(a);
    2042          56 :   T.N = N = maxss(as + 1, (long)ceil(prec2nbits(prec)*0.327));
    2043          56 :   T.E = E;
    2044          56 :   T.f = eval;
    2045          56 :   gN = stoi(N);
    2046          56 :   S = gtofp(gmul2n(eval(E, gN), -1), prec);
    2047        4403 :   for (m = as; m < N; ++m)
    2048             :   {
    2049        4347 :     S = gadd(S, eval(E, stoi(m)));
    2050        4347 :     S = gprec_wensure(S, prec);
    2051             :   }
    2052          56 :   S = gadd(S, gmulsg(N, intnum(&T, &_exfn, gen_1, fast, gel(tab, 2), prec)));
    2053          56 :   S = gadd(S, intnumgauexp(E, eval, gN, gel(tab, 1), prec));
    2054          56 :   return gerepileupto(av, S);
    2055             : }
    2056             : 
    2057             : GEN
    2058          63 : sumnumap0(GEN a, GEN code, GEN tab, long prec)
    2059          63 : { EXPR_WRAP(code, sumnumap(EXPR_ARG, a, tab, prec)); }
    2060             : 
    2061             : /* max (1, |zeros|), P a t_POL or scalar */
    2062             : static double
    2063         133 : polmax(GEN P)
    2064             : {
    2065         133 :   pari_sp av = avma;
    2066             :   double r;
    2067         133 :   if (typ(P) != t_POL || degpol(P) <= 0) return 1.0;
    2068         133 :   r = gtodouble(polrootsbound(P, NULL));
    2069         133 :   return gc_double(av, maxdd(r, 1.0));
    2070             : }
    2071             : 
    2072             : /* max (1, |poles|), F a t_POL or t_RFRAC or scalar */
    2073             : static double
    2074          21 : ratpolemax(GEN F)
    2075             : {
    2076          21 :   if (typ(F) == t_POL) return 1.0;
    2077          21 :   return polmax(gel(F,2));
    2078             : }
    2079             : /* max (1, |poles|, |zeros|)) */
    2080             : static double
    2081          42 : ratpolemax2(GEN F)
    2082             : {
    2083          42 :   if (typ(F) == t_POL) return polmax(F);
    2084          42 :   return maxdd(polmax(gel(F,1)), polmax(gel(F,2)));
    2085             : }
    2086             : 
    2087             : static GEN
    2088       22099 : sercoeff(GEN x, long n)
    2089             : {
    2090       22099 :   long N = n - valp(x);
    2091       22099 :   return (N < 0)? gen_0: gel(x,N+2);
    2092             : }
    2093             : 
    2094             : /* Compute the integral from N to infinity of a rational function F, deg(F) < -1
    2095             :  * We must have N > 2 * r, r = max(1, |poles F|). */
    2096             : static GEN
    2097          28 : intnumainfrat(GEN F, long N, double r, long prec)
    2098             : {
    2099          28 :   long B = prec2nbits(prec), v, k, lim;
    2100             :   GEN S, ser;
    2101          28 :   pari_sp av = avma;
    2102             : 
    2103          28 :   lim = (long)ceil(B/log2(N/r));
    2104          28 :   ser = gmul(F, real_1(prec + EXTRAPREC64));
    2105          28 :   ser = rfracrecip_to_ser_absolute(ser, lim+2);
    2106          28 :   v = valp(ser);
    2107          28 :   S = gdivgs(sercoeff(ser,lim+1), lim*N);
    2108             :   /* goes down to 2, but coeffs are 0 in degree < v */
    2109        1673 :   for (k = lim; k >= v; k--) /* S <- (S + coeff(ser,k)/(k-1)) / N */
    2110        1645 :     S = gdivgs(gadd(S, gdivgs(sercoeff(ser,k), k-1)), N);
    2111          28 :   if (v-2) S = gdiv(S, powuu(N, v-2));
    2112          28 :   return gerepilecopy(av, gprec_wtrunc(S, prec));
    2113             : }
    2114             : 
    2115             : static GEN
    2116          28 : rfrac_eval0(GEN R)
    2117             : {
    2118          28 :   GEN N, n, D = gel(R,2), d = constant_coeff(D);
    2119          28 :   if (gcmp0(d)) return NULL;
    2120          21 :   N = gel(R,1);
    2121          21 :   n = typ(N)==t_POL? constant_coeff(N): N;
    2122          21 :   return gdiv(n, d);
    2123             : }
    2124             : static GEN
    2125        2093 : rfrac_eval(GEN R, GEN a)
    2126             : {
    2127        2093 :   GEN D = gel(R,2), d = poleval(D,a);
    2128        2093 :   return gcmp0(d)? NULL: gdiv(poleval(gel(R,1),a), d);
    2129             : }
    2130             : /* R = \sum_i vR[i], eval at a omitting poles */
    2131             : static GEN
    2132        2093 : RFRAC_eval(GEN R, GEN vR, GEN a)
    2133             : {
    2134        2093 :   GEN S = rfrac_eval(R,a);
    2135        2093 :   if (!S && vR)
    2136             :   {
    2137           0 :     long i, l = lg(vR);
    2138           0 :     for (i = 1; i < l; i++)
    2139             :     {
    2140           0 :       GEN z = rfrac_eval(gel(vR,i), a);
    2141           0 :       if (z) S = S? gadd(S,z): z;
    2142             :     }
    2143             :   }
    2144        2093 :   return S;
    2145             : }
    2146             : static GEN
    2147        2093 : add_RFRAC_eval(GEN S, GEN R, GEN vR, GEN a)
    2148             : {
    2149        2093 :   GEN z = RFRAC_eval(R, vR, a);
    2150        2093 :   return z? gadd(S, z): S;
    2151             : }
    2152             : static GEN
    2153          21 : add_sumrfrac(GEN S, GEN R, GEN vR, long b)
    2154             : {
    2155             :   long m;
    2156        2114 :   for (m = b; m >= 1; m--) S = add_RFRAC_eval(S,R,vR,utoipos(m));
    2157          21 :   return S;
    2158             : }
    2159             : static void
    2160          28 : get_kN(long r, long B, long *pk, long *pN)
    2161             : {
    2162          28 :   long k = maxss(50, (long)ceil(0.241*B));
    2163             :   GEN z;
    2164          28 :   if (k&1L) k++;
    2165          28 :   *pk = k; constbern(k >> 1);
    2166          28 :   z = sqrtnr_abs(gmul2n(gtofp(bernfrac(k), LOWDEFAULTPREC), B), k);
    2167          28 :   *pN = maxss(2*r, r + 1 + itos(gceil(z)));
    2168          28 : }
    2169             : /* F a t_RFRAC, F0 = F(0) or NULL [pole], vF a vector of t_RFRAC summing to F
    2170             :  * or NULL [F atomic] */
    2171             : static GEN
    2172          28 : sumnumrat_i(GEN F, GEN F0, GEN vF, long prec)
    2173             : {
    2174          28 :   long B = prec2nbits(prec), vx, j, k, N;
    2175             :   GEN S, S1, S2, intf, _1;
    2176             :   double r;
    2177          28 :   if (poldegree(F, -1) > -2) pari_err(e_MISC, "sum diverges in sumnumrat");
    2178          21 :   vx = varn(gel(F,2));
    2179          21 :   r = ratpolemax(F);
    2180          21 :   get_kN((long)ceil(r), B, &k,&N);
    2181          21 :   intf = intnumainfrat(F, N, r, prec);
    2182             :   /* N > ratpolemax(F) is not a pole */
    2183          21 :   _1 = real_1(prec);
    2184          21 :   S1 = gmul2n(gmul(_1, gsubst(F, vx, utoipos(N))), -1);
    2185          21 :   S1 = add_sumrfrac(S1, F, vF, N-1);
    2186          21 :   if (F0) S1 = gadd(S1, F0);
    2187          21 :   S = gmul(_1, gsubst(F, vx, gaddgs(pol_x(vx), N)));
    2188          21 :   S = rfrac_to_ser(S, k + 2);
    2189          21 :   S2 = gen_0;
    2190        1008 :   for (j = 2; j <= k; j += 2)
    2191         987 :     S2 = gadd(S2, gmul(gdivgs(bernfrac(j),j), sercoeff(S, j-1)));
    2192          21 :   return gadd(intf, gsub(S1, S2));
    2193             : }
    2194             : /* sum_{n >= a} F(n) */
    2195             : GEN
    2196          56 : sumnumrat(GEN F, GEN a, long prec)
    2197             : {
    2198          56 :   pari_sp av = avma;
    2199             :   long vx;
    2200             :   GEN vF, F0;
    2201             : 
    2202          56 :   switch(typ(F))
    2203             :   {
    2204          42 :     case t_RFRAC: break;
    2205          14 :     case t_INT: case t_REAL: case t_COMPLEX: case t_POL:
    2206          14 :       if (gequal0(F)) return real_0(prec);
    2207           7 :     default: pari_err_TYPE("sumnumrat",F);
    2208             :   }
    2209          42 :   vx = varn(gel(F,2));
    2210          42 :   switch(typ(a))
    2211             :   {
    2212          21 :     case t_INT:
    2213          21 :       if (signe(a)) F = gsubst(F, vx, deg1pol_shallow(gen_1,a,vx));
    2214          21 :       F0 = rfrac_eval0(F);
    2215          21 :       vF = NULL;
    2216          21 :       break;
    2217          21 :     case t_INFINITY:
    2218          21 :       if (inf_get_sign(a) == -1)
    2219             :       { /* F(x) + F(-x). Could divide degree by 2, as G(x^2): pb with poles */
    2220          14 :         GEN F2 = gsubst(F, vx, RgX_neg(pol_x(vx)));
    2221          14 :         vF = mkvec2(F,F2);
    2222          14 :         F = gadd(F, F2);
    2223          14 :         if (gequal0(F)) { set_avma(av); return real_0(prec); }
    2224           7 :         F0 = rfrac_eval0(gel(vF,1));
    2225           7 :         break;
    2226             :       }
    2227             :     default:
    2228           7 :       pari_err_TYPE("sumnumrat",a);
    2229             :       return NULL; /* LCOV_EXCL_LINE */
    2230             :   }
    2231          28 :   return gerepileupto(av, sumnumrat_i(F, F0, vF, prec));
    2232             : }
    2233             : /* deg ((a / b) - 1), assuming b a t_POL of positive degree in main variable  */
    2234             : static long
    2235          56 : rfracm1_degree(GEN a, GEN b)
    2236             : {
    2237             :   long da, db;
    2238          56 :   if (typ(a) != t_POL || varn(a) != varn(b)) return 0;
    2239          56 :   da = degpol(a);
    2240          56 :   db = degpol(b); if (da != db) return maxss(da - db, 0);
    2241          56 :   return degpol(RgX_sub(a,b)) - db;
    2242             : }
    2243             : 
    2244             : /* prod_{n >= a} F(n) */
    2245             : GEN
    2246          28 : prodnumrat(GEN F, long a, long prec)
    2247             : {
    2248          28 :   pari_sp ltop = avma;
    2249          28 :   long B = prec2nbits(prec), j, k, m, N, vx;
    2250             :   GEN S, S1, S2, intf, G;
    2251             :   double r;
    2252             : 
    2253          28 :   switch(typ(F))
    2254             :   {
    2255          14 :     case t_RFRAC: break;
    2256          14 :     case t_INT: case t_REAL: case t_COMPLEX: case t_POL:
    2257          14 :       if (gequal1(F)) return real_1(prec);
    2258           7 :     default: pari_err_TYPE("prodnumrat",F);
    2259             :   }
    2260          14 :   if (rfracm1_degree(gel(F,1), gel(F,2)) > -2)
    2261           7 :     pari_err(e_MISC, "product diverges in prodnumrat");
    2262           7 :   vx = varn(gel(F,2));
    2263           7 :   if (a) F = gsubst(F, vx, gaddgs(pol_x(vx), a));
    2264           7 :   r = ratpolemax2(F);
    2265           7 :   get_kN((long)ceil(r), B, &k,&N);
    2266           7 :   G = gdiv(deriv(F, vx), F);
    2267           7 :   intf = intnumainfrat(gmul(pol_x(vx),G), N, r, prec);
    2268           7 :   intf = gneg(gadd(intf, gmulsg(N, glog(gsubst(F, vx, stoi(N)), prec))));
    2269           7 :   S = gmul(real_1(prec), gsubst(G, vx, gaddgs(pol_x(vx), N)));
    2270           7 :   S = rfrac_to_ser(S, k + 2);
    2271           7 :   S1 = gsqrt(gsubst(F, vx, utoipos(N)), prec);
    2272         714 :   for (m = 0; m < N; m++) S1 = gmul(S1, gsubst(F, vx, utoi(m)));
    2273           7 :   S2 = gen_0;
    2274         336 :   for (j = 2; j <= k; j += 2)
    2275         329 :     S2 = gadd(S2, gmul(gdivgs(bernfrac(j),j*(j-1)), sercoeff(S, j-2)));
    2276           7 :   return gerepileupto(ltop, gmul(S1, gexp(gsub(intf, S2), prec)));
    2277             : }
    2278             : 
    2279             : /* fan = factoru(n); sum_{d | n} mu(d)/d * s[n/d] */
    2280             : static GEN
    2281        4956 : sdmob(GEN s, long n, GEN fan)
    2282             : {
    2283        4956 :   GEN D = divisorsu_moebius(gel(fan,1)), S = sercoeff(s, n); /* d = 1 */
    2284        4956 :   long i, l = lg(D);
    2285       19110 :   for (i = 2; i < l; i++)
    2286       14154 :     S = gadd(S, gdivgs(sercoeff(s, n/labs(D[i])), D[i]));
    2287        4956 :   return S;
    2288             : }
    2289             : /* log (zeta(s) * prod_i (1 - P[i]^-s) */
    2290             : static GEN
    2291        4151 : logzetan(GEN s, GEN P, long prec)
    2292             : {
    2293        4151 :   GEN Z = gzeta(s, prec);
    2294        4151 :   long i, l = lg(P);
    2295       61747 :   for (i = 1; i < l; i++) Z = gsub(Z, gdiv(Z, gpow(gel(P,i), s, prec)));
    2296        4151 :   return glog(Z, prec);
    2297             : }
    2298             : static GEN
    2299          56 : sumlogzeta(GEN ser, GEN s, GEN P, double rs, double lN, long vF, long lim,
    2300             :            long prec)
    2301             : {
    2302          56 :   GEN z = gen_0, v = vecfactoru_i(vF,lim);
    2303          56 :   pari_sp av = avma;
    2304             :   long i, n;
    2305          56 :   if (typ(s) == t_INT) constbern((itos(s) * lim + 1) >> 1);
    2306        5012 :   for (n = lim, i = lg(v)-1; n >= vF; n--, i--)
    2307             :   {
    2308        4956 :     GEN t = sdmob(ser, n, gel(v,i));
    2309        4956 :     if (!gequal0(t))
    2310             :     { /* (n Re(s) - 1) log2(N) bits cancel in logzetan */
    2311        4151 :       long prec2 = prec + nbits2extraprec((n*rs-1) * lN);
    2312        4151 :       GEN L = logzetan(gmulsg(n,gprec_wensure(s,prec2)), P, prec2);
    2313        4151 :       z = gerepileupto(av, gadd(z, gmul(L, t)));
    2314        4151 :       z = gprec_wensure(z, prec);
    2315             :     }
    2316             :   }
    2317          56 :   return gprec_wtrunc(z, prec);
    2318             : }
    2319             : 
    2320             : static GEN
    2321         595 : rfrac_evalfp(GEN F, GEN x, long prec)
    2322             : {
    2323         595 :   GEN N = gel(F,1), D = gel(F,2), a, b = poleval(D, x);
    2324         595 :   a = (typ(N) == t_POL && varn(N) == varn(D))? poleval(N, x): N;
    2325         595 :   if (typ(a) != t_INT || typ(b) != t_INT ||
    2326         595 :       (lgefint(a) <= prec && lgefint(b) <= prec)) return gdiv(a, b);
    2327          70 :   return rdivii(a, b, prec + EXTRAPRECWORD);
    2328             : }
    2329             : 
    2330             : /* { F(p^s): p in P, p >= a }, F t_RFRAC */
    2331             : static GEN
    2332          56 : vFps(GEN P, long a, GEN F, GEN s, long prec)
    2333             : {
    2334          56 :   long i, j, l = lg(P);
    2335          56 :   GEN v = cgetg(l, t_VEC);
    2336         658 :   for (i = j = 1; i < l; i++)
    2337             :   {
    2338         602 :     GEN p = gel(P,i); if (cmpiu(p, a) < 0) continue;
    2339         595 :     gel(v, j++) = rfrac_evalfp(F, gpow(p, s, prec), prec);
    2340             :   }
    2341          56 :   setlg(v, j); return v;
    2342             : }
    2343             : 
    2344             : static void
    2345          98 : euler_set_Fs(GEN *F, GEN *s)
    2346             : {
    2347          98 :   if (!*s) *s = gen_1;
    2348          98 :   if (typ(*F) == t_RFRAC)
    2349             :   {
    2350             :     long m;
    2351          70 :     *F = rfrac_deflate_max(*F, &m);
    2352          70 :     if (m != 1) *s = gmulgs(*s, m);
    2353             :   }
    2354          98 : }
    2355             : /* sum_{p prime, p >= a} F(p^s), F rational function */
    2356             : GEN
    2357          42 : sumeulerrat(GEN F, GEN s, long a, long prec)
    2358             : {
    2359          42 :   pari_sp av = avma;
    2360             :   GEN ser, z, P;
    2361             :   double r, rs, RS, lN;
    2362          42 :   long B = prec2nbits(prec), prec2 = prec + EXTRAPREC64, vF, N, lim;
    2363             : 
    2364          42 :   euler_set_Fs(&F, &s);
    2365          42 :   switch(typ(F))
    2366             :   {
    2367          28 :     case t_RFRAC: break;
    2368          14 :     case t_INT: case t_REAL: case t_COMPLEX: case t_POL:
    2369          14 :       if (gequal0(F)) return real_0(prec);
    2370           7 :     default: pari_err_TYPE("sumeulerrat",F);
    2371             :   }
    2372             :   /* F t_RFRAC */
    2373          28 :   if (a < 2) a = 2;
    2374          28 :   vF = -poldegree(F, -1);
    2375          28 :   rs = gtodouble(real_i(s));
    2376          28 :   r = polmax(gel(F,2));
    2377          28 :   N = maxss(30, a); lN = log2((double)N);
    2378          28 :   RS = maxdd(1./vF, log2(r) / lN);
    2379          28 :   if (rs <= RS)
    2380           7 :     pari_err_DOMAIN("sumeulerrat", "real(s)", "<=",  dbltor(RS), dbltor(rs));
    2381          21 :   lim = (long)ceil(B / (rs*lN - log2(r)));
    2382          21 :   ser = gmul(real_1(prec2), F);
    2383          21 :   ser = rfracrecip_to_ser_absolute(ser, lim+1);
    2384          21 :   P = primes_interval(gen_2, utoipos(N));
    2385          21 :   z = sumlogzeta(ser, s, P, rs, lN, vF, lim, prec);
    2386          21 :   z = gadd(z, vecsum(vFps(P, a, F, s, prec)));
    2387          21 :   return gerepilecopy(av, gprec_wtrunc(z, prec));
    2388             : }
    2389             : 
    2390             : /* F = N/D; return F'/F. Assume D a t_POL */
    2391             : static GEN
    2392          35 : rfrac_logderiv(GEN N, GEN D)
    2393             : {
    2394          35 :   if (typ(N) != t_POL || varn(N) != varn(D)) return gdiv(gneg(RgX_deriv(D)), D);
    2395          35 :   if (!degpol(D)) return gdiv(RgX_deriv(N), N);
    2396          14 :   return gdiv(RgX_sub(RgX_mul(RgX_deriv(N), D), RgX_mul(RgX_deriv(D), N)),
    2397             :               RgX_mul(N, D));
    2398             : }
    2399             : 
    2400             : /* prod_{p prime, p >= a} F(p^s), F rational function */
    2401             : GEN
    2402          56 : prodeulerrat(GEN F, GEN s, long a, long prec)
    2403             : {
    2404          56 :   pari_sp ltop = avma;
    2405             :   GEN DF, NF, ser, P, z;
    2406             :   double r, rs, RS, lN;
    2407          56 :   long B = prec2nbits(prec), prec2 = prec + EXTRAPREC64, vF, N, lim;
    2408             : 
    2409          56 :   euler_set_Fs(&F, &s);
    2410          56 :   switch(typ(F))
    2411             :   {
    2412          42 :     case t_RFRAC: break;
    2413          14 :     case t_INT: case t_REAL: case t_COMPLEX: case t_POL:
    2414          14 :       if (gequal1(F)) return real_1(prec);
    2415           7 :     default: pari_err_TYPE("prodeulerrat",F);
    2416             :   } /* F t_RFRAC */
    2417          42 :   NF = gel(F, 1);
    2418          42 :   DF = gel(F, 2);
    2419          42 :   rs = gtodouble(real_i(s));
    2420          42 :   vF = - rfracm1_degree(NF, DF);
    2421          42 :   if (rs * vF <= 1) pari_err(e_MISC, "product diverges in prodeulerrat");
    2422          35 :   r = ratpolemax2(F);
    2423          35 :   N = maxss(maxss(30, a), (long)ceil(2*r)); lN = log2((double)N);
    2424          35 :   RS = maxdd(1./vF, log2(r) / lN);
    2425          35 :   if (rs <= RS)
    2426           0 :     pari_err_DOMAIN("prodeulerrat", "real(s)", "<=",  dbltor(RS), dbltor(rs));
    2427          35 :   lim = (long)ceil(B / (rs*lN - log2(r)));
    2428          35 :   (void)rfracrecip(&NF, &DF); /* returned value is 0 */
    2429          35 :   if (!RgX_is_ZX(DF) || !is_pm1(gel(DF,2))
    2430          35 :       || lim * log2(r) > 4 * B) NF = gmul(NF, real_1(prec2));
    2431          35 :   ser = integser(rfrac_to_ser(rfrac_logderiv(NF,DF), lim+3));
    2432             :   /* ser = log f, f = F(1/x) + O(x^(lim+1)) */
    2433          35 :   P = primes_interval(gen_2, utoipos(N));
    2434          35 :   z = gexp(sumlogzeta(ser, s, P, rs, lN, vF, lim, prec), prec);
    2435          35 :   z = gmul(z, vecprod(vFps(P, a, F, s, prec)));
    2436          35 :   return gerepilecopy(ltop, gprec_wtrunc(z, prec));
    2437             : }
    2438             : 
    2439             : /* Compute $\sum_{n\ge a}c(n)$ using Lagrange extrapolation.
    2440             : Assume that the $N$-th remainder term of the series has a
    2441             : regular asymptotic expansion in integral powers of $1/N$. */
    2442             : static GEN
    2443          42 : sumnumlagrange1init(GEN c1, long flag, long prec)
    2444             : {
    2445          42 :   pari_sp av = avma;
    2446             :   GEN V, W, T;
    2447             :   double c1d;
    2448          42 :   long B = prec2nbits(prec), prec2;
    2449             :   ulong n, N;
    2450          42 :   c1d = c1 ? gtodouble(c1) : 0.332;
    2451          42 :   N = (ulong)ceil(c1d*B); if ((N&1L) == 0) N++;
    2452          42 :   prec2 = nbits2prec(B+(long)ceil(1.8444*N) + 16);
    2453          42 :   W = vecbinome(N);
    2454          42 :   T = vecpowuu(N, N);
    2455          42 :   V = cgetg(N+1, t_VEC); gel(V,N) = gel(T,N);
    2456        4074 :   for (n = N-1; n >= 1; n--)
    2457             :   {
    2458        4032 :     pari_sp av = avma;
    2459        4032 :     GEN t = mulii(gel(W, n+1), gel(T,n));
    2460        4032 :     if (!odd(n)) togglesign_safe(&t);
    2461        4032 :     if (flag) t = addii(gel(V, n+1), t);
    2462        4032 :     gel(V, n) = gerepileuptoint(av, t);
    2463             :   }
    2464          42 :   V = gdiv(RgV_gtofp(V, prec2), mpfact(N));
    2465          42 :   return gerepilecopy(av, mkvec4(gen_1, stoi(prec2), gen_1, V));
    2466             : }
    2467             : 
    2468             : static GEN
    2469           7 : sumnumlagrange2init(GEN c1, long flag, long prec)
    2470             : {
    2471           7 :   pari_sp av = avma;
    2472             :   GEN V, W, T, told;
    2473           7 :   double c1d = c1 ? gtodouble(c1) : 0.228;
    2474           7 :   long B = prec2nbits(prec), prec2;
    2475             :   ulong n, N;
    2476             : 
    2477           7 :   N = (ulong)ceil(c1d*B); if ((N&1L) == 0) N++;
    2478           7 :   prec2 = nbits2prec(B+(long)ceil(1.18696*N) + 16);
    2479           7 :   W = vecbinome(2*N);
    2480           7 :   T = vecpowuu(N, 2*N);
    2481           7 :   V = cgetg(N+1, t_VEC); gel(V, N) = told = gel(T,N);
    2482         623 :   for (n = N-1; n >= 1; n--)
    2483             :   {
    2484         616 :     GEN tnew = mulii(gel(W, N-n+1), gel(T,n));
    2485         616 :     if (!odd(n)) togglesign_safe(&tnew);
    2486         616 :     told = addii(told, tnew);
    2487         616 :     if (flag) told = addii(gel(V, n+1), told);
    2488         616 :     gel(V, n) = told; told = tnew;
    2489             :   }
    2490           7 :   V = gdiv(RgV_gtofp(V, prec2), mpfact(2*N));
    2491           7 :   return gerepilecopy(av, mkvec4(gen_2, stoi(prec2), gen_1, V));
    2492             : }
    2493             : 
    2494             : static GEN
    2495     1844164 : _mpmul(GEN x, GEN y)
    2496             : {
    2497     1844164 :   if (!x) return y;
    2498     1839306 :   return y? mpmul(x, y): x;
    2499             : }
    2500             : /* Used only for al = 2, 1, 1/2, 1/3, 1/4. */
    2501             : static GEN
    2502          49 : sumnumlagrangeinit_i(GEN al, GEN c1, long flag, long prec)
    2503             : {
    2504          49 :   pari_sp av = avma;
    2505             :   GEN V, W;
    2506          49 :   double c1d = 0.0, c2;
    2507          49 :   long B = prec2nbits(prec), B1, prec2, dal;
    2508             :   ulong j, n, N;
    2509             : 
    2510          49 :   if (typ(al) == t_INT)
    2511             :   {
    2512          28 :     switch(itos_or_0(al))
    2513             :     {
    2514          21 :       case 1: return sumnumlagrange1init(c1, flag, prec);
    2515           7 :       case 2: return sumnumlagrange2init(c1, flag, prec);
    2516           0 :       default: pari_err_IMPL("sumnumlagrange for this alpha");
    2517             :     }
    2518             :   }
    2519          21 :   if (typ(al) != t_FRAC) pari_err_TYPE("sumnumlagrangeinit",al);
    2520          21 :   dal = itos_or_0(gel(al,2));
    2521          21 :   if (dal > 4 || !equali1(gel(al,1)))
    2522           7 :     pari_err_IMPL("sumnumlagrange for this alpha");
    2523          14 :   switch(dal)
    2524             :   {
    2525           7 :     case 2: c2 = 2.6441; c1d = 0.62; break;
    2526           7 :     case 3: c2 = 3.1578; c1d = 1.18; break;
    2527           0 :     case 4: c2 = 3.5364; c1d = 3.00; break;
    2528             :     default: return NULL; /* LCOV_EXCL_LINE */
    2529             :   }
    2530          14 :   if (c1)
    2531             :   {
    2532           0 :     c1d = gtodouble(c1);
    2533           0 :     if (c1d <= 0)
    2534           0 :       pari_err_DOMAIN("sumnumlagrangeinit", "c1", "<=", gen_0, c1);
    2535             :   }
    2536          14 :   N = (ulong)ceil(c1d*B); if ((N&1L) == 0) N++;
    2537          14 :   B1 = B + (long)ceil(c2*N) + 16;
    2538          14 :   prec2 = nbits2prec(B1);
    2539          14 :   V = vecpowug(N, al, prec2);
    2540          14 :   W = cgetg(N+1, t_VEC);
    2541        4872 :   for (n = 1; n <= N; ++n)
    2542             :   {
    2543        4858 :     pari_sp av2 = avma;
    2544        4858 :     GEN t = NULL, vn = gel(V, n);
    2545     1853880 :     for (j = 1; j <= N; j++)
    2546     1849022 :       if (j != n) t = _mpmul(t, mpsub(vn, gel(V, j)));
    2547        4858 :     gel(W, n) = gerepileuptoleaf(av2, mpdiv(gpowgs(vn, N-1), t));
    2548             :   }
    2549          14 :   if (flag)
    2550        4858 :     for (n = N-1; n >= 1; n--) gel(W, n) = gadd(gel(W, n+1), gel(W, n));
    2551          14 :   return gerepilecopy(av, mkvec4(al, stoi(prec2), gen_1, W));
    2552             : }
    2553             : 
    2554             : GEN
    2555          70 : sumnumlagrangeinit(GEN al, GEN c1, long prec)
    2556             : {
    2557          70 :   pari_sp ltop = avma;
    2558             :   GEN V, W, S, be;
    2559             :   long n, prec2, fl, N;
    2560             : 
    2561          70 :   if (!al) return sumnumlagrange1init(c1, 1, prec);
    2562          49 :   if (typ(al) != t_VEC) al = mkvec2(gen_1, al);
    2563          35 :   else if (lg(al) != 3) pari_err_TYPE("sumnumlagrangeinit",al);
    2564          49 :   be = gel(al,2);
    2565          49 :   al = gel(al,1);
    2566          49 :   if (gequal0(be)) return sumnumlagrangeinit_i(al, c1, 1, prec);
    2567          14 :   V = sumnumlagrangeinit_i(al, c1, 0, prec);
    2568          14 :   switch(typ(be))
    2569             :   {
    2570           0 :     case t_CLOSURE: fl = 1; break;
    2571           7 :     case t_INT: case t_FRAC: case t_REAL: fl = 0; break;
    2572           7 :     default: pari_err_TYPE("sumnumlagrangeinit", be);
    2573             :              return NULL; /* LCOV_EXCL_LINE */
    2574             :   }
    2575           7 :   prec2 = itos(gel(V, 2));
    2576           7 :   W = gel(V, 4);
    2577           7 :   N = lg(W) - 1;
    2578           7 :   S = gen_0; V = cgetg(N+1, t_VEC);
    2579         910 :   for (n = N; n >= 1; n--)
    2580             :   {
    2581         903 :     GEN tmp, gn = stoi(n);
    2582         903 :     tmp = fl ? closure_callgen1prec(be, gn, prec2) : gpow(gn, gneg(be), prec2);
    2583         903 :     tmp = gdiv(gel(W, n), tmp);
    2584         903 :     S = gadd(S, tmp);
    2585         903 :     gel(V, n) = (n == N)? tmp: gadd(gel(V, n+1), tmp);
    2586             :   }
    2587           7 :   return gerepilecopy(ltop, mkvec4(al, stoi(prec2), S, V));
    2588             : }
    2589             : 
    2590             : /* - sum_{n=1}^{as-1} f(n) */
    2591             : static GEN
    2592          14 : sumaux(void *E, GEN (*eval)(void*,GEN,long), long as, long prec)
    2593             : {
    2594          14 :   GEN S = gen_0;
    2595             :   long n;
    2596          14 :   if (as > 1)
    2597             :   {
    2598          14 :     for (n = 1; n < as; ++n)
    2599             :     {
    2600           7 :       S = gadd(S, eval(E, stoi(n), prec));
    2601           7 :       S = gprec_wensure(S, prec);
    2602             :     }
    2603           7 :     S = gneg(S);
    2604             :   }
    2605             :   else
    2606           7 :     for (n = as; n <= 0; ++n)
    2607             :     {
    2608           0 :       S = gadd(S, eval(E, stoi(n), prec));
    2609           0 :       S = gprec_wensure(S, prec);
    2610             :     }
    2611          14 :   return S;
    2612             : }
    2613             : 
    2614             : GEN
    2615          91 : sumnumlagrange(void *E, GEN (*eval)(void*,GEN,long), GEN a, GEN tab, long prec)
    2616             : {
    2617          91 :   pari_sp av = avma;
    2618             :   GEN s, S, al, V;
    2619             :   long as, prec2;
    2620             :   ulong n, l;
    2621             : 
    2622          91 :   if (typ(a) != t_INT) pari_err_TYPE("sumnumlagrange", a);
    2623          91 :   if (!tab) tab = sumnumlagrangeinit(NULL, tab, prec);
    2624          70 :   else if (lg(tab) != 5 || typ(gel(tab,2)) != t_INT || typ(gel(tab,4)) != t_VEC)
    2625           0 :     pari_err_TYPE("sumnumlagrange", tab);
    2626             : 
    2627          91 :   as = itos(a);
    2628          91 :   al = gel(tab, 1);
    2629          91 :   prec2 = itos(gel(tab, 2));
    2630          91 :   S = gel(tab, 3);
    2631          91 :   V = gel(tab, 4);
    2632          91 :   l = lg(V);
    2633          91 :   if (gequal(al, gen_2))
    2634             :   {
    2635          14 :     s = sumaux(E, eval, as, prec2);
    2636          14 :     as = 1;
    2637             :   }
    2638             :   else
    2639          77 :     s = gen_0;
    2640       16772 :   for (n = 1; n < l; n++)
    2641             :   {
    2642       16681 :     s = gadd(s, gmul(gel(V, n), eval(E, stoi(n+as-1), prec2)));
    2643       16681 :     s = gprec_wensure(s, prec);
    2644             :   }
    2645          91 :   if (!gequal1(S)) s = gdiv(s,S);
    2646          91 :   return gerepilecopy(av, gprec_wtrunc(s, prec));
    2647             : }
    2648             : 
    2649             : GEN
    2650          91 : sumnumlagrange0(GEN a, GEN code, GEN tab, long prec)
    2651          91 : { EXPR_WRAP(code, sumnumlagrange(EXPR_ARGPREC, a, tab, prec)); }

Generated by: LCOV version 1.13