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 22303-eb3e11d) Lines: 1304 1493 87.3 %
Date: 2018-04-21 06:16:28 Functions: 141 157 89.8 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11