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.12.1 lcov report (development 24988-2584e74448) Lines: 1074 1199 89.6 %
Date: 2020-01-26 05:57:03 Functions: 126 140 90.0 %
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. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /***********************************************************************/
      18             : /**                                                                   **/
      19             : /**                               FlxX                                **/
      20             : /**                                                                   **/
      21             : /***********************************************************************/
      22             : 
      23             : /* FlxX are t_POL with Flx coefficients.
      24             :  * Normally the variable ordering should be respected.*/
      25             : 
      26             : /*Similar to normalizepol, in place*/
      27             : /*FlxX_renormalize=zxX_renormalize */
      28             : GEN
      29     8393801 : FlxX_renormalize(GEN /*in place*/ x, long lx)
      30             : {
      31             :   long i;
      32    12476431 :   for (i = lx-1; i>1; i--)
      33    11754548 :     if (lgpol(gel(x,i))) break;
      34     8393884 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
      35     8393834 :   setlg(x, i+1); setsigne(x, i!=1); return x;
      36             : }
      37             : 
      38             : GEN
      39      613153 : pol1_FlxX(long v, long sv)
      40             : {
      41      613153 :   GEN z = cgetg(3, t_POL);
      42      613154 :   z[1] = evalsigne(1) | evalvarn(v);
      43      613154 :   gel(z,2) = pol1_Flx(sv); return z;
      44             : }
      45             : 
      46             : GEN
      47        7682 : polx_FlxX(long v, long sv)
      48             : {
      49        7682 :   GEN z = cgetg(4, t_POL);
      50        7682 :   z[1] = evalsigne(1) | evalvarn(v);
      51        7682 :   gel(z,2) = pol0_Flx(sv);
      52        7682 :   gel(z,3) = pol1_Flx(sv); return z;
      53             : }
      54             : 
      55             : long
      56     1826042 : FlxY_degreex(GEN b)
      57             : {
      58     1826042 :   long deg = -1, i;
      59     1826042 :   if (!signe(b)) return -1;
      60     6686447 :   for (i = 2; i < lg(b); ++i)
      61     4860405 :     deg = maxss(deg, degpol(gel(b, i)));
      62     1826042 :   return deg;
      63             : }
      64             : 
      65             : /*Lift coefficient of B to constant Flx, to give a FlxY*/
      66             : GEN
      67        1656 : Fly_to_FlxY(GEN B, long sv)
      68             : {
      69        1656 :   long lb=lg(B);
      70             :   long i;
      71        1656 :   GEN b=cgetg(lb,t_POL);
      72        1687 :   b[1]=evalsigne(1)|(((ulong)B[1])&VARNBITS);
      73       42549 :   for (i=2; i<lb; i++)
      74       40894 :     gel(b,i) = Fl_to_Flx(B[i], sv);
      75        1655 :   return FlxX_renormalize(b, lb);
      76             : }
      77             : 
      78             : GEN
      79     1644025 : zxX_to_FlxX(GEN B, ulong p)
      80             : {
      81     1644025 :   long i, lb = lg(B);
      82     1644025 :   GEN b = cgetg(lb,t_POL);
      83     5357112 :   for (i=2; i<lb; i++)
      84     3713087 :     gel(b,i) = zx_to_Flx(gel(B,i), p);
      85     1644025 :   b[1] = B[1]; return FlxX_renormalize(b, lb);
      86             : }
      87             : 
      88             : GEN
      89      482395 : FlxX_to_ZXX(GEN B)
      90             : {
      91      482395 :   long i, lb = lg(B);
      92      482395 :   GEN b = cgetg(lb,t_POL);
      93     2607133 :   for (i=2; i<lb; i++)
      94             :   {
      95     2124738 :     GEN c = gel(B,i);
      96     2124738 :     switch(lgpol(c))
      97             :     {
      98       44759 :       case 0:  c = gen_0; break;
      99       74453 :       case 1:  c = utoi(c[2]); break;
     100     2005526 :       default: c = Flx_to_ZX(c); break;
     101             :     }
     102     2124738 :     gel(b,i) = c;
     103             :   }
     104      482395 :   b[1] = B[1]; return b;
     105             : }
     106             : 
     107             : GEN
     108        1953 : FlxXC_to_ZXXC(GEN x)
     109        1953 : { pari_APPLY_type(t_COL, FlxX_to_ZXX(gel(x,i))) }
     110             : 
     111             : GEN
     112           0 : FlxXM_to_ZXXM(GEN x)
     113           0 : { pari_APPLY_same(FlxXC_to_ZXXC(gel(x,i))) }
     114             : 
     115             : /* Note: v is used _only_ for the t_INT. It must match
     116             :  * the variable of any t_POL coefficients. */
     117             : GEN
     118      552520 : ZXX_to_FlxX(GEN B, ulong p, long v)
     119             : {
     120      552520 :   long lb=lg(B);
     121             :   long i;
     122      552520 :   GEN b=cgetg(lb,t_POL);
     123      552520 :   b[1]=evalsigne(1)|(((ulong)B[1])&VARNBITS);
     124     4579449 :   for (i=2; i<lb; i++)
     125     4026929 :     switch (typ(gel(B,i)))
     126             :     {
     127             :     case t_INT:
     128      532364 :       gel(b,i) = Z_to_Flx(gel(B,i), p, evalvarn(v));
     129      532364 :       break;
     130             :     case t_POL:
     131     3494565 :       gel(b,i) = ZX_to_Flx(gel(B,i), p);
     132     3494565 :       break;
     133             :     }
     134      552520 :   return FlxX_renormalize(b, lb);
     135             : }
     136             : 
     137             : GEN
     138          12 : ZXXV_to_FlxXV(GEN x, ulong p, long v)
     139          12 : { pari_APPLY_type(t_VEC, ZXX_to_FlxX(gel(x,i), p, v)) }
     140             : 
     141             : GEN
     142         320 : ZXXT_to_FlxXT(GEN x, ulong p, long v)
     143             : {
     144         320 :   if (typ(x) == t_POL)
     145         306 :     return ZXX_to_FlxX(x, p, v);
     146             :   else
     147          14 :     pari_APPLY_type(t_VEC, ZXXT_to_FlxXT(gel(x,i), p, v))
     148             : }
     149             : 
     150             : GEN
     151      278214 : FlxX_to_FlxC(GEN x, long N, long sv)
     152             : {
     153             :   long i, l;
     154             :   GEN z;
     155      278214 :   l = lg(x)-1; x++;
     156      278214 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     157      278214 :   z = cgetg(N+1,t_COL);
     158      278214 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     159      278214 :   for (   ; i<=N; i++) gel(z,i) = pol0_Flx(sv);
     160      278214 :   return z;
     161             : }
     162             : 
     163             : static GEN
     164      121967 : FlxXV_to_FlxM_lg(GEN x, long m, long n, long sv)
     165             : {
     166             :   long i;
     167      121967 :   GEN y = cgetg(n+1, t_MAT);
     168      121967 :   for (i=1; i<=n; i++) gel(y,i) = FlxX_to_FlxC(gel(x,i), m, sv);
     169      121967 :   return y;
     170             : }
     171             : 
     172             : GEN
     173           0 : FlxXV_to_FlxM(GEN v, long n, long sv)
     174           0 : { return FlxXV_to_FlxM_lg(v, n, lg(v)-1, sv); }
     175             : 
     176             : /* matrix whose entries are given by the coeffs of the polynomial v in
     177             :  * two variables (considered as degree n polynomials) */
     178             : GEN
     179       18152 : FlxX_to_Flm(GEN v, long n)
     180             : {
     181       18152 :   long j, N = lg(v)-1;
     182       18152 :   GEN y = cgetg(N, t_MAT);
     183       18155 :   v++;
     184       18155 :   for (j=1; j<N; j++) gel(y,j) = Flx_to_Flv(gel(v,j), n);
     185       18150 :   return y;
     186             : }
     187             : 
     188             : GEN
     189       47880 : FlxX_to_Flx(GEN f)
     190             : {
     191       47880 :   long i, l = lg(f);
     192       47880 :   GEN V = cgetg(l, t_VECSMALL);
     193       47880 :   V[1] = ((ulong)f[1])&VARNBITS;
     194      687890 :   for(i=2; i<l; i++)
     195      640010 :     V[i] = lgpol(gel(f,i)) ? mael(f,i,2): 0L;
     196       47880 :   return V;
     197             : }
     198             : 
     199             : GEN
     200       35152 : Flm_to_FlxX(GEN x, long v,long w)
     201             : {
     202       35152 :   long j, lx = lg(x);
     203       35152 :   GEN y = cgetg(lx+1, t_POL);
     204       35161 :   y[1]=evalsigne(1) | v;
     205       35161 :   y++;
     206       35161 :   for (j=1; j<lx; j++) gel(y,j) = Flv_to_Flx(gel(x,j), w);
     207       35150 :   return FlxX_renormalize(--y, lx+1);
     208             : }
     209             : 
     210             : /* P(X,Y) --> P(Y,X), n is the degree in Y */
     211             : GEN
     212       22520 : FlxX_swap(GEN x, long n, long ws)
     213             : {
     214       22520 :   long j, lx = lg(x), ly = n+3;
     215       22520 :   GEN y = cgetg(ly, t_POL);
     216       22520 :   y[1] = x[1];
     217      247122 :   for (j=2; j<ly; j++)
     218             :   {
     219             :     long k;
     220      224602 :     GEN p1 = cgetg(lx, t_VECSMALL);
     221      224602 :     p1[1] = ws;
     222     7143083 :     for (k=2; k<lx; k++)
     223     6918481 :       if (j<lg(gel(x,k)))
     224     5661249 :         p1[k] = mael(x,k,j);
     225             :       else
     226     1257232 :         p1[k] = 0;
     227      224602 :     gel(y,j) = Flx_renormalize(p1,lx);
     228             :   }
     229       22520 :   return FlxX_renormalize(y,ly);
     230             : }
     231             : 
     232             : static GEN
     233     1673459 : zxX_to_Kronecker_spec(GEN P, long lp, long n)
     234             : { /* P(X) = sum Pi(Y) * X^i, return P( Y^(2n-1) ) */
     235     1673459 :   long i, j, k, l, N = (n<<1) + 1;
     236     1673459 :   GEN y = cgetg((N-2)*lp + 2, t_VECSMALL) + 2;
     237    16477408 :   for (k=i=0; i<lp; i++)
     238             :   {
     239    16463498 :     GEN c = gel(P,i);
     240    16463498 :     l = lg(c);
     241    16463498 :     if (l-3 >= n)
     242           0 :       pari_err_BUG("zxX_to_Kronecker, P is not reduced mod Q");
     243    16463611 :     for (j=2; j < l; j++) y[k++] = c[j];
     244    16463611 :     if (i == lp-1) break;
     245    14803945 :     for (   ; j < N; j++) y[k++] = 0;
     246             :   }
     247     1673576 :   y -= 2;
     248     1673576 :   y[1] = P[1]; setlg(y, k+2); return y;
     249             : }
     250             : 
     251             : GEN
     252     1328221 : zxX_to_Kronecker(GEN P, GEN Q)
     253             : {
     254     1328221 :   GEN z = zxX_to_Kronecker_spec(P+2, lg(P)-2, degpol(Q));
     255     1328238 :   z[1] = P[1]; return z;
     256             : }
     257             : 
     258             : GEN
     259      240342 : FlxX_add(GEN x, GEN y, ulong p)
     260             : {
     261             :   long i,lz;
     262             :   GEN z;
     263      240342 :   long lx=lg(x);
     264      240342 :   long ly=lg(y);
     265      240342 :   if (ly>lx) swapspec(x,y, lx,ly);
     266      240342 :   lz = lx; z = cgetg(lz, t_POL); z[1]=x[1];
     267      240353 :   for (i=2; i<ly; i++) gel(z,i) = Flx_add(gel(x,i), gel(y,i), p);
     268      240335 :   for (   ; i<lx; i++) gel(z,i) = Flx_copy(gel(x,i));
     269      240339 :   return FlxX_renormalize(z, lz);
     270             : }
     271             : 
     272             : GEN
     273         364 : FlxX_Flx_add(GEN y, GEN x, ulong p)
     274             : {
     275         364 :   long i, lz = lg(y);
     276             :   GEN z;
     277         364 :   if (signe(y) == 0) return scalarpol(x, varn(y));
     278         364 :   z = cgetg(lz,t_POL); z[1] = y[1];
     279         364 :   gel(z,2) = Flx_add(gel(y,2), x, p);
     280         364 :   if (lz == 3) z = FlxX_renormalize(z,lz);
     281             :   else
     282         294 :     for(i=3;i<lz;i++) gel(z,i) = Flx_copy(gel(y,i));
     283         364 :   return z;
     284             : }
     285             : 
     286             : GEN
     287       10453 : FlxX_Flx_sub(GEN y, GEN x, ulong p)
     288             : {
     289       10453 :   long i, lz = lg(y);
     290             :   GEN z;
     291       10453 :   if (signe(y) == 0) return scalarpol(x, varn(y));
     292       10453 :   z = cgetg(lz,t_POL); z[1] = y[1];
     293       10453 :   gel(z,2) = Flx_sub(gel(y,2), x, p);
     294       10453 :   if (lz == 3) z = FlxX_renormalize(z,lz);
     295             :   else
     296        8673 :     for(i=3;i<lz;i++) gel(z,i) = Flx_copy(gel(y,i));
     297       10453 :   return z;
     298             : }
     299             : 
     300             : GEN
     301        1795 : FlxX_neg(GEN x, ulong p)
     302             : {
     303        1795 :   long i, lx=lg(x);
     304        1795 :   GEN z = cgetg(lx, t_POL);
     305        1796 :   z[1]=x[1];
     306        1796 :   for (i=2; i<lx; i++) gel(z,i) = Flx_neg(gel(x,i), p);
     307        1795 :   return z;
     308             : }
     309             : 
     310             : GEN
     311         219 : FlxX_Fl_mul(GEN x, ulong y, 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         219 :   for (i=2; i<lx; i++) gel(z,i) = Flx_Fl_mul(gel(x,i), y, p);
     317         219 :   return FlxX_renormalize(z, lx);
     318             : }
     319             : 
     320             : GEN
     321           0 : FlxX_triple(GEN x, ulong p)
     322             : {
     323           0 :   long i, lx=lg(x);
     324           0 :   GEN z = cgetg(lx, t_POL);
     325           0 :   z[1]=x[1];
     326           0 :   for (i=2; i<lx; i++) gel(z,i) = Flx_triple(gel(x,i), p);
     327           0 :   return FlxX_renormalize(z, lx);
     328             : }
     329             : 
     330             : GEN
     331         219 : FlxX_double(GEN x, ulong p)
     332             : {
     333         219 :   long i, lx=lg(x);
     334         219 :   GEN z = cgetg(lx, t_POL);
     335         219 :   z[1]=x[1];
     336         219 :   for (i=2; i<lx; i++) gel(z,i) = Flx_double(gel(x,i), p);
     337         219 :   return FlxX_renormalize(z, lx);
     338             : }
     339             : 
     340             : GEN
     341       66759 : FlxX_deriv(GEN z, ulong p)
     342             : {
     343       66759 :   long i,l = lg(z)-1;
     344             :   GEN x;
     345       66759 :   if (l < 2) l = 2;
     346       66759 :   x = cgetg(l, t_POL); x[1] = z[1];
     347       66759 :   for (i=2; i<l; i++) gel(x,i) = Flx_mulu(gel(z,i+1), (ulong) i-1, p);
     348       66759 :   return FlxX_renormalize(x,l);
     349             : }
     350             : 
     351             : GEN
     352           0 : FlxX_translate1(GEN P, long p, long n)
     353             : {
     354             :   GEN Q;
     355           0 :   long i, l, ws, lP = lgpol(P);
     356           0 :   if (!lP) return gcopy(P);
     357           0 :   ws = mael(P,2,1);
     358           0 :   Q = FlxX_swap(P, n, ws);
     359           0 :   l = lg(Q);
     360           0 :   for (i=2; i<l; i++) gel(Q, i) = Flx_translate1(gel(Q, i), p);
     361           0 :   return FlxX_swap(Q, lP, ws);
     362             : }
     363             : 
     364             : GEN
     365           0 : zlxX_translate1(GEN P, long p, long e, long n)
     366             : {
     367             :   GEN Q;
     368           0 :   long i, l, ws, lP = lgpol(P);
     369           0 :   if (!lP) return gcopy(P);
     370           0 :   ws = mael(P,2,1);
     371           0 :   Q = FlxX_swap(P, n, ws);
     372           0 :   l = lg(Q);
     373           0 :   for (i=2; i<l; i++) gel(Q, i) = zlx_translate1(gel(Q, i), p, e);
     374           0 :   return FlxX_swap(Q, lP, ws);
     375             : }
     376             : 
     377             : static GEN
     378       66036 : FlxX_subspec(GEN x, GEN y, ulong p, long lx, long ly)
     379             : {
     380             :   long i,lz;
     381             :   GEN z;
     382             : 
     383       66036 :   if (ly <= lx)
     384             :   {
     385       66036 :     lz = lx+2; z = cgetg(lz, t_POL);
     386       66036 :     for (i=0; i<ly; i++) gel(z,i+2) = Flx_sub(gel(x,i),gel(y,i),p);
     387       66036 :     for (   ; i<lx; i++) gel(z,i+2) = Flx_copy(gel(x,i));
     388             :   }
     389             :   else
     390             :   {
     391           0 :     lz = ly+2; z = cgetg(lz, t_POL);
     392           0 :     for (i=0; i<lx; i++) gel(z,i+2) = Flx_sub(gel(x,i),gel(y,i),p);
     393           0 :     for (   ; i<ly; i++) gel(z,i+2) = Flx_neg(gel(y,i),p);
     394             :   }
     395       66036 :   z[1] = 0; return FlxX_renormalize(z, lz);
     396             : }
     397             : 
     398             : GEN
     399      130521 : FlxX_sub(GEN x, GEN y, ulong p)
     400             : {
     401             :   long lx,ly,i,lz;
     402             :   GEN z;
     403      130521 :   lx = lg(x); ly = lg(y);
     404      130521 :   lz=maxss(lx,ly);
     405      130521 :   z = cgetg(lz,t_POL);
     406      130538 :   if (lx >= ly)
     407             :   {
     408       70918 :     z[1] = x[1];
     409       70918 :     for (i=2; i<ly; i++) gel(z,i) = Flx_sub(gel(x,i),gel(y,i),p);
     410       70918 :     for (   ; i<lx; i++) gel(z,i) = Flx_copy(gel(x,i));
     411       70918 :     if (lx==ly) z = FlxX_renormalize(z, lz);
     412             :   }
     413             :   else
     414             :   {
     415       59620 :     z[1] = y[1];
     416       59620 :     for (i=2; i<lx; i++) gel(z,i) = Flx_sub(gel(x,i),gel(y,i),p);
     417       59602 :     for (   ; i<ly; i++) gel(z,i) = Flx_neg(gel(y,i),p);
     418             :   }
     419      130519 :   if (!lgpol(z)) { set_avma((pari_sp)(z + lz)); z = pol_0(varn(x)); }
     420      130516 :   return z;
     421             : }
     422             : 
     423             : GEN
     424           0 : FlxX_Flx_mul(GEN P, GEN U, ulong p)
     425             : {
     426           0 :   long i, lP = lg(P);
     427           0 :   GEN res = cgetg(lP,t_POL);
     428           0 :   res[1] = P[1];
     429           0 :   for(i=2; i<lP; i++)
     430           0 :     gel(res,i) = Flx_mul(U,gel(P,i), p);
     431           0 :   return FlxX_renormalize(res, lP);
     432             : }
     433             : 
     434             : GEN
     435      263414 : FlxY_evalx(GEN Q, ulong x, ulong p)
     436             : {
     437             :   GEN z;
     438      263414 :   long i, lb = lg(Q);
     439      263414 :   z = cgetg(lb,t_VECSMALL); z[1] = evalvarn(varn(Q));
     440      263331 :   for (i=2; i<lb; i++) z[i] = Flx_eval(gel(Q,i), x, p);
     441      263408 :   return Flx_renormalize(z, lb);
     442             : }
     443             : 
     444             : GEN
     445           0 : FlxY_Flx_translate(GEN P, GEN c, ulong p)
     446             : {
     447           0 :   pari_sp av = avma;
     448             :   GEN Q;
     449             :   long i, k, n;
     450             : 
     451           0 :   if (!signe(P) || gequal0(c)) return RgX_copy(P);
     452           0 :   Q = leafcopy(P); n = degpol(P);
     453           0 :   for (i=1; i<=n; i++)
     454             :   {
     455           0 :     for (k=n-i; k<n; k++)
     456           0 :       gel(Q,2+k) = Flx_add(gel(Q,2+k), Flx_mul(gel(Q,2+k+1), c, p), p);
     457           0 :     if (gc_needed(av,2))
     458             :     {
     459           0 :       if(DEBUGMEM>1)
     460           0 :         pari_warn(warnmem,"FlxY_Flx_translate, i = %ld/%ld", i,n);
     461           0 :       Q = gerepilecopy(av, Q);
     462             :     }
     463             :   }
     464           0 :   return gerepilecopy(av, Q);
     465             : }
     466             : 
     467             : GEN
     468     7953969 : FlxY_evalx_powers_pre(GEN pol, GEN ypowers, ulong p, ulong pi)
     469             : {
     470     7953969 :   long i, len = lg(pol);
     471     7953969 :   GEN res = cgetg(len, t_VECSMALL);
     472     7953969 :   res[1] = pol[1] & VARNBITS;
     473    26959151 :   for (i = 2; i < len; ++i)
     474    19005182 :     res[i] = Flx_eval_powers_pre(gel(pol, i), ypowers, p, pi);
     475     7953969 :   return Flx_renormalize(res, len);
     476             : }
     477             : 
     478             : ulong
     479     5333340 : FlxY_eval_powers_pre(GEN pol, GEN ypowers, GEN xpowers, ulong p, ulong pi)
     480             : {
     481     5333340 :   pari_sp av = avma;
     482     5333340 :   GEN t = FlxY_evalx_powers_pre(pol, ypowers, p, pi);
     483     5333340 :   return gc_ulong(av, Flx_eval_powers_pre(t, xpowers, p, pi));
     484             : }
     485             : 
     486             : GEN
     487      120942 : FlxY_FlxqV_evalx(GEN P, GEN x, GEN T, ulong p)
     488             : {
     489      120942 :   long i, lP = lg(P);
     490      120942 :   GEN res = cgetg(lP,t_POL);
     491      120942 :   res[1] = P[1];
     492      777893 :   for(i=2; i<lP; i++)
     493      656951 :     gel(res,i) = Flx_FlxqV_eval(gel(P,i), x, T, p);
     494      120942 :   return FlxX_renormalize(res, lP);
     495             : }
     496             : 
     497             : GEN
     498           0 : FlxY_Flxq_evalx(GEN P, GEN x, GEN T, ulong p)
     499             : {
     500           0 :   pari_sp av = avma;
     501           0 :   long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(P),1);
     502           0 :   GEN xp = Flxq_powers(x, n, T, p);
     503           0 :   return gerepileupto(av, FlxY_FlxqV_evalx(P, xp, T, p));
     504             : }
     505             : 
     506             : GEN
     507        4935 : FlxY_Flx_div(GEN x, GEN y, ulong p)
     508             : {
     509             :   long i, l;
     510             :   GEN z;
     511        4935 :   if (degpol(y) == 0)
     512             :   {
     513        3652 :     ulong t = uel(y,2);
     514        3652 :     if (t == 1) return x;
     515           0 :     t = Fl_inv(t, p);
     516           0 :     z = cgetg_copy(x, &l); z[1] = x[1];
     517           0 :     for (i=2; i<l; i++) gel(z,i) = Flx_Fl_mul(gel(x,i),t,p);
     518             :   }
     519             :   else
     520             :   {
     521        1284 :     z = cgetg_copy(x, &l); z[1] = x[1];
     522        1284 :     for (i=2; i<l; i++) gel(z,i) = Flx_div(gel(x,i),y,p);
     523             :   }
     524        1285 :   return z;
     525             : }
     526             : 
     527             : GEN
     528       32757 : FlxX_shift(GEN a, long n, long vs)
     529             : {
     530       32757 :   long i, l = lg(a);
     531             :   GEN  b;
     532       32757 :   if (l == 2 || !n) return a;
     533       28065 :   l += n;
     534       28065 :   if (n < 0)
     535             :   {
     536       17150 :     if (l <= 2) return pol_0(varn(a));
     537       12977 :     b = cgetg(l, t_POL); b[1] = a[1];
     538       12977 :     a -= n;
     539       12977 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     540             :   } else {
     541       10915 :     b = cgetg(l, t_POL); b[1] = a[1];
     542       10923 :     a -= n; n += 2;
     543       10923 :     for (i=2; i<n; i++) gel(b,i) = pol0_Flx(vs);
     544       10917 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     545             :   }
     546       23894 :   return b;
     547             : }
     548             : 
     549             : GEN
     550        9065 : FlxX_blocks(GEN P, long n, long m, long vs)
     551             : {
     552        9065 :   GEN z = cgetg(m+1,t_VEC);
     553        9070 :   long i,j, k=2, l = lg(P);
     554       27198 :   for(i=1; i<=m; i++)
     555             :   {
     556       18132 :     GEN zi = cgetg(n+2,t_POL);
     557       18154 :     zi[1] = P[1];
     558       18154 :     gel(z,i) = zi;
     559      129713 :     for(j=2; j<n+2; j++)
     560      111582 :       gel(zi, j) = k==l ? pol0_Flx(vs) : gel(P,k++);
     561       18131 :     zi = FlxX_renormalize(zi, n+2);
     562             :   }
     563        9066 :   return z;
     564             : }
     565             : 
     566             : static GEN
     567      141366 : FlxX_recipspec(GEN x, long l, long n, long vs)
     568             : {
     569             :   long i;
     570      141366 :   GEN z = cgetg(n+2,t_POL);
     571      141366 :   z[1] = 0; z += 2;
     572     3342181 :   for(i=0; i<l; i++)
     573     3200815 :     gel(z,n-i-1) = Flx_copy(gel(x,i));
     574      148114 :   for(   ; i<n; i++)
     575        6748 :     gel(z,n-i-1) = pol0_Flx(vs);
     576      141366 :   return FlxX_renormalize(z-2,n+2);
     577             : }
     578             : 
     579             : GEN
     580        1564 : FlxX_invLaplace(GEN x, ulong p)
     581             : {
     582        1564 :   long i, d = degpol(x);
     583             :   GEN y;
     584             :   ulong t;
     585        1564 :   if (d <= 1) return gcopy(x);
     586        1557 :   t = Fl_inv(factorial_Fl(d, p), p);
     587        1557 :   y = cgetg(d+3, t_POL);
     588        1565 :   y[1] = x[1];
     589       39887 :   for (i=d; i>=2; i--)
     590             :   {
     591       38330 :     gel(y,i+2) = Flx_Fl_mul(gel(x,i+2), t, p);
     592       38302 :     t = Fl_mul(t, i, p);
     593             :   }
     594        1557 :   gel(y,3) = Flx_copy(gel(x,3));
     595        1557 :   gel(y,2) = Flx_copy(gel(x,2));
     596        1557 :   return FlxX_renormalize(y, d+3);
     597             : }
     598             : 
     599             : GEN
     600         782 : FlxX_Laplace(GEN x, ulong p)
     601             : {
     602         782 :   long i, d = degpol(x);
     603         782 :   ulong t = 1;
     604             :   GEN y;
     605         782 :   if (d <= 1) return gcopy(x);
     606         782 :   y = cgetg(d+3, t_POL);
     607         782 :   y[1] = x[1];
     608         782 :   gel(y,2) = Flx_copy(gel(x,2));
     609         782 :   gel(y,3) = Flx_copy(gel(x,3));
     610       20125 :   for (i=2; i<=d; i++)
     611             :   {
     612       19343 :     t = Fl_mul(t, i%p, p);
     613       19354 :     gel(y,i+2) = Flx_Fl_mul(gel(x,i+2), t, p);
     614             :   }
     615         782 :   return FlxX_renormalize(y, d+3);
     616             : }
     617             : 
     618             : /***********************************************************************/
     619             : /**                                                                   **/
     620             : /**                               FlxqX                               **/
     621             : /**                                                                   **/
     622             : /***********************************************************************/
     623             : 
     624             : static GEN
     625     1673802 : get_FlxqX_red(GEN T, GEN *B)
     626             : {
     627     1673802 :   if (typ(T)!=t_VEC) { *B=NULL; return T; }
     628       83144 :   *B = gel(T,1); return gel(T,2);
     629             : }
     630             : 
     631             : GEN
     632       38141 : RgX_to_FlxqX(GEN x, GEN T, ulong p)
     633             : {
     634       38141 :   long i, l = lg(x);
     635       38141 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     636      683941 :   for (i = 2; i < l; i++)
     637      645800 :     gel(z,i) = Rg_to_Flxq(gel(x,i), T, p);
     638       38141 :   return FlxX_renormalize(z, l);
     639             : }
     640             : 
     641             : /* FlxqX are t_POL with Flxq coefficients.
     642             :  * Normally the variable ordering should be respected.*/
     643             : 
     644             : GEN
     645         529 : random_FlxqX(long d1, long v, GEN T, ulong p)
     646             : {
     647         529 :   long dT = get_Flx_degree(T), vT = get_Flx_var(T);
     648         529 :   long i, d = d1+2;
     649         529 :   GEN y = cgetg(d,t_POL); y[1] = evalsigne(1) | evalvarn(v);
     650         529 :   for (i=2; i<d; i++) gel(y,i) = random_Flx(dT, vT, p);
     651         529 :   return FlxX_renormalize(y,d);
     652             : }
     653             : 
     654             : /*Not stack clean*/
     655             : GEN
     656      944009 : Kronecker_to_FlxqX(GEN z, GEN T, ulong p)
     657             : {
     658      944009 :   long i,j,lx,l, N = (get_Flx_degree(T)<<1) + 1;
     659      943999 :   GEN x, t = cgetg(N,t_VECSMALL);
     660      944002 :   t[1] = get_Flx_var(T);
     661      943998 :   l = lg(z); lx = (l-2) / (N-2);
     662      943998 :   x = cgetg(lx+3,t_POL);
     663      944263 :   x[1] = z[1];
     664    16862027 :   for (i=2; i<lx+2; i++)
     665             :   {
     666    15918053 :     for (j=2; j<N; j++) t[j] = z[j];
     667    15918053 :     z += (N-2);
     668    15918053 :     gel(x,i) = Flx_rem(Flx_renormalize(t,N), T,p);
     669             :   }
     670      943974 :   N = (l-2) % (N-2) + 2;
     671      943974 :   for (j=2; j<N; j++) t[j] = z[j];
     672      943974 :   gel(x,i) = Flx_rem(Flx_renormalize(t,N), T,p);
     673      943981 :   return FlxX_renormalize(x, i+1);
     674             : }
     675             : 
     676             : GEN
     677      791391 : FlxqX_red(GEN z, GEN T, ulong p)
     678             : {
     679             :   GEN res;
     680      791391 :   long i, l = lg(z);
     681      791391 :   res = cgetg(l,t_POL); res[1] = z[1];
     682      791391 :   for(i=2;i<l;i++) gel(res,i) = Flx_rem(gel(z,i),T,p);
     683      791391 :   return FlxX_renormalize(res,l);
     684             : }
     685             : 
     686             : static GEN
     687      172620 : FlxqX_mulspec(GEN x, GEN y, GEN T, ulong p, long lx, long ly)
     688             : {
     689      172620 :   pari_sp ltop=avma;
     690             :   GEN z,kx,ky;
     691      172620 :   long dT =  get_Flx_degree(T);
     692      172620 :   kx= zxX_to_Kronecker_spec(x,lx,dT);
     693      172620 :   ky= zxX_to_Kronecker_spec(y,ly,dT);
     694      172620 :   z = Flx_mul(ky, kx, p);
     695      172620 :   z = Kronecker_to_FlxqX(z,T,p);
     696      172620 :   return gerepileupto(ltop,z);
     697             : }
     698             : 
     699             : GEN
     700      556822 : FlxqX_mul(GEN x, GEN y, GEN T, ulong p)
     701             : {
     702      556822 :   pari_sp ltop=avma;
     703      556822 :   GEN z, kx, ky, Tm = get_Flx_mod(T);
     704      556826 :   kx= zxX_to_Kronecker(x, Tm);
     705      556834 :   ky= zxX_to_Kronecker(y, Tm);
     706      556841 :   z = Flx_mul(ky, kx, p);
     707      556811 :   z = Kronecker_to_FlxqX(z, T, p);
     708      556799 :   return gerepileupto(ltop, z);
     709             : }
     710             : 
     711             : GEN
     712      214581 : FlxqX_sqr(GEN x, GEN T, ulong p)
     713             : {
     714      214581 :   pari_sp ltop=avma;
     715             :   GEN z,kx;
     716      214581 :   kx= zxX_to_Kronecker(x,get_Flx_mod(T));
     717      214581 :   z = Flx_sqr(kx, p);
     718      214581 :   z = Kronecker_to_FlxqX(z,T,p);
     719      214581 :   return gerepileupto(ltop,z);
     720             : }
     721             : 
     722             : GEN
     723       12312 : FlxqX_Flxq_mul(GEN P, GEN U, GEN T, ulong p)
     724             : {
     725       12312 :   long i, lP = lg(P);
     726       12312 :   GEN res = cgetg(lP,t_POL);
     727       12316 :   res[1] = P[1];
     728       71636 :   for(i=2; i<lP; i++)
     729       59324 :     gel(res,i) = Flxq_mul(U,gel(P,i), T,p);
     730       12312 :   return FlxX_renormalize(res, lP);
     731             : }
     732             : GEN
     733      255707 : FlxqX_Flxq_mul_to_monic(GEN P, GEN U, GEN T, ulong p)
     734             : {
     735      255707 :   long i, lP = lg(P);
     736      255707 :   GEN res = cgetg(lP,t_POL);
     737      255707 :   res[1] = P[1];
     738      255707 :   for(i=2; i<lP-1; i++) gel(res,i) = Flxq_mul(U,gel(P,i), T,p);
     739      255707 :   gel(res,lP-1) = pol1_Flx(get_Flx_var(T));
     740      255707 :   return FlxX_renormalize(res, lP);
     741             : }
     742             : 
     743             : GEN
     744      172995 : FlxqX_normalize(GEN z, GEN T, ulong p)
     745             : {
     746      172995 :   GEN p1 = leading_coeff(z);
     747      172995 :   if (!lgpol(z) || (!degpol(p1) && p1[1] == 1)) return z;
     748      172974 :   return FlxqX_Flxq_mul_to_monic(z, Flxq_inv(p1,T,p), T,p);
     749             : }
     750             : 
     751             : /* x and y in Z[Y][X]. Assume T irreducible mod p */
     752             : static GEN
     753     1311696 : FlxqX_divrem_basecase(GEN x, GEN y, GEN T, ulong p, GEN *pr)
     754             : {
     755             :   long vx, dx, dy, dz, i, j, sx, lr;
     756             :   pari_sp av0, av, tetpil;
     757             :   GEN z,p1,rem,lead;
     758             : 
     759     1311696 :   if (!signe(y)) pari_err_INV("FlxqX_divrem",y);
     760     1311696 :   vx=varn(x); dy=degpol(y); dx=degpol(x);
     761     1311696 :   if (dx < dy)
     762             :   {
     763       14185 :     if (pr)
     764             :     {
     765       13949 :       av0 = avma; x = FlxqX_red(x, T, p);
     766       13949 :       if (pr == ONLY_DIVIDES) { set_avma(av0); return signe(x)? NULL: pol_0(vx); }
     767       13949 :       if (pr == ONLY_REM) return x;
     768       13949 :       *pr = x;
     769             :     }
     770       14185 :     return pol_0(vx);
     771             :   }
     772     1297511 :   lead = leading_coeff(y);
     773     1297511 :   if (!dy) /* y is constant */
     774             :   {
     775      125011 :     if (pr && pr != ONLY_DIVIDES)
     776             :     {
     777      120419 :       if (pr == ONLY_REM) return pol_0(vx);
     778        8953 :       *pr = pol_0(vx);
     779             :     }
     780       13545 :     if (Flx_equal1(lead)) return gcopy(x);
     781        9415 :     av0 = avma; x = FlxqX_Flxq_mul(x,Flxq_inv(lead,T,p),T,p);
     782        9415 :     return gerepileupto(av0,x);
     783             :   }
     784     1172500 :   av0 = avma; dz = dx-dy;
     785     1172500 :   lead = Flx_equal1(lead)? NULL: gclone(Flxq_inv(lead,T,p));
     786     1172500 :   set_avma(av0);
     787     1172500 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
     788     1172500 :   x += 2; y += 2; z += 2;
     789             : 
     790     1172500 :   p1 = gel(x,dx); av = avma;
     791     1172500 :   gel(z,dz) = lead? gerepileupto(av, Flxq_mul(p1,lead, T, p)): gcopy(p1);
     792     3250355 :   for (i=dx-1; i>=dy; i--)
     793             :   {
     794     2077855 :     av=avma; p1=gel(x,i);
     795     7674861 :     for (j=i-dy+1; j<=i && j<=dz; j++)
     796     5597006 :       p1 = Flx_sub(p1, Flx_mul(gel(z,j),gel(y,i-j),p),p);
     797     2077855 :     if (lead) p1 = Flx_mul(p1, lead,p);
     798     2077855 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil,Flx_rem(p1,T,p));
     799             :   }
     800     1172500 :   if (!pr) { guncloneNULL(lead); return z-2; }
     801             : 
     802     1137590 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
     803     1393531 :   for (sx=0; ; i--)
     804             :   {
     805     1649472 :     p1 = gel(x,i);
     806     4690662 :     for (j=0; j<=i && j<=dz; j++)
     807     3297131 :       p1 = Flx_sub(p1, Flx_mul(gel(z,j),gel(y,i-j),p),p);
     808     1393531 :     tetpil=avma; p1 = Flx_rem(p1, T, p); if (lgpol(p1)) { sx = 1; break; }
     809      316169 :     if (!i) break;
     810      255941 :     set_avma(av);
     811             :   }
     812     1137590 :   if (pr == ONLY_DIVIDES)
     813             :   {
     814           0 :     guncloneNULL(lead);
     815           0 :     if (sx) return gc_NULL(av0);
     816           0 :     set_avma((pari_sp)rem); return z-2;
     817             :   }
     818     1137590 :   lr=i+3; rem -= lr;
     819     1137590 :   rem[0] = evaltyp(t_POL) | evallg(lr);
     820     1137590 :   rem[1] = z[-1];
     821     1137590 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
     822     1137590 :   rem += 2; gel(rem,i) = p1;
     823    10748306 :   for (i--; i>=0; i--)
     824             :   {
     825     9610716 :     av=avma; p1 = gel(x,i);
     826    33791515 :     for (j=0; j<=i && j<=dz; j++)
     827    24180799 :       p1 = Flx_sub(p1, Flx_mul(gel(z,j),gel(y,i-j),p), p);
     828     9610716 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, Flx_rem(p1, T, p));
     829             :   }
     830     1137590 :   rem -= 2;
     831     1137590 :   guncloneNULL(lead);
     832     1137590 :   if (!sx) (void)FlxX_renormalize(rem, lr);
     833     1137590 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
     834      223851 :   *pr = rem; return z-2;
     835             : }
     836             : 
     837             : static GEN
     838        1487 : FlxqX_invBarrett_basecase(GEN T, GEN Q, ulong p)
     839             : {
     840        1487 :   long i, l=lg(T)-1, lr = l-1, k;
     841        1487 :   long sv=Q[1];
     842        1487 :   GEN r=cgetg(lr,t_POL); r[1]=T[1];
     843        1487 :   gel(r,2) = pol1_Flx(sv);
     844       15519 :   for (i=3;i<lr;i++)
     845             :   {
     846       14032 :     pari_sp ltop=avma;
     847       14032 :     GEN u = Flx_neg(gel(T,l-i+2),p);
     848       97331 :     for (k=3;k<i;k++)
     849       83299 :       u = Flx_sub(u, Flxq_mul(gel(T,l-i+k),gel(r,k),Q,p),p);
     850       14032 :     gel(r,i) = gerepileupto(ltop, u);
     851             :   }
     852        1487 :   r = FlxX_renormalize(r,lr);
     853        1487 :   return r;
     854             : }
     855             : 
     856             : /* Return new lgpol */
     857             : static long
     858      191561 : FlxX_lgrenormalizespec(GEN x, long lx)
     859             : {
     860             :   long i;
     861      220745 :   for (i = lx-1; i>=0; i--)
     862      220745 :     if (lgpol(gel(x,i))) break;
     863      191561 :   return i+1;
     864             : }
     865             : 
     866             : static GEN
     867        3548 : FlxqX_invBarrett_Newton(GEN S, GEN T, ulong p)
     868             : {
     869        3548 :   pari_sp av = avma;
     870        3548 :   long nold, lx, lz, lq, l = degpol(S), i, lQ;
     871        3548 :   GEN q, y, z, x = cgetg(l+2, t_POL) + 2;
     872        3548 :   long dT = get_Flx_degree(T), vT = get_Flx_var(T);
     873        3548 :   ulong mask = quadratic_prec_mask(l-2); /* assume l > 2 */
     874        3548 :   for (i=0;i<l;i++) gel(x,i) = pol0_Flx(vT);
     875        3548 :   q = FlxX_recipspec(S+2,l+1,l+1,dT);
     876        3548 :   lQ = lgpol(q); q+=2;
     877             :   /* We work on _spec_ FlxX's, all the l[xzq] below are lgpol's */
     878             : 
     879             :   /* initialize */
     880        3548 :   gel(x,0) = Flxq_inv(gel(q,0),T, p);
     881        3548 :   if (lQ>1 && degpol(gel(q,1)) >= dT)
     882           0 :     gel(q,1) = Flx_rem(gel(q,1), T, p);
     883        3548 :   if (lQ>1 && lgpol(gel(q,1)))
     884        2537 :   {
     885        2537 :     GEN u = gel(q, 1);
     886        2537 :     if (!Flx_equal1(gel(x,0))) u = Flxq_mul(u, Flxq_sqr(gel(x,0), T,p), T,p);
     887        2537 :     gel(x,1) = Flx_neg(u, p); lx = 2;
     888             :   }
     889             :   else
     890        1011 :     lx = 1;
     891        3548 :   nold = 1;
     892       27685 :   for (; mask > 1; )
     893             :   { /* set x -= x(x*q - 1) + O(t^(nnew + 1)), knowing x*q = 1 + O(t^(nold+1)) */
     894       20589 :     long i, lnew, nnew = nold << 1;
     895             : 
     896       20589 :     if (mask & 1) nnew--;
     897       20589 :     mask >>= 1;
     898             : 
     899       20589 :     lnew = nnew + 1;
     900       20589 :     lq = FlxX_lgrenormalizespec(q, minss(lQ,lnew));
     901       20589 :     z = FlxqX_mulspec(x, q, T, p, lx, lq); /* FIXME: high product */
     902       20589 :     lz = lgpol(z); if (lz > lnew) lz = lnew;
     903       20589 :     z += 2;
     904             :     /* subtract 1 [=>first nold words are 0]: renormalize so that z(0) != 0 */
     905       20589 :     for (i = nold; i < lz; i++) if (lgpol(gel(z,i))) break;
     906       20589 :     nold = nnew;
     907       20589 :     if (i >= lz) continue; /* z-1 = 0(t^(nnew + 1)) */
     908             : 
     909             :     /* z + i represents (x*q - 1) / t^i */
     910       19686 :     lz = FlxX_lgrenormalizespec (z+i, lz-i);
     911       19686 :     z = FlxqX_mulspec(x, z+i, T,p, lx, lz); /* FIXME: low product */
     912       19686 :     lz = lgpol(z); z += 2;
     913       19686 :     if (lz > lnew-i) lz = FlxX_lgrenormalizespec(z, lnew-i);
     914             : 
     915       19686 :     lx = lz+ i;
     916       19686 :     y  = x + i; /* x -= z * t^i, in place */
     917       19686 :     for (i = 0; i < lz; i++) gel(y,i) = Flx_neg(gel(z,i), p);
     918             :   }
     919        3548 :   x -= 2; setlg(x, lx + 2); x[1] = S[1];
     920        3548 :   return gerepilecopy(av, x);
     921             : }
     922             : 
     923             : GEN
     924        5035 : FlxqX_invBarrett(GEN T, GEN Q, ulong p)
     925             : {
     926        5035 :   pari_sp ltop=avma;
     927        5035 :   long l=lg(T), v = varn(T);
     928             :   GEN r;
     929        5035 :   GEN c = gel(T,l-1);
     930        5035 :   if (l<5) return pol_0(v);
     931        5035 :   if (l<=FlxqX_INVBARRETT_LIMIT)
     932             :   {
     933        1487 :     if (!Flx_equal1(c))
     934             :     {
     935           0 :       GEN ci = Flxq_inv(c,Q,p);
     936           0 :       T = FlxqX_Flxq_mul(T, ci, Q, p);
     937           0 :       r = FlxqX_invBarrett_basecase(T,Q,p);
     938           0 :       r = FlxqX_Flxq_mul(r,ci,Q,p);
     939             :     } else
     940        1487 :       r = FlxqX_invBarrett_basecase(T,Q,p);
     941             :   } else
     942        3548 :     r = FlxqX_invBarrett_Newton(T,Q,p);
     943        5035 :   return gerepileupto(ltop, r);
     944             : }
     945             : 
     946             : GEN
     947      429003 : FlxqX_get_red(GEN S, GEN T, ulong p)
     948             : {
     949      429003 :   if (typ(S)==t_POL && lg(S)>FlxqX_BARRETT_LIMIT)
     950        3510 :     retmkvec2(FlxqX_invBarrett(S, T, p), S);
     951      425493 :   return S;
     952             : }
     953             : 
     954             : /* Compute x mod S where 2 <= degpol(S) <= l+1 <= 2*(degpol(S)-1)
     955             :  *  * and mg is the Barrett inverse of S. */
     956             : static GEN
     957       66309 : FlxqX_divrem_Barrettspec(GEN x, long l, GEN mg, GEN S, GEN T, ulong p, GEN *pr)
     958             : {
     959             :   GEN q, r;
     960       66309 :   long lt = degpol(S); /*We discard the leading term*/
     961             :   long ld, lm, lT, lmg;
     962       66309 :   ld = l-lt;
     963       66309 :   lm = minss(ld, lgpol(mg));
     964       66309 :   lT  = FlxX_lgrenormalizespec(S+2,lt);
     965       66309 :   lmg = FlxX_lgrenormalizespec(mg+2,lm);
     966       66309 :   q = FlxX_recipspec(x+lt,ld,ld,0);               /* q = rec(x)     lq<=ld*/
     967       66309 :   q = FlxqX_mulspec(q+2,mg+2,T,p,lgpol(q),lmg);   /* q = rec(x) * mg lq<=ld+lm*/
     968       66309 :   q = FlxX_recipspec(q+2,minss(ld,lgpol(q)),ld,0);/* q = rec (rec(x) * mg) lq<=ld*/
     969       66309 :   if (!pr) return q;
     970       66036 :   r = FlxqX_mulspec(q+2,S+2,T,p,lgpol(q),lT);     /* r = q*pol        lr<=ld+lt*/
     971       66036 :   r = FlxX_subspec(x,r+2,p,lt,minss(lt,lgpol(r)));/* r = x - r   lr<=lt */
     972       66036 :   if (pr == ONLY_REM) return r;
     973        9643 :   *pr = r; return q;
     974             : }
     975             : 
     976             : static GEN
     977       57458 : FlxqX_divrem_Barrett(GEN x, GEN mg, GEN S, GEN T, ulong p, GEN *pr)
     978             : {
     979       57458 :   GEN q = NULL, r = FlxqX_red(x, T, p);
     980       57458 :   long l = lgpol(r), lt = degpol(S), lm = 2*lt-1, v = varn(S);
     981             :   long i;
     982       57458 :   if (l <= lt)
     983             :   {
     984           0 :     if (pr == ONLY_REM) return r;
     985           0 :     if (pr == ONLY_DIVIDES) return signe(r)? NULL: pol_0(v);
     986           0 :     if (pr) *pr = r;
     987           0 :     return pol_0(v);
     988             :   }
     989       57458 :   if (lt <= 1)
     990           0 :     return FlxqX_divrem_basecase(x,S,T,p,pr);
     991       57458 :   if (pr != ONLY_REM && l>lm)
     992             :   {
     993         750 :     long vT = get_Flx_var(T);
     994         750 :     q = cgetg(l-lt+2, t_POL); q[1] = S[1];
     995         750 :     for (i=0;i<l-lt;i++) gel(q+2,i) = pol0_Flx(vT);
     996             :   }
     997      123849 :   while (l>lm)
     998             :   {
     999        8933 :     GEN zr, zq = FlxqX_divrem_Barrettspec(r+2+l-lm,lm,mg,S,T,p,&zr);
    1000        8933 :     long lz = lgpol(zr);
    1001        8933 :     if (pr != ONLY_REM)
    1002             :     {
    1003        2568 :       long lq = lgpol(zq);
    1004        2568 :       for(i=0; i<lq; i++) gel(q+2+l-lm,i) = gel(zq,2+i);
    1005             :     }
    1006        8933 :     for(i=0; i<lz; i++) gel(r+2+l-lm,i) = gel(zr,2+i);
    1007        8933 :     l = l-lm+lz;
    1008             :   }
    1009       57458 :   if (pr == ONLY_REM)
    1010             :   {
    1011       56393 :     if (l > lt)
    1012       56393 :       r = FlxqX_divrem_Barrettspec(r+2,l,mg,S,T,p,ONLY_REM);
    1013             :     else
    1014           0 :       r = FlxX_renormalize(r, l+2);
    1015       56393 :     setvarn(r, v); return r;
    1016             :   }
    1017        1065 :   if (l > lt)
    1018             :   {
    1019         983 :     GEN zq = FlxqX_divrem_Barrettspec(r+2,l,mg,S,T,p,pr? &r: NULL);
    1020         983 :     if (!q) q = zq;
    1021             :     else
    1022             :     {
    1023         668 :       long lq = lgpol(zq);
    1024         668 :       for(i=0; i<lq; i++) gel(q+2,i) = gel(zq,2+i);
    1025             :     }
    1026             :   }
    1027          82 :   else if (pr)
    1028          82 :     r = FlxX_renormalize(r, l+2);
    1029        1065 :   setvarn(q, v); q = FlxX_renormalize(q, lg(q));
    1030        1065 :   if (pr == ONLY_DIVIDES) return signe(r)? NULL: q;
    1031        1065 :   if (pr) { setvarn(r, v); *pr = r; }
    1032        1065 :   return q;
    1033             : }
    1034             : 
    1035             : GEN
    1036      287556 : FlxqX_divrem(GEN x, GEN S, GEN T, ulong p, GEN *pr)
    1037             : {
    1038             :   GEN B, y;
    1039             :   long dy, dx, d;
    1040      287556 :   if (pr==ONLY_REM) return FlxqX_rem(x, S, T, p);
    1041      287556 :   y = get_FlxqX_red(S, &B);
    1042      287556 :   dy = degpol(y); dx = degpol(x); d = dx-dy;
    1043      287556 :   if (!B && d+3 < FlxqX_DIVREM_BARRETT_LIMIT)
    1044      286491 :     return FlxqX_divrem_basecase(x,y,T,p,pr);
    1045             :   else
    1046             :   {
    1047        1065 :     pari_sp av = avma;
    1048        1065 :     GEN mg = B? B: FlxqX_invBarrett(y, T, p);
    1049        1065 :     GEN q = FlxqX_divrem_Barrett(x,mg,y,T,p,pr);
    1050        1065 :     if (!q) return gc_NULL(av);
    1051        1065 :     if (!pr || pr==ONLY_DIVIDES) return gerepilecopy(av, q);
    1052         792 :     gerepileall(av,2,&q,pr);
    1053         792 :     return q;
    1054             :   }
    1055             : }
    1056             : 
    1057             : GEN
    1058     1385816 : FlxqX_rem(GEN x, GEN S, GEN T, ulong p)
    1059             : {
    1060     1385816 :   GEN B, y = get_FlxqX_red(S, &B);
    1061     1385816 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
    1062     1385816 :   if (d < 0) return FlxqX_red(x, T, p);
    1063     1081598 :   if (!B && d+3 < FlxqX_REM_BARRETT_LIMIT)
    1064     1025205 :     return FlxqX_divrem_basecase(x,y, T, p, ONLY_REM);
    1065             :   else
    1066             :   {
    1067       56393 :     pari_sp av=avma;
    1068       56393 :     GEN mg = B? B: FlxqX_invBarrett(y, T, p);
    1069       56393 :     GEN r = FlxqX_divrem_Barrett(x, mg, y, T, p, ONLY_REM);
    1070       56393 :     return gerepileupto(av, r);
    1071             :   }
    1072             : }
    1073             : 
    1074             : static GEN
    1075         596 : FlxqX_halfgcd_basecase(GEN a, GEN b, GEN T, ulong p)
    1076             : {
    1077         596 :   pari_sp av=avma;
    1078             :   GEN u,u1,v,v1;
    1079         596 :   long vx = varn(a);
    1080         596 :   long n = lgpol(a)>>1;
    1081         596 :   u1 = v = pol_0(vx);
    1082         596 :   u = v1 = pol1_FlxX(vx, get_Flx_var(T));
    1083        9525 :   while (lgpol(b)>n)
    1084             :   {
    1085        8333 :     GEN r, q = FlxqX_divrem(a,b, T, p, &r);
    1086        8333 :     a = b; b = r; swap(u,u1); swap(v,v1);
    1087        8333 :     u1 = FlxX_sub(u1, FlxqX_mul(u, q, T, p), p);
    1088        8333 :     v1 = FlxX_sub(v1, FlxqX_mul(v, q ,T, p), p);
    1089        8333 :     if (gc_needed(av,2))
    1090             :     {
    1091           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_halfgcd (d = %ld)",degpol(b));
    1092           0 :       gerepileall(av,6, &a,&b,&u1,&v1,&u,&v);
    1093             :     }
    1094             :   }
    1095         596 :   return gerepilecopy(av, mkmat2(mkcol2(u,u1), mkcol2(v,v1)));
    1096             : }
    1097             : static GEN
    1098         768 : FlxqX_addmulmul(GEN u, GEN v, GEN x, GEN y, GEN T, ulong p)
    1099             : {
    1100         768 :   return FlxX_add(FlxqX_mul(u, x, T, p),FlxqX_mul(v, y, T, p), p);
    1101             : }
    1102             : 
    1103             : static GEN
    1104         384 : FlxqXM_FlxqX_mul2(GEN M, GEN x, GEN y, GEN T, ulong p)
    1105             : {
    1106         384 :   GEN res = cgetg(3, t_COL);
    1107         384 :   gel(res, 1) = FlxqX_addmulmul(gcoeff(M,1,1), gcoeff(M,1,2), x, y, T, p);
    1108         384 :   gel(res, 2) = FlxqX_addmulmul(gcoeff(M,2,1), gcoeff(M,2,2), x, y, T, p);
    1109         384 :   return res;
    1110             : }
    1111             : 
    1112             : static GEN
    1113         360 : FlxqXM_mul2(GEN A, GEN B, GEN T, ulong p)
    1114             : {
    1115         360 :   GEN A11=gcoeff(A,1,1),A12=gcoeff(A,1,2), B11=gcoeff(B,1,1),B12=gcoeff(B,1,2);
    1116         360 :   GEN A21=gcoeff(A,2,1),A22=gcoeff(A,2,2), B21=gcoeff(B,2,1),B22=gcoeff(B,2,2);
    1117         360 :   GEN M1 = FlxqX_mul(FlxX_add(A11,A22, p), FlxX_add(B11,B22, p), T, p);
    1118         360 :   GEN M2 = FlxqX_mul(FlxX_add(A21,A22, p), B11, T, p);
    1119         360 :   GEN M3 = FlxqX_mul(A11, FlxX_sub(B12,B22, p), T, p);
    1120         360 :   GEN M4 = FlxqX_mul(A22, FlxX_sub(B21,B11, p), T, p);
    1121         360 :   GEN M5 = FlxqX_mul(FlxX_add(A11,A12, p), B22, T, p);
    1122         360 :   GEN M6 = FlxqX_mul(FlxX_sub(A21,A11, p), FlxX_add(B11,B12, p), T, p);
    1123         360 :   GEN M7 = FlxqX_mul(FlxX_sub(A12,A22, p), FlxX_add(B21,B22, p), T, p);
    1124         360 :   GEN T1 = FlxX_add(M1,M4, p), T2 = FlxX_sub(M7,M5, p);
    1125         360 :   GEN T3 = FlxX_sub(M1,M2, p), T4 = FlxX_add(M3,M6, p);
    1126         360 :   retmkmat2(mkcol2(FlxX_add(T1,T2, p), FlxX_add(M2,M4, p)),
    1127             :             mkcol2(FlxX_add(M3,M5, p), FlxX_add(T3,T4, p)));
    1128             : }
    1129             : 
    1130             : /* Return [0,1;1,-q]*M */
    1131             : static GEN
    1132         360 : FlxqX_FlxqXM_qmul(GEN q, GEN M, GEN T, ulong p)
    1133             : {
    1134         360 :   GEN u, v, res = cgetg(3, t_MAT);
    1135         360 :   u = FlxX_sub(gcoeff(M,1,1), FlxqX_mul(gcoeff(M,2,1), q, T, p), p);
    1136         360 :   gel(res,1) = mkcol2(gcoeff(M,2,1), u);
    1137         360 :   v = FlxX_sub(gcoeff(M,1,2), FlxqX_mul(gcoeff(M,2,2), q, T, p), p);
    1138         360 :   gel(res,2) = mkcol2(gcoeff(M,2,2), v);
    1139         360 :   return res;
    1140             : }
    1141             : 
    1142             : static GEN
    1143           0 : matid2_FlxXM(long v, long sv)
    1144             : {
    1145           0 :   retmkmat2(mkcol2(pol1_FlxX(v, sv),pol_0(v)),
    1146             :             mkcol2(pol_0(v),pol1_FlxX(v, sv)));
    1147             : }
    1148             : 
    1149             : static GEN
    1150         363 : FlxqX_halfgcd_split(GEN x, GEN y, GEN T, ulong p)
    1151             : {
    1152         363 :   pari_sp av=avma;
    1153             :   GEN R, S, V;
    1154             :   GEN y1, r, q;
    1155         363 :   long l = lgpol(x), n = l>>1, k;
    1156         363 :   long vT = get_Flx_var(T);
    1157         363 :   if (lgpol(y)<=n) return matid2_FlxXM(varn(x),vT);
    1158         363 :   R = FlxqX_halfgcd(FlxX_shift(x,-n,vT),FlxX_shift(y,-n,vT), T, p);
    1159         363 :   V = FlxqXM_FlxqX_mul2(R,x,y, T, p); y1 = gel(V,2);
    1160         363 :   if (lgpol(y1)<=n) return gerepilecopy(av, R);
    1161         360 :   q = FlxqX_divrem(gel(V,1), y1, T, p, &r);
    1162         360 :   k = 2*n-degpol(y1);
    1163         360 :   S = FlxqX_halfgcd(FlxX_shift(y1,-k,vT), FlxX_shift(r,-k,vT), T, p);
    1164         360 :   return gerepileupto(av, FlxqXM_mul2(S,FlxqX_FlxqXM_qmul(q,R, T, p), T, p));
    1165             : }
    1166             : 
    1167             : /* Return M in GL_2(Fp[X]) such that:
    1168             : if [a',b']~=M*[a,b]~ then degpol(a')>= (lgpol(a)>>1) >degpol(b')
    1169             : */
    1170             : 
    1171             : static GEN
    1172         959 : FlxqX_halfgcd_i(GEN x, GEN y, GEN T, ulong p)
    1173             : {
    1174         959 :   if (lg(x)<=FlxqX_HALFGCD_LIMIT) return FlxqX_halfgcd_basecase(x, y, T, p);
    1175         363 :   return FlxqX_halfgcd_split(x, y, T, p);
    1176             : }
    1177             : 
    1178             : GEN
    1179         959 : FlxqX_halfgcd(GEN x, GEN y, GEN T, ulong p)
    1180             : {
    1181         959 :   pari_sp av = avma;
    1182             :   GEN M,q,r;
    1183         959 :   if (!signe(x))
    1184             :   {
    1185           0 :     long v = varn(x), vT = get_Flx_var(T);
    1186           0 :     retmkmat2(mkcol2(pol_0(v),pol1_FlxX(v,vT)),
    1187             :         mkcol2(pol1_FlxX(v,vT),pol_0(v)));
    1188             :   }
    1189         959 :   if (degpol(y)<degpol(x)) return FlxqX_halfgcd_i(x, y, T, p);
    1190          12 :   q = FlxqX_divrem(y, x, T, p, &r);
    1191          12 :   M = FlxqX_halfgcd_i(x, r, T, p);
    1192          12 :   gcoeff(M,1,1) = FlxX_sub(gcoeff(M,1,1), FlxqX_mul(q, gcoeff(M,1,2), T, p), p);
    1193          12 :   gcoeff(M,2,1) = FlxX_sub(gcoeff(M,2,1), FlxqX_mul(q, gcoeff(M,2,2), T, p), p);
    1194          12 :   return gerepilecopy(av, M);
    1195             : }
    1196             : 
    1197             : static GEN
    1198      154311 : FlxqX_gcd_basecase(GEN a, GEN b, GEN T, ulong p)
    1199             : {
    1200      154311 :   pari_sp av = avma, av0=avma;
    1201     1027649 :   while (signe(b))
    1202             :   {
    1203             :     GEN c;
    1204      719027 :     if (gc_needed(av0,2))
    1205             :     {
    1206          28 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_gcd (d = %ld)",degpol(b));
    1207          28 :       gerepileall(av0,2, &a,&b);
    1208             :     }
    1209      719027 :     av = avma; c = FlxqX_rem(a, b, T, p); a=b; b=c;
    1210             :   }
    1211      154311 :   set_avma(av); return a;
    1212             : }
    1213             : 
    1214             : GEN
    1215      159091 : FlxqX_gcd(GEN x, GEN y, GEN T, ulong p)
    1216             : {
    1217      159091 :   pari_sp av = avma;
    1218      159091 :   x = FlxqX_red(x, T, p);
    1219      159091 :   y = FlxqX_red(y, T, p);
    1220      159091 :   if (!signe(x)) return gerepileupto(av, y);
    1221      308643 :   while (lg(y)>FlxqX_GCD_LIMIT)
    1222             :   {
    1223             :     GEN c;
    1224          21 :     if (lgpol(y)<=(lgpol(x)>>1))
    1225             :     {
    1226           0 :       GEN r = FlxqX_rem(x, y, T, p);
    1227           0 :       x = y; y = r;
    1228             :     }
    1229          21 :     c = FlxqXM_FlxqX_mul2(FlxqX_halfgcd(x,y, T, p), x, y, T, p);
    1230          21 :     x = gel(c,1); y = gel(c,2);
    1231          21 :     gerepileall(av,2,&x,&y);
    1232             :   }
    1233      154311 :   return gerepileupto(av, FlxqX_gcd_basecase(x, y, T, p));
    1234             : }
    1235             : 
    1236             : static GEN
    1237        8960 : FlxqX_extgcd_basecase(GEN a, GEN b, GEN T, ulong p, GEN *ptu, GEN *ptv)
    1238             : {
    1239        8960 :   pari_sp av=avma;
    1240             :   GEN u,v,d,d1,v1;
    1241        8960 :   long vx = varn(a);
    1242        8960 :   d = a; d1 = b;
    1243        8960 :   v = pol_0(vx); v1 = pol1_FlxX(vx, get_Flx_var(T));
    1244       45927 :   while (signe(d1))
    1245             :   {
    1246       28007 :     GEN r, q = FlxqX_divrem(d, d1, T, p, &r);
    1247       28007 :     v = FlxX_sub(v,FlxqX_mul(q,v1,T, p),p);
    1248       28007 :     u=v; v=v1; v1=u;
    1249       28007 :     u=r; d=d1; d1=u;
    1250       28007 :     if (gc_needed(av,2))
    1251             :     {
    1252           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_extgcd (d = %ld)",degpol(d));
    1253           0 :       gerepileall(av,5, &d,&d1,&u,&v,&v1);
    1254             :     }
    1255             :   }
    1256        8960 :   if (ptu) *ptu = FlxqX_div(FlxX_sub(d,FlxqX_mul(b,v, T, p), p), a, T, p);
    1257        8960 :   *ptv = v; return d;
    1258             : }
    1259             : 
    1260             : static GEN
    1261           0 : FlxqX_extgcd_halfgcd(GEN x, GEN y, GEN T, ulong p, GEN *ptu, GEN *ptv)
    1262             : {
    1263           0 :   pari_sp av=avma;
    1264           0 :   GEN u,v,R = matid2_FlxXM(varn(x), get_Flx_var(T));
    1265           0 :   while (lg(y)>FlxqX_EXTGCD_LIMIT)
    1266             :   {
    1267             :     GEN M, c;
    1268           0 :     if (lgpol(y)<=(lgpol(x)>>1))
    1269             :     {
    1270           0 :       GEN r, q = FlxqX_divrem(x, y, T, p, &r);
    1271           0 :       x = y; y = r;
    1272           0 :       R = FlxqX_FlxqXM_qmul(q, R, T, p);
    1273             :     }
    1274           0 :     M = FlxqX_halfgcd(x,y, T, p);
    1275           0 :     c = FlxqXM_FlxqX_mul2(M, x,y, T, p);
    1276           0 :     R = FlxqXM_mul2(M, R, T, p);
    1277           0 :     x = gel(c,1); y = gel(c,2);
    1278           0 :     gerepileall(av,3,&x,&y,&R);
    1279             :   }
    1280           0 :   y = FlxqX_extgcd_basecase(x,y, T, p, &u,&v);
    1281           0 :   if (ptu) *ptu = FlxqX_addmulmul(u,v,gcoeff(R,1,1),gcoeff(R,2,1), T, p);
    1282           0 :   *ptv = FlxqX_addmulmul(u,v,gcoeff(R,1,2),gcoeff(R,2,2), T, p);
    1283           0 :   return y;
    1284             : }
    1285             : 
    1286             : /* x and y in Z[Y][X], return lift(gcd(x mod T,p, y mod T,p)). Set u and v st
    1287             :  * ux + vy = gcd (mod T,p) */
    1288             : GEN
    1289        8960 : FlxqX_extgcd(GEN x, GEN y, GEN T, ulong p, GEN *ptu, GEN *ptv)
    1290             : {
    1291             :   GEN d;
    1292        8960 :   pari_sp ltop=avma;
    1293        8960 :   x = FlxqX_red(x, T, p);
    1294        8960 :   y = FlxqX_red(y, T, p);
    1295        8960 :   if (lg(y)>FlxqX_EXTGCD_LIMIT)
    1296           0 :     d = FlxqX_extgcd_halfgcd(x, y, T, p, ptu, ptv);
    1297             :   else
    1298        8960 :     d = FlxqX_extgcd_basecase(x, y, T, p, ptu, ptv);
    1299        8960 :   gerepileall(ltop,ptu?3:2,&d,ptv,ptu);
    1300        8960 :   return d;
    1301             : }
    1302             : 
    1303             : GEN
    1304       18074 : FlxqX_safegcd(GEN P, GEN Q, GEN T, ulong p)
    1305             : {
    1306       18074 :   pari_sp av = avma;
    1307             :   GEN U;
    1308       18074 :   if (!signe(P)) return gcopy(Q);
    1309       18074 :   if (!signe(Q)) return gcopy(P);
    1310       18074 :   T = Flx_get_red(T,p);
    1311             :   for(;;)
    1312             :   {
    1313      111244 :     U = Flxq_invsafe(leading_coeff(Q), T, p);
    1314       64659 :     if (!U) return gc_NULL(av);
    1315       64659 :     Q = FlxqX_Flxq_mul_to_monic(Q,U,T,p);
    1316       64659 :     P = FlxqX_rem(P,Q,T,p);
    1317       64659 :     if (!signe(P)) break;
    1318       46585 :     if (gc_needed(av, 1))
    1319             :     {
    1320           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_safegcd");
    1321           0 :       gerepileall(av, 2, &P,&Q);
    1322             :     }
    1323       46585 :     swap(P, Q);
    1324             :   }
    1325       18074 :   U = Flxq_invsafe(leading_coeff(Q), T, p);
    1326       18074 :   if (!U) return gc_NULL(av);
    1327       18074 :   Q = FlxqX_Flxq_mul_to_monic(Q,U,T,p);
    1328       18074 :   return gerepileupto(av, Q);
    1329             : }
    1330             : 
    1331             : struct _FlxqX {ulong p; GEN T;};
    1332        1890 : static GEN _FlxqX_mul(void *data,GEN a,GEN b)
    1333             : {
    1334        1890 :   struct _FlxqX *d=(struct _FlxqX*)data;
    1335        1890 :   return FlxqX_mul(a,b,d->T,d->p);
    1336             : }
    1337       10255 : static GEN _FlxqX_sqr(void *data,GEN a)
    1338             : {
    1339       10255 :   struct _FlxqX *d=(struct _FlxqX*)data;
    1340       10255 :   return FlxqX_sqr(a,d->T,d->p);
    1341             : }
    1342             : 
    1343             : GEN
    1344       10227 : FlxqX_powu(GEN V, ulong n, GEN T, ulong p)
    1345             : {
    1346       10227 :   struct _FlxqX d; d.p=p; d.T=T;
    1347       10227 :   return gen_powu(V, n, (void*)&d, &_FlxqX_sqr, &_FlxqX_mul);
    1348             : }
    1349             : 
    1350             : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
    1351             : GEN
    1352          56 : FlxqX_resultant(GEN a, GEN b, GEN T, ulong p)
    1353             : {
    1354          56 :   long vT = get_Flx_var(T);
    1355             :   long da,db,dc;
    1356             :   pari_sp av;
    1357          56 :   GEN c,lb, res = pol1_Flx(vT);
    1358             : 
    1359          56 :   if (!signe(a) || !signe(b)) return pol0_Flx(vT);
    1360             : 
    1361          56 :   da = degpol(a);
    1362          56 :   db = degpol(b);
    1363          56 :   if (db > da)
    1364             :   {
    1365          21 :     swapspec(a,b, da,db);
    1366          21 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1367             :   }
    1368          56 :   if (!da) return pol1_Flx(vT); /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
    1369          56 :   av = avma;
    1370         203 :   while (db)
    1371             :   {
    1372          91 :     lb = gel(b,db+2);
    1373          91 :     c = FlxqX_rem(a,b, T,p);
    1374          91 :     a = b; b = c; dc = degpol(c);
    1375          91 :     if (dc < 0) { set_avma(av); return pol0_Flx(vT); }
    1376             : 
    1377          91 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1378          91 :     if (!equali1(lb)) res = Flxq_mul(res, Flxq_powu(lb, da - dc, T, p), T, p);
    1379          91 :     if (gc_needed(av,2))
    1380             :     {
    1381           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (da = %ld)",da);
    1382           0 :       gerepileall(av,3, &a,&b,&res);
    1383             :     }
    1384          91 :     da = db; /* = degpol(a) */
    1385          91 :     db = dc; /* = degpol(b) */
    1386             :   }
    1387          56 :   res = Flxq_mul(res, Flxq_powu(gel(b,2), da, T, p), T, p);
    1388          56 :   return gerepileupto(av, res);
    1389             : }
    1390             : 
    1391             : /* disc P = (-1)^(n(n-1)/2) lc(P)^(n - deg P' - 2) Res(P,P'), n = deg P */
    1392             : GEN
    1393          14 : FlxqX_disc(GEN P, GEN T, ulong p)
    1394             : {
    1395          14 :   pari_sp av = avma;
    1396          14 :   GEN L, dP = FlxX_deriv(P, p), D = FlxqX_resultant(P, dP, T, p);
    1397             :   long dd;
    1398          14 :   if (!lgpol(D)) return pol0_Flx(get_Flx_var(T));
    1399          14 :   dd = degpol(P) - 2 - degpol(dP); /* >= -1; > -1 iff p | deg(P) */
    1400          14 :   L = leading_coeff(P);
    1401          14 :   if (dd && !Flx_equal1(L))
    1402           0 :     D = (dd == -1)? Flxq_div(D,L,T,p): Flxq_mul(D, Flxq_powu(L, dd, T, p), T, p);
    1403          14 :   if (degpol(P) & 2) D = Flx_neg(D, p);
    1404          14 :   return gerepileupto(av, D);
    1405             : }
    1406             : 
    1407             : INLINE GEN
    1408        3910 : FlxXn_recip(GEN x, long n, long v)
    1409             : {
    1410        3910 :   return FlxX_recipspec(x+2, minss(lgpol(x), n), n, v);
    1411             : }
    1412             : 
    1413             : GEN
    1414        1564 : FlxqX_Newton(GEN P, long n, GEN T, ulong p)
    1415             : {
    1416        1564 :   pari_sp av = avma;
    1417        1564 :   long d = degpol(P), vT = get_Flx_var(T);
    1418        1564 :   GEN dP = FlxXn_recip(FlxX_deriv(P, p), d, vT);
    1419        1564 :   GEN Q = FlxqXn_mul(FlxqXn_inv(FlxXn_recip(P, d+1, vT), n, T, p), dP, n, T, p);
    1420        1564 :   return gerepilecopy(av, Q);
    1421             : }
    1422             : 
    1423             : GEN
    1424         782 : FlxqX_fromNewton(GEN P, GEN T, ulong p)
    1425             : {
    1426         782 :   pari_sp av = avma;
    1427         782 :   long vT = get_Flx_var(T);
    1428         782 :   long n = Flx_constant(constant_coeff(P))+1;
    1429         782 :   GEN z = FlxX_neg(FlxX_shift(P, -1, vT), p);
    1430         782 :   GEN Q = FlxXn_recip(FlxqXn_expint(z, n, T, p), n, vT);
    1431         782 :   return gerepilecopy(av, Q);
    1432             : }
    1433             : 
    1434             : static GEN
    1435         782 : FlxqX_composedsum(GEN P, GEN Q, GEN T, ulong p)
    1436             : {
    1437         782 :   long n = 1+ degpol(P)*degpol(Q);
    1438         782 :   GEN Pl = FlxX_invLaplace(FlxqX_Newton(P,n, T,p), p);
    1439         782 :   GEN Ql = FlxX_invLaplace(FlxqX_Newton(Q,n, T,p), p);
    1440         782 :   GEN L = FlxX_Laplace(FlxqXn_mul(Pl, Ql, n, T,p), p);
    1441         782 :   GEN R = FlxqX_fromNewton(L, T, p);
    1442         782 :   GEN lead = Flxq_mul(Flxq_powu(leading_coeff(P),degpol(Q), T, p),
    1443         782 :                       Flxq_powu(leading_coeff(Q),degpol(P), T, p), T, p);
    1444         782 :   return FlxqX_Flxq_mul(R, lead, T, p);
    1445             : }
    1446             : 
    1447             : GEN
    1448         782 : FlxqX_direct_compositum(GEN P, GEN Q, GEN T, ulong p)
    1449             : {
    1450         782 :   return FlxqX_composedsum(P, Q, T, p);
    1451             : }
    1452             : 
    1453             : GEN
    1454         902 : FlxqXV_prod(GEN V, GEN T, ulong p)
    1455             : {
    1456         902 :   struct _FlxqX d; d.p=p; d.T=T;
    1457         902 :   return gen_product(V, (void*)&d, &_FlxqX_mul);
    1458             : }
    1459             : 
    1460             : static GEN
    1461         890 : FlxqV_roots_to_deg1(GEN x, GEN T, ulong p, long v)
    1462             : {
    1463         890 :   long sv = get_Flx_var(T);
    1464         890 :   pari_APPLY_same(deg1pol_shallow(pol1_Flx(sv),Flx_neg(gel(x,i),p),v))
    1465             : }
    1466             : 
    1467             : GEN
    1468         890 : FlxqV_roots_to_pol(GEN V, GEN T, ulong p, long v)
    1469             : {
    1470         890 :   pari_sp ltop = avma;
    1471         890 :   GEN W = FlxqV_roots_to_deg1(V, T, p, v);
    1472         890 :   return gerepileupto(ltop, FlxqXV_prod(W, T, p));
    1473             : }
    1474             : 
    1475             : /*******************************************************************/
    1476             : /*                                                                 */
    1477             : /*                       (Fl[X]/T(X))[Y] / S(Y)                    */
    1478             : /*                                                                 */
    1479             : /*******************************************************************/
    1480             : 
    1481             : GEN
    1482      386437 : FlxqXQ_mul(GEN x, GEN y, GEN S, GEN T, ulong p) {
    1483      386437 :   return FlxqX_rem(FlxqX_mul(x,y,T,p),S,T,p);
    1484             : }
    1485             : 
    1486             : GEN
    1487      195252 : FlxqXQ_sqr(GEN x, GEN S, GEN T, ulong p) {
    1488      195252 :   return FlxqX_rem(FlxqX_sqr(x,T,p),S,T,p);
    1489             : }
    1490             : 
    1491             : GEN
    1492          14 : FlxqXQ_invsafe(GEN x, GEN S, GEN T, ulong p)
    1493             : {
    1494          14 :   GEN V, z = FlxqX_extgcd(get_FlxqX_mod(S), x, T, p, NULL, &V);
    1495          14 :   if (degpol(z)) return NULL;
    1496          14 :   z = Flxq_invsafe(gel(z,2),T,p);
    1497          14 :   if (!z) return NULL;
    1498          14 :   return FlxqX_Flxq_mul(V, z, T, p);
    1499             : }
    1500             : 
    1501             : GEN
    1502          14 : FlxqXQ_inv(GEN x, GEN S, GEN T,ulong p)
    1503             : {
    1504          14 :   pari_sp av = avma;
    1505          14 :   GEN U = FlxqXQ_invsafe(x, S, T, p);
    1506          14 :   if (!U) pari_err_INV("FlxqXQ_inv",x);
    1507          14 :   return gerepileupto(av, U);
    1508             : }
    1509             : 
    1510             : GEN
    1511           0 : FlxqXQ_div(GEN x, GEN y, GEN S, GEN T, ulong p) {
    1512           0 :   return FlxqXQ_mul(x, FlxqXQ_inv(y,S,T,p),S,T,p);
    1513             : }
    1514             : 
    1515             : struct _FlxqXQ {
    1516             :   GEN T, S;
    1517             :   ulong p;
    1518             : };
    1519             : static GEN
    1520           0 : _FlxqXQ_add(void *data, GEN x, GEN y) {
    1521           0 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1522           0 :   return FlxX_add(x,y, d->p);
    1523             : }
    1524             : static GEN
    1525        2121 : _FlxqXQ_sub(void *data, GEN x, GEN y) {
    1526        2121 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1527        2121 :   return FlxX_sub(x,y, d->p);
    1528             : }
    1529             : #if 0
    1530             : static GEN
    1531             : _FlxqXQ_cmul(void *data, GEN P, long a, GEN x) {
    1532             :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1533             :   return FlxX_Flx_mul(x,gel(P,a+2), d->p);
    1534             : }
    1535             : #endif
    1536             : static GEN
    1537        2440 : _FlxqXQ_red(void *data, GEN x) {
    1538        2440 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1539        2440 :   return FlxqX_red(x, d->T, d->p);
    1540             : }
    1541             : static GEN
    1542      136951 : _FlxqXQ_mul(void *data, GEN x, GEN y) {
    1543      136951 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1544      136951 :   return FlxqXQ_mul(x,y, d->S,d->T, d->p);
    1545             : }
    1546             : static GEN
    1547      194839 : _FlxqXQ_sqr(void *data, GEN x) {
    1548      194839 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1549      194839 :   return FlxqXQ_sqr(x, d->S,d->T, d->p);
    1550             : }
    1551             : 
    1552             : static GEN
    1553       95299 : _FlxqXQ_one(void *data) {
    1554       95299 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1555       95299 :   return pol1_FlxX(get_FlxqX_var(d->S),get_Flx_var(d->T));
    1556             : }
    1557             : 
    1558             : static GEN
    1559         191 : _FlxqXQ_zero(void *data) {
    1560         191 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1561         191 :   return pol_0(get_FlxqX_var(d->S));
    1562             : }
    1563             : 
    1564             : static struct bb_algebra FlxqXQ_algebra = { _FlxqXQ_red, _FlxqXQ_add,
    1565             :        _FlxqXQ_sub, _FlxqXQ_mul, _FlxqXQ_sqr, _FlxqXQ_one, _FlxqXQ_zero };
    1566             : 
    1567             : const struct bb_algebra *
    1568         219 : get_FlxqXQ_algebra(void **E, GEN S, GEN T, ulong p)
    1569             : {
    1570         219 :   GEN z = new_chunk(sizeof(struct _FlxqXQ));
    1571         219 :   struct _FlxqXQ *e = (struct _FlxqXQ *) z;
    1572         219 :   e->T = Flx_get_red(T, p);
    1573         219 :   e->S = FlxqX_get_red(S, e->T, p);
    1574         219 :   e->p  = p; *E = (void*)e;
    1575         219 :   return &FlxqXQ_algebra;
    1576             : }
    1577             : 
    1578             : /* x over Fq, return lift(x^n) mod S */
    1579             : GEN
    1580          56 : FlxqXQ_pow(GEN x, GEN n, GEN S, GEN T, ulong p)
    1581             : {
    1582          56 :   pari_sp av = avma;
    1583             :   struct _FlxqXQ D;
    1584          56 :   long s = signe(n);
    1585          56 :   if (!s) return pol1_FlxX(get_FlxqX_var(S),get_Flx_var(T));
    1586          56 :   if (s < 0) x = FlxqXQ_inv(x,S,T,p);
    1587          56 :   if (is_pm1(n)) return s < 0 ? x : gcopy(x);
    1588          56 :   if (degpol(x) >= get_FlxqX_degree(S)) x = FlxqX_rem(x,S,T,p);
    1589          56 :   T = Flx_get_red(T, p);
    1590          56 :   S = FlxqX_get_red(S, T, p);
    1591          56 :   D.S = S;
    1592          56 :   D.T = T;
    1593          56 :   D.p = p;
    1594          56 :   x = gen_pow_i(x, n, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul);
    1595          56 :   return gerepilecopy(av, x);
    1596             : }
    1597             : 
    1598             : /* x over Fq, return lift(x^n) mod S */
    1599             : GEN
    1600       73035 : FlxqXQ_powu(GEN x, ulong n, GEN S, GEN T, ulong p)
    1601             : {
    1602       73035 :   pari_sp av = avma;
    1603             :   struct _FlxqXQ D;
    1604       73035 :   switch(n)
    1605             :   {
    1606           0 :     case 0: return pol1_FlxX(get_FlxqX_var(S),get_Flx_var(T));
    1607        7490 :     case 1: return gcopy(x);
    1608         413 :     case 2: return FlxqXQ_sqr(x, S, T, p);
    1609             :   }
    1610       65132 :   T = Flx_get_red(T, p);
    1611       65132 :   S = FlxqX_get_red(S, T, p);
    1612       65132 :   D.S = S; D.T = T; D.p = p;
    1613       65132 :   x = gen_powu_i(x, n, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul);
    1614       65132 :   return gerepilecopy(av, x);
    1615             : }
    1616             : 
    1617             : GEN
    1618       93821 : FlxqXQ_powers(GEN x, long l, GEN S, GEN T, ulong p)
    1619             : {
    1620             :   struct _FlxqXQ D;
    1621       93821 :   int use_sqr = 2*degpol(x) >= get_FlxqX_degree(S);
    1622       93821 :   T = Flx_get_red(T, p);
    1623       93821 :   S = FlxqX_get_red(S, T, p);
    1624       93821 :   D.S = S; D.T = T; D.p = p;
    1625       93821 :   return gen_powers(x, l, use_sqr, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul,&_FlxqXQ_one);
    1626             : }
    1627             : 
    1628             : /* Let v a linear form, return the linear form z->v(tau*z)
    1629             :    that is, v*(M_tau) */
    1630             : 
    1631             : static GEN
    1632         430 : FlxqXQ_transmul_init(GEN tau, GEN S, GEN T, ulong p)
    1633             : {
    1634             :   GEN bht;
    1635         430 :   GEN h, Sp = get_FlxqX_red(S, &h);
    1636         430 :   long n = degpol(Sp), vS = varn(Sp), vT = get_Flx_var(T);
    1637         430 :   GEN ft = FlxX_recipspec(Sp+2, n+1, n+1, vT);
    1638         430 :   GEN bt = FlxX_recipspec(tau+2, lgpol(tau), n, vT);
    1639         430 :   setvarn(ft, vS); setvarn(bt, vS);
    1640         430 :   if (h)
    1641           0 :     bht = FlxqXn_mul(bt, h, n-1, T, p);
    1642             :   else
    1643             :   {
    1644         430 :     GEN bh = FlxqX_div(FlxX_shift(tau, n-1, vT), S, T, p);
    1645         430 :     bht = FlxX_recipspec(bh+2, lgpol(bh), n-1, vT);
    1646         430 :     setvarn(bht, vS);
    1647             :   }
    1648         430 :   return mkvec3(bt, bht, ft);
    1649             : }
    1650             : 
    1651             : static GEN
    1652         906 : FlxqXQ_transmul(GEN tau, GEN a, long n, GEN T, ulong p)
    1653             : {
    1654         906 :   pari_sp ltop = avma;
    1655             :   GEN t1, t2, t3, vec;
    1656         906 :   GEN bt = gel(tau, 1), bht = gel(tau, 2), ft = gel(tau, 3);
    1657         906 :   long vT = get_Flx_var(T);
    1658         906 :   if (signe(a)==0) return pol_0(varn(a));
    1659         906 :   t2 = FlxX_shift(FlxqX_mul(bt, a, T, p),1-n,vT);
    1660         906 :   if (signe(bht)==0) return gerepilecopy(ltop, t2);
    1661         607 :   t1 = FlxX_shift(FlxqX_mul(ft, a, T, p),-n,vT);
    1662         607 :   t3 = FlxqXn_mul(t1, bht, n-1, T, p);
    1663         607 :   vec = FlxX_sub(t2, FlxX_shift(t3, 1, vT), p);
    1664         607 :   return gerepileupto(ltop, vec);
    1665             : }
    1666             : 
    1667             : static GEN
    1668         215 : polxn_FlxX(long n, long v, long vT)
    1669             : {
    1670         215 :   long i, a = n+2;
    1671         215 :   GEN p = cgetg(a+1, t_POL);
    1672         215 :   p[1] = evalsigne(1)|evalvarn(v);
    1673         215 :   for (i = 2; i < a; i++) gel(p,i) = pol0_Flx(vT);
    1674         215 :   gel(p,a) = pol1_Flx(vT); return p;
    1675             : }
    1676             : 
    1677             : GEN
    1678         194 : FlxqXQ_minpoly(GEN x, GEN S, GEN T, ulong p)
    1679             : {
    1680         194 :   pari_sp ltop = avma;
    1681             :   long vS, vT, n;
    1682             :   GEN v_x, g, tau;
    1683         194 :   vS = get_FlxqX_var(S);
    1684         194 :   vT = get_Flx_var(T);
    1685         194 :   n = get_FlxqX_degree(S);
    1686         194 :   g = pol1_FlxX(vS,vT);
    1687         194 :   tau = pol1_FlxX(vS,vT);
    1688         194 :   S = FlxqX_get_red(S, T, p);
    1689         194 :   v_x = FlxqXQ_powers(x, usqrt(2*n), S, T, p);
    1690         603 :   while(signe(tau) != 0)
    1691             :   {
    1692             :     long i, j, m, k1;
    1693             :     GEN M, v, tr;
    1694             :     GEN g_prime, c;
    1695         215 :     if (degpol(g) == n) { tau = pol1_FlxX(vS, vT); g = pol1_FlxX(vS, vT); }
    1696         215 :     v = random_FlxqX(n, vS, T, p);
    1697         215 :     tr = FlxqXQ_transmul_init(tau, S, T, p);
    1698         215 :     v = FlxqXQ_transmul(tr, v, n, T, p);
    1699         215 :     m = 2*(n-degpol(g));
    1700         215 :     k1 = usqrt(m);
    1701         215 :     tr = FlxqXQ_transmul_init(gel(v_x,k1+1), S, T, p);
    1702         215 :     c = cgetg(m+2,t_POL);
    1703         215 :     c[1] = evalsigne(1)|evalvarn(vS);
    1704         906 :     for (i=0; i<m; i+=k1)
    1705             :     {
    1706         691 :       long mj = minss(m-i, k1);
    1707        2311 :       for (j=0; j<mj; j++)
    1708        1620 :         gel(c,m+1-(i+j)) = FlxqX_dotproduct(v, gel(v_x,j+1), T, p);
    1709         691 :       v = FlxqXQ_transmul(tr, v, n, T, p);
    1710             :     }
    1711         215 :     c = FlxX_renormalize(c, m+2);
    1712             :     /* now c contains <v,x^i> , i = 0..m-1  */
    1713         215 :     M = FlxqX_halfgcd(polxn_FlxX(m, vS, vT), c, T, p);
    1714         215 :     g_prime = gmael(M, 2, 2);
    1715         215 :     if (degpol(g_prime) < 1) continue;
    1716         208 :     g = FlxqX_mul(g, g_prime, T, p);
    1717         208 :     tau = FlxqXQ_mul(tau, FlxqX_FlxqXQV_eval(g_prime, v_x, S, T, p), S, T, p);
    1718             :   }
    1719         194 :   g = FlxqX_normalize(g,T, p);
    1720         194 :   return gerepilecopy(ltop,g);
    1721             : }
    1722             : 
    1723             : GEN
    1724           0 : FlxqXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, ulong p)
    1725             : {
    1726           0 :   return FlxXV_to_FlxM(FlxqXQ_powers(y,m-1,S,T,p), n, get_Flx_var(T));
    1727             : }
    1728             : 
    1729             : static GEN
    1730      121967 : FlxX_blocks_FlxM(GEN P, long n, long m, long v)
    1731             : {
    1732      121967 :   GEN z = cgetg(m+1,t_MAT);
    1733      121967 :   long i,j, k=2, l = lg(P);
    1734      463531 :   for(i=1; i<=m; i++)
    1735             :   {
    1736      341564 :     GEN zi = cgetg(n+1,t_COL);
    1737      341564 :     gel(z,i) = zi;
    1738     1053474 :     for(j=1; j<=n; j++)
    1739      711910 :       gel(zi, j) = k==l ? pol0_Flx(v) : gel(P,k++);
    1740             :   }
    1741      121967 :   return z;
    1742             : }
    1743             : 
    1744             : GEN
    1745      121967 : FlxqX_FlxqXQV_eval(GEN Q, GEN x, GEN S, GEN T, ulong p)
    1746             : {
    1747      121967 :   pari_sp btop, av = avma;
    1748      121967 :   long v = get_FlxqX_var(S), m = get_FlxqX_degree(S);
    1749      121967 :   long vT = get_Flx_var(T);
    1750      121967 :   long i, l = lg(x)-1, lQ = lgpol(Q), n,  d;
    1751             :   GEN A, B, C, R, g;
    1752      121967 :   if (lQ == 0) return pol_0(v);
    1753      121967 :   if (lQ <= l)
    1754             :   {
    1755       40941 :     n = l;
    1756       40941 :     d = 1;
    1757             :   }
    1758             :   else
    1759             :   {
    1760       81026 :     n = l-1;
    1761       81026 :     d = (lQ+n-1)/n;
    1762             :   }
    1763      121967 :   A = FlxXV_to_FlxM_lg(x, m, n, vT);
    1764      121967 :   B = FlxX_blocks_FlxM(Q, n, d, vT);
    1765      121967 :   C = gerepileupto(av, FlxqM_mul(A, B, T, p));
    1766      121967 :   g = gel(x, l);
    1767      121967 :   T = Flx_get_red(T, p);
    1768      121967 :   S = FlxqX_get_red(S, T, p);
    1769      121967 :   btop = avma;
    1770      121967 :   R = FlxV_to_FlxX(gel(C, d), v);
    1771      341564 :   for (i = d-1; i>0; i--)
    1772             :   {
    1773      219597 :     R = FlxX_add(FlxqXQ_mul(R, g, S, T, p), FlxV_to_FlxX(gel(C,i), v), p);
    1774      219597 :     if (gc_needed(btop,1))
    1775           7 :       R = gerepileupto(btop, R);
    1776             :   }
    1777      121967 :   return gerepilecopy(av, R);
    1778             : }
    1779             : 
    1780             : GEN
    1781       64782 : FlxqX_FlxqXQ_eval(GEN Q, GEN x, GEN S, GEN T, ulong p)
    1782             : {
    1783       64782 :   pari_sp av = avma;
    1784             :   GEN z, V;
    1785       64782 :   long d = degpol(Q), rtd;
    1786       64782 :   if (d < 0) return pol_0(get_FlxqX_var(S));
    1787       64782 :   rtd = (long) sqrt((double)d);
    1788       64782 :   T = Flx_get_red(T, p);
    1789       64782 :   S = FlxqX_get_red(S, T, p);
    1790       64782 :   V = FlxqXQ_powers(x, rtd, S, T, p);
    1791       64782 :   z = FlxqX_FlxqXQV_eval(Q, V, S, T, p);
    1792       64782 :   return gerepileupto(av, z);
    1793             : }
    1794             : 
    1795             : static GEN
    1796       63399 : FlxqXQ_autpow_sqr(void * E, GEN x)
    1797             : {
    1798       63399 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    1799       63399 :   GEN S = D->S, T = D->T;
    1800       63399 :   ulong p = D->p;
    1801       63399 :   GEN phi = gel(x,1), S1 = gel(x,2);
    1802       63399 :   long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(S1)+1,1);
    1803       63399 :   GEN V = Flxq_powers(phi, n, T, p);
    1804       63399 :   GEN phi2 = Flx_FlxqV_eval(phi, V, T, p);
    1805       63399 :   GEN Sphi = FlxY_FlxqV_evalx(S1, V, T, p);
    1806       63399 :   GEN S2 = FlxqX_FlxqXQ_eval(Sphi, S1, S, T, p);
    1807       63399 :   return mkvec2(phi2, S2);
    1808             : }
    1809             : 
    1810             : static GEN
    1811        1161 : FlxqXQ_autpow_mul(void * E, GEN x, GEN y)
    1812             : {
    1813        1161 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    1814        1161 :   GEN S = D->S, T = D->T;
    1815        1161 :   ulong p = D->p;
    1816        1161 :   GEN phi1 = gel(x,1), S1 = gel(x,2);
    1817        1161 :   GEN phi2 = gel(y,1), S2 = gel(y,2);
    1818        1161 :   long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(S1)+1,1);
    1819        1161 :   GEN V = Flxq_powers(phi2, n, T, p);
    1820        1161 :   GEN phi3 = Flx_FlxqV_eval(phi1, V, T, p);
    1821        1161 :   GEN Sphi = FlxY_FlxqV_evalx(S1, V, T, p);
    1822        1161 :   GEN S3 = FlxqX_FlxqXQ_eval(Sphi, S2, S, T, p);
    1823        1161 :   return mkvec2(phi3, S3);
    1824             : }
    1825             : 
    1826             : GEN
    1827       61845 : FlxqXQ_autpow(GEN aut, long n, GEN S, GEN T, ulong p)
    1828             : {
    1829       61845 :   pari_sp av = avma;
    1830             :   struct _FlxqXQ D;
    1831       61845 :   T = Flx_get_red(T, p);
    1832       61845 :   S = FlxqX_get_red(S, T, p);
    1833       61845 :   D.S=S; D.T=T; D.p=p;
    1834       61845 :   aut = gen_powu_i(aut,n,&D,FlxqXQ_autpow_sqr,FlxqXQ_autpow_mul);
    1835       61845 :   return gerepilecopy(av, aut);
    1836             : }
    1837             : 
    1838             : static GEN
    1839       28191 : FlxqXQ_autsum_mul(void *E, GEN x, GEN y)
    1840             : {
    1841       28191 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    1842       28191 :   GEN S = D->S, T = D->T;
    1843       28191 :   ulong p = D->p;
    1844       28191 :   GEN phi1 = gel(x,1), S1 = gel(x,2), a1 = gel(x,3);
    1845       28191 :   GEN phi2 = gel(y,1), S2 = gel(y,2), a2 = gel(y,3);
    1846       28191 :   long n2 = brent_kung_optpow(get_Flx_degree(T)-1, lgpol(S1)+lgpol(a1)+1,1);
    1847       28191 :   GEN V2 = Flxq_powers(phi2, n2, T, p);
    1848       28191 :   GEN phi3 = Flx_FlxqV_eval(phi1, V2, T, p);
    1849       28191 :   GEN Sphi = FlxY_FlxqV_evalx(S1, V2, T, p);
    1850       28191 :   GEN aphi = FlxY_FlxqV_evalx(a1, V2, T, p);
    1851       28191 :   long n = brent_kung_optpow(maxss(degpol(Sphi),degpol(aphi)),2,1);
    1852       28191 :   GEN V = FlxqXQ_powers(S2, n, S, T, p);
    1853       28191 :   GEN S3 = FlxqX_FlxqXQV_eval(Sphi, V, S, T, p);
    1854       28191 :   GEN aS = FlxqX_FlxqXQV_eval(aphi, V, S, T, p);
    1855       28191 :   GEN a3 = FlxqXQ_mul(aS, a2, S, T, p);
    1856       28191 :   return mkvec3(phi3, S3, a3);
    1857             : }
    1858             : 
    1859             : static GEN
    1860       16877 : FlxqXQ_autsum_sqr(void * T, GEN x)
    1861       16877 : { return FlxqXQ_autsum_mul(T, x, x); }
    1862             : 
    1863             : GEN
    1864       10738 : FlxqXQ_autsum(GEN aut, long n, GEN S, GEN T, ulong p)
    1865             : {
    1866       10738 :   pari_sp av = avma;
    1867             :   struct _FlxqXQ D;
    1868       10738 :   T = Flx_get_red(T, p);
    1869       10738 :   S = FlxqX_get_red(S, T, p);
    1870       10738 :   D.S=S; D.T=T; D.p=p;
    1871       10738 :   aut = gen_powu_i(aut,n,&D,FlxqXQ_autsum_sqr,FlxqXQ_autsum_mul);
    1872       10738 :   return gerepilecopy(av, aut);
    1873             : }
    1874             : 
    1875             : static GEN
    1876          27 : FlxqXQ_auttrace_mul(void *E, GEN x, GEN y)
    1877             : {
    1878          27 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    1879          27 :   GEN S = D->S, T = D->T;
    1880          27 :   ulong p = D->p;
    1881          27 :   GEN S1 = gel(x,1), a1 = gel(x,2);
    1882          27 :   GEN S2 = gel(y,1), a2 = gel(y,2);
    1883          27 :   long n = brent_kung_optpow(maxss(degpol(S1),degpol(a1)),2,1);
    1884          27 :   GEN V = FlxqXQ_powers(S2, n, S, T, p);
    1885          27 :   GEN S3 = FlxqX_FlxqXQV_eval(S1, V, S, T, p);
    1886          27 :   GEN aS = FlxqX_FlxqXQV_eval(a1, V, S, T, p);
    1887          27 :   GEN a3 = FlxX_add(aS, a2, p);
    1888          27 :   return mkvec2(S3, a3);
    1889             : }
    1890             : 
    1891             : static GEN
    1892          20 : FlxqXQ_auttrace_sqr(void *E, GEN x)
    1893          20 : { return FlxqXQ_auttrace_mul(E, x, x); }
    1894             : 
    1895             : GEN
    1896         314 : FlxqXQ_auttrace(GEN x, ulong n, GEN S, GEN T, ulong p)
    1897             : {
    1898         314 :   pari_sp av = avma;
    1899             :   struct _FlxqXQ D;
    1900         314 :   T = Flx_get_red(T, p);
    1901         314 :   S = FlxqX_get_red(S, T, p);
    1902         314 :   D.S=S; D.T=T; D.p=p;
    1903         314 :   x = gen_powu_i(x,n,(void*)&D,FlxqXQ_auttrace_sqr,FlxqXQ_auttrace_mul);
    1904         314 :   return gerepilecopy(av, x);
    1905             : }
    1906             : 
    1907             : /*******************************************************************/
    1908             : /*                                                                 */
    1909             : /*                      FlxYqQ                                     */
    1910             : /*                                                                 */
    1911             : /*******************************************************************/
    1912             : 
    1913             : /*Preliminary implementation to speed up FpX_ffisom*/
    1914             : typedef struct {
    1915             :   GEN S, T;
    1916             :   ulong p;
    1917             : } FlxYqq_muldata;
    1918             : 
    1919             : /* reduce x in Fl[X, Y] in the algebra Fl[X, Y]/ (P(X),Q(Y)) */
    1920             : static GEN
    1921       11260 : FlxYqq_redswap(GEN x, GEN S, GEN T, ulong p)
    1922             : {
    1923       11260 :   pari_sp ltop=avma;
    1924       11260 :   long n = get_Flx_degree(S);
    1925       11260 :   long m = get_Flx_degree(T);
    1926       11260 :   long w = get_Flx_var(T);
    1927       11260 :   GEN V = FlxX_swap(x,m,w);
    1928       11260 :   V = FlxqX_red(V,S,p);
    1929       11260 :   V = FlxX_swap(V,n,w);
    1930       11260 :   return gerepilecopy(ltop,V);
    1931             : }
    1932             : static GEN
    1933        7954 : FlxYqq_sqr(void *data, GEN x)
    1934             : {
    1935        7954 :   FlxYqq_muldata *D = (FlxYqq_muldata*)data;
    1936        7954 :   return FlxYqq_redswap(FlxqX_sqr(x, D->T, D->p),D->S,D->T,D->p);
    1937             : }
    1938             : 
    1939             : static GEN
    1940        3306 : FlxYqq_mul(void *data, GEN x, GEN y)
    1941             : {
    1942        3306 :   FlxYqq_muldata *D = (FlxYqq_muldata*)data;
    1943        3306 :   return FlxYqq_redswap(FlxqX_mul(x,y, D->T, D->p),D->S,D->T,D->p);
    1944             : }
    1945             : 
    1946             : /* x in Z[X,Y], S in Z[X] over Fq = Z[Y]/(p,T); compute lift(x^n mod (S,T,p)) */
    1947             : GEN
    1948        4202 : FlxYqq_pow(GEN x, GEN n, GEN S, GEN T, ulong p)
    1949             : {
    1950             :   FlxYqq_muldata D;
    1951        4202 :   D.S = S;
    1952        4202 :   D.T = T;
    1953        4202 :   D.p = p;
    1954        4202 :   return gen_pow(x, n, (void*)&D, &FlxYqq_sqr, &FlxYqq_mul);
    1955             : }
    1956             : 
    1957             : /*******************************************************************/
    1958             : /*                                                                 */
    1959             : /*                      FlxqXn                                     */
    1960             : /*                                                                 */
    1961             : /*******************************************************************/
    1962             : 
    1963             : GEN
    1964       40769 : FlxXn_red(GEN a, long n)
    1965             : {
    1966       40769 :   long i, L = n+2, l = lg(a);
    1967             :   GEN  b;
    1968       40769 :   if (L >= l) return a; /* deg(x) < n */
    1969       20927 :   b = cgetg(L, t_POL); b[1] = a[1];
    1970       20929 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
    1971       20929 :   return FlxX_renormalize(b,L);
    1972             : }
    1973             : 
    1974             : GEN
    1975       27641 : FlxqXn_mul(GEN a, GEN b, long n, GEN T, ulong p)
    1976       27641 : { return FlxXn_red(FlxqX_mul(a, b, T, p), n); }
    1977             : 
    1978             : GEN
    1979           0 : FlxqXn_sqr(GEN a, long n, GEN T, ulong p)
    1980           0 : { return FlxXn_red(FlxqX_sqr(a, T, p), n); }
    1981             : 
    1982             : /* (f*g) \/ x^n */
    1983             : static GEN
    1984       12345 : FlxqX_mulhigh_i(GEN f, GEN g, long n, GEN T, ulong p)
    1985             : {
    1986       12345 :   return FlxX_shift(FlxqX_mul(f, g, T, p), -n , get_Flx_var(T));
    1987             : }
    1988             : 
    1989             : static GEN
    1990        9065 : FlxqXn_mulhigh(GEN f, GEN g, long n2, long n, GEN T, ulong p)
    1991             : {
    1992        9065 :   long vT = get_Flx_var(T);
    1993        9065 :   GEN F = FlxX_blocks(f, n2, 2, vT), fl = gel(F,1), fh = gel(F,2);
    1994        9066 :   return FlxX_add(FlxqX_mulhigh_i(fl, g, n2, T, p),
    1995             :                   FlxqXn_mul(fh, g, n - n2, T, p), p);
    1996             : }
    1997             : 
    1998             : GEN
    1999        1564 : FlxqXn_inv(GEN f, long e, GEN T, ulong p)
    2000             : {
    2001        1564 :   pari_sp av = avma, av2;
    2002             :   ulong mask;
    2003             :   GEN W, a;
    2004        1564 :   long v = varn(f), n = 1, vT = get_Flx_var(T);
    2005             : 
    2006        1564 :   if (!signe(f)) pari_err_INV("FlxqXn_inv",f);
    2007        1564 :   a = Flxq_inv(gel(f,2), T, p);
    2008        1564 :   if (e == 1) return scalarpol(a, v);
    2009        1564 :   else if (e == 2)
    2010             :   {
    2011             :     GEN b;
    2012           0 :     if (degpol(f) <= 0) return scalarpol(a, v);
    2013           0 :     b = Flx_neg(gel(f,3), p);
    2014           0 :     if (lgpol(b)==0) return scalarpol(a, v);
    2015           0 :     b = Flxq_mul(b, Flxq_sqr(a, T, p), T, p);
    2016           0 :     W = deg1pol_shallow(b, a, v);
    2017           0 :     return gerepilecopy(av, W);
    2018             :   }
    2019        1564 :   W = scalarpol_shallow(Flxq_inv(gel(f,2), T, p),v);
    2020        1564 :   mask = quadratic_prec_mask(e);
    2021        1564 :   av2 = avma;
    2022        9693 :   for (;mask>1;)
    2023             :   {
    2024             :     GEN u, fr;
    2025        6565 :     long n2 = n;
    2026        6565 :     n<<=1; if (mask & 1) n--;
    2027        6565 :     mask >>= 1;
    2028        6565 :     fr = FlxXn_red(f, n);
    2029        6566 :     u = FlxqXn_mul(W, FlxqXn_mulhigh(fr, W, n2, n, T, p), n-n2, T, p);
    2030        6567 :     W = FlxX_sub(W, FlxX_shift(u, n2, vT), p);
    2031        6564 :     if (gc_needed(av2,2))
    2032             :     {
    2033           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxqXn_inv, e = %ld", n);
    2034           0 :       W = gerepileupto(av2, W);
    2035             :     }
    2036             :   }
    2037        1564 :   return gerepileupto(av, W);
    2038             : }
    2039             : 
    2040             : /* Compute intformal(x^n*S)/x^(n+1) */
    2041             : static GEN
    2042        3283 : FlxX_integXn(GEN x, long n, ulong p)
    2043             : {
    2044        3283 :   long i, lx = lg(x);
    2045             :   GEN y;
    2046        3283 :   if (lx == 2) return gcopy(x);
    2047        2726 :   y = cgetg(lx, t_POL); y[1] = x[1];
    2048       22020 :   for (i=2; i<lx; i++)
    2049             :   {
    2050       19296 :     GEN xi = gel(x,i);
    2051       19296 :     gel(y,i) = Flx_Fl_mul(xi, Fl_inv((n+i-1)%p, p), p);
    2052             :   }
    2053        2724 :   return FlxX_renormalize(y, lx);;
    2054             : }
    2055             : 
    2056             : GEN
    2057         781 : FlxqXn_expint(GEN h, long e, GEN T, ulong p)
    2058             : {
    2059         781 :   pari_sp av = avma, av2;
    2060         781 :   long v = varn(h), n = 1, vT = get_Flx_var(T);
    2061         781 :   GEN f = pol1_FlxX(v, vT), g = pol1_FlxX(v, vT);
    2062         782 :   ulong mask = quadratic_prec_mask(e);
    2063         782 :   av2 = avma;
    2064        4062 :   for (;mask>1;)
    2065             :   {
    2066             :     GEN u, w;
    2067        3280 :     long n2 = n;
    2068        3280 :     n<<=1; if (mask & 1) n--;
    2069        3280 :     mask >>= 1;
    2070        3280 :     u = FlxqXn_mul(g, FlxqX_mulhigh_i(f, FlxXn_red(h, n2-1), n2-1, T, p), n-n2, T, p);
    2071        3283 :     u = FlxX_add(u, FlxX_shift(FlxXn_red(h, n-1), 1-n2, vT), p);
    2072        3283 :     w = FlxqXn_mul(f, FlxX_integXn(u, n2-1, p), n-n2, T, p);
    2073        3283 :     f = FlxX_add(f, FlxX_shift(w, n2, vT), p);
    2074        3282 :     if (mask<=1) break;
    2075        2500 :     u = FlxqXn_mul(g, FlxqXn_mulhigh(f, g, n2, n, T, p), n-n2, T, p);
    2076        2501 :     g = FlxX_sub(g, FlxX_shift(u, n2, vT), p);
    2077        2499 :     if (gc_needed(av2,2))
    2078             :     {
    2079           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqXn_exp, e = %ld", n);
    2080           0 :       gerepileall(av2, 2, &f, &g);
    2081             :     }
    2082             :   }
    2083         782 :   return gerepileupto(av, f);
    2084             : }

Generated by: LCOV version 1.13