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.18.0 lcov report (development 29804-254f602fce) Lines: 1761 1955 90.1 %
Date: 2024-12-18 09:08:59 Functions: 187 201 93.0 %
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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /***********************************************************************/
      16             : /**                                                                   **/
      17             : /**               ARITHMETIC OPERATIONS ON POLYNOMIALS                **/
      18             : /**                         (third part)                              **/
      19             : /**                                                                   **/
      20             : /***********************************************************************/
      21             : #include "pari.h"
      22             : #include "paripriv.h"
      23             : 
      24             : #define DEBUGLEVEL DEBUGLEVEL_pol
      25             : 
      26             : /************************************************************************
      27             :  **                                                                    **
      28             :  **                      Ring membership                               **
      29             :  **                                                                    **
      30             :  ************************************************************************/
      31             : struct charact {
      32             :   GEN q;
      33             :   int isprime;
      34             : };
      35             : static void
      36        1239 : char_update_prime(struct charact *S, GEN p)
      37             : {
      38        1239 :   if (!S->isprime) { S->isprime = 1; S->q = p; }
      39        1239 :   if (!equalii(p, S->q)) pari_err_MODULUS("characteristic", S->q, p);
      40        1232 : }
      41             : static void
      42        6573 : char_update_int(struct charact *S, GEN n)
      43             : {
      44        6573 :   if (S->isprime)
      45             :   {
      46           7 :     if (dvdii(n, S->q)) return;
      47           7 :     pari_err_MODULUS("characteristic", S->q, n);
      48             :   }
      49        6566 :   S->q = gcdii(S->q, n);
      50             : }
      51             : static void
      52      177772 : charact(struct charact *S, GEN x)
      53             : {
      54      177772 :   const long tx = typ(x);
      55             :   long i, l;
      56      177772 :   switch(tx)
      57             :   {
      58        5124 :     case t_INTMOD:char_update_int(S, gel(x,1)); break;
      59        1148 :     case t_FFELT: char_update_prime(S, gel(x,4)); break;
      60       26516 :     case t_COMPLEX: case t_QUAD:
      61             :     case t_POLMOD: case t_POL: case t_SER: case t_RFRAC:
      62             :     case t_VEC: case t_COL: case t_MAT:
      63       26516 :       l = lg(x);
      64      176757 :       for (i=lontyp[tx]; i < l; i++) charact(S,gel(x,i));
      65       26502 :       break;
      66           7 :     case t_LIST:
      67           7 :       x = list_data(x);
      68           7 :       if (x) charact(S, x);
      69           7 :       break;
      70             :   }
      71      177744 : }
      72             : static void
      73        4634 : charact_res(struct charact *S, GEN x)
      74             : {
      75        4634 :   const long tx = typ(x);
      76             :   long i, l;
      77        4634 :   switch(tx)
      78             :   {
      79        1449 :     case t_INTMOD:char_update_int(S, gel(x,1)); break;
      80           0 :     case t_FFELT: char_update_prime(S, gel(x,4)); break;
      81          91 :     case t_PADIC: char_update_prime(S, gel(x,2)); break;
      82        1617 :     case t_COMPLEX: case t_QUAD:
      83             :     case t_POLMOD: case t_POL: case t_SER: case t_RFRAC:
      84             :     case t_VEC: case t_COL: case t_MAT:
      85        1617 :       l = lg(x);
      86        5922 :       for (i=lontyp[tx]; i < l; i++) charact_res(S,gel(x,i));
      87        1617 :       break;
      88           0 :     case t_LIST:
      89           0 :       x = list_data(x);
      90           0 :       if (x) charact_res(S, x);
      91           0 :       break;
      92             :   }
      93        4634 : }
      94             : GEN
      95       27517 : characteristic(GEN x)
      96             : {
      97             :   struct charact S;
      98       27517 :   S.q = gen_0; S.isprime = 0;
      99       27517 :   charact(&S, x); return S.q;
     100             : }
     101             : GEN
     102         329 : residual_characteristic(GEN x)
     103             : {
     104             :   struct charact S;
     105         329 :   S.q = gen_0; S.isprime = 0;
     106         329 :   charact_res(&S, x); return S.q;
     107             : }
     108             : 
     109             : int
     110    71842468 : Rg_is_Fp(GEN x, GEN *pp)
     111             : {
     112             :   GEN mod;
     113    71842468 :   switch(typ(x))
     114             :   {
     115     3203536 :   case t_INTMOD:
     116     3203536 :     mod = gel(x,1);
     117     3203536 :     if (!*pp) *pp = mod;
     118     2954266 :     else if (mod != *pp && !equalii(mod, *pp))
     119             :     {
     120           0 :       if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_Fp");
     121           0 :       return 0;
     122             :     }
     123     3203536 :     return 1;
     124    57249223 :   case t_INT:
     125    57249223 :     return 1;
     126    11389709 :   default: return 0;
     127             :   }
     128             : }
     129             : 
     130             : int
     131    28375624 : RgX_is_FpX(GEN x, GEN *pp)
     132             : {
     133    28375624 :   long i, lx = lg(x);
     134    88802283 :   for (i=2; i<lx; i++)
     135    71816371 :     if (!Rg_is_Fp(gel(x, i), pp))
     136    11389715 :       return 0;
     137    16985912 :   return 1;
     138             : }
     139             : 
     140             : int
     141           0 : RgV_is_FpV(GEN x, GEN *pp)
     142             : {
     143           0 :   long i, lx = lg(x);
     144           0 :   for (i=1; i<lx; i++)
     145           0 :     if (!Rg_is_Fp(gel(x,i), pp)) return 0;
     146           0 :   return 1;
     147             : }
     148             : 
     149             : int
     150           0 : RgM_is_FpM(GEN x, GEN *pp)
     151             : {
     152           0 :   long i, lx = lg(x);
     153           0 :   for (i=1; i<lx; i++)
     154           0 :     if (!RgV_is_FpV(gel(x, i), pp)) return 0;
     155           0 :   return 1;
     156             : }
     157             : 
     158             : int
     159       60256 : Rg_is_FpXQ(GEN x, GEN *pT, GEN *pp)
     160             : {
     161             :   GEN pol, mod, p;
     162       60256 :   switch(typ(x))
     163             :   {
     164       26089 :   case t_INTMOD:
     165       26089 :     return Rg_is_Fp(x, pp);
     166        8057 :   case t_INT:
     167        8057 :     return 1;
     168          21 :   case t_POL:
     169          21 :     return RgX_is_FpX(x, pp);
     170       21350 :   case t_FFELT:
     171       21350 :     mod = x; p = FF_p_i(x);
     172       21350 :     if (!*pp) *pp = p;
     173       21350 :     if (!*pT) *pT = mod;
     174       19824 :     else if (typ(*pT)!=t_FFELT || !FF_samefield(*pT,mod))
     175             :     {
     176          42 :       if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_FpXQ");
     177          42 :       return 0;
     178             :     }
     179       21308 :     return 1;
     180        4585 :   case t_POLMOD:
     181        4585 :     mod = gel(x,1); pol = gel(x, 2);
     182        4585 :     if (!RgX_is_FpX(mod, pp)) return 0;
     183        4585 :     if (typ(pol)==t_POL)
     184             :     {
     185        4578 :       if (!RgX_is_FpX(pol, pp)) return 0;
     186             :     }
     187           7 :     else if (!Rg_is_Fp(pol, pp)) return 0;
     188        4585 :     if (!*pT) *pT = mod;
     189        4431 :     else if (mod != *pT && !gequal(mod, *pT))
     190             :     {
     191           0 :       if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_FpXQ");
     192           0 :       return 0;
     193             :     }
     194        4585 :     return 1;
     195             : 
     196         154 :   default: return 0;
     197             :   }
     198             : }
     199             : 
     200             : int
     201        3318 : RgX_is_FpXQX(GEN x, GEN *pT, GEN *pp)
     202             : {
     203        3318 :   long i, lx = lg(x);
     204       62818 :   for (i = 2; i < lx; i++)
     205       59598 :     if (!Rg_is_FpXQ(gel(x,i), pT, pp)) return 0;
     206        3220 :   return 1;
     207             : }
     208             : 
     209             : /************************************************************************
     210             :  **                                                                    **
     211             :  **                      Ring conversion                               **
     212             :  **                                                                    **
     213             :  ************************************************************************/
     214             : 
     215             : /* p > 0 a t_INT, return lift(x * Mod(1,p)).
     216             :  * If x is an INTMOD, assume modulus is a multiple of p. */
     217             : GEN
     218    52318824 : Rg_to_Fp(GEN x, GEN p)
     219             : {
     220    52318824 :   if (lgefint(p) == 3) return utoi(Rg_to_Fl(x, uel(p,2)));
     221     4555780 :   switch(typ(x))
     222             :   {
     223      288418 :     case t_INT: return modii(x, p);
     224       18790 :     case t_FRAC: {
     225       18790 :       pari_sp av = avma;
     226       18790 :       GEN z = modii(gel(x,1), p);
     227       18790 :       if (z == gen_0) return gen_0;
     228       18785 :       return gerepileuptoint(av, remii(mulii(z, Fp_inv(gel(x,2), p)), p));
     229             :     }
     230          70 :     case t_PADIC: return padic_to_Fp(x, p);
     231     4248510 :     case t_INTMOD: {
     232     4248510 :       GEN q = gel(x,1), a = gel(x,2);
     233     4248510 :       if (equalii(q, p)) return icopy(a);
     234          13 :       if (!dvdii(q,p)) pari_err_MODULUS("Rg_to_Fp", q, p);
     235           0 :       return remii(a, p);
     236             :     }
     237           0 :     default: pari_err_TYPE("Rg_to_Fp",x);
     238             :       return NULL; /* LCOV_EXCL_LINE */
     239             :   }
     240             : }
     241             : /* If x is a POLMOD, assume modulus is a multiple of T. */
     242             : GEN
     243     1291601 : Rg_to_FpXQ(GEN x, GEN T, GEN p)
     244             : {
     245     1291601 :   long ta, tx = typ(x), v = get_FpX_var(T);
     246             :   GEN a, b;
     247     1291601 :   if (is_const_t(tx))
     248             :   {
     249       58601 :     if (tx == t_FFELT)
     250             :     {
     251       17355 :       GEN z = FF_to_FpXQ(x);
     252       17355 :       setvarn(z, v);
     253       17355 :       return z;
     254             :     }
     255       41246 :     return scalar_ZX(degpol(T)? Rg_to_Fp(x, p): gen_0, v);
     256             :   }
     257     1233000 :   switch(tx)
     258             :   {
     259     1230893 :     case t_POLMOD:
     260     1230893 :       b = gel(x,1);
     261     1230893 :       a = gel(x,2); ta = typ(a);
     262     1230893 :       if (is_const_t(ta))
     263        4102 :         return scalar_ZX(degpol(T)? Rg_to_Fp(a, p): gen_0, v);
     264     1226791 :       b = RgX_to_FpX(b, p); if (varn(b) != v) break;
     265     1226791 :       a = RgX_to_FpX(a, p);
     266     1226791 :       if (ZX_equal(b,get_FpX_mod(T)) || signe(FpX_rem(b,T,p))==0)
     267     1226791 :         return FpX_rem(a, T, p);
     268           0 :       break;
     269        2107 :     case t_POL:
     270        2107 :       if (varn(x) != v) break;
     271        2100 :       return FpX_rem(RgX_to_FpX(x,p), T, p);
     272           0 :     case t_RFRAC:
     273           0 :       a = Rg_to_FpXQ(gel(x,1), T,p);
     274           0 :       b = Rg_to_FpXQ(gel(x,2), T,p);
     275           0 :       return FpXQ_div(a,b, T,p);
     276             :   }
     277           7 :   pari_err_TYPE("Rg_to_FpXQ",x);
     278             :   return NULL; /* LCOV_EXCL_LINE */
     279             : }
     280             : GEN
     281     3552238 : RgX_to_FpX(GEN x, GEN p)
     282             : {
     283             :   long i, l;
     284     3552238 :   GEN z = cgetg_copy(x, &l); z[1] = x[1];
     285    15794921 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     286     3552238 :   return FpX_renormalize(z, l);
     287             : }
     288             : 
     289             : GEN
     290         140 : RgV_to_FpV(GEN x, GEN p)
     291         483 : { pari_APPLY_type(t_VEC, Rg_to_Fp(gel(x,i), p)) }
     292             : 
     293             : GEN
     294     1639097 : RgC_to_FpC(GEN x, GEN p)
     295    27661615 : { pari_APPLY_type(t_COL, Rg_to_Fp(gel(x,i), p)) }
     296             : 
     297             : GEN
     298      203170 : RgM_to_FpM(GEN x, GEN p)
     299     1842225 : { pari_APPLY_same(RgC_to_FpC(gel(x,i), p)) }
     300             : 
     301             : GEN
     302      369001 : RgV_to_Flv(GEN x, ulong p)
     303     1676894 : { pari_APPLY_ulong(Rg_to_Fl(gel(x,i), p)) }
     304             : 
     305             : GEN
     306      118296 : RgM_to_Flm(GEN x, ulong p)
     307      422998 : { pari_APPLY_same(RgV_to_Flv(gel(x,i), p)) }
     308             : 
     309             : GEN
     310        5028 : RgX_to_FpXQX(GEN x, GEN T, GEN p)
     311             : {
     312        5028 :   long i, l = lg(x);
     313        5028 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     314       42939 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T,p);
     315        5028 :   return FpXQX_renormalize(z, l);
     316             : }
     317             : GEN
     318       49186 : RgX_to_FqX(GEN x, GEN T, GEN p)
     319             : {
     320       49186 :   long i, l = lg(x);
     321       49186 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     322       49186 :   if (T)
     323       10990 :     for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T, p);
     324             :   else
     325      791282 :     for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     326       49186 :   return FpXQX_renormalize(z, l);
     327             : }
     328             : 
     329             : GEN
     330      219128 : RgC_to_FqC(GEN x, GEN T, GEN p)
     331             : {
     332      219128 :   long i, l = lg(x);
     333      219128 :   GEN z = cgetg(l, t_COL);
     334      219128 :   if (T)
     335     1430310 :     for (i = 1; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T, p);
     336             :   else
     337           0 :     for (i = 1; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     338      219128 :   return z;
     339             : }
     340             : 
     341             : GEN
     342       52325 : RgM_to_FqM(GEN x, GEN T, GEN p)
     343      271418 : { pari_APPLY_same(RgC_to_FqC(gel(x, i), T, p)) }
     344             : 
     345             : /* lg(V) > 1 */
     346             : GEN
     347      851487 : FpXV_FpC_mul(GEN V, GEN W, GEN p)
     348             : {
     349      851487 :   pari_sp av = avma;
     350      851487 :   long i, l = lg(V);
     351      851487 :   GEN z = ZX_Z_mul(gel(V,1),gel(W,1));
     352     4201029 :   for(i=2; i<l; i++)
     353             :   {
     354     3349542 :     z = ZX_add(z, ZX_Z_mul(gel(V,i),gel(W,i)));
     355     3349542 :     if ((i & 7) == 0) z = gerepileupto(av, z);
     356             :   }
     357      851487 :   return gerepileupto(av, FpX_red(z,p));
     358             : }
     359             : 
     360             : GEN
     361       55832 : FqX_Fq_add(GEN y, GEN x, GEN T, GEN p)
     362             : {
     363       55832 :   long i, lz = lg(y);
     364             :   GEN z;
     365       55832 :   if (!T) return FpX_Fp_add(y, x, p);
     366        8666 :   if (lz == 2) return scalarpol(x, varn(y));
     367        7952 :   z = cgetg(lz,t_POL); z[1] = y[1];
     368        7952 :   gel(z,2) = Fq_add(gel(y,2),x, T, p);
     369        7952 :   if (lz == 3) z = FpXX_renormalize(z,lz);
     370             :   else
     371       33145 :     for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
     372        7952 :   return z;
     373             : }
     374             : 
     375             : GEN
     376        1094 : FqX_Fq_sub(GEN y, GEN x, GEN T, GEN p)
     377             : {
     378        1094 :   long i, lz = lg(y);
     379             :   GEN z;
     380        1094 :   if (!T) return FpX_Fp_sub(y, x, p);
     381        1094 :   if (lz == 2) return scalarpol(x, varn(y));
     382        1094 :   z = cgetg(lz,t_POL); z[1] = y[1];
     383        1094 :   gel(z,2) = Fq_sub(gel(y,2), x, T, p);
     384        1094 :   if (lz == 3) z = FpXX_renormalize(z,lz);
     385             :   else
     386       10303 :     for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
     387        1094 :   return z;
     388             : }
     389             : 
     390             : GEN
     391      149065 : FqX_Fq_mul_to_monic(GEN P, GEN U, GEN T, GEN p)
     392             : {
     393             :   long i, lP;
     394      149065 :   GEN res = cgetg_copy(P, &lP); res[1] = P[1];
     395      918586 :   for(i=2; i<lP-1; i++) gel(res,i) = Fq_mul(U,gel(P,i), T,p);
     396      149065 :   gel(res,lP-1) = gen_1; return res;
     397             : }
     398             : 
     399             : GEN
     400       38334 : FpXQX_normalize(GEN z, GEN T, GEN p)
     401             : {
     402             :   GEN lc;
     403       38334 :   if (lg(z) == 2) return z;
     404       38320 :   lc = leading_coeff(z);
     405       38320 :   if (typ(lc) == t_POL)
     406             :   {
     407        2194 :     if (lg(lc) > 3) /* nonconstant */
     408        1922 :       return FqX_Fq_mul_to_monic(z, Fq_inv(lc,T,p), T,p);
     409             :     /* constant */
     410         272 :     lc = gel(lc,2);
     411         272 :     z = shallowcopy(z);
     412         272 :     gel(z, lg(z)-1) = lc;
     413             :   }
     414             :   /* lc a t_INT */
     415       36398 :   if (equali1(lc)) return z;
     416          80 :   return FqX_Fq_mul_to_monic(z, Fp_inv(lc,p), T,p);
     417             : }
     418             : 
     419             : GEN
     420      398873 : FqX_eval(GEN x, GEN y, GEN T, GEN p)
     421             : {
     422             :   pari_sp av;
     423             :   GEN p1, r;
     424      398873 :   long j, i=lg(x)-1;
     425      398873 :   if (i<=2)
     426       45971 :     return (i==2)? Fq_red(gel(x,2), T, p): gen_0;
     427      352902 :   av=avma; p1=gel(x,i);
     428             :   /* specific attention to sparse polynomials (see poleval)*/
     429             :   /*You've guessed it! It's a copy-paste(tm)*/
     430     1174025 :   for (i--; i>=2; i=j-1)
     431             :   {
     432      821809 :     for (j=i; !signe(gel(x,j)); j--)
     433         686 :       if (j==2)
     434             :       {
     435         490 :         if (i!=j) y = Fq_pow(y,utoipos(i-j+1), T, p);
     436         490 :         return gerepileupto(av, Fq_mul(p1,y, T, p));
     437             :       }
     438      821123 :     r = (i==j)? y: Fq_pow(y, utoipos(i-j+1), T, p);
     439      821123 :     p1 = Fq_add(Fq_mul(p1,r,T,p), gel(x,j), T, p);
     440             :   }
     441      352412 :   return gerepileupto(av, p1);
     442             : }
     443             : 
     444             : GEN
     445       99679 : FqXY_evalx(GEN Q, GEN x, GEN T, GEN p)
     446             : {
     447       99679 :   long i, lb = lg(Q);
     448             :   GEN z;
     449       99679 :   if (!T) return FpXY_evalx(Q, x, p);
     450       89319 :   z = cgetg(lb, t_POL); z[1] = Q[1];
     451      474735 :   for (i=2; i<lb; i++)
     452             :   {
     453      385416 :     GEN q = gel(Q,i);
     454      385416 :     gel(z,i) = typ(q) == t_INT? modii(q,p): FqX_eval(q, x, T, p);
     455             :   }
     456       89319 :   return FpXQX_renormalize(z, lb);
     457             : }
     458             : 
     459             : /* Q an FpXY, evaluate at (X,Y) = (x,y) */
     460             : GEN
     461       14623 : FqXY_eval(GEN Q, GEN y, GEN x, GEN T, GEN p)
     462             : {
     463       14623 :   pari_sp av = avma;
     464       14623 :   if (!T) return FpXY_eval(Q, y, x, p);
     465         966 :   return gerepileupto(av, FqX_eval(FqXY_evalx(Q, x, T, p), y, T, p));
     466             : }
     467             : 
     468             : /* a X^d */
     469             : GEN
     470    12244072 : monomial(GEN a, long d, long v)
     471             : {
     472             :   long i, n;
     473             :   GEN P;
     474    12244072 :   if (d < 0) {
     475          14 :     if (isrationalzero(a)) return pol_0(v);
     476          14 :     retmkrfrac(a, pol_xn(-d, v));
     477             :   }
     478    12244058 :   if (gequal0(a))
     479             :   {
     480       93275 :     if (isexactzero(a)) return scalarpol_shallow(a,v);
     481           0 :     n = d+2; P = cgetg(n+1, t_POL);
     482           0 :     P[1] = evalsigne(0) | evalvarn(v);
     483             :   }
     484             :   else
     485             :   {
     486    12150780 :     n = d+2; P = cgetg(n+1, t_POL);
     487    12150784 :     P[1] = evalsigne(1) | evalvarn(v);
     488             :   }
     489    31277952 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     490    12150784 :   gel(P,i) = a; return P;
     491             : }
     492             : GEN
     493     1863576 : monomialcopy(GEN a, long d, long v)
     494             : {
     495             :   long i, n;
     496             :   GEN P;
     497     1863576 :   if (d < 0) {
     498          14 :     if (isrationalzero(a)) return pol_0(v);
     499          14 :     retmkrfrac(gcopy(a), pol_xn(-d, v));
     500             :   }
     501     1863562 :   if (gequal0(a))
     502             :   {
     503          14 :     if (isexactzero(a)) return scalarpol(a,v);
     504           7 :     n = d+2; P = cgetg(n+1, t_POL);
     505           7 :     P[1] = evalsigne(0) | evalvarn(v);
     506             :   }
     507             :   else
     508             :   {
     509     1863548 :     n = d+2; P = cgetg(n+1, t_POL);
     510     1863548 :     P[1] = evalsigne(1) | evalvarn(v);
     511             :   }
     512     3510878 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     513     1863555 :   gel(P,i) = gcopy(a); return P;
     514             : }
     515             : GEN
     516      324750 : pol_x_powers(long N, long v)
     517             : {
     518      324750 :   GEN L = cgetg(N+1,t_VEC);
     519             :   long i;
     520      785261 :   for (i=1; i<=N; i++) gel(L,i) = pol_xn(i-1, v);
     521      324755 :   return L;
     522             : }
     523             : 
     524             : GEN
     525           0 : FqXQ_powers(GEN x, long l, GEN S, GEN T, GEN p)
     526             : {
     527           0 :   return T ? FpXQXQ_powers(x, l, S, T, p): FpXQ_powers(x, l, S, p);
     528             : }
     529             : 
     530             : GEN
     531           0 : FqXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, GEN p)
     532             : {
     533           0 :   return T ? FpXQXQ_matrix_pow(y, n, m, S, T, p): FpXQ_matrix_pow(y, n, m, S, p);
     534             : }
     535             : 
     536             : /*******************************************************************/
     537             : /*                                                                 */
     538             : /*                             Fq                                  */
     539             : /*                                                                 */
     540             : /*******************************************************************/
     541             : 
     542             : GEN
     543    11609099 : Fq_add(GEN x, GEN y, GEN T/*unused*/, GEN p)
     544             : {
     545             :   (void)T;
     546    11609099 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     547             :   {
     548     1143057 :     case 0: return Fp_add(x,y,p);
     549      764628 :     case 1: return FpX_Fp_add(x,y,p);
     550       92070 :     case 2: return FpX_Fp_add(y,x,p);
     551     9609344 :     case 3: return FpX_add(x,y,p);
     552             :   }
     553             :   return NULL;/*LCOV_EXCL_LINE*/
     554             : }
     555             : 
     556             : GEN
     557     8349766 : Fq_sub(GEN x, GEN y, GEN T/*unused*/, GEN p)
     558             : {
     559             :   (void)T;
     560     8349766 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     561             :   {
     562      256148 :     case 0: return Fp_sub(x,y,p);
     563       24480 :     case 1: return FpX_Fp_sub(x,y,p);
     564       23908 :     case 2: return Fp_FpX_sub(x,y,p);
     565     8045230 :     case 3: return FpX_sub(x,y,p);
     566             :   }
     567             :   return NULL;/*LCOV_EXCL_LINE*/
     568             : }
     569             : 
     570             : GEN
     571     1080568 : Fq_neg(GEN x, GEN T/*unused*/, GEN p)
     572             : {
     573             :   (void)T;
     574     1080568 :   return (typ(x)==t_POL)? FpX_neg(x,p): Fp_neg(x,p);
     575             : }
     576             : 
     577             : GEN
     578       83614 : Fq_halve(GEN x, GEN T/*unused*/, GEN p)
     579             : {
     580             :   (void)T;
     581       83614 :   return (typ(x)==t_POL)? FpX_halve(x,p): Fp_halve(x,p);
     582             : }
     583             : 
     584             : /* If T==NULL do not reduce*/
     585             : GEN
     586     8379208 : Fq_mul(GEN x, GEN y, GEN T, GEN p)
     587             : {
     588     8379208 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     589             :   {
     590     1037518 :     case 0: return Fp_mul(x,y,p);
     591      128947 :     case 1: return FpX_Fp_mul(x,y,p);
     592      402277 :     case 2: return FpX_Fp_mul(y,x,p);
     593     6810473 :     case 3: if (T) return FpXQ_mul(x,y,T,p);
     594     4231967 :             else return FpX_mul(x,y,p);
     595             :   }
     596             :   return NULL;/*LCOV_EXCL_LINE*/
     597             : }
     598             : 
     599             : /* If T==NULL do not reduce*/
     600             : GEN
     601      492834 : Fq_mulu(GEN x, ulong y, /*unused*/GEN T, GEN p)
     602             : {
     603             :   (void) T;
     604      492834 :   return typ(x)==t_POL ? FpX_Fp_mul(x,utoi(y),p): Fp_mulu(x, y, p);
     605             : }
     606             : 
     607             : /* y t_INT */
     608             : GEN
     609       43993 : Fq_Fp_mul(GEN x, GEN y, GEN T/*unused*/, GEN p)
     610             : {
     611             :   (void)T;
     612        6844 :   return (typ(x) == t_POL)? FpX_Fp_mul(x,y,p)
     613       50837 :                           : Fp_mul(x,y,p);
     614             : }
     615             : /* If T==NULL do not reduce*/
     616             : GEN
     617      613390 : Fq_sqr(GEN x, GEN T, GEN p)
     618             : {
     619      613390 :   if (typ(x) == t_POL)
     620             :   {
     621       72844 :     if (T) return FpXQ_sqr(x,T,p);
     622           0 :     else return FpX_sqr(x,p);
     623             :   }
     624             :   else
     625      540546 :     return Fp_sqr(x,p);
     626             : }
     627             : 
     628             : GEN
     629           0 : Fq_neg_inv(GEN x, GEN T, GEN p)
     630             : {
     631           0 :   if (typ(x) == t_INT) return Fp_inv(Fp_neg(x,p),p);
     632           0 :   return FpXQ_inv(FpX_neg(x,p),T,p);
     633             : }
     634             : 
     635             : GEN
     636           0 : Fq_invsafe(GEN x, GEN pol, GEN p)
     637             : {
     638           0 :   if (typ(x) == t_INT) return Fp_invsafe(x,p);
     639           0 :   return FpXQ_invsafe(x,pol,p);
     640             : }
     641             : 
     642             : GEN
     643       89437 : Fq_inv(GEN x, GEN pol, GEN p)
     644             : {
     645       89437 :   if (typ(x) == t_INT) return Fp_inv(x,p);
     646       81671 :   return FpXQ_inv(x,pol,p);
     647             : }
     648             : 
     649             : GEN
     650      343588 : Fq_div(GEN x, GEN y, GEN pol, GEN p)
     651             : {
     652      343588 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     653             :   {
     654      318269 :     case 0: return Fp_div(x,y,p);
     655       16702 :     case 1: return FpX_Fp_div(x,y,p);
     656         406 :     case 2: return FpX_Fp_mul(FpXQ_inv(y,pol,p),x,p);
     657        8211 :     case 3: return FpXQ_div(x,y,pol,p);
     658             :   }
     659             :   return NULL;/*LCOV_EXCL_LINE*/
     660             : }
     661             : 
     662             : GEN
     663      795326 : Fq_pow(GEN x, GEN n, GEN pol, GEN p)
     664             : {
     665      795326 :   if (typ(x) == t_INT) return Fp_pow(x,n,p);
     666      136842 :   return FpXQ_pow(x,n,pol,p);
     667             : }
     668             : 
     669             : GEN
     670       15050 : Fq_powu(GEN x, ulong n, GEN pol, GEN p)
     671             : {
     672       15050 :   if (typ(x) == t_INT) return Fp_powu(x,n,p);
     673        1267 :   return FpXQ_powu(x,n,pol,p);
     674             : }
     675             : 
     676             : GEN
     677     1894003 : Fq_sqrt(GEN x, GEN T, GEN p)
     678             : {
     679     1894003 :   if (typ(x) == t_INT)
     680             :   {
     681     1823898 :     if (!T || odd(get_FpX_degree(T))) return Fp_sqrt(x,p);
     682        9596 :     x = scalarpol_shallow(x, get_FpX_var(T));
     683             :   }
     684       79701 :   return FpXQ_sqrt(x,T,p);
     685             : }
     686             : GEN
     687      170744 : Fq_sqrtn(GEN x, GEN n, GEN T, GEN p, GEN *zeta)
     688             : {
     689      170744 :   if (typ(x) == t_INT)
     690             :   {
     691             :     long d;
     692      170387 :     if (!T) return Fp_sqrtn(x,n,p,zeta);
     693         119 :     d = get_FpX_degree(T);
     694         119 :     if (ugcdiu(n,d) == 1)
     695             :     {
     696          56 :       if (!zeta) return Fp_sqrtn(x,n,p,NULL);
     697             :       /* gcd(n,p-1)=gcd(n,q-1): same number of solutions in Fp and F_q */
     698          21 :       if (equalii(gcdii(subiu(p,1),n), gcdii(subiu(Fp_powu(p,d,n), 1), n)))
     699          14 :         return Fp_sqrtn(x,n,p,zeta);
     700             :     }
     701          70 :     x = scalarpol(x, get_FpX_var(T)); /* left on stack */
     702             :   }
     703         427 :   return FpXQ_sqrtn(x,n,T,p,zeta);
     704             : }
     705             : 
     706             : struct _Fq_field
     707             : {
     708             :   GEN T, p;
     709             : };
     710             : 
     711             : static GEN
     712      303247 : _Fq_red(void *E, GEN x)
     713      303247 : { struct _Fq_field *s = (struct _Fq_field *)E;
     714      303247 :   return Fq_red(x, s->T, s->p);
     715             : }
     716             : 
     717             : static GEN
     718      362523 : _Fq_add(void *E, GEN x, GEN y)
     719             : {
     720             :   (void) E;
     721      362523 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     722             :   {
     723          14 :     case 0: return addii(x,y);
     724           0 :     case 1: return ZX_Z_add(x,y);
     725       15918 :     case 2: return ZX_Z_add(y,x);
     726      346591 :     default: return ZX_add(x,y);
     727             :   }
     728             : }
     729             : 
     730             : static GEN
     731      315028 : _Fq_neg(void *E, GEN x) { (void) E; return typ(x)==t_POL?ZX_neg(x):negi(x); }
     732             : 
     733             : static GEN
     734      243341 : _Fq_mul(void *E, GEN x, GEN y)
     735             : {
     736             :   (void) E;
     737      243341 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     738             :   {
     739         679 :     case 0: return mulii(x,y);
     740        1085 :     case 1: return ZX_Z_mul(x,y);
     741        1043 :     case 2: return ZX_Z_mul(y,x);
     742      240534 :     default: return ZX_mul(x,y);
     743             :   }
     744             : }
     745             : 
     746             : static GEN
     747        9331 : _Fq_inv(void *E, GEN x)
     748        9331 : { struct _Fq_field *s = (struct _Fq_field *)E;
     749        9331 :   return Fq_inv(x,s->T,s->p);
     750             : }
     751             : 
     752             : static int
     753       38388 : _Fq_equal0(GEN x) { return signe(x)==0; }
     754             : 
     755             : static GEN
     756        4151 : _Fq_s(void *E, long x) { (void) E; return stoi(x); }
     757             : 
     758             : static const struct bb_field Fq_field={_Fq_red,_Fq_add,_Fq_mul,_Fq_neg,
     759             :                                        _Fq_inv,_Fq_equal0,_Fq_s};
     760             : 
     761        4725 : const struct bb_field *get_Fq_field(void **E, GEN T, GEN p)
     762             : {
     763        4725 :   if (!T)
     764           0 :     return get_Fp_field(E, p);
     765             :   else
     766             :   {
     767        4725 :     GEN z = new_chunk(sizeof(struct _Fq_field));
     768        4725 :     struct _Fq_field *e = (struct _Fq_field *) z;
     769        4725 :     e->T = T; e->p  = p; *E = (void*)e;
     770        4725 :     return &Fq_field;
     771             :   }
     772             : }
     773             : 
     774             : /*******************************************************************/
     775             : /*                                                                 */
     776             : /*                             Fq[X]                               */
     777             : /*                                                                 */
     778             : /*******************************************************************/
     779             : /* P(X + c) */
     780             : GEN
     781         266 : FpX_translate(GEN P, GEN c, GEN p)
     782             : {
     783         266 :   pari_sp av = avma;
     784             :   GEN Q, *R;
     785             :   long i, k, n;
     786             : 
     787         266 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     788         266 :   Q = leafcopy(P);
     789         266 :   R = (GEN*)(Q+2); n = degpol(P);
     790        3738 :   for (i=1; i<=n; i++)
     791             :   {
     792      118153 :     for (k=n-i; k<n; k++)
     793      114681 :       R[k] = Fp_add(R[k], Fp_mul(c, R[k+1], p), p);
     794             : 
     795        3472 :     if (gc_needed(av,2))
     796             :     {
     797           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FpX_translate, i = %ld/%ld", i,n);
     798           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     799             :     }
     800             :   }
     801         266 :   return gerepilecopy(av, FpX_renormalize(Q, lg(Q)));
     802             : }
     803             : /* P(X + c), c an Fq */
     804             : GEN
     805       33880 : FqX_translate(GEN P, GEN c, GEN T, GEN p)
     806             : {
     807       33880 :   pari_sp av = avma;
     808             :   GEN Q, *R;
     809             :   long i, k, n;
     810             : 
     811             :   /* signe works for t_(INT|POL) */
     812       33880 :   if (!signe(P) || !signe(c)) return RgX_copy(P);
     813       33880 :   Q = leafcopy(P);
     814       33880 :   R = (GEN*)(Q+2); n = degpol(P);
     815      150059 :   for (i=1; i<=n; i++)
     816             :   {
     817      433559 :     for (k=n-i; k<n; k++)
     818      317380 :       R[k] = Fq_add(R[k], Fq_mul(c, R[k+1], T, p), T, p);
     819             : 
     820      116179 :     if (gc_needed(av,2))
     821             :     {
     822           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FqX_translate, i = %ld/%ld", i,n);
     823           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     824             :     }
     825             :   }
     826       33880 :   return gerepilecopy(av, FpXQX_renormalize(Q, lg(Q)));
     827             : }
     828             : 
     829             : GEN
     830       40451 : FqV_roots_to_pol(GEN V, GEN T, GEN p, long v)
     831             : {
     832       40451 :   pari_sp ltop = avma;
     833             :   long k;
     834             :   GEN W;
     835       40451 :   if (lgefint(p) == 3)
     836             :   {
     837       31734 :     ulong pp = p[2];
     838       31734 :     GEN Tl = ZX_to_Flx(T, pp);
     839       31735 :     GEN Vl = ZXC_to_FlxC(V, pp, get_Flx_var(Tl));
     840       31735 :     Tl = FlxqV_roots_to_pol(Vl, Tl, pp, v);
     841       31734 :     return gerepileupto(ltop, FlxX_to_ZXX(Tl));
     842             :   }
     843        8717 :   W = cgetg(lg(V),t_VEC);
     844       78163 :   for(k=1; k < lg(V); k++)
     845       69446 :     gel(W,k) = deg1pol_shallow(gen_1,Fq_neg(gel(V,k),T,p),v);
     846        8717 :   return gerepileupto(ltop, FpXQXV_prod(W, T, p));
     847             : }
     848             : 
     849             : GEN
     850      109689 : FqV_red(GEN x, GEN T, GEN p)
     851      778946 : { pari_APPLY_type(t_VEC, Fq_red(gel(x,i), T, p)) }
     852             : 
     853             : GEN
     854       86560 : FqC_red(GEN x, GEN T, GEN p)
     855      590346 : { pari_APPLY_type(t_COL, Fq_red(gel(x,i), T, p)) }
     856             : 
     857             : GEN
     858        1701 : FqM_red(GEN x, GEN T, GEN p)
     859        5411 : { pari_APPLY_same(FqC_red(gel(x,i), T, p)) }
     860             : 
     861             : GEN
     862           0 : FqC_add(GEN x, GEN y, GEN T, GEN p)
     863             : {
     864           0 :   if (!T) return FpC_add(x, y, p);
     865           0 :   pari_APPLY_type(t_COL, Fq_add(gel(x,i), gel(y,i), T, p))
     866             : }
     867             : 
     868             : GEN
     869           0 : FqC_sub(GEN x, GEN y, GEN T, GEN p)
     870             : {
     871           0 :   if (!T) return FpC_sub(x, y, p);
     872           0 :   pari_APPLY_type(t_COL, Fq_sub(gel(x,i), gel(y,i), T, p))
     873             : }
     874             : 
     875             : GEN
     876           0 : FqC_Fq_mul(GEN x, GEN y, GEN T, GEN p)
     877             : {
     878           0 :   if (!T) return FpC_Fp_mul(x, y, p);
     879           0 :   pari_APPLY_type(t_COL, Fq_mul(gel(x,i),y,T,p))
     880             : }
     881             : 
     882             : GEN
     883         105 : FqC_FqV_mul(GEN x, GEN y, GEN T, GEN p)
     884             : {
     885         105 :   long i,j, lx=lg(x), ly=lg(y);
     886             :   GEN z;
     887         105 :   if (ly==1) return cgetg(1,t_MAT);
     888         105 :   z = cgetg(ly,t_MAT);
     889         819 :   for (j=1; j < ly; j++)
     890             :   {
     891         714 :     GEN zj = cgetg(lx,t_COL);
     892        4200 :     for (i=1; i<lx; i++) gel(zj,i) = Fq_mul(gel(x,i),gel(y,j), T, p);
     893         714 :     gel(z, j) = zj;
     894             :   }
     895         105 :   return z;
     896             : }
     897             : 
     898             : GEN
     899        5313 : FpXC_center(GEN x, GEN p, GEN pov2)
     900       40818 : { pari_APPLY_type(t_COL, FpX_center(gel(x,i), p, pov2)) }
     901             : 
     902             : GEN
     903        1737 : FpXM_center(GEN x, GEN p, GEN pov2)
     904        7050 : { pari_APPLY_same(FpXC_center(gel(x,i), p, pov2)) }
     905             : 
     906             : /*******************************************************************/
     907             : /*                                                                 */
     908             : /*                          GENERIC CRT                            */
     909             : /*                                                                 */
     910             : /*******************************************************************/
     911             : static GEN
     912     8255510 : primelist(forprime_t *S, long n, GEN dB)
     913             : {
     914     8255510 :   GEN P = cgetg(n+1, t_VECSMALL);
     915     8255484 :   long i = 1;
     916             :   ulong p;
     917    19722025 :   while (i <= n && (p = u_forprime_next(S)))
     918    11466541 :     if (!dB || umodiu(dB, p)) P[i++] = p;
     919     8255484 :   return P;
     920             : }
     921             : 
     922             : void
     923     7709534 : gen_inccrt_i(const char *str, GEN worker, GEN dB, long n, long mmin,
     924             :              forprime_t *S, GEN *pH, GEN *pmod, GEN crt(GEN, GEN, GEN*),
     925             :              GEN center(GEN, GEN, GEN))
     926             : {
     927     7709534 :   long m = mmin? minss(mmin, n): usqrt(n);
     928             :   GEN  H, P, mod;
     929             :   pari_timer ti;
     930     7709535 :   if (DEBUGLEVEL > 4)
     931             :   {
     932           0 :     timer_start(&ti);
     933           0 :     err_printf("%s: nb primes: %ld\n",str, n);
     934             :   }
     935     7709531 :   if (m == 1)
     936             :   {
     937     7423903 :     GEN P = primelist(S, n, dB);
     938     7423872 :     GEN done = closure_callgen1(worker, P);
     939     7423884 :     H = gel(done,1);
     940     7423884 :     mod = gel(done,2);
     941     7423884 :     if (!*pH && center) H = center(H, mod, shifti(mod,-1));
     942     7423849 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: modular", str);
     943             :   }
     944             :   else
     945             :   {
     946      285628 :     long i, s = (n+m-1)/m, r = m - (m*s-n), di = 0;
     947             :     struct pari_mt pt;
     948      285628 :     long pending = 0;
     949      285628 :     H = cgetg(m+1, t_VEC); P = cgetg(m+1, t_VEC);
     950      285628 :     mt_queue_start_lim(&pt, worker, m);
     951     1177880 :     for (i=1; i<=m || pending; i++)
     952             :     {
     953             :       GEN done;
     954      892252 :       GEN pr = i <= m ? mkvec(primelist(S, i<=r ? s: s-1, dB)): NULL;
     955      892254 :       mt_queue_submit(&pt, i, pr);
     956      892252 :       done = mt_queue_get(&pt, NULL, &pending);
     957      892252 :       if (done)
     958             :       {
     959      831606 :         di++;
     960      831606 :         gel(H, di) = gel(done,1);
     961      831606 :         gel(P, di) = gel(done,2);
     962      831606 :         if (DEBUGLEVEL>5) err_printf("%ld%% ",100*di/m);
     963             :       }
     964             :     }
     965      285628 :     mt_queue_end(&pt);
     966      285628 :     if (DEBUGLEVEL>5) err_printf("\n");
     967      285628 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: modular", str);
     968      285628 :     H = crt(H, P, &mod);
     969      285628 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: chinese", str);
     970             :   }
     971     7709477 :   if (*pH) H = crt(mkvec2(*pH, H), mkvec2(*pmod, mod), &mod);
     972     7709478 :   *pH = H; *pmod = mod;
     973     7709478 : }
     974             : void
     975     2051572 : gen_inccrt(const char *str, GEN worker, GEN dB, long n, long mmin,
     976             :            forprime_t *S, GEN *pH, GEN *pmod, GEN crt(GEN, GEN, GEN*),
     977             :            GEN center(GEN, GEN, GEN))
     978             : {
     979     2051572 :   pari_sp av = avma;
     980     2051572 :   gen_inccrt_i(str, worker, dB, n, mmin, S, pH, pmod, crt, center);
     981     2051546 :   gerepileall(av, 2, pH, pmod);
     982     2051656 : }
     983             : 
     984             : GEN
     985     1268714 : gen_crt(const char *str, GEN worker, forprime_t *S, GEN dB, ulong bound, long mmin, GEN *pmod,
     986             :         GEN crt(GEN, GEN, GEN*), GEN center(GEN, GEN, GEN))
     987             : {
     988     1268714 :   GEN mod = gen_1, H = NULL;
     989             :   ulong e;
     990             : 
     991     1268714 :   bound++;
     992     2537503 :   while (bound > (e = expi(mod)))
     993             :   {
     994     1268670 :     long n = (bound - e) / expu(S->p) + 1;
     995     1268708 :     gen_inccrt(str, worker, dB, n, mmin, S, &H, &mod, crt, center);
     996             :   }
     997     1268780 :   if (pmod) *pmod = mod;
     998     1268780 :   return H;
     999             : }
    1000             : 
    1001             : /*******************************************************************/
    1002             : /*                                                                 */
    1003             : /*                          MODULAR GCD                            */
    1004             : /*                                                                 */
    1005             : /*******************************************************************/
    1006             : /* return z = a mod q, b mod p (p,q) = 1; qinv = 1/q mod p; a in ]-q,q] */
    1007             : static GEN
    1008     5113635 : Fl_chinese_coprime(GEN a, ulong b, GEN q, ulong p, ulong qinv, GEN pq, GEN pq2)
    1009             : {
    1010     5113635 :   ulong d, amod = umodiu(a, p);
    1011     5113607 :   pari_sp av = avma;
    1012             :   GEN ax;
    1013             : 
    1014     5113607 :   if (b == amod) return NULL;
    1015     2104758 :   d = Fl_mul(Fl_sub(b, amod, p), qinv, p); /* != 0 */
    1016     2105350 :   if (d >= 1 + (p>>1))
    1017     1027020 :     ax = subii(a, mului(p-d, q));
    1018             :   else
    1019             :   {
    1020     1078330 :     ax = addii(a, mului(d, q)); /* in ]0, pq[ assuming a in ]-q,q[ */
    1021     1077887 :     if (cmpii(ax,pq2) > 0) ax = subii(ax,pq);
    1022             :   }
    1023     2104456 :   return gerepileuptoint(av, ax);
    1024             : }
    1025             : GEN
    1026         378 : Z_init_CRT(ulong Hp, ulong p) { return stoi(Fl_center(Hp, p, p>>1)); }
    1027             : GEN
    1028       31689 : ZX_init_CRT(GEN Hp, ulong p, long v)
    1029             : {
    1030       31689 :   long i, l = lg(Hp), lim = (long)(p>>1);
    1031       31689 :   GEN H = cgetg(l, t_POL);
    1032       31689 :   H[1] = evalsigne(1) | evalvarn(v);
    1033      796166 :   for (i=2; i<l; i++)
    1034      764477 :     gel(H,i) = stoi(Fl_center(Hp[i], p, lim));
    1035       31689 :   return ZX_renormalize(H,l);
    1036             : }
    1037             : 
    1038             : GEN
    1039        5775 : ZM_init_CRT(GEN Hp, ulong p)
    1040             : {
    1041        5775 :   long i,j, m, l = lg(Hp), lim = (long)(p>>1);
    1042        5775 :   GEN c, cp, H = cgetg(l, t_MAT);
    1043        5775 :   if (l==1) return H;
    1044        5775 :   m = lgcols(Hp);
    1045       18970 :   for (j=1; j<l; j++)
    1046             :   {
    1047       13195 :     cp = gel(Hp,j);
    1048       13195 :     c = cgetg(m, t_COL);
    1049       13195 :     gel(H,j) = c;
    1050      166411 :     for (i=1; i<m; i++) gel(c,i) = stoi(Fl_center(cp[i],p, lim));
    1051             :   }
    1052        5775 :   return H;
    1053             : }
    1054             : 
    1055             : int
    1056        7616 : Z_incremental_CRT(GEN *H, ulong Hp, GEN *ptq, ulong p)
    1057             : {
    1058        7616 :   GEN h, q = *ptq, qp = muliu(q,p);
    1059        7616 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1060        7616 :   int stable = 1;
    1061        7616 :   h = Fl_chinese_coprime(*H,Hp,q,p,qinv,qp,shifti(qp,-1));
    1062        7616 :   if (h) { *H = h; stable = 0; }
    1063        7616 :   *ptq = qp; return stable;
    1064             : }
    1065             : 
    1066             : static int
    1067      147473 : ZX_incremental_CRT_raw(GEN *ptH, GEN Hp, GEN q, GEN qp, ulong p)
    1068             : {
    1069      147473 :   GEN H = *ptH, h, qp2 = shifti(qp,-1);
    1070      147462 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1071      147474 :   long i, l = lg(H), lp = lg(Hp);
    1072      147474 :   int stable = 1;
    1073             : 
    1074      147474 :   if (l < lp)
    1075             :   { /* degree increases */
    1076           0 :     GEN x = cgetg(lp, t_POL);
    1077           0 :     for (i=1; i<l; i++)  x[i] = H[i];
    1078           0 :     for (   ; i<lp; i++) gel(x,i) = gen_0;
    1079           0 :     *ptH = H = x;
    1080           0 :     stable = 0;
    1081      147474 :   } else if (l > lp)
    1082             :   { /* degree decreases */
    1083           0 :     GEN x = cgetg(l, t_VECSMALL);
    1084           0 :     for (i=1; i<lp; i++)  x[i] = Hp[i];
    1085           0 :     for (   ; i<l; i++) x[i] = 0;
    1086           0 :     Hp = x; lp = l;
    1087             :   }
    1088     4933542 :   for (i=2; i<lp; i++)
    1089             :   {
    1090     4786203 :     h = Fl_chinese_coprime(gel(H,i),Hp[i],q,p,qinv,qp,qp2);
    1091     4786068 :     if (h) { gel(H,i) = h; stable = 0; }
    1092             :   }
    1093      147339 :   (void)ZX_renormalize(H,lp);
    1094      147475 :   return stable;
    1095             : }
    1096             : 
    1097             : int
    1098           0 : ZX_incremental_CRT(GEN *ptH, GEN Hp, GEN *ptq, ulong p)
    1099             : {
    1100           0 :   GEN q = *ptq, qp = muliu(q,p);
    1101           0 :   int stable = ZX_incremental_CRT_raw(ptH, Hp, q, qp, p);
    1102           0 :   *ptq = qp; return stable;
    1103             : }
    1104             : 
    1105             : int
    1106        5801 : ZM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
    1107             : {
    1108        5801 :   GEN h, H = *pH, q = *ptq, qp = muliu(q, p), qp2 = shifti(qp,-1);
    1109        5801 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1110        5801 :   long i,j, l = lg(H), m = lgcols(H);
    1111        5801 :   int stable = 1;
    1112       20944 :   for (j=1; j<l; j++)
    1113      157160 :     for (i=1; i<m; i++)
    1114             :     {
    1115      142017 :       h = Fl_chinese_coprime(gcoeff(H,i,j), coeff(Hp,i,j),q,p,qinv,qp,qp2);
    1116      142017 :       if (h) { gcoeff(H,i,j) = h; stable = 0; }
    1117             :     }
    1118        5801 :   *ptq = qp; return stable;
    1119             : }
    1120             : 
    1121             : GEN
    1122         623 : ZXM_init_CRT(GEN Hp, long deg, ulong p)
    1123             : {
    1124             :   long i, j, k;
    1125             :   GEN H;
    1126         623 :   long m, l = lg(Hp), lim = (long)(p>>1), n;
    1127         623 :   H = cgetg(l, t_MAT);
    1128         623 :   if (l==1) return H;
    1129         623 :   m = lgcols(Hp);
    1130         623 :   n = deg + 3;
    1131        2114 :   for (j=1; j<l; j++)
    1132             :   {
    1133        1491 :     GEN cp = gel(Hp,j);
    1134        1491 :     GEN c = cgetg(m, t_COL);
    1135        1491 :     gel(H,j) = c;
    1136       23905 :     for (i=1; i<m; i++)
    1137             :     {
    1138       22414 :       GEN dp = gel(cp, i);
    1139       22414 :       long l = lg(dp);
    1140       22414 :       GEN d = cgetg(n, t_POL);
    1141       22414 :       gel(c, i) = d;
    1142       22414 :       d[1] = dp[1] | evalsigne(1);
    1143       45647 :       for (k=2; k<l; k++)
    1144       23233 :         gel(d,k) = stoi(Fl_center(dp[k], p, lim));
    1145       44457 :       for (   ; k<n; k++)
    1146       22043 :         gel(d,k) = gen_0;
    1147             :     }
    1148             :   }
    1149         623 :   return H;
    1150             : }
    1151             : 
    1152             : int
    1153         653 : ZXM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
    1154             : {
    1155         653 :   GEN v, H = *pH, q = *ptq, qp = muliu(q, p), qp2 = shifti(qp,-1);
    1156         653 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1157         653 :   long i,j,k, l = lg(H), m = lgcols(H), n = lg(gmael(H,1,1));
    1158         653 :   int stable = 1;
    1159        2225 :   for (j=1; j<l; j++)
    1160       90418 :     for (i=1; i<m; i++)
    1161             :     {
    1162       88846 :       GEN h = gmael(H,j,i), hp = gmael(Hp,j,i);
    1163       88846 :       long lh = lg(hp);
    1164      246641 :       for (k=2; k<lh; k++)
    1165             :       {
    1166      157795 :         v = Fl_chinese_coprime(gel(h,k),uel(hp,k),q,p,qinv,qp,qp2);
    1167      157795 :         if (v) { gel(h,k) = v; stable = 0; }
    1168             :       }
    1169      108763 :       for (; k<n; k++)
    1170             :       {
    1171       19917 :         v = Fl_chinese_coprime(gel(h,k),0,q,p,qinv,qp,qp2);
    1172       19917 :         if (v) { gel(h,k) = v; stable = 0; }
    1173             :       }
    1174             :     }
    1175         653 :   *ptq = qp; return stable;
    1176             : }
    1177             : 
    1178             : /* record the degrees of Euclidean remainders (make them as large as
    1179             :  * possible : smaller values correspond to a degenerate sequence) */
    1180             : static void
    1181       23209 : Flx_resultant_set_dglist(GEN a, GEN b, GEN dglist, ulong p)
    1182             : {
    1183             :   long da,db,dc, ind;
    1184       23209 :   pari_sp av = avma;
    1185             : 
    1186       23209 :   if (lgpol(a)==0 || lgpol(b)==0) return;
    1187       21942 :   da = degpol(a);
    1188       21942 :   db = degpol(b);
    1189       21942 :   if (db > da)
    1190           0 :   { swapspec(a,b, da,db); }
    1191       21942 :   else if (!da) return;
    1192       21942 :   ind = 0;
    1193      144196 :   while (db)
    1194             :   {
    1195      122254 :     GEN c = Flx_rem(a,b, p);
    1196      122253 :     a = b; b = c; dc = degpol(c);
    1197      122253 :     if (dc < 0) break;
    1198             : 
    1199      122253 :     ind++;
    1200      122253 :     if (dc > dglist[ind]) dglist[ind] = dc;
    1201      122253 :     if (gc_needed(av,2))
    1202             :     {
    1203           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1204           0 :       gerepileall(av, 2, &a,&b);
    1205             :     }
    1206      122254 :     db = dc; /* = degpol(b) */
    1207             :   }
    1208       21942 :   if (ind+1 > lg(dglist)) setlg(dglist,ind+1);
    1209       21942 :   set_avma(av);
    1210             : }
    1211             : /* assuming the PRS finishes on a degree 1 polynomial C0 + C1X, with
    1212             :  * "generic" degree sequence as given by dglist, set *Ci and return
    1213             :  * resultant(a,b). Modular version of Collins's subresultant */
    1214             : static ulong
    1215     2084589 : Flx_resultant_all(GEN a, GEN b, long *C0, long *C1, GEN dglist, ulong p)
    1216             : {
    1217             :   long da,db,dc, ind;
    1218     2084589 :   ulong lb, res, g = 1UL, h = 1UL, ca = 1UL, cb = 1UL;
    1219     2084589 :   int s = 1;
    1220     2084589 :   pari_sp av = avma;
    1221             : 
    1222     2084589 :   *C0 = 1; *C1 = 0;
    1223     2084589 :   if (lgpol(a)==0 || lgpol(b)==0) return 0;
    1224     2075184 :   da = degpol(a);
    1225     2075203 :   db = degpol(b);
    1226     2075155 :   if (db > da)
    1227             :   {
    1228           0 :     swapspec(a,b, da,db);
    1229           0 :     if (both_odd(da,db)) s = -s;
    1230             :   }
    1231     2075155 :   else if (!da) return 1; /* = a[2] ^ db, since 0 <= db <= da = 0 */
    1232     2075155 :   ind = 0;
    1233    19788354 :   while (db)
    1234             :   { /* sub-resultant algo., applied to ca * a and cb * b, ca,cb scalars,
    1235             :      * da = deg a, db = deg b */
    1236    17717671 :     GEN c = Flx_rem(a,b, p);
    1237    17580998 :     long delta = da - db;
    1238             : 
    1239    17580998 :     if (both_odd(da,db)) s = -s;
    1240    17577587 :     lb = Fl_mul(b[db+2], cb, p);
    1241    17596762 :     a = b; b = c; dc = degpol(c);
    1242    17595815 :     ind++;
    1243    17595815 :     if (dc != dglist[ind]) return gc_ulong(av,0); /* degenerates */
    1244    17590924 :     if (g == h)
    1245             :     { /* frequent */
    1246    17531080 :       ulong cc = Fl_mul(ca, Fl_powu(Fl_div(lb,g,p), delta+1, p), p);
    1247    17652870 :       ca = cb;
    1248    17652870 :       cb = cc;
    1249             :     }
    1250             :     else
    1251             :     {
    1252       59844 :       ulong cc = Fl_mul(ca, Fl_powu(lb, delta+1, p), p);
    1253       59844 :       ulong ghdelta = Fl_mul(g, Fl_powu(h, delta, p), p);
    1254       59844 :       ca = cb;
    1255       59844 :       cb = Fl_div(cc, ghdelta, p);
    1256             :     }
    1257    17714262 :     da = db; /* = degpol(a) */
    1258    17714262 :     db = dc; /* = degpol(b) */
    1259             : 
    1260    17714262 :     g = lb;
    1261    17714262 :     if (delta == 1)
    1262    17614739 :       h = g; /* frequent */
    1263             :     else
    1264       99523 :       h = Fl_mul(h, Fl_powu(Fl_div(g,h,p), delta, p), p);
    1265             : 
    1266    17713630 :     if (gc_needed(av,2))
    1267             :     {
    1268           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1269           0 :       gerepileall(av, 2, &a,&b);
    1270             :     }
    1271             :   }
    1272     2070683 :   if (da > 1) return 0; /* Failure */
    1273             :   /* last nonconstant polynomial has degree 1 */
    1274     2070683 :   *C0 = Fl_mul(ca, a[2], p);
    1275     2070628 :   *C1 = Fl_mul(ca, a[3], p);
    1276     2070650 :   res = Fl_mul(cb, b[2], p);
    1277     2070609 :   if (s == -1) res = p - res;
    1278     2070609 :   return gc_ulong(av,res);
    1279             : }
    1280             : 
    1281             : /* Q a vector of polynomials representing B in Fp[X][Y], evaluate at X = x,
    1282             :  * Return 0 in case of degree drop. */
    1283             : static GEN
    1284     2108023 : FlxY_evalx_drop(GEN Q, ulong x, ulong p)
    1285             : {
    1286             :   GEN z;
    1287     2108023 :   long i, lb = lg(Q);
    1288     2108023 :   ulong leadz = Flx_eval(leading_coeff(Q), x, p);
    1289     2107682 :   long vs=mael(Q,2,1);
    1290     2107682 :   if (!leadz) return zero_Flx(vs);
    1291             : 
    1292     2097022 :   z = cgetg(lb, t_VECSMALL); z[1] = vs;
    1293    20050357 :   for (i=2; i<lb-1; i++) z[i] = Flx_eval(gel(Q,i), x, p);
    1294     2095012 :   z[i] = leadz; return z;
    1295             : }
    1296             : 
    1297             : GEN
    1298        2072 : FpXY_FpXQ_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
    1299             : {
    1300        2072 :   pari_sp av = avma;
    1301        2072 :   long i, lb = lg(Q);
    1302             :   GEN z;
    1303        2072 :   if (lb == 2) return pol_0(vx);
    1304        2072 :   z = gel(Q, lb-1);
    1305        2072 :   if (lb == 3 || !signe(y)) return typ(z)==t_INT? scalar_ZX(z, vx): ZX_copy(z);
    1306             : 
    1307        2072 :   if (typ(z) == t_INT) z = scalar_ZX_shallow(z, vx);
    1308       48636 :   for (i=lb-2; i>=2; i--)
    1309             :   {
    1310       46564 :     GEN c = gel(Q,i);
    1311       46564 :     z = FqX_Fq_mul(z, y, T, p);
    1312       46564 :     z = typ(c) == t_INT? FqX_Fq_add(z,c,T,p): FqX_add(z,c,T,p);
    1313             :   }
    1314        2072 :   return gerepileupto(av, z);
    1315             : }
    1316             : 
    1317             : static GEN
    1318      291703 : ZX_norml1(GEN x)
    1319             : {
    1320      291703 :   long i, l = lg(x);
    1321             :   GEN s;
    1322             : 
    1323      291703 :   if (l == 2) return gen_0;
    1324      199149 :   s = gel(x, l-1); /* != 0 */
    1325      697236 :   for (i = l-2; i > 1; i--) {
    1326      498106 :     GEN xi = gel(x,i);
    1327      498106 :     if (!signe(xi)) continue;
    1328      259384 :     s = addii_sign(s,1, xi,1);
    1329             :   }
    1330      199130 :   return s;
    1331             : }
    1332             : /* x >= 0, y != 0, return x + |y| */
    1333             : static GEN
    1334       25553 : addii_abs(GEN x, GEN y)
    1335             : {
    1336       25553 :   if (!signe(x)) return absi_shallow(y);
    1337       16044 :   return addii_sign(x,1, y,1);
    1338             : }
    1339             : 
    1340             : /* x a ZX, return sum_{i >= k} |x[i]| binomial(i, k) */
    1341             : static GEN
    1342       31652 : ZX_norml1_1(GEN x, long k)
    1343             : {
    1344       31652 :   long i, d = degpol(x);
    1345             :   GEN s, C; /* = binomial(i, k) */
    1346             : 
    1347       31652 :   if (!d || k > d) return gen_0;
    1348       31652 :   s = absi_shallow(gel(x, k+2)); /* may be 0 */
    1349       31654 :   C = gen_1;
    1350       68058 :   for (i = k+1; i <= d; i++) {
    1351       36408 :     GEN xi = gel(x,i+2);
    1352       36408 :     if (k) C = diviuexact(muliu(C, i), i-k);
    1353       36410 :     if (signe(xi)) s = addii_abs(s, mulii(C, xi));
    1354             :   }
    1355       31650 :   return s;
    1356             : }
    1357             : /* x has non-negative real coefficients */
    1358             : static GEN
    1359        3255 : RgX_norml1_1(GEN x, long k)
    1360             : {
    1361        3255 :   long i, d = degpol(x);
    1362             :   GEN s, C; /* = binomial(i, k) */
    1363             : 
    1364        3255 :   if (!d || k > d) return gen_0;
    1365        3255 :   s = gel(x, k+2); /* may be 0 */
    1366        3255 :   C = gen_1;
    1367        9128 :   for (i = k+1; i <= d; i++) {
    1368        5873 :     GEN xi = gel(x,i+2);
    1369        5873 :     if (k) C = diviuexact(muliu(C, i), i-k);
    1370        5873 :     if (!gequal0(xi)) s = gadd(s, gmul(C, xi));
    1371             :   }
    1372        3255 :   return s;
    1373             : }
    1374             : 
    1375             : /* N_2(A)^2 */
    1376             : static GEN
    1377        7997 : sqrN2(GEN A, long prec)
    1378             : {
    1379        7997 :   pari_sp av = avma;
    1380        7997 :   long i, l = lg(A);
    1381        7997 :   GEN a = gen_0;
    1382       39121 :   for (i = 2; i < l; i++)
    1383             :   {
    1384       31124 :     a = gadd(a, gabs(gnorm(gel(A,i)), prec));
    1385       31124 :     if (gc_needed(av,1))
    1386             :     {
    1387           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_RgXY_ResBound i = %ld",i);
    1388           0 :       a = gerepileupto(av, a);
    1389             :     }
    1390             :   }
    1391        7997 :   return a;
    1392             : }
    1393             : /* Interpolate at roots of 1 and use Hadamard bound for univariate resultant:
    1394             :  *   bound = N_2(A)^degpol B N_2(B)^degpol(A),  where
    1395             :  *     N_2(A) = sqrt(sum (N_1(Ai))^2)
    1396             :  * Return e such that Res(A, B) < 2^e */
    1397             : static GEN
    1398        7150 : RgX_RgXY_ResBound(GEN A, GEN B, long prec)
    1399             : {
    1400        7150 :   pari_sp av = avma;
    1401        7150 :   GEN b = gen_0, bnd;
    1402        7150 :   long i, lB = lg(B);
    1403       28184 :   for (i=2; i<lB; i++)
    1404             :   {
    1405       21034 :     GEN t = gel(B,i);
    1406       21034 :     if (typ(t) == t_POL) t = gnorml1(t, prec);
    1407       21034 :     b = gadd(b, gabs(gsqr(t), prec));
    1408       21034 :     if (gc_needed(av,1))
    1409             :     {
    1410           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_RgXY_ResBound i = %ld",i);
    1411           0 :       b = gerepileupto(av, b);
    1412             :     }
    1413             :   }
    1414        7150 :   bnd = gsqrt(gmul(gpowgs(sqrN2(A,prec), degpol(B)),
    1415             :                    gpowgs(b, degpol(A))), prec);
    1416        7150 :   return gerepileupto(av, bnd);
    1417             : }
    1418             : /* A,B in C[X] return RgX_RgXY_ResBound(A, B(x+y)) */
    1419             : static GEN
    1420         847 : RgX_RgXY_ResBound_1(GEN A, GEN B, long prec)
    1421             : {
    1422         847 :   pari_sp av = avma, av2;
    1423         847 :   GEN b = gen_0, bnd;
    1424         847 :   long i, lB = lg(B);
    1425         847 :   B = shallowcopy(B);
    1426        4102 :   for (i=2; i<lB; i++) gel(B,i) = gabs(gel(B,i), prec);
    1427         847 :   av2 = avma;
    1428        4102 :   for (i=2; i<lB; i++)
    1429             :   {
    1430        3255 :     b = gadd(b, gsqr(RgX_norml1_1(B, i-2)));
    1431        3255 :     if (gc_needed(av2,1))
    1432             :     {
    1433           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_RgXY_ResBound i = %ld",i);
    1434           0 :       b = gerepileupto(av2, b);
    1435             :     }
    1436             :   }
    1437         847 :   bnd = gsqrt(gmul(gpowgs(sqrN2(A,prec), degpol(B)),
    1438             :                    gpowgs(b, degpol(A))), prec);
    1439         847 :   return gerepileupto(av, bnd);
    1440             : }
    1441             : 
    1442             : /* log2 N_2(A)^2 */
    1443             : static double
    1444      176463 : log2N2(GEN A)
    1445             : {
    1446      176463 :   pari_sp av = avma;
    1447      176463 :   long i, l = lg(A);
    1448      176463 :   GEN a = gen_0;
    1449     1333461 :   for (i=2; i < l; i++)
    1450             :   {
    1451     1157002 :     a = addii(a, sqri(gel(A,i)));
    1452     1156999 :     if (gc_needed(av,1))
    1453             :     {
    1454           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
    1455           0 :       a = gerepileupto(av, a);
    1456             :     }
    1457             :   }
    1458      176459 :   return gc_double(av, dbllog2(a));
    1459             : }
    1460             : /* Interpolate at roots of 1 and use Hadamard bound for univariate resultant:
    1461             :  *   bound = N_2(A)^degpol B N_2(B)^degpol(A),  where
    1462             :  *     N_2(A) = sqrt(sum (N_1(Ai))^2)
    1463             :  * Return e such that Res(A, B) < 2^e */
    1464             : ulong
    1465      166384 : ZX_ZXY_ResBound(GEN A, GEN B, GEN dB)
    1466             : {
    1467      166384 :   pari_sp av = avma;
    1468      166384 :   GEN b = gen_0;
    1469      166384 :   long i, lB = lg(B);
    1470             :   double logb;
    1471     1259762 :   for (i=2; i<lB; i++)
    1472             :   {
    1473     1093389 :     GEN t = gel(B,i);
    1474     1093389 :     if (typ(t) == t_POL) t = ZX_norml1(t);
    1475     1093387 :     b = addii(b, sqri(t));
    1476     1093379 :     if (gc_needed(av,1))
    1477             :     {
    1478           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
    1479           0 :       b = gerepileupto(av, b);
    1480             :     }
    1481             :   }
    1482      166373 :   logb = dbllog2(b); if (dB) logb -= 2 * dbllog2(dB);
    1483      166379 :   i = (long)((degpol(B) * log2N2(A) + degpol(A) * logb) / 2);
    1484      166383 :   return gc_ulong(av, (i <= 0)? 1: 1 + (ulong)i);
    1485             : }
    1486             : /* A,B ZX. Return ZX_ZXY_ResBound(A(x), B(x+y)) */
    1487             : static ulong
    1488       10083 : ZX_ZXY_ResBound_1(GEN A, GEN B)
    1489             : {
    1490       10083 :   pari_sp av = avma;
    1491       10083 :   GEN b = gen_0;
    1492       10083 :   long i, lB = lg(B);
    1493       41736 :   for (i=2; i<lB; i++)
    1494             :   {
    1495       31652 :     b = addii(b, sqri(ZX_norml1_1(B, i-2)));
    1496       31654 :     if (gc_needed(av,1))
    1497             :     {
    1498           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
    1499           0 :       b = gerepileupto(av, b);
    1500             :     }
    1501             :   }
    1502       10084 :   i = (long)((degpol(B) * log2N2(A) + degpol(A) * dbllog2(b)) / 2);
    1503       10085 :   return gc_ulong(av, (i <= 0)? 1: 1 + (ulong)i);
    1504             : }
    1505             : /* special case B = A' */
    1506             : static ulong
    1507     1129938 : ZX_discbound(GEN A)
    1508             : {
    1509     1129938 :   pari_sp av = avma;
    1510     1129938 :   GEN a = gen_0, b = gen_0;
    1511     1129938 :   long i , lA = lg(A), dA = degpol(A);
    1512             :   double loga, logb;
    1513     6734577 :   for (i = 2; i < lA; i++)
    1514             :   {
    1515     5604889 :     GEN c = sqri(gel(A,i));
    1516     5604602 :     a = addii(a, c);
    1517     5604595 :     if (i > 2) b = addii(b, mulii(c, sqru(i-2)));
    1518     5604636 :     if (gc_needed(av,1))
    1519             :     {
    1520           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_discbound i = %ld",i);
    1521           0 :       gerepileall(av, 2, &a, &b);
    1522             :     }
    1523             :   }
    1524     1129688 :   loga = dbllog2(a);
    1525     1129848 :   logb = dbllog2(b); set_avma(av);
    1526     1129871 :   i = (long)(((dA-1) * loga + dA * logb) / 2);
    1527     1129871 :   return (i <= 0)? 1: 1 + (ulong)i;
    1528             : }
    1529             : 
    1530             : /* return Res(a(Y), b(n,Y)) over Fp. la = leading_coeff(a) [for efficiency] */
    1531             : static ulong
    1532     5535050 : Flx_FlxY_eval_resultant(GEN a, GEN b, ulong n, ulong p, ulong pi, ulong la)
    1533             : {
    1534     5535050 :   GEN ev = FlxY_evalx_pre(b, n, p, pi);
    1535     5535910 :   long drop = lg(b) - lg(ev);
    1536     5535910 :   ulong r = Flx_resultant_pre(a, ev, p, pi);
    1537     5534836 :   if (drop && la != 1) r = Fl_mul(r, Fl_powu_pre(la, drop, p, pi), p);
    1538     5534846 :   return r;
    1539             : }
    1540             : static GEN
    1541         284 : FpX_FpXY_eval_resultant(GEN a, GEN b, GEN n, GEN p, GEN la, long db, long vX)
    1542             : {
    1543         284 :   GEN ev = FpXY_evaly(b, n, p, vX);
    1544         284 :   long drop = db-degpol(ev);
    1545         284 :   GEN r = FpX_resultant(a, ev, p);
    1546         284 :   if (drop && !gequal1(la)) r = Fp_mul(r, Fp_powu(la, drop,p),p);
    1547         284 :   return r;
    1548             : }
    1549             : 
    1550             : /* assume dres := deg(Res_X(a,b), Y) <= deg(a,X) * deg(b,Y) < p */
    1551             : /* Return a Fly */
    1552             : static GEN
    1553      368284 : Flx_FlxY_resultant_polint(GEN a, GEN b, ulong p, ulong pi, long dres, long sx)
    1554             : {
    1555             :   long i;
    1556      368284 :   ulong n, la = Flx_lead(a);
    1557      368284 :   GEN  x = cgetg(dres+2, t_VECSMALL);
    1558      368284 :   GEN  y = cgetg(dres+2, t_VECSMALL);
    1559             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1560             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1561     2955116 :   for (i=0,n = 1; i < dres; n++)
    1562             :   {
    1563     2586838 :     x[++i] = n;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,pi,la);
    1564     2586734 :     x[++i] = p-n; y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,pi,la);
    1565             :   }
    1566      368278 :   if (i == dres)
    1567             :   {
    1568      362772 :     x[++i] = 0;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,pi,la);
    1569             :   }
    1570      368274 :   return Flv_polint(x,y, p, sx);
    1571             : }
    1572             : 
    1573             : static GEN
    1574        7575 : FlxX_pseudorem(GEN x, GEN y, ulong p, ulong pi)
    1575             : {
    1576        7575 :   long vx = varn(x), dx, dy, dz, i, lx, dp;
    1577        7575 :   pari_sp av = avma, av2;
    1578             : 
    1579        7575 :   if (!signe(y)) pari_err_INV("FlxX_pseudorem",y);
    1580        7575 :   (void)new_chunk(2);
    1581        7577 :   dx=degpol(x); x = RgX_recip_i(x)+2;
    1582        7584 :   dy=degpol(y); y = RgX_recip_i(y)+2; dz=dx-dy; dp = dz+1;
    1583        7584 :   av2 = avma;
    1584             :   for (;;)
    1585             :   {
    1586       62069 :     gel(x,0) = Flx_neg(gel(x,0), p); dp--;
    1587      232451 :     for (i=1; i<=dy; i++)
    1588      169828 :       gel(x,i) = Flx_add( Flx_mul_pre(gel(y,0), gel(x,i), p, pi),
    1589      170322 :                           Flx_mul_pre(gel(x,0), gel(y,i), p, pi), p );
    1590     1115457 :     for (   ; i<=dx; i++)
    1591     1054087 :       gel(x,i) = Flx_mul_pre(gel(y,0), gel(x,i), p, pi);
    1592       65970 :     do { x++; dx--; } while (dx >= 0 && lg(gel(x,0))==2);
    1593       61370 :     if (dx < dy) break;
    1594       53788 :     if (gc_needed(av2,1))
    1595             :     {
    1596           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_pseudorem dx = %ld >= %ld",dx,dy);
    1597           0 :       gerepilecoeffs(av2,x,dx+1);
    1598             :     }
    1599             :   }
    1600        7582 :   if (dx < 0) return zero_Flx(0);
    1601        7582 :   lx = dx+3; x -= 2;
    1602        7582 :   x[0]=evaltyp(t_POL) | _evallg(lx);
    1603        7582 :   x[1]=evalsigne(1) | evalvarn(vx);
    1604        7582 :   x = RgX_recip_i(x);
    1605        7585 :   if (dp)
    1606             :   { /* multiply by y[0]^dp   [beware dummy vars from FpX_FpXY_resultant] */
    1607        1983 :     GEN t = Flx_powu_pre(gel(y,0), dp, p, pi);
    1608        7931 :     for (i=2; i<lx; i++) gel(x,i) = Flx_mul_pre(gel(x,i), t, p, pi);
    1609             :   }
    1610        7584 :   return gerepilecopy(av, x);
    1611             : }
    1612             : 
    1613             : /* return a Flx */
    1614             : GEN
    1615        2536 : FlxX_resultant(GEN u, GEN v, ulong p, long sx)
    1616             : {
    1617        2536 :   pari_sp av = avma, av2;
    1618             :   long degq, dx, dy, du, dv, dr, signh;
    1619             :   ulong pi;
    1620             :   GEN z, g, h, r, p1;
    1621             : 
    1622        2536 :   dx = degpol(u); dy = degpol(v); signh = 1;
    1623        2538 :   if (dx < dy)
    1624             :   {
    1625           7 :     swap(u,v); lswap(dx,dy);
    1626           7 :     if (both_odd(dx, dy)) signh = -signh;
    1627             :   }
    1628        2538 :   if (dy < 0) return zero_Flx(sx);
    1629        2538 :   pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1630        2538 :   if (dy==0) return gerepileupto(av, Flx_powu_pre(gel(v,2),dx,p,pi));
    1631             : 
    1632        2538 :   g = h = pol1_Flx(sx); av2 = avma;
    1633             :   for(;;)
    1634             :   {
    1635        7578 :     r = FlxX_pseudorem(u,v,p,pi); dr = lg(r);
    1636        7590 :     if (dr == 2) { set_avma(av); return zero_Flx(sx); }
    1637        7590 :     du = degpol(u); dv = degpol(v); degq = du-dv;
    1638        7590 :     u = v; p1 = g; g = leading_coeff(u);
    1639        7590 :     switch(degq)
    1640             :     {
    1641           0 :       case 0: break;
    1642        5593 :       case 1:
    1643        5593 :         p1 = Flx_mul_pre(h,p1, p, pi); h = g; break;
    1644        1997 :       default:
    1645        1997 :         p1 = Flx_mul_pre(Flx_powu_pre(h,degq,p,pi), p1, p, pi);
    1646        1995 :         h = Flx_div_pre(Flx_powu_pre(g,degq,p,pi),
    1647        1995 :                         Flx_powu_pre(h,degq-1,p,pi), p, pi);
    1648             :     }
    1649        7580 :     if (both_odd(du,dv)) signh = -signh;
    1650        7576 :     v = FlxY_Flx_div(r, p1, p);
    1651        7576 :     if (dr==3) break;
    1652        5038 :     if (gc_needed(av2,1))
    1653             :     {
    1654           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_resultant, dr = %ld",dr);
    1655           0 :       gerepileall(av2,4, &u, &v, &g, &h);
    1656             :     }
    1657             :   }
    1658        2538 :   z = gel(v,2);
    1659        2538 :   if (dv > 1) z = Flx_div_pre(Flx_powu_pre(z,dv,p,pi),
    1660           0 :                               Flx_powu_pre(h,dv-1,p,pi), p, pi);
    1661        2538 :   if (signh < 0) z = Flx_neg(z,p);
    1662        2538 :   return gerepileupto(av, z);
    1663             : }
    1664             : 
    1665             : /* Warning:
    1666             :  * This function switches between valid and invalid variable ordering*/
    1667             : 
    1668             : static GEN
    1669        6156 : FlxY_to_FlyX(GEN b, long sv)
    1670             : {
    1671        6156 :   long i, n=-1;
    1672        6156 :   long sw = b[1]&VARNBITS;
    1673       21010 :   for(i=2;i<lg(b);i++) n = maxss(n,lgpol(gel(b,i)));
    1674        6155 :   return Flm_to_FlxX(Flm_transpose(FlxX_to_Flm(b,n)),sv,sw);
    1675             : }
    1676             : 
    1677             : /* Return a Fly*/
    1678             : GEN
    1679        6157 : Flx_FlxY_resultant(GEN a, GEN b, ulong p)
    1680             : {
    1681        6157 :   pari_sp ltop=avma;
    1682        6157 :   long dres = degpol(a)*degpol(b);
    1683        6156 :   long sx=a[1], sy=b[1]&VARNBITS;
    1684             :   GEN z;
    1685        6156 :   b = FlxY_to_FlyX(b,sx);
    1686        6156 :   if ((ulong)dres >= p)
    1687        2538 :     z = FlxX_resultant(Fly_to_FlxY(a, sy), b, p, sx);
    1688             :   else
    1689             :   {
    1690        3618 :     ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1691        3618 :     z = Flx_FlxY_resultant_polint(a, b, p, pi, (ulong)dres, sy);
    1692             :   }
    1693        6158 :   return gerepileupto(ltop,z);
    1694             : }
    1695             : 
    1696             : /* return a t_POL (in variable v >= 0) whose coeffs are the coeffs of b,
    1697             :  * in variable v. This is an incorrect PARI object if initially varn(b) << v.
    1698             :  * We could return a vector of coeffs, but it is convenient to have degpol()
    1699             :  * and friends available. Even in that case, it will behave nicely with all
    1700             :  * functions treating a polynomial as a vector of coeffs (eg poleval).
    1701             :  * FOR INTERNAL USE! */
    1702             : GEN
    1703      145653 : swap_vars(GEN b0, long v)
    1704             : {
    1705      145653 :   long i, n = RgX_degree(b0, v);
    1706             :   GEN b, x;
    1707      145652 :   if (n < 0) return pol_0(v);
    1708      145652 :   b = cgetg(n+3, t_POL); x = b + 2;
    1709      145652 :   b[1] = evalsigne(1) | evalvarn(v);
    1710      966528 :   for (i=0; i<=n; i++) gel(x,i) = polcoef_i(b0, i, v);
    1711      145649 :   return b;
    1712             : }
    1713             : 
    1714             : /* assume varn(b) << varn(a) */
    1715             : /* return a FpY*/
    1716             : GEN
    1717          15 : FpX_FpXY_resultant(GEN a, GEN b, GEN p)
    1718             : {
    1719          15 :   long i,n,dres, db, vY = varn(b), vX = varn(a);
    1720             :   GEN la,x,y;
    1721             : 
    1722          15 :   if (lgefint(p) == 3)
    1723             :   {
    1724           0 :     ulong pp = uel(p,2);
    1725           0 :     b = ZXX_to_FlxX(b, pp, vX);
    1726           0 :     a = ZX_to_Flx(a, pp);
    1727           0 :     x = Flx_FlxY_resultant(a, b, pp);
    1728           0 :     return Flx_to_ZX(x);
    1729             :   }
    1730          15 :   db = RgXY_degreex(b);
    1731          15 :   dres = degpol(a)*degpol(b);
    1732          15 :   la = leading_coeff(a);
    1733          15 :   x = cgetg(dres+2, t_VEC);
    1734          15 :   y = cgetg(dres+2, t_VEC);
    1735             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1736             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1737         157 :   for (i=0,n = 1; i < dres; n++)
    1738             :   {
    1739         142 :     gel(x,++i) = utoipos(n);
    1740         142 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1741         142 :     gel(x,++i) = subiu(p,n);
    1742         142 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1743             :   }
    1744          15 :   if (i == dres)
    1745             :   {
    1746           0 :     gel(x,++i) = gen_0;
    1747           0 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b, gel(x,i), p,la,db,vY);
    1748             :   }
    1749          15 :   return FpV_polint(x,y, p, vY);
    1750             : }
    1751             : 
    1752             : GEN
    1753          79 : FpX_composedsum(GEN P, GEN Q, GEN p)
    1754             : {
    1755          79 :   pari_sp av = avma;
    1756          79 :   if (lgefint(p)==3)
    1757             :   {
    1758           0 :     ulong pp = p[2];
    1759           0 :     GEN z = Flx_composedsum(ZX_to_Flx(P, pp), ZX_to_Flx(Q, pp), pp);
    1760           0 :     return gerepileupto(av, Flx_to_ZX(z));
    1761             :   }
    1762             :   else
    1763             :   {
    1764          79 :     long n = 1+ degpol(P)*degpol(Q);
    1765          79 :     GEN Pl = FpX_invLaplace(FpX_Newton(P,n,p), p);
    1766          79 :     GEN Ql = FpX_invLaplace(FpX_Newton(Q,n,p), p);
    1767          79 :     GEN L = FpX_Laplace(FpXn_mul(Pl, Ql, n, p), p);
    1768          79 :     GEN lead = Fp_mul(Fp_powu(leading_coeff(P),degpol(Q), p),
    1769          79 :         Fp_powu(leading_coeff(Q),degpol(P), p), p);
    1770          79 :     GEN R = FpX_fromNewton(L, p);
    1771          79 :     return gerepileupto(av, FpX_Fp_mul(R, lead, p));
    1772             :   }
    1773             : }
    1774             : 
    1775             : GEN
    1776           0 : FpX_composedprod(GEN P, GEN Q, GEN p)
    1777             : {
    1778           0 :   pari_sp av = avma;
    1779           0 :   if (lgefint(p)==3)
    1780             :   {
    1781           0 :     ulong pp = p[2];
    1782           0 :     GEN z = Flx_composedprod(ZX_to_Flx(P, pp), ZX_to_Flx(Q, pp), pp);
    1783           0 :     return gerepileupto(av, Flx_to_ZX(z));
    1784             :   }
    1785             :   else
    1786             :   {
    1787           0 :     long n = 1+ degpol(P)*degpol(Q);
    1788           0 :     GEN L = FpX_convol(FpX_Newton(P,n,p), FpX_Newton(Q,n,p), p);
    1789           0 :     return gerepileupto(av,FpX_fromNewton(L, p));
    1790             :   }
    1791             : }
    1792             : 
    1793             : static GEN
    1794          79 : _FpX_composedsum(void *E, GEN a, GEN b)
    1795          79 : { return FpX_composedsum(a,b, (GEN)E); }
    1796             : 
    1797             : GEN
    1798        1574 : FpXV_composedsum(GEN V, GEN p)
    1799             : {
    1800        1574 :   if (lgefint(p)==3)
    1801             :   {
    1802           0 :     ulong pp = p[2];
    1803           0 :     return Flx_to_ZX(FlxV_composedsum(ZXV_to_FlxV(V, pp), pp));
    1804             :   }
    1805        1574 :   return gen_product(V, (void *)p, &_FpX_composedsum);
    1806             : }
    1807             : 
    1808             : /* 0, 1, -1, 2, -2, ... */
    1809             : #define next_lambda(a) (a>0 ? -a : 1-a)
    1810             : 
    1811             : /* Assume A in Z[Y], B in Q[Y][X], B squarefree in (Q[Y]/(A))[X] and
    1812             :  * Res_Y(A, B) in Z[X]. Find a small lambda (start from *lambda, use
    1813             :  * next_lambda successively) such that C(X) = Res_Y(A(Y), B(X + lambda Y))
    1814             :  * is squarefree, reset *lambda to the chosen value and return C. Set LERS to
    1815             :  * the Last nonconstant polynomial in the Euclidean Remainder Sequence */
    1816             : static GEN
    1817       21504 : ZX_ZXY_resultant_LERS(GEN A, GEN B0, long *plambda, GEN *LERS)
    1818             : {
    1819             :   ulong bound, dp;
    1820       21504 :   pari_sp av = avma, av2 = 0;
    1821       21504 :   long lambda = *plambda, degA = degpol(A), dres = degA*degpol(B0);
    1822             :   long stable, checksqfree, i,n, cnt, degB;
    1823       21504 :   long v, vX = varn(B0), vY = varn(A); /* vY < vX */
    1824             :   GEN x, y, dglist, B, q, a, b, ev, H, H0, H1, Hp, H0p, H1p, C0, C1;
    1825             :   forprime_t S;
    1826             : 
    1827       21504 :   if (degA == 1)
    1828             :   {
    1829        1190 :     GEN a1 = gel(A,3), a0 = gel(A,2);
    1830        1190 :     B = lambda? RgX_translate(B0, monomial(stoi(lambda), 1, vY)): B0;
    1831        1190 :     H = gsubst(B, vY, gdiv(gneg(a0),a1));
    1832        1190 :    if (!equali1(a1)) H = RgX_Rg_mul(H, powiu(a1, poldegree(B,vY)));
    1833        1190 :     *LERS = mkvec2(scalarpol_shallow(a0,vX), scalarpol_shallow(a1,vX));
    1834        1190 :     return gc_all(av, 2, &H, LERS);
    1835             :   }
    1836             : 
    1837       20314 :   dglist = Hp = H0p = H1p = C0 = C1 = NULL; /* gcc -Wall */
    1838       20314 :   C0 = cgetg(dres+2, t_VECSMALL);
    1839       20314 :   C1 = cgetg(dres+2, t_VECSMALL);
    1840       20314 :   dglist = cgetg(dres+1, t_VECSMALL);
    1841       20314 :   x = cgetg(dres+2, t_VECSMALL);
    1842       20314 :   y = cgetg(dres+2, t_VECSMALL);
    1843       20314 :   B0 = leafcopy(B0);
    1844       20314 :   A = leafcopy(A);
    1845       20314 :   B = B0;
    1846       20314 :   v = fetch_var_higher(); setvarn(A,v);
    1847             :   /* make sure p large enough */
    1848       21095 : INIT:
    1849             :   /* always except the first time */
    1850       21095 :   if (av2) { set_avma(av2); lambda = next_lambda(lambda); }
    1851       21095 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    1852       21095 :   B = swap_vars(B, vY); setvarn(B,v);
    1853             :   /* B0(lambda v + x, v) */
    1854       21095 :   if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    1855       21095 :   av2 = avma;
    1856             : 
    1857       21095 :   if (degA <= 3)
    1858             :   { /* sub-resultant faster for small degrees */
    1859       10423 :     H = RgX_resultant_all(A,B,&q);
    1860       10423 :     if (typ(q) != t_POL || degpol(q)!=1) goto INIT;
    1861        9793 :     H0 = gel(q,2);
    1862        9793 :     if (typ(H0) == t_POL) setvarn(H0,vX); else H0 = scalarpol(H0,vX);
    1863        9793 :     H1 = gel(q,3);
    1864        9793 :     if (typ(H1) == t_POL) setvarn(H1,vX); else H1 = scalarpol(H1,vX);
    1865        9793 :     if (!ZX_is_squarefree(H)) goto INIT;
    1866        9751 :     goto END;
    1867             :   }
    1868             : 
    1869       10672 :   H = H0 = H1 = NULL;
    1870       10672 :   degB = degpol(B);
    1871       10672 :   bound = ZX_ZXY_ResBound(A, B, NULL);
    1872       10672 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    1873       10672 :   dp = 1;
    1874       10672 :   init_modular_big(&S);
    1875       10672 :   for(cnt = 0, checksqfree = 1;;)
    1876       49159 :   {
    1877       59831 :     ulong p = u_forprime_next(&S);
    1878             :     GEN Hi;
    1879       59831 :     a = ZX_to_Flx(A, p);
    1880       59831 :     b = ZXX_to_FlxX(B, p, varn(A));
    1881       59826 :     if (degpol(a) < degA || degpol(b) < degB) continue; /* p | lc(A)lc(B) */
    1882       59826 :     if (checksqfree)
    1883             :     { /* find degree list for generic Euclidean Remainder Sequence */
    1884       10672 :       long goal = minss(degpol(a), degpol(b)); /* longest possible */
    1885       73077 :       for (n=1; n <= goal; n++) dglist[n] = 0;
    1886       10672 :       setlg(dglist, 1);
    1887       23601 :       for (n=0; n <= dres; n++)
    1888             :       {
    1889       23209 :         ev = FlxY_evalx_drop(b, n, p);
    1890       23209 :         Flx_resultant_set_dglist(a, ev, dglist, p);
    1891       23209 :         if (lg(dglist)-1 == goal) break;
    1892             :       }
    1893             :       /* last pol in ERS has degree > 1 ? */
    1894       10672 :       goal = lg(dglist)-1;
    1895       10672 :       if (degpol(B) == 1) { if (!goal) goto INIT; }
    1896             :       else
    1897             :       {
    1898       10616 :         if (goal <= 1) goto INIT;
    1899       10560 :         if (dglist[goal] != 0 || dglist[goal-1] != 1) goto INIT;
    1900             :       }
    1901       10616 :       if (DEBUGLEVEL>4)
    1902           0 :         err_printf("Degree list for ERS (trials: %ld) = %Ps\n",n+1,dglist);
    1903             :     }
    1904             : 
    1905     2144598 :     for (i=0,n = 0; i <= dres; n++)
    1906             :     {
    1907     2084830 :       ev = FlxY_evalx_drop(b, n, p);
    1908     2084547 :       x[++i] = n; y[i] = Flx_resultant_all(a, ev, C0+i, C1+i, dglist, p);
    1909     2084827 :       if (!C1[i]) i--; /* C1(i) = 0. No way to recover C0(i) */
    1910             :     }
    1911       59768 :     Hi = Flv_Flm_polint(x, mkvec3(y,C0,C1), p, 0);
    1912       59775 :     Hp = gel(Hi,1); H0p = gel(Hi,2); H1p = gel(Hi,3);
    1913       59775 :     if (!H && degpol(Hp) != dres) continue;
    1914       59775 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1915       59775 :     if (checksqfree) {
    1916       10616 :       if (!Flx_is_squarefree(Hp, p)) goto INIT;
    1917       10563 :       if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    1918       10563 :       checksqfree = 0;
    1919             :     }
    1920             : 
    1921       59722 :     if (!H)
    1922             :     { /* initialize */
    1923       10563 :       q = utoipos(p); stable = 0;
    1924       10563 :       H = ZX_init_CRT(Hp, p,vX);
    1925       10563 :       H0= ZX_init_CRT(H0p, p,vX);
    1926       10563 :       H1= ZX_init_CRT(H1p, p,vX);
    1927             :     }
    1928             :     else
    1929             :     {
    1930       49159 :       GEN qp = muliu(q,p);
    1931       49159 :       stable  = ZX_incremental_CRT_raw(&H, Hp, q,qp, p)
    1932       49158 :               & ZX_incremental_CRT_raw(&H0,H0p, q,qp, p)
    1933       49159 :               & ZX_incremental_CRT_raw(&H1,H1p, q,qp, p);
    1934       49159 :       q = qp;
    1935             :     }
    1936             :     /* could make it probabilistic for H ? [e.g if stable twice, etc]
    1937             :      * Probabilistic anyway for H0, H1 */
    1938       59722 :     if (DEBUGLEVEL>5 && (stable ||  ++cnt==100))
    1939           0 :     { cnt=0; err_printf("%ld%%%s ",100*expi(q)/bound,stable?"s":""); }
    1940       59722 :     if (stable && (ulong)expi(q) >= bound) break; /* DONE */
    1941       49159 :     if (gc_needed(av,2))
    1942             :     {
    1943           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_rnfequation");
    1944           0 :       gerepileall(av2, 4, &H, &q, &H0, &H1);
    1945             :     }
    1946             :   }
    1947       20314 : END:
    1948       20314 :   if (DEBUGLEVEL>5) err_printf(" done\n");
    1949       20314 :   setvarn(H, vX); (void)delete_var();
    1950       20314 :   *LERS = mkvec2(H0,H1);
    1951       20314 :   *plambda = lambda; return gc_all(av, 2, &H, LERS);
    1952             : }
    1953             : 
    1954             : GEN
    1955       59317 : ZX_ZXY_resultant_all(GEN A, GEN B, long *plambda, GEN *LERS)
    1956             : {
    1957       59317 :   if (LERS)
    1958             :   {
    1959       21504 :     if (!plambda)
    1960           0 :       pari_err_BUG("ZX_ZXY_resultant_all [LERS != NULL needs lambda]");
    1961       21504 :     return ZX_ZXY_resultant_LERS(A, B, plambda, LERS);
    1962             :   }
    1963       37813 :   return ZX_ZXY_rnfequation(A, B, plambda);
    1964             : }
    1965             : 
    1966             : /* If lambda = NULL, return caract(Mod(A, T)), T,A in Z[X].
    1967             :  * Otherwise find a small lambda such that caract (Mod(A + lambda X, T)) is
    1968             :  * squarefree */
    1969             : GEN
    1970       22546 : ZXQ_charpoly_sqf(GEN A, GEN T, long *lambda, long v)
    1971             : {
    1972       22546 :   pari_sp av = avma;
    1973             :   GEN R, a;
    1974             :   long dA;
    1975             :   int delvar;
    1976             : 
    1977       22546 :   if (v < 0) v = 0;
    1978       22546 :   switch (typ(A))
    1979             :   {
    1980       22546 :     case t_POL: dA = degpol(A); if (dA > 0) break;
    1981           0 :       A = constant_coeff(A);
    1982           0 :     default:
    1983           0 :       if (lambda) { A = scalar_ZX_shallow(A,varn(T)); dA = 0; break;}
    1984           0 :       return gerepileupto(av, gpowgs(gsub(pol_x(v), A), degpol(T)));
    1985             :   }
    1986       22546 :   delvar = 0;
    1987       22546 :   if (varncmp(varn(T), 0) <= 0)
    1988             :   {
    1989        3639 :     long v0 = fetch_var(); delvar = 1;
    1990        3639 :     T = leafcopy(T); setvarn(T,v0);
    1991        3639 :     A = leafcopy(A); setvarn(A,v0);
    1992             :   }
    1993       22546 :   R = ZX_ZXY_rnfequation(T, deg1pol_shallow(gen_1, gneg_i(A), 0), lambda);
    1994       22546 :   if (delvar) (void)delete_var();
    1995       22546 :   setvarn(R, v); a = leading_coeff(T);
    1996       22546 :   if (!gequal1(a)) R = gdiv(R, powiu(a, dA));
    1997       22546 :   return gerepileupto(av, R);
    1998             : }
    1999             : 
    2000             : /* charpoly(Mod(A,T)), A may be in Q[X], but assume T and result are integral */
    2001             : GEN
    2002      993716 : ZXQ_charpoly(GEN A, GEN T, long v)
    2003             : {
    2004      993716 :   return (degpol(T) < 16) ? RgXQ_charpoly_i(A,T,v): ZXQ_charpoly_sqf(A,T, NULL, v);
    2005             : }
    2006             : 
    2007             : GEN
    2008        9773 : QXQ_charpoly(GEN A, GEN T, long v)
    2009             : {
    2010        9773 :   pari_sp av = avma;
    2011        9773 :   GEN den, B = Q_remove_denom(A, &den);
    2012        9773 :   GEN P = ZXQ_charpoly(B, T, v);
    2013        9773 :   return gerepilecopy(av, den ? RgX_rescale(P, ginv(den)): P);
    2014             : }
    2015             : 
    2016             : static ulong
    2017     3791222 : ZX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, ulong p)
    2018             : {
    2019     3791222 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    2020             :   ulong H, dp;
    2021     3791081 :   if (dropa && dropb) return 0; /* p | lc(A), p | lc(B) */
    2022     3791081 :   H = Flx_resultant(a, b, p);
    2023     3790766 :   if (dropa)
    2024             :   { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    2025           0 :     ulong c = b[degB+2]; /* lc(B) */
    2026           0 :     if (odd(degB)) c = p - c;
    2027           0 :     c = Fl_powu(c, dropa, p);
    2028           0 :     if (c != 1) H = Fl_mul(H, c, p);
    2029             :   }
    2030     3790766 :   else if (dropb)
    2031             :   { /* multiply by lc(A)^(deg B - deg b) */
    2032           0 :     ulong c = a[degA+2]; /* lc(A) */
    2033           0 :     c = Fl_powu(c, dropb, p);
    2034           0 :     if (c != 1) H = Fl_mul(H, c, p);
    2035             :   }
    2036     3790765 :   dp = dB ? umodiu(dB, p): 1;
    2037     3790765 :   if (dp != 1) H = Fl_mul(H, Fl_powu(Fl_inv(dp,p), degA, p), p);
    2038     3790771 :   return H;
    2039             : }
    2040             : 
    2041             : /* If B=NULL, assume B=A' */
    2042             : static GEN
    2043     1480818 : ZX_resultant_slice(GEN A, GEN B, GEN dB, GEN P, GEN *mod)
    2044             : {
    2045     1480818 :   pari_sp av = avma, av2;
    2046     1480818 :   long degA, degB, i, n = lg(P)-1;
    2047             :   GEN H, T;
    2048             : 
    2049     1480818 :   degA = degpol(A);
    2050     1480816 :   degB = B? degpol(B): degA - 1;
    2051     1480817 :   if (n == 1)
    2052             :   {
    2053      810027 :     ulong Hp, p = uel(P,1);
    2054      810027 :     GEN a = ZX_to_Flx(A, p), b = B? ZX_to_Flx(B, p): Flx_deriv(a, p);
    2055      810024 :     Hp = ZX_resultant_prime(a, b, dB, degA, degB, p);
    2056      810012 :     set_avma(av); *mod = utoipos(p); return utoi(Hp);
    2057             :   }
    2058      670790 :   T = ZV_producttree(P);
    2059      670790 :   A = ZX_nv_mod_tree(A, P, T);
    2060      670788 :   if (B) B = ZX_nv_mod_tree(B, P, T);
    2061      670788 :   H = cgetg(n+1, t_VECSMALL); av2 = avma;
    2062     3651604 :   for(i=1; i <= n; i++, set_avma(av2))
    2063             :   {
    2064     2980821 :     ulong p = P[i];
    2065     2980821 :     GEN a = gel(A,i), b = B? gel(B,i): Flx_deriv(a, p);
    2066     2981207 :     H[i] = ZX_resultant_prime(a, b, dB, degA, degB, p);
    2067             :   }
    2068      670783 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    2069      670788 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2070             : }
    2071             : 
    2072             : GEN
    2073     1480827 : ZX_resultant_worker(GEN P, GEN A, GEN B, GEN dB)
    2074             : {
    2075     1480827 :   GEN V = cgetg(3, t_VEC);
    2076     1480818 :   if (typ(B) == t_INT) B = NULL;
    2077     1480818 :   if (!signe(dB)) dB = NULL;
    2078     1480818 :   gel(V,1) = ZX_resultant_slice(A, B, dB, P, &gel(V,2));
    2079     1480820 :   return V;
    2080             : }
    2081             : 
    2082             : /* Compute Res(A, B/dB) in Z, assuming A,B in Z[X], dB in Z or NULL (= 1)
    2083             :  * If B=NULL, take B = A' and assume deg A > 1 and 'bound' is set */
    2084             : GEN
    2085     1346586 : ZX_resultant_all(GEN A, GEN B, GEN dB, ulong bound)
    2086             : {
    2087     1346586 :   pari_sp av = avma;
    2088             :   forprime_t S;
    2089             :   GEN  H, worker;
    2090     1346586 :   if (!B && degpol(A)==2)
    2091             :   {
    2092      113895 :     GEN a = gel(A,4), b = gel(A,3), c = gel(A,2);
    2093      113895 :     H = mulii(a, subii(shifti(mulii(a, c), 2), sqri(b)));
    2094      113889 :     if (dB) H = diviiexact(H, sqri(dB));
    2095      113889 :     return gerepileuptoint(av, H);
    2096             :   }
    2097     1232689 :   if (B)
    2098             :   {
    2099      154900 :     long a = degpol(A), b = degpol(B);
    2100      154900 :     if (a < 0 || b < 0) return gen_0;
    2101      154870 :     if (!a) return powiu(gel(A,2), b);
    2102      154870 :     if (!b) return powiu(gel(B,2), a);
    2103      153125 :     if (minss(a, b) <= 1)
    2104             :     {
    2105       76574 :       H = RgX_resultant_all(A, B, NULL);
    2106       76573 :       if (dB) H = diviiexact(H, powiu(dB, a));
    2107       76573 :       return gerepileuptoint(av, H);
    2108             :     }
    2109       76551 :     if (!bound) bound = ZX_ZXY_ResBound(A, B, dB);
    2110             :   }
    2111     1154345 :   worker = snm_closure(is_entry("_ZX_resultant_worker"),
    2112             :                        mkvec3(A, B? B: gen_0, dB? dB: gen_0));
    2113     1154394 :   init_modular_big(&S);
    2114     1154350 :   H = gen_crt("ZX_resultant_all", worker, &S, dB, bound, 0, NULL,
    2115             :               ZV_chinese_center, Fp_center);
    2116     1154410 :   return gerepileuptoint(av, H);
    2117             : }
    2118             : 
    2119             : /* A0 and B0 in Q[X] */
    2120             : GEN
    2121          56 : QX_resultant(GEN A0, GEN B0)
    2122             : {
    2123             :   GEN s, a, b, A, B;
    2124          56 :   pari_sp av = avma;
    2125             : 
    2126          56 :   A = Q_primitive_part(A0, &a);
    2127          56 :   B = Q_primitive_part(B0, &b);
    2128          56 :   s = ZX_resultant(A, B);
    2129          56 :   if (!signe(s)) { set_avma(av); return gen_0; }
    2130          56 :   if (a) s = gmul(s, gpowgs(a,degpol(B)));
    2131          56 :   if (b) s = gmul(s, gpowgs(b,degpol(A)));
    2132          56 :   return gerepileupto(av, s);
    2133             : }
    2134             : 
    2135             : GEN
    2136       57239 : ZX_resultant(GEN A, GEN B) { return ZX_resultant_all(A,B,NULL,0); }
    2137             : 
    2138             : GEN
    2139           0 : QXQ_intnorm(GEN A, GEN B)
    2140             : {
    2141             :   GEN c, n, R, lB;
    2142           0 :   long dA = degpol(A), dB = degpol(B);
    2143           0 :   pari_sp av = avma;
    2144           0 :   if (dA < 0) return gen_0;
    2145           0 :   A = Q_primitive_part(A, &c);
    2146           0 :   if (!c || typ(c) == t_INT) {
    2147           0 :     n = c;
    2148           0 :     R = ZX_resultant(B, A);
    2149             :   } else {
    2150           0 :     n = gel(c,1);
    2151           0 :     R = ZX_resultant_all(B, A, gel(c,2), 0);
    2152             :   }
    2153           0 :   if (n && !equali1(n)) R = mulii(R, powiu(n, dB));
    2154           0 :   lB = leading_coeff(B);
    2155           0 :   if (!equali1(lB)) R = diviiexact(R, powiu(lB, dA));
    2156           0 :   return gerepileuptoint(av, R);
    2157             : }
    2158             : 
    2159             : GEN
    2160       19418 : QXQ_norm(GEN A, GEN B)
    2161             : {
    2162             :   GEN c, R, lB;
    2163       19418 :   long dA = degpol(A), dB = degpol(B);
    2164       19418 :   pari_sp av = avma;
    2165       19418 :   if (dA < 0) return gen_0;
    2166       19418 :   A = Q_primitive_part(A, &c);
    2167       19418 :   R = ZX_resultant(B, A);
    2168       19418 :   if (c) R = gmul(R, gpowgs(c, dB));
    2169       19418 :   lB = leading_coeff(B);
    2170       19418 :   if (!equali1(lB)) R = gdiv(R, gpowgs(lB, dA));
    2171       19418 :   return gerepileupto(av, R);
    2172             : }
    2173             : 
    2174             : /* assume x has integral coefficients */
    2175             : GEN
    2176     1194940 : ZX_disc_all(GEN x, ulong bound)
    2177             : {
    2178     1194940 :   pari_sp av = avma;
    2179     1194940 :   long s, d = degpol(x);
    2180             :   GEN l, R;
    2181             : 
    2182     1194943 :   if (d <= 1) return d == 1? gen_1: gen_0;
    2183     1191727 :   s = (d & 2) ? -1: 1;
    2184     1191727 :   l = leading_coeff(x);
    2185     1191725 :   if (!bound) bound = ZX_discbound(x);
    2186     1191656 :   R = ZX_resultant_all(x, NULL, NULL, bound);
    2187     1191716 :   if (is_pm1(l))
    2188     1016221 :   { if (signe(l) < 0) s = -s; }
    2189             :   else
    2190      175495 :     R = diviiexact(R,l);
    2191     1191716 :   if (s == -1) togglesign_safe(&R);
    2192     1191757 :   return gerepileuptoint(av,R);
    2193             : }
    2194             : 
    2195             : GEN
    2196     1133105 : ZX_disc(GEN x) { return ZX_disc_all(x,0); }
    2197             : 
    2198             : static GEN
    2199        9010 : ZXQX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, GEN T, ulong p)
    2200             : {
    2201        9010 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    2202             :   GEN H, dp;
    2203        9010 :   if (dropa && dropb) return pol0_Flx(T[1]); /* p | lc(A), p | lc(B) */
    2204        9010 :   H = FlxqX_saferesultant(a, b, T, p);
    2205        9010 :   if (!H) return NULL;
    2206        9010 :   if (dropa)
    2207             :   { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    2208           0 :     GEN c = gel(b,degB+2); /* lc(B) */
    2209           0 :     if (odd(degB)) c = Flx_neg(c, p);
    2210           0 :     c = Flxq_powu(c, dropa, T, p);
    2211           0 :     if (!Flx_equal1(c)) H = Flxq_mul(H, c, T, p);
    2212             :   }
    2213        9010 :   else if (dropb)
    2214             :   { /* multiply by lc(A)^(deg B - deg b) */
    2215           0 :     GEN c = gel(a,degA+2); /* lc(A) */
    2216           0 :     c = Flxq_powu(c, dropb, T, p);
    2217           0 :     if (!Flx_equal1(c)) H = Flxq_mul(H, c, T, p);
    2218             :   }
    2219        9010 :   dp = dB ? ZX_to_Flx(dB, p): pol1_Flx(T[1]);
    2220        9010 :   if (!Flx_equal1(dp))
    2221             :   {
    2222           0 :     GEN idp = Flxq_invsafe(dp, T, p);
    2223           0 :     if (!idp) return NULL;
    2224           0 :     H = Flxq_mul(H, Flxq_powu(idp, degA, T, p), T, p);
    2225             :   }
    2226        9010 :   return H;
    2227             : }
    2228             : 
    2229             : /* If B=NULL, assume B=A' */
    2230             : static GEN
    2231        4251 : ZXQX_resultant_slice(GEN A, GEN B, GEN U, GEN dB, GEN P, GEN *mod)
    2232             : {
    2233        4251 :   pari_sp av = avma;
    2234        4251 :   long degA, degB, i, n = lg(P)-1;
    2235             :   GEN H, T;
    2236        4251 :   long v = varn(U), redo = 0;
    2237             : 
    2238        4251 :   degA = degpol(A);
    2239        4251 :   degB = B? degpol(B): degA - 1;
    2240        4251 :   if (n == 1)
    2241             :   {
    2242        2697 :     ulong p = uel(P,1);
    2243        2697 :     GEN a = ZXX_to_FlxX(A, p, v), b = B? ZXX_to_FlxX(B, p, v): FlxX_deriv(a, p);
    2244        2697 :     GEN u = ZX_to_Flx(U, p);
    2245        2697 :     GEN Hp = ZXQX_resultant_prime(a, b, dB, degA, degB, u, p);
    2246        2697 :     if (!Hp) { set_avma(av); *mod = gen_1; return pol_0(v); }
    2247        2697 :     Hp = gerepileupto(av, Flx_to_ZX(Hp)); *mod = utoipos(p); return Hp;
    2248             :   }
    2249        1554 :   T = ZV_producttree(P);
    2250        1554 :   A = ZXX_nv_mod_tree(A, P, T, v);
    2251        1554 :   if (B) B = ZXX_nv_mod_tree(B, P, T, v);
    2252        1554 :   U = ZX_nv_mod_tree(U, P, T);
    2253        1554 :   H = cgetg(n+1, t_VEC);
    2254        7867 :   for(i=1; i <= n; i++)
    2255             :   {
    2256        6313 :     ulong p = P[i];
    2257        6313 :     GEN a = gel(A,i), b = B? gel(B,i): FlxX_deriv(a, p), u = gel(U, i);
    2258        6313 :     GEN h = ZXQX_resultant_prime(a, b, dB, degA, degB, u, p);
    2259        6313 :     if (!h)
    2260             :     {
    2261           0 :       gel(H,i) = pol_0(v);
    2262           0 :       P[i] = 1; redo = 1;
    2263             :     }
    2264             :     else
    2265        6313 :       gel(H,i) = h;
    2266             :   }
    2267        1554 :   if (redo) T = ZV_producttree(P);
    2268        1554 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2269        1554 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2270             : }
    2271             : 
    2272             : GEN
    2273        4251 : ZXQX_resultant_worker(GEN P, GEN A, GEN B, GEN T, GEN dB)
    2274             : {
    2275        4251 :   GEN V = cgetg(3, t_VEC);
    2276        4251 :   if (isintzero(B)) B = NULL;
    2277        4251 :   if (!signe(dB)) dB = NULL;
    2278        4251 :   gel(V,1) = ZXQX_resultant_slice(A, B, T, dB, P, &gel(V,2));
    2279        4251 :   return V;
    2280             : }
    2281             : 
    2282             : static ulong
    2283        3776 : ZXQX_resultant_bound_i(GEN nf, GEN A, GEN B, GEN (*f)(GEN,GEN,long))
    2284             : {
    2285        3776 :   pari_sp av = avma;
    2286        3776 :   GEN r, M = nf_L2_bound(nf, NULL, &r);
    2287        3776 :   long v = nf_get_varn(nf), i, l = lg(r);
    2288        3776 :   GEN a = cgetg(l, t_COL);
    2289       11773 :   for (i = 1; i < l; i++)
    2290        7997 :     gel(a, i) = f(gsubst(A, v, gel(r,i)), gsubst(B, v, gel(r,i)), DEFAULTPREC);
    2291        3776 :   return gc_ulong(av, (ulong) dbllog2(gmul(M,RgC_fpnorml2(a, DEFAULTPREC))));
    2292             : }
    2293             : static ulong
    2294        3468 : ZXQX_resultant_bound(GEN nf, GEN A, GEN B)
    2295        3468 : { return ZXQX_resultant_bound_i(nf, A, B, &RgX_RgXY_ResBound); }
    2296             : 
    2297             : static GEN
    2298          56 : _ZXQ_powu(GEN x, ulong u, GEN T)
    2299          56 : { return typ(x) == t_INT? powiu(x, u): ZXQ_powu(x, u, T); }
    2300             : 
    2301             : /* Compute Res(A, B/dB) in Z[X]/T, assuming A,B in Z[X,Y], dB in Z or NULL (= 1)
    2302             :  * If B=NULL, take B = A' and assume deg A > 1 */
    2303             : static GEN
    2304        3465 : ZXQX_resultant_all(GEN A, GEN B, GEN T, GEN dB, ulong bound)
    2305             : {
    2306        3465 :   pari_sp av = avma;
    2307             :   forprime_t S;
    2308             :   GEN  H, worker;
    2309        3465 :   if (B)
    2310             :   {
    2311          63 :     long a = degpol(A), b = degpol(B);
    2312          63 :     if (a < 0 || b < 0) return gen_0;
    2313          63 :     if (!a) return _ZXQ_powu(gel(A,2), b, T);
    2314          63 :     if (!b) return _ZXQ_powu(gel(B,2), a, T);
    2315             :   } else
    2316        3402 :     if (!bound) B = RgX_deriv(A);
    2317        3465 :   if (!bound) bound = ZXQX_resultant_bound(nfinit(T, DEFAULTPREC), A, B);
    2318        3465 :   worker = snm_closure(is_entry("_ZXQX_resultant_worker"),
    2319             :                        mkvec4(A, B? B: gen_0, T, dB? dB: gen_0));
    2320        3465 :   init_modular_big(&S);
    2321        3465 :   H = gen_crt("ZXQX_resultant_all", worker, &S, dB, bound, 0, NULL,
    2322             :               nxV_chinese_center, FpX_center);
    2323        3465 :   if (DEBUGLEVEL)
    2324           0 :     err_printf("ZXQX_resultant_all: a priori bound: %lu, a posteriori: %lu\n",
    2325             :                bound, expi(gsupnorm(H, DEFAULTPREC)));
    2326        3465 :   return gerepileupto(av, H);
    2327             : }
    2328             : 
    2329             : GEN
    2330         119 : nfX_resultant(GEN nf, GEN x, GEN y)
    2331             : {
    2332         119 :   pari_sp av = avma;
    2333         119 :   GEN cx, cy, D, T = nf_get_pol(nf);
    2334         119 :   long dx = degpol(x), dy = degpol(y);
    2335         119 :   if (dx < 0 || dy < 0) return gen_0;
    2336         119 :   x = Q_primitive_part(x, &cx); if (cx) cx = gpowgs(cx, dy);
    2337         119 :   y = Q_primitive_part(y, &cy); if (cy) cy = gpowgs(cy, dx);
    2338         119 :   if (!dx)      D = _ZXQ_powu(gel(x,2), dy, T);
    2339         119 :   else if (!dy) D = _ZXQ_powu(gel(y,2), dx, T);
    2340             :   else
    2341             :   {
    2342          63 :     ulong bound = ZXQX_resultant_bound(nf, x, y);
    2343          63 :     D = ZXQX_resultant_all(x, y, T, NULL, bound);
    2344             :   }
    2345         119 :   cx = mul_content(cx, cy); if (cx) D = gmul(D, cx);
    2346         119 :   return gerepileupto(av, D);
    2347             : }
    2348             : 
    2349             : static GEN
    2350         238 : to_ZX(GEN a, long v) { return typ(a)==t_INT? scalarpol(a,v): a; }
    2351             : 
    2352             : static GEN
    2353        3402 : ZXQX_disc_all(GEN x, GEN T, ulong bound)
    2354             : {
    2355        3402 :   pari_sp av = avma;
    2356        3402 :   long s, d = degpol(x), v = varn(T);
    2357             :   GEN l, R;
    2358             : 
    2359        3402 :   if (d <= 1) return d == 1? pol_1(v): pol_0(v);
    2360        3402 :   s = (d & 2) ? -1: 1;
    2361        3402 :   l = leading_coeff(x);
    2362        3402 :   R = ZXQX_resultant_all(x, NULL, T, NULL, bound);
    2363        3402 :   if (!gequal1(l)) R = QXQ_div(R, to_ZX(l,v), T);
    2364        3402 :   if (s == -1) R = RgX_neg(R);
    2365        3402 :   return gerepileupto(av, R);
    2366             : }
    2367             : 
    2368             : GEN
    2369           7 : QX_disc(GEN x)
    2370             : {
    2371           7 :   pari_sp av = avma;
    2372           7 :   GEN c, d = ZX_disc( Q_primitive_part(x, &c) );
    2373           7 :   if (c) d = gmul(d, gpowgs(c, 2*degpol(x) - 2));
    2374           7 :   return gerepileupto(av, d);
    2375             : }
    2376             : 
    2377             : GEN
    2378        3598 : nfX_disc(GEN nf, GEN x)
    2379             : {
    2380        3598 :   pari_sp av = avma;
    2381        3598 :   GEN c, D, T = nf_get_pol(nf);
    2382             :   ulong bound;
    2383        3598 :   long d = degpol(x), v = varn(T);
    2384        3598 :   if (d <= 1) return d == 1? pol_1(v): pol_0(v);
    2385        3402 :   x = Q_primitive_part(x, &c);
    2386        3402 :   bound = ZXQX_resultant_bound(nf, x, RgX_deriv(x));
    2387        3402 :   D = ZXQX_disc_all(x, T, bound);
    2388        3402 :   if (c) D = gmul(D, gpowgs(c, 2*d - 2));
    2389        3402 :   return gerepileupto(av, D);
    2390             : }
    2391             : 
    2392             : GEN
    2393      834179 : QXQ_mul(GEN x, GEN y, GEN T)
    2394             : {
    2395      834179 :   GEN dx, nx = Q_primitive_part(x, &dx);
    2396      834177 :   GEN dy, ny = Q_primitive_part(y, &dy);
    2397      834179 :   GEN z = ZXQ_mul(nx, ny, T);
    2398      834181 :   if (dx || dy)
    2399             :   {
    2400      831381 :     GEN d = dx ? dy ? gmul(dx, dy): dx : dy;
    2401      831381 :     if (!gequal1(d)) z = ZX_Q_mul(z, d);
    2402             :   }
    2403      834180 :   return z;
    2404             : }
    2405             : 
    2406             : GEN
    2407      399540 : QXQ_sqr(GEN x, GEN T)
    2408             : {
    2409      399540 :   GEN dx, nx = Q_primitive_part(x, &dx);
    2410      399540 :   GEN z = ZXQ_sqr(nx, T);
    2411      399540 :   if (dx)
    2412      397804 :     z = ZX_Q_mul(z, gsqr(dx));
    2413      399540 :   return z;
    2414             : }
    2415             : 
    2416             : static GEN
    2417      210585 : QXQ_inv_slice(GEN A, GEN B, GEN P, GEN *mod)
    2418             : {
    2419      210585 :   pari_sp av = avma;
    2420      210585 :   long i, n = lg(P)-1, v = varn(A), redo = 0;
    2421             :   GEN H, T;
    2422      210585 :   if (n == 1)
    2423             :   {
    2424      164727 :     ulong p = uel(P,1);
    2425      164727 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    2426      164727 :     GEN U = Flxq_invsafe(a, b, p);
    2427      164727 :     if (!U)
    2428             :     {
    2429          24 :       set_avma(av);
    2430          24 :       *mod = gen_1; return pol_0(v);
    2431             :     }
    2432      164703 :     H = gerepilecopy(av, Flx_to_ZX(U));
    2433      164703 :     *mod = utoipos(p); return H;
    2434             :   }
    2435       45858 :   T = ZV_producttree(P);
    2436       45858 :   A = ZX_nv_mod_tree(A, P, T);
    2437       45858 :   B = ZX_nv_mod_tree(B, P, T);
    2438       45858 :   H = cgetg(n+1, t_VEC);
    2439      226986 :   for(i=1; i <= n; i++)
    2440             :   {
    2441      181128 :     ulong p = P[i];
    2442      181128 :     GEN a = gel(A,i), b = gel(B,i);
    2443      181128 :     GEN U = Flxq_invsafe(a, b, p);
    2444      181129 :     if (!U)
    2445             :     {
    2446         601 :       gel(H,i) = pol_0(v);
    2447         601 :       P[i] = 1; redo = 1;
    2448             :     }
    2449             :     else
    2450      180528 :       gel(H,i) = U;
    2451             :   }
    2452       45858 :   if (redo) T = ZV_producttree(P);
    2453       45858 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2454       45858 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2455             : }
    2456             : 
    2457             : GEN
    2458      210585 : QXQ_inv_worker(GEN P, GEN A, GEN B)
    2459             : {
    2460      210585 :   GEN V = cgetg(3, t_VEC);
    2461      210585 :   gel(V,1) = QXQ_inv_slice(A, B, P, &gel(V,2));
    2462      210585 :   return V;
    2463             : }
    2464             : 
    2465             : /* lift(1 / Mod(A,B)). B a ZX, A a scalar or a QX */
    2466             : GEN
    2467      145377 : QXQ_inv(GEN A, GEN B)
    2468             : {
    2469             :   GEN D, Ap, Bp;
    2470             :   ulong pp;
    2471      145377 :   pari_sp av2, av = avma;
    2472             :   forprime_t S;
    2473      145377 :   GEN worker, U, H = NULL, mod = gen_1;
    2474             :   pari_timer ti;
    2475             :   long k, dA, dB;
    2476      145377 :   if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
    2477             :   /* A a QX, B a ZX */
    2478      145377 :   A = Q_primitive_part(A, &D);
    2479      145377 :   dA = degpol(A); dB= degpol(B);
    2480             :   /* A, B in Z[X] */
    2481      145377 :   init_modular_small(&S);
    2482             :   do {
    2483      145377 :     pp = u_forprime_next(&S);
    2484      145377 :     Ap = ZX_to_Flx(A, pp);
    2485      145377 :     Bp = ZX_to_Flx(B, pp);
    2486      145377 :   } while (degpol(Ap) != dA || degpol(Bp) != dB);
    2487      145377 :   if (degpol(Flx_gcd(Ap, Bp, pp)) != 0 && degpol(ZX_gcd(A,B))!=0)
    2488          14 :     pari_err_INV("QXQ_inv",mkpolmod(A,B));
    2489      145363 :   worker = snm_closure(is_entry("_QXQ_inv_worker"), mkvec2(A, B));
    2490      145363 :   av2 = avma;
    2491      145363 :   for (k = 1; ;k *= 2)
    2492       41829 :   {
    2493             :     GEN res, b, N, den;
    2494      187192 :     gen_inccrt_i("QXQ_inv", worker, NULL, (k+1)>>1, 0, &S, &H, &mod,
    2495             :                  nxV_chinese_center, FpX_center);
    2496      187192 :     gerepileall(av2, 2, &H, &mod);
    2497      187192 :     b = sqrti(shifti(mod,-1));
    2498      187192 :     if (DEBUGLEVEL>5) timer_start(&ti);
    2499      187192 :     U = FpX_ratlift(H, mod, b, b, NULL);
    2500      187192 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: ratlift");
    2501      192837 :     if (!U) continue;
    2502      151008 :     N = Q_remove_denom(U, &den); if (!den) den = gen_1;
    2503      151008 :     res = Flx_rem(Flx_Fl_sub(Flx_mul(Ap, ZX_to_Flx(N,pp), pp),
    2504             :                   umodiu(den, pp), pp), Bp, pp);
    2505      151008 :     if (degpol(res) >= 0) continue;
    2506      145363 :     res = ZX_Z_sub(ZX_mul(A, N), den);
    2507      145363 :     res = ZX_is_monic(B) ? ZX_rem(res, B): RgX_pseudorem(res, B);
    2508      145363 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: final check");
    2509      145363 :     if (degpol(res)<0)
    2510             :     {
    2511      145363 :       if (D) U = RgX_Rg_div(U, D);
    2512      145363 :       return gerepilecopy(av, U);
    2513             :     }
    2514             :   }
    2515             : }
    2516             : 
    2517             : static GEN
    2518      119755 : QXQ_div_slice(GEN A, GEN B, GEN C, GEN P, GEN *mod)
    2519             : {
    2520      119755 :   pari_sp av = avma;
    2521      119755 :   long i, n = lg(P)-1, v = varn(A), redo = 0;
    2522             :   GEN H, T;
    2523      119755 :   if (n == 1)
    2524             :   {
    2525       43757 :     ulong p = uel(P,1);
    2526       43757 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p), c = ZX_to_Flx(C, p);
    2527       43757 :     GEN bi = Flxq_invsafe(b, c, p), U;
    2528       43757 :     if (!bi)
    2529             :     {
    2530           0 :       set_avma(av);
    2531           0 :       *mod = gen_1; return pol_0(v);
    2532             :     }
    2533       43757 :     U = Flxq_mul(a, bi, c, p);
    2534       43757 :     H = gerepilecopy(av, Flx_to_ZX(U));
    2535       43757 :     *mod = utoipos(p); return H;
    2536             :   }
    2537       75998 :   T = ZV_producttree(P);
    2538       75998 :   A = ZX_nv_mod_tree(A, P, T);
    2539       75998 :   B = ZX_nv_mod_tree(B, P, T);
    2540       75998 :   C = ZX_nv_mod_tree(C, P, T);
    2541       75998 :   H = cgetg(n+1, t_VEC);
    2542      334547 :   for(i=1; i <= n; i++)
    2543             :   {
    2544      258549 :     ulong p = P[i];
    2545      258549 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C, i);
    2546      258549 :     GEN bi = Flxq_invsafe(b, c, p);
    2547      258551 :     if (!bi)
    2548             :     {
    2549           4 :       gel(H,i) = pol_0(v);
    2550           4 :       P[i] = 1; redo = 1;
    2551             :     }
    2552             :     else
    2553      258547 :       gel(H,i) = Flxq_mul(a, bi, c, p);
    2554             :   }
    2555       75998 :   if (redo) T = ZV_producttree(P);
    2556       75998 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2557       75998 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2558             : }
    2559             : 
    2560             : GEN
    2561      119755 : QXQ_div_worker(GEN P, GEN A, GEN B, GEN C)
    2562             : {
    2563      119755 :   GEN V = cgetg(3, t_VEC);
    2564      119755 :   gel(V,1) = QXQ_div_slice(A, B, C, P, &gel(V,2));
    2565      119755 :   return V;
    2566             : }
    2567             : 
    2568             : /* lift(Mod(A/B, C)). C a ZX, A, B a scalar or a QX */
    2569             : GEN
    2570       32416 : QXQ_div(GEN A, GEN B, GEN C)
    2571             : {
    2572             :   GEN DA, DB, Ap, Bp, Cp;
    2573             :   ulong pp;
    2574       32416 :   pari_sp av2, av = avma;
    2575             :   forprime_t S;
    2576       32416 :   GEN worker, U, H = NULL, mod = gen_1;
    2577             :   pari_timer ti;
    2578             :   long k, dA, dB, dC;
    2579       32416 :   if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
    2580             :   /* A a QX, B a ZX */
    2581       32416 :   A = Q_primitive_part(A, &DA);
    2582       32416 :   B = Q_primitive_part(B, &DB);
    2583       32416 :   dA = degpol(A); dB = degpol(B); dC = degpol(C);
    2584             :   /* A, B in Z[X] */
    2585       32416 :   init_modular_small(&S);
    2586             :   do {
    2587       32416 :     pp = u_forprime_next(&S);
    2588       32416 :     Ap = ZX_to_Flx(A, pp);
    2589       32416 :     Bp = ZX_to_Flx(B, pp);
    2590       32416 :     Cp = ZX_to_Flx(C, pp);
    2591       32416 :   } while (degpol(Ap) != dA || degpol(Bp) != dB || degpol(Cp) != dC);
    2592       32416 :   if (degpol(Flx_gcd(Bp, Cp, pp)) != 0 && degpol(ZX_gcd(B,C))!=0)
    2593           0 :     pari_err_INV("QXQ_div",mkpolmod(B,C));
    2594       32416 :   worker = snm_closure(is_entry("_QXQ_div_worker"), mkvec3(A, B, C));
    2595       32416 :   av2 = avma;
    2596       32416 :   for (k = 1; ;k *= 2)
    2597       46415 :   {
    2598             :     GEN res, b, N, den;
    2599       78831 :     gen_inccrt_i("QXQ_div", worker, NULL, (k+1)>>1, 0, &S, &H, &mod,
    2600             :                  nxV_chinese_center, FpX_center);
    2601       78831 :     gerepileall(av2, 2, &H, &mod);
    2602       78831 :     b = sqrti(shifti(mod,-1));
    2603       78831 :     if (DEBUGLEVEL>5) timer_start(&ti);
    2604       78831 :     U = FpX_ratlift(H, mod, b, b, NULL);
    2605       78831 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_div: ratlift");
    2606       89433 :     if (!U) continue;
    2607       43018 :     N = Q_remove_denom(U, &den); if (!den) den = gen_1;
    2608       43018 :     res = Flx_rem(Flx_sub(Flx_mul(Bp, ZX_to_Flx(N,pp), pp),
    2609             :                           Flx_Fl_mul(Ap, umodiu(den, pp), pp), pp), Cp, pp);
    2610       43018 :     if (degpol(res) >= 0) continue;
    2611       32416 :     res = ZX_sub(ZX_mul(B, N), ZX_Z_mul(A,den));
    2612       32416 :     res = ZX_is_monic(C) ? ZX_rem(res, C): RgX_pseudorem(res, C);
    2613       32416 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_div: final check");
    2614       32416 :     if (degpol(res)<0)
    2615             :     {
    2616       32416 :       if (DA && DB) U = RgX_Rg_mul(U, gdiv(DA,DB));
    2617       27523 :       else if (DA) U = RgX_Rg_mul(U, DA);
    2618       15498 :       else if (DB) U = RgX_Rg_div(U, DB);
    2619       32416 :       return gerepilecopy(av, U);
    2620             :     }
    2621             :   }
    2622             : }
    2623             : 
    2624             : /************************************************************************
    2625             :  *                                                                      *
    2626             :  *                           ZXQ_minpoly                                *
    2627             :  *                                                                      *
    2628             :  ************************************************************************/
    2629             : 
    2630             : static GEN
    2631        3523 : ZXQ_minpoly_slice(GEN A, GEN B, long d, GEN P, GEN *mod)
    2632             : {
    2633        3523 :   pari_sp av = avma;
    2634        3523 :   long i, n = lg(P)-1, v = evalvarn(varn(B));
    2635             :   GEN H, T;
    2636        3523 :   if (n == 1)
    2637             :   {
    2638         716 :     ulong p = uel(P,1);
    2639         716 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    2640         716 :     GEN Hp = Flxq_minpoly(a, b, p);
    2641         716 :     if (degpol(Hp) != d) { p = 1; Hp = pol0_Flx(v); }
    2642         716 :     H = gerepileupto(av, Flx_to_ZX(Hp));
    2643         716 :     *mod = utoipos(p); return H;
    2644             :   }
    2645        2807 :   T = ZV_producttree(P);
    2646        2807 :   A = ZX_nv_mod_tree(A, P, T);
    2647        2807 :   B = ZX_nv_mod_tree(B, P, T);
    2648        2807 :   H = cgetg(n+1, t_VEC);
    2649       16838 :   for(i=1; i <= n; i++)
    2650             :   {
    2651       14031 :     ulong p = P[i];
    2652       14031 :     GEN a = gel(A,i), b = gel(B,i);
    2653       14031 :     GEN m = Flxq_minpoly(a, b, p);
    2654       14031 :     if (degpol(m) != d) { P[i] = 1; m = pol0_Flx(v); }
    2655       14031 :     gel(H, i) = m;
    2656             :   }
    2657        2807 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2658        2807 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2659             : }
    2660             : 
    2661             : GEN
    2662        3523 : ZXQ_minpoly_worker(GEN P, GEN A, GEN B, long d)
    2663             : {
    2664        3523 :   GEN V = cgetg(3, t_VEC);
    2665        3523 :   gel(V,1) = ZXQ_minpoly_slice(A, B, d, P, &gel(V,2));
    2666        3523 :   return V;
    2667             : }
    2668             : 
    2669             : GEN
    2670        1701 : ZXQ_minpoly(GEN A, GEN B, long d, ulong bound)
    2671             : {
    2672        1701 :   pari_sp av = avma;
    2673             :   GEN worker, H, dB;
    2674             :   forprime_t S;
    2675        1701 :   B = Q_remove_denom(B, &dB);
    2676        1701 :   worker = strtoclosure("_ZXQ_minpoly_worker", 3, A, B, stoi(d));
    2677        1701 :   init_modular_big(&S);
    2678        1701 :   H = gen_crt("ZXQ_minpoly", worker, &S, dB, bound, 0, NULL,
    2679             :                nxV_chinese_center, FpX_center_i);
    2680        1701 :   return gerepilecopy(av, H);
    2681             : }
    2682             : 
    2683             : /************************************************************************
    2684             :  *                                                                      *
    2685             :  *                   ZX_ZXY_resultant                                   *
    2686             :  *                                                                      *
    2687             :  ************************************************************************/
    2688             : 
    2689             : static GEN
    2690      364667 : ZX_ZXY_resultant_prime(GEN a, GEN b, ulong dp, ulong p,
    2691             :                        long degA, long degB, long dres, long sX)
    2692             : {
    2693      364667 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    2694      364665 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    2695      364665 :   GEN Hp = Flx_FlxY_resultant_polint(a, b, p, pi, dres, sX);
    2696      364667 :   if (dropa && dropb)
    2697           0 :     Hp = zero_Flx(sX);
    2698             :   else {
    2699      364667 :     if (dropa)
    2700             :     { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    2701           0 :       GEN c = gel(b,degB+2); /* lc(B) */
    2702           0 :       if (odd(degB)) c = Flx_neg(c, p);
    2703           0 :       if (!Flx_equal1(c)) {
    2704           0 :         c = Flx_powu_pre(c, dropa, p, pi);
    2705           0 :         if (!Flx_equal1(c)) Hp = Flx_mul_pre(Hp, c, p, pi);
    2706             :       }
    2707             :     }
    2708      364667 :     else if (dropb)
    2709             :     { /* multiply by lc(A)^(deg B - deg b) */
    2710           0 :       ulong c = uel(a, degA+2); /* lc(A) */
    2711           0 :       c = Fl_powu(c, dropb, p);
    2712           0 :       if (c != 1) Hp = Flx_Fl_mul_pre(Hp, c, p, pi);
    2713             :     }
    2714             :   }
    2715      364667 :   if (dp != 1) Hp = Flx_Fl_mul_pre(Hp, Fl_powu_pre(Fl_inv(dp,p), degA, p, pi), p, pi);
    2716      364666 :   return Hp;
    2717             : }
    2718             : 
    2719             : static GEN
    2720      124895 : ZX_ZXY_resultant_slice(GEN A, GEN B, GEN dB, long degA, long degB, long dres,
    2721             :                        GEN P, GEN *mod, long sX, long vY)
    2722             : {
    2723      124895 :   pari_sp av = avma;
    2724      124895 :   long i, n = lg(P)-1;
    2725             :   GEN H, T, D;
    2726      124895 :   if (n == 1)
    2727             :   {
    2728       40201 :     ulong p = uel(P,1);
    2729       40201 :     ulong dp = dB ? umodiu(dB, p): 1;
    2730       40201 :     GEN a = ZX_to_Flx(A, p), b = ZXX_to_FlxX(B, p, vY);
    2731       40201 :     GEN Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2732       40201 :     H = gerepileupto(av, Flx_to_ZX(Hp));
    2733       40201 :     *mod = utoipos(p); return H;
    2734             :   }
    2735       84694 :   T = ZV_producttree(P);
    2736       84694 :   A = ZX_nv_mod_tree(A, P, T);
    2737       84694 :   B = ZXX_nv_mod_tree(B, P, T, vY);
    2738       84694 :   D = dB ? Z_ZV_mod_tree(dB, P, T): NULL;
    2739       84694 :   H = cgetg(n+1, t_VEC);
    2740      363800 :   for(i=1; i <= n; i++)
    2741             :   {
    2742      279106 :     ulong p = P[i];
    2743      279106 :     GEN a = gel(A,i), b = gel(B,i);
    2744      279106 :     ulong dp = D ? uel(D, i): 1;
    2745      279106 :     gel(H,i) = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2746             :   }
    2747       84694 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2748       84694 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2749             : }
    2750             : 
    2751             : GEN
    2752      124895 : ZX_ZXY_resultant_worker(GEN P, GEN A, GEN B, GEN dB, GEN v)
    2753             : {
    2754      124895 :   GEN V = cgetg(3, t_VEC);
    2755      124895 :   if (isintzero(dB)) dB = NULL;
    2756      124895 :   gel(V,1) = ZX_ZXY_resultant_slice(A, B, dB, v[1], v[2], v[3], P, &gel(V,2), v[4], v[5]);
    2757      124895 :   return V;
    2758             : }
    2759             : 
    2760             : GEN
    2761       79162 : ZX_ZXY_resultant(GEN A, GEN B)
    2762             : {
    2763       79162 :   pari_sp av = avma;
    2764             :   forprime_t S;
    2765             :   ulong bound;
    2766       79162 :   long v = fetch_var_higher();
    2767       79162 :   long degA = degpol(A), degB, dres = degA * degpol(B);
    2768       79162 :   long vX = varn(B), vY = varn(A); /* assume vY has lower priority */
    2769       79162 :   long sX = evalvarn(vX);
    2770             :   GEN worker, H, dB;
    2771       79162 :   B = Q_remove_denom(B, &dB);
    2772       79161 :   if (!dB) B = leafcopy(B);
    2773       79161 :   A = leafcopy(A); setvarn(A,v);
    2774       79162 :   B = swap_vars(B, vY); setvarn(B,v); degB = degpol(B);
    2775       79161 :   bound = ZX_ZXY_ResBound(A, B, dB);
    2776       79160 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    2777      158320 :   worker = snm_closure(is_entry("_ZX_ZXY_resultant_worker"),
    2778       79160 :                        mkvec4(A, B, dB? dB: gen_0,
    2779             :                               mkvecsmall5(degA, degB, dres, sX, vY)));
    2780       79161 :   init_modular_big(&S);
    2781       79161 :   H = gen_crt("ZX_ZXY_resultant_all", worker, &S, dB, bound, 0, NULL,
    2782             :                nxV_chinese_center, FpX_center_i);
    2783       79162 :   setvarn(H, vX); (void)delete_var();
    2784       79162 :   return gerepilecopy(av, H);
    2785             : }
    2786             : 
    2787             : static long
    2788       40536 : ZX_ZXY_rnfequation_lambda(GEN A, GEN B0, long lambda)
    2789             : {
    2790       40536 :   pari_sp av = avma;
    2791       40536 :   long degA = degpol(A), degB, dres = degA*degpol(B0);
    2792       40536 :   long v = fetch_var_higher();
    2793       40537 :   long vX = varn(B0), vY = varn(A); /* assume vY has lower priority */
    2794       40537 :   long sX = evalvarn(vX);
    2795             :   GEN dB, B, a, b, Hp;
    2796             :   forprime_t S;
    2797             : 
    2798       40537 :   B0 = Q_remove_denom(B0, &dB);
    2799       40537 :   if (!dB) B0 = leafcopy(B0);
    2800       40537 :   A = leafcopy(A);
    2801       40537 :   B = B0;
    2802       40537 :   setvarn(A,v);
    2803       45361 : INIT:
    2804       45361 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    2805       45361 :   B = swap_vars(B, vY); setvarn(B,v);
    2806             :   /* B0(lambda v + x, v) */
    2807       45361 :   if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    2808             : 
    2809       45361 :   degB = degpol(B);
    2810       45361 :   init_modular_big(&S);
    2811             :   while (1)
    2812           0 :   {
    2813       45361 :     ulong p = u_forprime_next(&S);
    2814       45361 :     ulong dp = dB ? umodiu(dB, p): 1;
    2815       45361 :     if (!dp) continue;
    2816       45361 :     a = ZX_to_Flx(A, p);
    2817       45361 :     b = ZXX_to_FlxX(B, p, v);
    2818       45361 :     Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2819       45361 :     if (degpol(Hp) != dres) continue;
    2820       45361 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    2821       45361 :     if (!Flx_is_squarefree(Hp, p)) { lambda = next_lambda(lambda); goto INIT; }
    2822       40537 :     if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    2823       40537 :     (void)delete_var(); return gc_long(av,lambda);
    2824             :   }
    2825             : }
    2826             : 
    2827             : GEN
    2828       60527 : ZX_ZXY_rnfequation(GEN A, GEN B, long *lambda)
    2829             : {
    2830       60527 :   if (lambda)
    2831             :   {
    2832       40536 :     *lambda = ZX_ZXY_rnfequation_lambda(A, B, *lambda);
    2833       40537 :     if (*lambda) B = RgX_translate(B, monomial(stoi(*lambda), 1, varn(A)));
    2834             :   }
    2835       60528 :   return ZX_ZXY_resultant(A,B);
    2836             : }
    2837             : 
    2838             : static GEN
    2839       10351 : ZX_composedsum_slice(GEN A, GEN B, GEN P, GEN *mod)
    2840             : {
    2841       10351 :   pari_sp av = avma;
    2842       10351 :   long i, n = lg(P)-1;
    2843             :   GEN H, T;
    2844       10351 :   if (n == 1)
    2845             :   {
    2846        9849 :     ulong p = uel(P,1);
    2847        9849 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    2848        9845 :     GEN Hp = Flx_composedsum(a, b, p);
    2849        9844 :     H = gerepileupto(av, Flx_to_ZX(Hp));
    2850        9847 :     *mod = utoipos(p); return H;
    2851             :   }
    2852         502 :   T = ZV_producttree(P);
    2853         502 :   A = ZX_nv_mod_tree(A, P, T);
    2854         502 :   B = ZX_nv_mod_tree(B, P, T);
    2855         502 :   H = cgetg(n+1, t_VEC);
    2856        4526 :   for(i=1; i <= n; i++)
    2857             :   {
    2858        4024 :     ulong p = P[i];
    2859        4024 :     GEN a = gel(A,i), b = gel(B,i);
    2860        4024 :     gel(H,i) = Flx_composedsum(a, b, p);
    2861             :   }
    2862         502 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2863         502 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2864             : }
    2865             : 
    2866             : GEN
    2867       10351 : ZX_composedsum_worker(GEN P, GEN A, GEN B)
    2868             : {
    2869       10351 :   GEN V = cgetg(3, t_VEC);
    2870       10351 :   gel(V,1) = ZX_composedsum_slice(A, B, P, &gel(V,2));
    2871       10349 :   return V;
    2872             : }
    2873             : 
    2874             : static GEN
    2875       10085 : ZX_composedsum_i(GEN A, GEN B, GEN lead)
    2876             : {
    2877       10085 :   pari_sp av = avma;
    2878             :   forprime_t S;
    2879             :   ulong bound;
    2880             :   GEN H, worker, mod;
    2881       10085 :   if (degpol(A) < degpol(B)) swap(A, B);
    2882       10083 :   if (!lead) lead  = mulii(leading_coeff(A),leading_coeff(B));
    2883       10083 :   bound = ZX_ZXY_ResBound_1(A, B);
    2884       10085 :   worker = snm_closure(is_entry("_ZX_composedsum_worker"), mkvec2(A,B));
    2885       10087 :   init_modular_big(&S);
    2886       10084 :   H = gen_crt("ZX_composedsum", worker, &S, lead, bound, 0, &mod,
    2887             :               nxV_chinese_center, FpX_center);
    2888       10085 :   return gerepileupto(av, H);
    2889             : }
    2890             : 
    2891             : static long
    2892        9697 : ZX_compositum_lambda(GEN A, GEN B, GEN lead, long lambda)
    2893             : {
    2894        9697 :   pari_sp av = avma;
    2895             :   forprime_t S;
    2896             :   ulong p;
    2897        9697 :   init_modular_big(&S);
    2898        9699 :   p = u_forprime_next(&S);
    2899             :   while (1)
    2900         112 :   {
    2901             :     GEN Hp, a;
    2902        9810 :     if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    2903        9810 :     if (lead && dvdiu(lead,p)) { p = u_forprime_next(&S); continue; }
    2904        9803 :     a = ZX_to_Flx(ZX_rescale(A, stoi(-lambda)), p);
    2905        9805 :     Hp = Flx_composedsum(a, ZX_to_Flx(B, p), p);
    2906        9803 :     if (!Flx_is_squarefree(Hp, p)) { lambda = next_lambda(lambda); continue; }
    2907        9694 :     if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    2908        9694 :     return gc_long(av, lambda);
    2909             :   }
    2910             : }
    2911             : 
    2912             : GEN
    2913        9698 : ZX_compositum(GEN A, GEN B, long *lambda)
    2914             : {
    2915        9698 :   GEN lead  = mulii(leading_coeff(A),leading_coeff(B));
    2916        9697 :   if (lambda)
    2917             :   {
    2918        9697 :     *lambda = ZX_compositum_lambda(A, B, lead, *lambda);
    2919        9694 :     A = ZX_rescale(A, stoi(-*lambda));
    2920             :   }
    2921        9700 :   return ZX_composedsum_i(A, B, lead);
    2922             : }
    2923             : 
    2924             : GEN
    2925         385 : ZX_composedsum(GEN A, GEN B)
    2926         385 : { return ZX_composedsum_i(A, B, NULL); }
    2927             : 
    2928             : static GEN
    2929         352 : ZXQX_composedsum_slice(GEN A, GEN B, GEN C, GEN P, GEN *mod)
    2930             : {
    2931         352 :   pari_sp av = avma;
    2932         352 :   long i, n = lg(P)-1, dC = degpol(C), v = varn(C);
    2933             :   GEN H, T;
    2934         352 :   if (n == 1)
    2935             :   {
    2936         174 :     ulong p = uel(P,1);
    2937         174 :     GEN a = ZXX_to_FlxX(A, p, v), b = ZXX_to_FlxX(B, p, v);
    2938         174 :     GEN c = ZX_to_Flx(C, p);
    2939         174 :     GEN Hp = FlxX_to_Flm(FlxqX_composedsum(a, b, c, p), dC);
    2940         174 :     H = gerepileupto(av, Flm_to_ZM(Hp));
    2941         174 :     *mod = utoipos(p); return H;
    2942             :   }
    2943         178 :   T = ZV_producttree(P);
    2944         178 :   A = ZXX_nv_mod_tree(A, P, T, v);
    2945         178 :   B = ZXX_nv_mod_tree(B, P, T, v);
    2946         178 :   C = ZX_nv_mod_tree(C, P, T);
    2947         178 :   H = cgetg(n+1, t_VEC);
    2948         660 :   for(i=1; i <= n; i++)
    2949             :   {
    2950         482 :     ulong p = P[i];
    2951         482 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C,i);
    2952         482 :     gel(H,i) = FlxX_to_Flm(FlxqX_composedsum(a, b, c, p), dC);
    2953             :   }
    2954         178 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
    2955         178 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2956             : }
    2957             : 
    2958             : GEN
    2959         352 : ZXQX_composedsum_worker(GEN P, GEN A, GEN B, GEN C)
    2960             : {
    2961         352 :   GEN V = cgetg(3, t_VEC);
    2962         352 :   gel(V,1) = ZXQX_composedsum_slice(A, B, C, P, &gel(V,2));
    2963         352 :   return V;
    2964             : }
    2965             : 
    2966             : static GEN
    2967         308 : ZXQX_composedsum(GEN A, GEN B, GEN T, ulong bound)
    2968             : {
    2969         308 :   pari_sp av = avma;
    2970             :   forprime_t S;
    2971             :   GEN H, worker, mod;
    2972         308 :   GEN lead = mulii(Q_content(leading_coeff(A)), Q_content(leading_coeff(B)));
    2973         308 :   worker = snm_closure(is_entry("_ZXQX_composedsum_worker")
    2974             :                       , mkvec3(A,B,T));
    2975         308 :   init_modular_big(&S);
    2976         308 :   H = gen_crt("ZXQX_composedsum", worker, &S, lead, bound, 0, &mod,
    2977             :               nmV_chinese_center, FpM_center);
    2978         308 :   if (DEBUGLEVEL > 4)
    2979           0 :     err_printf("nfcompositum: a priori bound: %lu, a posteriori: %lu\n",
    2980             :                bound, expi(gsupnorm(H, DEFAULTPREC)));
    2981         308 :   return gerepilecopy(av, RgM_to_RgXX(H, varn(A), varn(T)));
    2982             : }
    2983             : 
    2984             : static long
    2985         308 : ZXQX_composedsum_bound(GEN nf, GEN A, GEN B)
    2986         308 : { return ZXQX_resultant_bound_i(nf, A, B, &RgX_RgXY_ResBound_1); }
    2987             : 
    2988             : GEN
    2989         308 : nf_direct_compositum(GEN nf, GEN A, GEN B)
    2990             : {
    2991         308 :   ulong bnd = ZXQX_composedsum_bound(nf, A, B);
    2992         308 :   return ZXQX_composedsum(A, B, nf_get_pol(nf), bnd);
    2993             : }
    2994             : 
    2995             : /************************************************************************
    2996             :  *                                                                      *
    2997             :  *                   IRREDUCIBLE POLYNOMIAL / Fp                        *
    2998             :  *                                                                      *
    2999             :  ************************************************************************/
    3000             : 
    3001             : /* irreducible (unitary) polynomial of degree n over Fp */
    3002             : GEN
    3003           0 : ffinit_rand(GEN p,long n)
    3004             : {
    3005           0 :   for(;;) {
    3006           0 :     pari_sp av = avma;
    3007           0 :     GEN pol = ZX_add(pol_xn(n, 0), random_FpX(n-1,0, p));
    3008           0 :     if (FpX_is_irred(pol, p)) return pol;
    3009           0 :     set_avma(av);
    3010             :   }
    3011             : }
    3012             : 
    3013             : /* return an extension of degree 2^l of F_2, assume l > 0
    3014             :  * Not stack clean. */
    3015             : static GEN
    3016         606 : ffinit_Artin_Schreier_2(long l)
    3017             : {
    3018             :   GEN Q, T, S;
    3019             :   long i, v;
    3020             : 
    3021         606 :   if (l == 1) return mkvecsmall4(0,1,1,1); /*x^2 + x + 1*/
    3022         557 :   v = fetch_var_higher();
    3023         557 :   S = mkvecsmall5(0, 0, 0, 1, 1); /* y(y^2 + y) */
    3024         557 :   Q = mkpoln(3, pol1_Flx(0), pol1_Flx(0), S); /* x^2 + x + y(y^2+y) */
    3025         557 :   setvarn(Q, v);
    3026             : 
    3027             :   /* x^4+x+1, irred over F_2, minimal polynomial of a root of Q */
    3028         557 :   T = mkvecsmalln(6,evalvarn(v),1UL,1UL,0UL,0UL,1UL);
    3029             :   /* Q = x^2 + x + a(y) irred. over K = F2[y] / (T(y))
    3030             :    * ==> x^2 + x + a(y) b irred. over K for any root b of Q
    3031             :    * ==> x^2 + x + (b^2+b)b */
    3032        3075 :   for (i=2; i<l; i++) T = Flx_FlxY_resultant(T, Q, 2); /* minpoly(b) / F2*/
    3033         557 :   (void)delete_var(); T[1] = 0; return T;
    3034             : }
    3035             : 
    3036             : /* return an extension of degree p^l of F_p, assume l > 0
    3037             :  * Not stack clean. */
    3038             : GEN
    3039         963 : ffinit_Artin_Schreier(ulong p, long l)
    3040             : {
    3041             :   long i, v;
    3042             :   GEN Q, R, S, T, xp;
    3043         963 :   if (p==2) return ffinit_Artin_Schreier_2(l);
    3044         357 :   xp = polxn_Flx(p,0); /* x^p */
    3045         357 :   T = Flx_sub(xp, mkvecsmall3(0,1,1),p); /* x^p - x - 1 */
    3046         357 :   if (l == 1) return T;
    3047             : 
    3048           7 :   v = evalvarn(fetch_var_higher());
    3049           7 :   xp[1] = v;
    3050           7 :   R = Flx_sub(polxn_Flx(2*p-1,0), polxn_Flx(p,0),p);
    3051           7 :   S = Flx_sub(xp, polx_Flx(0), p);
    3052           7 :   Q = FlxX_Flx_sub(Flx_to_FlxX(S, v), R, p); /* x^p - x - (y^(2p-1)-y^p) */
    3053          14 :   for (i = 2; i <= l; ++i) T = Flx_FlxY_resultant(T, Q, p);
    3054           7 :   (void)delete_var(); T[1] = 0; return T;
    3055             : }
    3056             : 
    3057             : static long
    3058      148779 : flinit_check(ulong p, long n, long l)
    3059             : {
    3060             :   ulong q;
    3061      148779 :   if (!uisprime(n)) return 0;
    3062      102020 :   q = p % n; if (!q) return 0;
    3063       99479 :   return ugcd((n-1)/Fl_order(q, n-1, n), l) == 1;
    3064             : }
    3065             : 
    3066             : static GEN
    3067       31776 : flinit(ulong p, long l)
    3068             : {
    3069       31776 :   ulong n = 1+l;
    3070       95935 :   while (!flinit_check(p,n,l)) n += l;
    3071       31776 :   if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
    3072       31776 :   return ZX_to_Flx(polsubcyclo(n,l,0), p);
    3073             : }
    3074             : 
    3075             : static GEN
    3076       28913 : ffinit_fact_Flx(ulong p, long n)
    3077             : {
    3078       28913 :   GEN P, F = factoru_pow(n), Fp = gel(F,1), Fe = gel(F,2), Fm = gel(F,3);
    3079       28913 :   long i, l = lg(Fm);
    3080       28913 :   P = cgetg(l, t_VEC);
    3081       61652 :   for (i = 1; i < l; i++)
    3082       32739 :     gel(P,i) = p==uel(Fp,i) ? ffinit_Artin_Schreier(p, Fe[i])
    3083       32739 :                             : flinit(p, uel(Fm,i));
    3084       28913 :   return FlxV_composedsum(P, p);
    3085             : }
    3086             : 
    3087             : static GEN
    3088       52851 : init_Flxq_i(ulong p, long n, long sv)
    3089             : {
    3090             :   GEN P;
    3091       52851 :   if (!odd(p) && p != 2) pari_err_PRIME("ffinit", utoi(p));
    3092       52844 :   if (n == 1) return polx_Flx(sv);
    3093       52844 :   if (flinit_check(p, n+1, n))
    3094             :   {
    3095       23931 :     P = const_vecsmall(n+2,1);
    3096       23931 :     P[1] = sv; return P;
    3097             :   }
    3098       28913 :   P = ffinit_fact_Flx(p,n);
    3099       28913 :   P[1] = sv; return P;
    3100             : }
    3101             : 
    3102             : GEN
    3103           0 : init_Flxq(ulong p, long n, long v)
    3104             : {
    3105           0 :   pari_sp av = avma;
    3106           0 :   return gerepileupto(av, init_Flxq_i(p, n, v));
    3107             : }
    3108             : 
    3109             : /* check if polsubcyclo(n,l,0) is irreducible modulo p */
    3110             : static long
    3111        7185 : fpinit_check(GEN p, long n, long l)
    3112             : {
    3113             :   ulong q;
    3114        7185 :   if (!uisprime(n)) return 0;
    3115        4450 :   q = umodiu(p,n); if (!q) return 0;
    3116        4450 :   return ugcd((n-1)/Fl_order(q, n-1, n), l) == 1;
    3117             : }
    3118             : 
    3119             : /* let k=2 if p%4==1, and k=4 else and assume k*p does not divide l.
    3120             :  * Return an irreducible polynomial of degree l over F_p.
    3121             :  * Variant of Adleman and Lenstra "Finding irreducible polynomials over
    3122             :  * finite fields", ACM, 1986 (5) 350--355.
    3123             :  * Not stack clean */
    3124             : static GEN
    3125        1653 : fpinit(GEN p, long l)
    3126             : {
    3127        1653 :   ulong n = 1+l;
    3128        5202 :   while (!fpinit_check(p,n,l)) n += l;
    3129        1653 :   if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
    3130        1653 :   return FpX_red(polsubcyclo(n,l,0),p);
    3131             : }
    3132             : 
    3133             : static GEN
    3134        1574 : ffinit_fact(GEN p, long n)
    3135             : {
    3136        1574 :   GEN P, F = factoru_pow(n), Fp = gel(F,1), Fe = gel(F,2), Fm = gel(F,3);
    3137        1574 :   long i, l = lg(Fm);
    3138        1574 :   P = cgetg(l, t_VEC);
    3139        3227 :   for (i = 1; i < l; ++i)
    3140        3306 :     gel(P,i) = absequaliu(p, Fp[i]) ?
    3141           0 :                  Flx_to_ZX(ffinit_Artin_Schreier(Fp[i], Fe[i]))
    3142        1653 :                : fpinit(p, Fm[i]);
    3143        1574 :   return FpXV_composedsum(P, p);
    3144             : }
    3145             : 
    3146             : static GEN
    3147       55100 : init_Fq_i(GEN p, long n, long v)
    3148             : {
    3149             :   GEN P;
    3150       55100 :   if (n <= 0) pari_err_DOMAIN("ffinit", "degree", "<=", gen_0, stoi(n));
    3151       55100 :   if (typ(p) != t_INT) pari_err_TYPE("ffinit",p);
    3152       55100 :   if (cmpiu(p, 2) < 0) pari_err_PRIME("ffinit",p);
    3153       55093 :   if (v < 0) v = 0;
    3154       55093 :   if (n == 1) return pol_x(v);
    3155       54841 :   if (lgefint(p) == 3)
    3156       52851 :     return Flx_to_ZX(init_Flxq_i(p[2], n, evalvarn(v)));
    3157        1990 :   if (!mpodd(p)) pari_err_PRIME("ffinit", p);
    3158        1983 :   if (fpinit_check(p, n+1, n)) return polcyclo(n+1, v);
    3159        1574 :   P = ffinit_fact(p,n);
    3160        1574 :   setvarn(P, v); return P;
    3161             : }
    3162             : GEN
    3163       54533 : init_Fq(GEN p, long n, long v)
    3164             : {
    3165       54533 :   pari_sp av = avma;
    3166       54533 :   return gerepileupto(av, init_Fq_i(p, n, v));
    3167             : }
    3168             : GEN
    3169         567 : ffinit(GEN p, long n, long v)
    3170             : {
    3171         567 :   pari_sp av = avma;
    3172         567 :   return gerepileupto(av, FpX_to_mod(init_Fq_i(p, n, v), p));
    3173             : }
    3174             : 
    3175             : GEN
    3176        3178 : ffnbirred(GEN p, long n)
    3177             : {
    3178        3178 :   pari_sp av = avma;
    3179        3178 :   GEN s = powiu(p,n), F = factoru(n), D = divisorsu_moebius(gel(F, 1));
    3180        3178 :   long j, l = lg(D);
    3181        6797 :   for (j = 2; j < l; j++) /* skip d = 1 */
    3182             :   {
    3183        3619 :     long md = D[j]; /* mu(d) * d, d squarefree */
    3184        3619 :     GEN pd = powiu(p, n / labs(md)); /* p^{n/d} */
    3185        3619 :     s = md > 0? addii(s, pd): subii(s,pd);
    3186             :   }
    3187        3178 :   return gerepileuptoint(av, diviuexact(s, n));
    3188             : }
    3189             : 
    3190             : GEN
    3191         616 : ffsumnbirred(GEN p, long n)
    3192             : {
    3193         616 :   pari_sp av = avma, av2;
    3194         616 :   GEN q, t = p, v = vecfactoru_i(1, n);
    3195             :   long i;
    3196         616 :   q = cgetg(n+1,t_VEC); gel(q,1) = p;
    3197        1764 :   for (i=2; i<=n; i++) gel(q,i) = mulii(gel(q,i-1), p);
    3198         616 :   av2 = avma;
    3199        1764 :   for (i=2; i<=n; i++)
    3200             :   {
    3201        1148 :     GEN s = gel(q,i), F = gel(v,i), D = divisorsu_moebius(gel(F,1));
    3202        1148 :     long j, l = lg(D);
    3203        2534 :     for (j = 2; j < l; j++) /* skip 1 */
    3204             :     {
    3205        1386 :       long md = D[j];
    3206        1386 :       GEN pd = gel(q, i / labs(md)); /* p^{i/d} */
    3207        1386 :       s = md > 0? addii(s, pd): subii(s, pd);
    3208             :     }
    3209        1148 :     t = gerepileuptoint(av2, addii(t, diviuexact(s, i)));
    3210             :   }
    3211         616 :   return gerepileuptoint(av, t);
    3212             : }
    3213             : 
    3214             : GEN
    3215         140 : ffnbirred0(GEN p, long n, long flag)
    3216             : {
    3217         140 :   if (typ(p) != t_INT) pari_err_TYPE("ffnbirred", p);
    3218         140 :   if (n <= 0) pari_err_DOMAIN("ffnbirred", "degree", "<=", gen_0, stoi(n));
    3219         140 :   switch(flag)
    3220             :   {
    3221          70 :     case 0: return ffnbirred(p, n);
    3222          70 :     case 1: return ffsumnbirred(p, n);
    3223             :   }
    3224           0 :   pari_err_FLAG("ffnbirred");
    3225             :   return NULL; /* LCOV_EXCL_LINE */
    3226             : }
    3227             : 
    3228             : static void
    3229        2261 : checkmap(GEN m, const char *s)
    3230             : {
    3231        2261 :   if (typ(m)!=t_VEC || lg(m)!=3 || typ(gel(m,1))!=t_FFELT)
    3232           0 :     pari_err_TYPE(s,m);
    3233        2261 : }
    3234             : 
    3235             : GEN
    3236         189 : ffembed(GEN a, GEN b)
    3237             : {
    3238         189 :   pari_sp av = avma;
    3239         189 :   GEN p, Ta, Tb, g, r = NULL;
    3240         189 :   if (typ(a)!=t_FFELT) pari_err_TYPE("ffembed",a);
    3241         189 :   if (typ(b)!=t_FFELT) pari_err_TYPE("ffembed",b);
    3242         189 :   p = FF_p_i(a); g = FF_gen(a);
    3243         189 :   if (!equalii(p, FF_p_i(b))) pari_err_MODULUS("ffembed",a,b);
    3244         189 :   Ta = FF_mod(a);
    3245         189 :   Tb = FF_mod(b);
    3246         189 :   if (degpol(Tb)%degpol(Ta)!=0)
    3247           7 :     pari_err_DOMAIN("ffembed",GENtostr_raw(a),"is not a subfield of",b,a);
    3248         182 :   r = gel(FFX_roots(Ta, b), 1);
    3249         182 :   return gerepilecopy(av, mkvec2(g,r));
    3250             : }
    3251             : 
    3252             : GEN
    3253          91 : ffextend(GEN a, GEN P, long v)
    3254             : {
    3255          91 :   pari_sp av = avma;
    3256             :   long n;
    3257             :   GEN p, T, R, g, m;
    3258          91 :   if (typ(a)!=t_FFELT) pari_err_TYPE("ffextend",a);
    3259          91 :   T = a; p = FF_p_i(a);
    3260          91 :   if (typ(P)!=t_POL || !RgX_is_FpXQX(P,&T,&p)) pari_err_TYPE("ffextend", P);
    3261          49 :   if (!FF_samefield(a, T)) pari_err_MODULUS("ffextend",a,T);
    3262          49 :   if (v < 0) v = varn(P);
    3263          49 :   n = FF_f(T) * degpol(P); R = ffinit(p, n, v); g = ffgen(R, v);
    3264          49 :   m = ffembed(a, g);
    3265          49 :   R = FFX_roots(ffmap(m, P),g);
    3266          49 :   return gerepilecopy(av, mkvec2(gel(R,1), m));
    3267             : }
    3268             : 
    3269             : GEN
    3270          42 : fffrobenius(GEN a, long n)
    3271             : {
    3272          42 :   if (typ(a)!=t_FFELT) pari_err_TYPE("fffrobenius",a);
    3273          42 :   retmkvec2(FF_gen(a), FF_Frobenius(a, n));
    3274             : }
    3275             : 
    3276             : GEN
    3277         133 : ffinvmap(GEN m)
    3278             : {
    3279         133 :   pari_sp av = avma;
    3280             :   long i, l;
    3281         133 :   GEN T, F, a, g, r, f = NULL;
    3282         133 :   checkmap(m, "ffinvmap");
    3283         133 :   a = gel(m,1); r = gel(m,2);
    3284         133 :   if (typ(r) != t_FFELT)
    3285           7 :    pari_err_TYPE("ffinvmap", m);
    3286         126 :   g = FF_gen(a);
    3287         126 :   T = FF_mod(r);
    3288         126 :   F = gel(FFX_factor(T, a), 1);
    3289         126 :   l = lg(F);
    3290         490 :   for(i=1; i<l; i++)
    3291             :   {
    3292         490 :     GEN s = FFX_rem(FF_to_FpXQ_i(r), gel(F, i), a);
    3293         490 :     if (degpol(s)==0 && gequal(constant_coeff(s),g)) { f = gel(F, i); break; }
    3294             :   }
    3295         126 :   if (f==NULL) pari_err_TYPE("ffinvmap", m);
    3296         126 :   if (degpol(f)==1) f = FF_neg_i(gel(f,2));
    3297         126 :   return gerepilecopy(av, mkvec2(FF_gen(r),f));
    3298             : }
    3299             : 
    3300             : static GEN
    3301        1260 : ffpartmapimage(const char *s, GEN r)
    3302             : {
    3303        1260 :    GEN a = NULL, p = NULL;
    3304        1260 :    if (typ(r)==t_POL && degpol(r) >= 1
    3305        1260 :       && RgX_is_FpXQX(r,&a,&p) && a && typ(a)==t_FFELT) return a;
    3306           0 :    pari_err_TYPE(s, r);
    3307             :    return NULL; /* LCOV_EXCL_LINE */
    3308             : }
    3309             : 
    3310             : static GEN
    3311        2709 : ffeltmap_i(GEN m, GEN x)
    3312             : {
    3313        2709 :    GEN r = gel(m,2);
    3314        2709 :    if (!FF_samefield(x, gel(m,1)))
    3315          84 :      pari_err_DOMAIN("ffmap","m","domain does not contain", x, r);
    3316        2625 :    if (typ(r)==t_FFELT)
    3317        1659 :      return FF_map(r, x);
    3318             :    else
    3319         966 :      return FFX_preimage(x, r, ffpartmapimage("ffmap", r));
    3320             : }
    3321             : 
    3322             : static GEN
    3323        4459 : ffmap_i(GEN m, GEN x)
    3324             : {
    3325             :   GEN y;
    3326        4459 :   long i, lx, tx = typ(x);
    3327        4459 :   switch(tx)
    3328             :   {
    3329        2541 :     case t_FFELT:
    3330        2541 :       return ffeltmap_i(m, x);
    3331        1267 :     case t_POL: case t_RFRAC: case t_SER:
    3332             :     case t_VEC: case t_COL: case t_MAT:
    3333        1267 :       y = cgetg_copy(x, &lx);
    3334        1988 :       for (i=1; i<lontyp[tx]; i++) y[i] = x[1];
    3335        4564 :       for (i=lontyp[tx]; i<lx; i++)
    3336             :       {
    3337        3339 :         GEN yi = ffmap_i(m, gel(x,i));
    3338        3297 :         if (!yi) return NULL;
    3339        3297 :         gel(y,i) = yi;
    3340             :       }
    3341        1225 :       return y;
    3342             :   }
    3343         651 :   return gcopy(x);
    3344             : }
    3345             : 
    3346             : GEN
    3347        1036 : ffmap(GEN m, GEN x)
    3348             : {
    3349        1036 :   pari_sp ltop = avma;
    3350             :   GEN y;
    3351        1036 :   checkmap(m, "ffmap");
    3352        1036 :   y = ffmap_i(m, x);
    3353        1036 :   if (y) return y;
    3354          42 :   set_avma(ltop); return cgetg(1,t_VEC);
    3355             : }
    3356             : 
    3357             : static GEN
    3358         252 : ffeltmaprel_i(GEN m, GEN x)
    3359             : {
    3360         252 :    GEN g = gel(m,1), r = gel(m,2);
    3361         252 :    if (!FF_samefield(x, g))
    3362           0 :      pari_err_DOMAIN("ffmap","m","domain does not contain", x, r);
    3363         252 :    if (typ(r)==t_FFELT)
    3364          84 :      retmkpolmod(FF_map(r, x), pol_x(FF_var(g)));
    3365             :    else
    3366         168 :      retmkpolmod(FFX_preimagerel(x, r, ffpartmapimage("ffmap", r)), gcopy(r));
    3367             : }
    3368             : 
    3369             : static GEN
    3370         252 : ffmaprel_i(GEN m, GEN x)
    3371             : {
    3372         252 :   switch(typ(x))
    3373             :   {
    3374         252 :     case t_FFELT:
    3375         252 :       return ffeltmaprel_i(m, x);
    3376           0 :     case t_POL: pari_APPLY_pol_normalized(ffmaprel_i(m, gel(x,i)));
    3377           0 :     case t_SER: pari_APPLY_ser_normalized(ffmaprel_i(m, gel(x,i)));
    3378           0 :     case t_RFRAC: case t_VEC: case t_COL: case t_MAT:
    3379           0 :       pari_APPLY_same(ffmaprel_i(m, gel(x,i)));
    3380             :   }
    3381           0 :   return gcopy(x);
    3382             : }
    3383             : GEN
    3384         252 : ffmaprel(GEN m, GEN x) { checkmap(m, "ffmaprel"); return ffmaprel_i(m, x); }
    3385             : 
    3386             : static void
    3387          84 : err_compo(GEN m, GEN n)
    3388          84 : { pari_err_DOMAIN("ffcompomap","m","domain does not contain codomain of",n,m); }
    3389             : 
    3390             : GEN
    3391         420 : ffcompomap(GEN m, GEN n)
    3392             : {
    3393         420 :   pari_sp av = avma;
    3394         420 :   GEN g = gel(n,1), r, m2, n2;
    3395         420 :   checkmap(m, "ffcompomap");
    3396         420 :   checkmap(n, "ffcompomap");
    3397         420 :   m2 = gel(m,2); n2 = gel(n,2);
    3398         420 :   switch((typ(m2)==t_POL)|((typ(n2)==t_POL)<<1))
    3399             :   {
    3400          84 :     case 0:
    3401          84 :       if (!FF_samefield(gel(m,1),n2)) err_compo(m,n);
    3402          42 :       r = FF_map(gel(m,2), n2);
    3403          42 :       break;
    3404          84 :     case 2:
    3405          84 :       r = ffmap_i(m, n2);
    3406          42 :       if (lg(r) == 1) err_compo(m,n);
    3407          42 :       break;
    3408         168 :     case 1:
    3409         168 :       r = ffeltmap_i(m, n2);
    3410         126 :       if (!r)
    3411             :       {
    3412             :         GEN a, A, R, M;
    3413             :         long dm, dn;
    3414          42 :         a = ffpartmapimage("ffcompomap",m2);
    3415          42 :         A = FF_to_FpXQ_i(FF_neg(n2));
    3416          42 :         setvarn(A, 1);
    3417          42 :         R = deg1pol(gen_1, A, 0);
    3418          42 :         setvarn(R, 0);
    3419          42 :         M = gcopy(m2);
    3420          42 :         setvarn(M, 1);
    3421          42 :         r = polresultant0(R, M, 1, 0);
    3422          42 :         dm = FF_f(gel(m,1)); dn = FF_f(gel(n,1));
    3423          42 :         if (dm % dn || !FFX_ispower(r, dm/dn, a, &r)) err_compo(m,n);
    3424          42 :         setvarn(r, varn(FF_mod(g)));
    3425             :       }
    3426         126 :       break;
    3427          84 :     case 3:
    3428             :     {
    3429             :       GEN M, R, T, p, a;
    3430          84 :       a = ffpartmapimage("ffcompomap",n2);
    3431          84 :       if (!FF_samefield(a, gel(m,1))) err_compo(m,n);
    3432          42 :       p = FF_p_i(gel(n,1));
    3433          42 :       T = FF_mod(gel(n,1));
    3434          42 :       setvarn(T, 1);
    3435          42 :       R = RgX_to_FpXQX(n2,T,p);
    3436          42 :       setvarn(R, 0);
    3437          42 :       M = gcopy(m2);
    3438          42 :       setvarn(M, 1);
    3439          42 :       r = polresultant0(R, M, 1, 0);
    3440          42 :       setvarn(r, varn(n2));
    3441             :     }
    3442             :   }
    3443         252 :   return gerepilecopy(av, mkvec2(g,r));
    3444             : }

Generated by: LCOV version 1.16