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

Generated by: LCOV version 1.11