Code coverage tests

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

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

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

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

Generated by: LCOV version 1.11