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 to exceed 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.18.0 lcov report (development 29804-254f602fce) Lines: 1188 1260 94.3 %
Date: 2024-12-18 09:08:59 Functions: 74 80 92.5 %
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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /********************************************************************/
      16             : /**                                                                **/
      17             : /**                 LLL Algorithm and close friends                **/
      18             : /**                                                                **/
      19             : /********************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : #define DEBUGLEVEL DEBUGLEVEL_qf
      24             : 
      25             : /********************************************************************/
      26             : /**             QR Factorization via Householder matrices          **/
      27             : /********************************************************************/
      28             : static int
      29    24488141 : no_prec_pb(GEN x)
      30             : {
      31    24413304 :   return (typ(x) != t_REAL || realprec(x) > DEFAULTPREC
      32    48901445 :                            || expo(x) < DEFAULTPREC>>1);
      33             : }
      34             : /* Find a Householder transformation which, applied to x[k..#x], zeroes
      35             :  * x[k+1..#x]; fill L = (mu_{i,j}). Return 0 if precision problem [obtained
      36             :  * a 0 vector], 1 otherwise */
      37             : static int
      38    24497093 : FindApplyQ(GEN x, GEN L, GEN B, long k, GEN Q, long prec)
      39             : {
      40    24497093 :   long i, lx = lg(x)-1;
      41    24497093 :   GEN x2, x1, xd = x + (k-1);
      42             : 
      43    24497093 :   x1 = gel(xd,1);
      44    24497093 :   x2 = mpsqr(x1);
      45    24496113 :   if (k < lx)
      46             :   {
      47    19305810 :     long lv = lx - (k-1) + 1;
      48    19305810 :     GEN beta, Nx, v = cgetg(lv, t_VEC);
      49    76219525 :     for (i=2; i<lv; i++) {
      50    56914178 :       x2 = mpadd(x2, mpsqr(gel(xd,i)));
      51    56913334 :       gel(v,i) = gel(xd,i);
      52             :     }
      53    19305347 :     if (!signe(x2)) return 0;
      54    19297194 :     Nx = gsqrt(x2, prec); if (signe(x1) < 0) setsigne(Nx, -1);
      55    19298282 :     gel(v,1) = mpadd(x1, Nx);
      56             : 
      57    19297411 :     if (!signe(x1))
      58      728813 :       beta = gtofp(x2, prec); /* make sure typ(beta) != t_INT */
      59             :     else
      60    18568598 :       beta = mpadd(x2, mpmul(Nx,x1));
      61    19297674 :     gel(Q,k) = mkvec2(invr(beta), v);
      62             : 
      63    19297954 :     togglesign(Nx);
      64    19297661 :     gcoeff(L,k,k) = Nx;
      65             :   }
      66             :   else
      67     5190303 :     gcoeff(L,k,k) = gel(x,k);
      68    24487964 :   gel(B,k) = x2;
      69    70161815 :   for (i=1; i<k; i++) gcoeff(L,k,i) = gel(x,i);
      70    24487964 :   return no_prec_pb(x2);
      71             : }
      72             : 
      73             : /* apply Householder transformation Q = [beta,v] to r with t_INT/t_REAL
      74             :  * coefficients, in place: r -= ((0|v).r * beta) v */
      75             : static void
      76    45683349 : ApplyQ(GEN Q, GEN r)
      77             : {
      78    45683349 :   GEN s, rd, beta = gel(Q,1), v = gel(Q,2);
      79    45683349 :   long i, l = lg(v), lr = lg(r);
      80             : 
      81    45683349 :   rd = r + (lr - l);
      82    45683349 :   s = mpmul(gel(v,1), gel(rd,1));
      83   473379062 :   for (i=2; i<l; i++) s = mpadd(s, mpmul(gel(v,i) ,gel(rd,i)));
      84    45679221 :   s = mpmul(beta, s);
      85   519246682 :   for (i=1; i<l; i++)
      86   473563920 :     if (signe(gel(v,i))) gel(rd,i) = mpsub(gel(rd,i), mpmul(s, gel(v,i)));
      87    45682762 : }
      88             : /* apply Q[1], ..., Q[j-1] to r */
      89             : static GEN
      90    16824309 : ApplyAllQ(GEN Q, GEN r, long j)
      91             : {
      92    16824309 :   pari_sp av = avma;
      93             :   long i;
      94    16824309 :   r = leafcopy(r);
      95    62505082 :   for (i=1; i<j; i++) ApplyQ(gel(Q,i), r);
      96    16822038 :   return gerepilecopy(av, r);
      97             : }
      98             : 
      99             : /* same, arbitrary coefficients [20% slower for t_REAL at DEFAULTPREC] */
     100             : static void
     101       22113 : RgC_ApplyQ(GEN Q, GEN r)
     102             : {
     103       22113 :   GEN s, rd, beta = gel(Q,1), v = gel(Q,2);
     104       22113 :   long i, l = lg(v), lr = lg(r);
     105             : 
     106       22113 :   rd = r + (lr - l);
     107       22113 :   s = gmul(gel(v,1), gel(rd,1));
     108      464373 :   for (i=2; i<l; i++) s = gadd(s, gmul(gel(v,i) ,gel(rd,i)));
     109       22113 :   s = gmul(beta, s);
     110      486486 :   for (i=1; i<l; i++)
     111      464373 :     if (signe(gel(v,i))) gel(rd,i) = gsub(gel(rd,i), gmul(s, gel(v,i)));
     112       22113 : }
     113             : static GEN
     114         567 : RgC_ApplyAllQ(GEN Q, GEN r, long j)
     115             : {
     116         567 :   pari_sp av = avma;
     117             :   long i;
     118         567 :   r = leafcopy(r);
     119       22680 :   for (i=1; i<j; i++) RgC_ApplyQ(gel(Q,i), r);
     120         567 :   return gerepilecopy(av, r);
     121             : }
     122             : 
     123             : int
     124          21 : RgM_QR_init(GEN x, GEN *pB, GEN *pQ, GEN *pL, long prec)
     125             : {
     126          21 :   x = RgM_gtomp(x, prec);
     127          21 :   return QR_init(x, pB, pQ, pL, prec);
     128             : }
     129             : 
     130             : static void
     131          35 : check_householder(GEN Q)
     132             : {
     133          35 :   long i, l = lg(Q);
     134          35 :   if (typ(Q) != t_VEC) pari_err_TYPE("mathouseholder", Q);
     135         854 :   for (i = 1; i < l; i++)
     136             :   {
     137         826 :     GEN q = gel(Q,i), v;
     138         826 :     if (typ(q) != t_VEC || lg(q) != 3) pari_err_TYPE("mathouseholder", Q);
     139         826 :     v = gel(q,2);
     140         826 :     if (typ(v) != t_VEC || lg(v)+i-2 != l) pari_err_TYPE("mathouseholder", Q);
     141             :   }
     142          28 : }
     143             : 
     144             : GEN
     145          35 : mathouseholder(GEN Q, GEN x)
     146             : {
     147          35 :   long l = lg(Q);
     148          35 :   check_householder(Q);
     149          28 :   switch(typ(x))
     150             :   {
     151          14 :     case t_MAT:
     152          14 :       if (lg(x) == 1) return cgetg(1, t_MAT);
     153          14 :       if (lgcols(x) != l+1) pari_err_TYPE("mathouseholder", x);
     154         574 :       pari_APPLY_same(RgC_ApplyAllQ(Q, gel(x,i), l));
     155           7 :     case t_COL:
     156           7 :       if (lg(x) == l+1) return RgC_ApplyAllQ(Q, x, l);
     157             :   }
     158           7 :   pari_err_TYPE("mathouseholder", x);
     159             :   return NULL; /* LCOV_EXCL_LINE */
     160             : }
     161             : 
     162             : GEN
     163          35 : matqr(GEN x, long flag, long prec)
     164             : {
     165          35 :   pari_sp av = avma;
     166             :   GEN B, Q, L;
     167          35 :   long n = lg(x)-1;
     168          35 :   if (typ(x) != t_MAT) pari_err_TYPE("matqr",x);
     169          35 :   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          21 :   if (n != nbrows(x)) pari_err_DIM("matqr");
     175          21 :   if (!RgM_QR_init(x, &B,&Q,&L, prec)) pari_err_PREC("matqr");
     176          21 :   if (!flag) Q = shallowtrans(mathouseholder(Q, matid(n)));
     177          21 :   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     7672425 : QR_init(GEN x, GEN *pB, GEN *pQ, GEN *pL, long prec)
     183             : {
     184     7672425 :   long j, k = lg(x)-1;
     185     7672425 :   GEN B = cgetg(k+1, t_VEC), Q = cgetg(k, t_VEC), L = zeromatcopy(k,k);
     186    29957739 :   for (j=1; j<=k; j++)
     187             :   {
     188    24496658 :     GEN r = gel(x,j);
     189    24496658 :     if (j > 1) r = ApplyAllQ(Q, r, j);
     190    24497058 :     if (!FindApplyQ(r, L, B, j, Q, prec)) return 0;
     191             :   }
     192     5461081 :   *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      297372 : gaussred_from_QR(GEN x, long prec)
     198             : {
     199      297372 :   long j, k = lg(x)-1;
     200             :   GEN B, Q, L;
     201      297372 :   if (!QR_init(x, &B,&Q,&L, prec)) return NULL;
     202     1062110 :   for (j=1; j<k; j++)
     203             :   {
     204      764725 :     GEN m = gel(L,j), invNx = invr(gel(m,j));
     205             :     long i;
     206      764723 :     gel(m,j) = gel(B,j);
     207     2959990 :     for (i=j+1; i<=k; i++) gel(m,i) = mpmul(invNx, gel(m,i));
     208             :   }
     209      297385 :   gcoeff(L,j,j) = gel(B,j);
     210      297385 :   return shallowtrans(L);
     211             : }
     212             : GEN
     213       14259 : R_from_QR(GEN x, long prec)
     214             : {
     215             :   GEN B, Q, L;
     216       14259 :   if (!QR_init(x, &B,&Q,&L, prec)) return NULL;
     217       14245 :   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       47784 : RgM_gram_schmidt(GEN e, GEN *ptB)
     228             : {
     229       47784 :   long i,j,lx = lg(e);
     230       47784 :   GEN f = RgM_shallowcopy(e), B, iB;
     231             : 
     232       47785 :   B = cgetg(lx, t_VEC);
     233       47785 :   iB= cgetg(lx, t_VEC);
     234             : 
     235      102220 :   for (i=1;i<lx;i++)
     236             :   {
     237       54436 :     GEN p1 = NULL;
     238       54436 :     pari_sp av = avma;
     239      116501 :     for (j=1; j<i; j++)
     240             :     {
     241       62065 :       GEN mu = gmul(RgV_dotproduct(gel(e,i),gel(f,j)), gel(iB,j));
     242       62065 :       GEN p2 = gmul(mu, gel(f,j));
     243       62065 :       p1 = p1? gadd(p1,p2): p2;
     244             :     }
     245       54436 :     p1 = p1? gerepileupto(av, gsub(gel(e,i), p1)): gel(e,i);
     246       54436 :     gel(f,i) = p1;
     247       54436 :     gel(B,i) = RgV_dotsquare(gel(f,i));
     248       54436 :     gel(iB,i) = ginv(gel(B,i));
     249             :   }
     250       47784 :   *ptB = B; return f;
     251             : }
     252             : 
     253             : /* B a Z-basis (which the caller should LLL-reduce for efficiency), t a vector.
     254             :  * Apply Babai's nearest plane algorithm to (B,t) */
     255             : GEN
     256       47784 : RgM_Babai(GEN B, GEN t)
     257             : {
     258       47784 :   GEN C, N, G = RgM_gram_schmidt(B, &N), b = t;
     259       47784 :   long j, n = lg(B)-1;
     260             : 
     261       47784 :   C = cgetg(n+1,t_COL);
     262      102221 :   for (j = n; j > 0; j--)
     263             :   {
     264       54436 :     GEN c = gdiv( RgV_dotproduct(b, gel(G,j)), gel(N,j) );
     265             :     long e;
     266       54436 :     c = grndtoi(c,&e);
     267       54436 :     if (e >= 0) return NULL;
     268       54436 :     if (signe(c)) b = RgC_sub(b, RgC_Rg_mul(gel(B,j), c));
     269       54436 :     gel(C,j) = c;
     270             :   }
     271       47785 :   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          49 :     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          35 :           gcoeff(tm1,1,i) = addii(mulii(q1neg, gcoeff(tm,1,1)),
     387          35 :                                   mulii(q2neg, gcoeff(tm,1,2)));
     388          35 :           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          35 :     {
     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         700 :           gcoeff(dot,k1,k1) = addii(gcoeff(dot,k1,k1),
     440         700 :                                     mulii(q, gcoeff(dot,k2,k1)));
     441        5600 :           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         280 :       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             :     } /* 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(double b, double x, double rho, long d, long dim, long delta, long t)
     494             : {
     495         882 :   double cond = delta * (d * (delta+1) - 2*b*dim + rho * (delta-1 + 2*t))
     496         882 :                 + x*dim*(dim - 1);
     497         882 :   if (DEBUGLEVEL >= 4)
     498           0 :     err_printf("delta = %d, t = %d (%.1lf)\n", delta, t, cond);
     499         882 :   return (cond <= 0);
     500             : }
     501             : 
     502             : static void
     503          21 : choose_params(GEN P, GEN N, GEN X, GEN B, long *pdelta, long *pt)
     504             : {
     505          21 :   long d = degpol(P), dim;
     506          21 :   GEN P0 = leading_coeff(P);
     507          21 :   double logN = gtodouble(glog(N, DEFAULTPREC)), x, b, rho;
     508          21 :   x = gtodouble(glog(X, DEFAULTPREC)) / logN;
     509          21 :   b = B? gtodouble(glog(B, DEFAULTPREC)) / logN: 1.;
     510          21 :   if (x * d >= b * b) pari_err_OVERFLOW("zncoppersmith [bound too large]");
     511             :   /* TODO : remove P0 completely */
     512          14 :   rho = is_pm1(P0)? 0: gtodouble(glog(P0, DEFAULTPREC)) / logN;
     513             : 
     514             :   /* Enumerate (delta,t) by increasing lattice dimension */
     515          14 :   for(dim = d + 1;; dim++)
     516         161 :   {
     517             :     long delta, t; /* dim = d*delta + t in the loop */
     518        1043 :     for (delta = 0, t = dim; t >= 0; delta++, t -= d)
     519         882 :       if (check(b,x,rho,d,dim,delta,t)) { *pdelta = delta; *pt = t; return; }
     520             :   }
     521             : }
     522             : 
     523             : static int
     524       14021 : sol_OK(GEN x, GEN N, GEN B)
     525       14021 : { return B? (cmpii(gcdii(x,N),B) >= 0): dvdii(x,N); }
     526             : /* deg(P) > 0, x >= 0. Find all j such that gcd(P(j), N) >= B, |j| <= x */
     527             : static GEN
     528           7 : do_exhaustive(GEN P, GEN N, long x, GEN B)
     529             : {
     530           7 :   GEN Pe, Po, sol = vecsmalltrunc_init(2*x + 2);
     531             :   pari_sp av;
     532             :   long j;
     533           7 :   RgX_even_odd(P, &Pe,&Po); av = avma;
     534           7 :   if (sol_OK(gel(P,2), N,B)) vecsmalltrunc_append(sol, 0);
     535        7007 :   for (j = 1; j <= x; j++, set_avma(av))
     536             :   {
     537        7000 :     GEN j2 = sqru(j), E = FpX_eval(Pe,j2,N), O = FpX_eval(Po,j2,N);
     538        7000 :     if (sol_OK(addmuliu(E,O,j), N,B)) vecsmalltrunc_append(sol, j);
     539        7000 :     if (sol_OK(submuliu(E,O,j), N,B)) vecsmalltrunc_append(sol,-j);
     540             :   }
     541           7 :   vecsmall_sort(sol); return zv_to_ZV(sol);
     542             : }
     543             : 
     544             : /* General Coppersmith, look for a root x0 <= p, p >= B, p | N, |x0| <= X.
     545             :  * B = N coded as NULL */
     546             : GEN
     547          35 : zncoppersmith(GEN P, GEN N, GEN X, GEN B)
     548             : {
     549             :   GEN Q, R, N0, M, sh, short_pol, *Xpowers, sol, nsp, cP, Z;
     550          35 :   long delta, i, j, row, d, l, t, dim, bnd = 10;
     551          35 :   const ulong X_SMALL = 1000;
     552          35 :   pari_sp av = avma;
     553             : 
     554          35 :   if (typ(P) != t_POL || !RgX_is_ZX(P)) pari_err_TYPE("zncoppersmith",P);
     555          28 :   if (typ(N) != t_INT) pari_err_TYPE("zncoppersmith",N);
     556          28 :   if (typ(X) != t_INT) {
     557           7 :     X = gfloor(X);
     558           7 :     if (typ(X) != t_INT) pari_err_TYPE("zncoppersmith",X);
     559             :   }
     560          28 :   if (signe(X) < 0) pari_err_DOMAIN("zncoppersmith", "X", "<", gen_0, X);
     561          28 :   P = FpX_red(P, N); d = degpol(P);
     562          28 :   if (d == 0) { set_avma(av); return cgetg(1, t_VEC); }
     563          28 :   if (d < 0) pari_err_ROOTS0("zncoppersmith");
     564          28 :   if (B && typ(B) != t_INT) B = gceil(B);
     565          28 :   if (abscmpiu(X, X_SMALL) <= 0)
     566           7 :     return gerepileupto(av, do_exhaustive(P, N, itos(X), B));
     567             : 
     568          21 :   if (B && equalii(B,N)) B = NULL;
     569          21 :   if (B) bnd = 1; /* bnd-hack is only for the case B = N */
     570          21 :   cP = gel(P,d+2);
     571          21 :   if (!gequal1(cP))
     572             :   {
     573             :     GEN r, z;
     574          14 :     gel(P,d+2) = cP = bezout(cP, N, &z, &r);
     575          35 :     for (j = 0; j < d; j++) gel(P,j+2) = Fp_mul(gel(P,j+2), z, N);
     576          14 :     if (!is_pm1(cP))
     577             :     {
     578           7 :       P = Q_primitive_part(P, &cP);
     579           7 :       if (cP) { N = diviiexact(N,cP); B = gceil(gdiv(B, cP)); }
     580             :     }
     581             :   }
     582          21 :   if (DEBUGLEVEL >= 2) err_printf("Modified P: %Ps\n", P);
     583             : 
     584          21 :   choose_params(P, N, X, B, &delta, &t);
     585          14 :   if (DEBUGLEVEL >= 2)
     586           0 :     err_printf("Init: trying delta = %d, t = %d\n", delta, t);
     587             :   for(;;)
     588             :   {
     589          14 :     dim = d * delta + t;
     590             :     /* TODO: In case of failure do not recompute the full vector */
     591          14 :     Xpowers = (GEN*)new_chunk(dim + 1);
     592          14 :     Xpowers[0] = gen_1;
     593         217 :     for (j = 1; j <= dim; j++) Xpowers[j] = mulii(Xpowers[j-1], X);
     594             : 
     595             :     /* TODO: in case of failure, use the part of the matrix already computed */
     596          14 :     M = zeromatcopy(dim,dim);
     597             : 
     598             :     /* Rows of M correspond to the polynomials
     599             :      * N^delta, N^delta Xi, ... N^delta (Xi)^d-1,
     600             :      * N^(delta-1)P(Xi), N^(delta-1)XiP(Xi), ... N^(delta-1)P(Xi)(Xi)^d-1,
     601             :      * ...
     602             :      * P(Xi)^delta, XiP(Xi)^delta, ..., P(Xi)^delta(Xi)^t-1 */
     603          42 :     for (j = 1; j <= d;   j++) gcoeff(M, j, j) = gel(Xpowers,j-1);
     604             : 
     605             :     /* P-part */
     606          14 :     if (delta) row = d + 1; else row = 0;
     607             : 
     608          14 :     Q = P;
     609          70 :     for (i = 1; i < delta; i++)
     610             :     {
     611         182 :       for (j = 0; j < d; j++,row++)
     612        1239 :         for (l = j + 1; l <= row; l++)
     613        1113 :           gcoeff(M, l, row) = mulii(Xpowers[l-1], gel(Q,l-j+1));
     614          56 :       Q = ZX_mul(Q, P);
     615             :     }
     616          63 :     for (j = 0; j < t; row++, j++)
     617         490 :       for (l = j + 1; l <= row; l++)
     618         441 :         gcoeff(M, l, row) = mulii(Xpowers[l-1], gel(Q,l-j+1));
     619             : 
     620             :     /* N-part */
     621          14 :     row = dim - t; N0 = N;
     622          84 :     while (row >= 1)
     623             :     {
     624         224 :       for (j = 0; j < d; j++,row--)
     625        1421 :         for (l = 1; l <= row; l++)
     626        1267 :           gcoeff(M, l, row) = mulii(gmael(M, row, l), N0);
     627          70 :       if (row >= 1) N0 = mulii(N0, N);
     628             :     }
     629             :     /* Z is the upper bound for the L^1 norm of the polynomial,
     630             :        ie. N^delta if B = N, B^delta otherwise */
     631          14 :     if (B) Z = powiu(B, delta); else Z = N0;
     632             : 
     633          14 :     if (DEBUGLEVEL >= 2)
     634             :     {
     635           0 :       if (DEBUGLEVEL >= 6) err_printf("Matrix to be reduced:\n%Ps\n", M);
     636           0 :       err_printf("Entering LLL\nbitsize bound: %ld\n", expi(Z));
     637           0 :       err_printf("expected shvector bitsize: %ld\n", expi(ZM_det_triangular(M))/dim);
     638             :     }
     639             : 
     640          14 :     sh = ZM_lll(M, 0.75, LLL_INPLACE);
     641             :     /* Take the first vector if it is non constant */
     642          14 :     short_pol = gel(sh,1);
     643          14 :     if (ZV_isscalar(short_pol)) short_pol = gel(sh, 2);
     644             : 
     645          14 :     nsp = gen_0;
     646         217 :     for (j = 1; j <= dim; j++) nsp = addii(nsp, absi_shallow(gel(short_pol,j)));
     647             : 
     648          14 :     if (DEBUGLEVEL >= 2)
     649             :     {
     650           0 :       err_printf("Candidate: %Ps\n", short_pol);
     651           0 :       err_printf("bitsize Norm: %ld\n", expi(nsp));
     652           0 :       err_printf("bitsize bound: %ld\n", expi(mului(bnd, Z)));
     653             :     }
     654          14 :     if (cmpii(nsp, mului(bnd, Z)) < 0) break; /* SUCCESS */
     655             : 
     656             :     /* Failed with the precomputed or supplied value */
     657           0 :     if (++t == d) { delta++; t = 1; }
     658           0 :     if (DEBUGLEVEL >= 2)
     659           0 :       err_printf("Increasing dim, delta = %d t = %d\n", delta, t);
     660             :   }
     661          14 :   bnd = itos(divii(nsp, Z)) + 1;
     662             : 
     663          14 :   while (!signe(gel(short_pol,dim))) dim--;
     664             : 
     665          14 :   R = cgetg(dim + 2, t_POL); R[1] = P[1];
     666         217 :   for (j = 1; j <= dim; j++)
     667         203 :     gel(R,j+1) = diviiexact(gel(short_pol,j), Xpowers[j-1]);
     668          14 :   gel(R,2) = subii(gel(R,2), mului(bnd - 1, N0));
     669             : 
     670          14 :   sol = cgetg(1, t_VEC);
     671          84 :   for (i = -bnd + 1; i < bnd; i++)
     672             :   {
     673          70 :     GEN r = nfrootsQ(R);
     674          70 :     if (DEBUGLEVEL >= 2) err_printf("Roots: %Ps\n", r);
     675          91 :     for (j = 1; j < lg(r); j++)
     676             :     {
     677          21 :       GEN z = gel(r,j);
     678          21 :       if (typ(z) == t_INT && sol_OK(FpX_eval(P,z,N), N,B))
     679          14 :         sol = shallowconcat(sol, z);
     680             :     }
     681          70 :     if (i < bnd) gel(R,2) = addii(gel(R,2), Z);
     682             :   }
     683          14 :   return gerepileupto(av, ZV_sort_uniq(sol));
     684             : }
     685             : 
     686             : /********************************************************************/
     687             : /**                                                                **/
     688             : /**                   LINEAR & ALGEBRAIC DEPENDENCE                **/
     689             : /**                                                                **/
     690             : /********************************************************************/
     691             : 
     692             : static int
     693        1634 : real_indep(GEN re, GEN im, long bit)
     694             : {
     695        1634 :   GEN d = gsub(gmul(gel(re,1),gel(im,2)), gmul(gel(re,2),gel(im,1)));
     696        1634 :   return (!gequal0(d) && gexpo(d) > - bit);
     697             : }
     698             : 
     699             : GEN
     700        8813 : lindepfull_bit(GEN x, long bit)
     701             : {
     702        8813 :   long lx = lg(x), ly, i, j;
     703             :   GEN re, im, M;
     704             : 
     705        8813 :   if (! is_vec_t(typ(x))) pari_err_TYPE("lindep2",x);
     706        8813 :   if (lx <= 2)
     707             :   {
     708          21 :     if (lx == 2 && gequal0(x)) return matid(1);
     709          14 :     return NULL;
     710             :   }
     711        8792 :   re = real_i(x);
     712        8792 :   im = imag_i(x);
     713             :   /* independent over R ? */
     714        8792 :   if (lx == 3 && real_indep(re,im,bit)) return NULL;
     715        8778 :   if (gequal0(im)) im = NULL;
     716        8778 :   ly = im? lx+2: lx+1;
     717        8778 :   M = cgetg(lx,t_MAT);
     718       41234 :   for (i=1; i<lx; i++)
     719             :   {
     720       32456 :     GEN c = cgetg(ly,t_COL); gel(M,i) = c;
     721      170460 :     for (j=1; j<lx; j++) gel(c,j) = gen_0;
     722       32456 :     gel(c,i) = gen_1;
     723       32456 :     gel(c,lx)           = gtrunc2n(gel(re,i), bit);
     724       32456 :     if (im) gel(c,lx+1) = gtrunc2n(gel(im,i), bit);
     725             :   }
     726        8778 :   return ZM_lll(M, 0.99, LLL_INPLACE);
     727             : }
     728             : GEN
     729        3311 : lindep_bit(GEN x, long bit)
     730             : {
     731        3311 :   pari_sp av = avma;
     732        3311 :   GEN v, M = lindepfull_bit(x,bit);
     733        3311 :   if (!M) { set_avma(av); return cgetg(1, t_COL); }
     734        3283 :   v = gel(M,1); setlg(v, lg(M));
     735        3283 :   return gerepilecopy(av, v);
     736             : }
     737             : /* deprecated */
     738             : GEN
     739         112 : lindep2(GEN x, long dig)
     740             : {
     741             :   long bit;
     742         112 :   if (dig < 0) pari_err_DOMAIN("lindep2", "accuracy", "<", gen_0, stoi(dig));
     743         112 :   if (dig) bit = (long) (dig/LOG10_2);
     744             :   else
     745             :   {
     746          98 :     bit = gprecision(x);
     747          98 :     if (!bit)
     748             :     {
     749          35 :       x = Q_primpart(x); /* left on stack */
     750          35 :       bit = 32 + gexpo(x);
     751             :     }
     752             :     else
     753          63 :       bit = (long)prec2nbits_mul(bit, 0.8);
     754             :   }
     755         112 :   return lindep_bit(x, bit);
     756             : }
     757             : 
     758             : /* x is a vector of elts of a p-adic field */
     759             : GEN
     760          28 : lindep_padic(GEN x)
     761             : {
     762          28 :   long i, j, prec = LONG_MAX, nx = lg(x)-1, v;
     763          28 :   pari_sp av = avma;
     764          28 :   GEN p = NULL, pn, m, a;
     765             : 
     766          28 :   if (nx < 2) return cgetg(1,t_COL);
     767         147 :   for (i=1; i<=nx; i++)
     768             :   {
     769         119 :     GEN c = gel(x,i), q;
     770         119 :     if (typ(c) != t_PADIC) continue;
     771             : 
     772          91 :     j = precp(c); if (j < prec) prec = j;
     773          91 :     q = gel(c,2);
     774          91 :     if (!p) p = q; else if (!equalii(p, q)) pari_err_MODULUS("lindep_padic", p, q);
     775             :   }
     776          28 :   if (!p) pari_err_TYPE("lindep_padic [not a p-adic vector]",x);
     777          28 :   v = gvaluation(x,p); pn = powiu(p,prec);
     778          28 :   if (v) x = gmul(x, powis(p, -v));
     779          28 :   x = RgV_to_FpV(x, pn);
     780             : 
     781          28 :   a = negi(gel(x,1));
     782          28 :   m = cgetg(nx,t_MAT);
     783         119 :   for (i=1; i<nx; i++)
     784             :   {
     785          91 :     GEN c = zerocol(nx);
     786          91 :     gel(c,1+i) = a;
     787          91 :     gel(c,1) = gel(x,i+1);
     788          91 :     gel(m,i) = c;
     789             :   }
     790          28 :   m = ZM_lll(ZM_hnfmodid(m, pn), 0.99, LLL_INPLACE);
     791          28 :   return gerepilecopy(av, gel(m,1));
     792             : }
     793             : /* x is a vector of t_POL/t_SER */
     794             : GEN
     795          77 : lindep_Xadic(GEN x)
     796             : {
     797          77 :   long i, prec = LONG_MAX, deg = 0, lx = lg(x), vx, v;
     798          77 :   pari_sp av = avma;
     799             :   GEN m;
     800             : 
     801          77 :   if (lx == 1) return cgetg(1,t_COL);
     802          77 :   vx = gvar(x);
     803          77 :   if (gequal0(x)) return col_ei(lx-1,1);
     804          70 :   v = gvaluation(x, pol_x(vx));
     805          70 :   if (!v)         x = shallowcopy(x);
     806           0 :   else if (v > 0) x = gdiv(x, pol_xn(v, vx));
     807           0 :   else            x = gmul(x, pol_xn(-v, vx));
     808             :   /* all t_SER have valuation >= 0 */
     809         735 :   for (i=1; i<lx; i++)
     810             :   {
     811         665 :     GEN c = gel(x,i);
     812         665 :     if (gvar(c) != vx) { gel(x,i) = scalarpol_shallow(c, vx); continue; }
     813         658 :     switch(typ(c))
     814             :     {
     815         231 :       case t_POL: deg = maxss(deg, degpol(c)); break;
     816           0 :       case t_RFRAC: pari_err_TYPE("lindep_Xadic", c);
     817         427 :       case t_SER:
     818         427 :         prec = minss(prec, valser(c)+lg(c)-2);
     819         427 :         gel(x,i) = ser2rfrac_i(c);
     820             :     }
     821             :   }
     822          70 :   if (prec == LONG_MAX) prec = deg+1;
     823          70 :   m = RgXV_to_RgM(x, prec);
     824          70 :   return gerepileupto(av, deplin(m));
     825             : }
     826             : static GEN
     827          35 : vec_lindep(GEN x)
     828             : {
     829          35 :   pari_sp av = avma;
     830          35 :   long i, l = lg(x); /* > 1 */
     831          35 :   long t = typ(gel(x,1)), h = lg(gel(x,1));
     832          35 :   GEN m = cgetg(l, t_MAT);
     833         126 :   for (i = 1; i < l; i++)
     834             :   {
     835          98 :     GEN y = gel(x,i);
     836          98 :     if (lg(y) != h || typ(y) != t) pari_err_TYPE("lindep",x);
     837          91 :     if (t != t_COL) y = shallowtrans(y); /* Sigh */
     838          91 :     gel(m,i) = y;
     839             :   }
     840          28 :   return gerepileupto(av, deplin(m));
     841             : }
     842             : 
     843             : GEN
     844           0 : lindep(GEN x) { return lindep2(x, 0); }
     845             : 
     846             : GEN
     847         434 : lindep0(GEN x,long bit)
     848             : {
     849         434 :   long i, tx = typ(x);
     850         434 :   if (tx == t_MAT) return deplin(x);
     851         147 :   if (! is_vec_t(tx)) pari_err_TYPE("lindep",x);
     852         441 :   for (i = 1; i < lg(x); i++)
     853         357 :     switch(typ(gel(x,i)))
     854             :     {
     855           7 :       case t_PADIC: return lindep_padic(x);
     856          21 :       case t_POL:
     857             :       case t_RFRAC:
     858          21 :       case t_SER: return lindep_Xadic(x);
     859          35 :       case t_VEC:
     860          35 :       case t_COL: return vec_lindep(x);
     861             :     }
     862          84 :   return lindep2(x, bit);
     863             : }
     864             : 
     865             : GEN
     866          77 : algdep0(GEN x, long n, long bit)
     867             : {
     868          77 :   long tx = typ(x), i;
     869             :   pari_sp av;
     870             :   GEN y;
     871             : 
     872          77 :   if (! is_scalar_t(tx)) pari_err_TYPE("algdep0",x);
     873          77 :   if (tx == t_POLMOD)
     874             :   {
     875          14 :     av = avma; y = minpoly(x, 0);
     876          14 :     return (degpol(y) > n)? gc_const(av, gen_1): y;
     877             :   }
     878          63 :   if (gequal0(x)) return pol_x(0);
     879          63 :   if (n <= 0)
     880             :   {
     881          14 :     if (!n) return gen_1;
     882           7 :     pari_err_DOMAIN("algdep", "degree", "<", gen_0, stoi(n));
     883             :   }
     884             : 
     885          49 :   av = avma; y = cgetg(n+2,t_COL);
     886          49 :   gel(y,1) = gen_1;
     887          49 :   gel(y,2) = x; /* n >= 1 */
     888         210 :   for (i=3; i<=n+1; i++) gel(y,i) = gmul(gel(y,i-1),x);
     889          49 :   if (typ(x) == t_PADIC)
     890          21 :     y = lindep_padic(y);
     891             :   else
     892          28 :     y = lindep2(y, bit);
     893          49 :   if (lg(y) == 1) pari_err(e_DOMAIN,"algdep", "degree(x)",">", stoi(n), x);
     894          49 :   y = RgV_to_RgX(y, 0);
     895          49 :   if (signe(leading_coeff(y)) > 0) return gerepilecopy(av, y);
     896          14 :   return gerepileupto(av, ZX_neg(y));
     897             : }
     898             : 
     899             : GEN
     900           0 : algdep(GEN x, long n)
     901             : {
     902           0 :   return algdep0(x,n,0);
     903             : }
     904             : 
     905             : static GEN
     906          56 : sertomat(GEN S, long p, long r, long vy)
     907             : {
     908             :   long n, m;
     909          56 :   GEN v = cgetg(r*p+1, t_VEC); /* v[r*n+m+1] = s^n * y^m */
     910             :   /* n = 0 */
     911         245 :   for (m = 0; m < r; m++) gel(v, m+1) = pol_xn(m, vy);
     912         175 :   for(n=1; n < p; n++)
     913         546 :     for (m = 0; m < r; m++)
     914             :     {
     915         427 :       GEN c = gel(S,n);
     916         427 :       if (m)
     917             :       {
     918         308 :         c = shallowcopy(c);
     919         308 :         setvalser(c, valser(c) + m);
     920             :       }
     921         427 :       gel(v, r*n + m + 1) = c;
     922             :     }
     923          56 :   return v;
     924             : }
     925             : 
     926             : GEN
     927          42 : seralgdep(GEN s, long p, long r)
     928             : {
     929          42 :   pari_sp av = avma;
     930             :   long vy, i, n, prec;
     931             :   GEN S, v, D;
     932             : 
     933          42 :   if (typ(s) != t_SER) pari_err_TYPE("seralgdep",s);
     934          42 :   if (p <= 0) pari_err_DOMAIN("seralgdep", "p", "<=", gen_0, stoi(p));
     935          42 :   if (r < 0) pari_err_DOMAIN("seralgdep", "r", "<", gen_0, stoi(r));
     936          42 :   if (is_bigint(addiu(muluu(p, r), 1))) pari_err_OVERFLOW("seralgdep");
     937          42 :   vy = varn(s);
     938          42 :   if (!vy) pari_err_PRIORITY("seralgdep", s, ">", 0);
     939          42 :   r++; p++;
     940          42 :   prec = valser(s) + lg(s)-2;
     941          42 :   if (r > prec) r = prec;
     942          42 :   S = cgetg(p+1, t_VEC); gel(S, 1) = s;
     943         119 :   for (i = 2; i <= p; i++) gel(S,i) = gmul(gel(S,i-1), s);
     944          42 :   v = sertomat(S, p, r, vy);
     945          42 :   D = lindep_Xadic(v);
     946          42 :   if (lg(D) == 1) { set_avma(av); return gen_0; }
     947          35 :   v = cgetg(p+1, t_VEC);
     948         133 :   for (n = 0; n < p; n++)
     949          98 :     gel(v, n+1) = RgV_to_RgX(vecslice(D, r*n+1, r*n+r), vy);
     950          35 :   return gerepilecopy(av, RgV_to_RgX(v, 0));
     951             : }
     952             : 
     953             : GEN
     954          14 : serdiffdep(GEN s, long p, long r)
     955             : {
     956          14 :   pari_sp av = avma;
     957             :   long vy, i, n, prec;
     958             :   GEN P, S, v, D;
     959             : 
     960          14 :   if (typ(s) != t_SER) pari_err_TYPE("serdiffdep",s);
     961          14 :   if (p <= 0) pari_err_DOMAIN("serdiffdep", "p", "<=", gen_0, stoi(p));
     962          14 :   if (r < 0) pari_err_DOMAIN("serdiffdep", "r", "<", gen_0, stoi(r));
     963          14 :   if (is_bigint(addiu(muluu(p, r), 1))) pari_err_OVERFLOW("serdiffdep");
     964          14 :   vy = varn(s);
     965          14 :   if (!vy) pari_err_PRIORITY("serdiffdep", s, ">", 0);
     966          14 :   r++; p++;
     967          14 :   prec = valser(s) + lg(s)-2;
     968          14 :   if (r > prec) r = prec;
     969          14 :   S = cgetg(p+1, t_VEC); gel(S, 1) = s;
     970          56 :   for (i = 2; i <= p; i++) gel(S,i) = derivser(gel(S,i-1));
     971          14 :   v = sertomat(S, p, r, vy);
     972          14 :   D = lindep_Xadic(v);
     973          14 :   if (lg(D) == 1) { set_avma(av); return gen_0; }
     974          14 :   P = RgV_to_RgX(vecslice(D, 1, r), vy);
     975          14 :   v = cgetg(p, t_VEC);
     976          56 :   for (n = 1; n < p; n++)
     977          42 :     gel(v, n) = RgV_to_RgX(vecslice(D, r*n+1, r*n+r), vy);
     978          14 :   return gerepilecopy(av, mkvec2(RgV_to_RgX(v, 0), gneg(P)));
     979             : }
     980             : 
     981             : /* FIXME: could precompute ZM_lll attached to V[2..] */
     982             : static GEN
     983        5502 : lindepcx(GEN V, long bit)
     984             : {
     985        5502 :   GEN Vr = real_i(V), Vi = imag_i(V);
     986        5502 :   if (gexpo(Vr) < -bit) V = Vi;
     987        5502 :   else if (gexpo(Vi) < -bit) V = Vr;
     988        5502 :   return lindepfull_bit(V, bit);
     989             : }
     990             : /* c floating point t_REAL or t_COMPLEX, T ZX, recognize in Q[x]/(T).
     991             :  * V helper vector (containing complex roots of T), MODIFIED */
     992             : static GEN
     993        5502 : cx_bestapprnf(GEN c, GEN T, GEN V, long bit)
     994             : {
     995        5502 :   GEN M, a, v = NULL;
     996             :   long i, l;
     997        5502 :   gel(V,1) = gneg(c); M = lindepcx(V, bit);
     998        5502 :   if (!M) pari_err(e_MISC, "cannot rationalize coeff in bestapprnf");
     999        5502 :   l = lg(M); a = NULL;
    1000        5502 :   for (i = 1; i < l; i ++) { v = gel(M,i); a = gel(v,1); if (signe(a)) break; }
    1001        5502 :   v = RgC_Rg_div(vecslice(v, 2, lg(M)-1), a);
    1002        5502 :   if (!T) return gel(v,1);
    1003        4830 :   v = RgV_to_RgX(v, varn(T)); l = lg(v);
    1004        4830 :   if (l == 2) return gen_0;
    1005        4165 :   if (l == 3) return gel(v,2);
    1006        3668 :   return mkpolmod(v, T);
    1007             : }
    1008             : static GEN
    1009        8246 : bestapprnf_i(GEN x, GEN T, GEN V, long bit)
    1010             : {
    1011        8246 :   long i, l, tx = typ(x);
    1012             :   GEN z;
    1013        8246 :   switch (tx)
    1014             :   {
    1015         833 :     case t_INT: case t_FRAC: return x;
    1016        5502 :     case t_REAL: case t_COMPLEX: return cx_bestapprnf(x, T, V, bit);
    1017           0 :     case t_POLMOD: if (RgX_equal(gel(x,1),T)) return x;
    1018           0 :                    break;
    1019        1911 :     case t_POL: case t_SER: case t_VEC: case t_COL: case t_MAT:
    1020        1911 :       l = lg(x); z = cgetg(l, tx);
    1021        3437 :       for (i = 1; i < lontyp[tx]; i++) z[i] = x[i];
    1022        8211 :       for (; i < l; i++) gel(z,i) = bestapprnf_i(gel(x,i), T, V, bit);
    1023        1911 :       return z;
    1024             :   }
    1025           0 :   pari_err_TYPE("mfcxtoQ", x);
    1026             :   return NULL;/*LCOV_EXCL_LINE*/
    1027             : }
    1028             : 
    1029             : GEN
    1030        1946 : bestapprnf(GEN x, GEN T, GEN roT, long prec)
    1031             : {
    1032        1946 :   pari_sp av = avma;
    1033        1946 :   long tx = typ(x), dT = 1, bit;
    1034             :   GEN V;
    1035             : 
    1036        1946 :   if (T)
    1037             :   {
    1038        1610 :     if (typ(T) != t_POL) T = nf_get_pol(checknf(T));
    1039        1610 :     else if (!RgX_is_ZX(T)) pari_err_TYPE("bestapprnf", T);
    1040        1610 :     dT = degpol(T);
    1041             :   }
    1042        1946 :   if (is_rational_t(tx)) return gcopy(x);
    1043        1946 :   if (tx == t_POLMOD)
    1044             :   {
    1045           0 :     if (!T || !RgX_equal(T, gel(x,1))) pari_err_TYPE("bestapprnf",x);
    1046           0 :     return gcopy(x);
    1047             :   }
    1048             : 
    1049        1946 :   if (roT)
    1050             :   {
    1051         644 :     long l = gprecision(roT);
    1052         644 :     switch(typ(roT))
    1053             :     {
    1054         644 :       case t_INT: case t_FRAC: case t_REAL: case t_COMPLEX: break;
    1055           0 :       default: pari_err_TYPE("bestapprnf", roT);
    1056             :     }
    1057         644 :     if (prec < l) prec = l;
    1058             :   }
    1059        1302 :   else if (!T)
    1060         336 :     roT = gen_1;
    1061             :   else
    1062             :   {
    1063         966 :     long n = poliscyclo(T); /* cyclotomic is an important special case */
    1064         966 :     roT = n? rootsof1u_cx(n,prec): gel(QX_complex_roots(T,prec), 1);
    1065             :   }
    1066        1946 :   V = vec_prepend(gpowers(roT, dT-1), NULL);
    1067        1946 :   bit = prec2nbits_mul(prec, 0.8);
    1068        1946 :   return gerepilecopy(av, bestapprnf_i(x, T, V, bit));
    1069             : }
    1070             : 
    1071             : /********************************************************************/
    1072             : /**                                                                **/
    1073             : /**                              MINIM                             **/
    1074             : /**                                                                **/
    1075             : /********************************************************************/
    1076             : void
    1077      121765 : minim_alloc(long n, double ***q, GEN *x, double **y,  double **z, double **v)
    1078             : {
    1079             :   long i, s;
    1080             : 
    1081      121765 :   *x = cgetg(n, t_VECSMALL);
    1082      121765 :   *q = (double**) new_chunk(n);
    1083      121764 :   s = n * sizeof(double);
    1084      121764 :   *y = (double*) stack_malloc_align(s, sizeof(double));
    1085      121765 :   *z = (double*) stack_malloc_align(s, sizeof(double));
    1086      121765 :   *v = (double*) stack_malloc_align(s, sizeof(double));
    1087      525910 :   for (i=1; i<n; i++) (*q)[i] = (double*) stack_malloc_align(s, sizeof(double));
    1088      121765 : }
    1089             : 
    1090             : static void
    1091          70 : cvp_alloc(long n, double ***q, GEN *x, double **y,  double **z, double **v, double **t, double **tpre)
    1092             : {
    1093             :   long i, s;
    1094             : 
    1095          70 :   *x = cgetg(n, t_VECSMALL);
    1096          70 :   *q = (double**) new_chunk(n);
    1097          70 :   s = n * sizeof(double);
    1098          70 :   *y = (double*) stack_malloc_align(s, sizeof(double));
    1099          70 :   *z = (double*) stack_malloc_align(s, sizeof(double));
    1100          70 :   *v = (double*) stack_malloc_align(s, sizeof(double));
    1101          70 :   *t = (double*) stack_malloc_align(s, sizeof(double));
    1102          70 :   *tpre = (double*) stack_malloc_align(s, sizeof(double));
    1103         392 :   for (i=1; i<n; i++) (*q)[i] = (double*) stack_malloc_align(s, sizeof(double));
    1104          70 : }
    1105             : 
    1106             : static GEN
    1107      245868 : ZC_canon(GEN V)
    1108             : {
    1109      245868 :   long l = lg(V), j;
    1110      571655 :   for (j = 1; j < l  &&  signe(gel(V,j)) == 0; ++j);
    1111      245868 :   return (j < l  &&  signe(gel(V,j)) < 0)? ZC_neg(V): V;
    1112             : }
    1113             : 
    1114             : static GEN
    1115        5502 : ZM_zc_mul_canon(GEN u, GEN x)
    1116             : {
    1117        5502 :   return ZC_canon(ZM_zc_mul(u,x));
    1118             : }
    1119             : 
    1120             : static GEN
    1121      240366 : ZM_zc_mul_canon_zm(GEN u, GEN x)
    1122             : {
    1123      240366 :   pari_sp av = avma;
    1124      240366 :   GEN M = ZV_to_zv(ZC_canon(ZM_zc_mul(u,x)));
    1125      240366 :   return gerepileupto(av, M);
    1126             : }
    1127             : 
    1128             : struct qfvec
    1129             : {
    1130             :   GEN a, r, u;
    1131             : };
    1132             : 
    1133             : static void
    1134           0 : err_minim(GEN a)
    1135             : {
    1136           0 :   pari_err_DOMAIN("minim0","form","is not",
    1137             :                   strtoGENstr("positive definite"),a);
    1138           0 : }
    1139             : 
    1140             : static GEN
    1141         902 : minim_lll(GEN a, GEN *u)
    1142             : {
    1143         902 :   *u = lllgramint(a);
    1144         902 :   if (lg(*u) != lg(a)) err_minim(a);
    1145         902 :   return qf_ZM_apply(a,*u);
    1146             : }
    1147             : 
    1148             : static void
    1149         902 : forqfvec_init_dolll(struct qfvec *qv, GEN *pa, long dolll)
    1150             : {
    1151         902 :   GEN r, u, a = *pa;
    1152         902 :   if (!dolll) u = NULL;
    1153             :   else
    1154             :   {
    1155         860 :     if (typ(a) != t_MAT || !RgM_is_ZM(a)) pari_err_TYPE("qfminim",a);
    1156         860 :     a = *pa = minim_lll(a, &u);
    1157             :   }
    1158         902 :   qv->a = RgM_gtofp(a, DEFAULTPREC);
    1159         902 :   r = qfgaussred_positive(qv->a);
    1160         902 :   if (!r)
    1161             :   {
    1162           0 :     r = qfgaussred_positive(a); /* exact computation */
    1163           0 :     if (!r) err_minim(a);
    1164           0 :     r = RgM_gtofp(r, DEFAULTPREC);
    1165             :   }
    1166         902 :   qv->r = r;
    1167         902 :   qv->u = u;
    1168         902 : }
    1169             : 
    1170             : static void
    1171          42 : forqfvec_init(struct qfvec *qv, GEN a)
    1172          42 : { forqfvec_init_dolll(qv, &a, 1); }
    1173             : 
    1174             : static void
    1175          42 : forqfvec_i(void *E, long (*fun)(void *, GEN, GEN, double), struct qfvec *qv, GEN BORNE)
    1176             : {
    1177          42 :   GEN x, a = qv->a, r = qv->r, u = qv->u;
    1178          42 :   long n = lg(a)-1, i, j, k;
    1179             :   double p,BOUND,*v,*y,*z,**q;
    1180          42 :   const double eps = 1e-10;
    1181          42 :   if (!BORNE) BORNE = gen_0;
    1182             :   else
    1183             :   {
    1184          28 :     BORNE = gfloor(BORNE);
    1185          28 :     if (typ(BORNE) != t_INT) pari_err_TYPE("minim0",BORNE);
    1186          35 :     if (signe(BORNE) <= 0) return;
    1187             :   }
    1188          35 :   if (n == 0) return;
    1189          28 :   minim_alloc(n+1, &q, &x, &y, &z, &v);
    1190          98 :   for (j=1; j<=n; j++)
    1191             :   {
    1192          70 :     v[j] = rtodbl(gcoeff(r,j,j));
    1193         133 :     for (i=1; i<j; i++) q[i][j] = rtodbl(gcoeff(r,i,j));
    1194             :   }
    1195             : 
    1196          28 :   if (gequal0(BORNE))
    1197             :   {
    1198             :     double c;
    1199          14 :     p = rtodbl(gcoeff(a,1,1));
    1200          42 :     for (i=2; i<=n; i++) { c = rtodbl(gcoeff(a,i,i)); if (c < p) p = c; }
    1201          14 :     BORNE = roundr(dbltor(p));
    1202             :   }
    1203             :   else
    1204          14 :     p = gtodouble(BORNE);
    1205          28 :   BOUND = p * (1 + eps);
    1206          28 :   if (BOUND > (double)ULONG_MAX || (ulong)BOUND != (ulong)p)
    1207           7 :     pari_err_PREC("forqfvec");
    1208             : 
    1209          21 :   k = n; y[n] = z[n] = 0;
    1210          21 :   x[n] = (long)sqrt(BOUND/v[n]);
    1211          56 :   for(;;x[1]--)
    1212             :   {
    1213             :     do
    1214             :     {
    1215         140 :       if (k>1)
    1216             :       {
    1217          84 :         long l = k-1;
    1218          84 :         z[l] = 0;
    1219         245 :         for (j=k; j<=n; j++) z[l] += q[l][j]*x[j];
    1220          84 :         p = (double)x[k] + z[k];
    1221          84 :         y[l] = y[k] + p*p*v[k];
    1222          84 :         x[l] = (long)floor(sqrt((BOUND-y[l])/v[l])-z[l]);
    1223          84 :         k = l;
    1224             :       }
    1225             :       for(;;)
    1226             :       {
    1227         189 :         p = (double)x[k] + z[k];
    1228         189 :         if (y[k] + p*p*v[k] <= BOUND) break;
    1229          49 :         k++; x[k]--;
    1230             :       }
    1231         140 :     } while (k > 1);
    1232          77 :     if (! x[1] && y[1]<=eps) break;
    1233             : 
    1234          56 :     p = (double)x[1] + z[1]; p = y[1] + p*p*v[1]; /* norm(x) */
    1235          56 :     if (fun(E, u, x, p)) break;
    1236             :   }
    1237             : }
    1238             : 
    1239             : void
    1240           0 : forqfvec(void *E, long (*fun)(void *, GEN, GEN, double), GEN a, GEN BORNE)
    1241             : {
    1242           0 :   pari_sp av = avma;
    1243             :   struct qfvec qv;
    1244           0 :   forqfvec_init(&qv, a);
    1245           0 :   forqfvec_i(E, fun, &qv, BORNE);
    1246           0 :   set_avma(av);
    1247           0 : }
    1248             : 
    1249             : struct qfvecwrap
    1250             : {
    1251             :   void *E;
    1252             :   long (*fun)(void *, GEN);
    1253             : };
    1254             : 
    1255             : static long
    1256          56 : forqfvec_wrap(void *E, GEN u, GEN x, double d)
    1257             : {
    1258          56 :   pari_sp av = avma;
    1259          56 :   struct qfvecwrap *W = (struct qfvecwrap *) E;
    1260             :   (void) d;
    1261          56 :   return gc_long(av, W->fun(W->E, ZM_zc_mul_canon(u, x)));
    1262             : }
    1263             : 
    1264             : void
    1265          42 : forqfvec1(void *E, long (*fun)(void *, GEN), GEN a, GEN BORNE)
    1266             : {
    1267          42 :   pari_sp av = avma;
    1268             :   struct qfvecwrap wr;
    1269             :   struct qfvec qv;
    1270          42 :   wr.E = E; wr.fun = fun;
    1271          42 :   forqfvec_init(&qv, a);
    1272          42 :   forqfvec_i((void*) &wr, forqfvec_wrap, &qv, BORNE);
    1273          35 :   set_avma(av);
    1274          35 : }
    1275             : 
    1276             : void
    1277          42 : forqfvec0(GEN a, GEN BORNE, GEN code)
    1278          42 : { EXPRVOID_WRAP(code, forqfvec1(EXPR_ARGVOID, a,  BORNE)) }
    1279             : 
    1280             : enum { min_ALL = 0, min_FIRST, min_VECSMALL, min_VECSMALL2 };
    1281             : 
    1282             : /* Minimal vectors for the integral definite quadratic form: a.
    1283             :  * Result u:
    1284             :  *   u[1]= Number of vectors of square norm <= BORNE
    1285             :  *   u[2]= maximum norm found
    1286             :  *   u[3]= list of vectors found (at most STOCKMAX, unless NULL)
    1287             :  *
    1288             :  *  If BORNE = NULL: Minimal nonzero vectors.
    1289             :  *  flag = min_ALL,   as above
    1290             :  *  flag = min_FIRST, exits when first suitable vector is found.
    1291             :  *  flag = min_VECSMALL, return a t_VECSMALL of (half) the number of vectors
    1292             :  *  for each norm
    1293             :  *  flag = min_VECSMALL2, same but count only vectors with even norm, and shift
    1294             :  *  the answer */
    1295             : static GEN
    1296         847 : minim0_dolll(GEN a, GEN BORNE, GEN STOCKMAX, long flag, long dolll)
    1297             : {
    1298             :   GEN x, u, r, L, gnorme;
    1299         847 :   long n = lg(a)-1, i, j, k, s, maxrank, sBORNE;
    1300         847 :   pari_sp av = avma, av1;
    1301             :   double p,maxnorm,BOUND,*v,*y,*z,**q;
    1302         847 :   const double eps = 1e-10;
    1303         847 :   int stockall = 0;
    1304             :   struct qfvec qv;
    1305             : 
    1306         847 :   if (!BORNE)
    1307          56 :     sBORNE = 0;
    1308             :   else
    1309             :   {
    1310         791 :     BORNE = gfloor(BORNE);
    1311         791 :     if (typ(BORNE) != t_INT) pari_err_TYPE("minim0",BORNE);
    1312         791 :     if (is_bigint(BORNE)) pari_err_PREC( "qfminim");
    1313         790 :     sBORNE = itos(BORNE); set_avma(av);
    1314         790 :     if (sBORNE < 0) sBORNE = 0;
    1315             :   }
    1316         846 :   if (!STOCKMAX)
    1317             :   {
    1318         335 :     stockall = 1;
    1319         335 :     maxrank = 200;
    1320             :   }
    1321             :   else
    1322             :   {
    1323         511 :     STOCKMAX = gfloor(STOCKMAX);
    1324         511 :     if (typ(STOCKMAX) != t_INT) pari_err_TYPE("minim0",STOCKMAX);
    1325         511 :     maxrank = itos(STOCKMAX);
    1326         511 :     if (maxrank < 0)
    1327           0 :       pari_err_TYPE("minim0 [negative number of vectors]",STOCKMAX);
    1328             :   }
    1329             : 
    1330         846 :   switch(flag)
    1331             :   {
    1332         462 :     case min_VECSMALL:
    1333             :     case min_VECSMALL2:
    1334         462 :       if (sBORNE <= 0) return cgetg(1, t_VECSMALL);
    1335         434 :       L = zero_zv(sBORNE);
    1336         434 :       if (flag == min_VECSMALL2) sBORNE <<= 1;
    1337         434 :       if (n == 0) return L;
    1338         434 :       break;
    1339          35 :     case min_FIRST:
    1340          35 :       if (n == 0 || (!sBORNE && BORNE)) return cgetg(1,t_VEC);
    1341          21 :       L = NULL; /* gcc -Wall */
    1342          21 :       break;
    1343         349 :     case min_ALL:
    1344         349 :       if (n == 0 || (!sBORNE && BORNE))
    1345          14 :         retmkvec3(gen_0, gen_0, cgetg(1, t_MAT));
    1346         335 :       L = new_chunk(1+maxrank);
    1347         335 :       break;
    1348           0 :     default:
    1349           0 :       return NULL;
    1350             :   }
    1351         790 :   minim_alloc(n+1, &q, &x, &y, &z, &v);
    1352             : 
    1353         790 :   forqfvec_init_dolll(&qv, &a, dolll);
    1354         790 :   av1 = avma;
    1355         790 :   r = qv.r;
    1356         790 :   u = qv.u;
    1357        5912 :   for (j=1; j<=n; j++)
    1358             :   {
    1359        5122 :     v[j] = rtodbl(gcoeff(r,j,j));
    1360       29579 :     for (i=1; i<j; i++) q[i][j] = rtodbl(gcoeff(r,i,j)); /* |.| <= 1/2 */
    1361             :   }
    1362             : 
    1363         790 :   if (sBORNE) maxnorm = 0.;
    1364             :   else
    1365             :   {
    1366          56 :     GEN B = gcoeff(a,1,1);
    1367          56 :     long t = 1;
    1368         616 :     for (i=2; i<=n; i++)
    1369             :     {
    1370         560 :       GEN c = gcoeff(a,i,i);
    1371         560 :       if (cmpii(c, B) < 0) { B = c; t = i; }
    1372             :     }
    1373          56 :     if (flag == min_FIRST) return gerepilecopy(av, mkvec2(B, gel(u,t)));
    1374          49 :     maxnorm = -1.; /* don't update maxnorm */
    1375          49 :     if (is_bigint(B)) return NULL;
    1376          48 :     sBORNE = itos(B);
    1377             :   }
    1378         782 :   BOUND = sBORNE * (1 + eps);
    1379         782 :   if ((long)BOUND != sBORNE) return NULL;
    1380             : 
    1381         770 :   s = 0;
    1382         770 :   k = n; y[n] = z[n] = 0;
    1383         770 :   x[n] = (long)sqrt(BOUND/v[n]);
    1384     1223264 :   for(;;x[1]--)
    1385             :   {
    1386             :     do
    1387             :     {
    1388     2245614 :       if (k>1)
    1389             :       {
    1390     1022259 :         long l = k-1;
    1391     1022259 :         z[l] = 0;
    1392    11756080 :         for (j=k; j<=n; j++) z[l] += q[l][j]*x[j];
    1393     1022259 :         p = (double)x[k] + z[k];
    1394     1022259 :         y[l] = y[k] + p*p*v[k];
    1395     1022259 :         x[l] = (long)floor(sqrt((BOUND-y[l])/v[l])-z[l]);
    1396     1022259 :         k = l;
    1397             :       }
    1398             :       for(;;)
    1399             :       {
    1400     3263729 :         p = (double)x[k] + z[k];
    1401     3263729 :         if (y[k] + p*p*v[k] <= BOUND) break;
    1402     1018115 :         k++; x[k]--;
    1403             :       }
    1404             :     }
    1405     2245614 :     while (k > 1);
    1406     1224034 :     if (! x[1] && y[1]<=eps) break;
    1407             : 
    1408     1223271 :     p = (double)x[1] + z[1]; p = y[1] + p*p*v[1]; /* norm(x) */
    1409     1223271 :     if (maxnorm >= 0)
    1410             :     {
    1411     1220723 :       if (p > maxnorm) maxnorm = p;
    1412             :     }
    1413             :     else
    1414             :     { /* maxnorm < 0 : only look for minimal vectors */
    1415        2548 :       pari_sp av2 = avma;
    1416        2548 :       gnorme = roundr(dbltor(p));
    1417        2548 :       if (cmpis(gnorme, sBORNE) >= 0) set_avma(av2);
    1418             :       else
    1419             :       {
    1420          14 :         sBORNE = itos(gnorme); set_avma(av1);
    1421          14 :         BOUND = sBORNE * (1+eps);
    1422          14 :         L = new_chunk(maxrank+1);
    1423          14 :         s = 0;
    1424             :       }
    1425             :     }
    1426     1223271 :     s++;
    1427             : 
    1428     1223271 :     switch(flag)
    1429             :     {
    1430           7 :       case min_FIRST:
    1431           7 :         if (dolll) x = ZM_zc_mul_canon(u,x);
    1432           7 :         return gerepilecopy(av, mkvec2(roundr(dbltor(p)), x));
    1433             : 
    1434      248241 :       case min_ALL:
    1435      248241 :         if (s > maxrank && stockall) /* overflow */
    1436             :         {
    1437         490 :           long maxranknew = maxrank << 1;
    1438         490 :           GEN Lnew = new_chunk(1 + maxranknew);
    1439      344890 :           for (i=1; i<=maxrank; i++) Lnew[i] = L[i];
    1440         490 :           L = Lnew; maxrank = maxranknew;
    1441             :         }
    1442      248241 :         if (s<=maxrank) gel(L,s) = leafcopy(x);
    1443      248241 :         break;
    1444             : 
    1445       39200 :       case min_VECSMALL:
    1446       39200 :         { ulong norm = (ulong)(p + 0.5); L[norm]++; }
    1447       39200 :         break;
    1448             : 
    1449      935823 :       case min_VECSMALL2:
    1450      935823 :         { ulong norm = (ulong)(p + 0.5); if (!odd(norm)) L[norm>>1]++; }
    1451      935823 :         break;
    1452             : 
    1453             :     }
    1454             :   }
    1455         763 :   switch(flag)
    1456             :   {
    1457           7 :     case min_FIRST:
    1458           7 :       set_avma(av); return cgetg(1,t_VEC);
    1459         434 :     case min_VECSMALL:
    1460             :     case min_VECSMALL2:
    1461         434 :       set_avma((pari_sp)L); return L;
    1462             :   }
    1463         322 :   r = (maxnorm >= 0) ? roundr(dbltor(maxnorm)): stoi(sBORNE);
    1464         322 :   k = minss(s,maxrank);
    1465         322 :   L[0] = evaltyp(t_MAT) | evallg(k + 1);
    1466         322 :   if (dolll)
    1467      246092 :     for (j=1; j<=k; j++)
    1468      245805 :       gel(L,j) = dolll==1 ? ZM_zc_mul_canon(u, gel(L,j))
    1469      245805 :                           : ZM_zc_mul_canon_zm(u, gel(L,j));
    1470         322 :   return gerepilecopy(av, mkvec3(stoi(s<<1), r, L));
    1471             : }
    1472             : 
    1473             : /* Closest vectors for the integral definite quadratic form: a.
    1474             :  * Code bases on minim0_dolll
    1475             :  * Result u:
    1476             :  *   u[1]= Number of closest vectors of square distance <= BORNE
    1477             :  *   u[2]= maximum squared distance found
    1478             :  *   u[3]= list of vectors found (at most STOCKMAX, unless NULL)
    1479             :  *
    1480             :  *  If BORNE = NULL or <= 0.: returns closest vectors.
    1481             :  *  flag = min_ALL,   as above
    1482             :  *  flag = min_FIRST, exits when first suitable vector is found.
    1483             : */
    1484             : static GEN
    1485          91 : cvp0_dolll(GEN a, GEN target, GEN BORNE, GEN STOCKMAX, long flag, long dolll)
    1486             : {
    1487             :   GEN x, u, r, L;
    1488             :   GEN uinv, tv;
    1489             :   GEN pd;
    1490          91 :   long n = lg(a)-1, nt = lg(target)-1, i, j, k, s, maxrank;
    1491          91 :   pari_sp av = avma, av1;
    1492             :   double p,maxnorm,BOUND,*v,*y,*z,*tt,**q, *tpre, sBORNE;
    1493          91 :   const double eps = 1e-10;
    1494          91 :   int stockall = 0;
    1495             :   struct qfvec qv;
    1496          91 :   int done = 0;
    1497          91 :   if (typ(target) != t_VEC && typ(target) != t_COL ) pari_err_TYPE("cvp0",target);
    1498          91 :   if (n != nt) pari_err_TYPE("cvp0 [different dimensions]",target);
    1499          77 :   if (!BORNE)
    1500           0 :     sBORNE = 0.;
    1501             :   else
    1502             :   {
    1503          77 :     if (typ(BORNE) != t_REAL && typ(BORNE) != t_INT && typ(BORNE) != t_FRAC ) pari_err_TYPE("cvp0",BORNE);
    1504          77 :     sBORNE = gtodouble(BORNE); set_avma(av);
    1505          77 :     if (sBORNE < 0.) sBORNE = 0.;
    1506             :   }
    1507          77 :   if (!STOCKMAX)
    1508             :   {
    1509          77 :     stockall = 1;
    1510          77 :     maxrank = 200;
    1511             :   }
    1512             :   else
    1513             :   {
    1514           0 :     STOCKMAX = gfloor(STOCKMAX);
    1515           0 :     if (typ(STOCKMAX) != t_INT) pari_err_TYPE("cvp0",STOCKMAX);
    1516           0 :     maxrank = itos(STOCKMAX);
    1517           0 :     if (maxrank < 0)
    1518           0 :       pari_err_TYPE("cvp0 [negative number of vectors]",STOCKMAX);
    1519             :   }
    1520             : 
    1521          77 :   L = (flag==min_ALL) ? new_chunk(1+maxrank) : NULL;
    1522          77 :   if (n == 0 ) {
    1523           7 :     if (flag==min_ALL) {
    1524           7 :       retmkvec3(gen_0, gen_0, cgetg(1, t_MAT));
    1525             :     }
    1526             :     else {
    1527           0 :       return cgetg(1,t_VEC);
    1528             :     }
    1529             :   }
    1530             : 
    1531          70 :   cvp_alloc(n+1, &q, &x, &y, &z, &v, &tt, &tpre);
    1532             : 
    1533          70 :   forqfvec_init_dolll(&qv, &a, dolll);
    1534          70 :   av1 = avma;
    1535          70 :   r = qv.r;
    1536          70 :   u = qv.u;
    1537         392 :   for (j=1; j<=n; j++)
    1538             :   {
    1539         322 :     v[j] = rtodbl(gcoeff(r,j,j));
    1540        1729 :     for (i=1; i<j; i++) q[i][j] = rtodbl(gcoeff(r,i,j)); /* |.| <= 1/2 */
    1541             :   }
    1542             : 
    1543          70 :   if( dolll ) {
    1544             :     /* compute U^-1 * tt */
    1545          70 :     uinv = ZM_inv(u, &pd);
    1546          70 :     tv = RgM_RgC_mul(uinv, target);
    1547         392 :     for (j=1; j<=n; j++)
    1548             :     {
    1549         322 :       tt[j] = gtodouble(gel(tv, j));
    1550             :     }
    1551             :   } else {
    1552           0 :     for (j=1; j<=n; j++)
    1553             :     {
    1554           0 :       tt[j] = gtodouble(gel(target, j));
    1555             :     }
    1556             :   }
    1557             : 
    1558          70 :   if (sBORNE) maxnorm = 0.;
    1559             :   else
    1560             :   {
    1561          28 :     GEN B = gcoeff(a,1,1);
    1562         112 :     for (i = 2; i <= n; i++)
    1563          84 :       B = addii(B, gcoeff(a,i,i));
    1564          28 :     maxnorm = -1.; /* don't update maxnorm */
    1565          28 :     if (is_bigint(B)) return NULL;
    1566          28 :     sBORNE = 0.;
    1567         140 :     for(i=1; i<=n; i++)
    1568         112 :       sBORNE += v[i];
    1569             :   }
    1570          70 :   BOUND = sBORNE * (1 + eps);
    1571             : 
    1572             :   /* precompute contribution of tt to z[l] */
    1573             : 
    1574         392 :   for(k=1; k <= n; k++) {
    1575         322 :     tpre[k] = -tt[k];
    1576        1729 :     for(j=k+1; j<=n; j++) {
    1577        1407 :       tpre[k] -= q[k][j] * tt[j];
    1578             :     }
    1579             :   }
    1580             : 
    1581          70 :   s = 0;
    1582          70 :   k = n; y[n] = 0;
    1583          70 :   z[n] = tpre[n];
    1584          70 :   x[n] = (long)floor(sqrt(BOUND/v[n])-z[n]);
    1585         889 :   for(;;x[1]--)
    1586             :   {
    1587             :     do
    1588             :     {
    1589        8582 :       if (k>1)
    1590             :       {
    1591        7665 :         long l = k-1;
    1592        7665 :         z[l] = tpre[l];
    1593       61488 :         for (j=k; j<=n; j++) z[l] += q[l][j]*x[j];
    1594        7665 :         p = (double)x[k] + z[k];
    1595        7665 :         y[l] = y[k] + p*p*v[k];
    1596        7665 :         x[l] = (long)floor(sqrt((BOUND-y[l])/v[l])-z[l]);
    1597        7665 :         k = l;
    1598             :       }
    1599             :       for(;;)
    1600             :       {
    1601       16247 :         p = (double)x[k] + z[k];
    1602       16247 :         if (y[k] + p*p*v[k] <= BOUND) break;
    1603        7735 :         if (k >= n) {
    1604          70 :           done = 1;
    1605          70 :           break;
    1606             :         }
    1607        7665 :         k++; x[k]--;
    1608             :       }
    1609             :     }
    1610        8582 :     while (k > 1 && !done);
    1611         959 :     if (done) break;
    1612             : 
    1613         889 :     p = (double)x[1] + z[1];
    1614         889 :     p = y[1] + p*p*v[1]; /* norm(x-target) */
    1615         889 :     if (maxnorm >= 0)
    1616             :     {
    1617         175 :       if (p > maxnorm) maxnorm = p;
    1618             :     }
    1619             :     else
    1620             :     { /* maxnorm < 0 : only look for closest vectors */
    1621         714 :       if (p * (1+10*eps) < sBORNE) {
    1622         231 :         sBORNE = p; set_avma(av1);
    1623         231 :         BOUND = sBORNE * (1+eps);
    1624         231 :         L = new_chunk(maxrank+1);
    1625         231 :         s = 0;
    1626             :       }
    1627             :     }
    1628         889 :     s++;
    1629             : 
    1630         889 :     switch(flag)
    1631             :     {
    1632           0 :       case min_FIRST:
    1633           0 :         if (dolll) x = ZM_zc_mul(u,x);
    1634           0 :         return gerepilecopy(av, mkvec2(dbltor(p), x));
    1635             : 
    1636         889 :       case min_ALL:
    1637         889 :         if (s > maxrank && stockall) /* overflow */
    1638             :         {
    1639           0 :           long maxranknew = maxrank << 1;
    1640           0 :           GEN Lnew = new_chunk(1 + maxranknew);
    1641           0 :           for (i=1; i<=maxrank; i++) Lnew[i] = L[i];
    1642           0 :           L = Lnew; maxrank = maxranknew;
    1643             :         }
    1644         889 :         if (s<=maxrank) gel(L,s) = leafcopy(x);
    1645         889 :         break;
    1646             :     }
    1647             :   }
    1648          70 :   switch(flag)
    1649             :   {
    1650           0 :     case min_FIRST:
    1651           0 :       set_avma(av); return cgetg(1,t_VEC);
    1652             :   }
    1653          70 :   r = (maxnorm >= 0) ? dbltor(maxnorm): dbltor(sBORNE);
    1654          70 :   k = minss(s,maxrank);
    1655          70 :   L[0] = evaltyp(t_MAT) | evallg(k + 1);
    1656         322 :   for (j=1; j<=k; j++)
    1657         252 :     gel(L,j) = (dolll==1) ? ZM_zc_mul(u, gel(L,j)) : zc_to_ZC(gel(L,j));
    1658          70 :   return gerepilecopy(av, mkvec3(stoi(s), r, L));
    1659             : }
    1660             : 
    1661             : static GEN
    1662         553 : minim0(GEN a, GEN BORNE, GEN STOCKMAX, long flag)
    1663             : {
    1664         553 :   GEN v = minim0_dolll(a, BORNE, STOCKMAX, flag, 1);
    1665         552 :   if (!v) pari_err_PREC("qfminim");
    1666         546 :   return v;
    1667             : }
    1668             : 
    1669             : static GEN
    1670          91 : cvp0(GEN a, GEN target, GEN BORNE, GEN STOCKMAX, long flag)
    1671             : {
    1672          91 :   GEN v = cvp0_dolll(a, target, BORNE, STOCKMAX, flag, 1);
    1673          77 :   if (!v) pari_err_PREC("qfcvp");
    1674          77 :   return v;
    1675             : }
    1676             : 
    1677             : static GEN
    1678         252 : minim0_zm(GEN a, GEN BORNE, GEN STOCKMAX, long flag)
    1679             : {
    1680         252 :   GEN v = minim0_dolll(a, BORNE, STOCKMAX, flag, 2);
    1681         252 :   if (!v) pari_err_PREC("qfminim");
    1682         252 :   return v;
    1683             : }
    1684             : 
    1685             : GEN
    1686         462 : qfrep0(GEN a, GEN borne, long flag)
    1687         462 : { return minim0(a, borne, gen_0, (flag & 1)? min_VECSMALL2: min_VECSMALL); }
    1688             : 
    1689             : GEN
    1690         133 : qfminim0(GEN a, GEN borne, GEN stockmax, long flag, long prec)
    1691             : {
    1692         133 :   switch(flag)
    1693             :   {
    1694          49 :     case 0: return minim0(a,borne,stockmax,min_ALL);
    1695          35 :     case 1: return minim0(a,borne,gen_0   ,min_FIRST);
    1696          49 :     case 2:
    1697             :     {
    1698          49 :       long maxnum = -1;
    1699          49 :       if (typ(a) != t_MAT) pari_err_TYPE("qfminim",a);
    1700          49 :       if (stockmax) {
    1701          14 :         if (typ(stockmax) != t_INT) pari_err_TYPE("qfminim",stockmax);
    1702          14 :         maxnum = itos(stockmax);
    1703             :       }
    1704          49 :       a = fincke_pohst(a,borne,maxnum,prec,NULL);
    1705          42 :       if (!a) pari_err_PREC("qfminim");
    1706          42 :       return a;
    1707             :     }
    1708           0 :     default: pari_err_FLAG("qfminim");
    1709             :   }
    1710             :   return NULL; /* LCOV_EXCL_LINE */
    1711             : }
    1712             : 
    1713             : 
    1714             : GEN
    1715          91 : qfcvp0(GEN a, GEN target, GEN borne, GEN stockmax, long flag)
    1716             : {
    1717          91 :   switch(flag)
    1718             :   {
    1719          91 :     case 0: return cvp0(a,target,borne,stockmax,min_ALL);
    1720           0 :     case 1: return cvp0(a,target,borne,gen_0   ,min_FIRST);
    1721             :     /* case 2:
    1722             :        TODO: more robust finke_pohst enumeration */
    1723           0 :     default: pari_err_FLAG("qfcvp");
    1724             :   }
    1725             :   return NULL; /* LCOV_EXCL_LINE */
    1726             : }
    1727             : 
    1728             : GEN
    1729           7 : minim(GEN a, GEN borne, GEN stockmax)
    1730           7 : { return minim0(a,borne,stockmax,min_ALL); }
    1731             : 
    1732             : GEN
    1733         252 : minim_zm(GEN a, GEN borne, GEN stockmax)
    1734         252 : { return minim0_zm(a,borne,stockmax,min_ALL); }
    1735             : 
    1736             : GEN
    1737          42 : minim_raw(GEN a, GEN BORNE, GEN STOCKMAX)
    1738          42 : { return minim0_dolll(a, BORNE, STOCKMAX, min_ALL, 0); }
    1739             : 
    1740             : GEN
    1741           0 : minim2(GEN a, GEN borne, GEN stockmax)
    1742           0 : { return minim0(a,borne,stockmax,min_FIRST); }
    1743             : 
    1744             : /* If V depends linearly from the columns of the matrix, return 0.
    1745             :  * Otherwise, update INVP and L and return 1. No GC. */
    1746             : static int
    1747        1652 : addcolumntomatrix(GEN V, GEN invp, GEN L)
    1748             : {
    1749        1652 :   long i,j,k, n = lg(invp);
    1750        1652 :   GEN a = cgetg(n, t_COL), ak = NULL, mak;
    1751             : 
    1752       84231 :   for (k = 1; k < n; k++)
    1753       83706 :     if (!L[k])
    1754             :     {
    1755       27902 :       ak = RgMrow_zc_mul(invp, V, k);
    1756       27902 :       if (!gequal0(ak)) break;
    1757             :     }
    1758        1652 :   if (k == n) return 0;
    1759        1127 :   L[k] = 1;
    1760        1127 :   mak = gneg_i(ak);
    1761       43253 :   for (i=k+1; i<n; i++)
    1762       42126 :     gel(a,i) = gdiv(RgMrow_zc_mul(invp, V, i), mak);
    1763       43883 :   for (j=1; j<=k; j++)
    1764             :   {
    1765       42756 :     GEN c = gel(invp,j), ck = gel(c,k);
    1766       42756 :     if (gequal0(ck)) continue;
    1767        8757 :     gel(c,k) = gdiv(ck, ak);
    1768        8757 :     if (j==k)
    1769       43253 :       for (i=k+1; i<n; i++)
    1770       42126 :         gel(c,i) = gmul(gel(a,i), ck);
    1771             :     else
    1772      184814 :       for (i=k+1; i<n; i++)
    1773      177184 :         gel(c,i) = gadd(gel(c,i), gmul(gel(a,i), ck));
    1774             :   }
    1775        1127 :   return 1;
    1776             : }
    1777             : 
    1778             : GEN
    1779          42 : qfperfection(GEN a)
    1780             : {
    1781          42 :   pari_sp av = avma;
    1782             :   GEN u, L;
    1783          42 :   long r, s, k, l, n = lg(a)-1;
    1784             : 
    1785          42 :   if (!n) return gen_0;
    1786          42 :   if (typ(a) != t_MAT || !RgM_is_ZM(a)) pari_err_TYPE("qfperfection",a);
    1787          42 :   a = minim_lll(a, &u);
    1788          42 :   L = minim_raw(a,NULL,NULL);
    1789          42 :   r = (n*(n+1)) >> 1;
    1790          42 :   if (L)
    1791             :   {
    1792             :     GEN D, V, invp;
    1793          35 :     L = gel(L, 3); l = lg(L);
    1794          35 :     if (l == 2) { set_avma(av); return gen_1; }
    1795             :     /* |L[i]|^2 fits  into a long for all i */
    1796          21 :     D = zero_zv(r);
    1797          21 :     V = cgetg(r+1, t_VECSMALL);
    1798          21 :     invp = matid(r);
    1799          21 :     s = 0;
    1800        1659 :     for (k = 1; k < l; k++)
    1801             :     {
    1802        1652 :       pari_sp av2 = avma;
    1803        1652 :       GEN x = gel(L,k);
    1804             :       long i, j, I;
    1805       21098 :       for (i = I = 1; i<=n; i++)
    1806      145278 :         for (j=i; j<=n; j++,I++) V[I] = x[i]*x[j];
    1807        1652 :       if (!addcolumntomatrix(V,invp,D)) set_avma(av2);
    1808        1127 :       else if (++s == r) break;
    1809             :     }
    1810             :   }
    1811             :   else
    1812             :   {
    1813             :     GEN M;
    1814           7 :     L = fincke_pohst(a,NULL,-1, DEFAULTPREC, NULL);
    1815           7 :     if (!L) pari_err_PREC("qfminim");
    1816           7 :     L = gel(L, 3); l = lg(L);
    1817           7 :     if (l == 2) { set_avma(av); return gen_1; }
    1818           7 :     M = cgetg(l, t_MAT);
    1819         959 :     for (k = 1; k < l; k++)
    1820             :     {
    1821         952 :       GEN x = gel(L,k), c = cgetg(r+1, t_COL);
    1822             :       long i, I, j;
    1823       16184 :       for (i = I = 1; i<=n; i++)
    1824      144704 :         for (j=i; j<=n; j++,I++) gel(c,I) = mulii(gel(x,i), gel(x,j));
    1825         952 :       gel(M,k) = c;
    1826             :     }
    1827           7 :     s = ZM_rank(M);
    1828             :   }
    1829          28 :   return gc_utoipos(av, s);
    1830             : }
    1831             : 
    1832             : static GEN
    1833         140 : clonefill(GEN S, long s, long t)
    1834             : { /* initialize to dummy values */
    1835         140 :   GEN T = S, dummy = cgetg(1, t_STR);
    1836             :   long i;
    1837      308745 :   for (i = s+1; i <= t; i++) gel(S,i) = dummy;
    1838         140 :   S = gclone(S); if (isclone(T)) gunclone(T);
    1839         140 :   return S;
    1840             : }
    1841             : 
    1842             : /* increment ZV x, by incrementing cell of index k. Initial value x0[k] was
    1843             :  * chosen to minimize qf(x) for given x0[1..k-1] and x0[k+1,..] = 0
    1844             :  * The last nonzero entry must be positive and goes through x0[k]+1,2,3,...
    1845             :  * Others entries go through: x0[k]+1,-1,2,-2,...*/
    1846             : INLINE void
    1847     2950291 : step(GEN x, GEN y, GEN inc, long k)
    1848             : {
    1849     2950291 :   if (!signe(gel(y,k))) /* x[k+1..] = 0 */
    1850      160682 :     gel(x,k) = addiu(gel(x,k), 1); /* leading coeff > 0 */
    1851             :   else
    1852             :   {
    1853     2789609 :     long i = inc[k];
    1854     2789609 :     gel(x,k) = addis(gel(x,k), i),
    1855     2789615 :     inc[k] = (i > 0)? -1-i: 1-i;
    1856             :   }
    1857     2950298 : }
    1858             : 
    1859             : /* 1 if we are "sure" that x < y, up to few rounding errors, i.e.
    1860             :  * x < y - epsilon. More precisely :
    1861             :  * - sign(x - y) < 0
    1862             :  * - lgprec(x-y) > 3 || expo(x - y) - expo(x) > -24 */
    1863             : static int
    1864     1216172 : mplessthan(GEN x, GEN y)
    1865             : {
    1866     1216172 :   pari_sp av = avma;
    1867     1216172 :   GEN z = mpsub(x, y);
    1868     1216170 :   set_avma(av);
    1869     1216171 :   if (typ(z) == t_INT) return (signe(z) < 0);
    1870     1216171 :   if (signe(z) >= 0) return 0;
    1871       22405 :   if (realprec(z) > LOWDEFAULTPREC) return 1;
    1872       22405 :   return ( expo(z) - mpexpo(x) > -24 );
    1873             : }
    1874             : 
    1875             : /* 1 if we are "sure" that x > y, up to few rounding errors, i.e.
    1876             :  * x > y + epsilon */
    1877             : static int
    1878     4616817 : mpgreaterthan(GEN x, GEN y)
    1879             : {
    1880     4616817 :   pari_sp av = avma;
    1881     4616817 :   GEN z = mpsub(x, y);
    1882     4616820 :   set_avma(av);
    1883     4616838 :   if (typ(z) == t_INT) return (signe(z) > 0);
    1884     4616838 :   if (signe(z) <= 0) return 0;
    1885     2691004 :   if (realprec(z) > LOWDEFAULTPREC) return 1;
    1886      477514 :   return ( expo(z) - mpexpo(x) > -24 );
    1887             : }
    1888             : 
    1889             : /* x a t_INT, y  t_INT or t_REAL */
    1890             : INLINE GEN
    1891     1228228 : mulimp(GEN x, GEN y)
    1892             : {
    1893     1228228 :   if (typ(y) == t_INT) return mulii(x,y);
    1894     1228228 :   return signe(x) ? mulir(x,y): gen_0;
    1895             : }
    1896             : /* x + y*z, x,z two mp's, y a t_INT */
    1897             : INLINE GEN
    1898    13537333 : addmulimp(GEN x, GEN y, GEN z)
    1899             : {
    1900    13537333 :   if (!signe(y)) return x;
    1901     5830745 :   if (typ(z) == t_INT) return mpadd(x, mulii(y, z));
    1902     5830745 :   return mpadd(x, mulir(y, z));
    1903             : }
    1904             : 
    1905             : /* yk + vk * (xk + zk)^2 */
    1906             : static GEN
    1907     5775269 : norm_aux(GEN xk, GEN yk, GEN zk, GEN vk)
    1908             : {
    1909     5775269 :   GEN t = mpadd(xk, zk);
    1910     5775260 :   if (typ(t) == t_INT) { /* probably gen_0, avoid loss of accuracy */
    1911      305967 :     yk = addmulimp(yk, sqri(t), vk);
    1912             :   } else {
    1913     5469293 :     yk = mpadd(yk, mpmul(sqrr(t), vk));
    1914             :   }
    1915     5775246 :   return yk;
    1916             : }
    1917             : /* yk + vk * (xk + zk)^2 < B + epsilon */
    1918             : static int
    1919     4164568 : check_bound(GEN B, GEN xk, GEN yk, GEN zk, GEN vk)
    1920             : {
    1921     4164568 :   pari_sp av = avma;
    1922     4164568 :   int f = mpgreaterthan(norm_aux(xk,yk,zk,vk), B);
    1923     4164569 :   return gc_bool(av, !f);
    1924             : }
    1925             : 
    1926             : /* q(k-th canonical basis vector), where q is given in Cholesky form
    1927             :  * q(x) = sum_{i = 1}^n q[i,i] (x[i] + sum_{j > i} q[i,j] x[j])^2.
    1928             :  * Namely q(e_k) = q[k,k] + sum_{i < k} q[i,i] q[i,k]^2
    1929             :  * Assume 1 <= k <= n. */
    1930             : static GEN
    1931         182 : cholesky_norm_ek(GEN q, long k)
    1932             : {
    1933         182 :   GEN t = gcoeff(q,k,k);
    1934             :   long i;
    1935        1484 :   for (i = 1; i < k; i++) t = norm_aux(gen_0, t, gcoeff(q,i,k), gcoeff(q,i,i));
    1936         182 :   return t;
    1937             : }
    1938             : 
    1939             : /* q is the Cholesky decomposition of a quadratic form
    1940             :  * Enumerate vectors whose norm is less than BORNE (Algo 2.5.7),
    1941             :  * minimal vectors if BORNE = NULL (implies check = NULL).
    1942             :  * If (check != NULL) consider only vectors passing the check, and assumes
    1943             :  *   we only want the smallest possible vectors */
    1944             : static GEN
    1945       14692 : smallvectors(GEN q, GEN BORNE, long maxnum, FP_chk_fun *CHECK)
    1946             : {
    1947       14692 :   long N = lg(q), n = N-1, i, j, k, s, stockmax, checkcnt = 1;
    1948             :   pari_sp av, av1;
    1949             :   GEN inc, S, x, y, z, v, p1, alpha, norms;
    1950             :   GEN norme1, normax1, borne1, borne2;
    1951       14692 :   GEN (*check)(void *,GEN) = CHECK? CHECK->f: NULL;
    1952       14692 :   void *data = CHECK? CHECK->data: NULL;
    1953       14692 :   const long skipfirst = CHECK? CHECK->skipfirst: 0;
    1954       14692 :   const int stockall = (maxnum == -1);
    1955             : 
    1956       14692 :   alpha = dbltor(0.95);
    1957       14692 :   normax1 = gen_0;
    1958             : 
    1959       14692 :   v = cgetg(N,t_VEC);
    1960       14692 :   inc = const_vecsmall(n, 1);
    1961             : 
    1962       14692 :   av = avma;
    1963       14692 :   stockmax = stockall? 2000: maxnum;
    1964       14692 :   norms = cgetg(check?(stockmax+1): 1,t_VEC); /* unused if (!check) */
    1965       14692 :   S = cgetg(stockmax+1,t_VEC);
    1966       14692 :   x = cgetg(N,t_COL);
    1967       14692 :   y = cgetg(N,t_COL);
    1968       14692 :   z = cgetg(N,t_COL);
    1969       97716 :   for (i=1; i<N; i++) {
    1970       83024 :     gel(v,i) = gcoeff(q,i,i);
    1971       83024 :     gel(x,i) = gel(y,i) = gel(z,i) = gen_0;
    1972             :   }
    1973       14692 :   if (BORNE)
    1974             :   {
    1975       14671 :     borne1 = BORNE;
    1976       14671 :     if (gsigne(borne1) <= 0) retmkvec3(gen_0, gen_0, cgetg(1,t_MAT));
    1977       14657 :     if (typ(borne1) != t_REAL)
    1978             :     {
    1979             :       long prec;
    1980         419 :       prec = nbits2prec(gexpo(borne1) + 10);
    1981         419 :       borne1 = gtofp(borne1, maxss(prec, DEFAULTPREC));
    1982             :     }
    1983             :   }
    1984             :   else
    1985             :   {
    1986          21 :     borne1 = gcoeff(q,1,1);
    1987         203 :     for (i=2; i<N; i++)
    1988             :     {
    1989         182 :       GEN b = cholesky_norm_ek(q, i);
    1990         182 :       if (gcmp(b, borne1) < 0) borne1 = b;
    1991             :     }
    1992             :     /* borne1 = norm of smallest basis vector */
    1993             :   }
    1994       14678 :   borne2 = mulrr(borne1,alpha);
    1995       14678 :   if (DEBUGLEVEL>2)
    1996           0 :     err_printf("smallvectors looking for norm < %P.4G\n",borne1);
    1997       14678 :   s = 0; k = n;
    1998      381915 :   for(;; step(x,y,inc,k)) /* main */
    1999             :   { /* x (supposedly) small vector, ZV.
    2000             :      * For all t >= k, we have
    2001             :      *   z[t] = sum_{j > t} q[t,j] * x[j]
    2002             :      *   y[t] = sum_{i > t} q[i,i] * (x[i] + z[i])^2
    2003             :      *        = 0 <=> x[i]=0 for all i>t */
    2004             :     do
    2005             :     {
    2006     1610143 :       int skip = 0;
    2007     1610143 :       if (k > 1)
    2008             :       {
    2009     1228228 :         long l = k-1;
    2010     1228228 :         av1 = avma;
    2011     1228228 :         p1 = mulimp(gel(x,k), gcoeff(q,l,k));
    2012    14459623 :         for (j=k+1; j<N; j++) p1 = addmulimp(p1, gel(x,j), gcoeff(q,l,j));
    2013     1228229 :         gel(z,l) = gerepileuptoleaf(av1,p1);
    2014             : 
    2015     1228232 :         av1 = avma;
    2016     1228232 :         p1 = norm_aux(gel(x,k), gel(y,k), gel(z,k), gel(v,k));
    2017     1228230 :         gel(y,l) = gerepileuptoleaf(av1, p1);
    2018             :         /* skip the [x_1,...,x_skipfirst,0,...,0] */
    2019     1228231 :         if ((l <= skipfirst && !signe(gel(y,skipfirst)))
    2020     1228231 :          || mplessthan(borne1, gel(y,l))) skip = 1;
    2021             :         else /* initial value, minimizing (x[l] + z[l])^2, hence qf(x) for
    2022             :                 the given x[1..l-1] */
    2023     1214280 :           gel(x,l) = mpround( mpneg(gel(z,l)) );
    2024     1228230 :         k = l;
    2025             :       }
    2026     1228230 :       for(;; step(x,y,inc,k))
    2027             :       { /* at most 2n loops */
    2028     2838376 :         if (!skip)
    2029             :         {
    2030     2824427 :           if (check_bound(borne1, gel(x,k),gel(y,k),gel(z,k),gel(v,k))) break;
    2031     1340153 :           step(x,y,inc,k);
    2032     1340160 :           if (check_bound(borne1, gel(x,k),gel(y,k),gel(z,k),gel(v,k))) break;
    2033             :         }
    2034     1242908 :         skip = 0; inc[k] = 1;
    2035     1242908 :         if (++k > n) goto END;
    2036             :       }
    2037             : 
    2038     1595473 :       if (gc_needed(av,2))
    2039             :       {
    2040          15 :         if(DEBUGMEM>1) pari_warn(warnmem,"smallvectors");
    2041          15 :         if (stockmax) S = clonefill(S, s, stockmax);
    2042          15 :         if (check) {
    2043          15 :           GEN dummy = cgetg(1, t_STR);
    2044        9629 :           for (i=s+1; i<=stockmax; i++) gel(norms,i) = dummy;
    2045             :         }
    2046          15 :         gerepileall(av,7,&x,&y,&z,&normax1,&borne1,&borne2,&norms);
    2047             :       }
    2048             :     }
    2049     1595473 :     while (k > 1);
    2050      381915 :     if (!signe(gel(x,1)) && !signe(gel(y,1))) continue; /* exclude 0 */
    2051             : 
    2052      381186 :     av1 = avma;
    2053      381186 :     norme1 = norm_aux(gel(x,1),gel(y,1),gel(z,1),gel(v,1));
    2054      381185 :     if (mpgreaterthan(norme1,borne1)) { set_avma(av1); continue; /* main */ }
    2055             : 
    2056      381186 :     norme1 = gerepileuptoleaf(av1,norme1);
    2057      381186 :     if (check)
    2058             :     {
    2059      312600 :       if (checkcnt < 5 && mpcmp(norme1, borne2) < 0)
    2060             :       {
    2061        4406 :         if (!check(data,x)) { checkcnt++ ; continue; /* main */}
    2062         496 :         if (DEBUGLEVEL>4) err_printf("New bound: %Ps", norme1);
    2063         496 :         borne1 = norme1;
    2064         496 :         borne2 = mulrr(borne1, alpha);
    2065         496 :         s = 0; checkcnt = 0;
    2066             :       }
    2067             :     }
    2068             :     else
    2069             :     {
    2070       68586 :       if (!BORNE) /* find minimal vectors */
    2071             :       {
    2072        1890 :         if (mplessthan(norme1, borne1))
    2073             :         { /* strictly smaller vector than previously known */
    2074           0 :           borne1 = norme1; /* + epsilon */
    2075           0 :           s = 0;
    2076             :         }
    2077             :       }
    2078             :       else
    2079       66696 :         if (mpcmp(norme1,normax1) > 0) normax1 = norme1;
    2080             :     }
    2081      377276 :     if (++s > stockmax) continue; /* too many vectors: no longer remember */
    2082      376345 :     if (check) gel(norms,s) = norme1;
    2083      376345 :     gel(S,s) = leafcopy(x);
    2084      376345 :     if (s != stockmax) continue; /* still room, get next vector */
    2085             : 
    2086         125 :     if (check)
    2087             :     { /* overflow, eliminate vectors failing "check" */
    2088         104 :       pari_sp av2 = avma;
    2089             :       long imin, imax;
    2090         104 :       GEN per = indexsort(norms), S2 = cgetg(stockmax+1, t_VEC);
    2091         104 :       if (DEBUGLEVEL>2) err_printf("sorting... [%ld elts]\n",s);
    2092             :       /* let N be the minimal norm so far for x satisfying 'check'. Keep
    2093             :        * all elements of norm N */
    2094       24689 :       for (i = 1; i <= s; i++)
    2095             :       {
    2096       24683 :         long k = per[i];
    2097       24683 :         if (check(data,gel(S,k))) { borne1 = gel(norms,k); break; }
    2098             :       }
    2099         104 :       imin = i;
    2100       21113 :       for (; i <= s; i++)
    2101       21093 :         if (mpgreaterthan(gel(norms,per[i]), borne1)) break;
    2102         104 :       imax = i;
    2103       21113 :       for (i=imin, s=0; i < imax; i++) gel(S2,++s) = gel(S,per[i]);
    2104       21113 :       for (i = 1; i <= s; i++) gel(S,i) = gel(S2,i);
    2105         104 :       set_avma(av2);
    2106         104 :       if (s) { borne2 = mulrr(borne1, alpha); checkcnt = 0; }
    2107         104 :       if (!stockall) continue;
    2108         104 :       if (s > stockmax/2) stockmax <<= 1;
    2109         104 :       norms = cgetg(stockmax+1, t_VEC);
    2110       21113 :       for (i = 1; i <= s; i++) gel(norms,i) = borne1;
    2111             :     }
    2112             :     else
    2113             :     {
    2114          21 :       if (!stockall && BORNE) goto END;
    2115          21 :       if (!stockall) continue;
    2116          21 :       stockmax <<= 1;
    2117             :     }
    2118             : 
    2119             :     {
    2120         125 :       GEN Snew = clonefill(vec_lengthen(S,stockmax), s, stockmax);
    2121         125 :       if (isclone(S)) gunclone(S);
    2122         125 :       S = Snew;
    2123             :     }
    2124             :   }
    2125       14678 : END:
    2126       14678 :   if (s < stockmax) stockmax = s;
    2127       14678 :   if (check)
    2128             :   {
    2129             :     GEN per, alph, pols, p;
    2130       14650 :     if (DEBUGLEVEL>2) err_printf("final sort & check...\n");
    2131       14650 :     setlg(norms,stockmax+1); per = indexsort(norms);
    2132       14650 :     alph = cgetg(stockmax+1,t_VEC);
    2133       14650 :     pols = cgetg(stockmax+1,t_VEC);
    2134       84467 :     for (j=0,i=1; i<=stockmax; i++)
    2135             :     {
    2136       70074 :       long t = per[i];
    2137       70074 :       GEN N = gel(norms,t);
    2138       70074 :       if (j && mpgreaterthan(N, borne1)) break;
    2139       69817 :       if ((p = check(data,gel(S,t))))
    2140             :       {
    2141       55866 :         if (!j) borne1 = N;
    2142       55866 :         j++;
    2143       55866 :         gel(pols,j) = p;
    2144       55866 :         gel(alph,j) = gel(S,t);
    2145             :       }
    2146             :     }
    2147       14650 :     setlg(pols,j+1);
    2148       14650 :     setlg(alph,j+1);
    2149       14650 :     if (stockmax && isclone(S)) { alph = gcopy(alph); gunclone(S); }
    2150       14650 :     return mkvec2(pols, alph);
    2151             :   }
    2152          28 :   if (stockmax)
    2153             :   {
    2154          21 :     setlg(S,stockmax+1);
    2155          21 :     settyp(S,t_MAT);
    2156          21 :     if (isclone(S)) { p1 = S; S = gcopy(S); gunclone(p1); }
    2157             :   }
    2158             :   else
    2159           7 :     S = cgetg(1,t_MAT);
    2160          28 :   return mkvec3(utoi(s<<1), borne1, S);
    2161             : }
    2162             : 
    2163             : /* solve q(x) = x~.a.x <= bound, a > 0.
    2164             :  * If check is non-NULL keep x only if check(x).
    2165             :  * If a is a vector, assume a[1] is the LLL-reduced Cholesky form of q */
    2166             : GEN
    2167       14713 : fincke_pohst(GEN a, GEN B0, long stockmax, long PREC, FP_chk_fun *CHECK)
    2168             : {
    2169       14713 :   pari_sp av = avma;
    2170             :   VOLATILE long i,j,l;
    2171       14713 :   VOLATILE GEN r,rinv,rinvtrans,u,v,res,z,vnorm,rperm,perm,uperm, bound = B0;
    2172             : 
    2173       14713 :   if (typ(a) == t_VEC)
    2174             :   {
    2175       14245 :     r = gel(a,1);
    2176       14245 :     u = NULL;
    2177             :   }
    2178             :   else
    2179             :   {
    2180         468 :     long prec = PREC;
    2181         468 :     l = lg(a);
    2182         468 :     if (l == 1)
    2183             :     {
    2184           7 :       if (CHECK) pari_err_TYPE("fincke_pohst [dimension 0]", a);
    2185           7 :       retmkvec3(gen_0, gen_0, cgetg(1,t_MAT));
    2186             :     }
    2187         461 :     u = lllfp(a, 0.75, LLL_GRAM | LLL_IM);
    2188         454 :     if (!u || lg(u) != lg(a)) return gc_NULL(av);
    2189         454 :     r = qf_RgM_apply(a,u);
    2190         454 :     i = gprecision(r);
    2191         454 :     if (i)
    2192         412 :       prec = i;
    2193             :     else {
    2194          42 :       prec = DEFAULTPREC + nbits2extraprec(gexpo(r));
    2195          42 :       if (prec < PREC) prec = PREC;
    2196             :     }
    2197         454 :     if (DEBUGLEVEL>2) err_printf("first LLL: prec = %ld\n", prec);
    2198         454 :     r = qfgaussred_positive(r);
    2199         454 :     if (!r) return gc_NULL(av);
    2200        1984 :     for (i=1; i<l; i++)
    2201             :     {
    2202        1530 :       GEN s = gsqrt(gcoeff(r,i,i), prec);
    2203        1530 :       gcoeff(r,i,i) = s;
    2204        4236 :       for (j=i+1; j<l; j++) gcoeff(r,i,j) = gmul(s, gcoeff(r,i,j));
    2205             :     }
    2206             :   }
    2207             :   /* now r~ * r = a in LLL basis */
    2208       14699 :   rinv = RgM_inv_upper(r);
    2209       14699 :   if (!rinv) return gc_NULL(av);
    2210       14699 :   rinvtrans = shallowtrans(rinv);
    2211       14699 :   if (DEBUGLEVEL>2)
    2212           0 :     err_printf("Fincke-Pohst, final LLL: prec = %ld\n", gprecision(rinvtrans));
    2213       14699 :   v = lll(rinvtrans);
    2214       14699 :   if (lg(v) != lg(rinvtrans)) return gc_NULL(av);
    2215             : 
    2216       14699 :   rinvtrans = RgM_mul(rinvtrans, v);
    2217       14699 :   v = ZM_inv(shallowtrans(v),NULL);
    2218       14699 :   r = RgM_mul(r,v);
    2219       14699 :   u = u? ZM_mul(u,v): v;
    2220             : 
    2221       14699 :   l = lg(r);
    2222       14699 :   vnorm = cgetg(l,t_VEC);
    2223       97751 :   for (j=1; j<l; j++) gel(vnorm,j) = gnorml2(gel(rinvtrans,j));
    2224       14699 :   rperm = cgetg(l,t_MAT);
    2225       14699 :   uperm = cgetg(l,t_MAT); perm = indexsort(vnorm);
    2226       97751 :   for (i=1; i<l; i++) { uperm[l-i] = u[perm[i]]; rperm[l-i] = r[perm[i]]; }
    2227       14699 :   u = uperm;
    2228       14699 :   r = rperm; res = NULL;
    2229       14699 :   pari_CATCH(e_PREC) { }
    2230             :   pari_TRY {
    2231             :     GEN q;
    2232       14699 :     if (CHECK && CHECK->f_init) bound = CHECK->f_init(CHECK, r, u);
    2233       14692 :     q = gaussred_from_QR(r, gprecision(vnorm));
    2234       14692 :     if (q) res = smallvectors(q, bound, stockmax, CHECK);
    2235       14692 :   } pari_ENDCATCH;
    2236       14699 :   if (!res) return gc_NULL(av);
    2237       14692 :   if (CHECK)
    2238             :   {
    2239       14650 :     if (CHECK->f_post) res = CHECK->f_post(CHECK, res, u);
    2240       14650 :     return res;
    2241             :   }
    2242             : 
    2243          42 :   z = cgetg(4,t_VEC);
    2244          42 :   gel(z,1) = gcopy(gel(res,1));
    2245          42 :   gel(z,2) = gcopy(gel(res,2));
    2246          42 :   gel(z,3) = ZM_mul(u, gel(res,3)); return gerepileupto(av,z);
    2247             : }

Generated by: LCOV version 1.16