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 - FlxX.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.14.0 lcov report (development 27775-aca467eab2) Lines: 1097 1236 88.8 %
Date: 2022-07-03 07:33:15 Functions: 127 145 87.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2019  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             : /***********************************************************************/
      19             : /**                                                                   **/
      20             : /**                               FlxX                                **/
      21             : /**                                                                   **/
      22             : /***********************************************************************/
      23             : 
      24             : /* FlxX are t_POL with Flx coefficients.
      25             :  * Normally the variable ordering should be respected.*/
      26             : 
      27             : /*Similar to normalizepol, in place*/
      28             : /*FlxX_renormalize=zxX_renormalize */
      29             : GEN
      30    13159736 : FlxX_renormalize(GEN /*in place*/ x, long lx)
      31             : {
      32             :   long i;
      33    17866538 :   for (i = lx-1; i>1; i--)
      34    17116383 :     if (lgpol(gel(x,i))) break;
      35    13159827 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
      36    13160487 :   setlg(x, i+1); setsigne(x, i!=1); return x;
      37             : }
      38             : 
      39             : GEN
      40      743342 : pol1_FlxX(long v, long sv)
      41             : {
      42      743342 :   GEN z = cgetg(3, t_POL);
      43      743342 :   z[1] = evalsigne(1) | evalvarn(v);
      44      743342 :   gel(z,2) = pol1_Flx(sv); return z;
      45             : }
      46             : 
      47             : GEN
      48       11287 : polx_FlxX(long v, long sv)
      49             : {
      50       11287 :   GEN z = cgetg(4, t_POL);
      51       11287 :   z[1] = evalsigne(1) | evalvarn(v);
      52       11287 :   gel(z,2) = pol0_Flx(sv);
      53       11287 :   gel(z,3) = pol1_Flx(sv); return z;
      54             : }
      55             : 
      56             : long
      57     2195852 : FlxY_degreex(GEN b)
      58             : {
      59     2195852 :   long deg = 0, i;
      60     2195852 :   if (!signe(b)) return -1;
      61     8116381 :   for (i = 2; i < lg(b); ++i)
      62     5920528 :     deg = maxss(deg, degpol(gel(b, i)));
      63     2195853 :   return deg;
      64             : }
      65             : 
      66             : /*Lift coefficient of B to constant Flx, to give a FlxY*/
      67             : GEN
      68        2644 : Fly_to_FlxY(GEN B, long sv)
      69             : {
      70        2644 :   long lb=lg(B);
      71             :   long i;
      72        2644 :   GEN b=cgetg(lb,t_POL);
      73        2646 :   b[1]=evalsigne(1)|(((ulong)B[1])&VARNBITS);
      74       68929 :   for (i=2; i<lb; i++)
      75       66301 :     gel(b,i) = Fl_to_Flx(B[i], sv);
      76        2628 :   return FlxX_renormalize(b, lb);
      77             : }
      78             : 
      79             : GEN
      80     2000130 : zxX_to_FlxX(GEN B, ulong p)
      81             : {
      82     2000130 :   long i, lb = lg(B);
      83     2000130 :   GEN b = cgetg(lb,t_POL);
      84     6508918 :   for (i=2; i<lb; i++)
      85     4508788 :     gel(b,i) = zx_to_Flx(gel(B,i), p);
      86     2000130 :   b[1] = B[1]; return FlxX_renormalize(b, lb);
      87             : }
      88             : 
      89             : GEN
      90     2710121 : FlxX_to_ZXX(GEN B)
      91             : {
      92     2710121 :   long i, lb = lg(B);
      93     2710121 :   GEN b = cgetg(lb,t_POL);
      94     8639925 :   for (i=2; i<lb; i++)
      95             :   {
      96     5931504 :     GEN c = gel(B,i);
      97     5931504 :     switch(lgpol(c))
      98             :     {
      99      265273 :       case 0:  c = gen_0; break;
     100      725271 :       case 1:  c = utoi(c[2]); break;
     101     4941007 :       default: c = Flx_to_ZX(c); break;
     102             :     }
     103     5929415 :     gel(b,i) = c;
     104             :   }
     105     2708421 :   b[1] = B[1]; return b;
     106             : }
     107             : 
     108             : /* Note: v is used _only_ for the t_INT. It must match
     109             :  * the variable of any t_POL coefficients. */
     110             : GEN
     111     2725278 : ZXX_to_FlxX(GEN B, ulong p, long v)
     112             : {
     113     2725278 :   long lb=lg(B);
     114             :   long i;
     115     2725278 :   GEN b=cgetg(lb,t_POL);
     116     2725161 :   b[1]=evalsigne(1)|(((ulong)B[1])&VARNBITS);
     117    13284031 :   for (i=2; i<lb; i++)
     118    10560891 :     switch (typ(gel(B,i)))
     119             :     {
     120     2040757 :     case t_INT:
     121     2040757 :       gel(b,i) = Z_to_Flx(gel(B,i), p, evalvarn(v));
     122     2040598 :       break;
     123     8522945 :     case t_POL:
     124     8522945 :       gel(b,i) = ZX_to_Flx(gel(B,i), p);
     125     8521083 :       break;
     126             :     }
     127    13282010 :   return FlxX_renormalize(b, lb);
     128             : }
     129             : 
     130             : GEN
     131           0 : ZXXV_to_FlxXV(GEN x, ulong p, long v)
     132           0 : { pari_APPLY_type(t_VEC, ZXX_to_FlxX(gel(x,i), p, v)) }
     133             : 
     134             : GEN
     135         320 : ZXXT_to_FlxXT(GEN x, ulong p, long v)
     136             : {
     137         320 :   if (typ(x) == t_POL)
     138         306 :     return ZXX_to_FlxX(x, p, v);
     139             :   else
     140          42 :     pari_APPLY_type(t_VEC, ZXXT_to_FlxXT(gel(x,i), p, v))
     141             : }
     142             : 
     143             : GEN
     144      296261 : FlxX_to_FlxC(GEN x, long N, long sv)
     145             : {
     146             :   long i, l;
     147             :   GEN z;
     148      296261 :   l = lg(x)-1; x++;
     149      296261 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     150      296261 :   z = cgetg(N+1,t_COL);
     151     2180812 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     152     3384635 :   for (   ; i<=N; i++) gel(z,i) = pol0_Flx(sv);
     153      296261 :   return z;
     154             : }
     155             : 
     156             : /* matrix whose entries are given by the coeffs of the polynomial v in
     157             :  * two variables (considered as degree n polynomials) */
     158             : GEN
     159       52952 : FlxX_to_Flm(GEN v, long n)
     160             : {
     161       52952 :   long j, N = lg(v)-1;
     162       52952 :   GEN y = cgetg(N, t_MAT);
     163       52952 :   v++;
     164      182299 :   for (j=1; j<N; j++) gel(y,j) = Flx_to_Flv(gel(v,j), n);
     165       52945 :   return y;
     166             : }
     167             : 
     168             : GEN
     169      150750 : FlxX_to_Flx(GEN f)
     170             : {
     171      150750 :   long i, l = lg(f);
     172      150750 :   GEN V = cgetg(l, t_VECSMALL);
     173      150752 :   V[1] = ((ulong)f[1])&VARNBITS;
     174     2294063 :   for(i=2; i<l; i++)
     175     2143311 :     V[i] = lgpol(gel(f,i)) ? mael(f,i,2): 0L;
     176      150752 :   return V;
     177             : }
     178             : 
     179             : GEN
     180      220546 : Flm_to_FlxX(GEN x, long v,long w)
     181             : {
     182      220546 :   long j, lx = lg(x);
     183      220546 :   GEN y = cgetg(lx+1, t_POL);
     184      220544 :   y[1]=evalsigne(1) | v;
     185      220544 :   y++;
     186      812056 :   for (j=1; j<lx; j++) gel(y,j) = Flv_to_Flx(gel(x,j), w);
     187      220505 :   return FlxX_renormalize(--y, lx+1);
     188             : }
     189             : 
     190             : /* P(X,Y) --> P(Y,X), n is the degree in Y */
     191             : GEN
     192      183010 : FlxX_swap(GEN x, long n, long ws)
     193             : {
     194      183010 :   long j, lx = lg(x), ly = n+3;
     195      183010 :   GEN y = cgetg(ly, t_POL);
     196      183007 :   y[1] = x[1];
     197     1172532 :   for (j=2; j<ly; j++)
     198             :   {
     199             :     long k;
     200      989556 :     GEN p1 = cgetg(lx, t_VECSMALL);
     201      989585 :     p1[1] = ws;
     202    11220172 :     for (k=2; k<lx; k++)
     203    10230587 :       if (j<lg(gel(x,k)))
     204     7194996 :         p1[k] = mael(x,k,j);
     205             :       else
     206     3035591 :         p1[k] = 0;
     207      989585 :     gel(y,j) = Flx_renormalize(p1,lx);
     208             :   }
     209      182976 :   return FlxX_renormalize(y,ly);
     210             : }
     211             : 
     212             : static GEN
     213     3015346 : zxX_to_Kronecker_spec(GEN P, long lp, long n)
     214             : { /* P(X) = sum Pi(Y) * X^i, return P( Y^(2n-1) ) */
     215     3015346 :   long i, j, k, l, N = (n<<1) + 1;
     216     3015346 :   GEN y = cgetg((N-2)*lp + 2, t_VECSMALL) + 2;
     217    22653170 :   for (k=i=0; i<lp; i++)
     218             :   {
     219    22626953 :     GEN c = gel(P,i);
     220    22626953 :     l = lg(c);
     221    22626953 :     if (l-3 >= n)
     222           0 :       pari_err_BUG("zxX_to_Kronecker, P is not reduced mod Q");
     223   129578443 :     for (j=2; j < l; j++) y[k++] = c[j];
     224    22626960 :     if (i == lp-1) break;
     225   175042487 :     for (   ; j < N; j++) y[k++] = 0;
     226             :   }
     227     3015335 :   y -= 2;
     228     3015335 :   y[1] = 0; setlg(y, k+2); return y;
     229             : }
     230             : 
     231             : GEN
     232     2491003 : zxX_to_Kronecker(GEN P, GEN Q)
     233             : {
     234     2491003 :   GEN z = zxX_to_Kronecker_spec(P+2, lg(P)-2, degpol(Q));
     235     2491000 :   z[1] = P[1]; return z;
     236             : }
     237             : 
     238             : GEN
     239      262416 : FlxX_add(GEN x, GEN y, ulong p)
     240             : {
     241             :   long i,lz;
     242             :   GEN z;
     243      262416 :   long lx=lg(x);
     244      262416 :   long ly=lg(y);
     245      262416 :   if (ly>lx) swapspec(x,y, lx,ly);
     246      262416 :   lz = lx; z = cgetg(lz, t_POL); z[1]=x[1];
     247     2145109 :   for (i=2; i<ly; i++) gel(z,i) = Flx_add(gel(x,i), gel(y,i), p);
     248     1766625 :   for (   ; i<lx; i++) gel(z,i) = Flx_copy(gel(x,i));
     249      262409 :   return FlxX_renormalize(z, lz);
     250             : }
     251             : 
     252             : GEN
     253         364 : FlxX_Flx_add(GEN y, GEN x, ulong p)
     254             : {
     255         364 :   long i, lz = lg(y);
     256             :   GEN z;
     257         364 :   if (signe(y) == 0) return scalarpol(x, varn(y));
     258         364 :   z = cgetg(lz,t_POL); z[1] = y[1];
     259         364 :   gel(z,2) = Flx_add(gel(y,2), x, p);
     260         364 :   if (lz == 3) z = FlxX_renormalize(z,lz);
     261             :   else
     262        1211 :     for(i=3;i<lz;i++) gel(z,i) = Flx_copy(gel(y,i));
     263         364 :   return z;
     264             : }
     265             : 
     266             : GEN
     267       10683 : FlxX_Flx_sub(GEN y, GEN x, ulong p)
     268             : {
     269       10683 :   long i, lz = lg(y);
     270             :   GEN z;
     271       10683 :   if (signe(y) == 0) return scalarpol(x, varn(y));
     272       10683 :   z = cgetg(lz,t_POL); z[1] = y[1];
     273       10683 :   gel(z,2) = Flx_sub(gel(y,2), x, p);
     274       10683 :   if (lz == 3) z = FlxX_renormalize(z,lz);
     275             :   else
     276       81389 :     for(i=3;i<lz;i++) gel(z,i) = Flx_copy(gel(y,i));
     277       10683 :   return z;
     278             : }
     279             : 
     280             : GEN
     281        1936 : FlxX_neg(GEN x, ulong p)
     282             : {
     283        1936 :   long i, lx=lg(x);
     284        1936 :   GEN z = cgetg(lx, t_POL);
     285        1936 :   z[1]=x[1];
     286       39168 :   for (i=2; i<lx; i++) gel(z,i) = Flx_neg(gel(x,i), p);
     287        1936 :   return z;
     288             : }
     289             : 
     290             : GEN
     291         219 : FlxX_Fl_mul(GEN x, ulong y, ulong p)
     292             : {
     293         219 :   long i, lx=lg(x);
     294         219 :   GEN z = cgetg(lx, t_POL);
     295         219 :   z[1]=x[1];
     296        1047 :   for (i=2; i<lx; i++) gel(z,i) = Flx_Fl_mul(gel(x,i), y, p);
     297         219 :   return FlxX_renormalize(z, lx);
     298             : }
     299             : 
     300             : GEN
     301           0 : FlxX_triple(GEN x, ulong p)
     302             : {
     303           0 :   long i, lx=lg(x);
     304           0 :   GEN z = cgetg(lx, t_POL);
     305           0 :   z[1]=x[1];
     306           0 :   for (i=2; i<lx; i++) gel(z,i) = Flx_triple(gel(x,i), p);
     307           0 :   return FlxX_renormalize(z, lx);
     308             : }
     309             : 
     310             : GEN
     311         219 : FlxX_double(GEN x, ulong p)
     312             : {
     313         219 :   long i, lx=lg(x);
     314         219 :   GEN z = cgetg(lx, t_POL);
     315         219 :   z[1]=x[1];
     316        1752 :   for (i=2; i<lx; i++) gel(z,i) = Flx_double(gel(x,i), p);
     317         219 :   return FlxX_renormalize(z, lx);
     318             : }
     319             : 
     320             : GEN
     321       79031 : FlxX_deriv(GEN z, ulong p)
     322             : {
     323       79031 :   long i,l = lg(z)-1;
     324             :   GEN x;
     325       79031 :   if (l < 2) l = 2;
     326       79031 :   x = cgetg(l, t_POL); x[1] = z[1];
     327      879444 :   for (i=2; i<l; i++) gel(x,i) = Flx_mulu(gel(z,i+1), (ulong) i-1, p);
     328       79027 :   return FlxX_renormalize(x,l);
     329             : }
     330             : 
     331             : GEN
     332           0 : FlxX_translate1(GEN P, long p, long n)
     333             : {
     334             :   GEN Q;
     335           0 :   long i, l, ws, lP = lgpol(P);
     336           0 :   if (!lP) return gcopy(P);
     337           0 :   ws = mael(P,2,1);
     338           0 :   Q = FlxX_swap(P, n, ws);
     339           0 :   l = lg(Q);
     340           0 :   for (i=2; i<l; i++) gel(Q, i) = Flx_translate1(gel(Q, i), p);
     341           0 :   return FlxX_swap(Q, lP, ws);
     342             : }
     343             : 
     344             : GEN
     345           0 : zlxX_translate1(GEN P, long p, long e, long n)
     346             : {
     347             :   GEN Q;
     348           0 :   long i, l, ws, lP = lgpol(P);
     349           0 :   if (!lP) return gcopy(P);
     350           0 :   ws = mael(P,2,1);
     351           0 :   Q = FlxX_swap(P, n, ws);
     352           0 :   l = lg(Q);
     353           0 :   for (i=2; i<l; i++) gel(Q, i) = zlx_translate1(gel(Q, i), p, e);
     354           0 :   return FlxX_swap(Q, lP, ws);
     355             : }
     356             : 
     357             : static GEN
     358       96717 : FlxX_subspec(GEN x, GEN y, ulong p, long lx, long ly)
     359             : {
     360             :   long i,lz;
     361             :   GEN z;
     362             : 
     363       96717 :   if (ly <= lx)
     364             :   {
     365       96717 :     lz = lx+2; z = cgetg(lz, t_POL);
     366     3009164 :     for (i=0; i<ly; i++) gel(z,i+2) = Flx_sub(gel(x,i),gel(y,i),p);
     367       98579 :     for (   ; i<lx; i++) gel(z,i+2) = Flx_copy(gel(x,i));
     368             :   }
     369             :   else
     370             :   {
     371           0 :     lz = ly+2; z = cgetg(lz, t_POL);
     372           0 :     for (i=0; i<lx; i++) gel(z,i+2) = Flx_sub(gel(x,i),gel(y,i),p);
     373           0 :     for (   ; i<ly; i++) gel(z,i+2) = Flx_neg(gel(y,i),p);
     374             :   }
     375       96717 :   z[1] = 0; return FlxX_renormalize(z, lz);
     376             : }
     377             : 
     378             : GEN
     379      569487 : FlxX_sub(GEN x, GEN y, ulong p)
     380             : {
     381             :   long lx,ly,i,lz;
     382             :   GEN z;
     383      569487 :   lx = lg(x); ly = lg(y);
     384      569487 :   lz=maxss(lx,ly);
     385      569487 :   z = cgetg(lz,t_POL);
     386      569482 :   if (lx >= ly)
     387             :   {
     388       85356 :     z[1] = x[1];
     389      343476 :     for (i=2; i<ly; i++) gel(z,i) = Flx_sub(gel(x,i),gel(y,i),p);
     390      313850 :     for (   ; i<lx; i++) gel(z,i) = Flx_copy(gel(x,i));
     391       85356 :     if (lx==ly) z = FlxX_renormalize(z, lz);
     392             :   }
     393             :   else
     394             :   {
     395      484126 :     z[1] = y[1];
     396     1209325 :     for (i=2; i<lx; i++) gel(z,i) = Flx_sub(gel(x,i),gel(y,i),p);
     397     1252109 :     for (   ; i<ly; i++) gel(z,i) = Flx_neg(gel(y,i),p);
     398             :   }
     399      569436 :   if (!lgpol(z)) { set_avma((pari_sp)(z + lz)); z = pol_0(varn(x)); }
     400      569450 :   return z;
     401             : }
     402             : 
     403             : GEN
     404           0 : FlxX_Flx_mul(GEN P, GEN U, ulong p)
     405             : {
     406           0 :   long i, lP = lg(P);
     407           0 :   GEN res = cgetg(lP,t_POL);
     408           0 :   res[1] = P[1];
     409           0 :   for(i=2; i<lP; i++)
     410           0 :     gel(res,i) = Flx_mul(U,gel(P,i), p);
     411           0 :   return FlxX_renormalize(res, lP);
     412             : }
     413             : 
     414             : GEN
     415     1268238 : FlxY_evalx(GEN Q, ulong x, ulong p)
     416             : {
     417             :   GEN z;
     418     1268238 :   long i, lb = lg(Q);
     419     1268238 :   z = cgetg(lb,t_VECSMALL); z[1] = evalvarn(varn(Q));
     420    10003957 :   for (i=2; i<lb; i++) z[i] = Flx_eval(gel(Q,i), x, p);
     421     1268496 :   return Flx_renormalize(z, lb);
     422             : }
     423             : 
     424             : GEN
     425           0 : FlxY_Flx_translate(GEN P, GEN c, ulong p)
     426             : {
     427           0 :   pari_sp av = avma;
     428             :   GEN Q;
     429             :   long i, k, n;
     430             : 
     431           0 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     432           0 :   Q = leafcopy(P); n = degpol(P);
     433           0 :   for (i=1; i<=n; i++)
     434             :   {
     435           0 :     for (k=n-i; k<n; k++)
     436           0 :       gel(Q,2+k) = Flx_add(gel(Q,2+k), Flx_mul(gel(Q,2+k+1), c, p), p);
     437           0 :     if (gc_needed(av,2))
     438             :     {
     439           0 :       if(DEBUGMEM>1)
     440           0 :         pari_warn(warnmem,"FlxY_Flx_translate, i = %ld/%ld", i,n);
     441           0 :       Q = gerepilecopy(av, Q);
     442             :     }
     443             :   }
     444           0 :   return gerepilecopy(av, Q);
     445             : }
     446             : 
     447             : GEN
     448     8243502 : FlxY_evalx_powers_pre(GEN pol, GEN ypowers, ulong p, ulong pi)
     449             : {
     450     8243502 :   long i, len = lg(pol);
     451     8243502 :   GEN res = cgetg(len, t_VECSMALL);
     452     8243502 :   res[1] = pol[1] & VARNBITS;
     453    27919869 :   for (i = 2; i < len; ++i)
     454    19676367 :     res[i] = Flx_eval_powers_pre(gel(pol, i), ypowers, p, pi);
     455     8243502 :   return Flx_renormalize(res, len);
     456             : }
     457             : 
     458             : ulong
     459     5519294 : FlxY_eval_powers_pre(GEN pol, GEN ypowers, GEN xpowers, ulong p, ulong pi)
     460             : {
     461     5519294 :   pari_sp av = avma;
     462     5519294 :   GEN t = FlxY_evalx_powers_pre(pol, ypowers, p, pi);
     463     5519294 :   return gc_ulong(av, Flx_eval_powers_pre(t, xpowers, p, pi));
     464             : }
     465             : 
     466             : GEN
     467      125686 : FlxY_FlxqV_evalx(GEN P, GEN x, GEN T, ulong p)
     468             : {
     469      125686 :   long i, lP = lg(P);
     470      125686 :   GEN res = cgetg(lP,t_POL);
     471      125686 :   res[1] = P[1];
     472      862894 :   for(i=2; i<lP; i++)
     473      737208 :     gel(res,i) = Flx_FlxqV_eval(gel(P,i), x, T, p);
     474      125686 :   return FlxX_renormalize(res, lP);
     475             : }
     476             : 
     477             : GEN
     478           0 : FlxY_Flxq_evalx(GEN P, GEN x, GEN T, ulong p)
     479             : {
     480           0 :   pari_sp av = avma;
     481           0 :   long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(P),1);
     482           0 :   GEN xp = Flxq_powers(x, n, T, p);
     483           0 :   return gerepileupto(av, FlxY_FlxqV_evalx(P, xp, T, p));
     484             : }
     485             : 
     486             : GEN
     487        7905 : FlxY_Flx_div(GEN x, GEN y, ulong p)
     488             : {
     489             :   long i, l;
     490             :   GEN z;
     491        7905 :   if (degpol(y) == 0)
     492             :   {
     493        5832 :     ulong t = uel(y,2);
     494        5832 :     if (t == 1) return x;
     495           0 :     t = Fl_inv(t, p);
     496           0 :     z = cgetg_copy(x, &l); z[1] = x[1];
     497           0 :     for (i=2; i<l; i++) gel(z,i) = Flx_Fl_mul(gel(x,i),t,p);
     498             :   }
     499             :   else
     500             :   {
     501        2073 :     z = cgetg_copy(x, &l); z[1] = x[1];
     502        4154 :     for (i=2; i<l; i++) gel(z,i) = Flx_div(gel(x,i),y,p);
     503             :   }
     504        2077 :   return z;
     505             : }
     506             : 
     507             : GEN
     508       38225 : FlxX_shift(GEN a, long n, long vs)
     509             : {
     510       38225 :   long i, l = lg(a);
     511             :   GEN  b;
     512       38225 :   if (l == 2 || !n) return a;
     513       33308 :   l += n;
     514       33308 :   if (n < 0)
     515             :   {
     516       20022 :     if (l <= 2) return pol_0(varn(a));
     517       15434 :     b = cgetg(l, t_POL); b[1] = a[1];
     518       15433 :     a -= n;
     519      242758 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     520             :   } else {
     521       13286 :     b = cgetg(l, t_POL); b[1] = a[1];
     522       13287 :     a -= n; n += 2;
     523       97979 :     for (i=2; i<n; i++) gel(b,i) = pol0_Flx(vs);
     524       90009 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     525             :   }
     526       28717 :   return b;
     527             : }
     528             : 
     529             : GEN
     530       10637 : FlxX_blocks(GEN P, long n, long m, long vs)
     531             : {
     532       10637 :   GEN z = cgetg(m+1,t_VEC);
     533       10637 :   long i,j, k=2, l = lg(P);
     534       31913 :   for(i=1; i<=m; i++)
     535             :   {
     536       21275 :     GEN zi = cgetg(n+2,t_POL);
     537       21274 :     zi[1] = P[1];
     538       21274 :     gel(z,i) = zi;
     539      143490 :     for(j=2; j<n+2; j++)
     540      122280 :       gel(zi, j) = k==l ? pol0_Flx(vs) : gel(P,k++);
     541       21210 :     zi = FlxX_renormalize(zi, n+2);
     542             :   }
     543       10638 :   return z;
     544             : }
     545             : 
     546             : static GEN
     547      206431 : FlxX_recipspec(GEN x, long l, long n, long vs)
     548             : {
     549             :   long i;
     550      206431 :   GEN z = cgetg(n+2,t_POL);
     551      206431 :   z[1] = 0; z += 2;
     552     4654713 :   for(i=0; i<l; i++)
     553     4448282 :     gel(z,n-i-1) = Flx_copy(gel(x,i));
     554      213350 :   for(   ; i<n; i++)
     555        6919 :     gel(z,n-i-1) = pol0_Flx(vs);
     556      206431 :   return FlxX_renormalize(z-2,n+2);
     557             : }
     558             : 
     559             : GEN
     560        1846 : FlxX_invLaplace(GEN x, ulong p)
     561             : {
     562        1846 :   long i, d = degpol(x);
     563             :   GEN y;
     564             :   ulong t;
     565        1846 :   if (d <= 1) return gcopy(x);
     566        1839 :   t = Fl_inv(factorial_Fl(d, p), p);
     567        1839 :   y = cgetg(d+3, t_POL);
     568        1839 :   y[1] = x[1];
     569       43723 :   for (i=d; i>=2; i--)
     570             :   {
     571       41884 :     gel(y,i+2) = Flx_Fl_mul(gel(x,i+2), t, p);
     572       41881 :     t = Fl_mul(t, i, p);
     573             :   }
     574        1839 :   gel(y,3) = Flx_copy(gel(x,3));
     575        1839 :   gel(y,2) = Flx_copy(gel(x,2));
     576        1839 :   return FlxX_renormalize(y, d+3);
     577             : }
     578             : 
     579             : GEN
     580         923 : FlxX_Laplace(GEN x, ulong p)
     581             : {
     582         923 :   long i, d = degpol(x);
     583         923 :   ulong t = 1;
     584             :   GEN y;
     585         923 :   if (d <= 1) return gcopy(x);
     586         923 :   y = cgetg(d+3, t_POL);
     587         923 :   y[1] = x[1];
     588         923 :   gel(y,2) = Flx_copy(gel(x,2));
     589         923 :   gel(y,3) = Flx_copy(gel(x,3));
     590       21930 :   for (i=2; i<=d; i++)
     591             :   {
     592       21007 :     t = Fl_mul(t, i%p, p);
     593       21007 :     gel(y,i+2) = Flx_Fl_mul(gel(x,i+2), t, p);
     594             :   }
     595         923 :   return FlxX_renormalize(y, d+3);
     596             : }
     597             : 
     598             : /***********************************************************************/
     599             : /**                                                                   **/
     600             : /**                               FlxXV                               **/
     601             : /**                                                                   **/
     602             : /***********************************************************************/
     603             : 
     604             : GEN
     605           0 : FlxXC_sub(GEN x, GEN y, ulong p)
     606           0 : { pari_APPLY_same(FlxX_sub(gel(x, i), gel(y,i), p)) }
     607             : 
     608             : static GEN
     609      127150 : FlxXV_to_FlxM_lg(GEN x, long m, long n, long sv)
     610             : {
     611             :   long i;
     612      127150 :   GEN y = cgetg(n+1, t_MAT);
     613      423411 :   for (i=1; i<=n; i++) gel(y,i) = FlxX_to_FlxC(gel(x,i), m, sv);
     614      127150 :   return y;
     615             : }
     616             : 
     617             : GEN
     618           0 : FlxXV_to_FlxM(GEN v, long n, long sv)
     619           0 : { return FlxXV_to_FlxM_lg(v, n, lg(v)-1, sv); }
     620             : 
     621             : GEN
     622       12292 : FlxXC_to_ZXXC(GEN x)
     623       61880 : { pari_APPLY_type(t_COL, FlxX_to_ZXX(gel(x,i))) }
     624             : 
     625             : GEN
     626           0 : FlxXM_to_ZXXM(GEN x)
     627           0 : { pari_APPLY_same(FlxXC_to_ZXXC(gel(x,i))) }
     628             : 
     629             : /***********************************************************************/
     630             : /**                                                                   **/
     631             : /**                               FlxqX                               **/
     632             : /**                                                                   **/
     633             : /***********************************************************************/
     634             : 
     635             : static GEN
     636     3422245 : get_FlxqX_red(GEN T, GEN *B)
     637             : {
     638     3422245 :   if (typ(T)!=t_VEC) { *B=NULL; return T; }
     639      125547 :   *B = gel(T,1); return gel(T,2);
     640             : }
     641             : 
     642             : GEN
     643      131780 : RgX_to_FlxqX(GEN x, GEN T, ulong p)
     644             : {
     645      131780 :   long i, l = lg(x);
     646      131780 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     647     2184765 :   for (i = 2; i < l; i++)
     648     2052995 :     gel(z,i) = Rg_to_Flxq(gel(x,i), T, p);
     649      131770 :   return FlxX_renormalize(z, l);
     650             : }
     651             : 
     652             : /* FlxqX are t_POL with Flxq coefficients.
     653             :  * Normally the variable ordering should be respected.*/
     654             : 
     655             : GEN
     656         585 : random_FlxqX(long d1, long v, GEN T, ulong p)
     657             : {
     658         585 :   long dT = get_Flx_degree(T), vT = get_Flx_var(T);
     659         585 :   long i, d = d1+2;
     660         585 :   GEN y = cgetg(d,t_POL); y[1] = evalsigne(1) | evalvarn(v);
     661        3146 :   for (i=2; i<d; i++) gel(y,i) = random_Flx(dT, vT, p);
     662         585 :   return FlxX_renormalize(y,d);
     663             : }
     664             : 
     665             : /*Not stack clean*/
     666             : GEN
     667     1660420 : Kronecker_to_FlxqX(GEN z, GEN T, ulong p)
     668             : {
     669     1660420 :   long i,j,lx,l, N = (get_Flx_degree(T)<<1) + 1;
     670     1660419 :   GEN x, t = cgetg(N,t_VECSMALL);
     671     1660417 :   t[1] = get_Flx_var(T);
     672     1660411 :   l = lg(z); lx = (l-2) / (N-2);
     673     1660411 :   x = cgetg(lx+3,t_POL);
     674     1660414 :   x[1] = z[1];
     675    23099931 :   for (i=2; i<lx+2; i++)
     676             :   {
     677   293203080 :     for (j=2; j<N; j++) t[j] = z[j];
     678    21439552 :     z += (N-2);
     679    21439552 :     gel(x,i) = Flx_rem(Flx_renormalize(t,N), T,p);
     680             :   }
     681     1660379 :   N = (l-2) % (N-2) + 2;
     682     5906005 :   for (j=2; j<N; j++) t[j] = z[j];
     683     1660379 :   gel(x,i) = Flx_rem(Flx_renormalize(t,N), T,p);
     684     1660349 :   return FlxX_renormalize(x, i+1);
     685             : }
     686             : 
     687             : GEN
     688     1193182 : FlxqX_red(GEN z, GEN T, ulong p)
     689             : {
     690             :   GEN res;
     691     1193182 :   long i, l = lg(z);
     692     1193182 :   res = cgetg(l,t_POL); res[1] = z[1];
     693    12140391 :   for(i=2;i<l;i++) gel(res,i) = Flx_rem(gel(z,i),T,p);
     694     1193138 :   return FlxX_renormalize(res,l);
     695             : }
     696             : 
     697             : static GEN
     698      262178 : FlxqX_mulspec(GEN x, GEN y, GEN T, ulong p, long lx, long ly)
     699             : {
     700      262178 :   pari_sp ltop=avma;
     701             :   GEN z,kx,ky;
     702      262178 :   long dT =  get_Flx_degree(T);
     703      262178 :   kx= zxX_to_Kronecker_spec(x,lx,dT);
     704      262178 :   ky= zxX_to_Kronecker_spec(y,ly,dT);
     705      262178 :   z = Flx_mul(ky, kx, p);
     706      262178 :   z = Kronecker_to_FlxqX(z,T,p);
     707      262178 :   return gerepileupto(ltop,z);
     708             : }
     709             : 
     710             : GEN
     711     1092727 : FlxqX_mul(GEN x, GEN y, GEN T, ulong p)
     712             : {
     713     1092727 :   pari_sp ltop=avma;
     714     1092727 :   GEN z, kx, ky, Tm = get_Flx_mod(T);
     715     1092725 :   kx= zxX_to_Kronecker(x, Tm);
     716     1092736 :   ky= zxX_to_Kronecker(y, Tm);
     717     1092734 :   z = Flx_mul(ky, kx, p);
     718     1092694 :   z = Kronecker_to_FlxqX(z, T, p);
     719     1092681 :   return gerepileupto(ltop, z);
     720             : }
     721             : 
     722             : GEN
     723      305574 : FlxqX_sqr(GEN x, GEN T, ulong p)
     724             : {
     725      305574 :   pari_sp ltop=avma;
     726             :   GEN z,kx;
     727      305574 :   kx= zxX_to_Kronecker(x,get_Flx_mod(T));
     728      305573 :   z = Flx_sqr(kx, p);
     729      305571 :   z = Kronecker_to_FlxqX(z,T,p);
     730      305568 :   return gerepileupto(ltop,z);
     731             : }
     732             : 
     733             : GEN
     734      154863 : FlxqX_Flxq_mul(GEN P, GEN U, GEN T, ulong p)
     735             : {
     736      154863 :   long i, lP = lg(P);
     737      154863 :   GEN res = cgetg(lP,t_POL);
     738      154863 :   res[1] = P[1];
     739      552055 :   for(i=2; i<lP; i++)
     740      397214 :     gel(res,i) = Flxq_mul(U,gel(P,i), T,p);
     741      154841 :   return FlxX_renormalize(res, lP);
     742             : }
     743             : GEN
     744      582279 : FlxqX_Flxq_mul_to_monic(GEN P, GEN U, GEN T, ulong p)
     745             : {
     746      582279 :   long i, lP = lg(P);
     747      582279 :   GEN res = cgetg(lP,t_POL);
     748      582272 :   res[1] = P[1];
     749     3302666 :   for(i=2; i<lP-1; i++) gel(res,i) = Flxq_mul(U,gel(P,i), T,p);
     750      581772 :   gel(res,lP-1) = pol1_Flx(get_Flx_var(T));
     751      582294 :   return FlxX_renormalize(res, lP);
     752             : }
     753             : 
     754             : GEN
     755      183958 : FlxqX_normalize(GEN z, GEN T, ulong p)
     756             : {
     757      183958 :   GEN p1 = leading_coeff(z);
     758      183958 :   if (!lgpol(z) || (!degpol(p1) && p1[1] == 1)) return z;
     759      183938 :   return FlxqX_Flxq_mul_to_monic(z, Flxq_inv(p1,T,p), T,p);
     760             : }
     761             : 
     762             : /* x and y in Z[Y][X]. Assume T irreducible mod p */
     763             : static GEN
     764     3013898 : FlxqX_divrem_basecase(GEN x, GEN y, GEN T, ulong p, GEN *pr)
     765             : {
     766             :   long vx, dx, dy, dz, i, j, sx, lr;
     767             :   pari_sp av0, av, tetpil;
     768             :   GEN z,p1,rem,lead;
     769             : 
     770     3013898 :   if (!signe(y)) pari_err_INV("FlxqX_divrem",y);
     771     3013898 :   vx=varn(x); dy=degpol(y); dx=degpol(x);
     772     3013896 :   if (dx < dy)
     773             :   {
     774       21734 :     if (pr)
     775             :     {
     776       21475 :       av0 = avma; x = FlxqX_red(x, T, p);
     777       21475 :       if (pr == ONLY_DIVIDES) { set_avma(av0); return signe(x)? NULL: pol_0(vx); }
     778       21475 :       if (pr == ONLY_REM) return x;
     779       21475 :       *pr = x;
     780             :     }
     781       21734 :     return pol_0(vx);
     782             :   }
     783     2992162 :   lead = leading_coeff(y);
     784     2992174 :   if (!dy) /* y is constant */
     785             :   {
     786      316109 :     if (pr && pr != ONLY_DIVIDES)
     787             :     {
     788      311496 :       if (pr == ONLY_REM) return pol_0(vx);
     789      133677 :       *pr = pol_0(vx);
     790             :     }
     791      138290 :     if (Flx_equal1(lead)) return gcopy(x);
     792      134153 :     av0 = avma; x = FlxqX_Flxq_mul(x,Flxq_inv(lead,T,p),T,p);
     793      134156 :     return gerepileupto(av0,x);
     794             :   }
     795     2676065 :   av0 = avma; dz = dx-dy;
     796     2676065 :   lead = Flx_equal1(lead)? NULL: gclone(Flxq_inv(lead,T,p));
     797     2676108 :   set_avma(av0);
     798     2676090 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
     799     2676113 :   x += 2; y += 2; z += 2;
     800             : 
     801     2676113 :   p1 = gel(x,dx); av = avma;
     802     2676113 :   gel(z,dz) = lead? gerepileupto(av, Flxq_mul(p1,lead, T, p)): gcopy(p1);
     803     6110406 :   for (i=dx-1; i>=dy; i--)
     804             :   {
     805     3434309 :     av=avma; p1=gel(x,i);
     806    11014826 :     for (j=i-dy+1; j<=i && j<=dz; j++)
     807     7580819 :       p1 = Flx_sub(p1, Flx_mul(gel(z,j),gel(y,i-j),p),p);
     808     3434007 :     if (lead) p1 = Flx_mul(p1, lead,p);
     809     3434010 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil,Flx_rem(p1,T,p));
     810             :   }
     811     2676097 :   if (!pr) { guncloneNULL(lead); return z-2; }
     812             : 
     813     2483475 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
     814     2483516 :   for (sx=0; ; i--)
     815             :   {
     816      307066 :     p1 = gel(x,i);
     817     8153642 :     for (j=0; j<=i && j<=dz; j++)
     818     5363398 :       p1 = Flx_sub(p1, Flx_mul(gel(z,j),gel(y,i-j),p),p);
     819     2790244 :     tetpil=avma; p1 = Flx_rem(p1, T, p); if (lgpol(p1)) { sx = 1; break; }
     820      374694 :     if (!i) break;
     821      307060 :     set_avma(av);
     822             :   }
     823     2483305 :   if (pr == ONLY_DIVIDES)
     824             :   {
     825           0 :     guncloneNULL(lead);
     826           0 :     if (sx) return gc_NULL(av0);
     827           0 :     return gc_const((pari_sp)rem, z-2);
     828             :   }
     829     2483305 :   lr=i+3; rem -= lr;
     830     2483305 :   rem[0] = evaltyp(t_POL) | evallg(lr);
     831     2483288 :   rem[1] = z[-1];
     832     2483288 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
     833     2483483 :   rem += 2; gel(rem,i) = p1;
     834    18535601 :   for (i--; i>=0; i--)
     835             :   {
     836    16052064 :     av=avma; p1 = gel(x,i);
     837    52562096 :     for (j=0; j<=i && j<=dz; j++)
     838    36516974 :       p1 = Flx_sub(p1, Flx_mul(gel(z,j),gel(y,i-j),p), p);
     839    16045122 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, Flx_rem(p1, T, p));
     840             :   }
     841     2483537 :   rem -= 2;
     842     2483537 :   guncloneNULL(lead);
     843     2483532 :   if (!sx) (void)FlxX_renormalize(rem, lr);
     844     2483542 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
     845     1206014 :   *pr = rem; return z-2;
     846             : }
     847             : 
     848             : static GEN
     849        1223 : FlxqX_invBarrett_basecase(GEN T, GEN Q, ulong p)
     850             : {
     851        1223 :   long i, l=lg(T)-1, lr = l-1, k;
     852        1223 :   long sv=Q[1];
     853        1223 :   GEN r=cgetg(lr,t_POL); r[1]=T[1];
     854        1223 :   gel(r,2) = pol1_Flx(sv);
     855       13231 :   for (i=3;i<lr;i++)
     856             :   {
     857       12008 :     pari_sp ltop=avma;
     858       12008 :     GEN u = Flx_neg(gel(T,l-i+2),p);
     859       84659 :     for (k=3;k<i;k++)
     860       72651 :       u = Flx_sub(u, Flxq_mul(gel(T,l-i+k),gel(r,k),Q,p),p);
     861       12008 :     gel(r,i) = gerepileupto(ltop, u);
     862             :   }
     863        1223 :   r = FlxX_renormalize(r,lr);
     864        1223 :   return r;
     865             : }
     866             : 
     867             : /* Return new lgpol */
     868             : static long
     869      295217 : FlxX_lgrenormalizespec(GEN x, long lx)
     870             : {
     871             :   long i;
     872      326144 :   for (i = lx-1; i>=0; i--)
     873      326144 :     if (lgpol(gel(x,i))) break;
     874      295217 :   return i+1;
     875             : }
     876             : 
     877             : static GEN
     878        6348 : FlxqX_invBarrett_Newton(GEN S, GEN T, ulong p)
     879             : {
     880        6348 :   pari_sp av = avma;
     881        6348 :   long nold, lx, lz, lq, l = degpol(S), i, lQ;
     882        6348 :   GEN q, y, z, x = cgetg(l+2, t_POL) + 2;
     883        6348 :   long dT = get_Flx_degree(T), vT = get_Flx_var(T);
     884        6348 :   ulong mask = quadratic_prec_mask(l-2); /* assume l > 2 */
     885      262924 :   for (i=0;i<l;i++) gel(x,i) = pol0_Flx(vT);
     886        6348 :   q = FlxX_recipspec(S+2,l+1,l+1,dT);
     887        6348 :   lQ = lgpol(q); q+=2;
     888             :   /* We work on _spec_ FlxX's, all the l[xzq] below are lgpol's */
     889             : 
     890             :   /* initialize */
     891        6348 :   gel(x,0) = Flxq_inv(gel(q,0),T, p);
     892        6348 :   if (lQ>1 && degpol(gel(q,1)) >= dT)
     893           0 :     gel(q,1) = Flx_rem(gel(q,1), T, p);
     894        6348 :   if (lQ>1 && lgpol(gel(q,1)))
     895        5337 :   {
     896        5337 :     GEN u = gel(q, 1);
     897        5337 :     if (!Flx_equal1(gel(x,0))) u = Flxq_mul(u, Flxq_sqr(gel(x,0), T,p), T,p);
     898        5337 :     gel(x,1) = Flx_neg(u, p); lx = 2;
     899             :   }
     900             :   else
     901        1011 :     lx = 1;
     902        6348 :   nold = 1;
     903       41035 :   for (; mask > 1; )
     904             :   { /* set x -= x(x*q - 1) + O(t^(nnew + 1)), knowing x*q = 1 + O(t^(nold+1)) */
     905       34687 :     long i, lnew, nnew = nold << 1;
     906             : 
     907       34687 :     if (mask & 1) nnew--;
     908       34687 :     mask >>= 1;
     909             : 
     910       34687 :     lnew = nnew + 1;
     911       34687 :     lq = FlxX_lgrenormalizespec(q, minss(lQ,lnew));
     912       34687 :     z = FlxqX_mulspec(x, q, T, p, lx, lq); /* FIXME: high product */
     913       34687 :     lz = lgpol(z); if (lz > lnew) lz = lnew;
     914       34687 :     z += 2;
     915             :     /* subtract 1 [=>first nold words are 0]: renormalize so that z(0) != 0 */
     916       70986 :     for (i = nold; i < lz; i++) if (lgpol(gel(z,i))) break;
     917       34687 :     nold = nnew;
     918       34687 :     if (i >= lz) continue; /* z-1 = 0(t^(nnew + 1)) */
     919             : 
     920             :     /* z + i represents (x*q - 1) / t^i */
     921       33784 :     lz = FlxX_lgrenormalizespec (z+i, lz-i);
     922       33784 :     z = FlxqX_mulspec(x, z+i, T,p, lx, lz); /* FIXME: low product */
     923       33784 :     lz = lgpol(z); z += 2;
     924       33784 :     if (lz > lnew-i) lz = FlxX_lgrenormalizespec(z, lnew-i);
     925             : 
     926       33784 :     lx = lz+ i;
     927       33784 :     y  = x + i; /* x -= z * t^i, in place */
     928      268084 :     for (i = 0; i < lz; i++) gel(y,i) = Flx_neg(gel(z,i), p);
     929             :   }
     930        6348 :   x -= 2; setlg(x, lx + 2); x[1] = S[1];
     931        6348 :   return gerepilecopy(av, x);
     932             : }
     933             : 
     934             : GEN
     935        7571 : FlxqX_invBarrett(GEN T, GEN Q, ulong p)
     936             : {
     937        7571 :   pari_sp ltop=avma;
     938        7571 :   long l=lg(T), v = varn(T);
     939             :   GEN r;
     940        7571 :   GEN c = gel(T,l-1);
     941        7571 :   if (l<5) return pol_0(v);
     942        7571 :   if (l<=FlxqX_INVBARRETT_LIMIT)
     943             :   {
     944        1223 :     if (!Flx_equal1(c))
     945             :     {
     946           0 :       GEN ci = Flxq_inv(c,Q,p);
     947           0 :       T = FlxqX_Flxq_mul(T, ci, Q, p);
     948           0 :       r = FlxqX_invBarrett_basecase(T,Q,p);
     949           0 :       r = FlxqX_Flxq_mul(r,ci,Q,p);
     950             :     } else
     951        1223 :       r = FlxqX_invBarrett_basecase(T,Q,p);
     952             :   } else
     953        6348 :     r = FlxqX_invBarrett_Newton(T,Q,p);
     954        7571 :   return gerepileupto(ltop, r);
     955             : }
     956             : 
     957             : GEN
     958      448004 : FlxqX_get_red(GEN S, GEN T, ulong p)
     959             : {
     960      448004 :   if (typ(S)==t_POL && lg(S)>FlxqX_BARRETT_LIMIT)
     961        6198 :     retmkvec2(FlxqX_invBarrett(S, T, p), S);
     962      441806 :   return S;
     963             : }
     964             : 
     965             : /* Compute x mod S where 2 <= degpol(S) <= l+1 <= 2*(degpol(S)-1)
     966             :  *  * and mg is the Barrett inverse of S. */
     967             : static GEN
     968       96990 : FlxqX_divrem_Barrettspec(GEN x, long l, GEN mg, GEN S, GEN T, ulong p, GEN *pr)
     969             : {
     970             :   GEN q, r;
     971       96990 :   long lt = degpol(S); /*We discard the leading term*/
     972             :   long ld, lm, lT, lmg;
     973       96990 :   ld = l-lt;
     974       96990 :   lm = minss(ld, lgpol(mg));
     975       96990 :   lT  = FlxX_lgrenormalizespec(S+2,lt);
     976       96990 :   lmg = FlxX_lgrenormalizespec(mg+2,lm);
     977       96990 :   q = FlxX_recipspec(x+lt,ld,ld,0);               /* q = rec(x)     lq<=ld*/
     978       96990 :   q = FlxqX_mulspec(q+2,mg+2,T,p,lgpol(q),lmg);   /* q = rec(x) * mg lq<=ld+lm*/
     979       96990 :   q = FlxX_recipspec(q+2,minss(ld,lgpol(q)),ld,0);/* q = rec (rec(x) * mg) lq<=ld*/
     980       96990 :   if (!pr) return q;
     981       96717 :   r = FlxqX_mulspec(q+2,S+2,T,p,lgpol(q),lT);     /* r = q*pol        lr<=ld+lt*/
     982       96717 :   r = FlxX_subspec(x,r+2,p,lt,minss(lt,lgpol(r)));/* r = x - r   lr<=lt */
     983       96717 :   if (pr == ONLY_REM) return r;
     984        7667 :   *pr = r; return q;
     985             : }
     986             : 
     987             : static GEN
     988       90115 : FlxqX_divrem_Barrett(GEN x, GEN mg, GEN S, GEN T, ulong p, GEN *pr)
     989             : {
     990       90115 :   GEN q = NULL, r = FlxqX_red(x, T, p);
     991       90115 :   long l = lgpol(r), lt = degpol(S), lm = 2*lt-1, v = varn(S);
     992             :   long i;
     993       90115 :   if (l <= lt)
     994             :   {
     995           0 :     if (pr == ONLY_REM) return r;
     996           0 :     if (pr == ONLY_DIVIDES) return signe(r)? NULL: pol_0(v);
     997           0 :     if (pr) *pr = r;
     998           0 :     return pol_0(v);
     999             :   }
    1000       90115 :   if (lt <= 1)
    1001           0 :     return FlxqX_divrem_basecase(x,S,T,p,pr);
    1002       90115 :   if (pr != ONLY_REM && l>lm)
    1003             :   {
    1004         750 :     long vT = get_Flx_var(T);
    1005         750 :     q = cgetg(l-lt+2, t_POL); q[1] = S[1];
    1006       51677 :     for (i=0;i<l-lt;i++) gel(q+2,i) = pol0_Flx(vT);
    1007             :   }
    1008       97072 :   while (l>lm)
    1009             :   {
    1010        6957 :     GEN zr, zq = FlxqX_divrem_Barrettspec(r+2+l-lm,lm,mg,S,T,p,&zr);
    1011        6957 :     long lz = lgpol(zr);
    1012        6957 :     if (pr != ONLY_REM)
    1013             :     {
    1014        2568 :       long lq = lgpol(zq);
    1015       46007 :       for(i=0; i<lq; i++) gel(q+2+l-lm,i) = gel(zq,2+i);
    1016             :     }
    1017       67691 :     for(i=0; i<lz; i++) gel(r+2+l-lm,i) = gel(zr,2+i);
    1018        6957 :     l = l-lm+lz;
    1019             :   }
    1020       90115 :   if (pr == ONLY_REM)
    1021             :   {
    1022       89050 :     if (l > lt)
    1023       89050 :       r = FlxqX_divrem_Barrettspec(r+2,l,mg,S,T,p,ONLY_REM);
    1024             :     else
    1025           0 :       r = FlxX_renormalize(r, l+2);
    1026       89050 :     setvarn(r, v); return r;
    1027             :   }
    1028        1065 :   if (l > lt)
    1029             :   {
    1030         983 :     GEN zq = FlxqX_divrem_Barrettspec(r+2,l,mg,S,T,p,pr? &r: NULL);
    1031         983 :     if (!q) q = zq;
    1032             :     else
    1033             :     {
    1034         668 :       long lq = lgpol(zq);
    1035        5572 :       for(i=0; i<lq; i++) gel(q+2,i) = gel(zq,2+i);
    1036             :     }
    1037             :   }
    1038          82 :   else if (pr)
    1039          82 :     r = FlxX_renormalize(r, l+2);
    1040        1065 :   setvarn(q, v); q = FlxX_renormalize(q, lg(q));
    1041        1065 :   if (pr == ONLY_DIVIDES) return signe(r)? NULL: q;
    1042        1065 :   if (pr) { setvarn(r, v); *pr = r; }
    1043        1065 :   return q;
    1044             : }
    1045             : 
    1046             : GEN
    1047     1559479 : FlxqX_divrem(GEN x, GEN S, GEN T, ulong p, GEN *pr)
    1048             : {
    1049             :   GEN B, y;
    1050             :   long dy, dx, d;
    1051     1559479 :   if (pr==ONLY_REM) return FlxqX_rem(x, S, T, p);
    1052     1559479 :   y = get_FlxqX_red(S, &B);
    1053     1559491 :   dy = degpol(y); dx = degpol(x); d = dx-dy;
    1054     1559551 :   if (!B && d+3 < FlxqX_DIVREM_BARRETT_LIMIT)
    1055     1558486 :     return FlxqX_divrem_basecase(x,y,T,p,pr);
    1056             :   else
    1057             :   {
    1058        1065 :     pari_sp av = avma;
    1059        1065 :     GEN mg = B? B: FlxqX_invBarrett(y, T, p);
    1060        1065 :     GEN q = FlxqX_divrem_Barrett(x,mg,y,T,p,pr);
    1061        1065 :     if (!q) return gc_NULL(av);
    1062        1065 :     if (!pr || pr==ONLY_DIVIDES) return gerepilecopy(av, q);
    1063         792 :     return gc_all(av, 2, &q, pr);
    1064             :   }
    1065             : }
    1066             : 
    1067             : GEN
    1068     1862286 : FlxqX_rem(GEN x, GEN S, GEN T, ulong p)
    1069             : {
    1070     1862286 :   GEN B, y = get_FlxqX_red(S, &B);
    1071     1862286 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
    1072     1862286 :   if (d < 0) return FlxqX_red(x, T, p);
    1073     1544451 :   if (!B && d+3 < FlxqX_REM_BARRETT_LIMIT)
    1074     1455401 :     return FlxqX_divrem_basecase(x,y, T, p, ONLY_REM);
    1075             :   else
    1076             :   {
    1077       89050 :     pari_sp av=avma;
    1078       89050 :     GEN mg = B? B: FlxqX_invBarrett(y, T, p);
    1079       89050 :     GEN r = FlxqX_divrem_Barrett(x, mg, y, T, p, ONLY_REM);
    1080       89050 :     return gerepileupto(av, r);
    1081             :   }
    1082             : }
    1083             : 
    1084             : static GEN
    1085         636 : FlxqX_halfgcd_basecase(GEN a, GEN b, GEN T, ulong p)
    1086             : {
    1087         636 :   pari_sp av=avma;
    1088             :   GEN u,u1,v,v1;
    1089         636 :   long vx = varn(a);
    1090         636 :   long n = lgpol(a)>>1;
    1091         636 :   u1 = v = pol_0(vx);
    1092         636 :   u = v1 = pol1_FlxX(vx, get_Flx_var(T));
    1093        9147 :   while (lgpol(b)>n)
    1094             :   {
    1095        8511 :     GEN r, q = FlxqX_divrem(a,b, T, p, &r);
    1096        8511 :     a = b; b = r; swap(u,u1); swap(v,v1);
    1097        8511 :     u1 = FlxX_sub(u1, FlxqX_mul(u, q, T, p), p);
    1098        8511 :     v1 = FlxX_sub(v1, FlxqX_mul(v, q ,T, p), p);
    1099        8511 :     if (gc_needed(av,2))
    1100             :     {
    1101           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_halfgcd (d = %ld)",degpol(b));
    1102           0 :       gerepileall(av,6, &a,&b,&u1,&v1,&u,&v);
    1103             :     }
    1104             :   }
    1105         636 :   return gerepilecopy(av, mkmat2(mkcol2(u,u1), mkcol2(v,v1)));
    1106             : }
    1107             : static GEN
    1108         768 : FlxqX_addmulmul(GEN u, GEN v, GEN x, GEN y, GEN T, ulong p)
    1109             : {
    1110         768 :   return FlxX_add(FlxqX_mul(u, x, T, p),FlxqX_mul(v, y, T, p), p);
    1111             : }
    1112             : 
    1113             : static GEN
    1114         384 : FlxqXM_FlxqX_mul2(GEN M, GEN x, GEN y, GEN T, ulong p)
    1115             : {
    1116         384 :   GEN res = cgetg(3, t_COL);
    1117         384 :   gel(res, 1) = FlxqX_addmulmul(gcoeff(M,1,1), gcoeff(M,1,2), x, y, T, p);
    1118         384 :   gel(res, 2) = FlxqX_addmulmul(gcoeff(M,2,1), gcoeff(M,2,2), x, y, T, p);
    1119         384 :   return res;
    1120             : }
    1121             : 
    1122             : static GEN
    1123         360 : FlxqXM_mul2(GEN A, GEN B, GEN T, ulong p)
    1124             : {
    1125         360 :   GEN A11=gcoeff(A,1,1),A12=gcoeff(A,1,2), B11=gcoeff(B,1,1),B12=gcoeff(B,1,2);
    1126         360 :   GEN A21=gcoeff(A,2,1),A22=gcoeff(A,2,2), B21=gcoeff(B,2,1),B22=gcoeff(B,2,2);
    1127         360 :   GEN M1 = FlxqX_mul(FlxX_add(A11,A22, p), FlxX_add(B11,B22, p), T, p);
    1128         360 :   GEN M2 = FlxqX_mul(FlxX_add(A21,A22, p), B11, T, p);
    1129         360 :   GEN M3 = FlxqX_mul(A11, FlxX_sub(B12,B22, p), T, p);
    1130         360 :   GEN M4 = FlxqX_mul(A22, FlxX_sub(B21,B11, p), T, p);
    1131         360 :   GEN M5 = FlxqX_mul(FlxX_add(A11,A12, p), B22, T, p);
    1132         360 :   GEN M6 = FlxqX_mul(FlxX_sub(A21,A11, p), FlxX_add(B11,B12, p), T, p);
    1133         360 :   GEN M7 = FlxqX_mul(FlxX_sub(A12,A22, p), FlxX_add(B21,B22, p), T, p);
    1134         360 :   GEN T1 = FlxX_add(M1,M4, p), T2 = FlxX_sub(M7,M5, p);
    1135         360 :   GEN T3 = FlxX_sub(M1,M2, p), T4 = FlxX_add(M3,M6, p);
    1136         360 :   retmkmat2(mkcol2(FlxX_add(T1,T2, p), FlxX_add(M2,M4, p)),
    1137             :             mkcol2(FlxX_add(M3,M5, p), FlxX_add(T3,T4, p)));
    1138             : }
    1139             : 
    1140             : /* Return [0,1;1,-q]*M */
    1141             : static GEN
    1142         360 : FlxqX_FlxqXM_qmul(GEN q, GEN M, GEN T, ulong p)
    1143             : {
    1144         360 :   GEN u, v, res = cgetg(3, t_MAT);
    1145         360 :   u = FlxX_sub(gcoeff(M,1,1), FlxqX_mul(gcoeff(M,2,1), q, T, p), p);
    1146         360 :   gel(res,1) = mkcol2(gcoeff(M,2,1), u);
    1147         360 :   v = FlxX_sub(gcoeff(M,1,2), FlxqX_mul(gcoeff(M,2,2), q, T, p), p);
    1148         360 :   gel(res,2) = mkcol2(gcoeff(M,2,2), v);
    1149         360 :   return res;
    1150             : }
    1151             : 
    1152             : static GEN
    1153           0 : matid2_FlxXM(long v, long sv)
    1154             : {
    1155           0 :   retmkmat2(mkcol2(pol1_FlxX(v, sv),pol_0(v)),
    1156             :             mkcol2(pol_0(v),pol1_FlxX(v, sv)));
    1157             : }
    1158             : 
    1159             : static GEN
    1160         363 : FlxqX_halfgcd_split(GEN x, GEN y, GEN T, ulong p)
    1161             : {
    1162         363 :   pari_sp av=avma;
    1163             :   GEN R, S, V;
    1164             :   GEN y1, r, q;
    1165         363 :   long l = lgpol(x), n = l>>1, k;
    1166         363 :   long vT = get_Flx_var(T);
    1167         363 :   if (lgpol(y)<=n) return matid2_FlxXM(varn(x),vT);
    1168         363 :   R = FlxqX_halfgcd(FlxX_shift(x,-n,vT),FlxX_shift(y,-n,vT), T, p);
    1169         363 :   V = FlxqXM_FlxqX_mul2(R,x,y, T, p); y1 = gel(V,2);
    1170         363 :   if (lgpol(y1)<=n) return gerepilecopy(av, R);
    1171         360 :   q = FlxqX_divrem(gel(V,1), y1, T, p, &r);
    1172         360 :   k = 2*n-degpol(y1);
    1173         360 :   S = FlxqX_halfgcd(FlxX_shift(y1,-k,vT), FlxX_shift(r,-k,vT), T, p);
    1174         360 :   return gerepileupto(av, FlxqXM_mul2(S,FlxqX_FlxqXM_qmul(q,R, T, p), T, p));
    1175             : }
    1176             : 
    1177             : /* Return M in GL_2(Fp[X]) such that:
    1178             : if [a',b']~=M*[a,b]~ then degpol(a')>= (lgpol(a)>>1) >degpol(b')
    1179             : */
    1180             : 
    1181             : static GEN
    1182         999 : FlxqX_halfgcd_i(GEN x, GEN y, GEN T, ulong p)
    1183             : {
    1184         999 :   if (lg(x)<=FlxqX_HALFGCD_LIMIT) return FlxqX_halfgcd_basecase(x, y, T, p);
    1185         363 :   return FlxqX_halfgcd_split(x, y, T, p);
    1186             : }
    1187             : 
    1188             : GEN
    1189         999 : FlxqX_halfgcd(GEN x, GEN y, GEN T, ulong p)
    1190             : {
    1191         999 :   pari_sp av = avma;
    1192             :   GEN M,q,r;
    1193         999 :   if (!signe(x))
    1194             :   {
    1195           0 :     long v = varn(x), vT = get_Flx_var(T);
    1196           0 :     retmkmat2(mkcol2(pol_0(v),pol1_FlxX(v,vT)),
    1197             :         mkcol2(pol1_FlxX(v,vT),pol_0(v)));
    1198             :   }
    1199         999 :   if (degpol(y)<degpol(x)) return FlxqX_halfgcd_i(x, y, T, p);
    1200          12 :   q = FlxqX_divrem(y, x, T, p, &r);
    1201          12 :   M = FlxqX_halfgcd_i(x, r, T, p);
    1202          12 :   gcoeff(M,1,1) = FlxX_sub(gcoeff(M,1,1), FlxqX_mul(q, gcoeff(M,1,2), T, p), p);
    1203          12 :   gcoeff(M,2,1) = FlxX_sub(gcoeff(M,2,1), FlxqX_mul(q, gcoeff(M,2,2), T, p), p);
    1204          12 :   return gerepilecopy(av, M);
    1205             : }
    1206             : 
    1207             : static GEN
    1208      162278 : FlxqX_gcd_basecase(GEN a, GEN b, GEN T, ulong p)
    1209             : {
    1210      162278 :   pari_sp av = avma, av0=avma;
    1211     1062792 :   while (signe(b))
    1212             :   {
    1213             :     GEN c;
    1214      900514 :     if (gc_needed(av0,2))
    1215             :     {
    1216          28 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_gcd (d = %ld)",degpol(b));
    1217          28 :       gerepileall(av0,2, &a,&b);
    1218             :     }
    1219      900514 :     av = avma; c = FlxqX_rem(a, b, T, p); a=b; b=c;
    1220             :   }
    1221      162278 :   return gc_const(av, a);
    1222             : }
    1223             : 
    1224             : GEN
    1225      167427 : FlxqX_gcd(GEN x, GEN y, GEN T, ulong p)
    1226             : {
    1227      167427 :   pari_sp av = avma;
    1228      167427 :   x = FlxqX_red(x, T, p);
    1229      167427 :   y = FlxqX_red(y, T, p);
    1230      167427 :   if (!signe(x)) return gerepileupto(av, y);
    1231      162299 :   while (lg(y)>FlxqX_GCD_LIMIT)
    1232             :   {
    1233             :     GEN c;
    1234          21 :     if (lgpol(y)<=(lgpol(x)>>1))
    1235             :     {
    1236           0 :       GEN r = FlxqX_rem(x, y, T, p);
    1237           0 :       x = y; y = r;
    1238             :     }
    1239          21 :     c = FlxqXM_FlxqX_mul2(FlxqX_halfgcd(x,y, T, p), x, y, T, p);
    1240          21 :     x = gel(c,1); y = gel(c,2);
    1241          21 :     gerepileall(av,2,&x,&y);
    1242             :   }
    1243      162278 :   return gerepileupto(av, FlxqX_gcd_basecase(x, y, T, p));
    1244             : }
    1245             : 
    1246             : static GEN
    1247      133689 : FlxqX_extgcd_basecase(GEN a, GEN b, GEN T, ulong p, GEN *ptu, GEN *ptv)
    1248             : {
    1249      133689 :   pari_sp av=avma;
    1250             :   GEN u,v,d,d1,v1;
    1251      133689 :   long vx = varn(a);
    1252      133689 :   d = a; d1 = b;
    1253      133689 :   v = pol_0(vx); v1 = pol1_FlxX(vx, get_Flx_var(T));
    1254      471336 :   while (signe(d1))
    1255             :   {
    1256      337653 :     GEN r, q = FlxqX_divrem(d, d1, T, p, &r);
    1257      337667 :     v = FlxX_sub(v,FlxqX_mul(q,v1,T, p),p);
    1258      337639 :     u=v; v=v1; v1=u;
    1259      337639 :     u=r; d=d1; d1=u;
    1260      337639 :     if (gc_needed(av,2))
    1261             :     {
    1262           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_extgcd (d = %ld)",degpol(d));
    1263           0 :       gerepileall(av,5, &d,&d1,&u,&v,&v1);
    1264             :     }
    1265             :   }
    1266      133683 :   if (ptu) *ptu = FlxqX_div(FlxX_sub(d,FlxqX_mul(b,v, T, p), p), a, T, p);
    1267      133684 :   *ptv = v; return d;
    1268             : }
    1269             : 
    1270             : static GEN
    1271           0 : FlxqX_extgcd_halfgcd(GEN x, GEN y, GEN T, ulong p, GEN *ptu, GEN *ptv)
    1272             : {
    1273           0 :   pari_sp av=avma;
    1274           0 :   GEN u,v,R = matid2_FlxXM(varn(x), get_Flx_var(T));
    1275           0 :   while (lg(y)>FlxqX_EXTGCD_LIMIT)
    1276             :   {
    1277             :     GEN M, c;
    1278           0 :     if (lgpol(y)<=(lgpol(x)>>1))
    1279             :     {
    1280           0 :       GEN r, q = FlxqX_divrem(x, y, T, p, &r);
    1281           0 :       x = y; y = r;
    1282           0 :       R = FlxqX_FlxqXM_qmul(q, R, T, p);
    1283             :     }
    1284           0 :     M = FlxqX_halfgcd(x,y, T, p);
    1285           0 :     c = FlxqXM_FlxqX_mul2(M, x,y, T, p);
    1286           0 :     R = FlxqXM_mul2(M, R, T, p);
    1287           0 :     x = gel(c,1); y = gel(c,2);
    1288           0 :     gerepileall(av,3,&x,&y,&R);
    1289             :   }
    1290           0 :   y = FlxqX_extgcd_basecase(x,y, T, p, &u,&v);
    1291           0 :   if (ptu) *ptu = FlxqX_addmulmul(u,v,gcoeff(R,1,1),gcoeff(R,2,1), T, p);
    1292           0 :   *ptv = FlxqX_addmulmul(u,v,gcoeff(R,1,2),gcoeff(R,2,2), T, p);
    1293           0 :   return y;
    1294             : }
    1295             : 
    1296             : /* x and y in Z[Y][X], return lift(gcd(x mod T,p, y mod T,p)). Set u and v st
    1297             :  * ux + vy = gcd (mod T,p) */
    1298             : GEN
    1299      133684 : FlxqX_extgcd(GEN x, GEN y, GEN T, ulong p, GEN *ptu, GEN *ptv)
    1300             : {
    1301      133684 :   pari_sp av = avma;
    1302             :   GEN d;
    1303      133684 :   x = FlxqX_red(x, T, p);
    1304      133688 :   y = FlxqX_red(y, T, p);
    1305      133688 :   if (lg(y)>FlxqX_EXTGCD_LIMIT)
    1306           0 :     d = FlxqX_extgcd_halfgcd(x, y, T, p, ptu, ptv);
    1307             :   else
    1308      133688 :     d = FlxqX_extgcd_basecase(x, y, T, p, ptu, ptv);
    1309      133685 :   return gc_all(av, ptu?3:2, &d, ptv, ptu);
    1310             : }
    1311             : 
    1312             : static GEN
    1313      319192 : FlxqX_saferem(GEN P, GEN Q, GEN T, ulong p)
    1314             : {
    1315      319192 :   GEN U = Flxq_invsafe(leading_coeff(Q), T, p);
    1316      319218 :   if (!U) return NULL;
    1317      319218 :   Q = FlxqX_Flxq_mul_to_monic(Q,U,T,p);
    1318      319237 :   return FlxqX_rem(P,Q,T,p);
    1319             : }
    1320             : 
    1321             : GEN
    1322       79129 : FlxqX_safegcd(GEN P, GEN Q, GEN T, ulong p)
    1323             : {
    1324       79129 :   pari_sp av = avma;
    1325             :   GEN U;
    1326       79129 :   if (!signe(P)) return gcopy(Q);
    1327       79129 :   if (!signe(Q)) return gcopy(P);
    1328       79129 :   T = Flx_get_red(T,p);
    1329             :   for(;;)
    1330             :   {
    1331      276430 :     P = FlxqX_saferem(P,Q,T,p);
    1332      276430 :     if (!P) return gc_NULL(av);
    1333      276431 :     if (!signe(P)) break;
    1334      197300 :     if (gc_needed(av, 1))
    1335             :     {
    1336           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_safegcd");
    1337           0 :       gerepileall(av, 2, &P,&Q);
    1338             :     }
    1339      197300 :     swap(P, Q);
    1340             :   }
    1341       79131 :   U = Flxq_invsafe(leading_coeff(Q), T, p);
    1342       79131 :   if (!U) return gc_NULL(av);
    1343       79131 :   Q = FlxqX_Flxq_mul_to_monic(Q,U,T,p);
    1344       79131 :   return gerepileupto(av, Q);
    1345             : }
    1346             : 
    1347             : struct _FlxqX {ulong p; GEN T;};
    1348       58708 : static GEN _FlxqX_mul(void *data,GEN a,GEN b)
    1349             : {
    1350       58708 :   struct _FlxqX *d=(struct _FlxqX*)data;
    1351       58708 :   return FlxqX_mul(a,b,d->T,d->p);
    1352             : }
    1353       10255 : static GEN _FlxqX_sqr(void *data,GEN a)
    1354             : {
    1355       10255 :   struct _FlxqX *d=(struct _FlxqX*)data;
    1356       10255 :   return FlxqX_sqr(a,d->T,d->p);
    1357             : }
    1358             : 
    1359             : GEN
    1360       10227 : FlxqX_powu(GEN V, ulong n, GEN T, ulong p)
    1361             : {
    1362       10227 :   struct _FlxqX d; d.p=p; d.T=T;
    1363       10227 :   return gen_powu(V, n, (void*)&d, &_FlxqX_sqr, &_FlxqX_mul);
    1364             : }
    1365             : 
    1366             : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
    1367             : GEN
    1368        6887 : FlxqX_saferesultant(GEN a, GEN b, GEN T, ulong p)
    1369             : {
    1370        6887 :   long vT = get_Flx_var(T);
    1371             :   long da,db,dc;
    1372             :   pari_sp av;
    1373        6887 :   GEN c,lb, res = pol1_Flx(vT);
    1374             : 
    1375        6887 :   if (!signe(a) || !signe(b)) return pol0_Flx(vT);
    1376             : 
    1377        6887 :   da = degpol(a);
    1378        6887 :   db = degpol(b);
    1379        6887 :   if (db > da)
    1380             :   {
    1381           0 :     swapspec(a,b, da,db);
    1382           0 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1383             :   }
    1384        6887 :   if (!da) return pol1_Flx(vT); /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
    1385        6887 :   av = avma;
    1386       49621 :   while (db)
    1387             :   {
    1388       42760 :     lb = gel(b,db+2);
    1389       42760 :     c = FlxqX_saferem(a,b, T,p);
    1390       42836 :     if (!c) return gc_NULL(av);
    1391       42836 :     a = b; b = c; dc = degpol(c);
    1392       42835 :     if (dc < 0) { set_avma(av); return pol0_Flx(vT); }
    1393             : 
    1394       42821 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1395       42821 :     if (!equali1(lb)) res = Flxq_mul(res, Flxq_powu(lb, da - dc, T, p), T, p);
    1396       42734 :     if (gc_needed(av,2))
    1397             :     {
    1398           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (da = %ld)",da);
    1399           0 :       gerepileall(av,3, &a,&b,&res);
    1400             :     }
    1401       42734 :     da = db; /* = degpol(a) */
    1402       42734 :     db = dc; /* = degpol(b) */
    1403             :   }
    1404        6861 :   res = Flxq_mul(res, Flxq_powu(gel(b,2), da, T, p), T, p);
    1405        6873 :   return gerepileupto(av, res);
    1406             : }
    1407             : 
    1408             : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
    1409             : GEN
    1410          56 : FlxqX_resultant(GEN a, GEN b, GEN T, ulong p)
    1411             : {
    1412          56 :   long vT = get_Flx_var(T);
    1413             :   long da,db,dc;
    1414             :   pari_sp av;
    1415          56 :   GEN c,lb, res = pol1_Flx(vT);
    1416             : 
    1417          56 :   if (!signe(a) || !signe(b)) return pol0_Flx(vT);
    1418             : 
    1419          56 :   da = degpol(a);
    1420          56 :   db = degpol(b);
    1421          56 :   if (db > da)
    1422             :   {
    1423          21 :     swapspec(a,b, da,db);
    1424          21 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1425             :   }
    1426          56 :   if (!da) return pol1_Flx(vT); /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
    1427          56 :   av = avma;
    1428         147 :   while (db)
    1429             :   {
    1430          91 :     lb = gel(b,db+2);
    1431          91 :     c = FlxqX_rem(a,b, T,p);
    1432          91 :     a = b; b = c; dc = degpol(c);
    1433          91 :     if (dc < 0) { set_avma(av); return pol0_Flx(vT); }
    1434             : 
    1435          91 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1436          91 :     if (!equali1(lb)) res = Flxq_mul(res, Flxq_powu(lb, da - dc, T, p), T, p);
    1437          91 :     if (gc_needed(av,2))
    1438             :     {
    1439           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (da = %ld)",da);
    1440           0 :       gerepileall(av,3, &a,&b,&res);
    1441             :     }
    1442          91 :     da = db; /* = degpol(a) */
    1443          91 :     db = dc; /* = degpol(b) */
    1444             :   }
    1445          56 :   res = Flxq_mul(res, Flxq_powu(gel(b,2), da, T, p), T, p);
    1446          56 :   return gerepileupto(av, res);
    1447             : }
    1448             : 
    1449             : /* disc P = (-1)^(n(n-1)/2) lc(P)^(n - deg P' - 2) Res(P,P'), n = deg P */
    1450             : GEN
    1451          14 : FlxqX_disc(GEN P, GEN T, ulong p)
    1452             : {
    1453          14 :   pari_sp av = avma;
    1454          14 :   GEN L, dP = FlxX_deriv(P, p), D = FlxqX_resultant(P, dP, T, p);
    1455             :   long dd;
    1456          14 :   if (!lgpol(D)) return pol0_Flx(get_Flx_var(T));
    1457          14 :   dd = degpol(P) - 2 - degpol(dP); /* >= -1; > -1 iff p | deg(P) */
    1458          14 :   L = leading_coeff(P);
    1459          14 :   if (dd && !Flx_equal1(L))
    1460           0 :     D = (dd == -1)? Flxq_div(D,L,T,p): Flxq_mul(D, Flxq_powu(L, dd, T, p), T, p);
    1461          14 :   if (degpol(P) & 2) D = Flx_neg(D, p);
    1462          14 :   return gerepileupto(av, D);
    1463             : }
    1464             : 
    1465             : INLINE GEN
    1466        4615 : FlxXn_recip(GEN x, long n, long v)
    1467             : {
    1468        4615 :   return FlxX_recipspec(x+2, minss(lgpol(x), n), n, v);
    1469             : }
    1470             : 
    1471             : GEN
    1472        1846 : FlxqX_Newton(GEN P, long n, GEN T, ulong p)
    1473             : {
    1474        1846 :   pari_sp av = avma;
    1475        1846 :   long d = degpol(P), vT = get_Flx_var(T);
    1476        1846 :   GEN dP = FlxXn_recip(FlxX_deriv(P, p), d, vT);
    1477        1846 :   GEN Q = FlxqXn_mul(FlxqXn_inv(FlxXn_recip(P, d+1, vT), n, T, p), dP, n, T, p);
    1478        1846 :   return gerepilecopy(av, Q);
    1479             : }
    1480             : 
    1481             : GEN
    1482         923 : FlxqX_fromNewton(GEN P, GEN T, ulong p)
    1483             : {
    1484         923 :   pari_sp av = avma;
    1485         923 :   long vT = get_Flx_var(T);
    1486         923 :   long n = Flx_constant(constant_coeff(P))+1;
    1487         923 :   GEN z = FlxX_neg(FlxX_shift(P, -1, vT), p);
    1488         923 :   GEN Q = FlxXn_recip(FlxqXn_expint(z, n, T, p), n, vT);
    1489         923 :   return gerepilecopy(av, Q);
    1490             : }
    1491             : 
    1492             : static GEN
    1493         923 : FlxqX_composedsum(GEN P, GEN Q, GEN T, ulong p)
    1494             : {
    1495         923 :   long n = 1+ degpol(P)*degpol(Q);
    1496         923 :   GEN Pl = FlxX_invLaplace(FlxqX_Newton(P,n, T,p), p);
    1497         923 :   GEN Ql = FlxX_invLaplace(FlxqX_Newton(Q,n, T,p), p);
    1498         923 :   GEN L = FlxX_Laplace(FlxqXn_mul(Pl, Ql, n, T,p), p);
    1499         923 :   GEN R = FlxqX_fromNewton(L, T, p);
    1500         923 :   GEN lead = Flxq_mul(Flxq_powu(leading_coeff(P),degpol(Q), T, p),
    1501         923 :                       Flxq_powu(leading_coeff(Q),degpol(P), T, p), T, p);
    1502         923 :   return FlxqX_Flxq_mul(R, lead, T, p);
    1503             : }
    1504             : 
    1505             : GEN
    1506         923 : FlxqX_direct_compositum(GEN P, GEN Q, GEN T, ulong p)
    1507             : {
    1508         923 :   return FlxqX_composedsum(P, Q, T, p);
    1509             : }
    1510             : 
    1511             : GEN
    1512       32337 : FlxqXV_prod(GEN V, GEN T, ulong p)
    1513             : {
    1514       32337 :   struct _FlxqX d; d.p=p; d.T=T;
    1515       32337 :   return gen_product(V, (void*)&d, &_FlxqX_mul);
    1516             : }
    1517             : 
    1518             : static GEN
    1519       32336 : FlxqV_roots_to_deg1(GEN x, GEN T, ulong p, long v)
    1520             : {
    1521       32336 :   long sv = get_Flx_var(T);
    1522      123324 :   pari_APPLY_same(deg1pol_shallow(pol1_Flx(sv),Flx_neg(gel(x,i),p),v))
    1523             : }
    1524             : 
    1525             : GEN
    1526       32336 : FlxqV_roots_to_pol(GEN V, GEN T, ulong p, long v)
    1527             : {
    1528       32336 :   pari_sp ltop = avma;
    1529       32336 :   GEN W = FlxqV_roots_to_deg1(V, T, p, v);
    1530       32339 :   return gerepileupto(ltop, FlxqXV_prod(W, T, p));
    1531             : }
    1532             : 
    1533             : /*******************************************************************/
    1534             : /*                                                                 */
    1535             : /*                       (Fl[X]/T(X))[Y] / S(Y)                    */
    1536             : /*                                                                 */
    1537             : /*******************************************************************/
    1538             : 
    1539             : GEN
    1540      416090 : FlxqXQ_mul(GEN x, GEN y, GEN S, GEN T, ulong p) {
    1541      416090 :   return FlxqX_rem(FlxqX_mul(x,y,T,p),S,T,p);
    1542             : }
    1543             : 
    1544             : GEN
    1545      213349 : FlxqXQ_sqr(GEN x, GEN S, GEN T, ulong p) {
    1546      213349 :   return FlxqX_rem(FlxqX_sqr(x,T,p),S,T,p);
    1547             : }
    1548             : 
    1549             : GEN
    1550          14 : FlxqXQ_invsafe(GEN x, GEN S, GEN T, ulong p)
    1551             : {
    1552          14 :   GEN V, z = FlxqX_extgcd(get_FlxqX_mod(S), x, T, p, NULL, &V);
    1553          14 :   if (degpol(z)) return NULL;
    1554          14 :   z = Flxq_invsafe(gel(z,2),T,p);
    1555          14 :   if (!z) return NULL;
    1556          14 :   return FlxqX_Flxq_mul(V, z, T, p);
    1557             : }
    1558             : 
    1559             : GEN
    1560          14 : FlxqXQ_inv(GEN x, GEN S, GEN T,ulong p)
    1561             : {
    1562          14 :   pari_sp av = avma;
    1563          14 :   GEN U = FlxqXQ_invsafe(x, S, T, p);
    1564          14 :   if (!U) pari_err_INV("FlxqXQ_inv",x);
    1565          14 :   return gerepileupto(av, U);
    1566             : }
    1567             : 
    1568             : GEN
    1569           0 : FlxqXQ_div(GEN x, GEN y, GEN S, GEN T, ulong p) {
    1570           0 :   return FlxqXQ_mul(x, FlxqXQ_inv(y,S,T,p),S,T,p);
    1571             : }
    1572             : 
    1573             : struct _FlxqXQ {
    1574             :   GEN T, S;
    1575             :   ulong p;
    1576             : };
    1577             : static GEN
    1578           0 : _FlxqXQ_add(void *data, GEN x, GEN y) {
    1579           0 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1580           0 :   return FlxX_add(x,y, d->p);
    1581             : }
    1582             : static GEN
    1583        2121 : _FlxqXQ_sub(void *data, GEN x, GEN y) {
    1584        2121 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1585        2121 :   return FlxX_sub(x,y, d->p);
    1586             : }
    1587             : #if 0
    1588             : static GEN
    1589             : _FlxqXQ_cmul(void *data, GEN P, long a, GEN x) {
    1590             :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1591             :   return FlxX_Flx_mul(x,gel(P,a+2), d->p);
    1592             : }
    1593             : #endif
    1594             : static GEN
    1595        2440 : _FlxqXQ_red(void *data, GEN x) {
    1596        2440 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1597        2440 :   return FlxqX_red(x, d->T, d->p);
    1598             : }
    1599             : static GEN
    1600      146900 : _FlxqXQ_mul(void *data, GEN x, GEN y) {
    1601      146900 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1602      146900 :   return FlxqXQ_mul(x,y, d->S,d->T, d->p);
    1603             : }
    1604             : static GEN
    1605      212922 : _FlxqXQ_sqr(void *data, GEN x) {
    1606      212922 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1607      212922 :   return FlxqXQ_sqr(x, d->S,d->T, d->p);
    1608             : }
    1609             : 
    1610             : static GEN
    1611      100179 : _FlxqXQ_one(void *data) {
    1612      100179 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1613      100179 :   return pol1_FlxX(get_FlxqX_var(d->S),get_Flx_var(d->T));
    1614             : }
    1615             : 
    1616             : static GEN
    1617         191 : _FlxqXQ_zero(void *data) {
    1618         191 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1619         191 :   return pol_0(get_FlxqX_var(d->S));
    1620             : }
    1621             : 
    1622             : static struct bb_algebra FlxqXQ_algebra = { _FlxqXQ_red, _FlxqXQ_add,
    1623             :        _FlxqXQ_sub, _FlxqXQ_mul, _FlxqXQ_sqr, _FlxqXQ_one, _FlxqXQ_zero };
    1624             : 
    1625             : const struct bb_algebra *
    1626         219 : get_FlxqXQ_algebra(void **E, GEN S, GEN T, ulong p)
    1627             : {
    1628         219 :   GEN z = new_chunk(sizeof(struct _FlxqXQ));
    1629         219 :   struct _FlxqXQ *e = (struct _FlxqXQ *) z;
    1630         219 :   e->T = Flx_get_red(T, p);
    1631         219 :   e->S = FlxqX_get_red(S, e->T, p);
    1632         219 :   e->p  = p; *E = (void*)e;
    1633         219 :   return &FlxqXQ_algebra;
    1634             : }
    1635             : 
    1636             : /* x over Fq, return lift(x^n) mod S */
    1637             : GEN
    1638          91 : FlxqXQ_pow(GEN x, GEN n, GEN S, GEN T, ulong p)
    1639             : {
    1640          91 :   pari_sp av = avma;
    1641             :   struct _FlxqXQ D;
    1642          91 :   long s = signe(n);
    1643          91 :   if (!s) return pol1_FlxX(get_FlxqX_var(S),get_Flx_var(T));
    1644          91 :   if (s < 0) x = FlxqXQ_inv(x,S,T,p);
    1645          91 :   if (is_pm1(n)) return s < 0 ? x : gcopy(x);
    1646          91 :   if (degpol(x) >= get_FlxqX_degree(S)) x = FlxqX_rem(x,S,T,p);
    1647          91 :   T = Flx_get_red(T, p);
    1648          91 :   S = FlxqX_get_red(S, T, p);
    1649          91 :   D.S = S;
    1650          91 :   D.T = T;
    1651          91 :   D.p = p;
    1652          91 :   x = gen_pow_i(x, n, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul);
    1653          91 :   return gerepilecopy(av, x);
    1654             : }
    1655             : 
    1656             : /* x over Fq, return lift(x^n) mod S */
    1657             : GEN
    1658       75085 : FlxqXQ_powu(GEN x, ulong n, GEN S, GEN T, ulong p)
    1659             : {
    1660       75085 :   pari_sp av = avma;
    1661             :   struct _FlxqXQ D;
    1662       75085 :   switch(n)
    1663             :   {
    1664           0 :     case 0: return pol1_FlxX(get_FlxqX_var(S),get_Flx_var(T));
    1665        7514 :     case 1: return gcopy(x);
    1666         427 :     case 2: return FlxqXQ_sqr(x, S, T, p);
    1667             :   }
    1668       67144 :   T = Flx_get_red(T, p);
    1669       67144 :   S = FlxqX_get_red(S, T, p);
    1670       67144 :   D.S = S; D.T = T; D.p = p;
    1671       67144 :   x = gen_powu_i(x, n, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul);
    1672       67144 :   return gerepilecopy(av, x);
    1673             : }
    1674             : 
    1675             : GEN
    1676       98701 : FlxqXQ_powers(GEN x, long l, GEN S, GEN T, ulong p)
    1677             : {
    1678             :   struct _FlxqXQ D;
    1679       98701 :   int use_sqr = 2*degpol(x) >= get_FlxqX_degree(S);
    1680       98701 :   T = Flx_get_red(T, p);
    1681       98701 :   S = FlxqX_get_red(S, T, p);
    1682       98701 :   D.S = S; D.T = T; D.p = p;
    1683       98701 :   return gen_powers(x, l, use_sqr, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul,&_FlxqXQ_one);
    1684             : }
    1685             : 
    1686             : /* Let v a linear form, return the linear form z->v(tau*z)
    1687             :    that is, v*(M_tau) */
    1688             : 
    1689             : static GEN
    1690         496 : FlxqXQ_transmul_init(GEN tau, GEN S, GEN T, ulong p)
    1691             : {
    1692             :   GEN bht;
    1693         496 :   GEN h, Sp = get_FlxqX_red(S, &h);
    1694         496 :   long n = degpol(Sp), vS = varn(Sp), vT = get_Flx_var(T);
    1695         496 :   GEN ft = FlxX_recipspec(Sp+2, n+1, n+1, vT);
    1696         496 :   GEN bt = FlxX_recipspec(tau+2, lgpol(tau), n, vT);
    1697         496 :   setvarn(ft, vS); setvarn(bt, vS);
    1698         496 :   if (h)
    1699           0 :     bht = FlxqXn_mul(bt, h, n-1, T, p);
    1700             :   else
    1701             :   {
    1702         496 :     GEN bh = FlxqX_div(FlxX_shift(tau, n-1, vT), S, T, p);
    1703         496 :     bht = FlxX_recipspec(bh+2, lgpol(bh), n-1, vT);
    1704         496 :     setvarn(bht, vS);
    1705             :   }
    1706         496 :   return mkvec3(bt, bht, ft);
    1707             : }
    1708             : 
    1709             : static GEN
    1710        1070 : FlxqXQ_transmul(GEN tau, GEN a, long n, GEN T, ulong p)
    1711             : {
    1712        1070 :   pari_sp ltop = avma;
    1713             :   GEN t1, t2, t3, vec;
    1714        1070 :   GEN bt = gel(tau, 1), bht = gel(tau, 2), ft = gel(tau, 3);
    1715        1070 :   long vT = get_Flx_var(T);
    1716        1070 :   if (signe(a)==0) return pol_0(varn(a));
    1717        1058 :   t2 = FlxX_shift(FlxqX_mul(bt, a, T, p),1-n,vT);
    1718        1058 :   if (signe(bht)==0) return gerepilecopy(ltop, t2);
    1719         733 :   t1 = FlxX_shift(FlxqX_mul(ft, a, T, p),-n,vT);
    1720         733 :   t3 = FlxqXn_mul(t1, bht, n-1, T, p);
    1721         733 :   vec = FlxX_sub(t2, FlxX_shift(t3, 1, vT), p);
    1722         733 :   return gerepileupto(ltop, vec);
    1723             : }
    1724             : 
    1725             : static GEN
    1726         248 : polxn_FlxX(long n, long v, long vT)
    1727             : {
    1728         248 :   long i, a = n+2;
    1729         248 :   GEN p = cgetg(a+1, t_POL);
    1730         248 :   p[1] = evalsigne(1)|evalvarn(v);
    1731        2206 :   for (i = 2; i < a; i++) gel(p,i) = pol0_Flx(vT);
    1732         248 :   gel(p,a) = pol1_Flx(vT); return p;
    1733             : }
    1734             : 
    1735             : GEN
    1736         217 : FlxqXQ_minpoly(GEN x, GEN S, GEN T, ulong p)
    1737             : {
    1738         217 :   pari_sp ltop = avma;
    1739             :   long vS, vT, n;
    1740             :   GEN v_x, g, tau;
    1741         217 :   vS = get_FlxqX_var(S);
    1742         217 :   vT = get_Flx_var(T);
    1743         217 :   n = get_FlxqX_degree(S);
    1744         217 :   g = pol1_FlxX(vS,vT);
    1745         217 :   tau = pol1_FlxX(vS,vT);
    1746         217 :   S = FlxqX_get_red(S, T, p);
    1747         217 :   v_x = FlxqXQ_powers(x, usqrt(2*n), S, T, p);
    1748         465 :   while(signe(tau) != 0)
    1749             :   {
    1750             :     long i, j, m, k1;
    1751             :     GEN M, v, tr;
    1752             :     GEN g_prime, c;
    1753         248 :     if (degpol(g) == n) { tau = pol1_FlxX(vS, vT); g = pol1_FlxX(vS, vT); }
    1754         248 :     v = random_FlxqX(n, vS, T, p);
    1755         248 :     tr = FlxqXQ_transmul_init(tau, S, T, p);
    1756         248 :     v = FlxqXQ_transmul(tr, v, n, T, p);
    1757         248 :     m = 2*(n-degpol(g));
    1758         248 :     k1 = usqrt(m);
    1759         248 :     tr = FlxqXQ_transmul_init(gel(v_x,k1+1), S, T, p);
    1760         248 :     c = cgetg(m+2,t_POL);
    1761         248 :     c[1] = evalsigne(1)|evalvarn(vS);
    1762        1070 :     for (i=0; i<m; i+=k1)
    1763             :     {
    1764         822 :       long mj = minss(m-i, k1);
    1765        2780 :       for (j=0; j<mj; j++)
    1766        1958 :         gel(c,m+1-(i+j)) = FlxqX_dotproduct(v, gel(v_x,j+1), T, p);
    1767         822 :       v = FlxqXQ_transmul(tr, v, n, T, p);
    1768             :     }
    1769         248 :     c = FlxX_renormalize(c, m+2);
    1770             :     /* now c contains <v,x^i> , i = 0..m-1  */
    1771         248 :     M = FlxqX_halfgcd(polxn_FlxX(m, vS, vT), c, T, p);
    1772         248 :     g_prime = gmael(M, 2, 2);
    1773         248 :     if (degpol(g_prime) < 1) continue;
    1774         241 :     g = FlxqX_mul(g, g_prime, T, p);
    1775         241 :     tau = FlxqXQ_mul(tau, FlxqX_FlxqXQV_eval(g_prime, v_x, S, T, p), S, T, p);
    1776             :   }
    1777         217 :   g = FlxqX_normalize(g,T, p);
    1778         217 :   return gerepilecopy(ltop,g);
    1779             : }
    1780             : 
    1781             : GEN
    1782           0 : FlxqXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, ulong p)
    1783             : {
    1784           0 :   return FlxXV_to_FlxM(FlxqXQ_powers(y,m-1,S,T,p), n, get_Flx_var(T));
    1785             : }
    1786             : 
    1787             : static GEN
    1788      127150 : FlxX_blocks_FlxM(GEN P, long n, long m, long v)
    1789             : {
    1790      127150 :   GEN z = cgetg(m+1,t_MAT);
    1791      127150 :   long i,j, k=2, l = lg(P);
    1792      493240 :   for(i=1; i<=m; i++)
    1793             :   {
    1794      366090 :     GEN zi = cgetg(n+1,t_COL);
    1795      366090 :     gel(z,i) = zi;
    1796     1170363 :     for(j=1; j<=n; j++)
    1797      804273 :       gel(zi, j) = k==l ? pol0_Flx(v) : gel(P,k++);
    1798             :   }
    1799      127150 :   return z;
    1800             : }
    1801             : 
    1802             : GEN
    1803      127150 : FlxqX_FlxqXQV_eval(GEN Q, GEN x, GEN S, GEN T, ulong p)
    1804             : {
    1805      127150 :   pari_sp btop, av = avma;
    1806      127150 :   long v = get_FlxqX_var(S), m = get_FlxqX_degree(S);
    1807      127150 :   long vT = get_Flx_var(T);
    1808      127150 :   long i, l = lg(x)-1, lQ = lgpol(Q), n,  d;
    1809             :   GEN A, B, C, R, g;
    1810      127150 :   if (lQ == 0) return pol_0(v);
    1811      127150 :   if (lQ <= l)
    1812             :   {
    1813       41686 :     n = l;
    1814       41686 :     d = 1;
    1815             :   }
    1816             :   else
    1817             :   {
    1818       85464 :     n = l-1;
    1819       85464 :     d = (lQ+n-1)/n;
    1820             :   }
    1821      127150 :   A = FlxXV_to_FlxM_lg(x, m, n, vT);
    1822      127150 :   B = FlxX_blocks_FlxM(Q, n, d, vT);
    1823      127150 :   C = gerepileupto(av, FlxqM_mul(A, B, T, p));
    1824      127150 :   g = gel(x, l);
    1825      127150 :   T = Flx_get_red(T, p);
    1826      127150 :   S = FlxqX_get_red(S, T, p);
    1827      127150 :   btop = avma;
    1828      127150 :   R = FlxV_to_FlxX(gel(C, d), v);
    1829      366090 :   for (i = d-1; i>0; i--)
    1830             :   {
    1831      238940 :     R = FlxX_add(FlxqXQ_mul(R, g, S, T, p), FlxV_to_FlxX(gel(C,i), v), p);
    1832      238940 :     if (gc_needed(btop,1))
    1833           7 :       R = gerepileupto(btop, R);
    1834             :   }
    1835      127150 :   return gerepilecopy(av, R);
    1836             : }
    1837             : 
    1838             : GEN
    1839       68989 : FlxqX_FlxqXQ_eval(GEN Q, GEN x, GEN S, GEN T, ulong p)
    1840             : {
    1841       68989 :   pari_sp av = avma;
    1842             :   GEN z, V;
    1843       68989 :   long d = degpol(Q), rtd;
    1844       68989 :   if (d < 0) return pol_0(get_FlxqX_var(S));
    1845       68989 :   rtd = (long) sqrt((double)d);
    1846       68989 :   T = Flx_get_red(T, p);
    1847       68989 :   S = FlxqX_get_red(S, T, p);
    1848       68989 :   V = FlxqXQ_powers(x, rtd, S, T, p);
    1849       68989 :   z = FlxqX_FlxqXQV_eval(Q, V, S, T, p);
    1850       68989 :   return gerepileupto(av, z);
    1851             : }
    1852             : 
    1853             : GEN
    1854           0 : FlxqXC_FlxqXQV_eval(GEN x, GEN v, GEN S, GEN T, ulong p)
    1855           0 : { pari_APPLY_type(t_COL, FlxqX_FlxqXQV_eval(gel(x,i), v, S, T, p)) }
    1856             : 
    1857             : GEN
    1858           0 : FlxqXC_FlxqXQ_eval(GEN x, GEN F, GEN S, GEN T, ulong p)
    1859             : {
    1860           0 :   long d = brent_kung_optpow(get_FlxqX_degree(S)-1,lg(x)-1,1);
    1861           0 :   GEN Fp = FlxqXQ_powers(F, d, S, T, p);
    1862           0 :   return FlxqXC_FlxqXQV_eval(x, Fp, S, T, p);
    1863             : }
    1864             : 
    1865             : static GEN
    1866       66451 : FlxqXQ_autpow_sqr(void * E, GEN x)
    1867             : {
    1868       66451 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    1869       66451 :   GEN S = D->S, T = D->T;
    1870       66451 :   ulong p = D->p;
    1871       66451 :   GEN phi = gel(x,1), S1 = gel(x,2);
    1872       66451 :   long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(S1)+1,1);
    1873       66451 :   GEN V = Flxq_powers(phi, n, T, p);
    1874       66451 :   GEN phi2 = Flx_FlxqV_eval(phi, V, T, p);
    1875       66451 :   GEN Sphi = FlxY_FlxqV_evalx(S1, V, T, p);
    1876       66451 :   GEN S2 = FlxqX_FlxqXQ_eval(Sphi, S1, S, T, p);
    1877       66451 :   return mkvec2(phi2, S2);
    1878             : }
    1879             : 
    1880             : static GEN
    1881        2197 : FlxqXQ_autpow_mul(void * E, GEN x, GEN y)
    1882             : {
    1883        2197 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    1884        2197 :   GEN S = D->S, T = D->T;
    1885        2197 :   ulong p = D->p;
    1886        2197 :   GEN phi1 = gel(x,1), S1 = gel(x,2);
    1887        2197 :   GEN phi2 = gel(y,1), S2 = gel(y,2);
    1888        2197 :   long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(S1)+1,1);
    1889        2197 :   GEN V = Flxq_powers(phi2, n, T, p);
    1890        2197 :   GEN phi3 = Flx_FlxqV_eval(phi1, V, T, p);
    1891        2197 :   GEN Sphi = FlxY_FlxqV_evalx(S1, V, T, p);
    1892        2197 :   GEN S3 = FlxqX_FlxqXQ_eval(Sphi, S2, S, T, p);
    1893        2197 :   return mkvec2(phi3, S3);
    1894             : }
    1895             : 
    1896             : GEN
    1897       63651 : FlxqXQ_autpow(GEN aut, long n, GEN S, GEN T, ulong p)
    1898             : {
    1899       63651 :   pari_sp av = avma;
    1900             :   struct _FlxqXQ D;
    1901       63651 :   T = Flx_get_red(T, p);
    1902       63651 :   S = FlxqX_get_red(S, T, p);
    1903       63651 :   D.S=S; D.T=T; D.p=p;
    1904       63651 :   aut = gen_powu_i(aut,n,&D,FlxqXQ_autpow_sqr,FlxqXQ_autpow_mul);
    1905       63651 :   return gerepilecopy(av, aut);
    1906             : }
    1907             : 
    1908             : static GEN
    1909       28519 : FlxqXQ_autsum_mul(void *E, GEN x, GEN y)
    1910             : {
    1911       28519 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    1912       28519 :   GEN S = D->S, T = D->T;
    1913       28519 :   ulong p = D->p;
    1914       28519 :   GEN phi1 = gel(x,1), S1 = gel(x,2), a1 = gel(x,3);
    1915       28519 :   GEN phi2 = gel(y,1), S2 = gel(y,2), a2 = gel(y,3);
    1916       28519 :   long n2 = brent_kung_optpow(get_Flx_degree(T)-1, lgpol(S1)+lgpol(a1)+1,1);
    1917       28519 :   GEN V2 = Flxq_powers(phi2, n2, T, p);
    1918       28519 :   GEN phi3 = Flx_FlxqV_eval(phi1, V2, T, p);
    1919       28519 :   GEN Sphi = FlxY_FlxqV_evalx(S1, V2, T, p);
    1920       28519 :   GEN aphi = FlxY_FlxqV_evalx(a1, V2, T, p);
    1921       28519 :   long n = brent_kung_optpow(maxss(degpol(Sphi),degpol(aphi)),2,1);
    1922       28519 :   GEN V = FlxqXQ_powers(S2, n, S, T, p);
    1923       28519 :   GEN S3 = FlxqX_FlxqXQV_eval(Sphi, V, S, T, p);
    1924       28519 :   GEN aS = FlxqX_FlxqXQV_eval(aphi, V, S, T, p);
    1925       28519 :   GEN a3 = FlxqXQ_mul(aS, a2, S, T, p);
    1926       28519 :   return mkvec3(phi3, S3, a3);
    1927             : }
    1928             : 
    1929             : static GEN
    1930       17177 : FlxqXQ_autsum_sqr(void * T, GEN x)
    1931       17177 : { return FlxqXQ_autsum_mul(T, x, x); }
    1932             : 
    1933             : GEN
    1934       10968 : FlxqXQ_autsum(GEN aut, long n, GEN S, GEN T, ulong p)
    1935             : {
    1936       10968 :   pari_sp av = avma;
    1937             :   struct _FlxqXQ D;
    1938       10968 :   T = Flx_get_red(T, p);
    1939       10968 :   S = FlxqX_get_red(S, T, p);
    1940       10968 :   D.S=S; D.T=T; D.p=p;
    1941       10968 :   aut = gen_powu_i(aut,n,&D,FlxqXQ_autsum_sqr,FlxqXQ_autsum_mul);
    1942       10968 :   return gerepilecopy(av, aut);
    1943             : }
    1944             : 
    1945             : static GEN
    1946          27 : FlxqXQ_auttrace_mul(void *E, GEN x, GEN y)
    1947             : {
    1948          27 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    1949          27 :   GEN S = D->S, T = D->T;
    1950          27 :   ulong p = D->p;
    1951          27 :   GEN S1 = gel(x,1), a1 = gel(x,2);
    1952          27 :   GEN S2 = gel(y,1), a2 = gel(y,2);
    1953          27 :   long n = brent_kung_optpow(maxss(degpol(S1),degpol(a1)),2,1);
    1954          27 :   GEN V = FlxqXQ_powers(S2, n, S, T, p);
    1955          27 :   GEN S3 = FlxqX_FlxqXQV_eval(S1, V, S, T, p);
    1956          27 :   GEN aS = FlxqX_FlxqXQV_eval(a1, V, S, T, p);
    1957          27 :   GEN a3 = FlxX_add(aS, a2, p);
    1958          27 :   return mkvec2(S3, a3);
    1959             : }
    1960             : 
    1961             : static GEN
    1962          20 : FlxqXQ_auttrace_sqr(void *E, GEN x)
    1963          20 : { return FlxqXQ_auttrace_mul(E, x, x); }
    1964             : 
    1965             : GEN
    1966         337 : FlxqXQ_auttrace(GEN x, ulong n, GEN S, GEN T, ulong p)
    1967             : {
    1968         337 :   pari_sp av = avma;
    1969             :   struct _FlxqXQ D;
    1970         337 :   T = Flx_get_red(T, p);
    1971         337 :   S = FlxqX_get_red(S, T, p);
    1972         337 :   D.S=S; D.T=T; D.p=p;
    1973         337 :   x = gen_powu_i(x,n,(void*)&D,FlxqXQ_auttrace_sqr,FlxqXQ_auttrace_mul);
    1974         337 :   return gerepilecopy(av, x);
    1975             : }
    1976             : 
    1977             : /*******************************************************************/
    1978             : /*                                                                 */
    1979             : /*                      FlxYqQ                                     */
    1980             : /*                                                                 */
    1981             : /*******************************************************************/
    1982             : 
    1983             : /*Preliminary implementation to speed up FpX_ffisom*/
    1984             : typedef struct {
    1985             :   GEN S, T;
    1986             :   ulong p;
    1987             : } FlxYqq_muldata;
    1988             : 
    1989             : /* reduce x in Fl[X, Y] in the algebra Fl[X, Y]/ (P(X),Q(Y)) */
    1990             : static GEN
    1991       91507 : FlxYqq_redswap(GEN x, GEN S, GEN T, ulong p)
    1992             : {
    1993       91507 :   pari_sp ltop=avma;
    1994       91507 :   long n = get_Flx_degree(S);
    1995       91507 :   long m = get_Flx_degree(T);
    1996       91507 :   long w = get_Flx_var(T);
    1997       91507 :   GEN V = FlxX_swap(x,m,w);
    1998       91505 :   V = FlxqX_red(V,S,p);
    1999       91505 :   V = FlxX_swap(V,n,w);
    2000       91504 :   return gerepilecopy(ltop,V);
    2001             : }
    2002             : static GEN
    2003       80850 : FlxYqq_sqr(void *data, GEN x)
    2004             : {
    2005       80850 :   FlxYqq_muldata *D = (FlxYqq_muldata*)data;
    2006       80850 :   return FlxYqq_redswap(FlxqX_sqr(x, D->T, D->p),D->S,D->T,D->p);
    2007             : }
    2008             : 
    2009             : static GEN
    2010       10656 : FlxYqq_mul(void *data, GEN x, GEN y)
    2011             : {
    2012       10656 :   FlxYqq_muldata *D = (FlxYqq_muldata*)data;
    2013       10656 :   return FlxYqq_redswap(FlxqX_mul(x,y, D->T, D->p),D->S,D->T,D->p);
    2014             : }
    2015             : 
    2016             : /* x in Z[X,Y], S in Z[X] over Fq = Z[Y]/(p,T); compute lift(x^n mod (S,T,p)) */
    2017             : GEN
    2018       39537 : FlxYqq_pow(GEN x, GEN n, GEN S, GEN T, ulong p)
    2019             : {
    2020             :   FlxYqq_muldata D;
    2021       39537 :   D.S = S;
    2022       39537 :   D.T = T;
    2023       39537 :   D.p = p;
    2024       39537 :   return gen_pow(x, n, (void*)&D, &FlxYqq_sqr, &FlxYqq_mul);
    2025             : }
    2026             : 
    2027             : /*******************************************************************/
    2028             : /*                                                                 */
    2029             : /*                      FlxqXn                                     */
    2030             : /*                                                                 */
    2031             : /*******************************************************************/
    2032             : 
    2033             : GEN
    2034       47896 : FlxXn_red(GEN a, long n)
    2035             : {
    2036       47896 :   long i, L = n+2, l = lg(a);
    2037             :   GEN  b;
    2038       47896 :   if (L >= l) return a; /* deg(x) < n */
    2039       25652 :   b = cgetg(L, t_POL); b[1] = a[1];
    2040      237376 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
    2041       25656 :   return FlxX_renormalize(b,L);
    2042             : }
    2043             : 
    2044             : GEN
    2045       32483 : FlxqXn_mul(GEN a, GEN b, long n, GEN T, ulong p)
    2046       32483 : { return FlxXn_red(FlxqX_mul(a, b, T, p), n); }
    2047             : 
    2048             : GEN
    2049           0 : FlxqXn_sqr(GEN a, long n, GEN T, ulong p)
    2050           0 : { return FlxXn_red(FlxqX_sqr(a, T, p), n); }
    2051             : 
    2052             : /* (f*g) \/ x^n */
    2053             : static GEN
    2054       14493 : FlxqX_mulhigh_i(GEN f, GEN g, long n, GEN T, ulong p)
    2055             : {
    2056       14493 :   return FlxX_shift(FlxqX_mul(f, g, T, p), -n , get_Flx_var(T));
    2057             : }
    2058             : 
    2059             : static GEN
    2060       10637 : FlxqXn_mulhigh(GEN f, GEN g, long n2, long n, GEN T, ulong p)
    2061             : {
    2062       10637 :   long vT = get_Flx_var(T);
    2063       10637 :   GEN F = FlxX_blocks(f, n2, 2, vT), fl = gel(F,1), fh = gel(F,2);
    2064       10638 :   return FlxX_add(FlxqX_mulhigh_i(fl, g, n2, T, p),
    2065             :                   FlxqXn_mul(fh, g, n - n2, T, p), p);
    2066             : }
    2067             : 
    2068             : GEN
    2069        1846 : FlxqXn_inv(GEN f, long e, GEN T, ulong p)
    2070             : {
    2071        1846 :   pari_sp av = avma, av2;
    2072             :   ulong mask;
    2073             :   GEN W, a;
    2074        1846 :   long v = varn(f), n = 1, vT = get_Flx_var(T);
    2075             : 
    2076        1846 :   if (!signe(f)) pari_err_INV("FlxqXn_inv",f);
    2077        1846 :   a = Flxq_inv(gel(f,2), T, p);
    2078        1846 :   if (e == 1) return scalarpol(a, v);
    2079        1846 :   else if (e == 2)
    2080             :   {
    2081             :     GEN b;
    2082           0 :     if (degpol(f) <= 0) return scalarpol(a, v);
    2083           0 :     b = Flx_neg(gel(f,3), p);
    2084           0 :     if (lgpol(b)==0) return scalarpol(a, v);
    2085           0 :     b = Flxq_mul(b, Flxq_sqr(a, T, p), T, p);
    2086           0 :     W = deg1pol_shallow(b, a, v);
    2087           0 :     return gerepilecopy(av, W);
    2088             :   }
    2089        1846 :   W = scalarpol_shallow(Flxq_inv(gel(f,2), T, p),v);
    2090        1846 :   mask = quadratic_prec_mask(e);
    2091        1846 :   av2 = avma;
    2092        9551 :   for (;mask>1;)
    2093             :   {
    2094             :     GEN u, fr;
    2095        7705 :     long n2 = n;
    2096        7705 :     n<<=1; if (mask & 1) n--;
    2097        7705 :     mask >>= 1;
    2098        7705 :     fr = FlxXn_red(f, n);
    2099        7706 :     u = FlxqXn_mul(W, FlxqXn_mulhigh(fr, W, n2, n, T, p), n-n2, T, p);
    2100        7708 :     W = FlxX_sub(W, FlxX_shift(u, n2, vT), p);
    2101        7704 :     if (gc_needed(av2,2))
    2102             :     {
    2103           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxqXn_inv, e = %ld", n);
    2104           0 :       W = gerepileupto(av2, W);
    2105             :     }
    2106             :   }
    2107        1846 :   return gerepileupto(av, W);
    2108             : }
    2109             : 
    2110             : /* Compute intformal(x^n*S)/x^(n+1) */
    2111             : static GEN
    2112        3854 : FlxX_integXn(GEN x, long n, ulong p)
    2113             : {
    2114        3854 :   long i, lx = lg(x);
    2115             :   GEN y;
    2116        3854 :   if (lx == 2) return gcopy(x);
    2117        3318 :   y = cgetg(lx, t_POL); y[1] = x[1];
    2118       24411 :   for (i=2; i<lx; i++)
    2119             :   {
    2120       21093 :     GEN xi = gel(x,i);
    2121       21093 :     gel(y,i) = Flx_Fl_mul(xi, Fl_inv((n+i-1)%p, p), p);
    2122             :   }
    2123        3318 :   return FlxX_renormalize(y, lx);;
    2124             : }
    2125             : 
    2126             : GEN
    2127         923 : FlxqXn_expint(GEN h, long e, GEN T, ulong p)
    2128             : {
    2129         923 :   pari_sp av = avma, av2;
    2130         923 :   long v = varn(h), n = 1, vT = get_Flx_var(T);
    2131         923 :   GEN f = pol1_FlxX(v, vT), g = pol1_FlxX(v, vT);
    2132         923 :   ulong mask = quadratic_prec_mask(e);
    2133         923 :   av2 = avma;
    2134        3854 :   for (;mask>1;)
    2135             :   {
    2136             :     GEN u, w;
    2137        3854 :     long n2 = n;
    2138        3854 :     n<<=1; if (mask & 1) n--;
    2139        3854 :     mask >>= 1;
    2140        3854 :     u = FlxqXn_mul(g, FlxqX_mulhigh_i(f, FlxXn_red(h, n2-1), n2-1, T, p), n-n2, T, p);
    2141        3854 :     u = FlxX_add(u, FlxX_shift(FlxXn_red(h, n-1), 1-n2, vT), p);
    2142        3854 :     w = FlxqXn_mul(f, FlxX_integXn(u, n2-1, p), n-n2, T, p);
    2143        3854 :     f = FlxX_add(f, FlxX_shift(w, n2, vT), p);
    2144        3854 :     if (mask<=1) break;
    2145        2931 :     u = FlxqXn_mul(g, FlxqXn_mulhigh(f, g, n2, n, T, p), n-n2, T, p);
    2146        2931 :     g = FlxX_sub(g, FlxX_shift(u, n2, vT), p);
    2147        2931 :     if (gc_needed(av2,2))
    2148             :     {
    2149           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqXn_exp, e = %ld", n);
    2150           0 :       gerepileall(av2, 2, &f, &g);
    2151             :     }
    2152             :   }
    2153         923 :   return gerepileupto(av, f);
    2154             : }

Generated by: LCOV version 1.13