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 20924-e159ed0) Lines: 1397 1549 90.2 %
Date: 2017-08-21 06:23:16 Functions: 153 169 90.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. 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     2291213 : brent_kung_optpow(long d, long n, long m)
      29             : {
      30             :   long p, r;
      31     2291213 :   long pold=1, rold=n*(d-1);
      32    12654756 :   for(p=2; p<=d; p++)
      33             :   {
      34    10363543 :     r = m*(p-1) + n*((d-1)/p);
      35    10363543 :     if (r<rold) { pold=p; rold=r; }
      36             :   }
      37     2291213 :   return pold;
      38             : }
      39             : 
      40             : static GEN
      41     9909068 : 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     9909068 :   pari_sp av = avma;
      45             :   long i;
      46     9909068 :   GEN z = cmul(E,P,a,ff->one(E));
      47     9909007 :   if (!z) z = gen_0;
      48    61043123 :   for (i=1; i<=n; i++)
      49             :   {
      50    51134096 :     GEN t = cmul(E,P,a+i,gel(V,i+1));
      51    51134165 :     if (t) {
      52    49797022 :       z = ff->add(E, z, t);
      53    49793774 :       if (gc_needed(av,2)) z = gerepileupto(av, z);
      54             :     }
      55             :   }
      56     9909027 :   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     6406216 : 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     6406216 :   pari_sp av = avma;
      69     6406216 :   long l = lg(V)-1;
      70             :   GEN z, u;
      71             : 
      72     6406216 :   if (d < 0) return ff->zero(E);
      73     5899391 :   if (d < l) return gerepileupto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
      74     2381022 :   if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
      75     2381022 :   if (DEBUGLEVEL>=8)
      76             :   {
      77           0 :     long cnt = 1 + (d - l) / (l-1);
      78           0 :     err_printf("RgX_RgXQV_eval(%ld/%ld): %ld RgXQ_mul\n", d, l-1, cnt);
      79             :   }
      80     2381022 :   d -= l;
      81     2381022 :   z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
      82     6390700 :   while (d >= l-1)
      83             :   {
      84     1628660 :     d -= l-1;
      85     1628660 :     u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
      86     1628634 :     z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
      87     1628657 :     if (gc_needed(av,2))
      88          61 :       z = gerepileupto(av, z);
      89             :   }
      90     2381021 :   u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
      91     2381019 :   z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
      92     2381018 :   return gerepileupto(av, ff->red(E,z));
      93             : }
      94             : 
      95             : GEN
      96     1061630 : 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     1061630 :   pari_sp av = avma;
     100             :   GEN z, V;
     101             :   long rtd;
     102     1061630 :   if (d < 0) return ff->zero(E);
     103     1061525 :   rtd = (long) sqrt((double)d);
     104     1061525 :   V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
     105     1061525 :   z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
     106     1061525 :   return gerepileupto(av, z);
     107             : }
     108             : 
     109             : static GEN
     110      729963 : _gen_nored(void *E, GEN x) { (void)E; return x; }
     111             : static GEN
     112    20663605 : _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      607091 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
     117             : static GEN
     118      197507 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
     119             : static GEN
     120      744691 : _gen_one(void *E) { (void)E; return gen_1; }
     121             : static GEN
     122       14553 : _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      353010 : _gen_cmul(void *E, GEN P, long a, GEN x)
     129      353010 : {(void)E; return gmul(gel(P,a+2), x);}
     130             : 
     131             : GEN
     132      125720 : RgX_RgV_eval(GEN Q, GEN x)
     133             : {
     134      125720 :   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        2814 : RgXV_RgV_eval(GEN Q, GEN x)
     145             : {
     146        2814 :   long i, l = lg(Q), vQ = gvar(Q);
     147        2814 :   GEN v = cgetg(l, t_VEC);
     148      237538 :   for (i = 1; i < l; i++)
     149             :   {
     150      234724 :     GEN Qi = gel(Q, i);
     151      234724 :     gel(v, i) = typ(Qi)==t_POL && varn(Qi)==vQ? RgX_RgV_eval(Qi, x): gcopy(Qi);
     152             :   }
     153        2814 :   return v;
     154             : }
     155             : 
     156             : const struct bb_algebra *
     157       92614 : get_Rg_algebra(void)
     158             : {
     159       92614 :   return &Rg_algebra;
     160             : }
     161             : 
     162             : static struct bb_ring Rg_ring = {  _gen_add, _gen_mul, _gen_sqr };
     163             : 
     164             : static GEN
     165        9058 : _RgX_divrem(void *E, GEN x, GEN y, GEN *r)
     166             : {
     167             :   (void) E;
     168        9058 :   return RgX_divrem(x, y, r);
     169             : }
     170             : 
     171             : GEN
     172        2219 : RgX_digits(GEN x, GEN T)
     173             : {
     174        2219 :   pari_sp av = avma;
     175        2219 :   long d = degpol(T), n = (lgpol(x)+d-1)/d;
     176        2219 :   GEN z = gen_digits(x,T,n,NULL, &Rg_ring, _RgX_divrem);
     177        2219 :   return gerepileupto(av, z);
     178             : }
     179             : 
     180             : /*******************************************************************/
     181             : /*                                                                 */
     182             : /*                         RgX                                     */
     183             : /*                                                                 */
     184             : /*******************************************************************/
     185             : 
     186             : long
     187    28796642 : RgX_equal(GEN x, GEN y)
     188             : {
     189    28796642 :   long i = lg(x);
     190             : 
     191    28796642 :   if (i != lg(y)) return 0;
     192   119816927 :   for (i--; i > 1; i--)
     193    91082074 :     if (!gequal(gel(x,i),gel(y,i))) return 0;
     194    28734853 :   return 1;
     195             : }
     196             : 
     197             : /* Returns 1 in the base ring over which x is defined */
     198             : /* HACK: this also works for t_SER */
     199             : GEN
     200      683870 : RgX_get_1(GEN x)
     201             : {
     202             :   GEN p, T;
     203      683870 :   long i, lx, tx = RgX_type(x, &p, &T, &lx);
     204      683870 :   if (RgX_type_is_composite(tx))
     205      477208 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     206      683870 :   switch(tx)
     207             :   {
     208          84 :     case t_INTMOD: retmkintmod(is_pm1(p)? gen_0: gen_1, icopy(p));
     209           7 :     case t_PADIC: return cvtop(gen_1, p, lx);
     210          35 :     case t_FFELT: return FF_1(T);
     211      683744 :     default: return gen_1;
     212             :   }
     213             : }
     214             : /* Returns 0 in the base ring over which x is defined */
     215             : /* HACK: this also works for t_SER */
     216             : GEN
     217      133203 : RgX_get_0(GEN x)
     218             : {
     219             :   GEN p, T;
     220      133203 :   long i, lx, tx = RgX_type(x, &p, &T, &lx);
     221      133203 :   if (RgX_type_is_composite(tx))
     222       28161 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     223      133203 :   switch(tx)
     224             :   {
     225         259 :     case t_INTMOD: retmkintmod(gen_0, icopy(p));
     226           0 :     case t_PADIC: return cvtop(gen_0, p, lx);
     227         210 :     case t_FFELT: return FF_zero(T);
     228      132734 :     default: return gen_0;
     229             :   }
     230             : }
     231             : 
     232             : GEN
     233        2562 : QX_ZXQV_eval(GEN P, GEN V, GEN dV)
     234             : {
     235        2562 :   long i, n = degpol(P);
     236             :   GEN z, dz, dP;
     237        2562 :   if (n < 0) return gen_0;
     238        2562 :   P = Q_remove_denom(P, &dP);
     239        2562 :   z = gel(P,2); if (n == 0) return icopy(z);
     240        1386 :   if (dV) z = mulii(dV, z); /* V[1] = dV */
     241        1386 :   z = ZX_Z_add_shallow(ZX_Z_mul(gel(V,2),gel(P,3)), z);
     242        1386 :   for (i=2; i<=n; i++) z = ZX_add(ZX_Z_mul(gel(V,i+1),gel(P,2+i)), z);
     243        1386 :   dz = mul_denom(dP, dV);
     244        1386 :   return dz? RgX_Rg_div(z, dz): z;
     245             : }
     246             : 
     247             : /* Return P(h * x), not memory clean */
     248             : GEN
     249        3913 : RgX_unscale(GEN P, GEN h)
     250             : {
     251        3913 :   long i, l = lg(P);
     252        3913 :   GEN hi = gen_1, Q = cgetg(l, t_POL);
     253        3913 :   Q[1] = P[1];
     254        3913 :   if (l == 2) return Q;
     255        3913 :   gel(Q,2) = gcopy(gel(P,2));
     256       21987 :   for (i=3; i<l; i++)
     257             :   {
     258       18074 :     hi = gmul(hi,h);
     259       18074 :     gel(Q,i) = gmul(gel(P,i), hi);
     260             :   }
     261        3913 :   return Q;
     262             : }
     263             : /* P a ZX, Return P(h * x), not memory clean; optimize for h = -1 */
     264             : GEN
     265      892605 : ZX_z_unscale(GEN P, long h)
     266             : {
     267      892605 :   long i, l = lg(P);
     268      892605 :   GEN Q = cgetg(l, t_POL);
     269      892605 :   Q[1] = P[1];
     270      892605 :   if (l == 2) return Q;
     271      892605 :   gel(Q,2) = gel(P,2);
     272      892605 :   if (l == 3) return Q;
     273      892605 :   if (h == -1)
     274      306411 :     for (i = 3; i < l; i++)
     275             :     {
     276      299424 :       gel(Q,i) = negi(gel(P,i));
     277      299424 :       if (++i == l) break;
     278      296163 :       gel(Q,i) = gel(P,i);
     279             :     }
     280             :   else
     281             :   {
     282             :     GEN hi;
     283      882357 :     gel(Q,3) = mulis(gel(P,3), h);
     284      882357 :     hi = sqrs(h);
     285     1830052 :     for (i = 4; i < l; i++)
     286             :     {
     287      947695 :       gel(Q,i) = mulii(gel(P,i), hi);
     288      947695 :       if (i != l-1) hi = mulis(hi,h);
     289             :     }
     290             :   }
     291      892605 :   return Q;
     292             : }
     293             : /* P a ZX, h a t_INT. Return P(h * x), not memory clean; optimize for h = -1 */
     294             : GEN
     295        7336 : ZX_unscale(GEN P, GEN h)
     296             : {
     297             :   long i, l;
     298             :   GEN Q, hi;
     299        7336 :   i = itos_or_0(h); if (i) return ZX_z_unscale(P, i);
     300          14 :   l = lg(P); Q = cgetg(l, t_POL);
     301          14 :   Q[1] = P[1];
     302          14 :   if (l == 2) return Q;
     303          14 :   gel(Q,2) = gel(P,2);
     304          14 :   if (l == 3) return Q;
     305          14 :   hi = h;
     306          14 :   gel(Q,3) = mulii(gel(P,3), hi);
     307          91 :   for (i = 4; i < l; i++)
     308             :   {
     309          77 :     hi = mulii(hi,h);
     310          77 :     gel(Q,i) = mulii(gel(P,i), hi);
     311             :   }
     312          14 :   return Q;
     313             : }
     314             : /* P a ZX. Return P(x << n), not memory clean */
     315             : GEN
     316       18161 : ZX_unscale2n(GEN P, long n)
     317             : {
     318       18161 :   long i, ni = n, l = lg(P);
     319       18161 :   GEN Q = cgetg(l, t_POL);
     320       18161 :   Q[1] = P[1];
     321       18161 :   if (l == 2) return Q;
     322       18161 :   gel(Q,2) = gel(P,2);
     323       18161 :   if (l == 3) return Q;
     324       18161 :   gel(Q,3) = shifti(gel(P,3), ni);
     325       73637 :   for (i=4; i<l; i++)
     326             :   {
     327       55476 :     ni += n;
     328       55476 :     gel(Q,i) = shifti(gel(P,i), ni);
     329             :   }
     330       18161 :   return Q;
     331             : }
     332             : /* P(h*X) / h, assuming h | P(0), i.e. the result is a ZX */
     333             : GEN
     334        1176 : ZX_unscale_div(GEN P, GEN h)
     335             : {
     336        1176 :   long i, l = lg(P);
     337        1176 :   GEN hi, Q = cgetg(l, t_POL);
     338        1176 :   Q[1] = P[1];
     339        1176 :   if (l == 2) return Q;
     340        1176 :   gel(Q,2) = diviiexact(gel(P,2), h);
     341        1176 :   if (l == 3) return Q;
     342        1176 :   gel(Q,3) = gel(P,3);
     343        1176 :   if (l == 4) return Q;
     344        1176 :   hi = h;
     345        1176 :   gel(Q,4) = mulii(gel(P,4), hi);
     346        5194 :   for (i=5; i<l; i++)
     347             :   {
     348        4018 :     hi = mulii(hi,h);
     349        4018 :     gel(Q,i) = mulii(gel(P,i), hi);
     350             :   }
     351        1176 :   return Q;
     352             : }
     353             : 
     354             : GEN
     355         224 : RgXV_unscale(GEN v, GEN h)
     356             : {
     357             :   long i, l;
     358             :   GEN w;
     359         224 :   if (!h || isint1(h)) return v;
     360         168 :   w = cgetg_copy(v, &l);
     361         168 :   for (i=1; i<l; i++) gel(w,i) = RgX_unscale(gel(v,i), h);
     362         168 :   return w;
     363             : }
     364             : 
     365             : /* Return h^degpol(P) P(x / h), not memory clean */
     366             : GEN
     367        1456 : RgX_rescale(GEN P, GEN h)
     368             : {
     369        1456 :   long i, l = lg(P);
     370        1456 :   GEN Q = cgetg(l,t_POL), hi = h;
     371        1456 :   Q[l-1] = P[l-1];
     372        8001 :   for (i=l-2; i>=2; i--)
     373             :   {
     374        8001 :     gel(Q,i) = gmul(gel(P,i), hi);
     375        8001 :     if (i == 2) break;
     376        6545 :     hi = gmul(hi,h);
     377             :   }
     378        1456 :   Q[1] = P[1]; return Q;
     379             : }
     380             : 
     381             : /* A(X^d) --> A(X) */
     382             : GEN
     383      114711 : RgX_deflate(GEN x0, long d)
     384             : {
     385             :   GEN z, y, x;
     386      114711 :   long i,id, dy, dx = degpol(x0);
     387      114711 :   if (d == 1 || dx <= 0) return leafcopy(x0);
     388       65750 :   dy = dx/d;
     389       65750 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     390       65750 :   z = y + 2;
     391       65750 :   x = x0+ 2;
     392       65750 :   for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
     393       65750 :   return y;
     394             : }
     395             : 
     396             : /* return x0(X^d) */
     397             : GEN
     398      244982 : RgX_inflate(GEN x0, long d)
     399             : {
     400      244982 :   long i, id, dy, dx = degpol(x0);
     401      244982 :   GEN x = x0 + 2, z, y;
     402      244982 :   if (dx <= 0) return leafcopy(x0);
     403      242462 :   dy = dx*d;
     404      242462 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     405      242463 :   z = y + 2;
     406      242463 :   for (i=0; i<=dy; i++) gel(z,i) = gen_0;
     407      242463 :   for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
     408      242463 :   return y;
     409             : }
     410             : 
     411             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     412             : GEN
     413     1031328 : RgX_translate(GEN P, GEN c)
     414             : {
     415     1031328 :   pari_sp av = avma;
     416             :   GEN Q, *R;
     417             :   long i, k, n;
     418             : 
     419     1031328 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     420     1026606 :   Q = leafcopy(P);
     421     1026606 :   R = (GEN*)(Q+2); n = degpol(P);
     422     1026606 :   if (gequal1(c))
     423             :   {
     424        2072 :     for (i=1; i<=n; i++)
     425             :     {
     426        1799 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], R[k+1]);
     427        1799 :       if (gc_needed(av,2))
     428             :       {
     429           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL(1), i = %ld/%ld", i,n);
     430           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     431             :       }
     432             :     }
     433             :   }
     434     1026333 :   else if (gequalm1(c))
     435             :   {
     436      133959 :     for (i=1; i<=n; i++)
     437             :     {
     438      114660 :       for (k=n-i; k<n; k++) R[k] = gsub(R[k], R[k+1]);
     439      114660 :       if (gc_needed(av,2))
     440             :       {
     441           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL(-1), i = %ld/%ld", i,n);
     442           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     443             :       }
     444             :     }
     445             :   }
     446             :   else
     447             :   {
     448     3483480 :     for (i=1; i<=n; i++)
     449             :     {
     450     2476446 :       for (k=n-i; k<n; k++) R[k] = gadd(R[k], gmul(c, R[k+1]));
     451     2476446 :       if (gc_needed(av,2))
     452             :       {
     453           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"TR_POL, i = %ld/%ld", i,n);
     454           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     455             :       }
     456             :     }
     457             :   }
     458     1026606 :   return gerepilecopy(av, Q);
     459             : }
     460             : 
     461             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     462             : GEN
     463      376196 : ZX_translate(GEN P, GEN c)
     464             : {
     465      376196 :   pari_sp av = avma;
     466             :   GEN Q, *R;
     467             :   long i, k, n;
     468             : 
     469      376196 :   if (!signe(P) || !signe(c)) return ZX_copy(P);
     470      376161 :   Q = leafcopy(P);
     471      376161 :   R = (GEN*)(Q+2); n = degpol(P);
     472      376161 :   if (equali1(c))
     473             :   {
     474     2356461 :     for (i=1; i<=n; i++)
     475             :     {
     476     2083468 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], R[k+1]);
     477     2083468 :       if (gc_needed(av,2))
     478             :       {
     479           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"ZX_translate(1), i = %ld/%ld", i,n);
     480           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     481             :       }
     482             :     }
     483             :   }
     484      103168 :   else if (equalim1(c))
     485             :   {
     486          70 :     for (i=1; i<=n; i++)
     487             :     {
     488          49 :       for (k=n-i; k<n; k++) R[k] = subii(R[k], R[k+1]);
     489          49 :       if (gc_needed(av,2))
     490             :       {
     491           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"ZX_translate(-1), i = %ld/%ld", i,n);
     492           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     493             :       }
     494             :     }
     495             :   }
     496             :   else
     497             :   {
     498      768881 :     for (i=1; i<=n; i++)
     499             :     {
     500      665734 :       for (k=n-i; k<n; k++) R[k] = addii(R[k], mulii(c, R[k+1]));
     501      665734 :       if (gc_needed(av,2))
     502             :       {
     503           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"ZX_translate, i = %ld/%ld", i,n);
     504           0 :         Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     505             :       }
     506             :     }
     507             :   }
     508      376161 :   return gerepilecopy(av, Q);
     509             : }
     510             : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
     511             : GEN
     512        5957 : RgXQX_translate(GEN P, GEN c, GEN T)
     513             : {
     514        5957 :   pari_sp av = avma;
     515             :   GEN Q, *R;
     516             :   long i, k, n;
     517             : 
     518        5957 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     519        5957 :   Q = leafcopy(P);
     520        5957 :   R = (GEN*)(Q+2); n = degpol(P);
     521       34629 :   for (i=1; i<=n; i++)
     522             :   {
     523      141610 :     for (k=n-i; k<n; k++)
     524             :     {
     525      112938 :       pari_sp av2 = avma;
     526      112938 :       R[k] = gerepileupto(av2, RgX_rem(gadd(R[k], gmul(c, R[k+1])), T));
     527             :     }
     528       28672 :     if (gc_needed(av,2))
     529             :     {
     530           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXQX_translate, i = %ld/%ld", i,n);
     531           0 :       Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
     532             :     }
     533             :   }
     534        5957 :   return gerepilecopy(av, Q);
     535             : }
     536             : 
     537             : /********************************************************************/
     538             : /**                                                                **/
     539             : /**                          CONVERSIONS                           **/
     540             : /**                       (not memory clean)                       **/
     541             : /**                                                                **/
     542             : /********************************************************************/
     543             : /* to INT / FRAC / (POLMOD mod T), not memory clean because T not copied,
     544             :  * but everything else is */
     545             : static GEN
     546       18099 : QXQ_to_mod_copy(GEN x, GEN T)
     547             : {
     548             :   long d;
     549       18099 :   switch(typ(x))
     550             :   {
     551        7217 :     case t_INT:  return icopy(x);
     552         791 :     case t_FRAC: return gcopy(x);
     553             :     case t_POL:
     554       10091 :       d = degpol(x);
     555       10091 :       if (d < 0) return gen_0;
     556        9769 :       if (d == 0) return gcopy(gel(x,2));
     557        9594 :       return mkpolmod(RgX_copy(x), T);
     558           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     559             :              return NULL;/* LCOV_EXCL_LINE */
     560             :   }
     561             : }
     562             : /* pure shallow version */
     563             : static GEN
     564      409868 : QXQ_to_mod(GEN x, GEN T)
     565             : {
     566             :   long d;
     567      409868 :   switch(typ(x))
     568             :   {
     569             :     case t_INT:
     570      356545 :     case t_FRAC: return x;
     571             :     case t_POL:
     572       53323 :       d = degpol(x);
     573       53323 :       if (d < 0) return gen_0;
     574       49028 :       if (d == 0) return gel(x,2);
     575       45123 :       return mkpolmod(x, T);
     576           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     577             :              return NULL;/* LCOV_EXCL_LINE */
     578             :   }
     579             : }
     580             : /* T a ZX, z lifted from (Q[Y]/(T(Y)))[X], apply QXQ_to_mod_copy to all coeffs.
     581             :  * Not memory clean because T not copied, but everything else is */
     582             : static GEN
     583        1932 : QXQX_to_mod(GEN z, GEN T)
     584             : {
     585        1932 :   long i,l = lg(z);
     586        1932 :   GEN x = cgetg(l,t_POL);
     587        1932 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_copy(gel(z,i), T);
     588        1932 :   x[1] = z[1]; return normalizepol_lg(x,l);
     589             : }
     590             : /* pure shallow version */
     591             : GEN
     592       83678 : QXQX_to_mod_shallow(GEN z, GEN T)
     593             : {
     594       83678 :   long i,l = lg(z);
     595       83678 :   GEN x = cgetg(l,t_POL);
     596       83678 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
     597       83678 :   x[1] = z[1]; return normalizepol_lg(x,l);
     598             : }
     599             : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
     600             : GEN
     601         602 : QXQXV_to_mod(GEN V, GEN T)
     602             : {
     603         602 :   long i, l = lg(V);
     604         602 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     605         602 :   for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
     606         602 :   return z;
     607             : }
     608             : /* Apply QXQ_to_mod_copy to all entries. Memory-clean ! */
     609             : GEN
     610        4726 : QXQV_to_mod(GEN V, GEN T)
     611             : {
     612        4726 :   long i, l = lg(V);
     613        4726 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     614        4726 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod_copy(gel(V,i), T);
     615        4726 :   return z;
     616             : }
     617             : 
     618             : GEN
     619      720375 : RgX_renormalize_lg(GEN x, long lx)
     620             : {
     621             :   long i;
     622     1981377 :   for (i = lx-1; i>1; i--)
     623     1866206 :     if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
     624      720375 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     625      720375 :   setlg(x, i+1); setsigne(x, i != 1); return x;
     626             : }
     627             : 
     628             : GEN
     629      487976 : RgV_to_RgX(GEN x, long v)
     630             : {
     631      487976 :   long i, k = lg(x);
     632             :   GEN p;
     633             : 
     634      487976 :   while (--k && gequal0(gel(x,k)));
     635      487975 :   if (!k) return pol_0(v);
     636      469467 :   i = k+2; p = cgetg(i,t_POL);
     637      469468 :   p[1] = evalsigne(1) | evalvarn(v);
     638      469468 :   x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
     639      469468 :   return p;
     640             : }
     641             : GEN
     642      149566 : RgV_to_RgX_reverse(GEN x, long v)
     643             : {
     644      149566 :   long j, k, l = lg(x);
     645             :   GEN p;
     646             : 
     647      149566 :   for (k = 1; k < l; k++)
     648      149566 :     if (!gequal0(gel(x,k))) break;
     649      149566 :   if (k == l) return pol_0(v);
     650      149566 :   k -= 1;
     651      149566 :   l -= k;
     652      149566 :   x += k;
     653      149566 :   p = cgetg(l+1,t_POL);
     654      149566 :   p[1] = evalsigne(1) | evalvarn(v);
     655      149566 :   for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
     656      149566 :   return p;
     657             : }
     658             : 
     659             : /* return the (N-dimensional) vector of coeffs of p */
     660             : GEN
     661     4083713 : RgX_to_RgC(GEN x, long N)
     662             : {
     663             :   long i, l;
     664             :   GEN z;
     665     4083713 :   l = lg(x)-1; x++;
     666     4083713 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     667     4083713 :   z = cgetg(N+1,t_COL);
     668     4083713 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     669     4083713 :   for (   ; i<=N; i++) gel(z,i) = gen_0;
     670     4083713 :   return z;
     671             : }
     672             : GEN
     673      680016 : Rg_to_RgC(GEN x, long N)
     674             : {
     675      680016 :   return (typ(x) == t_POL)? RgX_to_RgC(x,N): scalarcol_shallow(x, N);
     676             : }
     677             : 
     678             : /* vector of polynomials (in v) whose coeffs are given by the columns of x */
     679             : GEN
     680       47983 : RgM_to_RgXV(GEN x, long v)
     681             : {
     682       47983 :   long j, lx = lg(x);
     683       47983 :   GEN y = cgetg(lx, t_VEC);
     684       47983 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), v);
     685       47983 :   return y;
     686             : }
     687             : 
     688             : /* matrix whose entries are given by the coeffs of the polynomials in
     689             :  * vector v (considered as degree n-1 polynomials) */
     690             : GEN
     691      103540 : RgV_to_RgM(GEN v, long n)
     692             : {
     693      103540 :   long j, N = lg(v);
     694      103540 :   GEN y = cgetg(N, t_MAT);
     695      103540 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j), n);
     696      103540 :   return y;
     697             : }
     698             : GEN
     699        2317 : RgXV_to_RgM(GEN v, long n)
     700             : {
     701        2317 :   long j, N = lg(v);
     702        2317 :   GEN y = cgetg(N, t_MAT);
     703        2317 :   for (j=1; j<N; j++) gel(y,j) = RgX_to_RgC(gel(v,j), n);
     704        2317 :   return y;
     705             : }
     706             : 
     707             : /* polynomial (in v) of polynomials (in w) whose coeffs are given by the columns of x */
     708             : GEN
     709       15211 : RgM_to_RgXX(GEN x, long v,long w)
     710             : {
     711       15211 :   long j, lx = lg(x);
     712       15211 :   GEN y = cgetg(lx+1, t_POL);
     713       15211 :   y[1] = evalsigne(1) | evalvarn(v);
     714       15211 :   y++;
     715       15211 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
     716       15211 :   return normalizepol_lg(--y, lx+1);
     717             : }
     718             : 
     719             : /* matrix whose entries are given by the coeffs of the polynomial v in
     720             :  * two variables (considered as degree n-1 polynomials) */
     721             : GEN
     722          21 : RgXX_to_RgM(GEN v, long n)
     723             : {
     724          21 :   long j, N = lg(v)-1;
     725          21 :   GEN y = cgetg(N, t_MAT);
     726          21 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j+1), n);
     727          21 :   return y;
     728             : }
     729             : 
     730             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     731             : GEN
     732       11554 : RgXY_swapspec(GEN x, long n, long w, long nx)
     733             : {
     734       11554 :   long j, ly = n+3;
     735       11554 :   GEN y = cgetg(ly, t_POL);
     736       11554 :   y[1] = evalsigne(1);
     737      183010 :   for (j=2; j<ly; j++)
     738             :   {
     739             :     long k;
     740      171456 :     GEN a = cgetg(nx+2,t_POL);
     741      171456 :     a[1] = evalsigne(1) | evalvarn(w);
     742      931798 :     for (k=0; k<nx; k++)
     743             :     {
     744      760342 :       GEN xk = gel(x,k);
     745      760342 :       if (typ(xk)==t_POL)
     746      674767 :         gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
     747             :       else
     748       85575 :         gel(a,k+2) = j==2 ? xk: gen_0;
     749             :     }
     750      171456 :     gel(y,j) = normalizepol_lg(a, nx+2);
     751             :   }
     752       11554 :   return normalizepol_lg(y,ly);
     753             : }
     754             : 
     755             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     756             : GEN
     757         224 : RgXY_swap(GEN x, long n, long w)
     758             : {
     759         224 :   GEN z = RgXY_swapspec(x+2, n, w, lgpol(x));
     760         224 :   setvarn(z, varn(x)); return z;
     761             : }
     762             : 
     763             : long
     764          92 : RgXY_degreex(GEN b)
     765             : {
     766          92 :   long deg = -1, i;
     767          92 :   if (!signe(b)) return -1;
     768         584 :   for (i = 2; i < lg(b); ++i)
     769             :   {
     770         492 :     GEN bi = gel(b, i);
     771         492 :     if (typ(bi) == t_POL)
     772         470 :       deg = maxss(deg, degpol(bi));
     773             :   }
     774          92 :   return deg;
     775             : }
     776             : 
     777             : /* return (x % X^n). Shallow */
     778             : GEN
     779       81055 : RgXn_red_shallow(GEN a, long n)
     780             : {
     781       81055 :   long i, L = n+2, l = lg(a);
     782             :   GEN  b;
     783       81055 :   if (L >= l) return a; /* deg(x) < n */
     784       45040 :   b = cgetg(L, t_POL); b[1] = a[1];
     785       45040 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
     786       45040 :   return normalizepol_lg(b,L);
     787             : }
     788             : 
     789             : GEN
     790         336 : RgXnV_red_shallow(GEN P, long n)
     791             : {
     792         336 :   long i, l = lg(P);
     793         336 :   GEN Q = cgetg(l, t_VEC);
     794         336 :   for (i=1; i<l; i++) gel(Q,i) = RgXn_red_shallow(gel(P,i), n);
     795         336 :   return Q;
     796             : }
     797             : 
     798             : /* return (x * X^n). Shallow */
     799             : GEN
     800    57696746 : RgX_shift_shallow(GEN a, long n)
     801             : {
     802    57696746 :   long i, l = lg(a);
     803             :   GEN  b;
     804    57696746 :   if (l == 2 || !n) return a;
     805    42540442 :   l += n;
     806    42540442 :   if (n < 0)
     807             :   {
     808    37589108 :     if (l <= 2) return pol_0(varn(a));
     809    37570593 :     b = cgetg(l, t_POL); b[1] = a[1];
     810    37570593 :     a -= n;
     811    37570593 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     812             :   } else {
     813     4951334 :     b = cgetg(l, t_POL); b[1] = a[1];
     814     4951334 :     a -= n; n += 2;
     815     4951334 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     816     4951334 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     817             :   }
     818    42521927 :   return b;
     819             : }
     820             : /* return (x * X^n). */
     821             : GEN
     822     3402753 : RgX_shift(GEN a, long n)
     823             : {
     824     3402753 :   long i, l = lg(a);
     825             :   GEN  b;
     826     3402753 :   if (l == 2 || !n) return RgX_copy(a);
     827     3402522 :   l += n;
     828     3402522 :   if (n < 0)
     829             :   {
     830         595 :     if (l <= 2) return pol_0(varn(a));
     831         553 :     b = cgetg(l, t_POL); b[1] = a[1];
     832         553 :     a -= n;
     833         553 :     for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     834             :   } else {
     835     3401927 :     b = cgetg(l, t_POL); b[1] = a[1];
     836     3401927 :     a -= n; n += 2;
     837     3401927 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     838     3401927 :     for (   ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     839             :   }
     840     3402480 :   return b;
     841             : }
     842             : 
     843             : GEN
     844      315273 : RgX_rotate_shallow(GEN P, long k, long p)
     845             : {
     846      315273 :   long i, l = lgpol(P);
     847             :   GEN r;
     848      315273 :   if (signe(P)==0)
     849         427 :     return pol_0(varn(P));
     850      314846 :   r = cgetg(p+2,t_POL); r[1] = P[1];
     851     2091390 :   for(i=0; i<p; i++)
     852             :   {
     853     1776544 :     long s = 2+(i+k)%p;
     854     1776544 :     gel(r,s) = i<l? gel(P,2+i): gen_0;
     855             :   }
     856      314846 :   return RgX_renormalize(r);
     857             : }
     858             : 
     859             : GEN
     860     2859209 : RgX_mulXn(GEN x, long d)
     861             : {
     862             :   pari_sp av;
     863             :   GEN z;
     864             :   long v;
     865     2859209 :   if (d >= 0) return RgX_shift(x, d);
     866     1366780 :   d = -d;
     867     1366780 :   v = RgX_val(x);
     868     1366780 :   if (v >= d) return RgX_shift(x, -d);
     869     1366773 :   av = avma;
     870     1366773 :   z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
     871     1366773 :   return gerepileupto(av, z);
     872             : }
     873             : 
     874             : long
     875     2095188 : RgX_val(GEN x)
     876             : {
     877     2095188 :   long i, lx = lg(x);
     878     2095188 :   if (lx == 2) return LONG_MAX;
     879     2120766 :   for (i = 2; i < lx; i++)
     880     2120752 :     if (!isexactzero(gel(x,i))) break;
     881     2095146 :   if (i == lx) return LONG_MAX;/* possible with non-rational zeros */
     882     2095132 :   return i - 2;
     883             : }
     884             : long
     885    42473065 : RgX_valrem(GEN x, GEN *Z)
     886             : {
     887    42473065 :   long v, i, lx = lg(x);
     888    42473065 :   if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
     889    81803691 :   for (i = 2; i < lx; i++)
     890    81803691 :     if (!isexactzero(gel(x,i))) break;
     891             :   /* possible with non-rational zeros */
     892    42473065 :   if (i == lx) { *Z = pol_0(varn(x)); return LONG_MAX; }
     893    42473065 :   v = i - 2;
     894    42473065 :   *Z = RgX_shift_shallow(x, -v);
     895    42473065 :   return v;
     896             : }
     897             : long
     898        9550 : RgX_valrem_inexact(GEN x, GEN *Z)
     899             : {
     900             :   long v;
     901        9550 :   if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
     902        9844 :   for (v = 0;; v++)
     903        9844 :     if (!gequal0(gel(x,2+v))) break;
     904         301 :   if (Z) *Z = RgX_shift_shallow(x, -v);
     905        9543 :   return v;
     906             : }
     907             : 
     908             : GEN
     909           0 : RgXQC_red(GEN P, GEN T)
     910             : {
     911           0 :   long i, l = lg(P);
     912           0 :   GEN Q = cgetg(l, t_COL);
     913           0 :   for (i=1; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     914           0 :   return Q;
     915             : }
     916             : 
     917             : GEN
     918          56 : RgXQV_red(GEN P, GEN T)
     919             : {
     920          56 :   long i, l = lg(P);
     921          56 :   GEN Q = cgetg(l, t_VEC);
     922          56 :   for (i=1; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     923          56 :   return Q;
     924             : }
     925             : 
     926             : GEN
     927           0 : RgXQM_red(GEN P, GEN T)
     928             : {
     929           0 :   long i, l = lg(P);
     930           0 :   GEN Q = cgetg(l, t_MAT);
     931           0 :   for (i=1; i<l; i++) gel(Q,i) = RgXQC_red(gel(P,i), T);
     932           0 :   return Q;
     933             : }
     934             : 
     935             : GEN
     936           0 : RgXQM_mul(GEN P, GEN Q, GEN T)
     937             : {
     938           0 :   return RgXQM_red(RgM_mul(P, Q), T);
     939             : }
     940             : 
     941             : GEN
     942        4662 : RgXQX_red(GEN P, GEN T)
     943             : {
     944        4662 :   long i, l = lg(P);
     945        4662 :   GEN Q = cgetg(l, t_POL);
     946        4662 :   Q[1] = P[1];
     947        4662 :   for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
     948        4662 :   return normalizepol_lg(Q, l);
     949             : }
     950             : 
     951             : GEN
     952      221745 : RgX_deriv(GEN x)
     953             : {
     954      221745 :   long i,lx = lg(x)-1;
     955             :   GEN y;
     956             : 
     957      221745 :   if (lx<3) return pol_0(varn(x));
     958      194613 :   y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
     959      194613 :   for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
     960      194613 :   y[1] = x[1]; return normalizepol_lg(y,i);
     961             : }
     962             : 
     963             : GEN
     964      295814 : RgX_recipspec_shallow(GEN x, long l, long n)
     965             : {
     966             :   long i;
     967      295814 :   GEN z=cgetg(n+2,t_POL)+2;
     968    14703634 :   for(i=0; i<l; i++)
     969    14407819 :     gel(z,n-i-1) = gel(x,i);
     970      383539 :   for(   ; i<n; i++)
     971       87724 :     gel(z, n-i-1) = gen_0;
     972      295815 :   return normalizepol_lg(z-2,n+2);
     973             : }
     974             : 
     975             : /* return coefficients s.t x = x_0 X^n + ... + x_n */
     976             : GEN
     977        1708 : RgX_recip(GEN x)
     978             : {
     979             :   long lx, i, j;
     980        1708 :   GEN y = cgetg_copy(x, &lx);
     981        1708 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
     982        1708 :   return normalizepol_lg(y,lx);
     983             : }
     984             : /* shallow version */
     985             : GEN
     986      423747 : RgX_recip_shallow(GEN x)
     987             : {
     988             :   long lx, i, j;
     989      423747 :   GEN y = cgetg_copy(x, &lx);
     990      423761 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
     991      423761 :   return y;
     992             : }
     993             : /*******************************************************************/
     994             : /*                                                                 */
     995             : /*                      ADDITION / SUBTRACTION                     */
     996             : /*                                                                 */
     997             : /*******************************************************************/
     998             : /* same variable */
     999             : GEN
    1000    27710977 : RgX_add(GEN x, GEN y)
    1001             : {
    1002    27710977 :   long i, lx = lg(x), ly = lg(y);
    1003             :   GEN z;
    1004    27710977 :   if (ly <= lx) {
    1005    25824640 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1006    25824640 :     for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1007    25824640 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1008    25824640 :     z = normalizepol_lg(z, lx);
    1009             :   } else {
    1010     1886337 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1011     1886337 :     for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1012     1886337 :     for (   ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
    1013     1886337 :     z = normalizepol_lg(z, ly);
    1014             :   }
    1015    27710977 :   return z;
    1016             : }
    1017             : GEN
    1018    12006187 : RgX_sub(GEN x, GEN y)
    1019             : {
    1020    12006187 :   long i, lx = lg(x), ly = lg(y);
    1021             :   GEN z;
    1022    12006187 :   if (ly <= lx) {
    1023     9572811 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1024     9572811 :     for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1025     9572811 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1026     9572811 :     z = normalizepol_lg(z, lx);
    1027             :   } else {
    1028     2433376 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1029     2433376 :     for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1030     2433376 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    1031     2433376 :     z = normalizepol_lg(z, ly);
    1032             :   }
    1033    12006187 :   return z;
    1034             : }
    1035             : GEN
    1036     1024116 : RgX_neg(GEN x)
    1037             : {
    1038     1024116 :   long i, lx = lg(x);
    1039     1024116 :   GEN y = cgetg(lx, t_POL); y[1] = x[1];
    1040     1024116 :   for (i=2; i<lx; i++) gel(y,i) = gneg(gel(x,i));
    1041     1024116 :   return y;
    1042             : }
    1043             : 
    1044             : GEN
    1045    11760847 : RgX_Rg_add(GEN y, GEN x)
    1046             : {
    1047             :   GEN z;
    1048    11760847 :   long lz = lg(y), i;
    1049    11760847 :   if (lz == 2) return scalarpol(x,varn(y));
    1050    10047127 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1051    10047127 :   gel(z,2) = gadd(gel(y,2),x);
    1052    10047127 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1053             :   /* probably useless unless lz = 3, but cannot be skipped if y is
    1054             :    * an inexact 0 */
    1055    10047127 :   return normalizepol_lg(z,lz);
    1056             : }
    1057             : GEN
    1058        2422 : RgX_Rg_add_shallow(GEN y, GEN x)
    1059             : {
    1060             :   GEN z;
    1061        2422 :   long lz = lg(y), i;
    1062        2422 :   if (lz == 2) return scalarpol(x,varn(y));
    1063        2422 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1064        2422 :   gel(z,2) = gadd(gel(y,2),x);
    1065        2422 :   for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
    1066        2422 :   return z = normalizepol_lg(z,lz);
    1067             : }
    1068             : GEN
    1069       32103 : RgX_Rg_sub(GEN y, GEN x)
    1070             : {
    1071             :   GEN z;
    1072       32103 :   long lz = lg(y), i;
    1073       32103 :   if (lz == 2)
    1074             :   { /* scalarpol(gneg(x),varn(y)) optimized */
    1075        3864 :     long v = varn(y);
    1076        3864 :     if (isrationalzero(x)) return pol_0(v);
    1077          14 :     z = cgetg(3,t_POL);
    1078          28 :     z[1] = gequal0(x)? evalvarn(v)
    1079          14 :                    : evalvarn(v) | evalsigne(1);
    1080          14 :     gel(z,2) = gneg(x); return z;
    1081             :   }
    1082       28239 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1083       28239 :   gel(z,2) = gsub(gel(y,2),x);
    1084       28239 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1085       28239 :   return z = normalizepol_lg(z,lz);
    1086             : }
    1087             : GEN
    1088      402248 : Rg_RgX_sub(GEN x, GEN y)
    1089             : {
    1090             :   GEN z;
    1091      402248 :   long lz = lg(y), i;
    1092      402248 :   if (lz == 2) return scalarpol(x,varn(y));
    1093      401233 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1094      401233 :   gel(z,2) = gsub(x, gel(y,2));
    1095      401233 :   for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
    1096      401233 :   return z = normalizepol_lg(z,lz);
    1097             : }
    1098             : /*******************************************************************/
    1099             : /*                                                                 */
    1100             : /*                  KARATSUBA MULTIPLICATION                       */
    1101             : /*                                                                 */
    1102             : /*******************************************************************/
    1103             : #if 0
    1104             : /* to debug Karatsuba-like routines */
    1105             : GEN
    1106             : zx_debug_spec(GEN x, long nx)
    1107             : {
    1108             :   GEN z = cgetg(nx+2,t_POL);
    1109             :   long i;
    1110             :   for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
    1111             :   z[1] = evalsigne(1); return z;
    1112             : }
    1113             : 
    1114             : GEN
    1115             : RgX_debug_spec(GEN x, long nx)
    1116             : {
    1117             :   GEN z = cgetg(nx+2,t_POL);
    1118             :   long i;
    1119             :   for (i=0; i<nx; i++) z[i+2] = x[i];
    1120             :   z[1] = evalsigne(1); return z;
    1121             : }
    1122             : #endif
    1123             : 
    1124             : /* generic multiplication */
    1125             : GEN
    1126     2642529 : RgX_addspec_shallow(GEN x, GEN y, long nx, long ny)
    1127             : {
    1128             :   GEN z, t;
    1129             :   long i;
    1130     2642529 :   if (nx == ny) {
    1131      560713 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1132      560713 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1133      560713 :     return normalizepol_lg(z, nx+2);
    1134             :   }
    1135     2081816 :   if (ny < nx) {
    1136     1940471 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1137     1940471 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1138     1940471 :     for (   ; i < nx; i++) gel(t,i) = gel(x,i);
    1139     1940471 :     return normalizepol_lg(z, nx+2);
    1140             :   } else {
    1141      141345 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1142      141345 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1143      141345 :     for (   ; i < ny; i++) gel(t,i) = gel(y,i);
    1144      141345 :     return normalizepol_lg(z, ny+2);
    1145             :   }
    1146             : }
    1147             : GEN
    1148      178986 : RgX_addspec(GEN x, GEN y, long nx, long ny)
    1149             : {
    1150             :   GEN z, t;
    1151             :   long i;
    1152      178986 :   if (nx == ny) {
    1153         601 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1154         601 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1155         601 :     return normalizepol_lg(z, nx+2);
    1156             :   }
    1157      178385 :   if (ny < nx) {
    1158      178385 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1159      178385 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1160      178385 :     for (   ; i < nx; i++) gel(t,i) = gcopy(gel(x,i));
    1161      178385 :     return normalizepol_lg(z, nx+2);
    1162             :   } else {
    1163           0 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1164           0 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1165           0 :     for (   ; i < ny; i++) gel(t,i) = gcopy(gel(y,i));
    1166           0 :     return normalizepol_lg(z, ny+2);
    1167             :   }
    1168             : }
    1169             : 
    1170             : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
    1171             :  * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
    1172             :  * t_VECSMALL, to hold this, which can be left on stack: gerepile
    1173             :  * will not crash on it. The returned vector itself is not a proper GEN,
    1174             :  * we access the coefficients as x[i], i = 0..deg(x) */
    1175             : static GEN
    1176    21609353 : RgXspec_kill0(GEN x, long lx)
    1177             : {
    1178    21609353 :   GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit gerepile-wise */
    1179             :   long i;
    1180    97686533 :   for (i=0; i <lx; i++)
    1181             :   {
    1182    76077180 :     GEN c = gel(x,i);
    1183    76077180 :     z[i] = (long)(isrationalzero(c)? NULL: c);
    1184             :   }
    1185    21609353 :   return z;
    1186             : }
    1187             : 
    1188             : INLINE GEN
    1189    53540425 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
    1190             : {
    1191    53540425 :   pari_sp av = avma;
    1192    53540425 :   GEN s = NULL;
    1193             :   long i;
    1194             : 
    1195   234327523 :   for (i=a; i<b; i++)
    1196   180787098 :     if (gel(y,i) && gel(x,-i))
    1197             :     {
    1198   132202838 :       GEN t = gmul(gel(y,i), gel(x,-i));
    1199   132202838 :       s = s? gadd(s, t): t;
    1200             :     }
    1201    53540425 :   return s? gerepileupto(av, s): gen_0;
    1202             : }
    1203             : 
    1204             : /* assume nx >= ny > 0, return x * y * t^v */
    1205             : static GEN
    1206     7694002 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
    1207             : {
    1208             :   long i, lz, nz;
    1209             :   GEN z;
    1210             : 
    1211     7694002 :   x = RgXspec_kill0(x,nx);
    1212     7694002 :   y = RgXspec_kill0(y,ny);
    1213     7694002 :   lz = nx + ny + 1; nz = lz-2;
    1214     7694002 :   lz += v;
    1215     7694002 :   z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
    1216     7694002 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1217     7694002 :   for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
    1218     7694002 :   for (  ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
    1219     7694002 :   for (  ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
    1220     7694002 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1221             : }
    1222             : 
    1223             : /* return (x * X^d) + y. Assume d > 0 */
    1224             : GEN
    1225     1754698 : RgX_addmulXn_shallow(GEN x0, GEN y0, long d)
    1226             : {
    1227             :   GEN x, y, xd, yd, zd;
    1228             :   long a, lz, nx, ny;
    1229             : 
    1230     1754698 :   if (!signe(x0)) return y0;
    1231     1734816 :   ny = lgpol(y0);
    1232     1734816 :   nx = lgpol(x0);
    1233     1734816 :   zd = (GEN)avma;
    1234     1734816 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1235     1734816 :   if (a <= 0)
    1236             :   {
    1237      163552 :     lz = nx+d+2;
    1238      163552 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1239      163552 :     while (xd > x) gel(--zd,0) = gel(--xd,0);
    1240      163552 :     x = zd + a;
    1241      163552 :     while (zd > x) gel(--zd,0) = gen_0;
    1242             :   }
    1243             :   else
    1244             :   {
    1245     1571264 :     xd = new_chunk(d); yd = y+d;
    1246     1571264 :     x = RgX_addspec_shallow(x,yd, nx,a);
    1247     1571264 :     lz = (a>nx)? ny+2: lg(x)+d;
    1248     1571264 :     x += 2; while (xd > x) *--zd = *--xd;
    1249             :   }
    1250     1734816 :   while (yd > y) *--zd = *--yd;
    1251     1734816 :   *--zd = x0[1];
    1252     1734816 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1253             : }
    1254             : GEN
    1255      469910 : RgX_addmulXn(GEN x0, GEN y0, long d)
    1256             : {
    1257             :   GEN x, y, xd, yd, zd;
    1258             :   long a, lz, nx, ny;
    1259             : 
    1260      469910 :   if (!signe(x0)) return RgX_copy(y0);
    1261      469634 :   nx = lgpol(x0);
    1262      469634 :   ny = lgpol(y0);
    1263      469634 :   zd = (GEN)avma;
    1264      469634 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1265      469634 :   if (a <= 0)
    1266             :   {
    1267      290648 :     lz = nx+d+2;
    1268      290648 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1269      290648 :     while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
    1270      290648 :     x = zd + a;
    1271      290648 :     while (zd > x) gel(--zd,0) = gen_0;
    1272             :   }
    1273             :   else
    1274             :   {
    1275      178986 :     xd = new_chunk(d); yd = y+d;
    1276      178986 :     x = RgX_addspec(x,yd, nx,a);
    1277      178986 :     lz = (a>nx)? ny+2: lg(x)+d;
    1278      178986 :     x += 2; while (xd > x) *--zd = *--xd;
    1279             :   }
    1280      469634 :   while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
    1281      469634 :   *--zd = x0[1];
    1282      469634 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1283             : }
    1284             : 
    1285             : /* return x * y mod t^n */
    1286             : static GEN
    1287     3106108 : RgXn_mul_basecase(GEN x, GEN y, long n)
    1288             : {
    1289     3106108 :   long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
    1290             :   GEN z;
    1291     3106108 :   if (lx < 0) return pol_0(varn(x));
    1292     3106108 :   if (ly < 0) return pol_0(varn(x));
    1293     3106108 :   z = cgetg(lz, t_POL) + 2;
    1294     3106108 :   x+=2; if (lx > n) lx = n;
    1295     3106108 :   y+=2; if (ly > n) ly = n;
    1296     3106108 :   z[-1] = x[-1];
    1297     3106108 :   if (ly > lx) { swap(x,y); lswap(lx,ly); }
    1298     3106108 :   x = RgXspec_kill0(x, lx);
    1299     3106108 :   y = RgXspec_kill0(y, ly);
    1300             :   /* x:y:z [i] = term of degree i */
    1301     3106108 :   for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
    1302     3106108 :   for (  ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
    1303     3106108 :   for (  ; i<n; i++)  gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
    1304     3106108 :   return normalizepol_lg(z - 2, lz);
    1305             : }
    1306             : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
    1307             : GEN
    1308     3668124 : RgXn_mul(GEN f, GEN g, long n)
    1309             : {
    1310     3668124 :   pari_sp av = avma;
    1311             :   GEN fe,fo, ge,go, l,h,m;
    1312             :   long n0, n1;
    1313     3668124 :   if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
    1314     3108460 :   if (n < 80) return RgXn_mul_basecase(f,g,n);
    1315        2352 :   n0 = n>>1; n1 = n-n0;
    1316        2352 :   RgX_even_odd(f, &fe, &fo);
    1317        2352 :   RgX_even_odd(g, &ge, &go);
    1318        2352 :   l = RgXn_mul(fe,ge,n1);
    1319        2352 :   h = RgXn_mul(fo,go,n0);
    1320        2352 :   m = RgX_sub(RgXn_mul(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
    1321             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1322             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1323        2352 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1324             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1325        2352 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1326        2352 :   m = RgX_inflate(m,2);
    1327             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1328        2352 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1329        2352 :   h = RgX_inflate(h,2);
    1330        2352 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1331        2352 :   return gerepileupto(av, h);
    1332             : }
    1333             : /* (f*g) \/ x^n */
    1334             : GEN
    1335       33816 : RgX_mulhigh_i(GEN f, GEN g, long n)
    1336             : {
    1337       33816 :   long d = degpol(f)+degpol(g) + 1 - n;
    1338             :   GEN h;
    1339       33816 :   if (d <= 2) return RgX_shift_shallow(RgX_mul(f,g), -n);
    1340        1816 :   h = RgX_recip_shallow(RgXn_mul(RgX_recip_shallow(f),
    1341             :                                  RgX_recip_shallow(g), d));
    1342        1816 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1343             : }
    1344             : 
    1345             : /* (f*g) \/ x^n */
    1346             : GEN
    1347           0 : RgX_sqrhigh_i(GEN f, long n)
    1348             : {
    1349           0 :   long d = 2*degpol(f)+ 1 - n;
    1350             :   GEN h;
    1351           0 :   if (d <= 2) return RgX_shift_shallow(RgX_sqr(f), -n);
    1352           0 :   h = RgX_recip_shallow(RgXn_sqr(RgX_recip_shallow(f), d));
    1353           0 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1354             : }
    1355             : 
    1356             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
    1357             :  * b+2 were sent instead. na, nb = number of terms of a, b.
    1358             :  * Only c, c0, c1, c2 are genuine GEN.
    1359             :  */
    1360             : GEN
    1361     8225672 : RgX_mulspec(GEN a, GEN b, long na, long nb)
    1362             : {
    1363             :   GEN a0, c, c0;
    1364     8225672 :   long n0, n0a, i, v = 0;
    1365             :   pari_sp av;
    1366             : 
    1367     8225672 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
    1368     8225672 :   while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
    1369     8225672 :   if (na < nb) swapspec(a,b, na,nb);
    1370     8225672 :   if (!nb) return pol_0(0);
    1371             : 
    1372     8161107 :   if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
    1373      467105 :   RgX_shift_inplace_init(v);
    1374      467105 :   i = (na>>1); n0 = na-i; na = i;
    1375      467105 :   av = avma; a0 = a+n0; n0a = n0;
    1376      467105 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1377             : 
    1378      467105 :   if (nb > n0)
    1379             :   {
    1380             :     GEN b0,c1,c2;
    1381             :     long n0b;
    1382             : 
    1383      465237 :     nb -= n0; b0 = b+n0; n0b = n0;
    1384      465237 :     while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
    1385      465237 :     c = RgX_mulspec(a,b,n0a,n0b);
    1386      465237 :     c0 = RgX_mulspec(a0,b0, na,nb);
    1387             : 
    1388      465237 :     c2 = RgX_addspec_shallow(a0,a, na,n0a);
    1389      465237 :     c1 = RgX_addspec_shallow(b0,b, nb,n0b);
    1390             : 
    1391      465237 :     c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
    1392      465237 :     c2 = RgX_sub(c1, RgX_add(c0,c));
    1393      465237 :     c0 = RgX_addmulXn_shallow(c0, c2, n0);
    1394             :   }
    1395             :   else
    1396             :   {
    1397        1868 :     c = RgX_mulspec(a,b,n0a,nb);
    1398        1868 :     c0 = RgX_mulspec(a0,b,na,nb);
    1399             :   }
    1400      467105 :   c0 = RgX_addmulXn(c0,c,n0);
    1401      467105 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1402             : }
    1403             : 
    1404             : INLINE GEN
    1405      103931 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
    1406             : {
    1407      103931 :   pari_sp av = avma;
    1408      103931 :   GEN s = NULL;
    1409      103931 :   long j, l = (i+1)>>1;
    1410      503111 :   for (j=a; j<l; j++)
    1411             :   {
    1412      399180 :     GEN xj = gel(x,j), xx = gel(x,i-j);
    1413      399180 :     if (xj && xx)
    1414             :     {
    1415      386825 :       GEN t = gmul(xj, xx);
    1416      386825 :       s = s? gadd(s, t): t;
    1417             :     }
    1418             :   }
    1419      103931 :   if (s) s = gshift(s,1);
    1420      103931 :   if ((i&1) == 0)
    1421             :   {
    1422       56441 :     GEN t = gel(x, i>>1);
    1423       56441 :     if (t) {
    1424       53634 :       t = gsqr(t);
    1425       53634 :       s = s? gadd(s, t): t;
    1426             :     }
    1427             :   }
    1428      103931 :   return s? gerepileupto(av,s): gen_0;
    1429             : }
    1430             : static GEN
    1431        8447 : RgX_sqrspec_basecase(GEN x, long nx, long v)
    1432             : {
    1433             :   long i, lz, nz;
    1434             :   GEN z;
    1435             : 
    1436        8447 :   if (!nx) return pol_0(0);
    1437        8447 :   x = RgXspec_kill0(x,nx);
    1438        8447 :   lz = (nx << 1) + 1, nz = lz-2;
    1439        8447 :   lz += v;
    1440        8447 :   z = cgetg(lz,t_POL) + 2;
    1441        8447 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1442        8447 :   for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1443        8447 :   for (  ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
    1444        8447 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1445             : }
    1446             : /* return x^2 mod t^n */
    1447             : static GEN
    1448         686 : RgXn_sqr_basecase(GEN x, long n)
    1449             : {
    1450         686 :   long i, lz = n+2, lx = lgpol(x);
    1451             :   GEN z;
    1452         686 :   if (lx < 0) return pol_0(varn(x));
    1453         686 :   z = cgetg(lz, t_POL);
    1454         686 :   z[1] = x[1];
    1455         686 :   x+=2; if (lx > n) lx = n;
    1456         686 :   x = RgXspec_kill0(x,lx);
    1457         686 :   z+=2;/* x:z [i] = term of degree i */
    1458         686 :   for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1459         686 :   for (  ; i<n; i++)  gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
    1460         686 :   z -= 2; return normalizepol_lg(z, lz);
    1461             : }
    1462             : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
    1463             : GEN
    1464       15666 : RgXn_sqr(GEN f, long n)
    1465             : {
    1466       15666 :   pari_sp av = avma;
    1467             :   GEN fe,fo, l,h,m;
    1468             :   long n0, n1;
    1469       15666 :   if (2*degpol(f) < n) return RgX_sqr(f);
    1470         714 :   if (n < 80) return RgXn_sqr_basecase(f,n);
    1471          28 :   n0 = n>>1; n1 = n-n0;
    1472          28 :   RgX_even_odd(f, &fe, &fo);
    1473          28 :   l = RgXn_sqr(fe,n1);
    1474          28 :   h = RgXn_sqr(fo,n0);
    1475          28 :   m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
    1476             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1477             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1478          28 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1479             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1480          28 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1481          28 :   m = RgX_inflate(m,2);
    1482             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1483          28 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1484          28 :   h = RgX_inflate(h,2);
    1485          28 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1486          28 :   return gerepileupto(av, h);
    1487             : }
    1488             : 
    1489             : GEN
    1490        8872 : RgX_sqrspec(GEN a, long na)
    1491             : {
    1492             :   GEN a0, c, c0, c1;
    1493        8872 :   long n0, n0a, i, v = 0;
    1494             :   pari_sp av;
    1495             : 
    1496        8872 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
    1497        8872 :   if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
    1498         425 :   RgX_shift_inplace_init(v);
    1499         425 :   i = (na>>1); n0 = na-i; na = i;
    1500         425 :   av = avma; a0 = a+n0; n0a = n0;
    1501         425 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1502             : 
    1503         425 :   c = RgX_sqrspec(a,n0a);
    1504         425 :   c0 = RgX_sqrspec(a0,na);
    1505         425 :   c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
    1506         425 :   c0 = RgX_addmulXn_shallow(c0,c1, n0);
    1507         425 :   c0 = RgX_addmulXn(c0,c,n0);
    1508         425 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1509             : }
    1510             : 
    1511             : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
    1512             : GEN
    1513      416167 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
    1514             : {
    1515      416167 :   GEN z = RgX_mul(A, B);
    1516      416167 :   if (a < b)
    1517        5222 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(A, B, b-a), z, a);
    1518      410945 :   else if (a > b)
    1519      258334 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(B, A, a-b), z, b);
    1520             :   else
    1521      152611 :     z = RgX_addmulXn_shallow(RgX_add(A, B), z, a);
    1522      416167 :   return z;
    1523             : }
    1524             : 
    1525             : GEN
    1526     6825800 : RgX_mul_i(GEN x, GEN y)
    1527             : {
    1528     6825800 :   GEN z = RgX_mulspec(x+2, y+2, lgpol(x), lgpol(y));
    1529     6825800 :   setvarn(z, varn(x)); return z;
    1530             : }
    1531             : 
    1532             : static GEN
    1533       50905 : zero_FpX_mod(GEN p, long v)
    1534             : {
    1535       50905 :   GEN r = cgetg(3,t_POL);
    1536       50905 :   r[1] = evalvarn(v);
    1537       50905 :   gel(r,2) = mkintmod(gen_0, icopy(p));
    1538       50905 :   return r;
    1539             : }
    1540             : 
    1541             : static GEN
    1542      292536 : RgX_mul_FpX(GEN x, GEN y, GEN p)
    1543             : {
    1544      292536 :   pari_sp av = avma;
    1545      292536 :   GEN r = FpX_mul(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    1546      292536 :   if (signe(r)==0)
    1547       50807 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    1548      241729 :   return gerepileupto(av, FpX_to_mod(r, p));
    1549             : }
    1550             : 
    1551             : static GEN
    1552           0 : zero_FpXQX_mod(GEN pol, GEN p, long v)
    1553             : {
    1554           0 :   GEN r = cgetg(3,t_POL);
    1555           0 :   r[1] = evalvarn(v);
    1556           0 :   gel(r,2) = mkpolmod(mkintmod(gen_0, icopy(p)), gcopy(pol));
    1557           0 :   return r;
    1558             : }
    1559             : 
    1560             : static GEN
    1561          70 : RgX_mul_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    1562             : {
    1563          70 :   pari_sp av = avma;
    1564          70 :   GEN T = RgX_to_FpX(pol, p);
    1565          70 :   long dT = degpol(T);
    1566          70 :   GEN kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    1567          70 :   GEN ky = ZXX_to_Kronecker(RgX_to_FpXQX(y, T, p), dT);
    1568          70 :   GEN r = FpX_mul(kx, ky, p);
    1569          70 :   if (signe(r)==0)
    1570           0 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    1571          70 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    1572             : }
    1573             : 
    1574             : static GEN
    1575       88770 : RgX_mul_ZXQX(GEN x, GEN y, GEN T)
    1576             : {
    1577       88770 :   pari_sp av = avma;
    1578       88770 :   long dT = degpol(T);
    1579       88770 :   GEN r = ZXX_mul_Kronecker(liftpol_shallow(x), liftpol_shallow(y), dT);
    1580       88770 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    1581             : }
    1582             : 
    1583             : static GEN
    1584       46687 : RgX_mul_QXQX(GEN x, GEN y, GEN T)
    1585             : {
    1586       46687 :   pari_sp av = avma;
    1587       46687 :   long dT = degpol(T);
    1588       46687 :   GEN r = QX_mul(ZXX_to_Kronecker(liftpol_shallow(x), dT),
    1589             :                  ZXX_to_Kronecker(liftpol_shallow(y), dT));
    1590       46687 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    1591             : }
    1592             : 
    1593             : GEN
    1594        8022 : RgX_sqr_i(GEN x)
    1595             : {
    1596        8022 :   GEN z = RgX_sqrspec(x+2, lgpol(x));
    1597        8022 :   setvarn(z,varn(x)); return z;
    1598             : }
    1599             : 
    1600             : static GEN
    1601         637 : RgX_sqr_FpX(GEN x, GEN p)
    1602             : {
    1603         637 :   pari_sp av = avma;
    1604         637 :   GEN r = FpX_sqr(RgX_to_FpX(x, p), p);
    1605         637 :   if (signe(r)==0)
    1606          98 :   { avma = av; return zero_FpX_mod(p, varn(x)); }
    1607         539 :   return gerepileupto(av, FpX_to_mod(r, p));
    1608             : }
    1609             : 
    1610             : static GEN
    1611         189 : RgX_sqr_FpXQX(GEN x, GEN pol, GEN p)
    1612             : {
    1613         189 :   pari_sp av = avma;
    1614         189 :   GEN T = RgX_to_FpX(pol, p);
    1615         189 :   long dT = degpol(T);
    1616         189 :   GEN kx = ZXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    1617         189 :   GEN r = FpX_sqr(kx, p);
    1618         189 :   if (signe(r)==0)
    1619           0 :   { avma = av; return zero_FpXQX_mod(pol, p, varn(x)); }
    1620         189 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    1621             : }
    1622             : 
    1623             : static GEN
    1624        5083 : RgX_sqr_ZXQX(GEN x, GEN T)
    1625             : {
    1626        5083 :   pari_sp av = avma;
    1627        5083 :   long dT = degpol(T);
    1628        5083 :   GEN r = ZXX_sqr_Kronecker(liftpol_shallow(x), dT);
    1629        5083 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    1630             : }
    1631             : 
    1632             : static GEN
    1633        4286 : RgX_sqr_QXQX(GEN x, GEN T)
    1634             : {
    1635        4286 :   pari_sp av = avma;
    1636        4286 :   long dT = degpol(T);
    1637        4286 :   GEN r = QX_sqr(ZXX_to_Kronecker(liftpol_shallow(x), dT));
    1638        4286 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    1639             : }
    1640             : 
    1641             : #define code(t1,t2) ((t1 << 6) | t2)
    1642             : 
    1643             : GEN
    1644    38571528 : RgX_mul(GEN x, GEN y)
    1645             : {
    1646             :   GEN p, pol;
    1647             :   long pa;
    1648    38571528 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    1649    38571493 :   switch(t)
    1650             :   {
    1651    30722238 :     case t_INT:    return ZX_mul(x,y);
    1652      496166 :     case t_FRAC:   return QX_mul(x,y);
    1653       99226 :     case t_FFELT:  return FFX_mul(x, y, pol);
    1654      292536 :     case t_INTMOD: return RgX_mul_FpX(x, y, p);
    1655             :     case code(t_POLMOD, t_INT):
    1656       88770 :                    return RgX_mul_ZXQX(x, y, pol);
    1657             :     case code(t_POLMOD, t_FRAC):
    1658       46687 :                    return RgX_mul_QXQX(x, y, pol);
    1659             :     case code(t_POLMOD, t_INTMOD):
    1660          70 :                    return RgX_mul_FpXQX(x, y, pol, p);
    1661     6825800 :     default:       return RgX_mul_i(x,y);
    1662             :   }
    1663             : }
    1664             : 
    1665             : GEN
    1666     2830605 : RgX_sqr(GEN x)
    1667             : {
    1668             :   GEN p, pol;
    1669             :   long pa;
    1670     2830605 :   long t = RgX_type(x,&p,&pol,&pa);
    1671     2830623 :   switch(t)
    1672             :   {
    1673     2772226 :     case t_INT:    return ZX_sqr(x);
    1674       38199 :     case t_FRAC:   return QX_sqr(x);
    1675        1981 :     case t_FFELT:  return FFX_sqr(x, pol);
    1676         637 :     case t_INTMOD: return RgX_sqr_FpX(x, p);
    1677             :     case code(t_POLMOD, t_INT):
    1678        5083 :                    return RgX_sqr_ZXQX(x, pol);
    1679             :     case code(t_POLMOD, t_FRAC):
    1680        4286 :                    return RgX_sqr_QXQX(x, pol);
    1681             :     case code(t_POLMOD, t_INTMOD):
    1682         189 :                    return RgX_sqr_FpXQX(x, pol, p);
    1683        8022 :     default:       return RgX_sqr_i(x);
    1684             :   }
    1685             : }
    1686             : 
    1687             : #undef code
    1688             : 
    1689             : /*******************************************************************/
    1690             : /*                                                                 */
    1691             : /*                               DIVISION                          */
    1692             : /*                                                                 */
    1693             : /*******************************************************************/
    1694             : GEN
    1695      529762 : RgX_Rg_divexact(GEN x, GEN y) {
    1696             :   long i, lx;
    1697             :   GEN z;
    1698      529762 :   if (typ(y) == t_INT && is_pm1(y))
    1699      100761 :     return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1700      429001 :   z = cgetg_copy(x, &lx); z[1] = x[1];
    1701      429001 :   for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    1702      429001 :   return z;
    1703             : }
    1704             : GEN
    1705    22582725 : RgX_Rg_div(GEN x, GEN y) {
    1706             :   long i, lx;
    1707    22582725 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1708    22582725 :   for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
    1709    22582725 :   return normalizepol_lg(z, lx);
    1710             : }
    1711             : GEN
    1712        1974 : RgX_normalize(GEN x)
    1713             : {
    1714        1974 :   GEN d = NULL;
    1715        1974 :   long i, n = lg(x)-1;
    1716        1974 :   for (i = n; i > 1; i--)
    1717             :   {
    1718        1974 :     d = gel(x,i);
    1719        1974 :     if (!gequal0(d)) break;
    1720             :   }
    1721        1974 :   if (i == 1) return pol_0(varn(x));
    1722        1974 :   if (i == n && isint1(d)) return x;
    1723         784 :   return normalizepol_lg(RgX_Rg_div(x, d), i+1);
    1724             : }
    1725             : GEN
    1726        2457 : RgX_divs(GEN x, long y) {
    1727             :   long i, lx;
    1728        2457 :   GEN z = cgetg_copy(x, &lx); z[1] = x[1];
    1729        2457 :   for (i=2; i<lx; i++) gel(z,i) = gdivgs(gel(x,i),y);
    1730        2457 :   return normalizepol_lg(z, lx);
    1731             : }
    1732             : GEN
    1733       39147 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
    1734             : {
    1735       39147 :   long l = lg(a), i;
    1736       39147 :   GEN a0, z0, z = cgetg(l-1, t_POL);
    1737       39147 :   z[1] = a[1];
    1738       39147 :   a0 = a + l-1;
    1739       39147 :   z0 = z + l-2; *z0 = *a0--;
    1740      779346 :   for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1741             :   {
    1742      740199 :     GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
    1743      740199 :     gel(z0,0) = t;
    1744             :   }
    1745       39147 :   if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
    1746       39147 :   return z;
    1747             : }
    1748             : /* Polynomial division x / y:
    1749             :  *   if pr = ONLY_REM return remainder, otherwise return quotient
    1750             :  *   if pr = ONLY_DIVIDES return quotient if division is exact, else NULL
    1751             :  *   if pr != NULL set *pr to remainder, as the last object on stack */
    1752             : /* assume, typ(x) = typ(y) = t_POL, same variable */
    1753             : GEN
    1754    15640998 : RgX_divrem(GEN x, GEN y, GEN *pr)
    1755             : {
    1756             :   pari_sp avy, av, av1;
    1757             :   long dx,dy,dz,i,j,sx,lr;
    1758             :   GEN z,p1,p2,rem,y_lead,mod,p;
    1759             :   GEN (*f)(GEN,GEN);
    1760             : 
    1761    15640998 :   if (!signe(y)) pari_err_INV("RgX_divrem",y);
    1762             : 
    1763    15640998 :   dy = degpol(y);
    1764    15641027 :   y_lead = gel(y,dy+2);
    1765    15641027 :   if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
    1766             :   {
    1767           0 :     pari_warn(warner,"normalizing a polynomial with 0 leading term");
    1768           0 :     for (dy--; dy>=0; dy--)
    1769             :     {
    1770           0 :       y_lead = gel(y,dy+2);
    1771           0 :       if (!gequal0(y_lead)) break;
    1772             :     }
    1773             :   }
    1774    15640862 :   if (!dy) /* y is constant */
    1775             :   {
    1776       71810 :     if (pr == ONLY_REM) return pol_0(varn(x));
    1777       15166 :     z = RgX_Rg_div(x, y_lead);
    1778       15166 :     if (pr == ONLY_DIVIDES) return z;
    1779       13535 :     if (pr) *pr = pol_0(varn(x));
    1780       13535 :     return z;
    1781             :   }
    1782    15569052 :   dx = degpol(x);
    1783    15569065 :   if (dx < dy)
    1784             :   {
    1785     1819788 :     if (pr == ONLY_REM) return RgX_copy(x);
    1786      339309 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1787      339288 :     z = pol_0(varn(x));
    1788      339288 :     if (pr) *pr = RgX_copy(x);
    1789      339288 :     return z;
    1790             :   }
    1791             : 
    1792             :   /* x,y in R[X], y non constant */
    1793    13749277 :   av = avma;
    1794    13749277 :   p = NULL;
    1795    13749277 :   if (RgX_is_FpX(x, &p) && RgX_is_FpX(y, &p) && p)
    1796             :   {
    1797      357462 :     z = FpX_divrem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, pr);
    1798      357462 :     if (!z) { avma = av; return NULL; }
    1799      357462 :     z = FpX_to_mod(z, p);
    1800      357462 :     if (!pr || pr == ONLY_REM || pr == ONLY_DIVIDES)
    1801      305389 :       return gerepileupto(av, z);
    1802       52073 :     *pr = FpX_to_mod(*pr, p);
    1803       52073 :     gerepileall(av, 2, pr, &z);
    1804       52073 :     return z;
    1805             :   }
    1806    13392150 :   switch(typ(y_lead))
    1807             :   {
    1808             :     case t_REAL:
    1809           0 :       y_lead = ginv(y_lead);
    1810           0 :       f = gmul; mod = NULL;
    1811           0 :       break;
    1812             :     case t_INTMOD:
    1813        5158 :     case t_POLMOD: y_lead = ginv(y_lead);
    1814        5158 :       f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
    1815        5158 :       break;
    1816    13386992 :     default: if (gequal1(y_lead)) y_lead = NULL;
    1817    13386853 :       f = gdiv; mod = NULL;
    1818             :   }
    1819             : 
    1820    13392011 :   if (y_lead == NULL)
    1821    11616959 :     p2 = gel(x,dx+2);
    1822             :   else {
    1823             :     for(;;) {
    1824     1775052 :       p2 = f(gel(x,dx+2),y_lead);
    1825     1775331 :       p2 = simplify_shallow(p2);
    1826     1775331 :       if (!isexactzero(p2) || (--dx < 0)) break;
    1827           0 :     }
    1828     1775331 :     if (dx < dy) /* leading coeff of x was in fact zero */
    1829             :     {
    1830           0 :       if (pr == ONLY_DIVIDES) {
    1831           0 :         avma = av;
    1832           0 :         return (dx < 0)? pol_0(varn(x)) : NULL;
    1833             :       }
    1834           0 :       if (pr == ONLY_REM)
    1835             :       {
    1836           0 :         if (dx < 0)
    1837           0 :           return gerepilecopy(av, scalarpol(p2, varn(x)));
    1838             :         else
    1839             :         {
    1840             :           GEN t;
    1841           0 :           avma = av;
    1842           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1843           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1844           0 :           return t;
    1845             :         }
    1846             :       }
    1847           0 :       if (pr) /* cf ONLY_REM above */
    1848             :       {
    1849           0 :         if (dx < 0)
    1850             :         {
    1851           0 :           p2 = gclone(p2);
    1852           0 :           avma = av;
    1853           0 :           z = pol_0(varn(x));
    1854           0 :           x = scalarpol(p2, varn(x));
    1855           0 :           gunclone(p2);
    1856             :         }
    1857             :         else
    1858             :         {
    1859             :           GEN t;
    1860           0 :           avma = av;
    1861           0 :           z = pol_0(varn(x));
    1862           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1863           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1864           0 :           x = t;
    1865             :         }
    1866           0 :         *pr = x;
    1867             :       }
    1868             :       else
    1869             :       {
    1870           0 :         avma = av;
    1871           0 :         z = pol_0(varn(x));
    1872             :       }
    1873           0 :       return z;
    1874             :     }
    1875             :   }
    1876             :   /* dx >= dy */
    1877    13392290 :   avy = avma;
    1878    13392290 :   dz = dx-dy;
    1879    13392290 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1880    13391965 :   x += 2;
    1881    13391965 :   z += 2;
    1882    13391965 :   y += 2;
    1883    13391965 :   gel(z,dz) = gcopy(p2);
    1884             : 
    1885    41863378 :   for (i=dx-1; i>=dy; i--)
    1886             :   {
    1887    28471063 :     av1=avma; p1=gel(x,i);
    1888    28471063 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1889    28412986 :     if (y_lead) p1 = simplify(f(p1,y_lead));
    1890             : 
    1891    28412986 :     if (isrationalzero(p1)) { avma=av1; p1 = gen_0; }
    1892             :     else
    1893    18400514 :       p1 = avma==av1? gcopy(p1): gerepileupto(av1,p1);
    1894    28470310 :     gel(z,i-dy) = p1;
    1895             :   }
    1896    13392315 :   if (!pr) return gerepileupto(av,z-2);
    1897             : 
    1898     6978905 :   rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
    1899     7945016 :   for (sx=0; ; i--)
    1900             :   {
    1901     7945016 :     p1 = gel(x,i);
    1902             :     /* we always enter this loop at least once */
    1903     7945016 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1904     7941930 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1905     7941930 :     if (!gequal0(p1)) { sx = 1; break; } /* remainder is non-zero */
    1906     4092833 :     if (!isexactzero(p1)) break;
    1907     4083067 :     if (!i) break;
    1908      966156 :     avma=av1;
    1909      966156 :   }
    1910     6978543 :   if (pr == ONLY_DIVIDES)
    1911             :   {
    1912        1302 :     if (sx) { avma=av; return NULL; }
    1913        1295 :     avma = (pari_sp)rem;
    1914        1295 :     return gerepileupto(av,z-2);
    1915             :   }
    1916     6977241 :   lr=i+3; rem -= lr;
    1917     6977241 :   if (avma==av1) { avma = (pari_sp)rem; p1 = gcopy(p1); }
    1918     6875031 :   else p1 = gerepileupto((pari_sp)rem,p1);
    1919     6977431 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    1920     6977110 :   rem[1] = z[-1];
    1921     6977110 :   rem += 2;
    1922     6977110 :   gel(rem,i) = p1;
    1923    21941332 :   for (i--; i>=0; i--)
    1924             :   {
    1925    14963744 :     av1=avma; p1 = gel(x,i);
    1926    14963744 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1927    14911802 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1928    14958245 :     gel(rem,i) = avma==av1? gcopy(p1):gerepileupto(av1,p1);
    1929             :   }
    1930     6977588 :   rem -= 2;
    1931     6977588 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1932     6977597 :   if (pr == ONLY_REM) return gerepileupto(av,rem);
    1933     4035977 :   z -= 2;
    1934             :   {
    1935     4035977 :     GEN *gptr[2]; gptr[0]=&z; gptr[1]=&rem;
    1936     4035977 :     gerepilemanysp(av,avy,gptr,2); *pr = rem; return z;
    1937             :   }
    1938             : }
    1939             : 
    1940             : /* x and y in (R[Y]/T)[X]  (lifted), T in R[Y]. y preferably monic */
    1941             : GEN
    1942       20868 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
    1943             : {
    1944             :   long vx, dx, dy, dz, i, j, sx, lr;
    1945             :   pari_sp av0, av, tetpil;
    1946             :   GEN z,p1,rem,lead;
    1947             : 
    1948       20868 :   if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
    1949       20868 :   vx = varn(x);
    1950       20868 :   dx = degpol(x);
    1951       20868 :   dy = degpol(y);
    1952       20868 :   if (dx < dy)
    1953             :   {
    1954        1477 :     if (pr)
    1955             :     {
    1956        1477 :       av0 = avma; x = RgXQX_red(x, T);
    1957        1477 :       if (pr == ONLY_DIVIDES) { avma=av0; return signe(x)? NULL: gen_0; }
    1958        1477 :       if (pr == ONLY_REM) return x;
    1959           0 :       *pr = x;
    1960             :     }
    1961           0 :     return pol_0(vx);
    1962             :   }
    1963       19391 :   lead = leading_coeff(y);
    1964       19391 :   if (!dy) /* y is constant */
    1965             :   {
    1966           0 :     if (pr && pr != ONLY_DIVIDES)
    1967             :     {
    1968           0 :       if (pr == ONLY_REM) return pol_0(vx);
    1969           0 :       *pr = pol_0(vx);
    1970             :     }
    1971           0 :     if (gequal1(lead)) return RgX_copy(x);
    1972           0 :     av0 = avma; x = gmul(x, ginvmod(lead,T)); tetpil = avma;
    1973           0 :     return gerepile(av0,tetpil,RgXQX_red(x,T));
    1974             :   }
    1975       19391 :   av0 = avma; dz = dx-dy;
    1976       19391 :   lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
    1977       19391 :   avma = av0;
    1978       19391 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1979       19391 :   x += 2; y += 2; z += 2;
    1980             : 
    1981       19391 :   p1 = gel(x,dx); av = avma;
    1982       19391 :   gel(z,dz) = lead? gerepileupto(av, grem(gmul(p1,lead), T)): gcopy(p1);
    1983       88262 :   for (i=dx-1; i>=dy; i--)
    1984             :   {
    1985       68871 :     av=avma; p1=gel(x,i);
    1986       68871 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1987       68871 :     if (lead) p1 = gmul(grem(p1, T), lead);
    1988       68871 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil, grem(p1, T));
    1989             :   }
    1990       19391 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
    1991             : 
    1992       19391 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
    1993       29670 :   for (sx=0; ; i--)
    1994             :   {
    1995       29670 :     p1 = gel(x,i);
    1996       29670 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1997       29670 :     tetpil=avma; p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
    1998       14116 :     if (!i) break;
    1999       10279 :     avma=av;
    2000       10279 :   }
    2001       19391 :   if (pr == ONLY_DIVIDES)
    2002             :   {
    2003        1688 :     if (lead) gunclone(lead);
    2004        1688 :     if (sx) { avma=av0; return NULL; }
    2005        1618 :     avma = (pari_sp)rem; return z-2;
    2006             :   }
    2007       17703 :   lr=i+3; rem -= lr;
    2008       17703 :   rem[0] = evaltyp(t_POL) | evallg(lr);
    2009       17703 :   rem[1] = z[-1];
    2010       17703 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
    2011       17703 :   rem += 2; gel(rem,i) = p1;
    2012       32554 :   for (i--; i>=0; i--)
    2013             :   {
    2014       14851 :     av=avma; p1 = gel(x,i);
    2015       37084 :     for (j=0; j<=i && j<=dz; j++)
    2016       22233 :       p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    2017       14851 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, grem(p1, T));
    2018             :   }
    2019       17703 :   rem -= 2;
    2020       17703 :   if (lead) gunclone(lead);
    2021       17703 :   if (!sx) (void)normalizepol_lg(rem, lr);
    2022       17703 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
    2023          77 :   *pr = rem; return z-2;
    2024             : }
    2025             : 
    2026             : /*******************************************************************/
    2027             : /*                                                                 */
    2028             : /*                        PSEUDO-DIVISION                          */
    2029             : /*                                                                 */
    2030             : /*******************************************************************/
    2031             : INLINE GEN
    2032      693614 : rem(GEN c, GEN T)
    2033             : {
    2034      693614 :   if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
    2035      693614 :   return c;
    2036             : }
    2037             : 
    2038             : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
    2039             : int
    2040        5604 : ZXQX_dvd(GEN x, GEN y, GEN T)
    2041             : {
    2042             :   long dx, dy, dz, i, p, T_ismonic;
    2043        5604 :   pari_sp av = avma, av2;
    2044             :   GEN y_lead;
    2045             : 
    2046        5604 :   if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
    2047        5604 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2048        5604 :   if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
    2049             :   /* if monic, no point in using pseudo-division */
    2050        5604 :   if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
    2051        3413 :   T_ismonic = gequal1(leading_coeff(T));
    2052        3413 :   dx = degpol(x);
    2053        3413 :   if (dx < dy) return !signe(x);
    2054        3413 :   (void)new_chunk(2);
    2055        3413 :   x = RgX_recip_shallow(x)+2;
    2056        3413 :   y = RgX_recip_shallow(y)+2;
    2057             :   /* pay attention to sparse divisors */
    2058        6952 :   for (i = 1; i <= dy; i++)
    2059        3539 :     if (!signe(gel(y,i))) gel(y,i) = NULL;
    2060        3413 :   dz = dx-dy; p = dz+1;
    2061        3413 :   av2 = avma;
    2062             :   for (;;)
    2063             :   {
    2064       31338 :     GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
    2065       31338 :     x0 = gneg(x0); p--;
    2066       31338 :     m = gcdii(cx, y0);
    2067       31338 :     if (!equali1(m))
    2068             :     {
    2069       30330 :       x0 = gdiv(x0, m);
    2070       30330 :       y0 = diviiexact(y0, m);
    2071       30330 :       if (equali1(y0)) y0 = NULL;
    2072             :     }
    2073       63502 :     for (i=1; i<=dy; i++)
    2074             :     {
    2075       32164 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    2076       32164 :       if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
    2077       32164 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    2078       32164 :       gel(x,i) = c;
    2079             :     }
    2080      378998 :     for (   ; i<=dx; i++)
    2081             :     {
    2082      347660 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    2083      347660 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    2084      347660 :       gel(x,i) = c;
    2085             :     }
    2086       34786 :     do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
    2087       31338 :     if (dx < dy) break;
    2088       27925 :     if (gc_needed(av2,1))
    2089             :     {
    2090           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
    2091           0 :       gerepilecoeffs(av2,x,dx+1);
    2092             :     }
    2093       27925 :   }
    2094        3413 :   avma = av; return (dx < 0);
    2095             : }
    2096             : 
    2097             : /* T either NULL or a t_POL. */
    2098             : GEN
    2099       25899 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
    2100             : {
    2101       25899 :   long vx = varn(x), dx, dy, dz, i, lx, p;
    2102       25899 :   pari_sp av = avma, av2;
    2103             :   GEN y_lead;
    2104             : 
    2105       25899 :   if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
    2106       25899 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2107             :   /* if monic, no point in using pseudo-division */
    2108       25899 :   if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
    2109       22441 :   dx = degpol(x);
    2110       22441 :   if (dx < dy) return RgX_copy(x);
    2111       22441 :   (void)new_chunk(2);
    2112       22441 :   x = RgX_recip_shallow(x)+2;
    2113       22441 :   y = RgX_recip_shallow(y)+2;
    2114             :   /* pay attention to sparse divisors */
    2115       71689 :   for (i = 1; i <= dy; i++)
    2116       49248 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2117       22441 :   dz = dx-dy; p = dz+1;
    2118       22441 :   av2 = avma;
    2119             :   for (;;)
    2120             :   {
    2121       83172 :     gel(x,0) = gneg(gel(x,0)); p--;
    2122      268240 :     for (i=1; i<=dy; i++)
    2123             :     {
    2124      185068 :       GEN c = gmul(y_lead, gel(x,i));
    2125      185068 :       if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
    2126      185068 :       gel(x,i) = rem(c, T);
    2127             :     }
    2128      288491 :     for (   ; i<=dx; i++)
    2129             :     {
    2130      205319 :       GEN c = gmul(y_lead, gel(x,i));
    2131      205319 :       gel(x,i) = rem(c, T);
    2132             :     }
    2133       90837 :     do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
    2134       83172 :     if (dx < dy) break;
    2135       60731 :     if (gc_needed(av2,1))
    2136             :     {
    2137           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
    2138           0 :       gerepilecoeffs(av2,x,dx+1);
    2139             :     }
    2140       60731 :   }
    2141       22441 :   if (dx < 0) return pol_0(vx);
    2142       20250 :   lx = dx+3; x -= 2;
    2143       20250 :   x[0] = evaltyp(t_POL) | evallg(lx);
    2144       20250 :   x[1] = evalsigne(1) | evalvarn(vx);
    2145       20250 :   x = RgX_recip_shallow(x);
    2146       20250 :   if (p)
    2147             :   { /* multiply by y[0]^p   [beware dummy vars from FpX_FpXY_resultant] */
    2148        1176 :     GEN t = y_lead;
    2149        1176 :     if (T && typ(t) == t_POL && varn(t) == varn(T))
    2150           0 :       t = RgXQ_powu(t, p, T);
    2151             :     else
    2152        1176 :       t = gpowgs(t, p);
    2153        4060 :     for (i=2; i<lx; i++)
    2154             :     {
    2155        2884 :       GEN c = gmul(gel(x,i), t);
    2156        2884 :       gel(x,i) = rem(c,T);
    2157             :     }
    2158        1176 :     if (!T) return gerepileupto(av, x);
    2159             :   }
    2160       19074 :   return gerepilecopy(av, x);
    2161             : }
    2162             : 
    2163             : GEN
    2164       25899 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
    2165             : 
    2166             : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
    2167             : GEN
    2168       53922 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
    2169             : {
    2170       53922 :   long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
    2171       53922 :   pari_sp av = avma, av2;
    2172             :   GEN z, r, ypow, y_lead;
    2173             : 
    2174       53922 :   if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
    2175       53922 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2176       53922 :   if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
    2177       27321 :   dx = degpol(x);
    2178       27321 :   if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
    2179       27321 :   if (dx == dy)
    2180             :   {
    2181          28 :     GEN x_lead = gel(x,lg(x)-1);
    2182          28 :     x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
    2183          28 :     y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
    2184          28 :     r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
    2185          28 :     *ptr = gerepileupto(av, r); return scalarpol(x_lead, vx);
    2186             :   }
    2187       27293 :   (void)new_chunk(2);
    2188       27293 :   x = RgX_recip_shallow(x)+2;
    2189       27293 :   y = RgX_recip_shallow(y)+2;
    2190             :   /* pay attention to sparse divisors */
    2191      119392 :   for (i = 1; i <= dy; i++)
    2192       92099 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2193       27293 :   dz = dx-dy; p = dz+1;
    2194       27293 :   lz = dz+3;
    2195       27293 :   z = cgetg(lz, t_POL);
    2196       27293 :   z[1] = evalsigne(1) | evalvarn(vx);
    2197       27293 :   for (i = 2; i < lz; i++) gel(z,i) = gen_0;
    2198       27293 :   ypow = new_chunk(dz+1);
    2199       27293 :   gel(ypow,0) = gen_1;
    2200       27293 :   gel(ypow,1) = y_lead;
    2201       35125 :   for (i=2; i<=dz; i++)
    2202             :   {
    2203        7832 :     GEN c = gmul(gel(ypow,i-1), y_lead);
    2204        7832 :     gel(ypow,i) = rem(c,T);
    2205             :   }
    2206       27293 :   av2 = avma;
    2207       27293 :   for (iz=2;;)
    2208             :   {
    2209       56533 :     p--;
    2210       56533 :     gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
    2211      243367 :     for (i=1; i<=dy; i++)
    2212             :     {
    2213      186834 :       GEN c = gmul(y_lead, gel(x,i));
    2214      186834 :       if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
    2215      186834 :       gel(x,i) = rem(c, T);
    2216             :     }
    2217      105677 :     for (   ; i<=dx; i++)
    2218             :     {
    2219       49144 :       GEN c = gmul(y_lead, gel(x,i));
    2220       49144 :       gel(x,i) = rem(c,T);
    2221             :     }
    2222       56533 :     x++; dx--;
    2223       56533 :     while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
    2224       56533 :     if (dx < dy) break;
    2225       29240 :     if (gc_needed(av2,1))
    2226             :     {
    2227           0 :       GEN X = x-2;
    2228           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
    2229           0 :       X[0] = evaltyp(t_POL)|evallg(dx+3); X[1] = z[1]; /* hack */
    2230           0 :       gerepileall(av2,2, &X, &z); x = X+2;
    2231             :     }
    2232       29240 :   }
    2233       27293 :   while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
    2234       27293 :   if (dx < 0)
    2235          98 :     x = pol_0(vx);
    2236             :   else
    2237             :   {
    2238       27195 :     lx = dx+3; x -= 2;
    2239       27195 :     x[0] = evaltyp(t_POL) | evallg(lx);
    2240       27195 :     x[1] = evalsigne(1) | evalvarn(vx);
    2241       27195 :     x = RgX_recip_shallow(x);
    2242             :   }
    2243       27293 :   z = RgX_recip_shallow(z);
    2244       27293 :   r = x;
    2245       27293 :   if (p)
    2246             :   {
    2247        4029 :     GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
    2248        4029 :     if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
    2249             :   }
    2250       27293 :   gerepileall(av, 2, &z, &r);
    2251       27293 :   *ptr = r; return z;
    2252             : }
    2253             : GEN
    2254       53768 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
    2255       53768 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
    2256             : 
    2257             : GEN
    2258           0 : RgXQX_mul(GEN x, GEN y, GEN T)
    2259             : {
    2260           0 :   return RgXQX_red(RgX_mul(x,y), T);
    2261             : }
    2262             : GEN
    2263    70753008 : RgX_Rg_mul(GEN y, GEN x) {
    2264             :   long i, ly;
    2265    70753008 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2266    70753008 :   if (ly == 2) return z;
    2267    70585179 :   for (i = 2; i < ly; i++) gel(z,i) = gmul(x,gel(y,i));
    2268    70585172 :   return normalizepol_lg(z,ly);
    2269             : }
    2270             : GEN
    2271       13881 : RgX_muls(GEN y, long x) {
    2272             :   long i, ly;
    2273       13881 :   GEN z = cgetg_copy(y, &ly); z[1] = y[1];
    2274       13881 :   if (ly == 2) return z;
    2275       13846 :   for (i = 2; i < ly; i++) gel(z,i) = gmulsg(x,gel(y,i));
    2276       13846 :   return normalizepol_lg(z,ly);
    2277             : }
    2278             : GEN
    2279         756 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T)
    2280             : {
    2281         756 :   return RgXQX_red(RgX_Rg_mul(x,y), T);
    2282             : }
    2283             : GEN
    2284          56 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T)
    2285             : {
    2286          56 :   return RgXQV_red(RgV_Rg_mul(v,x), T);
    2287             : }
    2288             : 
    2289             : GEN
    2290           0 : RgXQX_sqr(GEN x, GEN T)
    2291             : {
    2292           0 :   return RgXQX_red(RgX_sqr(x), T);
    2293             : }
    2294             : 
    2295             : static GEN
    2296       65163 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
    2297             : static GEN
    2298           0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
    2299             : static GEN
    2300      248742 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
    2301             : static GEN
    2302      101909 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
    2303             : static GEN
    2304      110733 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
    2305             : static GEN
    2306      105686 : _one(void *data) { return pol_1(varn((GEN)data)); }
    2307             : static GEN
    2308         105 : _zero(void *data) { return pol_0(varn((GEN)data)); }
    2309             : static GEN
    2310       70679 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
    2311             : 
    2312             : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
    2313             :               _mul, _sqr, _one, _zero };
    2314             : 
    2315             : GEN
    2316           0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
    2317             : {
    2318           0 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
    2319             : }
    2320             : 
    2321             : GEN
    2322       44996 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
    2323             : {
    2324       44996 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2325       44996 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
    2326             : }
    2327             : 
    2328             : /* mod X^n */
    2329             : struct modXn {
    2330             :   long v; /* varn(X) */
    2331             :   long n;
    2332             : } ;
    2333             : static GEN
    2334        1806 : _sqrXn(void *data, GEN x) {
    2335        1806 :   struct modXn *S = (struct modXn*)data;
    2336        1806 :   return RgXn_sqr(x, S->n);
    2337             : }
    2338             : static GEN
    2339        1190 : _mulXn(void *data, GEN x, GEN y) {
    2340        1190 :   struct modXn *S = (struct modXn*)data;
    2341        1190 :   return RgXn_mul(x,y, S->n);
    2342             : }
    2343             : static GEN
    2344        1407 : _oneXn(void *data) {
    2345        1407 :   struct modXn *S = (struct modXn*)data;
    2346        1407 :   return pol_1(S->v);
    2347             : }
    2348             : static GEN
    2349           0 : _zeroXn(void *data) {
    2350           0 :   struct modXn *S = (struct modXn*)data;
    2351           0 :   return pol_0(S->v);
    2352             : }
    2353             : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
    2354             :                                           _oneXn, _zeroXn };
    2355             : 
    2356             : GEN
    2357         336 : RgXn_powers(GEN x, long m, long n)
    2358             : {
    2359         336 :   long d = degpol(x);
    2360         336 :   int use_sqr = (d<<1) >= n;
    2361             :   struct modXn S;
    2362         336 :   S.v = varn(x); S.n = n;
    2363         336 :   return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
    2364             : }
    2365             : 
    2366             : GEN
    2367        1526 : RgXn_powu_i(GEN x, ulong m, long n)
    2368             : {
    2369             :   struct modXn S;
    2370        1526 :   S.v = varn(x); S.n = n;
    2371        1526 :   return gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
    2372             : }
    2373             : GEN
    2374           0 : RgXn_powu(GEN x, ulong m, long n)
    2375             : {
    2376             :   struct modXn S;
    2377           0 :   S.v = varn(x); S.n = n;
    2378           0 :   return gen_powu(x, m, (void*)&S,_sqrXn,_mulXn);
    2379             : }
    2380             : 
    2381             : GEN
    2382         672 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
    2383             : {
    2384             :   struct modXn S;
    2385         672 :   S.v = varn(gel(x,2)); S.n = n;
    2386         672 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
    2387             : }
    2388             : 
    2389             : GEN
    2390           0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
    2391             : {
    2392           0 :   int use_sqr = 2*degpol(x) >= n;
    2393             :   struct modXn S;
    2394           0 :   S.v = varn(x); S.n = n;
    2395           0 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2396             : }
    2397             : 
    2398             : /* Q(x) mod t^n, x in R[t], n >= 1 */
    2399             : GEN
    2400        1799 : RgXn_eval(GEN Q, GEN x, long n)
    2401             : {
    2402        1799 :   long d = degpol(x);
    2403             :   int use_sqr;
    2404             :   struct modXn S;
    2405        1799 :   if (d == 1 && isrationalzero(gel(x,2)))
    2406             :   {
    2407        1792 :     GEN y = RgX_unscale(Q, gel(x,3));
    2408        1792 :     setvarn(y, varn(x)); return y;
    2409             :   }
    2410           7 :   S.v = varn(x);
    2411           7 :   S.n = n;
    2412           7 :   use_sqr = (d<<1) >= n;
    2413           7 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2414             : }
    2415             : 
    2416             : /* (f*g mod t^n) \ t^n2, assuming 2*n2 >= n */
    2417             : static GEN
    2418       33816 : RgXn_mulhigh(GEN f, GEN g, long n2, long n)
    2419             : {
    2420       33816 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2421       33816 :   return RgX_add(RgX_mulhigh_i(fl, g, n2), RgXn_mul(fh, g, n - n2));
    2422             : }
    2423             : 
    2424             : /* (f^2 mod t^n) \ t^n2, assuming 2*n2 >= n */
    2425             : static GEN
    2426           0 : RgXn_sqrhigh(GEN f, long n2, long n)
    2427             : {
    2428           0 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2429           0 :   return RgX_add(RgX_mulhigh_i(fl, f, n2), RgXn_mul(fh, f, n - n2));
    2430             : }
    2431             : 
    2432             : GEN
    2433      112602 : RgXn_inv(GEN f, long e)
    2434             : {
    2435      112602 :   pari_sp av = avma, av2;
    2436             :   ulong mask;
    2437             :   GEN W, a;
    2438      112602 :   long v = varn(f), n = 1;
    2439             : 
    2440      112602 :   if (!signe(f)) pari_err_INV("RgXn_inv",f);
    2441      112602 :   a = ginv(gel(f,2));
    2442      112602 :   if (e == 1) return scalarpol(a, v);
    2443      112602 :   else if (e == 2)
    2444             :   {
    2445             :     GEN b;
    2446       98994 :     if (degpol(f) <= 0 || gequal0(b = gel(f,3))) return scalarpol(a, v);
    2447       80185 :     b = gneg(b);
    2448       80185 :     if (!gequal1(a)) b = gmul(b, gsqr(a));
    2449       80185 :     W = deg1pol_shallow(b, a, v);
    2450       80185 :     return gerepilecopy(av, W);
    2451             :   }
    2452       13608 :   W = scalarpol_shallow(ginv(gel(f,2)),v);
    2453       13608 :   mask = quadratic_prec_mask(e);
    2454       13608 :   av2 = avma;
    2455       61032 :   for (;mask>1;)
    2456             :   {
    2457             :     GEN u, fr;
    2458       33816 :     long n2 = n;
    2459       33816 :     n<<=1; if (mask & 1) n--;
    2460       33816 :     mask >>= 1;
    2461       33816 :     fr = RgXn_red_shallow(f, n);
    2462       33816 :     u = RgXn_mul(W, RgXn_mulhigh(fr, W, n2, n), n-n2);
    2463       33816 :     W = RgX_sub(W, RgX_shift_shallow(u, n2));
    2464       33816 :     if (gc_needed(av2,2))
    2465             :     {
    2466           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
    2467           0 :       W = gerepileupto(av2, W);
    2468             :     }
    2469             :   }
    2470       13608 :   return gerepileupto(av, W);
    2471             : }
    2472             : 
    2473             : GEN
    2474       12586 : RgXn_exp(GEN h, long e)
    2475             : {
    2476       12586 :   pari_sp av = avma, av2;
    2477       12586 :   long v = varn(h), n=1;
    2478       12586 :   GEN f = pol_1(v), g = pol_1(v);
    2479       12586 :   ulong mask = quadratic_prec_mask(e);
    2480       12586 :   av2 = avma;
    2481       12586 :   if (signe(h)==0 || degpol(h)<1 || !gequal0(gel(h,2)))
    2482           0 :     pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
    2483       38822 :   for (;mask>1;)
    2484             :   {
    2485             :     GEN q, w;
    2486       13650 :     long n2 = n;
    2487       13650 :     n<<=1; if (mask & 1) n--;
    2488       13650 :     mask >>= 1;
    2489       13650 :     g = RgX_sub(RgX_muls(g,2),RgXn_mul(f,RgXn_sqr(g,n2),n2));
    2490       13650 :     q = RgX_deriv(RgXn_red_shallow(h,n2));
    2491       13650 :     w = RgX_add(q, RgXn_mul(g, RgX_sub(RgX_deriv(f), RgXn_mul(f,q,n-1)),n-1));
    2492       13650 :     f = RgX_add(f, RgXn_mul(f, RgX_sub(RgXn_red_shallow(h, n), RgX_integ(w)), n));
    2493       13650 :     if (gc_needed(av2,2))
    2494             :     {
    2495           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_exp, e = %ld", n);
    2496           0 :       gerepileall(av2, 2, &f, &g);
    2497             :     }
    2498             :   }
    2499       12586 :   return gerepileupto(av, f);
    2500             : }
    2501             : 
    2502             : GEN
    2503          84 : RgXn_reverse(GEN f, long e)
    2504             : {
    2505          84 :   pari_sp av = avma, av2;
    2506             :   ulong mask;
    2507             :   GEN fi, a, df, W, an;
    2508          84 :   long v = varn(f), n=1;
    2509          84 :   if (degpol(f)<1 || !gequal0(gel(f,2)))
    2510           0 :     pari_err_INV("serreverse",f);
    2511          84 :   fi = ginv(gel(f,3));
    2512          84 :   a = deg1pol_shallow(fi,gen_0,v);
    2513          84 :   if (e <= 2) return gerepilecopy(av, a);
    2514          84 :   W = scalarpol(fi,v);
    2515          84 :   df = RgX_deriv(f);
    2516          84 :   mask = quadratic_prec_mask(e);
    2517          84 :   av2 = avma;
    2518         504 :   for (;mask>1;)
    2519             :   {
    2520             :     GEN u, fa, fr;
    2521         336 :     long n2 = n, rt;
    2522         336 :     n<<=1; if (mask & 1) n--;
    2523         336 :     mask >>= 1;
    2524         336 :     fr = RgXn_red_shallow(f, n);
    2525         336 :     rt = brent_kung_optpow(degpol(fr), 4, 3);
    2526         336 :     an = RgXn_powers(a, rt, n);
    2527         336 :     if (n>1)
    2528             :     {
    2529         336 :       long n4 = (n2+1)>>1;
    2530         336 :       GEN dfr = RgXn_red_shallow(df, n2);
    2531         336 :       dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
    2532         336 :       u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
    2533         336 :       W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
    2534             :     }
    2535         336 :     fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
    2536         336 :     fa = RgX_shift(fa, -n2);
    2537         336 :     a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
    2538         336 :     if (gc_needed(av2,2))
    2539             :     {
    2540           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
    2541           0 :       gerepileall(av2, 2, &a, &W);
    2542             :     }
    2543             :   }
    2544          84 :   return gerepileupto(av, a);
    2545             : }
    2546             : 
    2547             : GEN
    2548           0 : RgXn_sqrt(GEN h, long e)
    2549             : {
    2550           0 :   pari_sp av = avma, av2;
    2551           0 :   long v = varn(h), n = 1;
    2552           0 :   GEN f = scalarpol(gen_1, v), df = f;
    2553           0 :   ulong mask = quadratic_prec_mask(e);
    2554           0 :   if (degpol(h)<0 || !gequal1(gel(h,2)))
    2555           0 :     pari_err_SQRTN("RgXn_sqrt",h);
    2556           0 :   av2 = avma;
    2557             :   while(1)
    2558             :   {
    2559           0 :     long n2 = n, m;
    2560             :     GEN g;
    2561           0 :     n<<=1; if (mask & 1) n--;
    2562           0 :     mask >>= 1;
    2563           0 :     m = n-n2;
    2564           0 :     g = RgX_sub(RgXn_sqrhigh(f, n2, n), RgX_shift_shallow(RgXn_red_shallow(h, n),-n2));
    2565           0 :     f = RgX_sub(f, RgX_shift_shallow(RgXn_mul(gmul2n(df, -1), g, m), n2));
    2566           0 :     if (mask==1) return gerepileupto(av, f);
    2567           0 :     g = RgXn_mul(df, RgXn_mulhigh(df, f, n2, n), m);
    2568           0 :     df = RgX_sub(df, RgX_shift_shallow(g, n2));
    2569           0 :     if (gc_needed(av2,2))
    2570             :     {
    2571           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_sqrt, e = %ld", n);
    2572           0 :       gerepileall(av2, 2, &f, &df);
    2573             :     }
    2574           0 :   }
    2575             : }
    2576             : 
    2577             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2578             : GEN
    2579      205766 : RgXQ_powu(GEN x, ulong n, GEN T)
    2580             : {
    2581             :   pari_sp av;
    2582             :   GEN y;
    2583             : 
    2584      205766 :   if (!n) return pol_1(varn(x));
    2585      204226 :   if (n == 1) return RgX_copy(x);
    2586      140140 :   av = avma;
    2587      140140 :   y = gen_powu(x, n, (void*)T, &_sqr, &_mul);
    2588      140139 :   return gerepileupto(av, y);
    2589             : }
    2590             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2591             : GEN
    2592       17877 : RgXQ_pow(GEN x, GEN n, GEN T)
    2593             : {
    2594             :   pari_sp av;
    2595       17877 :   long s = signe(n);
    2596             :   GEN y;
    2597             : 
    2598       17877 :   if (!s) return pol_1(varn(x));
    2599       17877 :   if (is_pm1(n) == 1)
    2600           0 :     return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
    2601       17877 :   av = avma;
    2602       17877 :   if (s < 0) x = RgXQ_inv(x, T);
    2603       17877 :   y = gen_pow(x, n, (void*)T, &_sqr, &_mul);
    2604       17877 :   return gerepileupto(av, y);
    2605             : }
    2606             : 
    2607             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2608             : GEN
    2609        2317 : RgXQ_powers(GEN x, long l, GEN T)
    2610             : {
    2611        2317 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2612        2317 :   return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
    2613             : }
    2614             : 
    2615             : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
    2616             : GEN
    2617        1883 : QXQ_powers(GEN a, long n, GEN T)
    2618             : {
    2619        1883 :   GEN den, v = RgXQ_powers(Q_remove_denom(a, &den), n, T);
    2620             :   /* den*a integral; v[i+1] = (den*a)^i in K */
    2621        1883 :   if (den)
    2622             :   { /* restore denominators */
    2623        1267 :     GEN d = den;
    2624             :     long i;
    2625        1267 :     gel(v,2) = a;
    2626        3850 :     for (i=3; i<=n+1; i++) {
    2627        2583 :       d = mulii(d,den);
    2628        2583 :       gel(v,i) = RgX_Rg_div(gel(v,i), d);
    2629             :     }
    2630             :   }
    2631        1883 :   return v;
    2632             : }
    2633             : 
    2634             : static GEN
    2635        1239 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
    2636             : {
    2637        1239 :   long l, i, m = 0;
    2638             :   GEN dz, z;
    2639        1239 :   GEN V = cgetg_copy(v, &l);
    2640        3983 :   for (i = imin; i < l; i++)
    2641             :   {
    2642        2744 :     GEN c = gel(v, i);
    2643        2744 :     if (typ(c) == t_POL) m = maxss(m, degpol(c));
    2644             :   }
    2645        1239 :   z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
    2646        1239 :   for (i = 1; i < imin; i++) V[i] = v[i];
    2647        3983 :   for (i = imin; i < l; i++)
    2648             :   {
    2649        2744 :     GEN c = gel(v,i);
    2650        2744 :     if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
    2651        2744 :     gel(V,i) = c;
    2652             :   }
    2653        1239 :   return V;
    2654             : }
    2655             : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
    2656             : GEN
    2657        1176 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
    2658        1176 : { return do_QXQ_eval(v, 1, a, T); }
    2659             : GEN
    2660          63 : QXX_QXQ_eval(GEN v, GEN a, GEN T)
    2661          63 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
    2662             : 
    2663             : GEN
    2664         287 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
    2665             : {
    2666         287 :   return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
    2667             : }
    2668             : 
    2669             : GEN
    2670          56 : RgXQ_minpoly_naive(GEN y, GEN P)
    2671             : {
    2672          56 :   pari_sp ltop=avma;
    2673          56 :   long n=lgpol(P);
    2674          56 :   GEN M=ker(RgXQ_matrix_pow(y,n,n,P));
    2675          56 :   M=content(RgM_to_RgXV(M,varn(P)));
    2676          56 :   return gerepileupto(ltop,M);
    2677             : }
    2678             : 
    2679             : GEN
    2680       34752 : RgXQ_norm(GEN x, GEN T)
    2681             : {
    2682             :   pari_sp av;
    2683       34752 :   long dx = degpol(x);
    2684             :   GEN L, y;
    2685             : 
    2686       34752 :   av = avma; y = resultant(T, x);
    2687       34752 :   L = leading_coeff(T);
    2688       34752 :   if (gequal1(L) || !signe(x)) return y;
    2689           0 :   return gerepileupto(av, gdiv(y, gpowgs(L, dx)));
    2690             : }
    2691             : 
    2692             : GEN
    2693      103928 : RgX_blocks(GEN P, long n, long m)
    2694             : {
    2695      103928 :   GEN z = cgetg(m+1,t_VEC);
    2696      103928 :   long i,j, k=2, l = lg(P);
    2697      504956 :   for(i=1; i<=m; i++)
    2698             :   {
    2699      401028 :     GEN zi = cgetg(n+2,t_POL);
    2700      401028 :     zi[1] = P[1];
    2701      401028 :     gel(z,i) = zi;
    2702     2373437 :     for(j=2; j<n+2; j++)
    2703     1972409 :       gel(zi, j) = k==l ? gen_0 : gel(P,k++);
    2704      401028 :     zi = RgX_renormalize_lg(zi, n+2);
    2705             :   }
    2706      103928 :   return z;
    2707             : }
    2708             : 
    2709             : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
    2710             : void
    2711       30261 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
    2712             : {
    2713       30261 :   long n = degpol(p), v = varn(p), n0, n1, i;
    2714             :   GEN p0, p1;
    2715             : 
    2716       60522 :   if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
    2717             : 
    2718       30261 :   n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
    2719       30261 :   p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
    2720       30261 :   p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
    2721      931453 :   for (i=0; i<n1; i++)
    2722             :   {
    2723      901192 :     p0[2+i] = p[2+(i<<1)];
    2724      901192 :     p1[2+i] = p[3+(i<<1)];
    2725             :   }
    2726       30261 :   if (n1 != n0)
    2727       21764 :     p0[2+i] = p[2+(i<<1)];
    2728       30261 :   *pe = normalizepol(p0);
    2729       30260 :   *po = normalizepol(p1);
    2730             : }
    2731             : 
    2732             : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
    2733             : GEN
    2734       40614 : RgX_splitting(GEN p, long k)
    2735             : {
    2736       40614 :   long n = degpol(p), v = varn(p), m, i, j, l;
    2737             :   GEN r;
    2738             : 
    2739       40614 :   m = n/k;
    2740       40614 :   r = cgetg(k+1,t_VEC);
    2741      223930 :   for(i=1; i<=k; i++)
    2742             :   {
    2743      183316 :     gel(r,i) = cgetg(m+3, t_POL);
    2744      183316 :     mael(r,i,1) = evalvarn(v)|evalsigne(1);
    2745             :   }
    2746      552426 :   for (j=1, i=0, l=2; i<=n; i++)
    2747             :   {
    2748      511812 :     gmael(r,j,l) = gel(p,2+i);
    2749      511812 :     if (j==k) { j=1; l++; } else j++;
    2750             :   }
    2751      223930 :   for(i=1; i<=k; i++)
    2752      183316 :     gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
    2753       40614 :   return r;
    2754             : }
    2755             : 
    2756             : /*******************************************************************/
    2757             : /*                                                                 */
    2758             : /*                        Kronecker form                           */
    2759             : /*                                                                 */
    2760             : /*******************************************************************/
    2761             : 
    2762             : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
    2763             :  * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
    2764             :  * normalized coefficients */
    2765             : GEN
    2766      145085 : Kronecker_to_mod(GEN z, GEN T)
    2767             : {
    2768      145085 :   long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
    2769      145085 :   GEN x, t = cgetg(N,t_POL);
    2770      145085 :   t[1] = T[1];
    2771      145085 :   lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
    2772      145085 :   x[1] = z[1];
    2773      145085 :   T = RgX_copy(T);
    2774      691735 :   for (i=2; i<lx+2; i++, z+= N-2)
    2775             :   {
    2776      546650 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
    2777      546650 :     gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2778             :   }
    2779      145085 :   N = (l-2) % (N-2) + 2;
    2780      145085 :   for (j=2; j<N; j++) t[j] = z[j];
    2781      145085 :   gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2782      145085 :   return normalizepol_lg(x, i+1);
    2783             : }

Generated by: LCOV version 1.11