Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - hnf_snf.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 22872-edcf83abb) Lines: 1516 1665 91.1 %
Date: 2018-07-20 05:36:03 Functions: 87 92 94.6 %
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     3855511 : count(GEN mat, long row, long len, long *firstnonzero)
      68             : {
      69     3855511 :   long j, n = 0;
      70             : 
      71   348485103 :   for (j=1; j<=len; j++)
      72             :   {
      73   345804289 :     long p = mael(mat,j,row);
      74   345804289 :     if (p)
      75             :     {
      76    10756441 :       if (labs(p)!=1) return -1;
      77     9581744 :       n++; *firstnonzero=j;
      78             :     }
      79             :   }
      80     2680814 :   return n;
      81             : }
      82             : 
      83             : static int
      84      151450 : count2(GEN mat, long row, long len)
      85             : {
      86             :   long j;
      87     4520695 :   for (j=len; j; j--)
      88     4442188 :     if (labs(mael(mat,j,row)) == 1) return j;
      89       78507 :   return 0;
      90             : }
      91             : 
      92             : static GEN
      93       44169 : hnffinal(GEN matgen,GEN perm,GEN* ptdep,GEN* ptB,GEN* ptC)
      94             : {
      95             :   GEN p1,p2,U,H,Hnew,Bnew,Cnew,diagH1;
      96       44169 :   GEN B = *ptB, C = *ptC, dep = *ptdep, depnew;
      97             :   pari_sp av;
      98             :   long i,j,k,s,i1,j1,zc;
      99       44169 :   long co = lg(C);
     100       44169 :   long col = lg(matgen)-1;
     101             :   long lnz, nlze, lig;
     102             : 
     103       44169 :   if (col == 0) return matgen;
     104       43812 :   lnz = nbrows(matgen); /* was called lnz-1 - nr in hnfspec */
     105       43812 :   nlze = nbrows(dep);
     106       43812 :   lig = nlze + lnz;
     107             :   /* H: lnz x lnz [disregarding initial 0 cols], U: col x col */
     108       43812 :   H = ZM_hnflll(matgen, &U, 0);
     109       43812 :   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       43812 :   zc = col - lnz; /* # of 0 columns, correspond to units */
     113       43812 :   if (nlze) { dep = ZM_mul(dep,U); dep += zc; }
     114             : 
     115       43812 :   diagH1 = new_chunk(lnz+1); /* diagH1[i] = 0 iff H[i,i] != 1 (set later) */
     116             : 
     117       43812 :   av = avma;
     118       43812 :   Cnew = cgetg(co, typ(C));
     119       43812 :   setlg(C, col+1); p1 = gmul(C,U);
     120       43812 :   for (j=1; j<=col; j++) gel(Cnew,j) = gel(p1,j);
     121       43812 :   for (   ; j<co ; j++)  gel(Cnew,j) = gel(C,j);
     122             : 
     123             :   /* Clean up B using new H */
     124      210603 :   for (s=0,i=lnz; i; i--)
     125             :   {
     126      166791 :     GEN Di = gel(dep,i), Hi = gel(H,i);
     127      166791 :     GEN h = gel(Hi,i); /* H[i,i] */
     128      166791 :     if ( (diagH1[i] = is_pm1(h)) ) { h = NULL; s++; }
     129    10553245 :     for (j=col+1; j<co; j++)
     130             :     {
     131    10386454 :       GEN z = gel(B,j-col);
     132    10386454 :       p1 = gel(z,i+nlze);
     133    10386454 :       if (h) p1 = truedivii(p1,h);
     134    10386454 :       if (!signe(p1)) continue;
     135     6366424 :       for (k=1; k<=nlze; k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Di,k)));
     136     6366424 :       for (   ; k<=lig;  k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Hi,k-nlze)));
     137     6366424 :       gel(Cnew,j) = gsub(gel(Cnew,j), gmul(p1, gel(Cnew,i+zc)));
     138             :     }
     139      166791 :     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       43812 :   p1 = cgetg(lnz+1,t_VEC); p2 = perm + nlze;
     146      210603 :   for (i1=0, j1=lnz-s, i=1; i<=lnz; i++) /* push the 1 rows down */
     147      166791 :     if (diagH1[i])
     148       78742 :       gel(p1,++j1) = gel(p2,i);
     149             :     else
     150       88049 :       gel(p2,++i1) = gel(p2,i);
     151       43812 :   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       43812 :   lig -= s; col -= s; lnz -= s;
     162       43812 :   Hnew = cgetg(lnz+1,t_MAT);
     163       43812 :   depnew = cgetg(lnz+1,t_MAT); /* only used if nlze > 0 */
     164       43812 :   Bnew = cgetg(co-col,t_MAT);
     165       43812 :   C = shallowcopy(Cnew);
     166      210603 :   for (j=1,i1=j1=0; j<=lnz+s; j++)
     167             :   {
     168      166791 :     GEN z = gel(H,j);
     169      166791 :     if (diagH1[j])
     170             :     { /* hit exactly s times */
     171       78742 :       i1++; C[i1+col] = Cnew[j+zc];
     172       78742 :       p1 = cgetg(lig+1,t_COL); gel(Bnew,i1) = p1;
     173       78742 :       for (i=1; i<=nlze; i++) gel(p1,i) = gcoeff(dep,i,j);
     174       78742 :       p1 += nlze;
     175             :     }
     176             :     else
     177             :     {
     178       88049 :       j1++; C[j1+zc] = Cnew[j+zc];
     179       88049 :       p1 = cgetg(lnz+1,t_COL); gel(Hnew,j1) = p1;
     180       88049 :       depnew[j1] = dep[j];
     181             :     }
     182      869640 :     for (i=k=1; k<=lnz; i++)
     183      702849 :       if (!diagH1[i]) p1[k++] = z[i];
     184             :   }
     185     1792036 :   for (j=s+1; j<co-col; j++)
     186             :   {
     187     1748224 :     GEN z = gel(B,j-s);
     188     1748224 :     p1 = cgetg(lig+1,t_COL); gel(Bnew,j) = p1;
     189     1748224 :     for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
     190     1748224 :     z += nlze; p1 += nlze;
     191     8336965 :     for (i=k=1; k<=lnz; i++)
     192     6588741 :       if (!diagH1[i]) gel(p1,k++) = gel(z,i);
     193             :   }
     194       43812 :   *ptdep = depnew;
     195       43812 :   *ptC = C;
     196       43812 :   *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 :   avma = av;
     209           0 : }
     210             : 
     211             : static GEN
     212      658450 : col_dup(long l, GEN col)
     213             : {
     214      658450 :   GEN c = new_chunk(l);
     215      658450 :   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       43812 : ZM_rowrankprofile(GEN x, long *nlze)
     221             : {
     222       43812 :   pari_sp av = avma;
     223             :   GEN d, y;
     224             :   long i, j, k, l, r;
     225             : 
     226       43812 :   x = shallowtrans(x); l = lg(x);
     227       43812 :   (void)new_chunk(l); /* HACK */
     228       43812 :   d = ZM_pivots(x,&r); avma = av;
     229       43812 :   *nlze = r;
     230       43812 :   if (!d) return identity_perm(l-1);
     231       40914 :   y = cgetg(l,t_VECSMALL);
     232      313300 :   for (i = j = 1, k = r+1; i<l; i++)
     233      272386 :     if (d[i]) y[k++] = i; else y[j++] = i;
     234       40914 :   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       24940 : 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       24940 :   const long li = lg(perm); /* = lgcols(mat0) */
     258       24940 :   const long CO = lg(mat0);
     259             : 
     260       24940 :   n = 0; /* -Wall */
     261             : 
     262       24940 :   C = *ptC; co = CO;
     263       24940 :   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       24940 :   if (DEBUGLEVEL>5)
     270             :   {
     271           0 :     err_printf("Entering hnfspec\n");
     272           0 :     p_mat(mat0,perm,0);
     273             :   }
     274       24940 :   matt = cgetg(co, t_MAT); /* dense part of mat (top) */
     275       24940 :   mat = cgetg(co, t_MAT);
     276      683390 :   for (j = 1; j < co; j++)
     277             :   {
     278      658450 :     GEN matj = col_dup(li, gel(mat0,j));
     279      658450 :     p1 = cgetg(k0+1,t_COL); gel(matt,j) = p1; gel(mat,j) = matj;
     280      658450 :     for (i=1; i<=k0; i++) gel(p1,i) = stoi(matj[perm[i]]);
     281             :   }
     282       24940 :   av = avma;
     283             : 
     284       24940 :   i = lig = li-1; col = co-1; lk0 = k0;
     285       24940 :   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     2887236 :   while (i > lk0 && col)
     288     2837356 :     switch( count(mat,perm[i],col,&n) )
     289             :     {
     290             :       case 0: /* move zero lines between k0+1 and lk0 */
     291       19187 :         lk0++; lswap(perm[i], perm[lk0]);
     292       19187 :         i = lig; continue;
     293             : 
     294             :       case 1: /* move trivial generator between lig+1 and li */
     295      139232 :         lswap(perm[i], perm[lig]);
     296      139232 :         if (T) swap(gel(T,n), gel(T,col));
     297      139232 :         swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     298      139232 :         if (p[perm[lig]] < 0) /* = -1 */
     299             :         { /* convert relation -g = 0 to g = 0 */
     300      113318 :           for (i=lk0+1; i<lig; i++) p[perm[i]] = -p[perm[i]];
     301      113318 :           if (T)
     302             :           {
     303      113318 :             p1 = gel(T,col);
     304     5854155 :             for (i=1; ; i++) /* T is a permuted identity: single non-0 entry */
     305    11594992 :               if (signe(gel(p1,i))) { togglesign_safe(&gel(p1,i)); break; }
     306             :           }
     307             :         }
     308      139232 :         lig--; col--; i = lig; continue;
     309             : 
     310     2678937 :       default: i--;
     311             :     }
     312       24940 :   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       24940 :   s = 0;
     319      275320 :   while (lig > lk0 && col && s < (long)(HIGHBIT>>1))
     320             :   {
     321     1039937 :     for (i=lig; i>lk0; i--)
     322     1018155 :       if (count(mat,perm[i],col,&n) > 0) break;
     323      247222 :     if (i == lk0) break;
     324             : 
     325             :     /* only 0, +/- 1 entries, at least 2 of them non-zero */
     326      225440 :     lswap(perm[i], perm[lig]);
     327      225440 :     swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     328      225440 :     if (T) swap(gel(T,n), gel(T,col));
     329      225440 :     if (p[perm[lig]] < 0)
     330             :     {
     331      157297 :       for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
     332      157297 :       if (T) ZV_togglesign(gel(T,col));
     333             :     }
     334    10720464 :     for (j=1; j<col; j++)
     335             :     {
     336    10495024 :       GEN matj = gel(mat,j);
     337             :       long t;
     338    10495024 :       if (! (t = matj[perm[lig]]) ) continue;
     339      619695 :       if (t == 1) {
     340      297908 :         for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= p[perm[i]]);
     341             :       }
     342             :       else { /* t = -1 */
     343      321787 :         for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] += p[perm[i]]);
     344             :       }
     345      619695 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
     346             :     }
     347      225440 :     lig--; col--;
     348      225440 :     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 avma = av;
     352             :     }
     353             :   }
     354             :   /* As above with lines containing a +/- 1 (no other assumption).
     355             :    * Stop when single precision becomes dangerous */
     356       24940 :   vmax = cgetg(co,t_VECSMALL);
     357      318718 :   for (j=1; j<=col; j++)
     358             :   {
     359      293778 :     GEN matj = gel(mat,j);
     360      293778 :     for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[i]);
     361      293778 :     vmax[j] = s;
     362             :   }
     363      122818 :   while (lig > lk0 && col)
     364             :   {
     365      158089 :     for (i=lig; i>lk0; i--)
     366      151450 :       if ( (n = count2(mat,perm[i],col)) ) break;
     367       79582 :     if (i == lk0) break;
     368             : 
     369       72943 :     lswap(vmax[n], vmax[col]);
     370       72943 :     lswap(perm[i], perm[lig]);
     371       72943 :     swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     372       72943 :     if (T) swap(gel(T,n), gel(T,col));
     373       72943 :     if (p[perm[lig]] < 0)
     374             :     {
     375       33079 :       for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
     376       33079 :       if (T) ZV_togglesign(gel(T,col));
     377             :     }
     378     1402779 :     for (j=1; j<col; j++)
     379             :     {
     380     1329841 :       GEN matj = gel(mat,j);
     381             :       long t;
     382     1329841 :       if (! (t = matj[perm[lig]]) ) continue;
     383      713428 :       if (vmax[col] && (ulong)labs(t) >= (HIGHBIT-vmax[j]) / vmax[col])
     384           5 :         goto END2;
     385             : 
     386      713423 :       for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= t*p[perm[i]]);
     387      713423 :       vmax[j] = s;
     388      713423 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
     389             :     }
     390       72938 :     lig--; col--;
     391       72938 :     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       24940 :   matb = cgetg(co,t_MAT); /* bottom part (complement of matt) */
     401      683390 :   for (j=1; j<co; j++)
     402             :   {
     403      658450 :     GEN matj = gel(mat,j);
     404      658450 :     p1 = cgetg(li-k0,t_COL); gel(matb,j) = p1;
     405      658450 :     p1 -= k0;
     406      658450 :     for (i=k0+1; i<li; i++) gel(p1,i) = stoi(matj[perm[i]]);
     407             :   }
     408       24940 :   if (DEBUGLEVEL>5)
     409             :   {
     410           0 :     err_printf("    after phase2:\n");
     411           0 :     p_mat(mat,perm,lk0);
     412             :   }
     413      437903 :   for (i=li-2; i>lig; i--)
     414             :   {
     415      412963 :     long h, i0 = i - k0, k = i + co-li;
     416      412963 :     GEN Bk = gel(matb,k);
     417    16190097 :     for (j=k+1; j<co; j++)
     418             :     {
     419    15777134 :       GEN Bj = gel(matb,j), v = gel(Bj,i0);
     420    15777134 :       s = signe(v); if (!s) continue;
     421             : 
     422     3179561 :       gel(Bj,i0) = gen_0;
     423     3179561 :       if (is_pm1(v))
     424             :       {
     425     1593876 :         if (s > 0) /* v = 1 */
     426      794628 :         { for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), gel(Bk,h)); }
     427             :         else /* v = -1 */
     428      799248 :         { for (h=1; h<i0; h++) gel(Bj,h) = addii(gel(Bj,h), gel(Bk,h)); }
     429             :       }
     430             :       else {
     431     1585685 :         for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), mulii(v,gel(Bk,h)));
     432             :       }
     433     3179561 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,k), negi(v));
     434     3179561 :       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       24940 :   for (j=1; j<co; j++) setlg(matb[j], lig-k0+1); /* bottom can be forgotten */
     444       24940 :   gerepileall(av, T? 2: 1, &matb, &T);
     445       24940 :   if (DEBUGLEVEL>5) err_printf("    matb cleaned up (using Id block)\n");
     446             : 
     447       24940 :   nlze = lk0 - k0;  /* # of 0 rows */
     448       24940 :   lnz = lig-nlze+1; /* 1 + # of non-0 rows (!= 0...0 1 0 ... 0) */
     449       24940 :   if (T) matt = ZM_mul(matt,T); /* update top rows */
     450       24940 :   extramat = cgetg(col+1,t_MAT); /* = new C minus the 0 rows */
     451      245780 :   for (j=1; j<=col; j++)
     452             :   {
     453      220840 :     GEN z = gel(matt,j);
     454      220840 :     GEN t = (gel(matb,j)) + nlze - k0;
     455      220840 :     p2=cgetg(lnz,t_COL); gel(extramat,j) = p2;
     456      220840 :     for (i=1; i<=k0; i++) gel(p2,i) = gel(z,i); /* top k0 rows */
     457      220840 :     for (   ; i<lnz; i++) gel(p2,i) = gel(t,i); /* other non-0 rows */
     458             :   }
     459       24940 :   if (!col) {
     460         357 :     permpro = identity_perm(lnz);
     461         357 :     nr = lnz;
     462             :   }
     463             :   else
     464       24583 :     permpro = ZM_rowrankprofile(extramat, &nr);
     465             :   /* lnz = lg(permpro) */
     466       24940 :   if (nlze)
     467             :   { /* put the nlze 0 rows (trivial generators) at the top */
     468        3922 :     p1 = new_chunk(lk0+1);
     469        3922 :     for (i=1; i<=nlze; i++) p1[i] = perm[i + k0];
     470        3922 :     for (   ; i<=lk0; i++)  p1[i] = perm[i - nlze];
     471        3922 :     for (i=1; i<=lk0; i++)  perm[i] = p1[i];
     472             :   }
     473             :   /* sort other rows according to permpro (nr redundant generators first) */
     474       24940 :   p1 = new_chunk(lnz); p2 = perm + nlze;
     475       24940 :   for (i=1; i<lnz; i++) p1[i] = p2[permpro[i]];
     476       24940 :   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       24940 :   matbnew = cgetg(col+1,t_MAT); /* dense+toobig, maximal rank. For hnffinal */
     492       24940 :   dep    = cgetg(col+1,t_MAT); /* rows dependent from the ones in matbnew */
     493      245780 :   for (j=1; j<=col; j++)
     494             :   {
     495      220840 :     GEN z = gel(extramat,j);
     496      220840 :     p1 = cgetg(nlze+nr+1,t_COL); gel(dep,j) = p1;
     497      220840 :     p2 = cgetg(lnz-nr,t_COL); gel(matbnew,j) = p2;
     498      220840 :     for (i=1; i<=nlze; i++) gel(p1,i) = gen_0;
     499      220840 :     p1 += nlze; for (i=1; i<=nr; i++) p1[i] = z[permpro[i]];
     500      220840 :     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       24940 :   B = cgetg(co-col,t_MAT);
     506      462550 :   for (j=col+1; j<co; j++)
     507             :   {
     508      437610 :     GEN y = gel(matt,j);
     509      437610 :     GEN z = gel(matb,j);
     510      437610 :     p1=cgetg(lig+1,t_COL); gel(B,j-col) = p1;
     511      437610 :     for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
     512      437610 :     p1 += nlze; z += nlze-k0;
     513     4542417 :     for (k=1; k<lnz; k++)
     514             :     {
     515     4104807 :       i = permpro[k];
     516     4104807 :       gel(p1,k) = (i <= k0)? gel(y,i): gel(z,i);
     517             :     }
     518             :   }
     519       24940 :   if (T) C = typ(C)==t_MAT? RgM_mul(C,T): RgV_RgM_mul(C,T);
     520       24940 :   gerepileall(av, 4, &matbnew, &B, &dep, &C);
     521       24940 :   *ptdep = dep;
     522       24940 :   *ptB = B;
     523       24940 :   H = hnffinal(matbnew, perm, ptdep, ptB, &C);
     524       24940 :   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       24940 :   *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       19229 : hnfadd_i(GEN H, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, /* cf hnfspec */
     612             :        GEN extramat,GEN extraC)
     613             : {
     614       19229 :   GEN matb, extratop, Cnew, permpro, B = *ptB, C = *ptC, dep = *ptdep;
     615             :   long i, lH, lB, li, lig, co, col, nlze;
     616             : 
     617       19229 :   if (lg(extramat) == 1) return H;
     618       19229 :   co = lg(C)-1;
     619       19229 :   lH = lg(H)-1;
     620       19229 :   lB = lg(B)-1;
     621       19229 :   li = lg(perm)-1;
     622       19229 :   lig = li - lB;
     623       19229 :   col = co - lB;
     624       19229 :   nlze = lig - lH;
     625             : 
     626             :  /*               col    co
     627             :   *       [ 0 |dep |     ]
     628             :   *  nlze [--------|  B  ]
     629             :   *       [ 0 | H  |     ]
     630             :   *       [--------|-----] lig
     631             :   *       [   0    | Id  ]
     632             :   *       [        |     ] li */
     633       19229 :   extratop = zm_to_ZM( rowslicepermute(extramat, perm, 1, lig) );
     634       19229 :   if (li != lig)
     635             :   { /* zero out bottom part, using the Id block */
     636       19229 :     GEN A = vecslice(C, col+1, co);
     637       19229 :     GEN c = rowslicepermute(extramat, perm, lig+1, li);
     638       19229 :     extraC   = gsub(extraC, typ(A)==t_MAT? RgM_zm_mul(A, c): RgV_zm_mul(A,c));
     639       19229 :     extratop = ZM_sub(extratop, ZM_zm_mul(B, c));
     640             :   }
     641             : 
     642       19229 :   extramat = shallowconcat(extratop, vconcat(dep, H));
     643       19229 :   Cnew     = shallowconcat(extraC, vecslice(C, col-lH+1, co));
     644       19229 :   if (DEBUGLEVEL>5) err_printf("    1st phase done\n");
     645       19229 :   permpro = ZM_rowrankprofile(extramat, &nlze);
     646       19229 :   extramat = rowpermute(extramat, permpro);
     647       19229 :   *ptB     = rowpermute(B,        permpro);
     648       19229 :   permpro = vecsmallpermute(perm, permpro);
     649       19229 :   for (i=1; i<=lig; i++) perm[i] = permpro[i]; /* perm o= permpro */
     650             : 
     651       19229 :   *ptdep  = rowslice(extramat, 1, nlze);
     652       19229 :   matb    = rowslice(extramat, nlze+1, lig);
     653       19229 :   if (DEBUGLEVEL>5) err_printf("    2nd phase done\n");
     654       19229 :   H = hnffinal(matb,perm,ptdep,ptB,&Cnew);
     655       19229 :   *ptC = shallowconcat(vecslice(C, 1, col-lH), Cnew);
     656       19229 :   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    23614113 : ZC_elem(GEN aj, GEN ak, GEN A, GEN U, long j, long k)
     672             : {
     673             :   GEN p1,u,v,d;
     674             : 
     675    23614113 :   if (!signe(ak)) {
     676       17336 :     swap(gel(A,j), gel(A,k));
     677       17336 :     if (U) swap(gel(U,j), gel(U,k));
     678    20536303 :     return;
     679             :   }
     680    23596777 :   d = bezout(aj,ak,&u,&v);
     681             :   /* frequent special case (u,v) = (1,0) or (0,1) */
     682    23596777 :   if (!signe(u))
     683             :   { /* ak | aj */
     684    10288438 :     p1 = diviiexact(aj,ak); togglesign(p1);
     685    10288438 :     ZC_lincomb1_inplace(gel(A,j), gel(A,k), p1);
     686    10288438 :     if (U)
     687      459150 :       ZC_lincomb1_inplace(gel(U,j), gel(U,k), p1);
     688    10288438 :     return;
     689             :   }
     690    13308339 :   if (!signe(v))
     691             :   { /* aj | ak */
     692    10213193 :     p1 = diviiexact(ak,aj); togglesign(p1);
     693    10213193 :     ZC_lincomb1_inplace(gel(A,k), gel(A,j), p1);
     694    10213193 :     swap(gel(A,j), gel(A,k));
     695    10213193 :     if (U) {
     696       49168 :       ZC_lincomb1_inplace(gel(U,k), gel(U,j), p1);
     697       49168 :       swap(gel(U,j), gel(U,k));
     698             :     }
     699    10213193 :     return;
     700             :   }
     701             : 
     702     3095146 :   if (!is_pm1(d)) { aj = diviiexact(aj, d); ak = diviiexact(ak, d); }
     703     3095146 :   p1 = gel(A,k); aj = negi(aj); /* NOT togglesign */
     704     3095146 :   gel(A,k) = ZC_lincomb(u,v, gel(A,j),p1);
     705     3095146 :   gel(A,j) = ZC_lincomb(aj,ak, p1,gel(A,j));
     706     3095146 :   if (U)
     707             :   {
     708      152364 :     p1 = gel(U,k);
     709      152364 :     gel(U,k) = ZC_lincomb(u,v, gel(U,j),p1);
     710      152364 :     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      237004 : col_mul(GEN x, GEN c)
     753             : {
     754      237004 :   if (typ(x) == t_INT)
     755             :   {
     756      237004 :     long s = signe(x);
     757      237004 :     if (!s) return NULL;
     758      178061 :     if (is_pm1(x)) return (s > 0)? c: RgC_neg(c);
     759             :   }
     760       32196 :   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       59251 : update(GEN u, GEN v, GEN a, GEN b, GEN *c1, GEN *c2)
     772             : {
     773             :   GEN p1,p2;
     774             : 
     775       59251 :   u = col_mul(u,*c1);
     776       59251 :   v = col_mul(v,*c2);
     777       59251 :   if (u) p1 = v? gadd(u,v): u;
     778         301 :   else   p1 = v? v: NULL;
     779             : 
     780       59251 :   a = col_mul(a,*c2);
     781       59251 :   b = col_mul(gneg_i(b),*c1);
     782       59251 :   if (a) p2 = b? RgC_add(a,b): a;
     783           0 :   else   p2 = b? b: NULL;
     784             : 
     785       59251 :   if (!p1) do_zero(*c1); else *c1 = p1;
     786       59251 :   if (!p2) do_zero(*c2); else *c2 = p2;
     787       59251 : }
     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     1351259 : ZM_reduce(GEN A, GEN U, long i, long j0)
     811             : {
     812     1351259 :   long j, lA = lg(A);
     813     1351259 :   GEN d = gcoeff(A,i,j0);
     814     1351259 :   if (signe(d) < 0)
     815             :   {
     816        4091 :     ZV_neg_inplace(gel(A,j0));
     817        4091 :     if (U) ZV_togglesign(gel(U,j0));
     818        4091 :     d = gcoeff(A,i,j0);
     819             :   }
     820     3494055 :   for (j=j0+1; j<lA; j++)
     821             :   {
     822     2142796 :     GEN q = truedivii(gcoeff(A,i,j), d);
     823     2142796 :     if (!signe(q)) continue;
     824             : 
     825      111206 :     togglesign(q);
     826      111206 :     ZC_lincomb1_inplace(gel(A,j), gel(A,j0), q);
     827      111206 :     if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,j0), q);
     828             :   }
     829     1351259 : }
     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      132041 : hnfmerge_get_1(GEN A, GEN B)
     876             : {
     877      132041 :   pari_sp av = avma;
     878      132041 :   long j, k, l = lg(A), lb;
     879      132041 :   GEN b, U = cgetg(l + 1, t_MAT), C = cgetg(l + 1, t_VEC);
     880             : 
     881      132041 :   b = gcoeff(B,1,1); lb = lgefint(b);
     882      265861 :   for (j = 1; j < l; j++)
     883             :   {
     884             :     GEN t;
     885      265861 :     long c = j+1;
     886      265861 :     gel(U,j) = col_ei(l-1, j);
     887      265861 :     gel(U,c) = zerocol(l-1); /* dummy */
     888      265861 :     gel(C,j) = vecslice(gel(A,j), 1,j);
     889      265861 :     gel(C,c) = vecslice(gel(B,j), 1,j);
     890      766590 :     for (k = j; k > 0; k--)
     891             :     {
     892      500729 :       t = gcoeff(C,k,c);
     893      500729 :       if (gequal0(t)) continue;
     894      436713 :       setlg(C[c], k+1);
     895      436713 :       ZC_elem(t, gcoeff(C,k,k), C, U, c, k);
     896      436713 :       if (lgefint(gcoeff(C,k,k)) > lb) gel(C,k) = FpC_red(gel(C,k), b);
     897      436713 :       if (j > 4)
     898             :       {
     899       47544 :         GEN u = gel(U,k);
     900             :         long h;
     901      647689 :         for (h=1; h<l; h++)
     902      600145 :           if (lgefint(gel(u,h)) > lb) gel(u,h) = remii(gel(u,h), b);
     903             :       }
     904             :     }
     905      265861 :     if (j == 1)
     906      132041 :       t = gcoeff(C,1,1);
     907             :     else
     908             :     {
     909             :       GEN u;
     910      133820 :       t = bezout(gcoeff(C,1,1), b, &u, NULL); /* >= 0 */
     911      133820 :       if (signe(u) && !equali1(u)) gel(U,1) = ZC_Z_mul(gel(U,1), u);
     912      133820 :       gcoeff(C,1,1) = t;
     913             :     }
     914      265861 :     if (equali1(t)) break;
     915             :   }
     916      132041 :   if (j >= l) return NULL;
     917      132041 :   b = lcmii(gcoeff(A,1,1),b);
     918      132041 :   A = FpC_red(ZM_ZC_mul(A,gel(U,1)), b);
     919      132041 :   return gerepileupto(av, FpC_center(A, b, shifti(b,-1)));
     920             : }
     921             : 
     922             : /* remove the first r columns */
     923             : static void
     924       48534 : remove_0cols(long r, GEN *pA, GEN *pB, long remove)
     925             : {
     926       48534 :   GEN A = *pA, B = *pB;
     927       48534 :   long l = lg(A);
     928       48534 :   A += r; A[0] = evaltyp(t_MAT) | evallg(l-r);
     929       48534 :   if (B && remove == 2) { B += r; B[0] = A[0]; }
     930       48534 :   *pA = A; *pB = B;
     931       48534 : }
     932             : 
     933             : /* Inefficient compared to hnfall. 'remove' = throw away lin.dep columns */
     934             : static GEN
     935       12450 : hnf_i(GEN A, int remove)
     936             : {
     937       12450 :   pari_sp av0 = avma, av;
     938             :   long s, n, m, j, k, li, def, ldef;
     939             : 
     940       12450 :   RgM_dimensions(A, &m, &n);
     941       12450 :   if (!n) return cgetg(1,t_MAT);
     942       12401 :   av = avma;
     943       12401 :   A = RgM_shallowcopy(A);
     944       12401 :   def = n; ldef = (m>n)? m-n: 0;
     945       44206 :   for (li=m; li>ldef; li--)
     946             :   {
     947       81589 :     for (j=def-1; j; j--)
     948             :     {
     949       49784 :       GEN a = gcoeff(A,li,j);
     950       49784 :       if (!signe(a)) continue;
     951             : 
     952             :       /* zero a = Aij  using  b = Aik */
     953       25912 :       k = (j==1)? def: j-1;
     954       25912 :       ZC_elem(a,gcoeff(A,li,k), A,NULL, j,k);
     955       25912 :       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       31805 :     s = signe(gcoeff(A,li,def));
     962       31805 :     if (s)
     963             :     {
     964       31105 :       if (s < 0) ZV_neg_inplace(gel(A,def));
     965       31105 :       ZM_reduce(A, NULL, li,def);
     966       31105 :       def--;
     967             :     }
     968             :     else
     969         700 :       if (ldef) ldef--;
     970       31805 :     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       12401 :   if (remove) { GEN B = NULL; remove_0cols(def, &A, &B, remove); }
     978       12401 :   return gerepileupto(av0, ZM_copy(A));
     979             : }
     980             : 
     981             : GEN
     982       15453 : 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     2666689 : FpV_Fp_mul_part_ip(GEN z, GEN u, GEN p, long k)
     987             : {
     988             :   long i;
     989     2666689 :   if (is_pm1(u)) {
     990      206853 :     if (signe(u) > 0) {
     991      863245 :       for (i = 1; i <= k; i++)
     992      673749 :         if (signe(gel(z,i))) gel(z,i) = modii(gel(z,i), p);
     993             :     } else {
     994       50044 :       for (i = 1; i <= k; i++)
     995       32687 :         if (signe(gel(z,i))) gel(z,i) = modii(negi(gel(z,i)), p);
     996             :     }
     997             :   }
     998             :   else {
     999     8703267 :     for (i = 1; i <= k; i++)
    1000     6243431 :       if (signe(gel(z,i))) gel(z,i) = Fp_mul(u,gel(z,i), p);
    1001             :   }
    1002     2666689 : }
    1003             : static void
    1004    21860606 : FpV_red_part_ipvec(GEN z, GEN p, long k)
    1005             : {
    1006             :   long i;
    1007    21860606 :   for (i = 1; i <= k; i++) gel(z,i) = modii(gel(z,i), gel(p,i));
    1008    21860606 : }
    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       48346 : ZpM_echelon(GEN x, long early_abort, GEN p, GEN pm)
    1014             : {
    1015       48346 :   pari_sp av0 = avma, av;
    1016             :   long m, li, co, i, j, k, def, ldef;
    1017             : 
    1018       48346 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1019       48346 :   li = lgcols(x);
    1020       48346 :   av = avma;
    1021       48346 :   x = RgM_shallowcopy(x);
    1022       48346 :   m = Z_pval(pm, p);
    1023             : 
    1024       48346 :   ldef = (li > co)? li - co: 0;
    1025      432838 :   for (def = co-1,i = li-1; i > ldef; i--)
    1026             :   {
    1027      385011 :     long vmin = LONG_MAX, kmin = 0;
    1028      385011 :     GEN umin = gen_0, pvmin, q;
    1029     3484123 :     for (k = 1; k <= def; k++)
    1030             :     {
    1031     3175267 :       GEN u = gcoeff(x,i,k);
    1032             :       long v;
    1033     3175267 :       if (!signe(u)) continue;
    1034     1526636 :       v = Z_pvalrem(u, p, &u);
    1035     1526636 :       if (v >= m) gcoeff(x,i,k) = gen_0;
    1036      990875 :       else if (v < vmin) {
    1037      372472 :         vmin = v; kmin = k; umin = u;
    1038      372472 :         if (!vmin) break;
    1039             :       }
    1040             :     }
    1041      385011 :     if (!kmin)
    1042             :     {
    1043      156236 :       if (early_abort) return NULL;
    1044      155717 :       gcoeff(x,i,def) = gen_0;
    1045      155717 :       ldef--;
    1046      155717 :       if (ldef < 0) ldef = 0;
    1047      155717 :       continue;
    1048             :     }
    1049      228775 :     if (kmin != def) swap(gel(x,def), gel(x,kmin));
    1050      228775 :     q = vmin? powiu(p, m-vmin): pm;
    1051             :     /* pivot has valuation vmin */
    1052      228775 :     umin = modii(umin, q);
    1053      228775 :     if (!equali1(umin))
    1054      177896 :       FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(umin,q), pm, i-1);
    1055      228775 :     gcoeff(x, i, def) = pvmin = powiu(p, vmin);
    1056     2615469 :     for (j = def-1; j; j--)
    1057             :     { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
    1058     2386694 :       GEN t, a = gcoeff(x,i,j) = modii(gcoeff(x,i,j), pm);
    1059     2386694 :       if (!signe(a)) continue;
    1060             : 
    1061     1171283 :       t = diviiexact(a, pvmin); togglesign(t);
    1062     1171283 :       ZC_lincomb1_inplace(gel(x,j), gel(x,def), t);
    1063     1171283 :       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      228775 :     def--;
    1070             :   }
    1071       47827 :   if (co > li)
    1072             :   {
    1073           0 :     x += co - li;
    1074           0 :     x[0] = evaltyp(t_MAT) | evallg(li);
    1075             :   }
    1076       47827 :   return gerepilecopy(av0, x);
    1077             : }
    1078             : GEN
    1079      797188 : zlm_echelon(GEN x, long early_abort, ulong p, ulong pm)
    1080             : {
    1081      797188 :   pari_sp av0 = avma;
    1082             :   long li, co, i, j, k, def, ldef;
    1083             :   ulong m;
    1084             : 
    1085      797188 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1086      797188 :   li = lgcols(x);
    1087      797188 :   x = Flm_copy(x);
    1088      797188 :   m = u_lval(pm, p);
    1089             : 
    1090      797188 :   ldef = (li > co)? li - co: 0;
    1091     4050849 :   for (def = co-1,i = li-1; i > ldef; i--)
    1092             :   {
    1093     3311472 :     long vmin = LONG_MAX, kmin = 0;
    1094     3311472 :     ulong umin = 0, pvmin, q;
    1095    12727240 :     for (k = 1; k <= def; k++)
    1096             :     {
    1097    11316881 :       ulong u = ucoeff(x,i,k);
    1098             :       long v;
    1099    11316881 :       if (!u) continue;
    1100     5263225 :       v = u_lvalrem(u, p, &u);
    1101     5263225 :       if (v >= (long) m) ucoeff(x,i,k) = 0;
    1102     5263225 :       else if (v < vmin) {
    1103     3316651 :         vmin = v; kmin = k; umin = u;
    1104     3316651 :         if (!vmin) break;
    1105             :       }
    1106             :     }
    1107     3311472 :     if (!kmin)
    1108             :     {
    1109      238185 :       if (early_abort) return NULL;
    1110      180374 :       ucoeff(x,i,def) = 0;
    1111      180374 :       ldef--;
    1112      180374 :       if (ldef < 0) ldef = 0;
    1113      180374 :       continue;
    1114             :     }
    1115     3073287 :     if (kmin != def) swap(gel(x,def), gel(x,kmin));
    1116     3073287 :     q = vmin? upowuu(p, m-vmin): pm;
    1117             :     /* pivot has valuation vmin */
    1118     3073287 :     umin %= q;
    1119     3073287 :     if (umin != 1)
    1120     1577143 :       Flv_Fl_mul_part_inplace(gel(x,def), Fl_inv(umin,q), pm, i-1);
    1121     3073287 :     ucoeff(x, i, def) = pvmin = upowuu(p, vmin);
    1122    18666087 :     for (j = def-1; j; j--)
    1123             :     { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
    1124    15592800 :       ulong t, a = ucoeff(x,i,j);
    1125    15592800 :       if (!a) continue;
    1126             : 
    1127     8657914 :       t = Fl_neg(a / pvmin, q);
    1128     8657914 :       Flc_lincomb1_inplace(gel(x,j), gel(x,def), t, pm);
    1129             :     }
    1130     3073287 :     def--;
    1131             :   }
    1132      739377 :   if (co > li)
    1133             :   {
    1134           0 :     x += co - li;
    1135           0 :     x[0] = evaltyp(t_MAT) | evallg(li);
    1136             :   }
    1137      739377 :   return gerepilecopy(av0, x);
    1138             : }
    1139             : 
    1140             : static int
    1141       14763 : ZV_allequal(GEN v)
    1142             : {
    1143       14763 :   long i, l = lg(v);
    1144       14763 :   if (l > 1)
    1145             :   {
    1146       14763 :     GEN x = gel(v,1);
    1147       14763 :     for (i = 2; i < l; i++) if (!equalii(x,gel(v,i))) return 0;
    1148             :   }
    1149        7623 :   return 1;
    1150             : }
    1151             : /* compute optimal D for hnfmod: x upper triangular */
    1152             : static GEN
    1153     3284431 : optimal_D(GEN x, GEN D)
    1154             : {
    1155     3284431 :   long i, n = nbrows(x);
    1156     3284431 :   GEN C = shallowcopy(D);
    1157     3284431 :   gel(C,1) = gcoeff(x,1,1);
    1158     3703486 :   for (i = 2; i < n; i++)
    1159             :   {
    1160     1257223 :     GEN c = mulii(gel(C,i-1), gcoeff(x,i,i));
    1161     1257223 :     if (signe(c) < 0) togglesign(c);
    1162     1257223 :     if (cmpii(c, gel(D,i)) >= 0) break;
    1163      419055 :     gel(C,i) = c;
    1164             :   }
    1165     3284431 :   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     3284494 : ZM_hnfmodall_i(GEN x, GEN D, long flag)
    1175             : {
    1176             :   pari_sp av;
    1177     3284494 :   const long center = (flag & hnf_CENTER);
    1178             :   long moddiag, modsame, nli, li, co, i, j, k, def, ldef;
    1179             :   GEN u, LDM;
    1180             : 
    1181     3284494 :   co = lg(x);
    1182     3284494 :   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     3284452 :   li = lgcols(x);
    1189     3284452 :   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     3284438 :   nli = li - 1;
    1195     3284438 :   modsame = typ(D)==t_INT;
    1196     3284438 :   if (!modsame)
    1197             :   {
    1198       14763 :     if (lg(D) != li) pari_err_DIM("ZM_hnfmod");
    1199       14763 :     if (ZV_allequal(D)) { modsame = 1; D = gel(D,1); }
    1200             :   }
    1201     3284438 :   moddiag = (flag & hnf_MODID) || !modsame;
    1202             :   /* modsame: triangularize mod fixed d*Id;
    1203             :    * moddiag: modulo diagonal matrix, else modulo multiple of determinant */
    1204             : 
    1205     3284438 :   if (modsame)
    1206             :   {
    1207     3277298 :     LDM = const_vecsmall(nli, 2*lgefint(D)-2);
    1208     3277298 :     D = const_vec(nli,D);
    1209             :   }
    1210             :   else
    1211             :   {
    1212        7140 :     LDM = cgetg(li, t_VECSMALL);
    1213        7140 :     for (i=1; i<li; i++) LDM[i] = lgefint(gel(D,i));
    1214             :   }
    1215     3284438 :   av = avma;
    1216     3284438 :   x = RgM_shallowcopy(x);
    1217             : 
    1218     3284438 :   ldef = 0;
    1219     3284438 :   if (li > co)
    1220             :   {
    1221       48902 :     ldef = li - co;
    1222       48902 :     if (!moddiag)
    1223           7 :       pari_err_DOMAIN("ZM_hnfmod","nb lines",">", strtoGENstr("nb columns"), x);
    1224             :   }
    1225    12271314 :   for (def = co-1,i = nli; i > ldef; i--,def--)
    1226             :   {
    1227     8986883 :     GEN d = gel(D,i);
    1228     8986883 :     long add_N = modsame;
    1229    41372151 :     for (j = 1; j < def; j++)
    1230             :     {
    1231    32385268 :       GEN p1, p2, b, a = gcoeff(x,i,j) = remii(gcoeff(x,i,j), d);
    1232    51018816 :       if (!signe(a)) continue;
    1233             : 
    1234    21515766 :       k = j+1;
    1235    21515766 :       b = gcoeff(x,i,k) = remii(gcoeff(x,i,k), d);
    1236    21515766 :       if (!signe(b)) { swap(gel(x,j), gel(x,k)); continue; }
    1237    13751720 :       if (add_N)
    1238             :       { /* ensure the moving pivot on row i divides d from now on */
    1239     4556902 :         add_N = 0;
    1240     4556902 :         if (!equali1(a))
    1241             :         { /* x[j] *= u; after this, a = x[i,j] | d */
    1242     3894053 :           GEN u = Fp_invgen(a, d, &a);
    1243             :           long t;
    1244     3894053 :           p1 = gel(x,j);
    1245     3894053 :           for (t = 1; t < i; t++) gel(p1,t) = mulii(gel(p1,t), u);
    1246     3894053 :           FpV_red_part_ipvec(p1, D, i-1);
    1247     3894053 :           gel(p1,i) = a;
    1248     3894053 :           if (2*lg(a) < lg(b))
    1249             :           { /* reduce x[i,k] mod x[i,j]: helps ZC_elem */
    1250        8571 :             GEN r, q = dvmdii(b, a, &r);
    1251        8571 :             togglesign(q);
    1252        8571 :             ZC_lincomb1_inplace_i(gel(x,k), gel(x,j), q, i-1);
    1253        8571 :             FpV_red_part_ipvec(gel(x,k), D, i-1);
    1254        8571 :             gcoeff(x,i,k) = b = r;
    1255             :           }
    1256             :         }
    1257             :       }
    1258    13751720 :       ZC_elem(a,b, x, NULL, j,k);
    1259    13751720 :       p1 = gel(x,j);
    1260    13751720 :       p2 = gel(x,k);
    1261             :       /* prevent coeffs explosion */
    1262    61210000 :       for (k = 1; k < i; k++)
    1263             :       {
    1264    47458280 :         if (lgefint(gel(p1,k)) > LDM[k]) gel(p1,k) = remii(gel(p1,k),gel(D,k));
    1265    47458280 :         if (lgefint(gel(p2,k)) > LDM[k]) gel(p2,k) = remii(gel(p2,k),gel(D,k));
    1266             :       }
    1267             :     }
    1268     8986883 :     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     8986883 :     if (moddiag && !signe(gcoeff(x,i,def)))
    1274             :     { /* missing pivot on line i, insert column */
    1275      577644 :       GEN a = cgetg(co + 1, t_MAT);
    1276      577644 :       for (k = 1; k <= def; k++) gel(a,k) = gel(x,k);
    1277      577644 :       gel(a,k++) = Rg_col_ei(gel(D,i), nli, i);
    1278      577644 :       for (     ; k <= co;  k++) gel(a,k) = gel(x,k-1);
    1279      577644 :       ldef--; if (ldef < 0) ldef = 0;
    1280      577644 :       co++; def++; x = a;
    1281             :     }
    1282             :   }
    1283     3284431 :   if (co < li)
    1284             :   { /* implies moddiag, add missing diag(D) components */
    1285       29582 :     GEN a = cgetg(li+1, t_MAT);
    1286       29582 :     for (k = 1; k <= li-co; k++) gel(a,k) = Rg_col_ei(gel(D,k), nli, k);
    1287       29582 :     for (i = 1; i < co; i++) gel(a,k-1+i) = gel(x,i);
    1288       29582 :     gel(a,li) = zerocol(nli); x = a;
    1289             :   }
    1290             :   else
    1291             :   {
    1292     3254849 :     x += co - li;
    1293     3254849 :     x[0] = evaltyp(t_MAT) | evallg(li); /* kill 0 columns */
    1294     3254849 :     if (moddiag) x = shallowconcat(x, zerocol(nli));
    1295             :   }
    1296     3284431 :   if (moddiag)
    1297             :   { /* x[li]: extra column, an accumulator discarded at the end */
    1298             :     GEN D2;
    1299     3171785 :     gcoeff(x,1,1) = gcdii(gcoeff(x,1,1), gel(D,1));
    1300     3171785 :     D2 = optimal_D(x,D);
    1301             :     /* add up missing diag(D) components */
    1302    11873077 :     for (i = nli; i > 0; i--)
    1303             :     {
    1304     8701292 :       gcoeff(x, i, li) = gel(D,i);
    1305    31202151 :       for (j = i; j > 0; j--)
    1306             :       {
    1307    22500859 :         GEN a = gcoeff(x, j, li);
    1308    22500859 :         if (!signe(a)) continue;
    1309     8978991 :         ZC_elem(a, gcoeff(x,j,j), x, NULL, li,j);
    1310     8978991 :         FpV_red_part_ipvec(gel(x,li), D, j-1);
    1311     8978991 :         FpV_red_part_ipvec(gel(x,j),  D, j-1);
    1312             :       }
    1313     8701292 :       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     3171785 :     D = D2;
    1320             :   }
    1321             :   else
    1322             :   {
    1323      112646 :     GEN b = gel(D,1);
    1324      462700 :     for (i = nli; i > 0; i--)
    1325             :     {
    1326      350054 :       GEN d = bezout(gcoeff(x,i,i),b, &u,NULL);
    1327      350054 :       gcoeff(x,i,i) = d;
    1328      350054 :       FpV_Fp_mul_part_ip(gel(x,i), u, b, i-1);
    1329      350054 :       if (i > 1) b = diviiexact(b,d);
    1330             :     }
    1331      112646 :     D = optimal_D(x,D);
    1332             :   }
    1333     3284431 :   x[0] = evaltyp(t_MAT) | evallg(li); /* kill 0 columns / discard accumulator */
    1334     3284431 :   if (flag & hnf_PART) return x;
    1335             : 
    1336    12335777 :   for (i = nli; i > 0; i--)
    1337             :   {
    1338     9051346 :     GEN diag = gcoeff(x,i,i);
    1339     9051346 :     if (signe(diag) < 0) { gel(x,i) = ZC_neg(gel(x,i)); diag = gcoeff(x,i,i); }
    1340     9051346 :     if (i != nli)
    1341     5766915 :       for (j = 1; j < i; j++) gcoeff(x,j,i) = remii(gcoeff(x,j,i), gel(D,j));
    1342    23619334 :     for (j = i+1; j < li; j++)
    1343             :     {
    1344    14567988 :       GEN b = gcoeff(x,i,j) = remii(gcoeff(x,i,j), gel(D,i));
    1345    14567988 :       GEN r, q = truedvmdii(b, diag, &r);
    1346             :       /* ensure -diag/2 <= r < diag/2 */
    1347    14567988 :       if (center && signe(r) && abscmpii(shifti(r,1),diag) >= 0)
    1348      165858 :       { r = subii(r,diag); q = addiu(q,1); }
    1349    14567988 :       if (!signe(q)) continue;
    1350     5532669 :       togglesign(q);
    1351     5532669 :       ZC_lincomb1_inplace_i(gel(x,j), gel(x,i), q, i-1);
    1352     5532669 :       gcoeff(x,i,j) = r;
    1353             :     }
    1354     9051346 :     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     3284431 :   return x;
    1361             : }
    1362             : GEN
    1363     3280035 : ZM_hnfmodall(GEN x, GEN dm, long flag)
    1364             : {
    1365     3280035 :   pari_sp av = avma;
    1366     3280035 :   return gerepilecopy(av, ZM_hnfmodall_i(x, dm, flag));
    1367             : }
    1368             : GEN
    1369      112639 : ZM_hnfmod(GEN x, GEN d) { return ZM_hnfmodall(x,d,0); }
    1370             : GEN
    1371     2925411 : ZM_hnfmodid(GEN x, GEN d)
    1372     2925411 : { 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     1582025 : 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     1582025 :   co = lg(x); if (co == 1) { *pP = cgetg(1,t_VECSMALL); return cgetg(1,t_MAT); }
    1383     1582025 :   li = lgcols(x);
    1384     1582025 :   iP = 1;
    1385     1582025 :   *pP = P = cgetg(li, t_VECSMALL);
    1386     1582025 :   av = avma;
    1387     1582025 :   x = FpM_red(x, p);
    1388             : 
    1389     1582025 :   ldef = (li > co)? li - co: 0;
    1390     5643363 :   for (def = co-1,i = li-1; i > ldef; i--)
    1391             :   {
    1392     4061338 :     GEN u = NULL;
    1393     6288425 :     for (k = def; k; k--)
    1394             :     {
    1395     4602957 :       u = gcoeff(x,i,k);
    1396     4602957 :       if (signe(u)) break;
    1397             :     }
    1398     4061338 :     if (!k)
    1399             :     {
    1400     1685468 :       if (--ldef < 0) ldef = 0;
    1401     1685468 :       continue;
    1402             :     }
    1403     2375870 :     P[iP++] = i;
    1404     2375870 :     if (k != def) swap(gel(x,def), gel(x,k));
    1405     2375870 :     if (!equali1(u))
    1406     2138739 :       FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(u,p), p, i-1);
    1407     2375870 :     gcoeff(x, i, def) = gen_1;
    1408     7392567 :     for (j = def-1; j; j--)
    1409             :     { /* zero x[i, 1..def-1] using x[i,def] = 1*/
    1410     5016697 :       GEN xj = gel(x,j), u = gel(xj,i);
    1411     5016697 :       if (!signe(u)) continue;
    1412             : 
    1413     3407489 :       ZC_lincomb1_inplace(xj, gel(x,def), negi(u));
    1414     3407489 :       for (k = 1; k < i; k++) gel(xj,k) = modii(gel(xj,k), p);
    1415             :     }
    1416     2375870 :     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     2375870 :     def--;
    1422             :   }
    1423             :   /* rank = iP - 1 */
    1424     1582025 :   setlg(P, iP); vecsmall_sort(P);
    1425     1582025 :   if (co > iP) x += co - iP;
    1426     1582025 :   x[0] = evaltyp(t_MAT) | evallg(iP);
    1427     1582025 :   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     1582025 : FpM_hnfend(pari_sp av, GEN x, GEN p)
    1433             : {
    1434     1582025 :   long i, l = lgcols(x);
    1435     5643363 :   for (i = l-1; i > 0; i--)
    1436             :   {
    1437     4061338 :     GEN diag = gcoeff(x,i,i);
    1438             :     long j;
    1439     4061338 :     if (is_pm1(diag))
    1440     4649729 :       for (j = i+1; j < l; j++)
    1441             :       {
    1442     2273859 :         GEN xj = gel(x,j), b = gel(xj,i);
    1443             :         long k;
    1444     2273859 :         if (!signe(b)) continue;
    1445     1751243 :         ZC_lincomb1_inplace(xj, gel(x,i), negi(b));
    1446     7937024 :         for (k=1; k<i; k++)
    1447     6185781 :           if (lgefint(gel(xj,k)) > 3) gel(xj,k) = remii(gel(xj,k), p);
    1448             :       }
    1449             :     else
    1450     1685468 :       for (j = i+1; j < l; j++) gcoeff(x,i,j) = modii(gcoeff(x,i,j), p);
    1451     4061338 :     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     1582025 :   return x;
    1458             : }
    1459             : GEN
    1460     1582025 : ZM_hnfmodprime(GEN x, GEN p)
    1461             : {
    1462     1582025 :   pari_sp av = avma;
    1463             :   GEN P, y;
    1464             :   long l, lP, i;
    1465     1582025 :   if (lg(x) == 1) return cgetg(1, t_MAT);
    1466     1582025 :   l = lgcols(x);
    1467     1582025 :   x = FpM_echelon(x, &P, p);
    1468     1582025 :   lP = lg(P); /* rank = lP-1 */
    1469     1582025 :   if (lP == l) { avma = av; return matid(l-1); }
    1470     1582025 :   y = scalarmat_shallow(p, l-1);
    1471     1582025 :   for (i = 1; i < lP; i++) gel(y,P[i]) = gel(x,i);
    1472     1582025 :   return gerepilecopy(av, FpM_hnfend(av,y,p));
    1473             : }
    1474             : 
    1475             : static GEN
    1476      241138 : allhnfmod(GEN x, GEN dm, int flag)
    1477             : {
    1478      241138 :   if (typ(x)!=t_MAT) pari_err_TYPE("allhnfmod",x);
    1479      241138 :   RgM_check_ZM(x, "allhnfmod");
    1480      241138 :   if (isintzero(dm)) return ZM_hnf(x);
    1481      241138 :   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      241124 : hnfmodid(GEN x, GEN d)
    1491             : {
    1492      241124 :   switch(typ(d))
    1493             :   {
    1494      241103 :     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      241124 :   return allhnfmod(x, d, hnf_MODID);
    1500             : }
    1501             : 
    1502             : /* M a ZM in HNF. Normalize with *centered* residues */
    1503             : GEN
    1504        1673 : ZM_hnfcenter(GEN M)
    1505             : {
    1506        1673 :   long i, j, k, N = lg(M)-1;
    1507        1673 :   pari_sp av = avma;
    1508             : 
    1509        6237 :   for (j=N-1; j>0; j--) /* skip last line */
    1510             :   {
    1511        4564 :     GEN Mj = gel(M,j), a = gel(Mj,j);
    1512       24934 :     for (k = j+1; k <= N; k++)
    1513             :     {
    1514       20370 :       GEN Mk = gel(M,k), q = diviiround(gel(Mk,j), a);
    1515       20370 :       long s = signe(q);
    1516       20370 :       if (!s) continue;
    1517       13160 :       if (is_pm1(q))
    1518             :       {
    1519        2674 :         if (s < 0)
    1520         364 :           for (i = 1; i <= j; i++) gel(Mk,i) = addii(gel(Mk,i), gel(Mj,i));
    1521             :         else
    1522        2310 :           for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), gel(Mj,i));
    1523             :       }
    1524             :       else
    1525       10486 :         for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), mulii(q,gel(Mj,i)));
    1526       13160 :       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        1673 :   return M;
    1534             : }
    1535             : 
    1536             : /***********************************************************************/
    1537             : /*                                                                     */
    1538             : /*                 HNFLLL (Havas, Majewski, Mathews)                   */
    1539             : /*                                                                     */
    1540             : /***********************************************************************/
    1541             : 
    1542             : static void
    1543      545132 : Minus(long j, GEN lambda)
    1544             : {
    1545      545132 :   long k, n = lg(lambda);
    1546             : 
    1547      545132 :   for (k=1  ; k<j; k++) togglesign_safe(&gcoeff(lambda,k,j));
    1548      545132 :   for (k=j+1; k<n; k++) togglesign_safe(&gcoeff(lambda,j,k));
    1549      545132 : }
    1550             : 
    1551             : /* index of first non-zero entry */
    1552             : static long
    1553    41151133 : findi(GEN M)
    1554             : {
    1555    41151133 :   long i, n = lg(M);
    1556   420733645 :   for (i=1; i<n; i++)
    1557   400120209 :     if (signe(gel(M,i))) return i;
    1558    20613436 :   return 0;
    1559             : }
    1560             : 
    1561             : static long
    1562    41151133 : findi_normalize(GEN Aj, GEN B, long j, GEN lambda)
    1563             : {
    1564    41151133 :   long r = findi(Aj);
    1565    41151133 :   if (r && signe(gel(Aj,r)) < 0)
    1566             :   {
    1567      545132 :     ZV_togglesign(Aj); if (B) ZV_togglesign(gel(B,j));
    1568      545132 :     Minus(j,lambda);
    1569             :   }
    1570    41151133 :   return r;
    1571             : }
    1572             : 
    1573             : static void
    1574    20575112 : 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    20575112 :   *row0 = findi_normalize(gel(A,j), B,j,lambda);
    1580    20575112 :   *row1 = findi_normalize(gel(A,k), B,k,lambda);
    1581    20575112 :   if (*row0)
    1582     7780328 :     q = truedivii(gcoeff(A,*row0,k), gcoeff(A,*row0,j));
    1583    12794784 :   else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
    1584     3030536 :     q = diviiround(gcoeff(lambda,j,k), gel(D,j));
    1585             :   else
    1586     9764248 :     return;
    1587             : 
    1588    10810864 :   if (signe(q))
    1589             :   {
    1590     5874899 :     GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
    1591     5874899 :     togglesign_safe(&q);
    1592     5874899 :     if (*row0) ZC_lincomb1_inplace(gel(A,k),gel(A,j),q);
    1593     5874899 :     if (B) ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
    1594     5874899 :     gel(Lk,j) = addmulii(gel(Lk,j), q, gel(D,j));
    1595     5874899 :     if (is_pm1(q))
    1596             :     {
    1597     1820045 :       if (signe(q) > 0)
    1598             :       {
    1599     6706772 :         for (i=1; i<j; i++)
    1600     6034939 :           if (signe(gel(Lj,i))) gel(Lk,i) = addii(gel(Lk,i), gel(Lj,i));
    1601             :       }
    1602             :       else
    1603             :       {
    1604     8014044 :         for (i=1; i<j; i++)
    1605     6865832 :           if (signe(gel(Lj,i))) gel(Lk,i) = subii(gel(Lk,i), gel(Lj,i));
    1606             :       }
    1607             :     }
    1608             :     else
    1609             :     {
    1610    23989790 :       for (i=1; i<j; i++)
    1611    19934936 :         if (signe(gel(Lj,i))) gel(Lk,i) = addmulii(gel(Lk,i), q, gel(Lj,i));
    1612             :     }
    1613             :   }
    1614             : }
    1615             : 
    1616             : static void
    1617     2898051 : hnfswap(GEN A, GEN B, long k, GEN lambda, GEN D)
    1618             : {
    1619     2898051 :   GEN t, p1, p2, Lk = gel(lambda,k);
    1620     2898051 :   long i,j,n = lg(A);
    1621             : 
    1622     2898051 :   swap(gel(A,k), gel(A,k-1));
    1623     2898051 :   if (B) swap(gel(B,k), gel(B,k-1));
    1624     2898051 :   for (j=k-2; j; j--) swap(gcoeff(lambda,j,k-1), gel(Lk,j));
    1625    39042341 :   for (i=k+1; i<n; i++)
    1626             :   {
    1627    36144290 :     GEN Li = gel(lambda,i);
    1628    36144290 :     p1 = mulii(gel(Li,k-1), gel(D,k));
    1629    36144290 :     p2 = mulii(gel(Li,k), gel(Lk,k-1));
    1630    36144290 :     t = subii(p1,p2);
    1631             : 
    1632    36144290 :     p1 = mulii(gel(Li,k), gel(D,k-2));
    1633    36144290 :     p2 = mulii(gel(Li,k-1), gel(Lk,k-1));
    1634    36144290 :     gel(Li,k-1) = diviiexact(addii(p1,p2), gel(D,k-1));
    1635    36144290 :     gel(Li,k) = diviiexact(t, gel(D,k-1));
    1636             :   }
    1637     2898051 :   p1 = mulii(gel(D,k-2), gel(D,k));
    1638     2898051 :   p2 = sqri(gel(Lk,k-1));
    1639     2898051 :   gel(D,k-1) = diviiexact(addii(p1,p2), gel(D,k-1));
    1640     2898051 : }
    1641             : 
    1642             : /* reverse row order in matrix A, IN PLACE */
    1643             : static GEN
    1644      106186 : reverse_rows(GEN A)
    1645             : {
    1646      106186 :   long i, j, h, n = lg(A);
    1647      106186 :   if (n == 1) return A;
    1648      106130 :   h = lgcols(A);
    1649      907188 :   for (j=1; j<n; j++)
    1650             :   {
    1651      801058 :     GEN c = gel(A,j);
    1652             :     /* start at (h-1) >>1 : if h = 2i even, no need to swap c[i] and itself */
    1653      801058 :     for (i=(h-1)>>1; i; i--) swap(gel(c,i), gel(c,h-i));
    1654             :   }
    1655      106130 :   return A;
    1656             : }
    1657             : /* decide whether to swap */
    1658             : static int
    1659     1430226 : must_swap(long k, GEN lambda, GEN D)
    1660             : {
    1661     1430226 :   pari_sp av = avma;
    1662     1430226 :   GEN z = addii(mulii(gel(D,k-2),gel(D,k)), sqri(gcoeff(lambda,k-1,k)));
    1663     1430226 :   long s = cmpii(z, sqri(gel(D,k-1)));
    1664     1430226 :   avma = av; return s < 0;
    1665             : }
    1666             : 
    1667             : GEN
    1668       53093 : ZM_hnflll(GEN A, GEN *ptB, int remove)
    1669             : {
    1670       53093 :   pari_sp av = avma;
    1671             :   long n, k, kmax;
    1672             :   GEN B, lambda, D;
    1673             : 
    1674       53093 :   n = lg(A);
    1675       53093 :   A = reverse_rows(ZM_copy(A)); /* ZM_copy for in place findi_normalize() */
    1676       53093 :   B = ptB? matid(n-1): NULL;
    1677       53093 :   D = const_vec(n, gen_1) + 1;
    1678       53093 :   lambda = zeromatcopy(n-1,n-1);
    1679       53093 :   k = kmax = 2;
    1680     5011959 :   while (k < n)
    1681             :   {
    1682             :     long row0, row1;
    1683             :     int do_swap;
    1684     4905773 :     reduce2(A,B,k,k-1,&row0,&row1,lambda,D);
    1685     4905773 :     if      (row0) do_swap = (!row1 || row0 <= row1);
    1686     1612799 :     else if (row1) do_swap = 0;
    1687     1369390 :     else do_swap = must_swap(k,lambda,D);
    1688     4905773 :     if (do_swap)
    1689             :     {
    1690     2837118 :       hnfswap(A,B,k,lambda,D);
    1691     2837118 :       if (k > 2) k--;
    1692             :     }
    1693             :     else
    1694             :     {
    1695             :       long i;
    1696    17737994 :       for (i=k-2; i; i--)
    1697             :       {
    1698             :         long row0, row1;
    1699    15669339 :         reduce2(A,B,k,i,&row0,&row1,lambda,D);
    1700    15669339 :         if (gc_needed(av,3))
    1701             :         {
    1702          14 :           GEN b = D-1;
    1703          14 :           if (DEBUGMEM>1) pari_warn(warnmem,"hnflll (reducing), kmax = %ld",kmax);
    1704          14 :           gerepileall(av, B? 4: 3, &A, &lambda, &b, &B);
    1705          14 :           D = b+1;
    1706             :         }
    1707             :       }
    1708     2068655 :       if (++k > kmax) kmax = k;
    1709             :     }
    1710     4905773 :     if (gc_needed(av,3))
    1711             :     {
    1712          94 :       GEN b = D-1;
    1713          94 :       if (DEBUGMEM>1) pari_warn(warnmem,"hnflll, kmax = %ld / %ld",kmax,n-1);
    1714          94 :       gerepileall(av, B? 4: 3, &A, &lambda, &b, &B);
    1715          94 :       D = b+1;
    1716             :     }
    1717             :   }
    1718             :   /* handle trivial case: return negative diag coefficient otherwise */
    1719       53093 :   if (n == 2) (void)findi_normalize(gel(A,1), B,1,lambda);
    1720       53093 :   A = reverse_rows(A);
    1721       53093 :   if (remove)
    1722             :   {
    1723             :     long i;
    1724       59393 :     for (i = 1; i < n; i++)
    1725       54115 :       if (!ZV_equal0(gel(A,i))) break;
    1726        9204 :     remove_0cols(i-1, &A, &B, remove);
    1727             :   }
    1728       53093 :   gerepileall(av, B? 2: 1, &A, &B);
    1729       53093 :   if (B) *ptB = B;
    1730       53093 :   return A;
    1731             : }
    1732             : 
    1733             : GEN
    1734           7 : hnflll(GEN x)
    1735             : {
    1736           7 :   GEN z = cgetg(3, t_VEC);
    1737           7 :   gel(z,1) = ZM_hnflll(x, &gel(z,2), 1);
    1738           7 :   return z;
    1739             : }
    1740             : 
    1741             : /* Variation on HNFLLL: Extended GCD */
    1742             : 
    1743             : static void
    1744      368207 : reduce1(GEN A, GEN B, long k, long j, GEN lambda, GEN D)
    1745             : {
    1746             :   GEN q;
    1747             :   long i;
    1748             : 
    1749      368207 :   if (signe(gel(A,j)))
    1750       55288 :     q = diviiround(gel(A,k),gel(A,j));
    1751      312919 :   else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
    1752        8360 :     q = diviiround(gcoeff(lambda,j,k), gel(D,j));
    1753             :   else
    1754      304559 :     return;
    1755             : 
    1756       63648 :   if (signe(q))
    1757             :   {
    1758       49559 :     GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
    1759       49559 :     togglesign_safe(&q);
    1760       49559 :     gel(A,k) = addmulii(gel(A,k), q, gel(A,j));
    1761       49559 :     ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
    1762       49559 :     gel(Lk,j) = addmulii(gel(Lk,j), q, gel(D,j));
    1763       73554 :     for (i=1; i<j; i++)
    1764       23995 :       if (signe(gel(Lj,i))) gel(Lk,i) = addmulii(gel(Lk,i), q, gel(Lj,i));
    1765             :   }
    1766             : }
    1767             : 
    1768             : static GEN
    1769       50932 : ZV_gcdext_i(GEN A)
    1770             : {
    1771       50932 :   long k, n = lg(A);
    1772             :   GEN B, lambda, D;
    1773             : 
    1774       50932 :   if (n == 1) retmkvec2(gen_1, cgetg(1,t_MAT));
    1775       50932 :   A = leafcopy(A);
    1776       50932 :   B = matid(n-1);
    1777       50932 :   lambda = zeromatcopy(n-1,n-1);
    1778       50932 :   D = const_vec(n, gen_1) + 1;
    1779       50932 :   k = 2;
    1780      291217 :   while (k < n)
    1781             :   {
    1782             :     int do_swap;
    1783             : 
    1784      189353 :     reduce1(A,B,k,k-1,lambda,D);
    1785      189353 :     if    (signe(gel(A,k-1))) do_swap = 1;
    1786      134065 :     else if (signe(gel(A,k))) do_swap = 0;
    1787       60836 :     else do_swap = must_swap(k,lambda,D);
    1788      189353 :     if (do_swap)
    1789             :     {
    1790       60933 :       hnfswap(A,B,k,lambda,D);
    1791       60933 :       if (k > 2) k--;
    1792             :     }
    1793             :     else
    1794             :     {
    1795             :       long i;
    1796      128420 :       for (i=k-2; i; i--) reduce1(A,B,k,i,lambda,D);
    1797      128420 :       k++;
    1798             :     }
    1799             :   }
    1800       50932 :   if (signe(gel(A,n-1)) < 0)
    1801             :   {
    1802        6689 :     gel(A,n-1) = negi(gel(A,n-1));
    1803        6689 :     ZV_togglesign(gel(B,n-1));
    1804             :   }
    1805       50932 :   return mkvec2(gel(A,n-1), B);
    1806             : }
    1807             : GEN
    1808       50918 : ZV_extgcd(GEN A)
    1809             : {
    1810       50918 :   pari_sp av = avma;
    1811       50918 :   return gerepilecopy(av, ZV_gcdext_i(A));
    1812             : }
    1813             : /* as ZV_extgcd, transforming the gcd into a t_MAT, for mathnf0 */
    1814             : static GEN
    1815          21 : ZV_hnfgcdext(GEN A)
    1816             : {
    1817          21 :   pari_sp av = avma;
    1818             :   GEN z;
    1819          21 :   if (lg(A) == 1) retmkvec2(cgetg(1,t_MAT),cgetg(1,t_MAT));
    1820          14 :   z = ZV_gcdext_i(A);
    1821          14 :   gel(z,1) = mkmat(mkcol(gel(z,1)));
    1822          14 :   return gerepilecopy(av, z);
    1823             : }
    1824             : 
    1825             : /* HNF with permutation. */
    1826             : GEN
    1827         259 : ZM_hnfperm(GEN A, GEN *ptU, GEN *ptperm)
    1828             : {
    1829             :   GEN U, c, l, perm, d, p, q, b;
    1830         259 :   pari_sp av = avma, av1;
    1831             :   long r, t, i, j, j1, k, m, n;
    1832             : 
    1833         259 :   n = lg(A)-1;
    1834         259 :   if (!n)
    1835             :   {
    1836           7 :     if (ptU) *ptU = cgetg(1,t_MAT);
    1837           7 :     if (ptperm) *ptperm = cgetg(1,t_VEC);
    1838           7 :     return cgetg(1, t_MAT);
    1839             :   }
    1840         252 :   m = nbrows(A);
    1841         252 :   c = zero_zv(m);
    1842         252 :   l = zero_zv(n);
    1843         252 :   perm = cgetg(m+1, t_VECSMALL);
    1844         252 :   av1 = avma;
    1845         252 :   A = RgM_shallowcopy(A);
    1846         252 :   U = ptU? matid(n): NULL;
    1847             :   /* U base change matrix : A0*U = A all along */
    1848        1281 :   for (r=0, k=1; k <= n; k++)
    1849             :   {
    1850        3283 :     for (j=1; j<k; j++)
    1851             :     {
    1852        2254 :       if (!l[j]) continue;
    1853        2121 :       t=l[j]; b=gcoeff(A,t,k);
    1854        2121 :       if (!signe(b)) continue;
    1855             : 
    1856         553 :       ZC_elem(b,gcoeff(A,t,j), A,U,k,j);
    1857         553 :       d = gcoeff(A,t,j);
    1858         553 :       if (signe(d) < 0)
    1859             :       {
    1860           0 :         ZV_neg_inplace(gel(A,j));
    1861           0 :         if (U) ZV_togglesign(gel(U,j));
    1862           0 :         d = gcoeff(A,t,j);
    1863             :       }
    1864        1428 :       for (j1=1; j1<j; j1++)
    1865             :       {
    1866         875 :         if (!l[j1]) continue;
    1867         861 :         q = truedivii(gcoeff(A,t,j1),d);
    1868         861 :         if (!signe(q)) continue;
    1869             : 
    1870         329 :         togglesign(q);
    1871         329 :         ZC_lincomb1_inplace(gel(A,j1), gel(A,j), q);
    1872         329 :         if (U) ZC_lincomb1_inplace(gel(U,j1), gel(U,j), q);
    1873             :       }
    1874             :     }
    1875        1029 :     t = m; while (t && (c[t] || !signe(gcoeff(A,t,k)))) t--;
    1876        1029 :     if (t)
    1877             :     {
    1878         931 :       p = gcoeff(A,t,k);
    1879       16975 :       for (i=t-1; i; i--)
    1880             :       {
    1881       16044 :         q = gcoeff(A,i,k);
    1882       16044 :         if (signe(q) && abscmpii(p,q) > 0) { p = q; t = i; }
    1883             :       }
    1884         931 :       perm[++r] = l[k] = t; c[t] = k;
    1885         931 :       if (signe(p) < 0)
    1886             :       {
    1887         111 :         ZV_neg_inplace(gel(A,k));
    1888         111 :         if (U) ZV_togglesign(gel(U,k));
    1889         111 :         p = gcoeff(A,t,k);
    1890             :       }
    1891             :       /* p > 0 */
    1892        2842 :       for (j=1; j<k; j++)
    1893             :       {
    1894        1911 :         if (!l[j]) continue;
    1895        1890 :         q = truedivii(gcoeff(A,t,j),p);
    1896        1890 :         if (!signe(q)) continue;
    1897             : 
    1898         231 :         togglesign(q);
    1899         231 :         ZC_lincomb1_inplace(gel(A,j), gel(A,k), q);
    1900         231 :         if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,k), q);
    1901             :       }
    1902             :     }
    1903        1029 :     if (gc_needed(av1,1))
    1904             :     {
    1905           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"hnfperm, k=%ld",k);
    1906           0 :       gerepileall(av1, U? 2: 1, &A, &U);
    1907             :     }
    1908             :   }
    1909         252 :   if (r < m)
    1910             :   {
    1911        3948 :     for (i=1,k=r; i<=m; i++)
    1912        3752 :       if (!c[i]) perm[++k] = i;
    1913             :   }
    1914             : 
    1915             : /* We have A0*U=A, U in Gl(n,Z)
    1916             :  * basis for Im(A):  columns of A s.t l[j]>0 (r   cols)
    1917             :  * basis for Ker(A): columns of U s.t l[j]=0 (n-r cols) */
    1918         252 :   p = cgetg(r+1,t_MAT);
    1919         252 :   for (i=1; i<=m/2; i++) lswap(perm[i], perm[m+1-i]);
    1920         252 :   if (U)
    1921             :   {
    1922          70 :     GEN u = cgetg(n+1,t_MAT);
    1923         322 :     for (t=1,k=r,j=1; j<=n; j++)
    1924         252 :       if (l[j])
    1925             :       {
    1926         154 :         u[k + n-r] = U[j];
    1927         154 :         gel(p,k--) = vecpermute(gel(A,j), perm);
    1928             :       }
    1929             :       else
    1930          98 :         u[t++] = U[j];
    1931          70 :     *ptU = u;
    1932          70 :     if (ptperm) *ptperm = perm;
    1933          70 :     gerepileall(av, ptperm? 3: 2, &p, ptU, ptperm);
    1934             :   }
    1935             :   else
    1936             :   {
    1937         959 :     for (k=r,j=1; j<=n; j++)
    1938         777 :       if (l[j]) gel(p,k--) = vecpermute(gel(A,j), perm);
    1939         182 :     if (ptperm) *ptperm = perm;
    1940         182 :     gerepileall(av, ptperm? 2: 1, &p, ptperm);
    1941             :   }
    1942         252 :   return p;
    1943             : }
    1944             : 
    1945             : GEN
    1946         175 : ZM_hnf_knapsack(GEN x)
    1947             : {
    1948         175 :   GEN t, perm, H = ZM_hnfperm(x,NULL,&perm);
    1949         175 :   long i,j, l = lg(H), h = lgcols(H);
    1950        2352 :   for (i=1; i<h; i++)
    1951             :   {
    1952        2261 :     int fl = 0;
    1953       13181 :     for (j=1; j<l; j++)
    1954             :     {
    1955       11004 :       t = gcoeff(H,i,j);
    1956       11004 :       if (signe(t))
    1957             :       {
    1958        2296 :         if (!is_pm1(t) || fl) return NULL;
    1959        2212 :         fl = 1;
    1960             :       }
    1961             :     }
    1962             :   }
    1963          91 :   return rowpermute(H, perm_inv(perm));
    1964             : }
    1965             : 
    1966             : GEN
    1967          14 : hnfperm(GEN A)
    1968             : {
    1969          14 :   GEN y = cgetg(4, t_VEC);
    1970          14 :   gel(y,1) = ZM_hnfperm(A, &gel(y,2), &gel(y,3));
    1971          14 :   return y;
    1972             : }
    1973             : 
    1974             : /* Hermite Normal Form, with base change matrix if ptB != NULL.
    1975             :  * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
    1976             :  * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
    1977             : GEN
    1978      116390 : ZM_hnfall_i(GEN A, GEN *ptB, long remove)
    1979             : {
    1980             :   pari_sp av;
    1981             :   long m, n, r, i, j, k, li;
    1982             :   GEN B, c, h, a;
    1983             : 
    1984      116390 :   RgM_dimensions(A, &m,&n);
    1985      116390 :   if (!n)
    1986             :   {
    1987           7 :     if (ptB) *ptB = cgetg(1,t_MAT);
    1988           7 :     return cgetg(1,t_MAT);
    1989             :   }
    1990      116383 :   c = zero_zv(m);
    1991      116383 :   h = const_vecsmall(n, m);
    1992      116383 :   av = avma;
    1993      116383 :   A = RgM_shallowcopy(A);
    1994      116383 :   B = ptB? matid(n): NULL;
    1995      116383 :   r = n+1;
    1996      391932 :   for (li=m; li; li--)
    1997             :   {
    1998      873927 :     for (j=1; j<r; j++)
    1999             :     {
    2000     1114088 :       for (i=h[j]; i>li; i--)
    2001             :       {
    2002      240385 :         a = gcoeff(A,i,j);
    2003      240385 :         k = c[i];
    2004             :         /* zero a = Aij  using  Aik */
    2005      240385 :         if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B,j,k);
    2006      240385 :         ZM_reduce(A,B, i,k); /* ensure reduced entries even if a = 0 */
    2007      240385 :         if (gc_needed(av,1))
    2008             :         {
    2009           0 :           if (DEBUGMEM>1)
    2010           0 :             pari_warn(warnmem,"ZM_hnfall[1], li = %ld, j = %ld", li, j);
    2011           0 :           gerepileall(av, B? 2: 1, &A, &B);
    2012             :         }
    2013             :       }
    2014      873703 :       if (signe( gcoeff(A,li,j) )) break;
    2015      598378 :       h[j] = li-1;
    2016             :     }
    2017      275549 :     if (j == r) continue;
    2018      275325 :     r--;
    2019      275325 :     if (j < r) /* A[j] != 0 */
    2020             :     {
    2021      199449 :       swap(gel(A,j), gel(A,r));
    2022      199449 :       if (B) swap(gel(B,j), gel(B,r));
    2023      199449 :       h[j] = h[r]; h[r] = li; c[li] = r;
    2024             :     }
    2025      275325 :     if (signe(gcoeff(A,li,r)) < 0)
    2026             :     {
    2027       62545 :       ZV_neg_inplace(gel(A,r));
    2028       62545 :       if (B) ZV_togglesign(gel(B,r));
    2029             :     }
    2030      275325 :     ZM_reduce(A,B, li,r);
    2031      275325 :     if (gc_needed(av,1))
    2032             :     {
    2033           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[2], li = %ld", li);
    2034           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2035             :     }
    2036             :   }
    2037             : 
    2038      116383 :   if (DEBUGLEVEL>5) err_printf("\nhnfall, final phase: ");
    2039      116383 :   r--; /* first r cols are in the image the n-r (independent) last ones */
    2040      700456 :   for (j=1; j<=r; j++)
    2041     1388517 :     for (i=h[j]; i; i--)
    2042             :     {
    2043      804444 :       a = gcoeff(A,i,j);
    2044      804444 :       k = c[i];
    2045      804444 :       if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B, j,k);
    2046      804444 :       ZM_reduce(A,B, i,k); /* ensure reduced entries, even if a = 0 */
    2047      804444 :       if (gc_needed(av,1))
    2048             :       {
    2049           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[3], j = %ld", j);
    2050           0 :         gerepileall(av, B? 2: 1, &A, &B);
    2051             :       }
    2052             :     }
    2053      116383 :   if (DEBUGLEVEL>5) err_printf("\n");
    2054      116383 :   if (remove) remove_0cols(r, &A, &B, remove);
    2055      116383 :   if (ptB) *ptB = B;
    2056      116383 :   return A;
    2057             : }
    2058             : GEN
    2059        3584 : ZM_hnfall(GEN A, GEN *ptB, long remove)
    2060             : {
    2061        3584 :   pari_sp av = avma;
    2062        3584 :   A = ZM_hnfall_i(A, ptB, remove);
    2063        3584 :   gerepileall(av, ptB? 2: 1, &A, ptB);
    2064        3584 :   return A;
    2065             : }
    2066             : 
    2067             : GEN
    2068          14 : hnfall(GEN x)
    2069             : {
    2070          14 :   GEN z = cgetg(3, t_VEC);
    2071          14 :   gel(z,1) = ZM_hnfall(x, (GEN*)(z+2), 1);
    2072          14 :   return z;
    2073             : }
    2074             : GEN
    2075          14 : hnf(GEN x) { return mathnf0(x,0); }
    2076             : 
    2077             : /* C = A^(-1)t where A and C are integral, A is upper triangular, t t_INT */
    2078             : GEN
    2079       20839 : hnf_invscale(GEN A, GEN t)
    2080             : {
    2081       20839 :   long n = lg(A)-1, i,j,k;
    2082       20839 :   GEN m, c = cgetg(n+1,t_MAT);
    2083             : 
    2084       20839 :   if (!n) return c;
    2085      114996 :   for (k=1; k<=n; k++)
    2086             :   { /* cf hnf_divscale with B = id, thus b = e_k */
    2087       94157 :     GEN u = cgetg(n+1, t_COL);
    2088       94157 :     pari_sp av = avma;
    2089       94157 :     gel(c,k) = u;
    2090       94157 :     gel(u,n) = k == n? gerepileuptoint(av, diviiexact(t, gcoeff(A,n,n))): gen_0;
    2091      621201 :     for (i=n-1; i>0; i--)
    2092             :     {
    2093      527044 :       av = avma; m = i == k? t: gen_0;
    2094      527044 :       for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2095      527044 :       gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
    2096             :     }
    2097             :   }
    2098       20839 :   return c;
    2099             : }
    2100             : 
    2101             : /* C = A^(-1)(tB) where A, B, C are integral, A is upper triangular, t t_INT */
    2102             : GEN
    2103      156850 : hnf_divscale(GEN A, GEN B, GEN t)
    2104             : {
    2105      156850 :   long n = lg(A)-1, i,j,k;
    2106      156850 :   GEN m, c = cgetg(n+1,t_MAT);
    2107             : 
    2108      156850 :   if (!n) return c;
    2109      626255 :   for (k=1; k<=n; k++)
    2110             :   {
    2111      469405 :     GEN u = cgetg(n+1, t_COL), b = gel(B,k);
    2112      469405 :     pari_sp av = avma;
    2113      469405 :     gel(c,k) = u; m = mulii(gel(b,n),t);
    2114      469405 :     gel(u,n) = gerepileuptoint(av, diviiexact(m, gcoeff(A,n,n)));
    2115     2126573 :     for (i=n-1; i>0; i--)
    2116             :     {
    2117     1657168 :       av = avma; m = mulii(gel(b,i),t);
    2118     1657168 :       for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2119     1657168 :       gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
    2120             :     }
    2121             :   }
    2122      156850 :   return c;
    2123             : }
    2124             : 
    2125             : /* A, B integral upper HNF. A^(-1) B integral ? */
    2126             : int
    2127          91 : hnfdivide(GEN A, GEN B)
    2128             : {
    2129          91 :   pari_sp av = avma;
    2130          91 :   long n = lg(A)-1, i,j,k;
    2131             :   GEN u, b, m, r;
    2132             : 
    2133          91 :   if (!n) return 1;
    2134          91 :   if (lg(B)-1 != n) pari_err_DIM("hnfdivide");
    2135          91 :   u = cgetg(n+1, t_COL);
    2136         413 :   for (k=1; k<=n; k++)
    2137             :   {
    2138         322 :     b = gel(B,k);
    2139         322 :     m = gel(b,k);
    2140         322 :     gel(u,k) = dvmdii(m, gcoeff(A,k,k), &r);
    2141         322 :     if (r != gen_0) { avma = av; return 0; }
    2142         798 :     for (i=k-1; i>0; i--)
    2143             :     {
    2144         476 :       m = gel(b,i);
    2145         476 :       for (j=i+1; j<=k; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2146         476 :       m = dvmdii(m, gcoeff(A,i,i), &r);
    2147         476 :       if (r != gen_0) { avma = av; return 0; }
    2148         476 :       gel(u,i) = m;
    2149             :     }
    2150             :   }
    2151          91 :   avma = av; return 1;
    2152             : }
    2153             : 
    2154             : /* A upper HNF, b integral vector. Return A^(-1) b if integral,
    2155             :  * NULL otherwise. Assume #A[,1] = #b. */
    2156             : GEN
    2157       70021 : hnf_invimage(GEN A, GEN b)
    2158             : {
    2159       70021 :   pari_sp av = avma;
    2160       70021 :   long n = lg(A)-1, m, i, k;
    2161             :   GEN u, r;
    2162             : 
    2163       70021 :   if (!n) return lg(b)==1? cgetg(1,t_COL):NULL;
    2164       69979 :   m = nbrows(A); /* m >= n */
    2165       69979 :   u = cgetg(n+1, t_COL);
    2166      225106 :   for (i = n, k = m; k > 0; k--)
    2167             :   {
    2168      225106 :     pari_sp av2 = avma;
    2169             :     long j;
    2170      225106 :     GEN t = gel(b,k), Aki = gcoeff(A,k,i);
    2171      225106 :     if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
    2172      225106 :     for (j=i+1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
    2173      225106 :     if (!signe(Aki))
    2174             :     {
    2175           0 :       if (signe(t)) { avma = av;return NULL; }
    2176           0 :       avma = av2; gel(u,i) = gen_0; continue;
    2177             :     }
    2178      225106 :     t = dvmdii(t, Aki, &r);
    2179      225106 :     if (r != gen_0) { avma = av; return NULL; }
    2180      192045 :     gel(u,i) = gerepileuptoint(av2, t);
    2181      192045 :     if (--i == 0) break;
    2182             :   }
    2183             :   /* If there is a solution, it must be u. Check remaining equations */
    2184       73836 :   for (; k > 0; k--)
    2185             :   {
    2186       36918 :     pari_sp av2 = avma;
    2187             :     long j;
    2188       36918 :     GEN t = gel(b,k);
    2189       36918 :     if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
    2190       36918 :     for (j=1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
    2191       36918 :     if (signe(t)) { avma = av;return NULL; }
    2192       36918 :     avma = av2;
    2193             :   }
    2194       36918 :   return u;
    2195             : }
    2196             : 
    2197             : /* A upper HNF, B integral matrix or column. Return A^(-1) B if integral,
    2198             :  * NULL otherwise */
    2199             : GEN
    2200       63560 : hnf_solve(GEN A, GEN B)
    2201             : {
    2202             :   pari_sp av;
    2203             :   long i, l;
    2204             :   GEN C;
    2205             : 
    2206       63560 :   if (typ(B) == t_COL) return hnf_invimage(A, B);
    2207       40201 :   av = avma; C = cgetg_copy(B, &l);
    2208       63259 :   for (i = 1; i < l; i++) {
    2209       42105 :     GEN c = hnf_invimage(A, gel(B,i));
    2210       42105 :     if (!c) { avma = av; return NULL; }
    2211       23058 :     gel(C,i) = c;
    2212             :   }
    2213       21154 :   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      136131 : snf_pile(pari_sp av, GEN *x, GEN *U, GEN *V)
    2235             : {
    2236             :   GEN *gptr[3];
    2237      136131 :   int c = 1; gptr[0]=x;
    2238      136131 :   if (*U) gptr[c++] = U;
    2239      136131 :   if (*V) gptr[c++] = V;
    2240      136131 :   gerepilemany(av,gptr,c);
    2241      136131 : }
    2242             : 
    2243             : static GEN
    2244       77360 : bezout_step(GEN *pa, GEN *pb, GEN *pu, GEN *pv)
    2245             : {
    2246       77360 :   GEN a = *pa, b = *pb, d;
    2247       77360 :   if (absequalii(a,b))
    2248             :   {
    2249       29288 :     long sa = signe(a), sb = signe(b);
    2250       29288 :     *pv = gen_0;
    2251       29288 :     if (sb == sa) {
    2252       29092 :       *pa = *pb = gen_1;
    2253       29092 :       if (sa > 0) {*pu=gen_1; return a;} else {*pu=gen_m1; return absi(a);}
    2254             :     }
    2255         196 :     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       48072 :   d = bezout(a,b, pu,pv);
    2259       48072 :   *pa = diviiexact(a, d);
    2260       48072 :   *pb = diviiexact(b, d); return d;
    2261             : }
    2262             : 
    2263             : static int
    2264      363142 : 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      157006 : ZM_snf_no_divide(GEN x, long i)
    2270             : {
    2271      157006 :   GEN b = gcoeff(x,i,i);
    2272             :   long j, k;
    2273             : 
    2274      157006 :   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      157006 :   if (is_pm1(b)) return 0;
    2282      285782 :   for (k = 1; k < i; k++)
    2283     1052053 :     for (j = 1; j < i; j++)
    2284      844637 :       if (!dvdii(gcoeff(x,k,j),b)) return k;
    2285       75664 :   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       76422 : ZM_snfall_i(GEN x, GEN *ptU, GEN *ptV, int return_vec)
    2292             : {
    2293       76422 :   pari_sp av0 = avma, av;
    2294             :   long i, j, k, m0, m, n0, n;
    2295       76422 :   GEN p1, u, v, U, V, V0, mdet, ys, perm = NULL;
    2296             : 
    2297       76422 :   n0 = n = lg(x)-1;
    2298       76422 :   if (!n) {
    2299        3655 :     if (ptU) *ptU = cgetg(1,t_MAT);
    2300        3655 :     if (ptV) *ptV = cgetg(1,t_MAT);
    2301        3655 :     return cgetg(1, return_vec? t_VEC: t_MAT);
    2302             :   }
    2303       72767 :   av = avma;
    2304       72767 :   m0 = m = nbrows(x);
    2305             : 
    2306       72767 :   U = ptU? gen_1: NULL; /* TRANSPOSE of row transform matrix [act on columns]*/
    2307       72767 :   V = ptV? gen_1: NULL;
    2308       72767 :   V0 = NULL;
    2309       72767 :   x = RgM_shallowcopy(x);
    2310       72767 :   if (m == n && ZM_ishnf(x))
    2311             :   {
    2312       64808 :     mdet = ZM_det_triangular(x);
    2313       64808 :     if (V) *ptV = matid(n);
    2314             :   }
    2315             :   else
    2316             :   {
    2317        7959 :     mdet = ZM_detmult(x);
    2318        7959 :     if (signe(mdet))
    2319             :     {
    2320        7938 :       if (!V)
    2321         287 :         p1 = ZM_hnfmod(x,mdet);
    2322             :       else
    2323             :       {
    2324        7651 :         if (m == n)
    2325             :         {
    2326        7630 :           p1 = ZM_hnfmod(x,mdet);
    2327        7630 :           *ptV = RgM_solve(x,p1);
    2328             :         }
    2329             :         else
    2330          21 :           p1 = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
    2331             :       }
    2332        7938 :       mdet = ZM_det_triangular(p1);
    2333             :     }
    2334             :     else
    2335          21 :       p1 = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
    2336        7959 :     x = p1;
    2337             :   }
    2338       72767 :   n = lg(x)-1;
    2339       72767 :   if (V)
    2340             :   {
    2341       68749 :     V = *ptV;
    2342       68749 :     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       72767 :   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       72746 :   if (U) U = matid(n);
    2368             : 
    2369             :   /* square, maximal rank n */
    2370       72746 :   p1 = gen_indexsort(RgM_diagonal_shallow(x), NULL, &negcmpii);
    2371       72746 :   ys = cgetg(n+1,t_MAT);
    2372       72746 :   for (j=1; j<=n; j++) gel(ys,j) = vecpermute(gel(x, p1[j]), p1);
    2373       72746 :   x = ys;
    2374       72746 :   if (U) U = vecpermute(U, p1);
    2375       72746 :   if (V) V = vecpermute(V, p1);
    2376             : 
    2377       72746 :   p1 = ZM_hnfmod(x, mdet);
    2378       72746 :   if (V) V = ZM_mul(V, RgM_solve(x,p1));
    2379       72746 :   x = p1;
    2380             : 
    2381       72746 :   if (DEBUGLEVEL>7) err_printf("starting SNF loop");
    2382      454100 :   for (i=n; i>1; i--)
    2383             :   {
    2384      154304 :     if (DEBUGLEVEL>7) err_printf("\ni = %ld: ",i);
    2385             :     for(;;)
    2386       57252 :     {
    2387      211556 :       int c = 0;
    2388             :       GEN a, b;
    2389      928597 :       for (j=i-1; j>=1; j--)
    2390             :       {
    2391      717041 :         b = gcoeff(x,i,j); if (!signe(b)) continue;
    2392        8755 :         a = gcoeff(x,i,i);
    2393        8755 :         ZC_elem(b, a, x,V, j,i);
    2394        8755 :         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      211556 :       if (DEBUGLEVEL>7) err_printf("; ");
    2401      928597 :       for (j=i-1; j>=1; j--)
    2402             :       {
    2403             :         GEN d;
    2404      717041 :         b = gcoeff(x,j,i); if (!signe(b)) continue;
    2405       77360 :         a = gcoeff(x,i,i);
    2406       77360 :         d = bezout_step(&a, &b, &u, &v);
    2407      451826 :         for (k = 1; k < i; k++)
    2408             :         {
    2409      374466 :           GEN t = addii(mulii(u,gcoeff(x,i,k)),mulii(v,gcoeff(x,j,k)));
    2410      748932 :           gcoeff(x,j,k) = subii(mulii(a,gcoeff(x,j,k)),
    2411      374466 :                                 mulii(b,gcoeff(x,i,k)));
    2412      374466 :           gcoeff(x,i,k) = t;
    2413             :         }
    2414       77360 :         gcoeff(x,j,i) = gen_0;
    2415       77360 :         gcoeff(x,i,i) = d;
    2416       77360 :         if (U) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
    2417       77360 :         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       77360 :         c = 1;
    2423             :       }
    2424      211556 :       if (!c)
    2425             :       {
    2426      157006 :         k = ZM_snf_no_divide(x, i);
    2427      157006 :         if (!k) break;
    2428             : 
    2429             :         /* x[k,j] != 0 mod b */
    2430        9093 :         for (j=1; j<=i; j++)
    2431        6391 :           gcoeff(x,i,j) = addii(gcoeff(x,i,j),gcoeff(x,k,j));
    2432        2702 :         if (U) gel(U,i) = gadd(gel(U,i),gel(U,k));
    2433             :       }
    2434       57252 :       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       72746 :   if (DEBUGLEVEL>7) err_printf("\n");
    2442      299796 :   for (k=1; k<=n; k++)
    2443      227050 :     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       72767 :   if (return_vec)
    2450             :   {
    2451       67702 :     long l = lg(x)-1;
    2452       67702 :     if (typ(x) == t_MAT) x = RgM_diagonal_shallow(x);
    2453       67702 :     if (m0 > l) x = shallowconcat(zerovec(m0-l), x);
    2454             :   }
    2455             : 
    2456       72767 :   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       72767 :   if (U)
    2462             :   {
    2463       56046 :     U = shallowtrans(U);
    2464       56046 :     if (perm) U = vecpermute(U, perm_inv(perm));
    2465             :   }
    2466       72767 :   snf_pile(av0, &x,&U,&V);
    2467       72767 :   if (ptU) *ptU = U;
    2468       72767 :   if (ptV) *ptV = V;
    2469       72767 :   return x;
    2470             : }
    2471             : GEN
    2472        8243 : 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             : 
    2476             : GEN
    2477         322 : smith(GEN x) {
    2478         322 :   if (typ(x)!=t_MAT) pari_err_TYPE("smith",x);
    2479         322 :   RgM_check_ZM(x, "smith");
    2480         322 :   return ZM_snfall_i(x, NULL,NULL, 1);
    2481             : }
    2482             : GEN
    2483          21 : smithall(GEN x)
    2484             : {
    2485          21 :   GEN z = cgetg(4, t_VEC);
    2486          21 :   if (typ(x)!=t_MAT) pari_err_TYPE("smithall",x);
    2487          21 :   RgM_check_ZM(x, "smithall");
    2488          21 :   gel(z,3) = ZM_snfall_i(x, (GEN*)(z+1),(GEN*)(z+2), 0);
    2489          21 :   return z;
    2490             : }
    2491             : 
    2492             : void
    2493      127679 : ZM_snfclean(GEN d, GEN u, GEN v)
    2494             : {
    2495      127679 :   long i, c, l = lg(d);
    2496             : 
    2497      127679 :   if (typ(d) == t_VEC)
    2498      127679 :     for (c=1; c<l; c++) { GEN t = gel(d,c); if (is_pm1(t)) break; }
    2499             :   else
    2500             :   {
    2501           0 :     for (c=1; c<l; c++) { GEN t = gcoeff(d,c,c); if (is_pm1(t)) break; }
    2502           0 :     if (c < l) for (i = 1; i < c; i++) setlg(gel(d,i), c);
    2503             :   }
    2504      127679 :   setlg(d, c);
    2505      127679 :   if (u) for (i=1; i<l; i++) setlg(gel(u,i), c);
    2506      127679 :   if (v) setlg(v, c);
    2507      127679 : }
    2508             : 
    2509             : /* Assume z was computed by [g]smithall(). Remove the 1s on the diagonal */
    2510             : GEN
    2511         539 : smithclean(GEN z)
    2512             : {
    2513             :   long i, j, h, l, c, d;
    2514             :   GEN U, V, y, D, t;
    2515             : 
    2516         539 :   if (typ(z) != t_VEC) pari_err_TYPE("smithclean",z);
    2517         539 :   l = lg(z); if (l == 1) return cgetg(1,t_VEC);
    2518         532 :   U = gel(z,1);
    2519         532 :   if (l != 4 || typ(U) != t_MAT)
    2520             :   { /* assume z = vector of elementary divisors */
    2521        1470 :     for (c=1; c<l; c++)
    2522        1309 :       if (gequal1(gel(z,c))) break;
    2523         511 :     return gcopy_lg(z, c);
    2524             :   }
    2525          21 :   V = gel(z,2);
    2526          21 :   D = gel(z,3);
    2527          21 :   l = lg(D);
    2528          21 :   if (l == 1) return gcopy(z);
    2529          21 :   h = lgcols(D);
    2530          21 :   if (h > l)
    2531             :   { /* D = vconcat(zero matrix, diagonal matrix) */
    2532          21 :     for (c=1+h-l, d=1; c<h; c++,d++)
    2533          21 :       if (gequal1(gcoeff(D,c,d))) break;
    2534             :   }
    2535           7 :   else if (h < l)
    2536             :   { /* D = concat(zero matrix, diagonal matrix) */
    2537           7 :     for (c=1, d=1+l-h; d<l; c++,d++)
    2538           7 :       if (gequal1(gcoeff(D,c,d))) break;
    2539             :   }
    2540             :   else
    2541             :   { /* D diagonal */
    2542           0 :     for (c=1; c<l; c++)
    2543           0 :       if (gequal1(gcoeff(D,c,c))) break;
    2544           0 :     d = c;
    2545             :   }
    2546             :   /* U was (h-1)x(h-1), V was (l-1)x(l-1), D was (h-1)x(l-1) */
    2547          21 :   y = cgetg(4,t_VEC);
    2548             :   /* truncate U to (c-1) x (h-1) */
    2549          21 :   gel(y,1) = t = cgetg(h,t_MAT);
    2550          21 :   for (j=1; j<h; j++) gel(t,j) = gcopy_lg(gel(U,j), c);
    2551             :   /* truncate V to (l-1) x (d-1) */
    2552          21 :   gel(y,2) = gcopy_lg(V, d);
    2553          21 :   gel(y,3) = t = zeromatcopy(c-1, d-1);
    2554             :   /* truncate D to a (c-1) x (d-1) matrix */
    2555          21 :   if (d > 1)
    2556             :   {
    2557          14 :     if (h > l)
    2558             :     {
    2559          14 :       for (i=1+h-l, j=1; i<c; i++,j++)
    2560           7 :         gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
    2561             :     }
    2562           7 :     else if (h < l)
    2563             :     {
    2564           7 :       for (i=1, j=1+l-h; j<d; i++,j++)
    2565           0 :         gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
    2566             :     }
    2567             :     else
    2568             :     {
    2569           0 :       for (j=1; j<d; j++)
    2570           0 :         gcoeff(t,j,j) = gcopy(gcoeff(D,j,j));
    2571             :     }
    2572             :   }
    2573          21 :   return y;
    2574             : }
    2575             : 
    2576             : /* does b = x[i,i] divide all entries in x[1..i-1,1..i-1] ? If so, return 0;
    2577             :  * else return the index of a problematic row */
    2578             : static long
    2579         112 : gsnf_no_divide(GEN x, long i, long vx)
    2580             : {
    2581         112 :   GEN b = gcoeff(x,i,i);
    2582             :   long j, k;
    2583             : 
    2584         112 :   if (gequal0(b))
    2585             :   {
    2586          14 :     for (k = 1; k < i; k++)
    2587          14 :       for (j = 1; j < i; j++)
    2588          14 :         if (!gequal0(gcoeff(x,k,j))) return k;
    2589           0 :     return 0;
    2590             :   }
    2591             : 
    2592          98 :   if (!is_RgX(b,vx) || degpol(b)<=0) return 0;
    2593         133 :   for (k = 1; k < i; k++)
    2594         231 :     for (j = 1; j < i; j++)
    2595             :     {
    2596         154 :       GEN z = gcoeff(x,k,j), r;
    2597         154 :       if (!is_RgX(z,vx)) z = scalarpol(z, vx);
    2598         154 :       r = RgX_rem(z, b);
    2599         154 :       if (signe(r) && (! isinexactreal(r) ||
    2600           0 :              gexpo(r) > 16 + gexpo(b) - prec2nbits(gprecision(r)))
    2601           7 :          ) return k;
    2602             :     }
    2603          49 :   return 0;
    2604             : }
    2605             : 
    2606             : /* Hermite Normal Form, with base change matrix if ptB != NULL.
    2607             :  * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
    2608             :  * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
    2609             : GEN
    2610          35 : RgM_hnfall(GEN A, GEN *pB, long remove)
    2611             : {
    2612             :   pari_sp av;
    2613             :   long li, j, k, m, n, def, ldef;
    2614             :   GEN B;
    2615          35 :   long vx = gvar(A);
    2616             : 
    2617          35 :   n = lg(A)-1;
    2618          35 :   if (vx==NO_VARIABLE || !n)
    2619             :   {
    2620           0 :     RgM_check_ZM(A, "mathnf0");
    2621           0 :     return ZM_hnfall(A, pB, remove);
    2622             :   }
    2623          35 :   m = nbrows(A);
    2624          35 :   av = avma;
    2625          35 :   A = RgM_shallowcopy(A);
    2626          35 :   B = pB? matid(n): NULL;
    2627          35 :   def = n; ldef = (m>n)? m-n: 0;
    2628          77 :   for (li=m; li>ldef; li--)
    2629             :   {
    2630             :     GEN d, T;
    2631          63 :     for (j=def-1; j; j--)
    2632             :     {
    2633          21 :       GEN a = gcoeff(A,li,j);
    2634          21 :       if (gequal0(a)) continue;
    2635             : 
    2636           7 :       k = (j==1)? def: j-1;
    2637           7 :       RgC_elem(a,gcoeff(A,li,k), A,B, j,k, li, vx);
    2638             :     }
    2639          42 :     T = normalize_as_RgX(gcoeff(A,li,def), vx, &d);
    2640          42 :     if (gequal0(T))
    2641           0 :     { if (ldef) ldef--; }
    2642             :     else
    2643             :     {
    2644          42 :       gcoeff(A,li,def) = T;
    2645          42 :       if (B && !gequal1(d)) gel(B, def) = RgC_Rg_div(gel(B, def), d);
    2646          42 :       RgM_reduce(A, B, li, def, vx);
    2647          42 :       def--;
    2648             :     }
    2649          42 :     if (gc_needed(av,1))
    2650             :     {
    2651           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ghnfall");
    2652           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2653             :     }
    2654             :   }
    2655             :   /* rank A = n - def */
    2656          35 :   if (remove) remove_0cols(def, &A, &B, remove);
    2657          35 :   gerepileall(av, B? 2: 1, &A, &B);
    2658          35 :   if (B) *pB = B;
    2659          35 :   return A;
    2660             : }
    2661             : 
    2662             : static GEN
    2663          42 : gsmithall_i(GEN x,long all)
    2664             : {
    2665             :   pari_sp av;
    2666             :   long i, j, k, n;
    2667             :   GEN z, u, v, U, V;
    2668          42 :   long vx = gvar(x);
    2669          42 :   if (typ(x)!=t_MAT) pari_err_TYPE("gsmithall",x);
    2670          42 :   if (vx==NO_VARIABLE) return all? smithall(x): smith(x);
    2671          35 :   n = lg(x)-1;
    2672          35 :   if (!n) return trivsmith(all);
    2673          35 :   if (lgcols(x) != n+1) pari_err_DIM("gsmithall");
    2674          35 :   av = avma;
    2675          35 :   x = RgM_shallowcopy(x);
    2676          35 :   if (all) { U = matid(n); V = matid(n); }
    2677         252 :   for (i=n; i>=2; i--)
    2678             :   {
    2679             :     for(;;)
    2680          98 :     {
    2681             :       GEN a, b, d;
    2682         189 :       int c = 0;
    2683         546 :       for (j=i-1; j>=1; j--)
    2684             :       {
    2685         357 :         b = gcoeff(x,i,j); if (gequal0(b)) continue;
    2686         140 :         a = gcoeff(x,i,i);
    2687         140 :         d = gbezout_step(&b, &a, &v, &u, vx);
    2688         469 :         for (k = 1; k < i; k++)
    2689             :         {
    2690         329 :           GEN t = gadd(gmul(u,gcoeff(x,k,i)),gmul(v,gcoeff(x,k,j)));
    2691         329 :           gcoeff(x,k,j) = gsub(gmul(a,gcoeff(x,k,j)),gmul(b,gcoeff(x,k,i)));
    2692         329 :           gcoeff(x,k,i) = t;
    2693             :         }
    2694         140 :         gcoeff(x,i,j) = gen_0;
    2695         140 :         gcoeff(x,i,i) = d;
    2696         140 :         if (all) update(u,v,a,b,(GEN*)(V+i),(GEN*)(V+j));
    2697             :       }
    2698         546 :       for (j=i-1; j>=1; j--)
    2699             :       {
    2700         357 :         b = gcoeff(x,j,i); if (gequal0(b)) continue;
    2701         119 :         a = gcoeff(x,i,i);
    2702         119 :         d = gbezout_step(&b, &a, &v, &u, vx);
    2703         406 :         for (k = 1; k < i; k++)
    2704             :         {
    2705         287 :           GEN t = gadd(gmul(u,gcoeff(x,i,k)),gmul(v,gcoeff(x,j,k)));
    2706         287 :           gcoeff(x,j,k) = gsub(gmul(a,gcoeff(x,j,k)),gmul(b,gcoeff(x,i,k)));
    2707         287 :           gcoeff(x,i,k) = t;
    2708             :         }
    2709         119 :         gcoeff(x,j,i) = gen_0;
    2710         119 :         gcoeff(x,i,i) = d;
    2711         119 :         if (all) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
    2712         119 :         c = 1;
    2713             :       }
    2714         189 :       if (!c)
    2715             :       {
    2716         112 :         k = gsnf_no_divide(x, i, vx);
    2717         112 :         if (!k) break;
    2718             : 
    2719          70 :         for (j=1; j<=i; j++)
    2720          49 :           gcoeff(x,i,j) = gadd(gcoeff(x,i,j),gcoeff(x,k,j));
    2721          21 :         if (all) gel(U,i) = gadd(gel(U,i),gel(U,k));
    2722             :       }
    2723          98 :       if (gc_needed(av,1))
    2724             :       {
    2725           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"gsmithall");
    2726           0 :         gerepileall(av, all? 3: 1, &x, &U, &V);
    2727             :       }
    2728             :     }
    2729             :   }
    2730         161 :   for (k=1; k<=n; k++)
    2731             :   {
    2732         126 :     GEN d, T = normalize_as_RgX(gcoeff(x,k,k), vx, &d);
    2733         126 :     if (gequal0(T)) continue;
    2734         112 :     if (all && !gequal1(d)) gel(V,k) = RgC_Rg_div(gel(V,k), d);
    2735         112 :     gcoeff(x,k,k) = T;
    2736             :   }
    2737          35 :   z = all? mkvec3(shallowtrans(U), V, x): RgM_diagonal_shallow(x);
    2738          35 :   return gerepilecopy(av, z);
    2739             : }
    2740             : 
    2741             : GEN
    2742         378 : matsnf0(GEN x,long flag)
    2743             : {
    2744         378 :   pari_sp av = avma;
    2745         378 :   if (flag > 7) pari_err_FLAG("matsnf");
    2746         378 :   if (typ(x) == t_VEC && flag & 4) return smithclean(x);
    2747         378 :   if (flag & 2) x = flag&1 ? gsmithall(x): gsmith(x);
    2748         336 :   else          x = flag&1 ?  smithall(x):  smith(x);
    2749         378 :   if (flag & 4) x = gerepileupto(av, smithclean(x));
    2750         378 :   return x;
    2751             : }
    2752             : 
    2753             : GEN
    2754          42 : gsmith(GEN x) { return gsmithall_i(x,0); }
    2755             : 
    2756             : GEN
    2757           0 : gsmithall(GEN x) { return gsmithall_i(x,1); }
    2758             : 
    2759             : /* H is a relation matrix, either in HNF or a t_VEC (diagonal HNF) */
    2760             : static GEN
    2761      127679 : snf_group(GEN H, GEN D, GEN *newU, GEN *newUi)
    2762             : {
    2763             :   long i, j, l;
    2764             : 
    2765      127679 :   ZM_snfclean(D, newU? *newU: NULL, newUi? *newUi: NULL);
    2766      127679 :   l = lg(D);
    2767      127679 :   if (newU) {
    2768      111272 :     GEN U = *newU;
    2769      354004 :     for (i = 1; i < l; i++)
    2770             :     {
    2771      242732 :       GEN d = gel(D,i), d2 = shifti(d, 1);
    2772     1296274 :       for (j = 1; j < lg(U); j++)
    2773     1053542 :         gcoeff(U,i,j) = centermodii(gcoeff(U,i,j), d, d2);
    2774             :     }
    2775      111272 :     *newU = U;
    2776             :   }
    2777      127679 :   if (newUi && l > 1)
    2778             :   { /* UHV=D -> U^-1 = (HV)D^-1 -> U^-1 = H(VD^-1 mod 1) mod H */
    2779             :     /* Ui = ZM_inv(U, NULL); setlg(Ui, l); */
    2780      123597 :     GEN V = *newUi, Ui;
    2781      123597 :     int Hvec = (typ(H) == t_VEC);
    2782      123597 :     for (i = 1; i < l; i++) gel(V,i) = FpC_red(gel(V,i), gel(D,i));
    2783      123597 :     if (!Hvec)
    2784             :     {
    2785       60422 :       if (ZM_isdiagonal(H)) { H = RgM_diagonal_shallow(H); Hvec = 1; }
    2786             :     }
    2787      123597 :     Ui = Hvec? ZM_diag_mul(H, V): ZM_mul(H, V);
    2788      123597 :     for (i = 1; i < l; i++) gel(Ui,i) = ZC_Z_divexact(gel(Ui,i), gel(D,i));
    2789      123597 :     if (Hvec)
    2790       88003 :     { for (i = 1; i < l; i++) gel(Ui,i) = vecmodii(gel(Ui,i), H); }
    2791             :     else
    2792       35594 :       Ui = ZM_hnfrem(Ui, H);
    2793      123597 :     *newUi = Ui;
    2794             :   }
    2795      127679 :   return D;
    2796             : }
    2797             : /* H relation matrix among row of generators g in HNF.  Let URV = D its SNF,
    2798             :  * newU R newV = newD its clean SNF (no 1 in Dnew). Return the diagonal of
    2799             :  * newD, newU and newUi such that  1/U = (newUi, ?).
    2800             :  * Rationale: let (G,0) = g Ui be the new generators then
    2801             :  * 0 = G U R --> G D = 0,  g = G newU  and  G = g newUi */
    2802             : GEN
    2803       64315 : ZM_snf_group(GEN H, GEN *newU, GEN *newUi)
    2804             : {
    2805       64315 :   GEN D = ZM_snfall_i(H, newU, newUi, 1);
    2806       64315 :   return snf_group(H, D, newU, newUi);
    2807             : }
    2808             : 
    2809             : /* D a ZV: SNF for matdiagonal(D). Faster because we only ensure elementary
    2810             :  * divisors condition: d[n] | ... | d[1] and need not convert D to matrix form*/
    2811             : GEN
    2812       63364 : ZV_snfall(GEN D, GEN *pU, GEN *pV)
    2813             : {
    2814       63364 :   pari_sp av = avma;
    2815       63364 :   long j, n = lg(D)-1;
    2816       63364 :   GEN U = pU? matid(n): NULL;
    2817       63364 :   GEN V = pV? matid(n): NULL;
    2818             :   GEN p;
    2819             : 
    2820       63364 :   D = leafcopy(D);
    2821      201586 :   for (j = n; j > 0; j--)
    2822             :   {
    2823      138222 :     GEN b = gel(D,j);
    2824      138222 :     if (signe(b) < 0)
    2825             :     {
    2826           0 :       gel(D,j) = negi(b);
    2827           0 :       if (V) ZV_togglesign(gel(V,j));
    2828             :     }
    2829             :   }
    2830             :   /* entries are non-negative integers */
    2831       63364 :   p = gen_indexsort(D, NULL, &negcmpii);
    2832       63364 :   D = vecpermute(D, p);
    2833       63364 :   if (U) U = vecpermute(U, p);
    2834       63364 :   if (V) V = vecpermute(V, p);
    2835             :   /* entries are sorted by decreasing value */
    2836      201586 :   for (j = n; j > 0; j--)
    2837             :   {
    2838      138222 :     GEN b = gel(D,j);
    2839             :     long i;
    2840      247891 :     for (i = j-1; i > 0; i--)
    2841             :     { /* invariant: a >= b. If au+bv = d is a Bezout relation, A=a/d and B=b/d
    2842             :        * we have [B,-A;u,v]*diag(a,b)*[1-u*A,1; -u*A,1]] = diag(Ab, d) */
    2843      110341 :       GEN a = gel(D,i), u,v, d = bezout(a,b, &u,&v), A, Wi, Wj;
    2844      110341 :       if (equalii(d,b)) continue;
    2845       13629 :       A = diviiexact(a,d);
    2846       13629 :       if (V)
    2847             :       {
    2848       13587 :         GEN t = mulii(u,A);
    2849       13587 :         Wi = ZC_lincomb(subui(1,t), negi(t), gel(V,i), gel(V,j));
    2850       13587 :         Wj = ZC_add(gel(V,i), gel(V,j));
    2851       13587 :         gel(V,i) = Wi;
    2852       13587 :         gel(V,j) = Wj;
    2853             :       }
    2854       13629 :       if (U)
    2855             :       {
    2856       13629 :         GEN B = diviiexact(b,d);
    2857       13629 :         Wi = ZC_lincomb(B, negi(A), gel(U,i), gel(U,j));
    2858       13629 :         Wj = ZC_lincomb(u, v, gel(U,i), gel(U,j));
    2859       13629 :         gel(U,i) = Wi;
    2860       13629 :         gel(U,j) = Wj;
    2861             :       }
    2862       13629 :       gel(D,i) = mulii(A,b); /* lcm(a,b) */
    2863       13629 :       gel(D,j) = d; /* gcd(a,b) */
    2864       13629 :       b = gel(D,j); if (equali1(b)) break;
    2865             :     }
    2866             :   }
    2867       63364 :   snf_pile(av, &D,&U,&V);
    2868       63364 :   if (U) *pU = shallowtrans(U);
    2869       63364 :   if (V) *pV = V;
    2870       63364 :   return D;
    2871             : }
    2872             : GEN
    2873       63364 : ZV_snf_group(GEN d, GEN *newU, GEN *newUi)
    2874             : {
    2875       63364 :   GEN D = ZV_snfall(d, newU, newUi);
    2876       63364 :   return snf_group(d, D, newU, newUi);
    2877             : }
    2878             : 
    2879             : /* D a vector of elementary divisors. Truncate (setlg) to leave out trivial
    2880             :  * entries (= 1) */
    2881             : void
    2882         224 : ZV_snf_trunc(GEN D)
    2883             : {
    2884         224 :   long i, l = lg(D);
    2885         490 :   for (i = 1; i < l; i++)
    2886         371 :     if (is_pm1(gel(D,i))) { setlg(D,i); break; }
    2887         224 : }

Generated by: LCOV version 1.13