Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - hnf_snf.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23332-367b47754) Lines: 1508 1658 91.0 %
Date: 2018-12-10 05:41:52 Functions: 86 92 93.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. 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             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : /**************************************************************/
      17             : /**                                                          **/
      18             : /**                HERMITE NORMAL FORM REDUCTION             **/
      19             : /**                                                          **/
      20             : /**************************************************************/
      21             : static GEN ZV_hnfgcdext(GEN A);
      22             : static GEN
      23          14 : hnfallgen(GEN x)
      24             : {
      25          14 :   GEN z = cgetg(3, t_VEC);
      26          14 :   gel(z,1) = RgM_hnfall(x, (GEN*)(z+2), 1);
      27          14 :   return z;
      28             : }
      29             : GEN
      30         217 : mathnf0(GEN x, long flag)
      31             : {
      32         217 :   switch(typ(x))
      33             :   {
      34             :     case t_VEC:
      35          70 :       if (RgV_is_ZV(x))
      36          42 :         switch (flag)
      37             :         {
      38             :           case 0:
      39          14 :             if (lg(x) == 1) return cgetg(1, t_MAT);
      40           7 :             retmkmat(mkcol(ZV_content(x)));
      41             :           case 1:
      42             :           case 4:
      43          21 :             return ZV_hnfgcdext(x);
      44             :         }
      45          35 :       x = gtomat(x); break;
      46         147 :     case t_MAT: break;
      47           0 :     default: pari_err_TYPE("mathnf0",x);
      48             :   }
      49             : 
      50         182 :   switch(flag)
      51             :   {
      52         133 :     case 0: case 2: return RgM_is_ZM(x)? ZM_hnf(x): RgM_hnfall(x,NULL,1);
      53          28 :     case 1: case 3: return RgM_is_ZM(x)? hnfall(x): hnfallgen(x);
      54           7 :     case 4: RgM_check_ZM(x, "mathnf0"); return hnflll(x);
      55          14 :     case 5: RgM_check_ZM(x, "mathnf0"); return hnfperm(x);
      56           0 :     default: pari_err_FLAG("mathnf");
      57             :   }
      58             :   return NULL; /* LCOV_EXCL_LINE */
      59             : }
      60             : 
      61             : /*******************************************************************/
      62             : /*                                                                 */
      63             : /*                SPECIAL HNF (FOR INTERNAL USE !!!)               */
      64             : /*                                                                 */
      65             : /*******************************************************************/
      66             : static int
      67     3873107 : count(GEN mat, long row, long len, long *firstnonzero)
      68             : {
      69     3873107 :   long j, n = 0;
      70             : 
      71   348590505 :   for (j=1; j<=len; j++)
      72             :   {
      73   345904574 :     long p = mael(mat,j,row);
      74   345904574 :     if (p)
      75             :     {
      76    10795992 :       if (labs(p)!=1) return -1;
      77     9608816 :       n++; *firstnonzero=j;
      78             :     }
      79             :   }
      80     2685931 :   return n;
      81             : }
      82             : 
      83             : static int
      84      157465 : count2(GEN mat, long row, long len)
      85             : {
      86             :   long j;
      87     4539449 :   for (j=len; j; j--)
      88     4460319 :     if (labs(mael(mat,j,row)) == 1) return j;
      89       79130 :   return 0;
      90             : }
      91             : 
      92             : static GEN
      93       48298 : hnffinal(GEN matgen,GEN perm,GEN* ptdep,GEN* ptB,GEN* ptC)
      94             : {
      95             :   GEN p1,p2,U,H,Hnew,Bnew,Cnew,diagH1;
      96       48298 :   GEN B = *ptB, C = *ptC, dep = *ptdep, depnew;
      97             :   pari_sp av;
      98             :   long i,j,k,s,i1,j1,zc;
      99       48298 :   long co = lg(C);
     100       48298 :   long col = lg(matgen)-1;
     101             :   long lnz, nlze, lig;
     102             : 
     103       48298 :   if (col == 0) return matgen;
     104       47815 :   lnz = nbrows(matgen); /* was called lnz-1 - nr in hnfspec */
     105       47815 :   nlze = nbrows(dep);
     106       47815 :   lig = nlze + lnz;
     107             :   /* H: lnz x lnz [disregarding initial 0 cols], U: col x col */
     108       47815 :   H = ZM_hnflll(matgen, &U, 0);
     109       47815 :   H += (lg(H)-1 - lnz); H[0] = evaltyp(t_MAT) | evallg(lnz+1);
     110             :   /* Only keep the part above the H (above the 0s is 0 since the dep rows
     111             :    * are dependent from the ones in matgen) */
     112       47815 :   zc = col - lnz; /* # of 0 columns, correspond to units */
     113       47815 :   if (nlze) { dep = ZM_mul(dep,U); dep += zc; }
     114             : 
     115       47815 :   diagH1 = new_chunk(lnz+1); /* diagH1[i] = 0 iff H[i,i] != 1 (set later) */
     116             : 
     117       47815 :   av = avma;
     118       47815 :   Cnew = cgetg(co, typ(C));
     119       47815 :   setlg(C, col+1); p1 = gmul(C,U);
     120       47815 :   for (j=1; j<=col; j++) gel(Cnew,j) = gel(p1,j);
     121       47815 :   for (   ; j<co ; j++)  gel(Cnew,j) = gel(C,j);
     122             : 
     123             :   /* Clean up B using new H */
     124      222001 :   for (s=0,i=lnz; i; i--)
     125             :   {
     126      174186 :     GEN Di = gel(dep,i), Hi = gel(H,i);
     127      174186 :     GEN h = gel(Hi,i); /* H[i,i] */
     128      174186 :     if ( (diagH1[i] = is_pm1(h)) ) { h = NULL; s++; }
     129    10582862 :     for (j=col+1; j<co; j++)
     130             :     {
     131    10408676 :       GEN z = gel(B,j-col);
     132    10408676 :       p1 = gel(z,i+nlze);
     133    10408676 :       if (h) p1 = truedivii(p1,h);
     134    10408676 :       if (!signe(p1)) continue;
     135     6375255 :       for (k=1; k<=nlze; k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Di,k)));
     136     6375255 :       for (   ; k<=lig;  k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Hi,k-nlze)));
     137     6375255 :       gel(Cnew,j) = gsub(gel(Cnew,j), gmul(p1, gel(Cnew,i+zc)));
     138             :     }
     139      174186 :     if (gc_needed(av,3))
     140             :     {
     141         126 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnffinal, i = %ld",i);
     142         126 :       gerepileall(av, 2, &Cnew, &B);
     143             :     }
     144             :   }
     145       47815 :   p1 = cgetg(lnz+1,t_VEC); p2 = perm + nlze;
     146      222001 :   for (i1=0, j1=lnz-s, i=1; i<=lnz; i++) /* push the 1 rows down */
     147      174186 :     if (diagH1[i])
     148       81962 :       gel(p1,++j1) = gel(p2,i);
     149             :     else
     150       92224 :       gel(p2,++i1) = gel(p2,i);
     151       47815 :   for (i=i1+1; i<=lnz; i++) gel(p2,i) = gel(p1,i);
     152             : 
     153             :   /* s = # extra redundant generators taken from H
     154             :    *          zc  col-s  co   zc = col - lnz
     155             :    *       [ 0 |dep |     ]    i = nlze + lnz - s = lig - s
     156             :    *  nlze [--------|  B' ]
     157             :    *       [ 0 | H' |     ]    H' = H minus the s rows with a 1 on diagonal
     158             :    *     i [--------|-----] lig-s           (= "1-rows")
     159             :    *       [   0    | Id  ]
     160             :    *       [        |     ] li */
     161       47815 :   lig -= s; col -= s; lnz -= s;
     162       47815 :   Hnew = cgetg(lnz+1,t_MAT);
     163       47815 :   depnew = cgetg(lnz+1,t_MAT); /* only used if nlze > 0 */
     164       47815 :   Bnew = cgetg(co-col,t_MAT);
     165       47815 :   C = shallowcopy(Cnew);
     166      222001 :   for (j=1,i1=j1=0; j<=lnz+s; j++)
     167             :   {
     168      174186 :     GEN z = gel(H,j);
     169      174186 :     if (diagH1[j])
     170             :     { /* hit exactly s times */
     171       81962 :       i1++; C[i1+col] = Cnew[j+zc];
     172       81962 :       p1 = cgetg(lig+1,t_COL); gel(Bnew,i1) = p1;
     173       81962 :       for (i=1; i<=nlze; i++) gel(p1,i) = gcoeff(dep,i,j);
     174       81962 :       p1 += nlze;
     175             :     }
     176             :     else
     177             :     {
     178       92224 :       j1++; C[j1+zc] = Cnew[j+zc];
     179       92224 :       p1 = cgetg(lnz+1,t_COL); gel(Hnew,j1) = p1;
     180       92224 :       depnew[j1] = dep[j];
     181             :     }
     182      885993 :     for (i=k=1; k<=lnz; i++)
     183      711807 :       if (!diagH1[i]) p1[k++] = z[i];
     184             :   }
     185     1807552 :   for (j=s+1; j<co-col; j++)
     186             :   {
     187     1759737 :     GEN z = gel(B,j-s);
     188     1759737 :     p1 = cgetg(lig+1,t_COL); gel(Bnew,j) = p1;
     189     1759737 :     for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
     190     1759737 :     z += nlze; p1 += nlze;
     191     8361311 :     for (i=k=1; k<=lnz; i++)
     192     6601574 :       if (!diagH1[i]) gel(p1,k++) = gel(z,i);
     193             :   }
     194       47815 :   *ptdep = depnew;
     195       47815 :   *ptC = C;
     196       47815 :   *ptB = Bnew; return Hnew;
     197             : }
     198             : 
     199             : /* for debugging */
     200             : static void
     201           0 : p_mat(GEN mat, GEN perm, long k)
     202             : {
     203           0 :   pari_sp av = avma;
     204           0 :   perm = vecslice(perm, k+1, lg(perm)-1);
     205           0 :   err_printf("Permutation: %Ps\n",perm);
     206           0 :   if (DEBUGLEVEL > 6)
     207           0 :     err_printf("matgen = %Ps\n", zm_to_ZM( rowpermute(mat, perm) ));
     208           0 :   set_avma(av);
     209           0 : }
     210             : 
     211             : static GEN
     212      681837 : col_dup(long l, GEN col)
     213             : {
     214      681837 :   GEN c = new_chunk(l);
     215      681837 :   memcpy(c,col,l * sizeof(long)); return c;
     216             : }
     217             : 
     218             : /* permutation giving imagecompl(x') | image(x'), x' = transpose of x */
     219             : static GEN
     220       47815 : ZM_rowrankprofile(GEN x, long *nlze)
     221             : {
     222       47815 :   pari_sp av = avma;
     223             :   GEN d, y;
     224             :   long i, j, k, l, r;
     225             : 
     226       47815 :   x = shallowtrans(x); l = lg(x);
     227       47815 :   (void)new_chunk(l); /* HACK */
     228       47815 :   d = ZM_pivots(x,&r); set_avma(av);
     229       47815 :   *nlze = r;
     230       47815 :   if (!d) return identity_perm(l-1);
     231       44826 :   y = cgetg(l,t_VECSMALL);
     232      325412 :   for (i = j = 1, k = r+1; i<l; i++)
     233      280586 :     if (d[i]) y[k++] = i; else y[j++] = i;
     234       44826 :   return y;
     235             : }
     236             : 
     237             : /* HNF reduce a relation matrix (column operations + row permutation)
     238             : ** Input:
     239             : **   mat = (li-1) x (co-1) matrix of long
     240             : **   C   = r x (co-1) matrix of GEN
     241             : **   perm= permutation vector (length li-1), indexing the rows of mat: easier
     242             : **     to maintain perm than to copy rows. For columns we can do it directly
     243             : **     using e.g. swap(mat[i], mat[j])
     244             : **   k0 = integer. The k0 first lines of mat are dense, the others are sparse.
     245             : ** Output: cf ASCII art in the function body
     246             : **
     247             : ** row permutations applied to perm
     248             : ** column operations applied to C. IN PLACE
     249             : **/
     250             : GEN
     251       27964 : hnfspec_i(GEN mat0, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, long k0)
     252             : {
     253             :   pari_sp av;
     254             :   long co, n, s, nlze, lnz, nr, i, j, k, lk0, col, lig, *p;
     255             :   GEN mat;
     256             :   GEN p1, p2, matb, matbnew, vmax, matt, T, extramat, B, C, H, dep, permpro;
     257       27964 :   const long li = lg(perm); /* = lgcols(mat0) */
     258       27964 :   const long CO = lg(mat0);
     259             : 
     260       27964 :   n = 0; /* -Wall */
     261             : 
     262       27964 :   C = *ptC; co = CO;
     263       27964 :   if (co > 300 && co > 1.5 * li)
     264             :   { /* treat the rest at the end */
     265           0 :     co = (long)(1.2 * li);
     266           0 :     setlg(C, co);
     267             :   }
     268             : 
     269       27964 :   if (DEBUGLEVEL>5)
     270             :   {
     271           0 :     err_printf("Entering hnfspec\n");
     272           0 :     p_mat(mat0,perm,0);
     273             :   }
     274       27964 :   matt = cgetg(co, t_MAT); /* dense part of mat (top) */
     275       27964 :   mat = cgetg(co, t_MAT);
     276      709801 :   for (j = 1; j < co; j++)
     277             :   {
     278      681837 :     GEN matj = col_dup(li, gel(mat0,j));
     279      681837 :     p1 = cgetg(k0+1,t_COL); gel(matt,j) = p1; gel(mat,j) = matj;
     280      681837 :     for (i=1; i<=k0; i++) gel(p1,i) = stoi(matj[perm[i]]);
     281             :   }
     282       27964 :   av = avma;
     283             : 
     284       27964 :   i = lig = li-1; col = co-1; lk0 = k0;
     285       27964 :   T = (k0 || (lg(C) > 1 && lgcols(C) > 1))? matid(col): NULL;
     286             :   /* Look for lines with a single non-0 entry, equal to 1 in absolute value */
     287     2902123 :   while (i > lk0 && col)
     288     2846195 :     switch( count(mat,perm[i],col,&n) )
     289             :     {
     290             :       case 0: /* move zero lines between k0+1 and lk0 */
     291       19180 :         lk0++; lswap(perm[i], perm[lk0]);
     292       19180 :         i = lig; continue;
     293             : 
     294             :       case 1: /* move trivial generator between lig+1 and li */
     295      139602 :         lswap(perm[i], perm[lig]);
     296      139602 :         if (T) swap(gel(T,n), gel(T,col));
     297      139602 :         swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     298      139602 :         if (p[perm[lig]] < 0) /* = -1 */
     299             :         { /* convert relation -g = 0 to g = 0 */
     300      113331 :           for (i=lk0+1; i<lig; i++) p[perm[i]] = -p[perm[i]];
     301      113331 :           if (T)
     302             :           {
     303      113331 :             p1 = gel(T,col);
     304     5854152 :             for (i=1; ; i++) /* T is a permuted identity: single non-0 entry */
     305    11594973 :               if (signe(gel(p1,i))) { togglesign_safe(&gel(p1,i)); break; }
     306             :           }
     307             :         }
     308      139602 :         lig--; col--; i = lig; continue;
     309             : 
     310     2687413 :       default: i--;
     311             :     }
     312       27964 :   if (DEBUGLEVEL>5) { err_printf("    after phase1:\n"); p_mat(mat,perm,0); }
     313             : 
     314             : #define absmax(s,z) {long _z; _z = labs(z); if (_z > s) s = _z;}
     315             :   /* Get rid of all lines containing only 0 and +/- 1, keeping track of column
     316             :    * operations in T. Leave the rows 1..lk0 alone [up to k0, coefficient
     317             :    * explosion, between k0+1 and lk0, row is 0] */
     318       27964 :   s = 0;
     319      283691 :   while (lig > lk0 && col && s < (long)(HIGHBIT>>1))
     320             :   {
     321     1051487 :     for (i=lig; i>lk0; i--)
     322     1026912 :       if (count(mat,perm[i],col,&n) > 0) break;
     323      252338 :     if (i == lk0) break;
     324             : 
     325             :     /* only 0, +/- 1 entries, at least 2 of them non-zero */
     326      227763 :     lswap(perm[i], perm[lig]);
     327      227763 :     swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     328      227763 :     if (T) swap(gel(T,n), gel(T,col));
     329      227763 :     if (p[perm[lig]] < 0)
     330             :     {
     331      157413 :       for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
     332      157413 :       if (T) ZV_togglesign(gel(T,col));
     333             :     }
     334    10741715 :     for (j=1; j<col; j++)
     335             :     {
     336    10513952 :       GEN matj = gel(mat,j);
     337             :       long t;
     338    10513952 :       if (! (t = matj[perm[lig]]) ) continue;
     339      624558 :       if (t == 1) {
     340      302429 :         for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= p[perm[i]]);
     341             :       }
     342             :       else { /* t = -1 */
     343      322129 :         for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] += p[perm[i]]);
     344             :       }
     345      624558 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
     346             :     }
     347      227763 :     lig--; col--;
     348      227763 :     if (gc_needed(av,3))
     349             :     {
     350           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[1]");
     351           0 :       if (T) T = gerepilecopy(av, T); else set_avma(av);
     352             :     }
     353             :   }
     354             :   /* As above with lines containing a +/- 1 (no other assumption).
     355             :    * Stop when single precision becomes dangerous */
     356       27964 :   vmax = cgetg(co,t_VECSMALL);
     357      342436 :   for (j=1; j<=col; j++)
     358             :   {
     359      314472 :     GEN matj = gel(mat,j);
     360      314472 :     for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[i]);
     361      314472 :     vmax[j] = s;
     362             :   }
     363      134258 :   while (lig > lk0 && col)
     364             :   {
     365      164573 :     for (i=lig; i>lk0; i--)
     366      157465 :       if ( (n = count2(mat,perm[i],col)) ) break;
     367       85443 :     if (i == lk0) break;
     368             : 
     369       78335 :     lswap(vmax[n], vmax[col]);
     370       78335 :     lswap(perm[i], perm[lig]);
     371       78335 :     swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     372       78335 :     if (T) swap(gel(T,n), gel(T,col));
     373       78335 :     if (p[perm[lig]] < 0)
     374             :     {
     375       33617 :       for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
     376       33617 :       if (T) ZV_togglesign(gel(T,col));
     377             :     }
     378     1442700 :     for (j=1; j<col; j++)
     379             :     {
     380     1364370 :       GEN matj = gel(mat,j);
     381             :       long t;
     382     1364370 :       if (! (t = matj[perm[lig]]) ) continue;
     383      730282 :       if (vmax[col] && (ulong)labs(t) >= (HIGHBIT-vmax[j]) / vmax[col])
     384           5 :         goto END2;
     385             : 
     386      730277 :       for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= t*p[perm[i]]);
     387      730277 :       vmax[j] = s;
     388      730277 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
     389             :     }
     390       78330 :     lig--; col--;
     391       78330 :     if (gc_needed(av,3))
     392             :     {
     393           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[2]");
     394           0 :       gerepileall(av, T? 2: 1, &vmax, &T);
     395             :     }
     396             :   }
     397             : 
     398             : END2: /* clean up mat: remove everything to the right of the 1s on diagonal */
     399             :   /* go multiprecision first */
     400       27964 :   matb = cgetg(co,t_MAT); /* bottom part (complement of matt) */
     401      709801 :   for (j=1; j<co; j++)
     402             :   {
     403      681837 :     GEN matj = gel(mat,j);
     404      681837 :     p1 = cgetg(li-k0,t_COL); gel(matb,j) = p1;
     405      681837 :     p1 -= k0;
     406      681837 :     for (i=k0+1; i<li; i++) gel(p1,i) = stoi(matj[perm[i]]);
     407             :   }
     408       27964 :   if (DEBUGLEVEL>5)
     409             :   {
     410           0 :     err_printf("    after phase2:\n");
     411           0 :     p_mat(mat,perm,lk0);
     412             :   }
     413      446009 :   for (i=li-2; i>lig; i--)
     414             :   {
     415      418045 :     long h, i0 = i - k0, k = i + co-li;
     416      418045 :     GEN Bk = gel(matb,k);
     417    16203565 :     for (j=k+1; j<co; j++)
     418             :     {
     419    15785520 :       GEN Bj = gel(matb,j), v = gel(Bj,i0);
     420    15785520 :       s = signe(v); if (!s) continue;
     421             : 
     422     3183313 :       gel(Bj,i0) = gen_0;
     423     3183313 :       if (is_pm1(v))
     424             :       {
     425     1596319 :         if (s > 0) /* v = 1 */
     426      796854 :         { for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), gel(Bk,h)); }
     427             :         else /* v = -1 */
     428      799465 :         { for (h=1; h<i0; h++) gel(Bj,h) = addii(gel(Bj,h), gel(Bk,h)); }
     429             :       }
     430             :       else {
     431     1586994 :         for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), mulii(v,gel(Bk,h)));
     432             :       }
     433     3183313 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,k), negi(v));
     434     3183313 :       if (gc_needed(av,3))
     435             :       {
     436          24 :         if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[3], (i,j) = %ld,%ld", i,j);
     437          24 :         for (h=1; h<co; h++) setlg(matb[h], i0+1); /* bottom can be forgotten */
     438          24 :         gerepileall(av, T? 2: 1, &matb, &T);
     439          24 :         Bk = gel(matb,k);
     440             :       }
     441             :     }
     442             :   }
     443       27964 :   for (j=1; j<co; j++) setlg(matb[j], lig-k0+1); /* bottom can be forgotten */
     444       27964 :   gerepileall(av, T? 2: 1, &matb, &T);
     445       27964 :   if (DEBUGLEVEL>5) err_printf("    matb cleaned up (using Id block)\n");
     446             : 
     447       27964 :   nlze = lk0 - k0;  /* # of 0 rows */
     448       27964 :   lnz = lig-nlze+1; /* 1 + # of non-0 rows (!= 0...0 1 0 ... 0) */
     449       27964 :   if (T) matt = ZM_mul(matt,T); /* update top rows */
     450       27964 :   extramat = cgetg(col+1,t_MAT); /* = new C minus the 0 rows */
     451      264106 :   for (j=1; j<=col; j++)
     452             :   {
     453      236142 :     GEN z = gel(matt,j);
     454      236142 :     GEN t = (gel(matb,j)) + nlze - k0;
     455      236142 :     p2=cgetg(lnz,t_COL); gel(extramat,j) = p2;
     456      236142 :     for (i=1; i<=k0; i++) gel(p2,i) = gel(z,i); /* top k0 rows */
     457      236142 :     for (   ; i<lnz; i++) gel(p2,i) = gel(t,i); /* other non-0 rows */
     458             :   }
     459       27964 :   if (!col) {
     460         483 :     permpro = identity_perm(lnz);
     461         483 :     nr = lnz;
     462             :   }
     463             :   else
     464       27481 :     permpro = ZM_rowrankprofile(extramat, &nr);
     465             :   /* lnz = lg(permpro) */
     466       27964 :   if (nlze)
     467             :   { /* put the nlze 0 rows (trivial generators) at the top */
     468        3915 :     p1 = new_chunk(lk0+1);
     469        3915 :     for (i=1; i<=nlze; i++) p1[i] = perm[i + k0];
     470        3915 :     for (   ; i<=lk0; i++)  p1[i] = perm[i - nlze];
     471        3915 :     for (i=1; i<=lk0; i++)  perm[i] = p1[i];
     472             :   }
     473             :   /* sort other rows according to permpro (nr redundant generators first) */
     474       27964 :   p1 = new_chunk(lnz); p2 = perm + nlze;
     475       27964 :   for (i=1; i<lnz; i++) p1[i] = p2[permpro[i]];
     476       27964 :   for (i=1; i<lnz; i++) p2[i] = p1[i];
     477             :   /* perm indexes the rows of mat
     478             :    *   |_0__|__redund__|__dense__|__too big__|_____done______|
     479             :    *   0  nlze                              lig             li
     480             :    *         \___nr___/ \___k0__/
     481             :    *         \____________lnz ______________/
     482             :    *
     483             :    *               col   co
     484             :    *       [dep     |     ]
     485             :    *    i0 [--------|  B  ] (i0 = nlze + nr)
     486             :    *       [matbnew |     ] matbnew has maximal rank = lnz-1 - nr
     487             :    * mat = [--------|-----] lig
     488             :    *       [   0    | Id  ]
     489             :    *       [        |     ] li */
     490             : 
     491       27964 :   matbnew = cgetg(col+1,t_MAT); /* dense+toobig, maximal rank. For hnffinal */
     492       27964 :   dep    = cgetg(col+1,t_MAT); /* rows dependent from the ones in matbnew */
     493      264106 :   for (j=1; j<=col; j++)
     494             :   {
     495      236142 :     GEN z = gel(extramat,j);
     496      236142 :     p1 = cgetg(nlze+nr+1,t_COL); gel(dep,j) = p1;
     497      236142 :     p2 = cgetg(lnz-nr,t_COL); gel(matbnew,j) = p2;
     498      236142 :     for (i=1; i<=nlze; i++) gel(p1,i) = gen_0;
     499      236142 :     p1 += nlze; for (i=1; i<=nr; i++) p1[i] = z[permpro[i]];
     500      236142 :     p2 -= nr;   for (   ; i<lnz; i++) p2[i] = z[permpro[i]];
     501             :   }
     502             : 
     503             :   /* redundant generators in terms of the genuine generators
     504             :    * (x_i) = - (g_i) B */
     505       27964 :   B = cgetg(co-col,t_MAT);
     506      473659 :   for (j=col+1; j<co; j++)
     507             :   {
     508      445695 :     GEN y = gel(matt,j);
     509      445695 :     GEN z = gel(matb,j);
     510      445695 :     p1=cgetg(lig+1,t_COL); gel(B,j-col) = p1;
     511      445695 :     for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
     512      445695 :     p1 += nlze; z += nlze-k0;
     513     4568786 :     for (k=1; k<lnz; k++)
     514             :     {
     515     4123091 :       i = permpro[k];
     516     4123091 :       gel(p1,k) = (i <= k0)? gel(y,i): gel(z,i);
     517             :     }
     518             :   }
     519       27964 :   if (T) C = typ(C)==t_MAT? RgM_mul(C,T): RgV_RgM_mul(C,T);
     520       27964 :   gerepileall(av, 4, &matbnew, &B, &dep, &C);
     521       27964 :   *ptdep = dep;
     522       27964 :   *ptB = B;
     523       27964 :   H = hnffinal(matbnew, perm, ptdep, ptB, &C);
     524       27964 :   if (CO > co)
     525             :   { /* treat the rest, N cols at a time (hnflll slow otherwise) */
     526           0 :     const long N = 300;
     527           0 :     long a, L = CO - co, l = minss(L, N); /* L columns to add */
     528           0 :     GEN CC = *ptC, m0 = mat0;
     529           0 :     setlg(CC, CO); /* restore */
     530           0 :     CC += co-1;
     531           0 :     m0 += co-1;
     532           0 :     for (a = l;;)
     533           0 :     {
     534             :       GEN MAT, emb;
     535           0 :       gerepileall(av, 4, &H,&C,ptB,ptdep);
     536           0 :       MAT = cgetg(l + 1, t_MAT);
     537           0 :       emb = cgetg(l + 1, typ(C));
     538           0 :       for (j = 1 ; j <= l; j++)
     539             :       {
     540           0 :         gel(MAT,j) = gel(m0,j);
     541           0 :         emb[j] = CC[j];
     542             :       }
     543           0 :       H = hnfadd_i(H, perm, ptdep, ptB, &C, MAT, emb);
     544           0 :       if (a == L) break;
     545           0 :       CC += l;
     546           0 :       m0 += l;
     547           0 :       a += l; if (a > L) { l = L - (a - l); a = L; }
     548             :     }
     549             :   }
     550       27964 :   *ptC = C; return H;
     551             : }
     552             : 
     553             : GEN
     554         322 : hnfspec(GEN mat, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, long k0)
     555             : {
     556         322 :   pari_sp av = avma;
     557         322 :   GEN H = hnfspec_i(mat, perm, ptdep, ptB, ptC, k0);
     558         322 :   gerepileall(av, 4, ptC, ptdep, ptB, &H); return H;
     559             : }
     560             : 
     561             : /* HNF reduce x, apply same transforms to C */
     562             : GEN
     563         322 : mathnfspec(GEN x, GEN *ptperm, GEN *ptdep, GEN *ptB, GEN *ptC)
     564             : {
     565         322 :   long i,j,k,ly,lx = lg(x);
     566             :   GEN z, perm;
     567         322 :   if (lx == 1) return cgetg(1, t_MAT);
     568         322 :   ly = lgcols(x);
     569         322 :   *ptperm = perm = identity_perm(ly-1);
     570         322 :   z = cgetg(lx,t_MAT);
     571        2604 :   for (i=1; i<lx; i++)
     572             :   {
     573        2282 :     GEN C = cgetg(ly,t_COL), D = gel(x,i);
     574        2282 :     gel(z,i) = C;
     575       88340 :     for (j=1; j<ly; j++)
     576             :     {
     577       86058 :       GEN d = gel(D,j);
     578       86058 :       if (is_bigint(d)) goto TOOLARGE;
     579       86058 :       C[j] = itos(d);
     580             :     }
     581             :   }
     582             :   /*  [ dep |     ]
     583             :    *  [-----|  B  ]
     584             :    *  [  H  |     ]
     585             :    *  [-----|-----]
     586             :    *  [  0  | Id  ] */
     587         322 :   return hnfspec(z,perm, ptdep, ptB, ptC, 0);
     588             : 
     589             : TOOLARGE:
     590           0 :   if (lg(*ptC) > 1 && lgcols(*ptC) > 1)
     591           0 :     pari_err_IMPL("mathnfspec with large entries");
     592           0 :   x = ZM_hnf(x); lx = lg(x); j = ly; k = 0;
     593           0 :   for (i=1; i<ly; i++)
     594             :   {
     595           0 :     if (equali1(gcoeff(x,i,i + lx-ly)))
     596           0 :       perm[--j] = i;
     597             :     else
     598           0 :       perm[++k] = i;
     599             :   }
     600           0 :   setlg(perm,k+1);
     601           0 :   x = rowpermute(x, perm); /* upper part */
     602           0 :   setlg(perm,ly);
     603           0 :   *ptB = vecslice(x, j+lx-ly, lx-1);
     604           0 :   setlg(x, j);
     605           0 :   *ptdep = rowslice(x, 1, lx-ly);
     606           0 :   return rowslice(x, lx-ly+1, k); /* H */
     607             : }
     608             : 
     609             : /* add new relations to a matrix treated by hnfspec (extramat / extraC) */
     610             : GEN
     611       20334 : hnfadd_i(GEN H, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, /* cf hnfspec */
     612             :        GEN extramat,GEN extraC)
     613             : {
     614       20334 :   GEN matb, extratop, Cnew, permpro, B = *ptB, C = *ptC, dep = *ptdep;
     615             :   long i, lH, lB, li, lig, co, col, nlze;
     616             : 
     617       20334 :   if (lg(extramat) == 1) return H;
     618       20334 :   co = lg(C)-1;
     619       20334 :   lH = lg(H)-1;
     620       20334 :   lB = lg(B)-1;
     621       20334 :   li = lg(perm)-1;
     622       20334 :   lig = li - lB;
     623       20334 :   col = co - lB;
     624       20334 :   nlze = lig - lH;
     625             : 
     626             :  /*               col    co
     627             :   *       [ 0 |dep |     ]
     628             :   *  nlze [--------|  B  ]
     629             :   *       [ 0 | H  |     ]
     630             :   *       [--------|-----] lig
     631             :   *       [   0    | Id  ]
     632             :   *       [        |     ] li */
     633       20334 :   extratop = zm_to_ZM( rowslicepermute(extramat, perm, 1, lig) );
     634       20334 :   if (li != lig)
     635             :   { /* zero out bottom part, using the Id block */
     636       20334 :     GEN A = vecslice(C, col+1, co);
     637       20334 :     GEN c = rowslicepermute(extramat, perm, lig+1, li);
     638       20334 :     extraC   = gsub(extraC, typ(A)==t_MAT? RgM_zm_mul(A, c): RgV_zm_mul(A,c));
     639       20334 :     extratop = ZM_sub(extratop, ZM_zm_mul(B, c));
     640             :   }
     641             : 
     642       20334 :   extramat = shallowconcat(extratop, vconcat(dep, H));
     643       20334 :   Cnew     = shallowconcat(extraC, vecslice(C, col-lH+1, co));
     644       20334 :   if (DEBUGLEVEL>5) err_printf("    1st phase done\n");
     645       20334 :   permpro = ZM_rowrankprofile(extramat, &nlze);
     646       20334 :   extramat = rowpermute(extramat, permpro);
     647       20334 :   *ptB     = rowpermute(B,        permpro);
     648       20334 :   permpro = vecsmallpermute(perm, permpro);
     649       20334 :   for (i=1; i<=lig; i++) perm[i] = permpro[i]; /* perm o= permpro */
     650             : 
     651       20334 :   *ptdep  = rowslice(extramat, 1, nlze);
     652       20334 :   matb    = rowslice(extramat, nlze+1, lig);
     653       20334 :   if (DEBUGLEVEL>5) err_printf("    2nd phase done\n");
     654       20334 :   H = hnffinal(matb,perm,ptdep,ptB,&Cnew);
     655       20334 :   *ptC = shallowconcat(vecslice(C, 1, col-lH), Cnew);
     656       20334 :   return H;
     657             : }
     658             : 
     659             : GEN
     660           0 : hnfadd(GEN H, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, /* cf hnfspec */
     661             :        GEN extramat,GEN extraC)
     662             : {
     663           0 :   pari_sp av = avma;
     664           0 :   H = hnfadd_i(H, perm, ptdep, ptB, ptC, ZM_to_zm(extramat), extraC);
     665           0 :   gerepileall(av, 4, ptC, ptdep, ptB, &H); return H;
     666             : }
     667             : 
     668             : /* zero aj = Aij (!= 0)  using  ak = Aik (maybe 0), via linear combination of
     669             :  * A[j] and A[k] of determinant 1. If U != NULL, likewise update its columns */
     670             : static void
     671    24211510 : ZC_elem(GEN aj, GEN ak, GEN A, GEN U, long j, long k)
     672             : {
     673             :   GEN p1,u,v,d;
     674             : 
     675    24211510 :   if (!signe(ak)) {
     676       18338 :     swap(gel(A,j), gel(A,k));
     677       18338 :     if (U) swap(gel(U,j), gel(U,k));
     678    21062144 :     return;
     679             :   }
     680    24193172 :   d = bezout(aj,ak,&u,&v);
     681             :   /* frequent special case (u,v) = (1,0) or (0,1) */
     682    24193284 :   if (!signe(u))
     683             :   { /* ak | aj */
     684    10707733 :     p1 = diviiexact(aj,ak); togglesign(p1);
     685    10707378 :     ZC_lincomb1_inplace(gel(A,j), gel(A,k), p1);
     686    10707433 :     if (U)
     687      525960 :       ZC_lincomb1_inplace(gel(U,j), gel(U,k), p1);
     688    10707533 :     return;
     689             :   }
     690    13485551 :   if (!signe(v))
     691             :   { /* aj | ak */
     692    10317986 :     p1 = diviiexact(ak,aj); togglesign(p1);
     693    10317947 :     ZC_lincomb1_inplace(gel(A,k), gel(A,j), p1);
     694    10317935 :     swap(gel(A,j), gel(A,k));
     695    10317935 :     if (U) {
     696       61105 :       ZC_lincomb1_inplace(gel(U,k), gel(U,j), p1);
     697       61105 :       swap(gel(U,j), gel(U,k));
     698             :     }
     699    10317935 :     return;
     700             :   }
     701             : 
     702     3167565 :   if (!is_pm1(d)) { aj = diviiexact(aj, d); ak = diviiexact(ak, d); }
     703     3167561 :   p1 = gel(A,k); aj = negi(aj); /* NOT togglesign */
     704     3167592 :   gel(A,k) = ZC_lincomb(u,v, gel(A,j),p1);
     705     3167552 :   gel(A,j) = ZC_lincomb(aj,ak, p1,gel(A,j));
     706     3167572 :   if (U)
     707             :   {
     708      185346 :     p1 = gel(U,k);
     709      185346 :     gel(U,k) = ZC_lincomb(u,v, gel(U,j),p1);
     710      185341 :     gel(U,j) = ZC_lincomb(aj,ak, p1,gel(U,j));
     711             :   }
     712             : }
     713             : 
     714             : INLINE int
     715        1001 : is_RgX(GEN a, long v) { return typ(a) == t_POL && varn(a)==v; }
     716             : /* set u,v such that au + bv = gcd(a,b), divide a,b by the gcd */
     717             : static GEN
     718         266 : gbezout_step(GEN *pa, GEN *pb, GEN *pu, GEN *pv, long vx)
     719             : {
     720         266 :   GEN a = *pa, b = *pb, d;
     721         266 :   if (gequal0(a))
     722             :   {
     723           0 :     *pa = gen_0; *pu = gen_0;
     724           0 :     *pb = gen_1; *pv = gen_1; return b;
     725             :   }
     726         266 :   a = is_RgX(a,vx)? RgX_renormalize(a): scalarpol(a, vx);
     727         266 :   b = is_RgX(b,vx)? RgX_renormalize(b): scalarpol(b, vx);
     728         266 :   d = RgX_extgcd(a,b, pu,pv);
     729         266 :   if (degpol(d)) { a = RgX_div(a, d); b = RgX_div(b, d); }
     730         154 :   else if (typ(gel(d,2)) == t_REAL && lg(gel(d,2)) <= 3)
     731             : #if 1
     732             :   { /* possible accuracy problem */
     733           0 :     GEN D = RgX_gcd_simple(a,b);
     734           0 :     if (degpol(D)) {
     735           0 :       D = RgX_normalize(D);
     736           0 :       a = RgX_div(a, D);
     737           0 :       b = RgX_div(b, D);
     738           0 :       d = RgX_extgcd(a,b, pu,pv); /* retry now */
     739           0 :       d = RgX_mul(d, D);
     740             :     }
     741             :   }
     742             : #else
     743             :   { /* less stable */
     744             :     d = RgX_extgcd_simple(a,b, pu,pv);
     745             :     if (degpol(d)) { a = RgX_div(a, d); b = RgX_div(b, d); }
     746             :   }
     747             : #endif
     748         266 :   *pa = a;
     749         266 :   *pb = b; return d;
     750             : }
     751             : static GEN
     752      309524 : col_mul(GEN x, GEN c)
     753             : {
     754      309524 :   if (typ(x) == t_INT)
     755             :   {
     756      309524 :     long s = signe(x);
     757      309524 :     if (!s) return NULL;
     758      232682 :     if (is_pm1(x)) return (s > 0)? c: RgC_neg(c);
     759             :   }
     760       41569 :   return RgC_Rg_mul(c, x);
     761             : }
     762             : static void
     763           0 : do_zero(GEN x)
     764             : {
     765           0 :   long i, lx = lg(x);
     766           0 :   for (i=1; i<lx; i++) gel(x,i) = gen_0;
     767           0 : }
     768             : 
     769             : /* (c1, c2) *= [u,-b; v,a] */
     770             : static void
     771       77381 : update(GEN u, GEN v, GEN a, GEN b, GEN *c1, GEN *c2)
     772             : {
     773             :   GEN p1,p2;
     774             : 
     775       77381 :   u = col_mul(u,*c1);
     776       77381 :   v = col_mul(v,*c2);
     777       77381 :   if (u) p1 = v? gadd(u,v): u;
     778         448 :   else   p1 = v? v: NULL;
     779             : 
     780       77381 :   a = col_mul(a,*c2);
     781       77381 :   b = col_mul(gneg_i(b),*c1);
     782       77381 :   if (a) p2 = b? RgC_add(a,b): a;
     783           0 :   else   p2 = b? b: NULL;
     784             : 
     785       77381 :   if (!p1) do_zero(*c1); else *c1 = p1;
     786       77381 :   if (!p2) do_zero(*c2); else *c2 = p2;
     787       77381 : }
     788             : 
     789             : /* zero aj = Aij (!= 0)  using  ak = Aik (maybe 0), via linear combination of
     790             :  * A[j] and A[k] of determinant 1. If U != NULL, likewise update its columns */
     791             : static void
     792           7 : RgC_elem(GEN aj, GEN ak, GEN A, GEN V, long j, long k, long li, long vx)
     793             : {
     794           7 :   GEN u,v, d = gbezout_step(&aj, &ak, &u, &v, vx);
     795             :   long l;
     796             :   /* (A[,k], A[,j]) *= [v, -aj; u, ak ] */
     797          14 :   for (l = 1; l < li; l++)
     798             :   {
     799           7 :     GEN t = gadd(gmul(u,gcoeff(A,l,j)), gmul(v,gcoeff(A,l,k)));
     800           7 :     gcoeff(A,l,j) = gsub(gmul(ak,gcoeff(A,l,j)), gmul(aj,gcoeff(A,l,k)));
     801           7 :     gcoeff(A,l,k) = t;
     802             :   }
     803           7 :   gcoeff(A,li,j) = gen_0;
     804           7 :   gcoeff(A,li,k) = d;
     805           7 :   if (V) update(v,u,ak,aj,(GEN*)(V+k),(GEN*)(V+j));
     806           7 : }
     807             : 
     808             : /* reduce A[i,j] mod A[i,j0] for j=j0+1... via column operations */
     809             : static void
     810     1467116 : ZM_reduce(GEN A, GEN U, long i, long j0)
     811             : {
     812     1467116 :   long j, lA = lg(A);
     813     1467116 :   GEN d = gcoeff(A,i,j0);
     814     1467116 :   if (signe(d) < 0)
     815             :   {
     816        5969 :     ZV_neg_inplace(gel(A,j0));
     817        5969 :     if (U) ZV_togglesign(gel(U,j0));
     818        5969 :     d = gcoeff(A,i,j0);
     819             :   }
     820     3710915 :   for (j=j0+1; j<lA; j++)
     821             :   {
     822     2243799 :     GEN q = truedivii(gcoeff(A,i,j), d);
     823     2243799 :     if (!signe(q)) continue;
     824             : 
     825      130445 :     togglesign(q);
     826      130445 :     ZC_lincomb1_inplace(gel(A,j), gel(A,j0), q);
     827      130445 :     if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,j0), q);
     828             :   }
     829     1467116 : }
     830             : 
     831             : /* normalize T as if it were a t_POL in variable v */
     832             : static GEN
     833         210 : normalize_as_RgX(GEN T, long v, GEN *pd)
     834             : {
     835             :   GEN d;
     836         210 :   if (!is_RgX(T,v)) { *pd = T; return gen_1; }
     837         175 :   d = leading_coeff(T);
     838         350 :   while (gequal0(d) || (typ(d) == t_REAL && lg(d) == 3
     839           0 :                         && gexpo(T) - expo(d) > (long)BITS_IN_LONG)) {
     840          14 :     T = normalizepol_lg(T, lg(T)-1);
     841          14 :     if (!signe(T)) { *pd = gen_1; return T; }
     842           0 :     d = leading_coeff(T);
     843             :   }
     844         161 :   if (degpol(T)) T = RgX_Rg_div(T,d); else { d = gel(T,2); T = gen_1; }
     845         161 :   *pd = d; return T;
     846             : }
     847             : /* reduce A[i,j] mod A[i,j0] for j=j0+1... via column operations */
     848             : static void
     849          42 : RgM_reduce(GEN A, GEN U, long i, long j0, long vx)
     850             : {
     851          42 :   long j, lA = lg(A);
     852          42 :   GEN d, T = normalize_as_RgX(gcoeff(A,i,j0), vx, &d);
     853          42 :   if (U && !gequal1(d)) gel(U,j0) = RgC_Rg_div(gel(U,j0), d);
     854          42 :   gcoeff(A,i,j0) = T;
     855             : 
     856          49 :   for (j=j0+1; j<lA; j++)
     857             :   {
     858           7 :     GEN t = gcoeff(A,i,j), q;
     859           7 :     if (gequal0(t)) continue;
     860           7 :     if (T == gen_1)
     861           0 :       q = t;
     862           7 :     else if (is_RgX(t,vx))
     863           7 :       q = RgX_div(t, T);
     864           0 :     else continue;
     865             : 
     866           7 :     if (gequal0(q)) continue;
     867           0 :     gel(A,j) = RgC_sub(gel(A,j), RgC_Rg_mul(gel(A,j0), q));
     868           0 :     if (U) gel(U,j) = RgC_sub(gel(U,j), RgC_Rg_mul(gel(U,j0), q));
     869             :   }
     870          42 : }
     871             : 
     872             : /* A,B square integral in upper HNF, of the same dimension > 0. Return Au
     873             :  * in Z^n (v in Z^n not computed), such that Au + Bv = [1, 0, ..., 0] */
     874             : GEN
     875      165591 : hnfmerge_get_1(GEN A, GEN B)
     876             : {
     877      165591 :   pari_sp av = avma;
     878      165591 :   long j, k, l = lg(A), lb;
     879      165591 :   GEN b, U = cgetg(l + 1, t_MAT), C = cgetg(l + 1, t_VEC);
     880             : 
     881      165566 :   b = gcoeff(B,1,1); lb = lgefint(b);
     882      317932 :   for (j = 1; j < l; j++)
     883             :   {
     884             :     GEN t;
     885      317933 :     long c = j+1;
     886      317933 :     gel(U,j) = col_ei(l-1, j);
     887      317992 :     gel(U,c) = zerocol(l-1); /* dummy */
     888      318002 :     gel(C,j) = vecslice(gel(A,j), 1,j);
     889      317997 :     gel(C,c) = vecslice(gel(B,j), 1,j);
     890      891406 :     for (k = j; k > 0; k--)
     891             :     {
     892      573519 :       t = gcoeff(C,k,c);
     893      573519 :       if (gequal0(t)) continue;
     894      508094 :       setlg(C[c], k+1);
     895      508075 :       ZC_elem(t, gcoeff(C,k,k), C, U, c, k);
     896      507783 :       if (lgefint(gcoeff(C,k,k)) > lb) gel(C,k) = FpC_red(gel(C,k), b);
     897      507783 :       if (j > 4)
     898             :       {
     899       48580 :         GEN u = gel(U,k);
     900             :         long h;
     901      654941 :         for (h=1; h<l; h++)
     902      606361 :           if (lgefint(gel(u,h)) > lb) gel(u,h) = remii(gel(u,h), b);
     903             :       }
     904             :     }
     905      317887 :     if (j == 1)
     906      165571 :       t = gcoeff(C,1,1);
     907             :     else
     908             :     {
     909             :       GEN u;
     910      152316 :       t = bezout(gcoeff(C,1,1), b, &u, NULL); /* >= 0 */
     911      152376 :       if (signe(u) && !equali1(u)) gel(U,1) = ZC_Z_mul(gel(U,1), u);
     912      152375 :       gcoeff(C,1,1) = t;
     913             :     }
     914      317946 :     if (equali1(t)) break;
     915             :   }
     916      165582 :   if (j >= l) return NULL;
     917      165582 :   b = lcmii(gcoeff(A,1,1),b);
     918      165565 :   A = FpC_red(ZM_ZC_mul(A,gel(U,1)), b);
     919      165592 :   return gerepileupto(av, FpC_center(A, b, shifti(b,-1)));
     920             : }
     921             : 
     922             : /* remove the first r columns */
     923             : static void
     924       72586 : remove_0cols(long r, GEN *pA, GEN *pB, long remove)
     925             : {
     926       72586 :   GEN A = *pA, B = *pB;
     927       72586 :   long l = lg(A);
     928       72586 :   A += r; A[0] = evaltyp(t_MAT) | evallg(l-r);
     929       72586 :   if (B && remove == 2) { B += r; B[0] = A[0]; }
     930       72586 :   *pA = A; *pB = B;
     931       72586 : }
     932             : 
     933             : /* Inefficient compared to hnfall. 'remove' = throw away lin.dep columns */
     934             : static GEN
     935       18288 : hnf_i(GEN A, int remove)
     936             : {
     937       18288 :   pari_sp av0 = avma, av;
     938             :   long s, n, m, j, k, li, def, ldef;
     939             : 
     940       18288 :   RgM_dimensions(A, &m, &n);
     941       18288 :   if (!n) return cgetg(1,t_MAT);
     942       18239 :   av = avma;
     943       18239 :   A = RgM_shallowcopy(A);
     944       18239 :   def = n; ldef = (m>n)? m-n: 0;
     945       63540 :   for (li=m; li>ldef; li--)
     946             :   {
     947      109253 :     for (j=def-1; j; j--)
     948             :     {
     949       63952 :       GEN a = gcoeff(A,li,j);
     950       63952 :       if (!signe(a)) continue;
     951             : 
     952             :       /* zero a = Aij  using  b = Aik */
     953       33619 :       k = (j==1)? def: j-1;
     954       33619 :       ZC_elem(a,gcoeff(A,li,k), A,NULL, j,k);
     955       33619 :       if (gc_needed(av,1))
     956             :       {
     957           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnf[1]. li=%ld",li);
     958           0 :         A = gerepilecopy(av, A);
     959             :       }
     960             :     }
     961       45301 :     s = signe(gcoeff(A,li,def));
     962       45301 :     if (s)
     963             :     {
     964       44601 :       if (s < 0) ZV_neg_inplace(gel(A,def));
     965       44601 :       ZM_reduce(A, NULL, li,def);
     966       44601 :       def--;
     967             :     }
     968             :     else
     969         700 :       if (ldef) ldef--;
     970       45301 :     if (gc_needed(av,1))
     971             :     {
     972           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnf[2]. li=%ld",li);
     973           0 :       A = gerepilecopy(av, A);
     974             :     }
     975             :   }
     976             :   /* rank A = n - def */
     977       18239 :   if (remove) { GEN B = NULL; remove_0cols(def, &A, &B, remove); }
     978       18239 :   return gerepileupto(av0, ZM_copy(A));
     979             : }
     980             : 
     981             : GEN
     982       21347 : ZM_hnf(GEN x) { return lg(x) > 8? ZM_hnfall(x, NULL, 1): hnf_i(x, 1); }
     983             : 
     984             : /* u*z[1..k] mod p, in place */
     985             : static void
     986     2814889 : FpV_Fp_mul_part_ip(GEN z, GEN u, GEN p, long k)
     987             : {
     988             :   long i;
     989     2814889 :   if (is_pm1(u)) {
     990      252031 :     if (signe(u) > 0) {
     991      942415 :       for (i = 1; i <= k; i++)
     992      720859 :         if (signe(gel(z,i))) gel(z,i) = modii(gel(z,i), p);
     993             :     } else {
     994       76315 :       for (i = 1; i <= k; i++)
     995       45840 :         if (signe(gel(z,i))) gel(z,i) = modii(negi(gel(z,i)), p);
     996             :     }
     997             :   }
     998             :   else {
     999     8912155 :     for (i = 1; i <= k; i++)
    1000     6349291 :       if (signe(gel(z,i))) gel(z,i) = Fp_mul(u,gel(z,i), p);
    1001             :   }
    1002     2814895 : }
    1003             : static void
    1004    22547049 : FpV_red_part_ipvec(GEN z, GEN p, long k)
    1005             : {
    1006             :   long i;
    1007    22547049 :   for (i = 1; i <= k; i++) gel(z,i) = modii(gel(z,i), gel(p,i));
    1008    22547093 : }
    1009             : 
    1010             : /* return x * U, in echelon form (mod p^m), where (det(U),p) = 1.
    1011             :  * If early_abort is set, return NULL as soon as one pivot is 0 (mod p^m) */
    1012             : GEN
    1013       49834 : ZpM_echelon(GEN x, long early_abort, GEN p, GEN pm)
    1014             : {
    1015       49834 :   pari_sp av0 = avma, av;
    1016             :   long m, li, co, i, j, k, def, ldef;
    1017             : 
    1018       49834 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1019       49834 :   li = lgcols(x);
    1020       49834 :   av = avma;
    1021       49834 :   x = RgM_shallowcopy(x);
    1022       49834 :   m = Z_pval(pm, p);
    1023             : 
    1024       49834 :   ldef = (li > co)? li - co: 0;
    1025      447188 :   for (def = co-1,i = li-1; i > ldef; i--)
    1026             :   {
    1027      397874 :     long vmin = LONG_MAX, kmin = 0;
    1028      397874 :     GEN umin = gen_0, pvmin, q;
    1029     3602998 :     for (k = 1; k <= def; k++)
    1030             :     {
    1031     3283432 :       GEN u = gcoeff(x,i,k);
    1032             :       long v;
    1033     3283432 :       if (!signe(u)) continue;
    1034     1562634 :       v = Z_pvalrem(u, p, &u);
    1035     1562634 :       if (v >= m) gcoeff(x,i,k) = gen_0;
    1036     1009246 :       else if (v < vmin) {
    1037      381094 :         vmin = v; kmin = k; umin = u;
    1038      381094 :         if (!vmin) break;
    1039             :       }
    1040             :     }
    1041      397874 :     if (!kmin)
    1042             :     {
    1043      163044 :       if (early_abort) return NULL;
    1044      162524 :       gcoeff(x,i,def) = gen_0;
    1045      162524 :       ldef--;
    1046      162524 :       if (ldef < 0) ldef = 0;
    1047      162524 :       continue;
    1048             :     }
    1049      234830 :     if (kmin != def) swap(gel(x,def), gel(x,kmin));
    1050      234830 :     q = vmin? powiu(p, m-vmin): pm;
    1051             :     /* pivot has valuation vmin */
    1052      234830 :     umin = modii(umin, q);
    1053      234830 :     if (!equali1(umin))
    1054      181834 :       FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(umin,q), pm, i-1);
    1055      234830 :     gcoeff(x, i, def) = pvmin = powiu(p, vmin);
    1056     2671475 :     for (j = def-1; j; j--)
    1057             :     { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
    1058     2436645 :       GEN t, a = gcoeff(x,i,j) = modii(gcoeff(x,i,j), pm);
    1059     2436645 :       if (!signe(a)) continue;
    1060             : 
    1061     1189777 :       t = diviiexact(a, pvmin); togglesign(t);
    1062     1189777 :       ZC_lincomb1_inplace(gel(x,j), gel(x,def), t);
    1063     1189777 :       if (gc_needed(av,1))
    1064             :       {
    1065          14 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZpM_echelon. i=%ld",i);
    1066          14 :         x = gerepilecopy(av, x); pvmin = gcoeff(x,i,def);
    1067             :       }
    1068             :     }
    1069      234830 :     def--;
    1070             :   }
    1071       49314 :   if (co > li)
    1072             :   {
    1073           0 :     x += co - li;
    1074           0 :     x[0] = evaltyp(t_MAT) | evallg(li);
    1075             :   }
    1076       49314 :   return gerepilecopy(av0, x);
    1077             : }
    1078             : GEN
    1079      849539 : zlm_echelon(GEN x, long early_abort, ulong p, ulong pm)
    1080             : {
    1081      849539 :   pari_sp av0 = avma;
    1082             :   long li, co, i, j, k, def, ldef;
    1083             :   ulong m;
    1084             : 
    1085      849539 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1086      849539 :   li = lgcols(x);
    1087      849536 :   x = Flm_copy(x);
    1088      849565 :   m = u_lval(pm, p);
    1089             : 
    1090      849563 :   ldef = (li > co)? li - co: 0;
    1091     4241164 :   for (def = co-1,i = li-1; i > ldef; i--)
    1092             :   {
    1093     3458966 :     long vmin = LONG_MAX, kmin = 0;
    1094     3458966 :     ulong umin = 0, pvmin, q;
    1095    13133471 :     for (k = 1; k <= def; k++)
    1096             :     {
    1097    11645678 :       ulong u = ucoeff(x,i,k);
    1098             :       long v;
    1099    11645678 :       if (!u) continue;
    1100     5475563 :       v = u_lvalrem(u, p, &u);
    1101     5475587 :       if (v >= (long) m) ucoeff(x,i,k) = 0;
    1102     5475587 :       else if (v < vmin) {
    1103     3456222 :         vmin = v; kmin = k; umin = u;
    1104     3456222 :         if (!vmin) break;
    1105             :       }
    1106             :     }
    1107     3458990 :     if (!kmin)
    1108             :     {
    1109      254859 :       if (early_abort) return NULL;
    1110      187484 :       ucoeff(x,i,def) = 0;
    1111      187484 :       ldef--;
    1112      187484 :       if (ldef < 0) ldef = 0;
    1113      187484 :       continue;
    1114             :     }
    1115     3204131 :     if (kmin != def) swap(gel(x,def), gel(x,kmin));
    1116     3204131 :     q = vmin? upowuu(p, m-vmin): pm;
    1117             :     /* pivot has valuation vmin */
    1118     3204143 :     umin %= q;
    1119     3204143 :     if (umin != 1)
    1120     1639965 :       Flv_Fl_mul_part_inplace(gel(x,def), Fl_inv(umin,q), pm, i-1);
    1121     3204131 :     ucoeff(x, i, def) = pvmin = upowuu(p, vmin);
    1122    19118841 :     for (j = def-1; j; j--)
    1123             :     { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
    1124    15914724 :       ulong t, a = ucoeff(x,i,j);
    1125    15914724 :       if (!a) continue;
    1126             : 
    1127     8903764 :       t = Fl_neg(a / pvmin, q);
    1128     8903767 :       Flc_lincomb1_inplace(gel(x,j), gel(x,def), t, pm);
    1129             :     }
    1130     3204117 :     def--;
    1131             :   }
    1132      782198 :   if (co > li)
    1133             :   {
    1134           0 :     x += co - li;
    1135           0 :     x[0] = evaltyp(t_MAT) | evallg(li);
    1136             :   }
    1137      782198 :   return gerepilecopy(av0, x);
    1138             : }
    1139             : 
    1140             : static int
    1141       15722 : ZV_allequal(GEN v)
    1142             : {
    1143       15722 :   long i, l = lg(v);
    1144       15722 :   if (l > 1)
    1145             :   {
    1146       15722 :     GEN x = gel(v,1);
    1147       15722 :     for (i = 2; i < l; i++) if (!equalii(x,gel(v,i))) return 0;
    1148             :   }
    1149        8330 :   return 1;
    1150             : }
    1151             : /* compute optimal D for hnfmod: x upper triangular */
    1152             : static GEN
    1153     3486659 : optimal_D(GEN x, GEN D)
    1154             : {
    1155     3486659 :   long i, n = nbrows(x);
    1156     3486658 :   GEN C = shallowcopy(D);
    1157     3486678 :   gel(C,1) = gcoeff(x,1,1);
    1158     3907630 :   for (i = 2; i < n; i++)
    1159             :   {
    1160     1272107 :     GEN c = mulii(gel(C,i-1), gcoeff(x,i,i));
    1161     1272094 :     if (signe(c) < 0) togglesign(c);
    1162     1272093 :     if (cmpii(c, gel(D,i)) >= 0) break;
    1163      420952 :     gel(C,i) = c;
    1164             :   }
    1165     3486675 :   return C;
    1166             : }
    1167             : 
    1168             : /* D = multiple of det x (usually detint(x)) or vector of positive moduli
    1169             :  * (compute hnf(x | D))
    1170             :  * flag & hnf_MODID: reduce mod D * matid [ otherwise as above ].
    1171             :  * flag & hnf_PART: don't reduce once diagonal is known
    1172             :  * flag & hnf_CENTER: centermod HNF (2|x[i,j]| <] x[i,i]) */
    1173             : GEN
    1174     3486759 : ZM_hnfmodall_i(GEN x, GEN D, long flag)
    1175             : {
    1176             :   pari_sp av;
    1177     3486759 :   const long center = (flag & hnf_CENTER);
    1178             :   long moddiag, modsame, nli, li, co, i, j, k, def, ldef;
    1179             :   GEN u, LDM;
    1180             : 
    1181     3486759 :   co = lg(x);
    1182     3486759 :   if (co == 1)
    1183             :   {
    1184          56 :     if (typ(D) == t_INT || lg(D) == 1) return cgetg(1,t_MAT);
    1185          14 :     x = diagonal_shallow(D); /* handle flags properly */
    1186          14 :     co = lg(x);
    1187             :   }
    1188     3486717 :   li = lgcols(x);
    1189     3486719 :   if (li == 1)
    1190             :   {
    1191          14 :     if (typ(D) != t_INT && lg(D) != li) pari_err_DIM("ZM_hnfmod");
    1192          14 :     return cgetg(1,t_MAT);
    1193             :   }
    1194     3486705 :   nli = li - 1;
    1195     3486705 :   modsame = typ(D)==t_INT;
    1196     3486705 :   if (!modsame)
    1197             :   {
    1198       15722 :     if (lg(D) != li) pari_err_DIM("ZM_hnfmod");
    1199       15722 :     if (ZV_allequal(D)) { modsame = 1; D = gel(D,1); }
    1200             :   }
    1201     3486705 :   moddiag = (flag & hnf_MODID) || !modsame;
    1202             :   /* modsame: triangularize mod fixed d*Id;
    1203             :    * moddiag: modulo diagonal matrix, else modulo multiple of determinant */
    1204             : 
    1205     3486705 :   if (modsame)
    1206             :   {
    1207     3479313 :     LDM = const_vecsmall(nli, 2*lgefint(D)-2);
    1208     3479325 :     D = const_vec(nli,D);
    1209             :   }
    1210             :   else
    1211             :   {
    1212        7392 :     LDM = cgetg(li, t_VECSMALL);
    1213        7392 :     for (i=1; i<li; i++) LDM[i] = lgefint(gel(D,i));
    1214             :   }
    1215     3486727 :   av = avma;
    1216     3486727 :   x = RgM_shallowcopy(x);
    1217             : 
    1218     3486725 :   ldef = 0;
    1219     3486725 :   if (li > co)
    1220             :   {
    1221       49063 :     ldef = li - co;
    1222       49063 :     if (!moddiag)
    1223           7 :       pari_err_DOMAIN("ZM_hnfmod","nb lines",">", strtoGENstr("nb columns"), x);
    1224             :   }
    1225    12881813 :   for (def = co-1,i = nli; i > ldef; i--,def--)
    1226             :   {
    1227     9395122 :     GEN d = gel(D,i);
    1228     9395122 :     long add_N = modsame;
    1229    42546937 :     for (j = 1; j < def; j++)
    1230             :     {
    1231    33151823 :       GEN p1, p2, b, a = gcoeff(x,i,j) = remii(gcoeff(x,i,j), d);
    1232    52381824 :       if (!signe(a)) continue;
    1233             : 
    1234    21962414 :       k = j+1;
    1235    21962414 :       b = gcoeff(x,i,k) = remii(gcoeff(x,i,k), d);
    1236    21962420 :       if (!signe(b)) { swap(gel(x,j), gel(x,k)); continue; }
    1237    13921898 :       if (add_N)
    1238             :       { /* ensure the moving pivot on row i divides d from now on */
    1239     4666663 :         add_N = 0;
    1240     4666663 :         if (!equali1(a))
    1241             :         { /* x[j] *= u; after this, a = x[i,j] | d */
    1242     3968571 :           GEN u = Fp_invgen(a, d, &a);
    1243             :           long t;
    1244     3968571 :           p1 = gel(x,j);
    1245     3968571 :           for (t = 1; t < i; t++) gel(p1,t) = mulii(gel(p1,t), u);
    1246     3968571 :           FpV_red_part_ipvec(p1, D, i-1);
    1247     3968571 :           gel(p1,i) = a;
    1248     3968571 :           if (2*lg(a) < lg(b))
    1249             :           { /* reduce x[i,k] mod x[i,j]: helps ZC_elem */
    1250        8708 :             GEN r, q = dvmdii(b, a, &r);
    1251        8708 :             togglesign(q);
    1252        8708 :             ZC_lincomb1_inplace_i(gel(x,k), gel(x,j), q, i-1);
    1253        8708 :             FpV_red_part_ipvec(gel(x,k), D, i-1);
    1254        8708 :             gcoeff(x,i,k) = b = r;
    1255             :           }
    1256             :         }
    1257             :       }
    1258    13921896 :       ZC_elem(a,b, x, NULL, j,k);
    1259    13921837 :       p1 = gel(x,j);
    1260    13921837 :       p2 = gel(x,k);
    1261             :       /* prevent coeffs explosion */
    1262    61691429 :       for (k = 1; k < i; k++)
    1263             :       {
    1264    47769580 :         if (lgefint(gel(p1,k)) > LDM[k]) gel(p1,k) = remii(gel(p1,k),gel(D,k));
    1265    47769592 :         if (lgefint(gel(p2,k)) > LDM[k]) gel(p2,k) = remii(gel(p2,k),gel(D,k));
    1266             :       }
    1267             :     }
    1268     9395114 :     if (gc_needed(av,2))
    1269             :     {
    1270           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[1]. i=%ld",i);
    1271           0 :       x = gerepilecopy(av, x);
    1272             :     }
    1273     9395114 :     if (moddiag && !signe(gcoeff(x,i,def)))
    1274             :     { /* missing pivot on line i, insert column */
    1275      597400 :       GEN a = cgetg(co + 1, t_MAT);
    1276      597400 :       for (k = 1; k <= def; k++) gel(a,k) = gel(x,k);
    1277      597400 :       gel(a,k++) = Rg_col_ei(gel(D,i), nli, i);
    1278      597400 :       for (     ; k <= co;  k++) gel(a,k) = gel(x,k-1);
    1279      597400 :       ldef--; if (ldef < 0) ldef = 0;
    1280      597400 :       co++; def++; x = a;
    1281             :     }
    1282             :   }
    1283     3486691 :   if (co < li)
    1284             :   { /* implies moddiag, add missing diag(D) components */
    1285       29631 :     GEN a = cgetg(li+1, t_MAT);
    1286       29631 :     for (k = 1; k <= li-co; k++) gel(a,k) = Rg_col_ei(gel(D,k), nli, k);
    1287       29631 :     for (i = 1; i < co; i++) gel(a,k-1+i) = gel(x,i);
    1288       29631 :     gel(a,li) = zerocol(nli); x = a;
    1289             :   }
    1290             :   else
    1291             :   {
    1292     3457060 :     x += co - li;
    1293     3457060 :     x[0] = evaltyp(t_MAT) | evallg(li); /* kill 0 columns */
    1294     3457056 :     if (moddiag) x = shallowconcat(x, zerocol(nli));
    1295             :   }
    1296     3486694 :   if (moddiag)
    1297             :   { /* x[li]: extra column, an accumulator discarded at the end */
    1298             :     GEN D2;
    1299     3324768 :     gcoeff(x,1,1) = gcdii(gcoeff(x,1,1), gel(D,1));
    1300     3324740 :     D2 = optimal_D(x,D);
    1301             :     /* add up missing diag(D) components */
    1302    12331502 :     for (i = nli; i > 0; i--)
    1303             :     {
    1304     9006730 :       gcoeff(x, i, li) = gel(D,i);
    1305    32117424 :       for (j = i; j > 0; j--)
    1306             :       {
    1307    23110686 :         GEN a = gcoeff(x, j, li);
    1308    23110686 :         if (!signe(a)) continue;
    1309     9284947 :         ZC_elem(a, gcoeff(x,j,j), x, NULL, li,j);
    1310     9284938 :         FpV_red_part_ipvec(gel(x,li), D, j-1);
    1311     9284941 :         FpV_red_part_ipvec(gel(x,j),  D, j-1);
    1312             :       }
    1313     9006738 :       if (gc_needed(av,1))
    1314             :       {
    1315           6 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[2]. i=%ld", i);
    1316           6 :         gerepileall(av, 2, &x, &D2);
    1317             :       }
    1318             :     }
    1319     3324772 :     D = D2;
    1320             :   }
    1321             :   else
    1322             :   {
    1323      161926 :     GEN b = gel(D,1);
    1324      614810 :     for (i = nli; i > 0; i--)
    1325             :     {
    1326      452884 :       GEN d = bezout(gcoeff(x,i,i),b, &u,NULL);
    1327      452884 :       gcoeff(x,i,i) = d;
    1328      452884 :       FpV_Fp_mul_part_ip(gel(x,i), u, b, i-1);
    1329      452884 :       if (i > 1) b = diviiexact(b,d);
    1330             :     }
    1331      161926 :     D = optimal_D(x,D);
    1332             :   }
    1333     3486698 :   x[0] = evaltyp(t_MAT) | evallg(li); /* kill 0 columns / discard accumulator */
    1334     3486747 :   if (flag & hnf_PART) return x;
    1335             : 
    1336    12946303 :   for (i = nli; i > 0; i--)
    1337             :   {
    1338     9459633 :     GEN diag = gcoeff(x,i,i);
    1339     9459633 :     if (signe(diag) < 0) { gel(x,i) = ZC_neg(gel(x,i)); diag = gcoeff(x,i,i); }
    1340     9459603 :     if (i != nli)
    1341     5972963 :       for (j = 1; j < i; j++) gcoeff(x,j,i) = remii(gcoeff(x,j,i), gel(D,j));
    1342    24400818 :     for (j = i+1; j < li; j++)
    1343             :     {
    1344    14941266 :       GEN b = gcoeff(x,i,j) = remii(gcoeff(x,i,j), gel(D,i));
    1345    14941258 :       GEN r, q = truedvmdii(b, diag, &r);
    1346             :       /* ensure -diag/2 <= r < diag/2 */
    1347    14941221 :       if (center && signe(r) && abscmpii(shifti(r,1),diag) >= 0)
    1348      215481 :       { r = subii(r,diag); q = addiu(q,1); }
    1349    14941216 :       if (!signe(q)) continue;
    1350     5618921 :       togglesign(q);
    1351     5618921 :       ZC_lincomb1_inplace_i(gel(x,j), gel(x,i), q, i-1);
    1352     5618921 :       gcoeff(x,i,j) = r;
    1353             :     }
    1354     9459552 :     if (gc_needed(av,1))
    1355             :     {
    1356           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[3]. i=%ld", i);
    1357           0 :       gerepileall(av, 2, &x, &D);
    1358             :     }
    1359             :   }
    1360     3486670 :   return x;
    1361             : }
    1362             : GEN
    1363     3481545 : ZM_hnfmodall(GEN x, GEN dm, long flag)
    1364             : {
    1365     3481545 :   pari_sp av = avma;
    1366     3481545 :   return gerepilecopy(av, ZM_hnfmodall_i(x, dm, flag));
    1367             : }
    1368             : GEN
    1369      161919 : ZM_hnfmod(GEN x, GEN d) { return ZM_hnfmodall(x,d,0); }
    1370             : GEN
    1371     3036939 : ZM_hnfmodid(GEN x, GEN d)
    1372     3036939 : { return ZM_hnfmodall(x,d,hnf_MODID); }
    1373             : /* return the column echelon form of x with 1's as pivots,
    1374             :  * P contains the row indices containing the pivots in increasing order */
    1375             : static GEN
    1376     1639899 : FpM_echelon(GEN x, GEN *pP, GEN p)
    1377             : {
    1378             :   pari_sp av;
    1379             :   long iP, li, co, i, j, k, def, ldef;
    1380             :   GEN P;
    1381             : 
    1382     1639899 :   co = lg(x); if (co == 1) { *pP = cgetg(1,t_VECSMALL); return cgetg(1,t_MAT); }
    1383     1639899 :   li = lgcols(x);
    1384     1639887 :   iP = 1;
    1385     1639887 :   *pP = P = cgetg(li, t_VECSMALL);
    1386     1639902 :   av = avma;
    1387     1639902 :   x = FpM_red(x, p);
    1388             : 
    1389     1639795 :   ldef = (li > co)? li - co: 0;
    1390     5830264 :   for (def = co-1,i = li-1; i > ldef; i--)
    1391             :   {
    1392     4190335 :     GEN u = NULL;
    1393     6500329 :     for (k = def; k; k--)
    1394             :     {
    1395     4755703 :       u = gcoeff(x,i,k);
    1396     4755703 :       if (signe(u)) break;
    1397             :     }
    1398     4190335 :     if (!k)
    1399             :     {
    1400     1744572 :       if (--ldef < 0) ldef = 0;
    1401     1744572 :       continue;
    1402             :     }
    1403     2445763 :     P[iP++] = i;
    1404     2445763 :     if (k != def) swap(gel(x,def), gel(x,k));
    1405     2445763 :     if (!equali1(u))
    1406     2180282 :       FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(u,p), p, i-1);
    1407     2445726 :     gcoeff(x, i, def) = gen_1;
    1408     7560474 :     for (j = def-1; j; j--)
    1409             :     { /* zero x[i, 1..def-1] using x[i,def] = 1*/
    1410     5114583 :       GEN xj = gel(x,j), u = gel(xj,i);
    1411     5114583 :       if (!signe(u)) continue;
    1412             : 
    1413     3472381 :       ZC_lincomb1_inplace(xj, gel(x,def), negi(u));
    1414     3472487 :       for (k = 1; k < i; k++) gel(xj,k) = modii(gel(xj,k), p);
    1415             :     }
    1416     2445891 :     if (gc_needed(av,2))
    1417             :     {
    1418           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpM_echelon. i=%ld",i);
    1419           0 :       x = gerepilecopy(av, x);
    1420             :     }
    1421     2445897 :     def--;
    1422             :   }
    1423             :   /* rank = iP - 1 */
    1424     1639929 :   setlg(P, iP); vecsmall_sort(P);
    1425     1639892 :   if (co > iP) x += co - iP;
    1426     1639892 :   x[0] = evaltyp(t_MAT) | evallg(iP);
    1427     1639891 :   return x;
    1428             : }
    1429             : /* given x square of maximal rank with 1 or p on diagonal from hnfmodid
    1430             :  * (=> a column containing p has its other entries at 0 ), return the HNF */
    1431             : static GEN
    1432     1639950 : FpM_hnfend(pari_sp av, GEN x, GEN p)
    1433             : {
    1434     1639950 :   long i, l = lgcols(x);
    1435     5830282 :   for (i = l-1; i > 0; i--)
    1436             :   {
    1437     4190406 :     GEN diag = gcoeff(x,i,i);
    1438             :     long j;
    1439     4190406 :     if (is_pm1(diag))
    1440     4743771 :       for (j = i+1; j < l; j++)
    1441             :       {
    1442     2297849 :         GEN xj = gel(x,j), b = gel(xj,i);
    1443             :         long k;
    1444     2297849 :         if (!signe(b)) continue;
    1445     1768735 :         ZC_lincomb1_inplace(xj, gel(x,i), negi(b));
    1446     7987232 :         for (k=1; k<i; k++)
    1447     6218539 :           if (lgefint(gel(xj,k)) > 3) gel(xj,k) = remii(gel(xj,k), p);
    1448             :       }
    1449             :     else
    1450     1744622 :       for (j = i+1; j < l; j++) gcoeff(x,i,j) = modii(gcoeff(x,i,j), p);
    1451     4190473 :     if (gc_needed(av,2))
    1452             :     {
    1453           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpM_hnfend. i=%ld",i);
    1454           0 :       x = gerepilecopy(av, x);
    1455             :     }
    1456             :   }
    1457     1639876 :   return x;
    1458             : }
    1459             : GEN
    1460     1639910 : ZM_hnfmodprime(GEN x, GEN p)
    1461             : {
    1462     1639910 :   pari_sp av = avma;
    1463             :   GEN P, y;
    1464             :   long l, lP, i;
    1465     1639910 :   if (lg(x) == 1) return cgetg(1, t_MAT);
    1466     1639910 :   l = lgcols(x);
    1467     1639906 :   x = FpM_echelon(x, &P, p);
    1468     1639890 :   lP = lg(P); /* rank = lP-1 */
    1469     1639890 :   if (lP == l) { set_avma(av); return matid(l-1); }
    1470     1639890 :   y = scalarmat_shallow(p, l-1);
    1471     1639944 :   for (i = 1; i < lP; i++) gel(y,P[i]) = gel(x,i);
    1472     1639944 :   return gerepilecopy(av, FpM_hnfend(av,y,p));
    1473             : }
    1474             : 
    1475             : static GEN
    1476      281836 : allhnfmod(GEN x, GEN dm, int flag)
    1477             : {
    1478      281836 :   if (typ(x)!=t_MAT) pari_err_TYPE("allhnfmod",x);
    1479      281836 :   RgM_check_ZM(x, "allhnfmod");
    1480      281836 :   if (isintzero(dm)) return ZM_hnf(x);
    1481      281836 :   return ZM_hnfmodall(x, dm, flag);
    1482             : }
    1483             : GEN
    1484          14 : hnfmod(GEN x, GEN d)
    1485             : {
    1486          14 :   if (typ(d) != t_INT) pari_err_TYPE("mathnfmod",d);
    1487          14 :   return allhnfmod(x, d, 0);
    1488             : }
    1489             : GEN
    1490      281822 : hnfmodid(GEN x, GEN d)
    1491             : {
    1492      281822 :   switch(typ(d))
    1493             :   {
    1494      281801 :     case t_INT: break;
    1495             :     case t_VEC: case t_COL:
    1496          21 :       if (RgV_is_ZV(d)) break;
    1497           0 :     default: pari_err_TYPE("mathnfmodid",d);
    1498             :   }
    1499      281822 :   return allhnfmod(x, d, hnf_MODID);
    1500             : }
    1501             : 
    1502             : /* M a ZM in HNF. Normalize with *centered* residues */
    1503             : GEN
    1504        1750 : ZM_hnfcenter(GEN M)
    1505             : {
    1506        1750 :   long i, j, k, N = lg(M)-1;
    1507        1750 :   pari_sp av = avma;
    1508             : 
    1509        6461 :   for (j=N-1; j>0; j--) /* skip last line */
    1510             :   {
    1511        4711 :     GEN Mj = gel(M,j), a = gel(Mj,j);
    1512       25319 :     for (k = j+1; k <= N; k++)
    1513             :     {
    1514       20608 :       GEN Mk = gel(M,k), q = diviiround(gel(Mk,j), a);
    1515       20608 :       long s = signe(q);
    1516       20608 :       if (!s) continue;
    1517       13300 :       if (is_pm1(q))
    1518             :       {
    1519        2760 :         if (s < 0)
    1520         379 :           for (i = 1; i <= j; i++) gel(Mk,i) = addii(gel(Mk,i), gel(Mj,i));
    1521             :         else
    1522        2381 :           for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), gel(Mj,i));
    1523             :       }
    1524             :       else
    1525       10540 :         for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), mulii(q,gel(Mj,i)));
    1526       13300 :       if (gc_needed(av,1))
    1527             :       {
    1528           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfcenter, j = %ld",j);
    1529           0 :         M = gerepilecopy(av, M);
    1530             :       }
    1531             :     }
    1532             :   }
    1533        1750 :   return M;
    1534             : }
    1535             : 
    1536             : /***********************************************************************/
    1537             : /*                                                                     */
    1538             : /*                 HNFLLL (Havas, Majewski, Mathews)                   */
    1539             : /*                                                                     */
    1540             : /***********************************************************************/
    1541             : 
    1542             : static void
    1543      557098 : Minus(long j, GEN lambda)
    1544             : {
    1545      557098 :   long k, n = lg(lambda);
    1546             : 
    1547      557098 :   for (k=1  ; k<j; k++) togglesign_safe(&gcoeff(lambda,k,j));
    1548      557098 :   for (k=j+1; k<n; k++) togglesign_safe(&gcoeff(lambda,j,k));
    1549      557098 : }
    1550             : 
    1551             : /* index of first non-zero entry */
    1552             : static long
    1553    41536729 : findi(GEN M)
    1554             : {
    1555    41536729 :   long i, n = lg(M);
    1556   421702885 :   for (i=1; i<n; i++)
    1557   400878590 :     if (signe(gel(M,i))) return i;
    1558    20824295 :   return 0;
    1559             : }
    1560             : 
    1561             : static long
    1562    41536729 : findi_normalize(GEN Aj, GEN B, long j, GEN lambda)
    1563             : {
    1564    41536729 :   long r = findi(Aj);
    1565    41536729 :   if (r && signe(gel(Aj,r)) < 0)
    1566             :   {
    1567      557098 :     ZV_togglesign(Aj); if (B) ZV_togglesign(gel(B,j));
    1568      557098 :     Minus(j,lambda);
    1569             :   }
    1570    41536729 :   return r;
    1571             : }
    1572             : 
    1573             : static void
    1574    20767679 : reduce2(GEN A, GEN B, long k, long j, long *row0, long *row1, GEN lambda, GEN D)
    1575             : {
    1576             :   GEN q;
    1577             :   long i;
    1578             : 
    1579    20767679 :   *row0 = findi_normalize(gel(A,j), B,j,lambda);
    1580    20767679 :   *row1 = findi_normalize(gel(A,k), B,k,lambda);
    1581    20767679 :   if (*row0)
    1582     7829699 :     q = truedivii(gcoeff(A,*row0,k), gcoeff(A,*row0,j));
    1583    12937980 :   else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
    1584     3046106 :     q = diviiround(gcoeff(lambda,j,k), gel(D,j));
    1585             :   else
    1586     9891874 :     return;
    1587             : 
    1588    10875805 :   if (signe(q))
    1589             :   {
    1590     5912901 :     GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
    1591     5912901 :     togglesign_safe(&q);
    1592     5912901 :     if (*row0) ZC_lincomb1_inplace(gel(A,k),gel(A,j),q);
    1593     5912901 :     if (B) ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
    1594     5912901 :     gel(Lk,j) = addmulii(gel(Lk,j), q, gel(D,j));
    1595     5912901 :     if (is_pm1(q))
    1596             :     {
    1597     1847337 :       if (signe(q) > 0)
    1598             :       {
    1599     6723799 :         for (i=1; i<j; i++)
    1600     6043732 :           if (signe(gel(Lj,i))) gel(Lk,i) = addii(gel(Lk,i), gel(Lj,i));
    1601             :       }
    1602             :       else
    1603             :       {
    1604     8069201 :         for (i=1; i<j; i++)
    1605     6901931 :           if (signe(gel(Lj,i))) gel(Lk,i) = subii(gel(Lk,i), gel(Lj,i));
    1606             :       }
    1607             :     }
    1608             :     else
    1609             :     {
    1610    24020385 :       for (i=1; i<j; i++)
    1611    19954821 :         if (signe(gel(Lj,i))) gel(Lk,i) = addmulii(gel(Lk,i), q, gel(Lj,i));
    1612             :     }
    1613             :   }
    1614             : }
    1615             : 
    1616             : static void
    1617     2976232 : hnfswap(GEN A, GEN B, long k, GEN lambda, GEN D)
    1618             : {
    1619     2976232 :   GEN t, p1, p2, Lk = gel(lambda,k);
    1620     2976232 :   long i,j,n = lg(A);
    1621             : 
    1622     2976232 :   swap(gel(A,k), gel(A,k-1));
    1623     2976232 :   if (B) swap(gel(B,k), gel(B,k-1));
    1624     2976232 :   for (j=k-2; j; j--) swap(gcoeff(lambda,j,k-1), gel(Lk,j));
    1625    39249841 :   for (i=k+1; i<n; i++)
    1626             :   {
    1627    36273609 :     GEN Li = gel(lambda,i);
    1628    36273609 :     p1 = mulii(gel(Li,k-1), gel(D,k));
    1629    36273609 :     p2 = mulii(gel(Li,k), gel(Lk,k-1));
    1630    36273609 :     t = subii(p1,p2);
    1631             : 
    1632    36273609 :     p1 = mulii(gel(Li,k), gel(D,k-2));
    1633    36273609 :     p2 = mulii(gel(Li,k-1), gel(Lk,k-1));
    1634    36273609 :     gel(Li,k-1) = diviiexact(addii(p1,p2), gel(D,k-1));
    1635    36273609 :     gel(Li,k) = diviiexact(t, gel(D,k-1));
    1636             :   }
    1637     2976232 :   p1 = mulii(gel(D,k-2), gel(D,k));
    1638     2976232 :   p2 = sqri(gel(Lk,k-1));
    1639     2976232 :   gel(D,k-1) = diviiexact(addii(p1,p2), gel(D,k-1));
    1640     2976232 : }
    1641             : 
    1642             : /* reverse row order in matrix A, IN PLACE */
    1643             : static GEN
    1644      120240 : reverse_rows(GEN A)
    1645             : {
    1646      120240 :   long i, j, h, n = lg(A);
    1647      120240 :   if (n == 1) return A;
    1648      120142 :   h = lgcols(A);
    1649      980952 :   for (j=1; j<n; j++)
    1650             :   {
    1651      860810 :     GEN c = gel(A,j);
    1652             :     /* start at (h-1) >>1 : if h = 2i even, no need to swap c[i] and itself */
    1653      860810 :     for (i=(h-1)>>1; i; i--) swap(gel(c,i), gel(c,h-i));
    1654             :   }
    1655      120142 :   return A;
    1656             : }
    1657             : /* decide whether to swap */
    1658             : static int
    1659     1472819 : must_swap(long k, GEN lambda, GEN D)
    1660             : {
    1661     1472819 :   pari_sp av = avma;
    1662     1472819 :   GEN z = addii(mulii(gel(D,k-2),gel(D,k)), sqri(gcoeff(lambda,k-1,k)));
    1663     1472819 :   return gc_bool(av, cmpii(z, sqri(gel(D,k-1))) < 0);
    1664             : }
    1665             : 
    1666             : GEN
    1667       60120 : ZM_hnflll(GEN A, GEN *ptB, int remove)
    1668             : {
    1669       60120 :   pari_sp av = avma;
    1670             :   long n, k, kmax;
    1671             :   GEN B, lambda, D;
    1672             : 
    1673       60120 :   n = lg(A);
    1674       60120 :   A = reverse_rows(ZM_copy(A)); /* ZM_copy for in place findi_normalize() */
    1675       60120 :   B = ptB? matid(n-1): NULL;
    1676       60120 :   D = const_vec(n, gen_1) + 1;
    1677       60120 :   lambda = zeromatcopy(n-1,n-1);
    1678       60120 :   k = kmax = 2;
    1679     5116958 :   while (k < n)
    1680             :   {
    1681             :     long row0, row1;
    1682             :     int do_swap;
    1683     4996718 :     reduce2(A,B,k,k-1,&row0,&row1,lambda,D);
    1684     4996718 :     if      (row0) do_swap = (!row1 || row0 <= row1);
    1685     1658845 :     else if (row1) do_swap = 0;
    1686     1400545 :     else do_swap = must_swap(k,lambda,D);
    1687     4996718 :     if (do_swap)
    1688             :     {
    1689     2875252 :       hnfswap(A,B,k,lambda,D);
    1690     2875252 :       if (k > 2) k--;
    1691             :     }
    1692             :     else
    1693             :     {
    1694             :       long i;
    1695    17892427 :       for (i=k-2; i; i--)
    1696             :       {
    1697             :         long row0, row1;
    1698    15770961 :         reduce2(A,B,k,i,&row0,&row1,lambda,D);
    1699    15770961 :         if (gc_needed(av,3))
    1700             :         {
    1701          14 :           GEN b = D-1;
    1702          14 :           if (DEBUGMEM>1) pari_warn(warnmem,"hnflll (reducing), kmax = %ld",kmax);
    1703          14 :           gerepileall(av, B? 4: 3, &A, &lambda, &b, &B);
    1704          14 :           D = b+1;
    1705             :         }
    1706             :       }
    1707     2121466 :       if (++k > kmax) kmax = k;
    1708             :     }
    1709     4996718 :     if (gc_needed(av,3))
    1710             :     {
    1711          94 :       GEN b = D-1;
    1712          94 :       if (DEBUGMEM>1) pari_warn(warnmem,"hnflll, kmax = %ld / %ld",kmax,n-1);
    1713          94 :       gerepileall(av, B? 4: 3, &A, &lambda, &b, &B);
    1714          94 :       D = b+1;
    1715             :     }
    1716             :   }
    1717             :   /* handle trivial case: return negative diag coefficient otherwise */
    1718       60120 :   if (n == 2) (void)findi_normalize(gel(A,1), B,1,lambda);
    1719       60120 :   A = reverse_rows(A);
    1720       60120 :   if (remove)
    1721             :   {
    1722             :     long i;
    1723       72665 :     for (i = 1; i < n; i++)
    1724       65196 :       if (!ZV_equal0(gel(A,i))) break;
    1725       12228 :     remove_0cols(i-1, &A, &B, remove);
    1726             :   }
    1727       60120 :   gerepileall(av, B? 2: 1, &A, &B);
    1728       60120 :   if (B) *ptB = B;
    1729       60120 :   return A;
    1730             : }
    1731             : 
    1732             : GEN
    1733           7 : hnflll(GEN x)
    1734             : {
    1735           7 :   GEN z = cgetg(3, t_VEC);
    1736           7 :   gel(z,1) = ZM_hnflll(x, &gel(z,2), 1);
    1737           7 :   return z;
    1738             : }
    1739             : 
    1740             : /* Variation on HNFLLL: Extended GCD */
    1741             : 
    1742             : static void
    1743      473130 : reduce1(GEN A, GEN B, long k, long j, GEN lambda, GEN D)
    1744             : {
    1745             :   GEN q;
    1746             :   long i;
    1747             : 
    1748      473130 :   if (signe(gel(A,j)))
    1749       93319 :     q = diviiround(gel(A,k),gel(A,j));
    1750      379811 :   else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
    1751       14149 :     q = diviiround(gcoeff(lambda,j,k), gel(D,j));
    1752             :   else
    1753      365662 :     return;
    1754             : 
    1755      107468 :   if (signe(q))
    1756             :   {
    1757       89781 :     GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
    1758       89781 :     togglesign_safe(&q);
    1759       89781 :     gel(A,k) = addmulii(gel(A,k), q, gel(A,j));
    1760       89781 :     ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
    1761       89781 :     gel(Lk,j) = addmulii(gel(Lk,j), q, gel(D,j));
    1762      122911 :     for (i=1; i<j; i++)
    1763       33130 :       if (signe(gel(Lj,i))) gel(Lk,i) = addmulii(gel(Lk,i), q, gel(Lj,i));
    1764             :   }
    1765             : }
    1766             : 
    1767             : static GEN
    1768       87591 : ZV_gcdext_i(GEN A)
    1769             : {
    1770       87591 :   long k, n = lg(A);
    1771             :   GEN B, lambda, D;
    1772             : 
    1773       87591 :   if (n == 1) retmkvec2(gen_1, cgetg(1,t_MAT));
    1774       87591 :   A = leafcopy(A);
    1775       87591 :   B = matid(n-1);
    1776       87591 :   lambda = zeromatcopy(n-1,n-1);
    1777       87591 :   D = const_vec(n, gen_1) + 1;
    1778       87591 :   k = 2;
    1779      459238 :   while (k < n)
    1780             :   {
    1781             :     int do_swap;
    1782             : 
    1783      284056 :     reduce1(A,B,k,k-1,lambda,D);
    1784      284056 :     if    (signe(gel(A,k-1))) do_swap = 1;
    1785      190737 :     else if (signe(gel(A,k))) do_swap = 0;
    1786       72274 :     else do_swap = must_swap(k,lambda,D);
    1787      284056 :     if (do_swap)
    1788             :     {
    1789      100980 :       hnfswap(A,B,k,lambda,D);
    1790      100980 :       if (k > 2) k--;
    1791             :     }
    1792             :     else
    1793             :     {
    1794             :       long i;
    1795      183076 :       for (i=k-2; i; i--) reduce1(A,B,k,i,lambda,D);
    1796      183076 :       k++;
    1797             :     }
    1798             :   }
    1799       87591 :   if (signe(gel(A,n-1)) < 0)
    1800             :   {
    1801        7648 :     gel(A,n-1) = negi(gel(A,n-1));
    1802        7648 :     ZV_togglesign(gel(B,n-1));
    1803             :   }
    1804       87591 :   return mkvec2(gel(A,n-1), B);
    1805             : }
    1806             : GEN
    1807       87577 : ZV_extgcd(GEN A)
    1808             : {
    1809       87577 :   pari_sp av = avma;
    1810       87577 :   return gerepilecopy(av, ZV_gcdext_i(A));
    1811             : }
    1812             : /* as ZV_extgcd, transforming the gcd into a t_MAT, for mathnf0 */
    1813             : static GEN
    1814          21 : ZV_hnfgcdext(GEN A)
    1815             : {
    1816          21 :   pari_sp av = avma;
    1817             :   GEN z;
    1818          21 :   if (lg(A) == 1) retmkvec2(cgetg(1,t_MAT),cgetg(1,t_MAT));
    1819          14 :   z = ZV_gcdext_i(A);
    1820          14 :   gel(z,1) = mkmat(mkcol(gel(z,1)));
    1821          14 :   return gerepilecopy(av, z);
    1822             : }
    1823             : 
    1824             : /* HNF with permutation. */
    1825             : GEN
    1826         266 : ZM_hnfperm(GEN A, GEN *ptU, GEN *ptperm)
    1827             : {
    1828             :   GEN U, c, l, perm, d, p, q, b;
    1829         266 :   pari_sp av = avma, av1;
    1830             :   long r, t, i, j, j1, k, m, n;
    1831             : 
    1832         266 :   n = lg(A)-1;
    1833         266 :   if (!n)
    1834             :   {
    1835           7 :     if (ptU) *ptU = cgetg(1,t_MAT);
    1836           7 :     if (ptperm) *ptperm = cgetg(1,t_VEC);
    1837           7 :     return cgetg(1, t_MAT);
    1838             :   }
    1839         259 :   m = nbrows(A);
    1840         259 :   c = zero_zv(m);
    1841         259 :   l = zero_zv(n);
    1842         259 :   perm = cgetg(m+1, t_VECSMALL);
    1843         259 :   av1 = avma;
    1844         259 :   A = RgM_shallowcopy(A);
    1845         259 :   U = ptU? matid(n): NULL;
    1846             :   /* U base change matrix : A0*U = A all along */
    1847        1316 :   for (r=0, k=1; k <= n; k++)
    1848             :   {
    1849        3353 :     for (j=1; j<k; j++)
    1850             :     {
    1851        2296 :       if (!l[j]) continue;
    1852        2163 :       t=l[j]; b=gcoeff(A,t,k);
    1853        2163 :       if (!signe(b)) continue;
    1854             : 
    1855         553 :       ZC_elem(b,gcoeff(A,t,j), A,U,k,j);
    1856         553 :       d = gcoeff(A,t,j);
    1857         553 :       if (signe(d) < 0)
    1858             :       {
    1859           0 :         ZV_neg_inplace(gel(A,j));
    1860           0 :         if (U) ZV_togglesign(gel(U,j));
    1861           0 :         d = gcoeff(A,t,j);
    1862             :       }
    1863        1428 :       for (j1=1; j1<j; j1++)
    1864             :       {
    1865         875 :         if (!l[j1]) continue;
    1866         861 :         q = truedivii(gcoeff(A,t,j1),d);
    1867         861 :         if (!signe(q)) continue;
    1868             : 
    1869         329 :         togglesign(q);
    1870         329 :         ZC_lincomb1_inplace(gel(A,j1), gel(A,j), q);
    1871         329 :         if (U) ZC_lincomb1_inplace(gel(U,j1), gel(U,j), q);
    1872             :       }
    1873             :     }
    1874        1057 :     t = m; while (t && (c[t] || !signe(gcoeff(A,t,k)))) t--;
    1875        1057 :     if (t)
    1876             :     {
    1877         959 :       p = gcoeff(A,t,k);
    1878       17353 :       for (i=t-1; i; i--)
    1879             :       {
    1880       16394 :         q = gcoeff(A,i,k);
    1881       16394 :         if (signe(q) && abscmpii(p,q) > 0) { p = q; t = i; }
    1882             :       }
    1883         959 :       perm[++r] = l[k] = t; c[t] = k;
    1884         959 :       if (signe(p) < 0)
    1885             :       {
    1886         125 :         ZV_neg_inplace(gel(A,k));
    1887         125 :         if (U) ZV_togglesign(gel(U,k));
    1888         125 :         p = gcoeff(A,t,k);
    1889             :       }
    1890             :       /* p > 0 */
    1891        2912 :       for (j=1; j<k; j++)
    1892             :       {
    1893        1953 :         if (!l[j]) continue;
    1894        1932 :         q = truedivii(gcoeff(A,t,j),p);
    1895        1932 :         if (!signe(q)) continue;
    1896             : 
    1897         231 :         togglesign(q);
    1898         231 :         ZC_lincomb1_inplace(gel(A,j), gel(A,k), q);
    1899         231 :         if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,k), q);
    1900             :       }
    1901             :     }
    1902        1057 :     if (gc_needed(av1,1))
    1903             :     {
    1904           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"hnfperm, k=%ld",k);
    1905           0 :       gerepileall(av1, U? 2: 1, &A, &U);
    1906             :     }
    1907             :   }
    1908         259 :   if (r < m)
    1909             :   {
    1910        4081 :     for (i=1,k=r; i<=m; i++)
    1911        3878 :       if (!c[i]) perm[++k] = i;
    1912             :   }
    1913             : 
    1914             : /* We have A0*U=A, U in Gl(n,Z)
    1915             :  * basis for Im(A):  columns of A s.t l[j]>0 (r   cols)
    1916             :  * basis for Ker(A): columns of U s.t l[j]=0 (n-r cols) */
    1917         259 :   p = cgetg(r+1,t_MAT);
    1918         259 :   for (i=1; i<=m/2; i++) lswap(perm[i], perm[m+1-i]);
    1919         259 :   if (U)
    1920             :   {
    1921          70 :     GEN u = cgetg(n+1,t_MAT);
    1922         322 :     for (t=1,k=r,j=1; j<=n; j++)
    1923         252 :       if (l[j])
    1924             :       {
    1925         154 :         u[k + n-r] = U[j];
    1926         154 :         gel(p,k--) = vecpermute(gel(A,j), perm);
    1927             :       }
    1928             :       else
    1929          98 :         u[t++] = U[j];
    1930          70 :     *ptU = u;
    1931          70 :     if (ptperm) *ptperm = perm;
    1932          70 :     gerepileall(av, ptperm? 3: 2, &p, ptU, ptperm);
    1933             :   }
    1934             :   else
    1935             :   {
    1936         994 :     for (k=r,j=1; j<=n; j++)
    1937         805 :       if (l[j]) gel(p,k--) = vecpermute(gel(A,j), perm);
    1938         189 :     if (ptperm) *ptperm = perm;
    1939         189 :     gerepileall(av, ptperm? 2: 1, &p, ptperm);
    1940             :   }
    1941         259 :   return p;
    1942             : }
    1943             : 
    1944             : GEN
    1945         182 : ZM_hnf_knapsack(GEN x)
    1946             : {
    1947         182 :   GEN t, perm, H = ZM_hnfperm(x,NULL,&perm);
    1948         182 :   long i,j, l = lg(H), h = lgcols(H);
    1949        2485 :   for (i=1; i<h; i++)
    1950             :   {
    1951        2387 :     int fl = 0;
    1952       13811 :     for (j=1; j<l; j++)
    1953             :     {
    1954       11508 :       t = gcoeff(H,i,j);
    1955       11508 :       if (signe(t))
    1956             :       {
    1957        2422 :         if (!is_pm1(t) || fl) return NULL;
    1958        2338 :         fl = 1;
    1959             :       }
    1960             :     }
    1961             :   }
    1962          98 :   return rowpermute(H, perm_inv(perm));
    1963             : }
    1964             : 
    1965             : GEN
    1966          14 : hnfperm(GEN A)
    1967             : {
    1968          14 :   GEN y = cgetg(4, t_VEC);
    1969          14 :   gel(y,1) = ZM_hnfperm(A, &gel(y,2), &gel(y,3));
    1970          14 :   return y;
    1971             : }
    1972             : 
    1973             : /* Hermite Normal Form, with base change matrix if ptB != NULL.
    1974             :  * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
    1975             :  * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
    1976             : GEN
    1977      133505 : ZM_hnfall_i(GEN A, GEN *ptB, long remove)
    1978             : {
    1979             :   pari_sp av;
    1980             :   long m, n, r, i, j, k, li;
    1981             :   GEN B, c, h, a;
    1982             : 
    1983      133505 :   RgM_dimensions(A, &m,&n);
    1984      133505 :   if (!n)
    1985             :   {
    1986           7 :     if (ptB) *ptB = cgetg(1,t_MAT);
    1987           7 :     return cgetg(1,t_MAT);
    1988             :   }
    1989      133498 :   c = zero_zv(m);
    1990      133498 :   h = const_vecsmall(n, m);
    1991      133498 :   av = avma;
    1992      133498 :   A = RgM_shallowcopy(A);
    1993      133498 :   B = ptB? matid(n): NULL;
    1994      133498 :   r = n+1;
    1995      442395 :   for (li=m; li; li--)
    1996             :   {
    1997      914366 :     for (j=1; j<r; j++)
    1998             :     {
    1999     1178313 :       for (i=h[j]; i>li; i--)
    2000             :       {
    2001      264171 :         a = gcoeff(A,i,j);
    2002      264171 :         k = c[i];
    2003             :         /* zero a = Aij  using  Aik */
    2004      264171 :         if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B,j,k);
    2005      264171 :         ZM_reduce(A,B, i,k); /* ensure reduced entries even if a = 0 */
    2006      264171 :         if (gc_needed(av,1))
    2007             :         {
    2008           0 :           if (DEBUGMEM>1)
    2009           0 :             pari_warn(warnmem,"ZM_hnfall[1], li = %ld, j = %ld", li, j);
    2010           0 :           gerepileall(av, B? 2: 1, &A, &B);
    2011             :         }
    2012             :       }
    2013      914142 :       if (signe( gcoeff(A,li,j) )) break;
    2014      605469 :       h[j] = li-1;
    2015             :     }
    2016      308897 :     if (j == r) continue;
    2017      308673 :     r--;
    2018      308673 :     if (j < r) /* A[j] != 0 */
    2019             :     {
    2020      227750 :       swap(gel(A,j), gel(A,r));
    2021      227750 :       if (B) swap(gel(B,j), gel(B,r));
    2022      227750 :       h[j] = h[r]; h[r] = li; c[li] = r;
    2023             :     }
    2024      308673 :     if (signe(gcoeff(A,li,r)) < 0)
    2025             :     {
    2026       75005 :       ZV_neg_inplace(gel(A,r));
    2027       75005 :       if (B) ZV_togglesign(gel(B,r));
    2028             :     }
    2029      308673 :     ZM_reduce(A,B, li,r);
    2030      308673 :     if (gc_needed(av,1))
    2031             :     {
    2032           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[2], li = %ld", li);
    2033           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2034             :     }
    2035             :   }
    2036             : 
    2037      133498 :   if (DEBUGLEVEL>5) err_printf("\nhnfall, final phase: ");
    2038      133498 :   r--; /* first r cols are in the image the n-r (independent) last ones */
    2039      742001 :   for (j=1; j<=r; j++)
    2040     1458174 :     for (i=h[j]; i; i--)
    2041             :     {
    2042      849671 :       a = gcoeff(A,i,j);
    2043      849671 :       k = c[i];
    2044      849671 :       if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B, j,k);
    2045      849671 :       ZM_reduce(A,B, i,k); /* ensure reduced entries, even if a = 0 */
    2046      849671 :       if (gc_needed(av,1))
    2047             :       {
    2048           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[3], j = %ld", j);
    2049           0 :         gerepileall(av, B? 2: 1, &A, &B);
    2050             :       }
    2051             :     }
    2052      133498 :   if (DEBUGLEVEL>5) err_printf("\n");
    2053      133498 :   if (remove) remove_0cols(r, &A, &B, remove);
    2054      133498 :   if (ptB) *ptB = B;
    2055      133498 :   return A;
    2056             : }
    2057             : GEN
    2058        3640 : ZM_hnfall(GEN A, GEN *ptB, long remove)
    2059             : {
    2060        3640 :   pari_sp av = avma;
    2061        3640 :   A = ZM_hnfall_i(A, ptB, remove);
    2062        3640 :   gerepileall(av, ptB? 2: 1, &A, ptB);
    2063        3640 :   return A;
    2064             : }
    2065             : 
    2066             : GEN
    2067          14 : hnfall(GEN x)
    2068             : {
    2069          14 :   GEN z = cgetg(3, t_VEC);
    2070          14 :   gel(z,1) = ZM_hnfall(x, (GEN*)(z+2), 1);
    2071          14 :   return z;
    2072             : }
    2073             : GEN
    2074          14 : hnf(GEN x) { return mathnf0(x,0); }
    2075             : 
    2076             : /* C = A^(-1)t where A and C are integral, A is upper triangular, t t_INT */
    2077             : GEN
    2078       28511 : hnf_invscale(GEN A, GEN t)
    2079             : {
    2080       28511 :   long n = lg(A)-1, i,j,k;
    2081       28511 :   GEN m, c = cgetg(n+1,t_MAT);
    2082             : 
    2083       28511 :   if (!n) return c;
    2084      139237 :   for (k=1; k<=n; k++)
    2085             :   { /* cf hnf_divscale with B = id, thus b = e_k */
    2086      110726 :     GEN u = cgetg(n+1, t_COL);
    2087      110726 :     pari_sp av = avma;
    2088      110726 :     gel(c,k) = u;
    2089      110726 :     gel(u,n) = k == n? gerepileuptoint(av, diviiexact(t, gcoeff(A,n,n))): gen_0;
    2090      660716 :     for (i=n-1; i>0; i--)
    2091             :     {
    2092      549990 :       av = avma; m = i == k? t: gen_0;
    2093      549990 :       for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2094      549990 :       gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
    2095             :     }
    2096             :   }
    2097       28511 :   return c;
    2098             : }
    2099             : 
    2100             : /* C = A^(-1)(tB) where A, B, C are integral, A is upper triangular, t t_INT */
    2101             : GEN
    2102      176660 : hnf_divscale(GEN A, GEN B, GEN t)
    2103             : {
    2104      176660 :   long n = lg(A)-1, i,j,k;
    2105      176660 :   GEN m, c = cgetg(n+1,t_MAT);
    2106             : 
    2107      176660 :   if (!n) return c;
    2108      686357 :   for (k=1; k<=n; k++)
    2109             :   {
    2110      509697 :     GEN u = cgetg(n+1, t_COL), b = gel(B,k);
    2111      509697 :     pari_sp av = avma;
    2112      509697 :     gel(c,k) = u; m = mulii(gel(b,n),t);
    2113      509697 :     gel(u,n) = gerepileuptoint(av, diviiexact(m, gcoeff(A,n,n)));
    2114     2212295 :     for (i=n-1; i>0; i--)
    2115             :     {
    2116     1702598 :       av = avma; m = mulii(gel(b,i),t);
    2117     1702598 :       for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2118     1702598 :       gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
    2119             :     }
    2120             :   }
    2121      176660 :   return c;
    2122             : }
    2123             : 
    2124             : /* A, B integral upper HNF. A^(-1) B integral ? */
    2125             : int
    2126          91 : hnfdivide(GEN A, GEN B)
    2127             : {
    2128          91 :   pari_sp av = avma;
    2129          91 :   long n = lg(A)-1, i,j,k;
    2130             :   GEN u, b, m, r;
    2131             : 
    2132          91 :   if (!n) return 1;
    2133          91 :   if (lg(B)-1 != n) pari_err_DIM("hnfdivide");
    2134          91 :   u = cgetg(n+1, t_COL);
    2135         413 :   for (k=1; k<=n; k++)
    2136             :   {
    2137         322 :     b = gel(B,k);
    2138         322 :     m = gel(b,k);
    2139         322 :     gel(u,k) = dvmdii(m, gcoeff(A,k,k), &r);
    2140         322 :     if (r != gen_0) return gc_long(av, 0);
    2141         798 :     for (i=k-1; i>0; i--)
    2142             :     {
    2143         476 :       m = gel(b,i);
    2144         476 :       for (j=i+1; j<=k; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2145         476 :       m = dvmdii(m, gcoeff(A,i,i), &r);
    2146         476 :       if (r != gen_0) return gc_long(av, 0);
    2147         476 :       gel(u,i) = m;
    2148             :     }
    2149             :   }
    2150          91 :   return gc_long(av, 1);
    2151             : }
    2152             : 
    2153             : /* A upper HNF, b integral vector. Return A^(-1) b if integral,
    2154             :  * NULL otherwise. Assume #A[,1] = #b. */
    2155             : GEN
    2156      106736 : hnf_invimage(GEN A, GEN b)
    2157             : {
    2158      106736 :   pari_sp av = avma;
    2159      106736 :   long n = lg(A)-1, m, i, k;
    2160             :   GEN u, r;
    2161             : 
    2162      106736 :   if (!n) return lg(b)==1? cgetg(1,t_COL):NULL;
    2163      106694 :   m = nbrows(A); /* m >= n */
    2164      106694 :   u = cgetg(n+1, t_COL);
    2165      288694 :   for (i = n, k = m; k > 0; k--)
    2166             :   {
    2167      288694 :     pari_sp av2 = avma;
    2168             :     long j;
    2169      288694 :     GEN t = gel(b,k), Aki = gcoeff(A,k,i);
    2170      288694 :     if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
    2171      288694 :     for (j=i+1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
    2172      288694 :     if (!signe(Aki))
    2173             :     {
    2174           0 :       if (signe(t)) return gc_NULL(av);
    2175           0 :       set_avma(av2); gel(u,i) = gen_0; continue;
    2176             :     }
    2177      288694 :     t = dvmdii(t, Aki, &r);
    2178      288694 :     if (r != gen_0) return gc_NULL(av);
    2179      238896 :     gel(u,i) = gerepileuptoint(av2, t);
    2180      238896 :     if (--i == 0) break;
    2181             :   }
    2182             :   /* If there is a solution, it must be u. Check remaining equations */
    2183      113792 :   for (; k > 0; k--)
    2184             :   {
    2185       56896 :     pari_sp av2 = avma;
    2186             :     long j;
    2187       56896 :     GEN t = gel(b,k);
    2188       56896 :     if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
    2189       56896 :     for (j=1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
    2190       56896 :     if (signe(t)) return gc_NULL(av);
    2191       56896 :     set_avma(av2);
    2192             :   }
    2193       56896 :   return u;
    2194             : }
    2195             : 
    2196             : /* A upper HNF, B integral matrix or column. Return A^(-1) B if integral,
    2197             :  * NULL otherwise */
    2198             : GEN
    2199       98476 : hnf_solve(GEN A, GEN B)
    2200             : {
    2201             :   pari_sp av;
    2202             :   long i, l;
    2203             :   GEN C;
    2204             : 
    2205       98476 :   if (typ(B) == t_COL) return hnf_invimage(A, B);
    2206       72604 :   av = avma; C = cgetg_copy(B, &l);
    2207      113526 :   for (i = 1; i < l; i++)
    2208             :   {
    2209       75376 :     GEN c = hnf_invimage(A, gel(B,i));
    2210       75376 :     if (!c) return gc_NULL(av);
    2211       40922 :     gel(C,i) = c;
    2212             :   }
    2213       38150 :   return C;
    2214             : }
    2215             : 
    2216             : /***************************************************************/
    2217             : /**                                                           **/
    2218             : /**               SMITH NORMAL FORM REDUCTION                 **/
    2219             : /**                                                           **/
    2220             : /***************************************************************/
    2221             : 
    2222             : static GEN
    2223           0 : trivsmith(long all)
    2224             : {
    2225             :   GEN z;
    2226           0 :   if (!all) return cgetg(1,t_VEC);
    2227           0 :   z=cgetg(4,t_VEC);
    2228           0 :   gel(z,1) = cgetg(1,t_MAT);
    2229           0 :   gel(z,2) = cgetg(1,t_MAT);
    2230           0 :   gel(z,3) = cgetg(1,t_MAT); return z;
    2231             : }
    2232             : 
    2233             : static void
    2234      199026 : snf_pile(pari_sp av, GEN *x, GEN *U, GEN *V)
    2235             : {
    2236             :   GEN *gptr[3];
    2237      199026 :   int c = 1; gptr[0]=x;
    2238      199026 :   if (*U) gptr[c++] = U;
    2239      199026 :   if (*V) gptr[c++] = V;
    2240      199026 :   gerepilemany(av,gptr,c);
    2241      199026 : }
    2242             : 
    2243             : static GEN
    2244       95503 : bezout_step(GEN *pa, GEN *pb, GEN *pu, GEN *pv)
    2245             : {
    2246       95503 :   GEN a = *pa, b = *pb, d;
    2247       95503 :   if (absequalii(a,b))
    2248             :   {
    2249       38339 :     long sa = signe(a), sb = signe(b);
    2250       38339 :     *pv = gen_0;
    2251       38339 :     if (sb == sa) {
    2252       38136 :       *pa = *pb = gen_1;
    2253       38136 :       if (sa > 0) {*pu=gen_1; return a;} else {*pu=gen_m1; return absi(a);}
    2254             :     }
    2255         203 :     if (sa > 0) { *pa = *pu = gen_1; *pb = gen_m1; return a; }
    2256           7 :     *pa = *pu = gen_m1; *pb = gen_1; return b;
    2257             :   }
    2258       57164 :   d = bezout(a,b, pu,pv);
    2259       57164 :   *pa = diviiexact(a, d);
    2260       57164 :   *pb = diviiexact(b, d); return d;
    2261             : }
    2262             : 
    2263             : static int
    2264      474834 : negcmpii(void *E, GEN x, GEN y) { (void)E; return -cmpii(x,y); }
    2265             : 
    2266             : /* does b = x[i,i] divide all entries in x[1..i-1,1..i-1] ? If so, return 0;
    2267             :  * else return the index of a problematic row */
    2268             : static long
    2269      188274 : ZM_snf_no_divide(GEN x, long i)
    2270             : {
    2271      188274 :   GEN b = gcoeff(x,i,i);
    2272             :   long j, k;
    2273             : 
    2274      188274 :   if (!signe(b))
    2275             :   { /* impossible in the current implementation : x square of maximal rank */
    2276           0 :     for (k = 1; k < i; k++)
    2277           0 :       for (j = 1; j < i; j++)
    2278           0 :         if (signe(gcoeff(x,k,j))) return k;
    2279           0 :     return 0;
    2280             :   }
    2281      188274 :   if (is_pm1(b)) return 0;
    2282      300271 :   for (k = 1; k < i; k++)
    2283     1078505 :     for (j = 1; j < i; j++)
    2284      862955 :       if (!dvdii(gcoeff(x,k,j),b)) return k;
    2285       81509 :   return 0;
    2286             : }
    2287             : 
    2288             : /* Return the SNF D of matrix X. If ptU/ptV non-NULL set them to U/V
    2289             :  * to that D = UXV */
    2290             : GEN
    2291      104170 : ZM_snfall_i(GEN x, GEN *ptU, GEN *ptV, int return_vec)
    2292             : {
    2293      104170 :   pari_sp av0 = avma, av;
    2294             :   long i, j, k, m0, m, n0, n;
    2295      104170 :   GEN p1, u, v, U, V, V0, mdet, ys, perm = NULL;
    2296             : 
    2297      104170 :   n0 = n = lg(x)-1;
    2298      104170 :   if (!n) {
    2299        4334 :     if (ptU) *ptU = cgetg(1,t_MAT);
    2300        4334 :     if (ptV) *ptV = cgetg(1,t_MAT);
    2301        4334 :     return cgetg(1, return_vec? t_VEC: t_MAT);
    2302             :   }
    2303       99836 :   av = avma;
    2304       99836 :   m0 = m = nbrows(x);
    2305             : 
    2306       99836 :   U = ptU? gen_1: NULL; /* TRANSPOSE of row transform matrix [act on columns]*/
    2307       99836 :   V = ptV? gen_1: NULL;
    2308       99836 :   V0 = NULL;
    2309       99836 :   x = RgM_shallowcopy(x);
    2310       99836 :   if (m == n && ZM_ishnf(x))
    2311             :   {
    2312       90687 :     mdet = ZM_det_triangular(x);
    2313       90687 :     if (V) *ptV = matid(n);
    2314             :   }
    2315             :   else
    2316             :   {
    2317        9149 :     mdet = ZM_detmult(x);
    2318        9149 :     if (signe(mdet))
    2319             :     {
    2320        9128 :       if (!V)
    2321         287 :         p1 = ZM_hnfmod(x,mdet);
    2322             :       else
    2323             :       {
    2324        8841 :         if (m == n)
    2325             :         {
    2326        8820 :           p1 = ZM_hnfmod(x,mdet);
    2327        8820 :           *ptV = RgM_solve(x,p1);
    2328             :         }
    2329             :         else
    2330          21 :           p1 = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
    2331             :       }
    2332        9128 :       mdet = ZM_det_triangular(p1);
    2333             :     }
    2334             :     else
    2335          21 :       p1 = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
    2336        9149 :     x = p1;
    2337             :   }
    2338       99836 :   n = lg(x)-1;
    2339       99836 :   if (V)
    2340             :   {
    2341       95818 :     V = *ptV;
    2342       95818 :     if (n != n0)
    2343             :     {
    2344          21 :       V0 = vecslice(V, 1, n0 - n); /* kernel */
    2345          21 :       V  = vecslice(V, n0-n+1, n0);
    2346          21 :       av = avma;
    2347             :     }
    2348             :   }
    2349             :   /* independent columns */
    2350       99836 :   if (!signe(mdet))
    2351             :   {
    2352          21 :     if (n)
    2353             :     {
    2354          21 :       x = ZM_snfall_i(shallowtrans(x), ptV, ptU, return_vec); /* swap ptV,ptU */
    2355          21 :       if (typ(x) == t_MAT && n != m) x = shallowtrans(x);
    2356          21 :       if (V) V = ZM_mul(V, shallowtrans(*ptV));
    2357          21 :       if (U) U = *ptU; /* TRANSPOSE */
    2358             :     }
    2359             :     else /* 0 matrix */
    2360             :     {
    2361           0 :       x = cgetg(1,t_MAT);
    2362           0 :       if (V) V = cgetg(1, t_MAT);
    2363           0 :       if (U) U = matid(m);
    2364             :     }
    2365          21 :     goto THEEND;
    2366             :   }
    2367       99815 :   if (U) U = matid(n);
    2368             : 
    2369             :   /* square, maximal rank n */
    2370       99815 :   p1 = gen_indexsort(RgM_diagonal_shallow(x), NULL, &negcmpii);
    2371       99815 :   ys = cgetg(n+1,t_MAT);
    2372       99815 :   for (j=1; j<=n; j++) gel(ys,j) = vecpermute(gel(x, p1[j]), p1);
    2373       99815 :   x = ys;
    2374       99815 :   if (U) U = vecpermute(U, p1);
    2375       99815 :   if (V) V = vecpermute(V, p1);
    2376             : 
    2377       99815 :   p1 = ZM_hnfmod(x, mdet);
    2378       99815 :   if (V) V = ZM_mul(V, RgM_solve(x,p1));
    2379       99815 :   x = p1;
    2380             : 
    2381       99815 :   if (DEBUGLEVEL>7) err_printf("starting SNF loop");
    2382      569754 :   for (i=n; i>1; i--)
    2383             :   {
    2384      185062 :     if (DEBUGLEVEL>7) err_printf("\ni = %ld: ",i);
    2385             :     for(;;)
    2386       73749 :     {
    2387      258811 :       int c = 0;
    2388             :       GEN a, b;
    2389     1047131 :       for (j=i-1; j>=1; j--)
    2390             :       {
    2391      788320 :         b = gcoeff(x,i,j); if (!signe(b)) continue;
    2392        9658 :         a = gcoeff(x,i,i);
    2393        9658 :         ZC_elem(b, a, x,V, j,i);
    2394        9658 :         if (gc_needed(av,1))
    2395             :         {
    2396           0 :           if (DEBUGMEM>1) pari_warn(warnmem,"[1]: ZM_snfall i = %ld", i);
    2397           0 :           snf_pile(av, &x,&U,&V);
    2398             :         }
    2399             :       }
    2400      258811 :       if (DEBUGLEVEL>7) err_printf("; ");
    2401     1047131 :       for (j=i-1; j>=1; j--)
    2402             :       {
    2403             :         GEN d;
    2404      788320 :         b = gcoeff(x,j,i); if (!signe(b)) continue;
    2405       95503 :         a = gcoeff(x,i,i);
    2406       95503 :         d = bezout_step(&a, &b, &u, &v);
    2407      508146 :         for (k = 1; k < i; k++)
    2408             :         {
    2409      412643 :           GEN t = addii(mulii(u,gcoeff(x,i,k)),mulii(v,gcoeff(x,j,k)));
    2410      825286 :           gcoeff(x,j,k) = subii(mulii(a,gcoeff(x,j,k)),
    2411      412643 :                                 mulii(b,gcoeff(x,i,k)));
    2412      412643 :           gcoeff(x,i,k) = t;
    2413             :         }
    2414       95503 :         gcoeff(x,j,i) = gen_0;
    2415       95503 :         gcoeff(x,i,i) = d;
    2416       95503 :         if (U) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
    2417       95503 :         if (gc_needed(av,1))
    2418             :         {
    2419           0 :           if (DEBUGMEM>1) pari_warn(warnmem,"[2]: ZM_snfall, i = %ld", i);
    2420           0 :           snf_pile(av, &x,&U,&V);
    2421             :         }
    2422       95503 :         c = 1;
    2423             :       }
    2424      258811 :       if (!c)
    2425             :       {
    2426      188274 :         k = ZM_snf_no_divide(x, i);
    2427      188274 :         if (!k) break;
    2428             : 
    2429             :         /* x[k,j] != 0 mod b */
    2430       10637 :         for (j=1; j<=i; j++)
    2431        7425 :           gcoeff(x,i,j) = addii(gcoeff(x,i,j),gcoeff(x,k,j));
    2432        3212 :         if (U) gel(U,i) = gadd(gel(U,i),gel(U,k));
    2433             :       }
    2434       73749 :       if (gc_needed(av,1))
    2435             :       {
    2436           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"[3]: ZM_snfall");
    2437           0 :         snf_pile(av, &x,&U,&V);
    2438             :       }
    2439             :     }
    2440             :   }
    2441       99815 :   if (DEBUGLEVEL>7) err_printf("\n");
    2442      384692 :   for (k=1; k<=n; k++)
    2443      284877 :     if (signe(gcoeff(x,k,k)) < 0)
    2444             :     {
    2445         112 :       if (V) ZV_togglesign(gel(V,k));
    2446         112 :       togglesign(gcoeff(x,k,k));
    2447             :     }
    2448             : THEEND:
    2449       99836 :   if (return_vec)
    2450             :   {
    2451       92426 :     long l = lg(x)-1;
    2452       92426 :     if (typ(x) == t_MAT) x = RgM_diagonal_shallow(x);
    2453       92426 :     if (m0 > l) x = shallowconcat(zerovec(m0-l), x);
    2454             :   }
    2455             : 
    2456       99836 :   if (V0)
    2457             :   {
    2458          21 :     if (!return_vec) x = shallowconcat(zeromat(m,n0-n), x);
    2459          21 :     if (V) V = shallowconcat(V0, V);
    2460             :   }
    2461       99836 :   if (U)
    2462             :   {
    2463       83115 :     U = shallowtrans(U);
    2464       83115 :     if (perm) U = vecpermute(U, perm_inv(perm));
    2465             :   }
    2466       99836 :   snf_pile(av0, &x,&U,&V);
    2467       99836 :   if (ptU) *ptU = U;
    2468       99836 :   if (ptV) *ptV = V;
    2469       99836 :   return x;
    2470             : }
    2471             : GEN
    2472       11267 : ZM_snfall(GEN x, GEN *U, GEN *V) { return ZM_snfall_i(x, U, V, 0); }
    2473             : GEN
    2474         427 : ZM_snf(GEN x) { return ZM_snfall_i(x, NULL,NULL, 1); }
    2475             : GEN
    2476         322 : smith(GEN x) { return ZM_snfall_i(x, NULL,NULL, 1); }
    2477             : GEN
    2478          21 : smithall(GEN x)
    2479             : {
    2480          21 :   GEN z = cgetg(4, t_VEC);
    2481          21 :   gel(z,3) = ZM_snfall_i(x, (GEN*)(z+1),(GEN*)(z+2), 0);
    2482          21 :   return z;
    2483             : }
    2484             : 
    2485             : void
    2486      188229 : ZM_snfclean(GEN d, GEN u, GEN v)
    2487             : {
    2488      188229 :   long i, c, l = lg(d);
    2489             : 
    2490      188229 :   if (typ(d) == t_VEC)
    2491      188229 :     for (c=1; c<l; c++) { GEN t = gel(d,c); if (is_pm1(t)) break; }
    2492             :   else
    2493             :   {
    2494           0 :     for (c=1; c<l; c++) { GEN t = gcoeff(d,c,c); if (is_pm1(t)) break; }
    2495           0 :     if (c < l) for (i = 1; i < c; i++) setlg(gel(d,i), c);
    2496             :   }
    2497      188229 :   setlg(d, c);
    2498      188229 :   if (u) for (i=1; i<l; i++) setlg(gel(u,i), c);
    2499      188229 :   if (v) setlg(v, c);
    2500      188229 : }
    2501             : 
    2502             : /* Assume z was computed by [g]smithall(). Remove the 1s on the diagonal */
    2503             : GEN
    2504         539 : smithclean(GEN z)
    2505             : {
    2506             :   long i, j, h, l, c, d;
    2507             :   GEN U, V, y, D, t;
    2508             : 
    2509         539 :   if (typ(z) != t_VEC) pari_err_TYPE("smithclean",z);
    2510         539 :   l = lg(z); if (l == 1) return cgetg(1,t_VEC);
    2511         532 :   U = gel(z,1);
    2512         532 :   if (l != 4 || typ(U) != t_MAT)
    2513             :   { /* assume z = vector of elementary divisors */
    2514        1470 :     for (c=1; c<l; c++)
    2515        1309 :       if (gequal1(gel(z,c))) break;
    2516         511 :     return gcopy_lg(z, c);
    2517             :   }
    2518          21 :   V = gel(z,2);
    2519          21 :   D = gel(z,3);
    2520          21 :   l = lg(D);
    2521          21 :   if (l == 1) return gcopy(z);
    2522          21 :   h = lgcols(D);
    2523          21 :   if (h > l)
    2524             :   { /* D = vconcat(zero matrix, diagonal matrix) */
    2525          21 :     for (c=1+h-l, d=1; c<h; c++,d++)
    2526          21 :       if (gequal1(gcoeff(D,c,d))) break;
    2527             :   }
    2528           7 :   else if (h < l)
    2529             :   { /* D = concat(zero matrix, diagonal matrix) */
    2530           7 :     for (c=1, d=1+l-h; d<l; c++,d++)
    2531           7 :       if (gequal1(gcoeff(D,c,d))) break;
    2532             :   }
    2533             :   else
    2534             :   { /* D diagonal */
    2535           0 :     for (c=1; c<l; c++)
    2536           0 :       if (gequal1(gcoeff(D,c,c))) break;
    2537           0 :     d = c;
    2538             :   }
    2539             :   /* U was (h-1)x(h-1), V was (l-1)x(l-1), D was (h-1)x(l-1) */
    2540          21 :   y = cgetg(4,t_VEC);
    2541             :   /* truncate U to (c-1) x (h-1) */
    2542          21 :   gel(y,1) = t = cgetg(h,t_MAT);
    2543          21 :   for (j=1; j<h; j++) gel(t,j) = gcopy_lg(gel(U,j), c);
    2544             :   /* truncate V to (l-1) x (d-1) */
    2545          21 :   gel(y,2) = gcopy_lg(V, d);
    2546          21 :   gel(y,3) = t = zeromatcopy(c-1, d-1);
    2547             :   /* truncate D to a (c-1) x (d-1) matrix */
    2548          21 :   if (d > 1)
    2549             :   {
    2550          14 :     if (h > l)
    2551             :     {
    2552          14 :       for (i=1+h-l, j=1; i<c; i++,j++)
    2553           7 :         gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
    2554             :     }
    2555           7 :     else if (h < l)
    2556             :     {
    2557           7 :       for (i=1, j=1+l-h; j<d; i++,j++)
    2558           0 :         gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
    2559             :     }
    2560             :     else
    2561             :     {
    2562           0 :       for (j=1; j<d; j++)
    2563           0 :         gcoeff(t,j,j) = gcopy(gcoeff(D,j,j));
    2564             :     }
    2565             :   }
    2566          21 :   return y;
    2567             : }
    2568             : 
    2569             : /* does b = x[i,i] divide all entries in x[1..i-1,1..i-1] ? If so, return 0;
    2570             :  * else return the index of a problematic row */
    2571             : static long
    2572         112 : gsnf_no_divide(GEN x, long i, long vx)
    2573             : {
    2574         112 :   GEN b = gcoeff(x,i,i);
    2575             :   long j, k;
    2576             : 
    2577         112 :   if (gequal0(b))
    2578             :   {
    2579          14 :     for (k = 1; k < i; k++)
    2580          14 :       for (j = 1; j < i; j++)
    2581          14 :         if (!gequal0(gcoeff(x,k,j))) return k;
    2582           0 :     return 0;
    2583             :   }
    2584             : 
    2585          98 :   if (!is_RgX(b,vx) || degpol(b)<=0) return 0;
    2586         133 :   for (k = 1; k < i; k++)
    2587         231 :     for (j = 1; j < i; j++)
    2588             :     {
    2589         154 :       GEN z = gcoeff(x,k,j), r;
    2590         154 :       if (!is_RgX(z,vx)) z = scalarpol(z, vx);
    2591         154 :       r = RgX_rem(z, b);
    2592         154 :       if (signe(r) && (! isinexactreal(r) ||
    2593           0 :              gexpo(r) > 16 + gexpo(b) - prec2nbits(gprecision(r)))
    2594           7 :          ) return k;
    2595             :     }
    2596          49 :   return 0;
    2597             : }
    2598             : 
    2599             : /* Hermite Normal Form, with base change matrix if ptB != NULL.
    2600             :  * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
    2601             :  * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
    2602             : GEN
    2603          35 : RgM_hnfall(GEN A, GEN *pB, long remove)
    2604             : {
    2605             :   pari_sp av;
    2606             :   long li, j, k, m, n, def, ldef;
    2607             :   GEN B;
    2608          35 :   long vx = gvar(A);
    2609             : 
    2610          35 :   n = lg(A)-1;
    2611          35 :   if (vx==NO_VARIABLE || !n)
    2612             :   {
    2613           0 :     RgM_check_ZM(A, "mathnf0");
    2614           0 :     return ZM_hnfall(A, pB, remove);
    2615             :   }
    2616          35 :   m = nbrows(A);
    2617          35 :   av = avma;
    2618          35 :   A = RgM_shallowcopy(A);
    2619          35 :   B = pB? matid(n): NULL;
    2620          35 :   def = n; ldef = (m>n)? m-n: 0;
    2621          77 :   for (li=m; li>ldef; li--)
    2622             :   {
    2623             :     GEN d, T;
    2624          63 :     for (j=def-1; j; j--)
    2625             :     {
    2626          21 :       GEN a = gcoeff(A,li,j);
    2627          21 :       if (gequal0(a)) continue;
    2628             : 
    2629           7 :       k = (j==1)? def: j-1;
    2630           7 :       RgC_elem(a,gcoeff(A,li,k), A,B, j,k, li, vx);
    2631             :     }
    2632          42 :     T = normalize_as_RgX(gcoeff(A,li,def), vx, &d);
    2633          42 :     if (gequal0(T))
    2634           0 :     { if (ldef) ldef--; }
    2635             :     else
    2636             :     {
    2637          42 :       gcoeff(A,li,def) = T;
    2638          42 :       if (B && !gequal1(d)) gel(B, def) = RgC_Rg_div(gel(B, def), d);
    2639          42 :       RgM_reduce(A, B, li, def, vx);
    2640          42 :       def--;
    2641             :     }
    2642          42 :     if (gc_needed(av,1))
    2643             :     {
    2644           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ghnfall");
    2645           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2646             :     }
    2647             :   }
    2648             :   /* rank A = n - def */
    2649          35 :   if (remove) remove_0cols(def, &A, &B, remove);
    2650          35 :   gerepileall(av, B? 2: 1, &A, &B);
    2651          35 :   if (B) *pB = B;
    2652          35 :   return A;
    2653             : }
    2654             : 
    2655             : static GEN
    2656          35 : RgXM_snf(GEN x,long all)
    2657             : {
    2658             :   pari_sp av;
    2659             :   long i, j, k, n;
    2660             :   GEN z, u, v, U, V;
    2661          35 :   long vx = gvar(x);
    2662          35 :   n = lg(x)-1; if (!n) return trivsmith(all);
    2663          35 :   if (vx==NO_VARIABLE) pari_err_TYPE("RgXM_snf",x);
    2664          35 :   if (lgcols(x) != n+1) pari_err_DIM("gsmithall");
    2665          35 :   av = avma;
    2666          35 :   x = RgM_shallowcopy(x);
    2667          35 :   if (all) { U = matid(n); V = matid(n); }
    2668         252 :   for (i=n; i>=2; i--)
    2669             :   {
    2670             :     for(;;)
    2671          98 :     {
    2672             :       GEN a, b, d;
    2673         189 :       int c = 0;
    2674         546 :       for (j=i-1; j>=1; j--)
    2675             :       {
    2676         357 :         b = gcoeff(x,i,j); if (gequal0(b)) continue;
    2677         140 :         a = gcoeff(x,i,i);
    2678         140 :         d = gbezout_step(&b, &a, &v, &u, vx);
    2679         469 :         for (k = 1; k < i; k++)
    2680             :         {
    2681         329 :           GEN t = gadd(gmul(u,gcoeff(x,k,i)),gmul(v,gcoeff(x,k,j)));
    2682         329 :           gcoeff(x,k,j) = gsub(gmul(a,gcoeff(x,k,j)),gmul(b,gcoeff(x,k,i)));
    2683         329 :           gcoeff(x,k,i) = t;
    2684             :         }
    2685         140 :         gcoeff(x,i,j) = gen_0;
    2686         140 :         gcoeff(x,i,i) = d;
    2687         140 :         if (all) update(u,v,a,b,(GEN*)(V+i),(GEN*)(V+j));
    2688             :       }
    2689         546 :       for (j=i-1; j>=1; j--)
    2690             :       {
    2691         357 :         b = gcoeff(x,j,i); if (gequal0(b)) continue;
    2692         119 :         a = gcoeff(x,i,i);
    2693         119 :         d = gbezout_step(&b, &a, &v, &u, vx);
    2694         406 :         for (k = 1; k < i; k++)
    2695             :         {
    2696         287 :           GEN t = gadd(gmul(u,gcoeff(x,i,k)),gmul(v,gcoeff(x,j,k)));
    2697         287 :           gcoeff(x,j,k) = gsub(gmul(a,gcoeff(x,j,k)),gmul(b,gcoeff(x,i,k)));
    2698         287 :           gcoeff(x,i,k) = t;
    2699             :         }
    2700         119 :         gcoeff(x,j,i) = gen_0;
    2701         119 :         gcoeff(x,i,i) = d;
    2702         119 :         if (all) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
    2703         119 :         c = 1;
    2704             :       }
    2705         189 :       if (!c)
    2706             :       {
    2707         112 :         k = gsnf_no_divide(x, i, vx);
    2708         112 :         if (!k) break;
    2709             : 
    2710          70 :         for (j=1; j<=i; j++)
    2711          49 :           gcoeff(x,i,j) = gadd(gcoeff(x,i,j),gcoeff(x,k,j));
    2712          21 :         if (all) gel(U,i) = gadd(gel(U,i),gel(U,k));
    2713             :       }
    2714          98 :       if (gc_needed(av,1))
    2715             :       {
    2716           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"gsmithall");
    2717           0 :         gerepileall(av, all? 3: 1, &x, &U, &V);
    2718             :       }
    2719             :     }
    2720             :   }
    2721         161 :   for (k=1; k<=n; k++)
    2722             :   {
    2723         126 :     GEN d, T = normalize_as_RgX(gcoeff(x,k,k), vx, &d);
    2724         126 :     if (gequal0(T)) continue;
    2725         112 :     if (all && !gequal1(d)) gel(V,k) = RgC_Rg_div(gel(V,k), d);
    2726         112 :     gcoeff(x,k,k) = T;
    2727             :   }
    2728          35 :   z = all? mkvec3(shallowtrans(U), V, x): RgM_diagonal_shallow(x);
    2729          35 :   return gerepilecopy(av, z);
    2730             : }
    2731             : 
    2732             : GEN
    2733         378 : matsnf0(GEN x,long flag)
    2734             : {
    2735         378 :   pari_sp av = avma;
    2736         378 :   if (flag > 7) pari_err_FLAG("matsnf");
    2737         378 :   if (typ(x) == t_VEC && flag & 4) return smithclean(x);
    2738         378 :   if (typ(x)!=t_MAT) pari_err_TYPE("matsnf",x);
    2739         378 :   if (RgM_is_ZM(x)) x = flag&1 ? smithall(x): smith(x);
    2740          35 :   else              x = RgXM_snf(x, flag&1);
    2741         378 :   if (flag & 4) x = gerepileupto(av, smithclean(x));
    2742         378 :   return x;
    2743             : }
    2744             : GEN
    2745           0 : gsmith(GEN x) { return RgXM_snf(x,0); }
    2746             : GEN
    2747           0 : gsmithall(GEN x) { return RgXM_snf(x,1); }
    2748             : 
    2749             : /* H is a relation matrix, either in HNF or a t_VEC (diagonal HNF) */
    2750             : static GEN
    2751      188229 : snf_group(GEN H, GEN D, GEN *newU, GEN *newUi)
    2752             : {
    2753             :   long i, j, l;
    2754             : 
    2755      188229 :   ZM_snfclean(D, newU? *newU: NULL, newUi? *newUi: NULL);
    2756      188229 :   l = lg(D);
    2757      188229 :   if (newU) {
    2758      171822 :     GEN U = *newU;
    2759      528290 :     for (i = 1; i < l; i++)
    2760             :     {
    2761      356468 :       GEN d = gel(D,i), d2 = shifti(d, 1);
    2762     1726354 :       for (j = 1; j < lg(U); j++)
    2763     1369886 :         gcoeff(U,i,j) = centermodii(gcoeff(U,i,j), d, d2);
    2764             :     }
    2765      171822 :     *newU = U;
    2766             :   }
    2767      188229 :   if (newUi && l > 1)
    2768             :   { /* UHV=D -> U^-1 = (HV)D^-1 -> U^-1 = H(VD^-1 mod 1) mod H */
    2769             :     /* Ui = ZM_inv(U, NULL); setlg(Ui, l); */
    2770      180766 :     GEN V = *newUi, Ui;
    2771      180766 :     int Hvec = (typ(H) == t_VEC);
    2772      180766 :     for (i = 1; i < l; i++) gel(V,i) = FpC_red(gel(V,i), gel(D,i));
    2773      180766 :     if (!Hvec)
    2774             :     {
    2775       81765 :       if (ZM_isdiagonal(H)) { H = RgM_diagonal_shallow(H); Hvec = 1; }
    2776             :     }
    2777      180766 :     Ui = Hvec? ZM_diag_mul(H, V): ZM_mul(H, V);
    2778      180766 :     for (i = 1; i < l; i++) gel(Ui,i) = ZC_Z_divexact(gel(Ui,i), gel(D,i));
    2779      180766 :     if (Hvec)
    2780      131900 :     { for (i = 1; i < l; i++) gel(Ui,i) = vecmodii(gel(Ui,i), H); }
    2781             :     else
    2782       48866 :       Ui = ZM_hnfrem(Ui, H);
    2783      180766 :     *newUi = Ui;
    2784             :   }
    2785      188229 :   return D;
    2786             : }
    2787             : /* H relation matrix among row of generators g in HNF.  Let URV = D its SNF,
    2788             :  * newU R newV = newD its clean SNF (no 1 in Dnew). Return the diagonal of
    2789             :  * newD, newU and newUi such that  1/U = (newUi, ?).
    2790             :  * Rationale: let (G,0) = g Ui be the new generators then
    2791             :  * 0 = G U R --> G D = 0,  g = G newU  and  G = g newUi */
    2792             : GEN
    2793       89039 : ZM_snf_group(GEN H, GEN *newU, GEN *newUi)
    2794             : {
    2795       89039 :   GEN D = ZM_snfall_i(H, newU, newUi, 1);
    2796       89039 :   return snf_group(H, D, newU, newUi);
    2797             : }
    2798             : 
    2799             : /* D a ZV: SNF for matdiagonal(D). Faster because we only ensure elementary
    2800             :  * divisors condition: d[n] | ... | d[1] and need not convert D to matrix form*/
    2801             : GEN
    2802       99190 : ZV_snfall(GEN D, GEN *pU, GEN *pV)
    2803             : {
    2804       99190 :   pari_sp av = avma;
    2805       99190 :   long j, n = lg(D)-1;
    2806       99190 :   GEN U = pU? matid(n): NULL;
    2807       99190 :   GEN V = pV? matid(n): NULL;
    2808             :   GEN p;
    2809             : 
    2810       99190 :   D = leafcopy(D);
    2811      324919 :   for (j = n; j > 0; j--)
    2812             :   {
    2813      225729 :     GEN b = gel(D,j);
    2814      225729 :     if (signe(b) < 0)
    2815             :     {
    2816           0 :       gel(D,j) = negi(b);
    2817           0 :       if (V) ZV_togglesign(gel(V,j));
    2818             :     }
    2819             :   }
    2820             :   /* entries are non-negative integers */
    2821       99190 :   p = gen_indexsort(D, NULL, &negcmpii);
    2822       99190 :   D = vecpermute(D, p);
    2823       99190 :   if (U) U = vecpermute(U, p);
    2824       99190 :   if (V) V = vecpermute(V, p);
    2825             :   /* entries are sorted by decreasing value */
    2826      324919 :   for (j = n; j > 0; j--)
    2827             :   {
    2828      225729 :     GEN b = gel(D,j);
    2829             :     long i;
    2830      414575 :     for (i = j-1; i > 0; i--)
    2831             :     { /* invariant: a >= b. If au+bv = d is a Bezout relation, A=a/d and B=b/d
    2832             :        * we have [B,-A;u,v]*diag(a,b)*[1-u*A,1; -u*A,1]] = diag(Ab, d) */
    2833      189987 :       GEN a = gel(D,i), u,v, d = bezout(a,b, &u,&v), A, Wi, Wj;
    2834      189987 :       if (equalii(d,b)) continue;
    2835       25123 :       A = diviiexact(a,d);
    2836       25123 :       if (V)
    2837             :       {
    2838       25081 :         GEN t = mulii(u,A);
    2839       25081 :         Wi = ZC_lincomb(subui(1,t), negi(t), gel(V,i), gel(V,j));
    2840       25081 :         Wj = ZC_add(gel(V,i), gel(V,j));
    2841       25081 :         gel(V,i) = Wi;
    2842       25081 :         gel(V,j) = Wj;
    2843             :       }
    2844       25123 :       if (U)
    2845             :       {
    2846       25123 :         GEN B = diviiexact(b,d);
    2847       25123 :         Wi = ZC_lincomb(B, negi(A), gel(U,i), gel(U,j));
    2848       25123 :         Wj = ZC_lincomb(u, v, gel(U,i), gel(U,j));
    2849       25123 :         gel(U,i) = Wi;
    2850       25123 :         gel(U,j) = Wj;
    2851             :       }
    2852       25123 :       gel(D,i) = mulii(A,b); /* lcm(a,b) */
    2853       25123 :       gel(D,j) = d; /* gcd(a,b) */
    2854       25123 :       b = gel(D,j); if (equali1(b)) break;
    2855             :     }
    2856             :   }
    2857       99190 :   snf_pile(av, &D,&U,&V);
    2858       99190 :   if (U) *pU = shallowtrans(U);
    2859       99190 :   if (V) *pV = V;
    2860       99190 :   return D;
    2861             : }
    2862             : GEN
    2863       99190 : ZV_snf_group(GEN d, GEN *newU, GEN *newUi)
    2864             : {
    2865       99190 :   GEN D = ZV_snfall(d, newU, newUi);
    2866       99190 :   return snf_group(d, D, newU, newUi);
    2867             : }
    2868             : 
    2869             : /* D a vector of elementary divisors. Truncate (setlg) to leave out trivial
    2870             :  * entries (= 1) */
    2871             : void
    2872         224 : ZV_snf_trunc(GEN D)
    2873             : {
    2874         224 :   long i, l = lg(D);
    2875         490 :   for (i = 1; i < l; i++)
    2876         371 :     if (is_pm1(gel(D,i))) { setlg(D,i); break; }
    2877         224 : }

Generated by: LCOV version 1.13