Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - bibli1.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21072-998352a) Lines: 1033 1089 94.9 %
Date: 2017-09-26 06:25:23 Functions: 64 70 91.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11