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 25406-bf255ab81b) Lines: 1101 1230 89.5 %
Date: 2020-06-04 05:59:24 Functions: 128 142 90.1 %
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     8465361 : FlxX_renormalize(GEN /*in place*/ x, long lx)
      30             : {
      31             :   long i;
      32    12548782 :   for (i = lx-1; i>1; i--)
      33    11826582 :     if (lgpol(gel(x,i))) break;
      34     8465345 :   stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
      35     8465382 :   setlg(x, i+1); setsigne(x, i!=1); return x;
      36             : }
      37             : 
      38             : GEN
      39      613142 : pol1_FlxX(long v, long sv)
      40             : {
      41      613142 :   GEN z = cgetg(3, t_POL);
      42      613142 :   z[1] = evalsigne(1) | evalvarn(v);
      43      613142 :   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     1839149 : FlxY_degreex(GEN b)
      57             : {
      58     1839149 :   long deg = 0, i;
      59     1839149 :   if (!signe(b)) return -1;
      60     6762070 :   for (i = 2; i < lg(b); ++i)
      61     4922921 :     deg = maxss(deg, degpol(gel(b, i)));
      62     1839149 :   return deg;
      63             : }
      64             : 
      65             : /*Lift coefficient of B to constant Flx, to give a FlxY*/
      66             : GEN
      67        1640 : Fly_to_FlxY(GEN B, long sv)
      68             : {
      69        1640 :   long lb=lg(B);
      70             :   long i;
      71        1640 :   GEN b=cgetg(lb,t_POL);
      72        1641 :   b[1]=evalsigne(1)|(((ulong)B[1])&VARNBITS);
      73       41647 :   for (i=2; i<lb; i++)
      74       40051 :     gel(b,i) = Fl_to_Flx(B[i], sv);
      75        1596 :   return FlxX_renormalize(b, lb);
      76             : }
      77             : 
      78             : GEN
      79     1656010 : zxX_to_FlxX(GEN B, ulong p)
      80             : {
      81     1656010 :   long i, lb = lg(B);
      82     1656010 :   GEN b = cgetg(lb,t_POL);
      83     5397350 :   for (i=2; i<lb; i++)
      84     3741340 :     gel(b,i) = zx_to_Flx(gel(B,i), p);
      85     1656010 :   b[1] = B[1]; return FlxX_renormalize(b, lb);
      86             : }
      87             : 
      88             : GEN
      89      477429 : FlxX_to_ZXX(GEN B)
      90             : {
      91      477429 :   long i, lb = lg(B);
      92      477429 :   GEN b = cgetg(lb,t_POL);
      93     2523466 :   for (i=2; i<lb; i++)
      94             :   {
      95     2046037 :     GEN c = gel(B,i);
      96     2046037 :     switch(lgpol(c))
      97             :     {
      98       45578 :       case 0:  c = gen_0; break;
      99       81363 :       case 1:  c = utoi(c[2]); break;
     100     1919096 :       default: c = Flx_to_ZX(c); break;
     101             :     }
     102     2046037 :     gel(b,i) = c;
     103             :   }
     104      477429 :   b[1] = B[1]; return b;
     105             : }
     106             : 
     107             : GEN
     108        2681 : FlxXC_to_ZXXC(GEN x)
     109       13650 : { 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      539325 : ZXX_to_FlxX(GEN B, ulong p, long v)
     119             : {
     120      539325 :   long lb=lg(B);
     121             :   long i;
     122      539325 :   GEN b=cgetg(lb,t_POL);
     123      539325 :   b[1]=evalsigne(1)|(((ulong)B[1])&VARNBITS);
     124     4317971 :   for (i=2; i<lb; i++)
     125     3778647 :     switch (typ(gel(B,i)))
     126             :     {
     127      547452 :     case t_INT:
     128      547452 :       gel(b,i) = Z_to_Flx(gel(B,i), p, evalvarn(v));
     129      547451 :       break;
     130     3231195 :     case t_POL:
     131     3231195 :       gel(b,i) = ZX_to_Flx(gel(B,i), p);
     132     3231195 :       break;
     133             :     }
     134     4317970 :   return FlxX_renormalize(b, lb);
     135             : }
     136             : 
     137             : GEN
     138          12 : ZXXV_to_FlxXV(GEN x, ulong p, long v)
     139          60 : { 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          42 :     pari_APPLY_type(t_VEC, ZXXT_to_FlxXT(gel(x,i), p, v))
     148             : }
     149             : 
     150             : GEN
     151      277753 : FlxX_to_FlxC(GEN x, long N, long sv)
     152             : {
     153             :   long i, l;
     154             :   GEN z;
     155      277753 :   l = lg(x)-1; x++;
     156      277753 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     157      277753 :   z = cgetg(N+1,t_COL);
     158     1914552 :   for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
     159     3285653 :   for (   ; i<=N; i++) gel(z,i) = pol0_Flx(sv);
     160      277753 :   return z;
     161             : }
     162             : 
     163             : static GEN
     164      121877 : FlxXV_to_FlxM_lg(GEN x, long m, long n, long sv)
     165             : {
     166             :   long i;
     167      121877 :   GEN y = cgetg(n+1, t_MAT);
     168      399630 :   for (i=1; i<=n; i++) gel(y,i) = FlxX_to_FlxC(gel(x,i), m, sv);
     169      121877 :   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       18185 : FlxX_to_Flm(GEN v, long n)
     180             : {
     181       18185 :   long j, N = lg(v)-1;
     182       18185 :   GEN y = cgetg(N, t_MAT);
     183       18187 :   v++;
     184       74799 :   for (j=1; j<N; j++) gel(y,j) = Flx_to_Flv(gel(v,j), n);
     185       18185 :   return y;
     186             : }
     187             : 
     188             : GEN
     189       53438 : FlxX_to_Flx(GEN f)
     190             : {
     191       53438 :   long i, l = lg(f);
     192       53438 :   GEN V = cgetg(l, t_VECSMALL);
     193       53438 :   V[1] = ((ulong)f[1])&VARNBITS;
     194      772107 :   for(i=2; i<l; i++)
     195      718669 :     V[i] = lgpol(gel(f,i)) ? mael(f,i,2): 0L;
     196       53438 :   return V;
     197             : }
     198             : 
     199             : GEN
     200       35137 : Flm_to_FlxX(GEN x, long v,long w)
     201             : {
     202       35137 :   long j, lx = lg(x);
     203       35137 :   GEN y = cgetg(lx+1, t_POL);
     204       35137 :   y[1]=evalsigne(1) | v;
     205       35137 :   y++;
     206      149244 :   for (j=1; j<lx; j++) gel(y,j) = Flv_to_Flx(gel(x,j), w);
     207       35127 :   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     7143179 :     for (k=2; k<lx; k++)
     223     6918577 :       if (j<lg(gel(x,k)))
     224     5661176 :         p1[k] = mael(x,k,j);
     225             :       else
     226     1257401 :         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     1674764 : zxX_to_Kronecker_spec(GEN P, long lp, long n)
     234             : { /* P(X) = sum Pi(Y) * X^i, return P( Y^(2n-1) ) */
     235     1674764 :   long i, j, k, l, N = (n<<1) + 1;
     236     1674764 :   GEN y = cgetg((N-2)*lp + 2, t_VECSMALL) + 2;
     237    16478989 :   for (k=i=0; i<lp; i++)
     238             :   {
     239    16464978 :     GEN c = gel(P,i);
     240    16464978 :     l = lg(c);
     241    16464978 :     if (l-3 >= n)
     242           0 :       pari_err_BUG("zxX_to_Kronecker, P is not reduced mod Q");
     243    96142114 :     for (j=2; j < l; j++) y[k++] = c[j];
     244    16464988 :     if (i == lp-1) break;
     245   127748053 :     for (   ; j < N; j++) y[k++] = 0;
     246             :   }
     247     1674777 :   y -= 2;
     248     1674777 :   y[1] = P[1]; setlg(y, k+2); return y;
     249             : }
     250             : 
     251             : GEN
     252     1329527 : zxX_to_Kronecker(GEN P, GEN Q)
     253             : {
     254     1329527 :   GEN z = zxX_to_Kronecker_spec(P+2, lg(P)-2, degpol(Q));
     255     1329527 :   z[1] = P[1]; return z;
     256             : }
     257             : 
     258             : GEN
     259      240545 : FlxX_add(GEN x, GEN y, ulong p)
     260             : {
     261             :   long i,lz;
     262             :   GEN z;
     263      240545 :   long lx=lg(x);
     264      240545 :   long ly=lg(y);
     265      240545 :   if (ly>lx) swapspec(x,y, lx,ly);
     266      240545 :   lz = lx; z = cgetg(lz, t_POL); z[1]=x[1];
     267     1733479 :   for (i=2; i<ly; i++) gel(z,i) = Flx_add(gel(x,i), gel(y,i), p);
     268     1735036 :   for (   ; i<lx; i++) gel(z,i) = Flx_copy(gel(x,i));
     269      240539 :   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        1211 :     for(i=3;i<lz;i++) gel(z,i) = Flx_copy(gel(y,i));
     283         364 :   return z;
     284             : }
     285             : 
     286             : GEN
     287       10472 : FlxX_Flx_sub(GEN y, GEN x, ulong p)
     288             : {
     289       10472 :   long i, lz = lg(y);
     290             :   GEN z;
     291       10472 :   if (signe(y) == 0) return scalarpol(x, varn(y));
     292       10472 :   z = cgetg(lz,t_POL); z[1] = y[1];
     293       10472 :   gel(z,2) = Flx_sub(gel(y,2), x, p);
     294       10472 :   if (lz == 3) z = FlxX_renormalize(z,lz);
     295             :   else
     296       80770 :     for(i=3;i<lz;i++) gel(z,i) = Flx_copy(gel(y,i));
     297       10472 :   return z;
     298             : }
     299             : 
     300             : GEN
     301        1809 : FlxX_neg(GEN x, ulong p)
     302             : {
     303        1809 :   long i, lx=lg(x);
     304        1809 :   GEN z = cgetg(lx, t_POL);
     305        1809 :   z[1]=x[1];
     306       37388 :   for (i=2; i<lx; i++) gel(z,i) = Flx_neg(gel(x,i), p);
     307        1809 :   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        1047 :   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        1752 :   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       73080 : FlxX_deriv(GEN z, ulong p)
     342             : {
     343       73080 :   long i,l = lg(z)-1;
     344             :   GEN x;
     345       73080 :   if (l < 2) l = 2;
     346       73080 :   x = cgetg(l, t_POL); x[1] = z[1];
     347      701990 :   for (i=2; i<l; i++) gel(x,i) = Flx_mulu(gel(z,i+1), (ulong) i-1, p);
     348       73080 :   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     2286855 :     for (i=0; i<ly; i++) gel(z,i+2) = Flx_sub(gel(x,i),gel(y,i),p);
     387       67562 :     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      130665 : FlxX_sub(GEN x, GEN y, ulong p)
     400             : {
     401             :   long lx,ly,i,lz;
     402             :   GEN z;
     403      130665 :   lx = lg(x); ly = lg(y);
     404      130665 :   lz=maxss(lx,ly);
     405      130665 :   z = cgetg(lz,t_POL);
     406      130664 :   if (lx >= ly)
     407             :   {
     408       70967 :     z[1] = x[1];
     409      323203 :     for (i=2; i<ly; i++) gel(z,i) = Flx_sub(gel(x,i),gel(y,i),p);
     410      270583 :     for (   ; i<lx; i++) gel(z,i) = Flx_copy(gel(x,i));
     411       70967 :     if (lx==ly) z = FlxX_renormalize(z, lz);
     412             :   }
     413             :   else
     414             :   {
     415       59697 :     z[1] = y[1];
     416      464181 :     for (i=2; i<lx; i++) gel(z,i) = Flx_sub(gel(x,i),gel(y,i),p);
     417      216010 :     for (   ; i<ly; i++) gel(z,i) = Flx_neg(gel(y,i),p);
     418             :   }
     419      130660 :   if (!lgpol(z)) { set_avma((pari_sp)(z + lz)); z = pol_0(varn(x)); }
     420      130665 :   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      278892 : FlxY_evalx(GEN Q, ulong x, ulong p)
     436             : {
     437             :   GEN z;
     438      278892 :   long i, lb = lg(Q);
     439      278892 :   z = cgetg(lb,t_VECSMALL); z[1] = evalvarn(varn(Q));
     440     2832474 :   for (i=2; i<lb; i++) z[i] = Flx_eval(gel(Q,i), x, p);
     441      279044 :   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     7983973 : FlxY_evalx_powers_pre(GEN pol, GEN ypowers, ulong p, ulong pi)
     469             : {
     470     7983973 :   long i, len = lg(pol);
     471     7983973 :   GEN res = cgetg(len, t_VECSMALL);
     472     7983973 :   res[1] = pol[1] & VARNBITS;
     473    27074932 :   for (i = 2; i < len; ++i)
     474    19090959 :     res[i] = Flx_eval_powers_pre(gel(pol, i), ypowers, p, pi);
     475     7983973 :   return Flx_renormalize(res, len);
     476             : }
     477             : 
     478             : ulong
     479     5352766 : FlxY_eval_powers_pre(GEN pol, GEN ypowers, GEN xpowers, ulong p, ulong pi)
     480             : {
     481     5352766 :   pari_sp av = avma;
     482     5352766 :   GEN t = FlxY_evalx_powers_pre(pol, ypowers, p, pi);
     483     5352766 :   return gc_ulong(av, Flx_eval_powers_pre(t, xpowers, p, pi));
     484             : }
     485             : 
     486             : GEN
     487      120852 : FlxY_FlxqV_evalx(GEN P, GEN x, GEN T, ulong p)
     488             : {
     489      120852 :   long i, lP = lg(P);
     490      120852 :   GEN res = cgetg(lP,t_POL);
     491      120852 :   res[1] = P[1];
     492      777335 :   for(i=2; i<lP; i++)
     493      656483 :     gel(res,i) = Flx_FlxqV_eval(gel(P,i), x, T, p);
     494      120852 :   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        4892 : FlxY_Flx_div(GEN x, GEN y, ulong p)
     508             : {
     509             :   long i, l;
     510             :   GEN z;
     511        4892 :   if (degpol(y) == 0)
     512             :   {
     513        3622 :     ulong t = uel(y,2);
     514        3622 :     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        1269 :     z = cgetg_copy(x, &l); z[1] = x[1];
     522        2545 :     for (i=2; i<l; i++) gel(z,i) = Flx_div(gel(x,i),y,p);
     523             :   }
     524        1272 :   return z;
     525             : }
     526             : 
     527             : GEN
     528       33127 : FlxX_shift(GEN a, long n, long vs)
     529             : {
     530       33127 :   long i, l = lg(a);
     531             :   GEN  b;
     532       33127 :   if (l == 2 || !n) return a;
     533       28337 :   l += n;
     534       28337 :   if (n < 0)
     535             :   {
     536       17320 :     if (l <= 2) return pol_0(varn(a));
     537       13062 :     b = cgetg(l, t_POL); b[1] = a[1];
     538       13065 :     a -= n;
     539      231113 :     for (i=2; i<l; i++) gel(b,i) = gel(a,i);
     540             :   } else {
     541       11017 :     b = cgetg(l, t_POL); b[1] = a[1];
     542       11019 :     a -= n; n += 2;
     543       88377 :     for (i=2; i<n; i++) gel(b,i) = pol0_Flx(vs);
     544       80874 :     for (   ; i<l; i++) gel(b,i) = gel(a,i);
     545             :   }
     546       24082 :   return b;
     547             : }
     548             : 
     549             : GEN
     550        9181 : FlxX_blocks(GEN P, long n, long m, long vs)
     551             : {
     552        9181 :   GEN z = cgetg(m+1,t_VEC);
     553        9181 :   long i,j, k=2, l = lg(P);
     554       27543 :   for(i=1; i<=m; i++)
     555             :   {
     556       18360 :     GEN zi = cgetg(n+2,t_POL);
     557       18360 :     zi[1] = P[1];
     558       18360 :     gel(z,i) = zi;
     559      130605 :     for(j=2; j<n+2; j++)
     560      112318 :       gel(zi, j) = k==l ? pol0_Flx(vs) : gel(P,k++);
     561       18287 :     zi = FlxX_renormalize(zi, n+2);
     562             :   }
     563        9183 :   return z;
     564             : }
     565             : 
     566             : static GEN
     567      141436 : FlxX_recipspec(GEN x, long l, long n, long vs)
     568             : {
     569             :   long i;
     570      141436 :   GEN z = cgetg(n+2,t_POL);
     571      141436 :   z[1] = 0; z += 2;
     572     3342461 :   for(i=0; i<l; i++)
     573     3201025 :     gel(z,n-i-1) = Flx_copy(gel(x,i));
     574      148184 :   for(   ; i<n; i++)
     575        6748 :     gel(z,n-i-1) = pol0_Flx(vs);
     576      141436 :   return FlxX_renormalize(z-2,n+2);
     577             : }
     578             : 
     579             : GEN
     580        1592 : FlxX_invLaplace(GEN x, ulong p)
     581             : {
     582        1592 :   long i, d = degpol(x);
     583             :   GEN y;
     584             :   ulong t;
     585        1592 :   if (d <= 1) return gcopy(x);
     586        1585 :   t = Fl_inv(factorial_Fl(d, p), p);
     587        1585 :   y = cgetg(d+3, t_POL);
     588        1585 :   y[1] = x[1];
     589       40186 :   for (i=d; i>=2; i--)
     590             :   {
     591       38601 :     gel(y,i+2) = Flx_Fl_mul(gel(x,i+2), t, p);
     592       38537 :     t = Fl_mul(t, i, p);
     593             :   }
     594        1585 :   gel(y,3) = Flx_copy(gel(x,3));
     595        1585 :   gel(y,2) = Flx_copy(gel(x,2));
     596        1585 :   return FlxX_renormalize(y, d+3);
     597             : }
     598             : 
     599             : GEN
     600         796 : FlxX_Laplace(GEN x, ulong p)
     601             : {
     602         796 :   long i, d = degpol(x);
     603         796 :   ulong t = 1;
     604             :   GEN y;
     605         796 :   if (d <= 1) return gcopy(x);
     606         796 :   y = cgetg(d+3, t_POL);
     607         796 :   y[1] = x[1];
     608         796 :   gel(y,2) = Flx_copy(gel(x,2));
     609         796 :   gel(y,3) = Flx_copy(gel(x,3));
     610       20268 :   for (i=2; i<=d; i++)
     611             :   {
     612       19472 :     t = Fl_mul(t, i%p, p);
     613       19472 :     gel(y,i+2) = Flx_Fl_mul(gel(x,i+2), t, p);
     614             :   }
     615         796 :   return FlxX_renormalize(y, d+3);
     616             : }
     617             : 
     618             : /***********************************************************************/
     619             : /**                                                                   **/
     620             : /**                               FlxqX                               **/
     621             : /**                                                                   **/
     622             : /***********************************************************************/
     623             : 
     624             : static GEN
     625     1720480 : get_FlxqX_red(GEN T, GEN *B)
     626             : {
     627     1720480 :   if (typ(T)!=t_VEC) { *B=NULL; return T; }
     628       83144 :   *B = gel(T,1); return gel(T,2);
     629             : }
     630             : 
     631             : GEN
     632       43006 : RgX_to_FlxqX(GEN x, GEN T, ulong p)
     633             : {
     634       43006 :   long i, l = lg(x);
     635       43006 :   GEN z = cgetg(l, t_POL); z[1] = x[1];
     636      761785 :   for (i = 2; i < l; i++)
     637      718785 :     gel(z,i) = Rg_to_Flxq(gel(x,i), T, p);
     638       43000 :   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        2740 :   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      944807 : Kronecker_to_FlxqX(GEN z, GEN T, ulong p)
     657             : {
     658      944807 :   long i,j,lx,l, N = (get_Flx_degree(T)<<1) + 1;
     659      944807 :   GEN x, t = cgetg(N,t_VECSMALL);
     660      944804 :   t[1] = get_Flx_var(T);
     661      944803 :   l = lg(z); lx = (l-2) / (N-2);
     662      944803 :   x = cgetg(lx+3,t_POL);
     663      944801 :   x[1] = z[1];
     664    16871359 :   for (i=2; i<lx+2; i++)
     665             :   {
     666   216110156 :     for (j=2; j<N; j++) t[j] = z[j];
     667    15926565 :     z += (N-2);
     668    15926565 :     gel(x,i) = Flx_rem(Flx_renormalize(t,N), T,p);
     669             :   }
     670      944794 :   N = (l-2) % (N-2) + 2;
     671     3320279 :   for (j=2; j<N; j++) t[j] = z[j];
     672      944794 :   gel(x,i) = Flx_rem(Flx_renormalize(t,N), T,p);
     673      944798 :   return FlxX_renormalize(x, i+1);
     674             : }
     675             : 
     676             : GEN
     677      792315 : FlxqX_red(GEN z, GEN T, ulong p)
     678             : {
     679             :   GEN res;
     680      792315 :   long i, l = lg(z);
     681      792315 :   res = cgetg(l,t_POL); res[1] = z[1];
     682     9050143 :   for(i=2;i<l;i++) gel(res,i) = Flx_rem(gel(z,i),T,p);
     683      792315 :   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      557350 : FlxqX_mul(GEN x, GEN y, GEN T, ulong p)
     701             : {
     702      557350 :   pari_sp ltop=avma;
     703      557350 :   GEN z, kx, ky, Tm = get_Flx_mod(T);
     704      557351 :   kx= zxX_to_Kronecker(x, Tm);
     705      557352 :   ky= zxX_to_Kronecker(y, Tm);
     706      557354 :   z = Flx_mul(ky, kx, p);
     707      557353 :   z = Kronecker_to_FlxqX(z, T, p);
     708      557339 :   return gerepileupto(ltop, z);
     709             : }
     710             : 
     711             : GEN
     712      214835 : FlxqX_sqr(GEN x, GEN T, ulong p)
     713             : {
     714      214835 :   pari_sp ltop=avma;
     715             :   GEN z,kx;
     716      214835 :   kx= zxX_to_Kronecker(x,get_Flx_mod(T));
     717      214835 :   z = Flx_sqr(kx, p);
     718      214835 :   z = Kronecker_to_FlxqX(z,T,p);
     719      214835 :   return gerepileupto(ltop,z);
     720             : }
     721             : 
     722             : GEN
     723       12326 : FlxqX_Flxq_mul(GEN P, GEN U, GEN T, ulong p)
     724             : {
     725       12326 :   long i, lP = lg(P);
     726       12326 :   GEN res = cgetg(lP,t_POL);
     727       12326 :   res[1] = P[1];
     728       71940 :   for(i=2; i<lP; i++)
     729       59614 :     gel(res,i) = Flxq_mul(U,gel(P,i), T,p);
     730       12326 :   return FlxX_renormalize(res, lP);
     731             : }
     732             : GEN
     733      302939 : FlxqX_Flxq_mul_to_monic(GEN P, GEN U, GEN T, ulong p)
     734             : {
     735      302939 :   long i, lP = lg(P);
     736      302939 :   GEN res = cgetg(lP,t_POL);
     737      302930 :   res[1] = P[1];
     738     1906472 :   for(i=2; i<lP-1; i++) gel(res,i) = Flxq_mul(U,gel(P,i), T,p);
     739      302542 :   gel(res,lP-1) = pol1_Flx(get_Flx_var(T));
     740      302954 :   return FlxX_renormalize(res, lP);
     741             : }
     742             : 
     743             : GEN
     744      173724 : FlxqX_normalize(GEN z, GEN T, ulong p)
     745             : {
     746      173724 :   GEN p1 = leading_coeff(z);
     747      173724 :   if (!lgpol(z) || (!degpol(p1) && p1[1] == 1)) return z;
     748      173703 :   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     1358358 : 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     1358358 :   if (!signe(y)) pari_err_INV("FlxqX_divrem",y);
     760     1358358 :   vx=varn(x); dy=degpol(y); dx=degpol(x);
     761     1358357 :   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     1344172 :   lead = leading_coeff(y);
     773     1344171 :   if (!dy) /* y is constant */
     774             :   {
     775      126980 :     if (pr && pr != ONLY_DIVIDES)
     776             :     {
     777      122388 :       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     1217191 :   av0 = avma; dz = dx-dy;
     785     1217191 :   lead = Flx_equal1(lead)? NULL: gclone(Flxq_inv(lead,T,p));
     786     1217187 :   set_avma(av0);
     787     1217188 :   z = cgetg(dz+3,t_POL); z[1] = x[1];
     788     1217188 :   x += 2; y += 2; z += 2;
     789             : 
     790     1217188 :   p1 = gel(x,dx); av = avma;
     791     1217188 :   gel(z,dz) = lead? gerepileupto(av, Flxq_mul(p1,lead, T, p)): gcopy(p1);
     792     3301855 :   for (i=dx-1; i>=dy; i--)
     793             :   {
     794     2084699 :     av=avma; p1=gel(x,i);
     795     7480852 :     for (j=i-dy+1; j<=i && j<=dz; j++)
     796     5396219 :       p1 = Flx_sub(p1, Flx_mul(gel(z,j),gel(y,i-j),p),p);
     797     2084633 :     if (lead) p1 = Flx_mul(p1, lead,p);
     798     2084633 :     tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil,Flx_rem(p1,T,p));
     799             :   }
     800     1217156 :   if (!pr) { guncloneNULL(lead); return z-2; }
     801             : 
     802     1182245 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
     803     1182256 :   for (sx=0; ; i--)
     804             :   {
     805      277039 :     p1 = gel(x,i);
     806     4842921 :     for (j=0; j<=i && j<=dz; j++)
     807     3383769 :       p1 = Flx_sub(p1, Flx_mul(gel(z,j),gel(y,i-j),p),p);
     808     1459152 :     tetpil=avma; p1 = Flx_rem(p1, T, p); if (lgpol(p1)) { sx = 1; break; }
     809      337273 :     if (!i) break;
     810      277038 :     set_avma(av);
     811             :   }
     812     1182206 :   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     1182206 :   lr=i+3; rem -= lr;
     819     1182206 :   rem[0] = evaltyp(t_POL) | evallg(lr);
     820     1182207 :   rem[1] = z[-1];
     821     1182207 :   p1 = gerepile((pari_sp)rem,tetpil,p1);
     822     1182239 :   rem += 2; gel(rem,i) = p1;
     823    11350561 :   for (i--; i>=0; i--)
     824             :   {
     825    10168327 :     av=avma; p1 = gel(x,i);
     826    35361355 :     for (j=0; j<=i && j<=dz; j++)
     827    25200949 :       p1 = Flx_sub(p1, Flx_mul(gel(z,j),gel(y,i-j),p), p);
     828    10160406 :     tetpil=avma; gel(rem,i) = gerepile(av,tetpil, Flx_rem(p1, T, p));
     829             :   }
     830     1182234 :   rem -= 2;
     831     1182234 :   guncloneNULL(lead);
     832     1182244 :   if (!sx) (void)FlxX_renormalize(rem, lr);
     833     1182244 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
     834      223865 :   *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      200288 :   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       24137 :   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       42748 :     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      202606 :     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      428907 : FlxqX_get_red(GEN S, GEN T, ulong p)
     948             : {
     949      428907 :   if (typ(S)==t_POL && lg(S)>FlxqX_BARRETT_LIMIT)
     950        3510 :     retmkvec2(FlxqX_invBarrett(S, T, p), S);
     951      425397 :   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       51677 :     for (i=0;i<l-lt;i++) gel(q+2,i) = pol0_Flx(vT);
     996             :   }
     997       66391 :   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       46007 :       for(i=0; i<lq; i++) gel(q+2+l-lm,i) = gel(zq,2+i);
    1005             :     }
    1006       79547 :     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        5572 :       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      287571 : FlxqX_divrem(GEN x, GEN S, GEN T, ulong p, GEN *pr)
    1037             : {
    1038             :   GEN B, y;
    1039             :   long dy, dx, d;
    1040      287571 :   if (pr==ONLY_REM) return FlxqX_rem(x, S, T, p);
    1041      287571 :   y = get_FlxqX_red(S, &B);
    1042      287571 :   dy = degpol(y); dx = degpol(x); d = dx-dy;
    1043      287571 :   if (!B && d+3 < FlxqX_DIVREM_BARRETT_LIMIT)
    1044      286506 :     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     1432479 : FlxqX_rem(GEN x, GEN S, GEN T, ulong p)
    1059             : {
    1060     1432479 :   GEN B, y = get_FlxqX_red(S, &B);
    1061     1432479 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
    1062     1432480 :   if (d < 0) return FlxqX_red(x, T, p);
    1063     1128244 :   if (!B && d+3 < FlxqX_REM_BARRETT_LIMIT)
    1064     1071851 :     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         603 : FlxqX_halfgcd_basecase(GEN a, GEN b, GEN T, ulong p)
    1076             : {
    1077         603 :   pari_sp av=avma;
    1078             :   GEN u,u1,v,v1;
    1079         603 :   long vx = varn(a);
    1080         603 :   long n = lgpol(a)>>1;
    1081         603 :   u1 = v = pol_0(vx);
    1082         603 :   u = v1 = pol1_FlxX(vx, get_Flx_var(T));
    1083        8950 :   while (lgpol(b)>n)
    1084             :   {
    1085        8347 :     GEN r, q = FlxqX_divrem(a,b, T, p, &r);
    1086        8347 :     a = b; b = r; swap(u,u1); swap(v,v1);
    1087        8347 :     u1 = FlxX_sub(u1, FlxqX_mul(u, q, T, p), p);
    1088        8347 :     v1 = FlxX_sub(v1, FlxqX_mul(v, q ,T, p), p);
    1089        8347 :     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         603 :   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         966 : FlxqX_halfgcd_i(GEN x, GEN y, GEN T, ulong p)
    1173             : {
    1174         966 :   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         966 : FlxqX_halfgcd(GEN x, GEN y, GEN T, ulong p)
    1180             : {
    1181         966 :   pari_sp av = avma;
    1182             :   GEN M,q,r;
    1183         966 :   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         966 :   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      154740 : FlxqX_gcd_basecase(GEN a, GEN b, GEN T, ulong p)
    1199             : {
    1200      154740 :   pari_sp av = avma, av0=avma;
    1201      882534 :   while (signe(b))
    1202             :   {
    1203             :     GEN c;
    1204      727794 :     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      727794 :     av = avma; c = FlxqX_rem(a, b, T, p); a=b; b=c;
    1210             :   }
    1211      154740 :   set_avma(av); return a;
    1212             : }
    1213             : 
    1214             : GEN
    1215      159544 : FlxqX_gcd(GEN x, GEN y, GEN T, ulong p)
    1216             : {
    1217      159544 :   pari_sp av = avma;
    1218      159544 :   x = FlxqX_red(x, T, p);
    1219      159544 :   y = FlxqX_red(y, T, p);
    1220      159544 :   if (!signe(x)) return gerepileupto(av, y);
    1221      154761 :   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      154740 :   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       36967 :   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             : static GEN
    1304      109537 : FlxqX_saferem(GEN P, GEN Q, GEN T, ulong p)
    1305             : {
    1306      109537 :   GEN U = Flxq_invsafe(leading_coeff(Q), T, p);
    1307      109572 :   if (!U) return NULL;
    1308      109572 :   Q = FlxqX_Flxq_mul_to_monic(Q,U,T,p);
    1309      109604 :   return FlxqX_rem(P,Q,T,p);
    1310             : }
    1311             : 
    1312             : GEN
    1313       19665 : FlxqX_safegcd(GEN P, GEN Q, GEN T, ulong p)
    1314             : {
    1315       19665 :   pari_sp av = avma;
    1316             :   GEN U;
    1317       19665 :   if (!signe(P)) return gcopy(Q);
    1318       19665 :   if (!signe(Q)) return gcopy(P);
    1319       19665 :   T = Flx_get_red(T,p);
    1320             :   for(;;)
    1321             :   {
    1322       71248 :     P = FlxqX_saferem(P,Q,T,p);
    1323       71248 :     if (!P) return gc_NULL(av);
    1324       71248 :     if (!signe(P)) break;
    1325       51583 :     if (gc_needed(av, 1))
    1326             :     {
    1327           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_safegcd");
    1328           0 :       gerepileall(av, 2, &P,&Q);
    1329             :     }
    1330       51583 :     swap(P, Q);
    1331             :   }
    1332       19665 :   U = Flxq_invsafe(leading_coeff(Q), T, p);
    1333       19665 :   if (!U) return gc_NULL(av);
    1334       19665 :   Q = FlxqX_Flxq_mul_to_monic(Q,U,T,p);
    1335       19665 :   return gerepileupto(av, Q);
    1336             : }
    1337             : 
    1338             : struct _FlxqX {ulong p; GEN T;};
    1339        1890 : static GEN _FlxqX_mul(void *data,GEN a,GEN b)
    1340             : {
    1341        1890 :   struct _FlxqX *d=(struct _FlxqX*)data;
    1342        1890 :   return FlxqX_mul(a,b,d->T,d->p);
    1343             : }
    1344       10255 : static GEN _FlxqX_sqr(void *data,GEN a)
    1345             : {
    1346       10255 :   struct _FlxqX *d=(struct _FlxqX*)data;
    1347       10255 :   return FlxqX_sqr(a,d->T,d->p);
    1348             : }
    1349             : 
    1350             : GEN
    1351       10227 : FlxqX_powu(GEN V, ulong n, GEN T, ulong p)
    1352             : {
    1353       10227 :   struct _FlxqX d; d.p=p; d.T=T;
    1354       10227 :   return gen_powu(V, n, (void*)&d, &_FlxqX_sqr, &_FlxqX_mul);
    1355             : }
    1356             : 
    1357             : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
    1358             : GEN
    1359        5910 : FlxqX_saferesultant(GEN a, GEN b, GEN T, ulong p)
    1360             : {
    1361        5910 :   long vT = get_Flx_var(T);
    1362             :   long da,db,dc;
    1363             :   pari_sp av;
    1364        5910 :   GEN c,lb, res = pol1_Flx(vT);
    1365             : 
    1366        5910 :   if (!signe(a) || !signe(b)) return pol0_Flx(vT);
    1367             : 
    1368        5910 :   da = degpol(a);
    1369        5910 :   db = degpol(b);
    1370        5910 :   if (db > da)
    1371             :   {
    1372           0 :     swapspec(a,b, da,db);
    1373           0 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1374             :   }
    1375        5910 :   if (!da) return pol1_Flx(vT); /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
    1376        5910 :   av = avma;
    1377       44194 :   while (db)
    1378             :   {
    1379       38289 :     lb = gel(b,db+2);
    1380       38289 :     c = FlxqX_saferem(a,b, T,p);
    1381       38382 :     if (!c) return gc_NULL(av);
    1382       38382 :     a = b; b = c; dc = degpol(c);
    1383       38378 :     if (dc < 0) { set_avma(av); return pol0_Flx(vT); }
    1384             : 
    1385       38378 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1386       38377 :     if (!equali1(lb)) res = Flxq_mul(res, Flxq_powu(lb, da - dc, T, p), T, p);
    1387       38285 :     if (gc_needed(av,2))
    1388             :     {
    1389           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (da = %ld)",da);
    1390           0 :       gerepileall(av,3, &a,&b,&res);
    1391             :     }
    1392       38284 :     da = db; /* = degpol(a) */
    1393       38284 :     db = dc; /* = degpol(b) */
    1394             :   }
    1395        5905 :   res = Flxq_mul(res, Flxq_powu(gel(b,2), da, T, p), T, p);
    1396        5909 :   return gerepileupto(av, res);
    1397             : }
    1398             : 
    1399             : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
    1400             : GEN
    1401          56 : FlxqX_resultant(GEN a, GEN b, GEN T, ulong p)
    1402             : {
    1403          56 :   long vT = get_Flx_var(T);
    1404             :   long da,db,dc;
    1405             :   pari_sp av;
    1406          56 :   GEN c,lb, res = pol1_Flx(vT);
    1407             : 
    1408          56 :   if (!signe(a) || !signe(b)) return pol0_Flx(vT);
    1409             : 
    1410          56 :   da = degpol(a);
    1411          56 :   db = degpol(b);
    1412          56 :   if (db > da)
    1413             :   {
    1414          21 :     swapspec(a,b, da,db);
    1415          21 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1416             :   }
    1417          56 :   if (!da) return pol1_Flx(vT); /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
    1418          56 :   av = avma;
    1419         147 :   while (db)
    1420             :   {
    1421          91 :     lb = gel(b,db+2);
    1422          91 :     c = FlxqX_rem(a,b, T,p);
    1423          91 :     a = b; b = c; dc = degpol(c);
    1424          91 :     if (dc < 0) { set_avma(av); return pol0_Flx(vT); }
    1425             : 
    1426          91 :     if (both_odd(da,db)) res = Flx_neg(res, p);
    1427          91 :     if (!equali1(lb)) res = Flxq_mul(res, Flxq_powu(lb, da - dc, T, p), T, p);
    1428          91 :     if (gc_needed(av,2))
    1429             :     {
    1430           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (da = %ld)",da);
    1431           0 :       gerepileall(av,3, &a,&b,&res);
    1432             :     }
    1433          91 :     da = db; /* = degpol(a) */
    1434          91 :     db = dc; /* = degpol(b) */
    1435             :   }
    1436          56 :   res = Flxq_mul(res, Flxq_powu(gel(b,2), da, T, p), T, p);
    1437          56 :   return gerepileupto(av, res);
    1438             : }
    1439             : 
    1440             : /* disc P = (-1)^(n(n-1)/2) lc(P)^(n - deg P' - 2) Res(P,P'), n = deg P */
    1441             : GEN
    1442          14 : FlxqX_disc(GEN P, GEN T, ulong p)
    1443             : {
    1444          14 :   pari_sp av = avma;
    1445          14 :   GEN L, dP = FlxX_deriv(P, p), D = FlxqX_resultant(P, dP, T, p);
    1446             :   long dd;
    1447          14 :   if (!lgpol(D)) return pol0_Flx(get_Flx_var(T));
    1448          14 :   dd = degpol(P) - 2 - degpol(dP); /* >= -1; > -1 iff p | deg(P) */
    1449          14 :   L = leading_coeff(P);
    1450          14 :   if (dd && !Flx_equal1(L))
    1451           0 :     D = (dd == -1)? Flxq_div(D,L,T,p): Flxq_mul(D, Flxq_powu(L, dd, T, p), T, p);
    1452          14 :   if (degpol(P) & 2) D = Flx_neg(D, p);
    1453          14 :   return gerepileupto(av, D);
    1454             : }
    1455             : 
    1456             : INLINE GEN
    1457        3980 : FlxXn_recip(GEN x, long n, long v)
    1458             : {
    1459        3980 :   return FlxX_recipspec(x+2, minss(lgpol(x), n), n, v);
    1460             : }
    1461             : 
    1462             : GEN
    1463        1592 : FlxqX_Newton(GEN P, long n, GEN T, ulong p)
    1464             : {
    1465        1592 :   pari_sp av = avma;
    1466        1592 :   long d = degpol(P), vT = get_Flx_var(T);
    1467        1592 :   GEN dP = FlxXn_recip(FlxX_deriv(P, p), d, vT);
    1468        1592 :   GEN Q = FlxqXn_mul(FlxqXn_inv(FlxXn_recip(P, d+1, vT), n, T, p), dP, n, T, p);
    1469        1592 :   return gerepilecopy(av, Q);
    1470             : }
    1471             : 
    1472             : GEN
    1473         796 : FlxqX_fromNewton(GEN P, GEN T, ulong p)
    1474             : {
    1475         796 :   pari_sp av = avma;
    1476         796 :   long vT = get_Flx_var(T);
    1477         796 :   long n = Flx_constant(constant_coeff(P))+1;
    1478         796 :   GEN z = FlxX_neg(FlxX_shift(P, -1, vT), p);
    1479         796 :   GEN Q = FlxXn_recip(FlxqXn_expint(z, n, T, p), n, vT);
    1480         796 :   return gerepilecopy(av, Q);
    1481             : }
    1482             : 
    1483             : static GEN
    1484         796 : FlxqX_composedsum(GEN P, GEN Q, GEN T, ulong p)
    1485             : {
    1486         796 :   long n = 1+ degpol(P)*degpol(Q);
    1487         796 :   GEN Pl = FlxX_invLaplace(FlxqX_Newton(P,n, T,p), p);
    1488         796 :   GEN Ql = FlxX_invLaplace(FlxqX_Newton(Q,n, T,p), p);
    1489         796 :   GEN L = FlxX_Laplace(FlxqXn_mul(Pl, Ql, n, T,p), p);
    1490         796 :   GEN R = FlxqX_fromNewton(L, T, p);
    1491         796 :   GEN lead = Flxq_mul(Flxq_powu(leading_coeff(P),degpol(Q), T, p),
    1492         796 :                       Flxq_powu(leading_coeff(Q),degpol(P), T, p), T, p);
    1493         796 :   return FlxqX_Flxq_mul(R, lead, T, p);
    1494             : }
    1495             : 
    1496             : GEN
    1497         796 : FlxqX_direct_compositum(GEN P, GEN Q, GEN T, ulong p)
    1498             : {
    1499         796 :   return FlxqX_composedsum(P, Q, T, p);
    1500             : }
    1501             : 
    1502             : GEN
    1503         902 : FlxqXV_prod(GEN V, GEN T, ulong p)
    1504             : {
    1505         902 :   struct _FlxqX d; d.p=p; d.T=T;
    1506         902 :   return gen_product(V, (void*)&d, &_FlxqX_mul);
    1507             : }
    1508             : 
    1509             : static GEN
    1510         890 : FlxqV_roots_to_deg1(GEN x, GEN T, ulong p, long v)
    1511             : {
    1512         890 :   long sv = get_Flx_var(T);
    1513        3578 :   pari_APPLY_same(deg1pol_shallow(pol1_Flx(sv),Flx_neg(gel(x,i),p),v))
    1514             : }
    1515             : 
    1516             : GEN
    1517         890 : FlxqV_roots_to_pol(GEN V, GEN T, ulong p, long v)
    1518             : {
    1519         890 :   pari_sp ltop = avma;
    1520         890 :   GEN W = FlxqV_roots_to_deg1(V, T, p, v);
    1521         890 :   return gerepileupto(ltop, FlxqXV_prod(W, T, p));
    1522             : }
    1523             : 
    1524             : /*******************************************************************/
    1525             : /*                                                                 */
    1526             : /*                       (Fl[X]/T(X))[Y] / S(Y)                    */
    1527             : /*                                                                 */
    1528             : /*******************************************************************/
    1529             : 
    1530             : GEN
    1531      386353 : FlxqXQ_mul(GEN x, GEN y, GEN S, GEN T, ulong p) {
    1532      386353 :   return FlxqX_rem(FlxqX_mul(x,y,T,p),S,T,p);
    1533             : }
    1534             : 
    1535             : GEN
    1536      195506 : FlxqXQ_sqr(GEN x, GEN S, GEN T, ulong p) {
    1537      195506 :   return FlxqX_rem(FlxqX_sqr(x,T,p),S,T,p);
    1538             : }
    1539             : 
    1540             : GEN
    1541          14 : FlxqXQ_invsafe(GEN x, GEN S, GEN T, ulong p)
    1542             : {
    1543          14 :   GEN V, z = FlxqX_extgcd(get_FlxqX_mod(S), x, T, p, NULL, &V);
    1544          14 :   if (degpol(z)) return NULL;
    1545          14 :   z = Flxq_invsafe(gel(z,2),T,p);
    1546          14 :   if (!z) return NULL;
    1547          14 :   return FlxqX_Flxq_mul(V, z, T, p);
    1548             : }
    1549             : 
    1550             : GEN
    1551          14 : FlxqXQ_inv(GEN x, GEN S, GEN T,ulong p)
    1552             : {
    1553          14 :   pari_sp av = avma;
    1554          14 :   GEN U = FlxqXQ_invsafe(x, S, T, p);
    1555          14 :   if (!U) pari_err_INV("FlxqXQ_inv",x);
    1556          14 :   return gerepileupto(av, U);
    1557             : }
    1558             : 
    1559             : GEN
    1560           0 : FlxqXQ_div(GEN x, GEN y, GEN S, GEN T, ulong p) {
    1561           0 :   return FlxqXQ_mul(x, FlxqXQ_inv(y,S,T,p),S,T,p);
    1562             : }
    1563             : 
    1564             : struct _FlxqXQ {
    1565             :   GEN T, S;
    1566             :   ulong p;
    1567             : };
    1568             : static GEN
    1569           0 : _FlxqXQ_add(void *data, GEN x, GEN y) {
    1570           0 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1571           0 :   return FlxX_add(x,y, d->p);
    1572             : }
    1573             : static GEN
    1574        2121 : _FlxqXQ_sub(void *data, GEN x, GEN y) {
    1575        2121 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1576        2121 :   return FlxX_sub(x,y, d->p);
    1577             : }
    1578             : #if 0
    1579             : static GEN
    1580             : _FlxqXQ_cmul(void *data, GEN P, long a, GEN x) {
    1581             :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1582             :   return FlxX_Flx_mul(x,gel(P,a+2), d->p);
    1583             : }
    1584             : #endif
    1585             : static GEN
    1586        2440 : _FlxqXQ_red(void *data, GEN x) {
    1587        2440 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1588        2440 :   return FlxqX_red(x, d->T, d->p);
    1589             : }
    1590             : static GEN
    1591      136926 : _FlxqXQ_mul(void *data, GEN x, GEN y) {
    1592      136926 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1593      136926 :   return FlxqXQ_mul(x,y, d->S,d->T, d->p);
    1594             : }
    1595             : static GEN
    1596      195093 : _FlxqXQ_sqr(void *data, GEN x) {
    1597      195093 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1598      195093 :   return FlxqXQ_sqr(x, d->S,d->T, d->p);
    1599             : }
    1600             : 
    1601             : static GEN
    1602       95254 : _FlxqXQ_one(void *data) {
    1603       95254 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1604       95254 :   return pol1_FlxX(get_FlxqX_var(d->S),get_Flx_var(d->T));
    1605             : }
    1606             : 
    1607             : static GEN
    1608         191 : _FlxqXQ_zero(void *data) {
    1609         191 :   struct _FlxqXQ *d = (struct _FlxqXQ*) data;
    1610         191 :   return pol_0(get_FlxqX_var(d->S));
    1611             : }
    1612             : 
    1613             : static struct bb_algebra FlxqXQ_algebra = { _FlxqXQ_red, _FlxqXQ_add,
    1614             :        _FlxqXQ_sub, _FlxqXQ_mul, _FlxqXQ_sqr, _FlxqXQ_one, _FlxqXQ_zero };
    1615             : 
    1616             : const struct bb_algebra *
    1617         219 : get_FlxqXQ_algebra(void **E, GEN S, GEN T, ulong p)
    1618             : {
    1619         219 :   GEN z = new_chunk(sizeof(struct _FlxqXQ));
    1620         219 :   struct _FlxqXQ *e = (struct _FlxqXQ *) z;
    1621         219 :   e->T = Flx_get_red(T, p);
    1622         219 :   e->S = FlxqX_get_red(S, e->T, p);
    1623         219 :   e->p  = p; *E = (void*)e;
    1624         219 :   return &FlxqXQ_algebra;
    1625             : }
    1626             : 
    1627             : /* x over Fq, return lift(x^n) mod S */
    1628             : GEN
    1629          56 : FlxqXQ_pow(GEN x, GEN n, GEN S, GEN T, ulong p)
    1630             : {
    1631          56 :   pari_sp av = avma;
    1632             :   struct _FlxqXQ D;
    1633          56 :   long s = signe(n);
    1634          56 :   if (!s) return pol1_FlxX(get_FlxqX_var(S),get_Flx_var(T));
    1635          56 :   if (s < 0) x = FlxqXQ_inv(x,S,T,p);
    1636          56 :   if (is_pm1(n)) return s < 0 ? x : gcopy(x);
    1637          56 :   if (degpol(x) >= get_FlxqX_degree(S)) x = FlxqX_rem(x,S,T,p);
    1638          56 :   T = Flx_get_red(T, p);
    1639          56 :   S = FlxqX_get_red(S, T, p);
    1640          56 :   D.S = S;
    1641          56 :   D.T = T;
    1642          56 :   D.p = p;
    1643          56 :   x = gen_pow_i(x, n, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul);
    1644          56 :   return gerepilecopy(av, x);
    1645             : }
    1646             : 
    1647             : /* x over Fq, return lift(x^n) mod S */
    1648             : GEN
    1649       73054 : FlxqXQ_powu(GEN x, ulong n, GEN S, GEN T, ulong p)
    1650             : {
    1651       73054 :   pari_sp av = avma;
    1652             :   struct _FlxqXQ D;
    1653       73054 :   switch(n)
    1654             :   {
    1655           0 :     case 0: return pol1_FlxX(get_FlxqX_var(S),get_Flx_var(T));
    1656        7490 :     case 1: return gcopy(x);
    1657         413 :     case 2: return FlxqXQ_sqr(x, S, T, p);
    1658             :   }
    1659       65151 :   T = Flx_get_red(T, p);
    1660       65151 :   S = FlxqX_get_red(S, T, p);
    1661       65151 :   D.S = S; D.T = T; D.p = p;
    1662       65151 :   x = gen_powu_i(x, n, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul);
    1663       65151 :   return gerepilecopy(av, x);
    1664             : }
    1665             : 
    1666             : GEN
    1667       93776 : FlxqXQ_powers(GEN x, long l, GEN S, GEN T, ulong p)
    1668             : {
    1669             :   struct _FlxqXQ D;
    1670       93776 :   int use_sqr = 2*degpol(x) >= get_FlxqX_degree(S);
    1671       93776 :   T = Flx_get_red(T, p);
    1672       93776 :   S = FlxqX_get_red(S, T, p);
    1673       93776 :   D.S = S; D.T = T; D.p = p;
    1674       93776 :   return gen_powers(x, l, use_sqr, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul,&_FlxqXQ_one);
    1675             : }
    1676             : 
    1677             : /* Let v a linear form, return the linear form z->v(tau*z)
    1678             :    that is, v*(M_tau) */
    1679             : 
    1680             : static GEN
    1681         430 : FlxqXQ_transmul_init(GEN tau, GEN S, GEN T, ulong p)
    1682             : {
    1683             :   GEN bht;
    1684         430 :   GEN h, Sp = get_FlxqX_red(S, &h);
    1685         430 :   long n = degpol(Sp), vS = varn(Sp), vT = get_Flx_var(T);
    1686         430 :   GEN ft = FlxX_recipspec(Sp+2, n+1, n+1, vT);
    1687         430 :   GEN bt = FlxX_recipspec(tau+2, lgpol(tau), n, vT);
    1688         430 :   setvarn(ft, vS); setvarn(bt, vS);
    1689         430 :   if (h)
    1690           0 :     bht = FlxqXn_mul(bt, h, n-1, T, p);
    1691             :   else
    1692             :   {
    1693         430 :     GEN bh = FlxqX_div(FlxX_shift(tau, n-1, vT), S, T, p);
    1694         430 :     bht = FlxX_recipspec(bh+2, lgpol(bh), n-1, vT);
    1695         430 :     setvarn(bht, vS);
    1696             :   }
    1697         430 :   return mkvec3(bt, bht, ft);
    1698             : }
    1699             : 
    1700             : static GEN
    1701         906 : FlxqXQ_transmul(GEN tau, GEN a, long n, GEN T, ulong p)
    1702             : {
    1703         906 :   pari_sp ltop = avma;
    1704             :   GEN t1, t2, t3, vec;
    1705         906 :   GEN bt = gel(tau, 1), bht = gel(tau, 2), ft = gel(tau, 3);
    1706         906 :   long vT = get_Flx_var(T);
    1707         906 :   if (signe(a)==0) return pol_0(varn(a));
    1708         906 :   t2 = FlxX_shift(FlxqX_mul(bt, a, T, p),1-n,vT);
    1709         906 :   if (signe(bht)==0) return gerepilecopy(ltop, t2);
    1710         607 :   t1 = FlxX_shift(FlxqX_mul(ft, a, T, p),-n,vT);
    1711         607 :   t3 = FlxqXn_mul(t1, bht, n-1, T, p);
    1712         607 :   vec = FlxX_sub(t2, FlxX_shift(t3, 1, vT), p);
    1713         607 :   return gerepileupto(ltop, vec);
    1714             : }
    1715             : 
    1716             : static GEN
    1717         215 : polxn_FlxX(long n, long v, long vT)
    1718             : {
    1719         215 :   long i, a = n+2;
    1720         215 :   GEN p = cgetg(a+1, t_POL);
    1721         215 :   p[1] = evalsigne(1)|evalvarn(v);
    1722        1835 :   for (i = 2; i < a; i++) gel(p,i) = pol0_Flx(vT);
    1723         215 :   gel(p,a) = pol1_Flx(vT); return p;
    1724             : }
    1725             : 
    1726             : GEN
    1727         194 : FlxqXQ_minpoly(GEN x, GEN S, GEN T, ulong p)
    1728             : {
    1729         194 :   pari_sp ltop = avma;
    1730             :   long vS, vT, n;
    1731             :   GEN v_x, g, tau;
    1732         194 :   vS = get_FlxqX_var(S);
    1733         194 :   vT = get_Flx_var(T);
    1734         194 :   n = get_FlxqX_degree(S);
    1735         194 :   g = pol1_FlxX(vS,vT);
    1736         194 :   tau = pol1_FlxX(vS,vT);
    1737         194 :   S = FlxqX_get_red(S, T, p);
    1738         194 :   v_x = FlxqXQ_powers(x, usqrt(2*n), S, T, p);
    1739         409 :   while(signe(tau) != 0)
    1740             :   {
    1741             :     long i, j, m, k1;
    1742             :     GEN M, v, tr;
    1743             :     GEN g_prime, c;
    1744         215 :     if (degpol(g) == n) { tau = pol1_FlxX(vS, vT); g = pol1_FlxX(vS, vT); }
    1745         215 :     v = random_FlxqX(n, vS, T, p);
    1746         215 :     tr = FlxqXQ_transmul_init(tau, S, T, p);
    1747         215 :     v = FlxqXQ_transmul(tr, v, n, T, p);
    1748         215 :     m = 2*(n-degpol(g));
    1749         215 :     k1 = usqrt(m);
    1750         215 :     tr = FlxqXQ_transmul_init(gel(v_x,k1+1), S, T, p);
    1751         215 :     c = cgetg(m+2,t_POL);
    1752         215 :     c[1] = evalsigne(1)|evalvarn(vS);
    1753         906 :     for (i=0; i<m; i+=k1)
    1754             :     {
    1755         691 :       long mj = minss(m-i, k1);
    1756        2311 :       for (j=0; j<mj; j++)
    1757        1620 :         gel(c,m+1-(i+j)) = FlxqX_dotproduct(v, gel(v_x,j+1), T, p);
    1758         691 :       v = FlxqXQ_transmul(tr, v, n, T, p);
    1759             :     }
    1760         215 :     c = FlxX_renormalize(c, m+2);
    1761             :     /* now c contains <v,x^i> , i = 0..m-1  */
    1762         215 :     M = FlxqX_halfgcd(polxn_FlxX(m, vS, vT), c, T, p);
    1763         215 :     g_prime = gmael(M, 2, 2);
    1764         215 :     if (degpol(g_prime) < 1) continue;
    1765         208 :     g = FlxqX_mul(g, g_prime, T, p);
    1766         208 :     tau = FlxqXQ_mul(tau, FlxqX_FlxqXQV_eval(g_prime, v_x, S, T, p), S, T, p);
    1767             :   }
    1768         194 :   g = FlxqX_normalize(g,T, p);
    1769         194 :   return gerepilecopy(ltop,g);
    1770             : }
    1771             : 
    1772             : GEN
    1773           0 : FlxqXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, ulong p)
    1774             : {
    1775           0 :   return FlxXV_to_FlxM(FlxqXQ_powers(y,m-1,S,T,p), n, get_Flx_var(T));
    1776             : }
    1777             : 
    1778             : static GEN
    1779      121877 : FlxX_blocks_FlxM(GEN P, long n, long m, long v)
    1780             : {
    1781      121877 :   GEN z = cgetg(m+1,t_MAT);
    1782      121877 :   long i,j, k=2, l = lg(P);
    1783      463337 :   for(i=1; i<=m; i++)
    1784             :   {
    1785      341460 :     GEN zi = cgetg(n+1,t_COL);
    1786      341460 :     gel(z,i) = zi;
    1787     1052895 :     for(j=1; j<=n; j++)
    1788      711435 :       gel(zi, j) = k==l ? pol0_Flx(v) : gel(P,k++);
    1789             :   }
    1790      121877 :   return z;
    1791             : }
    1792             : 
    1793             : GEN
    1794      121877 : FlxqX_FlxqXQV_eval(GEN Q, GEN x, GEN S, GEN T, ulong p)
    1795             : {
    1796      121877 :   pari_sp btop, av = avma;
    1797      121877 :   long v = get_FlxqX_var(S), m = get_FlxqX_degree(S);
    1798      121877 :   long vT = get_Flx_var(T);
    1799      121877 :   long i, l = lg(x)-1, lQ = lgpol(Q), n,  d;
    1800             :   GEN A, B, C, R, g;
    1801      121877 :   if (lQ == 0) return pol_0(v);
    1802      121877 :   if (lQ <= l)
    1803             :   {
    1804       40858 :     n = l;
    1805       40858 :     d = 1;
    1806             :   }
    1807             :   else
    1808             :   {
    1809       81019 :     n = l-1;
    1810       81019 :     d = (lQ+n-1)/n;
    1811             :   }
    1812      121877 :   A = FlxXV_to_FlxM_lg(x, m, n, vT);
    1813      121877 :   B = FlxX_blocks_FlxM(Q, n, d, vT);
    1814      121877 :   C = gerepileupto(av, FlxqM_mul(A, B, T, p));
    1815      121877 :   g = gel(x, l);
    1816      121877 :   T = Flx_get_red(T, p);
    1817      121877 :   S = FlxqX_get_red(S, T, p);
    1818      121877 :   btop = avma;
    1819      121877 :   R = FlxV_to_FlxX(gel(C, d), v);
    1820      341460 :   for (i = d-1; i>0; i--)
    1821             :   {
    1822      219583 :     R = FlxX_add(FlxqXQ_mul(R, g, S, T, p), FlxV_to_FlxX(gel(C,i), v), p);
    1823      219583 :     if (gc_needed(btop,1))
    1824           7 :       R = gerepileupto(btop, R);
    1825             :   }
    1826      121877 :   return gerepilecopy(av, R);
    1827             : }
    1828             : 
    1829             : GEN
    1830       64782 : FlxqX_FlxqXQ_eval(GEN Q, GEN x, GEN S, GEN T, ulong p)
    1831             : {
    1832       64782 :   pari_sp av = avma;
    1833             :   GEN z, V;
    1834       64782 :   long d = degpol(Q), rtd;
    1835       64782 :   if (d < 0) return pol_0(get_FlxqX_var(S));
    1836       64782 :   rtd = (long) sqrt((double)d);
    1837       64782 :   T = Flx_get_red(T, p);
    1838       64782 :   S = FlxqX_get_red(S, T, p);
    1839       64782 :   V = FlxqXQ_powers(x, rtd, S, T, p);
    1840       64782 :   z = FlxqX_FlxqXQV_eval(Q, V, S, T, p);
    1841       64782 :   return gerepileupto(av, z);
    1842             : }
    1843             : 
    1844             : static GEN
    1845       63399 : FlxqXQ_autpow_sqr(void * E, GEN x)
    1846             : {
    1847       63399 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    1848       63399 :   GEN S = D->S, T = D->T;
    1849       63399 :   ulong p = D->p;
    1850       63399 :   GEN phi = gel(x,1), S1 = gel(x,2);
    1851       63399 :   long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(S1)+1,1);
    1852       63399 :   GEN V = Flxq_powers(phi, n, T, p);
    1853       63399 :   GEN phi2 = Flx_FlxqV_eval(phi, V, T, p);
    1854       63399 :   GEN Sphi = FlxY_FlxqV_evalx(S1, V, T, p);
    1855       63399 :   GEN S2 = FlxqX_FlxqXQ_eval(Sphi, S1, S, T, p);
    1856       63399 :   return mkvec2(phi2, S2);
    1857             : }
    1858             : 
    1859             : static GEN
    1860        1161 : FlxqXQ_autpow_mul(void * E, GEN x, GEN y)
    1861             : {
    1862        1161 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    1863        1161 :   GEN S = D->S, T = D->T;
    1864        1161 :   ulong p = D->p;
    1865        1161 :   GEN phi1 = gel(x,1), S1 = gel(x,2);
    1866        1161 :   GEN phi2 = gel(y,1), S2 = gel(y,2);
    1867        1161 :   long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(S1)+1,1);
    1868        1161 :   GEN V = Flxq_powers(phi2, n, T, p);
    1869        1161 :   GEN phi3 = Flx_FlxqV_eval(phi1, V, T, p);
    1870        1161 :   GEN Sphi = FlxY_FlxqV_evalx(S1, V, T, p);
    1871        1161 :   GEN S3 = FlxqX_FlxqXQ_eval(Sphi, S2, S, T, p);
    1872        1161 :   return mkvec2(phi3, S3);
    1873             : }
    1874             : 
    1875             : GEN
    1876       61845 : FlxqXQ_autpow(GEN aut, long n, GEN S, GEN T, ulong p)
    1877             : {
    1878       61845 :   pari_sp av = avma;
    1879             :   struct _FlxqXQ D;
    1880       61845 :   T = Flx_get_red(T, p);
    1881       61845 :   S = FlxqX_get_red(S, T, p);
    1882       61845 :   D.S=S; D.T=T; D.p=p;
    1883       61845 :   aut = gen_powu_i(aut,n,&D,FlxqXQ_autpow_sqr,FlxqXQ_autpow_mul);
    1884       61845 :   return gerepilecopy(av, aut);
    1885             : }
    1886             : 
    1887             : static GEN
    1888       28146 : FlxqXQ_autsum_mul(void *E, GEN x, GEN y)
    1889             : {
    1890       28146 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    1891       28146 :   GEN S = D->S, T = D->T;
    1892       28146 :   ulong p = D->p;
    1893       28146 :   GEN phi1 = gel(x,1), S1 = gel(x,2), a1 = gel(x,3);
    1894       28146 :   GEN phi2 = gel(y,1), S2 = gel(y,2), a2 = gel(y,3);
    1895       28146 :   long n2 = brent_kung_optpow(get_Flx_degree(T)-1, lgpol(S1)+lgpol(a1)+1,1);
    1896       28146 :   GEN V2 = Flxq_powers(phi2, n2, T, p);
    1897       28146 :   GEN phi3 = Flx_FlxqV_eval(phi1, V2, T, p);
    1898       28146 :   GEN Sphi = FlxY_FlxqV_evalx(S1, V2, T, p);
    1899       28146 :   GEN aphi = FlxY_FlxqV_evalx(a1, V2, T, p);
    1900       28146 :   long n = brent_kung_optpow(maxss(degpol(Sphi),degpol(aphi)),2,1);
    1901       28146 :   GEN V = FlxqXQ_powers(S2, n, S, T, p);
    1902       28146 :   GEN S3 = FlxqX_FlxqXQV_eval(Sphi, V, S, T, p);
    1903       28146 :   GEN aS = FlxqX_FlxqXQV_eval(aphi, V, S, T, p);
    1904       28146 :   GEN a3 = FlxqXQ_mul(aS, a2, S, T, p);
    1905       28146 :   return mkvec3(phi3, S3, a3);
    1906             : }
    1907             : 
    1908             : static GEN
    1909       16864 : FlxqXQ_autsum_sqr(void * T, GEN x)
    1910       16864 : { return FlxqXQ_autsum_mul(T, x, x); }
    1911             : 
    1912             : GEN
    1913       10757 : FlxqXQ_autsum(GEN aut, long n, GEN S, GEN T, ulong p)
    1914             : {
    1915       10757 :   pari_sp av = avma;
    1916             :   struct _FlxqXQ D;
    1917       10757 :   T = Flx_get_red(T, p);
    1918       10757 :   S = FlxqX_get_red(S, T, p);
    1919       10757 :   D.S=S; D.T=T; D.p=p;
    1920       10757 :   aut = gen_powu_i(aut,n,&D,FlxqXQ_autsum_sqr,FlxqXQ_autsum_mul);
    1921       10757 :   return gerepilecopy(av, aut);
    1922             : }
    1923             : 
    1924             : static GEN
    1925          27 : FlxqXQ_auttrace_mul(void *E, GEN x, GEN y)
    1926             : {
    1927          27 :   struct _FlxqXQ *D = (struct _FlxqXQ *)E;
    1928          27 :   GEN S = D->S, T = D->T;
    1929          27 :   ulong p = D->p;
    1930          27 :   GEN S1 = gel(x,1), a1 = gel(x,2);
    1931          27 :   GEN S2 = gel(y,1), a2 = gel(y,2);
    1932          27 :   long n = brent_kung_optpow(maxss(degpol(S1),degpol(a1)),2,1);
    1933          27 :   GEN V = FlxqXQ_powers(S2, n, S, T, p);
    1934          27 :   GEN S3 = FlxqX_FlxqXQV_eval(S1, V, S, T, p);
    1935          27 :   GEN aS = FlxqX_FlxqXQV_eval(a1, V, S, T, p);
    1936          27 :   GEN a3 = FlxX_add(aS, a2, p);
    1937          27 :   return mkvec2(S3, a3);
    1938             : }
    1939             : 
    1940             : static GEN
    1941          20 : FlxqXQ_auttrace_sqr(void *E, GEN x)
    1942          20 : { return FlxqXQ_auttrace_mul(E, x, x); }
    1943             : 
    1944             : GEN
    1945         314 : FlxqXQ_auttrace(GEN x, ulong n, GEN S, GEN T, ulong p)
    1946             : {
    1947         314 :   pari_sp av = avma;
    1948             :   struct _FlxqXQ D;
    1949         314 :   T = Flx_get_red(T, p);
    1950         314 :   S = FlxqX_get_red(S, T, p);
    1951         314 :   D.S=S; D.T=T; D.p=p;
    1952         314 :   x = gen_powu_i(x,n,(void*)&D,FlxqXQ_auttrace_sqr,FlxqXQ_auttrace_mul);
    1953         314 :   return gerepilecopy(av, x);
    1954             : }
    1955             : 
    1956             : /*******************************************************************/
    1957             : /*                                                                 */
    1958             : /*                      FlxYqQ                                     */
    1959             : /*                                                                 */
    1960             : /*******************************************************************/
    1961             : 
    1962             : /*Preliminary implementation to speed up FpX_ffisom*/
    1963             : typedef struct {
    1964             :   GEN S, T;
    1965             :   ulong p;
    1966             : } FlxYqq_muldata;
    1967             : 
    1968             : /* reduce x in Fl[X, Y] in the algebra Fl[X, Y]/ (P(X),Q(Y)) */
    1969             : static GEN
    1970       11260 : FlxYqq_redswap(GEN x, GEN S, GEN T, ulong p)
    1971             : {
    1972       11260 :   pari_sp ltop=avma;
    1973       11260 :   long n = get_Flx_degree(S);
    1974       11260 :   long m = get_Flx_degree(T);
    1975       11260 :   long w = get_Flx_var(T);
    1976       11260 :   GEN V = FlxX_swap(x,m,w);
    1977       11260 :   V = FlxqX_red(V,S,p);
    1978       11260 :   V = FlxX_swap(V,n,w);
    1979       11260 :   return gerepilecopy(ltop,V);
    1980             : }
    1981             : static GEN
    1982        7954 : FlxYqq_sqr(void *data, GEN x)
    1983             : {
    1984        7954 :   FlxYqq_muldata *D = (FlxYqq_muldata*)data;
    1985        7954 :   return FlxYqq_redswap(FlxqX_sqr(x, D->T, D->p),D->S,D->T,D->p);
    1986             : }
    1987             : 
    1988             : static GEN
    1989        3306 : FlxYqq_mul(void *data, GEN x, GEN y)
    1990             : {
    1991        3306 :   FlxYqq_muldata *D = (FlxYqq_muldata*)data;
    1992        3306 :   return FlxYqq_redswap(FlxqX_mul(x,y, D->T, D->p),D->S,D->T,D->p);
    1993             : }
    1994             : 
    1995             : /* x in Z[X,Y], S in Z[X] over Fq = Z[Y]/(p,T); compute lift(x^n mod (S,T,p)) */
    1996             : GEN
    1997        4202 : FlxYqq_pow(GEN x, GEN n, GEN S, GEN T, ulong p)
    1998             : {
    1999             :   FlxYqq_muldata D;
    2000        4202 :   D.S = S;
    2001        4202 :   D.T = T;
    2002        4202 :   D.p = p;
    2003        4202 :   return gen_pow(x, n, (void*)&D, &FlxYqq_sqr, &FlxYqq_mul);
    2004             : }
    2005             : 
    2006             : /*******************************************************************/
    2007             : /*                                                                 */
    2008             : /*                      FlxqXn                                     */
    2009             : /*                                                                 */
    2010             : /*******************************************************************/
    2011             : 
    2012             : GEN
    2013       41307 : FlxXn_red(GEN a, long n)
    2014             : {
    2015       41307 :   long i, L = n+2, l = lg(a);
    2016             :   GEN  b;
    2017       41307 :   if (L >= l) return a; /* deg(x) < n */
    2018       21113 :   b = cgetg(L, t_POL); b[1] = a[1];
    2019      214165 :   for (i=2; i<L; i++) gel(b,i) = gel(a,i);
    2020       21110 :   return FlxX_renormalize(b,L);
    2021             : }
    2022             : 
    2023             : GEN
    2024       28003 : FlxqXn_mul(GEN a, GEN b, long n, GEN T, ulong p)
    2025       28003 : { return FlxXn_red(FlxqX_mul(a, b, T, p), n); }
    2026             : 
    2027             : GEN
    2028           0 : FlxqXn_sqr(GEN a, long n, GEN T, ulong p)
    2029           0 : { return FlxXn_red(FlxqX_sqr(a, T, p), n); }
    2030             : 
    2031             : /* (f*g) \/ x^n */
    2032             : static GEN
    2033       12506 : FlxqX_mulhigh_i(GEN f, GEN g, long n, GEN T, ulong p)
    2034             : {
    2035       12506 :   return FlxX_shift(FlxqX_mul(f, g, T, p), -n , get_Flx_var(T));
    2036             : }
    2037             : 
    2038             : static GEN
    2039        9181 : FlxqXn_mulhigh(GEN f, GEN g, long n2, long n, GEN T, ulong p)
    2040             : {
    2041        9181 :   long vT = get_Flx_var(T);
    2042        9181 :   GEN F = FlxX_blocks(f, n2, 2, vT), fl = gel(F,1), fh = gel(F,2);
    2043        9181 :   return FlxX_add(FlxqX_mulhigh_i(fl, g, n2, T, p),
    2044             :                   FlxqXn_mul(fh, g, n - n2, T, p), p);
    2045             : }
    2046             : 
    2047             : GEN
    2048        1592 : FlxqXn_inv(GEN f, long e, GEN T, ulong p)
    2049             : {
    2050        1592 :   pari_sp av = avma, av2;
    2051             :   ulong mask;
    2052             :   GEN W, a;
    2053        1592 :   long v = varn(f), n = 1, vT = get_Flx_var(T);
    2054             : 
    2055        1592 :   if (!signe(f)) pari_err_INV("FlxqXn_inv",f);
    2056        1592 :   a = Flxq_inv(gel(f,2), T, p);
    2057        1592 :   if (e == 1) return scalarpol(a, v);
    2058        1592 :   else if (e == 2)
    2059             :   {
    2060             :     GEN b;
    2061           0 :     if (degpol(f) <= 0) return scalarpol(a, v);
    2062           0 :     b = Flx_neg(gel(f,3), p);
    2063           0 :     if (lgpol(b)==0) return scalarpol(a, v);
    2064           0 :     b = Flxq_mul(b, Flxq_sqr(a, T, p), T, p);
    2065           0 :     W = deg1pol_shallow(b, a, v);
    2066           0 :     return gerepilecopy(av, W);
    2067             :   }
    2068        1592 :   W = scalarpol_shallow(Flxq_inv(gel(f,2), T, p),v);
    2069        1592 :   mask = quadratic_prec_mask(e);
    2070        1592 :   av2 = avma;
    2071        8244 :   for (;mask>1;)
    2072             :   {
    2073             :     GEN u, fr;
    2074        6652 :     long n2 = n;
    2075        6652 :     n<<=1; if (mask & 1) n--;
    2076        6652 :     mask >>= 1;
    2077        6652 :     fr = FlxXn_red(f, n);
    2078        6651 :     u = FlxqXn_mul(W, FlxqXn_mulhigh(fr, W, n2, n, T, p), n-n2, T, p);
    2079        6651 :     W = FlxX_sub(W, FlxX_shift(u, n2, vT), p);
    2080        6652 :     if (gc_needed(av2,2))
    2081             :     {
    2082           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"FlxqXn_inv, e = %ld", n);
    2083           0 :       W = gerepileupto(av2, W);
    2084             :     }
    2085             :   }
    2086        1592 :   return gerepileupto(av, W);
    2087             : }
    2088             : 
    2089             : /* Compute intformal(x^n*S)/x^(n+1) */
    2090             : static GEN
    2091        3326 : FlxX_integXn(GEN x, long n, ulong p)
    2092             : {
    2093        3326 :   long i, lx = lg(x);
    2094             :   GEN y;
    2095        3326 :   if (lx == 2) return gcopy(x);
    2096        2755 :   y = cgetg(lx, t_POL); y[1] = x[1];
    2097       22172 :   for (i=2; i<lx; i++)
    2098             :   {
    2099       19417 :     GEN xi = gel(x,i);
    2100       19417 :     gel(y,i) = Flx_Fl_mul(xi, Fl_inv((n+i-1)%p, p), p);
    2101             :   }
    2102        2755 :   return FlxX_renormalize(y, lx);;
    2103             : }
    2104             : 
    2105             : GEN
    2106         796 : FlxqXn_expint(GEN h, long e, GEN T, ulong p)
    2107             : {
    2108         796 :   pari_sp av = avma, av2;
    2109         796 :   long v = varn(h), n = 1, vT = get_Flx_var(T);
    2110         796 :   GEN f = pol1_FlxX(v, vT), g = pol1_FlxX(v, vT);
    2111         796 :   ulong mask = quadratic_prec_mask(e);
    2112         796 :   av2 = avma;
    2113        3326 :   for (;mask>1;)
    2114             :   {
    2115             :     GEN u, w;
    2116        3326 :     long n2 = n;
    2117        3326 :     n<<=1; if (mask & 1) n--;
    2118        3326 :     mask >>= 1;
    2119        3326 :     u = FlxqXn_mul(g, FlxqX_mulhigh_i(f, FlxXn_red(h, n2-1), n2-1, T, p), n-n2, T, p);
    2120        3326 :     u = FlxX_add(u, FlxX_shift(FlxXn_red(h, n-1), 1-n2, vT), p);
    2121        3326 :     w = FlxqXn_mul(f, FlxX_integXn(u, n2-1, p), n-n2, T, p);
    2122        3326 :     f = FlxX_add(f, FlxX_shift(w, n2, vT), p);
    2123        3326 :     if (mask<=1) break;
    2124        2530 :     u = FlxqXn_mul(g, FlxqXn_mulhigh(f, g, n2, n, T, p), n-n2, T, p);
    2125        2530 :     g = FlxX_sub(g, FlxX_shift(u, n2, vT), p);
    2126        2530 :     if (gc_needed(av2,2))
    2127             :     {
    2128           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FlxqXn_exp, e = %ld", n);
    2129           0 :       gerepileall(av2, 2, &f, &g);
    2130             :     }
    2131             :   }
    2132         796 :   return gerepileupto(av, f);
    2133             : }

Generated by: LCOV version 1.13