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 20459-9710128) Lines: 1279 1395 91.7 %
Date: 2017-04-28 05:33:48 Functions: 140 152 92.1 %
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     1680956 : brent_kung_optpow(long d, long n, long m)
      29             : {
      30             :   long p, r;
      31     1680956 :   long pold=1, rold=n*(d-1);
      32    10156673 :   for(p=2; p<=d; p++)
      33             :   {
      34     8475717 :     r = m*(p-1) + n*((d-1)/p);
      35     8475717 :     if (r<rold) { pold=p; rold=r; }
      36             :   }
      37     1680956 :   return pold;
      38             : }
      39             : 
      40             : static GEN
      41     8058516 : 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     8058516 :   pari_sp av = avma;
      45             :   long i;
      46     8058516 :   GEN z = cmul(E,P,a,ff->one(E));
      47     8058553 :   if (!z) z = gen_0;
      48    50808748 :   for (i=1; i<=n; i++)
      49             :   {
      50    42750242 :     GEN t = cmul(E,P,a+i,gel(V,i+1));
      51    42750571 :     if (t) {
      52    41674659 :       z = ff->add(E, z, t);
      53    41674048 :       if (gc_needed(av,2)) z = gerepileupto(av, z);
      54             :     }
      55             :   }
      56     8058506 :   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     5148546 : 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     5148546 :   pari_sp av = avma;
      69     5148546 :   long l = lg(V)-1;
      70             :   GEN z, u;
      71             : 
      72     5148546 :   if (d < 0) return ff->zero(E);
      73     4730899 :   if (d < l) return gerepileupto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
      74     1996222 :   if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
      75     1996222 :   d -= l;
      76     1996222 :   z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
      77     5323843 :   while (d >= l-1)
      78             :   {
      79     1331402 :     d -= l-1;
      80     1331402 :     u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
      81     1331391 :     z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
      82     1331399 :     if (gc_needed(av,2))
      83          48 :       z = gerepileupto(av, z);
      84             :   }
      85     1996222 :   u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
      86     1996222 :   z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
      87     1996217 :   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     1996217 :   return gerepileupto(av, ff->red(E,z));
      93             : }
      94             : 
      95             : GEN
      96      883278 : 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      883278 :   pari_sp av = avma;
     100             :   GEN z, V;
     101             :   long rtd;
     102      883278 :   if (d < 0) return ff->zero(E);
     103      883188 :   rtd = (long) sqrt((double)d);
     104      883188 :   V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
     105      883190 :   z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
     106      883190 :   return gerepileupto(av, z);
     107             : }
     108             : 
     109             : static GEN
     110      460980 : _gen_nored(void *E, GEN x) { (void)E; return x; }
     111             : static GEN
     112    17466858 : _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      464184 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
     117             : static GEN
     118      149148 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
     119             : static GEN
     120      469752 : _gen_one(void *E) { (void)E; return gen_1; }
     121             : static GEN
     122         144 : _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       27246 : _gen_cmul(void *E, GEN P, long a, GEN x)
     129       27246 : {(void)E; return gmul(gel(P,a+2), x);}
     130             : 
     131             : GEN
     132        8634 : RgX_RgV_eval(GEN Q, GEN x)
     133             : {
     134        8634 :   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         174 : RgXV_RgV_eval(GEN Q, GEN x)
     145             : {
     146         174 :   long i, l = lg(Q), vQ = gvar(Q);
     147         174 :   GEN v = cgetg(l, t_VEC);
     148       21150 :   for (i = 1; i < l; i++)
     149             :   {
     150       20976 :     GEN Qi = gel(Q, i);
     151       20976 :     gel(v, i) = typ(Qi)==t_POL && varn(Qi)==vQ? RgX_RgV_eval(Qi, x): gcopy(Qi);
     152             :   }
     153         174 :   return v;
     154             : }
     155             : 
     156             : const struct bb_algebra *
     157       60840 : get_Rg_algebra(void)
     158             : {
     159       60840 :   return &Rg_algebra;
     160             : }
     161             : 
     162             : /*******************************************************************/
     163             : /*                                                                 */
     164             : /*                         RgX                                     */
     165             : /*                                                                 */
     166             : /*******************************************************************/
     167             : 
     168             : long
     169     3413202 : RgX_equal(GEN x, GEN y)
     170             : {
     171     3413202 :   long i = lg(x);
     172             : 
     173     3413202 :   if (i != lg(y)) return 0;
     174    17618976 :   for (i--; i > 1; i--)
     175    14258022 :     if (!gequal(gel(x,i),gel(y,i))) return 0;
     176     3360954 :   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      487392 : RgX_get_1(GEN x)
     183             : {
     184             :   GEN p, T;
     185      487392 :   long i, lx, tx = RgX_type(x, &p, &T, &lx);
     186      487392 :   if (RgX_type_is_composite(tx))
     187        1038 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     188      487392 :   switch(tx)
     189             :   {
     190          42 :     case t_INTMOD: retmkintmod(gen_1, icopy(p));
     191           6 :     case t_PADIC: return cvtop(gen_1, p, lx);
     192          12 :     case t_FFELT: return FF_1(T);
     193      487332 :     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      109806 : RgX_get_0(GEN x)
     200             : {
     201             :   GEN p, T;
     202      109806 :   long i, lx, tx = RgX_type(x, &p, &T, &lx);
     203      109806 :   if (RgX_type_is_composite(tx))
     204       11514 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     205      109806 :   switch(tx)
     206             :   {
     207         210 :     case t_INTMOD: retmkintmod(gen_0, icopy(p));
     208           0 :     case t_PADIC: return cvtop(gen_0, p, lx);
     209         180 :     case t_FFELT: return FF_zero(T);
     210      109416 :     default: return gen_0;
     211             :   }
     212             : }
     213             : 
     214             : GEN
     215        1500 : QX_ZXQV_eval(GEN P, GEN V, GEN dV)
     216             : {
     217        1500 :   long i, n = degpol(P);
     218             :   GEN z, dz, dP;
     219        1500 :   if (n < 0) return gen_0;
     220        1500 :   P = Q_remove_denom(P, &dP);
     221        1500 :   z = gel(P,2); if (n == 0) return icopy(z);
     222         828 :   if (dV) z = mulii(dV, z); /* V[1] = dV */
     223         828 :   z = ZX_Z_add_shallow(ZX_Z_mul(gel(V,2),gel(P,3)), z);
     224         828 :   for (i=2; i<=n; i++) z = ZX_add(ZX_Z_mul(gel(V,i+1),gel(P,2+i)), z);
     225         828 :   dz = mul_denom(dP, dV);
     226         828 :   return dz? RgX_Rg_div(z, dz): z;
     227             : }
     228             : 
     229             : /* Return P(h * x), not memory clean */
     230             : GEN
     231        2874 : RgX_unscale(GEN P, GEN h)
     232             : {
     233        2874 :   long i, l = lg(P);
     234        2874 :   GEN hi = gen_1, Q = cgetg(l, t_POL);
     235        2874 :   Q[1] = P[1];
     236        2874 :   if (l == 2) return Q;
     237        2874 :   gel(Q,2) = gcopy(gel(P,2));
     238        7368 :   for (i=3; i<l; i++)
     239             :   {
     240        4494 :     hi = gmul(hi,h);
     241        4494 :     gel(Q,i) = gmul(gel(P,i), hi);
     242             :   }
     243        2874 :   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       12906 : ZX_unscale(GEN P, GEN h)
     248             : {
     249       12906 :   long i, l = lg(P);
     250       12906 :   GEN Q = cgetg(l, t_POL);
     251       12906 :   Q[1] = P[1];
     252       12906 :   if (l == 2) return Q;
     253       12906 :   gel(Q,2) = gel(P,2);
     254       12906 :   if (l == 3) return Q;
     255       12906 :   if (equalim1(h))
     256      254700 :     for (i=3; i<l; i++)
     257             :     {
     258      251190 :       gel(Q,i) = negi(gel(P,i));
     259      251190 :       if (++i == l) break;
     260      248934 :       gel(Q,i) = gel(P,i);
     261             :     }
     262             :   else
     263             :   {
     264        7140 :     GEN hi = h;
     265        7140 :     gel(Q,3) = mulii(gel(P,3), hi);
     266       41418 :     for (i=4; i<l; i++)
     267             :     {
     268       34278 :       hi = mulii(hi,h);
     269       34278 :       gel(Q,i) = mulii(gel(P,i), hi);
     270             :     }
     271             :   }
     272       12906 :   return Q;
     273             : }
     274             : /* P a ZX. Return P(x << n), not memory clean */
     275             : GEN
     276        8358 : ZX_unscale2n(GEN P, long n)
     277             : {
     278        8358 :   long i, ni = n, l = lg(P);
     279        8358 :   GEN Q = cgetg(l, t_POL);
     280        8358 :   Q[1] = P[1];
     281        8358 :   if (l == 2) return Q;
     282        8358 :   gel(Q,2) = gel(P,2);
     283        8358 :   if (l == 3) return Q;
     284        8358 :   gel(Q,3) = shifti(gel(P,3), ni);
     285       42048 :   for (i=4; i<l; i++)
     286             :   {
     287       33690 :     ni += n;
     288       33690 :     gel(Q,i) = shifti(gel(P,i), ni);
     289             :   }
     290        8358 :   return Q;
     291             : }
     292             : /* P(h*X) / h, assuming h | P(0), i.e. the result is a ZX */
     293             : GEN
     294         138 : ZX_unscale_div(GEN P, GEN h)
     295             : {
     296         138 :   long i, l = lg(P);
     297         138 :   GEN hi, Q = cgetg(l, t_POL);
     298         138 :   Q[1] = P[1];
     299         138 :   if (l == 2) return Q;
     300         138 :   gel(Q,2) = diviiexact(gel(P,2), h);
     301         138 :   if (l == 3) return Q;
     302         138 :   gel(Q,3) = gel(P,3);
     303         138 :   if (l == 4) return Q;
     304         138 :   hi = h;
     305         138 :   gel(Q,4) = mulii(gel(P,4), hi);
     306         432 :   for (i=5; i<l; i++)
     307             :   {
     308         294 :     hi = mulii(hi,h);
     309         294 :     gel(Q,i) = mulii(gel(P,i), hi);
     310             :   }
     311         138 :   return Q;
     312             : }
     313             : 
     314             : GEN
     315         174 : RgXV_unscale(GEN v, GEN h)
     316             : {
     317             :   long i, l;
     318             :   GEN w;
     319         174 :   if (!h || isint1(h)) return v;
     320         126 :   w = cgetg_copy(v, &l);
     321         126 :   for (i=1; i<l; i++) gel(w,i) = RgX_unscale(gel(v,i), h);
     322         126 :   return w;
     323             : }
     324             : 
     325             : /* Return h^degpol(P) P(x / h), not memory clean */
     326             : GEN
     327         942 : RgX_rescale(GEN P, GEN h)
     328             : {
     329         942 :   long i, l = lg(P);
     330         942 :   GEN Q = cgetg(l,t_POL), hi = h;
     331         942 :   Q[l-1] = P[l-1];
     332        5922 :   for (i=l-2; i>=2; i--)
     333             :   {
     334        5922 :     gel(Q,i) = gmul(gel(P,i), hi);
     335        5922 :     if (i == 2) break;
     336        4980 :     hi = gmul(hi,h);
     337             :   }
     338         942 :   Q[1] = P[1]; return Q;
     339             : }
     340             : 
     341             : /* A(X^d) --> A(X) */
     342             : GEN
     343       63570 : RgX_deflate(GEN x0, long d)
     344             : {
     345             :   GEN z, y, x;
     346       63570 :   long i,id, dy, dx = degpol(x0);
     347       63570 :   if (d == 1 || dx <= 0) return leafcopy(x0);
     348       42996 :   dy = dx/d;
     349       42996 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     350       42996 :   z = y + 2;
     351       42996 :   x = x0+ 2;
     352       42996 :   for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
     353       42996 :   return y;
     354             : }
     355             : 
     356             : /* return x0(X^d) */
     357             : GEN
     358       93028 : RgX_inflate(GEN x0, long d)
     359             : {
     360       93028 :   long i, id, dy, dx = degpol(x0);
     361       93028 :   GEN x = x0 + 2, z, y;
     362       93028 :   if (dx <= 0) return leafcopy(x0);
     363       92512 :   dy = dx*d;
     364       92512 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     365       92512 :   z = y + 2;
     366       92512 :   for (i=0; i<=dy; i++) gel(z,i) = gen_0;
     367       92512 :   for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
     368       92512 :   return y;
     369             : }
     370             : 
     371             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     372             : GEN
     373      873162 : RgX_translate(GEN P, GEN c)
     374             : {
     375      873162 :   pari_sp av = avma;
     376             :   GEN Q, *R;
     377             :   long i, k, n;
     378             : 
     379      873162 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     380      870636 :   Q = leafcopy(P);
     381      870636 :   R = (GEN*)(Q+2); n = degpol(P);
     382      870636 :   if (gequal1(c))
     383             :   {
     384        1776 :     for (i=1; i<=n; i++)
     385             :     {
     386        1542 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], R[k+1]);
     387        1542 :       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      870402 :   else if (gequalm1(c))
     395             :   {
     396      114582 :     for (i=1; i<=n; i++)
     397             :     {
     398       98124 :       for (k=n-i; k<n; k++) R[k] = gsub(R[k], R[k+1]);
     399       98124 :       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     2906154 :     for (i=1; i<=n; i++)
     409             :     {
     410     2052210 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], gmul(c, R[k+1]));
     411     2052210 :       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      870636 :   return gerepilecopy(av, Q);
     419             : }
     420             : 
     421             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     422             : GEN
     423      313962 : ZX_translate(GEN P, GEN c)
     424             : {
     425      313962 :   pari_sp av = avma;
     426             :   GEN Q, *R;
     427             :   long i, k, n;
     428             : 
     429      313962 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     430      313932 :   Q = leafcopy(P);
     431      313932 :   R = (GEN*)(Q+2); n = degpol(P);
     432      313932 :   if (equali1(c))
     433             :   {
     434     1951542 :     for (i=1; i<=n; i++)
     435             :     {
     436     1724166 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], R[k+1]);
     437     1724166 :       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       86556 :   else if (equalim1(c))
     445             :   {
     446          60 :     for (i=1; i<=n; i++)
     447             :     {
     448          42 :       for (k=n-i; k<n; k++) R[k] = subii(R[k], R[k+1]);
     449          42 :       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      639672 :     for (i=1; i<=n; i++)
     459             :     {
     460      553134 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], mulii(c, R[k+1]));
     461      553134 :       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      313932 :   return gerepilecopy(av, Q);
     469             : }
     470             : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
     471             : GEN
     472        5184 : RgXQX_translate(GEN P, GEN c, GEN T)
     473             : {
     474        5184 :   pari_sp av = avma;
     475             :   GEN Q, *R;
     476             :   long i, k, n;
     477             : 
     478        5184 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     479        5166 :   Q = leafcopy(P);
     480        5166 :   R = (GEN*)(Q+2); n = degpol(P);
     481       29664 :   for (i=1; i<=n; i++)
     482             :   {
     483      120948 :     for (k=n-i; k<n; k++)
     484             :     {
     485       96450 :       pari_sp av2 = avma;
     486       96450 :       R[k] = gerepileupto(av2, RgX_rem(gadd(R[k], gmul(c, R[k+1])), T));
     487             :     }
     488       24498 :     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        5166 :   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       12354 : QXQ_to_mod_copy(GEN x, GEN T)
     507             : {
     508             :   long d;
     509       12354 :   switch(typ(x))
     510             :   {
     511        4392 :     case t_INT:  return icopy(x);
     512         318 :     case t_FRAC: return gcopy(x);
     513             :     case t_POL:
     514        7644 :       d = degpol(x);
     515        7644 :       if (d < 0) return gen_0;
     516        7410 :       if (d == 0) return gcopy(gel(x,2));
     517        7140 :       return mkpolmod(RgX_copy(x), T);
     518           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     519             :              return NULL;/* LCOV_EXCL_LINE */
     520             :   }
     521             : }
     522             : /* pure shallow version */
     523             : static GEN
     524      349992 : QXQ_to_mod(GEN x, GEN T)
     525             : {
     526             :   long d;
     527      349992 :   switch(typ(x))
     528             :   {
     529             :     case t_INT:
     530      304458 :     case t_FRAC: return x;
     531             :     case t_POL:
     532       45534 :       d = degpol(x);
     533       45534 :       if (d < 0) return gen_0;
     534       41904 :       if (d == 0) return gel(x,2);
     535       38598 :       return mkpolmod(x, T);
     536           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     537             :              return NULL;/* LCOV_EXCL_LINE */
     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        1644 : QXQX_to_mod(GEN z, GEN T)
     544             : {
     545        1644 :   long i,l = lg(z);
     546        1644 :   GEN x = cgetg(l,t_POL);
     547        1644 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_copy(gel(z,i), T);
     548        1644 :   x[1] = z[1]; return normalizepol_lg(x,l);
     549             : }
     550             : /* pure shallow version */
     551             : GEN
     552       71238 : QXQX_to_mod_shallow(GEN z, GEN T)
     553             : {
     554       71238 :   long i,l = lg(z);
     555       71238 :   GEN x = cgetg(l,t_POL);
     556       71238 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
     557       71238 :   x[1] = z[1]; return normalizepol_lg(x,l);
     558             : }
     559             : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
     560             : GEN
     561         462 : QXQXV_to_mod(GEN V, GEN T)
     562             : {
     563         462 :   long i, l = lg(V);
     564         462 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     565         462 :   for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
     566         462 :   return z;
     567             : }
     568             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     569             : GEN
     570         924 : QXQV_to_mod(GEN V, GEN T)
     571             : {
     572         924 :   long i, l = lg(V);
     573         924 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     574         924 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod_copy(gel(V,i), T);
     575         924 :   return z;
     576             : }
     577             : 
     578             : GEN
     579      593748 : RgX_renormalize_lg(GEN x, long lx)
     580             : {
     581             :   long i;
     582     1659246 :   for (i = lx-1; i>1; i--)
     583     1566420 :     if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
     584      593748 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     585      593748 :   setlg(x, i+1); setsigne(x, i != 1); return x;
     586             : }
     587             : 
     588             : GEN
     589      303436 : RgV_to_RgX(GEN x, long v)
     590             : {
     591      303436 :   long i, k = lg(x);
     592             :   GEN p;
     593             : 
     594      303436 :   while (--k && gequal0(gel(x,k)));
     595      303436 :   if (!k) return pol_0(v);
     596      303112 :   i = k+2; p = cgetg(i,t_POL);
     597      303112 :   p[1] = evalsigne(1) | evalvarn(v);
     598      303112 :   x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
     599      303112 :   return p;
     600             : }
     601             : GEN
     602      128172 : RgV_to_RgX_reverse(GEN x, long v)
     603             : {
     604      128172 :   long j, k, l = lg(x);
     605             :   GEN p;
     606             : 
     607      128172 :   for (k = 1; k < l; k++)
     608      128172 :     if (!gequal0(gel(x,k))) break;
     609      128172 :   if (k == l) return pol_0(v);
     610      128172 :   k -= 1;
     611      128172 :   l -= k;
     612      128172 :   x += k;
     613      128172 :   p = cgetg(l+1,t_POL);
     614      128172 :   p[1] = evalsigne(1) | evalvarn(v);
     615      128172 :   for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
     616      128172 :   return p;
     617             : }
     618             : 
     619             : /* return the (N-dimensional) vector of coeffs of p */
     620             : GEN
     621     2897136 : RgX_to_RgC(GEN x, long N)
     622             : {
     623             :   long i, l;
     624             :   GEN z;
     625     2897136 :   l = lg(x)-1; x++;
     626     2897136 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     627     2897136 :   z = cgetg(N+1,t_COL);
     628     2897136 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     629     2897136 :   for (   ; i<=N; i++) gel(z,i) = gen_0;
     630     2897136 :   return z;
     631             : }
     632             : GEN
     633      108906 : Rg_to_RgC(GEN x, long N)
     634             : {
     635      108906 :   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       30384 : RgM_to_RgXV(GEN x, long v)
     641             : {
     642       30384 :   long j, lx = lg(x);
     643       30384 :   GEN y = cgetg(lx, t_VEC);
     644       30384 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), v);
     645       30384 :   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       21996 : RgV_to_RgM(GEN v, long n)
     652             : {
     653       21996 :   long j, N = lg(v);
     654       21996 :   GEN y = cgetg(N, t_MAT);
     655       21996 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j), n);
     656       21996 :   return y;
     657             : }
     658             : GEN
     659        1554 : RgXV_to_RgM(GEN v, long n)
     660             : {
     661        1554 :   long j, N = lg(v);
     662        1554 :   GEN y = cgetg(N, t_MAT);
     663        1554 :   for (j=1; j<N; j++) gel(y,j) = RgX_to_RgC(gel(v,j), n);
     664        1554 :   return y;
     665             : }
     666             : 
     667             : /* polynomial (in v) of polynomials (in w) whose coeffs are given by the columns of x */
     668             : GEN
     669       13122 : RgM_to_RgXX(GEN x, long v,long w)
     670             : {
     671       13122 :   long j, lx = lg(x);
     672       13122 :   GEN y = cgetg(lx+1, t_POL);
     673       13122 :   y[1] = evalsigne(1) | evalvarn(v);
     674       13122 :   y++;
     675       13122 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
     676       13122 :   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          18 : RgXX_to_RgM(GEN v, long n)
     683             : {
     684          18 :   long j, N = lg(v)-1;
     685          18 :   GEN y = cgetg(N, t_MAT);
     686          18 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j+1), n);
     687          18 :   return y;
     688             : }
     689             : 
     690             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     691             : GEN
     692       10842 : RgXY_swapspec(GEN x, long n, long w, long nx)
     693             : {
     694       10842 :   long j, ly = n+3;
     695       10842 :   GEN y = cgetg(ly, t_POL);
     696       10842 :   y[1] = evalsigne(1);
     697      162294 :   for (j=2; j<ly; j++)
     698             :   {
     699             :     long k;
     700      151452 :     GEN a = cgetg(nx+2,t_POL);
     701      151452 :     a[1] = evalsigne(1) | evalvarn(w);
     702      813822 :     for (k=0; k<nx; k++)
     703             :     {
     704      662370 :       GEN xk = gel(x,k);
     705      662370 :       if (typ(xk)==t_POL)
     706      589140 :         gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
     707             :       else
     708       73230 :         gel(a,k+2) = j==2 ? xk: gen_0;
     709             :     }
     710      151452 :     gel(y,j) = normalizepol_lg(a, nx+2);
     711             :   }
     712       10842 :   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         192 : RgXY_swap(GEN x, long n, long w)
     718             : {
     719         192 :   GEN z = RgXY_swapspec(x+2, n, w, lgpol(x));
     720         192 :   setvarn(z, varn(x)); return z;
     721             : }
     722             : 
     723             : long
     724           0 : RgXY_degreex(GEN b)
     725             : {
     726           0 :   long deg = -1, i;
     727           0 :   if (!signe(b)) return -1;
     728           0 :   for (i = 2; i < lg(b); ++i)
     729             :   {
     730           0 :     GEN bi = gel(b, i);
     731           0 :     if (typ(bi) == t_POL)
     732           0 :       deg = maxss(deg, degpol(bi));
     733             :   }
     734           0 :   return deg;
     735             : }
     736             : 
     737             : /* return (x % X^n). Shallow */
     738             : GEN
     739       32670 : RgXn_red_shallow(GEN a, long n)
     740             : {
     741       32670 :   long i, L = n+2, l = lg(a);
     742             :   GEN  b;
     743       32670 :   if (L >= l) return a; /* deg(x) < n */
     744       24966 :   b = cgetg(L, t_POL); b[1] = a[1];
     745       24966 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
     746       24966 :   return normalizepol_lg(b,L);
     747             : }
     748             : 
     749             : GEN
     750         288 : RgXnV_red_shallow(GEN P, long n)
     751             : {
     752         288 :   long i, l = lg(P);
     753         288 :   GEN Q = cgetg(l, t_VEC);
     754         288 :   for (i=1; i<l; i++) gel(Q,i) = RgXn_red_shallow(gel(P,i), n);
     755         288 :   return Q;
     756             : }
     757             : 
     758             : /* return (x * X^n). Shallow */
     759             : GEN
     760    45576778 : RgX_shift_shallow(GEN a, long n)
     761             : {
     762    45576778 :   long i, l = lg(a);
     763             :   GEN  b;
     764    45576778 :   if (l == 2 || !n) return a;
     765    33654952 :   l += n;
     766    33654952 :   if (n < 0)
     767             :   {
     768    30217812 :     if (l <= 2) return pol_0(varn(a));
     769    30207228 :     b = cgetg(l, t_POL); b[1] = a[1];
     770    30207228 :     a -= n;
     771    30207228 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     772             :   } else {
     773     3437140 :     b = cgetg(l, t_POL); b[1] = a[1];
     774     3437142 :     a -= n; n += 2;
     775     3437142 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     776     3437142 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     777             :   }
     778    33644370 :   return b;
     779             : }
     780             : /* return (x * X^n). */
     781             : GEN
     782     2884224 : RgX_shift(GEN a, long n)
     783             : {
     784     2884224 :   long i, l = lg(a);
     785             :   GEN  b;
     786     2884224 :   if (l == 2 || !n) return RgX_copy(a);
     787     2884032 :   l += n;
     788     2884032 :   if (n < 0)
     789             :   {
     790         510 :     if (l <= 2) return pol_0(varn(a));
     791         474 :     b = cgetg(l, t_POL); b[1] = a[1];
     792         474 :     a -= n;
     793         474 :     for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     794             :   } else {
     795     2883522 :     b = cgetg(l, t_POL); b[1] = a[1];
     796     2883522 :     a -= n; n += 2;
     797     2883522 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     798     2883522 :     for (   ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     799             :   }
     800     2883996 :   return b;
     801             : }
     802             : 
     803             : GEN
     804      270666 : RgX_rotate_shallow(GEN P, long k, long p)
     805             : {
     806      270666 :   long i, l = lgpol(P);
     807             :   GEN r;
     808      270666 :   if (signe(P)==0)
     809         366 :     return pol_0(varn(P));
     810      270300 :   r = cgetg(p+2,t_POL); r[1] = P[1];
     811     1796076 :   for(i=0; i<p; i++)
     812             :   {
     813     1525776 :     long s = 2+(i+k)%p;
     814     1525776 :     gel(r,s) = i<l? gel(P,2+i): gen_0;
     815             :   }
     816      270300 :   return RgX_renormalize(r);
     817             : }
     818             : 
     819             : GEN
     820     2300598 : RgX_mulXn(GEN x, long d)
     821             : {
     822             :   pari_sp av;
     823             :   GEN z;
     824             :   long v;
     825     2300598 :   if (d >= 0) return RgX_shift(x, d);
     826     1044774 :   d = -d;
     827     1044774 :   v = RgX_val(x);
     828     1044774 :   if (v >= d) return RgX_shift(x, -d);
     829     1044768 :   av = avma;
     830     1044768 :   z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
     831     1044768 :   return gerepileupto(av, z);
     832             : }
     833             : 
     834             : long
     835     1713792 : RgX_val(GEN x)
     836             : {
     837     1713792 :   long i, lx = lg(x);
     838     1713792 :   if (lx == 2) return LONG_MAX;
     839     1732182 :   for (i = 2; i < lx; i++)
     840     1732182 :     if (!isexactzero(gel(x,i))) break;
     841     1713780 :   if (i == lx) i--; /* possible with non-rational zeros */
     842     1713780 :   return i - 2;
     843             : }
     844             : long
     845    33533772 : RgX_valrem(GEN x, GEN *Z)
     846             : {
     847    33533772 :   long v, i, lx = lg(x);
     848    33533772 :   if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
     849    64950498 :   for (i = 2; i < lx; i++)
     850    64950498 :     if (!isexactzero(gel(x,i))) break;
     851    33533772 :   if (i == lx) i--; /* possible with non-rational zeros */
     852    33533772 :   v = i - 2;
     853    33533772 :   *Z = RgX_shift_shallow(x, -v);
     854    33533772 :   return v;
     855             : }
     856             : long
     857        2790 : RgX_valrem_inexact(GEN x, GEN *Z)
     858             : {
     859             :   long v;
     860        2790 :   if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
     861        3036 :   for (v = 0;; v++)
     862        3036 :     if (!gequal0(gel(x,2+v))) break;
     863         252 :   if (Z) *Z = RgX_shift_shallow(x, -v);
     864        2784 :   return v;
     865             : }
     866             : 
     867             : GEN
     868           0 : RgXQC_red(GEN P, GEN T)
     869             : {
     870           0 :   long i, l = lg(P);
     871           0 :   GEN Q = cgetg(l, t_COL);
     872           0 :   for (i=1; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     873           0 :   return Q;
     874             : }
     875             : 
     876             : GEN
     877          48 : RgXQV_red(GEN P, GEN T)
     878             : {
     879          48 :   long i, l = lg(P);
     880          48 :   GEN Q = cgetg(l, t_VEC);
     881          48 :   for (i=1; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     882          48 :   return Q;
     883             : }
     884             : 
     885             : GEN
     886           0 : RgXQM_red(GEN P, GEN T)
     887             : {
     888           0 :   long i, l = lg(P);
     889           0 :   GEN Q = cgetg(l, t_MAT);
     890           0 :   for (i=1; i<l; i++) gel(Q,i) = RgXQC_red(gel(P,i), T);
     891           0 :   return Q;
     892             : }
     893             : 
     894             : GEN
     895           0 : RgXQM_mul(GEN P, GEN Q, GEN T)
     896             : {
     897           0 :   return RgXQM_red(RgM_mul(P, Q), T);
     898             : }
     899             : 
     900             : GEN
     901        4740 : RgXQX_red(GEN P, GEN T)
     902             : {
     903        4740 :   long i, l = lg(P);
     904        4740 :   GEN Q = cgetg(l, t_POL);
     905        4740 :   Q[1] = P[1];
     906        4740 :   for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     907        4740 :   return normalizepol_lg(Q, l);
     908             : }
     909             : 
     910             : GEN
     911      150534 : RgX_deriv(GEN x)
     912             : {
     913      150534 :   long i,lx = lg(x)-1;
     914             :   GEN y;
     915             : 
     916      150534 :   if (lx<3) return pol_0(varn(x));
     917      149358 :   y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
     918      149358 :   for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
     919      149358 :   y[1] = x[1]; return normalizepol_lg(y,i);
     920             : }
     921             : 
     922             : GEN
     923      234324 : RgX_recipspec_shallow(GEN x, long l, long n)
     924             : {
     925             :   long i;
     926      234324 :   GEN z=cgetg(n+2,t_POL)+2;
     927    11258605 :   for(i=0; i<l; i++)
     928    11024281 :     gel(z,n-i-1) = gel(x,i);
     929      309480 :   for(   ; i<n; i++)
     930       75156 :     gel(z, n-i-1) = gen_0;
     931      234324 :   return normalizepol_lg(z-2,n+2);
     932             : }
     933             : 
     934             : /* return coefficients s.t x = x_0 X^n + ... + x_n */
     935             : GEN
     936         432 : RgX_recip(GEN x)
     937             : {
     938             :   long lx, i, j;
     939         432 :   GEN y = cgetg_copy(x, &lx);
     940         432 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
     941         432 :   return normalizepol_lg(y,lx);
     942             : }
     943             : /* shallow version */
     944             : GEN
     945      277173 : RgX_recip_shallow(GEN x)
     946             : {
     947             :   long lx, i, j;
     948      277173 :   GEN y = cgetg_copy(x, &lx);
     949      277193 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
     950      277193 :   return y;
     951             : }
     952             : /*******************************************************************/
     953             : /*                                                                 */
     954             : /*                      ADDITION / SUBTRACTION                     */
     955             : /*                                                                 */
     956             : /*******************************************************************/
     957             : /* same variable */
     958             : GEN
     959    12840520 : RgX_add(GEN x, GEN y)
     960             : {
     961    12840520 :   long i, lx = lg(x), ly = lg(y);
     962             :   GEN z;
     963    12840520 :   if (ly <= lx) {
     964    11651751 :     z = cgetg(lx,t_POL); z[1] = x[1];
     965    11651755 :     for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
     966    11651750 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
     967    11651750 :     z = normalizepol_lg(z, lx);
     968             :   } else {
     969     1188769 :     z = cgetg(ly,t_POL); z[1] = y[1];
     970     1188768 :     for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
     971     1188769 :     for (   ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
     972     1188771 :     z = normalizepol_lg(z, ly);
     973             :   }
     974    12840519 :   return z;
     975             : }
     976             : GEN
     977     8176961 : RgX_sub(GEN x, GEN y)
     978             : {
     979     8176961 :   long i, lx = lg(x), ly = lg(y);
     980             :   GEN z;
     981     8176961 :   if (ly <= lx) {
     982     6580547 :     z = cgetg(lx,t_POL); z[1] = x[1];
     983     6580560 :     for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
     984     6580548 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
     985     6580548 :     z = normalizepol_lg(z, lx);
     986             :   } else {
     987     1596414 :     z = cgetg(ly,t_POL); z[1] = y[1];
     988     1596414 :     for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
     989     1596414 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
     990     1596414 :     z = normalizepol_lg(z, ly);
     991             :   }
     992     8176962 :   return z;
     993             : }
     994             : GEN
     995      751788 : RgX_neg(GEN x)
     996             : {
     997      751788 :   long i, lx = lg(x);
     998      751788 :   GEN y = cgetg(lx, t_POL); y[1] = x[1];
     999      751788 :   for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
    1000      751788 :   return y;
    1001             : }
    1002             : 
    1003             : GEN
    1004     8608242 : RgX_Rg_add(GEN y, GEN x)
    1005             : {
    1006             :   GEN z;
    1007     8608242 :   long lz = lg(y), i;
    1008     8608242 :   if (lz == 2) return scalarpol(x,varn(y));
    1009     7283316 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1010     7283316 :   gel(z,2) = gadd(gel(y,2),x);
    1011     7283316 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1012             :   /* probably useless unless lz = 3, but cannot be skipped if y is
    1013             :    * an inexact 0 */
    1014     7283316 :   return normalizepol_lg(z,lz);
    1015             : }
    1016             : GEN
    1017        2076 : RgX_Rg_add_shallow(GEN y, GEN x)
    1018             : {
    1019             :   GEN z;
    1020        2076 :   long lz = lg(y), i;
    1021        2076 :   if (lz == 2) return scalarpol(x,varn(y));
    1022        2076 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1023        2076 :   gel(z,2) = gadd(gel(y,2),x);
    1024        2076 :   for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
    1025        2076 :   return z = normalizepol_lg(z,lz);
    1026             : }
    1027             : GEN
    1028       27666 : RgX_Rg_sub(GEN y, GEN x)
    1029             : {
    1030             :   GEN z;
    1031       27666 :   long lz = lg(y), i;
    1032       27666 :   if (lz == 2)
    1033             :   { /* scalarpol(gneg(x),varn(y)) optimized */
    1034        3312 :     long v = varn(y);
    1035        3312 :     if (isrationalzero(x)) return pol_0(v);
    1036          12 :     z = cgetg(3,t_POL);
    1037          24 :     z[1] = gequal0(x)? evalvarn(v)
    1038          12 :                    : evalvarn(v) | evalsigne(1);
    1039          12 :     gel(z,2) = gneg(x); return z;
    1040             :   }
    1041       24354 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1042       24354 :   gel(z,2) = gsub(gel(y,2),x);
    1043       24354 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1044       24354 :   return z = normalizepol_lg(z,lz);
    1045             : }
    1046             : GEN
    1047      265674 : Rg_RgX_sub(GEN x, GEN y)
    1048             : {
    1049             :   GEN z;
    1050      265674 :   long lz = lg(y), i;
    1051      265674 :   if (lz == 2) return scalarpol(x,varn(y));
    1052      264804 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1053      264804 :   gel(z,2) = gsub(x, gel(y,2));
    1054      264804 :   for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
    1055      264804 :   return z = normalizepol_lg(z,lz);
    1056             : }
    1057             : /*******************************************************************/
    1058             : /*                                                                 */
    1059             : /*                  KARATSUBA MULTIPLICATION                       */
    1060             : /*                                                                 */
    1061             : /*******************************************************************/
    1062             : #if 0
    1063             : /* to debug Karatsuba-like routines */
    1064             : GEN
    1065             : zx_debug_spec(GEN x, long nx)
    1066             : {
    1067             :   GEN z = cgetg(nx+2,t_POL);
    1068             :   long i;
    1069             :   for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
    1070             :   z[1] = evalsigne(1); return z;
    1071             : }
    1072             : 
    1073             : GEN
    1074             : RgX_debug_spec(GEN x, long nx)
    1075             : {
    1076             :   GEN z = cgetg(nx+2,t_POL);
    1077             :   long i;
    1078             :   for (i=0; i<nx; i++) z[i+2] = x[i];
    1079             :   z[1] = evalsigne(1); return z;
    1080             : }
    1081             : #endif
    1082             : 
    1083             : /* generic multiplication */
    1084             : 
    1085             : static GEN
    1086     2220695 : addpol(GEN x, GEN y, long lx, long ly)
    1087             : {
    1088             :   long i,lz;
    1089             :   GEN z;
    1090             : 
    1091     2220695 :   if (ly>lx) swapspec(x,y, lx,ly);
    1092     2220695 :   lz = lx+2; z = cgetg(lz,t_POL) + 2;
    1093     2220833 :   for (i=0; i<ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1094     2220697 :   for (   ; i<lx; i++) gel(z,i) = gel(x,i);
    1095     2220697 :   z -= 2; z[1]=0; return normalizepol_lg(z, lz);
    1096             : }
    1097             : 
    1098             : static GEN
    1099      228051 : addpolcopy(GEN x, GEN y, long lx, long ly)
    1100             : {
    1101             :   long i,lz;
    1102             :   GEN z;
    1103             : 
    1104      228051 :   if (ly>lx) swapspec(x,y, lx,ly);
    1105      228051 :   lz = lx+2; z = cgetg(lz,t_POL) + 2;
    1106      228076 :   for (i=0; i<ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1107      228067 :   for (   ; i<lx; i++) gel(z,i) = gcopy(gel(x,i));
    1108      228052 :   z -= 2; z[1]=0; return normalizepol_lg(z, lz);
    1109             : }
    1110             : 
    1111             : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
    1112             :  * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
    1113             :  * t_VECSMALL, to hold this, which can be left on stack: gerepile
    1114             :  * will not crash on it. The returned vector itself is not a proper GEN,
    1115             :  * we access the coefficients as x[i], i = 0..deg(x) */
    1116             : static GEN
    1117    25574139 : RgXspec_kill0(GEN x, long lx)
    1118             : {
    1119    25574139 :   GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit gerepile-wise */
    1120             :   long i;
    1121   110574296 :   for (i=0; i <lx; i++)
    1122             :   {
    1123    85000205 :     GEN c = gel(x,i);
    1124    85000205 :     z[i] = (long)(isrationalzero(c)? NULL: c);
    1125             :   }
    1126    25574091 :   return z;
    1127             : }
    1128             : 
    1129             : INLINE GEN
    1130    60986978 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
    1131             : {
    1132    60986978 :   pari_sp av = avma;
    1133    60986978 :   GEN s = NULL;
    1134             :   long i;
    1135             : 
    1136   244773752 :   for (i=a; i<b; i++)
    1137   183789548 :     if (gel(y,i) && gel(x,-i))
    1138             :     {
    1139   139289566 :       GEN t = gmul(gel(y,i), gel(x,-i));
    1140   139294849 :       s = s? gadd(s, t): t;
    1141             :     }
    1142    60984204 :   return s? gerepileupto(av, s): gen_0;
    1143             : }
    1144             : 
    1145             : /* assume nx >= ny > 0, return x * y * t^v */
    1146             : static GEN
    1147     9950166 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
    1148             : {
    1149             :   long i, lz, nz;
    1150             :   GEN z;
    1151             : 
    1152     9950166 :   x = RgXspec_kill0(x,nx);
    1153     9950136 :   y = RgXspec_kill0(y,ny);
    1154     9950140 :   lz = nx + ny + 1; nz = lz-2;
    1155     9950140 :   lz += v;
    1156     9950140 :   z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
    1157     9950195 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1158     9950195 :   for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
    1159     9950109 :   for (  ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
    1160     9950110 :   for (  ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
    1161     9950147 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1162             : }
    1163             : 
    1164             : /* return (x * X^d) + y. Assume d > 0 */
    1165             : GEN
    1166     1471059 : addmulXn(GEN x, GEN y, long d)
    1167             : {
    1168             :   GEN xd, yd, zd;
    1169             :   long a, lz, nx, ny;
    1170             : 
    1171     1471059 :   if (!signe(x)) return y;
    1172     1453989 :   ny = lgpol(y);
    1173     1453989 :   nx = lgpol(x);
    1174     1453989 :   zd = (GEN)avma;
    1175     1453989 :   x += 2; y += 2; a = ny-d;
    1176     1453989 :   if (a <= 0)
    1177             :   {
    1178      112368 :     lz = nx+d+2;
    1179      112368 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1180      112368 :     while (xd > x) gel(--zd,0) = gel(--xd,0);
    1181      112368 :     x = zd + a;
    1182      112368 :     while (zd > x) gel(--zd,0) = gen_0;
    1183             :   }
    1184             :   else
    1185             :   {
    1186     1341621 :     xd = new_chunk(d); yd = y+d;
    1187     1341621 :     x = addpol(x,yd, nx,a);
    1188     1341623 :     lz = (a>nx)? ny+2: lg(x)+d;
    1189     1341623 :     x += 2; while (xd > x) *--zd = *--xd;
    1190             :   }
    1191     1453991 :   while (yd > y) *--zd = *--yd;
    1192     1453991 :   *--zd = evalsigne(1);
    1193     1453991 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1194             : }
    1195             : 
    1196             : GEN
    1197       19524 : addshiftpol(GEN x, GEN y, long d)
    1198             : {
    1199       19524 :   long v = varn(x);
    1200       19524 :   x = addmulXn(x,y,d);
    1201       19524 :   setvarn(x,v); return x;
    1202             : }
    1203             : 
    1204             : /* as above, producing a clean malloc */
    1205             : static GEN
    1206      445311 : addmulXncopy(GEN x, GEN y, long d)
    1207             : {
    1208             :   GEN xd, yd, zd;
    1209             :   long a, lz, nx, ny;
    1210             : 
    1211      445311 :   if (!signe(x)) return RgX_copy(y);
    1212      445215 :   nx = lgpol(x);
    1213      445215 :   ny = lgpol(y);
    1214      445215 :   zd = (GEN)avma;
    1215      445215 :   x += 2; y += 2; a = ny-d;
    1216      445215 :   if (a <= 0)
    1217             :   {
    1218      217164 :     lz = nx+d+2;
    1219      217164 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1220      217183 :     while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
    1221      217183 :     x = zd + a;
    1222      217183 :     while (zd > x) gel(--zd,0) = gen_0;
    1223             :   }
    1224             :   else
    1225             :   {
    1226      228051 :     xd = new_chunk(d); yd = y+d;
    1227      228051 :     x = addpolcopy(x,yd, nx,a);
    1228      228052 :     lz = (a>nx)? ny+2: lg(x)+d;
    1229      228052 :     x += 2; while (xd > x) *--zd = *--xd;
    1230             :   }
    1231      445235 :   while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
    1232      445217 :   *--zd = evalsigne(1);
    1233      445217 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1234             : }
    1235             : 
    1236             : /* return x * y mod t^n */
    1237             : static GEN
    1238     2718966 : RgXn_mul_basecase(GEN x, GEN y, long n)
    1239             : {
    1240     2718966 :   long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
    1241             :   GEN z;
    1242     2718966 :   if (lx < 0) return pol_0(varn(x));
    1243     2718966 :   if (ly < 0) return pol_0(varn(x));
    1244     2718966 :   z = cgetg(lz, t_POL) + 2;
    1245     2718966 :   x+=2; if (lx > n) lx = n;
    1246     2718966 :   y+=2; if (ly > n) ly = n;
    1247     2718966 :   z[-1] = x[-1];
    1248     2718966 :   if (ly > lx) { swap(x,y); lswap(lx,ly); }
    1249     2718966 :   x = RgXspec_kill0(x, lx);
    1250     2718966 :   y = RgXspec_kill0(y, ly);
    1251             :   /* x:y:z [i] = term of degree i */
    1252     2718966 :   for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
    1253     2718966 :   for (  ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
    1254     2718966 :   for (  ; i<n; i++)  gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
    1255     2718966 :   return normalizepol_lg(z - 2, lz);
    1256             : }
    1257             : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
    1258             : GEN
    1259     3136584 : RgXn_mul(GEN f, GEN g, long n)
    1260             : {
    1261     3136584 :   pari_sp av = avma;
    1262             :   GEN fe,fo, ge,go, l,h,m;
    1263             :   long n0, n1;
    1264     3136584 :   if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
    1265     2719812 :   if (n < 80) return RgXn_mul_basecase(f,g,n);
    1266         846 :   n0 = n>>1; n1 = n-n0;
    1267         846 :   RgX_even_odd(f, &fe, &fo);
    1268         846 :   RgX_even_odd(g, &ge, &go);
    1269         846 :   l = RgXn_mul(fe,ge,n1);
    1270         846 :   h = RgXn_mul(fo,go,n0);
    1271         846 :   m = RgX_sub(RgXn_mul(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
    1272             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1273             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1274         846 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1275             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1276         846 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1277         846 :   m = RgX_inflate(m,2);
    1278             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1279         846 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1280         846 :   h = RgX_inflate(h,2);
    1281         846 :   h = addmulXncopy(addmulXn(h,m,1), l,1);
    1282         846 :   setvarn(h, varn(f)); return gerepileupto(av, h);
    1283             : }
    1284             : /* (f*g) \/ x^n */
    1285             : GEN
    1286       16890 : RgX_mulhigh_i(GEN f, GEN g, long n)
    1287             : {
    1288       16890 :   long d = degpol(f)+degpol(g) + 1 - n;
    1289             :   GEN h;
    1290       16890 :   if (d <= 2) return RgX_shift_shallow(RgX_mul(f,g), -n);
    1291        1290 :   h = RgX_recip_shallow(RgXn_mul(RgX_recip_shallow(f),
    1292             :                                  RgX_recip_shallow(g), d));
    1293        1290 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1294             : }
    1295             : 
    1296             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
    1297             :  * b+2 were sent instead. na, nb = number of terms of a, b.
    1298             :  * Only c, c0, c1, c2 are genuine GEN.
    1299             :  */
    1300             : GEN
    1301    10475688 : RgX_mulspec(GEN a, GEN b, long na, long nb)
    1302             : {
    1303             :   GEN a0, c, c0;
    1304    10475688 :   long n0, n0a, i, v = 0;
    1305             :   pari_sp av;
    1306             : 
    1307    10475688 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
    1308    10475687 :   while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
    1309    10475684 :   if (na < nb) swapspec(a,b, na,nb);
    1310    10475684 :   if (!nb) return pol_0(0);
    1311             : 
    1312    10393934 :   if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
    1313      443770 :   RgX_shift_inplace_init(v);
    1314      443772 :   i = (na>>1); n0 = na-i; na = i;
    1315      443772 :   av = avma; a0 = a+n0; n0a = n0;
    1316      443772 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1317             : 
    1318      443771 :   if (nb > n0)
    1319             :   {
    1320             :     GEN b0,c1,c2;
    1321             :     long n0b;
    1322             : 
    1323      439547 :     nb -= n0; b0 = b+n0; n0b = n0;
    1324      439547 :     while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
    1325      439547 :     c = RgX_mulspec(a,b,n0a,n0b);
    1326      439542 :     c0 = RgX_mulspec(a0,b0, na,nb);
    1327             : 
    1328      439538 :     c2 = addpol(a0,a, na,n0a);
    1329      439545 :     c1 = addpol(b0,b, nb,n0b);
    1330             : 
    1331      439544 :     c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
    1332      439546 :     c2 = RgX_sub(c1, RgX_add(c0,c));
    1333      439545 :     c0 = addmulXn(c0, c2, n0);
    1334             :   }
    1335             :   else
    1336             :   {
    1337        4224 :     c = RgX_mulspec(a,b,n0a,nb);
    1338        4224 :     c0 = RgX_mulspec(a0,b,na,nb);
    1339             :   }
    1340      443771 :   c0 = addmulXncopy(c0,c,n0);
    1341      443771 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1342             : }
    1343             : 
    1344             : INLINE GEN
    1345     2553153 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
    1346             : {
    1347     2553153 :   pari_sp av = avma;
    1348     2553153 :   GEN s = NULL;
    1349     2553153 :   long j, l = (i+1)>>1;
    1350     9680207 :   for (j=a; j<l; j++)
    1351             :   {
    1352     7128427 :     GEN xj = gel(x,j), xx = gel(x,i-j);
    1353     7128427 :     if (xj && xx)
    1354             :     {
    1355     4457605 :       GEN t = gmul(xj, xx);
    1356     4460976 :       s = s? gadd(s, t): t;
    1357             :     }
    1358             :   }
    1359     2551780 :   if (s) s = gshift(s,1);
    1360     2551862 :   if ((i&1) == 0)
    1361             :   {
    1362     1394002 :     GEN t = gel(x, i>>1);
    1363     1394002 :     if (t) {
    1364     1156430 :       t = gsqr(t);
    1365     1156466 :       s = s? gadd(s, t): t;
    1366             :     }
    1367             :   }
    1368     2551827 :   return s? gerepileupto(av,s): gen_0;
    1369             : }
    1370             : static GEN
    1371      235356 : RgX_sqrspec_basecase(GEN x, long nx, long v)
    1372             : {
    1373             :   long i, lz, nz;
    1374             :   GEN z;
    1375             : 
    1376      235356 :   if (!nx) return pol_0(0);
    1377      235350 :   x = RgXspec_kill0(x,nx);
    1378      235349 :   lz = (nx << 1) + 1, nz = lz-2;
    1379      235349 :   lz += v;
    1380      235349 :   z = cgetg(lz,t_POL) + 2;
    1381      235377 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1382      235377 :   for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1383      235356 :   for (  ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
    1384      235348 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1385             : }
    1386             : /* return x^2 mod t^n */
    1387             : static GEN
    1388         570 : RgXn_sqr_basecase(GEN x, long n)
    1389             : {
    1390         570 :   long i, lz = n+2, lx = lgpol(x);
    1391             :   GEN z;
    1392         570 :   if (lx < 0) return pol_0(varn(x));
    1393         570 :   z = cgetg(lz, t_POL);
    1394         570 :   z[1] = x[1];
    1395         570 :   x+=2; if (lx > n) lx = n;
    1396         570 :   x = RgXspec_kill0(x,lx);
    1397         570 :   z+=2;/* x:z [i] = term of degree i */
    1398         570 :   for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1399         570 :   for (  ; i<n; i++)  gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
    1400         570 :   z -= 2; return normalizepol_lg(z, lz);
    1401             : }
    1402             : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
    1403             : GEN
    1404        2040 : RgXn_sqr(GEN f, long n)
    1405             : {
    1406        2040 :   pari_sp av = avma;
    1407             :   GEN fe,fo, l,h,m;
    1408             :   long n0, n1;
    1409        2040 :   if (2*degpol(f) < n) return RgX_sqr(f);
    1410         594 :   if (n < 80) return RgXn_sqr_basecase(f,n);
    1411          24 :   n0 = n>>1; n1 = n-n0;
    1412          24 :   RgX_even_odd(f, &fe, &fo);
    1413          24 :   l = RgXn_sqr(fe,n1);
    1414          24 :   h = RgXn_sqr(fo,n0);
    1415          24 :   m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
    1416             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1417             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1418          24 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1419             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1420          24 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1421          24 :   m = RgX_inflate(m,2);
    1422             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1423          24 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1424          24 :   h = RgX_inflate(h,2);
    1425          24 :   h = addmulXncopy(addmulXn(h,m,1), l,1);
    1426          24 :   setvarn(h, varn(f)); return gerepileupto(av, h);
    1427             : }
    1428             : 
    1429             : GEN
    1430      236028 : RgX_sqrspec(GEN a, long na)
    1431             : {
    1432             :   GEN a0, c, c0, c1;
    1433      236028 :   long n0, n0a, i, v = 0;
    1434             :   pari_sp av;
    1435             : 
    1436      236028 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
    1437      236028 :   if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
    1438         672 :   RgX_shift_inplace_init(v);
    1439         672 :   i = (na>>1); n0 = na-i; na = i;
    1440         672 :   av = avma; a0 = a+n0; n0a = n0;
    1441         672 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1442             : 
    1443         672 :   c = RgX_sqrspec(a,n0a);
    1444         672 :   c0 = RgX_sqrspec(a0,na);
    1445         672 :   c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
    1446         672 :   c0 = addmulXn(c0,c1, n0);
    1447         672 :   c0 = addmulXncopy(c0,c,n0);
    1448         672 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1449             : }
    1450             : 
    1451             : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
    1452             : GEN
    1453      369522 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
    1454             : {
    1455      369522 :   GEN z = RgX_mul(A, B);
    1456      369522 :   if (a < b)
    1457        4476 :     z = addmulXn(addmulXn(A, B, b-a), z, a);
    1458      365046 :   else if (a > b)
    1459      236094 :     z = addmulXn(addmulXn(B, A, a-b), z, b);
    1460             :   else
    1461      128952 :     z = addmulXn(RgX_add(A, B), z, a);
    1462      369522 :   setvarn(z,varn(A)); return z;
    1463             : }
    1464             : 
    1465             : GEN
    1466     9147938 : RgX_mul(GEN x, GEN y)
    1467             : {
    1468     9147938 :   GEN z = RgX_mulspec(y+2, x+2, lgpol(y), lgpol(x));
    1469     9147937 :   setvarn(z,varn(x)); return z;
    1470             : }
    1471             : 
    1472             : GEN
    1473      234684 : RgX_sqr(GEN x)
    1474             : {
    1475      234684 :   GEN z = RgX_sqrspec(x+2, lgpol(x));
    1476      234681 :   setvarn(z,varn(x)); return z;
    1477             : }
    1478             : 
    1479             : /*******************************************************************/
    1480             : /*                                                                 */
    1481             : /*                               DIVISION                          */
    1482             : /*                                                                 */
    1483             : /*******************************************************************/
    1484             : GEN
    1485      436512 : RgX_Rg_divexact(GEN x, GEN y) {
    1486             :   long i, lx;
    1487             :   GEN z;
    1488      436512 :   if (typ(y) == t_INT && is_pm1(y))
    1489       79770 :     return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1490      356742 :   z = cgetg_copy(x, &lx); z[1] = x[1];
    1491      356742 :   for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    1492      356742 :   return z;
    1493             : }
    1494             : GEN
    1495    18071286 : RgX_Rg_div(GEN x, GEN y) {
    1496             :   long i, lx;
    1497    18071286 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1498    18071286 :   for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
    1499    18071286 :   return normalizepol_lg(z, lx);
    1500             : }
    1501             : GEN
    1502        1248 : RgX_normalize(GEN x)
    1503             : {
    1504        1248 :   GEN d = NULL;
    1505        1248 :   long i, n = lg(x)-1;
    1506        1248 :   for (i = n; i > 1; i--)
    1507             :   {
    1508        1248 :     d = gel(x,i);
    1509        1248 :     if (!gequal0(d)) break;
    1510             :   }
    1511        1248 :   if (i == 1) return pol_0(varn(x));
    1512        1248 :   if (i == n && isint1(d)) return x;
    1513         246 :   return normalizepol_lg(RgX_Rg_div(x, d), i+1);
    1514             : }
    1515             : GEN
    1516        1482 : RgX_divs(GEN x, long y) {
    1517             :   long i, lx;
    1518        1482 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1519        1482 :   for (i=2; i<lx; i++) gel(z,i) = gdivgs(gel(x,i),y);
    1520        1482 :   return normalizepol_lg(z, lx);
    1521             : }
    1522             : GEN
    1523       27372 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
    1524             : {
    1525       27372 :   long l = lg(a), i;
    1526       27372 :   GEN a0, z0, z = cgetg(l-1, t_POL);
    1527       27372 :   z[1] = a[1];
    1528       27372 :   a0 = a + l-1;
    1529       27372 :   z0 = z + l-2; *z0 = *a0--;
    1530      631308 :   for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1531             :   {
    1532      603936 :     GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
    1533      603936 :     gel(z0,0) = t;
    1534             :   }
    1535       27372 :   if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
    1536       27372 :   return z;
    1537             : }
    1538             : /* Polynomial division x / y:
    1539             :  *   if z = ONLY_REM  return remainder, otherwise return quotient
    1540             :  *   if z != NULL set *z to remainder
    1541             :  *   *z is the last object on stack (and thus can be disposed of with cgiv
    1542             :  *   instead of gerepile) */
    1543             : /* assume, typ(x) = typ(y) = t_POL, same variable */
    1544             : GEN
    1545    10600267 : RgX_divrem(GEN x, GEN y, GEN *pr)
    1546             : {
    1547             :   pari_sp avy, av, av1;
    1548             :   long dx,dy,dz,i,j,sx,lr;
    1549             :   GEN z,p1,p2,rem,y_lead,mod;
    1550             :   GEN (*f)(GEN,GEN);
    1551             : 
    1552    10600267 :   if (!signe(y)) pari_err_INV("RgX_divrem",y);
    1553             : 
    1554    10600267 :   dy = degpol(y);
    1555    10600312 :   y_lead = gel(y,dy+2);
    1556    10600312 :   if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
    1557             :   {
    1558           0 :     pari_warn(warner,"normalizing a polynomial with 0 leading term");
    1559           0 :     for (dy--; dy>=0; dy--)
    1560             :     {
    1561           0 :       y_lead = gel(y,dy+2);
    1562           0 :       if (!gequal0(y_lead)) break;
    1563             :     }
    1564             :   }
    1565    10600162 :   if (!dy) /* y is constant */
    1566             :   {
    1567        9072 :     if (pr == ONLY_REM) return pol_0(varn(x));
    1568        8550 :     z = RgX_Rg_div(x, y_lead);
    1569        8550 :     if (pr == ONLY_DIVIDES) return z;
    1570        7968 :     if (pr) *pr = pol_0(varn(x));
    1571        7968 :     return z;
    1572             :   }
    1573    10591090 :   dx = degpol(x);
    1574    10591093 :   if (dx < dy)
    1575             :   {
    1576      779716 :     if (pr == ONLY_REM) return RgX_copy(x);
    1577      239382 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1578      239364 :     z = pol_0(varn(x));
    1579      239364 :     if (pr) *pr = RgX_copy(x);
    1580      239364 :     return z;
    1581             :   }
    1582             : 
    1583             :   /* x,y in R[X], y non constant */
    1584     9811377 :   av = avma;
    1585     9811377 :   switch(typ(y_lead))
    1586             :   {
    1587             :     case t_REAL:
    1588           0 :       y_lead = ginv(y_lead);
    1589           0 :       f = gmul; mod = NULL;
    1590           0 :       break;
    1591             :     case t_INTMOD:
    1592        3870 :     case t_POLMOD: y_lead = ginv(y_lead);
    1593        3870 :       f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
    1594        3870 :       break;
    1595     9807507 :     default: if (gequal1(y_lead)) y_lead = NULL;
    1596     9807647 :       f = gdiv; mod = NULL;
    1597             :   }
    1598             : 
    1599     9811517 :   if (y_lead == NULL)
    1600     8517608 :     p2 = gel(x,dx+2);
    1601             :   else {
    1602             :     for(;;) {
    1603     1293909 :       p2 = f(gel(x,dx+2),y_lead);
    1604     1293966 :       p2 = simplify_shallow(p2);
    1605     1293966 :       if (!isexactzero(p2) || (--dx < 0)) break;
    1606           0 :     }
    1607     1293966 :     if (dx < dy) /* leading coeff of x was in fact zero */
    1608             :     {
    1609           0 :       if (pr == ONLY_DIVIDES) {
    1610           0 :         avma = av;
    1611           0 :         return (dx < 0)? pol_0(varn(x)) : NULL;
    1612             :       }
    1613           0 :       if (pr == ONLY_REM)
    1614             :       {
    1615           0 :         if (dx < 0)
    1616           0 :           return gerepilecopy(av, scalarpol(p2, varn(x)));
    1617             :         else
    1618             :         {
    1619             :           GEN t;
    1620           0 :           avma = av;
    1621           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1622           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1623           0 :           return t;
    1624             :         }
    1625             :       }
    1626           0 :       if (pr) /* cf ONLY_REM above */
    1627             :       {
    1628           0 :         if (dx < 0)
    1629             :         {
    1630           0 :           p2 = gclone(p2);
    1631           0 :           avma = av;
    1632           0 :           z = pol_0(varn(x));
    1633           0 :           x = scalarpol(p2, varn(x));
    1634           0 :           gunclone(p2);
    1635             :         }
    1636             :         else
    1637             :         {
    1638             :           GEN t;
    1639           0 :           avma = av;
    1640           0 :           z = pol_0(varn(x));
    1641           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1642           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1643           0 :           x = t;
    1644             :         }
    1645           0 :         *pr = x;
    1646             :       }
    1647             :       else
    1648             :       {
    1649           0 :         avma = av;
    1650           0 :         z = pol_0(varn(x));
    1651             :       }
    1652           0 :       return z;
    1653             :     }
    1654             :   }
    1655             :   /* dx >= dy */
    1656     9811574 :   avy = avma;
    1657     9811574 :   dz = dx-dy;
    1658     9811574 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1659     9811411 :   x += 2;
    1660     9811411 :   z += 2;
    1661     9811411 :   y += 2;
    1662     9811411 :   gel(z,dz) = gcopy(p2);
    1663             : 
    1664    29621059 :   for (i=dx-1; i>=dy; i--)
    1665             :   {
    1666    19809176 :     av1=avma; p1=gel(x,i);
    1667    19809176 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1668    19790641 :     if (y_lead) p1 = simplify(f(p1,y_lead));
    1669             : 
    1670    19790641 :     if (isrationalzero(p1)) { avma=av1; p1 = gen_0; }
    1671             :     else
    1672    11500418 :       p1 = avma==av1? gcopy(p1): gerepileupto(av1,p1);
    1673    19809024 :     gel(z,i-dy) = p1;
    1674             :   }
    1675     9811883 :   if (!pr) return gerepileupto(av,z-2);
    1676             : 
    1677     4929299 :   rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
    1678     5555804 :   for (sx=0; ; i--)
    1679             :   {
    1680     5555804 :     p1 = gel(x,i);
    1681             :     /* we always enter this loop at least once */
    1682     5555804 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1683     5555002 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1684     5555002 :     if (!gequal0(p1)) { sx = 1; break; } /* remainder is non-zero */
    1685     3240253 :     if (!isexactzero(p1)) break;
    1686     3234544 :     if (!i) break;
    1687      626554 :     avma=av1;
    1688      626554 :   }
    1689     4929073 :   if (pr == ONLY_DIVIDES)
    1690             :   {
    1691         594 :     if (sx) { avma=av; return NULL; }
    1692         588 :     avma = (pari_sp)rem;
    1693         588 :     return gerepileupto(av,z-2);
    1694             :   }
    1695     4928479 :   lr=i+3; rem -= lr;
    1696     4928479 :   if (avma==av1) { avma = (pari_sp)rem; p1 = gcopy(p1); }
    1697     4869517 :   else p1 = gerepileupto((pari_sp)rem,p1);
    1698     4928625 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1699     4928782 :   rem[1] = z[-1];
    1700     4928782 :   rem += 2;
    1701     4928782 :   gel(rem,i) = p1;
    1702    13245369 :   for (i--; i>=0; i--)
    1703             :   {
    1704     8316749 :     av1=avma; p1 = gel(x,i);
    1705     8316749 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1706     8348434 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1707     8313241 :     gel(rem,i) = avma==av1? gcopy(p1):gerepileupto(av1,p1);
    1708             :   }
    1709     4928620 :   rem -= 2;
    1710     4928620 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1711     4928638 :   if (pr == ONLY_REM) return gerepileupto(av,rem);
    1712     3310452 :   z -= 2;
    1713             :   {
    1714     3310452 :     GEN *gptr[2]; gptr[0]=&z; gptr[1]=&rem;
    1715     3310452 :     gerepilemanysp(av,avy,gptr,2); *pr = rem; return z;
    1716             :   }
    1717             : }
    1718             : 
    1719             : /* x and y in (R[Y]/T)[X]  (lifted), T in R[Y]. y preferably monic */
    1720             : GEN
    1721       18150 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
    1722             : {
    1723             :   long vx, dx, dy, dz, i, j, sx, lr;
    1724             :   pari_sp av0, av, tetpil;
    1725             :   GEN z,p1,rem,lead;
    1726             : 
    1727       18150 :   if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
    1728       18150 :   vx = varn(x);
    1729       18150 :   dx = degpol(x);
    1730       18150 :   dy = degpol(y);
    1731       18150 :   if (dx < dy)
    1732             :   {
    1733        1212 :     if (pr)
    1734             :     {
    1735        1212 :       av0 = avma; x = RgXQX_red(x, T);
    1736        1212 :       if (pr == ONLY_DIVIDES) { avma=av0; return signe(x)? NULL: gen_0; }
    1737        1212 :       if (pr == ONLY_REM) return x;
    1738           0 :       *pr = x;
    1739             :     }
    1740           0 :     return pol_0(vx);
    1741             :   }
    1742       16938 :   lead = leading_coeff(y);
    1743       16938 :   if (!dy) /* y is constant */
    1744             :   {
    1745           6 :     if (pr && pr != ONLY_DIVIDES)
    1746             :     {
    1747           0 :       if (pr == ONLY_REM) return pol_0(vx);
    1748           0 :       *pr = pol_0(vx);
    1749             :     }
    1750           6 :     if (gequal1(lead)) return RgX_copy(x);
    1751           0 :     av0 = avma; x = gmul(x, ginvmod(lead,T)); tetpil = avma;
    1752           0 :     return gerepile(av0,tetpil,RgXQX_red(x,T));
    1753             :   }
    1754       16932 :   av0 = avma; dz = dx-dy;
    1755       16932 :   lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
    1756       16932 :   avma = av0;
    1757       16932 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1758       16932 :   x += 2; y += 2; z += 2;
    1759             : 
    1760       16932 :   p1 = gel(x,dx); av = avma;
    1761       16932 :   gel(z,dz) = lead? gerepileupto(av, grem(gmul(p1,lead), T)): gcopy(p1);
    1762       90726 :   for (i=dx-1; i>=dy; i--)
    1763             :   {
    1764       73794 :     av=avma; p1=gel(x,i);
    1765       73794 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1766       73794 :     if (lead) p1 = gmul(grem(p1, T), lead);
    1767       73794 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil, grem(p1, T));
    1768             :   }
    1769       16932 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
    1770             : 
    1771       16932 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
    1772       25656 :   for (sx=0; ; i--)
    1773             :   {
    1774       25656 :     p1 = gel(x,i);
    1775       25656 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1776       25656 :     tetpil=avma; p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
    1777       12348 :     if (!i) break;
    1778        8724 :     avma=av;
    1779        8724 :   }
    1780       16932 :   if (pr == ONLY_DIVIDES)
    1781             :   {
    1782        3372 :     if (lead) gunclone(lead);
    1783        3372 :     if (sx) { avma=av0; return NULL; }
    1784        3192 :     avma = (pari_sp)rem; return z-2;
    1785             :   }
    1786       13560 :   lr=i+3; rem -= lr;
    1787       13560 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1788       13560 :   rem[1] = z[-1];
    1789       13560 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
    1790       13560 :   rem += 2; gel(rem,i) = p1;
    1791       26214 :   for (i--; i>=0; i--)
    1792             :   {
    1793       12654 :     av=avma; p1 = gel(x,i);
    1794       31638 :     for (j=0; j<=i && j<=dz; j++)
    1795       18984 :       p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1796       12654 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, grem(p1, T));
    1797             :   }
    1798       13560 :   rem -= 2;
    1799       13560 :   if (lead) gunclone(lead);
    1800       13560 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1801       13560 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
    1802          36 :   *pr = rem; return z-2;
    1803             : }
    1804             : 
    1805             : /*******************************************************************/
    1806             : /*                                                                 */
    1807             : /*                        PSEUDO-DIVISION                          */
    1808             : /*                                                                 */
    1809             : /*******************************************************************/
    1810             : INLINE GEN
    1811      537702 : rem(GEN c, GEN T)
    1812             : {
    1813      537702 :   if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
    1814      537702 :   return c;
    1815             : }
    1816             : 
    1817             : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
    1818             : int
    1819         942 : ZXQX_dvd(GEN x, GEN y, GEN T)
    1820             : {
    1821             :   long dx, dy, dz, i, p, T_ismonic;
    1822         942 :   pari_sp av = avma, av2;
    1823             :   GEN y_lead;
    1824             : 
    1825         942 :   if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
    1826         942 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1827         942 :   if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
    1828             :   /* if monic, no point in using pseudo-division */
    1829         942 :   if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
    1830         546 :   T_ismonic = gequal1(leading_coeff(T));
    1831         546 :   dx = degpol(x);
    1832         546 :   if (dx < dy) return !signe(x);
    1833         546 :   (void)new_chunk(2);
    1834         546 :   x = RgX_recip_shallow(x)+2;
    1835         546 :   y = RgX_recip_shallow(y)+2;
    1836             :   /* pay attention to sparse divisors */
    1837        1200 :   for (i = 1; i <= dy; i++)
    1838         654 :     if (!signe(gel(y,i))) gel(y,i) = NULL;
    1839         546 :   dz = dx-dy; p = dz+1;
    1840         546 :   av2 = avma;
    1841             :   for (;;)
    1842             :   {
    1843        6126 :     GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
    1844        6126 :     x0 = gneg(x0); p--;
    1845        6126 :     m = gcdii(cx, y0);
    1846        6126 :     if (!equali1(m))
    1847             :     {
    1848        5490 :       x0 = gdiv(x0, m);
    1849        5490 :       y0 = diviiexact(y0, m);
    1850        5490 :       if (equali1(y0)) y0 = NULL;
    1851             :     }
    1852       12960 :     for (i=1; i<=dy; i++)
    1853             :     {
    1854        6834 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1855        6834 :       if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
    1856        6834 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1857        6834 :       gel(x,i) = c;
    1858             :     }
    1859       66246 :     for (   ; i<=dx; i++)
    1860             :     {
    1861       60120 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    1862       60120 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    1863       60120 :       gel(x,i) = c;
    1864             :     }
    1865        6792 :     do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
    1866        6126 :     if (dx < dy) break;
    1867        5580 :     if (gc_needed(av2,1))
    1868             :     {
    1869           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
    1870           0 :       gerepilecoeffs(av2,x,dx+1);
    1871             :     }
    1872        5580 :   }
    1873         546 :   avma = av; return (dx < 0);
    1874             : }
    1875             : 
    1876             : /* T either NULL or a t_POL. */
    1877             : GEN
    1878       20394 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
    1879             : {
    1880       20394 :   long vx = varn(x), dx, dy, dz, i, lx, p;
    1881       20394 :   pari_sp av = avma, av2;
    1882             :   GEN y_lead;
    1883             : 
    1884       20394 :   if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
    1885       20394 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1886             :   /* if monic, no point in using pseudo-division */
    1887       20394 :   if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
    1888       17298 :   dx = degpol(x);
    1889       17298 :   if (dx < dy) return RgX_copy(x);
    1890       17298 :   (void)new_chunk(2);
    1891       17298 :   x = RgX_recip_shallow(x)+2;
    1892       17298 :   y = RgX_recip_shallow(y)+2;
    1893             :   /* pay attention to sparse divisors */
    1894       53898 :   for (i = 1; i <= dy; i++)
    1895       36600 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    1896       17298 :   dz = dx-dy; p = dz+1;
    1897       17298 :   av2 = avma;
    1898             :   for (;;)
    1899             :   {
    1900       71406 :     gel(x,0) = gneg(gel(x,0)); p--;
    1901      224694 :     for (i=1; i<=dy; i++)
    1902             :     {
    1903      153288 :       GEN c = gmul(y_lead, gel(x,i));
    1904      153288 :       if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
    1905      153288 :       gel(x,i) = rem(c, T);
    1906             :     }
    1907      265986 :     for (   ; i<=dx; i++)
    1908             :     {
    1909      194580 :       GEN c = gmul(y_lead, gel(x,i));
    1910      194580 :       gel(x,i) = rem(c, T);
    1911             :     }
    1912       77076 :     do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
    1913       71406 :     if (dx < dy) break;
    1914       54108 :     if (gc_needed(av2,1))
    1915             :     {
    1916           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
    1917           0 :       gerepilecoeffs(av2,x,dx+1);
    1918             :     }
    1919       54108 :   }
    1920       17298 :   if (dx < 0) return pol_0(vx);
    1921       16002 :   lx = dx+3; x -= 2;
    1922       16002 :   x[0] = evaltyp(t_POL) | evallg(lx);
    1923       16002 :   x[1] = evalsigne(1) | evalvarn(vx);
    1924       16002 :   x = RgX_recip_shallow(x);
    1925       16002 :   if (p)
    1926             :   { /* multiply by y[0]^p   [beware dummy vars from FpX_FpXY_resultant] */
    1927         924 :     GEN t = y_lead;
    1928         924 :     if (T && typ(t) == t_POL && varn(t) == varn(T))
    1929           0 :       t = RgXQ_powu(t, p, T);
    1930             :     else
    1931         924 :       t = gpowgs(t, p);
    1932        2940 :     for (i=2; i<lx; i++)
    1933             :     {
    1934        2016 :       GEN c = gmul(gel(x,i), t);
    1935        2016 :       gel(x,i) = rem(c,T);
    1936             :     }
    1937         924 :     if (!T) return gerepileupto(av, x);
    1938             :   }
    1939       15078 :   return gerepilecopy(av, x);
    1940             : }
    1941             : 
    1942             : GEN
    1943       20394 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
    1944             : 
    1945             : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
    1946             : GEN
    1947       38994 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
    1948             : {
    1949       38994 :   long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
    1950       38994 :   pari_sp av = avma, av2;
    1951             :   GEN z, r, ypow, y_lead;
    1952             : 
    1953       38994 :   if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
    1954       38994 :   dy = degpol(y); y_lead = gel(y,dy+2);
    1955       38994 :   if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
    1956       18348 :   dx = degpol(x);
    1957       18348 :   if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
    1958       18348 :   if (dx == dy)
    1959             :   {
    1960          24 :     GEN x_lead = gel(x,lg(x)-1);
    1961          24 :     x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
    1962          24 :     y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
    1963          24 :     r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
    1964          24 :     *ptr = gerepileupto(av, r); return scalarpol(x_lead, vx);
    1965             :   }
    1966       18324 :   (void)new_chunk(2);
    1967       18324 :   x = RgX_recip_shallow(x)+2;
    1968       18324 :   y = RgX_recip_shallow(y)+2;
    1969             :   /* pay attention to sparse divisors */
    1970       75516 :   for (i = 1; i <= dy; i++)
    1971       57192 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    1972       18324 :   dz = dx-dy; p = dz+1;
    1973       18324 :   lz = dz+3;
    1974       18324 :   z = cgetg(lz, t_POL);
    1975       18324 :   z[1] = evalsigne(1) | evalvarn(vx);
    1976       18324 :   for (i = 2; i < lz; i++) gel(z,i) = gen_0;
    1977       18324 :   ypow = new_chunk(dz+1);
    1978       18324 :   gel(ypow,0) = gen_1;
    1979       18324 :   gel(ypow,1) = y_lead;
    1980       23814 :   for (i=2; i<=dz; i++)
    1981             :   {
    1982        5490 :     GEN c = gmul(gel(ypow,i-1), y_lead);
    1983        5490 :     gel(ypow,i) = rem(c,T);
    1984             :   }
    1985       18324 :   av2 = avma;
    1986       18324 :   for (iz=2;;)
    1987             :   {
    1988       37194 :     p--;
    1989       37194 :     gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
    1990      149628 :     for (i=1; i<=dy; i++)
    1991             :     {
    1992      112434 :       GEN c = gmul(y_lead, gel(x,i));
    1993      112434 :       if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
    1994      112434 :       gel(x,i) = rem(c, T);
    1995             :     }
    1996       69894 :     for (   ; i<=dx; i++)
    1997             :     {
    1998       32700 :       GEN c = gmul(y_lead, gel(x,i));
    1999       32700 :       gel(x,i) = rem(c,T);
    2000             :     }
    2001       37194 :     x++; dx--;
    2002       37194 :     while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
    2003       37194 :     if (dx < dy) break;
    2004       18870 :     if (gc_needed(av2,1))
    2005             :     {
    2006           0 :       GEN X = x-2;
    2007           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
    2008           0 :       X[0] = evaltyp(t_POL)|evallg(dx+3); X[1] = z[1]; /* hack */
    2009           0 :       gerepileall(av2,2, &X, &z); x = X+2;
    2010             :     }
    2011       18870 :   }
    2012       18324 :   while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
    2013       18324 :   if (dx < 0)
    2014          84 :     x = pol_0(vx);
    2015             :   else
    2016             :   {
    2017       18240 :     lx = dx+3; x -= 2;
    2018       18240 :     x[0] = evaltyp(t_POL) | evallg(lx);
    2019       18240 :     x[1] = evalsigne(1) | evalvarn(vx);
    2020       18240 :     x = RgX_recip_shallow(x);
    2021             :   }
    2022       18324 :   z = RgX_recip_shallow(z);
    2023       18324 :   r = x;
    2024       18324 :   if (p)
    2025             :   {
    2026        3384 :     GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
    2027        3384 :     if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
    2028             :   }
    2029       18324 :   gerepileall(av, 2, &z, &r);
    2030       18324 :   *ptr = r; return z;
    2031             : }
    2032             : GEN
    2033       38892 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
    2034       38892 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
    2035             : 
    2036             : GEN
    2037        1422 : RgXQX_mul(GEN x, GEN y, GEN T)
    2038             : {
    2039        1422 :   return RgXQX_red(RgX_mul(x,y), T);
    2040             : }
    2041             : GEN
    2042    53116518 : RgX_Rg_mul(GEN y, GEN x) {
    2043             :   long i, ly;
    2044    53116518 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2045    53116518 :   if (ly == 2) return z;
    2046    53067198 :   for (i = 2; i < ly; i++) gel(z,i) = gmul(x,gel(y,i));
    2047    53067192 :   return normalizepol_lg(z,ly);
    2048             : }
    2049             : GEN
    2050         528 : RgX_muls(GEN y, long x) {
    2051             :   long i, ly;
    2052         528 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2053         528 :   if (ly == 2) return z;
    2054         498 :   for (i = 2; i < ly; i++) gel(z,i) = gmulsg(x,gel(y,i));
    2055         498 :   return normalizepol_lg(z,ly);
    2056             : }
    2057             : GEN
    2058          24 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T)
    2059             : {
    2060          24 :   return RgXQX_red(RgX_Rg_mul(x,y), T);
    2061             : }
    2062             : GEN
    2063          48 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T)
    2064             : {
    2065          48 :   return RgXQV_red(RgV_Rg_mul(v,x), T);
    2066             : }
    2067             : 
    2068             : GEN
    2069           0 : RgXQX_sqr(GEN x, GEN T)
    2070             : {
    2071           0 :   return RgXQX_red(RgX_sqr(x), T);
    2072             : }
    2073             : 
    2074             : static GEN
    2075       53868 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
    2076             : static GEN
    2077           0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
    2078             : static GEN
    2079      181584 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
    2080             : static GEN
    2081       74522 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
    2082             : static GEN
    2083       91380 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
    2084             : static GEN
    2085       86682 : _one(void *data) { return pol_1(varn((GEN)data)); }
    2086             : static GEN
    2087          90 : _zero(void *data) { return pol_0(varn((GEN)data)); }
    2088             : static GEN
    2089       58458 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
    2090             : 
    2091             : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
    2092             :               _mul, _sqr, _one, _zero };
    2093             : 
    2094             : GEN
    2095           0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
    2096             : {
    2097           0 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
    2098             : }
    2099             : 
    2100             : GEN
    2101       37020 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
    2102             : {
    2103       37020 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2104       37020 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
    2105             : }
    2106             : 
    2107             : /* mod X^n */
    2108             : struct modXn {
    2109             :   long v; /* varn(X) */
    2110             :   long n;
    2111             : } ;
    2112             : static GEN
    2113        1530 : _sqrXn(void *data, GEN x) {
    2114        1530 :   struct modXn *S = (struct modXn*)data;
    2115        1530 :   return RgXn_sqr(x, S->n);
    2116             : }
    2117             : static GEN
    2118        1008 : _mulXn(void *data, GEN x, GEN y) {
    2119        1008 :   struct modXn *S = (struct modXn*)data;
    2120        1008 :   return RgXn_mul(x,y, S->n);
    2121             : }
    2122             : static GEN
    2123        1206 : _oneXn(void *data) {
    2124        1206 :   struct modXn *S = (struct modXn*)data;
    2125        1206 :   return pol_1(S->v);
    2126             : }
    2127             : static GEN
    2128           0 : _zeroXn(void *data) {
    2129           0 :   struct modXn *S = (struct modXn*)data;
    2130           0 :   return pol_0(S->v);
    2131             : }
    2132             : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
    2133             :                                           _oneXn, _zeroXn };
    2134             : 
    2135             : GEN
    2136         288 : RgXn_powers(GEN x, long m, long n)
    2137             : {
    2138         288 :   long d = degpol(x);
    2139         288 :   int use_sqr = (d<<1) >= n;
    2140             :   struct modXn S;
    2141         288 :   S.v = varn(x); S.n = n;
    2142         288 :   return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
    2143             : }
    2144             : 
    2145             : GEN
    2146        1290 : RgXn_powu_i(GEN x, ulong m, long n)
    2147             : {
    2148             :   struct modXn S;
    2149        1290 :   S.v = varn(x); S.n = n;
    2150        1290 :   return gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
    2151             : }
    2152             : GEN
    2153           0 : RgXn_powu(GEN x, ulong m, long n)
    2154             : {
    2155             :   struct modXn S;
    2156           0 :   S.v = varn(x); S.n = n;
    2157           0 :   return gen_powu(x, m, (void*)&S,_sqrXn,_mulXn);
    2158             : }
    2159             : 
    2160             : GEN
    2161         576 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
    2162             : {
    2163             :   struct modXn S;
    2164         576 :   S.v = varn(gel(x,2)); S.n = n;
    2165         576 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
    2166             : }
    2167             : 
    2168             : GEN
    2169           0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
    2170             : {
    2171           0 :   int use_sqr = 2*degpol(x) >= n;
    2172             :   struct modXn S;
    2173           0 :   S.v = varn(x); S.n = n;
    2174           0 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2175             : }
    2176             : 
    2177             : /* Q(x) mod t^n, x in R[t], n >= 1 */
    2178             : GEN
    2179        1338 : RgXn_eval(GEN Q, GEN x, long n)
    2180             : {
    2181        1338 :   long d = degpol(x);
    2182             :   int use_sqr;
    2183             :   struct modXn S;
    2184        1338 :   if (d == 1 && isrationalzero(gel(x,2)))
    2185             :   {
    2186        1332 :     GEN y = RgX_unscale(Q, gel(x,3));
    2187        1332 :     setvarn(y, varn(x)); return y;
    2188             :   }
    2189           6 :   S.v = varn(x);
    2190           6 :   S.n = n;
    2191           6 :   use_sqr = (d<<1) >= n;
    2192           6 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2193             : }
    2194             : 
    2195             : /* (f*g mod t^n) \ t^n2, assuming 2*n2 >= n */
    2196             : static GEN
    2197       16890 : RgXn_mulhigh(GEN f, GEN g, long n2, long n)
    2198             : {
    2199       16890 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2200       16890 :   return RgX_add(RgX_mulhigh_i(fl, g, n2), RgXn_mul(fh, g, n - n2));
    2201             : }
    2202             : 
    2203             : GEN
    2204       84258 : RgXn_inv(GEN f, long e)
    2205             : {
    2206       84258 :   pari_sp av = avma, av2;
    2207             :   ulong mask;
    2208             :   GEN W, a;
    2209       84258 :   long v = varn(f), n = 1;
    2210             : 
    2211       84258 :   if (!signe(f)) pari_err_INV("RgXn_inv",f);
    2212       84258 :   a = ginv(gel(f,2));
    2213       84258 :   if (e == 1) return scalarpol(a, v);
    2214       84258 :   else if (e == 2)
    2215             :   {
    2216             :     GEN b;
    2217       77646 :     if (degpol(f) <= 0 || gequal0(b = gel(f,3))) return scalarpol(a, v);
    2218       74466 :     b = gneg(b);
    2219       74466 :     if (!gequal1(a)) b = gmul(b, gsqr(a));
    2220       74466 :     W = deg1pol_shallow(b, a, v);
    2221       74466 :     return gerepilecopy(av, W);
    2222             :   }
    2223        6612 :   W = scalarpol_shallow(ginv(gel(f,2)),v);
    2224        6612 :   mask = quadratic_prec_mask(e);
    2225        6612 :   av2 = avma;
    2226       30114 :   for (;mask>1;)
    2227             :   {
    2228             :     GEN u, fr;
    2229       16890 :     long n2 = n;
    2230       16890 :     n<<=1; if (mask & 1) n--;
    2231       16890 :     mask >>= 1;
    2232       16890 :     fr = RgXn_red_shallow(f, n);
    2233       16890 :     u = RgXn_mul(W, RgXn_mulhigh(fr, W, n2, n), n-n2);
    2234       16890 :     W = RgX_sub(W, RgX_shift_shallow(u, n2));
    2235       16890 :     if (gc_needed(av2,2))
    2236             :     {
    2237           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
    2238           0 :       W = gerepileupto(av2, W);
    2239             :     }
    2240             :   }
    2241        6612 :   return gerepileupto(av, W);
    2242             : }
    2243             : 
    2244             : GEN
    2245         174 : RgXn_exp(GEN h, long e)
    2246             : {
    2247         174 :   pari_sp av = avma, av2;
    2248         174 :   long v = varn(h), n=1;
    2249         174 :   GEN f = pol_1(v), g = pol_1(v);
    2250         174 :   ulong mask = quadratic_prec_mask(e);
    2251         174 :   av2 = avma;
    2252         174 :   if (signe(h)==0 || degpol(h)<1 || !gequal0(gel(h,2)))
    2253           0 :     pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
    2254         678 :   for (;mask>1;)
    2255             :   {
    2256             :     GEN q, w;
    2257         330 :     long n2 = n;
    2258         330 :     n<<=1; if (mask & 1) n--;
    2259         330 :     mask >>= 1;
    2260         330 :     g = RgX_sub(RgX_muls(g,2),RgXn_mul(f,RgXn_sqr(g,n2),n2));
    2261         330 :     q = RgX_deriv(RgXn_red_shallow(h,n2));
    2262         330 :     w = RgX_add(q, RgXn_mul(g, RgX_sub(RgX_deriv(f), RgXn_mul(f,q,n-1)),n-1));
    2263         330 :     f = RgX_add(f, RgXn_mul(f, RgX_sub(RgXn_red_shallow(h, n), RgX_integ(w)), n));
    2264         330 :     if (gc_needed(av2,2))
    2265             :     {
    2266           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_exp, e = %ld", n);
    2267           0 :       gerepileall(av2, 2, &f, &g);
    2268             :     }
    2269             :   }
    2270         174 :   return gerepileupto(av, f);
    2271             : }
    2272             : 
    2273             : GEN
    2274          72 : RgXn_reverse(GEN f, long e)
    2275             : {
    2276          72 :   pari_sp av = avma, av2;
    2277             :   ulong mask;
    2278             :   GEN fi, a, df, W, an;
    2279          72 :   long v = varn(f), n=1;
    2280          72 :   if (degpol(f)<1 || !gequal0(gel(f,2)))
    2281           0 :     pari_err_INV("serreverse",f);
    2282          72 :   fi = ginv(gel(f,3));
    2283          72 :   a = deg1pol_shallow(fi,gen_0,v);
    2284          72 :   if (e <= 2) return gerepilecopy(av, a);
    2285          72 :   W = scalarpol(fi,v);
    2286          72 :   df = RgX_deriv(f);
    2287          72 :   mask = quadratic_prec_mask(e);
    2288          72 :   av2 = avma;
    2289         432 :   for (;mask>1;)
    2290             :   {
    2291             :     GEN u, fa, fr;
    2292         288 :     long n2 = n, rt;
    2293         288 :     n<<=1; if (mask & 1) n--;
    2294         288 :     mask >>= 1;
    2295         288 :     fr = RgXn_red_shallow(f, n);
    2296         288 :     rt = brent_kung_optpow(degpol(fr), 4, 3);
    2297         288 :     an = RgXn_powers(a, rt, n);
    2298         288 :     if (n>1)
    2299             :     {
    2300         288 :       long n4 = (n2+1)>>1;
    2301         288 :       GEN dfr = RgXn_red_shallow(df, n2);
    2302         288 :       dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
    2303         288 :       u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
    2304         288 :       W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
    2305             :     }
    2306         288 :     fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
    2307         288 :     fa = RgX_shift(fa, -n2);
    2308         288 :     a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
    2309         288 :     if (gc_needed(av2,2))
    2310             :     {
    2311           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
    2312           0 :       gerepileall(av2, 2, &a, &W);
    2313             :     }
    2314             :   }
    2315          72 :   return gerepileupto(av, a);
    2316             : }
    2317             : 
    2318             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2319             : GEN
    2320      157008 : RgXQ_powu(GEN x, ulong n, GEN T)
    2321             : {
    2322             :   pari_sp av;
    2323             :   GEN y;
    2324             : 
    2325      157008 :   if (!n) return pol_1(varn(x));
    2326      155694 :   if (n == 1) return RgX_copy(x);
    2327      105532 :   av = avma;
    2328      105532 :   y = gen_powu(x, n, (void*)T, &_sqr, &_mul);
    2329      105532 :   return gerepileupto(av, y);
    2330             : }
    2331             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2332             : GEN
    2333       14916 : RgXQ_pow(GEN x, GEN n, GEN T)
    2334             : {
    2335             :   pari_sp av;
    2336       14916 :   long s = signe(n);
    2337             :   GEN y;
    2338             : 
    2339       14916 :   if (!s) return pol_1(varn(x));
    2340       14916 :   if (is_pm1(n) == 1)
    2341           0 :     return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
    2342       14916 :   av = avma;
    2343       14916 :   if (s < 0) x = RgXQ_inv(x, T);
    2344       14916 :   y = gen_pow(x, n, (void*)T, &_sqr, &_mul);
    2345       14916 :   return gerepileupto(av, y);
    2346             : }
    2347             : 
    2348             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2349             : GEN
    2350        1494 : RgXQ_powers(GEN x, long l, GEN T)
    2351             : {
    2352        1494 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2353        1494 :   return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
    2354             : }
    2355             : 
    2356             : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
    2357             : GEN
    2358        1230 : QXQ_powers(GEN a, long n, GEN T)
    2359             : {
    2360        1230 :   GEN den, v = RgXQ_powers(Q_remove_denom(a, &den), n, T);
    2361             :   /* den*a integral; v[i+1] = (den*a)^i in K */
    2362        1230 :   if (den)
    2363             :   { /* restore denominators */
    2364         744 :     GEN d = den;
    2365             :     long i;
    2366         744 :     gel(v,2) = a;
    2367        2850 :     for (i=3; i<=n+1; i++) {
    2368        2106 :       d = mulii(d,den);
    2369        2106 :       gel(v,i) = RgX_Rg_div(gel(v,i), d);
    2370             :     }
    2371             :   }
    2372        1230 :   return v;
    2373             : }
    2374             : 
    2375             : static GEN
    2376         726 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
    2377             : {
    2378         726 :   long l, i, m = 0;
    2379             :   GEN dz, z;
    2380         726 :   GEN V = cgetg_copy(v, &l);
    2381        2382 :   for (i = imin; i < l; i++)
    2382             :   {
    2383        1656 :     GEN c = gel(v, i);
    2384        1656 :     if (typ(c) == t_POL) m = maxss(m, degpol(c));
    2385             :   }
    2386         726 :   z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
    2387         726 :   for (i = 1; i < imin; i++) V[i] = v[i];
    2388        2382 :   for (i = imin; i < l; i++)
    2389             :   {
    2390        1656 :     GEN c = gel(v,i);
    2391        1656 :     if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
    2392        1656 :     gel(V,i) = c;
    2393             :   }
    2394         726 :   return V;
    2395             : }
    2396             : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
    2397             : GEN
    2398         672 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
    2399         672 : { return do_QXQ_eval(v, 1, a, T); }
    2400             : GEN
    2401          54 : QXX_QXQ_eval(GEN v, GEN a, GEN T)
    2402          54 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
    2403             : 
    2404             : GEN
    2405         246 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
    2406             : {
    2407         246 :   return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
    2408             : }
    2409             : 
    2410             : GEN
    2411          48 : RgXQ_minpoly_naive(GEN y, GEN P)
    2412             : {
    2413          48 :   pari_sp ltop=avma;
    2414          48 :   long n=lgpol(P);
    2415          48 :   GEN M=ker(RgXQ_matrix_pow(y,n,n,P));
    2416          48 :   M=content(RgM_to_RgXV(M,varn(P)));
    2417          48 :   return gerepileupto(ltop,M);
    2418             : }
    2419             : 
    2420             : GEN
    2421       28056 : RgXQ_norm(GEN x, GEN T)
    2422             : {
    2423             :   pari_sp av;
    2424       28056 :   long dx = degpol(x);
    2425             :   GEN L, y;
    2426             : 
    2427       28056 :   av = avma; y = resultant(T, x);
    2428       28056 :   L = leading_coeff(T);
    2429       28056 :   if (gequal1(L) || !signe(x)) return y;
    2430           0 :   return gerepileupto(av, gdiv(y, gpowgs(L, dx)));
    2431             : }
    2432             : 
    2433             : GEN
    2434       77034 : RgX_blocks(GEN P, long n, long m)
    2435             : {
    2436       77034 :   GEN z = cgetg(m+1,t_VEC);
    2437       77034 :   long i,j, k=2, l = lg(P);
    2438      396918 :   for(i=1; i<=m; i++)
    2439             :   {
    2440      319884 :     GEN zi = cgetg(n+2,t_POL);
    2441      319884 :     zi[1] = P[1];
    2442      319884 :     gel(z,i) = zi;
    2443     1960962 :     for(j=2; j<n+2; j++)
    2444     1641078 :       gel(zi, j) = k==l ? gen_0 : gel(P,k++);
    2445      319884 :     zi = RgX_renormalize_lg(zi, n+2);
    2446             :   }
    2447       77034 :   return z;
    2448             : }
    2449             : 
    2450             : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
    2451             : void
    2452       21285 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
    2453             : {
    2454       21285 :   long n = degpol(p), v = varn(p), n0, n1, i;
    2455             :   GEN p0, p1;
    2456             : 
    2457       42573 :   if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
    2458             : 
    2459       21286 :   n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
    2460       21286 :   p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
    2461       21286 :   p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
    2462      559495 :   for (i=0; i<n1; i++)
    2463             :   {
    2464      538207 :     p0[2+i] = p[2+(i<<1)];
    2465      538207 :     p1[2+i] = p[3+(i<<1)];
    2466             :   }
    2467       21288 :   if (n1 != n0)
    2468       15053 :     p0[2+i] = p[2+(i<<1)];
    2469       21288 :   *pe = normalizepol(p0);
    2470       21286 :   *po = normalizepol(p1);
    2471             : }
    2472             : 
    2473             : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
    2474             : GEN
    2475       34836 : RgX_splitting(GEN p, long k)
    2476             : {
    2477       34836 :   long n = degpol(p), v = varn(p), m, i, j, l;
    2478             :   GEN r;
    2479             : 
    2480       34836 :   m = n/k;
    2481       34836 :   r = cgetg(k+1,t_VEC);
    2482      192132 :   for(i=1; i<=k; i++)
    2483             :   {
    2484      157296 :     gel(r,i) = cgetg(m+3, t_POL);
    2485      157296 :     mael(r,i,1) = evalvarn(v)|evalsigne(1);
    2486             :   }
    2487      473988 :   for (j=1, i=0, l=2; i<=n; i++)
    2488             :   {
    2489      439152 :     gmael(r,j,l) = gel(p,2+i);
    2490      439152 :     if (j==k) { j=1; l++; } else j++;
    2491             :   }
    2492      192132 :   for(i=1; i<=k; i++)
    2493      157296 :     gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
    2494       34836 :   return r;
    2495             : }
    2496             : 
    2497             : /*******************************************************************/
    2498             : /*                                                                 */
    2499             : /*                        Kronecker form                           */
    2500             : /*                                                                 */
    2501             : /*******************************************************************/
    2502             : 
    2503             : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
    2504             :  * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
    2505             :  * normalized coefficients */
    2506             : GEN
    2507         162 : Kronecker_to_mod(GEN z, GEN T)
    2508             : {
    2509         162 :   long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
    2510         162 :   GEN x, t = cgetg(N,t_POL);
    2511         162 :   t[1] = T[1];
    2512         162 :   lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
    2513         162 :   x[1] = z[1];
    2514         162 :   T = RgX_copy(T);
    2515        3762 :   for (i=2; i<lx+2; i++, z+= N-2)
    2516             :   {
    2517        3600 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
    2518        3600 :     gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2519             :   }
    2520         162 :   N = (l-2) % (N-2) + 2;
    2521         162 :   for (j=2; j<N; j++) t[j] = z[j];
    2522         162 :   gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2523         162 :   return normalizepol_lg(x, i+1);
    2524             : }

Generated by: LCOV version 1.11