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.16.2 lcov report (development 29115-f22e516b23) Lines: 1747 1946 89.8 %
Date: 2024-02-22 08:05:55 Functions: 185 199 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      163394 : charact(struct charact *S, GEN x)
      53             : {
      54      163394 :   const long tx = typ(x);
      55             :   long i, l;
      56      163394 :   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       26334 :     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       26334 :       l = lg(x);
      64      174223 :       for (i=lontyp[tx]; i < l; i++) charact(S,gel(x,i));
      65       26320 :       break;
      66           7 :     case t_LIST:
      67           7 :       x = list_data(x);
      68           7 :       if (x) charact(S, x);
      69           7 :       break;
      70             :   }
      71      163366 : }
      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       15491 : characteristic(GEN x)
      96             : {
      97             :   struct charact S;
      98       15491 :   S.q = gen_0; S.isprime = 0;
      99       15491 :   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    68808109 : Rg_is_Fp(GEN x, GEN *pp)
     111             : {
     112             :   GEN mod;
     113    68808109 :   switch(typ(x))
     114             :   {
     115     3202780 :   case t_INTMOD:
     116     3202780 :     mod = gel(x,1);
     117     3202780 :     if (!*pp) *pp = mod;
     118     2953608 :     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     3202780 :     return 1;
     124    54580257 :   case t_INT:
     125    54580257 :     return 1;
     126    11025072 :   default: return 0;
     127             :   }
     128             : }
     129             : 
     130             : int
     131    27593290 : RgX_is_FpX(GEN x, GEN *pp)
     132             : {
     133    27593290 :   long i, lx = lg(x);
     134    85350226 :   for (i=2; i<lx; i++)
     135    68782003 :     if (!Rg_is_Fp(gel(x, i), pp))
     136    11025071 :       return 0;
     137    16568223 :   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       59304 : Rg_is_FpXQ(GEN x, GEN *pT, GEN *pp)
     160             : {
     161             :   GEN pol, mod, p;
     162       59304 :   switch(typ(x))
     163             :   {
     164       26089 :   case t_INTMOD:
     165       26089 :     return Rg_is_Fp(x, pp);
     166        7105 :   case t_INT:
     167        7105 :     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        3206 : RgX_is_FpXQX(GEN x, GEN *pT, GEN *pp)
     202             : {
     203        3206 :   long i, lx = lg(x);
     204       61754 :   for (i = 2; i < lx; i++)
     205       58646 :     if (!Rg_is_FpXQ(gel(x,i), pT, pp)) return 0;
     206        3108 :   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    35141017 : Rg_to_Fp(GEN x, GEN p)
     219             : {
     220    35141017 :   if (lgefint(p) == 3) return utoi(Rg_to_Fl(x, uel(p,2)));
     221     4556976 :   switch(typ(x))
     222             :   {
     223      288649 :     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     4249487 :     case t_INTMOD: {
     232     4249487 :       GEN q = gel(x,1), a = gel(x,2);
     233     4249487 :       if (equalii(q, p)) return icopy(a);
     234          14 :       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     1291496 : Rg_to_FpXQ(GEN x, GEN T, GEN p)
     244             : {
     245     1291496 :   long ta, tx = typ(x), v = get_FpX_var(T);
     246             :   GEN a, b;
     247     1291496 :   if (is_const_t(tx))
     248             :   {
     249       58531 :     if (tx == t_FFELT)
     250             :     {
     251       17355 :       GEN z = FF_to_FpXQ(x);
     252       17355 :       setvarn(z, v);
     253       17355 :       return z;
     254             :     }
     255       41176 :     return scalar_ZX(degpol(T)? Rg_to_Fp(x, p): gen_0, v);
     256             :   }
     257     1232965 :   switch(tx)
     258             :   {
     259     1230886 :     case t_POLMOD:
     260     1230886 :       b = gel(x,1);
     261     1230886 :       a = gel(x,2); ta = typ(a);
     262     1230886 :       if (is_const_t(ta))
     263        4095 :         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        2079 :     case t_POL:
     270        2079 :       if (varn(x) != v) break;
     271        2079 :       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           0 :   pari_err_TYPE("Rg_to_FpXQ",x);
     278             :   return NULL; /* LCOV_EXCL_LINE */
     279             : }
     280             : GEN
     281     3552023 : RgX_to_FpX(GEN x, GEN p)
     282             : {
     283             :   long i, l;
     284     3552023 :   GEN z = cgetg_copy(x, &l); z[1] = x[1];
     285    15793752 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     286     3552023 :   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      933010 : RgC_to_FpC(GEN x, GEN p)
     295    11541908 : { pari_APPLY_type(t_COL, Rg_to_Fp(gel(x,i), p)) }
     296             : 
     297             : GEN
     298      133805 : RgM_to_FpM(GEN x, GEN p)
     299     1066773 : { pari_APPLY_same(RgC_to_FpC(gel(x,i), p)) }
     300             : 
     301             : GEN
     302      342814 : RgV_to_Flv(GEN x, ulong p)
     303     1343113 : { pari_APPLY_ulong(Rg_to_Fl(gel(x,i), p)) }
     304             : 
     305             : GEN
     306      114124 : RgM_to_Flm(GEN x, ulong p)
     307      392639 : { pari_APPLY_same(RgV_to_Flv(gel(x,i), p)) }
     308             : 
     309             : GEN
     310        5014 : RgX_to_FpXQX(GEN x, GEN T, GEN p)
     311             : {
     312        5014 :   long i, l = lg(x);
     313        5014 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     314       42911 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T,p);
     315        5014 :   return FpXQX_renormalize(z, l);
     316             : }
     317             : GEN
     318       48741 : RgX_to_FqX(GEN x, GEN T, GEN p)
     319             : {
     320       48741 :   long i, l = lg(x);
     321       48741 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     322       48741 :   if (T)
     323       10990 :     for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T, p);
     324             :   else
     325      787026 :     for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     326       48741 :   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      149023 : FqX_Fq_mul_to_monic(GEN P, GEN U, GEN T, GEN p)
     392             : {
     393             :   long i, lP;
     394      149023 :   GEN res = cgetg_copy(P, &lP); res[1] = P[1];
     395      918544 :   for(i=2; i<lP-1; i++) gel(res,i) = Fq_mul(U,gel(P,i), T,p);
     396      149023 :   gel(res,lP-1) = gen_1; return res;
     397             : }
     398             : 
     399             : GEN
     400       38146 : FpXQX_normalize(GEN z, GEN T, GEN p)
     401             : {
     402             :   GEN lc;
     403       38146 :   if (lg(z) == 2) return z;
     404       38132 :   lc = leading_coeff(z);
     405       38132 :   if (typ(lc) == t_POL)
     406             :   {
     407        2152 :     if (lg(lc) > 3) /* nonconstant */
     408        1880 :       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       36252 :   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      398815 : FqX_eval(GEN x, GEN y, GEN T, GEN p)
     421             : {
     422             :   pari_sp av;
     423             :   GEN p1, r;
     424      398815 :   long j, i=lg(x)-1;
     425      398815 :   if (i<=2)
     426       45957 :     return (i==2)? Fq_red(gel(x,2), T, p): gen_0;
     427      352858 :   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     1173794 :   for (i--; i>=2; i=j-1)
     431             :   {
     432      821636 :     for (j=i; !signe(gel(x,j)); j--)
     433         700 :       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      820936 :     r = (i==j)? y: Fq_pow(y, utoipos(i-j+1), T, p);
     439      820936 :     p1 = Fq_add(Fq_mul(p1,r,T,p), gel(x,j), T, p);
     440             :   }
     441      352368 :   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    10445027 : monomial(GEN a, long d, long v)
     471             : {
     472             :   long i, n;
     473             :   GEN P;
     474    10445027 :   if (d < 0) {
     475          14 :     if (isrationalzero(a)) return pol_0(v);
     476          14 :     retmkrfrac(a, pol_xn(-d, v));
     477             :   }
     478    10445013 :   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    10351737 :     n = d+2; P = cgetg(n+1, t_POL);
     487    10351740 :     P[1] = evalsigne(1) | evalvarn(v);
     488             :   }
     489    29478573 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     490    10351740 :   gel(P,i) = a; return P;
     491             : }
     492             : GEN
     493     1863387 : monomialcopy(GEN a, long d, long v)
     494             : {
     495             :   long i, n;
     496             :   GEN P;
     497     1863387 :   if (d < 0) {
     498          14 :     if (isrationalzero(a)) return pol_0(v);
     499          14 :     retmkrfrac(gcopy(a), pol_xn(-d, v));
     500             :   }
     501     1863373 :   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     1863359 :     n = d+2; P = cgetg(n+1, t_POL);
     510     1863359 :     P[1] = evalsigne(1) | evalvarn(v);
     511             :   }
     512     3510668 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     513     1863366 :   gel(P,i) = gcopy(a); return P;
     514             : }
     515             : GEN
     516      324800 : pol_x_powers(long N, long v)
     517             : {
     518      324800 :   GEN L = cgetg(N+1,t_VEC);
     519             :   long i;
     520      784824 :   for (i=1; i<=N; i++) gel(L,i) = pol_xn(i-1, v);
     521      324807 :   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    11608901 : Fq_add(GEN x, GEN y, GEN T/*unused*/, GEN p)
     544             : {
     545             :   (void)T;
     546    11608901 :   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     9609146 :     case 3: return FpX_add(x,y,p);
     552             :   }
     553             :   return NULL;/*LCOV_EXCL_LINE*/
     554             : }
     555             : 
     556             : GEN
     557     8349764 : Fq_sub(GEN x, GEN y, GEN T/*unused*/, GEN p)
     558             : {
     559             :   (void)T;
     560     8349764 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     561             :   {
     562      256074 :     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     8045302 :     case 3: return FpX_sub(x,y,p);
     566             :   }
     567             :   return NULL;/*LCOV_EXCL_LINE*/
     568             : }
     569             : 
     570             : GEN
     571     1071666 : Fq_neg(GEN x, GEN T/*unused*/, GEN p)
     572             : {
     573             :   (void)T;
     574     1071666 :   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     8377825 : Fq_mul(GEN x, GEN y, GEN T, GEN p)
     587             : {
     588     8377825 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     589             :   {
     590     1037469 :     case 0: return Fp_mul(x,y,p);
     591      128947 :     case 1: return FpX_Fp_mul(x,y,p);
     592      402219 :     case 2: return FpX_Fp_mul(y,x,p);
     593     6809197 :     case 3: if (T) return FpXQ_mul(x,y,T,p);
     594     4232038 :             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      492715 : Fq_mulu(GEN x, ulong y, /*unused*/GEN T, GEN p)
     602             : {
     603             :   (void) T;
     604      492715 :   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       43965 : 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       50809 :                           : Fp_mul(x,y,p);
     614             : }
     615             : /* If T==NULL do not reduce*/
     616             : GEN
     617      613156 : Fq_sqr(GEN x, GEN T, GEN p)
     618             : {
     619      613156 :   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      540312 :     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       89324 : Fq_inv(GEN x, GEN pol, GEN p)
     644             : {
     645       89324 :   if (typ(x) == t_INT) return Fp_inv(x,p);
     646       81558 :   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      792507 : Fq_pow(GEN x, GEN n, GEN pol, GEN p)
     664             : {
     665      792507 :   if (typ(x) == t_INT) return Fp_pow(x,n,p);
     666      136643 :   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      170723 : Fq_sqrtn(GEN x, GEN n, GEN T, GEN p, GEN *zeta)
     688             : {
     689      170723 :   if (typ(x) == t_INT)
     690             :   {
     691             :     long d;
     692      170366 :     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      302701 : _Fq_red(void *E, GEN x)
     713      302701 : { struct _Fq_field *s = (struct _Fq_field *)E;
     714      302701 :   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      242795 : _Fq_mul(void *E, GEN x, GEN y)
     735             : {
     736             :   (void) E;
     737      242795 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     738             :   {
     739         133 :     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       13965 : _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        4179 : const struct bb_field *get_Fq_field(void **E, GEN T, GEN p)
     762             : {
     763        4179 :   if (!T)
     764           0 :     return get_Fp_field(E, p);
     765             :   else
     766             :   {
     767        4179 :     GEN z = new_chunk(sizeof(struct _Fq_field));
     768        4179 :     struct _Fq_field *e = (struct _Fq_field *) z;
     769        4179 :     e->T = T; e->p  = p; *E = (void*)e;
     770        4179 :     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       40470 : FqV_roots_to_pol(GEN V, GEN T, GEN p, long v)
     831             : {
     832       40470 :   pari_sp ltop = avma;
     833             :   long k;
     834             :   GEN W;
     835       40470 :   if (lgefint(p) == 3)
     836             :   {
     837       31765 :     ulong pp = p[2];
     838       31765 :     GEN Tl = ZX_to_Flx(T, pp);
     839       31766 :     GEN Vl = ZXC_to_FlxC(V, pp, get_Flx_var(Tl));
     840       31768 :     Tl = FlxqV_roots_to_pol(Vl, Tl, pp, v);
     841       31769 :     return gerepileupto(ltop, FlxX_to_ZXX(Tl));
     842             :   }
     843        8705 :   W = cgetg(lg(V),t_VEC);
     844       78039 :   for(k=1; k < lg(V); k++)
     845       69334 :     gel(W,k) = deg1pol_shallow(gen_1,Fq_neg(gel(V,k),T,p),v);
     846        8705 :   return gerepileupto(ltop, FpXQXV_prod(W, T, p));
     847             : }
     848             : 
     849             : GEN
     850      190561 : FqV_red(GEN x, GEN T, GEN p)
     851     1345554 : { pari_APPLY_same(Fq_red(gel(x,i), T, p)) }
     852             : 
     853             : GEN
     854           0 : FqC_add(GEN x, GEN y, GEN T, GEN p)
     855             : {
     856           0 :   if (!T) return FpC_add(x, y, p);
     857           0 :   pari_APPLY_type(t_COL, Fq_add(gel(x,i), gel(y,i), T, p))
     858             : }
     859             : 
     860             : GEN
     861           0 : FqC_sub(GEN x, GEN y, GEN T, GEN p)
     862             : {
     863           0 :   if (!T) return FpC_sub(x, y, p);
     864           0 :   pari_APPLY_type(t_COL, Fq_sub(gel(x,i), gel(y,i), T, p))
     865             : }
     866             : 
     867             : GEN
     868           0 : FqC_Fq_mul(GEN x, GEN y, GEN T, GEN p)
     869             : {
     870           0 :   if (!T) return FpC_Fp_mul(x, y, p);
     871           0 :   pari_APPLY_type(t_COL, Fq_mul(gel(x,i),y,T,p))
     872             : }
     873             : 
     874             : GEN
     875         105 : FqC_FqV_mul(GEN x, GEN y, GEN T, GEN p)
     876             : {
     877         105 :   long i,j, lx=lg(x), ly=lg(y);
     878             :   GEN z;
     879         105 :   if (ly==1) return cgetg(1,t_MAT);
     880         105 :   z = cgetg(ly,t_MAT);
     881         819 :   for (j=1; j < ly; j++)
     882             :   {
     883         714 :     GEN zj = cgetg(lx,t_COL);
     884        4200 :     for (i=1; i<lx; i++) gel(zj,i) = Fq_mul(gel(x,i),gel(y,j), T, p);
     885         714 :     gel(z, j) = zj;
     886             :   }
     887         105 :   return z;
     888             : }
     889             : 
     890             : GEN
     891        5271 : FpXC_center(GEN x, GEN p, GEN pov2)
     892       40524 : { pari_APPLY_type(t_COL, FpX_center(gel(x,i), p, pov2)) }
     893             : 
     894             : GEN
     895        1730 : FpXM_center(GEN x, GEN p, GEN pov2)
     896        7001 : { pari_APPLY_same(FpXC_center(gel(x,i), p, pov2)) }
     897             : 
     898             : /*******************************************************************/
     899             : /*                                                                 */
     900             : /*                          GENERIC CRT                            */
     901             : /*                                                                 */
     902             : /*******************************************************************/
     903             : static GEN
     904     8307270 : primelist(forprime_t *S, long n, GEN dB)
     905             : {
     906     8307270 :   GEN P = cgetg(n+1, t_VECSMALL);
     907     8307249 :   long i = 1;
     908             :   ulong p;
     909    19734469 :   while (i <= n && (p = u_forprime_next(S)))
     910    11427221 :     if (!dB || umodiu(dB, p)) P[i++] = p;
     911     8307257 :   return P;
     912             : }
     913             : 
     914             : void
     915     7793204 : gen_inccrt_i(const char *str, GEN worker, GEN dB, long n, long mmin,
     916             :              forprime_t *S, GEN *pH, GEN *pmod, GEN crt(GEN, GEN, GEN*),
     917             :              GEN center(GEN, GEN, GEN))
     918             : {
     919     7793204 :   long m = mmin? minss(mmin, n): usqrt(n);
     920             :   GEN  H, P, mod;
     921             :   pari_timer ti;
     922     7793187 :   if (DEBUGLEVEL > 4)
     923             :   {
     924           0 :     timer_start(&ti);
     925           0 :     err_printf("%s: nb primes: %ld\n",str, n);
     926             :   }
     927     7793174 :   if (m == 1)
     928             :   {
     929     7524359 :     GEN P = primelist(S, n, dB);
     930     7524354 :     GEN done = closure_callgen1(worker, P);
     931     7524303 :     H = gel(done,1);
     932     7524303 :     mod = gel(done,2);
     933     7524303 :     if (!*pH && center) H = center(H, mod, shifti(mod,-1));
     934     7524260 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: modular", str);
     935             :   }
     936             :   else
     937             :   {
     938      268815 :     long i, s = (n+m-1)/m, r = m - (m*s-n), di = 0;
     939             :     struct pari_mt pt;
     940      268815 :     long pending = 0;
     941      268815 :     H = cgetg(m+1, t_VEC); P = cgetg(m+1, t_VEC);
     942      268815 :     mt_queue_start_lim(&pt, worker, m);
     943     1108478 :     for (i=1; i<=m || pending; i++)
     944             :     {
     945             :       GEN done;
     946      839662 :       GEN pr = i <= m ? mkvec(primelist(S, i<=r ? s: s-1, dB)): NULL;
     947      839663 :       mt_queue_submit(&pt, i, pr);
     948      839662 :       done = mt_queue_get(&pt, NULL, &pending);
     949      839662 :       if (done)
     950             :       {
     951      782917 :         di++;
     952      782917 :         gel(H, di) = gel(done,1);
     953      782917 :         gel(P, di) = gel(done,2);
     954      782917 :         if (DEBUGLEVEL>5) err_printf("%ld%% ",100*di/m);
     955             :       }
     956             :     }
     957      268816 :     mt_queue_end(&pt);
     958      268816 :     if (DEBUGLEVEL>5) err_printf("\n");
     959      268816 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: modular", str);
     960      268816 :     H = crt(H, P, &mod);
     961      268816 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: chinese", str);
     962             :   }
     963     7793076 :   if (*pH) H = crt(mkvec2(*pH, H), mkvec2(*pmod, mod), &mod);
     964     7793076 :   *pH = H; *pmod = mod;
     965     7793076 : }
     966             : void
     967     2166375 : gen_inccrt(const char *str, GEN worker, GEN dB, long n, long mmin,
     968             :            forprime_t *S, GEN *pH, GEN *pmod, GEN crt(GEN, GEN, GEN*),
     969             :            GEN center(GEN, GEN, GEN))
     970             : {
     971     2166375 :   pari_sp av = avma;
     972     2166375 :   gen_inccrt_i(str, worker, dB, n, mmin, S, pH, pmod, crt, center);
     973     2166290 :   gerepileall(av, 2, pH, pmod);
     974     2166424 : }
     975             : 
     976             : GEN
     977     1390747 : gen_crt(const char *str, GEN worker, forprime_t *S, GEN dB, ulong bound, long mmin, GEN *pmod,
     978             :         GEN crt(GEN, GEN, GEN*), GEN center(GEN, GEN, GEN))
     979             : {
     980     1390747 :   GEN mod = gen_1, H = NULL;
     981             :   ulong e;
     982             : 
     983     1390747 :   bound++;
     984     2781528 :   while (bound > (e = expi(mod)))
     985             :   {
     986     1390712 :     long n = (bound - e) / expu(S->p) + 1;
     987     1390742 :     gen_inccrt(str, worker, dB, n, mmin, S, &H, &mod, crt, center);
     988             :   }
     989     1390758 :   if (pmod) *pmod = mod;
     990     1390758 :   return H;
     991             : }
     992             : 
     993             : /*******************************************************************/
     994             : /*                                                                 */
     995             : /*                          MODULAR GCD                            */
     996             : /*                                                                 */
     997             : /*******************************************************************/
     998             : /* return z = a mod q, b mod p (p,q) = 1; qinv = 1/q mod p; a in ]-q,q] */
     999             : static GEN
    1000     5112905 : Fl_chinese_coprime(GEN a, ulong b, GEN q, ulong p, ulong qinv, GEN pq, GEN pq2)
    1001             : {
    1002     5112905 :   ulong d, amod = umodiu(a, p);
    1003     5112956 :   pari_sp av = avma;
    1004             :   GEN ax;
    1005             : 
    1006     5112956 :   if (b == amod) return NULL;
    1007     2104755 :   d = Fl_mul(Fl_sub(b, amod, p), qinv, p); /* != 0 */
    1008     2105238 :   if (d >= 1 + (p>>1))
    1009     1026928 :     ax = subii(a, mului(p-d, q));
    1010             :   else
    1011             :   {
    1012     1078310 :     ax = addii(a, mului(d, q)); /* in ]0, pq[ assuming a in ]-q,q[ */
    1013     1077920 :     if (cmpii(ax,pq2) > 0) ax = subii(ax,pq);
    1014             :   }
    1015     2104545 :   return gerepileuptoint(av, ax);
    1016             : }
    1017             : GEN
    1018         378 : Z_init_CRT(ulong Hp, ulong p) { return stoi(Fl_center(Hp, p, p>>1)); }
    1019             : GEN
    1020       31542 : ZX_init_CRT(GEN Hp, ulong p, long v)
    1021             : {
    1022       31542 :   long i, l = lg(Hp), lim = (long)(p>>1);
    1023       31542 :   GEN H = cgetg(l, t_POL);
    1024       31542 :   H[1] = evalsigne(1) | evalvarn(v);
    1025      794361 :   for (i=2; i<l; i++)
    1026      762819 :     gel(H,i) = stoi(Fl_center(Hp[i], p, lim));
    1027       31542 :   return ZX_renormalize(H,l);
    1028             : }
    1029             : 
    1030             : GEN
    1031        3633 : ZM_init_CRT(GEN Hp, ulong p)
    1032             : {
    1033        3633 :   long i,j, m, l = lg(Hp), lim = (long)(p>>1);
    1034        3633 :   GEN c, cp, H = cgetg(l, t_MAT);
    1035        3633 :   if (l==1) return H;
    1036        3633 :   m = lgcols(Hp);
    1037       12544 :   for (j=1; j<l; j++)
    1038             :   {
    1039        8911 :     cp = gel(Hp,j);
    1040        8911 :     c = cgetg(m, t_COL);
    1041        8911 :     gel(H,j) = c;
    1042       87983 :     for (i=1; i<m; i++) gel(c,i) = stoi(Fl_center(cp[i],p, lim));
    1043             :   }
    1044        3633 :   return H;
    1045             : }
    1046             : 
    1047             : int
    1048        7616 : Z_incremental_CRT(GEN *H, ulong Hp, GEN *ptq, ulong p)
    1049             : {
    1050        7616 :   GEN h, q = *ptq, qp = muliu(q,p);
    1051        7616 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1052        7616 :   int stable = 1;
    1053        7616 :   h = Fl_chinese_coprime(*H,Hp,q,p,qinv,qp,shifti(qp,-1));
    1054        7616 :   if (h) { *H = h; stable = 0; }
    1055        7616 :   *ptq = qp; return stable;
    1056             : }
    1057             : 
    1058             : static int
    1059      147351 : ZX_incremental_CRT_raw(GEN *ptH, GEN Hp, GEN q, GEN qp, ulong p)
    1060             : {
    1061      147351 :   GEN H = *ptH, h, qp2 = shifti(qp,-1);
    1062      147347 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1063      147354 :   long i, l = lg(H), lp = lg(Hp);
    1064      147354 :   int stable = 1;
    1065             : 
    1066      147354 :   if (l < lp)
    1067             :   { /* degree increases */
    1068           0 :     GEN x = cgetg(lp, t_POL);
    1069           0 :     for (i=1; i<l; i++)  x[i] = H[i];
    1070           0 :     for (   ; i<lp; i++) gel(x,i) = gen_0;
    1071           0 :     *ptH = H = x;
    1072           0 :     stable = 0;
    1073      147354 :   } else if (l > lp)
    1074             :   { /* degree decreases */
    1075           0 :     GEN x = cgetg(l, t_VECSMALL);
    1076           0 :     for (i=1; i<lp; i++)  x[i] = Hp[i];
    1077           0 :     for (   ; i<l; i++) x[i] = 0;
    1078           0 :     Hp = x; lp = l;
    1079             :   }
    1080     4932781 :   for (i=2; i<lp; i++)
    1081             :   {
    1082     4785538 :     h = Fl_chinese_coprime(gel(H,i),Hp[i],q,p,qinv,qp,qp2);
    1083     4785427 :     if (h) { gel(H,i) = h; stable = 0; }
    1084             :   }
    1085      147243 :   (void)ZX_renormalize(H,lp);
    1086      147354 :   return stable;
    1087             : }
    1088             : 
    1089             : int
    1090           0 : ZX_incremental_CRT(GEN *ptH, GEN Hp, GEN *ptq, ulong p)
    1091             : {
    1092           0 :   GEN q = *ptq, qp = muliu(q,p);
    1093           0 :   int stable = ZX_incremental_CRT_raw(ptH, Hp, q, qp, p);
    1094           0 :   *ptq = qp; return stable;
    1095             : }
    1096             : 
    1097             : int
    1098        5801 : ZM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
    1099             : {
    1100        5801 :   GEN h, H = *pH, q = *ptq, qp = muliu(q, p), qp2 = shifti(qp,-1);
    1101        5801 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1102        5801 :   long i,j, l = lg(H), m = lgcols(H);
    1103        5801 :   int stable = 1;
    1104       20944 :   for (j=1; j<l; j++)
    1105      157160 :     for (i=1; i<m; i++)
    1106             :     {
    1107      142017 :       h = Fl_chinese_coprime(gcoeff(H,i,j), coeff(Hp,i,j),q,p,qinv,qp,qp2);
    1108      142017 :       if (h) { gcoeff(H,i,j) = h; stable = 0; }
    1109             :     }
    1110        5801 :   *ptq = qp; return stable;
    1111             : }
    1112             : 
    1113             : GEN
    1114         623 : ZXM_init_CRT(GEN Hp, long deg, ulong p)
    1115             : {
    1116             :   long i, j, k;
    1117             :   GEN H;
    1118         623 :   long m, l = lg(Hp), lim = (long)(p>>1), n;
    1119         623 :   H = cgetg(l, t_MAT);
    1120         623 :   if (l==1) return H;
    1121         623 :   m = lgcols(Hp);
    1122         623 :   n = deg + 3;
    1123        2114 :   for (j=1; j<l; j++)
    1124             :   {
    1125        1491 :     GEN cp = gel(Hp,j);
    1126        1491 :     GEN c = cgetg(m, t_COL);
    1127        1491 :     gel(H,j) = c;
    1128       23905 :     for (i=1; i<m; i++)
    1129             :     {
    1130       22414 :       GEN dp = gel(cp, i);
    1131       22414 :       long l = lg(dp);
    1132       22414 :       GEN d = cgetg(n, t_POL);
    1133       22414 :       gel(c, i) = d;
    1134       22414 :       d[1] = dp[1] | evalsigne(1);
    1135       45647 :       for (k=2; k<l; k++)
    1136       23233 :         gel(d,k) = stoi(Fl_center(dp[k], p, lim));
    1137       44457 :       for (   ; k<n; k++)
    1138       22043 :         gel(d,k) = gen_0;
    1139             :     }
    1140             :   }
    1141         623 :   return H;
    1142             : }
    1143             : 
    1144             : int
    1145         653 : ZXM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
    1146             : {
    1147         653 :   GEN v, H = *pH, q = *ptq, qp = muliu(q, p), qp2 = shifti(qp,-1);
    1148         653 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1149         653 :   long i,j,k, l = lg(H), m = lgcols(H), n = lg(gmael(H,1,1));
    1150         653 :   int stable = 1;
    1151        2225 :   for (j=1; j<l; j++)
    1152       90418 :     for (i=1; i<m; i++)
    1153             :     {
    1154       88846 :       GEN h = gmael(H,j,i), hp = gmael(Hp,j,i);
    1155       88846 :       long lh = lg(hp);
    1156      246641 :       for (k=2; k<lh; k++)
    1157             :       {
    1158      157795 :         v = Fl_chinese_coprime(gel(h,k),uel(hp,k),q,p,qinv,qp,qp2);
    1159      157795 :         if (v) { gel(h,k) = v; stable = 0; }
    1160             :       }
    1161      108763 :       for (; k<n; k++)
    1162             :       {
    1163       19917 :         v = Fl_chinese_coprime(gel(h,k),0,q,p,qinv,qp,qp2);
    1164       19917 :         if (v) { gel(h,k) = v; stable = 0; }
    1165             :       }
    1166             :     }
    1167         653 :   *ptq = qp; return stable;
    1168             : }
    1169             : 
    1170             : /* record the degrees of Euclidean remainders (make them as large as
    1171             :  * possible : smaller values correspond to a degenerate sequence) */
    1172             : static void
    1173       23160 : Flx_resultant_set_dglist(GEN a, GEN b, GEN dglist, ulong p)
    1174             : {
    1175             :   long da,db,dc, ind;
    1176       23160 :   pari_sp av = avma;
    1177             : 
    1178       23160 :   if (lgpol(a)==0 || lgpol(b)==0) return;
    1179       21893 :   da = degpol(a);
    1180       21893 :   db = degpol(b);
    1181       21893 :   if (db > da)
    1182           0 :   { swapspec(a,b, da,db); }
    1183       21893 :   else if (!da) return;
    1184       21893 :   ind = 0;
    1185      144010 :   while (db)
    1186             :   {
    1187      122117 :     GEN c = Flx_rem(a,b, p);
    1188      122119 :     a = b; b = c; dc = degpol(c);
    1189      122117 :     if (dc < 0) break;
    1190             : 
    1191      122117 :     ind++;
    1192      122117 :     if (dc > dglist[ind]) dglist[ind] = dc;
    1193      122117 :     if (gc_needed(av,2))
    1194             :     {
    1195           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1196           0 :       gerepileall(av, 2, &a,&b);
    1197             :     }
    1198      122117 :     db = dc; /* = degpol(b) */
    1199             :   }
    1200       21893 :   if (ind+1 > lg(dglist)) setlg(dglist,ind+1);
    1201       21893 :   set_avma(av);
    1202             : }
    1203             : /* assuming the PRS finishes on a degree 1 polynomial C0 + C1X, with
    1204             :  * "generic" degree sequence as given by dglist, set *Ci and return
    1205             :  * resultant(a,b). Modular version of Collins's subresultant */
    1206             : static ulong
    1207     2083170 : Flx_resultant_all(GEN a, GEN b, long *C0, long *C1, GEN dglist, ulong p)
    1208             : {
    1209             :   long da,db,dc, ind;
    1210     2083170 :   ulong lb, res, g = 1UL, h = 1UL, ca = 1UL, cb = 1UL;
    1211     2083170 :   int s = 1;
    1212     2083170 :   pari_sp av = avma;
    1213             : 
    1214     2083170 :   *C0 = 1; *C1 = 0;
    1215     2083170 :   if (lgpol(a)==0 || lgpol(b)==0) return 0;
    1216     2073993 :   da = degpol(a);
    1217     2074029 :   db = degpol(b);
    1218     2073978 :   if (db > da)
    1219             :   {
    1220           0 :     swapspec(a,b, da,db);
    1221           0 :     if (both_odd(da,db)) s = -s;
    1222             :   }
    1223     2073978 :   else if (!da) return 1; /* = a[2] ^ db, since 0 <= db <= da = 0 */
    1224     2073978 :   ind = 0;
    1225    19787737 :   while (db)
    1226             :   { /* sub-resultant algo., applied to ca * a and cb * b, ca,cb scalars,
    1227             :      * da = deg a, db = deg b */
    1228    17718318 :     GEN c = Flx_rem(a,b, p);
    1229    17562775 :     long delta = da - db;
    1230             : 
    1231    17562775 :     if (both_odd(da,db)) s = -s;
    1232    17560177 :     lb = Fl_mul(b[db+2], cb, p);
    1233    17605336 :     a = b; b = c; dc = degpol(c);
    1234    17615968 :     ind++;
    1235    17615968 :     if (dc != dglist[ind]) return gc_ulong(av,0); /* degenerates */
    1236    17611077 :     if (g == h)
    1237             :     { /* frequent */
    1238    17551237 :       ulong cc = Fl_mul(ca, Fl_powu(Fl_div(lb,g,p), delta+1, p), p);
    1239    17657284 :       ca = cb;
    1240    17657284 :       cb = cc;
    1241             :     }
    1242             :     else
    1243             :     {
    1244       59840 :       ulong cc = Fl_mul(ca, Fl_powu(lb, delta+1, p), p);
    1245       59843 :       ulong ghdelta = Fl_mul(g, Fl_powu(h, delta, p), p);
    1246       59843 :       ca = cb;
    1247       59843 :       cb = Fl_div(cc, ghdelta, p);
    1248             :     }
    1249    17714406 :     da = db; /* = degpol(a) */
    1250    17714406 :     db = dc; /* = degpol(b) */
    1251             : 
    1252    17714406 :     g = lb;
    1253    17714406 :     if (delta == 1)
    1254    17615138 :       h = g; /* frequent */
    1255             :     else
    1256       99268 :       h = Fl_mul(h, Fl_powu(Fl_div(g,h,p), delta, p), p);
    1257             : 
    1258    17714678 :     if (gc_needed(av,2))
    1259             :     {
    1260           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1261           0 :       gerepileall(av, 2, &a,&b);
    1262             :     }
    1263             :   }
    1264     2069419 :   if (da > 1) return 0; /* Failure */
    1265             :   /* last nonconstant polynomial has degree 1 */
    1266     2069419 :   *C0 = Fl_mul(ca, a[2], p);
    1267     2069405 :   *C1 = Fl_mul(ca, a[3], p);
    1268     2069447 :   res = Fl_mul(cb, b[2], p);
    1269     2069442 :   if (s == -1) res = p - res;
    1270     2069442 :   return gc_ulong(av,res);
    1271             : }
    1272             : 
    1273             : /* Q a vector of polynomials representing B in Fp[X][Y], evaluate at X = x,
    1274             :  * Return 0 in case of degree drop. */
    1275             : static GEN
    1276     2106692 : FlxY_evalx_drop(GEN Q, ulong x, ulong p)
    1277             : {
    1278             :   GEN z;
    1279     2106692 :   long i, lb = lg(Q);
    1280     2106692 :   ulong leadz = Flx_eval(leading_coeff(Q), x, p);
    1281     2106168 :   long vs=mael(Q,2,1);
    1282     2106168 :   if (!leadz) return zero_Flx(vs);
    1283             : 
    1284     2095508 :   z = cgetg(lb, t_VECSMALL); z[1] = vs;
    1285    20036698 :   for (i=2; i<lb-1; i++) z[i] = Flx_eval(gel(Q,i), x, p);
    1286     2091311 :   z[i] = leadz; return z;
    1287             : }
    1288             : 
    1289             : GEN
    1290        2072 : FpXY_FpXQ_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
    1291             : {
    1292        2072 :   pari_sp av = avma;
    1293        2072 :   long i, lb = lg(Q);
    1294             :   GEN z;
    1295        2072 :   if (lb == 2) return pol_0(vx);
    1296        2072 :   z = gel(Q, lb-1);
    1297        2072 :   if (lb == 3 || !signe(y)) return typ(z)==t_INT? scalar_ZX(z, vx): ZX_copy(z);
    1298             : 
    1299        2072 :   if (typ(z) == t_INT) z = scalar_ZX_shallow(z, vx);
    1300       48636 :   for (i=lb-2; i>=2; i--)
    1301             :   {
    1302       46564 :     GEN c = gel(Q,i);
    1303       46564 :     z = FqX_Fq_mul(z, y, T, p);
    1304       46564 :     z = typ(c) == t_INT? FqX_Fq_add(z,c,T,p): FqX_add(z,c,T,p);
    1305             :   }
    1306        2072 :   return gerepileupto(av, z);
    1307             : }
    1308             : 
    1309             : static GEN
    1310      272257 : ZX_norml1(GEN x)
    1311             : {
    1312      272257 :   long i, l = lg(x);
    1313             :   GEN s;
    1314             : 
    1315      272257 :   if (l == 2) return gen_0;
    1316      179703 :   s = gel(x, l-1); /* != 0 */
    1317      657889 :   for (i = l-2; i > 1; i--) {
    1318      478191 :     GEN xi = gel(x,i);
    1319      478191 :     if (!signe(xi)) continue;
    1320      239794 :     s = addii_sign(s,1, xi,1);
    1321             :   }
    1322      179698 :   return s;
    1323             : }
    1324             : /* x >= 0, y != 0, return x + |y| */
    1325             : static GEN
    1326       25589 : addii_abs(GEN x, GEN y)
    1327             : {
    1328       25589 :   if (!signe(x)) return absi_shallow(y);
    1329       16051 :   return addii_sign(x,1, y,1);
    1330             : }
    1331             : 
    1332             : /* x a ZX, return sum_{i >= k} |x[i]| binomial(i, k) */
    1333             : static GEN
    1334       31720 : ZX_norml1_1(GEN x, long k)
    1335             : {
    1336       31720 :   long i, d = degpol(x);
    1337             :   GEN s, C; /* = binomial(i, k) */
    1338             : 
    1339       31721 :   if (!d || k > d) return gen_0;
    1340       31721 :   s = absi_shallow(gel(x, k+2)); /* may be 0 */
    1341       31722 :   C = gen_1;
    1342       68192 :   for (i = k+1; i <= d; i++) {
    1343       36477 :     GEN xi = gel(x,i+2);
    1344       36477 :     if (k) C = diviuexact(muliu(C, i), i-k);
    1345       36475 :     if (signe(xi)) s = addii_abs(s, mulii(C, xi));
    1346             :   }
    1347       31715 :   return s;
    1348             : }
    1349             : /* x has non-negative real coefficients */
    1350             : static GEN
    1351        3255 : RgX_norml1_1(GEN x, long k)
    1352             : {
    1353        3255 :   long i, d = degpol(x);
    1354             :   GEN s, C; /* = binomial(i, k) */
    1355             : 
    1356        3255 :   if (!d || k > d) return gen_0;
    1357        3255 :   s = gel(x, k+2); /* may be 0 */
    1358        3255 :   C = gen_1;
    1359        9128 :   for (i = k+1; i <= d; i++) {
    1360        5873 :     GEN xi = gel(x,i+2);
    1361        5873 :     if (k) C = diviuexact(muliu(C, i), i-k);
    1362        5873 :     if (!gequal0(xi)) s = gadd(s, gmul(C, xi));
    1363             :   }
    1364        3255 :   return s;
    1365             : }
    1366             : 
    1367             : /* N_2(A)^2 */
    1368             : static GEN
    1369        7227 : sqrN2(GEN A, long prec)
    1370             : {
    1371        7227 :   pari_sp av = avma;
    1372        7227 :   long i, l = lg(A);
    1373        7227 :   GEN a = gen_0;
    1374       35607 :   for (i = 2; i < l; i++)
    1375             :   {
    1376       28380 :     a = gadd(a, gabs(gnorm(gel(A,i)), prec));
    1377       28380 :     if (gc_needed(av,1))
    1378             :     {
    1379           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_RgXY_ResBound i = %ld",i);
    1380           0 :       a = gerepileupto(av, a);
    1381             :     }
    1382             :   }
    1383        7227 :   return a;
    1384             : }
    1385             : /* Interpolate at roots of 1 and use Hadamard bound for univariate resultant:
    1386             :  *   bound = N_2(A)^degpol B N_2(B)^degpol(A),  where
    1387             :  *     N_2(A) = sqrt(sum (N_1(Ai))^2)
    1388             :  * Return e such that Res(A, B) < 2^e */
    1389             : static GEN
    1390        6380 : RgX_RgXY_ResBound(GEN A, GEN B, long prec)
    1391             : {
    1392        6380 :   pari_sp av = avma;
    1393        6380 :   GEN b = gen_0, bnd;
    1394        6380 :   long i, lB = lg(B);
    1395       25440 :   for (i=2; i<lB; i++)
    1396             :   {
    1397       19060 :     GEN t = gel(B,i);
    1398       19060 :     if (typ(t) == t_POL) t = gnorml1(t, prec);
    1399       19060 :     b = gadd(b, gabs(gsqr(t), prec));
    1400       19060 :     if (gc_needed(av,1))
    1401             :     {
    1402           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_RgXY_ResBound i = %ld",i);
    1403           0 :       b = gerepileupto(av, b);
    1404             :     }
    1405             :   }
    1406        6380 :   bnd = gsqrt(gmul(gpowgs(sqrN2(A,prec), degpol(B)),
    1407             :                    gpowgs(b, degpol(A))), prec);
    1408        6380 :   return gerepileupto(av, bnd);
    1409             : }
    1410             : /* A,B in C[X] return RgX_RgXY_ResBound(A, B(x+y)) */
    1411             : static GEN
    1412         847 : RgX_RgXY_ResBound_1(GEN A, GEN B, long prec)
    1413             : {
    1414         847 :   pari_sp av = avma, av2;
    1415         847 :   GEN b = gen_0, bnd;
    1416         847 :   long i, lB = lg(B);
    1417         847 :   B = shallowcopy(B);
    1418        4102 :   for (i=2; i<lB; i++) gel(B,i) = gabs(gel(B,i), prec);
    1419         847 :   av2 = avma;
    1420        4102 :   for (i=2; i<lB; i++)
    1421             :   {
    1422        3255 :     b = gadd(b, gsqr(RgX_norml1_1(B, i-2)));
    1423        3255 :     if (gc_needed(av2,1))
    1424             :     {
    1425           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_RgXY_ResBound i = %ld",i);
    1426           0 :       b = gerepileupto(av2, b);
    1427             :     }
    1428             :   }
    1429         847 :   bnd = gsqrt(gmul(gpowgs(sqrN2(A,prec), degpol(B)),
    1430             :                    gpowgs(b, degpol(A))), prec);
    1431         847 :   return gerepileupto(av, bnd);
    1432             : }
    1433             : 
    1434             : /* log2 N_2(A)^2 */
    1435             : static double
    1436      187509 : log2N2(GEN A)
    1437             : {
    1438      187509 :   pari_sp av = avma;
    1439      187509 :   long i, l = lg(A);
    1440      187509 :   GEN a = gen_0;
    1441     1130965 :   for (i=2; i < l; i++)
    1442             :   {
    1443      943469 :     a = addii(a, sqri(gel(A,i)));
    1444      943455 :     if (gc_needed(av,1))
    1445             :     {
    1446           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
    1447           0 :       a = gerepileupto(av, a);
    1448             :     }
    1449             :   }
    1450      187496 :   return gc_double(av, dbllog2(a));
    1451             : }
    1452             : /* Interpolate at roots of 1 and use Hadamard bound for univariate resultant:
    1453             :  *   bound = N_2(A)^degpol B N_2(B)^degpol(A),  where
    1454             :  *     N_2(A) = sqrt(sum (N_1(Ai))^2)
    1455             :  * Return e such that Res(A, B) < 2^e */
    1456             : ulong
    1457      177406 : ZX_ZXY_ResBound(GEN A, GEN B, GEN dB)
    1458             : {
    1459      177406 :   pari_sp av = avma;
    1460      177406 :   GEN b = gen_0;
    1461      177406 :   long i, lB = lg(B);
    1462             :   double logb;
    1463     1029972 :   for (i=2; i<lB; i++)
    1464             :   {
    1465      852576 :     GEN t = gel(B,i);
    1466      852576 :     if (typ(t) == t_POL) t = ZX_norml1(t);
    1467      852577 :     b = addii(b, sqri(t));
    1468      852565 :     if (gc_needed(av,1))
    1469             :     {
    1470           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
    1471           0 :       b = gerepileupto(av, b);
    1472             :     }
    1473             :   }
    1474      177396 :   logb = dbllog2(b); if (dB) logb -= 2 * dbllog2(dB);
    1475      177405 :   i = (long)((degpol(B) * log2N2(A) + degpol(A) * logb) / 2);
    1476      177406 :   return gc_ulong(av, (i <= 0)? 1: 1 + (ulong)i);
    1477             : }
    1478             : /* A,B ZX. Return ZX_ZXY_ResBound(A(x), B(x+y)) */
    1479             : static ulong
    1480       10105 : ZX_ZXY_ResBound_1(GEN A, GEN B)
    1481             : {
    1482       10105 :   pari_sp av = avma;
    1483       10105 :   GEN b = gen_0;
    1484       10105 :   long i, lB = lg(B);
    1485       41824 :   for (i=2; i<lB; i++)
    1486             :   {
    1487       31720 :     b = addii(b, sqri(ZX_norml1_1(B, i-2)));
    1488       31719 :     if (gc_needed(av,1))
    1489             :     {
    1490           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
    1491           0 :       b = gerepileupto(av, b);
    1492             :     }
    1493             :   }
    1494       10104 :   i = (long)((degpol(B) * log2N2(A) + degpol(A) * dbllog2(b)) / 2);
    1495       10104 :   return gc_ulong(av, (i <= 0)? 1: 1 + (ulong)i);
    1496             : }
    1497             : /* special case B = A' */
    1498             : static ulong
    1499     1127824 : ZX_discbound(GEN A)
    1500             : {
    1501     1127824 :   pari_sp av = avma;
    1502     1127824 :   GEN a = gen_0, b = gen_0;
    1503     1127824 :   long i , lA = lg(A), dA = degpol(A);
    1504             :   double loga, logb;
    1505     6715468 :   for (i = 2; i < lA; i++)
    1506             :   {
    1507     5587962 :     GEN c = sqri(gel(A,i));
    1508     5587433 :     a = addii(a, c);
    1509     5587563 :     if (i > 2) b = addii(b, mulii(c, sqru(i-2)));
    1510     5587583 :     if (gc_needed(av,1))
    1511             :     {
    1512           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZX_discbound i = %ld",i);
    1513           0 :       gerepileall(av, 2, &a, &b);
    1514             :     }
    1515             :   }
    1516     1127506 :   loga = dbllog2(a);
    1517     1127739 :   logb = dbllog2(b); set_avma(av);
    1518     1127775 :   i = (long)(((dA-1) * loga + dA * logb) / 2);
    1519     1127775 :   return (i <= 0)? 1: 1 + (ulong)i;
    1520             : }
    1521             : 
    1522             : /* return Res(a(Y), b(n,Y)) over Fp. la = leading_coeff(a) [for efficiency] */
    1523             : static ulong
    1524     2267881 : Flx_FlxY_eval_resultant(GEN a, GEN b, ulong n, ulong p, ulong pi, ulong la)
    1525             : {
    1526     2267881 :   GEN ev = FlxY_evalx_pre(b, n, p, pi);
    1527     2268031 :   long drop = lg(b) - lg(ev);
    1528     2268031 :   ulong r = Flx_resultant_pre(a, ev, p, pi);
    1529     2267798 :   if (drop && la != 1) r = Fl_mul(r, Fl_powu_pre(la, drop, p, pi), p);
    1530     2267806 :   return r;
    1531             : }
    1532             : static GEN
    1533         284 : FpX_FpXY_eval_resultant(GEN a, GEN b, GEN n, GEN p, GEN la, long db, long vX)
    1534             : {
    1535         284 :   GEN ev = FpXY_evaly(b, n, p, vX);
    1536         284 :   long drop = db-degpol(ev);
    1537         284 :   GEN r = FpX_resultant(a, ev, p);
    1538         284 :   if (drop && !gequal1(la)) r = Fp_mul(r, Fp_powu(la, drop,p),p);
    1539         284 :   return r;
    1540             : }
    1541             : 
    1542             : /* assume dres := deg(Res_X(a,b), Y) <= deg(a,X) * deg(b,Y) < p */
    1543             : /* Return a Fly */
    1544             : static GEN
    1545      177133 : Flx_FlxY_resultant_polint(GEN a, GEN b, ulong p, ulong pi, long dres, long sx)
    1546             : {
    1547             :   long i;
    1548      177133 :   ulong n, la = Flx_lead(a);
    1549      177133 :   GEN  x = cgetg(dres+2, t_VECSMALL);
    1550      177132 :   GEN  y = cgetg(dres+2, t_VECSMALL);
    1551             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1552             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1553     1225721 :   for (i=0,n = 1; i < dres; n++)
    1554             :   {
    1555     1048600 :     x[++i] = n;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,pi,la);
    1556     1048536 :     x[++i] = p-n; y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,pi,la);
    1557             :   }
    1558      177121 :   if (i == dres)
    1559             :   {
    1560      171695 :     x[++i] = 0;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,pi,la);
    1561             :   }
    1562      177129 :   return Flv_polint(x,y, p, sx);
    1563             : }
    1564             : 
    1565             : static GEN
    1566        7808 : FlxX_pseudorem(GEN x, GEN y, ulong p, ulong pi)
    1567             : {
    1568        7808 :   long vx = varn(x), dx, dy, dz, i, lx, dp;
    1569        7808 :   pari_sp av = avma, av2;
    1570             : 
    1571        7808 :   if (!signe(y)) pari_err_INV("FlxX_pseudorem",y);
    1572        7808 :   (void)new_chunk(2);
    1573        7810 :   dx=degpol(x); x = RgX_recip_i(x)+2;
    1574        7810 :   dy=degpol(y); y = RgX_recip_i(y)+2; dz=dx-dy; dp = dz+1;
    1575        7812 :   av2 = avma;
    1576             :   for (;;)
    1577             :   {
    1578       63769 :     gel(x,0) = Flx_neg(gel(x,0), p); dp--;
    1579      238052 :     for (i=1; i<=dy; i++)
    1580      172857 :       gel(x,i) = Flx_add( Flx_mul_pre(gel(y,0), gel(x,i), p, pi),
    1581      174238 :                           Flx_mul_pre(gel(x,0), gel(y,i), p, pi), p );
    1582     1105316 :     for (   ; i<=dx; i++)
    1583     1042365 :       gel(x,i) = Flx_mul_pre(gel(y,0), gel(x,i), p, pi);
    1584       67721 :     do { x++; dx--; } while (dx >= 0 && lg(gel(x,0))==2);
    1585       62951 :     if (dx < dy) break;
    1586       55156 :     if (gc_needed(av2,1))
    1587             :     {
    1588           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_pseudorem dx = %ld >= %ld",dx,dy);
    1589           0 :       gerepilecoeffs(av2,x,dx+1);
    1590             :     }
    1591             :   }
    1592        7795 :   if (dx < 0) return zero_Flx(0);
    1593        7795 :   lx = dx+3; x -= 2;
    1594        7795 :   x[0]=evaltyp(t_POL) | _evallg(lx);
    1595        7795 :   x[1]=evalsigne(1) | evalvarn(vx);
    1596        7795 :   x = RgX_recip_i(x);
    1597        7805 :   if (dp)
    1598             :   { /* multiply by y[0]^dp   [beware dummy vars from FpX_FpXY_resultant] */
    1599        2042 :     GEN t = Flx_powu_pre(gel(y,0), dp, p, pi);
    1600        8164 :     for (i=2; i<lx; i++) gel(x,i) = Flx_mul_pre(gel(x,i), t, p, pi);
    1601             :   }
    1602        7809 :   return gerepilecopy(av, x);
    1603             : }
    1604             : 
    1605             : /* return a Flx */
    1606             : GEN
    1607        2615 : FlxX_resultant(GEN u, GEN v, ulong p, long sx)
    1608             : {
    1609        2615 :   pari_sp av = avma, av2;
    1610             :   long degq, dx, dy, du, dv, dr, signh;
    1611             :   ulong pi;
    1612             :   GEN z, g, h, r, p1;
    1613             : 
    1614        2615 :   dx = degpol(u); dy = degpol(v); signh = 1;
    1615        2615 :   if (dx < dy)
    1616             :   {
    1617           7 :     swap(u,v); lswap(dx,dy);
    1618           7 :     if (both_odd(dx, dy)) signh = -signh;
    1619             :   }
    1620        2615 :   if (dy < 0) return zero_Flx(sx);
    1621        2615 :   pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1622        2615 :   if (dy==0) return gerepileupto(av, Flx_powu_pre(gel(v,2),dx,p,pi));
    1623             : 
    1624        2615 :   g = h = pol1_Flx(sx); av2 = avma;
    1625             :   for(;;)
    1626             :   {
    1627        7810 :     r = FlxX_pseudorem(u,v,p,pi); dr = lg(r);
    1628        7817 :     if (dr == 2) { set_avma(av); return zero_Flx(sx); }
    1629        7817 :     du = degpol(u); dv = degpol(v); degq = du-dv;
    1630        7817 :     u = v; p1 = g; g = leading_coeff(u);
    1631        7817 :     switch(degq)
    1632             :     {
    1633           0 :       case 0: break;
    1634        5760 :       case 1:
    1635        5760 :         p1 = Flx_mul_pre(h,p1, p, pi); h = g; break;
    1636        2057 :       default:
    1637        2057 :         p1 = Flx_mul_pre(Flx_powu_pre(h,degq,p,pi), p1, p, pi);
    1638        2054 :         h = Flx_div_pre(Flx_powu_pre(g,degq,p,pi),
    1639        2056 :                         Flx_powu_pre(h,degq-1,p,pi), p, pi);
    1640             :     }
    1641        7808 :     if (both_odd(du,dv)) signh = -signh;
    1642        7805 :     v = FlxY_Flx_div(r, p1, p);
    1643        7806 :     if (dr==3) break;
    1644        5191 :     if (gc_needed(av2,1))
    1645             :     {
    1646           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_resultant, dr = %ld",dr);
    1647           0 :       gerepileall(av2,4, &u, &v, &g, &h);
    1648             :     }
    1649             :   }
    1650        2615 :   z = gel(v,2);
    1651        2615 :   if (dv > 1) z = Flx_div_pre(Flx_powu_pre(z,dv,p,pi),
    1652           0 :                               Flx_powu_pre(h,dv-1,p,pi), p, pi);
    1653        2615 :   if (signh < 0) z = Flx_neg(z,p);
    1654        2615 :   return gerepileupto(av, z);
    1655             : }
    1656             : 
    1657             : /* Warning:
    1658             :  * This function switches between valid and invalid variable ordering*/
    1659             : 
    1660             : static GEN
    1661        6233 : FlxY_to_FlyX(GEN b, long sv)
    1662             : {
    1663        6233 :   long i, n=-1;
    1664        6233 :   long sw = b[1]&VARNBITS;
    1665       21319 :   for(i=2;i<lg(b);i++) n = maxss(n,lgpol(gel(b,i)));
    1666        6233 :   return Flm_to_FlxX(Flm_transpose(FlxX_to_Flm(b,n)),sv,sw);
    1667             : }
    1668             : 
    1669             : /* Return a Fly*/
    1670             : GEN
    1671        6233 : Flx_FlxY_resultant(GEN a, GEN b, ulong p)
    1672             : {
    1673        6233 :   pari_sp ltop=avma;
    1674        6233 :   long dres = degpol(a)*degpol(b);
    1675        6233 :   long sx=a[1], sy=b[1]&VARNBITS;
    1676             :   GEN z;
    1677        6233 :   b = FlxY_to_FlyX(b,sx);
    1678        6232 :   if ((ulong)dres >= p)
    1679        2614 :     z = FlxX_resultant(Fly_to_FlxY(a, sy), b, p, sx);
    1680             :   else
    1681             :   {
    1682        3618 :     ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1683        3618 :     z = Flx_FlxY_resultant_polint(a, b, p, pi, (ulong)dres, sy);
    1684             :   }
    1685        6233 :   return gerepileupto(ltop,z);
    1686             : }
    1687             : 
    1688             : /* return a t_POL (in variable v >= 0) whose coeffs are the coeffs of b,
    1689             :  * in variable v. This is an incorrect PARI object if initially varn(b) << v.
    1690             :  * We could return a vector of coeffs, but it is convenient to have degpol()
    1691             :  * and friends available. Even in that case, it will behave nicely with all
    1692             :  * functions treating a polynomial as a vector of coeffs (eg poleval).
    1693             :  * FOR INTERNAL USE! */
    1694             : GEN
    1695      125936 : swap_vars(GEN b0, long v)
    1696             : {
    1697      125936 :   long i, n = RgX_degree(b0, v);
    1698             :   GEN b, x;
    1699      125936 :   if (n < 0) return pol_0(v);
    1700      125936 :   b = cgetg(n+3, t_POL); x = b + 2;
    1701      125936 :   b[1] = evalsigne(1) | evalvarn(v);
    1702      640192 :   for (i=0; i<=n; i++) gel(x,i) = polcoef_i(b0, i, v);
    1703      125937 :   return b;
    1704             : }
    1705             : 
    1706             : /* assume varn(b) << varn(a) */
    1707             : /* return a FpY*/
    1708             : GEN
    1709          15 : FpX_FpXY_resultant(GEN a, GEN b, GEN p)
    1710             : {
    1711          15 :   long i,n,dres, db, vY = varn(b), vX = varn(a);
    1712             :   GEN la,x,y;
    1713             : 
    1714          15 :   if (lgefint(p) == 3)
    1715             :   {
    1716           0 :     ulong pp = uel(p,2);
    1717           0 :     b = ZXX_to_FlxX(b, pp, vX);
    1718           0 :     a = ZX_to_Flx(a, pp);
    1719           0 :     x = Flx_FlxY_resultant(a, b, pp);
    1720           0 :     return Flx_to_ZX(x);
    1721             :   }
    1722          15 :   db = RgXY_degreex(b);
    1723          15 :   dres = degpol(a)*degpol(b);
    1724          15 :   la = leading_coeff(a);
    1725          15 :   x = cgetg(dres+2, t_VEC);
    1726          15 :   y = cgetg(dres+2, t_VEC);
    1727             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1728             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1729         157 :   for (i=0,n = 1; i < dres; n++)
    1730             :   {
    1731         142 :     gel(x,++i) = utoipos(n);
    1732         142 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1733         142 :     gel(x,++i) = subiu(p,n);
    1734         142 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1735             :   }
    1736          15 :   if (i == dres)
    1737             :   {
    1738           0 :     gel(x,++i) = gen_0;
    1739           0 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b, gel(x,i), p,la,db,vY);
    1740             :   }
    1741          15 :   return FpV_polint(x,y, p, vY);
    1742             : }
    1743             : 
    1744             : GEN
    1745          79 : FpX_composedsum(GEN P, GEN Q, GEN p)
    1746             : {
    1747          79 :   pari_sp av = avma;
    1748          79 :   if (lgefint(p)==3)
    1749             :   {
    1750           0 :     ulong pp = p[2];
    1751           0 :     GEN z = Flx_composedsum(ZX_to_Flx(P, pp), ZX_to_Flx(Q, pp), pp);
    1752           0 :     return gerepileupto(av, Flx_to_ZX(z));
    1753             :   }
    1754             :   else
    1755             :   {
    1756          79 :     long n = 1+ degpol(P)*degpol(Q);
    1757          79 :     GEN Pl = FpX_invLaplace(FpX_Newton(P,n,p), p);
    1758          79 :     GEN Ql = FpX_invLaplace(FpX_Newton(Q,n,p), p);
    1759          79 :     GEN L = FpX_Laplace(FpXn_mul(Pl, Ql, n, p), p);
    1760          79 :     GEN lead = Fp_mul(Fp_powu(leading_coeff(P),degpol(Q), p),
    1761          79 :         Fp_powu(leading_coeff(Q),degpol(P), p), p);
    1762          79 :     GEN R = FpX_fromNewton(L, p);
    1763          79 :     return gerepileupto(av, FpX_Fp_mul(R, lead, p));
    1764             :   }
    1765             : }
    1766             : 
    1767             : GEN
    1768           0 : FpX_composedprod(GEN P, GEN Q, GEN p)
    1769             : {
    1770           0 :   pari_sp av = avma;
    1771           0 :   if (lgefint(p)==3)
    1772             :   {
    1773           0 :     ulong pp = p[2];
    1774           0 :     GEN z = Flx_composedprod(ZX_to_Flx(P, pp), ZX_to_Flx(Q, pp), pp);
    1775           0 :     return gerepileupto(av, Flx_to_ZX(z));
    1776             :   }
    1777             :   else
    1778             :   {
    1779           0 :     long n = 1+ degpol(P)*degpol(Q);
    1780           0 :     GEN L = FpX_convol(FpX_Newton(P,n,p), FpX_Newton(Q,n,p), p);
    1781           0 :     return gerepileupto(av,FpX_fromNewton(L, p));
    1782             :   }
    1783             : }
    1784             : 
    1785             : static GEN
    1786          79 : _FpX_composedsum(void *E, GEN a, GEN b)
    1787          79 : { return FpX_composedsum(a,b, (GEN)E); }
    1788             : 
    1789             : GEN
    1790        1574 : FpXV_composedsum(GEN V, GEN p)
    1791             : {
    1792        1574 :   if (lgefint(p)==3)
    1793             :   {
    1794           0 :     ulong pp = p[2];
    1795           0 :     return Flx_to_ZX(FlxV_composedsum(ZXV_to_FlxV(V, pp), pp));
    1796             :   }
    1797        1574 :   return gen_product(V, (void *)p, &_FpX_composedsum);
    1798             : }
    1799             : 
    1800             : /* 0, 1, -1, 2, -2, ... */
    1801             : #define next_lambda(a) (a>0 ? -a : 1-a)
    1802             : 
    1803             : /* Assume A in Z[Y], B in Q[Y][X], B squarefree in (Q[Y]/(A))[X] and
    1804             :  * Res_Y(A, B) in Z[X]. Find a small lambda (start from *lambda, use
    1805             :  * next_lambda successively) such that C(X) = Res_Y(A(Y), B(X + lambda Y))
    1806             :  * is squarefree, reset *lambda to the chosen value and return C. Set LERS to
    1807             :  * the Last nonconstant polynomial in the Euclidean Remainder Sequence */
    1808             : static GEN
    1809       20993 : ZX_ZXY_resultant_LERS(GEN A, GEN B0, long *plambda, GEN *LERS)
    1810             : {
    1811             :   ulong bound, dp;
    1812       20993 :   pari_sp av = avma, av2 = 0;
    1813       20993 :   long lambda = *plambda, degA = degpol(A), dres = degA*degpol(B0);
    1814             :   long stable, checksqfree, i,n, cnt, degB;
    1815       20993 :   long v, vX = varn(B0), vY = varn(A); /* vY < vX */
    1816             :   GEN x, y, dglist, B, q, a, b, ev, H, H0, H1, Hp, H0p, H1p, C0, C1;
    1817             :   forprime_t S;
    1818             : 
    1819       20993 :   if (degA == 1)
    1820             :   {
    1821        1043 :     GEN a1 = gel(A,3), a0 = gel(A,2);
    1822        1043 :     B = lambda? RgX_translate(B0, monomial(stoi(lambda), 1, vY)): B0;
    1823        1043 :     H = gsubst(B, vY, gdiv(gneg(a0),a1));
    1824        1043 :    if (!equali1(a1)) H = RgX_Rg_mul(H, powiu(a1, poldegree(B,vY)));
    1825        1043 :     *LERS = mkvec2(scalarpol_shallow(a0,vX), scalarpol_shallow(a1,vX));
    1826        1043 :     return gc_all(av, 2, &H, LERS);
    1827             :   }
    1828             : 
    1829       19950 :   dglist = Hp = H0p = H1p = C0 = C1 = NULL; /* gcc -Wall */
    1830       19950 :   C0 = cgetg(dres+2, t_VECSMALL);
    1831       19950 :   C1 = cgetg(dres+2, t_VECSMALL);
    1832       19950 :   dglist = cgetg(dres+1, t_VECSMALL);
    1833       19950 :   x = cgetg(dres+2, t_VECSMALL);
    1834       19950 :   y = cgetg(dres+2, t_VECSMALL);
    1835       19950 :   B0 = leafcopy(B0);
    1836       19950 :   A = leafcopy(A);
    1837       19950 :   B = B0;
    1838       19950 :   v = fetch_var_higher(); setvarn(A,v);
    1839             :   /* make sure p large enough */
    1840       20612 : INIT:
    1841             :   /* always except the first time */
    1842       20612 :   if (av2) { set_avma(av2); lambda = next_lambda(lambda); }
    1843       20612 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    1844       20612 :   B = swap_vars(B, vY); setvarn(B,v);
    1845             :   /* B0(lambda v + x, v) */
    1846       20612 :   if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    1847       20612 :   av2 = avma;
    1848             : 
    1849       20612 :   if (degA <= 3)
    1850             :   { /* sub-resultant faster for small degrees */
    1851        9989 :     H = RgX_resultant_all(A,B,&q);
    1852        9989 :     if (typ(q) != t_POL || degpol(q)!=1) goto INIT;
    1853        9478 :     H0 = gel(q,2);
    1854        9478 :     if (typ(H0) == t_POL) setvarn(H0,vX); else H0 = scalarpol(H0,vX);
    1855        9478 :     H1 = gel(q,3);
    1856        9478 :     if (typ(H1) == t_POL) setvarn(H1,vX); else H1 = scalarpol(H1,vX);
    1857        9478 :     if (!ZX_is_squarefree(H)) goto INIT;
    1858        9436 :     goto END;
    1859             :   }
    1860             : 
    1861       10623 :   H = H0 = H1 = NULL;
    1862       10623 :   degB = degpol(B);
    1863       10623 :   bound = ZX_ZXY_ResBound(A, B, NULL);
    1864       10623 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    1865       10623 :   dp = 1;
    1866       10623 :   init_modular_big(&S);
    1867       10623 :   for(cnt = 0, checksqfree = 1;;)
    1868       49118 :   {
    1869       59741 :     ulong p = u_forprime_next(&S);
    1870             :     GEN Hi;
    1871       59741 :     a = ZX_to_Flx(A, p);
    1872       59740 :     b = ZXX_to_FlxX(B, p, varn(A));
    1873       59741 :     if (degpol(a) < degA || degpol(b) < degB) continue; /* p | lc(A)lc(B) */
    1874       59741 :     if (checksqfree)
    1875             :     { /* find degree list for generic Euclidean Remainder Sequence */
    1876       10623 :       long goal = minss(degpol(a), degpol(b)); /* longest possible */
    1877       72895 :       for (n=1; n <= goal; n++) dglist[n] = 0;
    1878       10623 :       setlg(dglist, 1);
    1879       23552 :       for (n=0; n <= dres; n++)
    1880             :       {
    1881       23160 :         ev = FlxY_evalx_drop(b, n, p);
    1882       23160 :         Flx_resultant_set_dglist(a, ev, dglist, p);
    1883       23160 :         if (lg(dglist)-1 == goal) break;
    1884             :       }
    1885             :       /* last pol in ERS has degree > 1 ? */
    1886       10623 :       goal = lg(dglist)-1;
    1887       10623 :       if (degpol(B) == 1) { if (!goal) goto INIT; }
    1888             :       else
    1889             :       {
    1890       10567 :         if (goal <= 1) goto INIT;
    1891       10511 :         if (dglist[goal] != 0 || dglist[goal-1] != 1) goto INIT;
    1892             :       }
    1893       10567 :       if (DEBUGLEVEL>4)
    1894           0 :         err_printf("Degree list for ERS (trials: %ld) = %Ps\n",n+1,dglist);
    1895             :     }
    1896             : 
    1897     2143233 :     for (i=0,n = 0; i <= dres; n++)
    1898             :     {
    1899     2083575 :       ev = FlxY_evalx_drop(b, n, p);
    1900     2083188 :       x[++i] = n; y[i] = Flx_resultant_all(a, ev, C0+i, C1+i, dglist, p);
    1901     2083548 :       if (!C1[i]) i--; /* C1(i) = 0. No way to recover C0(i) */
    1902             :     }
    1903       59658 :     Hi = Flv_Flm_polint(x, mkvec3(y,C0,C1), p, 0);
    1904       59685 :     Hp = gel(Hi,1); H0p = gel(Hi,2); H1p = gel(Hi,3);
    1905       59685 :     if (!H && degpol(Hp) != dres) continue;
    1906       59685 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1907       59685 :     if (checksqfree) {
    1908       10567 :       if (!Flx_is_squarefree(Hp, p)) goto INIT;
    1909       10514 :       if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    1910       10514 :       checksqfree = 0;
    1911             :     }
    1912             : 
    1913       59632 :     if (!H)
    1914             :     { /* initialize */
    1915       10514 :       q = utoipos(p); stable = 0;
    1916       10514 :       H = ZX_init_CRT(Hp, p,vX);
    1917       10514 :       H0= ZX_init_CRT(H0p, p,vX);
    1918       10514 :       H1= ZX_init_CRT(H1p, p,vX);
    1919             :     }
    1920             :     else
    1921             :     {
    1922       49118 :       GEN qp = muliu(q,p);
    1923       49116 :       stable  = ZX_incremental_CRT_raw(&H, Hp, q,qp, p)
    1924       49118 :               & ZX_incremental_CRT_raw(&H0,H0p, q,qp, p)
    1925       49118 :               & ZX_incremental_CRT_raw(&H1,H1p, q,qp, p);
    1926       49118 :       q = qp;
    1927             :     }
    1928             :     /* could make it probabilistic for H ? [e.g if stable twice, etc]
    1929             :      * Probabilistic anyway for H0, H1 */
    1930       59632 :     if (DEBUGLEVEL>5 && (stable ||  ++cnt==100))
    1931           0 :     { cnt=0; err_printf("%ld%%%s ",100*expi(q)/bound,stable?"s":""); }
    1932       59632 :     if (stable && (ulong)expi(q) >= bound) break; /* DONE */
    1933       49118 :     if (gc_needed(av,2))
    1934             :     {
    1935           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_rnfequation");
    1936           0 :       gerepileall(av2, 4, &H, &q, &H0, &H1);
    1937             :     }
    1938             :   }
    1939       19950 : END:
    1940       19950 :   if (DEBUGLEVEL>5) err_printf(" done\n");
    1941       19950 :   setvarn(H, vX); (void)delete_var();
    1942       19950 :   *LERS = mkvec2(H0,H1);
    1943       19950 :   *plambda = lambda; return gc_all(av, 2, &H, LERS);
    1944             : }
    1945             : 
    1946             : GEN
    1947       58758 : ZX_ZXY_resultant_all(GEN A, GEN B, long *plambda, GEN *LERS)
    1948             : {
    1949       58758 :   if (LERS)
    1950             :   {
    1951       20993 :     if (!plambda)
    1952           0 :       pari_err_BUG("ZX_ZXY_resultant_all [LERS != NULL needs lambda]");
    1953       20993 :     return ZX_ZXY_resultant_LERS(A, B, plambda, LERS);
    1954             :   }
    1955       37765 :   return ZX_ZXY_rnfequation(A, B, plambda);
    1956             : }
    1957             : 
    1958             : /* If lambda = NULL, return caract(Mod(A, T)), T,A in Z[X].
    1959             :  * Otherwise find a small lambda such that caract (Mod(A + lambda X, T)) is
    1960             :  * squarefree */
    1961             : GEN
    1962        3476 : ZXQ_charpoly_sqf(GEN A, GEN T, long *lambda, long v)
    1963             : {
    1964        3476 :   pari_sp av = avma;
    1965             :   GEN R, a;
    1966             :   long dA;
    1967             :   int delvar;
    1968             : 
    1969        3476 :   if (v < 0) v = 0;
    1970        3476 :   switch (typ(A))
    1971             :   {
    1972        3476 :     case t_POL: dA = degpol(A); if (dA > 0) break;
    1973           0 :       A = constant_coeff(A);
    1974           0 :     default:
    1975           0 :       if (lambda) { A = scalar_ZX_shallow(A,varn(T)); dA = 0; break;}
    1976           0 :       return gerepileupto(av, gpowgs(gsub(pol_x(v), A), degpol(T)));
    1977             :   }
    1978        3476 :   delvar = 0;
    1979        3476 :   if (varn(T) == 0)
    1980             :   {
    1981        3275 :     long v0 = fetch_var(); delvar = 1;
    1982        3275 :     T = leafcopy(T); setvarn(T,v0);
    1983        3275 :     A = leafcopy(A); setvarn(A,v0);
    1984             :   }
    1985        3476 :   R = ZX_ZXY_rnfequation(T, deg1pol_shallow(gen_1, gneg_i(A), 0), lambda);
    1986        3476 :   if (delvar) (void)delete_var();
    1987        3476 :   setvarn(R, v); a = leading_coeff(T);
    1988        3476 :   if (!gequal1(a)) R = gdiv(R, powiu(a, dA));
    1989        3476 :   return gerepileupto(av, R);
    1990             : }
    1991             : 
    1992             : /* charpoly(Mod(A,T)), A may be in Q[X], but assume T and result are integral */
    1993             : GEN
    1994      120748 : ZXQ_charpoly(GEN A, GEN T, long v)
    1995             : {
    1996      120748 :   return (degpol(T) < 16) ? RgXQ_charpoly_i(A,T,v): ZXQ_charpoly_sqf(A,T, NULL, v);
    1997             : }
    1998             : 
    1999             : GEN
    2000        9723 : QXQ_charpoly(GEN A, GEN T, long v)
    2001             : {
    2002        9723 :   pari_sp av = avma;
    2003        9723 :   GEN den, B = Q_remove_denom(A, &den);
    2004        9723 :   GEN P = ZXQ_charpoly(B, T, v);
    2005        9723 :   return gerepilecopy(av, den ? RgX_rescale(P, ginv(den)): P);
    2006             : }
    2007             : 
    2008             : static ulong
    2009     3965843 : ZX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, ulong p)
    2010             : {
    2011     3965843 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    2012             :   ulong H, dp;
    2013     3965705 :   if (dropa && dropb) return 0; /* p | lc(A), p | lc(B) */
    2014     3965705 :   H = Flx_resultant(a, b, p);
    2015     3965269 :   if (dropa)
    2016             :   { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    2017           0 :     ulong c = b[degB+2]; /* lc(B) */
    2018           0 :     if (odd(degB)) c = p - c;
    2019           0 :     c = Fl_powu(c, dropa, p);
    2020           0 :     if (c != 1) H = Fl_mul(H, c, p);
    2021             :   }
    2022     3965269 :   else if (dropb)
    2023             :   { /* multiply by lc(A)^(deg B - deg b) */
    2024           0 :     ulong c = a[degA+2]; /* lc(A) */
    2025           0 :     c = Fl_powu(c, dropb, p);
    2026           0 :     if (c != 1) H = Fl_mul(H, c, p);
    2027             :   }
    2028     3965273 :   dp = dB ? umodiu(dB, p): 1;
    2029     3965273 :   if (dp != 1) H = Fl_mul(H, Fl_powu(Fl_inv(dp,p), degA, p), p);
    2030     3965281 :   return H;
    2031             : }
    2032             : 
    2033             : /* If B=NULL, assume B=A' */
    2034             : static GEN
    2035     1628271 : ZX_resultant_slice(GEN A, GEN B, GEN dB, GEN P, GEN *mod)
    2036             : {
    2037     1628271 :   pari_sp av = avma, av2;
    2038     1628271 :   long degA, degB, i, n = lg(P)-1;
    2039             :   GEN H, T;
    2040             : 
    2041     1628271 :   degA = degpol(A);
    2042     1628265 :   degB = B? degpol(B): degA - 1;
    2043     1628270 :   if (n == 1)
    2044             :   {
    2045      942823 :     ulong Hp, p = uel(P,1);
    2046      942823 :     GEN a = ZX_to_Flx(A, p), b = B? ZX_to_Flx(B, p): Flx_deriv(a, p);
    2047      942795 :     Hp = ZX_resultant_prime(a, b, dB, degA, degB, p);
    2048      942756 :     set_avma(av); *mod = utoipos(p); return utoi(Hp);
    2049             :   }
    2050      685447 :   T = ZV_producttree(P);
    2051      685452 :   A = ZX_nv_mod_tree(A, P, T);
    2052      685451 :   if (B) B = ZX_nv_mod_tree(B, P, T);
    2053      685451 :   H = cgetg(n+1, t_VECSMALL); av2 = avma;
    2054     3708056 :   for(i=1; i <= n; i++, set_avma(av2))
    2055             :   {
    2056     3022612 :     ulong p = P[i];
    2057     3022612 :     GEN a = gel(A,i), b = B? gel(B,i): Flx_deriv(a, p);
    2058     3023065 :     H[i] = ZX_resultant_prime(a, b, dB, degA, degB, p);
    2059             :   }
    2060      685444 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    2061      685451 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2062             : }
    2063             : 
    2064             : GEN
    2065     1628293 : ZX_resultant_worker(GEN P, GEN A, GEN B, GEN dB)
    2066             : {
    2067     1628293 :   GEN V = cgetg(3, t_VEC);
    2068     1628277 :   if (typ(B) == t_INT) B = NULL;
    2069     1628277 :   if (!signe(dB)) dB = NULL;
    2070     1628277 :   gel(V,1) = ZX_resultant_slice(A, B, dB, P, &gel(V,2));
    2071     1628292 :   return V;
    2072             : }
    2073             : 
    2074             : /* Compute Res(A, B/dB) in Z, assuming A,B in Z[X], dB in Z or NULL (= 1)
    2075             :  * If B=NULL, take B = A' and assume deg A > 1 and 'bound' is set */
    2076             : GEN
    2077     1297959 : ZX_resultant_all(GEN A, GEN B, GEN dB, ulong bound)
    2078             : {
    2079     1297959 :   pari_sp av = avma;
    2080             :   forprime_t S;
    2081             :   GEN  H, worker;
    2082     1297959 :   if (B)
    2083             :   {
    2084      108517 :     long a = degpol(A), b = degpol(B);
    2085      108517 :     if (a < 0 || b < 0) return gen_0;
    2086      108487 :     if (!a) return powiu(gel(A,2), b);
    2087      108487 :     if (!b) return powiu(gel(B,2), a);
    2088      106742 :     if (!bound) bound = ZX_ZXY_ResBound(A, B, dB);
    2089             :   }
    2090     1296186 :   worker = snm_closure(is_entry("_ZX_resultant_worker"),
    2091             :                        mkvec3(A, B? B: gen_0, dB? dB: gen_0));
    2092     1296238 :   init_modular_big(&S);
    2093     1296205 :   H = gen_crt("ZX_resultant_all", worker, &S, dB, bound, 0, NULL,
    2094             :               ZV_chinese_center, Fp_center);
    2095     1296218 :   return gerepileuptoint(av, H);
    2096             : }
    2097             : 
    2098             : /* A0 and B0 in Q[X] */
    2099             : GEN
    2100          56 : QX_resultant(GEN A0, GEN B0)
    2101             : {
    2102             :   GEN s, a, b, A, B;
    2103          56 :   pari_sp av = avma;
    2104             : 
    2105          56 :   A = Q_primitive_part(A0, &a);
    2106          56 :   B = Q_primitive_part(B0, &b);
    2107          56 :   s = ZX_resultant(A, B);
    2108          56 :   if (!signe(s)) { set_avma(av); return gen_0; }
    2109          56 :   if (a) s = gmul(s, gpowgs(a,degpol(B)));
    2110          56 :   if (b) s = gmul(s, gpowgs(b,degpol(A)));
    2111          56 :   return gerepileupto(av, s);
    2112             : }
    2113             : 
    2114             : GEN
    2115       25312 : ZX_resultant(GEN A, GEN B) { return ZX_resultant_all(A,B,NULL,0); }
    2116             : 
    2117             : GEN
    2118           0 : QXQ_intnorm(GEN A, GEN B)
    2119             : {
    2120             :   GEN c, n, R, lB;
    2121           0 :   long dA = degpol(A), dB = degpol(B);
    2122           0 :   pari_sp av = avma;
    2123           0 :   if (dA < 0) return gen_0;
    2124           0 :   A = Q_primitive_part(A, &c);
    2125           0 :   if (!c || typ(c) == t_INT) {
    2126           0 :     n = c;
    2127           0 :     R = ZX_resultant(B, A);
    2128             :   } else {
    2129           0 :     n = gel(c,1);
    2130           0 :     R = ZX_resultant_all(B, A, gel(c,2), 0);
    2131             :   }
    2132           0 :   if (n && !equali1(n)) R = mulii(R, powiu(n, dB));
    2133           0 :   lB = leading_coeff(B);
    2134           0 :   if (!equali1(lB)) R = diviiexact(R, powiu(lB, dA));
    2135           0 :   return gerepileuptoint(av, R);
    2136             : }
    2137             : 
    2138             : GEN
    2139       18858 : QXQ_norm(GEN A, GEN B)
    2140             : {
    2141             :   GEN c, R, lB;
    2142       18858 :   long dA = degpol(A), dB = degpol(B);
    2143       18858 :   pari_sp av = avma;
    2144       18858 :   if (dA < 0) return gen_0;
    2145       18858 :   A = Q_primitive_part(A, &c);
    2146       18858 :   R = ZX_resultant(B, A);
    2147       18858 :   if (c) R = gmul(R, gpowgs(c, dB));
    2148       18858 :   lB = leading_coeff(B);
    2149       18858 :   if (!equali1(lB)) R = gdiv(R, gpowgs(lB, dA));
    2150       18858 :   return gerepileupto(av, R);
    2151             : }
    2152             : 
    2153             : /* assume x has integral coefficients */
    2154             : GEN
    2155     1192575 : ZX_disc_all(GEN x, ulong bound)
    2156             : {
    2157     1192575 :   pari_sp av = avma;
    2158     1192575 :   long s, d = degpol(x);
    2159             :   GEN l, R;
    2160             : 
    2161     1192571 :   if (d <= 1) return d == 1? gen_1: gen_0;
    2162     1189488 :   s = (d & 2) ? -1: 1;
    2163     1189488 :   l = leading_coeff(x);
    2164     1189492 :   if (!bound) bound = ZX_discbound(x);
    2165     1189437 :   R = ZX_resultant_all(x, NULL, NULL, bound);
    2166     1189446 :   if (is_pm1(l))
    2167     1014155 :   { if (signe(l) < 0) s = -s; }
    2168             :   else
    2169      175285 :     R = diviiexact(R,l);
    2170     1189440 :   if (s == -1) togglesign_safe(&R);
    2171     1189442 :   return gerepileuptoint(av,R);
    2172             : }
    2173             : 
    2174             : GEN
    2175     1130860 : ZX_disc(GEN x) { return ZX_disc_all(x,0); }
    2176             : 
    2177             : static GEN
    2178        8586 : ZXQX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, GEN T, ulong p)
    2179             : {
    2180        8586 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    2181             :   GEN H, dp;
    2182        8586 :   if (dropa && dropb) return pol0_Flx(T[1]); /* p | lc(A), p | lc(B) */
    2183        8586 :   H = FlxqX_saferesultant(a, b, T, p);
    2184        8586 :   if (!H) return NULL;
    2185        8586 :   if (dropa)
    2186             :   { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    2187           0 :     GEN c = gel(b,degB+2); /* lc(B) */
    2188           0 :     if (odd(degB)) c = Flx_neg(c, p);
    2189           0 :     c = Flxq_powu(c, dropa, T, p);
    2190           0 :     if (!Flx_equal1(c)) H = Flxq_mul(H, c, T, p);
    2191             :   }
    2192        8586 :   else if (dropb)
    2193             :   { /* multiply by lc(A)^(deg B - deg b) */
    2194           0 :     GEN c = gel(a,degA+2); /* lc(A) */
    2195           0 :     c = Flxq_powu(c, dropb, T, p);
    2196           0 :     if (!Flx_equal1(c)) H = Flxq_mul(H, c, T, p);
    2197             :   }
    2198        8586 :   dp = dB ? ZX_to_Flx(dB, p): pol1_Flx(T[1]);
    2199        8586 :   if (!Flx_equal1(dp))
    2200             :   {
    2201           0 :     GEN idp = Flxq_invsafe(dp, T, p);
    2202           0 :     if (!idp) return NULL;
    2203           0 :     H = Flxq_mul(H, Flxq_powu(idp, degA, T, p), T, p);
    2204             :   }
    2205        8586 :   return H;
    2206             : }
    2207             : 
    2208             : /* If B=NULL, assume B=A' */
    2209             : static GEN
    2210        3842 : ZXQX_resultant_slice(GEN A, GEN B, GEN U, GEN dB, GEN P, GEN *mod)
    2211             : {
    2212        3842 :   pari_sp av = avma;
    2213        3842 :   long degA, degB, i, n = lg(P)-1;
    2214             :   GEN H, T;
    2215        3842 :   long v = varn(U), redo = 0;
    2216             : 
    2217        3842 :   degA = degpol(A);
    2218        3842 :   degB = B? degpol(B): degA - 1;
    2219        3842 :   if (n == 1)
    2220             :   {
    2221        2338 :     ulong p = uel(P,1);
    2222        2338 :     GEN a = ZXX_to_FlxX(A, p, v), b = B? ZXX_to_FlxX(B, p, v): FlxX_deriv(a, p);
    2223        2338 :     GEN u = ZX_to_Flx(U, p);
    2224        2338 :     GEN Hp = ZXQX_resultant_prime(a, b, dB, degA, degB, u, p);
    2225        2338 :     if (!Hp) { set_avma(av); *mod = gen_1; return pol_0(v); }
    2226        2338 :     Hp = gerepileupto(av, Flx_to_ZX(Hp)); *mod = utoipos(p); return Hp;
    2227             :   }
    2228        1504 :   T = ZV_producttree(P);
    2229        1504 :   A = ZXX_nv_mod_tree(A, P, T, v);
    2230        1504 :   if (B) B = ZXX_nv_mod_tree(B, P, T, v);
    2231        1504 :   U = ZX_nv_mod_tree(U, P, T);
    2232        1504 :   H = cgetg(n+1, t_VEC);
    2233        7752 :   for(i=1; i <= n; i++)
    2234             :   {
    2235        6248 :     ulong p = P[i];
    2236        6248 :     GEN a = gel(A,i), b = B? gel(B,i): FlxX_deriv(a, p), u = gel(U, i);
    2237        6248 :     GEN h = ZXQX_resultant_prime(a, b, dB, degA, degB, u, p);
    2238        6248 :     if (!h)
    2239             :     {
    2240           0 :       gel(H,i) = pol_0(v);
    2241           0 :       P[i] = 1; redo = 1;
    2242             :     }
    2243             :     else
    2244        6248 :       gel(H,i) = h;
    2245             :   }
    2246        1504 :   if (redo) T = ZV_producttree(P);
    2247        1504 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2248        1504 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2249             : }
    2250             : 
    2251             : GEN
    2252        3842 : ZXQX_resultant_worker(GEN P, GEN A, GEN B, GEN T, GEN dB)
    2253             : {
    2254        3842 :   GEN V = cgetg(3, t_VEC);
    2255        3842 :   if (isintzero(B)) B = NULL;
    2256        3842 :   if (!signe(dB)) dB = NULL;
    2257        3842 :   gel(V,1) = ZXQX_resultant_slice(A, B, T, dB, P, &gel(V,2));
    2258        3842 :   return V;
    2259             : }
    2260             : 
    2261             : static ulong
    2262        3377 : ZXQX_resultant_bound_i(GEN nf, GEN A, GEN B, GEN (*f)(GEN,GEN,long))
    2263             : {
    2264        3377 :   pari_sp av = avma;
    2265        3377 :   GEN r, M = nf_L2_bound(nf, NULL, &r);
    2266        3377 :   long v = nf_get_varn(nf), i, l = lg(r);
    2267        3377 :   GEN a = cgetg(l, t_COL);
    2268       10604 :   for (i = 1; i < l; i++)
    2269        7227 :     gel(a, i) = f(gsubst(A, v, gel(r,i)), gsubst(B, v, gel(r,i)), DEFAULTPREC);
    2270        3377 :   return gc_ulong(av, (ulong) dbllog2(gmul(M,RgC_fpnorml2(a, DEFAULTPREC))));
    2271             : }
    2272             : static ulong
    2273        3069 : ZXQX_resultant_bound(GEN nf, GEN A, GEN B)
    2274        3069 : { return ZXQX_resultant_bound_i(nf, A, B, &RgX_RgXY_ResBound); }
    2275             : 
    2276             : static GEN
    2277          56 : _ZXQ_powu(GEN x, ulong u, GEN T)
    2278          56 : { return typ(x) == t_INT? powiu(x, u): ZXQ_powu(x, u, T); }
    2279             : 
    2280             : /* Compute Res(A, B/dB) in Z[X]/T, assuming A,B in Z[X,Y], dB in Z or NULL (= 1)
    2281             :  * If B=NULL, take B = A' and assume deg A > 1 */
    2282             : static GEN
    2283        3066 : ZXQX_resultant_all(GEN A, GEN B, GEN T, GEN dB, ulong bound)
    2284             : {
    2285        3066 :   pari_sp av = avma;
    2286             :   forprime_t S;
    2287             :   GEN  H, worker;
    2288        3066 :   if (B)
    2289             :   {
    2290          49 :     long a = degpol(A), b = degpol(B);
    2291          49 :     if (a < 0 || b < 0) return gen_0;
    2292          49 :     if (!a) return _ZXQ_powu(gel(A,2), b, T);
    2293          49 :     if (!b) return _ZXQ_powu(gel(B,2), a, T);
    2294             :   } else
    2295        3017 :     if (!bound) B = RgX_deriv(A);
    2296        3066 :   if (!bound) bound = ZXQX_resultant_bound(nfinit(T, DEFAULTPREC), A, B);
    2297        3066 :   worker = snm_closure(is_entry("_ZXQX_resultant_worker"),
    2298             :                        mkvec4(A, B? B: gen_0, T, dB? dB: gen_0));
    2299        3066 :   init_modular_big(&S);
    2300        3066 :   H = gen_crt("ZXQX_resultant_all", worker, &S, dB, bound, 0, NULL,
    2301             :               nxV_chinese_center, FpX_center);
    2302        3066 :   if (DEBUGLEVEL)
    2303           0 :     err_printf("ZXQX_resultant_all: a priori bound: %lu, a posteriori: %lu\n",
    2304             :                bound, expi(gsupnorm(H, DEFAULTPREC)));
    2305        3066 :   return gerepileupto(av, H);
    2306             : }
    2307             : 
    2308             : GEN
    2309         105 : nfX_resultant(GEN nf, GEN x, GEN y)
    2310             : {
    2311         105 :   pari_sp av = avma;
    2312         105 :   GEN cx, cy, D, T = nf_get_pol(nf);
    2313         105 :   long dx = degpol(x), dy = degpol(y);
    2314         105 :   if (dx < 0 || dy < 0) return gen_0;
    2315         105 :   x = Q_primitive_part(x, &cx); if (cx) cx = gpowgs(cx, dy);
    2316         105 :   y = Q_primitive_part(y, &cy); if (cy) cy = gpowgs(cy, dx);
    2317         105 :   if (!dx)      D = _ZXQ_powu(gel(x,2), dy, T);
    2318         105 :   else if (!dy) D = _ZXQ_powu(gel(y,2), dx, T);
    2319             :   else
    2320             :   {
    2321          49 :     ulong bound = ZXQX_resultant_bound(nf, x, y);
    2322          49 :     D = ZXQX_resultant_all(x, y, T, NULL, bound);
    2323             :   }
    2324         105 :   cx = mul_content(cx, cy); if (cx) D = gmul(D, cx);
    2325         105 :   return gerepileupto(av, D);
    2326             : }
    2327             : 
    2328             : static GEN
    2329         217 : to_ZX(GEN a, long v) { return typ(a)==t_INT? scalarpol(a,v): a; }
    2330             : 
    2331             : static GEN
    2332        3017 : ZXQX_disc_all(GEN x, GEN T, ulong bound)
    2333             : {
    2334        3017 :   pari_sp av = avma;
    2335        3017 :   long s, d = degpol(x), v = varn(T);
    2336             :   GEN l, R;
    2337             : 
    2338        3017 :   if (d <= 1) return d == 1? pol_1(v): pol_0(v);
    2339        3017 :   s = (d & 2) ? -1: 1;
    2340        3017 :   l = leading_coeff(x);
    2341        3017 :   R = ZXQX_resultant_all(x, NULL, T, NULL, bound);
    2342        3017 :   if (!gequal1(l)) R = QXQ_div(R, to_ZX(l,v), T);
    2343        3017 :   if (s == -1) R = RgX_neg(R);
    2344        3017 :   return gerepileupto(av, R);
    2345             : }
    2346             : 
    2347             : GEN
    2348           7 : QX_disc(GEN x)
    2349             : {
    2350           7 :   pari_sp av = avma;
    2351           7 :   GEN c, d = ZX_disc( Q_primitive_part(x, &c) );
    2352           7 :   if (c) d = gmul(d, gpowgs(c, 2*degpol(x) - 2));
    2353           7 :   return gerepileupto(av, d);
    2354             : }
    2355             : 
    2356             : GEN
    2357        3178 : nfX_disc(GEN nf, GEN x)
    2358             : {
    2359        3178 :   pari_sp av = avma;
    2360        3178 :   GEN c, D, T = nf_get_pol(nf);
    2361             :   ulong bound;
    2362        3178 :   long d = degpol(x), v = varn(T);
    2363        3178 :   if (d <= 1) return d == 1? pol_1(v): pol_0(v);
    2364        3017 :   x = Q_primitive_part(x, &c);
    2365        3017 :   bound = ZXQX_resultant_bound(nf, x, RgX_deriv(x));
    2366        3017 :   D = ZXQX_disc_all(x, T, bound);
    2367        3017 :   if (c) D = gmul(D, gpowgs(c, 2*d - 2));
    2368        3017 :   return gerepileupto(av, D);
    2369             : }
    2370             : 
    2371             : GEN
    2372      825732 : QXQ_mul(GEN x, GEN y, GEN T)
    2373             : {
    2374      825732 :   GEN dx, nx = Q_primitive_part(x, &dx);
    2375      825729 :   GEN dy, ny = Q_primitive_part(y, &dy);
    2376      825728 :   GEN z = ZXQ_mul(nx, ny, T);
    2377      825731 :   if (dx || dy)
    2378             :   {
    2379      822673 :     GEN d = dx ? dy ? gmul(dx, dy): dx : dy;
    2380      822673 :     if (!gequal1(d)) z = ZX_Q_mul(z, d);
    2381             :   }
    2382      825732 :   return z;
    2383             : }
    2384             : 
    2385             : GEN
    2386      397859 : QXQ_sqr(GEN x, GEN T)
    2387             : {
    2388      397859 :   GEN dx, nx = Q_primitive_part(x, &dx);
    2389      397859 :   GEN z = ZXQ_sqr(nx, T);
    2390      397859 :   if (dx)
    2391      396032 :     z = ZX_Q_mul(z, gsqr(dx));
    2392      397859 :   return z;
    2393             : }
    2394             : 
    2395             : static GEN
    2396      209070 : QXQ_inv_slice(GEN A, GEN B, GEN P, GEN *mod)
    2397             : {
    2398      209070 :   pari_sp av = avma;
    2399      209070 :   long i, n = lg(P)-1, v = varn(A), redo = 0;
    2400             :   GEN H, T;
    2401      209070 :   if (n == 1)
    2402             :   {
    2403      163806 :     ulong p = uel(P,1);
    2404      163806 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    2405      163806 :     GEN U = Flxq_invsafe(a, b, p);
    2406      163806 :     if (!U)
    2407             :     {
    2408          24 :       set_avma(av);
    2409          24 :       *mod = gen_1; return pol_0(v);
    2410             :     }
    2411      163782 :     H = gerepilecopy(av, Flx_to_ZX(U));
    2412      163782 :     *mod = utoipos(p); return H;
    2413             :   }
    2414       45264 :   T = ZV_producttree(P);
    2415       45265 :   A = ZX_nv_mod_tree(A, P, T);
    2416       45265 :   B = ZX_nv_mod_tree(B, P, T);
    2417       45265 :   H = cgetg(n+1, t_VEC);
    2418      224984 :   for(i=1; i <= n; i++)
    2419             :   {
    2420      179719 :     ulong p = P[i];
    2421      179719 :     GEN a = gel(A,i), b = gel(B,i);
    2422      179719 :     GEN U = Flxq_invsafe(a, b, p);
    2423      179717 :     if (!U)
    2424             :     {
    2425         600 :       gel(H,i) = pol_0(v);
    2426         601 :       P[i] = 1; redo = 1;
    2427             :     }
    2428             :     else
    2429      179117 :       gel(H,i) = U;
    2430             :   }
    2431       45265 :   if (redo) T = ZV_producttree(P);
    2432       45265 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2433       45265 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2434             : }
    2435             : 
    2436             : GEN
    2437      209071 : QXQ_inv_worker(GEN P, GEN A, GEN B)
    2438             : {
    2439      209071 :   GEN V = cgetg(3, t_VEC);
    2440      209070 :   gel(V,1) = QXQ_inv_slice(A, B, P, &gel(V,2));
    2441      209071 :   return V;
    2442             : }
    2443             : 
    2444             : /* lift(1 / Mod(A,B)). B a ZX, A a scalar or a QX */
    2445             : GEN
    2446      144806 : QXQ_inv(GEN A, GEN B)
    2447             : {
    2448             :   GEN D, Ap, Bp;
    2449             :   ulong pp;
    2450      144806 :   pari_sp av2, av = avma;
    2451             :   forprime_t S;
    2452      144806 :   GEN worker, U, H = NULL, mod = gen_1;
    2453             :   pari_timer ti;
    2454             :   long k, dA, dB;
    2455      144806 :   if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
    2456             :   /* A a QX, B a ZX */
    2457      144806 :   A = Q_primitive_part(A, &D);
    2458      144806 :   dA = degpol(A); dB= degpol(B);
    2459             :   /* A, B in Z[X] */
    2460      144806 :   init_modular_small(&S);
    2461             :   do {
    2462      144806 :     pp = u_forprime_next(&S);
    2463      144806 :     Ap = ZX_to_Flx(A, pp);
    2464      144806 :     Bp = ZX_to_Flx(B, pp);
    2465      144806 :   } while (degpol(Ap) != dA || degpol(Bp) != dB);
    2466      144806 :   if (degpol(Flx_gcd(Ap, Bp, pp)) != 0 && degpol(ZX_gcd(A,B))!=0)
    2467          14 :     pari_err_INV("QXQ_inv",mkpolmod(A,B));
    2468      144792 :   worker = snm_closure(is_entry("_QXQ_inv_worker"), mkvec2(A, B));
    2469      144792 :   av2 = avma;
    2470      144792 :   for (k = 1; ;k *= 2)
    2471       41018 :   {
    2472             :     GEN res, b, N, den;
    2473      185810 :     gen_inccrt_i("QXQ_inv", worker, NULL, (k+1)>>1, 0, &S, &H, &mod,
    2474             :                  nxV_chinese_center, FpX_center);
    2475      185810 :     gerepileall(av2, 2, &H, &mod);
    2476      185810 :     b = sqrti(shifti(mod,-1));
    2477      185809 :     if (DEBUGLEVEL>5) timer_start(&ti);
    2478      185809 :     U = FpX_ratlift(H, mod, b, b, NULL);
    2479      185809 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: ratlift");
    2480      191379 :     if (!U) continue;
    2481      150361 :     N = Q_remove_denom(U, &den); if (!den) den = gen_1;
    2482      150360 :     res = Flx_rem(Flx_Fl_sub(Flx_mul(Ap, ZX_to_Flx(N,pp), pp),
    2483             :                   umodiu(den, pp), pp), Bp, pp);
    2484      150361 :     if (degpol(res) >= 0) continue;
    2485      144792 :     res = ZX_Z_sub(ZX_mul(A, N), den);
    2486      144792 :     res = ZX_is_monic(B) ? ZX_rem(res, B): RgX_pseudorem(res, B);
    2487      144792 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: final check");
    2488      144792 :     if (degpol(res)<0)
    2489             :     {
    2490      144792 :       if (D) U = RgX_Rg_div(U, D);
    2491      144792 :       return gerepilecopy(av, U);
    2492             :     }
    2493             :   }
    2494             : }
    2495             : 
    2496             : static GEN
    2497      117202 : QXQ_div_slice(GEN A, GEN B, GEN C, GEN P, GEN *mod)
    2498             : {
    2499      117202 :   pari_sp av = avma;
    2500      117202 :   long i, n = lg(P)-1, v = varn(A), redo = 0;
    2501             :   GEN H, T;
    2502      117202 :   if (n == 1)
    2503             :   {
    2504       42885 :     ulong p = uel(P,1);
    2505       42885 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p), c = ZX_to_Flx(C, p);
    2506       42885 :     GEN bi = Flxq_invsafe(b, c, p), U;
    2507       42885 :     if (!bi)
    2508             :     {
    2509           0 :       set_avma(av);
    2510           0 :       *mod = gen_1; return pol_0(v);
    2511             :     }
    2512       42885 :     U = Flxq_mul(a, bi, c, p);
    2513       42885 :     H = gerepilecopy(av, Flx_to_ZX(U));
    2514       42885 :     *mod = utoipos(p); return H;
    2515             :   }
    2516       74317 :   T = ZV_producttree(P);
    2517       74317 :   A = ZX_nv_mod_tree(A, P, T);
    2518       74317 :   B = ZX_nv_mod_tree(B, P, T);
    2519       74317 :   C = ZX_nv_mod_tree(C, P, T);
    2520       74317 :   H = cgetg(n+1, t_VEC);
    2521      327088 :   for(i=1; i <= n; i++)
    2522             :   {
    2523      252771 :     ulong p = P[i];
    2524      252771 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C, i);
    2525      252771 :     GEN bi = Flxq_invsafe(b, c, p);
    2526      252771 :     if (!bi)
    2527             :     {
    2528           0 :       gel(H,i) = pol_0(v);
    2529           0 :       P[i] = 1; redo = 1;
    2530             :     }
    2531             :     else
    2532      252771 :       gel(H,i) = Flxq_mul(a, bi, c, p);
    2533             :   }
    2534       74317 :   if (redo) T = ZV_producttree(P);
    2535       74317 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2536       74317 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2537             : }
    2538             : 
    2539             : GEN
    2540      117202 : QXQ_div_worker(GEN P, GEN A, GEN B, GEN C)
    2541             : {
    2542      117202 :   GEN V = cgetg(3, t_VEC);
    2543      117202 :   gel(V,1) = QXQ_div_slice(A, B, C, P, &gel(V,2));
    2544      117202 :   return V;
    2545             : }
    2546             : 
    2547             : /* lift(Mod(A/B, C)). C a ZX, A, B a scalar or a QX */
    2548             : GEN
    2549       31758 : QXQ_div(GEN A, GEN B, GEN C)
    2550             : {
    2551             :   GEN DA, DB, Ap, Bp, Cp;
    2552             :   ulong pp;
    2553       31758 :   pari_sp av2, av = avma;
    2554             :   forprime_t S;
    2555       31758 :   GEN worker, U, H = NULL, mod = gen_1;
    2556             :   pari_timer ti;
    2557             :   long k, dA, dB, dC;
    2558       31758 :   if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
    2559             :   /* A a QX, B a ZX */
    2560       31758 :   A = Q_primitive_part(A, &DA);
    2561       31758 :   B = Q_primitive_part(B, &DB);
    2562       31758 :   dA = degpol(A); dB = degpol(B); dC = degpol(C);
    2563             :   /* A, B in Z[X] */
    2564       31758 :   init_modular_small(&S);
    2565             :   do {
    2566       31758 :     pp = u_forprime_next(&S);
    2567       31758 :     Ap = ZX_to_Flx(A, pp);
    2568       31758 :     Bp = ZX_to_Flx(B, pp);
    2569       31758 :     Cp = ZX_to_Flx(C, pp);
    2570       31758 :   } while (degpol(Ap) != dA || degpol(Bp) != dB || degpol(Cp) != dC);
    2571       31758 :   if (degpol(Flx_gcd(Bp, Cp, pp)) != 0 && degpol(ZX_gcd(B,C))!=0)
    2572           0 :     pari_err_INV("QXQ_div",mkpolmod(B,C));
    2573       31758 :   worker = snm_closure(is_entry("_QXQ_div_worker"), mkvec3(A, B, C));
    2574       31758 :   av2 = avma;
    2575       31758 :   for (k = 1; ;k *= 2)
    2576       45440 :   {
    2577             :     GEN res, b, N, den;
    2578       77198 :     gen_inccrt_i("QXQ_div", worker, NULL, (k+1)>>1, 0, &S, &H, &mod,
    2579             :                  nxV_chinese_center, FpX_center);
    2580       77198 :     gerepileall(av2, 2, &H, &mod);
    2581       77198 :     b = sqrti(shifti(mod,-1));
    2582       77198 :     if (DEBUGLEVEL>5) timer_start(&ti);
    2583       77198 :     U = FpX_ratlift(H, mod, b, b, NULL);
    2584       77198 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_div: ratlift");
    2585       87615 :     if (!U) continue;
    2586       42175 :     N = Q_remove_denom(U, &den); if (!den) den = gen_1;
    2587       42175 :     res = Flx_rem(Flx_sub(Flx_mul(Bp, ZX_to_Flx(N,pp), pp),
    2588             :                           Flx_Fl_mul(Ap, umodiu(den, pp), pp), pp), Cp, pp);
    2589       42175 :     if (degpol(res) >= 0) continue;
    2590       31758 :     res = ZX_sub(ZX_mul(B, N), ZX_Z_mul(A,den));
    2591       31758 :     res = ZX_is_monic(C) ? ZX_rem(res, C): RgX_pseudorem(res, C);
    2592       31758 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_div: final check");
    2593       31758 :     if (degpol(res)<0)
    2594             :     {
    2595       31758 :       if (DA && DB) U = RgX_Rg_mul(U, gdiv(DA,DB));
    2596       26977 :       else if (DA) U = RgX_Rg_mul(U, DA);
    2597       15183 :       else if (DB) U = RgX_Rg_div(U, DB);
    2598       31758 :       return gerepilecopy(av, U);
    2599             :     }
    2600             :   }
    2601             : }
    2602             : 
    2603             : /************************************************************************
    2604             :  *                                                                      *
    2605             :  *                           ZXQ_minpoly                                *
    2606             :  *                                                                      *
    2607             :  ************************************************************************/
    2608             : 
    2609             : static GEN
    2610        3523 : ZXQ_minpoly_slice(GEN A, GEN B, long d, GEN P, GEN *mod)
    2611             : {
    2612        3523 :   pari_sp av = avma;
    2613        3523 :   long i, n = lg(P)-1, v = evalvarn(varn(B));
    2614             :   GEN H, T;
    2615        3523 :   if (n == 1)
    2616             :   {
    2617         716 :     ulong p = uel(P,1);
    2618         716 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    2619         716 :     GEN Hp = Flxq_minpoly(a, b, p);
    2620         716 :     if (degpol(Hp) != d) { p = 1; Hp = pol0_Flx(v); }
    2621         716 :     H = gerepileupto(av, Flx_to_ZX(Hp));
    2622         716 :     *mod = utoipos(p); return H;
    2623             :   }
    2624        2807 :   T = ZV_producttree(P);
    2625        2807 :   A = ZX_nv_mod_tree(A, P, T);
    2626        2807 :   B = ZX_nv_mod_tree(B, P, T);
    2627        2807 :   H = cgetg(n+1, t_VEC);
    2628       16838 :   for(i=1; i <= n; i++)
    2629             :   {
    2630       14031 :     ulong p = P[i];
    2631       14031 :     GEN a = gel(A,i), b = gel(B,i);
    2632       14031 :     GEN m = Flxq_minpoly(a, b, p);
    2633       14031 :     if (degpol(m) != d) { P[i] = 1; m = pol0_Flx(v); }
    2634       14031 :     gel(H, i) = m;
    2635             :   }
    2636        2807 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2637        2807 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2638             : }
    2639             : 
    2640             : GEN
    2641        3523 : ZXQ_minpoly_worker(GEN P, GEN A, GEN B, long d)
    2642             : {
    2643        3523 :   GEN V = cgetg(3, t_VEC);
    2644        3523 :   gel(V,1) = ZXQ_minpoly_slice(A, B, d, P, &gel(V,2));
    2645        3523 :   return V;
    2646             : }
    2647             : 
    2648             : GEN
    2649        1701 : ZXQ_minpoly(GEN A, GEN B, long d, ulong bound)
    2650             : {
    2651        1701 :   pari_sp av = avma;
    2652             :   GEN worker, H, dB;
    2653             :   forprime_t S;
    2654        1701 :   B = Q_remove_denom(B, &dB);
    2655        1701 :   worker = strtoclosure("_ZXQ_minpoly_worker", 3, A, B, stoi(d));
    2656        1701 :   init_modular_big(&S);
    2657        1701 :   H = gen_crt("ZXQ_minpoly", worker, &S, dB, bound, 0, NULL,
    2658             :                nxV_chinese_center, FpX_center_i);
    2659        1701 :   return gerepilecopy(av, H);
    2660             : }
    2661             : 
    2662             : /************************************************************************
    2663             :  *                                                                      *
    2664             :  *                   ZX_ZXY_resultant                                   *
    2665             :  *                                                                      *
    2666             :  ************************************************************************/
    2667             : 
    2668             : static GEN
    2669      173516 : ZX_ZXY_resultant_prime(GEN a, GEN b, ulong dp, ulong p,
    2670             :                        long degA, long degB, long dres, long sX)
    2671             : {
    2672      173516 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    2673      173516 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    2674      173516 :   GEN Hp = Flx_FlxY_resultant_polint(a, b, p, pi, dres, sX);
    2675      173515 :   if (dropa && dropb)
    2676           0 :     Hp = zero_Flx(sX);
    2677             :   else {
    2678      173515 :     if (dropa)
    2679             :     { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    2680           0 :       GEN c = gel(b,degB+2); /* lc(B) */
    2681           0 :       if (odd(degB)) c = Flx_neg(c, p);
    2682           0 :       if (!Flx_equal1(c)) {
    2683           0 :         c = Flx_powu_pre(c, dropa, p, pi);
    2684           0 :         if (!Flx_equal1(c)) Hp = Flx_mul_pre(Hp, c, p, pi);
    2685             :       }
    2686             :     }
    2687      173515 :     else if (dropb)
    2688             :     { /* multiply by lc(A)^(deg B - deg b) */
    2689           0 :       ulong c = uel(a, degA+2); /* lc(A) */
    2690           0 :       c = Fl_powu(c, dropb, p);
    2691           0 :       if (c != 1) Hp = Flx_Fl_mul_pre(Hp, c, p, pi);
    2692             :     }
    2693             :   }
    2694      173515 :   if (dp != 1) Hp = Flx_Fl_mul_pre(Hp, Fl_powu_pre(Fl_inv(dp,p), degA, p, pi), p, pi);
    2695      173515 :   return Hp;
    2696             : }
    2697             : 
    2698             : static GEN
    2699       69333 : ZX_ZXY_resultant_slice(GEN A, GEN B, GEN dB, long degA, long degB, long dres,
    2700             :                        GEN P, GEN *mod, long sX, long vY)
    2701             : {
    2702       69333 :   pari_sp av = avma;
    2703       69333 :   long i, n = lg(P)-1;
    2704             :   GEN H, T, D;
    2705       69333 :   if (n == 1)
    2706             :   {
    2707       40102 :     ulong p = uel(P,1);
    2708       40102 :     ulong dp = dB ? umodiu(dB, p): 1;
    2709       40102 :     GEN a = ZX_to_Flx(A, p), b = ZXX_to_FlxX(B, p, vY);
    2710       40102 :     GEN Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2711       40102 :     H = gerepileupto(av, Flx_to_ZX(Hp));
    2712       40102 :     *mod = utoipos(p); return H;
    2713             :   }
    2714       29231 :   T = ZV_producttree(P);
    2715       29231 :   A = ZX_nv_mod_tree(A, P, T);
    2716       29231 :   B = ZXX_nv_mod_tree(B, P, T, vY);
    2717       29231 :   D = dB ? Z_ZV_mod_tree(dB, P, T): NULL;
    2718       29231 :   H = cgetg(n+1, t_VEC);
    2719      117397 :   for(i=1; i <= n; i++)
    2720             :   {
    2721       88166 :     ulong p = P[i];
    2722       88166 :     GEN a = gel(A,i), b = gel(B,i);
    2723       88166 :     ulong dp = D ? uel(D, i): 1;
    2724       88166 :     gel(H,i) = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2725             :   }
    2726       29231 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2727       29231 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2728             : }
    2729             : 
    2730             : GEN
    2731       69333 : ZX_ZXY_resultant_worker(GEN P, GEN A, GEN B, GEN dB, GEN v)
    2732             : {
    2733       69333 :   GEN V = cgetg(3, t_VEC);
    2734       69333 :   if (isintzero(dB)) dB = NULL;
    2735       69333 :   gel(V,1) = ZX_ZXY_resultant_slice(A, B, dB, v[1], v[2], v[3], P, &gel(V,2), v[4], v[5]);
    2736       69333 :   return V;
    2737             : }
    2738             : 
    2739             : GEN
    2740       60041 : ZX_ZXY_resultant(GEN A, GEN B)
    2741             : {
    2742       60041 :   pari_sp av = avma;
    2743             :   forprime_t S;
    2744             :   ulong bound;
    2745       60041 :   long v = fetch_var_higher();
    2746       60041 :   long degA = degpol(A), degB, dres = degA * degpol(B);
    2747       60041 :   long vX = varn(B), vY = varn(A); /* assume vY has lower priority */
    2748       60041 :   long sX = evalvarn(vX);
    2749             :   GEN worker, H, dB;
    2750       60041 :   B = Q_remove_denom(B, &dB);
    2751       60041 :   if (!dB) B = leafcopy(B);
    2752       60041 :   A = leafcopy(A); setvarn(A,v);
    2753       60041 :   B = swap_vars(B, vY); setvarn(B,v); degB = degpol(B);
    2754       60041 :   bound = ZX_ZXY_ResBound(A, B, dB);
    2755       60041 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    2756      120082 :   worker = snm_closure(is_entry("_ZX_ZXY_resultant_worker"),
    2757       60041 :                        mkvec4(A, B, dB? dB: gen_0,
    2758             :                               mkvecsmall5(degA, degB, dres, sX, vY)));
    2759       60043 :   init_modular_big(&S);
    2760       60043 :   H = gen_crt("ZX_ZXY_resultant_all", worker, &S, dB, bound, 0, NULL,
    2761             :                nxV_chinese_center, FpX_center_i);
    2762       60042 :   setvarn(H, vX); (void)delete_var();
    2763       60042 :   return gerepilecopy(av, H);
    2764             : }
    2765             : 
    2766             : static long
    2767       40488 : ZX_ZXY_rnfequation_lambda(GEN A, GEN B0, long lambda)
    2768             : {
    2769       40488 :   pari_sp av = avma;
    2770       40488 :   long degA = degpol(A), degB, dres = degA*degpol(B0);
    2771       40488 :   long v = fetch_var_higher();
    2772       40488 :   long vX = varn(B0), vY = varn(A); /* assume vY has lower priority */
    2773       40488 :   long sX = evalvarn(vX);
    2774             :   GEN dB, B, a, b, Hp;
    2775             :   forprime_t S;
    2776             : 
    2777       40488 :   B0 = Q_remove_denom(B0, &dB);
    2778       40488 :   if (!dB) B0 = leafcopy(B0);
    2779       40488 :   A = leafcopy(A);
    2780       40488 :   B = B0;
    2781       40488 :   setvarn(A,v);
    2782       45248 : INIT:
    2783       45248 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    2784       45248 :   B = swap_vars(B, vY); setvarn(B,v);
    2785             :   /* B0(lambda v + x, v) */
    2786       45248 :   if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    2787             : 
    2788       45248 :   degB = degpol(B);
    2789       45248 :   init_modular_big(&S);
    2790             :   while (1)
    2791           0 :   {
    2792       45248 :     ulong p = u_forprime_next(&S);
    2793       45248 :     ulong dp = dB ? umodiu(dB, p): 1;
    2794       45248 :     if (!dp) continue;
    2795       45248 :     a = ZX_to_Flx(A, p);
    2796       45248 :     b = ZXX_to_FlxX(B, p, v);
    2797       45248 :     Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2798       45247 :     if (degpol(Hp) != dres) continue;
    2799       45247 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    2800       45247 :     if (!Flx_is_squarefree(Hp, p)) { lambda = next_lambda(lambda); goto INIT; }
    2801       40486 :     if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    2802       40486 :     (void)delete_var(); return gc_long(av,lambda);
    2803             :   }
    2804             : }
    2805             : 
    2806             : GEN
    2807       41409 : ZX_ZXY_rnfequation(GEN A, GEN B, long *lambda)
    2808             : {
    2809       41409 :   if (lambda)
    2810             :   {
    2811       40488 :     *lambda = ZX_ZXY_rnfequation_lambda(A, B, *lambda);
    2812       40486 :     if (*lambda) B = RgX_translate(B, monomial(stoi(*lambda), 1, varn(A)));
    2813             :   }
    2814       41407 :   return ZX_ZXY_resultant(A,B);
    2815             : }
    2816             : 
    2817             : static GEN
    2818       10370 : ZX_composedsum_slice(GEN A, GEN B, GEN P, GEN *mod)
    2819             : {
    2820       10370 :   pari_sp av = avma;
    2821       10370 :   long i, n = lg(P)-1;
    2822             :   GEN H, T;
    2823       10370 :   if (n == 1)
    2824             :   {
    2825        9868 :     ulong p = uel(P,1);
    2826        9868 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    2827        9861 :     GEN Hp = Flx_composedsum(a, b, p);
    2828        9863 :     H = gerepileupto(av, Flx_to_ZX(Hp));
    2829        9867 :     *mod = utoipos(p); return H;
    2830             :   }
    2831         502 :   T = ZV_producttree(P);
    2832         502 :   A = ZX_nv_mod_tree(A, P, T);
    2833         502 :   B = ZX_nv_mod_tree(B, P, T);
    2834         502 :   H = cgetg(n+1, t_VEC);
    2835        4526 :   for(i=1; i <= n; i++)
    2836             :   {
    2837        4024 :     ulong p = P[i];
    2838        4024 :     GEN a = gel(A,i), b = gel(B,i);
    2839        4024 :     gel(H,i) = Flx_composedsum(a, b, p);
    2840             :   }
    2841         502 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2842         502 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2843             : }
    2844             : 
    2845             : GEN
    2846       10370 : ZX_composedsum_worker(GEN P, GEN A, GEN B)
    2847             : {
    2848       10370 :   GEN V = cgetg(3, t_VEC);
    2849       10371 :   gel(V,1) = ZX_composedsum_slice(A, B, P, &gel(V,2));
    2850       10368 :   return V;
    2851             : }
    2852             : 
    2853             : static GEN
    2854       10106 : ZX_composedsum_i(GEN A, GEN B, GEN lead)
    2855             : {
    2856       10106 :   pari_sp av = avma;
    2857             :   forprime_t S;
    2858             :   ulong bound;
    2859             :   GEN H, worker, mod;
    2860       10106 :   if (degpol(A) < degpol(B)) swap(A, B);
    2861       10106 :   if (!lead) lead  = mulii(leading_coeff(A),leading_coeff(B));
    2862       10106 :   bound = ZX_ZXY_ResBound_1(A, B);
    2863       10102 :   worker = snm_closure(is_entry("_ZX_composedsum_worker"), mkvec2(A,B));
    2864       10108 :   init_modular_big(&S);
    2865       10103 :   H = gen_crt("ZX_composedsum", worker, &S, lead, bound, 0, &mod,
    2866             :               nxV_chinese_center, FpX_center);
    2867       10105 :   return gerepileupto(av, H);
    2868             : }
    2869             : 
    2870             : static long
    2871        9715 : ZX_compositum_lambda(GEN A, GEN B, GEN lead, long lambda)
    2872             : {
    2873        9715 :   pari_sp av = avma;
    2874             :   forprime_t S;
    2875             :   ulong p;
    2876        9715 :   init_modular_big(&S);
    2877        9719 :   p = u_forprime_next(&S);
    2878             :   while (1)
    2879         112 :   {
    2880             :     GEN Hp, a;
    2881        9831 :     if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    2882        9831 :     if (lead && dvdiu(lead,p)) { p = u_forprime_next(&S); continue; }
    2883        9821 :     a = ZX_to_Flx(ZX_rescale(A, stoi(-lambda)), p);
    2884        9817 :     Hp = Flx_composedsum(a, ZX_to_Flx(B, p), p);
    2885        9823 :     if (!Flx_is_squarefree(Hp, p)) { lambda = next_lambda(lambda); continue; }
    2886        9711 :     if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    2887        9711 :     return gc_long(av, lambda);
    2888             :   }
    2889             : }
    2890             : 
    2891             : GEN
    2892        9714 : ZX_compositum(GEN A, GEN B, long *lambda)
    2893             : {
    2894        9714 :   GEN lead  = mulii(leading_coeff(A),leading_coeff(B));
    2895        9718 :   if (lambda)
    2896             :   {
    2897        9718 :     *lambda = ZX_compositum_lambda(A, B, lead, *lambda);
    2898        9711 :     A = ZX_rescale(A, stoi(-*lambda));
    2899             :   }
    2900        9721 :   return ZX_composedsum_i(A, B, lead);
    2901             : }
    2902             : 
    2903             : GEN
    2904         385 : ZX_composedsum(GEN A, GEN B)
    2905         385 : { return ZX_composedsum_i(A, B, NULL); }
    2906             : 
    2907             : static GEN
    2908         352 : ZXQX_composedsum_slice(GEN A, GEN B, GEN C, GEN P, GEN *mod)
    2909             : {
    2910         352 :   pari_sp av = avma;
    2911         352 :   long i, n = lg(P)-1, dC = degpol(C), v = varn(C);
    2912             :   GEN H, T;
    2913         352 :   if (n == 1)
    2914             :   {
    2915         174 :     ulong p = uel(P,1);
    2916         174 :     GEN a = ZXX_to_FlxX(A, p, v), b = ZXX_to_FlxX(B, p, v);
    2917         174 :     GEN c = ZX_to_Flx(C, p);
    2918         174 :     GEN Hp = FlxX_to_Flm(FlxqX_composedsum(a, b, c, p), dC);
    2919         174 :     H = gerepileupto(av, Flm_to_ZM(Hp));
    2920         174 :     *mod = utoipos(p); return H;
    2921             :   }
    2922         178 :   T = ZV_producttree(P);
    2923         178 :   A = ZXX_nv_mod_tree(A, P, T, v);
    2924         178 :   B = ZXX_nv_mod_tree(B, P, T, v);
    2925         178 :   C = ZX_nv_mod_tree(C, P, T);
    2926         178 :   H = cgetg(n+1, t_VEC);
    2927         660 :   for(i=1; i <= n; i++)
    2928             :   {
    2929         482 :     ulong p = P[i];
    2930         482 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C,i);
    2931         482 :     gel(H,i) = FlxX_to_Flm(FlxqX_composedsum(a, b, c, p), dC);
    2932             :   }
    2933         178 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
    2934         178 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2935             : }
    2936             : 
    2937             : GEN
    2938         352 : ZXQX_composedsum_worker(GEN P, GEN A, GEN B, GEN C)
    2939             : {
    2940         352 :   GEN V = cgetg(3, t_VEC);
    2941         352 :   gel(V,1) = ZXQX_composedsum_slice(A, B, C, P, &gel(V,2));
    2942         352 :   return V;
    2943             : }
    2944             : 
    2945             : static GEN
    2946         308 : ZXQX_composedsum(GEN A, GEN B, GEN T, ulong bound)
    2947             : {
    2948         308 :   pari_sp av = avma;
    2949             :   forprime_t S;
    2950             :   GEN H, worker, mod;
    2951         308 :   GEN lead = mulii(Q_content(leading_coeff(A)), Q_content(leading_coeff(B)));
    2952         308 :   worker = snm_closure(is_entry("_ZXQX_composedsum_worker")
    2953             :                       , mkvec3(A,B,T));
    2954         308 :   init_modular_big(&S);
    2955         308 :   H = gen_crt("ZXQX_composedsum", worker, &S, lead, bound, 0, &mod,
    2956             :               nmV_chinese_center, FpM_center);
    2957         308 :   if (DEBUGLEVEL > 4)
    2958           0 :     err_printf("nfcompositum: a priori bound: %lu, a posteriori: %lu\n",
    2959             :                bound, expi(gsupnorm(H, DEFAULTPREC)));
    2960         308 :   return gerepilecopy(av, RgM_to_RgXX(H, varn(A), varn(T)));
    2961             : }
    2962             : 
    2963             : static long
    2964         308 : ZXQX_composedsum_bound(GEN nf, GEN A, GEN B)
    2965         308 : { return ZXQX_resultant_bound_i(nf, A, B, &RgX_RgXY_ResBound_1); }
    2966             : 
    2967             : GEN
    2968         308 : nf_direct_compositum(GEN nf, GEN A, GEN B)
    2969             : {
    2970         308 :   ulong bnd = ZXQX_composedsum_bound(nf, A, B);
    2971         308 :   return ZXQX_composedsum(A, B, nf_get_pol(nf), bnd);
    2972             : }
    2973             : 
    2974             : /************************************************************************
    2975             :  *                                                                      *
    2976             :  *                   IRREDUCIBLE POLYNOMIAL / Fp                        *
    2977             :  *                                                                      *
    2978             :  ************************************************************************/
    2979             : 
    2980             : /* irreducible (unitary) polynomial of degree n over Fp */
    2981             : GEN
    2982           0 : ffinit_rand(GEN p,long n)
    2983             : {
    2984           0 :   for(;;) {
    2985           0 :     pari_sp av = avma;
    2986           0 :     GEN pol = ZX_add(pol_xn(n, 0), random_FpX(n-1,0, p));
    2987           0 :     if (FpX_is_irred(pol, p)) return pol;
    2988           0 :     set_avma(av);
    2989             :   }
    2990             : }
    2991             : 
    2992             : /* return an extension of degree 2^l of F_2, assume l > 0
    2993             :  * Not stack clean. */
    2994             : static GEN
    2995         621 : ffinit_Artin_Schreier_2(long l)
    2996             : {
    2997             :   GEN Q, T, S;
    2998             :   long i, v;
    2999             : 
    3000         621 :   if (l == 1) return mkvecsmall4(0,1,1,1); /*x^2 + x + 1*/
    3001         572 :   v = fetch_var_higher();
    3002         572 :   S = mkvecsmall5(0, 0, 0, 1, 1); /* y(y^2 + y) */
    3003         572 :   Q = mkpoln(3, pol1_Flx(0), pol1_Flx(0), S); /* x^2 + x + y(y^2+y) */
    3004         572 :   setvarn(Q, v);
    3005             : 
    3006             :   /* x^4+x+1, irred over F_2, minimal polynomial of a root of Q */
    3007         572 :   T = mkvecsmalln(6,evalvarn(v),1UL,1UL,0UL,0UL,1UL);
    3008             :   /* Q = x^2 + x + a(y) irred. over K = F2[y] / (T(y))
    3009             :    * ==> x^2 + x + a(y) b irred. over K for any root b of Q
    3010             :    * ==> x^2 + x + (b^2+b)b */
    3011        3166 :   for (i=2; i<l; i++) T = Flx_FlxY_resultant(T, Q, 2); /* minpoly(b) / F2*/
    3012         572 :   (void)delete_var(); T[1] = 0; return T;
    3013             : }
    3014             : 
    3015             : /* return an extension of degree p^l of F_p, assume l > 0
    3016             :  * Not stack clean. */
    3017             : GEN
    3018         978 : ffinit_Artin_Schreier(ulong p, long l)
    3019             : {
    3020             :   long i, v;
    3021             :   GEN Q, R, S, T, xp;
    3022         978 :   if (p==2) return ffinit_Artin_Schreier_2(l);
    3023         357 :   xp = polxn_Flx(p,0); /* x^p */
    3024         357 :   T = Flx_sub(xp, mkvecsmall3(0,1,1),p); /* x^p - x - 1 */
    3025         357 :   if (l == 1) return T;
    3026             : 
    3027           7 :   v = evalvarn(fetch_var_higher());
    3028           7 :   xp[1] = v;
    3029           7 :   R = Flx_sub(polxn_Flx(2*p-1,0), polxn_Flx(p,0),p);
    3030           7 :   S = Flx_sub(xp, polx_Flx(0), p);
    3031           7 :   Q = FlxX_Flx_sub(Flx_to_FlxX(S, v), R, p); /* x^p - x - (y^(2p-1)-y^p) */
    3032          14 :   for (i = 2; i <= l; ++i) T = Flx_FlxY_resultant(T, Q, p);
    3033           7 :   (void)delete_var(); T[1] = 0; return T;
    3034             : }
    3035             : 
    3036             : static long
    3037      147933 : flinit_check(ulong p, long n, long l)
    3038             : {
    3039             :   ulong q;
    3040      147933 :   if (!uisprime(n)) return 0;
    3041      101208 :   q = p % n; if (!q) return 0;
    3042       98730 :   return ugcd((n-1)/Fl_order(q, n-1, n), l) == 1;
    3043             : }
    3044             : 
    3045             : static GEN
    3046       31727 : flinit(ulong p, long l)
    3047             : {
    3048       31727 :   ulong n = 1+l;
    3049       95809 :   while (!flinit_check(p,n,l)) n += l;
    3050       31727 :   if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
    3051       31727 :   return ZX_to_Flx(polsubcyclo(n,l,0), p);
    3052             : }
    3053             : 
    3054             : static GEN
    3055       28879 : ffinit_fact_Flx(ulong p, long n)
    3056             : {
    3057       28879 :   GEN P, F = factoru_pow(n), Fp = gel(F,1), Fe = gel(F,2), Fm = gel(F,3);
    3058       28879 :   long i, l = lg(Fm);
    3059       28879 :   P = cgetg(l, t_VEC);
    3060       61584 :   for (i = 1; i < l; ++i)
    3061       32705 :     gel(P,i) = p==uel(Fp,i) ?
    3062         978 :                  ffinit_Artin_Schreier(uel(Fp,i), Fe[i])
    3063       32705 :                : flinit(p, uel(Fm,i));
    3064       28879 :   return FlxV_composedsum(P, p);
    3065             : }
    3066             : 
    3067             : static GEN
    3068       52124 : init_Flxq_i(ulong p, long n, long sv)
    3069             : {
    3070             :   GEN P;
    3071       52124 :   if (n == 1) return polx_Flx(sv);
    3072       52124 :   if (flinit_check(p, n+1, n))
    3073             :   {
    3074       23245 :     P = const_vecsmall(n+2,1);
    3075       23245 :     P[1] = sv; return P;
    3076             :   }
    3077       28879 :   P = ffinit_fact_Flx(p,n);
    3078       28879 :   P[1] = sv; return P;
    3079             : }
    3080             : 
    3081             : GEN
    3082           0 : init_Flxq(ulong p, long n, long v)
    3083             : {
    3084           0 :   pari_sp av = avma;
    3085           0 :   return gerepileupto(av, init_Flxq_i(p, n, v));
    3086             : }
    3087             : 
    3088             : /* check if polsubcyclo(n,l,0) is irreducible modulo p */
    3089             : static long
    3090        7185 : fpinit_check(GEN p, long n, long l)
    3091             : {
    3092             :   ulong q;
    3093        7185 :   if (!uisprime(n)) return 0;
    3094        4450 :   q = umodiu(p,n); if (!q) return 0;
    3095        4450 :   return ugcd((n-1)/Fl_order(q, n-1, n), l) == 1;
    3096             : }
    3097             : 
    3098             : /* let k=2 if p%4==1, and k=4 else and assume k*p does not divide l.
    3099             :  * Return an irreducible polynomial of degree l over F_p.
    3100             :  * Variant of Adleman and Lenstra "Finding irreducible polynomials over
    3101             :  * finite fields", ACM, 1986 (5) 350--355.
    3102             :  * Not stack clean */
    3103             : static GEN
    3104        1653 : fpinit(GEN p, long l)
    3105             : {
    3106        1653 :   ulong n = 1+l;
    3107        5202 :   while (!fpinit_check(p,n,l)) n += l;
    3108        1653 :   if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
    3109        1653 :   return FpX_red(polsubcyclo(n,l,0),p);
    3110             : }
    3111             : 
    3112             : static GEN
    3113        1574 : ffinit_fact(GEN p, long n)
    3114             : {
    3115        1574 :   GEN P, F = factoru_pow(n), Fp = gel(F,1), Fe = gel(F,2), Fm = gel(F,3);
    3116        1574 :   long i, l = lg(Fm);
    3117        1574 :   P = cgetg(l, t_VEC);
    3118        3227 :   for (i = 1; i < l; ++i)
    3119        3306 :     gel(P,i) = absequaliu(p, Fp[i]) ?
    3120           0 :                  Flx_to_ZX(ffinit_Artin_Schreier(Fp[i], Fe[i]))
    3121        1653 :                : fpinit(p, Fm[i]);
    3122        1574 :   return FpXV_composedsum(P, p);
    3123             : }
    3124             : 
    3125             : static GEN
    3126       54366 : init_Fq_i(GEN p, long n, long v)
    3127             : {
    3128             :   GEN P;
    3129       54366 :   if (n <= 0) pari_err_DOMAIN("ffinit", "degree", "<=", gen_0, stoi(n));
    3130       54366 :   if (typ(p) != t_INT) pari_err_TYPE("ffinit",p);
    3131       54366 :   if (cmpiu(p, 2) < 0) pari_err_PRIME("ffinit",p);
    3132       54359 :   if (v < 0) v = 0;
    3133       54359 :   if (n == 1) return pol_x(v);
    3134       54107 :   if (lgefint(p) == 3)
    3135       52124 :     return Flx_to_ZX(init_Flxq_i(p[2], n, evalvarn(v)));
    3136        1983 :   if (fpinit_check(p, n+1, n)) return polcyclo(n+1, v);
    3137        1574 :   P = ffinit_fact(p,n);
    3138        1574 :   setvarn(P, v); return P;
    3139             : }
    3140             : GEN
    3141       53813 : init_Fq(GEN p, long n, long v)
    3142             : {
    3143       53813 :   pari_sp av = avma;
    3144       53813 :   return gerepileupto(av, init_Fq_i(p, n, v));
    3145             : }
    3146             : GEN
    3147         553 : ffinit(GEN p, long n, long v)
    3148             : {
    3149         553 :   pari_sp av = avma;
    3150         553 :   return gerepileupto(av, FpX_to_mod(init_Fq_i(p, n, v), p));
    3151             : }
    3152             : 
    3153             : GEN
    3154        3178 : ffnbirred(GEN p, long n)
    3155             : {
    3156        3178 :   pari_sp av = avma;
    3157        3178 :   GEN s = powiu(p,n), F = factoru(n), D = divisorsu_moebius(gel(F, 1));
    3158        3178 :   long j, l = lg(D);
    3159        6797 :   for (j = 2; j < l; j++) /* skip d = 1 */
    3160             :   {
    3161        3619 :     long md = D[j]; /* mu(d) * d, d squarefree */
    3162        3619 :     GEN pd = powiu(p, n / labs(md)); /* p^{n/d} */
    3163        3619 :     s = md > 0? addii(s, pd): subii(s,pd);
    3164             :   }
    3165        3178 :   return gerepileuptoint(av, diviuexact(s, n));
    3166             : }
    3167             : 
    3168             : GEN
    3169         616 : ffsumnbirred(GEN p, long n)
    3170             : {
    3171         616 :   pari_sp av = avma, av2;
    3172         616 :   GEN q, t = p, v = vecfactoru_i(1, n);
    3173             :   long i;
    3174         616 :   q = cgetg(n+1,t_VEC); gel(q,1) = p;
    3175        1764 :   for (i=2; i<=n; i++) gel(q,i) = mulii(gel(q,i-1), p);
    3176         616 :   av2 = avma;
    3177        1764 :   for (i=2; i<=n; i++)
    3178             :   {
    3179        1148 :     GEN s = gel(q,i), F = gel(v,i), D = divisorsu_moebius(gel(F,1));
    3180        1148 :     long j, l = lg(D);
    3181        2534 :     for (j = 2; j < l; j++) /* skip 1 */
    3182             :     {
    3183        1386 :       long md = D[j];
    3184        1386 :       GEN pd = gel(q, i / labs(md)); /* p^{i/d} */
    3185        1386 :       s = md > 0? addii(s, pd): subii(s, pd);
    3186             :     }
    3187        1148 :     t = gerepileuptoint(av2, addii(t, diviuexact(s, i)));
    3188             :   }
    3189         616 :   return gerepileuptoint(av, t);
    3190             : }
    3191             : 
    3192             : GEN
    3193         140 : ffnbirred0(GEN p, long n, long flag)
    3194             : {
    3195         140 :   if (typ(p) != t_INT) pari_err_TYPE("ffnbirred", p);
    3196         140 :   if (n <= 0) pari_err_DOMAIN("ffnbirred", "degree", "<=", gen_0, stoi(n));
    3197         140 :   switch(flag)
    3198             :   {
    3199          70 :     case 0: return ffnbirred(p, n);
    3200          70 :     case 1: return ffsumnbirred(p, n);
    3201             :   }
    3202           0 :   pari_err_FLAG("ffnbirred");
    3203             :   return NULL; /* LCOV_EXCL_LINE */
    3204             : }
    3205             : 
    3206             : static void
    3207        2261 : checkmap(GEN m, const char *s)
    3208             : {
    3209        2261 :   if (typ(m)!=t_VEC || lg(m)!=3 || typ(gel(m,1))!=t_FFELT)
    3210           0 :     pari_err_TYPE(s,m);
    3211        2261 : }
    3212             : 
    3213             : GEN
    3214         189 : ffembed(GEN a, GEN b)
    3215             : {
    3216         189 :   pari_sp av = avma;
    3217         189 :   GEN p, Ta, Tb, g, r = NULL;
    3218         189 :   if (typ(a)!=t_FFELT) pari_err_TYPE("ffembed",a);
    3219         189 :   if (typ(b)!=t_FFELT) pari_err_TYPE("ffembed",b);
    3220         189 :   p = FF_p_i(a); g = FF_gen(a);
    3221         189 :   if (!equalii(p, FF_p_i(b))) pari_err_MODULUS("ffembed",a,b);
    3222         189 :   Ta = FF_mod(a);
    3223         189 :   Tb = FF_mod(b);
    3224         189 :   if (degpol(Tb)%degpol(Ta)!=0)
    3225           7 :     pari_err_DOMAIN("ffembed",GENtostr_raw(a),"is not a subfield of",b,a);
    3226         182 :   r = gel(FFX_roots(Ta, b), 1);
    3227         182 :   return gerepilecopy(av, mkvec2(g,r));
    3228             : }
    3229             : 
    3230             : GEN
    3231          91 : ffextend(GEN a, GEN P, long v)
    3232             : {
    3233          91 :   pari_sp av = avma;
    3234             :   long n;
    3235             :   GEN p, T, R, g, m;
    3236          91 :   if (typ(a)!=t_FFELT) pari_err_TYPE("ffextend",a);
    3237          91 :   T = a; p = FF_p_i(a);
    3238          91 :   if (typ(P)!=t_POL || !RgX_is_FpXQX(P,&T,&p)) pari_err_TYPE("ffextend", P);
    3239          49 :   if (!FF_samefield(a, T)) pari_err_MODULUS("ffextend",a,T);
    3240          49 :   if (v < 0) v = varn(P);
    3241          49 :   n = FF_f(T) * degpol(P); R = ffinit(p, n, v); g = ffgen(R, v);
    3242          49 :   m = ffembed(a, g);
    3243          49 :   R = FFX_roots(ffmap(m, P),g);
    3244          49 :   return gerepilecopy(av, mkvec2(gel(R,1), m));
    3245             : }
    3246             : 
    3247             : GEN
    3248          42 : fffrobenius(GEN a, long n)
    3249             : {
    3250          42 :   if (typ(a)!=t_FFELT) pari_err_TYPE("fffrobenius",a);
    3251          42 :   retmkvec2(FF_gen(a), FF_Frobenius(a, n));
    3252             : }
    3253             : 
    3254             : GEN
    3255         133 : ffinvmap(GEN m)
    3256             : {
    3257         133 :   pari_sp av = avma;
    3258             :   long i, l;
    3259         133 :   GEN T, F, a, g, r, f = NULL;
    3260         133 :   checkmap(m, "ffinvmap");
    3261         133 :   a = gel(m,1); r = gel(m,2);
    3262         133 :   if (typ(r) != t_FFELT)
    3263           7 :    pari_err_TYPE("ffinvmap", m);
    3264         126 :   g = FF_gen(a);
    3265         126 :   T = FF_mod(r);
    3266         126 :   F = gel(FFX_factor(T, a), 1);
    3267         126 :   l = lg(F);
    3268         490 :   for(i=1; i<l; i++)
    3269             :   {
    3270         490 :     GEN s = FFX_rem(FF_to_FpXQ_i(r), gel(F, i), a);
    3271         490 :     if (degpol(s)==0 && gequal(constant_coeff(s),g)) { f = gel(F, i); break; }
    3272             :   }
    3273         126 :   if (f==NULL) pari_err_TYPE("ffinvmap", m);
    3274         126 :   if (degpol(f)==1) f = FF_neg_i(gel(f,2));
    3275         126 :   return gerepilecopy(av, mkvec2(FF_gen(r),f));
    3276             : }
    3277             : 
    3278             : static GEN
    3279        1260 : ffpartmapimage(const char *s, GEN r)
    3280             : {
    3281        1260 :    GEN a = NULL, p = NULL;
    3282        1260 :    if (typ(r)==t_POL && degpol(r) >= 1
    3283        1260 :       && RgX_is_FpXQX(r,&a,&p) && a && typ(a)==t_FFELT) return a;
    3284           0 :    pari_err_TYPE(s, r);
    3285             :    return NULL; /* LCOV_EXCL_LINE */
    3286             : }
    3287             : 
    3288             : static GEN
    3289        2709 : ffeltmap_i(GEN m, GEN x)
    3290             : {
    3291        2709 :    GEN r = gel(m,2);
    3292        2709 :    if (!FF_samefield(x, gel(m,1)))
    3293          84 :      pari_err_DOMAIN("ffmap","m","domain does not contain", x, r);
    3294        2625 :    if (typ(r)==t_FFELT)
    3295        1659 :      return FF_map(r, x);
    3296             :    else
    3297         966 :      return FFX_preimage(x, r, ffpartmapimage("ffmap", r));
    3298             : }
    3299             : 
    3300             : static GEN
    3301        4459 : ffmap_i(GEN m, GEN x)
    3302             : {
    3303             :   GEN y;
    3304        4459 :   long i, lx, tx = typ(x);
    3305        4459 :   switch(tx)
    3306             :   {
    3307        2541 :     case t_FFELT:
    3308        2541 :       return ffeltmap_i(m, x);
    3309        1267 :     case t_POL: case t_RFRAC: case t_SER:
    3310             :     case t_VEC: case t_COL: case t_MAT:
    3311        1267 :       y = cgetg_copy(x, &lx);
    3312        1988 :       for (i=1; i<lontyp[tx]; i++) y[i] = x[1];
    3313        4564 :       for (i=lontyp[tx]; i<lx; i++)
    3314             :       {
    3315        3339 :         GEN yi = ffmap_i(m, gel(x,i));
    3316        3297 :         if (!yi) return NULL;
    3317        3297 :         gel(y,i) = yi;
    3318             :       }
    3319        1225 :       return y;
    3320             :   }
    3321         651 :   return gcopy(x);
    3322             : }
    3323             : 
    3324             : GEN
    3325        1036 : ffmap(GEN m, GEN x)
    3326             : {
    3327        1036 :   pari_sp ltop = avma;
    3328             :   GEN y;
    3329        1036 :   checkmap(m, "ffmap");
    3330        1036 :   y = ffmap_i(m, x);
    3331        1036 :   if (y) return y;
    3332          42 :   set_avma(ltop); return cgetg(1,t_VEC);
    3333             : }
    3334             : 
    3335             : static GEN
    3336         252 : ffeltmaprel_i(GEN m, GEN x)
    3337             : {
    3338         252 :    GEN g = gel(m,1), r = gel(m,2);
    3339         252 :    if (!FF_samefield(x, g))
    3340           0 :      pari_err_DOMAIN("ffmap","m","domain does not contain", x, r);
    3341         252 :    if (typ(r)==t_FFELT)
    3342          84 :      retmkpolmod(FF_map(r, x), pol_x(FF_var(g)));
    3343             :    else
    3344         168 :      retmkpolmod(FFX_preimagerel(x, r, ffpartmapimage("ffmap", r)), gcopy(r));
    3345             : }
    3346             : 
    3347             : static GEN
    3348         252 : ffmaprel_i(GEN m, GEN x)
    3349             : {
    3350             :   GEN y;
    3351         252 :   long i, lx, tx = typ(x);
    3352         252 :   switch(tx)
    3353             :   {
    3354         252 :     case t_FFELT:
    3355         252 :       return ffeltmaprel_i(m, x);
    3356           0 :     case t_POL: case t_RFRAC: case t_SER:
    3357             :     case t_VEC: case t_COL: case t_MAT:
    3358           0 :       y = cgetg_copy(x, &lx);
    3359           0 :       for (i=1; i<lontyp[tx]; i++) y[i] = x[1];
    3360           0 :       for (i=lontyp[tx]; i<lx; i++)
    3361           0 :         gel(y,i) = ffmaprel_i(m, gel(x,i));
    3362           0 :       return y;
    3363             :   }
    3364           0 :   return gcopy(x);
    3365             : }
    3366             : 
    3367             : GEN
    3368         252 : ffmaprel(GEN m, GEN x)
    3369             : {
    3370         252 :   checkmap(m, "ffmaprel");
    3371         252 :   return ffmaprel_i(m, x);
    3372             : }
    3373             : 
    3374             : static void
    3375          84 : err_compo(GEN m, GEN n)
    3376          84 : { pari_err_DOMAIN("ffcompomap","m","domain does not contain codomain of",n,m); }
    3377             : 
    3378             : GEN
    3379         420 : ffcompomap(GEN m, GEN n)
    3380             : {
    3381         420 :   pari_sp av = avma;
    3382         420 :   GEN g = gel(n,1), r, m2, n2;
    3383         420 :   checkmap(m, "ffcompomap");
    3384         420 :   checkmap(n, "ffcompomap");
    3385         420 :   m2 = gel(m,2); n2 = gel(n,2);
    3386         420 :   switch((typ(m2)==t_POL)|((typ(n2)==t_POL)<<1))
    3387             :   {
    3388          84 :     case 0:
    3389          84 :       if (!FF_samefield(gel(m,1),n2)) err_compo(m,n);
    3390          42 :       r = FF_map(gel(m,2), n2);
    3391          42 :       break;
    3392          84 :     case 2:
    3393          84 :       r = ffmap_i(m, n2);
    3394          42 :       if (lg(r) == 1) err_compo(m,n);
    3395          42 :       break;
    3396         168 :     case 1:
    3397         168 :       r = ffeltmap_i(m, n2);
    3398         126 :       if (!r)
    3399             :       {
    3400             :         GEN a, A, R, M;
    3401             :         long dm, dn;
    3402          42 :         a = ffpartmapimage("ffcompomap",m2);
    3403          42 :         A = FF_to_FpXQ_i(FF_neg(n2));
    3404          42 :         setvarn(A, 1);
    3405          42 :         R = deg1pol(gen_1, A, 0);
    3406          42 :         setvarn(R, 0);
    3407          42 :         M = gcopy(m2);
    3408          42 :         setvarn(M, 1);
    3409          42 :         r = polresultant0(R, M, 1, 0);
    3410          42 :         dm = FF_f(gel(m,1)); dn = FF_f(gel(n,1));
    3411          42 :         if (dm % dn || !FFX_ispower(r, dm/dn, a, &r)) err_compo(m,n);
    3412          42 :         setvarn(r, varn(FF_mod(g)));
    3413             :       }
    3414         126 :       break;
    3415          84 :     case 3:
    3416             :     {
    3417             :       GEN M, R, T, p, a;
    3418          84 :       a = ffpartmapimage("ffcompomap",n2);
    3419          84 :       if (!FF_samefield(a, gel(m,1))) err_compo(m,n);
    3420          42 :       p = FF_p_i(gel(n,1));
    3421          42 :       T = FF_mod(gel(n,1));
    3422          42 :       setvarn(T, 1);
    3423          42 :       R = RgX_to_FpXQX(n2,T,p);
    3424          42 :       setvarn(R, 0);
    3425          42 :       M = gcopy(m2);
    3426          42 :       setvarn(M, 1);
    3427          42 :       r = polresultant0(R, M, 1, 0);
    3428          42 :       setvarn(r, varn(n2));
    3429             :     }
    3430             :   }
    3431         252 :   return gerepilecopy(av, mkvec2(g,r));
    3432             : }

Generated by: LCOV version 1.14