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.10.0 lcov report (development 19823-d80e022) Lines: 1014 1205 84.1 %
Date: 2016-12-03 05:49:13 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      559412 : charact(struct charact *S, GEN x)
      50             : {
      51      559412 :   const long tx = typ(x);
      52             :   long i, l;
      53      559412 :   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        6181 :       l = lg(x);
      61        6181 :       for (i=lontyp[tx]; i < l; i++) charact(S,gel(x,i));
      62        6174 :       break;
      63             :     case t_LIST:
      64           7 :       x = list_data(x);
      65           7 :       if (x) charact(S, x);
      66           7 :       break;
      67             :   }
      68      559398 : }
      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        5110 : characteristic(GEN x)
      93             : {
      94             :   struct charact S;
      95        5110 :   S.q = gen_0; S.isprime = 0;
      96        5110 :   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    15833339 : Rg_is_Fp(GEN x, GEN *pp)
     108             : {
     109             :   GEN mod;
     110    15833339 :   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     9618789 :     return 1;
     123     2364921 :   default: return 0;
     124             :   }
     125             : }
     126             : 
     127             : int
     128     2347425 : RgX_is_FpX(GEN x, GEN *pp)
     129             : {
     130     2347425 :   long i, lx = lg(x);
     131     8726941 :   for (i=2; i<lx; i++)
     132     8100283 :     if (!Rg_is_Fp(gel(x, i), pp))
     133     1720767 :       return 0;
     134      626658 :   return 1;
     135             : }
     136             : 
     137             : int
     138     1659526 : RgV_is_FpV(GEN x, GEN *pp)
     139             : {
     140     1659526 :   long i, lx = lg(x);
     141     8726273 :   for (i=1; i<lx; i++)
     142     7710894 :     if (!Rg_is_Fp(gel(x,i), pp)) return 0;
     143     1015379 :   return 1;
     144             : }
     145             : 
     146             : int
     147      746611 : RgM_is_FpM(GEN x, GEN *pp)
     148             : {
     149      746611 :   long i, lx = lg(x);
     150     1759596 :   for (i=1; i<lx; i++)
     151     1657111 :     if (!RgV_is_FpV(gel(x, i), pp)) return 0;
     152      102485 :   return 1;
     153             : }
     154             : 
     155             : int
     156       90923 : Rg_is_FpXQ(GEN x, GEN *pT, GEN *pp)
     157             : {
     158             :   GEN pol, mod, p;
     159       90923 :   switch(typ(x))
     160             :   {
     161             :   case t_INTMOD:
     162       22092 :     return Rg_is_Fp(x, pp);
     163             :   case t_INT:
     164       20342 :     return 1;
     165             :   case t_POL:
     166        8498 :     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       32816 : RgX_is_FpXQX(GEN x, GEN *pT, GEN *pp)
     199             : {
     200       32816 :   long i, lx = lg(x);
     201       97853 :   for (i = 2; i < lx; i++)
     202       90377 :     if (!Rg_is_FpXQ(gel(x,i), pT, pp)) return 0;
     203        7476 :   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    26582052 : Rg_to_Fp(GEN x, GEN p)
     216             : {
     217    26582052 :   if (lgefint(p) == 3) return utoi(Rg_to_Fl(x, uel(p,2)));
     218      386319 :   switch(typ(x))
     219             :   {
     220       25321 :     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      360938 :       GEN q = gel(x,1), a = gel(x,2);
     230      360938 :       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      140749 : RgX_to_FpX(GEN x, GEN p)
     277             : {
     278             :   long i, l;
     279      140749 :   GEN z = cgetg_copy(x, &l); z[1] = x[1];
     280      140749 :   for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
     281      140749 :   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       63340 : FqX_Fq_mul_to_monic(GEN P, GEN U, GEN T, GEN p)
     381             : {
     382             :   long i, lP;
     383       63340 :   GEN res = cgetg_copy(P, &lP); res[1] = P[1];
     384       63340 :   for(i=2; i<lP-1; i++) gel(res,i) = Fq_mul(U,gel(P,i), T,p);
     385       63340 :   gel(res,lP-1) = gen_1; return res;
     386             : }
     387             : 
     388             : GEN
     389        2695 : FpXQX_normalize(GEN z, GEN T, GEN p)
     390             : {
     391             :   GEN lc;
     392        2695 :   if (lg(z) == 2) return z;
     393        2695 :   lc = leading_coeff(z);
     394        2695 :   if (typ(lc) == t_POL)
     395             :   {
     396        1202 :     if (lg(lc) > 3) /* non-constant */
     397        1187 :       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        1508 :   if (equali1(lc)) return z;
     405          63 :   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      111461 : monomial(GEN a, long d, long v)
     460             : {
     461             :   long i, n;
     462             :   GEN P;
     463      111461 :   if (d < 0) {
     464           0 :     if (isrationalzero(a)) return pol_0(v);
     465           0 :     retmkrfrac(a, pol_xn(-d, v));
     466             :   }
     467      111461 :   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      111195 :     n = d+2; P = cgetg(n+1, t_POL);
     476      111195 :     P[1] = evalsigne(1) | evalvarn(v);
     477             :   }
     478      111195 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     479      111195 :   gel(P,i) = a; return P;
     480             : }
     481             : GEN
     482     7307354 : monomialcopy(GEN a, long d, long v)
     483             : {
     484             :   long i, n;
     485             :   GEN P;
     486     7307354 :   if (d < 0) {
     487           7 :     if (isrationalzero(a)) return pol_0(v);
     488           7 :     retmkrfrac(gcopy(a), pol_xn(-d, v));
     489             :   }
     490     7307347 :   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     7307340 :     n = d+2; P = cgetg(n+1, t_POL);
     499     7307340 :     P[1] = evalsigne(1) | evalvarn(v);
     500             :   }
     501     7307340 :   for (i = 2; i < n; i++) gel(P,i) = gen_0;
     502     7307340 :   gel(P,i) = gcopy(a); return P;
     503             : }
     504             : GEN
     505       19642 : pol_x_powers(long N, long v)
     506             : {
     507       19642 :   GEN L = cgetg(N+1,t_VEC);
     508             :   long i;
     509       19642 :   for (i=1; i<=N; i++) gel(L,i) = pol_xn(i-1, v);
     510       19642 :   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     6598792 : Fq_add(GEN x, GEN y, GEN T/*unused*/, GEN p)
     533             : {
     534             :   (void)T;
     535     6598792 :   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     3677971 :     case 3: return FpX_add(x,y,p);
     541             :   }
     542           0 :   return NULL;
     543             : }
     544             : 
     545             : GEN
     546     4470163 : Fq_sub(GEN x, GEN y, GEN T/*unused*/, GEN p)
     547             : {
     548             :   (void)T;
     549     4470163 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     550             :   {
     551      155412 :     case 0: return Fp_sub(x,y,p);
     552        1158 :     case 1: return FpX_Fp_sub(x,y,p);
     553        7824 :     case 2: return Fp_FpX_sub(x,y,p);
     554     4305769 :     case 3: return FpX_sub(x,y,p);
     555             :   }
     556           0 :   return NULL;
     557             : }
     558             : 
     559             : GEN
     560      580029 : Fq_neg(GEN x, GEN T/*unused*/, GEN p)
     561             : {
     562             :   (void)T;
     563      580029 :   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    42246997 : Fq_mul(GEN x, GEN y, GEN T, GEN p)
     576             : {
     577    42246997 :   switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
     578             :   {
     579     2378786 :     case 0: return Fp_mul(x,y,p);
     580      188325 :     case 1: return FpX_Fp_mul(x,y,p);
     581      316481 :     case 2: return FpX_Fp_mul(y,x,p);
     582    39363405 :     case 3: if (T) return FpXQ_mul(x,y,T,p);
     583     2751345 :             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       51309 : Fq_Fp_mul(GEN x, GEN y, GEN T/*unused*/, GEN p)
     599             : {
     600             :   (void)T;
     601      102618 :   return (typ(x) == t_POL)? FpX_Fp_mul(x,y,p)
     602       51309 :                           : Fp_mul(x,y,p);
     603             : }
     604             : /* If T==NULL do not reduce*/
     605             : GEN
     606      259587 : Fq_sqr(GEN x, GEN T, GEN p)
     607             : {
     608      259587 :   if (typ(x) == t_POL)
     609             :   {
     610       10967 :     if (T) return FpXQ_sqr(x,T,p);
     611           0 :     else return FpX_sqr(x,p);
     612             :   }
     613             :   else
     614      248620 :     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       20086 : Fq_inv(GEN x, GEN pol, GEN p)
     633             : {
     634       20086 :   if (typ(x) == t_INT) return Fp_inv(x,p);
     635       15403 :   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       21231 : Fq_pow(GEN x, GEN n, GEN pol, GEN p)
     653             : {
     654       21231 :   if (typ(x) == t_INT) return Fp_pow(x,n,p);
     655       14042 :   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       60873 : Fq_sqrtn(GEN x, GEN n, GEN T, GEN p, GEN *zeta)
     677             : {
     678       60873 :   if (typ(x) == t_INT)
     679             :   {
     680             :     long d;
     681       60068 :     if (!T) return Fp_sqrtn(x,n,p,zeta);
     682         231 :     d = get_FpX_degree(T);
     683         231 :     if (ugcd(umodiu(n,d),d) == 1)
     684             :     {
     685          42 :       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          35 :         if (equalii(gcdii(subiu(p,1),n), gcdii(subiu(Fp_powu(p,d,n), 1), n)))
     691          14 :           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       11011 : FqV_red(GEN z, GEN T, GEN p)
     839             : {
     840       11011 :   long i, l = lg(z);
     841       11011 :   GEN res = cgetg(l, typ(z));
     842       11011 :   for(i=1;i<l;i++) gel(res,i) = Fq_red(gel(z,i),T,p);
     843       11011 :   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        1638 : FqC_to_FlxC(GEN v, GEN T, GEN pp)
     894             : {
     895        1638 :   long j, N = lg(v);
     896        1638 :   long vT = evalvarn(get_FpX_var(T));
     897        1638 :   ulong p = pp[2];
     898        1638 :   GEN y = cgetg(N, t_COL);
     899       33824 :   for (j=1; j<N; j++)
     900       72903 :     gel(y,j) = (typ(gel(v,j))==t_INT?  Z_to_Flx(gel(v,j), p, vT)
     901       40717 :                                     : ZX_to_Flx(gel(v,j), p));
     902        1638 :   return y;
     903             : }
     904             : 
     905             : GEN
     906         329 : FqM_to_FlxM(GEN x, GEN T, GEN pp)
     907             : {
     908         329 :   long j, n = lg(x);
     909         329 :   GEN y = cgetg(n,t_MAT);
     910         329 :   if (n == 1) return y;
     911        1967 :   for (j=1; j<n; j++)
     912        1638 :     gel(y,j) = FqC_to_FlxC(gel(x,j), T, pp);
     913         329 :   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     9718118 : Fl_chinese_coprime(GEN a, ulong b, GEN q, ulong p, ulong qinv, GEN pq)
     924             : {
     925     9718118 :   ulong d, amod = umodiu(a, p);
     926     9718118 :   pari_sp av = avma;
     927             :   GEN ax;
     928             : 
     929     9718118 :   if (b == amod) return NULL;
     930     1553672 :   d = (b > amod)? b - amod: p - (amod - b); /* (b - a) mod p */
     931     1553672 :   (void)new_chunk(lgefint(pq)<<1); /* HACK */
     932     1553672 :   ax = mului(Fl_mul(d,qinv,p), q); /* d mod p, 0 mod q */
     933     1553672 :   avma = av; return addii(a, ax); /* in ]-q, pq[ assuming a in -]-q,q[ */
     934             : }
     935             : GEN
     936       16940 : Z_init_CRT(ulong Hp, ulong p) { return stoi(Fl_center(Hp, p, p>>1)); }
     937             : GEN
     938     3113651 : ZX_init_CRT(GEN Hp, ulong p, long v)
     939             : {
     940     3113651 :   long i, l = lg(Hp), lim = (long)(p>>1);
     941     3113651 :   GEN H = cgetg(l, t_POL);
     942     3113651 :   H[1] = evalsigne(1) | evalvarn(v);
     943    11001327 :   for (i=2; i<l; i++)
     944     7887676 :     gel(H,i) = stoi(Fl_center(Hp[i], p, lim));
     945     3113651 :   return H;
     946             : }
     947             : 
     948             : GEN
     949      109723 : ZM_init_CRT(GEN Hp, ulong p)
     950             : {
     951      109723 :   long i,j, m, l = lg(Hp), lim = (long)(p>>1);
     952      109723 :   GEN c, cp, H = cgetg(l, t_MAT);
     953      109723 :   if (l==1) return H;
     954      109716 :   m = lgcols(Hp);
     955      891492 :   for (j=1; j<l; j++)
     956             :   {
     957      781776 :     cp = gel(Hp,j);
     958      781776 :     c = cgetg(m, t_COL);
     959      781776 :     gel(H,j) = c;
     960      781776 :     for (i=1; i<m; i++) gel(c,i) = stoi(Fl_center(cp[i],p, lim));
     961             :   }
     962      109716 :   return H;
     963             : }
     964             : 
     965             : int
     966       58129 : Z_incremental_CRT(GEN *H, ulong Hp, GEN *ptq, ulong p)
     967             : {
     968       58129 :   GEN h, q = *ptq, qp = muliu(q,p), lim = shifti(qp,-1);
     969       58129 :   ulong qinv = Fl_inv(umodiu(q,p), p);
     970       58129 :   int stable = 1;
     971       58129 :   h = Fl_chinese_coprime(*H,Hp,q,p,qinv,qp);
     972       58129 :   if (h)
     973             :   {
     974       14405 :     if (cmpii(h,lim) > 0) h = subii(h,qp);
     975       14405 :     *H = h; stable = 0;
     976             :   }
     977       58129 :   *ptq = qp; return stable;
     978             : }
     979             : 
     980             : static int
     981        5737 : ZX_incremental_CRT_raw(GEN *ptH, GEN Hp, GEN q, GEN qp, ulong p)
     982             : {
     983        5737 :   GEN H = *ptH, h, lim = shifti(qp,-1);
     984        5737 :   ulong qinv = Fl_inv(umodiu(q,p), p);
     985        5737 :   long i, l = lg(H), lp = lg(Hp);
     986        5737 :   int stable = 1;
     987             : 
     988        5737 :   if (l < lp)
     989             :   { /* degree increases */
     990           0 :     GEN x = cgetg(lp, t_POL);
     991           0 :     for (i=1; i<l; i++)  x[i] = H[i];
     992           0 :     for (   ; i<lp; i++) gel(x,i) = gen_0;
     993           0 :     *ptH = H = x;
     994           0 :     stable = 0;
     995        5737 :   } else if (l > lp)
     996             :   { /* degree decreases */
     997           0 :     GEN x = cgetg(l, t_VECSMALL);
     998           0 :     for (i=1; i<lp; i++)  x[i] = Hp[i];
     999           0 :     for (   ; i<l; i++) x[i] = 0;
    1000           0 :     Hp = x; lp = l;
    1001             :   }
    1002      208903 :   for (i=2; i<lp; i++)
    1003             :   {
    1004      203166 :     h = Fl_chinese_coprime(gel(H,i),Hp[i],q,p,qinv,qp);
    1005      203166 :     if (h)
    1006             :     {
    1007       55909 :       if (cmpii(h,lim) > 0) h = subii(h,qp);
    1008       55909 :       gel(H,i) = h; stable = 0;
    1009             :     }
    1010             :   }
    1011        5737 :   return stable;
    1012             : }
    1013             : 
    1014             : int
    1015        4008 : ZX_incremental_CRT(GEN *ptH, GEN Hp, GEN *ptq, ulong p)
    1016             : {
    1017        4008 :   GEN q = *ptq, qp = muliu(q,p);
    1018        4008 :   int stable = ZX_incremental_CRT_raw(ptH, Hp, q, qp, p);
    1019        4008 :   *ptq = qp; return stable;
    1020             : }
    1021             : 
    1022             : int
    1023      110153 : ZM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
    1024             : {
    1025      110153 :   GEN h, H = *pH, q = *ptq, qp = muliu(q, p), lim = shifti(qp,-1);
    1026      110153 :   ulong qinv = Fl_inv(umodiu(q,p), p);
    1027      110153 :   long i,j, l = lg(H), m = lgcols(H);
    1028      110153 :   int stable = 1;
    1029      946960 :   for (j=1; j<l; j++)
    1030    10293630 :     for (i=1; i<m; i++)
    1031             :     {
    1032     9456823 :       h = Fl_chinese_coprime(gcoeff(H,i,j), coeff(Hp,i,j),q,p,qinv,qp);
    1033     9456823 :       if (h)
    1034             :       {
    1035     1483358 :         if (cmpii(h,lim) > 0) h = subii(h,qp);
    1036     1483358 :         gcoeff(H,i,j) = h; stable = 0;
    1037             :       }
    1038             :     }
    1039      110153 :   *ptq = qp; return stable;
    1040             : }
    1041             : 
    1042             : /* record the degrees of Euclidean remainders (make them as large as
    1043             :  * possible : smaller values correspond to a degenerate sequence) */
    1044             : static void
    1045        1561 : Flx_resultant_set_dglist(GEN a, GEN b, GEN dglist, ulong p)
    1046             : {
    1047             :   long da,db,dc, ind;
    1048        1561 :   pari_sp av = avma;
    1049             : 
    1050        1561 :   if (lgpol(a)==0 || lgpol(b)==0) return;
    1051        1561 :   da = degpol(a);
    1052        1561 :   db = degpol(b);
    1053        1561 :   if (db > da)
    1054           0 :   { swapspec(a,b, da,db); }
    1055        1561 :   else if (!da) return;
    1056        1561 :   ind = 0;
    1057        9814 :   while (db)
    1058             :   {
    1059        6692 :     GEN c = Flx_rem(a,b, p);
    1060        6692 :     a = b; b = c; dc = degpol(c);
    1061        6692 :     if (dc < 0) break;
    1062             : 
    1063        6692 :     ind++;
    1064        6692 :     if (dc > dglist[ind]) dglist[ind] = dc;
    1065        6692 :     if (gc_needed(av,2))
    1066             :     {
    1067           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1068           0 :       gerepileall(av, 2, &a,&b);
    1069             :     }
    1070        6692 :     db = dc; /* = degpol(b) */
    1071             :   }
    1072        1561 :   if (ind+1 > lg(dglist)) setlg(dglist,ind+1);
    1073        1561 :   avma = av; return;
    1074             : }
    1075             : /* assuming the PRS finishes on a degree 1 polynomial C0 + C1X, with
    1076             :  * "generic" degree sequence as given by dglist, set *Ci and return
    1077             :  * resultant(a,b). Modular version of Collins's subresultant */
    1078             : static ulong
    1079        6996 : Flx_resultant_all(GEN a, GEN b, long *C0, long *C1, GEN dglist, ulong p)
    1080             : {
    1081             :   long da,db,dc, ind;
    1082        6996 :   ulong lb, res, g = 1UL, h = 1UL, ca = 1UL, cb = 1UL;
    1083        6996 :   int s = 1;
    1084        6996 :   pari_sp av = avma;
    1085             : 
    1086        6996 :   *C0 = 1; *C1 = 0;
    1087        6996 :   if (lgpol(a)==0 || lgpol(b)==0) return 0;
    1088        6996 :   da = degpol(a);
    1089        6996 :   db = degpol(b);
    1090        6996 :   if (db > da)
    1091             :   {
    1092           0 :     swapspec(a,b, da,db);
    1093           0 :     if (both_odd(da,db)) s = -s;
    1094             :   }
    1095        6996 :   else if (!da) return 1; /* = a[2] ^ db, since 0 <= db <= da = 0 */
    1096        6996 :   ind = 0;
    1097       40727 :   while (db)
    1098             :   { /* sub-resultant algo., applied to ca * a and cb * b, ca,cb scalars,
    1099             :      * da = deg a, db = deg b */
    1100       27099 :     GEN c = Flx_rem(a,b, p);
    1101       27099 :     long delta = da - db;
    1102             : 
    1103       27099 :     if (both_odd(da,db)) s = -s;
    1104       27099 :     lb = Fl_mul(b[db+2], cb, p);
    1105       27099 :     a = b; b = c; dc = degpol(c);
    1106       27099 :     ind++;
    1107       27099 :     if (dc != dglist[ind]) { avma = av; return 0; } /* degenerates */
    1108       26735 :     if (g == h)
    1109             :     { /* frequent */
    1110       24621 :       ulong cc = Fl_mul(ca, Fl_powu(Fl_div(lb,g,p), delta+1, p), p);
    1111       24621 :       ca = cb;
    1112       24621 :       cb = cc;
    1113             :     }
    1114             :     else
    1115             :     {
    1116        2114 :       ulong cc = Fl_mul(ca, Fl_powu(lb, delta+1, p), p);
    1117        2114 :       ulong ghdelta = Fl_mul(g, Fl_powu(h, delta, p), p);
    1118        2114 :       ca = cb;
    1119        2114 :       cb = Fl_div(cc, ghdelta, p);
    1120             :     }
    1121       26735 :     da = db; /* = degpol(a) */
    1122       26735 :     db = dc; /* = degpol(b) */
    1123             : 
    1124       26735 :     g = lb;
    1125       26735 :     if (delta == 1)
    1126       17482 :       h = g; /* frequent */
    1127             :     else
    1128        9253 :       h = Fl_mul(h, Fl_powu(Fl_div(g,h,p), delta, p), p);
    1129             : 
    1130       26735 :     if (gc_needed(av,2))
    1131             :     {
    1132           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
    1133           0 :       gerepileall(av, 2, &a,&b);
    1134             :     }
    1135             :   }
    1136        6632 :   if (da > 1) return 0; /* Failure */
    1137             :   /* last non-constant polynomial has degree 1 */
    1138        6632 :   *C0 = Fl_mul(ca, a[2], p);
    1139        6632 :   *C1 = Fl_mul(ca, a[3], p);
    1140        6632 :   res = Fl_mul(cb, b[2], p);
    1141        6632 :   if (s == -1) res = p - res;
    1142        6632 :   avma = av; return res;
    1143             : }
    1144             : 
    1145             : /* Q a vector of polynomials representing B in Fp[X][Y], evaluate at X = x,
    1146             :  * Return 0 in case of degree drop. */
    1147             : static GEN
    1148        8557 : FlxY_evalx_drop(GEN Q, ulong x, ulong p)
    1149             : {
    1150             :   GEN z;
    1151        8557 :   long i, lb = lg(Q);
    1152        8557 :   ulong leadz = Flx_eval(leading_coeff(Q), x, p);
    1153        8557 :   long vs=mael(Q,2,1);
    1154        8557 :   if (!leadz) return zero_Flx(vs);
    1155             : 
    1156        8557 :   z = cgetg(lb, t_VECSMALL); z[1] = vs;
    1157        8557 :   for (i=2; i<lb-1; i++) z[i] = Flx_eval(gel(Q,i), x, p);
    1158        8557 :   z[i] = leadz; return z;
    1159             : }
    1160             : 
    1161             : GEN
    1162       17836 : FpXY_Fq_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
    1163             : {
    1164       17836 :   pari_sp av = avma;
    1165       17836 :   long i, lb = lg(Q);
    1166             :   GEN z;
    1167       17836 :   if (!T) return FpXY_evaly(Q, y, p, vx);
    1168        1148 :   if (lb == 2) return pol_0(vx);
    1169        1148 :   z = gel(Q, lb-1);
    1170        1148 :   if (lb == 3 || !signe(y)) return typ(z)==t_INT? scalar_ZX(z, vx): ZX_copy(z);
    1171             : 
    1172        1148 :   if (typ(z) == t_INT) z = scalar_ZX_shallow(z, vx);
    1173       28084 :   for (i=lb-2; i>=2; i--)
    1174             :   {
    1175       26936 :     GEN c = gel(Q,i);
    1176       26936 :     z = FqX_Fq_mul(z, y, T, p);
    1177       26936 :     z = typ(c) == t_INT? FqX_Fq_add(z,c,T,p): FqX_add(z,c,T,p);
    1178             :   }
    1179        1148 :   return gerepileupto(av, z);
    1180             : }
    1181             : 
    1182             : static GEN
    1183        5586 : ZX_norml1(GEN x)
    1184             : {
    1185        5586 :   long i, l = lg(x);
    1186             :   GEN s;
    1187             : 
    1188        5586 :   if (l == 2) return gen_0;
    1189        5586 :   s = gel(x, l-1); /* != 0 */
    1190       31087 :   for (i = l-2; i > 1; i--) {
    1191       25501 :     GEN xi = gel(x,i);
    1192       25501 :     if (!signe(x)) continue;
    1193       25501 :     s = addii_sign(s,1, xi,1);
    1194             :   }
    1195        5586 :   return s;
    1196             : }
    1197             : 
    1198             : /* Interpolate at roots of 1 and use Hadamard bound for univariate resultant:
    1199             :  *   bound = N_2(A)^degpol B N_2(B)^degpol(A),  where
    1200             :  *     N_2(A) = sqrt(sum (N_1(Ai))^2)
    1201             :  * Return e such that Res(A, B) < 2^e */
    1202             : ulong
    1203       47478 : ZX_ZXY_ResBound(GEN A, GEN B, GEN dB)
    1204             : {
    1205       47478 :   pari_sp av = avma;
    1206       47478 :   GEN a = gen_0, b = gen_0;
    1207       47478 :   long i , lA = lg(A), lB = lg(B);
    1208             :   double loga, logb;
    1209       47478 :   for (i=2; i<lA; i++) a = addii(a, sqri(gel(A,i)));
    1210      386945 :   for (i=2; i<lB; i++)
    1211             :   {
    1212      339467 :     GEN t = gel(B,i);
    1213      339467 :     if (typ(t) == t_POL) t = ZX_norml1(t);
    1214      339467 :     b = addii(b, sqri(t));
    1215             :   }
    1216       47478 :   loga = dbllog2(a);
    1217       47478 :   logb = dbllog2(b); if (dB) logb -= 2 * dbllog2(dB);
    1218       47478 :   i = (long)((degpol(B) * loga + degpol(A) * logb) / 2);
    1219       47478 :   avma = av; return (i <= 0)? 1: 1 + (ulong)i;
    1220             : }
    1221             : 
    1222             : /* return Res(a(Y), b(n,Y)) over Fp. la = leading_coeff(a) [for efficiency] */
    1223             : static ulong
    1224      192588 : Flx_FlxY_eval_resultant(GEN a, GEN b, ulong n, ulong p, ulong la)
    1225             : {
    1226      192588 :   GEN ev = FlxY_evalx(b, n, p);
    1227      192588 :   long drop = lg(b) - lg(ev);
    1228      192588 :   ulong r = Flx_resultant(a, ev, p);
    1229      192588 :   if (drop && la != 1) r = Fl_mul(r, Fl_powu(la, drop,p),p);
    1230      192588 :   return r;
    1231             : }
    1232             : static GEN
    1233           4 : FpX_FpXY_eval_resultant(GEN a, GEN b, GEN n, GEN p, GEN la, long db, long vX)
    1234             : {
    1235           4 :   GEN ev = FpXY_evaly(b, n, p, vX);
    1236           4 :   long drop = db-degpol(ev);
    1237           4 :   GEN r = FpX_resultant(a, ev, p);
    1238           4 :   if (drop && !gequal1(la)) r = Fp_mul(r, Fp_powu(la, drop,p),p);
    1239           4 :   return r;
    1240             : }
    1241             : 
    1242             : /* assume dres := deg(Res_X(a,b), Y) <= deg(a,X) * deg(b,Y) < p */
    1243             : /* Return a Fly */
    1244             : static GEN
    1245        4454 : Flx_FlxY_resultant_polint(GEN a, GEN b, ulong p, ulong dres, long sx)
    1246             : {
    1247        4454 :   ulong i, n, la = Flx_lead(a);
    1248        4454 :   GEN  x = cgetg(dres+2, t_VECSMALL);
    1249        4454 :   GEN  y = cgetg(dres+2, t_VECSMALL);
    1250             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1251             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1252       98730 :   for (i=0,n = 1; i < dres; n++)
    1253             :   {
    1254       94276 :     x[++i] = n;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1255       94276 :     x[++i] = p-n; y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1256             :   }
    1257        4454 :   if (i == dres)
    1258             :   {
    1259        4036 :     x[++i] = 0;   y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,la);
    1260             :   }
    1261        4454 :   return Flv_polint(x,y, p, sx);
    1262             : }
    1263             : 
    1264             : static GEN
    1265        4679 : FlxX_pseudorem(GEN x, GEN y, ulong p)
    1266             : {
    1267        4679 :   long vx = varn(x), dx, dy, dz, i, lx, dp;
    1268        4679 :   pari_sp av = avma, av2;
    1269             : 
    1270        4679 :   if (!signe(y)) pari_err_INV("FlxX_pseudorem",y);
    1271        4679 :   (void)new_chunk(2);
    1272        4682 :   dx=degpol(x); x = RgX_recip_shallow(x)+2;
    1273        4687 :   dy=degpol(y); y = RgX_recip_shallow(y)+2; dz=dx-dy; dp = dz+1;
    1274        4692 :   av2 = avma;
    1275             :   for (;;)
    1276             :   {
    1277       33206 :     gel(x,0) = Flx_neg(gel(x,0), p); dp--;
    1278      131595 :     for (i=1; i<=dy; i++)
    1279      193070 :       gel(x,i) = Flx_add( Flx_mul(gel(y,0), gel(x,i), p),
    1280       96535 :                               Flx_mul(gel(x,0), gel(y,i), p), p );
    1281      540932 :     for (   ; i<=dx; i++)
    1282      507737 :       gel(x,i) = Flx_mul(gel(y,0), gel(x,i), p);
    1283       35072 :     do { x++; dx--; } while (dx >= 0 && lg(gel(x,0))==2);
    1284       33195 :     if (dx < dy) break;
    1285       28510 :     if (gc_needed(av2,1))
    1286             :     {
    1287           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_pseudorem dx = %ld >= %ld",dx,dy);
    1288           0 :       gerepilecoeffs(av2,x,dx+1);
    1289             :     }
    1290       28514 :   }
    1291        4685 :   if (dx < 0) return zero_Flx(0);
    1292        4685 :   lx = dx+3; x -= 2;
    1293        4685 :   x[0]=evaltyp(t_POL) | evallg(lx);
    1294        4685 :   x[1]=evalsigne(1) | evalvarn(vx);
    1295        4685 :   x = RgX_recip_shallow(x);
    1296        4690 :   if (dp)
    1297             :   { /* multiply by y[0]^dp   [beware dummy vars from FpX_FpXY_resultant] */
    1298         960 :     GEN t = Flx_powu(gel(y,0), dp, p);
    1299        3834 :     for (i=2; i<lx; i++)
    1300        2873 :       gel(x,i) = Flx_mul(gel(x,i), t, p);
    1301             :   }
    1302        4691 :   return gerepilecopy(av, x);
    1303             : }
    1304             : 
    1305             : /* return a Flx */
    1306             : GEN
    1307        1515 : FlxX_resultant(GEN u, GEN v, ulong p, long sx)
    1308             : {
    1309        1515 :   pari_sp av = avma, av2;
    1310             :   long degq,dx,dy,du,dv,dr,signh;
    1311             :   GEN z,g,h,r,p1;
    1312             : 
    1313        1515 :   dx=degpol(u); dy=degpol(v); signh=1;
    1314        1516 :   if (dx < dy)
    1315             :   {
    1316          28 :     swap(u,v); lswap(dx,dy);
    1317          28 :     if (both_odd(dx, dy)) signh = -signh;
    1318             :   }
    1319        1516 :   if (dy < 0) return zero_Flx(sx);
    1320        1516 :   if (dy==0) return gerepileupto(av, Flx_powu(gel(v,2),dx,p));
    1321             : 
    1322        1516 :   g = h = pol1_Flx(sx); av2 = avma;
    1323             :   for(;;)
    1324             :   {
    1325        4681 :     r = FlxX_pseudorem(u,v,p); dr = lg(r);
    1326        4686 :     if (dr == 2) { avma = av; return zero_Flx(sx); }
    1327        4686 :     du = degpol(u); dv = degpol(v); degq = du-dv;
    1328        4687 :     u = v; p1 = g; g = leading_coeff(u);
    1329        4687 :     switch(degq)
    1330             :     {
    1331           0 :       case 0: break;
    1332             :       case 1:
    1333        3467 :         p1 = Flx_mul(h,p1, p); h = g; break;
    1334             :       default:
    1335        1220 :         p1 = Flx_mul(Flx_powu(h,degq,p), p1, p);
    1336        1219 :         h = Flx_div(Flx_powu(g,degq,p), Flx_powu(h,degq-1,p), p);
    1337             :     }
    1338        4678 :     if (both_odd(du,dv)) signh = -signh;
    1339        4679 :     v = FlxY_Flx_div(r, p1, p);
    1340        4681 :     if (dr==3) break;
    1341        3166 :     if (gc_needed(av2,1))
    1342             :     {
    1343           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"resultant_all, dr = %ld",dr);
    1344           0 :       gerepileall(av2,4, &u, &v, &g, &h);
    1345             :     }
    1346        3166 :   }
    1347        1515 :   z = gel(v,2);
    1348        1515 :   if (dv > 1) z = Flx_div(Flx_powu(z,dv,p), Flx_powu(h,dv-1,p), p);
    1349        1515 :   if (signh < 0) z = Flx_neg(z,p);
    1350        1515 :   return gerepileupto(av, z);
    1351             : }
    1352             : 
    1353             : /* Warning:
    1354             :  * This function switches between valid and invalid variable ordering*/
    1355             : 
    1356             : static GEN
    1357        1613 : FlxY_to_FlyX(GEN b, long sv)
    1358             : {
    1359        1613 :   long i, n=-1;
    1360        1613 :   long sw = b[1]&VARNBITS;
    1361        1613 :   for(i=2;i<lg(b);i++) n = maxss(n,lgpol(gel(b,i)));
    1362        1622 :   return Flm_to_FlxX(Flm_transpose(FlxX_to_Flm(b,n)),sv,sw);
    1363             : }
    1364             : 
    1365             : /* Return a Fly*/
    1366             : GEN
    1367        1615 : Flx_FlxY_resultant(GEN a, GEN b, ulong pp)
    1368             : {
    1369        1615 :   pari_sp ltop=avma;
    1370        1615 :   long dres = degpol(a)*degpol(b);
    1371        1617 :   long sx=a[1], sy=b[1]&VARNBITS;
    1372             :   GEN z;
    1373        1617 :   b = FlxY_to_FlyX(b,sx);
    1374        1620 :   if ((ulong)dres >= pp)
    1375        1516 :     z = FlxX_resultant(Fly_to_FlxY(a, sy), b, pp, sx);
    1376             :   else
    1377         104 :     z = Flx_FlxY_resultant_polint(a, b, pp, (ulong)dres, sy);
    1378        1619 :   return gerepileupto(ltop,z);
    1379             : }
    1380             : 
    1381             : /* return a t_POL (in variable v >= 0) whose coeffs are the coeffs of b,
    1382             :  * in variable v. This is an incorrect PARI object if initially varn(b) << v.
    1383             :  * We could return a vector of coeffs, but it is convenient to have degpol()
    1384             :  * and friends available. Even in that case, it will behave nicely with all
    1385             :  * functions treating a polynomial as a vector of coeffs (eg poleval).
    1386             :  * FOR INTERNAL USE! */
    1387             : GEN
    1388        5082 : swap_vars(GEN b0, long v)
    1389             : {
    1390        5082 :   long i, n = RgX_degree(b0, v);
    1391             :   GEN b, x;
    1392        5082 :   if (n < 0) return pol_0(v);
    1393        5082 :   b = cgetg(n+3, t_POL); x = b + 2;
    1394        5082 :   b[1] = evalsigne(1) | evalvarn(v);
    1395        5082 :   for (i=0; i<=n; i++) gel(x,i) = polcoeff_i(b0, i, v);
    1396        5082 :   return b;
    1397             : }
    1398             : 
    1399             : /* assume varn(b) << varn(a) */
    1400             : /* return a FpY*/
    1401             : GEN
    1402        1591 : FpX_FpXY_resultant(GEN a, GEN b, GEN p)
    1403             : {
    1404        1591 :   long i,n,dres, db, vY = varn(b), vX = varn(a);
    1405             :   GEN la,x,y;
    1406             : 
    1407        1591 :   if (lgefint(p) == 3)
    1408             :   {
    1409        1590 :     ulong pp = uel(p,2);
    1410        1590 :     b = ZXX_to_FlxX(b, pp, vX);
    1411        1592 :     a = ZX_to_Flx(a, pp);
    1412        1589 :     x = Flx_FlxY_resultant(a, b, pp);
    1413        1594 :     return Flx_to_ZX(x);
    1414             :   }
    1415           1 :   db = RgXY_degreex(b);
    1416           1 :   dres = degpol(a)*degpol(b);
    1417           1 :   la = leading_coeff(a);
    1418           1 :   x = cgetg(dres+2, t_VEC);
    1419           1 :   y = cgetg(dres+2, t_VEC);
    1420             :  /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
    1421             :   * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
    1422           3 :   for (i=0,n = 1; i < dres; n++)
    1423             :   {
    1424           2 :     gel(x,++i) = utoipos(n);
    1425           2 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1426           2 :     gel(x,++i) = subis(p,n);
    1427           2 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
    1428             :   }
    1429           1 :   if (i == dres)
    1430             :   {
    1431           0 :     gel(x,++i) = gen_0;
    1432           0 :     gel(y,i) = FpX_FpXY_eval_resultant(a,b, gel(x,i), p,la,db,vY);
    1433             :   }
    1434           1 :   return FpV_polint(x,y, p, vY);
    1435             : }
    1436             : 
    1437             : GEN
    1438         441 : FpX_direct_compositum(GEN a, GEN b, GEN p)
    1439             : {
    1440         441 :   GEN x = deg1pol_shallow(gen_1, pol_x(varn(a)), fetch_var_higher()); /* x+y */
    1441         441 :   x = FpX_FpXY_resultant(a, poleval(b,x),p);
    1442         441 :   (void)delete_var(); return x;
    1443             : }
    1444             : 
    1445             : /* 0, 1, -1, 2, -2, ... */
    1446             : #define next_lambda(a) (a>0 ? -a : 1-a)
    1447             : GEN
    1448           0 : FpX_compositum(GEN a, GEN b, GEN p)
    1449             : {
    1450           0 :   long k, v = fetch_var_higher();
    1451           0 :   for (k = 1;; k = next_lambda(k))
    1452             :   {
    1453           0 :     GEN x = deg1pol_shallow(gen_1, gmulsg(k, pol_x(v)), 0); /* x + k y */
    1454           0 :     GEN C = FpX_FpXY_resultant(a, poleval(b,x),p);
    1455           0 :     if (FpX_is_squarefree(C, p)) { (void)delete_var(); return C; }
    1456           0 :   }
    1457             : }
    1458             : 
    1459             : /* Assume A in Z[Y], B in Q[Y][X], and Res_Y(A, B) in Z[X].
    1460             :  * If lambda = NULL, return Res_Y(A,B).
    1461             :  * Otherwise, find a small lambda (start from *lambda, use the sequence above)
    1462             :  * such that R(X) = Res_Y(A(Y), B(X + lambda Y)) is squarefree, reset *lambda
    1463             :  * to the chosen value and return R
    1464             :  *
    1465             :  * If LERS is non-NULL, set it to the Last non-constant polynomial in the
    1466             :  * Euclidean Remainder Sequence */
    1467             : GEN
    1468        3885 : ZX_ZXY_resultant_all(GEN A, GEN B0, long *plambda, GEN *LERS)
    1469             : {
    1470        3885 :   int checksqfree = plambda? 1: 0, stable;
    1471        3885 :   long lambda = plambda? *plambda: 0, cnt = 0;
    1472             :   ulong bound, dp;
    1473        3885 :   pari_sp av = avma, av2 = 0;
    1474        3885 :   long i,n, lb, degA = degpol(A), dres = degA*degpol(B0);
    1475        3885 :   long v = fetch_var_higher();
    1476        3885 :   long vX = varn(B0), vY = varn(A); /* assume vY has lower priority */
    1477        3885 :   long sX = evalvarn(vX);
    1478             :   GEN x, y, dglist, dB, B, q, a, b, ev, H, H0, H1, Hp, H0p, H1p, C0, C1;
    1479             :   forprime_t S;
    1480             : 
    1481        3885 :   dglist = Hp = H0p = H1p = C0 = C1 = NULL; /* gcc -Wall */
    1482        3885 :   if (LERS)
    1483             :   {
    1484        1127 :     if (!checksqfree)
    1485           0 :       pari_err_BUG("ZX_ZXY_resultant_all [LERS != NULL needs lambda]");
    1486        1127 :     C0 = cgetg(dres+2, t_VECSMALL);
    1487        1127 :     C1 = cgetg(dres+2, t_VECSMALL);
    1488        1127 :     dglist = cgetg(dres+1, t_VECSMALL);
    1489             :   }
    1490        3885 :   x = cgetg(dres+2, t_VECSMALL);
    1491        3885 :   y = cgetg(dres+2, t_VECSMALL);
    1492        3885 :   B0 = Q_remove_denom(B0, &dB);
    1493        3885 :   if (!dB) B0 = leafcopy(B0);
    1494        3885 :   A = leafcopy(A);
    1495        3885 :   B = B0;
    1496        3885 :   setvarn(A,v);
    1497             :   /* make sure p large enough */
    1498             : INIT:
    1499             :   /* always except the first time */
    1500        5047 :   if (av2) { avma = av2; lambda = next_lambda(lambda); }
    1501        5047 :   if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
    1502        5047 :   B = swap_vars(B, vY); setvarn(B,v);
    1503             :   /* B0(lambda v + x, v) */
    1504        5047 :   if (DEBUGLEVEL>4 && checksqfree) err_printf("Trying lambda = %ld\n", lambda);
    1505        5047 :   av2 = avma;
    1506             : 
    1507        5047 :   if (degA <= 3)
    1508             :   { /* sub-resultant faster for small degrees */
    1509        3437 :     if (LERS)
    1510             :     { /* implies checksqfree */
    1511        1582 :       H = resultant_all(A,B,&q);
    1512        1582 :       if (typ(q) != t_POL || degpol(q)!=1) goto INIT;
    1513         980 :       H0 = gel(q,2);
    1514         980 :       if (typ(H0) == t_POL) setvarn(H0,vX); else H0 = scalarpol(H0,vX);
    1515         980 :       H1 = gel(q,3);
    1516         980 :       if (typ(H1) == t_POL) setvarn(H1,vX); else H1 = scalarpol(H1,vX);
    1517             :     }
    1518             :     else
    1519        1855 :       H = resultant(A,B);
    1520        2835 :     if (checksqfree && !ZX_is_squarefree(H)) goto INIT;
    1521        2660 :     goto END;
    1522             :   }
    1523             : 
    1524        1610 :   H = H0 = H1 = NULL;
    1525        1610 :   lb = lg(B);
    1526        1610 :   bound = ZX_ZXY_ResBound(A, B, dB);
    1527        1610 :   if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
    1528        1610 :   dp = 1;
    1529        1610 :   init_modular_big(&S);
    1530             :   while (1)
    1531             :   {
    1532        4747 :     ulong p = u_forprime_next(&S);
    1533        4747 :     if (dB) { dp = umodiu(dB, p); if (!dp) continue; }
    1534             : 
    1535        4747 :     a = ZX_to_Flx(A, p);
    1536        4747 :     b = ZXX_to_FlxX(B, p, varn(A));
    1537        4747 :     if (LERS)
    1538             :     {
    1539             :       GEN Hi;
    1540         397 :       if (degpol(a) < degA || lg(b) < lb) continue; /* p | lc(A)lc(B) */
    1541         397 :       if (checksqfree)
    1542             :       { /* find degree list for generic Euclidean Remainder Sequence */
    1543         196 :         long goal = minss(degpol(a), degpol(b)); /* longest possible */
    1544         196 :         for (n=1; n <= goal; n++) dglist[n] = 0;
    1545         196 :         setlg(dglist, 1);
    1546        1645 :         for (n=0; n <= dres; n++)
    1547             :         {
    1548        1561 :           ev = FlxY_evalx_drop(b, n, p);
    1549        1561 :           Flx_resultant_set_dglist(a, ev, dglist, p);
    1550        1561 :           if (lg(dglist)-1 == goal) break;
    1551             :         }
    1552             :         /* last pol in ERS has degree > 1 ? */
    1553         196 :         goal = lg(dglist)-1;
    1554         196 :         if (degpol(B) == 1) { if (!goal) goto INIT; }
    1555             :         else
    1556             :         {
    1557         189 :           if (goal <= 1) goto INIT;
    1558         182 :           if (dglist[goal] != 0 || dglist[goal-1] != 1) goto INIT;
    1559             :         }
    1560         189 :         if (DEBUGLEVEL>4)
    1561           0 :           err_printf("Degree list for ERS (trials: %ld) = %Ps\n",n+1,dglist);
    1562             :       }
    1563             : 
    1564        7386 :       for (i=0,n = 0; i <= dres; n++)
    1565             :       {
    1566        6996 :         ev = FlxY_evalx_drop(b, n, p);
    1567        6996 :         x[++i] = n; y[i] = Flx_resultant_all(a, ev, C0+i, C1+i, dglist, p);
    1568        6996 :         if (!C1[i]) i--; /* C1(i) = 0. No way to recover C0(i) */
    1569             :       }
    1570         390 :       Hi = Flv_Flm_polint(x, mkvec3(y,C0,C1), p, 0);
    1571         390 :       Hp = gel(Hi,1); H0p = gel(Hi,2); H1p = gel(Hi,3);
    1572             :     }
    1573             :     else
    1574             :     {
    1575        4350 :       long dropa = degA - degpol(a), dropb = lb - lg(b);
    1576        4350 :       Hp = Flx_FlxY_resultant_polint(a, b, p, (ulong)dres, sX);
    1577        4350 :       if (dropa && dropb)
    1578           0 :         Hp = zero_Flx(sX);
    1579             :       else {
    1580        4350 :         if (dropa)
    1581             :         { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    1582           0 :           GEN c = gel(b,lb-1); /* lc(B) */
    1583           0 :           if (!odd(lb)) c = Flx_neg(c, p); /* deg B = lb - 3 */
    1584           0 :           if (!Flx_equal1(c)) {
    1585           0 :             c = Flx_powu(c, dropa, p);
    1586           0 :             if (!Flx_equal1(c)) Hp = Flx_mul(Hp, c, p);
    1587             :           }
    1588             :         }
    1589        4350 :         else if (dropb)
    1590             :         { /* multiply by lc(A)^(deg B - deg b) */
    1591           0 :           ulong c = a[degA+2]; /* lc(A) */
    1592           0 :           c = Fl_powu(c, dropb, p);
    1593           0 :           if (c != 1) Hp = Flx_Fl_mul(Hp, c, p);
    1594             :         }
    1595             :       }
    1596             :     }
    1597        4740 :     if (!H && degpol(Hp) != dres) continue;
    1598        4740 :     if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1599        4740 :     if (checksqfree) {
    1600        1057 :       if (!Flx_is_squarefree(Hp, p)) goto INIT;
    1601         679 :       if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    1602         679 :       checksqfree = 0;
    1603             :     }
    1604             : 
    1605        4362 :     if (!H)
    1606             :     { /* initialize */
    1607        1225 :       q = utoipos(p); stable = 0;
    1608        1225 :       H = ZX_init_CRT(Hp, p,vX);
    1609        1225 :       if (LERS) {
    1610         189 :         H0= ZX_init_CRT(H0p, p,vX);
    1611         189 :         H1= ZX_init_CRT(H1p, p,vX);
    1612             :       }
    1613             :     }
    1614             :     else
    1615             :     {
    1616        3137 :       if (LERS) {
    1617         201 :         GEN qp = muliu(q,p);
    1618         402 :         stable  = ZX_incremental_CRT_raw(&H, Hp, q,qp, p)
    1619         201 :                 & ZX_incremental_CRT_raw(&H0,H0p, q,qp, p)
    1620         201 :                 & ZX_incremental_CRT_raw(&H1,H1p, q,qp, p);
    1621         201 :         q = qp;
    1622             :       }
    1623             :       else
    1624        2936 :         stable = ZX_incremental_CRT(&H, Hp, &q, p);
    1625             :     }
    1626             :     /* could make it probabilistic for H ? [e.g if stable twice, etc]
    1627             :      * Probabilistic anyway for H0, H1 */
    1628        4362 :     if (DEBUGLEVEL>5 && (stable ||  ++cnt==100))
    1629           0 :     { cnt=0; err_printf("%ld%%%s ",100*expi(q)/bound,stable?"s":""); }
    1630        4362 :     if (stable && (ulong)expi(q) >= bound) break; /* DONE */
    1631        3137 :     if (gc_needed(av,2))
    1632             :     {
    1633           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_rnfequation");
    1634           0 :       gerepileall(av2, LERS? 4: 2, &H, &q, &H0, &H1);
    1635             :     }
    1636        3137 :   }
    1637             : END:
    1638        3885 :   if (DEBUGLEVEL>5) err_printf(" done\n");
    1639        3885 :   setvarn(H, vX); (void)delete_var();
    1640        3885 :   if (plambda) *plambda = lambda;
    1641        3885 :   if (LERS)
    1642             :   {
    1643        1127 :     *LERS = mkvec2(H0,H1);
    1644        1127 :     gerepileall(av, 2, &H, LERS);
    1645        1127 :     return H;
    1646             :   }
    1647        2758 :   return gerepilecopy(av, H);
    1648             : }
    1649             : 
    1650             : GEN
    1651        2079 : ZX_ZXY_rnfequation(GEN A, GEN B, long *lambda)
    1652             : {
    1653        2079 :   return ZX_ZXY_resultant_all(A, B, lambda, NULL);
    1654             : }
    1655             : 
    1656             : /* If lambda = NULL, return caract(Mod(A, T)), T,A in Z[X].
    1657             :  * Otherwise find a small lambda such that caract (Mod(A + lambda X, T)) is
    1658             :  * squarefree */
    1659             : GEN
    1660        1855 : ZXQ_charpoly_sqf(GEN A, GEN T, long *lambda, long v)
    1661             : {
    1662        1855 :   pari_sp av = avma;
    1663             :   GEN R, a;
    1664             :   long dA;
    1665             :   int delvar;
    1666             : 
    1667        1855 :   if (v < 0) v = 0;
    1668        1855 :   switch (typ(A))
    1669             :   {
    1670        1855 :     case t_POL: dA = degpol(A); if (dA > 0) break;
    1671           0 :       A = constant_coeff(A);
    1672             :     default:
    1673           0 :       if (lambda) { A = scalar_ZX_shallow(A,varn(T)); dA = 0; break;}
    1674           0 :       return gerepileupto(av, gpowgs(gsub(pol_x(v), A), degpol(T)));
    1675             :   }
    1676        1855 :   delvar = 0;
    1677        1855 :   if (varn(T) == 0)
    1678             :   {
    1679        1799 :     long v0 = fetch_var(); delvar = 1;
    1680        1799 :     T = leafcopy(T); setvarn(T,v0);
    1681        1799 :     A = leafcopy(A); setvarn(A,v0);
    1682             :   }
    1683        1855 :   R = ZX_ZXY_rnfequation(T, deg1pol_shallow(gen_1, gneg_i(A), 0), lambda);
    1684        1855 :   if (delvar) (void)delete_var();
    1685        1855 :   setvarn(R, v); a = leading_coeff(T);
    1686        1855 :   if (!gequal1(a)) R = gdiv(R, powiu(a, dA));
    1687        1855 :   return gerepileupto(av, R);
    1688             : }
    1689             : 
    1690             : 
    1691             : /* charpoly(Mod(A,T)), A may be in Q[X], but assume T and result are integral */
    1692             : GEN
    1693       56848 : ZXQ_charpoly(GEN A, GEN T, long v)
    1694             : {
    1695       56848 :   return (degpol(T) < 16) ? RgXQ_charpoly(A,T,v): ZXQ_charpoly_sqf(A,T, NULL, v);
    1696             : }
    1697             : 
    1698             : GEN
    1699       46844 : QXQ_charpoly(GEN A, GEN T, long v)
    1700             : {
    1701       46844 :   pari_sp av = avma;
    1702       46844 :   GEN den, B = Q_remove_denom(A, &den);
    1703       46844 :   GEN P = ZXQ_charpoly(B, T, v);
    1704       46844 :   return gerepilecopy(av, den ? RgX_rescale(P, ginv(den)): P);
    1705             : }
    1706             : 
    1707             : static GEN
    1708      100227 : trivial_case(GEN A, GEN B)
    1709             : {
    1710             :   long d;
    1711      100227 :   if (typ(A) == t_INT) return powiu(A, degpol(B));
    1712       97790 :   d = degpol(A);
    1713       97790 :   if (d == 0) return trivial_case(gel(A,2),B);
    1714       96585 :   if (d < 0) return gen_0;
    1715       96570 :   return NULL;
    1716             : }
    1717             : 
    1718             : static long
    1719       47710 : get_nbprimes(ulong bound, ulong *pt_start)
    1720             : {
    1721             : #ifdef LONG_IS_64BIT
    1722       40770 :   ulong pstart = 4611686018427388039UL;
    1723             : #else
    1724        6940 :   ulong pstart = 1073741827UL;
    1725             : #endif
    1726       47710 :   *pt_start = pstart;
    1727       47710 :   return (bound/expu(pstart))+1;
    1728             : }
    1729             : 
    1730             : static ulong
    1731      316858 : ZX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, ulong p)
    1732             : {
    1733      316858 :   pari_sp av = avma;
    1734             :   ulong H;
    1735             :   long dropa, dropb;
    1736      316858 :   ulong dp = dB ? umodiu(dB, p): 1;
    1737      316899 :   if (!b) b = Flx_deriv(a, p);
    1738      316946 :   dropa = degA - degpol(a);
    1739      316950 :   dropb = degB - degpol(b);
    1740      316954 :   if (dropa && dropb) /* p | lc(A), p | lc(B) */
    1741           0 :   { avma = av; return 0; }
    1742      316954 :   H = Flx_resultant(a, b, p);
    1743      316931 :   if (dropa)
    1744             :   { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
    1745           0 :     ulong c = b[degB+2]; /* lc(B) */
    1746           0 :     if (odd(degB)) c = p - c;
    1747           0 :     c = Fl_powu(c, dropa, p);
    1748           0 :     if (c != 1) H = Fl_mul(H, c, p);
    1749             :   }
    1750      316931 :   else if (dropb)
    1751             :   { /* multiply by lc(A)^(deg B - deg b) */
    1752           0 :     ulong c = a[degA+2]; /* lc(A) */
    1753           0 :     c = Fl_powu(c, dropb, p);
    1754           0 :     if (c != 1) H = Fl_mul(H, c, p);
    1755             :   }
    1756      316919 :   if (dp != 1) H = Fl_mul(H, Fl_powu(Fl_inv(dp,p), degA, p), p);
    1757      316919 :   avma = av; return H;
    1758             : }
    1759             : 
    1760             : /* If B=NULL, assume B=A' */
    1761             : static GEN
    1762      205213 : ZX_resultant_slice(GEN A, GEN B, GEN dB, GEN P, GEN *mod)
    1763             : {
    1764      205213 :   pari_sp av = avma;
    1765      205213 :   long degA, degB, i, n = lg(P)-1;
    1766             :   GEN H, T;
    1767             : 
    1768      205213 :   degA = degpol(A);
    1769      205222 :   degB = B ? degpol(B): degA - 1;
    1770      205264 :   if (n == 1)
    1771             :   {
    1772      166451 :     ulong Hp, p = uel(P,1);
    1773             :     GEN a, b;
    1774      166451 :     a = ZX_to_Flx(A, p), b = B ? ZX_to_Flx(B, p): NULL;
    1775      166424 :     Hp = ZX_resultant_prime(a, b, dB, degA, degB, p);
    1776      166451 :     avma = av;
    1777      166451 :     *mod = utoi(p); return utoi(Hp);
    1778             :   }
    1779       38813 :   T = ZV_producttree(P);
    1780       38813 :   A = ZX_nv_mod_tree(A, P, T);
    1781       38813 :   if (B) B = ZX_nv_mod_tree(B, P, T);
    1782       38812 :   H = cgetg(n+1, t_VECSMALL);
    1783      189281 :   for(i=1; i <= n; i++)
    1784             :   {
    1785      150463 :     ulong p = P[i];
    1786      150463 :     GEN a = gel(A, i), b = B ? gel(B, i): NULL;
    1787      150463 :     H[i] = ZX_resultant_prime(a, b, dB, degA, degB, p);
    1788             :   }
    1789       38818 :   H = ZV_chinese_tree(H, P, T, mod);
    1790       38814 :   gerepileall(av, 2, &H, mod);
    1791       38816 :   return H;
    1792             : }
    1793             : 
    1794             : GEN
    1795      175068 : ZX_resultant_worker(GEN P, GEN A, GEN B, GEN dB)
    1796             : {
    1797      175068 :   GEN V = cgetg(3, t_VEC);
    1798      175138 :   if (isintzero(B)) B = NULL;
    1799      175088 :   if (isintzero(dB)) dB = NULL;
    1800      175134 :   gel(V,1) = ZX_resultant_slice(A,B,dB,P,&gel(V,2));
    1801      175067 :   return V;
    1802             : }
    1803             : 
    1804             : static GEN
    1805      205352 : primelist_disc(ulong *p, long n, GEN dB)
    1806             : {
    1807      205352 :   GEN P = cgetg(n+1, t_VECSMALL);
    1808             :   long i;
    1809      522362 :   for (i=1; i <= n; i++, *p = unextprime(*p+1))
    1810             :   {
    1811      317010 :     if (dB && umodiu(dB, *p)==0) { i--; continue; }
    1812      317010 :     P[i] = *p;
    1813             :   }
    1814      205352 :   return P;
    1815             : }
    1816             : 
    1817             : /* Res(A, B/dB), assuming the A,B in Z[X] and result is integer */
    1818             : /* if B=NULL, take B = A' */
    1819             : GEN
    1820       50162 : ZX_resultant_all(GEN A, GEN B, GEN dB, ulong bound)
    1821             : {
    1822             :   ulong p;
    1823       50162 :   pari_sp av = avma;
    1824             :   long n, m;
    1825             :   GEN  H, P, mod;
    1826       50162 :   int is_disc = !B;
    1827       50162 :   if (is_disc) B = ZX_deriv(A);
    1828             : 
    1829       50162 :   if ((H = trivial_case(A,B)) || (H = trivial_case(B,A))) return H;
    1830       47710 :   if (!bound) bound = ZX_ZXY_ResBound(A, B, dB);
    1831       47710 :   n = get_nbprimes(bound+1, &p);/* +1 to account for sign */
    1832       47710 :   if (is_disc)
    1833       25273 :     B = NULL;
    1834       47710 :   m = minss(degpol(A)+(B ? degpol(B): 0), n);
    1835       47710 :   if (m == 1)
    1836             :   {
    1837       24061 :     GEN P = primelist_disc(&p, n, dB);
    1838       24061 :     H = ZX_resultant_slice(A, B, dB, P, &mod);
    1839             :   }
    1840             :   else
    1841             :   {
    1842       23649 :     long i, s = n/m, r = n - m*s, di = 0;
    1843       23649 :     GEN worker = strtoclosure("_ZX_resultant_worker", 3, A, B?B:gen_0, dB?dB:gen_0);
    1844             :     struct pari_mt pt;
    1845             :     long pending;
    1846       23649 :     if (DEBUGLEVEL > 4)
    1847           0 :       err_printf("ZX_resultant: bound 2^%ld, nb primes: %ld\n",bound, n);
    1848       23649 :     H = cgetg(m+1+!!r, t_VEC); P = cgetg(m+1+!!r, t_VEC);
    1849       23649 :     mt_queue_start(&pt, worker);
    1850      218211 :     for (i=1; i<=m || pending; i++)
    1851             :     {
    1852             :       GEN done;
    1853      194562 :       mt_queue_submit(&pt, i, i<=m ? mkvec(primelist_disc(&p, s, dB)): NULL);
    1854      194562 :       done = mt_queue_get(&pt, NULL, &pending);
    1855      194562 :       if (done)
    1856             :       {
    1857      175183 :         di++;
    1858      175183 :         gel(H, di) = gel(done,1);
    1859      175183 :         gel(P, di) = gel(done,2);
    1860      175183 :         if (DEBUGLEVEL>5) err_printf("%ld%% ",100*di/m);
    1861             :       }
    1862             :     }
    1863       23649 :     mt_queue_end(&pt);
    1864       23649 :     if (r)
    1865             :     {
    1866        6108 :       GEN Pr = primelist_disc(&p, r, dB);
    1867        6108 :       gel(H, m+1) = ZX_resultant_slice(A, B, dB, Pr, &gel(P, m+1));
    1868             :     }
    1869       23649 :     H = ZV_chinese(H, P, &mod);
    1870       23649 :     if (DEBUGLEVEL>5) err_printf("done\n");
    1871             :   }
    1872       47710 :   H = Fp_center(H, mod, shifti(mod,-1));
    1873       47710 :   return gerepileuptoint(av, H);
    1874             : }
    1875             : 
    1876             : /* A0 and B0 in Q[X] */
    1877             : GEN
    1878       10121 : QX_resultant(GEN A0, GEN B0)
    1879             : {
    1880             :   GEN s, a, b, A, B;
    1881       10121 :   pari_sp av = avma;
    1882             : 
    1883       10121 :   A = Q_primitive_part(A0, &a);
    1884       10121 :   B = Q_primitive_part(B0, &b);
    1885       10121 :   s = ZX_resultant(A, B);
    1886       10121 :   if (!signe(s)) { avma = av; return gen_0; }
    1887       10121 :   if (a) s = gmul(s, gpowgs(a,degpol(B)));
    1888       10121 :   if (b) s = gmul(s, gpowgs(b,degpol(A)));
    1889       10121 :   return gerepileupto(av, s);
    1890             : }
    1891             : 
    1892             : GEN
    1893       24244 : ZX_resultant(GEN A, GEN B) { return ZX_resultant_all(A,B,NULL,0); }
    1894             : 
    1895             : GEN
    1896           0 : QXQ_intnorm(GEN A, GEN B)
    1897             : {
    1898             :   GEN c, n, R, lB;
    1899           0 :   long dA = degpol(A), dB = degpol(B);
    1900           0 :   pari_sp av = avma;
    1901           0 :   if (dA < 0) return gen_0;
    1902           0 :   A = Q_primitive_part(A, &c);
    1903           0 :   if (!c || typ(c) == t_INT) {
    1904           0 :     n = c;
    1905           0 :     R = ZX_resultant(B, A);
    1906             :   } else {
    1907           0 :     n = gel(c,1);
    1908           0 :     R = ZX_resultant_all(B, A, gel(c,2), 0);
    1909             :   }
    1910           0 :   if (n && !equali1(n)) R = mulii(R, powiu(n, dB));
    1911           0 :   lB = leading_coeff(B);
    1912           0 :   if (!equali1(lB)) R = diviiexact(R, powiu(lB, dA));
    1913           0 :   return gerepileuptoint(av, R);
    1914             : }
    1915             : 
    1916             : GEN
    1917           0 : QXQ_norm(GEN A, GEN B)
    1918             : {
    1919             :   GEN c, R, lB;
    1920           0 :   long dA = degpol(A), dB = degpol(B);
    1921           0 :   pari_sp av = avma;
    1922           0 :   if (dA < 0) return gen_0;
    1923           0 :   A = Q_primitive_part(A, &c);
    1924           0 :   R = ZX_resultant(B, A);
    1925           0 :   if (c) R = gmul(R, gpowgs(c, dB));
    1926           0 :   lB = leading_coeff(B);
    1927           0 :   if (!equali1(lB)) R = gdiv(R, gpowgs(lB, dA));
    1928           0 :   return gerepileupto(av, R);
    1929             : }
    1930             : 
    1931             : /* assume x has integral coefficients */
    1932             : GEN
    1933       26414 : ZX_disc_all(GEN x, ulong bound)
    1934             : {
    1935       26414 :   pari_sp av = avma;
    1936             :   GEN l, R;
    1937       26414 :   long s, d = degpol(x);
    1938       26414 :   if (d <= 1) return d ? gen_1: gen_0;
    1939       25273 :   s = (d & 2) ? -1: 1;
    1940       25273 :   l = leading_coeff(x);
    1941       25273 :   R = ZX_resultant_all(x, NULL, NULL, bound);
    1942       25273 :   if (is_pm1(l))
    1943       22487 :   { if (signe(l) < 0) s = -s; }
    1944             :   else
    1945        2786 :     R = diviiexact(R,l);
    1946       25273 :   if (s == -1) togglesign_safe(&R);
    1947       25273 :   return gerepileuptoint(av,R);
    1948             : }
    1949       24565 : GEN ZX_disc(GEN x) { return ZX_disc_all(x,0); }
    1950             : 
    1951             : GEN
    1952           0 : QX_disc(GEN x)
    1953             : {
    1954           0 :   pari_sp av = avma;
    1955           0 :   GEN c, d = ZX_disc( Q_primitive_part(x, &c) );
    1956           0 :   if (c) d = gmul(d, gpowgs(c, 2*degpol(x) - 2));
    1957           0 :   return gerepileupto(av, d);
    1958             : }
    1959             : 
    1960             : /* lift(1 / Mod(A,B)). B a ZX, A a scalar or a QX */
    1961             : GEN
    1962        7752 : QXQ_inv(GEN A, GEN B)
    1963             : {
    1964             :   GEN D, cU, q, U, V;
    1965             :   ulong p;
    1966        7752 :   pari_sp av2, av = avma;
    1967             :   forprime_t S;
    1968             :   pari_timer ti;
    1969        7752 :   if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
    1970             :   /* A a QX, B a ZX */
    1971        7752 :   if (degpol(A) < 15) return RgXQ_inv(A,B);
    1972           7 :   A = Q_primitive_part(A, &D);
    1973             :   /* A, B in Z[X] */
    1974           7 :   init_modular_small(&S);
    1975           7 :   if (DEBUGLEVEL>5) timer_start(&ti);
    1976           7 :   av2 = avma; U = NULL;
    1977         577 :   while ((p = u_forprime_next(&S)))
    1978             :   {
    1979             :     GEN a, b, qp, Up, Vp;
    1980             :     int stable;
    1981             : 
    1982         570 :     a = ZX_to_Flx(A, p);
    1983         570 :     b = ZX_to_Flx(B, p);
    1984             :     /* if p | Res(A/G, B/G), discard */
    1985         577 :     if (!Flx_extresultant(b,a,p, &Vp,&Up)) continue;
    1986             : 
    1987         570 :     if (!U)
    1988             :     { /* First time */
    1989           7 :       U = ZX_init_CRT(Up,p,varn(A));
    1990           7 :       V = ZX_init_CRT(Vp,p,varn(A));
    1991           7 :       q = utoipos(p); continue;
    1992             :     }
    1993         563 :     if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: mod %ld (bound 2^%ld)", p,expi(q));
    1994         563 :     qp = muliu(q,p);
    1995        1126 :     stable = ZX_incremental_CRT_raw(&U, Up, q,qp, p)
    1996         563 :            & ZX_incremental_CRT_raw(&V, Vp, q,qp, p);
    1997         563 :     if (stable)
    1998             :     { /* all stable: check divisibility */
    1999           7 :       GEN res = ZX_add(ZX_mul(A,U), ZX_mul(B,V));
    2000           7 :       if (degpol(res) == 0) {
    2001           7 :         res = gel(res,2);
    2002           7 :         D = D? gmul(D, res): res;
    2003          14 :         break;
    2004             :       } /* DONE */
    2005           0 :       if (DEBUGLEVEL) err_printf("QXQ_inv: char 0 check failed");
    2006             :     }
    2007         556 :     q = qp;
    2008         556 :     if (gc_needed(av,1))
    2009             :     {
    2010           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"QXQ_inv");
    2011           0 :       gerepileall(av2, 3, &q,&U,&V);
    2012             :     }
    2013             :   }
    2014           7 :   if (!p) pari_err_OVERFLOW("QXQ_inv [ran out of primes]");
    2015           7 :   cU = ZX_content(U);
    2016           7 :   if (!is_pm1(cU)) { U = Q_div_to_int(U, cU); D = gdiv(D, cU); }
    2017           7 :   return gerepileupto(av, RgX_Rg_div(U, D));
    2018             : }
    2019             : 
    2020             : /************************************************************************
    2021             :  *                                                                      *
    2022             :  *                   IRREDUCIBLE POLYNOMIAL / Fp                        *
    2023             :  *                                                                      *
    2024             :  ************************************************************************/
    2025             : 
    2026             : /* irreducible (unitary) polynomial of degree n over Fp */
    2027             : GEN
    2028           0 : ffinit_rand(GEN p,long n)
    2029             : {
    2030             :   for(;;) {
    2031           0 :     pari_sp av = avma;
    2032           0 :     GEN pol = ZX_add(pol_xn(n, 0), random_FpX(n-1,0, p));
    2033           0 :     if (FpX_is_irred(pol, p)) return pol;
    2034           0 :     avma = av;
    2035           0 :   }
    2036             : }
    2037             : 
    2038             : /* return an extension of degree 2^l of F_2, assume l > 0
    2039             :  * Not stack clean. */
    2040             : static GEN
    2041         288 : f2init(long l)
    2042             : {
    2043             :   GEN Q, T, S;
    2044             :   long i, v;
    2045             : 
    2046         288 :   if (l == 1) return polcyclo(3, 0);
    2047         253 :   v = fetch_var_higher();
    2048         254 :   S = mkpoln(4, gen_1,gen_1,gen_0,gen_0); /* y(y^2 + y) */
    2049         253 :   Q = mkpoln(3, gen_1,gen_1, S); /* x^2 + x + y(y^2+y) */
    2050         256 :   setvarn(Q, v);
    2051             : 
    2052             :   /* x^4+x+1, irred over F_2, minimal polynomial of a root of Q */
    2053         256 :   T = mkpoln(5, gen_1,gen_0,gen_0,gen_1,gen_1);
    2054         256 :   setvarn(T, v);
    2055             :   /* Q = x^2 + x + a(y) irred. over K = F2[y] / (T(y))
    2056             :    * ==> x^2 + x + a(y) b irred. over K for any root b of Q
    2057             :    * ==> x^2 + x + (b^2+b)b */
    2058         256 :   for (i=2; i<l; i++) T = FpX_FpXY_resultant(T, Q, gen_2); /* minpoly(b) / F2*/
    2059         256 :   (void)delete_var(); setvarn(T,0); return T;
    2060             : }
    2061             : 
    2062             : /* return an extension of degree p^l of F_p, assume l > 0
    2063             :  * Not stack clean. */
    2064             : GEN
    2065           0 : ffinit_Artin_Shreier(GEN ip, long l)
    2066             : {
    2067           0 :   long i, v, p = itos(ip);
    2068           0 :   GEN T, Q, xp = pol_xn(p,0); /* x^p */
    2069           0 :   T = ZX_sub(xp, deg1pol_shallow(gen_1,gen_1,0)); /* x^p - x - 1 */
    2070           0 :   if (l == 1) return T;
    2071             : 
    2072           0 :   v = fetch_var_higher();
    2073           0 :   setvarn(xp, v);
    2074           0 :   Q = ZX_sub(pol_xn(2*p-1,0), pol_xn(p,0));
    2075           0 :   Q = gsub(xp, deg1pol_shallow(gen_1, Q, v)); /* x^p - x - (y^(2p-1)-y^p) */
    2076           0 :   for (i = 2; i <= l; ++i) T = FpX_FpXY_resultant(T, Q, ip);
    2077           0 :   (void)delete_var(); setvarn(T,0); return T;
    2078             : }
    2079             : 
    2080             : /* check if polsubcyclo(n,l,0) is irreducible modulo p */
    2081             : static long
    2082       11594 : fpinit_check(GEN p, long n, long l)
    2083             : {
    2084             :   ulong q;
    2085       11594 :   if (!uisprime(n)) return 0;
    2086        5789 :   q = umodiu(p,n); if (!q) return 0;
    2087        5222 :   return cgcd((n-1)/Fl_order(q, n-1, n), l) == 1;
    2088             : }
    2089             : 
    2090             : /* let k=2 if p%4==1, and k=4 else and assume k*p does not divide l.
    2091             :  * Return an irreducible polynomial of degree l over F_p.
    2092             :  * Variant of Adleman and Lenstra "Finding irreducible polynomials over
    2093             :  * finite fields", ACM, 1986 (5) 350--355.
    2094             :  * Not stack clean */
    2095             : static GEN
    2096        2975 : fpinit(GEN p, long l)
    2097             : {
    2098        2975 :   ulong n = 1+l;
    2099        2975 :   while (!fpinit_check(p,n,l)) n += l;
    2100        2975 :   if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
    2101        2975 :   return FpX_red(polsubcyclo(n,l,0),p);
    2102             : }
    2103             : 
    2104             : static GEN
    2105        2726 : ffinit_fact(GEN p, long n)
    2106             : {
    2107        2726 :   GEN P, F = gel(factoru_pow(n),3);
    2108             :   long i;
    2109        2729 :   if (!odd(n) && absequaliu(p, 2))
    2110         290 :     P = f2init(vals(n)); /* if n is even, F[1] = 2^vals(n)*/
    2111             :   else
    2112        2439 :     P = fpinit(p, F[1]);
    2113        3170 :   for (i = 2; i < lg(F); ++i)
    2114         441 :     P = FpX_direct_compositum(fpinit(p, F[i]), P, p);
    2115        2729 :   return P;
    2116             : }
    2117             : 
    2118             : static GEN
    2119          96 : ffinit_nofact(GEN p, long n)
    2120             : {
    2121          96 :   GEN P, Q = NULL;
    2122          96 :   if (lgefint(p)==3)
    2123             :   {
    2124           0 :     ulong pp = p[2], q;
    2125           0 :     long v = u_lvalrem(n,pp,&q);
    2126           0 :     if (v>0)
    2127             :     {
    2128           0 :       Q = (pp == 2)? f2init(v): fpinit(p,n/q);
    2129           0 :       n = q;
    2130             :     }
    2131             :   }
    2132             :   /* n coprime to p */
    2133          96 :   if (n==1) P = Q;
    2134             :   else
    2135             :   {
    2136          96 :     P = fpinit(p, n);
    2137          96 :     if (Q) P = FpX_direct_compositum(P, Q, p);
    2138             :   }
    2139          96 :   return P;
    2140             : }
    2141             : 
    2142             : static GEN
    2143        3579 : init_Fq_i(GEN p, long n, long v)
    2144             : {
    2145             :   GEN P;
    2146        3579 :   if (n <= 0) pari_err_DOMAIN("ffinit", "degree", "<=", gen_0, stoi(n));
    2147        3581 :   if (typ(p) != t_INT) pari_err_TYPE("ffinit",p);
    2148        3581 :   if (signe(p) <= 0) pari_err_PRIME("ffinit",p);
    2149        3581 :   if (v < 0) v = 0;
    2150        3581 :   if (n == 1) return pol_x(v);
    2151        3399 :   if (fpinit_check(p, n+1, n)) return polcyclo(n+1, v);
    2152        2824 :   if (lgefint(p)-2 <= expu(n))
    2153        2726 :     P = ffinit_fact(p,n);
    2154             :   else
    2155          96 :     P = ffinit_nofact(p,n);
    2156        2825 :   setvarn(P, v); return P;
    2157             : }
    2158             : GEN
    2159        3453 : init_Fq(GEN p, long n, long v)
    2160             : {
    2161        3453 :   pari_sp av = avma;
    2162        3453 :   return gerepileupto(av, init_Fq_i(p, n, v));
    2163             : }
    2164             : GEN
    2165         126 : ffinit(GEN p, long n, long v)
    2166             : {
    2167         126 :   pari_sp av = avma;
    2168         126 :   return gerepileupto(av, FpX_to_mod(init_Fq_i(p, n, v), p));
    2169             : }
    2170             : 
    2171             : GEN
    2172        3178 : ffnbirred(GEN p, long n)
    2173             : {
    2174        3178 :   pari_sp av = avma;
    2175             :   long j;
    2176        3178 :   GEN s = gen_0, dk, pd;
    2177        3178 :   dk = divisorsu(n);
    2178       10535 :   for (j = 1; j < lg(dk); ++j)
    2179             :   {
    2180        7357 :     long d = dk[j];
    2181        7357 :     long m = moebiusu(d);
    2182        7357 :     if (!m) continue;
    2183        6797 :     pd = powiu(p, n/d);
    2184        6797 :     s = m>0 ? addii(s, pd): subii(s,pd);
    2185             :   }
    2186        3178 :   return gerepileuptoint(av, divis(s, n));
    2187             : }
    2188             : 
    2189             : GEN
    2190         434 : ffsumnbirred(GEN p, long n)
    2191             : {
    2192         434 :   pari_sp av = avma;
    2193             :   long i,j;
    2194         434 :   GEN v,q, t = gen_0;
    2195         434 :   v = cgetg(n+1,t_VECSMALL); v[1] = 1;
    2196         434 :   q = cgetg(n+1,t_VEC); gel(q,1) = p;
    2197        1547 :   for(i=2; i<=n; i++)
    2198             :   {
    2199        1113 :     v[i] = moebiusu(i);
    2200        1113 :     gel(q,i) = mulii(gel(q,i-1), p);
    2201             :   }
    2202        1981 :   for(i=1; i<=n; i++)
    2203             :   {
    2204        1547 :     GEN s = gen_0;
    2205        1547 :     GEN dk = divisorsu(i);
    2206        4725 :     for (j = 1; j < lg(dk); ++j)
    2207             :     {
    2208        3178 :       long d = dk[j], m = v[d];
    2209        3178 :       if (!m) continue;
    2210        2884 :       s = m>0 ? addii(s, gel(q, i/d)): subii(s, gel(q, i/d));
    2211             :     }
    2212        1547 :     t = addii(t, divis(s, i));
    2213             :   }
    2214         434 :   return gerepileuptoint(av, t);
    2215             : }
    2216             : 
    2217             : GEN
    2218         140 : ffnbirred0(GEN p, long n, long flag)
    2219             : {
    2220         140 :   if (typ(p) != t_INT) pari_err_TYPE("ffnbirred", p);
    2221         140 :   if (n <= 0) pari_err_DOMAIN("ffnbirred", "degree", "<=", gen_0, stoi(n));
    2222         140 :   switch(flag)
    2223             :   {
    2224             :     case 0:
    2225          70 :       return ffnbirred(p, n);
    2226             :     case 1:
    2227          70 :       return ffsumnbirred(p, n);
    2228             :     default:
    2229           0 :       pari_err_FLAG("ffnbirred");
    2230             :   }
    2231           0 :   return NULL; /* NOT REACHED */
    2232             : }

Generated by: LCOV version 1.11