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 29401-b333f5c7d5) Lines: 1757 1956 89.8 %
Date: 2024-06-19 09:03:24 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      175238 : charact(struct charact *S, GEN x)
      53             : {
      54      175238 :   const long tx = typ(x);
      55             :   long i, l;
      56      175238 :   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      175210 : }
      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       27335 : characteristic(GEN x)
      96             : {
      97             :   struct charact S;
      98       27335 :   S.q = gen_0; S.isprime = 0;
      99       27335 :   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    69084061 : Rg_is_Fp(GEN x, GEN *pp)
     111             : {
     112             :   GEN mod;
     113    69084061 :   switch(typ(x))
     114             :   {
     115     3203536 :   case t_INTMOD:
     116     3203536 :     mod = gel(x,1);
     117     3203536 :     if (!*pp) *pp = mod;
     118     2954266 :     else if (mod != *pp && !equalii(mod, *pp))
     119             :     {
     120           0 :       if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_Fp");
     121           0 :       return 0;
     122             :     }
     123     3203536 :     return 1;
     124    54574893 :   case t_INT:
     125    54574893 :     return 1;
     126    11305632 :   default: return 0;
     127             :   }
     128             : }
     129             : 
     130             : int
     131    27875606 : RgX_is_FpX(GEN x, GEN *pp)
     132             : {
     133    27875606 :   long i, lx = lg(x);
     134    85627937 :   for (i=2; i<lx; i++)
     135    69057964 :     if (!Rg_is_Fp(gel(x, i), pp))
     136    11305634 :       return 0;
     137    16569973 :   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    36902994 : Rg_to_Fp(GEN x, GEN p)
     219             : {
     220    36902994 :   if (lgefint(p) == 3) return utoi(Rg_to_Fl(x, uel(p,2)));
     221     4555686 :   switch(typ(x))
     222             :   {
     223      288398 :     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     4248430 :     case t_INTMOD: {
     232     4248430 :       GEN q = gel(x,1), a = gel(x,2);
     233     4248430 :       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     3552086 : RgX_to_FpX(GEN x, GEN p)
     282             : {
     283             :   long i, l;
     284     3552086 :   GEN z = cgetg_copy(x, &l); z[1] = x[1];
     285    15794325 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     286     3552086 :   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      933038 : RgC_to_FpC(GEN x, GEN p)
     295    11542048 : { pari_APPLY_type(t_COL, Rg_to_Fp(gel(x,i), p)) }
     296             : 
     297             : GEN
     298      133812 : RgM_to_FpM(GEN x, GEN p)
     299     1066808 : { 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       49186 : RgX_to_FqX(GEN x, GEN T, GEN p)
     319             : {
     320       49186 :   long i, l = lg(x);
     321       49186 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     322       49186 :   if (T)
     323       10990 :     for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T, p);
     324             :   else
     325      791282 :     for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     326       49186 :   return FpXQX_renormalize(z, l);
     327             : }
     328             : 
     329             : GEN
     330      219128 : RgC_to_FqC(GEN x, GEN T, GEN p)
     331             : {
     332      219128 :   long i, l = lg(x);
     333      219128 :   GEN z = cgetg(l, t_COL);
     334      219128 :   if (T)
     335     1430310 :     for (i = 1; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T, p);
     336             :   else
     337           0 :     for (i = 1; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     338      219128 :   return z;
     339             : }
     340             : 
     341             : GEN
     342       52325 : RgM_to_FqM(GEN x, GEN T, GEN p)
     343      271418 : { pari_APPLY_same(RgC_to_FqC(gel(x, i), T, p)) }
     344             : 
     345             : /* lg(V) > 1 */
     346             : GEN
     347      851487 : FpXV_FpC_mul(GEN V, GEN W, GEN p)
     348             : {
     349      851487 :   pari_sp av = avma;
     350      851487 :   long i, l = lg(V);
     351      851487 :   GEN z = ZX_Z_mul(gel(V,1),gel(W,1));
     352     4201029 :   for(i=2; i<l; i++)
     353             :   {
     354     3349542 :     z = ZX_add(z, ZX_Z_mul(gel(V,i),gel(W,i)));
     355     3349542 :     if ((i & 7) == 0) z = gerepileupto(av, z);
     356             :   }
     357      851487 :   return gerepileupto(av, FpX_red(z,p));
     358             : }
     359             : 
     360             : GEN
     361       55832 : FqX_Fq_add(GEN y, GEN x, GEN T, GEN p)
     362             : {
     363       55832 :   long i, lz = lg(y);
     364             :   GEN z;
     365       55832 :   if (!T) return FpX_Fp_add(y, x, p);
     366        8666 :   if (lz == 2) return scalarpol(x, varn(y));
     367        7952 :   z = cgetg(lz,t_POL); z[1] = y[1];
     368        7952 :   gel(z,2) = Fq_add(gel(y,2),x, T, p);
     369        7952 :   if (lz == 3) z = FpXX_renormalize(z,lz);
     370             :   else
     371       33145 :     for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
     372        7952 :   return z;
     373             : }
     374             : 
     375             : GEN
     376        1094 : FqX_Fq_sub(GEN y, GEN x, GEN T, GEN p)
     377             : {
     378        1094 :   long i, lz = lg(y);
     379             :   GEN z;
     380        1094 :   if (!T) return FpX_Fp_sub(y, x, p);
     381        1094 :   if (lz == 2) return scalarpol(x, varn(y));
     382        1094 :   z = cgetg(lz,t_POL); z[1] = y[1];
     383        1094 :   gel(z,2) = Fq_sub(gel(y,2), x, T, p);
     384        1094 :   if (lz == 3) z = FpXX_renormalize(z,lz);
     385             :   else
     386       10303 :     for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
     387        1094 :   return z;
     388             : }
     389             : 
     390             : GEN
     391      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       38167 : FpXQX_normalize(GEN z, GEN T, GEN p)
     401             : {
     402             :   GEN lc;
     403       38167 :   if (lg(z) == 2) return z;
     404       38153 :   lc = leading_coeff(z);
     405       38153 :   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       36273 :   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      398831 : FqX_eval(GEN x, GEN y, GEN T, GEN p)
     421             : {
     422             :   pari_sp av;
     423             :   GEN p1, r;
     424      398831 :   long j, i=lg(x)-1;
     425      398831 :   if (i<=2)
     426       45957 :     return (i==2)? Fq_red(gel(x,2), T, p): gen_0;
     427      352874 :   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     1173881 :   for (i--; i>=2; i=j-1)
     431             :   {
     432      821710 :     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      821010 :     r = (i==j)? y: Fq_pow(y, utoipos(i-j+1), T, p);
     439      821010 :     p1 = Fq_add(Fq_mul(p1,r,T,p), gel(x,j), T, p);
     440             :   }
     441      352381 :   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    10495036 : monomial(GEN a, long d, long v)
     471             : {
     472             :   long i, n;
     473             :   GEN P;
     474    10495036 :   if (d < 0) {
     475          14 :     if (isrationalzero(a)) return pol_0(v);
     476          14 :     retmkrfrac(a, pol_xn(-d, v));
     477             :   }
     478    10495022 :   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    10401747 :     n = d+2; P = cgetg(n+1, t_POL);
     487    10401746 :     P[1] = evalsigne(1) | evalvarn(v);
     488             :   }
     489    29528760 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     490    10401746 :   gel(P,i) = a; return P;
     491             : }
     492             : GEN
     493     1863380 : monomialcopy(GEN a, long d, long v)
     494             : {
     495             :   long i, n;
     496             :   GEN P;
     497     1863380 :   if (d < 0) {
     498          14 :     if (isrationalzero(a)) return pol_0(v);
     499          14 :     retmkrfrac(gcopy(a), pol_xn(-d, v));
     500             :   }
     501     1863366 :   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     1863352 :     n = d+2; P = cgetg(n+1, t_POL);
     510     1863352 :     P[1] = evalsigne(1) | evalvarn(v);
     511             :   }
     512     3510654 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     513     1863359 :   gel(P,i) = gcopy(a); return P;
     514             : }
     515             : GEN
     516      324060 : pol_x_powers(long N, long v)
     517             : {
     518      324060 :   GEN L = cgetg(N+1,t_VEC);
     519             :   long i;
     520      783339 :   for (i=1; i<=N; i++) gel(L,i) = pol_xn(i-1, v);
     521      324067 :   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    11608988 : Fq_add(GEN x, GEN y, GEN T/*unused*/, GEN p)
     544             : {
     545             :   (void)T;
     546    11608988 :   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     9609233 :     case 3: return FpX_add(x,y,p);
     552             :   }
     553             :   return NULL;/*LCOV_EXCL_LINE*/
     554             : }
     555             : 
     556             : GEN
     557     8349634 : Fq_sub(GEN x, GEN y, GEN T/*unused*/, GEN p)
     558             : {
     559             :   (void)T;
     560     8349634 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     561             :   {
     562      256088 :     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     8045158 :     case 3: return FpX_sub(x,y,p);
     566             :   }
     567             :   return NULL;/*LCOV_EXCL_LINE*/
     568             : }
     569             : 
     570             : GEN
     571     1078271 : Fq_neg(GEN x, GEN T/*unused*/, GEN p)
     572             : {
     573             :   (void)T;
     574     1078271 :   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     8377884 : Fq_mul(GEN x, GEN y, GEN T, GEN p)
     587             : {
     588     8377884 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     589             :   {
     590     1037518 :     case 0: return Fp_mul(x,y,p);
     591      128947 :     case 1: return FpX_Fp_mul(x,y,p);
     592      402235 :     case 2: return FpX_Fp_mul(y,x,p);
     593     6809187 :     case 3: if (T) return FpXQ_mul(x,y,T,p);
     594     4231894 :             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      492844 : Fq_mulu(GEN x, ulong y, /*unused*/GEN T, GEN p)
     602             : {
     603             :   (void) T;
     604      492844 :   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      613170 : Fq_sqr(GEN x, GEN T, GEN p)
     618             : {
     619      613170 :   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      540326 :     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       89337 : Fq_inv(GEN x, GEN pol, GEN p)
     644             : {
     645       89337 :   if (typ(x) == t_INT) return Fp_inv(x,p);
     646       81571 :   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      795141 : Fq_pow(GEN x, GEN n, GEN pol, GEN p)
     664             : {
     665      795141 :   if (typ(x) == t_INT) return Fp_pow(x,n,p);
     666      136664 :   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       40395 : FqV_roots_to_pol(GEN V, GEN T, GEN p, long v)
     831             : {
     832       40395 :   pari_sp ltop = avma;
     833             :   long k;
     834             :   GEN W;
     835       40395 :   if (lgefint(p) == 3)
     836             :   {
     837       31721 :     ulong pp = p[2];
     838       31721 :     GEN Tl = ZX_to_Flx(T, pp);
     839       31721 :     GEN Vl = ZXC_to_FlxC(V, pp, get_Flx_var(Tl));
     840       31722 :     Tl = FlxqV_roots_to_pol(Vl, Tl, pp, v);
     841       31721 :     return gerepileupto(ltop, FlxX_to_ZXX(Tl));
     842             :   }
     843        8674 :   W = cgetg(lg(V),t_VEC);
     844       77865 :   for(k=1; k < lg(V); k++)
     845       69190 :     gel(W,k) = deg1pol_shallow(gen_1,Fq_neg(gel(V,k),T,p),v);
     846        8675 :   return gerepileupto(ltop, FpXQXV_prod(W, T, p));
     847             : }
     848             : 
     849             : GEN
     850      192425 : FqV_red(GEN x, GEN T, GEN p)
     851     1354635 : { 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     8160473 : primelist(forprime_t *S, long n, GEN dB)
     905             : {
     906     8160473 :   GEN P = cgetg(n+1, t_VECSMALL);
     907     8160458 :   long i = 1;
     908             :   ulong p;
     909    19369898 :   while (i <= n && (p = u_forprime_next(S)))
     910    11209440 :     if (!dB || umodiu(dB, p)) P[i++] = p;
     911     8160457 :   return P;
     912             : }
     913             : 
     914             : void
     915     7657885 : 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     7657885 :   long m = mmin? minss(mmin, n): usqrt(n);
     920             :   GEN  H, P, mod;
     921             :   pari_timer ti;
     922     7657893 :   if (DEBUGLEVEL > 4)
     923             :   {
     924           0 :     timer_start(&ti);
     925           0 :     err_printf("%s: nb primes: %ld\n",str, n);
     926             :   }
     927     7657888 :   if (m == 1)
     928             :   {
     929     7394902 :     GEN P = primelist(S, n, dB);
     930     7394881 :     GEN done = closure_callgen1(worker, P);
     931     7394887 :     H = gel(done,1);
     932     7394887 :     mod = gel(done,2);
     933     7394887 :     if (!*pH && center) H = center(H, mod, shifti(mod,-1));
     934     7394819 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: modular", str);
     935             :   }
     936             :   else
     937             :   {
     938      262986 :     long i, s = (n+m-1)/m, r = m - (m*s-n), di = 0;
     939             :     struct pari_mt pt;
     940      262986 :     long pending = 0;
     941      262986 :     H = cgetg(m+1, t_VEC); P = cgetg(m+1, t_VEC);
     942      262987 :     mt_queue_start_lim(&pt, worker, m);
     943     1083779 :     for (i=1; i<=m || pending; i++)
     944             :     {
     945             :       GEN done;
     946      820792 :       GEN pr = i <= m ? mkvec(primelist(S, i<=r ? s: s-1, dB)): NULL;
     947      820797 :       mt_queue_submit(&pt, i, pr);
     948      820796 :       done = mt_queue_get(&pt, NULL, &pending);
     949      820795 :       if (done)
     950             :       {
     951      765574 :         di++;
     952      765574 :         gel(H, di) = gel(done,1);
     953      765574 :         gel(P, di) = gel(done,2);
     954      765574 :         if (DEBUGLEVEL>5) err_printf("%ld%% ",100*di/m);
     955             :       }
     956             :     }
     957      262987 :     mt_queue_end(&pt);
     958      262987 :     if (DEBUGLEVEL>5) err_printf("\n");
     959      262987 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: modular", str);
     960      262987 :     H = crt(H, P, &mod);
     961      262987 :     if (DEBUGLEVEL>4) timer_printf(&ti,"%s: chinese", str);
     962             :   }
     963     7657806 :   if (*pH) H = crt(mkvec2(*pH, H), mkvec2(*pmod, mod), &mod);
     964     7657806 :   *pH = H; *pmod = mod;
     965     7657806 : }
     966             : void
     967     2016399 : 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     2016399 :   pari_sp av = avma;
     972     2016399 :   gen_inccrt_i(str, worker, dB, n, mmin, S, pH, pmod, crt, center);
     973     2016349 :   gerepileall(av, 2, pH, pmod);
     974     2016471 : }
     975             : 
     976             : GEN
     977     1241518 : 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     1241518 :   GEN mod = gen_1, H = NULL;
     981             :   ulong e;
     982             : 
     983     1241518 :   bound++;
     984     2483095 :   while (bound > (e = expi(mod)))
     985             :   {
     986     1241464 :     long n = (bound - e) / expu(S->p) + 1;
     987     1241500 :     gen_inccrt(str, worker, dB, n, mmin, S, &H, &mod, crt, center);
     988             :   }
     989     1241556 :   if (pmod) *pmod = mod;
     990     1241556 :   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     5112920 : Fl_chinese_coprime(GEN a, ulong b, GEN q, ulong p, ulong qinv, GEN pq, GEN pq2)
    1001             : {
    1002     5112920 :   ulong d, amod = umodiu(a, p);
    1003     5112958 :   pari_sp av = avma;
    1004             :   GEN ax;
    1005             : 
    1006     5112958 :   if (b == amod) return NULL;
    1007     2104502 :   d = Fl_mul(Fl_sub(b, amod, p), qinv, p); /* != 0 */
    1008     2105314 :   if (d >= 1 + (p>>1))
    1009     1026987 :     ax = subii(a, mului(p-d, q));
    1010             :   else
    1011             :   {
    1012     1078327 :     ax = addii(a, mului(d, q)); /* in ]0, pq[ assuming a in ]-q,q[ */
    1013     1077955 :     if (cmpii(ax,pq2) > 0) ax = subii(ax,pq);
    1014             :   }
    1015     2104632 :   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       31647 : ZX_init_CRT(GEN Hp, ulong p, long v)
    1021             : {
    1022       31647 :   long i, l = lg(Hp), lim = (long)(p>>1);
    1023       31647 :   GEN H = cgetg(l, t_POL);
    1024       31647 :   H[1] = evalsigne(1) | evalvarn(v);
    1025      795872 :   for (i=2; i<l; i++)
    1026      764225 :     gel(H,i) = stoi(Fl_center(Hp[i], p, lim));
    1027       31647 :   return ZX_renormalize(H,l);
    1028             : }
    1029             : 
    1030             : GEN
    1031        5775 : ZM_init_CRT(GEN Hp, ulong p)
    1032             : {
    1033        5775 :   long i,j, m, l = lg(Hp), lim = (long)(p>>1);
    1034        5775 :   GEN c, cp, H = cgetg(l, t_MAT);
    1035        5775 :   if (l==1) return H;
    1036        5775 :   m = lgcols(Hp);
    1037       18970 :   for (j=1; j<l; j++)
    1038             :   {
    1039       13195 :     cp = gel(Hp,j);
    1040       13195 :     c = cgetg(m, t_COL);
    1041       13195 :     gel(H,j) = c;
    1042      166411 :     for (i=1; i<m; i++) gel(c,i) = stoi(Fl_center(cp[i],p, lim));
    1043             :   }
    1044        5775 :   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      147434 : ZX_incremental_CRT_raw(GEN *ptH, GEN Hp, GEN q, GEN qp, ulong p)
    1060             : {
    1061      147434 :   GEN H = *ptH, h, qp2 = shifti(qp,-1);
    1062      147426 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1063      147426 :   long i, l = lg(H), lp = lg(Hp);
    1064      147426 :   int stable = 1;
    1065             : 
    1066      147426 :   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      147426 :   } 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     4932896 :   for (i=2; i<lp; i++)
    1081             :   {
    1082     4785556 :     h = Fl_chinese_coprime(gel(H,i),Hp[i],q,p,qinv,qp,qp2);
    1083     4785470 :     if (h) { gel(H,i) = h; stable = 0; }
    1084             :   }
    1085      147340 :   (void)ZX_renormalize(H,lp);
    1086      147431 :   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       23195 : Flx_resultant_set_dglist(GEN a, GEN b, GEN dglist, ulong p)
    1174             : {
    1175             :   long da,db,dc, ind;
    1176       23195 :   pari_sp av = avma;
    1177             : 
    1178       23195 :   if (lgpol(a)==0 || lgpol(b)==0) return;
    1179       21928 :   da = degpol(a);
    1180       21928 :   db = degpol(b);
    1181       21928 :   if (db > da)
    1182           0 :   { swapspec(a,b, da,db); }
    1183       21928 :   else if (!da) return;
    1184       21928 :   ind = 0;
    1185      144149 :   while (db)
    1186             :   {
    1187      122223 :     GEN c = Flx_rem(a,b, p);
    1188      122221 :     a = b; b = c; dc = degpol(c);
    1189      122220 :     if (dc < 0) break;
    1190             : 
    1191      122220 :     ind++;
    1192      122220 :     if (dc > dglist[ind]) dglist[ind] = dc;
    1193      122220 :     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      122221 :     db = dc; /* = degpol(b) */
    1199             :   }
    1200       21926 :   if (ind+1 > lg(dglist)) setlg(dglist,ind+1);
    1201       21926 :   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     2084473 : Flx_resultant_all(GEN a, GEN b, long *C0, long *C1, GEN dglist, ulong p)
    1208             : {
    1209             :   long da,db,dc, ind;
    1210     2084473 :   ulong lb, res, g = 1UL, h = 1UL, ca = 1UL, cb = 1UL;
    1211     2084473 :   int s = 1;
    1212     2084473 :   pari_sp av = avma;
    1213             : 
    1214     2084473 :   *C0 = 1; *C1 = 0;
    1215     2084473 :   if (lgpol(a)==0 || lgpol(b)==0) return 0;
    1216     2074927 :   da = degpol(a);
    1217     2074916 :   db = degpol(b);
    1218     2074938 :   if (db > da)
    1219             :   {
    1220           0 :     swapspec(a,b, da,db);
    1221           0 :     if (both_odd(da,db)) s = -s;
    1222             :   }
    1223     2074938 :   else if (!da) return 1; /* = a[2] ^ db, since 0 <= db <= da = 0 */
    1224     2074938 :   ind = 0;
    1225    19790568 :   while (db)
    1226             :   { /* sub-resultant algo., applied to ca * a and cb * b, ca,cb scalars,
    1227             :      * da = deg a, db = deg b */
    1228    17720150 :     GEN c = Flx_rem(a,b, p);
    1229    17603049 :     long delta = da - db;
    1230             : 
    1231    17603049 :     if (both_odd(da,db)) s = -s;
    1232    17602226 :     lb = Fl_mul(b[db+2], cb, p);
    1233    17629293 :     a = b; b = c; dc = degpol(c);
    1234    17631866 :     ind++;
    1235    17631866 :     if (dc != dglist[ind]) return gc_ulong(av,0); /* degenerates */
    1236    17626975 :     if (g == h)
    1237             :     { /* frequent */
    1238    17567131 :       ulong cc = Fl_mul(ca, Fl_powu(Fl_div(lb,g,p), delta+1, p), p);
    1239    17656135 :       ca = cb;
    1240    17656135 :       cb = cc;
    1241             :     }
    1242             :     else
    1243             :     {
    1244       59844 :       ulong cc = Fl_mul(ca, Fl_powu(lb, delta+1, p), p);
    1245       59844 :       ulong ghdelta = Fl_mul(g, Fl_powu(h, delta, p), p);
    1246       59844 :       ca = cb;
    1247       59844 :       cb = Fl_div(cc, ghdelta, p);
    1248             :     }
    1249    17716722 :     da = db; /* = degpol(a) */
    1250    17716722 :     db = dc; /* = degpol(b) */
    1251             : 
    1252    17716722 :     g = lb;
    1253    17716722 :     if (delta == 1)
    1254    17617451 :       h = g; /* frequent */
    1255             :     else
    1256       99271 :       h = Fl_mul(h, Fl_powu(Fl_div(g,h,p), delta, p), p);
    1257             : 
    1258    17715670 :     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     2070418 :   if (da > 1) return 0; /* Failure */
    1265             :   /* last nonconstant polynomial has degree 1 */
    1266     2070418 :   *C0 = Fl_mul(ca, a[2], p);
    1267     2070364 :   *C1 = Fl_mul(ca, a[3], p);
    1268     2070366 :   res = Fl_mul(cb, b[2], p);
    1269     2070343 :   if (s == -1) res = p - res;
    1270     2070343 :   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     2107843 : FlxY_evalx_drop(GEN Q, ulong x, ulong p)
    1277             : {
    1278             :   GEN z;
    1279     2107843 :   long i, lb = lg(Q);
    1280     2107843 :   ulong leadz = Flx_eval(leading_coeff(Q), x, p);
    1281     2107477 :   long vs=mael(Q,2,1);
    1282     2107477 :   if (!leadz) return zero_Flx(vs);
    1283             : 
    1284     2096817 :   z = cgetg(lb, t_VECSMALL); z[1] = vs;
    1285    20058636 :   for (i=2; i<lb-1; i++) z[i] = Flx_eval(gel(Q,i), x, p);
    1286     2096568 :   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      272463 : ZX_norml1(GEN x)
    1311             : {
    1312      272463 :   long i, l = lg(x);
    1313             :   GEN s;
    1314             : 
    1315      272463 :   if (l == 2) return gen_0;
    1316      179909 :   s = gel(x, l-1); /* != 0 */
    1317      658423 :   for (i = l-2; i > 1; i--) {
    1318      478514 :     GEN xi = gel(x,i);
    1319      478514 :     if (!signe(xi)) continue;
    1320      239958 :     s = addii_sign(s,1, xi,1);
    1321             :   }
    1322      179909 :   return s;
    1323             : }
    1324             : /* x >= 0, y != 0, return x + |y| */
    1325             : static GEN
    1326       25594 : addii_abs(GEN x, GEN y)
    1327             : {
    1328       25594 :   if (!signe(x)) return absi_shallow(y);
    1329       16055 :   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       31714 : ZX_norml1_1(GEN x, long k)
    1335             : {
    1336       31714 :   long i, d = degpol(x);
    1337             :   GEN s, C; /* = binomial(i, k) */
    1338             : 
    1339       31714 :   if (!d || k > d) return gen_0;
    1340       31715 :   s = absi_shallow(gel(x, k+2)); /* may be 0 */
    1341       31714 :   C = gen_1;
    1342       68188 :   for (i = k+1; i <= d; i++) {
    1343       36478 :     GEN xi = gel(x,i+2);
    1344       36478 :     if (k) C = diviuexact(muliu(C, i), i-k);
    1345       36481 :     if (signe(xi)) s = addii_abs(s, mulii(C, xi));
    1346             :   }
    1347       31710 :   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        7409 : sqrN2(GEN A, long prec)
    1370             : {
    1371        7409 :   pari_sp av = avma;
    1372        7409 :   long i, l = lg(A);
    1373        7409 :   GEN a = gen_0;
    1374       36671 :   for (i = 2; i < l; i++)
    1375             :   {
    1376       29262 :     a = gadd(a, gabs(gnorm(gel(A,i)), prec));
    1377       29262 :     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        7409 :   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        6562 : RgX_RgXY_ResBound(GEN A, GEN B, long prec)
    1391             : {
    1392        6562 :   pari_sp av = avma;
    1393        6562 :   GEN b = gen_0, bnd;
    1394        6562 :   long i, lB = lg(B);
    1395       26322 :   for (i=2; i<lB; i++)
    1396             :   {
    1397       19760 :     GEN t = gel(B,i);
    1398       19760 :     if (typ(t) == t_POL) t = gnorml1(t, prec);
    1399       19760 :     b = gadd(b, gabs(gsqr(t), prec));
    1400       19760 :     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        6562 :   bnd = gsqrt(gmul(gpowgs(sqrN2(A,prec), degpol(B)),
    1407             :                    gpowgs(b, degpol(A))), prec);
    1408        6562 :   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      150679 : log2N2(GEN A)
    1437             : {
    1438      150679 :   pari_sp av = avma;
    1439      150679 :   long i, l = lg(A);
    1440      150679 :   GEN a = gen_0;
    1441      954936 :   for (i=2; i < l; i++)
    1442             :   {
    1443      804264 :     a = addii(a, sqri(gel(A,i)));
    1444      804258 :     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      150672 :   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      140577 : ZX_ZXY_ResBound(GEN A, GEN B, GEN dB)
    1458             : {
    1459      140577 :   pari_sp av = avma;
    1460      140577 :   GEN b = gen_0;
    1461      140577 :   long i, lB = lg(B);
    1462             :   double logb;
    1463      908630 :   for (i=2; i<lB; i++)
    1464             :   {
    1465      768065 :     GEN t = gel(B,i);
    1466      768065 :     if (typ(t) == t_POL) t = ZX_norml1(t);
    1467      768068 :     b = addii(b, sqri(t));
    1468      768053 :     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      140565 :   logb = dbllog2(b); if (dB) logb -= 2 * dbllog2(dB);
    1475      140574 :   i = (long)((degpol(B) * log2N2(A) + degpol(A) * logb) / 2);
    1476      140575 :   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       10107 : ZX_ZXY_ResBound_1(GEN A, GEN B)
    1481             : {
    1482       10107 :   pari_sp av = avma;
    1483       10107 :   GEN b = gen_0;
    1484       10107 :   long i, lB = lg(B);
    1485       41822 :   for (i=2; i<lB; i++)
    1486             :   {
    1487       31713 :     b = addii(b, sqri(ZX_norml1_1(B, i-2)));
    1488       31715 :     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       10109 :   i = (long)((degpol(B) * log2N2(A) + degpol(A) * dbllog2(b)) / 2);
    1495       10107 :   return gc_ulong(av, (i <= 0)? 1: 1 + (ulong)i);
    1496             : }
    1497             : /* special case B = A' */
    1498             : static ulong
    1499     1128645 : ZX_discbound(GEN A)
    1500             : {
    1501     1128645 :   pari_sp av = avma;
    1502     1128645 :   GEN a = gen_0, b = gen_0;
    1503     1128645 :   long i , lA = lg(A), dA = degpol(A);
    1504             :   double loga, logb;
    1505     6727798 :   for (i = 2; i < lA; i++)
    1506             :   {
    1507     5599324 :     GEN c = sqri(gel(A,i));
    1508     5599004 :     a = addii(a, c);
    1509     5599134 :     if (i > 2) b = addii(b, mulii(c, sqru(i-2)));
    1510     5599161 :     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     1128474 :   loga = dbllog2(a);
    1517     1128585 :   logb = dbllog2(b); set_avma(av);
    1518     1128602 :   i = (long)(((dA-1) * loga + dA * logb) / 2);
    1519     1128602 :   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     2270400 : Flx_FlxY_eval_resultant(GEN a, GEN b, ulong n, ulong p, ulong pi, ulong la)
    1525             : {
    1526     2270400 :   GEN ev = FlxY_evalx_pre(b, n, p, pi);
    1527     2270243 :   long drop = lg(b) - lg(ev);
    1528     2270243 :   ulong r = Flx_resultant_pre(a, ev, p, pi);
    1529     2270363 :   if (drop && la != 1) r = Fl_mul(r, Fl_powu_pre(la, drop, p, pi), p);
    1530     2270363 :   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      177324 : Flx_FlxY_resultant_polint(GEN a, GEN b, ulong p, ulong pi, long dres, long sx)
    1546             : {
    1547             :   long i;
    1548      177324 :   ulong n, la = Flx_lead(a);
    1549      177324 :   GEN  x = cgetg(dres+2, t_VECSMALL);
    1550      177323 :   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     1226845 :   for (i=0,n = 1; i < dres; n++)
    1554             :   {
    1555     1049520 :     x[++i] = n;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,pi,la);
    1556     1049480 :     x[++i] = p-n; y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,pi,la);
    1557             :   }
    1558      177325 :   if (i == dres)
    1559             :   {
    1560      171899 :     x[++i] = 0;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,pi,la);
    1561             :   }
    1562      177323 :   return Flv_polint(x,y, p, sx);
    1563             : }
    1564             : 
    1565             : static GEN
    1566        7573 : FlxX_pseudorem(GEN x, GEN y, ulong p, ulong pi)
    1567             : {
    1568        7573 :   long vx = varn(x), dx, dy, dz, i, lx, dp;
    1569        7573 :   pari_sp av = avma, av2;
    1570             : 
    1571        7573 :   if (!signe(y)) pari_err_INV("FlxX_pseudorem",y);
    1572        7573 :   (void)new_chunk(2);
    1573        7574 :   dx=degpol(x); x = RgX_recip_i(x)+2;
    1574        7574 :   dy=degpol(y); y = RgX_recip_i(y)+2; dz=dx-dy; dp = dz+1;
    1575        7574 :   av2 = avma;
    1576             :   for (;;)
    1577             :   {
    1578       62057 :     gel(x,0) = Flx_neg(gel(x,0), p); dp--;
    1579      232585 :     for (i=1; i<=dy; i++)
    1580      170197 :       gel(x,i) = Flx_add( Flx_mul_pre(gel(y,0), gel(x,i), p, pi),
    1581      170500 :                           Flx_mul_pre(gel(x,0), gel(y,i), p, pi), p );
    1582     1129370 :     for (   ; i<=dx; i++)
    1583     1067969 :       gel(x,i) = Flx_mul_pre(gel(y,0), gel(x,i), p, pi);
    1584       65971 :     do { x++; dx--; } while (dx >= 0 && lg(gel(x,0))==2);
    1585       61401 :     if (dx < dy) break;
    1586       53830 :     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        7571 :   if (dx < 0) return zero_Flx(0);
    1593        7571 :   lx = dx+3; x -= 2;
    1594        7571 :   x[0]=evaltyp(t_POL) | _evallg(lx);
    1595        7571 :   x[1]=evalsigne(1) | evalvarn(vx);
    1596        7571 :   x = RgX_recip_i(x);
    1597        7572 :   if (dp)
    1598             :   { /* multiply by y[0]^dp   [beware dummy vars from FpX_FpXY_resultant] */
    1599        1979 :     GEN t = Flx_powu_pre(gel(y,0), dp, p, pi);
    1600        7920 :     for (i=2; i<lx; i++) gel(x,i) = Flx_mul_pre(gel(x,i), t, p, pi);
    1601             :   }
    1602        7573 :   return gerepilecopy(av, x);
    1603             : }
    1604             : 
    1605             : /* return a Flx */
    1606             : GEN
    1607        2535 : FlxX_resultant(GEN u, GEN v, ulong p, long sx)
    1608             : {
    1609        2535 :   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        2535 :   dx = degpol(u); dy = degpol(v); signh = 1;
    1615        2535 :   if (dx < dy)
    1616             :   {
    1617           7 :     swap(u,v); lswap(dx,dy);
    1618           7 :     if (both_odd(dx, dy)) signh = -signh;
    1619             :   }
    1620        2535 :   if (dy < 0) return zero_Flx(sx);
    1621        2535 :   pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1622        2535 :   if (dy==0) return gerepileupto(av, Flx_powu_pre(gel(v,2),dx,p,pi));
    1623             : 
    1624        2535 :   g = h = pol1_Flx(sx); av2 = avma;
    1625             :   for(;;)
    1626             :   {
    1627        7573 :     r = FlxX_pseudorem(u,v,p,pi); dr = lg(r);
    1628        7574 :     if (dr == 2) { set_avma(av); return zero_Flx(sx); }
    1629        7574 :     du = degpol(u); dv = degpol(v); degq = du-dv;
    1630        7573 :     u = v; p1 = g; g = leading_coeff(u);
    1631        7574 :     switch(degq)
    1632             :     {
    1633           0 :       case 0: break;
    1634        5582 :       case 1:
    1635        5582 :         p1 = Flx_mul_pre(h,p1, p, pi); h = g; break;
    1636        1992 :       default:
    1637        1992 :         p1 = Flx_mul_pre(Flx_powu_pre(h,degq,p,pi), p1, p, pi);
    1638        1992 :         h = Flx_div_pre(Flx_powu_pre(g,degq,p,pi),
    1639        1993 :                         Flx_powu_pre(h,degq-1,p,pi), p, pi);
    1640             :     }
    1641        7574 :     if (both_odd(du,dv)) signh = -signh;
    1642        7571 :     v = FlxY_Flx_div(r, p1, p);
    1643        7570 :     if (dr==3) break;
    1644        5035 :     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        2535 :   z = gel(v,2);
    1651        2535 :   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        2535 :   if (signh < 0) z = Flx_neg(z,p);
    1654        2535 :   return gerepileupto(av, z);
    1655             : }
    1656             : 
    1657             : /* Warning:
    1658             :  * This function switches between valid and invalid variable ordering*/
    1659             : 
    1660             : static GEN
    1661        6146 : FlxY_to_FlyX(GEN b, long sv)
    1662             : {
    1663        6146 :   long i, n=-1;
    1664        6146 :   long sw = b[1]&VARNBITS;
    1665       20980 :   for(i=2;i<lg(b);i++) n = maxss(n,lgpol(gel(b,i)));
    1666        6146 :   return Flm_to_FlxX(Flm_transpose(FlxX_to_Flm(b,n)),sv,sw);
    1667             : }
    1668             : 
    1669             : /* Return a Fly*/
    1670             : GEN
    1671        6146 : Flx_FlxY_resultant(GEN a, GEN b, ulong p)
    1672             : {
    1673        6146 :   pari_sp ltop=avma;
    1674        6146 :   long dres = degpol(a)*degpol(b);
    1675        6146 :   long sx=a[1], sy=b[1]&VARNBITS;
    1676             :   GEN z;
    1677        6146 :   b = FlxY_to_FlyX(b,sx);
    1678        6146 :   if ((ulong)dres >= p)
    1679        2535 :     z = FlxX_resultant(Fly_to_FlxY(a, sy), b, p, sx);
    1680             :   else
    1681             :   {
    1682        3611 :     ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    1683        3611 :     z = Flx_FlxY_resultant_polint(a, b, p, pi, (ulong)dres, sy);
    1684             :   }
    1685        6146 :   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      126097 : swap_vars(GEN b0, long v)
    1696             : {
    1697      126097 :   long i, n = RgX_degree(b0, v);
    1698             :   GEN b, x;
    1699      126095 :   if (n < 0) return pol_0(v);
    1700      126095 :   b = cgetg(n+3, t_POL); x = b + 2;
    1701      126095 :   b[1] = evalsigne(1) | evalvarn(v);
    1702      640884 :   for (i=0; i<=n; i++) gel(x,i) = polcoef_i(b0, i, v);
    1703      126093 :   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       21034 : ZX_ZXY_resultant_LERS(GEN A, GEN B0, long *plambda, GEN *LERS)
    1810             : {
    1811             :   ulong bound, dp;
    1812       21034 :   pari_sp av = avma, av2 = 0;
    1813       21034 :   long lambda = *plambda, degA = degpol(A), dres = degA*degpol(B0);
    1814             :   long stable, checksqfree, i,n, cnt, degB;
    1815       21034 :   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       21034 :   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       19991 :   dglist = Hp = H0p = H1p = C0 = C1 = NULL; /* gcc -Wall */
    1830       19991 :   C0 = cgetg(dres+2, t_VECSMALL);
    1831       19992 :   C1 = cgetg(dres+2, t_VECSMALL);
    1832       19991 :   dglist = cgetg(dres+1, t_VECSMALL);
    1833       19991 :   x = cgetg(dres+2, t_VECSMALL);
    1834       19991 :   y = cgetg(dres+2, t_VECSMALL);
    1835       19992 :   B0 = leafcopy(B0);
    1836       19992 :   A = leafcopy(A);
    1837       19992 :   B = B0;
    1838       19992 :   v = fetch_var_higher(); setvarn(A,v);
    1839             :   /* make sure p large enough */
    1840       20654 : INIT:
    1841             :   /* always except the first time */
    1842       20654 :   if (av2) { set_avma(av2); lambda = next_lambda(lambda); }
    1843       20654 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    1844       20654 :   B = swap_vars(B, vY); setvarn(B,v);
    1845             :   /* B0(lambda v + x, v) */
    1846       20653 :   if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    1847       20653 :   av2 = avma;
    1848             : 
    1849       20653 :   if (degA <= 3)
    1850             :   { /* sub-resultant faster for small degrees */
    1851        9995 :     H = RgX_resultant_all(A,B,&q);
    1852        9996 :     if (typ(q) != t_POL || degpol(q)!=1) goto INIT;
    1853        9485 :     H0 = gel(q,2);
    1854        9485 :     if (typ(H0) == t_POL) setvarn(H0,vX); else H0 = scalarpol(H0,vX);
    1855        9485 :     H1 = gel(q,3);
    1856        9485 :     if (typ(H1) == t_POL) setvarn(H1,vX); else H1 = scalarpol(H1,vX);
    1857        9484 :     if (!ZX_is_squarefree(H)) goto INIT;
    1858        9443 :     goto END;
    1859             :   }
    1860             : 
    1861       10658 :   H = H0 = H1 = NULL;
    1862       10658 :   degB = degpol(B);
    1863       10658 :   bound = ZX_ZXY_ResBound(A, B, NULL);
    1864       10658 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    1865       10658 :   dp = 1;
    1866       10658 :   init_modular_big(&S);
    1867       10658 :   for(cnt = 0, checksqfree = 1;;)
    1868       49144 :   {
    1869       59802 :     ulong p = u_forprime_next(&S);
    1870             :     GEN Hi;
    1871       59802 :     a = ZX_to_Flx(A, p);
    1872       59802 :     b = ZXX_to_FlxX(B, p, varn(A));
    1873       59802 :     if (degpol(a) < degA || degpol(b) < degB) continue; /* p | lc(A)lc(B) */
    1874       59802 :     if (checksqfree)
    1875             :     { /* find degree list for generic Euclidean Remainder Sequence */
    1876       10658 :       long goal = minss(degpol(a), degpol(b)); /* longest possible */
    1877       73035 :       for (n=1; n <= goal; n++) dglist[n] = 0;
    1878       10658 :       setlg(dglist, 1);
    1879       23587 :       for (n=0; n <= dres; n++)
    1880             :       {
    1881       23195 :         ev = FlxY_evalx_drop(b, n, p);
    1882       23195 :         Flx_resultant_set_dglist(a, ev, dglist, p);
    1883       23193 :         if (lg(dglist)-1 == goal) break;
    1884             :       }
    1885             :       /* last pol in ERS has degree > 1 ? */
    1886       10656 :       goal = lg(dglist)-1;
    1887       10656 :       if (degpol(B) == 1) { if (!goal) goto INIT; }
    1888             :       else
    1889             :       {
    1890       10600 :         if (goal <= 1) goto INIT;
    1891       10544 :         if (dglist[goal] != 0 || dglist[goal-1] != 1) goto INIT;
    1892             :       }
    1893       10600 :       if (DEBUGLEVEL>4)
    1894           0 :         err_printf("Degree list for ERS (trials: %ld) = %Ps\n",n+1,dglist);
    1895             :     }
    1896             : 
    1897     2144388 :     for (i=0,n = 0; i <= dres; n++)
    1898             :     {
    1899     2084656 :       ev = FlxY_evalx_drop(b, n, p);
    1900     2084412 :       x[++i] = n; y[i] = Flx_resultant_all(a, ev, C0+i, C1+i, dglist, p);
    1901     2084643 :       if (!C1[i]) i--; /* C1(i) = 0. No way to recover C0(i) */
    1902             :     }
    1903       59732 :     Hi = Flv_Flm_polint(x, mkvec3(y,C0,C1), p, 0);
    1904       59747 :     Hp = gel(Hi,1); H0p = gel(Hi,2); H1p = gel(Hi,3);
    1905       59747 :     if (!H && degpol(Hp) != dres) continue;
    1906       59747 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1907       59747 :     if (checksqfree) {
    1908       10602 :       if (!Flx_is_squarefree(Hp, p)) goto INIT;
    1909       10549 :       if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    1910       10549 :       checksqfree = 0;
    1911             :     }
    1912             : 
    1913       59694 :     if (!H)
    1914             :     { /* initialize */
    1915       10549 :       q = utoipos(p); stable = 0;
    1916       10549 :       H = ZX_init_CRT(Hp, p,vX);
    1917       10549 :       H0= ZX_init_CRT(H0p, p,vX);
    1918       10549 :       H1= ZX_init_CRT(H1p, p,vX);
    1919             :     }
    1920             :     else
    1921             :     {
    1922       49145 :       GEN qp = muliu(q,p);
    1923       49145 :       stable  = ZX_incremental_CRT_raw(&H, Hp, q,qp, p)
    1924       49144 :               & ZX_incremental_CRT_raw(&H0,H0p, q,qp, p)
    1925       49145 :               & ZX_incremental_CRT_raw(&H1,H1p, q,qp, p);
    1926       49144 :       q = qp;
    1927             :     }
    1928             :     /* could make it probabilistic for H ? [e.g if stable twice, etc]
    1929             :      * Probabilistic anyway for H0, H1 */
    1930       59693 :     if (DEBUGLEVEL>5 && (stable ||  ++cnt==100))
    1931           0 :     { cnt=0; err_printf("%ld%%%s ",100*expi(q)/bound,stable?"s":""); }
    1932       59694 :     if (stable && (ulong)expi(q) >= bound) break; /* DONE */
    1933       49145 :     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       19992 : END:
    1940       19992 :   if (DEBUGLEVEL>5) err_printf(" done\n");
    1941       19992 :   setvarn(H, vX); (void)delete_var();
    1942       19992 :   *LERS = mkvec2(H0,H1);
    1943       19992 :   *plambda = lambda; return gc_all(av, 2, &H, LERS);
    1944             : }
    1945             : 
    1946             : GEN
    1947       58827 : ZX_ZXY_resultant_all(GEN A, GEN B, long *plambda, GEN *LERS)
    1948             : {
    1949       58827 :   if (LERS)
    1950             :   {
    1951       21034 :     if (!plambda)
    1952           0 :       pari_err_BUG("ZX_ZXY_resultant_all [LERS != NULL needs lambda]");
    1953       21034 :     return ZX_ZXY_resultant_LERS(A, B, plambda, LERS);
    1954             :   }
    1955       37793 :   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        3492 : ZXQ_charpoly_sqf(GEN A, GEN T, long *lambda, long v)
    1963             : {
    1964        3492 :   pari_sp av = avma;
    1965             :   GEN R, a;
    1966             :   long dA;
    1967             :   int delvar;
    1968             : 
    1969        3492 :   if (v < 0) v = 0;
    1970        3492 :   switch (typ(A))
    1971             :   {
    1972        3492 :     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        3492 :   delvar = 0;
    1979        3492 :   if (varn(T) == 0)
    1980             :   {
    1981        3296 :     long v0 = fetch_var(); delvar = 1;
    1982        3296 :     T = leafcopy(T); setvarn(T,v0);
    1983        3296 :     A = leafcopy(A); setvarn(A,v0);
    1984             :   }
    1985        3492 :   R = ZX_ZXY_rnfequation(T, deg1pol_shallow(gen_1, gneg_i(A), 0), lambda);
    1986        3492 :   if (delvar) (void)delete_var();
    1987        3492 :   setvarn(R, v); a = leading_coeff(T);
    1988        3492 :   if (!gequal1(a)) R = gdiv(R, powiu(a, dA));
    1989        3492 :   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      120897 : ZXQ_charpoly(GEN A, GEN T, long v)
    1995             : {
    1996      120897 :   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     3757254 : ZX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, ulong p)
    2010             : {
    2011     3757254 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    2012             :   ulong H, dp;
    2013     3757132 :   if (dropa && dropb) return 0; /* p | lc(A), p | lc(B) */
    2014     3757132 :   H = Flx_resultant(a, b, p);
    2015     3756965 :   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     3756965 :   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     3756967 :   dp = dB ? umodiu(dB, p): 1;
    2029     3756967 :   if (dp != 1) H = Fl_mul(H, Fl_powu(Fl_inv(dp,p), degA, p), p);
    2030     3756965 :   return H;
    2031             : }
    2032             : 
    2033             : /* If B=NULL, assume B=A' */
    2034             : static GEN
    2035     1469325 : ZX_resultant_slice(GEN A, GEN B, GEN dB, GEN P, GEN *mod)
    2036             : {
    2037     1469325 :   pari_sp av = avma, av2;
    2038     1469325 :   long degA, degB, i, n = lg(P)-1;
    2039             :   GEN H, T;
    2040             : 
    2041     1469325 :   degA = degpol(A);
    2042     1469319 :   degB = B? degpol(B): degA - 1;
    2043     1469320 :   if (n == 1)
    2044             :   {
    2045      804015 :     ulong Hp, p = uel(P,1);
    2046      804015 :     GEN a = ZX_to_Flx(A, p), b = B? ZX_to_Flx(B, p): Flx_deriv(a, p);
    2047      804018 :     Hp = ZX_resultant_prime(a, b, dB, degA, degB, p);
    2048      804037 :     set_avma(av); *mod = utoipos(p); return utoi(Hp);
    2049             :   }
    2050      665305 :   T = ZV_producttree(P);
    2051      665312 :   A = ZX_nv_mod_tree(A, P, T);
    2052      665309 :   if (B) B = ZX_nv_mod_tree(B, P, T);
    2053      665309 :   H = cgetg(n+1, t_VECSMALL); av2 = avma;
    2054     3618275 :   for(i=1; i <= n; i++, set_avma(av2))
    2055             :   {
    2056     2952967 :     ulong p = P[i];
    2057     2952967 :     GEN a = gel(A,i), b = B? gel(B,i): Flx_deriv(a, p);
    2058     2953244 :     H[i] = ZX_resultant_prime(a, b, dB, degA, degB, p);
    2059             :   }
    2060      665308 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    2061      665312 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2062             : }
    2063             : 
    2064             : GEN
    2065     1469338 : ZX_resultant_worker(GEN P, GEN A, GEN B, GEN dB)
    2066             : {
    2067     1469338 :   GEN V = cgetg(3, t_VEC);
    2068     1469327 :   if (typ(B) == t_INT) B = NULL;
    2069     1469327 :   if (!signe(dB)) dB = NULL;
    2070     1469327 :   gel(V,1) = ZX_resultant_slice(A, B, dB, P, &gel(V,2));
    2071     1469343 :   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     1312908 : ZX_resultant_all(GEN A, GEN B, GEN dB, ulong bound)
    2078             : {
    2079     1312908 :   pari_sp av = avma;
    2080             :   forprime_t S;
    2081             :   GEN  H, worker;
    2082     1312908 :   if (!B && degpol(A)==2)
    2083             :   {
    2084      113394 :     GEN a = gel(A,4), b = gel(A,3), c = gel(A,2);
    2085      113394 :     H = mulii(a, subii(shifti(mulii(a, c), 2), sqri(b)));
    2086      113379 :     if (dB) H = diviiexact(H, sqri(dB));
    2087      113379 :     return gerepileuptoint(av, H);
    2088             :   }
    2089     1199512 :   if (B)
    2090             :   {
    2091      122671 :     long a = degpol(A), b = degpol(B);
    2092      122671 :     if (a < 0 || b < 0) return gen_0;
    2093      122641 :     if (!a) return powiu(gel(A,2), b);
    2094      122641 :     if (!b) return powiu(gel(B,2), a);
    2095      120896 :     if (minss(a, b) <= 1)
    2096             :     {
    2097       51064 :       H = RgX_resultant_all(A, B, NULL);
    2098       51064 :       if (dB) H = diviiexact(H, powiu(dB, a));
    2099       51064 :       return gerepileuptoint(av, H);
    2100             :     }
    2101       69832 :     if (!bound) bound = ZX_ZXY_ResBound(A, B, dB);
    2102             :   }
    2103     1146681 :   worker = snm_closure(is_entry("_ZX_resultant_worker"),
    2104             :                        mkvec3(A, B? B: gen_0, dB? dB: gen_0));
    2105     1146725 :   init_modular_big(&S);
    2106     1146686 :   H = gen_crt("ZX_resultant_all", worker, &S, dB, bound, 0, NULL,
    2107             :               ZV_chinese_center, Fp_center);
    2108     1146715 :   return gerepileuptoint(av, H);
    2109             : }
    2110             : 
    2111             : /* A0 and B0 in Q[X] */
    2112             : GEN
    2113          56 : QX_resultant(GEN A0, GEN B0)
    2114             : {
    2115             :   GEN s, a, b, A, B;
    2116          56 :   pari_sp av = avma;
    2117             : 
    2118          56 :   A = Q_primitive_part(A0, &a);
    2119          56 :   B = Q_primitive_part(B0, &b);
    2120          56 :   s = ZX_resultant(A, B);
    2121          56 :   if (!signe(s)) { set_avma(av); return gen_0; }
    2122          56 :   if (a) s = gmul(s, gpowgs(a,degpol(B)));
    2123          56 :   if (b) s = gmul(s, gpowgs(b,degpol(A)));
    2124          56 :   return gerepileupto(av, s);
    2125             : }
    2126             : 
    2127             : GEN
    2128       25312 : ZX_resultant(GEN A, GEN B) { return ZX_resultant_all(A,B,NULL,0); }
    2129             : 
    2130             : GEN
    2131           0 : QXQ_intnorm(GEN A, GEN B)
    2132             : {
    2133             :   GEN c, n, R, lB;
    2134           0 :   long dA = degpol(A), dB = degpol(B);
    2135           0 :   pari_sp av = avma;
    2136           0 :   if (dA < 0) return gen_0;
    2137           0 :   A = Q_primitive_part(A, &c);
    2138           0 :   if (!c || typ(c) == t_INT) {
    2139           0 :     n = c;
    2140           0 :     R = ZX_resultant(B, A);
    2141             :   } else {
    2142           0 :     n = gel(c,1);
    2143           0 :     R = ZX_resultant_all(B, A, gel(c,2), 0);
    2144             :   }
    2145           0 :   if (n && !equali1(n)) R = mulii(R, powiu(n, dB));
    2146           0 :   lB = leading_coeff(B);
    2147           0 :   if (!equali1(lB)) R = diviiexact(R, powiu(lB, dA));
    2148           0 :   return gerepileuptoint(av, R);
    2149             : }
    2150             : 
    2151             : GEN
    2152       18858 : QXQ_norm(GEN A, GEN B)
    2153             : {
    2154             :   GEN c, R, lB;
    2155       18858 :   long dA = degpol(A), dB = degpol(B);
    2156       18858 :   pari_sp av = avma;
    2157       18858 :   if (dA < 0) return gen_0;
    2158       18858 :   A = Q_primitive_part(A, &c);
    2159       18858 :   R = ZX_resultant(B, A);
    2160       18858 :   if (c) R = gmul(R, gpowgs(c, dB));
    2161       18858 :   lB = leading_coeff(B);
    2162       18858 :   if (!equali1(lB)) R = gdiv(R, gpowgs(lB, dA));
    2163       18858 :   return gerepileupto(av, R);
    2164             : }
    2165             : 
    2166             : /* assume x has integral coefficients */
    2167             : GEN
    2168     1193346 : ZX_disc_all(GEN x, ulong bound)
    2169             : {
    2170     1193346 :   pari_sp av = avma;
    2171     1193346 :   long s, d = degpol(x);
    2172             :   GEN l, R;
    2173             : 
    2174     1193346 :   if (d <= 1) return d == 1? gen_1: gen_0;
    2175     1190256 :   s = (d & 2) ? -1: 1;
    2176     1190256 :   l = leading_coeff(x);
    2177     1190256 :   if (!bound) bound = ZX_discbound(x);
    2178     1190211 :   R = ZX_resultant_all(x, NULL, NULL, bound);
    2179     1190248 :   if (is_pm1(l))
    2180     1014962 :   { if (signe(l) < 0) s = -s; }
    2181             :   else
    2182      175285 :     R = diviiexact(R,l);
    2183     1190247 :   if (s == -1) togglesign_safe(&R);
    2184     1190246 :   return gerepileuptoint(av,R);
    2185             : }
    2186             : 
    2187             : GEN
    2188     1131675 : ZX_disc(GEN x) { return ZX_disc_all(x,0); }
    2189             : 
    2190             : static GEN
    2191        8672 : ZXQX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, GEN T, ulong p)
    2192             : {
    2193        8672 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    2194             :   GEN H, dp;
    2195        8671 :   if (dropa && dropb) return pol0_Flx(T[1]); /* p | lc(A), p | lc(B) */
    2196        8671 :   H = FlxqX_saferesultant(a, b, T, p);
    2197        8670 :   if (!H) return NULL;
    2198        8670 :   if (dropa)
    2199             :   { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    2200           0 :     GEN c = gel(b,degB+2); /* lc(B) */
    2201           0 :     if (odd(degB)) c = Flx_neg(c, p);
    2202           0 :     c = Flxq_powu(c, dropa, T, p);
    2203           0 :     if (!Flx_equal1(c)) H = Flxq_mul(H, c, T, p);
    2204             :   }
    2205        8670 :   else if (dropb)
    2206             :   { /* multiply by lc(A)^(deg B - deg b) */
    2207           0 :     GEN c = gel(a,degA+2); /* lc(A) */
    2208           0 :     c = Flxq_powu(c, dropb, T, p);
    2209           0 :     if (!Flx_equal1(c)) H = Flxq_mul(H, c, T, p);
    2210             :   }
    2211        8670 :   dp = dB ? ZX_to_Flx(dB, p): pol1_Flx(T[1]);
    2212        8671 :   if (!Flx_equal1(dp))
    2213             :   {
    2214           0 :     GEN idp = Flxq_invsafe(dp, T, p);
    2215           0 :     if (!idp) return NULL;
    2216           0 :     H = Flxq_mul(H, Flxq_powu(idp, degA, T, p), T, p);
    2217             :   }
    2218        8671 :   return H;
    2219             : }
    2220             : 
    2221             : /* If B=NULL, assume B=A' */
    2222             : static GEN
    2223        3901 : ZXQX_resultant_slice(GEN A, GEN B, GEN U, GEN dB, GEN P, GEN *mod)
    2224             : {
    2225        3901 :   pari_sp av = avma;
    2226        3901 :   long degA, degB, i, n = lg(P)-1;
    2227             :   GEN H, T;
    2228        3901 :   long v = varn(U), redo = 0;
    2229             : 
    2230        3901 :   degA = degpol(A);
    2231        3901 :   degB = B? degpol(B): degA - 1;
    2232        3901 :   if (n == 1)
    2233             :   {
    2234        2370 :     ulong p = uel(P,1);
    2235        2370 :     GEN a = ZXX_to_FlxX(A, p, v), b = B? ZXX_to_FlxX(B, p, v): FlxX_deriv(a, p);
    2236        2370 :     GEN u = ZX_to_Flx(U, p);
    2237        2370 :     GEN Hp = ZXQX_resultant_prime(a, b, dB, degA, degB, u, p);
    2238        2370 :     if (!Hp) { set_avma(av); *mod = gen_1; return pol_0(v); }
    2239        2370 :     Hp = gerepileupto(av, Flx_to_ZX(Hp)); *mod = utoipos(p); return Hp;
    2240             :   }
    2241        1531 :   T = ZV_producttree(P);
    2242        1531 :   A = ZXX_nv_mod_tree(A, P, T, v);
    2243        1531 :   if (B) B = ZXX_nv_mod_tree(B, P, T, v);
    2244        1531 :   U = ZX_nv_mod_tree(U, P, T);
    2245        1531 :   H = cgetg(n+1, t_VEC);
    2246        7832 :   for(i=1; i <= n; i++)
    2247             :   {
    2248        6301 :     ulong p = P[i];
    2249        6301 :     GEN a = gel(A,i), b = B? gel(B,i): FlxX_deriv(a, p), u = gel(U, i);
    2250        6302 :     GEN h = ZXQX_resultant_prime(a, b, dB, degA, degB, u, p);
    2251        6301 :     if (!h)
    2252             :     {
    2253           0 :       gel(H,i) = pol_0(v);
    2254           0 :       P[i] = 1; redo = 1;
    2255             :     }
    2256             :     else
    2257        6301 :       gel(H,i) = h;
    2258             :   }
    2259        1531 :   if (redo) T = ZV_producttree(P);
    2260        1531 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2261        1531 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2262             : }
    2263             : 
    2264             : GEN
    2265        3901 : ZXQX_resultant_worker(GEN P, GEN A, GEN B, GEN T, GEN dB)
    2266             : {
    2267        3901 :   GEN V = cgetg(3, t_VEC);
    2268        3901 :   if (isintzero(B)) B = NULL;
    2269        3901 :   if (!signe(dB)) dB = NULL;
    2270        3901 :   gel(V,1) = ZXQX_resultant_slice(A, B, T, dB, P, &gel(V,2));
    2271        3901 :   return V;
    2272             : }
    2273             : 
    2274             : static ulong
    2275        3433 : ZXQX_resultant_bound_i(GEN nf, GEN A, GEN B, GEN (*f)(GEN,GEN,long))
    2276             : {
    2277        3433 :   pari_sp av = avma;
    2278        3433 :   GEN r, M = nf_L2_bound(nf, NULL, &r);
    2279        3433 :   long v = nf_get_varn(nf), i, l = lg(r);
    2280        3433 :   GEN a = cgetg(l, t_COL);
    2281       10842 :   for (i = 1; i < l; i++)
    2282        7409 :     gel(a, i) = f(gsubst(A, v, gel(r,i)), gsubst(B, v, gel(r,i)), DEFAULTPREC);
    2283        3433 :   return gc_ulong(av, (ulong) dbllog2(gmul(M,RgC_fpnorml2(a, DEFAULTPREC))));
    2284             : }
    2285             : static ulong
    2286        3125 : ZXQX_resultant_bound(GEN nf, GEN A, GEN B)
    2287        3125 : { return ZXQX_resultant_bound_i(nf, A, B, &RgX_RgXY_ResBound); }
    2288             : 
    2289             : static GEN
    2290          56 : _ZXQ_powu(GEN x, ulong u, GEN T)
    2291          56 : { return typ(x) == t_INT? powiu(x, u): ZXQ_powu(x, u, T); }
    2292             : 
    2293             : /* Compute Res(A, B/dB) in Z[X]/T, assuming A,B in Z[X,Y], dB in Z or NULL (= 1)
    2294             :  * If B=NULL, take B = A' and assume deg A > 1 */
    2295             : static GEN
    2296        3122 : ZXQX_resultant_all(GEN A, GEN B, GEN T, GEN dB, ulong bound)
    2297             : {
    2298        3122 :   pari_sp av = avma;
    2299             :   forprime_t S;
    2300             :   GEN  H, worker;
    2301        3122 :   if (B)
    2302             :   {
    2303          63 :     long a = degpol(A), b = degpol(B);
    2304          63 :     if (a < 0 || b < 0) return gen_0;
    2305          63 :     if (!a) return _ZXQ_powu(gel(A,2), b, T);
    2306          63 :     if (!b) return _ZXQ_powu(gel(B,2), a, T);
    2307             :   } else
    2308        3059 :     if (!bound) B = RgX_deriv(A);
    2309        3122 :   if (!bound) bound = ZXQX_resultant_bound(nfinit(T, DEFAULTPREC), A, B);
    2310        3122 :   worker = snm_closure(is_entry("_ZXQX_resultant_worker"),
    2311             :                        mkvec4(A, B? B: gen_0, T, dB? dB: gen_0));
    2312        3122 :   init_modular_big(&S);
    2313        3122 :   H = gen_crt("ZXQX_resultant_all", worker, &S, dB, bound, 0, NULL,
    2314             :               nxV_chinese_center, FpX_center);
    2315        3122 :   if (DEBUGLEVEL)
    2316           0 :     err_printf("ZXQX_resultant_all: a priori bound: %lu, a posteriori: %lu\n",
    2317             :                bound, expi(gsupnorm(H, DEFAULTPREC)));
    2318        3122 :   return gerepileupto(av, H);
    2319             : }
    2320             : 
    2321             : GEN
    2322         119 : nfX_resultant(GEN nf, GEN x, GEN y)
    2323             : {
    2324         119 :   pari_sp av = avma;
    2325         119 :   GEN cx, cy, D, T = nf_get_pol(nf);
    2326         119 :   long dx = degpol(x), dy = degpol(y);
    2327         119 :   if (dx < 0 || dy < 0) return gen_0;
    2328         119 :   x = Q_primitive_part(x, &cx); if (cx) cx = gpowgs(cx, dy);
    2329         119 :   y = Q_primitive_part(y, &cy); if (cy) cy = gpowgs(cy, dx);
    2330         119 :   if (!dx)      D = _ZXQ_powu(gel(x,2), dy, T);
    2331         119 :   else if (!dy) D = _ZXQ_powu(gel(y,2), dx, T);
    2332             :   else
    2333             :   {
    2334          63 :     ulong bound = ZXQX_resultant_bound(nf, x, y);
    2335          63 :     D = ZXQX_resultant_all(x, y, T, NULL, bound);
    2336             :   }
    2337         119 :   cx = mul_content(cx, cy); if (cx) D = gmul(D, cx);
    2338         119 :   return gerepileupto(av, D);
    2339             : }
    2340             : 
    2341             : static GEN
    2342         217 : to_ZX(GEN a, long v) { return typ(a)==t_INT? scalarpol(a,v): a; }
    2343             : 
    2344             : static GEN
    2345        3059 : ZXQX_disc_all(GEN x, GEN T, ulong bound)
    2346             : {
    2347        3059 :   pari_sp av = avma;
    2348        3059 :   long s, d = degpol(x), v = varn(T);
    2349             :   GEN l, R;
    2350             : 
    2351        3059 :   if (d <= 1) return d == 1? pol_1(v): pol_0(v);
    2352        3059 :   s = (d & 2) ? -1: 1;
    2353        3059 :   l = leading_coeff(x);
    2354        3059 :   R = ZXQX_resultant_all(x, NULL, T, NULL, bound);
    2355        3059 :   if (!gequal1(l)) R = QXQ_div(R, to_ZX(l,v), T);
    2356        3059 :   if (s == -1) R = RgX_neg(R);
    2357        3059 :   return gerepileupto(av, R);
    2358             : }
    2359             : 
    2360             : GEN
    2361           7 : QX_disc(GEN x)
    2362             : {
    2363           7 :   pari_sp av = avma;
    2364           7 :   GEN c, d = ZX_disc( Q_primitive_part(x, &c) );
    2365           7 :   if (c) d = gmul(d, gpowgs(c, 2*degpol(x) - 2));
    2366           7 :   return gerepileupto(av, d);
    2367             : }
    2368             : 
    2369             : GEN
    2370        3220 : nfX_disc(GEN nf, GEN x)
    2371             : {
    2372        3220 :   pari_sp av = avma;
    2373        3220 :   GEN c, D, T = nf_get_pol(nf);
    2374             :   ulong bound;
    2375        3220 :   long d = degpol(x), v = varn(T);
    2376        3220 :   if (d <= 1) return d == 1? pol_1(v): pol_0(v);
    2377        3059 :   x = Q_primitive_part(x, &c);
    2378        3059 :   bound = ZXQX_resultant_bound(nf, x, RgX_deriv(x));
    2379        3059 :   D = ZXQX_disc_all(x, T, bound);
    2380        3059 :   if (c) D = gmul(D, gpowgs(c, 2*d - 2));
    2381        3059 :   return gerepileupto(av, D);
    2382             : }
    2383             : 
    2384             : GEN
    2385      829113 : QXQ_mul(GEN x, GEN y, GEN T)
    2386             : {
    2387      829113 :   GEN dx, nx = Q_primitive_part(x, &dx);
    2388      829113 :   GEN dy, ny = Q_primitive_part(y, &dy);
    2389      829112 :   GEN z = ZXQ_mul(nx, ny, T);
    2390      829113 :   if (dx || dy)
    2391             :   {
    2392      826313 :     GEN d = dx ? dy ? gmul(dx, dy): dx : dy;
    2393      826313 :     if (!gequal1(d)) z = ZX_Q_mul(z, d);
    2394             :   }
    2395      829113 :   return z;
    2396             : }
    2397             : 
    2398             : GEN
    2399      398428 : QXQ_sqr(GEN x, GEN T)
    2400             : {
    2401      398428 :   GEN dx, nx = Q_primitive_part(x, &dx);
    2402      398428 :   GEN z = ZXQ_sqr(nx, T);
    2403      398428 :   if (dx)
    2404      396692 :     z = ZX_Q_mul(z, gsqr(dx));
    2405      398428 :   return z;
    2406             : }
    2407             : 
    2408             : static GEN
    2409      210247 : QXQ_inv_slice(GEN A, GEN B, GEN P, GEN *mod)
    2410             : {
    2411      210247 :   pari_sp av = avma;
    2412      210247 :   long i, n = lg(P)-1, v = varn(A), redo = 0;
    2413             :   GEN H, T;
    2414      210247 :   if (n == 1)
    2415             :   {
    2416      164546 :     ulong p = uel(P,1);
    2417      164546 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    2418      164546 :     GEN U = Flxq_invsafe(a, b, p);
    2419      164547 :     if (!U)
    2420             :     {
    2421          24 :       set_avma(av);
    2422          24 :       *mod = gen_1; return pol_0(v);
    2423             :     }
    2424      164523 :     H = gerepilecopy(av, Flx_to_ZX(U));
    2425      164523 :     *mod = utoipos(p); return H;
    2426             :   }
    2427       45701 :   T = ZV_producttree(P);
    2428       45702 :   A = ZX_nv_mod_tree(A, P, T);
    2429       45702 :   B = ZX_nv_mod_tree(B, P, T);
    2430       45702 :   H = cgetg(n+1, t_VEC);
    2431      226302 :   for(i=1; i <= n; i++)
    2432             :   {
    2433      180600 :     ulong p = P[i];
    2434      180600 :     GEN a = gel(A,i), b = gel(B,i);
    2435      180600 :     GEN U = Flxq_invsafe(a, b, p);
    2436      180605 :     if (!U)
    2437             :     {
    2438         601 :       gel(H,i) = pol_0(v);
    2439         601 :       P[i] = 1; redo = 1;
    2440             :     }
    2441             :     else
    2442      180004 :       gel(H,i) = U;
    2443             :   }
    2444       45702 :   if (redo) T = ZV_producttree(P);
    2445       45702 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2446       45702 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2447             : }
    2448             : 
    2449             : GEN
    2450      210247 : QXQ_inv_worker(GEN P, GEN A, GEN B)
    2451             : {
    2452      210247 :   GEN V = cgetg(3, t_VEC);
    2453      210247 :   gel(V,1) = QXQ_inv_slice(A, B, P, &gel(V,2));
    2454      210249 :   return V;
    2455             : }
    2456             : 
    2457             : /* lift(1 / Mod(A,B)). B a ZX, A a scalar or a QX */
    2458             : GEN
    2459      145200 : QXQ_inv(GEN A, GEN B)
    2460             : {
    2461             :   GEN D, Ap, Bp;
    2462             :   ulong pp;
    2463      145200 :   pari_sp av2, av = avma;
    2464             :   forprime_t S;
    2465      145200 :   GEN worker, U, H = NULL, mod = gen_1;
    2466             :   pari_timer ti;
    2467             :   long k, dA, dB;
    2468      145200 :   if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
    2469             :   /* A a QX, B a ZX */
    2470      145200 :   A = Q_primitive_part(A, &D);
    2471      145200 :   dA = degpol(A); dB= degpol(B);
    2472             :   /* A, B in Z[X] */
    2473      145200 :   init_modular_small(&S);
    2474             :   do {
    2475      145200 :     pp = u_forprime_next(&S);
    2476      145200 :     Ap = ZX_to_Flx(A, pp);
    2477      145200 :     Bp = ZX_to_Flx(B, pp);
    2478      145200 :   } while (degpol(Ap) != dA || degpol(Bp) != dB);
    2479      145200 :   if (degpol(Flx_gcd(Ap, Bp, pp)) != 0 && degpol(ZX_gcd(A,B))!=0)
    2480          14 :     pari_err_INV("QXQ_inv",mkpolmod(A,B));
    2481      145186 :   worker = snm_closure(is_entry("_QXQ_inv_worker"), mkvec2(A, B));
    2482      145186 :   av2 = avma;
    2483      145186 :   for (k = 1; ;k *= 2)
    2484       41748 :   {
    2485             :     GEN res, b, N, den;
    2486      186934 :     gen_inccrt_i("QXQ_inv", worker, NULL, (k+1)>>1, 0, &S, &H, &mod,
    2487             :                  nxV_chinese_center, FpX_center);
    2488      186934 :     gerepileall(av2, 2, &H, &mod);
    2489      186934 :     b = sqrti(shifti(mod,-1));
    2490      186934 :     if (DEBUGLEVEL>5) timer_start(&ti);
    2491      186934 :     U = FpX_ratlift(H, mod, b, b, NULL);
    2492      186934 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: ratlift");
    2493      192622 :     if (!U) continue;
    2494      150874 :     N = Q_remove_denom(U, &den); if (!den) den = gen_1;
    2495      150874 :     res = Flx_rem(Flx_Fl_sub(Flx_mul(Ap, ZX_to_Flx(N,pp), pp),
    2496             :                   umodiu(den, pp), pp), Bp, pp);
    2497      150874 :     if (degpol(res) >= 0) continue;
    2498      145186 :     res = ZX_Z_sub(ZX_mul(A, N), den);
    2499      145186 :     res = ZX_is_monic(B) ? ZX_rem(res, B): RgX_pseudorem(res, B);
    2500      145186 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: final check");
    2501      145186 :     if (degpol(res)<0)
    2502             :     {
    2503      145186 :       if (D) U = RgX_Rg_div(U, D);
    2504      145186 :       return gerepilecopy(av, U);
    2505             :     }
    2506             :   }
    2507             : }
    2508             : 
    2509             : static GEN
    2510      117332 : QXQ_div_slice(GEN A, GEN B, GEN C, GEN P, GEN *mod)
    2511             : {
    2512      117332 :   pari_sp av = avma;
    2513      117332 :   long i, n = lg(P)-1, v = varn(A), redo = 0;
    2514             :   GEN H, T;
    2515      117332 :   if (n == 1)
    2516             :   {
    2517       42956 :     ulong p = uel(P,1);
    2518       42956 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p), c = ZX_to_Flx(C, p);
    2519       42956 :     GEN bi = Flxq_invsafe(b, c, p), U;
    2520       42956 :     if (!bi)
    2521             :     {
    2522           0 :       set_avma(av);
    2523           0 :       *mod = gen_1; return pol_0(v);
    2524             :     }
    2525       42956 :     U = Flxq_mul(a, bi, c, p);
    2526       42956 :     H = gerepilecopy(av, Flx_to_ZX(U));
    2527       42956 :     *mod = utoipos(p); return H;
    2528             :   }
    2529       74376 :   T = ZV_producttree(P);
    2530       74376 :   A = ZX_nv_mod_tree(A, P, T);
    2531       74376 :   B = ZX_nv_mod_tree(B, P, T);
    2532       74376 :   C = ZX_nv_mod_tree(C, P, T);
    2533       74376 :   H = cgetg(n+1, t_VEC);
    2534      327243 :   for(i=1; i <= n; i++)
    2535             :   {
    2536      252867 :     ulong p = P[i];
    2537      252867 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C, i);
    2538      252867 :     GEN bi = Flxq_invsafe(b, c, p);
    2539      252865 :     if (!bi)
    2540             :     {
    2541           0 :       gel(H,i) = pol_0(v);
    2542           0 :       P[i] = 1; redo = 1;
    2543             :     }
    2544             :     else
    2545      252865 :       gel(H,i) = Flxq_mul(a, bi, c, p);
    2546             :   }
    2547       74376 :   if (redo) T = ZV_producttree(P);
    2548       74376 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2549       74376 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2550             : }
    2551             : 
    2552             : GEN
    2553      117332 : QXQ_div_worker(GEN P, GEN A, GEN B, GEN C)
    2554             : {
    2555      117332 :   GEN V = cgetg(3, t_VEC);
    2556      117332 :   gel(V,1) = QXQ_div_slice(A, B, C, P, &gel(V,2));
    2557      117332 :   return V;
    2558             : }
    2559             : 
    2560             : /* lift(Mod(A/B, C)). C a ZX, A, B a scalar or a QX */
    2561             : GEN
    2562       31800 : QXQ_div(GEN A, GEN B, GEN C)
    2563             : {
    2564             :   GEN DA, DB, Ap, Bp, Cp;
    2565             :   ulong pp;
    2566       31800 :   pari_sp av2, av = avma;
    2567             :   forprime_t S;
    2568       31800 :   GEN worker, U, H = NULL, mod = gen_1;
    2569             :   pari_timer ti;
    2570             :   long k, dA, dB, dC;
    2571       31800 :   if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
    2572             :   /* A a QX, B a ZX */
    2573       31800 :   A = Q_primitive_part(A, &DA);
    2574       31800 :   B = Q_primitive_part(B, &DB);
    2575       31800 :   dA = degpol(A); dB = degpol(B); dC = degpol(C);
    2576             :   /* A, B in Z[X] */
    2577       31800 :   init_modular_small(&S);
    2578             :   do {
    2579       31800 :     pp = u_forprime_next(&S);
    2580       31800 :     Ap = ZX_to_Flx(A, pp);
    2581       31800 :     Bp = ZX_to_Flx(B, pp);
    2582       31800 :     Cp = ZX_to_Flx(C, pp);
    2583       31800 :   } while (degpol(Ap) != dA || degpol(Bp) != dB || degpol(Cp) != dC);
    2584       31800 :   if (degpol(Flx_gcd(Bp, Cp, pp)) != 0 && degpol(ZX_gcd(B,C))!=0)
    2585           0 :     pari_err_INV("QXQ_div",mkpolmod(B,C));
    2586       31800 :   worker = snm_closure(is_entry("_QXQ_div_worker"), mkvec3(A, B, C));
    2587       31800 :   av2 = avma;
    2588       31800 :   for (k = 1; ;k *= 2)
    2589       45518 :   {
    2590             :     GEN res, b, N, den;
    2591       77318 :     gen_inccrt_i("QXQ_div", worker, NULL, (k+1)>>1, 0, &S, &H, &mod,
    2592             :                  nxV_chinese_center, FpX_center);
    2593       77318 :     gerepileall(av2, 2, &H, &mod);
    2594       77318 :     b = sqrti(shifti(mod,-1));
    2595       77318 :     if (DEBUGLEVEL>5) timer_start(&ti);
    2596       77318 :     U = FpX_ratlift(H, mod, b, b, NULL);
    2597       77317 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_div: ratlift");
    2598       87728 :     if (!U) continue;
    2599       42210 :     N = Q_remove_denom(U, &den); if (!den) den = gen_1;
    2600       42211 :     res = Flx_rem(Flx_sub(Flx_mul(Bp, ZX_to_Flx(N,pp), pp),
    2601             :                           Flx_Fl_mul(Ap, umodiu(den, pp), pp), pp), Cp, pp);
    2602       42211 :     if (degpol(res) >= 0) continue;
    2603       31800 :     res = ZX_sub(ZX_mul(B, N), ZX_Z_mul(A,den));
    2604       31800 :     res = ZX_is_monic(C) ? ZX_rem(res, C): RgX_pseudorem(res, C);
    2605       31800 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_div: final check");
    2606       31800 :     if (degpol(res)<0)
    2607             :     {
    2608       31800 :       if (DA && DB) U = RgX_Rg_mul(U, gdiv(DA,DB));
    2609       26998 :       else if (DA) U = RgX_Rg_mul(U, DA);
    2610       15204 :       else if (DB) U = RgX_Rg_div(U, DB);
    2611       31800 :       return gerepilecopy(av, U);
    2612             :     }
    2613             :   }
    2614             : }
    2615             : 
    2616             : /************************************************************************
    2617             :  *                                                                      *
    2618             :  *                           ZXQ_minpoly                                *
    2619             :  *                                                                      *
    2620             :  ************************************************************************/
    2621             : 
    2622             : static GEN
    2623        3523 : ZXQ_minpoly_slice(GEN A, GEN B, long d, GEN P, GEN *mod)
    2624             : {
    2625        3523 :   pari_sp av = avma;
    2626        3523 :   long i, n = lg(P)-1, v = evalvarn(varn(B));
    2627             :   GEN H, T;
    2628        3523 :   if (n == 1)
    2629             :   {
    2630         716 :     ulong p = uel(P,1);
    2631         716 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    2632         716 :     GEN Hp = Flxq_minpoly(a, b, p);
    2633         716 :     if (degpol(Hp) != d) { p = 1; Hp = pol0_Flx(v); }
    2634         716 :     H = gerepileupto(av, Flx_to_ZX(Hp));
    2635         716 :     *mod = utoipos(p); return H;
    2636             :   }
    2637        2807 :   T = ZV_producttree(P);
    2638        2807 :   A = ZX_nv_mod_tree(A, P, T);
    2639        2807 :   B = ZX_nv_mod_tree(B, P, T);
    2640        2807 :   H = cgetg(n+1, t_VEC);
    2641       16838 :   for(i=1; i <= n; i++)
    2642             :   {
    2643       14031 :     ulong p = P[i];
    2644       14031 :     GEN a = gel(A,i), b = gel(B,i);
    2645       14031 :     GEN m = Flxq_minpoly(a, b, p);
    2646       14031 :     if (degpol(m) != d) { P[i] = 1; m = pol0_Flx(v); }
    2647       14031 :     gel(H, i) = m;
    2648             :   }
    2649        2807 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2650        2807 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2651             : }
    2652             : 
    2653             : GEN
    2654        3523 : ZXQ_minpoly_worker(GEN P, GEN A, GEN B, long d)
    2655             : {
    2656        3523 :   GEN V = cgetg(3, t_VEC);
    2657        3523 :   gel(V,1) = ZXQ_minpoly_slice(A, B, d, P, &gel(V,2));
    2658        3523 :   return V;
    2659             : }
    2660             : 
    2661             : GEN
    2662        1701 : ZXQ_minpoly(GEN A, GEN B, long d, ulong bound)
    2663             : {
    2664        1701 :   pari_sp av = avma;
    2665             :   GEN worker, H, dB;
    2666             :   forprime_t S;
    2667        1701 :   B = Q_remove_denom(B, &dB);
    2668        1701 :   worker = strtoclosure("_ZXQ_minpoly_worker", 3, A, B, stoi(d));
    2669        1701 :   init_modular_big(&S);
    2670        1701 :   H = gen_crt("ZXQ_minpoly", worker, &S, dB, bound, 0, NULL,
    2671             :                nxV_chinese_center, FpX_center_i);
    2672        1701 :   return gerepilecopy(av, H);
    2673             : }
    2674             : 
    2675             : /************************************************************************
    2676             :  *                                                                      *
    2677             :  *                   ZX_ZXY_resultant                                   *
    2678             :  *                                                                      *
    2679             :  ************************************************************************/
    2680             : 
    2681             : static GEN
    2682      173713 : ZX_ZXY_resultant_prime(GEN a, GEN b, ulong dp, ulong p,
    2683             :                        long degA, long degB, long dres, long sX)
    2684             : {
    2685      173713 :   long dropa = degA - degpol(a), dropb = degB - degpol(b);
    2686      173713 :   ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
    2687      173713 :   GEN Hp = Flx_FlxY_resultant_polint(a, b, p, pi, dres, sX);
    2688      173716 :   if (dropa && dropb)
    2689           0 :     Hp = zero_Flx(sX);
    2690             :   else {
    2691      173716 :     if (dropa)
    2692             :     { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    2693           0 :       GEN c = gel(b,degB+2); /* lc(B) */
    2694           0 :       if (odd(degB)) c = Flx_neg(c, p);
    2695           0 :       if (!Flx_equal1(c)) {
    2696           0 :         c = Flx_powu_pre(c, dropa, p, pi);
    2697           0 :         if (!Flx_equal1(c)) Hp = Flx_mul_pre(Hp, c, p, pi);
    2698             :       }
    2699             :     }
    2700      173716 :     else if (dropb)
    2701             :     { /* multiply by lc(A)^(deg B - deg b) */
    2702           0 :       ulong c = uel(a, degA+2); /* lc(A) */
    2703           0 :       c = Fl_powu(c, dropb, p);
    2704           0 :       if (c != 1) Hp = Flx_Fl_mul_pre(Hp, c, p, pi);
    2705             :     }
    2706             :   }
    2707      173716 :   if (dp != 1) Hp = Flx_Fl_mul_pre(Hp, Fl_powu_pre(Fl_inv(dp,p), degA, p, pi), p, pi);
    2708      173716 :   return Hp;
    2709             : }
    2710             : 
    2711             : static GEN
    2712       69386 : ZX_ZXY_resultant_slice(GEN A, GEN B, GEN dB, long degA, long degB, long dres,
    2713             :                        GEN P, GEN *mod, long sX, long vY)
    2714             : {
    2715       69386 :   pari_sp av = avma;
    2716       69386 :   long i, n = lg(P)-1;
    2717             :   GEN H, T, D;
    2718       69386 :   if (n == 1)
    2719             :   {
    2720       40106 :     ulong p = uel(P,1);
    2721       40106 :     ulong dp = dB ? umodiu(dB, p): 1;
    2722       40106 :     GEN a = ZX_to_Flx(A, p), b = ZXX_to_FlxX(B, p, vY);
    2723       40105 :     GEN Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2724       40105 :     H = gerepileupto(av, Flx_to_ZX(Hp));
    2725       40105 :     *mod = utoipos(p); return H;
    2726             :   }
    2727       29280 :   T = ZV_producttree(P);
    2728       29280 :   A = ZX_nv_mod_tree(A, P, T);
    2729       29280 :   B = ZXX_nv_mod_tree(B, P, T, vY);
    2730       29280 :   D = dB ? Z_ZV_mod_tree(dB, P, T): NULL;
    2731       29280 :   H = cgetg(n+1, t_VEC);
    2732      117571 :   for(i=1; i <= n; i++)
    2733             :   {
    2734       88290 :     ulong p = P[i];
    2735       88290 :     GEN a = gel(A,i), b = gel(B,i);
    2736       88290 :     ulong dp = D ? uel(D, i): 1;
    2737       88290 :     gel(H,i) = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2738             :   }
    2739       29281 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2740       29280 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2741             : }
    2742             : 
    2743             : GEN
    2744       69386 : ZX_ZXY_resultant_worker(GEN P, GEN A, GEN B, GEN dB, GEN v)
    2745             : {
    2746       69386 :   GEN V = cgetg(3, t_VEC);
    2747       69386 :   if (isintzero(dB)) dB = NULL;
    2748       69386 :   gel(V,1) = ZX_ZXY_resultant_slice(A, B, dB, v[1], v[2], v[3], P, &gel(V,2), v[4], v[5]);
    2749       69385 :   return V;
    2750             : }
    2751             : 
    2752             : GEN
    2753       60087 : ZX_ZXY_resultant(GEN A, GEN B)
    2754             : {
    2755       60087 :   pari_sp av = avma;
    2756             :   forprime_t S;
    2757             :   ulong bound;
    2758       60087 :   long v = fetch_var_higher();
    2759       60087 :   long degA = degpol(A), degB, dres = degA * degpol(B);
    2760       60087 :   long vX = varn(B), vY = varn(A); /* assume vY has lower priority */
    2761       60087 :   long sX = evalvarn(vX);
    2762             :   GEN worker, H, dB;
    2763       60087 :   B = Q_remove_denom(B, &dB);
    2764       60087 :   if (!dB) B = leafcopy(B);
    2765       60087 :   A = leafcopy(A); setvarn(A,v);
    2766       60087 :   B = swap_vars(B, vY); setvarn(B,v); degB = degpol(B);
    2767       60087 :   bound = ZX_ZXY_ResBound(A, B, dB);
    2768       60086 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    2769      120172 :   worker = snm_closure(is_entry("_ZX_ZXY_resultant_worker"),
    2770       60086 :                        mkvec4(A, B, dB? dB: gen_0,
    2771             :                               mkvecsmall5(degA, degB, dres, sX, vY)));
    2772       60087 :   init_modular_big(&S);
    2773       60087 :   H = gen_crt("ZX_ZXY_resultant_all", worker, &S, dB, bound, 0, NULL,
    2774             :                nxV_chinese_center, FpX_center_i);
    2775       60086 :   setvarn(H, vX); (void)delete_var();
    2776       60086 :   return gerepilecopy(av, H);
    2777             : }
    2778             : 
    2779             : static long
    2780       40516 : ZX_ZXY_rnfequation_lambda(GEN A, GEN B0, long lambda)
    2781             : {
    2782       40516 :   pari_sp av = avma;
    2783       40516 :   long degA = degpol(A), degB, dres = degA*degpol(B0);
    2784       40516 :   long v = fetch_var_higher();
    2785       40516 :   long vX = varn(B0), vY = varn(A); /* assume vY has lower priority */
    2786       40516 :   long sX = evalvarn(vX);
    2787             :   GEN dB, B, a, b, Hp;
    2788             :   forprime_t S;
    2789             : 
    2790       40516 :   B0 = Q_remove_denom(B0, &dB);
    2791       40516 :   if (!dB) B0 = leafcopy(B0);
    2792       40516 :   A = leafcopy(A);
    2793       40516 :   B = B0;
    2794       40516 :   setvarn(A,v);
    2795       45321 : INIT:
    2796       45321 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    2797       45321 :   B = swap_vars(B, vY); setvarn(B,v);
    2798             :   /* B0(lambda v + x, v) */
    2799       45321 :   if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    2800             : 
    2801       45321 :   degB = degpol(B);
    2802       45321 :   init_modular_big(&S);
    2803             :   while (1)
    2804           0 :   {
    2805       45320 :     ulong p = u_forprime_next(&S);
    2806       45320 :     ulong dp = dB ? umodiu(dB, p): 1;
    2807       45320 :     if (!dp) continue;
    2808       45320 :     a = ZX_to_Flx(A, p);
    2809       45320 :     b = ZXX_to_FlxX(B, p, v);
    2810       45320 :     Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
    2811       45320 :     if (degpol(Hp) != dres) continue;
    2812       45320 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    2813       45320 :     if (!Flx_is_squarefree(Hp, p)) { lambda = next_lambda(lambda); goto INIT; }
    2814       40516 :     if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    2815       40516 :     (void)delete_var(); return gc_long(av,lambda);
    2816             :   }
    2817             : }
    2818             : 
    2819             : GEN
    2820       41453 : ZX_ZXY_rnfequation(GEN A, GEN B, long *lambda)
    2821             : {
    2822       41453 :   if (lambda)
    2823             :   {
    2824       40516 :     *lambda = ZX_ZXY_rnfequation_lambda(A, B, *lambda);
    2825       40516 :     if (*lambda) B = RgX_translate(B, monomial(stoi(*lambda), 1, varn(A)));
    2826             :   }
    2827       41453 :   return ZX_ZXY_resultant(A,B);
    2828             : }
    2829             : 
    2830             : static GEN
    2831       10371 : ZX_composedsum_slice(GEN A, GEN B, GEN P, GEN *mod)
    2832             : {
    2833       10371 :   pari_sp av = avma;
    2834       10371 :   long i, n = lg(P)-1;
    2835             :   GEN H, T;
    2836       10371 :   if (n == 1)
    2837             :   {
    2838        9869 :     ulong p = uel(P,1);
    2839        9869 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    2840        9868 :     GEN Hp = Flx_composedsum(a, b, p);
    2841        9868 :     H = gerepileupto(av, Flx_to_ZX(Hp));
    2842        9870 :     *mod = utoipos(p); return H;
    2843             :   }
    2844         502 :   T = ZV_producttree(P);
    2845         502 :   A = ZX_nv_mod_tree(A, P, T);
    2846         502 :   B = ZX_nv_mod_tree(B, P, T);
    2847         502 :   H = cgetg(n+1, t_VEC);
    2848        4526 :   for(i=1; i <= n; i++)
    2849             :   {
    2850        4024 :     ulong p = P[i];
    2851        4024 :     GEN a = gel(A,i), b = gel(B,i);
    2852        4024 :     gel(H,i) = Flx_composedsum(a, b, p);
    2853             :   }
    2854         502 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    2855         502 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2856             : }
    2857             : 
    2858             : GEN
    2859       10371 : ZX_composedsum_worker(GEN P, GEN A, GEN B)
    2860             : {
    2861       10371 :   GEN V = cgetg(3, t_VEC);
    2862       10371 :   gel(V,1) = ZX_composedsum_slice(A, B, P, &gel(V,2));
    2863       10372 :   return V;
    2864             : }
    2865             : 
    2866             : static GEN
    2867       10107 : ZX_composedsum_i(GEN A, GEN B, GEN lead)
    2868             : {
    2869       10107 :   pari_sp av = avma;
    2870             :   forprime_t S;
    2871             :   ulong bound;
    2872             :   GEN H, worker, mod;
    2873       10107 :   if (degpol(A) < degpol(B)) swap(A, B);
    2874       10107 :   if (!lead) lead  = mulii(leading_coeff(A),leading_coeff(B));
    2875       10107 :   bound = ZX_ZXY_ResBound_1(A, B);
    2876       10107 :   worker = snm_closure(is_entry("_ZX_composedsum_worker"), mkvec2(A,B));
    2877       10107 :   init_modular_big(&S);
    2878       10106 :   H = gen_crt("ZX_composedsum", worker, &S, lead, bound, 0, &mod,
    2879             :               nxV_chinese_center, FpX_center);
    2880       10105 :   return gerepileupto(av, H);
    2881             : }
    2882             : 
    2883             : static long
    2884        9719 : ZX_compositum_lambda(GEN A, GEN B, GEN lead, long lambda)
    2885             : {
    2886        9719 :   pari_sp av = avma;
    2887             :   forprime_t S;
    2888             :   ulong p;
    2889        9719 :   init_modular_big(&S);
    2890        9718 :   p = u_forprime_next(&S);
    2891             :   while (1)
    2892         112 :   {
    2893             :     GEN Hp, a;
    2894        9830 :     if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    2895        9830 :     if (lead && dvdiu(lead,p)) { p = u_forprime_next(&S); continue; }
    2896        9823 :     a = ZX_to_Flx(ZX_rescale(A, stoi(-lambda)), p);
    2897        9825 :     Hp = Flx_composedsum(a, ZX_to_Flx(B, p), p);
    2898        9825 :     if (!Flx_is_squarefree(Hp, p)) { lambda = next_lambda(lambda); continue; }
    2899        9717 :     if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    2900        9717 :     return gc_long(av, lambda);
    2901             :   }
    2902             : }
    2903             : 
    2904             : GEN
    2905        9720 : ZX_compositum(GEN A, GEN B, long *lambda)
    2906             : {
    2907        9720 :   GEN lead  = mulii(leading_coeff(A),leading_coeff(B));
    2908        9719 :   if (lambda)
    2909             :   {
    2910        9719 :     *lambda = ZX_compositum_lambda(A, B, lead, *lambda);
    2911        9717 :     A = ZX_rescale(A, stoi(-*lambda));
    2912             :   }
    2913        9722 :   return ZX_composedsum_i(A, B, lead);
    2914             : }
    2915             : 
    2916             : GEN
    2917         385 : ZX_composedsum(GEN A, GEN B)
    2918         385 : { return ZX_composedsum_i(A, B, NULL); }
    2919             : 
    2920             : static GEN
    2921         352 : ZXQX_composedsum_slice(GEN A, GEN B, GEN C, GEN P, GEN *mod)
    2922             : {
    2923         352 :   pari_sp av = avma;
    2924         352 :   long i, n = lg(P)-1, dC = degpol(C), v = varn(C);
    2925             :   GEN H, T;
    2926         352 :   if (n == 1)
    2927             :   {
    2928         174 :     ulong p = uel(P,1);
    2929         174 :     GEN a = ZXX_to_FlxX(A, p, v), b = ZXX_to_FlxX(B, p, v);
    2930         174 :     GEN c = ZX_to_Flx(C, p);
    2931         174 :     GEN Hp = FlxX_to_Flm(FlxqX_composedsum(a, b, c, p), dC);
    2932         174 :     H = gerepileupto(av, Flm_to_ZM(Hp));
    2933         174 :     *mod = utoipos(p); return H;
    2934             :   }
    2935         178 :   T = ZV_producttree(P);
    2936         178 :   A = ZXX_nv_mod_tree(A, P, T, v);
    2937         178 :   B = ZXX_nv_mod_tree(B, P, T, v);
    2938         178 :   C = ZX_nv_mod_tree(C, P, T);
    2939         178 :   H = cgetg(n+1, t_VEC);
    2940         660 :   for(i=1; i <= n; i++)
    2941             :   {
    2942         482 :     ulong p = P[i];
    2943         482 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C,i);
    2944         482 :     gel(H,i) = FlxX_to_Flm(FlxqX_composedsum(a, b, c, p), dC);
    2945             :   }
    2946         178 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
    2947         178 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2948             : }
    2949             : 
    2950             : GEN
    2951         352 : ZXQX_composedsum_worker(GEN P, GEN A, GEN B, GEN C)
    2952             : {
    2953         352 :   GEN V = cgetg(3, t_VEC);
    2954         352 :   gel(V,1) = ZXQX_composedsum_slice(A, B, C, P, &gel(V,2));
    2955         352 :   return V;
    2956             : }
    2957             : 
    2958             : static GEN
    2959         308 : ZXQX_composedsum(GEN A, GEN B, GEN T, ulong bound)
    2960             : {
    2961         308 :   pari_sp av = avma;
    2962             :   forprime_t S;
    2963             :   GEN H, worker, mod;
    2964         308 :   GEN lead = mulii(Q_content(leading_coeff(A)), Q_content(leading_coeff(B)));
    2965         308 :   worker = snm_closure(is_entry("_ZXQX_composedsum_worker")
    2966             :                       , mkvec3(A,B,T));
    2967         308 :   init_modular_big(&S);
    2968         308 :   H = gen_crt("ZXQX_composedsum", worker, &S, lead, bound, 0, &mod,
    2969             :               nmV_chinese_center, FpM_center);
    2970         308 :   if (DEBUGLEVEL > 4)
    2971           0 :     err_printf("nfcompositum: a priori bound: %lu, a posteriori: %lu\n",
    2972             :                bound, expi(gsupnorm(H, DEFAULTPREC)));
    2973         308 :   return gerepilecopy(av, RgM_to_RgXX(H, varn(A), varn(T)));
    2974             : }
    2975             : 
    2976             : static long
    2977         308 : ZXQX_composedsum_bound(GEN nf, GEN A, GEN B)
    2978         308 : { return ZXQX_resultant_bound_i(nf, A, B, &RgX_RgXY_ResBound_1); }
    2979             : 
    2980             : GEN
    2981         308 : nf_direct_compositum(GEN nf, GEN A, GEN B)
    2982             : {
    2983         308 :   ulong bnd = ZXQX_composedsum_bound(nf, A, B);
    2984         308 :   return ZXQX_composedsum(A, B, nf_get_pol(nf), bnd);
    2985             : }
    2986             : 
    2987             : /************************************************************************
    2988             :  *                                                                      *
    2989             :  *                   IRREDUCIBLE POLYNOMIAL / Fp                        *
    2990             :  *                                                                      *
    2991             :  ************************************************************************/
    2992             : 
    2993             : /* irreducible (unitary) polynomial of degree n over Fp */
    2994             : GEN
    2995           0 : ffinit_rand(GEN p,long n)
    2996             : {
    2997           0 :   for(;;) {
    2998           0 :     pari_sp av = avma;
    2999           0 :     GEN pol = ZX_add(pol_xn(n, 0), random_FpX(n-1,0, p));
    3000           0 :     if (FpX_is_irred(pol, p)) return pol;
    3001           0 :     set_avma(av);
    3002             :   }
    3003             : }
    3004             : 
    3005             : /* return an extension of degree 2^l of F_2, assume l > 0
    3006             :  * Not stack clean. */
    3007             : static GEN
    3008         605 : ffinit_Artin_Schreier_2(long l)
    3009             : {
    3010             :   GEN Q, T, S;
    3011             :   long i, v;
    3012             : 
    3013         605 :   if (l == 1) return mkvecsmall4(0,1,1,1); /*x^2 + x + 1*/
    3014         556 :   v = fetch_var_higher();
    3015         556 :   S = mkvecsmall5(0, 0, 0, 1, 1); /* y(y^2 + y) */
    3016         556 :   Q = mkpoln(3, pol1_Flx(0), pol1_Flx(0), S); /* x^2 + x + y(y^2+y) */
    3017         556 :   setvarn(Q, v);
    3018             : 
    3019             :   /* x^4+x+1, irred over F_2, minimal polynomial of a root of Q */
    3020         556 :   T = mkvecsmalln(6,evalvarn(v),1UL,1UL,0UL,0UL,1UL);
    3021             :   /* Q = x^2 + x + a(y) irred. over K = F2[y] / (T(y))
    3022             :    * ==> x^2 + x + a(y) b irred. over K for any root b of Q
    3023             :    * ==> x^2 + x + (b^2+b)b */
    3024        3070 :   for (i=2; i<l; i++) T = Flx_FlxY_resultant(T, Q, 2); /* minpoly(b) / F2*/
    3025         556 :   (void)delete_var(); T[1] = 0; return T;
    3026             : }
    3027             : 
    3028             : /* return an extension of degree p^l of F_p, assume l > 0
    3029             :  * Not stack clean. */
    3030             : GEN
    3031         962 : ffinit_Artin_Schreier(ulong p, long l)
    3032             : {
    3033             :   long i, v;
    3034             :   GEN Q, R, S, T, xp;
    3035         962 :   if (p==2) return ffinit_Artin_Schreier_2(l);
    3036         357 :   xp = polxn_Flx(p,0); /* x^p */
    3037         357 :   T = Flx_sub(xp, mkvecsmall3(0,1,1),p); /* x^p - x - 1 */
    3038         357 :   if (l == 1) return T;
    3039             : 
    3040           7 :   v = evalvarn(fetch_var_higher());
    3041           7 :   xp[1] = v;
    3042           7 :   R = Flx_sub(polxn_Flx(2*p-1,0), polxn_Flx(p,0),p);
    3043           7 :   S = Flx_sub(xp, polx_Flx(0), p);
    3044           7 :   Q = FlxX_Flx_sub(Flx_to_FlxX(S, v), R, p); /* x^p - x - (y^(2p-1)-y^p) */
    3045          14 :   for (i = 2; i <= l; ++i) T = Flx_FlxY_resultant(T, Q, p);
    3046           7 :   (void)delete_var(); T[1] = 0; return T;
    3047             : }
    3048             : 
    3049             : static long
    3050      147917 : flinit_check(ulong p, long n, long l)
    3051             : {
    3052             :   ulong q;
    3053      147917 :   if (!uisprime(n)) return 0;
    3054      101208 :   q = p % n; if (!q) return 0;
    3055       98730 :   return ugcd((n-1)/Fl_order(q, n-1, n), l) == 1;
    3056             : }
    3057             : 
    3058             : static GEN
    3059       31727 : flinit(ulong p, long l)
    3060             : {
    3061       31727 :   ulong n = 1+l;
    3062       95809 :   while (!flinit_check(p,n,l)) n += l;
    3063       31727 :   if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
    3064       31727 :   return ZX_to_Flx(polsubcyclo(n,l,0), p);
    3065             : }
    3066             : 
    3067             : static GEN
    3068       28863 : ffinit_fact_Flx(ulong p, long n)
    3069             : {
    3070       28863 :   GEN P, F = factoru_pow(n), Fp = gel(F,1), Fe = gel(F,2), Fm = gel(F,3);
    3071       28863 :   long i, l = lg(Fm);
    3072       28863 :   P = cgetg(l, t_VEC);
    3073       61552 :   for (i = 1; i < l; i++)
    3074       32689 :     gel(P,i) = p==uel(Fp,i) ? ffinit_Artin_Schreier(p, Fe[i])
    3075       32689 :                             : flinit(p, uel(Fm,i));
    3076       28863 :   return FlxV_composedsum(P, p);
    3077             : }
    3078             : 
    3079             : static GEN
    3080       52115 : init_Flxq_i(ulong p, long n, long sv)
    3081             : {
    3082             :   GEN P;
    3083       52115 :   if (!odd(p) && p != 2) pari_err_PRIME("ffinit", utoi(p));
    3084       52108 :   if (n == 1) return polx_Flx(sv);
    3085       52108 :   if (flinit_check(p, n+1, n))
    3086             :   {
    3087       23245 :     P = const_vecsmall(n+2,1);
    3088       23245 :     P[1] = sv; return P;
    3089             :   }
    3090       28863 :   P = ffinit_fact_Flx(p,n);
    3091       28863 :   P[1] = sv; return P;
    3092             : }
    3093             : 
    3094             : GEN
    3095           0 : init_Flxq(ulong p, long n, long v)
    3096             : {
    3097           0 :   pari_sp av = avma;
    3098           0 :   return gerepileupto(av, init_Flxq_i(p, n, v));
    3099             : }
    3100             : 
    3101             : /* check if polsubcyclo(n,l,0) is irreducible modulo p */
    3102             : static long
    3103        7185 : fpinit_check(GEN p, long n, long l)
    3104             : {
    3105             :   ulong q;
    3106        7185 :   if (!uisprime(n)) return 0;
    3107        4450 :   q = umodiu(p,n); if (!q) return 0;
    3108        4450 :   return ugcd((n-1)/Fl_order(q, n-1, n), l) == 1;
    3109             : }
    3110             : 
    3111             : /* let k=2 if p%4==1, and k=4 else and assume k*p does not divide l.
    3112             :  * Return an irreducible polynomial of degree l over F_p.
    3113             :  * Variant of Adleman and Lenstra "Finding irreducible polynomials over
    3114             :  * finite fields", ACM, 1986 (5) 350--355.
    3115             :  * Not stack clean */
    3116             : static GEN
    3117        1653 : fpinit(GEN p, long l)
    3118             : {
    3119        1653 :   ulong n = 1+l;
    3120        5202 :   while (!fpinit_check(p,n,l)) n += l;
    3121        1653 :   if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
    3122        1653 :   return FpX_red(polsubcyclo(n,l,0),p);
    3123             : }
    3124             : 
    3125             : static GEN
    3126        1574 : ffinit_fact(GEN p, long n)
    3127             : {
    3128        1574 :   GEN P, F = factoru_pow(n), Fp = gel(F,1), Fe = gel(F,2), Fm = gel(F,3);
    3129        1574 :   long i, l = lg(Fm);
    3130        1574 :   P = cgetg(l, t_VEC);
    3131        3227 :   for (i = 1; i < l; ++i)
    3132        3306 :     gel(P,i) = absequaliu(p, Fp[i]) ?
    3133           0 :                  Flx_to_ZX(ffinit_Artin_Schreier(Fp[i], Fe[i]))
    3134        1653 :                : fpinit(p, Fm[i]);
    3135        1574 :   return FpXV_composedsum(P, p);
    3136             : }
    3137             : 
    3138             : static GEN
    3139       54364 : init_Fq_i(GEN p, long n, long v)
    3140             : {
    3141             :   GEN P;
    3142       54364 :   if (n <= 0) pari_err_DOMAIN("ffinit", "degree", "<=", gen_0, stoi(n));
    3143       54364 :   if (typ(p) != t_INT) pari_err_TYPE("ffinit",p);
    3144       54364 :   if (cmpiu(p, 2) < 0) pari_err_PRIME("ffinit",p);
    3145       54357 :   if (v < 0) v = 0;
    3146       54357 :   if (n == 1) return pol_x(v);
    3147       54105 :   if (lgefint(p) == 3)
    3148       52115 :     return Flx_to_ZX(init_Flxq_i(p[2], n, evalvarn(v)));
    3149        1990 :   if (!mpodd(p)) pari_err_PRIME("ffinit", p);
    3150        1983 :   if (fpinit_check(p, n+1, n)) return polcyclo(n+1, v);
    3151        1574 :   P = ffinit_fact(p,n);
    3152        1574 :   setvarn(P, v); return P;
    3153             : }
    3154             : GEN
    3155       53797 : init_Fq(GEN p, long n, long v)
    3156             : {
    3157       53797 :   pari_sp av = avma;
    3158       53797 :   return gerepileupto(av, init_Fq_i(p, n, v));
    3159             : }
    3160             : GEN
    3161         567 : ffinit(GEN p, long n, long v)
    3162             : {
    3163         567 :   pari_sp av = avma;
    3164         567 :   return gerepileupto(av, FpX_to_mod(init_Fq_i(p, n, v), p));
    3165             : }
    3166             : 
    3167             : GEN
    3168        3178 : ffnbirred(GEN p, long n)
    3169             : {
    3170        3178 :   pari_sp av = avma;
    3171        3178 :   GEN s = powiu(p,n), F = factoru(n), D = divisorsu_moebius(gel(F, 1));
    3172        3178 :   long j, l = lg(D);
    3173        6797 :   for (j = 2; j < l; j++) /* skip d = 1 */
    3174             :   {
    3175        3619 :     long md = D[j]; /* mu(d) * d, d squarefree */
    3176        3619 :     GEN pd = powiu(p, n / labs(md)); /* p^{n/d} */
    3177        3619 :     s = md > 0? addii(s, pd): subii(s,pd);
    3178             :   }
    3179        3178 :   return gerepileuptoint(av, diviuexact(s, n));
    3180             : }
    3181             : 
    3182             : GEN
    3183         616 : ffsumnbirred(GEN p, long n)
    3184             : {
    3185         616 :   pari_sp av = avma, av2;
    3186         616 :   GEN q, t = p, v = vecfactoru_i(1, n);
    3187             :   long i;
    3188         616 :   q = cgetg(n+1,t_VEC); gel(q,1) = p;
    3189        1764 :   for (i=2; i<=n; i++) gel(q,i) = mulii(gel(q,i-1), p);
    3190         616 :   av2 = avma;
    3191        1764 :   for (i=2; i<=n; i++)
    3192             :   {
    3193        1148 :     GEN s = gel(q,i), F = gel(v,i), D = divisorsu_moebius(gel(F,1));
    3194        1148 :     long j, l = lg(D);
    3195        2534 :     for (j = 2; j < l; j++) /* skip 1 */
    3196             :     {
    3197        1386 :       long md = D[j];
    3198        1386 :       GEN pd = gel(q, i / labs(md)); /* p^{i/d} */
    3199        1386 :       s = md > 0? addii(s, pd): subii(s, pd);
    3200             :     }
    3201        1148 :     t = gerepileuptoint(av2, addii(t, diviuexact(s, i)));
    3202             :   }
    3203         616 :   return gerepileuptoint(av, t);
    3204             : }
    3205             : 
    3206             : GEN
    3207         140 : ffnbirred0(GEN p, long n, long flag)
    3208             : {
    3209         140 :   if (typ(p) != t_INT) pari_err_TYPE("ffnbirred", p);
    3210         140 :   if (n <= 0) pari_err_DOMAIN("ffnbirred", "degree", "<=", gen_0, stoi(n));
    3211         140 :   switch(flag)
    3212             :   {
    3213          70 :     case 0: return ffnbirred(p, n);
    3214          70 :     case 1: return ffsumnbirred(p, n);
    3215             :   }
    3216           0 :   pari_err_FLAG("ffnbirred");
    3217             :   return NULL; /* LCOV_EXCL_LINE */
    3218             : }
    3219             : 
    3220             : static void
    3221        2261 : checkmap(GEN m, const char *s)
    3222             : {
    3223        2261 :   if (typ(m)!=t_VEC || lg(m)!=3 || typ(gel(m,1))!=t_FFELT)
    3224           0 :     pari_err_TYPE(s,m);
    3225        2261 : }
    3226             : 
    3227             : GEN
    3228         189 : ffembed(GEN a, GEN b)
    3229             : {
    3230         189 :   pari_sp av = avma;
    3231         189 :   GEN p, Ta, Tb, g, r = NULL;
    3232         189 :   if (typ(a)!=t_FFELT) pari_err_TYPE("ffembed",a);
    3233         189 :   if (typ(b)!=t_FFELT) pari_err_TYPE("ffembed",b);
    3234         189 :   p = FF_p_i(a); g = FF_gen(a);
    3235         189 :   if (!equalii(p, FF_p_i(b))) pari_err_MODULUS("ffembed",a,b);
    3236         189 :   Ta = FF_mod(a);
    3237         189 :   Tb = FF_mod(b);
    3238         189 :   if (degpol(Tb)%degpol(Ta)!=0)
    3239           7 :     pari_err_DOMAIN("ffembed",GENtostr_raw(a),"is not a subfield of",b,a);
    3240         182 :   r = gel(FFX_roots(Ta, b), 1);
    3241         182 :   return gerepilecopy(av, mkvec2(g,r));
    3242             : }
    3243             : 
    3244             : GEN
    3245          91 : ffextend(GEN a, GEN P, long v)
    3246             : {
    3247          91 :   pari_sp av = avma;
    3248             :   long n;
    3249             :   GEN p, T, R, g, m;
    3250          91 :   if (typ(a)!=t_FFELT) pari_err_TYPE("ffextend",a);
    3251          91 :   T = a; p = FF_p_i(a);
    3252          91 :   if (typ(P)!=t_POL || !RgX_is_FpXQX(P,&T,&p)) pari_err_TYPE("ffextend", P);
    3253          49 :   if (!FF_samefield(a, T)) pari_err_MODULUS("ffextend",a,T);
    3254          49 :   if (v < 0) v = varn(P);
    3255          49 :   n = FF_f(T) * degpol(P); R = ffinit(p, n, v); g = ffgen(R, v);
    3256          49 :   m = ffembed(a, g);
    3257          49 :   R = FFX_roots(ffmap(m, P),g);
    3258          49 :   return gerepilecopy(av, mkvec2(gel(R,1), m));
    3259             : }
    3260             : 
    3261             : GEN
    3262          42 : fffrobenius(GEN a, long n)
    3263             : {
    3264          42 :   if (typ(a)!=t_FFELT) pari_err_TYPE("fffrobenius",a);
    3265          42 :   retmkvec2(FF_gen(a), FF_Frobenius(a, n));
    3266             : }
    3267             : 
    3268             : GEN
    3269         133 : ffinvmap(GEN m)
    3270             : {
    3271         133 :   pari_sp av = avma;
    3272             :   long i, l;
    3273         133 :   GEN T, F, a, g, r, f = NULL;
    3274         133 :   checkmap(m, "ffinvmap");
    3275         133 :   a = gel(m,1); r = gel(m,2);
    3276         133 :   if (typ(r) != t_FFELT)
    3277           7 :    pari_err_TYPE("ffinvmap", m);
    3278         126 :   g = FF_gen(a);
    3279         126 :   T = FF_mod(r);
    3280         126 :   F = gel(FFX_factor(T, a), 1);
    3281         126 :   l = lg(F);
    3282         490 :   for(i=1; i<l; i++)
    3283             :   {
    3284         490 :     GEN s = FFX_rem(FF_to_FpXQ_i(r), gel(F, i), a);
    3285         490 :     if (degpol(s)==0 && gequal(constant_coeff(s),g)) { f = gel(F, i); break; }
    3286             :   }
    3287         126 :   if (f==NULL) pari_err_TYPE("ffinvmap", m);
    3288         126 :   if (degpol(f)==1) f = FF_neg_i(gel(f,2));
    3289         126 :   return gerepilecopy(av, mkvec2(FF_gen(r),f));
    3290             : }
    3291             : 
    3292             : static GEN
    3293        1260 : ffpartmapimage(const char *s, GEN r)
    3294             : {
    3295        1260 :    GEN a = NULL, p = NULL;
    3296        1260 :    if (typ(r)==t_POL && degpol(r) >= 1
    3297        1260 :       && RgX_is_FpXQX(r,&a,&p) && a && typ(a)==t_FFELT) return a;
    3298           0 :    pari_err_TYPE(s, r);
    3299             :    return NULL; /* LCOV_EXCL_LINE */
    3300             : }
    3301             : 
    3302             : static GEN
    3303        2709 : ffeltmap_i(GEN m, GEN x)
    3304             : {
    3305        2709 :    GEN r = gel(m,2);
    3306        2709 :    if (!FF_samefield(x, gel(m,1)))
    3307          84 :      pari_err_DOMAIN("ffmap","m","domain does not contain", x, r);
    3308        2625 :    if (typ(r)==t_FFELT)
    3309        1659 :      return FF_map(r, x);
    3310             :    else
    3311         966 :      return FFX_preimage(x, r, ffpartmapimage("ffmap", r));
    3312             : }
    3313             : 
    3314             : static GEN
    3315        4459 : ffmap_i(GEN m, GEN x)
    3316             : {
    3317             :   GEN y;
    3318        4459 :   long i, lx, tx = typ(x);
    3319        4459 :   switch(tx)
    3320             :   {
    3321        2541 :     case t_FFELT:
    3322        2541 :       return ffeltmap_i(m, x);
    3323        1267 :     case t_POL: case t_RFRAC: case t_SER:
    3324             :     case t_VEC: case t_COL: case t_MAT:
    3325        1267 :       y = cgetg_copy(x, &lx);
    3326        1988 :       for (i=1; i<lontyp[tx]; i++) y[i] = x[1];
    3327        4564 :       for (i=lontyp[tx]; i<lx; i++)
    3328             :       {
    3329        3339 :         GEN yi = ffmap_i(m, gel(x,i));
    3330        3297 :         if (!yi) return NULL;
    3331        3297 :         gel(y,i) = yi;
    3332             :       }
    3333        1225 :       return y;
    3334             :   }
    3335         651 :   return gcopy(x);
    3336             : }
    3337             : 
    3338             : GEN
    3339        1036 : ffmap(GEN m, GEN x)
    3340             : {
    3341        1036 :   pari_sp ltop = avma;
    3342             :   GEN y;
    3343        1036 :   checkmap(m, "ffmap");
    3344        1036 :   y = ffmap_i(m, x);
    3345        1036 :   if (y) return y;
    3346          42 :   set_avma(ltop); return cgetg(1,t_VEC);
    3347             : }
    3348             : 
    3349             : static GEN
    3350         252 : ffeltmaprel_i(GEN m, GEN x)
    3351             : {
    3352         252 :    GEN g = gel(m,1), r = gel(m,2);
    3353         252 :    if (!FF_samefield(x, g))
    3354           0 :      pari_err_DOMAIN("ffmap","m","domain does not contain", x, r);
    3355         252 :    if (typ(r)==t_FFELT)
    3356          84 :      retmkpolmod(FF_map(r, x), pol_x(FF_var(g)));
    3357             :    else
    3358         168 :      retmkpolmod(FFX_preimagerel(x, r, ffpartmapimage("ffmap", r)), gcopy(r));
    3359             : }
    3360             : 
    3361             : static GEN
    3362         252 : ffmaprel_i(GEN m, GEN x)
    3363             : {
    3364             :   GEN y;
    3365         252 :   long i, lx, tx = typ(x);
    3366         252 :   switch(tx)
    3367             :   {
    3368         252 :     case t_FFELT:
    3369         252 :       return ffeltmaprel_i(m, x);
    3370           0 :     case t_POL: case t_RFRAC: case t_SER:
    3371             :     case t_VEC: case t_COL: case t_MAT:
    3372           0 :       y = cgetg_copy(x, &lx);
    3373           0 :       for (i=1; i<lontyp[tx]; i++) y[i] = x[1];
    3374           0 :       for (i=lontyp[tx]; i<lx; i++)
    3375           0 :         gel(y,i) = ffmaprel_i(m, gel(x,i));
    3376           0 :       return y;
    3377             :   }
    3378           0 :   return gcopy(x);
    3379             : }
    3380             : 
    3381             : GEN
    3382         252 : ffmaprel(GEN m, GEN x)
    3383             : {
    3384         252 :   checkmap(m, "ffmaprel");
    3385         252 :   return ffmaprel_i(m, x);
    3386             : }
    3387             : 
    3388             : static void
    3389          84 : err_compo(GEN m, GEN n)
    3390          84 : { pari_err_DOMAIN("ffcompomap","m","domain does not contain codomain of",n,m); }
    3391             : 
    3392             : GEN
    3393         420 : ffcompomap(GEN m, GEN n)
    3394             : {
    3395         420 :   pari_sp av = avma;
    3396         420 :   GEN g = gel(n,1), r, m2, n2;
    3397         420 :   checkmap(m, "ffcompomap");
    3398         420 :   checkmap(n, "ffcompomap");
    3399         420 :   m2 = gel(m,2); n2 = gel(n,2);
    3400         420 :   switch((typ(m2)==t_POL)|((typ(n2)==t_POL)<<1))
    3401             :   {
    3402          84 :     case 0:
    3403          84 :       if (!FF_samefield(gel(m,1),n2)) err_compo(m,n);
    3404          42 :       r = FF_map(gel(m,2), n2);
    3405          42 :       break;
    3406          84 :     case 2:
    3407          84 :       r = ffmap_i(m, n2);
    3408          42 :       if (lg(r) == 1) err_compo(m,n);
    3409          42 :       break;
    3410         168 :     case 1:
    3411         168 :       r = ffeltmap_i(m, n2);
    3412         126 :       if (!r)
    3413             :       {
    3414             :         GEN a, A, R, M;
    3415             :         long dm, dn;
    3416          42 :         a = ffpartmapimage("ffcompomap",m2);
    3417          42 :         A = FF_to_FpXQ_i(FF_neg(n2));
    3418          42 :         setvarn(A, 1);
    3419          42 :         R = deg1pol(gen_1, A, 0);
    3420          42 :         setvarn(R, 0);
    3421          42 :         M = gcopy(m2);
    3422          42 :         setvarn(M, 1);
    3423          42 :         r = polresultant0(R, M, 1, 0);
    3424          42 :         dm = FF_f(gel(m,1)); dn = FF_f(gel(n,1));
    3425          42 :         if (dm % dn || !FFX_ispower(r, dm/dn, a, &r)) err_compo(m,n);
    3426          42 :         setvarn(r, varn(FF_mod(g)));
    3427             :       }
    3428         126 :       break;
    3429          84 :     case 3:
    3430             :     {
    3431             :       GEN M, R, T, p, a;
    3432          84 :       a = ffpartmapimage("ffcompomap",n2);
    3433          84 :       if (!FF_samefield(a, gel(m,1))) err_compo(m,n);
    3434          42 :       p = FF_p_i(gel(n,1));
    3435          42 :       T = FF_mod(gel(n,1));
    3436          42 :       setvarn(T, 1);
    3437          42 :       R = RgX_to_FpXQX(n2,T,p);
    3438          42 :       setvarn(R, 0);
    3439          42 :       M = gcopy(m2);
    3440          42 :       setvarn(M, 1);
    3441          42 :       r = polresultant0(R, M, 1, 0);
    3442          42 :       setvarn(r, varn(n2));
    3443             :     }
    3444             :   }
    3445         252 :   return gerepilecopy(av, mkvec2(g,r));
    3446             : }

Generated by: LCOV version 1.16