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

Generated by: LCOV version 1.11