Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - bibli1.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 19825-b77c7f8) Lines: 990 1040 95.2 %
Date: 2016-12-05 05:49:04 Functions: 61 66 92.4 %
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     1012823 : no_prec_pb(GEN x)
      27             : {
      28     2765733 :   return (typ(x) != t_REAL || realprec(x) > LOWDEFAULTPREC
      29     1014927 :                            || 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     1012823 : FindApplyQ(GEN x, GEN L, GEN B, long k, GEN Q, long prec)
      36             : {
      37     1012823 :   long i, lx = lg(x)-1;
      38     1012823 :   GEN x2, x1, xd = x + (k-1);
      39             : 
      40     1012823 :   x1 = gel(xd,1);
      41     1012823 :   x2 = mpsqr(x1);
      42     1012823 :   if (k < lx)
      43             :   {
      44      679194 :     long lv = lx - (k-1) + 1;
      45      679194 :     GEN beta, Nx, v = cgetg(lv, t_VEC);
      46     2375364 :     for (i=2; i<lv; i++) {
      47     1696170 :       x2 = mpadd(x2, mpsqr(gel(xd,i)));
      48     1696170 :       gel(v,i) = gel(xd,i);
      49             :     }
      50      679194 :     if (!signe(x2)) return 0;
      51      679194 :     Nx = gsqrt(x2, prec); if (signe(x1) < 0) setsigne(Nx, -1);
      52      679194 :     gel(v,1) = mpadd(x1, Nx);
      53             : 
      54      679194 :     if (!signe(x1))
      55          92 :       beta = gtofp(x2, prec); /* make sure typ(beta) != t_INT */
      56             :     else
      57      679102 :       beta = mpadd(x2, mpmul(Nx,x1));
      58      679194 :     gel(Q,k) = mkvec2(invr(beta), v);
      59             : 
      60      679194 :     togglesign(Nx);
      61      679194 :     gcoeff(L,k,k) = Nx;
      62             :   }
      63             :   else
      64      333629 :     gcoeff(L,k,k) = gel(x,k);
      65     1012823 :   gel(B,k) = x2;
      66     1012823 :   for (i=1; i<k; i++) gcoeff(L,k,i) = gel(x,i);
      67     1012823 :   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     1696152 : ApplyQ(GEN Q, GEN r)
      74             : {
      75     1696152 :   GEN s, rd, beta = gel(Q,1), v = gel(Q,2);
      76     1696152 :   long i, l = lg(v), lr = lg(r);
      77             : 
      78     1696152 :   rd = r + (lr - l);
      79     1696152 :   s = mpmul(gel(v,1), gel(rd,1));
      80     1696152 :   for (i=2; i<l; i++) s = mpadd(s, mpmul(gel(v,i) ,gel(rd,i)));
      81     1696152 :   s = mpmul(beta, s);
      82    12174360 :   for (i=1; i<l; i++)
      83    10478208 :     if (signe(gel(v,i))) gel(rd,i) = mpsub(gel(rd,i), mpmul(s, gel(v,i)));
      84     1696152 : }
      85             : /* apply Q[1], ..., Q[j-1] to r */
      86             : static GEN
      87      679188 : ApplyAllQ(GEN Q, GEN r, long j)
      88             : {
      89      679188 :   pari_sp av = avma;
      90             :   long i;
      91      679188 :   r = leafcopy(r);
      92      679188 :   for (i=1; i<j; i++) ApplyQ(gel(Q,i), r);
      93      679188 :   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      333635 : QR_init(GEN x, GEN *pB, GEN *pQ, GEN *pL, long prec)
     183             : {
     184      333635 :   long j, k = lg(x)-1;
     185      333635 :   GEN B = cgetg(k+1, t_VEC), Q = cgetg(k, t_VEC), L = zeromatcopy(k,k);
     186     1346446 :   for (j=1; j<=k; j++)
     187             :   {
     188     1012823 :     GEN r = gel(x,j);
     189     1012823 :     if (j > 1) r = ApplyAllQ(Q, r, j);
     190     1012823 :     if (!FindApplyQ(r, L, B, j, Q, prec)) return 0;
     191             :   }
     192      333623 :   *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      333258 : gaussred_from_QR(GEN x, long prec)
     198             : {
     199      333258 :   long j, k = lg(x)-1;
     200             :   GEN B, Q, L;
     201      333258 :   if (!QR_init(x, &B,&Q,&L, prec)) return NULL;
     202     1009744 :   for (j=1; j<k; j++)
     203             :   {
     204      676486 :     GEN m = gel(L,j), invNx = invr(gel(m,j));
     205             :     long i;
     206      676486 :     gel(m,j) = gel(B,j);
     207      676486 :     for (i=j+1; i<=k; i++) gel(m,i) = mpmul(invNx, gel(m,i));
     208             :   }
     209      333258 :   gcoeff(L,j,j) = gel(B,j);
     210      333258 :   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         434 : RgM_gram_schmidt(GEN e, GEN *ptB)
     228             : {
     229         434 :   long i,j,lx = lg(e);
     230         434 :   GEN f = RgM_shallowcopy(e), B, iB;
     231             : 
     232         434 :   B = cgetg(lx, t_VEC);
     233         434 :   iB= cgetg(lx, t_VEC);
     234             : 
     235        1435 :   for (i=1;i<lx;i++)
     236             :   {
     237        1001 :     GEN p1 = NULL;
     238        1001 :     pari_sp av = avma;
     239        2205 :     for (j=1; j<i; j++)
     240             :     {
     241        1204 :       GEN mu = gmul(RgV_dotproduct(gel(e,i),gel(f,j)), gel(iB,j));
     242        1204 :       GEN p2 = gmul(mu, gel(f,j));
     243        1204 :       p1 = p1? gadd(p1,p2): p2;
     244             :     }
     245        1001 :     p1 = p1? gerepileupto(av, gsub(gel(e,i), p1)): gel(e,i);
     246        1001 :     gel(f,i) = p1;
     247        1001 :     gel(B,i) = RgV_dotsquare(gel(f,i));
     248        1001 :     gel(iB,i) = ginv(gel(B,i));
     249             :   }
     250         434 :   *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         434 : RgM_Babai(GEN B, GEN t)
     257             : {
     258         434 :   GEN C, N, G = RgM_gram_schmidt(B, &N), b = t;
     259         434 :   long j, n = lg(B)-1;
     260             : 
     261         434 :   C = cgetg(n+1,t_COL);
     262        1435 :   for (j = n; j > 0; j--)
     263             :   {
     264        1001 :     GEN c = gdiv( RgV_dotproduct(b, gel(G,j)), gel(N,j) );
     265             :     long e;
     266        1001 :     c = grndtoi(c,&e);
     267        1001 :     if (e >= 0) return NULL;
     268        1001 :     if (signe(c)) b = RgC_sub(b, RgC_Rg_mul(gel(G,j), c));
     269        1001 :     gel(C,j) = c;
     270             :   }
     271         434 :   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        2796 : lindep2(GEN x, long bit)
     713             : {
     714        2796 :   long tx=typ(x), lx=lg(x), ly, i, j;
     715        2796 :   pari_sp av = avma;
     716             :   GEN re, im, M;
     717             : 
     718        2796 :   if (! is_vec_t(tx)) pari_err_TYPE("lindep2",x);
     719        2796 :   if (lx<=2)
     720             :   {
     721          21 :     if (lx == 2 && gequal0(x)) return mkcol(gen_1);
     722          14 :     return cgetg(1,t_COL);
     723             :   }
     724        2775 :   if (bit < 0) pari_err_DOMAIN("lindep2", "accuracy", "<", gen_0, stoi(bit));
     725        2775 :   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        2078 :     bit = (long) (bit/LOG10_2);
     738        2775 :   re = real_i(x);
     739        2775 :   im = imag_i(x);
     740             :   /* independent over R ? */
     741        2775 :   if (lx == 3 && real_indep(re,im,bit)) { avma = av; return cgetg(1, t_COL); }
     742        2768 :   if (gequal0(im)) im = NULL;
     743        2768 :   ly = im? lx+2: lx+1;
     744        2768 :   M = cgetg(lx,t_MAT);
     745       11222 :   for (i=1; i<lx; i++)
     746             :   {
     747        8454 :     GEN c = cgetg(ly,t_COL); gel(M,i) = c;
     748        8454 :     for (j=1; j<lx; j++) gel(c,j) = (i==j)? gen_1: gen_0;
     749        8454 :     gel(c,lx)           = gtrunc2n(gel(re,i), bit);
     750        8454 :     if (im) gel(c,lx+1) = gtrunc2n(gel(im,i), bit);
     751             :   }
     752        2768 :   M = ZM_lll(M, 0.99, LLL_INPLACE);
     753        2768 :   M = gel(M,1);
     754        2768 :   M[0] = evaltyp(t_COL) | evallg(lx);
     755        2768 :   return gerepilecopy(av, M);
     756             : }
     757             : 
     758             : void
     759      424039 : init_dalloc(void)
     760             : { /* correct alignment for dalloc */
     761      424039 :   (void)new_chunk((avma % sizeof(double)) / sizeof(long));
     762      424039 : }
     763             : 
     764             : double *
     765      148792 : dalloc(size_t n)
     766             : {
     767      148792 :   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             : static GEN
     838          35 : vec_lindep(GEN x)
     839             : {
     840          35 :   pari_sp av = avma;
     841          35 :   long i, l = lg(x); /* > 1 */
     842          35 :   long t = typ(gel(x,1)), h = lg(gel(x,1));
     843          35 :   GEN m = cgetg(l, t_MAT);
     844         126 :   for (i = 1; i < l; i++)
     845             :   {
     846          98 :     GEN y = gel(x,i);
     847          98 :     if (lg(y) != h || typ(y) != t) pari_err_TYPE("lindep",x);
     848          91 :     if (t != t_COL) y = shallowtrans(y); /* Sigh */
     849          91 :     gel(m,i) = y;
     850             :   }
     851          28 :   return gerepileupto(av, deplin(m));
     852             : }
     853             : 
     854             : GEN
     855         627 : lindep(GEN x) { return lindep2(x, 0); }
     856             : 
     857             : GEN
     858         252 : lindep0(GEN x,long bit)
     859             : {
     860         252 :   long i, tx = typ(x);
     861         252 :   if (tx == t_MAT) return deplin(x);
     862         133 :   if (! is_vec_t(tx)) pari_err_TYPE("lindep",x);
     863         399 :   for (i = 1; i < lg(x); i++)
     864         322 :     switch(typ(gel(x,i)))
     865             :     {
     866           7 :       case t_PADIC: return padic_lindep(x);
     867             :       case t_POL:
     868             :       case t_RFRAC:
     869          14 :       case t_SER: return Xadic_lindep(x);
     870             :       case t_VEC:
     871          35 :       case t_COL: return vec_lindep(x);
     872             :     }
     873          77 :   return lindep2(x, bit);
     874             : }
     875             : 
     876             : GEN
     877          49 : algdep0(GEN x, long n, long bit)
     878             : {
     879          49 :   long tx = typ(x), i;
     880             :   pari_sp av;
     881             :   GEN y;
     882             : 
     883          49 :   if (! is_scalar_t(tx)) pari_err_TYPE("algdep0",x);
     884          49 :   if (tx==t_POLMOD) { y = RgX_copy(gel(x,1)); setvarn(y,0); return y; }
     885          49 :   if (gequal0(x)) return pol_x(0);
     886          49 :   if (n <= 0)
     887             :   {
     888          14 :     if (!n) return gen_1;
     889           7 :     pari_err_DOMAIN("algdep", "degree", "<", gen_0, stoi(n));
     890             :   }
     891             : 
     892          35 :   av = avma; y = cgetg(n+2,t_COL);
     893          35 :   gel(y,1) = gen_1;
     894          35 :   gel(y,2) = x; /* n >= 1 */
     895          35 :   for (i=3; i<=n+1; i++) gel(y,i) = gmul(gel(y,i-1),x);
     896          35 :   if (typ(x) == t_PADIC)
     897           7 :     y = padic_lindep(y);
     898             :   else
     899          28 :     y = lindep2(y, bit);
     900          35 :   if (lg(y) == 1) pari_err(e_DOMAIN,"algdep", "degree(x)",">", stoi(n), x);
     901          35 :   y = RgV_to_RgX(y, 0);
     902          35 :   if (signe(leading_coeff(y)) > 0) return gerepilecopy(av, y);
     903           0 :   return gerepileupto(av, ZX_neg(y));
     904             : }
     905             : 
     906             : GEN
     907           0 : algdep(GEN x, long n)
     908             : {
     909           0 :   return algdep0(x,n,0);
     910             : }
     911             : 
     912             : GEN
     913          28 : seralgdep(GEN s, long p, long r)
     914             : {
     915          28 :   pari_sp av = avma;
     916             :   long vy, i, m, n, prec;
     917             :   GEN S, v, D;
     918             : 
     919          28 :   if (typ(s) != t_SER) pari_err_TYPE("seralgdep",s);
     920          28 :   if (p <= 0) pari_err_DOMAIN("seralgdep", "p", "<=", gen_0, stoi(p));
     921          28 :   if (r < 0) pari_err_DOMAIN("seralgdep", "r", "<", gen_0, stoi(r));
     922          28 :   if (is_bigint(addiu(muluu(p, r), 1))) pari_err_OVERFLOW("seralgdep");
     923          28 :   vy = varn(s);
     924          28 :   if (!vy) pari_err_PRIORITY("seralgdep", s, ">", 0);
     925          28 :   r++; p++;
     926          28 :   prec = valp(s) + lg(s)-2;
     927          28 :   if (r > prec) r = prec;
     928          28 :   S = cgetg(p+1, t_VEC); gel(S, 1) = s;
     929          28 :   for (i = 2; i <= p; i++) gel(S,i) = gmul(gel(S,i-1), s);
     930          28 :   v = cgetg(r*p+1, t_VEC); /* v[r*n+m+1] = s^n * y^m */
     931             :   /* n = 0 */
     932          28 :   for (m = 0; m < r; m++) gel(v, m+1) = pol_xn(m, vy);
     933          70 :   for(n=1; n < p; n++)
     934         175 :     for (m = 0; m < r; m++)
     935             :     {
     936         133 :       GEN c = gel(S,n);
     937         133 :       if (m)
     938             :       {
     939          91 :         c = shallowcopy(c);
     940          91 :         setvalp(c, valp(c) + m);
     941             :       }
     942         133 :       gel(v, r*n + m + 1) = c;
     943             :     }
     944          28 :   D = Xadic_lindep(v);
     945          28 :   if (lg(D) == 1) { avma = av; return gen_0; }
     946          21 :   v = cgetg(p+1, t_VEC);
     947          70 :   for (n = 0; n < p; n++)
     948          49 :     gel(v, n+1) = RgV_to_RgX(vecslice(D, r*n+1, r*n+r), vy);
     949          21 :   return gerepilecopy(av, RgV_to_RgX(v, 0));
     950             : }
     951             : 
     952             : /********************************************************************/
     953             : /**                                                                **/
     954             : /**                              MINIM                             **/
     955             : /**                                                                **/
     956             : /********************************************************************/
     957             : void
     958       19176 : minim_alloc(long n, double ***q, GEN *x, double **y,  double **z, double **v)
     959             : {
     960             :   long i, s;
     961             : 
     962       19176 :   *x = cgetg(n, t_VECSMALL);
     963       19176 :   *q = (double**) new_chunk(n);
     964       19176 :   s = n * sizeof(double);
     965       19176 :   init_dalloc();
     966       19176 :   *y = dalloc(s);
     967       19176 :   *z = dalloc(s);
     968       19176 :   *v = dalloc(s);
     969       19176 :   for (i=1; i<n; i++) (*q)[i] = dalloc(s);
     970       19176 : }
     971             : 
     972             : static GEN
     973      202629 : ZC_canon(GEN V)
     974             : {
     975      202629 :   long l = lg(V), j;
     976      202629 :   for (j = 1; j < l  &&  signe(gel(V,j)) == 0; ++j);
     977      202629 :   return (j < l  &&  signe(gel(V,j)) < 0)? ZC_neg(V): V;
     978             : }
     979             : 
     980             : static GEN
     981      202629 : ZM_zc_mul_canon(GEN u, GEN x)
     982             : {
     983      202629 :   return ZC_canon(ZM_zc_mul(u,x));
     984             : }
     985             : 
     986             : struct qfvec
     987             : {
     988             :   GEN a, r, u;
     989             : };
     990             : 
     991             : static void
     992           0 : err_minim(GEN a)
     993             : {
     994           0 :   pari_err_DOMAIN("minim0","form","is not",
     995             :                   strtoGENstr("positive definite"),a);
     996           0 : }
     997             : 
     998             : static GEN
     999         559 : minim_lll(GEN a, GEN *u)
    1000             : {
    1001         559 :   *u = lllgramint(a);
    1002         559 :   if (lg(*u) != lg(a)) err_minim(a);
    1003         559 :   return qf_apply_ZM(a,*u);
    1004             : }
    1005             : 
    1006             : static void
    1007         559 : forqfvec_init_dolll(struct qfvec *qv, GEN a, long dolll)
    1008             : {
    1009             :   GEN r, u;
    1010         559 :   if (!dolll) u = NULL;
    1011             :   else
    1012             :   {
    1013         517 :     if (typ(a) != t_MAT || !RgM_is_ZM(a)) pari_err_TYPE("qfminim",a);
    1014         517 :     a = minim_lll(a, &u);
    1015             :   }
    1016         559 :   qv->a = RgM_gtofp(a, DEFAULTPREC);
    1017         559 :   r = qfgaussred_positive(qv->a);
    1018         559 :   if (!r)
    1019             :   {
    1020           0 :     r = qfgaussred_positive(a); /* exact computation */
    1021           0 :     if (!r) err_minim(a);
    1022           0 :     r = RgM_gtofp(r, DEFAULTPREC);
    1023             :   }
    1024         559 :   qv->r = r;
    1025         559 :   qv->u = u;
    1026         559 : }
    1027             : 
    1028             : static void
    1029          21 : forqfvec_init(struct qfvec *qv, GEN a)
    1030          21 : { forqfvec_init_dolll(qv, a, 1); }
    1031             : 
    1032             : static void
    1033          21 : forqfvec_i(void *E, long (*fun)(void *, GEN, GEN, double), struct qfvec *qv, GEN BORNE)
    1034             : {
    1035          21 :   GEN x, a = qv->a, r = qv->r, u = qv->u;
    1036          21 :   long n = lg(a), i, j, k;
    1037             :   double p,BOUND,*v,*y,*z,**q;
    1038          21 :   const double eps = 0.0001;
    1039          21 :   if (!BORNE) BORNE = gen_0;
    1040             :   else
    1041             :   {
    1042          14 :     BORNE = gfloor(BORNE);
    1043          14 :     if (typ(BORNE) != t_INT) pari_err_TYPE("minim0",BORNE);
    1044             :   }
    1045          42 :   if (n == 1) return;
    1046          14 :   minim_alloc(n, &q, &x, &y, &z, &v);
    1047          14 :   n--;
    1048          42 :   for (j=1; j<=n; j++)
    1049             :   {
    1050          28 :     v[j] = rtodbl(gcoeff(r,j,j));
    1051          28 :     for (i=1; i<j; i++) q[i][j] = rtodbl(gcoeff(r,i,j));
    1052             :   }
    1053             : 
    1054          14 :   if (gequal0(BORNE))
    1055             :   {
    1056             :     double c;
    1057           7 :     p = rtodbl(gcoeff(a,1,1));
    1058           7 :     for (i=2; i<=n; i++) { c = rtodbl(gcoeff(a,i,i)); if (c < p) p = c; }
    1059           7 :     BORNE = roundr(dbltor(p));
    1060             :   }
    1061             :   else
    1062           7 :     p = gtodouble(BORNE);
    1063          14 :   BOUND = p * (1 + eps);
    1064          14 :   if (BOUND == p) pari_err_PREC("minim0");
    1065             : 
    1066          14 :   k = n; y[n] = z[n] = 0;
    1067          14 :   x[n] = (long)sqrt(BOUND/v[n]);
    1068          28 :   for(;;x[1]--)
    1069             :   {
    1070             :     do
    1071             :     {
    1072          49 :       if (k>1)
    1073             :       {
    1074          21 :         long l = k-1;
    1075          21 :         z[l] = 0;
    1076          21 :         for (j=k; j<=n; j++) z[l] += q[l][j]*x[j];
    1077          21 :         p = (double)x[k] + z[k];
    1078          21 :         y[l] = y[k] + p*p*v[k];
    1079          21 :         x[l] = (long)floor(sqrt((BOUND-y[l])/v[l])-z[l]);
    1080          21 :         k = l;
    1081             :       }
    1082             :       for(;;)
    1083             :       {
    1084          56 :         p = (double)x[k] + z[k];
    1085          56 :         if (y[k] + p*p*v[k] <= BOUND) break;
    1086           7 :         k++; x[k]--;
    1087           7 :       }
    1088          49 :     } while (k > 1);
    1089          42 :     if (! x[1] && y[1]<=eps) break;
    1090             : 
    1091          28 :     p = (double)x[1] + z[1]; p = y[1] + p*p*v[1]; /* norm(x) */
    1092          28 :     if (fun(E, u, x, p)) break;
    1093          28 :   }
    1094             : }
    1095             : 
    1096             : void
    1097           0 : forqfvec(void *E, long (*fun)(void *, GEN, GEN, double), GEN a, GEN BORNE)
    1098             : {
    1099           0 :   pari_sp av = avma;
    1100             :   struct qfvec qv;
    1101           0 :   forqfvec_init(&qv, a);
    1102           0 :   forqfvec_i(E, fun, &qv, BORNE);
    1103           0 :   avma = av;
    1104           0 : }
    1105             : 
    1106             : static long
    1107          28 : _gp_forqf(void *E, GEN u, GEN x, double p/*unused*/)
    1108             : {
    1109          28 :   pari_sp av = avma;
    1110             :   (void)p;
    1111          28 :   set_lex(-1, ZM_zc_mul_canon(u, x));
    1112          28 :   closure_evalvoid((GEN)E);
    1113          28 :   avma = av;
    1114          28 :   return loop_break();
    1115             : }
    1116             : 
    1117             : void
    1118          21 : forqfvec0(GEN a, GEN BORNE, GEN code)
    1119             : {
    1120          21 :   pari_sp av = avma;
    1121             :   struct qfvec qv;
    1122          21 :   forqfvec_init(&qv, a);
    1123          21 :   push_lex(gen_0, code);
    1124          21 :   forqfvec_i((void*) code, &_gp_forqf, &qv, BORNE);
    1125          21 :   pop_lex(1);
    1126          21 :   avma = av;
    1127          21 : }
    1128             : 
    1129             : enum { min_ALL = 0, min_FIRST, min_VECSMALL, min_VECSMALL2 };
    1130             : 
    1131             : /* Minimal vectors for the integral definite quadratic form: a.
    1132             :  * Result u:
    1133             :  *   u[1]= Number of vectors of square norm <= BORNE
    1134             :  *   u[2]= maximum norm found
    1135             :  *   u[3]= list of vectors found (at most STOCKMAX, unless NULL)
    1136             :  *
    1137             :  *  If BORNE = NULL: Minimal non-zero vectors.
    1138             :  *  flag = min_ALL,   as above
    1139             :  *  flag = min_FIRST, exits when first suitable vector is found.
    1140             :  *  flag = min_VECSMALL, return a t_VECSMALL of (half) the number of vectors
    1141             :  *  for each norm
    1142             :  *  flag = min_VECSMALL2, same but count only vectors with even norm, and shift
    1143             :  *  the answer */
    1144             : static GEN
    1145         560 : minim0_dolll(GEN a, GEN BORNE, GEN STOCKMAX, long flag, long dolll)
    1146             : {
    1147             :   GEN x, u, r, L, gnorme;
    1148         560 :   long n = lg(a), i, j, k, s, maxrank, sBORNE;
    1149         560 :   pari_sp av = avma, av1;
    1150             :   double p,maxnorm,BOUND,*v,*y,*z,**q;
    1151         560 :   const double eps = 1e-10;
    1152         560 :   int stockall = 0;
    1153             :   struct qfvec qv;
    1154             : 
    1155         560 :   if (!BORNE)
    1156          56 :     sBORNE = 0;
    1157             :   else
    1158             :   {
    1159         504 :     BORNE = gfloor(BORNE);
    1160         504 :     if (typ(BORNE) != t_INT) pari_err_TYPE("minim0",BORNE);
    1161         504 :     if (is_bigint(BORNE)) pari_err_PREC( "qfminim");
    1162         503 :     sBORNE = itos(BORNE); avma = av;
    1163             :   }
    1164         559 :   if (!STOCKMAX)
    1165             :   {
    1166         237 :     stockall = 1;
    1167         237 :     maxrank = 200;
    1168             :   }
    1169             :   else
    1170             :   {
    1171         322 :     STOCKMAX = gfloor(STOCKMAX);
    1172         322 :     if (typ(STOCKMAX) != t_INT) pari_err_TYPE("minim0",STOCKMAX);
    1173         322 :     maxrank = itos(STOCKMAX);
    1174         322 :     if (maxrank < 0)
    1175           0 :       pari_err_TYPE("minim0 [negative number of vectors]",STOCKMAX);
    1176             :   }
    1177             : 
    1178         559 :   switch(flag)
    1179             :   {
    1180             :     case min_VECSMALL:
    1181             :     case min_VECSMALL2:
    1182         273 :       if (sBORNE <= 0) return cgetg(1, t_VECSMALL);
    1183         273 :       L = zero_zv(sBORNE);
    1184         273 :       if (flag == min_VECSMALL2) sBORNE <<= 1;
    1185         273 :       if (n == 1) return L;
    1186         273 :       break;
    1187             :     case min_FIRST:
    1188          35 :       if (n == 1 || (!sBORNE && BORNE)) return cgetg(1,t_VEC);
    1189          21 :       L = NULL; /* gcc -Wall */
    1190          21 :       break;
    1191             :     case min_ALL:
    1192         251 :       if (n == 1 || (!sBORNE && BORNE))
    1193           7 :         retmkvec3(gen_0, gen_0, cgetg(1, t_MAT));
    1194         244 :       L = new_chunk(1+maxrank);
    1195         244 :       break;
    1196             :     default:
    1197           0 :       return NULL;
    1198             :   }
    1199         538 :   minim_alloc(n, &q, &x, &y, &z, &v);
    1200             : 
    1201         538 :   forqfvec_init_dolll(&qv, a, dolll);
    1202         538 :   av1 = avma;
    1203         538 :   r = qv.r;
    1204         538 :   u = qv.u;
    1205         538 :   n--;
    1206        4715 :   for (j=1; j<=n; j++)
    1207             :   {
    1208        4177 :     v[j] = rtodbl(gcoeff(r,j,j));
    1209        4177 :     for (i=1; i<j; i++) q[i][j] = rtodbl(gcoeff(r,i,j)); /* |.| <= 1/2 */
    1210             :   }
    1211             : 
    1212         538 :   if (sBORNE) maxnorm = 0.;
    1213             :   else
    1214             :   {
    1215          56 :     GEN B = gcoeff(a,1,1);
    1216          56 :     long t = 1;
    1217         616 :     for (i=2; i<=n; i++)
    1218             :     {
    1219         560 :       GEN c = gcoeff(a,i,i);
    1220         560 :       if (cmpii(c, B) < 0) { B = c; t = i; }
    1221             :     }
    1222          56 :     if (flag == min_FIRST) return gerepilecopy(av, mkvec2(B, gel(u,t)));
    1223          49 :     maxnorm = -1.; /* don't update maxnorm */
    1224          49 :     if (is_bigint(B)) return NULL;
    1225          48 :     sBORNE = itos(B);
    1226             :   }
    1227         530 :   BOUND = sBORNE * (1 + eps);
    1228         530 :   if ((long)BOUND != sBORNE) return NULL;
    1229             : 
    1230         518 :   s = 0;
    1231         518 :   k = n; y[n] = z[n] = 0;
    1232         518 :   x[n] = (long)sqrt(BOUND/v[n]);
    1233     1030155 :   for(;;x[1]--)
    1234             :   {
    1235             :     do
    1236             :     {
    1237     1910727 :       if (k>1)
    1238             :       {
    1239      880565 :         long l = k-1;
    1240      880565 :         z[l] = 0;
    1241      880565 :         for (j=k; j<=n; j++) z[l] += q[l][j]*x[j];
    1242      880565 :         p = (double)x[k] + z[k];
    1243      880565 :         y[l] = y[k] + p*p*v[k];
    1244      880565 :         x[l] = (long)floor(sqrt((BOUND-y[l])/v[l])-z[l]);
    1245      880565 :         k = l;
    1246             :       }
    1247             :       for(;;)
    1248             :       {
    1249     2787841 :         p = (double)x[k] + z[k];
    1250     2787841 :         if (y[k] + p*p*v[k] <= BOUND) break;
    1251      877114 :         k++; x[k]--;
    1252      877114 :       }
    1253             :     }
    1254     1910727 :     while (k > 1);
    1255     1030673 :     if (! x[1] && y[1]<=eps) break;
    1256             : 
    1257     1030162 :     p = (double)x[1] + z[1]; p = y[1] + p*p*v[1]; /* norm(x) */
    1258     1030162 :     if (maxnorm >= 0)
    1259             :     {
    1260     1027614 :       if (p > maxnorm) maxnorm = p;
    1261             :     }
    1262             :     else
    1263             :     { /* maxnorm < 0 : only look for minimal vectors */
    1264        2548 :       pari_sp av2 = avma;
    1265        2548 :       gnorme = roundr(dbltor(p));
    1266        2548 :       if (cmpis(gnorme, sBORNE) >= 0) avma = av2;
    1267             :       else
    1268             :       {
    1269          14 :         sBORNE = itos(gnorme); avma = av1;
    1270          14 :         BOUND = sBORNE * (1+eps);
    1271          14 :         s = 0;
    1272             :       }
    1273             :     }
    1274     1030162 :     s++;
    1275             : 
    1276     1030162 :     switch(flag)
    1277             :     {
    1278             :       case min_FIRST:
    1279           7 :         if (dolll) x = ZM_zc_mul_canon(u,x);
    1280           7 :         return gerepilecopy(av, mkvec2(roundr(dbltor(p)), x));
    1281             : 
    1282             :       case min_ALL:
    1283      205030 :         if (s > maxrank && stockall) /* overflow */
    1284             :         {
    1285         406 :           long maxranknew = maxrank << 1;
    1286         406 :           GEN Lnew = new_chunk(1 + maxranknew);
    1287         406 :           for (i=1; i<=maxrank; i++) Lnew[i] = L[i];
    1288         406 :           L = Lnew; maxrank = maxranknew;
    1289             :         }
    1290      205030 :         if (s<=maxrank) gel(L,s) = leafcopy(x);
    1291      205030 :         break;
    1292             : 
    1293             :       case min_VECSMALL:
    1294       39200 :         { ulong norm = (ulong)(p + 0.5); L[norm]++; }
    1295       39200 :         break;
    1296             : 
    1297             :       case min_VECSMALL2:
    1298      785925 :         { ulong norm = (ulong)(p + 0.5); if (!odd(norm)) L[norm>>1]++; }
    1299      785925 :         break;
    1300             : 
    1301             :     }
    1302     1030155 :   }
    1303         511 :   switch(flag)
    1304             :   {
    1305             :     case min_FIRST:
    1306           7 :       avma = av; return cgetg(1,t_VEC);
    1307             :     case min_VECSMALL:
    1308             :     case min_VECSMALL2:
    1309         273 :       avma = (pari_sp)L; return L;
    1310             :   }
    1311         231 :   r = (maxnorm >= 0) ? roundr(dbltor(maxnorm)): stoi(sBORNE);
    1312         231 :   k = minss(s,maxrank);
    1313         231 :   L[0] = evaltyp(t_MAT) | evallg(k + 1);
    1314         231 :   if (dolll)
    1315         196 :     for (j=1; j<=k; j++) gel(L,j) = ZM_zc_mul_canon(u, gel(L,j));
    1316         231 :   return gerepilecopy(av, mkvec3(stoi(s<<1), r, L));
    1317             : }
    1318             : 
    1319             : static GEN
    1320         518 : minim0(GEN a, GEN BORNE, GEN STOCKMAX, long flag)
    1321             : {
    1322         518 :   GEN v = minim0_dolll(a, BORNE, STOCKMAX, flag, 1);
    1323         517 :   if (!v) pari_err_PREC("qfminim");
    1324         511 :   return v;
    1325             : }
    1326             : 
    1327             : GEN
    1328         273 : qfrep0(GEN a, GEN borne, long flag)
    1329         273 : { return minim0(a, borne, gen_0, (flag & 1)? min_VECSMALL2: min_VECSMALL); }
    1330             : 
    1331             : GEN
    1332         105 : qfminim0(GEN a, GEN borne, GEN stockmax, long flag, long prec)
    1333             : {
    1334         105 :   switch(flag)
    1335             :   {
    1336          42 :     case 0: return minim0(a,borne,stockmax,min_ALL);
    1337          35 :     case 1: return minim0(a,borne,gen_0   ,min_FIRST);
    1338             :     case 2:
    1339             :     {
    1340          28 :       long maxnum = -1;
    1341          28 :       if (typ(a) != t_MAT) pari_err_TYPE("qfminim",a);
    1342          28 :       if (stockmax) {
    1343          14 :         if (typ(stockmax) != t_INT) pari_err_TYPE("qfminim",stockmax);
    1344          14 :         maxnum = itos(stockmax);
    1345             :       }
    1346          28 :       a = fincke_pohst(a,borne,maxnum,prec,NULL);
    1347          28 :       if (!a) pari_err_PREC("qfminim");
    1348          28 :       return a;
    1349             :     }
    1350           0 :     default: pari_err_FLAG("qfminim");
    1351             :   }
    1352           0 :   return NULL; /* not reached */
    1353             : }
    1354             : 
    1355             : GEN
    1356         168 : minim(GEN a, GEN borne, GEN stockmax)
    1357         168 : { return minim0(a,borne,stockmax,min_ALL); }
    1358             : 
    1359             : GEN
    1360          42 : minim_raw(GEN a, GEN BORNE, GEN STOCKMAX)
    1361          42 : { return minim0_dolll(a, BORNE, STOCKMAX, min_ALL, 0); }
    1362             : 
    1363             : GEN
    1364           0 : minim2(GEN a, GEN borne, GEN stockmax)
    1365           0 : { return minim0(a,borne,stockmax,min_FIRST); }
    1366             : 
    1367             : /* If V depends linearly from the columns of the matrix, return 0.
    1368             :  * Otherwise, update INVP and L and return 1. No GC. */
    1369             : static int
    1370        1652 : addcolumntomatrix(GEN V, GEN invp, GEN L)
    1371             : {
    1372        1652 :   long i,j,k, n = lg(invp);
    1373        1652 :   GEN a = cgetg(n, t_COL), ak = NULL, mak;
    1374             : 
    1375       84231 :   for (k = 1; k < n; k++)
    1376       83706 :     if (!L[k])
    1377             :     {
    1378       27811 :       ak = RgMrow_zc_mul(invp, V, k);
    1379       27811 :       if (!gequal0(ak)) break;
    1380             :     }
    1381        1652 :   if (k == n) return 0;
    1382        1127 :   L[k] = 1;
    1383        1127 :   mak = gneg_i(ak);
    1384       43253 :   for (i=k+1; i<n; i++)
    1385       42126 :     gel(a,i) = gdiv(RgMrow_zc_mul(invp, V, i), mak);
    1386       43883 :   for (j=1; j<=k; j++)
    1387             :   {
    1388       42756 :     GEN c = gel(invp,j), ck = gel(c,k);
    1389       42756 :     if (gequal0(ck)) continue;
    1390        9471 :     gel(c,k) = gdiv(ck, ak);
    1391        9471 :     if (j==k)
    1392       43253 :       for (i=k+1; i<n; i++)
    1393       42126 :         gel(c,i) = gmul(gel(a,i), ck);
    1394             :     else
    1395      209979 :       for (i=k+1; i<n; i++)
    1396      201635 :         gel(c,i) = gadd(gel(c,i), gmul(gel(a,i), ck));
    1397             :   }
    1398        1127 :   return 1;
    1399             : }
    1400             : 
    1401             : GEN
    1402          42 : perf(GEN a)
    1403             : {
    1404          42 :   pari_sp av = avma;
    1405             :   GEN u, L;
    1406          42 :   long r, s, k, l, n = lg(a)-1;
    1407             : 
    1408          42 :   if (!n) return gen_0;
    1409          42 :   if (typ(a) != t_MAT || !RgM_is_ZM(a)) pari_err_TYPE("qfperfection",a);
    1410          42 :   a = minim_lll(a, &u);
    1411          42 :   L = minim_raw(a,NULL,NULL);
    1412          42 :   r = (n*(n+1)) >> 1;
    1413          42 :   if (L)
    1414             :   {
    1415             :     GEN D, V, invp;
    1416          35 :     L = gel(L, 3); l = lg(L);
    1417          35 :     if (l == 2) { avma = av; return gen_1; }
    1418             : 
    1419          21 :     D = zero_zv(r);
    1420          21 :     V = cgetg(r+1, t_VECSMALL);
    1421          21 :     invp = matid(r);
    1422          21 :     s = 0;
    1423        1659 :     for (k = 1; k < l; k++)
    1424             :     {
    1425        1652 :       pari_sp av2 = avma;
    1426        1652 :       GEN x = gel(L,k);
    1427             :       long i, j, I;
    1428       21098 :       for (i = I = 1; i<=n; i++)
    1429       19446 :         for (j=i; j<=n; j++,I++) V[I] = x[i]*x[j];
    1430        1652 :       if (!addcolumntomatrix(V,invp,D)) avma = av2;
    1431        1127 :       else if (++s == r) break;
    1432             :     }
    1433             :   }
    1434             :   else
    1435             :   {
    1436             :     GEN M;
    1437           7 :     L = fincke_pohst(a,NULL,-1, DEFAULTPREC, NULL);
    1438           7 :     if (!L) pari_err_PREC("qfminim");
    1439           7 :     L = gel(L, 3); l = lg(L);
    1440           7 :     if (l == 2) { avma = av; return gen_1; }
    1441           7 :     M = cgetg(l, t_MAT);
    1442         959 :     for (k = 1; k < l; k++)
    1443             :     {
    1444         952 :       GEN x = gel(L,k), c = cgetg(r+1, t_COL);
    1445             :       long i, I, j;
    1446       16184 :       for (i = I = 1; i<=n; i++)
    1447       15232 :         for (j=i; j<=n; j++,I++) gel(c,I) = mulii(gel(x,i), gel(x,j));
    1448         952 :       gel(M,k) = c;
    1449             :     }
    1450           7 :     s = ZM_rank(M);
    1451             :   }
    1452          28 :  avma = av; return utoipos(s);
    1453             : }
    1454             : 
    1455             : static GEN
    1456          64 : clonefill(GEN S, long s, long t)
    1457             : { /* initialize to dummy values */
    1458          64 :   GEN T = S, dummy = cgetg(1, t_STR);
    1459             :   long i;
    1460          64 :   for (i = s+1; i <= t; i++) gel(S,i) = dummy;
    1461          64 :   S = gclone(S); if (isclone(T)) gunclone(T);
    1462          64 :   return S;
    1463             : }
    1464             : 
    1465             : /* increment ZV x, by incrementing cell of index k. Initial value x0[k] was
    1466             :  * chosen to minimize qf(x) for given x0[1..k-1] and x0[k+1,..] = 0
    1467             :  * The last non-zero entry must be positive and goes through x0[k]+1,2,3,...
    1468             :  * Others entries go through: x0[k]+1,-1,2,-2,...*/
    1469             : INLINE void
    1470      915913 : step(GEN x, GEN y, GEN inc, long k)
    1471             : {
    1472      915913 :   if (!signe(gel(y,k))) /* x[k+1..] = 0 */
    1473        9625 :     gel(x,k) = addiu(gel(x,k), 1); /* leading coeff > 0 */
    1474             :   else
    1475             :   {
    1476      906288 :     long i = inc[k];
    1477      906288 :     gel(x,k) = addis(gel(x,k), i),
    1478      906288 :     inc[k] = (i > 0)? -1-i: 1-i;
    1479             :   }
    1480      915913 : }
    1481             : 
    1482             : /* 1 if we are "sure" that x < y, up to few rounding errors, i.e.
    1483             :  * x < y - epsilon. More precisely :
    1484             :  * - sign(x - y) < 0
    1485             :  * - lgprec(x-y) > 3 || expo(x - y) - expo(x) > -24 */
    1486             : static int
    1487      359544 : mplessthan(GEN x, GEN y)
    1488             : {
    1489      359544 :   pari_sp av = avma;
    1490      359544 :   GEN z = mpsub(x, y);
    1491      359544 :   avma = av;
    1492      359544 :   if (typ(z) == t_INT) return (signe(z) < 0);
    1493      359544 :   if (signe(z) >= 0) return 0;
    1494       58704 :   if (realprec(z) > LOWDEFAULTPREC) return 1;
    1495       58704 :   return ( expo(z) - mpexpo(x) > -24 );
    1496             : }
    1497             : 
    1498             : /* 1 if we are "sure" that x > y, up to few rounding errors, i.e.
    1499             :  * x > y + epsilon */
    1500             : static int
    1501     1449813 : mpgreaterthan(GEN x, GEN y)
    1502             : {
    1503     1449813 :   pari_sp av = avma;
    1504     1449813 :   GEN z = mpsub(x, y);
    1505     1449813 :   avma = av;
    1506     1449813 :   if (typ(z) == t_INT) return (signe(z) > 0);
    1507     1449813 :   if (signe(z) <= 0) return 0;
    1508      820416 :   if (realprec(z) > LOWDEFAULTPREC) return 1;
    1509      144563 :   return ( expo(z) - mpexpo(x) > -24 );
    1510             : }
    1511             : 
    1512             : /* x a t_INT, y  t_INT or t_REAL */
    1513             : INLINE GEN
    1514      358004 : mulimp(GEN x, GEN y)
    1515             : {
    1516      358004 :   if (typ(y) == t_INT) return mulii(x,y);
    1517      358004 :   return signe(x) ? mulir(x,y): gen_0;
    1518             : }
    1519             : /* x + y*z, x,z two mp's, y a t_INT */
    1520             : INLINE GEN
    1521     4506857 : addmulimp(GEN x, GEN y, GEN z)
    1522             : {
    1523     4506857 :   if (!signe(y)) return x;
    1524     1949089 :   if (typ(z) == t_INT) return mpadd(x, mulii(y, z));
    1525     1949089 :   return mpadd(x, mulir(y, z));
    1526             : }
    1527             : 
    1528             : /* yk + vk * (xk + zk)^2 */
    1529             : static GEN
    1530     1802832 : norm_aux(GEN xk, GEN yk, GEN zk, GEN vk)
    1531             : {
    1532     1802832 :   GEN t = mpadd(xk, zk);
    1533     1802832 :   if (typ(t) == t_INT) { /* probably gen_0, avoid loss of accuracy */
    1534       17859 :     yk = addmulimp(yk, sqri(t), vk);
    1535             :   } else {
    1536     1784973 :     yk = mpadd(yk, mpmul(sqrr(t), vk));
    1537             :   }
    1538     1802832 :   return yk;
    1539             : }
    1540             : /* yk + vk * (xk + zk)^2 < B + epsilon */
    1541             : static int
    1542     1273574 : check_bound(GEN B, GEN xk, GEN yk, GEN zk, GEN vk)
    1543             : {
    1544     1273574 :   pari_sp av = avma;
    1545     1273574 :   int f = mpgreaterthan(norm_aux(xk,yk,zk,vk), B);
    1546     1273574 :   avma = av; return !f;
    1547             : }
    1548             : 
    1549             : /* q(k-th canonical basis vector), where q is given in Cholesky form
    1550             :  * q(x) = sum_{i = 1}^n q[i,i] (x[i] + sum_{j > i} q[i,j] x[j])^2.
    1551             :  * Namely q(e_k) = q[k,k] + sum_{i < k} q[i,i] q[i,k]^2
    1552             :  * Assume 1 <= k <= n. */
    1553             : static GEN
    1554         182 : cholesky_norm_ek(GEN q, long k)
    1555             : {
    1556         182 :   GEN t = gcoeff(q,k,k);
    1557             :   long i;
    1558         182 :   for (i = 1; i < k; i++) t = norm_aux(gen_0, t, gcoeff(q,i,k), gcoeff(q,i,i));
    1559         182 :   return t;
    1560             : }
    1561             : 
    1562             : /* q is the Cholesky decomposition of a quadratic form
    1563             :  * Enumerate vectors whose norm is less than BORNE (Algo 2.5.7),
    1564             :  * minimal vectors if BORNE = NULL (implies check = NULL).
    1565             :  * If (check != NULL) consider only vectors passing the check, and assumes
    1566             :  *   we only want the smallest possible vectors */
    1567             : static GEN
    1568         874 : smallvectors(GEN q, GEN BORNE, long maxnum, FP_chk_fun *CHECK)
    1569             : {
    1570         874 :   long N = lg(q), n = N-1, i, j, k, s, stockmax, checkcnt = 1;
    1571             :   pari_sp av, av1;
    1572             :   GEN inc, S, x, y, z, v, p1, alpha, norms;
    1573             :   GEN norme1, normax1, borne1, borne2;
    1574         874 :   GEN (*check)(void *,GEN) = CHECK? CHECK->f: NULL;
    1575         874 :   void *data = CHECK? CHECK->data: NULL;
    1576         874 :   const long skipfirst = CHECK? CHECK->skipfirst: 0;
    1577         874 :   const int stockall = (maxnum == -1);
    1578             : 
    1579         874 :   alpha = dbltor(0.95);
    1580         874 :   normax1 = gen_0;
    1581             : 
    1582         874 :   v = cgetg(N,t_VEC);
    1583         874 :   inc = const_vecsmall(n, 1);
    1584             : 
    1585         874 :   av = avma;
    1586         874 :   stockmax = stockall? 2000: maxnum;
    1587         874 :   norms = cgetg(check?(stockmax+1): 1,t_VEC); /* unused if (!check) */
    1588         874 :   S = cgetg(stockmax+1,t_VEC);
    1589         874 :   x = cgetg(N,t_COL);
    1590         874 :   y = cgetg(N,t_COL);
    1591         874 :   z = cgetg(N,t_COL);
    1592        5162 :   for (i=1; i<N; i++) {
    1593        4288 :     gel(v,i) = gcoeff(q,i,i);
    1594        4288 :     gel(x,i) = gel(y,i) = gel(z,i) = gen_0;
    1595             :   }
    1596         874 :   if (BORNE)
    1597             :   {
    1598         860 :     borne1 = BORNE;
    1599         860 :     if (typ(borne1) != t_REAL)
    1600             :     {
    1601             :       long prec;
    1602         517 :       if (gequal0(borne1)) retmkvec3(gen_0, gen_0, cgetg(1,t_MAT));
    1603         510 :       prec = nbits2prec(gexpo(borne1) + 10);
    1604         510 :       borne1 = gtofp(borne1, maxss(prec, DEFAULTPREC));
    1605             :     }
    1606             :   }
    1607             :   else
    1608             :   {
    1609          14 :     borne1 = gcoeff(q,1,1);
    1610         196 :     for (i=2; i<N; i++)
    1611             :     {
    1612         182 :       GEN b = cholesky_norm_ek(q, i);
    1613         182 :       if (gcmp(b, borne1) < 0) borne1 = b;
    1614             :     }
    1615             :     /* borne1 = norm of smallest basis vector */
    1616             :   }
    1617         867 :   borne2 = mulrr(borne1,alpha);
    1618         867 :   if (DEBUGLEVEL>2)
    1619           0 :     err_printf("smallvectors looking for norm < %P.4G\n",borne1);
    1620         867 :   s = 0; k = n;
    1621      170476 :   for(;; step(x,y,inc,k)) /* main */
    1622             :   { /* x (supposedly) small vector, ZV.
    1623             :      * For all t >= k, we have
    1624             :      *   z[t] = sum_{j > t} q[t,j] * x[j]
    1625             :      *   y[t] = sum_{i > t} q[i,i] * (x[i] + z[i])^2
    1626             :      *        = 0 <=> x[i]=0 for all i>t */
    1627             :     do
    1628             :     {
    1629      528480 :       int skip = 0;
    1630      528480 :       if (k > 1)
    1631             :       {
    1632      358004 :         long l = k-1;
    1633      358004 :         av1 = avma;
    1634      358004 :         p1 = mulimp(gel(x,k), gcoeff(q,l,k));
    1635      358004 :         for (j=k+1; j<N; j++) p1 = addmulimp(p1, gel(x,j), gcoeff(q,l,j));
    1636      358004 :         gel(z,l) = gerepileuptoleaf(av1,p1);
    1637             : 
    1638      358004 :         av1 = avma;
    1639      358004 :         p1 = norm_aux(gel(x,k), gel(y,k), gel(z,k), gel(v,k));
    1640      358004 :         gel(y,l) = gerepileuptoleaf(av1, p1);
    1641             :         /* skip the [x_1,...,x_skipfirst,0,...,0] */
    1642      358004 :         if ((l <= skipfirst && !signe(gel(y,skipfirst)))
    1643      357661 :          || mplessthan(borne1, gel(y,l))) skip = 1;
    1644             :         else /* initial value, minimizing (x[l] + z[l])^2, hence qf(x) for
    1645             :                 the given x[1..l-1] */
    1646      357661 :           gel(x,l) = mpround( mpneg(gel(z,l)) );
    1647      358004 :         k = l;
    1648             :       }
    1649      358004 :       for(;; step(x,y,inc,k))
    1650             :       { /* at most 2n loops */
    1651      886484 :         if (!skip)
    1652             :         {
    1653      886141 :           if (check_bound(borne1, gel(x,k),gel(y,k),gel(z,k),gel(v,k))) break;
    1654      387433 :           step(x,y,inc,k);
    1655      387433 :           if (check_bound(borne1, gel(x,k),gel(y,k),gel(z,k),gel(v,k))) break;
    1656             :         }
    1657      358871 :         skip = 0; inc[k] = 1;
    1658      358871 :         if (++k > n) goto END;
    1659      358004 :       }
    1660             : 
    1661      527613 :       if (gc_needed(av,2))
    1662             :       {
    1663           8 :         if(DEBUGMEM>1) pari_warn(warnmem,"smallvectors");
    1664           8 :         if (stockmax) S = clonefill(S, s, stockmax);
    1665           8 :         if (check) {
    1666           8 :           GEN dummy = cgetg(1, t_STR);
    1667           8 :           for (i=s+1; i<=stockmax; i++) gel(norms,i) = dummy;
    1668             :         }
    1669           8 :         gerepileall(av,7,&x,&y,&z,&normax1,&borne1,&borne2,&norms);
    1670             :       }
    1671             :     }
    1672      527613 :     while (k > 1);
    1673      170476 :     if (!signe(gel(x,1)) && !signe(gel(y,1))) continue; /* exclude 0 */
    1674             : 
    1675      169952 :     av1 = avma;
    1676      169952 :     norme1 = norm_aux(gel(x,1),gel(y,1),gel(z,1),gel(v,1));
    1677      169952 :     if (mpgreaterthan(norme1,borne1)) { avma = av1; continue; /* main */ }
    1678             : 
    1679      169952 :     norme1 = gerepileuptoleaf(av1,norme1);
    1680      169952 :     if (check)
    1681             :     {
    1682      101373 :       if (checkcnt < 5 && mpcmp(norme1, borne2) < 0)
    1683             :       {
    1684         517 :         if (!check(data,x)) { checkcnt++ ; continue; /* main */}
    1685         265 :         if (DEBUGLEVEL>4) err_printf("New bound: %Ps", norme1);
    1686         265 :         borne1 = norme1;
    1687         265 :         borne2 = mulrr(borne1, alpha);
    1688         265 :         s = 0; checkcnt = 0;
    1689             :       }
    1690             :     }
    1691             :     else
    1692             :     {
    1693       68579 :       if (!BORNE) /* find minimal vectors */
    1694             :       {
    1695        1883 :         if (mplessthan(norme1, borne1))
    1696             :         { /* strictly smaller vector than previously known */
    1697           0 :           borne1 = norme1; /* + epsilon */
    1698           0 :           s = 0;
    1699             :         }
    1700             :       }
    1701             :       else
    1702       66696 :         if (mpcmp(norme1,normax1) > 0) normax1 = norme1;
    1703             :     }
    1704      169700 :     if (++s > stockmax) continue; /* too many vectors: no longer remember */
    1705      168769 :     if (check) gel(norms,s) = norme1;
    1706      168769 :     gel(S,s) = leafcopy(x);
    1707      168769 :     if (s != stockmax) continue; /* still room, get next vector */
    1708             : 
    1709             :     /* overflow, eliminate vectors failing "check" */
    1710          56 :     if (check)
    1711             :     {
    1712          35 :       pari_sp av2 = avma;
    1713             :       long imin, imax;
    1714          35 :       GEN per = indexsort(norms);
    1715          35 :       if (DEBUGLEVEL>2) err_printf("sorting... [%ld elts]\n",s);
    1716             :       /* let N be the minimal norm so far for x satisfying 'check'. Keep
    1717             :        * all elements of norm N */
    1718        3675 :       for (i = 1; i <= s; i++)
    1719             :       {
    1720        3675 :         long k = per[i];
    1721        3675 :         if (check(data,gel(S,k))) { borne1 = gel(norms,k); break; }
    1722             :       }
    1723          35 :       imin = i;
    1724         273 :       for (; i <= s; i++)
    1725         273 :         if (mpgreaterthan(gel(norms,per[i]), borne1)) break;
    1726          35 :       imax = i;
    1727          35 :       for (i=imin, s=0; i < imax; i++) gel(S,++s) = gel(S,per[i]);
    1728          35 :       avma = av2;
    1729          35 :       if (s)
    1730             :       {
    1731          35 :         borne2 = mulrr(borne1, alpha);
    1732          35 :         checkcnt = 0;
    1733             :       }
    1734          35 :       if (!stockall) continue;
    1735          35 :       if (s > stockmax/2) stockmax <<= 1;
    1736          35 :       norms = cgetg(stockmax+1, t_VEC);
    1737          35 :       for (i = 1; i <= s; i++) gel(norms,i) = borne1;
    1738             :     }
    1739             :     else
    1740             :     {
    1741          21 :       if (!stockall && BORNE) goto END;
    1742          21 :       if (!stockall) continue;
    1743          21 :       stockmax <<= 1;
    1744             :     }
    1745             : 
    1746             :     {
    1747          56 :       GEN Snew = cgetg(stockmax + 1, t_VEC);
    1748          56 :       for (i = 1; i <= s; i++) gel(Snew,i) = gel(S,i);
    1749          56 :       Snew = clonefill(Snew, s, stockmax);
    1750          56 :       if (isclone(S)) gunclone(S);
    1751          56 :       S = Snew;
    1752             :     }
    1753      170476 :   }
    1754             : END:
    1755         867 :   if (s < stockmax) stockmax = s;
    1756         867 :   if (check)
    1757             :   {
    1758             :     GEN per, alph, pols, p;
    1759         846 :     if (DEBUGLEVEL>2) err_printf("final sort & check...\n");
    1760         846 :     setlg(norms,stockmax+1); per = indexsort(norms);
    1761         846 :     alph = cgetg(stockmax+1,t_VEC);
    1762         846 :     pols = cgetg(stockmax+1,t_VEC);
    1763        7777 :     for (j=0,i=1; i<=stockmax; i++)
    1764             :     {
    1765        6951 :       long t = per[i];
    1766        6951 :       GEN N = gel(norms,t);
    1767        6951 :       if (j && mpgreaterthan(N, borne1)) break;
    1768        6931 :       if ((p = check(data,gel(S,t))))
    1769             :       {
    1770        6280 :         if (!j) borne1 = N;
    1771        6280 :         j++;
    1772        6280 :         gel(pols,j) = p;
    1773        6280 :         gel(alph,j) = gel(S,t);
    1774             :       }
    1775             :     }
    1776         846 :     setlg(pols,j+1);
    1777         846 :     setlg(alph,j+1);
    1778         846 :     if (stockmax && isclone(S)) { alph = gcopy(alph); gunclone(S); }
    1779         846 :     return mkvec2(pols, alph);
    1780             :   }
    1781          21 :   if (stockmax)
    1782             :   {
    1783          14 :     setlg(S,stockmax+1);
    1784          14 :     settyp(S,t_MAT);
    1785          14 :     if (isclone(S)) { p1 = S; S = gcopy(S); gunclone(p1); }
    1786             :   }
    1787             :   else
    1788           7 :     S = cgetg(1,t_MAT);
    1789          21 :   return mkvec3(utoi(s<<1), borne1, S);
    1790             : }
    1791             : 
    1792             : /* solve q(x) = x~.a.x <= bound, a > 0.
    1793             :  * If check is non-NULL keep x only if check(x).
    1794             :  * If a is a vector, assume a[1] is the LLL-reduced Cholesky form of q */
    1795             : GEN
    1796         889 : fincke_pohst(GEN a, GEN B0, long stockmax, long PREC, FP_chk_fun *CHECK)
    1797             : {
    1798         889 :   pari_sp av = avma;
    1799             :   VOLATILE long i,j,l;
    1800         889 :   VOLATILE GEN r,rinv,rinvtrans,u,v,res,z,vnorm,rperm,perm,uperm, bound = B0;
    1801             : 
    1802         889 :   if (typ(a) == t_VEC)
    1803             :   {
    1804         351 :     r = gel(a,1);
    1805         351 :     u = NULL;
    1806             :   }
    1807             :   else
    1808             :   {
    1809         538 :     long prec = PREC;
    1810         538 :     l = lg(a);
    1811         538 :     if (l == 1)
    1812             :     {
    1813           7 :       if (CHECK) pari_err_TYPE("fincke_pohst [dimension 0]", a);
    1814           7 :       retmkvec3(gen_0, gen_0, cgetg(1,t_MAT));
    1815             :     }
    1816         531 :     u = lllfp(a, 0.75, LLL_GRAM);
    1817         531 :     if (lg(u) != lg(a)) return NULL;
    1818         531 :     r = qf_apply_RgM(a,u);
    1819         531 :     i = gprecision(r);
    1820         531 :     if (i)
    1821         503 :       prec = i;
    1822             :     else {
    1823          28 :       prec = DEFAULTPREC + nbits2extraprec(gexpo(r));
    1824          28 :       if (prec < PREC) prec = PREC;
    1825             :     }
    1826         531 :     if (DEBUGLEVEL>2) err_printf("first LLL: prec = %ld\n", prec);
    1827         531 :     r = qfgaussred_positive(r);
    1828         531 :     if (!r) return NULL;
    1829        2362 :     for (i=1; i<l; i++)
    1830             :     {
    1831        1831 :       GEN s = gsqrt(gcoeff(r,i,i), prec);
    1832        1831 :       gcoeff(r,i,i) = s;
    1833        1831 :       for (j=i+1; j<l; j++) gcoeff(r,i,j) = gmul(s, gcoeff(r,i,j));
    1834             :     }
    1835             :   }
    1836             :   /* now r~ * r = a in LLL basis */
    1837         882 :   rinv = RgM_inv_upper(r);
    1838         882 :   if (!rinv) return NULL;
    1839         882 :   rinvtrans = shallowtrans(rinv);
    1840         882 :   if (DEBUGLEVEL>2)
    1841           0 :     err_printf("Fincke-Pohst, final LLL: prec = %ld\n", gprecision(rinvtrans));
    1842         882 :   v = lll(rinvtrans);
    1843         882 :   if (lg(v) != lg(rinvtrans)) return NULL;
    1844             : 
    1845         882 :   rinvtrans = RgM_mul(rinvtrans, v);
    1846         882 :   v = ZM_inv(shallowtrans(v),gen_1);
    1847         882 :   r = RgM_mul(r,v);
    1848         882 :   u = u? ZM_mul(u,v): v;
    1849             : 
    1850         882 :   l = lg(r);
    1851         882 :   vnorm = cgetg(l,t_VEC);
    1852         882 :   for (j=1; j<l; j++) gel(vnorm,j) = gnorml2(gel(rinvtrans,j));
    1853         882 :   rperm = cgetg(l,t_MAT);
    1854         882 :   uperm = cgetg(l,t_MAT); perm = indexsort(vnorm);
    1855         882 :   for (i=1; i<l; i++) { uperm[l-i] = u[perm[i]]; rperm[l-i] = r[perm[i]]; }
    1856         882 :   u = uperm;
    1857         882 :   r = rperm; res = NULL;
    1858         882 :   pari_CATCH(e_PREC) { }
    1859             :   pari_TRY {
    1860             :     GEN q;
    1861         882 :     if (CHECK && CHECK->f_init) bound = CHECK->f_init(CHECK, r, u);
    1862         874 :     q = gaussred_from_QR(r, gprecision(vnorm));
    1863         874 :     if (!q) pari_err_PREC("fincke_pohst");
    1864         874 :     res = smallvectors(q, bound, stockmax, CHECK);
    1865         874 :   } pari_ENDCATCH;
    1866         882 :   if (CHECK)
    1867             :   {
    1868         854 :     if (CHECK->f_post) res = CHECK->f_post(CHECK, res, u);
    1869         854 :     return res;
    1870             :   }
    1871          28 :   if (!res) pari_err_PREC("fincke_pohst");
    1872             : 
    1873          28 :   z = cgetg(4,t_VEC);
    1874          28 :   gel(z,1) = gcopy(gel(res,1));
    1875          28 :   gel(z,2) = gcopy(gel(res,2));
    1876          28 :   gel(z,3) = ZM_mul(u, gel(res,3)); return gerepileupto(av,z);
    1877             : }

Generated by: LCOV version 1.11