Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - RgX.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.18.0 lcov report (development 29806-4d001396c7) Lines: 1612 1765 91.3 %
Date: 2024-12-21 09:08:57 Functions: 200 218 91.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : #include "pari.h"
      16             : #include "paripriv.h"
      17             : 
      18             : #define DEBUGLEVEL DEBUGLEVEL_pol
      19             : 
      20             : /*******************************************************************/
      21             : /*                                                                 */
      22             : /*                         GENERIC                                 */
      23             : /*                                                                 */
      24             : /*******************************************************************/
      25             : 
      26             : /* Return optimal parameter l for the evaluation of n/m polynomials of degree d
      27             :    Fractional values can be used if the evaluations are done with different
      28             :    accuracies, and thus have different weights.
      29             :  */
      30             : long
      31    19687453 : brent_kung_optpow(long d, long n, long m)
      32             : {
      33             :   long p, r;
      34    19687453 :   long pold=1, rold=n*(d-1);
      35    92682108 :   for(p=2; p<=d; p++)
      36             :   {
      37    72994655 :     r = m*(p-1) + n*((d-1)/p);
      38    72994655 :     if (r<rold) { pold=p; rold=r; }
      39             :   }
      40    19687453 :   return pold;
      41             : }
      42             : 
      43             : static GEN
      44    14486799 : gen_RgXQ_eval_powers(GEN P, GEN V, long a, long n, void *E, const struct bb_algebra *ff,
      45             :                                            GEN cmul(void *E, GEN P, long a, GEN x))
      46             : {
      47    14486799 :   pari_sp av = avma;
      48             :   long i;
      49    14486799 :   GEN z = cmul(E,P,a,ff->one(E));
      50    14465113 :   if (!z) z = gen_0;
      51    75542235 :   for (i=1; i<=n; i++)
      52             :   {
      53    61078156 :     GEN t = cmul(E,P,a+i,gel(V,i+1));
      54    61095141 :     if (t) {
      55    46173854 :       z = ff->add(E, z, t);
      56    46139197 :       if (gc_needed(av,2)) z = gerepileupto(av, z);
      57             :     }
      58             :   }
      59    14464079 :   return ff->red(E,z);
      60             : }
      61             : 
      62             : /* Brent & Kung
      63             :  * (Fast algorithms for manipulating formal power series, JACM 25:581-595, 1978)
      64             :  *
      65             :  * V as output by FpXQ_powers(x,l,T,p). For optimal performance, l is as given
      66             :  * by brent_kung_optpow */
      67             : GEN
      68    12438056 : gen_bkeval_powers(GEN P, long d, GEN V, void *E, const struct bb_algebra *ff,
      69             :                                      GEN cmul(void *E, GEN P, long a, GEN x))
      70             : {
      71    12438056 :   pari_sp av = avma;
      72    12438056 :   long l = lg(V)-1;
      73             :   GEN z, u;
      74             : 
      75    12438056 :   if (d < 0) return ff->zero(E);
      76    11831071 :   if (d < l) return gerepileupto(av, gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul));
      77     1147803 :   if (l<2) pari_err_DOMAIN("gen_RgX_bkeval_powers", "#powers", "<",gen_2,V);
      78     1147803 :   if (DEBUGLEVEL>=8)
      79             :   {
      80           0 :     long cnt = 1 + (d - l) / (l-1);
      81           0 :     err_printf("RgX_RgXQV_eval(%ld/%ld): %ld RgXQ_mul\n", d, l-1, cnt);
      82             :   }
      83     1147803 :   d -= l;
      84     1147803 :   z = gen_RgXQ_eval_powers(P,V,d+1,l-1,E,ff,cmul);
      85     2654814 :   while (d >= l-1)
      86             :   {
      87     1505860 :     d -= l-1;
      88     1505860 :     u = gen_RgXQ_eval_powers(P,V,d+1,l-2,E,ff,cmul);
      89     1505905 :     z = ff->add(E,u, ff->mul(E,z,gel(V,l)));
      90     1505833 :     if (gc_needed(av,2))
      91         105 :       z = gerepileupto(av, z);
      92             :   }
      93     1148954 :   u = gen_RgXQ_eval_powers(P,V,0,d,E,ff,cmul);
      94     1148955 :   z = ff->add(E,u, ff->mul(E,z,gel(V,d+2)));
      95     1148958 :   return gerepileupto(av, ff->red(E,z));
      96             : }
      97             : 
      98             : GEN
      99      866712 : gen_bkeval(GEN Q, long d, GEN x, int use_sqr, void *E, const struct bb_algebra *ff,
     100             :                                       GEN cmul(void *E, GEN P, long a, GEN x))
     101             : {
     102      866712 :   pari_sp av = avma;
     103             :   GEN z, V;
     104             :   long rtd;
     105      866712 :   if (d < 0) return ff->zero(E);
     106      865634 :   rtd = (long) sqrt((double)d);
     107      865634 :   V = gen_powers(x,rtd,use_sqr,E,ff->sqr,ff->mul,ff->one);
     108      865650 :   z = gen_bkeval_powers(Q, d, V, E, ff, cmul);
     109      865640 :   return gerepileupto(av, z);
     110             : }
     111             : 
     112             : static GEN
     113     2027295 : _gen_nored(void *E, GEN x) { (void)E; return x; }
     114             : static GEN
     115    19227861 : _gen_add(void *E, GEN x, GEN y) { (void)E; return gadd(x, y); }
     116             : static GEN
     117           0 : _gen_sub(void *E, GEN x, GEN y) { (void)E; return gsub(x, y); }
     118             : static GEN
     119     1842267 : _gen_mul(void *E, GEN x, GEN y) { (void)E; return gmul(x, y); }
     120             : static GEN
     121      599533 : _gen_sqr(void *E, GEN x) { (void)E; return gsqr(x); }
     122             : static GEN
     123     2068338 : _gen_one(void *E) { (void)E; return gen_1; }
     124             : static GEN
     125       24184 : _gen_zero(void *E) { (void)E; return gen_0; }
     126             : 
     127             : static struct bb_algebra Rg_algebra = { _gen_nored, _gen_add, _gen_sub,
     128             :               _gen_mul, _gen_sqr,_gen_one,_gen_zero };
     129             : 
     130             : static GEN
     131      512818 : _gen_cmul(void *E, GEN P, long a, GEN x)
     132      512818 : {(void)E; return gmul(gel(P,a+2), x);}
     133             : 
     134             : GEN
     135      166768 : RgX_RgV_eval(GEN Q, GEN x)
     136             : {
     137      166768 :   return gen_bkeval_powers(Q, degpol(Q), x, NULL, &Rg_algebra, _gen_cmul);
     138             : }
     139             : 
     140             : GEN
     141           0 : RgX_Rg_eval_bk(GEN Q, GEN x)
     142             : {
     143           0 :   return gen_bkeval(Q, degpol(Q), x, 1, NULL, &Rg_algebra, _gen_cmul);
     144             : }
     145             : 
     146             : GEN
     147        2947 : RgXV_RgV_eval(GEN Q, GEN x)
     148             : {
     149        2947 :   long i, l = lg(Q), vQ = gvar(Q);
     150        2947 :   GEN v = cgetg(l, t_VEC);
     151      248311 :   for (i = 1; i < l; i++)
     152             :   {
     153      245364 :     GEN Qi = gel(Q, i);
     154      245364 :     gel(v, i) = typ(Qi)==t_POL && varn(Qi)==vQ? RgX_RgV_eval(Qi, x): gcopy(Qi);
     155             :   }
     156        2947 :   return v;
     157             : }
     158             : 
     159             : GEN
     160      666145 : RgX_homogenous_evalpow(GEN P, GEN A, GEN B)
     161             : {
     162      666145 :   pari_sp av = avma, btop;
     163      666145 :   long i, d = degpol(P), o;
     164             :   GEN s;
     165      666146 :   if (signe(P)==0) return pol_0(varn(P));
     166      666146 :   s = gel(P, d+2);
     167      666146 :   if (d == 0) return gcopy(s);
     168      662870 :   o = RgX_deflate_order(P);
     169      662888 :   if (o > 1) A = gpowgs(A, o);
     170      662897 :   btop = avma;
     171     2304949 :   for (i = d-o; i >= 0; i-=o)
     172             :   {
     173     1642060 :     s = gadd(gmul(s, A), gmul(gel(B,d+1-i), gel(P,i+2)));
     174     1642052 :     if (gc_needed(btop,1))
     175             :     {
     176          13 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_homogenous_eval(%ld)",i);
     177          13 :       s = gerepileupto(btop, s);
     178             :     }
     179             :   }
     180      662889 :   return gerepileupto(av, s);
     181             : }
     182             : 
     183             : GEN
     184        1652 : QXQX_homogenous_evalpow(GEN P, GEN A, GEN B, GEN T)
     185             : {
     186        1652 :   pari_sp av = avma;
     187        1652 :   long i, d = degpol(P), v = varn(A);
     188             :   GEN s;
     189        1652 :   if (signe(P)==0) return pol_0(v);
     190        1652 :   if (d == 0) return scalarpol(gel(P, d+2), v);
     191        1232 :   s = scalarpol_shallow(gel(P, d+2), v);
     192        4963 :   for (i = d-1; i >= 0; i--)
     193             :   {
     194        3731 :     GEN c = gel(P,i+2), b = gel(B,d+1-i);
     195        3731 :     s = RgX_add(QXQX_mul(s, A, T), typ(c)==t_POL ? QXQX_QXQ_mul(b, c, T): gmul(b, c));
     196        3731 :     if (gc_needed(av,1))
     197             :     {
     198           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"QXQX_homogenous_eval(%ld)",i);
     199           0 :       s = gerepileupto(av, s);
     200             :     }
     201             :   }
     202        1232 :   return gerepileupto(av, s);
     203             : }
     204             : 
     205             : const struct bb_algebra *
     206      281044 : get_Rg_algebra(void)
     207             : {
     208      281044 :   return &Rg_algebra;
     209             : }
     210             : 
     211             : static struct bb_ring Rg_ring = {  _gen_add, _gen_mul, _gen_sqr };
     212             : 
     213             : static GEN
     214       30352 : _RgX_divrem(void *E, GEN x, GEN y, GEN *r)
     215             : {
     216             :   (void) E;
     217       30352 :   return RgX_divrem(x, y, r);
     218             : }
     219             : 
     220             : GEN
     221        6923 : RgX_digits(GEN x, GEN T)
     222             : {
     223        6923 :   long d = degpol(T), n = (lgpol(x)+d-1)/d;
     224        6923 :   return gen_digits(x,T,n,NULL, &Rg_ring, _RgX_divrem);
     225             : }
     226             : 
     227             : /*******************************************************************/
     228             : /*                                                                 */
     229             : /*                         RgX                                     */
     230             : /*                                                                 */
     231             : /*******************************************************************/
     232             : 
     233             : long
     234    24744366 : RgX_equal(GEN x, GEN y)
     235             : {
     236    24744366 :   long i = lg(x);
     237             : 
     238    24744366 :   if (i != lg(y)) return 0;
     239   106462046 :   for (i--; i > 1; i--)
     240    82034243 :     if (!gequal(gel(x,i),gel(y,i))) return 0;
     241    24427803 :   return 1;
     242             : }
     243             : 
     244             : /* Returns 1 in the base ring over which x is defined */
     245             : /* HACK: this also works for t_SER */
     246             : GEN
     247   156673899 : Rg_get_1(GEN x)
     248             : {
     249             :   GEN p, T;
     250   156673899 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     251   156673899 :   if (RgX_type_is_composite(tx))
     252    11628685 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     253   156673899 :   switch(tx)
     254             :   {
     255      795550 :     case t_INTMOD: retmkintmod(is_pm1(p)? gen_0: gen_1, icopy(p));
     256         973 :     case t_PADIC: return cvtop(gen_1, p, lx);
     257        5516 :     case t_FFELT: return FF_1(T);
     258   155871860 :     default: return gen_1;
     259             :   }
     260             : }
     261             : /* Returns 0 in the base ring over which x is defined */
     262             : /* HACK: this also works for t_SER */
     263             : GEN
     264     6985412 : Rg_get_0(GEN x)
     265             : {
     266             :   GEN p, T;
     267     6985412 :   long i, lx, tx = Rg_type(x, &p, &T, &lx);
     268     6985412 :   if (RgX_type_is_composite(tx))
     269       57274 :     RgX_type_decode(tx, &i /*junk*/, &tx);
     270     6985412 :   switch(tx)
     271             :   {
     272         532 :     case t_INTMOD: retmkintmod(gen_0, icopy(p));
     273          42 :     case t_PADIC: return zeropadic(p, lx);
     274         231 :     case t_FFELT: return FF_zero(T);
     275     6984607 :     default: return gen_0;
     276             :   }
     277             : }
     278             : 
     279             : GEN
     280        6286 : QX_ZXQV_eval(GEN P, GEN V, GEN dV)
     281             : {
     282        6286 :   long i, n = degpol(P);
     283             :   GEN z, dz, dP;
     284        6286 :   if (n < 0) return gen_0;
     285        6286 :   P = Q_remove_denom(P, &dP);
     286        6286 :   z = gel(P,2); if (n == 0) return icopy(z);
     287        3619 :   if (dV) z = mulii(dV, z); /* V[1] = dV */
     288        3619 :   z = ZX_Z_add_shallow(ZX_Z_mul(gel(V,2),gel(P,3)), z);
     289        7210 :   for (i=2; i<=n; i++) z = ZX_add(ZX_Z_mul(gel(V,i+1),gel(P,2+i)), z);
     290        3619 :   dz = mul_denom(dP, dV);
     291        3619 :   return dz? RgX_Rg_div(z, dz): z;
     292             : }
     293             : 
     294             : /* Return P(h * x), not memory clean */
     295             : GEN
     296       19875 : RgX_unscale(GEN P, GEN h)
     297             : {
     298       19875 :   long i, l = lg(P);
     299       19875 :   GEN hi = gen_1, Q = cgetg(l, t_POL);
     300       19875 :   Q[1] = P[1];
     301       19875 :   if (l == 2) return Q;
     302       19847 :   gel(Q,2) = gcopy(gel(P,2));
     303       42524 :   for (i=3; i<l; i++)
     304             :   {
     305       22677 :     hi = gmul(hi,h);
     306       22677 :     gel(Q,i) = gmul(gel(P,i), hi);
     307             :   }
     308       19847 :   return Q;
     309             : }
     310             : /* P a ZX, Return P(h * x), not memory clean; optimize for h = -1 */
     311             : GEN
     312     1463531 : ZX_z_unscale(GEN P, long h)
     313             : {
     314     1463531 :   long i, l = lg(P);
     315     1463531 :   GEN Q = cgetg(l, t_POL);
     316     1463532 :   Q[1] = P[1];
     317     1463532 :   if (l == 2) return Q;
     318     1374986 :   gel(Q,2) = gel(P,2);
     319     1374986 :   if (l == 3) return Q;
     320     1348680 :   if (h == -1)
     321      236376 :     for (i = 3; i < l; i++)
     322             :     {
     323      195961 :       gel(Q,i) = negi(gel(P,i));
     324      195961 :       if (++i == l) break;
     325      145007 :       gel(Q,i) = gel(P,i);
     326             :     }
     327             :   else
     328             :   {
     329             :     GEN hi;
     330     1257311 :     gel(Q,3) = mulis(gel(P,3), h);
     331     1257311 :     hi = sqrs(h);
     332     5397297 :     for (i = 4; i < l; i++)
     333             :     {
     334     4139986 :       gel(Q,i) = mulii(gel(P,i), hi);
     335     4139984 :       if (i != l-1) hi = mulis(hi,h);
     336             :     }
     337             :   }
     338     1348680 :   return Q;
     339             : }
     340             : /* P a ZX, h a t_INT. Return P(h * x), not memory clean; optimize for h = -1 */
     341             : GEN
     342     1009704 : ZX_unscale(GEN P, GEN h)
     343             : {
     344             :   long i, l;
     345             :   GEN Q, hi;
     346     1009704 :   i = itos_or_0(h); if (i) return ZX_z_unscale(P, i);
     347         881 :   l = lg(P); Q = cgetg(l, t_POL);
     348         881 :   Q[1] = P[1];
     349         881 :   if (l == 2) return Q;
     350         881 :   gel(Q,2) = gel(P,2);
     351         881 :   if (l == 3) return Q;
     352         881 :   hi = h;
     353         881 :   gel(Q,3) = mulii(gel(P,3), hi);
     354        2741 :   for (i = 4; i < l; i++)
     355             :   {
     356        1860 :     hi = mulii(hi,h);
     357        1860 :     gel(Q,i) = mulii(gel(P,i), hi);
     358             :   }
     359         881 :   return Q;
     360             : }
     361             : /* P a ZX. Return P(x << n), not memory clean */
     362             : GEN
     363      926625 : ZX_unscale2n(GEN P, long n)
     364             : {
     365      926625 :   long i, ni = n, l = lg(P);
     366      926625 :   GEN Q = cgetg(l, t_POL);
     367      926622 :   Q[1] = P[1];
     368      926622 :   if (l == 2) return Q;
     369      926622 :   gel(Q,2) = gel(P,2);
     370      926622 :   if (l == 3) return Q;
     371      926622 :   gel(Q,3) = shifti(gel(P,3), ni);
     372     3168617 :   for (i=4; i<l; i++)
     373             :   {
     374     2242038 :     ni += n;
     375     2242038 :     gel(Q,i) = shifti(gel(P,i), ni);
     376             :   }
     377      926579 :   return Q;
     378             : }
     379             : /* P(h*X) / h, assuming h | P(0), i.e. the result is a ZX */
     380             : GEN
     381       12997 : ZX_unscale_div(GEN P, GEN h)
     382             : {
     383       12997 :   long i, l = lg(P);
     384       12997 :   GEN hi, Q = cgetg(l, t_POL);
     385       12997 :   Q[1] = P[1];
     386       12997 :   if (l == 2) return Q;
     387       12997 :   gel(Q,2) = diviiexact(gel(P,2), h);
     388       12997 :   if (l == 3) return Q;
     389       12997 :   gel(Q,3) = gel(P,3);
     390       12997 :   if (l == 4) return Q;
     391       12997 :   hi = h;
     392       12997 :   gel(Q,4) = mulii(gel(P,4), hi);
     393       64121 :   for (i=5; i<l; i++)
     394             :   {
     395       51124 :     hi = mulii(hi,h);
     396       51124 :     gel(Q,i) = mulii(gel(P,i), hi);
     397             :   }
     398       12997 :   return Q;
     399             : }
     400             : /* P(h*X) / h^k, assuming the result is a ZX */
     401             : GEN
     402         966 : ZX_unscale_divpow(GEN P, GEN h, long k)
     403             : {
     404         966 :   long i, j, l = lg(P);
     405         966 :   GEN H, Q = cgetg(l, t_POL);
     406         966 :   Q[1] = P[1]; if (l == 2) return Q;
     407         966 :   H = gpowers(h, maxss(k, l - 3 - k));
     408        3864 :   for (i = 2, j = k+1; j > 1 && i < l; i++)
     409        2898 :     gel(Q, i) = diviiexact(gel(P, i), gel(H, j--));
     410         966 :   if (i == l) return Q;
     411         966 :   gel(Q, i) = gel(P, i); i++;
     412        3542 :   for (j = 2; i < l; i++) gel(Q, i) = mulii(gel(P, i), gel(H, j++));
     413         966 :   return Q;
     414             : }
     415             : 
     416             : GEN
     417        5845 : RgXV_unscale(GEN x, GEN h)
     418             : {
     419        5845 :   if (isint1(h)) return gcopy(x);
     420       16745 :   pari_APPLY_same(RgX_unscale(gel(x,i), h));
     421             : }
     422             : 
     423             : /* Return h^degpol(P) P(x / h), not memory clean */
     424             : GEN
     425     4334194 : RgX_rescale(GEN P, GEN h)
     426             : {
     427     4334194 :   long i, l = lg(P);
     428     4334194 :   GEN Q = cgetg(l,t_POL), hi = h;
     429     4334189 :   gel(Q,l-1) = gel(P,l-1);
     430    11404188 :   for (i=l-2; i>=2; i--)
     431             :   {
     432    11401557 :     gel(Q,i) = gmul(gel(P,i), hi);
     433    11401435 :     if (i == 2) break;
     434     7069687 :     hi = gmul(hi,h);
     435             :   }
     436     4334379 :   Q[1] = P[1]; return Q;
     437             : }
     438             : 
     439             : GEN
     440        2401 : RgXV_rescale(GEN x, GEN h)
     441             : {
     442        2401 :   if (isint1(h)) return RgX_copy(x);
     443       16086 :   pari_APPLY_same(RgX_rescale(gel(x,i), h));
     444             : }
     445             : 
     446             : /* A(X^d) --> A(X) */
     447             : GEN
     448      984838 : RgX_deflate(GEN x0, long d)
     449             : {
     450             :   GEN z, y, x;
     451      984838 :   long i,id, dy, dx = degpol(x0);
     452      984836 :   if (d == 1 || dx <= 0) return leafcopy(x0);
     453      375012 :   dy = dx/d;
     454      375012 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     455      375015 :   z = y + 2;
     456      375015 :   x = x0+ 2;
     457     1509449 :   for (i=id=0; i<=dy; i++,id+=d) gel(z,i) = gel(x,id);
     458      375015 :   return y;
     459             : }
     460             : 
     461             : GEN
     462       18732 : RgX_homogenize(GEN P, long v)
     463             : {
     464             :   long i, l, d;
     465       18732 :   GEN Q = cgetg_copy(P, &l);
     466       18732 :   Q[1] = P[1]; d = l-3;
     467      165998 :   for (i = 2; i < l; i++) gel(Q,i) = monomial(gel(P,i), d--, v);
     468       18732 :   return Q;
     469             : }
     470             : 
     471             : /* F a t_RFRAC */
     472             : long
     473         105 : rfrac_deflate_order(GEN F)
     474             : {
     475         105 :   GEN N = gel(F,1), D = gel(F,2);
     476         105 :   long m = (degpol(D) <= 0)? 0: RgX_deflate_order(D);
     477         105 :   if (m == 1) return 1;
     478          42 :   if (typ(N) == t_POL && varn(N) == varn(D))
     479          28 :     m = cgcd(m, RgX_deflate_order(N));
     480          42 :   return m;
     481             : }
     482             : /* F a t_RFRAC */
     483             : GEN
     484         105 : rfrac_deflate_max(GEN F, long *m)
     485             : {
     486         105 :   *m = rfrac_deflate_order(F);
     487         105 :   return rfrac_deflate(F, *m);
     488             : }
     489             : /* F a t_RFRAC */
     490             : GEN
     491         105 : rfrac_deflate(GEN F, long m)
     492             : {
     493         105 :   GEN N = gel(F,1), D = gel(F,2);
     494         105 :   if (m == 1) return F;
     495          42 :   if (typ(N) == t_POL && varn(N) == varn(D)) N = RgX_deflate(N, m);
     496          42 :   D = RgX_deflate(D, m); return mkrfrac(N, D);
     497             : }
     498             : 
     499             : /* return x0(X^d) */
     500             : GEN
     501      798459 : RgX_inflate(GEN x0, long d)
     502             : {
     503      798459 :   long i, id, dy, dx = degpol(x0);
     504      798459 :   GEN x = x0 + 2, z, y;
     505      798459 :   if (dx <= 0) return leafcopy(x0);
     506      790843 :   dy = dx*d;
     507      790843 :   y = cgetg(dy+3, t_POL); y[1] = x0[1];
     508      790843 :   z = y + 2;
     509    27868889 :   for (i=0; i<=dy; i++) gel(z,i) = gen_0;
     510    11417189 :   for (i=id=0; i<=dx; i++,id+=d) gel(z,id) = gel(x,i);
     511      790843 :   return y;
     512             : }
     513             : 
     514             : /* return P(X + c) using destructive Horner, optimize for c = 1,-1 */
     515             : static GEN
     516     5956806 : RgX_translate_basecase(GEN P, GEN c)
     517             : {
     518     5956806 :   pari_sp av = avma;
     519             :   GEN Q, R;
     520             :   long i, k, n;
     521             : 
     522     5956806 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     523     5955283 :   Q = leafcopy(P);
     524     5955282 :   R = Q+2; n = degpol(P);
     525     5955281 :   if (isint1(c))
     526             :   {
     527       26402 :     for (i=1; i<=n; i++)
     528             :     {
     529      135182 :       for (k=n-i; k<n; k++) gel(R,k) = gadd(gel(R,k), gel(R,k+1));
     530       22336 :       if (gc_needed(av,2))
     531             :       {
     532           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"RgX_translate(1), i = %ld/%ld", i,n);
     533           0 :         Q = gerepilecopy(av, Q); R = Q+2;
     534             :       }
     535             :     }
     536             :   }
     537     5951214 :   else if (isintm1(c))
     538             :   {
     539      901571 :     for (i=1; i<=n; i++)
     540             :     {
     541     2855594 :       for (k=n-i; k<n; k++) gel(R,k) = gsub(gel(R,k), gel(R,k+1));
     542      683918 :       if (gc_needed(av,2))
     543             :       {
     544           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"RgX_translate(-1), i = %ld/%ld", i,n);
     545           0 :         Q = gerepilecopy(av, Q); R = Q+2;
     546             :       }
     547             :     }
     548             :   }
     549             :   else
     550             :   {
     551    19600432 :     for (i=1; i<=n; i++)
     552             :     {
     553    46009381 :       for (k=n-i; k<n; k++) gel(R,k) = gadd(gel(R,k), gmul(c, gel(R,k+1)));
     554    13866869 :       if (gc_needed(av,2))
     555             :       {
     556           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"RgX_translate, i = %ld/%ld", i,n);
     557           0 :         Q = gerepilecopy(av, Q); R = Q+2;
     558             :       }
     559             :     }
     560             :   }
     561     5954924 :   return gerepilecopy(av, Q);
     562             : }
     563             : GEN
     564     5957233 : RgX_translate(GEN P, GEN c)
     565             : {
     566     5957233 :   pari_sp av = avma;
     567     5957233 :   long n = degpol(P);
     568     5957225 :   if (n < 40)
     569     5956805 :     return RgX_translate_basecase(P, c);
     570             :   else
     571             :   {
     572         420 :     long d = n >> 1;
     573         420 :     GEN Q = RgX_translate(RgX_shift_shallow(P, -d), c);
     574         420 :     GEN R = RgX_translate(RgXn_red_shallow(P, d), c);
     575         420 :     GEN S = gpowgs(deg1pol_shallow(gen_1, c, varn(P)), d);
     576         420 :     return gerepileupto(av, RgX_add(RgX_mul(Q, S), R));
     577             :   }
     578             : }
     579             : 
     580             : /* P(ax + b) */
     581             : GEN
     582           0 : RgX_affine(GEN P, GEN a, GEN b)
     583             : {
     584           0 :   if (signe(b)) P = RgX_translate(P, b);
     585           0 :   return RgX_unscale(P, a);
     586             : }
     587             : 
     588             : /* return lift( P(X + c) ) using Horner, c in R[y]/(T) */
     589             : GEN
     590       27529 : RgXQX_translate(GEN P, GEN c, GEN T)
     591             : {
     592       27529 :   pari_sp av = avma;
     593             :   GEN Q, R;
     594             :   long i, k, n;
     595             : 
     596       27529 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     597       27186 :   Q = leafcopy(P);
     598       27186 :   R = Q+2; n = degpol(P);
     599       89759 :   for (i=1; i<=n; i++)
     600             :   {
     601      270605 :     for (k=n-i; k<n; k++)
     602             :     {
     603      208032 :       pari_sp av2 = avma;
     604      208032 :       gel(R,k) = gerepileupto(av2,
     605      208032 :                    RgX_rem(gadd(gel(R,k), gmul(c, gel(R,k+1))), T));
     606             :     }
     607       62573 :     if (gc_needed(av,2))
     608             :     {
     609           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXQX_translate, i = %ld/%ld", i,n);
     610           0 :       Q = gerepilecopy(av, Q); R = Q+2;
     611             :     }
     612             :   }
     613       27186 :   return gerepilecopy(av, Q);
     614             : }
     615             : 
     616             : /********************************************************************/
     617             : /**                                                                **/
     618             : /**                          CONVERSIONS                           **/
     619             : /**                       (not memory clean)                       **/
     620             : /**                                                                **/
     621             : /********************************************************************/
     622             : /* to INT / FRAC / (POLMOD mod T), not memory clean because T not copied,
     623             :  * but everything else is */
     624             : static GEN
     625      167394 : QXQ_to_mod(GEN x, GEN T)
     626             : {
     627             :   long d;
     628      167394 :   switch(typ(x))
     629             :   {
     630       62577 :     case t_INT:  return icopy(x);
     631        2079 :     case t_FRAC: return gcopy(x);
     632      102740 :     case t_POL:
     633      102740 :       d = degpol(x);
     634      102740 :       if (d < 0) return gen_0;
     635      100472 :       if (d == 0) return gcopy(gel(x,2));
     636       98333 :       return mkpolmod(RgX_copy(x), T);
     637           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     638             :              return NULL;/* LCOV_EXCL_LINE */
     639             :   }
     640             : }
     641             : /* pure shallow version */
     642             : GEN
     643      780714 : QXQ_to_mod_shallow(GEN x, GEN T)
     644             : {
     645             :   long d;
     646      780714 :   switch(typ(x))
     647             :   {
     648      501244 :     case t_INT:
     649      501244 :     case t_FRAC: return x;
     650      279470 :     case t_POL:
     651      279470 :       d = degpol(x);
     652      279470 :       if (d < 0) return gen_0;
     653      232548 :       if (d == 0) return gel(x,2);
     654      216018 :       return mkpolmod(x, T);
     655           0 :     default: pari_err_TYPE("QXQ_to_mod",x);
     656             :              return NULL;/* LCOV_EXCL_LINE */
     657             :   }
     658             : }
     659             : /* T a ZX, z lifted from (Q[Y]/(T(Y)))[X], apply QXQ_to_mod to all coeffs.
     660             :  * Not memory clean because T not copied, but everything else is */
     661             : static GEN
     662       37366 : QXQX_to_mod(GEN z, GEN T)
     663             : {
     664       37366 :   long i,l = lg(z);
     665       37366 :   GEN x = cgetg(l,t_POL);
     666      186546 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod(gel(z,i), T);
     667       37368 :   x[1] = z[1]; return normalizepol_lg(x,l);
     668             : }
     669             : /* pure shallow version */
     670             : GEN
     671      181283 : QXQX_to_mod_shallow(GEN z, GEN T)
     672             : {
     673      181283 :   long i,l = lg(z);
     674      181283 :   GEN x = cgetg(l,t_POL);
     675      879397 :   for (i=2; i<l; i++) gel(x,i) = QXQ_to_mod_shallow(gel(z,i), T);
     676      181283 :   x[1] = z[1]; return normalizepol_lg(x,l);
     677             : }
     678             : /* Apply QXQX_to_mod to all entries. Memory-clean ! */
     679             : GEN
     680       12586 : QXQXV_to_mod(GEN V, GEN T)
     681             : {
     682       12586 :   long i, l = lg(V);
     683       12586 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     684       49952 :   for (i=1;i<l; i++) gel(z,i) = QXQX_to_mod(gel(V,i), T);
     685       12586 :   return z;
     686             : }
     687             : /* Apply QXQ_to_mod to all entries. Memory-clean ! */
     688             : GEN
     689       18233 : QXQV_to_mod(GEN V, GEN T)
     690             : {
     691       18233 :   long i, l = lg(V);
     692       18233 :   GEN z = cgetg(l, t_VEC); T = ZX_copy(T);
     693       36447 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod(gel(V,i), T);
     694       18233 :   return z;
     695             : }
     696             : 
     697             : /* Apply QXQ_to_mod to all entries. Memory-clean ! */
     698             : GEN
     699       14336 : QXQC_to_mod_shallow(GEN V, GEN T)
     700             : {
     701       14336 :   long i, l = lg(V);
     702       14336 :   GEN z = cgetg(l, t_COL);
     703       96936 :   for (i=1;i<l; i++) gel(z,i) = QXQ_to_mod_shallow(gel(V,i), T);
     704       14336 :   return z;
     705             : }
     706             : 
     707             : GEN
     708        6573 : QXQM_to_mod_shallow(GEN V, GEN T)
     709             : {
     710        6573 :   long i, l = lg(V);
     711        6573 :   GEN z = cgetg(l, t_MAT);
     712       20909 :   for (i=1; i<l; i++) gel(z,i) = QXQC_to_mod_shallow(gel(V,i), T);
     713        6573 :   return z;
     714             : }
     715             : 
     716             : GEN
     717     6164222 : RgX_renormalize_lg(GEN x, long lx)
     718             : {
     719             :   long i;
     720     9187931 :   for (i = lx-1; i>1; i--)
     721     8735726 :     if (! gequal0(gel(x,i))) break; /* _not_ isexactzero */
     722     6164222 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
     723     6164222 :   setlg(x, i+1); setsigne(x, i != 1); return x;
     724             : }
     725             : 
     726             : GEN
     727     1502113 : RgV_to_RgX(GEN x, long v)
     728             : {
     729     1502113 :   long i, k = lg(x);
     730             :   GEN p;
     731             : 
     732     4173325 :   while (--k && gequal0(gel(x,k)));
     733     1502110 :   if (!k) return pol_0(v);
     734     1494839 :   i = k+2; p = cgetg(i,t_POL);
     735     1494840 :   p[1] = evalsigne(1) | evalvarn(v);
     736    10278010 :   x--; for (k=2; k<i; k++) gel(p,k) = gel(x,k);
     737     1494840 :   return p;
     738             : }
     739             : GEN
     740      190005 : RgV_to_RgX_reverse(GEN x, long v)
     741             : {
     742      190005 :   long j, k, l = lg(x);
     743             :   GEN p;
     744             : 
     745      191573 :   for (k = 1; k < l; k++)
     746      191573 :     if (!gequal0(gel(x,k))) break;
     747      190005 :   if (k == l) return pol_0(v);
     748      190005 :   k -= 1;
     749      190005 :   l -= k;
     750      190005 :   x += k;
     751      190005 :   p = cgetg(l+1,t_POL);
     752      190005 :   p[1] = evalsigne(1) | evalvarn(v);
     753      999045 :   for (j=2, k=l; j<=l; j++) gel(p,j) = gel(x,--k);
     754      190005 :   return p;
     755             : }
     756             : 
     757             : /* return the (N-dimensional) vector of coeffs of p */
     758             : GEN
     759    13343421 : RgX_to_RgC(GEN x, long N)
     760             : {
     761             :   long i, l;
     762             :   GEN z;
     763    13343421 :   l = lg(x)-1; x++;
     764    13343421 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     765    13343421 :   z = cgetg(N+1,t_COL);
     766    80554016 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     767    24351233 :   for (   ; i<=N; i++) gel(z,i) = gen_0;
     768    13343556 :   return z;
     769             : }
     770             : GEN
     771     1349239 : Rg_to_RgC(GEN x, long N)
     772             : {
     773     1349239 :   return (typ(x) == t_POL)? RgX_to_RgC(x,N): scalarcol_shallow(x, N);
     774             : }
     775             : 
     776             : /* vector of polynomials (in v) whose coefs are given by the columns of x */
     777             : GEN
     778      302889 : RgM_to_RgXV(GEN x, long v)
     779     1292896 : { pari_APPLY_type(t_VEC, RgV_to_RgX(gel(x,i), v)) }
     780             : GEN
     781        7202 : RgM_to_RgXV_reverse(GEN x, long v)
     782       28808 : { pari_APPLY_type(t_VEC, RgV_to_RgX_reverse(gel(x,i), v)) }
     783             : 
     784             : /* matrix whose entries are given by the coeffs of the polynomials in
     785             :  * vector v (considered as degree n-1 polynomials) */
     786             : GEN
     787      334802 : RgV_to_RgM(GEN x, long n)
     788     1681549 : { pari_APPLY_type(t_MAT, Rg_to_RgC(gel(x,i), n)) }
     789             : 
     790             : GEN
     791       77925 : RgXV_to_RgM(GEN x, long n)
     792      394801 : { pari_APPLY_type(t_MAT, RgX_to_RgC(gel(x,i), n)) }
     793             : 
     794             : /* polynomial (in v) of polynomials (in w) whose coeffs are given by the columns of x */
     795             : GEN
     796       23466 : RgM_to_RgXX(GEN x, long v,long w)
     797             : {
     798       23466 :   long j, lx = lg(x);
     799       23466 :   GEN y = cgetg(lx+1, t_POL);
     800       23466 :   y[1] = evalsigne(1) | evalvarn(v);
     801       23466 :   y++;
     802      130418 :   for (j=1; j<lx; j++) gel(y,j) = RgV_to_RgX(gel(x,j), w);
     803       23466 :   return normalizepol_lg(--y, lx+1);
     804             : }
     805             : 
     806             : /* matrix whose entries are given by the coeffs of the polynomial v in
     807             :  * two variables (considered as degree n-1 polynomials) */
     808             : GEN
     809         322 : RgXX_to_RgM(GEN v, long n)
     810             : {
     811         322 :   long j, N = lg(v)-1;
     812         322 :   GEN y = cgetg(N, t_MAT);
     813        1043 :   for (j=1; j<N; j++) gel(y,j) = Rg_to_RgC(gel(v,j+1), n);
     814         322 :   return y;
     815             : }
     816             : 
     817             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     818             : GEN
     819       31300 : RgXY_swapspec(GEN x, long n, long w, long nx)
     820             : {
     821       31300 :   long j, ly = n+3;
     822       31300 :   GEN y = cgetg(ly, t_POL);
     823       31300 :   y[1] = evalsigne(1);
     824      384928 :   for (j=2; j<ly; j++)
     825             :   {
     826             :     long k;
     827      353628 :     GEN a = cgetg(nx+2,t_POL);
     828      353628 :     a[1] = evalsigne(1) | evalvarn(w);
     829     1809573 :     for (k=0; k<nx; k++)
     830             :     {
     831     1455945 :       GEN xk = gel(x,k);
     832     1455945 :       if (typ(xk)==t_POL && varn(xk)==w)
     833     1367429 :         gel(a,k+2) = j<lg(xk)? gel(xk,j): gen_0;
     834             :       else
     835       88516 :         gel(a,k+2) = j==2 ? xk: gen_0;
     836             :     }
     837      353628 :     gel(y,j) = normalizepol_lg(a, nx+2);
     838             :   }
     839       31300 :   return normalizepol_lg(y,ly);
     840             : }
     841             : 
     842             : /* P(X,Y) --> P(Y,X), n is an upper bound for deg_Y(P) */
     843             : GEN
     844         952 : RgXY_swap(GEN x, long n, long w)
     845             : {
     846         952 :   GEN z = RgXY_swapspec(x+2, n, w, lgpol(x));
     847         952 :   setvarn(z, varn(x)); return z;
     848             : }
     849             : 
     850             : long
     851         288 : RgXY_degreex(GEN b)
     852             : {
     853         288 :   long deg = 0, i;
     854         288 :   if (!signe(b)) return -1;
     855        1396 :   for (i = 2; i < lg(b); ++i)
     856             :   {
     857        1108 :     GEN bi = gel(b, i);
     858        1108 :     if (typ(bi) == t_POL)
     859        1030 :       deg = maxss(deg, degpol(bi));
     860             :   }
     861         288 :   return deg;
     862             : }
     863             : 
     864             : GEN
     865       38738 : RgXY_derivx(GEN x) { pari_APPLY_pol(RgX_deriv(gel(x,i))); }
     866             : 
     867             : /* return (x % X^n). Shallow */
     868             : GEN
     869     7923957 : RgXn_red_shallow(GEN a, long n)
     870             : {
     871     7923957 :   long i, L = n+2, l = lg(a);
     872             :   GEN  b;
     873     7923957 :   if (L >= l) return a; /* deg(x) < n */
     874     5735120 :   b = cgetg(L, t_POL); b[1] = a[1];
     875    36587487 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
     876     5735120 :   return normalizepol_lg(b,L);
     877             : }
     878             : 
     879             : GEN
     880         483 : RgXnV_red_shallow(GEN x, long n)
     881        2268 : { pari_APPLY_type(t_VEC, RgXn_red_shallow(gel(x,i), n)) }
     882             : 
     883             : /* return (x * X^n). Shallow */
     884             : GEN
     885   175341819 : RgX_shift_shallow(GEN a, long n)
     886             : {
     887   175341819 :   long i, l = lg(a);
     888             :   GEN  b;
     889   175341819 :   if (l == 2 || !n) return a;
     890   110343036 :   l += n;
     891   110343036 :   if (n < 0)
     892             :   {
     893    55493548 :     if (l <= 2) return pol_0(varn(a));
     894    54022013 :     b = cgetg(l, t_POL); b[1] = a[1];
     895    54022723 :     a -= n;
     896   165322889 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     897             :   } else {
     898    54849488 :     b = cgetg(l, t_POL); b[1] = a[1];
     899    54854466 :     a -= n; n += 2;
     900   119044261 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     901   211094120 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     902             :   }
     903   108877189 :   return b;
     904             : }
     905             : /* return (x * X^n). */
     906             : GEN
     907     1567515 : RgX_shift(GEN a, long n)
     908             : {
     909     1567515 :   long i, l = lg(a);
     910             :   GEN  b;
     911     1567515 :   if (l == 2 || !n) return RgX_copy(a);
     912     1567151 :   l += n;
     913     1567151 :   if (n < 0)
     914             :   {
     915         847 :     if (l <= 2) return pol_0(varn(a));
     916         777 :     b = cgetg(l, t_POL); b[1] = a[1];
     917         777 :     a -= n;
     918        2947 :     for (i=2; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     919             :   } else {
     920     1566304 :     b = cgetg(l, t_POL); b[1] = a[1];
     921     1566304 :     a -= n; n += 2;
     922     3915621 :     for (i=2; i<n; i++) gel(b,i) = gen_0;
     923     4319721 :     for (   ; i<l; i++) gel(b,i) = gcopy(gel(a,i));
     924             :   }
     925     1567081 :   return b;
     926             : }
     927             : 
     928             : GEN
     929      317037 : RgX_rotate_shallow(GEN P, long k, long p)
     930             : {
     931      317037 :   long i, l = lgpol(P);
     932             :   GEN r;
     933      317037 :   if (signe(P)==0)
     934        1365 :     return pol_0(varn(P));
     935      315672 :   r = cgetg(p+2,t_POL); r[1] = P[1];
     936     2100644 :   for(i=0; i<p; i++)
     937             :   {
     938     1784972 :     long s = 2+(i+k)%p;
     939     1784972 :     gel(r,s) = i<l? gel(P,2+i): gen_0;
     940             :   }
     941      315672 :   return RgX_renormalize(r);
     942             : }
     943             : 
     944             : GEN
     945     2984952 : RgX_mulXn(GEN x, long d)
     946             : {
     947             :   pari_sp av;
     948             :   GEN z;
     949             :   long v;
     950     2984952 :   if (d >= 0) return RgX_shift(x, d);
     951     1472328 :   d = -d;
     952     1472328 :   v = RgX_val(x);
     953     1472328 :   if (v >= d) return RgX_shift(x, -d);
     954     1472321 :   av = avma;
     955     1472321 :   z = gred_rfrac_simple(RgX_shift_shallow(x, -v), pol_xn(d - v, varn(x)));
     956     1472321 :   return gerepileupto(av, z);
     957             : }
     958             : 
     959             : long
     960         588 : RgXV_maxdegree(GEN x)
     961             : {
     962         588 :   long d = -1, i, l = lg(x);
     963        4494 :   for (i = 1; i < l; i++)
     964        3906 :     d = maxss(d, degpol(gel(x,i)));
     965         588 :   return d;
     966             : }
     967             : 
     968             : long
     969     3647339 : RgX_val(GEN x)
     970             : {
     971     3647339 :   long i, lx = lg(x);
     972     3647339 :   if (lx == 2) return LONG_MAX;
     973     4577296 :   for (i = 2; i < lx; i++)
     974     4577240 :     if (!isexactzero(gel(x,i))) break;
     975     3647178 :   if (i == lx) return LONG_MAX;/* possible with nonrational zeros */
     976     3647122 :   return i - 2;
     977             : }
     978             : long
     979    82396604 : RgX_valrem(GEN x, GEN *Z)
     980             : {
     981    82396604 :   long v, i, lx = lg(x);
     982    82396604 :   if (lx == 2) { *Z = pol_0(varn(x)); return LONG_MAX; }
     983   128189482 :   for (i = 2; i < lx; i++)
     984   128191488 :     if (!isexactzero(gel(x,i))) break;
     985             :   /* possible with nonrational zeros */
     986    82397789 :   if (i == lx)
     987             :   {
     988          14 :     *Z = scalarpol_shallow(Rg_get_0(x), varn(x));
     989          14 :     return LONG_MAX;
     990             :   }
     991    82397775 :   v = i - 2;
     992    82397775 :   *Z = RgX_shift_shallow(x, -v);
     993    82411882 :   return v;
     994             : }
     995             : long
     996      851763 : RgX_valrem_inexact(GEN x, GEN *Z)
     997             : {
     998             :   long v;
     999      851763 :   if (!signe(x)) { if (Z) *Z = pol_0(varn(x)); return LONG_MAX; }
    1000      868743 :   for (v = 0;; v++)
    1001      868743 :     if (!gequal0(gel(x,2+v))) break;
    1002      851756 :   if (Z) *Z = RgX_shift_shallow(x, -v);
    1003      851756 :   return v;
    1004             : }
    1005             : 
    1006             : GEN
    1007       67067 : RgXQC_red(GEN x, GEN T)
    1008      409479 : { pari_APPLY_type(t_COL, grem(gel(x,i), T)) }
    1009             : 
    1010             : GEN
    1011        1211 : RgXQV_red(GEN x, GEN T)
    1012       27832 : { pari_APPLY_type(t_VEC, grem(gel(x,i), T)) }
    1013             : 
    1014             : GEN
    1015       12901 : RgXQM_red(GEN x, GEN T)
    1016       79968 : { pari_APPLY_same(RgXQC_red(gel(x,i), T)) }
    1017             : 
    1018             : GEN
    1019         322 : RgXQM_mul(GEN P, GEN Q, GEN T)
    1020             : {
    1021         322 :   return RgXQM_red(RgM_mul(P, Q), T);
    1022             : }
    1023             : 
    1024             : GEN
    1025      482701 : RgXQX_red(GEN P, GEN T)
    1026             : {
    1027      482701 :   long i, l = lg(P);
    1028      482701 :   GEN Q = cgetg(l, t_POL);
    1029      482701 :   Q[1] = P[1];
    1030     2546359 :   for (i=2; i<l; i++) gel(Q,i) = grem(gel(P,i), T);
    1031      482701 :   return normalizepol_lg(Q, l);
    1032             : }
    1033             : 
    1034             : GEN
    1035      823324 : RgX_deriv(GEN x)
    1036             : {
    1037      823324 :   long i,lx = lg(x)-1;
    1038             :   GEN y;
    1039             : 
    1040      823324 :   if (lx<3) return pol_0(varn(x));
    1041      820237 :   y = cgetg(lx,t_POL); gel(y,2) = gcopy(gel(x,3));
    1042     3633406 :   for (i=3; i<lx ; i++) gel(y,i) = gmulsg(i-1,gel(x,i+1));
    1043      820233 :   y[1] = x[1]; return normalizepol_lg(y,i);
    1044             : }
    1045             : 
    1046             : GEN
    1047     1379226 : RgX_recipspec_shallow(GEN x, long l, long n)
    1048             : {
    1049             :   long i;
    1050     1379226 :   GEN z = cgetg(n+2,t_POL);
    1051     1379232 :   z[1] = 0; z += 2;
    1052    36082802 :   for(i=0; i<l; i++) gel(z,n-i-1) = gel(x,i);
    1053     1602483 :   for(   ; i<n; i++) gel(z, n-i-1) = gen_0;
    1054     1379232 :   return normalizepol_lg(z-2,n+2);
    1055             : }
    1056             : 
    1057             : GEN
    1058      639065 : RgXn_recip_shallow(GEN P, long n)
    1059             : {
    1060      639065 :   GEN Q = RgX_recipspec_shallow(P+2, lgpol(P), n);
    1061      639073 :   setvarn(Q, varn(P));
    1062      639073 :   return Q;
    1063             : }
    1064             : 
    1065             : /* return coefficients s.t x = x_0 X^n + ... + x_n */
    1066             : GEN
    1067       31248 : RgX_recip(GEN x)
    1068             : {
    1069             :   long lx, i, j;
    1070       31248 :   GEN y = cgetg_copy(x, &lx);
    1071      274029 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gcopy(gel(x,j));
    1072       31248 :   return normalizepol_lg(y,lx);
    1073             : }
    1074             : /* shallow version */
    1075             : GEN
    1076       59367 : RgX_recip_shallow(GEN x)
    1077             : {
    1078             :   long lx, i, j;
    1079       59367 :   GEN y = cgetg_copy(x, &lx);
    1080      356041 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
    1081       59367 :   return normalizepol_lg(y,lx);
    1082             : }
    1083             : 
    1084             : GEN
    1085     5110441 : RgX_recip_i(GEN x)
    1086             : {
    1087             :   long lx, i, j;
    1088     5110441 :   GEN y = cgetg_copy(x, &lx);
    1089    25926792 :   y[1] = x[1]; for (i=2,j=lx-1; i<lx; i++,j--) gel(y,i) = gel(x,j);
    1090     5110434 :   return y;
    1091             : }
    1092             : /*******************************************************************/
    1093             : /*                                                                 */
    1094             : /*                      ADDITION / SUBTRACTION                     */
    1095             : /*                                                                 */
    1096             : /*******************************************************************/
    1097             : /* same variable */
    1098             : GEN
    1099    82455614 : RgX_add(GEN x, GEN y)
    1100             : {
    1101    82455614 :   long i, lx = lg(x), ly = lg(y);
    1102             :   GEN z;
    1103    82455614 :   if (ly <= lx) {
    1104    70067346 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1105   279886847 :     for (i=2; i < ly; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1106   123414803 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1107    70044739 :     z = normalizepol_lg(z, lx);
    1108             :   } else {
    1109    12388268 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1110    49079409 :     for (i=2; i < lx; i++) gel(z,i) = gadd(gel(x,i),gel(y,i));
    1111    36416391 :     for (   ; i < ly; i++) gel(z,i) = gcopy(gel(y,i));
    1112    12389915 :     z = normalizepol_lg(z, ly);
    1113             :   }
    1114    82448368 :   return z;
    1115             : }
    1116             : GEN
    1117    63713831 : RgX_sub(GEN x, GEN y)
    1118             : {
    1119    63713831 :   long i, lx = lg(x), ly = lg(y);
    1120             :   GEN z;
    1121    63713831 :   if (ly <= lx) {
    1122    29990378 :     z = cgetg(lx,t_POL); z[1] = x[1];
    1123   157166003 :     for (i=2; i < ly; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1124    56353866 :     for (   ; i < lx; i++) gel(z,i) = gcopy(gel(x,i));
    1125    29970487 :     z = normalizepol_lg(z, lx);
    1126             :   } else {
    1127    33723453 :     z = cgetg(ly,t_POL); z[1] = y[1];
    1128   125453532 :     for (i=2; i < lx; i++) gel(z,i) = gsub(gel(x,i),gel(y,i));
    1129    74659194 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    1130    33694458 :     z = normalizepol_lg(z, ly);
    1131             :   }
    1132    63712057 :   return z;
    1133             : }
    1134             : GEN
    1135     7773798 : RgX_neg(GEN x)
    1136    49900741 : { pari_APPLY_pol_normalized(gneg(gel(x,i))); }
    1137             : 
    1138             : GEN
    1139    22049301 : RgX_Rg_add(GEN y, GEN x)
    1140             : {
    1141             :   GEN z;
    1142    22049301 :   long lz = lg(y), i;
    1143    22049301 :   if (lz == 2) return scalarpol(x,varn(y));
    1144    19000938 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1145    19000886 :   gel(z,2) = gadd(gel(y,2),x);
    1146    69200410 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1147             :   /* probably useless unless lz = 3, but cannot be skipped if y is
    1148             :    * an inexact 0 */
    1149    19000851 :   return normalizepol_lg(z,lz);
    1150             : }
    1151             : GEN
    1152       64886 : RgX_Rg_add_shallow(GEN y, GEN x)
    1153             : {
    1154             :   GEN z;
    1155       64886 :   long lz = lg(y), i;
    1156       64886 :   if (lz == 2) return scalarpol(x,varn(y));
    1157       64886 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1158       64886 :   gel(z,2) = gadd(gel(y,2),x);
    1159      129930 :   for(i=3; i<lz; i++) gel(z,i) = gel(y,i);
    1160       64885 :   return normalizepol_lg(z,lz);
    1161             : }
    1162             : GEN
    1163      180057 : RgX_Rg_sub(GEN y, GEN x)
    1164             : {
    1165             :   GEN z;
    1166      180057 :   long lz = lg(y), i;
    1167      180057 :   if (lz == 2)
    1168             :   { /* scalarpol(gneg(x),varn(y)) optimized */
    1169        2653 :     long v = varn(y);
    1170        2653 :     if (isrationalzero(x)) return pol_0(v);
    1171          49 :     z = cgetg(3,t_POL);
    1172          49 :     z[1] = gequal0(x)? evalvarn(v)
    1173          49 :                    : evalvarn(v) | evalsigne(1);
    1174          49 :     gel(z,2) = gneg(x); return z;
    1175             :   }
    1176      177404 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1177      177404 :   gel(z,2) = gsub(gel(y,2),x);
    1178      466730 :   for(i=3; i<lz; i++) gel(z,i) = gcopy(gel(y,i));
    1179      177404 :   return normalizepol_lg(z,lz);
    1180             : }
    1181             : GEN
    1182     4856115 : Rg_RgX_sub(GEN x, GEN y)
    1183             : {
    1184             :   GEN z;
    1185     4856115 :   long lz = lg(y), i;
    1186     4856115 :   if (lz == 2) return scalarpol(x,varn(y));
    1187     4854946 :   z = cgetg(lz,t_POL); z[1] = y[1];
    1188     4854939 :   gel(z,2) = gsub(x, gel(y,2));
    1189     7280602 :   for(i=3; i<lz; i++) gel(z,i) = gneg(gel(y,i));
    1190     4854888 :   return normalizepol_lg(z,lz);
    1191             : }
    1192             : /*******************************************************************/
    1193             : /*                                                                 */
    1194             : /*                  KARATSUBA MULTIPLICATION                       */
    1195             : /*                                                                 */
    1196             : /*******************************************************************/
    1197             : #if 0
    1198             : /* to debug Karatsuba-like routines */
    1199             : GEN
    1200             : zx_debug_spec(GEN x, long nx)
    1201             : {
    1202             :   GEN z = cgetg(nx+2,t_POL);
    1203             :   long i;
    1204             :   for (i=0; i<nx; i++) gel(z,i+2) = stoi(x[i]);
    1205             :   z[1] = evalsigne(1); return z;
    1206             : }
    1207             : 
    1208             : GEN
    1209             : RgX_debug_spec(GEN x, long nx)
    1210             : {
    1211             :   GEN z = cgetg(nx+2,t_POL);
    1212             :   long i;
    1213             :   for (i=0; i<nx; i++) z[i+2] = x[i];
    1214             :   z[1] = evalsigne(1); return z;
    1215             : }
    1216             : #endif
    1217             : 
    1218             : /* generic multiplication */
    1219             : GEN
    1220     8885548 : RgX_addspec_shallow(GEN x, GEN y, long nx, long ny)
    1221             : {
    1222             :   GEN z, t;
    1223             :   long i;
    1224     8885548 :   if (nx == ny) {
    1225     1529182 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1226     4933641 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1227     1529175 :     return normalizepol_lg(z, nx+2);
    1228             :   }
    1229     7356366 :   if (ny < nx) {
    1230     7169493 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1231    26391043 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1232    17141714 :     for (   ; i < nx; i++) gel(t,i) = gel(x,i);
    1233     7168979 :     return normalizepol_lg(z, nx+2);
    1234             :   } else {
    1235      186873 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1236     3678916 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1237      452099 :     for (   ; i < ny; i++) gel(t,i) = gel(y,i);
    1238      186885 :     return normalizepol_lg(z, ny+2);
    1239             :   }
    1240             : }
    1241             : GEN
    1242      223572 : RgX_addspec(GEN x, GEN y, long nx, long ny)
    1243             : {
    1244             :   GEN z, t;
    1245             :   long i;
    1246      223572 :   if (nx == ny) {
    1247       12824 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1248     2185778 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1249       12824 :     return normalizepol_lg(z, nx+2);
    1250             :   }
    1251      210748 :   if (ny < nx) {
    1252      208970 :     z = cgetg(nx+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1253     3730328 :     for (i=0; i < ny; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1254     2383608 :     for (   ; i < nx; i++) gel(t,i) = gcopy(gel(x,i));
    1255      208970 :     return normalizepol_lg(z, nx+2);
    1256             :   } else {
    1257        1778 :     z = cgetg(ny+2,t_POL); z[1] = evalsigne(1)|evalvarn(0); t = z+2;
    1258      330904 :     for (i=0; i < nx; i++) gel(t,i) = gadd(gel(x,i),gel(y,i));
    1259       12222 :     for (   ; i < ny; i++) gel(t,i) = gcopy(gel(y,i));
    1260        1778 :     return normalizepol_lg(z, ny+2);
    1261             :   }
    1262             : }
    1263             : 
    1264             : /* Return the vector of coefficients of x, where we replace rational 0s by NULL
    1265             :  * [ to speed up basic operation s += x[i]*y[j] ]. We create a proper
    1266             :  * t_VECSMALL, to hold this, which can be left on stack: gerepile
    1267             :  * will not crash on it. The returned vector itself is not a proper GEN,
    1268             :  * we access the coefficients as x[i], i = 0..deg(x) */
    1269             : static GEN
    1270    56507372 : RgXspec_kill0(GEN x, long lx)
    1271             : {
    1272    56507372 :   GEN z = cgetg(lx+1, t_VECSMALL) + 1; /* inhibit gerepile-wise */
    1273             :   long i;
    1274   191986423 :   for (i=0; i <lx; i++)
    1275             :   {
    1276   135479048 :     GEN c = gel(x,i);
    1277   135479048 :     z[i] = (long)(isrationalzero(c)? NULL: c);
    1278             :   }
    1279    56507375 :   return z;
    1280             : }
    1281             : 
    1282             : INLINE GEN
    1283    94276145 : RgX_mulspec_basecase_limb(GEN x, GEN y, long a, long b)
    1284             : {
    1285    94276145 :   pari_sp av = avma;
    1286    94276145 :   GEN s = NULL;
    1287             :   long i;
    1288             : 
    1289   356953398 :   for (i=a; i<b; i++)
    1290   262686239 :     if (gel(y,i) && gel(x,-i))
    1291             :     {
    1292   184570436 :       GEN t = gmul(gel(y,i), gel(x,-i));
    1293   184565125 :       s = s? gadd(s, t): t;
    1294             :     }
    1295    94267159 :   return s? gerepileupto(av, s): gen_0;
    1296             : }
    1297             : 
    1298             : /* assume nx >= ny > 0, return x * y * t^v */
    1299             : static GEN
    1300    21621267 : RgX_mulspec_basecase(GEN x, GEN y, long nx, long ny, long v)
    1301             : {
    1302             :   long i, lz, nz;
    1303             :   GEN z;
    1304             : 
    1305    21621267 :   x = RgXspec_kill0(x,nx);
    1306    21621221 :   y = RgXspec_kill0(y,ny);
    1307    21621241 :   lz = nx + ny + 1; nz = lz-2;
    1308    21621241 :   lz += v;
    1309    21621241 :   z = cgetg(lz, t_POL) + 2; /* x:y:z [i] = term of degree i */
    1310    33361808 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1311    57285180 :   for (i=0; i<ny; i++)gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0, i+1);
    1312    41350024 :   for (  ; i<nx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ny);
    1313    35664097 :   for (  ; i<nz; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-nx+1,ny);
    1314    21620686 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1315             : }
    1316             : 
    1317             : /* return (x * X^d) + y. Assume d > 0 */
    1318             : GEN
    1319    10037560 : RgX_addmulXn_shallow(GEN x0, GEN y0, long d)
    1320             : {
    1321             :   GEN x, y, xd, yd, zd;
    1322             :   long a, lz, nx, ny;
    1323             : 
    1324    10037560 :   if (!signe(x0)) return y0;
    1325     9403724 :   ny = lgpol(y0);
    1326     9403712 :   nx = lgpol(x0);
    1327     9403839 :   zd = (GEN)avma;
    1328     9403839 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1329     9403839 :   if (a <= 0)
    1330             :   {
    1331     1476725 :     lz = nx+d+2;
    1332     1476725 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1333     3392063 :     while (xd > x) gel(--zd,0) = gel(--xd,0);
    1334     1476726 :     x = zd + a;
    1335     1494427 :     while (zd > x) gel(--zd,0) = gen_0;
    1336             :   }
    1337             :   else
    1338             :   {
    1339     7927114 :     xd = new_chunk(d); yd = y+d;
    1340     7927114 :     x = RgX_addspec_shallow(x,yd, nx,a);
    1341     7927031 :     lz = (a>nx)? ny+2: lg(x)+d;
    1342    39524642 :     x += 2; while (xd > x) *--zd = *--xd;
    1343             :   }
    1344    22910816 :   while (yd > y) *--zd = *--yd;
    1345     9403757 :   *--zd = x0[1];
    1346     9403757 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1347             : }
    1348             : GEN
    1349      516145 : RgX_addmulXn(GEN x0, GEN y0, long d)
    1350             : {
    1351             :   GEN x, y, xd, yd, zd;
    1352             :   long a, lz, nx, ny;
    1353             : 
    1354      516145 :   if (!signe(x0)) return RgX_copy(y0);
    1355      515361 :   nx = lgpol(x0);
    1356      515361 :   ny = lgpol(y0);
    1357      515361 :   zd = (GEN)avma;
    1358      515361 :   x = x0 + 2; y = y0 + 2; a = ny-d;
    1359      515361 :   if (a <= 0)
    1360             :   {
    1361      291789 :     lz = nx+d+2;
    1362      291789 :     (void)new_chunk(lz); xd = x+nx; yd = y+ny;
    1363     4294362 :     while (xd > x) gel(--zd,0) = gcopy(gel(--xd,0));
    1364      291789 :     x = zd + a;
    1365      757955 :     while (zd > x) gel(--zd,0) = gen_0;
    1366             :   }
    1367             :   else
    1368             :   {
    1369      223572 :     xd = new_chunk(d); yd = y+d;
    1370      223572 :     x = RgX_addspec(x,yd, nx,a);
    1371      223572 :     lz = (a>nx)? ny+2: lg(x)+d;
    1372     8432092 :     x += 2; while (xd > x) *--zd = *--xd;
    1373             :   }
    1374     2623756 :   while (yd > y) gel(--zd,0) = gcopy(gel(--yd,0));
    1375      515361 :   *--zd = x0[1];
    1376      515361 :   *--zd = evaltyp(t_POL) | evallg(lz); return zd;
    1377             : }
    1378             : 
    1379             : /* return x * y mod t^n */
    1380             : static GEN
    1381     6627959 : RgXn_mul_basecase(GEN x, GEN y, long n)
    1382             : {
    1383     6627959 :   long i, lz = n+2, lx = lgpol(x), ly = lgpol(y);
    1384             :   GEN z;
    1385     6627959 :   if (lx < 0) return pol_0(varn(x));
    1386     6627959 :   if (ly < 0) return pol_0(varn(x));
    1387     6627959 :   z = cgetg(lz, t_POL) + 2;
    1388     6627959 :   x+=2; if (lx > n) lx = n;
    1389     6627959 :   y+=2; if (ly > n) ly = n;
    1390     6627959 :   z[-1] = x[-1];
    1391     6627959 :   if (ly > lx) { swap(x,y); lswap(lx,ly); }
    1392     6627959 :   x = RgXspec_kill0(x, lx);
    1393     6627959 :   y = RgXspec_kill0(y, ly);
    1394             :   /* x:y:z [i] = term of degree i */
    1395    26223885 :   for (i=0;i<ly; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,i+1);
    1396    11826184 :   for (  ; i<lx; i++) gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, 0,ly);
    1397     6674031 :   for (  ; i<n; i++)  gel(z,i) = RgX_mulspec_basecase_limb(x+i,y, i-lx+1,ly);
    1398     6627959 :   return normalizepol_lg(z - 2, lz);
    1399             : }
    1400             : /* Mulders / Karatsuba product f*g mod t^n (Hanrot-Zimmermann variant) */
    1401             : static GEN
    1402     9413940 : RgXn_mul2(GEN f, GEN g, long n)
    1403             : {
    1404     9413940 :   pari_sp av = avma;
    1405             :   GEN fe,fo, ge,go, l,h,m;
    1406             :   long n0, n1;
    1407     9413940 :   if (degpol(f) + degpol(g) < n) return RgX_mul(f,g);
    1408     6663449 :   if (n < 80) return RgXn_mul_basecase(f,g,n);
    1409       35490 :   n0 = n>>1; n1 = n-n0;
    1410       35490 :   RgX_even_odd(f, &fe, &fo);
    1411       35490 :   RgX_even_odd(g, &ge, &go);
    1412       35490 :   l = RgXn_mul2(fe,ge,n1);
    1413       35490 :   h = RgXn_mul2(fo,go,n0);
    1414       35490 :   m = RgX_sub(RgXn_mul2(RgX_add(fe,fo),RgX_add(ge,go),n0), RgX_add(l,h));
    1415             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1416             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1417       35490 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1418             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1419       35490 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1420       35490 :   m = RgX_inflate(m,2);
    1421             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1422       35490 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1423       35490 :   h = RgX_inflate(h,2);
    1424       35490 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1425       35490 :   return gerepileupto(av, h);
    1426             : }
    1427             : /* (f*g) \/ x^n */
    1428             : static GEN
    1429     1589258 : RgX_mulhigh_i2(GEN f, GEN g, long n)
    1430             : {
    1431     1589258 :   long d = degpol(f)+degpol(g) + 1 - n;
    1432             :   GEN h;
    1433     1589258 :   if (d <= 2) return RgX_shift_shallow(RgX_mul(f,g), -n);
    1434       29654 :   h = RgX_recip_i(RgXn_mul2(RgX_recip_i(f),
    1435             :                                   RgX_recip_i(g), d));
    1436       29654 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1437             : }
    1438             : 
    1439             : /* (f*g) \/ x^n */
    1440             : static GEN
    1441           0 : RgX_sqrhigh_i2(GEN f, long n)
    1442             : {
    1443           0 :   long d = 2*degpol(f)+ 1 - n;
    1444             :   GEN h;
    1445           0 :   if (d <= 2) return RgX_shift_shallow(RgX_sqr(f), -n);
    1446           0 :   h = RgX_recip_i(RgXn_sqr(RgX_recip_i(f), d));
    1447           0 :   return RgX_shift_shallow(h, d-1-degpol(h)); /* possibly (fg)(0) = 0 */
    1448             : }
    1449             : 
    1450             : /* fast product (Karatsuba) of polynomials a,b. These are not real GENs, a+2,
    1451             :  * b+2 were sent instead. na, nb = number of terms of a, b.
    1452             :  * Only c, c0, c1, c2 are genuine GEN.
    1453             :  */
    1454             : GEN
    1455    22350833 : RgX_mulspec(GEN a, GEN b, long na, long nb)
    1456             : {
    1457             :   GEN a0, c, c0;
    1458    22350833 :   long n0, n0a, i, v = 0;
    1459             :   pari_sp av;
    1460             : 
    1461    28735018 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v++; }
    1462    28241543 :   while (nb && isrationalzero(gel(b,0))) { b++; nb--; v++; }
    1463    22350801 :   if (na < nb) swapspec(a,b, na,nb);
    1464    22350801 :   if (!nb) return pol_0(0);
    1465             : 
    1466    22101845 :   if (nb < RgX_MUL_LIMIT) return RgX_mulspec_basecase(a,b,na,nb, v);
    1467      480597 :   RgX_shift_inplace_init(v);
    1468      480616 :   i = (na>>1); n0 = na-i; na = i;
    1469      480616 :   av = avma; a0 = a+n0; n0a = n0;
    1470     1336132 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1471             : 
    1472      480616 :   if (nb > n0)
    1473             :   {
    1474             :     GEN b0,c1,c2;
    1475             :     long n0b;
    1476             : 
    1477      479217 :     nb -= n0; b0 = b+n0; n0b = n0;
    1478     1447196 :     while (n0b && isrationalzero(gel(b,n0b-1))) n0b--;
    1479      479217 :     c = RgX_mulspec(a,b,n0a,n0b);
    1480      479217 :     c0 = RgX_mulspec(a0,b0, na,nb);
    1481             : 
    1482      479217 :     c2 = RgX_addspec_shallow(a0,a, na,n0a);
    1483      479217 :     c1 = RgX_addspec_shallow(b0,b, nb,n0b);
    1484             : 
    1485      479217 :     c1 = RgX_mulspec(c1+2,c2+2, lgpol(c1),lgpol(c2));
    1486      479217 :     c2 = RgX_sub(c1, RgX_add(c0,c));
    1487      479217 :     c0 = RgX_addmulXn_shallow(c0, c2, n0);
    1488             :   }
    1489             :   else
    1490             :   {
    1491        1399 :     c = RgX_mulspec(a,b,n0a,nb);
    1492        1399 :     c0 = RgX_mulspec(a0,b,na,nb);
    1493             :   }
    1494      480616 :   c0 = RgX_addmulXn(c0,c,n0);
    1495      480616 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1496             : }
    1497             : 
    1498             : INLINE GEN
    1499       50774 : RgX_sqrspec_basecase_limb(GEN x, long a, long i)
    1500             : {
    1501       50774 :   pari_sp av = avma;
    1502       50774 :   GEN s = NULL;
    1503       50774 :   long j, l = (i+1)>>1;
    1504      139986 :   for (j=a; j<l; j++)
    1505             :   {
    1506       89212 :     GEN xj = gel(x,j), xx = gel(x,i-j);
    1507       89212 :     if (xj && xx)
    1508             :     {
    1509       81568 :       GEN t = gmul(xj, xx);
    1510       81568 :       s = s? gadd(s, t): t;
    1511             :     }
    1512             :   }
    1513       50774 :   if (s) s = gshift(s,1);
    1514       50774 :   if ((i&1) == 0)
    1515             :   {
    1516       29967 :     GEN t = gel(x, i>>1);
    1517       29967 :     if (t) {
    1518       27573 :       t = gsqr(t);
    1519       27573 :       s = s? gadd(s, t): t;
    1520             :     }
    1521             :   }
    1522       50774 :   return s? gerepileupto(av,s): gen_0;
    1523             : }
    1524             : static GEN
    1525        9160 : RgX_sqrspec_basecase(GEN x, long nx, long v)
    1526             : {
    1527             :   long i, lz, nz;
    1528             :   GEN z;
    1529             : 
    1530        9160 :   if (!nx) return pol_0(0);
    1531        9160 :   x = RgXspec_kill0(x,nx);
    1532        9160 :   lz = (nx << 1) + 1, nz = lz-2;
    1533        9160 :   lz += v;
    1534        9160 :   z = cgetg(lz,t_POL) + 2;
    1535       13528 :   for (i=0; i<v; i++) gel(z++, 0) = gen_0;
    1536       39127 :   for (i=0; i<nx; i++)gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1537       29967 :   for (  ; i<nz; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, i-nx+1, i);
    1538        9160 :   z -= v+2; z[1] = 0; return normalizepol_lg(z, lz);
    1539             : }
    1540             : /* return x^2 mod t^n */
    1541             : static GEN
    1542           0 : RgXn_sqr_basecase(GEN x, long n)
    1543             : {
    1544           0 :   long i, lz = n+2, lx = lgpol(x);
    1545             :   GEN z;
    1546           0 :   if (lx < 0) return pol_0(varn(x));
    1547           0 :   z = cgetg(lz, t_POL);
    1548           0 :   z[1] = x[1];
    1549           0 :   x+=2; if (lx > n) lx = n;
    1550           0 :   x = RgXspec_kill0(x,lx);
    1551           0 :   z+=2;/* x:z [i] = term of degree i */
    1552           0 :   for (i=0;i<lx; i++) gel(z,i) = RgX_sqrspec_basecase_limb(x, 0, i);
    1553           0 :   for (  ; i<n; i++)  gel(z,i) = RgX_sqrspec_basecase_limb(x, i-lx+1, i);
    1554           0 :   z -= 2; return normalizepol_lg(z, lz);
    1555             : }
    1556             : /* Mulders / Karatsuba product f^2 mod t^n (Hanrot-Zimmermann variant) */
    1557             : static GEN
    1558         315 : RgXn_sqr2(GEN f, long n)
    1559             : {
    1560         315 :   pari_sp av = avma;
    1561             :   GEN fe,fo, l,h,m;
    1562             :   long n0, n1;
    1563         315 :   if (2*degpol(f) < n) return RgX_sqr_i(f);
    1564           0 :   if (n < 80) return RgXn_sqr_basecase(f,n);
    1565           0 :   n0 = n>>1; n1 = n-n0;
    1566           0 :   RgX_even_odd(f, &fe, &fo);
    1567           0 :   l = RgXn_sqr(fe,n1);
    1568           0 :   h = RgXn_sqr(fo,n0);
    1569           0 :   m = RgX_sub(RgXn_sqr(RgX_add(fe,fo),n0), RgX_add(l,h));
    1570             :   /* n1-1 <= n0 <= n1, deg l,m <= n1-1, deg h <= n0-1
    1571             :    * result is t^2 h(t^2) + t m(t^2) + l(t^2) */
    1572           0 :   l = RgX_inflate(l,2); /* deg l <= 2n1 - 2 <= n-1 */
    1573             :   /* deg(t m(t^2)) <= 2n1 - 1 <= n, truncate to < n */
    1574           0 :   if (2*degpol(m)+1 == n) m = normalizepol_lg(m, lg(m)-1);
    1575           0 :   m = RgX_inflate(m,2);
    1576             :   /* deg(t^2 h(t^2)) <= 2n0 <= n, truncate to < n */
    1577           0 :   if (2*degpol(h)+2 == n) h = normalizepol_lg(h, lg(h)-1);
    1578           0 :   h = RgX_inflate(h,2);
    1579           0 :   h = RgX_addmulXn(RgX_addmulXn_shallow(h,m,1), l,1);
    1580           0 :   return gerepileupto(av, h);
    1581             : }
    1582             : GEN
    1583        9199 : RgX_sqrspec(GEN a, long na)
    1584             : {
    1585             :   GEN a0, c, c0, c1;
    1586        9199 :   long n0, n0a, i, v = 0;
    1587             :   pari_sp av;
    1588             : 
    1589       11383 :   while (na && isrationalzero(gel(a,0))) { a++; na--; v += 2; }
    1590        9199 :   if (na<RgX_SQR_LIMIT) return RgX_sqrspec_basecase(a, na, v);
    1591          39 :   RgX_shift_inplace_init(v);
    1592          39 :   i = (na>>1); n0 = na-i; na = i;
    1593          39 :   av = avma; a0 = a+n0; n0a = n0;
    1594          39 :   while (n0a && isrationalzero(gel(a,n0a-1))) n0a--;
    1595             : 
    1596          39 :   c = RgX_sqrspec(a,n0a);
    1597          39 :   c0 = RgX_sqrspec(a0,na);
    1598          39 :   c1 = gmul2n(RgX_mulspec(a0,a, na,n0a), 1);
    1599          39 :   c0 = RgX_addmulXn_shallow(c0,c1, n0);
    1600          39 :   c0 = RgX_addmulXn(c0,c,n0);
    1601          39 :   return RgX_shift_inplace(gerepileupto(av,c0), v);
    1602             : }
    1603             : 
    1604             : /* (X^a + A)(X^b + B) - X^(a+b), where deg A < a, deg B < b */
    1605             : GEN
    1606     1711151 : RgX_mul_normalized(GEN A, long a, GEN B, long b)
    1607             : {
    1608     1711151 :   GEN z = RgX_mul(A, B);
    1609     1711144 :   if (a < b)
    1610       10446 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(A, B, b-a), z, a);
    1611     1700698 :   else if (a > b)
    1612     1098950 :     z = RgX_addmulXn_shallow(RgX_addmulXn_shallow(B, A, a-b), z, b);
    1613             :   else
    1614      601748 :     z = RgX_addmulXn_shallow(RgX_add(A, B), z, a);
    1615     1711146 :   return z;
    1616             : }
    1617             : 
    1618             : GEN
    1619    20910427 : RgX_mul_i(GEN x, GEN y)
    1620             : {
    1621    20910427 :   GEN z = RgX_mulspec(x+2, y+2, lgpol(x), lgpol(y));
    1622    20909980 :   setvarn(z, varn(x)); return z;
    1623             : }
    1624             : 
    1625             : GEN
    1626        9121 : RgX_sqr_i(GEN x)
    1627             : {
    1628        9121 :   GEN z = RgX_sqrspec(x+2, lgpol(x));
    1629        9121 :   setvarn(z,varn(x)); return z;
    1630             : }
    1631             : 
    1632             : /*******************************************************************/
    1633             : /*                                                                 */
    1634             : /*                               DIVISION                          */
    1635             : /*                                                                 */
    1636             : /*******************************************************************/
    1637             : GEN
    1638     7161698 : RgX_Rg_divexact(GEN x, GEN y) {
    1639     7161698 :   long i, lx = lg(x);
    1640             :   GEN z;
    1641     7161698 :   if (lx == 2) return gcopy(x);
    1642     7110980 :   switch(typ(y))
    1643             :   {
    1644     6951221 :     case t_INT:
    1645     6951221 :       if (is_pm1(y)) return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1646     6576652 :       break;
    1647        5215 :     case t_INTMOD: case t_POLMOD: return RgX_Rg_mul(x, ginv(y));
    1648             :   }
    1649     6731196 :   z = cgetg(lx, t_POL); z[1] = x[1];
    1650    34374176 :   for (i=2; i<lx; i++) gel(z,i) = gdivexact(gel(x,i),y);
    1651     6729900 :   return z;
    1652             : }
    1653             : GEN
    1654    30170334 : RgX_Rg_div(GEN x, GEN y) {
    1655    30170334 :   long i, lx = lg(x);
    1656             :   GEN z;
    1657    30170334 :   if (lx == 2) return gcopy(x);
    1658    29910465 :   switch(typ(y))
    1659             :   {
    1660    20716424 :     case t_INT:
    1661    20716424 :       if (is_pm1(y)) return signe(y) < 0 ? RgX_neg(x): RgX_copy(x);
    1662     4115848 :       break;
    1663       13125 :     case t_INTMOD: case t_POLMOD: return RgX_Rg_mul(x, ginv(y));
    1664             :   }
    1665    13296764 :   z = cgetg(lx, t_POL); z[1] = x[1];
    1666    49575589 :   for (i=2; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
    1667    13296304 :   return normalizepol_lg(z, lx);
    1668             : }
    1669             : GEN
    1670       39018 : RgX_normalize(GEN x)
    1671             : {
    1672       39018 :   GEN z, d = NULL;
    1673       39018 :   long i, n = lg(x)-1;
    1674       39018 :   for (i = n; i > 1; i--) { d = gel(x,i); if (!gequal0(d)) break; }
    1675       39018 :   if (i == 1) return pol_0(varn(x));
    1676       39018 :   if (i == n && isint1(d)) return x;
    1677       17584 :   n = i; z = cgetg(n+1, t_POL); z[1] = x[1];
    1678       31570 :   for (i=2; i<n; i++) gel(z,i) = gdiv(gel(x,i),d);
    1679       17584 :   gel(z,n) = Rg_get_1(d); return z;
    1680             : }
    1681             : GEN
    1682       10423 : RgX_divs(GEN x, long y) { pari_APPLY_pol(gdivgs(gel(x,i),y)); }
    1683             : GEN
    1684      250640 : RgX_div_by_X_x(GEN a, GEN x, GEN *r)
    1685             : {
    1686      250640 :   long l = lg(a), i;
    1687             :   GEN a0, z0, z;
    1688             : 
    1689      250640 :   if (l <= 3)
    1690             :   {
    1691           0 :     if (r) *r = l == 2? gen_0: gcopy(gel(a,2));
    1692           0 :     return pol_0(varn(a));
    1693             :   }
    1694      250640 :   z = cgetg(l-1, t_POL);
    1695      250645 :   z[1] = a[1];
    1696      250645 :   a0 = a + l-1;
    1697      250645 :   z0 = z + l-2; *z0 = *a0--;
    1698     2954246 :   for (i=l-3; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
    1699             :   {
    1700     2703605 :     GEN t = gadd(gel(a0--,0), gmul(x, gel(z0--,0)));
    1701     2703601 :     gel(z0,0) = t;
    1702             :   }
    1703      250641 :   if (r) *r = gadd(gel(a0,0), gmul(x, gel(z0,0)));
    1704      250641 :   return z;
    1705             : }
    1706             : /* Polynomial division x / y:
    1707             :  *   if pr = ONLY_REM return remainder, otherwise return quotient
    1708             :  *   if pr = ONLY_DIVIDES return quotient if division is exact, else NULL
    1709             :  *   if pr != NULL set *pr to remainder, as the last object on stack */
    1710             : /* assume, typ(x) = typ(y) = t_POL, same variable */
    1711             : static GEN
    1712    23611834 : RgX_divrem_i(GEN x, GEN y, GEN *pr)
    1713             : {
    1714             :   pari_sp avy, av, av1;
    1715             :   long dx,dy,dz,i,j,sx,lr;
    1716             :   GEN z,p1,p2,rem,y_lead,mod,p;
    1717             :   GEN (*f)(GEN,GEN);
    1718             : 
    1719    23611834 :   if (!signe(y)) pari_err_INV("RgX_divrem",y);
    1720             : 
    1721    23611834 :   dy = degpol(y);
    1722    23611810 :   y_lead = gel(y,dy+2);
    1723    23611810 :   if (gequal0(y_lead)) /* normalize denominator if leading term is 0 */
    1724             :   {
    1725           0 :     pari_warn(warner,"normalizing a polynomial with 0 leading term");
    1726           0 :     for (dy--; dy>=0; dy--)
    1727             :     {
    1728           0 :       y_lead = gel(y,dy+2);
    1729           0 :       if (!gequal0(y_lead)) break;
    1730             :     }
    1731             :   }
    1732    23611798 :   if (!dy) /* y is constant */
    1733             :   {
    1734        6857 :     if (pr == ONLY_REM) return pol_0(varn(x));
    1735        6850 :     z = RgX_Rg_div(x, y_lead);
    1736        6850 :     if (pr == ONLY_DIVIDES) return z;
    1737        2636 :     if (pr) *pr = pol_0(varn(x));
    1738        2636 :     return z;
    1739             :   }
    1740    23604941 :   dx = degpol(x);
    1741    23604884 :   if (dx < dy)
    1742             :   {
    1743     3858807 :     if (pr == ONLY_REM) return RgX_copy(x);
    1744      374792 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1745      374771 :     z = pol_0(varn(x));
    1746      374771 :     if (pr) *pr = RgX_copy(x);
    1747      374771 :     return z;
    1748             :   }
    1749             : 
    1750             :   /* x,y in R[X], y non constant */
    1751    19746077 :   av = avma;
    1752    19746077 :   p = NULL;
    1753    19746077 :   if (RgX_is_FpX(x, &p) && RgX_is_FpX(y, &p) && p)
    1754             :   {
    1755      238784 :     z = FpX_divrem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p, pr);
    1756      238784 :     if (!z) return gc_NULL(av);
    1757      238784 :     z = FpX_to_mod(z, p);
    1758      238784 :     if (!pr || pr == ONLY_REM || pr == ONLY_DIVIDES)
    1759      122528 :       return gerepileupto(av, z);
    1760      116256 :     *pr = FpX_to_mod(*pr, p);
    1761      116256 :     return gc_all(av, 2, &z, pr);
    1762             :   }
    1763    19507441 :   switch(typ(y_lead))
    1764             :   {
    1765           0 :     case t_REAL:
    1766           0 :       y_lead = ginv(y_lead);
    1767           0 :       f = gmul; mod = NULL;
    1768           0 :       break;
    1769        2074 :     case t_INTMOD:
    1770        2074 :     case t_POLMOD: y_lead = ginv(y_lead);
    1771        2074 :       f = gmul; mod = gmodulo(gen_1, gel(y_lead,1));
    1772        2074 :       break;
    1773    19505367 :     default: if (gequal1(y_lead)) y_lead = NULL;
    1774    19505350 :       f = gdiv; mod = NULL;
    1775             :   }
    1776             : 
    1777    19507424 :   if (y_lead == NULL)
    1778    17492938 :     p2 = gel(x,dx+2);
    1779             :   else {
    1780             :     for(;;) {
    1781     2014486 :       p2 = f(gel(x,dx+2),y_lead);
    1782     2014487 :       p2 = simplify_shallow(p2);
    1783     2014487 :       if (!isexactzero(p2) || (--dx < 0)) break;
    1784             :     }
    1785     2014487 :     if (dx < dy) /* leading coeff of x was in fact zero */
    1786             :     {
    1787           0 :       if (pr == ONLY_DIVIDES) {
    1788           0 :         set_avma(av);
    1789           0 :         return (dx < 0)? pol_0(varn(x)) : NULL;
    1790             :       }
    1791           0 :       if (pr == ONLY_REM)
    1792             :       {
    1793           0 :         if (dx < 0)
    1794           0 :           return gerepilecopy(av, scalarpol(p2, varn(x)));
    1795             :         else
    1796             :         {
    1797             :           GEN t;
    1798           0 :           set_avma(av);
    1799           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1800           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1801           0 :           return t;
    1802             :         }
    1803             :       }
    1804           0 :       if (pr) /* cf ONLY_REM above */
    1805             :       {
    1806           0 :         if (dx < 0)
    1807             :         {
    1808           0 :           p2 = gclone(p2);
    1809           0 :           set_avma(av);
    1810           0 :           z = pol_0(varn(x));
    1811           0 :           x = scalarpol(p2, varn(x));
    1812           0 :           gunclone(p2);
    1813             :         }
    1814             :         else
    1815             :         {
    1816             :           GEN t;
    1817           0 :           set_avma(av);
    1818           0 :           z = pol_0(varn(x));
    1819           0 :           t = cgetg(dx + 3, t_POL); t[1] = x[1];
    1820           0 :           for (i = 2; i < dx + 3; i++) gel(t,i) = gcopy(gel(x,i));
    1821           0 :           x = t;
    1822             :         }
    1823           0 :         *pr = x;
    1824             :       }
    1825             :       else
    1826             :       {
    1827           0 :         set_avma(av);
    1828           0 :         z = pol_0(varn(x));
    1829             :       }
    1830           0 :       return z;
    1831             :     }
    1832             :   }
    1833             :   /* dx >= dy */
    1834    19507425 :   avy = avma;
    1835    19507425 :   dz = dx-dy;
    1836    19507425 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1837    19507401 :   x += 2;
    1838    19507401 :   z += 2;
    1839    19507401 :   y += 2;
    1840    19507401 :   gel(z,dz) = gcopy(p2);
    1841             : 
    1842    54034064 :   for (i=dx-1; i>=dy; i--)
    1843             :   {
    1844    34526926 :     av1=avma; p1=gel(x,i);
    1845  1140379342 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1846    34521898 :     if (y_lead) p1 = simplify(f(p1,y_lead));
    1847             : 
    1848    34521898 :     if (isrationalzero(p1)) { set_avma(av1); p1 = gen_0; }
    1849             :     else
    1850    24532010 :       p1 = avma==av1? gcopy(p1): gerepileupto(av1,p1);
    1851    34526136 :     gel(z,i-dy) = p1;
    1852             :   }
    1853    19507138 :   if (!pr) return gerepileupto(av,z-2);
    1854             : 
    1855    12532241 :   rem = (GEN)avma; av1 = (pari_sp)new_chunk(dx+3);
    1856    12896809 :   for (sx=0; ; i--)
    1857             :   {
    1858    12896809 :     p1 = gel(x,i);
    1859             :     /* we always enter this loop at least once */
    1860    32392302 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1861    12895743 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1862    12895743 :     if (!gequal0(p1)) { sx = 1; break; } /* remainder is nonzero */
    1863     3586675 :     if (!isexactzero(p1)) break;
    1864     3437445 :     if (!i) break;
    1865      364519 :     set_avma(av1);
    1866             :   }
    1867    12531528 :   if (pr == ONLY_DIVIDES)
    1868             :   {
    1869        7917 :     if (sx) return gc_NULL(av);
    1870        7896 :     set_avma((pari_sp)rem); return gerepileupto(av,z-2);
    1871             :   }
    1872    12523611 :   lr=i+3; rem -= lr;
    1873    12523611 :   if (avma==av1) { set_avma((pari_sp)rem); p1 = gcopy(p1); }
    1874    12479992 :   else p1 = gerepileupto((pari_sp)rem,p1);
    1875    12524308 :   rem[0] = evaltyp(t_POL) | _evallg(lr);
    1876    12524308 :   rem[1] = z[-1];
    1877    12524308 :   rem += 2;
    1878    12524308 :   gel(rem,i) = p1;
    1879    17249579 :   for (i--; i>=0; i--)
    1880             :   {
    1881     4725300 :     av1=avma; p1 = gel(x,i);
    1882    13587245 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1883     4724827 :     if (mod && avma==av1) p1 = gmul(p1,mod);
    1884     4725051 :     gel(rem,i) = avma==av1? gcopy(p1):gerepileupto(av1,p1);
    1885             :   }
    1886    12524279 :   rem -= 2;
    1887    12524279 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1888    12524562 :   if (pr == ONLY_REM) return gerepileupto(av,rem);
    1889     6758582 :   z -= 2;
    1890             :   {
    1891     6758582 :     GEN *gptr[2]; gptr[0]=&z; gptr[1]=&rem;
    1892     6758582 :     gerepilemanysp(av,avy,gptr,2); *pr = rem; return z;
    1893             :   }
    1894             : }
    1895             : 
    1896             : GEN
    1897    14361856 : RgX_divrem(GEN x, GEN y, GEN *pr)
    1898             : {
    1899    14361856 :   if (pr == ONLY_REM) return RgX_rem(x, y);
    1900    14361856 :   return RgX_divrem_i(x, y, pr);
    1901             : }
    1902             : 
    1903             : /* x and y in (R[Y]/T)[X]  (lifted), T in R[Y]. y preferably monic */
    1904             : GEN
    1905      143345 : RgXQX_divrem(GEN x, GEN y, GEN T, GEN *pr)
    1906             : {
    1907             :   long vx, dx, dy, dz, i, j, sx, lr;
    1908             :   pari_sp av0, av, tetpil;
    1909             :   GEN z,p1,rem,lead;
    1910             : 
    1911      143345 :   if (!signe(y)) pari_err_INV("RgXQX_divrem",y);
    1912      143345 :   vx = varn(x);
    1913      143345 :   dx = degpol(x);
    1914      143345 :   dy = degpol(y);
    1915      143345 :   if (dx < dy)
    1916             :   {
    1917       31473 :     if (pr)
    1918             :     {
    1919       31445 :       av0 = avma; x = RgXQX_red(x, T);
    1920       31445 :       if (pr == ONLY_DIVIDES) { set_avma(av0); return signe(x)? NULL: gen_0; }
    1921       31438 :       if (pr == ONLY_REM) return x;
    1922           0 :       *pr = x;
    1923             :     }
    1924          28 :     return pol_0(vx);
    1925             :   }
    1926      111872 :   lead = leading_coeff(y);
    1927      111872 :   if (!dy) /* y is constant */
    1928             :   {
    1929         602 :     if (pr && pr != ONLY_DIVIDES)
    1930             :     {
    1931           0 :       if (pr == ONLY_REM) return pol_0(vx);
    1932           0 :       *pr = pol_0(vx);
    1933             :     }
    1934         602 :     if (gequal1(lead)) return RgX_copy(x);
    1935           0 :     av0 = avma; x = gmul(x, ginvmod(lead,T)); tetpil = avma;
    1936           0 :     return gerepile(av0,tetpil,RgXQX_red(x,T));
    1937             :   }
    1938      111270 :   av0 = avma; dz = dx-dy;
    1939      111270 :   lead = gequal1(lead)? NULL: gclone(ginvmod(lead,T));
    1940      111270 :   set_avma(av0);
    1941      111270 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
    1942      111270 :   x += 2; y += 2; z += 2;
    1943             : 
    1944      111270 :   p1 = gel(x,dx); av = avma;
    1945      111270 :   gel(z,dz) = lead? gerepileupto(av, grem(gmul(p1,lead), T)): gcopy(p1);
    1946      571004 :   for (i=dx-1; i>=dy; i--)
    1947             :   {
    1948      459734 :     av=avma; p1=gel(x,i);
    1949     2395215 :     for (j=i-dy+1; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1950      459700 :     if (lead) p1 = gmul(grem(p1, T), lead);
    1951      459700 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil, grem(p1, T));
    1952             :   }
    1953      111270 :   if (!pr) { guncloneNULL(lead); return z-2; }
    1954             : 
    1955      109996 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
    1956      185634 :   for (sx=0; ; i--)
    1957             :   {
    1958      185634 :     p1 = gel(x,i);
    1959      642521 :     for (j=0; j<=i && j<=dz; j++) p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1960      185632 :     tetpil=avma; p1 = grem(p1, T); if (!gequal0(p1)) { sx = 1; break; }
    1961      102833 :     if (!i) break;
    1962       75638 :     set_avma(av);
    1963             :   }
    1964      109996 :   if (pr == ONLY_DIVIDES)
    1965             :   {
    1966       27190 :     guncloneNULL(lead);
    1967       27190 :     if (sx) return gc_NULL(av0);
    1968       24619 :     return gc_const((pari_sp)rem, z-2);
    1969             :   }
    1970       82806 :   lr=i+3; rem -= lr;
    1971       82806 :   rem[0] = evaltyp(t_POL) | _evallg(lr);
    1972       82806 :   rem[1] = z[-1];
    1973       82806 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
    1974       82806 :   rem += 2; gel(rem,i) = p1;
    1975      179107 :   for (i--; i>=0; i--)
    1976             :   {
    1977       96301 :     av=avma; p1 = gel(x,i);
    1978      322820 :     for (j=0; j<=i && j<=dz; j++)
    1979      226519 :       p1 = gsub(p1, gmul(gel(z,j),gel(y,i-j)));
    1980       96301 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, grem(p1, T));
    1981             :   }
    1982       82806 :   rem -= 2;
    1983       82806 :   guncloneNULL(lead);
    1984       82806 :   if (!sx) (void)normalizepol_lg(rem, lr);
    1985       82806 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
    1986         161 :   *pr = rem; return z-2;
    1987             : }
    1988             : 
    1989             : /*******************************************************************/
    1990             : /*                                                                 */
    1991             : /*                        PSEUDO-DIVISION                          */
    1992             : /*                                                                 */
    1993             : /*******************************************************************/
    1994             : INLINE GEN
    1995     4039637 : rem(GEN c, GEN T)
    1996             : {
    1997     4039637 :   if (T && typ(c) == t_POL && varn(c) == varn(T)) c = RgX_rem(c, T);
    1998     4039636 :   return c;
    1999             : }
    2000             : 
    2001             : /* x, y, are ZYX, lc(y) is an integer, T is a ZY */
    2002             : int
    2003       17282 : ZXQX_dvd(GEN x, GEN y, GEN T)
    2004             : {
    2005             :   long dx, dy, i, T_ismonic;
    2006       17282 :   pari_sp av = avma, av2;
    2007             :   GEN y_lead;
    2008             : 
    2009       17282 :   if (!signe(y)) pari_err_INV("ZXQX_dvd",y);
    2010       17282 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2011       17282 :   if (typ(y_lead) == t_POL) y_lead = gel(y_lead, 2); /* t_INT */
    2012             :   /* if monic, no point in using pseudo-division */
    2013       17282 :   if (gequal1(y_lead)) return signe(RgXQX_rem(x, y, T)) == 0;
    2014       14741 :   T_ismonic = gequal1(leading_coeff(T));
    2015       14741 :   dx = degpol(x);
    2016       14741 :   if (dx < dy) return !signe(x);
    2017       14741 :   (void)new_chunk(2);
    2018       14741 :   x = RgX_recip_i(x)+2;
    2019       14741 :   y = RgX_recip_i(y)+2;
    2020             :   /* pay attention to sparse divisors */
    2021       29983 :   for (i = 1; i <= dy; i++)
    2022       15242 :     if (!signe(gel(y,i))) gel(y,i) = NULL;
    2023       14741 :   av2 = avma;
    2024             :   for (;;)
    2025       72872 :   {
    2026       87613 :     GEN m, x0 = gel(x,0), y0 = y_lead, cx = content(x0);
    2027       87613 :     x0 = gneg(x0);
    2028       87613 :     m = gcdii(cx, y0);
    2029       87613 :     if (!equali1(m))
    2030             :     {
    2031       85118 :       x0 = gdiv(x0, m);
    2032       85118 :       y0 = diviiexact(y0, m);
    2033       85118 :       if (equali1(y0)) y0 = NULL;
    2034             :     }
    2035      178999 :     for (i=1; i<=dy; i++)
    2036             :     {
    2037       91386 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    2038       91386 :       if (gel(y,i)) c = gadd(c, gmul(x0,gel(y,i)));
    2039       91386 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    2040       91386 :       gel(x,i) = c;
    2041             :     }
    2042      687617 :     for (   ; i<=dx; i++)
    2043             :     {
    2044      600004 :       GEN c = gel(x,i); if (y0) c = gmul(y0, c);
    2045      600004 :       if (typ(c) == t_POL) c = T_ismonic ? ZX_rem(c, T): RgX_rem(c, T);
    2046      600004 :       gel(x,i) = c;
    2047             :     }
    2048      102110 :     do { x++; dx--; } while (dx >= 0 && !signe(gel(x,0)));
    2049       87613 :     if (dx < dy) break;
    2050       72872 :     if (gc_needed(av2,1))
    2051             :     {
    2052          28 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZXQX_dvd dx = %ld >= %ld",dx,dy);
    2053          28 :       gerepilecoeffs(av2,x,dx+1);
    2054             :     }
    2055             :   }
    2056       14741 :   return gc_bool(av, dx < 0);
    2057             : }
    2058             : 
    2059             : /* T either NULL or a t_POL. */
    2060             : GEN
    2061      629963 : RgXQX_pseudorem(GEN x, GEN y, GEN T)
    2062             : {
    2063      629963 :   long vx = varn(x), dx, dy, dz, i, lx, p;
    2064      629963 :   pari_sp av = avma, av2;
    2065             :   GEN y_lead;
    2066             : 
    2067      629963 :   if (!signe(y)) pari_err_INV("RgXQX_pseudorem",y);
    2068      629963 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2069             :   /* if monic, no point in using pseudo-division */
    2070      629964 :   if (gequal1(y_lead)) return T? RgXQX_rem(x, y, T): RgX_rem(x, y);
    2071      580588 :   dx = degpol(x);
    2072      580588 :   if (dx < dy) return RgX_copy(x);
    2073      580588 :   (void)new_chunk(2);
    2074      580587 :   x = RgX_recip_i(x)+2;
    2075      580585 :   y = RgX_recip_i(y)+2;
    2076             :   /* pay attention to sparse divisors */
    2077     1956001 :   for (i = 1; i <= dy; i++)
    2078     1375415 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2079      580586 :   dz = dx-dy; p = dz+1;
    2080      580586 :   av2 = avma;
    2081             :   for (;;)
    2082             :   {
    2083     1247251 :     gel(x,0) = gneg(gel(x,0)); p--;
    2084     4193363 :     for (i=1; i<=dy; i++)
    2085             :     {
    2086     2946117 :       GEN c = gmul(y_lead, gel(x,i));
    2087     2946047 :       if (gel(y,i)) c = gadd(c, gmul(gel(x,0),gel(y,i)));
    2088     2946113 :       gel(x,i) = rem(c, T);
    2089             :     }
    2090     2179125 :     for (   ; i<=dx; i++)
    2091             :     {
    2092      931868 :       GEN c = gmul(y_lead, gel(x,i));
    2093      931859 :       gel(x,i) = rem(c, T);
    2094             :     }
    2095     1293479 :     do { x++; dx--; } while (dx >= 0 && gequal0(gel(x,0)));
    2096     1247255 :     if (dx < dy) break;
    2097      666665 :     if (gc_needed(av2,1))
    2098             :     {
    2099           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudorem dx = %ld >= %ld",dx,dy);
    2100           0 :       gerepilecoeffs(av2,x,dx+1);
    2101             :     }
    2102             :   }
    2103      580590 :   if (dx < 0) return pol_0(vx);
    2104      579407 :   lx = dx+3; x -= 2;
    2105      579407 :   x[0] = evaltyp(t_POL) | _evallg(lx);
    2106      579407 :   x[1] = evalsigne(1) | evalvarn(vx);
    2107      579407 :   x = RgX_recip_i(x);
    2108      579408 :   if (p)
    2109             :   { /* multiply by y[0]^p   [beware dummy vars from FpX_FpXY_resultant] */
    2110       28615 :     GEN t = y_lead;
    2111       28615 :     if (T && typ(t) == t_POL && varn(t) == varn(T))
    2112           0 :       t = RgXQ_powu(t, p, T);
    2113             :     else
    2114       28615 :       t = gpowgs(t, p);
    2115       88534 :     for (i=2; i<lx; i++)
    2116             :     {
    2117       59919 :       GEN c = gmul(gel(x,i), t);
    2118       59919 :       gel(x,i) = rem(c,T);
    2119             :     }
    2120       28615 :     if (!T) return gerepileupto(av, x);
    2121             :   }
    2122      550793 :   return gerepilecopy(av, x);
    2123             : }
    2124             : 
    2125             : GEN
    2126      629963 : RgX_pseudorem(GEN x, GEN y) { return RgXQX_pseudorem(x,y, NULL); }
    2127             : 
    2128             : /* Compute z,r s.t lc(y)^(dx-dy+1) x = z y + r */
    2129             : GEN
    2130       12084 : RgXQX_pseudodivrem(GEN x, GEN y, GEN T, GEN *ptr)
    2131             : {
    2132       12084 :   long vx = varn(x), dx, dy, dz, i, iz, lx, lz, p;
    2133       12084 :   pari_sp av = avma, av2;
    2134             :   GEN z, r, ypow, y_lead;
    2135             : 
    2136       12084 :   if (!signe(y)) pari_err_INV("RgXQX_pseudodivrem",y);
    2137       12084 :   dy = degpol(y); y_lead = gel(y,dy+2);
    2138       12084 :   if (gequal1(y_lead)) return T? RgXQX_divrem(x,y, T, ptr): RgX_divrem(x,y, ptr);
    2139        7589 :   dx = degpol(x);
    2140        7589 :   if (dx < dy) { *ptr = RgX_copy(x); return pol_0(vx); }
    2141        7589 :   if (dx == dy)
    2142             :   {
    2143          98 :     GEN x_lead = gel(x,lg(x)-1);
    2144          98 :     x = RgX_renormalize_lg(leafcopy(x), lg(x)-1);
    2145          98 :     y = RgX_renormalize_lg(leafcopy(y), lg(y)-1);
    2146          98 :     r = RgX_sub(RgX_Rg_mul(x, y_lead), RgX_Rg_mul(y, x_lead));
    2147          98 :     *ptr = gerepileupto(av, r); return scalarpol(x_lead, vx);
    2148             :   }
    2149        7491 :   (void)new_chunk(2);
    2150        7491 :   x = RgX_recip_i(x)+2;
    2151        7491 :   y = RgX_recip_i(y)+2;
    2152             :   /* pay attention to sparse divisors */
    2153       39049 :   for (i = 1; i <= dy; i++)
    2154       31558 :     if (isexactzero(gel(y,i))) gel(y,i) = NULL;
    2155        7491 :   dz = dx-dy; p = dz+1;
    2156        7491 :   lz = dz+3;
    2157        7491 :   z = cgetg(lz, t_POL);
    2158        7491 :   z[1] = evalsigne(1) | evalvarn(vx);
    2159       28745 :   for (i = 2; i < lz; i++) gel(z,i) = gen_0;
    2160        7491 :   ypow = new_chunk(dz+1);
    2161        7491 :   gel(ypow,0) = gen_1;
    2162        7491 :   gel(ypow,1) = y_lead;
    2163       13763 :   for (i=2; i<=dz; i++)
    2164             :   {
    2165        6272 :     GEN c = gmul(gel(ypow,i-1), y_lead);
    2166        6272 :     gel(ypow,i) = rem(c,T);
    2167             :   }
    2168        7491 :   av2 = avma;
    2169        7491 :   for (iz=2;;)
    2170             :   {
    2171       15682 :     p--;
    2172       15682 :     gel(z,iz++) = rem(gmul(gel(x,0), gel(ypow,p)), T);
    2173       70034 :     for (i=1; i<=dy; i++)
    2174             :     {
    2175       54352 :       GEN c = gmul(y_lead, gel(x,i));
    2176       54352 :       if (gel(y,i)) c = gsub(c, gmul(gel(x,0),gel(y,i)));
    2177       54352 :       gel(x,i) = rem(c, T);
    2178             :     }
    2179       41184 :     for (   ; i<=dx; i++)
    2180             :     {
    2181       25502 :       GEN c = gmul(y_lead, gel(x,i));
    2182       25502 :       gel(x,i) = rem(c,T);
    2183             :     }
    2184       15682 :     x++; dx--;
    2185       21254 :     while (dx >= dy && gequal0(gel(x,0))) { x++; dx--; iz++; }
    2186       15682 :     if (dx < dy) break;
    2187        8191 :     if (gc_needed(av2,1))
    2188             :     {
    2189           0 :       GEN X = x-2;
    2190           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_pseudodivrem dx=%ld >= %ld",dx,dy);
    2191           0 :       X[0] = evaltyp(t_POL)|_evallg(dx+3); X[1] = z[1]; /* hack */
    2192           0 :       gerepileall(av2,2, &X, &z); x = X+2;
    2193             :     }
    2194             :   }
    2195       14036 :   while (dx >= 0 && gequal0(gel(x,0))) { x++; dx--; }
    2196        7491 :   if (dx < 0)
    2197         182 :     x = pol_0(vx);
    2198             :   else
    2199             :   {
    2200        7309 :     lx = dx+3; x -= 2;
    2201        7309 :     x[0] = evaltyp(t_POL) | _evallg(lx);
    2202        7309 :     x[1] = evalsigne(1) | evalvarn(vx);
    2203        7309 :     x = RgX_recip_i(x);
    2204             :   }
    2205        7491 :   z = RgX_recip_i(z);
    2206        7491 :   r = x;
    2207        7491 :   if (p)
    2208             :   {
    2209        3339 :     GEN c = gel(ypow,p); r = RgX_Rg_mul(r, c);
    2210        3339 :     if (T && typ(c) == t_POL && varn(c) == varn(T)) r = RgXQX_red(r, T);
    2211             :   }
    2212        7491 :   *ptr = r; return gc_all(av, 2, &z, ptr);
    2213             : }
    2214             : GEN
    2215       11846 : RgX_pseudodivrem(GEN x, GEN y, GEN *ptr)
    2216       11846 : { return RgXQX_pseudodivrem(x,y,NULL,ptr); }
    2217             : 
    2218             : GEN
    2219           0 : RgXQX_mul(GEN x, GEN y, GEN T)
    2220           0 : { return RgXQX_red(RgX_mul(x,y), T); }
    2221             : GEN
    2222   524149512 : RgX_Rg_mul(GEN x, GEN y) { pari_APPLY_pol(gmul(y, gel(x,i))); }
    2223             : GEN
    2224      141351 : RgX_mul2n(GEN x, long n) { pari_APPLY_pol(gmul2n(gel(x,i), n)); }
    2225             : GEN
    2226       26376 : RgX_muls(GEN x, long y) { pari_APPLY_pol(gmulsg(y, gel(x,i))); }
    2227             : GEN
    2228          35 : RgXQX_RgXQ_mul(GEN x, GEN y, GEN T) { return RgXQX_red(RgX_Rg_mul(x,y), T); }
    2229             : GEN
    2230         133 : RgXQV_RgXQ_mul(GEN v, GEN x, GEN T) { return RgXQV_red(RgV_Rg_mul(v,x), T); }
    2231             : 
    2232             : GEN
    2233           0 : RgXQX_sqr(GEN x, GEN T) { return RgXQX_red(RgX_sqr(x), T); }
    2234             : 
    2235             : GEN
    2236           0 : RgXQX_powers(GEN P, long n, GEN T)
    2237             : {
    2238           0 :   GEN v = cgetg(n+2, t_VEC);
    2239             :   long i;
    2240           0 :   gel(v, 1) = pol_1(varn(T));
    2241           0 :   if (n==0) return v;
    2242           0 :   gel(v, 2) = gcopy(P);
    2243           0 :   for (i = 2; i <= n; i++) gel(v,i+1) = RgXQX_mul(P, gel(v,i), T);
    2244           0 :   return v;
    2245             : }
    2246             : 
    2247             : static GEN
    2248      613872 : _add(void *data, GEN x, GEN y) { (void)data; return RgX_add(x, y); }
    2249             : static GEN
    2250           0 : _sub(void *data, GEN x, GEN y) { (void)data; return RgX_sub(x, y); }
    2251             : static GEN
    2252       67352 : _sqr(void *data, GEN x) { return RgXQ_sqr(x, (GEN)data); }
    2253             : static GEN
    2254       88307 : _pow(void *data, GEN x, GEN n) { return RgXQ_pow(x, n, (GEN)data); }
    2255             : static GEN
    2256      274857 : _mul(void *data, GEN x, GEN y) { return RgXQ_mul(x,y, (GEN)data); }
    2257             : static GEN
    2258     1023176 : _cmul(void *data, GEN P, long a, GEN x) { (void)data; return RgX_Rg_mul(x,gel(P,a+2)); }
    2259             : static GEN
    2260     1001285 : _one(void *data) { return pol_1(varn((GEN)data)); }
    2261             : static GEN
    2262         973 : _zero(void *data) { return pol_0(varn((GEN)data)); }
    2263             : static GEN
    2264      713508 : _red(void *data, GEN x) { (void)data; return gcopy(x); }
    2265             : 
    2266             : static struct bb_algebra RgXQ_algebra = { _red, _add, _sub,
    2267             :               _mul, _sqr, _one, _zero };
    2268             : 
    2269             : GEN
    2270           0 : RgX_RgXQV_eval(GEN Q, GEN x, GEN T)
    2271             : {
    2272           0 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)T,&RgXQ_algebra,_cmul);
    2273             : }
    2274             : 
    2275             : GEN
    2276      409302 : RgX_RgXQ_eval(GEN Q, GEN x, GEN T)
    2277             : {
    2278      409302 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2279      409302 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)T,&RgXQ_algebra,_cmul);
    2280             : }
    2281             : 
    2282             : /* mod X^n */
    2283             : struct modXn {
    2284             :   long v; /* varn(X) */
    2285             :   long n;
    2286             : } ;
    2287             : static GEN
    2288       11893 : _sqrXn(void *data, GEN x) {
    2289       11893 :   struct modXn *S = (struct modXn*)data;
    2290       11893 :   return RgXn_sqr(x, S->n);
    2291             : }
    2292             : static GEN
    2293        4528 : _mulXn(void *data, GEN x, GEN y) {
    2294        4528 :   struct modXn *S = (struct modXn*)data;
    2295        4528 :   return RgXn_mul(x,y, S->n);
    2296             : }
    2297             : static GEN
    2298        1939 : _oneXn(void *data) {
    2299        1939 :   struct modXn *S = (struct modXn*)data;
    2300        1939 :   return pol_1(S->v);
    2301             : }
    2302             : static GEN
    2303           0 : _zeroXn(void *data) {
    2304           0 :   struct modXn *S = (struct modXn*)data;
    2305           0 :   return pol_0(S->v);
    2306             : }
    2307             : static struct bb_algebra RgXn_algebra = { _red, _add, _sub, _mulXn, _sqrXn,
    2308             :                                           _oneXn, _zeroXn };
    2309             : 
    2310             : GEN
    2311         483 : RgXn_powers(GEN x, long m, long n)
    2312             : {
    2313         483 :   long d = degpol(x);
    2314         483 :   int use_sqr = (d<<1) >= n;
    2315             :   struct modXn S;
    2316         483 :   S.v = varn(x); S.n = n;
    2317         483 :   return gen_powers(x,m,use_sqr,(void*)&S,_sqrXn,_mulXn,_oneXn);
    2318             : }
    2319             : 
    2320             : GEN
    2321        2286 : RgXn_powu_i(GEN x, ulong m, long n)
    2322             : {
    2323             :   struct modXn S;
    2324             :   long v;
    2325        2286 :   if (n == 0) return x;
    2326        2286 :   v = RgX_valrem(x, &x);
    2327        2286 :   if (v) { n -= m * v; if (n <= 0) return pol_0(varn(x)); }
    2328        2265 :   S.v = varn(x); S.n = n;
    2329        2265 :   x = gen_powu_i(x, m, (void*)&S,_sqrXn,_mulXn);
    2330        2265 :   if (v) x = RgX_shift_shallow(x, m * v);
    2331        2265 :   return x;
    2332             : }
    2333             : GEN
    2334           0 : RgXn_powu(GEN x, ulong m, long n)
    2335             : {
    2336             :   pari_sp av;
    2337           0 :   if (n == 0) return gcopy(x);
    2338           0 :   av = avma; return gerepilecopy(av, RgXn_powu_i(x, m, n));
    2339             : }
    2340             : 
    2341             : GEN
    2342         966 : RgX_RgXnV_eval(GEN Q, GEN x, long n)
    2343             : {
    2344             :   struct modXn S;
    2345         966 :   S.v = varn(gel(x,2)); S.n = n;
    2346         966 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&S,&RgXn_algebra,_cmul);
    2347             : }
    2348             : 
    2349             : GEN
    2350           0 : RgX_RgXn_eval(GEN Q, GEN x, long n)
    2351             : {
    2352           0 :   int use_sqr = 2*degpol(x) >= n;
    2353             :   struct modXn S;
    2354           0 :   S.v = varn(x); S.n = n;
    2355           0 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2356             : }
    2357             : 
    2358             : /* Q(x) mod t^n, x in R[t], n >= 1 */
    2359             : GEN
    2360        5159 : RgXn_eval(GEN Q, GEN x, long n)
    2361             : {
    2362        5159 :   long d = degpol(x);
    2363             :   int use_sqr;
    2364             :   struct modXn S;
    2365        5159 :   if (d == 1 && isrationalzero(gel(x,2)))
    2366             :   {
    2367        5152 :     GEN y = RgX_unscale(Q, gel(x,3));
    2368        5152 :     setvarn(y, varn(x)); return y;
    2369             :   }
    2370           7 :   S.v = varn(x);
    2371           7 :   S.n = n;
    2372           7 :   use_sqr = (d<<1) >= n;
    2373           7 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&S,&RgXn_algebra,_cmul);
    2374             : }
    2375             : 
    2376             : /* (f*g mod t^n) \ t^n2, assuming 2*n2 >= n */
    2377             : static GEN
    2378     2614484 : RgXn_mulhigh(GEN f, GEN g, long n2, long n)
    2379             : {
    2380     2614484 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2381     2614484 :   return RgX_add(RgX_mulhigh_i(fl, g, n2), RgXn_mul(fh, g, n - n2));
    2382             : }
    2383             : 
    2384             : /* (f^2 mod t^n) \ t^n2, assuming 2*n2 >= n */
    2385             : static GEN
    2386          14 : RgXn_sqrhigh(GEN f, long n2, long n)
    2387             : {
    2388          14 :   GEN F = RgX_blocks(f, n2, 2), fl = gel(F,1), fh = gel(F,2);
    2389          14 :   return RgX_add(RgX_mulhigh_i(fl, f, n2), RgXn_mul(fh, f, n - n2));
    2390             : }
    2391             : 
    2392             : static GEN
    2393     2188923 : RgXn_div_gen(GEN g, GEN f, long e)
    2394             : {
    2395             :   pari_sp av;
    2396             :   ulong mask;
    2397             :   GEN W, a;
    2398     2188923 :   long v = varn(f), n = 1;
    2399             : 
    2400     2188923 :   if (!signe(f)) pari_err_INV("RgXn_inv",f);
    2401     2188916 :   a = ginv(gel(f,2));
    2402     2188910 :   if (e == 1 && !g) return scalarpol(a, v);
    2403     2184024 :   else if (e == 2 && !g)
    2404             :   {
    2405             :     GEN b;
    2406      836927 :     if (degpol(f) <= 0 || gequal0(b = gel(f,3))) return scalarpol(a, v);
    2407      246251 :     b = gneg(b);
    2408      246246 :     if (!gequal1(a)) b = gmul(b, gsqr(a));
    2409      246245 :     return deg1pol(b, a, v);
    2410             :   }
    2411     1347097 :   av = avma;
    2412     1347097 :   W = scalarpol_shallow(a,v);
    2413     1347096 :   mask = quadratic_prec_mask(e);
    2414     3955623 :   while (mask > 1)
    2415             :   {
    2416             :     GEN u, fr;
    2417     2608527 :     long n2 = n;
    2418     2608527 :     n<<=1; if (mask & 1) n--;
    2419     2608527 :     mask >>= 1;
    2420     2608527 :     fr = RgXn_red_shallow(f, n);
    2421     2608527 :     if (mask>1 || !g)
    2422             :     {
    2423     1324547 :       u = RgXn_mul(W, RgXn_mulhigh(fr, W, n2, n), n-n2);
    2424     1324547 :       W = RgX_sub(W, RgX_shift_shallow(u, n2));
    2425             :     }
    2426             :     else
    2427             :     {
    2428     1283980 :       GEN y = RgXn_mul(g, W, n), yt =  RgXn_red_shallow(y, n-n2);
    2429     1283980 :       u = RgXn_mul(yt, RgXn_mulhigh(fr,  W, n2, n), n-n2);
    2430     1283980 :       W = RgX_sub(y, RgX_shift_shallow(u, n2));
    2431             :     }
    2432     2608527 :     if (gc_needed(av,2))
    2433             :     {
    2434           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_inv, e = %ld", n);
    2435           0 :       W = gerepileupto(av, W);
    2436             :     }
    2437             :   }
    2438     1347096 :   return W;
    2439             : }
    2440             : 
    2441             : static GEN
    2442         119 : RgXn_div_FpX(GEN x, GEN y, long e, GEN p)
    2443             : {
    2444             :   GEN r;
    2445         119 :   if (lgefint(p) == 3)
    2446             :   {
    2447         119 :     ulong pp = uel(p, 2);
    2448         119 :     if (pp == 2)
    2449          14 :       r = F2x_to_ZX(F2xn_div(RgX_to_F2x(x), RgX_to_F2x(y), e));
    2450             :     else
    2451         105 :       r = Flx_to_ZX_inplace(Flxn_div(RgX_to_Flx(x, pp), RgX_to_Flx(y, pp), e, pp));
    2452             :   }
    2453             :   else
    2454           0 :     r = FpXn_div(RgX_to_FpX(x, p), RgX_to_FpX(y, p), e, p);
    2455         119 :   return FpX_to_mod(r, p);
    2456             : }
    2457             : 
    2458             : static GEN
    2459           7 : RgXn_div_FpXQX(GEN x, GEN y, long n, GEN pol, GEN p)
    2460             : {
    2461           7 :   GEN r, T = RgX_to_FpX(pol, p);
    2462           7 :   if (signe(T) == 0) pari_err_OP("/", x, y);
    2463           7 :   r = FpXQXn_div(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), n, T, p);
    2464           7 :   return FpXQX_to_mod(r, T, p);
    2465             : }
    2466             : 
    2467             : static GEN
    2468          91 : RgXn_inv_FpX(GEN x, long e, GEN p)
    2469             : {
    2470             :   GEN r;
    2471          91 :   if (lgefint(p) == 3)
    2472             :   {
    2473          91 :     ulong pp = uel(p, 2);
    2474          91 :     if (pp == 2)
    2475          28 :       r = F2x_to_ZX(F2xn_inv(RgX_to_F2x(x), e));
    2476             :     else
    2477          63 :       r = Flx_to_ZX_inplace(Flxn_inv(RgX_to_Flx(x, pp), e, pp));
    2478             :   }
    2479             :   else
    2480           0 :     r = FpXn_inv(RgX_to_FpX(x, p), e, p);
    2481          91 :   return FpX_to_mod(r, p);
    2482             : }
    2483             : 
    2484             : static GEN
    2485           0 : RgXn_inv_FpXQX(GEN x, long n, GEN pol, GEN p)
    2486             : {
    2487           0 :   GEN r, T = RgX_to_FpX(pol, p);
    2488           0 :   if (signe(T) == 0) pari_err_OP("/", gen_1, x);
    2489           0 :   r = FpXQXn_inv(RgX_to_FpXQX(x, T, p), n, T, p);
    2490           0 :   return FpXQX_to_mod(r, T, p);
    2491             : }
    2492             : 
    2493             : #define code(t1,t2) ((t1 << 6) | t2)
    2494             : 
    2495             : static GEN
    2496      905032 : RgXn_inv_fast(GEN x, long e)
    2497             : {
    2498             :   GEN p, pol;
    2499             :   long pa;
    2500      905032 :   long t = RgX_type(x,&p,&pol,&pa);
    2501      905034 :   switch(t)
    2502             :   {
    2503          91 :     case t_INTMOD: return RgXn_inv_FpX(x, e, p);
    2504           0 :     case code(t_POLMOD, t_INTMOD):
    2505           0 :                    return RgXn_inv_FpXQX(x, e, pol, p);
    2506      904943 :     default:       return NULL;
    2507             :   }
    2508             : }
    2509             : 
    2510             : static GEN
    2511     1284106 : RgXn_div_fast(GEN x, GEN y, long e)
    2512             : {
    2513             :   GEN p, pol;
    2514             :   long pa;
    2515     1284106 :   long t = RgX_type2(x,y,&p,&pol,&pa);
    2516     1284106 :   switch(t)
    2517             :   {
    2518         119 :     case t_INTMOD: return RgXn_div_FpX(x, y, e, p);
    2519           7 :     case code(t_POLMOD, t_INTMOD):
    2520           7 :                    return RgXn_div_FpXQX(x, y, e, pol, p);
    2521     1283980 :     default:       return NULL;
    2522             :   }
    2523             : }
    2524             : #undef code
    2525             : 
    2526             : GEN
    2527     1284106 : RgXn_div_i(GEN g, GEN f, long e)
    2528             : {
    2529     1284106 :   GEN h = RgXn_div_fast(g, f, e);
    2530     1284106 :   if (h) return h;
    2531     1283980 :   return RgXn_div_gen(g, f, e);
    2532             : }
    2533             : 
    2534             : GEN
    2535      584443 : RgXn_div(GEN g, GEN f, long e)
    2536             : {
    2537      584443 :   pari_sp av = avma;
    2538      584443 :   return gerepileupto(av, RgXn_div_i(g, f, e));
    2539             : }
    2540             : 
    2541             : GEN
    2542      905032 : RgXn_inv_i(GEN f, long e)
    2543             : {
    2544      905032 :   GEN h = RgXn_inv_fast(f, e);
    2545      905034 :   if (h) return h;
    2546      904943 :   return RgXn_div_gen(NULL, f, e);
    2547             : }
    2548             : 
    2549             : GEN
    2550      807453 : RgXn_inv(GEN f, long e)
    2551             : {
    2552      807453 :   pari_sp av = avma;
    2553      807453 :   return gerepileupto(av, RgXn_inv_i(f, e));
    2554             : }
    2555             : 
    2556             : /* intformal(x^n*S) / x^(n+1) */
    2557             : static GEN
    2558       56377 : RgX_integXn(GEN x, long n)
    2559      114356 : { pari_APPLY_pol_normalized(gdivgs(gel(x,i), n+i-1)); }
    2560             : 
    2561             : GEN
    2562       52920 : RgXn_expint(GEN h, long e)
    2563             : {
    2564       52920 :   pari_sp av = avma, av2;
    2565       52920 :   long v = varn(h), n;
    2566       52920 :   GEN f = pol_1(v), g;
    2567             :   ulong mask;
    2568             : 
    2569       52920 :   if (!signe(h)) return f;
    2570       50428 :   g = pol_1(v);
    2571       50428 :   n = 1; mask = quadratic_prec_mask(e);
    2572       50428 :   av2 = avma;
    2573       56378 :   for (;mask>1;)
    2574             :   {
    2575             :     GEN u, w;
    2576       56378 :     long n2 = n;
    2577       56378 :     n<<=1; if (mask & 1) n--;
    2578       56378 :     mask >>= 1;
    2579       56378 :     u = RgXn_mul(g, RgX_mulhigh_i(f, RgXn_red_shallow(h, n2-1), n2-1), n-n2);
    2580       56377 :     u = RgX_add(u, RgX_shift_shallow(RgXn_red_shallow(h, n-1), 1-n2));
    2581       56377 :     w = RgXn_mul(f, RgX_integXn(u, n2-1), n-n2);
    2582       56378 :     f = RgX_add(f, RgX_shift_shallow(w, n2));
    2583       56377 :     if (mask<=1) break;
    2584        5950 :     u = RgXn_mul(g, RgXn_mulhigh(f, g, n2, n), n-n2);
    2585        5950 :     g = RgX_sub(g, RgX_shift_shallow(u, n2));
    2586        5950 :     if (gc_needed(av2,2))
    2587             :     {
    2588           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"RgXn_expint, e = %ld", n);
    2589           0 :       gerepileall(av2, 2, &f, &g);
    2590             :     }
    2591             :   }
    2592       50427 :   return gerepileupto(av, f);
    2593             : }
    2594             : 
    2595             : GEN
    2596           0 : RgXn_exp(GEN h, long e)
    2597             : {
    2598           0 :   long d = degpol(h);
    2599           0 :   if (d < 0) return pol_1(varn(h));
    2600           0 :   if (!d || !gequal0(gel(h,2)))
    2601           0 :     pari_err_DOMAIN("RgXn_exp","valuation", "<", gen_1, h);
    2602           0 :   return RgXn_expint(RgX_deriv(h), e);
    2603             : }
    2604             : 
    2605             : GEN
    2606         154 : RgXn_reverse(GEN f, long e)
    2607             : {
    2608         154 :   pari_sp av = avma, av2;
    2609             :   ulong mask;
    2610             :   GEN fi, a, df, W, an;
    2611         154 :   long v = varn(f), n=1;
    2612         154 :   if (degpol(f)<1 || !gequal0(gel(f,2)))
    2613           0 :     pari_err_INV("serreverse",f);
    2614         154 :   fi = ginv(gel(f,3));
    2615         154 :   a = deg1pol_shallow(fi,gen_0,v);
    2616         154 :   if (e <= 2) return gerepilecopy(av, a);
    2617         133 :   W = scalarpol(fi,v);
    2618         133 :   df = RgX_deriv(f);
    2619         133 :   mask = quadratic_prec_mask(e);
    2620         133 :   av2 = avma;
    2621         616 :   for (;mask>1;)
    2622             :   {
    2623             :     GEN u, fa, fr;
    2624         483 :     long n2 = n, rt;
    2625         483 :     n<<=1; if (mask & 1) n--;
    2626         483 :     mask >>= 1;
    2627         483 :     fr = RgXn_red_shallow(f, n);
    2628         483 :     rt = brent_kung_optpow(degpol(fr), 4, 3);
    2629         483 :     an = RgXn_powers(a, rt, n);
    2630         483 :     if (n>1)
    2631             :     {
    2632         483 :       long n4 = (n2+1)>>1;
    2633         483 :       GEN dfr = RgXn_red_shallow(df, n2);
    2634         483 :       dfr = RgX_RgXnV_eval(dfr, RgXnV_red_shallow(an, n2), n2);
    2635         483 :       u = RgX_shift(RgX_Rg_sub(RgXn_mul(W, dfr, n2), gen_1), -n4);
    2636         483 :       W = RgX_sub(W, RgX_shift(RgXn_mul(u, W, n2-n4), n4));
    2637             :     }
    2638         483 :     fa = RgX_sub(RgX_RgXnV_eval(fr, an, n), pol_x(v));
    2639         483 :     fa = RgX_shift(fa, -n2);
    2640         483 :     a = RgX_sub(a, RgX_shift(RgXn_mul(W, fa, n-n2), n2));
    2641         483 :     if (gc_needed(av2,2))
    2642             :     {
    2643           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_reverse, e = %ld", n);
    2644           0 :       gerepileall(av2, 2, &a, &W);
    2645             :     }
    2646             :   }
    2647         133 :   return gerepileupto(av, a);
    2648             : }
    2649             : 
    2650             : GEN
    2651           7 : RgXn_sqrt(GEN h, long e)
    2652             : {
    2653           7 :   pari_sp av = avma, av2;
    2654           7 :   long v = varn(h), n = 1;
    2655           7 :   GEN f = scalarpol(gen_1, v), df = f;
    2656           7 :   ulong mask = quadratic_prec_mask(e);
    2657           7 :   if (degpol(h)<0 || !gequal1(gel(h,2)))
    2658           0 :     pari_err_SQRTN("RgXn_sqrt",h);
    2659           7 :   av2 = avma;
    2660             :   while(1)
    2661           7 :   {
    2662          14 :     long n2 = n, m;
    2663             :     GEN g;
    2664          14 :     n<<=1; if (mask & 1) n--;
    2665          14 :     mask >>= 1;
    2666          14 :     m = n-n2;
    2667          14 :     g = RgX_sub(RgXn_sqrhigh(f, n2, n), RgX_shift_shallow(RgXn_red_shallow(h, n),-n2));
    2668          14 :     f = RgX_sub(f, RgX_shift_shallow(RgXn_mul(gmul2n(df, -1), g, m), n2));
    2669          14 :     if (mask==1) return gerepileupto(av, f);
    2670           7 :     g = RgXn_mul(df, RgXn_mulhigh(df, f, n2, n), m);
    2671           7 :     df = RgX_sub(df, RgX_shift_shallow(g, n2));
    2672           7 :     if (gc_needed(av2,2))
    2673             :     {
    2674           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgXn_sqrt, e = %ld", n);
    2675           0 :       gerepileall(av2, 2, &f, &df);
    2676             :     }
    2677             :   }
    2678             : }
    2679             : 
    2680             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2681             : GEN
    2682      115779 : RgXQ_powu(GEN x, ulong n, GEN T)
    2683             : {
    2684      115779 :   pari_sp av = avma;
    2685             : 
    2686      115779 :   if (!n) return pol_1(varn(x));
    2687       67938 :   if (n == 1) return RgX_copy(x);
    2688       19740 :   x = gen_powu_i(x, n, (void*)T, &_sqr, &_mul);
    2689       19740 :   return gerepilecopy(av, x);
    2690             : }
    2691             : /* x,T in Rg[X], n in N, compute lift(x^n mod T)) */
    2692             : GEN
    2693      102412 : RgXQ_pow(GEN x, GEN n, GEN T)
    2694             : {
    2695             :   pari_sp av;
    2696      102412 :   long s = signe(n);
    2697             : 
    2698      102412 :   if (!s) return pol_1(varn(x));
    2699      102412 :   if (is_pm1(n) == 1)
    2700       88307 :     return (s < 0)? RgXQ_inv(x, T): RgX_copy(x);
    2701       14105 :   av = avma;
    2702       14105 :   if (s < 0) x = RgXQ_inv(x, T);
    2703       14105 :   x = gen_pow_i(x, n, (void*)T, &_sqr, &_mul);
    2704       14105 :   return gerepilecopy(av, x);
    2705             : }
    2706             : static GEN
    2707      200566 : _ZXQsqr(void *data, GEN x) { return ZXQ_sqr(x, (GEN)data); }
    2708             : static GEN
    2709      111173 : _ZXQmul(void *data, GEN x, GEN y) { return ZXQ_mul(x,y, (GEN)data); }
    2710             : 
    2711             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2712             : GEN
    2713       11417 : ZXQ_powers(GEN x, long l, GEN T)
    2714             : {
    2715       11417 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2716       11417 :   return gen_powers(x, l, use_sqr, (void *)T,_ZXQsqr,_ZXQmul,_one);
    2717             : }
    2718             : 
    2719             : /* x,T in Z[X], n in N, compute lift(x^n mod T)) */
    2720             : GEN
    2721      168511 : ZXQ_powu(GEN x, ulong n, GEN T)
    2722             : {
    2723      168511 :   pari_sp av = avma;
    2724             : 
    2725      168511 :   if (!n) return pol_1(varn(x));
    2726      168511 :   if (n == 1) return ZX_copy(x);
    2727      113876 :   x = gen_powu_i(x, n, (void*)T, &_ZXQsqr, &_ZXQmul);
    2728      113878 :   return gerepilecopy(av, x);
    2729             : }
    2730             : 
    2731             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    2732             : GEN
    2733        8547 : RgXQ_powers(GEN x, long l, GEN T)
    2734             : {
    2735        8547 :   int use_sqr = 2*degpol(x) >= degpol(T);
    2736        8547 :   return gen_powers(x, l, use_sqr, (void *)T,_sqr,_mul,_one);
    2737             : }
    2738             : 
    2739             : GEN
    2740        7118 : RgXQV_factorback(GEN L, GEN e, GEN T)
    2741             : {
    2742        7118 :   return gen_factorback(L, e, (void*)T, &_mul, &_pow, &_one);
    2743             : }
    2744             : 
    2745             : /* a in K = Q[X]/(T), returns [a^0, ..., a^n] */
    2746             : GEN
    2747        8855 : QXQ_powers(GEN a, long n, GEN T)
    2748             : {
    2749             :   GEN den, v;
    2750        8855 :   if (!isint1(leading_coeff(T))) return RgXQ_powers(a, n, T);
    2751        8834 :   v = ZXQ_powers(Q_remove_denom(a, &den), n, T);
    2752             :   /* den*a integral; v[i+1] = (den*a)^i in K */
    2753        8834 :   if (den)
    2754             :   { /* restore denominators */
    2755        5435 :     GEN d = den;
    2756             :     long i;
    2757        5435 :     gel(v,2) = a;
    2758       27006 :     for (i=3; i<=n+1; i++) {
    2759       21571 :       d = mulii(d,den);
    2760       21571 :       gel(v,i) = RgX_Rg_div(gel(v,i), d);
    2761             :     }
    2762             :   }
    2763        8834 :   return v;
    2764             : }
    2765             : 
    2766             : static GEN
    2767        2905 : do_QXQ_eval(GEN v, long imin, GEN a, GEN T)
    2768             : {
    2769        2905 :   long l, i, m = 0;
    2770             :   GEN dz, z;
    2771        2905 :   GEN V = cgetg_copy(v, &l);
    2772        9898 :   for (i = imin; i < l; i++)
    2773             :   {
    2774        6993 :     GEN c = gel(v, i);
    2775        6993 :     if (typ(c) == t_POL) m = maxss(m, degpol(c));
    2776             :   }
    2777        2905 :   z = Q_remove_denom(QXQ_powers(a, m, T), &dz);
    2778        3136 :   for (i = 1; i < imin; i++) V[i] = v[i];
    2779        9898 :   for (i = imin; i < l; i++)
    2780             :   {
    2781        6993 :     GEN c = gel(v,i);
    2782        6993 :     if (typ(c) == t_POL) c = QX_ZXQV_eval(c, z, dz);
    2783        6993 :     gel(V,i) = c;
    2784             :   }
    2785        2905 :   return V;
    2786             : }
    2787             : /* [ s(a mod T) | s <- lift(v) ], a,T are QX, v a QXV */
    2788             : GEN
    2789        2674 : QXV_QXQ_eval(GEN v, GEN a, GEN T)
    2790        2674 : { return do_QXQ_eval(v, 1, a, T); }
    2791             : 
    2792             : GEN
    2793         231 : QXY_QXQ_evalx(GEN v, GEN a, GEN T)
    2794         231 : { return normalizepol(do_QXQ_eval(v, 2, a, T)); }
    2795             : 
    2796             : GEN
    2797        2940 : RgXQ_matrix_pow(GEN y, long n, long m, GEN P)
    2798             : {
    2799        2940 :   return RgXV_to_RgM(RgXQ_powers(y,m-1,P),n);
    2800             : }
    2801             : 
    2802             : GEN
    2803        5144 : RgXQ_norm(GEN x, GEN T)
    2804             : {
    2805             :   pari_sp av;
    2806        5144 :   long dx = degpol(x);
    2807             :   GEN L, y;
    2808        5144 :   if (degpol(T)==0) return gpowgs(x,0);
    2809        5137 :   av = avma; y = resultant(T, x);
    2810        5137 :   L = leading_coeff(T);
    2811        5137 :   if (gequal1(L) || !signe(x)) return y;
    2812           0 :   return gerepileupto(av, gdiv(y, gpowgs(L, dx)));
    2813             : }
    2814             : 
    2815             : GEN
    2816         476 : RgXQ_trace(GEN x, GEN T)
    2817             : {
    2818         476 :   pari_sp av = avma;
    2819             :   GEN dT, z;
    2820             :   long n;
    2821         476 :   if (degpol(T)==0) return gmulgs(x,0);
    2822         469 :   dT = RgX_deriv(T); n = degpol(dT);
    2823         469 :   z = RgXQ_mul(x, dT, T);
    2824         469 :   if (degpol(z)<n) return gc_const(av, gen_0);
    2825         420 :   return gerepileupto(av, gdiv(gel(z,2+n), gel(T,3+n)));
    2826             : }
    2827             : 
    2828             : GEN
    2829     2787609 : RgX_blocks(GEN P, long n, long m)
    2830             : {
    2831     2787609 :   GEN z = cgetg(m+1,t_VEC);
    2832     2787609 :   long i,j, k=2, l = lg(P);
    2833     8556405 :   for(i=1; i<=m; i++)
    2834             :   {
    2835     5768796 :     GEN zi = cgetg(n+2,t_POL);
    2836     5768796 :     zi[1] = P[1];
    2837     5768796 :     gel(z,i) = zi;
    2838    16990226 :     for(j=2; j<n+2; j++)
    2839    11221430 :       gel(zi, j) = k==l ? gen_0 : gel(P,k++);
    2840     5768796 :     zi = RgX_renormalize_lg(zi, n+2);
    2841             :   }
    2842     2787609 :   return z;
    2843             : }
    2844             : 
    2845             : /* write p(X) = e(X^2) + Xo(X^2), shallow function */
    2846             : void
    2847    49649329 : RgX_even_odd(GEN p, GEN *pe, GEN *po)
    2848             : {
    2849    49649329 :   long n = degpol(p), v = varn(p), n0, n1, i;
    2850             :   GEN p0, p1;
    2851             : 
    2852    49650654 :   if (n <= 0) { *pe = RgX_copy(p); *po = zeropol(v); return; }
    2853             : 
    2854    49557377 :   n0 = (n>>1)+1; n1 = n+1 - n0; /* n1 <= n0 <= n1+1 */
    2855    49557377 :   p0 = cgetg(n0+2, t_POL); p0[1] = evalvarn(v)|evalsigne(1);
    2856    49560047 :   p1 = cgetg(n1+2, t_POL); p1[1] = evalvarn(v)|evalsigne(1);
    2857   146476726 :   for (i=0; i<n1; i++)
    2858             :   {
    2859    96916344 :     p0[2+i] = p[2+(i<<1)];
    2860    96916344 :     p1[2+i] = p[3+(i<<1)];
    2861             :   }
    2862    49560382 :   if (n1 != n0)
    2863    17959822 :     p0[2+i] = p[2+(i<<1)];
    2864    49560382 :   *pe = normalizepol(p0);
    2865    49565988 :   *po = normalizepol(p1);
    2866             : }
    2867             : 
    2868             : /* write p(X) = a_0(X^k) + Xa_1(X^k) + ... + X^(k-1)a_{k-1}(X^k), shallow function */
    2869             : GEN
    2870       43631 : RgX_splitting(GEN p, long k)
    2871             : {
    2872       43631 :   long n = degpol(p), v = varn(p), m, i, j, l;
    2873             :   GEN r;
    2874             : 
    2875       43631 :   m = n/k;
    2876       43631 :   r = cgetg(k+1,t_VEC);
    2877      234059 :   for(i=1; i<=k; i++)
    2878             :   {
    2879      190428 :     gel(r,i) = cgetg(m+3, t_POL);
    2880      190428 :     mael(r,i,1) = evalvarn(v)|evalsigne(1);
    2881             :   }
    2882      571991 :   for (j=1, i=0, l=2; i<=n; i++)
    2883             :   {
    2884      528360 :     gmael(r,j,l) = gel(p,2+i);
    2885      528360 :     if (j==k) { j=1; l++; } else j++;
    2886             :   }
    2887      234059 :   for(i=1; i<=k; i++)
    2888      190428 :     gel(r,i) = normalizepol_lg(gel(r,i),i<j?l+1:l);
    2889       43631 :   return r;
    2890             : }
    2891             : 
    2892             : /*******************************************************************/
    2893             : /*                                                                 */
    2894             : /*                        Kronecker form                           */
    2895             : /*                                                                 */
    2896             : /*******************************************************************/
    2897             : 
    2898             : /* z in R[Y] representing an elt in R[X,Y] mod T(Y) in Kronecker form,
    2899             :  * i.e subst(lift(z), x, y^(2deg(z)-1)). Recover the "real" z, with
    2900             :  * normalized coefficients */
    2901             : GEN
    2902      186343 : Kronecker_to_mod(GEN z, GEN T)
    2903             : {
    2904      186343 :   long i,j,lx,l = lg(z), N = (degpol(T)<<1) + 1;
    2905      186343 :   GEN x, t = cgetg(N,t_POL);
    2906      186342 :   t[1] = T[1];
    2907      186342 :   lx = (l-2) / (N-2); x = cgetg(lx+3,t_POL);
    2908      186342 :   x[1] = z[1];
    2909      186342 :   T = RgX_copy(T);
    2910     1405142 :   for (i=2; i<lx+2; i++, z+= N-2)
    2911             :   {
    2912     5792556 :     for (j=2; j<N; j++) gel(t,j) = gel(z,j);
    2913     1218799 :     gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2914             :   }
    2915      186343 :   N = (l-2) % (N-2) + 2;
    2916      473291 :   for (j=2; j<N; j++) t[j] = z[j];
    2917      186343 :   gel(x,i) = mkpolmod(RgX_rem(normalizepol_lg(t,N), T), T);
    2918      186343 :   return normalizepol_lg(x, i+1);
    2919             : }
    2920             : 
    2921             : /*******************************************************************/
    2922             : /*                                                                 */
    2923             : /*                        Domain detection                         */
    2924             : /*                                                                 */
    2925             : /*******************************************************************/
    2926             : 
    2927             : static GEN
    2928      152790 : zero_FpX_mod(GEN p, long v)
    2929             : {
    2930      152790 :   GEN r = cgetg(3,t_POL);
    2931      152790 :   r[1] = evalvarn(v);
    2932      152790 :   gel(r,2) = mkintmod(gen_0, icopy(p));
    2933      152790 :   return r;
    2934             : }
    2935             : 
    2936             : static GEN
    2937      818173 : RgX_mul_FpX(GEN x, GEN y, GEN p)
    2938             : {
    2939      818173 :   pari_sp av = avma;
    2940             :   GEN r;
    2941      818173 :   if (lgefint(p) == 3)
    2942             :   {
    2943      766817 :     ulong pp = uel(p, 2);
    2944      766817 :     r = Flx_to_ZX_inplace(Flx_mul(RgX_to_Flx(x, pp),
    2945             :                                   RgX_to_Flx(y, pp), pp));
    2946             :   }
    2947             :   else
    2948       51356 :     r = FpX_mul(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    2949      818173 :   if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
    2950      703120 :   return gerepileupto(av, FpX_to_mod(r, p));
    2951             : }
    2952             : 
    2953             : static GEN
    2954         532 : zero_FpXQX_mod(GEN pol, GEN p, long v)
    2955             : {
    2956         532 :   GEN r = cgetg(3,t_POL);
    2957         532 :   r[1] = evalvarn(v);
    2958         532 :   gel(r,2) = mkpolmod(mkintmod(gen_0, icopy(p)), gcopy(pol));
    2959         532 :   return r;
    2960             : }
    2961             : 
    2962             : static GEN
    2963        2044 : RgX_mul_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    2964             : {
    2965        2044 :   pari_sp av = avma;
    2966             :   long dT;
    2967             :   GEN kx, ky, r;
    2968        2044 :   GEN T = RgX_to_FpX(pol, p);
    2969        2044 :   if (signe(T)==0) pari_err_OP("*", x, y);
    2970        2037 :   dT = degpol(T);
    2971        2037 :   kx = RgXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    2972        2037 :   ky = RgXX_to_Kronecker(RgX_to_FpXQX(y, T, p), dT);
    2973        2037 :   r = FpX_mul(kx, ky, p);
    2974        2037 :   if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
    2975        1512 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    2976             : }
    2977             : 
    2978             : static GEN
    2979      440294 : RgX_liftred(GEN x, GEN T)
    2980      440294 : { return RgXQX_red(liftpol_shallow(x), T); }
    2981             : 
    2982             : static GEN
    2983      171210 : RgX_mul_QXQX(GEN x, GEN y, GEN T)
    2984             : {
    2985      171210 :   pari_sp av = avma;
    2986      171210 :   long dT = degpol(T);
    2987      171210 :   GEN r = QX_mul(RgXX_to_Kronecker(RgX_liftred(x, T), dT),
    2988             :                  RgXX_to_Kronecker(RgX_liftred(y, T), dT));
    2989      171210 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    2990             : }
    2991             : 
    2992             : static GEN
    2993        1421 : RgX_sqr_FpX(GEN x, GEN p)
    2994             : {
    2995        1421 :   pari_sp av = avma;
    2996             :   GEN r;
    2997        1421 :   if (lgefint(p) == 3)
    2998             :   {
    2999        1217 :     ulong pp = uel(p, 2);
    3000        1217 :     r = Flx_to_ZX_inplace(Flx_sqr(RgX_to_Flx(x, pp), pp));
    3001             :   }
    3002             :   else
    3003         204 :     r = FpX_sqr(RgX_to_FpX(x, p), p);
    3004        1421 :   if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
    3005        1281 :   return gerepileupto(av, FpX_to_mod(r, p));
    3006             : }
    3007             : 
    3008             : static GEN
    3009         196 : RgX_sqr_FpXQX(GEN x, GEN pol, GEN p)
    3010             : {
    3011         196 :   pari_sp av = avma;
    3012             :   long dT;
    3013         196 :   GEN kx, r, T = RgX_to_FpX(pol, p);
    3014         196 :   if (signe(T)==0) pari_err_OP("*",x,x);
    3015         189 :   dT = degpol(T);
    3016         189 :   kx = RgXX_to_Kronecker(RgX_to_FpXQX(x, T, p), dT);
    3017         189 :   r = FpX_sqr(kx, p);
    3018         189 :   if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
    3019         189 :   return gerepileupto(av, Kronecker_to_mod(FpX_to_mod(r, p), pol));
    3020             : }
    3021             : 
    3022             : static GEN
    3023       13432 : RgX_sqr_QXQX(GEN x, GEN T)
    3024             : {
    3025       13432 :   pari_sp av = avma;
    3026       13432 :   long dT = degpol(T);
    3027       13432 :   GEN r = QX_sqr(RgXX_to_Kronecker(RgX_liftred(x, T), dT));
    3028       13432 :   return gerepileupto(av, Kronecker_to_mod(r, T));
    3029             : }
    3030             : 
    3031             : static GEN
    3032       68670 : RgX_rem_FpX(GEN x, GEN y, GEN p)
    3033             : {
    3034       68670 :   pari_sp av = avma;
    3035             :   GEN r;
    3036       68670 :   if (lgefint(p) == 3)
    3037             :   {
    3038       51880 :     ulong pp = uel(p, 2);
    3039       51880 :     r = Flx_to_ZX_inplace(Flx_rem(RgX_to_Flx(x, pp),
    3040             :                                   RgX_to_Flx(y, pp), pp));
    3041             :   }
    3042             :   else
    3043       16790 :     r = FpX_rem(RgX_to_FpX(x, p), RgX_to_FpX(y, p), p);
    3044       68670 :   if (signe(r)==0) { set_avma(av); return zero_FpX_mod(p, varn(x)); }
    3045       31073 :   return gerepileupto(av, FpX_to_mod(r, p));
    3046             : }
    3047             : 
    3048             : static GEN
    3049       42221 : RgX_rem_QXQX(GEN x, GEN y, GEN T)
    3050             : {
    3051       42221 :   pari_sp av = avma;
    3052             :   GEN r;
    3053       42221 :   r = RgXQX_rem(RgX_liftred(x, T), RgX_liftred(y, T), T);
    3054       42221 :   return gerepilecopy(av, QXQX_to_mod_shallow(r, T));
    3055             : }
    3056             : static GEN
    3057          70 : RgX_rem_FpXQX(GEN x, GEN y, GEN pol, GEN p)
    3058             : {
    3059          70 :   pari_sp av = avma;
    3060             :   GEN r;
    3061          70 :   GEN T = RgX_to_FpX(pol, p);
    3062          70 :   if (signe(T) == 0) pari_err_OP("%", x, y);
    3063          63 :   if (lgefint(p) == 3)
    3064             :   {
    3065          55 :     ulong pp = uel(p, 2);
    3066          55 :     GEN Tp = ZX_to_Flx(T, pp);
    3067          55 :     r = FlxX_to_ZXX(FlxqX_rem(RgX_to_FlxqX(x, Tp, pp),
    3068             :                               RgX_to_FlxqX(y, Tp, pp), Tp, pp));
    3069             :   }
    3070             :   else
    3071           8 :     r = FpXQX_rem(RgX_to_FpXQX(x, T, p), RgX_to_FpXQX(y, T, p), T, p);
    3072          63 :   if (signe(r)==0) { set_avma(av); return zero_FpXQX_mod(pol, p, varn(x)); }
    3073          56 :   return gerepileupto(av, FpXQX_to_mod(r, T, p));
    3074             : }
    3075             : 
    3076             : #define code(t1,t2) ((t1 << 6) | t2)
    3077             : static GEN
    3078    77697740 : RgX_mul_fast(GEN x, GEN y)
    3079             : {
    3080             :   GEN p, pol;
    3081             :   long pa;
    3082    77697740 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    3083    77698077 :   switch(t)
    3084             :   {
    3085    41496433 :     case t_INT:    return ZX_mul(x,y);
    3086     3328016 :     case t_FRAC:   return QX_mul(x,y);
    3087      104735 :     case t_FFELT:  return FFX_mul(x, y, pol);
    3088      818173 :     case t_INTMOD: return RgX_mul_FpX(x, y, p);
    3089      171210 :     case code(t_POLMOD, t_INT):
    3090             :     case code(t_POLMOD, t_FRAC):
    3091      171210 :                    return RgX_mul_QXQX(x, y, pol);
    3092        2005 :     case code(t_POLMOD, t_INTMOD):
    3093        2005 :                    return RgX_mul_FpXQX(x, y, pol, p);
    3094    31777505 :     default:       return NULL;
    3095             :   }
    3096             : }
    3097             : static GEN
    3098     1359787 : RgX_sqr_fast(GEN x)
    3099             : {
    3100             :   GEN p, pol;
    3101             :   long pa;
    3102     1359787 :   long t = RgX_type(x,&p,&pol,&pa);
    3103     1359798 :   switch(t)
    3104             :   {
    3105     1253626 :     case t_INT:    return ZX_sqr(x);
    3106       79471 :     case t_FRAC:   return QX_sqr(x);
    3107        2534 :     case t_FFELT:  return FFX_sqr(x, pol);
    3108        1421 :     case t_INTMOD: return RgX_sqr_FpX(x, p);
    3109       13432 :     case code(t_POLMOD, t_INT):
    3110             :     case code(t_POLMOD, t_FRAC):
    3111       13432 :                    return RgX_sqr_QXQX(x, pol);
    3112         193 :     case code(t_POLMOD, t_INTMOD):
    3113         193 :                    return RgX_sqr_FpXQX(x, pol, p);
    3114        9121 :     default:       return NULL;
    3115             :   }
    3116             : }
    3117             : 
    3118             : static GEN
    3119    14521440 : RgX_rem_fast(GEN x, GEN y)
    3120             : {
    3121             :   GEN p, pol;
    3122             :   long pa;
    3123    14521440 :   long t = RgX_type2(x,y, &p,&pol,&pa);
    3124    14521579 :   switch(t)
    3125             :   {
    3126     3381828 :     case t_INT:    return ZX_is_monic(y) ? ZX_rem(x,y): NULL;
    3127     1795153 :     case t_FRAC:   return RgX_is_ZX(y) && ZX_is_monic(y) ? QX_ZX_rem(x,y): NULL;
    3128          84 :     case t_FFELT:  return FFX_rem(x, y, pol);
    3129       68670 :     case t_INTMOD: return RgX_rem_FpX(x, y, p);
    3130       42221 :     case code(t_POLMOD, t_INT):
    3131             :     case code(t_POLMOD, t_FRAC):
    3132       42221 :                    return RgX_rem_QXQX(x, y, pol);
    3133          60 :     case code(t_POLMOD, t_INTMOD):
    3134          60 :                    return RgX_rem_FpXQX(x, y, pol, p);
    3135     9233564 :     default:       return NULL;
    3136             :   }
    3137             : }
    3138             : 
    3139             : #undef code
    3140             : 
    3141             : GEN
    3142    63965618 : RgX_mul(GEN x, GEN y)
    3143             : {
    3144    63965618 :   GEN z = RgX_mul_fast(x,y);
    3145    63965797 :   if (!z) z = RgX_mul_i(x,y);
    3146    63965343 :   return z;
    3147             : }
    3148             : 
    3149             : GEN
    3150     1347635 : RgX_sqr(GEN x)
    3151             : {
    3152     1347635 :   GEN z = RgX_sqr_fast(x);
    3153     1347642 :   if (!z) z = RgX_sqr_i(x);
    3154     1347642 :   return z;
    3155             : }
    3156             : 
    3157             : GEN
    3158    14521451 : RgX_rem(GEN x, GEN y)
    3159             : {
    3160    14521451 :   GEN z = RgX_rem_fast(x, y);
    3161    14521562 :   if (!z) z = RgX_divrem_i(x, y, ONLY_REM);
    3162    14521438 :   return z;
    3163             : }
    3164             : 
    3165             : static GEN
    3166           0 : _RgX_mul(void* E, GEN x, GEN y)
    3167           0 : { (void) E; return RgX_mul(x, y); }
    3168             : 
    3169             : GEN
    3170           0 : RgXV_prod(GEN V)
    3171           0 : { return gen_product(V, NULL, &_RgX_mul); }
    3172             : 
    3173             : GEN
    3174    11061244 : RgXn_mul(GEN f, GEN g, long n)
    3175             : {
    3176    11061244 :   pari_sp av = avma;
    3177    11061244 :   GEN h = RgX_mul_fast(f,g);
    3178    11061244 :   if (!h) return RgXn_mul2(f,g,n);
    3179     1783428 :   if (degpol(h) < n) return h;
    3180      382941 :   return gerepilecopy(av, RgXn_red_shallow(h, n));
    3181             : }
    3182             : 
    3183             : GEN
    3184       12152 : RgXn_sqr(GEN f, long n)
    3185             : {
    3186       12152 :   pari_sp av = avma;
    3187       12152 :   GEN g = RgX_sqr_fast(f);
    3188       12152 :   if (!g) return RgXn_sqr2(f,n);
    3189       11837 :   if (degpol(g) < n) return g;
    3190       10640 :   return gerepilecopy(av, RgXn_red_shallow(g, n));
    3191             : }
    3192             : 
    3193             : /* (f*g) \/ x^n */
    3194             : GEN
    3195     2670876 : RgX_mulhigh_i(GEN f, GEN g, long n)
    3196             : {
    3197     2670876 :   GEN h = RgX_mul_fast(f,g);
    3198     2670876 :   return h? RgX_shift_shallow(h, -n): RgX_mulhigh_i2(f,g,n);
    3199             : }
    3200             : 
    3201             : /* (f*g) \/ x^n */
    3202             : GEN
    3203           0 : RgX_sqrhigh_i(GEN f, long n)
    3204             : {
    3205           0 :   GEN h = RgX_sqr_fast(f);
    3206           0 :   return h? RgX_shift_shallow(h, -n): RgX_sqrhigh_i2(f,n);
    3207             : }

Generated by: LCOV version 1.16