Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - polarit3.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 24988-2584e74448) Lines: 1590 1781 89.3 %
Date: 2020-01-26 05:57:03 Functions: 170 185 91.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2005  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /***********************************************************************/
      15             : /**                                                                   **/
      16             : /**               ARITHMETIC OPERATIONS ON POLYNOMIALS                **/
      17             : /**                         (third part)                              **/
      18             : /**                                                                   **/
      19             : /***********************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : /************************************************************************
      24             :  **                                                                    **
      25             :  **                      Ring membership                               **
      26             :  **                                                                    **
      27             :  ************************************************************************/
      28             : struct charact {
      29             :   GEN q;
      30             :   int isprime;
      31             : };
      32             : static void
      33        1239 : char_update_prime(struct charact *S, GEN p)
      34             : {
      35        1239 :   if (!S->isprime) { S->isprime = 1; S->q = p; }
      36        1239 :   if (!equalii(p, S->q)) pari_err_MODULUS("characteristic", S->q, p);
      37        1232 : }
      38             : static void
      39        5194 : char_update_int(struct charact *S, GEN n)
      40             : {
      41        5194 :   if (S->isprime)
      42             :   {
      43           7 :     if (dvdii(n, S->q)) return;
      44           7 :     pari_err_MODULUS("characteristic", S->q, n);
      45             :   }
      46        5187 :   S->q = gcdii(S->q, n);
      47             : }
      48             : static void
      49      608230 : charact(struct charact *S, GEN x)
      50             : {
      51      608230 :   const long tx = typ(x);
      52             :   long i, l;
      53      608230 :   switch(tx)
      54             :   {
      55        4221 :     case t_INTMOD:char_update_int(S, gel(x,1)); break;
      56        1148 :     case t_FFELT: char_update_prime(S, gel(x,4)); break;
      57             :     case t_COMPLEX: case t_QUAD:
      58             :     case t_POLMOD: case t_POL: case t_SER: case t_RFRAC:
      59             :     case t_VEC: case t_COL: case t_MAT:
      60       20097 :       l = lg(x);
      61       20097 :       for (i=lontyp[tx]; i < l; i++) charact(S,gel(x,i));
      62       20083 :       break;
      63             :     case t_LIST:
      64           7 :       x = list_data(x);
      65           7 :       if (x) charact(S, x);
      66           7 :       break;
      67             :   }
      68      608202 : }
      69             : static void
      70       33252 : charact_res(struct charact *S, GEN x)
      71             : {
      72       33252 :   const long tx = typ(x);
      73             :   long i, l;
      74       33252 :   switch(tx)
      75             :   {
      76         973 :     case t_INTMOD:char_update_int(S, gel(x,1)); break;
      77           0 :     case t_FFELT: char_update_prime(S, gel(x,4)); break;
      78          91 :     case t_PADIC: char_update_prime(S, gel(x,2)); break;
      79             :     case t_COMPLEX: case t_QUAD:
      80             :     case t_POLMOD: case t_POL: case t_SER: case t_RFRAC:
      81             :     case t_VEC: case t_COL: case t_MAT:
      82       10352 :       l = lg(x);
      83       10352 :       for (i=lontyp[tx]; i < l; i++) charact_res(S,gel(x,i));
      84       10352 :       break;
      85             :     case t_LIST:
      86           0 :       x = list_data(x);
      87           0 :       if (x) charact_res(S, x);
      88           0 :       break;
      89             :   }
      90       33252 : }
      91             : GEN
      92       11039 : characteristic(GEN x)
      93             : {
      94             :   struct charact S;
      95       11039 :   S.q = gen_0; S.isprime = 0;
      96       11039 :   charact(&S, x); return S.q;
      97             : }
      98             : GEN
      99        2497 : residual_characteristic(GEN x)
     100             : {
     101             :   struct charact S;
     102        2497 :   S.q = gen_0; S.isprime = 0;
     103        2497 :   charact_res(&S, x); return S.q;
     104             : }
     105             : 
     106             : int
     107    60609850 : Rg_is_Fp(GEN x, GEN *pp)
     108             : {
     109             :   GEN mod;
     110    60609850 :   switch(typ(x))
     111             :   {
     112             :   case t_INTMOD:
     113     4256868 :     mod = gel(x,1);
     114     4256868 :     if (!*pp) *pp = mod;
     115     4021955 :     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     4256868 :     return 1;
     121             :   case t_INT:
     122    52326495 :     return 1;
     123     4026487 :   default: return 0;
     124             :   }
     125             : }
     126             : 
     127             : int
     128    20335488 : RgX_is_FpX(GEN x, GEN *pp)
     129             : {
     130    20335488 :   long i, lx = lg(x);
     131    76893063 :   for (i=2; i<lx; i++)
     132    60584062 :     if (!Rg_is_Fp(gel(x, i), pp))
     133     4026487 :       return 0;
     134    16309001 :   return 1;
     135             : }
     136             : 
     137             : int
     138           0 : RgV_is_FpV(GEN x, GEN *pp)
     139             : {
     140           0 :   long i, lx = lg(x);
     141           0 :   for (i=1; i<lx; i++)
     142           0 :     if (!Rg_is_Fp(gel(x,i), pp)) return 0;
     143           0 :   return 1;
     144             : }
     145             : 
     146             : int
     147           0 : RgM_is_FpM(GEN x, GEN *pp)
     148             : {
     149           0 :   long i, lx = lg(x);
     150           0 :   for (i=1; i<lx; i++)
     151           0 :     if (!RgV_is_FpV(gel(x, i), pp)) return 0;
     152           0 :   return 1;
     153             : }
     154             : 
     155             : int
     156       58338 : Rg_is_FpXQ(GEN x, GEN *pT, GEN *pp)
     157             : {
     158             :   GEN pol, mod, p;
     159       58338 :   switch(typ(x))
     160             :   {
     161             :   case t_INTMOD:
     162       25781 :     return Rg_is_Fp(x, pp);
     163             :   case t_INT:
     164        6538 :     return 1;
     165             :   case t_POL:
     166          42 :     return RgX_is_FpX(x, pp);
     167             :   case t_FFELT:
     168       21350 :     mod = x; p = FF_p_i(x);
     169       21350 :     if (!*pp) *pp = p;
     170       21350 :     if (!*pT) *pT = mod;
     171       19824 :     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       21308 :     return 1;
     177             :   case t_POLMOD:
     178        4543 :     mod = gel(x,1); pol = gel(x, 2);
     179        4543 :     if (!RgX_is_FpX(mod, pp)) return 0;
     180        4543 :     if (typ(pol)==t_POL)
     181             :     {
     182        4536 :       if (!RgX_is_FpX(pol, pp)) return 0;
     183             :     }
     184           7 :     else if (!Rg_is_Fp(pol, pp)) return 0;
     185        4543 :     if (!*pT) *pT = mod;
     186        4431 :     else if (mod != *pT && !gequal(mod, *pT))
     187             :     {
     188           0 :       if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_FpXQ");
     189           0 :       return 0;
     190             :     }
     191        4543 :     return 1;
     192             : 
     193          84 :   default: return 0;
     194             :   }
     195             : }
     196             : 
     197             : int
     198        3045 : RgX_is_FpXQX(GEN x, GEN *pT, GEN *pp)
     199             : {
     200        3045 :   long i, lx = lg(x);
     201       60837 :   for (i = 2; i < lx; i++)
     202       57834 :     if (!Rg_is_FpXQ(gel(x,i), pT, pp)) return 0;
     203        3003 :   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    32585163 : Rg_to_Fp(GEN x, GEN p)
     216             : {
     217    32585163 :   if (lgefint(p) == 3) return utoi(Rg_to_Fl(x, uel(p,2)));
     218     3089508 :   switch(typ(x))
     219             :   {
     220      208945 :     case t_INT: return modii(x, p);
     221             :     case t_FRAC: {
     222         121 :       pari_sp av = avma;
     223         121 :       GEN z = modii(gel(x,1), p);
     224         121 :       if (z == gen_0) return gen_0;
     225         121 :       return gerepileuptoint(av, remii(mulii(z, Fp_inv(gel(x,2), p)), p));
     226             :     }
     227           0 :     case t_PADIC: return padic_to_Fp(x, p);
     228             :     case t_INTMOD: {
     229     2880442 :       GEN q = gel(x,1), a = gel(x,2);
     230     2880442 :       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     1292383 : Rg_to_FpXQ(GEN x, GEN T, GEN p)
     241             : {
     242     1292383 :   long ta, tx = typ(x), v = get_FpX_var(T);
     243             :   GEN a, b;
     244     1292383 :   if (is_const_t(tx))
     245             :   {
     246       57913 :     if (tx == t_FFELT)
     247             :     {
     248       17085 :       GEN z = FF_to_FpXQ(x);
     249       17085 :       setvarn(z, v);
     250       17085 :       return z;
     251             :     }
     252       40828 :     return scalar_ZX(degpol(T)? Rg_to_Fp(x, p): gen_0, v);
     253             :   }
     254     1234470 :   switch(tx)
     255             :   {
     256             :     case t_POLMOD:
     257     1229619 :       b = gel(x,1);
     258     1229619 :       a = gel(x,2); ta = typ(a);
     259     1229619 :       if (is_const_t(ta))
     260        4025 :         return scalar_ZX(degpol(T)? Rg_to_Fp(a, p): gen_0, v);
     261     1225594 :       b = RgX_to_FpX(b, p); if (varn(b) != v) break;
     262     1225594 :       a = RgX_to_FpX(a, p);
     263     1225594 :       if (ZX_equal(b,get_FpX_mod(T)) || signe(FpX_rem(b,T,p))==0)
     264     1225594 :         return FpX_rem(a, T, p);
     265           0 :       break;
     266             :     case t_POL:
     267        4851 :       if (varn(x) != v) break;
     268        4851 :       return FpX_rem(RgX_to_FpX(x,p), T, p);
     269             :     case t_RFRAC:
     270           0 :       a = Rg_to_FpXQ(gel(x,1), T,p);
     271           0 :       b = Rg_to_FpXQ(gel(x,2), T,p);
     272           0 :       return FpXQ_div(a,b, T,p);
     273             :   }
     274           0 :   pari_err_TYPE("Rg_to_FpXQ",x);
     275             :   return NULL; /* LCOV_EXCL_LINE */
     276             : }
     277             : GEN
     278     3432167 : RgX_to_FpX(GEN x, GEN p)
     279             : {
     280             :   long i, l;
     281     3432167 :   GEN z = cgetg_copy(x, &l); z[1] = x[1];
     282     3432167 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     283     3432167 :   return FpX_renormalize(z, l);
     284             : }
     285             : 
     286             : GEN
     287         126 : RgV_to_FpV(GEN x, GEN p)
     288         126 : { pari_APPLY_type(t_VEC, Rg_to_Fp(gel(x,i), p)) }
     289             : 
     290             : GEN
     291      915890 : RgC_to_FpC(GEN x, GEN p)
     292      915890 : { pari_APPLY_type(t_COL, Rg_to_Fp(gel(x,i), p)) }
     293             : 
     294             : GEN
     295      128333 : RgM_to_FpM(GEN x, GEN p)
     296      128333 : { pari_APPLY_same(RgC_to_FpC(gel(x,i), p)) }
     297             : 
     298             : GEN
     299      282099 : RgV_to_Flv(GEN x, ulong p)
     300      282099 : { pari_APPLY_ulong(Rg_to_Fl(gel(x,i), p)) }
     301             : 
     302             : GEN
     303      114250 : RgM_to_Flm(GEN x, ulong p)
     304      114250 : { pari_APPLY_same(RgV_to_Flv(gel(x,i), p)) }
     305             : 
     306             : GEN
     307        5000 : RgX_to_FpXQX(GEN x, GEN T, GEN p)
     308             : {
     309        5000 :   long i, l = lg(x);
     310        5000 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     311        5000 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T,p);
     312        5000 :   return FpXQX_renormalize(z, l);
     313             : }
     314             : GEN
     315        3108 : RgX_to_FqX(GEN x, GEN T, GEN p)
     316             : {
     317        3108 :   long i, l = lg(x);
     318        3108 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     319        3108 :   if (T)
     320         623 :     for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T, p);
     321             :   else
     322        2485 :     for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     323        3108 :   return FpXQX_renormalize(z, l);
     324             : }
     325             : 
     326             : GEN
     327      219184 : RgC_to_FqC(GEN x, GEN T, GEN p)
     328             : {
     329      219184 :   long i, l = lg(x);
     330      219184 :   GEN z = cgetg(l, t_COL);
     331      219184 :   if (T)
     332      219184 :     for (i = 1; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T, p);
     333             :   else
     334           0 :     for (i = 1; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     335      219184 :   return z;
     336             : }
     337             : 
     338             : GEN
     339       52332 : RgM_to_FqM(GEN x, GEN T, GEN p)
     340       52332 : { pari_APPLY_same(RgC_to_FqC(gel(x, i), T, p)) }
     341             : 
     342             : /* lg(V) > 1 */
     343             : GEN
     344      849765 : FpXV_FpC_mul(GEN V, GEN W, GEN p)
     345             : {
     346      849765 :   pari_sp av = avma;
     347      849765 :   long i, l = lg(V);
     348      849765 :   GEN z = ZX_Z_mul(gel(V,1),gel(W,1));
     349     4181499 :   for(i=2; i<l; i++)
     350             :   {
     351     3331734 :     z = ZX_add(z, ZX_Z_mul(gel(V,i),gel(W,i)));
     352     3331734 :     if ((i & 7) == 0) z = gerepileupto(av, z);
     353             :   }
     354      849765 :   return gerepileupto(av, FpX_red(z,p));
     355             : }
     356             : 
     357             : GEN
     358        1596 : FqX_Fq_add(GEN y, GEN x, GEN T, GEN p)
     359             : {
     360        1596 :   long i, lz = lg(y);
     361             :   GEN z;
     362        1596 :   if (!T) return FpX_Fp_add(y, x, p);
     363        1596 :   if (lz == 2) return scalarpol(x, varn(y));
     364        1596 :   z = cgetg(lz,t_POL); z[1] = y[1];
     365        1596 :   gel(z,2) = Fq_add(gel(y,2),x, T, p);
     366        1596 :   if (lz == 3) z = FpXX_renormalize(z,lz);
     367             :   else
     368         287 :     for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
     369        1596 :   return z;
     370             : }
     371             : 
     372             : GEN
     373        1048 : FqX_Fq_sub(GEN y, GEN x, GEN T, GEN p)
     374             : {
     375        1048 :   long i, lz = lg(y);
     376             :   GEN z;
     377        1048 :   if (!T) return FpX_Fp_sub(y, x, p);
     378        1048 :   if (lz == 2) return scalarpol(x, varn(y));
     379        1048 :   z = cgetg(lz,t_POL); z[1] = y[1];
     380        1048 :   gel(z,2) = Fq_sub(gel(y,2), x, T, p);
     381        1048 :   if (lz == 3) z = FpXX_renormalize(z,lz);
     382             :   else
     383         926 :     for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
     384        1048 :   return z;
     385             : }
     386             : 
     387             : GEN
     388      148804 : FqX_Fq_mul_to_monic(GEN P, GEN U, GEN T, GEN p)
     389             : {
     390             :   long i, lP;
     391      148804 :   GEN res = cgetg_copy(P, &lP); res[1] = P[1];
     392      148804 :   for(i=2; i<lP-1; i++) gel(res,i) = Fq_mul(U,gel(P,i), T,p);
     393      148804 :   gel(res,lP-1) = gen_1; return res;
     394             : }
     395             : 
     396             : GEN
     397        3988 : FpXQX_normalize(GEN z, GEN T, GEN p)
     398             : {
     399             :   GEN lc;
     400        3988 :   if (lg(z) == 2) return z;
     401        3974 :   lc = leading_coeff(z);
     402        3974 :   if (typ(lc) == t_POL)
     403             :   {
     404        1940 :     if (lg(lc) > 3) /* non-constant */
     405        1670 :       return FqX_Fq_mul_to_monic(z, Fq_inv(lc,T,p), T,p);
     406             :     /* constant */
     407         270 :     lc = gel(lc,2);
     408         270 :     z = shallowcopy(z);
     409         270 :     gel(z, lg(z)-1) = lc;
     410             :   }
     411             :   /* lc a t_INT */
     412        2304 :   if (equali1(lc)) return z;
     413          71 :   return FqX_Fq_mul_to_monic(z, Fp_inv(lc,p), T,p);
     414             : }
     415             : 
     416             : GEN
     417      127491 : FqX_eval(GEN x, GEN y, GEN T, GEN p)
     418             : {
     419             :   pari_sp av;
     420             :   GEN p1, r;
     421      127491 :   long j, i=lg(x)-1;
     422      127491 :   if (i<=2)
     423       26271 :     return (i==2)? Fq_red(gel(x,2), T, p): gen_0;
     424      101220 :   av=avma; p1=gel(x,i);
     425             :   /* specific attention to sparse polynomials (see poleval)*/
     426             :   /*You've guessed it! It's a copy-paste(tm)*/
     427      298480 :   for (i--; i>=2; i=j-1)
     428             :   {
     429      197764 :     for (j=i; !signe(gel(x,j)); j--)
     430         504 :       if (j==2)
     431             :       {
     432         315 :         if (i!=j) y = Fq_pow(y,utoipos(i-j+1), T, p);
     433         315 :         return gerepileupto(av, Fq_mul(p1,y, T, p));
     434             :       }
     435      197260 :     r = (i==j)? y: Fq_pow(y, utoipos(i-j+1), T, p);
     436      197260 :     p1 = Fq_add(Fq_mul(p1,r,T,p), gel(x,j), T, p);
     437             :   }
     438      100905 :   return gerepileupto(av, p1);
     439             : }
     440             : 
     441             : GEN
     442       31521 : FqXY_evalx(GEN Q, GEN x, GEN T, GEN p)
     443             : {
     444       31521 :   long i, lb = lg(Q);
     445             :   GEN z;
     446       31521 :   if (!T) return FpXY_evalx(Q, x, p);
     447       20993 :   z = cgetg(lb, t_POL); z[1] = Q[1];
     448      117068 :   for (i=2; i<lb; i++)
     449             :   {
     450       96075 :     GEN q = gel(Q,i);
     451       96075 :     gel(z,i) = typ(q) == t_INT? modii(q,p): FqX_eval(q, x, T, p);
     452             :   }
     453       20993 :   return FpXQX_renormalize(z, lb);
     454             : }
     455             : 
     456             : /* Q an FpXY, evaluate at (X,Y) = (x,y) */
     457             : GEN
     458       14350 : FqXY_eval(GEN Q, GEN y, GEN x, GEN T, GEN p)
     459             : {
     460       14350 :   pari_sp av = avma;
     461       14350 :   if (!T) return FpXY_eval(Q, y, x, p);
     462         588 :   return gerepileupto(av, FqX_eval(FqXY_evalx(Q, x, T, p), y, T, p));
     463             : }
     464             : 
     465             : /* a X^d */
     466             : GEN
     467     7624126 : monomial(GEN a, long d, long v)
     468             : {
     469             :   long i, n;
     470             :   GEN P;
     471     7624126 :   if (d < 0) {
     472           0 :     if (isrationalzero(a)) return pol_0(v);
     473           0 :     retmkrfrac(a, pol_xn(-d, v));
     474             :   }
     475     7624126 :   if (gequal0(a))
     476             :   {
     477        9212 :     if (isexactzero(a)) return scalarpol_shallow(a,v);
     478           0 :     n = d+2; P = cgetg(n+1, t_POL);
     479           0 :     P[1] = evalsigne(0) | evalvarn(v);
     480             :   }
     481             :   else
     482             :   {
     483     7614914 :     n = d+2; P = cgetg(n+1, t_POL);
     484     7614914 :     P[1] = evalsigne(1) | evalvarn(v);
     485             :   }
     486     7614914 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     487     7614914 :   gel(P,i) = a; return P;
     488             : }
     489             : GEN
     490     1860790 : monomialcopy(GEN a, long d, long v)
     491             : {
     492             :   long i, n;
     493             :   GEN P;
     494     1860790 :   if (d < 0) {
     495          14 :     if (isrationalzero(a)) return pol_0(v);
     496          14 :     retmkrfrac(gcopy(a), pol_xn(-d, v));
     497             :   }
     498     1860776 :   if (gequal0(a))
     499             :   {
     500           7 :     if (isexactzero(a)) return scalarpol(a,v);
     501           0 :     n = d+2; P = cgetg(n+1, t_POL);
     502           0 :     P[1] = evalsigne(0) | evalvarn(v);
     503             :   }
     504             :   else
     505             :   {
     506     1860769 :     n = d+2; P = cgetg(n+1, t_POL);
     507     1860769 :     P[1] = evalsigne(1) | evalvarn(v);
     508             :   }
     509     1860769 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     510     1860769 :   gel(P,i) = gcopy(a); return P;
     511             : }
     512             : GEN
     513       23821 : pol_x_powers(long N, long v)
     514             : {
     515       23821 :   GEN L = cgetg(N+1,t_VEC);
     516             :   long i;
     517       23821 :   for (i=1; i<=N; i++) gel(L,i) = pol_xn(i-1, v);
     518       23821 :   return L;
     519             : }
     520             : 
     521             : GEN
     522           0 : FqXQ_powers(GEN x, long l, GEN S, GEN T, GEN p)
     523             : {
     524           0 :   return T ? FpXQXQ_powers(x, l, S, T, p): FpXQ_powers(x, l, S, p);
     525             : }
     526             : 
     527             : GEN
     528           0 : FqXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, GEN p)
     529             : {
     530           0 :   return T ? FpXQXQ_matrix_pow(y, n, m, S, T, p): FpXQ_matrix_pow(y, n, m, S, p);
     531             : }
     532             : 
     533             : /*******************************************************************/
     534             : /*                                                                 */
     535             : /*                             Fq                                  */
     536             : /*                                                                 */
     537             : /*******************************************************************/
     538             : 
     539             : GEN
     540     6983510 : Fq_add(GEN x, GEN y, GEN T/*unused*/, GEN p)
     541             : {
     542             :   (void)T;
     543     6983510 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     544             :   {
     545     2482640 :     case 0: return Fp_add(x,y,p);
     546      204029 :     case 1: return FpX_Fp_add(x,y,p);
     547      327314 :     case 2: return FpX_Fp_add(y,x,p);
     548     3969527 :     case 3: return FpX_add(x,y,p);
     549             :   }
     550             :   return NULL;/*LCOV_EXCL_LINE*/
     551             : }
     552             : 
     553             : GEN
     554     4771684 : Fq_sub(GEN x, GEN y, GEN T/*unused*/, GEN p)
     555             : {
     556             :   (void)T;
     557     4771684 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     558             :   {
     559      167548 :     case 0: return Fp_sub(x,y,p);
     560        2357 :     case 1: return FpX_Fp_sub(x,y,p);
     561       10222 :     case 2: return Fp_FpX_sub(x,y,p);
     562     4591557 :     case 3: return FpX_sub(x,y,p);
     563             :   }
     564             :   return NULL;/*LCOV_EXCL_LINE*/
     565             : }
     566             : 
     567             : GEN
     568      535526 : Fq_neg(GEN x, GEN T/*unused*/, GEN p)
     569             : {
     570             :   (void)T;
     571      535526 :   return (typ(x)==t_POL)? FpX_neg(x,p): Fp_neg(x,p);
     572             : }
     573             : 
     574             : GEN
     575       12801 : Fq_halve(GEN x, GEN T/*unused*/, GEN p)
     576             : {
     577             :   (void)T;
     578       12801 :   return (typ(x)==t_POL)? FpX_halve(x,p): Fp_halve(x,p);
     579             : }
     580             : 
     581             : /* If T==NULL do not reduce*/
     582             : GEN
     583     7772102 : Fq_mul(GEN x, GEN y, GEN T, GEN p)
     584             : {
     585     7772102 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     586             :   {
     587     2535890 :     case 0: return Fp_mul(x,y,p);
     588       72278 :     case 1: return FpX_Fp_mul(x,y,p);
     589      130012 :     case 2: return FpX_Fp_mul(y,x,p);
     590     5033922 :     case 3: if (T) return FpXQ_mul(x,y,T,p);
     591     2839362 :             else return FpX_mul(x,y,p);
     592             :   }
     593             :   return NULL;/*LCOV_EXCL_LINE*/
     594             : }
     595             : 
     596             : /* If T==NULL do not reduce*/
     597             : GEN
     598      772844 : Fq_mulu(GEN x, ulong y, /*unused*/GEN T, GEN p)
     599             : {
     600             :   (void) T;
     601      772844 :   return typ(x)==t_POL ? FpX_Fp_mul(x,utoi(y),p): Fp_mulu(x, y, p);
     602             : }
     603             : 
     604             : /* y t_INT */
     605             : GEN
     606       56769 : Fq_Fp_mul(GEN x, GEN y, GEN T/*unused*/, GEN p)
     607             : {
     608             :   (void)T;
     609       56769 :   return (typ(x) == t_POL)? FpX_Fp_mul(x,y,p)
     610       56769 :                           : Fp_mul(x,y,p);
     611             : }
     612             : /* If T==NULL do not reduce*/
     613             : GEN
     614      273839 : Fq_sqr(GEN x, GEN T, GEN p)
     615             : {
     616      273839 :   if (typ(x) == t_POL)
     617             :   {
     618       13062 :     if (T) return FpXQ_sqr(x,T,p);
     619           0 :     else return FpX_sqr(x,p);
     620             :   }
     621             :   else
     622      260777 :     return Fp_sqr(x,p);
     623             : }
     624             : 
     625             : GEN
     626           0 : Fq_neg_inv(GEN x, GEN T, GEN p)
     627             : {
     628           0 :   if (typ(x) == t_INT) return Fp_inv(Fp_neg(x,p),p);
     629           0 :   return FpXQ_inv(FpX_neg(x,p),T,p);
     630             : }
     631             : 
     632             : GEN
     633           0 : Fq_invsafe(GEN x, GEN pol, GEN p)
     634             : {
     635           0 :   if (typ(x) == t_INT) return Fp_invsafe(x,p);
     636           0 :   return FpXQ_invsafe(x,pol,p);
     637             : }
     638             : 
     639             : GEN
     640       34966 : Fq_inv(GEN x, GEN pol, GEN p)
     641             : {
     642       34966 :   if (typ(x) == t_INT) return Fp_inv(x,p);
     643       29551 :   return FpXQ_inv(x,pol,p);
     644             : }
     645             : 
     646             : GEN
     647      514605 : Fq_div(GEN x, GEN y, GEN pol, GEN p)
     648             : {
     649      514605 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     650             :   {
     651      485107 :     case 0: return Fp_div(x,y,p);
     652       23975 :     case 1: return FpX_Fp_mul(x,Fp_inv(y,p),p);
     653         280 :     case 2: return FpX_Fp_mul(FpXQ_inv(y,pol,p),x,p);
     654        5243 :     case 3: return FpXQ_div(x,y,pol,p);
     655             :   }
     656             :   return NULL;/*LCOV_EXCL_LINE*/
     657             : }
     658             : 
     659             : GEN
     660       33369 : Fq_pow(GEN x, GEN n, GEN pol, GEN p)
     661             : {
     662       33369 :   if (typ(x) == t_INT) return Fp_pow(x,n,p);
     663       10465 :   return FpXQ_pow(x,n,pol,p);
     664             : }
     665             : 
     666             : GEN
     667       14651 : Fq_powu(GEN x, ulong n, GEN pol, GEN p)
     668             : {
     669       14651 :   if (typ(x) == t_INT) return Fp_powu(x,n,p);
     670         749 :   return FpXQ_powu(x,n,pol,p);
     671             : }
     672             : 
     673             : GEN
     674      709980 : Fq_sqrt(GEN x, GEN T, GEN p)
     675             : {
     676      709980 :   if (typ(x) == t_INT)
     677             :   {
     678      699160 :     if (!T || odd(get_FpX_degree(T))) return Fp_sqrt(x,p);
     679         336 :     x = scalarpol_shallow(x, get_FpX_var(T));
     680             :   }
     681       11156 :   return FpXQ_sqrt(x,T,p);
     682             : }
     683             : GEN
     684       60851 : Fq_sqrtn(GEN x, GEN n, GEN T, GEN p, GEN *zeta)
     685             : {
     686       60851 :   if (typ(x) == t_INT)
     687             :   {
     688             :     long d;
     689       60585 :     if (!T) return Fp_sqrtn(x,n,p,zeta);
     690         812 :     d = get_FpX_degree(T);
     691         812 :     if (ugcdiu(n,d) == 1)
     692             :     {
     693         651 :       if (!zeta) return Fp_sqrtn(x,n,p,NULL);
     694             :       /* gcd(n,p-1)=gcd(n,q-1): same number of solutions in Fp and F_q */
     695         644 :       if (equalii(gcdii(subiu(p,1),n), gcdii(subiu(Fp_powu(p,d,n), 1), n)))
     696         623 :         return Fp_sqrtn(x,n,p,zeta);
     697             :     }
     698         182 :     x = scalarpol(x, get_FpX_var(T)); /* left on stack */
     699             :   }
     700         448 :   return FpXQ_sqrtn(x,n,T,p,zeta);
     701             : }
     702             : 
     703             : struct _Fq_field
     704             : {
     705             :   GEN T, p;
     706             : };
     707             : 
     708             : static GEN
     709      302463 : _Fq_red(void *E, GEN x)
     710      302463 : { struct _Fq_field *s = (struct _Fq_field *)E;
     711      302463 :   return Fq_red(x, s->T, s->p);
     712             : }
     713             : 
     714             : static GEN
     715      357525 : _Fq_add(void *E, GEN x, GEN y)
     716             : {
     717             :   (void) E;
     718      357525 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     719             :   {
     720          14 :     case 0: return addii(x,y);
     721           0 :     case 1: return ZX_Z_add(x,y);
     722       15918 :     case 2: return ZX_Z_add(y,x);
     723      341593 :     default: return ZX_add(x,y);
     724             :   }
     725             : }
     726             : 
     727             : static GEN
     728      315028 : _Fq_neg(void *E, GEN x) { (void) E; return typ(x)==t_POL?ZX_neg(x):negi(x); }
     729             : 
     730             : static GEN
     731      237559 : _Fq_mul(void *E, GEN x, GEN y)
     732             : {
     733             :   (void) E;
     734      237559 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     735             :   {
     736         133 :     case 0: return mulii(x,y);
     737        1085 :     case 1: return ZX_Z_mul(x,y);
     738        1043 :     case 2: return ZX_Z_mul(y,x);
     739      235298 :     default: return ZX_mul(x,y);
     740             :   }
     741             : }
     742             : 
     743             : static GEN
     744        9331 : _Fq_inv(void *E, GEN x)
     745        9331 : { struct _Fq_field *s = (struct _Fq_field *)E;
     746        9331 :   return Fq_inv(x,s->T,s->p);
     747             : }
     748             : 
     749             : static int
     750       38388 : _Fq_equal0(GEN x) { return signe(x)==0; }
     751             : 
     752             : static GEN
     753       13965 : _Fq_s(void *E, long x) { (void) E; return stoi(x); }
     754             : 
     755             : static const struct bb_field Fq_field={_Fq_red,_Fq_add,_Fq_mul,_Fq_neg,
     756             :                                        _Fq_inv,_Fq_equal0,_Fq_s};
     757             : 
     758        4165 : const struct bb_field *get_Fq_field(void **E, GEN T, GEN p)
     759             : {
     760        4165 :   if (!T)
     761           0 :     return get_Fp_field(E, p);
     762             :   else
     763             :   {
     764        4165 :     GEN z = new_chunk(sizeof(struct _Fq_field));
     765        4165 :     struct _Fq_field *e = (struct _Fq_field *) z;
     766        4165 :     e->T = T; e->p  = p; *E = (void*)e;
     767        4165 :     return &Fq_field;
     768             :   }
     769             : }
     770             : 
     771             : /*******************************************************************/
     772             : /*                                                                 */
     773             : /*                             Fq[X]                               */
     774             : /*                                                                 */
     775             : /*******************************************************************/
     776             : /* P(X + c) */
     777             : GEN
     778         266 : FpX_translate(GEN P, GEN c, GEN p)
     779             : {
     780         266 :   pari_sp av = avma;
     781             :   GEN Q, *R;
     782             :   long i, k, n;
     783             : 
     784         266 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     785         266 :   Q = leafcopy(P);
     786         266 :   R = (GEN*)(Q+2); n = degpol(P);
     787        3738 :   for (i=1; i<=n; i++)
     788             :   {
     789      118153 :     for (k=n-i; k<n; k++)
     790      114681 :       R[k] = Fp_add(R[k], Fp_mul(c, R[k+1], p), p);
     791             : 
     792        3472 :     if (gc_needed(av,2))
     793             :     {
     794           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FpX_translate, i = %ld/%ld", i,n);
     795           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     796             :     }
     797             :   }
     798         266 :   return gerepilecopy(av, FpX_renormalize(Q, lg(Q)));
     799             : }
     800             : /* P(X + c), c an Fq */
     801             : GEN
     802       33880 : FqX_translate(GEN P, GEN c, GEN T, GEN p)
     803             : {
     804       33880 :   pari_sp av = avma;
     805             :   GEN Q, *R;
     806             :   long i, k, n;
     807             : 
     808             :   /* signe works for t_(INT|POL) */
     809       33880 :   if (!signe(P) || !signe(c)) return RgX_copy(P);
     810       33880 :   Q = leafcopy(P);
     811       33880 :   R = (GEN*)(Q+2); n = degpol(P);
     812      150059 :   for (i=1; i<=n; i++)
     813             :   {
     814      433559 :     for (k=n-i; k<n; k++)
     815      317380 :       R[k] = Fq_add(R[k], Fq_mul(c, R[k+1], T, p), T, p);
     816             : 
     817      116179 :     if (gc_needed(av,2))
     818             :     {
     819           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FqX_translate, i = %ld/%ld", i,n);
     820           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     821             :     }
     822             :   }
     823       33880 :   return gerepilecopy(av, FpXQX_renormalize(Q, lg(Q)));
     824             : }
     825             : 
     826             : GEN
     827        5145 : FqV_roots_to_pol(GEN V, GEN T, GEN p, long v)
     828             : {
     829        5145 :   pari_sp ltop = avma;
     830             :   long k;
     831             :   GEN W;
     832        5145 :   if (lgefint(p) == 3)
     833             :   {
     834         491 :     ulong pp = p[2];
     835         491 :     GEN Tl = ZX_to_Flx(T, pp);
     836         491 :     GEN Vl = FqV_to_FlxV(V, T, p);
     837         491 :     Tl = FlxqV_roots_to_pol(Vl, Tl, pp, v);
     838         491 :     return gerepileupto(ltop, FlxX_to_ZXX(Tl));
     839             :   }
     840        4654 :   W = cgetg(lg(V),t_VEC);
     841       60038 :   for(k=1; k < lg(V); k++)
     842       55384 :     gel(W,k) = deg1pol_shallow(gen_1,Fq_neg(gel(V,k),T,p),v);
     843        4654 :   return gerepileupto(ltop, FpXQXV_prod(W, T, p));
     844             : }
     845             : 
     846             : GEN
     847      128725 : FqV_red(GEN x, GEN T, GEN p)
     848      128725 : { pari_APPLY_same(Fq_red(gel(x,i), T, p)) }
     849             : 
     850             : GEN
     851           0 : FqC_add(GEN x, GEN y, GEN T, GEN p)
     852             : {
     853           0 :   if (!T) return FpC_add(x, y, p);
     854           0 :   pari_APPLY_type(t_COL, Fq_add(gel(x,i), gel(y,i), T, p))
     855             : }
     856             : 
     857             : GEN
     858           0 : FqC_sub(GEN x, GEN y, GEN T, GEN p)
     859             : {
     860           0 :   if (!T) return FpC_sub(x, y, p);
     861           0 :   pari_APPLY_type(t_COL, Fq_sub(gel(x,i), gel(y,i), T, p))
     862             : }
     863             : 
     864             : GEN
     865           0 : FqC_Fq_mul(GEN x, GEN y, GEN T, GEN p)
     866             : {
     867           0 :   if (!T) return FpC_Fp_mul(x, y, p);
     868           0 :   pari_APPLY_type(t_COL, Fq_mul(gel(x,i),y,T,p))
     869             : }
     870             : 
     871             : GEN
     872         491 : FqV_to_FlxV(GEN x, GEN T, GEN pp)
     873             : {
     874         491 :   long vT = evalvarn(get_FpX_var(T));
     875         491 :   ulong p = pp[2];
     876         491 :   pari_APPLY_type(t_VEC, typ(gel(x,i))==t_INT?  Z_to_Flx(gel(x,i), p, vT)
     877             :                                              : ZX_to_Flx(gel(x,i), p))
     878             : }
     879             : 
     880             : GEN
     881       79583 : FqC_to_FlxC(GEN x, GEN T, GEN pp)
     882             : {
     883       79583 :   long vT = evalvarn(get_FpX_var(T));
     884       79582 :   ulong p = pp[2];
     885       79582 :   pari_APPLY_type(t_COL, typ(gel(x,i))==t_INT?  Z_to_Flx(gel(x,i), p, vT)
     886             :                                              : ZX_to_Flx(gel(x,i), p))
     887             : }
     888             : 
     889             : GEN
     890       11174 : FqM_to_FlxM(GEN x, GEN T, GEN p)
     891       11174 : { pari_APPLY_same(FqC_to_FlxC(gel(x,i), T, p)) }
     892             : 
     893             : GEN
     894        3048 : FpXC_center(GEN x, GEN p, GEN pov2)
     895        3048 : { pari_APPLY_type(t_COL, FpX_center(gel(x,i), p, pov2)) }
     896             : 
     897             : GEN
     898        1393 : FpXM_center(GEN x, GEN p, GEN pov2)
     899        1393 : { pari_APPLY_same(FpXC_center(gel(x,i), p, pov2)) }
     900             : 
     901             : /*******************************************************************/
     902             : /*                                                                 */
     903             : /*                          GENERIC CRT                            */
     904             : /*                                                                 */
     905             : /*******************************************************************/
     906             : static GEN
     907     4337581 : primelist(forprime_t *S, long n, GEN dB)
     908             : {
     909     4337581 :   GEN P = cgetg(n+1, t_VECSMALL);
     910     4337573 :   long i = 1;
     911             :   ulong p;
     912    13882203 :   while (i <= n && (p = u_forprime_next(S)))
     913     5207057 :     if (!dB || umodiu(dB, p)) P[i++] = p;
     914     4337617 :   return P;
     915             : }
     916             : 
     917             : void
     918     3574845 : gen_inccrt_i(const char *str, GEN worker, GEN dB, long n, long mmin,
     919             :              forprime_t *S, GEN *pH, GEN *pmod, GEN crt(GEN, GEN, GEN*),
     920             :              GEN center(GEN, GEN, GEN))
     921             : {
     922     3574845 :   long m = minss(mmin, n);
     923             :   GEN  H, P, mod;
     924             :   pari_timer ti;
     925     3574845 :   if (DEBUGLEVEL > 4)
     926             :   {
     927           0 :     timer_start(&ti);
     928           0 :     err_printf("%s: nb primes: %ld\n",str, n);
     929             :   }
     930     3574845 :   if (m == 1)
     931             :   {
     932     3460108 :     GEN P = primelist(S, n, dB);
     933     3460108 :     GEN done = closure_callgen1(worker, P);
     934     3460108 :     H = gel(done,1);
     935     3460108 :     mod = gel(done,2);
     936     3460108 :     if (!*pH && center) H = center(H, mod, shifti(mod,-1));
     937     3460108 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: modular", str);
     938             :   }
     939             :   else
     940             :   {
     941      114737 :     long i, s = (n+m-1)/m, r = m - (m*s-n), di = 0;
     942             :     struct pari_mt pt;
     943      114737 :     long pending = 0;
     944      114737 :     H = cgetg(m+1, t_VEC); P = cgetg(m+1, t_VEC);
     945      114737 :     mt_queue_start_lim(&pt, worker, m);
     946     1087726 :     for (i=1; i<=m || pending; i++)
     947             :     {
     948             :       GEN done;
     949      972989 :       GEN pr = i <= m ? mkvec(primelist(S, i<=r ? s: s-1, dB)): NULL;
     950      972992 :       mt_queue_submit(&pt, i, pr);
     951      972956 :       done = mt_queue_get(&pt, NULL, &pending);
     952      972953 :       if (done)
     953             :       {
     954      877463 :         di++;
     955      877463 :         gel(H, di) = gel(done,1);
     956      877463 :         gel(P, di) = gel(done,2);
     957      877463 :         if (DEBUGLEVEL>5) err_printf("%ld%% ",100*di/m);
     958             :       }
     959             :     }
     960      114737 :     mt_queue_end(&pt);
     961      114737 :     if (DEBUGLEVEL>5) err_printf("\n");
     962      114737 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: modular", str);
     963      114737 :     H = crt(H, P, &mod);
     964      114737 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: chinese", str);
     965             :   }
     966     3574845 :   if (*pH) H = crt(mkvec2(*pH, H), mkvec2(*pmod, mod), &mod);
     967     3574845 :   *pH = H; *pmod = mod;
     968     3574845 : }
     969             : void
     970      253441 : gen_inccrt(const char *str, GEN worker, GEN dB, long n, long mmin,
     971             :            forprime_t *S, GEN *pH, GEN *pmod, GEN crt(GEN, GEN, GEN*),
     972             :            GEN center(GEN, GEN, GEN))
     973             : {
     974      253441 :   pari_sp av = avma;
     975      253441 :   gen_inccrt_i(str, worker, dB, n, mmin, S, pH, pmod, crt, center);
     976      253441 :   gerepileall(av, 2, pH, pmod);
     977      253441 : }
     978             : 
     979             : GEN
     980      119760 : gen_crt(const char *str, GEN worker, forprime_t *S, GEN dB, ulong bound, long mmin, GEN *pmod,
     981             :         GEN crt(GEN, GEN, GEN*), GEN center(GEN, GEN, GEN))
     982             : {
     983      119760 :   GEN mod = gen_1, H = NULL;
     984             :   ulong e;
     985             : 
     986      119760 :   bound++;
     987      359280 :   while (bound > (e = expi(mod)))
     988             :   {
     989      119760 :     long n = (bound - e) / expu(S->p) + 1;
     990      119760 :     gen_inccrt(str, worker, dB, n, mmin, S, &H, &mod, crt, center);
     991             :   }
     992      119760 :   if (pmod) *pmod = mod;
     993      119760 :   return H;
     994             : }
     995             : 
     996             : /*******************************************************************/
     997             : /*                                                                 */
     998             : /*                          MODULAR GCD                            */
     999             : /*                                                                 */
    1000             : /*******************************************************************/
    1001             : /* return z = a mod q, b mod p (p,q) = 1; qinv = 1/q mod p; a in ]-q,q] */
    1002             : static GEN
    1003      453438 : Fl_chinese_coprime(GEN a, ulong b, GEN q, ulong p, ulong qinv, GEN pq, GEN pq2)
    1004             : {
    1005      453438 :   ulong d, amod = umodiu(a, p);
    1006      453438 :   pari_sp av = avma;
    1007             :   GEN ax;
    1008             : 
    1009      453438 :   if (b == amod) return NULL;
    1010      200463 :   d = Fl_mul(Fl_sub(b, amod, p), qinv, p); /* != 0 */
    1011      200463 :   if (d >= 1 + (p>>1))
    1012       99613 :     ax = subii(a, mului(p-d, q));
    1013             :   else
    1014             :   {
    1015      100850 :     ax = addii(a, mului(d, q)); /* in ]0, pq[ assuming a in ]-q,q[ */
    1016      100850 :     if (cmpii(ax,pq2) > 0) ax = subii(ax,pq);
    1017             :   }
    1018      200463 :   return gerepileuptoint(av, ax);
    1019             : }
    1020             : GEN
    1021         364 : Z_init_CRT(ulong Hp, ulong p) { return stoi(Fl_center(Hp, p, p>>1)); }
    1022             : GEN
    1023        1428 : ZX_init_CRT(GEN Hp, ulong p, long v)
    1024             : {
    1025        1428 :   long i, l = lg(Hp), lim = (long)(p>>1);
    1026        1428 :   GEN H = cgetg(l, t_POL);
    1027        1428 :   H[1] = evalsigne(1) | evalvarn(v);
    1028       17626 :   for (i=2; i<l; i++)
    1029       16198 :     gel(H,i) = stoi(Fl_center(Hp[i], p, lim));
    1030        1428 :   return ZX_renormalize(H,l);
    1031             : }
    1032             : 
    1033             : GEN
    1034        2682 : ZM_init_CRT(GEN Hp, ulong p)
    1035             : {
    1036        2682 :   long i,j, m, l = lg(Hp), lim = (long)(p>>1);
    1037        2682 :   GEN c, cp, H = cgetg(l, t_MAT);
    1038        2682 :   if (l==1) return H;
    1039        2682 :   m = lgcols(Hp);
    1040        9285 :   for (j=1; j<l; j++)
    1041             :   {
    1042        6603 :     cp = gel(Hp,j);
    1043        6603 :     c = cgetg(m, t_COL);
    1044        6603 :     gel(H,j) = c;
    1045        6603 :     for (i=1; i<m; i++) gel(c,i) = stoi(Fl_center(cp[i],p, lim));
    1046             :   }
    1047        2682 :   return H;
    1048             : }
    1049             : 
    1050             : int
    1051        7483 : Z_incremental_CRT(GEN *H, ulong Hp, GEN *ptq, ulong p)
    1052             : {
    1053        7483 :   GEN h, q = *ptq, qp = muliu(q,p);
    1054        7483 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1055        7483 :   int stable = 1;
    1056        7483 :   h = Fl_chinese_coprime(*H,Hp,q,p,qinv,qp,shifti(qp,-1));
    1057        7483 :   if (h) { *H = h; stable = 0; }
    1058        7483 :   *ptq = qp; return stable;
    1059             : }
    1060             : 
    1061             : static int
    1062        3501 : ZX_incremental_CRT_raw(GEN *ptH, GEN Hp, GEN q, GEN qp, ulong p)
    1063             : {
    1064        3501 :   GEN H = *ptH, h, qp2 = shifti(qp,-1);
    1065        3501 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1066        3501 :   long i, l = lg(H), lp = lg(Hp);
    1067        3501 :   int stable = 1;
    1068             : 
    1069        3501 :   if (l < lp)
    1070             :   { /* degree increases */
    1071           0 :     GEN x = cgetg(lp, t_POL);
    1072           0 :     for (i=1; i<l; i++)  x[i] = H[i];
    1073           0 :     for (   ; i<lp; i++) gel(x,i) = gen_0;
    1074           0 :     *ptH = H = x;
    1075           0 :     stable = 0;
    1076        3501 :   } else if (l > lp)
    1077             :   { /* degree decreases */
    1078           0 :     GEN x = cgetg(l, t_VECSMALL);
    1079           0 :     for (i=1; i<lp; i++)  x[i] = Hp[i];
    1080           0 :     for (   ; i<l; i++) x[i] = 0;
    1081           0 :     Hp = x; lp = l;
    1082             :   }
    1083       62258 :   for (i=2; i<lp; i++)
    1084             :   {
    1085       58757 :     h = Fl_chinese_coprime(gel(H,i),Hp[i],q,p,qinv,qp,qp2);
    1086       58757 :     if (h) { gel(H,i) = h; stable = 0; }
    1087             :   }
    1088        3501 :   (void)ZX_renormalize(H,lp);
    1089        3501 :   return stable;
    1090             : }
    1091             : 
    1092             : int
    1093           0 : ZX_incremental_CRT(GEN *ptH, GEN Hp, GEN *ptq, ulong p)
    1094             : {
    1095           0 :   GEN q = *ptq, qp = muliu(q,p);
    1096           0 :   int stable = ZX_incremental_CRT_raw(ptH, Hp, q, qp, p);
    1097           0 :   *ptq = qp; return stable;
    1098             : }
    1099             : 
    1100             : int
    1101        6331 : ZM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
    1102             : {
    1103        6331 :   GEN h, H = *pH, q = *ptq, qp = muliu(q, p), qp2 = shifti(qp,-1);
    1104        6331 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1105        6331 :   long i,j, l = lg(H), m = lgcols(H);
    1106        6331 :   int stable = 1;
    1107       21041 :   for (j=1; j<l; j++)
    1108      154818 :     for (i=1; i<m; i++)
    1109             :     {
    1110      140108 :       h = Fl_chinese_coprime(gcoeff(H,i,j), coeff(Hp,i,j),q,p,qinv,qp,qp2);
    1111      140108 :       if (h) { gcoeff(H,i,j) = h; stable = 0; }
    1112             :     }
    1113        6331 :   *ptq = qp; return stable;
    1114             : }
    1115             : 
    1116             : GEN
    1117         959 : ZXM_init_CRT(GEN Hp, long deg, ulong p)
    1118             : {
    1119             :   long i, j, k;
    1120             :   GEN H;
    1121         959 :   long m, l = lg(Hp), lim = (long)(p>>1), n;
    1122         959 :   H = cgetg(l, t_MAT);
    1123         959 :   if (l==1) return H;
    1124         959 :   m = lgcols(Hp);
    1125         959 :   n = deg + 3;
    1126        4151 :   for (j=1; j<l; j++)
    1127             :   {
    1128        3192 :     GEN cp = gel(Hp,j);
    1129        3192 :     GEN c = cgetg(m, t_COL);
    1130        3192 :     gel(H,j) = c;
    1131       48832 :     for (i=1; i<m; i++)
    1132             :     {
    1133       45640 :       GEN dp = gel(cp, i);
    1134       45640 :       long l = lg(dp);
    1135       45640 :       GEN d = cgetg(n, t_POL);
    1136       45640 :       gel(c, i) = d;
    1137       45640 :       d[1] = dp[1];
    1138       91294 :       for (k=2; k<l; k++)
    1139       45654 :         gel(d,k) = stoi(Fl_center(dp[k], p, lim));
    1140       91672 :       for (   ; k<n; k++)
    1141       46032 :         gel(d,k) = gen_0;
    1142             :     }
    1143             :   }
    1144         959 :   return H;
    1145             : }
    1146             : 
    1147             : int
    1148         867 : ZXM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
    1149             : {
    1150         867 :   GEN v, H = *pH, q = *ptq, qp = muliu(q, p), qp2 = shifti(qp,-1);
    1151         867 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1152         867 :   long i,j,k, l = lg(H), m = lgcols(H), n = lg(gmael(H,1,1));
    1153         867 :   int stable = 1;
    1154        6528 :   for (j=1; j<l; j++)
    1155      129196 :     for (i=1; i<m; i++)
    1156             :     {
    1157      123535 :       GEN h = gmael(H,j,i), hp = gmael(Hp,j,i);
    1158      123535 :       long lh = lg(hp);
    1159      250257 :       for (k=2; k<lh; k++)
    1160             :       {
    1161      126722 :         v = Fl_chinese_coprime(gel(h,k),uel(hp,k),q,p,qinv,qp,qp2);
    1162      126722 :         if (v) { gel(h,k) = v; stable = 0; }
    1163             :       }
    1164      243903 :       for (; k<n; k++)
    1165             :       {
    1166      120368 :         v = Fl_chinese_coprime(gel(h,k),0,q,p,qinv,qp,qp2);
    1167      120368 :         if (v) { gel(h,k) = v; stable = 0; }
    1168             :       }
    1169             :     }
    1170         867 :   *ptq = qp; return stable;
    1171             : }
    1172             : 
    1173             : /* record the degrees of Euclidean remainders (make them as large as
    1174             :  * possible : smaller values correspond to a degenerate sequence) */
    1175             : static void
    1176        2457 : Flx_resultant_set_dglist(GEN a, GEN b, GEN dglist, ulong p)
    1177             : {
    1178             :   long da,db,dc, ind;
    1179        2457 :   pari_sp av = avma;
    1180             : 
    1181        2457 :   if (lgpol(a)==0 || lgpol(b)==0) return;
    1182        2436 :   da = degpol(a);
    1183        2436 :   db = degpol(b);
    1184        2436 :   if (db > da)
    1185           0 :   { swapspec(a,b, da,db); }
    1186        2436 :   else if (!da) return;
    1187        2436 :   ind = 0;
    1188       14252 :   while (db)
    1189             :   {
    1190        9380 :     GEN c = Flx_rem(a,b, p);
    1191        9380 :     a = b; b = c; dc = degpol(c);
    1192        9380 :     if (dc < 0) break;
    1193             : 
    1194        9380 :     ind++;
    1195        9380 :     if (dc > dglist[ind]) dglist[ind] = dc;
    1196        9380 :     if (gc_needed(av,2))
    1197             :     {
    1198           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1199           0 :       gerepileall(av, 2, &a,&b);
    1200             :     }
    1201        9380 :     db = dc; /* = degpol(b) */
    1202             :   }
    1203        2436 :   if (ind+1 > lg(dglist)) setlg(dglist,ind+1);
    1204        2436 :   set_avma(av);
    1205             : }
    1206             : /* assuming the PRS finishes on a degree 1 polynomial C0 + C1X, with
    1207             :  * "generic" degree sequence as given by dglist, set *Ci and return
    1208             :  * resultant(a,b). Modular version of Collins's subresultant */
    1209             : static ulong
    1210       31928 : Flx_resultant_all(GEN a, GEN b, long *C0, long *C1, GEN dglist, ulong p)
    1211             : {
    1212             :   long da,db,dc, ind;
    1213       31928 :   ulong lb, res, g = 1UL, h = 1UL, ca = 1UL, cb = 1UL;
    1214       31928 :   int s = 1;
    1215       31928 :   pari_sp av = avma;
    1216             : 
    1217       31928 :   *C0 = 1; *C1 = 0;
    1218       31928 :   if (lgpol(a)==0 || lgpol(b)==0) return 0;
    1219       31886 :   da = degpol(a);
    1220       31886 :   db = degpol(b);
    1221       31886 :   if (db > da)
    1222             :   {
    1223           0 :     swapspec(a,b, da,db);
    1224           0 :     if (both_odd(da,db)) s = -s;
    1225             :   }
    1226       31886 :   else if (!da) return 1; /* = a[2] ^ db, since 0 <= db <= da = 0 */
    1227       31886 :   ind = 0;
    1228      319903 :   while (db)
    1229             :   { /* sub-resultant algo., applied to ca * a and cb * b, ca,cb scalars,
    1230             :      * da = deg a, db = deg b */
    1231      256584 :     GEN c = Flx_rem(a,b, p);
    1232      256584 :     long delta = da - db;
    1233             : 
    1234      256584 :     if (both_odd(da,db)) s = -s;
    1235      256584 :     lb = Fl_mul(b[db+2], cb, p);
    1236      256584 :     a = b; b = c; dc = degpol(c);
    1237      256584 :     ind++;
    1238      256584 :     if (dc != dglist[ind]) return gc_ulong(av,0); /* degenerates */
    1239      256131 :     if (g == h)
    1240             :     { /* frequent */
    1241      253262 :       ulong cc = Fl_mul(ca, Fl_powu(Fl_div(lb,g,p), delta+1, p), p);
    1242      253262 :       ca = cb;
    1243      253262 :       cb = cc;
    1244             :     }
    1245             :     else
    1246             :     {
    1247        2869 :       ulong cc = Fl_mul(ca, Fl_powu(lb, delta+1, p), p);
    1248        2869 :       ulong ghdelta = Fl_mul(g, Fl_powu(h, delta, p), p);
    1249        2869 :       ca = cb;
    1250        2869 :       cb = Fl_div(cc, ghdelta, p);
    1251             :     }
    1252      256131 :     da = db; /* = degpol(a) */
    1253      256131 :     db = dc; /* = degpol(b) */
    1254             : 
    1255      256131 :     g = lb;
    1256      256131 :     if (delta == 1)
    1257      244287 :       h = g; /* frequent */
    1258             :     else
    1259       11844 :       h = Fl_mul(h, Fl_powu(Fl_div(g,h,p), delta, p), p);
    1260             : 
    1261      256131 :     if (gc_needed(av,2))
    1262             :     {
    1263           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1264           0 :       gerepileall(av, 2, &a,&b);
    1265             :     }
    1266             :   }
    1267       31433 :   if (da > 1) return 0; /* Failure */
    1268             :   /* last non-constant polynomial has degree 1 */
    1269       31433 :   *C0 = Fl_mul(ca, a[2], p);
    1270       31433 :   *C1 = Fl_mul(ca, a[3], p);
    1271       31433 :   res = Fl_mul(cb, b[2], p);
    1272       31433 :   if (s == -1) res = p - res;
    1273       31433 :   return gc_ulong(av,res);
    1274             : }
    1275             : 
    1276             : /* Q a vector of polynomials representing B in Fp[X][Y], evaluate at X = x,
    1277             :  * Return 0 in case of degree drop. */
    1278             : static GEN
    1279       34385 : FlxY_evalx_drop(GEN Q, ulong x, ulong p)
    1280             : {
    1281             :   GEN z;
    1282       34385 :   long i, lb = lg(Q);
    1283       34385 :   ulong leadz = Flx_eval(leading_coeff(Q), x, p);
    1284       34385 :   long vs=mael(Q,2,1);
    1285       34385 :   if (!leadz) return zero_Flx(vs);
    1286             : 
    1287       34322 :   z = cgetg(lb, t_VECSMALL); z[1] = vs;
    1288       34322 :   for (i=2; i<lb-1; i++) z[i] = Flx_eval(gel(Q,i), x, p);
    1289       34322 :   z[i] = leadz; return z;
    1290             : }
    1291             : 
    1292             : GEN
    1293       19964 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
    1294             : {
    1295       19964 :   pari_sp av = avma;
    1296       19964 :   long i, lb = lg(Q);
    1297             :   GEN z;
    1298       19964 :   if (!T) return FpXY_evaly(Q, y, p, vx);
    1299        1232 :   if (lb == 2) return pol_0(vx);
    1300        1232 :   z = gel(Q, lb-1);
    1301        1232 :   if (lb == 3 || !signe(y)) return typ(z)==t_INT? scalar_ZX(z, vx): ZX_copy(z);
    1302             : 
    1303        1232 :   if (typ(z) == t_INT) z = scalar_ZX_shallow(z, vx);
    1304       26572 :   for (i=lb-2; i>=2; i--)
    1305             :   {
    1306       25340 :     GEN c = gel(Q,i);
    1307       25340 :     z = FqX_Fq_mul(z, y, T, p);
    1308       25340 :     z = typ(c) == t_INT? FqX_Fq_add(z,c,T,p): FqX_add(z,c,T,p);
    1309             :   }
    1310        1232 :   return gerepileupto(av, z);
    1311             : }
    1312             : 
    1313             : static GEN
    1314       17026 : ZX_norml1(GEN x)
    1315             : {
    1316       17026 :   long i, l = lg(x);
    1317             :   GEN s;
    1318             : 
    1319       17026 :   if (l == 2) return gen_0;
    1320       10404 :   s = gel(x, l-1); /* != 0 */
    1321       37659 :   for (i = l-2; i > 1; i--) {
    1322       27255 :     GEN xi = gel(x,i);
    1323       27255 :     if (!signe(x)) continue;
    1324       27255 :     s = addii_sign(s,1, xi,1);
    1325             :   }
    1326       10404 :   return s;
    1327             : }
    1328             : 
    1329             : /* Interpolate at roots of 1 and use Hadamard bound for univariate resultant:
    1330             :  *   bound = N_2(A)^degpol B N_2(B)^degpol(A),  where
    1331             :  *     N_2(A) = sqrt(sum (N_1(Ai))^2)
    1332             :  * Return e such that Res(A, B) < 2^e */
    1333             : ulong
    1334       38811 : ZX_ZXY_ResBound(GEN A, GEN B, GEN dB)
    1335             : {
    1336       38811 :   pari_sp av = avma;
    1337       38811 :   GEN a = gen_0, b = gen_0;
    1338       38811 :   long i , lA = lg(A), lB = lg(B);
    1339             :   double loga, logb;
    1340      312021 :   for (i=2; i<lA; i++)
    1341             :   {
    1342      273210 :     a = addii(a, sqri(gel(A,i)));
    1343      273210 :     if (gc_needed(av,1))
    1344             :     {
    1345           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
    1346           0 :       a = gerepileupto(av, a);
    1347             :     }
    1348             :   }
    1349       38811 :   loga = dbllog2(a); set_avma(av);
    1350      278678 :   for (i=2; i<lB; i++)
    1351             :   {
    1352      239867 :     GEN t = gel(B,i);
    1353      239867 :     if (typ(t) == t_POL) t = ZX_norml1(t);
    1354      239867 :     b = addii(b, sqri(t));
    1355      239867 :     if (gc_needed(av,1))
    1356             :     {
    1357           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
    1358           0 :       b = gerepileupto(av, b);
    1359             :     }
    1360             :   }
    1361       38811 :   logb = dbllog2(b); if (dB) logb -= 2 * dbllog2(dB);
    1362       38811 :   i = (long)((degpol(B) * loga + degpol(A) * logb) / 2);
    1363       38811 :   return gc_ulong(av, (i <= 0)? 1: 1 + (ulong)i);
    1364             : }
    1365             : /* special case B = A' */
    1366             : static ulong
    1367       57887 : ZX_discbound(GEN A)
    1368             : {
    1369       57887 :   pari_sp av = avma;
    1370       57887 :   GEN a = gen_0, b = gen_0;
    1371       57887 :   long i , lA = lg(A), dA = degpol(A);
    1372             :   double loga, logb;
    1373      755695 :   for (i = 2; i < lA; i++)
    1374             :   {
    1375      697807 :     GEN c = sqri(gel(A,i));
    1376      697807 :     a = addii(a, c);
    1377      697810 :     if (i > 2) b = addii(b, mulii(c, sqru(i-2)));
    1378      697807 :     if (gc_needed(av,1))
    1379             :     {
    1380           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_discbound i = %ld",i);
    1381           0 :       gerepileall(av, 2, &a, &b);
    1382             :     }
    1383             :   }
    1384       57888 :   loga = dbllog2(a);
    1385       57888 :   logb = dbllog2(b); set_avma(av);
    1386       57888 :   i = (long)(((dA-1) * loga + dA * logb) / 2);
    1387       57888 :   return (i <= 0)? 1: 1 + (ulong)i;
    1388             : }
    1389             : 
    1390             : /* return Res(a(Y), b(n,Y)) over Fp. la = leading_coeff(a) [for efficiency] */
    1391             : static ulong
    1392      259213 : Flx_FlxY_eval_resultant(GEN a, GEN b, ulong n, ulong p, ulong la)
    1393             : {
    1394      259213 :   GEN ev = FlxY_evalx(b, n, p);
    1395      259231 :   long drop = lg(b) - lg(ev);
    1396      259231 :   ulong r = Flx_resultant(a, ev, p);
    1397      259212 :   if (drop && la != 1) r = Fl_mul(r, Fl_powu(la, drop,p),p);
    1398      259213 :   return r;
    1399             : }
    1400             : static GEN
    1401           4 : FpX_FpXY_eval_resultant(GEN a, GEN b, GEN n, GEN p, GEN la, long db, long vX)
    1402             : {
    1403           4 :   GEN ev = FpXY_evaly(b, n, p, vX);
    1404           4 :   long drop = db-degpol(ev);
    1405           4 :   GEN r = FpX_resultant(a, ev, p);
    1406           4 :   if (drop && !gequal1(la)) r = Fp_mul(r, Fp_powu(la, drop,p),p);
    1407           4 :   return r;
    1408             : }
    1409             : 
    1410             : /* assume dres := deg(Res_X(a,b), Y) <= deg(a,X) * deg(b,Y) < p */
    1411             : /* Return a Fly */
    1412             : static GEN
    1413       14605 : Flx_FlxY_resultant_polint(GEN a, GEN b, ulong p, long dres, long sx)
    1414             : {
    1415             :   long i;
    1416       14605 :   ulong n, la = Flx_lead(a);
    1417       14605 :   GEN  x = cgetg(dres+2, t_VECSMALL);
    1418       14605 :   GEN  y = cgetg(dres+2, t_VECSMALL);
    1419             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1420             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1421      138313 :   for (i=0,n = 1; i < dres; n++)
    1422             :   {
    1423      123708 :     x[++i] = n;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1424      123699 :     x[++i] = p-n; y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1425             :   }
    1426       14605 :   if (i == dres)
    1427             :   {
    1428       11873 :     x[++i] = 0;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1429             :   }
    1430       14605 :   return Flv_polint(x,y, p, sx);
    1431             : }
    1432             : 
    1433             : static GEN
    1434        4940 : FlxX_pseudorem(GEN x, GEN y, ulong p)
    1435             : {
    1436        4940 :   long vx = varn(x), dx, dy, dz, i, lx, dp;
    1437        4940 :   pari_sp av = avma, av2;
    1438             : 
    1439        4940 :   if (!signe(y)) pari_err_INV("FlxX_pseudorem",y);
    1440        4940 :   (void)new_chunk(2);
    1441        4939 :   dx=degpol(x); x = RgX_recip_shallow(x)+2;
    1442        4941 :   dy=degpol(y); y = RgX_recip_shallow(y)+2; dz=dx-dy; dp = dz+1;
    1443        4939 :   av2 = avma;
    1444             :   for (;;)
    1445             :   {
    1446       75413 :     gel(x,0) = Flx_neg(gel(x,0), p); dp--;
    1447      150336 :     for (i=1; i<=dy; i++)
    1448      218058 :       gel(x,i) = Flx_add( Flx_mul(gel(y,0), gel(x,i), p),
    1449      109029 :                               Flx_mul(gel(x,0), gel(y,i), p), p );
    1450      656798 :     for (   ; i<=dx; i++)
    1451      616670 :       gel(x,i) = Flx_mul(gel(y,0), gel(x,i), p);
    1452       42693 :     do { x++; dx--; } while (dx >= 0 && lg(gel(x,0))==2);
    1453       40128 :     if (dx < dy) break;
    1454       35197 :     if (gc_needed(av2,1))
    1455             :     {
    1456           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_pseudorem dx = %ld >= %ld",dx,dy);
    1457           0 :       gerepilecoeffs(av2,x,dx+1);
    1458             :     }
    1459             :   }
    1460        4931 :   if (dx < 0) return zero_Flx(0);
    1461        4931 :   lx = dx+3; x -= 2;
    1462        4931 :   x[0]=evaltyp(t_POL) | evallg(lx);
    1463        4931 :   x[1]=evalsigne(1) | evalvarn(vx);
    1464        4931 :   x = RgX_recip_shallow(x);
    1465        4940 :   if (dp)
    1466             :   { /* multiply by y[0]^dp   [beware dummy vars from FpX_FpXY_resultant] */
    1467        1299 :     GEN t = Flx_powu(gel(y,0), dp, p);
    1468        5197 :     for (i=2; i<lx; i++)
    1469        3897 :       gel(x,i) = Flx_mul(gel(x,i), t, p);
    1470             :   }
    1471        4941 :   return gerepilecopy(av, x);
    1472             : }
    1473             : 
    1474             : /* return a Flx */
    1475             : GEN
    1476        1657 : FlxX_resultant(GEN u, GEN v, ulong p, long sx)
    1477             : {
    1478        1657 :   pari_sp av = avma, av2;
    1479             :   long degq,dx,dy,du,dv,dr,signh;
    1480             :   GEN z,g,h,r,p1;
    1481             : 
    1482        1657 :   dx=degpol(u); dy=degpol(v); signh=1;
    1483        1657 :   if (dx < dy)
    1484             :   {
    1485           7 :     swap(u,v); lswap(dx,dy);
    1486           7 :     if (both_odd(dx, dy)) signh = -signh;
    1487             :   }
    1488        1657 :   if (dy < 0) return zero_Flx(sx);
    1489        1657 :   if (dy==0) return gerepileupto(av, Flx_powu(gel(v,2),dx,p));
    1490             : 
    1491        1657 :   g = h = pol1_Flx(sx); av2 = avma;
    1492             :   for(;;)
    1493             :   {
    1494        8223 :     r = FlxX_pseudorem(u,v,p); dr = lg(r);
    1495        4941 :     if (dr == 2) { set_avma(av); return zero_Flx(sx); }
    1496        4941 :     du = degpol(u); dv = degpol(v); degq = du-dv;
    1497        4940 :     u = v; p1 = g; g = leading_coeff(u);
    1498        4943 :     switch(degq)
    1499             :     {
    1500           0 :       case 0: break;
    1501             :       case 1:
    1502        3630 :         p1 = Flx_mul(h,p1, p); h = g; break;
    1503             :       default:
    1504        1313 :         p1 = Flx_mul(Flx_powu(h,degq,p), p1, p);
    1505        1311 :         h = Flx_div(Flx_powu(g,degq,p), Flx_powu(h,degq-1,p), p);
    1506             :     }
    1507        4936 :     if (both_odd(du,dv)) signh = -signh;
    1508        4935 :     v = FlxY_Flx_div(r, p1, p);
    1509        4940 :     if (dr==3) break;
    1510        3283 :     if (gc_needed(av2,1))
    1511             :     {
    1512           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_resultant, dr = %ld",dr);
    1513           0 :       gerepileall(av2,4, &u, &v, &g, &h);
    1514             :     }
    1515             :   }
    1516        1657 :   z = gel(v,2);
    1517        1657 :   if (dv > 1) z = Flx_div(Flx_powu(z,dv,p), Flx_powu(h,dv-1,p), p);
    1518        1657 :   if (signh < 0) z = Flx_neg(z,p);
    1519        1657 :   return gerepileupto(av, z);
    1520             : }
    1521             : 
    1522             : /* Warning:
    1523             :  * This function switches between valid and invalid variable ordering*/
    1524             : 
    1525             : static GEN
    1526        4414 : FlxY_to_FlyX(GEN b, long sv)
    1527             : {
    1528        4414 :   long i, n=-1;
    1529        4414 :   long sw = b[1]&VARNBITS;
    1530        4414 :   for(i=2;i<lg(b);i++) n = maxss(n,lgpol(gel(b,i)));
    1531        4413 :   return Flm_to_FlxX(Flm_transpose(FlxX_to_Flm(b,n)),sv,sw);
    1532             : }
    1533             : 
    1534             : /* Return a Fly*/
    1535             : GEN
    1536        4414 : Flx_FlxY_resultant(GEN a, GEN b, ulong pp)
    1537             : {
    1538        4414 :   pari_sp ltop=avma;
    1539        4414 :   long dres = degpol(a)*degpol(b);
    1540        4414 :   long sx=a[1], sy=b[1]&VARNBITS;
    1541             :   GEN z;
    1542        4414 :   b = FlxY_to_FlyX(b,sx);
    1543        4413 :   if ((ulong)dres >= pp)
    1544        1656 :     z = FlxX_resultant(Fly_to_FlxY(a, sy), b, pp, sx);
    1545             :   else
    1546        2757 :     z = Flx_FlxY_resultant_polint(a, b, pp, (ulong)dres, sy);
    1547        4413 :   return gerepileupto(ltop,z);
    1548             : }
    1549             : 
    1550             : /* return a t_POL (in variable v >= 0) whose coeffs are the coeffs of b,
    1551             :  * in variable v. This is an incorrect PARI object if initially varn(b) << v.
    1552             :  * We could return a vector of coeffs, but it is convenient to have degpol()
    1553             :  * and friends available. Even in that case, it will behave nicely with all
    1554             :  * functions treating a polynomial as a vector of coeffs (eg poleval).
    1555             :  * FOR INTERNAL USE! */
    1556             : GEN
    1557        9814 : swap_vars(GEN b0, long v)
    1558             : {
    1559        9814 :   long i, n = RgX_degree(b0, v);
    1560             :   GEN b, x;
    1561        9814 :   if (n < 0) return pol_0(v);
    1562        9814 :   b = cgetg(n+3, t_POL); x = b + 2;
    1563        9814 :   b[1] = evalsigne(1) | evalvarn(v);
    1564        9814 :   for (i=0; i<=n; i++) gel(x,i) = polcoef_i(b0, i, v);
    1565        9814 :   return b;
    1566             : }
    1567             : 
    1568             : /* assume varn(b) << varn(a) */
    1569             : /* return a FpY*/
    1570             : GEN
    1571           1 : FpX_FpXY_resultant(GEN a, GEN b, GEN p)
    1572             : {
    1573           1 :   long i,n,dres, db, vY = varn(b), vX = varn(a);
    1574             :   GEN la,x,y;
    1575             : 
    1576           1 :   if (lgefint(p) == 3)
    1577             :   {
    1578           0 :     ulong pp = uel(p,2);
    1579           0 :     b = ZXX_to_FlxX(b, pp, vX);
    1580           0 :     a = ZX_to_Flx(a, pp);
    1581           0 :     x = Flx_FlxY_resultant(a, b, pp);
    1582           0 :     return Flx_to_ZX(x);
    1583             :   }
    1584           1 :   db = RgXY_degreex(b);
    1585           1 :   dres = degpol(a)*degpol(b);
    1586           1 :   la = leading_coeff(a);
    1587           1 :   x = cgetg(dres+2, t_VEC);
    1588           1 :   y = cgetg(dres+2, t_VEC);
    1589             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1590             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1591           3 :   for (i=0,n = 1; i < dres; n++)
    1592             :   {
    1593           2 :     gel(x,++i) = utoipos(n);
    1594           2 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1595           2 :     gel(x,++i) = subiu(p,n);
    1596           2 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1597             :   }
    1598           1 :   if (i == dres)
    1599             :   {
    1600           0 :     gel(x,++i) = gen_0;
    1601           0 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b, gel(x,i), p,la,db,vY);
    1602             :   }
    1603           1 :   return FpV_polint(x,y, p, vY);
    1604             : }
    1605             : 
    1606             : static GEN
    1607          30 : FpX_composedsum(GEN P, GEN Q, GEN p)
    1608             : {
    1609          30 :   long n = 1+ degpol(P)*degpol(Q);
    1610          30 :   GEN Pl = FpX_invLaplace(FpX_Newton(P,n,p), p);
    1611          30 :   GEN Ql = FpX_invLaplace(FpX_Newton(Q,n,p), p);
    1612          30 :   GEN L = FpX_Laplace(FpXn_mul(Pl, Ql, n, p), p);
    1613          30 :   GEN lead = Fp_mul(Fp_powu(leading_coeff(P),degpol(Q), p),
    1614          30 :                     Fp_powu(leading_coeff(Q),degpol(P), p), p);
    1615          30 :   GEN R = FpX_fromNewton(L, p);
    1616          30 :   return FpX_Fp_mul(R, lead, p);
    1617             : }
    1618             : 
    1619             : #if 0
    1620             : GEN
    1621             : FpX_composedprod(GEN P, GEN Q, GEN p)
    1622             : {
    1623             :   long n = 1+ degpol(P)*degpol(Q);
    1624             :   GEN L=FpX_convol(FpX_Newton(P,n,p), FpX_Newton(Q,n,p), p);
    1625             :   return FpX_fromNewton(L, p);
    1626             : }
    1627             : #endif
    1628             : 
    1629             : GEN
    1630          30 : FpX_direct_compositum(GEN a, GEN b, GEN p)
    1631             : {
    1632          30 :   if (lgefint(p)==3)
    1633             :   {
    1634           0 :     pari_sp av = avma;
    1635           0 :     ulong pp = p[2];
    1636           0 :     GEN z = Flx_direct_compositum(ZX_to_Flx(a, pp), ZX_to_Flx(b, pp), pp);
    1637           0 :     return gerepileupto(av, Flx_to_ZX(z));
    1638             :   }
    1639          30 :   return FpX_composedsum(a, b, p);
    1640             : }
    1641             : 
    1642             : static GEN
    1643          30 : _FpX_direct_compositum(void *E, GEN a, GEN b)
    1644          30 : { return FpX_direct_compositum(a,b, (GEN)E); }
    1645             : 
    1646             : GEN
    1647         497 : FpXV_direct_compositum(GEN V, GEN p)
    1648             : {
    1649         497 :   if (lgefint(p)==3)
    1650             :   {
    1651           0 :     ulong pp = p[2];
    1652           0 :     return Flx_to_ZX(FlxV_direct_compositum(ZXV_to_FlxV(V, pp), pp));
    1653             :   }
    1654         497 :   return gen_product(V, (void *)p, &_FpX_direct_compositum);
    1655             : }
    1656             : 
    1657             : /* 0, 1, -1, 2, -2, ... */
    1658             : #define next_lambda(a) (a>0 ? -a : 1-a)
    1659             : GEN
    1660           0 : FpX_compositum(GEN a, GEN b, GEN p)
    1661             : {
    1662           0 :   long k, v = fetch_var_higher();
    1663           0 :   for (k = 1;; k = next_lambda(k))
    1664           0 :   {
    1665           0 :     GEN x = deg1pol_shallow(gen_1, gmulsg(k, pol_x(v)), 0); /* x + k y */
    1666           0 :     GEN C = FpX_FpXY_resultant(a, poleval(b,x),p);
    1667           0 :     if (FpX_is_squarefree(C, p)) { (void)delete_var(); return C; }
    1668             :   }
    1669             : }
    1670             : 
    1671             : /* Assume A in Z[Y], B in Q[Y][X], B squarefree in (Q[Y]/(A))[X] and
    1672             :  * Res_Y(A, B) in Z[X]. Find a small lambda (start from *lambda, use
    1673             :  * next_lambda successively) such that C(X) = Res_Y(A(Y), B(X + lambda Y))
    1674             :  * is squarefree, reset *lambda to the chosen value and return C. Set LERS to
    1675             :  * the Last non-constant polynomial in the Euclidean Remainder Sequence */
    1676             : static GEN
    1677        2926 : ZX_ZXY_resultant_LERS(GEN A, GEN B0, long *plambda, GEN *LERS)
    1678             : {
    1679             :   ulong bound, dp;
    1680        2926 :   pari_sp av = avma, av2 = 0;
    1681        2926 :   long lambda = *plambda, degA = degpol(A), dres = degA*degpol(B0);
    1682             :   long stable, checksqfree, i,n, cnt, degB;
    1683        2926 :   long v, vX = varn(B0), vY = varn(A); /* vY < vX */
    1684             :   GEN x, y, dglist, B, q, a, b, ev, H, H0, H1, Hp, H0p, H1p, C0, C1;
    1685             :   forprime_t S;
    1686             : 
    1687        2926 :   if (degA == 1)
    1688             :   {
    1689         616 :     GEN a1 = gel(A,3), a0 = gel(A,2);
    1690         616 :     B = lambda? RgX_translate(B0, monomial(stoi(lambda), 1, vY)): B0;
    1691         616 :     H = gsubst(B, vY, gdiv(gneg(a0),a1));
    1692         616 :    if (!equali1(a1)) H = RgX_Rg_mul(H, powiu(a1, poldegree(B,vY)));
    1693         616 :     *LERS = mkvec2(scalarpol_shallow(a0,vX), scalarpol_shallow(a1,vX));
    1694         616 :     gerepileall(av, 2, &H, LERS);
    1695         616 :     return H;
    1696             :   }
    1697             : 
    1698        2310 :   dglist = Hp = H0p = H1p = C0 = C1 = NULL; /* gcc -Wall */
    1699        2310 :   C0 = cgetg(dres+2, t_VECSMALL);
    1700        2310 :   C1 = cgetg(dres+2, t_VECSMALL);
    1701        2310 :   dglist = cgetg(dres+1, t_VECSMALL);
    1702        2310 :   x = cgetg(dres+2, t_VECSMALL);
    1703        2310 :   y = cgetg(dres+2, t_VECSMALL);
    1704        2310 :   B0 = leafcopy(B0);
    1705        2310 :   A = leafcopy(A);
    1706        2310 :   B = B0;
    1707        2310 :   v = fetch_var_higher(); setvarn(A,v);
    1708             :   /* make sure p large enough */
    1709             : INIT:
    1710             :   /* always except the first time */
    1711        2891 :   if (av2) { set_avma(av2); lambda = next_lambda(lambda); }
    1712        2891 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    1713        2891 :   B = swap_vars(B, vY); setvarn(B,v);
    1714             :   /* B0(lambda v + x, v) */
    1715        2891 :   if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    1716        2891 :   av2 = avma;
    1717             : 
    1718        2891 :   if (degA <= 3)
    1719             :   { /* sub-resultant faster for small degrees */
    1720        2331 :     H = RgX_resultant_all(A,B,&q);
    1721        2331 :     if (typ(q) != t_POL || degpol(q)!=1) goto INIT;
    1722        1876 :     H0 = gel(q,2);
    1723        1876 :     if (typ(H0) == t_POL) setvarn(H0,vX); else H0 = scalarpol(H0,vX);
    1724        1876 :     H1 = gel(q,3);
    1725        1876 :     if (typ(H1) == t_POL) setvarn(H1,vX); else H1 = scalarpol(H1,vX);
    1726        1876 :     if (!ZX_is_squarefree(H)) goto INIT;
    1727        1834 :     goto END;
    1728             :   }
    1729             : 
    1730         560 :   H = H0 = H1 = NULL;
    1731         560 :   degB = degpol(B);
    1732         560 :   bound = ZX_ZXY_ResBound(A, B, NULL);
    1733         560 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    1734         560 :   dp = 1;
    1735         560 :   init_modular_big(&S);
    1736         560 :   for(cnt = 0, checksqfree = 1;;)
    1737        1167 :   {
    1738        1727 :     ulong p = u_forprime_next(&S);
    1739             :     GEN Hi;
    1740        1727 :     a = ZX_to_Flx(A, p);
    1741        1727 :     b = ZXX_to_FlxX(B, p, varn(A));
    1742        1727 :     if (degpol(a) < degA || degpol(b) < degB) continue; /* p | lc(A)lc(B) */
    1743        1727 :     if (checksqfree)
    1744             :     { /* find degree list for generic Euclidean Remainder Sequence */
    1745         560 :       long goal = minss(degpol(a), degpol(b)); /* longest possible */
    1746         560 :       for (n=1; n <= goal; n++) dglist[n] = 0;
    1747         560 :       setlg(dglist, 1);
    1748        2597 :       for (n=0; n <= dres; n++)
    1749             :       {
    1750        2457 :         ev = FlxY_evalx_drop(b, n, p);
    1751        2457 :         Flx_resultant_set_dglist(a, ev, dglist, p);
    1752        2457 :         if (lg(dglist)-1 == goal) break;
    1753             :       }
    1754             :       /* last pol in ERS has degree > 1 ? */
    1755         560 :       goal = lg(dglist)-1;
    1756         560 :       if (degpol(B) == 1) { if (!goal) goto INIT; }
    1757             :       else
    1758             :       {
    1759         553 :         if (goal <= 1) goto INIT;
    1760         511 :         if (dglist[goal] != 0 || dglist[goal-1] != 1) goto INIT;
    1761             :       }
    1762         518 :       if (DEBUGLEVEL>4)
    1763           0 :         err_printf("Degree list for ERS (trials: %ld) = %Ps\n",n+1,dglist);
    1764             :     }
    1765             : 
    1766       33613 :     for (i=0,n = 0; i <= dres; n++)
    1767             :     {
    1768       31928 :       ev = FlxY_evalx_drop(b, n, p);
    1769       31928 :       x[++i] = n; y[i] = Flx_resultant_all(a, ev, C0+i, C1+i, dglist, p);
    1770       31928 :       if (!C1[i]) i--; /* C1(i) = 0. No way to recover C0(i) */
    1771             :     }
    1772        1685 :     Hi = Flv_Flm_polint(x, mkvec3(y,C0,C1), p, 0);
    1773        1685 :     Hp = gel(Hi,1); H0p = gel(Hi,2); H1p = gel(Hi,3);
    1774        1685 :     if (!H && degpol(Hp) != dres) continue;
    1775        1685 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1776        1685 :     if (checksqfree) {
    1777         518 :       if (!Flx_is_squarefree(Hp, p)) goto INIT;
    1778         476 :       if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    1779         476 :       checksqfree = 0;
    1780             :     }
    1781             : 
    1782        1643 :     if (!H)
    1783             :     { /* initialize */
    1784         476 :       q = utoipos(p); stable = 0;
    1785         476 :       H = ZX_init_CRT(Hp, p,vX);
    1786         476 :       H0= ZX_init_CRT(H0p, p,vX);
    1787         476 :       H1= ZX_init_CRT(H1p, p,vX);
    1788             :     }
    1789             :     else
    1790             :     {
    1791        1167 :       GEN qp = muliu(q,p);
    1792        2334 :       stable  = ZX_incremental_CRT_raw(&H, Hp, q,qp, p)
    1793        1167 :               & ZX_incremental_CRT_raw(&H0,H0p, q,qp, p)
    1794        1167 :               & ZX_incremental_CRT_raw(&H1,H1p, q,qp, p);
    1795        1167 :       q = qp;
    1796             :     }
    1797             :     /* could make it probabilistic for H ? [e.g if stable twice, etc]
    1798             :      * Probabilistic anyway for H0, H1 */
    1799        1643 :     if (DEBUGLEVEL>5 && (stable ||  ++cnt==100))
    1800           0 :     { cnt=0; err_printf("%ld%%%s ",100*expi(q)/bound,stable?"s":""); }
    1801        1643 :     if (stable && (ulong)expi(q) >= bound) break; /* DONE */
    1802        1167 :     if (gc_needed(av,2))
    1803             :     {
    1804           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_rnfequation");
    1805           0 :       gerepileall(av2, 4, &H, &q, &H0, &H1);
    1806             :     }
    1807             :   }
    1808             : END:
    1809        2310 :   if (DEBUGLEVEL>5) err_printf(" done\n");
    1810        2310 :   setvarn(H, vX); (void)delete_var();
    1811        2310 :   *LERS = mkvec2(H0,H1);
    1812        2310 :   gerepileall(av, 2, &H, LERS);
    1813        2310 :   *plambda = lambda; return H;
    1814             : }
    1815             : 
    1816             : GEN
    1817        3675 : ZX_ZXY_resultant_all(GEN A, GEN B, long *plambda, GEN *LERS)
    1818             : {
    1819        3675 :   if (LERS)
    1820             :   {
    1821        2926 :     if (!plambda)
    1822           0 :       pari_err_BUG("ZX_ZXY_resultant_all [LERS != NULL needs lambda]");
    1823        2926 :     return ZX_ZXY_resultant_LERS(A, B, plambda, LERS);
    1824             :   }
    1825         749 :   return ZX_ZXY_rnfequation(A, B, plambda);
    1826             : }
    1827             : 
    1828             : /* If lambda = NULL, return caract(Mod(A, T)), T,A in Z[X].
    1829             :  * Otherwise find a small lambda such that caract (Mod(A + lambda X, T)) is
    1830             :  * squarefree */
    1831             : GEN
    1832        1904 : ZXQ_charpoly_sqf(GEN A, GEN T, long *lambda, long v)
    1833             : {
    1834        1904 :   pari_sp av = avma;
    1835             :   GEN R, a;
    1836             :   long dA;
    1837             :   int delvar;
    1838             : 
    1839        1904 :   if (v < 0) v = 0;
    1840        1904 :   switch (typ(A))
    1841             :   {
    1842        1904 :     case t_POL: dA = degpol(A); if (dA > 0) break;
    1843           0 :       A = constant_coeff(A);
    1844             :     default:
    1845           0 :       if (lambda) { A = scalar_ZX_shallow(A,varn(T)); dA = 0; break;}
    1846           0 :       return gerepileupto(av, gpowgs(gsub(pol_x(v), A), degpol(T)));
    1847             :   }
    1848        1904 :   delvar = 0;
    1849        1904 :   if (varn(T) == 0)
    1850             :   {
    1851        1820 :     long v0 = fetch_var(); delvar = 1;
    1852        1820 :     T = leafcopy(T); setvarn(T,v0);
    1853        1820 :     A = leafcopy(A); setvarn(A,v0);
    1854             :   }
    1855        1904 :   R = ZX_ZXY_rnfequation(T, deg1pol_shallow(gen_1, gneg_i(A), 0), lambda);
    1856        1904 :   if (delvar) (void)delete_var();
    1857        1904 :   setvarn(R, v); a = leading_coeff(T);
    1858        1904 :   if (!gequal1(a)) R = gdiv(R, powiu(a, dA));
    1859        1904 :   return gerepileupto(av, R);
    1860             : }
    1861             : 
    1862             : 
    1863             : /* charpoly(Mod(A,T)), A may be in Q[X], but assume T and result are integral */
    1864             : GEN
    1865       13899 : ZXQ_charpoly(GEN A, GEN T, long v)
    1866             : {
    1867       13899 :   return (degpol(T) < 16) ? RgXQ_charpoly(A,T,v): ZXQ_charpoly_sqf(A,T, NULL, v);
    1868             : }
    1869             : 
    1870             : GEN
    1871         812 : QXQ_charpoly(GEN A, GEN T, long v)
    1872             : {
    1873         812 :   pari_sp av = avma;
    1874         812 :   GEN den, B = Q_remove_denom(A, &den);
    1875         812 :   GEN P = ZXQ_charpoly(B, T, v);
    1876         812 :   return gerepilecopy(av, den ? RgX_rescale(P, ginv(den)): P);
    1877             : }
    1878             : 
    1879             : static ulong
    1880     1527578 : ZX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, ulong p)
    1881             : {
    1882     1527578 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    1883             :   ulong H, dp;
    1884     1527529 :   if (dropa && dropb) return 0; /* p | lc(A), p | lc(B) */
    1885     1527529 :   H = Flx_resultant(a, b, p);
    1886     1527159 :   if (dropa)
    1887             :   { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    1888           0 :     ulong c = b[degB+2]; /* lc(B) */
    1889           0 :     if (odd(degB)) c = p - c;
    1890           0 :     c = Fl_powu(c, dropa, p);
    1891           0 :     if (c != 1) H = Fl_mul(H, c, p);
    1892             :   }
    1893     1527159 :   else if (dropb)
    1894             :   { /* multiply by lc(A)^(deg B - deg b) */
    1895           0 :     ulong c = a[degA+2]; /* lc(A) */
    1896           0 :     c = Fl_powu(c, dropb, p);
    1897           0 :     if (c != 1) H = Fl_mul(H, c, p);
    1898             :   }
    1899     1527155 :   dp = dB ? umodiu(dB, p): 1;
    1900     1527153 :   if (dp != 1) H = Fl_mul(H, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1901     1527153 :   return H;
    1902             : }
    1903             : 
    1904             : /* If B=NULL, assume B=A' */
    1905             : static GEN
    1906      727622 : ZX_resultant_slice(GEN A, GEN B, GEN dB, GEN P, GEN *mod)
    1907             : {
    1908      727622 :   pari_sp av = avma, av2;
    1909      727622 :   long degA, degB, i, n = lg(P)-1;
    1910             :   GEN H, T;
    1911             : 
    1912      727622 :   degA = degpol(A);
    1913      727615 :   degB = B? degpol(B): degA - 1;
    1914      727621 :   if (n == 1)
    1915             :   {
    1916      308013 :     ulong Hp, p = uel(P,1);
    1917      308013 :     GEN a = ZX_to_Flx(A, p), b = B? ZX_to_Flx(B, p): Flx_deriv(a, p);
    1918      308022 :     Hp = ZX_resultant_prime(a, b, dB, degA, degB, p);
    1919      307987 :     set_avma(av); *mod = utoipos(p); return utoi(Hp);
    1920             :   }
    1921      419608 :   T = ZV_producttree(P);
    1922      419606 :   A = ZX_nv_mod_tree(A, P, T);
    1923      419592 :   if (B) B = ZX_nv_mod_tree(B, P, T);
    1924      419592 :   H = cgetg(n+1, t_VECSMALL); av2 = avma;
    1925     1638779 :   for(i=1; i <= n; i++, set_avma(av2))
    1926             :   {
    1927     1219276 :     ulong p = P[i];
    1928     1219276 :     GEN a = gel(A,i), b = B? gel(B,i): Flx_deriv(a, p);
    1929     1219575 :     H[i] = ZX_resultant_prime(a, b, dB, degA, degB, p);
    1930             :   }
    1931      419586 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    1932      419603 :   *mod = gmael(T, lg(T)-1, 1);
    1933      419603 :   gerepileall(av, 2, &H, mod);
    1934      419606 :   return H;
    1935             : }
    1936             : 
    1937             : GEN
    1938      727621 : ZX_resultant_worker(GEN P, GEN A, GEN B, GEN dB)
    1939             : {
    1940      727621 :   GEN V = cgetg(3, t_VEC);
    1941      727618 :   if (typ(B) == t_INT) B = NULL;
    1942      727618 :   if (!signe(dB)) dB = NULL;
    1943      727618 :   gel(V,1) = ZX_resultant_slice(A, B, dB, P, &gel(V,2));
    1944      727585 :   return V;
    1945             : }
    1946             : 
    1947             : /* Compute Res(A, B/dB) in Z, assuming A,B in Z[X], dB in Z or NULL (= 1)
    1948             :  * If B=NULL, take B = A' and assume deg A > 1 and 'bound' is set */
    1949             : GEN
    1950       95933 : ZX_resultant_all(GEN A, GEN B, GEN dB, ulong bound)
    1951             : {
    1952       95933 :   pari_sp av = avma;
    1953             :   forprime_t S;
    1954             :   long m;
    1955             :   GEN  H, worker;
    1956       95933 :   if (B)
    1957             :   {
    1958       34790 :     long a = degpol(A), b = degpol(B);
    1959       34790 :     if (a < 0 || b < 0) return gen_0;
    1960       34760 :     if (!a) return powiu(gel(A,2), b);
    1961       34760 :     if (!b) return powiu(gel(B,2), a);
    1962       33610 :     if (!bound) bound = ZX_ZXY_ResBound(A, B, dB);
    1963             :   }
    1964       94753 :   worker = snm_closure(is_entry("_ZX_resultant_worker"),
    1965             :                        mkvec3(A, B? B: gen_0, dB? dB: gen_0));
    1966       94753 :   m = degpol(A)+(B ? degpol(B): 0);
    1967       94753 :   init_modular_big(&S);
    1968       94753 :   H = gen_crt("ZX_resultant_all", worker, &S, dB, bound, m, NULL,
    1969             :               ZV_chinese_center, Fp_center);
    1970       94753 :   return gerepileuptoint(av, H);
    1971             : }
    1972             : 
    1973             : /* A0 and B0 in Q[X] */
    1974             : GEN
    1975       12641 : QX_resultant(GEN A0, GEN B0)
    1976             : {
    1977             :   GEN s, a, b, A, B;
    1978       12641 :   pari_sp av = avma;
    1979             : 
    1980       12641 :   A = Q_primitive_part(A0, &a);
    1981       12641 :   B = Q_primitive_part(B0, &b);
    1982       12641 :   s = ZX_resultant(A, B);
    1983       12641 :   if (!signe(s)) { set_avma(av); return gen_0; }
    1984       12641 :   if (a) s = gmul(s, gpowgs(a,degpol(B)));
    1985       12641 :   if (b) s = gmul(s, gpowgs(b,degpol(A)));
    1986       12641 :   return gerepileupto(av, s);
    1987             : }
    1988             : 
    1989             : GEN
    1990       34065 : ZX_resultant(GEN A, GEN B) { return ZX_resultant_all(A,B,NULL,0); }
    1991             : 
    1992             : GEN
    1993           0 : QXQ_intnorm(GEN A, GEN B)
    1994             : {
    1995             :   GEN c, n, R, lB;
    1996           0 :   long dA = degpol(A), dB = degpol(B);
    1997           0 :   pari_sp av = avma;
    1998           0 :   if (dA < 0) return gen_0;
    1999           0 :   A = Q_primitive_part(A, &c);
    2000           0 :   if (!c || typ(c) == t_INT) {
    2001           0 :     n = c;
    2002           0 :     R = ZX_resultant(B, A);
    2003             :   } else {
    2004           0 :     n = gel(c,1);
    2005           0 :     R = ZX_resultant_all(B, A, gel(c,2), 0);
    2006             :   }
    2007           0 :   if (n && !equali1(n)) R = mulii(R, powiu(n, dB));
    2008           0 :   lB = leading_coeff(B);
    2009           0 :   if (!equali1(lB)) R = diviiexact(R, powiu(lB, dA));
    2010           0 :   return gerepileuptoint(av, R);
    2011             : }
    2012             : 
    2013             : GEN
    2014           0 : QXQ_norm(GEN A, GEN B)
    2015             : {
    2016             :   GEN c, R, lB;
    2017           0 :   long dA = degpol(A), dB = degpol(B);
    2018           0 :   pari_sp av = avma;
    2019           0 :   if (dA < 0) return gen_0;
    2020           0 :   A = Q_primitive_part(A, &c);
    2021           0 :   R = ZX_resultant(B, A);
    2022           0 :   if (c) R = gmul(R, gpowgs(c, dB));
    2023           0 :   lB = leading_coeff(B);
    2024           0 :   if (!equali1(lB)) R = gdiv(R, gpowgs(lB, dA));
    2025           0 :   return gerepileupto(av, R);
    2026             : }
    2027             : 
    2028             : /* assume x has integral coefficients */
    2029             : GEN
    2030       62942 : ZX_disc_all(GEN x, ulong bound)
    2031             : {
    2032       62942 :   pari_sp av = avma;
    2033       62942 :   long s, d = degpol(x);
    2034             :   GEN l, R;
    2035             : 
    2036       62941 :   if (d <= 1) return d == 1? gen_1: gen_0;
    2037       61142 :   s = (d & 2) ? -1: 1;
    2038       61142 :   l = leading_coeff(x);
    2039       61142 :   if (!bound) bound = ZX_discbound(x);
    2040       61143 :   R = ZX_resultant_all(x, NULL, NULL, bound);
    2041       61143 :   if (is_pm1(l))
    2042       58266 :   { if (signe(l) < 0) s = -s; }
    2043             :   else
    2044        2877 :     R = diviiexact(R,l);
    2045       61143 :   if (s == -1) togglesign_safe(&R);
    2046       61143 :   return gerepileuptoint(av,R);
    2047             : }
    2048             : 
    2049             : GEN
    2050       59652 : ZX_disc(GEN x) { return ZX_disc_all(x,0); }
    2051             : 
    2052             : GEN
    2053          21 : QX_disc(GEN x)
    2054             : {
    2055          21 :   pari_sp av = avma;
    2056          21 :   GEN c, d = ZX_disc( Q_primitive_part(x, &c) );
    2057          21 :   if (c) d = gmul(d, gpowgs(c, 2*degpol(x) - 2));
    2058          21 :   return gerepileupto(av, d);
    2059             : }
    2060             : 
    2061             : GEN
    2062       43797 : QXQ_mul(GEN x, GEN y, GEN T)
    2063             : {
    2064       43797 :   GEN dx, nx = Q_primitive_part(x, &dx);
    2065       43797 :   GEN dy, ny = Q_primitive_part(y, &dy);
    2066       43797 :   GEN z = ZXQ_mul(nx, ny, T);
    2067       43797 :   if (dx || dy)
    2068             :   {
    2069       43797 :     GEN d = dx ? dy ? gmul(dx, dy): dx : dy;
    2070       43797 :     if (!gequal1(d)) z = ZX_Q_mul(z, d);
    2071             :   }
    2072       43797 :   return z;
    2073             : }
    2074             : 
    2075             : GEN
    2076       11423 : QXQ_sqr(GEN x, GEN T)
    2077             : {
    2078       11423 :   GEN dx, nx = Q_primitive_part(x, &dx);
    2079       11423 :   GEN z = ZXQ_sqr(nx, T);
    2080       11423 :   if (dx)
    2081       11423 :     z = ZX_Q_mul(z, gsqr(dx));
    2082       11423 :   return z;
    2083             : }
    2084             : 
    2085             : static GEN
    2086       57307 : QXQ_inv_slice(GEN A, GEN B, GEN P, GEN *mod)
    2087             : {
    2088       57307 :   pari_sp av = avma;
    2089       57307 :   long i, n = lg(P)-1, v = varn(A), redo = 0;
    2090             :   GEN H, T;
    2091       57307 :   if (n == 1)
    2092             :   {
    2093       51810 :     ulong p = uel(P,1);
    2094       51810 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    2095       51811 :     GEN U = Flxq_invsafe(a, b, p);
    2096       51811 :     if (!U)
    2097             :     {
    2098          96 :       set_avma(av);
    2099          96 :       *mod = gen_1; return pol_0(v);
    2100             :     }
    2101       51715 :     H = gerepilecopy(av, Flx_to_ZX(U));
    2102       51714 :     *mod = utoi(p);
    2103       51714 :     return H;
    2104             :   }
    2105        5497 :   T = ZV_producttree(P);
    2106        5497 :   A = ZX_nv_mod_tree(A, P, T);
    2107        5497 :   B = ZX_nv_mod_tree(B, P, T);
    2108        5496 :   H = cgetg(n+1, t_VEC);
    2109       41997 :   for(i=1; i <= n; i++)
    2110             :   {
    2111       36501 :     ulong p = P[i];
    2112       36501 :     GEN a = gel(A,i), b = gel(B,i);
    2113       36501 :     GEN U = Flxq_invsafe(a, b, p);
    2114       36499 :     if (!U)
    2115             :     {
    2116         527 :       gel(H,i) = pol_0(v);
    2117         524 :       P[i] = 1; redo = 1;
    2118             :     }
    2119             :     else
    2120       35972 :       gel(H,i) = U;
    2121             :   }
    2122        5496 :   if (redo) T = ZV_producttree(P);
    2123        5496 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2124        5497 :   *mod = gmael(T, lg(T)-1, 1);
    2125        5497 :   gerepileall(av, 2, &H, mod);
    2126        5497 :   return H;
    2127             : }
    2128             : 
    2129             : GEN
    2130       57307 : QXQ_inv_worker(GEN P, GEN A, GEN B)
    2131             : {
    2132       57307 :   GEN V = cgetg(3, t_VEC);
    2133       57307 :   gel(V,1) = QXQ_inv_slice(A, B, P, &gel(V,2));
    2134       57307 :   return V;
    2135             : }
    2136             : 
    2137             : /* lift(1 / Mod(A,B)). B a ZX, A a scalar or a QX */
    2138             : GEN
    2139       26968 : QXQ_inv(GEN A, GEN B)
    2140             : {
    2141             :   GEN D, Ap, Bp;
    2142             :   ulong pp;
    2143       26968 :   pari_sp av2, av = avma;
    2144             :   forprime_t S;
    2145       26968 :   GEN worker, U, H = NULL, mod = gen_1;
    2146             :   pari_timer ti;
    2147             :   long k, dA, dB;
    2148       26968 :   if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
    2149             :   /* A a QX, B a ZX */
    2150       26968 :   A = Q_primitive_part(A, &D);
    2151       26968 :   dA = degpol(A); dB= degpol(B);
    2152             :   /* A, B in Z[X] */
    2153       26968 :   init_modular_small(&S);
    2154             :   do {
    2155       26968 :     pp = u_forprime_next(&S);
    2156       26968 :     Ap = ZX_to_Flx(A, pp);
    2157       26968 :     Bp = ZX_to_Flx(B, pp);
    2158       26968 :   } while (degpol(Ap) != dA || degpol(Bp) != dB);
    2159       26968 :   if (degpol(Flx_gcd(Ap, Bp, pp)) != 0 && degpol(ZX_gcd(A,B))!=0)
    2160          14 :     pari_err_INV("QXQ_inv",mkpolmod(A,B));
    2161       26954 :   worker = snm_closure(is_entry("_QXQ_inv_worker"), mkvec2(A, B));
    2162       26954 :   av2 = avma;
    2163       40683 :   for (k = 1; ;k *= 2)
    2164       13729 :   {
    2165             :     GEN res, b, N, den;
    2166       40683 :     gen_inccrt_i("QXQ_inv", worker, NULL, (k+1)>>1, dB, &S, &H, &mod,
    2167             :                  nxV_chinese_center, FpX_center);
    2168       40683 :     gerepileall(av2, 2, &H, &mod);
    2169       40683 :     b = sqrti(shifti(mod,-1));
    2170       40683 :     if (DEBUGLEVEL>5) timer_start(&ti);
    2171       40683 :     U = FpX_ratlift(H, mod, b, b, NULL);
    2172       40683 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: ratlift");
    2173       54412 :     if (!U) continue;
    2174       28378 :     N = Q_remove_denom(U, &den); if (!den) den = gen_1;
    2175       28378 :     res = Flx_rem(Flx_Fl_sub(Flx_mul(Ap, ZX_to_Flx(N,pp), pp),
    2176             :                   umodiu(den, pp), pp), Bp, pp);
    2177       28378 :     if (degpol(res) >= 0) continue;
    2178       26954 :     res = ZX_Z_sub(ZX_mul(A, N), den);
    2179       26954 :     res = ZX_is_monic(B) ? ZX_rem(res, B): RgX_pseudorem(res, B);
    2180       26954 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: final check");
    2181       26954 :     if (degpol(res)<0)
    2182             :     {
    2183       26954 :       if (D) U = RgX_Rg_div(U, D);
    2184       26954 :       return gerepilecopy(av, U);
    2185             :     }
    2186             :   }
    2187             : }
    2188             : 
    2189             : static GEN
    2190       10181 : QXQ_div_slice(GEN A, GEN B, GEN C, GEN P, GEN *mod)
    2191             : {
    2192       10181 :   pari_sp av = avma;
    2193       10181 :   long i, n = lg(P)-1, v = varn(A), redo = 0;
    2194             :   GEN H, T;
    2195       10181 :   if (n == 1)
    2196             :   {
    2197        9761 :     ulong p = uel(P,1);
    2198        9761 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p), c = ZX_to_Flx(C, p);
    2199        9761 :     GEN bi = Flxq_invsafe(b, c, p), U;
    2200        9761 :     if (!bi)
    2201             :     {
    2202           0 :       set_avma(av);
    2203           0 :       *mod = gen_1; return pol_0(v);
    2204             :     }
    2205        9761 :     U = Flxq_mul(a, bi, c, p);
    2206        9761 :     H = gerepilecopy(av, Flx_to_ZX(U));
    2207        9761 :     *mod = utoi(p);
    2208        9761 :     return H;
    2209             :   }
    2210         420 :   T = ZV_producttree(P);
    2211         420 :   A = ZX_nv_mod_tree(A, P, T);
    2212         420 :   B = ZX_nv_mod_tree(B, P, T);
    2213         420 :   C = ZX_nv_mod_tree(C, P, T);
    2214         420 :   H = cgetg(n+1, t_VEC);
    2215        1356 :   for(i=1; i <= n; i++)
    2216             :   {
    2217         936 :     ulong p = P[i];
    2218         936 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C, i);
    2219         936 :     GEN bi = Flxq_invsafe(b, c, p);
    2220         936 :     if (!bi)
    2221             :     {
    2222           0 :       gel(H,i) = pol_0(v);
    2223           0 :       P[i] = 1; redo = 1;
    2224             :     }
    2225             :     else
    2226         936 :       gel(H,i) = Flxq_mul(a, bi, c, p);
    2227             :   }
    2228         420 :   if (redo) T = ZV_producttree(P);
    2229         420 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2230         420 :   *mod = gmael(T, lg(T)-1, 1);
    2231         420 :   gerepileall(av, 2, &H, mod);
    2232         420 :   return H;
    2233             : }
    2234             : 
    2235             : GEN
    2236       10181 : QXQ_div_worker(GEN P, GEN A, GEN B, GEN C)
    2237             : {
    2238       10181 :   GEN V = cgetg(3, t_VEC);
    2239       10181 :   gel(V,1) = QXQ_div_slice(A, B, C, P, &gel(V,2));
    2240       10181 :   return V;
    2241             : }
    2242             : 
    2243             : /* lift(Mod(A/B, C)). C a ZX, A, B a scalar or a QX */
    2244             : GEN
    2245        2604 : QXQ_div(GEN A, GEN B, GEN C)
    2246             : {
    2247             :   GEN DA, DB, Ap, Bp, Cp;
    2248             :   ulong pp;
    2249        2604 :   pari_sp av2, av = avma;
    2250             :   forprime_t S;
    2251        2604 :   GEN worker, U, H = NULL, mod = gen_1;
    2252             :   pari_timer ti;
    2253             :   long k, dA, dB, dC;
    2254        2604 :   if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
    2255             :   /* A a QX, B a ZX */
    2256        2604 :   A = Q_primitive_part(A, &DA);
    2257        2604 :   B = Q_primitive_part(B, &DB);
    2258        2604 :   dA = degpol(A); dB = degpol(B); dC = degpol(C);
    2259             :   /* A, B in Z[X] */
    2260        2604 :   init_modular_small(&S);
    2261             :   do {
    2262        2604 :     pp = u_forprime_next(&S);
    2263        2604 :     Ap = ZX_to_Flx(A, pp);
    2264        2604 :     Bp = ZX_to_Flx(B, pp);
    2265        2604 :     Cp = ZX_to_Flx(C, pp);
    2266        2604 :   } while (degpol(Ap) != dA || degpol(Bp) != dB || degpol(Cp) != dC);
    2267        2604 :   if (degpol(Flx_gcd(Bp, Cp, pp)) != 0 && degpol(ZX_gcd(B,C))!=0)
    2268           0 :     pari_err_INV("QXQ_div",mkpolmod(B,C));
    2269        2604 :   worker = snm_closure(is_entry("_QXQ_div_worker"), mkvec3(A, B, C));
    2270        2604 :   av2 = avma;
    2271        4920 :   for (k = 1; ;k *= 2)
    2272        2316 :   {
    2273             :     GEN res, b, N, den;
    2274        4920 :     gen_inccrt_i("QXQ_div", worker, NULL, (k+1)>>1, dC, &S, &H, &mod,
    2275             :                  nxV_chinese_center, FpX_center);
    2276        4920 :     gerepileall(av2, 2, &H, &mod);
    2277        4920 :     b = sqrti(shifti(mod,-1));
    2278        4920 :     if (DEBUGLEVEL>5) timer_start(&ti);
    2279        4920 :     U = FpX_ratlift(H, mod, b, b, NULL);
    2280        4920 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_div: ratlift");
    2281        7236 :     if (!U) continue;
    2282        2654 :     N = Q_remove_denom(U, &den); if (!den) den = gen_1;
    2283        2654 :     res = Flx_rem(Flx_sub(Flx_mul(Bp, ZX_to_Flx(N,pp), pp),
    2284             :                           Flx_Fl_mul(Ap, umodiu(den, pp), pp), pp), Cp, pp);
    2285        2654 :     if (degpol(res) >= 0) continue;
    2286        2604 :     res = ZX_sub(ZX_mul(B, N), ZX_Z_mul(A,den));
    2287        2604 :     res = ZX_is_monic(C) ? ZX_rem(res, C): RgX_pseudorem(res, C);
    2288        2604 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_div: final check");
    2289        2604 :     if (degpol(res)<0)
    2290             :     {
    2291        2604 :       if (DA && DB) U = RgX_Rg_mul(U, gdiv(DA,DB));
    2292        2030 :       else if (DA) U = RgX_Rg_mul(U, DA);
    2293        1533 :       else if (DB) U = RgX_Rg_div(U, DB);
    2294        2604 :       return gerepilecopy(av, U);
    2295             :     }
    2296             :   }
    2297             : }
    2298             : 
    2299             : /************************************************************************
    2300             :  *                                                                      *
    2301             :  *                           ZXQ_minpoly                                *
    2302             :  *                                                                      *
    2303             :  ************************************************************************/
    2304             : 
    2305             : static GEN
    2306       11252 : ZXQ_minpoly_slice(GEN A, GEN B, long d, GEN P, GEN *mod)
    2307             : {
    2308       11252 :   pari_sp av = avma;
    2309       11252 :   long i, n = lg(P)-1, v = evalvarn(varn(B));
    2310             :   GEN H, T;
    2311       11252 :   if (n == 1)
    2312             :   {
    2313        8927 :     ulong p = uel(P,1);
    2314        8927 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    2315        8927 :     GEN Hp = Flxq_minpoly(a, b, p);
    2316        8927 :     if (degpol(Hp) != d) { p = 1; Hp = pol0_Flx(v); }
    2317        8927 :     H = Flx_to_ZX(Hp);
    2318        8927 :     *mod = utoi(p);
    2319        8927 :     gerepileall(av, 2, &H, mod);
    2320        8927 :     return H;
    2321             :   }
    2322        2325 :   T = ZV_producttree(P);
    2323        2325 :   A = ZX_nv_mod_tree(A, P, T);
    2324        2325 :   B = ZX_nv_mod_tree(B, P, T);
    2325        2325 :   H = cgetg(n+1, t_VEC);
    2326        7402 :   for(i=1; i <= n; i++)
    2327             :   {
    2328        5077 :     ulong p = P[i];
    2329        5077 :     GEN a = gel(A,i), b = gel(B,i);
    2330        5077 :     GEN m = Flxq_minpoly(a, b, p);
    2331        5077 :     if (degpol(m) != d) { P[i] = 1; m = pol0_Flx(v); }
    2332        5077 :     gel(H, i) = m;
    2333             :   }
    2334        2325 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2335        2325 :   *mod = gmael(T, lg(T)-1, 1);
    2336        2325 :   gerepileall(av, 2, &H, mod);
    2337        2325 :   return H;
    2338             : }
    2339             : 
    2340             : GEN
    2341       11252 : ZXQ_minpoly_worker(GEN P, GEN A, GEN B, long d)
    2342             : {
    2343       11252 :   GEN V = cgetg(3, t_VEC);
    2344       11252 :   gel(V,1) = ZXQ_minpoly_slice(A, B, d, P, &gel(V,2));
    2345       11252 :   return V;
    2346             : }
    2347             : 
    2348             : GEN
    2349        1337 : ZXQ_minpoly(GEN A, GEN B, long d, ulong bound)
    2350             : {
    2351        1337 :   pari_sp av = avma;
    2352             :   GEN worker, H, dB;
    2353             :   forprime_t S;
    2354        1337 :   B = Q_remove_denom(B, &dB);
    2355        1337 :   worker = strtoclosure("_ZXQ_minpoly_worker", 3, A, B, stoi(d));
    2356        1337 :   init_modular_big(&S);
    2357        1337 :   H = gen_crt("ZXQ_minpoly", worker, &S, dB, bound, degpol(B), NULL,
    2358             :                nxV_chinese_center, FpX_center_i);
    2359        1337 :   return gerepilecopy(av, H);
    2360             : }
    2361             : 
    2362             : /************************************************************************
    2363             :  *                                                                      *
    2364             :  *                   ZX_ZXY_resultant                                   *
    2365             :  *                                                                      *
    2366             :  ************************************************************************/
    2367             : 
    2368             : static GEN
    2369       11848 : ZX_ZXY_resultant_prime(GEN a, GEN b, ulong dp, ulong p,
    2370             :                        long degA, long degB, long dres, long sX)
    2371             : {
    2372       11848 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    2373       11848 :   GEN Hp = Flx_FlxY_resultant_polint(a, b, p, dres, sX);
    2374       11848 :   if (dropa && dropb)
    2375           0 :     Hp = zero_Flx(sX);
    2376             :   else {
    2377       11848 :     if (dropa)
    2378             :     { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    2379           0 :       GEN c = gel(b,degB+2); /* lc(B) */
    2380           0 :       if (odd(degB)) c = Flx_neg(c, p);
    2381           0 :       if (!Flx_equal1(c)) {
    2382           0 :         c = Flx_powu(c, dropa, p);
    2383           0 :         if (!Flx_equal1(c)) Hp = Flx_mul(Hp, c, p);
    2384             :       }
    2385             :     }
    2386       11848 :     else if (dropb)
    2387             :     { /* multiply by lc(A)^(deg B - deg b) */
    2388           0 :       ulong c = uel(a, degA+2); /* lc(A) */
    2389           0 :       c = Fl_powu(c, dropb, p);
    2390           0 :       if (c != 1) Hp = Flx_Fl_mul(Hp, c, p);
    2391             :     }
    2392             :   }
    2393       11848 :   if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    2394       11848 :   return Hp;
    2395             : }
    2396             : 
    2397             : static GEN
    2398        8650 : ZX_ZXY_resultant_slice(GEN A, GEN B, GEN dB, long degA, long degB, long dres,
    2399             :                        GEN P, GEN *mod, long sX, long vY)
    2400             : {
    2401        8650 :   pari_sp av = avma;
    2402        8650 :   long i, n = lg(P)-1;
    2403             :   GEN H, T, D;
    2404        8650 :   if (n == 1)
    2405             :   {
    2406        8306 :     ulong p = uel(P,1);
    2407        8306 :     ulong dp = dB ? umodiu(dB, p): 1;
    2408        8306 :     GEN a = ZX_to_Flx(A, p), b = ZXX_to_FlxX(B, p, vY);
    2409        8306 :     GEN Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2410        8305 :     H = Flx_to_ZX(Hp);
    2411        8306 :     *mod = utoi(p);
    2412        8306 :     gerepileall(av, 2, &H, mod);
    2413        8306 :     return H;
    2414             :   }
    2415         344 :   T = ZV_producttree(P);
    2416         344 :   A = ZX_nv_mod_tree(A, P, T);
    2417         344 :   B = ZXX_nv_mod_tree(B, P, T, vY);
    2418         344 :   D = dB ? Z_ZV_mod_tree(dB, P, T): NULL;
    2419         344 :   H = cgetg(n+1, t_VEC);
    2420        1072 :   for(i=1; i <= n; i++)
    2421             :   {
    2422         728 :     ulong p = P[i];
    2423         728 :     GEN a = gel(A,i), b = gel(B,i);
    2424         728 :     ulong dp = D ? uel(D, i): 1;
    2425         728 :     gel(H,i) = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2426             :   }
    2427         344 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2428         344 :   *mod = gmael(T, lg(T)-1, 1);
    2429         344 :   gerepileall(av, 2, &H, mod);
    2430         344 :   return H;
    2431             : }
    2432             : 
    2433             : GEN
    2434        8650 : ZX_ZXY_resultant_worker(GEN P, GEN A, GEN B, GEN dB, GEN v)
    2435             : {
    2436        8650 :   GEN V = cgetg(3, t_VEC);
    2437        8650 :   if (isintzero(dB)) dB = NULL;
    2438        8650 :   gel(V,1) = ZX_ZXY_resultant_slice(A, B, dB, v[1], v[2], v[3], P, &gel(V,2), v[4], v[5]);
    2439        8650 :   return V;
    2440             : }
    2441             : 
    2442             : GEN
    2443        4074 : ZX_ZXY_resultant(GEN A, GEN B)
    2444             : {
    2445        4074 :   pari_sp av = avma;
    2446             :   forprime_t S;
    2447             :   ulong bound;
    2448        4074 :   long v = fetch_var_higher();
    2449        4074 :   long degA = degpol(A), degB, dres = degA * degpol(B);
    2450        4074 :   long vX = varn(B), vY = varn(A); /* assume vY has lower priority */
    2451        4074 :   long sX = evalvarn(vX);
    2452             :   GEN worker, H, dB;
    2453        4074 :   B = Q_remove_denom(B, &dB);
    2454        4074 :   if (!dB) B = leafcopy(B);
    2455        4074 :   A = leafcopy(A); setvarn(A,v);
    2456        4074 :   B = swap_vars(B, vY); setvarn(B,v); degB = degpol(B);
    2457        4074 :   bound = ZX_ZXY_ResBound(A, B, dB);
    2458        4074 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    2459        8148 :   worker = snm_closure(is_entry("_ZX_ZXY_resultant_worker"),
    2460        4074 :                        mkvec4(A, B, dB? dB: gen_0,
    2461             :                               mkvecsmall5(degA, degB,dres, vY, sX)));
    2462        4074 :   init_modular_big(&S);
    2463        4074 :   H = gen_crt("ZX_ZXY_resultant_all", worker, &S, dB, bound, degpol(A)+degpol(B), NULL,
    2464             :                nxV_chinese_center, FpX_center_i);
    2465        4074 :   setvarn(H, vX); (void)delete_var();
    2466        4074 :   return gerepilecopy(av, H);
    2467             : }
    2468             : 
    2469             : static long
    2470        2254 : ZX_ZXY_rnfequation_lambda(GEN A, GEN B0, long lambda)
    2471             : {
    2472        2254 :   pari_sp av = avma;
    2473        2254 :   long degA = degpol(A), degB, dres = degA*degpol(B0);
    2474        2254 :   long v = fetch_var_higher();
    2475        2254 :   long vX = varn(B0), vY = varn(A); /* assume vY has lower priority */
    2476        2254 :   long sX = evalvarn(vX);
    2477             :   GEN dB, B, a, b, Hp;
    2478             :   forprime_t S;
    2479             : 
    2480        2254 :   B0 = Q_remove_denom(B0, &dB);
    2481        2254 :   if (!dB) B0 = leafcopy(B0);
    2482        2254 :   A = leafcopy(A);
    2483        2254 :   B = B0;
    2484        2254 :   setvarn(A,v);
    2485             : INIT:
    2486        2814 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    2487        2814 :   B = swap_vars(B, vY); setvarn(B,v);
    2488             :   /* B0(lambda v + x, v) */
    2489        2814 :   if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    2490             : 
    2491        2814 :   degB = degpol(B);
    2492        2814 :   init_modular_big(&S);
    2493             :   while (1)
    2494           0 :   {
    2495        2814 :     ulong p = u_forprime_next(&S);
    2496        2814 :     ulong dp = dB ? umodiu(dB, p): 1;
    2497        2814 :     if (!dp) continue;
    2498        2814 :     a = ZX_to_Flx(A, p);
    2499        2814 :     b = ZXX_to_FlxX(B, p, v);
    2500        2814 :     Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2501        2814 :     if (degpol(Hp) != dres) continue;
    2502        2814 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    2503        2814 :     if (!Flx_is_squarefree(Hp, p)) { lambda = next_lambda(lambda); goto INIT; }
    2504        2254 :     if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    2505        4508 :     (void)delete_var(); return gc_long(av,lambda);
    2506             :   }
    2507             : }
    2508             : 
    2509             : GEN
    2510        2870 : ZX_ZXY_rnfequation(GEN A, GEN B, long *lambda)
    2511             : {
    2512        2870 :   if (lambda)
    2513             :   {
    2514        2254 :     *lambda = ZX_ZXY_rnfequation_lambda(A, B, *lambda);
    2515        2254 :     B = RgX_translate(B, monomial(stoi(*lambda), 1, varn(A)));
    2516             :   }
    2517        2870 :   return ZX_ZXY_resultant(A,B);
    2518             : }
    2519             : 
    2520             : static GEN
    2521         845 : ZX_direct_compositum_slice(GEN A, GEN B, GEN P, GEN *mod)
    2522             : {
    2523         845 :   pari_sp av = avma;
    2524         845 :   long i, n = lg(P)-1;
    2525             :   GEN H, T;
    2526         845 :   if (n == 1)
    2527             :   {
    2528         754 :     ulong p = uel(P,1);
    2529         754 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    2530         754 :     GEN Hp = Flx_direct_compositum(a, b, p);
    2531         754 :     H = gerepileupto(av, Flx_to_ZX(Hp));
    2532         754 :     *mod = utoi(p);
    2533         754 :     return H;
    2534             :   }
    2535          91 :   T = ZV_producttree(P);
    2536          91 :   A = ZX_nv_mod_tree(A, P, T);
    2537          91 :   B = ZX_nv_mod_tree(B, P, T);
    2538          91 :   H = cgetg(n+1, t_VEC);
    2539         510 :   for(i=1; i <= n; i++)
    2540             :   {
    2541         419 :     ulong p = P[i];
    2542         419 :     GEN a = gel(A,i), b = gel(B,i);
    2543         419 :     gel(H,i) = Flx_direct_compositum(a, b, p);
    2544             :   }
    2545          91 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2546          91 :   *mod = gmael(T, lg(T)-1, 1);
    2547          91 :   gerepileall(av, 2, &H, mod);
    2548          91 :   return H;
    2549             : }
    2550             : 
    2551             : GEN
    2552         845 : ZX_direct_compositum_worker(GEN P, GEN A, GEN B)
    2553             : {
    2554         845 :   GEN V = cgetg(3, t_VEC);
    2555         845 :   gel(V,1) = ZX_direct_compositum_slice(A, B, P, &gel(V,2));
    2556         845 :   return V;
    2557             : }
    2558             : 
    2559             : /* Assume A,B irreducible (in particular squarefree) and define linearly
    2560             :  * disjoint extensions: no factorisation needed */
    2561             : static GEN
    2562         567 : ZX_direct_compositum(GEN A, GEN B, GEN lead)
    2563             : {
    2564         567 :   pari_sp av = avma;
    2565             :   forprime_t S;
    2566         567 :   long m = maxss(degpol(A),degpol(B));
    2567             :   ulong bound;
    2568             :   GEN H, worker, mod;
    2569         567 :   bound = ZX_ZXY_ResBound(A, poleval(B,deg1pol(gen_1,pol_x(1),0)), NULL);
    2570         567 :   worker = snm_closure(is_entry("_ZX_direct_compositum_worker"), mkvec2(A,B));
    2571         567 :   init_modular_big(&S);
    2572         567 :   H = gen_crt("ZX_direct_compositum", worker, &S, lead, bound, m, &mod,
    2573             :               nxV_chinese_center, FpX_center);
    2574         567 :   return gerepileupto(av, H);
    2575             : }
    2576             : 
    2577             : static long
    2578         182 : ZX_compositum_lambda(GEN A, GEN B, GEN lead, long lambda)
    2579             : {
    2580         182 :   pari_sp av = avma;
    2581             :   forprime_t S;
    2582             :   ulong p;
    2583         182 :   init_modular_big(&S);
    2584         182 :   p = u_forprime_next(&S);
    2585             :   while (1)
    2586          56 :   {
    2587             :     GEN Hp, a;
    2588         238 :     if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    2589         238 :     if (lead && dvdiu(lead,p)) { p = u_forprime_next(&S); continue; }
    2590         231 :     a = ZX_to_Flx(ZX_rescale(A, stoi(-lambda)), p);
    2591         231 :     Hp = Flx_direct_compositum(a, ZX_to_Flx(B, p), p);
    2592         231 :     if (!Flx_is_squarefree(Hp, p)) { lambda = next_lambda(lambda); continue; }
    2593         182 :     if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    2594         364 :     return gc_long(av, lambda);
    2595             :   }
    2596             : }
    2597             : 
    2598             : GEN
    2599         567 : ZX_compositum(GEN A, GEN B, long *lambda)
    2600             : {
    2601         567 :   GEN lead  = mulii(leading_coeff(A),leading_coeff(B));
    2602         567 :   if (lambda)
    2603             :   {
    2604         182 :     *lambda = ZX_compositum_lambda(A, B, lead, *lambda);
    2605         182 :     A = ZX_rescale(A, stoi(-*lambda));
    2606             :   }
    2607         567 :   return ZX_direct_compositum(A, B, lead);
    2608             : }
    2609             : 
    2610             : GEN
    2611         385 : ZX_compositum_disjoint(GEN A, GEN B)
    2612         385 : { return ZX_compositum(A, B, NULL); }
    2613             : 
    2614             : static GEN
    2615         358 : ZXQX_direct_compositum_slice(GEN A, GEN B, GEN C, GEN P, GEN *mod)
    2616             : {
    2617         358 :   pari_sp av = avma;
    2618         358 :   long i, n = lg(P)-1, dC = degpol(C), v = varn(C);
    2619             :   GEN H, T;
    2620         358 :   if (n == 1)
    2621             :   {
    2622         347 :     ulong p = uel(P,1);
    2623         347 :     GEN a = ZXX_to_FlxX(A, p, v), b = ZXX_to_FlxX(B, p, v);
    2624         347 :     GEN c = ZX_to_Flx(C, p);
    2625         347 :     GEN Hp = FlxX_to_Flm(FlxqX_direct_compositum(a, b, c, p), dC);
    2626         347 :     H = gerepileupto(av, Flm_to_ZM(Hp));
    2627         347 :     *mod = utoi(p);
    2628         347 :     return H;
    2629             :   }
    2630          11 :   T = ZV_producttree(P);
    2631          11 :   A = ZXX_nv_mod_tree(A, P, T, v);
    2632          11 :   B = ZXX_nv_mod_tree(B, P, T, v);
    2633          11 :   C = ZX_nv_mod_tree(C, P, T);
    2634          11 :   H = cgetg(n+1, t_VEC);
    2635          33 :   for(i=1; i <= n; i++)
    2636             :   {
    2637          22 :     ulong p = P[i];
    2638          22 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C,i);
    2639          22 :     gel(H,i) = FlxX_to_Flm(FlxqX_direct_compositum(a, b, c, p), dC);
    2640             :   }
    2641          11 :   H = nmV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2642          11 :   *mod = gmael(T, lg(T)-1, 1);
    2643          11 :   gerepileall(av, 2, &H, mod);
    2644          11 :   return H;
    2645             : }
    2646             : 
    2647             : GEN
    2648         358 : ZXQX_direct_compositum_worker(GEN P, GEN A, GEN B, GEN C)
    2649             : {
    2650         358 :   GEN V = cgetg(3, t_VEC);
    2651         358 :   gel(V,1) = ZXQX_direct_compositum_slice(A, B, C, P, &gel(V,2));
    2652         358 :   return V;
    2653             : }
    2654             : 
    2655             : /* Interpolate at roots of 1 and use Hadamard bound for univariate resultant:
    2656             :  *   bound = N_2(A)^degpol B N_2(B)^degpol(A),  where
    2657             :  *     N_2(A) = sqrt(sum (N_1(Ai))^2)
    2658             :  * Return e such that Res(A, B) < 2^e */
    2659             : static GEN
    2660         469 : RgX_RgXY_ResBound(GEN A, GEN B)
    2661             : {
    2662         469 :   pari_sp av = avma, av2;
    2663         469 :   GEN a = gen_0, b = gen_0, bnd;
    2664         469 :   long i , lA = lg(A), lB = lg(B);
    2665        2128 :   for (i=2; i<lA; i++)
    2666             :   {
    2667        1659 :     a = gadd(a, gnorm(gel(A,i)));
    2668        1659 :     if (gc_needed(av,1))
    2669             :     {
    2670           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_RgXY_ResBound i = %ld",i);
    2671           0 :       a = gerepileupto(av, a);
    2672             :     }
    2673             :   }
    2674         469 :   av2 = avma;
    2675        2219 :   for (i=2; i<lB; i++)
    2676             :   {
    2677        1750 :     GEN t = gel(B,i);
    2678        1750 :     if (typ(t) == t_POL) t = gnorml1(t, DEFAULTPREC);
    2679        1750 :     b = gadd(b, gsqr(t));
    2680        1750 :     if (gc_needed(av,1))
    2681             :     {
    2682           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_RgXY_ResBound i = %ld",i);
    2683           0 :       b = gerepileupto(av2, b);
    2684             :     }
    2685             :   }
    2686         469 :   bnd = gsqrt(gmul(gpowgs(a, degpol(B)), gpowgs(b, degpol(A))), DEFAULTPREC);
    2687         469 :   return gerepileupto(av, bnd);
    2688             : }
    2689             : 
    2690             : static GEN
    2691         217 : ZXQX_direct_compositum(GEN A, GEN B, GEN T, ulong bound)
    2692             : {
    2693         217 :   pari_sp av = avma;
    2694             :   forprime_t S;
    2695         217 :   long m = maxss(degpol(A),degpol(B));
    2696             :   GEN H, worker, mod;
    2697         217 :   GEN lead = mulii(Q_content(leading_coeff(A)), Q_content(leading_coeff(B)));
    2698         217 :   worker = snm_closure(is_entry("_ZXQX_direct_compositum_worker")
    2699             :                       , mkvec3(A,B,T));
    2700         217 :   init_modular_big(&S);
    2701         217 :   H = gen_crt("ZXQX_direct_compositum", worker, &S, lead, bound, m, &mod,
    2702             :               nmV_chinese_center, FpM_center);
    2703         217 :   if (DEBUGLEVEL > 4)
    2704           0 :     err_printf("nfcompositum: a priori bound: %lu, a posteriori: %lu\n",
    2705             :                bound, expi(gsupnorm(H, DEFAULTPREC)));
    2706         217 :   return gerepilecopy(av, RgM_to_RgXX(H, varn(A), varn(T)));
    2707             : }
    2708             : 
    2709             : static GEN
    2710         217 : L2_bound(GEN nf, GEN den)
    2711             : {
    2712         217 :   GEN M, L, prep, T = nf_get_pol(nf), tozk = nf_get_invzk(nf);
    2713         217 :   long prec = ZM_max_lg(tozk) + ZX_max_lg(T) + nbits2prec(degpol(T));
    2714         217 :   (void)initgaloisborne(nf, den? den: gen_1, prec, &L, &prep, NULL);
    2715         217 :   M = vandermondeinverse(L, RgX_gtofp(T,prec), den, prep);
    2716         217 :   return RgM_fpnorml2(RgM_mul(tozk,M), DEFAULTPREC);
    2717             : }
    2718             : 
    2719             : static long
    2720         217 : ZXQX_direct_compositum_bound(GEN nf, GEN A, GEN B)
    2721             : {
    2722         217 :   pari_sp av = avma;
    2723         217 :   GEN M = L2_bound(nf, NULL);
    2724         217 :   GEN r = nf_get_roots(nf);
    2725         217 :   long v = nf_get_varn(nf), i, l = lg(r);
    2726         217 :   GEN a = cgetg(l, t_COL);
    2727         686 :   for (i = 1; i < l; i++)
    2728         938 :     gel(a, i) =  RgX_RgXY_ResBound(gsubst(A, v, gel(r,i)),
    2729         469 :                  poleval(gsubst(B, v, gel(r,i)),
    2730             :                          deg1pol(gen_1, pol_x(1), 0)));
    2731         217 :   return gc_long(av, (long) dbllog2(gmul(M,RgC_fpnorml2(a, DEFAULTPREC))));
    2732             : }
    2733             : 
    2734             : GEN
    2735         217 : nf_direct_compositum(GEN nf, GEN A, GEN B)
    2736             : {
    2737         217 :   ulong bnd = ZXQX_direct_compositum_bound(nf, A, B);
    2738         217 :   return ZXQX_direct_compositum(A, B, nf_get_pol(nf), bnd);
    2739             : }
    2740             : 
    2741             : /************************************************************************
    2742             :  *                                                                      *
    2743             :  *                   IRREDUCIBLE POLYNOMIAL / Fp                        *
    2744             :  *                                                                      *
    2745             :  ************************************************************************/
    2746             : 
    2747             : /* irreducible (unitary) polynomial of degree n over Fp */
    2748             : GEN
    2749           0 : ffinit_rand(GEN p,long n)
    2750             : {
    2751           0 :   for(;;) {
    2752           0 :     pari_sp av = avma;
    2753           0 :     GEN pol = ZX_add(pol_xn(n, 0), random_FpX(n-1,0, p));
    2754           0 :     if (FpX_is_irred(pol, p)) return pol;
    2755           0 :     set_avma(av);
    2756             :   }
    2757             : }
    2758             : 
    2759             : /* return an extension of degree 2^l of F_2, assume l > 0
    2760             :  * Not stack clean. */
    2761             : static GEN
    2762         393 : ffinit_Artin_Schreier_2(long l)
    2763             : {
    2764             :   GEN Q, T, S;
    2765             :   long i, v;
    2766             : 
    2767         393 :   if (l == 1) return mkvecsmall4(0,1,1,1); /*x^2 + x + 1*/
    2768         358 :   v = fetch_var_higher();
    2769         358 :   S = mkvecsmall5(0, 0, 0, 1, 1); /* y(y^2 + y) */
    2770         358 :   Q = mkpoln(3, pol1_Flx(0), pol1_Flx(0), S); /* x^2 + x + y(y^2+y) */
    2771         358 :   setvarn(Q, v);
    2772             : 
    2773             :   /* x^4+x+1, irred over F_2, minimal polynomial of a root of Q */
    2774         358 :   T = mkvecsmalln(6,evalvarn(v),1UL,1UL,0UL,0UL,1UL);
    2775             :   /* Q = x^2 + x + a(y) irred. over K = F2[y] / (T(y))
    2776             :    * ==> x^2 + x + a(y) b irred. over K for any root b of Q
    2777             :    * ==> x^2 + x + (b^2+b)b */
    2778         358 :   for (i=2; i<l; i++) T = Flx_FlxY_resultant(T, Q, 2); /* minpoly(b) / F2*/
    2779         358 :   (void)delete_var(); T[1] = 0; return T;
    2780             : }
    2781             : 
    2782             : /* return an extension of degree p^l of F_p, assume l > 0
    2783             :  * Not stack clean. */
    2784             : GEN
    2785         582 : ffinit_Artin_Schreier(ulong p, long l)
    2786             : {
    2787             :   long i, v;
    2788             :   GEN Q, R, S, T, xp;
    2789         582 :   if (p==2) return ffinit_Artin_Schreier_2(l);
    2790         189 :   xp = polxn_Flx(p,0); /* x^p */
    2791         189 :   T = Flx_sub(xp, mkvecsmall3(0,1,1),p); /* x^p - x - 1 */
    2792         189 :   if (l == 1) return T;
    2793             : 
    2794           7 :   v = evalvarn(fetch_var_higher());
    2795           7 :   xp[1] = v;
    2796           7 :   R = Flx_sub(polxn_Flx(2*p-1,0), polxn_Flx(p,0),p);
    2797           7 :   S = Flx_sub(xp, polx_Flx(0), p);
    2798           7 :   Q = FlxX_Flx_sub(Flx_to_FlxX(S, v), R, p); /* x^p - x - (y^(2p-1)-y^p) */
    2799           7 :   for (i = 2; i <= l; ++i) T = Flx_FlxY_resultant(T, Q, p);
    2800           7 :   (void)delete_var(); T[1] = 0; return T;
    2801             : }
    2802             : 
    2803             : static long
    2804       20612 : flinit_check(ulong p, long n, long l)
    2805             : {
    2806             :   ulong q;
    2807       20612 :   if (!uisprime(n)) return 0;
    2808       12848 :   q = p % n; if (!q) return 0;
    2809       10769 :   return ugcd((n-1)/Fl_order(q, n-1, n), l) == 1;
    2810             : }
    2811             : 
    2812             : static GEN
    2813        5024 : flinit(ulong p, long l)
    2814             : {
    2815        5024 :   ulong n = 1+l;
    2816        5024 :   while (!flinit_check(p,n,l)) n += l;
    2817        5024 :   if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
    2818        5024 :   return ZX_to_Flx(polsubcyclo(n,l,0), p);
    2819             : }
    2820             : 
    2821             : static GEN
    2822        4978 : ffinit_fact_Flx(ulong p, long n)
    2823             : {
    2824        4978 :   GEN P, F = factoru_pow(n), Fp = gel(F,1), Fe = gel(F,2), Fm = gel(F,3);
    2825        4978 :   long i, l = lg(Fm);
    2826        4978 :   P = cgetg(l, t_VEC);
    2827       10584 :   for (i = 1; i < l; ++i)
    2828       11212 :     gel(P,i) = p==uel(Fp,i) ?
    2829         582 :                  ffinit_Artin_Schreier(uel(Fp,i), Fe[i])
    2830        6188 :                : flinit(p, uel(Fm,i));
    2831        4978 :   return FlxV_direct_compositum(P, p);
    2832             : }
    2833             : 
    2834             : static GEN
    2835        7043 : init_Flxq_i(ulong p, long n, long sv)
    2836             : {
    2837             :   GEN P;
    2838        7043 :   if (n == 1) return polx_Flx(sv);
    2839        7043 :   if (flinit_check(p, n+1, n))
    2840             :   {
    2841        2065 :     P = const_vecsmall(n+2,1);
    2842        2065 :     P[1] = sv; return P;
    2843             :   }
    2844        4978 :   P = ffinit_fact_Flx(p,n);
    2845        4978 :   P[1] = sv; return P;
    2846             : }
    2847             : 
    2848             : GEN
    2849           0 : init_Flxq(ulong p, long n, long v)
    2850             : {
    2851           0 :   pari_sp av = avma;
    2852           0 :   return gerepileupto(av, init_Flxq_i(p, n, v));
    2853             : }
    2854             : 
    2855             : /* check if polsubcyclo(n,l,0) is irreducible modulo p */
    2856             : static long
    2857        2188 : fpinit_check(GEN p, long n, long l)
    2858             : {
    2859             :   ulong q;
    2860        2188 :   if (!uisprime(n)) return 0;
    2861        1537 :   q = umodiu(p,n); if (!q) return 0;
    2862        1537 :   return ugcd((n-1)/Fl_order(q, n-1, n), l) == 1;
    2863             : }
    2864             : 
    2865             : /* let k=2 if p%4==1, and k=4 else and assume k*p does not divide l.
    2866             :  * Return an irreducible polynomial of degree l over F_p.
    2867             :  * Variant of Adleman and Lenstra "Finding irreducible polynomials over
    2868             :  * finite fields", ACM, 1986 (5) 350--355.
    2869             :  * Not stack clean */
    2870             : static GEN
    2871         527 : fpinit(GEN p, long l)
    2872             : {
    2873         527 :   ulong n = 1+l;
    2874         527 :   while (!fpinit_check(p,n,l)) n += l;
    2875         527 :   if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
    2876         527 :   return FpX_red(polsubcyclo(n,l,0),p);
    2877             : }
    2878             : 
    2879             : static GEN
    2880         497 : ffinit_fact(GEN p, long n)
    2881             : {
    2882         497 :   GEN P, F = factoru_pow(n), Fp = gel(F,1), Fe = gel(F,2), Fm = gel(F,3);
    2883         497 :   long i, l = lg(Fm);
    2884         497 :   P = cgetg(l, t_VEC);
    2885        1024 :   for (i = 1; i < l; ++i)
    2886        1054 :     gel(P,i) = absequaliu(p, Fp[i]) ?
    2887           0 :                  Flx_to_ZX(ffinit_Artin_Schreier(Fp[i], Fe[i]))
    2888         527 :                : fpinit(p, Fm[i]);
    2889         497 :   return FpXV_direct_compositum(P, p);
    2890             : }
    2891             : 
    2892             : static GEN
    2893        7883 : init_Fq_i(GEN p, long n, long v)
    2894             : {
    2895             :   GEN P;
    2896        7883 :   if (n <= 0) pari_err_DOMAIN("ffinit", "degree", "<=", gen_0, stoi(n));
    2897        7883 :   if (typ(p) != t_INT) pari_err_TYPE("ffinit",p);
    2898        7883 :   if (signe(p) <= 0) pari_err_PRIME("ffinit",p);
    2899        7883 :   if (v < 0) v = 0;
    2900        7883 :   if (n == 1) return pol_x(v);
    2901        7631 :   if (lgefint(p) == 3)
    2902        7043 :     return Flx_to_ZX(init_Flxq_i(p[2], n, evalvarn(v)));
    2903         588 :   if (fpinit_check(p, n+1, n)) return polcyclo(n+1, v);
    2904         497 :   P = ffinit_fact(p,n);
    2905         497 :   setvarn(P, v); return P;
    2906             : }
    2907             : GEN
    2908        7428 : init_Fq(GEN p, long n, long v)
    2909             : {
    2910        7428 :   pari_sp av = avma;
    2911        7428 :   return gerepileupto(av, init_Fq_i(p, n, v));
    2912             : }
    2913             : GEN
    2914         455 : ffinit(GEN p, long n, long v)
    2915             : {
    2916         455 :   pari_sp av = avma;
    2917         455 :   return gerepileupto(av, FpX_to_mod(init_Fq_i(p, n, v), p));
    2918             : }
    2919             : 
    2920             : GEN
    2921        3178 : ffnbirred(GEN p, long n)
    2922             : {
    2923        3178 :   pari_sp av = avma;
    2924        3178 :   GEN s = powiu(p,n), F = factoru(n), D = divisorsu_moebius(gel(F, 1));
    2925        3178 :   long j, l = lg(D);
    2926        6797 :   for (j = 2; j < l; j++) /* skip d = 1 */
    2927             :   {
    2928        3619 :     long md = D[j]; /* mu(d) * d, d squarefree */
    2929        3619 :     GEN pd = powiu(p, n / labs(md)); /* p^{n/d} */
    2930        3619 :     s = md > 0? addii(s, pd): subii(s,pd);
    2931             :   }
    2932        3178 :   return gerepileuptoint(av, diviuexact(s, n));
    2933             : }
    2934             : 
    2935             : GEN
    2936         427 : ffsumnbirred(GEN p, long n)
    2937             : {
    2938         427 :   pari_sp av = avma, av2;
    2939         427 :   GEN q, t = p, v = vecfactoru(1, n);
    2940             :   long i;
    2941         427 :   q = cgetg(n+1,t_VEC); gel(q,1) = p;
    2942         427 :   for (i=2; i<=n; i++) gel(q,i) = mulii(gel(q,i-1), p);
    2943         427 :   av2 = avma;
    2944        1575 :   for (i=2; i<=n; i++)
    2945             :   {
    2946        1148 :     GEN s = gel(q,i), F = gel(v,i), D = divisorsu_moebius(gel(F,1));
    2947        1148 :     long j, l = lg(D);
    2948        2534 :     for (j = 2; j < l; j++) /* skip 1 */
    2949             :     {
    2950        1386 :       long md = D[j];
    2951        1386 :       GEN pd = gel(q, i / labs(md)); /* p^{i/d} */
    2952        1386 :       s = md > 0? addii(s, pd): subii(s, pd);
    2953             :     }
    2954        1148 :     t = gerepileuptoint(av2, addii(t, diviuexact(s, i)));
    2955             :   }
    2956         427 :   return gerepileuptoint(av, t);
    2957             : }
    2958             : 
    2959             : GEN
    2960         140 : ffnbirred0(GEN p, long n, long flag)
    2961             : {
    2962         140 :   if (typ(p) != t_INT) pari_err_TYPE("ffnbirred", p);
    2963         140 :   if (n <= 0) pari_err_DOMAIN("ffnbirred", "degree", "<=", gen_0, stoi(n));
    2964         140 :   switch(flag)
    2965             :   {
    2966          70 :     case 0: return ffnbirred(p, n);
    2967          70 :     case 1: return ffsumnbirred(p, n);
    2968             :   }
    2969           0 :   pari_err_FLAG("ffnbirred");
    2970             :   return NULL; /* LCOV_EXCL_LINE */
    2971             : }
    2972             : 
    2973             : static void
    2974        2254 : checkmap(GEN m, const char *s)
    2975             : {
    2976        2254 :   if (typ(m)!=t_VEC || lg(m)!=3 || typ(gel(m,1))!=t_FFELT)
    2977           0 :     pari_err_TYPE(s,m);
    2978        2254 : }
    2979             : 
    2980             : GEN
    2981         182 : ffembed(GEN a, GEN b)
    2982             : {
    2983         182 :   pari_sp av = avma;
    2984         182 :   GEN p, Ta, Tb, g, r = NULL;
    2985         182 :   if (typ(a)!=t_FFELT) pari_err_TYPE("ffembed",a);
    2986         182 :   if (typ(b)!=t_FFELT) pari_err_TYPE("ffembed",b);
    2987         182 :   p = FF_p_i(a); g = FF_gen(a);
    2988         182 :   if (!equalii(p, FF_p_i(b))) pari_err_MODULUS("ffembed",a,b);
    2989         182 :   Ta = FF_mod(a);
    2990         182 :   Tb = FF_mod(b);
    2991         182 :   if (degpol(Tb)%degpol(Ta)!=0)
    2992           7 :     pari_err_DOMAIN("ffembed",GENtostr_raw(a),"is not a subfield of",b,a);
    2993         175 :   r = gel(FFX_roots(Ta, b), 1);
    2994         175 :   return gerepilecopy(av, mkvec2(g,r));
    2995             : }
    2996             : 
    2997             : GEN
    2998          91 : ffextend(GEN a, GEN P, long v)
    2999             : {
    3000          91 :   pari_sp av = avma;
    3001             :   long n;
    3002             :   GEN p, T, R, g, m;
    3003          91 :   if (typ(a)!=t_FFELT) pari_err_TYPE("ffextend",a);
    3004          91 :   T = a; p = FF_p_i(a);
    3005          91 :   if (typ(P)!=t_POL || !RgX_is_FpXQX(P,&T,&p)) pari_err_TYPE("ffextend", P);
    3006          49 :   if (!FF_samefield(a, T)) pari_err_MODULUS("ffextend",a,T);
    3007          49 :   if (v < 0) v = varn(P);
    3008          49 :   n = FF_f(T) * degpol(P); R = ffinit(p, n, v); g = ffgen(R, v);
    3009          49 :   m = ffembed(a, g);
    3010          49 :   R = FFX_roots(ffmap(m, P),g);
    3011          49 :   return gerepilecopy(av, mkvec2(gel(R,1), m));
    3012             : }
    3013             : 
    3014             : GEN
    3015          42 : fffrobenius(GEN a, long n)
    3016             : {
    3017          42 :   if (typ(a)!=t_FFELT) pari_err_TYPE("fffrobenius",a);
    3018          42 :   retmkvec2(FF_gen(a), FF_Frobenius(a, n));
    3019             : }
    3020             : 
    3021             : GEN
    3022         133 : ffinvmap(GEN m)
    3023             : {
    3024         133 :   pari_sp av = avma;
    3025             :   long i, l;
    3026         133 :   GEN T, F, a, g, r, f = NULL;
    3027         133 :   checkmap(m, "ffinvmap");
    3028         133 :   a = gel(m,1); r = gel(m,2);
    3029         133 :   if (typ(r) != t_FFELT)
    3030           7 :    pari_err_TYPE("ffinvmap", m);
    3031         126 :   g = FF_gen(a);
    3032         126 :   T = FF_mod(r);
    3033         126 :   F = gel(FFX_factor(T, a), 1);
    3034         126 :   l = lg(F);
    3035         490 :   for(i=1; i<l; i++)
    3036             :   {
    3037         490 :     GEN s = FFX_rem(FF_to_FpXQ_i(r), gel(F, i), a);
    3038         490 :     if (degpol(s)==0 && gequal(constant_coeff(s),g)) { f = gel(F, i); break; }
    3039             :   }
    3040         126 :   if (f==NULL) pari_err_TYPE("ffinvmap", m);
    3041         126 :   if (degpol(f)==1) f = FF_neg_i(gel(f,2));
    3042         126 :   return gerepilecopy(av, mkvec2(FF_gen(r),f));
    3043             : }
    3044             : 
    3045             : static GEN
    3046        1260 : ffpartmapimage(const char *s, GEN r)
    3047             : {
    3048        1260 :    GEN a = NULL, p = NULL;
    3049        1260 :    if (typ(r)==t_POL && degpol(r) >= 1
    3050        1260 :       && RgX_is_FpXQX(r,&a,&p) && a && typ(a)==t_FFELT) return a;
    3051           0 :    pari_err_TYPE(s, r);
    3052             :    return NULL; /* LCOV_EXCL_LINE */
    3053             : }
    3054             : 
    3055             : static GEN
    3056        2702 : ffeltmap_i(GEN m, GEN x)
    3057             : {
    3058        2702 :    GEN r = gel(m,2);
    3059        2702 :    if (!FF_samefield(x, gel(m,1)))
    3060          84 :      pari_err_DOMAIN("ffmap","m","domain does not contain", x, r);
    3061        2618 :    if (typ(r)==t_FFELT)
    3062        1652 :      return FF_map(r, x);
    3063             :    else
    3064         966 :      return FFX_preimage(x, r, ffpartmapimage("ffmap", r));
    3065             : }
    3066             : 
    3067             : static GEN
    3068        4452 : ffmap_i(GEN m, GEN x)
    3069             : {
    3070             :   GEN y;
    3071        4452 :   long i, lx, tx = typ(x);
    3072        4452 :   switch(tx)
    3073             :   {
    3074             :     case t_FFELT:
    3075        2534 :       return ffeltmap_i(m, x);
    3076             :     case t_POL: case t_RFRAC: case t_SER:
    3077             :     case t_VEC: case t_COL: case t_MAT:
    3078        1267 :       y = cgetg_copy(x, &lx);
    3079        1267 :       for (i=1; i<lontyp[tx]; i++) y[i] = x[1];
    3080        4564 :       for (i=lontyp[tx]; i<lx; i++)
    3081             :       {
    3082        3339 :         GEN yi = ffmap_i(m, gel(x,i));
    3083        3297 :         if (!yi) return NULL;
    3084        3297 :         gel(y,i) = yi;
    3085             :       }
    3086        1225 :       return y;
    3087             :   }
    3088         651 :   return gcopy(x);
    3089             : }
    3090             : 
    3091             : GEN
    3092        1029 : ffmap(GEN m, GEN x)
    3093             : {
    3094        1029 :   pari_sp ltop = avma;
    3095             :   GEN y;
    3096        1029 :   checkmap(m, "ffmap");
    3097        1029 :   y = ffmap_i(m, x);
    3098        1029 :   if (y) return y;
    3099          42 :   set_avma(ltop); return cgetg(1,t_VEC);
    3100             : }
    3101             : 
    3102             : static GEN
    3103         252 : ffeltmaprel_i(GEN m, GEN x)
    3104             : {
    3105         252 :    GEN g = gel(m,1), r = gel(m,2);
    3106         252 :    if (!FF_samefield(x, g))
    3107           0 :      pari_err_DOMAIN("ffmap","m","domain does not contain", x, r);
    3108         252 :    if (typ(r)==t_FFELT)
    3109          84 :      retmkpolmod(FF_map(r, x), pol_x(FF_var(g)));
    3110             :    else
    3111         168 :      retmkpolmod(FFX_preimagerel(x, r, ffpartmapimage("ffmap", r)), gcopy(r));
    3112             : }
    3113             : 
    3114             : static GEN
    3115         252 : ffmaprel_i(GEN m, GEN x)
    3116             : {
    3117             :   GEN y;
    3118         252 :   long i, lx, tx = typ(x);
    3119         252 :   switch(tx)
    3120             :   {
    3121             :     case t_FFELT:
    3122         252 :       return ffeltmaprel_i(m, x);
    3123             :     case t_POL: case t_RFRAC: case t_SER:
    3124             :     case t_VEC: case t_COL: case t_MAT:
    3125           0 :       y = cgetg_copy(x, &lx);
    3126           0 :       for (i=1; i<lontyp[tx]; i++) y[i] = x[1];
    3127           0 :       for (i=lontyp[tx]; i<lx; i++)
    3128           0 :         gel(y,i) = ffmaprel_i(m, gel(x,i));
    3129           0 :       return y;
    3130             :   }
    3131           0 :   return gcopy(x);
    3132             : }
    3133             : 
    3134             : GEN
    3135         252 : ffmaprel(GEN m, GEN x)
    3136             : {
    3137         252 :   checkmap(m, "ffmaprel");
    3138         252 :   return ffmaprel_i(m, x);
    3139             : }
    3140             : 
    3141             : static void
    3142          84 : err_compo(GEN m, GEN n)
    3143          84 : { pari_err_DOMAIN("ffcompomap","m","domain does not contain codomain of",n,m); }
    3144             : 
    3145             : GEN
    3146         420 : ffcompomap(GEN m, GEN n)
    3147             : {
    3148         420 :   pari_sp av = avma;
    3149         420 :   GEN g = gel(n,1), r, m2, n2;
    3150         420 :   checkmap(m, "ffcompomap");
    3151         420 :   checkmap(n, "ffcompomap");
    3152         420 :   m2 = gel(m,2); n2 = gel(n,2);
    3153         420 :   switch((typ(m2)==t_POL)|((typ(n2)==t_POL)<<1))
    3154             :   {
    3155             :     case 0:
    3156          84 :       if (!FF_samefield(gel(m,1),n2)) err_compo(m,n);
    3157          42 :       r = FF_map(gel(m,2), n2);
    3158          42 :       break;
    3159             :     case 2:
    3160          84 :       r = ffmap_i(m, n2);
    3161          42 :       if (lg(r) == 1) err_compo(m,n);
    3162          42 :       break;
    3163             :     case 1:
    3164         168 :       r = ffeltmap_i(m, n2);
    3165         126 :       if (!r)
    3166             :       {
    3167             :         GEN a, A, R, M;
    3168             :         long dm, dn;
    3169          42 :         a = ffpartmapimage("ffcompomap",m2);
    3170          42 :         A = FF_to_FpXQ_i(FF_neg(n2));
    3171          42 :         setvarn(A, 1);
    3172          42 :         R = deg1pol(gen_1, A, 0);
    3173          42 :         setvarn(R, 0);
    3174          42 :         M = gcopy(m2);
    3175          42 :         setvarn(M, 1);
    3176          42 :         r = polresultant0(R, M, 1, 0);
    3177          42 :         dm = FF_f(gel(m,1)); dn = FF_f(gel(n,1));
    3178          42 :         if (dm % dn || !FFX_ispower(r, dm/dn, a, &r)) err_compo(m,n);
    3179          42 :         setvarn(r, varn(FF_mod(g)));
    3180             :       }
    3181         126 :       break;
    3182             :     case 3:
    3183             :     {
    3184             :       GEN M, R, T, p, a;
    3185          84 :       a = ffpartmapimage("ffcompomap",n2);
    3186          84 :       if (!FF_samefield(a, gel(m,1))) err_compo(m,n);
    3187          42 :       p = FF_p_i(gel(n,1));
    3188          42 :       T = FF_mod(gel(n,1));
    3189          42 :       setvarn(T, 1);
    3190          42 :       R = RgX_to_FpXQX(n2,T,p);
    3191          42 :       setvarn(R, 0);
    3192          42 :       M = gcopy(m2);
    3193          42 :       setvarn(M, 1);
    3194          42 :       r = polresultant0(R, M, 1, 0);
    3195          42 :       setvarn(r, varn(n2));
    3196             :     }
    3197             :   }
    3198         252 :   return gerepilecopy(av, mkvec2(g,r));
    3199             : }

Generated by: LCOV version 1.13