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 21739-44c3c17) Lines: 1429 1461 97.8 %
Date: 2018-01-20 06:18:48 Functions: 117 118 99.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11