Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - hnf_snf.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.18.0 lcov report (development 29712-7c8a932571) Lines: 1612 1788 90.2 %
Date: 2024-11-15 09:08:45 Functions: 97 107 90.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : #include "pari.h"
      16             : #include "paripriv.h"
      17             : 
      18             : #define DEBUGLEVEL DEBUGLEVEL_mathnf
      19             : 
      20             : /**************************************************************/
      21             : /**                                                          **/
      22             : /**                HERMITE NORMAL FORM REDUCTION             **/
      23             : /**                                                          **/
      24             : /**************************************************************/
      25             : static GEN ZV_hnfgcdext(GEN A);
      26             : static GEN
      27          21 : hnfallgen(GEN x)
      28             : {
      29          21 :   GEN z = cgetg(3, t_VEC);
      30          21 :   gel(z,1) = RgM_hnfall(x, (GEN*)(z+2), 1);
      31          21 :   return z;
      32             : }
      33             : GEN
      34         287 : mathnf0(GEN x, long flag)
      35             : {
      36         287 :   switch(typ(x))
      37             :   {
      38          70 :     case t_VEC:
      39          70 :       if (RgV_is_ZV(x))
      40             :         switch (flag)
      41             :         {
      42          14 :           case 0:
      43          14 :             if (lg(x) == 1) return cgetg(1, t_MAT);
      44           7 :             retmkmat(mkcol(ZV_content(x)));
      45          21 :           case 1:
      46             :           case 4:
      47          21 :             return ZV_hnfgcdext(x);
      48             :         }
      49          35 :       x = gtomat(x); break;
      50         217 :     case t_MAT: break;
      51           0 :     default: pari_err_TYPE("mathnf0",x);
      52             :   }
      53             : 
      54         252 :   switch(flag)
      55             :   {
      56         196 :     case 0: case 2: return RgM_is_ZM(x)? ZM_hnf(x): RgM_hnfall(x,NULL,1);
      57          35 :     case 1: case 3: return RgM_is_ZM(x)? hnfall(x): hnfallgen(x);
      58           7 :     case 4: RgM_check_ZM(x, "mathnf0"); return hnflll(x);
      59          14 :     case 5: RgM_check_ZM(x, "mathnf0"); return hnfperm(x);
      60           0 :     default: pari_err_FLAG("mathnf");
      61             :   }
      62             :   return NULL; /* LCOV_EXCL_LINE */
      63             : }
      64             : 
      65             : /*******************************************************************/
      66             : /*                                                                 */
      67             : /*                SPECIAL HNF (FOR INTERNAL USE !!!)               */
      68             : /*                                                                 */
      69             : /*******************************************************************/
      70             : static int
      71     8100835 : count(GEN mat, long row, long len, long *firstnonzero)
      72             : {
      73     8100835 :   long j, n = 0;
      74             : 
      75   731154319 :   for (j=1; j<=len; j++)
      76             :   {
      77   724834200 :     long p = mael(mat,j,row);
      78   724834200 :     if (p)
      79             :     {
      80    23024378 :       if (labs(p)!=1) return -1;
      81    21243662 :       n++; *firstnonzero=j;
      82             :     }
      83             :   }
      84     6320119 :   return n;
      85             : }
      86             : 
      87             : static int
      88      420431 : count2(GEN mat, long row, long len)
      89             : {
      90             :   long j;
      91     4443779 :   for (j=len; j; j--)
      92     4314790 :     if (labs(mael(mat,j,row)) == 1) return j;
      93      128989 :   return 0;
      94             : }
      95             : 
      96             : static GEN
      97      225487 : hnffinal(GEN matgen,GEN perm,GEN* ptdep,GEN* ptB,GEN* ptC)
      98             : {
      99             :   GEN p1,p2,U,H,Hnew,Bnew,Cnew,diagH1;
     100      225487 :   GEN B = *ptB, C = *ptC, dep = *ptdep, depnew;
     101             :   pari_sp av;
     102             :   long i,j,k,s,i1,j1,zc;
     103      225487 :   long co = lg(C);
     104      225487 :   long col = lg(matgen)-1;
     105             :   long lnz, nlze, lig;
     106             : 
     107      225487 :   if (col == 0) return matgen;
     108      225487 :   lnz = nbrows(matgen); /* was called lnz-1 - nr in hnfspec */
     109      225487 :   nlze = nbrows(dep);
     110      225485 :   lig = nlze + lnz;
     111             :   /* H: lnz x lnz [disregarding initial 0 cols], U: col x col */
     112      225485 :   H = ZM_hnflll(matgen, &U, 0);
     113      225487 :   H += lg(H)-1 - lnz; H[0] = evaltyp(t_MAT) | _evallg(lnz+1);
     114             :   /* Only keep the part above the H (above the 0s is 0 since the dep rows
     115             :    * are dependent from the ones in matgen) */
     116      225487 :   zc = col - lnz; /* # of 0 columns, correspond to units */
     117      225487 :   if (nlze) { dep = ZM_mul(dep,U); dep += zc; }
     118             : 
     119      225487 :   diagH1 = new_chunk(lnz+1); /* diagH1[i] = 0 iff H[i,i] != 1 (set later) */
     120             : 
     121      225487 :   av = avma;
     122      225487 :   Cnew = cgetg(co, typ(C));
     123      225486 :   setlg(C, col+1); p1 = gmul(C,U);
     124     1899452 :   for (j=1; j<=col; j++) gel(Cnew,j) = gel(p1,j);
     125     3193617 :   for (   ; j<co ; j++)  gel(Cnew,j) = gel(C,j);
     126             : 
     127             :   /* Clean up B using new H */
     128      895507 :   for (s=0,i=lnz; i; i--)
     129             :   {
     130      670047 :     GEN Di = gel(dep,i), Hi = gel(H,i);
     131      670047 :     GEN h = gel(Hi,i); /* H[i,i] */
     132      670047 :     if ( (diagH1[i] = is_pm1(h)) ) { h = NULL; s++; }
     133    16378024 :     for (j=col+1; j<co; j++)
     134             :     {
     135    15708349 :       GEN z = gel(B,j-col);
     136    15708349 :       p1 = gel(z,i+nlze);
     137    15708349 :       if (h) p1 = truedivii(p1,h);
     138    15709553 :       if (!signe(p1)) continue;
     139    27610683 :       for (k=1; k<=nlze; k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Di,k)));
     140   134346502 :       for (   ; k<=lig;  k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Hi,k-nlze)));
     141     9309968 :       gel(Cnew,j) = gsub(gel(Cnew,j), gmul(p1, gel(Cnew,i+zc)));
     142             :     }
     143      669675 :     if (gc_needed(av,2))
     144             :     {
     145         746 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnffinal, i = %ld",i);
     146         746 :       gerepileall(av, 2, &Cnew, &B);
     147             :     }
     148             :   }
     149      225460 :   p1 = cgetg(lnz+1,t_VEC); p2 = perm + nlze;
     150      895556 :   for (i1=0, j1=lnz-s, i=1; i<=lnz; i++) /* push the 1 rows down */
     151      670073 :     if (diagH1[i])
     152      305157 :       gel(p1,++j1) = gel(p2,i);
     153             :     else
     154      364916 :       gel(p2,++i1) = gel(p2,i);
     155      530640 :   for (i=i1+1; i<=lnz; i++) gel(p2,i) = gel(p1,i);
     156             : 
     157             :   /* s = # extra redundant generators taken from H
     158             :    *          zc  col-s  co   zc = col - lnz
     159             :    *       [ 0 |dep |     ]    i = nlze + lnz - s = lig - s
     160             :    *  nlze [--------|  B' ]
     161             :    *       [ 0 | H' |     ]    H' = H minus the s rows with a 1 on diagonal
     162             :    *     i [--------|-----] lig-s           (= "1-rows")
     163             :    *       [   0    | Id  ]
     164             :    *       [        |     ] li */
     165      225483 :   lig -= s; col -= s; lnz -= s;
     166      225483 :   Hnew = cgetg(lnz+1,t_MAT);
     167      225485 :   depnew = cgetg(lnz+1,t_MAT); /* only used if nlze > 0 */
     168      225485 :   Bnew = cgetg(co-col,t_MAT);
     169      225485 :   C = shallowcopy(Cnew);
     170      895561 :   for (j=1,i1=j1=0; j<=lnz+s; j++)
     171             :   {
     172      670076 :     GEN z = gel(H,j);
     173      670076 :     if (diagH1[j])
     174             :     { /* hit exactly s times */
     175      305158 :       i1++; C[i1+col] = Cnew[j+zc];
     176      305158 :       p1 = cgetg(lig+1,t_COL); gel(Bnew,i1) = p1;
     177      535827 :       for (i=1; i<=nlze; i++) gel(p1,i) = gcoeff(dep,i,j);
     178      305158 :       p1 += nlze;
     179             :     }
     180             :     else
     181             :     {
     182      364918 :       j1++; C[j1+zc] = Cnew[j+zc];
     183      364918 :       p1 = cgetg(lnz+1,t_COL); gel(Hnew,j1) = p1;
     184      364918 :       depnew[j1] = dep[j];
     185             :     }
     186     2561781 :     for (i=k=1; k<=lnz; i++)
     187     1891705 :       if (!diagH1[i]) p1[k++] = z[i];
     188             :   }
     189     3193488 :   for (j=s+1; j<co-col; j++)
     190             :   {
     191     2968012 :     GEN z = gel(B,j-s);
     192     2968012 :     p1 = cgetg(lig+1,t_COL); gel(Bnew,j) = p1;
     193     5208760 :     for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
     194     2968003 :     z += nlze; p1 += nlze;
     195    12975174 :     for (i=k=1; k<=lnz; i++)
     196    10007171 :       if (!diagH1[i]) gel(p1,k++) = gel(z,i);
     197             :   }
     198      225476 :   *ptdep = depnew;
     199      225476 :   *ptC = C;
     200      225476 :   *ptB = Bnew; return Hnew;
     201             : }
     202             : 
     203             : /* for debugging */
     204             : static void
     205           0 : p_mat(GEN mat, GEN perm, long k)
     206             : {
     207           0 :   pari_sp av = avma;
     208           0 :   perm = vecslice(perm, k+1, lg(perm)-1);
     209           0 :   err_printf("Permutation: %Ps\n",perm);
     210           0 :   if (DEBUGLEVEL > 6)
     211           0 :     err_printf("matgen = %Ps\n", zm_to_ZM( rowpermute(mat, perm) ));
     212           0 :   set_avma(av);
     213           0 : }
     214             : 
     215             : static GEN
     216     2609133 : col_dup(long l, GEN col)
     217             : {
     218     2609133 :   GEN c = new_chunk(l);
     219     2609126 :   memcpy(c,col,l * sizeof(long)); return c;
     220             : }
     221             : 
     222             : /* permutation giving imagecompl(x') | image(x'), x' = transpose of x */
     223             : static GEN
     224      225483 : ZM_rowrankprofile(GEN x, long *nlze)
     225             : {
     226      225483 :   pari_sp av = avma;
     227             :   GEN d, y;
     228             :   long i, j, k, l, r;
     229             : 
     230      225483 :   x = shallowtrans(x); l = lg(x);
     231      225483 :   (void)new_chunk(l); /* HACK */
     232      225483 :   d = ZM_pivots(x,&r); set_avma(av);
     233      225485 :   *nlze = r;
     234      225485 :   if (!d) return identity_perm(l-1);
     235      215378 :   y = cgetg(l,t_VECSMALL);
     236      893165 :   for (i = j = 1, k = r+1; i<l; i++)
     237      677788 :     if (d[i]) y[k++] = i; else y[j++] = i;
     238      215377 :   return y;
     239             : }
     240             : 
     241             : /* HNF reduce a relation matrix (column operations + row permutation)
     242             : ** Input:
     243             : **   mat = (li-1) x (co-1) matrix of long
     244             : **   C   = r x (co-1) matrix of GEN
     245             : **   perm= permutation vector (length li-1), indexing the rows of mat: easier
     246             : **     to maintain perm than to copy rows. For columns we can do it directly
     247             : **     using e.g. swap(mat[i], mat[j])
     248             : **   k0 = integer. The k0 first lines of mat are dense, the others are sparse.
     249             : ** Output: cf ASCII art in the function body
     250             : **
     251             : ** row permutations applied to perm
     252             : ** column operations applied to C. IN PLACE
     253             : **/
     254             : GEN
     255      134279 : hnfspec_i(GEN mat0, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, long k0)
     256             : {
     257             :   pari_sp av;
     258             :   long co, n, s, nlze, lnz, nr, i, j, k, lk0, col, lig, *p;
     259             :   GEN mat;
     260             :   GEN p1, p2, matb, matbnew, vmax, matt, T, extramat, B, C, H, dep, permpro;
     261      134279 :   const long li = lg(perm); /* = lgcols(mat0) */
     262      134279 :   const long CO = lg(mat0);
     263             : 
     264      134279 :   n = 0; /* -Wall */
     265             : 
     266      134279 :   C = *ptC; co = CO;
     267      134279 :   if (co > 300 && co > 1.5 * li)
     268             :   { /* treat the rest at the end */
     269           0 :     co = (long)(1.2 * li);
     270           0 :     setlg(C, co);
     271             :   }
     272             : 
     273      134279 :   if (DEBUGLEVEL>5)
     274             :   {
     275           0 :     err_printf("Entering hnfspec\n");
     276           0 :     p_mat(mat0,perm,0);
     277             :   }
     278      134279 :   matt = cgetg(co, t_MAT); /* dense part of mat (top) */
     279      134279 :   mat = cgetg(co, t_MAT);
     280     2743406 :   for (j = 1; j < co; j++)
     281             :   {
     282     2609127 :     GEN matj = col_dup(li, gel(mat0,j));
     283     2609137 :     p1 = cgetg(k0+1,t_COL); gel(matt,j) = p1; gel(mat,j) = matj;
     284    12286249 :     for (i=1; i<=k0; i++) gel(p1,i) = stoi(matj[perm[i]]);
     285             :   }
     286      134279 :   av = avma;
     287             : 
     288      134279 :   i = lig = li-1; col = co-1; lk0 = k0;
     289      134279 :   T = (k0 || (lg(C) > 1 && lgcols(C) > 1))? matid(col): NULL;
     290             :   /* Look for lines with a single nonzero entry, equal to 1 in absolute value */
     291     6385992 :   while (i > lk0 && col)
     292     6251698 :     switch( count(mat,perm[i],col,&n) )
     293             :     {
     294       26206 :       case 0: /* move zero lines between k0+1 and lk0 */
     295       26206 :         lk0++; lswap(perm[i], perm[lk0]);
     296       26206 :         i = lig; continue;
     297             : 
     298      484561 :       case 1: /* move trivial generator between lig+1 and li */
     299      484561 :         lswap(perm[i], perm[lig]);
     300      484561 :         if (T) swap(gel(T,n), gel(T,col));
     301      484561 :         swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     302      484561 :         if (p[perm[lig]] < 0) /* = -1 */
     303             :         { /* convert relation -g = 0 to g = 0 */
     304    10573221 :           for (i=lk0+1; i<lig; i++) p[perm[i]] = -p[perm[i]];
     305      437105 :           if (T)
     306             :           {
     307      437105 :             p1 = gel(T,col);
     308    11274128 :             for (i=1; ; i++) /* T = permuted identity: single nonzero entry */
     309    11274128 :               if (signe(gel(p1,i))) { togglesign_safe(&gel(p1,i)); break; }
     310             :           }
     311             :         }
     312      484572 :         lig--; col--; i = lig; continue;
     313             : 
     314     5740934 :       default: i--;
     315             :     }
     316      134294 :   if (DEBUGLEVEL>5) { err_printf("    after phase1:\n"); p_mat(mat,perm,0); }
     317             : 
     318             : #define absmax(s,z) {long _z; _z = labs(z); if (_z > s) s = _z;}
     319             :   /* Get rid of all lines containing only 0 and +/- 1, keeping track of column
     320             :    * operations in T. Leave the rows 1..lk0 alone [up to k0, coefficient
     321             :    * explosion, between k0+1 and lk0, row is 0] */
     322      134279 :   s = 0;
     323      775777 :   while (lig > lk0 && col && s < (long)(HIGHBIT>>1))
     324             :   {
     325     1974101 :     for (i=lig; i>lk0; i--)
     326     1849188 :       if (count(mat,perm[i],col,&n) > 0) break;
     327      766429 :     if (i == lk0) break;
     328             : 
     329             :     /* only 0, +/- 1 entries, at least 2 of them nonzero */
     330      641524 :     lswap(perm[i], perm[lig]);
     331      641524 :     swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     332      641524 :     if (T) swap(gel(T,n), gel(T,col));
     333      641524 :     if (p[perm[lig]] < 0)
     334             :     {
     335     7800686 :       for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
     336      416797 :       if (T) ZV_togglesign(gel(T,col));
     337             :     }
     338    21442079 :     for (j=1; j<col; j++)
     339             :     {
     340    20800581 :       GEN matj = gel(mat,j);
     341             :       long t;
     342    20800581 :       if (! (t = matj[perm[lig]]) ) continue;
     343     1512779 :       if (t == 1) {
     344    26306407 :         for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= p[perm[i]]);
     345             :       }
     346             :       else { /* t = -1 */
     347    19415779 :         for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] += p[perm[i]]);
     348             :       }
     349     1512779 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
     350             :     }
     351      641498 :     lig--; col--;
     352      641498 :     if (gc_needed(av,3))
     353             :     {
     354           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[1]");
     355           0 :       if (T) T = gerepilecopy(av, T); else set_avma(av);
     356             :     }
     357             :   }
     358             :   /* As above with lines containing a +/- 1 (no other assumption).
     359             :    * Stop when single precision becomes dangerous */
     360      134264 :   vmax = cgetg(co,t_VECSMALL);
     361     1617481 :   for (j=1; j<=col; j++)
     362             :   {
     363     1483201 :     GEN matj = gel(mat,j);
     364     7079418 :     for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[i]);
     365     1483201 :     vmax[j] = s;
     366             :   }
     367      425721 :   while (lig > lk0 && col)
     368             :   {
     369      447970 :     for (i=lig; i>lk0; i--)
     370      420431 :       if ( (n = count2(mat,perm[i],col)) ) break;
     371      318981 :     if (i == lk0) break;
     372             : 
     373      291442 :     lswap(vmax[n], vmax[col]);
     374      291442 :     lswap(perm[i], perm[lig]);
     375      291442 :     swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     376      291442 :     if (T) swap(gel(T,n), gel(T,col));
     377      291442 :     if (p[perm[lig]] < 0)
     378             :     {
     379      593544 :       for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
     380       95717 :       if (T) ZV_togglesign(gel(T,col));
     381             :     }
     382     3892815 :     for (j=1; j<col; j++)
     383             :     {
     384     3601422 :       GEN matj = gel(mat,j);
     385             :       long t;
     386     3601422 :       if (! (t = matj[perm[lig]]) ) continue;
     387     1723629 :       if (vmax[col] && (ulong)labs(t) >= (HIGHBIT-vmax[j]) / vmax[col])
     388           0 :         goto END2;
     389             : 
     390    20122788 :       for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= t*p[perm[i]]);
     391     1723629 :       vmax[j] = s;
     392     1723629 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
     393             :     }
     394      291393 :     lig--; col--;
     395      291393 :     if (gc_needed(av,3))
     396             :     {
     397           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[2]");
     398           0 :       gerepileall(av, T? 2: 1, &vmax, &T);
     399             :     }
     400             :   }
     401             : 
     402      106741 : END2: /* clean up mat: remove everything to the right of the 1s on diagonal */
     403             :   /* go multiprecision first */
     404      134280 :   matb = cgetg(co,t_MAT); /* bottom part (complement of matt) */
     405     2743514 :   for (j=1; j<co; j++)
     406             :   {
     407     2609191 :     GEN matj = gel(mat,j);
     408     2609191 :     p1 = cgetg(li-k0,t_COL); gel(matb,j) = p1;
     409     2609185 :     p1 -= k0;
     410    82476432 :     for (i=k0+1; i<li; i++) gel(p1,i) = stoi(matj[perm[i]]);
     411             :   }
     412      134323 :   if (DEBUGLEVEL>5)
     413             :   {
     414           0 :     err_printf("    after phase2:\n");
     415           0 :     p_mat(mat,perm,lk0);
     416             :   }
     417     1417973 :   for (i=li-2; i>lig; i--)
     418             :   {
     419     1283697 :     long h, i0 = i - k0, k = i + co-li;
     420     1283697 :     GEN Bk = gel(matb,k);
     421    29089044 :     for (j=k+1; j<co; j++)
     422             :     {
     423    27805500 :       GEN Bj = gel(matb,j), v = gel(Bj,i0);
     424    27805500 :       s = signe(v); if (!s) continue;
     425             : 
     426     5390903 :       gel(Bj,i0) = gen_0;
     427     5390903 :       if (is_pm1(v))
     428             :       {
     429     3081783 :         if (s > 0) /* v = 1 */
     430    46870803 :         { for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), gel(Bk,h)); }
     431             :         else /* v = -1 */
     432    40312556 :         { for (h=1; h<i0; h++) gel(Bj,h) = addii(gel(Bj,h), gel(Bk,h)); }
     433             :       }
     434             :       else {
     435    55615907 :         for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), mulii(v,gel(Bk,h)));
     436             :       }
     437     5390722 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,k), negi(v));
     438             :     }
     439     1283544 :     if (gc_needed(av,2))
     440             :     {
     441           6 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[3], i = %ld", i);
     442        1590 :       for (h=1; h<co; h++) setlg(matb[h], i0+1); /* bottom can be forgotten */
     443           6 :       gerepileall(av, T? 2: 1, &matb, &T);
     444             :     }
     445             :   }
     446     2743468 :   for (j=1; j<co; j++) setlg(matb[j], lig-k0+1); /* bottom can be forgotten */
     447      134275 :   gerepileall(av, T? 2: 1, &matb, &T);
     448      134280 :   if (DEBUGLEVEL>5) err_printf("    matb cleaned up (using Id block)\n");
     449             : 
     450      134280 :   nlze = lk0 - k0;  /* # of 0 rows */
     451      134280 :   lnz = lig-nlze+1; /* 1 + # of nonzero rows (!= 0...0 1 0 ... 0) */
     452      134280 :   if (T) matt = ZM_mul(matt,T); /* update top rows */
     453      134278 :   extramat = cgetg(col+1,t_MAT); /* = new C minus the 0 rows */
     454     1326008 :   for (j=1; j<=col; j++)
     455             :   {
     456     1191731 :     GEN z = gel(matt,j);
     457     1191731 :     GEN t = (gel(matb,j)) + nlze - k0;
     458     1191731 :     p2=cgetg(lnz,t_COL); gel(extramat,j) = p2;
     459     5094435 :     for (i=1; i<=k0; i++) gel(p2,i) = gel(z,i); /* top k0 rows */
     460     1989104 :     for (   ; i<lnz; i++) gel(p2,i) = gel(t,i); /* other nonzero rows */
     461             :   }
     462      134277 :   if (!col) {
     463           0 :     permpro = identity_perm(lnz);
     464           0 :     nr = lnz;
     465             :   }
     466             :   else
     467      134277 :     permpro = ZM_rowrankprofile(extramat, &nr);
     468             :   /* lnz = lg(permpro) */
     469      134279 :   if (nlze)
     470             :   { /* put the nlze 0 rows (trivial generators) at the top */
     471       11563 :     p1 = new_chunk(lk0+1);
     472       37769 :     for (i=1; i<=nlze; i++) p1[i] = perm[i + k0];
     473       60111 :     for (   ; i<=lk0; i++)  p1[i] = perm[i - nlze];
     474       86317 :     for (i=1; i<=lk0; i++)  perm[i] = p1[i];
     475             :   }
     476             :   /* sort other rows according to permpro (nr redundant generators first) */
     477      134279 :   p1 = new_chunk(lnz); p2 = perm + nlze;
     478      589946 :   for (i=1; i<lnz; i++) p1[i] = p2[permpro[i]];
     479      589946 :   for (i=1; i<lnz; i++) p2[i] = p1[i];
     480             :   /* perm indexes the rows of mat
     481             :    *   |_0__|__redund__|__dense__|__too big__|_____done______|
     482             :    *   0  nlze                              lig             li
     483             :    *         \___nr___/ \___k0__/
     484             :    *         \____________lnz ______________/
     485             :    *
     486             :    *               col   co
     487             :    *       [dep     |     ]
     488             :    *    i0 [--------|  B  ] (i0 = nlze + nr)
     489             :    *       [matbnew |     ] matbnew has maximal rank = lnz-1 - nr
     490             :    * mat = [--------|-----] lig
     491             :    *       [   0    | Id  ]
     492             :    *       [        |     ] li */
     493             : 
     494      134279 :   matbnew = cgetg(col+1,t_MAT); /* dense+toobig, maximal rank. For hnffinal */
     495      134279 :   dep    = cgetg(col+1,t_MAT); /* rows dependent from the ones in matbnew */
     496     1326013 :   for (j=1; j<=col; j++)
     497             :   {
     498     1191733 :     GEN z = gel(extramat,j);
     499     1191733 :     p1 = cgetg(nlze+nr+1,t_COL); gel(dep,j) = p1;
     500     1191735 :     p2 = cgetg(lnz-nr,t_COL); gel(matbnew,j) = p2;
     501     1550478 :     for (i=1; i<=nlze; i++) gel(p1,i) = gen_0;
     502     1260602 :     p1 += nlze; for (i=1; i<=nr; i++) p1[i] = z[permpro[i]];
     503     5822913 :     p2 -= nr;   for (   ; i<lnz; i++) p2[i] = z[permpro[i]];
     504             :   }
     505             : 
     506             :   /* redundant generators in terms of the genuine generators
     507             :    * (x_i) = - (g_i) B */
     508      134280 :   B = cgetg(co-col,t_MAT);
     509     1551782 :   for (j=col+1; j<co; j++)
     510             :   {
     511     1417504 :     GEN y = gel(matt,j);
     512     1417504 :     GEN z = gel(matb,j);
     513     1417504 :     p1=cgetg(lig+1,t_COL); gel(B,j-col) = p1;
     514     2979254 :     for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
     515     1417503 :     p1 += nlze; z += nlze-k0;
     516    10119067 :     for (k=1; k<lnz; k++)
     517             :     {
     518     8701564 :       i = permpro[k];
     519     8701564 :       gel(p1,k) = (i <= k0)? gel(y,i): gel(z,i);
     520             :     }
     521             :   }
     522      134278 :   if (T) C = typ(C)==t_MAT? RgM_ZM_mul(C,T): RgV_RgM_mul(C,T);
     523      134278 :   gerepileall(av, 4, &matbnew, &B, &dep, &C);
     524      134280 :   *ptdep = dep;
     525      134280 :   *ptB = B;
     526      134280 :   H = hnffinal(matbnew, perm, ptdep, ptB, &C);
     527      134280 :   if (CO > co)
     528             :   { /* treat the rest, N cols at a time (hnflll slow otherwise) */
     529           0 :     const long N = 300;
     530           0 :     long a, L = CO - co, l = minss(L, N); /* L columns to add */
     531           0 :     GEN CC = *ptC, m0 = mat0;
     532           0 :     setlg(CC, CO); /* restore */
     533           0 :     CC += co-1;
     534           0 :     m0 += co-1;
     535           0 :     for (a = l;;)
     536           0 :     {
     537             :       GEN MAT, emb;
     538           0 :       gerepileall(av, 4, &H,&C,ptB,ptdep);
     539           0 :       MAT = cgetg(l + 1, t_MAT);
     540           0 :       emb = cgetg(l + 1, typ(C));
     541           0 :       for (j = 1 ; j <= l; j++)
     542             :       {
     543           0 :         gel(MAT,j) = gel(m0,j);
     544           0 :         emb[j] = CC[j];
     545             :       }
     546           0 :       H = hnfadd_i(H, perm, ptdep, ptB, &C, MAT, emb);
     547           0 :       if (a == L) break;
     548           0 :       CC += l;
     549           0 :       m0 += l;
     550           0 :       a += l; if (a > L) { l = L - (a - l); a = L; }
     551             :     }
     552             :   }
     553      134280 :   *ptC = C; return H;
     554             : }
     555             : 
     556             : GEN
     557           0 : hnfspec(GEN mat, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, long k0)
     558             : {
     559           0 :   pari_sp av = avma;
     560           0 :   GEN H = hnfspec_i(mat, perm, ptdep, ptB, ptC, k0);
     561           0 :   return gc_all(av, 4, &H, ptC, ptdep, ptB);
     562             : }
     563             : 
     564             : /* HNF reduce x, apply same transforms to C */
     565             : GEN
     566           0 : mathnfspec(GEN x, GEN *pperm, GEN *pdep, GEN *pB, GEN *pC)
     567             : {
     568           0 :   long i, j, k, l, n, ly, lx = lg(x);
     569             :   GEN z, v1, perm;
     570           0 :   if (lx == 1) return cgetg(1, t_MAT);
     571           0 :   ly = lgcols(x);
     572           0 :   *pperm = perm = identity_perm(ly-1);
     573           0 :   z = cgetg(lx,t_MAT);
     574           0 :   for (i=1; i<lx; i++)
     575             :   {
     576           0 :     GEN C = cgetg(ly,t_COL), D = gel(x,i);
     577           0 :     gel(z,i) = C;
     578           0 :     for (j=1; j<ly; j++)
     579             :     {
     580           0 :       GEN d = gel(D,j);
     581           0 :       if (is_bigint(d)) goto TOOLARGE;
     582           0 :       C[j] = itos(d);
     583             :     }
     584             :   }
     585             :   /*  [ dep |     ]
     586             :    *  [-----|  B  ]
     587             :    *  [  H  |     ]
     588             :    *  [-----|-----]
     589             :    *  [  0  | Id  ] */
     590           0 :   return hnfspec(z,perm, pdep, pB, pC, 0);
     591             : 
     592           0 : TOOLARGE:
     593           0 :   if (lg(*pC) > 1 && lgcols(*pC) > 1)
     594           0 :     pari_err_IMPL("mathnfspec with large entries");
     595           0 :   x = ZM_hnf(x); lx = lg(x);
     596           0 :   v1 = cgetg(ly, t_VECSMALL);
     597           0 :   n = lx - ly;
     598           0 :   for (i = k = l = 1; i < ly; i++)
     599           0 :     if (equali1(gcoeff(x,i,i + n))) v1[l++] = i; else perm[k++] = i;
     600           0 :   setlg(perm, k);
     601           0 :   setlg(v1, l);
     602           0 :   x = rowpermute(x, perm); /* upper part */
     603           0 :   *pperm = vecsmall_concat(perm, v1);
     604           0 :   *pB = vecslice(x, k+n, lx-1);
     605           0 :   setlg(x, k);
     606           0 :   *pdep = rowslice(x, 1, n);
     607           0 :   return n? rowslice(x, n+1, k-1): x; /* H */
     608             : }
     609             : 
     610             : /* add new relations to a matrix treated by hnfspec (extramat / extraC) */
     611             : GEN
     612       91207 : hnfadd_i(GEN H, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, /* cf hnfspec */
     613             :        GEN extramat,GEN extraC)
     614             : {
     615       91207 :   GEN matb, extratop, Cnew, permpro, B = *ptB, C = *ptC, dep = *ptdep;
     616             :   long i, lH, lB, li, lig, co, col, nlze;
     617             : 
     618       91207 :   if (lg(extramat) == 1) return H;
     619       91207 :   co = lg(C)-1;
     620       91207 :   lH = lg(H)-1;
     621       91207 :   lB = lg(B)-1;
     622       91207 :   li = lg(perm)-1;
     623       91207 :   lig = li - lB;
     624       91207 :   col = co - lB;
     625       91207 :   nlze = lig - lH;
     626             : 
     627             :  /*               col    co
     628             :   *       [ 0 |dep |     ]
     629             :   *  nlze [--------|  B  ]
     630             :   *       [ 0 | H  |     ]
     631             :   *       [--------|-----] lig
     632             :   *       [   0    | Id  ]
     633             :   *       [        |     ] li */
     634       91207 :   extratop = zm_to_ZM( rowslicepermute(extramat, perm, 1, lig) );
     635       91207 :   if (li != lig)
     636             :   { /* zero out bottom part, using the Id block */
     637       91039 :     GEN A = vecslice(C, col+1, co);
     638       91039 :     GEN c = rowslicepermute(extramat, perm, lig+1, li);
     639       91039 :     extraC   = gsub(extraC, typ(A)==t_MAT? RgM_zm_mul(A, c): RgV_zm_mul(A,c));
     640       91039 :     extratop = ZM_sub(extratop, ZM_zm_mul(B, c));
     641             :   }
     642             : 
     643       91204 :   extramat = shallowconcat(extratop, vconcat(dep, H));
     644       91206 :   Cnew     = shallowconcat(extraC, vecslice(C, col-lH+1, co));
     645       91204 :   if (DEBUGLEVEL>5) err_printf("    1st phase done\n");
     646       91204 :   permpro = ZM_rowrankprofile(extramat, &nlze);
     647       91206 :   extramat = rowpermute(extramat, permpro);
     648       91207 :   *ptB     = rowpermute(B,        permpro);
     649       91207 :   permpro = vecsmallpermute(perm, permpro);
     650      313330 :   for (i=1; i<=lig; i++) perm[i] = permpro[i]; /* perm o= permpro */
     651             : 
     652       91207 :   *ptdep  = rowslice(extramat, 1, nlze);
     653       91207 :   matb    = rowslice(extramat, nlze+1, lig);
     654       91207 :   if (DEBUGLEVEL>5) err_printf("    2nd phase done\n");
     655       91207 :   H = hnffinal(matb,perm,ptdep,ptB,&Cnew);
     656       91207 :   *ptC = shallowconcat(vecslice(C, 1, col-lH), Cnew);
     657       91207 :   return H;
     658             : }
     659             : 
     660             : GEN
     661           0 : hnfadd(GEN H, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, /* cf hnfspec */
     662             :        GEN extramat,GEN extraC)
     663             : {
     664           0 :   pari_sp av = avma;
     665           0 :   H = hnfadd_i(H, perm, ptdep, ptB, ptC, ZM_to_zm(extramat), extraC);
     666           0 :   return gc_all(av, 4, &H, ptC, ptdep, ptB);
     667             : }
     668             : 
     669             : /* zero aj = Aij (!= 0)  using  ak = Aik (maybe 0), via linear combination of
     670             :  * A[j] and A[k] of determinant 1. If U != NULL, likewise update its columns */
     671             : static void
     672    39794988 : ZC_elem(GEN aj, GEN ak, GEN A, GEN U, long j, long k)
     673             : {
     674             :   GEN p1,u,v,d;
     675             : 
     676    39794988 :   if (!signe(ak)) {
     677      107330 :     swap(gel(A,j), gel(A,k));
     678      107330 :     if (U) swap(gel(U,j), gel(U,k));
     679    36092096 :     return;
     680             :   }
     681    39687658 :   d = bezout(aj,ak,&u,&v);
     682             :   /* frequent special case (u,v) = (1,0) or (0,1) */
     683    39692561 :   if (!signe(u))
     684             :   { /* ak | aj */
     685    19545454 :     p1 = diviiexact(aj,ak); togglesign(p1);
     686    19546033 :     ZC_lincomb1_inplace(gel(A,j), gel(A,k), p1);
     687    19554520 :     if (U)
     688     2266225 :       ZC_lincomb1_inplace(gel(U,j), gel(U,k), p1);
     689    19554542 :     return;
     690             :   }
     691    20147107 :   if (!signe(v))
     692             :   { /* aj | ak */
     693    16432142 :     p1 = diviiexact(ak,aj); togglesign(p1);
     694    16430665 :     ZC_lincomb1_inplace(gel(A,k), gel(A,j), p1);
     695    16430224 :     swap(gel(A,j), gel(A,k));
     696    16430224 :     if (U) {
     697      372040 :       ZC_lincomb1_inplace(gel(U,k), gel(U,j), p1);
     698      372040 :       swap(gel(U,j), gel(U,k));
     699             :     }
     700    16430224 :     return;
     701             :   }
     702             : 
     703     3714965 :   if (!is_pm1(d)) { aj = diviiexact(aj, d); ak = diviiexact(ak, d); }
     704     3714939 :   p1 = gel(A,k); aj = negi(aj); /* NOT togglesign */
     705     3715231 :   gel(A,k) = ZC_lincomb(u,v, gel(A,j),p1);
     706     3714668 :   gel(A,j) = ZC_lincomb(aj,ak, p1,gel(A,j));
     707     3714574 :   if (U)
     708             :   {
     709      750042 :     p1 = gel(U,k);
     710      750042 :     gel(U,k) = ZC_lincomb(u,v, gel(U,j),p1);
     711      750128 :     gel(U,j) = ZC_lincomb(aj,ak, p1,gel(U,j));
     712             :   }
     713             : }
     714             : 
     715             : INLINE int
     716        2590 : is_RgX(GEN a, long v) { return typ(a) == t_POL && varn(a)==v; }
     717             : /* set u,v such that au + bv = gcd(a,b), divide a,b by the gcd */
     718             : static GEN
     719         882 : gbezout_step(GEN *pa, GEN *pb, GEN *pu, GEN *pv, long vx)
     720             : {
     721         882 :   GEN a = *pa, b = *pb, d, l;
     722         882 :   if (gequal0(a))
     723             :   {
     724           0 :     *pa = gen_0; *pu = gen_0;
     725           0 :     *pb = gen_1; *pv = gen_1; return b;
     726             :   }
     727         882 :   a = is_RgX(a,vx)? RgX_renormalize(a): scalarpol(a, vx);
     728         882 :   b = is_RgX(b,vx)? RgX_renormalize(b): scalarpol(b, vx);
     729         882 :   d = RgX_extgcd(a,b, pu,pv);
     730         882 :   l = pollead(d,vx);
     731         882 :   d = RgX_Rg_div(d,l);
     732         882 :   *pu = RgX_Rg_div(*pu,l);
     733         882 :   *pv = RgX_Rg_div(*pv,l);
     734         882 :   if (degpol(d)) { a = RgX_div(a, d); b = RgX_div(b, d); }
     735         238 :   else if (typ(gel(d,2)) == t_REAL && lg(gel(d,2)) <= 3)
     736             : #if 1
     737             :   { /* possible accuracy problem */
     738           0 :     GEN D = RgX_gcd_simple(a,b);
     739           0 :     if (degpol(D)) {
     740           0 :       D = RgX_normalize(D);
     741           0 :       a = RgX_div(a, D);
     742           0 :       b = RgX_div(b, D);
     743           0 :       d = RgX_extgcd(a,b, pu,pv); /* retry now */
     744           0 :       d = RgX_mul(d, D);
     745             :     }
     746             :   }
     747             : #else
     748             :   { /* less stable */
     749             :     d = RgX_extgcd_simple(a,b, pu,pv);
     750             :     if (degpol(d)) { a = RgX_div(a, d); b = RgX_div(b, d); }
     751             :   }
     752             : #endif
     753         882 :   *pa = a;
     754         882 :   *pb = b; return d;
     755             : }
     756             : static GEN
     757     2698085 : col_mul(GEN x, GEN c)
     758             : {
     759     2698085 :   if (typ(x) == t_INT)
     760             :   {
     761     2696073 :     long s = signe(x);
     762     2696073 :     if (!s) return NULL;
     763     2044048 :     if (is_pm1(x)) return (s > 0)? c: RgC_neg(c);
     764             :   }
     765      308150 :   return RgC_Rg_mul(c, x);
     766             : }
     767             : static void
     768           0 : do_zero(GEN x)
     769             : {
     770           0 :   long i, lx = lg(x);
     771           0 :   for (i=1; i<lx; i++) gel(x,i) = gen_0;
     772           0 : }
     773             : 
     774             : /* (c1, c2) *= [u,-b; v,a] */
     775             : static void
     776      674535 : update(GEN u, GEN v, GEN a, GEN b, GEN *c1, GEN *c2)
     777             : {
     778             :   GEN p1,p2;
     779             : 
     780      674535 :   u = col_mul(u,*c1);
     781      674560 :   v = col_mul(v,*c2);
     782      674586 :   if (u) p1 = v? gadd(u,v): u;
     783        6494 :   else   p1 = v? v: NULL;
     784             : 
     785      674584 :   a = col_mul(a,*c2);
     786      674620 :   b = col_mul(gneg_i(b),*c1);
     787      674617 :   if (a) p2 = b? RgC_add(a,b): a;
     788           0 :   else   p2 = b? b: NULL;
     789             : 
     790      674562 :   if (!p1) do_zero(*c1); else *c1 = p1;
     791      674562 :   if (!p2) do_zero(*c2); else *c2 = p2;
     792      674562 : }
     793             : 
     794             : /* zero aj = Aij (!= 0)  using  ak = Aik (maybe 0), via linear combination of
     795             :  * A[j] and A[k] of determinant 1. If U != NULL, likewise update its columns */
     796             : static void
     797         511 : RgC_elem(GEN aj, GEN ak, GEN A, GEN V, long j, long k, long li, long vx)
     798             : {
     799         511 :   GEN u,v, d = gbezout_step(&aj, &ak, &u, &v, vx);
     800             :   long l;
     801             :   /* (A[,k], A[,j]) *= [v, -aj; u, ak ] */
     802        1778 :   for (l = 1; l < li; l++)
     803             :   {
     804        1267 :     GEN t = gadd(gmul(u,gcoeff(A,l,j)), gmul(v,gcoeff(A,l,k)));
     805        1267 :     gcoeff(A,l,j) = gsub(gmul(ak,gcoeff(A,l,j)), gmul(aj,gcoeff(A,l,k)));
     806        1267 :     gcoeff(A,l,k) = t;
     807             :   }
     808         511 :   gcoeff(A,li,j) = gen_0;
     809         511 :   gcoeff(A,li,k) = d;
     810         511 :   if (V) update(v,u,ak,aj,(GEN*)(V+k),(GEN*)(V+j));
     811         511 : }
     812             : 
     813             : /* reduce A[i,j] mod A[i,j0] for j=j0+1... via column operations */
     814             : static void
     815     4142181 : ZM_reduce(GEN A, GEN U, long i, long j0)
     816             : {
     817     4142181 :   long j, lA = lg(A);
     818     4142181 :   GEN d = gcoeff(A,i,j0);
     819     4142181 :   if (signe(d) < 0)
     820             :   {
     821       29586 :     ZV_neg_inplace(gel(A,j0));
     822       29586 :     if (U) ZV_togglesign(gel(U,j0));
     823       29586 :     d = gcoeff(A,i,j0);
     824             :   }
     825     8264874 :   for (j=j0+1; j<lA; j++)
     826             :   {
     827     4122648 :     GEN q = truedivii(gcoeff(A,i,j), d);
     828     4122680 :     if (!signe(q)) continue;
     829             : 
     830      257772 :     togglesign(q);
     831      257866 :     ZC_lincomb1_inplace(gel(A,j), gel(A,j0), q);
     832      257869 :     if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,j0), q);
     833             :   }
     834     4142226 : }
     835             : 
     836             : /* normalize T as if it were a t_POL in variable v */
     837             : static GEN
     838         364 : normalize_as_RgX(GEN T, long v, GEN *pd)
     839             : {
     840             :   GEN d;
     841         364 :   if (!is_RgX(T,v)) { *pd = T; return gen_1; }
     842         336 :   d = leading_coeff(T);
     843         336 :   while (gequal0(d) || (typ(d) == t_REAL && lg(d) == 3
     844           0 :                         && gexpo(T) - expo(d) > (long)BITS_IN_LONG)) {
     845          14 :     T = normalizepol_lg(T, lg(T)-1);
     846          14 :     if (!signe(T)) { *pd = gen_1; return T; }
     847           0 :     d = leading_coeff(T);
     848             :   }
     849         322 :   if (degpol(T)) T = RgX_Rg_div(T,d); else { d = gel(T,2); T = gen_1; }
     850         322 :   *pd = d; return T;
     851             : }
     852             : /* reduce A[i,j] mod A[i,j0] for j=j0+1... via column operations */
     853             : static void
     854          84 : RgM_reduce(GEN A, GEN U, long i, long j0, long vx)
     855             : {
     856          84 :   long j, lA = lg(A);
     857          84 :   GEN d, T = normalize_as_RgX(gcoeff(A,i,j0), vx, &d);
     858          84 :   if (U && !gequal1(d)) gel(U,j0) = RgC_Rg_div(gel(U,j0), d);
     859          84 :   gcoeff(A,i,j0) = T;
     860             : 
     861         196 :   for (j=j0+1; j<lA; j++)
     862             :   {
     863         112 :     GEN t = gcoeff(A,i,j), q;
     864         112 :     if (gequal0(t)) continue;
     865          14 :     if (T == gen_1)
     866           0 :       q = t;
     867          14 :     else if (is_RgX(t,vx))
     868          14 :       q = RgX_div(t, T);
     869           0 :     else continue;
     870             : 
     871          14 :     if (gequal0(q)) continue;
     872           7 :     gel(A,j) = RgC_sub(gel(A,j), RgC_Rg_mul(gel(A,j0), q));
     873           7 :     if (U) gel(U,j) = RgC_sub(gel(U,j), RgC_Rg_mul(gel(U,j0), q));
     874             :   }
     875          84 : }
     876             : 
     877             : /* A,B square integral in upper HNF, of the same dimension > 0. Return Au
     878             :  * in Z^n (v in Z^n not computed), such that Au + Bv = [1, 0, ..., 0] */
     879             : GEN
     880      743789 : hnfmerge_get_1(GEN A, GEN B)
     881             : {
     882      743789 :   pari_sp av = avma;
     883      743789 :   long j, k, l = lg(A), lb;
     884      743789 :   GEN b, U = cgetg(l + 1, t_MAT), C = cgetg(l + 1, t_VEC);
     885             : 
     886      743835 :   b = gcoeff(B,1,1); lb = lgefint(b);
     887     1510772 :   for (j = 1; j < l; j++)
     888             :   {
     889             :     GEN t;
     890     1510768 :     long c = j+1;
     891     1510768 :     gel(U,j) = col_ei(l-1, j);
     892     1510876 :     gel(U,c) = zerocol(l-1); /* dummy */
     893     1510868 :     gel(C,j) = vecslice(gel(A,j), 1,j);
     894     1510879 :     gel(C,c) = vecslice(gel(B,j), 1,j);
     895     4195821 :     for (k = j; k > 0; k--)
     896             :     {
     897     2685423 :       t = gcoeff(C,k,c);
     898     2685423 :       if (gequal0(t)) continue;
     899     2479807 :       setlg(C[c], k+1);
     900     2479748 :       ZC_elem(t, gcoeff(C,k,k), C, U, c, k);
     901     2478936 :       if (lgefint(gcoeff(C,k,k)) > lb) gel(C,k) = FpC_red(gel(C,k), b);
     902     2478936 :       if (j > 4)
     903             :       {
     904       89324 :         GEN u = gel(U,k);
     905             :         long h;
     906     1337233 :         for (h=1; h<l; h++)
     907     1247909 :           if (lgefint(gel(u,h)) > lb) gel(u,h) = remii(gel(u,h), b);
     908             :       }
     909             :     }
     910     1510398 :     if (j == 1)
     911      743627 :       t = gcoeff(C,1,1);
     912             :     else
     913             :     {
     914             :       GEN u;
     915      766771 :       t = bezout(gcoeff(C,1,1), b, &u, NULL); /* >= 0 */
     916      767185 :       if (signe(u) && !equali1(u)) gel(U,1) = ZC_Z_mul(gel(U,1), u);
     917      767200 :       gcoeff(C,1,1) = t;
     918             :     }
     919     1510827 :     if (equali1(t)) break;
     920             :   }
     921      743701 :   if (j >= l) return NULL;
     922      743701 :   b = lcmii(gcoeff(A,1,1),b);
     923      743704 :   A = FpC_red(ZM_ZC_mul(A,gel(U,1)), b);
     924      743580 :   return gerepileupto(av, FpC_center(A, b, shifti(b,-1)));
     925             : }
     926             : 
     927             : /* remove the first r columns */
     928             : static void
     929      321585 : remove_0cols(long r, GEN *pA, GEN *pB, long remove)
     930             : {
     931      321585 :   GEN A = *pA, B = *pB;
     932      321585 :   long l = lg(A);
     933      321585 :   A += r; A[0] = evaltyp(t_MAT) | _evallg(l-r);
     934      321585 :   if (B && remove == 2) { B += r; B[0] = A[0]; }
     935      321585 :   *pA = A; *pB = B;
     936      321585 : }
     937             : 
     938             : /* Inefficient compared to hnfall. 'remove' = throw away lin.dep columns */
     939             : static GEN
     940       61642 : hnf_i(GEN A, int remove)
     941             : {
     942       61642 :   pari_sp av0 = avma, av;
     943             :   long s, n, m, j, k, li, def, ldef;
     944             : 
     945       61642 :   RgM_dimensions(A, &m, &n);
     946       61642 :   if (!n) return cgetg(1,t_MAT);
     947       61390 :   av = avma;
     948       61390 :   A = RgM_shallowcopy(A);
     949       61390 :   def = n; ldef = (m>n)? m-n: 0;
     950      164627 :   for (li=m; li>ldef; li--)
     951             :   {
     952      410005 :     for (j=def-1; j; j--)
     953             :     {
     954      306768 :       GEN a = gcoeff(A,li,j);
     955      306768 :       if (!signe(a)) continue;
     956             : 
     957             :       /* zero a = Aij  using  b = Aik */
     958      157749 :       k = (j==1)? def: j-1;
     959      157749 :       ZC_elem(a,gcoeff(A,li,k), A,NULL, j,k);
     960      157749 :       if (gc_needed(av,1))
     961             :       {
     962           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnf[1]. li=%ld",li);
     963           0 :         A = gerepilecopy(av, A);
     964             :       }
     965             :     }
     966      103237 :     s = signe(gcoeff(A,li,def));
     967      103237 :     if (s)
     968             :     {
     969      101395 :       if (s < 0) ZV_neg_inplace(gel(A,def));
     970      101395 :       ZM_reduce(A, NULL, li,def);
     971      101395 :       def--;
     972             :     }
     973             :     else
     974        1842 :       if (ldef) ldef--;
     975      103237 :     if (gc_needed(av,1))
     976             :     {
     977           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnf[2]. li=%ld",li);
     978           0 :       A = gerepilecopy(av, A);
     979             :     }
     980             :   }
     981             :   /* rank A = n - def */
     982       61390 :   if (remove) { GEN B = NULL; remove_0cols(def, &A, &B, remove); }
     983       61390 :   return gerepileupto(av0, ZM_copy(A));
     984             : }
     985             : 
     986             : GEN
     987       88782 : ZM_hnf(GEN x) { return lg(x) > 8? ZM_hnfall(x, NULL, 1): hnf_i(x, 1); }
     988             : 
     989             : /* u*z[1..k] mod p, in place */
     990             : static void
     991     5309115 : FpV_Fp_mul_part_ip(GEN z, GEN u, GEN p, long k)
     992             : {
     993             :   long i;
     994     5309115 :   if (is_pm1(u)) {
     995      199510 :     if (signe(u) > 0) {
     996      589872 :       for (i = 1; i <= k; i++)
     997      444962 :         if (signe(gel(z,i))) gel(z,i) = modii(gel(z,i), p);
     998             :     } else {
     999      153081 :       for (i = 1; i <= k; i++)
    1000       98483 :         if (signe(gel(z,i))) gel(z,i) = modii(negi(gel(z,i)), p);
    1001             :     }
    1002             :   }
    1003             :   else {
    1004    19280872 :     for (i = 1; i <= k; i++)
    1005    14171886 :       if (signe(gel(z,i))) gel(z,i) = Fp_mul(u,gel(z,i), p);
    1006             :   }
    1007     5308494 : }
    1008             : static void
    1009    36040744 : FpV_red_part_ipvec(GEN z, GEN p, long k)
    1010             : {
    1011             :   long i;
    1012   109293341 :   for (i = 1; i <= k; i++) gel(z,i) = modii(gel(z,i), gel(p,i));
    1013    36037035 : }
    1014             : 
    1015             : /* return x * U, in echelon form (mod p^m), where (det(U),p) = 1.
    1016             :  * If early_abort is set, return NULL as soon as one pivot is 0 (mod p^m) */
    1017             : GEN
    1018      524920 : ZpM_echelon(GEN x, long early_abort, GEN p, GEN pm)
    1019             : {
    1020      524920 :   pari_sp av0 = avma, av;
    1021             :   long m, li, co, i, j, k, def, ldef;
    1022             : 
    1023      524920 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1024      524920 :   li = lgcols(x);
    1025      524920 :   av = avma;
    1026      524920 :   x = RgM_shallowcopy(x);
    1027      524921 :   m = Z_pval(pm, p);
    1028             : 
    1029      524921 :   ldef = (li > co)? li - co: 0;
    1030     2502494 :   for (def = co-1,i = li-1; i > ldef; i--)
    1031             :   {
    1032     1978226 :     long vmin = LONG_MAX, kmin = 0;
    1033     1978226 :     GEN umin = gen_0, pvmin, q;
    1034     9653246 :     for (k = 1; k <= def; k++)
    1035             :     {
    1036     8138339 :       GEN u = gcoeff(x,i,k);
    1037             :       long v;
    1038     8138339 :       if (!signe(u)) continue;
    1039     3850543 :       v = Z_pvalrem(u, p, &u);
    1040     3850538 :       if (v >= m) gcoeff(x,i,k) = gen_0;
    1041     2717985 :       else if (v < vmin) {
    1042     1397620 :         vmin = v; kmin = k; umin = u;
    1043     1397620 :         if (!vmin) break;
    1044             :       }
    1045             :     }
    1046     1978221 :     if (!kmin)
    1047             :     {
    1048      923234 :       if (early_abort) return NULL;
    1049      922546 :       gcoeff(x,i,def) = gen_0;
    1050      922546 :       ldef--;
    1051      922546 :       if (ldef < 0) ldef = 0;
    1052      922546 :       continue;
    1053             :     }
    1054     1054987 :     if (kmin != def) swap(gel(x,def), gel(x,kmin));
    1055     1054987 :     q = vmin? powiu(p, m-vmin): pm;
    1056             :     /* pivot has valuation vmin */
    1057     1054991 :     umin = modii(umin, q);
    1058     1055000 :     if (!equali1(umin))
    1059      529598 :       FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(umin,q), pm, i-1);
    1060     1055006 :     gcoeff(x, i, def) = pvmin = powiu(p, vmin);
    1061     7106174 :     for (j = def-1; j; j--)
    1062             :     { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
    1063     6051147 :       GEN t, a = gcoeff(x,i,j) = modii(gcoeff(x,i,j), pm);
    1064     6050552 :       if (!signe(a)) continue;
    1065             : 
    1066     2836139 :       t = diviiexact(a, pvmin); togglesign(t);
    1067     2836792 :       ZC_lincomb1_inplace(gel(x,j), gel(x,def), t);
    1068     2836747 :       if (gc_needed(av,1))
    1069             :       {
    1070          14 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZpM_echelon. i=%ld",i);
    1071          14 :         x = gerepilecopy(av, x); pvmin = gcoeff(x,i,def);
    1072             :       }
    1073             :     }
    1074     1055027 :     def--;
    1075             :   }
    1076      524268 :   if (co > li)
    1077             :   {
    1078           0 :     x += co - li;
    1079           0 :     x[0] = evaltyp(t_MAT) | _evallg(li);
    1080             :   }
    1081      524268 :   return gerepilecopy(av0, x);
    1082             : }
    1083             : GEN
    1084     4657998 : zlm_echelon(GEN x, long early_abort, ulong p, ulong pm)
    1085             : {
    1086     4657998 :   pari_sp av0 = avma;
    1087             :   long li, co, i, j, k, def, ldef;
    1088             :   ulong m;
    1089             : 
    1090     4657998 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1091     4657998 :   li = lgcols(x);
    1092     4658001 :   x = Flm_copy(x);
    1093     4658166 :   m = u_lval(pm, p);
    1094             : 
    1095     4658173 :   ldef = (li > co)? li - co: 0;
    1096    20765494 :   for (def = co-1,i = li-1; i > ldef; i--)
    1097             :   {
    1098    16292651 :     long vmin = LONG_MAX, kmin = 0;
    1099    16292651 :     ulong umin = 0, pvmin, q;
    1100    49089655 :     for (k = 1; k <= def; k++)
    1101             :     {
    1102    41488907 :       ulong u = ucoeff(x,i,k);
    1103             :       long v;
    1104    41488907 :       if (!u) continue;
    1105    22667236 :       v = u_lvalrem(u, p, &u);
    1106    22667177 :       if (v >= (long) m) ucoeff(x,i,k) = 0;
    1107    22667177 :       else if (v < vmin) {
    1108    16337935 :         vmin = v; kmin = k; umin = u;
    1109    16337935 :         if (!vmin) break;
    1110             :       }
    1111             :     }
    1112    16292592 :     if (!kmin)
    1113             :     {
    1114     1014433 :       if (early_abort) return NULL;
    1115      829707 :       ucoeff(x,i,def) = 0;
    1116      829707 :       ldef--;
    1117      829707 :       if (ldef < 0) ldef = 0;
    1118      829707 :       continue;
    1119             :     }
    1120    15278159 :     if (kmin != def) swap(gel(x,def), gel(x,kmin));
    1121    15278159 :     q = vmin? upowuu(p, m-vmin): pm;
    1122             :     /* pivot has valuation vmin */
    1123    15278162 :     umin %= q;
    1124    15278162 :     if (umin != 1)
    1125     7733562 :       Flv_Fl_mul_part_inplace(gel(x,def), Fl_inv(umin,q), pm, i-1);
    1126    15278173 :     ucoeff(x, i, def) = pvmin = upowuu(p, vmin);
    1127    67514581 :     for (j = def-1; j; j--)
    1128             :     { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
    1129    52236967 :       ulong t, a = ucoeff(x,i,j);
    1130    52236967 :       if (!a) continue;
    1131             : 
    1132    29491804 :       t = Fl_neg(a / pvmin, q);
    1133    29491782 :       Flc_lincomb1_inplace(gel(x,j), gel(x,def), t, pm);
    1134             :     }
    1135    15277614 :     def--;
    1136             :   }
    1137     4472843 :   if (co > li)
    1138             :   {
    1139           0 :     x += co - li;
    1140           0 :     x[0] = evaltyp(t_MAT) | _evallg(li);
    1141             :   }
    1142     4472843 :   return gerepilecopy(av0, x);
    1143             : }
    1144             : 
    1145             : static int
    1146      219981 : ZV_allequal(GEN v)
    1147             : {
    1148      219981 :   long i, l = lg(v);
    1149      219981 :   if (l > 1)
    1150             :   {
    1151      219983 :     GEN x = gel(v,1);
    1152      389418 :     for (i = 2; i < l; i++) if (!equalii(x,gel(v,i))) return 0;
    1153             :   }
    1154      199288 :   return 1;
    1155             : }
    1156             : /* compute optimal D for hnfmod: x upper triangular */
    1157             : static GEN
    1158     4963299 : optimal_D(GEN x, GEN D)
    1159             : {
    1160     4963299 :   long i, n = nbrows(x);
    1161     4963156 :   GEN C = shallowcopy(D);
    1162     4963491 :   gel(C,1) = gcoeff(x,1,1);
    1163     5384614 :   for (i = 2; i < n; i++)
    1164             :   {
    1165     2562917 :     GEN c = mulii(gel(C,i-1), gcoeff(x,i,i));
    1166     2562777 :     if (signe(c) < 0) togglesign(c);
    1167     2562776 :     if (cmpii(c, gel(D,i)) >= 0) break;
    1168      421123 :     gel(C,i) = c;
    1169             :   }
    1170     4963325 :   return C;
    1171             : }
    1172             : 
    1173             : /* D = multiple of det x (usually detint(x)) or vector of positive moduli
    1174             :  * (compute hnf(x | D))
    1175             :  * flag & hnf_MODID: reduce mod D * matid [ otherwise as above ].
    1176             :  * flag & hnf_PART: don't reduce once diagonal is known
    1177             :  * flag & hnf_CENTER: centermod HNF (2|x[i,j]| <] x[i,i]) */
    1178             : GEN
    1179     4965378 : ZM_hnfmodall_i(GEN x, GEN D, long flag)
    1180             : {
    1181             :   pari_sp av;
    1182     4965378 :   const long center = (flag & hnf_CENTER);
    1183             :   long moddiag, modsame, nli, li, co, i, j, k, def, ldef;
    1184             :   GEN u, LDM;
    1185             : 
    1186     4965378 :   co = lg(x);
    1187     4965378 :   if (co == 1)
    1188             :   {
    1189        1568 :     if (typ(D) == t_INT || lg(D) == 1) return cgetg(1,t_MAT);
    1190         105 :     x = diagonal_shallow(D); /* handle flags properly */
    1191         105 :     co = lg(x);
    1192             :   }
    1193     4963915 :   li = lgcols(x);
    1194     4963907 :   if (li == 1)
    1195             :   {
    1196         176 :     if (typ(D) != t_INT && lg(D) != li) pari_err_DIM("ZM_hnfmod");
    1197         176 :     return cgetg(1,t_MAT);
    1198             :   }
    1199     4963731 :   nli = li - 1;
    1200     4963731 :   modsame = typ(D)==t_INT;
    1201     4963731 :   if (!modsame)
    1202             :   {
    1203      219996 :     if (lg(D) != li) pari_err_DIM("ZM_hnfmod");
    1204      219982 :     if (ZV_allequal(D)) { modsame = 1; D = gel(D,1); }
    1205             :   }
    1206     4963714 :   moddiag = (flag & hnf_MODID) || !modsame;
    1207             :   /* modsame: triangularize mod fixed d*Id;
    1208             :    * moddiag: modulo diagonal matrix, else modulo multiple of determinant */
    1209             : 
    1210     4963714 :   if (modsame)
    1211             :   {
    1212     4943068 :     LDM = const_vecsmall(nli, 2*lgefint(D)-2);
    1213     4943988 :     D = const_vec(nli,D);
    1214             :   }
    1215             :   else
    1216             :   {
    1217       20646 :     LDM = cgetg(li, t_VECSMALL);
    1218       68067 :     for (i=1; i<li; i++) LDM[i] = lgefint(gel(D,i));
    1219             :   }
    1220     4964647 :   av = avma;
    1221     4964647 :   x = RgM_shallowcopy(x);
    1222             : 
    1223     4964802 :   ldef = 0;
    1224     4964802 :   if (li > co)
    1225             :   {
    1226      139219 :     ldef = li - co;
    1227      139219 :     if (!moddiag)
    1228           7 :       pari_err_DOMAIN("ZM_hnfmod","nb lines",">", strtoGENstr("nb columns"), x);
    1229             :   }
    1230    19569821 :   for (def = co-1,i = nli; i > ldef; i--,def--)
    1231             :   {
    1232    14598335 :     GEN d = gel(D,i);
    1233    14598335 :     long add_N = modsame;
    1234    63404401 :     for (j = 1; j < def; j++)
    1235             :     {
    1236    48799392 :       GEN p1, p2, b, a = gcoeff(x,i,j) = remii(gcoeff(x,i,j), d);
    1237    61232127 :       if (!signe(a)) continue;
    1238             : 
    1239    32825085 :       k = j+1;
    1240    32825085 :       b = gcoeff(x,i,k) = remii(gcoeff(x,i,k), d);
    1241    32827931 :       if (!signe(b)) { swap(gel(x,j), gel(x,k)); continue; }
    1242    20395527 :       if (add_N)
    1243             :       { /* ensure the moving pivot on row i divides d from now on */
    1244     7278321 :         add_N = 0;
    1245     7278321 :         if (!equali1(a))
    1246             :         { /* x[j] *= u; after this, a = x[i,j] | d */
    1247     5921782 :           GEN u = Fp_invgen(a, d, &a);
    1248             :           long t;
    1249     5923052 :           p1 = gel(x,j);
    1250    20915121 :           for (t = 1; t < i; t++) gel(p1,t) = mulii(gel(p1,t), u);
    1251     5921523 :           FpV_red_part_ipvec(p1, D, i-1);
    1252     5920792 :           gel(p1,i) = a;
    1253     5920792 :           if (2*lg(a) < lg(b))
    1254             :           { /* reduce x[i,k] mod x[i,j]: helps ZC_elem */
    1255       26593 :             GEN r, q = dvmdii(b, a, &r);
    1256       26593 :             togglesign(q);
    1257       26593 :             ZC_lincomb1_inplace_i(gel(x,k), gel(x,j), q, i-1);
    1258       26593 :             FpV_red_part_ipvec(gel(x,k), D, i-1);
    1259       26593 :             gcoeff(x,i,k) = b = r;
    1260             :           }
    1261             :         }
    1262             :       }
    1263    20394494 :       ZC_elem(a,b, x, NULL, j,k);
    1264    20399025 :       p1 = gel(x,j);
    1265    20399025 :       p2 = gel(x,k);
    1266             :       /* prevent coeffs explosion */
    1267   119469822 :       for (k = 1; k < i; k++)
    1268             :       {
    1269    99070798 :         if (lgefint(gel(p1,k)) > LDM[k]) gel(p1,k) = remii(gel(p1,k),gel(D,k));
    1270    99070797 :         if (lgefint(gel(p2,k)) > LDM[k]) gel(p2,k) = remii(gel(p2,k),gel(D,k));
    1271             :       }
    1272             :     }
    1273    14605009 :     if (gc_needed(av,2))
    1274             :     {
    1275          24 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[1]. i=%ld",i);
    1276          24 :       x = gerepilecopy(av, x);
    1277             :     }
    1278    14605009 :     if (moddiag && !signe(gcoeff(x,i,def)))
    1279             :     { /* missing pivot on line i, insert column */
    1280      968118 :       GEN a = cgetg(co + 1, t_MAT);
    1281     5175978 :       for (k = 1; k <= def; k++) gel(a,k) = gel(x,k);
    1282      968130 :       gel(a,k++) = Rg_col_ei(gel(D,i), nli, i);
    1283     4153749 :       for (     ; k <= co;  k++) gel(a,k) = gel(x,k-1);
    1284      968128 :       ldef--; if (ldef < 0) ldef = 0;
    1285      968128 :       co++; def++; x = a;
    1286             :     }
    1287             :   }
    1288     4971486 :   if (co < li)
    1289             :   { /* implies moddiag, add missing diag(D) components */
    1290      107163 :     GEN a = cgetg(li+1, t_MAT);
    1291      257959 :     for (k = 1; k <= li-co; k++) gel(a,k) = Rg_col_ei(gel(D,k), nli, k);
    1292      307626 :     for (i = 1; i < co; i++) gel(a,k-1+i) = gel(x,i);
    1293      107163 :     gel(a,li) = zerocol(nli); x = a;
    1294             :   }
    1295             :   else
    1296             :   {
    1297     4864323 :     x += co - li;
    1298     4864323 :     x[0] = evaltyp(t_MAT) | _evallg(li); /* kill 0 columns */
    1299     4864323 :     if (moddiag) x = shallowconcat(x, zerocol(nli));
    1300             :   }
    1301     4964734 :   if (moddiag)
    1302             :   { /* x[li]: extra column, an accumulator discarded at the end */
    1303             :     GEN D2;
    1304     4799171 :     gcoeff(x,1,1) = gcdii(gcoeff(x,1,1), gel(D,1));
    1305     4797863 :     D2 = optimal_D(x,D);
    1306             :     /* add up missing diag(D) components */
    1307    19093757 :     for (i = nli; i > 0; i--)
    1308             :     {
    1309    14295363 :       gcoeff(x, i, li) = gel(D,i);
    1310    55118779 :       for (j = i; j > 0; j--)
    1311             :       {
    1312    40825500 :         GEN a = gcoeff(x, j, li);
    1313    40825500 :         if (!signe(a)) continue;
    1314    15060309 :         ZC_elem(a, gcoeff(x,j,j), x, NULL, li,j);
    1315    15059774 :         FpV_red_part_ipvec(gel(x,li), D, j-1);
    1316    15057329 :         FpV_red_part_ipvec(gel(x,j),  D, j-1);
    1317             :       }
    1318    14293279 :       if (gc_needed(av,1))
    1319             :       {
    1320          32 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[2]. i=%ld", i);
    1321          32 :         gerepileall(av, 2, &x, &D2);
    1322             :       }
    1323             :     }
    1324     4798394 :     D = D2;
    1325             :   }
    1326             :   else
    1327             :   {
    1328      165563 :     GEN b = gel(D,1);
    1329      616770 :     for (i = nli; i > 0; i--)
    1330             :     {
    1331      451206 :       GEN d = bezout(gcoeff(x,i,i),b, &u,NULL);
    1332      451216 :       gcoeff(x,i,i) = d;
    1333      451216 :       FpV_Fp_mul_part_ip(gel(x,i), u, b, i-1);
    1334      451201 :       if (i > 1) b = diviiexact(b,d);
    1335             :     }
    1336      165564 :     D = optimal_D(x,D);
    1337             :   }
    1338     4963954 :   x[0] = evaltyp(t_MAT) | _evallg(li); /* kill 0 columns, discard accumulator */
    1339     4963954 :   if (flag & hnf_PART) return x;
    1340             : 
    1341    19711583 :   for (i = nli; i > 0; i--)
    1342             :   {
    1343    14751094 :     GEN diag = gcoeff(x,i,i);
    1344    14751094 :     if (signe(diag) < 0) { gel(x,i) = ZC_neg(gel(x,i)); diag = gcoeff(x,i,i); }
    1345    14751100 :     if (i != nli)
    1346    27179758 :       for (j = 1; j < i; j++) gcoeff(x,j,i) = remii(gcoeff(x,j,i), gel(D,j));
    1347    41927950 :     for (j = i+1; j < li; j++)
    1348             :     {
    1349    27178246 :       GEN b = gcoeff(x,i,j) = remii(gcoeff(x,i,j), gel(D,i));
    1350    27177385 :       GEN r, q = truedvmdii(b, diag, &r);
    1351             :       /* ensure -diag/2 <= r < diag/2 */
    1352    27176957 :       if (center && signe(r) && abscmpii(shifti(r,1),diag) >= 0)
    1353      620130 :       { r = subii(r,diag); q = addiu(q,1); }
    1354    27178728 :       if (!signe(q)) continue;
    1355    10885843 :       togglesign(q);
    1356    10885831 :       ZC_lincomb1_inplace_i(gel(x,j), gel(x,i), q, i-1);
    1357    10884013 :       gcoeff(x,i,j) = r;
    1358             :     }
    1359    14749704 :     if (gc_needed(av,1))
    1360             :     {
    1361          40 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[3]. i=%ld", i);
    1362          40 :       gerepileall(av, 2, &x, &D);
    1363             :     }
    1364             :   }
    1365     4960489 :   return x;
    1366             : }
    1367             : GEN
    1368     4915823 : ZM_hnfmodall(GEN x, GEN dm, long flag)
    1369             : {
    1370     4915823 :   pari_sp av = avma;
    1371     4915823 :   return gerepilecopy(av, ZM_hnfmodall_i(x, dm, flag));
    1372             : }
    1373             : GEN
    1374      165555 : ZM_hnfmod(GEN x, GEN d) { return ZM_hnfmodall(x,d,0); }
    1375             : GEN
    1376     4340822 : ZM_hnfmodid(GEN x, GEN d)
    1377     4340822 : { return ZM_hnfmodall(x,d,hnf_MODID); }
    1378             : /* return the column echelon form of x with 1's as pivots,
    1379             :  * P contains the row indices containing the pivots in increasing order */
    1380             : static GEN
    1381     3144761 : FpM_echelon(GEN x, GEN *pP, GEN p)
    1382             : {
    1383             :   pari_sp av;
    1384             :   long iP, li, co, i, j, k, def, ldef;
    1385             :   GEN P;
    1386             : 
    1387     3144761 :   co = lg(x); if (co == 1) { *pP = cgetg(1,t_VECSMALL); return cgetg(1,t_MAT); }
    1388     3144761 :   li = lgcols(x);
    1389     3144741 :   iP = 1;
    1390     3144741 :   *pP = P = cgetg(li, t_VECSMALL);
    1391     3144760 :   av = avma;
    1392     3144760 :   x = FpM_red(x, p);
    1393             : 
    1394     3144101 :   ldef = (li > co)? li - co: 0;
    1395    11987378 :   for (def = co-1,i = li-1; i > ldef; i--)
    1396             :   {
    1397     8840933 :     GEN u = NULL;
    1398    13640562 :     for (k = def; k; k--)
    1399             :     {
    1400    10232074 :       u = gcoeff(x,i,k);
    1401    10232074 :       if (signe(u)) break;
    1402             :     }
    1403     8840933 :     if (!k)
    1404             :     {
    1405     3410498 :       if (--ldef < 0) ldef = 0;
    1406     3410498 :       continue;
    1407             :     }
    1408     5430435 :     P[iP++] = i;
    1409     5430435 :     if (k != def) swap(gel(x,def), gel(x,k));
    1410     5430435 :     if (!equali1(u))
    1411     4328173 :       FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(u,p), p, i-1);
    1412     5432375 :     gcoeff(x, i, def) = gen_1;
    1413    17217881 :     for (j = def-1; j; j--)
    1414             :     { /* zero x[i, 1..def-1] using x[i,def] = 1*/
    1415    11785816 :       GEN xj = gel(x,j), u = gel(xj,i);
    1416    11785816 :       if (!signe(u)) continue;
    1417             : 
    1418     7455409 :       ZC_lincomb1_inplace(xj, gel(x,def), negi(u));
    1419    38510222 :       for (k = 1; k < i; k++) gel(xj,k) = modii(gel(xj,k), p);
    1420             :     }
    1421     5432065 :     if (gc_needed(av,2))
    1422             :     {
    1423           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpM_echelon. i=%ld",i);
    1424           0 :       x = gerepilecopy(av, x);
    1425             :     }
    1426     5432779 :     def--;
    1427             :   }
    1428             :   /* rank = iP - 1 */
    1429     3146445 :   setlg(P, iP); vecsmall_sort(P);
    1430     3144685 :   if (co > iP) x += co - iP;
    1431     3144685 :   x[0] = evaltyp(t_MAT) | _evallg(iP);
    1432     3144685 :   return x;
    1433             : }
    1434             : /* given x square of maximal rank with 1 or p on diagonal from hnfmodid
    1435             :  * (=> a column containing p has its other entries at 0 ), return the HNF */
    1436             : static GEN
    1437     3144887 : FpM_hnfend(pari_sp av, GEN x, GEN p)
    1438             : {
    1439     3144887 :   long i, l = lgcols(x);
    1440    11987963 :   for (i = l-1; i > 0; i--)
    1441             :   {
    1442     8844174 :     GEN diag = gcoeff(x,i,i);
    1443             :     long j;
    1444     8844174 :     if (is_pm1(diag))
    1445    11161838 :       for (j = i+1; j < l; j++)
    1446             :       {
    1447     5728299 :         GEN xj = gel(x,j), b = gel(xj,i);
    1448             :         long k;
    1449     5728299 :         if (!signe(b)) continue;
    1450     4027279 :         ZC_lincomb1_inplace(xj, gel(x,i), negi(b));
    1451    19563959 :         for (k=1; k<i; k++)
    1452    15536837 :           if (lgefint(gel(xj,k)) > 3) gel(xj,k) = remii(gel(xj,k), p);
    1453             :       }
    1454             :     else
    1455     9810843 :       for (j = i+1; j < l; j++) gcoeff(x,i,j) = modii(gcoeff(x,i,j), p);
    1456     8843743 :     if (gc_needed(av,2))
    1457             :     {
    1458           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpM_hnfend. i=%ld",i);
    1459           0 :       x = gerepilecopy(av, x);
    1460             :     }
    1461             :   }
    1462     3143789 :   return x;
    1463             : }
    1464             : GEN
    1465     3144760 : ZM_hnfmodprime(GEN x, GEN p)
    1466             : {
    1467     3144760 :   pari_sp av = avma;
    1468             :   GEN P, y;
    1469             :   long l, lP, i;
    1470     3144760 :   if (lg(x) == 1) return cgetg(1, t_MAT);
    1471     3144760 :   l = lgcols(x);
    1472     3144754 :   x = FpM_echelon(x, &P, p);
    1473     3144689 :   lP = lg(P); /* rank = lP-1 */
    1474     3144689 :   if (lP == l) { set_avma(av); return matid(l-1); }
    1475     3144689 :   y = scalarmat_shallow(p, l-1);
    1476     8578611 :   for (i = 1; i < lP; i++) gel(y,P[i]) = gel(x,i);
    1477     3144879 :   return gerepilecopy(av, FpM_hnfend(av,y,p));
    1478             : }
    1479             : 
    1480             : static GEN
    1481      408044 : allhnfmod(GEN x, GEN dm, int flag)
    1482             : {
    1483      408044 :   if (typ(x)!=t_MAT) pari_err_TYPE("allhnfmod",x);
    1484      408044 :   RgM_check_ZM(x, "allhnfmod");
    1485      408036 :   if (isintzero(dm)) return ZM_hnf(x);
    1486      408038 :   return ZM_hnfmodall(x, dm, flag);
    1487             : }
    1488             : GEN
    1489          14 : hnfmod(GEN x, GEN d)
    1490             : {
    1491          14 :   if (typ(d) != t_INT) pari_err_TYPE("mathnfmod",d);
    1492          14 :   return allhnfmod(x, d, 0);
    1493             : }
    1494             : GEN
    1495      408031 : hnfmodid(GEN x, GEN d)
    1496             : {
    1497      408031 :   switch(typ(d))
    1498             :   {
    1499      390845 :     case t_INT: break;
    1500       17186 :     case t_VEC: case t_COL:
    1501       17186 :       if (RgV_is_ZV(d)) break;
    1502           0 :     default: pari_err_TYPE("mathnfmodid",d);
    1503             :   }
    1504      408031 :   return allhnfmod(x, d, hnf_MODID);
    1505             : }
    1506             : 
    1507             : /* M a ZM in HNF. Normalize with *centered* residues */
    1508             : GEN
    1509       25160 : ZM_hnfcenter(GEN M)
    1510             : {
    1511       25160 :   long i, j, k, N = lg(M)-1;
    1512       25160 :   pari_sp av = avma;
    1513             : 
    1514       88802 :   for (j=N-1; j>0; j--) /* skip last line */
    1515             :   {
    1516       63638 :     GEN Mj = gel(M,j), a = gel(Mj,j);
    1517      213032 :     for (k = j+1; k <= N; k++)
    1518             :     {
    1519      149390 :       GEN Mk = gel(M,k), q = diviiround(gel(Mk,j), a);
    1520      149397 :       long s = signe(q);
    1521      149397 :       if (!s) continue;
    1522       68435 :       if (is_pm1(q))
    1523             :       {
    1524       32100 :         if (s < 0)
    1525        8584 :           for (i = 1; i <= j; i++) gel(Mk,i) = addii(gel(Mk,i), gel(Mj,i));
    1526             :         else
    1527       93800 :           for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), gel(Mj,i));
    1528             :       }
    1529             :       else
    1530      204365 :         for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), mulii(q,gel(Mj,i)));
    1531       68435 :       if (gc_needed(av,1))
    1532             :       {
    1533           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfcenter, j = %ld",j);
    1534           0 :         M = gerepilecopy(av, M);
    1535             :       }
    1536             :     }
    1537             :   }
    1538       25164 :   return M;
    1539             : }
    1540             : 
    1541             : /***********************************************************************/
    1542             : /*                                                                     */
    1543             : /*                 HNFLLL (Havas, Majewski, Mathews)                   */
    1544             : /*                                                                     */
    1545             : /***********************************************************************/
    1546             : 
    1547             : static void
    1548     2050977 : Minus(long j, GEN lambda)
    1549             : {
    1550     2050977 :   long k, n = lg(lambda);
    1551             : 
    1552    11755871 :   for (k=1  ; k<j; k++) togglesign_safe(&gcoeff(lambda,k,j));
    1553    17432277 :   for (k=j+1; k<n; k++) togglesign_safe(&gcoeff(lambda,j,k));
    1554     2050999 : }
    1555             : 
    1556             : /* index of first nonzero entry */
    1557             : static long
    1558   105843306 : findi(GEN M)
    1559             : {
    1560   105843306 :   long i, n = lg(M);
    1561  2612434502 :   for (i=1; i<n; i++)
    1562  2581011038 :     if (signe(gel(M,i))) return i;
    1563    31423464 :   return 0;
    1564             : }
    1565             : 
    1566             : static long
    1567   105837230 : findi_normalize(GEN Aj, GEN B, long j, GEN lambda)
    1568             : {
    1569   105837230 :   long r = findi(Aj);
    1570   105864958 :   if (r && signe(gel(Aj,r)) < 0)
    1571             :   {
    1572     2050916 :     ZV_togglesign(Aj); if (B) ZV_togglesign(gel(B,j));
    1573     2050971 :     Minus(j,lambda);
    1574             :   }
    1575   105869066 :   return r;
    1576             : }
    1577             : 
    1578             : static void
    1579     6447860 : subzi(GEN *a, GEN b)
    1580             : {
    1581     6447860 :   pari_sp av = avma;
    1582     6447860 :   b = subii(*a, b);
    1583     6447405 :   if (lgefint(b)<=lg(*a) && isonstack(*a)) { affii(b,*a); set_avma(av); }
    1584     1300608 :   else *a = b;
    1585     6448074 : }
    1586             : 
    1587             : static void
    1588   239211721 : addzi(GEN *a, GEN b)
    1589             : {
    1590   239211721 :   pari_sp av = avma;
    1591   239211721 :   b = addii(*a, b);
    1592   239337747 :   if (lgefint(b)<=lg(*a) && isonstack(*a)) { affii(b,*a); set_avma(av); }
    1593    17132056 :   else *a = b;
    1594   239711359 : }
    1595             : 
    1596             : /* x + y*z */
    1597             : static void
    1598   517709522 : addmulzii(GEN *x, GEN y, GEN z)
    1599             : {
    1600   517709522 :   long lx = lgefint(*x);
    1601             :   pari_sp av;
    1602             :   GEN t;
    1603   517709522 :   long ly = lgefint(y), lz;
    1604   517709522 :   if (ly == 2) return;
    1605   236109444 :   lz = lgefint(z);
    1606   236109444 :   av = avma; (void)new_chunk(lx+ly+lz); /*HACK*/
    1607   237784743 :   t = mulii(z, y);
    1608   236734179 :   set_avma(av); return addzi(x,t);
    1609             : }
    1610             : 
    1611             : static void
    1612    21828828 : ZC_lincomb1z_i(GEN X, GEN Y, GEN v, long n)
    1613             : {
    1614             :   long i;
    1615   500628051 :   for (i = n; i; i--) addmulzii(&gel(X,i), gel(Y,i), v);
    1616    21588742 : }
    1617             : /* X <- X + v Y (elementary col operation) */
    1618             : static void
    1619    21819290 : ZC_lincomb1z(GEN X, GEN Y, GEN v)
    1620             : {
    1621    21819290 :   if (lgefint(v) != 2) return ZC_lincomb1z_i(X, Y, v, lg(X)-1);
    1622             : }
    1623             : 
    1624             : static void
    1625    52900795 : reduce2(GEN A, GEN B, long k, long j, long *row0, long *row1, GEN lambda, GEN D)
    1626             : {
    1627             :   GEN q;
    1628             :   long i;
    1629             : 
    1630    52900795 :   *row0 = findi_normalize(gel(A,j), B,j,lambda);
    1631    52938868 :   *row1 = findi_normalize(gel(A,k), B,k,lambda);
    1632    52975815 :   if (*row0)
    1633    30717464 :     q = truedivii(gcoeff(A,*row0,k), gcoeff(A,*row0,j));
    1634             :   else
    1635             :   {
    1636    22258351 :     pari_sp btop = avma;
    1637    22258351 :     int c = abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j));
    1638    22214714 :     set_avma(btop);
    1639    22237768 :     if (c > 0)
    1640     7496728 :       q = diviiround(gcoeff(lambda,j,k), gel(D,j));
    1641             :     else
    1642    14741040 :       return;
    1643             :   }
    1644             : 
    1645    38206238 :   if (signe(q))
    1646             :   {
    1647    14663019 :     GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
    1648    14663019 :     togglesign_safe(&q);
    1649    14666180 :     if (*row0) ZC_lincomb1z(gel(A,k),gel(A,j),q);
    1650    14666887 :     if (B) ZC_lincomb1z(gel(B,k),gel(B,j),q);
    1651    14637455 :     addmulzii(&gel(Lk,j), q, gel(D,j));
    1652    14632369 :     if (is_pm1(q))
    1653             :     {
    1654     5404852 :       if (signe(q) > 0)
    1655             :       {
    1656     5532907 :         for (i=1; i<j; i++)
    1657     3465506 :           if (signe(gel(Lj,i))) addzi(&gel(Lk,i), gel(Lj,i));
    1658             :       }
    1659             :       else
    1660             :       {
    1661    11202793 :         for (i=1; i<j; i++)
    1662     7865880 :           if (signe(gel(Lj,i))) subzi(&gel(Lk,i), gel(Lj,i));
    1663             :       }
    1664             :     }
    1665             :     else
    1666             :     {
    1667    41145856 :       for (i=1; i<j; i++)
    1668    31887605 :         if (signe(gel(Lj,i))) addmulzii(&gel(Lk,i), q, gel(Lj,i));
    1669             :     }
    1670             :   }
    1671             : }
    1672             : static void
    1673    47782788 : affii2_or_copy_gc(pari_sp av, GEN x, GEN *y, GEN x1, GEN *y1)
    1674             : {
    1675    47782788 :   long l = lg(*y), l1 = lg(*y1);
    1676    47782788 :   if(x==*y && x1==*y1) return;
    1677    47782788 :   if (lgefint(x) <= l && isonstack(*y) && lgefint(x1) <= l1 && isonstack(*y1))
    1678             :   {
    1679    46448769 :     affii(x,*y);
    1680    46475425 :     affii(x1,*y1);
    1681    46507255 :     set_avma(av);
    1682             :   }
    1683     1343374 :   else if (lgefint(x) <= l && isonstack(*y))
    1684             :   {
    1685      949731 :     affii(x,*y);
    1686      949745 :     *y1 = gerepilecopy(av, x1);
    1687             :   }
    1688      393646 :   else if (lgefint(x1) <= l1 && isonstack(*y1))
    1689             :   {
    1690      346725 :     affii(x1,*y1);
    1691      346727 :     *y = gerepilecopy(av, x);
    1692             :   }
    1693             :   else
    1694             :   {
    1695       46920 :     *y  = cgeti(2*lg(x));
    1696       52752 :     *y1 = cgeti(2*lg(x1));
    1697       52752 :     affii(x,*y);
    1698       52752 :     affii(x1,*y1);
    1699       52752 :     gerepileall(av,2,y,y1);
    1700             :   }
    1701             : }
    1702             : static void
    1703     8459064 : affii_or_copy_gc(pari_sp av, GEN x, GEN *y)
    1704             : {
    1705     8459064 :   long l = lg(*y);
    1706     8459064 :   if (lgefint(x) <= l && isonstack(*y))
    1707             :   {
    1708     6815907 :     affii(x,*y);
    1709     6818574 :     set_avma(av);
    1710             :   }
    1711             :   else
    1712     1643231 :     *y = gerepileuptoint(av, x);
    1713     8470635 : }
    1714             : static void
    1715     8471827 : hnfswap(GEN A, GEN B, long k, GEN lambda, GEN D)
    1716             : {
    1717             :   pari_sp av;
    1718     8471827 :   GEN t, p1, p2, Lk = gel(lambda,k);
    1719     8471827 :   long i,j,n = lg(A);
    1720             : 
    1721     8471827 :   swap(gel(A,k), gel(A,k-1));
    1722     8471827 :   if (B) swap(gel(B,k), gel(B,k-1));
    1723    46217797 :   for (j=k-2; j; j--) swap(gcoeff(lambda,j,k-1), gel(Lk,j));
    1724   106052744 :   for (i=k+1; i<n; i++)
    1725             :   {
    1726    97572493 :     pari_sp btop = avma;
    1727    97572493 :     GEN Li = gel(lambda,i);
    1728    97572493 :     if (signe(gel(Li,k-1))==0 && signe(gel(Li,k))==0) continue;
    1729    47824836 :     p1 = mulii(gel(Li,k-1), gel(D,k));
    1730    47799892 :     p2 = mulii(gel(Li,k), gel(Lk,k-1));
    1731    47790557 :     t = subii(p1,p2);
    1732    47793662 :     p1 = mulii(gel(Li,k), gel(D,k-2));
    1733    47788806 :     p2 = mulii(gel(Li,k-1), gel(Lk,k-1));
    1734    47790922 :     affii2_or_copy_gc(btop, diviiexact(addii(p1,p2), gel(D,k-1)),
    1735    47786778 :                       &gel(Li,k-1), diviiexact(t, gel(D,k-1)), &gel(Li,k));
    1736             :   }
    1737     8480251 :   av = avma;
    1738     8480251 :   p1 = mulii(gel(D,k-2), gel(D,k));
    1739     8464336 :   p2 = sqri(gel(Lk,k-1));
    1740     8460923 :   affii_or_copy_gc(av, diviiexact(addii(p1,p2), gel(D,k-1)), &gel(D,k-1));
    1741     8467167 : }
    1742             : 
    1743             : /* reverse row order in matrix A, IN PLACE */
    1744             : static GEN
    1745      461976 : reverse_rows(GEN A)
    1746             : {
    1747      461976 :   long i, j, h, n = lg(A);
    1748      461976 :   if (n == 1) return A;
    1749      461976 :   h = lgcols(A);
    1750     3855972 :   for (j=1; j<n; j++)
    1751             :   {
    1752     3393994 :     GEN c = gel(A,j);
    1753             :     /* start at (h-1) >>1 : if h = 2i even, no need to swap c[i] and itself */
    1754     9550423 :     for (i=(h-1)>>1; i; i--) swap(gel(c,i), gel(c,h-i));
    1755             :   }
    1756      461978 :   return A;
    1757             : }
    1758             : /* decide whether to swap */
    1759             : static int
    1760     4614226 : must_swap(long k, GEN lambda, GEN D)
    1761             : {
    1762     4614226 :   pari_sp av = avma;
    1763     4614226 :   GEN z = addii(mulii(gel(D,k-2),gel(D,k)), sqri(gcoeff(lambda,k-1,k)));
    1764     4612358 :   return gc_bool(av, cmpii(z, sqri(gel(D,k-1))) < 0);
    1765             : }
    1766             : 
    1767             : GEN
    1768      230994 : ZM_hnflll(GEN A, GEN *ptB, int remove)
    1769             : {
    1770      230994 :   pari_sp av = avma;
    1771             :   long n, k, kmax;
    1772             :   GEN B, lambda, D;
    1773             : 
    1774      230994 :   n = lg(A);
    1775      230994 :   A = reverse_rows(ZM_copy(A)); /* ZM_copy for in place findi_normalize() */
    1776      230995 :   B = ptB? matid(n-1): NULL;
    1777      230996 :   D = const_vec(n, gen_1) + 1;
    1778      230996 :   lambda = zeromatcopy(n-1,n-1);
    1779      216431 :   k = kmax = 2;
    1780    15734718 :   while (k < n)
    1781             :   {
    1782             :     long row0, row1;
    1783             :     int do_swap;
    1784    15503728 :     reduce2(A,B,k,k-1,&row0,&row1,lambda,D);
    1785    15498382 :     if      (row0) do_swap = (!row1 || row0 <= row1);
    1786     5557134 :     else if (row1) do_swap = 0;
    1787     4358701 :     else do_swap = must_swap(k,lambda,D);
    1788    15519913 :     if (do_swap)
    1789             :     {
    1790     8226197 :       hnfswap(A,B,k,lambda,D);
    1791     8227567 :       if (k > 2) k--;
    1792             :     }
    1793             :     else
    1794             :     {
    1795             :       long i;
    1796    44704803 :       for (i=k-2; i; i--)
    1797             :       {
    1798             :         long row0, row1;
    1799    37414083 :         reduce2(A,B,k,i,&row0,&row1,lambda,D);
    1800             :       }
    1801     7290720 :       if (++k > kmax) kmax = k;
    1802             :     }
    1803    15518287 :     if (gc_needed(av,3))
    1804             :     {
    1805           0 :       GEN b = D-1;
    1806           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"hnflll, kmax = %ld / %ld",kmax,n-1);
    1807           0 :       gerepileall(av, B? 4: 3, &A, &lambda, &b, &B);
    1808           0 :       if (gc_needed(av,1)) paristack_resize(0); /* avoid desperation GC */
    1809           0 :       D = b+1;
    1810             :     }
    1811             :   }
    1812             :   /* handle trivial case: return negative diag coefficient otherwise */
    1813      230990 :   if (n == 2) (void)findi_normalize(gel(A,1), B,1,lambda);
    1814      230990 :   A = reverse_rows(A);
    1815      230993 :   if (remove)
    1816             :   {
    1817             :     long i;
    1818        7546 :     for (i = 1; i < n; i++)
    1819        7322 :       if (!ZV_equal0(gel(A,i))) break;
    1820        2282 :     remove_0cols(i-1, &A, &B, remove);
    1821             :   }
    1822      230993 :   gerepileall(av, B? 2: 1, &A, &B);
    1823      230996 :   if (B) *ptB = B;
    1824      230996 :   return A;
    1825             : }
    1826             : 
    1827             : GEN
    1828           7 : hnflll(GEN x)
    1829             : {
    1830           7 :   GEN z = cgetg(3, t_VEC);
    1831           7 :   gel(z,1) = ZM_hnflll(x, &gel(z,2), 1);
    1832           7 :   return z;
    1833             : }
    1834             : 
    1835             : /* Variation on HNFLLL: Extended GCD */
    1836             : 
    1837             : static void
    1838      950510 : reduce1(GEN A, GEN B, long k, long j, GEN lambda, GEN D)
    1839             : {
    1840             :   GEN q;
    1841             :   long i;
    1842             : 
    1843      950510 :   if (signe(gel(A,j)))
    1844      172934 :     q = diviiround(gel(A,k),gel(A,j));
    1845      777576 :   else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
    1846       96207 :     q = diviiround(gcoeff(lambda,j,k), gel(D,j));
    1847             :   else
    1848      681369 :     return;
    1849             : 
    1850      269141 :   if (signe(q))
    1851             :   {
    1852      232549 :     GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
    1853      232549 :     togglesign_safe(&q);
    1854      232549 :     gel(A,k) = addmulii(gel(A,k), q, gel(A,j));
    1855      232549 :     ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
    1856      232549 :     gel(Lk,j) = addmulii(gel(Lk,j), q, gel(D,j));
    1857      421761 :     for (i=1; i<j; i++)
    1858      189212 :       if (signe(gel(Lj,i))) gel(Lk,i) = addmulii(gel(Lk,i), q, gel(Lj,i));
    1859             :   }
    1860             : }
    1861             : 
    1862             : static GEN
    1863      109221 : ZV_gcdext_i(GEN A)
    1864             : {
    1865      109221 :   long k, n = lg(A);
    1866             :   GEN B, lambda, D;
    1867             : 
    1868      109221 :   if (n == 1) retmkvec2(gen_1, cgetg(1,t_MAT));
    1869      109221 :   A = leafcopy(A);
    1870      109221 :   B = matid(n-1);
    1871      109221 :   lambda = zeromatcopy(n-1,n-1);
    1872      109221 :   D = const_vec(n, gen_1) + 1;
    1873      109221 :   k = 2;
    1874      698177 :   while (k < n)
    1875             :   {
    1876             :     int do_swap;
    1877             : 
    1878      588956 :     reduce1(A,B,k,k-1,lambda,D);
    1879      588956 :     if    (signe(gel(A,k-1))) do_swap = 1;
    1880      416022 :     else if (signe(gel(A,k))) do_swap = 0;
    1881      223530 :     else do_swap = must_swap(k,lambda,D);
    1882      588956 :     if (do_swap)
    1883             :     {
    1884      241222 :       hnfswap(A,B,k,lambda,D);
    1885      241222 :       if (k > 2) k--;
    1886             :     }
    1887             :     else
    1888             :     {
    1889             :       long i;
    1890      709288 :       for (i=k-2; i; i--) reduce1(A,B,k,i,lambda,D);
    1891      347734 :       k++;
    1892             :     }
    1893             :   }
    1894      109221 :   if (signe(gel(A,n-1)) < 0)
    1895             :   {
    1896       13402 :     gel(A,n-1) = negi(gel(A,n-1));
    1897       13402 :     ZV_togglesign(gel(B,n-1));
    1898             :   }
    1899      109221 :   return mkvec2(gel(A,n-1), B);
    1900             : }
    1901             : GEN
    1902      109207 : ZV_extgcd(GEN A)
    1903             : {
    1904      109207 :   pari_sp av = avma;
    1905      109207 :   return gerepilecopy(av, ZV_gcdext_i(A));
    1906             : }
    1907             : /* as ZV_extgcd, transforming the gcd into a t_MAT, for mathnf0 */
    1908             : static GEN
    1909          21 : ZV_hnfgcdext(GEN A)
    1910             : {
    1911          21 :   pari_sp av = avma;
    1912             :   GEN z;
    1913          21 :   if (lg(A) == 1) retmkvec2(cgetg(1,t_MAT),cgetg(1,t_MAT));
    1914          14 :   z = ZV_gcdext_i(A);
    1915          14 :   gel(z,1) = mkmat(mkcol(gel(z,1)));
    1916          14 :   return gerepilecopy(av, z);
    1917             : }
    1918             : 
    1919             : /* HNF with permutation. */
    1920             : GEN
    1921         385 : ZM_hnfperm(GEN A, GEN *ptU, GEN *ptperm)
    1922             : {
    1923             :   GEN U, c, l, perm, d, p, q, b;
    1924         385 :   pari_sp av = avma, av1;
    1925             :   long r, t, i, j, j1, k, m, n;
    1926             : 
    1927         385 :   n = lg(A)-1;
    1928         385 :   if (!n)
    1929             :   {
    1930           7 :     if (ptU) *ptU = cgetg(1,t_MAT);
    1931           7 :     if (ptperm) *ptperm = cgetg(1,t_VEC);
    1932           7 :     return cgetg(1, t_MAT);
    1933             :   }
    1934         378 :   m = nbrows(A);
    1935         378 :   c = zero_zv(m);
    1936         378 :   l = zero_zv(n);
    1937         378 :   perm = cgetg(m+1, t_VECSMALL);
    1938         378 :   av1 = avma;
    1939         378 :   A = RgM_shallowcopy(A);
    1940         378 :   U = ptU? matid(n): NULL;
    1941             :   /* U base change matrix : A0*U = A all along */
    1942        1722 :   for (r=0, k=1; k <= n; k++)
    1943             :   {
    1944        3976 :     for (j=1; j<k; j++)
    1945             :     {
    1946        2632 :       if (!l[j]) continue;
    1947        2478 :       t=l[j]; b=gcoeff(A,t,k);
    1948        2478 :       if (!signe(b)) continue;
    1949             : 
    1950         532 :       ZC_elem(b,gcoeff(A,t,j), A,U,k,j);
    1951         532 :       d = gcoeff(A,t,j);
    1952         532 :       if (signe(d) < 0)
    1953             :       {
    1954           0 :         ZV_neg_inplace(gel(A,j));
    1955           0 :         if (U) ZV_togglesign(gel(U,j));
    1956           0 :         d = gcoeff(A,t,j);
    1957             :       }
    1958        1372 :       for (j1=1; j1<j; j1++)
    1959             :       {
    1960         840 :         if (!l[j1]) continue;
    1961         819 :         q = truedivii(gcoeff(A,t,j1),d);
    1962         819 :         if (!signe(q)) continue;
    1963             : 
    1964         329 :         togglesign(q);
    1965         329 :         ZC_lincomb1_inplace(gel(A,j1), gel(A,j), q);
    1966         329 :         if (U) ZC_lincomb1_inplace(gel(U,j1), gel(U,j), q);
    1967             :       }
    1968             :     }
    1969        4585 :     t = m; while (t && (c[t] || !signe(gcoeff(A,t,k)))) t--;
    1970        1344 :     if (t)
    1971             :     {
    1972        1225 :       p = gcoeff(A,t,k);
    1973       19971 :       for (i=t-1; i; i--)
    1974             :       {
    1975       18746 :         q = gcoeff(A,i,k);
    1976       18746 :         if (signe(q) && abscmpii(p,q) > 0) { p = q; t = i; }
    1977             :       }
    1978        1225 :       perm[++r] = l[k] = t; c[t] = k;
    1979        1225 :       if (signe(p) < 0)
    1980             :       {
    1981         142 :         ZV_neg_inplace(gel(A,k));
    1982         142 :         if (U) ZV_togglesign(gel(U,k));
    1983         142 :         p = gcoeff(A,t,k);
    1984             :       }
    1985             :       /* p > 0 */
    1986        3493 :       for (j=1; j<k; j++)
    1987             :       {
    1988        2268 :         if (!l[j]) continue;
    1989        2233 :         q = truedivii(gcoeff(A,t,j),p);
    1990        2233 :         if (!signe(q)) continue;
    1991             : 
    1992         238 :         togglesign(q);
    1993         238 :         ZC_lincomb1_inplace(gel(A,j), gel(A,k), q);
    1994         238 :         if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,k), q);
    1995             :       }
    1996             :     }
    1997        1344 :     if (gc_needed(av1,1))
    1998             :     {
    1999           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"hnfperm, k=%ld",k);
    2000           0 :       gerepileall(av1, U? 2: 1, &A, &U);
    2001             :     }
    2002             :   }
    2003         378 :   if (r < m)
    2004             :   {
    2005        5131 :     for (i=1,k=r; i<=m; i++)
    2006        4816 :       if (!c[i]) perm[++k] = i;
    2007             :   }
    2008             : 
    2009             : /* We have A0*U=A, U in Gl(n,Z)
    2010             :  * basis for Im(A):  columns of A s.t l[j]>0 (r   cols)
    2011             :  * basis for Ker(A): columns of U s.t l[j]=0 (n-r cols) */
    2012         378 :   p = cgetg(r+1,t_MAT);
    2013        2814 :   for (i=1; i<=m/2; i++) lswap(perm[i], perm[m+1-i]);
    2014         378 :   if (U)
    2015             :   {
    2016          84 :     GEN u = cgetg(n+1,t_MAT);
    2017         378 :     for (t=1,k=r,j=1; j<=n; j++)
    2018         294 :       if (l[j])
    2019             :       {
    2020         182 :         u[k + n-r] = U[j];
    2021         182 :         gel(p,k--) = vecpermute(gel(A,j), perm);
    2022             :       }
    2023             :       else
    2024         112 :         u[t++] = U[j];
    2025          84 :     *ptU = u;
    2026          84 :     if (ptperm) *ptperm = perm;
    2027          84 :     gerepileall(av, ptperm? 3: 2, &p, ptU, ptperm);
    2028             :   }
    2029             :   else
    2030             :   {
    2031        1344 :     for (k=r,j=1; j<=n; j++)
    2032        1050 :       if (l[j]) gel(p,k--) = vecpermute(gel(A,j), perm);
    2033         294 :     if (ptperm) *ptperm = perm;
    2034         294 :     gerepileall(av, ptperm? 2: 1, &p, ptperm);
    2035             :   }
    2036         378 :   return p;
    2037             : }
    2038             : 
    2039             : GEN
    2040         238 : ZM_hnf_knapsack(GEN x)
    2041             : {
    2042         238 :   GEN t, perm, H = ZM_hnfperm(x,NULL,&perm);
    2043         238 :   long i,j, l = lg(H), h = lgcols(H);
    2044        3213 :   for (i=1; i<h; i++)
    2045             :   {
    2046        3066 :     int fl = 0;
    2047       16697 :     for (j=1; j<l; j++)
    2048             :     {
    2049       13722 :       t = gcoeff(H,i,j);
    2050       13722 :       if (signe(t))
    2051             :       {
    2052        3101 :         if (!is_pm1(t) || fl) return NULL;
    2053        3010 :         fl = 1;
    2054             :       }
    2055             :     }
    2056             :   }
    2057         147 :   return rowpermute(H, perm_inv(perm));
    2058             : }
    2059             : 
    2060             : GEN
    2061          14 : hnfperm(GEN A)
    2062             : {
    2063          14 :   GEN y = cgetg(4, t_VEC);
    2064          14 :   gel(y,1) = ZM_hnfperm(A, &gel(y,2), &gel(y,3));
    2065          14 :   return y;
    2066             : }
    2067             : 
    2068             : /* Hermite Normal Form, with base change matrix if ptB != NULL.
    2069             :  * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
    2070             :  * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
    2071             : GEN
    2072      725316 : ZM_hnfall_i(GEN A, GEN *ptB, long remove)
    2073             : {
    2074             :   pari_sp av;
    2075             :   long m, n, r, i, j, k, li;
    2076             :   GEN B, c, h, a;
    2077             : 
    2078      725316 :   RgM_dimensions(A, &m,&n);
    2079      725313 :   if (!n)
    2080             :   {
    2081           7 :     if (ptB) *ptB = cgetg(1,t_MAT);
    2082           7 :     return cgetg(1,t_MAT);
    2083             :   }
    2084      725306 :   c = zero_zv(m);
    2085      725301 :   h = const_vecsmall(n, m);
    2086      725301 :   av = avma;
    2087      725301 :   A = RgM_shallowcopy(A);
    2088      725309 :   B = ptB? matid(n): NULL;
    2089      725313 :   r = n+1;
    2090     2031714 :   for (li=m; li; li--)
    2091             :   {
    2092     2925847 :     for (j=1; j<r; j++)
    2093             :     {
    2094     3560707 :       for (i=h[j]; i>li; i--)
    2095             :       {
    2096      635336 :         a = gcoeff(A,i,j);
    2097      635336 :         k = c[i];
    2098             :         /* zero a = Aij  using  Aik */
    2099      635336 :         if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B,j,k);
    2100      635274 :         ZM_reduce(A,B, i,k); /* ensure reduced entries even if a = 0 */
    2101             :       }
    2102     2925371 :       if (gc_needed(av,1) && (j & 0x7f) == 0)
    2103             :       {
    2104           0 :         if (DEBUGMEM>1)
    2105           0 :           pari_warn(warnmem,"ZM_hnfall[1], li = %ld, j = %ld", li, j);
    2106           0 :         gerepileall(av, B? 2: 1, &A, &B);
    2107             :       }
    2108     2925400 :       if (signe( gcoeff(A,li,j) )) break;
    2109     1619412 :       h[j] = li-1;
    2110             :     }
    2111     1306407 :     if (j == r) continue;
    2112     1306030 :     r--;
    2113     1306030 :     if (j < r) /* A[j] != 0 */
    2114             :     {
    2115      846814 :       swap(gel(A,j), gel(A,r));
    2116      846814 :       if (B) swap(gel(B,j), gel(B,r));
    2117      846814 :       h[j] = h[r]; h[r] = li; c[li] = r;
    2118             :     }
    2119     1306030 :     if (signe(gcoeff(A,li,r)) < 0)
    2120             :     {
    2121      302907 :       ZV_neg_inplace(gel(A,r));
    2122      302922 :       if (B) ZV_togglesign(gel(B,r));
    2123             :     }
    2124     1306043 :     ZM_reduce(A,B, li,r);
    2125     1306024 :     if (gc_needed(av,1))
    2126             :     {
    2127           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[2], li = %ld", li);
    2128           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2129             :     }
    2130             :   }
    2131             : 
    2132      725279 :   if (DEBUGLEVEL>5) err_printf("\nhnfall, final phase: ");
    2133      725301 :   r--; /* first r cols are in the image the n-r (independent) last ones */
    2134     2506828 :   for (j=1; j<=r; j++)
    2135             :   {
    2136     3881538 :     for (i=h[j]; i; i--)
    2137             :     {
    2138     2100019 :       a = gcoeff(A,i,j);
    2139     2100019 :       k = c[i];
    2140     2100019 :       if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B, j,k);
    2141     2099979 :       ZM_reduce(A,B, i,k); /* ensure reduced entries, even if a = 0 */
    2142             :     }
    2143     1781519 :     if (gc_needed(av,1) && (j & 0x7f) == 0)
    2144             :     {
    2145           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[3], j = %ld", j);
    2146           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2147             :     }
    2148             :   }
    2149      725280 :   if (DEBUGLEVEL>5) err_printf("\n");
    2150      725280 :   if (remove) remove_0cols(r, &A, &B, remove);
    2151      725296 :   if (ptB) *ptB = B;
    2152      725296 :   return A;
    2153             : }
    2154             : GEN
    2155       28617 : ZM_hnfall(GEN A, GEN *ptB, long remove)
    2156             : {
    2157       28617 :   pari_sp av = avma;
    2158       28617 :   A = ZM_hnfall_i(A, ptB, remove);
    2159       28617 :   return gc_all(av, ptB? 2: 1, &A, ptB);
    2160             : }
    2161             : 
    2162             : GEN
    2163          14 : hnfall(GEN x)
    2164             : {
    2165          14 :   GEN z = cgetg(3, t_VEC);
    2166          14 :   gel(z,1) = ZM_hnfall(x, (GEN*)(z+2), 1);
    2167          14 :   return z;
    2168             : }
    2169             : GEN
    2170          14 : hnf(GEN x) { return mathnf0(x,0); }
    2171             : 
    2172             : /* C = A^(-1)t where A and C are integral, A is upper triangular, t t_INT */
    2173             : GEN
    2174      237216 : hnf_invscale(GEN A, GEN t)
    2175             : {
    2176      237216 :   long n = lg(A)-1, i,j,k;
    2177      237216 :   GEN m, c = cgetg(n+1,t_MAT);
    2178             : 
    2179      237213 :   if (!n) return c;
    2180      879874 :   for (k=1; k<=n; k++)
    2181             :   { /* cf hnf_divscale with B = id, thus b = e_k */
    2182      642694 :     GEN u = cgetg(n+1, t_COL);
    2183      642699 :     pari_sp av = avma;
    2184      642699 :     gel(c,k) = u;
    2185      642699 :     gel(u,n) = k == n? gerepileuptoint(av, diviiexact(t, gcoeff(A,n,n))): gen_0;
    2186     2175182 :     for (i=n-1; i>0; i--)
    2187             :     {
    2188     1532521 :       av = avma; m = i == k? t: gen_0;
    2189     5799598 :       for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2190     1532289 :       gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
    2191             :     }
    2192             :   }
    2193      237180 :   return c;
    2194             : }
    2195             : 
    2196             : /* C = A^(-1)(tB) where A, B, C are integral, A is upper triangular, t t_INT */
    2197             : GEN
    2198      201746 : hnf_divscale(GEN A, GEN B, GEN t)
    2199             : {
    2200      201746 :   long n = lg(A)-1, i,j,k;
    2201      201746 :   GEN m, c = cgetg(n+1,t_MAT);
    2202             : 
    2203      201746 :   if (!n) return c;
    2204      889919 :   for (k=1; k<=n; k++)
    2205             :   {
    2206      688177 :     GEN u = cgetg(n+1, t_COL), b = gel(B,k);
    2207      688176 :     pari_sp av = avma;
    2208      688176 :     gel(c,k) = u; m = mulii(gel(b,n),t);
    2209      688166 :     gel(u,n) = gerepileuptoint(av, diviiexact(m, gcoeff(A,n,n)));
    2210     3929798 :     for (i=n-1; i>0; i--)
    2211             :     {
    2212     3241625 :       av = avma; m = mulii(gel(b,i),t);
    2213    30151582 :       for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2214     3241565 :       gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
    2215             :     }
    2216             :   }
    2217      201742 :   return c;
    2218             : }
    2219             : 
    2220             : /* A, B integral upper HNF. A^(-1) B integral ? */
    2221             : int
    2222         133 : hnfdivide(GEN A, GEN B)
    2223             : {
    2224         133 :   pari_sp av = avma;
    2225         133 :   long n = lg(A)-1, i,j,k;
    2226             :   GEN u, b, m, r;
    2227             : 
    2228         133 :   if (!n) return 1;
    2229         133 :   if (lg(B)-1 != n) pari_err_DIM("hnfdivide");
    2230         133 :   u = cgetg(n+1, t_COL);
    2231         483 :   for (k=1; k<=n; k++)
    2232             :   {
    2233         364 :     b = gel(B,k);
    2234         364 :     m = gel(b,k);
    2235         364 :     gel(u,k) = dvmdii(m, gcoeff(A,k,k), &r);
    2236         364 :     if (r != gen_0) return gc_long(av, 0);
    2237         826 :     for (i=k-1; i>0; i--)
    2238             :     {
    2239         476 :       m = gel(b,i);
    2240        1337 :       for (j=i+1; j<=k; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2241         476 :       m = dvmdii(m, gcoeff(A,i,i), &r);
    2242         476 :       if (r != gen_0) return gc_long(av, 0);
    2243         476 :       gel(u,i) = m;
    2244             :     }
    2245             :   }
    2246         119 :   return gc_long(av, 1);
    2247             : }
    2248             : 
    2249             : /* A upper HNF, b integral vector. Return A^(-1) b if integral,
    2250             :  * NULL otherwise. Assume #A[,1] = #b. */
    2251             : GEN
    2252      366159 : hnf_invimage(GEN A, GEN b)
    2253             : {
    2254      366159 :   pari_sp av = avma;
    2255      366159 :   long n = lg(A)-1, m, i, k;
    2256             :   GEN u, r;
    2257             : 
    2258      366159 :   if (!n) return lg(b)==1? cgetg(1,t_COL):NULL;
    2259      366117 :   m = nbrows(A); /* m >= n */
    2260      366119 :   u = cgetg(n+1, t_COL);
    2261      830308 :   for (i = n, k = m; k > 0; k--)
    2262             :   {
    2263      830308 :     pari_sp av2 = avma;
    2264             :     long j;
    2265      830308 :     GEN t = gel(b,k), Aki = gcoeff(A,k,i);
    2266      830308 :     if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
    2267     2000244 :     for (j=i+1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
    2268      830270 :     if (!signe(Aki))
    2269             :     {
    2270           0 :       if (signe(t)) return gc_NULL(av);
    2271           0 :       set_avma(av2); gel(u,i) = gen_0; continue;
    2272             :     }
    2273      830270 :     t = dvmdii(t, Aki, &r);
    2274      830223 :     if (r != gen_0) return gc_NULL(av);
    2275      607868 :     gel(u,i) = gerepileuptoint(av2, t);
    2276      607956 :     if (--i == 0) break;
    2277             :   }
    2278             :   /* If there is a solution, it must be u. Check remaining equations */
    2279      287511 :   for (; k > 0; k--)
    2280             :   {
    2281      143759 :     pari_sp av2 = avma;
    2282             :     long j;
    2283      143759 :     GEN t = gel(b,k);
    2284      143759 :     if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
    2285      550080 :     for (j=1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
    2286      143753 :     if (signe(t)) return gc_NULL(av);
    2287      143753 :     set_avma(av2);
    2288             :   }
    2289      143752 :   return u;
    2290             : }
    2291             : 
    2292             : /* A upper HNF, B integral matrix or column. Return A^(-1) B if integral,
    2293             :  * NULL otherwise */
    2294             : GEN
    2295      314915 : hnf_solve(GEN A, GEN B)
    2296             : {
    2297             :   pari_sp av;
    2298             :   long i, l;
    2299             :   GEN C;
    2300             : 
    2301      314915 :   if (typ(B) == t_COL) return hnf_invimage(A, B);
    2302      252198 :   av = avma; C = cgetg_copy(B, &l);
    2303      351481 :   for (i = 1; i < l; i++)
    2304             :   {
    2305      266473 :     GEN c = hnf_invimage(A, gel(B,i));
    2306      266457 :     if (!c) return gc_NULL(av);
    2307       99266 :     gel(C,i) = c;
    2308             :   }
    2309       85008 :   return C;
    2310             : }
    2311             : 
    2312             : /***************************************************************/
    2313             : /**                                                           **/
    2314             : /**               SMITH NORMAL FORM REDUCTION                 **/
    2315             : /**                                                           **/
    2316             : /***************************************************************/
    2317             : 
    2318             : static GEN
    2319           0 : trivsmith(long all)
    2320             : {
    2321             :   GEN z;
    2322           0 :   if (!all) return cgetg(1,t_VEC);
    2323           0 :   z=cgetg(4,t_VEC);
    2324           0 :   gel(z,1) = cgetg(1,t_MAT);
    2325           0 :   gel(z,2) = cgetg(1,t_MAT);
    2326           0 :   gel(z,3) = cgetg(1,t_MAT); return z;
    2327             : }
    2328             : 
    2329             : static void
    2330          48 : snf_pile1(pari_sp av, GEN *x, GEN *U)
    2331             : {
    2332             :   GEN *gptr[2];
    2333          48 :   int c = 1; gptr[0]=x;
    2334          48 :   if (*U) gptr[c++] = U;
    2335          48 :   gerepilemany(av,gptr,c);
    2336          48 : }
    2337             : static void
    2338      984738 : snf_pile(pari_sp av, GEN *x, GEN *U, GEN *V)
    2339             : {
    2340             :   GEN *gptr[3];
    2341      984738 :   int c = 1; gptr[0]=x;
    2342      984738 :   if (*U) gptr[c++] = U;
    2343      984738 :   if (*V) gptr[c++] = V;
    2344      984738 :   gerepilemany(av,gptr,c);
    2345      984809 : }
    2346             : 
    2347             : static GEN
    2348      702634 : bezout_step(GEN *pa, GEN *pb, GEN *pu, GEN *pv)
    2349             : {
    2350      702634 :   GEN a = *pa, b = *pb, d;
    2351      702634 :   if (absequalii(a,b))
    2352             :   {
    2353      418351 :     long sa = signe(a), sb = signe(b);
    2354      418351 :     *pv = gen_0;
    2355      418351 :     if (sb == sa) {
    2356      410689 :       *pa = *pb = gen_1;
    2357      410689 :       if (sa > 0) {*pu=gen_1; return a;} else {*pu=gen_m1; return absi(a);}
    2358             :     }
    2359        7662 :     if (sa > 0) { *pa = *pu = gen_1; *pb = gen_m1; return a; }
    2360        1162 :     *pa = *pu = gen_m1; *pb = gen_1; return b;
    2361             :   }
    2362      284287 :   d = bezout(a,b, pu,pv);
    2363      284302 :   *pa = diviiexact(a, d);
    2364      284291 :   *pb = diviiexact(b, d); return d;
    2365             : }
    2366             : 
    2367             : static int
    2368      520204 : negcmpii(void *E, GEN x, GEN y) { (void)E; return -cmpii(x,y); }
    2369             : 
    2370             : /* x square of maximal rank; does b = x[i,i] divide all entries in
    2371             :  * x[1..i-1, 1..i-1] ? If so, return 0; else the index of a problematic row */
    2372             : static long
    2373     1076986 : ZM_snf_no_divide(GEN x, long i)
    2374             : {
    2375     1076986 :   GEN b = gcoeff(x,i,i);
    2376             :   long j, k;
    2377             : 
    2378     1076986 :   if (is_pm1(b)) return 0;
    2379      912843 :   for (k = 1; k < i; k++)
    2380     2267827 :     for (j = 1; j < i; j++)
    2381     1791581 :       if (!dvdii(gcoeff(x,k,j),b)) return k;
    2382      283939 :   return 0;
    2383             : }
    2384             : 
    2385             : static void
    2386     1388270 : ZM_redpart(GEN x, GEN p, long I)
    2387             : {
    2388     1388270 :   long l = lgefint(p), i, j;
    2389     5914095 :   for (i = 1; i <= I; i++)
    2390    27221971 :     for (j = 1; j <= I; j++)
    2391             :     {
    2392    22696146 :       GEN c = gcoeff(x,i,j);
    2393    22696146 :       if (lgefint(c) > l) gcoeff(x,i,j) = remii(c, p);
    2394             :     }
    2395     1388270 : }
    2396             : static void
    2397      862899 : ZMrow_divexact_inplace(GEN M, long i, GEN c)
    2398             : {
    2399      862899 :   long j, l = lg(M);
    2400     3419895 :   for (j = 1; j < l; j++) gcoeff(M,i,j) = diviiexact(gcoeff(M,i,j), c);
    2401      863022 : }
    2402             : 
    2403             : /* Return the SNF D of matrix X. If ptU/ptV non-NULL set them to U/V
    2404             :  * to that D = UXV */
    2405             : GEN
    2406      722032 : ZM_snfall_i(GEN x, GEN *ptU, GEN *ptV, long flag)
    2407             : {
    2408      722032 :   pari_sp av0 = avma, av;
    2409      722032 :   const long return_vec = flag & 1;
    2410             :   long i, j, k, m0, m, n0, n;
    2411      722032 :   GEN u, v, U, V, V0, mdet, A = NULL, perm = NULL;
    2412             : 
    2413      722032 :   n0 = n = lg(x)-1;
    2414      722032 :   if (!n) {
    2415       41182 :     if (ptU) *ptU = cgetg(1,t_MAT);
    2416       41182 :     if (ptV) *ptV = cgetg(1,t_MAT);
    2417       41181 :     return cgetg(1, return_vec? t_VEC: t_MAT);
    2418             :   }
    2419      680850 :   m0 = m = nbrows(x);
    2420             : 
    2421      680861 :   U = V = V0 = NULL; /* U = TRANSPOSE of row transform matrix [act on columns]*/
    2422      680861 :   if (m == n && ZM_ishnf(x))
    2423             :   {
    2424      611205 :     mdet = ZM_det_triangular(x); /* != 0 */
    2425      611116 :     if (ptV) *ptV = matid(n);
    2426             :   }
    2427             :   else
    2428             :   {
    2429       69679 :     mdet = ZM_detmult(x);
    2430       69691 :     if (!signe(mdet))
    2431          77 :       x = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
    2432             :     else
    2433             :     { /* m <= n */
    2434       69614 :       if (!ptV)
    2435        9520 :         x = ZM_hnfmod(x,mdet);
    2436       60094 :       else if (m == n)
    2437             :       {
    2438       60066 :         GEN H = ZM_hnfmod(x,mdet);
    2439       60066 :         *ptV = ZM_gauss(x,H);
    2440       60066 :         x = H;
    2441             :       }
    2442             :       else
    2443          28 :         x = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
    2444       69614 :       mdet = ZM_det_triangular(x); /* > 0 */
    2445             :     }
    2446       69690 :     n = lg(x)-1; /* n independent columns */
    2447       69690 :     if (ptV)
    2448             :     {
    2449       60114 :       V = *ptV;
    2450       60114 :       if (n != n0)
    2451             :       {
    2452          28 :         V0 = vecslice(V, 1, n0 - n); /* kernel */
    2453          28 :         V  = vecslice(V, n0-n+1, n0);
    2454             :       }
    2455             :     }
    2456       69690 :     if (!signe(mdet))
    2457             :     {
    2458          77 :       if (n)
    2459             :       {
    2460          70 :         x = ZM_snfall_i(shallowtrans(x), ptV, ptU, return_vec); /* swap V,U */
    2461          70 :         if (!return_vec && n != m) x = shallowtrans(x);
    2462          70 :         if (ptV) V = ZM_mul(V, shallowtrans(*ptV));
    2463          70 :         if (ptU) U = *ptU; /* TRANSPOSE */
    2464             :       }
    2465             :       else /* 0 matrix */
    2466             :       {
    2467           7 :         x = cgetg(1,t_MAT);
    2468           7 :         if (ptV) V = cgetg(1, t_MAT);
    2469           7 :         if (ptU) U = matid(m);
    2470             :       }
    2471          77 :       goto THEEND;
    2472             :     }
    2473             :   }
    2474      680816 :   if (ptV || ptU) U = matid(n); /* we will compute V in terms of U */
    2475      680833 :   if (DEBUGLEVEL>7) err_printf("starting SNF loop");
    2476             : 
    2477             :   /* square, maximal rank n */
    2478      680833 :   A = x; x = shallowcopy(x); av = avma;
    2479     1605159 :   for (i = n; i > 1; i--)
    2480             :   {
    2481      924341 :     if (DEBUGLEVEL>7) err_printf("\ni = %ld: ",i);
    2482             :     for(;;)
    2483      703458 :     {
    2484     1627774 :       int c = 0;
    2485             :       GEN a, b;
    2486     4669239 :       for (j = i-1; j >= 1; j--)
    2487             :       {
    2488     3041619 :         b = gcoeff(x,i,j); if (!signe(b)) continue;
    2489      267032 :         a = gcoeff(x,i,i);
    2490      267032 :         ZC_elem(b, a, x,NULL, j,i);
    2491      267023 :         if (gc_needed(av,1))
    2492             :         {
    2493           0 :           if (DEBUGMEM>1) pari_warn(warnmem,"[1]: ZM_snfall i = %ld", i);
    2494           0 :           snf_pile1(av, &x,&U);
    2495             :         }
    2496             :       }
    2497     1627620 :       if (DEBUGLEVEL>7) err_printf("; ");
    2498     4669211 :       for (j=i-1; j>=1; j--)
    2499             :       {
    2500             :         GEN d;
    2501     3041585 :         b = gcoeff(x,j,i); if (!signe(b)) continue;
    2502      702629 :         a = gcoeff(x,i,i);
    2503      702629 :         d = bezout_step(&a, &b, &u, &v);
    2504     4189965 :         for (k = 1; k < i; k++)
    2505             :         {
    2506     3487413 :           GEN t = addii(mulii(u,gcoeff(x,i,k)),mulii(v,gcoeff(x,j,k)));
    2507     3487440 :           gcoeff(x,j,k) = subii(mulii(a,gcoeff(x,j,k)),
    2508     3487448 :                                 mulii(b,gcoeff(x,i,k)));
    2509     3487347 :           gcoeff(x,i,k) = t;
    2510             :         }
    2511      702552 :         gcoeff(x,j,i) = gen_0;
    2512      702552 :         gcoeff(x,i,i) = d;
    2513      702552 :         if (U) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
    2514      702576 :         if (gc_needed(av,1))
    2515             :         {
    2516          48 :           if (DEBUGMEM>1) pari_warn(warnmem,"[2]: ZM_snfall, i = %ld", i);
    2517          48 :           snf_pile1(av, &x,&U);
    2518             :         }
    2519      702578 :         c = 1;
    2520             :       }
    2521     1627626 :       if (!c)
    2522             :       {
    2523     1076987 :         k = ZM_snf_no_divide(x, i);
    2524     1076927 :         if (!k) break;
    2525             : 
    2526             :         /* x[k,j] != 0 mod b */
    2527      582442 :         for (j = 1; j <= i; j++)
    2528      429851 :           gcoeff(x,i,j) = addii(gcoeff(x,i,j),gcoeff(x,k,j));
    2529      152591 :         if (U) gel(U,i) = gadd(gel(U,i),gel(U,k));
    2530             :       }
    2531      703227 :       ZM_redpart(x, mdet, i);
    2532      703418 :       if (U && (flag & 2)) ZM_redpart(U, mdet, n);
    2533      703458 :       if (gc_needed(av,1))
    2534             :       {
    2535           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"[3]: ZM_snfall");
    2536           0 :         snf_pile1(av, &x,&U);
    2537             :       }
    2538             :     }
    2539             :   }
    2540      680818 :   if (DEBUGLEVEL>7) err_printf("\n");
    2541     2285075 :   for (k = n; k; k--)
    2542             :   {
    2543     1604676 :     GEN d = gcdii(gcoeff(x,k,k), mdet);
    2544     1604551 :     gcoeff(x,k,k) = d;
    2545     1604551 :     if (!is_pm1(d)) mdet = diviiexact(mdet,d);
    2546             :   }
    2547      680399 : THEEND:
    2548      680476 :   if (U) U = shallowtrans(U);
    2549      680675 :   if (ptV && A)
    2550             :   { /* U A V = D => D^(-1) U A = V^(-1) */
    2551      666547 :     long l = lg(x);
    2552      666547 :     GEN W = ZM_mul(U, A);
    2553     1529177 :     for (i = 1; i < l; i++)
    2554             :     {
    2555     1336826 :       GEN c = gcoeff(x,i,i);
    2556     1336826 :       if (is_pm1(c)) break; /* only 1 from now on */
    2557      862611 :       ZMrow_divexact_inplace(W, i, c);
    2558             :     }
    2559      666587 :     if (flag & 2)
    2560             :     {
    2561      642263 :       W = FpM_red(W, gcoeff(x,1,1));
    2562      642396 :       W = matinvmod(W, gcoeff(x,1,1));
    2563             :     }
    2564             :     else
    2565       24324 :       W = ZM_inv(W, NULL);
    2566      666562 :     V = V? ZM_mul(V, W): W;
    2567             :   }
    2568      680676 :   if (return_vec)
    2569             :   {
    2570      656656 :     long l = lg(x)-1;
    2571      656656 :     if (typ(x) == t_MAT) x = RgM_diagonal_shallow(x);
    2572      656655 :     if (m0 > l) x = shallowconcat(zerovec(m0-l), x);
    2573             :   }
    2574             : 
    2575      680675 :   if (V0)
    2576             :   { /* add kernel */
    2577          28 :     if (!return_vec) x = shallowconcat(zeromat(m,n0-n), x);
    2578          28 :     if (ptV) V = shallowconcat(V0, V);
    2579             :   }
    2580      680675 :   if (perm && U) U = vecpermute(U, perm_inv(perm));
    2581      680675 :   snf_pile(av0, &x,&U,&V);
    2582      680945 :   if (ptU) *ptU = U;
    2583      680945 :   if (ptV) *ptV = V;
    2584      680945 :   return x;
    2585             : }
    2586             : GEN
    2587       63973 : ZM_snfall(GEN x, GEN *U, GEN *V) { return ZM_snfall_i(x, U, V, 0); }
    2588             : GEN
    2589       10843 : ZM_snf(GEN x) { return ZM_snfall_i(x, NULL,NULL, 1); }
    2590             : GEN
    2591         385 : smith(GEN x) { return ZM_snfall_i(x, NULL,NULL, 1); }
    2592             : GEN
    2593          35 : smithall(GEN x)
    2594             : {
    2595          35 :   GEN z = cgetg(4, t_VEC);
    2596          35 :   gel(z,3) = ZM_snfall_i(x, (GEN*)(z+1),(GEN*)(z+2), 0);
    2597          35 :   return z;
    2598             : }
    2599             : 
    2600             : GEN
    2601      214281 : ZV_snfclean(GEN d)
    2602             : {
    2603      214281 :   long c, l = lg(d);
    2604      226649 :   for (c = 1; c < l; c++) { GEN t = gel(d,c); if (is_pm1(t)) break; }
    2605      214281 :   return c == l? d: vec_shorten(d, c-1);
    2606             : }
    2607             : void
    2608      947625 : ZM_snfclean(GEN d, GEN u, GEN v)
    2609             : {
    2610      947625 :   long i, c, l = lg(d);
    2611             : 
    2612      947625 :   if (typ(d) == t_VEC)
    2613     2432165 :     for (c=1; c<l; c++) { GEN t = gel(d,c); if (is_pm1(t)) break; }
    2614             :   else
    2615             :   {
    2616           0 :     for (c=1; c<l; c++) { GEN t = gcoeff(d,c,c); if (is_pm1(t)) break; }
    2617           0 :     if (c < l) for (i = 1; i < c; i++) setlg(gel(d,i), c);
    2618             :   }
    2619      947622 :   setlg(d, c);
    2620     2927731 :   if (u) for (i=1; i<l; i++) setlg(gel(u,i), c);
    2621      947593 :   if (v) setlg(v, c);
    2622      947590 : }
    2623             : 
    2624             : /* Assume z was computed by [g]smithall(). Remove the 1s on the diagonal */
    2625             : GEN
    2626         693 : smithclean(GEN z)
    2627             : {
    2628             :   long i, j, h, l, c, d;
    2629             :   GEN U, V, y, D, t;
    2630             : 
    2631         693 :   if (typ(z) != t_VEC) pari_err_TYPE("smithclean",z);
    2632         693 :   l = lg(z); if (l == 1) return cgetg(1,t_VEC);
    2633         686 :   U = gel(z,1);
    2634         686 :   if (l != 4 || typ(U) != t_MAT)
    2635             :   { /* assume z = vector of elementary divisors */
    2636        1827 :     for (c=1; c<l; c++)
    2637        1519 :       if (gequal1(gel(z,c))) break;
    2638         665 :     return gcopy_lg(z, c);
    2639             :   }
    2640          21 :   V = gel(z,2);
    2641          21 :   D = gel(z,3);
    2642          21 :   l = lg(D);
    2643          21 :   if (l == 1) return gcopy(z);
    2644          21 :   h = lgcols(D);
    2645          21 :   if (h > l)
    2646             :   { /* D = vconcat(zero matrix, diagonal matrix) */
    2647          21 :     for (c=1+h-l, d=1; c<h; c++,d++)
    2648          21 :       if (gequal1(gcoeff(D,c,d))) break;
    2649             :   }
    2650           7 :   else if (h < l)
    2651             :   { /* D = concat(zero matrix, diagonal matrix) */
    2652           7 :     for (c=1, d=1+l-h; d<l; c++,d++)
    2653           7 :       if (gequal1(gcoeff(D,c,d))) break;
    2654             :   }
    2655             :   else
    2656             :   { /* D diagonal */
    2657           0 :     for (c=1; c<l; c++)
    2658           0 :       if (gequal1(gcoeff(D,c,c))) break;
    2659           0 :     d = c;
    2660             :   }
    2661             :   /* U was (h-1)x(h-1), V was (l-1)x(l-1), D was (h-1)x(l-1) */
    2662          21 :   y = cgetg(4,t_VEC);
    2663             :   /* truncate U to (c-1) x (h-1) */
    2664          21 :   gel(y,1) = t = cgetg(h,t_MAT);
    2665          77 :   for (j=1; j<h; j++) gel(t,j) = gcopy_lg(gel(U,j), c);
    2666             :   /* truncate V to (l-1) x (d-1) */
    2667          21 :   gel(y,2) = gcopy_lg(V, d);
    2668          21 :   gel(y,3) = t = zeromatcopy(c-1, d-1);
    2669             :   /* truncate D to a (c-1) x (d-1) matrix */
    2670          21 :   if (d > 1)
    2671             :   {
    2672          14 :     if (h > l)
    2673             :     {
    2674          14 :       for (i=1+h-l, j=1; i<c; i++,j++)
    2675           7 :         gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
    2676             :     }
    2677           7 :     else if (h < l)
    2678             :     {
    2679           7 :       for (i=1, j=1+l-h; j<d; i++,j++)
    2680           0 :         gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
    2681             :     }
    2682             :     else
    2683             :     {
    2684           0 :       for (j=1; j<d; j++)
    2685           0 :         gcoeff(t,j,j) = gcopy(gcoeff(D,j,j));
    2686             :     }
    2687             :   }
    2688          21 :   return y;
    2689             : }
    2690             : 
    2691             : /* does b = x[i,i] divide all entries in x[1..i-1,1..i-1] ? If so, return 0;
    2692             :  * else return the index of a problematic row */
    2693             : static long
    2694         196 : gsnf_no_divide(GEN x, long i, long vx)
    2695             : {
    2696         196 :   GEN b = gcoeff(x,i,i);
    2697             :   long j, k;
    2698             : 
    2699         196 :   if (gequal0(b))
    2700             :   {
    2701          14 :     for (k = 1; k < i; k++)
    2702          14 :       for (j = 1; j < i; j++)
    2703          14 :         if (!gequal0(gcoeff(x,k,j))) return k;
    2704           0 :     return 0;
    2705             :   }
    2706             : 
    2707         182 :   if (!is_RgX(b,vx) || degpol(b)<=0) return 0;
    2708         217 :   for (k = 1; k < i; k++)
    2709         378 :     for (j = 1; j < i; j++)
    2710             :     {
    2711         266 :       GEN z = gcoeff(x,k,j), r;
    2712         266 :       if (!is_RgX(z,vx)) z = scalarpol(z, vx);
    2713         266 :       r = RgX_rem(z, b);
    2714         266 :       if (signe(r) && (! isinexactreal(r) ||
    2715           0 :              gexpo(r) > 16 + gexpo(b) - gprecision(r))
    2716          35 :          ) return k;
    2717             :     }
    2718          70 :   return 0;
    2719             : }
    2720             : 
    2721             : /* Hermite Normal Form, with base change matrix if ptB != NULL.
    2722             :  * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
    2723             :  * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
    2724             : GEN
    2725          42 : RgM_hnfall(GEN A, GEN *pB, long remove)
    2726             : {
    2727             :   pari_sp av;
    2728             :   long li, j, k, m, n, def, ldef;
    2729             :   GEN B;
    2730          42 :   long vx = gvar(A);
    2731             : 
    2732          42 :   n = lg(A)-1;
    2733          42 :   if (vx==NO_VARIABLE || !n)
    2734             :   {
    2735           0 :     RgM_check_ZM(A, "mathnf0");
    2736           0 :     return ZM_hnfall(A, pB, remove);
    2737             :   }
    2738          42 :   m = nbrows(A);
    2739          42 :   av = avma;
    2740          42 :   A = RgM_shallowcopy(A);
    2741          42 :   B = pB? matid(n): NULL;
    2742          42 :   def = n; ldef = (m>n)? m-n: 0;
    2743         126 :   for (li=m; li>ldef; li--)
    2744             :   {
    2745             :     GEN d, T;
    2746         714 :     for (j=def-1; j; j--)
    2747             :     {
    2748         630 :       GEN a = gcoeff(A,li,j);
    2749         630 :       if (gequal0(a)) continue;
    2750             : 
    2751         511 :       k = (j==1)? def: j-1;
    2752         511 :       RgC_elem(a,gcoeff(A,li,k), A,B, j,k, li, vx);
    2753             :     }
    2754          84 :     T = normalize_as_RgX(gcoeff(A,li,def), vx, &d);
    2755          84 :     if (gequal0(T))
    2756           0 :     { if (ldef) ldef--; }
    2757             :     else
    2758             :     {
    2759          84 :       if (!gequal1(d))
    2760             :       {
    2761           7 :         gel(A,def) = RgC_Rg_div(gel(A,def), d);
    2762           7 :         if (B) gel(B, def) = RgC_Rg_div(gel(B, def), d);
    2763             :       }
    2764          84 :       RgM_reduce(A, B, li, def, vx);
    2765          84 :       def--;
    2766             :     }
    2767          84 :     if (gc_needed(av,1))
    2768             :     {
    2769           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ghnfall");
    2770           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2771             :     }
    2772             :   }
    2773             :   /* rank A = n - def */
    2774          42 :   if (remove) remove_0cols(def, &A, &B, remove);
    2775          42 :   gerepileall(av, B? 2: 1, &A, &B);
    2776          42 :   if (B) *pB = B;
    2777          42 :   return A;
    2778             : }
    2779             : 
    2780             : static GEN
    2781          49 : RgXM_snf(GEN x,long all)
    2782             : {
    2783             :   pari_sp av;
    2784             :   long i, j, k, n;
    2785             :   GEN z, u, v, U, V;
    2786          49 :   long vx = gvar(x);
    2787          49 :   n = lg(x)-1; if (!n) return trivsmith(all);
    2788          49 :   if (vx==NO_VARIABLE) pari_err_TYPE("RgXM_snf",x);
    2789          49 :   if (lgcols(x) != n+1) pari_err_DIM("gsmithall");
    2790          49 :   av = avma;
    2791          49 :   x = RgM_shallowcopy(x);
    2792          49 :   if (all) { U = matid(n); V = matid(n); }
    2793         196 :   for (i=n; i>=2; i--)
    2794             :   {
    2795             :     for(;;)
    2796         168 :     {
    2797             :       GEN a, b, d;
    2798         315 :       int c = 0;
    2799        1092 :       for (j=i-1; j>=1; j--)
    2800             :       {
    2801         777 :         b = gcoeff(x,i,j); if (gequal0(b)) continue;
    2802         196 :         a = gcoeff(x,i,i);
    2803         196 :         d = gbezout_step(&b, &a, &v, &u, vx);
    2804         700 :         for (k = 1; k < i; k++)
    2805             :         {
    2806         504 :           GEN t = gadd(gmul(u,gcoeff(x,k,i)),gmul(v,gcoeff(x,k,j)));
    2807         504 :           gcoeff(x,k,j) = gsub(gmul(a,gcoeff(x,k,j)),gmul(b,gcoeff(x,k,i)));
    2808         504 :           gcoeff(x,k,i) = t;
    2809             :         }
    2810         196 :         gcoeff(x,i,j) = gen_0;
    2811         196 :         gcoeff(x,i,i) = d;
    2812         196 :         if (all) update(u,v,a,b,(GEN*)(V+i),(GEN*)(V+j));
    2813             :       }
    2814        1092 :       for (j=i-1; j>=1; j--)
    2815             :       {
    2816         777 :         b = gcoeff(x,j,i); if (gequal0(b)) continue;
    2817         175 :         a = gcoeff(x,i,i);
    2818         175 :         d = gbezout_step(&b, &a, &v, &u, vx);
    2819         651 :         for (k = 1; k < i; k++)
    2820             :         {
    2821         476 :           GEN t = gadd(gmul(u,gcoeff(x,i,k)),gmul(v,gcoeff(x,j,k)));
    2822         476 :           gcoeff(x,j,k) = gsub(gmul(a,gcoeff(x,j,k)),gmul(b,gcoeff(x,i,k)));
    2823         476 :           gcoeff(x,i,k) = t;
    2824             :         }
    2825         175 :         gcoeff(x,j,i) = gen_0;
    2826         175 :         gcoeff(x,i,i) = d;
    2827         175 :         if (all) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
    2828         175 :         c = 1;
    2829             :       }
    2830         315 :       if (!c)
    2831             :       {
    2832         196 :         k = gsnf_no_divide(x, i, vx);
    2833         196 :         if (!k) break;
    2834             : 
    2835         245 :         for (j=1; j<=i; j++)
    2836         196 :           gcoeff(x,i,j) = gadd(gcoeff(x,i,j),gcoeff(x,k,j));
    2837          49 :         if (all) gel(U,i) = gadd(gel(U,i),gel(U,k));
    2838             :       }
    2839         168 :       if (gc_needed(av,1))
    2840             :       {
    2841           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"gsmithall");
    2842           0 :         gerepileall(av, all? 3: 1, &x, &U, &V);
    2843             :       }
    2844             :     }
    2845             :   }
    2846         245 :   for (k=1; k<=n; k++)
    2847             :   {
    2848         196 :     GEN d, T = normalize_as_RgX(gcoeff(x,k,k), vx, &d);
    2849         196 :     if (gequal0(T)) continue;
    2850         182 :     if (all && !gequal1(d)) gel(V,k) = RgC_Rg_div(gel(V,k), d);
    2851         182 :     gcoeff(x,k,k) = T;
    2852             :   }
    2853          49 :   z = all? mkvec3(shallowtrans(U), V, x): RgM_diagonal_shallow(x);
    2854          49 :   return gerepilecopy(av, z);
    2855             : }
    2856             : 
    2857             : GEN
    2858         469 : matsnf0(GEN x,long flag)
    2859             : {
    2860         469 :   pari_sp av = avma;
    2861         469 :   if (flag > 7) pari_err_FLAG("matsnf");
    2862         469 :   if (typ(x) == t_VEC && flag & 4) return smithclean(x);
    2863         469 :   if (typ(x)!=t_MAT) pari_err_TYPE("matsnf",x);
    2864         469 :   if (RgM_is_ZM(x)) x = flag&1 ? smithall(x): smith(x);
    2865          49 :   else              x = RgXM_snf(x, flag&1);
    2866         469 :   if (flag & 4) x = gerepileupto(av, smithclean(x));
    2867         469 :   return x;
    2868             : }
    2869             : GEN
    2870           0 : gsmith(GEN x) { return RgXM_snf(x,0); }
    2871             : GEN
    2872           0 : gsmithall(GEN x) { return RgXM_snf(x,1); }
    2873             : 
    2874             : /* H is a relation matrix, either in HNF or a t_VEC (diagonal HNF) */
    2875             : static GEN
    2876      947630 : snf_group(GEN H, GEN D, GEN *newU, GEN *newUi)
    2877             : {
    2878             :   long i, j, l;
    2879             : 
    2880      947630 :   ZM_snfclean(D, newU? *newU: NULL, newUi? *newUi: NULL);
    2881      947588 :   l = lg(D);
    2882      947588 :   if (newU) {
    2883      825701 :     GEN U = *newU;
    2884     2129379 :     for (i = 1; i < l; i++)
    2885             :     {
    2886     1304243 :       GEN d = gel(D,i), d2 = shifti(d, 1);
    2887     5096332 :       for (j = 1; j < lg(U); j++)
    2888     3792654 :         gcoeff(U,i,j) = centermodii(gcoeff(U,i,j), d, d2);
    2889             :     }
    2890      825136 :     *newU = U;
    2891             :   }
    2892      947023 :   if (newUi && l > 1)
    2893             :   { /* UHV=D -> U^-1 = (HV)D^-1 -> U^-1 = H(VD^-1 mod 1) mod H */
    2894             :     /* Ui = ZM_inv(U, NULL); setlg(Ui, l); */
    2895      859977 :     GEN V = *newUi, Ui;
    2896      859977 :     int Hvec = (typ(H) == t_VEC);
    2897     2343405 :     for (i = 1; i < l; i++) gel(V,i) = FpC_red(gel(V,i), gel(D,i));
    2898      859940 :     if (!Hvec)
    2899             :     {
    2900      556500 :       if (ZM_isdiagonal(H)) { H = RgM_diagonal_shallow(H); Hvec = 1; }
    2901             :     }
    2902      860104 :     Ui = Hvec? ZM_diag_mul(H, V): ZM_mul(H, V);
    2903     2343163 :     for (i = 1; i < l; i++) gel(Ui,i) = ZC_Z_divexact(gel(Ui,i), gel(D,i));
    2904      859846 :     *newUi = Hvec? ZM_ZV_mod(Ui, H): ZM_hnfrem(Ui, H);
    2905             :   }
    2906      947093 :   return D;
    2907             : }
    2908             : /* H relation matrix among row of generators g in HNF.  Let URV = D its SNF,
    2909             :  * newU R newV = newD its clean SNF (no 1 in Dnew). Return the diagonal of
    2910             :  * newD, newU and newUi such that  1/U = (newUi, ?).
    2911             :  * Rationale: let (G,0) = g Ui be the new generators then
    2912             :  * 0 = G U R --> G D = 0,  g = G newU  and  G = g newUi */
    2913             : GEN
    2914      643680 : ZM_snf_group(GEN H, GEN *newU, GEN *newUi)
    2915             : {
    2916      643680 :   GEN D = ZM_snfall_i(H, newU, newUi, 1 + 2);
    2917      643775 :   return snf_group(H, D, newU, newUi);
    2918             : }
    2919             : 
    2920             : /* D a ZV: SNF for matdiagonal(D). Faster because we only ensure elementary
    2921             :  * divisors condition: d[n] | ... | d[1] and need not convert D to matrix form*/
    2922             : GEN
    2923      303866 : ZV_snfall(GEN D, GEN *pU, GEN *pV)
    2924             : {
    2925      303866 :   pari_sp av = avma;
    2926      303866 :   long j, n = lg(D)-1;
    2927      303866 :   GEN U = pU? matid(n): NULL;
    2928      303870 :   GEN V = pV? matid(n): NULL;
    2929             :   GEN p;
    2930             : 
    2931      303870 :   D = leafcopy(D);
    2932      966620 :   for (j = n; j > 0; j--)
    2933             :   {
    2934      662750 :     GEN b = gel(D,j);
    2935      662750 :     if (signe(b) < 0)
    2936             :     {
    2937           0 :       gel(D,j) = negi(b);
    2938           0 :       if (V) ZV_togglesign(gel(V,j));
    2939             :     }
    2940             :   }
    2941             :   /* entries are nonnegative integers */
    2942      303870 :   p = gen_indexsort(D, NULL, &negcmpii);
    2943      303868 :   D = vecpermute(D, p);
    2944      303868 :   if (U) U = vecpermute(U, p);
    2945      303866 :   if (V) V = vecpermute(V, p);
    2946             :   /* entries are sorted by decreasing value */
    2947      966573 :   for (j = n; j > 0; j--)
    2948             :   {
    2949      662707 :     GEN b = gel(D,j);
    2950             :     long i;
    2951     1184246 :     for (i = j-1; i > 0; i--)
    2952             :     { /* invariant: a >= b. If au+bv = d is a Bezout relation, A=a/d and B=b/d
    2953             :        * we have [B,-A;u,v]*diag(a,b)*[1-u*A,1; -u*A,1]] = diag(Ab, d) */
    2954      533732 :       GEN a = gel(D,i), u,v, d = bezout(a,b, &u,&v), A, Wi, Wj;
    2955      533731 :       if (equalii(d,b)) continue;
    2956       70389 :       A = diviiexact(a,d);
    2957       70389 :       if (V)
    2958             :       {
    2959       70333 :         GEN t = mulii(u,A);
    2960       70332 :         Wi = ZC_lincomb(subui(1,t), negi(t), gel(V,i), gel(V,j));
    2961       70332 :         Wj = ZC_add(gel(V,i), gel(V,j));
    2962       70332 :         gel(V,i) = Wi;
    2963       70332 :         gel(V,j) = Wj;
    2964             :       }
    2965       70388 :       if (U)
    2966             :       {
    2967       70388 :         GEN B = diviiexact(b,d);
    2968       70388 :         Wi = ZC_lincomb(B, negi(A), gel(U,i), gel(U,j));
    2969       70388 :         Wj = ZC_lincomb(u, v, gel(U,i), gel(U,j));
    2970       70388 :         gel(U,i) = Wi;
    2971       70388 :         gel(U,j) = Wj;
    2972             :       }
    2973       70388 :       gel(D,i) = mulii(A,b); /* lcm(a,b) */
    2974       70386 :       gel(D,j) = d; /* gcd(a,b) */
    2975       70386 :       b = gel(D,j); if (equali1(b)) break;
    2976             :     }
    2977             :   }
    2978      303866 :   snf_pile(av, &D,&U,&V);
    2979      303874 :   if (U) *pU = shallowtrans(U);
    2980      303871 :   if (V) *pV = V;
    2981      303871 :   return D;
    2982             : }
    2983             : GEN
    2984      303855 : ZV_snf_group(GEN d, GEN *newU, GEN *newUi)
    2985             : {
    2986      303855 :   GEN D = ZV_snfall(d, newU, newUi);
    2987      303870 :   return snf_group(d, D, newU, newUi);
    2988             : }
    2989             : 
    2990             : /* D a vector of elementary divisors. Truncate (setlg) to leave out trivial
    2991             :  * entries (= 1) */
    2992             : void
    2993           0 : ZV_snf_trunc(GEN D)
    2994             : {
    2995           0 :   long i, l = lg(D);
    2996           0 :   for (i = 1; i < l; i++)
    2997           0 :     if (is_pm1(gel(D,i))) { setlg(D,i); break; }
    2998           0 : }
    2999             : 
    3000             : long
    3001           0 : zv_snf_rank(GEN D, ulong p)
    3002             : {
    3003           0 :   long i, l = lg(D);
    3004           0 :   if (!p) return l - 1;
    3005           0 :   for (i = 1; i < l; i++)
    3006           0 :     if (D[i] % p) break;
    3007           0 :   return i - 1;
    3008             : }
    3009             : long
    3010          49 : ZV_snf_rank_u(GEN D, ulong p)
    3011             : {
    3012          49 :   long i, l = lg(D);
    3013          49 :   while (l > 1 && D[l-1] == 1) l--;
    3014          49 :   if (!p) return l - 1;
    3015          49 :   if (p == 2)
    3016             :   {
    3017          49 :     for (i = 1; i < l; i++)
    3018          42 :       if (mpodd(gel(D,i))) break;
    3019             :   }
    3020          35 :   else if (!(p & (p-1)))
    3021             :   { /* power of 2 */
    3022          14 :     long n = vals(p);
    3023          28 :     for (i = 1; i < l; i++)
    3024          28 :       if (umodi2n(gel(D,i), n)) break;
    3025             :   }
    3026             :   else
    3027             :   {
    3028          49 :     for (i = 1; i < l; i++)
    3029          42 :       if (umodiu(gel(D,i), p)) break;
    3030             :   }
    3031          49 :   return i - 1;
    3032             : }
    3033             : long
    3034          91 : ZV_snf_rank(GEN D, GEN p)
    3035             : {
    3036          91 :   long i, l = lg(D);
    3037          91 :   if (lgefint(p) == 3) return ZV_snf_rank_u(D, p[2]);
    3038          77 :   while (l > 1 && equali1(gel(D, l-1))) l--;
    3039          42 :   if (!signe(p)) return l - 1;
    3040          77 :   for (i = 1; i < l; i++)
    3041          70 :     if (!dvdii(gel(D,i), p)) break;
    3042          21 :   return i - 1;
    3043             : }
    3044             : long
    3045         154 : snfrank(GEN D, GEN p)
    3046             : {
    3047             :   long i, l;
    3048         154 :   if (typ(D) != t_VEC) pari_err_TYPE("snfrank", D);
    3049         154 :   if (!p) p = gen_0;
    3050         154 :   l = lg(D);
    3051         154 :   if (l == 4 && typ(gel(D,3)) == t_MAT)
    3052             :   { /* from matsnf(,1) */
    3053          14 :     pari_sp av = avma;
    3054             :     long z;
    3055             :     GEN v;
    3056          14 :     D = gel(D,3); l = lg(D);
    3057          14 :     if (l == 1) return 0;
    3058          14 :     z = lgcols(D) - l; /* missing columns of 0s */
    3059          14 :     if (z < 0) pari_err_TYPE("snfrank", D);
    3060          14 :     v = cgetg(l, t_VEC);
    3061          35 :     for (i = 1; i < l; i++) gel(v, i) = gcoeff(D, i + z, i);
    3062          14 :     return gc_long(av, z + snfrank(v, p));
    3063             :   }
    3064         140 :   switch(typ(p))
    3065             :   {
    3066          98 :     case t_INT:
    3067          98 :       if (RgV_is_ZV(D)) return ZV_snf_rank(D, p);
    3068           7 :       if (!signe(p)) break; /* allow p = 0 */
    3069           0 :       pari_err_TYPE("snfrank", D);
    3070          42 :     case t_POL: break;
    3071           0 :     default: pari_err_TYPE("snfrank", p);
    3072             :   }
    3073         175 :   while (l > 1 && gequal1(gel(D, l-1))) l--;
    3074          49 :   if (gequal0(p)) return l - 1;
    3075         112 :   for (i = 1; i < l; i++)
    3076          91 :     if (!gdvd(gel(D,i), p)) break;
    3077          42 :   return i - 1;
    3078             : }

Generated by: LCOV version 1.16