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

Generated by: LCOV version 1.13