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.8.0 lcov report (development 19614-52e089f) Lines: 978 1028 95.1 %
Date: 2016-09-28 05:54:17 Functions: 60 65 92.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      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     1011221 : no_prec_pb(GEN x)
      27             : {
      28     2762236 :   return (typ(x) != t_REAL || realprec(x) > LOWDEFAULTPREC
      29     1013325 :                            || 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     1011221 : FindApplyQ(GEN x, GEN L, GEN B, long k, GEN Q, long prec)
      36             : {
      37     1011221 :   long i, lx = lg(x)-1;
      38     1011221 :   GEN x2, x1, xd = x + (k-1);
      39             : 
      40     1011221 :   x1 = gel(xd,1);
      41     1011221 :   x2 = mpsqr(x1);
      42     1011221 :   if (k < lx)
      43             :   {
      44      679082 :     long lv = lx - (k-1) + 1;
      45      679082 :     GEN beta, Nx, v = cgetg(lv, t_VEC);
      46     2387417 :     for (i=2; i<lv; i++) {
      47     1708335 :       x2 = mpadd(x2, mpsqr(gel(xd,i)));
      48     1708335 :       gel(v,i) = gel(xd,i);
      49             :     }
      50      679082 :     if (!signe(x2)) return 0;
      51      679082 :     Nx = gsqrt(x2, prec); if (signe(x1) < 0) setsigne(Nx, -1);
      52      679082 :     gel(v,1) = mpadd(x1, Nx);
      53             : 
      54      679082 :     if (!signe(x1))
      55         127 :       beta = gtofp(x2, prec); /* make sure typ(beta) != t_INT */
      56             :     else
      57      678955 :       beta = mpadd(x2, mpmul(Nx,x1));
      58      679082 :     gel(Q,k) = mkvec2(invr(beta), v);
      59             : 
      60      679082 :     togglesign(Nx);
      61      679082 :     gcoeff(L,k,k) = Nx;
      62             :   }
      63             :   else
      64      332139 :     gcoeff(L,k,k) = gel(x,k);
      65     1011221 :   gel(B,k) = x2;
      66     1011221 :   for (i=1; i<k; i++) gcoeff(L,k,i) = gel(x,i);
      67     1011221 :   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     1708317 : ApplyQ(GEN Q, GEN r)
      74             : {
      75     1708317 :   GEN s, rd, beta = gel(Q,1), v = gel(Q,2);
      76     1708317 :   long i, l = lg(v), lr = lg(r);
      77             : 
      78     1708317 :   rd = r + (lr - l);
      79     1708317 :   s = mpmul(gel(v,1), gel(rd,1));
      80     1708317 :   for (i=2; i<l; i++) s = mpadd(s, mpmul(gel(v,i) ,gel(rd,i)));
      81     1708317 :   s = mpmul(beta, s);
      82    12389417 :   for (i=1; i<l; i++)
      83    10681100 :     if (signe(gel(v,i))) gel(rd,i) = mpsub(gel(rd,i), mpmul(s, gel(v,i)));
      84     1708317 : }
      85             : /* apply Q[1], ..., Q[j-1] to r */
      86             : static GEN
      87      679076 : ApplyAllQ(GEN Q, GEN r, long j)
      88             : {
      89      679076 :   pari_sp av = avma;
      90             :   long i;
      91      679076 :   r = leafcopy(r);
      92      679076 :   for (i=1; i<j; i++) ApplyQ(gel(Q,i), r);
      93      679076 :   return gerepilecopy(av, r);
      94             : }
      95             : 
      96             : /* same, arbitrary coefficients [20% slower for t_REAL at DEFAULTPREC] */
      97             : static void
      98       21840 : RgC_ApplyQ(GEN Q, GEN r)
      99             : {
     100       21840 :   GEN s, rd, beta = gel(Q,1), v = gel(Q,2);
     101       21840 :   long i, l = lg(v), lr = lg(r);
     102             : 
     103       21840 :   rd = r + (lr - l);
     104       21840 :   s = gmul(gel(v,1), gel(rd,1));
     105       21840 :   for (i=2; i<l; i++) s = gadd(s, gmul(gel(v,i) ,gel(rd,i)));
     106       21840 :   s = gmul(beta, s);
     107      480480 :   for (i=1; i<l; i++)
     108      458640 :     if (signe(gel(v,i))) gel(rd,i) = gsub(gel(rd,i), gmul(s, gel(v,i)));
     109       21840 : }
     110             : static GEN
     111         560 : RgC_ApplyAllQ(GEN Q, GEN r, long j)
     112             : {
     113         560 :   pari_sp av = avma;
     114             :   long i;
     115         560 :   r = leafcopy(r);
     116         560 :   for (i=1; i<j; i++) RgC_ApplyQ(gel(Q,i), r);
     117         560 :   return gerepilecopy(av, r);
     118             : }
     119             : 
     120             : int
     121          14 : RgM_QR_init(GEN x, GEN *pB, GEN *pQ, GEN *pL, long prec)
     122             : {
     123          14 :   x = RgM_gtomp(x, prec);
     124          14 :   return QR_init(x, pB, pQ, pL, prec);
     125             : }
     126             : 
     127             : static void
     128          28 : check_householder(GEN Q)
     129             : {
     130          28 :   long i, l = lg(Q);
     131          28 :   if (typ(Q) != t_VEC) pari_err_TYPE("mathouseholder", Q);
     132         840 :   for (i = 1; i < l; i++)
     133             :   {
     134         819 :     GEN q = gel(Q,i), v;
     135         819 :     if (typ(q) != t_VEC || lg(q) != 3) pari_err_TYPE("mathouseholder", Q);
     136         819 :     v = gel(q,2);
     137         819 :     if (typ(v) != t_VEC || lg(v)+i-2 != l) pari_err_TYPE("mathouseholder", Q);
     138             :   }
     139          21 : }
     140             : 
     141             : GEN
     142          28 : mathouseholder(GEN Q, GEN v)
     143             : {
     144          28 :   long l = lg(Q);
     145          28 :   check_householder(Q);
     146          21 :   switch(typ(v))
     147             :   {
     148             :     case t_MAT:
     149             :     {
     150             :       long lx, i;
     151          14 :       GEN M = cgetg_copy(v, &lx);
     152          14 :       for (i = 1; i < lx; i++) gel(M,i) = RgC_ApplyAllQ(Q, gel(v,i), l);
     153          14 :       return M;
     154             :     }
     155           0 :     case t_COL: if (lg(v) == l) break;
     156             :       /* fall through */
     157           7 :     default: pari_err_TYPE("mathouseholder", v);
     158             :   }
     159           0 :   return RgC_ApplyAllQ(Q, v, l);
     160             : }
     161             : 
     162             : GEN
     163          28 : matqr(GEN x, long flag, long prec)
     164             : {
     165          28 :   pari_sp av = avma;
     166             :   GEN B, Q, L;
     167          28 :   long n = lg(x)-1;
     168          28 :   if (typ(x) != t_MAT) pari_err_TYPE("matqr",x);
     169          28 :   if (!n)
     170             :   {
     171          14 :     if (!flag) retmkvec2(cgetg(1,t_MAT),cgetg(1,t_MAT));
     172           7 :     retmkvec2(cgetg(1,t_VEC),cgetg(1,t_MAT));
     173             :   }
     174          14 :   if (n != nbrows(x)) pari_err_DIM("matqr");
     175          14 :   if (!RgM_QR_init(x, &B,&Q,&L, prec)) pari_err_PREC("matqr");
     176          14 :   if (!flag) Q = shallowtrans(mathouseholder(Q, matid(n)));
     177          14 :   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      332145 : QR_init(GEN x, GEN *pB, GEN *pQ, GEN *pL, long prec)
     183             : {
     184      332145 :   long j, k = lg(x)-1;
     185      332145 :   GEN B = cgetg(k+1, t_VEC), Q = cgetg(k, t_VEC), L = zeromatcopy(k,k);
     186     1343354 :   for (j=1; j<=k; j++)
     187             :   {
     188     1011221 :     GEN r = gel(x,j);
     189     1011221 :     if (j > 1) r = ApplyAllQ(Q, r, j);
     190     1011221 :     if (!FindApplyQ(r, L, B, j, Q, prec)) return 0;
     191             :   }
     192      332133 :   *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      331768 : gaussred_from_QR(GEN x, long prec)
     198             : {
     199      331768 :   long j, k = lg(x)-1;
     200             :   GEN B, Q, L;
     201      331768 :   if (!QR_init(x, &B,&Q,&L, prec)) return NULL;
     202     1008142 :   for (j=1; j<k; j++)
     203             :   {
     204      676374 :     GEN m = gel(L,j), invNx = invr(gel(m,j));
     205             :     long i;
     206      676374 :     gel(m,j) = gel(B,j);
     207      676374 :     for (i=j+1; i<=k; i++) gel(m,i) = mpmul(invNx, gel(m,i));
     208             :   }
     209      331768 :   gcoeff(L,j,j) = gel(B,j);
     210      331768 :   return shallowtrans(L);
     211             : }
     212             : GEN
     213         363 : R_from_QR(GEN x, long prec)
     214             : {
     215             :   GEN B, Q, L;
     216         363 :   if (!QR_init(x, &B,&Q,&L, prec)) return NULL;
     217         351 :   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         322 : RgM_gram_schmidt(GEN e, GEN *ptB)
     228             : {
     229         322 :   long i,j,lx = lg(e);
     230         322 :   GEN f = RgM_shallowcopy(e), B, iB;
     231             : 
     232         322 :   B = cgetg(lx, t_VEC);
     233         322 :   iB= cgetg(lx, t_VEC);
     234             : 
     235         994 :   for (i=1;i<lx;i++)
     236             :   {
     237         672 :     GEN p1 = NULL;
     238         672 :     pari_sp av = avma;
     239        1554 :     for (j=1; j<i; j++)
     240             :     {
     241         882 :       GEN mu = gmul(RgV_dotproduct(gel(e,i),gel(f,j)), gel(iB,j));
     242         882 :       GEN p2 = gmul(mu, gel(f,j));
     243         882 :       p1 = p1? gadd(p1,p2): p2;
     244             :     }
     245         672 :     p1 = p1? gerepileupto(av, gsub(gel(e,i), p1)): gel(e,i);
     246         672 :     gel(f,i) = p1;
     247         672 :     gel(B,i) = RgV_dotsquare(gel(f,i));
     248         672 :     gel(iB,i) = ginv(gel(B,i));
     249             :   }
     250         322 :   *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         322 : RgM_Babai(GEN B, GEN t)
     257             : {
     258         322 :   GEN C, N, G = RgM_gram_schmidt(B, &N), b = t;
     259         322 :   long j, n = lg(B)-1;
     260             : 
     261         322 :   C = cgetg(n+1,t_COL);
     262         994 :   for (j = n; j > 0; j--)
     263             :   {
     264         672 :     GEN c = gdiv( RgV_dotproduct(b, gel(G,j)), gel(N,j) );
     265             :     long e;
     266         672 :     c = grndtoi(c,&e);
     267         672 :     if (e >= 0) return NULL;
     268         672 :     if (signe(c)) b = RgC_sub(b, RgC_Rg_mul(gel(G,j), c));
     269         672 :     gel(C,j) = c;
     270             :   }
     271         322 :   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          35 : lllintpartialall(GEN m, long flag)
     303             : {
     304          35 :   const long ncol = lg(m)-1;
     305          35 :   const pari_sp av = avma;
     306             :   GEN tm1, tm2, mid;
     307             : 
     308          35 :   if (ncol <= 1) return flag? matid(ncol): gcopy(m);
     309             : 
     310          14 :   tm1 = flag? matid(ncol): NULL;
     311             :   {
     312          14 :     const pari_sp av2 = avma;
     313          14 :     GEN dot11 = ZV_dotsquare(gel(m,1));
     314          14 :     GEN dot22 = ZV_dotsquare(gel(m,2));
     315          14 :     GEN dot12 = ZV_dotproduct(gel(m,1), gel(m,2));
     316          14 :     GEN tm  = matid(2); /* For first two columns only */
     317             : 
     318          14 :     int progress = 0;
     319          14 :     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          63 :     while (npass2 < 2 || progress)
     327             :     {
     328          42 :       GEN dot12new, q = diviiround(dot12, dot22);
     329             : 
     330          35 :       npass2++; progress = signe(q);
     331          35 :       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          21 :         togglesign(q);
     336          21 :         dot12new = addii(dot12, mulii(q, dot22));
     337          21 :         dot11 = addii(dot11, mulii(q, addii(dot12, dot12new)));
     338          21 :         dot12 = dot12new;
     339          21 :         ZC_lincomb1_inplace(gel(tm,1), gel(tm,2), q);
     340             :       }
     341             : 
     342             :       /* Interchange the output vectors v1 and v2.  */
     343          35 :       swap(dot11,dot22);
     344          35 :       swap(gel(tm,1), gel(tm,2));
     345             : 
     346             :       /* Occasionally (including final pass) do garbage collection.  */
     347          35 :       if ((npass2 & 0xff) == 0 || !progress)
     348          14 :         gerepileall(av2, 4, &dot11,&dot12,&dot22,&tm);
     349             :     } /* while npass2 < 2 || progress */
     350             : 
     351             :     {
     352             :       long i;
     353           7 :       GEN det12 = subii(mulii(dot11, dot22), sqri(dot12));
     354             : 
     355           7 :       mid = cgetg(ncol+1, t_MAT);
     356          21 :       for (i = 1; i <= 2; i++)
     357             :       {
     358          14 :         GEN tmi = gel(tm,i);
     359          14 :         if (tm1)
     360             :         {
     361          14 :           GEN tm1i = gel(tm1,i);
     362          14 :           gel(tm1i,1) = gel(tmi,1);
     363          14 :           gel(tm1i,2) = gel(tmi,2);
     364             :         }
     365          14 :         gel(mid,i) = ZC_lincomb(gel(tmi,1),gel(tmi,2), gel(m,1),gel(m,2));
     366             :       }
     367          42 :       for (i = 3; i <= ncol; i++)
     368             :       {
     369          35 :         GEN c = gel(m,i);
     370          35 :         GEN dot1i = ZV_dotproduct(gel(mid,1), c);
     371          35 :         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          35 :         GEN q1neg = subii(mulii(dot12, dot2i), mulii(dot22, dot1i));
     380          35 :         GEN q2neg = subii(mulii(dot12, dot1i), mulii(dot11, dot2i));
     381             : 
     382          35 :         q1neg = diviiround(q1neg, det12);
     383          35 :         q2neg = diviiround(q2neg, det12);
     384          35 :         if (tm1)
     385             :         {
     386          70 :           gcoeff(tm1,1,i) = addii(mulii(q1neg, gcoeff(tm,1,1)),
     387          35 :                                   mulii(q2neg, gcoeff(tm,1,2)));
     388          70 :           gcoeff(tm1,2,i) = addii(mulii(q1neg, gcoeff(tm,2,1)),
     389          35 :                                   mulii(q2neg, gcoeff(tm,2,2)));
     390             :         }
     391          35 :         gel(mid,i) = ZC_add(c, ZC_lincomb(q1neg,q2neg, gel(mid,1),gel(mid,2)));
     392             :       } /* for i */
     393             :     } /* local block */
     394             :   }
     395           7 :   if (DEBUGLEVEL>6)
     396             :   {
     397           0 :     if (tm1) err_printf("tm1 = %Ps",tm1);
     398           0 :     err_printf("mid = %Ps",mid); /* = m * tm1 */
     399             :   }
     400           7 :   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           7 :     const pari_sp av3 = avma;
     406           7 :     long i, j, npass = 0, e = LONG_MAX;
     407           7 :     GEN dot = cgetg(ncol+1, t_MAT); /* scalar products */
     408             : 
     409           7 :     tm2 = matid(ncol);
     410          56 :     for (i=1; i <= ncol; i++)
     411             :     {
     412          49 :       gel(dot,i) = cgetg(ncol+1,t_COL);
     413         245 :       for (j=1; j <= i; j++)
     414         196 :         gcoeff(dot,j,i) = gcoeff(dot,i,j) = ZV_dotproduct(gel(mid,i),gel(mid,j));
     415             :     }
     416             :     for(;;)
     417             :     {
     418          42 :       long reductions = 0, olde = e;
     419         336 :       for (i=1; i <= ncol; i++)
     420             :       {
     421             :         long ijdif;
     422        2058 :         for (ijdif=1; ijdif < ncol; ijdif++)
     423             :         {
     424             :           long d, k1, k2;
     425             :           GEN codi, q;
     426             : 
     427        1764 :           j = i + ijdif; if (j > ncol) j -= ncol;
     428             :           /* let k1, resp. k2,  index of larger, resp. smaller, column */
     429        1764 :           if (cmpii(gcoeff(dot,i,i), gcoeff(dot,j,j)) > 0) { k1 = i; k2 = j; }
     430        1022 :           else                                             { k1 = j; k2 = i; }
     431        1764 :           codi = gcoeff(dot,k2,k2);
     432        1764 :           q = signe(codi)? diviiround(gcoeff(dot,k1,k2), codi): gen_0;
     433        1764 :           if (!signe(q)) continue;
     434             : 
     435             :           /* Try to subtract a multiple of column k2 from column k1.  */
     436         700 :           reductions++; togglesign_safe(&q);
     437         700 :           ZC_lincomb1_inplace(gel(tm2,k1), gel(tm2,k2), q);
     438         700 :           ZC_lincomb1_inplace(gel(dot,k1), gel(dot,k2), q);
     439        1400 :           gcoeff(dot,k1,k1) = addii(gcoeff(dot,k1,k1),
     440         700 :                                     mulii(q, gcoeff(dot,k2,k1)));
     441         700 :           for (d = 1; d <= ncol; d++) gcoeff(dot,k1,d) = gcoeff(dot,d,k1);
     442             :         } /* for ijdif */
     443         294 :         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          42 :       if (!reductions) break;
     450          35 :       e = 0;
     451          35 :       for (i = 1; i <= ncol; i++) e += expi( gcoeff(dot,i,i) );
     452          35 :       if (e == olde) break;
     453          35 :       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          35 :     } /* for(;;)*/
     460             : 
     461             :    /* Sort columns so smallest comes first in m * tm1 * tm2.
     462             :     * Use insertion sort. */
     463          49 :     for (i = 1; i < ncol; i++)
     464             :     {
     465          42 :       long j, s = i;
     466             : 
     467         189 :       for (j = i+1; j <= ncol; j++)
     468         147 :         if (cmpii(gcoeff(dot,s,s),gcoeff(dot,j,j)) > 0) s = j;
     469          42 :       if (i != s)
     470             :       { /* Exchange with proper column; only the diagonal of dot is updated */
     471          28 :         swap(gel(tm2,i), gel(tm2,s));
     472          28 :         swap(gcoeff(dot,i,i), gcoeff(dot,s,s));
     473             :       }
     474             :     }
     475             :   } /* local block */
     476           7 :   return gerepileupto(av, ZM_mul(tm1? tm1: mid, tm2));
     477             : }
     478             : 
     479             : GEN
     480          35 : 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         882 : check_condition(double beta, double tau, double rho, long d, long delta, long t)
     494             : {
     495         882 :   long dim = d*delta + t;
     496        1764 :   double cond = d*delta*(delta+1)/2 - beta*delta*dim
     497         882 :     + rho*delta*(delta - 1) / 2
     498         882 :     + rho * t * delta + tau*dim*(dim - 1)/2;
     499             : 
     500         882 :   if (DEBUGLEVEL >= 4)
     501           0 :     err_printf("delta = %d, t = %d, cond = %.1lf\n", delta, t, cond);
     502             : 
     503         882 :   return (cond <= 0);
     504             : }
     505             : 
     506             : static void
     507          14 : choose_params(GEN P, GEN N, GEN X, GEN B, long *pdelta, long *pt)
     508             : {
     509          14 :   long d = degpol(P);
     510          14 :   GEN P0 = leading_coeff(P);
     511          14 :   double logN = gtodouble(glog(N, DEFAULTPREC));
     512             :   double tau, beta, rho;
     513             :   long delta, t;
     514          14 :   tau = gtodouble(glog(X, DEFAULTPREC)) / logN;
     515          14 :   beta = B? gtodouble(glog(B, DEFAULTPREC)) / logN: 1.;
     516          14 :   if (tau >= beta * beta / d)
     517           0 :     pari_err_OVERFLOW("zncoppersmith [bound too large]");
     518             :   /* TODO : remove P0 completely ! */
     519          14 :   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          14 :   t = d; delta = 0;
     524             :   for(;;)
     525             :   {
     526         175 :     t += d * delta + 1; delta = 0;
     527        1218 :     while (t >= 0) {
     528         882 :       if (check_condition(beta, tau, rho, d, delta, t)) {
     529          28 :         *pdelta = delta; *pt = t; return;
     530             :       }
     531         868 :       delta++; t -= d;
     532             :     }
     533         161 :   }
     534             : }
     535             : 
     536             : static int
     537       14021 : sol_OK(GEN x, GEN N, GEN B)
     538       14021 : { 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           7 : do_exhaustive(GEN P, GEN N, long x, GEN B)
     542             : {
     543           7 :   GEN Pe, Po, sol = vecsmalltrunc_init(2*x + 2);
     544             :   pari_sp av;
     545             :   long j;
     546           7 :   RgX_even_odd(P, &Pe,&Po); av = avma;
     547           7 :   if (sol_OK(gel(P,2), N,B)) vecsmalltrunc_append(sol, 0);
     548        7007 :   for (j = 1; j <= x; j++, avma = av)
     549             :   {
     550        7000 :     GEN j2 = sqru(j), E = FpX_eval(Pe,j2,N), O = FpX_eval(Po,j2,N);
     551        7000 :     if (sol_OK(addmuliu(E,O,j), N,B)) vecsmalltrunc_append(sol, j);
     552        7000 :     if (sol_OK(submuliu(E,O,j), N,B)) vecsmalltrunc_append(sol,-j);
     553             :   }
     554           7 :   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          21 : 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          21 :   long delta, i, j, row, d, l, dim, t, bnd = 10;
     564          21 :   const ulong X_SMALL = 1000;
     565             : 
     566          21 :   pari_sp av = avma;
     567             : 
     568          21 :   if (typ(P0) != t_POL) pari_err_TYPE("zncoppersmith",P0);
     569          21 :   if (typ(N) != t_INT) pari_err_TYPE("zncoppersmith",N);
     570          21 :   if (typ(X) != t_INT) {
     571           7 :     X = gfloor(X);
     572           7 :     if (typ(X) != t_INT) pari_err_TYPE("zncoppersmith",X);
     573             :   }
     574          21 :   if (signe(X) < 0) pari_err_DOMAIN("zncoppersmith", "X", "<", gen_0, X);
     575          21 :   d = degpol(P0);
     576          21 :   if (d == 0) { avma = av; return cgetg(1, t_VEC); }
     577          21 :   if (d < 0) pari_err_ROOTS0("zncoppersmith");
     578          21 :   if (B && typ(B) != t_INT) B = gceil(B);
     579             : 
     580          21 :   if (abscmpiu(X, X_SMALL) <= 0)
     581           7 :     return gerepileupto(av, do_exhaustive(P0, N, itos(X), B));
     582             : 
     583          14 :   if (B && equalii(B,N)) B = NULL;
     584          14 :   if (B) bnd = 1; /* bnd-hack is only for the case B = N */
     585          14 :   P = leafcopy(P0);
     586          14 :   if (!gequal1(gel(P,d+2)))
     587             :   {
     588             :     GEN r, z;
     589           7 :     gel(P,d+2) = bezout(gel(P,d+2), N, &z, &r);
     590           7 :     for (j = 0; j < d; j++) gel(P,j+2) = modii(mulii(gel(P,j+2), z), N);
     591             :   }
     592          14 :   if (DEBUGLEVEL >= 2) err_printf("Modified P: %Ps\n", P);
     593             : 
     594          14 :   choose_params(P, N, X, B, &delta, &t);
     595          14 :   if (DEBUGLEVEL >= 2)
     596           0 :     err_printf("Init: trying delta = %d, t = %d\n", delta, t);
     597             :   for(;;)
     598             :   {
     599          14 :     dim = d * delta + t;
     600             : 
     601             :     /* TODO: In case of failure do not recompute the full vector */
     602          14 :     Xpowers = (GEN*)new_chunk(dim + 1);
     603          14 :     Xpowers[0] = gen_1;
     604          14 :     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          14 :     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          14 :     for (j = 1; j <= d;   j++) gcoeff(M, j, j) = gel(Xpowers,j-1);
     615             : 
     616             :     /* P-part */
     617          14 :     if (delta) row = d + 1; else row = 0;
     618             : 
     619          14 :     Q = P;
     620          70 :     for (i = 1; i < delta; i++)
     621             :     {
     622         182 :       for (j = 0; j < d; j++,row++)
     623        1239 :         for (l = j + 1; l <= row; l++)
     624        1113 :           gcoeff(M, l, row) = mulii(Xpowers[l-1], gel(Q,l-j+1));
     625          56 :       Q = ZX_mul(Q, P);
     626             :     }
     627          63 :     for (j = 0; j < t; row++, j++)
     628         490 :       for (l = j + 1; l <= row; l++)
     629         441 :         gcoeff(M, l, row) = mulii(Xpowers[l-1], gel(Q,l-j+1));
     630             : 
     631             :     /* N-part */
     632          14 :     row = dim - t; N0 = N;
     633          98 :     while (row >= 1)
     634             :     {
     635         224 :       for (j = 0; j < d; j++,row--)
     636        1421 :         for (l = 1; l <= row; l++)
     637        1267 :           gcoeff(M, l, row) = mulii(gmael(M, row, l), N0);
     638          70 :       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          14 :     if (B) Z = powiu(B, delta); else Z = N0;
     643             : 
     644          14 :     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          14 :     sh = ZM_lll(M, 0.75, LLL_INPLACE);
     652             :     /* Take the first vector if it is non constant */
     653          14 :     short_pol = gel(sh,1);
     654          14 :     if (ZV_isscalar(short_pol)) short_pol = gel(sh, 2);
     655             : 
     656          14 :     nsp = gen_0;
     657          14 :     for (j = 1; j <= dim; j++) nsp = addii(nsp, absi(gel(short_pol,j)));
     658             : 
     659          14 :     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          14 :     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          14 :   bnd = itos(divii(nsp, Z)) + 1;
     673             : 
     674          14 :   while (!signe(gel(short_pol,dim))) dim--;
     675             : 
     676          14 :   R = cgetg(dim + 2, t_POL); R[1] = P[1];
     677         217 :   for (j = 1; j <= dim; j++)
     678         203 :     gel(R,j+1) = diviiexact(gel(short_pol,j), Xpowers[j-1]);
     679          14 :   gel(R,2) = subii(gel(R,2), mului(bnd - 1, N0));
     680             : 
     681          14 :   sol = cgetg(1, t_VEC);
     682         112 :   for (i = -bnd + 1; i < bnd; i++)
     683             :   {
     684          98 :     GEN r = nfrootsQ(R);
     685          98 :     if (DEBUGLEVEL >= 2) err_printf("Roots: %Ps\n", r);
     686         119 :     for (j = 1; j < lg(r); j++)
     687             :     {
     688          21 :       GEN z = gel(r,j);
     689          21 :       if (typ(z) == t_INT && sol_OK(FpX_eval(P,z,N), N,B))
     690          14 :         sol = shallowconcat(sol, z);
     691             :     }
     692          98 :     if (i < bnd) gel(R,2) = addii(gel(R,2), Z);
     693             :   }
     694          14 :   return gerepileupto(av, ZV_sort_uniq(sol));
     695             : }
     696             : 
     697             : /********************************************************************/
     698             : /**                                                                **/
     699             : /**                   LINEAR & ALGEBRAIC DEPENDENCE                **/
     700             : /**                                                                **/
     701             : /********************************************************************/
     702             : 
     703             : static int
     704         634 : real_indep(GEN re, GEN im, long bitprec)
     705             : {
     706        1268 :   GEN p1 = gsub(gmul(gel(re,1),gel(im,2)),
     707        1268 :                 gmul(gel(re,2),gel(im,1)));
     708         634 :   return (!gequal0(p1) && gexpo(p1) > - bitprec);
     709             : }
     710             : 
     711             : GEN
     712        2852 : lindep2(GEN x, long bit)
     713             : {
     714        2852 :   long tx=typ(x), lx=lg(x), ly, i, j;
     715        2852 :   pari_sp av = avma;
     716             :   GEN re, im, M;
     717             : 
     718        2852 :   if (! is_vec_t(tx)) pari_err_TYPE("lindep2",x);
     719        2852 :   if (lx<=2)
     720             :   {
     721          21 :     if (lx == 2 && gequal0(x)) return mkcol(gen_1);
     722          14 :     return cgetg(1,t_COL);
     723             :   }
     724        2831 :   if (bit < 0) pari_err_DOMAIN("lindep2", "accuracy", "<", gen_0, stoi(bit));
     725        2831 :   if (!bit)
     726             :   {
     727         697 :     bit = gprecision(x);
     728         697 :     if (!bit)
     729             :     {
     730          14 :       x = primpart(x);
     731          14 :       bit = 32 + gexpo(x);
     732             :     }
     733             :     else
     734         683 :       bit = (long)prec2nbits_mul(bit, 0.8);
     735             :   }
     736             :   else
     737        2134 :     bit = (long) (bit/LOG10_2);
     738        2831 :   re = real_i(x);
     739        2831 :   im = imag_i(x);
     740             :   /* independent over R ? */
     741        2831 :   if (lx == 3 && real_indep(re,im,bit)) { avma = av; return cgetg(1, t_COL); }
     742        2824 :   if (gequal0(im)) im = NULL;
     743        2824 :   ly = im? lx+2: lx+1;
     744        2824 :   M = cgetg(lx,t_MAT);
     745       11446 :   for (i=1; i<lx; i++)
     746             :   {
     747        8622 :     GEN c = cgetg(ly,t_COL); gel(M,i) = c;
     748        8622 :     for (j=1; j<lx; j++) gel(c,j) = (i==j)? gen_1: gen_0;
     749        8622 :     gel(c,lx)           = gtrunc2n(gel(re,i), bit);
     750        8622 :     if (im) gel(c,lx+1) = gtrunc2n(gel(im,i), bit);
     751             :   }
     752        2824 :   M = ZM_lll(M, 0.99, LLL_INPLACE);
     753        2824 :   M = gel(M,1);
     754        2824 :   M[0] = evaltyp(t_COL) | evallg(lx);
     755        2824 :   return gerepilecopy(av, M);
     756             : }
     757             : 
     758             : void
     759      422786 : init_dalloc(void)
     760             : { /* correct alignment for dalloc */
     761      422786 :   (void)new_chunk((avma % sizeof(double)) / sizeof(long));
     762      422786 : }
     763             : 
     764             : double *
     765      149637 : dalloc(size_t n)
     766             : {
     767      149637 :   return (double*)new_chunk(n / sizeof(long));
     768             : }
     769             : 
     770             : /* x is a vector of elts of a p-adic field */
     771             : GEN
     772          14 : padic_lindep(GEN x)
     773             : {
     774          14 :   long i, j, prec = LONG_MAX, nx = lg(x)-1, v;
     775          14 :   pari_sp av = avma;
     776          14 :   GEN p = NULL, pn, m, a;
     777             : 
     778          14 :   if (nx < 2) return cgetg(1,t_COL);
     779          49 :   for (i=1; i<=nx; i++)
     780             :   {
     781          35 :     GEN c = gel(x,i), q;
     782          35 :     if (typ(c) != t_PADIC) continue;
     783             : 
     784          21 :     j = precp(c); if (j < prec) prec = j;
     785          21 :     q = gel(c,2);
     786          21 :     if (!p) p = q; else if (!equalii(p, q)) pari_err_MODULUS("padic_lindep", p, q);
     787             :   }
     788          14 :   if (!p) pari_err_TYPE("padic_lindep [not a p-adic vector]",x);
     789          14 :   v = gvaluation(x,p); pn = powiu(p,prec);
     790          14 :   if (v) x = gmul(x, powis(p, -v));
     791          14 :   x = RgV_to_FpV(x, pn);
     792             : 
     793          14 :   a = negi(gel(x,1));
     794          14 :   m = cgetg(nx,t_MAT);
     795          35 :   for (i=1; i<nx; i++)
     796             :   {
     797          21 :     GEN c = zerocol(nx);
     798          21 :     gel(c,1+i) = a;
     799          21 :     gel(c,1) = gel(x,i+1);
     800          21 :     gel(m,i) = c;
     801             :   }
     802          14 :   m = ZM_lll(ZM_hnfmodid(m, pn), 0.99, LLL_INPLACE);
     803          14 :   return gerepilecopy(av, gel(m,1));
     804             : }
     805             : /* x is a vector of t_POL/t_SER */
     806             : GEN
     807          42 : Xadic_lindep(GEN x)
     808             : {
     809          42 :   long i, prec = LONG_MAX, deg = 0, lx = lg(x), vx, v;
     810          42 :   pari_sp av = avma;
     811             :   GEN m;
     812             : 
     813          42 :   if (lx == 1) return cgetg(1,t_COL);
     814          42 :   vx = gvar(x);
     815          42 :   v = gvaluation(x, pol_x(vx));
     816          42 :   if (!v)         x = shallowcopy(x);
     817           0 :   else if (v > 0) x = gdiv(x, pol_xn(v, vx));
     818           0 :   else            x = gmul(x, pol_xn(-v, vx));
     819             :   /* all t_SER have valuation >= 0 */
     820         308 :   for (i=1; i<lx; i++)
     821             :   {
     822         266 :     GEN c = gel(x,i);
     823         266 :     if (gvar(c) != vx) { gel(x,i) = scalarpol_shallow(c, vx); continue; }
     824         259 :     switch(typ(c))
     825             :     {
     826         126 :       case t_POL: deg = maxss(deg, degpol(c)); break;
     827           0 :       case t_RFRAC: pari_err_TYPE("Xadic_lindep", c);
     828             :       case t_SER:
     829         133 :         prec = minss(prec, valp(c)+lg(c)-2);
     830         133 :         gel(x,i) = ser2rfrac_i(c);
     831             :     }
     832             :   }
     833          42 :   if (prec == LONG_MAX) prec = deg+1;
     834          42 :   m = RgXV_to_RgM(x, prec);
     835          42 :   return gerepileupto(av, deplin(m));
     836             : }
     837             : 
     838             : GEN
     839         627 : lindep(GEN x) { return lindep2(x, 0); }
     840             : 
     841             : GEN
     842         217 : lindep0(GEN x,long bit)
     843             : {
     844         217 :   long i, tx = typ(x);
     845         217 :   if (tx == t_MAT) return deplin(x);
     846          98 :   if (! is_vec_t(tx)) pari_err_TYPE("lindep",x);
     847         364 :   for (i = 1; i < lg(x); i++)
     848         287 :     switch(typ(gel(x,i)))
     849             :     {
     850           7 :       case t_PADIC: return padic_lindep(x);
     851             :       case t_POL:
     852             :       case t_RFRAC:
     853          14 :       case t_SER: return Xadic_lindep(x);
     854             :     }
     855          77 :   return lindep2(x, bit);
     856             : }
     857             : 
     858             : GEN
     859          49 : algdep0(GEN x, long n, long bit)
     860             : {
     861          49 :   long tx = typ(x), i;
     862             :   pari_sp av;
     863             :   GEN y;
     864             : 
     865          49 :   if (! is_scalar_t(tx)) pari_err_TYPE("algdep0",x);
     866          49 :   if (tx==t_POLMOD) { y = RgX_copy(gel(x,1)); setvarn(y,0); return y; }
     867          49 :   if (gequal0(x)) return pol_x(0);
     868          49 :   if (n <= 0)
     869             :   {
     870          14 :     if (!n) return gen_1;
     871           7 :     pari_err_DOMAIN("algdep", "degree", "<", gen_0, stoi(n));
     872             :   }
     873             : 
     874          35 :   av = avma; y = cgetg(n+2,t_COL);
     875          35 :   gel(y,1) = gen_1;
     876          35 :   gel(y,2) = x; /* n >= 1 */
     877          35 :   for (i=3; i<=n+1; i++) gel(y,i) = gmul(gel(y,i-1),x);
     878          35 :   if (typ(x) == t_PADIC)
     879           7 :     y = padic_lindep(y);
     880             :   else
     881          28 :     y = lindep2(y, bit);
     882          35 :   if (lg(y) == 1) pari_err(e_DOMAIN,"algdep", "degree(x)",">", stoi(n), x);
     883          35 :   y = RgV_to_RgX(y, 0);
     884          35 :   if (signe(leading_coeff(y)) > 0) return gerepilecopy(av, y);
     885           0 :   return gerepileupto(av, ZX_neg(y));
     886             : }
     887             : 
     888             : GEN
     889           0 : algdep(GEN x, long n)
     890             : {
     891           0 :   return algdep0(x,n,0);
     892             : }
     893             : 
     894             : GEN
     895          28 : seralgdep(GEN s, long p, long r)
     896             : {
     897          28 :   pari_sp av = avma;
     898             :   long vy, i, m, n, prec;
     899             :   GEN S, v, D;
     900             : 
     901          28 :   if (typ(s) != t_SER) pari_err_TYPE("seralgdep",s);
     902          28 :   if (p <= 0) pari_err_DOMAIN("seralgdep", "p", "<=", gen_0, stoi(p));
     903          28 :   if (r < 0) pari_err_DOMAIN("seralgdep", "r", "<", gen_0, stoi(r));
     904          28 :   if (is_bigint(addiu(muluu(p, r), 1))) pari_err_OVERFLOW("seralgdep");
     905          28 :   vy = varn(s);
     906          28 :   if (!vy) pari_err_PRIORITY("seralgdep", s, ">", 0);
     907          28 :   r++; p++;
     908          28 :   prec = valp(s) + lg(s)-2;
     909          28 :   if (r > prec) r = prec;
     910          28 :   S = cgetg(p+1, t_VEC); gel(S, 1) = s;
     911          28 :   for (i = 2; i <= p; i++) gel(S,i) = gmul(gel(S,i-1), s);
     912          28 :   v = cgetg(r*p+1, t_VEC); /* v[r*n+m+1] = s^n * y^m */
     913             :   /* n = 0 */
     914          28 :   for (m = 0; m < r; m++) gel(v, m+1) = pol_xn(m, vy);
     915          70 :   for(n=1; n < p; n++)
     916         175 :     for (m = 0; m < r; m++)
     917             :     {
     918         133 :       GEN c = gel(S,n);
     919         133 :       if (m)
     920             :       {
     921          91 :         c = shallowcopy(c);
     922          91 :         setvalp(c, valp(c) + m);
     923             :       }
     924         133 :       gel(v, r*n + m + 1) = c;
     925             :     }
     926          28 :   D = Xadic_lindep(v);
     927          28 :   if (lg(D) == 1) { avma = av; return gen_0; }
     928          21 :   v = cgetg(p+1, t_VEC);
     929          70 :   for (n = 0; n < p; n++)
     930          49 :     gel(v, n+1) = RgV_to_RgX(vecslice(D, r*n+1, r*n+r), vy);
     931          21 :   return gerepilecopy(av, RgV_to_RgX(v, 0));
     932             : }
     933             : 
     934             : /********************************************************************/
     935             : /**                                                                **/
     936             : /**                              MINIM                             **/
     937             : /**                                                                **/
     938             : /********************************************************************/
     939             : void
     940       18756 : minim_alloc(long n, double ***q, GEN *x, double **y,  double **z, double **v)
     941             : {
     942             :   long i, s;
     943             : 
     944       18756 :   *x = cgetg(n, t_VECSMALL);
     945       18756 :   *q = (double**) new_chunk(n);
     946       18756 :   s = n * sizeof(double);
     947       18756 :   init_dalloc();
     948       18756 :   *y = dalloc(s);
     949       18756 :   *z = dalloc(s);
     950       18756 :   *v = dalloc(s);
     951       18756 :   for (i=1; i<n; i++) (*q)[i] = dalloc(s);
     952       18756 : }
     953             : 
     954             : static GEN
     955      202629 : ZC_canon(GEN V)
     956             : {
     957      202629 :   long l = lg(V), j;
     958      202629 :   for (j = 1; j < l  &&  signe(gel(V,j)) == 0; ++j);
     959      202629 :   return (j < l  &&  signe(gel(V,j)) < 0)? ZC_neg(V): V;
     960             : }
     961             : 
     962             : static GEN
     963      202629 : ZM_zc_mul_canon(GEN u, GEN x)
     964             : {
     965      202629 :   return ZC_canon(ZM_zc_mul(u,x));
     966             : }
     967             : 
     968             : struct qfvec
     969             : {
     970             :   GEN a, r, u;
     971             : };
     972             : 
     973             : static void
     974           0 : err_minim(GEN a)
     975             : {
     976           0 :   pari_err_DOMAIN("minim0","form","is not",
     977             :                   strtoGENstr("positive definite"),a);
     978           0 : }
     979             : 
     980             : static GEN
     981         559 : minim_lll(GEN a, GEN *u)
     982             : {
     983         559 :   *u = lllgramint(a);
     984         559 :   if (lg(*u) != lg(a)) err_minim(a);
     985         559 :   return qf_apply_ZM(a,*u);
     986             : }
     987             : 
     988             : static void
     989         559 : forqfvec_init_dolll(struct qfvec *qv, GEN a, long dolll)
     990             : {
     991             :   GEN r, u;
     992         559 :   if (!dolll) u = NULL;
     993             :   else
     994             :   {
     995         517 :     if (typ(a) != t_MAT || !RgM_is_ZM(a)) pari_err_TYPE("qfminim",a);
     996         517 :     a = minim_lll(a, &u);
     997             :   }
     998         559 :   qv->a = RgM_gtofp(a, DEFAULTPREC);
     999         559 :   r = qfgaussred_positive(qv->a);
    1000         559 :   if (!r)
    1001             :   {
    1002           0 :     r = qfgaussred_positive(a); /* exact computation */
    1003           0 :     if (!r) err_minim(a);
    1004           0 :     r = RgM_gtofp(r, DEFAULTPREC);
    1005             :   }
    1006         559 :   qv->r = r;
    1007         559 :   qv->u = u;
    1008         559 : }
    1009             : 
    1010             : static void
    1011          21 : forqfvec_init(struct qfvec *qv, GEN a)
    1012          21 : { forqfvec_init_dolll(qv, a, 1); }
    1013             : 
    1014             : static void
    1015          21 : forqfvec_i(void *E, long (*fun)(void *, GEN, GEN, double), struct qfvec *qv, GEN BORNE)
    1016             : {
    1017          21 :   GEN x, a = qv->a, r = qv->r, u = qv->u;
    1018          21 :   long n = lg(a), i, j, k;
    1019             :   double p,BOUND,*v,*y,*z,**q;
    1020          21 :   const double eps = 0.0001;
    1021          21 :   if (!BORNE) BORNE = gen_0;
    1022             :   else
    1023             :   {
    1024          14 :     BORNE = gfloor(BORNE);
    1025          14 :     if (typ(BORNE) != t_INT) pari_err_TYPE("minim0",BORNE);
    1026             :   }
    1027          42 :   if (n == 1) return;
    1028          14 :   minim_alloc(n, &q, &x, &y, &z, &v);
    1029          14 :   n--;
    1030          42 :   for (j=1; j<=n; j++)
    1031             :   {
    1032          28 :     v[j] = rtodbl(gcoeff(r,j,j));
    1033          28 :     for (i=1; i<j; i++) q[i][j] = rtodbl(gcoeff(r,i,j));
    1034             :   }
    1035             : 
    1036          14 :   if (gequal0(BORNE))
    1037             :   {
    1038             :     double c;
    1039           7 :     p = rtodbl(gcoeff(a,1,1));
    1040           7 :     for (i=2; i<=n; i++) { c = rtodbl(gcoeff(a,i,i)); if (c < p) p = c; }
    1041           7 :     BORNE = roundr(dbltor(p));
    1042             :   }
    1043             :   else
    1044           7 :     p = gtodouble(BORNE);
    1045          14 :   BOUND = p * (1 + eps);
    1046          14 :   if (BOUND == p) pari_err_PREC("minim0");
    1047             : 
    1048          14 :   k = n; y[n] = z[n] = 0;
    1049          14 :   x[n] = (long)sqrt(BOUND/v[n]);
    1050          28 :   for(;;x[1]--)
    1051             :   {
    1052             :     do
    1053             :     {
    1054          49 :       if (k>1)
    1055             :       {
    1056          21 :         long l = k-1;
    1057          21 :         z[l] = 0;
    1058          21 :         for (j=k; j<=n; j++) z[l] += q[l][j]*x[j];
    1059          21 :         p = (double)x[k] + z[k];
    1060          21 :         y[l] = y[k] + p*p*v[k];
    1061          21 :         x[l] = (long)floor(sqrt((BOUND-y[l])/v[l])-z[l]);
    1062          21 :         k = l;
    1063             :       }
    1064             :       for(;;)
    1065             :       {
    1066          56 :         p = (double)x[k] + z[k];
    1067          56 :         if (y[k] + p*p*v[k] <= BOUND) break;
    1068           7 :         k++; x[k]--;
    1069           7 :       }
    1070          49 :     } while (k > 1);
    1071          42 :     if (! x[1] && y[1]<=eps) break;
    1072             : 
    1073          28 :     p = (double)x[1] + z[1]; p = y[1] + p*p*v[1]; /* norm(x) */
    1074          28 :     if (fun(E, u, x, p)) break;
    1075          28 :   }
    1076             : }
    1077             : 
    1078             : void
    1079           0 : forqfvec(void *E, long (*fun)(void *, GEN, GEN, double), GEN a, GEN BORNE)
    1080             : {
    1081           0 :   pari_sp av = avma;
    1082             :   struct qfvec qv;
    1083           0 :   forqfvec_init(&qv, a);
    1084           0 :   forqfvec_i(E, fun, &qv, BORNE);
    1085           0 :   avma = av;
    1086           0 : }
    1087             : 
    1088             : static long
    1089          28 : _gp_forqf(void *E, GEN u, GEN x, double p/*unused*/)
    1090             : {
    1091          28 :   pari_sp av = avma;
    1092             :   (void)p;
    1093          28 :   set_lex(-1, ZM_zc_mul_canon(u, x));
    1094          28 :   closure_evalvoid((GEN)E);
    1095          28 :   avma = av;
    1096          28 :   return loop_break();
    1097             : }
    1098             : 
    1099             : void
    1100          21 : forqfvec0(GEN a, GEN BORNE, GEN code)
    1101             : {
    1102          21 :   pari_sp av = avma;
    1103             :   struct qfvec qv;
    1104          21 :   forqfvec_init(&qv, a);
    1105          21 :   push_lex(gen_0, code);
    1106          21 :   forqfvec_i((void*) code, &_gp_forqf, &qv, BORNE);
    1107          21 :   pop_lex(1);
    1108          21 :   avma = av;
    1109          21 : }
    1110             : 
    1111             : enum { min_ALL = 0, min_FIRST, min_VECSMALL, min_VECSMALL2 };
    1112             : 
    1113             : /* Minimal vectors for the integral definite quadratic form: a.
    1114             :  * Result u:
    1115             :  *   u[1]= Number of vectors of square norm <= BORNE
    1116             :  *   u[2]= maximum norm found
    1117             :  *   u[3]= list of vectors found (at most STOCKMAX, unless NULL)
    1118             :  *
    1119             :  *  If BORNE = NULL: Minimal non-zero vectors.
    1120             :  *  flag = min_ALL,   as above
    1121             :  *  flag = min_FIRST, exits when first suitable vector is found.
    1122             :  *  flag = min_VECSMALL, return a t_VECSMALL of (half) the number of vectors
    1123             :  *  for each norm
    1124             :  *  flag = min_VECSMALL2, same but count only vectors with even norm, and shift
    1125             :  *  the answer */
    1126             : static GEN
    1127         560 : minim0_dolll(GEN a, GEN BORNE, GEN STOCKMAX, long flag, long dolll)
    1128             : {
    1129             :   GEN x, u, r, L, gnorme;
    1130         560 :   long n = lg(a), i, j, k, s, maxrank, sBORNE;
    1131         560 :   pari_sp av = avma, av1;
    1132             :   double p,maxnorm,BOUND,*v,*y,*z,**q;
    1133         560 :   const double eps = 1e-10;
    1134         560 :   int stockall = 0;
    1135             :   struct qfvec qv;
    1136             : 
    1137         560 :   if (!BORNE)
    1138          56 :     sBORNE = 0;
    1139             :   else
    1140             :   {
    1141         504 :     BORNE = gfloor(BORNE);
    1142         504 :     if (typ(BORNE) != t_INT) pari_err_TYPE("minim0",BORNE);
    1143         504 :     if (is_bigint(BORNE)) pari_err_PREC( "qfminim");
    1144         503 :     sBORNE = itos(BORNE); avma = av;
    1145             :   }
    1146         559 :   if (!STOCKMAX)
    1147             :   {
    1148         237 :     stockall = 1;
    1149         237 :     maxrank = 200;
    1150             :   }
    1151             :   else
    1152             :   {
    1153         322 :     STOCKMAX = gfloor(STOCKMAX);
    1154         322 :     if (typ(STOCKMAX) != t_INT) pari_err_TYPE("minim0",STOCKMAX);
    1155         322 :     maxrank = itos(STOCKMAX);
    1156         322 :     if (maxrank < 0)
    1157           0 :       pari_err_TYPE("minim0 [negative number of vectors]",STOCKMAX);
    1158             :   }
    1159             : 
    1160         559 :   switch(flag)
    1161             :   {
    1162             :     case min_VECSMALL:
    1163             :     case min_VECSMALL2:
    1164         273 :       if (sBORNE <= 0) return cgetg(1, t_VECSMALL);
    1165         273 :       L = zero_zv(sBORNE);
    1166         273 :       if (flag == min_VECSMALL2) sBORNE <<= 1;
    1167         273 :       if (n == 1) return L;
    1168         273 :       break;
    1169             :     case min_FIRST:
    1170          35 :       if (n == 1 || (!sBORNE && BORNE)) return cgetg(1,t_VEC);
    1171          21 :       L = NULL; /* gcc -Wall */
    1172          21 :       break;
    1173             :     case min_ALL:
    1174         251 :       if (n == 1 || (!sBORNE && BORNE))
    1175           7 :         retmkvec3(gen_0, gen_0, cgetg(1, t_MAT));
    1176         244 :       L = new_chunk(1+maxrank);
    1177         244 :       break;
    1178             :     default:
    1179           0 :       return NULL;
    1180             :   }
    1181         538 :   minim_alloc(n, &q, &x, &y, &z, &v);
    1182             : 
    1183         538 :   forqfvec_init_dolll(&qv, a, dolll);
    1184         538 :   av1 = avma;
    1185         538 :   r = qv.r;
    1186         538 :   u = qv.u;
    1187         538 :   n--;
    1188        4715 :   for (j=1; j<=n; j++)
    1189             :   {
    1190        4177 :     v[j] = rtodbl(gcoeff(r,j,j));
    1191        4177 :     for (i=1; i<j; i++) q[i][j] = rtodbl(gcoeff(r,i,j)); /* |.| <= 1/2 */
    1192             :   }
    1193             : 
    1194         538 :   if (sBORNE) maxnorm = 0.;
    1195             :   else
    1196             :   {
    1197          56 :     GEN B = gcoeff(a,1,1);
    1198          56 :     long t = 1;
    1199         616 :     for (i=2; i<=n; i++)
    1200             :     {
    1201         560 :       GEN c = gcoeff(a,i,i);
    1202         560 :       if (cmpii(c, B) < 0) { B = c; t = i; }
    1203             :     }
    1204          56 :     if (flag == min_FIRST) return gerepilecopy(av, mkvec2(B, gel(u,t)));
    1205          49 :     maxnorm = -1.; /* don't update maxnorm */
    1206          49 :     if (is_bigint(B)) return NULL;
    1207          48 :     sBORNE = itos(B);
    1208             :   }
    1209         530 :   BOUND = sBORNE * (1 + eps);
    1210         530 :   if ((long)BOUND != sBORNE) return NULL;
    1211             : 
    1212         518 :   s = 0;
    1213         518 :   k = n; y[n] = z[n] = 0;
    1214         518 :   x[n] = (long)sqrt(BOUND/v[n]);
    1215     1030155 :   for(;;x[1]--)
    1216             :   {
    1217             :     do
    1218             :     {
    1219     1910727 :       if (k>1)
    1220             :       {
    1221      880565 :         long l = k-1;
    1222      880565 :         z[l] = 0;
    1223      880565 :         for (j=k; j<=n; j++) z[l] += q[l][j]*x[j];
    1224      880565 :         p = (double)x[k] + z[k];
    1225      880565 :         y[l] = y[k] + p*p*v[k];
    1226      880565 :         x[l] = (long)floor(sqrt((BOUND-y[l])/v[l])-z[l]);
    1227      880565 :         k = l;
    1228             :       }
    1229             :       for(;;)
    1230             :       {
    1231     2787841 :         p = (double)x[k] + z[k];
    1232     2787841 :         if (y[k] + p*p*v[k] <= BOUND) break;
    1233      877114 :         k++; x[k]--;
    1234      877114 :       }
    1235             :     }
    1236     1910727 :     while (k > 1);
    1237     1030673 :     if (! x[1] && y[1]<=eps) break;
    1238             : 
    1239     1030162 :     p = (double)x[1] + z[1]; p = y[1] + p*p*v[1]; /* norm(x) */
    1240     1030162 :     if (maxnorm >= 0)
    1241             :     {
    1242     1027614 :       if (p > maxnorm) maxnorm = p;
    1243             :     }
    1244             :     else
    1245             :     { /* maxnorm < 0 : only look for minimal vectors */
    1246        2548 :       pari_sp av2 = avma;
    1247        2548 :       gnorme = roundr(dbltor(p));
    1248        2548 :       if (cmpis(gnorme, sBORNE) >= 0) avma = av2;
    1249             :       else
    1250             :       {
    1251          14 :         sBORNE = itos(gnorme); avma = av1;
    1252          14 :         BOUND = sBORNE * (1+eps);
    1253          14 :         s = 0;
    1254             :       }
    1255             :     }
    1256     1030162 :     s++;
    1257             : 
    1258     1030162 :     switch(flag)
    1259             :     {
    1260             :       case min_FIRST:
    1261           7 :         if (dolll) x = ZM_zc_mul_canon(u,x);
    1262           7 :         return gerepilecopy(av, mkvec2(roundr(dbltor(p)), x));
    1263             : 
    1264             :       case min_ALL:
    1265      205030 :         if (s > maxrank && stockall) /* overflow */
    1266             :         {
    1267         406 :           long maxranknew = maxrank << 1;
    1268         406 :           GEN Lnew = new_chunk(1 + maxranknew);
    1269         406 :           for (i=1; i<=maxrank; i++) Lnew[i] = L[i];
    1270         406 :           L = Lnew; maxrank = maxranknew;
    1271             :         }
    1272      205030 :         if (s<=maxrank) gel(L,s) = leafcopy(x);
    1273      205030 :         break;
    1274             : 
    1275             :       case min_VECSMALL:
    1276       39200 :         { ulong norm = (ulong)(p + 0.5); L[norm]++; }
    1277       39200 :         break;
    1278             : 
    1279             :       case min_VECSMALL2:
    1280      785925 :         { ulong norm = (ulong)(p + 0.5); if (!odd(norm)) L[norm>>1]++; }
    1281      785925 :         break;
    1282             : 
    1283             :     }
    1284     1030155 :   }
    1285         511 :   switch(flag)
    1286             :   {
    1287             :     case min_FIRST:
    1288           7 :       avma = av; return cgetg(1,t_VEC);
    1289             :     case min_VECSMALL:
    1290             :     case min_VECSMALL2:
    1291         273 :       avma = (pari_sp)L; return L;
    1292             :   }
    1293         231 :   r = (maxnorm >= 0) ? roundr(dbltor(maxnorm)): stoi(sBORNE);
    1294         231 :   k = minss(s,maxrank);
    1295         231 :   L[0] = evaltyp(t_MAT) | evallg(k + 1);
    1296         231 :   if (dolll)
    1297         196 :     for (j=1; j<=k; j++) gel(L,j) = ZM_zc_mul_canon(u, gel(L,j));
    1298         231 :   return gerepilecopy(av, mkvec3(stoi(s<<1), r, L));
    1299             : }
    1300             : 
    1301             : static GEN
    1302         518 : minim0(GEN a, GEN BORNE, GEN STOCKMAX, long flag)
    1303             : {
    1304         518 :   GEN v = minim0_dolll(a, BORNE, STOCKMAX, flag, 1);
    1305         517 :   if (!v) pari_err_PREC("qfminim");
    1306         511 :   return v;
    1307             : }
    1308             : 
    1309             : GEN
    1310         273 : qfrep0(GEN a, GEN borne, long flag)
    1311         273 : { return minim0(a, borne, gen_0, (flag & 1)? min_VECSMALL2: min_VECSMALL); }
    1312             : 
    1313             : GEN
    1314         105 : qfminim0(GEN a, GEN borne, GEN stockmax, long flag, long prec)
    1315             : {
    1316         105 :   switch(flag)
    1317             :   {
    1318          42 :     case 0: return minim0(a,borne,stockmax,min_ALL);
    1319          35 :     case 1: return minim0(a,borne,gen_0   ,min_FIRST);
    1320             :     case 2:
    1321             :     {
    1322          28 :       long maxnum = -1;
    1323          28 :       if (typ(a) != t_MAT) pari_err_TYPE("qfminim",a);
    1324          28 :       if (stockmax) {
    1325          14 :         if (typ(stockmax) != t_INT) pari_err_TYPE("qfminim",stockmax);
    1326          14 :         maxnum = itos(stockmax);
    1327             :       }
    1328          28 :       a = fincke_pohst(a,borne,maxnum,prec,NULL);
    1329          28 :       if (!a) pari_err_PREC("qfminim");
    1330          28 :       return a;
    1331             :     }
    1332           0 :     default: pari_err_FLAG("qfminim");
    1333             :   }
    1334           0 :   return NULL; /* not reached */
    1335             : }
    1336             : 
    1337             : GEN
    1338         168 : minim(GEN a, GEN borne, GEN stockmax)
    1339         168 : { return minim0(a,borne,stockmax,min_ALL); }
    1340             : 
    1341             : GEN
    1342          42 : minim_raw(GEN a, GEN BORNE, GEN STOCKMAX)
    1343          42 : { return minim0_dolll(a, BORNE, STOCKMAX, min_ALL, 0); }
    1344             : 
    1345             : GEN
    1346           0 : minim2(GEN a, GEN borne, GEN stockmax)
    1347           0 : { return minim0(a,borne,stockmax,min_FIRST); }
    1348             : 
    1349             : /* If V depends linearly from the columns of the matrix, return 0.
    1350             :  * Otherwise, update INVP and L and return 1. No GC. */
    1351             : static int
    1352        1652 : addcolumntomatrix(GEN V, GEN invp, GEN L)
    1353             : {
    1354        1652 :   long i,j,k, n = lg(invp);
    1355        1652 :   GEN a = cgetg(n, t_COL), ak = NULL, mak;
    1356             : 
    1357       84231 :   for (k = 1; k < n; k++)
    1358       83706 :     if (!L[k])
    1359             :     {
    1360       27811 :       ak = RgMrow_zc_mul(invp, V, k);
    1361       27811 :       if (!gequal0(ak)) break;
    1362             :     }
    1363        1652 :   if (k == n) return 0;
    1364        1127 :   L[k] = 1;
    1365        1127 :   mak = gneg_i(ak);
    1366       43253 :   for (i=k+1; i<n; i++)
    1367       42126 :     gel(a,i) = gdiv(RgMrow_zc_mul(invp, V, i), mak);
    1368       43883 :   for (j=1; j<=k; j++)
    1369             :   {
    1370       42756 :     GEN c = gel(invp,j), ck = gel(c,k);
    1371       42756 :     if (gequal0(ck)) continue;
    1372        9471 :     gel(c,k) = gdiv(ck, ak);
    1373        9471 :     if (j==k)
    1374       43253 :       for (i=k+1; i<n; i++)
    1375       42126 :         gel(c,i) = gmul(gel(a,i), ck);
    1376             :     else
    1377      209979 :       for (i=k+1; i<n; i++)
    1378      201635 :         gel(c,i) = gadd(gel(c,i), gmul(gel(a,i), ck));
    1379             :   }
    1380        1127 :   return 1;
    1381             : }
    1382             : 
    1383             : GEN
    1384          42 : perf(GEN a)
    1385             : {
    1386          42 :   pari_sp av = avma;
    1387             :   GEN u, L;
    1388          42 :   long r, s, k, l, n = lg(a)-1;
    1389             : 
    1390          42 :   if (!n) return gen_0;
    1391          42 :   if (typ(a) != t_MAT || !RgM_is_ZM(a)) pari_err_TYPE("qfperfection",a);
    1392          42 :   a = minim_lll(a, &u);
    1393          42 :   L = minim_raw(a,NULL,NULL);
    1394          42 :   r = (n*(n+1)) >> 1;
    1395          42 :   if (L)
    1396             :   {
    1397             :     GEN D, V, invp;
    1398          35 :     L = gel(L, 3); l = lg(L);
    1399          35 :     if (l == 2) { avma = av; return gen_1; }
    1400             : 
    1401          21 :     D = zero_zv(r);
    1402          21 :     V = cgetg(r+1, t_VECSMALL);
    1403          21 :     invp = matid(r);
    1404          21 :     s = 0;
    1405        1659 :     for (k = 1; k < l; k++)
    1406             :     {
    1407        1652 :       pari_sp av2 = avma;
    1408        1652 :       GEN x = gel(L,k);
    1409             :       long i, j, I;
    1410       21098 :       for (i = I = 1; i<=n; i++)
    1411       19446 :         for (j=i; j<=n; j++,I++) V[I] = x[i]*x[j];
    1412        1652 :       if (!addcolumntomatrix(V,invp,D)) avma = av2;
    1413        1127 :       else if (++s == r) break;
    1414             :     }
    1415             :   }
    1416             :   else
    1417             :   {
    1418             :     GEN M;
    1419           7 :     L = fincke_pohst(a,NULL,-1, DEFAULTPREC, NULL);
    1420           7 :     if (!L) pari_err_PREC("qfminim");
    1421           7 :     L = gel(L, 3); l = lg(L);
    1422           7 :     if (l == 2) { avma = av; return gen_1; }
    1423           7 :     M = cgetg(l, t_MAT);
    1424         959 :     for (k = 1; k < l; k++)
    1425             :     {
    1426         952 :       GEN x = gel(L,k), c = cgetg(r+1, t_COL);
    1427             :       long i, I, j;
    1428       16184 :       for (i = I = 1; i<=n; i++)
    1429       15232 :         for (j=i; j<=n; j++,I++) gel(c,I) = mulii(gel(x,i), gel(x,j));
    1430         952 :       gel(M,k) = c;
    1431             :     }
    1432           7 :     s = ZM_rank(M);
    1433             :   }
    1434          28 :  avma = av; return utoipos(s);
    1435             : }
    1436             : 
    1437             : static GEN
    1438          64 : clonefill(GEN S, long s, long t)
    1439             : { /* initialize to dummy values */
    1440          64 :   GEN T = S, dummy = cgetg(1, t_STR);
    1441             :   long i;
    1442          64 :   for (i = s+1; i <= t; i++) gel(S,i) = dummy;
    1443          64 :   S = gclone(S); if (isclone(T)) gunclone(T);
    1444          64 :   return S;
    1445             : }
    1446             : 
    1447             : /* increment ZV x, by incrementing cell of index k. Initial value x0[k] was
    1448             :  * chosen to minimize qf(x) for given x0[1..k-1] and x0[k+1,..] = 0
    1449             :  * The last non-zero entry must be positive and goes through x0[k]+1,2,3,...
    1450             :  * Others entries go through: x0[k]+1,-1,2,-2,...*/
    1451             : INLINE void
    1452      915619 : step(GEN x, GEN y, GEN inc, long k)
    1453             : {
    1454      915619 :   if (!signe(gel(y,k))) /* x[k+1..] = 0 */
    1455        9331 :     gel(x,k) = addiu(gel(x,k), 1); /* leading coeff > 0 */
    1456             :   else
    1457             :   {
    1458      906288 :     long i = inc[k];
    1459      906288 :     gel(x,k) = addis(gel(x,k), i),
    1460      906288 :     inc[k] = (i > 0)? -1-i: 1-i;
    1461             :   }
    1462      915619 : }
    1463             : 
    1464             : /* 1 if we are "sure" that x < y, up to few rounding errors, i.e.
    1465             :  * x < y - epsilon. More precisely :
    1466             :  * - sign(x - y) < 0
    1467             :  * - lgprec(x-y) > 3 || expo(x - y) - expo(x) > -24 */
    1468             : static int
    1469      359462 : mplessthan(GEN x, GEN y)
    1470             : {
    1471      359462 :   pari_sp av = avma;
    1472      359462 :   GEN z = mpsub(x, y);
    1473      359462 :   avma = av;
    1474      359462 :   if (typ(z) == t_INT) return (signe(z) < 0);
    1475      359462 :   if (signe(z) >= 0) return 0;
    1476       58704 :   if (realprec(z) > LOWDEFAULTPREC) return 1;
    1477       58704 :   return ( expo(z) - mpexpo(x) > -24 );
    1478             : }
    1479             : 
    1480             : /* 1 if we are "sure" that x > y, up to few rounding errors, i.e.
    1481             :  * x > y + epsilon */
    1482             : static int
    1483     1449389 : mpgreaterthan(GEN x, GEN y)
    1484             : {
    1485     1449389 :   pari_sp av = avma;
    1486     1449389 :   GEN z = mpsub(x, y);
    1487     1449389 :   avma = av;
    1488     1449389 :   if (typ(z) == t_INT) return (signe(z) > 0);
    1489     1449389 :   if (signe(z) <= 0) return 0;
    1490      820170 :   if (realprec(z) > LOWDEFAULTPREC) return 1;
    1491      144575 :   return ( expo(z) - mpexpo(x) > -24 );
    1492             : }
    1493             : 
    1494             : /* x a t_INT, y  t_INT or t_REAL */
    1495             : INLINE GEN
    1496      357922 : mulimp(GEN x, GEN y)
    1497             : {
    1498      357922 :   if (typ(y) == t_INT) return mulii(x,y);
    1499      357922 :   return signe(x) ? mulir(x,y): gen_0;
    1500             : }
    1501             : /* x + y*z, x,z two mp's, y a t_INT */
    1502             : INLINE GEN
    1503     4506310 : addmulimp(GEN x, GEN y, GEN z)
    1504             : {
    1505     4506310 :   if (!signe(y)) return x;
    1506     1948747 :   if (typ(z) == t_INT) return mpadd(x, mulii(y, z));
    1507     1948747 :   return mpadd(x, mulir(y, z));
    1508             : }
    1509             : 
    1510             : /* yk + vk * (xk + zk)^2 */
    1511             : static GEN
    1512     1802326 : norm_aux(GEN xk, GEN yk, GEN zk, GEN vk)
    1513             : {
    1514     1802326 :   GEN t = mpadd(xk, zk);
    1515     1802326 :   if (typ(t) == t_INT) { /* probably gen_0, avoid loss of accuracy */
    1516       17353 :     yk = addmulimp(yk, sqri(t), vk);
    1517             :   } else {
    1518     1784973 :     yk = mpadd(yk, mpmul(sqrr(t), vk));
    1519             :   }
    1520     1802326 :   return yk;
    1521             : }
    1522             : /* yk + vk * (xk + zk)^2 < B + epsilon */
    1523             : static int
    1524     1273198 : check_bound(GEN B, GEN xk, GEN yk, GEN zk, GEN vk)
    1525             : {
    1526     1273198 :   pari_sp av = avma;
    1527     1273198 :   int f = mpgreaterthan(norm_aux(xk,yk,zk,vk), B);
    1528     1273198 :   avma = av; return !f;
    1529             : }
    1530             : 
    1531             : /* q(k-th canonical basis vector), where q is given in Cholesky form
    1532             :  * q(x) = sum_{i = 1}^n q[i,i] (x[i] + sum_{j > i} q[i,j] x[j])^2.
    1533             :  * Namely q(e_k) = q[k,k] + sum_{i < k} q[i,i] q[i,k]^2
    1534             :  * Assume 1 <= k <= n. */
    1535             : static GEN
    1536         182 : cholesky_norm_ek(GEN q, long k)
    1537             : {
    1538         182 :   GEN t = gcoeff(q,k,k);
    1539             :   long i;
    1540         182 :   for (i = 1; i < k; i++) t = norm_aux(gen_0, t, gcoeff(q,i,k), gcoeff(q,i,i));
    1541         182 :   return t;
    1542             : }
    1543             : 
    1544             : /* q is the Cholesky decomposition of a quadratic form
    1545             :  * Enumerate vectors whose norm is less than BORNE (Algo 2.5.7),
    1546             :  * minimal vectors if BORNE = NULL (implies check = NULL).
    1547             :  * If (check != NULL) consider only vectors passing the check, and assumes
    1548             :  *   we only want the smallest possible vectors */
    1549             : static GEN
    1550         833 : smallvectors(GEN q, GEN BORNE, long maxnum, FP_chk_fun *CHECK)
    1551             : {
    1552         833 :   long N = lg(q), n = N-1, i, j, k, s, stockmax, checkcnt = 1;
    1553             :   pari_sp av, av1;
    1554             :   GEN inc, S, x, y, z, v, p1, alpha, norms;
    1555             :   GEN norme1, normax1, borne1, borne2;
    1556         833 :   GEN (*check)(void *,GEN) = CHECK? CHECK->f: NULL;
    1557         833 :   void *data = CHECK? CHECK->data: NULL;
    1558         833 :   const long skipfirst = CHECK? CHECK->skipfirst: 0;
    1559         833 :   const int stockall = (maxnum == -1);
    1560             : 
    1561         833 :   alpha = dbltor(0.95);
    1562         833 :   normax1 = gen_0;
    1563             : 
    1564         833 :   v = cgetg(N,t_VEC);
    1565         833 :   inc = const_vecsmall(n, 1);
    1566             : 
    1567         833 :   av = avma;
    1568         833 :   stockmax = stockall? 2000: maxnum;
    1569         833 :   norms = cgetg(check?(stockmax+1): 1,t_VEC); /* unused if (!check) */
    1570         833 :   S = cgetg(stockmax+1,t_VEC);
    1571         833 :   x = cgetg(N,t_COL);
    1572         833 :   y = cgetg(N,t_COL);
    1573         833 :   z = cgetg(N,t_COL);
    1574        4998 :   for (i=1; i<N; i++) {
    1575        4165 :     gel(v,i) = gcoeff(q,i,i);
    1576        4165 :     gel(x,i) = gel(y,i) = gel(z,i) = gen_0;
    1577             :   }
    1578         833 :   if (BORNE)
    1579             :   {
    1580         819 :     borne1 = BORNE;
    1581         819 :     if (typ(borne1) != t_REAL)
    1582             :     {
    1583             :       long prec;
    1584         476 :       if (gequal0(borne1)) retmkvec3(gen_0, gen_0, cgetg(1,t_MAT));
    1585         469 :       prec = nbits2prec(gexpo(borne1) + 10);
    1586         469 :       borne1 = gtofp(borne1, maxss(prec, DEFAULTPREC));
    1587             :     }
    1588             :   }
    1589             :   else
    1590             :   {
    1591          14 :     borne1 = gcoeff(q,1,1);
    1592         196 :     for (i=2; i<N; i++)
    1593             :     {
    1594         182 :       GEN b = cholesky_norm_ek(q, i);
    1595         182 :       if (gcmp(b, borne1) < 0) borne1 = b;
    1596             :     }
    1597             :     /* borne1 = norm of smallest basis vector */
    1598             :   }
    1599         826 :   borne2 = mulrr(borne1,alpha);
    1600         826 :   if (DEBUGLEVEL>2)
    1601           0 :     err_printf("smallvectors looking for norm < %P.4G\n",borne1);
    1602         826 :   s = 0; k = n;
    1603      170387 :   for(;; step(x,y,inc,k)) /* main */
    1604             :   { /* x (supposedly) small vector, ZV.
    1605             :      * For all t >= k, we have
    1606             :      *   z[t] = sum_{j > t} q[t,j] * x[j]
    1607             :      *   y[t] = sum_{i > t} q[i,i] * (x[i] + z[i])^2
    1608             :      *        = 0 <=> x[i]=0 for all i>t */
    1609             :     do
    1610             :     {
    1611      528309 :       int skip = 0;
    1612      528309 :       if (k > 1)
    1613             :       {
    1614      357922 :         long l = k-1;
    1615      357922 :         av1 = avma;
    1616      357922 :         p1 = mulimp(gel(x,k), gcoeff(q,l,k));
    1617      357922 :         for (j=k+1; j<N; j++) p1 = addmulimp(p1, gel(x,j), gcoeff(q,l,j));
    1618      357922 :         gel(z,l) = gerepileuptoleaf(av1,p1);
    1619             : 
    1620      357922 :         av1 = avma;
    1621      357922 :         p1 = norm_aux(gel(x,k), gel(y,k), gel(z,k), gel(v,k));
    1622      357922 :         gel(y,l) = gerepileuptoleaf(av1, p1);
    1623             :         /* skip the [x_1,...,x_skipfirst,0,...,0] */
    1624      357922 :         if ((l <= skipfirst && !signe(gel(y,skipfirst)))
    1625      357579 :          || mplessthan(borne1, gel(y,l))) skip = 1;
    1626             :         else /* initial value, minimizing (x[l] + z[l])^2, hence qf(x) for
    1627             :                 the given x[1..l-1] */
    1628      357579 :           gel(x,l) = mpround( mpneg(gel(z,l)) );
    1629      357922 :         k = l;
    1630             :       }
    1631      357922 :       for(;; step(x,y,inc,k))
    1632             :       { /* at most 2n loops */
    1633      886231 :         if (!skip)
    1634             :         {
    1635      885888 :           if (check_bound(borne1, gel(x,k),gel(y,k),gel(z,k),gel(v,k))) break;
    1636      387310 :           step(x,y,inc,k);
    1637      387310 :           if (check_bound(borne1, gel(x,k),gel(y,k),gel(z,k),gel(v,k))) break;
    1638             :         }
    1639      358748 :         skip = 0; inc[k] = 1;
    1640      358748 :         if (++k > n) goto END;
    1641      357922 :       }
    1642             : 
    1643      527483 :       if (gc_needed(av,2))
    1644             :       {
    1645           8 :         if(DEBUGMEM>1) pari_warn(warnmem,"smallvectors");
    1646           8 :         if (stockmax) S = clonefill(S, s, stockmax);
    1647           8 :         if (check) {
    1648           8 :           GEN dummy = cgetg(1, t_STR);
    1649           8 :           for (i=s+1; i<=stockmax; i++) gel(norms,i) = dummy;
    1650             :         }
    1651           8 :         gerepileall(av,7,&x,&y,&z,&normax1,&borne1,&borne2,&norms);
    1652             :       }
    1653             :     }
    1654      527483 :     while (k > 1);
    1655      170387 :     if (!signe(gel(x,1)) && !signe(gel(y,1))) continue; /* exclude 0 */
    1656             : 
    1657      169904 :     av1 = avma;
    1658      169904 :     norme1 = norm_aux(gel(x,1),gel(y,1),gel(z,1),gel(v,1));
    1659      169904 :     if (mpgreaterthan(norme1,borne1)) { avma = av1; continue; /* main */ }
    1660             : 
    1661      169904 :     norme1 = gerepileuptoleaf(av1,norme1);
    1662      169904 :     if (check)
    1663             :     {
    1664      101325 :       if (checkcnt < 5 && mpcmp(norme1, borne2) < 0)
    1665             :       {
    1666         469 :         if (!check(data,x)) { checkcnt++ ; continue; /* main */}
    1667         210 :         if (DEBUGLEVEL>4) err_printf("New bound: %Ps", norme1);
    1668         210 :         borne1 = norme1;
    1669         210 :         borne2 = mulrr(borne1, alpha);
    1670         210 :         s = 0; checkcnt = 0;
    1671             :       }
    1672             :     }
    1673             :     else
    1674             :     {
    1675       68579 :       if (!BORNE) /* find minimal vectors */
    1676             :       {
    1677        1883 :         if (mplessthan(norme1, borne1))
    1678             :         { /* strictly smaller vector than previously known */
    1679           0 :           borne1 = norme1; /* + epsilon */
    1680           0 :           s = 0;
    1681             :         }
    1682             :       }
    1683             :       else
    1684       66696 :         if (mpcmp(norme1,normax1) > 0) normax1 = norme1;
    1685             :     }
    1686      169645 :     if (++s > stockmax) continue; /* too many vectors: no longer remember */
    1687      168714 :     if (check) gel(norms,s) = norme1;
    1688      168714 :     gel(S,s) = leafcopy(x);
    1689      168714 :     if (s != stockmax) continue; /* still room, get next vector */
    1690             : 
    1691             :     /* overflow, eliminate vectors failing "check" */
    1692          56 :     if (check)
    1693             :     {
    1694          35 :       pari_sp av2 = avma;
    1695             :       long imin, imax;
    1696          35 :       GEN per = indexsort(norms);
    1697          35 :       if (DEBUGLEVEL>2) err_printf("sorting... [%ld elts]\n",s);
    1698             :       /* let N be the minimal norm so far for x satisfying 'check'. Keep
    1699             :        * all elements of norm N */
    1700        3675 :       for (i = 1; i <= s; i++)
    1701             :       {
    1702        3675 :         long k = per[i];
    1703        3675 :         if (check(data,gel(S,k))) { borne1 = gel(norms,k); break; }
    1704             :       }
    1705          35 :       imin = i;
    1706         273 :       for (; i <= s; i++)
    1707         273 :         if (mpgreaterthan(gel(norms,per[i]), borne1)) break;
    1708          35 :       imax = i;
    1709          35 :       for (i=imin, s=0; i < imax; i++) gel(S,++s) = gel(S,per[i]);
    1710          35 :       avma = av2;
    1711          35 :       if (s)
    1712             :       {
    1713          35 :         borne2 = mulrr(borne1, alpha);
    1714          35 :         checkcnt = 0;
    1715             :       }
    1716          35 :       if (!stockall) continue;
    1717          35 :       if (s > stockmax/2) stockmax <<= 1;
    1718          35 :       norms = cgetg(stockmax+1, t_VEC);
    1719          35 :       for (i = 1; i <= s; i++) gel(norms,i) = borne1;
    1720             :     }
    1721             :     else
    1722             :     {
    1723          21 :       if (!stockall && BORNE) goto END;
    1724          21 :       if (!stockall) continue;
    1725          21 :       stockmax <<= 1;
    1726             :     }
    1727             : 
    1728             :     {
    1729          56 :       GEN Snew = cgetg(stockmax + 1, t_VEC);
    1730          56 :       for (i = 1; i <= s; i++) gel(Snew,i) = gel(S,i);
    1731          56 :       Snew = clonefill(Snew, s, stockmax);
    1732          56 :       if (isclone(S)) gunclone(S);
    1733          56 :       S = Snew;
    1734             :     }
    1735      170387 :   }
    1736             : END:
    1737         826 :   if (s < stockmax) stockmax = s;
    1738         826 :   if (check)
    1739             :   {
    1740             :     GEN per, alph, pols, p;
    1741         805 :     if (DEBUGLEVEL>2) err_printf("final sort & check...\n");
    1742         805 :     setlg(norms,stockmax+1); per = indexsort(norms);
    1743         805 :     alph = cgetg(stockmax+1,t_VEC);
    1744         805 :     pols = cgetg(stockmax+1,t_VEC);
    1745        7681 :     for (j=0,i=1; i<=stockmax; i++)
    1746             :     {
    1747        6896 :       long t = per[i];
    1748        6896 :       GEN N = gel(norms,t);
    1749        6896 :       if (j && mpgreaterthan(N, borne1)) break;
    1750        6876 :       if ((p = check(data,gel(S,t))))
    1751             :       {
    1752        6225 :         if (!j) borne1 = N;
    1753        6225 :         j++;
    1754        6225 :         gel(pols,j) = p;
    1755        6225 :         gel(alph,j) = gel(S,t);
    1756             :       }
    1757             :     }
    1758         805 :     setlg(pols,j+1);
    1759         805 :     setlg(alph,j+1);
    1760         805 :     if (stockmax && isclone(S)) { alph = gcopy(alph); gunclone(S); }
    1761         805 :     return mkvec2(pols, alph);
    1762             :   }
    1763          21 :   if (stockmax)
    1764             :   {
    1765          14 :     setlg(S,stockmax+1);
    1766          14 :     settyp(S,t_MAT);
    1767          14 :     if (isclone(S)) { p1 = S; S = gcopy(S); gunclone(p1); }
    1768             :   }
    1769             :   else
    1770           7 :     S = cgetg(1,t_MAT);
    1771          21 :   return mkvec3(utoi(s<<1), borne1, S);
    1772             : }
    1773             : 
    1774             : /* solve q(x) = x~.a.x <= bound, a > 0.
    1775             :  * If check is non-NULL keep x only if check(x).
    1776             :  * If a is a vector, assume a[1] is the LLL-reduced Cholesky form of q */
    1777             : GEN
    1778         848 : fincke_pohst(GEN a, GEN B0, long stockmax, long PREC, FP_chk_fun *CHECK)
    1779             : {
    1780         848 :   pari_sp av = avma;
    1781             :   VOLATILE long i,j,l;
    1782         848 :   VOLATILE GEN r,rinv,rinvtrans,u,v,res,z,vnorm,rperm,perm,uperm, bound = B0;
    1783             : 
    1784         848 :   if (typ(a) == t_VEC)
    1785             :   {
    1786         351 :     r = gel(a,1);
    1787         351 :     u = NULL;
    1788             :   }
    1789             :   else
    1790             :   {
    1791         497 :     long prec = PREC;
    1792         497 :     l = lg(a);
    1793         497 :     if (l == 1)
    1794             :     {
    1795           7 :       if (CHECK) pari_err_TYPE("fincke_pohst [dimension 0]", a);
    1796           7 :       retmkvec3(gen_0, gen_0, cgetg(1,t_MAT));
    1797             :     }
    1798         490 :     u = lllfp(a, 0.75, LLL_GRAM);
    1799         490 :     if (lg(u) != lg(a)) return NULL;
    1800         490 :     r = qf_apply_RgM(a,u);
    1801         490 :     i = gprecision(r);
    1802         490 :     if (i)
    1803         462 :       prec = i;
    1804             :     else {
    1805          28 :       prec = DEFAULTPREC + nbits2extraprec(gexpo(r));
    1806          28 :       if (prec < PREC) prec = PREC;
    1807             :     }
    1808         490 :     if (DEBUGLEVEL>2) err_printf("first LLL: prec = %ld\n", prec);
    1809         490 :     r = qfgaussred_positive(r);
    1810         490 :     if (!r) return NULL;
    1811        2198 :     for (i=1; i<l; i++)
    1812             :     {
    1813        1708 :       GEN s = gsqrt(gcoeff(r,i,i), prec);
    1814        1708 :       gcoeff(r,i,i) = s;
    1815        1708 :       for (j=i+1; j<l; j++) gcoeff(r,i,j) = gmul(s, gcoeff(r,i,j));
    1816             :     }
    1817             :   }
    1818             :   /* now r~ * r = a in LLL basis */
    1819         841 :   rinv = RgM_inv_upper(r);
    1820         841 :   if (!rinv) return NULL;
    1821         841 :   rinvtrans = shallowtrans(rinv);
    1822         841 :   if (DEBUGLEVEL>2)
    1823           0 :     err_printf("Fincke-Pohst, final LLL: prec = %ld\n", gprecision(rinvtrans));
    1824         841 :   v = lll(rinvtrans);
    1825         841 :   if (lg(v) != lg(rinvtrans)) return NULL;
    1826             : 
    1827         841 :   rinvtrans = RgM_mul(rinvtrans, v);
    1828         841 :   v = ZM_inv(shallowtrans(v),gen_1);
    1829         841 :   r = RgM_mul(r,v);
    1830         841 :   u = u? ZM_mul(u,v): v;
    1831             : 
    1832         841 :   l = lg(r);
    1833         841 :   vnorm = cgetg(l,t_VEC);
    1834         841 :   for (j=1; j<l; j++) gel(vnorm,j) = gnorml2(gel(rinvtrans,j));
    1835         841 :   rperm = cgetg(l,t_MAT);
    1836         841 :   uperm = cgetg(l,t_MAT); perm = indexsort(vnorm);
    1837         841 :   for (i=1; i<l; i++) { uperm[l-i] = u[perm[i]]; rperm[l-i] = r[perm[i]]; }
    1838         841 :   u = uperm;
    1839         841 :   r = rperm; res = NULL;
    1840         841 :   pari_CATCH(e_PREC) { }
    1841             :   pari_TRY {
    1842             :     GEN q;
    1843         841 :     if (CHECK && CHECK->f_init) bound = CHECK->f_init(CHECK, r, u);
    1844         833 :     q = gaussred_from_QR(r, gprecision(vnorm));
    1845         833 :     if (!q) pari_err_PREC("fincke_pohst");
    1846         833 :     res = smallvectors(q, bound, stockmax, CHECK);
    1847         833 :   } pari_ENDCATCH;
    1848         841 :   if (CHECK)
    1849             :   {
    1850         813 :     if (CHECK->f_post) res = CHECK->f_post(CHECK, res, u);
    1851         813 :     return res;
    1852             :   }
    1853          28 :   if (!res) pari_err_PREC("fincke_pohst");
    1854             : 
    1855          28 :   z = cgetg(4,t_VEC);
    1856          28 :   gel(z,1) = gcopy(gel(res,1));
    1857          28 :   gel(z,2) = gcopy(gel(res,2));
    1858          28 :   gel(z,3) = ZM_mul(u, gel(res,3)); return gerepileupto(av,z);
    1859             : }

Generated by: LCOV version 1.11