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 19191-d7615ef) Lines: 727 768 94.7 %
Date: 2016-07-23 07:10:28 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 && absi_cmp(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 = logint(B, q, qb);
     687        4767 :   amin = b + d;
     688        4767 :   if (gcmp(powiu(q, amin), A) <= 0)
     689             :   {
     690        1330 :     a = logint(A, q, qa);
     691        1330 :     b = a - d; *qb = powiu(q, b);
     692             :   }
     693             :   else
     694             :   { /* not enough room */
     695        3437 :     a = amin;  *qa = powiu(q, a);
     696        3437 :     fl = 1;
     697             :   }
     698        4767 :   if (DEBUGLEVEL > 3) {
     699           0 :     err_printf("S_2   bound: %Ps^%ld\n", q,b);
     700           0 :     err_printf("coeff bound: %Ps^%ld\n", q,a);
     701             :   }
     702        4767 :   *pta = a;
     703        4767 :   *ptb = b; return fl;
     704             : }
     705             : 
     706             : /* use van Hoeij's knapsack algorithm */
     707             : static GEN
     708        4767 : combine_factors(GEN target, GEN famod, GEN p, long klim)
     709             : {
     710             :   GEN la, B, A, res, L, pa, pb, listmod;
     711        4767 :   long a,b, l, maxK, n = degpol(target);
     712             :   int done;
     713             :   pari_timer T;
     714             : 
     715        4767 :   A = factor_bound(target);
     716             : 
     717        4767 :   la = absi(leading_coeff(target));
     718        4767 :   B = mului(n, sqri(mulii(la, root_bound(target)))); /* = bound for S_2 */
     719             : 
     720        4767 :   (void)cmbf_precs(p, A, B, &a, &b, &pa, &pb);
     721             : 
     722        4767 :   if (DEBUGLEVEL>2) timer_start(&T);
     723        4767 :   famod = ZpX_liftfact(target,famod,NULL,p,a,pa);
     724        4767 :   if (DEBUGLEVEL>2) timer_printf(&T, "Hensel lift (mod %Ps^%ld)", p,a);
     725        4767 :   L = cmbf(target, famod, A, p, a, b, klim, &maxK, &done);
     726        4767 :   if (DEBUGLEVEL>2) timer_printf(&T, "Naive recombination");
     727             : 
     728        4767 :   res     = gel(L,1);
     729        4767 :   listmod = gel(L,2); l = lg(listmod)-1;
     730        4767 :   famod = gel(listmod,l);
     731        4767 :   if (maxK > 0 && lg(famod)-1 > 2*maxK)
     732             :   {
     733          91 :     if (l!=1) A = factor_bound(gel(res,l));
     734          91 :     if (DEBUGLEVEL > 4) err_printf("last factor still to be checked\n");
     735          91 :     L = LLL_cmbf(gel(res,l), famod, p, pa, A, a, maxK);
     736          91 :     if (DEBUGLEVEL>2) timer_printf(&T,"Knapsack");
     737             :     /* remove last elt, possibly unfactored. Add all new ones. */
     738          91 :     setlg(res, l); res = shallowconcat(res, L);
     739             :   }
     740        4767 :   return res;
     741             : }
     742             : 
     743             : /* Assume 'a' a squarefree ZX; return 0 if no root (fl=1) / irreducible (fl=0).
     744             :  * Otherwise return prime p such that a mod p has fewest roots / factors */
     745             : static ulong
     746      597065 : pick_prime(GEN a, long fl, pari_timer *T)
     747             : {
     748      597065 :   pari_sp av = avma, av1;
     749      597065 :   const long MAXNP = 7, da = degpol(a);
     750      597065 :   long nmax = da+1, np;
     751      597065 :   ulong chosenp = 0;
     752      597065 :   GEN lead = gel(a,da+2);
     753             :   forprime_t S;
     754      597065 :   if (equali1(lead)) lead = NULL;
     755      597065 :   u_forprime_init(&S, 2, ULONG_MAX);
     756      597065 :   av1 = avma;
     757     3410232 :   for (np = 0; np < MAXNP; avma = av1)
     758             :   {
     759     3366223 :     ulong p = u_forprime_next(&S);
     760             :     long nfacp;
     761             :     GEN z;
     762             : 
     763     3366223 :     if (!p) pari_err_OVERFLOW("DDF [out of small primes]");
     764     3366223 :     if (lead && !umodiu(lead,p)) continue;
     765     3363955 :     z = ZX_to_Flx(a, p);
     766     3363955 :     if (!Flx_is_squarefree(z, p)) continue;
     767             : 
     768     2118578 :     if (fl)
     769             :     {
     770     2070012 :       nfacp = Flx_nbroots(z, p);
     771     2070012 :       if (!nfacp) { chosenp = 0; break; } /* no root */
     772             :     }
     773             :     else
     774             :     {
     775       48566 :       nfacp = Flx_nbfact(z, p);
     776       48566 :       if (nfacp == 1) { chosenp = 0; break; } /* irreducible */
     777             :     }
     778     1565536 :     if (DEBUGLEVEL>4)
     779           0 :       err_printf("...tried prime %3lu (%-3ld %s). Time = %ld\n",
     780             :                   p, nfacp, fl? "roots": "factors", timer_delay(T));
     781     1565536 :     if (nfacp < nmax)
     782             :     {
     783      531314 :       nmax = nfacp; chosenp = p;
     784      531314 :       if (da > 100 && nmax < 5) break; /* large degree, few factors. Enough */
     785             :     }
     786     1565522 :     np++;
     787             :   }
     788      597065 :   avma = av; return chosenp;
     789             : }
     790             : 
     791             : /* Assume pol squarefree mod p; return vector of rational roots of a */
     792             : static GEN
     793      581973 : DDF_roots(GEN A)
     794             : {
     795             :   GEN p, lc, lcpol, z, pe, pes2, bound;
     796             :   long i, m, e, lz;
     797             :   ulong pp;
     798             :   pari_sp av;
     799             :   pari_timer T;
     800             : 
     801      581973 :   if (DEBUGLEVEL>2) timer_start(&T);
     802      581973 :   pp = pick_prime(A, 1, &T);
     803      581973 :   if (!pp) return cgetg(1,t_VEC); /* no root */
     804       39256 :   p = utoipos(pp);
     805       39256 :   lc = leading_coeff(A);
     806       39256 :   if (is_pm1(lc))
     807       38381 :   { lc = NULL; lcpol = A; }
     808             :   else
     809         875 :   { lc = absi_shallow(lc); lcpol = ZX_Z_mul(A, lc); }
     810       39256 :   bound = root_bound(A); if (lc) bound = mulii(lc, bound);
     811       39256 :   e = logint(addiu(shifti(bound, 1), 1), p, &pe);
     812       39256 :   pes2 = shifti(pe, -1);
     813       39256 :   if (DEBUGLEVEL>2) timer_printf(&T, "Root bound");
     814       39256 :   av = avma;
     815       39256 :   z = ZpX_roots(A, p, e); lz = lg(z);
     816       39256 :   z = deg1_from_roots(z, varn(A));
     817       39256 :   if (DEBUGLEVEL>2) timer_printf(&T, "Hensel lift (mod %lu^%ld)", pp,e);
     818       79345 :   for (m=1, i=1; i < lz; i++)
     819             :   {
     820       40089 :     GEN q, r, y = gel(z,i);
     821       40089 :     if (lc) y = ZX_Z_mul(y, lc);
     822       40089 :     y = centermod_i(y, pe, pes2);
     823       40089 :     if (! (q = ZX_divides(lcpol, y)) ) continue;
     824             : 
     825        2240 :     lcpol = q;
     826        2240 :     r = negi( constant_coeff(y) );
     827        2240 :     if (lc) {
     828        1246 :       r = gdiv(r,lc);
     829        1246 :       lcpol = Q_primpart(lcpol);
     830        1246 :       lc = absi_shallow( leading_coeff(lcpol) );
     831        1246 :       if (is_pm1(lc)) lc = NULL; else lcpol = ZX_Z_mul(lcpol, lc);
     832             :     }
     833        2240 :     gel(z,m++) = r;
     834        2240 :     if (gc_needed(av,2))
     835             :     {
     836           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"DDF_roots, m = %ld", m);
     837           0 :       gerepileall(av, lc? 3:2, &z, &lcpol, &lc);
     838             : 
     839             :     }
     840             :   }
     841       39256 :   if (DEBUGLEVEL>2) timer_printf(&T, "Recombination");
     842       39256 :   z[0] = evaltyp(t_VEC) | evallg(m); return z;
     843             : }
     844             : 
     845             : /* Assume a squarefree ZX, deg(a) > 0, return rational factors.
     846             :  * In fact, a(0) != 0 but we don't use this */
     847             : static GEN
     848       15092 : DDF(GEN a)
     849             : {
     850             :   GEN ap, prime, famod, z;
     851       15092 :   long ti = 0;
     852       15092 :   ulong p = 0;
     853       15092 :   pari_sp av = avma;
     854             :   pari_timer T, T2;
     855             : 
     856       15092 :   if (DEBUGLEVEL>2) { timer_start(&T); timer_start(&T2); }
     857       15092 :   p = pick_prime(a, 0, &T2);
     858       15092 :   if (!p) return mkvec(a);
     859        4767 :   prime = utoipos(p);
     860        4767 :   ap = Flx_normalize(ZX_to_Flx(a, p), p);
     861        4767 :   famod = gel(Flx_factor(ap, p), 1);
     862        4767 :   if (DEBUGLEVEL>2)
     863             :   {
     864           0 :     if (DEBUGLEVEL>4) timer_printf(&T2, "splitting mod p = %lu", p);
     865           0 :     ti = timer_delay(&T);
     866           0 :     err_printf("Time setup: %ld\n", ti);
     867             :   }
     868        4767 :   z = combine_factors(a, FlxV_to_ZXV(famod), prime, degpol(a)-1);
     869        4767 :   if (DEBUGLEVEL>2)
     870           0 :     err_printf("Total Time: %ld\n===========\n", ti + timer_delay(&T));
     871        4767 :   return gerepilecopy(av, z);
     872             : }
     873             : 
     874             : /* Distinct Degree Factorization (deflating first)
     875             :  * Assume x squarefree, degree(x) > 0, x(0) != 0 */
     876             : GEN
     877       10710 : ZX_DDF(GEN x)
     878             : {
     879             :   GEN L;
     880             :   long m;
     881       10710 :   x = ZX_deflate_max(x, &m);
     882       10710 :   L = DDF(x);
     883       10710 :   if (m > 1)
     884             :   {
     885        3563 :     GEN e, v, fa = factoru(m);
     886             :     long i,j,k, l;
     887             : 
     888        3563 :     e = gel(fa,2); k = 0;
     889        3563 :     fa= gel(fa,1); l = lg(fa);
     890        3563 :     for (i=1; i<l; i++) k += e[i];
     891        3563 :     v = cgetg(k+1, t_VECSMALL); k = 1;
     892        7259 :     for (i=1; i<l; i++)
     893        3696 :       for (j=1; j<=e[i]; j++) v[k++] = fa[i];
     894        7868 :     for (k--; k; k--)
     895             :     {
     896        4305 :       GEN L2 = cgetg(1,t_VEC);
     897        8687 :       for (i=1; i < lg(L); i++)
     898        4382 :               L2 = shallowconcat(L2, DDF(RgX_inflate(gel(L,i), v[k])));
     899        4305 :       L = L2;
     900             :     }
     901             :   }
     902       10710 :   return L;
     903             : }
     904             : 
     905             : /* SquareFree Factorization. f = prod P^e, all e distinct, in Z[X] (char 0
     906             :  * would be enough, if ZX_gcd --> ggcd). Return (P), set *ex = (e) */
     907             : GEN
     908        7479 : ZX_squff(GEN f, GEN *ex)
     909             : {
     910             :   GEN T, V, P, e;
     911             :   long i, k, n, val;
     912             : 
     913        7479 :   if (signe(leading_coeff(f)) < 0) f = gneg_i(f);
     914        7479 :   val = ZX_valrem(f, &f);
     915        7479 :   n = 1 + degpol(f); if (val) n++;
     916        7479 :   e = cgetg(n,t_VECSMALL);
     917        7479 :   P = cgetg(n,t_COL);
     918             : 
     919        7479 :   T = ZX_gcd_all(f, ZX_deriv(f), &V);
     920        7822 :   for (k=i=1;; k++)
     921             :   {
     922        7822 :     pari_sp av = avma;
     923        7822 :     GEN W = ZX_gcd_all(T,V, &T);
     924        7822 :     long dW = degpol(W);
     925             :     /* W = prod P^e, e > k; V = prod P^e, e >= k */
     926        7822 :     if (dW == degpol(V)) /* V | T */
     927             :     {
     928             :       GEN U;
     929        1225 :       if (!dW) { avma = av; break; }
     930         217 :       while ( (U = ZX_divides(T, V)) ) { k++; T = U; }
     931         217 :       T = gerepileupto(av, T);
     932             :     }
     933             :     else
     934             :     {
     935        6597 :       gel(P,i) = Q_primpart(RgX_div(V,W));
     936        6597 :       e[i] = k; i++;
     937        6597 :       if (!dW) break;
     938         126 :       V = W;
     939             :     }
     940         343 :   }
     941        7479 :   if (val) { gel(P,i) = pol_x(varn(f)); e[i] = val; i++;}
     942        7479 :   setlg(P,i);
     943        7479 :   setlg(e,i); *ex = e; return P;
     944             : }
     945             : 
     946             : static GEN
     947        2758 : fact_from_DDF(GEN fa, GEN e, long n)
     948             : {
     949        2758 :   GEN v,w, y = cgetg(3, t_MAT);
     950        2758 :   long i,j,k, l = lg(fa);
     951             : 
     952        2758 :   v = cgetg(n+1,t_COL); gel(y,1) = v;
     953        2758 :   w = cgetg(n+1,t_COL); gel(y,2) = w;
     954        5782 :   for (k=i=1; i<l; i++)
     955             :   {
     956        3024 :     GEN L = gel(fa,i), ex = utoipos(e[i]);
     957        3024 :     long J = lg(L);
     958        7924 :     for (j=1; j<J; j++,k++)
     959             :     {
     960        4900 :       gel(v,k) = gcopy(gel(L,j));
     961        4900 :       gel(w,k) = ex;
     962             :     }
     963             :   }
     964        2758 :   return y;
     965             : }
     966             : 
     967             : /* Factor x in Z[t] */
     968             : static GEN
     969        2758 : ZX_factor_i(GEN x)
     970             : {
     971             :   GEN fa,ex,y;
     972             :   long n,i,l;
     973             : 
     974        2758 :   if (!signe(x)) return prime_fact(x);
     975        2758 :   fa = ZX_squff(x, &ex);
     976        2758 :   l = lg(fa); n = 0;
     977        5782 :   for (i=1; i<l; i++)
     978             :   {
     979        3024 :     gel(fa,i) = ZX_DDF(gel(fa,i));
     980        3024 :     n += lg(gel(fa,i))-1;
     981             :   }
     982        2758 :   y = fact_from_DDF(fa,ex,n);
     983        2758 :   return sort_factor_pol(y, cmpii);
     984             : }
     985             : GEN
     986        2443 : ZX_factor(GEN x)
     987             : {
     988        2443 :   pari_sp av = avma;
     989        2443 :   return gerepileupto(av, ZX_factor_i(x));
     990             : }
     991             : GEN
     992         315 : QX_factor(GEN x)
     993             : {
     994         315 :   pari_sp av = avma;
     995         315 :   return gerepileupto(av, ZX_factor_i(Q_primpart(x)));
     996             : }
     997             : 
     998             : long
     999        8687 : ZX_is_irred(GEN x)
    1000             : {
    1001        8687 :   pari_sp av = avma;
    1002        8687 :   long l = lg(x);
    1003             :   GEN y;
    1004        8687 :   if (l <= 3) return 0; /* degree < 1 */
    1005        8687 :   if (l == 4) return 1; /* degree 1 */
    1006        7427 :   if (ZX_val(x)) return 0;
    1007        7224 :   if (!ZX_is_squarefree(x)) return 0;
    1008        7175 :   y = ZX_DDF(x); avma = av;
    1009        7175 :   return (lg(y) == 2);
    1010             : }
    1011             : 
    1012             : GEN
    1013      581973 : nfrootsQ(GEN x)
    1014             : {
    1015      581973 :   pari_sp av = avma;
    1016             :   GEN z;
    1017             :   long val;
    1018             : 
    1019      581973 :   if (typ(x)!=t_POL) pari_err_TYPE("nfrootsQ",x);
    1020      581973 :   if (!signe(x)) pari_err_ROOTS0("nfrootsQ");
    1021      581973 :   x = Q_primpart(x);
    1022      581973 :   RgX_check_ZX(x,"nfrootsQ");
    1023      581973 :   val = ZX_valrem(x, &x);
    1024      581973 :   (void)ZX_gcd_all(x, ZX_deriv(x), &x);
    1025      581973 :   z = DDF_roots(x);
    1026      581973 :   if (val) z = shallowconcat(z, gen_0);
    1027      581973 :   return gerepileupto(av, sort(z));
    1028             : }
    1029             : 
    1030             : /************************************************************************
    1031             :  *                   GCD OVER Z[X] / Q[X]                               *
    1032             :  ************************************************************************/
    1033             : int
    1034       16464 : ZX_is_squarefree(GEN x)
    1035             : {
    1036       16464 :   pari_sp av = avma;
    1037             :   GEN d;
    1038             :   long m;
    1039             :   int r;
    1040       16464 :   if (lg(x) == 2) return 0;
    1041       16464 :   m = ZX_deflate_order(x);
    1042       16464 :   if (m > 1)
    1043             :   {
    1044        4830 :     if (!signe(gel(x,2))) return 0;
    1045        4767 :     x = RgX_deflate(x, m);
    1046             :   }
    1047       16401 :   d = ZX_gcd(x,ZX_deriv(x));
    1048       16401 :   r = (lg(d) == 3); avma = av; return r;
    1049             : }
    1050             : 
    1051             : #if 0
    1052             : /* ceil( || p ||_oo / lc(p) ) */
    1053             : static GEN
    1054             : maxnorm(GEN p)
    1055             : {
    1056             :   long i, n = degpol(p), av = avma;
    1057             :   GEN x, m = gen_0;
    1058             : 
    1059             :   p += 2;
    1060             :   for (i=0; i<n; i++)
    1061             :   {
    1062             :     x = gel(p,i);
    1063             :     if (absi_cmp(x,m) > 0) m = x;
    1064             :   }
    1065             :   m = divii(m, gel(p,n));
    1066             :   return gerepileuptoint(av, addis(absi(m),1));
    1067             : }
    1068             : #endif
    1069             : 
    1070             : /* A, B in Z[X] */
    1071             : GEN
    1072     4096866 : ZX_gcd_all(GEN A, GEN B, GEN *Anew)
    1073             : {
    1074             :   GEN R, a, b, q, H, Hp, g, Ag, Bg;
    1075     4096866 :   long m, n, valX, valA, vA = varn(A);
    1076             :   ulong p;
    1077             :   pari_sp ltop, av;
    1078             :   forprime_t S;
    1079             : 
    1080     4096866 :   if (!signe(A)) { if (Anew) *Anew = pol_0(vA); return ZX_copy(B); }
    1081     4096866 :   if (!signe(B)) { if (Anew) *Anew = pol_1(vA); return ZX_copy(A); }
    1082     4095774 :   valA = ZX_valrem(A, &A);
    1083     4095774 :   valX = minss(valA, ZX_valrem(B, &B));
    1084     4095774 :   ltop = avma;
    1085             : 
    1086     4095774 :   n = 1 + minss(degpol(A), degpol(B)); /* > degree(gcd) */
    1087     4095774 :   g = gcdii(leading_coeff(A), leading_coeff(B)); /* multiple of lead(gcd) */
    1088     4095774 :   if (is_pm1(g)) {
    1089     4089738 :     g = NULL;
    1090     4089738 :     Ag = A;
    1091     4089738 :     Bg = B;
    1092             :   } else {
    1093        6036 :     Ag = ZX_Z_mul(A,g);
    1094        6036 :     Bg = ZX_Z_mul(B,g);
    1095             :   }
    1096     4095774 :   init_modular_big(&S);
    1097     4095774 :   av = avma;
    1098     4095774 :   R = NULL;/*-Wall*/
    1099     4095774 :   H = NULL;
    1100    11302463 :   while ((p = u_forprime_next(&S)))
    1101             :   {
    1102     7206689 :     if (g && !umodiu(g,p)) continue;
    1103     7206689 :     a = ZX_to_Flx(A, p);
    1104     7206689 :     b = ZX_to_Flx(B, p); Hp = Flx_gcd(a,b, p);
    1105     7206689 :     m = degpol(Hp);
    1106     7206689 :     if (m == 0) { /* coprime. DONE */
    1107      985217 :       avma = ltop;
    1108      985217 :       if (Anew) {
    1109      602952 :         if (valA != valX) A = RgX_shift(A, valA - valX);
    1110      602952 :         *Anew = A;
    1111             :       }
    1112      985217 :       return monomial(gen_1, valX, vA);
    1113             :     }
    1114     6221472 :     if (m > n) continue; /* p | Res(A/G, B/G). Discard */
    1115             : 
    1116     6221472 :     if (!g) /* make sure lead(H) = g mod p */
    1117     6219464 :       Hp = Flx_normalize(Hp, p);
    1118             :     else
    1119             :     {
    1120        2008 :       ulong t = Fl_mul(umodiu(g, p), Fl_inv(Hp[m+2],p), p);
    1121        2008 :       Hp = Flx_Fl_mul(Hp, t, p);
    1122             :     }
    1123     6221472 :     if (m < n)
    1124             :     { /* First time or degree drop [all previous p were as above; restart]. */
    1125     3110557 :       H = ZX_init_CRT(Hp,p,vA);
    1126     3110557 :       q = utoipos(p); n = m; continue;
    1127             :     }
    1128     3110915 :     if (DEBUGLEVEL>5) err_printf("gcd mod %lu (bound 2^%ld)\n", p,expi(q));
    1129     3110915 :     if (gc_needed(av,1))
    1130             :     {
    1131           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"QX_gcd");
    1132           0 :       gerepileall(av, 3, &H, &q, &Hp);
    1133             :     }
    1134             : 
    1135     3110915 :     if (!ZX_incremental_CRT(&H, Hp, &q, p)) continue;
    1136             :     /* H stable: check divisibility */
    1137     3110557 :     if (!ZX_divides(Bg, H)) continue;
    1138     3110557 :     R = ZX_divides(Ag, H);
    1139     3110557 :     if (R) break;
    1140             :   }
    1141     3110557 :   if (!p) pari_err_OVERFLOW("ZX_gcd_all [ran out of primes]");
    1142     3110557 :   if (Anew) {
    1143       11306 :     A = R;
    1144       11306 :     if (valA != valX) A = RgX_shift(A, valA - valX);
    1145       11306 :     *Anew = A;
    1146             :   }
    1147     3110557 :   return valX ? RgX_shift(H, valX): H;
    1148             : }
    1149             : GEN
    1150     3481523 : ZX_gcd(GEN A, GEN B) { return ZX_gcd_all(A,B,NULL); }
    1151             : 
    1152             : static GEN
    1153     3453346 : _gcd(GEN a, GEN b)
    1154             : {
    1155     3453346 :   if (!a) a = gen_1;
    1156     3453346 :   if (!b) b = gen_1;
    1157     3453346 :   return Q_gcd(a,b);
    1158             : }
    1159             : /* A0 and B0 in Q[X] */
    1160             : GEN
    1161     3453346 : QX_gcd(GEN A0, GEN B0)
    1162             : {
    1163             :   GEN a, b, D;
    1164     3453346 :   pari_sp av = avma, av2;
    1165             : 
    1166     3453346 :   D = ZX_gcd(Q_primitive_part(A0, &a), Q_primitive_part(B0, &b));
    1167     3453346 :   av2 = avma; a = _gcd(a,b);
    1168     3453346 :   if (isint1(a)) avma = av2; else D = RgX_Rg_mul(D, a);
    1169     3453346 :   return gerepileupto(av, D);
    1170             : }
    1171             : 
    1172             : /*****************************************************************************
    1173             :  * Variants of the Bradford-Davenport algorithm: look for cyclotomic         *
    1174             :  * factors, and decide whether a ZX is cyclotomic or a product of cyclotomic *
    1175             :  *****************************************************************************/
    1176             : /* f of degree 1, return a cyclotomic factor (Phi_1 or Phi_2) or NULL */
    1177             : static GEN
    1178           0 : BD_deg1(GEN f)
    1179             : {
    1180           0 :   GEN a = gel(f,3), b = gel(f,2); /* f = ax + b */
    1181           0 :   if (!absi_equal(a,b)) return NULL;
    1182           0 :   return polcyclo((signe(a) == signe(b))? 2: 1, varn(f));
    1183             : }
    1184             : 
    1185             : /* f a squarefree ZX; not divisible by any Phi_n, n even */
    1186             : static GEN
    1187         406 : BD_odd(GEN f)
    1188             : {
    1189         812 :   while(degpol(f) > 1)
    1190             :   {
    1191         406 :     GEN f1 = ZX_graeffe(f); /* contain all cyclotomic divisors of f */
    1192         406 :     if (ZX_equal(f1, f)) return f; /* product of cyclotomics */
    1193           0 :     f = ZX_gcd(f, f1);
    1194             :   }
    1195           0 :   if (degpol(f) == 1) return BD_deg1(f);
    1196           0 :   return NULL; /* no cyclotomic divisor */
    1197             : }
    1198             : 
    1199             : static GEN
    1200        2310 : myconcat(GEN v, GEN x)
    1201             : {
    1202        2310 :   if (typ(x) != t_VEC) x = mkvec(x);
    1203        2310 :   if (!v) return x;
    1204        1470 :   return shallowconcat(v, x);
    1205             : }
    1206             : 
    1207             : /* Bradford-Davenport algorithm.
    1208             :  * f a squarefree ZX of degree > 0, return NULL or a vector of coprime
    1209             :  * cyclotomic factors of f [ possibly reducible ] */
    1210             : static GEN
    1211        2359 : BD(GEN f)
    1212             : {
    1213        2359 :   GEN G = NULL, Gs = NULL, Gp = NULL, Gi = NULL;
    1214             :   GEN fs2, fp, f2, f1, fe, fo, fe1, fo1;
    1215        2359 :   RgX_even_odd(f, &fe, &fo);
    1216        2359 :   fe1 = ZX_eval1(fe);
    1217        2359 :   fo1 = ZX_eval1(fo);
    1218        2359 :   if (absi_equal(fe1, fo1)) /* f(1) = 0 or f(-1) = 0 */
    1219             :   {
    1220        1519 :     long i, v = varn(f);
    1221        1519 :     if (!signe(fe1))
    1222         371 :       G = mkvec2(polcyclo(1, v), polcyclo(2, v)); /* both 0 */
    1223        1148 :     else if (signe(fe1) == signe(fo1))
    1224         693 :       G = mkvec(polcyclo(2, v)); /*f(-1) = 0*/
    1225             :     else
    1226         455 :       G = mkvec(polcyclo(1, v)); /*f(1) = 0*/
    1227        1519 :     for (i = lg(G)-1; i; i--) f = RgX_div(f, gel(G,i));
    1228             :   }
    1229             :   /* f no longer divisible by Phi_1 or Phi_2 */
    1230        2359 :   if (degpol(f) <= 1) return G;
    1231        2058 :   f1 = ZX_graeffe(f); /* has at most square factors */
    1232        2058 :   if (ZX_equal(f1, f)) return myconcat(G,f); /* f = product of Phi_n, n odd */
    1233             : 
    1234        1183 :   fs2 = ZX_gcd_all(f1, ZX_deriv(f1), &f2); /* fs2 squarefree */
    1235        1183 :   if (degpol(fs2))
    1236             :   { /* fs contains all Phi_n | f, 4 | n; and only those */
    1237             :     /* In that case, Graeffe(Phi_n) = Phi_{n/2}^2, and Phi_n = Phi_{n/2}(x^2) */
    1238        1029 :     GEN fs = RgX_inflate(fs2, 2);
    1239        1029 :     (void)ZX_gcd_all(f, fs, &f); /* remove those Phi_n | f, 4 | n */
    1240        1029 :     Gs = BD(fs2);
    1241        1029 :     if (Gs)
    1242             :     {
    1243             :       long i;
    1244        1029 :       for (i = lg(Gs)-1; i; i--) gel(Gs,i) = RgX_inflate(gel(Gs,i), 2);
    1245             :       /* prod Gs[i] is the product of all Phi_n | f, 4 | n */
    1246        1029 :       G = myconcat(G, Gs);
    1247             :     }
    1248             :     /* f2 = f1 / fs2 */
    1249        1029 :     f1 = RgX_div(f2, fs2); /* f1 / fs2^2 */
    1250             :   }
    1251        1183 :   fp = ZX_gcd(f, f1); /* contains all Phi_n | f, n > 1 odd; and only those */
    1252        1183 :   if (degpol(fp))
    1253             :   {
    1254         196 :     Gp = BD_odd(fp);
    1255             :     /* Gp is the product of all Phi_n | f, n odd */
    1256         196 :     if (Gp) G = myconcat(G, Gp);
    1257         196 :     f = RgX_div(f, fp);
    1258             :   }
    1259        1183 :   if (degpol(f))
    1260             :   { /* contains all Phi_n originally dividing f, n = 2 mod 4, n > 2;
    1261             :      * and only those
    1262             :      * In that case, Graeffe(Phi_n) = Phi_{n/2}, and Phi_n = Phi_{n/2}(-x) */
    1263         210 :     Gi = BD_odd(ZX_unscale(f, gen_m1));
    1264         210 :     if (Gi)
    1265             :     { /* N.B. Phi_2 does not divide f */
    1266         210 :       Gi = ZX_unscale(Gi, gen_m1);
    1267             :       /* Gi is the product of all Phi_n | f, n = 2 mod 4 */
    1268         210 :       G = myconcat(G, Gi);
    1269             :     }
    1270             :   }
    1271        1183 :   return G;
    1272             : }
    1273             : 
    1274             : /* Let f be a non-zero QX, return the (squarefree) product of cyclotomic
    1275             :  * divisors of f */
    1276             : GEN
    1277         315 : polcyclofactors(GEN f)
    1278             : {
    1279         315 :   pari_sp av = avma;
    1280         315 :   if (typ(f) != t_POL || !signe(f)) pari_err_TYPE("polcyclofactors",f);
    1281         315 :   (void)RgX_valrem(f, &f);
    1282         315 :   f = Q_primpart(f);
    1283         315 :   RgX_check_ZX(f,"polcyclofactors");
    1284         315 :   if (degpol(f))
    1285             :   {
    1286         315 :     (void)ZX_gcd_all(f, ZX_deriv(f), &f);
    1287         315 :     f = BD(f);
    1288         315 :     if (f) return gerepilecopy(av, f);
    1289             :   }
    1290           0 :   avma = av; return cgetg(1,t_VEC);
    1291             : }
    1292             : 
    1293             : /* return t*x mod T(x), T a monic ZX. Assume deg(t) < deg(T) */
    1294             : static GEN
    1295       46116 : ZXQ_mul_by_X(GEN t, GEN T)
    1296             : {
    1297             :   GEN lt;
    1298       46116 :   t = RgX_shift_shallow(t, 1);
    1299       46116 :   if (degpol(t) < degpol(T)) return t;
    1300        3920 :   lt = leading_coeff(t);
    1301        3920 :   if (is_pm1(lt)) return signe(lt) > 0 ? ZX_sub(t, T): ZX_add(t, T);
    1302         217 :   return ZX_sub(t, ZX_Z_mul(T, leading_coeff(t)));
    1303             : }
    1304             : /* f a product of Phi_n, all n odd; deg f > 1. Is it irreducible ? */
    1305             : static long
    1306         889 : BD_odd_iscyclo(GEN f)
    1307             : {
    1308             :   pari_sp av;
    1309             :   long d, e, n, bound;
    1310             :   GEN t;
    1311         889 :   f = ZX_deflate_max(f, &e);
    1312         889 :   av = avma;
    1313             :   /* The original f is cyclotomic (= Phi_{ne}) iff the present one is Phi_n,
    1314             :    * where all prime dividing e also divide n. If current f is Phi_n,
    1315             :    * then n is odd and squarefree */
    1316         889 :   d = degpol(f); /* = phi(n) */
    1317             :   /* Let e > 0, g multiplicative such that
    1318             :        g(p) = p / (p-1)^(1+e) < 1 iff p < (p-1)^(1+e)
    1319             :      For all squarefree odd n, we have g(n) < C, hence n < C phi(n)^(1+e), where
    1320             :        C = \prod_{p odd | p > (p-1)^(1+e)} g(p)
    1321             :      For e = 1/10,   we obtain p = 3, 5 and C < 1.523
    1322             :      For e = 1/100,  we obtain p = 3, 5, ..., 29 and C < 2.573
    1323             :      In fact, for n <= 10^7 odd & squarefree, we have n < 2.92 * phi(n)
    1324             :      By the above, n<10^7 covers all d <= (10^7/2.573)^(1/(1+1/100)) < 3344391.
    1325             :   */
    1326         889 :   if (d <= 3344391)
    1327         889 :     bound = (long)(2.92 * d);
    1328             :   else
    1329           0 :     bound = (long)(2.573 * pow(d,1.01));
    1330             :   /* IF f = Phi_n, n squarefree odd, then n <= bound */
    1331         889 :   t = monomial(gen_1, d-1, varn(f));
    1332       46151 :   for (n = d; n <= bound; n++)
    1333             :   {
    1334       46116 :     t = ZXQ_mul_by_X(t, f);
    1335             :     /* t = (X mod f(X))^d */
    1336       46116 :     if (degpol(t) == 0) break;
    1337       45262 :     if (gc_needed(av,1))
    1338             :     {
    1339         454 :       if(DEBUGMEM>1) pari_warn(warnmem,"BD_odd_iscyclo");
    1340         454 :       t = gerepilecopy(av, t);
    1341             :     }
    1342             :   }
    1343         889 :   if (n > bound || eulerphiu(n) != (ulong)d) return 0;
    1344             : 
    1345         826 :   if (e > 1) return (ucoprime_part(e, n) == 1)? e * n : 0;
    1346         700 :   return n;
    1347             : }
    1348             : 
    1349             : /* Checks if f, monic squarefree ZX with |constant coeff| = 1, is a cyclotomic
    1350             :  * polynomial. Returns n if f = Phi_n, and 0 otherwise */
    1351             : static long
    1352        2562 : BD_iscyclo(GEN f)
    1353             : {
    1354        2562 :   pari_sp av = avma;
    1355             :   GEN f2, fn, f1;
    1356             : 
    1357        2562 :   if (degpol(f) == 1) return isint1(gel(f,2))? 2: 1;
    1358        2352 :   f1 = ZX_graeffe(f);
    1359             :   /* f = product of Phi_n, n odd */
    1360        2352 :   if (ZX_equal(f, f1)) { avma = av; return BD_odd_iscyclo(f); }
    1361             : 
    1362        1876 :   fn = ZX_unscale(f, gen_m1); /* f(-x) */
    1363             :   /* f = product of Phi_n, n = 2 mod 4 */
    1364        1876 :   if (ZX_equal(f1, fn)) return 2*BD_odd_iscyclo(fn);
    1365             : 
    1366        1463 :   if (issquareall(f1, &f2))
    1367             :   {
    1368         672 :     GEN lt = leading_coeff(f2);
    1369             :     long c;
    1370         672 :     if (signe(lt) < 0) f2 = ZX_neg(f2);
    1371         672 :     c = BD_iscyclo(f2);
    1372         672 :     return odd(c)? 0: 2*c;
    1373             :   }
    1374         791 :   avma = av; return 0;
    1375             : }
    1376             : long
    1377        4318 : poliscyclo(GEN f)
    1378             : {
    1379             :   long d;
    1380        4318 :   if (typ(f) != t_POL) pari_err_TYPE("poliscyclo", f);
    1381        4311 :   d = degpol(f);
    1382        4311 :   if (d <= 0 || !RgX_is_ZX(f)) return 0;
    1383        4304 :   if (!equali1(gel(f,d+2)) || !is_pm1(gel(f,2))) return 0;
    1384        1925 :   if (d == 1) return signe(gel(f,2)) > 0? 2: 1;
    1385        1890 :   return ZX_is_squarefree(f)? BD_iscyclo(f): 0;
    1386             : }
    1387             : 
    1388             : long
    1389        1029 : poliscycloprod(GEN f)
    1390             : {
    1391        1029 :   pari_sp av = avma;
    1392        1029 :   long i, d = degpol(f);
    1393        1029 :   if (typ(f) != t_POL) pari_err_TYPE("poliscycloprod",f);
    1394        1029 :   if (!RgX_is_ZX(f)) return 0;
    1395        1029 :   if (!equali1(leading_coeff(f)) || !is_pm1(constant_coeff(f))) return 0;
    1396        1029 :   if (d < 2) return (d == 1);
    1397        1022 :   if ( degpol(ZX_gcd_all(f, ZX_deriv(f), &f)) )
    1398             :   {
    1399          14 :     d = degpol(f);
    1400          14 :     if (d == 1) return 1;
    1401             :   }
    1402        1015 :   f = BD(f); if (!f) return 0;
    1403        1015 :   for (i = lg(f)-1; i; i--) d -= degpol(gel(f,i));
    1404        1015 :   avma = av; return d == 0;
    1405             : }

Generated by: LCOV version 1.11