Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - language - sumiter.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23344-f0cc1b3f6) Lines: 1081 1116 96.9 %
Date: 2018-12-12 05:41:43 Functions: 91 91 100.0 %
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             : GEN
      18     1272429 : iferrpari(GEN a, GEN b, GEN c)
      19             : {
      20             :   GEN res;
      21             :   struct pari_evalstate state;
      22     1272429 :   evalstate_save(&state);
      23     1272428 :   pari_CATCH(CATCH_ALL)
      24             :   {
      25             :     GEN E;
      26       68846 :     if (!b&&!c) return gnil;
      27       34430 :     E = evalstate_restore_err(&state);
      28       34430 :     if (c)
      29             :     {
      30         284 :       push_lex(E,c);
      31         284 :       res = closure_evalnobrk(c);
      32         277 :       pop_lex(1);
      33         277 :       if (gequal0(res))
      34           7 :         pari_err(0, E);
      35             :     }
      36       34416 :     if (!b) return gnil;
      37       34416 :     push_lex(E,b);
      38       34416 :     res = closure_evalgen(b);
      39       34416 :     pop_lex(1);
      40       34416 :     return res;
      41             :   } pari_TRY {
      42     1272429 :     res = closure_evalgen(a);
      43     1238004 :   } pari_ENDCATCH;
      44     1238004 :   return res;
      45             : }
      46             : 
      47             : /********************************************************************/
      48             : /**                                                                **/
      49             : /**                        ITERATIONS                              **/
      50             : /**                                                                **/
      51             : /********************************************************************/
      52             : 
      53             : static void
      54     5066673 : forparii(GEN a, GEN b, GEN code)
      55             : {
      56     5066673 :   pari_sp av, av0 = avma;
      57             :   GEN aa;
      58     5066673 :   if (gcmp(b,a) < 0) return;
      59     5006458 :   if (typ(b) != t_INFINITY) b = gfloor(b);
      60     5006457 :   aa = a = setloop(a);
      61     5006456 :   av=avma;
      62     5006456 :   push_lex(a,code);
      63    55855655 :   while (gcmp(a,b) <= 0)
      64             :   {
      65    45981724 :     closure_evalvoid(code); if (loop_break()) break;
      66    45884536 :     a = get_lex(-1);
      67    45866472 :     if (a == aa)
      68             :     {
      69    45866444 :       a = incloop(a);
      70    45842709 :       if (a != aa) { set_lex(-1,a); aa = a; }
      71             :     }
      72             :     else
      73             :     { /* 'code' modified a ! Be careful (and slow) from now on */
      74          28 :       a = gaddgs(a,1);
      75          28 :       if (gc_needed(av,1))
      76             :       {
      77           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"forparii");
      78           0 :         a = gerepileupto(av,a);
      79             :       }
      80          28 :       set_lex(-1,a);
      81             :     }
      82             :   }
      83     5000367 :   pop_lex(1);  set_avma(av0);
      84             : }
      85             : 
      86             : void
      87     5066680 : forpari(GEN a, GEN b, GEN code)
      88             : {
      89     5066680 :   pari_sp ltop=avma, av;
      90     5066680 :   if (typ(a) == t_INT) { forparii(a,b,code); return; }
      91           7 :   b = gcopy(b); /* Kludge to work-around the a+(a=2) bug */
      92           7 :   av=avma;
      93           7 :   push_lex(a,code);
      94          35 :   while (gcmp(a,b) <= 0)
      95             :   {
      96          21 :     closure_evalvoid(code); if (loop_break()) break;
      97          21 :     a = get_lex(-1); a = gaddgs(a,1);
      98          21 :     if (gc_needed(av,1))
      99             :     {
     100           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"forpari");
     101           0 :       a = gerepileupto(av,a);
     102             :     }
     103          21 :     set_lex(-1, a);
     104             :   }
     105           7 :   pop_lex(1); set_avma(ltop);
     106             : }
     107             : 
     108             : /* 0 < a <= b. Using small consecutive chunks to 1) limit memory use, 2) allow
     109             :  * cheap early abort */
     110             : static int
     111          56 : forfactoredpos(ulong a, ulong b, GEN code)
     112             : {
     113          56 :   const ulong step = 1024;
     114          56 :   pari_sp av = avma;
     115             :   ulong x1;
     116        6881 :   for(x1 = a;; x1 += step, set_avma(av))
     117        6825 :   { /* beware overflow, fuse last two bins (avoid a tiny remainder) */
     118        6881 :     ulong j, lv, x2 = (b >= 2*step && b - 2*step >= x1)? x1-1 + step: b;
     119        6881 :     GEN v = vecfactoru(x1, x2);
     120        6881 :     lv = lg(v);
     121     7008386 :     for (j = 1; j < lv; j++)
     122             :     {
     123     7001519 :       ulong n = x1-1 + j;
     124     7001519 :       set_lex(-1, mkvec2(utoipos(n), Flm_to_ZM(gel(v,j))));
     125     7001519 :       closure_evalvoid(code);
     126     7001519 :       if (loop_break()) return 1;
     127             :     }
     128        6867 :     if (x2 == b) break;
     129        6825 :     set_lex(-1, gen_0);
     130             :   }
     131          42 :   return 0;
     132             : }
     133             : 
     134             : /* vector of primes to squarefree factorization */
     135             : static GEN
     136     4255531 : zv_to_ZM(GEN v)
     137     4255531 : { return mkmat2(zc_to_ZC(v), const_col(lg(v)-1,gen_1)); }
     138             : /* vector of primes to negative squarefree factorization */
     139             : static GEN
     140     4255531 : zv_to_mZM(GEN v)
     141             : {
     142     4255531 :   long i, l = lg(v);
     143     4255531 :   GEN w = cgetg(l+1, t_COL);
     144     4255531 :   gel(w,1) = gen_m1; for (i = 1; i < l; i++) gel(w,i+1) = utoipos(v[i]);
     145     4255531 :   return mkmat2(w, const_col(l,gen_1));
     146             : }
     147             : /* 0 <= a <= b. Using small consecutive chunks to 1) limit memory use, 2) allow
     148             :  * cheap early abort */
     149             : static void
     150          14 : forsquarefreepos(ulong a, ulong b, GEN code)
     151             : {
     152          14 :   const ulong step = 1024;
     153          14 :   pari_sp av = avma;
     154             :   ulong x1;
     155        6839 :   for(x1 = a;; x1 += step, set_avma(av))
     156        6825 :   { /* beware overflow, fuse last two bins (avoid a tiny remainder) */
     157        6839 :     ulong j, lv, x2 = (b >= 2*step && b - 2*step >= x1)? x1-1 + step: b;
     158        6839 :     GEN v = vecfactorsquarefreeu(x1, x2);
     159        6839 :     lv = lg(v);
     160     7006916 :     for (j = 1; j < lv; j++) if (gel(v,j))
     161             :     {
     162     4255531 :       ulong n = x1-1 + j;
     163     4255531 :       set_lex(-1, mkvec2(utoipos(n), zv_to_ZM(gel(v,j))));
     164     4255531 :       closure_evalvoid(code); if (loop_break()) return;
     165             :     }
     166        6839 :     if (x2 == b) break;
     167        6825 :     set_lex(-1, gen_0);
     168             :   }
     169             : }
     170             : /* 0 <= a <= b. Loop from -b, ... -a through squarefree integers */
     171             : static void
     172          14 : forsquarefreeneg(ulong a, ulong b, GEN code)
     173             : {
     174          14 :   const ulong step = 1024;
     175          14 :   pari_sp av = avma;
     176             :   ulong x2;
     177        6839 :   for(x2 = b;; x2 -= step, set_avma(av))
     178        6825 :   { /* beware overflow, fuse last two bins (avoid a tiny remainder) */
     179        6839 :     ulong j, x1 = (x2 >= 2*step && x2-2*step >= a)? x2+1 - step: a;
     180        6839 :     GEN v = vecfactorsquarefreeu(x1, x2);
     181     7006916 :     for (j = lg(v)-1; j > 0; j--) if (gel(v,j))
     182             :     {
     183     4255531 :       ulong n = x1-1 + j;
     184     4255531 :       set_lex(-1, mkvec2(utoineg(n), zv_to_mZM(gel(v,j))));
     185     4255531 :       closure_evalvoid(code); if (loop_break()) return;
     186             :     }
     187        6839 :     if (x1 == a) break;
     188        6825 :     set_lex(-1, gen_0);
     189             :   }
     190             : }
     191             : void
     192          28 : forsquarefree(GEN a, GEN b, GEN code)
     193             : {
     194          28 :   pari_sp av = avma;
     195             :   long s;
     196          28 :   if (typ(a) != t_INT) pari_err_TYPE("forsquarefree", a);
     197          28 :   if (typ(b) != t_INT) pari_err_TYPE("forsquarefree", b);
     198          28 :   if (cmpii(a,b) > 0) return;
     199          28 :   s = signe(a);
     200          28 :   if (s * signe(b) < 0) pari_err_TYPE("forsquarefree [!= signs]", mkvec2(a,b));
     201          28 :   push_lex(NULL,code);
     202          28 :   if (s < 0) forsquarefreeneg(itou(b), itou(a), code);
     203          14 :   else       forsquarefreepos(itou(a), itou(b), code);
     204          28 :   pop_lex(1); set_avma(av);
     205             : }
     206             : 
     207             : /* convert factoru(n) to factor(-n); M pre-allocated factorization matrix
     208             :  * with (-1)^1 already set */
     209             : static void
     210     7001582 : Flm2negfact(GEN v, GEN M)
     211             : {
     212     7001582 :   GEN p = gel(v,1), e = gel(v,2), P = gel(M,1), E = gel(M,2);
     213     7001582 :   long i, l = lg(p);
     214    26980058 :   for (i = 1; i < l; i++)
     215             :   {
     216    19978476 :     gel(P,i+1) = utoipos(p[i]);
     217    19978476 :     gel(E,i+1) = utoipos(e[i]);
     218             :   }
     219     7001582 :   setlg(P,l+1);
     220     7001582 :   setlg(E,l+1);
     221     7001582 : }
     222             : /* 0 < a <= b, from -b to -a */
     223             : static int
     224          84 : forfactoredneg(ulong a, ulong b, GEN code)
     225             : {
     226          84 :   const ulong step = 1024;
     227             :   GEN P, E, M;
     228             :   pari_sp av;
     229             :   ulong x2;
     230             : 
     231          84 :   P = cgetg(18, t_COL); gel(P,1) = gen_m1;
     232          84 :   E = cgetg(18, t_COL); gel(E,1) = gen_1;
     233          84 :   M = mkmat2(P,E);
     234          84 :   av = avma;
     235        6909 :   for(x2 = b;; x2 -= step, set_avma(av))
     236        6825 :   { /* beware overflow, fuse last two bins (avoid a tiny remainder) */
     237        6909 :     ulong j, x1 = (x2 >= 2*step && x2-2*step >= a)? x2+1 - step: a;
     238        6909 :     GEN v = vecfactoru(x1, x2);
     239     7008470 :     for (j = lg(v)-1; j; j--)
     240             :     { /* run backward: from factor(x1..x2) to factor(-x2..-x1) */
     241     7001582 :       ulong n = x1-1 + j;
     242     7001582 :       Flm2negfact(gel(v,j), M);
     243     7001582 :       set_lex(-1, mkvec2(utoineg(n), M));
     244     7001582 :       closure_evalvoid(code); if (loop_break()) return 1;
     245             :     }
     246        6888 :     if (x1 == a) break;
     247        6825 :     set_lex(-1, gen_0);
     248             :   }
     249          63 :   return 0;
     250             : }
     251             : static int
     252          70 : eval0(GEN code)
     253             : {
     254          70 :   pari_sp av = avma;
     255          70 :   set_lex(-1, mkvec2(gen_0, mkmat2(mkcol(gen_0),mkcol(gen_1))));
     256          70 :   closure_evalvoid(code); set_avma(av);
     257          70 :   return loop_break();
     258             : }
     259             : void
     260         133 : forfactored(GEN a, GEN b, GEN code)
     261             : {
     262         133 :   pari_sp av = avma;
     263         133 :   long sa, sb, stop = 0;
     264         133 :   if (typ(a) != t_INT) pari_err_TYPE("forfactored", a);
     265         133 :   if (typ(b) != t_INT) pari_err_TYPE("forfactored", b);
     266         133 :   if (cmpii(a,b) > 0) return;
     267         126 :   push_lex(NULL,code);
     268         126 :   sa = signe(a);
     269         126 :   sb = signe(b);
     270         126 :   if (sa < 0)
     271             :   {
     272          84 :     stop = forfactoredneg((sb < 0)? b[2]: 1UL, itou(a), code);
     273          84 :     if (!stop && sb >= 0) stop = eval0(code);
     274          84 :     if (!stop && sb > 0) forfactoredpos(1UL, b[2], code);
     275             :   }
     276             :   else
     277             :   {
     278          42 :     if (!sa) stop = eval0(code);
     279          42 :     if (!stop && sb) forfactoredpos(sa? a[2]: 1UL, itou(b), code);
     280             :   }
     281         126 :   pop_lex(1); set_avma(av);
     282             : }
     283             : void
     284     1793680 : whilepari(GEN a, GEN b)
     285             : {
     286     1793680 :   pari_sp av = avma;
     287             :   for(;;)
     288    16907156 :   {
     289    18700836 :     GEN res = closure_evalnobrk(a);
     290    18700836 :     if (gequal0(res)) break;
     291    16907156 :     set_avma(av);
     292    16907156 :     closure_evalvoid(b); if (loop_break()) break;
     293             :   }
     294     1793680 :   set_avma(av);
     295     1793680 : }
     296             : 
     297             : void
     298      222070 : untilpari(GEN a, GEN b)
     299             : {
     300      222070 :   pari_sp av = avma;
     301             :   for(;;)
     302     1456677 :   {
     303             :     GEN res;
     304     1678747 :     closure_evalvoid(b); if (loop_break()) break;
     305     1678747 :     res = closure_evalnobrk(a);
     306     1678747 :     if (!gequal0(res)) break;
     307     1456677 :     set_avma(av);
     308             :   }
     309      222070 :   set_avma(av);
     310      222070 : }
     311             : 
     312          28 : static int negcmp(GEN x, GEN y) { return gcmp(y,x); }
     313             : 
     314             : void
     315        1491 : forstep(GEN a, GEN b, GEN s, GEN code)
     316             : {
     317             :   long ss, i;
     318        1491 :   pari_sp av, av0 = avma;
     319        1491 :   GEN v = NULL;
     320             :   int (*cmp)(GEN,GEN);
     321             : 
     322        1491 :   b = gcopy(b);
     323        1491 :   s = gcopy(s); av = avma;
     324        1491 :   switch(typ(s))
     325             :   {
     326           7 :     case t_VEC: case t_COL: ss = gsigne(vecsum(s)); v = s; break;
     327           7 :     case t_INTMOD: a = gadd(a, gmod(gsub(gel(s,2),a), gel(s,1)));
     328           7 :                    s = gel(s,1);
     329        1484 :     default: ss = gsigne(s);
     330             :   }
     331        1491 :   if (!ss) pari_err_DOMAIN("forstep","step","=",gen_0,s);
     332        1484 :   cmp = (ss > 0)? &gcmp: &negcmp;
     333        1484 :   i = 0;
     334        1484 :   push_lex(a,code);
     335       50519 :   while (cmp(a,b) <= 0)
     336             :   {
     337       47551 :     closure_evalvoid(code); if (loop_break()) break;
     338       47551 :     if (v)
     339             :     {
     340          42 :       if (++i >= lg(v)) i = 1;
     341          42 :       s = gel(v,i);
     342             :     }
     343       47551 :     a = get_lex(-1); a = gadd(a,s);
     344             : 
     345       47551 :     if (gc_needed(av,1))
     346             :     {
     347           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"forstep");
     348           0 :       a = gerepileupto(av,a);
     349             :     }
     350       47551 :     set_lex(-1,a);
     351             :   }
     352        1484 :   pop_lex(1); set_avma(av0);
     353        1484 : }
     354             : 
     355             : static void
     356          14 : _fordiv(GEN a, GEN code, GEN (*D)(GEN))
     357             : {
     358             :   long i, l;
     359          14 :   pari_sp av2, av = avma;
     360          14 :   GEN t = D(a);
     361          14 :   push_lex(gen_0,code); l=lg(t); av2 = avma;
     362         105 :   for (i=1; i<l; i++)
     363             :   {
     364          91 :     set_lex(-1,gel(t,i));
     365          91 :     closure_evalvoid(code); if (loop_break()) break;
     366          91 :     set_avma(av2);
     367             :   }
     368          14 :   pop_lex(1); set_avma(av);
     369          14 : }
     370             : void
     371           7 : fordiv(GEN a, GEN code) { return _fordiv(a, code, &divisors); }
     372             : void
     373           7 : fordivfactored(GEN a, GEN code) { return _fordiv(a, code, &divisors_factored); }
     374             : 
     375             : /* Embedded for loops:
     376             :  *   fl = 0: execute ch (a), where a = (ai) runs through all n-uplets in
     377             :  *     [m1,M1] x ... x [mn,Mn]
     378             :  *   fl = 1: impose a1 <= ... <= an
     379             :  *   fl = 2:        a1 <  ... <  an
     380             :  */
     381             : /* increment and return d->a [over integers]*/
     382             : static GEN
     383      181432 : _next_i(forvec_t *d)
     384             : {
     385      181432 :   long i = d->n;
     386      181432 :   if (d->first) { d->first = 0; return (GEN)d->a; }
     387             :   for (;;) {
     388      284818 :     if (cmpii(d->a[i], d->M[i]) < 0) {
     389      181204 :       d->a[i] = incloop(d->a[i]);
     390      181204 :       return (GEN)d->a;
     391             :     }
     392       51864 :     d->a[i] = resetloop(d->a[i], d->m[i]);
     393       51864 :     if (--i <= 0) return NULL;
     394             :   }
     395             : }
     396             : /* increment and return d->a [generic]*/
     397             : static GEN
     398          63 : _next(forvec_t *d)
     399             : {
     400          63 :   long i = d->n;
     401          63 :   if (d->first) { d->first = 0; return (GEN)d->a; }
     402             :   for (;;) {
     403         140 :     d->a[i] = gaddgs(d->a[i], 1);
     404          98 :     if (gcmp(d->a[i], d->M[i]) <= 0) return (GEN)d->a;
     405          49 :     d->a[i] = d->m[i];
     406          49 :     if (--i <= 0) return NULL;
     407             :   }
     408             : }
     409             : 
     410             : /* non-decreasing order [over integers] */
     411             : static GEN
     412         113 : _next_le_i(forvec_t *d)
     413             : {
     414         113 :   long i = d->n;
     415         113 :   if (d->first) { d->first = 0; return (GEN)d->a; }
     416             :   for (;;) {
     417         253 :     if (cmpii(d->a[i], d->M[i]) < 0)
     418             :     {
     419          81 :       d->a[i] = incloop(d->a[i]);
     420             :       /* m[i] < a[i] <= M[i] <= M[i+1] */
     421         217 :       while (i < d->n)
     422             :       {
     423             :         GEN t;
     424          55 :         i++;
     425          55 :         if (cmpii(d->a[i-1], d->a[i]) <= 0) continue;
     426             :         /* a[i] < a[i-1] <= M[i-1] <= M[i] */
     427          55 :         t = d->a[i-1]; if (cmpii(t, d->m[i]) < 0) t = d->m[i];
     428          55 :         d->a[i] = resetloop(d->a[i], t);/*a[i]:=max(a[i-1],m[i])*/
     429             :       }
     430          81 :       return (GEN)d->a;
     431             :     }
     432          94 :     d->a[i] = resetloop(d->a[i], d->m[i]);
     433          94 :     if (--i <= 0) return NULL;
     434             :   }
     435             : }
     436             : /* non-decreasing order [generic] */
     437             : static GEN
     438         154 : _next_le(forvec_t *d)
     439             : {
     440         154 :   long i = d->n;
     441         154 :   if (d->first) { d->first = 0; return (GEN)d->a; }
     442             :   for (;;) {
     443         392 :     d->a[i] = gaddgs(d->a[i], 1);
     444         266 :     if (gcmp(d->a[i], d->M[i]) <= 0)
     445             :     {
     446         350 :       while (i < d->n)
     447             :       {
     448             :         GEN c;
     449          98 :         i++;
     450          98 :         if (gcmp(d->a[i-1], d->a[i]) <= 0) continue;
     451             :         /* M[i] >= M[i-1] >= a[i-1] > a[i] */
     452          98 :         c = gceil(gsub(d->a[i-1], d->a[i]));
     453          98 :         d->a[i] = gadd(d->a[i], c);
     454             :         /* a[i-1] <= a[i] < M[i-1] + 1 => a[i] < M[i]+1 => a[i] <= M[i] */
     455             :       }
     456         126 :       return (GEN)d->a;
     457             :     }
     458         140 :     d->a[i] = d->m[i];
     459         140 :     if (--i <= 0) return NULL;
     460             :   }
     461             : }
     462             : /* strictly increasing order [over integers] */
     463             : static GEN
     464     1173502 : _next_lt_i(forvec_t *d)
     465             : {
     466     1173502 :   long i = d->n;
     467     1173502 :   if (d->first) { d->first = 0; return (GEN)d->a; }
     468             :   for (;;) {
     469     1413315 :     if (cmpii(d->a[i], d->M[i]) < 0)
     470             :     {
     471     1159904 :       d->a[i] = incloop(d->a[i]);
     472             :       /* m[i] < a[i] <= M[i] < M[i+1] */
     473     2436301 :       while (i < d->n)
     474             :       {
     475             :         pari_sp av;
     476             :         GEN t;
     477      116493 :         i++;
     478      116493 :         if (cmpii(d->a[i-1], d->a[i]) < 0) continue;
     479      116493 :         av = avma;
     480             :         /* M[i] > M[i-1] >= a[i-1] */
     481      116493 :         t = addiu(d->a[i-1],1); if (cmpii(t, d->m[i]) < 0) t = d->m[i];
     482      116493 :         d->a[i] = resetloop(d->a[i], t);/*a[i]:=max(a[i-1]+1,m[i]) <= M[i]*/
     483      116493 :         set_avma(av);
     484             :       }
     485     1159904 :       return (GEN)d->a;
     486             :     }
     487      130105 :     d->a[i] = resetloop(d->a[i], d->m[i]);
     488      130105 :     if (--i <= 0) return NULL;
     489             :   }
     490             : }
     491             : /* strictly increasing order [generic] */
     492             : static GEN
     493          84 : _next_lt(forvec_t *d)
     494             : {
     495          84 :   long i = d->n;
     496          84 :   if (d->first) { d->first = 0; return (GEN)d->a; }
     497             :   for (;;) {
     498         196 :     d->a[i] = gaddgs(d->a[i], 1);
     499         133 :     if (gcmp(d->a[i], d->M[i]) <= 0)
     500             :     {
     501         147 :       while (i < d->n)
     502             :       {
     503             :         GEN c;
     504          35 :         i++;
     505          35 :         if (gcmp(d->a[i-1], d->a[i]) < 0) continue;
     506             :         /* M[i] > M[i-1] >= a[i-1] >= a[i] */
     507          35 :         c = addiu(gfloor(gsub(d->a[i-1], d->a[i])), 1); /* > a[i-1] - a[i] */
     508          35 :         d->a[i] = gadd(d->a[i], c);
     509             :         /* a[i-1] < a[i] <= M[i-1] + 1 => a[i] < M[i]+1 => a[i] <= M[i] */
     510             :       }
     511          56 :       return (GEN)d->a;
     512             :     }
     513          77 :     d->a[i] = d->m[i];
     514          77 :     if (--i <= 0) return NULL;
     515             :   }
     516             : }
     517             : /* for forvec(v=[],) */
     518             : static GEN
     519          14 : _next_void(forvec_t *d)
     520             : {
     521          14 :   if (d->first) { d->first = 0; return (GEN)d->a; }
     522           7 :   return NULL;
     523             : }
     524             : 
     525             : /* Initialize minima (m) and maxima (M); guarantee M[i] - m[i] integer and
     526             :  *   if flag = 1: m[i-1] <= m[i] <= M[i] <= M[i+1]
     527             :  *   if flag = 2: m[i-1] <  m[i] <= M[i] <  M[i+1],
     528             :  * for all i */
     529             : int
     530        7006 : forvec_init(forvec_t *d, GEN x, long flag)
     531             : {
     532        7006 :   long i, tx = typ(x), l = lg(x), t = t_INT;
     533        7006 :   if (!is_vec_t(tx)) pari_err_TYPE("forvec [not a vector]", x);
     534        7006 :   d->first = 1;
     535        7006 :   d->n = l - 1;
     536        7006 :   d->a = (GEN*)cgetg(l,tx);
     537        7006 :   d->m = (GEN*)cgetg(l,tx);
     538        7006 :   d->M = (GEN*)cgetg(l,tx);
     539        7006 :   if (l == 1) { d->next = &_next_void; return 1; }
     540       21249 :   for (i = 1; i < l; i++)
     541             :   {
     542       14278 :     GEN a, e = gel(x,i), m = gel(e,1), M = gel(e,2);
     543       14278 :     tx = typ(e);
     544       14278 :     if (! is_vec_t(tx) || lg(e)!=3)
     545          14 :       pari_err_TYPE("forvec [expected vector not of type [min,MAX]]",e);
     546       14264 :     if (typ(m) != t_INT) t = t_REAL;
     547       14264 :     if (i > 1) switch(flag)
     548             :     {
     549             :       case 1: /* a >= m[i-1] - m */
     550          51 :         a = gceil(gsub(d->m[i-1], m));
     551          51 :         if (typ(a) != t_INT) pari_err_TYPE("forvec",a);
     552          51 :         if (signe(a) > 0) m = gadd(m, a); else m = gcopy(m);
     553          51 :         break;
     554             :       case 2: /* a > m[i-1] - m */
     555        6848 :         a = gfloor(gsub(d->m[i-1], m));
     556        6848 :         if (typ(a) != t_INT) pari_err_TYPE("forvec",a);
     557        6848 :         a = addiu(a, 1);
     558        6848 :         if (signe(a) > 0) m = gadd(m, a); else m = gcopy(m);
     559        6848 :         break;
     560         380 :       default: m = gcopy(m);
     561         380 :         break;
     562             :     }
     563       14264 :     M = gadd(m, gfloor(gsub(M,m))); /* ensure M-m is an integer */
     564       14257 :     if (gcmp(m,M) > 0) { d->a = NULL; d->next = &_next; return 0; }
     565       14250 :     d->m[i] = m;
     566       14250 :     d->M[i] = M;
     567             :   }
     568        7022 :   if (flag == 1) for (i = l-2; i >= 1; i--)
     569             :   {
     570          51 :     GEN M = d->M[i], a = gfloor(gsub(d->M[i+1], M));
     571          51 :     if (typ(a) != t_INT) pari_err_TYPE("forvec",a);
     572             :     /* M[i]+a <= M[i+1] */
     573          51 :     if (signe(a) < 0) d->M[i] = gadd(M, a);
     574             :   }
     575       13782 :   else if (flag == 2) for (i = l-2; i >= 1; i--)
     576             :   {
     577        6841 :     GEN M = d->M[i], a = gceil(gsub(d->M[i+1], M));
     578        6841 :     if (typ(a) != t_INT) pari_err_TYPE("forvec",a);
     579        6841 :     a = subiu(a, 1);
     580             :     /* M[i]+a < M[i+1] */
     581        6841 :     if (signe(a) < 0) d->M[i] = gadd(M, a);
     582             :   }
     583        6971 :   if (t == t_INT) {
     584       21074 :     for (i = 1; i < l; i++) {
     585       14138 :       d->a[i] = setloop(d->m[i]);
     586       14138 :       if (typ(d->M[i]) != t_INT) d->M[i] = gfloor(d->M[i]);
     587             :     }
     588             :   } else {
     589          35 :     for (i = 1; i < l; i++) d->a[i] = d->m[i];
     590             :   }
     591        6971 :   switch(flag)
     592             :   {
     593         121 :     case 0: d->next = t==t_INT? &_next_i:    &_next; break;
     594          30 :     case 1: d->next = t==t_INT? &_next_le_i: &_next_le; break;
     595        6813 :     case 2: d->next = t==t_INT? &_next_lt_i: &_next_lt; break;
     596           7 :     default: pari_err_FLAG("forvec");
     597             :   }
     598        6964 :   return 1;
     599             : }
     600             : GEN
     601     1355362 : forvec_next(forvec_t *d) { return d->next(d); }
     602             : 
     603             : void
     604        7000 : forvec(GEN x, GEN code, long flag)
     605             : {
     606        7000 :   pari_sp av = avma;
     607             :   forvec_t T;
     608             :   GEN v;
     609        7000 :   if (!forvec_init(&T, x, flag)) { set_avma(av); return; }
     610        6965 :   push_lex((GEN)T.a, code);
     611        6965 :   while ((v = forvec_next(&T)))
     612             :   {
     613     1348333 :     closure_evalvoid(code);
     614     1348333 :     if (loop_break()) break;
     615             :   }
     616        6965 :   pop_lex(1); set_avma(av);
     617             : }
     618             : 
     619             : /********************************************************************/
     620             : /**                                                                **/
     621             : /**                              SUMS                              **/
     622             : /**                                                                **/
     623             : /********************************************************************/
     624             : 
     625             : GEN
     626       70161 : somme(GEN a, GEN b, GEN code, GEN x)
     627             : {
     628       70161 :   pari_sp av, av0 = avma;
     629             :   GEN p1;
     630             : 
     631       70161 :   if (typ(a) != t_INT) pari_err_TYPE("sum",a);
     632       70161 :   if (!x) x = gen_0;
     633       70161 :   if (gcmp(b,a) < 0) return gcopy(x);
     634             : 
     635       70161 :   b = gfloor(b);
     636       70161 :   a = setloop(a);
     637       70161 :   av=avma;
     638       70161 :   push_lex(a,code);
     639             :   for(;;)
     640             :   {
     641     3669071 :     p1 = closure_evalnobrk(code);
     642     1869616 :     x=gadd(x,p1); if (cmpii(a,b) >= 0) break;
     643     1799455 :     a = incloop(a);
     644     1799455 :     if (gc_needed(av,1))
     645             :     {
     646           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"sum");
     647           0 :       x = gerepileupto(av,x);
     648             :     }
     649     1799455 :     set_lex(-1,a);
     650             :   }
     651       70161 :   pop_lex(1); return gerepileupto(av0,x);
     652             : }
     653             : 
     654             : static GEN
     655          21 : sum_init(GEN x0, GEN t)
     656             : {
     657          21 :   long tp = typ(t);
     658             :   GEN x;
     659          21 :   if (is_vec_t(tp))
     660             :   {
     661           7 :     x = const_vec(lg(t)-1, x0);
     662           7 :     settyp(x, tp);
     663             :   }
     664             :   else
     665          14 :     x = x0;
     666          21 :   return x;
     667             : }
     668             : 
     669             : GEN
     670          21 : suminf(void *E, GEN (*eval)(void *, GEN), GEN a, long prec)
     671             : {
     672          21 :   long fl = 0, G = prec2nbits(prec) + 5;
     673          21 :   pari_sp av0 = avma, av;
     674          21 :   GEN x = NULL, _1;
     675             : 
     676          21 :   if (typ(a) != t_INT) pari_err_TYPE("suminf",a);
     677          21 :   a = setloop(a);
     678          21 :   av = avma;
     679             :   for(;;)
     680       15358 :   {
     681       15379 :     GEN t = eval(E, a);
     682       15379 :     if (!x) _1 = x = sum_init(real_1(prec), t);
     683             : 
     684       15379 :     x = gadd(x,t);
     685       15379 :     if (!gequal0(t) && gexpo(t) > gexpo(x)-G)
     686       15316 :       fl = 0;
     687          63 :     else if (++fl == 3)
     688          21 :       break;
     689       15358 :     a = incloop(a);
     690       15358 :     if (gc_needed(av,1))
     691             :     {
     692           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"suminf");
     693           0 :       gerepileall(av,2, &x, &_1);
     694             :     }
     695             :   }
     696          21 :   return gerepileupto(av0, gsub(x, _1));
     697             : }
     698             : GEN
     699          21 : suminf0(GEN a, GEN code, long prec)
     700          21 : { EXPR_WRAP(code, suminf(EXPR_ARG, a, prec)); }
     701             : 
     702             : GEN
     703          49 : sumdivexpr(GEN num, GEN code)
     704             : {
     705          49 :   pari_sp av = avma;
     706          49 :   GEN y = gen_0, t = divisors(num);
     707          49 :   long i, l = lg(t);
     708             : 
     709          49 :   push_lex(gen_0, code);
     710        9289 :   for (i=1; i<l; i++)
     711             :   {
     712        9240 :     set_lex(-1,gel(t,i));
     713        9240 :     y = gadd(y, closure_evalnobrk(code));
     714             :   }
     715          49 :   pop_lex(1); return gerepileupto(av,y);
     716             : }
     717             : GEN
     718          42 : sumdivmultexpr(GEN num, GEN code)
     719             : {
     720          42 :   pari_sp av = avma;
     721          42 :   GEN y = gen_1, P,E;
     722          42 :   int isint = divisors_init(num, &P,&E);
     723          42 :   long i, l = lg(P);
     724             :   GEN (*mul)(GEN,GEN);
     725             : 
     726          42 :   if (l == 1) { set_avma(av); return gen_1; }
     727          42 :   push_lex(gen_0, code);
     728          42 :   mul = isint? mulii: gmul;
     729         196 :   for (i=1; i<l; i++)
     730             :   {
     731         154 :     GEN p = gel(P,i), q = p, z = gen_1;
     732         154 :     long j, e = E[i];
     733         560 :     for (j = 1; j <= e; j++, q = mul(q, p))
     734             :     {
     735         560 :       set_lex(-1, q);
     736         560 :       z = gadd(z, closure_evalnobrk(code));
     737         560 :       if (j == e) break;
     738             :     }
     739         154 :     y = gmul(y, z);
     740             :   }
     741          42 :   pop_lex(1); return gerepileupto(av,y);
     742             : }
     743             : 
     744             : /********************************************************************/
     745             : /**                                                                **/
     746             : /**                           PRODUCTS                             **/
     747             : /**                                                                **/
     748             : /********************************************************************/
     749             : 
     750             : GEN
     751      120134 : produit(GEN a, GEN b, GEN code, GEN x)
     752             : {
     753      120134 :   pari_sp av, av0 = avma;
     754             :   GEN p1;
     755             : 
     756      120134 :   if (typ(a) != t_INT) pari_err_TYPE("prod",a);
     757      120134 :   if (!x) x = gen_1;
     758      120134 :   if (gcmp(b,a) < 0) return gcopy(x);
     759             : 
     760      115206 :   b = gfloor(b);
     761      115206 :   a = setloop(a);
     762      115206 :   av=avma;
     763      115206 :   push_lex(a,code);
     764             :   for(;;)
     765             :   {
     766      582330 :     p1 = closure_evalnobrk(code);
     767      348768 :     x = gmul(x,p1); if (cmpii(a,b) >= 0) break;
     768      233562 :     a = incloop(a);
     769      233562 :     if (gc_needed(av,1))
     770             :     {
     771           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"prod");
     772           0 :       x = gerepileupto(av,x);
     773             :     }
     774      233562 :     set_lex(-1,a);
     775             :   }
     776      115206 :   pop_lex(1); return gerepileupto(av0,x);
     777             : }
     778             : 
     779             : GEN
     780           7 : prodinf(void *E, GEN (*eval)(void *, GEN), GEN a, long prec)
     781             : {
     782           7 :   pari_sp av0 = avma, av;
     783             :   long fl,G;
     784           7 :   GEN p1,x = real_1(prec);
     785             : 
     786           7 :   if (typ(a) != t_INT) pari_err_TYPE("prodinf",a);
     787           7 :   a = setloop(a);
     788           7 :   av = avma;
     789           7 :   fl=0; G = -prec2nbits(prec)-5;
     790             :   for(;;)
     791             :   {
     792        1897 :     p1 = eval(E, a); if (gequal0(p1)) { x = p1; break; }
     793         952 :     x = gmul(x,p1); a = incloop(a);
     794         952 :     p1 = gsubgs(p1, 1);
     795         952 :     if (gequal0(p1) || gexpo(p1) <= G) { if (++fl==3) break; } else fl=0;
     796         945 :     if (gc_needed(av,1))
     797             :     {
     798           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"prodinf");
     799           0 :       x = gerepileupto(av,x);
     800             :     }
     801             :   }
     802           7 :   return gerepilecopy(av0,x);
     803             : }
     804             : GEN
     805           7 : prodinf1(void *E, GEN (*eval)(void *, GEN), GEN a, long prec)
     806             : {
     807           7 :   pari_sp av0 = avma, av;
     808             :   long fl,G;
     809           7 :   GEN p1,p2,x = real_1(prec);
     810             : 
     811           7 :   if (typ(a) != t_INT) pari_err_TYPE("prodinf1",a);
     812           7 :   a = setloop(a);
     813           7 :   av = avma;
     814           7 :   fl=0; G = -prec2nbits(prec)-5;
     815             :   for(;;)
     816             :   {
     817        1897 :     p2 = eval(E, a); p1 = gaddgs(p2,1);
     818         952 :     if (gequal0(p1)) { x = p1; break; }
     819         952 :     x = gmul(x,p1); a = incloop(a);
     820         952 :     if (gequal0(p2) || gexpo(p2) <= G) { if (++fl==3) break; } else fl=0;
     821         945 :     if (gc_needed(av,1))
     822             :     {
     823           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"prodinf1");
     824           0 :       x = gerepileupto(av,x);
     825             :     }
     826             :   }
     827           7 :   return gerepilecopy(av0,x);
     828             : }
     829             : GEN
     830          21 : prodinf0(GEN a, GEN code, long flag, long prec)
     831             : {
     832          21 :   switch(flag)
     833             :   {
     834           7 :     case 0: EXPR_WRAP(code, prodinf (EXPR_ARG, a, prec));
     835           7 :     case 1: EXPR_WRAP(code, prodinf1(EXPR_ARG, a, prec));
     836             :   }
     837           7 :   pari_err_FLAG("prodinf");
     838             :   return NULL; /* LCOV_EXCL_LINE */
     839             : }
     840             : 
     841             : GEN
     842           7 : prodeuler(void *E, GEN (*eval)(void *, GEN), GEN a, GEN b, long prec)
     843             : {
     844           7 :   pari_sp av, av0 = avma;
     845           7 :   GEN x = real_1(prec), prime;
     846             :   forprime_t T;
     847             : 
     848           7 :   av = avma;
     849           7 :   if (!forprime_init(&T, a,b)) { set_avma(av); return x; }
     850             : 
     851           7 :   av = avma;
     852        8617 :   while ( (prime = forprime_next(&T)) )
     853             :   {
     854        8603 :     x = gmul(x, eval(E, prime));
     855        8603 :     if (gc_needed(av,1))
     856             :     {
     857           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"prodeuler");
     858           0 :       x = gerepilecopy(av, x);
     859             :     }
     860             :   }
     861           7 :   return gerepilecopy(av0,x);
     862             : }
     863             : GEN
     864           7 : prodeuler0(GEN a, GEN b, GEN code, long prec)
     865           7 : { EXPR_WRAP(code, prodeuler(EXPR_ARG, a, b, prec)); }
     866             : GEN
     867         126 : direuler0(GEN a, GEN b, GEN code, GEN c)
     868         126 : { EXPR_WRAP(code, direuler(EXPR_ARG, a, b, c)); }
     869             : 
     870             : /********************************************************************/
     871             : /**                                                                **/
     872             : /**                       VECTORS & MATRICES                       **/
     873             : /**                                                                **/
     874             : /********************************************************************/
     875             : 
     876             : INLINE GEN
     877     2354687 : copyupto(GEN z, GEN t)
     878             : {
     879     2354687 :   if (is_universal_constant(z) || (z>(GEN)pari_mainstack->bot && z<=t))
     880     2354680 :     return z;
     881             :   else
     882           7 :     return gcopy(z);
     883             : }
     884             : 
     885             : GEN
     886      115122 : vecexpr0(GEN vec, GEN code, GEN pred)
     887             : {
     888      115122 :   switch(typ(vec))
     889             :   {
     890             :     case t_LIST:
     891             :     {
     892          21 :       if (list_typ(vec)==t_LIST_MAP)
     893           7 :         vec = mapdomain_shallow(vec);
     894             :       else
     895          14 :         vec = list_data(vec);
     896          21 :       if (!vec) return cgetg(1, t_VEC);
     897          14 :       break;
     898             :     }
     899             :     case t_VECSMALL:
     900           7 :       vec = vecsmall_to_vec(vec);
     901           7 :       break;
     902      115094 :     case t_VEC: case t_COL: case t_MAT: break;
     903           0 :     default: pari_err_TYPE("[_|_<-_,_]",vec);
     904             :   }
     905      115115 :   if (pred && code)
     906         343 :     EXPR_WRAP(code,vecselapply((void*)pred,&gp_evalbool,EXPR_ARGUPTO,vec))
     907      114772 :   else if (code)
     908      114772 :     EXPR_WRAP(code,vecapply(EXPR_ARGUPTO,vec))
     909             :   else
     910           0 :     EXPR_WRAP(pred,vecselect(EXPR_ARGBOOL,vec))
     911             : }
     912             : 
     913             : GEN
     914         175 : vecexpr1(GEN vec, GEN code, GEN pred)
     915             : {
     916         175 :   GEN v = vecexpr0(vec, code, pred);
     917         175 :   return lg(v) == 1? v: shallowconcat1(v);
     918             : }
     919             : 
     920             : GEN
     921     2354597 : vecteur(GEN nmax, GEN code)
     922             : {
     923             :   GEN y, c;
     924     2354597 :   long i, m = gtos(nmax);
     925             : 
     926     2354597 :   if (m < 0)  pari_err_DOMAIN("vector", "dimension", "<", gen_0, stoi(m));
     927     2354583 :   if (!code) return zerovec(m);
     928        8526 :   c = cgetipos(3); /* left on stack */
     929        8526 :   y = cgetg(m+1,t_VEC); push_lex(c, code);
     930      569575 :   for (i=1; i<=m; i++)
     931             :   {
     932      561063 :     c[2] = i;
     933      561063 :     gel(y,i) = copyupto(closure_evalnobrk(code), y);
     934      561049 :     set_lex(-1,c);
     935             :   }
     936        8512 :   pop_lex(1); return y;
     937             : }
     938             : 
     939             : GEN
     940         763 : vecteursmall(GEN nmax, GEN code)
     941             : {
     942             :   pari_sp av;
     943             :   GEN y, c;
     944         763 :   long i, m = gtos(nmax);
     945             : 
     946         763 :   if (m < 0)  pari_err_DOMAIN("vectorsmall", "dimension", "<", gen_0, stoi(m));
     947         756 :   if (!code) return zero_zv(m);
     948         735 :   c = cgetipos(3); /* left on stack */
     949         735 :   y = cgetg(m+1,t_VECSMALL); push_lex(c,code);
     950         735 :   av = avma;
     951       15974 :   for (i = 1; i <= m; i++)
     952             :   {
     953       15246 :     c[2] = i;
     954       15246 :     y[i] = gtos(closure_evalnobrk(code));
     955       15239 :     set_avma(av);
     956       15239 :     set_lex(-1,c);
     957             :   }
     958         728 :   pop_lex(1); return y;
     959             : }
     960             : 
     961             : GEN
     962         490 : vvecteur(GEN nmax, GEN n)
     963             : {
     964         490 :   GEN y = vecteur(nmax,n);
     965         483 :   settyp(y,t_COL); return y;
     966             : }
     967             : 
     968             : GEN
     969      138271 : matrice(GEN nlig, GEN ncol, GEN code)
     970             : {
     971             :   GEN c1, c2, y;
     972             :   long i, m, n;
     973             : 
     974      138271 :   n = gtos(nlig);
     975      138271 :   m = ncol? gtos(ncol): n;
     976      138271 :   if (m < 0)  pari_err_DOMAIN("matrix", "nbcols", "<", gen_0, stoi(m));
     977      138264 :   if (n < 0)  pari_err_DOMAIN("matrix", "nbrows", "<", gen_0, stoi(n));
     978      138257 :   if (!m) return cgetg(1,t_MAT);
     979      138187 :   if (!code || !n) return zeromatcopy(n, m);
     980      136031 :   c1 = cgetipos(3); push_lex(c1,code);
     981      136031 :   c2 = cgetipos(3); push_lex(c2,NULL); /* c1,c2 left on stack */
     982      136031 :   y = cgetg(m+1,t_MAT);
     983      526750 :   for (i = 1; i <= m; i++)
     984             :   {
     985      390719 :     GEN z = cgetg(n+1,t_COL);
     986             :     long j;
     987      390719 :     c2[2] = i; gel(y,i) = z;
     988     2184350 :     for (j = 1; j <= n; j++)
     989             :     {
     990     1793631 :       c1[2] = j;
     991     1793631 :       gel(z,j) = copyupto(closure_evalnobrk(code), y);
     992     1793631 :       set_lex(-2,c1);
     993     1793631 :       set_lex(-1,c2);
     994             :     }
     995             :   }
     996      136031 :   pop_lex(2); return y;
     997             : }
     998             : 
     999             : /********************************************************************/
    1000             : /**                                                                **/
    1001             : /**                         SUMMING SERIES                         **/
    1002             : /**                                                                **/
    1003             : /********************************************************************/
    1004             : /* h = (2+2x)g'- g; g has t_INT coeffs */
    1005             : static GEN
    1006        1246 : delt(GEN g, long n)
    1007             : {
    1008        1246 :   GEN h = cgetg(n+3,t_POL);
    1009             :   long k;
    1010        1246 :   h[1] = g[1];
    1011        1246 :   gel(h,2) = gel(g,2);
    1012      359597 :   for (k=1; k<n; k++)
    1013      358351 :     gel(h,k+2) = addii(mului(k+k+1,gel(g,k+2)), mului(k<<1,gel(g,k+1)));
    1014        1246 :   gel(h,n+2) = mului(n<<1, gel(g,n+1)); return h;
    1015             : }
    1016             : 
    1017             : #ifdef _MSC_VER /* Bill Daly: work around a MSVC bug */
    1018             : #pragma optimize("g",off)
    1019             : #endif
    1020             : /* P = polzagier(n,m)(-X), unnormalized (P(0) != 1) */
    1021             : static GEN
    1022          49 : polzag1(long n, long m)
    1023             : {
    1024          49 :   const long d = n - m, d2 = d<<1, r = (m+1)>>1, D = (d+1)>>1;
    1025             :   long i, k;
    1026          49 :   pari_sp av = avma;
    1027             :   GEN g, T;
    1028             : 
    1029          49 :   if (d <= 0 || m < 0) return pol_0(0);
    1030          49 :   g = cgetg(d+2, t_POL);
    1031          49 :   g[1] = evalsigne(1)|evalvarn(0);
    1032          49 :   T = cgetg(d+1,t_VEC);
    1033             :   /* T[k+1] = binomial(2d,2k+1), 0 <= k < d */
    1034          49 :   gel(T,1) = utoipos(d2);
    1035        1267 :   for (k = 1; k < D; k++)
    1036             :   {
    1037        1218 :     long k2 = k<<1;
    1038        2436 :     gel(T,k+1) = diviiexact(mulii(gel(T,k), muluu(d2-k2+1, d2-k2)),
    1039        1218 :                             muluu(k2,k2+1));
    1040             :   }
    1041          49 :   for (; k < d; k++) gel(T,k+1) = gel(T,d-k);
    1042          49 :   gel(g,2) = gel(T,d); /* binomial(2d, 2(d-1)+1) */
    1043        2499 :   for (i = 1; i < d; i++)
    1044             :   {
    1045        2450 :     pari_sp av2 = avma;
    1046        2450 :     GEN s, t = gel(T,d-i); /* binomial(2d, 2(d-1-i)+1) */
    1047        2450 :     s = t;
    1048      180082 :     for (k = d-i; k < d; k++)
    1049             :     {
    1050      177632 :       long k2 = k<<1;
    1051      177632 :       t = diviiexact(mulii(t, muluu(d2-k2+1, d-k)), muluu(k2+1,k-(d-i)+1));
    1052      177632 :       s = addii(s, t);
    1053             :     }
    1054             :     /* g_i = sum_{d-1-i <= k < d}, binomial(2*d, 2*k+1)*binomial(k,d-1-i) */
    1055        2450 :     gel(g,i+2) = gerepileuptoint(av2, s);
    1056             :   }
    1057             :   /* sum_{0 <= i < d} g_i x^i * (x+x^2)^r */
    1058          49 :   g = RgX_mulXn(gmul(g, gpowgs(deg1pol(gen_1,gen_1,0),r)), r);
    1059          49 :   if (!odd(m)) g = delt(g, n);
    1060        1274 :   for (i=1; i<=r; i++)
    1061             :   {
    1062        1225 :     g = delt(ZX_deriv(g), n);
    1063        1225 :     if (gc_needed(av,4))
    1064             :     {
    1065           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"polzag, i = %ld/%ld", i,r);
    1066           0 :       g = gerepilecopy(av, g);
    1067             :     }
    1068             :   }
    1069          49 :   return g;
    1070             : }
    1071             : GEN
    1072          28 : polzag(long n, long m)
    1073             : {
    1074          28 :   pari_sp av = avma;
    1075          28 :   GEN g = ZX_z_unscale(polzag1(n,m), -1);
    1076          28 :   return gerepileupto(av, RgX_Rg_div(g,gel(g,2)));
    1077             : }
    1078             : 
    1079             : GEN
    1080          14 : sumalt(void *E, GEN (*eval)(void *, GEN), GEN a, long prec)
    1081             : {
    1082             :   ulong k, N;
    1083          14 :   pari_sp av = avma, av2;
    1084             :   GEN s, az, c, d;
    1085             : 
    1086          14 :   if (typ(a) != t_INT) pari_err_TYPE("sumalt",a);
    1087          14 :   N = (ulong)(0.39322*(prec2nbits(prec) + 7)); /*0.39322 > 1/log_2(3+sqrt(8))*/
    1088          14 :   d = powru(addsr(3, sqrtr(utor(8,prec))), N);
    1089          14 :   d = shiftr(addrr(d, invr(d)),-1);
    1090          14 :   a = setloop(a);
    1091          14 :   az = gen_m1; c = d;
    1092          14 :   s = gen_0;
    1093          14 :   av2 = avma;
    1094         742 :   for (k=0; ; k++) /* k < N */
    1095             :   {
    1096        1470 :     c = addir(az,c); s = gadd(s, gmul(c, eval(E, a)));
    1097         742 :     if (k==N-1) break;
    1098         728 :     az = diviuuexact(muluui((N-k)<<1,N+k,az), k+1, (k<<1)+1);
    1099         728 :     a = incloop(a); /* in place! */
    1100         728 :     if (gc_needed(av,4))
    1101             :     {
    1102           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"sumalt, k = %ld/%ld", k,N-1);
    1103           0 :       gerepileall(av2, 3, &az,&c,&s);
    1104             :     }
    1105             :   }
    1106          14 :   return gerepileupto(av, gdiv(s,d));
    1107             : }
    1108             : 
    1109             : GEN
    1110           7 : sumalt2(void *E, GEN (*eval)(void *, GEN), GEN a, long prec)
    1111             : {
    1112             :   long k, N;
    1113           7 :   pari_sp av = avma, av2;
    1114             :   GEN s, dn, pol;
    1115             : 
    1116           7 :   if (typ(a) != t_INT) pari_err_TYPE("sumalt",a);
    1117           7 :   N = (long)(0.307073*(prec2nbits(prec) + 5)); /*0.307073 > 1/log_2(\beta_B)*/
    1118           7 :   pol = ZX_div_by_X_1(polzag1(N,N>>1), &dn);
    1119           7 :   a = setloop(a);
    1120           7 :   N = degpol(pol);
    1121           7 :   s = gen_0;
    1122           7 :   av2 = avma;
    1123         280 :   for (k=0; k<=N; k++)
    1124             :   {
    1125         280 :     GEN t = itor(gel(pol,k+2), prec+EXTRAPRECWORD);
    1126         280 :     s = gadd(s, gmul(t, eval(E, a)));
    1127         280 :     if (k == N) break;
    1128         273 :     a = incloop(a); /* in place! */
    1129         273 :     if (gc_needed(av,4))
    1130             :     {
    1131           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"sumalt2, k = %ld/%ld", k,N-1);
    1132           0 :       s = gerepileupto(av2, s);
    1133             :     }
    1134             :   }
    1135           7 :   return gerepileupto(av, gdiv(s,dn));
    1136             : }
    1137             : 
    1138             : GEN
    1139          21 : sumalt0(GEN a, GEN code, long flag, long prec)
    1140             : {
    1141          21 :   switch(flag)
    1142             :   {
    1143           7 :     case 0: EXPR_WRAP(code, sumalt (EXPR_ARG,a,prec));
    1144           7 :     case 1: EXPR_WRAP(code, sumalt2(EXPR_ARG,a,prec));
    1145           7 :     default: pari_err_FLAG("sumalt");
    1146             :   }
    1147             :   return NULL; /* LCOV_EXCL_LINE */
    1148             : }
    1149             : 
    1150             : /* For k > 0, set S[k*2^i] <- g(k*2^i), k*2^i <= N = #S.
    1151             :  * Only needed with k odd (but also works for g even). */
    1152             : static void
    1153        7406 : binsum(GEN S, ulong k, void *E, GEN (*f)(void *, GEN), GEN a,
    1154             :         long G, long prec)
    1155             : {
    1156        7406 :   long e, i, N = lg(S)-1, l = expu(N / k); /* k 2^l <= N < k 2^(l+1) */
    1157             :   pari_sp av;
    1158        7406 :   GEN r, t = gen_0;
    1159             : 
    1160        7406 :   gel(S, k << l) = cgetr(prec); av = avma;
    1161        7406 :   G -= l;
    1162        7406 :   r = utoipos(k<<l);
    1163     3964471 :   for(e=0;;e++) /* compute g(k 2^l) with absolute error ~ 2^(G-l) */
    1164     3957065 :   {
    1165     3964471 :     GEN u = gtofp(f(E, addii(a,r)), prec);
    1166     3964471 :     if (typ(u) != t_REAL) pari_err_TYPE("sumpos",u);
    1167     3964471 :     if (!signe(u)) break;
    1168     3964282 :     if (!e)
    1169        7217 :       t = u;
    1170             :     else {
    1171     3957065 :       shiftr_inplace(u, e);
    1172     3957065 :       t = addrr(t,u);
    1173     3957065 :       if (expo(u) < G) break;
    1174             :     }
    1175     3957065 :     r = shifti(r,1);
    1176             :   }
    1177        7406 :   gel(S, k << l) = t = gerepileuptoleaf(av, t);
    1178             :   /* g(j) = 2g(2j) + f(a+j) for all j > 0 */
    1179       14812 :   for(i = l-1; i >= 0; i--)
    1180             :   { /* t ~ g(2 * k*2^i) with error ~ 2^(G-i-1) */
    1181             :     GEN u;
    1182        7406 :     av = avma; u = gtofp(f(E, addiu(a, k << i)), prec);
    1183        7406 :     if (typ(u) != t_REAL) pari_err_TYPE("sumpos",u);
    1184        7406 :     t = addrr(gtofp(u,prec), mpshift(t,1)); /* ~ g(k*2^i) */
    1185        7406 :     gel(S, k << i) = t = gerepileuptoleaf(av, t);
    1186             :   }
    1187        7406 : }
    1188             : /* For k > 0, let g(k) := \sum_{e >= 0} 2^e f(a + k*2^e).
    1189             :  * Return [g(k), 1 <= k <= N] */
    1190             : static GEN
    1191          70 : sumpos_init(void *E, GEN (*f)(void *, GEN), GEN a, long N, long prec)
    1192             : {
    1193          70 :   GEN S = cgetg(N+1,t_VEC);
    1194          70 :   long k, G = -prec2nbits(prec) - 5;
    1195          70 :   for (k=1; k<=N; k+=2) binsum(S,k, E,f, a,G,prec);
    1196          70 :   return S;
    1197             : }
    1198             : 
    1199             : GEN
    1200          56 : sumpos(void *E, GEN (*eval)(void *, GEN), GEN a, long prec)
    1201             : {
    1202             :   ulong k, N;
    1203          56 :   pari_sp av = avma;
    1204             :   GEN s, az, c, d, S;
    1205             : 
    1206          56 :   if (typ(a) != t_INT) pari_err_TYPE("sumpos",a);
    1207          56 :   a = subiu(a,1);
    1208          56 :   N = (ulong)(0.4*(prec2nbits(prec) + 7));
    1209          56 :   if (odd(N)) N++; /* extra precision for free */
    1210          56 :   d = powru(addsr(3, sqrtr(utor(8,prec))), N);
    1211          56 :   d = shiftr(addrr(d, invr(d)),-1);
    1212          56 :   az = gen_m1; c = d;
    1213             : 
    1214          56 :   S = sumpos_init(E, eval, a, N, prec);
    1215          56 :   s = gen_0;
    1216       10360 :   for (k=0; k<N; k++)
    1217             :   {
    1218             :     GEN t;
    1219       10360 :     c = addir(az,c);
    1220       10360 :     t = mulrr(gel(S,k+1), c);
    1221       10360 :     s = odd(k)? mpsub(s, t): mpadd(s, t);
    1222       10360 :     if (k == N-1) break;
    1223       10304 :     az = diviuuexact(muluui((N-k)<<1,N+k,az), k+1, (k<<1)+1);
    1224             :   }
    1225          56 :   return gerepileupto(av, gdiv(s,d));
    1226             : }
    1227             : 
    1228             : GEN
    1229          14 : sumpos2(void *E, GEN (*eval)(void *, GEN), GEN a, long prec)
    1230             : {
    1231             :   ulong k, N;
    1232          14 :   pari_sp av = avma;
    1233             :   GEN s, pol, dn, S;
    1234             : 
    1235          14 :   if (typ(a) != t_INT) pari_err_TYPE("sumpos2",a);
    1236          14 :   a = subiu(a,1);
    1237          14 :   N = (ulong)(0.31*(prec2nbits(prec) + 5));
    1238             : 
    1239          14 :   if (odd(N)) N++; /* extra precision for free */
    1240          14 :   S = sumpos_init(E, eval, a, N, prec);
    1241          14 :   pol = ZX_div_by_X_1(polzag1(N,N>>1), &dn);
    1242          14 :   s = gen_0;
    1243        4466 :   for (k=0; k<N; k++)
    1244             :   {
    1245        4452 :     GEN t = mulri(gel(S,k+1), gel(pol,k+2));
    1246        4452 :     s = odd(k)? mpsub(s,t): mpadd(s,t);
    1247             :   }
    1248          14 :   return gerepileupto(av, gdiv(s,dn));
    1249             : }
    1250             : 
    1251             : GEN
    1252          77 : sumpos0(GEN a, GEN code, long flag, long prec)
    1253             : {
    1254          77 :   switch(flag)
    1255             :   {
    1256          56 :     case 0: EXPR_WRAP(code, sumpos (EXPR_ARG,a,prec));
    1257          14 :     case 1: EXPR_WRAP(code, sumpos2(EXPR_ARG,a,prec));
    1258           7 :     default: pari_err_FLAG("sumpos");
    1259             :   }
    1260             :   return NULL; /* LCOV_EXCL_LINE */
    1261             : }
    1262             : 
    1263             : /********************************************************************/
    1264             : /**                                                                **/
    1265             : /**            SEARCH FOR REAL ZEROS of an expression              **/
    1266             : /**                                                                **/
    1267             : /********************************************************************/
    1268             : /* Brent's method, [a,b] bracketing interval */
    1269             : GEN
    1270         896 : zbrent(void *E, GEN (*eval)(void *, GEN), GEN a, GEN b, long prec)
    1271             : {
    1272             :   long sig, iter, itmax;
    1273         896 :   pari_sp av = avma;
    1274             :   GEN c, d, e, tol, fa, fb, fc;
    1275             : 
    1276         896 :   if (typ(a) != t_REAL || realprec(a) < prec) a = gtofp(a, prec);
    1277         896 :   if (typ(b) != t_REAL || realprec(b) < prec) b = gtofp(b, prec);
    1278         896 :   sig = cmprr(b, a);
    1279         896 :   if (!sig) return gerepileupto(av, a);
    1280         896 :   if (sig < 0) {c = a; a = b; b = c;} else c = b;
    1281         896 :   fa = eval(E, a);
    1282         896 :   fb = eval(E, b);
    1283         896 :   if (gsigne(fa)*gsigne(fb) > 0)
    1284           7 :     pari_err_DOMAIN("solve", "f(a)f(b)", ">", gen_0, mkvec2(fa, fb));
    1285         889 :   itmax = prec2nbits(prec) * 2 + 1;
    1286         889 :   tol = real2n(5-prec2nbits(prec), LOWDEFAULTPREC);
    1287         889 :   fc = fb;
    1288         889 :   e = d = NULL; /* gcc -Wall */
    1289       14510 :   for (iter = 1; iter <= itmax; ++iter)
    1290             :   {
    1291             :     GEN xm, tol1;
    1292       14510 :     if (gsigne(fb)*gsigne(fc) > 0)
    1293             :     {
    1294        7713 :       c = a; fc = fa; e = d = subrr(b, a);
    1295             :     }
    1296       14510 :     if (gcmp(gabs(fc, 0), gabs(fb, 0)) < 0)
    1297             :     {
    1298        3764 :       a = b; b = c; c = a; fa = fb; fb = fc; fc = fa;
    1299             :     }
    1300       14510 :     tol1 = abscmprr(tol, b) > 0? sqrr(tol): mulrr(tol, absr(b));
    1301       14510 :     xm = shiftr(subrr(c, b), -1);
    1302       14510 :     if (abscmprr(xm, tol1) <= 0 || gequal0(fb)) break; /* SUCCESS */
    1303             : 
    1304       13621 :     if (abscmprr(e, tol1) >= 0 && gcmp(gabs(fa, 0), gabs(fb, 0)) > 0)
    1305       12280 :     { /* attempt interpolation */
    1306       12280 :       GEN min1, min2, p, q, s = gdiv(fb, fa);
    1307       12280 :       if (cmprr(a, c) == 0)
    1308             :       {
    1309        7392 :         p = gmul2n(gmul(xm, s), 1);
    1310        7392 :         q = gsubsg(1, s);
    1311             :       }
    1312             :       else
    1313             :       {
    1314        4888 :         GEN r = gdiv(fb, fc);
    1315        4888 :         q = gdiv(fa, fc);
    1316        4888 :         p = gmul2n(gmul(gsub(q, r), gmul(xm, q)), 1);
    1317        4888 :         p = gmul(s, gsub(p, gmul(gsub(b, a), gsubgs(r, 1))));
    1318        4888 :         q = gmul(gmul(gsubgs(q, 1), gsubgs(r, 1)), gsubgs(s, 1));
    1319             :       }
    1320       12280 :       if (gsigne(p) > 0) q = gneg_i(q); else p = gneg_i(p);
    1321       12280 :       min1 = gsub(gmulsg(3, gmul(xm,q)), gabs(gmul(q, tol1), 0));
    1322       12280 :       min2 = gabs(gmul(e, q), 0);
    1323       12280 :       if (gcmp(gmul2n(p, 1), gmin_shallow(min1, min2)) < 0)
    1324       10337 :         { e = d; d = gdiv(p, q); } /* interpolation OK */
    1325             :       else
    1326        1943 :         { d = xm; e = d; } /* failed, use bisection */
    1327             :     }
    1328        1341 :     else { d = xm; e = d; } /* bound decreasing too slowly, use bisection */
    1329       13621 :     a = b; fa = fb;
    1330       13621 :     if (gcmp(gabs(d, 0), tol1) > 0) b = gadd(b, d);
    1331         851 :     else if (gsigne(xm) > 0)      b = addrr(b, tol1);
    1332         498 :     else                          b = subrr(b, tol1);
    1333       13621 :     if (realprec(b) < prec) b = rtor(b, prec);
    1334       13621 :     fb = eval(E, b);
    1335             :   }
    1336         889 :   if (iter > itmax) pari_err_IMPL("solve recovery [too many iterations]");
    1337         889 :   return gerepileuptoleaf(av, rcopy(b));
    1338             : }
    1339             : 
    1340             : GEN
    1341          21 : zbrent0(GEN a, GEN b, GEN code, long prec)
    1342          21 : { EXPR_WRAP(code, zbrent(EXPR_ARG, a, b, prec)); }
    1343             : 
    1344             : /* Find zeros of a function in the real interval [a,b] by interval splitting */
    1345             : GEN
    1346          98 : solvestep(void *E, GEN (*f)(void *,GEN), GEN a, GEN b, GEN step, long flag, long prec)
    1347             : {
    1348          98 :   const long ITMAX = 10;
    1349          98 :   pari_sp av = avma;
    1350             :   GEN fa, a0, b0;
    1351          98 :   long sa0, it, bit = prec2nbits(prec) / 2, ct = 0, s = gcmp(a,b);
    1352             : 
    1353          98 :   if (!s) return gequal0(f(E, a)) ? gcopy(mkvec(a)): cgetg(1,t_VEC);
    1354          98 :   if (s > 0) swap(a, b);
    1355          98 :   if (flag&4)
    1356             :   {
    1357          84 :     if (gcmpgs(step,1)<=0) pari_err_DOMAIN("solvestep","step","<=",gen_1,step);
    1358          84 :     if (gsigne(a) <= 0) pari_err_DOMAIN("solvestep","a","<=",gen_0,a);
    1359             :   }
    1360          14 :   else if (gsigne(step) <= 0)
    1361           7 :     pari_err_DOMAIN("solvestep","step","<=",gen_0,step);
    1362          91 :   a0 = a = gtofp(a, prec); fa = f(E, a);
    1363          91 :   b0 = b = gtofp(b, prec); step = gtofp(step, prec);
    1364          91 :   sa0 = gsigne(fa);
    1365          91 :   if (gexpo(fa) < -bit) sa0 = 0;
    1366          98 :   for (it = 0; it < ITMAX; it++)
    1367             :   {
    1368          98 :     pari_sp av2 = avma;
    1369          98 :     GEN v = cgetg(1, t_VEC);
    1370          98 :     long sa = sa0;
    1371          98 :     a = a0; b = b0;
    1372        2457 :     while (gcmp(a,b) < 0)
    1373             :     {
    1374        2261 :       GEN fc, c = (flag&4)? gmul(a, step): gadd(a, step);
    1375             :       long sc;
    1376        2261 :       if (gcmp(c,b) > 0) c = b;
    1377        2261 :       fc = f(E, c); sc = gsigne(fc);
    1378        2261 :       if (gexpo(fc) < -bit) sc = 0;
    1379        2261 :       if (!sc || sa*sc < 0)
    1380             :       {
    1381         441 :         GEN z = sc? zbrent(E, f, a, c, prec): c;
    1382             :         long e;
    1383         441 :         (void)grndtoi(z, &e);
    1384         441 :         if (e <= -bit) ct = 1;
    1385         441 :         if ((flag&1) && ((!(flag&8)) || ct)) return gerepileupto(av, z);
    1386         441 :         v = shallowconcat(v, z);
    1387             :       }
    1388        2261 :       a = c; fa = fc; sa = sc;
    1389             :     }
    1390          98 :     if ((!(flag&2) || lg(v) > 1) && (!(flag&8) || ct))
    1391          91 :       return gerepilecopy(av, v);
    1392           7 :     step = (flag&4)? sqrtr(sqrtr(step)): gmul2n(step, -2);
    1393           7 :     gerepileall(av2, 2, &fa, &step);
    1394             :   }
    1395           0 :   pari_err_IMPL("solvestep recovery [too many iterations]");
    1396             :   return NULL;/*LCOV_EXCL_LINE*/
    1397             : }
    1398             : 
    1399             : GEN
    1400          14 : solvestep0(GEN a, GEN b, GEN step, GEN code, long flag, long prec)
    1401          14 : { EXPR_WRAP(code, solvestep(EXPR_ARG, a,b, step, flag, prec)); }
    1402             : 
    1403             : /********************************************************************/
    1404             : /**                     Numerical derivation                       **/
    1405             : /********************************************************************/
    1406             : 
    1407             : struct deriv_data
    1408             : {
    1409             :   GEN code;
    1410             :   GEN args;
    1411             : };
    1412             : 
    1413         147 : static GEN deriv_eval(void *E, GEN x, long prec)
    1414             : {
    1415         147 :  struct deriv_data *data=(struct deriv_data *)E;
    1416         147 :  gel(data->args,1)=x;
    1417         147 :  return closure_callgenvecprec(data->code, data->args, prec);
    1418             : }
    1419             : 
    1420             : /* Rationale: (f(2^-e) - f(-2^-e) + O(2^-b)) / (2 * 2^-e) = f'(0) + O(2^-2e)
    1421             :  * since 2nd derivatives cancel.
    1422             :  *   prec(LHS) = b - e
    1423             :  *   prec(RHS) = 2e, equal when  b = 3e = 3/2 b0 (b0 = required final bitprec)
    1424             :  *
    1425             :  * For f'(x), x far from 0: prec(LHS) = b - e - expo(x)
    1426             :  * --> pr = 3/2 b0 + expo(x) */
    1427             : GEN
    1428        1029 : derivnum(void *E, GEN (*eval)(void *, GEN, long), GEN x, long prec)
    1429             : {
    1430        1029 :   long newprec, e, ex = maxss(0, gexpo(x)), p = precision(x);
    1431        1029 :   long b0 = prec2nbits(p ? p: prec), b = (long)ceil(b0 * 1.5 + ex);
    1432             :   GEN eps, u, v, y;
    1433        1029 :   pari_sp av = avma;
    1434        1029 :   newprec = nbits2prec(b + BITS_IN_LONG);
    1435        1029 :   switch(typ(x))
    1436             :   {
    1437             :     case t_REAL:
    1438             :     case t_COMPLEX:
    1439         427 :       x = gprec_w(x, newprec);
    1440             :   }
    1441        1029 :   e = b0/2; /* 1/2 required prec (in sig. bits) */
    1442        1029 :   eps = real2n(-e, nbits2prec(b-e));
    1443        1029 :   u = eval(E, gsub(x, eps), newprec);
    1444        1029 :   v = eval(E, gadd(x, eps), newprec);
    1445        1029 :   y = gmul2n(gsub(v,u), e-1);
    1446        1029 :   return gerepilecopy(av, gprec_w(y, nbits2prec(b0)));
    1447             : }
    1448             : 
    1449             : /* Fornberg interpolation algorithm for finite differences coefficients
    1450             : * using N+1 equidistant grid points around 0 [ assume N even >= M ].
    1451             : * Compute \delta[m]_{N,nu} for all derivation orders m = 0..M such that
    1452             : *   h^m * f^{(m)}(0) = \sum_{nu = 0}^n delta[m]_{N,nu}  f(a_nu) + O(h^{N-m+1}),
    1453             : * for step size h.
    1454             : * Return a = [0,-1,1...,-N2,N2] and vector of vectors d: d[m+1][nu+1]
    1455             : * = (M!/m!) * delta[m]_{N,nu}, nu = 0..N */
    1456             : static void
    1457          42 : FD(long M, long N, GEN *pd, GEN *pa)
    1458             : {
    1459             :   GEN d, a, b, W, Wp, t, F, Mfact;
    1460             :   long N2, m, nu, i;
    1461             : 
    1462          42 :   if (odd(N)) N++; /* make it even */
    1463          42 :   N2 = N>>1;
    1464          42 :   F = cgetg(N+2, t_VEC);
    1465          42 :   a = cgetg(N+2, t_VEC);
    1466          42 :   b = cgetg(N2+1, t_VEC);
    1467          42 :   gel(a,1) = gen_0;
    1468         413 :   for (i = 1; i <= N2; i++)
    1469             :   {
    1470         371 :     gel(a,2*i)   = utoineg(i);
    1471         371 :     gel(a,2*i+1) = utoipos(i);
    1472         371 :     gel(b,i) = sqru(i);
    1473             :   }
    1474             :   /* w = \prod (X - a[i]) = x W(x^2) */
    1475          42 :   Mfact = mpfact(M);
    1476          42 :   W = roots_to_pol(b, 0);
    1477          42 :   Wp = ZX_deriv(W);
    1478          42 :   t = gel(W,2); /* w'(0) */
    1479          42 :   t = diviiexact(t, Mfact);
    1480          42 :   gel(F,1) = RgX_Rg_div(RgX_inflate(W,2), t);
    1481         413 :   for (i = 1; i <= N2; i++)
    1482             :   { /* t = w'(a_{2i}) = w'(a_{2i+1}) */
    1483         371 :     GEN r, t = mulii(shifti(gel(b,i),1), poleval(Wp, gel(b,i)));
    1484             :     GEN U, S, T;
    1485         371 :     U = RgX_inflate(RgX_div_by_X_x(W, gel(b,i), &r), 2);
    1486         371 :     U = RgX_shift_shallow(U, 1);
    1487         371 :     U = RgXn_red_shallow(U, M+1); /* higher terms not needed */
    1488         371 :     t = diviiexact(t, Mfact);
    1489         371 :     U = RgX_Rg_div(U, t);
    1490         371 :     S = RgX_shift_shallow(U,1);
    1491         371 :     T = RgX_Rg_mul(U, gel(a,2*i+1));
    1492         371 :     gel(F,2*i)   = RgX_sub(S, T);
    1493         371 :     gel(F,2*i+1) = RgX_add(S, T);
    1494             :   }
    1495             :   /* F[i] = M! w(X) / ((X-a[i])w'(a[i])) + O(X^(M+1)) */
    1496          42 :   d = cgetg(M+2, t_VEC);
    1497         336 :   for (m = 0; m <= M; m++)
    1498             :   {
    1499         294 :     GEN v = cgetg(N+2, t_VEC); /* coeff(F[nu],X^m) */
    1500         294 :     for (nu = 0; nu <= N; nu++) gel(v, nu+1) = gmael(F, nu+1, m+2);
    1501         294 :     gel(d,m+1) = v;
    1502             :   }
    1503          42 :   *pd = d;
    1504          42 :   *pa = a;
    1505          42 : }
    1506             : 
    1507             : static void
    1508         273 : chk_ord(long m)
    1509             : {
    1510         273 :   if (m < 0)
    1511          14 :     pari_err_DOMAIN("derivnumk", "derivation order", "<", gen_0, stoi(m));
    1512         259 : }
    1513             : 
    1514             : GEN
    1515          56 : derivnumk(void *E, GEN (*eval)(void *, GEN, long), GEN x, GEN ind0, long prec)
    1516             : {
    1517             :   GEN A, D, X, F, ind;
    1518             :   long M, fpr, p, i, pr, l, lA, e, ex, eD, newprec;
    1519          56 :   pari_sp av = avma;
    1520          56 :   int allodd = 1;
    1521             : 
    1522          56 :   ind = gtovecsmall(ind0);
    1523          56 :   l = lg(ind);
    1524          56 :   F = cgetg(l, t_VEC);
    1525          56 :   M = vecsmall_max(ind);
    1526          56 :   chk_ord(M);
    1527          56 :   if (!M) /* silly degenerate case */
    1528             :   {
    1529          14 :     X = eval(E, x, prec);
    1530          14 :     for (i = 1; i < l; i++) { chk_ord(ind[i]); gel(F,i) = X; }
    1531           7 :     if (typ(ind0) == t_INT) F = gel(F,1);
    1532           7 :     return gerepilecopy(av, F);
    1533             :   }
    1534          42 :   FD(M, 3*M-1, &D,&A); /* optimal if 'eval' uses quadratic time */
    1535             : 
    1536          42 :   p = precision(x);
    1537          42 :   fpr = p ? prec2nbits(p): prec2nbits(prec);
    1538          42 :   eD = gexpo(gel(D,M));
    1539          42 :   e = (fpr + 3*M*log2((double)M)) / (2*M);
    1540          42 :   ex = gexpo(x);
    1541          42 :   if (ex < 0) ex = 0; /* near 0 */
    1542          42 :   pr = (long)ceil(fpr + e * M); /* ~ 3fpr/2 */
    1543          42 :   newprec = nbits2prec(pr + eD + ex + BITS_IN_LONG);
    1544          42 :   switch(typ(x))
    1545             :   {
    1546             :     case t_REAL:
    1547             :     case t_COMPLEX:
    1548          14 :       x = gprec_w(x, newprec);
    1549             :   }
    1550          42 :   lA = lg(A); X = cgetg(lA, t_VEC);
    1551          56 :   for (i = 1; i < l; i++)
    1552          49 :     if (!odd(ind[i])) { allodd = 0; break; }
    1553             :   /* if only odd derivation orders, the value at 0 (A[1]) is not needed */
    1554          42 :   gel(X, 1) = gen_0;
    1555         819 :   for (i = allodd? 2: 1; i < lA; i++)
    1556             :   {
    1557         777 :     GEN t = eval(E, gadd(x, gmul2n(gel(A,i), -e)), newprec);
    1558         777 :     if (!gprecision(t)) t = gtofp(t, newprec);
    1559         777 :     gel(X, i) = t;
    1560             :   }
    1561             : 
    1562         175 :   for (i = 1; i < l; i++)
    1563             :   {
    1564             :     GEN t;
    1565         140 :     long m = ind[i]; chk_ord(m);
    1566         133 :     t = gmul2n(RgV_dotproduct(gel(D,m+1), X), e*m);
    1567         133 :     if (m < M) t = gdiv(t, mulu_interval(m+1,M));
    1568         133 :     gel(F,i) = t;
    1569             :   }
    1570          35 :   if (typ(ind0) == t_INT) F = gel(F,1);
    1571          35 :   return gerepilecopy(av, gprec_w(F, nbits2prec(fpr)));
    1572             : }
    1573             : /* v(t') */
    1574             : static long
    1575          14 : rfrac_val_deriv(GEN t)
    1576             : {
    1577          14 :   long v = varn(gel(t,2));
    1578          14 :   return gvaluation(deriv(t, v), pol_x(v));
    1579             : }
    1580             : 
    1581             : GEN
    1582        1064 : derivfunk(void *E, GEN (*eval)(void *, GEN, long), GEN x, GEN ind0, long prec)
    1583             : {
    1584             :   pari_sp av;
    1585             :   GEN ind, xp, ixp, F, G;
    1586             :   long i, l, vx, M;
    1587        1064 :   if (!ind0) return derivfun(E, eval, x, prec);
    1588          84 :   switch(typ(x))
    1589             :   {
    1590             :   case t_REAL: case t_INT: case t_FRAC: case t_COMPLEX:
    1591          56 :     return derivnumk(E,eval, x, ind0, prec);
    1592             :   case t_POL:
    1593          14 :     ind = gtovecsmall(ind0);
    1594          14 :     M = vecsmall_max(ind);
    1595          14 :     xp = RgX_deriv(x);
    1596          14 :     x = RgX_to_ser(x, precdl+2 + M * (1+RgX_val(xp)));
    1597          14 :     break;
    1598             :   case t_RFRAC:
    1599           7 :     ind = gtovecsmall(ind0);
    1600           7 :     M = vecsmall_max(ind);
    1601           7 :     x = rfrac_to_ser(x, precdl+2 + M * (1+rfrac_val_deriv(x)));
    1602           7 :     xp = derivser(x);
    1603           7 :     break;
    1604             :   case t_SER:
    1605           7 :     ind = gtovecsmall(ind0);
    1606           7 :     M = vecsmall_max(ind);
    1607           7 :     xp = derivser(x);
    1608           7 :     break;
    1609           0 :   default: pari_err_TYPE("numerical derivation",x);
    1610             :     return NULL; /*LCOV_EXCL_LINE*/
    1611             :   }
    1612          28 :   av = avma; chk_ord(M);
    1613          28 :   vx = varn(x);
    1614          28 :   ixp = M? ginv(xp): NULL;
    1615          28 :   F = cgetg(M+2, t_VEC);
    1616          28 :   gel(F,1) = eval(E, x, prec);
    1617          28 :   for (i = 1; i <= M; i++) gel(F,i+1) = gmul(deriv(gel(F,i),vx), ixp);
    1618          28 :   l = lg(ind); G = cgetg(l, t_VEC);
    1619          56 :   for (i = 1; i < l; i++)
    1620             :   {
    1621          28 :     long m = ind[i]; chk_ord(m);
    1622          28 :     gel(G,i) = gel(F,m+1);
    1623             :   }
    1624          28 :   if (typ(ind0) == t_INT) G = gel(G,1);
    1625          28 :   return gerepilecopy(av, G);
    1626             : }
    1627             : 
    1628             : GEN
    1629        1064 : derivfun(void *E, GEN (*eval)(void *, GEN, long), GEN x, long prec)
    1630             : {
    1631        1064 :   pari_sp av = avma;
    1632             :   GEN xp;
    1633             :   long vx;
    1634        1064 :   switch(typ(x))
    1635             :   {
    1636             :   case t_REAL: case t_INT: case t_FRAC: case t_COMPLEX:
    1637        1029 :     return derivnum(E,eval, x, prec);
    1638             :   case t_POL:
    1639          14 :     xp = RgX_deriv(x);
    1640          14 :     x = RgX_to_ser(x, precdl+2+ (1 + RgX_val(xp)));
    1641          14 :     break;
    1642             :   case t_RFRAC:
    1643           7 :     x = rfrac_to_ser(x, precdl+2+ (1 + rfrac_val_deriv(x)));
    1644             :     /* fall through */
    1645             :   case t_SER:
    1646          14 :     xp = derivser(x);
    1647          14 :     break;
    1648           7 :   default: pari_err_TYPE("formal derivation",x);
    1649             :     return NULL; /*LCOV_EXCL_LINE*/
    1650             :   }
    1651          28 :   vx = varn(x);
    1652          28 :   return gerepileupto(av, gdiv(deriv(eval(E, x, prec),vx), xp));
    1653             : }
    1654             : 
    1655             : GEN
    1656          21 : laurentseries(void *E, GEN (*f)(void*,GEN x, long), long M, long v, long prec)
    1657             : {
    1658          21 :   pari_sp av = avma;
    1659             :   long d;
    1660             : 
    1661          21 :   if (v < 0) v = 0;
    1662          21 :   d = maxss(M+1,1);
    1663             :   for (;;)
    1664          14 :   {
    1665             :     long i, dr, vr;
    1666             :     GEN s;
    1667          35 :     s = cgetg(d+2, t_SER); s[1] = evalsigne(1) | evalvalp(1) | evalvarn(v);
    1668          35 :     gel(s, 2) = gen_1; for (i = 3; i <= d+1; i++) gel(s, i) = gen_0;
    1669          35 :     s = f(E, s, prec);
    1670          35 :     if (typ(s) != t_SER || varn(s) != v) pari_err_TYPE("laurentseries", s);
    1671          35 :     vr = valp(s);
    1672          35 :     if (M < vr) { set_avma(av); return zeroser(v, M); }
    1673          35 :     dr = lg(s) + vr - 3 - M;
    1674          35 :     if (dr >= 0) return gerepileupto(av, s);
    1675          14 :     set_avma(av); d -= dr;
    1676             :   }
    1677             : }
    1678             : static GEN
    1679          35 : _evalclosprec(void *E, GEN x, long prec)
    1680             : {
    1681             :   GEN s;
    1682          35 :   push_localprec(prec); s = closure_callgen1((GEN)E, x);
    1683          35 :   pop_localprec(); return s;
    1684             : }
    1685             : #define CLOS_ARGPREC __E, &_evalclosprec
    1686             : GEN
    1687          35 : laurentseries0(GEN f, long M, long v, long prec)
    1688             : {
    1689          35 :   if (typ(f) != t_CLOSURE || closure_arity(f) != 1 || closure_is_variadic(f))
    1690          14 :     pari_err_TYPE("laurentseries",f);
    1691          21 :   EXPR_WRAP(f, laurentseries(CLOS_ARGPREC,M,v,prec));
    1692             : }
    1693             : 
    1694             : GEN
    1695        1064 : derivnum0(GEN a, GEN code, GEN ind, long prec)
    1696        1064 : { EXPR_WRAP(code, derivfunk(EXPR_ARGPREC,a,ind,prec)); }
    1697             : 
    1698             : GEN
    1699          84 : derivfun0(GEN code, GEN args, long prec)
    1700             : {
    1701             :   struct deriv_data E;
    1702          84 :   E.code=code; E.args=args;
    1703          84 :   return derivfun((void*)&E, deriv_eval, gel(args,1), prec);
    1704             : }
    1705             : 
    1706             : /********************************************************************/
    1707             : /**                   Numerical extrapolation                      **/
    1708             : /********************************************************************/
    1709             : /* [u(n), u <= N] */
    1710             : static GEN
    1711         126 : get_u(void *E, GEN (*f)(void *, GEN, long), long N, long prec)
    1712             : {
    1713             :   long n;
    1714             :   GEN u;
    1715         126 :   if (f)
    1716             :   {
    1717         112 :     GEN v = f(E, utoipos(N), prec);
    1718         112 :     u = cgetg(N+1, t_VEC);
    1719         112 :     if (typ(v) != t_VEC || lg(v) != N+1) { gel(u,N) = v; v = NULL; }
    1720             :     else
    1721             :     {
    1722          14 :       GEN w = f(E, gen_1, LOWDEFAULTPREC);
    1723          14 :       if (typ(w) != t_VEC || lg(w) != 2) { gel(u,N) = v; v = NULL; }
    1724             :     }
    1725         112 :     if (v) u = v;
    1726             :     else
    1727          98 :       for (n = 1; n < N; n++) gel(u,n) = f(E, utoipos(n), prec);
    1728             :   }
    1729             :   else
    1730             :   {
    1731          14 :     GEN v = (GEN)E;
    1732          14 :     long t = lg(v)-1;
    1733          14 :     if (t < N) pari_err_COMPONENT("limitnum","<",stoi(N), stoi(t));
    1734          14 :     u = vecslice(v, 1, N);
    1735             :   }
    1736        9905 :   for (n = 1; n <= N; n++)
    1737             :   {
    1738        9779 :     GEN un = gel(u,n);
    1739        9779 :     if (is_rational_t(typ(un))) gel(u,n) = gtofp(un, prec);
    1740             :   }
    1741         126 :   return u;
    1742             : }
    1743             : 
    1744             : struct limit
    1745             : {
    1746             :   long prec0; /* target accuracy */
    1747             :   long prec; /* working accuracy */
    1748             :   long N; /* number of terms */
    1749             :   long a; /* = 1, 2 (alpha = 1, 2) or 0 (other alpha) */
    1750             :   GEN u; /* sequence to extrapolate */
    1751             :   GEN na; /* [n^alpha, n <= N] */
    1752             :   GEN coef; /* or NULL (alpha != 1) */
    1753             : };
    1754             : 
    1755             : static GEN
    1756        9465 : _gi(void *E, GEN x)
    1757             : {
    1758        9465 :   GEN A = (GEN)E, y = gsubgs(x, 1);
    1759        9465 :   if (gequal0(y)) return A;
    1760        9458 :   return gdiv(gsubgs(gpow(x, A, LOWDEFAULTPREC), 1), y);
    1761             : }
    1762             : static GEN
    1763          75 : _g(void *E, GEN x)
    1764             : {
    1765          75 :   GEN D = (GEN)E, A = gel(D,1), T = gel(D,2);
    1766          75 :   const long prec = LOWDEFAULTPREC;
    1767          75 :   return gadd(glog(x,prec), intnum((void*)A, _gi, gen_0, gaddgs(x,1), T, prec));
    1768             : }
    1769             : 
    1770             : /* solve log(b) + int_0^{b+1} (x^(1/a)-1) / (x-1) dx = 0, b in [0,1]
    1771             :  * return -log_2(b), rounded up */
    1772             : static double
    1773         126 : get_accu(GEN a)
    1774             : {
    1775         126 :   pari_sp av = avma;
    1776         126 :   const long prec = LOWDEFAULTPREC;
    1777         126 :   const double We2 = 1.844434455794; /* (W(1/e) + 1) / log(2) */
    1778             :   GEN b, T;
    1779         126 :   if (!a) return We2;
    1780          42 :   if (typ(a) == t_INT) switch(itos_or_0(a))
    1781             :   {
    1782           0 :     case 1: return We2;
    1783          21 :     case 2: return 1.186955309668;
    1784           0 :     case 3: return 0.883182331990;
    1785             :   }
    1786          21 :   else if (typ(a) == t_FRAC && equali1(gel(a,1))) switch(itos_or_0(gel(a,2)))
    1787             :   {
    1788          14 :     case 2: return 2.644090500290;
    1789           0 :     case 3: return 3.157759214459;
    1790           0 :     case 4: return 3.536383237500;
    1791             :   }
    1792           7 :   T = intnuminit(gen_0, gen_1, 0, prec);
    1793           7 :   b = zbrent((void*)mkvec2(ginv(a), T), &_g, dbltor(1E-5), gen_1, prec);
    1794           7 :   return gc_double(av, -dbllog2r(b));
    1795             : }
    1796             : 
    1797             : static double
    1798         133 : get_c(GEN a)
    1799             : {
    1800         133 :   double A = a? gtodouble(a): 1.0;
    1801         133 :   if (A <= 0) pari_err_DOMAIN("limitnum","alpha","<=",gen_0, a);
    1802         126 :   if (A >= 2) return 0.2270;
    1803          98 :   if (A >= 1) return 0.3318;
    1804          14 :   if (A >= 0.5) return 0.6212;
    1805           0 :   if (A >= 0.3333) return 1.2;
    1806           0 :   return 3; /* only tested for A >= 0.25 */
    1807             : }
    1808             : 
    1809             : /* #u > 1, prod_{j != i} u[i] - u[j] */
    1810             : static GEN
    1811        1750 : proddiff(GEN u, long i)
    1812             : {
    1813        1750 :   pari_sp av = avma;
    1814        1750 :   long l = lg(u), j;
    1815        1750 :   GEN p = NULL;
    1816        1750 :   if (i == 1)
    1817             :   {
    1818          21 :     p = gsub(gel(u,1), gel(u,2));
    1819        1729 :     for (j = 3; j < l; j++)
    1820        1708 :       p = gmul(p, gsub(gel(u,i), gel(u,j)));
    1821             :   }
    1822             :   else
    1823             :   {
    1824        1729 :     p = gsub(gel(u,i), gel(u,1));
    1825      144312 :     for (j = 2; j < l; j++)
    1826      142583 :       if (j != i) p = gmul(p, gsub(gel(u,i), gel(u,j)));
    1827             :   }
    1828        1750 :   return gerepileupto(av, p);
    1829             : }
    1830             : static GEN
    1831           7 : vecpows(GEN u, long N)
    1832             : {
    1833             :   long i, l;
    1834           7 :   GEN v = cgetg_copy(u, &l);
    1835           7 :   for (i = 1; i < l; i++) gel(v,i) = gpowgs(gel(u,i), N);
    1836           7 :   return v;
    1837             : }
    1838             : 
    1839             : static void
    1840         133 : limit_init(struct limit *L, void *E, GEN (*f)(void*,GEN,long),
    1841             :            GEN alpha, long prec)
    1842             : {
    1843         133 :   long bitprec = prec2nbits(prec), n, N, a = 0, na = 0;
    1844             :   GEN c, v, T;
    1845             : 
    1846         133 :   L->N = N = ceil(get_c(alpha) * bitprec);
    1847         126 :   if (alpha && typ(alpha) == t_FRAC)
    1848             :   {
    1849          14 :     long da = itos_or_0(gel(alpha,2));
    1850          14 :     na = itos_or_0(gel(alpha,1));
    1851          14 :     if (da && na && da <= 4 && na <= 4)
    1852          14 :     { /* don't bother other cases */
    1853          14 :       long e = (N-1) % da, k = (N-1) / da;
    1854          14 :       if (e) { N += da - e; k++; } /* N = 1 (mod d) => simpler ^ (n/d)(N-1) */
    1855          14 :       na *= k;
    1856             :     }
    1857             :     else
    1858           0 :       na = da = 0;
    1859             :   }
    1860         126 :   L->prec = nbits2prec(bitprec + (long)ceil(get_accu(alpha) * N));
    1861         126 :   L->prec0 = prec;
    1862         126 :   L->u = get_u(E, f, N, L->prec);
    1863         126 :   if (!alpha) a = 1;
    1864          42 :   else if (typ(alpha) == t_INT)
    1865             :   {
    1866          21 :     a = itos_or_0(alpha);
    1867          21 :     if (a > 2) a = 0;
    1868             :   }
    1869         126 :   L->a = a;
    1870         126 :   L->coef = v = cgetg(N+1, t_VEC);
    1871         126 :   if (!a)
    1872             :   {
    1873          21 :     long prec2 = gprecision(alpha);
    1874             :     GEN u, U;
    1875          21 :     if (!prec2) prec2 = L->prec;
    1876          21 :     L->na = u = vecpowug(N, alpha, prec2);
    1877          21 :     U = na? vecpowuu(N, na): vecpows(u, N-1);
    1878          21 :     for (n = 1; n <= N; n++) gel(v,n) = gdiv(gel(U,n), proddiff(u,n));
    1879          21 :     return;
    1880             :   }
    1881         105 :   L->na = NULL;
    1882         105 :   c = mpfactr(N-1, L->prec);
    1883         105 :   if (a == 1)
    1884             :   {
    1885          84 :     c = invr(c);
    1886          84 :     if (!odd(N)) togglesign(c);
    1887          84 :     gel(v,1) = c;
    1888          84 :     for (n = 2; n <= N; n++) gel(v,n) = divru(mulrs(gel(v,n-1), n-1-N), n);
    1889             :   }
    1890             :   else
    1891             :   { /* a = 2 */
    1892          21 :     c = invr(mulru(sqrr(c), (N*(N+1)) >> 1));
    1893          21 :     if (!odd(N)) togglesign(c);
    1894          21 :     gel(v,1) = c;
    1895          21 :     for (n = 2; n <= N; n++) gel(v,n) = divru(mulrs(gel(v,n-1), n-1-N), N+n);
    1896             :   }
    1897         105 :   T = vecpowuu(N, a*N);
    1898         105 :   for (n = 2; n <= N; n++) gel(v,n) = mulri(gel(v,n), gel(T,n));
    1899             : }
    1900             : 
    1901             : /* Zagier/Lagrange extrapolation */
    1902             : static GEN
    1903        1054 : limitnum_i(struct limit *L)
    1904        1054 : { return gprec_w(RgV_dotproduct(L->u,L->coef), L->prec0); }
    1905             : GEN
    1906          84 : limitnum(void *E, GEN (*f)(void *, GEN, long), GEN alpha, long prec)
    1907             : {
    1908          84 :   pari_sp av = avma;
    1909             :   struct limit L;
    1910          84 :   limit_init(&L, E,f, alpha, prec);
    1911          77 :   return gerepilecopy(av, limitnum_i(&L));
    1912             : }
    1913             : GEN
    1914          91 : limitnum0(GEN u, GEN alpha, long prec)
    1915             : {
    1916          91 :   void *E = (void*)u;
    1917          91 :   GEN (*f)(void*,GEN,long) = NULL;
    1918          91 :   switch(typ(u))
    1919             :   {
    1920             :     case t_COL:
    1921           7 :     case t_VEC: break;
    1922          77 :     case t_CLOSURE: f = gp_callprec; break;
    1923           7 :     default: pari_err_TYPE("limitnum", u);
    1924             :   }
    1925          84 :   return limitnum(E,f, alpha, prec);
    1926             : }
    1927             : 
    1928             : GEN
    1929          49 : asympnum(void *E, GEN (*f)(void *, GEN, long), GEN alpha, long prec)
    1930             : {
    1931          49 :   const long MAX = 100;
    1932          49 :   pari_sp av = avma;
    1933          49 :   GEN u, vres = vectrunc_init(MAX);
    1934          49 :   long i, B = prec2nbits(prec);
    1935          49 :   double LB = 0.9*expu(B); /* 0.9 and 0.95 below are heuristic */
    1936             :   struct limit L;
    1937          49 :   limit_init(&L, E,f, alpha, prec);
    1938          49 :   if (alpha) LB *= gtodouble(alpha);
    1939          49 :   if (!L.na) L.na = vecpowuu(L.N, L.a);
    1940          49 :   u = L.u;
    1941         977 :   for(i = 1; i <= MAX; i++)
    1942             :   {
    1943         977 :     GEN a, v, q, s = limitnum_i(&L);
    1944             :     long n;
    1945             :     /* NOT bestappr: lindep properly ignores the lower bits */
    1946         977 :     v = lindep_bit(mkvec2(gen_1, s), maxss((long)(0.95*floor(B - i*LB)), 32));
    1947         977 :     if (lg(v) == 1) break;
    1948         970 :     q = gel(v,2); if (!signe(q)) break;
    1949         970 :     a = gdiv(negi(gel(v,1)), q);
    1950         970 :     s = gsub(s, a);
    1951             :     /* |s|q^2 > eps */
    1952         970 :     if (!gequal0(s) && gexpo(s) + 2*expi(q) > -17) break;
    1953         928 :     vectrunc_append(vres, a);
    1954         928 :     for (n = 1; n <= L.N; n++) gel(u,n) = gmul(gsub(gel(u,n), a), gel(L.na,n));
    1955             :   }
    1956          49 :   return gerepilecopy(av, vres);
    1957             : }
    1958             : GEN
    1959          56 : asympnum0(GEN u, GEN alpha, long prec)
    1960             : {
    1961          56 :   void *E = (void*)u;
    1962          56 :   GEN (*f)(void*,GEN,long) = NULL;
    1963          56 :   switch(typ(u))
    1964             :   {
    1965             :     case t_COL:
    1966           7 :     case t_VEC: break;
    1967          42 :     case t_CLOSURE: f = gp_callprec; break;
    1968           7 :     default: pari_err_TYPE("asympnum", u);
    1969             :   }
    1970          49 :   return asympnum(E,f, alpha, prec);
    1971             : }

Generated by: LCOV version 1.13