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 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.10.0 lcov report (development 19837-cc815bb) Lines: 719 750 95.9 %
Date: 2016-12-10 05:49:10 Functions: 41 42 97.6 %
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     6264115 : 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     6264115 :   dy=degpol(y);
      26     6264115 :   dx=degpol(x);
      27     6264115 :   dz=dx-dy; if (dz<0) return NULL;
      28     6263926 :   z=cgetg(dz+3,t_POL); z[1] = x[1];
      29     6263926 :   x += 2; y += 2; z += 2;
      30     6263926 :   y_lead = gel(y,dy);
      31     6263926 :   if (equali1(y_lead)) y_lead = NULL;
      32             : 
      33     6263926 :   p1 = gel(x,dx);
      34     6263926 :   if (y_lead) {
      35             :     GEN r;
      36        3551 :     p1 = dvmdii(p1,y_lead, &r);
      37        3551 :     if (r != gen_0) return NULL;
      38             :   }
      39     6260375 :   else p1 = icopy(p1);
      40     6263926 :   gel(z,dz) = p1;
      41     6962331 :   for (i=dx-1; i>=dy; i--)
      42             :   {
      43      698699 :     av = avma; p1 = gel(x,i);
      44     2370867 :     for (j=i-dy+1; j<=i && j<=dz; j++)
      45     1672168 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
      46      698699 :     if (y_lead) {
      47             :       GEN r;
      48       14399 :       p1 = dvmdii(p1,y_lead, &r);
      49       14399 :       if (r != gen_0) return NULL;
      50             :     }
      51      698664 :     if (B && abscmpii(p1, B) > 0) return NULL;
      52      698405 :     p1 = gerepileuptoint(av, p1);
      53      698405 :     gel(z,i-dy) = p1;
      54             :   }
      55     6263632 :   av = avma;
      56    15731385 :   for (; i >= 0; i--)
      57             :   {
      58     9505588 :     p1 = gel(x,i);
      59             :     /* we always enter this loop at least once */
      60    19992347 :     for (j=0; j<=i && j<=dz; j++)
      61    10486759 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
      62     9505588 :     if (signe(p1)) return NULL;
      63     9467753 :     avma = av;
      64             :   }
      65     6225797 :   return z - 2;
      66             : }
      67             : static GEN
      68     6261560 : 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(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        6112 : Mignotte_bound(GEN S)
     107             : {
     108        6112 :   long i, d = degpol(S);
     109        6112 :   GEN C, N2, t, binlS, lS = leading_coeff(S), bin = vecbinome(d-1);
     110             : 
     111        6112 :   N2 = sqrtr(RgX_fpnorml2(S,DEFAULTPREC));
     112        6112 :   binlS = is_pm1(lS)? bin: ZC_Z_mul(bin, lS);
     113             : 
     114             :   /* i = 0 */
     115        6112 :   C = gel(binlS,1);
     116             :   /* i = d */
     117        6112 :   t = N2; if (gcmp(C, t) < 0) C = t;
     118       85090 :   for (i = 1; i < d; i++)
     119             :   {
     120       78978 :     t = addri(mulir(gel(bin,i), N2), gel(binlS,i+1));
     121       78978 :     if (mpcmp(C, t) < 0) C = t;
     122             :   }
     123        6112 :   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        6112 : Beauzamy_bound(GEN S)
     129             : {
     130        6112 :   const long prec = DEFAULTPREC;
     131        6112 :   long i, d = degpol(S);
     132             :   GEN bin, lS, s, C;
     133        6112 :   bin = vecbinome(d);
     134             : 
     135        6112 :   s = real_0(prec);
     136       97314 :   for (i=0; i<=d; i++)
     137             :   {
     138       91202 :     GEN c = gel(S,i+2);
     139       91202 :     if (gequal0(c)) continue;
     140             :     /* s += P_i^2 / binomial(d,i) */
     141       75116 :     s = addrr(s, divri(itor(sqri(c), prec), gel(bin,i+1)));
     142             :   }
     143             :   /* s = [S]_2^2 */
     144        6112 :   C = powruhalf(stor(3,prec), 3 + 2*d); /* 3^{3/2 + d} */
     145        6112 :   C = divrr(mulrr(C, s), mulur(4*d, mppi(prec)));
     146        6112 :   lS = absi(leading_coeff(S));
     147        6112 :   return mulir(lS, sqrtr(C));
     148             : }
     149             : 
     150             : static GEN
     151        6112 : factor_bound(GEN S)
     152             : {
     153        6112 :   pari_sp av = avma;
     154        6112 :   GEN a = Mignotte_bound(S);
     155        6112 :   GEN b = Beauzamy_bound(S);
     156        6112 :   if (DEBUGLEVEL>2)
     157             :   {
     158           0 :     err_printf("Mignotte bound: %Ps\n",a);
     159           0 :     err_printf("Beauzamy bound: %Ps\n",b);
     160             :   }
     161        6112 :   return gerepileupto(av, ceil_safe(gmin(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        4929 : cmbf(GEN pol, GEN famod, GEN bound, GEN p, long a, long b,
     173             :      long klim, long *pmaxK, int *done)
     174             : {
     175        4929 :   long K = 1, cnt = 1, i,j,k, curdeg, lfamod = lg(famod)-1;
     176             :   ulong spa_b, spa_bs2, Sbound;
     177        4929 :   GEN lc, lcpol, pa = powiu(p,a), pas2 = shifti(pa,-1);
     178        4929 :   GEN trace1   = cgetg(lfamod+1, t_VECSMALL);
     179        4929 :   GEN trace2   = cgetg(lfamod+1, t_VECSMALL);
     180        4929 :   GEN ind      = cgetg(lfamod+1, t_VECSMALL);
     181        4929 :   GEN deg      = cgetg(lfamod+1, t_VECSMALL);
     182        4929 :   GEN degsofar = cgetg(lfamod+1, t_VECSMALL);
     183        4929 :   GEN listmod  = cgetg(lfamod+1, t_VEC);
     184        4929 :   GEN fa       = cgetg(lfamod+1, t_VEC);
     185             : 
     186        4929 :   *pmaxK = cmbf_maxK(lfamod);
     187        4929 :   lc = absi(leading_coeff(pol));
     188        4929 :   if (is_pm1(lc)) lc = NULL;
     189        4929 :   lcpol = lc? ZX_Z_mul(pol, lc): pol;
     190             : 
     191             :   {
     192        4929 :     GEN pa_b,pa_bs2,pb, lc2 = lc? sqri(lc): NULL;
     193             : 
     194        4929 :     pa_b = powiu(p, a-b); /* < 2^31 */
     195        4929 :     pa_bs2 = shifti(pa_b,-1);
     196        4929 :     pb= powiu(p, b);
     197       19645 :     for (i=1; i <= lfamod; i++)
     198             :     {
     199       14716 :       GEN T1,T2, P = gel(famod,i);
     200       14716 :       long d = degpol(P);
     201             : 
     202       14716 :       deg[i] = d; P += 2;
     203       14716 :       T1 = gel(P,d-1);/* = - S_1 */
     204       14716 :       T2 = sqri(T1);
     205       14716 :       if (d > 1) T2 = subii(T2, shifti(gel(P,d-2),1));
     206       14716 :       T2 = modii(T2, pa); /* = S_2 Newton sum */
     207       14716 :       if (lc)
     208             :       {
     209         658 :         T1 = Fp_mul(lc, T1, pa);
     210         658 :         T2 = Fp_mul(lc2,T2, pa);
     211             :       }
     212       14716 :       uel(trace1,i) = itou(diviiround(T1, pb));
     213       14716 :       uel(trace2,i) = itou(diviiround(T2, pb));
     214             :     }
     215        4929 :     spa_b   = uel(pa_b,2); /* < 2^31 */
     216        4929 :     spa_bs2 = uel(pa_bs2,2); /* < 2^31 */
     217             :   }
     218        4929 :   degsofar[0] = 0; /* sentinel */
     219             : 
     220             :   /* ind runs through strictly increasing sequences of length K,
     221             :    * 1 <= ind[i] <= lfamod */
     222             : nextK:
     223        9914 :   if (K > *pmaxK || 2*K > lfamod) goto END;
     224        5965 :   if (DEBUGLEVEL > 3)
     225           0 :     err_printf("\n### K = %d, %Ps combinations\n", K,binomial(utoipos(lfamod), K));
     226        5965 :   setlg(ind, K+1); ind[1] = 1;
     227        5965 :   Sbound = (ulong) ((K+1)>>1);
     228        5965 :   i = 1; curdeg = deg[ind[1]];
     229             :   for(;;)
     230             :   { /* try all combinations of K factors */
     231      365472 :     for (j = i; j < K; j++)
     232             :     {
     233       44632 :       degsofar[j] = curdeg;
     234       44632 :       ind[j+1] = ind[j]+1; curdeg += deg[ind[j+1]];
     235             :     }
     236      320840 :     if (curdeg <= klim) /* trial divide */
     237             :     {
     238             :       GEN y, q, list;
     239             :       pari_sp av;
     240             :       ulong t;
     241             : 
     242             :       /* d - 1 test */
     243      933116 :       for (t=uel(trace1,ind[1]),i=2; i<=K; i++)
     244      612276 :         t = Fl_add(t, uel(trace1,ind[i]), spa_b);
     245      320840 :       if (t > spa_bs2) t = spa_b - t;
     246      320840 :       if (t > Sbound)
     247             :       {
     248      285567 :         if (DEBUGLEVEL>6) err_printf(".");
     249      285567 :         goto NEXT;
     250             :       }
     251             :       /* d - 2 test */
     252      112784 :       for (t=uel(trace2,ind[1]),i=2; i<=K; i++)
     253       77511 :         t = Fl_add(t, uel(trace2,ind[i]), spa_b);
     254       35273 :       if (t > spa_bs2) t = spa_b - t;
     255       35273 :       if (t > Sbound)
     256             :       {
     257       22134 :         if (DEBUGLEVEL>6) err_printf("|");
     258       22134 :         goto NEXT;
     259             :       }
     260             : 
     261       13139 :       av = avma;
     262             :       /* check trailing coeff */
     263       13139 :       y = lc;
     264       56357 :       for (i=1; i<=K; i++)
     265             :       {
     266       43218 :         GEN q = constant_coeff(gel(famod,ind[i]));
     267       43218 :         if (y) q = mulii(y, q);
     268       43218 :         y = centermodii(q, pa, pas2);
     269             :       }
     270       13139 :       if (!signe(y) || remii(constant_coeff(lcpol), y) != gen_0)
     271             :       {
     272       10724 :         if (DEBUGLEVEL>3) err_printf("T");
     273       10724 :         avma = av; goto NEXT;
     274             :       }
     275        2415 :       y = lc; /* full computation */
     276        5313 :       for (i=1; i<=K; i++)
     277             :       {
     278        2898 :         GEN q = gel(famod,ind[i]);
     279        2898 :         if (y) q = gmul(y, q);
     280        2898 :         y = centermod_i(q, pa, pas2);
     281             :       }
     282             : 
     283             :       /* y is the candidate factor */
     284        2415 :       if (! (q = ZX_divides_i(lcpol,y,bound)) )
     285             :       {
     286         259 :         if (DEBUGLEVEL>3) err_printf("*");
     287         259 :         avma = av; goto NEXT;
     288             :       }
     289             :       /* found a factor */
     290        2156 :       list = cgetg(K+1, t_VEC);
     291        2156 :       gel(listmod,cnt) = list;
     292        2156 :       for (i=1; i<=K; i++) list[i] = famod[ind[i]];
     293             : 
     294        2156 :       y = Q_primpart(y);
     295        2156 :       gel(fa,cnt++) = y;
     296             :       /* fix up pol */
     297        2156 :       pol = q;
     298        2156 :       if (lc) pol = Q_div_to_int(pol, leading_coeff(y));
     299       10227 :       for (i=j=k=1; i <= lfamod; i++)
     300             :       { /* remove used factors */
     301        8071 :         if (j <= K && i == ind[j]) j++;
     302             :         else
     303             :         {
     304        5628 :           gel(famod,k) = gel(famod,i);
     305        5628 :           uel(trace1,k) = uel(trace1,i);
     306        5628 :           uel(trace2,k) = uel(trace2,i);
     307        5628 :           deg[k] = deg[i]; k++;
     308             :         }
     309             :       }
     310        2156 :       lfamod -= K;
     311        2156 :       *pmaxK = cmbf_maxK(lfamod);
     312        2156 :       if (lfamod < 2*K) goto END;
     313        1176 :       i = 1; curdeg = deg[ind[1]];
     314        1176 :       bound = factor_bound(pol);
     315        1176 :       if (lc) lc = absi(leading_coeff(pol));
     316        1176 :       lcpol = lc? ZX_Z_mul(pol, lc): pol;
     317        1176 :       if (DEBUGLEVEL>3)
     318           0 :         err_printf("\nfound factor %Ps\nremaining modular factor(s): %ld\n",
     319             :                    y, lfamod);
     320        1176 :       continue;
     321             :     }
     322             : 
     323             : NEXT:
     324      318684 :     for (i = K+1;;)
     325             :     {
     326      368014 :       if (--i == 0) { K++; goto nextK; }
     327      363029 :       if (++ind[i] <= lfamod - K + i)
     328             :       {
     329      313699 :         curdeg = degsofar[i-1] + deg[ind[i]];
     330      313699 :         if (curdeg <= klim) break;
     331             :       }
     332       49330 :     }
     333      314875 :   }
     334             : END:
     335        4929 :   *done = 1;
     336        4929 :   if (degpol(pol) > 0)
     337             :   { /* leftover factor */
     338        4929 :     if (signe(leading_coeff(pol)) < 0) pol = ZX_neg(pol);
     339        4929 :     if (lfamod >= 2*K) *done = 0;
     340             : 
     341        4929 :     setlg(famod, lfamod+1);
     342        4929 :     gel(listmod,cnt) = leafcopy(famod);
     343        4929 :     gel(fa,cnt++) = pol;
     344             :   }
     345        4929 :   if (DEBUGLEVEL>6) err_printf("\n");
     346        4929 :   setlg(listmod, cnt);
     347        4929 :   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       76162 : shifteval(GEN Q, long n)
     355             : {
     356       76162 :   long i, l = lg(Q);
     357             :   GEN s;
     358             : 
     359       76162 :   if (!signe(Q)) return gen_0;
     360       76162 :   s = gel(Q,l-1);
     361       76162 :   for (i = l-2; i > 1; i--) s = addii(gel(Q,i), shifti(s, n));
     362       76162 :   return s;
     363             : }
     364             : 
     365             : /* return integer y such that all |a| <= y if P(a) = 0 */
     366             : static GEN
     367       44353 : root_bound(GEN P0)
     368             : {
     369       44353 :   GEN Q = leafcopy(P0), lP = absi(leading_coeff(Q)), x,y,z;
     370       44353 :   long k, d = degpol(Q);
     371             : 
     372             :   /* P0 = lP x^d + Q, deg Q < d */
     373       44353 :   Q = normalizepol_lg(Q, d+2);
     374       44353 :   for (k=lg(Q)-1; k>1; k--) gel(Q,k) = absi(gel(Q,k));
     375       44353 :   k = (long)(fujiwara_bound(P0));
     376       76253 :   for (  ; k >= 0; k--)
     377             :   {
     378       76162 :     pari_sp av = avma;
     379             :     /* y = 2^k; Q(y) >= lP y^d ? */
     380       76162 :     if (cmpii(shifteval(Q,k), shifti(lP, d*k)) >= 0) break;
     381       31900 :     avma = av;
     382             :   }
     383       44353 :   if (k < 0) k = 0;
     384       44353 :   x = int2n(k);
     385       44353 :   y = int2n(k+1);
     386      292107 :   for(k=0; ; k++)
     387             :   {
     388      292107 :     z = shifti(addii(x,y), -1);
     389      292107 :     if (equalii(x,z) || k > 5) break;
     390      247754 :     if (cmpii(poleval(Q,z), mulii(lP, powiu(z, d))) < 0)
     391      132175 :       y = z;
     392             :     else
     393      115579 :       x = z;
     394      247754 :   }
     395       44353 :   return y;
     396             : }
     397             : 
     398             : GEN
     399         203 : chk_factors_get(GEN lt, GEN famod, GEN c, GEN T, GEN N)
     400             : {
     401         203 :   long i = 1, j, l = lg(famod);
     402         203 :   GEN V = cgetg(l, t_VEC);
     403        5677 :   for (j = 1; j < l; j++)
     404        5474 :     if (signe(gel(c,j))) gel(V,i++) = gel(famod,j);
     405         203 :   if (lt && i > 1) gel(V,1) = RgX_Rg_mul(gel(V,1), lt);
     406         203 :   setlg(V, i);
     407         203 :   return T? FpXQXV_prod(V, T, N): FpXV_prod(V,N);
     408             : }
     409             : 
     410             : static GEN
     411         119 : chk_factors(GEN P, GEN M_L, GEN bound, GEN famod, GEN pa)
     412             : {
     413             :   long i, r;
     414         119 :   GEN pol = P, list, piv, y, ltpol, lt, paov2;
     415             : 
     416         119 :   piv = ZM_hnf_knapsack(M_L);
     417         119 :   if (!piv) return NULL;
     418          49 :   if (DEBUGLEVEL>7) err_printf("ZM_hnf_knapsack output:\n%Ps\n",piv);
     419             : 
     420          49 :   r  = lg(piv)-1;
     421          49 :   list = cgetg(r+1, t_VEC);
     422          49 :   lt = absi(leading_coeff(pol));
     423          49 :   if (is_pm1(lt)) lt = NULL;
     424          49 :   ltpol = lt? ZX_Z_mul(pol, lt): pol;
     425          49 :   paov2 = shifti(pa,-1);
     426          49 :   for (i = 1;;)
     427             :   {
     428         140 :     if (DEBUGLEVEL) err_printf("LLL_cmbf: checking factor %ld\n",i);
     429         140 :     y = chk_factors_get(lt, famod, gel(piv,i), NULL, pa);
     430         140 :     y = FpX_center(y, pa, paov2);
     431         140 :     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(leading_coeff(pol));
     440          35 :       ltpol = ZX_Z_mul(pol, lt);
     441             :     }
     442             :     else
     443          56 :       ltpol = pol;
     444          91 :   }
     445          42 :   y = Q_primpart(pol);
     446          42 :   gel(list,i) = y; return list;
     447             : }
     448             : 
     449             : GEN
     450        1085 : 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        1085 :   if (DEBUGLEVEL>2) timer_start(&T);
     457        1085 :   u = ZM_lll_norms(m, final? 0.999: 0.75, LLL_INPLACE, &norm);
     458        1085 :   if (DEBUGLEVEL>2) *ti_LLL += timer_delay(&T);
     459        5824 :   for (R=lg(m)-1; R > 0; R--)
     460        5824 :     if (cmprr(gel(norm,R), Bnorm) < 0) break;
     461        1085 :   for (i=1; i<=R; i++) setlg(u[i], n0+1);
     462        1085 :   if (R <= 1)
     463             :   {
     464          84 :     if (!R) pari_err_BUG("LLL_cmbf [no factor]");
     465          84 :     return NULL; /* irreducible */
     466             :   }
     467        1001 :   setlg(u, R+1); return u;
     468             : }
     469             : 
     470             : static ulong
     471          14 : next2pow(ulong a)
     472             : {
     473          14 :   ulong b = 1;
     474          14 :   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          91 : LLL_cmbf(GEN P, GEN famod, GEN p, GEN pa, GEN bound, long a, long rec)
     487             : {
     488          91 :   const long N0 = 1; /* # of traces added at each step */
     489          91 :   double BitPerFactor = 0.4; /* nb bits in p^(a-b) / modular factor */
     490          91 :   long i,j,tmax,n0,C, dP = degpol(P);
     491          91 :   double logp = log((double)itos(p)), LOGp2 = LOG2/logp;
     492          91 :   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          91 :   long ti_LLL = 0, ti_CF  = 0;
     496             : 
     497          91 :   lP = absi(leading_coeff(P));
     498          91 :   if (is_pm1(lP)) lP = NULL;
     499          91 :   Br = root_bound(P);
     500          91 :   if (lP) Br = mulii(lP, Br);
     501          91 :   logBr = gtodouble(glog(Br, DEFAULTPREC)) / logp;
     502             : 
     503          91 :   n0 = lg(famod) - 1;
     504          91 :   C = (long)ceil( sqrt(N0 * n0 / 4.) ); /* > 1 */
     505          91 :   Bnorm = dbltor(n0 * (C*C + N0*n0/4.) * 1.00001);
     506          91 :   ZERO = zeromat(n0, N0);
     507             : 
     508          91 :   av = avma;
     509          91 :   TT = cgetg(n0+1, t_VEC);
     510          91 :   Tra  = cgetg(n0+1, t_MAT);
     511        1792 :   for (i=1; i<=n0; i++)
     512             :   {
     513        1701 :     TT[i]  = 0;
     514        1701 :     gel(Tra,i) = cgetg(N0+1, t_COL);
     515             :   }
     516          91 :   CM_L = scalarmat_s(C, n0);
     517             :   /* tmax = current number of traces used (and computed so far) */
     518         441 :   for (tmax = 0;; tmax += N0)
     519             :   {
     520         441 :     long b, bmin, bgood, delta, tnew = tmax + N0, r = lg(CM_L)-1;
     521             :     GEN M_L, q, CM_Lp, oldCM_L;
     522         441 :     int first = 1;
     523             :     pari_timer ti2, TI;
     524             : 
     525         441 :     bmin = (long)ceil(b0 + tnew*logBr);
     526         441 :     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         441 :     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          14 :       for (i=1; i<=n0; i++) TT[i] = 0;
     539             :     }
     540        8365 :     for (i=1; i<=n0; i++)
     541             :     {
     542        7924 :       GEN p1 = gel(Tra,i);
     543        7924 :       GEN p2 = polsym_gen(gel(famod,i), gel(TT,i), tnew, NULL, pa);
     544        7924 :       gel(TT,i) = p2;
     545        7924 :       p2 += 1+tmax; /* ignore traces number 0...tmax */
     546        7924 :       for (j=1; j<=N0; j++) gel(p1,j) = gel(p2,j);
     547        7924 :       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         441 :     if (DEBUGLEVEL>2) { timer_start(&ti2); timer_start(&TI); }
     560         441 :     oldCM_L = CM_L;
     561         441 :     av2 = avma;
     562         441 :     delta = b = 0; /* -Wall */
     563             : AGAIN:
     564         840 :     M_L = Q_div_to_int(CM_L, utoipos(C));
     565         840 :     T2 = centermod( ZM_mul(Tra, M_L), pa );
     566         840 :     if (first)
     567             :     { /* initialize lattice, using few p-adic digits for traces */
     568         441 :       double t = gexpo(T2) - maxdd(32.0, BitPerFactor*r);
     569         441 :       bgood = (long) (t * LOGp2);
     570         441 :       b = maxss(bmin, bgood);
     571         441 :       delta = a - b;
     572             :     }
     573             :     else
     574             :     { /* add more p-adic digits and continue reduction */
     575         399 :       long b0 = (long)(gexpo(T2) * LOGp2);
     576         399 :       if (b0 < b) b = b0;
     577         399 :       b = maxss(b-delta, bmin);
     578         399 :       if (b - delta/2 < bmin) b = bmin; /* near there. Go all the way */
     579             :     }
     580             : 
     581         840 :     q = powiu(p, b);
     582         840 :     m = vconcat( CM_L, gdivround(T2, q) );
     583         840 :     if (first)
     584             :     {
     585         441 :       GEN P1 = scalarmat(powiu(p, a-b), N0);
     586         441 :       first = 0;
     587         441 :       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         840 :     CM_L = LLL_check_progress(Bnorm, n0, m, b == bmin, /*dbg:*/ &ti_LLL);
     595         840 :     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         931 :     if (!CM_L) { list = mkvec(P); break; }
     599         791 :     if (b > bmin)
     600             :     {
     601         399 :       CM_L = gerepilecopy(av2, CM_L);
     602         399 :       goto AGAIN;
     603             :     }
     604         392 :     if (DEBUGLEVEL>2) timer_printf(&ti2, "for this block of traces");
     605             : 
     606         392 :     i = lg(CM_L) - 1;
     607         392 :     if (i == r && ZM_equal(CM_L, oldCM_L))
     608             :     {
     609          21 :       CM_L = oldCM_L;
     610          21 :       avma = av2; continue;
     611             :     }
     612             : 
     613         371 :     CM_Lp = FpM_image(CM_L, utoipos(27449)); /* inexpensive test */
     614         371 :     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         371 :     if (i <= r && i*rec < n0)
     621             :     {
     622             :       pari_timer ti;
     623         119 :       if (DEBUGLEVEL>2) timer_start(&ti);
     624         119 :       list = chk_factors(P, Q_div_to_int(CM_L,utoipos(C)), bound, famod, pa);
     625         119 :       if (DEBUGLEVEL>2) ti_CF += timer_delay(&ti);
     626         119 :       if (list) break;
     627          77 :       if (DEBUGLEVEL>2) err_printf("LLL_cmbf: chk_factors failed");
     628             :     }
     629         329 :     CM_L = gerepilecopy(av2, CM_L);
     630         329 :     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         350 :   }
     636          91 :   if (DEBUGLEVEL>2)
     637           0 :     err_printf("* Time LLL: %ld\n* Time Check Factor: %ld\n",ti_LLL,ti_CF);
     638          91 :   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        4929 : cmbf_precs(GEN q, GEN A, GEN B, long *pta, long *ptb, GEN *qa, GEN *qb)
     644             : {
     645        4929 :   long a,b,amin,d = (long)(31 * LOG2/gtodouble(glog(q,DEFAULTPREC)) - 1e-5);
     646        4929 :   int fl = 0;
     647             : 
     648        4929 :   b = logintall(B, q, qb) + 1;
     649        4929 :   *qb = mulii(*qb, q);
     650        4929 :   amin = b + d;
     651        4929 :   if (gcmp(powiu(q, amin), A) <= 0)
     652             :   {
     653        1344 :     a = logintall(A, q, qa) + 1;
     654        1344 :     *qa = mulii(*qa, q);
     655        1344 :     b = a - d; *qb = powiu(q, b);
     656             :   }
     657             :   else
     658             :   { /* not enough room */
     659        3585 :     a = amin;  *qa = powiu(q, a);
     660        3585 :     fl = 1;
     661             :   }
     662        4929 :   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        4929 :   *pta = a;
     667        4929 :   *ptb = b; return fl;
     668             : }
     669             : 
     670             : /* use van Hoeij's knapsack algorithm */
     671             : static GEN
     672        4929 : combine_factors(GEN target, GEN famod, GEN p, long klim)
     673             : {
     674             :   GEN la, B, A, res, L, pa, pb, listmod;
     675        4929 :   long a,b, l, maxK, n = degpol(target);
     676             :   int done;
     677             :   pari_timer T;
     678             : 
     679        4929 :   A = factor_bound(target);
     680             : 
     681        4929 :   la = absi(leading_coeff(target));
     682        4929 :   B = mului(n, sqri(mulii(la, root_bound(target)))); /* = bound for S_2 */
     683             : 
     684        4929 :   (void)cmbf_precs(p, A, B, &a, &b, &pa, &pb);
     685             : 
     686        4929 :   if (DEBUGLEVEL>2) timer_start(&T);
     687        4929 :   famod = ZpX_liftfact(target, famod, pa, p, a);
     688        4929 :   if (DEBUGLEVEL>2) timer_printf(&T, "Hensel lift (mod %Ps^%ld)", p,a);
     689        4929 :   L = cmbf(target, famod, A, p, a, b, klim, &maxK, &done);
     690        4929 :   if (DEBUGLEVEL>2) timer_printf(&T, "Naive recombination");
     691             : 
     692        4929 :   res     = gel(L,1);
     693        4929 :   listmod = gel(L,2); l = lg(listmod)-1;
     694        4929 :   famod = gel(listmod,l);
     695        4929 :   if (maxK > 0 && lg(famod)-1 > 2*maxK)
     696             :   {
     697          91 :     if (l!=1) A = factor_bound(gel(res,l));
     698          91 :     if (DEBUGLEVEL > 4) err_printf("last factor still to be checked\n");
     699          91 :     L = LLL_cmbf(gel(res,l), famod, p, pa, A, a, maxK);
     700          91 :     if (DEBUGLEVEL>2) timer_printf(&T,"Knapsack");
     701             :     /* remove last elt, possibly unfactored. Add all new ones. */
     702          91 :     setlg(res, l); res = shallowconcat(res, L);
     703             :   }
     704        4929 :   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      597493 : pick_prime(GEN a, long fl, pari_timer *T)
     711             : {
     712      597493 :   pari_sp av = avma, av1;
     713      597493 :   const long MAXNP = 7, da = degpol(a);
     714      597493 :   long nmax = da+1, np;
     715      597493 :   ulong chosenp = 0;
     716      597493 :   GEN lead = gel(a,da+2);
     717             :   forprime_t S;
     718      597493 :   if (equali1(lead)) lead = NULL;
     719      597493 :   u_forprime_init(&S, 2, ULONG_MAX);
     720      597493 :   av1 = avma;
     721     3411595 :   for (np = 0; np < MAXNP; avma = av1)
     722             :   {
     723     3367347 :     ulong p = u_forprime_next(&S);
     724             :     long nfacp;
     725             :     GEN z;
     726             : 
     727     3367347 :     if (!p) pari_err_OVERFLOW("DDF [out of small primes]");
     728     3367347 :     if (lead && !umodiu(lead,p)) continue;
     729     3364925 :     z = ZX_to_Flx(a, p);
     730     3364925 :     if (!Flx_is_squarefree(z, p)) continue;
     731             : 
     732     2120601 :     if (fl)
     733             :     {
     734     2070355 :       nfacp = Flx_nbroots(z, p);
     735     2070355 :       if (!nfacp) { chosenp = 0; break; } /* no root */
     736             :     }
     737             :     else
     738             :     {
     739       50246 :       nfacp = Flx_nbfact(z, p);
     740       50246 :       if (nfacp == 1) { chosenp = 0; break; } /* irreducible */
     741             :     }
     742     1567370 :     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     1567370 :     if (nfacp < nmax)
     746             :     {
     747      531708 :       nmax = nfacp; chosenp = p;
     748      531708 :       if (da > 100 && nmax < 5) break; /* large degree, few factors. Enough */
     749             :     }
     750     1567356 :     np++;
     751             :   }
     752      597493 :   avma = av; return chosenp;
     753             : }
     754             : 
     755             : /* Assume A a squarefree ZX; return the vector of its rational roots */
     756             : static GEN
     757      581910 : 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      581910 :   if (DEBUGLEVEL>2) timer_start(&T);
     766      581910 :   pp = pick_prime(A, 1, &T);
     767      581910 :   if (!pp) return cgetg(1,t_VEC); /* no root */
     768       39333 :   p = utoipos(pp);
     769       39333 :   lc = leading_coeff(A);
     770       39333 :   if (is_pm1(lc))
     771       38332 :   { lc = NULL; lcpol = A; }
     772             :   else
     773        1001 :   { lc = absi_shallow(lc); lcpol = ZX_Z_mul(A, lc); }
     774       39333 :   bound = root_bound(A); if (lc) bound = mulii(lc, bound);
     775       39333 :   e = logintall(addiu(shifti(bound, 1), 1), p, &pe) + 1;
     776       39333 :   pe = mulii(pe, p);
     777       39333 :   pes2 = shifti(pe, -1);
     778       39333 :   if (DEBUGLEVEL>2) timer_printf(&T, "Root bound");
     779       39333 :   av = avma;
     780       39333 :   z = ZpX_roots(A, p, e); lz = lg(z);
     781       39333 :   z = deg1_from_roots(z, varn(A));
     782       39333 :   if (DEBUGLEVEL>2) timer_printf(&T, "Hensel lift (mod %lu^%ld)", pp,e);
     783       79555 :   for (m=1, i=1; i < lz; i++)
     784             :   {
     785       40222 :     GEN q, r, y = gel(z,i);
     786       40222 :     if (lc) y = ZX_Z_mul(y, lc);
     787       40222 :     y = centermod_i(y, pe, pes2);
     788       40222 :     if (! (q = ZX_divides(lcpol, y)) ) continue;
     789             : 
     790        2387 :     lcpol = q;
     791        2387 :     r = negi( constant_coeff(y) );
     792        2387 :     if (lc) {
     793        1414 :       r = gdiv(r,lc);
     794        1414 :       lcpol = Q_primpart(lcpol);
     795        1414 :       lc = absi_shallow( leading_coeff(lcpol) );
     796        1414 :       if (is_pm1(lc)) lc = NULL; else lcpol = ZX_Z_mul(lcpol, lc);
     797             :     }
     798        2387 :     gel(z,m++) = r;
     799        2387 :     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       39333 :   if (DEBUGLEVEL>2) timer_printf(&T, "Recombination");
     806       39333 :   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       15583 : DDF(GEN a)
     813             : {
     814             :   GEN ap, prime, famod, z;
     815       15583 :   long ti = 0;
     816       15583 :   ulong p = 0;
     817       15583 :   pari_sp av = avma;
     818             :   pari_timer T, T2;
     819             : 
     820       15583 :   if (DEBUGLEVEL>2) { timer_start(&T); timer_start(&T2); }
     821       15583 :   p = pick_prime(a, 0, &T2);
     822       15583 :   if (!p) return mkvec(a);
     823        4929 :   prime = utoipos(p);
     824        4929 :   ap = Flx_normalize(ZX_to_Flx(a, p), p);
     825        4929 :   famod = gel(Flx_factor(ap, p), 1);
     826        4929 :   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        4929 :   z = combine_factors(a, FlxV_to_ZXV(famod), prime, degpol(a)-1);
     833        4929 :   if (DEBUGLEVEL>2)
     834           0 :     err_printf("Total Time: %ld\n===========\n", ti + timer_delay(&T));
     835        4929 :   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       11026 : ZX_DDF(GEN x)
     842             : {
     843             :   GEN L;
     844             :   long m;
     845       11026 :   x = ZX_deflate_max(x, &m);
     846       11026 :   L = DDF(x);
     847       11026 :   if (m > 1)
     848             :   {
     849        3738 :     GEN e, v, fa = factoru(m);
     850             :     long i,j,k, l;
     851             : 
     852        3738 :     e = gel(fa,2); k = 0;
     853        3738 :     fa= gel(fa,1); l = lg(fa);
     854        3738 :     for (i=1; i<l; i++) k += e[i];
     855        3738 :     v = cgetg(k+1, t_VECSMALL); k = 1;
     856        7609 :     for (i=1; i<l; i++)
     857        3871 :       for (j=1; j<=e[i]; j++) v[k++] = fa[i];
     858        8218 :     for (k--; k; k--)
     859             :     {
     860        4480 :       GEN L2 = cgetg(1,t_VEC);
     861        9037 :       for (i=1; i < lg(L); i++)
     862        4557 :               L2 = shallowconcat(L2, DDF(RgX_inflate(gel(L,i), v[k])));
     863        4480 :       L = L2;
     864             :     }
     865             :   }
     866       11026 :   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        8207 : ZX_squff(GEN f, GEN *ex)
     873             : {
     874             :   GEN T, V, P, e;
     875             :   long i, k, n, val;
     876             : 
     877        8207 :   if (signe(leading_coeff(f)) < 0) f = gneg_i(f);
     878        8207 :   val = ZX_valrem(f, &f);
     879        8207 :   n = 1 + degpol(f); if (val) n++;
     880        8207 :   e = cgetg(n,t_VECSMALL);
     881        8207 :   P = cgetg(n,t_COL);
     882             : 
     883        8207 :   T = ZX_gcd_all(f, ZX_deriv(f), &V);
     884        8550 :   for (k=i=1;; k++)
     885             :   {
     886        8550 :     pari_sp av = avma;
     887        8550 :     GEN W = ZX_gcd_all(T,V, &T);
     888        8550 :     long dW = degpol(W);
     889             :     /* W = prod P^e, e > k; V = prod P^e, e >= k */
     890        8550 :     if (dW == degpol(V)) /* V | T */
     891             :     {
     892             :       GEN U;
     893        1232 :       if (!dW) { avma = av; break; }
     894         217 :       while ( (U = ZX_divides(T, V)) ) { k++; T = U; }
     895         217 :       T = gerepileupto(av, T);
     896             :     }
     897             :     else
     898             :     {
     899        7318 :       gel(P,i) = Q_primpart(RgX_div(V,W));
     900        7318 :       e[i] = k; i++;
     901        7318 :       if (!dW) break;
     902         126 :       V = W;
     903             :     }
     904         343 :   }
     905        8207 :   if (val) { gel(P,i) = pol_x(varn(f)); e[i] = val; i++;}
     906        8207 :   setlg(P,i);
     907        8207 :   setlg(e,i); *ex = e; return P;
     908             : }
     909             : 
     910             : static GEN
     911        2787 : fact_from_DDF(GEN fa, GEN e, long n)
     912             : {
     913        2787 :   GEN v,w, y = cgetg(3, t_MAT);
     914        2787 :   long i,j,k, l = lg(fa);
     915             : 
     916        2787 :   v = cgetg(n+1,t_COL); gel(y,1) = v;
     917        2787 :   w = cgetg(n+1,t_COL); gel(y,2) = w;
     918        5840 :   for (k=i=1; i<l; i++)
     919             :   {
     920        3053 :     GEN L = gel(fa,i), ex = utoipos(e[i]);
     921        3053 :     long J = lg(L);
     922        7982 :     for (j=1; j<J; j++,k++)
     923             :     {
     924        4929 :       gel(v,k) = gcopy(gel(L,j));
     925        4929 :       gel(w,k) = ex;
     926             :     }
     927             :   }
     928        2787 :   return y;
     929             : }
     930             : 
     931             : /* Factor x in Z[t] */
     932             : static GEN
     933        2787 : ZX_factor_i(GEN x)
     934             : {
     935             :   GEN fa,ex,y;
     936             :   long n,i,l;
     937             : 
     938        2787 :   if (!signe(x)) return prime_fact(x);
     939        2787 :   fa = ZX_squff(x, &ex);
     940        2787 :   l = lg(fa); n = 0;
     941        5840 :   for (i=1; i<l; i++)
     942             :   {
     943        3053 :     gel(fa,i) = ZX_DDF(gel(fa,i));
     944        3053 :     n += lg(gel(fa,i))-1;
     945             :   }
     946        2787 :   y = fact_from_DDF(fa,ex,n);
     947        2787 :   return sort_factor_pol(y, cmpii);
     948             : }
     949             : GEN
     950        2472 : ZX_factor(GEN x)
     951             : {
     952        2472 :   pari_sp av = avma;
     953        2472 :   return gerepileupto(av, ZX_factor_i(x));
     954             : }
     955             : GEN
     956         315 : QX_factor(GEN x)
     957             : {
     958         315 :   pari_sp av = avma;
     959         315 :   return gerepileupto(av, ZX_factor_i(Q_primpart(x)));
     960             : }
     961             : 
     962             : long
     963        8981 : ZX_is_irred(GEN x)
     964             : {
     965        8981 :   pari_sp av = avma;
     966        8981 :   long l = lg(x);
     967             :   GEN y;
     968        8981 :   if (l <= 3) return 0; /* degree < 1 */
     969        8981 :   if (l == 4) return 1; /* degree 1 */
     970        7714 :   if (ZX_val(x)) return 0;
     971        7511 :   if (!ZX_is_squarefree(x)) return 0;
     972        7469 :   y = ZX_DDF(x); avma = av;
     973        7469 :   return (lg(y) == 2);
     974             : }
     975             : 
     976             : GEN
     977      581910 : nfrootsQ(GEN x)
     978             : {
     979      581910 :   pari_sp av = avma;
     980             :   GEN z;
     981             :   long val;
     982             : 
     983      581910 :   if (typ(x)!=t_POL) pari_err_TYPE("nfrootsQ",x);
     984      581910 :   if (!signe(x)) pari_err_ROOTS0("nfrootsQ");
     985      581910 :   x = Q_primpart(x);
     986      581910 :   RgX_check_ZX(x,"nfrootsQ");
     987      581910 :   val = ZX_valrem(x, &x);
     988      581910 :   z = DDF_roots( ZX_radical(x) );
     989      581910 :   if (val) z = shallowconcat(z, gen_0);
     990      581910 :   return gerepileupto(av, sort(z));
     991             : }
     992             : 
     993             : /************************************************************************
     994             :  *                   GCD OVER Z[X] / Q[X]                               *
     995             :  ************************************************************************/
     996             : int
     997       16884 : ZX_is_squarefree(GEN x)
     998             : {
     999       16884 :   pari_sp av = avma;
    1000             :   GEN d;
    1001             :   long m;
    1002             :   int r;
    1003       16884 :   if (lg(x) == 2) return 0;
    1004       16884 :   m = ZX_deflate_order(x);
    1005       16884 :   if (m > 1)
    1006             :   {
    1007        5117 :     if (!signe(gel(x,2))) return 0;
    1008        5054 :     x = RgX_deflate(x, m);
    1009             :   }
    1010       16821 :   d = ZX_gcd(x,ZX_deriv(x));
    1011       16821 :   r = (lg(d) == 3); avma = av; return r;
    1012             : }
    1013             : 
    1014             : #if 0
    1015             : /* ceil( || p ||_oo / lc(p) ) */
    1016             : static GEN
    1017             : maxnorm(GEN p)
    1018             : {
    1019             :   long i, n = degpol(p), av = avma;
    1020             :   GEN x, m = gen_0;
    1021             : 
    1022             :   p += 2;
    1023             :   for (i=0; i<n; i++)
    1024             :   {
    1025             :     x = gel(p,i);
    1026             :     if (abscmpii(x,m) > 0) m = x;
    1027             :   }
    1028             :   m = divii(m, gel(p,n));
    1029             :   return gerepileuptoint(av, addis(absi(m),1));
    1030             : }
    1031             : #endif
    1032             : 
    1033             : /* A, B in Z[X] */
    1034             : GEN
    1035     4099615 : ZX_gcd_all(GEN A, GEN B, GEN *Anew)
    1036             : {
    1037             :   GEN R, a, b, q, H, Hp, g, Ag, Bg;
    1038     4099615 :   long m, n, valX, valA, vA = varn(A);
    1039             :   ulong p;
    1040             :   int small;
    1041             :   pari_sp ltop, av;
    1042             :   forprime_t S;
    1043             : 
    1044     4099615 :   if (!signe(A)) { if (Anew) *Anew = pol_0(vA); return ZX_copy(B); }
    1045     4099615 :   if (!signe(B)) { if (Anew) *Anew = pol_1(vA); return ZX_copy(A); }
    1046     4098516 :   valA = ZX_valrem(A, &A);
    1047     4098516 :   valX = minss(valA, ZX_valrem(B, &B));
    1048     4098516 :   ltop = avma;
    1049             : 
    1050     4098516 :   n = 1 + minss(degpol(A), degpol(B)); /* > degree(gcd) */
    1051     4098516 :   g = gcdii(leading_coeff(A), leading_coeff(B)); /* multiple of lead(gcd) */
    1052     4098516 :   if (is_pm1(g)) {
    1053     4091990 :     g = NULL;
    1054     4091990 :     Ag = A;
    1055     4091990 :     Bg = B;
    1056             :   } else {
    1057        6526 :     Ag = ZX_Z_mul(A,g);
    1058        6526 :     Bg = ZX_Z_mul(B,g);
    1059             :   }
    1060     4098516 :   small = (ZX_max_lg(A) == 3 && ZX_max_lg(B) == 3);
    1061     4098516 :   init_modular_big(&S);
    1062     4098516 :   av = avma;
    1063     4098516 :   R = NULL;/*-Wall*/
    1064     4098516 :   H = NULL;
    1065     8197490 :   while ((p = u_forprime_next(&S)))
    1066             :   {
    1067     4098974 :     if (g && !umodiu(g,p)) continue;
    1068     4098974 :     a = ZX_to_Flx(A, p);
    1069     4098974 :     b = ZX_to_Flx(B, p); Hp = Flx_gcd(a,b, p);
    1070     4098974 :     m = degpol(Hp);
    1071     4098974 :     if (m == 0) { /* coprime. DONE */
    1072      987994 :       avma = ltop;
    1073      987994 :       if (Anew) {
    1074      605179 :         if (valA != valX) A = RgX_shift(A, valA - valX);
    1075      605179 :         *Anew = A;
    1076             :       }
    1077      987994 :       return pol_xn(valX, vA);
    1078             :     }
    1079     3110980 :     if (m > n) continue; /* p | Res(A/G, B/G). Discard */
    1080             : 
    1081     3110980 :     if (!g) /* make sure lead(H) = g mod p */
    1082     3110016 :       Hp = Flx_normalize(Hp, p);
    1083             :     else
    1084             :     {
    1085         964 :       ulong t = Fl_mul(umodiu(g, p), Fl_inv(Hp[m+2],p), p);
    1086         964 :       Hp = Flx_Fl_mul(Hp, t, p);
    1087             :     }
    1088     3110980 :     if (m < n)
    1089             :     { /* First time or degree drop [all previous p were as above; restart]. */
    1090     3110522 :       H = ZX_init_CRT(Hp,p,vA);
    1091     3110522 :       q = utoipos(p); n = m;
    1092     3110522 :       if (!small) continue; /* if gcd is small, try our luck */
    1093             :     }
    1094             :     else
    1095         458 :       if (!ZX_incremental_CRT(&H, Hp, &q, p)) continue;
    1096     3110522 :     if (DEBUGLEVEL>5) err_printf("gcd mod %lu (bound 2^%ld)\n", p,expi(q));
    1097             :     /* H stable: check divisibility */
    1098     3110522 :     if (!ZX_divides(Bg, H)) continue;
    1099     3110522 :     R = ZX_divides(Ag, H);
    1100     3110522 :     if (R) break;
    1101             : 
    1102           0 :     if (gc_needed(av,1))
    1103             :     {
    1104           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"QX_gcd");
    1105           0 :       gerepileall(av, 3, &H, &q, &Hp);
    1106             :     }
    1107             :   }
    1108     3110522 :   if (!p) pari_err_OVERFLOW("ZX_gcd_all [ran out of primes]");
    1109     3110522 :   if (Anew) {
    1110       11320 :     A = R;
    1111       11320 :     if (valA != valX) A = RgX_shift(A, valA - valX);
    1112       11320 :     *Anew = A;
    1113             :   }
    1114     3110522 :   return valX ? RgX_shift(H, valX): H;
    1115             : }
    1116             : GEN
    1117     3482024 : ZX_gcd(GEN A, GEN B) { return ZX_gcd_all(A,B,NULL); }
    1118             : GEN
    1119      597600 : ZX_radical(GEN A) { GEN B; (void)ZX_gcd_all(A,ZX_deriv(A),&B); return B; }
    1120             : 
    1121             : static GEN
    1122     3453427 : _gcd(GEN a, GEN b)
    1123             : {
    1124     3453427 :   if (!a) a = gen_1;
    1125     3453427 :   if (!b) b = gen_1;
    1126     3453427 :   return Q_gcd(a,b);
    1127             : }
    1128             : /* A0 and B0 in Q[X] */
    1129             : GEN
    1130     3453427 : QX_gcd(GEN A0, GEN B0)
    1131             : {
    1132             :   GEN a, b, D;
    1133     3453427 :   pari_sp av = avma, av2;
    1134             : 
    1135     3453427 :   D = ZX_gcd(Q_primitive_part(A0, &a), Q_primitive_part(B0, &b));
    1136     3453427 :   av2 = avma; a = _gcd(a,b);
    1137     3453427 :   if (isint1(a)) avma = av2; else D = RgX_Rg_mul(D, a);
    1138     3453427 :   return gerepileupto(av, D);
    1139             : }
    1140             : 
    1141             : /*****************************************************************************
    1142             :  * Variants of the Bradford-Davenport algorithm: look for cyclotomic         *
    1143             :  * factors, and decide whether a ZX is cyclotomic or a product of cyclotomic *
    1144             :  *****************************************************************************/
    1145             : /* f of degree 1, return a cyclotomic factor (Phi_1 or Phi_2) or NULL */
    1146             : static GEN
    1147           0 : BD_deg1(GEN f)
    1148             : {
    1149           0 :   GEN a = gel(f,3), b = gel(f,2); /* f = ax + b */
    1150           0 :   if (!absequalii(a,b)) return NULL;
    1151           0 :   return polcyclo((signe(a) == signe(b))? 2: 1, varn(f));
    1152             : }
    1153             : 
    1154             : /* f a squarefree ZX; not divisible by any Phi_n, n even */
    1155             : static GEN
    1156         406 : BD_odd(GEN f)
    1157             : {
    1158         812 :   while(degpol(f) > 1)
    1159             :   {
    1160         406 :     GEN f1 = ZX_graeffe(f); /* contain all cyclotomic divisors of f */
    1161         406 :     if (ZX_equal(f1, f)) return f; /* product of cyclotomics */
    1162           0 :     f = ZX_gcd(f, f1);
    1163             :   }
    1164           0 :   if (degpol(f) == 1) return BD_deg1(f);
    1165           0 :   return NULL; /* no cyclotomic divisor */
    1166             : }
    1167             : 
    1168             : static GEN
    1169        2310 : myconcat(GEN v, GEN x)
    1170             : {
    1171        2310 :   if (typ(x) != t_VEC) x = mkvec(x);
    1172        2310 :   if (!v) return x;
    1173        1470 :   return shallowconcat(v, x);
    1174             : }
    1175             : 
    1176             : /* Bradford-Davenport algorithm.
    1177             :  * f a squarefree ZX of degree > 0, return NULL or a vector of coprime
    1178             :  * cyclotomic factors of f [ possibly reducible ] */
    1179             : static GEN
    1180        2359 : BD(GEN f)
    1181             : {
    1182        2359 :   GEN G = NULL, Gs = NULL, Gp = NULL, Gi = NULL;
    1183             :   GEN fs2, fp, f2, f1, fe, fo, fe1, fo1;
    1184        2359 :   RgX_even_odd(f, &fe, &fo);
    1185        2359 :   fe1 = ZX_eval1(fe);
    1186        2359 :   fo1 = ZX_eval1(fo);
    1187        2359 :   if (absequalii(fe1, fo1)) /* f(1) = 0 or f(-1) = 0 */
    1188             :   {
    1189        1519 :     long i, v = varn(f);
    1190        1519 :     if (!signe(fe1))
    1191         371 :       G = mkvec2(polcyclo(1, v), polcyclo(2, v)); /* both 0 */
    1192        1148 :     else if (signe(fe1) == signe(fo1))
    1193         693 :       G = mkvec(polcyclo(2, v)); /*f(-1) = 0*/
    1194             :     else
    1195         455 :       G = mkvec(polcyclo(1, v)); /*f(1) = 0*/
    1196        1519 :     for (i = lg(G)-1; i; i--) f = RgX_div(f, gel(G,i));
    1197             :   }
    1198             :   /* f no longer divisible by Phi_1 or Phi_2 */
    1199        2359 :   if (degpol(f) <= 1) return G;
    1200        2058 :   f1 = ZX_graeffe(f); /* has at most square factors */
    1201        2058 :   if (ZX_equal(f1, f)) return myconcat(G,f); /* f = product of Phi_n, n odd */
    1202             : 
    1203        1183 :   fs2 = ZX_gcd_all(f1, ZX_deriv(f1), &f2); /* fs2 squarefree */
    1204        1183 :   if (degpol(fs2))
    1205             :   { /* fs contains all Phi_n | f, 4 | n; and only those */
    1206             :     /* In that case, Graeffe(Phi_n) = Phi_{n/2}^2, and Phi_n = Phi_{n/2}(x^2) */
    1207        1029 :     GEN fs = RgX_inflate(fs2, 2);
    1208        1029 :     (void)ZX_gcd_all(f, fs, &f); /* remove those Phi_n | f, 4 | n */
    1209        1029 :     Gs = BD(fs2);
    1210        1029 :     if (Gs)
    1211             :     {
    1212             :       long i;
    1213        1029 :       for (i = lg(Gs)-1; i; i--) gel(Gs,i) = RgX_inflate(gel(Gs,i), 2);
    1214             :       /* prod Gs[i] is the product of all Phi_n | f, 4 | n */
    1215        1029 :       G = myconcat(G, Gs);
    1216             :     }
    1217             :     /* f2 = f1 / fs2 */
    1218        1029 :     f1 = RgX_div(f2, fs2); /* f1 / fs2^2 */
    1219             :   }
    1220        1183 :   fp = ZX_gcd(f, f1); /* contains all Phi_n | f, n > 1 odd; and only those */
    1221        1183 :   if (degpol(fp))
    1222             :   {
    1223         196 :     Gp = BD_odd(fp);
    1224             :     /* Gp is the product of all Phi_n | f, n odd */
    1225         196 :     if (Gp) G = myconcat(G, Gp);
    1226         196 :     f = RgX_div(f, fp);
    1227             :   }
    1228        1183 :   if (degpol(f))
    1229             :   { /* contains all Phi_n originally dividing f, n = 2 mod 4, n > 2;
    1230             :      * and only those
    1231             :      * In that case, Graeffe(Phi_n) = Phi_{n/2}, and Phi_n = Phi_{n/2}(-x) */
    1232         210 :     Gi = BD_odd(ZX_unscale(f, gen_m1));
    1233         210 :     if (Gi)
    1234             :     { /* N.B. Phi_2 does not divide f */
    1235         210 :       Gi = ZX_unscale(Gi, gen_m1);
    1236             :       /* Gi is the product of all Phi_n | f, n = 2 mod 4 */
    1237         210 :       G = myconcat(G, Gi);
    1238             :     }
    1239             :   }
    1240        1183 :   return G;
    1241             : }
    1242             : 
    1243             : /* Let f be a non-zero QX, return the (squarefree) product of cyclotomic
    1244             :  * divisors of f */
    1245             : GEN
    1246         315 : polcyclofactors(GEN f)
    1247             : {
    1248         315 :   pari_sp av = avma;
    1249         315 :   if (typ(f) != t_POL || !signe(f)) pari_err_TYPE("polcyclofactors",f);
    1250         315 :   (void)RgX_valrem(f, &f);
    1251         315 :   f = Q_primpart(f);
    1252         315 :   RgX_check_ZX(f,"polcyclofactors");
    1253         315 :   if (degpol(f))
    1254             :   {
    1255         315 :     f = BD(ZX_radical(f));
    1256         315 :     if (f) return gerepilecopy(av, f);
    1257             :   }
    1258           0 :   avma = av; return cgetg(1,t_VEC);
    1259             : }
    1260             : 
    1261             : /* return t*x mod T(x), T a monic ZX. Assume deg(t) < deg(T) */
    1262             : static GEN
    1263       46018 : ZXQ_mul_by_X(GEN t, GEN T)
    1264             : {
    1265             :   GEN lt;
    1266       46018 :   t = RgX_shift_shallow(t, 1);
    1267       46018 :   if (degpol(t) < degpol(T)) return t;
    1268        3822 :   lt = leading_coeff(t);
    1269        3822 :   if (is_pm1(lt)) return signe(lt) > 0 ? ZX_sub(t, T): ZX_add(t, T);
    1270         217 :   return ZX_sub(t, ZX_Z_mul(T, leading_coeff(t)));
    1271             : }
    1272             : /* f a product of Phi_n, all n odd; deg f > 1. Is it irreducible ? */
    1273             : static long
    1274         840 : BD_odd_iscyclo(GEN f)
    1275             : {
    1276             :   pari_sp av;
    1277             :   long d, e, n, bound;
    1278             :   GEN t;
    1279         840 :   f = ZX_deflate_max(f, &e);
    1280         840 :   av = avma;
    1281             :   /* The original f is cyclotomic (= Phi_{ne}) iff the present one is Phi_n,
    1282             :    * where all prime dividing e also divide n. If current f is Phi_n,
    1283             :    * then n is odd and squarefree */
    1284         840 :   d = degpol(f); /* = phi(n) */
    1285             :   /* Let e > 0, g multiplicative such that
    1286             :        g(p) = p / (p-1)^(1+e) < 1 iff p < (p-1)^(1+e)
    1287             :      For all squarefree odd n, we have g(n) < C, hence n < C phi(n)^(1+e), where
    1288             :        C = \prod_{p odd | p > (p-1)^(1+e)} g(p)
    1289             :      For e = 1/10,   we obtain p = 3, 5 and C < 1.523
    1290             :      For e = 1/100,  we obtain p = 3, 5, ..., 29 and C < 2.573
    1291             :      In fact, for n <= 10^7 odd & squarefree, we have n < 2.92 * phi(n)
    1292             :      By the above, n<10^7 covers all d <= (10^7/2.573)^(1/(1+1/100)) < 3344391.
    1293             :   */
    1294         840 :   if (d <= 3344391)
    1295         840 :     bound = (long)(2.92 * d);
    1296             :   else
    1297           0 :     bound = (long)(2.573 * pow(d,1.01));
    1298             :   /* IF f = Phi_n, n squarefree odd, then n <= bound */
    1299         840 :   t = pol_xn(d-1, varn(f));
    1300       46053 :   for (n = d; n <= bound; n++)
    1301             :   {
    1302       46018 :     t = ZXQ_mul_by_X(t, f);
    1303             :     /* t = (X mod f(X))^d */
    1304       46018 :     if (degpol(t) == 0) break;
    1305       45213 :     if (gc_needed(av,1))
    1306             :     {
    1307         454 :       if(DEBUGMEM>1) pari_warn(warnmem,"BD_odd_iscyclo");
    1308         454 :       t = gerepilecopy(av, t);
    1309             :     }
    1310             :   }
    1311         840 :   if (n > bound || eulerphiu(n) != (ulong)d) return 0;
    1312             : 
    1313         777 :   if (e > 1) return (u_ppo(e, n) == 1)? e * n : 0;
    1314         651 :   return n;
    1315             : }
    1316             : 
    1317             : /* Checks if f, monic squarefree ZX with |constant coeff| = 1, is a cyclotomic
    1318             :  * polynomial. Returns n if f = Phi_n, and 0 otherwise */
    1319             : static long
    1320        2387 : BD_iscyclo(GEN f)
    1321             : {
    1322        2387 :   pari_sp av = avma;
    1323             :   GEN f2, fn, f1;
    1324             : 
    1325        2387 :   if (degpol(f) == 1) return isint1(gel(f,2))? 2: 1;
    1326        2254 :   f1 = ZX_graeffe(f);
    1327             :   /* f = product of Phi_n, n odd */
    1328        2254 :   if (ZX_equal(f, f1)) { avma = av; return BD_odd_iscyclo(f); }
    1329             : 
    1330        1792 :   fn = ZX_unscale(f, gen_m1); /* f(-x) */
    1331             :   /* f = product of Phi_n, n = 2 mod 4 */
    1332        1792 :   if (ZX_equal(f1, fn)) return 2*BD_odd_iscyclo(fn);
    1333             : 
    1334        1414 :   if (issquareall(f1, &f2))
    1335             :   {
    1336         595 :     GEN lt = leading_coeff(f2);
    1337             :     long c;
    1338         595 :     if (signe(lt) < 0) f2 = ZX_neg(f2);
    1339         595 :     c = BD_iscyclo(f2);
    1340         595 :     return odd(c)? 0: 2*c;
    1341             :   }
    1342         819 :   avma = av; return 0;
    1343             : }
    1344             : long
    1345        3682 : poliscyclo(GEN f)
    1346             : {
    1347             :   long d;
    1348        3682 :   if (typ(f) != t_POL) pari_err_TYPE("poliscyclo", f);
    1349        3675 :   d = degpol(f);
    1350        3675 :   if (d <= 0 || !RgX_is_ZX(f)) return 0;
    1351        3668 :   if (!equali1(gel(f,d+2)) || !is_pm1(gel(f,2))) return 0;
    1352        1827 :   if (d == 1) return signe(gel(f,2)) > 0? 2: 1;
    1353        1792 :   return ZX_is_squarefree(f)? BD_iscyclo(f): 0;
    1354             : }
    1355             : 
    1356             : long
    1357        1029 : poliscycloprod(GEN f)
    1358             : {
    1359        1029 :   pari_sp av = avma;
    1360        1029 :   long i, d = degpol(f);
    1361        1029 :   if (typ(f) != t_POL) pari_err_TYPE("poliscycloprod",f);
    1362        1029 :   if (!RgX_is_ZX(f)) return 0;
    1363        1029 :   if (!equali1(leading_coeff(f)) || !is_pm1(constant_coeff(f))) return 0;
    1364        1029 :   if (d < 2) return (d == 1);
    1365        1022 :   if ( degpol(ZX_gcd_all(f, ZX_deriv(f), &f)) )
    1366             :   {
    1367          14 :     d = degpol(f);
    1368          14 :     if (d == 1) return 1;
    1369             :   }
    1370        1015 :   f = BD(f); if (!f) return 0;
    1371        1015 :   for (i = lg(f)-1; i; i--) d -= degpol(gel(f,i));
    1372        1015 :   avma = av; return d == 0;
    1373             : }

Generated by: LCOV version 1.11