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.8.0 lcov report (development 19357-d770f77) Lines: 730 771 94.7 %
Date: 2016-08-27 06:11:27 Functions: 41 43 95.3 %
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     6264031 : 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     6264031 :   dy=degpol(y);
      26     6264031 :   dx=degpol(x);
      27     6264031 :   dz=dx-dy; if (dz<0) return NULL;
      28     6263842 :   z=cgetg(dz+3,t_POL); z[1] = x[1];
      29     6263842 :   x += 2; y += 2; z += 2;
      30     6263842 :   y_lead = gel(y,dy);
      31     6263842 :   if (equali1(y_lead)) y_lead = NULL;
      32             : 
      33     6263842 :   p1 = gel(x,dx);
      34     6263842 :   if (y_lead) {
      35             :     GEN r;
      36        3341 :     p1 = dvmdii(p1,y_lead, &r);
      37        3341 :     if (r != gen_0) return NULL;
      38             :   }
      39     6260501 :   else p1 = icopy(p1);
      40     6263842 :   gel(z,dz) = p1;
      41     6962240 :   for (i=dx-1; i>=dy; i--)
      42             :   {
      43      698692 :     av = avma; p1 = gel(x,i);
      44     2371777 :     for (j=i-dy+1; j<=i && j<=dz; j++)
      45     1673085 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
      46      698692 :     if (y_lead) {
      47             :       GEN r;
      48       13860 :       p1 = dvmdii(p1,y_lead, &r);
      49       13860 :       if (r != gen_0) return NULL;
      50             :     }
      51      698643 :     if (B && abscmpii(p1, B) > 0) return NULL;
      52      698398 :     p1 = gerepileuptoint(av, p1);
      53      698398 :     gel(z,i-dy) = p1;
      54             :   }
      55     6263548 :   av = avma;
      56    15730881 :   for (; i >= 0; i--)
      57             :   {
      58     9505161 :     p1 = gel(x,i);
      59             :     /* we always enter this loop at least once */
      60    19991619 :     for (j=0; j<=i && j<=dz; j++)
      61    10486458 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
      62     9505161 :     if (signe(p1)) return NULL;
      63     9467333 :     avma = av;
      64             :   }
      65     6225720 :   return z - 2;
      66             : }
      67             : static GEN
      68     6261497 : 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        5943 : Mignotte_bound(GEN S)
     107             : {
     108        5943 :   long i, d = degpol(S);
     109        5943 :   GEN C, N2, t, binlS, lS = leading_coeff(S), bin = vecbinome(d-1);
     110             : 
     111        5943 :   N2 = sqrtr(RgX_fpnorml2(S,DEFAULTPREC));
     112        5943 :   binlS = is_pm1(lS)? bin: ZC_Z_mul(bin, lS);
     113             : 
     114             :   /* i = 0 */
     115        5943 :   C = gel(binlS,1);
     116             :   /* i = d */
     117        5943 :   t = N2; if (gcmp(C, t) < 0) C = t;
     118       84042 :   for (i = 1; i < d; i++)
     119             :   {
     120       78099 :     t = addri(mulir(gel(bin,i), N2), gel(binlS,i+1));
     121       78099 :     if (mpcmp(C, t) < 0) C = t;
     122             :   }
     123        5943 :   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        5943 : Beauzamy_bound(GEN S)
     129             : {
     130        5943 :   const long prec = DEFAULTPREC;
     131        5943 :   long i, d = degpol(S);
     132             :   GEN bin, lS, s, C;
     133        5943 :   bin = vecbinome(d);
     134             : 
     135        5943 :   s = real_0(prec);
     136       95928 :   for (i=0; i<=d; i++)
     137             :   {
     138       89985 :     GEN c = gel(S,i+2);
     139       89985 :     if (gequal0(c)) continue;
     140             :     /* s += P_i^2 / binomial(d,i) */
     141       74144 :     s = addrr(s, divri(itor(sqri(c), prec), gel(bin,i+1)));
     142             :   }
     143             :   /* s = [S]_2^2 */
     144        5943 :   C = powruhalf(stor(3,prec), 3 + 2*d); /* 3^{3/2 + d} */
     145        5943 :   C = divrr(mulrr(C, s), mulur(4*d, mppi(prec)));
     146        5943 :   lS = absi(leading_coeff(S));
     147        5943 :   return mulir(lS, sqrtr(C));
     148             : }
     149             : 
     150             : static GEN
     151        5943 : factor_bound(GEN S)
     152             : {
     153        5943 :   pari_sp av = avma;
     154        5943 :   GEN a = Mignotte_bound(S);
     155        5943 :   GEN b = Beauzamy_bound(S);
     156        5943 :   if (DEBUGLEVEL>2)
     157             :   {
     158           0 :     err_printf("Mignotte bound: %Ps\n",a);
     159           0 :     err_printf("Beauzamy bound: %Ps\n",b);
     160             :   }
     161        5943 :   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        4767 : cmbf(GEN pol, GEN famod, GEN bound, GEN p, long a, long b,
     173             :      long klim, long *pmaxK, int *done)
     174             : {
     175        4767 :   long K = 1, cnt = 1, i,j,k, curdeg, lfamod = lg(famod)-1;
     176             :   ulong spa_b, spa_bs2, Sbound;
     177        4767 :   GEN lc, lcpol, pa = powiu(p,a), pas2 = shifti(pa,-1);
     178        4767 :   GEN trace1   = cgetg(lfamod+1, t_VECSMALL);
     179        4767 :   GEN trace2   = cgetg(lfamod+1, t_VECSMALL);
     180        4767 :   GEN ind      = cgetg(lfamod+1, t_VECSMALL);
     181        4767 :   GEN deg      = cgetg(lfamod+1, t_VECSMALL);
     182        4767 :   GEN degsofar = cgetg(lfamod+1, t_VECSMALL);
     183        4767 :   GEN listmod  = cgetg(lfamod+1, t_VEC);
     184        4767 :   GEN fa       = cgetg(lfamod+1, t_VEC);
     185             : 
     186        4767 :   *pmaxK = cmbf_maxK(lfamod);
     187        4767 :   lc = absi(leading_coeff(pol));
     188        4767 :   if (is_pm1(lc)) lc = NULL;
     189        4767 :   lcpol = lc? ZX_Z_mul(pol, lc): pol;
     190             : 
     191             :   {
     192        4767 :     GEN pa_b,pa_bs2,pb, lc2 = lc? sqri(lc): NULL;
     193             : 
     194        4767 :     pa_b = powiu(p, a-b); /* < 2^31 */
     195        4767 :     pa_bs2 = shifti(pa_b,-1);
     196        4767 :     pb= powiu(p, b);
     197       19152 :     for (i=1; i <= lfamod; i++)
     198             :     {
     199       14385 :       GEN T1,T2, P = gel(famod,i);
     200       14385 :       long d = degpol(P);
     201             : 
     202       14385 :       deg[i] = d; P += 2;
     203       14385 :       T1 = gel(P,d-1);/* = - S_1 */
     204       14385 :       T2 = sqri(T1);
     205       14385 :       if (d > 1) T2 = subii(T2, shifti(gel(P,d-2),1));
     206       14385 :       T2 = modii(T2, pa); /* = S_2 Newton sum */
     207       14385 :       if (lc)
     208             :       {
     209         658 :         T1 = Fp_mul(lc, T1, pa);
     210         658 :         T2 = Fp_mul(lc2,T2, pa);
     211             :       }
     212       14385 :       uel(trace1,i) = itou(diviiround(T1, pb));
     213       14385 :       uel(trace2,i) = itou(diviiround(T2, pb));
     214             :     }
     215        4767 :     spa_b   = uel(pa_b,2); /* < 2^31 */
     216        4767 :     spa_bs2 = uel(pa_bs2,2); /* < 2^31 */
     217             :   }
     218        4767 :   degsofar[0] = 0; /* sentinel */
     219             : 
     220             :   /* ind runs through strictly increasing sequences of length K,
     221             :    * 1 <= ind[i] <= lfamod */
     222             : nextK:
     223        9583 :   if (K > *pmaxK || 2*K > lfamod) goto END;
     224        5803 :   if (DEBUGLEVEL > 3)
     225           0 :     err_printf("\n### K = %d, %Ps combinations\n", K,binomial(utoipos(lfamod), K));
     226        5803 :   setlg(ind, K+1); ind[1] = 1;
     227        5803 :   Sbound = (ulong) ((K+1)>>1);
     228        5803 :   i = 1; curdeg = deg[ind[1]];
     229             :   for(;;)
     230             :   { /* try all combinations of K factors */
     231      365106 :     for (j = i; j < K; j++)
     232             :     {
     233       44618 :       degsofar[j] = curdeg;
     234       44618 :       ind[j+1] = ind[j]+1; curdeg += deg[ind[j+1]];
     235             :     }
     236      320488 :     if (curdeg <= klim) /* trial divide */
     237             :     {
     238             :       GEN y, q, list;
     239             :       pari_sp av;
     240             :       ulong t;
     241             : 
     242             :       /* d - 1 test */
     243      932729 :       for (t=uel(trace1,ind[1]),i=2; i<=K; i++)
     244      612241 :         t = Fl_add(t, uel(trace1,ind[i]), spa_b);
     245      320488 :       if (t > spa_bs2) t = spa_b - t;
     246      320488 :       if (t > Sbound)
     247             :       {
     248      285257 :         if (DEBUGLEVEL>6) err_printf(".");
     249      285257 :         goto NEXT;
     250             :       }
     251             :       /* d - 2 test */
     252      112721 :       for (t=uel(trace2,ind[1]),i=2; i<=K; i++)
     253       77490 :         t = Fl_add(t, uel(trace2,ind[i]), spa_b);
     254       35231 :       if (t > spa_bs2) t = spa_b - t;
     255       35231 :       if (t > Sbound)
     256             :       {
     257       22092 :         if (DEBUGLEVEL>6) err_printf("|");
     258       22092 :         goto NEXT;
     259             :       }
     260             : 
     261       13139 :       av = avma;
     262             :       /* check trailing coeff */
     263       13139 :       y = lc;
     264       56350 :       for (i=1; i<=K; i++)
     265             :       {
     266       43211 :         GEN q = constant_coeff(gel(famod,ind[i]));
     267       43211 :         if (y) q = mulii(y, q);
     268       43211 :         y = centermodii(q, pa, pas2);
     269             :       }
     270       13139 :       if (!signe(y) || remii(constant_coeff(lcpol), y) != gen_0)
     271             :       {
     272       10745 :         if (DEBUGLEVEL>3) err_printf("T");
     273       10745 :         avma = av; goto NEXT;
     274             :       }
     275        2394 :       y = lc; /* full computation */
     276        5264 :       for (i=1; i<=K; i++)
     277             :       {
     278        2870 :         GEN q = gel(famod,ind[i]);
     279        2870 :         if (y) q = gmul(y, q);
     280        2870 :         y = centermod_i(q, pa, pas2);
     281             :       }
     282             : 
     283             :       /* y is the candidate factor */
     284        2394 :       if (! (q = ZX_divides_i(lcpol,y,bound)) )
     285             :       {
     286         238 :         if (DEBUGLEVEL>3) err_printf("*");
     287         238 :         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       10234 :       for (i=j=k=1; i <= lfamod; i++)
     300             :       { /* remove used factors */
     301        8078 :         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        1169 :       i = 1; curdeg = deg[ind[1]];
     314        1169 :       bound = factor_bound(pol);
     315        1169 :       if (lc) lc = absi(leading_coeff(pol));
     316        1169 :       lcpol = lc? ZX_Z_mul(pol, lc): pol;
     317        1169 :       if (DEBUGLEVEL>3)
     318           0 :         err_printf("\nfound factor %Ps\nremaining modular factor(s): %ld\n",
     319             :                    y, lfamod);
     320        1169 :       continue;
     321             :     }
     322             : 
     323             : NEXT:
     324      318332 :     for (i = K+1;;)
     325             :     {
     326      367472 :       if (--i == 0) { K++; goto nextK; }
     327      362656 :       if (++ind[i] <= lfamod - K + i)
     328             :       {
     329      313516 :         curdeg = degsofar[i-1] + deg[ind[i]];
     330      313516 :         if (curdeg <= klim) break;
     331             :       }
     332       49140 :     }
     333      314685 :   }
     334             : END:
     335        4767 :   *done = 1;
     336        4767 :   if (degpol(pol) > 0)
     337             :   { /* leftover factor */
     338        4767 :     if (signe(leading_coeff(pol)) < 0) pol = ZX_neg(pol);
     339        4767 :     if (lfamod >= 2*K) *done = 0;
     340             : 
     341        4767 :     setlg(famod, lfamod+1);
     342        4767 :     gel(listmod,cnt) = leafcopy(famod);
     343        4767 :     gel(fa,cnt++) = pol;
     344             :   }
     345        4767 :   if (DEBUGLEVEL>6) err_printf("\n");
     346        4767 :   setlg(listmod, cnt);
     347        4767 :   setlg(fa, cnt); return mkvec2(fa, listmod);
     348             : }
     349             : 
     350             : void
     351           0 : factor_quad(GEN x, GEN res, long *ptcnt)
     352             : {
     353           0 :   GEN a = gel(x,4), b = gel(x,3), c = gel(x,2), d, u, z1, z2, t;
     354           0 :   GEN D = subii(sqri(b), shifti(mulii(a,c), 2));
     355           0 :   long v, cnt = *ptcnt;
     356             : 
     357           0 :   if (!Z_issquareall(D, &d)) { gel(res,cnt++) = x; *ptcnt = cnt; return; }
     358             : 
     359           0 :   t = shifti(negi(addii(b, d)), -1);
     360           0 :   z1 = gdiv(t, a); u = denom(z1);
     361           0 :   z2 = gdiv(addii(t, d), a);
     362           0 :   v = varn(x);
     363           0 :   gel(res,cnt++) = gmul(u, gsub(pol_x(v), z1)); u = diviiexact(a, u);
     364           0 :   gel(res,cnt++) = gmul(u, gsub(pol_x(v), z2)); *ptcnt = cnt;
     365             : }
     366             : 
     367             : /* recombination of modular factors: van Hoeij's algorithm */
     368             : 
     369             : /* Q in Z[X], return Q(2^n) */
     370             : static GEN
     371       75810 : shifteval(GEN Q, long n)
     372             : {
     373       75810 :   long i, l = lg(Q);
     374             :   GEN s;
     375             : 
     376       75810 :   if (!signe(Q)) return gen_0;
     377       75810 :   s = gel(Q,l-1);
     378       75810 :   for (i = l-2; i > 1; i--) s = addii(gel(Q,i), shifti(s, n));
     379       75810 :   return s;
     380             : }
     381             : 
     382             : /* return integer y such that all |a| <= y if P(a) = 0 */
     383             : static GEN
     384       44114 : root_bound(GEN P0)
     385             : {
     386       44114 :   GEN Q = leafcopy(P0), lP = absi(leading_coeff(Q)), x,y,z;
     387       44114 :   long k, d = degpol(Q);
     388             : 
     389             :   /* P0 = lP x^d + Q, deg Q < d */
     390       44114 :   Q = normalizepol_lg(Q, d+2);
     391       44114 :   for (k=lg(Q)-1; k>1; k--) gel(Q,k) = absi(gel(Q,k));
     392       44114 :   k = (long)(fujiwara_bound(P0));
     393       75887 :   for (  ; k >= 0; k--)
     394             :   {
     395       75810 :     pari_sp av = avma;
     396             :     /* y = 2^k; Q(y) >= lP y^d ? */
     397       75810 :     if (cmpii(shifteval(Q,k), shifti(lP, d*k)) >= 0) break;
     398       31773 :     avma = av;
     399             :   }
     400       44114 :   if (k < 0) k = 0;
     401       44114 :   x = int2n(k);
     402       44114 :   y = int2n(k+1);
     403      292369 :   for(k=0; ; k++)
     404             :   {
     405      292369 :     z = shifti(addii(x,y), -1);
     406      292369 :     if (equalii(x,z) || k > 5) break;
     407      248255 :     if (cmpii(poleval(Q,z), mulii(lP, powiu(z, d))) < 0)
     408      132440 :       y = z;
     409             :     else
     410      115815 :       x = z;
     411      248255 :   }
     412       44114 :   return y;
     413             : }
     414             : 
     415             : GEN
     416         175 : special_pivot(GEN x)
     417             : {
     418         175 :   GEN t, perm, H = ZM_hnfperm(x,NULL,&perm);
     419         175 :   long i,j, l = lg(H), h = lgcols(H);
     420        1855 :   for (i=1; i<h; i++)
     421             :   {
     422        1778 :     int fl = 0;
     423       10955 :     for (j=1; j<l; j++)
     424             :     {
     425        9275 :       t = gcoeff(H,i,j);
     426        9275 :       if (signe(t))
     427             :       {
     428        1813 :         if (!is_pm1(t) || fl) return NULL;
     429        1715 :         fl = 1;
     430             :       }
     431             :     }
     432             :   }
     433          77 :   return rowpermute(H, perm_inv(perm));
     434             : }
     435             : 
     436             : GEN
     437         266 : chk_factors_get(GEN lt, GEN famod, GEN c, GEN T, GEN N)
     438             : {
     439         266 :   long i = 1, j, l = lg(famod);
     440         266 :   GEN V = cgetg(l, t_VEC);
     441        7476 :   for (j = 1; j < l; j++)
     442        7210 :     if (signe(gel(c,j))) gel(V,i++) = gel(famod,j);
     443         266 :   if (lt && i > 1) gel(V,1) = RgX_Rg_mul(gel(V,1), lt);
     444         266 :   setlg(V, i);
     445         266 :   return T? FpXQXV_prod(V, T, N): FpXV_prod(V,N);
     446             : }
     447             : 
     448             : static GEN
     449         119 : chk_factors(GEN P, GEN M_L, GEN bound, GEN famod, GEN pa)
     450             : {
     451             :   long i, r;
     452         119 :   GEN pol = P, list, piv, y, ltpol, lt, paov2;
     453             : 
     454         119 :   piv = special_pivot(M_L);
     455         119 :   if (!piv) return NULL;
     456          49 :   if (DEBUGLEVEL>7) err_printf("special_pivot output:\n%Ps\n",piv);
     457             : 
     458          49 :   r  = lg(piv)-1;
     459          49 :   list = cgetg(r+1, t_VEC);
     460          49 :   lt = absi(leading_coeff(pol));
     461          49 :   if (is_pm1(lt)) lt = NULL;
     462          49 :   ltpol = lt? ZX_Z_mul(pol, lt): pol;
     463          49 :   paov2 = shifti(pa,-1);
     464          49 :   for (i = 1;;)
     465             :   {
     466         140 :     if (DEBUGLEVEL) err_printf("LLL_cmbf: checking factor %ld\n",i);
     467         140 :     y = chk_factors_get(lt, famod, gel(piv,i), NULL, pa);
     468         140 :     y = FpX_center(y, pa, paov2);
     469         140 :     if (! (pol = ZX_divides_i(ltpol,y,bound)) ) return NULL;
     470         133 :     if (lt) y = Q_primpart(y);
     471         133 :     gel(list,i) = y;
     472         133 :     if (++i >= r) break;
     473             : 
     474          91 :     if (lt)
     475             :     {
     476          35 :       pol = ZX_Z_divexact(pol, leading_coeff(y));
     477          35 :       lt = absi(leading_coeff(pol));
     478          35 :       ltpol = ZX_Z_mul(pol, lt);
     479             :     }
     480             :     else
     481          56 :       ltpol = pol;
     482          91 :   }
     483          42 :   y = Q_primpart(pol);
     484          42 :   gel(list,i) = y; return list;
     485             : }
     486             : 
     487             : GEN
     488        1330 : LLL_check_progress(GEN Bnorm, long n0, GEN m, int final, long *ti_LLL)
     489             : {
     490             :   GEN norm, u;
     491             :   long i, R;
     492             :   pari_timer T;
     493             : 
     494        1330 :   if (DEBUGLEVEL>2) timer_start(&T);
     495        1330 :   u = ZM_lll_norms(m, final? 0.999: 0.75, LLL_INPLACE, &norm);
     496        1330 :   if (DEBUGLEVEL>2) *ti_LLL += timer_delay(&T);
     497        8897 :   for (R=lg(m)-1; R > 0; R--)
     498        8897 :     if (cmprr(gel(norm,R), Bnorm) < 0) break;
     499        1330 :   for (i=1; i<=R; i++) setlg(u[i], n0+1);
     500        1330 :   if (R <= 1)
     501             :   {
     502         119 :     if (!R) pari_err_BUG("LLL_cmbf [no factor]");
     503         119 :     return NULL; /* irreducible */
     504             :   }
     505        1211 :   setlg(u, R+1); return u;
     506             : }
     507             : 
     508             : static ulong
     509          14 : next2pow(ulong a)
     510             : {
     511          14 :   ulong b = 1;
     512          14 :   while (b < a) b <<= 1;
     513          14 :   return b;
     514             : }
     515             : 
     516             : /* Recombination phase of Berlekamp-Zassenhaus algorithm using a variant of
     517             :  * van Hoeij's knapsack
     518             :  *
     519             :  * P = squarefree in Z[X].
     520             :  * famod = array of (lifted) modular factors mod p^a
     521             :  * bound = Mignotte bound for the size of divisors of P (for the sup norm)
     522             :  * previously recombined all set of factors with less than rec elts */
     523             : static GEN
     524          91 : LLL_cmbf(GEN P, GEN famod, GEN p, GEN pa, GEN bound, long a, long rec)
     525             : {
     526          91 :   const long N0 = 1; /* # of traces added at each step */
     527          91 :   double BitPerFactor = 0.4; /* nb bits in p^(a-b) / modular factor */
     528          91 :   long i,j,tmax,n0,C, dP = degpol(P);
     529          91 :   double logp = log((double)itos(p)), LOGp2 = LOG2/logp;
     530          91 :   double b0 = log((double)dP*2) / logp, logBr;
     531             :   GEN lP, Br, Bnorm, Tra, T2, TT, CM_L, m, list, ZERO;
     532             :   pari_sp av, av2;
     533          91 :   long ti_LLL = 0, ti_CF  = 0;
     534             : 
     535          91 :   lP = absi(leading_coeff(P));
     536          91 :   if (is_pm1(lP)) lP = NULL;
     537          91 :   Br = root_bound(P);
     538          91 :   if (lP) Br = mulii(lP, Br);
     539          91 :   logBr = gtodouble(glog(Br, DEFAULTPREC)) / logp;
     540             : 
     541          91 :   n0 = lg(famod) - 1;
     542          91 :   C = (long)ceil( sqrt(N0 * n0 / 4.) ); /* > 1 */
     543          91 :   Bnorm = dbltor(n0 * (C*C + N0*n0/4.) * 1.00001);
     544          91 :   ZERO = zeromat(n0, N0);
     545             : 
     546          91 :   av = avma;
     547          91 :   TT = cgetg(n0+1, t_VEC);
     548          91 :   Tra  = cgetg(n0+1, t_MAT);
     549        1792 :   for (i=1; i<=n0; i++)
     550             :   {
     551        1701 :     TT[i]  = 0;
     552        1701 :     gel(Tra,i) = cgetg(N0+1, t_COL);
     553             :   }
     554          91 :   CM_L = scalarmat_s(C, n0);
     555             :   /* tmax = current number of traces used (and computed so far) */
     556         441 :   for (tmax = 0;; tmax += N0)
     557             :   {
     558         441 :     long b, bmin, bgood, delta, tnew = tmax + N0, r = lg(CM_L)-1;
     559             :     GEN M_L, q, CM_Lp, oldCM_L;
     560         441 :     int first = 1;
     561             :     pari_timer ti2, TI;
     562             : 
     563         441 :     bmin = (long)ceil(b0 + tnew*logBr);
     564         441 :     if (DEBUGLEVEL>2)
     565           0 :       err_printf("\nLLL_cmbf: %ld potential factors (tmax = %ld, bmin = %ld)\n",
     566             :                  r, tmax, bmin);
     567             : 
     568             :     /* compute Newton sums (possibly relifting first) */
     569         441 :     if (a <= bmin)
     570             :     {
     571          14 :       a = (long)ceil(bmin + 3*N0*logBr) + 1; /* enough for 3 more rounds */
     572          14 :       a = (long)next2pow((ulong)a);
     573             : 
     574          14 :       pa = powiu(p,a);
     575          14 :       famod = ZpX_liftfact(P,famod,NULL,p,a,pa);
     576          14 :       for (i=1; i<=n0; i++) TT[i] = 0;
     577             :     }
     578        8365 :     for (i=1; i<=n0; i++)
     579             :     {
     580        7924 :       GEN p1 = gel(Tra,i);
     581        7924 :       GEN p2 = polsym_gen(gel(famod,i), gel(TT,i), tnew, NULL, pa);
     582        7924 :       gel(TT,i) = p2;
     583        7924 :       p2 += 1+tmax; /* ignore traces number 0...tmax */
     584        7924 :       for (j=1; j<=N0; j++) gel(p1,j) = gel(p2,j);
     585        7924 :       if (lP)
     586             :       { /* make Newton sums integral */
     587        1848 :         GEN lPpow = powiu(lP, tmax);
     588        3696 :         for (j=1; j<=N0; j++)
     589             :         {
     590        1848 :           lPpow = mulii(lPpow,lP);
     591        1848 :           gel(p1,j) = mulii(gel(p1,j), lPpow);
     592             :         }
     593             :       }
     594             :     }
     595             : 
     596             :     /* compute truncation parameter */
     597         441 :     if (DEBUGLEVEL>2) { timer_start(&ti2); timer_start(&TI); }
     598         441 :     oldCM_L = CM_L;
     599         441 :     av2 = avma;
     600         441 :     delta = b = 0; /* -Wall */
     601             : AGAIN:
     602         840 :     M_L = Q_div_to_int(CM_L, utoipos(C));
     603         840 :     T2 = centermod( ZM_mul(Tra, M_L), pa );
     604         840 :     if (first)
     605             :     { /* initialize lattice, using few p-adic digits for traces */
     606         441 :       double t = gexpo(T2) - maxdd(32.0, BitPerFactor*r);
     607         441 :       bgood = (long) (t * LOGp2);
     608         441 :       b = maxss(bmin, bgood);
     609         441 :       delta = a - b;
     610             :     }
     611             :     else
     612             :     { /* add more p-adic digits and continue reduction */
     613         399 :       long b0 = (long)(gexpo(T2) * LOGp2);
     614         399 :       if (b0 < b) b = b0;
     615         399 :       b = maxss(b-delta, bmin);
     616         399 :       if (b - delta/2 < bmin) b = bmin; /* near there. Go all the way */
     617             :     }
     618             : 
     619         840 :     q = powiu(p, b);
     620         840 :     m = vconcat( CM_L, gdivround(T2, q) );
     621         840 :     if (first)
     622             :     {
     623         441 :       GEN P1 = scalarmat(powiu(p, a-b), N0);
     624         441 :       first = 0;
     625         441 :       m = shallowconcat( m, vconcat(ZERO, P1) );
     626             :       /*     [ C M_L        0     ]
     627             :        * m = [                    ]   square matrix
     628             :        *     [  T2'  p^(a-b) I_N0 ]   T2' = Tra * M_L  truncated
     629             :        */
     630             :     }
     631             : 
     632         840 :     CM_L = LLL_check_progress(Bnorm, n0, m, b == bmin, /*dbg:*/ &ti_LLL);
     633         840 :     if (DEBUGLEVEL>2)
     634           0 :       err_printf("LLL_cmbf: (a,b) =%4ld,%4ld; r =%3ld -->%3ld, time = %ld\n",
     635           0 :                  a,b, lg(m)-1, CM_L? lg(CM_L)-1: 1, timer_delay(&TI));
     636         931 :     if (!CM_L) { list = mkvec(P); break; }
     637         791 :     if (b > bmin)
     638             :     {
     639         399 :       CM_L = gerepilecopy(av2, CM_L);
     640         399 :       goto AGAIN;
     641             :     }
     642         392 :     if (DEBUGLEVEL>2) timer_printf(&ti2, "for this block of traces");
     643             : 
     644         392 :     i = lg(CM_L) - 1;
     645         392 :     if (i == r && ZM_equal(CM_L, oldCM_L))
     646             :     {
     647          21 :       CM_L = oldCM_L;
     648          21 :       avma = av2; continue;
     649             :     }
     650             : 
     651         371 :     CM_Lp = FpM_image(CM_L, utoipos(27449)); /* inexpensive test */
     652         371 :     if (lg(CM_Lp) != lg(CM_L))
     653             :     {
     654           7 :       if (DEBUGLEVEL>2) err_printf("LLL_cmbf: rank decrease\n");
     655           7 :       CM_L = ZM_hnf(CM_L);
     656             :     }
     657             : 
     658         371 :     if (i <= r && i*rec < n0)
     659             :     {
     660             :       pari_timer ti;
     661         119 :       if (DEBUGLEVEL>2) timer_start(&ti);
     662         119 :       list = chk_factors(P, Q_div_to_int(CM_L,utoipos(C)), bound, famod, pa);
     663         119 :       if (DEBUGLEVEL>2) ti_CF += timer_delay(&ti);
     664         119 :       if (list) break;
     665          77 :       if (DEBUGLEVEL>2) err_printf("LLL_cmbf: chk_factors failed");
     666             :     }
     667         329 :     CM_L = gerepilecopy(av2, CM_L);
     668         329 :     if (gc_needed(av,1))
     669             :     {
     670           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"LLL_cmbf");
     671           0 :       gerepileall(av, 5, &CM_L, &TT, &Tra, &famod, &pa);
     672             :     }
     673         350 :   }
     674          91 :   if (DEBUGLEVEL>2)
     675           0 :     err_printf("* Time LLL: %ld\n* Time Check Factor: %ld\n",ti_LLL,ti_CF);
     676          91 :   return list;
     677             : }
     678             : 
     679             : /* Find a,b minimal such that A < q^a, B < q^b, 1 << q^(a-b) < 2^31 */
     680             : static int
     681        4767 : cmbf_precs(GEN q, GEN A, GEN B, long *pta, long *ptb, GEN *qa, GEN *qb)
     682             : {
     683        4767 :   long a,b,amin,d = (long)(31 * LOG2/gtodouble(glog(q,DEFAULTPREC)) - 1e-5);
     684        4767 :   int fl = 0;
     685             : 
     686        4767 :   b = logintall(B, q, qb) + 1;
     687        4767 :   *qb = mulii(*qb, q);
     688        4767 :   amin = b + d;
     689        4767 :   if (gcmp(powiu(q, amin), A) <= 0)
     690             :   {
     691        1330 :     a = logintall(A, q, qa) + 1;
     692        1330 :     *qa = mulii(*qa, q);
     693        1330 :     b = a - d; *qb = powiu(q, b);
     694             :   }
     695             :   else
     696             :   { /* not enough room */
     697        3437 :     a = amin;  *qa = powiu(q, a);
     698        3437 :     fl = 1;
     699             :   }
     700        4767 :   if (DEBUGLEVEL > 3) {
     701           0 :     err_printf("S_2   bound: %Ps^%ld\n", q,b);
     702           0 :     err_printf("coeff bound: %Ps^%ld\n", q,a);
     703             :   }
     704        4767 :   *pta = a;
     705        4767 :   *ptb = b; return fl;
     706             : }
     707             : 
     708             : /* use van Hoeij's knapsack algorithm */
     709             : static GEN
     710        4767 : combine_factors(GEN target, GEN famod, GEN p, long klim)
     711             : {
     712             :   GEN la, B, A, res, L, pa, pb, listmod;
     713        4767 :   long a,b, l, maxK, n = degpol(target);
     714             :   int done;
     715             :   pari_timer T;
     716             : 
     717        4767 :   A = factor_bound(target);
     718             : 
     719        4767 :   la = absi(leading_coeff(target));
     720        4767 :   B = mului(n, sqri(mulii(la, root_bound(target)))); /* = bound for S_2 */
     721             : 
     722        4767 :   (void)cmbf_precs(p, A, B, &a, &b, &pa, &pb);
     723             : 
     724        4767 :   if (DEBUGLEVEL>2) timer_start(&T);
     725        4767 :   famod = ZpX_liftfact(target,famod,NULL,p,a,pa);
     726        4767 :   if (DEBUGLEVEL>2) timer_printf(&T, "Hensel lift (mod %Ps^%ld)", p,a);
     727        4767 :   L = cmbf(target, famod, A, p, a, b, klim, &maxK, &done);
     728        4767 :   if (DEBUGLEVEL>2) timer_printf(&T, "Naive recombination");
     729             : 
     730        4767 :   res     = gel(L,1);
     731        4767 :   listmod = gel(L,2); l = lg(listmod)-1;
     732        4767 :   famod = gel(listmod,l);
     733        4767 :   if (maxK > 0 && lg(famod)-1 > 2*maxK)
     734             :   {
     735          91 :     if (l!=1) A = factor_bound(gel(res,l));
     736          91 :     if (DEBUGLEVEL > 4) err_printf("last factor still to be checked\n");
     737          91 :     L = LLL_cmbf(gel(res,l), famod, p, pa, A, a, maxK);
     738          91 :     if (DEBUGLEVEL>2) timer_printf(&T,"Knapsack");
     739             :     /* remove last elt, possibly unfactored. Add all new ones. */
     740          91 :     setlg(res, l); res = shallowconcat(res, L);
     741             :   }
     742        4767 :   return res;
     743             : }
     744             : 
     745             : /* Assume 'a' a squarefree ZX; return 0 if no root (fl=1) / irreducible (fl=0).
     746             :  * Otherwise return prime p such that a mod p has fewest roots / factors */
     747             : static ulong
     748      597128 : pick_prime(GEN a, long fl, pari_timer *T)
     749             : {
     750      597128 :   pari_sp av = avma, av1;
     751      597128 :   const long MAXNP = 7, da = degpol(a);
     752      597128 :   long nmax = da+1, np;
     753      597128 :   ulong chosenp = 0;
     754      597128 :   GEN lead = gel(a,da+2);
     755             :   forprime_t S;
     756      597128 :   if (equali1(lead)) lead = NULL;
     757      597128 :   u_forprime_init(&S, 2, ULONG_MAX);
     758      597128 :   av1 = avma;
     759     3410393 :   for (np = 0; np < MAXNP; avma = av1)
     760             :   {
     761     3366384 :     ulong p = u_forprime_next(&S);
     762             :     long nfacp;
     763             :     GEN z;
     764             : 
     765     3366384 :     if (!p) pari_err_OVERFLOW("DDF [out of small primes]");
     766     3366384 :     if (lead && !umodiu(lead,p)) continue;
     767     3364116 :     z = ZX_to_Flx(a, p);
     768     3364116 :     if (!Flx_is_squarefree(z, p)) continue;
     769             : 
     770     2118676 :     if (fl)
     771             :     {
     772     2070012 :       nfacp = Flx_nbroots(z, p);
     773     2070012 :       if (!nfacp) { chosenp = 0; break; } /* no root */
     774             :     }
     775             :     else
     776             :     {
     777       48664 :       nfacp = Flx_nbfact(z, p);
     778       48664 :       if (nfacp == 1) { chosenp = 0; break; } /* irreducible */
     779             :     }
     780     1565571 :     if (DEBUGLEVEL>4)
     781           0 :       err_printf("...tried prime %3lu (%-3ld %s). Time = %ld\n",
     782             :                   p, nfacp, fl? "roots": "factors", timer_delay(T));
     783     1565571 :     if (nfacp < nmax)
     784             :     {
     785      531335 :       nmax = nfacp; chosenp = p;
     786      531335 :       if (da > 100 && nmax < 5) break; /* large degree, few factors. Enough */
     787             :     }
     788     1565557 :     np++;
     789             :   }
     790      597128 :   avma = av; return chosenp;
     791             : }
     792             : 
     793             : /* Assume pol squarefree mod p; return vector of rational roots of a */
     794             : static GEN
     795      581973 : DDF_roots(GEN A)
     796             : {
     797             :   GEN p, lc, lcpol, z, pe, pes2, bound;
     798             :   long i, m, e, lz;
     799             :   ulong pp;
     800             :   pari_sp av;
     801             :   pari_timer T;
     802             : 
     803      581973 :   if (DEBUGLEVEL>2) timer_start(&T);
     804      581973 :   pp = pick_prime(A, 1, &T);
     805      581973 :   if (!pp) return cgetg(1,t_VEC); /* no root */
     806       39256 :   p = utoipos(pp);
     807       39256 :   lc = leading_coeff(A);
     808       39256 :   if (is_pm1(lc))
     809       38381 :   { lc = NULL; lcpol = A; }
     810             :   else
     811         875 :   { lc = absi_shallow(lc); lcpol = ZX_Z_mul(A, lc); }
     812       39256 :   bound = root_bound(A); if (lc) bound = mulii(lc, bound);
     813       39256 :   e = logintall(addiu(shifti(bound, 1), 1), p, &pe) + 1;
     814       39256 :   pe = mulii(pe, p);
     815       39256 :   pes2 = shifti(pe, -1);
     816       39256 :   if (DEBUGLEVEL>2) timer_printf(&T, "Root bound");
     817       39256 :   av = avma;
     818       39256 :   z = ZpX_roots(A, p, e); lz = lg(z);
     819       39256 :   z = deg1_from_roots(z, varn(A));
     820       39256 :   if (DEBUGLEVEL>2) timer_printf(&T, "Hensel lift (mod %lu^%ld)", pp,e);
     821       79345 :   for (m=1, i=1; i < lz; i++)
     822             :   {
     823       40089 :     GEN q, r, y = gel(z,i);
     824       40089 :     if (lc) y = ZX_Z_mul(y, lc);
     825       40089 :     y = centermod_i(y, pe, pes2);
     826       40089 :     if (! (q = ZX_divides(lcpol, y)) ) continue;
     827             : 
     828        2240 :     lcpol = q;
     829        2240 :     r = negi( constant_coeff(y) );
     830        2240 :     if (lc) {
     831        1246 :       r = gdiv(r,lc);
     832        1246 :       lcpol = Q_primpart(lcpol);
     833        1246 :       lc = absi_shallow( leading_coeff(lcpol) );
     834        1246 :       if (is_pm1(lc)) lc = NULL; else lcpol = ZX_Z_mul(lcpol, lc);
     835             :     }
     836        2240 :     gel(z,m++) = r;
     837        2240 :     if (gc_needed(av,2))
     838             :     {
     839           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"DDF_roots, m = %ld", m);
     840           0 :       gerepileall(av, lc? 3:2, &z, &lcpol, &lc);
     841             : 
     842             :     }
     843             :   }
     844       39256 :   if (DEBUGLEVEL>2) timer_printf(&T, "Recombination");
     845       39256 :   z[0] = evaltyp(t_VEC) | evallg(m); return z;
     846             : }
     847             : 
     848             : /* Assume a squarefree ZX, deg(a) > 0, return rational factors.
     849             :  * In fact, a(0) != 0 but we don't use this */
     850             : static GEN
     851       15155 : DDF(GEN a)
     852             : {
     853             :   GEN ap, prime, famod, z;
     854       15155 :   long ti = 0;
     855       15155 :   ulong p = 0;
     856       15155 :   pari_sp av = avma;
     857             :   pari_timer T, T2;
     858             : 
     859       15155 :   if (DEBUGLEVEL>2) { timer_start(&T); timer_start(&T2); }
     860       15155 :   p = pick_prime(a, 0, &T2);
     861       15155 :   if (!p) return mkvec(a);
     862        4767 :   prime = utoipos(p);
     863        4767 :   ap = Flx_normalize(ZX_to_Flx(a, p), p);
     864        4767 :   famod = gel(Flx_factor(ap, p), 1);
     865        4767 :   if (DEBUGLEVEL>2)
     866             :   {
     867           0 :     if (DEBUGLEVEL>4) timer_printf(&T2, "splitting mod p = %lu", p);
     868           0 :     ti = timer_delay(&T);
     869           0 :     err_printf("Time setup: %ld\n", ti);
     870             :   }
     871        4767 :   z = combine_factors(a, FlxV_to_ZXV(famod), prime, degpol(a)-1);
     872        4767 :   if (DEBUGLEVEL>2)
     873           0 :     err_printf("Total Time: %ld\n===========\n", ti + timer_delay(&T));
     874        4767 :   return gerepilecopy(av, z);
     875             : }
     876             : 
     877             : /* Distinct Degree Factorization (deflating first)
     878             :  * Assume x squarefree, degree(x) > 0, x(0) != 0 */
     879             : GEN
     880       10745 : ZX_DDF(GEN x)
     881             : {
     882             :   GEN L;
     883             :   long m;
     884       10745 :   x = ZX_deflate_max(x, &m);
     885       10745 :   L = DDF(x);
     886       10745 :   if (m > 1)
     887             :   {
     888        3591 :     GEN e, v, fa = factoru(m);
     889             :     long i,j,k, l;
     890             : 
     891        3591 :     e = gel(fa,2); k = 0;
     892        3591 :     fa= gel(fa,1); l = lg(fa);
     893        3591 :     for (i=1; i<l; i++) k += e[i];
     894        3591 :     v = cgetg(k+1, t_VECSMALL); k = 1;
     895        7315 :     for (i=1; i<l; i++)
     896        3724 :       for (j=1; j<=e[i]; j++) v[k++] = fa[i];
     897        7924 :     for (k--; k; k--)
     898             :     {
     899        4333 :       GEN L2 = cgetg(1,t_VEC);
     900        8743 :       for (i=1; i < lg(L); i++)
     901        4410 :               L2 = shallowconcat(L2, DDF(RgX_inflate(gel(L,i), v[k])));
     902        4333 :       L = L2;
     903             :     }
     904             :   }
     905       10745 :   return L;
     906             : }
     907             : 
     908             : /* SquareFree Factorization. f = prod P^e, all e distinct, in Z[X] (char 0
     909             :  * would be enough, if ZX_gcd --> ggcd). Return (P), set *ex = (e) */
     910             : GEN
     911        7500 : ZX_squff(GEN f, GEN *ex)
     912             : {
     913             :   GEN T, V, P, e;
     914             :   long i, k, n, val;
     915             : 
     916        7500 :   if (signe(leading_coeff(f)) < 0) f = gneg_i(f);
     917        7500 :   val = ZX_valrem(f, &f);
     918        7500 :   n = 1 + degpol(f); if (val) n++;
     919        7500 :   e = cgetg(n,t_VECSMALL);
     920        7500 :   P = cgetg(n,t_COL);
     921             : 
     922        7500 :   T = ZX_gcd_all(f, ZX_deriv(f), &V);
     923        7843 :   for (k=i=1;; k++)
     924             :   {
     925        7843 :     pari_sp av = avma;
     926        7843 :     GEN W = ZX_gcd_all(T,V, &T);
     927        7843 :     long dW = degpol(W);
     928             :     /* W = prod P^e, e > k; V = prod P^e, e >= k */
     929        7843 :     if (dW == degpol(V)) /* V | T */
     930             :     {
     931             :       GEN U;
     932        1225 :       if (!dW) { avma = av; break; }
     933         217 :       while ( (U = ZX_divides(T, V)) ) { k++; T = U; }
     934         217 :       T = gerepileupto(av, T);
     935             :     }
     936             :     else
     937             :     {
     938        6618 :       gel(P,i) = Q_primpart(RgX_div(V,W));
     939        6618 :       e[i] = k; i++;
     940        6618 :       if (!dW) break;
     941         126 :       V = W;
     942             :     }
     943         343 :   }
     944        7500 :   if (val) { gel(P,i) = pol_x(varn(f)); e[i] = val; i++;}
     945        7500 :   setlg(P,i);
     946        7500 :   setlg(e,i); *ex = e; return P;
     947             : }
     948             : 
     949             : static GEN
     950        2758 : fact_from_DDF(GEN fa, GEN e, long n)
     951             : {
     952        2758 :   GEN v,w, y = cgetg(3, t_MAT);
     953        2758 :   long i,j,k, l = lg(fa);
     954             : 
     955        2758 :   v = cgetg(n+1,t_COL); gel(y,1) = v;
     956        2758 :   w = cgetg(n+1,t_COL); gel(y,2) = w;
     957        5782 :   for (k=i=1; i<l; i++)
     958             :   {
     959        3024 :     GEN L = gel(fa,i), ex = utoipos(e[i]);
     960        3024 :     long J = lg(L);
     961        7924 :     for (j=1; j<J; j++,k++)
     962             :     {
     963        4900 :       gel(v,k) = gcopy(gel(L,j));
     964        4900 :       gel(w,k) = ex;
     965             :     }
     966             :   }
     967        2758 :   return y;
     968             : }
     969             : 
     970             : /* Factor x in Z[t] */
     971             : static GEN
     972        2758 : ZX_factor_i(GEN x)
     973             : {
     974             :   GEN fa,ex,y;
     975             :   long n,i,l;
     976             : 
     977        2758 :   if (!signe(x)) return prime_fact(x);
     978        2758 :   fa = ZX_squff(x, &ex);
     979        2758 :   l = lg(fa); n = 0;
     980        5782 :   for (i=1; i<l; i++)
     981             :   {
     982        3024 :     gel(fa,i) = ZX_DDF(gel(fa,i));
     983        3024 :     n += lg(gel(fa,i))-1;
     984             :   }
     985        2758 :   y = fact_from_DDF(fa,ex,n);
     986        2758 :   return sort_factor_pol(y, cmpii);
     987             : }
     988             : GEN
     989        2443 : ZX_factor(GEN x)
     990             : {
     991        2443 :   pari_sp av = avma;
     992        2443 :   return gerepileupto(av, ZX_factor_i(x));
     993             : }
     994             : GEN
     995         315 : QX_factor(GEN x)
     996             : {
     997         315 :   pari_sp av = avma;
     998         315 :   return gerepileupto(av, ZX_factor_i(Q_primpart(x)));
     999             : }
    1000             : 
    1001             : long
    1002        8722 : ZX_is_irred(GEN x)
    1003             : {
    1004        8722 :   pari_sp av = avma;
    1005        8722 :   long l = lg(x);
    1006             :   GEN y;
    1007        8722 :   if (l <= 3) return 0; /* degree < 1 */
    1008        8722 :   if (l == 4) return 1; /* degree 1 */
    1009        7462 :   if (ZX_val(x)) return 0;
    1010        7259 :   if (!ZX_is_squarefree(x)) return 0;
    1011        7210 :   y = ZX_DDF(x); avma = av;
    1012        7210 :   return (lg(y) == 2);
    1013             : }
    1014             : 
    1015             : GEN
    1016      581973 : nfrootsQ(GEN x)
    1017             : {
    1018      581973 :   pari_sp av = avma;
    1019             :   GEN z;
    1020             :   long val;
    1021             : 
    1022      581973 :   if (typ(x)!=t_POL) pari_err_TYPE("nfrootsQ",x);
    1023      581973 :   if (!signe(x)) pari_err_ROOTS0("nfrootsQ");
    1024      581973 :   x = Q_primpart(x);
    1025      581973 :   RgX_check_ZX(x,"nfrootsQ");
    1026      581973 :   val = ZX_valrem(x, &x);
    1027      581973 :   (void)ZX_gcd_all(x, ZX_deriv(x), &x);
    1028      581973 :   z = DDF_roots(x);
    1029      581973 :   if (val) z = shallowconcat(z, gen_0);
    1030      581973 :   return gerepileupto(av, sort(z));
    1031             : }
    1032             : 
    1033             : /************************************************************************
    1034             :  *                   GCD OVER Z[X] / Q[X]                               *
    1035             :  ************************************************************************/
    1036             : int
    1037       16380 : ZX_is_squarefree(GEN x)
    1038             : {
    1039       16380 :   pari_sp av = avma;
    1040             :   GEN d;
    1041             :   long m;
    1042             :   int r;
    1043       16380 :   if (lg(x) == 2) return 0;
    1044       16380 :   m = ZX_deflate_order(x);
    1045       16380 :   if (m > 1)
    1046             :   {
    1047        4781 :     if (!signe(gel(x,2))) return 0;
    1048        4718 :     x = RgX_deflate(x, m);
    1049             :   }
    1050       16317 :   d = ZX_gcd(x,ZX_deriv(x));
    1051       16317 :   r = (lg(d) == 3); avma = av; return r;
    1052             : }
    1053             : 
    1054             : #if 0
    1055             : /* ceil( || p ||_oo / lc(p) ) */
    1056             : static GEN
    1057             : maxnorm(GEN p)
    1058             : {
    1059             :   long i, n = degpol(p), av = avma;
    1060             :   GEN x, m = gen_0;
    1061             : 
    1062             :   p += 2;
    1063             :   for (i=0; i<n; i++)
    1064             :   {
    1065             :     x = gel(p,i);
    1066             :     if (abscmpii(x,m) > 0) m = x;
    1067             :   }
    1068             :   m = divii(m, gel(p,n));
    1069             :   return gerepileuptoint(av, addis(absi(m),1));
    1070             : }
    1071             : #endif
    1072             : 
    1073             : /* A, B in Z[X] */
    1074             : GEN
    1075     4096908 : ZX_gcd_all(GEN A, GEN B, GEN *Anew)
    1076             : {
    1077             :   GEN R, a, b, q, H, Hp, g, Ag, Bg;
    1078     4096908 :   long m, n, valX, valA, vA = varn(A);
    1079             :   ulong p;
    1080             :   pari_sp ltop, av;
    1081             :   forprime_t S;
    1082             : 
    1083     4096908 :   if (!signe(A)) { if (Anew) *Anew = pol_0(vA); return ZX_copy(B); }
    1084     4096908 :   if (!signe(B)) { if (Anew) *Anew = pol_1(vA); return ZX_copy(A); }
    1085     4095816 :   valA = ZX_valrem(A, &A);
    1086     4095816 :   valX = minss(valA, ZX_valrem(B, &B));
    1087     4095816 :   ltop = avma;
    1088             : 
    1089     4095816 :   n = 1 + minss(degpol(A), degpol(B)); /* > degree(gcd) */
    1090     4095816 :   g = gcdii(leading_coeff(A), leading_coeff(B)); /* multiple of lead(gcd) */
    1091     4095816 :   if (is_pm1(g)) {
    1092     4089780 :     g = NULL;
    1093     4089780 :     Ag = A;
    1094     4089780 :     Bg = B;
    1095             :   } else {
    1096        6036 :     Ag = ZX_Z_mul(A,g);
    1097        6036 :     Bg = ZX_Z_mul(B,g);
    1098             :   }
    1099     4095816 :   init_modular_big(&S);
    1100     4095816 :   av = avma;
    1101     4095816 :   R = NULL;/*-Wall*/
    1102     4095816 :   H = NULL;
    1103    11302547 :   while ((p = u_forprime_next(&S)))
    1104             :   {
    1105     7206731 :     if (g && !umodiu(g,p)) continue;
    1106     7206731 :     a = ZX_to_Flx(A, p);
    1107     7206731 :     b = ZX_to_Flx(B, p); Hp = Flx_gcd(a,b, p);
    1108     7206731 :     m = degpol(Hp);
    1109     7206731 :     if (m == 0) { /* coprime. DONE */
    1110      985259 :       avma = ltop;
    1111      985259 :       if (Anew) {
    1112      602994 :         if (valA != valX) A = RgX_shift(A, valA - valX);
    1113      602994 :         *Anew = A;
    1114             :       }
    1115      985259 :       return monomial(gen_1, valX, vA);
    1116             :     }
    1117     6221472 :     if (m > n) continue; /* p | Res(A/G, B/G). Discard */
    1118             : 
    1119     6221472 :     if (!g) /* make sure lead(H) = g mod p */
    1120     6219464 :       Hp = Flx_normalize(Hp, p);
    1121             :     else
    1122             :     {
    1123        2008 :       ulong t = Fl_mul(umodiu(g, p), Fl_inv(Hp[m+2],p), p);
    1124        2008 :       Hp = Flx_Fl_mul(Hp, t, p);
    1125             :     }
    1126     6221472 :     if (m < n)
    1127             :     { /* First time or degree drop [all previous p were as above; restart]. */
    1128     3110557 :       H = ZX_init_CRT(Hp,p,vA);
    1129     3110557 :       q = utoipos(p); n = m; continue;
    1130             :     }
    1131     3110915 :     if (DEBUGLEVEL>5) err_printf("gcd mod %lu (bound 2^%ld)\n", p,expi(q));
    1132     3110915 :     if (gc_needed(av,1))
    1133             :     {
    1134           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"QX_gcd");
    1135           0 :       gerepileall(av, 3, &H, &q, &Hp);
    1136             :     }
    1137             : 
    1138     3110915 :     if (!ZX_incremental_CRT(&H, Hp, &q, p)) continue;
    1139             :     /* H stable: check divisibility */
    1140     3110557 :     if (!ZX_divides(Bg, H)) continue;
    1141     3110557 :     R = ZX_divides(Ag, H);
    1142     3110557 :     if (R) break;
    1143             :   }
    1144     3110557 :   if (!p) pari_err_OVERFLOW("ZX_gcd_all [ran out of primes]");
    1145     3110557 :   if (Anew) {
    1146       11306 :     A = R;
    1147       11306 :     if (valA != valX) A = RgX_shift(A, valA - valX);
    1148       11306 :     *Anew = A;
    1149             :   }
    1150     3110557 :   return valX ? RgX_shift(H, valX): H;
    1151             : }
    1152             : GEN
    1153     3481523 : ZX_gcd(GEN A, GEN B) { return ZX_gcd_all(A,B,NULL); }
    1154             : 
    1155             : static GEN
    1156     3453430 : _gcd(GEN a, GEN b)
    1157             : {
    1158     3453430 :   if (!a) a = gen_1;
    1159     3453430 :   if (!b) b = gen_1;
    1160     3453430 :   return Q_gcd(a,b);
    1161             : }
    1162             : /* A0 and B0 in Q[X] */
    1163             : GEN
    1164     3453430 : QX_gcd(GEN A0, GEN B0)
    1165             : {
    1166             :   GEN a, b, D;
    1167     3453430 :   pari_sp av = avma, av2;
    1168             : 
    1169     3453430 :   D = ZX_gcd(Q_primitive_part(A0, &a), Q_primitive_part(B0, &b));
    1170     3453430 :   av2 = avma; a = _gcd(a,b);
    1171     3453430 :   if (isint1(a)) avma = av2; else D = RgX_Rg_mul(D, a);
    1172     3453430 :   return gerepileupto(av, D);
    1173             : }
    1174             : 
    1175             : /*****************************************************************************
    1176             :  * Variants of the Bradford-Davenport algorithm: look for cyclotomic         *
    1177             :  * factors, and decide whether a ZX is cyclotomic or a product of cyclotomic *
    1178             :  *****************************************************************************/
    1179             : /* f of degree 1, return a cyclotomic factor (Phi_1 or Phi_2) or NULL */
    1180             : static GEN
    1181           0 : BD_deg1(GEN f)
    1182             : {
    1183           0 :   GEN a = gel(f,3), b = gel(f,2); /* f = ax + b */
    1184           0 :   if (!absequalii(a,b)) return NULL;
    1185           0 :   return polcyclo((signe(a) == signe(b))? 2: 1, varn(f));
    1186             : }
    1187             : 
    1188             : /* f a squarefree ZX; not divisible by any Phi_n, n even */
    1189             : static GEN
    1190         406 : BD_odd(GEN f)
    1191             : {
    1192         812 :   while(degpol(f) > 1)
    1193             :   {
    1194         406 :     GEN f1 = ZX_graeffe(f); /* contain all cyclotomic divisors of f */
    1195         406 :     if (ZX_equal(f1, f)) return f; /* product of cyclotomics */
    1196           0 :     f = ZX_gcd(f, f1);
    1197             :   }
    1198           0 :   if (degpol(f) == 1) return BD_deg1(f);
    1199           0 :   return NULL; /* no cyclotomic divisor */
    1200             : }
    1201             : 
    1202             : static GEN
    1203        2310 : myconcat(GEN v, GEN x)
    1204             : {
    1205        2310 :   if (typ(x) != t_VEC) x = mkvec(x);
    1206        2310 :   if (!v) return x;
    1207        1470 :   return shallowconcat(v, x);
    1208             : }
    1209             : 
    1210             : /* Bradford-Davenport algorithm.
    1211             :  * f a squarefree ZX of degree > 0, return NULL or a vector of coprime
    1212             :  * cyclotomic factors of f [ possibly reducible ] */
    1213             : static GEN
    1214        2359 : BD(GEN f)
    1215             : {
    1216        2359 :   GEN G = NULL, Gs = NULL, Gp = NULL, Gi = NULL;
    1217             :   GEN fs2, fp, f2, f1, fe, fo, fe1, fo1;
    1218        2359 :   RgX_even_odd(f, &fe, &fo);
    1219        2359 :   fe1 = ZX_eval1(fe);
    1220        2359 :   fo1 = ZX_eval1(fo);
    1221        2359 :   if (absequalii(fe1, fo1)) /* f(1) = 0 or f(-1) = 0 */
    1222             :   {
    1223        1519 :     long i, v = varn(f);
    1224        1519 :     if (!signe(fe1))
    1225         371 :       G = mkvec2(polcyclo(1, v), polcyclo(2, v)); /* both 0 */
    1226        1148 :     else if (signe(fe1) == signe(fo1))
    1227         693 :       G = mkvec(polcyclo(2, v)); /*f(-1) = 0*/
    1228             :     else
    1229         455 :       G = mkvec(polcyclo(1, v)); /*f(1) = 0*/
    1230        1519 :     for (i = lg(G)-1; i; i--) f = RgX_div(f, gel(G,i));
    1231             :   }
    1232             :   /* f no longer divisible by Phi_1 or Phi_2 */
    1233        2359 :   if (degpol(f) <= 1) return G;
    1234        2058 :   f1 = ZX_graeffe(f); /* has at most square factors */
    1235        2058 :   if (ZX_equal(f1, f)) return myconcat(G,f); /* f = product of Phi_n, n odd */
    1236             : 
    1237        1183 :   fs2 = ZX_gcd_all(f1, ZX_deriv(f1), &f2); /* fs2 squarefree */
    1238        1183 :   if (degpol(fs2))
    1239             :   { /* fs contains all Phi_n | f, 4 | n; and only those */
    1240             :     /* In that case, Graeffe(Phi_n) = Phi_{n/2}^2, and Phi_n = Phi_{n/2}(x^2) */
    1241        1029 :     GEN fs = RgX_inflate(fs2, 2);
    1242        1029 :     (void)ZX_gcd_all(f, fs, &f); /* remove those Phi_n | f, 4 | n */
    1243        1029 :     Gs = BD(fs2);
    1244        1029 :     if (Gs)
    1245             :     {
    1246             :       long i;
    1247        1029 :       for (i = lg(Gs)-1; i; i--) gel(Gs,i) = RgX_inflate(gel(Gs,i), 2);
    1248             :       /* prod Gs[i] is the product of all Phi_n | f, 4 | n */
    1249        1029 :       G = myconcat(G, Gs);
    1250             :     }
    1251             :     /* f2 = f1 / fs2 */
    1252        1029 :     f1 = RgX_div(f2, fs2); /* f1 / fs2^2 */
    1253             :   }
    1254        1183 :   fp = ZX_gcd(f, f1); /* contains all Phi_n | f, n > 1 odd; and only those */
    1255        1183 :   if (degpol(fp))
    1256             :   {
    1257         196 :     Gp = BD_odd(fp);
    1258             :     /* Gp is the product of all Phi_n | f, n odd */
    1259         196 :     if (Gp) G = myconcat(G, Gp);
    1260         196 :     f = RgX_div(f, fp);
    1261             :   }
    1262        1183 :   if (degpol(f))
    1263             :   { /* contains all Phi_n originally dividing f, n = 2 mod 4, n > 2;
    1264             :      * and only those
    1265             :      * In that case, Graeffe(Phi_n) = Phi_{n/2}, and Phi_n = Phi_{n/2}(-x) */
    1266         210 :     Gi = BD_odd(ZX_unscale(f, gen_m1));
    1267         210 :     if (Gi)
    1268             :     { /* N.B. Phi_2 does not divide f */
    1269         210 :       Gi = ZX_unscale(Gi, gen_m1);
    1270             :       /* Gi is the product of all Phi_n | f, n = 2 mod 4 */
    1271         210 :       G = myconcat(G, Gi);
    1272             :     }
    1273             :   }
    1274        1183 :   return G;
    1275             : }
    1276             : 
    1277             : /* Let f be a non-zero QX, return the (squarefree) product of cyclotomic
    1278             :  * divisors of f */
    1279             : GEN
    1280         315 : polcyclofactors(GEN f)
    1281             : {
    1282         315 :   pari_sp av = avma;
    1283         315 :   if (typ(f) != t_POL || !signe(f)) pari_err_TYPE("polcyclofactors",f);
    1284         315 :   (void)RgX_valrem(f, &f);
    1285         315 :   f = Q_primpart(f);
    1286         315 :   RgX_check_ZX(f,"polcyclofactors");
    1287         315 :   if (degpol(f))
    1288             :   {
    1289         315 :     (void)ZX_gcd_all(f, ZX_deriv(f), &f);
    1290         315 :     f = BD(f);
    1291         315 :     if (f) return gerepilecopy(av, f);
    1292             :   }
    1293           0 :   avma = av; return cgetg(1,t_VEC);
    1294             : }
    1295             : 
    1296             : /* return t*x mod T(x), T a monic ZX. Assume deg(t) < deg(T) */
    1297             : static GEN
    1298       46018 : ZXQ_mul_by_X(GEN t, GEN T)
    1299             : {
    1300             :   GEN lt;
    1301       46018 :   t = RgX_shift_shallow(t, 1);
    1302       46018 :   if (degpol(t) < degpol(T)) return t;
    1303        3822 :   lt = leading_coeff(t);
    1304        3822 :   if (is_pm1(lt)) return signe(lt) > 0 ? ZX_sub(t, T): ZX_add(t, T);
    1305         217 :   return ZX_sub(t, ZX_Z_mul(T, leading_coeff(t)));
    1306             : }
    1307             : /* f a product of Phi_n, all n odd; deg f > 1. Is it irreducible ? */
    1308             : static long
    1309         840 : BD_odd_iscyclo(GEN f)
    1310             : {
    1311             :   pari_sp av;
    1312             :   long d, e, n, bound;
    1313             :   GEN t;
    1314         840 :   f = ZX_deflate_max(f, &e);
    1315         840 :   av = avma;
    1316             :   /* The original f is cyclotomic (= Phi_{ne}) iff the present one is Phi_n,
    1317             :    * where all prime dividing e also divide n. If current f is Phi_n,
    1318             :    * then n is odd and squarefree */
    1319         840 :   d = degpol(f); /* = phi(n) */
    1320             :   /* Let e > 0, g multiplicative such that
    1321             :        g(p) = p / (p-1)^(1+e) < 1 iff p < (p-1)^(1+e)
    1322             :      For all squarefree odd n, we have g(n) < C, hence n < C phi(n)^(1+e), where
    1323             :        C = \prod_{p odd | p > (p-1)^(1+e)} g(p)
    1324             :      For e = 1/10,   we obtain p = 3, 5 and C < 1.523
    1325             :      For e = 1/100,  we obtain p = 3, 5, ..., 29 and C < 2.573
    1326             :      In fact, for n <= 10^7 odd & squarefree, we have n < 2.92 * phi(n)
    1327             :      By the above, n<10^7 covers all d <= (10^7/2.573)^(1/(1+1/100)) < 3344391.
    1328             :   */
    1329         840 :   if (d <= 3344391)
    1330         840 :     bound = (long)(2.92 * d);
    1331             :   else
    1332           0 :     bound = (long)(2.573 * pow(d,1.01));
    1333             :   /* IF f = Phi_n, n squarefree odd, then n <= bound */
    1334         840 :   t = monomial(gen_1, d-1, varn(f));
    1335       46053 :   for (n = d; n <= bound; n++)
    1336             :   {
    1337       46018 :     t = ZXQ_mul_by_X(t, f);
    1338             :     /* t = (X mod f(X))^d */
    1339       46018 :     if (degpol(t) == 0) break;
    1340       45213 :     if (gc_needed(av,1))
    1341             :     {
    1342         454 :       if(DEBUGMEM>1) pari_warn(warnmem,"BD_odd_iscyclo");
    1343         454 :       t = gerepilecopy(av, t);
    1344             :     }
    1345             :   }
    1346         840 :   if (n > bound || eulerphiu(n) != (ulong)d) return 0;
    1347             : 
    1348         777 :   if (e > 1) return (ucoprime_part(e, n) == 1)? e * n : 0;
    1349         651 :   return n;
    1350             : }
    1351             : 
    1352             : /* Checks if f, monic squarefree ZX with |constant coeff| = 1, is a cyclotomic
    1353             :  * polynomial. Returns n if f = Phi_n, and 0 otherwise */
    1354             : static long
    1355        2366 : BD_iscyclo(GEN f)
    1356             : {
    1357        2366 :   pari_sp av = avma;
    1358             :   GEN f2, fn, f1;
    1359             : 
    1360        2366 :   if (degpol(f) == 1) return isint1(gel(f,2))? 2: 1;
    1361        2233 :   f1 = ZX_graeffe(f);
    1362             :   /* f = product of Phi_n, n odd */
    1363        2233 :   if (ZX_equal(f, f1)) { avma = av; return BD_odd_iscyclo(f); }
    1364             : 
    1365        1771 :   fn = ZX_unscale(f, gen_m1); /* f(-x) */
    1366             :   /* f = product of Phi_n, n = 2 mod 4 */
    1367        1771 :   if (ZX_equal(f1, fn)) return 2*BD_odd_iscyclo(fn);
    1368             : 
    1369        1393 :   if (issquareall(f1, &f2))
    1370             :   {
    1371         595 :     GEN lt = leading_coeff(f2);
    1372             :     long c;
    1373         595 :     if (signe(lt) < 0) f2 = ZX_neg(f2);
    1374         595 :     c = BD_iscyclo(f2);
    1375         595 :     return odd(c)? 0: 2*c;
    1376             :   }
    1377         798 :   avma = av; return 0;
    1378             : }
    1379             : long
    1380        3520 : poliscyclo(GEN f)
    1381             : {
    1382             :   long d;
    1383        3520 :   if (typ(f) != t_POL) pari_err_TYPE("poliscyclo", f);
    1384        3513 :   d = degpol(f);
    1385        3513 :   if (d <= 0 || !RgX_is_ZX(f)) return 0;
    1386        3506 :   if (!equali1(gel(f,d+2)) || !is_pm1(gel(f,2))) return 0;
    1387        1806 :   if (d == 1) return signe(gel(f,2)) > 0? 2: 1;
    1388        1771 :   return ZX_is_squarefree(f)? BD_iscyclo(f): 0;
    1389             : }
    1390             : 
    1391             : long
    1392        1029 : poliscycloprod(GEN f)
    1393             : {
    1394        1029 :   pari_sp av = avma;
    1395        1029 :   long i, d = degpol(f);
    1396        1029 :   if (typ(f) != t_POL) pari_err_TYPE("poliscycloprod",f);
    1397        1029 :   if (!RgX_is_ZX(f)) return 0;
    1398        1029 :   if (!equali1(leading_coeff(f)) || !is_pm1(constant_coeff(f))) return 0;
    1399        1029 :   if (d < 2) return (d == 1);
    1400        1022 :   if ( degpol(ZX_gcd_all(f, ZX_deriv(f), &f)) )
    1401             :   {
    1402          14 :     d = degpol(f);
    1403          14 :     if (d == 1) return 1;
    1404             :   }
    1405        1015 :   f = BD(f); if (!f) return 0;
    1406        1015 :   for (i = lg(f)-1; i; i--) d -= degpol(gel(f,i));
    1407        1015 :   avma = av; return d == 0;
    1408             : }

Generated by: LCOV version 1.11