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

Generated by: LCOV version 1.14