Code coverage tests

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

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

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

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

Generated by: LCOV version 1.13