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 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.8.0 lcov report (development 19614-52e089f) Lines: 1012 1203 84.1 %
Date: 2016-09-28 05:54:17 Functions: 112 126 88.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2005  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /***********************************************************************/
      15             : /**                                                                   **/
      16             : /**               ARITHMETIC OPERATIONS ON POLYNOMIALS                **/
      17             : /**                         (third part)                              **/
      18             : /**                                                                   **/
      19             : /***********************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : /************************************************************************
      24             :  **                                                                    **
      25             :  **                      Ring membership                               **
      26             :  **                                                                    **
      27             :  ************************************************************************/
      28             : struct charact {
      29             :   GEN q;
      30             :   int isprime;
      31             : };
      32             : static void
      33        3290 : char_update_prime(struct charact *S, GEN p)
      34             : {
      35        3290 :   if (!S->isprime) { S->isprime = 1; S->q = p; }
      36        3290 :   if (!equalii(p, S->q)) pari_err_MODULUS("characteristic", S->q, p);
      37        3283 : }
      38             : static void
      39        3423 : char_update_int(struct charact *S, GEN n)
      40             : {
      41        3423 :   if (S->isprime)
      42             :   {
      43        3423 :     if (dvdii(n, S->q)) return;
      44           0 :     pari_err_MODULUS("characteristic", S->q, n);
      45             :   }
      46        3423 :   S->q = gcdii(S->q, n);
      47             : }
      48             : static void
      49      559251 : charact(struct charact *S, GEN x)
      50             : {
      51      559251 :   const long tx = typ(x);
      52             :   long i, l;
      53      559251 :   switch(tx)
      54             :   {
      55        3157 :     case t_INTMOD:char_update_int(S, gel(x,1)); break;
      56        3227 :     case t_FFELT: char_update_prime(S, gel(x,4)); break;
      57             :     case t_COMPLEX: case t_QUAD:
      58             :     case t_POLMOD: case t_POL: case t_SER: case t_RFRAC:
      59             :     case t_VEC: case t_COL: case t_MAT:
      60        6167 :       l = lg(x);
      61        6167 :       for (i=lontyp[tx]; i < l; i++) charact(S,gel(x,i));
      62        6160 :       break;
      63             :     case t_LIST:
      64           7 :       x = list_data(x);
      65           7 :       if (x) charact(S, x);
      66           7 :       break;
      67             :   }
      68      559237 : }
      69             : static void
      70       30296 : charact_res(struct charact *S, GEN x)
      71             : {
      72       30296 :   const long tx = typ(x);
      73             :   long i, l;
      74       30296 :   switch(tx)
      75             :   {
      76         266 :     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          63 :     case t_PADIC: char_update_prime(S, gel(x,2)); break;
      79             :     case t_COMPLEX: case t_QUAD:
      80             :     case t_POLMOD: case t_POL: case t_SER: case t_RFRAC:
      81             :     case t_VEC: case t_COL: case t_MAT:
      82        9317 :       l = lg(x);
      83        9317 :       for (i=lontyp[tx]; i < l; i++) charact_res(S,gel(x,i));
      84        9317 :       break;
      85             :     case t_LIST:
      86           0 :       x = list_data(x);
      87           0 :       if (x) charact_res(S, x);
      88           0 :       break;
      89             :   }
      90       30296 : }
      91             : GEN
      92        5068 : characteristic(GEN x)
      93             : {
      94             :   struct charact S;
      95        5068 :   S.q = gen_0; S.isprime = 0;
      96        5068 :   charact(&S, x); return S.q;
      97             : }
      98             : GEN
      99        2324 : residual_characteristic(GEN x)
     100             : {
     101             :   struct charact S;
     102        2324 :   S.q = gen_0; S.isprime = 0;
     103        2324 :   charact_res(&S, x); return S.q;
     104             : }
     105             : 
     106             : int
     107    16689925 : Rg_is_Fp(GEN x, GEN *pp)
     108             : {
     109             :   GEN mod;
     110    16689925 :   switch(typ(x))
     111             :   {
     112             :   case t_INTMOD:
     113     3849629 :     mod = gel(x,1);
     114     3849629 :     if (!*pp) *pp = mod;
     115     3797353 :     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     3849629 :     return 1;
     121             :   case t_INT:
     122    10497799 :     return 1;
     123     2342497 :   default: return 0;
     124             :   }
     125             : }
     126             : 
     127             : int
     128     2352210 : RgX_is_FpX(GEN x, GEN *pp)
     129             : {
     130     2352210 :   long i, lx = lg(x);
     131     8985150 :   for (i=2; i<lx; i++)
     132     8341597 :     if (!Rg_is_Fp(gel(x, i), pp))
     133     1708657 :       return 0;
     134      643553 :   return 1;
     135             : }
     136             : 
     137             : int
     138     1778611 : RgV_is_FpV(GEN x, GEN *pp)
     139             : {
     140     1778611 :   long i, lx = lg(x);
     141     9470944 :   for (i=1; i<lx; i++)
     142     8326166 :     if (!Rg_is_Fp(gel(x,i), pp)) return 0;
     143     1144778 :   return 1;
     144             : }
     145             : 
     146             : int
     147      763605 : RgM_is_FpM(GEN x, GEN *pp)
     148             : {
     149      763605 :   long i, lx = lg(x);
     150     1905947 :   for (i=1; i<lx; i++)
     151     1776098 :     if (!RgV_is_FpV(gel(x, i), pp)) return 0;
     152      129849 :   return 1;
     153             : }
     154             : 
     155             : int
     156       90909 : Rg_is_FpXQ(GEN x, GEN *pT, GEN *pp)
     157             : {
     158             :   GEN pol, mod, p;
     159       90909 :   switch(typ(x))
     160             :   {
     161             :   case t_INTMOD:
     162       22092 :     return Rg_is_Fp(x, pp);
     163             :   case t_INT:
     164       20335 :     return 1;
     165             :   case t_POL:
     166        8491 :     return RgX_is_FpX(x, pp);
     167             :   case t_FFELT:
     168       10010 :     mod = FF_1(x); p = FF_p_i(x);
     169       10010 :     if (!*pp) *pp = p;
     170       10010 :     if (!*pT) *pT = mod;
     171       10010 :     if ((p != *pp && !equalii(p, *pp)) || (mod != *pT && !gequal(mod, *pT)))
     172             :     {
     173           0 :       if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_FpXQ");
     174           0 :       return 0;
     175             :     }
     176       10010 :     return 1;
     177             :   case t_POLMOD:
     178        5397 :     mod = gel(x,1); pol = gel(x, 2);
     179        5397 :     if (!RgX_is_FpX(mod, pp)) return 0;
     180        5397 :     if (typ(pol)==t_POL)
     181             :     {
     182        5327 :       if (!RgX_is_FpX(pol, pp)) return 0;
     183             :     }
     184          70 :     else if (!Rg_is_Fp(pol, pp)) return 0;
     185        5376 :     if (!*pT) *pT = mod;
     186        4718 :     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        5376 :     return 1;
     192             : 
     193       24584 :   default: return 0;
     194             :   }
     195             : }
     196             : 
     197             : int
     198       32809 : RgX_is_FpXQX(GEN x, GEN *pT, GEN *pp)
     199             : {
     200       32809 :   long i, lx = lg(x);
     201       97832 :   for (i = 2; i < lx; i++)
     202       90363 :     if (!Rg_is_FpXQ(gel(x,i), pT, pp)) return 0;
     203        7469 :   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    26587599 : Rg_to_Fp(GEN x, GEN p)
     216             : {
     217    26587599 :   if (lgefint(p) == 3) return utoi(Rg_to_Fl(x, uel(p,2)));
     218      386715 :   switch(typ(x))
     219             :   {
     220       25742 :     case t_INT: return modii(x, p);
     221             :     case t_FRAC: {
     222          60 :       pari_sp av = avma;
     223          60 :       GEN z = modii(gel(x,1), p);
     224          60 :       if (z == gen_0) return gen_0;
     225          60 :       return gerepileuptoint(av, remii(mulii(z, Fp_inv(gel(x,2), p)), p));
     226             :     }
     227           0 :     case t_PADIC: return padic_to_Fp(x, p);
     228             :     case t_INTMOD: {
     229      360913 :       GEN q = gel(x,1), a = gel(x,2);
     230      360913 :       if (equalii(q, p)) return icopy(a);
     231          14 :       if (!dvdii(q,p)) pari_err_MODULUS("Rg_to_Fp", q, p);
     232           0 :       return remii(a, p);
     233             :     }
     234           0 :     default: pari_err_TYPE("Rg_to_Fp",x);
     235           0 :       return NULL; /* not reached */
     236             :   }
     237             : }
     238             : /* If x is a POLMOD, assume modulus is a multiple of T. */
     239             : GEN
     240       46264 : Rg_to_FpXQ(GEN x, GEN T, GEN p)
     241             : {
     242       46264 :   long ta, tx = typ(x), v = get_FpX_var(T);
     243             :   GEN a, b;
     244       46264 :   if (is_const_t(tx))
     245             :   {
     246       41777 :     if (tx == t_FFELT)
     247             :     {
     248       21812 :       GEN z = FF_to_FpXQ(x);
     249       21812 :       setvarn(z, v);
     250       21812 :       return z;
     251             :     }
     252       19965 :     return scalar_ZX(Rg_to_Fp(x, p), v);
     253             :   }
     254        4487 :   switch(tx)
     255             :   {
     256             :     case t_POLMOD:
     257        3724 :       b = gel(x,1);
     258        3724 :       a = gel(x,2); ta = typ(a);
     259        3724 :       if (is_const_t(ta)) return scalar_ZX(Rg_to_Fp(a, p), v);
     260        3682 :       b = RgX_to_FpX(b, p); if (varn(b) != v) break;
     261        3682 :       a = RgX_to_FpX(a, p); if (ZX_equal(b,get_FpX_mod(T))) return a;
     262           0 :       if (signe(FpX_rem(b,T,p))==0) return FpX_rem(a, T, p);
     263           0 :       break;
     264             :     case t_POL:
     265         763 :       if (varn(x) != v) break;
     266         763 :       return FpX_rem(RgX_to_FpX(x,p), T, p);
     267             :     case t_RFRAC:
     268           0 :       a = Rg_to_FpXQ(gel(x,1), T,p);
     269           0 :       b = Rg_to_FpXQ(gel(x,2), T,p);
     270           0 :       return FpXQ_div(a,b, T,p);
     271             :   }
     272           0 :   pari_err_TYPE("Rg_to_FpXQ",x);
     273           0 :   return NULL; /* not reached */
     274             : }
     275             : GEN
     276      140735 : RgX_to_FpX(GEN x, GEN p)
     277             : {
     278             :   long i, l;
     279      140735 :   GEN z = cgetg_copy(x, &l); z[1] = x[1];
     280      140735 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     281      140735 :   return FpX_renormalize(z, l);
     282             : }
     283             : 
     284             : GEN
     285        1099 : RgV_to_FpV(GEN x, GEN p)
     286             : {
     287        1099 :   long i, l = lg(x);
     288        1099 :   GEN z = cgetg(l, t_VEC);
     289        1099 :   for (i = 1; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     290        1099 :   return z;
     291             : }
     292             : 
     293             : GEN
     294      608852 : RgC_to_FpC(GEN x, GEN p)
     295             : {
     296      608852 :   long i, l = lg(x);
     297      608852 :   GEN z = cgetg(l, t_COL);
     298      608852 :   for (i = 1; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     299      608852 :   return z;
     300             : }
     301             : 
     302             : GEN
     303       44684 : RgM_to_FpM(GEN x, GEN p)
     304             : {
     305       44684 :   long i, l = lg(x);
     306       44684 :   GEN z = cgetg(l, t_MAT);
     307       44684 :   for (i = 1; i < l; i++) gel(z,i) = RgC_to_FpC(gel(x,i), p);
     308       44684 :   return z;
     309             : }
     310             : GEN
     311       12615 : RgV_to_Flv(GEN x, ulong p)
     312             : {
     313       12615 :   long l = lg(x), i;
     314       12615 :   GEN a = cgetg(l, t_VECSMALL);
     315       12615 :   for (i=1; i<l; i++) a[i] = Rg_to_Fl(gel(x,i), p);
     316       12615 :   return a;
     317             : }
     318             : GEN
     319        1768 : RgM_to_Flm(GEN x, ulong p)
     320             : {
     321             :   long l, i;
     322        1768 :   GEN a = cgetg_copy(x, &l);
     323        1768 :   for (i=1; i<l; i++) gel(a,i) = RgV_to_Flv(gel(x,i), p);
     324        1768 :   return a;
     325             : }
     326             : 
     327             : GEN
     328         336 : RgX_to_FpXQX(GEN x, GEN T, GEN p)
     329             : {
     330         336 :   long i, l = lg(x);
     331         336 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     332         336 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T,p);
     333         336 :   return FpXQX_renormalize(z, l);
     334             : }
     335             : GEN
     336         665 : RgX_to_FqX(GEN x, GEN T, GEN p)
     337             : {
     338         665 :   long i, l = lg(x);
     339         665 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     340         665 :   if (T)
     341       11263 :     for (i = 2; i < l; i++)
     342       10640 :       gel(z,i) = simplify_shallow(Rg_to_FpXQ(gel(x,i), T, p));
     343             :   else
     344        1554 :     for (i = 2; i < l; i++)
     345        1512 :       gel(z,i) = Rg_to_Fp(gel(x,i), p);
     346         665 :   return FpXQX_renormalize(z, l);
     347             : }
     348             : 
     349             : /* lg(V) > 1 */
     350             : GEN
     351      848638 : FpXV_FpC_mul(GEN V, GEN W, GEN p)
     352             : {
     353      848638 :   pari_sp av = avma;
     354      848638 :   long i, l = lg(V);
     355      848638 :   GEN z = ZX_Z_mul(gel(V,1),gel(W,1));
     356     4173540 :   for(i=2; i<l; i++)
     357             :   {
     358     3324902 :     z = ZX_add(z, ZX_Z_mul(gel(V,i),gel(W,i)));
     359     3324902 :     if ((i & 7) == 0) z = gerepileupto(av, z);
     360             :   }
     361      848638 :   return gerepileupto(av, FpX_red(z,p));
     362             : }
     363             : 
     364             : GEN
     365        2275 : FqX_Fq_add(GEN y, GEN x, GEN T, GEN p)
     366             : {
     367        2275 :   long i, lz = lg(y);
     368             :   GEN z;
     369        2275 :   if (!T) return FpX_Fp_add(y, x, p);
     370        2275 :   if (lz == 2) return scalarpol(x, varn(y));
     371        2275 :   z = cgetg(lz,t_POL); z[1] = y[1];
     372        2275 :   gel(z,2) = Fq_add(gel(y,2),x, T, p);
     373        2275 :   if (lz == 3) z = FpXX_renormalize(z,lz);
     374             :   else
     375        1239 :     for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
     376        2275 :   return z;
     377             : }
     378             : 
     379             : GEN
     380       63361 : FqX_Fq_mul_to_monic(GEN P, GEN U, GEN T, GEN p)
     381             : {
     382             :   long i, lP;
     383       63361 :   GEN res = cgetg_copy(P, &lP); res[1] = P[1];
     384       63361 :   for(i=2; i<lP-1; i++) gel(res,i) = Fq_mul(U,gel(P,i), T,p);
     385       63361 :   gel(res,lP-1) = gen_1; return res;
     386             : }
     387             : 
     388             : GEN
     389        2793 : FpXQX_normalize(GEN z, GEN T, GEN p)
     390             : {
     391             :   GEN lc;
     392        2793 :   if (lg(z) == 2) return z;
     393        2793 :   lc = leading_coeff(z);
     394        2793 :   if (typ(lc) == t_POL)
     395             :   {
     396        1195 :     if (lg(lc) > 3) /* non-constant */
     397        1180 :       return FqX_Fq_mul_to_monic(z, Fq_inv(lc,T,p), T,p);
     398             :     /* constant */
     399          15 :     lc = gel(lc,2);
     400          15 :     z = shallowcopy(z);
     401          15 :     gel(z, lg(z)-1) = lc;
     402             :   }
     403             :   /* lc a t_INT */
     404        1613 :   if (equali1(lc)) return z;
     405          91 :   return FqX_Fq_mul_to_monic(z, Fp_inv(lc,p), T,p);
     406             : }
     407             : 
     408             : GEN
     409      123375 : FqX_eval(GEN x, GEN y, GEN T, GEN p)
     410             : {
     411             :   pari_sp av;
     412             :   GEN p1, r;
     413      123375 :   long j, i=lg(x)-1;
     414      123375 :   if (i<=2)
     415       24136 :     return (i==2)? Fq_red(gel(x,2), T, p): gen_0;
     416       99239 :   av=avma; p1=gel(x,i);
     417             :   /* specific attention to sparse polynomials (see poleval)*/
     418             :   /*You've guessed it! It's a copy-paste(tm)*/
     419      291095 :   for (i--; i>=2; i=j-1)
     420             :   {
     421      192171 :     for (j=i; !signe(gel(x,j)); j--)
     422         315 :       if (j==2)
     423             :       {
     424         182 :         if (i!=j) y = Fq_pow(y,utoipos(i-j+1), T, p);
     425         182 :         return gerepileupto(av, Fq_mul(p1,y, T, p));
     426             :       }
     427      191856 :     r = (i==j)? y: Fq_pow(y, utoipos(i-j+1), T, p);
     428      191856 :     p1 = Fq_add(Fq_mul(p1,r,T,p), gel(x,j), T, p);
     429             :   }
     430       99057 :   return gerepileupto(av, p1);
     431             : }
     432             : 
     433             : GEN
     434       30380 : FqXY_evalx(GEN Q, GEN x, GEN T, GEN p)
     435             : {
     436       30380 :   long i, lb = lg(Q);
     437             :   GEN z;
     438       30380 :   if (!T) return FpXY_evalx(Q, x, p);
     439       20720 :   z = cgetg(lb, t_POL); z[1] = Q[1];
     440      115969 :   for (i=2; i<lb; i++)
     441             :   {
     442       95249 :     GEN q = gel(Q,i);
     443       95249 :     gel(z,i) = typ(q) == t_INT? modii(q,p): FqX_eval(q, x, T, p);
     444             :   }
     445       20720 :   return FpXQX_renormalize(z, lb);
     446             : }
     447             : 
     448             : /* Q an FpXY, evaluate at (X,Y) = (x,y) */
     449             : GEN
     450       12733 : FqXY_eval(GEN Q, GEN y, GEN x, GEN T, GEN p)
     451             : {
     452       12733 :   pari_sp av = avma;
     453       12733 :   if (!T) return FpXY_eval(Q, y, x, p);
     454         336 :   return gerepileupto(av, FqX_eval(FqXY_evalx(Q, x, T, p), y, T, p));
     455             : }
     456             : 
     457             : /* a X^d */
     458             : GEN
     459      111468 : monomial(GEN a, long d, long v)
     460             : {
     461             :   long i, n;
     462             :   GEN P;
     463      111468 :   if (d < 0) {
     464           0 :     if (isrationalzero(a)) return pol_0(v);
     465           0 :     retmkrfrac(a, pol_xn(-d, v));
     466             :   }
     467      111468 :   if (gequal0(a))
     468             :   {
     469         266 :     if (isexactzero(a)) return scalarpol_shallow(a,v);
     470           0 :     n = d+2; P = cgetg(n+1, t_POL);
     471           0 :     P[1] = evalsigne(0) | evalvarn(v);
     472             :   }
     473             :   else
     474             :   {
     475      111202 :     n = d+2; P = cgetg(n+1, t_POL);
     476      111202 :     P[1] = evalsigne(1) | evalvarn(v);
     477             :   }
     478      111202 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     479      111202 :   gel(P,i) = a; return P;
     480             : }
     481             : GEN
     482     7307305 : monomialcopy(GEN a, long d, long v)
     483             : {
     484             :   long i, n;
     485             :   GEN P;
     486     7307305 :   if (d < 0) {
     487           7 :     if (isrationalzero(a)) return pol_0(v);
     488           7 :     retmkrfrac(gcopy(a), pol_xn(-d, v));
     489             :   }
     490     7307298 :   if (gequal0(a))
     491             :   {
     492           7 :     if (isexactzero(a)) return scalarpol(a,v);
     493           0 :     n = d+2; P = cgetg(n+1, t_POL);
     494           0 :     P[1] = evalsigne(0) | evalvarn(v);
     495             :   }
     496             :   else
     497             :   {
     498     7307291 :     n = d+2; P = cgetg(n+1, t_POL);
     499     7307291 :     P[1] = evalsigne(1) | evalvarn(v);
     500             :   }
     501     7307291 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     502     7307291 :   gel(P,i) = gcopy(a); return P;
     503             : }
     504             : GEN
     505       19530 : pol_x_powers(long N, long v)
     506             : {
     507       19530 :   GEN L = cgetg(N+1,t_VEC);
     508             :   long i;
     509       19530 :   for (i=1; i<=N; i++) gel(L,i) = pol_xn(i-1, v);
     510       19530 :   return L;
     511             : }
     512             : 
     513             : GEN
     514           0 : FqXQ_powers(GEN x, long l, GEN S, GEN T, GEN p)
     515             : {
     516           0 :   return T ? FpXQXQ_powers(x, l, S, T, p): FpXQ_powers(x, l, S, p);
     517             : }
     518             : 
     519             : GEN
     520           0 : FqXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, GEN p)
     521             : {
     522           0 :   return T ? FpXQXQ_matrix_pow(y, n, m, S, T, p): FpXQ_matrix_pow(y, n, m, S, p);
     523             : }
     524             : 
     525             : /*******************************************************************/
     526             : /*                                                                 */
     527             : /*                             Fq                                  */
     528             : /*                                                                 */
     529             : /*******************************************************************/
     530             : 
     531             : GEN
     532     6679914 : Fq_add(GEN x, GEN y, GEN T/*unused*/, GEN p)
     533             : {
     534             :   (void)T;
     535     6679914 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     536             :   {
     537     2378998 :     case 0: return Fp_add(x,y,p);
     538      203988 :     case 1: return FpX_Fp_add(x,y,p);
     539      337835 :     case 2: return FpX_Fp_add(y,x,p);
     540     3759093 :     case 3: return FpX_add(x,y,p);
     541             :   }
     542           0 :   return NULL;
     543             : }
     544             : 
     545             : GEN
     546     4619511 : Fq_sub(GEN x, GEN y, GEN T/*unused*/, GEN p)
     547             : {
     548             :   (void)T;
     549     4619511 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     550             :   {
     551      156833 :     case 0: return Fp_sub(x,y,p);
     552        1158 :     case 1: return FpX_Fp_sub(x,y,p);
     553        8118 :     case 2: return Fp_FpX_sub(x,y,p);
     554     4453402 :     case 3: return FpX_sub(x,y,p);
     555             :   }
     556           0 :   return NULL;
     557             : }
     558             : 
     559             : GEN
     560      587174 : Fq_neg(GEN x, GEN T/*unused*/, GEN p)
     561             : {
     562             :   (void)T;
     563      587174 :   return (typ(x)==t_POL)? FpX_neg(x,p): Fp_neg(x,p);
     564             : }
     565             : 
     566             : GEN
     567       11471 : Fq_halve(GEN x, GEN T/*unused*/, GEN p)
     568             : {
     569             :   (void)T;
     570       11471 :   return (typ(x)==t_POL)? FpX_halve(x,p): Fp_halve(x,p);
     571             : }
     572             : 
     573             : /* If T==NULL do not reduce*/
     574             : GEN
     575    42459140 : Fq_mul(GEN x, GEN y, GEN T, GEN p)
     576             : {
     577    42459140 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     578             :   {
     579     2379038 :     case 0: return Fp_mul(x,y,p);
     580      188917 :     case 1: return FpX_Fp_mul(x,y,p);
     581      316481 :     case 2: return FpX_Fp_mul(y,x,p);
     582    39574704 :     case 3: if (T) return FpXQ_mul(x,y,T,p);
     583     2962644 :             else return FpX_mul(x,y,p);
     584             :   }
     585           0 :   return NULL;
     586             : }
     587             : 
     588             : /* If T==NULL do not reduce*/
     589             : GEN
     590      861237 : Fq_mulu(GEN x, ulong y, /*unused*/GEN T, GEN p)
     591             : {
     592             :   (void) T;
     593      861237 :   return typ(x)==t_POL ? FpX_Fp_mul(x,utoi(y),p): Fp_mulu(x, y, p);
     594             : }
     595             : 
     596             : /* y t_INT */
     597             : GEN
     598       51743 : Fq_Fp_mul(GEN x, GEN y, GEN T/*unused*/, GEN p)
     599             : {
     600             :   (void)T;
     601      103486 :   return (typ(x) == t_POL)? FpX_Fp_mul(x,y,p)
     602       51743 :                           : Fp_mul(x,y,p);
     603             : }
     604             : /* If T==NULL do not reduce*/
     605             : GEN
     606      262513 : Fq_sqr(GEN x, GEN T, GEN p)
     607             : {
     608      262513 :   if (typ(x) == t_POL)
     609             :   {
     610       11856 :     if (T) return FpXQ_sqr(x,T,p);
     611           0 :     else return FpX_sqr(x,p);
     612             :   }
     613             :   else
     614      250657 :     return Fp_sqr(x,p);
     615             : }
     616             : 
     617             : GEN
     618           0 : Fq_neg_inv(GEN x, GEN T, GEN p)
     619             : {
     620           0 :   if (typ(x) == t_INT) return Fp_inv(Fp_neg(x,p),p);
     621           0 :   return FpXQ_inv(FpX_neg(x,p),T,p);
     622             : }
     623             : 
     624             : GEN
     625           0 : Fq_invsafe(GEN x, GEN pol, GEN p)
     626             : {
     627           0 :   if (typ(x) == t_INT) return Fp_invsafe(x,p);
     628           0 :   return FpXQ_invsafe(x,pol,p);
     629             : }
     630             : 
     631             : GEN
     632       20079 : Fq_inv(GEN x, GEN pol, GEN p)
     633             : {
     634       20079 :   if (typ(x) == t_INT) return Fp_inv(x,p);
     635       15396 :   return FpXQ_inv(x,pol,p);
     636             : }
     637             : 
     638             : GEN
     639      479262 : Fq_div(GEN x, GEN y, GEN pol, GEN p)
     640             : {
     641      479262 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     642             :   {
     643      451563 :     case 0: return Fp_div(x,y,p);
     644       22771 :     case 1: return FpX_Fp_mul(x,Fp_inv(y,p),p);
     645         140 :     case 2: return FpX_Fp_mul(FpXQ_inv(y,pol,p),x,p);
     646        4788 :     case 3: return FpXQ_div(x,y,pol,p);
     647             :   }
     648           0 :   return NULL;
     649             : }
     650             : 
     651             : GEN
     652       11116 : Fq_pow(GEN x, GEN n, GEN pol, GEN p)
     653             : {
     654       11116 :   if (typ(x) == t_INT) return Fp_pow(x,n,p);
     655        8288 :   return FpXQ_pow(x,n,pol,p);
     656             : }
     657             : 
     658             : GEN
     659       12985 : Fq_powu(GEN x, ulong n, GEN pol, GEN p)
     660             : {
     661       12985 :   if (typ(x) == t_INT) return Fp_powu(x,n,p);
     662         553 :   return FpXQ_powu(x,n,pol,p);
     663             : }
     664             : 
     665             : GEN
     666      698920 : Fq_sqrt(GEN x, GEN T, GEN p)
     667             : {
     668      698920 :   if (typ(x) == t_INT)
     669             :   {
     670      692566 :     if (!T || odd(get_FpX_degree(T))) return Fp_sqrt(x,p);
     671           0 :     x = scalarpol_shallow(x, get_FpX_var(T));
     672             :   }
     673        6354 :   return FpXQ_sqrt(x,T,p);
     674             : }
     675             : GEN
     676       60866 : Fq_sqrtn(GEN x, GEN n, GEN T, GEN p, GEN *zeta)
     677             : {
     678       60866 :   if (typ(x) == t_INT)
     679             :   {
     680             :     long d;
     681       60061 :     if (!T) return Fp_sqrtn(x,n,p,zeta);
     682         224 :     d = get_FpX_degree(T);
     683         224 :     if (ugcd(umodiu(n,d),d) == 1)
     684             :     {
     685          35 :       if (!zeta)
     686           7 :         return Fp_sqrtn(x,n,p,NULL);
     687             :       else
     688             :       {
     689             :         /* gcd(n,p-1)=gcd(n,p^d-1) <=> same number of solutions if Fp and F_{p^d} */
     690          28 :         if (equalii(gcdii(subiu(p,1),n), gcdii(subiu(Fp_powu(p,d,n), 1), n)))
     691           7 :           return Fp_sqrtn(x,n,p,zeta);
     692             :       }
     693             :     }
     694         210 :     x = scalarpol(x, get_FpX_var(T)); /* left on stack */
     695             :   }
     696        1015 :   return FpXQ_sqrtn(x,n,T,p,zeta);
     697             : }
     698             : 
     699             : struct _Fq_field
     700             : {
     701             :   GEN T, p;
     702             : };
     703             : 
     704             : static GEN
     705       49696 : _Fq_red(void *E, GEN x)
     706       49696 : { struct _Fq_field *s = (struct _Fq_field *)E;
     707       49696 :   return Fq_red(x, s->T, s->p);
     708             : }
     709             : 
     710             : static GEN
     711      613027 : _Fq_add(void *E, GEN x, GEN y)
     712             : {
     713             :   (void) E;
     714      613027 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     715             :   {
     716           0 :     case 0: return addii(x,y);
     717           0 :     case 1: return ZX_Z_add(x,y);
     718          90 :     case 2: return ZX_Z_add(y,x);
     719      612937 :     default: return ZX_add(x,y);
     720             :   }
     721             : }
     722             : 
     723             : static GEN
     724        1287 : _Fq_neg(void *E, GEN x) { (void) E; return typ(x)==t_POL?ZX_neg(x):negi(x); }
     725             : 
     726             : static GEN
     727      637241 : _Fq_mul(void *E, GEN x, GEN y)
     728             : {
     729             :   (void) E;
     730      637241 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     731             :   {
     732           0 :     case 0: return mulii(x,y);
     733          42 :     case 1: return ZX_Z_mul(x,y);
     734          28 :     case 2: return ZX_Z_mul(y,x);
     735      637171 :     default: return ZX_mul(x,y);
     736             :   }
     737             : }
     738             : 
     739             : static GEN
     740        1210 : _Fq_inv(void *E, GEN x)
     741        1210 : { struct _Fq_field *s = (struct _Fq_field *)E;
     742        1210 :   return Fq_inv(x,s->T,s->p);
     743             : }
     744             : 
     745             : static int
     746       25182 : _Fq_equal0(GEN x) { return signe(x)==0; }
     747             : 
     748             : static GEN
     749        1534 : _Fq_s(void *E, long x) { (void) E; return stoi(x); }
     750             : 
     751             : static const struct bb_field Fq_field={_Fq_red,_Fq_add,_Fq_mul,_Fq_neg,
     752             :                                        _Fq_inv,_Fq_equal0,_Fq_s};
     753             : 
     754         427 : const struct bb_field *get_Fq_field(void **E, GEN T, GEN p)
     755             : {
     756         427 :   GEN z = new_chunk(sizeof(struct _Fq_field));
     757         427 :   struct _Fq_field *e = (struct _Fq_field *) z;
     758         427 :   e->T = T; e->p  = p; *E = (void*)e;
     759         427 :   return &Fq_field;
     760             : }
     761             : 
     762             : /*******************************************************************/
     763             : /*                                                                 */
     764             : /*                             Fq[X]                               */
     765             : /*                                                                 */
     766             : /*******************************************************************/
     767             : /* P(X + c) */
     768             : GEN
     769           0 : FpX_translate(GEN P, GEN c, GEN p)
     770             : {
     771           0 :   pari_sp av = avma;
     772             :   GEN Q, *R;
     773             :   long i, k, n;
     774             : 
     775           0 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     776           0 :   Q = leafcopy(P);
     777           0 :   R = (GEN*)(Q+2); n = degpol(P);
     778           0 :   for (i=1; i<=n; i++)
     779             :   {
     780           0 :     for (k=n-i; k<n; k++)
     781           0 :       R[k] = Fp_add(R[k], Fp_mul(c, R[k+1], p), p);
     782             : 
     783           0 :     if (gc_needed(av,2))
     784             :     {
     785           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FpX_translate, i = %ld/%ld", i,n);
     786           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     787             :     }
     788             :   }
     789           0 :   return gerepilecopy(av, FpX_renormalize(Q, lg(Q)));
     790             : }
     791             : /* P(X + c), c an Fq */
     792             : GEN
     793       39466 : FqX_translate(GEN P, GEN c, GEN T, GEN p)
     794             : {
     795       39466 :   pari_sp av = avma;
     796             :   GEN Q, *R;
     797             :   long i, k, n;
     798             : 
     799             :   /* signe works for t_(INT|POL) */
     800       39466 :   if (!signe(P) || !signe(c)) return RgX_copy(P);
     801       39466 :   Q = leafcopy(P);
     802       39466 :   R = (GEN*)(Q+2); n = degpol(P);
     803      172935 :   for (i=1; i<=n; i++)
     804             :   {
     805      486717 :     for (k=n-i; k<n; k++)
     806      353248 :       R[k] = Fq_add(R[k], Fq_mul(c, R[k+1], T, p), T, p);
     807             : 
     808      133469 :     if (gc_needed(av,2))
     809             :     {
     810           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FqX_translate, i = %ld/%ld", i,n);
     811           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     812             :     }
     813             :   }
     814       39466 :   return gerepilecopy(av, FpXQX_renormalize(Q, lg(Q)));
     815             : }
     816             : 
     817             : GEN
     818         588 : FqV_roots_to_pol(GEN V, GEN T, GEN p, long v)
     819             : {
     820         588 :   pari_sp ltop = avma;
     821             :   long k;
     822             :   GEN W;
     823         588 :   if (lgefint(p) == 3)
     824             :   {
     825         549 :     ulong pp = p[2];
     826         549 :     GEN Tl = ZX_to_Flx(T, pp);
     827         549 :     GEN Vl = FqV_to_FlxV(V, T, p);
     828         549 :     Tl = FlxqV_roots_to_pol(Vl, Tl, pp, v);
     829         549 :     return gerepileupto(ltop, FlxX_to_ZXX(Tl));
     830             :   }
     831          39 :   W = cgetg(lg(V),t_VEC);
     832         255 :   for(k=1; k < lg(V); k++)
     833         216 :     gel(W,k) = deg1pol_shallow(gen_1,Fq_neg(gel(V,k),T,p),v);
     834          39 :   return gerepileupto(ltop, FpXQXV_prod(W, T, p));
     835             : }
     836             : 
     837             : GEN
     838       11977 : FqV_red(GEN z, GEN T, GEN p)
     839             : {
     840       11977 :   long i, l = lg(z);
     841       11977 :   GEN res = cgetg(l, typ(z));
     842       11977 :   for(i=1;i<l;i++) gel(res,i) = Fq_red(gel(z,i),T,p);
     843       11977 :   return res;
     844             : }
     845             : 
     846             : GEN
     847           0 : FqC_add(GEN x, GEN y, GEN T, GEN p)
     848             : {
     849           0 :   long i, lx = lg(x);
     850             :   GEN z;
     851           0 :   if (!T) return FpC_add(x, y, p);
     852           0 :   z = cgetg(lx, t_COL);
     853           0 :   for (i = 1; i < lx; i++) gel(z, i) = Fq_add(gel(x, i), gel(y, i), T, p);
     854           0 :   return z;
     855             : }
     856             : 
     857             : GEN
     858           0 : FqC_sub(GEN x, GEN y, GEN T, GEN p)
     859             : {
     860           0 :   long i, lx = lg(x);
     861             :   GEN z;
     862           0 :   if (!T) return FpC_sub(x, y, p);
     863           0 :   z = cgetg(lx, t_COL);
     864           0 :   for (i = 1; i < lx; i++) gel(z, i) = Fq_sub(gel(x, i), gel(y, i), T, p);
     865           0 :   return z;
     866             : }
     867             : 
     868             : GEN
     869           0 : FqC_Fq_mul(GEN x, GEN y, GEN T, GEN p)
     870             : {
     871           0 :   long i, l = lg(x);
     872             :   GEN z;
     873           0 :   if (!T) return FpC_Fp_mul(x, y, p);
     874           0 :   z = cgetg(l, t_COL);
     875           0 :   for (i=1;i<l;i++) gel(z,i) = Fq_mul(gel(x,i),y,T,p);
     876           0 :   return z;
     877             : }
     878             : 
     879             : GEN
     880         549 : FqV_to_FlxV(GEN v, GEN T, GEN pp)
     881             : {
     882         549 :   long j, N = lg(v);
     883         549 :   long vT = evalvarn(get_FpX_var(T));
     884         549 :   ulong p = pp[2];
     885         549 :   GEN y = cgetg(N, t_VEC);
     886        2748 :   for (j=1; j<N; j++)
     887        4398 :     gel(y,j) = (typ(gel(v,j))==t_INT?  Z_to_Flx(gel(v,j), p, vT)
     888        2199 :                                     : ZX_to_Flx(gel(v,j), p));
     889         549 :   return y;
     890             : }
     891             : 
     892             : GEN
     893        1932 : FqC_to_FlxC(GEN v, GEN T, GEN pp)
     894             : {
     895        1932 :   long j, N = lg(v);
     896        1932 :   long vT = evalvarn(get_FpX_var(T));
     897        1932 :   ulong p = pp[2];
     898        1932 :   GEN y = cgetg(N, t_COL);
     899       37688 :   for (j=1; j<N; j++)
     900       82227 :     gel(y,j) = (typ(gel(v,j))==t_INT?  Z_to_Flx(gel(v,j), p, vT)
     901       46471 :                                     : ZX_to_Flx(gel(v,j), p));
     902        1932 :   return y;
     903             : }
     904             : 
     905             : GEN
     906         392 : FqM_to_FlxM(GEN x, GEN T, GEN pp)
     907             : {
     908         392 :   long j, n = lg(x);
     909         392 :   GEN y = cgetg(n,t_MAT);
     910         392 :   if (n == 1) return y;
     911        2324 :   for (j=1; j<n; j++)
     912        1932 :     gel(y,j) = FqC_to_FlxC(gel(x,j), T, pp);
     913         392 :   return y;
     914             : }
     915             : 
     916             : /*******************************************************************/
     917             : /*                                                                 */
     918             : /*                          MODULAR GCD                            */
     919             : /*                                                                 */
     920             : /*******************************************************************/
     921             : /* return z = a mod q, b mod p (p,q) = 1. qinv = 1/q mod p */
     922             : static GEN
     923     9964255 : Fl_chinese_coprime(GEN a, ulong b, GEN q, ulong p, ulong qinv, GEN pq)
     924             : {
     925     9964255 :   ulong d, amod = umodiu(a, p);
     926     9964255 :   pari_sp av = avma;
     927             :   GEN ax;
     928             : 
     929     9964255 :   if (b == amod) return NULL;
     930     2038708 :   d = (b > amod)? b - amod: p - (amod - b); /* (b - a) mod p */
     931     2038708 :   (void)new_chunk(lgefint(pq)<<1); /* HACK */
     932     2038708 :   ax = mului(Fl_mul(d,qinv,p), q); /* d mod p, 0 mod q */
     933     2038708 :   avma = av; return addii(a, ax); /* in ]-q, pq[ assuming a in -]-q,q[ */
     934             : }
     935             : GEN
     936       16870 : Z_init_CRT(ulong Hp, ulong p) { return stoi(Fl_center(Hp, p, p>>1)); }
     937             : GEN
     938     3113686 : ZX_init_CRT(GEN Hp, ulong p, long v)
     939             : {
     940     3113686 :   long i, l = lg(Hp), lim = (long)(p>>1);
     941     3113686 :   GEN H = cgetg(l, t_POL);
     942     3113686 :   H[1] = evalsigne(1) | evalvarn(v);
     943    11001369 :   for (i=2; i<l; i++)
     944     7887683 :     gel(H,i) = stoi(Fl_center(Hp[i], p, lim));
     945     3113686 :   return H;
     946             : }
     947             : 
     948             : /* assume lg(Hp) > 1 */
     949             : GEN
     950      106768 : ZM_init_CRT(GEN Hp, ulong p)
     951             : {
     952      106768 :   long i,j, m = lgcols(Hp), l = lg(Hp), lim = (long)(p>>1);
     953      106768 :   GEN c,cp,H = cgetg(l, t_MAT);
     954      875783 :   for (j=1; j<l; j++)
     955             :   {
     956      769015 :     cp = gel(Hp,j);
     957      769015 :     c = cgetg(m, t_COL);
     958      769015 :     gel(H,j) = c;
     959      769015 :     for (i=1; i<m; i++) gel(c,i) = stoi(Fl_center(cp[i],p, lim));
     960             :   }
     961      106768 :   return H;
     962             : }
     963             : 
     964             : int
     965       58129 : Z_incremental_CRT(GEN *H, ulong Hp, GEN *ptq, ulong p)
     966             : {
     967       58129 :   GEN h, q = *ptq, qp = muliu(q,p), lim = shifti(qp,-1);
     968       58129 :   ulong qinv = Fl_inv(umodiu(q,p), p);
     969       58129 :   int stable = 1;
     970       58129 :   h = Fl_chinese_coprime(*H,Hp,q,p,qinv,qp);
     971       58129 :   if (h)
     972             :   {
     973       14405 :     if (cmpii(h,lim) > 0) h = subii(h,qp);
     974       14405 :     *H = h; stable = 0;
     975             :   }
     976       58129 :   *ptq = qp; return stable;
     977             : }
     978             : 
     979             : static int
     980        5778 : ZX_incremental_CRT_raw(GEN *ptH, GEN Hp, GEN q, GEN qp, ulong p)
     981             : {
     982        5778 :   GEN H = *ptH, h, lim = shifti(qp,-1);
     983        5778 :   ulong qinv = Fl_inv(umodiu(q,p), p);
     984        5778 :   long i, l = lg(H), lp = lg(Hp);
     985        5778 :   int stable = 1;
     986             : 
     987        5778 :   if (l < lp)
     988             :   { /* degree increases */
     989           0 :     GEN x = cgetg(lp, t_POL);
     990           0 :     for (i=1; i<l; i++)  x[i] = H[i];
     991           0 :     for (   ; i<lp; i++) gel(x,i) = gen_0;
     992           0 :     *ptH = H = x;
     993           0 :     stable = 0;
     994        5778 :   } else if (l > lp)
     995             :   { /* degree decreases */
     996           0 :     GEN x = cgetg(l, t_VECSMALL);
     997           0 :     for (i=1; i<lp; i++)  x[i] = Hp[i];
     998           0 :     for (   ; i<l; i++) x[i] = 0;
     999           0 :     Hp = x; lp = l;
    1000             :   }
    1001      214219 :   for (i=2; i<lp; i++)
    1002             :   {
    1003      208441 :     h = Fl_chinese_coprime(gel(H,i),Hp[i],q,p,qinv,qp);
    1004      208441 :     if (h)
    1005             :     {
    1006       57084 :       if (cmpii(h,lim) > 0) h = subii(h,qp);
    1007       57084 :       gel(H,i) = h; stable = 0;
    1008             :     }
    1009             :   }
    1010        5778 :   return stable;
    1011             : }
    1012             : 
    1013             : int
    1014        4049 : ZX_incremental_CRT(GEN *ptH, GEN Hp, GEN *ptq, ulong p)
    1015             : {
    1016        4049 :   GEN q = *ptq, qp = muliu(q,p);
    1017        4049 :   int stable = ZX_incremental_CRT_raw(ptH, Hp, q, qp, p);
    1018        4049 :   *ptq = qp; return stable;
    1019             : }
    1020             : 
    1021             : int
    1022      112331 : ZM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
    1023             : {
    1024      112331 :   GEN h, H = *pH, q = *ptq, qp = muliu(q, p), lim = shifti(qp,-1);
    1025      112331 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1026      112331 :   long i,j, l = lg(H), m = lgcols(H);
    1027      112331 :   int stable = 1;
    1028      984877 :   for (j=1; j<l; j++)
    1029    10570231 :     for (i=1; i<m; i++)
    1030             :     {
    1031     9697685 :       h = Fl_chinese_coprime(gcoeff(H,i,j), coeff(Hp,i,j),q,p,qinv,qp);
    1032     9697685 :       if (h)
    1033             :       {
    1034     1967219 :         if (cmpii(h,lim) > 0) h = subii(h,qp);
    1035     1967219 :         gcoeff(H,i,j) = h; stable = 0;
    1036             :       }
    1037             :     }
    1038      112331 :   *ptq = qp; return stable;
    1039             : }
    1040             : 
    1041             : /* record the degrees of Euclidean remainders (make them as large as
    1042             :  * possible : smaller values correspond to a degenerate sequence) */
    1043             : static void
    1044        1561 : Flx_resultant_set_dglist(GEN a, GEN b, GEN dglist, ulong p)
    1045             : {
    1046             :   long da,db,dc, ind;
    1047        1561 :   pari_sp av = avma;
    1048             : 
    1049        1561 :   if (lgpol(a)==0 || lgpol(b)==0) return;
    1050        1561 :   da = degpol(a);
    1051        1561 :   db = degpol(b);
    1052        1561 :   if (db > da)
    1053           0 :   { swapspec(a,b, da,db); }
    1054        1561 :   else if (!da) return;
    1055        1561 :   ind = 0;
    1056        9814 :   while (db)
    1057             :   {
    1058        6692 :     GEN c = Flx_rem(a,b, p);
    1059        6692 :     a = b; b = c; dc = degpol(c);
    1060        6692 :     if (dc < 0) break;
    1061             : 
    1062        6692 :     ind++;
    1063        6692 :     if (dc > dglist[ind]) dglist[ind] = dc;
    1064        6692 :     if (gc_needed(av,2))
    1065             :     {
    1066           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1067           0 :       gerepileall(av, 2, &a,&b);
    1068             :     }
    1069        6692 :     db = dc; /* = degpol(b) */
    1070             :   }
    1071        1561 :   if (ind+1 > lg(dglist)) setlg(dglist,ind+1);
    1072        1561 :   avma = av; return;
    1073             : }
    1074             : /* assuming the PRS finishes on a degree 1 polynomial C0 + C1X, with
    1075             :  * "generic" degree sequence as given by dglist, set *Ci and return
    1076             :  * resultant(a,b). Modular version of Collins's subresultant */
    1077             : static ulong
    1078        6996 : Flx_resultant_all(GEN a, GEN b, long *C0, long *C1, GEN dglist, ulong p)
    1079             : {
    1080             :   long da,db,dc, ind;
    1081        6996 :   ulong lb, res, g = 1UL, h = 1UL, ca = 1UL, cb = 1UL;
    1082        6996 :   int s = 1;
    1083        6996 :   pari_sp av = avma;
    1084             : 
    1085        6996 :   *C0 = 1; *C1 = 0;
    1086        6996 :   if (lgpol(a)==0 || lgpol(b)==0) return 0;
    1087        6996 :   da = degpol(a);
    1088        6996 :   db = degpol(b);
    1089        6996 :   if (db > da)
    1090             :   {
    1091           0 :     swapspec(a,b, da,db);
    1092           0 :     if (both_odd(da,db)) s = -s;
    1093             :   }
    1094        6996 :   else if (!da) return 1; /* = a[2] ^ db, since 0 <= db <= da = 0 */
    1095        6996 :   ind = 0;
    1096       40727 :   while (db)
    1097             :   { /* sub-resultant algo., applied to ca * a and cb * b, ca,cb scalars,
    1098             :      * da = deg a, db = deg b */
    1099       27099 :     GEN c = Flx_rem(a,b, p);
    1100       27099 :     long delta = da - db;
    1101             : 
    1102       27099 :     if (both_odd(da,db)) s = -s;
    1103       27099 :     lb = Fl_mul(b[db+2], cb, p);
    1104       27099 :     a = b; b = c; dc = degpol(c);
    1105       27099 :     ind++;
    1106       27099 :     if (dc != dglist[ind]) { avma = av; return 0; } /* degenerates */
    1107       26735 :     if (g == h)
    1108             :     { /* frequent */
    1109       24621 :       ulong cc = Fl_mul(ca, Fl_powu(Fl_div(lb,g,p), delta+1, p), p);
    1110       24621 :       ca = cb;
    1111       24621 :       cb = cc;
    1112             :     }
    1113             :     else
    1114             :     {
    1115        2114 :       ulong cc = Fl_mul(ca, Fl_powu(lb, delta+1, p), p);
    1116        2114 :       ulong ghdelta = Fl_mul(g, Fl_powu(h, delta, p), p);
    1117        2114 :       ca = cb;
    1118        2114 :       cb = Fl_div(cc, ghdelta, p);
    1119             :     }
    1120       26735 :     da = db; /* = degpol(a) */
    1121       26735 :     db = dc; /* = degpol(b) */
    1122             : 
    1123       26735 :     g = lb;
    1124       26735 :     if (delta == 1)
    1125       17482 :       h = g; /* frequent */
    1126             :     else
    1127        9253 :       h = Fl_mul(h, Fl_powu(Fl_div(g,h,p), delta, p), p);
    1128             : 
    1129       26735 :     if (gc_needed(av,2))
    1130             :     {
    1131           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1132           0 :       gerepileall(av, 2, &a,&b);
    1133             :     }
    1134             :   }
    1135        6632 :   if (da > 1) return 0; /* Failure */
    1136             :   /* last non-constant polynomial has degree 1 */
    1137        6632 :   *C0 = Fl_mul(ca, a[2], p);
    1138        6632 :   *C1 = Fl_mul(ca, a[3], p);
    1139        6632 :   res = Fl_mul(cb, b[2], p);
    1140        6632 :   if (s == -1) res = p - res;
    1141        6632 :   avma = av; return res;
    1142             : }
    1143             : 
    1144             : /* Q a vector of polynomials representing B in Fp[X][Y], evaluate at X = x,
    1145             :  * Return 0 in case of degree drop. */
    1146             : static GEN
    1147        8557 : FlxY_evalx_drop(GEN Q, ulong x, ulong p)
    1148             : {
    1149             :   GEN z;
    1150        8557 :   long i, lb = lg(Q);
    1151        8557 :   ulong leadz = Flx_eval(leading_coeff(Q), x, p);
    1152        8557 :   long vs=mael(Q,2,1);
    1153        8557 :   if (!leadz) return zero_Flx(vs);
    1154             : 
    1155        8557 :   z = cgetg(lb, t_VECSMALL); z[1] = vs;
    1156        8557 :   for (i=2; i<lb-1; i++) z[i] = Flx_eval(gel(Q,i), x, p);
    1157        8557 :   z[i] = leadz; return z;
    1158             : }
    1159             : 
    1160             : GEN
    1161       17836 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
    1162             : {
    1163       17836 :   pari_sp av = avma;
    1164       17836 :   long i, lb = lg(Q);
    1165             :   GEN z;
    1166       17836 :   if (!T) return FpXY_evaly(Q, y, p, vx);
    1167        1148 :   if (lb == 2) return pol_0(vx);
    1168        1148 :   z = gel(Q, lb-1);
    1169        1148 :   if (lb == 3 || !signe(y)) return typ(z)==t_INT? scalar_ZX(z, vx): ZX_copy(z);
    1170             : 
    1171        1148 :   if (typ(z) == t_INT) z = scalar_ZX_shallow(z, vx);
    1172       28084 :   for (i=lb-2; i>=2; i--)
    1173             :   {
    1174       26936 :     GEN c = gel(Q,i);
    1175       26936 :     z = FqX_Fq_mul(z, y, T, p);
    1176       26936 :     z = typ(c) == t_INT? FqX_Fq_add(z,c,T,p): FqX_add(z,c,T,p);
    1177             :   }
    1178        1148 :   return gerepileupto(av, z);
    1179             : }
    1180             : 
    1181             : static GEN
    1182        5607 : ZX_norml1(GEN x)
    1183             : {
    1184        5607 :   long i, l = lg(x);
    1185             :   GEN s;
    1186             : 
    1187        5607 :   if (l == 2) return gen_0;
    1188        5607 :   s = gel(x, l-1); /* != 0 */
    1189       31143 :   for (i = l-2; i > 1; i--) {
    1190       25536 :     GEN xi = gel(x,i);
    1191       25536 :     if (!signe(x)) continue;
    1192       25536 :     s = addii_sign(s,1, xi,1);
    1193             :   }
    1194        5607 :   return s;
    1195             : }
    1196             : 
    1197             : /* Interpolate at roots of 1 and use Hadamard bound for univariate resultant:
    1198             :  *   bound = N_2(A)^degpol B N_2(B)^degpol(A),  where
    1199             :  *     N_2(A) = sqrt(sum (N_1(Ai))^2)
    1200             :  * Return e such that Res(A, B) < 2^e */
    1201             : ulong
    1202      579035 : ZX_ZXY_ResBound(GEN A, GEN B, GEN dB)
    1203             : {
    1204      579035 :   pari_sp av = avma;
    1205      579035 :   GEN a = gen_0, b = gen_0;
    1206      579035 :   long i , lA = lg(A), lB = lg(B);
    1207             :   double loga, logb;
    1208      579035 :   for (i=2; i<lA; i++) a = addii(a, sqri(gel(A,i)));
    1209     2047594 :   for (i=2; i<lB; i++)
    1210             :   {
    1211     1468559 :     GEN t = gel(B,i);
    1212     1468559 :     if (typ(t) == t_POL) t = ZX_norml1(t);
    1213     1468559 :     b = addii(b, sqri(t));
    1214             :   }
    1215      579035 :   loga = dbllog2(a);
    1216      579035 :   logb = dbllog2(b); if (dB) logb -= 2 * dbllog2(dB);
    1217      579035 :   i = (long)((degpol(B) * loga + degpol(A) * logb) / 2);
    1218      579035 :   avma = av; return (i <= 0)? 1: 1 + (ulong)i;
    1219             : }
    1220             : 
    1221             : /* return Res(a(Y), b(n,Y)) over Fp. la = leading_coeff(a) [for efficiency] */
    1222             : static ulong
    1223      198209 : Flx_FlxY_eval_resultant(GEN a, GEN b, ulong n, ulong p, ulong la)
    1224             : {
    1225      198209 :   GEN ev = FlxY_evalx(b, n, p);
    1226      198209 :   long drop = lg(b) - lg(ev);
    1227      198209 :   ulong r = Flx_resultant(a, ev, p);
    1228      198209 :   if (drop && la != 1) r = Fl_mul(r, Fl_powu(la, drop,p),p);
    1229      198209 :   return r;
    1230             : }
    1231             : static GEN
    1232           4 : FpX_FpXY_eval_resultant(GEN a, GEN b, GEN n, GEN p, GEN la, long db, long vX)
    1233             : {
    1234           4 :   GEN ev = FpXY_evaly(b, n, p, vX);
    1235           4 :   long drop = db-degpol(ev);
    1236           4 :   GEN r = FpX_resultant(a, ev, p);
    1237           4 :   if (drop && !gequal1(la)) r = Fp_mul(r, Fp_powu(la, drop,p),p);
    1238           4 :   return r;
    1239             : }
    1240             : 
    1241             : /* assume dres := deg(Res_X(a,b), Y) <= deg(a,X) * deg(b,Y) < p */
    1242             : /* Return a Fly */
    1243             : static GEN
    1244        4516 : Flx_FlxY_resultant_polint(GEN a, GEN b, ulong p, ulong dres, long sx)
    1245             : {
    1246        4516 :   ulong i, n, la = Flx_lead(a);
    1247        4516 :   GEN  x = cgetg(dres+2, t_VECSMALL);
    1248        4516 :   GEN  y = cgetg(dres+2, t_VECSMALL);
    1249             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1250             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1251      101592 :   for (i=0,n = 1; i < dres; n++)
    1252             :   {
    1253       97076 :     x[++i] = n;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1254       97076 :     x[++i] = p-n; y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1255             :   }
    1256        4516 :   if (i == dres)
    1257             :   {
    1258        4057 :     x[++i] = 0;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1259             :   }
    1260        4516 :   return Flv_polint(x,y, p, sx);
    1261             : }
    1262             : 
    1263             : static GEN
    1264        4691 : FlxX_pseudorem(GEN x, GEN y, ulong p)
    1265             : {
    1266        4691 :   long vx = varn(x), dx, dy, dz, i, lx, dp;
    1267        4691 :   pari_sp av = avma, av2;
    1268             : 
    1269        4691 :   if (!signe(y)) pari_err_INV("FlxX_pseudorem",y);
    1270        4691 :   (void)new_chunk(2);
    1271        4692 :   dx=degpol(x); x = RgX_recip_shallow(x)+2;
    1272        4693 :   dy=degpol(y); y = RgX_recip_shallow(y)+2; dz=dx-dy; dp = dz+1;
    1273        4698 :   av2 = avma;
    1274             :   for (;;)
    1275             :   {
    1276       33489 :     gel(x,0) = Flx_neg(gel(x,0), p); dp--;
    1277      131852 :     for (i=1; i<=dy; i++)
    1278      194370 :       gel(x,i) = Flx_add( Flx_mul(gel(y,0), gel(x,i), p),
    1279       97185 :                               Flx_mul(gel(x,0), gel(y,i), p), p );
    1280      549761 :     for (   ; i<=dx; i++)
    1281      516273 :       gel(x,i) = Flx_mul(gel(y,0), gel(x,i), p);
    1282       35389 :     do { x++; dx--; } while (dx >= 0 && lg(gel(x,0))==2);
    1283       33488 :     if (dx < dy) break;
    1284       28789 :     if (gc_needed(av2,1))
    1285             :     {
    1286           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_pseudorem dx = %ld >= %ld",dx,dy);
    1287           0 :       gerepilecoeffs(av2,x,dx+1);
    1288             :     }
    1289       28791 :   }
    1290        4699 :   if (dx < 0) return zero_Flx(0);
    1291        4699 :   lx = dx+3; x -= 2;
    1292        4699 :   x[0]=evaltyp(t_POL) | evallg(lx);
    1293        4700 :   x[1]=evalsigne(1) | evalvarn(vx);
    1294        4700 :   x = RgX_recip_shallow(x);
    1295        4700 :   if (dp)
    1296             :   { /* multiply by y[0]^dp   [beware dummy vars from FpX_FpXY_resultant] */
    1297         972 :     GEN t = Flx_powu(gel(y,0), dp, p);
    1298        3889 :     for (i=2; i<lx; i++)
    1299        2917 :       gel(x,i) = Flx_mul(gel(x,i), t, p);
    1300             :   }
    1301        4700 :   return gerepilecopy(av, x);
    1302             : }
    1303             : 
    1304             : /* return a Flx */
    1305             : GEN
    1306        1521 : FlxX_resultant(GEN u, GEN v, ulong p, long sx)
    1307             : {
    1308        1521 :   pari_sp av = avma, av2;
    1309             :   long degq,dx,dy,du,dv,dr,signh;
    1310             :   GEN z,g,h,r,p1;
    1311             : 
    1312        1521 :   dx=degpol(u); dy=degpol(v); signh=1;
    1313        1520 :   if (dx < dy)
    1314             :   {
    1315          28 :     swap(u,v); lswap(dx,dy);
    1316          28 :     if (both_odd(dx, dy)) signh = -signh;
    1317             :   }
    1318        1520 :   if (dy < 0) return zero_Flx(sx);
    1319        1520 :   if (dy==0) return gerepileupto(av, Flx_powu(gel(v,2),dx,p));
    1320             : 
    1321        1520 :   g = h = pol1_Flx(sx); av2 = avma;
    1322             :   for(;;)
    1323             :   {
    1324        4695 :     r = FlxX_pseudorem(u,v,p); dr = lg(r);
    1325        4695 :     if (dr == 2) { avma = av; return zero_Flx(sx); }
    1326        4695 :     du = degpol(u); dv = degpol(v); degq = du-dv;
    1327        4698 :     u = v; p1 = g; g = leading_coeff(u);
    1328        4700 :     switch(degq)
    1329             :     {
    1330           0 :       case 0: break;
    1331             :       case 1:
    1332        3476 :         p1 = Flx_mul(h,p1, p); h = g; break;
    1333             :       default:
    1334        1224 :         p1 = Flx_mul(Flx_powu(h,degq,p), p1, p);
    1335        1225 :         h = Flx_div(Flx_powu(g,degq,p), Flx_powu(h,degq-1,p), p);
    1336             :     }
    1337        4695 :     if (both_odd(du,dv)) signh = -signh;
    1338        4694 :     v = FlxY_Flx_div(r, p1, p);
    1339        4696 :     if (dr==3) break;
    1340        3173 :     if (gc_needed(av2,1))
    1341             :     {
    1342           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"resultant_all, dr = %ld",dr);
    1343           0 :       gerepileall(av2,4, &u, &v, &g, &h);
    1344             :     }
    1345        3172 :   }
    1346        1523 :   z = gel(v,2);
    1347        1523 :   if (dv > 1) z = Flx_div(Flx_powu(z,dv,p), Flx_powu(h,dv-1,p), p);
    1348        1523 :   if (signh < 0) z = Flx_neg(z,p);
    1349        1523 :   return gerepileupto(av, z);
    1350             : }
    1351             : 
    1352             : /* Warning:
    1353             :  * This function switches between valid and invalid variable ordering*/
    1354             : 
    1355             : static GEN
    1356        1629 : FlxY_to_FlyX(GEN b, long sv)
    1357             : {
    1358        1629 :   long i, n=-1;
    1359        1629 :   long sw = b[1]&VARNBITS;
    1360        1629 :   for(i=2;i<lg(b);i++) n = maxss(n,lgpol(gel(b,i)));
    1361        1627 :   return Flm_to_FlxX(Flm_transpose(FlxX_to_Flm(b,n)),sv,sw);
    1362             : }
    1363             : 
    1364             : /* Return a Fly*/
    1365             : GEN
    1366        1624 : Flx_FlxY_resultant(GEN a, GEN b, ulong pp)
    1367             : {
    1368        1624 :   pari_sp ltop=avma;
    1369        1624 :   long dres = degpol(a)*degpol(b);
    1370        1627 :   long sx=a[1], sy=b[1]&VARNBITS;
    1371             :   GEN z;
    1372        1627 :   b = FlxY_to_FlyX(b,sx);
    1373        1628 :   if ((ulong)dres >= pp)
    1374        1524 :     z = FlxX_resultant(Fly_to_FlxY(a, sy), b, pp, sx);
    1375             :   else
    1376         104 :     z = Flx_FlxY_resultant_polint(a, b, pp, (ulong)dres, sy);
    1377        1629 :   return gerepileupto(ltop,z);
    1378             : }
    1379             : 
    1380             : /* return a t_POL (in variable v >= 0) whose coeffs are the coeffs of b,
    1381             :  * in variable v. This is an incorrect PARI object if initially varn(b) << v.
    1382             :  * We could return a vector of coeffs, but it is convenient to have degpol()
    1383             :  * and friends available. Even in that case, it will behave nicely with all
    1384             :  * functions treating a polynomial as a vector of coeffs (eg poleval).
    1385             :  * FOR INTERNAL USE! */
    1386             : GEN
    1387        5096 : swap_vars(GEN b0, long v)
    1388             : {
    1389        5096 :   long i, n = RgX_degree(b0, v);
    1390             :   GEN b, x;
    1391        5096 :   if (n < 0) return pol_0(v);
    1392        5096 :   b = cgetg(n+3, t_POL); x = b + 2;
    1393        5096 :   b[1] = evalsigne(1) | evalvarn(v);
    1394        5096 :   for (i=0; i<=n; i++) gel(x,i) = polcoeff_i(b0, i, v);
    1395        5096 :   return b;
    1396             : }
    1397             : 
    1398             : /* assume varn(b) << varn(a) */
    1399             : /* return a FpY*/
    1400             : GEN
    1401        1598 : FpX_FpXY_resultant(GEN a, GEN b, GEN p)
    1402             : {
    1403        1598 :   long i,n,dres, db, vY = varn(b), vX = varn(a);
    1404             :   GEN la,x,y;
    1405             : 
    1406        1598 :   if (lgefint(p) == 3)
    1407             :   {
    1408        1597 :     ulong pp = uel(p,2);
    1409        1597 :     b = ZXX_to_FlxX(b, pp, vX);
    1410        1599 :     a = ZX_to_Flx(a, pp);
    1411        1597 :     x = Flx_FlxY_resultant(a, b, pp);
    1412        1601 :     return Flx_to_ZX(x);
    1413             :   }
    1414           1 :   db = RgXY_degreex(b);
    1415           1 :   dres = degpol(a)*degpol(b);
    1416           1 :   la = leading_coeff(a);
    1417           1 :   x = cgetg(dres+2, t_VEC);
    1418           1 :   y = cgetg(dres+2, t_VEC);
    1419             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1420             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1421           3 :   for (i=0,n = 1; i < dres; n++)
    1422             :   {
    1423           2 :     gel(x,++i) = utoipos(n);
    1424           2 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1425           2 :     gel(x,++i) = subis(p,n);
    1426           2 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1427             :   }
    1428           1 :   if (i == dres)
    1429             :   {
    1430           0 :     gel(x,++i) = gen_0;
    1431           0 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b, gel(x,i), p,la,db,vY);
    1432             :   }
    1433           1 :   return FpV_polint(x,y, p, vY);
    1434             : }
    1435             : 
    1436             : GEN
    1437         434 : FpX_direct_compositum(GEN a, GEN b, GEN p)
    1438             : {
    1439         434 :   GEN x = deg1pol_shallow(gen_1, pol_x(varn(a)), fetch_var_higher()); /* x+y */
    1440         434 :   x = FpX_FpXY_resultant(a, poleval(b,x),p);
    1441         434 :   (void)delete_var(); return x;
    1442             : }
    1443             : 
    1444             : /* 0, 1, -1, 2, -2, ... */
    1445             : #define next_lambda(a) (a>0 ? -a : 1-a)
    1446             : GEN
    1447           0 : FpX_compositum(GEN a, GEN b, GEN p)
    1448             : {
    1449           0 :   long k, v = fetch_var_higher();
    1450           0 :   for (k = 1;; k = next_lambda(k))
    1451             :   {
    1452           0 :     GEN x = deg1pol_shallow(gen_1, gmulsg(k, pol_x(v)), 0); /* x + k y */
    1453           0 :     GEN C = FpX_FpXY_resultant(a, poleval(b,x),p);
    1454           0 :     if (FpX_is_squarefree(C, p)) { (void)delete_var(); return C; }
    1455           0 :   }
    1456             : }
    1457             : 
    1458             : /* Assume A in Z[Y], B in Q[Y][X], and Res_Y(A, B) in Z[X].
    1459             :  * If lambda = NULL, return Res_Y(A,B).
    1460             :  * Otherwise, find a small lambda (start from *lambda, use the sequence above)
    1461             :  * such that R(X) = Res_Y(A(Y), B(X + lambda Y)) is squarefree, reset *lambda
    1462             :  * to the chosen value and return R
    1463             :  *
    1464             :  * If LERS is non-NULL, set it to the Last non-constant polynomial in the
    1465             :  * Euclidean Remainder Sequence */
    1466             : GEN
    1467        3892 : ZX_ZXY_resultant_all(GEN A, GEN B0, long *plambda, GEN *LERS)
    1468             : {
    1469        3892 :   int checksqfree = plambda? 1: 0, stable;
    1470        3892 :   long lambda = plambda? *plambda: 0, cnt = 0;
    1471             :   ulong bound, dp;
    1472        3892 :   pari_sp av = avma, av2 = 0;
    1473        3892 :   long i,n, lb, degA = degpol(A), dres = degA*degpol(B0);
    1474        3892 :   long v = fetch_var_higher();
    1475        3892 :   long vX = varn(B0), vY = varn(A); /* assume vY has lower priority */
    1476        3892 :   long sX = evalvarn(vX);
    1477             :   GEN x, y, dglist, dB, B, q, a, b, ev, H, H0, H1, Hp, H0p, H1p, C0, C1;
    1478             :   forprime_t S;
    1479             : 
    1480        3892 :   dglist = Hp = H0p = H1p = C0 = C1 = NULL; /* gcc -Wall */
    1481        3892 :   if (LERS)
    1482             :   {
    1483        1127 :     if (!checksqfree)
    1484           0 :       pari_err_BUG("ZX_ZXY_resultant_all [LERS != NULL needs lambda]");
    1485        1127 :     C0 = cgetg(dres+2, t_VECSMALL);
    1486        1127 :     C1 = cgetg(dres+2, t_VECSMALL);
    1487        1127 :     dglist = cgetg(dres+1, t_VECSMALL);
    1488             :   }
    1489        3892 :   x = cgetg(dres+2, t_VECSMALL);
    1490        3892 :   y = cgetg(dres+2, t_VECSMALL);
    1491        3892 :   B0 = Q_remove_denom(B0, &dB);
    1492        3892 :   if (!dB) B0 = leafcopy(B0);
    1493        3892 :   A = leafcopy(A);
    1494        3892 :   B = B0;
    1495        3892 :   setvarn(A,v);
    1496             :   /* make sure p large enough */
    1497             : INIT:
    1498             :   /* always except the first time */
    1499        5061 :   if (av2) { avma = av2; lambda = next_lambda(lambda); }
    1500        5061 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    1501        5061 :   B = swap_vars(B, vY); setvarn(B,v);
    1502             :   /* B0(lambda v + x, v) */
    1503        5061 :   if (DEBUGLEVEL>4 && checksqfree) err_printf("Trying lambda = %ld\n", lambda);
    1504        5061 :   av2 = avma;
    1505             : 
    1506        5061 :   if (degA <= 3)
    1507             :   { /* sub-resultant faster for small degrees */
    1508        3437 :     if (LERS)
    1509             :     { /* implies checksqfree */
    1510        1582 :       H = resultant_all(A,B,&q);
    1511        1582 :       if (typ(q) != t_POL || degpol(q)!=1) goto INIT;
    1512         980 :       H0 = gel(q,2);
    1513         980 :       if (typ(H0) == t_POL) setvarn(H0,vX); else H0 = scalarpol(H0,vX);
    1514         980 :       H1 = gel(q,3);
    1515         980 :       if (typ(H1) == t_POL) setvarn(H1,vX); else H1 = scalarpol(H1,vX);
    1516             :     }
    1517             :     else
    1518        1855 :       H = resultant(A,B);
    1519        2835 :     if (checksqfree && !ZX_is_squarefree(H)) goto INIT;
    1520        2660 :     goto END;
    1521             :   }
    1522             : 
    1523        1624 :   H = H0 = H1 = NULL;
    1524        1624 :   lb = lg(B);
    1525        1624 :   bound = ZX_ZXY_ResBound(A, B, dB);
    1526        1624 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    1527        1624 :   dp = 1;
    1528        1624 :   init_modular_big(&S);
    1529             :   while (1)
    1530             :   {
    1531        4809 :     ulong p = u_forprime_next(&S);
    1532        4809 :     if (dB) { dp = umodiu(dB, p); if (!dp) continue; }
    1533             : 
    1534        4809 :     a = ZX_to_Flx(A, p);
    1535        4809 :     b = ZXX_to_FlxX(B, p, varn(A));
    1536        4809 :     if (LERS)
    1537             :     {
    1538             :       GEN Hi;
    1539         397 :       if (degpol(a) < degA || lg(b) < lb) continue; /* p | lc(A)lc(B) */
    1540         397 :       if (checksqfree)
    1541             :       { /* find degree list for generic Euclidean Remainder Sequence */
    1542         196 :         long goal = minss(degpol(a), degpol(b)); /* longest possible */
    1543         196 :         for (n=1; n <= goal; n++) dglist[n] = 0;
    1544         196 :         setlg(dglist, 1);
    1545        1645 :         for (n=0; n <= dres; n++)
    1546             :         {
    1547        1561 :           ev = FlxY_evalx_drop(b, n, p);
    1548        1561 :           Flx_resultant_set_dglist(a, ev, dglist, p);
    1549        1561 :           if (lg(dglist)-1 == goal) break;
    1550             :         }
    1551             :         /* last pol in ERS has degree > 1 ? */
    1552         196 :         goal = lg(dglist)-1;
    1553         196 :         if (degpol(B) == 1) { if (!goal) goto INIT; }
    1554             :         else
    1555             :         {
    1556         189 :           if (goal <= 1) goto INIT;
    1557         182 :           if (dglist[goal] != 0 || dglist[goal-1] != 1) goto INIT;
    1558             :         }
    1559         189 :         if (DEBUGLEVEL>4)
    1560           0 :           err_printf("Degree list for ERS (trials: %ld) = %Ps\n",n+1,dglist);
    1561             :       }
    1562             : 
    1563        7386 :       for (i=0,n = 0; i <= dres; n++)
    1564             :       {
    1565        6996 :         ev = FlxY_evalx_drop(b, n, p);
    1566        6996 :         x[++i] = n; y[i] = Flx_resultant_all(a, ev, C0+i, C1+i, dglist, p);
    1567        6996 :         if (!C1[i]) i--; /* C1(i) = 0. No way to recover C0(i) */
    1568             :       }
    1569         390 :       Hi = Flv_Flm_polint(x, mkvec3(y,C0,C1), p, 0);
    1570         390 :       Hp = gel(Hi,1); H0p = gel(Hi,2); H1p = gel(Hi,3);
    1571             :     }
    1572             :     else
    1573             :     {
    1574        4412 :       long dropa = degA - degpol(a), dropb = lb - lg(b);
    1575        4412 :       Hp = Flx_FlxY_resultant_polint(a, b, p, (ulong)dres, sX);
    1576        4412 :       if (dropa && dropb)
    1577           0 :         Hp = zero_Flx(sX);
    1578             :       else {
    1579        4412 :         if (dropa)
    1580             :         { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    1581           0 :           GEN c = gel(b,lb-1); /* lc(B) */
    1582           0 :           if (!odd(lb)) c = Flx_neg(c, p); /* deg B = lb - 3 */
    1583           0 :           if (!Flx_equal1(c)) {
    1584           0 :             c = Flx_powu(c, dropa, p);
    1585           0 :             if (!Flx_equal1(c)) Hp = Flx_mul(Hp, c, p);
    1586             :           }
    1587             :         }
    1588        4412 :         else if (dropb)
    1589             :         { /* multiply by lc(A)^(deg B - deg b) */
    1590           0 :           ulong c = a[degA+2]; /* lc(A) */
    1591           0 :           c = Fl_powu(c, dropb, p);
    1592           0 :           if (c != 1) Hp = Flx_Fl_mul(Hp, c, p);
    1593             :         }
    1594             :       }
    1595             :     }
    1596        4802 :     if (!H && degpol(Hp) != dres) continue;
    1597        4802 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1598        4802 :     if (checksqfree) {
    1599        1071 :       if (!Flx_is_squarefree(Hp, p)) goto INIT;
    1600         686 :       if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    1601         686 :       checksqfree = 0;
    1602             :     }
    1603             : 
    1604        4417 :     if (!H)
    1605             :     { /* initialize */
    1606        1232 :       q = utoipos(p); stable = 0;
    1607        1232 :       H = ZX_init_CRT(Hp, p,vX);
    1608        1232 :       if (LERS) {
    1609         189 :         H0= ZX_init_CRT(H0p, p,vX);
    1610         189 :         H1= ZX_init_CRT(H1p, p,vX);
    1611             :       }
    1612             :     }
    1613             :     else
    1614             :     {
    1615        3185 :       if (LERS) {
    1616         201 :         GEN qp = muliu(q,p);
    1617         402 :         stable  = ZX_incremental_CRT_raw(&H, Hp, q,qp, p)
    1618         201 :                 & ZX_incremental_CRT_raw(&H0,H0p, q,qp, p)
    1619         201 :                 & ZX_incremental_CRT_raw(&H1,H1p, q,qp, p);
    1620         201 :         q = qp;
    1621             :       }
    1622             :       else
    1623        2984 :         stable = ZX_incremental_CRT(&H, Hp, &q, p);
    1624             :     }
    1625             :     /* could make it probabilistic for H ? [e.g if stable twice, etc]
    1626             :      * Probabilistic anyway for H0, H1 */
    1627        4417 :     if (DEBUGLEVEL>5 && (stable ||  ++cnt==100))
    1628           0 :     { cnt=0; err_printf("%ld%%%s ",100*expi(q)/bound,stable?"s":""); }
    1629        4417 :     if (stable && (ulong)expi(q) >= bound) break; /* DONE */
    1630        3185 :     if (gc_needed(av,2))
    1631             :     {
    1632           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_rnfequation");
    1633           0 :       gerepileall(av2, LERS? 4: 2, &H, &q, &H0, &H1);
    1634             :     }
    1635        3185 :   }
    1636             : END:
    1637        3892 :   if (DEBUGLEVEL>5) err_printf(" done\n");
    1638        3892 :   setvarn(H, vX); (void)delete_var();
    1639        3892 :   if (plambda) *plambda = lambda;
    1640        3892 :   if (LERS)
    1641             :   {
    1642        1127 :     *LERS = mkvec2(H0,H1);
    1643        1127 :     gerepileall(av, 2, &H, LERS);
    1644        1127 :     return H;
    1645             :   }
    1646        2765 :   return gerepilecopy(av, H);
    1647             : }
    1648             : 
    1649             : GEN
    1650        2086 : ZX_ZXY_rnfequation(GEN A, GEN B, long *lambda)
    1651             : {
    1652        2086 :   return ZX_ZXY_resultant_all(A, B, lambda, NULL);
    1653             : }
    1654             : 
    1655             : /* If lambda = NULL, return caract(Mod(A, T)), T,A in Z[X].
    1656             :  * Otherwise find a small lambda such that caract (Mod(A + lambda X, T)) is
    1657             :  * squarefree */
    1658             : GEN
    1659        1855 : ZXQ_charpoly_sqf(GEN A, GEN T, long *lambda, long v)
    1660             : {
    1661        1855 :   pari_sp av = avma;
    1662             :   GEN R, a;
    1663             :   long dA;
    1664             :   int delvar;
    1665             : 
    1666        1855 :   if (v < 0) v = 0;
    1667        1855 :   switch (typ(A))
    1668             :   {
    1669        1855 :     case t_POL: dA = degpol(A); if (dA > 0) break;
    1670           0 :       A = constant_coeff(A);
    1671             :     default:
    1672           0 :       if (lambda) { A = scalar_ZX_shallow(A,varn(T)); dA = 0; break;}
    1673           0 :       return gerepileupto(av, gpowgs(gsub(pol_x(v), A), degpol(T)));
    1674             :   }
    1675        1855 :   delvar = 0;
    1676        1855 :   if (varn(T) == 0)
    1677             :   {
    1678        1799 :     long v0 = fetch_var(); delvar = 1;
    1679        1799 :     T = leafcopy(T); setvarn(T,v0);
    1680        1799 :     A = leafcopy(A); setvarn(A,v0);
    1681             :   }
    1682        1855 :   R = ZX_ZXY_rnfequation(T, deg1pol_shallow(gen_1, gneg_i(A), 0), lambda);
    1683        1855 :   if (delvar) (void)delete_var();
    1684        1855 :   setvarn(R, v); a = leading_coeff(T);
    1685        1855 :   if (!gequal1(a)) R = gdiv(R, powiu(a, dA));
    1686        1855 :   return gerepileupto(av, R);
    1687             : }
    1688             : 
    1689             : 
    1690             : /* charpoly(Mod(A,T)), A may be in Q[X], but assume T and result are integral */
    1691             : GEN
    1692       55964 : ZXQ_charpoly(GEN A, GEN T, long v)
    1693             : {
    1694       55964 :   return (degpol(T) < 16) ? RgXQ_charpoly(A,T,v): ZXQ_charpoly_sqf(A,T, NULL, v);
    1695             : }
    1696             : 
    1697             : GEN
    1698       46844 : QXQ_charpoly(GEN A, GEN T, long v)
    1699             : {
    1700       46844 :   pari_sp av = avma;
    1701       46844 :   GEN den, B = Q_remove_denom(A, &den);
    1702       46844 :   GEN P = ZXQ_charpoly(B, T, v);
    1703       46844 :   return gerepilecopy(av, den ? RgX_rescale(P, ginv(den)): P);
    1704             : }
    1705             : 
    1706             : static GEN
    1707     1164013 : trivial_case(GEN A, GEN B)
    1708             : {
    1709             :   long d;
    1710     1164013 :   if (typ(A) == t_INT) return powiu(A, degpol(B));
    1711     1161576 :   d = degpol(A);
    1712     1161576 :   if (d == 0) return trivial_case(gel(A,2),B);
    1713     1160371 :   if (d < 0) return gen_0;
    1714     1160356 :   return NULL;
    1715             : }
    1716             : 
    1717             : static long
    1718      579603 : get_nbprimes(ulong bound, ulong *pt_start)
    1719             : {
    1720             : #ifdef LONG_IS_64BIT
    1721      495159 :   ulong pstart = 4611686018427388039UL;
    1722             : #else
    1723       84444 :   ulong pstart = 1073741827UL;
    1724             : #endif
    1725      579603 :   *pt_start = pstart;
    1726      579603 :   return (bound/expu(pstart))+1;
    1727             : }
    1728             : 
    1729             : static ulong
    1730      989128 : ZX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, ulong p)
    1731             : {
    1732      989128 :   pari_sp av = avma;
    1733             :   ulong H;
    1734             :   long dropa, dropb;
    1735      989128 :   ulong dp = dB ? umodiu(dB, p): 1;
    1736      989158 :   if (!b) b = Flx_deriv(a, p);
    1737      989210 :   dropa = degA - degpol(a);
    1738      989211 :   dropb = degB - degpol(b);
    1739      989221 :   if (dropa && dropb) /* p | lc(A), p | lc(B) */
    1740           0 :   { avma = av; return 0; }
    1741      989221 :   H = Flx_resultant(a, b, p);
    1742      989187 :   if (dropa)
    1743             :   { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    1744           0 :     ulong c = b[degB+2]; /* lc(B) */
    1745           0 :     if (odd(degB)) c = p - c;
    1746           0 :     c = Fl_powu(c, dropa, p);
    1747           0 :     if (c != 1) H = Fl_mul(H, c, p);
    1748             :   }
    1749      989187 :   else if (dropb)
    1750             :   { /* multiply by lc(A)^(deg B - deg b) */
    1751           0 :     ulong c = a[degA+2]; /* lc(A) */
    1752           0 :     c = Fl_powu(c, dropb, p);
    1753           0 :     if (c != 1) H = Fl_mul(H, c, p);
    1754             :   }
    1755      989184 :   if (dp != 1) H = Fl_mul(H, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1756      989183 :   avma = av; return H;
    1757             : }
    1758             : 
    1759             : /* If B=NULL, assume B=A' */
    1760             : static GEN
    1761      870987 : ZX_resultant_slice(GEN A, GEN B, GEN dB, GEN P, GEN *mod)
    1762             : {
    1763      870987 :   pari_sp av = avma;
    1764      870987 :   long degA, degB, i, n = lg(P)-1;
    1765             :   GEN H, T;
    1766             : 
    1767      870987 :   degA = degpol(A);
    1768      870999 :   degB = B ? degpol(B): degA - 1;
    1769      871010 :   if (n == 1)
    1770             :   {
    1771      823452 :     ulong Hp, p = uel(P,1);
    1772             :     GEN a, b;
    1773      823452 :     a = ZX_to_Flx(A, p), b = B ? ZX_to_Flx(B, p): NULL;
    1774      823461 :     Hp = ZX_resultant_prime(a, b, dB, degA, degB, p);
    1775      823485 :     avma = av;
    1776      823485 :     *mod = utoi(p); return utoi(Hp);
    1777             :   }
    1778       47558 :   T = ZV_producttree(P);
    1779       47560 :   A = ZX_nv_mod_tree(A, P, T);
    1780       47561 :   if (B) B = ZX_nv_mod_tree(B, P, T);
    1781       47561 :   H = cgetg(n+1, t_VECSMALL);
    1782      213252 :   for(i=1; i <= n; i++)
    1783             :   {
    1784      165693 :     ulong p = P[i];
    1785      165693 :     GEN a = gel(A, i), b = B ? gel(B, i): NULL;
    1786      165693 :     H[i] = ZX_resultant_prime(a, b, dB, degA, degB, p);
    1787             :   }
    1788       47559 :   H = ZV_chinese_tree(H, P, T, mod);
    1789       47559 :   gerepileall(av, 2, &H, mod);
    1790       47560 :   return H;
    1791             : }
    1792             : 
    1793             : GEN
    1794      387505 : ZX_resultant_worker(GEN P, GEN A, GEN B, GEN dB)
    1795             : {
    1796      387505 :   GEN V = cgetg(3, t_VEC);
    1797      387573 :   if (isintzero(B)) B = NULL;
    1798      387550 :   if (isintzero(dB)) dB = NULL;
    1799      387566 :   gel(V,1) = ZX_resultant_slice(A,B,dB,P,&gel(V,2));
    1800      387494 :   return V;
    1801             : }
    1802             : 
    1803             : static GEN
    1804      871142 : primelist_disc(ulong *p, long n, GEN dB)
    1805             : {
    1806      871142 :   GEN P = cgetg(n+1, t_VECSMALL);
    1807             :   long i;
    1808     1860424 :   for (i=1; i <= n; i++, *p = unextprime(*p+1))
    1809             :   {
    1810      989282 :     if (dB && umodiu(dB, *p)==0) { i--; continue; }
    1811      989282 :     P[i] = *p;
    1812             :   }
    1813      871142 :   return P;
    1814             : }
    1815             : 
    1816             : /* Res(A, B/dB), assuming the A,B in Z[X] and result is integer */
    1817             : /* if B=NULL, take B = A' */
    1818             : GEN
    1819      582055 : ZX_resultant_all(GEN A, GEN B, GEN dB, ulong bound)
    1820             : {
    1821             :   ulong p;
    1822      582055 :   pari_sp av = avma;
    1823             :   long n, m;
    1824             :   GEN  H, P, mod;
    1825      582055 :   int is_disc = !B;
    1826      582055 :   if (is_disc) B = ZX_deriv(A);
    1827             : 
    1828      582055 :   if ((H = trivial_case(A,B)) || (H = trivial_case(B,A))) return H;
    1829      579603 :   if (!bound) bound = ZX_ZXY_ResBound(A, B, dB);
    1830      579603 :   n = get_nbprimes(bound+1, &p);/* +1 to account for sign */
    1831      579603 :   if (is_disc)
    1832       25581 :     B = NULL;
    1833      579603 :   m = minss(degpol(A)+(B ? degpol(B): 0), n);
    1834      579603 :   if (m == 1)
    1835             :   {
    1836      469583 :     GEN P = primelist_disc(&p, n, dB);
    1837      469583 :     H = ZX_resultant_slice(A, B, dB, P, &mod);
    1838             :   }
    1839             :   else
    1840             :   {
    1841      110020 :     long i, s = n/m, r = n - m*s, di = 0;
    1842      110020 :     GEN worker = strtoclosure("_ZX_resultant_worker", 3, A, B?B:gen_0, dB?dB:gen_0);
    1843             :     struct pari_mt pt;
    1844             :     long pending;
    1845      110020 :     if (DEBUGLEVEL > 4)
    1846           0 :       err_printf("ZX_resultant: bound 2^%ld, nb primes: %ld\n",bound, n);
    1847      110020 :     H = cgetg(m+1+!!r, t_VEC); P = cgetg(m+1+!!r, t_VEC);
    1848      110020 :     mt_queue_start(&pt, worker);
    1849      539017 :     for (i=1; i<=m || pending; i++)
    1850             :     {
    1851             :       GEN done;
    1852      428997 :       mt_queue_submit(&pt, i, i<=m ? mkvec(primelist_disc(&p, s, dB)): NULL);
    1853      428997 :       done = mt_queue_get(&pt, NULL, &pending);
    1854      428997 :       if (done)
    1855             :       {
    1856      387629 :         di++;
    1857      387629 :         gel(H, di) = gel(done,1);
    1858      387629 :         gel(P, di) = gel(done,2);
    1859      387629 :         if (DEBUGLEVEL>5) err_printf("%ld%% ",100*di/m);
    1860             :       }
    1861             :     }
    1862      110020 :     mt_queue_end(&pt);
    1863      110020 :     if (r)
    1864             :     {
    1865       13930 :       GEN Pr = primelist_disc(&p, r, dB);
    1866       13930 :       gel(H, m+1) = ZX_resultant_slice(A, B, dB, Pr, &gel(P, m+1));
    1867             :     }
    1868      110020 :     H = ZV_chinese(H, P, &mod);
    1869      110020 :     if (DEBUGLEVEL>5) err_printf("done\n");
    1870             :   }
    1871      579603 :   H = Fp_center(H, mod, shifti(mod,-1));
    1872      579603 :   return gerepileuptoint(av, H);
    1873             : }
    1874             : 
    1875             : /* A0 and B0 in Q[X] */
    1876             : GEN
    1877       10812 : QX_resultant(GEN A0, GEN B0)
    1878             : {
    1879             :   GEN s, a, b, A, B;
    1880       10812 :   pari_sp av = avma;
    1881             : 
    1882       10812 :   A = Q_primitive_part(A0, &a);
    1883       10812 :   B = Q_primitive_part(B0, &b);
    1884       10812 :   s = ZX_resultant(A, B);
    1885       10812 :   if (!signe(s)) { avma = av; return gen_0; }
    1886       10812 :   if (a) s = gmul(s, gpowgs(a,degpol(B)));
    1887       10812 :   if (b) s = gmul(s, gpowgs(b,degpol(A)));
    1888       10812 :   return gerepileupto(av, s);
    1889             : }
    1890             : 
    1891             : GEN
    1892       24078 : ZX_resultant(GEN A, GEN B) { return ZX_resultant_all(A,B,NULL,0); }
    1893             : 
    1894             : GEN
    1895           0 : QXQ_intnorm(GEN A, GEN B)
    1896             : {
    1897             :   GEN c, n, R, lB;
    1898           0 :   long dA = degpol(A), dB = degpol(B);
    1899           0 :   pari_sp av = avma;
    1900           0 :   if (dA < 0) return gen_0;
    1901           0 :   A = Q_primitive_part(A, &c);
    1902           0 :   if (!c || typ(c) == t_INT) {
    1903           0 :     n = c;
    1904           0 :     R = ZX_resultant(B, A);
    1905             :   } else {
    1906           0 :     n = gel(c,1);
    1907           0 :     R = ZX_resultant_all(B, A, gel(c,2), 0);
    1908             :   }
    1909           0 :   if (n && !equali1(n)) R = mulii(R, powiu(n, dB));
    1910           0 :   lB = leading_coeff(B);
    1911           0 :   if (!equali1(lB)) R = diviiexact(R, powiu(lB, dA));
    1912           0 :   return gerepileuptoint(av, R);
    1913             : }
    1914             : 
    1915             : GEN
    1916           0 : QXQ_norm(GEN A, GEN B)
    1917             : {
    1918             :   GEN c, R, lB;
    1919           0 :   long dA = degpol(A), dB = degpol(B);
    1920           0 :   pari_sp av = avma;
    1921           0 :   if (dA < 0) return gen_0;
    1922           0 :   A = Q_primitive_part(A, &c);
    1923           0 :   R = ZX_resultant(B, A);
    1924           0 :   if (c) R = gmul(R, gpowgs(c, dB));
    1925           0 :   lB = leading_coeff(B);
    1926           0 :   if (!equali1(lB)) R = gdiv(R, gpowgs(lB, dA));
    1927           0 :   return gerepileupto(av, R);
    1928             : }
    1929             : 
    1930             : /* assume x has integral coefficients */
    1931             : GEN
    1932       26715 : ZX_disc_all(GEN x, ulong bound)
    1933             : {
    1934       26715 :   pari_sp av = avma;
    1935             :   GEN l, R;
    1936       26715 :   long s, d = degpol(x);
    1937       26715 :   if (d <= 1) return d ? gen_1: gen_0;
    1938       25581 :   s = (d & 2) ? -1: 1;
    1939       25581 :   l = leading_coeff(x);
    1940       25581 :   R = ZX_resultant_all(x, NULL, NULL, bound);
    1941       25581 :   if (is_pm1(l))
    1942       22795 :   { if (signe(l) < 0) s = -s; }
    1943             :   else
    1944        2786 :     R = diviiexact(R,l);
    1945       25581 :   if (s == -1) togglesign_safe(&R);
    1946       25581 :   return gerepileuptoint(av,R);
    1947             : }
    1948       24516 : GEN ZX_disc(GEN x) { return ZX_disc_all(x,0); }
    1949             : 
    1950             : GEN
    1951           0 : QX_disc(GEN x)
    1952             : {
    1953           0 :   pari_sp av = avma;
    1954           0 :   GEN c, d = ZX_disc( Q_primitive_part(x, &c) );
    1955           0 :   if (c) d = gmul(d, gpowgs(c, 2*degpol(x) - 2));
    1956           0 :   return gerepileupto(av, d);
    1957             : }
    1958             : 
    1959             : /* lift(1 / Mod(A,B)). B a ZX, A a scalar or a QX */
    1960             : GEN
    1961        7717 : QXQ_inv(GEN A, GEN B)
    1962             : {
    1963             :   GEN D, cU, q, U, V;
    1964             :   ulong p;
    1965        7717 :   pari_sp av2, av = avma;
    1966             :   forprime_t S;
    1967             :   pari_timer ti;
    1968        7717 :   if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
    1969             :   /* A a QX, B a ZX */
    1970        7717 :   if (degpol(A) < 15) return RgXQ_inv(A,B);
    1971           7 :   A = Q_primitive_part(A, &D);
    1972             :   /* A, B in Z[X] */
    1973           7 :   init_modular_small(&S);
    1974           7 :   if (DEBUGLEVEL>5) timer_start(&ti);
    1975           7 :   av2 = avma; U = NULL;
    1976         577 :   while ((p = u_forprime_next(&S)))
    1977             :   {
    1978             :     GEN a, b, qp, Up, Vp;
    1979             :     int stable;
    1980             : 
    1981         570 :     a = ZX_to_Flx(A, p);
    1982         570 :     b = ZX_to_Flx(B, p);
    1983             :     /* if p | Res(A/G, B/G), discard */
    1984         577 :     if (!Flx_extresultant(b,a,p, &Vp,&Up)) continue;
    1985             : 
    1986         570 :     if (!U)
    1987             :     { /* First time */
    1988           7 :       U = ZX_init_CRT(Up,p,varn(A));
    1989           7 :       V = ZX_init_CRT(Vp,p,varn(A));
    1990           7 :       q = utoipos(p); continue;
    1991             :     }
    1992         563 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: mod %ld (bound 2^%ld)", p,expi(q));
    1993         563 :     qp = muliu(q,p);
    1994        1126 :     stable = ZX_incremental_CRT_raw(&U, Up, q,qp, p)
    1995         563 :            & ZX_incremental_CRT_raw(&V, Vp, q,qp, p);
    1996         563 :     if (stable)
    1997             :     { /* all stable: check divisibility */
    1998           7 :       GEN res = ZX_add(ZX_mul(A,U), ZX_mul(B,V));
    1999           7 :       if (degpol(res) == 0) {
    2000           7 :         res = gel(res,2);
    2001           7 :         D = D? gmul(D, res): res;
    2002          14 :         break;
    2003             :       } /* DONE */
    2004           0 :       if (DEBUGLEVEL) err_printf("QXQ_inv: char 0 check failed");
    2005             :     }
    2006         556 :     q = qp;
    2007         556 :     if (gc_needed(av,1))
    2008             :     {
    2009           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"QXQ_inv");
    2010           0 :       gerepileall(av2, 3, &q,&U,&V);
    2011             :     }
    2012             :   }
    2013           7 :   if (!p) pari_err_OVERFLOW("QXQ_inv [ran out of primes]");
    2014           7 :   cU = ZX_content(U);
    2015           7 :   if (!is_pm1(cU)) { U = Q_div_to_int(U, cU); D = gdiv(D, cU); }
    2016           7 :   return gerepileupto(av, RgX_Rg_div(U, D));
    2017             : }
    2018             : 
    2019             : /************************************************************************
    2020             :  *                                                                      *
    2021             :  *                   IRREDUCIBLE POLYNOMIAL / Fp                        *
    2022             :  *                                                                      *
    2023             :  ************************************************************************/
    2024             : 
    2025             : /* irreducible (unitary) polynomial of degree n over Fp */
    2026             : GEN
    2027           0 : ffinit_rand(GEN p,long n)
    2028             : {
    2029             :   for(;;) {
    2030           0 :     pari_sp av = avma;
    2031           0 :     GEN pol = ZX_add(pol_xn(n, 0), random_FpX(n-1,0, p));
    2032           0 :     if (FpX_is_irred(pol, p)) return pol;
    2033           0 :     avma = av;
    2034           0 :   }
    2035             : }
    2036             : 
    2037             : /* return an extension of degree 2^l of F_2, assume l > 0
    2038             :  * Not stack clean. */
    2039             : static GEN
    2040         293 : f2init(long l)
    2041             : {
    2042             :   GEN Q, T, S;
    2043             :   long i, v;
    2044             : 
    2045         293 :   if (l == 1) return polcyclo(3, 0);
    2046         258 :   v = fetch_var_higher();
    2047         257 :   S = mkpoln(4, gen_1,gen_1,gen_0,gen_0); /* y(y^2 + y) */
    2048         257 :   Q = mkpoln(3, gen_1,gen_1, S); /* x^2 + x + y(y^2+y) */
    2049         259 :   setvarn(Q, v);
    2050             : 
    2051             :   /* x^4+x+1, irred over F_2, minimal polynomial of a root of Q */
    2052         259 :   T = mkpoln(5, gen_1,gen_0,gen_0,gen_1,gen_1);
    2053         259 :   setvarn(T, v);
    2054             :   /* Q = x^2 + x + a(y) irred. over K = F2[y] / (T(y))
    2055             :    * ==> x^2 + x + a(y) b irred. over K for any root b of Q
    2056             :    * ==> x^2 + x + (b^2+b)b */
    2057         259 :   for (i=2; i<l; i++) T = FpX_FpXY_resultant(T, Q, gen_2); /* minpoly(b) / F2*/
    2058         259 :   (void)delete_var(); setvarn(T,0); return T;
    2059             : }
    2060             : 
    2061             : /* return an extension of degree p^l of F_p, assume l > 0
    2062             :  * Not stack clean. */
    2063             : GEN
    2064           0 : ffinit_Artin_Shreier(GEN ip, long l)
    2065             : {
    2066           0 :   long i, v, p = itos(ip);
    2067           0 :   GEN T, Q, xp = pol_xn(p,0); /* x^p */
    2068           0 :   T = ZX_sub(xp, deg1pol_shallow(gen_1,gen_1,0)); /* x^p - x - 1 */
    2069           0 :   if (l == 1) return T;
    2070             : 
    2071           0 :   v = fetch_var_higher();
    2072           0 :   setvarn(xp, v);
    2073           0 :   Q = ZX_sub(pol_xn(2*p-1,0), pol_xn(p,0));
    2074           0 :   Q = gsub(xp, deg1pol_shallow(gen_1, Q, v)); /* x^p - x - (y^(2p-1)-y^p) */
    2075           0 :   for (i = 2; i <= l; ++i) T = FpX_FpXY_resultant(T, Q, ip);
    2076           0 :   (void)delete_var(); setvarn(T,0); return T;
    2077             : }
    2078             : 
    2079             : /* check if polsubcyclo(n,l,0) is irreducible modulo p */
    2080             : static long
    2081       11547 : fpinit_check(GEN p, long n, long l)
    2082             : {
    2083             :   ulong q;
    2084       11547 :   if (!uisprime(n)) return 0;
    2085        5775 :   q = umodiu(p,n); if (!q) return 0;
    2086        5208 :   return cgcd((n-1)/Fl_order(q, n-1, n), l) == 1;
    2087             : }
    2088             : 
    2089             : /* let k=2 if p%4==1, and k=4 else and assume k*p does not divide l.
    2090             :  * Return an irreducible polynomial of degree l over F_p.
    2091             :  * Variant of Adleman and Lenstra "Finding irreducible polynomials over
    2092             :  * finite fields", ACM, 1986 (5) 350--355.
    2093             :  * Not stack clean */
    2094             : static GEN
    2095        2961 : fpinit(GEN p, long l)
    2096             : {
    2097        2961 :   ulong n = 1+l;
    2098        2961 :   while (!fpinit_check(p,n,l)) n += l;
    2099        2961 :   if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
    2100        2961 :   return FpX_red(polsubcyclo(n,l,0),p);
    2101             : }
    2102             : 
    2103             : static GEN
    2104        2724 : ffinit_fact(GEN p, long n)
    2105             : {
    2106        2724 :   GEN P, F = gel(factoru_pow(n),3);
    2107             :   long i;
    2108        2725 :   if (!odd(n) && absequaliu(p, 2))
    2109         294 :     P = f2init(vals(n)); /* if n is even, F[1] = 2^vals(n)*/
    2110             :   else
    2111        2430 :     P = fpinit(p, F[1]);
    2112        3159 :   for (i = 2; i < lg(F); ++i)
    2113         434 :     P = FpX_direct_compositum(fpinit(p, F[i]), P, p);
    2114        2725 :   return P;
    2115             : }
    2116             : 
    2117             : static GEN
    2118          96 : ffinit_nofact(GEN p, long n)
    2119             : {
    2120          96 :   GEN P, Q = NULL;
    2121          96 :   if (lgefint(p)==3)
    2122             :   {
    2123           0 :     ulong pp = p[2], q;
    2124           0 :     long v = u_lvalrem(n,pp,&q);
    2125           0 :     if (v>0)
    2126             :     {
    2127           0 :       Q = (pp == 2)? f2init(v): fpinit(p,n/q);
    2128           0 :       n = q;
    2129             :     }
    2130             :   }
    2131             :   /* n coprime to p */
    2132          96 :   if (n==1) P = Q;
    2133             :   else
    2134             :   {
    2135          96 :     P = fpinit(p, n);
    2136          96 :     if (Q) P = FpX_direct_compositum(P, Q, p);
    2137             :   }
    2138          96 :   return P;
    2139             : }
    2140             : 
    2141             : static GEN
    2142        3575 : init_Fq_i(GEN p, long n, long v)
    2143             : {
    2144             :   GEN P;
    2145        3575 :   if (n <= 0) pari_err_DOMAIN("ffinit", "degree", "<=", gen_0, stoi(n));
    2146        3574 :   if (typ(p) != t_INT) pari_err_TYPE("ffinit",p);
    2147        3574 :   if (signe(p) <= 0) pari_err_PRIME("ffinit",p);
    2148        3574 :   if (v < 0) v = 0;
    2149        3574 :   if (n == 1) return pol_x(v);
    2150        3392 :   if (fpinit_check(p, n+1, n)) return polcyclo(n+1, v);
    2151        2819 :   if (lgefint(p)-2 <= expu(n))
    2152        2724 :     P = ffinit_fact(p,n);
    2153             :   else
    2154          96 :     P = ffinit_nofact(p,n);
    2155        2821 :   setvarn(P, v); return P;
    2156             : }
    2157             : GEN
    2158        3449 : init_Fq(GEN p, long n, long v)
    2159             : {
    2160        3449 :   pari_sp av = avma;
    2161        3449 :   return gerepileupto(av, init_Fq_i(p, n, v));
    2162             : }
    2163             : GEN
    2164         126 : ffinit(GEN p, long n, long v)
    2165             : {
    2166         126 :   pari_sp av = avma;
    2167         126 :   return gerepileupto(av, FpX_to_mod(init_Fq_i(p, n, v), p));
    2168             : }
    2169             : 
    2170             : GEN
    2171        3178 : ffnbirred(GEN p, long n)
    2172             : {
    2173        3178 :   pari_sp av = avma;
    2174             :   long j;
    2175        3178 :   GEN s = gen_0, dk, pd;
    2176        3178 :   dk = divisorsu(n);
    2177       10535 :   for (j = 1; j < lg(dk); ++j)
    2178             :   {
    2179        7357 :     long d = dk[j];
    2180        7357 :     long m = moebiusu(d);
    2181        7357 :     if (!m) continue;
    2182        6797 :     pd = powiu(p, n/d);
    2183        6797 :     s = m>0 ? addii(s, pd): subii(s,pd);
    2184             :   }
    2185        3178 :   return gerepileuptoint(av, divis(s, n));
    2186             : }
    2187             : 
    2188             : GEN
    2189         427 : ffsumnbirred(GEN p, long n)
    2190             : {
    2191         427 :   pari_sp av = avma;
    2192             :   long i,j;
    2193         427 :   GEN v,q, t = gen_0;
    2194         427 :   v = cgetg(n+1,t_VECSMALL); v[1] = 1;
    2195         427 :   q = cgetg(n+1,t_VEC); gel(q,1) = p;
    2196        1470 :   for(i=2; i<=n; i++)
    2197             :   {
    2198        1043 :     v[i] = moebiusu(i);
    2199        1043 :     gel(q,i) = mulii(gel(q,i-1), p);
    2200             :   }
    2201        1897 :   for(i=1; i<=n; i++)
    2202             :   {
    2203        1470 :     GEN s = gen_0;
    2204        1470 :     GEN dk = divisorsu(i);
    2205        4445 :     for (j = 1; j < lg(dk); ++j)
    2206             :     {
    2207        2975 :       long d = dk[j], m = v[d];
    2208        2975 :       if (!m) continue;
    2209        2709 :       s = m>0 ? addii(s, gel(q, i/d)): subii(s, gel(q, i/d));
    2210             :     }
    2211        1470 :     t = addii(t, divis(s, i));
    2212             :   }
    2213         427 :   return gerepileuptoint(av, t);
    2214             : }
    2215             : 
    2216             : GEN
    2217         140 : ffnbirred0(GEN p, long n, long flag)
    2218             : {
    2219         140 :   if (typ(p) != t_INT) pari_err_TYPE("ffnbirred", p);
    2220         140 :   if (n <= 0) pari_err_DOMAIN("ffnbirred", "degree", "<=", gen_0, stoi(n));
    2221         140 :   switch(flag)
    2222             :   {
    2223             :     case 0:
    2224          70 :       return ffnbirred(p, n);
    2225             :     case 1:
    2226          70 :       return ffsumnbirred(p, n);
    2227             :     default:
    2228           0 :       pari_err_FLAG("ffnbirred");
    2229             :   }
    2230           0 :   return NULL; /* NOT REACHED */
    2231             : }

Generated by: LCOV version 1.11