Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - RgX.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 19823-d80e022) Lines: 1254 1376 91.1 %
Date: 2016-12-03 05:49:13 Functions: 138 148 93.2 %
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. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /*******************************************************************/
      18             : /*                                                                 */
      19             : /*                         GENERIC                                 */
      20             : /*                                                                 */
      21             : /*******************************************************************/
      22             : 
      23             : /* Return optimal parameter l for the evaluation of n/m polynomials of degree d
      24             :    Fractional values can be used if the evaluations are done with different
      25             :    accuracies, and thus have different weights.
      26             :  */
      27             : long
      28     1969669 : brent_kung_optpow(long d, long n, long m)
      29             : {
      30             :   long p, r;
      31     1969669 :   long pold=1, rold=n*(d-1);
      32    11792749 :   for(p=2; p<=d; p++)
      33             :   {
      34     9823080 :     r = m*(p-1) + n*((d-1)/p);
      35     9823080 :     if (r<rold) { pold=p; rold=r; }
      36             :   }
      37     1969669 :   return pold;
      38             : }
      39             : 
      40             : static GEN
      41     8167614 : gen_RgXQ_eval_powers(GEN P, GEN V, long a, long n, void *E, const struct bb_algebra *ff,
      42             :                                            GEN cmul(void *E, GEN P, long a, GEN x))
      43             : {
      44     8167614 :   pari_sp av = avma;
      45             :   long i;
      46     8167614 :   GEN z = cmul(E,P,a,ff->one(E));
      47     8167542 :   if (!z) z = gen_0;
      48    61373004 :   for (i=1; i<=n; i++)
      49             :   {
      50    53205401 :     GEN t = cmul(E,P,a+i,gel(V,i+1));
      51    53205143 :     if (t) {
      52    52013381 :       z = ff->add(E, z, t);
      53    52013379 :       if (gc_needed(av,2)) z = gerepileupto(av, z);
      54             :     }
      55             :   }
      56     8167603 :   return ff->red(E,z);
      57             : }
      58             : 
      59             : /* Brent & Kung
      60             :  * (Fast algorithms for manipulating formal power series, JACM 25:581-595, 1978)
      61             :  *
      62             :  * V as output by FpXQ_powers(x,l,T,p). For optimal performance, l is as given
      63             :  * by brent_kung_optpow */
      64             : GEN
      65     5056682 : gen_bkeval_powers(GEN P, long d, GEN V, void *E, const struct bb_algebra *ff,
      66             :                                      GEN cmul(void *E, GEN P, long a, GEN x))
      67             : {
      68     5056682 :   pari_sp av = avma;
      69     5056682 :   long l = lg(V)-1;
      70             :   GEN z, u;
      71             : 
      72     5056682 :   if (d < 0) return ff->zero(E);
      73     4586149 :   if (d < l) return gerepileupto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
      74     2153113 :   if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
      75     2153113 :   d -= l;
      76     2153113 :   z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
      77     5734581 :   while (d >= l-1)
      78             :   {
      79     1428350 :     d -= l-1;
      80     1428350 :     u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
      81     1428342 :     z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
      82     1428353 :     if (gc_needed(av,2))
      83          79 :       z = gerepileupto(av, z);
      84             :   }
      85     2153116 :   u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
      86     2153115 :   z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
      87     2153117 :   if (DEBUGLEVEL>=8)
      88             :   {
      89           0 :     long cnt = 1 + (d - l) / (l-1);
      90           0 :     err_printf("RgX_RgXQV_eval: %ld RgXQ_mul [%ld]\n", cnt, l-1);
      91             :   }
      92     2153117 :   return gerepileupto(av, ff->red(E,z));
      93             : }
      94             : 
      95             : GEN
      96     1025967 : gen_bkeval(GEN Q, long d, GEN x, int use_sqr, void *E, const struct bb_algebra *ff,
      97             :                                       GEN cmul(void *E, GEN P, long a, GEN x))
      98             : {
      99     1025967 :   pari_sp av = avma;
     100             :   GEN z, V;
     101             :   long rtd;
     102     1025967 :   if (d < 0) return ff->zero(E);
     103     1025862 :   rtd = (long) sqrt((double)d);
     104     1025862 :   V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
     105     1025860 :   z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
     106     1025863 :   return gerepileupto(av, z);
     107             : }
     108             : 
     109             : static GEN
     110      531142 : _gen_nored(void *E, GEN x) { (void)E; return x; }
     111             : static GEN
     112    32936424 : _gen_add(void *E, GEN x, GEN y) { (void)E; return gadd(x, y); }
     113             : static GEN
     114           0 : _gen_sub(void *E, GEN x, GEN y) { (void)E; return gsub(x, y); }
     115             : static GEN
     116      552358 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
     117             : static GEN
     118      177928 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
     119             : static GEN
     120      541796 : _gen_one(void *E) { (void)E; return gen_1; }
     121             : static GEN
     122          84 : _gen_zero(void *E) { (void)E; return gen_0; }
     123             : 
     124             : static struct bb_algebra Rg_algebra = { _gen_nored, _gen_add, _gen_sub,
     125             :               _gen_mul, _gen_sqr,_gen_one,_gen_zero };
     126             : 
     127             : static GEN
     128       16135 : _gen_cmul(void *E, GEN P, long a, GEN x)
     129       16135 : {(void)E; return gmul(gel(P,a+2), x);}
     130             : 
     131             : GEN
     132        5257 : RgX_RgV_eval(GEN Q, GEN x)
     133             : {
     134        5257 :   return gen_bkeval_powers(Q, degpol(Q), x, NULL, &Rg_algebra, _gen_cmul);
     135             : }
     136             : 
     137             : GEN
     138           0 : RgX_Rg_eval_bk(GEN Q, GEN x)
     139             : {
     140           0 :   return gen_bkeval(Q, degpol(Q), x, 1, NULL, &Rg_algebra, _gen_cmul);
     141             : }
     142             : 
     143             : GEN
     144         105 : RgXV_RgV_eval(GEN Q, GEN x)
     145             : {
     146         105 :   long i, l = lg(Q), vQ = gvar(Q);
     147         105 :   GEN v = cgetg(l, t_VEC);
     148       12726 :   for (i = 1; i < l; i++)
     149             :   {
     150       12621 :     GEN Qi = gel(Q, i);
     151       12621 :     gel(v, i) = typ(Qi)==t_POL && varn(Qi)==vQ? RgX_RgV_eval(Qi, x): gcopy(Qi);
     152             :   }
     153         105 :   return v;
     154             : }
     155             : 
     156             : const struct bb_algebra *
     157       67687 : get_Rg_algebra(void)
     158             : {
     159       67687 :   return &Rg_algebra;
     160             : }
     161             : 
     162             : /*******************************************************************/
     163             : /*                                                                 */
     164             : /*                         RgX                                     */
     165             : /*                                                                 */
     166             : /*******************************************************************/
     167             : 
     168             : long
     169     3791197 : RgX_equal(GEN x, GEN y)
     170             : {
     171     3791197 :   long i = lg(x);
     172             : 
     173     3791197 :   if (i != lg(y)) return 0;
     174    19611673 :   for (i--; i > 1; i--)
     175    15881236 :     if (!gequal(gel(x,i),gel(y,i))) return 0;
     176     3730437 :   return 1;
     177             : }
     178             : 
     179             : /* Returns 1 in the base ring over which x is defined */
     180             : /* HACK: this also works for t_SER */
     181             : GEN
     182      214415 : RgX_get_1(GEN x)
     183             : {
     184             :   GEN p, T;
     185      214415 :   long i, lx, tx = RgX_type(x, &p, &T, &lx);
     186      214415 :   if (RgX_type_is_composite(tx))
     187        1197 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     188      214415 :   switch(tx)
     189             :   {
     190          49 :     case t_INTMOD: retmkintmod(gen_1, icopy(p));
     191           7 :     case t_PADIC: return cvtop(gen_1, p, lx);
     192          14 :     case t_FFELT: return FF_1(T);
     193      214345 :     default: return gen_1;
     194             :   }
     195             : }
     196             : /* Returns 0 in the base ring over which x is defined */
     197             : /* HACK: this also works for t_SER */
     198             : GEN
     199      110873 : RgX_get_0(GEN x)
     200             : {
     201             :   GEN p, T;
     202      110873 :   long i, lx, tx = RgX_type(x, &p, &T, &lx);
     203      110873 :   if (RgX_type_is_composite(tx))
     204       13118 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     205      110873 :   switch(tx)
     206             :   {
     207          35 :     case t_INTMOD: retmkintmod(gen_0, icopy(p));
     208           0 :     case t_PADIC: return cvtop(gen_0, p, lx);
     209           0 :     case t_FFELT: return FF_zero(T);
     210      110838 :     default: return gen_0;
     211             :   }
     212             : }
     213             : 
     214             : GEN
     215        1722 : QX_ZXQV_eval(GEN P, GEN V, GEN dV)
     216             : {
     217        1722 :   long i, n = degpol(P);
     218             :   GEN z, dz, dP;
     219        1722 :   if (n < 0) return gen_0;
     220        1722 :   P = Q_remove_denom(P, &dP);
     221        1722 :   z = gel(P,2); if (n == 0) return icopy(z);
     222         952 :   if (dV) z = mulii(dV, z); /* V[1] = dV */
     223         952 :   z = ZX_Z_add_shallow(ZX_Z_mul(gel(V,2),gel(P,3)), z);
     224         952 :   for (i=2; i<=n; i++) z = ZX_add(ZX_Z_mul(gel(V,i+1),gel(P,2+i)), z);
     225         952 :   dz = mul_denom(dP, dV);
     226         952 :   return dz? RgX_Rg_div(z, dz): z;
     227             : }
     228             : 
     229             : /* Return P(h * x), not memory clean */
     230             : GEN
     231        3248 : RgX_unscale(GEN P, GEN h)
     232             : {
     233        3248 :   long i, l = lg(P);
     234        3248 :   GEN hi = gen_1, Q = cgetg(l, t_POL);
     235        3248 :   Q[1] = P[1];
     236        3248 :   if (l == 2) return Q;
     237        3248 :   gel(Q,2) = gcopy(gel(P,2));
     238        8484 :   for (i=3; i<l; i++)
     239             :   {
     240        5236 :     hi = gmul(hi,h);
     241        5236 :     gel(Q,i) = gmul(gel(P,i), hi);
     242             :   }
     243        3248 :   return Q;
     244             : }
     245             : /* P a ZX, h a t_INT. Return P(h * x), not memory clean; optimize for h = -1 */
     246             : GEN
     247        7489 : ZX_unscale(GEN P, GEN h)
     248             : {
     249        7489 :   long i, l = lg(P);
     250        7489 :   GEN Q = cgetg(l, t_POL);
     251        7489 :   Q[1] = P[1];
     252        7489 :   if (l == 2) return Q;
     253        7489 :   gel(Q,2) = gel(P,2);
     254        7489 :   if (l == 3) return Q;
     255        7489 :   if (equalim1(h))
     256      295097 :     for (i=3; i<l; i++)
     257             :     {
     258      291239 :       gel(Q,i) = negi(gel(P,i));
     259      291239 :       if (++i == l) break;
     260      288812 :       gel(Q,i) = gel(P,i);
     261             :     }
     262             :   else
     263             :   {
     264        1204 :     GEN hi = h;
     265        1204 :     gel(Q,3) = mulii(gel(P,3), hi);
     266        6405 :     for (i=4; i<l; i++)
     267             :     {
     268        5201 :       hi = mulii(hi,h);
     269        5201 :       gel(Q,i) = mulii(gel(P,i), hi);
     270             :     }
     271             :   }
     272        7489 :   return Q;
     273             : }
     274             : /* P a ZX. Return P(x << n), not memory clean */
     275             : GEN
     276        9005 : ZX_unscale2n(GEN P, long n)
     277             : {
     278        9005 :   long i, ni = n, l = lg(P);
     279        9005 :   GEN Q = cgetg(l, t_POL);
     280        9005 :   Q[1] = P[1];
     281        9005 :   if (l == 2) return Q;
     282        9005 :   gel(Q,2) = gel(P,2);
     283        9005 :   if (l == 3) return Q;
     284        9005 :   gel(Q,3) = shifti(gel(P,3), ni);
     285       43269 :   for (i=4; i<l; i++)
     286             :   {
     287       34264 :     ni += n;
     288       34264 :     gel(Q,i) = shifti(gel(P,i), ni);
     289             :   }
     290        9005 :   return Q;
     291             : }
     292             : /* P(h*X) / h, assuming h | P(0), i.e. the result is a ZX */
     293             : GEN
     294         154 : ZX_unscale_div(GEN P, GEN h)
     295             : {
     296         154 :   long i, l = lg(P);
     297         154 :   GEN hi, Q = cgetg(l, t_POL);
     298         154 :   Q[1] = P[1];
     299         154 :   if (l == 2) return Q;
     300         154 :   gel(Q,2) = diviiexact(gel(P,2), h);
     301         154 :   if (l == 3) return Q;
     302         154 :   gel(Q,3) = gel(P,3);
     303         154 :   if (l == 4) return Q;
     304         154 :   hi = h;
     305         154 :   gel(Q,4) = mulii(gel(P,4), hi);
     306         497 :   for (i=5; i<l; i++)
     307             :   {
     308         343 :     hi = mulii(hi,h);
     309         343 :     gel(Q,i) = mulii(gel(P,i), hi);
     310             :   }
     311         154 :   return Q;
     312             : }
     313             : 
     314             : GEN
     315         203 : RgXV_unscale(GEN v, GEN h)
     316             : {
     317             :   long i, l;
     318             :   GEN w;
     319         203 :   if (!h || isint1(h)) return v;
     320         147 :   w = cgetg_copy(v, &l);
     321         147 :   for (i=1; i<l; i++) gel(w,i) = RgX_unscale(gel(v,i), h);
     322         147 :   return w;
     323             : }
     324             : 
     325             : /* Return h^degpol(P) P(x / h), not memory clean */
     326             : GEN
     327        1099 : RgX_rescale(GEN P, GEN h)
     328             : {
     329        1099 :   long i, l = lg(P);
     330        1099 :   GEN Q = cgetg(l,t_POL), hi = h;
     331        1099 :   Q[l-1] = P[l-1];
     332        6909 :   for (i=l-2; i>=2; i--)
     333             :   {
     334        6909 :     gel(Q,i) = gmul(gel(P,i), hi);
     335        6909 :     if (i == 2) break;
     336        5810 :     hi = gmul(hi,h);
     337             :   }
     338        1099 :   Q[1] = P[1]; return Q;
     339             : }
     340             : 
     341             : /* A(X^d) --> A(X) */
     342             : GEN
     343       70920 : RgX_deflate(GEN x0, long d)
     344             : {
     345             :   GEN z, y, x;
     346       70920 :   long i,id, dy, dx = degpol(x0);
     347       70920 :   if (d == 1 || dx <= 0) return leafcopy(x0);
     348       48917 :   dy = dx/d;
     349       48917 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     350       48917 :   z = y + 2;
     351       48917 :   x = x0+ 2;
     352       48917 :   for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
     353       48917 :   return y;
     354             : }
     355             : 
     356             : /* return x0(X^d) */
     357             : GEN
     358      104482 : RgX_inflate(GEN x0, long d)
     359             : {
     360      104482 :   long i, id, dy, dx = degpol(x0);
     361      104482 :   GEN x = x0 + 2, z, y;
     362      104482 :   if (dx <= 0) return leafcopy(x0);
     363      104111 :   dy = dx*d;
     364      104111 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     365      104112 :   z = y + 2;
     366      104112 :   for (i=0; i<=dy; i++) gel(z,i) = gen_0;
     367      104112 :   for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
     368      104112 :   return y;
     369             : }
     370             : 
     371             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     372             : GEN
     373      970835 : RgX_translate(GEN P, GEN c)
     374             : {
     375      970835 :   pari_sp av = avma;
     376             :   GEN Q, *R;
     377             :   long i, k, n;
     378             : 
     379      970835 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     380      968042 :   Q = leafcopy(P);
     381      968042 :   R = (GEN*)(Q+2); n = degpol(P);
     382      968042 :   if (gequal1(c))
     383             :   {
     384        2016 :     for (i=1; i<=n; i++)
     385             :     {
     386        1757 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], R[k+1]);
     387        1757 :       if (gc_needed(av,2))
     388             :       {
     389           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL(1), i = %ld/%ld", i,n);
     390           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     391             :       }
     392             :     }
     393             :   }
     394      967783 :   else if (gequalm1(c))
     395             :   {
     396      133084 :     for (i=1; i<=n; i++)
     397             :     {
     398      113960 :       for (k=n-i; k<n; k++) R[k] = gsub(R[k], R[k+1]);
     399      113960 :       if (gc_needed(av,2))
     400             :       {
     401           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL(-1), i = %ld/%ld", i,n);
     402           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     403             :       }
     404             :     }
     405             :   }
     406             :   else
     407             :   {
     408     3271965 :     for (i=1; i<=n; i++)
     409             :     {
     410     2323306 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], gmul(c, R[k+1]));
     411     2323306 :       if (gc_needed(av,2))
     412             :       {
     413           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL, i = %ld/%ld", i,n);
     414           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     415             :       }
     416             :     }
     417             :   }
     418      968042 :   return gerepilecopy(av, Q);
     419             : }
     420             : 
     421             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     422             : GEN
     423       35025 : ZX_translate(GEN P, GEN c)
     424             : {
     425       35025 :   pari_sp av = avma;
     426             :   GEN Q, *R;
     427             :   long i, k, n;
     428             : 
     429       35025 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     430       34990 :   Q = leafcopy(P);
     431       34990 :   R = (GEN*)(Q+2); n = degpol(P);
     432       34990 :   if (equali1(c))
     433             :   {
     434      375432 :     for (i=1; i<=n; i++)
     435             :     {
     436      345512 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], R[k+1]);
     437      345512 :       if (gc_needed(av,2))
     438             :       {
     439           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"ZX_translate(1), i = %ld/%ld", i,n);
     440           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     441             :       }
     442             :     }
     443             :   }
     444        5070 :   else if (equalim1(c))
     445             :   {
     446          28 :     for (i=1; i<=n; i++)
     447             :     {
     448          21 :       for (k=n-i; k<n; k++) R[k] = subii(R[k], R[k+1]);
     449          21 :       if (gc_needed(av,2))
     450             :       {
     451           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"ZX_translate(-1), i = %ld/%ld", i,n);
     452           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     453             :       }
     454             :     }
     455             :   }
     456             :   else
     457             :   {
     458       61643 :     for (i=1; i<=n; i++)
     459             :     {
     460       56580 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], mulii(c, R[k+1]));
     461       56580 :       if (gc_needed(av,2))
     462             :       {
     463           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"ZX_translate, i = %ld/%ld", i,n);
     464           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     465             :       }
     466             :     }
     467             :   }
     468       34990 :   return gerepilecopy(av, Q);
     469             : }
     470             : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
     471             : GEN
     472        6048 : RgXQX_translate(GEN P, GEN c, GEN T)
     473             : {
     474        6048 :   pari_sp av = avma;
     475             :   GEN Q, *R;
     476             :   long i, k, n;
     477             : 
     478        6048 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     479        6027 :   Q = leafcopy(P);
     480        6027 :   R = (GEN*)(Q+2); n = degpol(P);
     481       34608 :   for (i=1; i<=n; i++)
     482             :   {
     483      141106 :     for (k=n-i; k<n; k++)
     484             :     {
     485      112525 :       pari_sp av2 = avma;
     486      112525 :       R[k] = gerepileupto(av2, RgX_rem(gadd(R[k], gmul(c, R[k+1])), T));
     487             :     }
     488       28581 :     if (gc_needed(av,2))
     489             :     {
     490           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXQX_translate, i = %ld/%ld", i,n);
     491           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     492             :     }
     493             :   }
     494        6027 :   return gerepilecopy(av, Q);
     495             : }
     496             : 
     497             : /********************************************************************/
     498             : /**                                                                **/
     499             : /**                          CONVERSIONS                           **/
     500             : /**                       (not memory clean)                       **/
     501             : /**                                                                **/
     502             : /********************************************************************/
     503             : /* to INT / FRAC / (POLMOD mod T), not memory clean because T not copied,
     504             :  * but everything else is */
     505             : static GEN
     506       13920 : QXQ_to_mod_copy(GEN x, GEN T)
     507             : {
     508             :   long d;
     509       13920 :   switch(typ(x))
     510             :   {
     511        5033 :     case t_INT:  return icopy(x);
     512         371 :     case t_FRAC: return gcopy(x);
     513             :     case t_POL:
     514        8516 :       d = degpol(x);
     515        8516 :       if (d < 0) return gen_0;
     516        8264 :       if (d == 0) return gcopy(gel(x,2));
     517        7949 :       return mkpolmod(RgX_copy(x), T);
     518           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     519           0 :              return NULL;/* not reached */
     520             :   }
     521             : }
     522             : /* pure shallow version */
     523             : static GEN
     524      407964 : QXQ_to_mod(GEN x, GEN T)
     525             : {
     526             :   long d;
     527      407964 :   switch(typ(x))
     528             :   {
     529             :     case t_INT:
     530      354844 :     case t_FRAC: return x;
     531             :     case t_POL:
     532       53120 :       d = degpol(x);
     533       53120 :       if (d < 0) return gen_0;
     534       48888 :       if (d == 0) return gel(x,2);
     535       45031 :       return mkpolmod(x, T);
     536           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     537           0 :              return NULL;/* not reached */
     538             :   }
     539             : }
     540             : /* T a ZX, z lifted from (Q[Y]/(T(Y)))[X], apply QXQ_to_mod_copy to all coeffs.
     541             :  * Not memory clean because T not copied, but everything else is */
     542             : static GEN
     543        1883 : QXQX_to_mod(GEN z, GEN T)
     544             : {
     545        1883 :   long i,l = lg(z);
     546        1883 :   GEN x = cgetg(l,t_POL);
     547        1883 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_copy(gel(z,i), T);
     548        1883 :   x[1] = z[1]; return normalizepol_lg(x,l);
     549             : }
     550             : /* pure shallow version */
     551             : GEN
     552       82992 : QXQX_to_mod_shallow(GEN z, GEN T)
     553             : {
     554       82992 :   long i,l = lg(z);
     555       82992 :   GEN x = cgetg(l,t_POL);
     556       82992 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
     557       82992 :   x[1] = z[1]; return normalizepol_lg(x,l);
     558             : }
     559             : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
     560             : GEN
     561         525 : QXQXV_to_mod(GEN V, GEN T)
     562             : {
     563         525 :   long i, l = lg(V);
     564         525 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     565         525 :   for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
     566         525 :   return z;
     567             : }
     568             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     569             : GEN
     570         869 : QXQV_to_mod(GEN V, GEN T)
     571             : {
     572         869 :   long i, l = lg(V);
     573         869 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     574         869 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod_copy(gel(V,i), T);
     575         869 :   return z;
     576             : }
     577             : 
     578             : GEN
     579      645638 : RgX_renormalize_lg(GEN x, long lx)
     580             : {
     581             :   long i;
     582     1843632 :   for (i = lx-1; i>1; i--)
     583     1742041 :     if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
     584      645638 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     585      645638 :   setlg(x, i+1); setsigne(x, i != 1); return x;
     586             : }
     587             : 
     588             : GEN
     589      359228 : RgV_to_RgX(GEN x, long v)
     590             : {
     591      359228 :   long i, k = lg(x);
     592             :   GEN p;
     593             : 
     594      359228 :   while (--k && gequal0(gel(x,k)));
     595      359228 :   if (!k) return pol_0(v);
     596      358850 :   i = k+2; p = cgetg(i,t_POL);
     597      358850 :   p[1] = evalsigne(1) | evalvarn(v);
     598      358850 :   x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
     599      358850 :   return p;
     600             : }
     601             : GEN
     602      149566 : RgV_to_RgX_reverse(GEN x, long v)
     603             : {
     604      149566 :   long j, k, l = lg(x);
     605             :   GEN p;
     606             : 
     607      149566 :   for (k = 1; k < l; k++)
     608      149566 :     if (!gequal0(gel(x,k))) break;
     609      149566 :   if (k == l) return pol_0(v);
     610      149566 :   k -= 1;
     611      149566 :   l -= k;
     612      149566 :   x += k;
     613      149566 :   p = cgetg(l+1,t_POL);
     614      149566 :   p[1] = evalsigne(1) | evalvarn(v);
     615      149566 :   for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
     616      149566 :   return p;
     617             : }
     618             : 
     619             : /* return the (N-dimensional) vector of coeffs of p */
     620             : GEN
     621     3364754 : RgX_to_RgC(GEN x, long N)
     622             : {
     623             :   long i, l;
     624             :   GEN z;
     625     3364754 :   l = lg(x)-1; x++;
     626     3364754 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     627     3364754 :   z = cgetg(N+1,t_COL);
     628     3364754 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     629     3364754 :   for (   ; i<=N; i++) gel(z,i) = gen_0;
     630     3364754 :   return z;
     631             : }
     632             : GEN
     633       25606 : Rg_to_RgC(GEN x, long N)
     634             : {
     635       25606 :   return (typ(x) == t_POL)? RgX_to_RgC(x,N): scalarcol_shallow(x, N);
     636             : }
     637             : 
     638             : /* vector of polynomials (in v) whose coeffs are given by the columns of x */
     639             : GEN
     640       35201 : RgM_to_RgXV(GEN x, long v)
     641             : {
     642       35201 :   long j, lx = lg(x);
     643       35201 :   GEN y = cgetg(lx, t_VEC);
     644       35201 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), v);
     645       35201 :   return y;
     646             : }
     647             : 
     648             : /* matrix whose entries are given by the coeffs of the polynomials in
     649             :  * vector v (considered as degree n-1 polynomials) */
     650             : GEN
     651        6027 : RgV_to_RgM(GEN v, long n)
     652             : {
     653        6027 :   long j, N = lg(v);
     654        6027 :   GEN y = cgetg(N, t_MAT);
     655        6027 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j), n);
     656        6027 :   return y;
     657             : }
     658             : GEN
     659        5152 : RgXV_to_RgM(GEN v, long n)
     660             : {
     661        5152 :   long j, N = lg(v);
     662        5152 :   GEN y = cgetg(N, t_MAT);
     663        5152 :   for (j=1; j<N; j++) gel(y,j) = RgX_to_RgC(gel(v,j), n);
     664        5152 :   return y;
     665             : }
     666             : 
     667             : /* polynomial (in v) of polynomials (in w) whose coeffs are given by the columns of x */
     668             : GEN
     669       15309 : RgM_to_RgXX(GEN x, long v,long w)
     670             : {
     671       15309 :   long j, lx = lg(x);
     672       15309 :   GEN y = cgetg(lx+1, t_POL);
     673       15309 :   y[1] = evalsigne(1) | evalvarn(v);
     674       15309 :   y++;
     675       15309 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
     676       15309 :   return normalizepol_lg(--y, lx+1);
     677             : }
     678             : 
     679             : /* matrix whose entries are given by the coeffs of the polynomial v in
     680             :  * two variables (considered as degree n-1 polynomials) */
     681             : GEN
     682          21 : RgXX_to_RgM(GEN v, long n)
     683             : {
     684          21 :   long j, N = lg(v)-1;
     685          21 :   GEN y = cgetg(N, t_MAT);
     686          21 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j+1), n);
     687          21 :   return y;
     688             : }
     689             : 
     690             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     691             : GEN
     692       12922 : RgXY_swapspec(GEN x, long n, long w, long nx)
     693             : {
     694       12922 :   long j, ly = n+3;
     695       12922 :   GEN y = cgetg(ly, t_POL);
     696       12922 :   y[1] = evalsigne(1);
     697      192131 :   for (j=2; j<ly; j++)
     698             :   {
     699             :     long k;
     700      179209 :     GEN a = cgetg(nx+2,t_POL);
     701      179209 :     a[1] = evalsigne(1) | evalvarn(w);
     702      950740 :     for (k=0; k<nx; k++)
     703             :     {
     704      771531 :       GEN xk = gel(x,k);
     705      771531 :       if (typ(xk)==t_POL)
     706      686362 :         gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
     707             :       else
     708       85169 :         gel(a,k+2) = j==2 ? xk: gen_0;
     709             :     }
     710      179209 :     gel(y,j) = normalizepol_lg(a, nx+2);
     711             :   }
     712       12922 :   return normalizepol_lg(y,ly);
     713             : }
     714             : 
     715             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     716             : GEN
     717         224 : RgXY_swap(GEN x, long n, long w)
     718             : {
     719         224 :   GEN z = RgXY_swapspec(x+2, n, w, lgpol(x));
     720         224 :   setvarn(z, varn(x)); return z;
     721             : }
     722             : 
     723             : long
     724           1 : RgXY_degreex(GEN b)
     725             : {
     726           1 :   long deg = -1, i;
     727           1 :   if (!signe(b)) return -1;
     728           3 :   for (i = 2; i < lg(b); ++i)
     729             :   {
     730           2 :     GEN bi = gel(b, i);
     731           2 :     if (typ(bi) == t_POL)
     732           1 :       deg = maxss(deg, degpol(bi));
     733             :   }
     734           1 :   return deg;
     735             : }
     736             : 
     737             : /* return (x % X^n). Shallow */
     738             : GEN
     739        4329 : RgXn_red_shallow(GEN a, long n)
     740             : {
     741        4329 :   long i, L, l = lg(a);
     742             :   GEN  b;
     743        4329 :   if (l == 2 || !n) return pol_0(varn(a));
     744        4329 :   L = n+2; if (L > l) L = l;
     745        4329 :   b = cgetg(L, t_POL); b[1] = a[1];
     746        4329 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
     747        4329 :   return normalizepol_lg(b,L);
     748             : }
     749             : 
     750             : GEN
     751         336 : RgXnV_red_shallow(GEN P, long n)
     752             : {
     753         336 :   long i, l = lg(P);
     754         336 :   GEN Q = cgetg(l, t_VEC);
     755         336 :   for (i=1; i<l; i++) gel(Q,i) = RgXn_red_shallow(gel(P,i), n);
     756         336 :   return Q;
     757             : }
     758             : 
     759             : /* return (x * X^n). Shallow */
     760             : GEN
     761    54508027 : RgX_shift_shallow(GEN a, long n)
     762             : {
     763    54508027 :   long i, l = lg(a);
     764             :   GEN  b;
     765    54508027 :   if (l == 2 || !n) return a;
     766    40551789 :   l += n;
     767    40551789 :   if (n < 0)
     768             :   {
     769    36530528 :     if (l <= 2) return pol_0(varn(a));
     770    36530122 :     b = cgetg(l, t_POL); b[1] = a[1];
     771    36530122 :     a -= n;
     772    36530122 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     773             :   } else {
     774     4021261 :     b = cgetg(l, t_POL); b[1] = a[1];
     775     4021261 :     a -= n; n += 2;
     776     4021261 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     777     4021261 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     778             :   }
     779    40551383 :   return b;
     780             : }
     781             : /* return (x * X^n). */
     782             : GEN
     783     3346181 : RgX_shift(GEN a, long n)
     784             : {
     785     3346181 :   long i, l = lg(a);
     786             :   GEN  b;
     787     3346181 :   if (l == 2 || !n) return RgX_copy(a);
     788     3345957 :   l += n;
     789     3345957 :   if (n < 0)
     790             :   {
     791         595 :     if (l <= 2) return pol_0(varn(a));
     792         553 :     b = cgetg(l, t_POL); b[1] = a[1];
     793         553 :     a -= n;
     794         553 :     for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     795             :   } else {
     796     3345362 :     b = cgetg(l, t_POL); b[1] = a[1];
     797     3345362 :     a -= n; n += 2;
     798     3345362 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     799     3345362 :     for (   ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     800             :   }
     801     3345915 :   return b;
     802             : }
     803             : 
     804             : GEN
     805      310303 : RgX_rotate_shallow(GEN P, long k, long p)
     806             : {
     807      310303 :   long i, l = lgpol(P);
     808             :   GEN r;
     809      310303 :   if (signe(P)==0)
     810         329 :     return pol_0(varn(P));
     811      309974 :   r = cgetg(p+2,t_POL); r[1] = P[1];
     812     2063166 :   for(i=0; i<p; i++)
     813             :   {
     814     1753192 :     long s = 2+(i+k)%p;
     815     1753192 :     gel(r,s) = i<l? gel(P,2+i): gen_0;
     816             :   }
     817      309974 :   return RgX_renormalize(r);
     818             : }
     819             : 
     820             : GEN
     821     2580149 : RgX_mulXn(GEN x, long d)
     822             : {
     823             :   pari_sp av;
     824             :   GEN z;
     825             :   long v;
     826     2580149 :   if (d >= 0) return RgX_shift(x, d);
     827     1133666 :   d = -d;
     828     1133666 :   v = RgX_val(x);
     829     1133666 :   if (v >= d) return RgX_shift(x, -d);
     830     1133659 :   av = avma;
     831     1133659 :   z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
     832     1133659 :   return gerepileupto(av, z);
     833             : }
     834             : 
     835             : long
     836     2076506 : RgX_val(GEN x)
     837             : {
     838     2076506 :   long i, lx = lg(x);
     839     2076506 :   if (lx == 2) return LONG_MAX;
     840     2091444 :   for (i = 2; i < lx; i++)
     841     2091444 :     if (!isexactzero(gel(x,i))) break;
     842     2076492 :   if (i == lx) i--; /* possible with non-rational zeros */
     843     2076492 :   return i - 2;
     844             : }
     845             : long
     846    40395654 : RgX_valrem(GEN x, GEN *Z)
     847             : {
     848    40395654 :   long v, i, lx = lg(x);
     849    40395654 :   if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
     850    77467816 :   for (i = 2; i < lx; i++)
     851    77467816 :     if (!isexactzero(gel(x,i))) break;
     852    40395654 :   if (i == lx) i--; /* possible with non-rational zeros */
     853    40395654 :   v = i - 2;
     854    40395654 :   *Z = RgX_shift_shallow(x, -v);
     855    40395654 :   return v;
     856             : }
     857             : long
     858        2648 : RgX_valrem_inexact(GEN x, GEN *Z)
     859             : {
     860             :   long v;
     861        2648 :   if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
     862        2809 :   for (v = 0;; v++)
     863        2809 :     if (!gequal0(gel(x,2+v))) break;
     864         161 :   if (Z) *Z = RgX_shift_shallow(x, -v);
     865        2648 :   return v;
     866             : }
     867             : 
     868             : GEN
     869           0 : RgXQC_red(GEN P, GEN T)
     870             : {
     871           0 :   long i, l = lg(P);
     872           0 :   GEN Q = cgetg(l, t_COL);
     873           0 :   for (i=1; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     874           0 :   return Q;
     875             : }
     876             : 
     877             : GEN
     878          42 : RgXQV_red(GEN P, GEN T)
     879             : {
     880          42 :   long i, l = lg(P);
     881          42 :   GEN Q = cgetg(l, t_VEC);
     882          42 :   for (i=1; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     883          42 :   return Q;
     884             : }
     885             : 
     886             : GEN
     887        5509 : RgXQX_red(GEN P, GEN T)
     888             : {
     889        5509 :   long i, l = lg(P);
     890        5509 :   GEN Q = cgetg(l, t_POL);
     891        5509 :   Q[1] = P[1];
     892        5509 :   for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     893        5509 :   return normalizepol_lg(Q, l);
     894             : }
     895             : 
     896             : GEN
     897      266434 : RgX_deriv(GEN x)
     898             : {
     899      266434 :   long i,lx = lg(x)-1;
     900             :   GEN y;
     901             : 
     902      266434 :   if (lx<3) return pol_0(varn(x));
     903      265650 :   y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
     904      265650 :   for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
     905      265650 :   y[1] = x[1]; return normalizepol_lg(y,i);
     906             : }
     907             : 
     908             : GEN
     909      290808 : RgX_recipspec_shallow(GEN x, long l, long n)
     910             : {
     911             :   long i;
     912      290808 :   GEN z=cgetg(n+2,t_POL)+2;
     913    13593345 :   for(i=0; i<l; i++)
     914    13302535 :     gel(z,n-i-1) = gel(x,i);
     915      378534 :   for(   ; i<n; i++)
     916       87724 :     gel(z, n-i-1) = gen_0;
     917      290810 :   return normalizepol_lg(z-2,n+2);
     918             : }
     919             : 
     920             : /* return coefficients s.t x = x_0 X^n + ... + x_n */
     921             : GEN
     922       63847 : RgX_recip(GEN x)
     923             : {
     924             :   long lx, i, j;
     925       63847 :   GEN y = cgetg_copy(x, &lx);
     926       63847 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
     927       63847 :   return normalizepol_lg(y,lx);
     928             : }
     929             : /* shallow version */
     930             : GEN
     931      435056 : RgX_recip_shallow(GEN x)
     932             : {
     933             :   long lx, i, j;
     934      435056 :   GEN y = cgetg_copy(x, &lx);
     935      435076 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
     936      435076 :   return y;
     937             : }
     938             : /*******************************************************************/
     939             : /*                                                                 */
     940             : /*                      ADDITION / SUBTRACTION                     */
     941             : /*                                                                 */
     942             : /*******************************************************************/
     943             : /* same variable */
     944             : GEN
     945    15535832 : RgX_add(GEN x, GEN y)
     946             : {
     947    15535832 :   long i, lx = lg(x), ly = lg(y);
     948             :   GEN z;
     949    15535832 :   if (ly <= lx) {
     950    14181183 :     z = cgetg(lx,t_POL); z[1] = x[1];
     951    14181192 :     for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
     952    14181185 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
     953    14181185 :     z = normalizepol_lg(z, lx);
     954             :   } else {
     955     1354649 :     z = cgetg(ly,t_POL); z[1] = y[1];
     956     1354654 :     for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
     957     1354652 :     for (   ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
     958     1354650 :     z = normalizepol_lg(z, ly);
     959             :   }
     960    15535833 :   return z;
     961             : }
     962             : GEN
     963     9657496 : RgX_sub(GEN x, GEN y)
     964             : {
     965     9657496 :   long i, lx = lg(x), ly = lg(y);
     966             :   GEN z;
     967     9657496 :   if (ly <= lx) {
     968     7817268 :     z = cgetg(lx,t_POL); z[1] = x[1];
     969     7817302 :     for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
     970     7817270 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
     971     7817270 :     z = normalizepol_lg(z, lx);
     972             :   } else {
     973     1840228 :     z = cgetg(ly,t_POL); z[1] = y[1];
     974     1840228 :     for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
     975     1840228 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
     976     1840228 :     z = normalizepol_lg(z, ly);
     977             :   }
     978     9657499 :   return z;
     979             : }
     980             : GEN
     981     1177630 : RgX_neg(GEN x)
     982             : {
     983     1177630 :   long i, lx = lg(x);
     984     1177630 :   GEN y = cgetg(lx, t_POL); y[1] = x[1];
     985     1177630 :   for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
     986     1177630 :   return y;
     987             : }
     988             : 
     989             : GEN
     990    10345600 : RgX_Rg_add(GEN y, GEN x)
     991             : {
     992             :   GEN z;
     993    10345600 :   long lz = lg(y), i;
     994    10345600 :   if (lz == 2) return scalarpol(x,varn(y));
     995     8905596 :   z = cgetg(lz,t_POL); z[1] = y[1];
     996     8905596 :   gel(z,2) = gadd(gel(y,2),x);
     997     8905596 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
     998             :   /* probably useless unless lz = 3, but cannot be skipped if y is
     999             :    * an inexact 0 */
    1000     8905596 :   return normalizepol_lg(z,lz);
    1001             : }
    1002             : GEN
    1003        2422 : RgX_Rg_add_shallow(GEN y, GEN x)
    1004             : {
    1005             :   GEN z;
    1006        2422 :   long lz = lg(y), i;
    1007        2422 :   if (lz == 2) return scalarpol(x,varn(y));
    1008        2422 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1009        2422 :   gel(z,2) = gadd(gel(y,2),x);
    1010        2422 :   for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
    1011        2422 :   return z = normalizepol_lg(z,lz);
    1012             : }
    1013             : GEN
    1014       32138 : RgX_Rg_sub(GEN y, GEN x)
    1015             : {
    1016             :   GEN z;
    1017       32138 :   long lz = lg(y), i;
    1018       32138 :   if (lz == 2)
    1019             :   { /* scalarpol(gneg(x),varn(y)) optimized */
    1020        3864 :     long v = varn(y);
    1021        3864 :     if (isrationalzero(x)) return pol_0(v);
    1022          14 :     z = cgetg(3,t_POL);
    1023          28 :     z[1] = gequal0(x)? evalvarn(v)
    1024          14 :                    : evalvarn(v) | evalsigne(1);
    1025          14 :     gel(z,2) = gneg(x); return z;
    1026             :   }
    1027       28274 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1028       28274 :   gel(z,2) = gsub(gel(y,2),x);
    1029       28274 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1030       28274 :   return z = normalizepol_lg(z,lz);
    1031             : }
    1032             : GEN
    1033      305096 : Rg_RgX_sub(GEN x, GEN y)
    1034             : {
    1035             :   GEN z;
    1036      305096 :   long lz = lg(y), i;
    1037      305096 :   if (lz == 2) return scalarpol(x,varn(y));
    1038      304081 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1039      304081 :   gel(z,2) = gsub(x, gel(y,2));
    1040      304081 :   for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
    1041      304081 :   return z = normalizepol_lg(z,lz);
    1042             : }
    1043             : /*******************************************************************/
    1044             : /*                                                                 */
    1045             : /*                  KARATSUBA MULTIPLICATION                       */
    1046             : /*                                                                 */
    1047             : /*******************************************************************/
    1048             : #if 0
    1049             : /* to debug Karatsuba-like routines */
    1050             : GEN
    1051             : zx_debug_spec(GEN x, long nx)
    1052             : {
    1053             :   GEN z = cgetg(nx+2,t_POL);
    1054             :   long i;
    1055             :   for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
    1056             :   z[1] = evalsigne(1); return z;
    1057             : }
    1058             : 
    1059             : GEN
    1060             : RgX_debug_spec(GEN x, long nx)
    1061             : {
    1062             :   GEN z = cgetg(nx+2,t_POL);
    1063             :   long i;
    1064             :   for (i=0; i<nx; i++) z[i+2] = x[i];
    1065             :   z[1] = evalsigne(1); return z;
    1066             : }
    1067             : #endif
    1068             : 
    1069             : /* generic multiplication */
    1070             : 
    1071             : static GEN
    1072     2820027 : addpol(GEN x, GEN y, long lx, long ly)
    1073             : {
    1074             :   long i,lz;
    1075             :   GEN z;
    1076             : 
    1077     2820027 :   if (ly>lx) swapspec(x,y, lx,ly);
    1078     2820027 :   lz = lx+2; z = cgetg(lz,t_POL) + 2;
    1079     2820184 :   for (i=0; i<ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1080     2820019 :   for (   ; i<lx; i++) gel(z,i) = gel(x,i);
    1081     2820019 :   z -= 2; z[1]=0; return normalizepol_lg(z, lz);
    1082             : }
    1083             : 
    1084             : static GEN
    1085      292961 : addpolcopy(GEN x, GEN y, long lx, long ly)
    1086             : {
    1087             :   long i,lz;
    1088             :   GEN z;
    1089             : 
    1090      292961 :   if (ly>lx) swapspec(x,y, lx,ly);
    1091      292961 :   lz = lx+2; z = cgetg(lz,t_POL) + 2;
    1092      292986 :   for (i=0; i<ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1093      292964 :   for (   ; i<lx; i++) gel(z,i) = gcopy(gel(x,i));
    1094      292964 :   z -= 2; z[1]=0; return normalizepol_lg(z, lz);
    1095             : }
    1096             : 
    1097             : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
    1098             :  * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
    1099             :  * t_VECSMALL, to hold this, which can be left on stack: gerepile
    1100             :  * will not crash on it. The returned vector itself is not a proper GEN,
    1101             :  * we access the coefficients as x[i], i = 0..deg(x) */
    1102             : static GEN
    1103    30326943 : RgXspec_kill0(GEN x, long lx)
    1104             : {
    1105    30326943 :   GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit gerepile-wise */
    1106             :   long i;
    1107   128707943 :   for (i=0; i <lx; i++)
    1108             :   {
    1109    98381039 :     GEN c = gel(x,i);
    1110    98381039 :     z[i] = (long)(isrationalzero(c)? NULL: c);
    1111             :   }
    1112    30326904 :   return z;
    1113             : }
    1114             : 
    1115             : INLINE GEN
    1116    70592003 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
    1117             : {
    1118    70592003 :   pari_sp av = avma;
    1119    70592003 :   GEN s = NULL;
    1120             :   long i;
    1121             : 
    1122   277930651 :   for (i=a; i<b; i++)
    1123   207342738 :     if (gel(y,i) && gel(x,-i))
    1124             :     {
    1125   158764659 :       GEN t = gmul(gel(y,i), gel(x,-i));
    1126   158770402 :       s = s? gadd(s, t): t;
    1127             :     }
    1128    70587913 :   return s? gerepileupto(av, s): gen_0;
    1129             : }
    1130             : 
    1131             : /* assume nx >= ny > 0, return x * y * t^v */
    1132             : static GEN
    1133    11975254 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
    1134             : {
    1135             :   long i, lz, nz;
    1136             :   GEN z;
    1137             : 
    1138    11975254 :   x = RgXspec_kill0(x,nx);
    1139    11975248 :   y = RgXspec_kill0(y,ny);
    1140    11975241 :   lz = nx + ny + 1; nz = lz-2;
    1141    11975241 :   lz += v;
    1142    11975241 :   z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
    1143    11975287 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1144    11975287 :   for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
    1145    11975190 :   for (  ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
    1146    11975182 :   for (  ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
    1147    11975252 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1148             : }
    1149             : 
    1150             : /* return (x * X^d) + y. Assume d > 0 */
    1151             : GEN
    1152     1870451 : addmulXn(GEN x, GEN y, long d)
    1153             : {
    1154             :   GEN xd, yd, zd;
    1155             :   long a, lz, nx, ny;
    1156             : 
    1157     1870451 :   if (!signe(x)) return y;
    1158     1804647 :   ny = lgpol(y);
    1159     1804647 :   nx = lgpol(x);
    1160     1804647 :   zd = (GEN)avma;
    1161     1804647 :   x += 2; y += 2; a = ny-d;
    1162     1804647 :   if (a <= 0)
    1163             :   {
    1164      141463 :     lz = nx+d+2;
    1165      141463 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1166      141465 :     while (xd > x) gel(--zd,0) = gel(--xd,0);
    1167      141465 :     x = zd + a;
    1168      141465 :     while (zd > x) gel(--zd,0) = gen_0;
    1169             :   }
    1170             :   else
    1171             :   {
    1172     1663184 :     xd = new_chunk(d); yd = y+d;
    1173     1663184 :     x = addpol(x,yd, nx,a);
    1174     1663181 :     lz = (a>nx)? ny+2: lg(x)+d;
    1175     1663181 :     x += 2; while (xd > x) *--zd = *--xd;
    1176             :   }
    1177     1804646 :   while (yd > y) *--zd = *--yd;
    1178     1804646 :   *--zd = evalsigne(1);
    1179     1804646 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1180             : }
    1181             : 
    1182             : GEN
    1183      114380 : addshiftpol(GEN x, GEN y, long d)
    1184             : {
    1185      114380 :   long v = varn(x);
    1186      114380 :   x = addmulXn(x,y,d);
    1187      114380 :   setvarn(x,v); return x;
    1188             : }
    1189             : 
    1190             : /* as above, producing a clean malloc */
    1191             : static GEN
    1192      585402 : addmulXncopy(GEN x, GEN y, long d)
    1193             : {
    1194             :   GEN xd, yd, zd;
    1195             :   long a, lz, nx, ny;
    1196             : 
    1197      585402 :   if (!signe(x)) return RgX_copy(y);
    1198      585382 :   nx = lgpol(x);
    1199      585382 :   ny = lgpol(y);
    1200      585382 :   zd = (GEN)avma;
    1201      585382 :   x += 2; y += 2; a = ny-d;
    1202      585382 :   if (a <= 0)
    1203             :   {
    1204      292420 :     lz = nx+d+2;
    1205      292420 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1206      292426 :     while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
    1207      292426 :     x = zd + a;
    1208      292426 :     while (zd > x) gel(--zd,0) = gen_0;
    1209             :   }
    1210             :   else
    1211             :   {
    1212      292962 :     xd = new_chunk(d); yd = y+d;
    1213      292961 :     x = addpolcopy(x,yd, nx,a);
    1214      292963 :     lz = (a>nx)? ny+2: lg(x)+d;
    1215      292963 :     x += 2; while (xd > x) *--zd = *--xd;
    1216             :   }
    1217      585389 :   while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
    1218      585382 :   *--zd = evalsigne(1);
    1219      585382 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1220             : }
    1221             : 
    1222             : /* return x * y mod t^n */
    1223             : static GEN
    1224     3062832 : RgXn_mul_basecase(GEN x, GEN y, long n)
    1225             : {
    1226     3062832 :   long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
    1227             :   GEN z;
    1228     3062832 :   if (lx < 0) return pol_0(varn(x));
    1229     3062832 :   if (ly < 0) return pol_0(varn(x));
    1230     3062832 :   z = cgetg(lz, t_POL) + 2;
    1231     3062832 :   x+=2; if (lx > n) lx = n;
    1232     3062832 :   y+=2; if (ly > n) ly = n;
    1233     3062832 :   z[-1] = x[-1];
    1234     3062832 :   if (ly > lx) { swap(x,y); lswap(lx,ly); }
    1235     3062832 :   x = RgXspec_kill0(x, lx);
    1236     3062832 :   y = RgXspec_kill0(y, ly);
    1237             :   /* x:y:z [i] = term of degree i */
    1238     3062832 :   for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
    1239     3062832 :   for (  ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
    1240     3062832 :   for (  ; i<n; i++)  gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
    1241     3062832 :   return normalizepol_lg(z - 2, lz);
    1242             : }
    1243             : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
    1244             : GEN
    1245     3520625 : RgXn_mul(GEN f, GEN g, long n)
    1246             : {
    1247     3520625 :   pari_sp av = avma;
    1248             :   GEN fe,fo, ge,go, l,h,m;
    1249             :   long n0, n1;
    1250     3520625 :   if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
    1251     3063063 :   if (n < 80) return RgXn_mul_basecase(f,g,n);
    1252         231 :   n0 = n>>1; n1 = n-n0;
    1253         231 :   RgX_even_odd(f, &fe, &fo);
    1254         231 :   RgX_even_odd(g, &ge, &go);
    1255         231 :   l = RgXn_mul(fe,ge,n1);
    1256         231 :   h = RgXn_mul(fo,go,n0);
    1257         231 :   m = RgX_sub(RgXn_mul(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
    1258             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1259             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1260         231 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1261             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1262         231 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1263         231 :   m = RgX_inflate(m,2);
    1264             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1265         231 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1266         231 :   h = RgX_inflate(h,2);
    1267         231 :   h = addmulXncopy(addmulXn(h,m,1), l,1);
    1268         231 :   setvarn(h, varn(f)); return gerepileupto(av, h);
    1269             : }
    1270             : 
    1271             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
    1272             :  * b+2 were sent instead. na, nb = number of terms of a, b.
    1273             :  * Only c, c0, c1, c2 are genuine GEN.
    1274             :  */
    1275             : GEN
    1276    12629774 : RgX_mulspec(GEN a, GEN b, long na, long nb)
    1277             : {
    1278             :   GEN a0, c, c0;
    1279    12629774 :   long n0, n0a, i, v = 0;
    1280             :   pari_sp av;
    1281             : 
    1282    12629774 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
    1283    12629771 :   while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
    1284    12629768 :   if (na < nb) swapspec(a,b, na,nb);
    1285    12629768 :   if (!nb) return pol_0(0);
    1286             : 
    1287    12559943 :   if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
    1288      584689 :   RgX_shift_inplace_init(v);
    1289      584689 :   i = (na>>1); n0 = na-i; na = i;
    1290      584689 :   av = avma; a0 = a+n0; n0a = n0;
    1291      584689 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1292             : 
    1293      584688 :   if (nb > n0)
    1294             :   {
    1295             :     GEN b0,c1,c2;
    1296             :     long n0b;
    1297             : 
    1298      578427 :     nb -= n0; b0 = b+n0; n0b = n0;
    1299      578427 :     while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
    1300      578427 :     c = RgX_mulspec(a,b,n0a,n0b);
    1301      578428 :     c0 = RgX_mulspec(a0,b0, na,nb);
    1302             : 
    1303      578427 :     c2 = addpol(a0,a, na,n0a);
    1304      578428 :     c1 = addpol(b0,b, nb,n0b);
    1305             : 
    1306      578426 :     c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
    1307      578426 :     c2 = RgX_sub(c1, RgX_add(c0,c));
    1308      578431 :     c0 = addmulXn(c0, c2, n0);
    1309             :   }
    1310             :   else
    1311             :   {
    1312        6261 :     c = RgX_mulspec(a,b,n0a,nb);
    1313        6261 :     c0 = RgX_mulspec(a0,b,na,nb);
    1314             :   }
    1315      584690 :   c0 = addmulXncopy(c0,c,n0);
    1316      584690 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1317             : }
    1318             : 
    1319             : INLINE GEN
    1320     2704210 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
    1321             : {
    1322     2704210 :   pari_sp av = avma;
    1323     2704210 :   GEN s = NULL;
    1324     2704210 :   long j, l = (i+1)>>1;
    1325    10061430 :   for (j=a; j<l; j++)
    1326             :   {
    1327     7359678 :     GEN xj = gel(x,j), xx = gel(x,i-j);
    1328     7359678 :     if (xj && xx)
    1329             :     {
    1330     4746291 :       GEN t = gmul(xj, xx);
    1331     4749506 :       s = s? gadd(s, t): t;
    1332             :     }
    1333             :   }
    1334     2701752 :   if (s) s = gshift(s,1);
    1335     2701930 :   if ((i&1) == 0)
    1336             :   {
    1337     1476442 :     GEN t = gel(x, i>>1);
    1338     1476442 :     if (t) {
    1339     1232481 :       t = gsqr(t);
    1340     1232493 :       s = s? gadd(s, t): t;
    1341             :     }
    1342             :   }
    1343     2701901 :   return s? gerepileupto(av,s): gen_0;
    1344             : }
    1345             : static GEN
    1346      250139 : RgX_sqrspec_basecase(GEN x, long nx, long v)
    1347             : {
    1348             :   long i, lz, nz;
    1349             :   GEN z;
    1350             : 
    1351      250139 :   if (!nx) return pol_0(0);
    1352      250132 :   x = RgXspec_kill0(x,nx);
    1353      250129 :   lz = (nx << 1) + 1, nz = lz-2;
    1354      250129 :   lz += v;
    1355      250129 :   z = cgetg(lz,t_POL) + 2;
    1356      250140 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1357      250140 :   for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1358      250125 :   for (  ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
    1359      250131 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1360             : }
    1361             : /* return x^2 mod t^n */
    1362             : static GEN
    1363         665 : RgXn_sqr_basecase(GEN x, long n)
    1364             : {
    1365         665 :   long i, lz = n+2, lx = lgpol(x);
    1366             :   GEN z;
    1367         665 :   if (lx < 0) return pol_0(varn(x));
    1368         665 :   z = cgetg(lz, t_POL);
    1369         665 :   z[1] = x[1];
    1370         665 :   x+=2; if (lx > n) lx = n;
    1371         665 :   x = RgXspec_kill0(x,lx);
    1372         665 :   z+=2;/* x:z [i] = term of degree i */
    1373         665 :   for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1374         665 :   for (  ; i<n; i++)  gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
    1375         665 :   z -= 2; return normalizepol_lg(z, lz);
    1376             : }
    1377             : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
    1378             : GEN
    1379        1967 : RgXn_sqr(GEN f, long n)
    1380             : {
    1381        1967 :   pari_sp av = avma;
    1382             :   GEN fe,fo, l,h,m;
    1383             :   long n0, n1;
    1384        1967 :   if (2*degpol(f) < n) return RgX_sqr(f);
    1385         693 :   if (n < 80) return RgXn_sqr_basecase(f,n);
    1386          28 :   n0 = n>>1; n1 = n-n0;
    1387          28 :   RgX_even_odd(f, &fe, &fo);
    1388          28 :   l = RgXn_sqr(fe,n1);
    1389          28 :   h = RgXn_sqr(fo,n0);
    1390          28 :   m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
    1391             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1392             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1393          28 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1394             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1395          28 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1396          28 :   m = RgX_inflate(m,2);
    1397             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1398          28 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1399          28 :   h = RgX_inflate(h,2);
    1400          28 :   h = addmulXncopy(addmulXn(h,m,1), l,1);
    1401          28 :   setvarn(h, varn(f)); return gerepileupto(av, h);
    1402             : }
    1403             : 
    1404             : GEN
    1405      250593 : RgX_sqrspec(GEN a, long na)
    1406             : {
    1407             :   GEN a0, c, c0, c1;
    1408      250593 :   long n0, n0a, i, v = 0;
    1409             :   pari_sp av;
    1410             : 
    1411      250593 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
    1412      250592 :   if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
    1413         453 :   RgX_shift_inplace_init(v);
    1414         453 :   i = (na>>1); n0 = na-i; na = i;
    1415         453 :   av = avma; a0 = a+n0; n0a = n0;
    1416         453 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1417             : 
    1418         453 :   c = RgX_sqrspec(a,n0a);
    1419         453 :   c0 = RgX_sqrspec(a0,na);
    1420         453 :   c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
    1421         453 :   c0 = addmulXn(c0,c1, n0);
    1422         453 :   c0 = addmulXncopy(c0,c,n0);
    1423         453 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1424             : }
    1425             : 
    1426             : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
    1427             : GEN
    1428      415661 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
    1429             : {
    1430      415661 :   GEN z = RgX_mul(A, B);
    1431      415661 :   if (a < b)
    1432        5201 :     z = addmulXn(addmulXn(A, B, b-a), z, a);
    1433      410460 :   else if (a > b)
    1434      261091 :     z = addmulXn(addmulXn(B, A, a-b), z, b);
    1435             :   else
    1436      149369 :     z = addmulXn(RgX_add(A, B), z, a);
    1437      415661 :   setvarn(z,varn(A)); return z;
    1438             : }
    1439             : 
    1440             : GEN
    1441    10881519 : RgX_mul(GEN x, GEN y)
    1442             : {
    1443    10881519 :   GEN z = RgX_mulspec(y+2, x+2, lgpol(y), lgpol(x));
    1444    10881518 :   setvarn(z,varn(x)); return z;
    1445             : }
    1446             : 
    1447             : GEN
    1448      249686 : RgX_sqr(GEN x)
    1449             : {
    1450      249686 :   GEN z = RgX_sqrspec(x+2, lgpol(x));
    1451      249684 :   setvarn(z,varn(x)); return z;
    1452             : }
    1453             : 
    1454             : /*******************************************************************/
    1455             : /*                                                                 */
    1456             : /*                               DIVISION                          */
    1457             : /*                                                                 */
    1458             : /*******************************************************************/
    1459             : GEN
    1460      918885 : RgX_Rg_divexact(GEN x, GEN y) {
    1461             :   long i, lx;
    1462             :   GEN z;
    1463      918885 :   if (typ(y) == t_INT && is_pm1(y))
    1464       51747 :     return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1465      867138 :   z = cgetg_copy(x, &lx); z[1] = x[1];
    1466      867138 :   for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    1467      867138 :   return z;
    1468             : }
    1469             : GEN
    1470    21890879 : RgX_Rg_div(GEN x, GEN y) {
    1471             :   long i, lx;
    1472    21890879 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1473    21890879 :   for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
    1474    21890879 :   return normalizepol_lg(z, lx);
    1475             : }
    1476             : GEN
    1477        1456 : RgX_normalize(GEN x)
    1478             : {
    1479        1456 :   GEN d = NULL;
    1480        1456 :   long i, n = lg(x)-1;
    1481        1456 :   for (i = n; i > 1; i--)
    1482             :   {
    1483        1456 :     d = gel(x,i);
    1484        1456 :     if (!gequal0(d)) break;
    1485             :   }
    1486        1456 :   if (i == 1) return pol_0(varn(x));
    1487        1456 :   if (i == n && isint1(d)) return x;
    1488         287 :   return normalizepol_lg(RgX_Rg_div(x, d), i+1);
    1489             : }
    1490             : GEN
    1491        1729 : RgX_divs(GEN x, long y) {
    1492             :   long i, lx;
    1493        1729 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1494        1729 :   for (i=2; i<lx; i++) gel(z,i) = gdivgs(gel(x,i),y);
    1495        1729 :   return normalizepol_lg(z, lx);
    1496             : }
    1497             : GEN
    1498       27457 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
    1499             : {
    1500       27457 :   long l = lg(a), i;
    1501       27457 :   GEN a0, z0, z = cgetg(l-1, t_POL);
    1502       27457 :   z[1] = a[1];
    1503       27457 :   a0 = a + l-1;
    1504       27457 :   z0 = z + l-2; *z0 = *a0--;
    1505      695059 :   for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1506             :   {
    1507      667602 :     GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
    1508      667602 :     gel(z0,0) = t;
    1509             :   }
    1510       27457 :   if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
    1511       27457 :   return z;
    1512             : }
    1513             : /* Polynomial division x / y:
    1514             :  *   if z = ONLY_REM  return remainder, otherwise return quotient
    1515             :  *   if z != NULL set *z to remainder
    1516             :  *   *z is the last object on stack (and thus can be disposed of with cgiv
    1517             :  *   instead of gerepile) */
    1518             : /* assume, typ(x) = typ(y) = t_POL, same variable */
    1519             : GEN
    1520    12649707 : RgX_divrem(GEN x, GEN y, GEN *pr)
    1521             : {
    1522             :   pari_sp avy, av, av1;
    1523             :   long dx,dy,dz,i,j,sx,lr;
    1524             :   GEN z,p1,p2,rem,y_lead,mod;
    1525             :   GEN (*f)(GEN,GEN);
    1526             : 
    1527    12649707 :   if (!signe(y)) pari_err_INV("RgX_divrem",y);
    1528             : 
    1529    12649707 :   dy = degpol(y);
    1530    12649830 :   y_lead = gel(y,dy+2);
    1531    12649830 :   if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
    1532             :   {
    1533           0 :     pari_warn(warner,"normalizing a polynomial with 0 leading term");
    1534           0 :     for (dy--; dy>=0; dy--)
    1535             :     {
    1536           0 :       y_lead = gel(y,dy+2);
    1537           0 :       if (!gequal0(y_lead)) break;
    1538             :     }
    1539             :   }
    1540    12649653 :   if (!dy) /* y is constant */
    1541             :   {
    1542       55499 :     if (pr == ONLY_REM) return pol_0(varn(x));
    1543       54890 :     z = RgX_Rg_div(x, y_lead);
    1544       54890 :     if (pr == ONLY_DIVIDES) return z;
    1545       54211 :     if (pr) *pr = pol_0(varn(x));
    1546       54211 :     return z;
    1547             :   }
    1548    12594154 :   dx = degpol(x);
    1549    12594157 :   if (dx < dy)
    1550             :   {
    1551      894552 :     if (pr == ONLY_REM) return RgX_copy(x);
    1552      305572 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1553      305551 :     z = pol_0(varn(x));
    1554      305551 :     if (pr) *pr = RgX_copy(x);
    1555      305551 :     return z;
    1556             :   }
    1557             : 
    1558             :   /* x,y in R[X], y non constant */
    1559    11699605 :   av = avma;
    1560    11699605 :   switch(typ(y_lead))
    1561             :   {
    1562             :     case t_REAL:
    1563           0 :       y_lead = ginv(y_lead);
    1564           0 :       f = gmul; mod = NULL;
    1565           0 :       break;
    1566             :     case t_INTMOD:
    1567        4535 :     case t_POLMOD: y_lead = ginv(y_lead);
    1568        4535 :       f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
    1569        4535 :       break;
    1570    11695070 :     default: if (gequal1(y_lead)) y_lead = NULL;
    1571    11695177 :       f = gdiv; mod = NULL;
    1572             :   }
    1573             : 
    1574    11699712 :   if (y_lead == NULL)
    1575     9929175 :     p2 = gel(x,dx+2);
    1576             :   else {
    1577             :     for(;;) {
    1578     1770537 :       p2 = f(gel(x,dx+2),y_lead);
    1579     1770743 :       p2 = simplify_shallow(p2);
    1580     1770743 :       if (!isexactzero(p2) || (--dx < 0)) break;
    1581           0 :     }
    1582     1770743 :     if (dx < dy) /* leading coeff of x was in fact zero */
    1583             :     {
    1584           0 :       if (pr == ONLY_DIVIDES) {
    1585           0 :         avma = av;
    1586           0 :         return (dx < 0)? pol_0(varn(x)) : NULL;
    1587             :       }
    1588           0 :       if (pr == ONLY_REM)
    1589             :       {
    1590           0 :         if (dx < 0)
    1591           0 :           return gerepilecopy(av, scalarpol(p2, varn(x)));
    1592             :         else
    1593             :         {
    1594             :           GEN t;
    1595           0 :           avma = av;
    1596           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1597           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1598           0 :           return t;
    1599             :         }
    1600             :       }
    1601           0 :       if (pr) /* cf ONLY_REM above */
    1602             :       {
    1603           0 :         if (dx < 0)
    1604             :         {
    1605           0 :           p2 = gclone(p2);
    1606           0 :           avma = av;
    1607           0 :           z = pol_0(varn(x));
    1608           0 :           x = scalarpol(p2, varn(x));
    1609           0 :           gunclone(p2);
    1610             :         }
    1611             :         else
    1612             :         {
    1613             :           GEN t;
    1614           0 :           avma = av;
    1615           0 :           z = pol_0(varn(x));
    1616           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1617           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1618           0 :           x = t;
    1619             :         }
    1620           0 :         *pr = x;
    1621             :       }
    1622             :       else
    1623             :       {
    1624           0 :         avma = av;
    1625           0 :         z = pol_0(varn(x));
    1626             :       }
    1627           0 :       return z;
    1628             :     }
    1629             :   }
    1630             :   /* dx >= dy */
    1631    11699918 :   avy = avma;
    1632    11699918 :   dz = dx-dy;
    1633    11699918 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1634    11699647 :   x += 2;
    1635    11699647 :   z += 2;
    1636    11699647 :   y += 2;
    1637    11699647 :   gel(z,dz) = gcopy(p2);
    1638             : 
    1639    33549223 :   for (i=dx-1; i>=dy; i--)
    1640             :   {
    1641    21849059 :     av1=avma; p1=gel(x,i);
    1642    21849059 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1643    21798581 :     if (y_lead) p1 = simplify(f(p1,y_lead));
    1644             : 
    1645    21798581 :     if (isrationalzero(p1)) { avma=av1; p1 = gen_0; }
    1646             :     else
    1647    12504634 :       p1 = avma==av1? gcopy(p1): gerepileupto(av1,p1);
    1648    21849018 :     gel(z,i-dy) = p1;
    1649             :   }
    1650    11700164 :   if (!pr) return gerepileupto(av,z-2);
    1651             : 
    1652     5658227 :   rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
    1653     6205259 :   for (sx=0; ; i--)
    1654             :   {
    1655     6205259 :     p1 = gel(x,i);
    1656             :     /* we always enter this loop at least once */
    1657     6205259 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1658     6203878 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1659     6203878 :     if (!gequal0(p1)) { sx = 1; break; } /* remainder is non-zero */
    1660     3606197 :     if (!isexactzero(p1)) break;
    1661     3599676 :     if (!i) break;
    1662      547103 :     avma=av1;
    1663      547103 :   }
    1664     5658053 :   if (pr == ONLY_DIVIDES)
    1665             :   {
    1666         693 :     if (sx) { avma=av; return NULL; }
    1667         686 :     avma = (pari_sp)rem;
    1668         686 :     return gerepileupto(av,z-2);
    1669             :   }
    1670     5657360 :   lr=i+3; rem -= lr;
    1671     5657360 :   if (avma==av1) { avma = (pari_sp)rem; p1 = gcopy(p1); }
    1672     5607510 :   else p1 = gerepileupto((pari_sp)rem,p1);
    1673     5657466 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1674     5657133 :   rem[1] = z[-1];
    1675     5657133 :   rem += 2;
    1676     5657133 :   gel(rem,i) = p1;
    1677    14615114 :   for (i--; i>=0; i--)
    1678             :   {
    1679     8957588 :     av1=avma; p1 = gel(x,i);
    1680     8957588 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1681     8847053 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1682     8953278 :     gel(rem,i) = avma==av1? gcopy(p1):gerepileupto(av1,p1);
    1683             :   }
    1684     5657526 :   rem -= 2;
    1685     5657526 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1686     5657523 :   if (pr == ONLY_REM) return gerepileupto(av,rem);
    1687     3830943 :   z -= 2;
    1688             :   {
    1689     3830943 :     GEN *gptr[2]; gptr[0]=&z; gptr[1]=&rem;
    1690     3830943 :     gerepilemanysp(av,avy,gptr,2); *pr = rem; return z;
    1691             :   }
    1692             : }
    1693             : 
    1694             : /* x and y in (R[Y]/T)[X]  (lifted), T in R[Y]. y preferably monic */
    1695             : GEN
    1696       20725 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
    1697             : {
    1698             :   long vx, dx, dy, dz, i, j, sx, lr;
    1699             :   pari_sp av0, av, tetpil;
    1700             :   GEN z,p1,rem,lead;
    1701             : 
    1702       20725 :   if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
    1703       20725 :   vx = varn(x);
    1704       20725 :   dx = degpol(x);
    1705       20725 :   dy = degpol(y);
    1706       20725 :   if (dx < dy)
    1707             :   {
    1708        1414 :     if (pr)
    1709             :     {
    1710        1414 :       av0 = avma; x = RgXQX_red(x, T);
    1711        1414 :       if (pr == ONLY_DIVIDES) { avma=av0; return signe(x)? NULL: gen_0; }
    1712        1414 :       if (pr == ONLY_REM) return x;
    1713           0 :       *pr = x;
    1714             :     }
    1715           0 :     return pol_0(vx);
    1716             :   }
    1717       19311 :   lead = leading_coeff(y);
    1718       19311 :   if (!dy) /* y is constant */
    1719             :   {
    1720           7 :     if (pr && pr != ONLY_DIVIDES)
    1721             :     {
    1722           0 :       if (pr == ONLY_REM) return pol_0(vx);
    1723           0 :       *pr = pol_0(vx);
    1724             :     }
    1725           7 :     if (gequal1(lead)) return RgX_copy(x);
    1726           0 :     av0 = avma; x = gmul(x, ginvmod(lead,T)); tetpil = avma;
    1727           0 :     return gerepile(av0,tetpil,RgXQX_red(x,T));
    1728             :   }
    1729       19304 :   av0 = avma; dz = dx-dy;
    1730       19304 :   lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
    1731       19304 :   avma = av0;
    1732       19304 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1733       19304 :   x += 2; y += 2; z += 2;
    1734             : 
    1735       19304 :   p1 = gel(x,dx); av = avma;
    1736       19304 :   gel(z,dz) = lead? gerepileupto(av, grem(gmul(p1,lead), T)): gcopy(p1);
    1737      104193 :   for (i=dx-1; i>=dy; i--)
    1738             :   {
    1739       84889 :     av=avma; p1=gel(x,i);
    1740       84889 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1741       84889 :     if (lead) p1 = gmul(grem(p1, T), lead);
    1742       84889 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil, grem(p1, T));
    1743             :   }
    1744       19304 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
    1745             : 
    1746       19304 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
    1747       29457 :   for (sx=0; ; i--)
    1748             :   {
    1749       29457 :     p1 = gel(x,i);
    1750       29457 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1751       29457 :     tetpil=avma; p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
    1752       13966 :     if (!i) break;
    1753       10153 :     avma=av;
    1754       10153 :   }
    1755       19304 :   if (pr == ONLY_DIVIDES)
    1756             :   {
    1757        3484 :     if (lead) gunclone(lead);
    1758        3484 :     if (sx) { avma=av0; return NULL; }
    1759        3309 :     avma = (pari_sp)rem; return z-2;
    1760             :   }
    1761       15820 :   lr=i+3; rem -= lr;
    1762       15820 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1763       15820 :   rem[1] = z[-1];
    1764       15820 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
    1765       15820 :   rem += 2; gel(rem,i) = p1;
    1766       30580 :   for (i--; i>=0; i--)
    1767             :   {
    1768       14760 :     av=avma; p1 = gel(x,i);
    1769       36902 :     for (j=0; j<=i && j<=dz; j++)
    1770       22142 :       p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1771       14760 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, grem(p1, T));
    1772             :   }
    1773       15820 :   rem -= 2;
    1774       15820 :   if (lead) gunclone(lead);
    1775       15820 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1776       15820 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
    1777          42 :   *pr = rem; return z-2;
    1778             : }
    1779             : 
    1780             : /*******************************************************************/
    1781             : /*                                                                 */
    1782             : /*                        PSEUDO-DIVISION                          */
    1783             : /*                                                                 */
    1784             : /*******************************************************************/
    1785             : INLINE GEN
    1786      917592 : rem(GEN c, GEN T)
    1787             : {
    1788      917592 :   if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
    1789      917592 :   return c;
    1790             : }
    1791             : 
    1792             : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
    1793             : int
    1794        1099 : ZXQX_dvd(GEN x, GEN y, GEN T)
    1795             : {
    1796             :   long dx, dy, dz, i, p, T_ismonic;
    1797        1099 :   pari_sp av = avma, av2;
    1798             :   GEN y_lead;
    1799             : 
    1800        1099 :   if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
    1801        1099 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1802        1099 :   if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
    1803             :   /* if monic, no point in using pseudo-division */
    1804        1099 :   if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
    1805         637 :   T_ismonic = gequal1(leading_coeff(T));
    1806         637 :   dx = degpol(x);
    1807         637 :   if (dx < dy) return !signe(x);
    1808         637 :   (void)new_chunk(2);
    1809         637 :   x = RgX_recip_shallow(x)+2;
    1810         637 :   y = RgX_recip_shallow(y)+2;
    1811             :   /* pay attention to sparse divisors */
    1812        1400 :   for (i = 1; i <= dy; i++)
    1813         763 :     if (!signe(gel(y,i))) gel(y,i) = NULL;
    1814         637 :   dz = dx-dy; p = dz+1;
    1815         637 :   av2 = avma;
    1816             :   for (;;)
    1817             :   {
    1818        7147 :     GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
    1819        7147 :     x0 = gneg(x0); p--;
    1820        7147 :     m = gcdii(cx, y0);
    1821        7147 :     if (!equali1(m))
    1822             :     {
    1823        6405 :       x0 = gdiv(x0, m);
    1824        6405 :       y0 = diviiexact(y0, m);
    1825        6405 :       if (equali1(y0)) y0 = NULL;
    1826             :     }
    1827       15120 :     for (i=1; i<=dy; i++)
    1828             :     {
    1829        7973 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1830        7973 :       if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
    1831        7973 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1832        7973 :       gel(x,i) = c;
    1833             :     }
    1834       77287 :     for (   ; i<=dx; i++)
    1835             :     {
    1836       70140 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1837       70140 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1838       70140 :       gel(x,i) = c;
    1839             :     }
    1840        7924 :     do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
    1841        7147 :     if (dx < dy) break;
    1842        6510 :     if (gc_needed(av2,1))
    1843             :     {
    1844           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
    1845           0 :       gerepilecoeffs(av2,x,dx+1);
    1846             :     }
    1847        6510 :   }
    1848         637 :   avma = av; return (dx < 0);
    1849             : }
    1850             : 
    1851             : /* T either NULL or a t_POL. */
    1852             : GEN
    1853       69210 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
    1854             : {
    1855       69210 :   long vx = varn(x), dx, dy, dz, i, lx, p;
    1856       69210 :   pari_sp av = avma, av2;
    1857             :   GEN y_lead;
    1858             : 
    1859       69210 :   if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
    1860       69210 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1861             :   /* if monic, no point in using pseudo-division */
    1862       69210 :   if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
    1863       65339 :   dx = degpol(x);
    1864       65339 :   if (dx < dy) return RgX_copy(x);
    1865       65339 :   (void)new_chunk(2);
    1866       65339 :   x = RgX_recip_shallow(x)+2;
    1867       65339 :   y = RgX_recip_shallow(y)+2;
    1868             :   /* pay attention to sparse divisors */
    1869      244705 :   for (i = 1; i <= dy; i++)
    1870      179366 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    1871       65339 :   dz = dx-dy; p = dz+1;
    1872       65339 :   av2 = avma;
    1873             :   for (;;)
    1874             :   {
    1875      173315 :     gel(x,0) = gneg(gel(x,0)); p--;
    1876      624058 :     for (i=1; i<=dy; i++)
    1877             :     {
    1878      450743 :       GEN c = gmul(y_lead, gel(x,i));
    1879      450743 :       if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
    1880      450743 :       gel(x,i) = rem(c, T);
    1881             :     }
    1882      445458 :     for (   ; i<=dx; i++)
    1883             :     {
    1884      272143 :       GEN c = gmul(y_lead, gel(x,i));
    1885      272143 :       gel(x,i) = rem(c, T);
    1886             :     }
    1887      180406 :     do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
    1888      173315 :     if (dx < dy) break;
    1889      107976 :     if (gc_needed(av2,1))
    1890             :     {
    1891           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
    1892           0 :       gerepilecoeffs(av2,x,dx+1);
    1893             :     }
    1894      107976 :   }
    1895       65339 :   if (dx < 0) return pol_0(vx);
    1896       63820 :   lx = dx+3; x -= 2;
    1897       63820 :   x[0] = evaltyp(t_POL) | evallg(lx);
    1898       63820 :   x[1] = evalsigne(1) | evalvarn(vx);
    1899       63820 :   x = RgX_recip_shallow(x);
    1900       63820 :   if (p)
    1901             :   { /* multiply by y[0]^p   [beware dummy vars from FpX_FpXY_resultant] */
    1902        1561 :     GEN t = y_lead;
    1903        1561 :     if (T && typ(t) == t_POL && varn(t) == varn(T))
    1904           0 :       t = RgXQ_powu(t, p, T);
    1905             :     else
    1906        1561 :       t = gpowgs(t, p);
    1907        5607 :     for (i=2; i<lx; i++)
    1908             :     {
    1909        4046 :       GEN c = gmul(gel(x,i), t);
    1910        4046 :       gel(x,i) = rem(c,T);
    1911             :     }
    1912        1561 :     if (!T) return gerepileupto(av, x);
    1913             :   }
    1914       62259 :   return gerepilecopy(av, x);
    1915             : }
    1916             : 
    1917             : GEN
    1918       69210 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
    1919             : 
    1920             : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
    1921             : GEN
    1922       24235 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
    1923             : {
    1924       24235 :   long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
    1925       24235 :   pari_sp av = avma, av2;
    1926             :   GEN z, r, ypow, y_lead;
    1927             : 
    1928       24235 :   if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
    1929       24235 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1930       24235 :   if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
    1931       17003 :   dx = degpol(x);
    1932       17003 :   if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
    1933       17003 :   if (dx == dy)
    1934             :   {
    1935          28 :     GEN x_lead = gel(x,lg(x)-1);
    1936          28 :     x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
    1937          28 :     y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
    1938          28 :     r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
    1939          28 :     *ptr = gerepileupto(av, r); return scalarpol(x_lead, vx);
    1940             :   }
    1941       16975 :   (void)new_chunk(2);
    1942       16975 :   x = RgX_recip_shallow(x)+2;
    1943       16975 :   y = RgX_recip_shallow(y)+2;
    1944             :   /* pay attention to sparse divisors */
    1945       73626 :   for (i = 1; i <= dy; i++)
    1946       56651 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    1947       16975 :   dz = dx-dy; p = dz+1;
    1948       16975 :   lz = dz+3;
    1949       16975 :   z = cgetg(lz, t_POL);
    1950       16975 :   z[1] = evalsigne(1) | evalvarn(vx);
    1951       16975 :   for (i = 2; i < lz; i++) gel(z,i) = gen_0;
    1952       16975 :   ypow = new_chunk(dz+1);
    1953       16975 :   gel(ypow,0) = gen_1;
    1954       16975 :   gel(ypow,1) = y_lead;
    1955       23050 :   for (i=2; i<=dz; i++)
    1956             :   {
    1957        6075 :     GEN c = gmul(gel(ypow,i-1), y_lead);
    1958        6075 :     gel(ypow,i) = rem(c,T);
    1959             :   }
    1960       16975 :   av2 = avma;
    1961       16975 :   for (iz=2;;)
    1962             :   {
    1963       35855 :     p--;
    1964       35855 :     gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
    1965      151177 :     for (i=1; i<=dy; i++)
    1966             :     {
    1967      115322 :       GEN c = gmul(y_lead, gel(x,i));
    1968      115322 :       if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
    1969      115322 :       gel(x,i) = rem(c, T);
    1970             :     }
    1971       69263 :     for (   ; i<=dx; i++)
    1972             :     {
    1973       33408 :       GEN c = gmul(y_lead, gel(x,i));
    1974       33408 :       gel(x,i) = rem(c,T);
    1975             :     }
    1976       35855 :     x++; dx--;
    1977       35855 :     while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
    1978       35855 :     if (dx < dy) break;
    1979       18880 :     if (gc_needed(av2,1))
    1980             :     {
    1981           0 :       GEN X = x-2;
    1982           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
    1983           0 :       X[0] = evaltyp(t_POL)|evallg(dx+3); X[1] = z[1]; /* hack */
    1984           0 :       gerepileall(av2,2, &X, &z); x = X+2;
    1985             :     }
    1986       18880 :   }
    1987       16975 :   while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
    1988       16975 :   if (dx < 0)
    1989          98 :     x = pol_0(vx);
    1990             :   else
    1991             :   {
    1992       16877 :     lx = dx+3; x -= 2;
    1993       16877 :     x[0] = evaltyp(t_POL) | evallg(lx);
    1994       16877 :     x[1] = evalsigne(1) | evalvarn(vx);
    1995       16877 :     x = RgX_recip_shallow(x);
    1996             :   }
    1997       16975 :   z = RgX_recip_shallow(z);
    1998       16975 :   r = x;
    1999       16975 :   if (p)
    2000             :   {
    2001        2615 :     GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
    2002        2615 :     if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
    2003             :   }
    2004       16975 :   gerepileall(av, 2, &z, &r);
    2005       16975 :   *ptr = r; return z;
    2006             : }
    2007             : GEN
    2008       24116 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
    2009       24116 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
    2010             : 
    2011             : GEN
    2012        1638 : RgXQX_mul(GEN x, GEN y, GEN T)
    2013             : {
    2014        1638 :   return RgXQX_red(RgX_mul(x,y), T);
    2015             : }
    2016             : GEN
    2017    63413960 : RgX_Rg_mul(GEN y, GEN x) {
    2018             :   long i, ly;
    2019    63413960 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2020    63413960 :   if (ly == 2) return z;
    2021    63356497 :   for (i = 2; i < ly; i++) gel(z,i) = gmul(x,gel(y,i));
    2022    63356490 :   return normalizepol_lg(z,ly);
    2023             : }
    2024             : GEN
    2025         231 : RgX_muls(GEN y, long x) {
    2026             :   long i, ly;
    2027         231 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2028         231 :   if (ly == 2) return z;
    2029         196 :   for (i = 2; i < ly; i++) gel(z,i) = gmulsg(x,gel(y,i));
    2030         196 :   return normalizepol_lg(z,ly);
    2031             : }
    2032             : GEN
    2033          28 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T)
    2034             : {
    2035          28 :   return RgXQX_red(RgX_Rg_mul(x,y), T);
    2036             : }
    2037             : GEN
    2038          42 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T)
    2039             : {
    2040          42 :   return RgXQV_red(RgV_Rg_mul(v,x), T);
    2041             : }
    2042             : 
    2043             : GEN
    2044           0 : RgXQX_sqr(GEN x, GEN T)
    2045             : {
    2046           0 :   return RgXQX_red(RgX_sqr(x), T);
    2047             : }
    2048             : 
    2049             : static GEN
    2050       69587 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
    2051             : static GEN
    2052           0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
    2053             : static GEN
    2054      190425 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
    2055             : static GEN
    2056       79739 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
    2057             : static GEN
    2058      114653 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
    2059             : static GEN
    2060      105973 : _one(void *data) { return pol_1(varn((GEN)data)); }
    2061             : static GEN
    2062         105 : _zero(void *data) { return pol_0(varn((GEN)data)); }
    2063             : static GEN
    2064       72660 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
    2065             : 
    2066             : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
    2067             :               _mul, _sqr, _one, _zero };
    2068             : 
    2069             : GEN
    2070           0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
    2071             : {
    2072           0 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
    2073             : }
    2074             : 
    2075             : GEN
    2076       44492 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
    2077             : {
    2078       44492 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2079       44492 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
    2080             : }
    2081             : 
    2082             : /* mod X^n */
    2083             : struct modXn {
    2084             :   long v; /* varn(X) */
    2085             :   long n;
    2086             : } ;
    2087             : static GEN
    2088        1785 : _sqrXn(void *data, GEN x) {
    2089        1785 :   struct modXn *S = (struct modXn*)data;
    2090        1785 :   return RgXn_sqr(x, S->n);
    2091             : }
    2092             : static GEN
    2093        1176 : _mulXn(void *data, GEN x, GEN y) {
    2094        1176 :   struct modXn *S = (struct modXn*)data;
    2095        1176 :   return RgXn_mul(x,y, S->n);
    2096             : }
    2097             : static GEN
    2098        1407 : _oneXn(void *data) {
    2099        1407 :   struct modXn *S = (struct modXn*)data;
    2100        1407 :   return pol_1(S->v);
    2101             : }
    2102             : static GEN
    2103           0 : _zeroXn(void *data) {
    2104           0 :   struct modXn *S = (struct modXn*)data;
    2105           0 :   return pol_0(S->v);
    2106             : }
    2107             : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
    2108             :                                           _oneXn, _zeroXn };
    2109             : 
    2110             : GEN
    2111         336 : RgXn_powers(GEN x, long m, long n)
    2112             : {
    2113         336 :   long d = degpol(x);
    2114         336 :   int use_sqr = (d<<1) >= n;
    2115             :   struct modXn S;
    2116         336 :   S.v = varn(x); S.n = n;
    2117         336 :   return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
    2118             : }
    2119             : 
    2120             : GEN
    2121        1505 : RgXn_powu_i(GEN x, ulong m, long n)
    2122             : {
    2123             :   struct modXn S;
    2124        1505 :   S.v = varn(x); S.n = n;
    2125        1505 :   return gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
    2126             : }
    2127             : GEN
    2128           0 : RgXn_powu(GEN x, ulong m, long n)
    2129             : {
    2130             :   struct modXn S;
    2131           0 :   S.v = varn(x); S.n = n;
    2132           0 :   return gen_powu(x, m, (void*)&S,_sqrXn,_mulXn);
    2133             : }
    2134             : 
    2135             : GEN
    2136         672 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
    2137             : {
    2138             :   struct modXn S;
    2139         672 :   S.v = varn(gel(x,2)); S.n = n;
    2140         672 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
    2141             : }
    2142             : 
    2143             : GEN
    2144           0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
    2145             : {
    2146           0 :   int use_sqr = 2*degpol(x) >= n;
    2147             :   struct modXn S;
    2148           0 :   S.v = varn(x); S.n = n;
    2149           0 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2150             : }
    2151             : 
    2152             : /* Q(x) mod t^n, x in R[t], n >= 1 */
    2153             : GEN
    2154        1442 : RgXn_eval(GEN Q, GEN x, long n)
    2155             : {
    2156        1442 :   long d = degpol(x);
    2157             :   int use_sqr;
    2158             :   struct modXn S;
    2159        1442 :   if (d == 1 && isrationalzero(gel(x,2)))
    2160             :   {
    2161        1435 :     GEN y = RgX_unscale(Q, gel(x,3));
    2162        1435 :     setvarn(y, varn(x)); return y;
    2163             :   }
    2164           7 :   S.v = varn(x);
    2165           7 :   S.n = n;
    2166           7 :   use_sqr = (d<<1) >= n;
    2167           7 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2168             : }
    2169             : 
    2170             : GEN
    2171         462 : RgXn_inv(GEN f, long e)
    2172             : {
    2173         462 :   pari_sp av = avma, av2;
    2174             :   ulong mask;
    2175             :   GEN W;
    2176         462 :   long v = varn(f), n=1;
    2177         462 :   if (signe(f)==0)
    2178           0 :     pari_err_INV("RgXn_inv",f);
    2179         462 :   W = scalarpol(ginv(gel(f,2)),v);
    2180         462 :   mask = quadratic_prec_mask(e);
    2181         462 :   av2 = avma;
    2182        2541 :   for (;mask>1;)
    2183             :   {
    2184             :     GEN u, fr;
    2185        1617 :     long n2 = n;
    2186        1617 :     n<<=1; if (mask & 1) n--;
    2187        1617 :     mask >>= 1;
    2188        1617 :     fr = RgXn_red_shallow(f, n);
    2189        1617 :     u = RgX_shift_shallow(RgXn_mul(W, fr, n), -n2);
    2190        1617 :     W = RgX_sub(W, RgX_shift_shallow(RgXn_mul(u, W, n-n2), n2));
    2191        1617 :     if (gc_needed(av2,2))
    2192             :     {
    2193           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
    2194           0 :       W = gerepileupto(av2, W);
    2195             :     }
    2196             :   }
    2197         462 :   return gerepileupto(av, W);
    2198             : }
    2199             : 
    2200             : GEN
    2201           0 : RgXn_exp(GEN h, long e)
    2202             : {
    2203           0 :   pari_sp av = avma, av2;
    2204           0 :   long v = varn(h), n=1;
    2205           0 :   GEN f = pol_1(v), g = pol_1(v);
    2206           0 :   ulong mask = quadratic_prec_mask(e);
    2207           0 :   av2 = avma;
    2208           0 :   if (signe(h)==0 || degpol(h)<1 || !gequal0(gel(h,2)))
    2209           0 :     pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
    2210           0 :   for (;mask>1;)
    2211             :   {
    2212             :     GEN q, w;
    2213           0 :     long n2 = n;
    2214           0 :     n<<=1; if (mask & 1) n--;
    2215           0 :     mask >>= 1;
    2216           0 :     g = RgX_sub(RgX_muls(g,2),RgXn_mul(f,RgXn_sqr(g,n2),n2));
    2217           0 :     q = RgX_deriv(RgXn_red_shallow(h,n2));
    2218           0 :     w = RgX_add(q, RgXn_mul(g, RgX_sub(RgX_deriv(f), RgXn_mul(f,q,n-1)),n-1));
    2219           0 :     f = RgX_add(f, RgXn_mul(f, RgX_sub(RgXn_red_shallow(h, n), RgX_integ(w)), n));
    2220           0 :     if (gc_needed(av2,2))
    2221             :     {
    2222           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_exp, e = %ld", n);
    2223           0 :       gerepileall(av2, 2, &f, &g);
    2224             :     }
    2225             :   }
    2226           0 :   return gerepileupto(av, f);
    2227             : }
    2228             : 
    2229             : GEN
    2230          84 : RgXn_reverse(GEN f, long e)
    2231             : {
    2232          84 :   pari_sp av = avma, av2;
    2233             :   ulong mask;
    2234             :   GEN fi, a, df, W, an;
    2235          84 :   long v = varn(f), n=1;
    2236          84 :   if (degpol(f)<1 || !gequal0(gel(f,2)))
    2237           0 :     pari_err_INV("serreverse",f);
    2238          84 :   fi = ginv(gel(f,3));
    2239          84 :   a = deg1pol_shallow(fi,gen_0,v);
    2240          84 :   if (e <= 2) return gerepilecopy(av, a);
    2241          84 :   W = scalarpol(fi,v);
    2242          84 :   df = RgX_deriv(f);
    2243          84 :   mask = quadratic_prec_mask(e);
    2244          84 :   av2 = avma;
    2245         504 :   for (;mask>1;)
    2246             :   {
    2247             :     GEN u, fa, fr;
    2248         336 :     long n2 = n, rt;
    2249         336 :     n<<=1; if (mask & 1) n--;
    2250         336 :     mask >>= 1;
    2251         336 :     fr = RgXn_red_shallow(f, n);
    2252         336 :     rt = brent_kung_optpow(degpol(fr), 4, 3);
    2253         336 :     an = RgXn_powers(a, rt, n);
    2254         336 :     if (n>1)
    2255             :     {
    2256         336 :       long n4 = (n2+1)>>1;
    2257         336 :       GEN dfr = RgXn_red_shallow(df, n2);
    2258         336 :       dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
    2259         336 :       u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
    2260         336 :       W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
    2261             :     }
    2262         336 :     fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
    2263         336 :     fa = RgX_shift(fa, -n2);
    2264         336 :     a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
    2265         336 :     if (gc_needed(av2,2))
    2266             :     {
    2267           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
    2268           0 :       gerepileall(av2, 2, &a, &W);
    2269             :     }
    2270             :   }
    2271          84 :   return gerepileupto(av, a);
    2272             : }
    2273             : 
    2274             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2275             : GEN
    2276      180508 : RgXQ_powu(GEN x, ulong n, GEN T)
    2277             : {
    2278             :   pari_sp av;
    2279             :   GEN y;
    2280             : 
    2281      180508 :   if (!n) return pol_1(varn(x));
    2282      178975 :   if (n == 1) return RgX_copy(x);
    2283      121141 :   av = avma;
    2284      121141 :   y = gen_powu(x, n, (void*)T, &_sqr, &_mul);
    2285      121141 :   return gerepileupto(av, y);
    2286             : }
    2287             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2288             : GEN
    2289        2477 : RgXQ_pow(GEN x, GEN n, GEN T)
    2290             : {
    2291             :   pari_sp av;
    2292        2477 :   long s = signe(n);
    2293             :   GEN y;
    2294             : 
    2295        2477 :   if (!s) return pol_1(varn(x));
    2296        2477 :   if (is_pm1(n) == 1)
    2297           0 :     return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
    2298        2477 :   av = avma;
    2299        2477 :   if (s < 0) x = RgXQ_inv(x, T);
    2300        2477 :   y = gen_pow(x, n, (void*)T, &_sqr, &_mul);
    2301        2477 :   return gerepileupto(av, y);
    2302             : }
    2303             : 
    2304             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2305             : GEN
    2306        1694 : RgXQ_powers(GEN x, long l, GEN T)
    2307             : {
    2308        1694 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2309        1694 :   return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
    2310             : }
    2311             : 
    2312             : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
    2313             : GEN
    2314        1386 : QXQ_powers(GEN a, long n, GEN T)
    2315             : {
    2316        1386 :   GEN den, v = RgXQ_powers(Q_remove_denom(a, &den), n, T);
    2317             :   /* den*a integral; v[i+1] = (den*a)^i in K */
    2318        1386 :   if (den)
    2319             :   { /* restore denominators */
    2320         847 :     GEN d = den;
    2321             :     long i;
    2322         847 :     gel(v,2) = a;
    2323        3283 :     for (i=3; i<=n+1; i++) {
    2324        2436 :       d = mulii(d,den);
    2325        2436 :       gel(v,i) = RgX_Rg_div(gel(v,i), d);
    2326             :     }
    2327             :   }
    2328        1386 :   return v;
    2329             : }
    2330             : 
    2331             : static GEN
    2332         812 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
    2333             : {
    2334         812 :   long l, i, m = 0;
    2335             :   GEN dz, z;
    2336         812 :   GEN V = cgetg_copy(v, &l);
    2337        2611 :   for (i = imin; i < l; i++)
    2338             :   {
    2339        1799 :     GEN c = gel(v, i);
    2340        1799 :     if (typ(c) == t_POL) m = maxss(m, degpol(c));
    2341             :   }
    2342         812 :   z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
    2343         812 :   for (i = 1; i < imin; i++) V[i] = v[i];
    2344        2611 :   for (i = imin; i < l; i++)
    2345             :   {
    2346        1799 :     GEN c = gel(v,i);
    2347        1799 :     if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
    2348        1799 :     gel(V,i) = c;
    2349             :   }
    2350         812 :   return V;
    2351             : }
    2352             : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
    2353             : GEN
    2354         770 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
    2355         770 : { return do_QXQ_eval(v, 1, a, T); }
    2356             : GEN
    2357          42 : QXX_QXQ_eval(GEN v, GEN a, GEN T)
    2358          42 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
    2359             : 
    2360             : GEN
    2361         287 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
    2362             : {
    2363         287 :   return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
    2364             : }
    2365             : 
    2366             : GEN
    2367          56 : RgXQ_minpoly_naive(GEN y, GEN P)
    2368             : {
    2369          56 :   pari_sp ltop=avma;
    2370          56 :   long n=lgpol(P);
    2371          56 :   GEN M=ker(RgXQ_matrix_pow(y,n,n,P));
    2372          56 :   M=content(RgM_to_RgXV(M,varn(P)));
    2373          56 :   return gerepileupto(ltop,M);
    2374             : }
    2375             : 
    2376             : GEN
    2377       32980 : RgXQ_norm(GEN x, GEN T)
    2378             : {
    2379             :   pari_sp av;
    2380       32980 :   long dx = degpol(x);
    2381             :   GEN L, y;
    2382             : 
    2383       32980 :   av = avma; y = resultant(T, x);
    2384       32980 :   L = leading_coeff(T);
    2385       32980 :   if (gequal1(L) || !signe(x)) return y;
    2386           0 :   return gerepileupto(av, gdiv(y, gpowgs(L, dx)));
    2387             : }
    2388             : 
    2389             : GEN
    2390       69776 : RgX_blocks(GEN P, long n, long m)
    2391             : {
    2392       69776 :   GEN z = cgetg(m+1,t_VEC);
    2393       69776 :   long i,j, k=2, l = lg(P);
    2394      401604 :   for(i=1; i<=m; i++)
    2395             :   {
    2396      331828 :     GEN zi = cgetg(n+2,t_POL);
    2397      331828 :     zi[1] = P[1];
    2398      331828 :     gel(z,i) = zi;
    2399     2015993 :     for(j=2; j<n+2; j++)
    2400     1684165 :       gel(zi, j) = k==l ? gen_0 : gel(P,k++);
    2401      331828 :     zi = RgX_renormalize_lg(zi, n+2);
    2402             :   }
    2403       69776 :   return z;
    2404             : }
    2405             : 
    2406             : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
    2407             : void
    2408       22615 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
    2409             : {
    2410       22615 :   long n = degpol(p), v = varn(p), n0, n1, i;
    2411             :   GEN p0, p1;
    2412             : 
    2413       45231 :   if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
    2414             : 
    2415       22616 :   n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
    2416       22616 :   p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
    2417       22616 :   p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
    2418      560357 :   for (i=0; i<n1; i++)
    2419             :   {
    2420      537741 :     p0[2+i] = p[2+(i<<1)];
    2421      537741 :     p1[2+i] = p[3+(i<<1)];
    2422             :   }
    2423       22616 :   if (n1 != n0)
    2424       15768 :     p0[2+i] = p[2+(i<<1)];
    2425       22616 :   *pe = normalizepol(p0);
    2426       22615 :   *po = normalizepol(p1);
    2427             : }
    2428             : 
    2429             : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
    2430             : GEN
    2431       40446 : RgX_splitting(GEN p, long k)
    2432             : {
    2433       40446 :   long n = degpol(p), v = varn(p), m, i, j, l;
    2434             :   GEN r;
    2435             : 
    2436       40446 :   m = n/k;
    2437       40446 :   r = cgetg(k+1,t_VEC);
    2438      222978 :   for(i=1; i<=k; i++)
    2439             :   {
    2440      182532 :     gel(r,i) = cgetg(m+3, t_POL);
    2441      182532 :     mael(r,i,1) = evalvarn(v)|evalsigne(1);
    2442             :   }
    2443      538244 :   for (j=1, i=0, l=2; i<=n; i++)
    2444             :   {
    2445      497798 :     gmael(r,j,l) = gel(p,2+i);
    2446      497798 :     if (j==k) { j=1; l++; } else j++;
    2447             :   }
    2448      222978 :   for(i=1; i<=k; i++)
    2449      182532 :     gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
    2450       40446 :   return r;
    2451             : }
    2452             : 
    2453             : /*******************************************************************/
    2454             : /*                                                                 */
    2455             : /*                        Kronecker form                           */
    2456             : /*                                                                 */
    2457             : /*******************************************************************/
    2458             : 
    2459             : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
    2460             :  * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
    2461             :  * normalized coefficients */
    2462             : GEN
    2463         189 : Kronecker_to_mod(GEN z, GEN T)
    2464             : {
    2465         189 :   long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
    2466         189 :   GEN x, t = cgetg(N,t_POL);
    2467         189 :   t[1] = T[1];
    2468         189 :   lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
    2469         189 :   x[1] = z[1];
    2470         189 :   T = RgX_copy(T);
    2471        4389 :   for (i=2; i<lx+2; i++, z+= N-2)
    2472             :   {
    2473        4200 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
    2474        4200 :     gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2475             :   }
    2476         189 :   N = (l-2) % (N-2) + 2;
    2477         189 :   for (j=2; j<N; j++) t[j] = z[j];
    2478         189 :   gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2479         189 :   return normalizepol_lg(x, i+1);
    2480             : }

Generated by: LCOV version 1.11