Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - polarit3.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23690-5d6e28857) Lines: 1326 1516 87.5 %
Date: 2019-03-18 05:43:21 Functions: 145 160 90.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        1239 : char_update_prime(struct charact *S, GEN p)
      34             : {
      35        1239 :   if (!S->isprime) { S->isprime = 1; S->q = p; }
      36        1239 :   if (!equalii(p, S->q)) pari_err_MODULUS("characteristic", S->q, p);
      37        1232 : }
      38             : static void
      39        5194 : char_update_int(struct charact *S, GEN n)
      40             : {
      41        5194 :   if (S->isprime)
      42             :   {
      43           7 :     if (dvdii(n, S->q)) return;
      44           7 :     pari_err_MODULUS("characteristic", S->q, n);
      45             :   }
      46        5187 :   S->q = gcdii(S->q, n);
      47             : }
      48             : static void
      49      605955 : charact(struct charact *S, GEN x)
      50             : {
      51      605955 :   const long tx = typ(x);
      52             :   long i, l;
      53      605955 :   switch(tx)
      54             :   {
      55        4221 :     case t_INTMOD:char_update_int(S, gel(x,1)); break;
      56        1148 :     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       19782 :       l = lg(x);
      61       19782 :       for (i=lontyp[tx]; i < l; i++) charact(S,gel(x,i));
      62       19768 :       break;
      63             :     case t_LIST:
      64           7 :       x = list_data(x);
      65           7 :       if (x) charact(S, x);
      66           7 :       break;
      67             :   }
      68      605927 : }
      69             : static void
      70       33245 : charact_res(struct charact *S, GEN x)
      71             : {
      72       33245 :   const long tx = typ(x);
      73             :   long i, l;
      74       33245 :   switch(tx)
      75             :   {
      76         973 :     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       10345 :       l = lg(x);
      83       10345 :       for (i=lontyp[tx]; i < l; i++) charact_res(S,gel(x,i));
      84       10345 :       break;
      85             :     case t_LIST:
      86           0 :       x = list_data(x);
      87           0 :       if (x) charact_res(S, x);
      88           0 :       break;
      89             :   }
      90       33245 : }
      91             : GEN
      92       10500 : characteristic(GEN x)
      93             : {
      94             :   struct charact S;
      95       10500 :   S.q = gen_0; S.isprime = 0;
      96       10500 :   charact(&S, x); return S.q;
      97             : }
      98             : GEN
      99        2497 : residual_characteristic(GEN x)
     100             : {
     101             :   struct charact S;
     102        2497 :   S.q = gen_0; S.isprime = 0;
     103        2497 :   charact_res(&S, x); return S.q;
     104             : }
     105             : 
     106             : int
     107    58462930 : Rg_is_Fp(GEN x, GEN *pp)
     108             : {
     109             :   GEN mod;
     110    58462930 :   switch(typ(x))
     111             :   {
     112             :   case t_INTMOD:
     113     4256896 :     mod = gel(x,1);
     114     4256896 :     if (!*pp) *pp = mod;
     115     4021983 :     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     4256896 :     return 1;
     121             :   case t_INT:
     122    50499185 :     return 1;
     123     3706849 :   default: return 0;
     124             :   }
     125             : }
     126             : 
     127             : int
     128    19850896 : RgX_is_FpX(GEN x, GEN *pp)
     129             : {
     130    19850896 :   long i, lx = lg(x);
     131    74581189 :   for (i=2; i<lx; i++)
     132    58437142 :     if (!Rg_is_Fp(gel(x, i), pp))
     133     3706849 :       return 0;
     134    16144047 :   return 1;
     135             : }
     136             : 
     137             : int
     138           0 : RgV_is_FpV(GEN x, GEN *pp)
     139             : {
     140           0 :   long i, lx = lg(x);
     141           0 :   for (i=1; i<lx; i++)
     142           0 :     if (!Rg_is_Fp(gel(x,i), pp)) return 0;
     143           0 :   return 1;
     144             : }
     145             : 
     146             : int
     147           0 : RgM_is_FpM(GEN x, GEN *pp)
     148             : {
     149           0 :   long i, lx = lg(x);
     150           0 :   for (i=1; i<lx; i++)
     151           0 :     if (!RgV_is_FpV(gel(x, i), pp)) return 0;
     152           0 :   return 1;
     153             : }
     154             : 
     155             : int
     156       57946 : Rg_is_FpXQ(GEN x, GEN *pT, GEN *pp)
     157             : {
     158             :   GEN pol, mod, p;
     159       57946 :   switch(typ(x))
     160             :   {
     161             :   case t_INTMOD:
     162       25781 :     return Rg_is_Fp(x, pp);
     163             :   case t_INT:
     164        6531 :     return 1;
     165             :   case t_POL:
     166          21 :     return RgX_is_FpX(x, pp);
     167             :   case t_FFELT:
     168       20986 :     mod = x; p = FF_p_i(x);
     169       20986 :     if (!*pp) *pp = p;
     170       20986 :     if (!*pT) *pT = mod;
     171       19544 :     else if (typ(*pT)!=t_FFELT || !FF_samefield(*pT,mod))
     172             :     {
     173          42 :       if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_FpXQ");
     174          42 :       return 0;
     175             :     }
     176       20944 :     return 1;
     177             :   case t_POLMOD:
     178        4543 :     mod = gel(x,1); pol = gel(x, 2);
     179        4543 :     if (!RgX_is_FpX(mod, pp)) return 0;
     180        4543 :     if (typ(pol)==t_POL)
     181             :     {
     182        4536 :       if (!RgX_is_FpX(pol, pp)) return 0;
     183             :     }
     184           7 :     else if (!Rg_is_Fp(pol, pp)) return 0;
     185        4543 :     if (!*pT) *pT = mod;
     186        4431 :     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        4543 :     return 1;
     192             : 
     193          84 :   default: return 0;
     194             :   }
     195             : }
     196             : 
     197             : int
     198        2961 : RgX_is_FpXQX(GEN x, GEN *pT, GEN *pp)
     199             : {
     200        2961 :   long i, lx = lg(x);
     201       60389 :   for (i = 2; i < lx; i++)
     202       57470 :     if (!Rg_is_FpXQ(gel(x,i), pT, pp)) return 0;
     203        2919 :   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    32062446 : Rg_to_Fp(GEN x, GEN p)
     216             : {
     217    32062446 :   if (lgefint(p) == 3) return utoi(Rg_to_Fl(x, uel(p,2)));
     218     2635833 :   switch(typ(x))
     219             :   {
     220      181510 :     case t_INT: return modii(x, p);
     221             :     case t_FRAC: {
     222         121 :       pari_sp av = avma;
     223         121 :       GEN z = modii(gel(x,1), p);
     224         121 :       if (z == gen_0) return gen_0;
     225         121 :       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     2454202 :       GEN q = gel(x,1), a = gel(x,2);
     230     2454202 :       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     1273841 : Rg_to_FpXQ(GEN x, GEN T, GEN p)
     241             : {
     242     1273841 :   long ta, tx = typ(x), v = get_FpX_var(T);
     243             :   GEN a, b;
     244     1273841 :   if (is_const_t(tx))
     245             :   {
     246       56653 :     if (tx == t_FFELT)
     247             :     {
     248       17085 :       GEN z = FF_to_FpXQ(x);
     249       17085 :       setvarn(z, v);
     250       17085 :       return z;
     251             :     }
     252       39568 :     return scalar_ZX(degpol(T)? Rg_to_Fp(x, p): gen_0, v);
     253             :   }
     254     1217188 :   switch(tx)
     255             :   {
     256             :     case t_POLMOD:
     257     1212519 :       b = gel(x,1);
     258     1212519 :       a = gel(x,2); ta = typ(a);
     259     1212519 :       if (is_const_t(ta))
     260        4025 :         return scalar_ZX(degpol(T)? Rg_to_Fp(a, p): gen_0, v);
     261     1208494 :       b = RgX_to_FpX(b, p); if (varn(b) != v) break;
     262     1208494 :       a = RgX_to_FpX(a, p);
     263     1208494 :       if (ZX_equal(b,get_FpX_mod(T)) || signe(FpX_rem(b,T,p))==0)
     264     1208494 :         return FpX_rem(a, T, p);
     265           0 :       break;
     266             :     case t_POL:
     267        4669 :       if (varn(x) != v) break;
     268        4669 :       return FpX_rem(RgX_to_FpX(x,p), T, p);
     269             :     case t_RFRAC:
     270           0 :       a = Rg_to_FpXQ(gel(x,1), T,p);
     271           0 :       b = Rg_to_FpXQ(gel(x,2), T,p);
     272           0 :       return FpXQ_div(a,b, T,p);
     273             :   }
     274           0 :   pari_err_TYPE("Rg_to_FpXQ",x);
     275             :   return NULL; /* LCOV_EXCL_LINE */
     276             : }
     277             : GEN
     278     3383245 : RgX_to_FpX(GEN x, GEN p)
     279             : {
     280             :   long i, l;
     281     3383245 :   GEN z = cgetg_copy(x, &l); z[1] = x[1];
     282     3383245 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     283     3383245 :   return FpX_renormalize(z, l);
     284             : }
     285             : 
     286             : GEN
     287        1022 : RgV_to_FpV(GEN x, GEN p)
     288        1022 : { pari_APPLY_type(t_VEC, Rg_to_Fp(gel(x,i), p)) }
     289             : 
     290             : GEN
     291      922236 : RgC_to_FpC(GEN x, GEN p)
     292      922236 : { pari_APPLY_type(t_COL, Rg_to_Fp(gel(x,i), p)) }
     293             : 
     294             : GEN
     295      129379 : RgM_to_FpM(GEN x, GEN p)
     296      129379 : { pari_APPLY_same(RgC_to_FpC(gel(x,i), p)) }
     297             : 
     298             : GEN
     299      281777 : RgV_to_Flv(GEN x, ulong p)
     300      281777 : { pari_APPLY_ulong(Rg_to_Fl(gel(x,i), p)) }
     301             : 
     302             : GEN
     303      114236 : RgM_to_Flm(GEN x, ulong p)
     304      114236 : { pari_APPLY_same(RgV_to_Flv(gel(x,i), p)) }
     305             : 
     306             : GEN
     307        2646 : RgX_to_FpXQX(GEN x, GEN T, GEN p)
     308             : {
     309        2646 :   long i, l = lg(x);
     310        2646 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     311        2646 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T,p);
     312        2646 :   return FpXQX_renormalize(z, l);
     313             : }
     314             : GEN
     315        1281 : RgX_to_FqX(GEN x, GEN T, GEN p)
     316             : {
     317        1281 :   long i, l = lg(x);
     318        1281 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     319        1281 :   if (T)
     320         616 :     for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T, p);
     321             :   else
     322         665 :     for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     323        1281 :   return FpXQX_renormalize(z, l);
     324             : }
     325             : 
     326             : GEN
     327      218862 : RgC_to_FqC(GEN x, GEN T, GEN p)
     328             : {
     329      218862 :   long i, l = lg(x);
     330      218862 :   GEN z = cgetg(l, t_COL);
     331      218862 :   if (T)
     332      218862 :     for (i = 1; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T, p);
     333             :   else
     334           0 :     for (i = 1; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     335      218862 :   return z;
     336             : }
     337             : 
     338             : GEN
     339       52318 : RgM_to_FqM(GEN x, GEN T, GEN p)
     340       52318 : { pari_APPLY_same(RgC_to_FqC(gel(x, i), T, p)) }
     341             : 
     342             : /* lg(V) > 1 */
     343             : GEN
     344      849765 : FpXV_FpC_mul(GEN V, GEN W, GEN p)
     345             : {
     346      849765 :   pari_sp av = avma;
     347      849765 :   long i, l = lg(V);
     348      849765 :   GEN z = ZX_Z_mul(gel(V,1),gel(W,1));
     349     4181499 :   for(i=2; i<l; i++)
     350             :   {
     351     3331734 :     z = ZX_add(z, ZX_Z_mul(gel(V,i),gel(W,i)));
     352     3331734 :     if ((i & 7) == 0) z = gerepileupto(av, z);
     353             :   }
     354      849765 :   return gerepileupto(av, FpX_red(z,p));
     355             : }
     356             : 
     357             : GEN
     358        1596 : FqX_Fq_add(GEN y, GEN x, GEN T, GEN p)
     359             : {
     360        1596 :   long i, lz = lg(y);
     361             :   GEN z;
     362        1596 :   if (!T) return FpX_Fp_add(y, x, p);
     363        1596 :   if (lz == 2) return scalarpol(x, varn(y));
     364        1596 :   z = cgetg(lz,t_POL); z[1] = y[1];
     365        1596 :   gel(z,2) = Fq_add(gel(y,2),x, T, p);
     366        1596 :   if (lz == 3) z = FpXX_renormalize(z,lz);
     367             :   else
     368         287 :     for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
     369        1596 :   return z;
     370             : }
     371             : 
     372             : GEN
     373        1048 : FqX_Fq_sub(GEN y, GEN x, GEN T, GEN p)
     374             : {
     375        1048 :   long i, lz = lg(y);
     376             :   GEN z;
     377        1048 :   if (!T) return FpX_Fp_sub(y, x, p);
     378        1048 :   if (lz == 2) return scalarpol(x, varn(y));
     379        1048 :   z = cgetg(lz,t_POL); z[1] = y[1];
     380        1048 :   gel(z,2) = Fq_sub(gel(y,2), x, T, p);
     381        1048 :   if (lz == 3) z = FpXX_renormalize(z,lz);
     382             :   else
     383         926 :     for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
     384        1048 :   return z;
     385             : }
     386             : 
     387             : GEN
     388      144701 : FqX_Fq_mul_to_monic(GEN P, GEN U, GEN T, GEN p)
     389             : {
     390             :   long i, lP;
     391      144701 :   GEN res = cgetg_copy(P, &lP); res[1] = P[1];
     392      144701 :   for(i=2; i<lP-1; i++) gel(res,i) = Fq_mul(U,gel(P,i), T,p);
     393      144701 :   gel(res,lP-1) = gen_1; return res;
     394             : }
     395             : 
     396             : GEN
     397        3924 : FpXQX_normalize(GEN z, GEN T, GEN p)
     398             : {
     399             :   GEN lc;
     400        3924 :   if (lg(z) == 2) return z;
     401        3910 :   lc = leading_coeff(z);
     402        3910 :   if (typ(lc) == t_POL)
     403             :   {
     404        1883 :     if (lg(lc) > 3) /* non-constant */
     405        1634 :       return FqX_Fq_mul_to_monic(z, Fq_inv(lc,T,p), T,p);
     406             :     /* constant */
     407         249 :     lc = gel(lc,2);
     408         249 :     z = shallowcopy(z);
     409         249 :     gel(z, lg(z)-1) = lc;
     410             :   }
     411             :   /* lc a t_INT */
     412        2276 :   if (equali1(lc)) return z;
     413          57 :   return FqX_Fq_mul_to_monic(z, Fp_inv(lc,p), T,p);
     414             : }
     415             : 
     416             : GEN
     417      127379 : FqX_eval(GEN x, GEN y, GEN T, GEN p)
     418             : {
     419             :   pari_sp av;
     420             :   GEN p1, r;
     421      127379 :   long j, i=lg(x)-1;
     422      127379 :   if (i<=2)
     423       26348 :     return (i==2)? Fq_red(gel(x,2), T, p): gen_0;
     424      101031 :   av=avma; p1=gel(x,i);
     425             :   /* specific attention to sparse polynomials (see poleval)*/
     426             :   /*You've guessed it! It's a copy-paste(tm)*/
     427      297171 :   for (i--; i>=2; i=j-1)
     428             :   {
     429      196588 :     for (j=i; !signe(gel(x,j)); j--)
     430         448 :       if (j==2)
     431             :       {
     432         301 :         if (i!=j) y = Fq_pow(y,utoipos(i-j+1), T, p);
     433         301 :         return gerepileupto(av, Fq_mul(p1,y, T, p));
     434             :       }
     435      196140 :     r = (i==j)? y: Fq_pow(y, utoipos(i-j+1), T, p);
     436      196140 :     p1 = Fq_add(Fq_mul(p1,r,T,p), gel(x,j), T, p);
     437             :   }
     438      100730 :   return gerepileupto(av, p1);
     439             : }
     440             : 
     441             : GEN
     442       31591 : FqXY_evalx(GEN Q, GEN x, GEN T, GEN p)
     443             : {
     444       31591 :   long i, lb = lg(Q);
     445             :   GEN z;
     446       31591 :   if (!T) return FpXY_evalx(Q, x, p);
     447       20993 :   z = cgetg(lb, t_POL); z[1] = Q[1];
     448      117068 :   for (i=2; i<lb; i++)
     449             :   {
     450       96075 :     GEN q = gel(Q,i);
     451       96075 :     gel(z,i) = typ(q) == t_INT? modii(q,p): FqX_eval(q, x, T, p);
     452             :   }
     453       20993 :   return FpXQX_renormalize(z, lb);
     454             : }
     455             : 
     456             : /* Q an FpXY, evaluate at (X,Y) = (x,y) */
     457             : GEN
     458       14497 : FqXY_eval(GEN Q, GEN y, GEN x, GEN T, GEN p)
     459             : {
     460       14497 :   pari_sp av = avma;
     461       14497 :   if (!T) return FpXY_eval(Q, y, x, p);
     462         588 :   return gerepileupto(av, FqX_eval(FqXY_evalx(Q, x, T, p), y, T, p));
     463             : }
     464             : 
     465             : /* a X^d */
     466             : GEN
     467      511077 : monomial(GEN a, long d, long v)
     468             : {
     469             :   long i, n;
     470             :   GEN P;
     471      511077 :   if (d < 0) {
     472           0 :     if (isrationalzero(a)) return pol_0(v);
     473           0 :     retmkrfrac(a, pol_xn(-d, v));
     474             :   }
     475      511077 :   if (gequal0(a))
     476             :   {
     477        8932 :     if (isexactzero(a)) return scalarpol_shallow(a,v);
     478           0 :     n = d+2; P = cgetg(n+1, t_POL);
     479           0 :     P[1] = evalsigne(0) | evalvarn(v);
     480             :   }
     481             :   else
     482             :   {
     483      502145 :     n = d+2; P = cgetg(n+1, t_POL);
     484      502145 :     P[1] = evalsigne(1) | evalvarn(v);
     485             :   }
     486      502145 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     487      502145 :   gel(P,i) = a; return P;
     488             : }
     489             : GEN
     490     1860818 : monomialcopy(GEN a, long d, long v)
     491             : {
     492             :   long i, n;
     493             :   GEN P;
     494     1860818 :   if (d < 0) {
     495          14 :     if (isrationalzero(a)) return pol_0(v);
     496          14 :     retmkrfrac(gcopy(a), pol_xn(-d, v));
     497             :   }
     498     1860804 :   if (gequal0(a))
     499             :   {
     500           7 :     if (isexactzero(a)) return scalarpol(a,v);
     501           0 :     n = d+2; P = cgetg(n+1, t_POL);
     502           0 :     P[1] = evalsigne(0) | evalvarn(v);
     503             :   }
     504             :   else
     505             :   {
     506     1860797 :     n = d+2; P = cgetg(n+1, t_POL);
     507     1860797 :     P[1] = evalsigne(1) | evalvarn(v);
     508             :   }
     509     1860797 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     510     1860797 :   gel(P,i) = gcopy(a); return P;
     511             : }
     512             : GEN
     513       23188 : pol_x_powers(long N, long v)
     514             : {
     515       23188 :   GEN L = cgetg(N+1,t_VEC);
     516             :   long i;
     517       23188 :   for (i=1; i<=N; i++) gel(L,i) = pol_xn(i-1, v);
     518       23188 :   return L;
     519             : }
     520             : 
     521             : GEN
     522           0 : FqXQ_powers(GEN x, long l, GEN S, GEN T, GEN p)
     523             : {
     524           0 :   return T ? FpXQXQ_powers(x, l, S, T, p): FpXQ_powers(x, l, S, p);
     525             : }
     526             : 
     527             : GEN
     528           0 : FqXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, GEN p)
     529             : {
     530           0 :   return T ? FpXQXQ_matrix_pow(y, n, m, S, T, p): FpXQ_matrix_pow(y, n, m, S, p);
     531             : }
     532             : 
     533             : /*******************************************************************/
     534             : /*                                                                 */
     535             : /*                             Fq                                  */
     536             : /*                                                                 */
     537             : /*******************************************************************/
     538             : 
     539             : GEN
     540     6962290 : Fq_add(GEN x, GEN y, GEN T/*unused*/, GEN p)
     541             : {
     542             :   (void)T;
     543     6962290 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     544             :   {
     545     2485111 :     case 0: return Fp_add(x,y,p);
     546      203791 :     case 1: return FpX_Fp_add(x,y,p);
     547      327041 :     case 2: return FpX_Fp_add(y,x,p);
     548     3946347 :     case 3: return FpX_add(x,y,p);
     549             :   }
     550             :   return NULL;/*LCOV_EXCL_LINE*/
     551             : }
     552             : 
     553             : GEN
     554     4689674 : Fq_sub(GEN x, GEN y, GEN T/*unused*/, GEN p)
     555             : {
     556             :   (void)T;
     557     4689674 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     558             :   {
     559      167415 :     case 0: return Fp_sub(x,y,p);
     560        2191 :     case 1: return FpX_Fp_sub(x,y,p);
     561       10122 :     case 2: return Fp_FpX_sub(x,y,p);
     562     4509946 :     case 3: return FpX_sub(x,y,p);
     563             :   }
     564             :   return NULL;/*LCOV_EXCL_LINE*/
     565             : }
     566             : 
     567             : GEN
     568      474058 : Fq_neg(GEN x, GEN T/*unused*/, GEN p)
     569             : {
     570             :   (void)T;
     571      474058 :   return (typ(x)==t_POL)? FpX_neg(x,p): Fp_neg(x,p);
     572             : }
     573             : 
     574             : GEN
     575       12899 : Fq_halve(GEN x, GEN T/*unused*/, GEN p)
     576             : {
     577             :   (void)T;
     578       12899 :   return (typ(x)==t_POL)? FpX_halve(x,p): Fp_halve(x,p);
     579             : }
     580             : 
     581             : /* If T==NULL do not reduce*/
     582             : GEN
     583    42744895 : Fq_mul(GEN x, GEN y, GEN T, GEN p)
     584             : {
     585    42744895 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     586             :   {
     587     2539908 :     case 0: return Fp_mul(x,y,p);
     588       71532 :     case 1: return FpX_Fp_mul(x,y,p);
     589      131055 :     case 2: return FpX_Fp_mul(y,x,p);
     590    40002400 :     case 3: if (T) return FpXQ_mul(x,y,T,p);
     591     2757339 :             else return FpX_mul(x,y,p);
     592             :   }
     593             :   return NULL;/*LCOV_EXCL_LINE*/
     594             : }
     595             : 
     596             : /* If T==NULL do not reduce*/
     597             : GEN
     598      774287 : Fq_mulu(GEN x, ulong y, /*unused*/GEN T, GEN p)
     599             : {
     600             :   (void) T;
     601      774287 :   return typ(x)==t_POL ? FpX_Fp_mul(x,utoi(y),p): Fp_mulu(x, y, p);
     602             : }
     603             : 
     604             : /* y t_INT */
     605             : GEN
     606       56860 : Fq_Fp_mul(GEN x, GEN y, GEN T/*unused*/, GEN p)
     607             : {
     608             :   (void)T;
     609       56860 :   return (typ(x) == t_POL)? FpX_Fp_mul(x,y,p)
     610       56860 :                           : Fp_mul(x,y,p);
     611             : }
     612             : /* If T==NULL do not reduce*/
     613             : GEN
     614      270892 : Fq_sqr(GEN x, GEN T, GEN p)
     615             : {
     616      270892 :   if (typ(x) == t_POL)
     617             :   {
     618       11843 :     if (T) return FpXQ_sqr(x,T,p);
     619           0 :     else return FpX_sqr(x,p);
     620             :   }
     621             :   else
     622      259049 :     return Fp_sqr(x,p);
     623             : }
     624             : 
     625             : GEN
     626           0 : Fq_neg_inv(GEN x, GEN T, GEN p)
     627             : {
     628           0 :   if (typ(x) == t_INT) return Fp_inv(Fp_neg(x,p),p);
     629           0 :   return FpXQ_inv(FpX_neg(x,p),T,p);
     630             : }
     631             : 
     632             : GEN
     633           0 : Fq_invsafe(GEN x, GEN pol, GEN p)
     634             : {
     635           0 :   if (typ(x) == t_INT) return Fp_invsafe(x,p);
     636           0 :   return FpXQ_invsafe(x,pol,p);
     637             : }
     638             : 
     639             : GEN
     640       34662 : Fq_inv(GEN x, GEN pol, GEN p)
     641             : {
     642       34662 :   if (typ(x) == t_INT) return Fp_inv(x,p);
     643       29205 :   return FpXQ_inv(x,pol,p);
     644             : }
     645             : 
     646             : GEN
     647      516467 : Fq_div(GEN x, GEN y, GEN pol, GEN p)
     648             : {
     649      516467 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     650             :   {
     651      486976 :     case 0: return Fp_div(x,y,p);
     652       23975 :     case 1: return FpX_Fp_mul(x,Fp_inv(y,p),p);
     653         280 :     case 2: return FpX_Fp_mul(FpXQ_inv(y,pol,p),x,p);
     654        5236 :     case 3: return FpXQ_div(x,y,pol,p);
     655             :   }
     656             :   return NULL;/*LCOV_EXCL_LINE*/
     657             : }
     658             : 
     659             : GEN
     660       33124 : Fq_pow(GEN x, GEN n, GEN pol, GEN p)
     661             : {
     662       33124 :   if (typ(x) == t_INT) return Fp_pow(x,n,p);
     663       10227 :   return FpXQ_pow(x,n,pol,p);
     664             : }
     665             : 
     666             : GEN
     667       14770 : Fq_powu(GEN x, ulong n, GEN pol, GEN p)
     668             : {
     669       14770 :   if (typ(x) == t_INT) return Fp_powu(x,n,p);
     670         749 :   return FpXQ_powu(x,n,pol,p);
     671             : }
     672             : 
     673             : GEN
     674      709301 : Fq_sqrt(GEN x, GEN T, GEN p)
     675             : {
     676      709301 :   if (typ(x) == t_INT)
     677             :   {
     678      698670 :     if (!T || odd(get_FpX_degree(T))) return Fp_sqrt(x,p);
     679         301 :     x = scalarpol_shallow(x, get_FpX_var(T));
     680             :   }
     681       10932 :   return FpXQ_sqrt(x,T,p);
     682             : }
     683             : GEN
     684       60755 : Fq_sqrtn(GEN x, GEN n, GEN T, GEN p, GEN *zeta)
     685             : {
     686       60755 :   if (typ(x) == t_INT)
     687             :   {
     688             :     long d;
     689       60517 :     if (!T) return Fp_sqrtn(x,n,p,zeta);
     690         779 :     d = get_FpX_degree(T);
     691         779 :     if (ugcdiu(n,d) == 1)
     692             :     {
     693         618 :       if (!zeta) return Fp_sqrtn(x,n,p,NULL);
     694             :       /* gcd(n,p-1)=gcd(n,q-1): same number of solutions in Fp and F_q */
     695         611 :       if (equalii(gcdii(subiu(p,1),n), gcdii(subiu(Fp_powu(p,d,n), 1), n)))
     696         590 :         return Fp_sqrtn(x,n,p,zeta);
     697             :     }
     698         182 :     x = scalarpol(x, get_FpX_var(T)); /* left on stack */
     699             :   }
     700         420 :   return FpXQ_sqrtn(x,n,T,p,zeta);
     701             : }
     702             : 
     703             : struct _Fq_field
     704             : {
     705             :   GEN T, p;
     706             : };
     707             : 
     708             : static GEN
     709      302141 : _Fq_red(void *E, GEN x)
     710      302141 : { struct _Fq_field *s = (struct _Fq_field *)E;
     711      302141 :   return Fq_red(x, s->T, s->p);
     712             : }
     713             : 
     714             : static GEN
     715      350763 : _Fq_add(void *E, GEN x, GEN y)
     716             : {
     717             :   (void) E;
     718      350763 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     719             :   {
     720          14 :     case 0: return addii(x,y);
     721           0 :     case 1: return ZX_Z_add(x,y);
     722       15918 :     case 2: return ZX_Z_add(y,x);
     723      334831 :     default: return ZX_add(x,y);
     724             :   }
     725             : }
     726             : 
     727             : static GEN
     728      315028 : _Fq_neg(void *E, GEN x) { (void) E; return typ(x)==t_POL?ZX_neg(x):negi(x); }
     729             : 
     730             : static GEN
     731      230475 : _Fq_mul(void *E, GEN x, GEN y)
     732             : {
     733             :   (void) E;
     734      230475 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     735             :   {
     736         133 :     case 0: return mulii(x,y);
     737        1085 :     case 1: return ZX_Z_mul(x,y);
     738        1043 :     case 2: return ZX_Z_mul(y,x);
     739      228214 :     default: return ZX_mul(x,y);
     740             :   }
     741             : }
     742             : 
     743             : static GEN
     744        9331 : _Fq_inv(void *E, GEN x)
     745        9331 : { struct _Fq_field *s = (struct _Fq_field *)E;
     746        9331 :   return Fq_inv(x,s->T,s->p);
     747             : }
     748             : 
     749             : static int
     750       38388 : _Fq_equal0(GEN x) { return signe(x)==0; }
     751             : 
     752             : static GEN
     753       13965 : _Fq_s(void *E, long x) { (void) E; return stoi(x); }
     754             : 
     755             : static const struct bb_field Fq_field={_Fq_red,_Fq_add,_Fq_mul,_Fq_neg,
     756             :                                        _Fq_inv,_Fq_equal0,_Fq_s};
     757             : 
     758        4144 : const struct bb_field *get_Fq_field(void **E, GEN T, GEN p)
     759             : {
     760        4144 :   if (!T)
     761           0 :     return get_Fp_field(E, p);
     762             :   else
     763             :   {
     764        4144 :     GEN z = new_chunk(sizeof(struct _Fq_field));
     765        4144 :     struct _Fq_field *e = (struct _Fq_field *) z;
     766        4144 :     e->T = T; e->p  = p; *E = (void*)e;
     767        4144 :     return &Fq_field;
     768             :   }
     769             : }
     770             : 
     771             : /*******************************************************************/
     772             : /*                                                                 */
     773             : /*                             Fq[X]                               */
     774             : /*                                                                 */
     775             : /*******************************************************************/
     776             : /* P(X + c) */
     777             : GEN
     778           0 : FpX_translate(GEN P, GEN c, GEN p)
     779             : {
     780           0 :   pari_sp av = avma;
     781             :   GEN Q, *R;
     782             :   long i, k, n;
     783             : 
     784           0 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     785           0 :   Q = leafcopy(P);
     786           0 :   R = (GEN*)(Q+2); n = degpol(P);
     787           0 :   for (i=1; i<=n; i++)
     788             :   {
     789           0 :     for (k=n-i; k<n; k++)
     790           0 :       R[k] = Fp_add(R[k], Fp_mul(c, R[k+1], p), p);
     791             : 
     792           0 :     if (gc_needed(av,2))
     793             :     {
     794           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FpX_translate, i = %ld/%ld", i,n);
     795           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     796             :     }
     797             :   }
     798           0 :   return gerepilecopy(av, FpX_renormalize(Q, lg(Q)));
     799             : }
     800             : /* P(X + c), c an Fq */
     801             : GEN
     802       34167 : FqX_translate(GEN P, GEN c, GEN T, GEN p)
     803             : {
     804       34167 :   pari_sp av = avma;
     805             :   GEN Q, *R;
     806             :   long i, k, n;
     807             : 
     808             :   /* signe works for t_(INT|POL) */
     809       34167 :   if (!signe(P) || !signe(c)) return RgX_copy(P);
     810       34167 :   Q = leafcopy(P);
     811       34167 :   R = (GEN*)(Q+2); n = degpol(P);
     812      151781 :   for (i=1; i<=n; i++)
     813             :   {
     814      439299 :     for (k=n-i; k<n; k++)
     815      321685 :       R[k] = Fq_add(R[k], Fq_mul(c, R[k+1], T, p), T, p);
     816             : 
     817      117614 :     if (gc_needed(av,2))
     818             :     {
     819           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FqX_translate, i = %ld/%ld", i,n);
     820           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     821             :     }
     822             :   }
     823       34167 :   return gerepilecopy(av, FpXQX_renormalize(Q, lg(Q)));
     824             : }
     825             : 
     826             : GEN
     827         665 : FqV_roots_to_pol(GEN V, GEN T, GEN p, long v)
     828             : {
     829         665 :   pari_sp ltop = avma;
     830             :   long k;
     831             :   GEN W;
     832         665 :   if (lgefint(p) == 3)
     833             :   {
     834         591 :     ulong pp = p[2];
     835         591 :     GEN Tl = ZX_to_Flx(T, pp);
     836         591 :     GEN Vl = FqV_to_FlxV(V, T, p);
     837         591 :     Tl = FlxqV_roots_to_pol(Vl, Tl, pp, v);
     838         591 :     return gerepileupto(ltop, FlxX_to_ZXX(Tl));
     839             :   }
     840          74 :   W = cgetg(lg(V),t_VEC);
     841         402 :   for(k=1; k < lg(V); k++)
     842         328 :     gel(W,k) = deg1pol_shallow(gen_1,Fq_neg(gel(V,k),T,p),v);
     843          74 :   return gerepileupto(ltop, FpXQXV_prod(W, T, p));
     844             : }
     845             : 
     846             : GEN
     847      128559 : FqV_red(GEN x, GEN T, GEN p)
     848      128559 : { pari_APPLY_same(Fq_red(gel(x,i), T, p)) }
     849             : 
     850             : GEN
     851           0 : FqC_add(GEN x, GEN y, GEN T, GEN p)
     852             : {
     853           0 :   if (!T) return FpC_add(x, y, p);
     854           0 :   pari_APPLY_type(t_COL, Fq_add(gel(x,i), gel(y,i), T, p))
     855             : }
     856             : 
     857             : GEN
     858           0 : FqC_sub(GEN x, GEN y, GEN T, GEN p)
     859             : {
     860           0 :   if (!T) return FpC_sub(x, y, p);
     861           0 :   pari_APPLY_type(t_COL, Fq_sub(gel(x,i), gel(y,i), T, p))
     862             : }
     863             : 
     864             : GEN
     865           0 : FqC_Fq_mul(GEN x, GEN y, GEN T, GEN p)
     866             : {
     867           0 :   if (!T) return FpC_Fp_mul(x, y, p);
     868           0 :   pari_APPLY_type(t_COL, Fq_mul(gel(x,i),y,T,p))
     869             : }
     870             : 
     871             : GEN
     872         591 : FqV_to_FlxV(GEN x, GEN T, GEN pp)
     873             : {
     874         591 :   long vT = evalvarn(get_FpX_var(T));
     875         591 :   ulong p = pp[2];
     876         591 :   pari_APPLY_type(t_VEC, typ(gel(x,i))==t_INT?  Z_to_Flx(gel(x,i), p, vT)
     877             :                                              : ZX_to_Flx(gel(x,i), p))
     878             : }
     879             : 
     880             : GEN
     881       76926 : FqC_to_FlxC(GEN x, GEN T, GEN pp)
     882             : {
     883       76926 :   long vT = evalvarn(get_FpX_var(T));
     884       76923 :   ulong p = pp[2];
     885       76923 :   pari_APPLY_type(t_COL, typ(gel(x,i))==t_INT?  Z_to_Flx(gel(x,i), p, vT)
     886             :                                              : ZX_to_Flx(gel(x,i), p))
     887             : }
     888             : 
     889             : GEN
     890       11127 : FqM_to_FlxM(GEN x, GEN T, GEN p)
     891       11127 : { pari_APPLY_same(FqC_to_FlxC(gel(x,i), T, p)) }
     892             : 
     893             : GEN
     894        2964 : FpXC_center(GEN x, GEN p, GEN pov2)
     895        2964 : { pari_APPLY_type(t_COL, FpX_center(gel(x,i), p, pov2)) }
     896             : 
     897             : GEN
     898        1366 : FpXM_center(GEN x, GEN p, GEN pov2)
     899        1366 : { pari_APPLY_same(FpXC_center(gel(x,i), p, pov2)) }
     900             : 
     901             : /*******************************************************************/
     902             : /*                                                                 */
     903             : /*                          GENERIC CRT                            */
     904             : /*                                                                 */
     905             : /*******************************************************************/
     906             : 
     907             : static long
     908      340865 : get_nbprimes(ulong bound, ulong *pt_start)
     909             : {
     910             : #ifdef LONG_IS_64BIT
     911      291953 :   ulong pstart = 4611686018427388039UL;
     912             : #else
     913       48912 :   ulong pstart = 1073741827UL;
     914             : #endif
     915      340865 :   if (pt_start) *pt_start = pstart;
     916      340865 :   return (bound/expu(pstart))+1;
     917             : }
     918             : 
     919             : static GEN
     920      796308 : primelist_disc(ulong *p, long n, GEN dB)
     921             : {
     922      796308 :   ulong u = 0;
     923      796308 :   GEN P = cgetg(n+1, t_VECSMALL);
     924             :   long i;
     925      796336 :   if (dB && typ(dB)==t_VECSMALL) { u = uel(dB,1); dB = NULL; }
     926     2412915 :   for (i=1; i <= n; i++, *p = unextprime(*p+1))
     927             :   {
     928     1616577 :     if (dB && umodiu(dB, *p)==0) { i--; continue; }
     929     1616579 :     if (u && *p%u!=1) { i--; continue; }
     930     1610198 :     P[i] = *p;
     931             :   }
     932      796304 :   return P;
     933             : }
     934             : 
     935             : void
     936      242096 : gen_inccrt(const char *str, GEN worker, GEN dB, long n, long mmin,
     937             :            ulong *p, GEN *pt_H, GEN *pt_mod, GEN crt(GEN, GEN, GEN*),
     938             :            GEN center(GEN, GEN, GEN))
     939             : {
     940      242096 :   pari_sp av = avma;
     941             :   long m;
     942             :   GEN  H, P, mod;
     943             :   pari_timer ti;
     944      242096 :   if (!*p) (void) get_nbprimes(1, p);
     945      242096 :   m = minss(mmin, n);
     946      242096 :   if (DEBUGLEVEL > 4)
     947             :   {
     948           0 :       timer_start(&ti);
     949           0 :       err_printf("%s: nb primes: %ld\n",str, n);
     950             :   }
     951      242096 :   if (m == 1)
     952             :   {
     953      177069 :     GEN P = primelist_disc(p, n, dB);
     954      177069 :     GEN done = closure_callgen1(worker, P);
     955      177069 :     H = gel(done,1);
     956      177069 :     mod = gel(done,2);
     957      177069 :     if (!*pt_H && center) H = center(H, mod, shifti(mod,-1));
     958      177069 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: modular", str);
     959             :   }
     960             :   else
     961             :   {
     962       65027 :     long i, s = (n+m-1)/m, r = m - (m*s-n), di = 0;
     963             :     struct pari_mt pt;
     964       65027 :     long pending = 0;
     965       65027 :     H = cgetg(m+1, t_VEC); P = cgetg(m+1, t_VEC);
     966       65025 :     mt_queue_start_lim(&pt, worker, m);
     967      758045 :     for (i=1; i<=m || pending; i++)
     968             :     {
     969             :       GEN done;
     970      693016 :       GEN pr = i <= m ? mkvec(primelist_disc(p, i<=r ? s: s-1, dB)): NULL;
     971      693014 :       mt_queue_submit(&pt, i, pr);
     972      693011 :       done = mt_queue_get(&pt, NULL, &pending);
     973      693011 :       if (done)
     974             :       {
     975      619237 :         di++;
     976      619237 :         gel(H, di) = gel(done,1);
     977      619237 :         gel(P, di) = gel(done,2);
     978      619237 :         if (DEBUGLEVEL>5) err_printf("%ld%% ",100*di/m);
     979             :       }
     980             :     }
     981       65029 :     mt_queue_end(&pt);
     982       65029 :     if (DEBUGLEVEL>5) err_printf("\n");
     983       65029 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: modular", str);
     984       65029 :     H = crt(H, P, &mod);
     985       65029 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: chinese", str);
     986             :   }
     987      242098 :   if (*pt_H)
     988       14430 :     H = crt(mkvec2(*pt_H, H), mkvec2(*pt_mod, mod), &mod);
     989      242098 :   *pt_H = H;
     990      242098 :   *pt_mod = mod;
     991      242098 :   gerepileall(av, 2, pt_H, pt_mod);
     992      242098 : }
     993             : 
     994             : GEN
     995      113201 : gen_crt(const char *str, GEN worker, GEN dB, ulong bound, long mmin, GEN *pt_mod,
     996             :         GEN crt(GEN, GEN, GEN*), GEN center(GEN, GEN, GEN))
     997             : {
     998      113201 :   ulong p = 0;
     999      113201 :   GEN mod = gen_1, H = NULL;
    1000      113201 :   bound++;
    1001      339604 :   while ((ulong)expi(mod) < bound)
    1002             :   {
    1003      113200 :     long n = get_nbprimes(bound-expi(mod), NULL);
    1004      113200 :     gen_inccrt(str, worker, dB, n, mmin, &p, &H, &mod, crt, center);
    1005             :   }
    1006      113202 :   if (pt_mod) *pt_mod = mod;
    1007      113202 :   return H;
    1008             : }
    1009             : 
    1010             : /*******************************************************************/
    1011             : /*                                                                 */
    1012             : /*                          MODULAR GCD                            */
    1013             : /*                                                                 */
    1014             : /*******************************************************************/
    1015             : /* return z = a mod q, b mod p (p,q) = 1; qinv = 1/q mod p; a in ]-q,q] */
    1016             : static GEN
    1017     2412282 : Fl_chinese_coprime(GEN a, ulong b, GEN q, ulong p, ulong qinv, GEN pq, GEN pq2)
    1018             : {
    1019     2412282 :   ulong d, amod = umodiu(a, p);
    1020     2412282 :   pari_sp av = avma;
    1021             :   GEN ax;
    1022             : 
    1023     2412282 :   if (b == amod) return NULL;
    1024     1509934 :   d = Fl_mul(Fl_sub(b, amod, p), qinv, p); /* != 0 */
    1025     1509934 :   if (d >= 1 + (p>>1))
    1026      750622 :     ax = subii(a, mului(p-d, q));
    1027             :   else
    1028             :   {
    1029      759312 :     ax = addii(a, mului(d, q)); /* in ]0, pq[ assuming a in ]-q,q[ */
    1030      759312 :     if (cmpii(ax,pq2) > 0) ax = subii(ax,pq);
    1031             :   }
    1032     1509934 :   return gerepileuptoint(av, ax);
    1033             : }
    1034             : GEN
    1035         364 : Z_init_CRT(ulong Hp, ulong p) { return stoi(Fl_center(Hp, p, p>>1)); }
    1036             : GEN
    1037     3191932 : ZX_init_CRT(GEN Hp, ulong p, long v)
    1038             : {
    1039     3191932 :   long i, l = lg(Hp), lim = (long)(p>>1);
    1040     3191932 :   GEN H = cgetg(l, t_POL);
    1041     3191932 :   H[1] = evalsigne(1) | evalvarn(v);
    1042    11390231 :   for (i=2; i<l; i++)
    1043     8198299 :     gel(H,i) = stoi(Fl_center(Hp[i], p, lim));
    1044     3191932 :   return ZX_renormalize(H,l);
    1045             : }
    1046             : 
    1047             : GEN
    1048       95005 : ZM_init_CRT(GEN Hp, ulong p)
    1049             : {
    1050       95005 :   long i,j, m, l = lg(Hp), lim = (long)(p>>1);
    1051       95005 :   GEN c, cp, H = cgetg(l, t_MAT);
    1052       95005 :   if (l==1) return H;
    1053       50828 :   m = lgcols(Hp);
    1054      130440 :   for (j=1; j<l; j++)
    1055             :   {
    1056       79612 :     cp = gel(Hp,j);
    1057       79612 :     c = cgetg(m, t_COL);
    1058       79612 :     gel(H,j) = c;
    1059       79612 :     for (i=1; i<m; i++) gel(c,i) = stoi(Fl_center(cp[i],p, lim));
    1060             :   }
    1061       50828 :   return H;
    1062             : }
    1063             : 
    1064             : int
    1065        7511 : Z_incremental_CRT(GEN *H, ulong Hp, GEN *ptq, ulong p)
    1066             : {
    1067        7511 :   GEN h, q = *ptq, qp = muliu(q,p);
    1068        7511 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1069        7511 :   int stable = 1;
    1070        7511 :   h = Fl_chinese_coprime(*H,Hp,q,p,qinv,qp,shifti(qp,-1));
    1071        7511 :   if (h) { *H = h; stable = 0; }
    1072        7511 :   *ptq = qp; return stable;
    1073             : }
    1074             : 
    1075             : static int
    1076      240430 : ZX_incremental_CRT_raw(GEN *ptH, GEN Hp, GEN q, GEN qp, ulong p)
    1077             : {
    1078      240430 :   GEN H = *ptH, h, qp2 = shifti(qp,-1);
    1079      240430 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1080      240430 :   long i, l = lg(H), lp = lg(Hp);
    1081      240430 :   int stable = 1;
    1082             : 
    1083      240430 :   if (l < lp)
    1084             :   { /* degree increases */
    1085           0 :     GEN x = cgetg(lp, t_POL);
    1086           0 :     for (i=1; i<l; i++)  x[i] = H[i];
    1087           0 :     for (   ; i<lp; i++) gel(x,i) = gen_0;
    1088           0 :     *ptH = H = x;
    1089           0 :     stable = 0;
    1090      240430 :   } else if (l > lp)
    1091             :   { /* degree decreases */
    1092           0 :     GEN x = cgetg(l, t_VECSMALL);
    1093           0 :     for (i=1; i<lp; i++)  x[i] = Hp[i];
    1094           0 :     for (   ; i<l; i++) x[i] = 0;
    1095           0 :     Hp = x; lp = l;
    1096             :   }
    1097     2044939 :   for (i=2; i<lp; i++)
    1098             :   {
    1099     1804509 :     h = Fl_chinese_coprime(gel(H,i),Hp[i],q,p,qinv,qp,qp2);
    1100     1804509 :     if (h) { gel(H,i) = h; stable = 0; }
    1101             :   }
    1102      240430 :   (void)ZX_renormalize(H,lp);
    1103      240430 :   return stable;
    1104             : }
    1105             : 
    1106             : int
    1107       14056 : ZX_incremental_CRT(GEN *ptH, GEN Hp, GEN *ptq, ulong p)
    1108             : {
    1109       14056 :   GEN q = *ptq, qp = muliu(q,p);
    1110       14056 :   int stable = ZX_incremental_CRT_raw(ptH, Hp, q, qp, p);
    1111       14056 :   *ptq = qp; return stable;
    1112             : }
    1113             : 
    1114             : int
    1115       18463 : ZM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
    1116             : {
    1117       18463 :   GEN h, H = *pH, q = *ptq, qp = muliu(q, p), qp2 = shifti(qp,-1);
    1118       18463 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1119       18463 :   long i,j, l = lg(H), m = lgcols(H);
    1120       18463 :   int stable = 1;
    1121       49084 :   for (j=1; j<l; j++)
    1122      463469 :     for (i=1; i<m; i++)
    1123             :     {
    1124      432848 :       h = Fl_chinese_coprime(gcoeff(H,i,j), coeff(Hp,i,j),q,p,qinv,qp,qp2);
    1125      432848 :       if (h) { gcoeff(H,i,j) = h; stable = 0; }
    1126             :     }
    1127       18463 :   *ptq = qp; return stable;
    1128             : }
    1129             : 
    1130             : GEN
    1131         966 : ZXM_init_CRT(GEN Hp, long deg, ulong p)
    1132             : {
    1133             :   long i, j, k;
    1134             :   GEN H;
    1135         966 :   long m, l = lg(Hp), lim = (long)(p>>1), n;
    1136         966 :   H = cgetg(l, t_MAT);
    1137         966 :   if (l==1) return H;
    1138         966 :   m = lgcols(Hp);
    1139         966 :   n = deg + 3;
    1140        3675 :   for (j=1; j<l; j++)
    1141             :   {
    1142        2709 :     GEN cp = gel(Hp,j);
    1143        2709 :     GEN c = cgetg(m, t_COL);
    1144        2709 :     gel(H,j) = c;
    1145       38304 :     for (i=1; i<m; i++)
    1146             :     {
    1147       35595 :       GEN dp = gel(cp, i);
    1148       35595 :       long l = lg(dp);
    1149       35595 :       GEN d = cgetg(n, t_POL);
    1150       35595 :       gel(c, i) = d;
    1151       35595 :       d[1] = dp[1];
    1152       72772 :       for (k=2; k<l; k++)
    1153       37177 :         gel(d,k) = stoi(Fl_center(dp[k], p, lim));
    1154       70014 :       for (   ; k<n; k++)
    1155       34419 :         gel(d,k) = gen_0;
    1156             :     }
    1157             :   }
    1158         966 :   return H;
    1159             : }
    1160             : 
    1161             : int
    1162         900 : ZXM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
    1163             : {
    1164         900 :   GEN v, H = *pH, q = *ptq, qp = muliu(q, p), qp2 = shifti(qp,-1);
    1165         900 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1166         900 :   long i,j,k, l = lg(H), m = lgcols(H), n = lg(gmael(H,1,1));
    1167         900 :   int stable = 1;
    1168        4961 :   for (j=1; j<l; j++)
    1169       87758 :     for (i=1; i<m; i++)
    1170             :     {
    1171       83697 :       GEN h = gmael(H,j,i), hp = gmael(Hp,j,i);
    1172       83697 :       long lh = lg(hp);
    1173      181387 :       for (k=2; k<lh; k++)
    1174             :       {
    1175       97690 :         v = Fl_chinese_coprime(gel(h,k),uel(hp,k),q,p,qinv,qp,qp2);
    1176       97690 :         if (v) { gel(h,k) = v; stable = 0; }
    1177             :       }
    1178      153421 :       for (; k<n; k++)
    1179             :       {
    1180       69724 :         v = Fl_chinese_coprime(gel(h,k),0,q,p,qinv,qp,qp2);
    1181       69724 :         if (v) { gel(h,k) = v; stable = 0; }
    1182             :       }
    1183             :     }
    1184         900 :   *ptq = qp; return stable;
    1185             : }
    1186             : 
    1187             : /* record the degrees of Euclidean remainders (make them as large as
    1188             :  * possible : smaller values correspond to a degenerate sequence) */
    1189             : static void
    1190        2464 : Flx_resultant_set_dglist(GEN a, GEN b, GEN dglist, ulong p)
    1191             : {
    1192             :   long da,db,dc, ind;
    1193        2464 :   pari_sp av = avma;
    1194             : 
    1195        2464 :   if (lgpol(a)==0 || lgpol(b)==0) return;
    1196        2443 :   da = degpol(a);
    1197        2443 :   db = degpol(b);
    1198        2443 :   if (db > da)
    1199           0 :   { swapspec(a,b, da,db); }
    1200        2443 :   else if (!da) return;
    1201        2443 :   ind = 0;
    1202       14077 :   while (db)
    1203             :   {
    1204        9191 :     GEN c = Flx_rem(a,b, p);
    1205        9191 :     a = b; b = c; dc = degpol(c);
    1206        9191 :     if (dc < 0) break;
    1207             : 
    1208        9191 :     ind++;
    1209        9191 :     if (dc > dglist[ind]) dglist[ind] = dc;
    1210        9191 :     if (gc_needed(av,2))
    1211             :     {
    1212           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1213           0 :       gerepileall(av, 2, &a,&b);
    1214             :     }
    1215        9191 :     db = dc; /* = degpol(b) */
    1216             :   }
    1217        2443 :   if (ind+1 > lg(dglist)) setlg(dglist,ind+1);
    1218        2443 :   set_avma(av);
    1219             : }
    1220             : /* assuming the PRS finishes on a degree 1 polynomial C0 + C1X, with
    1221             :  * "generic" degree sequence as given by dglist, set *Ci and return
    1222             :  * resultant(a,b). Modular version of Collins's subresultant */
    1223             : static ulong
    1224       31295 : Flx_resultant_all(GEN a, GEN b, long *C0, long *C1, GEN dglist, ulong p)
    1225             : {
    1226             :   long da,db,dc, ind;
    1227       31295 :   ulong lb, res, g = 1UL, h = 1UL, ca = 1UL, cb = 1UL;
    1228       31295 :   int s = 1;
    1229       31295 :   pari_sp av = avma;
    1230             : 
    1231       31295 :   *C0 = 1; *C1 = 0;
    1232       31295 :   if (lgpol(a)==0 || lgpol(b)==0) return 0;
    1233       31253 :   da = degpol(a);
    1234       31253 :   db = degpol(b);
    1235       31253 :   if (db > da)
    1236             :   {
    1237           0 :     swapspec(a,b, da,db);
    1238           0 :     if (both_odd(da,db)) s = -s;
    1239             :   }
    1240       31253 :   else if (!da) return 1; /* = a[2] ^ db, since 0 <= db <= da = 0 */
    1241       31253 :   ind = 0;
    1242      316361 :   while (db)
    1243             :   { /* sub-resultant algo., applied to ca * a and cb * b, ca,cb scalars,
    1244             :      * da = deg a, db = deg b */
    1245      254294 :     GEN c = Flx_rem(a,b, p);
    1246      254294 :     long delta = da - db;
    1247             : 
    1248      254294 :     if (both_odd(da,db)) s = -s;
    1249      254294 :     lb = Fl_mul(b[db+2], cb, p);
    1250      254294 :     a = b; b = c; dc = degpol(c);
    1251      254294 :     ind++;
    1252      254294 :     if (dc != dglist[ind]) return gc_ulong(av,0); /* degenerates */
    1253      253855 :     if (g == h)
    1254             :     { /* frequent */
    1255      250599 :       ulong cc = Fl_mul(ca, Fl_powu(Fl_div(lb,g,p), delta+1, p), p);
    1256      250599 :       ca = cb;
    1257      250599 :       cb = cc;
    1258             :     }
    1259             :     else
    1260             :     {
    1261        3256 :       ulong cc = Fl_mul(ca, Fl_powu(lb, delta+1, p), p);
    1262        3256 :       ulong ghdelta = Fl_mul(g, Fl_powu(h, delta, p), p);
    1263        3256 :       ca = cb;
    1264        3256 :       cb = Fl_div(cc, ghdelta, p);
    1265             :     }
    1266      253855 :     da = db; /* = degpol(a) */
    1267      253855 :     db = dc; /* = degpol(b) */
    1268             : 
    1269      253855 :     g = lb;
    1270      253855 :     if (delta == 1)
    1271      242046 :       h = g; /* frequent */
    1272             :     else
    1273       11809 :       h = Fl_mul(h, Fl_powu(Fl_div(g,h,p), delta, p), p);
    1274             : 
    1275      253855 :     if (gc_needed(av,2))
    1276             :     {
    1277           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1278           0 :       gerepileall(av, 2, &a,&b);
    1279             :     }
    1280             :   }
    1281       30814 :   if (da > 1) return 0; /* Failure */
    1282             :   /* last non-constant polynomial has degree 1 */
    1283       30814 :   *C0 = Fl_mul(ca, a[2], p);
    1284       30814 :   *C1 = Fl_mul(ca, a[3], p);
    1285       30814 :   res = Fl_mul(cb, b[2], p);
    1286       30814 :   if (s == -1) res = p - res;
    1287       30814 :   return gc_ulong(av,res);
    1288             : }
    1289             : 
    1290             : /* Q a vector of polynomials representing B in Fp[X][Y], evaluate at X = x,
    1291             :  * Return 0 in case of degree drop. */
    1292             : static GEN
    1293       33759 : FlxY_evalx_drop(GEN Q, ulong x, ulong p)
    1294             : {
    1295             :   GEN z;
    1296       33759 :   long i, lb = lg(Q);
    1297       33759 :   ulong leadz = Flx_eval(leading_coeff(Q), x, p);
    1298       33759 :   long vs=mael(Q,2,1);
    1299       33759 :   if (!leadz) return zero_Flx(vs);
    1300             : 
    1301       33696 :   z = cgetg(lb, t_VECSMALL); z[1] = vs;
    1302       33696 :   for (i=2; i<lb-1; i++) z[i] = Flx_eval(gel(Q,i), x, p);
    1303       33696 :   z[i] = leadz; return z;
    1304             : }
    1305             : 
    1306             : GEN
    1307       20062 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
    1308             : {
    1309       20062 :   pari_sp av = avma;
    1310       20062 :   long i, lb = lg(Q);
    1311             :   GEN z;
    1312       20062 :   if (!T) return FpXY_evaly(Q, y, p, vx);
    1313        1232 :   if (lb == 2) return pol_0(vx);
    1314        1232 :   z = gel(Q, lb-1);
    1315        1232 :   if (lb == 3 || !signe(y)) return typ(z)==t_INT? scalar_ZX(z, vx): ZX_copy(z);
    1316             : 
    1317        1232 :   if (typ(z) == t_INT) z = scalar_ZX_shallow(z, vx);
    1318       26572 :   for (i=lb-2; i>=2; i--)
    1319             :   {
    1320       25340 :     GEN c = gel(Q,i);
    1321       25340 :     z = FqX_Fq_mul(z, y, T, p);
    1322       25340 :     z = typ(c) == t_INT? FqX_Fq_add(z,c,T,p): FqX_add(z,c,T,p);
    1323             :   }
    1324        1232 :   return gerepileupto(av, z);
    1325             : }
    1326             : 
    1327             : static GEN
    1328       16968 : ZX_norml1(GEN x)
    1329             : {
    1330       16968 :   long i, l = lg(x);
    1331             :   GEN s;
    1332             : 
    1333       16968 :   if (l == 2) return gen_0;
    1334       10346 :   s = gel(x, l-1); /* != 0 */
    1335       37548 :   for (i = l-2; i > 1; i--) {
    1336       27202 :     GEN xi = gel(x,i);
    1337       27202 :     if (!signe(x)) continue;
    1338       27202 :     s = addii_sign(s,1, xi,1);
    1339             :   }
    1340       10346 :   return s;
    1341             : }
    1342             : 
    1343             : /* Interpolate at roots of 1 and use Hadamard bound for univariate resultant:
    1344             :  *   bound = N_2(A)^degpol B N_2(B)^degpol(A),  where
    1345             :  *     N_2(A) = sqrt(sum (N_1(Ai))^2)
    1346             :  * Return e such that Res(A, B) < 2^e */
    1347             : ulong
    1348       93908 : ZX_ZXY_ResBound(GEN A, GEN B, GEN dB)
    1349             : {
    1350       93908 :   pari_sp av = avma, av2;
    1351       93908 :   GEN a = gen_0, b = gen_0;
    1352       93908 :   long i , lA = lg(A), lB = lg(B);
    1353             :   double loga, logb;
    1354      986434 :   for (i=2; i<lA; i++)
    1355             :   {
    1356      892528 :     a = addii(a, sqri(gel(A,i)));
    1357      892537 :     if (gc_needed(av,1))
    1358             :     {
    1359           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
    1360           0 :       a = gerepileupto(av, a);
    1361             :     }
    1362             :   }
    1363       93906 :   a = gerepileuptoint(av, a);
    1364       93902 :   av2 = avma;
    1365      897973 :   for (i=2; i<lB; i++)
    1366             :   {
    1367      804073 :     GEN t = gel(B,i);
    1368      804073 :     if (typ(t) == t_POL) t = ZX_norml1(t);
    1369      804073 :     b = addii(b, sqri(t));
    1370      804077 :     if (gc_needed(av2,1))
    1371             :     {
    1372           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
    1373           0 :       b = gerepileupto(av2, b);
    1374             :     }
    1375             :   }
    1376       93900 :   loga = dbllog2(a);
    1377       93899 :   logb = dbllog2(b); if (dB) logb -= 2 * dbllog2(dB);
    1378       93901 :   i = (long)((degpol(B) * loga + degpol(A) * logb) / 2);
    1379       93901 :   return gc_ulong(av, (i <= 0)? 1: 1 + (ulong)i);
    1380             : }
    1381             : 
    1382             : /* return Res(a(Y), b(n,Y)) over Fp. la = leading_coeff(a) [for efficiency] */
    1383             : static ulong
    1384      264600 : Flx_FlxY_eval_resultant(GEN a, GEN b, ulong n, ulong p, ulong la)
    1385             : {
    1386      264600 :   GEN ev = FlxY_evalx(b, n, p);
    1387      264683 :   long drop = lg(b) - lg(ev);
    1388      264683 :   ulong r = Flx_resultant(a, ev, p);
    1389      264584 :   if (drop && la != 1) r = Fl_mul(r, Fl_powu(la, drop,p),p);
    1390      264584 :   return r;
    1391             : }
    1392             : static GEN
    1393           4 : FpX_FpXY_eval_resultant(GEN a, GEN b, GEN n, GEN p, GEN la, long db, long vX)
    1394             : {
    1395           4 :   GEN ev = FpXY_evaly(b, n, p, vX);
    1396           4 :   long drop = db-degpol(ev);
    1397           4 :   GEN r = FpX_resultant(a, ev, p);
    1398           4 :   if (drop && !gequal1(la)) r = Fp_mul(r, Fp_powu(la, drop,p),p);
    1399           4 :   return r;
    1400             : }
    1401             : 
    1402             : /* assume dres := deg(Res_X(a,b), Y) <= deg(a,X) * deg(b,Y) < p */
    1403             : /* Return a Fly */
    1404             : static GEN
    1405       13483 : Flx_FlxY_resultant_polint(GEN a, GEN b, ulong p, long dres, long sx)
    1406             : {
    1407             :   long i;
    1408       13483 :   ulong n, la = Flx_lead(a);
    1409       13483 :   GEN  x = cgetg(dres+2, t_VECSMALL);
    1410       13483 :   GEN  y = cgetg(dres+2, t_VECSMALL);
    1411             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1412             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1413      140643 :   for (i=0,n = 1; i < dres; n++)
    1414             :   {
    1415      127167 :     x[++i] = n;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1416      127160 :     x[++i] = p-n; y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1417             :   }
    1418       13476 :   if (i == dres)
    1419             :   {
    1420       10719 :     x[++i] = 0;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1421             :   }
    1422       13476 :   return Flv_polint(x,y, p, sx);
    1423             : }
    1424             : 
    1425             : static GEN
    1426        6328 : FlxX_pseudorem(GEN x, GEN y, ulong p)
    1427             : {
    1428        6328 :   long vx = varn(x), dx, dy, dz, i, lx, dp;
    1429        6328 :   pari_sp av = avma, av2;
    1430             : 
    1431        6328 :   if (!signe(y)) pari_err_INV("FlxX_pseudorem",y);
    1432        6328 :   (void)new_chunk(2);
    1433        6336 :   dx=degpol(x); x = RgX_recip_shallow(x)+2;
    1434        6340 :   dy=degpol(y); y = RgX_recip_shallow(y)+2; dz=dx-dy; dp = dz+1;
    1435        6340 :   av2 = avma;
    1436             :   for (;;)
    1437             :   {
    1438       84050 :     gel(x,0) = Flx_neg(gel(x,0), p); dp--;
    1439      174879 :     for (i=1; i<=dy; i++)
    1440      254684 :       gel(x,i) = Flx_add( Flx_mul(gel(y,0), gel(x,i), p),
    1441      127342 :                               Flx_mul(gel(x,0), gel(y,i), p), p );
    1442      631896 :     for (   ; i<=dx; i++)
    1443      586791 :       gel(x,i) = Flx_mul(gel(y,0), gel(x,i), p);
    1444       47646 :     do { x++; dx--; } while (dx >= 0 && lg(gel(x,0))==2);
    1445       45105 :     if (dx < dy) break;
    1446       38820 :     if (gc_needed(av2,1))
    1447             :     {
    1448           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_pseudorem dx = %ld >= %ld",dx,dy);
    1449           0 :       gerepilecoeffs(av2,x,dx+1);
    1450             :     }
    1451             :   }
    1452        6285 :   if (dx < 0) return zero_Flx(0);
    1453        6285 :   lx = dx+3; x -= 2;
    1454        6285 :   x[0]=evaltyp(t_POL) | evallg(lx);
    1455        6282 :   x[1]=evalsigne(1) | evalvarn(vx);
    1456        6282 :   x = RgX_recip_shallow(x);
    1457        6333 :   if (dp)
    1458             :   { /* multiply by y[0]^dp   [beware dummy vars from FpX_FpXY_resultant] */
    1459        1331 :     GEN t = Flx_powu(gel(y,0), dp, p);
    1460        5297 :     for (i=2; i<lx; i++)
    1461        3965 :       gel(x,i) = Flx_mul(gel(x,i), t, p);
    1462             :   }
    1463        6334 :   return gerepilecopy(av, x);
    1464             : }
    1465             : 
    1466             : /* return a Flx */
    1467             : GEN
    1468        2052 : FlxX_resultant(GEN u, GEN v, ulong p, long sx)
    1469             : {
    1470        2052 :   pari_sp av = avma, av2;
    1471             :   long degq,dx,dy,du,dv,dr,signh;
    1472             :   GEN z,g,h,r,p1;
    1473             : 
    1474        2052 :   dx=degpol(u); dy=degpol(v); signh=1;
    1475        2054 :   if (dx < dy)
    1476             :   {
    1477           0 :     swap(u,v); lswap(dx,dy);
    1478           0 :     if (both_odd(dx, dy)) signh = -signh;
    1479             :   }
    1480        2054 :   if (dy < 0) return zero_Flx(sx);
    1481        2054 :   if (dy==0) return gerepileupto(av, Flx_powu(gel(v,2),dx,p));
    1482             : 
    1483        2054 :   g = h = pol1_Flx(sx); av2 = avma;
    1484             :   for(;;)
    1485             :   {
    1486       10584 :     r = FlxX_pseudorem(u,v,p); dr = lg(r);
    1487        6342 :     if (dr == 2) { set_avma(av); return zero_Flx(sx); }
    1488        6342 :     du = degpol(u); dv = degpol(v); degq = du-dv;
    1489        6339 :     u = v; p1 = g; g = leading_coeff(u);
    1490        6341 :     switch(degq)
    1491             :     {
    1492           0 :       case 0: break;
    1493             :       case 1:
    1494        4696 :         p1 = Flx_mul(h,p1, p); h = g; break;
    1495             :       default:
    1496        1645 :         p1 = Flx_mul(Flx_powu(h,degq,p), p1, p);
    1497        1639 :         h = Flx_div(Flx_powu(g,degq,p), Flx_powu(h,degq-1,p), p);
    1498             :     }
    1499        6312 :     if (both_odd(du,dv)) signh = -signh;
    1500        6308 :     v = FlxY_Flx_div(r, p1, p);
    1501        6317 :     if (dr==3) break;
    1502        4257 :     if (gc_needed(av2,1))
    1503             :     {
    1504           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_resultant, dr = %ld",dr);
    1505           0 :       gerepileall(av2,4, &u, &v, &g, &h);
    1506             :     }
    1507             :   }
    1508        2060 :   z = gel(v,2);
    1509        2060 :   if (dv > 1) z = Flx_div(Flx_powu(z,dv,p), Flx_powu(h,dv-1,p), p);
    1510        2060 :   if (signh < 0) z = Flx_neg(z,p);
    1511        2060 :   return gerepileupto(av, z);
    1512             : }
    1513             : 
    1514             : /* Warning:
    1515             :  * This function switches between valid and invalid variable ordering*/
    1516             : 
    1517             : static GEN
    1518        2073 : FlxY_to_FlyX(GEN b, long sv)
    1519             : {
    1520        2073 :   long i, n=-1;
    1521        2073 :   long sw = b[1]&VARNBITS;
    1522        2073 :   for(i=2;i<lg(b);i++) n = maxss(n,lgpol(gel(b,i)));
    1523        2073 :   return Flm_to_FlxX(Flm_transpose(FlxX_to_Flm(b,n)),sv,sw);
    1524             : }
    1525             : 
    1526             : /* Return a Fly*/
    1527             : GEN
    1528        2069 : Flx_FlxY_resultant(GEN a, GEN b, ulong pp)
    1529             : {
    1530        2069 :   pari_sp ltop=avma;
    1531        2069 :   long dres = degpol(a)*degpol(b);
    1532        2064 :   long sx=a[1], sy=b[1]&VARNBITS;
    1533             :   GEN z;
    1534        2064 :   b = FlxY_to_FlyX(b,sx);
    1535        2053 :   if ((ulong)dres >= pp)
    1536        2040 :     z = FlxX_resultant(Fly_to_FlxY(a, sy), b, pp, sx);
    1537             :   else
    1538          13 :     z = Flx_FlxY_resultant_polint(a, b, pp, (ulong)dres, sy);
    1539        2082 :   return gerepileupto(ltop,z);
    1540             : }
    1541             : 
    1542             : /* return a t_POL (in variable v >= 0) whose coeffs are the coeffs of b,
    1543             :  * in variable v. This is an incorrect PARI object if initially varn(b) << v.
    1544             :  * We could return a vector of coeffs, but it is convenient to have degpol()
    1545             :  * and friends available. Even in that case, it will behave nicely with all
    1546             :  * functions treating a polynomial as a vector of coeffs (eg poleval).
    1547             :  * FOR INTERNAL USE! */
    1548             : GEN
    1549       10731 : swap_vars(GEN b0, long v)
    1550             : {
    1551       10731 :   long i, n = RgX_degree(b0, v);
    1552             :   GEN b, x;
    1553       10731 :   if (n < 0) return pol_0(v);
    1554       10731 :   b = cgetg(n+3, t_POL); x = b + 2;
    1555       10731 :   b[1] = evalsigne(1) | evalvarn(v);
    1556       10731 :   for (i=0; i<=n; i++) gel(x,i) = polcoef_i(b0, i, v);
    1557       10731 :   return b;
    1558             : }
    1559             : 
    1560             : /* assume varn(b) << varn(a) */
    1561             : /* return a FpY*/
    1562             : GEN
    1563        2039 : FpX_FpXY_resultant(GEN a, GEN b, GEN p)
    1564             : {
    1565        2039 :   long i,n,dres, db, vY = varn(b), vX = varn(a);
    1566             :   GEN la,x,y;
    1567             : 
    1568        2039 :   if (lgefint(p) == 3)
    1569             :   {
    1570        2038 :     ulong pp = uel(p,2);
    1571        2038 :     b = ZXX_to_FlxX(b, pp, vX);
    1572        2037 :     a = ZX_to_Flx(a, pp);
    1573        2042 :     x = Flx_FlxY_resultant(a, b, pp);
    1574        2056 :     return Flx_to_ZX(x);
    1575             :   }
    1576           1 :   db = RgXY_degreex(b);
    1577           1 :   dres = degpol(a)*degpol(b);
    1578           1 :   la = leading_coeff(a);
    1579           1 :   x = cgetg(dres+2, t_VEC);
    1580           1 :   y = cgetg(dres+2, t_VEC);
    1581             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1582             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1583           3 :   for (i=0,n = 1; i < dres; n++)
    1584             :   {
    1585           2 :     gel(x,++i) = utoipos(n);
    1586           2 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1587           2 :     gel(x,++i) = subiu(p,n);
    1588           2 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1589             :   }
    1590           1 :   if (i == dres)
    1591             :   {
    1592           0 :     gel(x,++i) = gen_0;
    1593           0 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b, gel(x,i), p,la,db,vY);
    1594             :   }
    1595           1 :   return FpV_polint(x,y, p, vY);
    1596             : }
    1597             : 
    1598             : static GEN
    1599         182 : FpX_diamondsum(GEN P, GEN Q, GEN p)
    1600             : {
    1601         182 :   long n = 1+ degpol(P)*degpol(Q);
    1602         182 :   GEN Pl = FpX_invLaplace(FpX_Newton(P,n,p), p);
    1603         182 :   GEN Ql = FpX_invLaplace(FpX_Newton(Q,n,p), p);
    1604         182 :   GEN L = FpX_Laplace(FpXn_mul(Pl, Ql, n, p), p);
    1605         182 :   return FpX_fromNewton(L, p);
    1606             : }
    1607             : 
    1608             : #if 0
    1609             : GEN
    1610             : FpX_diamondprod(GEN P, GEN Q, GEN p)
    1611             : {
    1612             :   long n = 1+ degpol(P)*degpol(Q);
    1613             :   GEN L=FpX_convol(FpX_Newton(P,n,p), FpX_Newton(Q,n,p), p);
    1614             :   return FpX_fromNewton(L, p);
    1615             : }
    1616             : #endif
    1617             : 
    1618             : GEN
    1619         602 : FpX_direct_compositum(GEN a, GEN b, GEN p)
    1620             : {
    1621         602 :   long da = degpol(a), db = degpol(b);
    1622         602 :   if (cmpis(p, da*db) > 0)
    1623         182 :     return FpX_diamondsum(a, b, p);
    1624             :   else
    1625             :   {
    1626         420 :     long v = varn(a), w = fetch_var_higher();
    1627         420 :     GEN mx = deg1pol_shallow(gen_m1, gen_0, v);
    1628         420 :     GEN r, ymx = deg1pol_shallow(gen_1, mx, w); /* Y-X */
    1629         420 :     if (degpol(a) < degpol(b)) swap(a,b);
    1630         420 :     r = FpX_FpXY_resultant(a, poleval(b,ymx),p);
    1631         420 :     setvarn(r, v); (void)delete_var(); return r;
    1632             :   }
    1633             : }
    1634             : 
    1635             : static GEN
    1636         602 : _FpX_direct_compositum(void *E, GEN a, GEN b)
    1637         602 : { return FpX_direct_compositum(a,b, (GEN)E); }
    1638             : 
    1639             : GEN
    1640        5410 : FpXV_direct_compositum(GEN V, GEN p)
    1641             : {
    1642        5410 :   return gen_product(V, (void *)p, &_FpX_direct_compositum);
    1643             : }
    1644             : 
    1645             : /* 0, 1, -1, 2, -2, ... */
    1646             : #define next_lambda(a) (a>0 ? -a : 1-a)
    1647             : GEN
    1648           0 : FpX_compositum(GEN a, GEN b, GEN p)
    1649             : {
    1650           0 :   long k, v = fetch_var_higher();
    1651           0 :   for (k = 1;; k = next_lambda(k))
    1652           0 :   {
    1653           0 :     GEN x = deg1pol_shallow(gen_1, gmulsg(k, pol_x(v)), 0); /* x + k y */
    1654           0 :     GEN C = FpX_FpXY_resultant(a, poleval(b,x),p);
    1655           0 :     if (FpX_is_squarefree(C, p)) { (void)delete_var(); return C; }
    1656             :   }
    1657             : }
    1658             : 
    1659             : /* Assume A in Z[Y], B in Q[Y][X], B squarefree in (Q[Y]/(A))[X] and
    1660             :  * Res_Y(A, B) in Z[X]. Find a small lambda (start from *lambda, use
    1661             :  * next_lambda successively) such that C(X) = Res_Y(A(Y), B(X + lambda Y))
    1662             :  * is squarefree, reset *lambda to the chosen value and return C. Set LERS to
    1663             :  * the Last non-constant polynomial in the Euclidean Remainder Sequence */
    1664             : static GEN
    1665        2751 : ZX_ZXY_resultant_LERS(GEN A, GEN B0, long *plambda, GEN *LERS)
    1666             : {
    1667             :   ulong bound, dp;
    1668        2751 :   pari_sp av = avma, av2 = 0;
    1669        2751 :   long lambda = *plambda, degA = degpol(A), dres = degA*degpol(B0);
    1670             :   long stable, checksqfree, i,n, cnt, degB;
    1671        2751 :   long v, vX = varn(B0), vY = varn(A); /* vY < vX */
    1672             :   GEN x, y, dglist, B, q, a, b, ev, H, H0, H1, Hp, H0p, H1p, C0, C1;
    1673             :   forprime_t S;
    1674             : 
    1675        2751 :   if (degA == 1)
    1676             :   {
    1677         616 :     GEN a1 = gel(A,3), a0 = gel(A,2);
    1678         616 :     B = lambda? RgX_translate(B0, monomial(stoi(lambda), 1, vY)): B0;
    1679         616 :     H = gsubst(B, vY, gdiv(gneg(a0),a1));
    1680         616 :    if (!equali1(a1)) H = RgX_Rg_mul(H, powiu(a1, poldegree(B,vY)));
    1681         616 :     *LERS = mkvec2(scalarpol_shallow(a0,vX), scalarpol_shallow(a1,vX));
    1682         616 :     gerepileall(av, 2, &H, LERS);
    1683         616 :     return H;
    1684             :   }
    1685             : 
    1686        2135 :   dglist = Hp = H0p = H1p = C0 = C1 = NULL; /* gcc -Wall */
    1687        2135 :   C0 = cgetg(dres+2, t_VECSMALL);
    1688        2135 :   C1 = cgetg(dres+2, t_VECSMALL);
    1689        2135 :   dglist = cgetg(dres+1, t_VECSMALL);
    1690        2135 :   x = cgetg(dres+2, t_VECSMALL);
    1691        2135 :   y = cgetg(dres+2, t_VECSMALL);
    1692        2135 :   B0 = leafcopy(B0);
    1693        2135 :   A = leafcopy(A);
    1694        2135 :   B = B0;
    1695        2135 :   v = fetch_var_higher(); setvarn(A,v);
    1696             :   /* make sure p large enough */
    1697             : INIT:
    1698             :   /* always except the first time */
    1699        2737 :   if (av2) { set_avma(av2); lambda = next_lambda(lambda); }
    1700        2737 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    1701        2737 :   B = swap_vars(B, vY); setvarn(B,v);
    1702             :   /* B0(lambda v + x, v) */
    1703        2737 :   if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    1704        2737 :   av2 = avma;
    1705             : 
    1706        2737 :   if (degA <= 3)
    1707             :   { /* sub-resultant faster for small degrees */
    1708        2170 :     H = RgX_resultant_all(A,B,&q);
    1709        2170 :     if (typ(q) != t_POL || degpol(q)!=1) goto INIT;
    1710        1715 :     H0 = gel(q,2);
    1711        1715 :     if (typ(H0) == t_POL) setvarn(H0,vX); else H0 = scalarpol(H0,vX);
    1712        1715 :     H1 = gel(q,3);
    1713        1715 :     if (typ(H1) == t_POL) setvarn(H1,vX); else H1 = scalarpol(H1,vX);
    1714        1715 :     if (!ZX_is_squarefree(H)) goto INIT;
    1715        1673 :     goto END;
    1716             :   }
    1717             : 
    1718         567 :   H = H0 = H1 = NULL;
    1719         567 :   degB = degpol(B);
    1720         567 :   bound = ZX_ZXY_ResBound(A, B, NULL);
    1721         567 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    1722         567 :   dp = 1;
    1723         567 :   init_modular_big(&S);
    1724         567 :   for(cnt = 0, checksqfree = 1;;)
    1725        1118 :   {
    1726        1685 :     ulong p = u_forprime_next(&S);
    1727             :     GEN Hi;
    1728        1685 :     a = ZX_to_Flx(A, p);
    1729        1685 :     b = ZXX_to_FlxX(B, p, varn(A));
    1730        1685 :     if (degpol(a) < degA || degpol(b) < degB) continue; /* p | lc(A)lc(B) */
    1731        1685 :     if (checksqfree)
    1732             :     { /* find degree list for generic Euclidean Remainder Sequence */
    1733         567 :       long goal = minss(degpol(a), degpol(b)); /* longest possible */
    1734         567 :       for (n=1; n <= goal; n++) dglist[n] = 0;
    1735         567 :       setlg(dglist, 1);
    1736        2604 :       for (n=0; n <= dres; n++)
    1737             :       {
    1738        2464 :         ev = FlxY_evalx_drop(b, n, p);
    1739        2464 :         Flx_resultant_set_dglist(a, ev, dglist, p);
    1740        2464 :         if (lg(dglist)-1 == goal) break;
    1741             :       }
    1742             :       /* last pol in ERS has degree > 1 ? */
    1743         567 :       goal = lg(dglist)-1;
    1744         567 :       if (degpol(B) == 1) { if (!goal) goto INIT; }
    1745             :       else
    1746             :       {
    1747         560 :         if (goal <= 1) goto INIT;
    1748         497 :         if (dglist[goal] != 0 || dglist[goal-1] != 1) goto INIT;
    1749             :       }
    1750         504 :       if (DEBUGLEVEL>4)
    1751           0 :         err_printf("Degree list for ERS (trials: %ld) = %Ps\n",n+1,dglist);
    1752             :     }
    1753             : 
    1754       32917 :     for (i=0,n = 0; i <= dres; n++)
    1755             :     {
    1756       31295 :       ev = FlxY_evalx_drop(b, n, p);
    1757       31295 :       x[++i] = n; y[i] = Flx_resultant_all(a, ev, C0+i, C1+i, dglist, p);
    1758       31295 :       if (!C1[i]) i--; /* C1(i) = 0. No way to recover C0(i) */
    1759             :     }
    1760        1622 :     Hi = Flv_Flm_polint(x, mkvec3(y,C0,C1), p, 0);
    1761        1622 :     Hp = gel(Hi,1); H0p = gel(Hi,2); H1p = gel(Hi,3);
    1762        1622 :     if (!H && degpol(Hp) != dres) continue;
    1763        1622 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1764        1622 :     if (checksqfree) {
    1765         504 :       if (!Flx_is_squarefree(Hp, p)) goto INIT;
    1766         462 :       if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    1767         462 :       checksqfree = 0;
    1768             :     }
    1769             : 
    1770        1580 :     if (!H)
    1771             :     { /* initialize */
    1772         462 :       q = utoipos(p); stable = 0;
    1773         462 :       H = ZX_init_CRT(Hp, p,vX);
    1774         462 :       H0= ZX_init_CRT(H0p, p,vX);
    1775         462 :       H1= ZX_init_CRT(H1p, p,vX);
    1776             :     }
    1777             :     else
    1778             :     {
    1779        1118 :       GEN qp = muliu(q,p);
    1780        2236 :       stable  = ZX_incremental_CRT_raw(&H, Hp, q,qp, p)
    1781        1118 :               & ZX_incremental_CRT_raw(&H0,H0p, q,qp, p)
    1782        1118 :               & ZX_incremental_CRT_raw(&H1,H1p, q,qp, p);
    1783        1118 :       q = qp;
    1784             :     }
    1785             :     /* could make it probabilistic for H ? [e.g if stable twice, etc]
    1786             :      * Probabilistic anyway for H0, H1 */
    1787        1580 :     if (DEBUGLEVEL>5 && (stable ||  ++cnt==100))
    1788           0 :     { cnt=0; err_printf("%ld%%%s ",100*expi(q)/bound,stable?"s":""); }
    1789        1580 :     if (stable && (ulong)expi(q) >= bound) break; /* DONE */
    1790        1118 :     if (gc_needed(av,2))
    1791             :     {
    1792           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_rnfequation");
    1793           0 :       gerepileall(av2, 4, &H, &q, &H0, &H1);
    1794             :     }
    1795             :   }
    1796             : END:
    1797        2135 :   if (DEBUGLEVEL>5) err_printf(" done\n");
    1798        2135 :   setvarn(H, vX); (void)delete_var();
    1799        2135 :   *LERS = mkvec2(H0,H1);
    1800        2135 :   gerepileall(av, 2, &H, LERS);
    1801        2135 :   *plambda = lambda; return H;
    1802             : }
    1803             : 
    1804             : GEN
    1805        3654 : ZX_ZXY_resultant_all(GEN A, GEN B, long *plambda, GEN *LERS)
    1806             : {
    1807        3654 :   if (LERS)
    1808             :   {
    1809        2751 :     if (!plambda)
    1810           0 :       pari_err_BUG("ZX_ZXY_resultant_all [LERS != NULL needs lambda]");
    1811        2751 :     return ZX_ZXY_resultant_LERS(A, B, plambda, LERS);
    1812             :   }
    1813         903 :   return ZX_ZXY_rnfequation(A, B, plambda);
    1814             : }
    1815             : 
    1816             : /* If lambda = NULL, return caract(Mod(A, T)), T,A in Z[X].
    1817             :  * Otherwise find a small lambda such that caract (Mod(A + lambda X, T)) is
    1818             :  * squarefree */
    1819             : GEN
    1820        1869 : ZXQ_charpoly_sqf(GEN A, GEN T, long *lambda, long v)
    1821             : {
    1822        1869 :   pari_sp av = avma;
    1823             :   GEN R, a;
    1824             :   long dA;
    1825             :   int delvar;
    1826             : 
    1827        1869 :   if (v < 0) v = 0;
    1828        1869 :   switch (typ(A))
    1829             :   {
    1830        1869 :     case t_POL: dA = degpol(A); if (dA > 0) break;
    1831           0 :       A = constant_coeff(A);
    1832             :     default:
    1833           0 :       if (lambda) { A = scalar_ZX_shallow(A,varn(T)); dA = 0; break;}
    1834           0 :       return gerepileupto(av, gpowgs(gsub(pol_x(v), A), degpol(T)));
    1835             :   }
    1836        1869 :   delvar = 0;
    1837        1869 :   if (varn(T) == 0)
    1838             :   {
    1839        1785 :     long v0 = fetch_var(); delvar = 1;
    1840        1785 :     T = leafcopy(T); setvarn(T,v0);
    1841        1785 :     A = leafcopy(A); setvarn(A,v0);
    1842             :   }
    1843        1869 :   R = ZX_ZXY_rnfequation(T, deg1pol_shallow(gen_1, gneg_i(A), 0), lambda);
    1844        1869 :   if (delvar) (void)delete_var();
    1845        1869 :   setvarn(R, v); a = leading_coeff(T);
    1846        1869 :   if (!gequal1(a)) R = gdiv(R, powiu(a, dA));
    1847        1869 :   return gerepileupto(av, R);
    1848             : }
    1849             : 
    1850             : 
    1851             : /* charpoly(Mod(A,T)), A may be in Q[X], but assume T and result are integral */
    1852             : GEN
    1853       13306 : ZXQ_charpoly(GEN A, GEN T, long v)
    1854             : {
    1855       13306 :   return (degpol(T) < 16) ? RgXQ_charpoly(A,T,v): ZXQ_charpoly_sqf(A,T, NULL, v);
    1856             : }
    1857             : 
    1858             : GEN
    1859         847 : QXQ_charpoly(GEN A, GEN T, long v)
    1860             : {
    1861         847 :   pari_sp av = avma;
    1862         847 :   GEN den, B = Q_remove_denom(A, &den);
    1863         847 :   GEN P = ZXQ_charpoly(B, T, v);
    1864         847 :   return gerepilecopy(av, den ? RgX_rescale(P, ginv(den)): P);
    1865             : }
    1866             : 
    1867             : static GEN
    1868      191909 : trivial_case(GEN A, GEN B)
    1869             : {
    1870             :   long d;
    1871      191909 :   if (typ(A) == t_INT) return powiu(A, degpol(B));
    1872      183907 :   d = degpol(A);
    1873      183908 :   if (d == 0) return trivial_case(gel(A,2),B);
    1874      180813 :   if (d < 0) return gen_0;
    1875      180791 :   return NULL;
    1876             : }
    1877             : 
    1878             : static ulong
    1879     1388564 : ZX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, ulong p)
    1880             : {
    1881     1388564 :   pari_sp av = avma;
    1882             :   ulong H;
    1883             :   long dropa, dropb;
    1884     1388564 :   ulong dp = dB ? umodiu(dB, p): 1;
    1885     1388573 :   if (!b) b = Flx_deriv(a, p);
    1886     1388651 :   dropa = degA - degpol(a);
    1887     1388584 :   dropb = degB - degpol(b);
    1888     1388500 :   if (dropa && dropb) return gc_ulong(av,0); /* p | lc(A), p | lc(B) */
    1889     1388500 :   H = Flx_resultant(a, b, p);
    1890     1387204 :   if (dropa)
    1891             :   { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    1892           0 :     ulong c = b[degB+2]; /* lc(B) */
    1893           0 :     if (odd(degB)) c = p - c;
    1894           0 :     c = Fl_powu(c, dropa, p);
    1895           0 :     if (c != 1) H = Fl_mul(H, c, p);
    1896             :   }
    1897     1387204 :   else if (dropb)
    1898             :   { /* multiply by lc(A)^(deg B - deg b) */
    1899           0 :     ulong c = a[degA+2]; /* lc(A) */
    1900           0 :     c = Fl_powu(c, dropb, p);
    1901           0 :     if (c != 1) H = Fl_mul(H, c, p);
    1902             :   }
    1903     1387207 :   if (dp != 1) H = Fl_mul(H, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1904     1387222 :   return gc_ulong(av,H);
    1905             : }
    1906             : 
    1907             : /* If B=NULL, assume B=A' */
    1908             : static GEN
    1909      595350 : ZX_resultant_slice(GEN A, GEN B, GEN dB, GEN P, GEN *mod)
    1910             : {
    1911      595350 :   pari_sp av = avma;
    1912      595350 :   long degA, degB, i, n = lg(P)-1;
    1913             :   GEN H, T;
    1914             : 
    1915      595350 :   degA = degpol(A);
    1916      595347 :   degB = B ? degpol(B): degA - 1;
    1917      595351 :   if (n == 1)
    1918             :   {
    1919      199476 :     ulong Hp, p = uel(P,1);
    1920             :     GEN a, b;
    1921      199476 :     a = ZX_to_Flx(A, p), b = B ? ZX_to_Flx(B, p): NULL;
    1922      199464 :     Hp = ZX_resultant_prime(a, b, dB, degA, degB, p);
    1923      199253 :     set_avma(av);
    1924      199246 :     *mod = utoi(p); return utoi(Hp);
    1925             :   }
    1926      395875 :   T = ZV_producttree(P);
    1927      395882 :   A = ZX_nv_mod_tree(A, P, T);
    1928      395847 :   if (B) B = ZX_nv_mod_tree(B, P, T);
    1929      395847 :   H = cgetg(n+1, t_VECSMALL);
    1930     1583873 :   for(i=1; i <= n; i++)
    1931             :   {
    1932     1189026 :     ulong p = P[i];
    1933     1189026 :     GEN a = gel(A,i), b = B? gel(B,i): NULL;
    1934     1189026 :     H[i] = ZX_resultant_prime(a, b, dB, degA, degB, p);
    1935             :   }
    1936      394847 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    1937      395627 :   *mod = gmael(T, lg(T)-1, 1);
    1938      395627 :   gerepileall(av, 2, &H, mod);
    1939      395790 :   return H;
    1940             : }
    1941             : 
    1942             : GEN
    1943      595360 : ZX_resultant_worker(GEN P, GEN A, GEN B, GEN dB)
    1944             : {
    1945      595360 :   GEN V = cgetg(3, t_VEC);
    1946      595349 :   if (isintzero(B)) B = NULL;
    1947      595352 :   if (isintzero(dB)) dB = NULL;
    1948      595351 :   gel(V,1) = ZX_resultant_slice(A,B,dB,P,&gel(V,2));
    1949      594985 :   return V;
    1950             : }
    1951             : 
    1952             : /* Res(A, B/dB), assuming the A,B in Z[X] and result is integer */
    1953             : /* if B=NULL, take B = A' */
    1954             : GEN
    1955       97842 : ZX_resultant_all(GEN A, GEN B, GEN dB, ulong bound)
    1956             : {
    1957       97842 :   pari_sp av = avma;
    1958             :   long m;
    1959             :   GEN  H, worker;
    1960       97842 :   int is_disc = !B;
    1961       97842 :   if (is_disc) B = ZX_deriv(A);
    1962       97838 :   if ((H = trivial_case(A,B)) || (H = trivial_case(B,A))) return H;
    1963       89813 :   if (!bound) bound = ZX_ZXY_ResBound(A, B, dB);
    1964       89813 :   if (is_disc)
    1965       57739 :     B = NULL;
    1966       89813 :   worker = strtoclosure("_ZX_resultant_worker", 3, A, B?B:gen_0, dB?dB:gen_0);
    1967       89820 :   m = degpol(A)+(B ? degpol(B): 0);
    1968       89820 :   H = gen_crt("ZX_resultant_all", worker, dB, bound, m, NULL,
    1969             :                ZV_chinese_center, Fp_center);
    1970       89821 :   return gerepileuptoint(av, H);
    1971             : }
    1972             : 
    1973             : /* A0 and B0 in Q[X] */
    1974             : GEN
    1975       15268 : QX_resultant(GEN A0, GEN B0)
    1976             : {
    1977             :   GEN s, a, b, A, B;
    1978       15268 :   pari_sp av = avma;
    1979             : 
    1980       15268 :   A = Q_primitive_part(A0, &a);
    1981       15268 :   B = Q_primitive_part(B0, &b);
    1982       15268 :   s = ZX_resultant(A, B);
    1983       15268 :   if (!signe(s)) { set_avma(av); return gen_0; }
    1984       15268 :   if (a) s = gmul(s, gpowgs(a,degpol(B)));
    1985       15268 :   if (b) s = gmul(s, gpowgs(b,degpol(A)));
    1986       15268 :   return gerepileupto(av, s);
    1987             : }
    1988             : 
    1989             : GEN
    1990       39431 : ZX_resultant(GEN A, GEN B) { return ZX_resultant_all(A,B,NULL,0); }
    1991             : 
    1992             : GEN
    1993           0 : QXQ_intnorm(GEN A, GEN B)
    1994             : {
    1995             :   GEN c, n, R, lB;
    1996           0 :   long dA = degpol(A), dB = degpol(B);
    1997           0 :   pari_sp av = avma;
    1998           0 :   if (dA < 0) return gen_0;
    1999           0 :   A = Q_primitive_part(A, &c);
    2000           0 :   if (!c || typ(c) == t_INT) {
    2001           0 :     n = c;
    2002           0 :     R = ZX_resultant(B, A);
    2003             :   } else {
    2004           0 :     n = gel(c,1);
    2005           0 :     R = ZX_resultant_all(B, A, gel(c,2), 0);
    2006             :   }
    2007           0 :   if (n && !equali1(n)) R = mulii(R, powiu(n, dB));
    2008           0 :   lB = leading_coeff(B);
    2009           0 :   if (!equali1(lB)) R = diviiexact(R, powiu(lB, dA));
    2010           0 :   return gerepileuptoint(av, R);
    2011             : }
    2012             : 
    2013             : GEN
    2014           0 : QXQ_norm(GEN A, GEN B)
    2015             : {
    2016             :   GEN c, R, lB;
    2017           0 :   long dA = degpol(A), dB = degpol(B);
    2018           0 :   pari_sp av = avma;
    2019           0 :   if (dA < 0) return gen_0;
    2020           0 :   A = Q_primitive_part(A, &c);
    2021           0 :   R = ZX_resultant(B, A);
    2022           0 :   if (c) R = gmul(R, gpowgs(c, dB));
    2023           0 :   lB = leading_coeff(B);
    2024           0 :   if (!equali1(lB)) R = gdiv(R, gpowgs(lB, dA));
    2025           0 :   return gerepileupto(av, R);
    2026             : }
    2027             : 
    2028             : /* assume x has integral coefficients */
    2029             : GEN
    2030       59453 : ZX_disc_all(GEN x, ulong bound)
    2031             : {
    2032       59453 :   pari_sp av = avma;
    2033             :   GEN l, R;
    2034       59453 :   long s, d = degpol(x);
    2035       59452 :   if (d <= 1) return d ? gen_1: gen_0;
    2036       57744 :   s = (d & 2) ? -1: 1;
    2037       57744 :   l = leading_coeff(x);
    2038       57744 :   R = ZX_resultant_all(x, NULL, NULL, bound);
    2039       57748 :   if (is_pm1(l))
    2040       54871 :   { if (signe(l) < 0) s = -s; }
    2041             :   else
    2042        2877 :     R = diviiexact(R,l);
    2043       57748 :   if (s == -1) togglesign_safe(&R);
    2044       57748 :   return gerepileuptoint(av,R);
    2045             : }
    2046       58381 : GEN ZX_disc(GEN x) { return ZX_disc_all(x,0); }
    2047             : 
    2048             : GEN
    2049          21 : QX_disc(GEN x)
    2050             : {
    2051          21 :   pari_sp av = avma;
    2052          21 :   GEN c, d = ZX_disc( Q_primitive_part(x, &c) );
    2053          21 :   if (c) d = gmul(d, gpowgs(c, 2*degpol(x) - 2));
    2054          21 :   return gerepileupto(av, d);
    2055             : }
    2056             : 
    2057             : GEN
    2058       44559 : QXQ_mul(GEN x, GEN y, GEN T)
    2059             : {
    2060       44559 :   GEN dx, nx = Q_primitive_part(x, &dx);
    2061       44559 :   GEN dy, ny = Q_primitive_part(y, &dy);
    2062       44559 :   GEN z = ZXQ_mul(nx, ny, T);
    2063       44559 :   if (dx || dy)
    2064             :   {
    2065       44559 :     GEN d = dx ? dy ? gmul(dx, dy): dx : dy;
    2066       44559 :     if (!gequal1(d)) z = ZX_Q_mul(z, d);
    2067             :   }
    2068       44559 :   return z;
    2069             : }
    2070             : 
    2071             : GEN
    2072       11403 : QXQ_sqr(GEN x, GEN T)
    2073             : {
    2074       11403 :   GEN dx, nx = Q_primitive_part(x, &dx);
    2075       11403 :   GEN z = ZXQ_sqr(nx, T);
    2076       11403 :   if (dx)
    2077       11403 :     z = ZX_Q_mul(z, gsqr(dx));
    2078       11403 :   return z;
    2079             : }
    2080             : 
    2081             : /* lift(1 / Mod(A,B)). B a ZX, A a scalar or a QX */
    2082             : GEN
    2083       30598 : QXQ_inv(GEN A, GEN B)
    2084             : {
    2085             :   GEN D, cU, q, U, V;
    2086             :   ulong p;
    2087       30598 :   pari_sp av2, av = avma;
    2088             :   forprime_t S;
    2089             :   pari_timer ti;
    2090       30598 :   if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
    2091             :   /* A a QX, B a ZX */
    2092       30598 :   A = Q_primitive_part(A, &D);
    2093             :   /* A, B in Z[X] */
    2094       30598 :   init_modular_small(&S);
    2095       30598 :   if (DEBUGLEVEL>5) timer_start(&ti);
    2096       30598 :   av2 = avma; U = NULL;
    2097      172010 :   while ((p = u_forprime_next(&S)))
    2098             :   {
    2099             :     GEN a, b, qp, Up, Vp;
    2100             :     int stable;
    2101             : 
    2102      141412 :     a = ZX_to_Flx(A, p);
    2103      141412 :     b = ZX_to_Flx(B, p);
    2104             :     /* if p | Res(A/G, B/G), discard */
    2105      171996 :     if (!Flx_extresultant(b,a,p, &Vp,&Up)) continue;
    2106             : 
    2107      141398 :     if (!U)
    2108             :     { /* First time */
    2109       30584 :       U = ZX_init_CRT(Up,p,varn(A));
    2110       30584 :       V = ZX_init_CRT(Vp,p,varn(A));
    2111       30584 :       q = utoipos(p); continue;
    2112             :     }
    2113      110814 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: mod %ld (bound 2^%ld)", p,expi(q));
    2114      110814 :     qp = muliu(q,p);
    2115      221628 :     stable = ZX_incremental_CRT_raw(&U, Up, q,qp, p)
    2116      110814 :            & ZX_incremental_CRT_raw(&V, Vp, q,qp, p);
    2117      110814 :     if (stable)
    2118             :     { /* all stable: check divisibility */
    2119       30584 :       GEN res = ZX_add(ZX_mul(A,U), ZX_mul(B,V));
    2120       30584 :       if (degpol(res) == 0) {
    2121       30584 :         res = gel(res,2);
    2122       30584 :         D = D? gmul(D, res): res;
    2123       61168 :         break;
    2124             :       } /* DONE */
    2125           0 :       if (DEBUGLEVEL) err_printf("QXQ_inv: char 0 check failed");
    2126             :     }
    2127       80230 :     q = qp;
    2128       80230 :     if (gc_needed(av,1))
    2129             :     {
    2130          23 :       if (DEBUGMEM>1) pari_warn(warnmem,"QXQ_inv");
    2131          23 :       gerepileall(av2, 3, &q,&U,&V);
    2132             :     }
    2133             :   }
    2134       30584 :   if (!p) pari_err_OVERFLOW("QXQ_inv [ran out of primes]");
    2135       30584 :   cU = ZX_content(U);
    2136       30584 :   if (!is_pm1(cU)) { U = Q_div_to_int(U, cU); D = gdiv(D, cU); }
    2137       30584 :   return gerepileupto(av, RgX_Rg_div(U, D));
    2138             : }
    2139             : 
    2140             : /* lift(C / Mod(A,B)). B monic ZX, A and C scalar or QX. Use when result is
    2141             :  * small */
    2142             : GEN
    2143         273 : QXQ_div_ratlift(GEN C, GEN A, GEN B)
    2144             : {
    2145             :   GEN dA, dC, q, U;
    2146             :   ulong p, ct, delay;
    2147         273 :   pari_sp av2, av = avma;
    2148             :   forprime_t S;
    2149             :   pari_timer ti;
    2150         273 :   if (is_scalar_t(typ(A)))
    2151             :   {
    2152           0 :     A = gdiv(C,A);
    2153           0 :     if (typ(A) != t_POL) A = scalarpol(A, varn(B));
    2154           0 :     return A;
    2155             :   }
    2156             :   /* A a QX, B a ZX */
    2157         273 :   A = Q_remove_denom(A, &dA);
    2158         273 :   C = Q_remove_denom(C, &dC);
    2159         273 :   if (typ(C) != t_POL) C = scalarpol_shallow(C, varn(B));
    2160         273 :   if (dA) C = ZX_Z_mul(C,dA);
    2161             :   /* A, B, C in Z[X] */
    2162         273 :   init_modular_small(&S);
    2163         273 :   if (DEBUGLEVEL>5) timer_start(&ti);
    2164         273 :   av2 = avma; U = NULL; ct = 0; delay = 1;
    2165        1938 :   while ((p = u_forprime_next(&S)))
    2166             :   {
    2167             :     GEN a, b, Up, Ur;
    2168        1665 :     a = ZX_to_Flx(A, p);
    2169        1665 :     b = ZX_to_Flx(B, p);
    2170             :     /* if p | Res(A/G, B/G), discard */
    2171        1665 :     Up = Flxq_invsafe(a,b,p); if (!Up) continue;
    2172        1665 :     Up = Flxq_mul(Up, ZX_to_Flx(C,p), b, p);
    2173             : 
    2174        1665 :     if (!U)
    2175             :     { /* First time */
    2176         273 :       U = ZX_init_CRT(Up,p,varn(A));
    2177         273 :       q = utoipos(p);
    2178             :     }
    2179             :     else
    2180             :     {
    2181        1392 :       GEN qp = muliu(q,p);
    2182        1392 :       (void)ZX_incremental_CRT_raw(&U, Up, q,qp, p);
    2183        1392 :       q = qp;
    2184             :     }
    2185        1665 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_div: mod %ld (bound 2^%ld)", p,expi(q));
    2186        1665 :     b = sqrti(shifti(q,-1));
    2187        1665 :     Ur = FpX_ratlift(U,q,b,b,NULL);
    2188        1665 :     if (Ur && ++ct == delay)
    2189             :     { /* check divisibility */
    2190         287 :       GEN d, V = Q_remove_denom(Ur,&d), W = d? ZX_Z_mul(C,d): C;
    2191         287 :       if (!signe(ZX_rem(ZX_sub(ZX_mul(A,V), W), B))) { U = Ur; break; }
    2192          14 :       delay <<= 1;
    2193          14 :       if (DEBUGLEVEL) err_printf("QXQ_div: check failed, delay = %ld",delay);
    2194             :     }
    2195        1392 :     if (gc_needed(av,1))
    2196             :     {
    2197           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"QXQ_div");
    2198           0 :       gerepileall(av2, 2, &q,&U);
    2199             :     }
    2200             :   }
    2201         273 :   if (!p) pari_err_OVERFLOW("QXQ_div [ran out of primes]");
    2202         273 :   if (!dC) return gerepilecopy(av, U);
    2203           0 :   return gerepileupto(av, RgX_Rg_div(U, dC));
    2204             : }
    2205             : 
    2206             : /************************************************************************
    2207             :  *                                                                      *
    2208             :  *                   ZX_ZXY_resultant                                   *
    2209             :  *                                                                      *
    2210             :  ************************************************************************/
    2211             : 
    2212             : static GEN
    2213       13470 : ZX_ZXY_resultant_prime(GEN a, GEN b, ulong dp, ulong p,
    2214             :                        long degA, long degB, long dres, long sX)
    2215             : {
    2216       13470 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    2217       13470 :   GEN Hp = Flx_FlxY_resultant_polint(a, b, p, dres, sX);
    2218       13467 :   if (dropa && dropb)
    2219           0 :     Hp = zero_Flx(sX);
    2220             :   else {
    2221       13467 :     if (dropa)
    2222             :     { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    2223           0 :       GEN c = gel(b,degB+2); /* lc(B) */
    2224           0 :       if (odd(degB)) c = Flx_neg(c, p);
    2225           0 :       if (!Flx_equal1(c)) {
    2226           0 :         c = Flx_powu(c, dropa, p);
    2227           0 :         if (!Flx_equal1(c)) Hp = Flx_mul(Hp, c, p);
    2228             :       }
    2229             :     }
    2230       13467 :     else if (dropb)
    2231             :     { /* multiply by lc(A)^(deg B - deg b) */
    2232           0 :       ulong c = uel(a, degA+2); /* lc(A) */
    2233           0 :       c = Fl_powu(c, dropb, p);
    2234           0 :       if (c != 1) Hp = Flx_Fl_mul(Hp, c, p);
    2235             :     }
    2236             :   }
    2237       13467 :   if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    2238       13467 :   return Hp;
    2239             : }
    2240             : 
    2241             : static GEN
    2242        9462 : ZX_ZXY_resultant_slice(GEN A, GEN B, GEN dB, long degA, long degB, long dres,
    2243             :                        GEN P, GEN *mod, long sX, long vY)
    2244             : {
    2245        9462 :   pari_sp av = avma;
    2246        9462 :   long i, n = lg(P)-1;
    2247             :   GEN H, T, D;
    2248        9462 :   if (n == 1)
    2249             :   {
    2250        9055 :     ulong p = uel(P,1);
    2251        9055 :     ulong dp = dB ? umodiu(dB, p): 1;
    2252        9055 :     GEN a = ZX_to_Flx(A, p), b = ZXX_to_FlxX(B, p, vY);
    2253        9055 :     GEN Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2254        9052 :     H = Flx_to_ZX(Hp);
    2255        9051 :     *mod = utoi(p);
    2256        9052 :     gerepileall(av, 2, &H, mod);
    2257        9054 :     return H;
    2258             :   }
    2259         407 :   T = ZV_producttree(P);
    2260         407 :   A = ZX_nv_mod_tree(A, P, T);
    2261         407 :   B = ZXX_nv_mod_tree(B, P, T, vY);
    2262         407 :   D = dB ? Z_ZV_mod_tree(dB, P, T): NULL;
    2263         407 :   H = cgetg(n+1, t_VEC);
    2264        1441 :   for(i=1; i <= n; i++)
    2265             :   {
    2266        1034 :     ulong p = P[i];
    2267        1034 :     GEN a = gel(A,i), b = gel(B,i);
    2268        1034 :     ulong dp = D ? uel(D, i): 1;
    2269        1034 :     gel(H,i) = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2270             :   }
    2271         407 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2272         407 :   *mod = gmael(T, lg(T)-1, 1);
    2273         407 :   gerepileall(av, 2, &H, mod);
    2274         407 :   return H;
    2275             : }
    2276             : 
    2277             : GEN
    2278        9462 : ZX_ZXY_resultant_worker(GEN P, GEN A, GEN B, GEN dB, GEN v)
    2279             : {
    2280        9462 :   GEN V = cgetg(3, t_VEC);
    2281        9462 :   if (isintzero(dB)) dB = NULL;
    2282        9462 :   gel(V,1) = ZX_ZXY_resultant_slice(A, B, dB, v[1], v[2], v[3], P, &gel(V,2), v[4], v[5]);
    2283        9461 :   return V;
    2284             : }
    2285             : 
    2286             : GEN
    2287        4578 : ZX_ZXY_resultant(GEN A, GEN B)
    2288             : {
    2289        4578 :   pari_sp av = avma;
    2290             :   ulong bound;
    2291        4578 :   long v = fetch_var_higher();
    2292        4578 :   long degA = degpol(A), degB, dres = degA * degpol(B);
    2293        4578 :   long vX = varn(B), vY = varn(A); /* assume vY has lower priority */
    2294        4578 :   long sX = evalvarn(vX);
    2295             :   GEN worker, H, dB;
    2296        4578 :   B = Q_remove_denom(B, &dB);
    2297        4578 :   if (!dB) B = leafcopy(B);
    2298        4578 :   A = leafcopy(A); setvarn(A,v);
    2299        4578 :   B = swap_vars(B, vY); setvarn(B,v); degB = degpol(B);
    2300        4578 :   bound = ZX_ZXY_ResBound(A, B, dB);
    2301        4578 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    2302        4578 :   worker = strtoclosure("_ZX_ZXY_resultant_worker", 4, A, B, dB?dB:gen_0,
    2303             :                         mkvecsmall5(degA, degB,dres, vY, sX));
    2304        4578 :   H = gen_crt("ZX_ZXY_resultant_all", worker, dB, bound, degpol(A)+degpol(B), NULL,
    2305             :                nxV_chinese_center, FpX_center_i);
    2306        4578 :   setvarn(H, vX); (void)delete_var();
    2307        4578 :   return gerepilecopy(av, H);
    2308             : }
    2309             : 
    2310             : static long
    2311        2793 : ZX_ZXY_rnfequation_lambda(GEN A, GEN B0, long lambda)
    2312             : {
    2313        2793 :   pari_sp av = avma;
    2314        2793 :   long degA = degpol(A), degB, dres = degA*degpol(B0);
    2315        2793 :   long v = fetch_var_higher();
    2316        2793 :   long vX = varn(B0), vY = varn(A); /* assume vY has lower priority */
    2317        2793 :   long sX = evalvarn(vX);
    2318             :   GEN dB, B, a, b, Hp;
    2319             :   forprime_t S;
    2320             : 
    2321        2793 :   B0 = Q_remove_denom(B0, &dB);
    2322        2793 :   if (!dB) B0 = leafcopy(B0);
    2323        2793 :   A = leafcopy(A);
    2324        2793 :   B = B0;
    2325        2793 :   setvarn(A,v);
    2326             : INIT:
    2327        3381 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    2328        3381 :   B = swap_vars(B, vY); setvarn(B,v);
    2329             :   /* B0(lambda v + x, v) */
    2330        3381 :   if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    2331             : 
    2332        3381 :   degB = degpol(B);
    2333        3381 :   init_modular_big(&S);
    2334             :   while (1)
    2335           0 :   {
    2336        3381 :     ulong p = u_forprime_next(&S);
    2337        3381 :     ulong dp = dB ? umodiu(dB, p): 1;
    2338        3381 :     if (!dp) continue;
    2339        3381 :     a = ZX_to_Flx(A, p);
    2340        3381 :     b = ZXX_to_FlxX(B, p, v);
    2341        3381 :     Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2342        3381 :     if (degpol(Hp) != dres) continue;
    2343        3381 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    2344        3381 :     if (!Flx_is_squarefree(Hp, p)) { lambda = next_lambda(lambda); goto INIT; }
    2345        2793 :     if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    2346        5586 :     (void)delete_var(); return gc_long(av,lambda);
    2347             :   }
    2348             : }
    2349             : 
    2350             : GEN
    2351        3374 : ZX_ZXY_rnfequation(GEN A, GEN B, long *lambda)
    2352             : {
    2353        3374 :   if (lambda)
    2354             :   {
    2355        2793 :     *lambda = ZX_ZXY_rnfequation_lambda(A, B, *lambda);
    2356        2793 :     B = RgX_translate(B, monomial(stoi(*lambda), 1, varn(A)));
    2357             :   }
    2358        3374 :   return ZX_ZXY_resultant(A,B);
    2359             : }
    2360             : 
    2361             : /************************************************************************
    2362             :  *                                                                      *
    2363             :  *                   IRREDUCIBLE POLYNOMIAL / Fp                        *
    2364             :  *                                                                      *
    2365             :  ************************************************************************/
    2366             : 
    2367             : /* irreducible (unitary) polynomial of degree n over Fp */
    2368             : GEN
    2369           0 : ffinit_rand(GEN p,long n)
    2370             : {
    2371           0 :   for(;;) {
    2372           0 :     pari_sp av = avma;
    2373           0 :     GEN pol = ZX_add(pol_xn(n, 0), random_FpX(n-1,0, p));
    2374           0 :     if (FpX_is_irred(pol, p)) return pol;
    2375           0 :     set_avma(av);
    2376             :   }
    2377             : }
    2378             : 
    2379             : /* return an extension of degree 2^l of F_2, assume l > 0
    2380             :  * Not stack clean. */
    2381             : static GEN
    2382         389 : f2init(long l)
    2383             : {
    2384             :   GEN Q, T, S;
    2385             :   long i, v;
    2386             : 
    2387         389 :   if (l == 1) return polcyclo(3, 0);
    2388         354 :   v = fetch_var_higher();
    2389         354 :   S = mkpoln(4, gen_1,gen_1,gen_0,gen_0); /* y(y^2 + y) */
    2390         353 :   Q = mkpoln(3, gen_1,gen_1, S); /* x^2 + x + y(y^2+y) */
    2391         355 :   setvarn(Q, v);
    2392             : 
    2393             :   /* x^4+x+1, irred over F_2, minimal polynomial of a root of Q */
    2394         355 :   T = mkpoln(5, gen_1,gen_0,gen_0,gen_1,gen_1);
    2395         354 :   setvarn(T, v);
    2396             :   /* Q = x^2 + x + a(y) irred. over K = F2[y] / (T(y))
    2397             :    * ==> x^2 + x + a(y) b irred. over K for any root b of Q
    2398             :    * ==> x^2 + x + (b^2+b)b */
    2399         354 :   for (i=2; i<l; i++) T = FpX_FpXY_resultant(T, Q, gen_2); /* minpoly(b) / F2*/
    2400         357 :   (void)delete_var(); setvarn(T,0); return T;
    2401             : }
    2402             : 
    2403             : /* return an extension of degree p^l of F_p, assume l > 0
    2404             :  * Not stack clean. */
    2405             : GEN
    2406           0 : ffinit_Artin_Shreier(GEN ip, long l)
    2407             : {
    2408           0 :   long i, v, p = itos(ip);
    2409           0 :   GEN T, Q, xp = pol_xn(p,0); /* x^p */
    2410           0 :   T = ZX_sub(xp, deg1pol_shallow(gen_1,gen_1,0)); /* x^p - x - 1 */
    2411           0 :   if (l == 1) return T;
    2412             : 
    2413           0 :   v = fetch_var_higher();
    2414           0 :   setvarn(xp, v);
    2415           0 :   Q = ZX_sub(pol_xn(2*p-1,0), pol_xn(p,0));
    2416           0 :   Q = gsub(xp, deg1pol_shallow(gen_1, Q, v)); /* x^p - x - (y^(2p-1)-y^p) */
    2417           0 :   for (i = 2; i <= l; ++i) T = FpX_FpXY_resultant(T, Q, ip);
    2418           0 :   (void)delete_var(); setvarn(T,0); return T;
    2419             : }
    2420             : 
    2421             : /* check if polsubcyclo(n,l,0) is irreducible modulo p */
    2422             : static long
    2423       22829 : fpinit_check(GEN p, long n, long l)
    2424             : {
    2425             :   ulong q;
    2426       22829 :   if (!uisprime(n)) return 0;
    2427       14343 :   q = umodiu(p,n); if (!q) return 0;
    2428       12278 :   return ugcd((n-1)/Fl_order(q, n-1, n), l) == 1;
    2429             : }
    2430             : 
    2431             : /* let k=2 if p%4==1, and k=4 else and assume k*p does not divide l.
    2432             :  * Return an irreducible polynomial of degree l over F_p.
    2433             :  * Variant of Adleman and Lenstra "Finding irreducible polynomials over
    2434             :  * finite fields", ACM, 1986 (5) 350--355.
    2435             :  * Not stack clean */
    2436             : static GEN
    2437        5621 : fpinit(GEN p, long l)
    2438             : {
    2439        5621 :   ulong n = 1+l;
    2440        5621 :   while (!fpinit_check(p,n,l)) n += l;
    2441        5621 :   if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
    2442        5621 :   return FpX_red(polsubcyclo(n,l,0),p);
    2443             : }
    2444             : 
    2445             : static GEN
    2446        5406 : ffinit_fact(GEN p, long n)
    2447             : {
    2448        5406 :   GEN P, F = gel(factoru_pow(n),3);
    2449        5409 :   long i, l = lg(F);
    2450        5409 :   P= cgetg(l, t_VEC);
    2451        5408 :   if (!odd(n) && absequaliu(p, 2))
    2452         389 :     gel(P,1) = f2init(vals(n)); /* if n is even, F[1] = 2^vals(n)*/
    2453             :   else
    2454        5019 :     gel(P,1) = fpinit(p, F[1]);
    2455        6012 :   for (i = 2; i < l; ++i)
    2456         602 :     gel(P,i) = fpinit(p, F[i]);
    2457        5410 :   return FpXV_direct_compositum(P, p);
    2458             : }
    2459             : 
    2460             : static GEN
    2461        7807 : init_Fq_i(GEN p, long n, long v)
    2462             : {
    2463             :   GEN P;
    2464        7807 :   if (n <= 0) pari_err_DOMAIN("ffinit", "degree", "<=", gen_0, stoi(n));
    2465        7807 :   if (typ(p) != t_INT) pari_err_TYPE("ffinit",p);
    2466        7807 :   if (signe(p) <= 0) pari_err_PRIME("ffinit",p);
    2467        7807 :   if (v < 0) v = 0;
    2468        7807 :   if (n == 1) return pol_x(v);
    2469        7555 :   if (fpinit_check(p, n+1, n)) return polcyclo(n+1, v);
    2470        5406 :   P = ffinit_fact(p,n);
    2471        5411 :   setvarn(P, v); return P;
    2472             : }
    2473             : GEN
    2474        7401 : init_Fq(GEN p, long n, long v)
    2475             : {
    2476        7401 :   pari_sp av = avma;
    2477        7401 :   return gerepileupto(av, init_Fq_i(p, n, v));
    2478             : }
    2479             : GEN
    2480         406 : ffinit(GEN p, long n, long v)
    2481             : {
    2482         406 :   pari_sp av = avma;
    2483         406 :   return gerepileupto(av, FpX_to_mod(init_Fq_i(p, n, v), p));
    2484             : }
    2485             : 
    2486             : GEN
    2487        3178 : ffnbirred(GEN p, long n)
    2488             : {
    2489        3178 :   pari_sp av = avma;
    2490        3178 :   GEN s = powiu(p,n), F = factoru(n), D = divisorsu_moebius(gel(F, 1));
    2491        3178 :   long j, l = lg(D);
    2492        6797 :   for (j = 2; j < l; j++) /* skip d = 1 */
    2493             :   {
    2494        3619 :     long md = D[j]; /* mu(d) * d, d squarefree */
    2495        3619 :     GEN pd = powiu(p, n / labs(md)); /* p^{n/d} */
    2496        3619 :     s = md > 0? addii(s, pd): subii(s,pd);
    2497             :   }
    2498        3178 :   return gerepileuptoint(av, diviuexact(s, n));
    2499             : }
    2500             : 
    2501             : GEN
    2502         441 : ffsumnbirred(GEN p, long n)
    2503             : {
    2504         441 :   pari_sp av = avma, av2;
    2505         441 :   GEN q, t = p, v = vecfactoru(1, n);
    2506             :   long i;
    2507         441 :   q = cgetg(n+1,t_VEC); gel(q,1) = p;
    2508         441 :   for (i=2; i<=n; i++) gel(q,i) = mulii(gel(q,i-1), p);
    2509         441 :   av2 = avma;
    2510        1589 :   for (i=2; i<=n; i++)
    2511             :   {
    2512        1148 :     GEN s = gel(q,i), F = gel(v,i), D = divisorsu_moebius(gel(F,1));
    2513        1148 :     long j, l = lg(D);
    2514        2534 :     for (j = 2; j < l; j++) /* skip 1 */
    2515             :     {
    2516        1386 :       long md = D[j];
    2517        1386 :       GEN pd = gel(q, i / labs(md)); /* p^{i/d} */
    2518        1386 :       s = md > 0? addii(s, pd): subii(s, pd);
    2519             :     }
    2520        1148 :     t = gerepileuptoint(av2, addii(t, diviuexact(s, i)));
    2521             :   }
    2522         441 :   return gerepileuptoint(av, t);
    2523             : }
    2524             : 
    2525             : GEN
    2526         140 : ffnbirred0(GEN p, long n, long flag)
    2527             : {
    2528         140 :   if (typ(p) != t_INT) pari_err_TYPE("ffnbirred", p);
    2529         140 :   if (n <= 0) pari_err_DOMAIN("ffnbirred", "degree", "<=", gen_0, stoi(n));
    2530         140 :   switch(flag)
    2531             :   {
    2532          70 :     case 0: return ffnbirred(p, n);
    2533          70 :     case 1: return ffsumnbirred(p, n);
    2534             :   }
    2535           0 :   pari_err_FLAG("ffnbirred");
    2536             :   return NULL; /* LCOV_EXCL_LINE */
    2537             : }
    2538             : 
    2539             : static void
    2540        2128 : checkmap(GEN m, const char *s)
    2541             : {
    2542        2128 :   if (typ(m)!=t_VEC || lg(m)!=3 || typ(gel(m,1))!=t_FFELT)
    2543           0 :     pari_err_TYPE(s,m);
    2544        2128 : }
    2545             : 
    2546             : GEN
    2547         182 : ffembed(GEN a, GEN b)
    2548             : {
    2549         182 :   pari_sp av = avma;
    2550         182 :   GEN p, Ta, Tb, g, r = NULL;
    2551         182 :   if (typ(a)!=t_FFELT) pari_err_TYPE("ffembed",a);
    2552         182 :   if (typ(b)!=t_FFELT) pari_err_TYPE("ffembed",b);
    2553         182 :   p = FF_p_i(a); g = FF_gen(a);
    2554         182 :   if (!equalii(p, FF_p_i(b))) pari_err_MODULUS("ffembed",a,b);
    2555         182 :   Ta = FF_mod(a);
    2556         182 :   Tb = FF_mod(b);
    2557         182 :   if (degpol(Tb)%degpol(Ta)!=0)
    2558           7 :     pari_err_DOMAIN("ffembed",GENtostr_raw(a),"is not a subfield of",b,a);
    2559         175 :   r = gel(FFX_roots(Ta, b), 1);
    2560         175 :   return gerepilecopy(av, mkvec2(g,r));
    2561             : }
    2562             : 
    2563             : GEN
    2564          91 : ffextend(GEN a, GEN P, long v)
    2565             : {
    2566          91 :   pari_sp av = avma;
    2567             :   long n;
    2568             :   GEN p, T, R, g, m;
    2569          91 :   if (typ(a)!=t_FFELT) pari_err_TYPE("ffextend",a);
    2570          91 :   T = a; p = FF_p_i(a);
    2571          91 :   if (typ(P)!=t_POL || !RgX_is_FpXQX(P,&T,&p)) pari_err_TYPE("ffextend", P);
    2572          49 :   if (!FF_samefield(a, T)) pari_err_MODULUS("ffextend",a,T);
    2573          49 :   if (v < 0) v = varn(P);
    2574          49 :   n = FF_f(T) * degpol(P); R = ffinit(p, n, v); g = ffgen(R, v);
    2575          49 :   m = ffembed(a, g);
    2576          49 :   R = FFX_roots(ffmap(m, P),g);
    2577          49 :   return gerepilecopy(av, mkvec2(gel(R,1), m));
    2578             : }
    2579             : 
    2580             : GEN
    2581          42 : fffrobenius(GEN a, long n)
    2582             : {
    2583             :   GEN g;
    2584          42 :   if (typ(a)!=t_FFELT) pari_err_TYPE("fffrobenius",a);
    2585          42 :   retmkvec2(g=FF_gen(a), FF_Frobenius(g, n));
    2586             : }
    2587             : 
    2588             : GEN
    2589         133 : ffinvmap(GEN m)
    2590             : {
    2591         133 :   pari_sp av = avma;
    2592             :   long i, l;
    2593         133 :   GEN T, F, a, g, r, f = NULL;
    2594         133 :   checkmap(m, "ffinvmap");
    2595         133 :   a = gel(m,1); r = gel(m,2);
    2596         133 :   if (typ(r) != t_FFELT)
    2597           7 :    pari_err_TYPE("ffinvmap", m);
    2598         126 :   g = FF_gen(a);
    2599         126 :   T = FF_mod(r);
    2600         126 :   F = gel(FFX_factor(T, a), 1);
    2601         126 :   l = lg(F);
    2602         532 :   for(i=1; i<l; i++)
    2603             :   {
    2604         532 :     GEN s = FFX_rem(FF_to_FpXQ_i(r), gel(F, i), a);
    2605         532 :     if (degpol(s)==0 && gequal(constant_term(s),g)) { f = gel(F, i); break; }
    2606             :   }
    2607         126 :   if (f==NULL) pari_err_TYPE("ffinvmap", m);
    2608         126 :   if (degpol(f)==1) f = FF_neg_i(gel(f,2));
    2609         126 :   return gerepilecopy(av, mkvec2(FF_gen(r),f));
    2610             : }
    2611             : 
    2612             : static GEN
    2613        1176 : ffpartmapimage(const char *s, GEN r)
    2614             : {
    2615        1176 :    GEN a = NULL, p = NULL;
    2616        1176 :    if (typ(r)==t_POL && degpol(r) >= 1
    2617        1176 :       && RgX_is_FpXQX(r,&a,&p) && a && typ(a)==t_FFELT) return a;
    2618           0 :    pari_err_TYPE(s, r);
    2619             :    return NULL; /* LCOV_EXCL_LINE */
    2620             : }
    2621             : 
    2622             : static GEN
    2623        2702 : ffeltmap_i(GEN m, GEN x)
    2624             : {
    2625        2702 :    GEN r = gel(m,2);
    2626        2702 :    if (!FF_samefield(x, gel(m,1)))
    2627          84 :      pari_err_DOMAIN("ffmap","m","domain does not contain", x, r);
    2628        2618 :    if (typ(r)==t_FFELT)
    2629        1652 :      return FF_map(r, x);
    2630             :    else
    2631         966 :      return FFX_preimage(x, r, ffpartmapimage("ffmap", r));
    2632             : }
    2633             : 
    2634             : static GEN
    2635        4452 : ffmap_i(GEN m, GEN x)
    2636             : {
    2637             :   GEN y;
    2638        4452 :   long i, lx, tx = typ(x);
    2639        4452 :   switch(tx)
    2640             :   {
    2641             :     case t_FFELT:
    2642        2534 :       return ffeltmap_i(m, x);
    2643             :     case t_POL: case t_RFRAC: case t_SER:
    2644             :     case t_VEC: case t_COL: case t_MAT:
    2645        1267 :       y = cgetg_copy(x, &lx);
    2646        1267 :       for (i=1; i<lontyp[tx]; i++) y[i] = x[1];
    2647        4564 :       for (i=lontyp[tx]; i<lx; i++)
    2648             :       {
    2649        3339 :         GEN yi = ffmap_i(m, gel(x,i));
    2650        3297 :         if (!yi) return NULL;
    2651        3297 :         gel(y,i) = yi;
    2652             :       }
    2653        1225 :       return y;
    2654             :   }
    2655         651 :   return gcopy(x);
    2656             : }
    2657             : 
    2658             : GEN
    2659        1029 : ffmap(GEN m, GEN x)
    2660             : {
    2661        1029 :   pari_sp ltop = avma;
    2662             :   GEN y;
    2663        1029 :   checkmap(m, "ffmap");
    2664        1029 :   y = ffmap_i(m, x);
    2665        1029 :   if (y) return y;
    2666          42 :   set_avma(ltop); return cgetg(1,t_VEC);
    2667             : }
    2668             : 
    2669             : static GEN
    2670         126 : ffeltmaprel_i(GEN m, GEN x)
    2671             : {
    2672         126 :    GEN g = gel(m,1), r = gel(m,2);
    2673         126 :    if (!FF_samefield(x, g))
    2674           0 :      pari_err_DOMAIN("ffmap","m","domain does not contain", x, r);
    2675         126 :    if (typ(r)==t_FFELT)
    2676          42 :      retmkpolmod(FF_map(r, x), pol_x(FF_var(g)));
    2677             :    else
    2678          84 :      retmkpolmod(FFX_preimagerel(x, r, ffpartmapimage("ffmap", r)), gcopy(r));
    2679             : }
    2680             : 
    2681             : static GEN
    2682         126 : ffmaprel_i(GEN m, GEN x)
    2683             : {
    2684             :   GEN y;
    2685         126 :   long i, lx, tx = typ(x);
    2686         126 :   switch(tx)
    2687             :   {
    2688             :     case t_FFELT:
    2689         126 :       return ffeltmaprel_i(m, x);
    2690             :     case t_POL: case t_RFRAC: case t_SER:
    2691             :     case t_VEC: case t_COL: case t_MAT:
    2692           0 :       y = cgetg_copy(x, &lx);
    2693           0 :       for (i=1; i<lontyp[tx]; i++) y[i] = x[1];
    2694           0 :       for (i=lontyp[tx]; i<lx; i++)
    2695           0 :         gel(y,i) = ffmaprel_i(m, gel(x,i));
    2696           0 :       return y;
    2697             :   }
    2698           0 :   return gcopy(x);
    2699             : }
    2700             : 
    2701             : GEN
    2702         126 : ffmaprel(GEN m, GEN x)
    2703             : {
    2704         126 :   checkmap(m, "ffmaprel");
    2705         126 :   return ffmaprel_i(m, x);
    2706             : }
    2707             : 
    2708             : static void
    2709          84 : err_compo(GEN m, GEN n)
    2710          84 : { pari_err_DOMAIN("ffcompomap","m","domain does not contain codomain of",n,m); }
    2711             : 
    2712             : GEN
    2713         420 : ffcompomap(GEN m, GEN n)
    2714             : {
    2715         420 :   pari_sp av = avma;
    2716         420 :   GEN g = gel(n,1), r, m2, n2;
    2717         420 :   checkmap(m, "ffcompomap");
    2718         420 :   checkmap(n, "ffcompomap");
    2719         420 :   m2 = gel(m,2); n2 = gel(n,2);
    2720         420 :   switch((typ(m2)==t_POL)|((typ(n2)==t_POL)<<1))
    2721             :   {
    2722             :     case 0:
    2723          84 :       if (!FF_samefield(gel(m,1),n2)) err_compo(m,n);
    2724          42 :       r = FF_map(gel(m,2), n2);
    2725          42 :       break;
    2726             :     case 2:
    2727          84 :       r = ffmap_i(m, n2);
    2728          42 :       if (lg(r) == 1) err_compo(m,n);
    2729          42 :       break;
    2730             :     case 1:
    2731         168 :       r = ffeltmap_i(m, n2);
    2732         126 :       if (!r)
    2733             :       {
    2734             :         GEN a, A, R, M;
    2735             :         long dm, dn;
    2736          42 :         a = ffpartmapimage("ffcompomap",m2);
    2737          42 :         A = FF_to_FpXQ_i(FF_neg(n2));
    2738          42 :         setvarn(A, 1);
    2739          42 :         R = deg1pol(gen_1, A, 0);
    2740          42 :         setvarn(R, 0);
    2741          42 :         M = gcopy(m2);
    2742          42 :         setvarn(M, 1);
    2743          42 :         r = polresultant0(R, M, 1, 0);
    2744          42 :         dm = FF_f(gel(m,1)); dn = FF_f(gel(n,1));
    2745          42 :         if (dm % dn || !FFX_ispower(r, dm/dn, a, &r)) err_compo(m,n);
    2746          42 :         setvarn(r, varn(FF_mod(g)));
    2747             :       }
    2748         126 :       break;
    2749             :     case 3:
    2750             :     {
    2751             :       GEN M, R, T, p, a;
    2752          84 :       a = ffpartmapimage("ffcompomap",n2);
    2753          84 :       if (!FF_samefield(a, gel(m,1))) err_compo(m,n);
    2754          42 :       p = FF_p_i(gel(n,1));
    2755          42 :       T = FF_mod(gel(n,1));
    2756          42 :       setvarn(T, 1);
    2757          42 :       R = RgX_to_FpXQX(n2,T,p);
    2758          42 :       setvarn(R, 0);
    2759          42 :       M = gcopy(m2);
    2760          42 :       setvarn(M, 1);
    2761          42 :       r = polresultant0(R, M, 1, 0);
    2762          42 :       setvarn(r, varn(n2));
    2763             :     }
    2764             :   }
    2765         252 :   return gerepilecopy(av, mkvec2(g,r));
    2766             : }

Generated by: LCOV version 1.13