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 - QX_factor.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 25406-bf255ab81b) Lines: 768 797 96.4 %
Date: 2020-06-04 05:59:24 Functions: 46 47 97.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : #include "pari.h"
      14             : #include "paripriv.h"
      15             : 
      16             : /* x,y two ZX, y non constant. Return q = x/y if y divides x in Z[X] and NULL
      17             :  * otherwise. If not NULL, B is a t_INT upper bound for ||q||_oo. */
      18             : static GEN
      19     6305266 : ZX_divides_i(GEN x, GEN y, GEN B)
      20             : {
      21             :   long dx, dy, dz, i, j;
      22             :   pari_sp av;
      23             :   GEN z,p1,y_lead;
      24             : 
      25     6305266 :   dy=degpol(y);
      26     6305266 :   dx=degpol(x);
      27     6305266 :   dz=dx-dy; if (dz<0) return NULL;
      28     6304797 :   z=cgetg(dz+3,t_POL); z[1] = x[1];
      29     6304797 :   x += 2; y += 2; z += 2;
      30     6304797 :   y_lead = gel(y,dy);
      31     6304797 :   if (equali1(y_lead)) y_lead = NULL;
      32             : 
      33     6304797 :   p1 = gel(x,dx);
      34     6304797 :   if (y_lead) {
      35             :     GEN r;
      36       12917 :     p1 = dvmdii(p1,y_lead, &r);
      37       12917 :     if (r != gen_0) return NULL;
      38             :   }
      39     6291880 :   else p1 = icopy(p1);
      40     6302034 :   gel(z,dz) = p1;
      41     7139527 :   for (i=dx-1; i>=dy; i--)
      42             :   {
      43      839201 :     av = avma; p1 = gel(x,i);
      44     2950738 :     for (j=i-dy+1; j<=i && j<=dz; j++)
      45     2111537 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
      46      839201 :     if (y_lead) {
      47             :       GEN r;
      48       29036 :       p1 = dvmdii(p1,y_lead, &r);
      49       29036 :       if (r != gen_0) return NULL;
      50             :     }
      51      838004 :     if (B && abscmpii(p1, B) > 0) return NULL;
      52      837493 :     p1 = gerepileuptoint(av, p1);
      53      837493 :     gel(z,i-dy) = p1;
      54             :   }
      55     6300326 :   av = avma;
      56    15950900 :   for (; i >= 0; i--)
      57             :   {
      58     9688550 :     p1 = gel(x,i);
      59             :     /* we always enter this loop at least once */
      60    20871636 :     for (j=0; j<=i && j<=dz; j++)
      61    11183086 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
      62     9688550 :     if (signe(p1)) return NULL;
      63     9650574 :     set_avma(av);
      64             :   }
      65     6262350 :   return z - 2;
      66             : }
      67             : static GEN
      68     6300499 : ZX_divides(GEN x, GEN y) { return ZX_divides_i(x,y,NULL); }
      69             : 
      70             : #if 0
      71             : /* cf Beauzamy et al: upper bound for
      72             :  *      lc(x) * [2^(5/8) / pi^(3/8)] e^(1/4n) 2^(n/2) sqrt([x]_2)/ n^(3/8)
      73             :  * where [x]_2 = sqrt(\sum_i=0^n x[i]^2 / binomial(n,i)). One factor has
      74             :  * all coeffs less than then bound */
      75             : static GEN
      76             : two_factor_bound(GEN x)
      77             : {
      78             :   long i, j, n = lg(x) - 3;
      79             :   pari_sp av = avma;
      80             :   GEN *invbin, c, r = cgetr(3), z;
      81             : 
      82             :   x += 2; invbin = (GEN*)new_chunk(n+1);
      83             :   z = real_1(LOWDEFAULTPREC); /* invbin[i] = 1 / binomial(n, i) */
      84             :   for (i=0,j=n; j >= i; i++,j--)
      85             :   {
      86             :     invbin[i] = invbin[j] = z;
      87             :     z = divru(mulru(z, i+1), n-i);
      88             :   }
      89             :   z = invbin[0]; /* = 1 */
      90             :   for (i=0; i<=n; i++)
      91             :   {
      92             :     c = gel(x,i); if (!signe(c)) continue;
      93             :     affir(c, r);
      94             :     z = addrr(z, mulrr(sqrr(r), invbin[i]));
      95             :   }
      96             :   z = shiftr(sqrtr(z), n);
      97             :   z = divrr(z, dbltor(pow((double)n, 0.75)));
      98             :   z = roundr_safe(sqrtr(z));
      99             :   z = mulii(z, absi_shallow(gel(x,n)));
     100             :   return gerepileuptoint(av, shifti(z, 1));
     101             : }
     102             : #endif
     103             : 
     104             : /* A | S ==> |a_i| <= binom(d-1, i-1) || S ||_2 + binom(d-1, i) lc(S) */
     105             : static GEN
     106       10913 : Mignotte_bound(GEN S)
     107             : {
     108       10913 :   long i, d = degpol(S);
     109       10913 :   GEN C, N2, t, binlS, lS = leading_coeff(S), bin = vecbinomial(d-1);
     110             : 
     111       10913 :   N2 = sqrtr(RgX_fpnorml2(S,DEFAULTPREC));
     112       10913 :   binlS = is_pm1(lS)? bin: ZC_Z_mul(bin, lS);
     113             : 
     114             :   /* i = 0 */
     115       10913 :   C = gel(binlS,1);
     116             :   /* i = d */
     117       10913 :   t = N2; if (gcmp(C, t) < 0) C = t;
     118      114884 :   for (i = 1; i < d; i++)
     119             :   {
     120      103971 :     t = addri(mulir(gel(bin,i), N2), gel(binlS,i+1));
     121      103971 :     if (mpcmp(C, t) < 0) C = t;
     122             :   }
     123       10913 :   return C;
     124             : }
     125             : /* A | S ==> |a_i|^2 <= 3^{3/2 + d} / (4 \pi d) [P]_2^2,
     126             :  * where [P]_2 is Bombieri's 2-norm */
     127             : static GEN
     128       10913 : Beauzamy_bound(GEN S)
     129             : {
     130       10913 :   const long prec = DEFAULTPREC;
     131       10913 :   long i, d = degpol(S);
     132             :   GEN bin, lS, s, C;
     133       10913 :   bin = vecbinomial(d);
     134             : 
     135       10913 :   s = real_0(prec);
     136      136710 :   for (i=0; i<=d; i++)
     137             :   {
     138      125797 :     GEN c = gel(S,i+2);
     139      125797 :     if (gequal0(c)) continue;
     140             :     /* s += P_i^2 / binomial(d,i) */
     141      101262 :     s = addrr(s, divri(itor(sqri(c), prec), gel(bin,i+1)));
     142             :   }
     143             :   /* s = [S]_2^2 */
     144       10913 :   C = powruhalf(utor(3,prec), 3 + 2*d); /* 3^{3/2 + d} */
     145       10913 :   C = divrr(mulrr(C, s), mulur(4*d, mppi(prec)));
     146       10913 :   lS = absi_shallow(leading_coeff(S));
     147       10913 :   return mulir(lS, sqrtr(C));
     148             : }
     149             : 
     150             : static GEN
     151       10913 : factor_bound(GEN S)
     152             : {
     153       10913 :   pari_sp av = avma;
     154       10913 :   GEN a = Mignotte_bound(S);
     155       10913 :   GEN b = Beauzamy_bound(S);
     156       10913 :   if (DEBUGLEVEL>2)
     157             :   {
     158           0 :     err_printf("Mignotte bound: %Ps\n",a);
     159           0 :     err_printf("Beauzamy bound: %Ps\n",b);
     160             :   }
     161       10913 :   return gerepileupto(av, ceil_safe(gmin_shallow(a, b)));
     162             : }
     163             : 
     164             : /* Naive recombination of modular factors: combine up to maxK modular
     165             :  * factors, degree <= klim
     166             :  *
     167             :  * target = polynomial we want to factor
     168             :  * famod = array of modular factors.  Product should be congruent to
     169             :  * target/lc(target) modulo p^a
     170             :  * For true factors: S1,S2 <= p^b, with b <= a and p^(b-a) < 2^31 */
     171             : static GEN
     172        9310 : cmbf(GEN pol, GEN famod, GEN bound, GEN p, long a, long b,
     173             :      long klim, long *pmaxK, int *done)
     174             : {
     175        9310 :   long K = 1, cnt = 1, i,j,k, curdeg, lfamod = lg(famod)-1;
     176             :   ulong spa_b, spa_bs2, Sbound;
     177        9310 :   GEN lc, lcpol, pa = powiu(p,a), pas2 = shifti(pa,-1);
     178        9310 :   GEN trace1   = cgetg(lfamod+1, t_VECSMALL);
     179        9310 :   GEN trace2   = cgetg(lfamod+1, t_VECSMALL);
     180        9310 :   GEN ind      = cgetg(lfamod+1, t_VECSMALL);
     181        9310 :   GEN deg      = cgetg(lfamod+1, t_VECSMALL);
     182        9310 :   GEN degsofar = cgetg(lfamod+1, t_VECSMALL);
     183        9310 :   GEN listmod  = cgetg(lfamod+1, t_VEC);
     184        9310 :   GEN fa       = cgetg(lfamod+1, t_VEC);
     185             : 
     186        9310 :   *pmaxK = cmbf_maxK(lfamod);
     187        9310 :   lc = absi_shallow(leading_coeff(pol));
     188        9310 :   if (equali1(lc)) lc = NULL;
     189        9310 :   lcpol = lc? ZX_Z_mul(pol, lc): pol;
     190             : 
     191             :   {
     192        9310 :     GEN pa_b,pa_bs2,pb, lc2 = lc? sqri(lc): NULL;
     193             : 
     194        9310 :     pa_b = powiu(p, a-b); /* < 2^31 */
     195        9310 :     pa_bs2 = shifti(pa_b,-1);
     196        9310 :     pb= powiu(p, b);
     197       34538 :     for (i=1; i <= lfamod; i++)
     198             :     {
     199       25228 :       GEN T1,T2, P = gel(famod,i);
     200       25228 :       long d = degpol(P);
     201             : 
     202       25228 :       deg[i] = d; P += 2;
     203       25228 :       T1 = gel(P,d-1);/* = - S_1 */
     204       25228 :       T2 = sqri(T1);
     205       25228 :       if (d > 1) T2 = subii(T2, shifti(gel(P,d-2),1));
     206       25228 :       T2 = modii(T2, pa); /* = S_2 Newton sum */
     207       25228 :       if (lc)
     208             :       {
     209        1239 :         T1 = Fp_mul(lc, T1, pa);
     210        1239 :         T2 = Fp_mul(lc2,T2, pa);
     211             :       }
     212       25228 :       uel(trace1,i) = itou(diviiround(T1, pb));
     213       25228 :       uel(trace2,i) = itou(diviiround(T2, pb));
     214             :     }
     215        9310 :     spa_b   = uel(pa_b,2); /* < 2^31 */
     216        9310 :     spa_bs2 = uel(pa_bs2,2); /* < 2^31 */
     217             :   }
     218        9310 :   degsofar[0] = 0; /* sentinel */
     219             : 
     220             :   /* ind runs through strictly increasing sequences of length K,
     221             :    * 1 <= ind[i] <= lfamod */
     222       17584 : nextK:
     223       17584 :   if (K > *pmaxK || 2*K > lfamod) goto END;
     224       10801 :   if (DEBUGLEVEL > 3)
     225           0 :     err_printf("\n### K = %d, %Ps combinations\n", K,binomial(utoipos(lfamod), K));
     226       10801 :   setlg(ind, K+1); ind[1] = 1;
     227       10801 :   Sbound = (ulong) ((K+1)>>1);
     228       10801 :   i = 1; curdeg = deg[ind[1]];
     229             :   for(;;)
     230             :   { /* try all combinations of K factors */
     231      443310 :     for (j = i; j < K; j++)
     232             :     {
     233       56595 :       degsofar[j] = curdeg;
     234       56595 :       ind[j+1] = ind[j]+1; curdeg += deg[ind[j+1]];
     235             :     }
     236      386715 :     if (curdeg <= klim) /* trial divide */
     237             :     {
     238             :       GEN y, q, list;
     239             :       pari_sp av;
     240             :       ulong t;
     241             : 
     242             :       /* d - 1 test */
     243     1110921 :       for (t=uel(trace1,ind[1]),i=2; i<=K; i++)
     244      724206 :         t = Fl_add(t, uel(trace1,ind[i]), spa_b);
     245      386715 :       if (t > spa_bs2) t = spa_b - t;
     246      386715 :       if (t > Sbound)
     247             :       {
     248      346003 :         if (DEBUGLEVEL>6) err_printf(".");
     249      346003 :         goto NEXT;
     250             :       }
     251             :       /* d - 2 test */
     252      121233 :       for (t=uel(trace2,ind[1]),i=2; i<=K; i++)
     253       80521 :         t = Fl_add(t, uel(trace2,ind[i]), spa_b);
     254       40712 :       if (t > spa_bs2) t = spa_b - t;
     255       40712 :       if (t > Sbound)
     256             :       {
     257       24801 :         if (DEBUGLEVEL>6) err_printf("|");
     258       24801 :         goto NEXT;
     259             :       }
     260             : 
     261       15911 :       av = avma;
     262             :       /* check trailing coeff */
     263       15911 :       y = lc;
     264       62601 :       for (i=1; i<=K; i++)
     265             :       {
     266       46690 :         GEN q = constant_coeff(gel(famod,ind[i]));
     267       46690 :         if (y) q = mulii(y, q);
     268       46690 :         y = centermodii(q, pa, pas2);
     269             :       }
     270       15911 :       if (!signe(y) || !dvdii(constant_coeff(lcpol), y))
     271             :       {
     272       11305 :         if (DEBUGLEVEL>3) err_printf("T");
     273       11305 :         set_avma(av); goto NEXT;
     274             :       }
     275        4606 :       y = lc; /* full computation */
     276        9989 :       for (i=1; i<=K; i++)
     277             :       {
     278        5383 :         GEN q = gel(famod,ind[i]);
     279        5383 :         if (y) q = gmul(y, q);
     280        5383 :         y = centermod_i(q, pa, pas2);
     281             :       }
     282             : 
     283             :       /* y is the candidate factor */
     284        4606 :       if (! (q = ZX_divides_i(lcpol,y,bound)) )
     285             :       {
     286         483 :         if (DEBUGLEVEL>3) err_printf("*");
     287         483 :         set_avma(av); goto NEXT;
     288             :       }
     289             :       /* found a factor */
     290        4123 :       list = cgetg(K+1, t_VEC);
     291        4123 :       gel(listmod,cnt) = list;
     292        8575 :       for (i=1; i<=K; i++) list[i] = famod[ind[i]];
     293             : 
     294        4123 :       y = Q_primpart(y);
     295        4123 :       gel(fa,cnt++) = y;
     296             :       /* fix up pol */
     297        4123 :       pol = q;
     298        4123 :       if (lc) pol = Q_div_to_int(pol, leading_coeff(y));
     299       16737 :       for (i=j=k=1; i <= lfamod; i++)
     300             :       { /* remove used factors */
     301       12614 :         if (j <= K && i == ind[j]) j++;
     302             :         else
     303             :         {
     304        8162 :           gel(famod,k) = gel(famod,i);
     305        8162 :           uel(trace1,k) = uel(trace1,i);
     306        8162 :           uel(trace2,k) = uel(trace2,i);
     307        8162 :           deg[k] = deg[i]; k++;
     308             :         }
     309             :       }
     310        4123 :       lfamod -= K;
     311        4123 :       *pmaxK = cmbf_maxK(lfamod);
     312        4123 :       if (lfamod < 2*K) goto END;
     313        1596 :       i = 1; curdeg = deg[ind[1]];
     314        1596 :       bound = factor_bound(pol);
     315        1596 :       if (lc) lc = absi_shallow(leading_coeff(pol));
     316        1596 :       lcpol = lc? ZX_Z_mul(pol, lc): pol;
     317        1596 :       if (DEBUGLEVEL>3)
     318           0 :         err_printf("\nfound factor %Ps\nremaining modular factor(s): %ld\n",
     319             :                    y, lfamod);
     320        1596 :       continue;
     321             :     }
     322             : 
     323           0 : NEXT:
     324      382592 :     for (i = K+1;;)
     325             :     {
     326      447132 :       if (--i == 0) { K++; goto nextK; }
     327      438858 :       if (++ind[i] <= lfamod - K + i)
     328             :       {
     329      374318 :         curdeg = degsofar[i-1] + deg[ind[i]];
     330      374318 :         if (curdeg <= klim) break;
     331             :       }
     332             :     }
     333             :   }
     334        9310 : END:
     335        9310 :   *done = 1;
     336        9310 :   if (degpol(pol) > 0)
     337             :   { /* leftover factor */
     338        9310 :     if (signe(leading_coeff(pol)) < 0) pol = ZX_neg(pol);
     339        9310 :     if (lfamod >= 2*K) *done = 0;
     340             : 
     341        9310 :     setlg(famod, lfamod+1);
     342        9310 :     gel(listmod,cnt) = leafcopy(famod);
     343        9310 :     gel(fa,cnt++) = pol;
     344             :   }
     345        9310 :   if (DEBUGLEVEL>6) err_printf("\n");
     346        9310 :   setlg(listmod, cnt);
     347        9310 :   setlg(fa, cnt); return mkvec2(fa, listmod);
     348             : }
     349             : 
     350             : /* recombination of modular factors: van Hoeij's algorithm */
     351             : 
     352             : /* Q in Z[X], return Q(2^n) */
     353             : static GEN
     354       88277 : shifteval(GEN Q, long n)
     355             : {
     356       88277 :   long i, l = lg(Q);
     357             :   GEN s;
     358             : 
     359       88277 :   if (!signe(Q)) return gen_0;
     360       88277 :   s = gel(Q,l-1);
     361      385351 :   for (i = l-2; i > 1; i--) s = addii(gel(Q,i), shifti(s, n));
     362       88277 :   return s;
     363             : }
     364             : 
     365             : /* return integer y such that all |a| <= y if P(a) = 0 */
     366             : static GEN
     367       52496 : root_bound(GEN P0)
     368             : {
     369       52496 :   GEN Q = leafcopy(P0), lP = absi_shallow(leading_coeff(Q)), x,y,z;
     370       52496 :   long k, d = degpol(Q);
     371             : 
     372             :   /* P0 = lP x^d + Q, deg Q < d */
     373       52496 :   Q = normalizepol_lg(Q, d+2);
     374      279352 :   for (k=lg(Q)-1; k>1; k--) gel(Q,k) = absi_shallow(gel(Q,k));
     375       52496 :   k = (long)(fujiwara_bound(P0));
     376       88557 :   for (  ; k >= 0; k--)
     377             :   {
     378       88277 :     pari_sp av = avma;
     379             :     /* y = 2^k; Q(y) >= lP y^d ? */
     380       88277 :     if (cmpii(shifteval(Q,k), shifti(lP, d*k)) >= 0) break;
     381       36061 :     set_avma(av);
     382             :   }
     383       52496 :   if (k < 0) k = 0;
     384       52496 :   x = int2n(k);
     385       52496 :   y = int2n(k+1);
     386       52496 :   for(k=0; ; k++)
     387             :   {
     388      315342 :     z = shifti(addii(x,y), -1);
     389      315342 :     if (equalii(x,z) || k > 5) break;
     390      262846 :     if (cmpii(poleval(Q,z), mulii(lP, powiu(z, d))) < 0)
     391      140585 :       y = z;
     392             :     else
     393      122261 :       x = z;
     394             :   }
     395       52496 :   return y;
     396             : }
     397             : 
     398             : GEN
     399         301 : chk_factors_get(GEN lt, GEN famod, GEN c, GEN T, GEN N)
     400             : {
     401         301 :   long i = 1, j, l = lg(famod);
     402         301 :   GEN V = cgetg(l, t_VEC);
     403        7805 :   for (j = 1; j < l; j++)
     404        7504 :     if (signe(gel(c,j))) gel(V,i++) = gel(famod,j);
     405         301 :   if (lt && i > 1) gel(V,1) = RgX_Rg_mul(gel(V,1), lt);
     406         301 :   setlg(V, i);
     407         301 :   return T? FpXQXV_prod(V, T, N): FpXV_prod(V,N);
     408             : }
     409             : 
     410             : static GEN
     411         140 : chk_factors(GEN P, GEN M_L, GEN bound, GEN famod, GEN pa)
     412             : {
     413             :   long i, r;
     414         140 :   GEN pol = P, list, piv, y, ltpol, lt, paov2;
     415             : 
     416         140 :   piv = ZM_hnf_knapsack(M_L);
     417         140 :   if (!piv) return NULL;
     418          70 :   if (DEBUGLEVEL>7) err_printf("ZM_hnf_knapsack output:\n%Ps\n",piv);
     419             : 
     420          70 :   r  = lg(piv)-1;
     421          70 :   list = cgetg(r+1, t_VEC);
     422          70 :   lt = absi_shallow(leading_coeff(pol));
     423          70 :   if (equali1(lt)) lt = NULL;
     424          70 :   ltpol = lt? ZX_Z_mul(pol, lt): pol;
     425          70 :   paov2 = shifti(pa,-1);
     426          70 :   for (i = 1;;)
     427             :   {
     428         161 :     if (DEBUGLEVEL) err_printf("LLL_cmbf: checking factor %ld\n",i);
     429         161 :     y = chk_factors_get(lt, famod, gel(piv,i), NULL, pa);
     430         161 :     y = FpX_center_i(y, pa, paov2);
     431         161 :     if (! (pol = ZX_divides_i(ltpol,y,bound)) ) return NULL;
     432         133 :     if (lt) y = Q_primpart(y);
     433         133 :     gel(list,i) = y;
     434         133 :     if (++i >= r) break;
     435             : 
     436          91 :     if (lt)
     437             :     {
     438          35 :       pol = ZX_Z_divexact(pol, leading_coeff(y));
     439          35 :       lt = absi_shallow(leading_coeff(pol));
     440          35 :       ltpol = ZX_Z_mul(pol, lt);
     441             :     }
     442             :     else
     443          56 :       ltpol = pol;
     444             :   }
     445          42 :   y = Q_primpart(pol);
     446          42 :   gel(list,i) = y; return list;
     447             : }
     448             : 
     449             : GEN
     450        1295 : LLL_check_progress(GEN Bnorm, long n0, GEN m, int final, long *ti_LLL)
     451             : {
     452             :   GEN norm, u;
     453             :   long i, R;
     454             :   pari_timer T;
     455             : 
     456        1295 :   if (DEBUGLEVEL>2) timer_start(&T);
     457        1295 :   u = ZM_lll_norms(m, final? 0.999: 0.75, LLL_INPLACE, &norm);
     458        1295 :   if (DEBUGLEVEL>2) *ti_LLL += timer_delay(&T);
     459        8750 :   for (R=lg(m)-1; R > 0; R--)
     460        8750 :     if (cmprr(gel(norm,R), Bnorm) < 0) break;
     461       13916 :   for (i=1; i<=R; i++) setlg(u[i], n0+1);
     462        1295 :   if (R <= 1)
     463             :   {
     464          84 :     if (!R) pari_err_BUG("LLL_cmbf [no factor]");
     465          84 :     return NULL; /* irreducible */
     466             :   }
     467        1211 :   setlg(u, R+1); return u;
     468             : }
     469             : 
     470             : static ulong
     471          14 : next2pow(ulong a)
     472             : {
     473          14 :   ulong b = 1;
     474         112 :   while (b < a) b <<= 1;
     475          14 :   return b;
     476             : }
     477             : 
     478             : /* Recombination phase of Berlekamp-Zassenhaus algorithm using a variant of
     479             :  * van Hoeij's knapsack
     480             :  *
     481             :  * P = squarefree in Z[X].
     482             :  * famod = array of (lifted) modular factors mod p^a
     483             :  * bound = Mignotte bound for the size of divisors of P (for the sup norm)
     484             :  * previously recombined all set of factors with less than rec elts */
     485             : static GEN
     486         105 : LLL_cmbf(GEN P, GEN famod, GEN p, GEN pa, GEN bound, long a, long rec)
     487             : {
     488         105 :   const long N0 = 1; /* # of traces added at each step */
     489         105 :   double BitPerFactor = 0.4; /* nb bits in p^(a-b) / modular factor */
     490         105 :   long i,j,tmax,n0,C, dP = degpol(P);
     491         105 :   double logp = log((double)itos(p)), LOGp2 = M_LN2/logp;
     492         105 :   double b0 = log((double)dP*2) / logp, logBr;
     493             :   GEN lP, Br, Bnorm, Tra, T2, TT, CM_L, m, list, ZERO;
     494             :   pari_sp av, av2;
     495         105 :   long ti_LLL = 0, ti_CF  = 0;
     496             : 
     497         105 :   lP = absi_shallow(leading_coeff(P));
     498         105 :   if (equali1(lP)) lP = NULL;
     499         105 :   Br = root_bound(P);
     500         105 :   if (lP) Br = mulii(lP, Br);
     501         105 :   logBr = gtodouble(glog(Br, DEFAULTPREC)) / logp;
     502             : 
     503         105 :   n0 = lg(famod) - 1;
     504         105 :   C = (long)ceil( sqrt(N0 * n0 / 4.) ); /* > 1 */
     505         105 :   Bnorm = dbltor(n0 * (C*C + N0*n0/4.) * 1.00001);
     506         105 :   ZERO = zeromat(n0, N0);
     507             : 
     508         105 :   av = avma;
     509         105 :   TT = cgetg(n0+1, t_VEC);
     510         105 :   Tra  = cgetg(n0+1, t_MAT);
     511        2142 :   for (i=1; i<=n0; i++)
     512             :   {
     513        2037 :     TT[i]  = 0;
     514        2037 :     gel(Tra,i) = cgetg(N0+1, t_COL);
     515             :   }
     516         105 :   CM_L = scalarmat_s(C, n0);
     517             :   /* tmax = current number of traces used (and computed so far) */
     518         105 :   for (tmax = 0;; tmax += N0)
     519         420 :   {
     520         525 :     long b, bmin, bgood, delta, tnew = tmax + N0, r = lg(CM_L)-1;
     521             :     GEN M_L, q, CM_Lp, oldCM_L;
     522         525 :     int first = 1;
     523             :     pari_timer ti2, TI;
     524             : 
     525         525 :     bmin = (long)ceil(b0 + tnew*logBr);
     526         525 :     if (DEBUGLEVEL>2)
     527           0 :       err_printf("\nLLL_cmbf: %ld potential factors (tmax = %ld, bmin = %ld)\n",
     528             :                  r, tmax, bmin);
     529             : 
     530             :     /* compute Newton sums (possibly relifting first) */
     531         525 :     if (a <= bmin)
     532             :     {
     533          14 :       a = (long)ceil(bmin + 3*N0*logBr) + 1; /* enough for 3 more rounds */
     534          14 :       a = (long)next2pow((ulong)a);
     535             : 
     536          14 :       pa = powiu(p,a);
     537          14 :       famod = ZpX_liftfact(P, famod, pa, p, a);
     538         266 :       for (i=1; i<=n0; i++) TT[i] = 0;
     539             :     }
     540       10689 :     for (i=1; i<=n0; i++)
     541             :     {
     542       10164 :       GEN p1 = gel(Tra,i);
     543       10164 :       GEN p2 = polsym_gen(gel(famod,i), gel(TT,i), tnew, NULL, pa);
     544       10164 :       gel(TT,i) = p2;
     545       10164 :       p2 += 1+tmax; /* ignore traces number 0...tmax */
     546       20328 :       for (j=1; j<=N0; j++) gel(p1,j) = gel(p2,j);
     547       10164 :       if (lP)
     548             :       { /* make Newton sums integral */
     549        1848 :         GEN lPpow = powiu(lP, tmax);
     550        3696 :         for (j=1; j<=N0; j++)
     551             :         {
     552        1848 :           lPpow = mulii(lPpow,lP);
     553        1848 :           gel(p1,j) = mulii(gel(p1,j), lPpow);
     554             :         }
     555             :       }
     556             :     }
     557             : 
     558             :     /* compute truncation parameter */
     559         525 :     if (DEBUGLEVEL>2) { timer_start(&ti2); timer_start(&TI); }
     560         525 :     oldCM_L = CM_L;
     561         525 :     av2 = avma;
     562         525 :     delta = b = 0; /* -Wall */
     563         959 : AGAIN:
     564         959 :     M_L = Q_div_to_int(CM_L, utoipos(C));
     565         959 :     T2 = centermod( ZM_mul(Tra, M_L), pa );
     566         959 :     if (first)
     567             :     { /* initialize lattice, using few p-adic digits for traces */
     568         525 :       double t = gexpo(T2) - maxdd(32.0, BitPerFactor*r);
     569         525 :       bgood = (long) (t * LOGp2);
     570         525 :       b = maxss(bmin, bgood);
     571         525 :       delta = a - b;
     572             :     }
     573             :     else
     574             :     { /* add more p-adic digits and continue reduction */
     575         434 :       long b0 = (long)(gexpo(T2) * LOGp2);
     576         434 :       if (b0 < b) b = b0;
     577         434 :       b = maxss(b-delta, bmin);
     578         434 :       if (b - delta/2 < bmin) b = bmin; /* near there. Go all the way */
     579             :     }
     580             : 
     581         959 :     q = powiu(p, b);
     582         959 :     m = vconcat( CM_L, gdivround(T2, q) );
     583         959 :     if (first)
     584             :     {
     585         525 :       GEN P1 = scalarmat(powiu(p, a-b), N0);
     586         525 :       first = 0;
     587         525 :       m = shallowconcat( m, vconcat(ZERO, P1) );
     588             :       /*     [ C M_L        0     ]
     589             :        * m = [                    ]   square matrix
     590             :        *     [  T2'  p^(a-b) I_N0 ]   T2' = Tra * M_L  truncated
     591             :        */
     592             :     }
     593             : 
     594         959 :     CM_L = LLL_check_progress(Bnorm, n0, m, b == bmin, /*dbg:*/ &ti_LLL);
     595         959 :     if (DEBUGLEVEL>2)
     596           0 :       err_printf("LLL_cmbf: (a,b) =%4ld,%4ld; r =%3ld -->%3ld, time = %ld\n",
     597           0 :                  a,b, lg(m)-1, CM_L? lg(CM_L)-1: 1, timer_delay(&TI));
     598         959 :     if (!CM_L) { list = mkvec(P); break; }
     599         896 :     if (b > bmin)
     600             :     {
     601         434 :       CM_L = gerepilecopy(av2, CM_L);
     602         434 :       goto AGAIN;
     603             :     }
     604         462 :     if (DEBUGLEVEL>2) timer_printf(&ti2, "for this block of traces");
     605             : 
     606         462 :     i = lg(CM_L) - 1;
     607         462 :     if (i == r && ZM_equal(CM_L, oldCM_L))
     608             :     {
     609          56 :       CM_L = oldCM_L;
     610          56 :       set_avma(av2); continue;
     611             :     }
     612             : 
     613         406 :     CM_Lp = FpM_image(CM_L, utoipos(27449)); /* inexpensive test */
     614         406 :     if (lg(CM_Lp) != lg(CM_L))
     615             :     {
     616           7 :       if (DEBUGLEVEL>2) err_printf("LLL_cmbf: rank decrease\n");
     617           7 :       CM_L = ZM_hnf(CM_L);
     618             :     }
     619             : 
     620         406 :     if (i <= r && i*rec < n0)
     621             :     {
     622             :       pari_timer ti;
     623         140 :       if (DEBUGLEVEL>2) timer_start(&ti);
     624         140 :       list = chk_factors(P, Q_div_to_int(CM_L,utoipos(C)), bound, famod, pa);
     625         140 :       if (DEBUGLEVEL>2) ti_CF += timer_delay(&ti);
     626         140 :       if (list) break;
     627          98 :       if (DEBUGLEVEL>2) err_printf("LLL_cmbf: chk_factors failed");
     628             :     }
     629         364 :     CM_L = gerepilecopy(av2, CM_L);
     630         364 :     if (gc_needed(av,1))
     631             :     {
     632           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"LLL_cmbf");
     633           0 :       gerepileall(av, 5, &CM_L, &TT, &Tra, &famod, &pa);
     634             :     }
     635             :   }
     636         105 :   if (DEBUGLEVEL>2)
     637           0 :     err_printf("* Time LLL: %ld\n* Time Check Factor: %ld\n",ti_LLL,ti_CF);
     638         105 :   return list;
     639             : }
     640             : 
     641             : /* Find a,b minimal such that A < q^a, B < q^b, 1 << q^(a-b) < 2^31 */
     642             : static int
     643        9310 : cmbf_precs(GEN q, GEN A, GEN B, long *pta, long *ptb, GEN *qa, GEN *qb)
     644             : {
     645        9310 :   long a,b,amin,d = (long)(31 * M_LN2/gtodouble(glog(q,DEFAULTPREC)) - 1e-5);
     646        9310 :   int fl = 0;
     647             : 
     648        9310 :   b = logintall(B, q, qb) + 1;
     649        9310 :   *qb = mulii(*qb, q);
     650        9310 :   amin = b + d;
     651        9310 :   if (gcmp(powiu(q, amin), A) <= 0)
     652             :   {
     653        1540 :     a = logintall(A, q, qa) + 1;
     654        1540 :     *qa = mulii(*qa, q);
     655        1540 :     b = a - d; *qb = powiu(q, b);
     656             :   }
     657             :   else
     658             :   { /* not enough room */
     659        7770 :     a = amin;  *qa = powiu(q, a);
     660        7770 :     fl = 1;
     661             :   }
     662        9310 :   if (DEBUGLEVEL > 3) {
     663           0 :     err_printf("S_2   bound: %Ps^%ld\n", q,b);
     664           0 :     err_printf("coeff bound: %Ps^%ld\n", q,a);
     665             :   }
     666        9310 :   *pta = a;
     667        9310 :   *ptb = b; return fl;
     668             : }
     669             : 
     670             : /* use van Hoeij's knapsack algorithm */
     671             : static GEN
     672        9310 : combine_factors(GEN target, GEN famod, GEN p, long klim)
     673             : {
     674             :   GEN la, B, A, res, L, pa, pb, listmod;
     675        9310 :   long a,b, l, maxK, n = degpol(target);
     676             :   int done;
     677             :   pari_timer T;
     678             : 
     679        9310 :   A = factor_bound(target);
     680             : 
     681        9310 :   la = absi_shallow(leading_coeff(target));
     682        9310 :   B = mului(n, sqri(mulii(la, root_bound(target)))); /* = bound for S_2 */
     683             : 
     684        9310 :   (void)cmbf_precs(p, A, B, &a, &b, &pa, &pb);
     685             : 
     686        9310 :   if (DEBUGLEVEL>2) timer_start(&T);
     687        9310 :   famod = ZpX_liftfact(target, famod, pa, p, a);
     688        9310 :   if (DEBUGLEVEL>2) timer_printf(&T, "Hensel lift (mod %Ps^%ld)", p,a);
     689        9310 :   L = cmbf(target, famod, A, p, a, b, klim, &maxK, &done);
     690        9310 :   if (DEBUGLEVEL>2) timer_printf(&T, "Naive recombination");
     691             : 
     692        9310 :   res     = gel(L,1);
     693        9310 :   listmod = gel(L,2); l = lg(listmod)-1;
     694        9310 :   famod = gel(listmod,l);
     695        9310 :   if (maxK > 0 && lg(famod)-1 > 2*maxK)
     696             :   {
     697         105 :     if (l!=1) A = factor_bound(gel(res,l));
     698         105 :     if (DEBUGLEVEL > 4) err_printf("last factor still to be checked\n");
     699         105 :     L = LLL_cmbf(gel(res,l), famod, p, pa, A, a, maxK);
     700         105 :     if (DEBUGLEVEL>2) timer_printf(&T,"Knapsack");
     701             :     /* remove last elt, possibly unfactored. Add all new ones. */
     702         105 :     setlg(res, l); res = shallowconcat(res, L);
     703             :   }
     704        9310 :   return res;
     705             : }
     706             : 
     707             : /* Assume 'a' a squarefree ZX; return 0 if no root (fl=1) / irreducible (fl=0).
     708             :  * Otherwise return prime p such that a mod p has fewest roots / factors */
     709             : static ulong
     710      673782 : pick_prime(GEN a, long fl, pari_timer *T)
     711             : {
     712      673782 :   pari_sp av = avma, av1;
     713      673782 :   const long MAXNP = 7, da = degpol(a);
     714      673782 :   long nmax = da+1, np;
     715      673782 :   ulong chosenp = 0;
     716      673782 :   GEN lead = gel(a,da+2);
     717             :   forprime_t S;
     718      673782 :   if (equali1(lead)) lead = NULL;
     719      673782 :   u_forprime_init(&S, 2, ULONG_MAX);
     720      673782 :   av1 = avma;
     721     3734552 :   for (np = 0; np < MAXNP; set_avma(av1))
     722             :   {
     723     3682182 :     ulong p = u_forprime_next(&S);
     724             :     long nfacp;
     725             :     GEN z;
     726             : 
     727     3682182 :     if (!p) pari_err_OVERFLOW("DDF [out of small primes]");
     728     3682182 :     if (lead && !umodiu(lead,p)) continue;
     729     3632465 :     z = ZX_to_Flx(a, p);
     730     3632464 :     if (!Flx_is_squarefree(z, p)) continue;
     731             : 
     732     2305947 :     if (fl)
     733             :     {
     734     2192122 :       nfacp = Flx_nbroots(z, p);
     735     2192122 :       if (!nfacp) { chosenp = 0; break; } /* no root */
     736             :     }
     737             :     else
     738             :     {
     739      113825 :       nfacp = Flx_nbfact(z, p);
     740      113826 :       if (nfacp == 1) { chosenp = 0; break; } /* irreducible */
     741             :     }
     742     1684557 :     if (DEBUGLEVEL>4)
     743           0 :       err_printf("...tried prime %3lu (%-3ld %s). Time = %ld\n",
     744             :                   p, nfacp, fl? "roots": "factors", timer_delay(T));
     745     1684558 :     if (nfacp < nmax)
     746             :     {
     747      572840 :       nmax = nfacp; chosenp = p;
     748      572840 :       if (da > 100 && nmax < 5) break; /* large degree, few factors. Enough */
     749             :     }
     750     1684537 :     np++;
     751             :   }
     752      673782 :   return gc_ulong(av, chosenp);
     753             : }
     754             : 
     755             : /* Assume A a squarefree ZX; return the vector of its rational roots */
     756             : static GEN
     757      631537 : DDF_roots(GEN A)
     758             : {
     759             :   GEN p, lc, lcpol, z, pe, pes2, bound;
     760             :   long i, m, e, lz;
     761             :   ulong pp;
     762             :   pari_sp av;
     763             :   pari_timer T;
     764             : 
     765      631537 :   if (DEBUGLEVEL>2) timer_start(&T);
     766      631537 :   pp = pick_prime(A, 1, &T);
     767      631537 :   if (!pp) return cgetg(1,t_VEC); /* no root */
     768       43081 :   p = utoipos(pp);
     769       43081 :   lc = leading_coeff(A);
     770       43081 :   if (is_pm1(lc))
     771       39794 :   { lc = NULL; lcpol = A; }
     772             :   else
     773        3287 :   { lc = absi_shallow(lc); lcpol = ZX_Z_mul(A, lc); }
     774       43081 :   bound = root_bound(A); if (lc) bound = mulii(lc, bound);
     775       43081 :   e = logintall(addiu(shifti(bound, 1), 1), p, &pe) + 1;
     776       43081 :   pe = mulii(pe, p);
     777       43081 :   pes2 = shifti(pe, -1);
     778       43081 :   if (DEBUGLEVEL>2) timer_printf(&T, "Root bound");
     779       43081 :   av = avma;
     780       43081 :   z = ZpX_roots(A, p, e); lz = lg(z);
     781       43081 :   z = deg1_from_roots(z, varn(A));
     782       43081 :   if (DEBUGLEVEL>2) timer_printf(&T, "Hensel lift (mod %lu^%ld)", pp,e);
     783       87569 :   for (m=1, i=1; i < lz; i++)
     784             :   {
     785       44488 :     GEN q, r, y = gel(z,i);
     786       44488 :     if (lc) y = ZX_Z_mul(y, lc);
     787       44488 :     y = centermod_i(y, pe, pes2);
     788       44488 :     if (! (q = ZX_divides(lcpol, y)) ) continue;
     789             : 
     790        5403 :     lcpol = q;
     791        5403 :     r = negi( constant_coeff(y) );
     792        5403 :     if (lc) {
     793        2709 :       r = gdiv(r,lc);
     794        2709 :       lcpol = Q_primpart(lcpol);
     795        2709 :       lc = absi_shallow( leading_coeff(lcpol) );
     796        2709 :       if (is_pm1(lc)) lc = NULL; else lcpol = ZX_Z_mul(lcpol, lc);
     797             :     }
     798        5403 :     gel(z,m++) = r;
     799        5403 :     if (gc_needed(av,2))
     800             :     {
     801           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"DDF_roots, m = %ld", m);
     802           0 :       gerepileall(av, lc? 3:2, &z, &lcpol, &lc);
     803             :     }
     804             :   }
     805       43081 :   if (DEBUGLEVEL>2) timer_printf(&T, "Recombination");
     806       43081 :   z[0] = evaltyp(t_VEC) | evallg(m); return z;
     807             : }
     808             : 
     809             : /* Assume a squarefree ZX, deg(a) > 0, return rational factors.
     810             :  * In fact, a(0) != 0 but we don't use this */
     811             : static GEN
     812       42245 : DDF(GEN a)
     813             : {
     814             :   GEN ap, prime, famod, z;
     815       42245 :   long ti = 0;
     816       42245 :   ulong p = 0;
     817       42245 :   pari_sp av = avma;
     818             :   pari_timer T, T2;
     819             : 
     820       42245 :   if (DEBUGLEVEL>2) { timer_start(&T); timer_start(&T2); }
     821       42245 :   p = pick_prime(a, 0, &T2);
     822       42245 :   if (!p) return mkvec(a);
     823        9310 :   prime = utoipos(p);
     824        9310 :   ap = Flx_normalize(ZX_to_Flx(a, p), p);
     825        9310 :   famod = gel(Flx_factor(ap, p), 1);
     826        9310 :   if (DEBUGLEVEL>2)
     827             :   {
     828           0 :     if (DEBUGLEVEL>4) timer_printf(&T2, "splitting mod p = %lu", p);
     829           0 :     ti = timer_delay(&T);
     830           0 :     err_printf("Time setup: %ld\n", ti);
     831             :   }
     832        9310 :   z = combine_factors(a, FlxV_to_ZXV(famod), prime, degpol(a)-1);
     833        9310 :   if (DEBUGLEVEL>2)
     834           0 :     err_printf("Total Time: %ld\n===========\n", ti + timer_delay(&T));
     835        9310 :   return gerepilecopy(av, z);
     836             : }
     837             : 
     838             : /* Distinct Degree Factorization (deflating first)
     839             :  * Assume x squarefree, degree(x) > 0, x(0) != 0 */
     840             : GEN
     841       30443 : ZX_DDF(GEN x)
     842             : {
     843             :   GEN L;
     844             :   long m;
     845       30443 :   x = ZX_deflate_max(x, &m);
     846       30443 :   L = DDF(x);
     847       30443 :   if (m > 1)
     848             :   {
     849       10514 :     GEN e, v, fa = factoru(m);
     850             :     long i,j,k, l;
     851             : 
     852       10514 :     e = gel(fa,2); k = 0;
     853       10514 :     fa= gel(fa,1); l = lg(fa);
     854       21266 :     for (i=1; i<l; i++) k += e[i];
     855       10514 :     v = cgetg(k+1, t_VECSMALL); k = 1;
     856       21266 :     for (i=1; i<l; i++)
     857       22428 :       for (j=1; j<=e[i]; j++) v[k++] = fa[i];
     858       22190 :     for (k--; k; k--)
     859             :     {
     860       11676 :       GEN L2 = cgetg(1,t_VEC);
     861       23478 :       for (i=1; i < lg(L); i++)
     862       11802 :               L2 = shallowconcat(L2, DDF(RgX_inflate(gel(L,i), v[k])));
     863       11676 :       L = L2;
     864             :     }
     865             :   }
     866       30443 :   return L;
     867             : }
     868             : 
     869             : /* SquareFree Factorization. f = prod P^e, all e distinct, in Z[X] (char 0
     870             :  * would be enough, if ZX_gcd --> ggcd). Return (P), set *ex = (e) */
     871             : GEN
     872       48142 : ZX_squff(GEN f, GEN *ex)
     873             : {
     874             :   GEN T, V, P, e;
     875       48142 :   long i, k, val = ZX_valrem(f, &f), n = 2 + degpol(f);
     876             : 
     877       48142 :   if (signe(leading_coeff(f)) < 0) f = ZX_neg(f);
     878       48142 :   e = cgetg(n,t_VECSMALL);
     879       48142 :   P = cgetg(n,t_COL);
     880       48142 :   T = ZX_gcd_all(f, ZX_deriv(f), &V);
     881       48142 :   for (k = i = 1;; k++)
     882        2190 :   {
     883       50332 :     GEN W = ZX_gcd_all(T,V, &T); /* V and W are squarefree */
     884       50332 :     long dW = degpol(W), dV = degpol(V);
     885             :     /* f = prod_i T_i^{e_i}
     886             :      * W = prod_{i: e_i > k} T_i,
     887             :      * V = prod_{i: e_i >= k} T_i,
     888             :      * T = prod_{i: e_i > k} T_i^{e_i - k} */
     889       50332 :     if (!dW)
     890             :     {
     891       48142 :       if (dV) { gel(P,i) = Q_primpart(V); e[i] = k; i++; }
     892       48142 :       break;
     893             :     }
     894        2190 :     if (dW == dV)
     895             :     {
     896             :       GEN U;
     897         840 :       while ( (U = ZX_divides(T, V)) ) { k++; T = U; }
     898             :     }
     899             :     else
     900             :     {
     901        1553 :       gel(P,i) = Q_primpart(RgX_div(V,W));
     902        1553 :       e[i] = k; i++; V = W;
     903             :     }
     904             :   }
     905       48142 :   if (val) { gel(P,i) = pol_x(varn(f)); e[i] = val; i++;}
     906       48142 :   setlg(P,i);
     907       48142 :   setlg(e,i); *ex = e; return P;
     908             : }
     909             : 
     910             : static GEN
     911        7812 : fact_from_DDF(GEN fa, GEN e, long n)
     912             : {
     913        7812 :   GEN v,w, y = cgetg(3, t_MAT);
     914        7812 :   long i,j,k, l = lg(fa);
     915             : 
     916        7812 :   v = cgetg(n+1,t_COL); gel(y,1) = v;
     917        7812 :   w = cgetg(n+1,t_COL); gel(y,2) = w;
     918       16359 :   for (k=i=1; i<l; i++)
     919             :   {
     920        8547 :     GEN L = gel(fa,i), ex = utoipos(e[i]);
     921        8547 :     long J = lg(L);
     922       21000 :     for (j=1; j<J; j++,k++)
     923             :     {
     924       12453 :       gel(v,k) = gcopy(gel(L,j));
     925       12453 :       gel(w,k) = ex;
     926             :     }
     927             :   }
     928        7812 :   return y;
     929             : }
     930             : 
     931             : /* Factor x in Z[t] */
     932             : static GEN
     933        7819 : ZX_factor_i(GEN x)
     934             : {
     935             :   GEN fa,ex,y;
     936             :   long n,i,l;
     937             : 
     938        7819 :   if (!signe(x)) return prime_fact(x);
     939        7812 :   fa = ZX_squff(x, &ex);
     940        7812 :   l = lg(fa); n = 0;
     941       16359 :   for (i=1; i<l; i++)
     942             :   {
     943        8547 :     gel(fa,i) = ZX_DDF(gel(fa,i));
     944        8547 :     n += lg(gel(fa,i))-1;
     945             :   }
     946        7812 :   y = fact_from_DDF(fa,ex,n);
     947        7812 :   return sort_factor_pol(y, cmpii);
     948             : }
     949             : GEN
     950        7434 : ZX_factor(GEN x)
     951             : {
     952        7434 :   pari_sp av = avma;
     953        7434 :   return gerepileupto(av, ZX_factor_i(x));
     954             : }
     955             : GEN
     956         385 : QX_factor(GEN x)
     957             : {
     958         385 :   pari_sp av = avma;
     959         385 :   return gerepileupto(av, ZX_factor_i(Q_primpart(x)));
     960             : }
     961             : 
     962             : long
     963       23166 : ZX_is_irred(GEN x)
     964             : {
     965       23166 :   pari_sp av = avma;
     966       23166 :   long l = lg(x);
     967             :   GEN y;
     968       23166 :   if (l <= 3) return 0; /* degree < 1 */
     969       23166 :   if (l == 4) return 1; /* degree 1 */
     970       21385 :   if (ZX_val(x)) return 0;
     971       21189 :   if (!ZX_is_squarefree(x)) return 0;
     972       21119 :   y = ZX_DDF(x); set_avma(av);
     973       21119 :   return (lg(y) == 2);
     974             : }
     975             : 
     976             : GEN
     977      631537 : nfrootsQ(GEN x)
     978             : {
     979      631537 :   pari_sp av = avma;
     980             :   GEN z;
     981             :   long val;
     982             : 
     983      631537 :   if (typ(x)!=t_POL) pari_err_TYPE("nfrootsQ",x);
     984      631537 :   if (!signe(x)) pari_err_ROOTS0("nfrootsQ");
     985      631537 :   x = Q_primpart(x);
     986      631537 :   RgX_check_ZX(x,"nfrootsQ");
     987      631537 :   val = ZX_valrem(x, &x);
     988      631537 :   z = DDF_roots( ZX_radical(x) );
     989      631537 :   if (val) z = shallowconcat(z, gen_0);
     990      631537 :   return gerepileupto(av, sort(z));
     991             : }
     992             : 
     993             : /************************************************************************
     994             :  *                   GCD OVER Z[X] / Q[X]                               *
     995             :  ************************************************************************/
     996             : int
     997       39560 : ZX_is_squarefree(GEN x)
     998             : {
     999       39560 :   pari_sp av = avma;
    1000             :   GEN d;
    1001             :   long m;
    1002       39560 :   if (lg(x) == 2) return 0;
    1003       39560 :   m = ZX_deflate_order(x);
    1004       39560 :   if (m > 1)
    1005             :   {
    1006       11137 :     if (!signe(gel(x,2))) return 0;
    1007       11081 :     x = RgX_deflate(x, m);
    1008             :   }
    1009       39504 :   d = ZX_gcd(x,ZX_deriv(x));
    1010       39504 :   return gc_bool(av, lg(d) == 3);
    1011             : }
    1012             : 
    1013             : static int
    1014       31944 : ZX_gcd_filter(GEN *pt_A, GEN *pt_P)
    1015             : {
    1016       31944 :   GEN A = *pt_A, P = *pt_P;
    1017       31944 :   long i, j, l = lg(A), n = 1, d = degpol(gel(A,1));
    1018             :   GEN B, Q;
    1019       64904 :   for (i=2; i<l; i++)
    1020             :   {
    1021       32960 :     long di = degpol(gel(A,i));
    1022       32960 :     if (di==d) n++;
    1023          36 :     else if (d > di)
    1024          36 :     { n=1; d = di; }
    1025             :   }
    1026       31944 :   if (n == l-1)
    1027       31908 :     return 0;
    1028          36 :   B = cgetg(n+1, t_VEC);
    1029          36 :   Q = cgetg(n+1, typ(P));
    1030         156 :   for (i=1, j=1; i<l; i++)
    1031             :   {
    1032         120 :     if (degpol(gel(A,i))==d)
    1033             :     {
    1034          84 :       gel(B,j) = gel(A,i);
    1035          84 :       Q[j] = P[i];
    1036          84 :       j++;
    1037             :     }
    1038             :   }
    1039          36 :   *pt_A = B; *pt_P = Q; return 1;
    1040             : }
    1041             : 
    1042             : static GEN
    1043     3159189 : ZX_gcd_Flx(GEN a, GEN b, ulong g, ulong p)
    1044             : {
    1045     3159189 :   GEN H = Flx_gcd(a, b, p);
    1046     3159189 :   if (!g)
    1047     3147512 :     return Flx_normalize(H, p);
    1048             :   else
    1049             :   {
    1050       11677 :     ulong t = Fl_mul(g, Fl_inv(Flx_lead(H), p), p);
    1051       11677 :     return Flx_Fl_mul(H, t, p);
    1052             :   }
    1053             : }
    1054             : 
    1055             : static GEN
    1056     3154904 : ZX_gcd_slice(GEN A, GEN B, GEN g, GEN P, GEN *mod)
    1057             : {
    1058     3154904 :   pari_sp av = avma;
    1059     3154904 :   long i, n = lg(P)-1;
    1060             :   GEN H, T;
    1061     3154904 :   if (n == 1)
    1062             :   {
    1063     3151587 :     ulong p = uel(P,1), gp = g ? umodiu(g, p): 0;
    1064     3151587 :     GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
    1065     3151587 :     GEN Hp = ZX_gcd_Flx(a, b, gp, p);
    1066     3151587 :     H = gerepileupto(av, Flx_to_ZX(Hp));
    1067     3151587 :     *mod = utoi(p);
    1068     3151587 :     return H;
    1069             :   }
    1070        3317 :   T = ZV_producttree(P);
    1071        3317 :   A = ZX_nv_mod_tree(A, P, T);
    1072        3317 :   B = ZX_nv_mod_tree(B, P, T);
    1073        3317 :   g = g ?  Z_ZV_mod_tree(g, P, T): NULL;
    1074        3317 :   H = cgetg(n+1, t_VEC);
    1075       10919 :   for(i=1; i <= n; i++)
    1076             :   {
    1077        7602 :     ulong p = P[i];
    1078        7602 :     GEN a = gel(A,i), b = gel(B,i);
    1079        7602 :     gel(H,i) = ZX_gcd_Flx(a, b, g? g[i]: 0, p);
    1080             :   }
    1081        3317 :   if (ZX_gcd_filter(&H, &P))
    1082          12 :     T = ZV_producttree(P);
    1083        3317 :   H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
    1084        3317 :   *mod = gmael(T, lg(T)-1, 1);
    1085        3317 :   gerepileall(av, 2, &H, mod);
    1086        3317 :   return H;
    1087             : }
    1088             : 
    1089             : GEN
    1090     3154904 : ZX_gcd_worker(GEN P, GEN A, GEN B, GEN g)
    1091             : {
    1092     3154904 :   GEN V = cgetg(3, t_VEC);
    1093     3154904 :   gel(V,1) = ZX_gcd_slice(A, B, equali1(g)? NULL: g , P, &gel(V,2));
    1094     3154904 :   return V;
    1095             : }
    1096             : 
    1097             : static GEN
    1098       28627 : ZX_gcd_chinese(GEN A, GEN P, GEN *mod)
    1099             : {
    1100       28627 :   ZX_gcd_filter(&A, &P);
    1101       28627 :   return nxV_chinese_center(A, P, mod);
    1102             : }
    1103             : 
    1104             : GEN
    1105    10253116 : ZX_gcd_all(GEN A, GEN B, GEN *Anew)
    1106             : {
    1107    10253116 :   pari_sp av = avma;
    1108    10253116 :   long k, valH, valA, valB, vA = varn(A), dA = degpol(A), dB = degpol(B);
    1109    10253116 :   GEN worker, c, cA, cB, g, Ag, Bg, H = NULL, mod = gen_1, R;
    1110             :   GEN Ap, Bp, Hp;
    1111             :   forprime_t S;
    1112             :   ulong pp;
    1113    10253116 :   if (dA < 0) { if (Anew) *Anew = pol_0(vA); return ZX_copy(B); }
    1114    10253095 :   if (dB < 0) { if (Anew) *Anew = pol_1(vA); return ZX_copy(A); }
    1115    10252297 :   A = Q_primitive_part(A, &cA);
    1116    10252297 :   B = Q_primitive_part(B, &cB);
    1117    10252296 :   valA = ZX_valrem(A, &A); dA -= valA;
    1118    10252296 :   valB = ZX_valrem(B, &B); dB -= valB;
    1119    10252297 :   valH = minss(valA, valB);
    1120    10252297 :   valA -= valH; /* valuation(Anew) */
    1121    10252297 :   c = (cA && cB)? gcdii(cA, cB): NULL; /* content(gcd) */
    1122    10252297 :   if (!dA || !dB)
    1123             :   {
    1124     6122479 :     if (Anew) *Anew = RgX_shift_shallow(A, valA);
    1125     6122479 :     return monomial(c? c: gen_1, valH, vA);
    1126             :   }
    1127     4129818 :   g = gcdii(leading_coeff(A), leading_coeff(B)); /* multiple of lead(gcd) */
    1128     4129817 :   if (is_pm1(g)) {
    1129     4036980 :     g = NULL;
    1130     4036980 :     Ag = A;
    1131     4036980 :     Bg = B;
    1132             :   } else {
    1133       92837 :     Ag = ZX_Z_mul(A,g);
    1134       92837 :     Bg = ZX_Z_mul(B,g);
    1135             :   }
    1136     4129817 :   init_modular_big(&S);
    1137             :   do {
    1138     4129832 :     pp = u_forprime_next(&S);
    1139     4129832 :     Ap = ZX_to_Flx(Ag, pp);
    1140     4129832 :     Bp = ZX_to_Flx(Bg, pp);
    1141     4129832 :   } while (degpol(Ap) != dA || degpol(Bp) != dB);
    1142     4129818 :   if (degpol(Flx_gcd(Ap, Bp, pp)) == 0)
    1143             :   {
    1144     1003589 :     if (Anew) *Anew = RgX_shift_shallow(A, valA);
    1145     1003589 :     return monomial(c? c: gen_1, valH, vA);
    1146             :   }
    1147     3126229 :   worker = snm_closure(is_entry("_ZX_gcd_worker"), mkvec3(A, B, g? g: gen_1));
    1148     3126229 :   av = avma;
    1149     3126229 :   for (k = 1; ;k *= 2)
    1150             :   {
    1151     3153798 :     gen_inccrt_i("ZX_gcd", worker, g, (k+1)>>1, 0, &S, &H, &mod, ZX_gcd_chinese, NULL);
    1152     3153798 :     gerepileall(av, 2, &H, &mod);
    1153     3153798 :     Hp = ZX_to_Flx(H, pp);
    1154     3153798 :     if (lgpol(Flx_rem(Ap, Hp, pp)) || lgpol(Flx_rem(Bp, Hp, pp))) continue;
    1155     3128912 :     if (!ZX_divides(Bg, H)) continue;
    1156     3126259 :     R = ZX_divides(Ag, H);
    1157     3126259 :     if (R) break;
    1158             :   }
    1159             :   /* lead(H) = g */
    1160     3126229 :   if (g) H = Q_primpart(H);
    1161     3126229 :   if (c) H = ZX_Z_mul(H,c);
    1162     3126229 :   if (DEBUGLEVEL>5) err_printf("done\n");
    1163     3126229 :   if (Anew) *Anew = RgX_shift_shallow(R, valA);
    1164     3126229 :   return valH? RgX_shift_shallow(H, valH): H;
    1165             : }
    1166             : 
    1167             : #if 0
    1168             : /* ceil( || p ||_oo / lc(p) ) */
    1169             : static GEN
    1170             : maxnorm(GEN p)
    1171             : {
    1172             :   long i, n = degpol(p), av = avma;
    1173             :   GEN x, m = gen_0;
    1174             : 
    1175             :   p += 2;
    1176             :   for (i=0; i<n; i++)
    1177             :   {
    1178             :     x = gel(p,i);
    1179             :     if (abscmpii(x,m) > 0) m = x;
    1180             :   }
    1181             :   m = divii(m, gel(p,n));
    1182             :   return gerepileuptoint(av, addiu(absi_shallow(m),1));
    1183             : }
    1184             : #endif
    1185             : 
    1186             : GEN
    1187     9490416 : ZX_gcd(GEN A, GEN B)
    1188             : {
    1189     9490416 :   pari_sp av = avma;
    1190     9490416 :   return gerepilecopy(av, ZX_gcd_all(A,B,NULL));
    1191             : }
    1192             : 
    1193             : GEN
    1194      660110 : ZX_radical(GEN A) { GEN B; (void)ZX_gcd_all(A,ZX_deriv(A),&B); return B; }
    1195             : 
    1196             : static GEN
    1197        3940 : _gcd(GEN a, GEN b)
    1198             : {
    1199        3940 :   if (!a) a = gen_1;
    1200        3940 :   if (!b) b = gen_1;
    1201        3940 :   return Q_gcd(a,b);
    1202             : }
    1203             : /* A0 and B0 in Q[X] */
    1204             : GEN
    1205        3940 : QX_gcd(GEN A0, GEN B0)
    1206             : {
    1207             :   GEN a, b, D;
    1208        3940 :   pari_sp av = avma, av2;
    1209             : 
    1210        3940 :   D = ZX_gcd(Q_primitive_part(A0, &a), Q_primitive_part(B0, &b));
    1211        3940 :   av2 = avma; a = _gcd(a,b);
    1212        3940 :   if (isint1(a)) set_avma(av2); else D = ZX_Q_mul(D, a);
    1213        3940 :   return gerepileupto(av, D);
    1214             : }
    1215             : 
    1216             : /*****************************************************************************
    1217             :  * Variants of the Bradford-Davenport algorithm: look for cyclotomic         *
    1218             :  * factors, and decide whether a ZX is cyclotomic or a product of cyclotomic *
    1219             :  *****************************************************************************/
    1220             : /* f of degree 1, return a cyclotomic factor (Phi_1 or Phi_2) or NULL */
    1221             : static GEN
    1222           0 : BD_deg1(GEN f)
    1223             : {
    1224           0 :   GEN a = gel(f,3), b = gel(f,2); /* f = ax + b */
    1225           0 :   if (!absequalii(a,b)) return NULL;
    1226           0 :   return polcyclo((signe(a) == signe(b))? 2: 1, varn(f));
    1227             : }
    1228             : 
    1229             : /* f a squarefree ZX; not divisible by any Phi_n, n even */
    1230             : static GEN
    1231         406 : BD_odd(GEN f)
    1232             : {
    1233         406 :   while(degpol(f) > 1)
    1234             :   {
    1235         406 :     GEN f1 = ZX_graeffe(f); /* contain all cyclotomic divisors of f */
    1236         406 :     if (ZX_equal(f1, f)) return f; /* product of cyclotomics */
    1237           0 :     f = ZX_gcd(f, f1);
    1238             :   }
    1239           0 :   if (degpol(f) == 1) return BD_deg1(f);
    1240           0 :   return NULL; /* no cyclotomic divisor */
    1241             : }
    1242             : 
    1243             : static GEN
    1244        2310 : myconcat(GEN v, GEN x)
    1245             : {
    1246        2310 :   if (typ(x) != t_VEC) x = mkvec(x);
    1247        2310 :   if (!v) return x;
    1248        1470 :   return shallowconcat(v, x);
    1249             : }
    1250             : 
    1251             : /* Bradford-Davenport algorithm.
    1252             :  * f a squarefree ZX of degree > 0, return NULL or a vector of coprime
    1253             :  * cyclotomic factors of f [ possibly reducible ] */
    1254             : static GEN
    1255        2359 : BD(GEN f)
    1256             : {
    1257        2359 :   GEN G = NULL, Gs = NULL, Gp = NULL, Gi = NULL;
    1258             :   GEN fs2, fp, f2, f1, fe, fo, fe1, fo1;
    1259        2359 :   RgX_even_odd(f, &fe, &fo);
    1260        2359 :   fe1 = ZX_eval1(fe);
    1261        2359 :   fo1 = ZX_eval1(fo);
    1262        2359 :   if (absequalii(fe1, fo1)) /* f(1) = 0 or f(-1) = 0 */
    1263             :   {
    1264        1519 :     long i, v = varn(f);
    1265        1519 :     if (!signe(fe1))
    1266         371 :       G = mkvec2(polcyclo(1, v), polcyclo(2, v)); /* both 0 */
    1267        1148 :     else if (signe(fe1) == signe(fo1))
    1268         693 :       G = mkvec(polcyclo(2, v)); /*f(-1) = 0*/
    1269             :     else
    1270         455 :       G = mkvec(polcyclo(1, v)); /*f(1) = 0*/
    1271        3409 :     for (i = lg(G)-1; i; i--) f = RgX_div(f, gel(G,i));
    1272             :   }
    1273             :   /* f no longer divisible by Phi_1 or Phi_2 */
    1274        2359 :   if (degpol(f) <= 1) return G;
    1275        2058 :   f1 = ZX_graeffe(f); /* has at most square factors */
    1276        2058 :   if (ZX_equal(f1, f)) return myconcat(G,f); /* f = product of Phi_n, n odd */
    1277             : 
    1278        1183 :   fs2 = ZX_gcd_all(f1, ZX_deriv(f1), &f2); /* fs2 squarefree */
    1279        1183 :   if (degpol(fs2))
    1280             :   { /* fs contains all Phi_n | f, 4 | n; and only those */
    1281             :     /* In that case, Graeffe(Phi_n) = Phi_{n/2}^2, and Phi_n = Phi_{n/2}(x^2) */
    1282        1029 :     GEN fs = RgX_inflate(fs2, 2);
    1283        1029 :     (void)ZX_gcd_all(f, fs, &f); /* remove those Phi_n | f, 4 | n */
    1284        1029 :     Gs = BD(fs2);
    1285        1029 :     if (Gs)
    1286             :     {
    1287             :       long i;
    1288        2555 :       for (i = lg(Gs)-1; i; i--) gel(Gs,i) = RgX_inflate(gel(Gs,i), 2);
    1289             :       /* prod Gs[i] is the product of all Phi_n | f, 4 | n */
    1290        1029 :       G = myconcat(G, Gs);
    1291             :     }
    1292             :     /* f2 = f1 / fs2 */
    1293        1029 :     f1 = RgX_div(f2, fs2); /* f1 / fs2^2 */
    1294             :   }
    1295        1183 :   fp = ZX_gcd(f, f1); /* contains all Phi_n | f, n > 1 odd; and only those */
    1296        1183 :   if (degpol(fp))
    1297             :   {
    1298         196 :     Gp = BD_odd(fp);
    1299             :     /* Gp is the product of all Phi_n | f, n odd */
    1300         196 :     if (Gp) G = myconcat(G, Gp);
    1301         196 :     f = RgX_div(f, fp);
    1302             :   }
    1303        1183 :   if (degpol(f))
    1304             :   { /* contains all Phi_n originally dividing f, n = 2 mod 4, n > 2;
    1305             :      * and only those
    1306             :      * In that case, Graeffe(Phi_n) = Phi_{n/2}, and Phi_n = Phi_{n/2}(-x) */
    1307         210 :     Gi = BD_odd(ZX_z_unscale(f, -1));
    1308         210 :     if (Gi)
    1309             :     { /* N.B. Phi_2 does not divide f */
    1310         210 :       Gi = ZX_z_unscale(Gi, -1);
    1311             :       /* Gi is the product of all Phi_n | f, n = 2 mod 4 */
    1312         210 :       G = myconcat(G, Gi);
    1313             :     }
    1314             :   }
    1315        1183 :   return G;
    1316             : }
    1317             : 
    1318             : /* Let f be a non-zero QX, return the (squarefree) product of cyclotomic
    1319             :  * divisors of f */
    1320             : GEN
    1321         315 : polcyclofactors(GEN f)
    1322             : {
    1323         315 :   pari_sp av = avma;
    1324         315 :   if (typ(f) != t_POL || !signe(f)) pari_err_TYPE("polcyclofactors",f);
    1325         315 :   (void)RgX_valrem(f, &f);
    1326         315 :   f = Q_primpart(f);
    1327         315 :   RgX_check_ZX(f,"polcyclofactors");
    1328         315 :   if (degpol(f))
    1329             :   {
    1330         315 :     f = BD(ZX_radical(f));
    1331         315 :     if (f) return gerepilecopy(av, f);
    1332             :   }
    1333           0 :   set_avma(av); return cgetg(1,t_VEC);
    1334             : }
    1335             : 
    1336             : /* return t*x mod T(x), T a monic ZX. Assume deg(t) < deg(T) */
    1337             : static GEN
    1338       46452 : ZXQ_mul_by_X(GEN t, GEN T)
    1339             : {
    1340             :   GEN lt;
    1341       46452 :   t = RgX_shift_shallow(t, 1);
    1342       46452 :   if (degpol(t) < degpol(T)) return t;
    1343        4228 :   lt = leading_coeff(t);
    1344        4228 :   if (is_pm1(lt)) return signe(lt) > 0 ? ZX_sub(t, T): ZX_add(t, T);
    1345         217 :   return ZX_sub(t, ZX_Z_mul(T, leading_coeff(t)));
    1346             : }
    1347             : /* f a product of Phi_n, all n odd; deg f > 1. Is it irreducible ? */
    1348             : static long
    1349        1029 : BD_odd_iscyclo(GEN f)
    1350             : {
    1351             :   pari_sp av;
    1352             :   long d, e, n, bound;
    1353             :   GEN t;
    1354        1029 :   f = ZX_deflate_max(f, &e);
    1355        1029 :   av = avma;
    1356             :   /* The original f is cyclotomic (= Phi_{ne}) iff the present one is Phi_n,
    1357             :    * where all prime dividing e also divide n. If current f is Phi_n,
    1358             :    * then n is odd and squarefree */
    1359        1029 :   d = degpol(f); /* = phi(n) */
    1360             :   /* Let e > 0, g multiplicative such that
    1361             :        g(p) = p / (p-1)^(1+e) < 1 iff p < (p-1)^(1+e)
    1362             :      For all squarefree odd n, we have g(n) < C, hence n < C phi(n)^(1+e), where
    1363             :        C = \prod_{p odd | p > (p-1)^(1+e)} g(p)
    1364             :      For e = 1/10,   we obtain p = 3, 5 and C < 1.523
    1365             :      For e = 1/100,  we obtain p = 3, 5, ..., 29 and C < 2.573
    1366             :      In fact, for n <= 10^7 odd & squarefree, we have n < 2.92 * phi(n)
    1367             :      By the above, n<10^7 covers all d <= (10^7/2.573)^(1/(1+1/100)) < 3344391.
    1368             :   */
    1369        1029 :   if (d <= 3344391)
    1370        1029 :     bound = (long)(2.92 * d);
    1371             :   else
    1372           0 :     bound = (long)(2.573 * pow(d,1.01));
    1373             :   /* IF f = Phi_n, n squarefree odd, then n <= bound */
    1374        1029 :   t = pol_xn(d-1, varn(f));
    1375       46487 :   for (n = d; n <= bound; n++)
    1376             :   {
    1377       46452 :     t = ZXQ_mul_by_X(t, f);
    1378             :     /* t = (X mod f(X))^d */
    1379       46452 :     if (degpol(t) == 0) break;
    1380       45458 :     if (gc_needed(av,1))
    1381             :     {
    1382         461 :       if(DEBUGMEM>1) pari_warn(warnmem,"BD_odd_iscyclo");
    1383         461 :       t = gerepilecopy(av, t);
    1384             :     }
    1385             :   }
    1386        1029 :   if (n > bound || eulerphiu(n) != (ulong)d) return 0;
    1387             : 
    1388         966 :   if (e > 1) return (u_ppo(e, n) == 1)? e * n : 0;
    1389         840 :   return n;
    1390             : }
    1391             : 
    1392             : /* Checks if f, monic squarefree ZX with |constant coeff| = 1, is a cyclotomic
    1393             :  * polynomial. Returns n if f = Phi_n, and 0 otherwise */
    1394             : static long
    1395        4417 : BD_iscyclo(GEN f)
    1396             : {
    1397        4417 :   pari_sp av = avma;
    1398             :   GEN f2, fn, f1;
    1399             : 
    1400        4417 :   if (degpol(f) == 1) return isint1(gel(f,2))? 2: 1;
    1401        4165 :   f1 = ZX_graeffe(f);
    1402             :   /* f = product of Phi_n, n odd */
    1403        4165 :   if (ZX_equal(f, f1)) return gc_long(av, BD_odd_iscyclo(f));
    1404             : 
    1405        3535 :   fn = ZX_z_unscale(f, -1); /* f(-x) */
    1406             :   /* f = product of Phi_n, n = 2 mod 4 */
    1407        3535 :   if (ZX_equal(f1, fn)) return gc_long(av, 2*BD_odd_iscyclo(fn));
    1408             : 
    1409        3136 :   if (issquareall(f1, &f2))
    1410             :   {
    1411         910 :     GEN lt = leading_coeff(f2);
    1412             :     long c;
    1413         910 :     if (signe(lt) < 0) f2 = ZX_neg(f2);
    1414         910 :     c = BD_iscyclo(f2);
    1415         910 :     return odd(c)? 0: 2*c;
    1416             :   }
    1417        2226 :   return gc_long(av, 0);
    1418             : }
    1419             : long
    1420        6510 : poliscyclo(GEN f)
    1421             : {
    1422             :   long d;
    1423        6510 :   if (typ(f) != t_POL) pari_err_TYPE("poliscyclo", f);
    1424        6503 :   d = degpol(f);
    1425        6503 :   if (d <= 0 || !RgX_is_ZX(f)) return 0;
    1426        6496 :   if (!equali1(gel(f,d+2)) || !is_pm1(gel(f,2))) return 0;
    1427        3570 :   if (d == 1) return signe(gel(f,2)) > 0? 2: 1;
    1428        3507 :   return ZX_is_squarefree(f)? BD_iscyclo(f): 0;
    1429             : }
    1430             : 
    1431             : long
    1432        1029 : poliscycloprod(GEN f)
    1433             : {
    1434        1029 :   pari_sp av = avma;
    1435        1029 :   long i, d = degpol(f);
    1436        1029 :   if (typ(f) != t_POL) pari_err_TYPE("poliscycloprod",f);
    1437        1029 :   if (!RgX_is_ZX(f)) return 0;
    1438        1029 :   if (!ZX_is_monic(f) || !is_pm1(constant_coeff(f))) return 0;
    1439        1029 :   if (d < 2) return (d == 1);
    1440        1022 :   if ( degpol(ZX_gcd_all(f, ZX_deriv(f), &f)) )
    1441             :   {
    1442          14 :     d = degpol(f);
    1443          14 :     if (d == 1) return 1;
    1444             :   }
    1445        1015 :   f = BD(f); if (!f) return 0;
    1446        3619 :   for (i = lg(f)-1; i; i--) d -= degpol(gel(f,i));
    1447        1015 :   return gc_long(av, d == 0);
    1448             : }

Generated by: LCOV version 1.13