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 - bibli1.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20443-183d202) Lines: 984 1034 95.2 %
Date: 2017-03-27 05:17:48 Functions: 59 65 90.8 %
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             : 
      14             : /********************************************************************/
      15             : /**                                                                **/
      16             : /**                 LLL Algorithm and close friends                **/
      17             : /**                                                                **/
      18             : /********************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : /********************************************************************/
      23             : /**             QR Factorization via Householder matrices          **/
      24             : /********************************************************************/
      25             : static int
      26      715420 : no_prec_pb(GEN x)
      27             : {
      28     1954695 :   return (typ(x) != t_REAL || realprec(x) > LOWDEFAULTPREC
      29      717140 :                            || expo(x) < BITS_IN_LONG/2);
      30             : }
      31             : /* Find a Householder transformation which, applied to x[k..#x], zeroes
      32             :  * x[k+1..#x]; fill L = (mu_{i,j}). Return 0 if precision problem [obtained
      33             :  * a 0 vector], 1 otherwise */
      34             : static int
      35      715420 : FindApplyQ(GEN x, GEN L, GEN B, long k, GEN Q, long prec)
      36             : {
      37      715420 :   long i, lx = lg(x)-1;
      38      715420 :   GEN x2, x1, xd = x + (k-1);
      39             : 
      40      715420 :   x1 = gel(xd,1);
      41      715420 :   x2 = mpsqr(x1);
      42      715420 :   if (k < lx)
      43             :   {
      44      480270 :     long lv = lx - (k-1) + 1;
      45      480270 :     GEN beta, Nx, v = cgetg(lv, t_VEC);
      46     1678935 :     for (i=2; i<lv; i++) {
      47     1198665 :       x2 = mpadd(x2, mpsqr(gel(xd,i)));
      48     1198665 :       gel(v,i) = gel(xd,i);
      49             :     }
      50      480270 :     if (!signe(x2)) return 0;
      51      480270 :     Nx = gsqrt(x2, prec); if (signe(x1) < 0) setsigne(Nx, -1);
      52      480270 :     gel(v,1) = mpadd(x1, Nx);
      53             : 
      54      480270 :     if (!signe(x1))
      55          85 :       beta = gtofp(x2, prec); /* make sure typ(beta) != t_INT */
      56             :     else
      57      480185 :       beta = mpadd(x2, mpmul(Nx,x1));
      58      480270 :     gel(Q,k) = mkvec2(invr(beta), v);
      59             : 
      60      480270 :     togglesign(Nx);
      61      480270 :     gcoeff(L,k,k) = Nx;
      62             :   }
      63             :   else
      64      235150 :     gcoeff(L,k,k) = gel(x,k);
      65      715420 :   gel(B,k) = x2;
      66      715420 :   for (i=1; i<k; i++) gcoeff(L,k,i) = gel(x,i);
      67      715420 :   return no_prec_pb(x2);
      68             : }
      69             : 
      70             : /* apply Householder transformation Q = [beta,v] to r with t_INT/t_REAL
      71             :  * coefficients, in place: r -= ((0|v).r * beta) v */
      72             : static void
      73     1198650 : ApplyQ(GEN Q, GEN r)
      74             : {
      75     1198650 :   GEN s, rd, beta = gel(Q,1), v = gel(Q,2);
      76     1198650 :   long i, l = lg(v), lr = lg(r);
      77             : 
      78     1198650 :   rd = r + (lr - l);
      79     1198650 :   s = mpmul(gel(v,1), gel(rd,1));
      80     1198650 :   for (i=2; i<l; i++) s = mpadd(s, mpmul(gel(v,i) ,gel(rd,i)));
      81     1198650 :   s = mpmul(beta, s);
      82     8605165 :   for (i=1; i<l; i++)
      83     7406515 :     if (signe(gel(v,i))) gel(rd,i) = mpsub(gel(rd,i), mpmul(s, gel(v,i)));
      84     1198650 : }
      85             : /* apply Q[1], ..., Q[j-1] to r */
      86             : static GEN
      87      480265 : ApplyAllQ(GEN Q, GEN r, long j)
      88             : {
      89      480265 :   pari_sp av = avma;
      90             :   long i;
      91      480265 :   r = leafcopy(r);
      92      480265 :   for (i=1; i<j; i++) ApplyQ(gel(Q,i), r);
      93      480265 :   return gerepilecopy(av, r);
      94             : }
      95             : 
      96             : /* same, arbitrary coefficients [20% slower for t_REAL at DEFAULTPREC] */
      97             : static void
      98       15600 : RgC_ApplyQ(GEN Q, GEN r)
      99             : {
     100       15600 :   GEN s, rd, beta = gel(Q,1), v = gel(Q,2);
     101       15600 :   long i, l = lg(v), lr = lg(r);
     102             : 
     103       15600 :   rd = r + (lr - l);
     104       15600 :   s = gmul(gel(v,1), gel(rd,1));
     105       15600 :   for (i=2; i<l; i++) s = gadd(s, gmul(gel(v,i) ,gel(rd,i)));
     106       15600 :   s = gmul(beta, s);
     107      343200 :   for (i=1; i<l; i++)
     108      327600 :     if (signe(gel(v,i))) gel(rd,i) = gsub(gel(rd,i), gmul(s, gel(v,i)));
     109       15600 : }
     110             : static GEN
     111         400 : RgC_ApplyAllQ(GEN Q, GEN r, long j)
     112             : {
     113         400 :   pari_sp av = avma;
     114             :   long i;
     115         400 :   r = leafcopy(r);
     116         400 :   for (i=1; i<j; i++) RgC_ApplyQ(gel(Q,i), r);
     117         400 :   return gerepilecopy(av, r);
     118             : }
     119             : 
     120             : int
     121          10 : RgM_QR_init(GEN x, GEN *pB, GEN *pQ, GEN *pL, long prec)
     122             : {
     123          10 :   x = RgM_gtomp(x, prec);
     124          10 :   return QR_init(x, pB, pQ, pL, prec);
     125             : }
     126             : 
     127             : static void
     128          20 : check_householder(GEN Q)
     129             : {
     130          20 :   long i, l = lg(Q);
     131          20 :   if (typ(Q) != t_VEC) pari_err_TYPE("mathouseholder", Q);
     132         600 :   for (i = 1; i < l; i++)
     133             :   {
     134         585 :     GEN q = gel(Q,i), v;
     135         585 :     if (typ(q) != t_VEC || lg(q) != 3) pari_err_TYPE("mathouseholder", Q);
     136         585 :     v = gel(q,2);
     137         585 :     if (typ(v) != t_VEC || lg(v)+i-2 != l) pari_err_TYPE("mathouseholder", Q);
     138             :   }
     139          15 : }
     140             : 
     141             : GEN
     142          20 : mathouseholder(GEN Q, GEN v)
     143             : {
     144          20 :   long l = lg(Q);
     145          20 :   check_householder(Q);
     146          15 :   switch(typ(v))
     147             :   {
     148             :     case t_MAT:
     149             :     {
     150             :       long lx, i;
     151          10 :       GEN M = cgetg_copy(v, &lx);
     152          10 :       for (i = 1; i < lx; i++) gel(M,i) = RgC_ApplyAllQ(Q, gel(v,i), l);
     153          10 :       return M;
     154             :     }
     155           0 :     case t_COL: if (lg(v) == l) break;
     156             :       /* fall through */
     157           5 :     default: pari_err_TYPE("mathouseholder", v);
     158             :   }
     159           0 :   return RgC_ApplyAllQ(Q, v, l);
     160             : }
     161             : 
     162             : GEN
     163          20 : matqr(GEN x, long flag, long prec)
     164             : {
     165          20 :   pari_sp av = avma;
     166             :   GEN B, Q, L;
     167          20 :   long n = lg(x)-1;
     168          20 :   if (typ(x) != t_MAT) pari_err_TYPE("matqr",x);
     169          20 :   if (!n)
     170             :   {
     171          10 :     if (!flag) retmkvec2(cgetg(1,t_MAT),cgetg(1,t_MAT));
     172           5 :     retmkvec2(cgetg(1,t_VEC),cgetg(1,t_MAT));
     173             :   }
     174          10 :   if (n != nbrows(x)) pari_err_DIM("matqr");
     175          10 :   if (!RgM_QR_init(x, &B,&Q,&L, prec)) pari_err_PREC("matqr");
     176          10 :   if (!flag) Q = shallowtrans(mathouseholder(Q, matid(n)));
     177          10 :   return gerepilecopy(av, mkvec2(Q, shallowtrans(L)));
     178             : }
     179             : 
     180             : /* compute B = | x[k] |^2, Q = Householder transforms and L = mu_{i,j} */
     181             : int
     182      235155 : QR_init(GEN x, GEN *pB, GEN *pQ, GEN *pL, long prec)
     183             : {
     184      235155 :   long j, k = lg(x)-1;
     185      235155 :   GEN B = cgetg(k+1, t_VEC), Q = cgetg(k, t_VEC), L = zeromatcopy(k,k);
     186      950565 :   for (j=1; j<=k; j++)
     187             :   {
     188      715420 :     GEN r = gel(x,j);
     189      715420 :     if (j > 1) r = ApplyAllQ(Q, r, j);
     190      715420 :     if (!FindApplyQ(r, L, B, j, Q, prec)) return 0;
     191             :   }
     192      235145 :   *pB = B; *pQ = Q; *pL = L; return 1;
     193             : }
     194             : /* x a square t_MAT with t_INT / t_REAL entries and maximal rank. Return
     195             :  * qfgaussred(x~*x) */
     196             : GEN
     197      234885 : gaussred_from_QR(GEN x, long prec)
     198             : {
     199      234885 :   long j, k = lg(x)-1;
     200             :   GEN B, Q, L;
     201      234885 :   if (!QR_init(x, &B,&Q,&L, prec)) return NULL;
     202      713220 :   for (j=1; j<k; j++)
     203             :   {
     204      478335 :     GEN m = gel(L,j), invNx = invr(gel(m,j));
     205             :     long i;
     206      478335 :     gel(m,j) = gel(B,j);
     207      478335 :     for (i=j+1; i<=k; i++) gel(m,i) = mpmul(invNx, gel(m,i));
     208             :   }
     209      234885 :   gcoeff(L,j,j) = gel(B,j);
     210      234885 :   return shallowtrans(L);
     211             : }
     212             : GEN
     213         260 : R_from_QR(GEN x, long prec)
     214             : {
     215             :   GEN B, Q, L;
     216         260 :   if (!QR_init(x, &B,&Q,&L, prec)) return NULL;
     217         250 :   return shallowtrans(L);
     218             : }
     219             : 
     220             : /********************************************************************/
     221             : /**             QR Factorization via Gram-Schmidt                  **/
     222             : /********************************************************************/
     223             : 
     224             : /* return Gram-Schmidt orthogonal basis (f) attached to (e), B is the
     225             :  * vector of the (f_i . f_i)*/
     226             : GEN
     227         310 : RgM_gram_schmidt(GEN e, GEN *ptB)
     228             : {
     229         310 :   long i,j,lx = lg(e);
     230         310 :   GEN f = RgM_shallowcopy(e), B, iB;
     231             : 
     232         310 :   B = cgetg(lx, t_VEC);
     233         310 :   iB= cgetg(lx, t_VEC);
     234             : 
     235        1025 :   for (i=1;i<lx;i++)
     236             :   {
     237         715 :     GEN p1 = NULL;
     238         715 :     pari_sp av = avma;
     239        1575 :     for (j=1; j<i; j++)
     240             :     {
     241         860 :       GEN mu = gmul(RgV_dotproduct(gel(e,i),gel(f,j)), gel(iB,j));
     242         860 :       GEN p2 = gmul(mu, gel(f,j));
     243         860 :       p1 = p1? gadd(p1,p2): p2;
     244             :     }
     245         715 :     p1 = p1? gerepileupto(av, gsub(gel(e,i), p1)): gel(e,i);
     246         715 :     gel(f,i) = p1;
     247         715 :     gel(B,i) = RgV_dotsquare(gel(f,i));
     248         715 :     gel(iB,i) = ginv(gel(B,i));
     249             :   }
     250         310 :   *ptB = B; return f;
     251             : }
     252             : 
     253             : /* Assume B an LLL-reduced basis, t a vector. Apply Babai's nearest plane
     254             :  * algorithm to (B,t) */
     255             : GEN
     256         310 : RgM_Babai(GEN B, GEN t)
     257             : {
     258         310 :   GEN C, N, G = RgM_gram_schmidt(B, &N), b = t;
     259         310 :   long j, n = lg(B)-1;
     260             : 
     261         310 :   C = cgetg(n+1,t_COL);
     262        1025 :   for (j = n; j > 0; j--)
     263             :   {
     264         715 :     GEN c = gdiv( RgV_dotproduct(b, gel(G,j)), gel(N,j) );
     265             :     long e;
     266         715 :     c = grndtoi(c,&e);
     267         715 :     if (e >= 0) return NULL;
     268         715 :     if (signe(c)) b = RgC_sub(b, RgC_Rg_mul(gel(G,j), c));
     269         715 :     gel(C,j) = c;
     270             :   }
     271         310 :   return C;
     272             : }
     273             : 
     274             : /********************************************************************/
     275             : /**                                                                **/
     276             : /**                          LLL ALGORITHM                         **/
     277             : /**                                                                **/
     278             : /********************************************************************/
     279             : /* Def: a matrix M is said to be -partially reduced- if | m1 +- m2 | >= |m1|
     280             :  * for any two columns m1 != m2, in M.
     281             :  *
     282             :  * Input: an integer matrix mat whose columns are linearly independent. Find
     283             :  * another matrix T such that mat * T is partially reduced.
     284             :  *
     285             :  * Output: mat * T if flag = 0;  T if flag != 0,
     286             :  *
     287             :  * This routine is designed to quickly reduce lattices in which one row
     288             :  * is huge compared to the other rows.  For example, when searching for a
     289             :  * polynomial of degree 3 with root a mod N, the four input vectors might
     290             :  * be the coefficients of
     291             :  *     X^3 - (a^3 mod N), X^2 - (a^2 mod N), X - (a mod N), N.
     292             :  * All four constant coefficients are O(p) and the rest are O(1). By the
     293             :  * pigeon-hole principle, the coefficients of the smallest vector in the
     294             :  * lattice are O(p^(1/4)), hence significant reduction of vector lengths
     295             :  * can be anticipated.
     296             :  *
     297             :  * An improved algorithm would look only at the leading digits of dot*.  It
     298             :  * would use single-precision calculations as much as possible.
     299             :  *
     300             :  * Original code: Peter Montgomery (1994) */
     301             : static GEN
     302          25 : lllintpartialall(GEN m, long flag)
     303             : {
     304          25 :   const long ncol = lg(m)-1;
     305          25 :   const pari_sp av = avma;
     306             :   GEN tm1, tm2, mid;
     307             : 
     308          25 :   if (ncol <= 1) return flag? matid(ncol): gcopy(m);
     309             : 
     310          10 :   tm1 = flag? matid(ncol): NULL;
     311             :   {
     312          10 :     const pari_sp av2 = avma;
     313          10 :     GEN dot11 = ZV_dotsquare(gel(m,1));
     314          10 :     GEN dot22 = ZV_dotsquare(gel(m,2));
     315          10 :     GEN dot12 = ZV_dotproduct(gel(m,1), gel(m,2));
     316          10 :     GEN tm  = matid(2); /* For first two columns only */
     317             : 
     318          10 :     int progress = 0;
     319          10 :     long npass2 = 0;
     320             : 
     321             : /* Row reduce the first two columns of m. Our best result so far is
     322             :  * (first two columns of m)*tm.
     323             :  *
     324             :  * Initially tm = 2 x 2 identity matrix.
     325             :  * Inner products of the reduced matrix are in dot11, dot12, dot22. */
     326          45 :     while (npass2 < 2 || progress)
     327             :     {
     328          30 :       GEN dot12new, q = diviiround(dot12, dot22);
     329             : 
     330          25 :       npass2++; progress = signe(q);
     331          25 :       if (progress)
     332             :       {/* Conceptually replace (v1, v2) by (v1 - q*v2, v2), where v1 and v2
     333             :         * represent the reduced basis for the first two columns of the matrix.
     334             :         * We do this by updating tm and the inner products. */
     335          15 :         togglesign(q);
     336          15 :         dot12new = addii(dot12, mulii(q, dot22));
     337          15 :         dot11 = addii(dot11, mulii(q, addii(dot12, dot12new)));
     338          15 :         dot12 = dot12new;
     339          15 :         ZC_lincomb1_inplace(gel(tm,1), gel(tm,2), q);
     340             :       }
     341             : 
     342             :       /* Interchange the output vectors v1 and v2.  */
     343          25 :       swap(dot11,dot22);
     344          25 :       swap(gel(tm,1), gel(tm,2));
     345             : 
     346             :       /* Occasionally (including final pass) do garbage collection.  */
     347          25 :       if ((npass2 & 0xff) == 0 || !progress)
     348          10 :         gerepileall(av2, 4, &dot11,&dot12,&dot22,&tm);
     349             :     } /* while npass2 < 2 || progress */
     350             : 
     351             :     {
     352             :       long i;
     353           5 :       GEN det12 = subii(mulii(dot11, dot22), sqri(dot12));
     354             : 
     355           5 :       mid = cgetg(ncol+1, t_MAT);
     356          15 :       for (i = 1; i <= 2; i++)
     357             :       {
     358          10 :         GEN tmi = gel(tm,i);
     359          10 :         if (tm1)
     360             :         {
     361          10 :           GEN tm1i = gel(tm1,i);
     362          10 :           gel(tm1i,1) = gel(tmi,1);
     363          10 :           gel(tm1i,2) = gel(tmi,2);
     364             :         }
     365          10 :         gel(mid,i) = ZC_lincomb(gel(tmi,1),gel(tmi,2), gel(m,1),gel(m,2));
     366             :       }
     367          30 :       for (i = 3; i <= ncol; i++)
     368             :       {
     369          25 :         GEN c = gel(m,i);
     370          25 :         GEN dot1i = ZV_dotproduct(gel(mid,1), c);
     371          25 :         GEN dot2i = ZV_dotproduct(gel(mid,2), c);
     372             :        /* ( dot11  dot12 ) (q1)   ( dot1i )
     373             :         * ( dot12  dot22 ) (q2) = ( dot2i )
     374             :         *
     375             :         * Round -q1 and -q2 to nearest integer. Then compute
     376             :         *   c - q1*mid[1] - q2*mid[2].
     377             :         * This will be approximately orthogonal to the first two vectors in
     378             :         * the new basis. */
     379          25 :         GEN q1neg = subii(mulii(dot12, dot2i), mulii(dot22, dot1i));
     380          25 :         GEN q2neg = subii(mulii(dot12, dot1i), mulii(dot11, dot2i));
     381             : 
     382          25 :         q1neg = diviiround(q1neg, det12);
     383          25 :         q2neg = diviiround(q2neg, det12);
     384          25 :         if (tm1)
     385             :         {
     386          50 :           gcoeff(tm1,1,i) = addii(mulii(q1neg, gcoeff(tm,1,1)),
     387          25 :                                   mulii(q2neg, gcoeff(tm,1,2)));
     388          50 :           gcoeff(tm1,2,i) = addii(mulii(q1neg, gcoeff(tm,2,1)),
     389          25 :                                   mulii(q2neg, gcoeff(tm,2,2)));
     390             :         }
     391          25 :         gel(mid,i) = ZC_add(c, ZC_lincomb(q1neg,q2neg, gel(mid,1),gel(mid,2)));
     392             :       } /* for i */
     393             :     } /* local block */
     394             :   }
     395           5 :   if (DEBUGLEVEL>6)
     396             :   {
     397           0 :     if (tm1) err_printf("tm1 = %Ps",tm1);
     398           0 :     err_printf("mid = %Ps",mid); /* = m * tm1 */
     399             :   }
     400           5 :   gerepileall(av, tm1? 2: 1, &mid, &tm1);
     401             :   {
     402             :    /* For each pair of column vectors v and w in mid * tm2,
     403             :     * try to replace (v, w) by (v, v - q*w) for some q.
     404             :     * We compute all inner products and check them repeatedly. */
     405           5 :     const pari_sp av3 = avma;
     406           5 :     long i, j, npass = 0, e = LONG_MAX;
     407           5 :     GEN dot = cgetg(ncol+1, t_MAT); /* scalar products */
     408             : 
     409           5 :     tm2 = matid(ncol);
     410          40 :     for (i=1; i <= ncol; i++)
     411             :     {
     412          35 :       gel(dot,i) = cgetg(ncol+1,t_COL);
     413         175 :       for (j=1; j <= i; j++)
     414         140 :         gcoeff(dot,j,i) = gcoeff(dot,i,j) = ZV_dotproduct(gel(mid,i),gel(mid,j));
     415             :     }
     416             :     for(;;)
     417             :     {
     418          30 :       long reductions = 0, olde = e;
     419         240 :       for (i=1; i <= ncol; i++)
     420             :       {
     421             :         long ijdif;
     422        1470 :         for (ijdif=1; ijdif < ncol; ijdif++)
     423             :         {
     424             :           long d, k1, k2;
     425             :           GEN codi, q;
     426             : 
     427        1260 :           j = i + ijdif; if (j > ncol) j -= ncol;
     428             :           /* let k1, resp. k2,  index of larger, resp. smaller, column */
     429        1260 :           if (cmpii(gcoeff(dot,i,i), gcoeff(dot,j,j)) > 0) { k1 = i; k2 = j; }
     430         730 :           else                                             { k1 = j; k2 = i; }
     431        1260 :           codi = gcoeff(dot,k2,k2);
     432        1260 :           q = signe(codi)? diviiround(gcoeff(dot,k1,k2), codi): gen_0;
     433        1260 :           if (!signe(q)) continue;
     434             : 
     435             :           /* Try to subtract a multiple of column k2 from column k1.  */
     436         500 :           reductions++; togglesign_safe(&q);
     437         500 :           ZC_lincomb1_inplace(gel(tm2,k1), gel(tm2,k2), q);
     438         500 :           ZC_lincomb1_inplace(gel(dot,k1), gel(dot,k2), q);
     439        1000 :           gcoeff(dot,k1,k1) = addii(gcoeff(dot,k1,k1),
     440         500 :                                     mulii(q, gcoeff(dot,k2,k1)));
     441         500 :           for (d = 1; d <= ncol; d++) gcoeff(dot,k1,d) = gcoeff(dot,d,k1);
     442             :         } /* for ijdif */
     443         210 :         if (gc_needed(av3,2))
     444             :         {
     445           0 :           if(DEBUGMEM>1) pari_warn(warnmem,"lllintpartialall");
     446           0 :           gerepileall(av3, 2, &dot,&tm2);
     447             :         }
     448             :       } /* for i */
     449          30 :       if (!reductions) break;
     450          25 :       e = 0;
     451          25 :       for (i = 1; i <= ncol; i++) e += expi( gcoeff(dot,i,i) );
     452          25 :       if (e == olde) break;
     453          25 :       if (DEBUGLEVEL>6)
     454             :       {
     455           0 :         npass++;
     456           0 :         err_printf("npass = %ld, red. last time = %ld, log_2(det) ~ %ld\n\n",
     457             :                     npass, reductions, e);
     458             :       }
     459          25 :     } /* for(;;)*/
     460             : 
     461             :    /* Sort columns so smallest comes first in m * tm1 * tm2.
     462             :     * Use insertion sort. */
     463          35 :     for (i = 1; i < ncol; i++)
     464             :     {
     465          30 :       long j, s = i;
     466             : 
     467         135 :       for (j = i+1; j <= ncol; j++)
     468         105 :         if (cmpii(gcoeff(dot,s,s),gcoeff(dot,j,j)) > 0) s = j;
     469          30 :       if (i != s)
     470             :       { /* Exchange with proper column; only the diagonal of dot is updated */
     471          20 :         swap(gel(tm2,i), gel(tm2,s));
     472          20 :         swap(gcoeff(dot,i,i), gcoeff(dot,s,s));
     473             :       }
     474             :     }
     475             :   } /* local block */
     476           5 :   return gerepileupto(av, ZM_mul(tm1? tm1: mid, tm2));
     477             : }
     478             : 
     479             : GEN
     480          25 : lllintpartial(GEN mat) { return lllintpartialall(mat,1); }
     481             : 
     482             : GEN
     483           0 : lllintpartial_inplace(GEN mat) { return lllintpartialall(mat,0); }
     484             : 
     485             : /********************************************************************/
     486             : /**                                                                **/
     487             : /**                    COPPERSMITH ALGORITHM                       **/
     488             : /**           Finding small roots of univariate equations.         **/
     489             : /**                                                                **/
     490             : /********************************************************************/
     491             : 
     492             : static int
     493         630 : check_condition(double beta, double tau, double rho, long d, long delta, long t)
     494             : {
     495         630 :   long dim = d*delta + t;
     496        1260 :   double cond = d*delta*(delta+1)/2 - beta*delta*dim
     497         630 :     + rho*delta*(delta - 1) / 2
     498         630 :     + rho * t * delta + tau*dim*(dim - 1)/2;
     499             : 
     500         630 :   if (DEBUGLEVEL >= 4)
     501           0 :     err_printf("delta = %d, t = %d, cond = %.1lf\n", delta, t, cond);
     502             : 
     503         630 :   return (cond <= 0);
     504             : }
     505             : 
     506             : static void
     507          10 : choose_params(GEN P, GEN N, GEN X, GEN B, long *pdelta, long *pt)
     508             : {
     509          10 :   long d = degpol(P);
     510          10 :   GEN P0 = leading_coeff(P);
     511          10 :   double logN = gtodouble(glog(N, DEFAULTPREC));
     512             :   double tau, beta, rho;
     513             :   long delta, t;
     514          10 :   tau = gtodouble(glog(X, DEFAULTPREC)) / logN;
     515          10 :   beta = B? gtodouble(glog(B, DEFAULTPREC)) / logN: 1.;
     516          10 :   if (tau >= beta * beta / d)
     517           0 :     pari_err_OVERFLOW("zncoppersmith [bound too large]");
     518             :   /* TODO : remove P0 completely ! */
     519          10 :   rho = gtodouble(glog(P0, DEFAULTPREC)) / logN;
     520             : 
     521             :   /* Enumerate (delta,t) by increasing dimension of resulting lattice.
     522             :    * Not subtle, but o(1) for computing time */
     523          10 :   t = d; delta = 0;
     524             :   for(;;)
     525             :   {
     526         125 :     t += d * delta + 1; delta = 0;
     527         870 :     while (t >= 0) {
     528         630 :       if (check_condition(beta, tau, rho, d, delta, t)) {
     529          20 :         *pdelta = delta; *pt = t; return;
     530             :       }
     531         620 :       delta++; t -= d;
     532             :     }
     533         115 :   }
     534             : }
     535             : 
     536             : static int
     537       10015 : sol_OK(GEN x, GEN N, GEN B)
     538       10015 : { return B? (cmpii(gcdii(x,N),B) >= 0): !signe(remii(x,N)); }
     539             : /* deg(P) > 0, x >= 0. Find all j such that gcd(P(j), N) >= B, |j| <= x */
     540             : static GEN
     541           5 : do_exhaustive(GEN P, GEN N, long x, GEN B)
     542             : {
     543           5 :   GEN Pe, Po, sol = vecsmalltrunc_init(2*x + 2);
     544             :   pari_sp av;
     545             :   long j;
     546           5 :   RgX_even_odd(P, &Pe,&Po); av = avma;
     547           5 :   if (sol_OK(gel(P,2), N,B)) vecsmalltrunc_append(sol, 0);
     548        5005 :   for (j = 1; j <= x; j++, avma = av)
     549             :   {
     550        5000 :     GEN j2 = sqru(j), E = FpX_eval(Pe,j2,N), O = FpX_eval(Po,j2,N);
     551        5000 :     if (sol_OK(addmuliu(E,O,j), N,B)) vecsmalltrunc_append(sol, j);
     552        5000 :     if (sol_OK(submuliu(E,O,j), N,B)) vecsmalltrunc_append(sol,-j);
     553             :   }
     554           5 :   vecsmall_sort(sol); return zv_to_ZV(sol);
     555             : }
     556             : 
     557             : /* General Coppersmith, look for a root x0 <= p, p >= B, p | N, |x0| <= X.
     558             :  * B = N coded as NULL */
     559             : GEN
     560          15 : zncoppersmith(GEN P0, GEN N, GEN X, GEN B)
     561             : {
     562             :   GEN Q, R, N0, M, sh, short_pol, *Xpowers, sol, nsp, P, Z;
     563          15 :   long delta, i, j, row, d, l, dim, t, bnd = 10;
     564          15 :   const ulong X_SMALL = 1000;
     565             : 
     566          15 :   pari_sp av = avma;
     567             : 
     568          15 :   if (typ(P0) != t_POL) pari_err_TYPE("zncoppersmith",P0);
     569          15 :   if (typ(N) != t_INT) pari_err_TYPE("zncoppersmith",N);
     570          15 :   if (typ(X) != t_INT) {
     571           5 :     X = gfloor(X);
     572           5 :     if (typ(X) != t_INT) pari_err_TYPE("zncoppersmith",X);
     573             :   }
     574          15 :   if (signe(X) < 0) pari_err_DOMAIN("zncoppersmith", "X", "<", gen_0, X);
     575          15 :   d = degpol(P0);
     576          15 :   if (d == 0) { avma = av; return cgetg(1, t_VEC); }
     577          15 :   if (d < 0) pari_err_ROOTS0("zncoppersmith");
     578          15 :   if (B && typ(B) != t_INT) B = gceil(B);
     579             : 
     580          15 :   if (abscmpiu(X, X_SMALL) <= 0)
     581           5 :     return gerepileupto(av, do_exhaustive(P0, N, itos(X), B));
     582             : 
     583          10 :   if (B && equalii(B,N)) B = NULL;
     584          10 :   if (B) bnd = 1; /* bnd-hack is only for the case B = N */
     585          10 :   P = leafcopy(P0);
     586          10 :   if (!gequal1(gel(P,d+2)))
     587             :   {
     588             :     GEN r, z;
     589           5 :     gel(P,d+2) = bezout(gel(P,d+2), N, &z, &r);
     590           5 :     for (j = 0; j < d; j++) gel(P,j+2) = modii(mulii(gel(P,j+2), z), N);
     591             :   }
     592          10 :   if (DEBUGLEVEL >= 2) err_printf("Modified P: %Ps\n", P);
     593             : 
     594          10 :   choose_params(P, N, X, B, &delta, &t);
     595          10 :   if (DEBUGLEVEL >= 2)
     596           0 :     err_printf("Init: trying delta = %d, t = %d\n", delta, t);
     597             :   for(;;)
     598             :   {
     599          10 :     dim = d * delta + t;
     600             : 
     601             :     /* TODO: In case of failure do not recompute the full vector */
     602          10 :     Xpowers = (GEN*)new_chunk(dim + 1);
     603          10 :     Xpowers[0] = gen_1;
     604          10 :     for (j = 1; j <= dim; j++) Xpowers[j] = mulii(Xpowers[j-1], X);
     605             : 
     606             :     /* TODO: in case of failure, use the part of the matrix already computed */
     607          10 :     M = zeromatcopy(dim,dim);
     608             : 
     609             :     /* Rows of M correspond to the polynomials
     610             :      * N^delta, N^delta Xi, ... N^delta (Xi)^d-1,
     611             :      * N^(delta-1)P(Xi), N^(delta-1)XiP(Xi), ... N^(delta-1)P(Xi)(Xi)^d-1,
     612             :      * ...
     613             :      * P(Xi)^delta, XiP(Xi)^delta, ..., P(Xi)^delta(Xi)^t-1 */
     614          10 :     for (j = 1; j <= d;   j++) gcoeff(M, j, j) = gel(Xpowers,j-1);
     615             : 
     616             :     /* P-part */
     617          10 :     if (delta) row = d + 1; else row = 0;
     618             : 
     619          10 :     Q = P;
     620          50 :     for (i = 1; i < delta; i++)
     621             :     {
     622         130 :       for (j = 0; j < d; j++,row++)
     623         885 :         for (l = j + 1; l <= row; l++)
     624         795 :           gcoeff(M, l, row) = mulii(Xpowers[l-1], gel(Q,l-j+1));
     625          40 :       Q = ZX_mul(Q, P);
     626             :     }
     627          45 :     for (j = 0; j < t; row++, j++)
     628         350 :       for (l = j + 1; l <= row; l++)
     629         315 :         gcoeff(M, l, row) = mulii(Xpowers[l-1], gel(Q,l-j+1));
     630             : 
     631             :     /* N-part */
     632          10 :     row = dim - t; N0 = N;
     633          70 :     while (row >= 1)
     634             :     {
     635         160 :       for (j = 0; j < d; j++,row--)
     636        1015 :         for (l = 1; l <= row; l++)
     637         905 :           gcoeff(M, l, row) = mulii(gmael(M, row, l), N0);
     638          50 :       if (row >= 1) N0 = mulii(N0, N);
     639             :     }
     640             :     /* Z is the upper bound for the L^1 norm of the polynomial,
     641             :        ie. N^delta if B = N, B^delta otherwise */
     642          10 :     if (B) Z = powiu(B, delta); else Z = N0;
     643             : 
     644          10 :     if (DEBUGLEVEL >= 2)
     645             :     {
     646           0 :       if (DEBUGLEVEL >= 6) err_printf("Matrix to be reduced:\n%Ps\n", M);
     647           0 :       err_printf("Entering LLL\nbitsize bound: %ld\n", expi(Z));
     648           0 :       err_printf("expected shvector bitsize: %ld\n", expi(ZM_det_triangular(M))/dim);
     649             :     }
     650             : 
     651          10 :     sh = ZM_lll(M, 0.75, LLL_INPLACE);
     652             :     /* Take the first vector if it is non constant */
     653          10 :     short_pol = gel(sh,1);
     654          10 :     if (ZV_isscalar(short_pol)) short_pol = gel(sh, 2);
     655             : 
     656          10 :     nsp = gen_0;
     657          10 :     for (j = 1; j <= dim; j++) nsp = addii(nsp, absi(gel(short_pol,j)));
     658             : 
     659          10 :     if (DEBUGLEVEL >= 2)
     660             :     {
     661           0 :       err_printf("Candidate: %Ps\n", short_pol);
     662           0 :       err_printf("bitsize Norm: %ld\n", expi(nsp));
     663           0 :       err_printf("bitsize bound: %ld\n", expi(mului(bnd, Z)));
     664             :     }
     665          10 :     if (cmpii(nsp, mului(bnd, Z)) < 0) break; /* SUCCESS */
     666             : 
     667             :     /* Failed with the precomputed or supplied value */
     668           0 :     t++; if (t == d) { delta++; t = 1; }
     669           0 :     if (DEBUGLEVEL >= 2)
     670           0 :       err_printf("Increasing dim, delta = %d t = %d\n", delta, t);
     671           0 :   }
     672          10 :   bnd = itos(divii(nsp, Z)) + 1;
     673             : 
     674          10 :   while (!signe(gel(short_pol,dim))) dim--;
     675             : 
     676          10 :   R = cgetg(dim + 2, t_POL); R[1] = P[1];
     677         155 :   for (j = 1; j <= dim; j++)
     678         145 :     gel(R,j+1) = diviiexact(gel(short_pol,j), Xpowers[j-1]);
     679          10 :   gel(R,2) = subii(gel(R,2), mului(bnd - 1, N0));
     680             : 
     681          10 :   sol = cgetg(1, t_VEC);
     682          80 :   for (i = -bnd + 1; i < bnd; i++)
     683             :   {
     684          70 :     GEN r = nfrootsQ(R);
     685          70 :     if (DEBUGLEVEL >= 2) err_printf("Roots: %Ps\n", r);
     686          85 :     for (j = 1; j < lg(r); j++)
     687             :     {
     688          15 :       GEN z = gel(r,j);
     689          15 :       if (typ(z) == t_INT && sol_OK(FpX_eval(P,z,N), N,B))
     690          10 :         sol = shallowconcat(sol, z);
     691             :     }
     692          70 :     if (i < bnd) gel(R,2) = addii(gel(R,2), Z);
     693             :   }
     694          10 :   return gerepileupto(av, ZV_sort_uniq(sol));
     695             : }
     696             : 
     697             : /********************************************************************/
     698             : /**                                                                **/
     699             : /**                   LINEAR & ALGEBRAIC DEPENDENCE                **/
     700             : /**                                                                **/
     701             : /********************************************************************/
     702             : 
     703             : static int
     704         525 : real_indep(GEN re, GEN im, long bit)
     705             : {
     706         525 :   GEN d = gsub(gmul(gel(re,1),gel(im,2)), gmul(gel(re,2),gel(im,1)));
     707         525 :   return (!gequal0(d) && gexpo(d) > - bit);
     708             : }
     709             : 
     710             : GEN
     711        2085 : lindep_bit(GEN x, long bit)
     712             : {
     713        2085 :   long lx = lg(x), ly, i, j;
     714        2085 :   pari_sp av = avma;
     715             :   GEN re, im, M;
     716             : 
     717        2085 :   if (! is_vec_t(typ(x))) pari_err_TYPE("lindep2",x);
     718        2085 :   if (lx <= 2)
     719             :   {
     720          15 :     if (lx == 2 && gequal0(x)) return mkcol(gen_1);
     721          10 :     return cgetg(1,t_COL);
     722             :   }
     723        2070 :   re = real_i(x);
     724        2070 :   im = imag_i(x);
     725             :   /* independent over R ? */
     726        2070 :   if (lx == 3 && real_indep(re,im,bit)) { avma = av; return cgetg(1, t_COL); }
     727        2065 :   if (gequal0(im)) im = NULL;
     728        2065 :   ly = im? lx+2: lx+1;
     729        2065 :   M = cgetg(lx,t_MAT);
     730        8295 :   for (i=1; i<lx; i++)
     731             :   {
     732        6230 :     GEN c = cgetg(ly,t_COL); gel(M,i) = c;
     733        6230 :     for (j=1; j<lx; j++) gel(c,j) = (i==j)? gen_1: gen_0;
     734        6230 :     gel(c,lx)           = gtrunc2n(gel(re,i), bit);
     735        6230 :     if (im) gel(c,lx+1) = gtrunc2n(gel(im,i), bit);
     736             :   }
     737        2065 :   M = ZM_lll(M, 0.99, LLL_INPLACE);
     738        2065 :   M = gel(M,1);
     739        2065 :   M[0] = evaltyp(t_COL) | evallg(lx);
     740        2065 :   return gerepilecopy(av, M);
     741             : }
     742             : /* deprecated */
     743             : GEN
     744          75 : lindep2(GEN x, long dig)
     745             : {
     746             :   long bit;
     747          75 :   if (dig < 0) pari_err_DOMAIN("lindep2", "accuracy", "<", gen_0, stoi(dig));
     748          75 :   if (dig) bit = (long) (dig/LOG10_2);
     749             :   else
     750             :   {
     751          65 :     bit = gprecision(x);
     752          65 :     if (!bit)
     753             :     {
     754          25 :       x = Q_primpart(x); /* left on stack */
     755          25 :       bit = 32 + gexpo(x);
     756             :     }
     757             :     else
     758          40 :       bit = (long)prec2nbits_mul(bit, 0.8);
     759             :   }
     760          75 :   return lindep_bit(x, bit);
     761             : }
     762             : 
     763             : /* x is a vector of elts of a p-adic field */
     764             : GEN
     765          10 : lindep_padic(GEN x)
     766             : {
     767          10 :   long i, j, prec = LONG_MAX, nx = lg(x)-1, v;
     768          10 :   pari_sp av = avma;
     769          10 :   GEN p = NULL, pn, m, a;
     770             : 
     771          10 :   if (nx < 2) return cgetg(1,t_COL);
     772          35 :   for (i=1; i<=nx; i++)
     773             :   {
     774          25 :     GEN c = gel(x,i), q;
     775          25 :     if (typ(c) != t_PADIC) continue;
     776             : 
     777          15 :     j = precp(c); if (j < prec) prec = j;
     778          15 :     q = gel(c,2);
     779          15 :     if (!p) p = q; else if (!equalii(p, q)) pari_err_MODULUS("lindep_padic", p, q);
     780             :   }
     781          10 :   if (!p) pari_err_TYPE("lindep_padic [not a p-adic vector]",x);
     782          10 :   v = gvaluation(x,p); pn = powiu(p,prec);
     783          10 :   if (v) x = gmul(x, powis(p, -v));
     784          10 :   x = RgV_to_FpV(x, pn);
     785             : 
     786          10 :   a = negi(gel(x,1));
     787          10 :   m = cgetg(nx,t_MAT);
     788          25 :   for (i=1; i<nx; i++)
     789             :   {
     790          15 :     GEN c = zerocol(nx);
     791          15 :     gel(c,1+i) = a;
     792          15 :     gel(c,1) = gel(x,i+1);
     793          15 :     gel(m,i) = c;
     794             :   }
     795          10 :   m = ZM_lll(ZM_hnfmodid(m, pn), 0.99, LLL_INPLACE);
     796          10 :   return gerepilecopy(av, gel(m,1));
     797             : }
     798             : /* x is a vector of t_POL/t_SER */
     799             : GEN
     800          30 : lindep_Xadic(GEN x)
     801             : {
     802          30 :   long i, prec = LONG_MAX, deg = 0, lx = lg(x), vx, v;
     803          30 :   pari_sp av = avma;
     804             :   GEN m;
     805             : 
     806          30 :   if (lx == 1) return cgetg(1,t_COL);
     807          30 :   vx = gvar(x);
     808          30 :   v = gvaluation(x, pol_x(vx));
     809          30 :   if (!v)         x = shallowcopy(x);
     810           0 :   else if (v > 0) x = gdiv(x, pol_xn(v, vx));
     811           0 :   else            x = gmul(x, pol_xn(-v, vx));
     812             :   /* all t_SER have valuation >= 0 */
     813         220 :   for (i=1; i<lx; i++)
     814             :   {
     815         190 :     GEN c = gel(x,i);
     816         190 :     if (gvar(c) != vx) { gel(x,i) = scalarpol_shallow(c, vx); continue; }
     817         185 :     switch(typ(c))
     818             :     {
     819          90 :       case t_POL: deg = maxss(deg, degpol(c)); break;
     820           0 :       case t_RFRAC: pari_err_TYPE("lindep_Xadic", c);
     821             :       case t_SER:
     822          95 :         prec = minss(prec, valp(c)+lg(c)-2);
     823          95 :         gel(x,i) = ser2rfrac_i(c);
     824             :     }
     825             :   }
     826          30 :   if (prec == LONG_MAX) prec = deg+1;
     827          30 :   m = RgXV_to_RgM(x, prec);
     828          30 :   return gerepileupto(av, deplin(m));
     829             : }
     830             : static GEN
     831          25 : vec_lindep(GEN x)
     832             : {
     833          25 :   pari_sp av = avma;
     834          25 :   long i, l = lg(x); /* > 1 */
     835          25 :   long t = typ(gel(x,1)), h = lg(gel(x,1));
     836          25 :   GEN m = cgetg(l, t_MAT);
     837          90 :   for (i = 1; i < l; i++)
     838             :   {
     839          70 :     GEN y = gel(x,i);
     840          70 :     if (lg(y) != h || typ(y) != t) pari_err_TYPE("lindep",x);
     841          65 :     if (t != t_COL) y = shallowtrans(y); /* Sigh */
     842          65 :     gel(m,i) = y;
     843             :   }
     844          20 :   return gerepileupto(av, deplin(m));
     845             : }
     846             : 
     847             : GEN
     848           0 : lindep(GEN x) { return lindep2(x, 0); }
     849             : 
     850             : GEN
     851         240 : lindep0(GEN x,long bit)
     852             : {
     853         240 :   long i, tx = typ(x);
     854         240 :   if (tx == t_MAT) return deplin(x);
     855          95 :   if (! is_vec_t(tx)) pari_err_TYPE("lindep",x);
     856         285 :   for (i = 1; i < lg(x); i++)
     857         230 :     switch(typ(gel(x,i)))
     858             :     {
     859           5 :       case t_PADIC: return lindep_padic(x);
     860             :       case t_POL:
     861             :       case t_RFRAC:
     862          10 :       case t_SER: return lindep_Xadic(x);
     863             :       case t_VEC:
     864          25 :       case t_COL: return vec_lindep(x);
     865             :     }
     866          55 :   return lindep2(x, bit);
     867             : }
     868             : 
     869             : GEN
     870          35 : algdep0(GEN x, long n, long bit)
     871             : {
     872          35 :   long tx = typ(x), i;
     873             :   pari_sp av;
     874             :   GEN y;
     875             : 
     876          35 :   if (! is_scalar_t(tx)) pari_err_TYPE("algdep0",x);
     877          35 :   if (tx==t_POLMOD) { y = RgX_copy(gel(x,1)); setvarn(y,0); return y; }
     878          35 :   if (gequal0(x)) return pol_x(0);
     879          35 :   if (n <= 0)
     880             :   {
     881          10 :     if (!n) return gen_1;
     882           5 :     pari_err_DOMAIN("algdep", "degree", "<", gen_0, stoi(n));
     883             :   }
     884             : 
     885          25 :   av = avma; y = cgetg(n+2,t_COL);
     886          25 :   gel(y,1) = gen_1;
     887          25 :   gel(y,2) = x; /* n >= 1 */
     888          25 :   for (i=3; i<=n+1; i++) gel(y,i) = gmul(gel(y,i-1),x);
     889          25 :   if (typ(x) == t_PADIC)
     890           5 :     y = lindep_padic(y);
     891             :   else
     892          20 :     y = lindep2(y, bit);
     893          25 :   if (lg(y) == 1) pari_err(e_DOMAIN,"algdep", "degree(x)",">", stoi(n), x);
     894          25 :   y = RgV_to_RgX(y, 0);
     895          25 :   if (signe(leading_coeff(y)) > 0) return gerepilecopy(av, y);
     896           0 :   return gerepileupto(av, ZX_neg(y));
     897             : }
     898             : 
     899             : GEN
     900           0 : algdep(GEN x, long n)
     901             : {
     902           0 :   return algdep0(x,n,0);
     903             : }
     904             : 
     905             : GEN
     906          20 : seralgdep(GEN s, long p, long r)
     907             : {
     908          20 :   pari_sp av = avma;
     909             :   long vy, i, m, n, prec;
     910             :   GEN S, v, D;
     911             : 
     912          20 :   if (typ(s) != t_SER) pari_err_TYPE("seralgdep",s);
     913          20 :   if (p <= 0) pari_err_DOMAIN("seralgdep", "p", "<=", gen_0, stoi(p));
     914          20 :   if (r < 0) pari_err_DOMAIN("seralgdep", "r", "<", gen_0, stoi(r));
     915          20 :   if (is_bigint(addiu(muluu(p, r), 1))) pari_err_OVERFLOW("seralgdep");
     916          20 :   vy = varn(s);
     917          20 :   if (!vy) pari_err_PRIORITY("seralgdep", s, ">", 0);
     918          20 :   r++; p++;
     919          20 :   prec = valp(s) + lg(s)-2;
     920          20 :   if (r > prec) r = prec;
     921          20 :   S = cgetg(p+1, t_VEC); gel(S, 1) = s;
     922          20 :   for (i = 2; i <= p; i++) gel(S,i) = gmul(gel(S,i-1), s);
     923          20 :   v = cgetg(r*p+1, t_VEC); /* v[r*n+m+1] = s^n * y^m */
     924             :   /* n = 0 */
     925          20 :   for (m = 0; m < r; m++) gel(v, m+1) = pol_xn(m, vy);
     926          50 :   for(n=1; n < p; n++)
     927         125 :     for (m = 0; m < r; m++)
     928             :     {
     929          95 :       GEN c = gel(S,n);
     930          95 :       if (m)
     931             :       {
     932          65 :         c = shallowcopy(c);
     933          65 :         setvalp(c, valp(c) + m);
     934             :       }
     935          95 :       gel(v, r*n + m + 1) = c;
     936             :     }
     937          20 :   D = lindep_Xadic(v);
     938          20 :   if (lg(D) == 1) { avma = av; return gen_0; }
     939          15 :   v = cgetg(p+1, t_VEC);
     940          50 :   for (n = 0; n < p; n++)
     941          35 :     gel(v, n+1) = RgV_to_RgX(vecslice(D, r*n+1, r*n+r), vy);
     942          15 :   return gerepilecopy(av, RgV_to_RgX(v, 0));
     943             : }
     944             : 
     945             : /********************************************************************/
     946             : /**                                                                **/
     947             : /**                              MINIM                             **/
     948             : /**                                                                **/
     949             : /********************************************************************/
     950             : void
     951       13790 : minim_alloc(long n, double ***q, GEN *x, double **y,  double **z, double **v)
     952             : {
     953             :   long i, s;
     954             : 
     955       13790 :   *x = cgetg(n, t_VECSMALL);
     956       13790 :   *q = (double**) new_chunk(n);
     957       13790 :   s = n * sizeof(double);
     958       13790 :   *y = (double*) stack_malloc_align(s, sizeof(double));
     959       13790 :   *z = (double*) stack_malloc_align(s, sizeof(double));
     960       13790 :   *v = (double*) stack_malloc_align(s, sizeof(double));
     961       13790 :   for (i=1; i<n; i++) (*q)[i] = (double*) stack_malloc_align(s, sizeof(double));
     962       13790 : }
     963             : 
     964             : static GEN
     965      144735 : ZC_canon(GEN V)
     966             : {
     967      144735 :   long l = lg(V), j;
     968      144735 :   for (j = 1; j < l  &&  signe(gel(V,j)) == 0; ++j);
     969      144735 :   return (j < l  &&  signe(gel(V,j)) < 0)? ZC_neg(V): V;
     970             : }
     971             : 
     972             : static GEN
     973      144735 : ZM_zc_mul_canon(GEN u, GEN x)
     974             : {
     975      144735 :   return ZC_canon(ZM_zc_mul(u,x));
     976             : }
     977             : 
     978             : struct qfvec
     979             : {
     980             :   GEN a, r, u;
     981             : };
     982             : 
     983             : static void
     984           0 : err_minim(GEN a)
     985             : {
     986           0 :   pari_err_DOMAIN("minim0","form","is not",
     987             :                   strtoGENstr("positive definite"),a);
     988           0 : }
     989             : 
     990             : static GEN
     991         400 : minim_lll(GEN a, GEN *u)
     992             : {
     993         400 :   *u = lllgramint(a);
     994         400 :   if (lg(*u) != lg(a)) err_minim(a);
     995         400 :   return qf_apply_ZM(a,*u);
     996             : }
     997             : 
     998             : static void
     999         400 : forqfvec_init_dolll(struct qfvec *qv, GEN a, long dolll)
    1000             : {
    1001             :   GEN r, u;
    1002         400 :   if (!dolll) u = NULL;
    1003             :   else
    1004             :   {
    1005         370 :     if (typ(a) != t_MAT || !RgM_is_ZM(a)) pari_err_TYPE("qfminim",a);
    1006         370 :     a = minim_lll(a, &u);
    1007             :   }
    1008         400 :   qv->a = RgM_gtofp(a, DEFAULTPREC);
    1009         400 :   r = qfgaussred_positive(qv->a);
    1010         400 :   if (!r)
    1011             :   {
    1012           0 :     r = qfgaussred_positive(a); /* exact computation */
    1013           0 :     if (!r) err_minim(a);
    1014           0 :     r = RgM_gtofp(r, DEFAULTPREC);
    1015             :   }
    1016         400 :   qv->r = r;
    1017         400 :   qv->u = u;
    1018         400 : }
    1019             : 
    1020             : static void
    1021          15 : forqfvec_init(struct qfvec *qv, GEN a)
    1022          15 : { forqfvec_init_dolll(qv, a, 1); }
    1023             : 
    1024             : static void
    1025          15 : forqfvec_i(void *E, long (*fun)(void *, GEN, GEN, double), struct qfvec *qv, GEN BORNE)
    1026             : {
    1027          15 :   GEN x, a = qv->a, r = qv->r, u = qv->u;
    1028          15 :   long n = lg(a), i, j, k;
    1029             :   double p,BOUND,*v,*y,*z,**q;
    1030          15 :   const double eps = 0.0001;
    1031          15 :   if (!BORNE) BORNE = gen_0;
    1032             :   else
    1033             :   {
    1034          10 :     BORNE = gfloor(BORNE);
    1035          10 :     if (typ(BORNE) != t_INT) pari_err_TYPE("minim0",BORNE);
    1036             :   }
    1037          30 :   if (n == 1) return;
    1038          10 :   minim_alloc(n, &q, &x, &y, &z, &v);
    1039          10 :   n--;
    1040          30 :   for (j=1; j<=n; j++)
    1041             :   {
    1042          20 :     v[j] = rtodbl(gcoeff(r,j,j));
    1043          20 :     for (i=1; i<j; i++) q[i][j] = rtodbl(gcoeff(r,i,j));
    1044             :   }
    1045             : 
    1046          10 :   if (gequal0(BORNE))
    1047             :   {
    1048             :     double c;
    1049           5 :     p = rtodbl(gcoeff(a,1,1));
    1050           5 :     for (i=2; i<=n; i++) { c = rtodbl(gcoeff(a,i,i)); if (c < p) p = c; }
    1051           5 :     BORNE = roundr(dbltor(p));
    1052             :   }
    1053             :   else
    1054           5 :     p = gtodouble(BORNE);
    1055          10 :   BOUND = p * (1 + eps);
    1056          10 :   if (BOUND == p) pari_err_PREC("minim0");
    1057             : 
    1058          10 :   k = n; y[n] = z[n] = 0;
    1059          10 :   x[n] = (long)sqrt(BOUND/v[n]);
    1060          20 :   for(;;x[1]--)
    1061             :   {
    1062             :     do
    1063             :     {
    1064          35 :       if (k>1)
    1065             :       {
    1066          15 :         long l = k-1;
    1067          15 :         z[l] = 0;
    1068          15 :         for (j=k; j<=n; j++) z[l] += q[l][j]*x[j];
    1069          15 :         p = (double)x[k] + z[k];
    1070          15 :         y[l] = y[k] + p*p*v[k];
    1071          15 :         x[l] = (long)floor(sqrt((BOUND-y[l])/v[l])-z[l]);
    1072          15 :         k = l;
    1073             :       }
    1074             :       for(;;)
    1075             :       {
    1076          40 :         p = (double)x[k] + z[k];
    1077          40 :         if (y[k] + p*p*v[k] <= BOUND) break;
    1078           5 :         k++; x[k]--;
    1079           5 :       }
    1080          35 :     } while (k > 1);
    1081          30 :     if (! x[1] && y[1]<=eps) break;
    1082             : 
    1083          20 :     p = (double)x[1] + z[1]; p = y[1] + p*p*v[1]; /* norm(x) */
    1084          20 :     if (fun(E, u, x, p)) break;
    1085          20 :   }
    1086             : }
    1087             : 
    1088             : void
    1089           0 : forqfvec(void *E, long (*fun)(void *, GEN, GEN, double), GEN a, GEN BORNE)
    1090             : {
    1091           0 :   pari_sp av = avma;
    1092             :   struct qfvec qv;
    1093           0 :   forqfvec_init(&qv, a);
    1094           0 :   forqfvec_i(E, fun, &qv, BORNE);
    1095           0 :   avma = av;
    1096           0 : }
    1097             : 
    1098             : static long
    1099          20 : _gp_forqf(void *E, GEN u, GEN x, double p/*unused*/)
    1100             : {
    1101          20 :   pari_sp av = avma;
    1102             :   (void)p;
    1103          20 :   set_lex(-1, ZM_zc_mul_canon(u, x));
    1104          20 :   closure_evalvoid((GEN)E);
    1105          20 :   avma = av;
    1106          20 :   return loop_break();
    1107             : }
    1108             : 
    1109             : void
    1110          15 : forqfvec0(GEN a, GEN BORNE, GEN code)
    1111             : {
    1112          15 :   pari_sp av = avma;
    1113             :   struct qfvec qv;
    1114          15 :   forqfvec_init(&qv, a);
    1115          15 :   push_lex(gen_0, code);
    1116          15 :   forqfvec_i((void*) code, &_gp_forqf, &qv, BORNE);
    1117          15 :   pop_lex(1);
    1118          15 :   avma = av;
    1119          15 : }
    1120             : 
    1121             : enum { min_ALL = 0, min_FIRST, min_VECSMALL, min_VECSMALL2 };
    1122             : 
    1123             : /* Minimal vectors for the integral definite quadratic form: a.
    1124             :  * Result u:
    1125             :  *   u[1]= Number of vectors of square norm <= BORNE
    1126             :  *   u[2]= maximum norm found
    1127             :  *   u[3]= list of vectors found (at most STOCKMAX, unless NULL)
    1128             :  *
    1129             :  *  If BORNE = NULL: Minimal non-zero vectors.
    1130             :  *  flag = min_ALL,   as above
    1131             :  *  flag = min_FIRST, exits when first suitable vector is found.
    1132             :  *  flag = min_VECSMALL, return a t_VECSMALL of (half) the number of vectors
    1133             :  *  for each norm
    1134             :  *  flag = min_VECSMALL2, same but count only vectors with even norm, and shift
    1135             :  *  the answer */
    1136             : static GEN
    1137         400 : minim0_dolll(GEN a, GEN BORNE, GEN STOCKMAX, long flag, long dolll)
    1138             : {
    1139             :   GEN x, u, r, L, gnorme;
    1140         400 :   long n = lg(a), i, j, k, s, maxrank, sBORNE;
    1141         400 :   pari_sp av = avma, av1;
    1142             :   double p,maxnorm,BOUND,*v,*y,*z,**q;
    1143         400 :   const double eps = 1e-10;
    1144         400 :   int stockall = 0;
    1145             :   struct qfvec qv;
    1146             : 
    1147         400 :   if (!BORNE)
    1148          40 :     sBORNE = 0;
    1149             :   else
    1150             :   {
    1151         360 :     BORNE = gfloor(BORNE);
    1152         360 :     if (typ(BORNE) != t_INT) pari_err_TYPE("minim0",BORNE);
    1153         360 :     if (is_bigint(BORNE)) pari_err_PREC( "qfminim");
    1154         360 :     sBORNE = itos(BORNE); avma = av;
    1155             :   }
    1156         400 :   if (!STOCKMAX)
    1157             :   {
    1158         170 :     stockall = 1;
    1159         170 :     maxrank = 200;
    1160             :   }
    1161             :   else
    1162             :   {
    1163         230 :     STOCKMAX = gfloor(STOCKMAX);
    1164         230 :     if (typ(STOCKMAX) != t_INT) pari_err_TYPE("minim0",STOCKMAX);
    1165         230 :     maxrank = itos(STOCKMAX);
    1166         230 :     if (maxrank < 0)
    1167           0 :       pari_err_TYPE("minim0 [negative number of vectors]",STOCKMAX);
    1168             :   }
    1169             : 
    1170         400 :   switch(flag)
    1171             :   {
    1172             :     case min_VECSMALL:
    1173             :     case min_VECSMALL2:
    1174         195 :       if (sBORNE <= 0) return cgetg(1, t_VECSMALL);
    1175         195 :       L = zero_zv(sBORNE);
    1176         195 :       if (flag == min_VECSMALL2) sBORNE <<= 1;
    1177         195 :       if (n == 1) return L;
    1178         195 :       break;
    1179             :     case min_FIRST:
    1180          25 :       if (n == 1 || (!sBORNE && BORNE)) return cgetg(1,t_VEC);
    1181          15 :       L = NULL; /* gcc -Wall */
    1182          15 :       break;
    1183             :     case min_ALL:
    1184         180 :       if (n == 1 || (!sBORNE && BORNE))
    1185           5 :         retmkvec3(gen_0, gen_0, cgetg(1, t_MAT));
    1186         175 :       L = new_chunk(1+maxrank);
    1187         175 :       break;
    1188             :     default:
    1189           0 :       return NULL;
    1190             :   }
    1191         385 :   minim_alloc(n, &q, &x, &y, &z, &v);
    1192             : 
    1193         385 :   forqfvec_init_dolll(&qv, a, dolll);
    1194         385 :   av1 = avma;
    1195         385 :   r = qv.r;
    1196         385 :   u = qv.u;
    1197         385 :   n--;
    1198        3370 :   for (j=1; j<=n; j++)
    1199             :   {
    1200        2985 :     v[j] = rtodbl(gcoeff(r,j,j));
    1201        2985 :     for (i=1; i<j; i++) q[i][j] = rtodbl(gcoeff(r,i,j)); /* |.| <= 1/2 */
    1202             :   }
    1203             : 
    1204         385 :   if (sBORNE) maxnorm = 0.;
    1205             :   else
    1206             :   {
    1207          40 :     GEN B = gcoeff(a,1,1);
    1208          40 :     long t = 1;
    1209         440 :     for (i=2; i<=n; i++)
    1210             :     {
    1211         400 :       GEN c = gcoeff(a,i,i);
    1212         400 :       if (cmpii(c, B) < 0) { B = c; t = i; }
    1213             :     }
    1214          40 :     if (flag == min_FIRST) return gerepilecopy(av, mkvec2(B, gel(u,t)));
    1215          35 :     maxnorm = -1.; /* don't update maxnorm */
    1216          35 :     if (is_bigint(B)) return NULL;
    1217          35 :     sBORNE = itos(B);
    1218             :   }
    1219         380 :   BOUND = sBORNE * (1 + eps);
    1220         380 :   if ((long)BOUND != sBORNE) return NULL;
    1221             : 
    1222         370 :   s = 0;
    1223         370 :   k = n; y[n] = z[n] = 0;
    1224         370 :   x[n] = (long)sqrt(BOUND/v[n]);
    1225      735665 :   for(;;x[1]--)
    1226             :   {
    1227             :     do
    1228             :     {
    1229     1364640 :       if (k>1)
    1230             :       {
    1231      628970 :         long l = k-1;
    1232      628970 :         z[l] = 0;
    1233      628970 :         for (j=k; j<=n; j++) z[l] += q[l][j]*x[j];
    1234      628970 :         p = (double)x[k] + z[k];
    1235      628970 :         y[l] = y[k] + p*p*v[k];
    1236      628970 :         x[l] = (long)floor(sqrt((BOUND-y[l])/v[l])-z[l]);
    1237      628970 :         k = l;
    1238             :       }
    1239             :       for(;;)
    1240             :       {
    1241     1991145 :         p = (double)x[k] + z[k];
    1242     1991145 :         if (y[k] + p*p*v[k] <= BOUND) break;
    1243      626505 :         k++; x[k]--;
    1244      626505 :       }
    1245             :     }
    1246     1364640 :     while (k > 1);
    1247      736035 :     if (! x[1] && y[1]<=eps) break;
    1248             : 
    1249      735670 :     p = (double)x[1] + z[1]; p = y[1] + p*p*v[1]; /* norm(x) */
    1250      735670 :     if (maxnorm >= 0)
    1251             :     {
    1252      733850 :       if (p > maxnorm) maxnorm = p;
    1253             :     }
    1254             :     else
    1255             :     { /* maxnorm < 0 : only look for minimal vectors */
    1256        1820 :       pari_sp av2 = avma;
    1257        1820 :       gnorme = roundr(dbltor(p));
    1258        1820 :       if (cmpis(gnorme, sBORNE) >= 0) avma = av2;
    1259             :       else
    1260             :       {
    1261          10 :         sBORNE = itos(gnorme); avma = av1;
    1262          10 :         BOUND = sBORNE * (1+eps);
    1263          10 :         L = new_chunk(maxrank+1);
    1264          10 :         s = 0;
    1265             :       }
    1266             :     }
    1267      735670 :     s++;
    1268             : 
    1269      735670 :     switch(flag)
    1270             :     {
    1271             :       case min_FIRST:
    1272           5 :         if (dolll) x = ZM_zc_mul_canon(u,x);
    1273           5 :         return gerepilecopy(av, mkvec2(roundr(dbltor(p)), x));
    1274             : 
    1275             :       case min_ALL:
    1276      146450 :         if (s > maxrank && stockall) /* overflow */
    1277             :         {
    1278         290 :           long maxranknew = maxrank << 1;
    1279         290 :           GEN Lnew = new_chunk(1 + maxranknew);
    1280         290 :           for (i=1; i<=maxrank; i++) Lnew[i] = L[i];
    1281         290 :           L = Lnew; maxrank = maxranknew;
    1282             :         }
    1283      146450 :         if (s<=maxrank) gel(L,s) = leafcopy(x);
    1284      146450 :         break;
    1285             : 
    1286             :       case min_VECSMALL:
    1287       28000 :         { ulong norm = (ulong)(p + 0.5); L[norm]++; }
    1288       28000 :         break;
    1289             : 
    1290             :       case min_VECSMALL2:
    1291      561215 :         { ulong norm = (ulong)(p + 0.5); if (!odd(norm)) L[norm>>1]++; }
    1292      561215 :         break;
    1293             : 
    1294             :     }
    1295      735665 :   }
    1296         365 :   switch(flag)
    1297             :   {
    1298             :     case min_FIRST:
    1299           5 :       avma = av; return cgetg(1,t_VEC);
    1300             :     case min_VECSMALL:
    1301             :     case min_VECSMALL2:
    1302         195 :       avma = (pari_sp)L; return L;
    1303             :   }
    1304         165 :   r = (maxnorm >= 0) ? roundr(dbltor(maxnorm)): stoi(sBORNE);
    1305         165 :   k = minss(s,maxrank);
    1306         165 :   L[0] = evaltyp(t_MAT) | evallg(k + 1);
    1307         165 :   if (dolll)
    1308         140 :     for (j=1; j<=k; j++) gel(L,j) = ZM_zc_mul_canon(u, gel(L,j));
    1309         165 :   return gerepilecopy(av, mkvec3(stoi(s<<1), r, L));
    1310             : }
    1311             : 
    1312             : static GEN
    1313         370 : minim0(GEN a, GEN BORNE, GEN STOCKMAX, long flag)
    1314             : {
    1315         370 :   GEN v = minim0_dolll(a, BORNE, STOCKMAX, flag, 1);
    1316         370 :   if (!v) pari_err_PREC("qfminim");
    1317         365 :   return v;
    1318             : }
    1319             : 
    1320             : GEN
    1321         195 : qfrep0(GEN a, GEN borne, long flag)
    1322         195 : { return minim0(a, borne, gen_0, (flag & 1)? min_VECSMALL2: min_VECSMALL); }
    1323             : 
    1324             : GEN
    1325          75 : qfminim0(GEN a, GEN borne, GEN stockmax, long flag, long prec)
    1326             : {
    1327          75 :   switch(flag)
    1328             :   {
    1329          30 :     case 0: return minim0(a,borne,stockmax,min_ALL);
    1330          25 :     case 1: return minim0(a,borne,gen_0   ,min_FIRST);
    1331             :     case 2:
    1332             :     {
    1333          20 :       long maxnum = -1;
    1334          20 :       if (typ(a) != t_MAT) pari_err_TYPE("qfminim",a);
    1335          20 :       if (stockmax) {
    1336          10 :         if (typ(stockmax) != t_INT) pari_err_TYPE("qfminim",stockmax);
    1337          10 :         maxnum = itos(stockmax);
    1338             :       }
    1339          20 :       a = fincke_pohst(a,borne,maxnum,prec,NULL);
    1340          20 :       if (!a) pari_err_PREC("qfminim");
    1341          20 :       return a;
    1342             :     }
    1343           0 :     default: pari_err_FLAG("qfminim");
    1344             :   }
    1345             :   return NULL; /* LCOV_EXCL_LINE */
    1346             : }
    1347             : 
    1348             : GEN
    1349         120 : minim(GEN a, GEN borne, GEN stockmax)
    1350         120 : { return minim0(a,borne,stockmax,min_ALL); }
    1351             : 
    1352             : GEN
    1353          30 : minim_raw(GEN a, GEN BORNE, GEN STOCKMAX)
    1354          30 : { return minim0_dolll(a, BORNE, STOCKMAX, min_ALL, 0); }
    1355             : 
    1356             : GEN
    1357           0 : minim2(GEN a, GEN borne, GEN stockmax)
    1358           0 : { return minim0(a,borne,stockmax,min_FIRST); }
    1359             : 
    1360             : /* If V depends linearly from the columns of the matrix, return 0.
    1361             :  * Otherwise, update INVP and L and return 1. No GC. */
    1362             : static int
    1363        1180 : addcolumntomatrix(GEN V, GEN invp, GEN L)
    1364             : {
    1365        1180 :   long i,j,k, n = lg(invp);
    1366        1180 :   GEN a = cgetg(n, t_COL), ak = NULL, mak;
    1367             : 
    1368       60165 :   for (k = 1; k < n; k++)
    1369       59790 :     if (!L[k])
    1370             :     {
    1371       19865 :       ak = RgMrow_zc_mul(invp, V, k);
    1372       19865 :       if (!gequal0(ak)) break;
    1373             :     }
    1374        1180 :   if (k == n) return 0;
    1375         805 :   L[k] = 1;
    1376         805 :   mak = gneg_i(ak);
    1377       30895 :   for (i=k+1; i<n; i++)
    1378       30090 :     gel(a,i) = gdiv(RgMrow_zc_mul(invp, V, i), mak);
    1379       31345 :   for (j=1; j<=k; j++)
    1380             :   {
    1381       30540 :     GEN c = gel(invp,j), ck = gel(c,k);
    1382       30540 :     if (gequal0(ck)) continue;
    1383        6765 :     gel(c,k) = gdiv(ck, ak);
    1384        6765 :     if (j==k)
    1385       30895 :       for (i=k+1; i<n; i++)
    1386       30090 :         gel(c,i) = gmul(gel(a,i), ck);
    1387             :     else
    1388      149985 :       for (i=k+1; i<n; i++)
    1389      144025 :         gel(c,i) = gadd(gel(c,i), gmul(gel(a,i), ck));
    1390             :   }
    1391         805 :   return 1;
    1392             : }
    1393             : 
    1394             : GEN
    1395          30 : perf(GEN a)
    1396             : {
    1397          30 :   pari_sp av = avma;
    1398             :   GEN u, L;
    1399          30 :   long r, s, k, l, n = lg(a)-1;
    1400             : 
    1401          30 :   if (!n) return gen_0;
    1402          30 :   if (typ(a) != t_MAT || !RgM_is_ZM(a)) pari_err_TYPE("qfperfection",a);
    1403          30 :   a = minim_lll(a, &u);
    1404          30 :   L = minim_raw(a,NULL,NULL);
    1405          30 :   r = (n*(n+1)) >> 1;
    1406          30 :   if (L)
    1407             :   {
    1408             :     GEN D, V, invp;
    1409          25 :     L = gel(L, 3); l = lg(L);
    1410          25 :     if (l == 2) { avma = av; return gen_1; }
    1411             : 
    1412          15 :     D = zero_zv(r);
    1413          15 :     V = cgetg(r+1, t_VECSMALL);
    1414          15 :     invp = matid(r);
    1415          15 :     s = 0;
    1416        1185 :     for (k = 1; k < l; k++)
    1417             :     {
    1418        1180 :       pari_sp av2 = avma;
    1419        1180 :       GEN x = gel(L,k);
    1420             :       long i, j, I;
    1421       15070 :       for (i = I = 1; i<=n; i++)
    1422       13890 :         for (j=i; j<=n; j++,I++) V[I] = x[i]*x[j];
    1423        1180 :       if (!addcolumntomatrix(V,invp,D)) avma = av2;
    1424         805 :       else if (++s == r) break;
    1425             :     }
    1426             :   }
    1427             :   else
    1428             :   {
    1429             :     GEN M;
    1430           5 :     L = fincke_pohst(a,NULL,-1, DEFAULTPREC, NULL);
    1431           5 :     if (!L) pari_err_PREC("qfminim");
    1432           5 :     L = gel(L, 3); l = lg(L);
    1433           5 :     if (l == 2) { avma = av; return gen_1; }
    1434           5 :     M = cgetg(l, t_MAT);
    1435         685 :     for (k = 1; k < l; k++)
    1436             :     {
    1437         680 :       GEN x = gel(L,k), c = cgetg(r+1, t_COL);
    1438             :       long i, I, j;
    1439       11560 :       for (i = I = 1; i<=n; i++)
    1440       10880 :         for (j=i; j<=n; j++,I++) gel(c,I) = mulii(gel(x,i), gel(x,j));
    1441         680 :       gel(M,k) = c;
    1442             :     }
    1443           5 :     s = ZM_rank(M);
    1444             :   }
    1445          20 :  avma = av; return utoipos(s);
    1446             : }
    1447             : 
    1448             : static GEN
    1449          45 : clonefill(GEN S, long s, long t)
    1450             : { /* initialize to dummy values */
    1451          45 :   GEN T = S, dummy = cgetg(1, t_STR);
    1452             :   long i;
    1453          45 :   for (i = s+1; i <= t; i++) gel(S,i) = dummy;
    1454          45 :   S = gclone(S); if (isclone(T)) gunclone(T);
    1455          45 :   return S;
    1456             : }
    1457             : 
    1458             : /* increment ZV x, by incrementing cell of index k. Initial value x0[k] was
    1459             :  * chosen to minimize qf(x) for given x0[1..k-1] and x0[k+1,..] = 0
    1460             :  * The last non-zero entry must be positive and goes through x0[k]+1,2,3,...
    1461             :  * Others entries go through: x0[k]+1,-1,2,-2,...*/
    1462             : INLINE void
    1463      653785 : step(GEN x, GEN y, GEN inc, long k)
    1464             : {
    1465      653785 :   if (!signe(gel(y,k))) /* x[k+1..] = 0 */
    1466        6415 :     gel(x,k) = addiu(gel(x,k), 1); /* leading coeff > 0 */
    1467             :   else
    1468             :   {
    1469      647370 :     long i = inc[k];
    1470      647370 :     gel(x,k) = addis(gel(x,k), i),
    1471      647370 :     inc[k] = (i > 0)? -1-i: 1-i;
    1472             :   }
    1473      653785 : }
    1474             : 
    1475             : /* 1 if we are "sure" that x < y, up to few rounding errors, i.e.
    1476             :  * x < y - epsilon. More precisely :
    1477             :  * - sign(x - y) < 0
    1478             :  * - lgprec(x-y) > 3 || expo(x - y) - expo(x) > -24 */
    1479             : static int
    1480      256685 : mplessthan(GEN x, GEN y)
    1481             : {
    1482      256685 :   pari_sp av = avma;
    1483      256685 :   GEN z = mpsub(x, y);
    1484      256685 :   avma = av;
    1485      256685 :   if (typ(z) == t_INT) return (signe(z) < 0);
    1486      256685 :   if (signe(z) >= 0) return 0;
    1487       41970 :   if (realprec(z) > LOWDEFAULTPREC) return 1;
    1488       41970 :   return ( expo(z) - mpexpo(x) > -24 );
    1489             : }
    1490             : 
    1491             : /* 1 if we are "sure" that x > y, up to few rounding errors, i.e.
    1492             :  * x > y + epsilon */
    1493             : static int
    1494     1034965 : mpgreaterthan(GEN x, GEN y)
    1495             : {
    1496     1034965 :   pari_sp av = avma;
    1497     1034965 :   GEN z = mpsub(x, y);
    1498     1034965 :   avma = av;
    1499     1034965 :   if (typ(z) == t_INT) return (signe(z) > 0);
    1500     1034965 :   if (signe(z) <= 0) return 0;
    1501      585716 :   if (realprec(z) > LOWDEFAULTPREC) return 1;
    1502      111601 :   return ( expo(z) - mpexpo(x) > -24 );
    1503             : }
    1504             : 
    1505             : /* x a t_INT, y  t_INT or t_REAL */
    1506             : INLINE GEN
    1507      255585 : mulimp(GEN x, GEN y)
    1508             : {
    1509      255585 :   if (typ(y) == t_INT) return mulii(x,y);
    1510      255585 :   return signe(x) ? mulir(x,y): gen_0;
    1511             : }
    1512             : /* x + y*z, x,z two mp's, y a t_INT */
    1513             : INLINE GEN
    1514     3218325 : addmulimp(GEN x, GEN y, GEN z)
    1515             : {
    1516     3218325 :   if (!signe(y)) return x;
    1517     1391685 :   if (typ(z) == t_INT) return mpadd(x, mulii(y, z));
    1518     1391685 :   return mpadd(x, mulir(y, z));
    1519             : }
    1520             : 
    1521             : /* yk + vk * (xk + zk)^2 */
    1522             : static GEN
    1523     1286990 : norm_aux(GEN xk, GEN yk, GEN zk, GEN vk)
    1524             : {
    1525     1286990 :   GEN t = mpadd(xk, zk);
    1526     1286990 :   if (typ(t) == t_INT) { /* probably gen_0, avoid loss of accuracy */
    1527       11965 :     yk = addmulimp(yk, sqri(t), vk);
    1528             :   } else {
    1529     1275025 :     yk = mpadd(yk, mpmul(sqrr(t), vk));
    1530             :   }
    1531     1286990 :   return yk;
    1532             : }
    1533             : /* yk + vk * (xk + zk)^2 < B + epsilon */
    1534             : static int
    1535      909125 : check_bound(GEN B, GEN xk, GEN yk, GEN zk, GEN vk)
    1536             : {
    1537      909125 :   pari_sp av = avma;
    1538      909125 :   int f = mpgreaterthan(norm_aux(xk,yk,zk,vk), B);
    1539      909125 :   avma = av; return !f;
    1540             : }
    1541             : 
    1542             : /* q(k-th canonical basis vector), where q is given in Cholesky form
    1543             :  * q(x) = sum_{i = 1}^n q[i,i] (x[i] + sum_{j > i} q[i,j] x[j])^2.
    1544             :  * Namely q(e_k) = q[k,k] + sum_{i < k} q[i,i] q[i,k]^2
    1545             :  * Assume 1 <= k <= n. */
    1546             : static GEN
    1547         130 : cholesky_norm_ek(GEN q, long k)
    1548             : {
    1549         130 :   GEN t = gcoeff(q,k,k);
    1550             :   long i;
    1551         130 :   for (i = 1; i < k; i++) t = norm_aux(gen_0, t, gcoeff(q,i,k), gcoeff(q,i,i));
    1552         130 :   return t;
    1553             : }
    1554             : 
    1555             : /* q is the Cholesky decomposition of a quadratic form
    1556             :  * Enumerate vectors whose norm is less than BORNE (Algo 2.5.7),
    1557             :  * minimal vectors if BORNE = NULL (implies check = NULL).
    1558             :  * If (check != NULL) consider only vectors passing the check, and assumes
    1559             :  *   we only want the smallest possible vectors */
    1560             : static GEN
    1561         560 : smallvectors(GEN q, GEN BORNE, long maxnum, FP_chk_fun *CHECK)
    1562             : {
    1563         560 :   long N = lg(q), n = N-1, i, j, k, s, stockmax, checkcnt = 1;
    1564             :   pari_sp av, av1;
    1565             :   GEN inc, S, x, y, z, v, p1, alpha, norms;
    1566             :   GEN norme1, normax1, borne1, borne2;
    1567         560 :   GEN (*check)(void *,GEN) = CHECK? CHECK->f: NULL;
    1568         560 :   void *data = CHECK? CHECK->data: NULL;
    1569         560 :   const long skipfirst = CHECK? CHECK->skipfirst: 0;
    1570         560 :   const int stockall = (maxnum == -1);
    1571             : 
    1572         560 :   alpha = dbltor(0.95);
    1573         560 :   normax1 = gen_0;
    1574             : 
    1575         560 :   v = cgetg(N,t_VEC);
    1576         560 :   inc = const_vecsmall(n, 1);
    1577             : 
    1578         560 :   av = avma;
    1579         560 :   stockmax = stockall? 2000: maxnum;
    1580         560 :   norms = cgetg(check?(stockmax+1): 1,t_VEC); /* unused if (!check) */
    1581         560 :   S = cgetg(stockmax+1,t_VEC);
    1582         560 :   x = cgetg(N,t_COL);
    1583         560 :   y = cgetg(N,t_COL);
    1584         560 :   z = cgetg(N,t_COL);
    1585        3425 :   for (i=1; i<N; i++) {
    1586        2865 :     gel(v,i) = gcoeff(q,i,i);
    1587        2865 :     gel(x,i) = gel(y,i) = gel(z,i) = gen_0;
    1588             :   }
    1589         560 :   if (BORNE)
    1590             :   {
    1591         550 :     borne1 = BORNE;
    1592         550 :     if (typ(borne1) != t_REAL)
    1593             :     {
    1594             :       long prec;
    1595         305 :       if (gequal0(borne1)) retmkvec3(gen_0, gen_0, cgetg(1,t_MAT));
    1596         300 :       prec = nbits2prec(gexpo(borne1) + 10);
    1597         300 :       borne1 = gtofp(borne1, maxss(prec, DEFAULTPREC));
    1598             :     }
    1599             :   }
    1600             :   else
    1601             :   {
    1602          10 :     borne1 = gcoeff(q,1,1);
    1603         140 :     for (i=2; i<N; i++)
    1604             :     {
    1605         130 :       GEN b = cholesky_norm_ek(q, i);
    1606         130 :       if (gcmp(b, borne1) < 0) borne1 = b;
    1607             :     }
    1608             :     /* borne1 = norm of smallest basis vector */
    1609             :   }
    1610         555 :   borne2 = mulrr(borne1,alpha);
    1611         555 :   if (DEBUGLEVEL>2)
    1612           0 :     err_printf("smallvectors looking for norm < %P.4G\n",borne1);
    1613         555 :   s = 0; k = n;
    1614      121660 :   for(;; step(x,y,inc,k)) /* main */
    1615             :   { /* x (supposedly) small vector, ZV.
    1616             :      * For all t >= k, we have
    1617             :      *   z[t] = sum_{j > t} q[t,j] * x[j]
    1618             :      *   y[t] = sum_{i > t} q[i,i] * (x[i] + z[i])^2
    1619             :      *        = 0 <=> x[i]=0 for all i>t */
    1620             :     do
    1621             :     {
    1622      377245 :       int skip = 0;
    1623      377245 :       if (k > 1)
    1624             :       {
    1625      255585 :         long l = k-1;
    1626      255585 :         av1 = avma;
    1627      255585 :         p1 = mulimp(gel(x,k), gcoeff(q,l,k));
    1628      255585 :         for (j=k+1; j<N; j++) p1 = addmulimp(p1, gel(x,j), gcoeff(q,l,j));
    1629      255585 :         gel(z,l) = gerepileuptoleaf(av1,p1);
    1630             : 
    1631      255585 :         av1 = avma;
    1632      255585 :         p1 = norm_aux(gel(x,k), gel(y,k), gel(z,k), gel(v,k));
    1633      255585 :         gel(y,l) = gerepileuptoleaf(av1, p1);
    1634             :         /* skip the [x_1,...,x_skipfirst,0,...,0] */
    1635      255585 :         if ((l <= skipfirst && !signe(gel(y,skipfirst)))
    1636      255340 :          || mplessthan(borne1, gel(y,l))) skip = 1;
    1637             :         else /* initial value, minimizing (x[l] + z[l])^2, hence qf(x) for
    1638             :                 the given x[1..l-1] */
    1639      255340 :           gel(x,l) = mpround( mpneg(gel(z,l)) );
    1640      255585 :         k = l;
    1641             :       }
    1642      255585 :       for(;; step(x,y,inc,k))
    1643             :       { /* at most 2n loops */
    1644      632830 :         if (!skip)
    1645             :         {
    1646      632585 :           if (check_bound(borne1, gel(x,k),gel(y,k),gel(z,k),gel(v,k))) break;
    1647      276540 :           step(x,y,inc,k);
    1648      276540 :           if (check_bound(borne1, gel(x,k),gel(y,k),gel(z,k),gel(v,k))) break;
    1649             :         }
    1650      256140 :         skip = 0; inc[k] = 1;
    1651      256140 :         if (++k > n) goto END;
    1652      255585 :       }
    1653             : 
    1654      376690 :       if (gc_needed(av,2))
    1655             :       {
    1656           5 :         if(DEBUGMEM>1) pari_warn(warnmem,"smallvectors");
    1657           5 :         if (stockmax) S = clonefill(S, s, stockmax);
    1658           5 :         if (check) {
    1659           5 :           GEN dummy = cgetg(1, t_STR);
    1660           5 :           for (i=s+1; i<=stockmax; i++) gel(norms,i) = dummy;
    1661             :         }
    1662           5 :         gerepileall(av,7,&x,&y,&z,&normax1,&borne1,&borne2,&norms);
    1663             :       }
    1664             :     }
    1665      376690 :     while (k > 1);
    1666      121660 :     if (!signe(gel(x,1)) && !signe(gel(y,1))) continue; /* exclude 0 */
    1667             : 
    1668      121350 :     av1 = avma;
    1669      121350 :     norme1 = norm_aux(gel(x,1),gel(y,1),gel(z,1),gel(v,1));
    1670      121350 :     if (mpgreaterthan(norme1,borne1)) { avma = av1; continue; /* main */ }
    1671             : 
    1672      121350 :     norme1 = gerepileuptoleaf(av1,norme1);
    1673      121350 :     if (check)
    1674             :     {
    1675       72365 :       if (checkcnt < 5 && mpcmp(norme1, borne2) < 0)
    1676             :       {
    1677         305 :         if (!check(data,x)) { checkcnt++ ; continue; /* main */}
    1678         125 :         if (DEBUGLEVEL>4) err_printf("New bound: %Ps", norme1);
    1679         125 :         borne1 = norme1;
    1680         125 :         borne2 = mulrr(borne1, alpha);
    1681         125 :         s = 0; checkcnt = 0;
    1682             :       }
    1683             :     }
    1684             :     else
    1685             :     {
    1686       48985 :       if (!BORNE) /* find minimal vectors */
    1687             :       {
    1688        1345 :         if (mplessthan(norme1, borne1))
    1689             :         { /* strictly smaller vector than previously known */
    1690           0 :           borne1 = norme1; /* + epsilon */
    1691           0 :           s = 0;
    1692             :         }
    1693             :       }
    1694             :       else
    1695       47640 :         if (mpcmp(norme1,normax1) > 0) normax1 = norme1;
    1696             :     }
    1697      121170 :     if (++s > stockmax) continue; /* too many vectors: no longer remember */
    1698      120505 :     if (check) gel(norms,s) = norme1;
    1699      120505 :     gel(S,s) = leafcopy(x);
    1700      120505 :     if (s != stockmax) continue; /* still room, get next vector */
    1701             : 
    1702             :     /* overflow, eliminate vectors failing "check" */
    1703          40 :     if (check)
    1704             :     {
    1705          25 :       pari_sp av2 = avma;
    1706             :       long imin, imax;
    1707          25 :       GEN per = indexsort(norms);
    1708          25 :       if (DEBUGLEVEL>2) err_printf("sorting... [%ld elts]\n",s);
    1709             :       /* let N be the minimal norm so far for x satisfying 'check'. Keep
    1710             :        * all elements of norm N */
    1711        2625 :       for (i = 1; i <= s; i++)
    1712             :       {
    1713        2625 :         long k = per[i];
    1714        2625 :         if (check(data,gel(S,k))) { borne1 = gel(norms,k); break; }
    1715             :       }
    1716          25 :       imin = i;
    1717         195 :       for (; i <= s; i++)
    1718         195 :         if (mpgreaterthan(gel(norms,per[i]), borne1)) break;
    1719          25 :       imax = i;
    1720          25 :       for (i=imin, s=0; i < imax; i++) gel(S,++s) = gel(S,per[i]);
    1721          25 :       avma = av2;
    1722          25 :       if (s)
    1723             :       {
    1724          25 :         borne2 = mulrr(borne1, alpha);
    1725          25 :         checkcnt = 0;
    1726             :       }
    1727          25 :       if (!stockall) continue;
    1728          25 :       if (s > stockmax/2) stockmax <<= 1;
    1729          25 :       norms = cgetg(stockmax+1, t_VEC);
    1730          25 :       for (i = 1; i <= s; i++) gel(norms,i) = borne1;
    1731             :     }
    1732             :     else
    1733             :     {
    1734          15 :       if (!stockall && BORNE) goto END;
    1735          15 :       if (!stockall) continue;
    1736          15 :       stockmax <<= 1;
    1737             :     }
    1738             : 
    1739             :     {
    1740          40 :       GEN Snew = cgetg(stockmax + 1, t_VEC);
    1741          40 :       for (i = 1; i <= s; i++) gel(Snew,i) = gel(S,i);
    1742          40 :       Snew = clonefill(Snew, s, stockmax);
    1743          40 :       if (isclone(S)) gunclone(S);
    1744          40 :       S = Snew;
    1745             :     }
    1746      121660 :   }
    1747             : END:
    1748         555 :   if (s < stockmax) stockmax = s;
    1749         555 :   if (check)
    1750             :   {
    1751             :     GEN per, alph, pols, p;
    1752         540 :     if (DEBUGLEVEL>2) err_printf("final sort & check...\n");
    1753         540 :     setlg(norms,stockmax+1); per = indexsort(norms);
    1754         540 :     alph = cgetg(stockmax+1,t_VEC);
    1755         540 :     pols = cgetg(stockmax+1,t_VEC);
    1756        5425 :     for (j=0,i=1; i<=stockmax; i++)
    1757             :     {
    1758        4900 :       long t = per[i];
    1759        4900 :       GEN N = gel(norms,t);
    1760        4900 :       if (j && mpgreaterthan(N, borne1)) break;
    1761        4885 :       if ((p = check(data,gel(S,t))))
    1762             :       {
    1763        4420 :         if (!j) borne1 = N;
    1764        4420 :         j++;
    1765        4420 :         gel(pols,j) = p;
    1766        4420 :         gel(alph,j) = gel(S,t);
    1767             :       }
    1768             :     }
    1769         540 :     setlg(pols,j+1);
    1770         540 :     setlg(alph,j+1);
    1771         540 :     if (stockmax && isclone(S)) { alph = gcopy(alph); gunclone(S); }
    1772         540 :     return mkvec2(pols, alph);
    1773             :   }
    1774          15 :   if (stockmax)
    1775             :   {
    1776          10 :     setlg(S,stockmax+1);
    1777          10 :     settyp(S,t_MAT);
    1778          10 :     if (isclone(S)) { p1 = S; S = gcopy(S); gunclone(p1); }
    1779             :   }
    1780             :   else
    1781           5 :     S = cgetg(1,t_MAT);
    1782          15 :   return mkvec3(utoi(s<<1), borne1, S);
    1783             : }
    1784             : 
    1785             : /* solve q(x) = x~.a.x <= bound, a > 0.
    1786             :  * If check is non-NULL keep x only if check(x).
    1787             :  * If a is a vector, assume a[1] is the LLL-reduced Cholesky form of q */
    1788             : GEN
    1789         570 : fincke_pohst(GEN a, GEN B0, long stockmax, long PREC, FP_chk_fun *CHECK)
    1790             : {
    1791         570 :   pari_sp av = avma;
    1792             :   VOLATILE long i,j,l;
    1793         570 :   VOLATILE GEN r,rinv,rinvtrans,u,v,res,z,vnorm,rperm,perm,uperm, bound = B0;
    1794             : 
    1795         570 :   if (typ(a) == t_VEC)
    1796             :   {
    1797         250 :     r = gel(a,1);
    1798         250 :     u = NULL;
    1799             :   }
    1800             :   else
    1801             :   {
    1802         320 :     long prec = PREC;
    1803         320 :     l = lg(a);
    1804         320 :     if (l == 1)
    1805             :     {
    1806           5 :       if (CHECK) pari_err_TYPE("fincke_pohst [dimension 0]", a);
    1807           5 :       retmkvec3(gen_0, gen_0, cgetg(1,t_MAT));
    1808             :     }
    1809         315 :     u = lllfp(a, 0.75, LLL_GRAM);
    1810         315 :     if (lg(u) != lg(a)) return NULL;
    1811         315 :     r = qf_apply_RgM(a,u);
    1812         315 :     i = gprecision(r);
    1813         315 :     if (i)
    1814         295 :       prec = i;
    1815             :     else {
    1816          20 :       prec = DEFAULTPREC + nbits2extraprec(gexpo(r));
    1817          20 :       if (prec < PREC) prec = PREC;
    1818             :     }
    1819         315 :     if (DEBUGLEVEL>2) err_printf("first LLL: prec = %ld\n", prec);
    1820         315 :     r = qfgaussred_positive(r);
    1821         315 :     if (!r) return NULL;
    1822        1425 :     for (i=1; i<l; i++)
    1823             :     {
    1824        1110 :       GEN s = gsqrt(gcoeff(r,i,i), prec);
    1825        1110 :       gcoeff(r,i,i) = s;
    1826        1110 :       for (j=i+1; j<l; j++) gcoeff(r,i,j) = gmul(s, gcoeff(r,i,j));
    1827             :     }
    1828             :   }
    1829             :   /* now r~ * r = a in LLL basis */
    1830         565 :   rinv = RgM_inv_upper(r);
    1831         565 :   if (!rinv) return NULL;
    1832         565 :   rinvtrans = shallowtrans(rinv);
    1833         565 :   if (DEBUGLEVEL>2)
    1834           0 :     err_printf("Fincke-Pohst, final LLL: prec = %ld\n", gprecision(rinvtrans));
    1835         565 :   v = lll(rinvtrans);
    1836         565 :   if (lg(v) != lg(rinvtrans)) return NULL;
    1837             : 
    1838         565 :   rinvtrans = RgM_mul(rinvtrans, v);
    1839         565 :   v = ZM_inv(shallowtrans(v),gen_1);
    1840         565 :   r = RgM_mul(r,v);
    1841         565 :   u = u? ZM_mul(u,v): v;
    1842             : 
    1843         565 :   l = lg(r);
    1844         565 :   vnorm = cgetg(l,t_VEC);
    1845         565 :   for (j=1; j<l; j++) gel(vnorm,j) = gnorml2(gel(rinvtrans,j));
    1846         565 :   rperm = cgetg(l,t_MAT);
    1847         565 :   uperm = cgetg(l,t_MAT); perm = indexsort(vnorm);
    1848         565 :   for (i=1; i<l; i++) { uperm[l-i] = u[perm[i]]; rperm[l-i] = r[perm[i]]; }
    1849         565 :   u = uperm;
    1850         565 :   r = rperm; res = NULL;
    1851         565 :   pari_CATCH(e_PREC) { }
    1852             :   pari_TRY {
    1853             :     GEN q;
    1854         565 :     if (CHECK && CHECK->f_init) bound = CHECK->f_init(CHECK, r, u);
    1855         560 :     q = gaussred_from_QR(r, gprecision(vnorm));
    1856         560 :     if (!q) pari_err_PREC("fincke_pohst");
    1857         560 :     res = smallvectors(q, bound, stockmax, CHECK);
    1858         560 :   } pari_ENDCATCH;
    1859         565 :   if (CHECK)
    1860             :   {
    1861         545 :     if (CHECK->f_post) res = CHECK->f_post(CHECK, res, u);
    1862         545 :     return res;
    1863             :   }
    1864          20 :   if (!res) pari_err_PREC("fincke_pohst");
    1865             : 
    1866          20 :   z = cgetg(4,t_VEC);
    1867          20 :   gel(z,1) = gcopy(gel(res,1));
    1868          20 :   gel(z,2) = gcopy(gel(res,2));
    1869          20 :   gel(z,3) = ZM_mul(u, gel(res,3)); return gerepileupto(av,z);
    1870             : }

Generated by: LCOV version 1.11