Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - polarit3.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21059-cbe0d6a) Lines: 1136 1340 84.8 %
Date: 2017-09-22 06:24:58 Functions: 121 135 89.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2005  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             : /***********************************************************************/
      15             : /**                                                                   **/
      16             : /**               ARITHMETIC OPERATIONS ON POLYNOMIALS                **/
      17             : /**                         (third part)                              **/
      18             : /**                                                                   **/
      19             : /***********************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : /************************************************************************
      24             :  **                                                                    **
      25             :  **                      Ring membership                               **
      26             :  **                                                                    **
      27             :  ************************************************************************/
      28             : struct charact {
      29             :   GEN q;
      30             :   int isprime;
      31             : };
      32             : static void
      33        3619 : char_update_prime(struct charact *S, GEN p)
      34             : {
      35        3619 :   if (!S->isprime) { S->isprime = 1; S->q = p; }
      36        3619 :   if (!equalii(p, S->q)) pari_err_MODULUS("characteristic", S->q, p);
      37        3612 : }
      38             : static void
      39        4347 : char_update_int(struct charact *S, GEN n)
      40             : {
      41        4347 :   if (S->isprime)
      42             :   {
      43        4347 :     if (dvdii(n, S->q)) return;
      44           7 :     pari_err_MODULUS("characteristic", S->q, n);
      45             :   }
      46        4340 :   S->q = gcdii(S->q, n);
      47             : }
      48             : static void
      49      594734 : charact(struct charact *S, GEN x)
      50             : {
      51      594734 :   const long tx = typ(x);
      52             :   long i, l;
      53      594734 :   switch(tx)
      54             :   {
      55        3759 :     case t_INTMOD:char_update_int(S, gel(x,1)); break;
      56        3528 :     case t_FFELT: char_update_prime(S, gel(x,4)); break;
      57             :     case t_COMPLEX: case t_QUAD:
      58             :     case t_POLMOD: case t_POL: case t_SER: case t_RFRAC:
      59             :     case t_VEC: case t_COL: case t_MAT:
      60       15449 :       l = lg(x);
      61       15449 :       for (i=lontyp[tx]; i < l; i++) charact(S,gel(x,i));
      62       15435 :       break;
      63             :     case t_LIST:
      64           7 :       x = list_data(x);
      65           7 :       if (x) charact(S, x);
      66           7 :       break;
      67             :   }
      68      594706 : }
      69             : static void
      70       32340 : charact_res(struct charact *S, GEN x)
      71             : {
      72       32340 :   const long tx = typ(x);
      73             :   long i, l;
      74       32340 :   switch(tx)
      75             :   {
      76         588 :     case t_INTMOD:char_update_int(S, gel(x,1)); break;
      77           0 :     case t_FFELT: char_update_prime(S, gel(x,4)); break;
      78          91 :     case t_PADIC: char_update_prime(S, gel(x,2)); break;
      79             :     case t_COMPLEX: case t_QUAD:
      80             :     case t_POLMOD: case t_POL: case t_SER: case t_RFRAC:
      81             :     case t_VEC: case t_COL: case t_MAT:
      82        9919 :       l = lg(x);
      83        9919 :       for (i=lontyp[tx]; i < l; i++) charact_res(S,gel(x,i));
      84        9919 :       break;
      85             :     case t_LIST:
      86           0 :       x = list_data(x);
      87           0 :       if (x) charact_res(S, x);
      88           0 :       break;
      89             :   }
      90       32340 : }
      91             : GEN
      92        8491 : characteristic(GEN x)
      93             : {
      94             :   struct charact S;
      95        8491 :   S.q = gen_0; S.isprime = 0;
      96        8491 :   charact(&S, x); return S.q;
      97             : }
      98             : GEN
      99        2415 : residual_characteristic(GEN x)
     100             : {
     101             :   struct charact S;
     102        2415 :   S.q = gen_0; S.isprime = 0;
     103        2415 :   charact_res(&S, x); return S.q;
     104             : }
     105             : 
     106             : int
     107    73653220 : Rg_is_Fp(GEN x, GEN *pp)
     108             : {
     109             :   GEN mod;
     110    73653220 :   switch(typ(x))
     111             :   {
     112             :   case t_INTMOD:
     113     6047223 :     mod = gel(x,1);
     114     6047223 :     if (!*pp) *pp = mod;
     115     5883983 :     else if (mod != *pp && !equalii(mod, *pp))
     116             :     {
     117           0 :       if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_Fp");
     118           0 :       return 0;
     119             :     }
     120     6047223 :     return 1;
     121             :   case t_INT:
     122    63136316 :     return 1;
     123     4469681 :   default: return 0;
     124             :   }
     125             : }
     126             : 
     127             : int
     128    19343636 : RgX_is_FpX(GEN x, GEN *pp)
     129             : {
     130    19343636 :   long i, lx = lg(x);
     131    74984726 :   for (i=2; i<lx; i++)
     132    59126172 :     if (!Rg_is_Fp(gel(x, i), pp))
     133     3485082 :       return 0;
     134    15858554 :   return 1;
     135             : }
     136             : 
     137             : int
     138     2409527 : RgV_is_FpV(GEN x, GEN *pp)
     139             : {
     140     2409527 :   long i, lx = lg(x);
     141    15942813 :   for (i=1; i<lx; i++)
     142    14517885 :     if (!Rg_is_Fp(gel(x,i), pp)) return 0;
     143     1424928 :   return 1;
     144             : }
     145             : 
     146             : int
     147     1143658 : RgM_is_FpM(GEN x, GEN *pp)
     148             : {
     149     1143658 :   long i, lx = lg(x);
     150     2566185 :   for (i=1; i<lx; i++)
     151     2407105 :     if (!RgV_is_FpV(gel(x, i), pp)) return 0;
     152      159080 :   return 1;
     153             : }
     154             : 
     155             : int
     156       15911 : Rg_is_FpXQ(GEN x, GEN *pT, GEN *pp)
     157             : {
     158             :   GEN pol, mod, p;
     159       15911 :   switch(typ(x))
     160             :   {
     161             :   case t_INTMOD:
     162        9142 :     return Rg_is_Fp(x, pp);
     163             :   case t_INT:
     164        1155 :     return 1;
     165             :   case t_POL:
     166          21 :     return RgX_is_FpX(x, pp);
     167             :   case t_FFELT:
     168        3528 :     mod = FF_1(x); p = FF_p_i(x);
     169        3528 :     if (!*pp) *pp = p;
     170        3528 :     if (!*pT) *pT = mod;
     171        3528 :     if ((p != *pp && !equalii(p, *pp)) || (mod != *pT && !gequal(mod, *pT)))
     172             :     {
     173           0 :       if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_FpXQ");
     174           0 :       return 0;
     175             :     }
     176        3528 :     return 1;
     177             :   case t_POLMOD:
     178        1981 :     mod = gel(x,1); pol = gel(x, 2);
     179        1981 :     if (!RgX_is_FpX(mod, pp)) return 0;
     180        1981 :     if (typ(pol)==t_POL)
     181             :     {
     182        1960 :       if (!RgX_is_FpX(pol, pp)) return 0;
     183             :     }
     184          21 :     else if (!Rg_is_Fp(pol, pp)) return 0;
     185        1981 :     if (!*pT) *pT = mod;
     186        1470 :     else if (mod != *pT && !gequal(mod, *pT))
     187             :     {
     188           0 :       if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_FpXQ");
     189           0 :       return 0;
     190             :     }
     191        1981 :     return 1;
     192             : 
     193          84 :   default: return 0;
     194             :   }
     195             : }
     196             : 
     197             : int
     198        1456 : RgX_is_FpXQX(GEN x, GEN *pT, GEN *pp)
     199             : {
     200        1456 :   long i, lx = lg(x);
     201       16821 :   for (i = 2; i < lx; i++)
     202       15365 :     if (!Rg_is_FpXQ(gel(x,i), pT, pp)) return 0;
     203        1456 :   return 1;
     204             : }
     205             : 
     206             : /************************************************************************
     207             :  **                                                                    **
     208             :  **                      Ring conversion                               **
     209             :  **                                                                    **
     210             :  ************************************************************************/
     211             : 
     212             : /* p > 0 a t_INT, return lift(x * Mod(1,p)).
     213             :  * If x is an INTMOD, assume modulus is a multiple of p. */
     214             : GEN
     215    32963610 : Rg_to_Fp(GEN x, GEN p)
     216             : {
     217    32963610 :   if (lgefint(p) == 3) return utoi(Rg_to_Fl(x, uel(p,2)));
     218      477744 :   switch(typ(x))
     219             :   {
     220       37386 :     case t_INT: return modii(x, p);
     221             :     case t_FRAC: {
     222          60 :       pari_sp av = avma;
     223          60 :       GEN z = modii(gel(x,1), p);
     224          60 :       if (z == gen_0) return gen_0;
     225          60 :       return gerepileuptoint(av, remii(mulii(z, Fp_inv(gel(x,2), p)), p));
     226             :     }
     227           0 :     case t_PADIC: return padic_to_Fp(x, p);
     228             :     case t_INTMOD: {
     229      440298 :       GEN q = gel(x,1), a = gel(x,2);
     230      440298 :       if (equalii(q, p)) return icopy(a);
     231          14 :       if (!dvdii(q,p)) pari_err_MODULUS("Rg_to_Fp", q, p);
     232           0 :       return remii(a, p);
     233             :     }
     234           0 :     default: pari_err_TYPE("Rg_to_Fp",x);
     235             :       return NULL; /* LCOV_EXCL_LINE */
     236             :   }
     237             : }
     238             : /* If x is a POLMOD, assume modulus is a multiple of T. */
     239             : GEN
     240       48406 : Rg_to_FpXQ(GEN x, GEN T, GEN p)
     241             : {
     242       48406 :   long ta, tx = typ(x), v = get_FpX_var(T);
     243             :   GEN a, b;
     244       48406 :   if (is_const_t(tx))
     245             :   {
     246       41357 :     if (tx == t_FFELT)
     247             :     {
     248       21630 :       GEN z = FF_to_FpXQ(x);
     249       21630 :       setvarn(z, v);
     250       21630 :       return z;
     251             :     }
     252       19727 :     return scalar_ZX(Rg_to_Fp(x, p), v);
     253             :   }
     254        7049 :   switch(tx)
     255             :   {
     256             :     case t_POLMOD:
     257        6034 :       b = gel(x,1);
     258        6034 :       a = gel(x,2); ta = typ(a);
     259        6034 :       if (is_const_t(ta)) return scalar_ZX(Rg_to_Fp(a, p), v);
     260        5929 :       b = RgX_to_FpX(b, p); if (varn(b) != v) break;
     261        5929 :       a = RgX_to_FpX(a, p); if (ZX_equal(b,get_FpX_mod(T))) return a;
     262           0 :       if (signe(FpX_rem(b,T,p))==0) return FpX_rem(a, T, p);
     263           0 :       break;
     264             :     case t_POL:
     265        1015 :       if (varn(x) != v) break;
     266        1015 :       return FpX_rem(RgX_to_FpX(x,p), T, p);
     267             :     case t_RFRAC:
     268           0 :       a = Rg_to_FpXQ(gel(x,1), T,p);
     269           0 :       b = Rg_to_FpXQ(gel(x,2), T,p);
     270           0 :       return FpXQ_div(a,b, T,p);
     271             :   }
     272           0 :   pari_err_TYPE("Rg_to_FpXQ",x);
     273             :   return NULL; /* LCOV_EXCL_LINE */
     274             : }
     275             : GEN
     276      382483 : RgX_to_FpX(GEN x, GEN p)
     277             : {
     278             :   long i, l;
     279      382483 :   GEN z = cgetg_copy(x, &l); z[1] = x[1];
     280      382483 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     281      382483 :   return FpX_renormalize(z, l);
     282             : }
     283             : 
     284             : GEN
     285        1099 : RgV_to_FpV(GEN x, GEN p)
     286             : {
     287        1099 :   long i, l = lg(x);
     288        1099 :   GEN z = cgetg(l, t_VEC);
     289        1099 :   for (i = 1; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     290        1099 :   return z;
     291             : }
     292             : 
     293             : GEN
     294      918665 : RgC_to_FpC(GEN x, GEN p)
     295             : {
     296      918665 :   long i, l = lg(x);
     297      918665 :   GEN z = cgetg(l, t_COL);
     298      918665 :   for (i = 1; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     299      918665 :   return z;
     300             : }
     301             : 
     302             : GEN
     303       99480 : RgM_to_FpM(GEN x, GEN p)
     304             : {
     305       99480 :   long i, l = lg(x);
     306       99480 :   GEN z = cgetg(l, t_MAT);
     307       99480 :   for (i = 1; i < l; i++) gel(z,i) = RgC_to_FpC(gel(x,i), p);
     308       99480 :   return z;
     309             : }
     310             : GEN
     311       17116 : RgV_to_Flv(GEN x, ulong p)
     312             : {
     313       17116 :   long l = lg(x), i;
     314       17116 :   GEN a = cgetg(l, t_VECSMALL);
     315       17116 :   for (i=1; i<l; i++) a[i] = Rg_to_Fl(gel(x,i), p);
     316       17116 :   return a;
     317             : }
     318             : GEN
     319        2034 : RgM_to_Flm(GEN x, ulong p)
     320             : {
     321             :   long l, i;
     322        2034 :   GEN a = cgetg_copy(x, &l);
     323        2034 :   for (i=1; i<l; i++) gel(a,i) = RgV_to_Flv(gel(x,i), p);
     324        2034 :   return a;
     325             : }
     326             : 
     327             : GEN
     328         518 : RgX_to_FpXQX(GEN x, GEN T, GEN p)
     329             : {
     330         518 :   long i, l = lg(x);
     331         518 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     332         518 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T,p);
     333         518 :   return FpXQX_renormalize(z, l);
     334             : }
     335             : GEN
     336         735 : RgX_to_FqX(GEN x, GEN T, GEN p)
     337             : {
     338         735 :   long i, l = lg(x);
     339         735 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     340         735 :   if (T)
     341       11046 :     for (i = 2; i < l; i++)
     342       10353 :       gel(z,i) = simplify_shallow(Rg_to_FpXQ(gel(x,i), T, p));
     343             :   else
     344        1554 :     for (i = 2; i < l; i++)
     345        1512 :       gel(z,i) = Rg_to_Fp(gel(x,i), p);
     346         735 :   return FpXQX_renormalize(z, l);
     347             : }
     348             : 
     349             : /* lg(V) > 1 */
     350             : GEN
     351      849765 : FpXV_FpC_mul(GEN V, GEN W, GEN p)
     352             : {
     353      849765 :   pari_sp av = avma;
     354      849765 :   long i, l = lg(V);
     355      849765 :   GEN z = ZX_Z_mul(gel(V,1),gel(W,1));
     356     4181499 :   for(i=2; i<l; i++)
     357             :   {
     358     3331734 :     z = ZX_add(z, ZX_Z_mul(gel(V,i),gel(W,i)));
     359     3331734 :     if ((i & 7) == 0) z = gerepileupto(av, z);
     360             :   }
     361      849765 :   return gerepileupto(av, FpX_red(z,p));
     362             : }
     363             : 
     364             : GEN
     365        1260 : FqX_Fq_add(GEN y, GEN x, GEN T, GEN p)
     366             : {
     367        1260 :   long i, lz = lg(y);
     368             :   GEN z;
     369        1260 :   if (!T) return FpX_Fp_add(y, x, p);
     370        1260 :   if (lz == 2) return scalarpol(x, varn(y));
     371        1260 :   z = cgetg(lz,t_POL); z[1] = y[1];
     372        1260 :   gel(z,2) = Fq_add(gel(y,2),x, T, p);
     373        1260 :   if (lz == 3) z = FpXX_renormalize(z,lz);
     374             :   else
     375         350 :     for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
     376        1260 :   return z;
     377             : }
     378             : 
     379             : GEN
     380        1041 : FqX_Fq_sub(GEN y, GEN x, GEN T, GEN p)
     381             : {
     382        1041 :   long i, lz = lg(y);
     383             :   GEN z;
     384        1041 :   if (!T) return FpX_Fp_sub(y, x, p);
     385        1041 :   if (lz == 2) return scalarpol(x, varn(y));
     386        1041 :   z = cgetg(lz,t_POL); z[1] = y[1];
     387        1041 :   gel(z,2) = Fq_sub(gel(y,2), x, T, p);
     388        1041 :   if (lz == 3) z = FpXX_renormalize(z,lz);
     389             :   else
     390         954 :     for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
     391        1041 :   return z;
     392             : }
     393             : 
     394             : GEN
     395       63546 : FqX_Fq_mul_to_monic(GEN P, GEN U, GEN T, GEN p)
     396             : {
     397             :   long i, lP;
     398       63546 :   GEN res = cgetg_copy(P, &lP); res[1] = P[1];
     399       63546 :   for(i=2; i<lP-1; i++) gel(res,i) = Fq_mul(U,gel(P,i), T,p);
     400       63546 :   gel(res,lP-1) = gen_1; return res;
     401             : }
     402             : 
     403             : GEN
     404        3160 : FpXQX_normalize(GEN z, GEN T, GEN p)
     405             : {
     406             :   GEN lc;
     407        3160 :   if (lg(z) == 2) return z;
     408        3146 :   lc = leading_coeff(z);
     409        3146 :   if (typ(lc) == t_POL)
     410             :   {
     411        1466 :     if (lg(lc) > 3) /* non-constant */
     412        1421 :       return FqX_Fq_mul_to_monic(z, Fq_inv(lc,T,p), T,p);
     413             :     /* constant */
     414          45 :     lc = gel(lc,2);
     415          45 :     z = shallowcopy(z);
     416          45 :     gel(z, lg(z)-1) = lc;
     417             :   }
     418             :   /* lc a t_INT */
     419        1725 :   if (equali1(lc)) return z;
     420          35 :   return FqX_Fq_mul_to_monic(z, Fp_inv(lc,p), T,p);
     421             : }
     422             : 
     423             : GEN
     424      123403 : FqX_eval(GEN x, GEN y, GEN T, GEN p)
     425             : {
     426             :   pari_sp av;
     427             :   GEN p1, r;
     428      123403 :   long j, i=lg(x)-1;
     429      123403 :   if (i<=2)
     430       24122 :     return (i==2)? Fq_red(gel(x,2), T, p): gen_0;
     431       99281 :   av=avma; p1=gel(x,i);
     432             :   /* specific attention to sparse polynomials (see poleval)*/
     433             :   /*You've guessed it! It's a copy-paste(tm)*/
     434      291235 :   for (i--; i>=2; i=j-1)
     435             :   {
     436      192283 :     for (j=i; !signe(gel(x,j)); j--)
     437         329 :       if (j==2)
     438             :       {
     439         182 :         if (i!=j) y = Fq_pow(y,utoipos(i-j+1), T, p);
     440         182 :         return gerepileupto(av, Fq_mul(p1,y, T, p));
     441             :       }
     442      191954 :     r = (i==j)? y: Fq_pow(y, utoipos(i-j+1), T, p);
     443      191954 :     p1 = Fq_add(Fq_mul(p1,r,T,p), gel(x,j), T, p);
     444             :   }
     445       99099 :   return gerepileupto(av, p1);
     446             : }
     447             : 
     448             : GEN
     449       30380 : FqXY_evalx(GEN Q, GEN x, GEN T, GEN p)
     450             : {
     451       30380 :   long i, lb = lg(Q);
     452             :   GEN z;
     453       30380 :   if (!T) return FpXY_evalx(Q, x, p);
     454       20720 :   z = cgetg(lb, t_POL); z[1] = Q[1];
     455      115969 :   for (i=2; i<lb; i++)
     456             :   {
     457       95249 :     GEN q = gel(Q,i);
     458       95249 :     gel(z,i) = typ(q) == t_INT? modii(q,p): FqX_eval(q, x, T, p);
     459             :   }
     460       20720 :   return FpXQX_renormalize(z, lb);
     461             : }
     462             : 
     463             : /* Q an FpXY, evaluate at (X,Y) = (x,y) */
     464             : GEN
     465       12733 : FqXY_eval(GEN Q, GEN y, GEN x, GEN T, GEN p)
     466             : {
     467       12733 :   pari_sp av = avma;
     468       12733 :   if (!T) return FpXY_eval(Q, y, x, p);
     469         336 :   return gerepileupto(av, FqX_eval(FqXY_evalx(Q, x, T, p), y, T, p));
     470             : }
     471             : 
     472             : /* a X^d */
     473             : GEN
     474      575785 : monomial(GEN a, long d, long v)
     475             : {
     476             :   long i, n;
     477             :   GEN P;
     478      575785 :   if (d < 0) {
     479           0 :     if (isrationalzero(a)) return pol_0(v);
     480           0 :     retmkrfrac(a, pol_xn(-d, v));
     481             :   }
     482      575785 :   if (gequal0(a))
     483             :   {
     484        8617 :     if (isexactzero(a)) return scalarpol_shallow(a,v);
     485           0 :     n = d+2; P = cgetg(n+1, t_POL);
     486           0 :     P[1] = evalsigne(0) | evalvarn(v);
     487             :   }
     488             :   else
     489             :   {
     490      567168 :     n = d+2; P = cgetg(n+1, t_POL);
     491      567168 :     P[1] = evalsigne(1) | evalvarn(v);
     492             :   }
     493      567168 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     494      567168 :   gel(P,i) = a; return P;
     495             : }
     496             : GEN
     497     7598318 : monomialcopy(GEN a, long d, long v)
     498             : {
     499             :   long i, n;
     500             :   GEN P;
     501     7598318 :   if (d < 0) {
     502           7 :     if (isrationalzero(a)) return pol_0(v);
     503           7 :     retmkrfrac(gcopy(a), pol_xn(-d, v));
     504             :   }
     505     7598311 :   if (gequal0(a))
     506             :   {
     507           7 :     if (isexactzero(a)) return scalarpol(a,v);
     508           0 :     n = d+2; P = cgetg(n+1, t_POL);
     509           0 :     P[1] = evalsigne(0) | evalvarn(v);
     510             :   }
     511             :   else
     512             :   {
     513     7598304 :     n = d+2; P = cgetg(n+1, t_POL);
     514     7598304 :     P[1] = evalsigne(1) | evalvarn(v);
     515             :   }
     516     7598304 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     517     7598304 :   gel(P,i) = gcopy(a); return P;
     518             : }
     519             : GEN
     520       19908 : pol_x_powers(long N, long v)
     521             : {
     522       19908 :   GEN L = cgetg(N+1,t_VEC);
     523             :   long i;
     524       19908 :   for (i=1; i<=N; i++) gel(L,i) = pol_xn(i-1, v);
     525       19908 :   return L;
     526             : }
     527             : 
     528             : GEN
     529           0 : FqXQ_powers(GEN x, long l, GEN S, GEN T, GEN p)
     530             : {
     531           0 :   return T ? FpXQXQ_powers(x, l, S, T, p): FpXQ_powers(x, l, S, p);
     532             : }
     533             : 
     534             : GEN
     535           0 : FqXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, GEN p)
     536             : {
     537           0 :   return T ? FpXQXQ_matrix_pow(y, n, m, S, T, p): FpXQ_matrix_pow(y, n, m, S, p);
     538             : }
     539             : 
     540             : /*******************************************************************/
     541             : /*                                                                 */
     542             : /*                             Fq                                  */
     543             : /*                                                                 */
     544             : /*******************************************************************/
     545             : 
     546             : GEN
     547     6947679 : Fq_add(GEN x, GEN y, GEN T/*unused*/, GEN p)
     548             : {
     549             :   (void)T;
     550     6947679 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     551             :   {
     552     2382232 :     case 0: return Fp_add(x,y,p);
     553      204169 :     case 1: return FpX_Fp_add(x,y,p);
     554      338059 :     case 2: return FpX_Fp_add(y,x,p);
     555     4023219 :     case 3: return FpX_add(x,y,p);
     556             :   }
     557           0 :   return NULL;
     558             : }
     559             : 
     560             : GEN
     561     4686661 : Fq_sub(GEN x, GEN y, GEN T/*unused*/, GEN p)
     562             : {
     563             :   (void)T;
     564     4686661 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     565             :   {
     566      155494 :     case 0: return Fp_sub(x,y,p);
     567        2356 :     case 1: return FpX_Fp_sub(x,y,p);
     568        8331 :     case 2: return Fp_FpX_sub(x,y,p);
     569     4520480 :     case 3: return FpX_sub(x,y,p);
     570             :   }
     571           0 :   return NULL;
     572             : }
     573             : 
     574             : GEN
     575      464633 : Fq_neg(GEN x, GEN T/*unused*/, GEN p)
     576             : {
     577             :   (void)T;
     578      464633 :   return (typ(x)==t_POL)? FpX_neg(x,p): Fp_neg(x,p);
     579             : }
     580             : 
     581             : GEN
     582       11457 : Fq_halve(GEN x, GEN T/*unused*/, GEN p)
     583             : {
     584             :   (void)T;
     585       11457 :   return (typ(x)==t_POL)? FpX_halve(x,p): Fp_halve(x,p);
     586             : }
     587             : 
     588             : /* If T==NULL do not reduce*/
     589             : GEN
     590    42552770 : Fq_mul(GEN x, GEN y, GEN T, GEN p)
     591             : {
     592    42552770 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     593             :   {
     594     2380761 :     case 0: return Fp_mul(x,y,p);
     595      188231 :     case 1: return FpX_Fp_mul(x,y,p);
     596      143224 :     case 2: return FpX_Fp_mul(y,x,p);
     597    39840554 :     case 3: if (T) return FpXQ_mul(x,y,T,p);
     598     2934309 :             else return FpX_mul(x,y,p);
     599             :   }
     600           0 :   return NULL;
     601             : }
     602             : 
     603             : /* If T==NULL do not reduce*/
     604             : GEN
     605      731527 : Fq_mulu(GEN x, ulong y, /*unused*/GEN T, GEN p)
     606             : {
     607             :   (void) T;
     608      731527 :   return typ(x)==t_POL ? FpX_Fp_mul(x,utoi(y),p): Fp_mulu(x, y, p);
     609             : }
     610             : 
     611             : /* y t_INT */
     612             : GEN
     613       51687 : Fq_Fp_mul(GEN x, GEN y, GEN T/*unused*/, GEN p)
     614             : {
     615             :   (void)T;
     616      103374 :   return (typ(x) == t_POL)? FpX_Fp_mul(x,y,p)
     617       51687 :                           : Fp_mul(x,y,p);
     618             : }
     619             : /* If T==NULL do not reduce*/
     620             : GEN
     621      261449 : Fq_sqr(GEN x, GEN T, GEN p)
     622             : {
     623      261449 :   if (typ(x) == t_POL)
     624             :   {
     625       11199 :     if (T) return FpXQ_sqr(x,T,p);
     626           0 :     else return FpX_sqr(x,p);
     627             :   }
     628             :   else
     629      250250 :     return Fp_sqr(x,p);
     630             : }
     631             : 
     632             : GEN
     633           0 : Fq_neg_inv(GEN x, GEN T, GEN p)
     634             : {
     635           0 :   if (typ(x) == t_INT) return Fp_inv(Fp_neg(x,p),p);
     636           0 :   return FpXQ_inv(FpX_neg(x,p),T,p);
     637             : }
     638             : 
     639             : GEN
     640           0 : Fq_invsafe(GEN x, GEN pol, GEN p)
     641             : {
     642           0 :   if (typ(x) == t_INT) return Fp_invsafe(x,p);
     643           0 :   return FpXQ_invsafe(x,pol,p);
     644             : }
     645             : 
     646             : GEN
     647       24702 : Fq_inv(GEN x, GEN pol, GEN p)
     648             : {
     649       24702 :   if (typ(x) == t_INT) return Fp_inv(x,p);
     650       19949 :   return FpXQ_inv(x,pol,p);
     651             : }
     652             : 
     653             : GEN
     654      479192 : Fq_div(GEN x, GEN y, GEN pol, GEN p)
     655             : {
     656      479192 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     657             :   {
     658      451976 :     case 0: return Fp_div(x,y,p);
     659       22806 :     case 1: return FpX_Fp_mul(x,Fp_inv(y,p),p);
     660         196 :     case 2: return FpX_Fp_mul(FpXQ_inv(y,pol,p),x,p);
     661        4214 :     case 3: return FpXQ_div(x,y,pol,p);
     662             :   }
     663           0 :   return NULL;
     664             : }
     665             : 
     666             : GEN
     667       21595 : Fq_pow(GEN x, GEN n, GEN pol, GEN p)
     668             : {
     669       21595 :   if (typ(x) == t_INT) return Fp_pow(x,n,p);
     670        9044 :   return FpXQ_pow(x,n,pol,p);
     671             : }
     672             : 
     673             : GEN
     674       12985 : Fq_powu(GEN x, ulong n, GEN pol, GEN p)
     675             : {
     676       12985 :   if (typ(x) == t_INT) return Fp_powu(x,n,p);
     677         553 :   return FpXQ_powu(x,n,pol,p);
     678             : }
     679             : 
     680             : GEN
     681      709056 : Fq_sqrt(GEN x, GEN T, GEN p)
     682             : {
     683      709056 :   if (typ(x) == t_INT)
     684             :   {
     685      698544 :     if (!T || odd(get_FpX_degree(T))) return Fp_sqrt(x,p);
     686         287 :     x = scalarpol_shallow(x, get_FpX_var(T));
     687             :   }
     688       10799 :   return FpXQ_sqrt(x,T,p);
     689             : }
     690             : GEN
     691       60600 : Fq_sqrtn(GEN x, GEN n, GEN T, GEN p, GEN *zeta)
     692             : {
     693       60600 :   if (typ(x) == t_INT)
     694             :   {
     695             :     long d;
     696       60390 :     if (!T) return Fp_sqrtn(x,n,p,zeta);
     697         610 :     d = get_FpX_degree(T);
     698         610 :     if (ugcd(umodiu(n,d),d) == 1)
     699             :     {
     700         414 :       if (!zeta)
     701           7 :         return Fp_sqrtn(x,n,p,NULL);
     702             :       else
     703             :       {
     704             :         /* gcd(n,p-1)=gcd(n,p^d-1) <=> same number of solutions if Fp and F_{p^d} */
     705         407 :         if (equalii(gcdii(subiu(p,1),n), gcdii(subiu(Fp_powu(p,d,n), 1), n)))
     706         386 :           return Fp_sqrtn(x,n,p,zeta);
     707             :       }
     708             :     }
     709         217 :     x = scalarpol(x, get_FpX_var(T)); /* left on stack */
     710             :   }
     711         427 :   return FpXQ_sqrtn(x,n,T,p,zeta);
     712             : }
     713             : 
     714             : struct _Fq_field
     715             : {
     716             :   GEN T, p;
     717             : };
     718             : 
     719             : static GEN
     720        2268 : _Fq_red(void *E, GEN x)
     721        2268 : { struct _Fq_field *s = (struct _Fq_field *)E;
     722        2268 :   return Fq_red(x, s->T, s->p);
     723             : }
     724             : 
     725             : static GEN
     726        1344 : _Fq_add(void *E, GEN x, GEN y)
     727             : {
     728             :   (void) E;
     729        1344 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     730             :   {
     731          14 :     case 0: return addii(x,y);
     732           0 :     case 1: return ZX_Z_add(x,y);
     733         210 :     case 2: return ZX_Z_add(y,x);
     734        1120 :     default: return ZX_add(x,y);
     735             :   }
     736             : }
     737             : 
     738             : static GEN
     739         665 : _Fq_neg(void *E, GEN x) { (void) E; return typ(x)==t_POL?ZX_neg(x):negi(x); }
     740             : 
     741             : static GEN
     742        2219 : _Fq_mul(void *E, GEN x, GEN y)
     743             : {
     744             :   (void) E;
     745        2219 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     746             :   {
     747          63 :     case 0: return mulii(x,y);
     748         315 :     case 1: return ZX_Z_mul(x,y);
     749          56 :     case 2: return ZX_Z_mul(y,x);
     750        1785 :     default: return ZX_mul(x,y);
     751             :   }
     752             : }
     753             : 
     754             : static GEN
     755         322 : _Fq_inv(void *E, GEN x)
     756         322 : { struct _Fq_field *s = (struct _Fq_field *)E;
     757         322 :   return Fq_inv(x,s->T,s->p);
     758             : }
     759             : 
     760             : static int
     761         714 : _Fq_equal0(GEN x) { return signe(x)==0; }
     762             : 
     763             : static GEN
     764         448 : _Fq_s(void *E, long x) { (void) E; return stoi(x); }
     765             : 
     766             : static const struct bb_field Fq_field={_Fq_red,_Fq_add,_Fq_mul,_Fq_neg,
     767             :                                        _Fq_inv,_Fq_equal0,_Fq_s};
     768             : 
     769         161 : const struct bb_field *get_Fq_field(void **E, GEN T, GEN p)
     770             : {
     771         161 :   GEN z = new_chunk(sizeof(struct _Fq_field));
     772         161 :   struct _Fq_field *e = (struct _Fq_field *) z;
     773         161 :   e->T = T; e->p  = p; *E = (void*)e;
     774         161 :   return &Fq_field;
     775             : }
     776             : 
     777             : /*******************************************************************/
     778             : /*                                                                 */
     779             : /*                             Fq[X]                               */
     780             : /*                                                                 */
     781             : /*******************************************************************/
     782             : /* P(X + c) */
     783             : GEN
     784           0 : FpX_translate(GEN P, GEN c, GEN p)
     785             : {
     786           0 :   pari_sp av = avma;
     787             :   GEN Q, *R;
     788             :   long i, k, n;
     789             : 
     790           0 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     791           0 :   Q = leafcopy(P);
     792           0 :   R = (GEN*)(Q+2); n = degpol(P);
     793           0 :   for (i=1; i<=n; i++)
     794             :   {
     795           0 :     for (k=n-i; k<n; k++)
     796           0 :       R[k] = Fp_add(R[k], Fp_mul(c, R[k+1], p), p);
     797             : 
     798           0 :     if (gc_needed(av,2))
     799             :     {
     800           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FpX_translate, i = %ld/%ld", i,n);
     801           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     802             :     }
     803             :   }
     804           0 :   return gerepilecopy(av, FpX_renormalize(Q, lg(Q)));
     805             : }
     806             : /* P(X + c), c an Fq */
     807             : GEN
     808       43589 : FqX_translate(GEN P, GEN c, GEN T, GEN p)
     809             : {
     810       43589 :   pari_sp av = avma;
     811             :   GEN Q, *R;
     812             :   long i, k, n;
     813             : 
     814             :   /* signe works for t_(INT|POL) */
     815       43589 :   if (!signe(P) || !signe(c)) return RgX_copy(P);
     816       43589 :   Q = leafcopy(P);
     817       43589 :   R = (GEN*)(Q+2); n = degpol(P);
     818      194922 :   for (i=1; i<=n; i++)
     819             :   {
     820      553028 :     for (k=n-i; k<n; k++)
     821      401695 :       R[k] = Fq_add(R[k], Fq_mul(c, R[k+1], T, p), T, p);
     822             : 
     823      151333 :     if (gc_needed(av,2))
     824             :     {
     825           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FqX_translate, i = %ld/%ld", i,n);
     826           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     827             :     }
     828             :   }
     829       43589 :   return gerepilecopy(av, FpXQX_renormalize(Q, lg(Q)));
     830             : }
     831             : 
     832             : GEN
     833         630 : FqV_roots_to_pol(GEN V, GEN T, GEN p, long v)
     834             : {
     835         630 :   pari_sp ltop = avma;
     836             :   long k;
     837             :   GEN W;
     838         630 :   if (lgefint(p) == 3)
     839             :   {
     840         591 :     ulong pp = p[2];
     841         591 :     GEN Tl = ZX_to_Flx(T, pp);
     842         591 :     GEN Vl = FqV_to_FlxV(V, T, p);
     843         591 :     Tl = FlxqV_roots_to_pol(Vl, Tl, pp, v);
     844         591 :     return gerepileupto(ltop, FlxX_to_ZXX(Tl));
     845             :   }
     846          39 :   W = cgetg(lg(V),t_VEC);
     847         255 :   for(k=1; k < lg(V); k++)
     848         216 :     gel(W,k) = deg1pol_shallow(gen_1,Fq_neg(gel(V,k),T,p),v);
     849          39 :   return gerepileupto(ltop, FpXQXV_prod(W, T, p));
     850             : }
     851             : 
     852             : GEN
     853      123277 : FqV_red(GEN z, GEN T, GEN p)
     854             : {
     855      123277 :   long i, l = lg(z);
     856      123277 :   GEN res = cgetg(l, typ(z));
     857      123277 :   for(i=1;i<l;i++) gel(res,i) = Fq_red(gel(z,i),T,p);
     858      123277 :   return res;
     859             : }
     860             : 
     861             : GEN
     862           0 : FqC_add(GEN x, GEN y, GEN T, GEN p)
     863             : {
     864           0 :   long i, lx = lg(x);
     865             :   GEN z;
     866           0 :   if (!T) return FpC_add(x, y, p);
     867           0 :   z = cgetg(lx, t_COL);
     868           0 :   for (i = 1; i < lx; i++) gel(z, i) = Fq_add(gel(x, i), gel(y, i), T, p);
     869           0 :   return z;
     870             : }
     871             : 
     872             : GEN
     873           0 : FqC_sub(GEN x, GEN y, GEN T, GEN p)
     874             : {
     875           0 :   long i, lx = lg(x);
     876             :   GEN z;
     877           0 :   if (!T) return FpC_sub(x, y, p);
     878           0 :   z = cgetg(lx, t_COL);
     879           0 :   for (i = 1; i < lx; i++) gel(z, i) = Fq_sub(gel(x, i), gel(y, i), T, p);
     880           0 :   return z;
     881             : }
     882             : 
     883             : GEN
     884           0 : FqC_Fq_mul(GEN x, GEN y, GEN T, GEN p)
     885             : {
     886           0 :   long i, l = lg(x);
     887             :   GEN z;
     888           0 :   if (!T) return FpC_Fp_mul(x, y, p);
     889           0 :   z = cgetg(l, t_COL);
     890           0 :   for (i=1;i<l;i++) gel(z,i) = Fq_mul(gel(x,i),y,T,p);
     891           0 :   return z;
     892             : }
     893             : 
     894             : GEN
     895         591 : FqV_to_FlxV(GEN v, GEN T, GEN pp)
     896             : {
     897         591 :   long j, N = lg(v);
     898         591 :   long vT = evalvarn(get_FpX_var(T));
     899         591 :   ulong p = pp[2];
     900         591 :   GEN y = cgetg(N, t_VEC);
     901        2874 :   for (j=1; j<N; j++)
     902        4566 :     gel(y,j) = (typ(gel(v,j))==t_INT?  Z_to_Flx(gel(v,j), p, vT)
     903        2283 :                                     : ZX_to_Flx(gel(v,j), p));
     904         591 :   return y;
     905             : }
     906             : 
     907             : GEN
     908       41673 : FqC_to_FlxC(GEN v, GEN T, GEN pp)
     909             : {
     910       41673 :   long j, N = lg(v);
     911       41673 :   long vT = evalvarn(get_FpX_var(T));
     912       41673 :   ulong p = pp[2];
     913       41673 :   GEN y = cgetg(N, t_COL);
     914     1097112 :   for (j=1; j<N; j++)
     915     2346458 :     gel(y,j) = (typ(gel(v,j))==t_INT?  Z_to_Flx(gel(v,j), p, vT)
     916     1291019 :                                     : ZX_to_Flx(gel(v,j), p));
     917       41673 :   return y;
     918             : }
     919             : 
     920             : GEN
     921        8010 : FqM_to_FlxM(GEN x, GEN T, GEN pp)
     922             : {
     923        8010 :   long j, n = lg(x);
     924        8010 :   GEN y = cgetg(n,t_MAT);
     925        8010 :   if (n == 1) return y;
     926       49683 :   for (j=1; j<n; j++)
     927       41673 :     gel(y,j) = FqC_to_FlxC(gel(x,j), T, pp);
     928        8010 :   return y;
     929             : }
     930             : 
     931             : /*******************************************************************/
     932             : /*                                                                 */
     933             : /*                          MODULAR GCD                            */
     934             : /*                                                                 */
     935             : /*******************************************************************/
     936             : /* return z = a mod q, b mod p (p,q) = 1. qinv = 1/q mod p */
     937             : static GEN
     938    11836541 : Fl_chinese_coprime(GEN a, ulong b, GEN q, ulong p, ulong qinv, GEN pq)
     939             : {
     940    11836541 :   ulong d, amod = umodiu(a, p);
     941    11836541 :   pari_sp av = avma;
     942             :   GEN ax;
     943             : 
     944    11836541 :   if (b == amod) return NULL;
     945     3114169 :   d = (b > amod)? b - amod: p - (amod - b); /* (b - a) mod p */
     946     3114169 :   (void)new_chunk(lgefint(pq)<<1); /* HACK */
     947     3114169 :   ax = mului(Fl_mul(d,qinv,p), q); /* d mod p, 0 mod q */
     948     3114169 :   avma = av; return addii(a, ax); /* in ]-q, pq[ assuming a in -]-q,q[ */
     949             : }
     950             : GEN
     951       17087 : Z_init_CRT(ulong Hp, ulong p) { return stoi(Fl_center(Hp, p, p>>1)); }
     952             : GEN
     953     3163666 : ZX_init_CRT(GEN Hp, ulong p, long v)
     954             : {
     955     3163666 :   long i, l = lg(Hp), lim = (long)(p>>1);
     956     3163666 :   GEN H = cgetg(l, t_POL);
     957     3163666 :   H[1] = evalsigne(1) | evalvarn(v);
     958    11210156 :   for (i=2; i<l; i++)
     959     8046490 :     gel(H,i) = stoi(Fl_center(Hp[i], p, lim));
     960     3163666 :   return H;
     961             : }
     962             : 
     963             : GEN
     964      219483 : ZM_init_CRT(GEN Hp, ulong p)
     965             : {
     966      219483 :   long i,j, m, l = lg(Hp), lim = (long)(p>>1);
     967      219483 :   GEN c, cp, H = cgetg(l, t_MAT);
     968      219483 :   if (l==1) return H;
     969      176076 :   m = lgcols(Hp);
     970     1080960 :   for (j=1; j<l; j++)
     971             :   {
     972      904884 :     cp = gel(Hp,j);
     973      904884 :     c = cgetg(m, t_COL);
     974      904884 :     gel(H,j) = c;
     975      904884 :     for (i=1; i<m; i++) gel(c,i) = stoi(Fl_center(cp[i],p, lim));
     976             :   }
     977      176076 :   return H;
     978             : }
     979             : 
     980             : int
     981       58655 : Z_incremental_CRT(GEN *H, ulong Hp, GEN *ptq, ulong p)
     982             : {
     983       58655 :   GEN h, q = *ptq, qp = muliu(q,p), lim = shifti(qp,-1);
     984       58655 :   ulong qinv = Fl_inv(umodiu(q,p), p);
     985       58655 :   int stable = 1;
     986       58655 :   h = Fl_chinese_coprime(*H,Hp,q,p,qinv,qp);
     987       58655 :   if (h)
     988             :   {
     989       14650 :     if (cmpii(h,lim) > 0) h = subii(h,qp);
     990       14650 :     *H = h; stable = 0;
     991             :   }
     992       58655 :   *ptq = qp; return stable;
     993             : }
     994             : 
     995             : static int
     996      172573 : ZX_incremental_CRT_raw(GEN *ptH, GEN Hp, GEN q, GEN qp, ulong p)
     997             : {
     998      172573 :   GEN H = *ptH, h, lim = shifti(qp,-1);
     999      172573 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1000      172573 :   long i, l = lg(H), lp = lg(Hp);
    1001      172573 :   int stable = 1;
    1002             : 
    1003      172573 :   if (l < lp)
    1004             :   { /* degree increases */
    1005           0 :     GEN x = cgetg(lp, t_POL);
    1006           0 :     for (i=1; i<l; i++)  x[i] = H[i];
    1007           0 :     for (   ; i<lp; i++) gel(x,i) = gen_0;
    1008           0 :     *ptH = H = x;
    1009           0 :     stable = 0;
    1010      172573 :   } else if (l > lp)
    1011             :   { /* degree decreases */
    1012           0 :     GEN x = cgetg(l, t_VECSMALL);
    1013           0 :     for (i=1; i<lp; i++)  x[i] = Hp[i];
    1014           0 :     for (   ; i<l; i++) x[i] = 0;
    1015           0 :     Hp = x; lp = l;
    1016             :   }
    1017     1413554 :   for (i=2; i<lp; i++)
    1018             :   {
    1019     1240981 :     h = Fl_chinese_coprime(gel(H,i),Hp[i],q,p,qinv,qp);
    1020     1240981 :     if (h)
    1021             :     {
    1022      928440 :       if (cmpii(h,lim) > 0) h = subii(h,qp);
    1023      928440 :       gel(H,i) = h; stable = 0;
    1024             :     }
    1025             :   }
    1026      172573 :   return stable;
    1027             : }
    1028             : 
    1029             : int
    1030        1202 : ZX_incremental_CRT(GEN *ptH, GEN Hp, GEN *ptq, ulong p)
    1031             : {
    1032        1202 :   GEN q = *ptq, qp = muliu(q,p);
    1033        1202 :   int stable = ZX_incremental_CRT_raw(ptH, Hp, q, qp, p);
    1034        1202 :   *ptq = qp; return stable;
    1035             : }
    1036             : 
    1037             : int
    1038      147452 : ZM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
    1039             : {
    1040      147452 :   GEN h, H = *pH, q = *ptq, qp = muliu(q, p), lim = shifti(qp,-1);
    1041      147452 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1042      147452 :   long i,j, l = lg(H), m = lgcols(H);
    1043      147452 :   int stable = 1;
    1044     1068274 :   for (j=1; j<l; j++)
    1045    11312439 :     for (i=1; i<m; i++)
    1046             :     {
    1047    10391617 :       h = Fl_chinese_coprime(gcoeff(H,i,j), coeff(Hp,i,j),q,p,qinv,qp);
    1048    10391617 :       if (h)
    1049             :       {
    1050     2064167 :         if (cmpii(h,lim) > 0) h = subii(h,qp);
    1051     2064167 :         gcoeff(H,i,j) = h; stable = 0;
    1052             :       }
    1053             :     }
    1054      147452 :   *ptq = qp; return stable;
    1055             : }
    1056             : 
    1057             : GEN
    1058        1449 : ZVM_init_CRT(GEN Hp, ulong p)
    1059             : {
    1060             :   long i, j, k;
    1061             :   GEN c, cp, d, dp, H;
    1062        1449 :   long m, l = lg(Hp), lim = (long)(p>>1), n;
    1063        1449 :   H = cgetg(l, t_MAT);
    1064        1449 :   if (l==1) return H;
    1065        1449 :   m = lgcols(Hp);
    1066        1449 :   n = lg(gmael(Hp,1,1));
    1067        6097 :   for (j=1; j<l; j++)
    1068             :   {
    1069        4648 :     cp = gel(Hp,j);
    1070        4648 :     c = cgetg(m, t_COL);
    1071        4648 :     gel(H,j) = c;
    1072       67963 :     for (i=1; i<m; i++)
    1073             :     {
    1074       63315 :       dp = gel(cp, i);
    1075       63315 :       d = cgetg(n, t_VEC);
    1076       63315 :       gel(c, i) = d;
    1077      190827 :       for (k=1; k<n; k++)
    1078      127512 :         gel(d,k) = stoi(Fl_center(dp[k], p, lim));
    1079             :     }
    1080             :   }
    1081        1449 :   return H;
    1082             : }
    1083             : 
    1084             : int
    1085         590 : ZVM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
    1086             : {
    1087         590 :   GEN h, H = *pH, q = *ptq, qp = muliu(q, p), lim = shifti(qp,-1);
    1088         590 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1089         590 :   long i,j,k, l = lg(H), m = lgcols(H), n = lg(gmael(H,1,1));
    1090         590 :   int stable = 1;
    1091        3873 :   for (j=1; j<l; j++)
    1092       75927 :     for (i=1; i<m; i++)
    1093      217932 :       for (k=1; k<n; k++)
    1094             :       {
    1095      145288 :         h = Fl_chinese_coprime(gmael3(H,j,i,k), mael3(Hp,j,i,k),q,p,qinv,qp);
    1096      145288 :         if (h)
    1097             :         {
    1098      106912 :           if (cmpii(h,lim) > 0) h = subii(h,qp);
    1099      106912 :           gmael3(H,j,i,k) = h; stable = 0;
    1100             :         }
    1101             :       }
    1102         590 :   *ptq = qp; return stable;
    1103             : }
    1104             : 
    1105             : /* record the degrees of Euclidean remainders (make them as large as
    1106             :  * possible : smaller values correspond to a degenerate sequence) */
    1107             : static void
    1108        1561 : Flx_resultant_set_dglist(GEN a, GEN b, GEN dglist, ulong p)
    1109             : {
    1110             :   long da,db,dc, ind;
    1111        1561 :   pari_sp av = avma;
    1112             : 
    1113        1561 :   if (lgpol(a)==0 || lgpol(b)==0) return;
    1114        1561 :   da = degpol(a);
    1115        1561 :   db = degpol(b);
    1116        1561 :   if (db > da)
    1117           0 :   { swapspec(a,b, da,db); }
    1118        1561 :   else if (!da) return;
    1119        1561 :   ind = 0;
    1120        9814 :   while (db)
    1121             :   {
    1122        6692 :     GEN c = Flx_rem(a,b, p);
    1123        6692 :     a = b; b = c; dc = degpol(c);
    1124        6692 :     if (dc < 0) break;
    1125             : 
    1126        6692 :     ind++;
    1127        6692 :     if (dc > dglist[ind]) dglist[ind] = dc;
    1128        6692 :     if (gc_needed(av,2))
    1129             :     {
    1130           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1131           0 :       gerepileall(av, 2, &a,&b);
    1132             :     }
    1133        6692 :     db = dc; /* = degpol(b) */
    1134             :   }
    1135        1561 :   if (ind+1 > lg(dglist)) setlg(dglist,ind+1);
    1136        1561 :   avma = av; return;
    1137             : }
    1138             : /* assuming the PRS finishes on a degree 1 polynomial C0 + C1X, with
    1139             :  * "generic" degree sequence as given by dglist, set *Ci and return
    1140             :  * resultant(a,b). Modular version of Collins's subresultant */
    1141             : static ulong
    1142        6996 : Flx_resultant_all(GEN a, GEN b, long *C0, long *C1, GEN dglist, ulong p)
    1143             : {
    1144             :   long da,db,dc, ind;
    1145        6996 :   ulong lb, res, g = 1UL, h = 1UL, ca = 1UL, cb = 1UL;
    1146        6996 :   int s = 1;
    1147        6996 :   pari_sp av = avma;
    1148             : 
    1149        6996 :   *C0 = 1; *C1 = 0;
    1150        6996 :   if (lgpol(a)==0 || lgpol(b)==0) return 0;
    1151        6996 :   da = degpol(a);
    1152        6996 :   db = degpol(b);
    1153        6996 :   if (db > da)
    1154             :   {
    1155           0 :     swapspec(a,b, da,db);
    1156           0 :     if (both_odd(da,db)) s = -s;
    1157             :   }
    1158        6996 :   else if (!da) return 1; /* = a[2] ^ db, since 0 <= db <= da = 0 */
    1159        6996 :   ind = 0;
    1160       40727 :   while (db)
    1161             :   { /* sub-resultant algo., applied to ca * a and cb * b, ca,cb scalars,
    1162             :      * da = deg a, db = deg b */
    1163       27099 :     GEN c = Flx_rem(a,b, p);
    1164       27099 :     long delta = da - db;
    1165             : 
    1166       27099 :     if (both_odd(da,db)) s = -s;
    1167       27099 :     lb = Fl_mul(b[db+2], cb, p);
    1168       27099 :     a = b; b = c; dc = degpol(c);
    1169       27099 :     ind++;
    1170       27099 :     if (dc != dglist[ind]) { avma = av; return 0; } /* degenerates */
    1171       26735 :     if (g == h)
    1172             :     { /* frequent */
    1173       24621 :       ulong cc = Fl_mul(ca, Fl_powu(Fl_div(lb,g,p), delta+1, p), p);
    1174       24621 :       ca = cb;
    1175       24621 :       cb = cc;
    1176             :     }
    1177             :     else
    1178             :     {
    1179        2114 :       ulong cc = Fl_mul(ca, Fl_powu(lb, delta+1, p), p);
    1180        2114 :       ulong ghdelta = Fl_mul(g, Fl_powu(h, delta, p), p);
    1181        2114 :       ca = cb;
    1182        2114 :       cb = Fl_div(cc, ghdelta, p);
    1183             :     }
    1184       26735 :     da = db; /* = degpol(a) */
    1185       26735 :     db = dc; /* = degpol(b) */
    1186             : 
    1187       26735 :     g = lb;
    1188       26735 :     if (delta == 1)
    1189       17482 :       h = g; /* frequent */
    1190             :     else
    1191        9253 :       h = Fl_mul(h, Fl_powu(Fl_div(g,h,p), delta, p), p);
    1192             : 
    1193       26735 :     if (gc_needed(av,2))
    1194             :     {
    1195           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1196           0 :       gerepileall(av, 2, &a,&b);
    1197             :     }
    1198             :   }
    1199        6632 :   if (da > 1) return 0; /* Failure */
    1200             :   /* last non-constant polynomial has degree 1 */
    1201        6632 :   *C0 = Fl_mul(ca, a[2], p);
    1202        6632 :   *C1 = Fl_mul(ca, a[3], p);
    1203        6632 :   res = Fl_mul(cb, b[2], p);
    1204        6632 :   if (s == -1) res = p - res;
    1205        6632 :   avma = av; return res;
    1206             : }
    1207             : 
    1208             : /* Q a vector of polynomials representing B in Fp[X][Y], evaluate at X = x,
    1209             :  * Return 0 in case of degree drop. */
    1210             : static GEN
    1211        8557 : FlxY_evalx_drop(GEN Q, ulong x, ulong p)
    1212             : {
    1213             :   GEN z;
    1214        8557 :   long i, lb = lg(Q);
    1215        8557 :   ulong leadz = Flx_eval(leading_coeff(Q), x, p);
    1216        8557 :   long vs=mael(Q,2,1);
    1217        8557 :   if (!leadz) return zero_Flx(vs);
    1218             : 
    1219        8557 :   z = cgetg(lb, t_VECSMALL); z[1] = vs;
    1220        8557 :   for (i=2; i<lb-1; i++) z[i] = Flx_eval(gel(Q,i), x, p);
    1221        8557 :   z[i] = leadz; return z;
    1222             : }
    1223             : 
    1224             : GEN
    1225       17836 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
    1226             : {
    1227       17836 :   pari_sp av = avma;
    1228       17836 :   long i, lb = lg(Q);
    1229             :   GEN z;
    1230       17836 :   if (!T) return FpXY_evaly(Q, y, p, vx);
    1231        1148 :   if (lb == 2) return pol_0(vx);
    1232        1148 :   z = gel(Q, lb-1);
    1233        1148 :   if (lb == 3 || !signe(y)) return typ(z)==t_INT? scalar_ZX(z, vx): ZX_copy(z);
    1234             : 
    1235        1148 :   if (typ(z) == t_INT) z = scalar_ZX_shallow(z, vx);
    1236       28084 :   for (i=lb-2; i>=2; i--)
    1237             :   {
    1238       26936 :     GEN c = gel(Q,i);
    1239       26936 :     z = FqX_Fq_mul(z, y, T, p);
    1240       26936 :     z = typ(c) == t_INT? FqX_Fq_add(z,c,T,p): FqX_add(z,c,T,p);
    1241             :   }
    1242        1148 :   return gerepileupto(av, z);
    1243             : }
    1244             : 
    1245             : static GEN
    1246       15246 : ZX_norml1(GEN x)
    1247             : {
    1248       15246 :   long i, l = lg(x);
    1249             :   GEN s;
    1250             : 
    1251       15246 :   if (l == 2) return gen_0;
    1252        8624 :   s = gel(x, l-1); /* != 0 */
    1253       31556 :   for (i = l-2; i > 1; i--) {
    1254       22932 :     GEN xi = gel(x,i);
    1255       22932 :     if (!signe(x)) continue;
    1256       22932 :     s = addii_sign(s,1, xi,1);
    1257             :   }
    1258        8624 :   return s;
    1259             : }
    1260             : 
    1261             : /* Interpolate at roots of 1 and use Hadamard bound for univariate resultant:
    1262             :  *   bound = N_2(A)^degpol B N_2(B)^degpol(A),  where
    1263             :  *     N_2(A) = sqrt(sum (N_1(Ai))^2)
    1264             :  * Return e such that Res(A, B) < 2^e */
    1265             : ulong
    1266       75297 : ZX_ZXY_ResBound(GEN A, GEN B, GEN dB)
    1267             : {
    1268       75297 :   pari_sp av = avma;
    1269       75297 :   GEN a = gen_0, b = gen_0;
    1270       75297 :   long i , lA = lg(A), lB = lg(B);
    1271             :   double loga, logb;
    1272      856911 :   for (i=2; i<lA; i++)
    1273             :   {
    1274      781614 :     a = addii(a, sqri(gel(A,i)));
    1275      781614 :     if (gc_needed(av,1))
    1276             :     {
    1277           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
    1278           0 :       a = gerepileupto(av, a);
    1279             :     }
    1280             :   }
    1281      787756 :   for (i=2; i<lB; i++)
    1282             :   {
    1283      712459 :     GEN t = gel(B,i);
    1284      712459 :     if (typ(t) == t_POL) t = ZX_norml1(t);
    1285      712459 :     b = addii(b, sqri(t));
    1286      712459 :     if (gc_needed(av,1))
    1287             :     {
    1288           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
    1289           0 :       b = gerepileupto(av, b);
    1290             :     }
    1291             :   }
    1292       75297 :   loga = dbllog2(a);
    1293       75297 :   logb = dbllog2(b); if (dB) logb -= 2 * dbllog2(dB);
    1294       75297 :   i = (long)((degpol(B) * loga + degpol(A) * logb) / 2);
    1295       75297 :   avma = av; return (i <= 0)? 1: 1 + (ulong)i;
    1296             : }
    1297             : 
    1298             : /* return Res(a(Y), b(n,Y)) over Fp. la = leading_coeff(a) [for efficiency] */
    1299             : static ulong
    1300      258344 : Flx_FlxY_eval_resultant(GEN a, GEN b, ulong n, ulong p, ulong la)
    1301             : {
    1302      258344 :   GEN ev = FlxY_evalx(b, n, p);
    1303      258031 :   long drop = lg(b) - lg(ev);
    1304      258031 :   ulong r = Flx_resultant(a, ev, p);
    1305      258323 :   if (drop && la != 1) r = Fl_mul(r, Fl_powu(la, drop,p),p);
    1306      258319 :   return r;
    1307             : }
    1308             : static GEN
    1309           4 : FpX_FpXY_eval_resultant(GEN a, GEN b, GEN n, GEN p, GEN la, long db, long vX)
    1310             : {
    1311           4 :   GEN ev = FpXY_evaly(b, n, p, vX);
    1312           4 :   long drop = db-degpol(ev);
    1313           4 :   GEN r = FpX_resultant(a, ev, p);
    1314           4 :   if (drop && !gequal1(la)) r = Fp_mul(r, Fp_powu(la, drop,p),p);
    1315           4 :   return r;
    1316             : }
    1317             : 
    1318             : /* assume dres := deg(Res_X(a,b), Y) <= deg(a,X) * deg(b,Y) < p */
    1319             : /* Return a Fly */
    1320             : static GEN
    1321       12648 : Flx_FlxY_resultant_polint(GEN a, GEN b, ulong p, long dres, long sx)
    1322             : {
    1323             :   long i;
    1324       12648 :   ulong n, la = Flx_lead(a);
    1325       12655 :   GEN  x = cgetg(dres+2, t_VECSMALL);
    1326       12647 :   GEN  y = cgetg(dres+2, t_VECSMALL);
    1327             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1328             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1329      136999 :   for (i=0,n = 1; i < dres; n++)
    1330             :   {
    1331      124338 :     x[++i] = n;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1332      124317 :     x[++i] = p-n; y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1333             :   }
    1334       12661 :   if (i == dres)
    1335             :   {
    1336        9865 :     x[++i] = 0;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1337             :   }
    1338       12661 :   return Flv_polint(x,y, p, sx);
    1339             : }
    1340             : 
    1341             : static GEN
    1342        9141 : FlxX_pseudorem(GEN x, GEN y, ulong p)
    1343             : {
    1344        9141 :   long vx = varn(x), dx, dy, dz, i, lx, dp;
    1345        9141 :   pari_sp av = avma, av2;
    1346             : 
    1347        9141 :   if (!signe(y)) pari_err_INV("FlxX_pseudorem",y);
    1348        9141 :   (void)new_chunk(2);
    1349        9143 :   dx=degpol(x); x = RgX_recip_shallow(x)+2;
    1350        9145 :   dy=degpol(y); y = RgX_recip_shallow(y)+2; dz=dx-dy; dp = dz+1;
    1351        9146 :   av2 = avma;
    1352             :   for (;;)
    1353             :   {
    1354       70639 :     gel(x,0) = Flx_neg(gel(x,0), p); dp--;
    1355      271982 :     for (i=1; i<=dy; i++)
    1356      399676 :       gel(x,i) = Flx_add( Flx_mul(gel(y,0), gel(x,i), p),
    1357      199838 :                               Flx_mul(gel(x,0), gel(y,i), p), p );
    1358     1248247 :     for (   ; i<=dx; i++)
    1359     1177616 :       gel(x,i) = Flx_mul(gel(y,0), gel(x,i), p);
    1360       74882 :     do { x++; dx--; } while (dx >= 0 && lg(gel(x,0))==2);
    1361       70631 :     if (dx < dy) break;
    1362       61489 :     if (gc_needed(av2,1))
    1363             :     {
    1364           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_pseudorem dx = %ld >= %ld",dx,dy);
    1365           0 :       gerepilecoeffs(av2,x,dx+1);
    1366             :     }
    1367       61493 :   }
    1368        9142 :   if (dx < 0) return zero_Flx(0);
    1369        9142 :   lx = dx+3; x -= 2;
    1370        9142 :   x[0]=evaltyp(t_POL) | evallg(lx);
    1371        9142 :   x[1]=evalsigne(1) | evalvarn(vx);
    1372        9142 :   x = RgX_recip_shallow(x);
    1373        9143 :   if (dp)
    1374             :   { /* multiply by y[0]^dp   [beware dummy vars from FpX_FpXY_resultant] */
    1375        2147 :     GEN t = Flx_powu(gel(y,0), dp, p);
    1376        8595 :     for (i=2; i<lx; i++)
    1377        6444 :       gel(x,i) = Flx_mul(gel(x,i), t, p);
    1378             :   }
    1379        9147 :   return gerepilecopy(av, x);
    1380             : }
    1381             : 
    1382             : /* return a Flx */
    1383             : GEN
    1384        3001 : FlxX_resultant(GEN u, GEN v, ulong p, long sx)
    1385             : {
    1386        3001 :   pari_sp av = avma, av2;
    1387             :   long degq,dx,dy,du,dv,dr,signh;
    1388             :   GEN z,g,h,r,p1;
    1389             : 
    1390        3001 :   dx=degpol(u); dy=degpol(v); signh=1;
    1391        3003 :   if (dx < dy)
    1392             :   {
    1393          28 :     swap(u,v); lswap(dx,dy);
    1394          28 :     if (both_odd(dx, dy)) signh = -signh;
    1395             :   }
    1396        3003 :   if (dy < 0) return zero_Flx(sx);
    1397        3003 :   if (dy==0) return gerepileupto(av, Flx_powu(gel(v,2),dx,p));
    1398             : 
    1399        3003 :   g = h = pol1_Flx(sx); av2 = avma;
    1400             :   for(;;)
    1401             :   {
    1402        9141 :     r = FlxX_pseudorem(u,v,p); dr = lg(r);
    1403        9146 :     if (dr == 2) { avma = av; return zero_Flx(sx); }
    1404        9146 :     du = degpol(u); dv = degpol(v); degq = du-dv;
    1405        9146 :     u = v; p1 = g; g = leading_coeff(u);
    1406        9148 :     switch(degq)
    1407             :     {
    1408           0 :       case 0: break;
    1409             :       case 1:
    1410        6740 :         p1 = Flx_mul(h,p1, p); h = g; break;
    1411             :       default:
    1412        2408 :         p1 = Flx_mul(Flx_powu(h,degq,p), p1, p);
    1413        2408 :         h = Flx_div(Flx_powu(g,degq,p), Flx_powu(h,degq-1,p), p);
    1414             :     }
    1415        9140 :     if (both_odd(du,dv)) signh = -signh;
    1416        9141 :     v = FlxY_Flx_div(r, p1, p);
    1417        9142 :     if (dr==3) break;
    1418        6141 :     if (gc_needed(av2,1))
    1419             :     {
    1420           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_resultant, dr = %ld",dr);
    1421           0 :       gerepileall(av2,4, &u, &v, &g, &h);
    1422             :     }
    1423        6141 :   }
    1424        3001 :   z = gel(v,2);
    1425        3001 :   if (dv > 1) z = Flx_div(Flx_powu(z,dv,p), Flx_powu(h,dv-1,p), p);
    1426        3001 :   if (signh < 0) z = Flx_neg(z,p);
    1427        3001 :   return gerepileupto(av, z);
    1428             : }
    1429             : 
    1430             : /* Warning:
    1431             :  * This function switches between valid and invalid variable ordering*/
    1432             : 
    1433             : static GEN
    1434        3104 : FlxY_to_FlyX(GEN b, long sv)
    1435             : {
    1436        3104 :   long i, n=-1;
    1437        3104 :   long sw = b[1]&VARNBITS;
    1438        3104 :   for(i=2;i<lg(b);i++) n = maxss(n,lgpol(gel(b,i)));
    1439        3105 :   return Flm_to_FlxX(Flm_transpose(FlxX_to_Flm(b,n)),sv,sw);
    1440             : }
    1441             : 
    1442             : /* Return a Fly*/
    1443             : GEN
    1444        3104 : Flx_FlxY_resultant(GEN a, GEN b, ulong pp)
    1445             : {
    1446        3104 :   pari_sp ltop=avma;
    1447        3104 :   long dres = degpol(a)*degpol(b);
    1448        3106 :   long sx=a[1], sy=b[1]&VARNBITS;
    1449             :   GEN z;
    1450        3106 :   b = FlxY_to_FlyX(b,sx);
    1451        3105 :   if ((ulong)dres >= pp)
    1452        3001 :     z = FlxX_resultant(Fly_to_FlxY(a, sy), b, pp, sx);
    1453             :   else
    1454         104 :     z = Flx_FlxY_resultant_polint(a, b, pp, (ulong)dres, sy);
    1455        3105 :   return gerepileupto(ltop,z);
    1456             : }
    1457             : 
    1458             : /* return a t_POL (in variable v >= 0) whose coeffs are the coeffs of b,
    1459             :  * in variable v. This is an incorrect PARI object if initially varn(b) << v.
    1460             :  * We could return a vector of coeffs, but it is convenient to have degpol()
    1461             :  * and friends available. Even in that case, it will behave nicely with all
    1462             :  * functions treating a polynomial as a vector of coeffs (eg poleval).
    1463             :  * FOR INTERNAL USE! */
    1464             : GEN
    1465        9478 : swap_vars(GEN b0, long v)
    1466             : {
    1467        9478 :   long i, n = RgX_degree(b0, v);
    1468             :   GEN b, x;
    1469        9478 :   if (n < 0) return pol_0(v);
    1470        9478 :   b = cgetg(n+3, t_POL); x = b + 2;
    1471        9478 :   b[1] = evalsigne(1) | evalvarn(v);
    1472        9478 :   for (i=0; i<=n; i++) gel(x,i) = polcoeff_i(b0, i, v);
    1473        9478 :   return b;
    1474             : }
    1475             : 
    1476             : /* assume varn(b) << varn(a) */
    1477             : /* return a FpY*/
    1478             : GEN
    1479        3077 : FpX_FpXY_resultant(GEN a, GEN b, GEN p)
    1480             : {
    1481        3077 :   long i,n,dres, db, vY = varn(b), vX = varn(a);
    1482             :   GEN la,x,y;
    1483             : 
    1484        3077 :   if (lgefint(p) == 3)
    1485             :   {
    1486        3076 :     ulong pp = uel(p,2);
    1487        3076 :     b = ZXX_to_FlxX(b, pp, vX);
    1488        3074 :     a = ZX_to_Flx(a, pp);
    1489        3077 :     x = Flx_FlxY_resultant(a, b, pp);
    1490        3079 :     return Flx_to_ZX(x);
    1491             :   }
    1492           1 :   db = RgXY_degreex(b);
    1493           1 :   dres = degpol(a)*degpol(b);
    1494           1 :   la = leading_coeff(a);
    1495           1 :   x = cgetg(dres+2, t_VEC);
    1496           1 :   y = cgetg(dres+2, t_VEC);
    1497             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1498             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1499           3 :   for (i=0,n = 1; i < dres; n++)
    1500             :   {
    1501           2 :     gel(x,++i) = utoipos(n);
    1502           2 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1503           2 :     gel(x,++i) = subiu(p,n);
    1504           2 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1505             :   }
    1506           1 :   if (i == dres)
    1507             :   {
    1508           0 :     gel(x,++i) = gen_0;
    1509           0 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b, gel(x,i), p,la,db,vY);
    1510             :   }
    1511           1 :   return FpV_polint(x,y, p, vY);
    1512             : }
    1513             : 
    1514             : GEN
    1515         441 : FpX_direct_compositum(GEN a, GEN b, GEN p)
    1516             : {
    1517         441 :   GEN x = deg1pol_shallow(gen_1, pol_x(varn(a)), fetch_var_higher()); /* x+y */
    1518         441 :   x = FpX_FpXY_resultant(a, poleval(b,x),p);
    1519         441 :   (void)delete_var(); return x;
    1520             : }
    1521             : 
    1522             : /* 0, 1, -1, 2, -2, ... */
    1523             : #define next_lambda(a) (a>0 ? -a : 1-a)
    1524             : GEN
    1525           0 : FpX_compositum(GEN a, GEN b, GEN p)
    1526             : {
    1527           0 :   long k, v = fetch_var_higher();
    1528           0 :   for (k = 1;; k = next_lambda(k))
    1529             :   {
    1530           0 :     GEN x = deg1pol_shallow(gen_1, gmulsg(k, pol_x(v)), 0); /* x + k y */
    1531           0 :     GEN C = FpX_FpXY_resultant(a, poleval(b,x),p);
    1532           0 :     if (FpX_is_squarefree(C, p)) { (void)delete_var(); return C; }
    1533           0 :   }
    1534             : }
    1535             : 
    1536             : /* Assume A in Z[Y], B in Q[Y][X], and Res_Y(A, B) in Z[X].
    1537             :  * If lambda = NULL, return Res_Y(A,B).
    1538             :  * Otherwise, find a small lambda (start from *lambda, use the sequence above)
    1539             :  * such that R(X) = Res_Y(A(Y), B(X + lambda Y)) is squarefree, reset *lambda
    1540             :  * to the chosen value and return R
    1541             :  *
    1542             :  * If LERS is non-NULL, set it to the Last non-constant polynomial in the
    1543             :  * Euclidean Remainder Sequence */
    1544             : static GEN
    1545        1582 : ZX_ZXY_resultant_LERS(GEN A, GEN B0, long *plambda, GEN *LERS)
    1546             : {
    1547        1582 :   int checksqfree = plambda? 1: 0, stable;
    1548        1582 :   long lambda = plambda? *plambda: 0, cnt = 0;
    1549             :   ulong bound, dp;
    1550        1582 :   pari_sp av = avma, av2 = 0;
    1551        1582 :   long i,n, degA = degpol(A), degB, dres = degA*degpol(B0);
    1552        1582 :   long v = fetch_var_higher();
    1553        1582 :   long vX = varn(B0), vY = varn(A); /* assume vY has lower priority */
    1554        1582 :   long sX = evalvarn(vX);
    1555             :   GEN x, y, dglist, dB, B, q, a, b, ev, H, H0, H1, Hp, H0p, H1p, C0, C1;
    1556             :   forprime_t S;
    1557             : 
    1558        1582 :   dglist = Hp = H0p = H1p = C0 = C1 = NULL; /* gcc -Wall */
    1559        1582 :   if (LERS)
    1560             :   {
    1561        1582 :     if (!checksqfree)
    1562           0 :       pari_err_BUG("ZX_ZXY_resultant_all [LERS != NULL needs lambda]");
    1563        1582 :     C0 = cgetg(dres+2, t_VECSMALL);
    1564        1582 :     C1 = cgetg(dres+2, t_VECSMALL);
    1565        1582 :     dglist = cgetg(dres+1, t_VECSMALL);
    1566             :   }
    1567        1582 :   x = cgetg(dres+2, t_VECSMALL);
    1568        1582 :   y = cgetg(dres+2, t_VECSMALL);
    1569        1582 :   B0 = Q_remove_denom(B0, &dB);
    1570        1582 :   if (!dB) B0 = leafcopy(B0);
    1571        1582 :   A = leafcopy(A);
    1572        1582 :   B = B0;
    1573        1582 :   setvarn(A,v);
    1574             :   /* make sure p large enough */
    1575             : INIT:
    1576             :   /* always except the first time */
    1577        2317 :   if (av2) { avma = av2; lambda = next_lambda(lambda); }
    1578        2317 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    1579        2317 :   B = swap_vars(B, vY); setvarn(B,v);
    1580             :   /* B0(lambda v + x, v) */
    1581        2317 :   if (DEBUGLEVEL>4 && checksqfree) err_printf("Trying lambda = %ld\n", lambda);
    1582        2317 :   av2 = avma;
    1583             : 
    1584        2317 :   if (degA <= 3)
    1585             :   { /* sub-resultant faster for small degrees */
    1586        2121 :     if (LERS)
    1587             :     { /* implies checksqfree */
    1588        2121 :       H = RgX_resultant_all(A,B,&q);
    1589        2121 :       if (typ(q) != t_POL || degpol(q)!=1) goto INIT;
    1590        1435 :       H0 = gel(q,2);
    1591        1435 :       if (typ(H0) == t_POL) setvarn(H0,vX); else H0 = scalarpol(H0,vX);
    1592        1435 :       H1 = gel(q,3);
    1593        1435 :       if (typ(H1) == t_POL) setvarn(H1,vX); else H1 = scalarpol(H1,vX);
    1594             :     }
    1595             :     else
    1596           0 :       H = resultant(A,B);
    1597        1435 :     if (checksqfree && !ZX_is_squarefree(H)) goto INIT;
    1598        1393 :     if (dB) H = ZX_Z_divexact(H, powiu(dB, degA));
    1599        1393 :     goto END;
    1600             :   }
    1601             : 
    1602         196 :   H = H0 = H1 = NULL;
    1603         196 :   degB = degpol(B);
    1604         196 :   bound = ZX_ZXY_ResBound(A, B, dB);
    1605         196 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    1606         196 :   dp = 1;
    1607         196 :   init_modular_big(&S);
    1608             :   while (1)
    1609             :   {
    1610         397 :     ulong p = u_forprime_next(&S);
    1611         397 :     if (dB) { dp = umodiu(dB, p); if (!dp) continue; }
    1612             : 
    1613         397 :     a = ZX_to_Flx(A, p);
    1614         397 :     b = ZXX_to_FlxX(B, p, varn(A));
    1615         397 :     if (LERS)
    1616             :     {
    1617             :       GEN Hi;
    1618         397 :       if (degpol(a) < degA || degpol(b) < degB) continue; /* p | lc(A)lc(B) */
    1619         397 :       if (checksqfree)
    1620             :       { /* find degree list for generic Euclidean Remainder Sequence */
    1621         196 :         long goal = minss(degpol(a), degpol(b)); /* longest possible */
    1622         196 :         for (n=1; n <= goal; n++) dglist[n] = 0;
    1623         196 :         setlg(dglist, 1);
    1624        1645 :         for (n=0; n <= dres; n++)
    1625             :         {
    1626        1561 :           ev = FlxY_evalx_drop(b, n, p);
    1627        1561 :           Flx_resultant_set_dglist(a, ev, dglist, p);
    1628        1561 :           if (lg(dglist)-1 == goal) break;
    1629             :         }
    1630             :         /* last pol in ERS has degree > 1 ? */
    1631         196 :         goal = lg(dglist)-1;
    1632         196 :         if (degpol(B) == 1) { if (!goal) goto INIT; }
    1633             :         else
    1634             :         {
    1635         189 :           if (goal <= 1) goto INIT;
    1636         182 :           if (dglist[goal] != 0 || dglist[goal-1] != 1) goto INIT;
    1637             :         }
    1638         189 :         if (DEBUGLEVEL>4)
    1639           0 :           err_printf("Degree list for ERS (trials: %ld) = %Ps\n",n+1,dglist);
    1640             :       }
    1641             : 
    1642        7386 :       for (i=0,n = 0; i <= dres; n++)
    1643             :       {
    1644        6996 :         ev = FlxY_evalx_drop(b, n, p);
    1645        6996 :         x[++i] = n; y[i] = Flx_resultant_all(a, ev, C0+i, C1+i, dglist, p);
    1646        6996 :         if (!C1[i]) i--; /* C1(i) = 0. No way to recover C0(i) */
    1647             :       }
    1648         390 :       Hi = Flv_Flm_polint(x, mkvec3(y,C0,C1), p, 0);
    1649         390 :       Hp = gel(Hi,1); H0p = gel(Hi,2); H1p = gel(Hi,3);
    1650             :     }
    1651             :     else
    1652             :     {
    1653           0 :       long dropa = degA - degpol(a), dropb = degB - degpol(b);
    1654           0 :       Hp = Flx_FlxY_resultant_polint(a, b, p, (ulong)dres, sX);
    1655           0 :       if (dropa && dropb)
    1656           0 :         Hp = zero_Flx(sX);
    1657             :       else {
    1658           0 :         if (dropa)
    1659             :         { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    1660           0 :           GEN c = gel(b,degB+2); /* lc(B) */
    1661           0 :           if (odd(degB)) c = Flx_neg(c, p);
    1662           0 :           if (!Flx_equal1(c)) {
    1663           0 :             c = Flx_powu(c, dropa, p);
    1664           0 :             if (!Flx_equal1(c)) Hp = Flx_mul(Hp, c, p);
    1665             :           }
    1666             :         }
    1667           0 :         else if (dropb)
    1668             :         { /* multiply by lc(A)^(deg B - deg b) */
    1669           0 :           ulong c = a[degA+2]; /* lc(A) */
    1670           0 :           c = Fl_powu(c, dropb, p);
    1671           0 :           if (c != 1) Hp = Flx_Fl_mul(Hp, c, p);
    1672             :         }
    1673             :       }
    1674             :     }
    1675         390 :     if (!H && degpol(Hp) != dres) continue;
    1676         390 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1677         390 :     if (checksqfree) {
    1678         189 :       if (!Flx_is_squarefree(Hp, p)) goto INIT;
    1679         189 :       if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    1680         189 :       checksqfree = 0;
    1681             :     }
    1682             : 
    1683         390 :     if (!H)
    1684             :     { /* initialize */
    1685         189 :       q = utoipos(p); stable = 0;
    1686         189 :       H = ZX_init_CRT(Hp, p,vX);
    1687         189 :       if (LERS) {
    1688         189 :         H0= ZX_init_CRT(H0p, p,vX);
    1689         189 :         H1= ZX_init_CRT(H1p, p,vX);
    1690             :       }
    1691             :     }
    1692             :     else
    1693             :     {
    1694         201 :       if (LERS) {
    1695         201 :         GEN qp = muliu(q,p);
    1696         402 :         stable  = ZX_incremental_CRT_raw(&H, Hp, q,qp, p)
    1697         201 :                 & ZX_incremental_CRT_raw(&H0,H0p, q,qp, p)
    1698         201 :                 & ZX_incremental_CRT_raw(&H1,H1p, q,qp, p);
    1699         201 :         q = qp;
    1700             :       }
    1701             :       else
    1702           0 :         stable = ZX_incremental_CRT(&H, Hp, &q, p);
    1703             :     }
    1704             :     /* could make it probabilistic for H ? [e.g if stable twice, etc]
    1705             :      * Probabilistic anyway for H0, H1 */
    1706         390 :     if (DEBUGLEVEL>5 && (stable ||  ++cnt==100))
    1707           0 :     { cnt=0; err_printf("%ld%%%s ",100*expi(q)/bound,stable?"s":""); }
    1708         390 :     if (stable && (ulong)expi(q) >= bound) break; /* DONE */
    1709         201 :     if (gc_needed(av,2))
    1710             :     {
    1711           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_rnfequation");
    1712           0 :       gerepileall(av2, LERS? 4: 2, &H, &q, &H0, &H1);
    1713             :     }
    1714         201 :   }
    1715             : END:
    1716        1582 :   if (DEBUGLEVEL>5) err_printf(" done\n");
    1717        1582 :   setvarn(H, vX); (void)delete_var();
    1718        1582 :   if (plambda) *plambda = lambda;
    1719        1582 :   if (LERS)
    1720             :   {
    1721        1582 :     *LERS = mkvec2(H0,H1);
    1722        1582 :     gerepileall(av, 2, &H, LERS);
    1723        1582 :     return H;
    1724             :   }
    1725           0 :   return gerepilecopy(av, H);
    1726             : }
    1727             : 
    1728             : GEN
    1729        2590 : ZX_ZXY_resultant_all(GEN A, GEN B, long *plambda, GEN *LERS)
    1730             : {
    1731        2590 :   if (LERS)
    1732        1582 :     return ZX_ZXY_resultant_LERS(A, B, plambda, LERS);
    1733        1008 :   return ZX_ZXY_rnfequation(A, B, plambda);
    1734             : }
    1735             : 
    1736             : /* If lambda = NULL, return caract(Mod(A, T)), T,A in Z[X].
    1737             :  * Otherwise find a small lambda such that caract (Mod(A + lambda X, T)) is
    1738             :  * squarefree */
    1739             : GEN
    1740        1862 : ZXQ_charpoly_sqf(GEN A, GEN T, long *lambda, long v)
    1741             : {
    1742        1862 :   pari_sp av = avma;
    1743             :   GEN R, a;
    1744             :   long dA;
    1745             :   int delvar;
    1746             : 
    1747        1862 :   if (v < 0) v = 0;
    1748        1862 :   switch (typ(A))
    1749             :   {
    1750        1862 :     case t_POL: dA = degpol(A); if (dA > 0) break;
    1751           0 :       A = constant_coeff(A);
    1752             :     default:
    1753           0 :       if (lambda) { A = scalar_ZX_shallow(A,varn(T)); dA = 0; break;}
    1754           0 :       return gerepileupto(av, gpowgs(gsub(pol_x(v), A), degpol(T)));
    1755             :   }
    1756        1862 :   delvar = 0;
    1757        1862 :   if (varn(T) == 0)
    1758             :   {
    1759        1806 :     long v0 = fetch_var(); delvar = 1;
    1760        1806 :     T = leafcopy(T); setvarn(T,v0);
    1761        1806 :     A = leafcopy(A); setvarn(A,v0);
    1762             :   }
    1763        1862 :   R = ZX_ZXY_rnfequation(T, deg1pol_shallow(gen_1, gneg_i(A), 0), lambda);
    1764        1862 :   if (delvar) (void)delete_var();
    1765        1862 :   setvarn(R, v); a = leading_coeff(T);
    1766        1862 :   if (!gequal1(a)) R = gdiv(R, powiu(a, dA));
    1767        1862 :   return gerepileupto(av, R);
    1768             : }
    1769             : 
    1770             : 
    1771             : /* charpoly(Mod(A,T)), A may be in Q[X], but assume T and result are integral */
    1772             : GEN
    1773       11486 : ZXQ_charpoly(GEN A, GEN T, long v)
    1774             : {
    1775       11486 :   return (degpol(T) < 16) ? RgXQ_charpoly(A,T,v): ZXQ_charpoly_sqf(A,T, NULL, v);
    1776             : }
    1777             : 
    1778             : GEN
    1779         819 : QXQ_charpoly(GEN A, GEN T, long v)
    1780             : {
    1781         819 :   pari_sp av = avma;
    1782         819 :   GEN den, B = Q_remove_denom(A, &den);
    1783         819 :   GEN P = ZXQ_charpoly(B, T, v);
    1784         819 :   return gerepilecopy(av, den ? RgX_rescale(P, ginv(den)): P);
    1785             : }
    1786             : 
    1787             : static GEN
    1788      155555 : trivial_case(GEN A, GEN B)
    1789             : {
    1790             :   long d;
    1791      155555 :   if (typ(A) == t_INT) return powiu(A, degpol(B));
    1792      148078 :   d = degpol(A);
    1793      148078 :   if (d == 0) return trivial_case(gel(A,2),B);
    1794      145011 :   if (d < 0) return gen_0;
    1795      144996 :   return NULL;
    1796             : }
    1797             : 
    1798             : static long
    1799       76123 : get_nbprimes(ulong bound, ulong *pt_start)
    1800             : {
    1801             : #ifdef LONG_IS_64BIT
    1802       65124 :   ulong pstart = 4611686018427388039UL;
    1803             : #else
    1804       10999 :   ulong pstart = 1073741827UL;
    1805             : #endif
    1806       76123 :   *pt_start = pstart;
    1807       76123 :   return (bound/expu(pstart))+1;
    1808             : }
    1809             : 
    1810             : static ulong
    1811     1287615 : ZX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, ulong p)
    1812             : {
    1813     1287615 :   pari_sp av = avma;
    1814             :   ulong H;
    1815             :   long dropa, dropb;
    1816     1287615 :   ulong dp = dB ? umodiu(dB, p): 1;
    1817     1287680 :   if (!b) b = Flx_deriv(a, p);
    1818     1287619 :   dropa = degA - degpol(a);
    1819     1287625 :   dropb = degB - degpol(b);
    1820     1287624 :   if (dropa && dropb) /* p | lc(A), p | lc(B) */
    1821           0 :   { avma = av; return 0; }
    1822     1287624 :   H = Flx_resultant(a, b, p);
    1823     1287473 :   if (dropa)
    1824             :   { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    1825           0 :     ulong c = b[degB+2]; /* lc(B) */
    1826           0 :     if (odd(degB)) c = p - c;
    1827           0 :     c = Fl_powu(c, dropa, p);
    1828           0 :     if (c != 1) H = Fl_mul(H, c, p);
    1829             :   }
    1830     1287473 :   else if (dropb)
    1831             :   { /* multiply by lc(A)^(deg B - deg b) */
    1832           0 :     ulong c = a[degA+2]; /* lc(A) */
    1833           0 :     c = Fl_powu(c, dropb, p);
    1834           0 :     if (c != 1) H = Fl_mul(H, c, p);
    1835             :   }
    1836     1287466 :   if (dp != 1) H = Fl_mul(H, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1837     1287468 :   avma = av; return H;
    1838             : }
    1839             : 
    1840             : /* If B=NULL, assume B=A' */
    1841             : static GEN
    1842      557287 : ZX_resultant_slice(GEN A, GEN B, GEN dB, GEN P, GEN *mod)
    1843             : {
    1844      557287 :   pari_sp av = avma;
    1845      557287 :   long degA, degB, i, n = lg(P)-1;
    1846             :   GEN H, T;
    1847             : 
    1848      557287 :   degA = degpol(A);
    1849      557302 :   degB = B ? degpol(B): degA - 1;
    1850      557349 :   if (n == 1)
    1851             :   {
    1852      177622 :     ulong Hp, p = uel(P,1);
    1853             :     GEN a, b;
    1854      177622 :     a = ZX_to_Flx(A, p), b = B ? ZX_to_Flx(B, p): NULL;
    1855      177590 :     Hp = ZX_resultant_prime(a, b, dB, degA, degB, p);
    1856      177624 :     avma = av;
    1857      177624 :     *mod = utoi(p); return utoi(Hp);
    1858             :   }
    1859      379727 :   T = ZV_producttree(P);
    1860      379700 :   A = ZX_nv_mod_tree(A, P, T);
    1861      379668 :   if (B) B = ZX_nv_mod_tree(B, P, T);
    1862      379668 :   H = cgetg(n+1, t_VECSMALL);
    1863     1489700 :   for(i=1; i <= n; i++)
    1864             :   {
    1865     1110017 :     ulong p = P[i];
    1866     1110017 :     GEN a = gel(A,i), b = B? gel(B,i): NULL;
    1867     1110017 :     H[i] = ZX_resultant_prime(a, b, dB, degA, degB, p);
    1868             :   }
    1869      379683 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    1870      379698 :   *mod = gmael(T, lg(T)-1, 1);
    1871      379698 :   gerepileall(av, 2, &H, mod);
    1872      379727 :   return H;
    1873             : }
    1874             : 
    1875             : GEN
    1876      503374 : ZX_resultant_worker(GEN P, GEN A, GEN B, GEN dB)
    1877             : {
    1878      503374 :   GEN V = cgetg(3, t_VEC);
    1879      503439 :   if (isintzero(B)) B = NULL;
    1880      503422 :   if (isintzero(dB)) dB = NULL;
    1881      503432 :   gel(V,1) = ZX_resultant_slice(A,B,dB,P,&gel(V,2));
    1882      503316 :   return V;
    1883             : }
    1884             : 
    1885             : static GEN
    1886      561637 : primelist_disc(ulong *p, long n, GEN dB)
    1887             : {
    1888      561637 :   GEN P = cgetg(n+1, t_VECSMALL);
    1889             :   long i;
    1890     1859116 :   for (i=1; i <= n; i++, *p = unextprime(*p+1))
    1891             :   {
    1892     1297479 :     if (dB && umodiu(dB, *p)==0) { i--; continue; }
    1893     1297479 :     P[i] = *p;
    1894             :   }
    1895      561637 :   return P;
    1896             : }
    1897             : 
    1898             : /* Res(A, B/dB), assuming the A,B in Z[X] and result is integer */
    1899             : /* if B=NULL, take B = A' */
    1900             : GEN
    1901       79415 : ZX_resultant_all(GEN A, GEN B, GEN dB, ulong bound)
    1902             : {
    1903             :   ulong p;
    1904       79415 :   pari_sp av = avma;
    1905             :   long n, m;
    1906             :   GEN  H, P, mod;
    1907       79415 :   int is_disc = !B;
    1908       79415 :   if (is_disc) B = ZX_deriv(A);
    1909             : 
    1910       79415 :   if ((H = trivial_case(A,B)) || (H = trivial_case(B,A))) return H;
    1911       71923 :   if (!bound) bound = ZX_ZXY_ResBound(A, B, dB);
    1912       71923 :   n = get_nbprimes(bound+1, &p);/* +1 to account for sign */
    1913       71923 :   if (is_disc)
    1914       47700 :     B = NULL;
    1915       71923 :   m = minss(degpol(A)+(B ? degpol(B): 0), n);
    1916       71923 :   if (m == 1)
    1917             :   {
    1918       33646 :     GEN P = primelist_disc(&p, n, dB);
    1919       33646 :     H = ZX_resultant_slice(A, B, dB, P, &mod);
    1920             :   }
    1921             :   else
    1922             :   {
    1923       38277 :     long i, s = n/m, r = n - m*s, di = 0;
    1924       38277 :     GEN worker = strtoclosure("_ZX_resultant_worker", 3, A, B?B:gen_0, dB?dB:gen_0);
    1925             :     struct pari_mt pt;
    1926             :     long pending;
    1927       38277 :     if (DEBUGLEVEL > 4)
    1928           0 :       err_printf("ZX_resultant: bound 2^%ld, nb primes: %ld\n",bound, n);
    1929       38277 :     H = cgetg(m+1+!!r, t_VEC); P = cgetg(m+1+!!r, t_VEC);
    1930       38277 :     mt_queue_start_lim(&pt, worker, m);
    1931      588798 :     for (i=1; i<=m || pending; i++)
    1932             :     {
    1933             :       GEN done;
    1934      550521 :       mt_queue_submit(&pt, i, i<=m ? mkvec(primelist_disc(&p, s, dB)): NULL);
    1935      550521 :       done = mt_queue_get(&pt, NULL, &pending);
    1936      550521 :       if (done)
    1937             :       {
    1938      503490 :         di++;
    1939      503490 :         gel(H, di) = gel(done,1);
    1940      503490 :         gel(P, di) = gel(done,2);
    1941      503490 :         if (DEBUGLEVEL>5) err_printf("%ld%% ",100*di/m);
    1942             :       }
    1943             :     }
    1944       38277 :     mt_queue_end(&pt);
    1945       38277 :     if (r)
    1946             :     {
    1947       20301 :       GEN Pr = primelist_disc(&p, r, dB);
    1948       20301 :       gel(H, m+1) = ZX_resultant_slice(A, B, dB, Pr, &gel(P, m+1));
    1949             :     }
    1950       38277 :     H = ZV_chinese(H, P, &mod);
    1951       38277 :     if (DEBUGLEVEL>5) err_printf("done\n");
    1952             :   }
    1953       71923 :   H = Fp_center(H, mod, shifti(mod,-1));
    1954       71923 :   return gerepileuptoint(av, H);
    1955             : }
    1956             : 
    1957             : /* A0 and B0 in Q[X] */
    1958             : GEN
    1959       10530 : QX_resultant(GEN A0, GEN B0)
    1960             : {
    1961             :   GEN s, a, b, A, B;
    1962       10530 :   pari_sp av = avma;
    1963             : 
    1964       10530 :   A = Q_primitive_part(A0, &a);
    1965       10530 :   B = Q_primitive_part(B0, &b);
    1966       10530 :   s = ZX_resultant(A, B);
    1967       10530 :   if (!signe(s)) { avma = av; return gen_0; }
    1968       10530 :   if (a) s = gmul(s, gpowgs(a,degpol(B)));
    1969       10530 :   if (b) s = gmul(s, gpowgs(b,degpol(A)));
    1970       10530 :   return gerepileupto(av, s);
    1971             : }
    1972             : 
    1973             : GEN
    1974       31070 : ZX_resultant(GEN A, GEN B) { return ZX_resultant_all(A,B,NULL,0); }
    1975             : 
    1976             : GEN
    1977           0 : QXQ_intnorm(GEN A, GEN B)
    1978             : {
    1979             :   GEN c, n, R, lB;
    1980           0 :   long dA = degpol(A), dB = degpol(B);
    1981           0 :   pari_sp av = avma;
    1982           0 :   if (dA < 0) return gen_0;
    1983           0 :   A = Q_primitive_part(A, &c);
    1984           0 :   if (!c || typ(c) == t_INT) {
    1985           0 :     n = c;
    1986           0 :     R = ZX_resultant(B, A);
    1987             :   } else {
    1988           0 :     n = gel(c,1);
    1989           0 :     R = ZX_resultant_all(B, A, gel(c,2), 0);
    1990             :   }
    1991           0 :   if (n && !equali1(n)) R = mulii(R, powiu(n, dB));
    1992           0 :   lB = leading_coeff(B);
    1993           0 :   if (!equali1(lB)) R = diviiexact(R, powiu(lB, dA));
    1994           0 :   return gerepileuptoint(av, R);
    1995             : }
    1996             : 
    1997             : GEN
    1998           0 : QXQ_norm(GEN A, GEN B)
    1999             : {
    2000             :   GEN c, R, lB;
    2001           0 :   long dA = degpol(A), dB = degpol(B);
    2002           0 :   pari_sp av = avma;
    2003           0 :   if (dA < 0) return gen_0;
    2004           0 :   A = Q_primitive_part(A, &c);
    2005           0 :   R = ZX_resultant(B, A);
    2006           0 :   if (c) R = gmul(R, gpowgs(c, dB));
    2007           0 :   lB = leading_coeff(B);
    2008           0 :   if (!equali1(lB)) R = gdiv(R, gpowgs(lB, dA));
    2009           0 :   return gerepileupto(av, R);
    2010             : }
    2011             : 
    2012             : /* assume x has integral coefficients */
    2013             : GEN
    2014       49037 : ZX_disc_all(GEN x, ulong bound)
    2015             : {
    2016       49037 :   pari_sp av = avma;
    2017             :   GEN l, R;
    2018       49037 :   long s, d = degpol(x);
    2019       49037 :   if (d <= 1) return d ? gen_1: gen_0;
    2020       47700 :   s = (d & 2) ? -1: 1;
    2021       47700 :   l = leading_coeff(x);
    2022       47700 :   R = ZX_resultant_all(x, NULL, NULL, bound);
    2023       47700 :   if (is_pm1(l))
    2024       44893 :   { if (signe(l) < 0) s = -s; }
    2025             :   else
    2026        2807 :     R = diviiexact(R,l);
    2027       47700 :   if (s == -1) togglesign_safe(&R);
    2028       47700 :   return gerepileuptoint(av,R);
    2029             : }
    2030       48008 : GEN ZX_disc(GEN x) { return ZX_disc_all(x,0); }
    2031             : 
    2032             : GEN
    2033           0 : QX_disc(GEN x)
    2034             : {
    2035           0 :   pari_sp av = avma;
    2036           0 :   GEN c, d = ZX_disc( Q_primitive_part(x, &c) );
    2037           0 :   if (c) d = gmul(d, gpowgs(c, 2*degpol(x) - 2));
    2038           0 :   return gerepileupto(av, d);
    2039             : }
    2040             : 
    2041             : /* lift(1 / Mod(A,B)). B a ZX, A a scalar or a QX */
    2042             : GEN
    2043       24252 : QXQ_inv(GEN A, GEN B)
    2044             : {
    2045             :   GEN D, cU, q, U, V;
    2046             :   ulong p;
    2047       24252 :   pari_sp av2, av = avma;
    2048             :   forprime_t S;
    2049             :   pari_timer ti;
    2050       24252 :   if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
    2051             :   /* A a QX, B a ZX */
    2052       24252 :   A = Q_primitive_part(A, &D);
    2053             :   /* A, B in Z[X] */
    2054       24252 :   init_modular_small(&S);
    2055       24252 :   if (DEBUGLEVEL>5) timer_start(&ti);
    2056       24252 :   av2 = avma; U = NULL;
    2057      133888 :   while ((p = u_forprime_next(&S)))
    2058             :   {
    2059             :     GEN a, b, qp, Up, Vp;
    2060             :     int stable;
    2061             : 
    2062      109636 :     a = ZX_to_Flx(A, p);
    2063      109636 :     b = ZX_to_Flx(B, p);
    2064             :     /* if p | Res(A/G, B/G), discard */
    2065      133881 :     if (!Flx_extresultant(b,a,p, &Vp,&Up)) continue;
    2066             : 
    2067      109629 :     if (!U)
    2068             :     { /* First time */
    2069       24245 :       U = ZX_init_CRT(Up,p,varn(A));
    2070       24245 :       V = ZX_init_CRT(Vp,p,varn(A));
    2071       24245 :       q = utoipos(p); continue;
    2072             :     }
    2073       85384 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: mod %ld (bound 2^%ld)", p,expi(q));
    2074       85384 :     qp = muliu(q,p);
    2075      170768 :     stable = ZX_incremental_CRT_raw(&U, Up, q,qp, p)
    2076       85384 :            & ZX_incremental_CRT_raw(&V, Vp, q,qp, p);
    2077       85384 :     if (stable)
    2078             :     { /* all stable: check divisibility */
    2079       24245 :       GEN res = ZX_add(ZX_mul(A,U), ZX_mul(B,V));
    2080       24245 :       if (degpol(res) == 0) {
    2081       24245 :         res = gel(res,2);
    2082       24245 :         D = D? gmul(D, res): res;
    2083       48490 :         break;
    2084             :       } /* DONE */
    2085           0 :       if (DEBUGLEVEL) err_printf("QXQ_inv: char 0 check failed");
    2086             :     }
    2087       61139 :     q = qp;
    2088       61139 :     if (gc_needed(av,1))
    2089             :     {
    2090          21 :       if (DEBUGMEM>1) pari_warn(warnmem,"QXQ_inv");
    2091          21 :       gerepileall(av2, 3, &q,&U,&V);
    2092             :     }
    2093             :   }
    2094       24245 :   if (!p) pari_err_OVERFLOW("QXQ_inv [ran out of primes]");
    2095       24245 :   cU = ZX_content(U);
    2096       24245 :   if (!is_pm1(cU)) { U = Q_div_to_int(U, cU); D = gdiv(D, cU); }
    2097       24245 :   return gerepileupto(av, RgX_Rg_div(U, D));
    2098             : }
    2099             : 
    2100             : /************************************************************************
    2101             :  *                                                                      *
    2102             :  *                   ZX_ZXY_resultant                                   *
    2103             :  *                                                                      *
    2104             :  ************************************************************************/
    2105             : 
    2106             : static GEN
    2107       12545 : ZX_ZXY_resultant_prime(GEN a, GEN b, ulong dp, ulong p, long degA, long degB, long dres, long sX)
    2108             : {
    2109       12545 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    2110       12549 :   GEN Hp = Flx_FlxY_resultant_polint(a, b, p, dres, sX);
    2111       12559 :   if (dropa && dropb)
    2112           0 :     Hp = zero_Flx(sX);
    2113             :   else {
    2114       12559 :     if (dropa)
    2115             :     { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    2116           0 :       GEN c = gel(b,degB+2); /* lc(B) */
    2117           0 :       if (odd(degB)) c = Flx_neg(c, p);
    2118           0 :       if (!Flx_equal1(c)) {
    2119           0 :         c = Flx_powu(c, dropa, p);
    2120           0 :         if (!Flx_equal1(c)) Hp = Flx_mul(Hp, c, p);
    2121             :       }
    2122             :     }
    2123       12559 :     else if (dropb)
    2124             :     { /* multiply by lc(A)^(deg B - deg b) */
    2125           0 :       ulong c = uel(a, degA+2); /* lc(A) */
    2126           0 :       c = Fl_powu(c, dropb, p);
    2127           0 :       if (c != 1) Hp = Flx_Fl_mul(Hp, c, p);
    2128             :     }
    2129             :   }
    2130       12558 :   if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    2131       12558 :   return Hp;
    2132             : }
    2133             : 
    2134             : GEN
    2135        9619 : ZX_ZXY_resultant_worker(GEN a, GEN b, ulong dp, ulong p, GEN v)
    2136             : {
    2137        9619 :   return ZX_ZXY_resultant_prime(a, b, dp, p, v[1], v[2], v[3], v[4]);
    2138             : }
    2139             : 
    2140             : static GEN
    2141        4200 : ZX_ZXY_resultant_slice(GEN A, GEN B, GEN dB, long degA, long degB, long dres,
    2142             :                        GEN P, GEN *mod, long sX, long vY)
    2143             : {
    2144        4200 :   long i, n = lg(P)-1, di = 0, pending;
    2145             :   GEN H, T, R, D;
    2146        4200 :   GEN worker = strtoclosure("_ZX_ZXY_resultant_worker", 1, mkvecsmall4(degA, degB, dres, sX));
    2147             :   struct pari_mt pt;
    2148        4200 :   T = ZV_producttree(P);
    2149        4200 :   R = ZV_chinesetree(P, T);
    2150        4200 :   A = ZX_nv_mod_tree(A, P, T);
    2151        4200 :   B = ZXX_nv_mod_tree(B, P, T, vY);
    2152        4200 :   D = dB ? Z_ZV_mod_tree(dB, P, T): NULL;
    2153        4200 :   H = cgetg(n+1, t_VEC);
    2154        4200 :   mt_queue_start_lim(&pt, worker, n);
    2155       14465 :   for (i=1; i<=n || pending; i++)
    2156             :   {
    2157             :     GEN done;
    2158       19899 :     mt_queue_submit(&pt, i,
    2159        9634 :       i<=n ? mkvec4(gel(A,i), gel(B,i), D ? utoi(uel(D, i)): gen_1, utoi(uel(P,i)))
    2160             :            : NULL);
    2161       10265 :     done = mt_queue_get(&pt, &di, &pending);
    2162       10265 :     if(done)
    2163             :     {
    2164        9634 :       gel(H,di) = done;
    2165        9634 :       if (DEBUGLEVEL>5) err_printf("%ld%% ",100*di/n);
    2166             :     }
    2167             :   }
    2168        4200 :   mt_queue_end(&pt);
    2169        4200 :   if (mod) *mod = gmael(T, lg(T)-1, 1);
    2170        4200 :   return nxV_chinese_center_tree(H, P, T, R);
    2171             : }
    2172             : 
    2173             : GEN
    2174        4200 : ZX_ZXY_resultant(GEN A, GEN B)
    2175             : {
    2176        4200 :   pari_sp av = avma;
    2177             :   ulong bound;
    2178        4200 :   long n, v = fetch_var_higher();
    2179        4200 :   long degA = degpol(A), degB, dres = degA * degpol(B);
    2180        4200 :   long vX = varn(B), vY = varn(A); /* assume vY has lower priority */
    2181        4200 :   long sX = evalvarn(vX);
    2182             :   GEN dB, H, P;
    2183             :   ulong p;
    2184        4200 :   B = Q_remove_denom(B, &dB);
    2185        4200 :   if (!dB) B = leafcopy(B);
    2186        4200 :   A = leafcopy(A); setvarn(A,v);
    2187        4200 :   B = swap_vars(B, vY); setvarn(B,v); degB = degpol(B);
    2188        4200 :   bound = ZX_ZXY_ResBound(A, B, dB);
    2189        4200 :   n = get_nbprimes(bound+1, &p);/* +1 to account for sign */
    2190        4200 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    2191             : 
    2192        4200 :   P = primelist_disc(&p, n, dB);
    2193        4200 :   H = ZX_ZXY_resultant_slice(A, B, dB, degA, degB, dres, P, NULL, sX, vY);
    2194        4200 :   setvarn(H, vX); (void)delete_var();
    2195        4200 :   return gerepilecopy(av, H);
    2196             : }
    2197             : 
    2198             : static long
    2199        2443 : ZX_ZXY_rnfequation_lambda(GEN A, GEN B0, long lambda)
    2200             : {
    2201        2443 :   pari_sp av = avma;
    2202        2443 :   long degA = degpol(A), degB, dres = degA*degpol(B0);
    2203        2443 :   long v = fetch_var_higher();
    2204        2443 :   long vX = varn(B0), vY = varn(A); /* assume vY has lower priority */
    2205        2443 :   long sX = evalvarn(vX);
    2206             :   GEN dB, B, a, b, Hp;
    2207             :   forprime_t S;
    2208             : 
    2209        2443 :   B0 = Q_remove_denom(B0, &dB);
    2210        2443 :   if (!dB) B0 = leafcopy(B0);
    2211        2443 :   A = leafcopy(A);
    2212        2443 :   B = B0;
    2213        2443 :   setvarn(A,v);
    2214             : INIT:
    2215        2926 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    2216        2926 :   B = swap_vars(B, vY); setvarn(B,v);
    2217             :   /* B0(lambda v + x, v) */
    2218        2926 :   if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    2219             : 
    2220        2926 :   degB = degpol(B);
    2221        2926 :   init_modular_big(&S);
    2222             :   while (1)
    2223             :   {
    2224        2926 :     ulong p = u_forprime_next(&S);
    2225        2926 :     ulong dp = dB ? umodiu(dB, p): 1;
    2226        2926 :     if (!dp) continue;
    2227        2926 :     a = ZX_to_Flx(A, p);
    2228        2926 :     b = ZXX_to_FlxX(B, p, v);
    2229        2926 :     Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2230        2926 :     if (degpol(Hp) != dres) continue;
    2231        2926 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    2232        2926 :     if (!Flx_is_squarefree(Hp, p)) { lambda = next_lambda(lambda); goto INIT; }
    2233        2443 :     if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    2234        4886 :     avma = av; (void)delete_var(); return lambda;
    2235           0 :   }
    2236             : }
    2237             : 
    2238             : GEN
    2239        2996 : ZX_ZXY_rnfequation(GEN A, GEN B, long *lambda)
    2240             : {
    2241        2996 :   if (lambda)
    2242             :   {
    2243        2443 :     *lambda = ZX_ZXY_rnfequation_lambda(A, B, *lambda);
    2244        2443 :     B = RgX_translate(B, monomial(stoi(*lambda), 1, varn(A)));
    2245             :   }
    2246        2996 :   return ZX_ZXY_resultant(A,B);
    2247             : }
    2248             : 
    2249             : /************************************************************************
    2250             :  *                                                                      *
    2251             :  *                   IRREDUCIBLE POLYNOMIAL / Fp                        *
    2252             :  *                                                                      *
    2253             :  ************************************************************************/
    2254             : 
    2255             : /* irreducible (unitary) polynomial of degree n over Fp */
    2256             : GEN
    2257           0 : ffinit_rand(GEN p,long n)
    2258             : {
    2259             :   for(;;) {
    2260           0 :     pari_sp av = avma;
    2261           0 :     GEN pol = ZX_add(pol_xn(n, 0), random_FpX(n-1,0, p));
    2262           0 :     if (FpX_is_irred(pol, p)) return pol;
    2263           0 :     avma = av;
    2264           0 :   }
    2265             : }
    2266             : 
    2267             : /* return an extension of degree 2^l of F_2, assume l > 0
    2268             :  * Not stack clean. */
    2269             : static GEN
    2270         587 : f2init(long l)
    2271             : {
    2272             :   GEN Q, T, S;
    2273             :   long i, v;
    2274             : 
    2275         587 :   if (l == 1) return polcyclo(3, 0);
    2276         552 :   v = fetch_var_higher();
    2277         552 :   S = mkpoln(4, gen_1,gen_1,gen_0,gen_0); /* y(y^2 + y) */
    2278         552 :   Q = mkpoln(3, gen_1,gen_1, S); /* x^2 + x + y(y^2+y) */
    2279         553 :   setvarn(Q, v);
    2280             : 
    2281             :   /* x^4+x+1, irred over F_2, minimal polynomial of a root of Q */
    2282         553 :   T = mkpoln(5, gen_1,gen_0,gen_0,gen_1,gen_1);
    2283         554 :   setvarn(T, v);
    2284             :   /* Q = x^2 + x + a(y) irred. over K = F2[y] / (T(y))
    2285             :    * ==> x^2 + x + a(y) b irred. over K for any root b of Q
    2286             :    * ==> x^2 + x + (b^2+b)b */
    2287         554 :   for (i=2; i<l; i++) T = FpX_FpXY_resultant(T, Q, gen_2); /* minpoly(b) / F2*/
    2288         553 :   (void)delete_var(); setvarn(T,0); return T;
    2289             : }
    2290             : 
    2291             : /* return an extension of degree p^l of F_p, assume l > 0
    2292             :  * Not stack clean. */
    2293             : GEN
    2294           0 : ffinit_Artin_Shreier(GEN ip, long l)
    2295             : {
    2296           0 :   long i, v, p = itos(ip);
    2297           0 :   GEN T, Q, xp = pol_xn(p,0); /* x^p */
    2298           0 :   T = ZX_sub(xp, deg1pol_shallow(gen_1,gen_1,0)); /* x^p - x - 1 */
    2299           0 :   if (l == 1) return T;
    2300             : 
    2301           0 :   v = fetch_var_higher();
    2302           0 :   setvarn(xp, v);
    2303           0 :   Q = ZX_sub(pol_xn(2*p-1,0), pol_xn(p,0));
    2304           0 :   Q = gsub(xp, deg1pol_shallow(gen_1, Q, v)); /* x^p - x - (y^(2p-1)-y^p) */
    2305           0 :   for (i = 2; i <= l; ++i) T = FpX_FpXY_resultant(T, Q, ip);
    2306           0 :   (void)delete_var(); setvarn(T,0); return T;
    2307             : }
    2308             : 
    2309             : /* check if polsubcyclo(n,l,0) is irreducible modulo p */
    2310             : static long
    2311       12298 : fpinit_check(GEN p, long n, long l)
    2312             : {
    2313             :   ulong q;
    2314       12298 :   if (!uisprime(n)) return 0;
    2315        5950 :   q = umodiu(p,n); if (!q) return 0;
    2316        5376 :   return cgcd((n-1)/Fl_order(q, n-1, n), l) == 1;
    2317             : }
    2318             : 
    2319             : /* let k=2 if p%4==1, and k=4 else and assume k*p does not divide l.
    2320             :  * Return an irreducible polynomial of degree l over F_p.
    2321             :  * Variant of Adleman and Lenstra "Finding irreducible polynomials over
    2322             :  * finite fields", ACM, 1986 (5) 350--355.
    2323             :  * Not stack clean */
    2324             : static GEN
    2325        3059 : fpinit(GEN p, long l)
    2326             : {
    2327        3059 :   ulong n = 1+l;
    2328        3059 :   while (!fpinit_check(p,n,l)) n += l;
    2329        3059 :   if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
    2330        3059 :   return FpX_red(polsubcyclo(n,l,0),p);
    2331             : }
    2332             : 
    2333             : static GEN
    2334        3095 : ffinit_fact(GEN p, long n)
    2335             : {
    2336        3095 :   GEN P, F = gel(factoru_pow(n),3);
    2337             :   long i;
    2338        3096 :   if (!odd(n) && absequaliu(p, 2))
    2339         588 :     P = f2init(vals(n)); /* if n is even, F[1] = 2^vals(n)*/
    2340             :   else
    2341        2508 :     P = fpinit(p, F[1]);
    2342        3537 :   for (i = 2; i < lg(F); ++i)
    2343         441 :     P = FpX_direct_compositum(fpinit(p, F[i]), P, p);
    2344        3096 :   return P;
    2345             : }
    2346             : 
    2347             : static GEN
    2348         110 : ffinit_nofact(GEN p, long n)
    2349             : {
    2350         110 :   GEN P, Q = NULL;
    2351         110 :   if (lgefint(p)==3)
    2352             :   {
    2353           0 :     ulong pp = p[2], q;
    2354           0 :     long v = u_lvalrem(n,pp,&q);
    2355           0 :     if (v>0)
    2356             :     {
    2357           0 :       Q = (pp == 2)? f2init(v): fpinit(p,n/q);
    2358           0 :       n = q;
    2359             :     }
    2360             :   }
    2361             :   /* n coprime to p */
    2362         110 :   if (n==1) P = Q;
    2363             :   else
    2364             :   {
    2365         110 :     P = fpinit(p, n);
    2366         110 :     if (Q) P = FpX_direct_compositum(P, Q, p);
    2367             :   }
    2368         110 :   return P;
    2369             : }
    2370             : 
    2371             : static GEN
    2372        4010 : init_Fq_i(GEN p, long n, long v)
    2373             : {
    2374             :   GEN P;
    2375        4010 :   if (n <= 0) pari_err_DOMAIN("ffinit", "degree", "<=", gen_0, stoi(n));
    2376        4010 :   if (typ(p) != t_INT) pari_err_TYPE("ffinit",p);
    2377        4010 :   if (signe(p) <= 0) pari_err_PRIME("ffinit",p);
    2378        4010 :   if (v < 0) v = 0;
    2379        4010 :   if (n == 1) return pol_x(v);
    2380        3821 :   if (fpinit_check(p, n+1, n)) return polcyclo(n+1, v);
    2381        3206 :   if (lgefint(p)-2 <= expu(n))
    2382        3095 :     P = ffinit_fact(p,n);
    2383             :   else
    2384         110 :     P = ffinit_nofact(p,n);
    2385        3206 :   setvarn(P, v); return P;
    2386             : }
    2387             : GEN
    2388        3877 : init_Fq(GEN p, long n, long v)
    2389             : {
    2390        3877 :   pari_sp av = avma;
    2391        3877 :   return gerepileupto(av, init_Fq_i(p, n, v));
    2392             : }
    2393             : GEN
    2394         133 : ffinit(GEN p, long n, long v)
    2395             : {
    2396         133 :   pari_sp av = avma;
    2397         133 :   return gerepileupto(av, FpX_to_mod(init_Fq_i(p, n, v), p));
    2398             : }
    2399             : 
    2400             : GEN
    2401        3178 : ffnbirred(GEN p, long n)
    2402             : {
    2403        3178 :   pari_sp av = avma;
    2404             :   long j;
    2405        3178 :   GEN s = gen_0, dk, pd;
    2406        3178 :   dk = divisorsu(n);
    2407       10535 :   for (j = 1; j < lg(dk); ++j)
    2408             :   {
    2409        7357 :     long d = dk[j];
    2410        7357 :     long m = moebiusu(d);
    2411        7357 :     if (!m) continue;
    2412        6797 :     pd = powiu(p, n/d);
    2413        6797 :     s = m>0 ? addii(s, pd): subii(s,pd);
    2414             :   }
    2415        3178 :   return gerepileuptoint(av, divis(s, n));
    2416             : }
    2417             : 
    2418             : GEN
    2419         434 : ffsumnbirred(GEN p, long n)
    2420             : {
    2421         434 :   pari_sp av = avma;
    2422             :   long i,j;
    2423         434 :   GEN v,q, t = gen_0;
    2424         434 :   v = cgetg(n+1,t_VECSMALL); v[1] = 1;
    2425         434 :   q = cgetg(n+1,t_VEC); gel(q,1) = p;
    2426        1547 :   for(i=2; i<=n; i++)
    2427             :   {
    2428        1113 :     v[i] = moebiusu(i);
    2429        1113 :     gel(q,i) = mulii(gel(q,i-1), p);
    2430             :   }
    2431        1981 :   for(i=1; i<=n; i++)
    2432             :   {
    2433        1547 :     GEN s = gen_0;
    2434        1547 :     GEN dk = divisorsu(i);
    2435        4725 :     for (j = 1; j < lg(dk); ++j)
    2436             :     {
    2437        3178 :       long d = dk[j], m = v[d];
    2438        3178 :       if (!m) continue;
    2439        2884 :       s = m>0 ? addii(s, gel(q, i/d)): subii(s, gel(q, i/d));
    2440             :     }
    2441        1547 :     t = addii(t, divis(s, i));
    2442             :   }
    2443         434 :   return gerepileuptoint(av, t);
    2444             : }
    2445             : 
    2446             : GEN
    2447         140 : ffnbirred0(GEN p, long n, long flag)
    2448             : {
    2449         140 :   if (typ(p) != t_INT) pari_err_TYPE("ffnbirred", p);
    2450         140 :   if (n <= 0) pari_err_DOMAIN("ffnbirred", "degree", "<=", gen_0, stoi(n));
    2451         140 :   switch(flag)
    2452             :   {
    2453             :     case 0:
    2454          70 :       return ffnbirred(p, n);
    2455             :     case 1:
    2456          70 :       return ffsumnbirred(p, n);
    2457             :     default:
    2458           0 :       pari_err_FLAG("ffnbirred");
    2459             :   }
    2460             :   return NULL; /* LCOV_EXCL_LINE */
    2461             : }

Generated by: LCOV version 1.11