Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - polarit3.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 25406-bf255ab81b) Lines: 1713 1921 89.2 %
Date: 2020-06-04 05:59:24 Functions: 179 194 92.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13