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

Generated by: LCOV version 1.13