Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - polarit2.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.18.1 lcov report (development 29884-9d10e5cf54) Lines: 2240 2477 90.4 %
Date: 2025-01-20 09:10:26 Functions: 211 221 95.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /***********************************************************************/
      16             : /**                                                                   **/
      17             : /**               ARITHMETIC OPERATIONS ON POLYNOMIALS                **/
      18             : /**                         (second part)                             **/
      19             : /**                                                                   **/
      20             : /***********************************************************************/
      21             : #include "pari.h"
      22             : #include "paripriv.h"
      23             : 
      24             : #define DEBUGLEVEL DEBUGLEVEL_pol
      25             : 
      26             : /* compute Newton sums S_1(P), ... , S_n(P). S_k(P) = sum a_j^k, a_j root of P
      27             :  * If N != NULL, assume p-adic roots and compute mod N [assume integer coeffs]
      28             :  * If T != NULL, compute mod (T,N) [assume integer coeffs if N != NULL]
      29             :  * If y0!= NULL, precomputed i-th powers, i=1..m, m = length(y0).
      30             :  * Not memory clean in the latter case */
      31             : GEN
      32      130721 : polsym_gen(GEN P, GEN y0, long n, GEN T, GEN N)
      33             : {
      34      130721 :   long dP=degpol(P), i, k, m;
      35             :   pari_sp av1, av2;
      36             :   GEN s,y,P_lead;
      37             : 
      38      130721 :   if (n<0) pari_err_IMPL("polsym of a negative n");
      39      130721 :   if (typ(P) != t_POL) pari_err_TYPE("polsym",P);
      40      130721 :   if (!signe(P)) pari_err_ROOTS0("polsym");
      41      130721 :   y = cgetg(n+2,t_COL);
      42      130721 :   if (y0)
      43             :   {
      44       13237 :     if (typ(y0) != t_COL) pari_err_TYPE("polsym_gen",y0);
      45       13237 :     m = lg(y0)-1;
      46       63987 :     for (i=1; i<=m; i++) gel(y,i) = gel(y0,i); /* not memory clean */
      47             :   }
      48             :   else
      49             :   {
      50      117484 :     m = 1;
      51      117484 :     gel(y,1) = stoi(dP);
      52             :   }
      53      130721 :   P += 2; /* strip codewords */
      54             : 
      55      130721 :   P_lead = gel(P,dP); if (gequal1(P_lead)) P_lead = NULL;
      56      130721 :   if (P_lead)
      57             :   {
      58           7 :     if (N) P_lead = Fq_inv(P_lead,T,N);
      59           7 :     else if (T) P_lead = QXQ_inv(P_lead,T);
      60             :   }
      61      388171 :   for (k=m; k<=n; k++)
      62             :   {
      63      257449 :     av1 = avma; s = (dP>=k)? gmulsg(k,gel(P,dP-k)): gen_0;
      64      744882 :     for (i=1; i<k && i<=dP; i++)
      65      487445 :       s = gadd(s, gmul(gel(y,k-i+1),gel(P,dP-i)));
      66      257437 :     if (N)
      67             :     {
      68       18760 :       s = Fq_red(s, T, N);
      69       18760 :       if (P_lead) s = Fq_mul(s, P_lead, T, N);
      70             :     }
      71      238677 :     else if (T)
      72             :     {
      73           0 :       s = grem(s, T);
      74           0 :       if (P_lead) s = grem(gmul(s, P_lead), T);
      75             :     }
      76             :     else
      77      238677 :       if (P_lead) s = gdiv(s, P_lead);
      78      257437 :     av2 = avma; gel(y,k+1) = gerepile(av1,av2, gneg(s));
      79             :   }
      80      130722 :   return y;
      81             : }
      82             : 
      83             : GEN
      84      113305 : polsym(GEN x, long n)
      85             : {
      86      113305 :   return polsym_gen(x, NULL, n, NULL,NULL);
      87             : }
      88             : 
      89             : /* centered residue x mod p. po2 = shifti(p, -1) or NULL (euclidean residue) */
      90             : GEN
      91    87843802 : centermodii(GEN x, GEN p, GEN po2)
      92             : {
      93    87843802 :   GEN y = remii(x, p);
      94    87888144 :   switch(signe(y))
      95             :   {
      96    10621762 :     case 0: break;
      97    54435305 :     case 1: if (po2 && abscmpii(y,po2) > 0) y = subii(y, p);
      98    54254527 :       break;
      99    23220109 :     case -1: if (!po2 || abscmpii(y,po2) > 0) y = addii(y, p);
     100    22984699 :       break;
     101             :   }
     102    87471956 :   return y;
     103             : }
     104             : 
     105             : static long
     106           0 : s_centermod(long x, ulong pp, ulong pps2)
     107             : {
     108           0 :   long y = x % (long)pp;
     109           0 :   if (y < 0) y += pp;
     110           0 :   return Fl_center(y, pp,pps2);
     111             : }
     112             : 
     113             : /* for internal use */
     114             : GEN
     115    14470375 : centermod_i(GEN x, GEN p, GEN ps2)
     116             : {
     117             :   long i, lx;
     118             :   pari_sp av;
     119             :   GEN y;
     120             : 
     121    14470375 :   if (!ps2) ps2 = shifti(p,-1);
     122    14470194 :   switch(typ(x))
     123             :   {
     124     1416863 :     case t_INT: return centermodii(x,p,ps2);
     125             : 
     126     6095069 :     case t_POL: lx = lg(x);
     127     6095069 :       y = cgetg(lx,t_POL); y[1] = x[1];
     128    42024337 :       for (i=2; i<lx; i++)
     129             :       {
     130    35928065 :         av = avma;
     131    35928065 :         gel(y,i) = gerepileuptoint(av, centermodii(gel(x,i),p,ps2));
     132             :       }
     133     6096272 :       return normalizepol_lg(y, lx);
     134             : 
     135    29631702 :     case t_COL: pari_APPLY_same(centermodii(gel(x,i),p,ps2));
     136       13531 :     case t_MAT: pari_APPLY_same(centermod_i(gel(x,i),p,ps2));
     137             : 
     138           0 :     case t_VECSMALL: lx = lg(x);
     139             :     {
     140           0 :       ulong pp = itou(p), pps2 = itou(ps2);
     141           0 :       pari_APPLY_long(s_centermod(x[i], pp, pps2));
     142             :     }
     143             :   }
     144           0 :   return x;
     145             : }
     146             : 
     147             : GEN
     148    10956833 : centermod(GEN x, GEN p) { return centermod_i(x,p,NULL); }
     149             : 
     150             : static GEN
     151         343 : RgX_Frobenius_deflate(GEN S, ulong p)
     152             : {
     153         343 :   if (degpol(S)%p)
     154           0 :     return NULL;
     155             :   else
     156             :   {
     157         343 :     GEN F = RgX_deflate(S, p);
     158         343 :     long i, l = lg(F);
     159        1043 :     for (i=2; i<l; i++)
     160             :     {
     161         721 :       GEN Fi = gel(F,i), R;
     162         721 :       if (typ(Fi)==t_POL)
     163             :       {
     164         259 :         if (signe(RgX_deriv(Fi))==0)
     165         238 :           gel(F,i) = RgX_Frobenius_deflate(gel(F, i), p);
     166          21 :         else return NULL;
     167             :       }
     168         462 :       else if (ispower(Fi, utoi(p), &R))
     169         462 :         gel(F,i) = R;
     170           0 :       else return NULL;
     171             :     }
     172         322 :     return F;
     173             :   }
     174             : }
     175             : 
     176             : static GEN
     177         245 : RgXY_squff(GEN f)
     178             : {
     179         245 :   long i, q, n = degpol(f);
     180         245 :   ulong p = itos_or_0(characteristic(f));
     181         245 :   GEN u = const_vec(n+1, pol_1(varn(f)));
     182         245 :   for(q = 1;;q *= p)
     183          84 :   {
     184         329 :     GEN t, v, tv, r = RgX_gcd(f, RgX_deriv(f));
     185         329 :     if (degpol(r) == 0) { gel(u, q) = f; break; }
     186         126 :     t = RgX_div(f, r);
     187         126 :     if (degpol(t) > 0)
     188             :     {
     189             :       long j;
     190          28 :       for(j = 1;;j++)
     191             :       {
     192         140 :         v = RgX_gcd(r, t);
     193         140 :         tv = RgX_div(t, v);
     194         140 :         if (degpol(tv) > 0) gel(u, j*q) = tv;
     195         140 :         if (degpol(v) <= 0) break;
     196         112 :         r = RgX_div(r, v);
     197         112 :         t = v;
     198             :       }
     199          28 :       if (degpol(r) == 0) break;
     200             :     }
     201         105 :     if (!p) break;
     202         105 :     f = RgX_Frobenius_deflate(r, p);
     203         105 :     if (!f) { gel(u, q) = r; break; }
     204             :   }
     205         931 :   for (i = n; i; i--)
     206         931 :     if (degpol(gel(u,i))) break;
     207         245 :   setlg(u,i+1); return u;
     208             : }
     209             : 
     210             : /* Lmod contains modular factors of *F (NULL codes an empty slot: used factor)
     211             :  * Lfac accumulates irreducible factors as they are found.
     212             :  * p is a product of modular factors in Lmod[1..i-1] (NULL for p = 1), not
     213             :  * a rational factor of *F
     214             :  * Find an irreducible factor of *F divisible by p (by including
     215             :  * exhaustively further factors from Lmod[i..]); return 0 on failure, else 1.
     216             :  * Update Lmod, Lfac and *F */
     217             : static int
     218        6475 : RgX_cmbf(GEN p, long i, GEN BLOC, GEN Lmod, GEN Lfac, GEN *F)
     219             : {
     220             :   pari_sp av;
     221             :   GEN q;
     222        6475 :   if (i == lg(Lmod)) return 0;
     223        3388 :   if (RgX_cmbf(p, i+1, BLOC, Lmod, Lfac, F) && p) return 1;
     224        3206 :   if (!gel(Lmod,i)) return 0;
     225        3157 :   p = p? RgX_mul(p, gel(Lmod,i)): gel(Lmod,i);
     226        3157 :   av = avma;
     227        3157 :   q = RgV_to_RgX(RgX_digits(p, BLOC), varn(*F));
     228        3157 :   if (degpol(q))
     229             :   {
     230        2779 :     GEN R, Q = RgX_divrem(*F, q, &R);
     231        2779 :     if (signe(R)==0) { vectrunc_append(Lfac, q); *F = Q; return 1; }
     232             :   }
     233        2828 :   set_avma(av);
     234        2828 :   if (RgX_cmbf(p, i+1, BLOC, Lmod, Lfac, F)) { gel(Lmod,i) = NULL; return 1; }
     235        2569 :   return 0;
     236             : }
     237             : 
     238             : static GEN factor_domain(GEN x, GEN flag);
     239             : 
     240             : static GEN
     241         434 : ok_bloc(GEN f, GEN BLOC, ulong c)
     242             : {
     243         434 :   GEN F = poleval(f, BLOC);
     244         434 :   return issquarefree(c ? gmul(F,mkintmodu(1,c)): F)? F: NULL;
     245             : }
     246             : static GEN
     247         112 : random_FpX_monic(long n, long v, GEN p)
     248             : {
     249         112 :   long i, d = n + 2;
     250         112 :   GEN y = cgetg(d + 1, t_POL); y[1] = evalsigne(1) | evalvarn(v);
     251         364 :   for (i = 2; i < d; i++) gel(y,i) = randomi(p);
     252         112 :   gel(y,i) = gen_1; return y;
     253             : }
     254             : static GEN
     255         273 : RgXY_factor_squarefree(GEN f, GEN dom)
     256             : {
     257         273 :   pari_sp av = avma;
     258         273 :   ulong i, c = itou_or_0(residual_characteristic(f));
     259         273 :   long vy = gvar2(f), val = RgX_valrem(f, &f), n = RgXY_degreex(f);
     260         273 :   GEN y, Lmod, F = NULL, BLOC = NULL, Lfac = coltrunc_init(degpol(f)+2);
     261         273 :   GEN gc = c? utoipos(c): NULL;
     262         273 :   if (val)
     263             :   {
     264          35 :     GEN x = pol_x(varn(f));
     265          35 :     if (dom)
     266             :     {
     267          14 :       GEN one = Rg_get_1(dom);
     268          14 :       if (typ(one) != t_INT) x = RgX_Rg_mul(x, one);
     269             :     }
     270          35 :     vectrunc_append(Lfac, x); if (!degpol(f)) return Lfac;
     271             :   }
     272         259 :   y = pol_x(vy);
     273             :   for(;;)
     274             :   {
     275         322 :     for (i = 0; !c || i < c; i++)
     276             :     {
     277         322 :       BLOC = gpowgs(gaddgs(y, i), n+1);
     278         322 :       if ((F = ok_bloc(f, BLOC, c))) break;
     279         147 :       if (c)
     280             :       {
     281         112 :         BLOC = random_FpX_monic(n, vy, gc);
     282         112 :         if ((F = ok_bloc(f, BLOC, c))) break;
     283             :       }
     284             :     }
     285         259 :     if (!c || i < c) break;
     286           0 :     n++;
     287             :   }
     288         259 :   if (DEBUGLEVEL >= 2)
     289           0 :     err_printf("bifactor: bloc:(x+%ld)^%ld, deg f=%ld\n",i,n,RgXY_degreex(f));
     290         259 :   Lmod = gel(factor_domain(F,dom),1);
     291         259 :   if (DEBUGLEVEL >= 2)
     292           0 :     err_printf("bifactor: %ld local factors\n",lg(Lmod)-1);
     293         259 :   (void)RgX_cmbf(NULL, 1, BLOC, Lmod, Lfac, &f);
     294         259 :   if (degpol(f)) vectrunc_append(Lfac, f);
     295         259 :   return gerepilecopy(av, Lfac);
     296             : }
     297             : 
     298             : static GEN
     299         245 : FE_matconcat(GEN F, GEN E, long l)
     300             : {
     301         245 :   setlg(E,l); E = shallowconcat1(E);
     302         245 :   setlg(F,l); F = shallowconcat1(F); return mkmat2(F,E);
     303             : }
     304             : 
     305             : static int
     306         385 : gen_cmp_RgXY(void *data, GEN x, GEN y)
     307             : {
     308         385 :   long vx = varn(x), vy = varn(y);
     309         385 :   return (vx == vy)? gen_cmp_RgX(data, x, y): -varncmp(vx, vy);
     310             : }
     311             : static GEN
     312         245 : RgXY_factor(GEN f, GEN dom)
     313             : {
     314         245 :   pari_sp av = avma;
     315             :   GEN C, F, E, cf, V;
     316             :   long i, j, l;
     317         245 :   if (dom) { GEN c = Rg_get_1(dom); if (typ(c) != t_INT) f = RgX_Rg_mul(f,c); }
     318         245 :   cf = content(f);
     319         245 :   V = RgXY_squff(gdiv(f, cf)); l = lg(V);
     320         245 :   C = factor_domain(cf, dom);
     321         245 :   F = cgetg(l+1, t_VEC); gel(F,1) = gel(C,1);
     322         245 :   E = cgetg(l+1, t_VEC); gel(E,1) = gel(C,2);
     323         756 :   for (i=1, j=2; i < l; i++)
     324             :   {
     325         511 :     GEN v = gel(V,i);
     326         511 :     if (degpol(v))
     327             :     {
     328         273 :       gel(F,j) = v = RgXY_factor_squarefree(v, dom);
     329         273 :       gel(E,j) = const_col(lg(v)-1, utoipos(i));
     330         273 :       j++;
     331             :     }
     332             :   }
     333         245 :   f = FE_matconcat(F,E,j);
     334         245 :   (void)sort_factor(f,(void*)cmp_universal, &gen_cmp_RgXY);
     335         245 :   return gerepilecopy(av, f);
     336             : }
     337             : 
     338             : /***********************************************************************/
     339             : /**                                                                   **/
     340             : /**                          FACTORIZATION                            **/
     341             : /**                                                                   **/
     342             : /***********************************************************************/
     343             : static long RgX_settype(GEN x, long *t, GEN *p, GEN *pol, long *pa, GEN *ff, long *t2, long *var);
     344             : #define assign_or_fail(x,y) { GEN __x = x;\
     345             :   if (!*y) *y=__x; else if (!gequal(__x,*y)) return 0;\
     346             : }
     347             : #define update_prec(x,y) { long __x = x; if (__x < *y) *y=__x; }
     348             : 
     349             : static const long tsh = 6;
     350             : #define code(t1,t2) ((t1 << 6) | t2)
     351             : void
     352    11687198 : RgX_type_decode(long x, long *t1, long *t2)
     353             : {
     354    11687198 :   *t1 = x >> tsh;
     355    11687198 :   *t2 = (x & ((1L<<tsh)-1));
     356    11687198 : }
     357             : int
     358   163302596 : RgX_type_is_composite(long t) { return t >= tsh; }
     359             : 
     360             : static int
     361  3150351762 : settype(GEN c, long *t, GEN *p, GEN *pol, long *pa, GEN *ff, long *t2, long *var)
     362             : {
     363             :   long j;
     364  3150351762 :   switch(typ(c))
     365             :   {
     366  2411404974 :     case t_INT:
     367  2411404974 :       break;
     368    32421574 :     case t_FRAC:
     369    32421574 :       t[1]=1; break;
     370             :       break;
     371   292742928 :     case t_REAL:
     372   292742928 :       update_prec(precision(c), pa);
     373   292742492 :       t[2]=1; break;
     374    30653208 :     case t_INTMOD:
     375    30653208 :       assign_or_fail(gel(c,1),p);
     376    30653208 :       t[3]=1; break;
     377     1915493 :     case t_FFELT:
     378     1915493 :       if (!*ff) *ff=c; else if (!FF_samefield(c,*ff)) return 0;
     379     1915493 :       assign_or_fail(FF_p_i(c),p);
     380     1915493 :       t[5]=1; break;
     381   324190096 :     case t_COMPLEX:
     382   972568236 :       for (j=1; j<=2; j++)
     383             :       {
     384   648378514 :         GEN d = gel(c,j);
     385   648378514 :         switch(typ(d))
     386             :         {
     387     2343621 :           case t_INT: case t_FRAC:
     388     2343621 :             if (!*t2) *t2 = t_COMPLEX;
     389     2343621 :             t[1]=1; break;
     390   646034858 :           case t_REAL:
     391   646034858 :             update_prec(precision(d), pa);
     392   646034491 :             if (!*t2) *t2 = t_COMPLEX;
     393   646034491 :             t[2]=1; break;
     394          14 :           case t_INTMOD:
     395          14 :             assign_or_fail(gel(d,1),p);
     396          14 :             if (!signe(*p) || mod4(*p) != 3) return 0;
     397           7 :             if (!*t2) *t2 = t_COMPLEX;
     398           7 :             t[3]=1; break;
     399          21 :           case t_PADIC:
     400          21 :             update_prec(precp(d)+valp(d), pa);
     401          21 :             assign_or_fail(gel(d,2),p);
     402          21 :             if (!*t2) *t2 = t_COMPLEX;
     403          21 :             t[7]=1; break;
     404           0 :           default: return 0;
     405             :         }
     406             :       }
     407   324189722 :       if (!t[2]) assign_or_fail(mkpoln(3, gen_1,gen_0,gen_1), pol); /*x^2+1*/
     408   324189722 :       break;
     409     2333005 :     case t_PADIC:
     410     2333005 :       update_prec(precp(c)+valp(c), pa);
     411     2333005 :       assign_or_fail(gel(c,2),p);
     412     2333005 :       t[7]=1; break;
     413        1960 :     case t_QUAD:
     414        1960 :       assign_or_fail(gel(c,1),pol);
     415        5880 :       for (j=2; j<=3; j++)
     416             :       {
     417        3920 :         GEN d = gel(c,j);
     418        3920 :         switch(typ(d))
     419             :         {
     420        3885 :           case t_INT: case t_FRAC:
     421        3885 :             t[8]=1; break;
     422          28 :           case t_INTMOD:
     423          28 :             assign_or_fail(gel(d,1),p);
     424          28 :             if (*t2 != t_POLMOD) *t2 = t_QUAD;
     425          28 :             t[3]=1; break;
     426           7 :           case t_PADIC:
     427           7 :             update_prec(precp(d)+valp(d), pa);
     428           7 :             assign_or_fail(gel(d,2),p);
     429           7 :             if (*t2 != t_POLMOD) *t2 = t_QUAD;
     430           7 :             t[7]=1; break;
     431           0 :           default: return 0;
     432             :         }
     433             :       }
     434        1960 :       break;
     435     4027579 :     case t_POLMOD:
     436     4027579 :       assign_or_fail(gel(c,1),pol);
     437     4027376 :       if (typ(gel(c,2))==t_POL && varn(gel(c,2))!=varn(gel(c,1))) return 0;
     438    12075345 :       for (j=1; j<=2; j++)
     439             :       {
     440             :         GEN pbis, polbis;
     441             :         long pabis;
     442     8052055 :         *t2 = t_POLMOD;
     443     8052055 :         switch(Rg_type(gel(c,j),&pbis,&polbis,&pabis))
     444             :         {
     445     4507257 :           case t_INT: break;
     446      957551 :           case t_FRAC:   t[1]=1; break;
     447     2583161 :           case t_INTMOD: t[3]=1; break;
     448           7 :           case t_PADIC:  t[7]=1; update_prec(pabis,pa); break;
     449        4081 :           default: return 0;
     450             :         }
     451     8047976 :         if (pbis) assign_or_fail(pbis,p);
     452     8047976 :         if (polbis) assign_or_fail(polbis,pol);
     453             :       }
     454     4023290 :       break;
     455     6772171 :     case t_RFRAC: t[10] = 1;
     456     6772171 :       if (!settype(gel(c,1),t,p,pol,pa,ff,t2,var)) return 0;
     457     6772171 :       c = gel(c,2); /* fall through */
     458    50658929 :     case t_POL: t[10] = 1;
     459    50658929 :       if (!RgX_settype(c,t,p,pol,pa,ff,t2,var)) return 0;
     460    50680426 :       if (*var == NO_VARIABLE) { *var = varn(c); break; }
     461             :       /* if more than one free var, ensure varn() == *var fails. FIXME: should
     462             :        * keep the list of all variables, later t_POLMOD may cancel them */
     463    30477902 :       if (*var != varn(c)) *var = MAXVARN+1;
     464    30477902 :       break;
     465        2016 :     default: return 0;
     466             :   }
     467  3150366144 :   return 1;
     468             : }
     469             : /* t[0] unused. Other values, if set, indicate a coefficient of type
     470             :  * t[1] : t_FRAC
     471             :  * t[2] : t_REAL
     472             :  * t[3] : t_INTMOD
     473             :  * t[4] : Unused
     474             :  * t[5] : t_FFELT
     475             :  * t[6] : Unused
     476             :  * t[7] : t_PADIC
     477             :  * t[8] : t_QUAD of rationals (t_INT/t_FRAC)
     478             :  * t[9]:  Unused
     479             :  * t[10]: t_POL (recursive factorisation) */
     480             : /* if t2 != 0: t_POLMOD/t_QUAD/t_COMPLEX of modular (t_INTMOD/t_PADIC,
     481             :  * given by t) */
     482             : static long
     483   322639541 : choosetype(long *t, long t2, GEN ff, GEN *pol, long var)
     484             : {
     485   322639541 :   if (t[10] && (!*pol || var!=varn(*pol))) return t_POL;
     486   302452138 :   if (t2) /* polmod/quad/complex of intmod/padic */
     487             :   {
     488    22062527 :     if (t[2] && (t[3]||t[7])) return 0;
     489    22062527 :     if (t[3]) return code(t2,t_INTMOD);
     490    22032693 :     if (t[7]) return code(t2,t_PADIC);
     491    22032644 :     if (t[2]) return t_COMPLEX;
     492      615067 :     if (t[1]) return code(t2,t_FRAC);
     493      232676 :     return code(t2,t_INT);
     494             :   }
     495   280389611 :   if (t[5]) /* ffelt */
     496             :   {
     497      225905 :     if (t[2]||t[8]||t[9]) return 0;
     498      225905 :     *pol=ff; return t_FFELT;
     499             :   }
     500   280163706 :   if (t[2]) /* inexact, real */
     501             :   {
     502    43569256 :     if (t[3]||t[7]||t[9]) return 0;
     503    43569267 :     return t_REAL;
     504             :   }
     505   236594450 :   if (t[10]) return t_POL;
     506   236594450 :   if (t[8]) return code(t_QUAD,t_INT);
     507   236593617 :   if (t[3]) return t_INTMOD;
     508   232384476 :   if (t[7]) return t_PADIC;
     509   232006568 :   if (t[1]) return t_FRAC;
     510   224501038 :   return t_INT;
     511             : }
     512             : 
     513             : static long
     514   386222362 : RgX_settype(GEN x, long *t, GEN *p, GEN *pol, long *pa, GEN *ff, long *t2, long *var)
     515             : {
     516   386222362 :   long i, lx = lg(x);
     517  1396587551 :   for (i=2; i<lx; i++)
     518  1010367506 :     if (!settype(gel(x,i),t,p,pol,pa,ff,t2,var)) return 0;
     519   386220045 :   return 1;
     520             : }
     521             : 
     522             : static long
     523   269008314 : RgC_settype(GEN x, long *t, GEN *p, GEN *pol, long *pa, GEN *ff, long *t2, long *var)
     524             : {
     525   269008314 :   long i, l = lg(x);
     526  2347087142 :   for (i = 1; i<l; i++)
     527  2078081141 :     if (!settype(gel(x,i),t,p,pol,pa,ff,t2,var)) return 0;
     528   269006001 :   return 1;
     529             : }
     530             : 
     531             : static long
     532    48254177 : RgM_settype(GEN x, long *t, GEN *p, GEN *pol, long *pa, GEN *ff, long *t2, long *var)
     533             : {
     534    48254177 :   long i, l = lg(x);
     535   284134797 :   for (i = 1; i < l; i++)
     536   235882966 :     if (!RgC_settype(gel(x,i),t,p,pol,pa,ff,t2,var)) return 0;
     537    48251831 :   return 1;
     538             : }
     539             : 
     540             : long
     541   171354671 : Rg_type(GEN x, GEN *p, GEN *pol, long *pa)
     542             : {
     543   171354671 :   long t[] = {0,0,0,0,0,0,0,0,0,0,0};
     544   171354671 :   long t2 = 0, var = NO_VARIABLE;
     545   171354671 :   GEN ff = NULL;
     546   171354671 :   *p = *pol = NULL; *pa = LONG_MAX;
     547   171354671 :   switch(typ(x))
     548             :   {
     549    55138255 :     case t_INT: case t_REAL: case t_INTMOD: case t_FRAC: case t_FFELT:
     550             :     case t_COMPLEX: case t_PADIC: case t_QUAD:
     551    55138255 :       if (!settype(x,t,p,pol,pa,&ff,&t2,&var)) return 0;
     552    55138255 :       break;
     553   115675233 :     case t_POL: case t_SER:
     554   115675233 :       if (!RgX_settype(x,t,p,pol,pa,&ff,&t2,&var)) return 0;
     555   115674955 :       break;
     556          21 :     case t_VEC: case t_COL:
     557          21 :       if(!RgC_settype(x, t, p, pol, pa, &ff, &t2, &var)) return 0;
     558          21 :       break;
     559         126 :     case t_MAT:
     560         126 :       if(!RgM_settype(x, t, p, pol, pa, &ff, &t2, &var)) return 0;
     561         126 :       break;
     562      541036 :     default: return 0;
     563             :   }
     564   170813357 :   return choosetype(t,t2,ff,pol,var);
     565             : }
     566             : 
     567             : long
     568     2505002 : RgX_type(GEN x, GEN *p, GEN *pol, long *pa)
     569             : {
     570     2505002 :   long t[] = {0,0,0,0,0,0,0,0,0,0,0,0};
     571     2505002 :   long t2 = 0, var = NO_VARIABLE;
     572     2505002 :   GEN ff = NULL;
     573     2505002 :   *p = *pol = NULL; *pa = LONG_MAX;
     574     2505002 :   if (!RgX_settype(x,t,p,pol,pa,&ff,&t2,&var)) return 0;
     575     2504957 :   return choosetype(t,t2,ff,pol,var);
     576             : }
     577             : 
     578             : long
     579         294 : RgX_Rg_type(GEN x, GEN y, GEN *p, GEN *pol, long *pa)
     580             : {
     581         294 :   long t[] = {0,0,0,0,0,0,0,0,0,0,0,0};
     582         294 :   long t2 = 0, var = NO_VARIABLE;
     583         294 :   GEN ff = NULL;
     584         294 :   *p = *pol = NULL; *pa = LONG_MAX;
     585         294 :   if (!RgX_settype(x,t,p,pol,pa,&ff,&t2,&var)) return 0;
     586         294 :   if (!settype(y,t,p,pol,pa,&ff,&t2,&var)) return 0;
     587         294 :   return choosetype(t,t2,ff,pol,var);
     588             : }
     589             : 
     590             : long
     591   106393315 : RgX_type2(GEN x, GEN y, GEN *p, GEN *pol, long *pa)
     592             : {
     593   106393315 :   long t[] = {0,0,0,0,0,0,0,0,0,0,0,0};
     594   106393315 :   long t2 = 0, var = NO_VARIABLE;
     595   106393315 :   GEN ff = NULL;
     596   106393315 :   *p = *pol = NULL; *pa = LONG_MAX;
     597   212784282 :   if (!RgX_settype(x,t,p,pol,pa,&ff,&t2,&var) ||
     598   106394113 :       !RgX_settype(y,t,p,pol,pa,&ff,&t2,&var)) return 0;
     599   106390849 :   return choosetype(t,t2,ff,pol,var);
     600             : }
     601             : 
     602             : long
     603     1525873 : RgX_type3(GEN x, GEN y, GEN z, GEN *p, GEN *pol, long *pa)
     604             : {
     605     1525873 :   long t[] = {0,0,0,0,0,0,0,0,0,0,0,0};
     606     1525873 :   long t2 = 0, var = NO_VARIABLE;
     607     1525873 :   GEN ff = NULL;
     608     1525873 :   *p = *pol = NULL; *pa = LONG_MAX;
     609     3051750 :   if (!RgX_settype(x,t,p,pol,pa,&ff,&t2,&var) ||
     610     3051751 :       !RgX_settype(y,t,p,pol,pa,&ff,&t2,&var) ||
     611     1525877 :       !RgX_settype(z,t,p,pol,pa,&ff,&t2,&var)) return 0;
     612     1525876 :   return choosetype(t,t2,ff,pol,var);
     613             : }
     614             : 
     615             : long
     616      659820 : RgM_type(GEN x, GEN *p, GEN *pol, long *pa)
     617             : {
     618      659820 :   long t[] = {0,0,0,0,0,0,0,0,0,0,0,0};
     619      659820 :   long t2 = 0, var = NO_VARIABLE;
     620      659820 :   GEN ff = NULL;
     621      659820 :   *p = *pol = NULL; *pa = LONG_MAX;
     622      659820 :   if (!RgM_settype(x,t,p,pol,pa,&ff,&t2,&var)) return 0;
     623      658767 :   return choosetype(t,t2,ff,pol,var);
     624             : }
     625             : 
     626             : long
     627      774301 : RgV_type(GEN x, GEN *p, GEN *pol, long *pa)
     628             : {
     629      774301 :   long t[] = {0,0,0,0,0,0,0,0,0,0,0,0};
     630      774301 :   long t2 = 0, var = NO_VARIABLE;
     631      774301 :   GEN ff = NULL;
     632      774301 :   *p = *pol = NULL; *pa = LONG_MAX;
     633      774301 :   if (!RgC_settype(x,t,p,pol,pa,&ff,&t2,&var)) return 0;
     634      774301 :   return choosetype(t,t2,ff,pol,var);
     635             : }
     636             : 
     637             : long
     638         203 : RgV_type2(GEN x, GEN y, GEN *p, GEN *pol, long *pa)
     639             : {
     640         203 :   long t[] = {0,0,0,0,0,0,0,0,0,0,0,0};
     641         203 :   long t2 = 0, var = NO_VARIABLE;
     642         203 :   GEN ff = NULL;
     643         203 :   *p = *pol = NULL; *pa = LONG_MAX;
     644         406 :   if (!RgC_settype(x,t,p,pol,pa,&ff,&t2,&var) ||
     645         203 :       !RgC_settype(y,t,p,pol,pa,&ff,&t2,&var)) return 0;
     646         203 :   return choosetype(t,t2,ff,pol,var);
     647             : }
     648             : 
     649             : long
     650    32351983 : RgM_RgC_type(GEN x, GEN y, GEN *p, GEN *pol, long *pa)
     651             : {
     652    32351983 :   long t[] = {0,0,0,0,0,0,0,0,0,0,0,0};
     653    32351983 :   long t2 = 0, var = NO_VARIABLE;
     654    32351983 :   GEN ff = NULL;
     655    32351983 :   *p = *pol = NULL; *pa = LONG_MAX;
     656    64703699 :   if (!RgM_settype(x,t,p,pol,pa,&ff,&t2,&var) ||
     657    32352933 :       !RgC_settype(y,t,p,pol,pa,&ff,&t2,&var)) return 0;
     658    32350916 :   return choosetype(t,t2,ff,pol,var);
     659             : }
     660             : 
     661             : long
     662     7621325 : RgM_type2(GEN x, GEN y, GEN *p, GEN *pol, long *pa)
     663             : {
     664     7621325 :   long t[] = {0,0,0,0,0,0,0,0,0,0,0,0};
     665     7621325 :   long t2 = 0, var = NO_VARIABLE;
     666     7621325 :   GEN ff = NULL;
     667     7621325 :   *p = *pol = NULL; *pa = LONG_MAX;
     668    15242227 :   if (!RgM_settype(x,t,p,pol,pa,&ff,&t2,&var) ||
     669     7621492 :       !RgM_settype(y,t,p,pol,pa,&ff,&t2,&var)) return 0;
     670     7620797 :   return choosetype(t,t2,ff,pol,var);
     671             : }
     672             : 
     673             : GEN
     674       59375 : factor0(GEN x, GEN flag)
     675             : {
     676             :   ulong B;
     677       59375 :   long tx = typ(x);
     678       59375 :   if (!flag) return factor(x);
     679         259 :   if ((tx != t_INT && tx!=t_FRAC) || typ(flag) != t_INT)
     680         175 :     return factor_domain(x, flag);
     681          84 :   if (signe(flag) < 0) pari_err_FLAG("factor");
     682          84 :   switch(lgefint(flag))
     683             :   {
     684          14 :     case 2: B = 0; break;
     685          70 :     case 3: B = flag[2]; break;
     686           0 :     default: pari_err_OVERFLOW("factor [large prime bound]");
     687             :              return NULL; /*LCOV_EXCL_LINE*/
     688             :   }
     689          84 :   return boundfact(x, B);
     690             : }
     691             : 
     692             : GEN
     693      156911 : deg1_from_roots(GEN L, long v)
     694             : {
     695      156911 :   long i, l = lg(L);
     696      156911 :   GEN z = cgetg(l,t_COL);
     697      462664 :   for (i=1; i<l; i++)
     698      305753 :     gel(z,i) = deg1pol_shallow(gen_1, gneg(gel(L,i)), v);
     699      156911 :   return z;
     700             : }
     701             : GEN
     702       63896 : roots_from_deg1(GEN x)
     703             : {
     704       63896 :   long i,l = lg(x);
     705       63896 :   GEN r = cgetg(l,t_VEC);
     706      392965 :   for (i=1; i<l; i++) { GEN P = gel(x,i); gel(r,i) = gneg(gel(P,2)); }
     707       63891 :   return r;
     708             : }
     709             : 
     710             : static GEN
     711          42 : Qi_factor_p(GEN p)
     712             : {
     713          42 :   GEN a, b; (void)cornacchia(gen_1, p, &a,&b);
     714          42 :   return mkcomplex(a, b);
     715             : }
     716             : 
     717             : static GEN
     718          49 : Qi_primpart(GEN x, GEN *c)
     719             : {
     720          49 :   GEN a = real_i(x), b = imag_i(x), n = gcdii(a, b);
     721          49 :   *c = n; if (n == gen_1) return x;
     722          49 :   retmkcomplex(diviiexact(a,n), diviiexact(b,n));
     723             : }
     724             : 
     725             : static GEN
     726          70 : Qi_primpart_try(GEN x, GEN c)
     727             : {
     728             :   GEN r, y;
     729          70 :   if (typ(x) == t_INT)
     730             :   {
     731          42 :     y = dvmdii(x, c, &r); if (r != gen_0) return NULL;
     732             :   }
     733             :   else
     734             :   {
     735          28 :     GEN a = gel(x,1), b = gel(x,2); y = cgetg(3, t_COMPLEX);
     736          28 :     gel(y,1) = dvmdii(a, c, &r); if (r != gen_0) return NULL;
     737          14 :     gel(y,2) = dvmdii(b, c, &r); if (r != gen_0) return NULL;
     738             :   }
     739          56 :   return y;
     740             : }
     741             : 
     742             : static int
     743          91 : Qi_cmp(GEN x, GEN y)
     744             : {
     745             :   int v;
     746          91 :   if (typ(x) != t_COMPLEX)
     747           0 :     return (typ(y) == t_COMPLEX)? -1: gcmp(x, y);
     748          91 :   if (typ(y) != t_COMPLEX) return 1;
     749          63 :   v = cmpii(gel(x,2), gel(y,2));
     750          63 :   if (v) return v;
     751          28 :   return gcmp(gel(x,1), gel(y,1));
     752             : }
     753             : 
     754             : /* 0 or canonical representative in Z[i]^* / <i> (impose imag(x) >= 0) */
     755             : static GEN
     756         469 : Qi_normal(GEN x)
     757             : {
     758         469 :   if (typ(x) != t_COMPLEX) return absi_shallow(x);
     759         469 :   if (signe(gel(x,1)) < 0) x = gneg(x);
     760         469 :   if (signe(gel(x,2)) < 0) x = mulcxI(x);
     761         469 :   return x;
     762             : }
     763             : 
     764             : static GEN
     765          49 : Qi_factor(GEN x)
     766             : {
     767          49 :   pari_sp av = avma;
     768          49 :   GEN a = real_i(x), b = imag_i(x), d = gen_1, n, y, fa, P, E, P2, E2;
     769          49 :   long t1 = typ(a);
     770          49 :   long t2 = typ(b), i, j, l, exp = 0;
     771          49 :   if (t1 == t_FRAC) d = gel(a,2);
     772          49 :   if (t2 == t_FRAC) d = lcmii(d, gel(b,2));
     773          49 :   if (d == gen_1) y = x;
     774             :   else
     775             :   {
     776          21 :     y = gmul(x, d);
     777          21 :     a = real_i(y); t1 = typ(a);
     778          21 :     b = imag_i(y); t2 = typ(b);
     779             :   }
     780          49 :   if (t1 != t_INT || t2 != t_INT) return NULL;
     781          49 :   y = Qi_primpart(y, &n);
     782          49 :   fa = factor(cxnorm(y));
     783          49 :   P = gel(fa,1);
     784          49 :   E = gel(fa,2); l = lg(P);
     785          49 :   P2 = cgetg(l, t_COL);
     786          49 :   E2 = cgetg(l, t_COL);
     787         105 :   for (j = 1, i = l-1; i > 0; i--) /* remove largest factors first */
     788             :   { /* either p = 2 (ramified) or those factors split in Q(i) */
     789          56 :     GEN p = gel(P,i), w, w2, t, we, pe;
     790          56 :     long v, e = itos(gel(E,i));
     791          56 :     int is2 = absequaliu(p, 2);
     792          56 :     w = is2? mkcomplex(gen_1,gen_1): Qi_factor_p(p);
     793          56 :     w2 = Qi_normal( conj_i(w) );
     794             :     /* w * w2 * I^3 = p, w2 = conj(w) * I */
     795          56 :     pe = powiu(p, e);
     796          56 :     we = gpowgs(w, e);
     797          56 :     t = Qi_primpart_try( gmul(y, conj_i(we)), pe );
     798          56 :     if (t) y = t; /* y /= w^e */
     799             :     else {
     800             :       /* y /= conj(w)^e, should be y /= w2^e */
     801          14 :       y = Qi_primpart_try( gmul(y, we), pe );
     802          14 :       swap(w, w2); exp -= e; /* += 3*e mod 4 */
     803             :     }
     804          56 :     gel(P,i) = w;
     805          56 :     v = Z_pvalrem(n, p, &n);
     806          56 :     if (v) {
     807           7 :       exp -= v; /* += 3*v mod 4 */
     808           7 :       if (is2) v <<= 1; /* 2 = w^2 I^3 */
     809             :       else {
     810           0 :         gel(P2,j) = w2;
     811           0 :         gel(E2,j) = utoipos(v); j++;
     812             :       }
     813           7 :       gel(E,i) = stoi(e + v);
     814             :     }
     815          56 :     v = Z_pvalrem(d, p, &d);
     816          56 :     if (v) {
     817           7 :       exp += v; /* -= 3*v mod 4 */
     818           7 :       if (is2) v <<= 1; /* 2 is ramified */
     819             :       else {
     820           7 :         gel(P2,j) = w2;
     821           7 :         gel(E2,j) = utoineg(v); j++;
     822             :       }
     823           7 :       gel(E,i) = stoi(e - v);
     824             :     }
     825          56 :     exp &= 3;
     826             :   }
     827          49 :   if (j > 1) {
     828           7 :     long k = 1;
     829           7 :     GEN P1 = cgetg(l, t_COL);
     830           7 :     GEN E1 = cgetg(l, t_COL);
     831             :     /* remove factors with exponent 0 */
     832          14 :     for (i = 1; i < l; i++)
     833           7 :       if (signe(gel(E,i)))
     834             :       {
     835           0 :         gel(P1,k) = gel(P,i);
     836           0 :         gel(E1,k) = gel(E,i);
     837           0 :         k++;
     838             :       }
     839           7 :     setlg(P1, k); setlg(E1, k);
     840           7 :     setlg(P2, j); setlg(E2, j);
     841           7 :     fa = famat_mul_shallow(mkmat2(P1,E1), mkmat2(P2,E2));
     842             :   }
     843          49 :   if (!equali1(n) || !equali1(d))
     844             :   {
     845          28 :     GEN Fa = factor(Qdivii(n, d));
     846          28 :     P = gel(Fa,1); l = lg(P);
     847          28 :     E = gel(Fa,2);
     848          70 :     for (i = 1; i < l; i++)
     849             :     {
     850          42 :       GEN w, p = gel(P,i);
     851             :       long e;
     852             :       int is2;
     853          42 :       switch(mod4(p))
     854             :       {
     855          14 :         case 3: continue;
     856          14 :         case 2: is2 = 1; break;
     857          14 :         default:is2 = 0; break;
     858             :       }
     859          28 :       e = itos(gel(E,i));
     860          28 :       w = is2? mkcomplex(gen_1,gen_1): Qi_factor_p(p);
     861          28 :       gel(P,i) = w;
     862          28 :       if (is2)
     863          14 :         gel(E,i) = stoi(2*e);
     864             :       else
     865             :       {
     866          14 :         P = vec_append(P, Qi_normal( conj_i(w) ));
     867          14 :         E = vec_append(E, gel(E,i));
     868             :       }
     869          28 :       exp -= e; /* += 3*e mod 4 */
     870          28 :       exp &= 3;
     871             :     }
     872          28 :     gel(Fa,1) = P;
     873          28 :     gel(Fa,2) = E;
     874          28 :     fa = famat_mul_shallow(fa, Fa);
     875             :   }
     876          49 :   fa = sort_factor(fa, (void*)&Qi_cmp, &cmp_nodata);
     877             : 
     878          49 :   y = gmul(y, powIs(exp));
     879          49 :   if (!gequal1(y)) {
     880          35 :     gel(fa,1) = vec_prepend(gel(fa,1), y);
     881          35 :     gel(fa,2) = vec_prepend(gel(fa,2), gen_1);
     882             :   }
     883          49 :   return gerepilecopy(av, fa);
     884             : }
     885             : 
     886             : GEN
     887        9768 : Q_factor_limit(GEN x, ulong lim)
     888             : {
     889        9768 :   pari_sp av = avma;
     890             :   GEN a, b;
     891        9768 :   if (typ(x) == t_INT) return Z_factor_limit(x, lim);
     892        5232 :   a = Z_factor_limit(gel(x,1), lim);
     893        5232 :   b = Z_factor_limit(gel(x,2), lim); gel(b,2) = ZC_neg(gel(b,2));
     894        5232 :   return gerepilecopy(av, ZM_merge_factor(a,b));
     895             : }
     896             : GEN
     897       21421 : Q_factor(GEN x)
     898             : {
     899       21421 :   pari_sp av = avma;
     900             :   GEN a, b;
     901       21421 :   if (typ(x) == t_INT) return Z_factor(x);
     902          34 :   a = Z_factor(gel(x,1));
     903          35 :   b = Z_factor(gel(x,2)); gel(b,2) = ZC_neg(gel(b,2));
     904          35 :   return gerepilecopy(av, ZM_merge_factor(a,b));
     905             : }
     906             : 
     907             : /* replace quadratic number over Fp or Q by t_POL in v */
     908             : static GEN
     909         420 : quadratic_to_RgX(GEN z, long v)
     910             : {
     911             :   GEN a, b;
     912         420 :   switch(typ(z))
     913             :   {
     914         343 :     case t_INT: case t_FRAC: case t_INTMOD: return z;
     915          35 :     case t_COMPLEX: a = gel(z,2); b = gel(z,1); break;
     916          42 :     case t_QUAD: a = gel(z,3); b = gel(z,2); break;
     917           0 :     default: pari_err_IMPL("factor for general polynomials"); /* paranoia */
     918             :              return NULL; /* LCOV_EXCL_LINE */
     919             :   }
     920          77 :   return deg1pol_shallow(a, b, v);
     921             : }
     922             : /* replace t_QUAD/t_COMPLEX [of rationals] coeffs by t_POL in v */
     923             : static GEN
     924          98 : RgX_fix_quadratic(GEN x, long v)
     925         518 : { pari_APPLY_pol_normalized(quadratic_to_RgX(gel(x,i), v)); }
     926             : static GEN
     927         252 : RgXQ_factor_i(GEN x, GEN T, GEN p, long t1, long t2, long *pv)
     928             : {
     929         252 :   *pv = -1;
     930         252 :   if (t2 == t_PADIC) return NULL;
     931         217 :   if (t2 == t_INTMOD)
     932             :   {
     933          56 :     T = RgX_to_FpX(T,p);
     934          56 :     if (!FpX_is_irred(T,p)) return NULL;
     935             :   }
     936         196 :   if (t1 != t_POLMOD)
     937             :   { /* replace w in x by t_POL */
     938          98 :     if (t2 != t_INTMOD) T = leafcopy(T);
     939          98 :     *pv = fetch_var(); setvarn(T, *pv);
     940          98 :     x = RgX_fix_quadratic(x, *pv);
     941             :   }
     942         196 :   if (t2 == t_INTMOD) return factmod(x, mkvec2(p,T));
     943         161 :   return nffactor(T, x);
     944             : }
     945             : static GEN
     946         252 : RgXQ_factor(GEN x, GEN T, GEN p, long tx)
     947             : {
     948         252 :   pari_sp av = avma;
     949             :   long t1, t2, v;
     950             :   GEN w, y;
     951         252 :   RgX_type_decode(tx, &t1, &t2);
     952         252 :   y = RgXQ_factor_i(x, T, p, t1, t2, &v);
     953         252 :   if (!y) pari_err_IMPL("factor for general polynomials");
     954         196 :   if (v < 0) return gerepileupto(av, y);
     955             :   /* substitute back w */
     956          98 :   w = (t1 == t_COMPLEX)? gen_I(): mkquad(T,gen_0,gen_1);
     957          98 :   gel(y,1) = gsubst(liftpol_shallow(gel(y,1)), v, w);
     958          98 :   (void)delete_var(); return gerepilecopy(av, y);
     959             : }
     960             : 
     961             : static GEN
     962          28 : RX_factor(GEN x, long prec)
     963             : {
     964          28 :   GEN y = cgetg(3,t_MAT), R, P;
     965          28 :   pari_sp av = avma;
     966          28 :   long v = varn(x), i, l, r1;
     967             : 
     968          28 :   R = cleanroots(x, prec); l = lg(R);
     969          70 :   for (r1 = 1; r1 < l; r1++)
     970          49 :     if (typ(gel(R,r1)) == t_COMPLEX) break;
     971          28 :   l = (r1+l)>>1; P = cgetg(l,t_COL);
     972          70 :   for (i = 1; i < r1; i++)
     973          42 :     gel(P,i) = deg1pol_shallow(gen_1, negr(gel(R,i)), v);
     974          35 :   for (   ; i < l; i++)
     975             :   {
     976           7 :     GEN a = gel(R,2*i-r1), t;
     977           7 :     t = gmul2n(gel(a,1), 1); togglesign(t);
     978           7 :     gel(P,i) = deg2pol_shallow(gen_1, t, gnorm(a), v);
     979             :   }
     980          28 :   gel(y,1) = gerepileupto(av, P);
     981          28 :   gel(y,2) = const_col(l-1, gen_1); return y;
     982             : }
     983             : static GEN
     984          21 : CX_factor(GEN x, long prec)
     985             : {
     986          21 :   GEN y = cgetg(3,t_MAT), R;
     987          21 :   pari_sp av = avma;
     988          21 :   long v = varn(x);
     989             : 
     990          21 :   R = roots(x, prec);
     991          21 :   gel(y,1) = gerepileupto(av, deg1_from_roots(R, v));
     992          21 :   gel(y,2) = const_col(degpol(x), gen_1); return y;
     993             : }
     994             : 
     995             : static GEN
     996       13811 : RgX_factor(GEN x, GEN dom)
     997             : {
     998             :   GEN  p, T;
     999       13811 :   long pa, tx = dom ? RgX_Rg_type(x,dom,&p,&T,&pa): RgX_type(x,&p,&T,&pa);
    1000       13811 :   switch(tx)
    1001             :   {
    1002           7 :     case 0: pari_err_IMPL("factor for general polynomials");
    1003         245 :     case t_POL: return RgXY_factor(x, dom);
    1004       12782 :     case t_INT: return ZX_factor(x);
    1005           7 :     case t_FRAC: return QX_factor(x);
    1006         329 :     case t_INTMOD: return factmod(x, p);
    1007          42 :     case t_PADIC: return factorpadic(x, p, pa);
    1008          98 :     case t_FFELT: return FFX_factor(x, T);
    1009          21 :     case t_COMPLEX: return CX_factor(x, pa);
    1010          28 :     case t_REAL: return RX_factor(x, pa);
    1011             :   }
    1012         252 :   return RgXQ_factor(x, T, p, tx);
    1013             : }
    1014             : 
    1015             : static GEN
    1016       63015 : factor_domain(GEN x, GEN dom)
    1017             : {
    1018       63015 :   long tx = typ(x), tdom = dom ? typ(dom): 0;
    1019             :   pari_sp av;
    1020             : 
    1021       63015 :   if (gequal0(x))
    1022          63 :     switch(tx)
    1023             :     {
    1024          63 :       case t_INT:
    1025             :       case t_COMPLEX:
    1026             :       case t_POL:
    1027          63 :       case t_RFRAC: return prime_fact(x);
    1028           0 :       default: pari_err_TYPE("factor",x);
    1029             :     }
    1030       62952 :   av = avma;
    1031       62952 :   switch(tx)
    1032             :   {
    1033        2611 :     case t_POL: return RgX_factor(x, dom);
    1034          35 :     case t_RFRAC: {
    1035          35 :       GEN a = gel(x,1), b = gel(x,2);
    1036          35 :       GEN y = famat_inv_shallow(RgX_factor(b, dom));
    1037          35 :       if (typ(a)==t_POL) y = famat_mul_shallow(RgX_factor(a, dom), y);
    1038          35 :       return gerepilecopy(av, sort_factor_pol(y, cmp_universal));
    1039             :     }
    1040       60236 :     case t_INT:  if (tdom==0 || tdom==t_INT) return Z_factor(x);
    1041          28 :     case t_FRAC: if (tdom==0 || tdom==t_INT) return Q_factor(x);
    1042             :     case t_COMPLEX: /* fall through */
    1043          49 :       if (tdom==0 || tdom==t_COMPLEX)
    1044          49 :       { GEN y = Qi_factor(x); if (y) return y; }
    1045             :       /* fall through */
    1046             :   }
    1047           0 :   pari_err_TYPE("factor",x);
    1048             :   return NULL; /* LCOV_EXCL_LINE */
    1049             : }
    1050             : 
    1051             : GEN
    1052       62336 : factor(GEN x) { return factor_domain(x, NULL); }
    1053             : 
    1054             : /*******************************************************************/
    1055             : /*                                                                 */
    1056             : /*                     ROOTS --> MONIC POLYNOMIAL                  */
    1057             : /*                                                                 */
    1058             : /*******************************************************************/
    1059             : static GEN
    1060     1718719 : normalized_mul(void *E, GEN x, GEN y)
    1061             : {
    1062     1718719 :   long a = gel(x,1)[1], b = gel(y,1)[1];
    1063             :   (void) E;
    1064     1718716 :   return mkvec2(mkvecsmall(a + b),
    1065     1718719 :                 RgX_mul_normalized(gel(x,2),a, gel(y,2),b));
    1066             : }
    1067             : /* L = [Vecsmall([a]), A], with a > 0, A an RgX, deg(A) < a; return X^a + A */
    1068             : static GEN
    1069     1038814 : normalized_to_RgX(GEN L)
    1070             : {
    1071     1038814 :   long i, a = gel(L,1)[1];
    1072     1038814 :   GEN A = gel(L,2);
    1073     1038814 :   GEN z = cgetg(a + 3, t_POL);
    1074     1038814 :   z[1] = evalsigne(1) | evalvarn(varn(A));
    1075     5784861 :   for (i = 2; i < lg(A); i++) gel(z,i) = gcopy(gel(A,i));
    1076     1043968 :   for (     ; i < a+2;   i++) gel(z,i) = gen_0;
    1077     1038816 :   gel(z,i) = gen_1; return z;
    1078             : }
    1079             : 
    1080             : static GEN
    1081          14 : roots_to_pol_FpV(GEN x, long v, GEN p)
    1082             : {
    1083          14 :   pari_sp av = avma;
    1084             :   GEN r;
    1085          14 :   if (lgefint(p) == 3)
    1086             :   {
    1087          14 :     ulong pp = uel(p, 2);
    1088          14 :     r = Flx_to_ZX_inplace(Flv_roots_to_pol(RgV_to_Flv(x, pp), pp, v<<VARNSHIFT));
    1089             :   }
    1090             :   else
    1091           0 :     r = FpV_roots_to_pol(RgV_to_FpV(x, p), p, v);
    1092          14 :   return gerepileupto(av, FpX_to_mod(r, p));
    1093             : }
    1094             : 
    1095             : static GEN
    1096           7 : roots_to_pol_FqV(GEN x, long v, GEN pol, GEN p)
    1097             : {
    1098           7 :   pari_sp av = avma;
    1099           7 :   GEN r, T = RgX_to_FpX(pol, p);
    1100           7 :   if (signe(T)==0) pari_err_OP("/", x, pol);
    1101           7 :   r = FqV_roots_to_pol(RgC_to_FqC(x, T, p), T, p, v);
    1102           7 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    1103             : }
    1104             : 
    1105             : static GEN
    1106      774217 : roots_to_pol_fast(GEN x, long v)
    1107             : {
    1108             :   GEN p, pol;
    1109             :   long pa;
    1110      774217 :   long t = RgV_type(x, &p,&pol,&pa);
    1111      774217 :   switch(t)
    1112             :   {
    1113          14 :     case t_INTMOD: return roots_to_pol_FpV(x, v, p);
    1114          14 :     case t_FFELT:  return FFV_roots_to_pol(x, pol, v);
    1115           7 :     case code(t_POLMOD, t_INTMOD):
    1116           7 :                    return roots_to_pol_FqV(x, v, pol, p);
    1117      774182 :     default:       return NULL;
    1118             :   }
    1119             : }
    1120             : 
    1121             : /* compute prod (x - a[i]) */
    1122             : GEN
    1123      774291 : roots_to_pol(GEN a, long v)
    1124             : {
    1125      774291 :   pari_sp av = avma;
    1126      774291 :   long i, k, lx = lg(a);
    1127             :   GEN L;
    1128      774291 :   if (lx == 1) return pol_1(v);
    1129      774217 :   L = roots_to_pol_fast(a, v);
    1130      774217 :   if (L) return L;
    1131      774182 :   L = cgetg(lx, t_VEC);
    1132     1660349 :   for (k=1,i=1; i<lx-1; i+=2)
    1133             :   {
    1134      886167 :     GEN s = gel(a,i), t = gel(a,i+1);
    1135      886167 :     GEN x0 = gmul(s,t);
    1136      886167 :     GEN x1 = gneg(gadd(s,t));
    1137      886167 :     gel(L,k++) = mkvec2(mkvecsmall(2), deg1pol_shallow(x1,x0,v));
    1138             :   }
    1139     1465914 :   if (i < lx) gel(L,k++) = mkvec2(mkvecsmall(1),
    1140      691732 :                                   scalarpol_shallow(gneg(gel(a,i)), v));
    1141      774182 :   setlg(L, k); L = gen_product(L, NULL, normalized_mul);
    1142      774182 :   return gerepileupto(av, normalized_to_RgX(L));
    1143             : }
    1144             : 
    1145             : /* prod_{i=1..r1} (x - a[i]) prod_{i=1..r2} (x - a[i])(x - conj(a[i]))*/
    1146             : GEN
    1147      264634 : roots_to_pol_r1(GEN a, long v, long r1)
    1148             : {
    1149      264634 :   pari_sp av = avma;
    1150      264634 :   long i, k, lx = lg(a);
    1151             :   GEN L;
    1152      264634 :   if (lx == 1) return pol_1(v);
    1153      264634 :   L = cgetg(lx, t_VEC);
    1154      711109 :   for (k=1,i=1; i<r1; i+=2)
    1155             :   {
    1156      446476 :     GEN s = gel(a,i), t = gel(a,i+1);
    1157      446476 :     GEN x0 = gmul(s,t);
    1158      446473 :     GEN x1 = gneg(gadd(s,t));
    1159      446475 :     gel(L,k++) = mkvec2(mkvecsmall(2), deg1pol_shallow(x1,x0,v));
    1160             :   }
    1161      336756 :   if (i < r1+1) gel(L,k++) = mkvec2(mkvecsmall(1),
    1162       72123 :                                     scalarpol_shallow(gneg(gel(a,i)), v));
    1163      925665 :   for (i=r1+1; i<lx; i++)
    1164             :   {
    1165      661031 :     GEN s = gel(a,i);
    1166      661031 :     GEN x0 = gnorm(s);
    1167      661020 :     GEN x1 = gneg(gtrace(s));
    1168      661022 :     gel(L,k++) = mkvec2(mkvecsmall(2), deg1pol_shallow(x1,x0,v));
    1169             :   }
    1170      264634 :   setlg(L, k); L = gen_product(L, NULL, normalized_mul);
    1171      264633 :   return gerepileupto(av, normalized_to_RgX(L));
    1172             : }
    1173             : 
    1174             : GEN
    1175          56 : polfromroots(GEN a, long v)
    1176             : {
    1177          56 :   if (!is_vec_t(typ(a)))
    1178           0 :     pari_err_TYPE("polfromroots",a);
    1179          56 :   if (v < 0) v = 0;
    1180          56 :   if (varncmp(gvar(a), v) <= 0) pari_err_PRIORITY("polfromroots",a,"<=",v);
    1181          49 :   return roots_to_pol(a, v);
    1182             : }
    1183             : 
    1184             : /*******************************************************************/
    1185             : /*                                                                 */
    1186             : /*                          FACTORBACK                             */
    1187             : /*                                                                 */
    1188             : /*******************************************************************/
    1189             : static GEN
    1190    54633353 : mul(void *a, GEN x, GEN y) { (void)a; return gmul(x,y);}
    1191             : static GEN
    1192    79109735 : powi(void *a, GEN x, GEN y) { (void)a; return powgi(x,y);}
    1193             : static GEN
    1194    28644560 : Fpmul(void *a, GEN x, GEN y) { return Fp_mul(x,y,(GEN)a); }
    1195             : static GEN
    1196      244383 : Fppow(void *a, GEN x, GEN n) { return Fp_pow(x,n,(GEN)a); }
    1197             : 
    1198             : /* [L,e] = [fa, NULL] or [elts, NULL] or [elts, exponents] */
    1199             : GEN
    1200    33735627 : gen_factorback(GEN L, GEN e, void *data, GEN (*_mul)(void*,GEN,GEN),
    1201             :                GEN (*_pow)(void*,GEN,GEN), GEN (*_one)(void*))
    1202             : {
    1203    33735627 :   pari_sp av = avma;
    1204             :   long k, l, lx;
    1205             :   GEN p,x;
    1206             : 
    1207    33735627 :   if (e) /* supplied vector of exponents */
    1208     1415699 :     p = L;
    1209             :   else
    1210             :   {
    1211    32319928 :     switch(typ(L)) {
    1212     8360979 :       case t_VEC:
    1213             :       case t_COL: /* product of the L[i] */
    1214     8360979 :         if (lg(L)==1) return _one? _one(data): gen_1;
    1215     8284447 :         return gerepileupto(av, gen_product(L, data, _mul));
    1216    23958958 :       case t_MAT: /* genuine factorization */
    1217    23958958 :         l = lg(L);
    1218    23958958 :         if (l == 3) break;
    1219             :         /*fall through*/
    1220             :       default:
    1221           6 :         pari_err_TYPE("factorback [not a factorization]", L);
    1222             :     }
    1223    23958951 :     p = gel(L,1);
    1224    23958951 :     e = gel(L,2);
    1225             :   }
    1226    25374650 :   if (!is_vec_t(typ(p))) pari_err_TYPE("factorback [not a vector]", p);
    1227             :   /* p = elts, e = expo */
    1228    25374636 :   lx = lg(p);
    1229             :   /* check whether e is an integral vector of correct length */
    1230    25374636 :   switch(typ(e))
    1231             :   {
    1232      122797 :     case t_VECSMALL:
    1233      122797 :       if (lx != lg(e))
    1234           0 :         pari_err_TYPE("factorback [not an exponent vector]", e);
    1235      122797 :       if (lx == 1) return _one? _one(data): gen_1;
    1236      122538 :       x = cgetg(lx,t_VEC);
    1237     1250547 :       for (l=1,k=1; k<lx; k++)
    1238     1128009 :         if (e[k]) gel(x,l++) = _pow(data, gel(p,k), stoi(e[k]));
    1239      122538 :       break;
    1240    25251832 :     case t_VEC: case t_COL:
    1241    25251832 :       if (lx != lg(e) || !RgV_is_ZV(e))
    1242          14 :         pari_err_TYPE("factorback [not an exponent vector]", e);
    1243    25251819 :       if (lx == 1) return _one? _one(data): gen_1;
    1244    25142225 :       x = cgetg(lx,t_VEC);
    1245   105168590 :       for (l=1,k=1; k<lx; k++)
    1246    80026366 :         if (signe(gel(e,k))) gel(x,l++) = _pow(data, gel(p,k), gel(e,k));
    1247    25142224 :       break;
    1248           7 :     default:
    1249           7 :       pari_err_TYPE("factorback [not an exponent vector]", e);
    1250             :       return NULL;/*LCOV_EXCL_LINE*/
    1251             :   }
    1252    25264762 :   if (l==1) return gerepileupto(av, _one? _one(data): gen_1);
    1253    25204035 :   x[0] = evaltyp(t_VEC) | _evallg(l);
    1254    25204035 :   return gerepileupto(av, gen_product(x, data, _mul));
    1255             : }
    1256             : 
    1257             : GEN
    1258     8468753 : FpV_factorback(GEN L, GEN e, GEN p)
    1259     8468753 : { return gen_factorback(L, e, (void*)p, &Fpmul, &Fppow, NULL); }
    1260             : 
    1261             : ulong
    1262      108623 : Flv_factorback(GEN L, GEN e, ulong p)
    1263             : {
    1264      108623 :   long i, l = lg(e);
    1265      108623 :   ulong r = 1UL, ri = 1UL;
    1266      529663 :   for (i = 1; i < l; i++)
    1267             :   {
    1268      421040 :     long c = e[i];
    1269      421040 :     if (!c) continue;
    1270      178632 :     if (c < 0)
    1271           0 :       ri = Fl_mul(ri, Fl_powu(L[i],-c,p), p);
    1272             :     else
    1273      178632 :       r = Fl_mul(r, Fl_powu(L[i],c,p), p);
    1274             :   }
    1275      108623 :   if (ri != 1UL) r = Fl_div(r, ri, p);
    1276      108623 :   return r;
    1277             : }
    1278             : GEN
    1279        2499 : FlxqV_factorback(GEN L, GEN e, GEN Tp, ulong p)
    1280             : {
    1281        2499 :   pari_sp av = avma;
    1282        2499 :   GEN Hi = NULL, H = NULL;
    1283        2499 :   long i, l = lg(L), v = get_Flx_var(Tp);
    1284      168168 :   for (i = 1; i < l; i++)
    1285             :   {
    1286      165618 :     GEN x, ei = gel(e,i);
    1287      165618 :     long s = signe(ei);
    1288      165618 :     if (!s) continue;
    1289      157597 :     x = Flxq_pow(gel(L,i), s > 0? ei: negi(ei), Tp, p);
    1290      157594 :     if (s > 0)
    1291       79407 :       H = H? Flxq_mul(H, x, Tp, p): x;
    1292             :     else
    1293       78187 :       Hi = Hi? Flxq_mul(Hi, x, Tp, p): x;
    1294             :   }
    1295        2550 :   if (!Hi)
    1296             :   {
    1297           0 :     if (!H) { set_avma(av); return mkvecsmall2(v,1); }
    1298           0 :     return gerepileuptoleaf(av, H);
    1299             :   }
    1300        2550 :   Hi = Flxq_inv(Hi, Tp, p);
    1301        2499 :   return gerepileuptoleaf(av, H? Flxq_mul(H,Hi,Tp,p): Hi);
    1302             : }
    1303             : GEN
    1304          14 : FqV_factorback(GEN L, GEN e, GEN Tp, GEN p)
    1305             : {
    1306          14 :   pari_sp av = avma;
    1307          14 :   GEN Hi = NULL, H = NULL;
    1308          14 :   long i, l = lg(L), small = typ(e) == t_VECSMALL;
    1309        1554 :   for (i = 1; i < l; i++)
    1310             :   {
    1311             :     GEN x;
    1312             :     long s;
    1313        1540 :     if (small)
    1314             :     {
    1315           0 :       s = e[i]; if (!s) continue;
    1316           0 :       x = Fq_powu(gel(L,i), labs(s), Tp, p);
    1317             :     }
    1318             :     else
    1319             :     {
    1320        1540 :       GEN ei = gel(e,i);
    1321        1540 :       s = signe(ei); if (!s) continue;
    1322        1540 :       x = Fq_pow(gel(L,i), s > 0? ei: negi(ei), Tp, p);
    1323             :     }
    1324        1540 :     if (s > 0)
    1325         819 :       H = H? Fq_mul(H, x, Tp, p): x;
    1326             :     else
    1327         721 :       Hi = Hi? Fq_mul(Hi, x, Tp, p): x;
    1328             :   }
    1329          14 :   if (Hi)
    1330             :   {
    1331           7 :     Hi = Fq_inv(Hi, Tp, p);
    1332           7 :     H = H? Fq_mul(H,Hi,Tp,p): Hi;
    1333             :   }
    1334           7 :   else if (!H) return gc_const(av, gen_1);
    1335          14 :   return gerepileupto(av, H);
    1336             : }
    1337             : 
    1338             : GEN
    1339    24935472 : factorback2(GEN L, GEN e) { return gen_factorback(L, e, NULL, &mul, &powi, NULL); }
    1340             : GEN
    1341     1400821 : factorback(GEN fa) { return factorback2(fa, NULL); }
    1342             : 
    1343             : GEN
    1344       10003 : vecprod(GEN v)
    1345             : {
    1346       10003 :   pari_sp av = avma;
    1347       10003 :   if (!is_vec_t(typ(v)))
    1348           0 :     pari_err_TYPE("vecprod", v);
    1349       10003 :   if (lg(v) == 1) return gen_1;
    1350        9226 :   return gerepilecopy(av, gen_product(v, NULL, mul));
    1351             : }
    1352             : 
    1353             : static int
    1354       11165 : RgX_is_irred_i(GEN x)
    1355             : {
    1356             :   GEN y, p, pol;
    1357       11165 :   long l = lg(x), pa;
    1358             : 
    1359       11165 :   if (!signe(x) || l <= 3) return 0;
    1360       11165 :   switch(RgX_type(x,&p,&pol,&pa))
    1361             :   {
    1362          21 :     case t_INTMOD: return FpX_is_irred(RgX_to_FpX(x,p), p);
    1363           0 :     case t_COMPLEX: return l == 4;
    1364           0 :     case t_REAL:
    1365           0 :       if (l == 4) return 1;
    1366           0 :       if (l > 5) return 0;
    1367           0 :       return gsigne(RgX_disc(x)) > 0;
    1368             :   }
    1369       11144 :   y = RgX_factor(x, NULL);
    1370       11144 :   return (lg(gcoeff(y,1,1))==l);
    1371             : }
    1372             : static int
    1373       11165 : RgX_is_irred(GEN x)
    1374       11165 : { pari_sp av = avma; return gc_bool(av, RgX_is_irred_i(x)); }
    1375             : long
    1376       11165 : polisirreducible(GEN x)
    1377             : {
    1378       11165 :   long tx = typ(x);
    1379       11165 :   if (tx == t_POL) return RgX_is_irred(x);
    1380           0 :   if (!is_scalar_t(tx)) pari_err_TYPE("polisirreducible",x);
    1381           0 :   return 0;
    1382             : }
    1383             : 
    1384             : /*******************************************************************/
    1385             : /*                                                                 */
    1386             : /*                         GENERIC GCD                             */
    1387             : /*                                                                 */
    1388             : /*******************************************************************/
    1389             : /* x is a COMPLEX or a QUAD */
    1390             : static GEN
    1391        2555 : triv_cont_gcd(GEN x, GEN y)
    1392             : {
    1393        2555 :   pari_sp av = avma;
    1394             :   GEN c;
    1395        2555 :   if (typ(x)==t_COMPLEX)
    1396             :   {
    1397        2233 :     GEN a = gel(x,1), b = gel(x,2);
    1398        2233 :     if (typ(a) == t_REAL || typ(b) == t_REAL) return gen_1;
    1399          21 :     c = ggcd(a,b);
    1400             :   }
    1401             :   else
    1402         322 :     c = ggcd(gel(x,2),gel(x,3));
    1403         343 :   return gerepileupto(av, ggcd(c,y));
    1404             : }
    1405             : 
    1406             : /* y is a PADIC, x a rational number or an INTMOD */
    1407             : static GEN
    1408        1407 : padic_gcd(GEN x, GEN y)
    1409             : {
    1410        1407 :   GEN p = gel(y,2);
    1411        1407 :   long v = gvaluation(x,p), w = valp(y);
    1412        1407 :   if (w < v) v = w;
    1413        1407 :   return powis(p, v);
    1414             : }
    1415             : 
    1416             : static void
    1417         896 : Zi_mul3(GEN xr, GEN xi, GEN yr, GEN yi, GEN *zr, GEN *zi)
    1418             : {
    1419         896 :   GEN p3 = addii(xr,xi);
    1420         896 :   GEN p4 = addii(yr,yi);
    1421         896 :   GEN p1 = mulii(xr,yr);
    1422         896 :   GEN p2 = mulii(xi,yi);
    1423         896 :   p3 = mulii(p3,p4);
    1424         896 :   p4 = addii(p2,p1);
    1425         896 :   *zr = subii(p1,p2); *zi = subii(p3,p4);
    1426         896 : }
    1427             : 
    1428             : static GEN
    1429         448 : Zi_rem(GEN x, GEN y)
    1430             : {
    1431         448 :   GEN xr = real_i(x), xi = imag_i(x);
    1432         448 :   GEN yr = real_i(y), yi = imag_i(y);
    1433         448 :   GEN n = addii(sqri(yr), sqri(yi));
    1434             :   GEN ur, ui, zr, zi;
    1435         448 :   Zi_mul3(xr, xi, yr, negi(yi), &ur, &ui);
    1436         448 :   Zi_mul3(yr, yi, diviiround(ur, n), diviiround(ui, n), &zr, &zi);
    1437         448 :   return mkcomplex(subii(xr,zr), subii(xi,zi));
    1438             : }
    1439             : 
    1440             : static GEN
    1441         399 : Qi_gcd(GEN x, GEN y)
    1442             : {
    1443         399 :   pari_sp av = avma, btop;
    1444             :   GEN dx, dy;
    1445         399 :   x = Q_remove_denom(x, &dx);
    1446         399 :   y = Q_remove_denom(y, &dy);
    1447         399 :   btop = avma;
    1448         847 :   while (!gequal0(y))
    1449             :   {
    1450         448 :     GEN z = Zi_rem(x,y);
    1451         448 :     x = y; y = z;
    1452         448 :     if (gc_needed(btop,1)) {
    1453           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"Qi_gcd");
    1454           0 :       gerepileall(btop,2, &x,&y);
    1455             :     }
    1456             :   }
    1457         399 :   x = Qi_normal(x);
    1458         399 :   if (typ(x) == t_COMPLEX)
    1459             :   {
    1460         280 :     if      (gequal0(gel(x,2))) x = gel(x,1);
    1461         203 :     else if (gequal0(gel(x,1))) x = gel(x,2);
    1462             :   }
    1463         399 :   if (!dx && !dy) return gerepilecopy(av, x);
    1464          35 :   return gerepileupto(av, gdiv(x, dx? (dy? lcmii(dx, dy): dx): dy));
    1465             : }
    1466             : 
    1467             : static int
    1468        2590 : c_is_rational(GEN x)
    1469        2590 : { return is_rational_t(typ(gel(x,1))) && is_rational_t(typ(gel(x,2))); }
    1470             : static GEN
    1471        1267 : c_zero_gcd(GEN c)
    1472             : {
    1473        1267 :   GEN x = gel(c,1), y = gel(c,2);
    1474        1267 :   long tx = typ(x), ty = typ(y);
    1475        1267 :   if (tx == t_REAL || ty == t_REAL) return gen_1;
    1476          42 :   if (tx == t_PADIC || tx == t_INTMOD
    1477          42 :    || ty == t_PADIC || ty == t_INTMOD) return ggcd(x, y);
    1478          35 :   return Qi_gcd(c, gen_0);
    1479             : }
    1480             : 
    1481             : /* gcd(x, 0) */
    1482             : static GEN
    1483     8253909 : zero_gcd(GEN x)
    1484             : {
    1485             :   pari_sp av;
    1486     8253909 :   switch(typ(x))
    1487             :   {
    1488       47700 :     case t_INT: return absi(x);
    1489       46669 :     case t_FRAC: return absfrac(x);
    1490        1267 :     case t_COMPLEX: return c_zero_gcd(x);
    1491         602 :     case t_REAL: return gen_1;
    1492         728 :     case t_PADIC: return powis(gel(x,2), valp(x));
    1493         252 :     case t_SER: return pol_xnall(valser(x), varn(x));
    1494        3286 :     case t_POLMOD: {
    1495        3286 :       GEN d = gel(x,2);
    1496        3286 :       if (typ(d) == t_POL && varn(d) == varn(gel(x,1))) return content(d);
    1497         441 :       return isinexact(d)? zero_gcd(d): gcopy(d);
    1498             :     }
    1499     7909066 :     case t_POL:
    1500     7909066 :       if (!isinexact(x)) break;
    1501          14 :       av = avma;
    1502          14 :       return gerepileupto(av, monomialcopy(content(x), RgX_val(x), varn(x)));
    1503             : 
    1504      217109 :     case t_RFRAC:
    1505      217109 :       if (!isinexact(x)) break;
    1506           0 :       av = avma;
    1507           0 :       return gerepileupto(av, gdiv(zero_gcd(gel(x,1)), gel(x,2)));
    1508             :   }
    1509     8153391 :   return gcopy(x);
    1510             : }
    1511             : /* z is an exact zero, t_INT, t_INTMOD or t_FFELT */
    1512             : static GEN
    1513     8878592 : zero_gcd2(GEN y, GEN z)
    1514             : {
    1515             :   pari_sp av;
    1516     8878592 :   switch(typ(z))
    1517             :   {
    1518     8234704 :     case t_INT: return zero_gcd(y);
    1519      640220 :     case t_INTMOD:
    1520      640220 :       av = avma;
    1521      640220 :       return gerepileupto(av, gmul(y, mkintmod(gen_1,gel(z,1))));
    1522        3668 :     case t_FFELT:
    1523        3668 :       av = avma;
    1524        3668 :       return gerepileupto(av, gmul(y, FF_1(z)));
    1525           0 :     default:
    1526           0 :       pari_err_TYPE("zero_gcd", z);
    1527             :       return NULL;/*LCOV_EXCL_LINE*/
    1528             :   }
    1529             : }
    1530             : static GEN
    1531     2745858 : cont_gcd_pol_i(GEN x, GEN y) { return scalarpol(ggcd(content(x),y), varn(x));}
    1532             : /* tx = t_POL, y considered as constant */
    1533             : static GEN
    1534     2745858 : cont_gcd_pol(GEN x, GEN y)
    1535     2745858 : { pari_sp av = avma; return gerepileupto(av, cont_gcd_pol_i(x,y)); }
    1536             : /* tx = t_RFRAC, y considered as constant */
    1537             : static GEN
    1538       10143 : cont_gcd_rfrac(GEN x, GEN y)
    1539             : {
    1540       10143 :   pari_sp av = avma;
    1541       10143 :   GEN cx; x = primitive_part(x, &cx);
    1542             :   /* e.g. Mod(1,2) / (2*y+1) => primitive_part = Mod(1,2)*y^0 */
    1543       10143 :   if (typ(x) != t_RFRAC) x = cont_gcd_pol_i(x, y);
    1544       10143 :   else x = gred_rfrac_simple(ggcd(cx? cx: gen_1, y), gel(x,2));
    1545       10143 :   return gerepileupto(av, x);
    1546             : }
    1547             : /* !is_const_t(tx), tx != t_POL,t_RFRAC, y considered as constant */
    1548             : static GEN
    1549        2593 : cont_gcd_gen(GEN x, GEN y)
    1550             : {
    1551        2593 :   pari_sp av = avma;
    1552        2593 :   return gerepileupto(av, ggcd(content(x),y));
    1553             : }
    1554             : /* !is_const(tx), y considered as constant */
    1555             : static GEN
    1556     2758573 : cont_gcd(GEN x, long tx, GEN y)
    1557             : {
    1558     2758573 :   switch(tx)
    1559             :   {
    1560       10143 :     case t_RFRAC: return cont_gcd_rfrac(x,y);
    1561     2745837 :     case t_POL: return cont_gcd_pol(x,y);
    1562        2593 :     default: return cont_gcd_gen(x,y);
    1563             :   }
    1564             : }
    1565             : static GEN
    1566    12444819 : gcdiq(GEN x, GEN y)
    1567             : {
    1568             :   GEN z;
    1569    12444819 :   if (!signe(x)) return Q_abs(y);
    1570     4635703 :   z = cgetg(3,t_FRAC);
    1571     4635726 :   gel(z,1) = gcdii(x,gel(y,1));
    1572     4635691 :   gel(z,2) = icopy(gel(y,2));
    1573     4635697 :   return z;
    1574             : }
    1575             : static GEN
    1576    27377138 : gcdqq(GEN x, GEN y)
    1577             : {
    1578    27377138 :   GEN z = cgetg(3,t_FRAC);
    1579    27377116 :   gel(z,1) = gcdii(gel(x,1), gel(y,1));
    1580    27376939 :   gel(z,2) = lcmii(gel(x,2), gel(y,2));
    1581    27377064 :   return z;
    1582             : }
    1583             : /* assume x,y t_INT or t_FRAC */
    1584             : GEN
    1585  1004402885 : Q_gcd(GEN x, GEN y)
    1586             : {
    1587  1004402885 :   long tx = typ(x), ty = typ(y);
    1588  1004402885 :   if (tx == t_INT)
    1589   967557762 :   { return (ty == t_INT)? gcdii(x,y): gcdiq(x,y); }
    1590             :   else
    1591    36845123 :   { return (ty == t_INT)? gcdiq(y,x): gcdqq(x,y); }
    1592             : }
    1593             : static GEN
    1594        6089 : gcd3(GEN x, GEN y, GEN z) { return ggcd(ggcd(x, y), z); }
    1595             : 
    1596             : GEN
    1597    30225355 : ggcd(GEN x, GEN y)
    1598             : {
    1599    30225355 :   long vx, vy, tx = typ(x), ty = typ(y);
    1600             :   pari_sp av, tetpil;
    1601             :   GEN p1,z;
    1602             : 
    1603    60450710 :   if (is_noncalc_t(tx) || is_matvec_t(tx) ||
    1604    60450710 :       is_noncalc_t(ty) || is_matvec_t(ty)) pari_err_TYPE2("gcd",x,y);
    1605    30225355 :   if (tx>ty) { swap(x,y); lswap(tx,ty); }
    1606             :   /* tx <= ty */
    1607    30225355 :   z = gisexactzero(x); if (z) return zero_gcd2(y,z);
    1608    27130726 :   z = gisexactzero(y); if (z) return zero_gcd2(x,z);
    1609    21346763 :   if (is_const_t(tx))
    1610             :   {
    1611    13344489 :     if (ty == tx) switch(tx)
    1612             :     {
    1613     8089774 :       case t_INT:
    1614     8089774 :         return gcdii(x,y);
    1615             : 
    1616     2179226 :       case t_INTMOD: z=cgetg(3,t_INTMOD);
    1617     2179226 :         if (equalii(gel(x,1),gel(y,1)))
    1618     2179219 :           gel(z,1) = icopy(gel(x,1));
    1619             :         else
    1620           7 :           gel(z,1) = gcdii(gel(x,1),gel(y,1));
    1621     2179226 :         if (gequal1(gel(z,1))) gel(z,2) = gen_0;
    1622             :         else
    1623             :         {
    1624     2179226 :           av = avma; p1 = gcdii(gel(z,1),gel(x,2));
    1625     2179226 :           if (!equali1(p1))
    1626             :           {
    1627           7 :             p1 = gcdii(p1,gel(y,2));
    1628           7 :             if (equalii(p1, gel(z,1))) { cgiv(p1); p1 = gen_0; }
    1629           7 :             else p1 = gerepileuptoint(av, p1);
    1630             :           }
    1631     2179226 :           gel(z,2) = p1;
    1632             :         }
    1633     2179226 :         return z;
    1634             : 
    1635      262695 :       case t_FRAC:
    1636      262695 :         return gcdqq(x,y);
    1637             : 
    1638        5656 :       case t_FFELT:
    1639        5656 :         if (!FF_samefield(x,y)) pari_err_OP("gcd",x,y);
    1640        5656 :         return FF_equal0(x) && FF_equal0(y)? FF_zero(y): FF_1(y);
    1641             : 
    1642          21 :       case t_COMPLEX:
    1643          21 :         if (c_is_rational(x) && c_is_rational(y)) return Qi_gcd(x,y);
    1644           7 :         return triv_cont_gcd(y,x);
    1645             : 
    1646          14 :       case t_PADIC:
    1647          14 :         if (!equalii(gel(x,2),gel(y,2))) return gen_1;
    1648           7 :         return powis(gel(y,2), minss(valp(x), valp(y)));
    1649             : 
    1650         133 :       case t_QUAD:
    1651         133 :         av=avma; p1=gdiv(x,y);
    1652         133 :         if (gequal0(gel(p1,3)))
    1653             :         {
    1654          14 :           p1=gel(p1,2);
    1655          14 :           if (typ(p1)==t_INT) { set_avma(av); return gcopy(y); }
    1656           7 :           tetpil=avma; return gerepile(av,tetpil, gdiv(y,gel(p1,2)));
    1657             :         }
    1658         119 :         if (typ(gel(p1,2))==t_INT && typ(gel(p1,3))==t_INT) {set_avma(av); return gcopy(y);}
    1659         112 :         p1 = ginv(p1); set_avma(av);
    1660         112 :         if (typ(gel(p1,2))==t_INT && typ(gel(p1,3))==t_INT) return gcopy(x);
    1661         105 :         return triv_cont_gcd(y,x);
    1662             : 
    1663           0 :       default: return gen_1; /* t_REAL */
    1664             :     }
    1665     2806970 :     if (is_const_t(ty)) switch(tx)
    1666             :     {
    1667       74276 :       case t_INT:
    1668       74276 :         switch(ty)
    1669             :         {
    1670          77 :           case t_INTMOD: z = cgetg(3,t_INTMOD);
    1671          77 :             gel(z,1) = icopy(gel(y,1)); av = avma;
    1672          77 :             p1 = gcdii(gel(y,1),gel(y,2));
    1673          77 :             if (!equali1(p1)) {
    1674          14 :               p1 = gcdii(x,p1);
    1675          14 :               if (equalii(p1, gel(z,1))) { cgiv(p1); p1 = gen_0; }
    1676             :               else
    1677          14 :                 p1 = gerepileuptoint(av, p1);
    1678             :             }
    1679          77 :             gel(z,2) = p1; return z;
    1680             : 
    1681        8162 :           case t_REAL: return gen_1;
    1682             : 
    1683       61977 :           case t_FRAC:
    1684       61977 :             return gcdiq(x,y);
    1685             : 
    1686        2471 :           case t_COMPLEX:
    1687        2471 :             if (c_is_rational(y)) return Qi_gcd(x,y);
    1688        2128 :             return triv_cont_gcd(y,x);
    1689             : 
    1690          70 :           case t_FFELT:
    1691          70 :             if (!FF_equal0(y)) return FF_1(y);
    1692           0 :             return dvdii(x, gel(y,4))? FF_zero(y): FF_1(y);
    1693             : 
    1694        1393 :           case t_PADIC:
    1695        1393 :             return padic_gcd(x,y);
    1696             : 
    1697         126 :           case t_QUAD:
    1698         126 :             return triv_cont_gcd(y,x);
    1699           0 :           default:
    1700           0 :             pari_err_TYPE2("gcd",x,y);
    1701             :         }
    1702             : 
    1703          14 :       case t_REAL:
    1704          14 :         switch(ty)
    1705             :         {
    1706          14 :           case t_INTMOD:
    1707             :           case t_FFELT:
    1708          14 :           case t_PADIC: pari_err_TYPE2("gcd",x,y);
    1709           0 :           default: return gen_1;
    1710             :         }
    1711             : 
    1712          49 :       case t_INTMOD:
    1713          49 :         switch(ty)
    1714             :         {
    1715          14 :           case t_FRAC:
    1716          14 :             av = avma;
    1717          14 :             if (!equali1(gcdii(gel(x,1),gel(y,2)))) pari_err_OP("gcd",x,y);
    1718           7 :             set_avma(av); return ggcd(gel(y,1), x);
    1719             : 
    1720          14 :           case t_FFELT:
    1721             :           {
    1722          14 :             GEN p = gel(y,4);
    1723          14 :             if (!dvdii(gel(x,1), p)) pari_err_OP("gcd",x,y);
    1724           7 :             if (!FF_equal0(y)) return FF_1(y);
    1725           0 :             return dvdii(gel(x,2),p)? FF_zero(y): FF_1(y);
    1726             :           }
    1727             : 
    1728          14 :           case t_COMPLEX: case t_QUAD:
    1729          14 :             return triv_cont_gcd(y,x);
    1730             : 
    1731           7 :           case t_PADIC:
    1732           7 :             return padic_gcd(x,y);
    1733             : 
    1734           0 :           default: pari_err_TYPE2("gcd",x,y);
    1735             :         }
    1736             : 
    1737         210 :       case t_FRAC:
    1738         210 :         switch(ty)
    1739             :         {
    1740          84 :           case t_COMPLEX:
    1741          84 :             if (c_is_rational(y)) return Qi_gcd(x,y);
    1742             :           case t_QUAD:
    1743         154 :             return triv_cont_gcd(y,x);
    1744          42 :           case t_FFELT:
    1745             :           {
    1746          42 :             GEN p = gel(y,4);
    1747          42 :             if (dvdii(gel(x,2), p)) pari_err_OP("gcd",x,y);
    1748          21 :             if (!FF_equal0(y)) return FF_1(y);
    1749           0 :             return dvdii(gel(x,1),p)? FF_zero(y): FF_1(y);
    1750             :           }
    1751             : 
    1752           7 :           case t_PADIC:
    1753           7 :             return padic_gcd(x,y);
    1754             : 
    1755           0 :           default: pari_err_TYPE2("gcd",x,y);
    1756             :         }
    1757          70 :       case t_FFELT:
    1758          70 :         switch(ty)
    1759             :         {
    1760          42 :           case t_PADIC:
    1761             :           {
    1762          42 :             GEN p = gel(y,2);
    1763          42 :             long v = valp(y);
    1764          42 :             if (!equalii(p, gel(x,4)) || v < 0) pari_err_OP("gcd",x,y);
    1765          14 :             return (v && FF_equal0(x))? FF_zero(x): FF_1(x);
    1766             :           }
    1767          28 :           default: pari_err_TYPE2("gcd",x,y);
    1768             :         }
    1769             : 
    1770          14 :       case t_COMPLEX:
    1771          14 :         switch(ty)
    1772             :         {
    1773          14 :           case t_PADIC:
    1774          14 :           case t_QUAD: return triv_cont_gcd(x,y);
    1775           0 :           default: pari_err_TYPE2("gcd",x,y);
    1776             :         }
    1777             : 
    1778           7 :       case t_PADIC:
    1779           7 :         switch(ty)
    1780             :         {
    1781           7 :           case t_QUAD: return triv_cont_gcd(y,x);
    1782           0 :           default: pari_err_TYPE2("gcd",x,y);
    1783             :         }
    1784             : 
    1785           0 :       default: return gen_1; /* tx = t_REAL */
    1786             :     }
    1787     2732330 :     return cont_gcd(y,ty, x);
    1788             :   }
    1789             : 
    1790     8002274 :   if (tx == t_POLMOD)
    1791             :   {
    1792        6138 :     if (ty == t_POLMOD)
    1793             :     {
    1794        6061 :       GEN T = gel(x,1), Ty = gel(y,1);
    1795        6061 :       vx = varn(T); vy = varn(Ty);
    1796        6061 :       z = cgetg(3,t_POLMOD);
    1797        6061 :       if (vx == vy)
    1798        6047 :         T = RgX_equal(T,Ty)? RgX_copy(T): RgX_gcd(T, Ty);
    1799             :       else
    1800          14 :         T = RgX_copy(varncmp(vx,vy) < 0? T: Ty);
    1801        6061 :       gel(z,1) = T;
    1802        6061 :       if (degpol(T) <= 0) gel(z,2) = gen_0;
    1803             :       else
    1804             :       {
    1805        6061 :         GEN X = gel(x,2), Y = gel(y,2), d;
    1806        6061 :         av = avma; d = ggcd(content(X), content(Y));
    1807        6061 :         if (!gequal1(d)) { X = gdiv(X,d); Y = gdiv(Y,d); }
    1808        6061 :         gel(z,2) = gerepileupto(av, gmul(d, gcd3(T, X, Y)));
    1809             :       }
    1810        6061 :       return z;
    1811             :     }
    1812          77 :     vx = varn(gel(x,1));
    1813          77 :     switch(ty)
    1814             :     {
    1815          49 :       case t_POL:
    1816          49 :         vy = varn(y);
    1817          49 :         if (varncmp(vy,vx) < 0) return cont_gcd_pol(y, x);
    1818          28 :         z = cgetg(3,t_POLMOD);
    1819          28 :         gel(z,1) = RgX_copy(gel(x,1)); av = avma;
    1820          28 :         gel(z,2) = gerepileupto(av, gcd3(gel(x,1), gel(x,2), y));
    1821          28 :         return z;
    1822             : 
    1823          28 :       case t_RFRAC:
    1824          28 :         vy = varn(gel(y,2));
    1825          28 :         if (varncmp(vy,vx) < 0) return cont_gcd_rfrac(y, x);
    1826          28 :         av = avma;
    1827          28 :         if (degpol(ggcd(gel(x,1),gel(y,2)))) pari_err_OP("gcd",x,y);
    1828          21 :         set_avma(av); return gdiv(ggcd(gel(y,1),x), content(gel(y,2)));
    1829             :     }
    1830             :   }
    1831             : 
    1832     7996136 :   vx = gvar(x);
    1833     7996136 :   vy = gvar(y);
    1834     7996136 :   if (varncmp(vy, vx) < 0) return cont_gcd(y,ty, x);
    1835     7983942 :   if (varncmp(vy, vx) > 0) return cont_gcd(x,tx, y);
    1836             : 
    1837             :   /* vx = vy: same main variable */
    1838     7969893 :   switch(tx)
    1839             :   {
    1840     7809057 :     case t_POL:
    1841             :       switch(ty)
    1842             :       {
    1843     6955955 :         case t_POL: return RgX_gcd(x,y);
    1844          28 :         case t_SER:
    1845          28 :           z = ggcd(content(x), content(y));
    1846          28 :           return monomialcopy(z, minss(valser(y),gval(x,vx)), vx);
    1847      853074 :         case t_RFRAC:
    1848      853074 :           av = avma; z = gred_rfrac_simple(ggcd(gel(y,1), x), gel(y,2));
    1849      853074 :           return gerepileupto(av, z);
    1850             :       }
    1851           0 :       break;
    1852             : 
    1853          14 :     case t_SER:
    1854          14 :       z = ggcd(content(x), content(y));
    1855             :       switch(ty)
    1856             :       {
    1857           7 :         case t_SER:   return monomialcopy(z, minss(valser(x),valser(y)), vx);
    1858           7 :         case t_RFRAC: return monomialcopy(z, minss(valser(x),gval(y,vx)), vx);
    1859             :       }
    1860           0 :       break;
    1861             : 
    1862      160822 :     case t_RFRAC:
    1863             :     {
    1864      160822 :       GEN xd = gel(x,2), yd = gel(y,2);
    1865      160822 :       if (ty != t_RFRAC) pari_err_TYPE2("gcd",x,y);
    1866      160822 :       z = cgetg(3,t_RFRAC); av = avma;
    1867      160822 :       gel(z,2) = gerepileupto(av, RgX_mul(xd, RgX_div(yd, RgX_gcd(xd, yd))));
    1868      160822 :       gel(z,1) = ggcd(gel(x,1), gel(y,1)); return z;
    1869             :     }
    1870             :   }
    1871           0 :   pari_err_TYPE2("gcd",x,y);
    1872             :   return NULL; /* LCOV_EXCL_LINE */
    1873             : }
    1874             : GEN
    1875     5203595 : ggcd0(GEN x, GEN y) { return y? ggcd(x,y): content(x); }
    1876             : 
    1877             : static GEN
    1878        3680 : fix_lcm(GEN x)
    1879             : {
    1880             :   GEN t;
    1881        3680 :   switch(typ(x))
    1882             :   {
    1883        3575 :     case t_INT:
    1884        3575 :       x = absi_shallow(x); break;
    1885          98 :     case t_POL:
    1886          98 :       if (lg(x) <= 2) break;
    1887          98 :       t = leading_coeff(x);
    1888          98 :       if (typ(t) == t_INT && signe(t) < 0) x = gneg(x);
    1889             :   }
    1890        3680 :   return x;
    1891             : }
    1892             : GEN
    1893        2898 : glcm0(GEN x, GEN y)
    1894             : {
    1895        2898 :   if (!y) return fix_lcm(gassoc_proto(glcm,x,y));
    1896        2849 :   return glcm(x,y);
    1897             : }
    1898             : GEN
    1899        3575 : ZV_lcm(GEN x) { return fix_lcm(gassoc_proto(lcmii,x,NULL)); }
    1900             : GEN
    1901        3283 : glcm(GEN x, GEN y)
    1902             : {
    1903             :   pari_sp av;
    1904             :   GEN z;
    1905        3283 :   if (typ(x)==t_INT && typ(y)==t_INT) return lcmii(x,y);
    1906          70 :   av = avma; z = ggcd(x,y);
    1907          70 :   if (!gequal1(z))
    1908             :   {
    1909          63 :     if (gequal0(z)) { set_avma(av); return gmul(x,y); }
    1910          49 :     y = gdiv(y,z);
    1911             :   }
    1912          56 :   return gerepileupto(av, fix_lcm(gmul(x,y)));
    1913             : }
    1914             : 
    1915             : /* x + r ~ x ? Assume x,r are t_POL, deg(r) <= deg(x) */
    1916             : static int
    1917           0 : pol_approx0(GEN r, GEN x, int exact)
    1918             : {
    1919             :   long i, l;
    1920           0 :   if (exact) return !signe(r);
    1921           0 :   l = minss(lg(x), lg(r));
    1922           0 :   for (i = 2; i < l; i++)
    1923           0 :     if (!cx_approx0(gel(r,i), gel(x,i))) return 0;
    1924           0 :   return 1;
    1925             : }
    1926             : 
    1927             : GEN
    1928           0 : RgX_gcd_simple(GEN x, GEN y)
    1929             : {
    1930           0 :   pari_sp av1, av = avma;
    1931           0 :   GEN r, yorig = y;
    1932           0 :   int exact = !(isinexactreal(x) || isinexactreal(y));
    1933             : 
    1934             :   for(;;)
    1935             :   {
    1936           0 :     av1 = avma; r = RgX_rem(x,y);
    1937           0 :     if (pol_approx0(r, x, exact))
    1938             :     {
    1939           0 :       set_avma(av1);
    1940           0 :       if (y == yorig) return RgX_copy(y);
    1941           0 :       y = normalizepol_approx(y, lg(y));
    1942           0 :       if (lg(y) == 3) { set_avma(av); return pol_1(varn(x)); }
    1943           0 :       return gerepileupto(av,y);
    1944             :     }
    1945           0 :     x = y; y = r;
    1946           0 :     if (gc_needed(av,1)) {
    1947           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_gcd_simple");
    1948           0 :       gerepileall(av,2, &x,&y);
    1949             :     }
    1950             :   }
    1951             : }
    1952             : GEN
    1953           0 : RgX_extgcd_simple(GEN a, GEN b, GEN *pu, GEN *pv)
    1954             : {
    1955           0 :   pari_sp av = avma;
    1956             :   GEN q, r, d, d1, u, v, v1;
    1957           0 :   int exact = !(isinexactreal(a) || isinexactreal(b));
    1958             : 
    1959           0 :   d = a; d1 = b; v = gen_0; v1 = gen_1;
    1960             :   for(;;)
    1961             :   {
    1962           0 :     if (pol_approx0(d1, a, exact)) break;
    1963           0 :     q = poldivrem(d,d1, &r);
    1964           0 :     v = gsub(v, gmul(q,v1));
    1965           0 :     u=v; v=v1; v1=u;
    1966           0 :     u=r; d=d1; d1=u;
    1967             :   }
    1968           0 :   u = gsub(d, gmul(b,v));
    1969           0 :   u = RgX_div(u,a);
    1970             : 
    1971           0 :   gerepileall(av, 3, &u,&v,&d);
    1972           0 :   *pu = u;
    1973           0 :   *pv = v; return d;
    1974             : }
    1975             : 
    1976             : GEN
    1977          91 : ghalfgcd(GEN x, GEN y)
    1978             : {
    1979          91 :   long tx = typ(x), ty = typ(y);
    1980          91 :   if (tx==t_INT && ty==t_INT) return halfgcdii(x, y);
    1981          63 :   if (tx==t_POL && ty==t_POL && varn(x)==varn(y))
    1982             :   {
    1983          63 :     pari_sp av = avma;
    1984          63 :     GEN a, b, M = RgX_halfgcd_all(x, y, &a, &b);
    1985          63 :     return gerepilecopy(av, mkvec2(M, mkcol2(a,b)));
    1986             :   }
    1987           0 :   pari_err_OP("halfgcd", x, y);
    1988             :   return NULL; /* LCOV_EXCL_LINE */
    1989             : }
    1990             : 
    1991             : /*******************************************************************/
    1992             : /*                                                                 */
    1993             : /*                    CONTENT / PRIMITIVE PART                     */
    1994             : /*                                                                 */
    1995             : /*******************************************************************/
    1996             : 
    1997             : GEN
    1998    72286782 : content(GEN x)
    1999             : {
    2000    72286782 :   long lx, i, t, tx = typ(x);
    2001    72286782 :   pari_sp av = avma;
    2002             :   GEN c;
    2003             : 
    2004    72286782 :   if (is_scalar_t(tx)) return zero_gcd(x);
    2005    72270373 :   switch(tx)
    2006             :   {
    2007       10178 :     case t_RFRAC:
    2008             :     {
    2009       10178 :       GEN n = gel(x,1), d = gel(x,2);
    2010             :       /* -- varncmp(vn, vd) < 0 can't happen
    2011             :        * -- if n is POLMOD, its main variable (in the sense of gvar2)
    2012             :        *    has lower priority than denominator */
    2013       10178 :       if (typ(n) == t_POLMOD || varncmp(gvar(n), varn(d)) > 0)
    2014       10141 :         n = isinexact(n)? zero_gcd(n): gcopy(n);
    2015             :       else
    2016          37 :         n = content(n);
    2017       10178 :       return gerepileupto(av, gdiv(n, content(d)));
    2018             :     }
    2019             : 
    2020     2820198 :     case t_VEC: case t_COL:
    2021     2820198 :       lx = lg(x); if (lx==1) return gen_0;
    2022     2820191 :       break;
    2023             : 
    2024          21 :     case t_MAT:
    2025             :     {
    2026             :       long hx, j;
    2027          21 :       lx = lg(x);
    2028          21 :       if (lx == 1) return gen_0;
    2029          14 :       hx = lgcols(x);
    2030          14 :       if (hx == 1) return gen_0;
    2031           7 :       if (lx == 2) { x = gel(x,1); lx = lg(x); break; }
    2032           7 :       if (hx == 2) { x = row_i(x, 1, 1, lx-1); break; }
    2033           7 :       c = content(gel(x,1));
    2034          14 :       for (j=2; j<lx; j++)
    2035          21 :         for (i=1; i<hx; i++) c = ggcd(c,gcoeff(x,i,j));
    2036           7 :       if (typ(c) == t_INTMOD || isinexact(c)) return gc_const(av, gen_1);
    2037           7 :       return gerepileupto(av,c);
    2038             :     }
    2039             : 
    2040    69439766 :     case t_POL: case t_SER:
    2041    69439766 :       lx = lg(x); if (lx == 2) return gen_0;
    2042    69416575 :       break;
    2043          21 :     case t_VECSMALL: return utoi(zv_content(x));
    2044         189 :     case t_QFB:
    2045         189 :       lx = 4; break;
    2046             : 
    2047           0 :     default: pari_err_TYPE("content",x);
    2048             :       return NULL; /* LCOV_EXCL_LINE */
    2049             :   }
    2050   213271402 :   for (i=lontyp[tx]; i<lx; i++)
    2051   154072200 :     if (typ(gel(x,i)) != t_INT) break;
    2052    72236955 :   lx--; c = gel(x,lx);
    2053    72236955 :   t = typ(c); if (is_matvec_t(t)) c = content(c);
    2054    72236956 :   if (i > lx)
    2055             :   { /* integer coeffs */
    2056    62065315 :     while (lx-- > lontyp[tx])
    2057             :     {
    2058    60613578 :       c = gcdii(c, gel(x,lx));
    2059    60613553 :       if (equali1(c)) return gc_const(av, gen_1);
    2060             :     }
    2061             :   }
    2062             :   else
    2063             :   {
    2064    13037753 :     if (isinexact(c)) c = zero_gcd(c);
    2065    34264710 :     while (lx-- > lontyp[tx])
    2066             :     {
    2067    21226957 :       GEN d = gel(x,lx);
    2068    21226957 :       t = typ(d); if (is_matvec_t(t)) d = content(d);
    2069    21226957 :       c = ggcd(c, d);
    2070             :     }
    2071    13037753 :     if (isinexact(c)) return gc_const(av, gen_1);
    2072             :   }
    2073    14489490 :   switch(typ(c))
    2074             :   {
    2075     1456651 :     case t_INT:
    2076     1456651 :       c = absi_shallow(c); break;
    2077           0 :     case t_VEC: case t_COL: case t_MAT:
    2078           0 :       pari_err_TYPE("content",x);
    2079             :   }
    2080             : 
    2081    14489495 :   return av==avma? gcopy(c): gerepileupto(av,c);
    2082             : }
    2083             : 
    2084             : GEN
    2085     1100318 : primitive_part(GEN x, GEN *ptc)
    2086             : {
    2087     1100318 :   pari_sp av = avma;
    2088     1100318 :   GEN c = content(x);
    2089     1100292 :   if (gequal1(c)) { set_avma(av); c = NULL; }
    2090      170239 :   else if (!gequal0(c)) x = gdiv(x,c);
    2091     1100300 :   if (ptc) *ptc = c;
    2092     1100300 :   return x;
    2093             : }
    2094             : GEN
    2095        2233 : primpart(GEN x) { return primitive_part(x, NULL); }
    2096             : 
    2097             : static GEN
    2098   172987454 : Q_content_v(GEN x, long imin, long l)
    2099             : {
    2100   172987454 :   pari_sp av = avma;
    2101   172987454 :   long i = l-1;
    2102   172987454 :   GEN d = Q_content_safe(gel(x,i));
    2103   172992929 :   if (!d) return NULL;
    2104  1177151076 :   for (i--; i >= imin; i--)
    2105             :   {
    2106  1004269475 :     GEN c = Q_content_safe(gel(x,i));
    2107  1004372944 :     if (!c) return NULL;
    2108  1004372916 :     d = Q_gcd(d, c);
    2109  1004158205 :     if (gc_needed(av,1)) d = gerepileupto(av, d);
    2110             :   }
    2111   172881601 :   return gerepileupto(av, d);
    2112             : }
    2113             : /* As content(), but over Q. Treats polynomial as elts of Q[x1,...xn], instead
    2114             :  * of Q(x2,...,xn)[x1] */
    2115             : GEN
    2116  1262301993 : Q_content_safe(GEN x)
    2117             : {
    2118             :   long l;
    2119  1262301993 :   switch(typ(x))
    2120             :   {
    2121  1051644349 :     case t_INT:  return absi(x);
    2122    37371157 :     case t_FRAC: return absfrac(x);
    2123   126275588 :     case t_COMPLEX: case t_VEC: case t_COL: case t_MAT:
    2124   126275588 :       l = lg(x); return l==1? gen_1: Q_content_v(x, 1, l);
    2125    47056159 :     case t_POL:
    2126    47056159 :       l = lg(x); return l==2? gen_0: Q_content_v(x, 2, l);
    2127       32687 :     case t_POLMOD: return Q_content_safe(gel(x,2));
    2128          21 :     case t_RFRAC:
    2129             :     {
    2130             :       GEN a, b;
    2131          21 :       a = Q_content_safe(gel(x,1)); if (!a) return NULL;
    2132          21 :       b = Q_content_safe(gel(x,2)); if (!b) return NULL;
    2133          21 :       return gdiv(a, b);
    2134             :     }
    2135             :   }
    2136         330 :   return NULL;
    2137             : }
    2138             : GEN
    2139      194127 : Q_content(GEN x)
    2140             : {
    2141      194127 :   GEN c = Q_content_safe(x);
    2142      194127 :   if (!c) pari_err_TYPE("Q_content",x);
    2143      194127 :   return c;
    2144             : }
    2145             : 
    2146             : GEN
    2147       13146 : ZX_content(GEN x)
    2148             : {
    2149       13146 :   long i, l = lg(x);
    2150             :   GEN d;
    2151             :   pari_sp av;
    2152             : 
    2153       13146 :   if (l == 2) return gen_0;
    2154       13146 :   d = gel(x,2);
    2155       13146 :   if (l == 3) return absi(d);
    2156        9170 :   av = avma;
    2157       18963 :   for (i=3; !is_pm1(d) && i<l; i++) d = gcdii(d, gel(x,i));
    2158        9170 :   if (signe(d) < 0) d = negi(d);
    2159        9170 :   return gerepileuptoint(av, d);
    2160             : }
    2161             : 
    2162             : static GEN
    2163     2283255 : Z_content_v(GEN x, long i, long l)
    2164             : {
    2165     2283255 :   pari_sp av = avma;
    2166     2283255 :   GEN d = Z_content(gel(x,i));
    2167     2283255 :   if (!d) return NULL;
    2168     5880807 :   for (i++; i<l; i++)
    2169             :   {
    2170     5360405 :     GEN c = Z_content(gel(x,i));
    2171     5360499 :     if (!c) return NULL;
    2172     4744945 :     d = gcdii(d, c); if (equali1(d)) return NULL;
    2173     3908910 :     if ((i & 255) == 0) d = gerepileuptoint(av, d);
    2174             :   }
    2175      520402 :   return gerepileuptoint(av, d);
    2176             : }
    2177             : /* return NULL for 1 */
    2178             : GEN
    2179     9885129 : Z_content(GEN x)
    2180             : {
    2181             :   long l;
    2182     9885129 :   switch(typ(x))
    2183             :   {
    2184     7582885 :     case t_INT:
    2185     7582885 :       if (is_pm1(x)) return NULL;
    2186     6659788 :       return absi(x);
    2187     2248907 :     case t_COMPLEX: case t_VEC: case t_COL: case t_MAT:
    2188     2248907 :       l = lg(x); return l==1? NULL: Z_content_v(x, 1, l);
    2189       53431 :     case t_POL:
    2190       53431 :       l = lg(x); return l==2? gen_0: Z_content_v(x, 2, l);
    2191           0 :     case t_POLMOD: return Z_content(gel(x,2));
    2192             :   }
    2193           0 :   pari_err_TYPE("Z_content", x);
    2194             :   return NULL; /* LCOV_EXCL_LINE */
    2195             : }
    2196             : 
    2197             : static GEN
    2198    53719225 : Q_denom_v(GEN x, long i, long l)
    2199             : {
    2200    53719225 :   pari_sp av = avma;
    2201    53719225 :   GEN d = Q_denom_safe(gel(x,i));
    2202    53719069 :   if (!d) return NULL;
    2203   186703911 :   for (i++; i<l; i++)
    2204             :   {
    2205   132984908 :     GEN D = Q_denom_safe(gel(x,i));
    2206   132984735 :     if (!D) return NULL;
    2207   132984735 :     if (D != gen_1) d = lcmii(d, D);
    2208   132984572 :     if ((i & 255) == 0) d = gerepileuptoint(av, d);
    2209             :   }
    2210    53719003 :   return gerepileuptoint(av, d);
    2211             : }
    2212             : /* NOT MEMORY CLEAN (because of t_FRAC).
    2213             :  * As denom(), but over Q. Treats polynomial as elts of Q[x1,...xn], instead
    2214             :  * of Q(x2,...,xn)[x1] */
    2215             : GEN
    2216   245449689 : Q_denom_safe(GEN x)
    2217             : {
    2218             :   long l;
    2219   245449689 :   switch(typ(x))
    2220             :   {
    2221   157786082 :     case t_INT: return gen_1;
    2222          28 :     case t_PADIC: l = valp(x); return l < 0? powiu(gel(x,2), -l): gen_1;
    2223    33681577 :     case t_FRAC: return gel(x,2);
    2224         504 :     case t_QUAD: return Q_denom_v(x, 2, 4);
    2225    41385286 :     case t_COMPLEX: case t_VEC: case t_COL: case t_MAT:
    2226    41385286 :       l = lg(x); return l==1? gen_1: Q_denom_v(x, 1, l);
    2227    12500377 :     case t_POL: case t_SER:
    2228    12500377 :       l = lg(x); return l==2? gen_1: Q_denom_v(x, 2, l);
    2229       93401 :     case t_POLMOD: return Q_denom(gel(x,2));
    2230        8134 :     case t_RFRAC:
    2231             :     {
    2232             :       GEN a, b;
    2233        8134 :       a = Q_content(gel(x,1)); if (!a) return NULL;
    2234        8134 :       b = Q_content(gel(x,2)); if (!b) return NULL;
    2235        8134 :       return Q_denom(gdiv(a, b));
    2236             :     }
    2237             :   }
    2238          66 :   return NULL;
    2239             : }
    2240             : GEN
    2241     3186083 : Q_denom(GEN x)
    2242             : {
    2243     3186083 :   GEN d = Q_denom_safe(x);
    2244     3186081 :   if (!d) pari_err_TYPE("Q_denom",x);
    2245     3186081 :   return d;
    2246             : }
    2247             : 
    2248             : GEN
    2249    55562645 : Q_remove_denom(GEN x, GEN *ptd)
    2250             : {
    2251    55562645 :   GEN d = Q_denom_safe(x);
    2252    55562719 :   if (d) { if (d == gen_1) d = NULL; else x = Q_muli_to_int(x,d); }
    2253    55562292 :   if (ptd) *ptd = d;
    2254    55562292 :   return x;
    2255             : }
    2256             : 
    2257             : /* return y = x * d, assuming x rational, and d,y integral */
    2258             : GEN
    2259   140737817 : Q_muli_to_int(GEN x, GEN d)
    2260             : {
    2261             :   GEN y, xn, xd;
    2262             :   pari_sp av;
    2263             : 
    2264   140737817 :   if (typ(d) != t_INT) pari_err_TYPE("Q_muli_to_int",d);
    2265   140742471 :   switch (typ(x))
    2266             :   {
    2267    44849230 :     case t_INT:
    2268    44849230 :       return mulii(x,d);
    2269             : 
    2270    65239251 :     case t_FRAC:
    2271    65239251 :       xn = gel(x,1);
    2272    65239251 :       xd = gel(x,2); av = avma;
    2273    65239251 :       y = mulii(xn, diviiexact(d, xd));
    2274    65234628 :       return gerepileuptoint(av, y);
    2275          42 :     case t_COMPLEX:
    2276          42 :       y = cgetg(3,t_COMPLEX);
    2277          42 :       gel(y,1) = Q_muli_to_int(gel(x,1),d);
    2278          42 :       gel(y,2) = Q_muli_to_int(gel(x,2),d);
    2279          42 :       return y;
    2280          14 :     case t_PADIC:
    2281          14 :       y = gcopy(x); if (!isint1(d)) setvalp(y, 0);
    2282          14 :       return y;
    2283         175 :     case t_QUAD:
    2284         175 :       y = cgetg(4,t_QUAD);
    2285         175 :       gel(y,1) = ZX_copy(gel(x,1));
    2286         175 :       gel(y,2) = Q_muli_to_int(gel(x,2),d);
    2287         175 :       gel(y,3) = Q_muli_to_int(gel(x,3),d); return y;
    2288             : 
    2289    19961426 :     case t_VEC:
    2290             :     case t_COL:
    2291   101194887 :     case t_MAT: pari_APPLY_same(Q_muli_to_int(gel(x,i), d));
    2292    47842770 :     case t_POL: pari_APPLY_pol_normalized(Q_muli_to_int(gel(x,i), d));
    2293          21 :     case t_SER: pari_APPLY_ser_normalized(Q_muli_to_int(gel(x,i), d));
    2294             : 
    2295       50539 :     case t_POLMOD:
    2296       50539 :       retmkpolmod(Q_muli_to_int(gel(x,2), d), RgX_copy(gel(x,1)));
    2297          21 :     case t_RFRAC:
    2298          21 :       return gmul(x, d);
    2299             :   }
    2300           0 :   pari_err_TYPE("Q_muli_to_int",x);
    2301             :   return NULL; /* LCOV_EXCL_LINE */
    2302             : }
    2303             : 
    2304             : static void
    2305    29591451 : rescale_init(GEN c, int *exact, long *emin, GEN *D)
    2306             : {
    2307             :   long e, i;
    2308    29591451 :   switch(typ(c))
    2309             :   {
    2310    20069106 :     case t_REAL:
    2311    20069106 :       *exact = 0;
    2312    20069106 :       if (!signe(c)) return;
    2313    19545893 :       e = expo(c) + 1 - bit_prec(c);
    2314    22062115 :       for (i = lg(c)-1; i > 2; i--, e += BITS_IN_LONG)
    2315    16679182 :         if (c[i]) break;
    2316    19545891 :       e += vals(c[i]); break; /* e[2] != 0 */
    2317     9517764 :     case t_INT:
    2318     9517764 :       if (!signe(c)) return;
    2319     1342007 :       e = expi(c);
    2320     1342015 :       break;
    2321        4545 :     case t_FRAC:
    2322        4545 :       e = expi(gel(c,1)) - expi(gel(c,2));
    2323        4545 :       if (*exact) *D = lcmii(*D, gel(c,2));
    2324        4545 :       break;
    2325          48 :     default:
    2326          48 :       pari_err_TYPE("rescale_to_int",c);
    2327             :       return; /* LCOV_EXCL_LINE */
    2328             :   }
    2329    20892452 :   if (e < *emin) *emin = e;
    2330             : }
    2331             : GEN
    2332     4608725 : RgM_rescale_to_int(GEN x)
    2333             : {
    2334     4608725 :   long lx = lg(x), i,j, hx, emin;
    2335             :   GEN D;
    2336             :   int exact;
    2337             : 
    2338     4608725 :   if (lx == 1) return cgetg(1,t_MAT);
    2339     4608725 :   hx = lgcols(x);
    2340     4608725 :   exact = 1;
    2341     4608725 :   emin = HIGHEXPOBIT;
    2342     4608725 :   D = gen_1;
    2343    15316229 :   for (j = 1; j < lx; j++)
    2344    40106819 :     for (i = 1; i < hx; i++) rescale_init(gcoeff(x,i,j), &exact, &emin, &D);
    2345     4608687 :   if (exact) return D == gen_1 ? x: Q_muli_to_int(x, D);
    2346     4608588 :   return grndtoi(gmul2n(x, -emin), NULL);
    2347             : }
    2348             : GEN
    2349       37478 : RgX_rescale_to_int(GEN x)
    2350             : {
    2351       37478 :   long lx = lg(x), i, emin;
    2352             :   GEN D;
    2353             :   int exact;
    2354       37478 :   if (lx == 2) return gcopy(x); /* rare */
    2355       37478 :   exact = 1;
    2356       37478 :   emin = HIGHEXPOBIT;
    2357       37478 :   D = gen_1;
    2358      229590 :   for (i = 2; i < lx; i++) rescale_init(gel(x,i), &exact, &emin, &D);
    2359       37478 :   if (exact) return D == gen_1 ? x: Q_muli_to_int(x, D);
    2360       36351 :   return grndtoi(gmul2n(x, -emin), NULL);
    2361             : }
    2362             : 
    2363             : /* return x * n/d. x: rational; d,n,result: integral; d,n coprime */
    2364             : static GEN
    2365    11536535 : Q_divmuli_to_int(GEN x, GEN d, GEN n)
    2366             : {
    2367             :   GEN y, xn, xd;
    2368             :   pari_sp av;
    2369             : 
    2370    11536535 :   switch(typ(x))
    2371             :   {
    2372     2904119 :     case t_INT:
    2373     2904119 :       av = avma; y = diviiexact(x,d);
    2374     2904119 :       return gerepileuptoint(av, mulii(y,n));
    2375             : 
    2376     5791802 :     case t_FRAC:
    2377     5791802 :       xn = gel(x,1);
    2378     5791802 :       xd = gel(x,2); av = avma;
    2379     5791802 :       y = mulii(diviiexact(xn, d), diviiexact(n, xd));
    2380     5791802 :       return gerepileuptoint(av, y);
    2381             : 
    2382      451620 :     case t_VEC:
    2383             :     case t_COL:
    2384     4051543 :     case t_MAT: pari_APPLY_same(Q_divmuli_to_int(gel(x,i), d,n));
    2385     7852091 :     case t_POL: pari_APPLY_pol_normalized(Q_divmuli_to_int(gel(x,i), d,n));
    2386             : 
    2387           0 :     case t_RFRAC:
    2388           0 :       av = avma;
    2389           0 :       return gerepileupto(av, gmul(x,mkfrac(n,d)));
    2390             : 
    2391           0 :     case t_POLMOD:
    2392           0 :       retmkpolmod(Q_divmuli_to_int(gel(x,2), d,n), RgX_copy(gel(x,1)));
    2393             :   }
    2394           0 :   pari_err_TYPE("Q_divmuli_to_int",x);
    2395             :   return NULL; /* LCOV_EXCL_LINE */
    2396             : }
    2397             : 
    2398             : /* return x / d. x: rational; d,result: integral. */
    2399             : static GEN
    2400   168765849 : Q_divi_to_int(GEN x, GEN d)
    2401             : {
    2402   168765849 :   switch(typ(x))
    2403             :   {
    2404   143389829 :     case t_INT:
    2405   143389829 :       return diviiexact(x,d);
    2406             : 
    2407    20296032 :     case t_VEC:
    2408             :     case t_COL:
    2409   159383101 :     case t_MAT: pari_APPLY_same(Q_divi_to_int(gel(x,i), d));
    2410    22201936 :     case t_POL: pari_APPLY_pol_normalized(Q_divi_to_int(gel(x,i), d));
    2411             : 
    2412           0 :     case t_RFRAC:
    2413           0 :       return gdiv(x,d);
    2414             : 
    2415        5887 :     case t_POLMOD:
    2416        5887 :       retmkpolmod(Q_divi_to_int(gel(x,2), d), RgX_copy(gel(x,1)));
    2417             :   }
    2418           0 :   pari_err_TYPE("Q_divi_to_int",x);
    2419             :   return NULL; /* LCOV_EXCL_LINE */
    2420             : }
    2421             : /* c t_FRAC */
    2422             : static GEN
    2423    10262355 : Q_divq_to_int(GEN x, GEN c)
    2424             : {
    2425    10262355 :   GEN n = gel(c,1), d = gel(c,2);
    2426    10262355 :   if (is_pm1(n)) {
    2427     7788842 :     GEN y = Q_muli_to_int(x,d);
    2428     7788768 :     if (signe(n) < 0) y = gneg(y);
    2429     7788769 :     return y;
    2430             :   }
    2431     2473514 :   return Q_divmuli_to_int(x, n,d);
    2432             : }
    2433             : 
    2434             : /* return y = x / c, assuming x,c rational, and y integral */
    2435             : GEN
    2436      486813 : Q_div_to_int(GEN x, GEN c)
    2437             : {
    2438      486813 :   switch(typ(c))
    2439             :   {
    2440      485897 :     case t_INT:  return Q_divi_to_int(x, c);
    2441         916 :     case t_FRAC: return Q_divq_to_int(x, c);
    2442             :   }
    2443           0 :   pari_err_TYPE("Q_div_to_int",c);
    2444             :   return NULL; /* LCOV_EXCL_LINE */
    2445             : }
    2446             : /* return y = x * c, assuming x,c rational, and y integral */
    2447             : GEN
    2448           0 : Q_mul_to_int(GEN x, GEN c)
    2449             : {
    2450             :   GEN d, n;
    2451           0 :   switch(typ(c))
    2452             :   {
    2453           0 :     case t_INT: return Q_muli_to_int(x, c);
    2454           0 :     case t_FRAC:
    2455           0 :       n = gel(c,1);
    2456           0 :       d = gel(c,2);
    2457           0 :       return Q_divmuli_to_int(x, d,n);
    2458             :   }
    2459           0 :   pari_err_TYPE("Q_mul_to_int",c);
    2460             :   return NULL; /* LCOV_EXCL_LINE */
    2461             : }
    2462             : 
    2463             : GEN
    2464    84907073 : Q_primitive_part(GEN x, GEN *ptc)
    2465             : {
    2466    84907073 :   pari_sp av = avma;
    2467    84907073 :   GEN c = Q_content_safe(x);
    2468    84905267 :   if (c)
    2469             :   {
    2470    84905334 :     if (typ(c) == t_INT)
    2471             :     {
    2472    74643896 :       if (equali1(c)) { set_avma(av); c = NULL; }
    2473    12507717 :       else if (signe(c)) x = Q_divi_to_int(x, c);
    2474             :     }
    2475    10261438 :     else x = Q_divq_to_int(x, c);
    2476             :   }
    2477    84902388 :   if (ptc) *ptc = c;
    2478    84902388 :   return x;
    2479             : }
    2480             : GEN
    2481     7752341 : Q_primpart(GEN x) { return Q_primitive_part(x, NULL); }
    2482             : GEN
    2483      113431 : vec_Q_primpart(GEN x)
    2484      640605 : { pari_APPLY_same(Q_primpart(gel(x,i))) }
    2485             : GEN
    2486       16212 : row_Q_primpart(GEN M)
    2487       16212 : { return shallowtrans(vec_Q_primpart(shallowtrans(M))); }
    2488             : 
    2489             : /*******************************************************************/
    2490             : /*                                                                 */
    2491             : /*                           SUBRESULTANT                          */
    2492             : /*                                                                 */
    2493             : /*******************************************************************/
    2494             : /* for internal use */
    2495             : GEN
    2496    27728321 : gdivexact(GEN x, GEN y)
    2497             : {
    2498             :   long i,lx;
    2499             :   GEN z;
    2500    27728321 :   if (gequal1(y)) return x;
    2501    27716498 :   if (typ(y) == t_POLMOD) return gmul(x, ginv(y));
    2502    27716400 :   switch(typ(x))
    2503             :   {
    2504    22908575 :     case t_INT:
    2505    22908575 :       if (typ(y)==t_INT) return diviiexact(x,y);
    2506         109 :       if (!signe(x)) return gen_0;
    2507          74 :       break;
    2508        8603 :     case t_INTMOD:
    2509             :     case t_FFELT:
    2510        8603 :     case t_POLMOD: return gmul(x,ginv(y));
    2511     4812214 :     case t_POL:
    2512     4812214 :       switch(typ(y))
    2513             :       {
    2514         714 :         case t_INTMOD:
    2515             :         case t_FFELT:
    2516         714 :         case t_POLMOD: return gmul(x,ginv(y));
    2517      221929 :         case t_POL: { /* not stack-clean */
    2518             :           long v;
    2519      221929 :           if (varn(x)!=varn(y)) break;
    2520      220963 :           v = RgX_valrem(y,&y);
    2521      220963 :           if (v) x = RgX_shift_shallow(x,-v);
    2522      220963 :           if (!degpol(y)) { y = gel(y,2); break; }
    2523      216700 :           return RgX_div(x,y);
    2524             :         }
    2525           0 :         case t_RFRAC:
    2526           0 :           if (varn(gel(y,2)) != varn(x)) break;
    2527           0 :           return gdiv(x, y);
    2528             :       }
    2529     4594800 :       return RgX_Rg_divexact(x, y);
    2530        4946 :     case t_VEC: case t_COL: case t_MAT:
    2531        4946 :       lx = lg(x); z = new_chunk(lx);
    2532       54182 :       for (i=1; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    2533        4946 :       z[0] = x[0]; return z;
    2534             :   }
    2535           0 :   if (DEBUGLEVEL) pari_warn(warner,"missing case in gdivexact");
    2536           0 :   return gdiv(x,y);
    2537             : }
    2538             : 
    2539             : static GEN
    2540     1149763 : init_resultant(GEN x, GEN y)
    2541             : {
    2542     1149763 :   long tx = typ(x), ty = typ(y), vx, vy;
    2543     1149763 :   if (is_scalar_t(tx) || is_scalar_t(ty))
    2544             :   {
    2545          14 :     if (gequal0(x) || gequal0(y)) return gmul(x,y); /* keep type info */
    2546          14 :     if (tx==t_POL) return gpowgs(y, degpol(x));
    2547           0 :     if (ty==t_POL) return gpowgs(x, degpol(y));
    2548           0 :     return gen_1;
    2549             :   }
    2550     1149746 :   if (tx!=t_POL) pari_err_TYPE("resultant",x);
    2551     1149746 :   if (ty!=t_POL) pari_err_TYPE("resultant",y);
    2552     1149746 :   if (!signe(x) || !signe(y)) return gmul(Rg_get_0(x),Rg_get_0(y)); /*type*/
    2553     1149669 :   vx = varn(x);
    2554     1149669 :   vy = varn(y); if (vx == vy) return NULL;
    2555           6 :   return (varncmp(vx,vy) < 0)? gpowgs(y,degpol(x)): gpowgs(x,degpol(y));
    2556             : }
    2557             : 
    2558             : /* x an RgX, y a scalar */
    2559             : static GEN
    2560           7 : scalar_res(GEN x, GEN y, GEN *U, GEN *V)
    2561             : {
    2562           7 :   *V = gpowgs(y,degpol(x)-1);
    2563           7 :   *U = gen_0; return gmul(y, *V);
    2564             : }
    2565             : 
    2566             : /* return 0 if the subresultant chain can be interrupted.
    2567             :  * Set u = NULL if the resultant is 0. */
    2568             : static int
    2569       11832 : subres_step(GEN *u, GEN *v, GEN *g, GEN *h, GEN *uze, GEN *um1, long *signh)
    2570             : {
    2571       11832 :   GEN u0, c, r, q = RgX_pseudodivrem(*u,*v, &r);
    2572             :   long du, dv, dr, degq;
    2573             : 
    2574       11832 :   if (gequal0(leading_coeff(r))) r = RgX_renormalize(r);
    2575       11832 :   dr = lg(r); if (!signe(r)) { *u = NULL; return 0; }
    2576       11587 :   du = degpol(*u);
    2577       11587 :   dv = degpol(*v);
    2578       11587 :   degq = du - dv;
    2579       11587 :   if (*um1 == gen_1)
    2580        6455 :     u0 = gpowgs(gel(*v,dv+2),degq+1);
    2581        5132 :   else if (*um1 == gen_0)
    2582        2318 :     u0 = gen_0;
    2583             :   else /* except in those 2 cases, um1 is an RgX */
    2584        2814 :     u0 = RgX_Rg_mul(*um1, gpowgs(gel(*v,dv+2),degq+1));
    2585             : 
    2586       11587 :   if (*uze == gen_0) /* except in that case, uze is an RgX */
    2587        6455 :     u0 = scalarpol(u0, varn(*u)); /* now an RgX */
    2588             :   else
    2589        5132 :     u0 = gsub(u0, gmul(q,*uze));
    2590             : 
    2591       11587 :   *um1 = *uze;
    2592       11587 :   *uze = u0; /* uze <- lead(v)^(degq + 1) * um1 - q * uze */
    2593             : 
    2594       11587 :   *u = *v; c = *g; *g  = leading_coeff(*u);
    2595       11587 :   switch(degq)
    2596             :   {
    2597        1666 :     case 0: break;
    2598        8059 :     case 1:
    2599        8059 :       c = gmul(*h,c); *h = *g; break;
    2600        1862 :     default:
    2601        1862 :       c = gmul(gpowgs(*h,degq), c);
    2602        1862 :       *h = gdivexact(gpowgs(*g,degq), gpowgs(*h,degq-1));
    2603             :   }
    2604       11587 :   if (typ(c) == t_POLMOD)
    2605             :   {
    2606         904 :     c = ginv(c);
    2607         904 :     *v = RgX_Rg_mul(r,c);
    2608         904 :     *uze = RgX_Rg_mul(*uze,c);
    2609             :   }
    2610             :   else
    2611             :   {
    2612       10683 :     *v  = RgX_Rg_divexact(r,c);
    2613       10683 :     *uze= RgX_Rg_divexact(*uze,c);
    2614             :   }
    2615       11587 :   if (both_odd(du, dv)) *signh = -*signh;
    2616       11587 :   return (dr > 3);
    2617             : }
    2618             : 
    2619             : /* compute U, V s.t Ux + Vy = resultant(x,y) */
    2620             : static GEN
    2621        2388 : subresext_i(GEN x, GEN y, GEN *U, GEN *V)
    2622             : {
    2623             :   pari_sp av, av2;
    2624        2388 :   long dx, dy, du, signh, tx = typ(x), ty = typ(y);
    2625             :   GEN r, z, g, h, p1, cu, cv, u, v, um1, uze, vze;
    2626             : 
    2627        2388 :   if (!is_extscalar_t(tx)) pari_err_TYPE("subresext",x);
    2628        2388 :   if (!is_extscalar_t(ty)) pari_err_TYPE("subresext",y);
    2629        2388 :   if (gequal0(x) || gequal0(y)) { *U = *V = gen_0; return gen_0; }
    2630        2388 :   if (tx != t_POL) {
    2631           7 :     if (ty != t_POL) { *U = ginv(x); *V = gen_0; return gen_1; }
    2632           7 :     return scalar_res(y,x,V,U);
    2633             :   }
    2634        2381 :   if (ty != t_POL) return scalar_res(x,y,U,V);
    2635        2381 :   if (varn(x) != varn(y))
    2636           0 :     return varncmp(varn(x), varn(y)) < 0? scalar_res(x,y,U,V)
    2637           0 :                                         : scalar_res(y,x,V,U);
    2638        2381 :   if (gequal0(leading_coeff(x))) x = RgX_renormalize(x);
    2639        2381 :   if (gequal0(leading_coeff(y))) y = RgX_renormalize(y);
    2640        2381 :   dx = degpol(x);
    2641        2381 :   dy = degpol(y);
    2642        2381 :   signh = 1;
    2643        2381 :   if (dx < dy)
    2644             :   {
    2645         890 :     pswap(U,V); lswap(dx,dy); swap(x,y);
    2646         890 :     if (both_odd(dx, dy)) signh = -signh;
    2647             :   }
    2648        2381 :   if (dy == 0)
    2649             :   {
    2650           0 :     *V = gpowgs(gel(y,2),dx-1);
    2651           0 :     *U = gen_0; return gmul(*V,gel(y,2));
    2652             :   }
    2653        2381 :   av = avma;
    2654        2381 :   u = x = primitive_part(x, &cu);
    2655        2381 :   v = y = primitive_part(y, &cv);
    2656        2381 :   g = h = gen_1; av2 = avma;
    2657        2381 :   um1 = gen_1; uze = gen_0;
    2658             :   for(;;)
    2659             :   {
    2660        7037 :     if (!subres_step(&u, &v, &g, &h, &uze, &um1, &signh)) break;
    2661        4656 :     if (gc_needed(av2,1))
    2662             :     {
    2663           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"subresext, dr = %ld", degpol(v));
    2664           0 :       gerepileall(av2,6, &u,&v,&g,&h,&uze,&um1);
    2665             :     }
    2666             :   }
    2667             :   /* uze an RgX */
    2668        2381 :   if (!u) { *U = *V = gen_0; return gc_const(av, gen_0); }
    2669        2374 :   z = gel(v,2); du = degpol(u);
    2670        2374 :   if (du > 1)
    2671             :   { /* z = gdivexact(gpowgs(z,du), gpowgs(h,du-1)); */
    2672         252 :     p1 = gpowgs(gdiv(z,h),du-1);
    2673         252 :     z = gmul(z,p1);
    2674         252 :     uze = RgX_Rg_mul(uze, p1);
    2675             :   }
    2676        2374 :   if (signh < 0) { z = gneg_i(z); uze = RgX_neg(uze); }
    2677             : 
    2678        2374 :   vze = RgX_divrem(Rg_RgX_sub(z, RgX_mul(uze,x)), y, &r);
    2679        2374 :   if (signe(r)) pari_warn(warner,"inexact computation in subresext");
    2680             :   /* uze ppart(x) + vze ppart(y) = z = resultant(ppart(x), ppart(y)), */
    2681        2374 :   p1 = gen_1;
    2682        2374 :   if (cu) p1 = gmul(p1, gpowgs(cu,dy));
    2683        2374 :   if (cv) p1 = gmul(p1, gpowgs(cv,dx));
    2684        2374 :   cu = cu? gdiv(p1,cu): p1;
    2685        2374 :   cv = cv? gdiv(p1,cv): p1;
    2686        2374 :   z = gmul(z,p1);
    2687        2374 :   *U = RgX_Rg_mul(uze,cu);
    2688        2374 :   *V = RgX_Rg_mul(vze,cv);
    2689        2374 :   return z;
    2690             : }
    2691             : GEN
    2692           0 : subresext(GEN x, GEN y, GEN *U, GEN *V)
    2693             : {
    2694           0 :   pari_sp av = avma;
    2695           0 :   GEN z = subresext_i(x, y, U, V);
    2696           0 :   return gc_all(av, 3, &z, U, V);
    2697             : }
    2698             : 
    2699             : static GEN
    2700         434 : zero_extgcd(GEN y, GEN *U, GEN *V, long vx)
    2701             : {
    2702         434 :   GEN x=content(y);
    2703         434 :   *U=pol_0(vx); *V = scalarpol(ginv(x), vx); return gmul(y,*V);
    2704             : }
    2705             : 
    2706             : static int
    2707        6440 : must_negate(GEN x)
    2708             : {
    2709        6440 :   GEN t = leading_coeff(x);
    2710        6440 :   switch(typ(t))
    2711             :   {
    2712        4536 :     case t_INT: case t_REAL:
    2713        4536 :       return (signe(t) < 0);
    2714           0 :     case t_FRAC:
    2715           0 :       return (signe(gel(t,1)) < 0);
    2716             :   }
    2717        1904 :   return 0;
    2718             : }
    2719             : 
    2720             : static GEN
    2721         217 : gc_gcdext(pari_sp av, GEN r, GEN *u, GEN *v)
    2722             : {
    2723         217 :   if (!u && !v) return gerepileupto(av, r);
    2724         217 :   if (u  &&  v) return gc_all(av, 3, &r, u, v);
    2725           0 :   return gc_all(av, 2, &r, u ? u: v);
    2726             : }
    2727             : 
    2728             : static GEN
    2729         133 : RgX_extgcd_FpX(GEN x, GEN y, GEN p, GEN *u, GEN *v)
    2730             : {
    2731         133 :   pari_sp av = avma;
    2732         133 :   GEN r = FpX_extgcd(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, u, v);
    2733         133 :   if (u) *u = FpX_to_mod(*u, p);
    2734         133 :   if (v) *v = FpX_to_mod(*v, p);
    2735         133 :   return gc_gcdext(av, FpX_to_mod(r, p), u, v);
    2736             : }
    2737             : 
    2738             : static GEN
    2739           7 : RgX_extgcd_FpXQX(GEN x, GEN y, GEN pol, GEN p, GEN *U, GEN *V)
    2740             : {
    2741           7 :   pari_sp av = avma;
    2742           7 :   GEN r, T = RgX_to_FpX(pol, p);
    2743           7 :   r = FpXQX_extgcd(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p, U, V);
    2744           7 :   return gc_gcdext(av, FpXQX_to_mod(r, T, p), U, V);
    2745             : }
    2746             : 
    2747             : static GEN
    2748        4529 : RgX_extgcd_fast(GEN x, GEN y, GEN *U, GEN *V)
    2749             : {
    2750             :   GEN p, pol;
    2751             :   long pa;
    2752        4529 :   long t = RgX_type(x, &p,&pol,&pa);
    2753        4529 :   switch(t)
    2754             :   {
    2755          21 :     case t_FFELT:  return FFX_extgcd(x, y, pol, U, V);
    2756         133 :     case t_INTMOD: return RgX_extgcd_FpX(x, y, p, U, V);
    2757           7 :     case code(t_POLMOD, t_INTMOD):
    2758           7 :                    return RgX_extgcd_FpXQX(x, y, pol, p, U, V);
    2759        4368 :     default:       return NULL;
    2760             :   }
    2761             : }
    2762             : 
    2763             : /* compute U, V s.t Ux + Vy = GCD(x,y) using subresultant */
    2764             : GEN
    2765        4970 : RgX_extgcd(GEN x, GEN y, GEN *U, GEN *V)
    2766             : {
    2767             :   pari_sp av, av2, tetpil;
    2768             :   long signh; /* junk */
    2769        4970 :   long dx, dy, vx, tx = typ(x), ty = typ(y);
    2770             :   GEN r, z, g, h, p1, cu, cv, u, v, um1, uze, vze, *gptr[3];
    2771             : 
    2772        4970 :   if (tx!=t_POL) pari_err_TYPE("RgX_extgcd",x);
    2773        4970 :   if (ty!=t_POL) pari_err_TYPE("RgX_extgcd",y);
    2774        4970 :   if ( varncmp(varn(x),varn(y))) pari_err_VAR("RgX_extgcd",x,y);
    2775        4970 :   vx=varn(x);
    2776        4970 :   if (!signe(x))
    2777             :   {
    2778          14 :     if (signe(y)) return zero_extgcd(y,U,V,vx);
    2779           7 :     *U = pol_0(vx); *V = pol_0(vx);
    2780           7 :     return pol_0(vx);
    2781             :   }
    2782        4956 :   if (!signe(y)) return zero_extgcd(x,V,U,vx);
    2783        4529 :   r = RgX_extgcd_fast(x, y, U, V);
    2784        4529 :   if (r) return r;
    2785        4368 :   dx = degpol(x); dy = degpol(y);
    2786        4368 :   if (dx < dy) { pswap(U,V); lswap(dx,dy); swap(x,y); }
    2787        4368 :   if (dy==0) { *U=pol_0(vx); *V=ginv(y); return pol_1(vx); }
    2788             : 
    2789        4179 :   av = avma;
    2790        4179 :   u = x = primitive_part(x, &cu);
    2791        4179 :   v = y = primitive_part(y, &cv);
    2792        4179 :   g = h = gen_1; av2 = avma;
    2793        4179 :   um1 = gen_1; uze = gen_0;
    2794             :   for(;;)
    2795             :   {
    2796        4389 :     if (!subres_step(&u, &v, &g, &h, &uze, &um1, &signh)) break;
    2797         210 :     if (gc_needed(av2,1))
    2798             :     {
    2799           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_extgcd, dr = %ld",degpol(v));
    2800           0 :       gerepileall(av2,6,&u,&v,&g,&h,&uze,&um1);
    2801             :     }
    2802             :   }
    2803        4179 :   if (uze != gen_0) {
    2804             :     GEN r;
    2805        3969 :     vze = RgX_divrem(RgX_sub(v, RgX_mul(uze,x)), y, &r);
    2806        3969 :     if (signe(r)) pari_warn(warner,"inexact computation in RgX_extgcd");
    2807        3969 :     if (cu) uze = RgX_Rg_div(uze,cu);
    2808        3969 :     if (cv) vze = RgX_Rg_div(vze,cv);
    2809        3969 :     p1 = ginv(content(v));
    2810             :   }
    2811             :   else /* y | x */
    2812             :   {
    2813         210 :     vze = cv ? RgX_Rg_div(pol_1(vx),cv): pol_1(vx);
    2814         210 :     uze = pol_0(vx);
    2815         210 :     p1 = gen_1;
    2816             :   }
    2817        4179 :   if (must_negate(v)) p1 = gneg(p1);
    2818        4179 :   tetpil = avma;
    2819        4179 :   z = RgX_Rg_mul(v,p1);
    2820        4179 :   *U = RgX_Rg_mul(uze,p1);
    2821        4179 :   *V = RgX_Rg_mul(vze,p1);
    2822        4179 :   gptr[0] = &z;
    2823        4179 :   gptr[1] = U;
    2824        4179 :   gptr[2] = V;
    2825        4179 :   gerepilemanysp(av,tetpil,gptr,3); return z;
    2826             : }
    2827             : 
    2828             : static GEN
    2829          14 : RgX_halfgcd_all_i(GEN a, GEN b, GEN *pa, GEN *pb)
    2830             : {
    2831          14 :   pari_sp av=avma;
    2832          14 :   long m = degpol(a), va = varn(a);
    2833             :   GEN R, u,u1,v,v1;
    2834          14 :   u1 = v = pol_0(va);
    2835          14 :   u = v1 = pol_1(va);
    2836          14 :   if (degpol(a)<degpol(b))
    2837             :   {
    2838           0 :     swap(a,b);
    2839           0 :     swap(u,v); swap(u1,v1);
    2840             :   }
    2841          42 :   while (2*degpol(b) >= m)
    2842             :   {
    2843          28 :     GEN r, q = RgX_pseudodivrem(a,b,&r);
    2844          28 :     GEN l = gpowgs(leading_coeff(b), degpol(a)-degpol(b)+1);
    2845          28 :     GEN g = ggcd(l, content(r));
    2846          28 :     q = RgX_Rg_div(q, g);
    2847          28 :     r = RgX_Rg_div(r, g);
    2848          28 :     l = gdiv(l, g);
    2849          28 :     a = b; b = r; swap(u,v); swap(u1,v1);
    2850          28 :     v  = RgX_sub(gmul(l,v),  RgX_mul(u, q));
    2851          28 :     v1 = RgX_sub(gmul(l,v1), RgX_mul(u1, q));
    2852          28 :     if (gc_needed(av,2))
    2853             :     {
    2854           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"halfgcd (d = %ld)",degpol(b));
    2855           0 :       gerepileall(av,6, &a,&b,&u1,&v1,&u,&v);
    2856             :     }
    2857             :   }
    2858          14 :   if (pa) *pa = a;
    2859          14 :   if (pb) *pb = b;
    2860          14 :   R = mkmat22(u,u1,v,v1);
    2861          14 :   return !pa && pb ? gc_all(av, 2, &R, pb): gc_all(av, 1+!!pa+!!pb, &R, pa, pb);
    2862             : }
    2863             : 
    2864             : static GEN
    2865          28 : RgX_halfgcd_all_FpX(GEN x, GEN y, GEN p, GEN *a, GEN *b)
    2866             : {
    2867          28 :   pari_sp av = avma;
    2868             :   GEN M;
    2869          28 :   if (lgefint(p) == 3)
    2870             :   {
    2871          14 :     ulong pp = uel(p, 2);
    2872          14 :     GEN xp = RgX_to_Flx(x, pp), yp = RgX_to_Flx(y, pp);
    2873          14 :     M = Flx_halfgcd_all(xp, yp, pp, a, b);
    2874          14 :     M = FlxM_to_ZXM(M); *a = Flx_to_ZX(*a); *b = Flx_to_ZX(*b);
    2875             :   }
    2876             :   else
    2877             :   {
    2878          14 :     x = RgX_to_FpX(x, p); y = RgX_to_FpX(y, p);
    2879          14 :     M = FpX_halfgcd_all(x, y, p, a, b);
    2880             :   }
    2881          28 :   return !a && b ? gc_all(av, 2, &M, b): gc_all(av, 1+!!a+!!b, &M, a, b);
    2882             : }
    2883             : 
    2884             : static GEN
    2885           0 : RgX_halfgcd_all_FpXQX(GEN x, GEN y, GEN pol, GEN p, GEN *a, GEN *b)
    2886             : {
    2887           0 :   pari_sp av = avma;
    2888           0 :   GEN M, T = RgX_to_FpX(pol, p);
    2889           0 :   if (signe(T)==0) pari_err_OP("halfgcd", x, y);
    2890           0 :   x = RgX_to_FpXQX(x, T, p); y = RgX_to_FpXQX(y, T, p);
    2891           0 :   M = FpXQX_halfgcd_all(x, y, T, p, a, b);
    2892           0 :   if (a) *a = FqX_to_mod(*a, T, p);
    2893           0 :   if (b) *b = FqX_to_mod(*b, T, p);
    2894           0 :   M = FqXM_to_mod(M, T, p);
    2895           0 :   return !a && b ? gc_all(av, 2, &M, b): gc_all(av, 1+!!a+!!b, &M, a, b);
    2896             : }
    2897             : 
    2898             : static GEN
    2899          63 : RgX_halfgcd_all_fast(GEN x, GEN y, GEN *a, GEN *b)
    2900             : {
    2901             :   GEN p, pol;
    2902             :   long pa;
    2903          63 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    2904          63 :   switch(t)
    2905             :   {
    2906          21 :     case t_FFELT:  return FFX_halfgcd_all(x, y, pol, a, b);
    2907          28 :     case t_INTMOD: return RgX_halfgcd_all_FpX(x, y, p, a, b);
    2908           0 :     case code(t_POLMOD, t_INTMOD):
    2909           0 :                    return RgX_halfgcd_all_FpXQX(x, y, pol, p, a, b);
    2910          14 :     default:       return NULL;
    2911             :   }
    2912             : }
    2913             : 
    2914             : GEN
    2915          63 : RgX_halfgcd_all(GEN x, GEN y, GEN *a, GEN *b)
    2916             : {
    2917          63 :   GEN z = RgX_halfgcd_all_fast(x, y, a, b);
    2918          63 :   if (z) return z;
    2919          14 :   return RgX_halfgcd_all_i(x, y, a, b);
    2920             : }
    2921             : 
    2922             : GEN
    2923           0 : RgX_halfgcd(GEN x, GEN y)
    2924           0 : { return  RgX_halfgcd_all(x, y, NULL, NULL); }
    2925             : 
    2926             : int
    2927         112 : RgXQ_ratlift(GEN x, GEN T, long amax, long bmax, GEN *P, GEN *Q)
    2928             : {
    2929         112 :   pari_sp av = avma, av2, tetpil;
    2930             :   long signh; /* junk */
    2931             :   long vx;
    2932             :   GEN g, h, p1, cu, cv, u, v, um1, uze, *gptr[2];
    2933             : 
    2934         112 :   if (typ(x)!=t_POL) pari_err_TYPE("RgXQ_ratlift",x);
    2935         112 :   if (typ(T)!=t_POL) pari_err_TYPE("RgXQ_ratlift",T);
    2936         112 :   if ( varncmp(varn(x),varn(T)) ) pari_err_VAR("RgXQ_ratlift",x,T);
    2937         112 :   if (bmax < 0) pari_err_DOMAIN("ratlift", "bmax", "<", gen_0, stoi(bmax));
    2938         112 :   if (!signe(T)) {
    2939           0 :     if (degpol(x) <= amax) {
    2940           0 :       *P = RgX_copy(x);
    2941           0 :       *Q = pol_1(varn(x));
    2942           0 :       return 1;
    2943             :     }
    2944           0 :     return 0;
    2945             :   }
    2946         112 :   if (amax+bmax >= degpol(T))
    2947           0 :     pari_err_DOMAIN("ratlift", "amax+bmax", ">=", stoi(degpol(T)),
    2948             :                     mkvec3(stoi(amax), stoi(bmax), T));
    2949         112 :   vx = varn(T);
    2950         112 :   u = x = primitive_part(x, &cu);
    2951         112 :   v = T = primitive_part(T, &cv);
    2952         112 :   g = h = gen_1; av2 = avma;
    2953         112 :   um1 = gen_1; uze = gen_0;
    2954             :   for(;;)
    2955             :   {
    2956         406 :     (void) subres_step(&u, &v, &g, &h, &uze, &um1, &signh);
    2957         406 :     if (!u || (typ(uze)==t_POL && degpol(uze)>bmax)) return gc_bool(av,0);
    2958         406 :     if (typ(v)!=t_POL || degpol(v)<=amax) break;
    2959         294 :     if (gc_needed(av2,1))
    2960             :     {
    2961           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXQ_ratlift, dr = %ld", degpol(v));
    2962           0 :       gerepileall(av2,6,&u,&v,&g,&h,&uze,&um1);
    2963             :     }
    2964             :   }
    2965         112 :   if (uze == gen_0)
    2966             :   {
    2967           0 :     set_avma(av); *P = pol_0(vx); *Q = pol_1(vx);
    2968           0 :     return 1;
    2969             :   }
    2970         112 :   if (cu) uze = RgX_Rg_div(uze,cu);
    2971         112 :   p1 = ginv(content(v));
    2972         112 :   if (must_negate(v)) p1 = gneg(p1);
    2973         112 :   tetpil = avma;
    2974         112 :   *P = RgX_Rg_mul(v,p1);
    2975         112 :   *Q = RgX_Rg_mul(uze,p1);
    2976         112 :   gptr[0] = P;
    2977         112 :   gptr[1] = Q;
    2978         112 :   gerepilemanysp(av,tetpil,gptr,2); return 1;
    2979             : }
    2980             : 
    2981             : GEN
    2982           0 : RgX_chinese_coprime(GEN x, GEN y, GEN Tx, GEN Ty, GEN Tz)
    2983             : {
    2984           0 :   pari_sp av = avma;
    2985           0 :   GEN ax = RgX_mul(RgXQ_inv(Tx,Ty), Tx);
    2986           0 :   GEN p1 = RgX_mul(ax, RgX_sub(y,x));
    2987           0 :   p1 = RgX_add(x,p1);
    2988           0 :   if (!Tz) Tz = RgX_mul(Tx,Ty);
    2989           0 :   p1 = RgX_rem(p1, Tz);
    2990           0 :   return gerepileupto(av,p1);
    2991             : }
    2992             : 
    2993             : /*******************************************************************/
    2994             : /*                                                                 */
    2995             : /*                RESULTANT USING DUCOS VARIANT                    */
    2996             : /*                                                                 */
    2997             : /*******************************************************************/
    2998             : /* x^n / y^(n-1), assume n > 0 */
    2999             : static GEN
    3000      628742 : Lazard(GEN x, GEN y, long n)
    3001             : {
    3002             :   long a;
    3003             :   GEN c;
    3004             : 
    3005      628742 :   if (n == 1) return x;
    3006       10004 :   a = 1 << expu(n); /* a = 2^k <= n < 2^(k+1) */
    3007       10004 :   c=x; n-=a;
    3008       20631 :   while (a>1)
    3009             :   {
    3010       10627 :     a>>=1; c=gdivexact(gsqr(c),y);
    3011       10627 :     if (n>=a) { c=gdivexact(gmul(c,x),y); n -= a; }
    3012             :   }
    3013       10004 :   return c;
    3014             : }
    3015             : 
    3016             : /* F (x/y)^(n-1), assume n >= 1 */
    3017             : static GEN
    3018      832592 : Lazard2(GEN F, GEN x, GEN y, long n)
    3019             : {
    3020      832592 :   if (n == 1) return F;
    3021        4836 :   return RgX_Rg_divexact(RgX_Rg_mul(F, Lazard(x,y,n-1)), y);
    3022             : }
    3023             : 
    3024             : static GEN
    3025      832591 : RgX_neg_i(GEN x, long lx)
    3026             : {
    3027             :   long i;
    3028      832591 :   GEN y = cgetg(lx, t_POL); y[1] = x[1];
    3029     2177263 :   for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
    3030      832591 :   return y;
    3031             : }
    3032             : static GEN
    3033     2445529 : RgX_Rg_mul_i(GEN y, GEN x, long ly)
    3034             : {
    3035             :   long i;
    3036             :   GEN z;
    3037     2445529 :   if (isrationalzero(x)) return pol_0(varn(y));
    3038     2445251 :   z = cgetg(ly,t_POL); z[1] = y[1];
    3039     6412104 :   for (i = 2; i < ly; i++) gel(z,i) = gmul(x,gel(y,i));
    3040     2445214 :   return z;
    3041             : }
    3042             : static long
    3043     2440475 : reductum_lg(GEN x, long lx)
    3044             : {
    3045     2440475 :   long i = lx-2;
    3046     2512640 :   while (i > 1 && gequal0(gel(x,i))) i--;
    3047     2440479 :   return i+1;
    3048             : }
    3049             : 
    3050             : #define addshift(x,y) RgX_addmulXn_shallow((x),(y),1)
    3051             : /* delta = deg(P) - deg(Q) > 0, deg(Q) > 0, P,Q,Z t_POL in the same variable,
    3052             :  * s "scalar". Return prem(P, -Q) / s^delta lc(P) */
    3053             : static GEN
    3054      832593 : nextSousResultant(GEN P, GEN Q, GEN Z, GEN s)
    3055             : {
    3056      832593 :   GEN p0, q0, h0, TMP, H, A, z0 = leading_coeff(Z);
    3057             :   long p, q, j, lP, lQ;
    3058             :   pari_sp av;
    3059             : 
    3060      832593 :   p = degpol(P); p0 = gel(P,p+2); lP = reductum_lg(P,lg(P));
    3061      832593 :   q = degpol(Q); q0 = gel(Q,q+2); lQ = reductum_lg(Q,lg(Q));
    3062             :   /* p > q. Very often p - 1 = q */
    3063      832591 :   av = avma;
    3064             :   /* H = RgX_neg(reductum(Z)) optimized, using Q ~ Z */
    3065      832591 :   H = RgX_neg_i(Z, lQ); /* deg H < q */
    3066             : 
    3067      832592 :   A = (q+2 < lP)? RgX_Rg_mul_i(H, gel(P,q+2), lQ): NULL;
    3068      841134 :   for (j = q+1; j < p; j++)
    3069             :   {
    3070        8551 :     if (degpol(H) == q-1)
    3071             :     { /* h0 = coeff of degree q-1 = leading coeff */
    3072        7347 :       h0 = gel(H,q+1); (void)normalizepol_lg(H, q+1);
    3073        7347 :       H = addshift(H, RgX_Rg_divexact(RgX_Rg_mul_i(Q, gneg(h0), lQ), q0));
    3074             :     }
    3075             :     else
    3076        1204 :       H = RgX_shift_shallow(H, 1);
    3077        8551 :     if (j+2 < lP)
    3078             :     {
    3079        6545 :       TMP = RgX_Rg_mul(H, gel(P,j+2));
    3080        6545 :       A = A? RgX_add(A, TMP): TMP;
    3081             :     }
    3082        8551 :     if (gc_needed(av,1))
    3083             :     {
    3084         147 :       if(DEBUGMEM>1) pari_warn(warnmem,"nextSousResultant j = %ld/%ld",j,p);
    3085         147 :       gerepileall(av,A?2:1,&H,&A);
    3086             :     }
    3087             :   }
    3088      832583 :   if (q+2 < lP) lP = reductum_lg(P, q+3);
    3089      832585 :   TMP = RgX_Rg_mul_i(P, z0, lP);
    3090      832588 :   A = A? RgX_add(A, TMP): TMP;
    3091      832588 :   A = RgX_Rg_divexact(A, p0);
    3092      832586 :   if (degpol(H) == q-1)
    3093             :   {
    3094      830296 :     h0 = gel(H,q+1); (void)normalizepol_lg(H, q+1); /* destroy old H */
    3095      830302 :     A = RgX_add(RgX_Rg_mul(addshift(H,A),q0), RgX_Rg_mul_i(Q, gneg(h0), lQ));
    3096             :   }
    3097             :   else
    3098        2290 :     A = RgX_Rg_mul(addshift(H,A), q0);
    3099      832587 :   return RgX_Rg_divexact(A, s);
    3100             : }
    3101             : #undef addshift
    3102             : 
    3103             : static GEN
    3104     1247820 : RgX_pseudodenom(GEN x)
    3105             : {
    3106     1247820 :   GEN m = NULL;
    3107     1247820 :   long l = lg(x), i;
    3108     6187263 :   for (i = 2; i < l; i++)
    3109             :   {
    3110     4939443 :     GEN xi = gel(x, i);
    3111     4939443 :     if (typ(xi) == t_RFRAC)
    3112             :     {
    3113          42 :       GEN d = denom_i(xi);
    3114          42 :       if (!m || signe(RgX_pseudorem(m, d)))
    3115          42 :         m = m ? gmul(m, d): d;
    3116             :     }
    3117             :   }
    3118     1247820 :   return m;
    3119             : }
    3120             : 
    3121             : /* Ducos's subresultant */
    3122             : GEN
    3123     1064647 : RgX_resultant_all(GEN P, GEN Q, GEN *sol)
    3124             : {
    3125             :   pari_sp av, av2;
    3126     1064647 :   long dP, dQ, delta, sig = 1;
    3127             :   GEN DP, DQ, cP, cQ, Z, s;
    3128             : 
    3129     1064647 :   dP = degpol(P);
    3130     1064647 :   dQ = degpol(Q); delta = dP - dQ;
    3131     1064646 :   if (delta < 0)
    3132             :   {
    3133         903 :     if (both_odd(dP, dQ)) sig = -1;
    3134         903 :     swap(P,Q); lswap(dP, dQ); delta = -delta;
    3135             :   }
    3136     1064646 :   if (sol) *sol = gen_0;
    3137     1064646 :   av = avma;
    3138     1064646 :   if (dQ <= 0)
    3139             :   {
    3140         854 :     if (dQ < 0) return Rg_get_0(P);
    3141         854 :     s = gpowgs(gel(Q,2), dP);
    3142         854 :     if (sig == -1) s = gerepileupto(av, gneg(s));
    3143         854 :     return s;
    3144             :   }
    3145     1063792 :   if (dQ == 1)
    3146             :   {
    3147      439884 :     if (sol) *sol = Q;
    3148      439884 :     s = RgX_homogenous_evalpow(P, gel(Q,2), gpowers(gneg(gel(Q,3)), dP));
    3149      439878 :     if (sig==-1) s = gneg(s);
    3150      439878 :     return gc_all(av, sol ? 2: 1, &s, sol);
    3151             :   }
    3152             :   /* primitive_part is also possible here, but possibly very costly,
    3153             :    * and hardly ever worth it */
    3154             : 
    3155      623908 :   DP = RgX_pseudodenom(P); if (DP) P = gmul(P,DP);
    3156      623910 :   DQ = RgX_pseudodenom(Q); if (DQ) Q = gmul(Q,DQ);
    3157      623910 :   P = Q_primitive_part(P, &cP);
    3158      623909 :   Q = Q_primitive_part(Q, &cQ);
    3159      623909 :   av2 = avma;
    3160      623909 :   s = gpowgs(leading_coeff(Q),delta);
    3161      623910 :   if (both_odd(dP, dQ)) sig = -sig;
    3162      623910 :   Z = Q;
    3163      623910 :   Q = RgX_pseudorem(P, Q);
    3164      623910 :   P = Z;
    3165     1456497 :   while(degpol(Q) > 0)
    3166             :   {
    3167      832585 :     delta = degpol(P) - degpol(Q); /* > 0 */
    3168      832592 :     Z = Lazard2(Q, leading_coeff(Q), s, delta);
    3169      832592 :     if (both_odd(degpol(P), degpol(Q))) sig = -sig;
    3170      832592 :     Q = nextSousResultant(P, Q, Z, s);
    3171      832588 :     P = Z;
    3172      832588 :     if (gc_needed(av,1))
    3173             :     {
    3174          13 :       if(DEBUGMEM>1) pari_warn(warnmem,"resultant_all, degpol Q = %ld",degpol(Q));
    3175          13 :       gerepileall(av2,2,&P,&Q);
    3176             :     }
    3177      832588 :     s = leading_coeff(P);
    3178             :   }
    3179      623903 :   if (!signe(Q)) { set_avma(av); return Rg_get_0(Q); }
    3180      623903 :   s = Lazard(leading_coeff(Q), s, degpol(P));
    3181      623906 :   if (sig == -1) s = gneg(s);
    3182      623906 :   if (DP) s = gdiv(s, gpowgs(DP,dQ));
    3183      623906 :   if (DQ) s = gdiv(s, gpowgs(DQ,dP));
    3184      623906 :   if (cP) s = gmul(s, gpowgs(cP,dQ));
    3185      623906 :   if (cQ) s = gmul(s, gpowgs(cQ,dP));
    3186      623908 :   if (!sol) return gerepilecopy(av, s);
    3187        2387 :   *sol = P; return gc_all(av, 2, &s, sol);
    3188             : }
    3189             : 
    3190             : static GEN
    3191          28 : RgX_resultant_FpX(GEN x, GEN y, GEN p)
    3192             : {
    3193          28 :   pari_sp av = avma;
    3194             :   GEN r;
    3195          28 :   if (lgefint(p) == 3)
    3196             :   {
    3197          14 :     ulong pp = uel(p, 2);
    3198          14 :     r = utoi(Flx_resultant(RgX_to_Flx(x, pp), RgX_to_Flx(y, pp), pp));
    3199             :   }
    3200             :   else
    3201          14 :     r = FpX_resultant(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    3202          28 :   return gerepileupto(av, Fp_to_mod(r, p));
    3203             : }
    3204             : 
    3205             : static GEN
    3206          21 : RgX_resultant_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    3207             : {
    3208          21 :   pari_sp av = avma;
    3209          21 :   GEN r, T = RgX_to_FpX(pol, p);
    3210          21 :   r = FpXQX_resultant(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    3211          21 :   return gerepileupto(av, FpX_to_mod(r, p));
    3212             : }
    3213             : 
    3214             : static GEN
    3215     1149741 : resultant_fast(GEN x, GEN y)
    3216             : {
    3217             :   GEN p, pol;
    3218             :   long pa, t;
    3219     1149741 :   p = init_resultant(x,y);
    3220     1149740 :   if (p) return p;
    3221     1149642 :   t = RgX_type2(x,y, &p,&pol,&pa);
    3222     1149651 :   switch(t)
    3223             :   {
    3224        2688 :     case t_INT:    return ZX_resultant(x,y);
    3225          56 :     case t_FRAC:   return QX_resultant(x,y);
    3226          21 :     case t_FFELT:  return FFX_resultant(x,y,pol);
    3227          28 :     case t_INTMOD: return RgX_resultant_FpX(x, y, p);
    3228          21 :     case code(t_POLMOD, t_INTMOD):
    3229          21 :                    return RgX_resultant_FpXQX(x, y, pol, p);
    3230     1146837 :     default:       return NULL;
    3231             :   }
    3232             : }
    3233             : 
    3234             : static GEN
    3235      169886 : RgX_resultant_sylvester(GEN x, GEN y)
    3236             : {
    3237      169886 :   pari_sp av = avma;
    3238      169886 :   return gerepileupto(av, det(RgX_sylvestermatrix(x,y)));
    3239             : }
    3240             : 
    3241             : /* Return resultant(P,Q).
    3242             :  * Uses Sylvester's matrix if P or Q inexact, a modular algorithm if they
    3243             :  * are in Q[X], and Ducos/Lazard optimization of the subresultant algorithm
    3244             :  * in the "generic" case. */
    3245             : GEN
    3246     1149741 : resultant(GEN P, GEN Q)
    3247             : {
    3248     1149741 :   GEN z = resultant_fast(P,Q);
    3249     1149749 :   if (z) return z;
    3250     1146837 :   if (isinexact(P) || isinexact(Q)) return RgX_resultant_sylvester(P,Q);
    3251      976973 :   return RgX_resultant_all(P, Q, NULL);
    3252             : }
    3253             : 
    3254             : /*******************************************************************/
    3255             : /*                                                                 */
    3256             : /*               RESULTANT USING SYLVESTER MATRIX                  */
    3257             : /*                                                                 */
    3258             : /*******************************************************************/
    3259             : static GEN
    3260      371720 : syl_RgC(GEN x, long j, long d, long D, long cp)
    3261             : {
    3262      371720 :   GEN c = cgetg(d+1,t_COL);
    3263             :   long i;
    3264      990262 :   for (i=1; i< j; i++) gel(c,i) = gen_0;
    3265     2142578 :   for (   ; i<=D; i++) { GEN t = gel(x,D-i+2); gel(c,i) = cp? gcopy(t): t; }
    3266      990262 :   for (   ; i<=d; i++) gel(c,i) = gen_0;
    3267      371720 :   return c;
    3268             : }
    3269             : static GEN
    3270      169893 : syl_RgM(GEN x, GEN y, long cp)
    3271             : {
    3272      169893 :   long j, d, dx = degpol(x), dy = degpol(y);
    3273             :   GEN M;
    3274      169893 :   if (dx < 0) return dy < 0? cgetg(1,t_MAT): zeromat(dy,dy);
    3275      169893 :   if (dy < 0) return zeromat(dx,dx);
    3276      169893 :   d = dx+dy; M = cgetg(d+1,t_MAT);
    3277      442039 :   for (j=1; j<=dy; j++) gel(M,j)    = syl_RgC(x,j,d,j+dx, cp);
    3278      269467 :   for (j=1; j<=dx; j++) gel(M,j+dy) = syl_RgC(y,j,d,j+dy, cp);
    3279      169893 :   return M;
    3280             : }
    3281             : GEN
    3282      169886 : RgX_sylvestermatrix(GEN x, GEN y) { return syl_RgM(x,y,0); }
    3283             : GEN
    3284           7 : sylvestermatrix(GEN x, GEN y)
    3285             : {
    3286           7 :   if (typ(x)!=t_POL) pari_err_TYPE("sylvestermatrix",x);
    3287           7 :   if (typ(y)!=t_POL) pari_err_TYPE("sylvestermatrix",y);
    3288           7 :   if (varn(x) != varn(y)) pari_err_VAR("sylvestermatrix",x,y);
    3289           7 :   return syl_RgM(x,y,1);
    3290             : }
    3291             : 
    3292             : GEN
    3293          21 : resultant2(GEN x, GEN y)
    3294             : {
    3295          21 :   GEN r = init_resultant(x,y);
    3296          21 :   return r? r: RgX_resultant_sylvester(x,y);
    3297             : }
    3298             : 
    3299             : /* let vx = main variable of x, v0 a variable of highest priority;
    3300             :  * return a t_POL in variable v0:
    3301             :  * if vx <= v, return subst(x, v, pol_x(v0))
    3302             :  * if vx >  v, return scalarpol(x, v0) */
    3303             : static GEN
    3304         343 : fix_pol(GEN x, long v, long v0)
    3305             : {
    3306         343 :   long vx, tx = typ(x);
    3307         343 :   if (tx != t_POL)
    3308          42 :     vx = gvar(x);
    3309             :   else
    3310             :   { /* shortcut: almost nothing to do */
    3311         301 :     vx = varn(x);
    3312         301 :     if (v == vx)
    3313             :     {
    3314         119 :       if (v0 != v) { x = leafcopy(x); setvarn(x, v0); }
    3315         119 :       return x;
    3316             :     }
    3317             :   }
    3318         224 :   if (varncmp(v, vx) > 0)
    3319             :   {
    3320         217 :     x = gsubst(x, v, pol_x(v0));
    3321         217 :     if (typ(x) != t_POL) vx = gvar(x);
    3322             :     else
    3323             :     {
    3324         210 :       vx = varn(x);
    3325         210 :       if (vx == v0) return x;
    3326             :     }
    3327             :   }
    3328          49 :   if (varncmp(vx, v0) <= 0) pari_err_TYPE("polresultant", x);
    3329          42 :   return scalarpol_shallow(x, v0);
    3330             : }
    3331             : 
    3332             : /* resultant of x and y with respect to variable v, or with respect to their
    3333             :  * main variable if v < 0. */
    3334             : GEN
    3335         504 : polresultant0(GEN x, GEN y, long v, long flag)
    3336             : {
    3337         504 :   pari_sp av = avma;
    3338             : 
    3339         504 :   if (v >= 0)
    3340             :   {
    3341         147 :     long v0 = fetch_var_higher();
    3342         147 :     x = fix_pol(x,v, v0);
    3343         147 :     y = fix_pol(y,v, v0);
    3344             :   }
    3345         504 :   switch(flag)
    3346             :   {
    3347         497 :     case 2:
    3348         497 :     case 0: x=resultant(x,y); break;
    3349           7 :     case 1: x=resultant2(x,y); break;
    3350           0 :     default: pari_err_FLAG("polresultant");
    3351             :   }
    3352         504 :   if (v >= 0) (void)delete_var();
    3353         504 :   return gerepileupto(av,x);
    3354             : }
    3355             : 
    3356             : static GEN
    3357          77 : RgX_extresultant_FpX(GEN x, GEN y, GEN p, GEN *u, GEN *v)
    3358             : {
    3359          77 :   pari_sp av = avma;
    3360          77 :   GEN r = FpX_extresultant(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, u, v);
    3361          77 :   if (signe(r) == 0) { *u = gen_0; *v = gen_0; return gc_const(av, gen_0); }
    3362          77 :   if (u) *u = FpX_to_mod(*u, p);
    3363          77 :   if (v) *v = FpX_to_mod(*v, p);
    3364          77 :   return gc_gcdext(av, Fp_to_mod(r, p), u, v);
    3365             : }
    3366             : 
    3367             : static GEN
    3368        1568 : RgX_extresultant_fast(GEN x, GEN y, GEN *U, GEN *V)
    3369             : {
    3370             :   GEN p, pol;
    3371             :   long pa;
    3372        1568 :   long t = RgX_type2(x, y, &p,&pol,&pa);
    3373        1568 :   switch(t)
    3374             :   {
    3375          77 :     case t_INTMOD: return RgX_extresultant_FpX(x, y, p, U, V);
    3376        1491 :     default:       return NULL;
    3377             :   }
    3378             : }
    3379             : 
    3380             : GEN
    3381        1575 : polresultantext0(GEN x, GEN y, long v)
    3382             : {
    3383        1575 :   GEN R = NULL, U, V;
    3384        1575 :   pari_sp av = avma;
    3385             : 
    3386        1575 :   if (v >= 0)
    3387             :   {
    3388          14 :     long v0 = fetch_var_higher();
    3389          14 :     x = fix_pol(x,v, v0);
    3390          14 :     y = fix_pol(y,v, v0);
    3391             :   }
    3392        1575 :   if (typ(x)==t_POL && typ(y)==t_POL)
    3393        1568 :     R = RgX_extresultant_fast(x, y, &U, &V);
    3394        1575 :   if (!R)
    3395        1498 :     R = subresext_i(x,y, &U,&V);
    3396        1575 :   if (v >= 0)
    3397             :   {
    3398          14 :     (void)delete_var();
    3399          14 :     if (typ(U) == t_POL && varn(U) != v) U = poleval(U, pol_x(v));
    3400          14 :     if (typ(V) == t_POL && varn(V) != v) V = poleval(V, pol_x(v));
    3401             :   }
    3402        1575 :   return gerepilecopy(av, mkvec3(U,V,R));
    3403             : }
    3404             : GEN
    3405        1463 : polresultantext(GEN x, GEN y) { return polresultantext0(x,y,-1); }
    3406             : 
    3407             : /*******************************************************************/
    3408             : /*                                                                 */
    3409             : /*             CHARACTERISTIC POLYNOMIAL USING RESULTANT           */
    3410             : /*                                                                 */
    3411             : /*******************************************************************/
    3412             : 
    3413             : static GEN
    3414          14 : RgXQ_charpoly_FpXQ(GEN x, GEN T, GEN p, long v)
    3415             : {
    3416          14 :   pari_sp av = avma;
    3417             :   GEN r;
    3418          14 :   if (lgefint(p)==3)
    3419             :   {
    3420           0 :     ulong pp = p[2];
    3421           0 :     r = Flx_to_ZX(Flxq_charpoly(RgX_to_Flx(x, pp), RgX_to_Flx(T, pp), pp));
    3422             :   }
    3423             :   else
    3424          14 :     r = FpXQ_charpoly(RgX_to_FpX(x, p), RgX_to_FpX(T, p), p);
    3425          14 :   r = FpX_to_mod(r, p); setvarn(r, v);
    3426          14 :   return gerepileupto(av, r);
    3427             : }
    3428             : 
    3429             : static GEN
    3430       12970 : RgXQ_charpoly_fast(GEN x, GEN T, long v)
    3431             : {
    3432             :   GEN p, pol;
    3433       12970 :   long pa, t = RgX_type2(x,T, &p,&pol,&pa);
    3434       12970 :   switch(t)
    3435             :   {
    3436        9413 :     case t_INT:    return ZXQ_charpoly(x, T, v);
    3437        2171 :     case t_FRAC:
    3438             :     {
    3439        2171 :       pari_sp av = avma;
    3440             :       GEN cT;
    3441        2171 :       T = Q_primitive_part(T, &cT);
    3442        2171 :       T = QXQ_charpoly(x, T, v);
    3443        2171 :       if (cT) T = gerepileupto(av, T); /* silly rare case */
    3444        2171 :       return T;
    3445             :     }
    3446          14 :     case t_INTMOD: return RgXQ_charpoly_FpXQ(x, T, p, v);
    3447        1372 :     default:       return NULL;
    3448             :   }
    3449             : }
    3450             : 
    3451             : /* (v - x)^d */
    3452             : static GEN
    3453         126 : caract_const(pari_sp av, GEN x, long v, long d)
    3454         126 : { return gerepileupto(av, gpowgs(deg1pol_shallow(gen_1, gneg_i(x), v), d)); }
    3455             : 
    3456             : GEN
    3457      975392 : RgXQ_charpoly_i(GEN x, GEN T, long v)
    3458             : {
    3459      975392 :   pari_sp av = avma;
    3460      975392 :   long d = degpol(T), dx = degpol(x), v0;
    3461             :   GEN ch, L;
    3462      975392 :   if (dx >= degpol(T)) { x = RgX_rem(x, T); dx = degpol(x); }
    3463      975392 :   if (dx <= 0) return dx? pol_xn(d, v): caract_const(av, gel(x,2), v, d);
    3464             : 
    3465      975322 :   v0 = fetch_var_higher();
    3466      975322 :   x = RgX_neg(x);
    3467      975318 :   gel(x,2) = gadd(gel(x,2), pol_x(v));
    3468      975311 :   setvarn(x, v0);
    3469      975311 :   T = leafcopy(T); setvarn(T, v0);
    3470      975312 :   ch = resultant(T, x);
    3471      975322 :   (void)delete_var();
    3472             :   /* test for silly input: x mod (deg 0 polynomial) */
    3473      975322 :   if (typ(ch) != t_POL)
    3474           7 :     pari_err_PRIORITY("RgXQ_charpoly", pol_x(v), "<", gvar(ch));
    3475      975315 :   L = leading_coeff(ch);
    3476      975315 :   if (!gequal1(L)) ch = RgX_Rg_div(ch, L);
    3477      975315 :   return gerepileupto(av, ch);
    3478             : }
    3479             : 
    3480             : /* return caract(Mod(x,T)) in variable v */
    3481             : GEN
    3482       12970 : RgXQ_charpoly(GEN x, GEN T, long v)
    3483             : {
    3484       12970 :   GEN ch = RgXQ_charpoly_fast(x, T, v);
    3485       12970 :   if (ch) return ch;
    3486        1372 :   return RgXQ_charpoly_i(x, T, v);
    3487             : }
    3488             : 
    3489             : /* characteristic polynomial (in v) of x over nf, where x is an element of the
    3490             :  * algebra nf[t]/(Q(t)) */
    3491             : GEN
    3492         224 : rnfcharpoly(GEN nf, GEN Q, GEN x, long v)
    3493             : {
    3494         224 :   const char *f = "rnfcharpoly";
    3495         224 :   long dQ = degpol(Q);
    3496         224 :   pari_sp av = avma;
    3497             :   GEN T;
    3498             : 
    3499         224 :   if (v < 0) v = 0;
    3500         224 :   nf = checknf(nf); T = nf_get_pol(nf);
    3501         224 :   Q = RgX_nffix(f, T,Q,0);
    3502         224 :   switch(typ(x))
    3503             :   {
    3504          28 :     case t_INT:
    3505          28 :     case t_FRAC: return caract_const(av, x, v, dQ);
    3506          91 :     case t_POLMOD:
    3507          91 :       x = polmod_nffix2(f,T,Q, x,0);
    3508          56 :       break;
    3509          56 :     case t_POL:
    3510          56 :       x = varn(x) == varn(T)? Rg_nffix(f,T,x,0): RgX_nffix(f, T,x,0);
    3511          42 :       break;
    3512          49 :     default: pari_err_TYPE(f,x);
    3513             :   }
    3514          98 :   if (typ(x) != t_POL) return caract_const(av, x, v, dQ);
    3515             :   /* x a t_POL in variable vQ */
    3516          56 :   if (degpol(x) >= dQ) x = RgX_rem(x, Q);
    3517          56 :   if (dQ <= 1) return caract_const(av, constant_coeff(x), v, 1);
    3518          56 :   return gerepilecopy(av, lift_if_rational( RgXQ_charpoly(x, Q, v) ));
    3519             : }
    3520             : 
    3521             : /*******************************************************************/
    3522             : /*                                                                 */
    3523             : /*                  GCD USING SUBRESULTANT                         */
    3524             : /*                                                                 */
    3525             : /*******************************************************************/
    3526             : static int inexact(GEN x, int *simple);
    3527             : static int
    3528       38367 : isinexactall(GEN x, int *simple)
    3529             : {
    3530       38367 :   long i, lx = lg(x);
    3531      139020 :   for (i=2; i<lx; i++)
    3532      100667 :     if (inexact(gel(x,i), simple)) return 1;
    3533       38353 :   return 0;
    3534             : }
    3535             : /* return 1 if coeff explosion is not possible */
    3536             : static int
    3537      100919 : inexact(GEN x, int *simple)
    3538             : {
    3539      100919 :   int junk = 0;
    3540      100919 :   switch(typ(x))
    3541             :   {
    3542       64729 :     case t_INT: case t_FRAC: return 0;
    3543             : 
    3544           7 :     case t_REAL: case t_PADIC: case t_SER: return 1;
    3545             : 
    3546        4011 :     case t_INTMOD:
    3547             :     case t_FFELT:
    3548        4011 :       if (!*simple) *simple = 1;
    3549        4011 :       return 0;
    3550             : 
    3551          77 :     case t_COMPLEX:
    3552          77 :       return inexact(gel(x,1), simple)
    3553          77 :           || inexact(gel(x,2), simple);
    3554           0 :     case t_QUAD:
    3555           0 :       *simple = 0;
    3556           0 :       return inexact(gel(x,2), &junk)
    3557           0 :           || inexact(gel(x,3), &junk);
    3558             : 
    3559         819 :     case t_POLMOD:
    3560         819 :       return isinexactall(gel(x,1), simple);
    3561       31227 :     case t_POL:
    3562       31227 :       *simple = -1;
    3563       31227 :       return isinexactall(x, &junk);
    3564          49 :     case t_RFRAC:
    3565          49 :       *simple = -1;
    3566          49 :       return inexact(gel(x,1), &junk)
    3567          49 :           || inexact(gel(x,2), &junk);
    3568             :   }
    3569           0 :   *simple = -1; return 0;
    3570             : }
    3571             : 
    3572             : /* x monomial, y t_POL in the same variable */
    3573             : static GEN
    3574     1706300 : gcdmonome(GEN x, GEN y)
    3575             : {
    3576     1706300 :   pari_sp av = avma;
    3577     1706300 :   long dx = degpol(x), e = RgX_valrem(y, &y);
    3578     1706300 :   long i, l = lg(y);
    3579     1706300 :   GEN t, v = cgetg(l, t_VEC);
    3580     1706300 :   gel(v,1) = gel(x,dx+2);
    3581     3425882 :   for (i = 2; i < l; i++) gel(v,i) = gel(y,i);
    3582     1706300 :   t = content(v); /* gcd(lc(x), cont(y)) */
    3583     1706300 :   t = simplify_shallow(t);
    3584     1706300 :   if (dx < e) e = dx;
    3585     1706300 :   return gerepileupto(av, monomialcopy(t, e, varn(x)));
    3586             : }
    3587             : 
    3588             : static GEN
    3589      112462 : RgX_gcd_FpX(GEN x, GEN y, GEN p)
    3590             : {
    3591      112462 :   pari_sp av = avma;
    3592             :   GEN r;
    3593      112462 :   if (lgefint(p) == 3)
    3594             :   {
    3595      112448 :     ulong pp = uel(p, 2);
    3596      112448 :     r = Flx_to_ZX_inplace(Flx_gcd(RgX_to_Flx(x, pp),
    3597             :                                   RgX_to_Flx(y, pp), pp));
    3598             :   }
    3599             :   else
    3600          14 :     r = FpX_gcd(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    3601      112462 :   return gerepileupto(av, FpX_to_mod(r, p));
    3602             : }
    3603             : 
    3604             : static GEN
    3605           7 : RgX_gcd_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    3606             : {
    3607           7 :   pari_sp av = avma;
    3608           7 :   GEN r, T = RgX_to_FpX(pol, p);
    3609           7 :   if (signe(T)==0) pari_err_OP("gcd", x, y);
    3610           7 :   r = FpXQX_gcd(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    3611           7 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    3612             : }
    3613             : 
    3614             : static GEN
    3615       10878 : RgX_liftred(GEN x, GEN T)
    3616       10878 : { return RgXQX_red(liftpol_shallow(x), T); }
    3617             : 
    3618             : static GEN
    3619        2289 : RgX_gcd_ZXQX(GEN x, GEN y, GEN T)
    3620             : {
    3621        2289 :   pari_sp av = avma;
    3622        2289 :   GEN r = ZXQX_gcd(RgX_liftred(x, T), RgX_liftred(y, T), T);
    3623        2289 :   return gerepilecopy(av, QXQX_to_mod_shallow(r, T));
    3624             : }
    3625             : 
    3626             : static GEN
    3627        3150 : RgX_gcd_QXQX(GEN x, GEN y, GEN T)
    3628             : {
    3629        3150 :   pari_sp av = avma;
    3630        3150 :   GEN r = QXQX_gcd(RgX_liftred(x, T), RgX_liftred(y, T), T);
    3631        3150 :   return gerepilecopy(av, QXQX_to_mod_shallow(r, T));
    3632             : }
    3633             : 
    3634             : static GEN
    3635    11289530 : RgX_gcd_fast(GEN x, GEN y)
    3636             : {
    3637             :   GEN p, pol;
    3638             :   long pa;
    3639    11289530 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    3640    11289530 :   switch(t)
    3641             :   {
    3642     9451763 :     case t_INT:    return ZX_gcd(x, y);
    3643        7707 :     case t_FRAC:   return QX_gcd(x, y);
    3644        2590 :     case t_FFELT:  return FFX_gcd(x, y, pol);
    3645      112462 :     case t_INTMOD: return RgX_gcd_FpX(x, y, p);
    3646           7 :     case code(t_POLMOD, t_INTMOD):
    3647           7 :                    return RgX_gcd_FpXQX(x, y, pol, p);
    3648        2296 :     case code(t_POLMOD, t_INT):
    3649        2296 :                    return ZX_is_monic(pol)? RgX_gcd_ZXQX(x,y,pol): NULL;
    3650        3164 :     case code(t_POLMOD, t_FRAC):
    3651        6328 :                    return RgX_is_ZX(pol) && ZX_is_monic(pol) ?
    3652        6328 :                                             RgX_gcd_QXQX(x,y,pol): NULL;
    3653     1709541 :     default:       return NULL;
    3654             :   }
    3655             : }
    3656             : 
    3657             : /* x, y are t_POL in the same variable */
    3658             : GEN
    3659    11289530 : RgX_gcd(GEN x, GEN y)
    3660             : {
    3661             :   long dx, dy;
    3662             :   pari_sp av, av1;
    3663             :   GEN d, g, h, p1, p2, u, v;
    3664    11289530 :   int simple = 0;
    3665    11289530 :   GEN z = RgX_gcd_fast(x, y);
    3666    11289530 :   if (z) return z;
    3667     1709562 :   if (isexactzero(y)) return RgX_copy(x);
    3668     1709464 :   if (isexactzero(x)) return RgX_copy(y);
    3669     1709464 :   if (RgX_is_monomial(x)) return gcdmonome(x,y);
    3670        4298 :   if (RgX_is_monomial(y)) return gcdmonome(y,x);
    3671        3164 :   if (isinexactall(x,&simple) || isinexactall(y,&simple))
    3672             :   {
    3673           7 :     av = avma; u = ggcd(content(x), content(y));
    3674           7 :     return gerepileupto(av, scalarpol(u, varn(x)));
    3675             :   }
    3676             : 
    3677        3157 :   av = avma;
    3678        3157 :   if (simple > 0) x = RgX_gcd_simple(x,y);
    3679             :   else
    3680             :   {
    3681        3157 :     dx = lg(x); dy = lg(y);
    3682        3157 :     if (dx < dy) { swap(x,y); lswap(dx,dy); }
    3683        3157 :     if (dy==3)
    3684             :     {
    3685           0 :       d = ggcd(gel(y,2), content(x));
    3686           0 :       return gerepileupto(av, scalarpol(d, varn(x)));
    3687             :     }
    3688        3157 :     u = primitive_part(x, &p1); if (!p1) p1 = gen_1;
    3689        3157 :     v = primitive_part(y, &p2); if (!p2) p2 = gen_1;
    3690        3157 :     d = ggcd(p1,p2);
    3691        3157 :     av1 = avma;
    3692        3157 :     g = h = gen_1;
    3693             :     for(;;)
    3694        1197 :     {
    3695        4354 :       GEN r = RgX_pseudorem(u,v);
    3696        4354 :       long degq, du, dv, dr = lg(r);
    3697             : 
    3698        4354 :       if (!signe(r)) break;
    3699        2205 :       if (dr <= 3)
    3700             :       {
    3701        1008 :         set_avma(av1);
    3702        1008 :         return gerepileupto(av, scalarpol(d, varn(x)));
    3703             :       }
    3704        1197 :       du = lg(u); dv = lg(v); degq = du-dv;
    3705        1197 :       u = v; p1 = g; g = leading_coeff(u);
    3706        1197 :       switch(degq)
    3707             :       {
    3708         189 :         case 0: break;
    3709         917 :         case 1:
    3710         917 :           p1 = gmul(h,p1); h = g; break;
    3711          91 :         default:
    3712          91 :           p1 = gmul(gpowgs(h,degq), p1);
    3713          91 :           h = gdiv(gpowgs(g,degq), gpowgs(h,degq-1));
    3714             :       }
    3715        1197 :       v = RgX_Rg_div(r,p1);
    3716        1197 :       if (gc_needed(av1,1))
    3717             :       {
    3718           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"RgX_gcd, dr = %ld", degpol(r));
    3719           0 :         gerepileall(av1,4, &u,&v,&g,&h);
    3720             :       }
    3721             :     }
    3722        2149 :     x = RgX_Rg_mul(primpart(v), d);
    3723             :   }
    3724        2149 :   if (must_negate(x)) x = RgX_neg(x);
    3725        2149 :   return gerepileupto(av,x);
    3726             : }
    3727             : 
    3728             : /* disc P = (-1)^(n(n-1)/2) lc(P)^(n - deg P' - 2) Res(P,P'), n = deg P */
    3729             : static GEN
    3730         392 : RgX_disc_i(GEN P)
    3731             : {
    3732         392 :   long n = degpol(P), dd;
    3733             :   GEN N, D, L, y;
    3734         392 :   if (!signe(P) || !n) return Rg_get_0(P);
    3735         385 :   if (n == 1) return Rg_get_1(P);
    3736         385 :   if (n == 2) {
    3737         126 :     GEN a = gel(P,4), b = gel(P,3), c = gel(P,2);
    3738         126 :     return gsub(gsqr(b), gmul2n(gmul(a,c),2));
    3739             :   }
    3740         259 :   y = RgX_deriv(P);
    3741         259 :   N = characteristic(P);
    3742         259 :   if (signe(N)) y = gmul(y, mkintmod(gen_1,N));
    3743         259 :   if (!signe(y)) return Rg_get_0(y);
    3744         259 :   dd = n - 2 - degpol(y);
    3745         259 :   if (isinexact(P))
    3746          14 :     D = resultant2(P,y);
    3747             :   else
    3748             :   {
    3749         245 :     D = RgX_resultant_all(P, y, NULL);
    3750         245 :     if (D == gen_0) return Rg_get_0(y);
    3751             :   }
    3752         259 :   L = leading_coeff(P);
    3753         259 :   if (dd && !gequal1(L)) D = (dd == -1)? gdiv(D, L): gmul(D, gpowgs(L, dd));
    3754         259 :   if (n & 2) D = gneg(D);
    3755         259 :   return D;
    3756             : }
    3757             : 
    3758             : static GEN
    3759          42 : RgX_disc_FpX(GEN x, GEN p)
    3760             : {
    3761          42 :   pari_sp av = avma;
    3762          42 :   GEN r = FpX_disc(RgX_to_FpX(x, p), p);
    3763          42 :   return gerepileupto(av, Fp_to_mod(r, p));
    3764             : }
    3765             : 
    3766             : static GEN
    3767          28 : RgX_disc_FpXQX(GEN x, GEN pol, GEN p)
    3768             : {
    3769          28 :   pari_sp av = avma;
    3770          28 :   GEN r, T = RgX_to_FpX(pol, p);
    3771          28 :   r = FpXQX_disc(RgX_to_FpXQX(x, T, p), T, p);
    3772          28 :   return gerepileupto(av, FpX_to_mod(r, p));
    3773             : }
    3774             : 
    3775             : static GEN
    3776      123251 : RgX_disc_fast(GEN x)
    3777             : {
    3778             :   GEN p, pol;
    3779             :   long pa;
    3780      123251 :   long t = RgX_type(x, &p,&pol,&pa);
    3781      123251 :   switch(t)
    3782             :   {
    3783      122747 :     case t_INT:    return ZX_disc(x);
    3784           7 :     case t_FRAC:   return QX_disc(x);
    3785          35 :     case t_FFELT:  return FFX_disc(x, pol);
    3786          42 :     case t_INTMOD: return RgX_disc_FpX(x, p);
    3787          28 :     case code(t_POLMOD, t_INTMOD):
    3788          28 :                    return RgX_disc_FpXQX(x, pol, p);
    3789         392 :     default:       return NULL;
    3790             :   }
    3791             : }
    3792             : 
    3793             : GEN
    3794      123251 : RgX_disc(GEN x)
    3795             : {
    3796             :   pari_sp av;
    3797      123251 :   GEN z = RgX_disc_fast(x);
    3798      123251 :   if (z) return z;
    3799         392 :   av = avma;
    3800         392 :   return gerepileupto(av, RgX_disc_i(x));
    3801             : }
    3802             : 
    3803             : GEN
    3804        4714 : poldisc0(GEN x, long v)
    3805             : {
    3806        4714 :   long v0, tx = typ(x);
    3807             :   pari_sp av;
    3808             :   GEN D;
    3809        4714 :   if (tx == t_POL && (v < 0 || v == varn(x))) return RgX_disc(x);
    3810          35 :   switch(tx)
    3811             :   {
    3812           0 :     case t_QUAD:
    3813           0 :       return quad_disc(x);
    3814           0 :     case t_POLMOD:
    3815           0 :       if (v >= 0 && varn(gel(x,1)) != v) break;
    3816           0 :       return RgX_disc(gel(x,1));
    3817          14 :     case t_QFB:
    3818          14 :       return icopy(qfb_disc(x));
    3819           0 :     case t_VEC: case t_COL: case t_MAT:
    3820           0 :       pari_APPLY_same(poldisc0(gel(x,i), v));
    3821             :   }
    3822          21 :   if (v < 0) pari_err_TYPE("poldisc",x);
    3823          21 :   av = avma; v0 = fetch_var_higher();
    3824          21 :   x = fix_pol(x,v, v0);
    3825          14 :   D = RgX_disc(x); (void)delete_var();
    3826          14 :   return gerepileupto(av, D);
    3827             : }
    3828             : 
    3829             : GEN
    3830           7 : reduceddiscsmith(GEN x)
    3831             : {
    3832           7 :   long j, n = degpol(x);
    3833           7 :   pari_sp av = avma;
    3834             :   GEN xp, M;
    3835             : 
    3836           7 :   if (typ(x) != t_POL) pari_err_TYPE("poldiscreduced",x);
    3837           7 :   if (n<=0) pari_err_CONSTPOL("poldiscreduced");
    3838           7 :   RgX_check_ZX(x,"poldiscreduced");
    3839           7 :   if (!gequal1(gel(x,n+2)))
    3840           0 :     pari_err_IMPL("nonmonic polynomial in poldiscreduced");
    3841           7 :   M = cgetg(n+1,t_MAT);
    3842           7 :   xp = ZX_deriv(x);
    3843          28 :   for (j=1; j<=n; j++)
    3844             :   {
    3845          21 :     gel(M,j) = RgX_to_RgC(xp, n);
    3846          21 :     if (j<n) xp = RgX_rem(RgX_shift_shallow(xp, 1), x);
    3847             :   }
    3848           7 :   return gerepileupto(av, ZM_snf(M));
    3849             : }
    3850             : 
    3851             : /***********************************************************************/
    3852             : /**                                                                   **/
    3853             : /**                       STURM ALGORITHM                             **/
    3854             : /**              (number of real roots of x in [a,b])                 **/
    3855             : /**                                                                   **/
    3856             : /***********************************************************************/
    3857             : static GEN
    3858         525 : R_to_Q_up(GEN x)
    3859             : {
    3860             :   long e;
    3861         525 :   switch(typ(x))
    3862             :   {
    3863         525 :     case t_INT: case t_FRAC: case t_INFINITY: return x;
    3864           0 :     case t_REAL:
    3865           0 :       x = mantissa_real(x,&e);
    3866           0 :       return gmul2n(addiu(x,1), -e);
    3867           0 :     default: pari_err_TYPE("R_to_Q_up", x);
    3868             :       return NULL; /* LCOV_EXCL_LINE */
    3869             :   }
    3870             : }
    3871             : static GEN
    3872         525 : R_to_Q_down(GEN x)
    3873             : {
    3874             :   long e;
    3875         525 :   switch(typ(x))
    3876             :   {
    3877         525 :     case t_INT: case t_FRAC: case t_INFINITY: return x;
    3878           0 :     case t_REAL:
    3879           0 :       x = mantissa_real(x,&e);
    3880           0 :       return gmul2n(subiu(x,1), -e);
    3881           0 :     default: pari_err_TYPE("R_to_Q_down", x);
    3882             :       return NULL; /* LCOV_EXCL_LINE */
    3883             :   }
    3884             : }
    3885             : 
    3886             : static long
    3887        1148 : sturmpart_i(GEN x, GEN ab)
    3888             : {
    3889        1148 :   long tx = typ(x);
    3890        1148 :   if (gequal0(x)) pari_err_ROOTS0("sturm");
    3891        1148 :   if (tx != t_POL)
    3892             :   {
    3893           0 :     if (is_real_t(tx)) return 0;
    3894           0 :     pari_err_TYPE("sturm",x);
    3895             :   }
    3896        1148 :   if (lg(x) == 3) return 0;
    3897        1148 :   if (!RgX_is_ZX(x)) x = RgX_rescale_to_int(x);
    3898        1148 :   (void)ZX_gcd_all(x, ZX_deriv(x), &x);
    3899        1148 :   if (ab)
    3900             :   {
    3901             :     GEN A, B;
    3902         525 :     if (typ(ab) != t_VEC || lg(ab) != 3) pari_err_TYPE("RgX_sturmpart", ab);
    3903         525 :     A = R_to_Q_down(gel(ab,1));
    3904         525 :     B = R_to_Q_up(gel(ab,2));
    3905         525 :     ab = mkvec2(A, B);
    3906             :   }
    3907        1148 :   return ZX_sturmpart(x, ab);
    3908             : }
    3909             : /* Deprecated: RgX_sturmpart() should be preferred  */
    3910             : long
    3911         385 : sturmpart(GEN x, GEN a, GEN b)
    3912             : {
    3913         385 :   pari_sp av = avma;
    3914         385 :   if (!b && a && typ(a) == t_VEC) return RgX_sturmpart(x, a);
    3915         385 :   if (!a) a = mkmoo();
    3916         385 :   if (!b) b = mkoo();
    3917         385 :   return gc_long(av, sturmpart_i(x, mkvec2(a,b)));
    3918             : }
    3919             : long
    3920         763 : RgX_sturmpart(GEN x, GEN ab)
    3921         763 : { pari_sp av = avma; return gc_long(av, sturmpart_i(x, ab)); }
    3922             : 
    3923             : /***********************************************************************/
    3924             : /**                                                                   **/
    3925             : /**                        GENERIC EXTENDED GCD                       **/
    3926             : /**                                                                   **/
    3927             : /***********************************************************************/
    3928             : /* assume typ(x) = typ(y) = t_POL */
    3929             : static GEN
    3930         890 : RgXQ_inv_i(GEN x, GEN y)
    3931             : {
    3932         890 :   long vx=varn(x), vy=varn(y);
    3933             :   pari_sp av;
    3934             :   GEN u, v, d;
    3935             : 
    3936         890 :   while (vx != vy)
    3937             :   {
    3938           0 :     if (varncmp(vx,vy) > 0)
    3939             :     {
    3940           0 :       d = (vx == NO_VARIABLE)? ginv(x): gred_rfrac_simple(gen_1, x);
    3941           0 :       return scalarpol(d, vy);
    3942             :     }
    3943           0 :     if (lg(x)!=3) pari_err_INV("RgXQ_inv",mkpolmod(x,y));
    3944           0 :     x = gel(x,2); vx = gvar(x);
    3945             :   }
    3946         890 :   av = avma; d = subresext_i(x,y,&u,&v/*junk*/);
    3947         890 :   if (gequal0(d)) pari_err_INV("RgXQ_inv",mkpolmod(x,y));
    3948         890 :   d = gdiv(u,d);
    3949         890 :   if (typ(d) != t_POL || varn(d) != vy) d = scalarpol(d, vy);
    3950         890 :   return gerepileupto(av, d);
    3951             : }
    3952             : 
    3953             : /*Assume x is a polynomial and y is not */
    3954             : static GEN
    3955         112 : scalar_bezout(GEN x, GEN y, GEN *U, GEN *V)
    3956             : {
    3957         112 :   long vx = varn(x);
    3958         112 :   int xis0 = signe(x)==0, yis0 = gequal0(y);
    3959         112 :   if (xis0 && yis0) { *U = *V = pol_0(vx); return pol_0(vx); }
    3960          84 :   if (yis0) { *U=pol_1(vx); *V = pol_0(vx); return RgX_copy(x);}
    3961          56 :   *U=pol_0(vx); *V= ginv(y); return pol_1(vx);
    3962             : }
    3963             : /* Assume x==0, y!=0 */
    3964             : static GEN
    3965          63 : zero_bezout(GEN y, GEN *U, GEN *V)
    3966             : {
    3967          63 :   *U=gen_0; *V = ginv(y); return gen_1;
    3968             : }
    3969             : 
    3970             : GEN
    3971         427 : gbezout(GEN x, GEN y, GEN *u, GEN *v)
    3972             : {
    3973         427 :   long tx=typ(x), ty=typ(y), vx;
    3974         427 :   if (tx == t_INT && ty == t_INT) return bezout(x,y,u,v);
    3975         392 :   if (tx != t_POL)
    3976             :   {
    3977         140 :     if (ty == t_POL)
    3978          56 :       return scalar_bezout(y,x,v,u);
    3979             :     else
    3980             :     {
    3981          84 :       int xis0 = gequal0(x), yis0 = gequal0(y);
    3982          84 :       if (xis0 && yis0) { *u = *v = gen_0; return gen_0; }
    3983          63 :       if (xis0) return zero_bezout(y,u,v);
    3984          42 :       else return zero_bezout(x,v,u);
    3985             :     }
    3986             :   }
    3987         252 :   else if (ty != t_POL) return scalar_bezout(x,y,u,v);
    3988         196 :   vx = varn(x);
    3989         196 :   if (vx != varn(y))
    3990           0 :     return varncmp(vx, varn(y)) < 0? scalar_bezout(x,y,u,v)
    3991           0 :                                    : scalar_bezout(y,x,v,u);
    3992         196 :   return RgX_extgcd(x,y,u,v);
    3993             : }
    3994             : 
    3995             : GEN
    3996         427 : gcdext0(GEN x, GEN y)
    3997             : {
    3998         427 :   GEN z=cgetg(4,t_VEC);
    3999         427 :   gel(z,3) = gbezout(x,y,(GEN*)(z+1),(GEN*)(z+2));
    4000         427 :   return z;
    4001             : }
    4002             : 
    4003             : /*******************************************************************/
    4004             : /*                                                                 */
    4005             : /*                    GENERIC (modular) INVERSE                    */
    4006             : /*                                                                 */
    4007             : /*******************************************************************/
    4008             : 
    4009             : GEN
    4010       35166 : ginvmod(GEN x, GEN y)
    4011             : {
    4012       35166 :   long tx=typ(x);
    4013             : 
    4014       35166 :   switch(typ(y))
    4015             :   {
    4016       35168 :     case t_POL:
    4017       35168 :       if (tx==t_POL) return RgXQ_inv(x,y);
    4018       13734 :       if (is_scalar_t(tx)) return ginv(x);
    4019           0 :       break;
    4020           0 :     case t_INT:
    4021           0 :       if (tx==t_INT) return Fp_inv(x,y);
    4022           0 :       if (tx==t_POL) return gen_0;
    4023             :   }
    4024           0 :   pari_err_TYPE2("ginvmod",x,y);
    4025             :   return NULL; /* LCOV_EXCL_LINE */
    4026             : }
    4027             : 
    4028             : /***********************************************************************/
    4029             : /**                                                                   **/
    4030             : /**                         NEWTON POLYGON                            **/
    4031             : /**                                                                   **/
    4032             : /***********************************************************************/
    4033             : 
    4034             : /* assume leading coeff of x is nonzero */
    4035             : GEN
    4036          28 : newtonpoly(GEN x, GEN p)
    4037             : {
    4038          28 :   pari_sp av = avma;
    4039             :   long n, ind, a, b;
    4040             :   GEN y, vval;
    4041             : 
    4042          28 :   if (typ(x) != t_POL) pari_err_TYPE("newtonpoly",x);
    4043          28 :   n = degpol(x); if (n<=0) return cgetg(1,t_VEC);
    4044          28 :   vval = new_chunk(n+1);
    4045          28 :   y = cgetg(n+1,t_VEC); x += 2; /* now x[i] = term of degree i */
    4046         168 :   for (a = 0; a <= n; a++) vval[a] = gvaluation(gel(x,a),p);
    4047          42 :   for (a = 0, ind = 1; a < n; a++)
    4048             :   {
    4049          42 :     if (vval[a] != LONG_MAX) break;
    4050          14 :     gel(y,ind++) = mkoo();
    4051             :   }
    4052          84 :   for (b = a+1; b <= n; a = b, b = a+1)
    4053             :   {
    4054             :     long u1, u2, c;
    4055          70 :     while (vval[b] == LONG_MAX) b++;
    4056          56 :     u1 = vval[a] - vval[b];
    4057          56 :     u2 = b - a;
    4058         154 :     for (c = b+1; c <= n; c++)
    4059             :     {
    4060             :       long r1, r2;
    4061          98 :       if (vval[c] == LONG_MAX) continue;
    4062          70 :       r1 = vval[a] - vval[c];
    4063          70 :       r2 = c - a;
    4064          70 :       if (u1*r2 <= u2*r1) { u1 = r1; u2 = r2; b = c; }
    4065             :     }
    4066         154 :     while (ind <= b) gel(y,ind++) = sstoQ(u1,u2);
    4067             :   }
    4068          28 :   stackdummy((pari_sp)vval, av); return y;
    4069             : }
    4070             : 
    4071             : static GEN
    4072      274309 : RgXQ_mul_FpXQ(GEN x, GEN y, GEN T, GEN p)
    4073             : {
    4074      274309 :   pari_sp av = avma;
    4075             :   GEN r;
    4076      274309 :   if (lgefint(p) == 3)
    4077             :   {
    4078      152402 :     ulong pp = uel(p, 2);
    4079      152402 :     r = Flx_to_ZX_inplace(Flxq_mul(RgX_to_Flx(x, pp),
    4080             :                 RgX_to_Flx(y, pp), RgX_to_Flx(T, pp), pp));
    4081             :   }
    4082             :   else
    4083      121907 :     r = FpXQ_mul(RgX_to_FpX(x, p), RgX_to_FpX(y, p), RgX_to_FpX(T, p), p);
    4084      274309 :   return gerepileupto(av, FpX_to_mod(r, p));
    4085             : }
    4086             : 
    4087             : static GEN
    4088          14 : RgXQ_sqr_FpXQ(GEN x, GEN y, GEN p)
    4089             : {
    4090          14 :   pari_sp av = avma;
    4091             :   GEN r;
    4092          14 :   if (lgefint(p) == 3)
    4093             :   {
    4094           7 :     ulong pp = uel(p, 2);
    4095           7 :     r = Flx_to_ZX_inplace(Flxq_sqr(RgX_to_Flx(x, pp),
    4096             :                                    RgX_to_Flx(y, pp), pp));
    4097             :   }
    4098             :   else
    4099           7 :     r = FpXQ_sqr(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    4100          14 :   return gerepileupto(av, FpX_to_mod(r, p));
    4101             : }
    4102             : 
    4103             : static GEN
    4104       12054 : RgXQ_inv_FpXQ(GEN x, GEN y, GEN p)
    4105             : {
    4106       12054 :   pari_sp av = avma;
    4107             :   GEN r;
    4108       12054 :   if (lgefint(p) == 3)
    4109             :   {
    4110        6088 :     ulong pp = uel(p, 2);
    4111        6088 :     r = Flx_to_ZX_inplace(Flxq_inv(RgX_to_Flx(x, pp),
    4112             :                                    RgX_to_Flx(y, pp), pp));
    4113             :   }
    4114             :   else
    4115        5966 :     r = FpXQ_inv(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    4116       12054 :   return gerepileupto(av, FpX_to_mod(r, p));
    4117             : }
    4118             : 
    4119             : static GEN
    4120         385 : RgXQ_mul_FpXQXQ(GEN x, GEN y, GEN S, GEN pol, GEN p)
    4121             : {
    4122         385 :   pari_sp av = avma;
    4123             :   GEN r;
    4124         385 :   GEN T = RgX_to_FpX(pol, p);
    4125         385 :   if (signe(T)==0) pari_err_OP("*",x,y);
    4126         385 :   if (lgefint(p) == 3)
    4127             :   {
    4128         241 :     ulong pp = uel(p, 2);
    4129         241 :     GEN Tp = ZX_to_Flx(T, pp);
    4130         241 :     r = FlxX_to_ZXX(FlxqXQ_mul(RgX_to_FlxqX(x, Tp, pp),
    4131             :                                RgX_to_FlxqX(y, Tp, pp),
    4132             :                                RgX_to_FlxqX(S, Tp, pp), Tp, pp));
    4133             :   }
    4134             :   else
    4135         144 :     r = FpXQXQ_mul(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p),
    4136             :                    RgX_to_FpXQX(S, T, p), T, p);
    4137         385 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    4138             : }
    4139             : 
    4140             : static GEN
    4141           0 : RgXQ_sqr_FpXQXQ(GEN x, GEN y, GEN pol, GEN p)
    4142             : {
    4143           0 :   pari_sp av = avma;
    4144             :   GEN r;
    4145           0 :   GEN T = RgX_to_FpX(pol, p);
    4146           0 :   if (signe(T)==0) pari_err_OP("*",x,x);
    4147           0 :   if (lgefint(p) == 3)
    4148             :   {
    4149           0 :     ulong pp = uel(p, 2);
    4150           0 :     GEN Tp = ZX_to_Flx(T, pp);
    4151           0 :     r = FlxX_to_ZXX(FlxqXQ_sqr(RgX_to_FlxqX(x, Tp, pp),
    4152             :                                RgX_to_FlxqX(y, Tp, pp), Tp, pp));
    4153             :   }
    4154             :   else
    4155           0 :     r = FpXQXQ_sqr(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    4156           0 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    4157             : }
    4158             : 
    4159             : static GEN
    4160           7 : RgXQ_inv_FpXQXQ(GEN x, GEN y, GEN pol, GEN p)
    4161             : {
    4162           7 :   pari_sp av = avma;
    4163             :   GEN r;
    4164           7 :   GEN T = RgX_to_FpX(pol, p);
    4165           7 :   if (signe(T)==0) pari_err_OP("^",x,gen_m1);
    4166           7 :   if (lgefint(p) == 3)
    4167             :   {
    4168           7 :     ulong pp = uel(p, 2);
    4169           7 :     GEN Tp = ZX_to_Flx(T, pp);
    4170           7 :     r = FlxX_to_ZXX(FlxqXQ_inv(RgX_to_FlxqX(x, Tp, pp),
    4171             :                                RgX_to_FlxqX(y, Tp, pp), Tp, pp));
    4172             :   }
    4173             :   else
    4174           0 :     r = FpXQXQ_inv(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    4175           7 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    4176             : }
    4177             : 
    4178             : static GEN
    4179     1525873 : RgXQ_mul_fast(GEN x, GEN y, GEN T)
    4180             : {
    4181             :   GEN p, pol;
    4182             :   long pa;
    4183     1525873 :   long t = RgX_type3(x,y,T, &p,&pol,&pa);
    4184     1525876 :   switch(t)
    4185             :   {
    4186      582433 :     case t_INT:    return ZX_is_monic(T) ? ZXQ_mul(x,y,T): NULL;
    4187      629715 :     case t_FRAC:   return RgX_is_ZX(T) && ZX_is_monic(T) ? QXQ_mul(x,y,T): NULL;
    4188         105 :     case t_FFELT:  return FFXQ_mul(x, y, T, pol);
    4189      274309 :     case t_INTMOD: return RgXQ_mul_FpXQ(x, y, T, p);
    4190         385 :     case code(t_POLMOD, t_INTMOD):
    4191         385 :                    return RgXQ_mul_FpXQXQ(x, y, T, pol, p);
    4192       38929 :     default:       return NULL;
    4193             :   }
    4194             : }
    4195             : 
    4196             : GEN
    4197     1525870 : RgXQ_mul(GEN x, GEN y, GEN T)
    4198             : {
    4199     1525870 :   GEN z = RgXQ_mul_fast(x, y, T);
    4200     1525873 :   if (!z) z = RgX_rem(RgX_mul(x, y), T);
    4201     1525872 :   return z;
    4202             : }
    4203             : 
    4204             : static GEN
    4205      458508 : RgXQ_sqr_fast(GEN x, GEN T)
    4206             : {
    4207             :   GEN p, pol;
    4208             :   long pa;
    4209      458508 :   long t = RgX_type2(x, T, &p,&pol,&pa);
    4210      458508 :   switch(t)
    4211             :   {
    4212      111504 :     case t_INT:    return ZX_is_monic(T) ? ZXQ_sqr(x,T): NULL;
    4213      340153 :     case t_FRAC:   return RgX_is_ZX(T) && ZX_is_monic(T) ? QXQ_sqr(x,T): NULL;
    4214           7 :     case t_FFELT:  return FFXQ_sqr(x, T, pol);
    4215          14 :     case t_INTMOD: return RgXQ_sqr_FpXQ(x, T, p);
    4216           0 :     case code(t_POLMOD, t_INTMOD):
    4217           0 :                    return RgXQ_sqr_FpXQXQ(x, T, pol, p);
    4218        6830 :     default:       return NULL;
    4219             :   }
    4220             : }
    4221             : 
    4222             : GEN
    4223      458508 : RgXQ_sqr(GEN x, GEN T)
    4224             : {
    4225      458508 :   GEN z = RgXQ_sqr_fast(x, T);
    4226      458508 :   if (!z) z = RgX_rem(RgX_sqr(x), T);
    4227      458508 :   return z;
    4228             : }
    4229             : 
    4230             : static GEN
    4231      135069 : RgXQ_inv_fast(GEN x, GEN y)
    4232             : {
    4233             :   GEN p, pol;
    4234             :   long pa;
    4235      135069 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    4236      135069 :   switch(t)
    4237             :   {
    4238       90240 :     case t_INT:    return QXQ_inv(x,y);
    4239       31871 :     case t_FRAC:   return RgX_is_ZX(y)? QXQ_inv(x,y): NULL;
    4240          14 :     case t_FFELT:  return FFXQ_inv(x, y, pol);
    4241       12054 :     case t_INTMOD: return RgXQ_inv_FpXQ(x, y, p);
    4242           7 :     case code(t_POLMOD, t_INTMOD):
    4243           7 :                    return RgXQ_inv_FpXQXQ(x, y, pol, p);
    4244         883 :     default:       return NULL;
    4245             :   }
    4246             : }
    4247             : 
    4248             : GEN
    4249      135069 : RgXQ_inv(GEN x, GEN y)
    4250             : {
    4251      135069 :   GEN z = RgXQ_inv_fast(x, y);
    4252      135055 :   if (!z) z = RgXQ_inv_i(x, y);
    4253      135055 :   return z;
    4254             : }

Generated by: LCOV version 1.16