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.16.2 lcov report (development 29115-f22e516b23) Lines: 1557 1728 90.1 %
Date: 2024-04-14 08:06:32 Functions: 90 100 90.0 %
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     7980714 : count(GEN mat, long row, long len, long *firstnonzero)
      72             : {
      73     7980714 :   long j, n = 0;
      74             : 
      75   714492812 :   for (j=1; j<=len; j++)
      76             :   {
      77   708227312 :     long p = mael(mat,j,row);
      78   708227312 :     if (p)
      79             :     {
      80    22673231 :       if (labs(p)!=1) return -1;
      81    20958017 :       n++; *firstnonzero=j;
      82             :     }
      83             :   }
      84     6265500 :   return n;
      85             : }
      86             : 
      87             : static int
      88      409981 : count2(GEN mat, long row, long len)
      89             : {
      90             :   long j;
      91     4202918 :   for (j=len; j; j--)
      92     4077961 :     if (labs(mael(mat,j,row)) == 1) return j;
      93      124957 :   return 0;
      94             : }
      95             : 
      96             : static GEN
      97      210684 : hnffinal(GEN matgen,GEN perm,GEN* ptdep,GEN* ptB,GEN* ptC)
      98             : {
      99             :   GEN p1,p2,U,H,Hnew,Bnew,Cnew,diagH1;
     100      210684 :   GEN B = *ptB, C = *ptC, dep = *ptdep, depnew;
     101             :   pari_sp av;
     102             :   long i,j,k,s,i1,j1,zc;
     103      210684 :   long co = lg(C);
     104      210684 :   long col = lg(matgen)-1;
     105             :   long lnz, nlze, lig;
     106             : 
     107      210684 :   if (col == 0) return matgen;
     108      210684 :   lnz = nbrows(matgen); /* was called lnz-1 - nr in hnfspec */
     109      210684 :   nlze = nbrows(dep);
     110      210684 :   lig = nlze + lnz;
     111             :   /* H: lnz x lnz [disregarding initial 0 cols], U: col x col */
     112      210684 :   H = ZM_hnflll(matgen, &U, 0);
     113      210684 :   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      210684 :   zc = col - lnz; /* # of 0 columns, correspond to units */
     117      210684 :   if (nlze) { dep = ZM_mul(dep,U); dep += zc; }
     118             : 
     119      210684 :   diagH1 = new_chunk(lnz+1); /* diagH1[i] = 0 iff H[i,i] != 1 (set later) */
     120             : 
     121      210684 :   av = avma;
     122      210684 :   Cnew = cgetg(co, typ(C));
     123      210684 :   setlg(C, col+1); p1 = gmul(C,U);
     124     1863878 :   for (j=1; j<=col; j++) gel(Cnew,j) = gel(p1,j);
     125     3075135 :   for (   ; j<co ; j++)  gel(Cnew,j) = gel(C,j);
     126             : 
     127             :   /* Clean up B using new H */
     128      848848 :   for (s=0,i=lnz; i; i--)
     129             :   {
     130      638181 :     GEN Di = gel(dep,i), Hi = gel(H,i);
     131      638181 :     GEN h = gel(Hi,i); /* H[i,i] */
     132      638181 :     if ( (diagH1[i] = is_pm1(h)) ) { h = NULL; s++; }
     133    15539473 :     for (j=col+1; j<co; j++)
     134             :     {
     135    14901638 :       GEN z = gel(B,j-col);
     136    14901638 :       p1 = gel(z,i+nlze);
     137    14901638 :       if (h) p1 = truedivii(p1,h);
     138    14902837 :       if (!signe(p1)) continue;
     139    26251158 :       for (k=1; k<=nlze; k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Di,k)));
     140   130170490 :       for (   ; k<=lig;  k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Hi,k-nlze)));
     141     8962717 :       gel(Cnew,j) = gsub(gel(Cnew,j), gmul(p1, gel(Cnew,i+zc)));
     142             :     }
     143      637835 :     if (gc_needed(av,2))
     144             :     {
     145         741 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnffinal, i = %ld",i);
     146         741 :       gerepileall(av, 2, &Cnew, &B);
     147             :     }
     148             :   }
     149      210667 :   p1 = cgetg(lnz+1,t_VEC); p2 = perm + nlze;
     150      848887 :   for (i1=0, j1=lnz-s, i=1; i<=lnz; i++) /* push the 1 rows down */
     151      638206 :     if (diagH1[i])
     152      302579 :       gel(p1,++j1) = gel(p2,i);
     153             :     else
     154      335627 :       gel(p2,++i1) = gel(p2,i);
     155      513260 :   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      210681 :   lig -= s; col -= s; lnz -= s;
     166      210681 :   Hnew = cgetg(lnz+1,t_MAT);
     167      210683 :   depnew = cgetg(lnz+1,t_MAT); /* only used if nlze > 0 */
     168      210682 :   Bnew = cgetg(co-col,t_MAT);
     169      210682 :   C = shallowcopy(Cnew);
     170      848893 :   for (j=1,i1=j1=0; j<=lnz+s; j++)
     171             :   {
     172      638210 :     GEN z = gel(H,j);
     173      638210 :     if (diagH1[j])
     174             :     { /* hit exactly s times */
     175      302580 :       i1++; C[i1+col] = Cnew[j+zc];
     176      302580 :       p1 = cgetg(lig+1,t_COL); gel(Bnew,i1) = p1;
     177      529978 :       for (i=1; i<=nlze; i++) gel(p1,i) = gcoeff(dep,i,j);
     178      302580 :       p1 += nlze;
     179             :     }
     180             :     else
     181             :     {
     182      335630 :       j1++; C[j1+zc] = Cnew[j+zc];
     183      335630 :       p1 = cgetg(lnz+1,t_COL); gel(Hnew,j1) = p1;
     184      335629 :       depnew[j1] = dep[j];
     185             :     }
     186     2480989 :     for (i=k=1; k<=lnz; i++)
     187     1842780 :       if (!diagH1[i]) p1[k++] = z[i];
     188             :   }
     189     3075111 :   for (j=s+1; j<co-col; j++)
     190             :   {
     191     2864430 :     GEN z = gel(B,j-s);
     192     2864430 :     p1 = cgetg(lig+1,t_COL); gel(Bnew,j) = p1;
     193     4956532 :     for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
     194     2864428 :     z += nlze; p1 += nlze;
     195    12397165 :     for (i=k=1; k<=lnz; i++)
     196     9532737 :       if (!diagH1[i]) gel(p1,k++) = gel(z,i);
     197             :   }
     198      210681 :   *ptdep = depnew;
     199      210681 :   *ptC = C;
     200      210681 :   *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     2652781 : col_dup(long l, GEN col)
     217             : {
     218     2652781 :   GEN c = new_chunk(l);
     219     2652780 :   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      210682 : ZM_rowrankprofile(GEN x, long *nlze)
     225             : {
     226      210682 :   pari_sp av = avma;
     227             :   GEN d, y;
     228             :   long i, j, k, l, r;
     229             : 
     230      210682 :   x = shallowtrans(x); l = lg(x);
     231      210684 :   (void)new_chunk(l); /* HACK */
     232      210684 :   d = ZM_pivots(x,&r); set_avma(av);
     233      210683 :   *nlze = r;
     234      210683 :   if (!d) return identity_perm(l-1);
     235      194712 :   y = cgetg(l,t_VECSMALL);
     236      839942 :   for (i = j = 1, k = r+1; i<l; i++)
     237      645230 :     if (d[i]) y[k++] = i; else y[j++] = i;
     238      194712 :   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      133962 : 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      133962 :   const long li = lg(perm); /* = lgcols(mat0) */
     262      133962 :   const long CO = lg(mat0);
     263             : 
     264      133962 :   n = 0; /* -Wall */
     265             : 
     266      133962 :   C = *ptC; co = CO;
     267      133962 :   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      133962 :   if (DEBUGLEVEL>5)
     274             :   {
     275           0 :     err_printf("Entering hnfspec\n");
     276           0 :     p_mat(mat0,perm,0);
     277             :   }
     278      133962 :   matt = cgetg(co, t_MAT); /* dense part of mat (top) */
     279      133964 :   mat = cgetg(co, t_MAT);
     280     2786738 :   for (j = 1; j < co; j++)
     281             :   {
     282     2652773 :     GEN matj = col_dup(li, gel(mat0,j));
     283     2652780 :     p1 = cgetg(k0+1,t_COL); gel(matt,j) = p1; gel(mat,j) = matj;
     284    12338665 :     for (i=1; i<=k0; i++) gel(p1,i) = stoi(matj[perm[i]]);
     285             :   }
     286      133965 :   av = avma;
     287             : 
     288      133965 :   i = lig = li-1; col = co-1; lk0 = k0;
     289      133965 :   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     6316568 :   while (i > lk0 && col)
     292     6182592 :     switch( count(mat,perm[i],col,&n) )
     293             :     {
     294       26401 :       case 0: /* move zero lines between k0+1 and lk0 */
     295       26401 :         lk0++; lswap(perm[i], perm[lk0]);
     296       26401 :         i = lig; continue;
     297             : 
     298      480809 :       case 1: /* move trivial generator between lig+1 and li */
     299      480809 :         lswap(perm[i], perm[lig]);
     300      480809 :         if (T) swap(gel(T,n), gel(T,col));
     301      480809 :         swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     302      480809 :         if (p[perm[lig]] < 0) /* = -1 */
     303             :         { /* convert relation -g = 0 to g = 0 */
     304    10363672 :           for (i=lk0+1; i<lig; i++) p[perm[i]] = -p[perm[i]];
     305      434572 :           if (T)
     306             :           {
     307      434571 :             p1 = gel(T,col);
     308    11116801 :             for (i=1; ; i++) /* T = permuted identity: single nonzero entry */
     309    11116801 :               if (signe(gel(p1,i))) { togglesign_safe(&gel(p1,i)); break; }
     310             :           }
     311             :         }
     312      480813 :         lig--; col--; i = lig; continue;
     313             : 
     314     5675389 :       default: i--;
     315             :     }
     316      133976 :   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      133965 :   s = 0;
     323      778849 :   while (lig > lk0 && col && s < (long)(HIGHBIT>>1))
     324             :   {
     325     1922760 :     for (i=lig; i>lk0; i--)
     326     1798202 :       if (count(mat,perm[i],col,&n) > 0) break;
     327      769493 :     if (i == lk0) break;
     328             : 
     329             :     /* only 0, +/- 1 entries, at least 2 of them nonzero */
     330      644933 :     lswap(perm[i], perm[lig]);
     331      644933 :     swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     332      644933 :     if (T) swap(gel(T,n), gel(T,col));
     333      644933 :     if (p[perm[lig]] < 0)
     334             :     {
     335     7621928 :       for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
     336      416157 :       if (T) ZV_togglesign(gel(T,col));
     337             :     }
     338    20924012 :     for (j=1; j<col; j++)
     339             :     {
     340    20279128 :       GEN matj = gel(mat,j);
     341             :       long t;
     342    20279128 :       if (! (t = matj[perm[lig]]) ) continue;
     343     1520315 :       if (t == 1) {
     344    25374617 :         for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= p[perm[i]]);
     345             :       }
     346             :       else { /* t = -1 */
     347    18377872 :         for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] += p[perm[i]]);
     348             :       }
     349     1520315 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
     350             :     }
     351      644884 :     lig--; col--;
     352      644884 :     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      133920 :   vmax = cgetg(co,t_VECSMALL);
     361     1661026 :   for (j=1; j<=col; j++)
     362             :   {
     363     1527064 :     GEN matj = gel(mat,j);
     364     6990957 :     for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[i]);
     365     1527064 :     vmax[j] = s;
     366             :   }
     367      418987 :   while (lig > lk0 && col)
     368             :   {
     369      436191 :     for (i=lig; i>lk0; i--)
     370      409981 :       if ( (n = count2(mat,perm[i],col)) ) break;
     371      311234 :     if (i == lk0) break;
     372             : 
     373      285024 :     lswap(vmax[n], vmax[col]);
     374      285024 :     lswap(perm[i], perm[lig]);
     375      285024 :     swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     376      285024 :     if (T) swap(gel(T,n), gel(T,col));
     377      285024 :     if (p[perm[lig]] < 0)
     378             :     {
     379      568524 :       for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
     380       99031 :       if (T) ZV_togglesign(gel(T,col));
     381             :     }
     382     3869170 :     for (j=1; j<col; j++)
     383             :     {
     384     3584232 :       GEN matj = gel(mat,j);
     385             :       long t;
     386     3584232 :       if (! (t = matj[perm[lig]]) ) continue;
     387     1802099 :       if (vmax[col] && (ulong)labs(t) >= (HIGHBIT-vmax[j]) / vmax[col])
     388           0 :         goto END2;
     389             : 
     390    18285797 :       for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= t*p[perm[i]]);
     391     1802099 :       vmax[j] = s;
     392     1802099 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
     393             :     }
     394      284938 :     lig--; col--;
     395      284938 :     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      107753 : END2: /* clean up mat: remove everything to the right of the 1s on diagonal */
     403             :   /* go multiprecision first */
     404      133963 :   matb = cgetg(co,t_MAT); /* bottom part (complement of matt) */
     405     2786665 :   for (j=1; j<co; j++)
     406             :   {
     407     2652807 :     GEN matj = gel(mat,j);
     408     2652807 :     p1 = cgetg(li-k0,t_COL); gel(matb,j) = p1;
     409     2652806 :     p1 -= k0;
     410    80604813 :     for (i=k0+1; i<li; i++) gel(p1,i) = stoi(matj[perm[i]]);
     411             :   }
     412      133858 :   if (DEBUGLEVEL>5)
     413             :   {
     414           0 :     err_printf("    after phase2:\n");
     415           0 :     p_mat(mat,perm,lk0);
     416             :   }
     417     1411150 :   for (i=li-2; i>lig; i--)
     418             :   {
     419     1277189 :     long h, i0 = i - k0, k = i + co-li;
     420     1277189 :     GEN Bk = gel(matb,k);
     421    28290622 :     for (j=k+1; j<co; j++)
     422             :     {
     423    27013714 :       GEN Bj = gel(matb,j), v = gel(Bj,i0);
     424    27013714 :       s = signe(v); if (!s) continue;
     425             : 
     426     5226026 :       gel(Bj,i0) = gen_0;
     427     5226026 :       if (is_pm1(v))
     428             :       {
     429     3029859 :         if (s > 0) /* v = 1 */
     430    44473679 :         { for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), gel(Bk,h)); }
     431             :         else /* v = -1 */
     432    37900765 :         { for (h=1; h<i0; h++) gel(Bj,h) = addii(gel(Bj,h), gel(Bk,h)); }
     433             :       }
     434             :       else {
     435    49643760 :         for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), mulii(v,gel(Bk,h)));
     436             :       }
     437     5225802 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,k), negi(v));
     438             :     }
     439     1276908 :     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     2786798 :   for (j=1; j<co; j++) setlg(matb[j], lig-k0+1); /* bottom can be forgotten */
     447      133981 :   gerepileall(av, T? 2: 1, &matb, &T);
     448      133965 :   if (DEBUGLEVEL>5) err_printf("    matb cleaned up (using Id block)\n");
     449             : 
     450      133965 :   nlze = lk0 - k0;  /* # of 0 rows */
     451      133965 :   lnz = lig-nlze+1; /* 1 + # of nonzero rows (!= 0...0 1 0 ... 0) */
     452      133965 :   if (T) matt = ZM_mul(matt,T); /* update top rows */
     453      133963 :   extramat = cgetg(col+1,t_MAT); /* = new C minus the 0 rows */
     454     1376010 :   for (j=1; j<=col; j++)
     455             :   {
     456     1242048 :     GEN z = gel(matt,j);
     457     1242048 :     GEN t = (gel(matb,j)) + nlze - k0;
     458     1242048 :     p2=cgetg(lnz,t_COL); gel(extramat,j) = p2;
     459     5211181 :     for (i=1; i<=k0; i++) gel(p2,i) = gel(z,i); /* top k0 rows */
     460     2001533 :     for (   ; i<lnz; i++) gel(p2,i) = gel(t,i); /* other nonzero rows */
     461             :   }
     462      133962 :   if (!col) {
     463           0 :     permpro = identity_perm(lnz);
     464           0 :     nr = lnz;
     465             :   }
     466             :   else
     467      133962 :     permpro = ZM_rowrankprofile(extramat, &nr);
     468             :   /* lnz = lg(permpro) */
     469      133965 :   if (nlze)
     470             :   { /* put the nlze 0 rows (trivial generators) at the top */
     471       11505 :     p1 = new_chunk(lk0+1);
     472       37906 :     for (i=1; i<=nlze; i++) p1[i] = perm[i + k0];
     473       59711 :     for (   ; i<=lk0; i++)  p1[i] = perm[i - nlze];
     474       86112 :     for (i=1; i<=lk0; i++)  perm[i] = p1[i];
     475             :   }
     476             :   /* sort other rows according to permpro (nr redundant generators first) */
     477      133965 :   p1 = new_chunk(lnz); p2 = perm + nlze;
     478      586381 :   for (i=1; i<lnz; i++) p1[i] = p2[permpro[i]];
     479      586381 :   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      133965 :   matbnew = cgetg(col+1,t_MAT); /* dense+toobig, maximal rank. For hnffinal */
     495      133965 :   dep    = cgetg(col+1,t_MAT); /* rows dependent from the ones in matbnew */
     496     1376052 :   for (j=1; j<=col; j++)
     497             :   {
     498     1242087 :     GEN z = gel(extramat,j);
     499     1242087 :     p1 = cgetg(nlze+nr+1,t_COL); gel(dep,j) = p1;
     500     1242087 :     p2 = cgetg(lnz-nr,t_COL); gel(matbnew,j) = p2;
     501     1589347 :     for (i=1; i<=nlze; i++) gel(p1,i) = gen_0;
     502     1315824 :     p1 += nlze; for (i=1; i<=nr; i++) p1[i] = z[permpro[i]];
     503     5897142 :     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      133965 :   B = cgetg(co-col,t_MAT);
     509     1544741 :   for (j=col+1; j<co; j++)
     510             :   {
     511     1410777 :     GEN y = gel(matt,j);
     512     1410777 :     GEN z = gel(matb,j);
     513     1410777 :     p1=cgetg(lig+1,t_COL); gel(B,j-col) = p1;
     514     2967329 :     for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
     515     1410776 :     p1 += nlze; z += nlze-k0;
     516     9949641 :     for (k=1; k<lnz; k++)
     517             :     {
     518     8538865 :       i = permpro[k];
     519     8538865 :       gel(p1,k) = (i <= k0)? gel(y,i): gel(z,i);
     520             :     }
     521             :   }
     522      133964 :   if (T) C = typ(C)==t_MAT? RgM_ZM_mul(C,T): RgV_RgM_mul(C,T);
     523      133963 :   gerepileall(av, 4, &matbnew, &B, &dep, &C);
     524      133965 :   *ptdep = dep;
     525      133965 :   *ptB = B;
     526      133965 :   H = hnffinal(matbnew, perm, ptdep, ptB, &C);
     527      133964 :   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      133964 :   *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       76718 : hnfadd_i(GEN H, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, /* cf hnfspec */
     613             :        GEN extramat,GEN extraC)
     614             : {
     615       76718 :   GEN matb, extratop, Cnew, permpro, B = *ptB, C = *ptC, dep = *ptdep;
     616             :   long i, lH, lB, li, lig, co, col, nlze;
     617             : 
     618       76718 :   if (lg(extramat) == 1) return H;
     619       76718 :   co = lg(C)-1;
     620       76718 :   lH = lg(H)-1;
     621       76718 :   lB = lg(B)-1;
     622       76718 :   li = lg(perm)-1;
     623       76718 :   lig = li - lB;
     624       76718 :   col = co - lB;
     625       76718 :   nlze = lig - lH;
     626             : 
     627             :  /*               col    co
     628             :   *       [ 0 |dep |     ]
     629             :   *  nlze [--------|  B  ]
     630             :   *       [ 0 | H  |     ]
     631             :   *       [--------|-----] lig
     632             :   *       [   0    | Id  ]
     633             :   *       [        |     ] li */
     634       76718 :   extratop = zm_to_ZM( rowslicepermute(extramat, perm, 1, lig) );
     635       76719 :   if (li != lig)
     636             :   { /* zero out bottom part, using the Id block */
     637       76705 :     GEN A = vecslice(C, col+1, co);
     638       76705 :     GEN c = rowslicepermute(extramat, perm, lig+1, li);
     639       76705 :     extraC   = gsub(extraC, typ(A)==t_MAT? RgM_zm_mul(A, c): RgV_zm_mul(A,c));
     640       76704 :     extratop = ZM_sub(extratop, ZM_zm_mul(B, c));
     641             :   }
     642             : 
     643       76718 :   extramat = shallowconcat(extratop, vconcat(dep, H));
     644       76719 :   Cnew     = shallowconcat(extraC, vecslice(C, col-lH+1, co));
     645       76719 :   if (DEBUGLEVEL>5) err_printf("    1st phase done\n");
     646       76719 :   permpro = ZM_rowrankprofile(extramat, &nlze);
     647       76719 :   extramat = rowpermute(extramat, permpro);
     648       76719 :   *ptB     = rowpermute(B,        permpro);
     649       76719 :   permpro = vecsmallpermute(perm, permpro);
     650      269536 :   for (i=1; i<=lig; i++) perm[i] = permpro[i]; /* perm o= permpro */
     651             : 
     652       76719 :   *ptdep  = rowslice(extramat, 1, nlze);
     653       76719 :   matb    = rowslice(extramat, nlze+1, lig);
     654       76719 :   if (DEBUGLEVEL>5) err_printf("    2nd phase done\n");
     655       76719 :   H = hnffinal(matb,perm,ptdep,ptB,&Cnew);
     656       76719 :   *ptC = shallowconcat(vecslice(C, 1, col-lH), Cnew);
     657       76719 :   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    39097005 : ZC_elem(GEN aj, GEN ak, GEN A, GEN U, long j, long k)
     673             : {
     674             :   GEN p1,u,v,d;
     675             : 
     676    39097005 :   if (!signe(ak)) {
     677       99253 :     swap(gel(A,j), gel(A,k));
     678       99253 :     if (U) swap(gel(U,j), gel(U,k));
     679    35506185 :     return;
     680             :   }
     681    38997752 :   d = bezout(aj,ak,&u,&v);
     682             :   /* frequent special case (u,v) = (1,0) or (0,1) */
     683    39001247 :   if (!signe(u))
     684             :   { /* ak | aj */
     685    19158773 :     p1 = diviiexact(aj,ak); togglesign(p1);
     686    19159105 :     ZC_lincomb1_inplace(gel(A,j), gel(A,k), p1);
     687    19167084 :     if (U)
     688     2262094 :       ZC_lincomb1_inplace(gel(U,j), gel(U,k), p1);
     689    19167119 :     return;
     690             :   }
     691    19842474 :   if (!signe(v))
     692             :   { /* aj | ak */
     693    16241619 :     p1 = diviiexact(ak,aj); togglesign(p1);
     694    16240145 :     ZC_lincomb1_inplace(gel(A,k), gel(A,j), p1);
     695    16239794 :     swap(gel(A,j), gel(A,k));
     696    16239794 :     if (U) {
     697      368642 :       ZC_lincomb1_inplace(gel(U,k), gel(U,j), p1);
     698      368661 :       swap(gel(U,j), gel(U,k));
     699             :     }
     700    16239813 :     return;
     701             :   }
     702             : 
     703     3600855 :   if (!is_pm1(d)) { aj = diviiexact(aj, d); ak = diviiexact(ak, d); }
     704     3600828 :   p1 = gel(A,k); aj = negi(aj); /* NOT togglesign */
     705     3601123 :   gel(A,k) = ZC_lincomb(u,v, gel(A,j),p1);
     706     3600638 :   gel(A,j) = ZC_lincomb(aj,ak, p1,gel(A,j));
     707     3600484 :   if (U)
     708             :   {
     709      747107 :     p1 = gel(U,k);
     710      747107 :     gel(U,k) = ZC_lincomb(u,v, gel(U,j),p1);
     711      747169 :     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;
     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 :   if (degpol(d)) { a = RgX_div(a, d); b = RgX_div(b, d); }
     731         238 :   else if (typ(gel(d,2)) == t_REAL && lg(gel(d,2)) <= 3)
     732             : #if 1
     733             :   { /* possible accuracy problem */
     734           0 :     GEN D = RgX_gcd_simple(a,b);
     735           0 :     if (degpol(D)) {
     736           0 :       D = RgX_normalize(D);
     737           0 :       a = RgX_div(a, D);
     738           0 :       b = RgX_div(b, D);
     739           0 :       d = RgX_extgcd(a,b, pu,pv); /* retry now */
     740           0 :       d = RgX_mul(d, D);
     741             :     }
     742             :   }
     743             : #else
     744             :   { /* less stable */
     745             :     d = RgX_extgcd_simple(a,b, pu,pv);
     746             :     if (degpol(d)) { a = RgX_div(a, d); b = RgX_div(b, d); }
     747             :   }
     748             : #endif
     749         882 :   *pa = a;
     750         882 :   *pb = b; return d;
     751             : }
     752             : static GEN
     753     2693481 : col_mul(GEN x, GEN c)
     754             : {
     755     2693481 :   if (typ(x) == t_INT)
     756             :   {
     757     2691465 :     long s = signe(x);
     758     2691465 :     if (!s) return NULL;
     759     2040524 :     if (is_pm1(x)) return (s > 0)? c: RgC_neg(c);
     760             :   }
     761      307464 :   return RgC_Rg_mul(c, x);
     762             : }
     763             : static void
     764           0 : do_zero(GEN x)
     765             : {
     766           0 :   long i, lx = lg(x);
     767           0 :   for (i=1; i<lx; i++) gel(x,i) = gen_0;
     768           0 : }
     769             : 
     770             : /* (c1, c2) *= [u,-b; v,a] */
     771             : static void
     772      673387 : update(GEN u, GEN v, GEN a, GEN b, GEN *c1, GEN *c2)
     773             : {
     774             :   GEN p1,p2;
     775             : 
     776      673387 :   u = col_mul(u,*c1);
     777      673423 :   v = col_mul(v,*c2);
     778      673447 :   if (u) p1 = v? gadd(u,v): u;
     779        6518 :   else   p1 = v? v: NULL;
     780             : 
     781      673445 :   a = col_mul(a,*c2);
     782      673487 :   b = col_mul(gneg_i(b),*c1);
     783      673490 :   if (a) p2 = b? RgC_add(a,b): a;
     784           0 :   else   p2 = b? b: NULL;
     785             : 
     786      673454 :   if (!p1) do_zero(*c1); else *c1 = p1;
     787      673455 :   if (!p2) do_zero(*c2); else *c2 = p2;
     788      673455 : }
     789             : 
     790             : /* zero aj = Aij (!= 0)  using  ak = Aik (maybe 0), via linear combination of
     791             :  * A[j] and A[k] of determinant 1. If U != NULL, likewise update its columns */
     792             : static void
     793         511 : RgC_elem(GEN aj, GEN ak, GEN A, GEN V, long j, long k, long li, long vx)
     794             : {
     795         511 :   GEN u,v, d = gbezout_step(&aj, &ak, &u, &v, vx);
     796             :   long l;
     797             :   /* (A[,k], A[,j]) *= [v, -aj; u, ak ] */
     798        1778 :   for (l = 1; l < li; l++)
     799             :   {
     800        1267 :     GEN t = gadd(gmul(u,gcoeff(A,l,j)), gmul(v,gcoeff(A,l,k)));
     801        1267 :     gcoeff(A,l,j) = gsub(gmul(ak,gcoeff(A,l,j)), gmul(aj,gcoeff(A,l,k)));
     802        1267 :     gcoeff(A,l,k) = t;
     803             :   }
     804         511 :   gcoeff(A,li,j) = gen_0;
     805         511 :   gcoeff(A,li,k) = d;
     806         511 :   if (V) update(v,u,ak,aj,(GEN*)(V+k),(GEN*)(V+j));
     807         511 : }
     808             : 
     809             : /* reduce A[i,j] mod A[i,j0] for j=j0+1... via column operations */
     810             : static void
     811     4143047 : ZM_reduce(GEN A, GEN U, long i, long j0)
     812             : {
     813     4143047 :   long j, lA = lg(A);
     814     4143047 :   GEN d = gcoeff(A,i,j0);
     815     4143047 :   if (signe(d) < 0)
     816             :   {
     817       33795 :     ZV_neg_inplace(gel(A,j0));
     818       33795 :     if (U) ZV_togglesign(gel(U,j0));
     819       33795 :     d = gcoeff(A,i,j0);
     820             :   }
     821     8320177 :   for (j=j0+1; j<lA; j++)
     822             :   {
     823     4177084 :     GEN q = truedivii(gcoeff(A,i,j), d);
     824     4177104 :     if (!signe(q)) continue;
     825             : 
     826      256417 :     togglesign(q);
     827      256521 :     ZC_lincomb1_inplace(gel(A,j), gel(A,j0), q);
     828      256516 :     if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,j0), q);
     829             :   }
     830     4143093 : }
     831             : 
     832             : /* normalize T as if it were a t_POL in variable v */
     833             : static GEN
     834         364 : normalize_as_RgX(GEN T, long v, GEN *pd)
     835             : {
     836             :   GEN d;
     837         364 :   if (!is_RgX(T,v)) { *pd = T; return gen_1; }
     838         336 :   d = leading_coeff(T);
     839         336 :   while (gequal0(d) || (typ(d) == t_REAL && lg(d) == 3
     840           0 :                         && gexpo(T) - expo(d) > (long)BITS_IN_LONG)) {
     841          14 :     T = normalizepol_lg(T, lg(T)-1);
     842          14 :     if (!signe(T)) { *pd = gen_1; return T; }
     843           0 :     d = leading_coeff(T);
     844             :   }
     845         322 :   if (degpol(T)) T = RgX_Rg_div(T,d); else { d = gel(T,2); T = gen_1; }
     846         322 :   *pd = d; return T;
     847             : }
     848             : /* reduce A[i,j] mod A[i,j0] for j=j0+1... via column operations */
     849             : static void
     850          84 : RgM_reduce(GEN A, GEN U, long i, long j0, long vx)
     851             : {
     852          84 :   long j, lA = lg(A);
     853          84 :   GEN d, T = normalize_as_RgX(gcoeff(A,i,j0), vx, &d);
     854          84 :   if (U && !gequal1(d)) gel(U,j0) = RgC_Rg_div(gel(U,j0), d);
     855          84 :   gcoeff(A,i,j0) = T;
     856             : 
     857         196 :   for (j=j0+1; j<lA; j++)
     858             :   {
     859         112 :     GEN t = gcoeff(A,i,j), q;
     860         112 :     if (gequal0(t)) continue;
     861          14 :     if (T == gen_1)
     862           0 :       q = t;
     863          14 :     else if (is_RgX(t,vx))
     864          14 :       q = RgX_div(t, T);
     865           0 :     else continue;
     866             : 
     867          14 :     if (gequal0(q)) continue;
     868           7 :     gel(A,j) = RgC_sub(gel(A,j), RgC_Rg_mul(gel(A,j0), q));
     869           7 :     if (U) gel(U,j) = RgC_sub(gel(U,j), RgC_Rg_mul(gel(U,j0), q));
     870             :   }
     871          84 : }
     872             : 
     873             : /* A,B square integral in upper HNF, of the same dimension > 0. Return Au
     874             :  * in Z^n (v in Z^n not computed), such that Au + Bv = [1, 0, ..., 0] */
     875             : GEN
     876      741094 : hnfmerge_get_1(GEN A, GEN B)
     877             : {
     878      741094 :   pari_sp av = avma;
     879      741094 :   long j, k, l = lg(A), lb;
     880      741094 :   GEN b, U = cgetg(l + 1, t_MAT), C = cgetg(l + 1, t_VEC);
     881             : 
     882      741121 :   b = gcoeff(B,1,1); lb = lgefint(b);
     883     1504993 :   for (j = 1; j < l; j++)
     884             :   {
     885             :     GEN t;
     886     1504991 :     long c = j+1;
     887     1504991 :     gel(U,j) = col_ei(l-1, j);
     888     1505094 :     gel(U,c) = zerocol(l-1); /* dummy */
     889     1505095 :     gel(C,j) = vecslice(gel(A,j), 1,j);
     890     1505132 :     gel(C,c) = vecslice(gel(B,j), 1,j);
     891     4178142 :     for (k = j; k > 0; k--)
     892             :     {
     893     2673528 :       t = gcoeff(C,k,c);
     894     2673528 :       if (gequal0(t)) continue;
     895     2469423 :       setlg(C[c], k+1);
     896     2469371 :       ZC_elem(t, gcoeff(C,k,k), C, U, c, k);
     897     2467907 :       if (lgefint(gcoeff(C,k,k)) > lb) gel(C,k) = FpC_red(gel(C,k), b);
     898     2467907 :       if (j > 4)
     899             :       {
     900       89296 :         GEN u = gel(U,k);
     901             :         long h;
     902     1337037 :         for (h=1; h<l; h++)
     903     1247741 :           if (lgefint(gel(u,h)) > lb) gel(u,h) = remii(gel(u,h), b);
     904             :       }
     905             :     }
     906     1504614 :     if (j == 1)
     907      740906 :       t = gcoeff(C,1,1);
     908             :     else
     909             :     {
     910             :       GEN u;
     911      763708 :       t = bezout(gcoeff(C,1,1), b, &u, NULL); /* >= 0 */
     912      764104 :       if (signe(u) && !equali1(u)) gel(U,1) = ZC_Z_mul(gel(U,1), u);
     913      764109 :       gcoeff(C,1,1) = t;
     914             :     }
     915     1505015 :     if (equali1(t)) break;
     916             :   }
     917      740958 :   if (j >= l) return NULL;
     918      740958 :   b = lcmii(gcoeff(A,1,1),b);
     919      741011 :   A = FpC_red(ZM_ZC_mul(A,gel(U,1)), b);
     920      740865 :   return gerepileupto(av, FpC_center(A, b, shifti(b,-1)));
     921             : }
     922             : 
     923             : /* remove the first r columns */
     924             : static void
     925      309844 : remove_0cols(long r, GEN *pA, GEN *pB, long remove)
     926             : {
     927      309844 :   GEN A = *pA, B = *pB;
     928      309844 :   long l = lg(A);
     929      309844 :   A += r; A[0] = evaltyp(t_MAT) | _evallg(l-r);
     930      309844 :   if (B && remove == 2) { B += r; B[0] = A[0]; }
     931      309844 :   *pA = A; *pB = B;
     932      309844 : }
     933             : 
     934             : /* Inefficient compared to hnfall. 'remove' = throw away lin.dep columns */
     935             : static GEN
     936       54447 : hnf_i(GEN A, int remove)
     937             : {
     938       54447 :   pari_sp av0 = avma, av;
     939             :   long s, n, m, j, k, li, def, ldef;
     940             : 
     941       54447 :   RgM_dimensions(A, &m, &n);
     942       54447 :   if (!n) return cgetg(1,t_MAT);
     943       54195 :   av = avma;
     944       54195 :   A = RgM_shallowcopy(A);
     945       54195 :   def = n; ldef = (m>n)? m-n: 0;
     946      146712 :   for (li=m; li>ldef; li--)
     947             :   {
     948      381674 :     for (j=def-1; j; j--)
     949             :     {
     950      289157 :       GEN a = gcoeff(A,li,j);
     951      289157 :       if (!signe(a)) continue;
     952             : 
     953             :       /* zero a = Aij  using  b = Aik */
     954      147182 :       k = (j==1)? def: j-1;
     955      147182 :       ZC_elem(a,gcoeff(A,li,k), A,NULL, j,k);
     956      147182 :       if (gc_needed(av,1))
     957             :       {
     958           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnf[1]. li=%ld",li);
     959           0 :         A = gerepilecopy(av, A);
     960             :       }
     961             :     }
     962       92517 :     s = signe(gcoeff(A,li,def));
     963       92517 :     if (s)
     964             :     {
     965       91719 :       if (s < 0) ZV_neg_inplace(gel(A,def));
     966       91719 :       ZM_reduce(A, NULL, li,def);
     967       91719 :       def--;
     968             :     }
     969             :     else
     970         798 :       if (ldef) ldef--;
     971       92517 :     if (gc_needed(av,1))
     972             :     {
     973           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnf[2]. li=%ld",li);
     974           0 :       A = gerepilecopy(av, A);
     975             :     }
     976             :   }
     977             :   /* rank A = n - def */
     978       54195 :   if (remove) { GEN B = NULL; remove_0cols(def, &A, &B, remove); }
     979       54195 :   return gerepileupto(av0, ZM_copy(A));
     980             : }
     981             : 
     982             : GEN
     983       77385 : ZM_hnf(GEN x) { return lg(x) > 8? ZM_hnfall(x, NULL, 1): hnf_i(x, 1); }
     984             : 
     985             : /* u*z[1..k] mod p, in place */
     986             : static void
     987     5297555 : FpV_Fp_mul_part_ip(GEN z, GEN u, GEN p, long k)
     988             : {
     989             :   long i;
     990     5297555 :   if (is_pm1(u)) {
     991      192631 :     if (signe(u) > 0) {
     992      579122 :       for (i = 1; i <= k; i++)
     993      438495 :         if (signe(gel(z,i))) gel(z,i) = modii(gel(z,i), p);
     994             :     } else {
     995      145823 :       for (i = 1; i <= k; i++)
     996       93822 :         if (signe(gel(z,i))) gel(z,i) = modii(negi(gel(z,i)), p);
     997             :     }
     998             :   }
     999             :   else {
    1000    19277630 :     for (i = 1; i <= k; i++)
    1001    14173364 :       if (signe(gel(z,i))) gel(z,i) = Fp_mul(u,gel(z,i), p);
    1002             :   }
    1003     5296894 : }
    1004             : static void
    1005    35115415 : FpV_red_part_ipvec(GEN z, GEN p, long k)
    1006             : {
    1007             :   long i;
    1008   105484534 :   for (i = 1; i <= k; i++) gel(z,i) = modii(gel(z,i), gel(p,i));
    1009    35111614 : }
    1010             : 
    1011             : /* return x * U, in echelon form (mod p^m), where (det(U),p) = 1.
    1012             :  * If early_abort is set, return NULL as soon as one pivot is 0 (mod p^m) */
    1013             : GEN
    1014      524243 : ZpM_echelon(GEN x, long early_abort, GEN p, GEN pm)
    1015             : {
    1016      524243 :   pari_sp av0 = avma, av;
    1017             :   long m, li, co, i, j, k, def, ldef;
    1018             : 
    1019      524243 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1020      524243 :   li = lgcols(x);
    1021      524243 :   av = avma;
    1022      524243 :   x = RgM_shallowcopy(x);
    1023      524245 :   m = Z_pval(pm, p);
    1024             : 
    1025      524246 :   ldef = (li > co)? li - co: 0;
    1026     2499390 :   for (def = co-1,i = li-1; i > ldef; i--)
    1027             :   {
    1028     1975799 :     long vmin = LONG_MAX, kmin = 0;
    1029     1975799 :     GEN umin = gen_0, pvmin, q;
    1030     9639686 :     for (k = 1; k <= def; k++)
    1031             :     {
    1032     8127278 :       GEN u = gcoeff(x,i,k);
    1033             :       long v;
    1034     8127278 :       if (!signe(u)) continue;
    1035     3844736 :       v = Z_pvalrem(u, p, &u);
    1036     3844701 :       if (v >= m) gcoeff(x,i,k) = gen_0;
    1037     2713741 :       else if (v < vmin) {
    1038     1395914 :         vmin = v; kmin = k; umin = u;
    1039     1395914 :         if (!vmin) break;
    1040             :       }
    1041             :     }
    1042     1975764 :     if (!kmin)
    1043             :     {
    1044      922210 :       if (early_abort) return NULL;
    1045      921522 :       gcoeff(x,i,def) = gen_0;
    1046      921522 :       ldef--;
    1047      921522 :       if (ldef < 0) ldef = 0;
    1048      921522 :       continue;
    1049             :     }
    1050     1053554 :     if (kmin != def) swap(gel(x,def), gel(x,kmin));
    1051     1053554 :     q = vmin? powiu(p, m-vmin): pm;
    1052             :     /* pivot has valuation vmin */
    1053     1053559 :     umin = modii(umin, q);
    1054     1053596 :     if (!equali1(umin))
    1055      529670 :       FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(umin,q), pm, i-1);
    1056     1053589 :     gcoeff(x, i, def) = pvmin = powiu(p, vmin);
    1057     7102198 :     for (j = def-1; j; j--)
    1058             :     { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
    1059     6048576 :       GEN t, a = gcoeff(x,i,j) = modii(gcoeff(x,i,j), pm);
    1060     6048128 :       if (!signe(a)) continue;
    1061             : 
    1062     2834190 :       t = diviiexact(a, pvmin); togglesign(t);
    1063     2834738 :       ZC_lincomb1_inplace(gel(x,j), gel(x,def), t);
    1064     2834661 :       if (gc_needed(av,1))
    1065             :       {
    1066          14 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZpM_echelon. i=%ld",i);
    1067          14 :         x = gerepilecopy(av, x); pvmin = gcoeff(x,i,def);
    1068             :       }
    1069             :     }
    1070     1053622 :     def--;
    1071             :   }
    1072      523591 :   if (co > li)
    1073             :   {
    1074           0 :     x += co - li;
    1075           0 :     x[0] = evaltyp(t_MAT) | _evallg(li);
    1076             :   }
    1077      523591 :   return gerepilecopy(av0, x);
    1078             : }
    1079             : GEN
    1080     4650276 : zlm_echelon(GEN x, long early_abort, ulong p, ulong pm)
    1081             : {
    1082     4650276 :   pari_sp av0 = avma;
    1083             :   long li, co, i, j, k, def, ldef;
    1084             :   ulong m;
    1085             : 
    1086     4650276 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1087     4650276 :   li = lgcols(x);
    1088     4650287 :   x = Flm_copy(x);
    1089     4650423 :   m = u_lval(pm, p);
    1090             : 
    1091     4650427 :   ldef = (li > co)? li - co: 0;
    1092    20743995 :   for (def = co-1,i = li-1; i > ldef; i--)
    1093             :   {
    1094    16278956 :     long vmin = LONG_MAX, kmin = 0;
    1095    16278956 :     ulong umin = 0, pvmin, q;
    1096    49070189 :     for (k = 1; k <= def; k++)
    1097             :     {
    1098    41475667 :       ulong u = ucoeff(x,i,k);
    1099             :       long v;
    1100    41475667 :       if (!u) continue;
    1101    22653407 :       v = u_lvalrem(u, p, &u);
    1102    22653312 :       if (v >= (long) m) ucoeff(x,i,k) = 0;
    1103    22653312 :       else if (v < vmin) {
    1104    16322468 :         vmin = v; kmin = k; umin = u;
    1105    16322468 :         if (!vmin) break;
    1106             :       }
    1107             :     }
    1108    16278861 :     if (!kmin)
    1109             :     {
    1110     1015217 :       if (early_abort) return NULL;
    1111      830429 :       ucoeff(x,i,def) = 0;
    1112      830429 :       ldef--;
    1113      830429 :       if (ldef < 0) ldef = 0;
    1114      830429 :       continue;
    1115             :     }
    1116    15263644 :     if (kmin != def) swap(gel(x,def), gel(x,kmin));
    1117    15263644 :     q = vmin? upowuu(p, m-vmin): pm;
    1118             :     /* pivot has valuation vmin */
    1119    15263661 :     umin %= q;
    1120    15263661 :     if (umin != 1)
    1121     7730240 :       Flv_Fl_mul_part_inplace(gel(x,def), Fl_inv(umin,q), pm, i-1);
    1122    15263652 :     ucoeff(x, i, def) = pvmin = upowuu(p, vmin);
    1123    67519242 :     for (j = def-1; j; j--)
    1124             :     { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
    1125    52256103 :       ulong t, a = ucoeff(x,i,j);
    1126    52256103 :       if (!a) continue;
    1127             : 
    1128    29505298 :       t = Fl_neg(a / pvmin, q);
    1129    29505210 :       Flc_lincomb1_inplace(gel(x,j), gel(x,def), t, pm);
    1130             :     }
    1131    15263139 :     def--;
    1132             :   }
    1133     4465039 :   if (co > li)
    1134             :   {
    1135           0 :     x += co - li;
    1136           0 :     x[0] = evaltyp(t_MAT) | _evallg(li);
    1137             :   }
    1138     4465039 :   return gerepilecopy(av0, x);
    1139             : }
    1140             : 
    1141             : static int
    1142      219759 : ZV_allequal(GEN v)
    1143             : {
    1144      219759 :   long i, l = lg(v);
    1145      219759 :   if (l > 1)
    1146             :   {
    1147      219759 :     GEN x = gel(v,1);
    1148      389013 :     for (i = 2; i < l; i++) if (!equalii(x,gel(v,i))) return 0;
    1149             :   }
    1150      199071 :   return 1;
    1151             : }
    1152             : /* compute optimal D for hnfmod: x upper triangular */
    1153             : static GEN
    1154     4773192 : optimal_D(GEN x, GEN D)
    1155             : {
    1156     4773192 :   long i, n = nbrows(x);
    1157     4773058 :   GEN C = shallowcopy(D);
    1158     4773340 :   gel(C,1) = gcoeff(x,1,1);
    1159     5184889 :   for (i = 2; i < n; i++)
    1160             :   {
    1161     2574829 :     GEN c = mulii(gel(C,i-1), gcoeff(x,i,i));
    1162     2574709 :     if (signe(c) < 0) togglesign(c);
    1163     2574709 :     if (cmpii(c, gel(D,i)) >= 0) break;
    1164      411549 :     gel(C,i) = c;
    1165             :   }
    1166     4773199 :   return C;
    1167             : }
    1168             : 
    1169             : /* D = multiple of det x (usually detint(x)) or vector of positive moduli
    1170             :  * (compute hnf(x | D))
    1171             :  * flag & hnf_MODID: reduce mod D * matid [ otherwise as above ].
    1172             :  * flag & hnf_PART: don't reduce once diagonal is known
    1173             :  * flag & hnf_CENTER: centermod HNF (2|x[i,j]| <] x[i,i]) */
    1174             : GEN
    1175     4775306 : ZM_hnfmodall_i(GEN x, GEN D, long flag)
    1176             : {
    1177             :   pari_sp av;
    1178     4775306 :   const long center = (flag & hnf_CENTER);
    1179             :   long moddiag, modsame, nli, li, co, i, j, k, def, ldef;
    1180             :   GEN u, LDM;
    1181             : 
    1182     4775306 :   co = lg(x);
    1183     4775306 :   if (co == 1)
    1184             :   {
    1185        1561 :     if (typ(D) == t_INT || lg(D) == 1) return cgetg(1,t_MAT);
    1186         105 :     x = diagonal_shallow(D); /* handle flags properly */
    1187         105 :     co = lg(x);
    1188             :   }
    1189     4773850 :   li = lgcols(x);
    1190     4773846 :   if (li == 1)
    1191             :   {
    1192         185 :     if (typ(D) != t_INT && lg(D) != li) pari_err_DIM("ZM_hnfmod");
    1193         185 :     return cgetg(1,t_MAT);
    1194             :   }
    1195     4773661 :   nli = li - 1;
    1196     4773661 :   modsame = typ(D)==t_INT;
    1197     4773661 :   if (!modsame)
    1198             :   {
    1199      219772 :     if (lg(D) != li) pari_err_DIM("ZM_hnfmod");
    1200      219758 :     if (ZV_allequal(D)) { modsame = 1; D = gel(D,1); }
    1201             :   }
    1202     4773646 :   moddiag = (flag & hnf_MODID) || !modsame;
    1203             :   /* modsame: triangularize mod fixed d*Id;
    1204             :    * moddiag: modulo diagonal matrix, else modulo multiple of determinant */
    1205             : 
    1206     4773646 :   if (modsame)
    1207             :   {
    1208     4752982 :     LDM = const_vecsmall(nli, 2*lgefint(D)-2);
    1209     4753759 :     D = const_vec(nli,D);
    1210             :   }
    1211             :   else
    1212             :   {
    1213       20664 :     LDM = cgetg(li, t_VECSMALL);
    1214       68039 :     for (i=1; i<li; i++) LDM[i] = lgefint(gel(D,i));
    1215             :   }
    1216     4774464 :   av = avma;
    1217     4774464 :   x = RgM_shallowcopy(x);
    1218             : 
    1219     4774618 :   ldef = 0;
    1220     4774618 :   if (li > co)
    1221             :   {
    1222      135729 :     ldef = li - co;
    1223      135729 :     if (!moddiag)
    1224           7 :       pari_err_DOMAIN("ZM_hnfmod","nb lines",">", strtoGENstr("nb columns"), x);
    1225             :   }
    1226    18969378 :   for (def = co-1,i = nli; i > ldef; i--,def--)
    1227             :   {
    1228    14188629 :     GEN d = gel(D,i);
    1229    14188629 :     long add_N = modsame;
    1230    61100155 :     for (j = 1; j < def; j++)
    1231             :     {
    1232    46905405 :       GEN p1, p2, b, a = gcoeff(x,i,j) = remii(gcoeff(x,i,j), d);
    1233    58844569 :       if (!signe(a)) continue;
    1234             : 
    1235    32036561 :       k = j+1;
    1236    32036561 :       b = gcoeff(x,i,k) = remii(gcoeff(x,i,k), d);
    1237    32039247 :       if (!signe(b)) { swap(gel(x,j), gel(x,k)); continue; }
    1238    20099893 :       if (add_N)
    1239             :       { /* ensure the moving pivot on row i divides d from now on */
    1240     7146188 :         add_N = 0;
    1241     7146188 :         if (!equali1(a))
    1242             :         { /* x[j] *= u; after this, a = x[i,j] | d */
    1243     5804298 :           GEN u = Fp_invgen(a, d, &a);
    1244             :           long t;
    1245     5805524 :           p1 = gel(x,j);
    1246    20588605 :           for (t = 1; t < i; t++) gel(p1,t) = mulii(gel(p1,t), u);
    1247     5804107 :           FpV_red_part_ipvec(p1, D, i-1);
    1248     5803465 :           gel(p1,i) = a;
    1249     5803465 :           if (2*lg(a) < lg(b))
    1250             :           { /* reduce x[i,k] mod x[i,j]: helps ZC_elem */
    1251       26417 :             GEN r, q = dvmdii(b, a, &r);
    1252       26417 :             togglesign(q);
    1253       26417 :             ZC_lincomb1_inplace_i(gel(x,k), gel(x,j), q, i-1);
    1254       26417 :             FpV_red_part_ipvec(gel(x,k), D, i-1);
    1255       26417 :             gcoeff(x,i,k) = b = r;
    1256             :           }
    1257             :         }
    1258             :       }
    1259    20098994 :       ZC_elem(a,b, x, NULL, j,k);
    1260    20103524 :       p1 = gel(x,j);
    1261    20103524 :       p2 = gel(x,k);
    1262             :       /* prevent coeffs explosion */
    1263   115836140 :       for (k = 1; k < i; k++)
    1264             :       {
    1265    95732622 :         if (lgefint(gel(p1,k)) > LDM[k]) gel(p1,k) = remii(gel(p1,k),gel(D,k));
    1266    95732616 :         if (lgefint(gel(p2,k)) > LDM[k]) gel(p2,k) = remii(gel(p2,k),gel(D,k));
    1267             :       }
    1268             :     }
    1269    14194750 :     if (gc_needed(av,2))
    1270             :     {
    1271          24 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[1]. i=%ld",i);
    1272          24 :       x = gerepilecopy(av, x);
    1273             :     }
    1274    14194750 :     if (moddiag && !signe(gcoeff(x,i,def)))
    1275             :     { /* missing pivot on line i, insert column */
    1276      933524 :       GEN a = cgetg(co + 1, t_MAT);
    1277     4960517 :       for (k = 1; k <= def; k++) gel(a,k) = gel(x,k);
    1278      933526 :       gel(a,k++) = Rg_col_ei(gel(D,i), nli, i);
    1279     3930838 :       for (     ; k <= co;  k++) gel(a,k) = gel(x,k-1);
    1280      933527 :       ldef--; if (ldef < 0) ldef = 0;
    1281      933527 :       co++; def++; x = a;
    1282             :     }
    1283             :   }
    1284     4780749 :   if (co < li)
    1285             :   { /* implies moddiag, add missing diag(D) components */
    1286      103809 :     GEN a = cgetg(li+1, t_MAT);
    1287      242500 :     for (k = 1; k <= li-co; k++) gel(a,k) = Rg_col_ei(gel(D,k), nli, k);
    1288      284207 :     for (i = 1; i < co; i++) gel(a,k-1+i) = gel(x,i);
    1289      103810 :     gel(a,li) = zerocol(nli); x = a;
    1290             :   }
    1291             :   else
    1292             :   {
    1293     4676940 :     x += co - li;
    1294     4676940 :     x[0] = evaltyp(t_MAT) | _evallg(li); /* kill 0 columns */
    1295     4676940 :     if (moddiag) x = shallowconcat(x, zerocol(nli));
    1296             :   }
    1297     4774395 :   if (moddiag)
    1298             :   { /* x[li]: extra column, an accumulator discarded at the end */
    1299             :     GEN D2;
    1300     4618923 :     gcoeff(x,1,1) = gcdii(gcoeff(x,1,1), gel(D,1));
    1301     4617827 :     D2 = optimal_D(x,D);
    1302             :     /* add up missing diag(D) components */
    1303    18513737 :     for (i = nli; i > 0; i--)
    1304             :     {
    1305    13895461 :       gcoeff(x, i, li) = gel(D,i);
    1306    53113402 :       for (j = i; j > 0; j--)
    1307             :       {
    1308    39220200 :         GEN a = gcoeff(x, j, li);
    1309    39220200 :         if (!signe(a)) continue;
    1310    14656233 :         ZC_elem(a, gcoeff(x,j,j), x, NULL, li,j);
    1311    14655024 :         FpV_red_part_ipvec(gel(x,li), D, j-1);
    1312    14653006 :         FpV_red_part_ipvec(gel(x,j),  D, j-1);
    1313             :       }
    1314    13893202 :       if (gc_needed(av,1))
    1315             :       {
    1316          32 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[2]. i=%ld", i);
    1317          32 :         gerepileall(av, 2, &x, &D2);
    1318             :       }
    1319             :     }
    1320     4618276 :     D = D2;
    1321             :   }
    1322             :   else
    1323             :   {
    1324      155472 :     GEN b = gel(D,1);
    1325      584883 :     for (i = nli; i > 0; i--)
    1326             :     {
    1327      429407 :       GEN d = bezout(gcoeff(x,i,i),b, &u,NULL);
    1328      429417 :       gcoeff(x,i,i) = d;
    1329      429417 :       FpV_Fp_mul_part_ip(gel(x,i), u, b, i-1);
    1330      429410 :       if (i > 1) b = diviiexact(b,d);
    1331             :     }
    1332      155476 :     D = optimal_D(x,D);
    1333             :   }
    1334     4773752 :   x[0] = evaltyp(t_MAT) | _evallg(li); /* kill 0 columns, discard accumulator */
    1335     4773752 :   if (flag & hnf_PART) return x;
    1336             : 
    1337    19099492 :   for (i = nli; i > 0; i--)
    1338             :   {
    1339    14328997 :     GEN diag = gcoeff(x,i,i);
    1340    14328997 :     if (signe(diag) < 0) { gel(x,i) = ZC_neg(gel(x,i)); diag = gcoeff(x,i,i); }
    1341    14329002 :     if (i != nli)
    1342    25958161 :       for (j = 1; j < i; j++) gcoeff(x,j,i) = remii(gcoeff(x,j,i), gel(D,j));
    1343    40284465 :     for (j = i+1; j < li; j++)
    1344             :     {
    1345    25956754 :       GEN b = gcoeff(x,i,j) = remii(gcoeff(x,i,j), gel(D,i));
    1346    25955838 :       GEN r, q = truedvmdii(b, diag, &r);
    1347             :       /* ensure -diag/2 <= r < diag/2 */
    1348    25955460 :       if (center && signe(r) && abscmpii(shifti(r,1),diag) >= 0)
    1349      616987 :       { r = subii(r,diag); q = addiu(q,1); }
    1350    25957095 :       if (!signe(q)) continue;
    1351    10765997 :       togglesign(q);
    1352    10765970 :       ZC_lincomb1_inplace_i(gel(x,j), gel(x,i), q, i-1);
    1353    10764381 :       gcoeff(x,i,j) = r;
    1354             :     }
    1355    14327711 :     if (gc_needed(av,1))
    1356             :     {
    1357          40 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[3]. i=%ld", i);
    1358          40 :       gerepileall(av, 2, &x, &D);
    1359             :     }
    1360             :   }
    1361     4770495 :   return x;
    1362             : }
    1363             : GEN
    1364     4726255 : ZM_hnfmodall(GEN x, GEN dm, long flag)
    1365             : {
    1366     4726255 :   pari_sp av = avma;
    1367     4726255 :   return gerepilecopy(av, ZM_hnfmodall_i(x, dm, flag));
    1368             : }
    1369             : GEN
    1370      155469 : ZM_hnfmod(GEN x, GEN d) { return ZM_hnfmodall(x,d,0); }
    1371             : GEN
    1372     4172158 : ZM_hnfmodid(GEN x, GEN d)
    1373     4172158 : { return ZM_hnfmodall(x,d,hnf_MODID); }
    1374             : /* return the column echelon form of x with 1's as pivots,
    1375             :  * P contains the row indices containing the pivots in increasing order */
    1376             : static GEN
    1377     3167910 : FpM_echelon(GEN x, GEN *pP, GEN p)
    1378             : {
    1379             :   pari_sp av;
    1380             :   long iP, li, co, i, j, k, def, ldef;
    1381             :   GEN P;
    1382             : 
    1383     3167910 :   co = lg(x); if (co == 1) { *pP = cgetg(1,t_VECSMALL); return cgetg(1,t_MAT); }
    1384     3167910 :   li = lgcols(x);
    1385     3167889 :   iP = 1;
    1386     3167889 :   *pP = P = cgetg(li, t_VECSMALL);
    1387     3167943 :   av = avma;
    1388     3167943 :   x = FpM_red(x, p);
    1389             : 
    1390     3167170 :   ldef = (li > co)? li - co: 0;
    1391    12053623 :   for (def = co-1,i = li-1; i > ldef; i--)
    1392             :   {
    1393     8883903 :     GEN u = NULL;
    1394    13714733 :     for (k = def; k; k--)
    1395             :     {
    1396    10283647 :       u = gcoeff(x,i,k);
    1397    10283647 :       if (signe(u)) break;
    1398             :     }
    1399     8883903 :     if (!k)
    1400             :     {
    1401     3433124 :       if (--ldef < 0) ldef = 0;
    1402     3433124 :       continue;
    1403             :     }
    1404     5450779 :     P[iP++] = i;
    1405     5450779 :     if (k != def) swap(gel(x,def), gel(x,k));
    1406     5450779 :     if (!equali1(u))
    1407     4338309 :       FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(u,p), p, i-1);
    1408     5452818 :     gcoeff(x, i, def) = gen_1;
    1409    17251904 :     for (j = def-1; j; j--)
    1410             :     { /* zero x[i, 1..def-1] using x[i,def] = 1*/
    1411    11799269 :       GEN xj = gel(x,j), u = gel(xj,i);
    1412    11799269 :       if (!signe(u)) continue;
    1413             : 
    1414     7464047 :       ZC_lincomb1_inplace(xj, gel(x,def), negi(u));
    1415    38511503 :       for (k = 1; k < i; k++) gel(xj,k) = modii(gel(xj,k), p);
    1416             :     }
    1417     5452635 :     if (gc_needed(av,2))
    1418             :     {
    1419           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpM_echelon. i=%ld",i);
    1420           0 :       x = gerepilecopy(av, x);
    1421             :     }
    1422     5453329 :     def--;
    1423             :   }
    1424             :   /* rank = iP - 1 */
    1425     3169720 :   setlg(P, iP); vecsmall_sort(P);
    1426     3167924 :   if (co > iP) x += co - iP;
    1427     3167924 :   x[0] = evaltyp(t_MAT) | _evallg(iP);
    1428     3167924 :   return x;
    1429             : }
    1430             : /* given x square of maximal rank with 1 or p on diagonal from hnfmodid
    1431             :  * (=> a column containing p has its other entries at 0 ), return the HNF */
    1432             : static GEN
    1433     3168173 : FpM_hnfend(pari_sp av, GEN x, GEN p)
    1434             : {
    1435     3168173 :   long i, l = lgcols(x);
    1436    12054454 :   for (i = l-1; i > 0; i--)
    1437             :   {
    1438     8887461 :     GEN diag = gcoeff(x,i,i);
    1439             :     long j;
    1440     8887461 :     if (is_pm1(diag))
    1441    11176825 :       for (j = i+1; j < l; j++)
    1442             :       {
    1443     5722760 :         GEN xj = gel(x,j), b = gel(xj,i);
    1444             :         long k;
    1445     5722760 :         if (!signe(b)) continue;
    1446     4023169 :         ZC_lincomb1_inplace(xj, gel(x,i), negi(b));
    1447    19551395 :         for (k=1; k<i; k++)
    1448    15528413 :           if (lgefint(gel(xj,k)) > 3) gel(xj,k) = remii(gel(xj,k), p);
    1449             :       }
    1450             :     else
    1451     9851726 :       for (j = i+1; j < l; j++) gcoeff(x,i,j) = modii(gcoeff(x,i,j), p);
    1452     8886935 :     if (gc_needed(av,2))
    1453             :     {
    1454           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpM_hnfend. i=%ld",i);
    1455           0 :       x = gerepilecopy(av, x);
    1456             :     }
    1457             :   }
    1458     3166993 :   return x;
    1459             : }
    1460             : GEN
    1461     3167922 : ZM_hnfmodprime(GEN x, GEN p)
    1462             : {
    1463     3167922 :   pari_sp av = avma;
    1464             :   GEN P, y;
    1465             :   long l, lP, i;
    1466     3167922 :   if (lg(x) == 1) return cgetg(1, t_MAT);
    1467     3167922 :   l = lgcols(x);
    1468     3167902 :   x = FpM_echelon(x, &P, p);
    1469     3167926 :   lP = lg(P); /* rank = lP-1 */
    1470     3167926 :   if (lP == l) { set_avma(av); return matid(l-1); }
    1471     3167926 :   y = scalarmat_shallow(p, l-1);
    1472     8622471 :   for (i = 1; i < lP; i++) gel(y,P[i]) = gel(x,i);
    1473     3168168 :   return gerepilecopy(av, FpM_hnfend(av,y,p));
    1474             : }
    1475             : 
    1476             : static GEN
    1477      397344 : allhnfmod(GEN x, GEN dm, int flag)
    1478             : {
    1479      397344 :   if (typ(x)!=t_MAT) pari_err_TYPE("allhnfmod",x);
    1480      397344 :   RgM_check_ZM(x, "allhnfmod");
    1481      397340 :   if (isintzero(dm)) return ZM_hnf(x);
    1482      397340 :   return ZM_hnfmodall(x, dm, flag);
    1483             : }
    1484             : GEN
    1485          14 : hnfmod(GEN x, GEN d)
    1486             : {
    1487          14 :   if (typ(d) != t_INT) pari_err_TYPE("mathnfmod",d);
    1488          14 :   return allhnfmod(x, d, 0);
    1489             : }
    1490             : GEN
    1491      397330 : hnfmodid(GEN x, GEN d)
    1492             : {
    1493      397330 :   switch(typ(d))
    1494             :   {
    1495      380179 :     case t_INT: break;
    1496       17151 :     case t_VEC: case t_COL:
    1497       17151 :       if (RgV_is_ZV(d)) break;
    1498           0 :     default: pari_err_TYPE("mathnfmodid",d);
    1499             :   }
    1500      397330 :   return allhnfmod(x, d, hnf_MODID);
    1501             : }
    1502             : 
    1503             : /* M a ZM in HNF. Normalize with *centered* residues */
    1504             : GEN
    1505       25080 : ZM_hnfcenter(GEN M)
    1506             : {
    1507       25080 :   long i, j, k, N = lg(M)-1;
    1508       25080 :   pari_sp av = avma;
    1509             : 
    1510       88351 :   for (j=N-1; j>0; j--) /* skip last line */
    1511             :   {
    1512       63269 :     GEN Mj = gel(M,j), a = gel(Mj,j);
    1513      210890 :     for (k = j+1; k <= N; k++)
    1514             :     {
    1515      147619 :       GEN Mk = gel(M,k), q = diviiround(gel(Mk,j), a);
    1516      147620 :       long s = signe(q);
    1517      147620 :       if (!s) continue;
    1518       68422 :       if (is_pm1(q))
    1519             :       {
    1520       31900 :         if (s < 0)
    1521        8291 :           for (i = 1; i <= j; i++) gel(Mk,i) = addii(gel(Mk,i), gel(Mj,i));
    1522             :         else
    1523       92942 :           for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), gel(Mj,i));
    1524             :       }
    1525             :       else
    1526      204347 :         for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), mulii(q,gel(Mj,i)));
    1527       68422 :       if (gc_needed(av,1))
    1528             :       {
    1529           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfcenter, j = %ld",j);
    1530           0 :         M = gerepilecopy(av, M);
    1531             :       }
    1532             :     }
    1533             :   }
    1534       25082 :   return M;
    1535             : }
    1536             : 
    1537             : /***********************************************************************/
    1538             : /*                                                                     */
    1539             : /*                 HNFLLL (Havas, Majewski, Mathews)                   */
    1540             : /*                                                                     */
    1541             : /***********************************************************************/
    1542             : 
    1543             : static void
    1544     2024693 : Minus(long j, GEN lambda)
    1545             : {
    1546     2024693 :   long k, n = lg(lambda);
    1547             : 
    1548    11674251 :   for (k=1  ; k<j; k++) togglesign_safe(&gcoeff(lambda,k,j));
    1549    17063175 :   for (k=j+1; k<n; k++) togglesign_safe(&gcoeff(lambda,j,k));
    1550     2024707 : }
    1551             : 
    1552             : /* index of first nonzero entry */
    1553             : static long
    1554   105960256 : findi(GEN M)
    1555             : {
    1556   105960256 :   long i, n = lg(M);
    1557  2670512099 :   for (i=1; i<n; i++)
    1558  2639394017 :     if (signe(gel(M,i))) return i;
    1559    31118082 :   return 0;
    1560             : }
    1561             : 
    1562             : static long
    1563   105924414 : findi_normalize(GEN Aj, GEN B, long j, GEN lambda)
    1564             : {
    1565   105924414 :   long r = findi(Aj);
    1566   106045505 :   if (r && signe(gel(Aj,r)) < 0)
    1567             :   {
    1568     2024463 :     ZV_togglesign(Aj); if (B) ZV_togglesign(gel(B,j));
    1569     2024684 :     Minus(j,lambda);
    1570             :   }
    1571   106065999 :   return r;
    1572             : }
    1573             : 
    1574             : static void
    1575    52962530 : reduce2(GEN A, GEN B, long k, long j, long *row0, long *row1, GEN lambda, GEN D)
    1576             : {
    1577             :   GEN q;
    1578             :   long i;
    1579             : 
    1580    52962530 :   *row0 = findi_normalize(gel(A,j), B,j,lambda);
    1581    53045519 :   *row1 = findi_normalize(gel(A,k), B,k,lambda);
    1582    53123209 :   if (*row0)
    1583    31067800 :     q = truedivii(gcoeff(A,*row0,k), gcoeff(A,*row0,j));
    1584    22055409 :   else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
    1585     7295634 :     q = diviiround(gcoeff(lambda,j,k), gel(D,j));
    1586             :   else
    1587    14741845 :     return;
    1588             : 
    1589    38354395 :   if (signe(q))
    1590             :   {
    1591    14321813 :     GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
    1592    14321813 :     togglesign_safe(&q);
    1593    14325260 :     if (*row0) ZC_lincomb1_inplace(gel(A,k),gel(A,j),q);
    1594    14321907 :     if (B) ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
    1595    14299162 :     gel(Lk,j) = addmulii(gel(Lk,j), q, gel(D,j));
    1596    14293774 :     if (is_pm1(q))
    1597             :     {
    1598     5389162 :       if (signe(q) > 0)
    1599             :       {
    1600     5385978 :         for (i=1; i<j; i++)
    1601     3328979 :           if (signe(gel(Lj,i))) gel(Lk,i) = addii(gel(Lk,i), gel(Lj,i));
    1602             :       }
    1603             :       else
    1604             :       {
    1605    11296848 :         for (i=1; i<j; i++)
    1606     7964746 :           if (signe(gel(Lj,i))) gel(Lk,i) = subii(gel(Lk,i), gel(Lj,i));
    1607             :       }
    1608             :     }
    1609             :     else
    1610             :     {
    1611    39738905 :       for (i=1; i<j; i++)
    1612    30804829 :         if (signe(gel(Lj,i))) gel(Lk,i) = addmulii(gel(Lk,i), q, gel(Lj,i));
    1613             :     }
    1614             :   }
    1615             : }
    1616             : 
    1617             : static void
    1618     8370456 : hnfswap(GEN A, GEN B, long k, GEN lambda, GEN D)
    1619             : {
    1620     8370456 :   GEN t, p1, p2, Lk = gel(lambda,k);
    1621     8370456 :   long i,j,n = lg(A);
    1622             : 
    1623     8370456 :   swap(gel(A,k), gel(A,k-1));
    1624     8370456 :   if (B) swap(gel(B,k), gel(B,k-1));
    1625    46297540 :   for (j=k-2; j; j--) swap(gcoeff(lambda,j,k-1), gel(Lk,j));
    1626   104416903 :   for (i=k+1; i<n; i++)
    1627             :   {
    1628    96040973 :     GEN Li = gel(lambda,i);
    1629    96040973 :     p1 = mulii(gel(Li,k-1), gel(D,k));
    1630    96046712 :     p2 = mulii(gel(Li,k), gel(Lk,k-1));
    1631    96038368 :     t = subii(p1,p2);
    1632             : 
    1633    96048047 :     p1 = mulii(gel(Li,k), gel(D,k-2));
    1634    96047449 :     p2 = mulii(gel(Li,k-1), gel(Lk,k-1));
    1635    96067792 :     gel(Li,k-1) = diviiexact(addii(p1,p2), gel(D,k-1));
    1636    96026753 :     gel(Li,k) = diviiexact(t, gel(D,k-1));
    1637             :   }
    1638     8375930 :   p1 = mulii(gel(D,k-2), gel(D,k));
    1639     8367516 :   p2 = sqri(gel(Lk,k-1));
    1640     8363848 :   gel(D,k-1) = diviiexact(addii(p1,p2), gel(D,k-1));
    1641     8360576 : }
    1642             : 
    1643             : /* reverse row order in matrix A, IN PLACE */
    1644             : static GEN
    1645      427738 : reverse_rows(GEN A)
    1646             : {
    1647      427738 :   long i, j, h, n = lg(A);
    1648      427738 :   if (n == 1) return A;
    1649      427738 :   h = lgcols(A);
    1650     3769874 :   for (j=1; j<n; j++)
    1651             :   {
    1652     3342135 :     GEN c = gel(A,j);
    1653             :     /* start at (h-1) >>1 : if h = 2i even, no need to swap c[i] and itself */
    1654     9358250 :     for (i=(h-1)>>1; i; i--) swap(gel(c,i), gel(c,h-i));
    1655             :   }
    1656      427739 :   return A;
    1657             : }
    1658             : /* decide whether to swap */
    1659             : static int
    1660     4732264 : must_swap(long k, GEN lambda, GEN D)
    1661             : {
    1662     4732264 :   pari_sp av = avma;
    1663     4732264 :   GEN z = addii(mulii(gel(D,k-2),gel(D,k)), sqri(gcoeff(lambda,k-1,k)));
    1664     4731057 :   return gc_bool(av, cmpii(z, sqri(gel(D,k-1))) < 0);
    1665             : }
    1666             : 
    1667             : GEN
    1668      213876 : ZM_hnflll(GEN A, GEN *ptB, int remove)
    1669             : {
    1670      213876 :   pari_sp av = avma;
    1671             :   long n, k, kmax;
    1672             :   GEN B, lambda, D;
    1673             : 
    1674      213876 :   n = lg(A);
    1675      213876 :   A = reverse_rows(ZM_copy(A)); /* ZM_copy for in place findi_normalize() */
    1676      213876 :   B = ptB? matid(n-1): NULL;
    1677      213875 :   D = const_vec(n, gen_1) + 1;
    1678      213875 :   lambda = zeromatcopy(n-1,n-1);
    1679      183511 :   k = kmax = 2;
    1680    15555522 :   while (k < n)
    1681             :   {
    1682             :     long row0, row1;
    1683             :     int do_swap;
    1684    15341652 :     reduce2(A,B,k,k-1,&row0,&row1,lambda,D);
    1685    15362558 :     if      (row0) do_swap = (!row1 || row0 <= row1);
    1686     5622917 :     else if (row1) do_swap = 0;
    1687     4477066 :     else do_swap = must_swap(k,lambda,D);
    1688    15382641 :     if (do_swap)
    1689             :     {
    1690     8128624 :       hnfswap(A,B,k,lambda,D);
    1691     8119606 :       if (k > 2) k--;
    1692             :     }
    1693             :     else
    1694             :     {
    1695             :       long i;
    1696    44947322 :       for (i=k-2; i; i--)
    1697             :       {
    1698             :         long row0, row1;
    1699    37694917 :         reduce2(A,B,k,i,&row0,&row1,lambda,D);
    1700             :       }
    1701     7252405 :       if (++k > kmax) kmax = k;
    1702             :     }
    1703    15372011 :     if (gc_needed(av,3))
    1704             :     {
    1705         187 :       GEN b = D-1;
    1706         187 :       if (DEBUGMEM>1) pari_warn(warnmem,"hnflll, kmax = %ld / %ld",kmax,n-1);
    1707         187 :       gerepileall(av, B? 4: 3, &A, &lambda, &b, &B);
    1708         187 :       if (gc_needed(av,1)) paristack_resize(0); /* avoid desperation GC */
    1709         187 :       D = b+1;
    1710             :     }
    1711             :   }
    1712             :   /* handle trivial case: return negative diag coefficient otherwise */
    1713      213870 :   if (n == 2) (void)findi_normalize(gel(A,1), B,1,lambda);
    1714      213870 :   A = reverse_rows(A);
    1715      213875 :   if (remove)
    1716             :   {
    1717             :     long i;
    1718        7371 :     for (i = 1; i < n; i++)
    1719        7147 :       if (!ZV_equal0(gel(A,i))) break;
    1720        2107 :     remove_0cols(i-1, &A, &B, remove);
    1721             :   }
    1722      213875 :   gerepileall(av, B? 2: 1, &A, &B);
    1723      213876 :   if (B) *ptB = B;
    1724      213876 :   return A;
    1725             : }
    1726             : 
    1727             : GEN
    1728           7 : hnflll(GEN x)
    1729             : {
    1730           7 :   GEN z = cgetg(3, t_VEC);
    1731           7 :   gel(z,1) = ZM_hnflll(x, &gel(z,2), 1);
    1732           7 :   return z;
    1733             : }
    1734             : 
    1735             : /* Variation on HNFLLL: Extended GCD */
    1736             : 
    1737             : static void
    1738      949238 : reduce1(GEN A, GEN B, long k, long j, GEN lambda, GEN D)
    1739             : {
    1740             :   GEN q;
    1741             :   long i;
    1742             : 
    1743      949238 :   if (signe(gel(A,j)))
    1744      172523 :     q = diviiround(gel(A,k),gel(A,j));
    1745      776715 :   else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
    1746       96200 :     q = diviiround(gcoeff(lambda,j,k), gel(D,j));
    1747             :   else
    1748      680515 :     return;
    1749             : 
    1750      268723 :   if (signe(q))
    1751             :   {
    1752      232241 :     GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
    1753      232241 :     togglesign_safe(&q);
    1754      232241 :     gel(A,k) = addmulii(gel(A,k), q, gel(A,j));
    1755      232241 :     ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
    1756      232241 :     gel(Lk,j) = addmulii(gel(Lk,j), q, gel(D,j));
    1757      421411 :     for (i=1; i<j; i++)
    1758      189170 :       if (signe(gel(Lj,i))) gel(Lk,i) = addmulii(gel(Lk,i), q, gel(Lj,i));
    1759             :   }
    1760             : }
    1761             : 
    1762             : static GEN
    1763      109046 : ZV_gcdext_i(GEN A)
    1764             : {
    1765      109046 :   long k, n = lg(A);
    1766             :   GEN B, lambda, D;
    1767             : 
    1768      109046 :   if (n == 1) retmkvec2(gen_1, cgetg(1,t_MAT));
    1769      109046 :   A = leafcopy(A);
    1770      109046 :   B = matid(n-1);
    1771      109046 :   lambda = zeromatcopy(n-1,n-1);
    1772      109046 :   D = const_vec(n, gen_1) + 1;
    1773      109046 :   k = 2;
    1774      697271 :   while (k < n)
    1775             :   {
    1776             :     int do_swap;
    1777             : 
    1778      588225 :     reduce1(A,B,k,k-1,lambda,D);
    1779      588225 :     if    (signe(gel(A,k-1))) do_swap = 1;
    1780      415702 :     else if (signe(gel(A,k))) do_swap = 0;
    1781      223481 :     else do_swap = must_swap(k,lambda,D);
    1782      588225 :     if (do_swap)
    1783             :     {
    1784      240790 :       hnfswap(A,B,k,lambda,D);
    1785      240790 :       if (k > 2) k--;
    1786             :     }
    1787             :     else
    1788             :     {
    1789             :       long i;
    1790      708448 :       for (i=k-2; i; i--) reduce1(A,B,k,i,lambda,D);
    1791      347435 :       k++;
    1792             :     }
    1793             :   }
    1794      109046 :   if (signe(gel(A,n-1)) < 0)
    1795             :   {
    1796       13328 :     gel(A,n-1) = negi(gel(A,n-1));
    1797       13328 :     ZV_togglesign(gel(B,n-1));
    1798             :   }
    1799      109046 :   return mkvec2(gel(A,n-1), B);
    1800             : }
    1801             : GEN
    1802      109032 : ZV_extgcd(GEN A)
    1803             : {
    1804      109032 :   pari_sp av = avma;
    1805      109032 :   return gerepilecopy(av, ZV_gcdext_i(A));
    1806             : }
    1807             : /* as ZV_extgcd, transforming the gcd into a t_MAT, for mathnf0 */
    1808             : static GEN
    1809          21 : ZV_hnfgcdext(GEN A)
    1810             : {
    1811          21 :   pari_sp av = avma;
    1812             :   GEN z;
    1813          21 :   if (lg(A) == 1) retmkvec2(cgetg(1,t_MAT),cgetg(1,t_MAT));
    1814          14 :   z = ZV_gcdext_i(A);
    1815          14 :   gel(z,1) = mkmat(mkcol(gel(z,1)));
    1816          14 :   return gerepilecopy(av, z);
    1817             : }
    1818             : 
    1819             : /* HNF with permutation. */
    1820             : GEN
    1821         385 : ZM_hnfperm(GEN A, GEN *ptU, GEN *ptperm)
    1822             : {
    1823             :   GEN U, c, l, perm, d, p, q, b;
    1824         385 :   pari_sp av = avma, av1;
    1825             :   long r, t, i, j, j1, k, m, n;
    1826             : 
    1827         385 :   n = lg(A)-1;
    1828         385 :   if (!n)
    1829             :   {
    1830           7 :     if (ptU) *ptU = cgetg(1,t_MAT);
    1831           7 :     if (ptperm) *ptperm = cgetg(1,t_VEC);
    1832           7 :     return cgetg(1, t_MAT);
    1833             :   }
    1834         378 :   m = nbrows(A);
    1835         378 :   c = zero_zv(m);
    1836         378 :   l = zero_zv(n);
    1837         378 :   perm = cgetg(m+1, t_VECSMALL);
    1838         378 :   av1 = avma;
    1839         378 :   A = RgM_shallowcopy(A);
    1840         378 :   U = ptU? matid(n): NULL;
    1841             :   /* U base change matrix : A0*U = A all along */
    1842        1722 :   for (r=0, k=1; k <= n; k++)
    1843             :   {
    1844        3976 :     for (j=1; j<k; j++)
    1845             :     {
    1846        2632 :       if (!l[j]) continue;
    1847        2478 :       t=l[j]; b=gcoeff(A,t,k);
    1848        2478 :       if (!signe(b)) continue;
    1849             : 
    1850         533 :       ZC_elem(b,gcoeff(A,t,j), A,U,k,j);
    1851         533 :       d = gcoeff(A,t,j);
    1852         533 :       if (signe(d) < 0)
    1853             :       {
    1854           0 :         ZV_neg_inplace(gel(A,j));
    1855           0 :         if (U) ZV_togglesign(gel(U,j));
    1856           0 :         d = gcoeff(A,t,j);
    1857             :       }
    1858        1375 :       for (j1=1; j1<j; j1++)
    1859             :       {
    1860         842 :         if (!l[j1]) continue;
    1861         821 :         q = truedivii(gcoeff(A,t,j1),d);
    1862         821 :         if (!signe(q)) continue;
    1863             : 
    1864         329 :         togglesign(q);
    1865         329 :         ZC_lincomb1_inplace(gel(A,j1), gel(A,j), q);
    1866         329 :         if (U) ZC_lincomb1_inplace(gel(U,j1), gel(U,j), q);
    1867             :       }
    1868             :     }
    1869        4585 :     t = m; while (t && (c[t] || !signe(gcoeff(A,t,k)))) t--;
    1870        1344 :     if (t)
    1871             :     {
    1872        1225 :       p = gcoeff(A,t,k);
    1873       19971 :       for (i=t-1; i; i--)
    1874             :       {
    1875       18746 :         q = gcoeff(A,i,k);
    1876       18746 :         if (signe(q) && abscmpii(p,q) > 0) { p = q; t = i; }
    1877             :       }
    1878        1225 :       perm[++r] = l[k] = t; c[t] = k;
    1879        1225 :       if (signe(p) < 0)
    1880             :       {
    1881         156 :         ZV_neg_inplace(gel(A,k));
    1882         156 :         if (U) ZV_togglesign(gel(U,k));
    1883         156 :         p = gcoeff(A,t,k);
    1884             :       }
    1885             :       /* p > 0 */
    1886        3493 :       for (j=1; j<k; j++)
    1887             :       {
    1888        2268 :         if (!l[j]) continue;
    1889        2233 :         q = truedivii(gcoeff(A,t,j),p);
    1890        2233 :         if (!signe(q)) continue;
    1891             : 
    1892         245 :         togglesign(q);
    1893         245 :         ZC_lincomb1_inplace(gel(A,j), gel(A,k), q);
    1894         245 :         if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,k), q);
    1895             :       }
    1896             :     }
    1897        1344 :     if (gc_needed(av1,1))
    1898             :     {
    1899           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"hnfperm, k=%ld",k);
    1900           0 :       gerepileall(av1, U? 2: 1, &A, &U);
    1901             :     }
    1902             :   }
    1903         378 :   if (r < m)
    1904             :   {
    1905        5131 :     for (i=1,k=r; i<=m; i++)
    1906        4816 :       if (!c[i]) perm[++k] = i;
    1907             :   }
    1908             : 
    1909             : /* We have A0*U=A, U in Gl(n,Z)
    1910             :  * basis for Im(A):  columns of A s.t l[j]>0 (r   cols)
    1911             :  * basis for Ker(A): columns of U s.t l[j]=0 (n-r cols) */
    1912         378 :   p = cgetg(r+1,t_MAT);
    1913        2814 :   for (i=1; i<=m/2; i++) lswap(perm[i], perm[m+1-i]);
    1914         378 :   if (U)
    1915             :   {
    1916          84 :     GEN u = cgetg(n+1,t_MAT);
    1917         378 :     for (t=1,k=r,j=1; j<=n; j++)
    1918         294 :       if (l[j])
    1919             :       {
    1920         182 :         u[k + n-r] = U[j];
    1921         182 :         gel(p,k--) = vecpermute(gel(A,j), perm);
    1922             :       }
    1923             :       else
    1924         112 :         u[t++] = U[j];
    1925          84 :     *ptU = u;
    1926          84 :     if (ptperm) *ptperm = perm;
    1927          84 :     gerepileall(av, ptperm? 3: 2, &p, ptU, ptperm);
    1928             :   }
    1929             :   else
    1930             :   {
    1931        1344 :     for (k=r,j=1; j<=n; j++)
    1932        1050 :       if (l[j]) gel(p,k--) = vecpermute(gel(A,j), perm);
    1933         294 :     if (ptperm) *ptperm = perm;
    1934         294 :     gerepileall(av, ptperm? 2: 1, &p, ptperm);
    1935             :   }
    1936         378 :   return p;
    1937             : }
    1938             : 
    1939             : GEN
    1940         238 : ZM_hnf_knapsack(GEN x)
    1941             : {
    1942         238 :   GEN t, perm, H = ZM_hnfperm(x,NULL,&perm);
    1943         238 :   long i,j, l = lg(H), h = lgcols(H);
    1944        3213 :   for (i=1; i<h; i++)
    1945             :   {
    1946        3066 :     int fl = 0;
    1947       16697 :     for (j=1; j<l; j++)
    1948             :     {
    1949       13722 :       t = gcoeff(H,i,j);
    1950       13722 :       if (signe(t))
    1951             :       {
    1952        3101 :         if (!is_pm1(t) || fl) return NULL;
    1953        3010 :         fl = 1;
    1954             :       }
    1955             :     }
    1956             :   }
    1957         147 :   return rowpermute(H, perm_inv(perm));
    1958             : }
    1959             : 
    1960             : GEN
    1961          14 : hnfperm(GEN A)
    1962             : {
    1963          14 :   GEN y = cgetg(4, t_VEC);
    1964          14 :   gel(y,1) = ZM_hnfperm(A, &gel(y,2), &gel(y,3));
    1965          14 :   return y;
    1966             : }
    1967             : 
    1968             : /* Hermite Normal Form, with base change matrix if ptB != NULL.
    1969             :  * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
    1970             :  * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
    1971             : GEN
    1972      717632 : ZM_hnfall_i(GEN A, GEN *ptB, long remove)
    1973             : {
    1974             :   pari_sp av;
    1975             :   long m, n, r, i, j, k, li;
    1976             :   GEN B, c, h, a;
    1977             : 
    1978      717632 :   RgM_dimensions(A, &m,&n);
    1979      717631 :   if (!n)
    1980             :   {
    1981           7 :     if (ptB) *ptB = cgetg(1,t_MAT);
    1982           7 :     return cgetg(1,t_MAT);
    1983             :   }
    1984      717624 :   c = zero_zv(m);
    1985      717616 :   h = const_vecsmall(n, m);
    1986      717613 :   av = avma;
    1987      717613 :   A = RgM_shallowcopy(A);
    1988      717627 :   B = ptB? matid(n): NULL;
    1989      717631 :   r = n+1;
    1990     2015250 :   for (li=m; li; li--)
    1991             :   {
    1992     2910905 :     for (j=1; j<r; j++)
    1993             :     {
    1994     3544521 :       for (i=h[j]; i>li; i--)
    1995             :       {
    1996      634078 :         a = gcoeff(A,i,j);
    1997      634078 :         k = c[i];
    1998             :         /* zero a = Aij  using  Aik */
    1999      634078 :         if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B,j,k);
    2000      634013 :         ZM_reduce(A,B, i,k); /* ensure reduced entries even if a = 0 */
    2001             :       }
    2002     2910443 :       if (gc_needed(av,1) && (j & 0x7f) == 0)
    2003             :       {
    2004           0 :         if (DEBUGMEM>1)
    2005           0 :           pari_warn(warnmem,"ZM_hnfall[1], li = %ld, j = %ld", li, j);
    2006           0 :         gerepileall(av, B? 2: 1, &A, &B);
    2007             :       }
    2008     2910464 :       if (signe( gcoeff(A,li,j) )) break;
    2009     1613255 :       h[j] = li-1;
    2010             :     }
    2011     1297590 :     if (j == r) continue;
    2012     1297261 :     r--;
    2013     1297261 :     if (j < r) /* A[j] != 0 */
    2014             :     {
    2015      839921 :       swap(gel(A,j), gel(A,r));
    2016      839921 :       if (B) swap(gel(B,j), gel(B,r));
    2017      839921 :       h[j] = h[r]; h[r] = li; c[li] = r;
    2018             :     }
    2019     1297261 :     if (signe(gcoeff(A,li,r)) < 0)
    2020             :     {
    2021      298984 :       ZV_neg_inplace(gel(A,r));
    2022      299013 :       if (B) ZV_togglesign(gel(B,r));
    2023             :     }
    2024     1297289 :     ZM_reduce(A,B, li,r);
    2025     1297293 :     if (gc_needed(av,1))
    2026             :     {
    2027           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[2], li = %ld", li);
    2028           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2029             :     }
    2030             :   }
    2031             : 
    2032      717600 :   if (DEBUGLEVEL>5) err_printf("\nhnfall, final phase: ");
    2033      717617 :   r--; /* first r cols are in the image the n-r (independent) last ones */
    2034     2471055 :   for (j=1; j<=r; j++)
    2035             :   {
    2036     3874058 :     for (i=h[j]; i; i--)
    2037             :     {
    2038     2120627 :       a = gcoeff(A,i,j);
    2039     2120627 :       k = c[i];
    2040     2120627 :       if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B, j,k);
    2041     2120594 :       ZM_reduce(A,B, i,k); /* ensure reduced entries, even if a = 0 */
    2042             :     }
    2043     1753431 :     if (gc_needed(av,1) && (j & 0x7f) == 0)
    2044             :     {
    2045           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[3], j = %ld", j);
    2046           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2047             :     }
    2048             :   }
    2049      717606 :   if (DEBUGLEVEL>5) err_printf("\n");
    2050      717606 :   if (remove) remove_0cols(r, &A, &B, remove);
    2051      717604 :   if (ptB) *ptB = B;
    2052      717604 :   return A;
    2053             : }
    2054             : GEN
    2055       24415 : ZM_hnfall(GEN A, GEN *ptB, long remove)
    2056             : {
    2057       24415 :   pari_sp av = avma;
    2058       24415 :   A = ZM_hnfall_i(A, ptB, remove);
    2059       24415 :   return gc_all(av, ptB? 2: 1, &A, ptB);
    2060             : }
    2061             : 
    2062             : GEN
    2063          14 : hnfall(GEN x)
    2064             : {
    2065          14 :   GEN z = cgetg(3, t_VEC);
    2066          14 :   gel(z,1) = ZM_hnfall(x, (GEN*)(z+2), 1);
    2067          14 :   return z;
    2068             : }
    2069             : GEN
    2070          14 : hnf(GEN x) { return mathnf0(x,0); }
    2071             : 
    2072             : /* C = A^(-1)t where A and C are integral, A is upper triangular, t t_INT */
    2073             : GEN
    2074      236785 : hnf_invscale(GEN A, GEN t)
    2075             : {
    2076      236785 :   long n = lg(A)-1, i,j,k;
    2077      236785 :   GEN m, c = cgetg(n+1,t_MAT);
    2078             : 
    2079      236786 :   if (!n) return c;
    2080      878203 :   for (k=1; k<=n; k++)
    2081             :   { /* cf hnf_divscale with B = id, thus b = e_k */
    2082      641455 :     GEN u = cgetg(n+1, t_COL);
    2083      641457 :     pari_sp av = avma;
    2084      641457 :     gel(c,k) = u;
    2085      641457 :     gel(u,n) = k == n? gerepileuptoint(av, diviiexact(t, gcoeff(A,n,n))): gen_0;
    2086     2169780 :     for (i=n-1; i>0; i--)
    2087             :     {
    2088     1528363 :       av = avma; m = i == k? t: gen_0;
    2089     5782394 :       for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2090     1528182 :       gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
    2091             :     }
    2092             :   }
    2093      236748 :   return c;
    2094             : }
    2095             : 
    2096             : /* C = A^(-1)(tB) where A, B, C are integral, A is upper triangular, t t_INT */
    2097             : GEN
    2098      195924 : hnf_divscale(GEN A, GEN B, GEN t)
    2099             : {
    2100      195924 :   long n = lg(A)-1, i,j,k;
    2101      195924 :   GEN m, c = cgetg(n+1,t_MAT);
    2102             : 
    2103      195924 :   if (!n) return c;
    2104      870382 :   for (k=1; k<=n; k++)
    2105             :   {
    2106      674461 :     GEN u = cgetg(n+1, t_COL), b = gel(B,k);
    2107      674461 :     pari_sp av = avma;
    2108      674461 :     gel(c,k) = u; m = mulii(gel(b,n),t);
    2109      674452 :     gel(u,n) = gerepileuptoint(av, diviiexact(m, gcoeff(A,n,n)));
    2110     3894041 :     for (i=n-1; i>0; i--)
    2111             :     {
    2112     3219583 :       av = avma; m = mulii(gel(b,i),t);
    2113    30099083 :       for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2114     3219527 :       gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
    2115             :     }
    2116             :   }
    2117      195921 :   return c;
    2118             : }
    2119             : 
    2120             : /* A, B integral upper HNF. A^(-1) B integral ? */
    2121             : int
    2122         133 : hnfdivide(GEN A, GEN B)
    2123             : {
    2124         133 :   pari_sp av = avma;
    2125         133 :   long n = lg(A)-1, i,j,k;
    2126             :   GEN u, b, m, r;
    2127             : 
    2128         133 :   if (!n) return 1;
    2129         133 :   if (lg(B)-1 != n) pari_err_DIM("hnfdivide");
    2130         133 :   u = cgetg(n+1, t_COL);
    2131         483 :   for (k=1; k<=n; k++)
    2132             :   {
    2133         364 :     b = gel(B,k);
    2134         364 :     m = gel(b,k);
    2135         364 :     gel(u,k) = dvmdii(m, gcoeff(A,k,k), &r);
    2136         364 :     if (r != gen_0) return gc_long(av, 0);
    2137         826 :     for (i=k-1; i>0; i--)
    2138             :     {
    2139         476 :       m = gel(b,i);
    2140        1337 :       for (j=i+1; j<=k; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2141         476 :       m = dvmdii(m, gcoeff(A,i,i), &r);
    2142         476 :       if (r != gen_0) return gc_long(av, 0);
    2143         476 :       gel(u,i) = m;
    2144             :     }
    2145             :   }
    2146         119 :   return gc_long(av, 1);
    2147             : }
    2148             : 
    2149             : /* A upper HNF, b integral vector. Return A^(-1) b if integral,
    2150             :  * NULL otherwise. Assume #A[,1] = #b. */
    2151             : GEN
    2152      365808 : hnf_invimage(GEN A, GEN b)
    2153             : {
    2154      365808 :   pari_sp av = avma;
    2155      365808 :   long n = lg(A)-1, m, i, k;
    2156             :   GEN u, r;
    2157             : 
    2158      365808 :   if (!n) return lg(b)==1? cgetg(1,t_COL):NULL;
    2159      365766 :   m = nbrows(A); /* m >= n */
    2160      365767 :   u = cgetg(n+1, t_COL);
    2161      829405 :   for (i = n, k = m; k > 0; k--)
    2162             :   {
    2163      829405 :     pari_sp av2 = avma;
    2164             :     long j;
    2165      829405 :     GEN t = gel(b,k), Aki = gcoeff(A,k,i);
    2166      829405 :     if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
    2167     1997780 :     for (j=i+1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
    2168      829358 :     if (!signe(Aki))
    2169             :     {
    2170           0 :       if (signe(t)) return gc_NULL(av);
    2171           0 :       set_avma(av2); gel(u,i) = gen_0; continue;
    2172             :     }
    2173      829358 :     t = dvmdii(t, Aki, &r);
    2174      829267 :     if (r != gen_0) return gc_NULL(av);
    2175      606916 :     gel(u,i) = gerepileuptoint(av2, t);
    2176      607029 :     if (--i == 0) break;
    2177             :   }
    2178             :   /* If there is a solution, it must be u. Check remaining equations */
    2179      286785 :   for (; k > 0; k--)
    2180             :   {
    2181      143390 :     pari_sp av2 = avma;
    2182             :     long j;
    2183      143390 :     GEN t = gel(b,k);
    2184      143390 :     if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
    2185      548543 :     for (j=1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
    2186      143395 :     if (signe(t)) return gc_NULL(av);
    2187      143395 :     set_avma(av2);
    2188             :   }
    2189      143395 :   return u;
    2190             : }
    2191             : 
    2192             : /* A upper HNF, B integral matrix or column. Return A^(-1) B if integral,
    2193             :  * NULL otherwise */
    2194             : GEN
    2195      314485 : hnf_solve(GEN A, GEN B)
    2196             : {
    2197             :   pari_sp av;
    2198             :   long i, l;
    2199             :   GEN C;
    2200             : 
    2201      314485 :   if (typ(B) == t_COL) return hnf_invimage(A, B);
    2202      251851 :   av = avma; C = cgetg_copy(B, &l);
    2203      350894 :   for (i = 1; i < l; i++)
    2204             :   {
    2205      266092 :     GEN c = hnf_invimage(A, gel(B,i));
    2206      266062 :     if (!c) return gc_NULL(av);
    2207       99025 :     gel(C,i) = c;
    2208             :   }
    2209       84802 :   return C;
    2210             : }
    2211             : 
    2212             : /***************************************************************/
    2213             : /**                                                           **/
    2214             : /**               SMITH NORMAL FORM REDUCTION                 **/
    2215             : /**                                                           **/
    2216             : /***************************************************************/
    2217             : 
    2218             : static GEN
    2219           0 : trivsmith(long all)
    2220             : {
    2221             :   GEN z;
    2222           0 :   if (!all) return cgetg(1,t_VEC);
    2223           0 :   z=cgetg(4,t_VEC);
    2224           0 :   gel(z,1) = cgetg(1,t_MAT);
    2225           0 :   gel(z,2) = cgetg(1,t_MAT);
    2226           0 :   gel(z,3) = cgetg(1,t_MAT); return z;
    2227             : }
    2228             : 
    2229             : static void
    2230          63 : snf_pile1(pari_sp av, GEN *x, GEN *U)
    2231             : {
    2232             :   GEN *gptr[2];
    2233          63 :   int c = 1; gptr[0]=x;
    2234          63 :   if (*U) gptr[c++] = U;
    2235          63 :   gerepilemany(av,gptr,c);
    2236          63 : }
    2237             : static void
    2238      972102 : snf_pile(pari_sp av, GEN *x, GEN *U, GEN *V)
    2239             : {
    2240             :   GEN *gptr[3];
    2241      972102 :   int c = 1; gptr[0]=x;
    2242      972102 :   if (*U) gptr[c++] = U;
    2243      972102 :   if (*V) gptr[c++] = V;
    2244      972102 :   gerepilemany(av,gptr,c);
    2245      972159 : }
    2246             : 
    2247             : static GEN
    2248      695656 : bezout_step(GEN *pa, GEN *pb, GEN *pu, GEN *pv)
    2249             : {
    2250      695656 :   GEN a = *pa, b = *pb, d;
    2251      695656 :   if (absequalii(a,b))
    2252             :   {
    2253      414145 :     long sa = signe(a), sb = signe(b);
    2254      414145 :     *pv = gen_0;
    2255      414145 :     if (sb == sa) {
    2256      406722 :       *pa = *pb = gen_1;
    2257      406722 :       if (sa > 0) {*pu=gen_1; return a;} else {*pu=gen_m1; return absi(a);}
    2258             :     }
    2259        7423 :     if (sa > 0) { *pa = *pu = gen_1; *pb = gen_m1; return a; }
    2260        1148 :     *pa = *pu = gen_m1; *pb = gen_1; return b;
    2261             :   }
    2262      281508 :   d = bezout(a,b, pu,pv);
    2263      281530 :   *pa = diviiexact(a, d);
    2264      281518 :   *pb = diviiexact(b, d); return d;
    2265             : }
    2266             : 
    2267             : static int
    2268      517582 : negcmpii(void *E, GEN x, GEN y) { (void)E; return -cmpii(x,y); }
    2269             : 
    2270             : /* x square of maximal rank; does b = x[i,i] divide all entries in
    2271             :  * x[1..i-1, 1..i-1] ? If so, return 0; else the index of a problematic row */
    2272             : static long
    2273     1063357 : ZM_snf_no_divide(GEN x, long i)
    2274             : {
    2275     1063357 :   GEN b = gcoeff(x,i,i);
    2276             :   long j, k;
    2277             : 
    2278     1063357 :   if (is_pm1(b)) return 0;
    2279      907470 :   for (k = 1; k < i; k++)
    2280     2256800 :     for (j = 1; j < i; j++)
    2281     1782217 :       if (!dvdii(gcoeff(x,k,j),b)) return k;
    2282      283207 :   return 0;
    2283             : }
    2284             : 
    2285             : static void
    2286     1378093 : ZM_redpart(GEN x, GEN p, long I)
    2287             : {
    2288     1378093 :   long l = lgefint(p), i, j;
    2289     5878774 :   for (i = 1; i <= I; i++)
    2290    27110511 :     for (j = 1; j <= I; j++)
    2291             :     {
    2292    22609830 :       GEN c = gcoeff(x,i,j);
    2293    22609830 :       if (lgefint(c) > l) gcoeff(x,i,j) = remii(c, p);
    2294             :     }
    2295     1378093 : }
    2296             : static void
    2297      861319 : ZMrow_divexact_inplace(GEN M, long i, GEN c)
    2298             : {
    2299      861319 :   long j, l = lg(M);
    2300     3411899 :   for (j = 1; j < l; j++) gcoeff(M,i,j) = diviiexact(gcoeff(M,i,j), c);
    2301      861410 : }
    2302             : 
    2303             : /* Return the SNF D of matrix X. If ptU/ptV non-NULL set them to U/V
    2304             :  * to that D = UXV */
    2305             : GEN
    2306      711763 : ZM_snfall_i(GEN x, GEN *ptU, GEN *ptV, long flag)
    2307             : {
    2308      711763 :   pari_sp av0 = avma, av;
    2309      711763 :   const long return_vec = flag & 1;
    2310             :   long i, j, k, m0, m, n0, n;
    2311      711763 :   GEN u, v, U, V, V0, mdet, A = NULL, perm = NULL;
    2312             : 
    2313      711763 :   n0 = n = lg(x)-1;
    2314      711763 :   if (!n) {
    2315       41063 :     if (ptU) *ptU = cgetg(1,t_MAT);
    2316       41063 :     if (ptV) *ptV = cgetg(1,t_MAT);
    2317       41063 :     return cgetg(1, return_vec? t_VEC: t_MAT);
    2318             :   }
    2319      670700 :   m0 = m = nbrows(x);
    2320             : 
    2321      670713 :   U = V = V0 = NULL; /* U = TRANSPOSE of row transform matrix [act on columns]*/
    2322      670713 :   if (m == n && ZM_ishnf(x))
    2323             :   {
    2324      610313 :     mdet = ZM_det_triangular(x); /* != 0 */
    2325      610207 :     if (ptV) *ptV = matid(n);
    2326             :   }
    2327             :   else
    2328             :   {
    2329       60428 :     mdet = ZM_detmult(x);
    2330       60444 :     if (!signe(mdet))
    2331          77 :       x = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
    2332             :     else
    2333             :     { /* m <= n */
    2334       60367 :       if (!ptV)
    2335         357 :         x = ZM_hnfmod(x,mdet);
    2336       60010 :       else if (m == n)
    2337             :       {
    2338       59982 :         GEN H = ZM_hnfmod(x,mdet);
    2339       59983 :         *ptV = ZM_gauss(x,H);
    2340       59983 :         x = H;
    2341             :       }
    2342             :       else
    2343          28 :         x = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
    2344       60368 :       mdet = ZM_det_triangular(x); /* > 0 */
    2345             :     }
    2346       60445 :     n = lg(x)-1; /* n independent columns */
    2347       60445 :     if (ptV)
    2348             :     {
    2349       60032 :       V = *ptV;
    2350       60032 :       if (n != n0)
    2351             :       {
    2352          28 :         V0 = vecslice(V, 1, n0 - n); /* kernel */
    2353          28 :         V  = vecslice(V, n0-n+1, n0);
    2354             :       }
    2355             :     }
    2356       60445 :     if (!signe(mdet))
    2357             :     {
    2358          77 :       if (n)
    2359             :       {
    2360          70 :         x = ZM_snfall_i(shallowtrans(x), ptV, ptU, return_vec); /* swap V,U */
    2361          70 :         if (!return_vec && n != m) x = shallowtrans(x);
    2362          70 :         if (ptV) V = ZM_mul(V, shallowtrans(*ptV));
    2363          70 :         if (ptU) U = *ptU; /* TRANSPOSE */
    2364             :       }
    2365             :       else /* 0 matrix */
    2366             :       {
    2367           7 :         x = cgetg(1,t_MAT);
    2368           7 :         if (ptV) V = cgetg(1, t_MAT);
    2369           7 :         if (ptU) U = matid(m);
    2370             :       }
    2371          77 :       goto THEEND;
    2372             :     }
    2373             :   }
    2374      670673 :   if (ptV || ptU) U = matid(n); /* we will compute V in terms of U */
    2375      670677 :   if (DEBUGLEVEL>7) err_printf("starting SNF loop");
    2376             : 
    2377             :   /* square, maximal rank n */
    2378      670677 :   A = x; x = shallowcopy(x); av = avma;
    2379     1584328 :   for (i = n; i > 1; i--)
    2380             :   {
    2381      913692 :     if (DEBUGLEVEL>7) err_printf("\ni = %ld: ",i);
    2382             :     for(;;)
    2383      693995 :     {
    2384     1607660 :       int c = 0;
    2385             :       GEN a, b;
    2386     4624991 :       for (j = i-1; j >= 1; j--)
    2387             :       {
    2388     3017460 :         b = gcoeff(x,i,j); if (!signe(b)) continue;
    2389      262474 :         a = gcoeff(x,i,i);
    2390      262474 :         ZC_elem(b, a, x,NULL, j,i);
    2391      262471 :         if (gc_needed(av,1))
    2392             :         {
    2393           0 :           if (DEBUGMEM>1) pari_warn(warnmem,"[1]: ZM_snfall i = %ld", i);
    2394           0 :           snf_pile1(av, &x,&U);
    2395             :         }
    2396             :       }
    2397     1607531 :       if (DEBUGLEVEL>7) err_printf("; ");
    2398     4624955 :       for (j=i-1; j>=1; j--)
    2399             :       {
    2400             :         GEN d;
    2401     3017415 :         b = gcoeff(x,j,i); if (!signe(b)) continue;
    2402      695653 :         a = gcoeff(x,i,i);
    2403      695653 :         d = bezout_step(&a, &b, &u, &v);
    2404     4172175 :         for (k = 1; k < i; k++)
    2405             :         {
    2406     3476624 :           GEN t = addii(mulii(u,gcoeff(x,i,k)),mulii(v,gcoeff(x,j,k)));
    2407     3476671 :           gcoeff(x,j,k) = subii(mulii(a,gcoeff(x,j,k)),
    2408     3476673 :                                 mulii(b,gcoeff(x,i,k)));
    2409     3476548 :           gcoeff(x,i,k) = t;
    2410             :         }
    2411      695551 :         gcoeff(x,j,i) = gen_0;
    2412      695551 :         gcoeff(x,i,i) = d;
    2413      695551 :         if (U) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
    2414      695615 :         if (gc_needed(av,1))
    2415             :         {
    2416          63 :           if (DEBUGMEM>1) pari_warn(warnmem,"[2]: ZM_snfall, i = %ld", i);
    2417          63 :           snf_pile1(av, &x,&U);
    2418             :         }
    2419      695616 :         c = 1;
    2420             :       }
    2421     1607540 :       if (!c)
    2422             :       {
    2423     1063358 :         k = ZM_snf_no_divide(x, i);
    2424     1063298 :         if (!k) break;
    2425             : 
    2426             :         /* x[k,j] != 0 mod b */
    2427      573119 :         for (j = 1; j <= i; j++)
    2428      423497 :           gcoeff(x,i,j) = addii(gcoeff(x,i,j),gcoeff(x,k,j));
    2429      149622 :         if (U) gel(U,i) = gadd(gel(U,i),gel(U,k));
    2430             :       }
    2431      693802 :       ZM_redpart(x, mdet, i);
    2432      693942 :       if (U && (flag & 2)) ZM_redpart(U, mdet, n);
    2433      693991 :       if (gc_needed(av,1))
    2434             :       {
    2435           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"[3]: ZM_snfall");
    2436           0 :         snf_pile1(av, &x,&U);
    2437             :       }
    2438             :     }
    2439             :   }
    2440      670636 :   if (DEBUGLEVEL>7) err_printf("\n");
    2441     2253999 :   for (k = n; k; k--)
    2442             :   {
    2443     1583842 :     GEN d = gcdii(gcoeff(x,k,k), mdet);
    2444     1583634 :     gcoeff(x,k,k) = d;
    2445     1583634 :     if (!is_pm1(d)) mdet = diviiexact(mdet,d);
    2446             :   }
    2447      670157 : THEEND:
    2448      670234 :   if (U) U = shallowtrans(U);
    2449      670464 :   if (ptV && A)
    2450             :   { /* U A V = D => D^(-1) U A = V^(-1) */
    2451      665595 :     long l = lg(x);
    2452      665595 :     GEN W = ZM_mul(U, A);
    2453     1526692 :     for (i = 1; i < l; i++)
    2454             :     {
    2455     1334847 :       GEN c = gcoeff(x,i,i);
    2456     1334847 :       if (is_pm1(c)) break; /* only 1 from now on */
    2457      861022 :       ZMrow_divexact_inplace(W, i, c);
    2458             :     }
    2459      665680 :     if (flag & 2)
    2460             :     {
    2461      641395 :       W = FpM_red(W, gcoeff(x,1,1));
    2462      641513 :       W = matinvmod(W, gcoeff(x,1,1));
    2463             :     }
    2464             :     else
    2465       24285 :       W = ZM_inv(W, NULL);
    2466      665658 :     V = V? ZM_mul(V, W): W;
    2467             :   }
    2468      670511 :   if (return_vec)
    2469             :   {
    2470      646575 :     long l = lg(x)-1;
    2471      646575 :     if (typ(x) == t_MAT) x = RgM_diagonal_shallow(x);
    2472      646570 :     if (m0 > l) x = shallowconcat(zerovec(m0-l), x);
    2473             :   }
    2474             : 
    2475      670506 :   if (V0)
    2476             :   { /* add kernel */
    2477          28 :     if (!return_vec) x = shallowconcat(zeromat(m,n0-n), x);
    2478          28 :     if (ptV) V = shallowconcat(V0, V);
    2479             :   }
    2480      670506 :   if (perm && U) U = vecpermute(U, perm_inv(perm));
    2481      670506 :   snf_pile(av0, &x,&U,&V);
    2482      670803 :   if (ptU) *ptU = U;
    2483      670803 :   if (ptV) *ptV = V;
    2484      670803 :   return x;
    2485             : }
    2486             : GEN
    2487       63805 : ZM_snfall(GEN x, GEN *U, GEN *V) { return ZM_snfall_i(x, U, V, 0); }
    2488             : GEN
    2489        1638 : ZM_snf(GEN x) { return ZM_snfall_i(x, NULL,NULL, 1); }
    2490             : GEN
    2491         385 : smith(GEN x) { return ZM_snfall_i(x, NULL,NULL, 1); }
    2492             : GEN
    2493          35 : smithall(GEN x)
    2494             : {
    2495          35 :   GEN z = cgetg(4, t_VEC);
    2496          35 :   gel(z,3) = ZM_snfall_i(x, (GEN*)(z+1),(GEN*)(z+2), 0);
    2497          35 :   return z;
    2498             : }
    2499             : 
    2500             : GEN
    2501      214165 : ZV_snfclean(GEN d)
    2502             : {
    2503      214165 :   long c, l = lg(d);
    2504      226526 :   for (c = 1; c < l; c++) { GEN t = gel(d,c); if (is_pm1(t)) break; }
    2505      214165 :   return c == l? d: vec_shorten(d, c-1);
    2506             : }
    2507             : void
    2508      944251 : ZM_snfclean(GEN d, GEN u, GEN v)
    2509             : {
    2510      944251 :   long i, c, l = lg(d);
    2511             : 
    2512      944251 :   if (typ(d) == t_VEC)
    2513     2424581 :     for (c=1; c<l; c++) { GEN t = gel(d,c); if (is_pm1(t)) break; }
    2514             :   else
    2515             :   {
    2516           0 :     for (c=1; c<l; c++) { GEN t = gcoeff(d,c,c); if (is_pm1(t)) break; }
    2517           0 :     if (c < l) for (i = 1; i < c; i++) setlg(gel(d,i), c);
    2518             :   }
    2519      944232 :   setlg(d, c);
    2520     2919586 :   if (u) for (i=1; i<l; i++) setlg(gel(u,i), c);
    2521      944201 :   if (v) setlg(v, c);
    2522      944199 : }
    2523             : 
    2524             : /* Assume z was computed by [g]smithall(). Remove the 1s on the diagonal */
    2525             : GEN
    2526         693 : smithclean(GEN z)
    2527             : {
    2528             :   long i, j, h, l, c, d;
    2529             :   GEN U, V, y, D, t;
    2530             : 
    2531         693 :   if (typ(z) != t_VEC) pari_err_TYPE("smithclean",z);
    2532         693 :   l = lg(z); if (l == 1) return cgetg(1,t_VEC);
    2533         686 :   U = gel(z,1);
    2534         686 :   if (l != 4 || typ(U) != t_MAT)
    2535             :   { /* assume z = vector of elementary divisors */
    2536        1827 :     for (c=1; c<l; c++)
    2537        1519 :       if (gequal1(gel(z,c))) break;
    2538         665 :     return gcopy_lg(z, c);
    2539             :   }
    2540          21 :   V = gel(z,2);
    2541          21 :   D = gel(z,3);
    2542          21 :   l = lg(D);
    2543          21 :   if (l == 1) return gcopy(z);
    2544          21 :   h = lgcols(D);
    2545          21 :   if (h > l)
    2546             :   { /* D = vconcat(zero matrix, diagonal matrix) */
    2547          21 :     for (c=1+h-l, d=1; c<h; c++,d++)
    2548          21 :       if (gequal1(gcoeff(D,c,d))) break;
    2549             :   }
    2550           7 :   else if (h < l)
    2551             :   { /* D = concat(zero matrix, diagonal matrix) */
    2552           7 :     for (c=1, d=1+l-h; d<l; c++,d++)
    2553           7 :       if (gequal1(gcoeff(D,c,d))) break;
    2554             :   }
    2555             :   else
    2556             :   { /* D diagonal */
    2557           0 :     for (c=1; c<l; c++)
    2558           0 :       if (gequal1(gcoeff(D,c,c))) break;
    2559           0 :     d = c;
    2560             :   }
    2561             :   /* U was (h-1)x(h-1), V was (l-1)x(l-1), D was (h-1)x(l-1) */
    2562          21 :   y = cgetg(4,t_VEC);
    2563             :   /* truncate U to (c-1) x (h-1) */
    2564          21 :   gel(y,1) = t = cgetg(h,t_MAT);
    2565          77 :   for (j=1; j<h; j++) gel(t,j) = gcopy_lg(gel(U,j), c);
    2566             :   /* truncate V to (l-1) x (d-1) */
    2567          21 :   gel(y,2) = gcopy_lg(V, d);
    2568          21 :   gel(y,3) = t = zeromatcopy(c-1, d-1);
    2569             :   /* truncate D to a (c-1) x (d-1) matrix */
    2570          21 :   if (d > 1)
    2571             :   {
    2572          14 :     if (h > l)
    2573             :     {
    2574          14 :       for (i=1+h-l, j=1; i<c; i++,j++)
    2575           7 :         gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
    2576             :     }
    2577           7 :     else if (h < l)
    2578             :     {
    2579           7 :       for (i=1, j=1+l-h; j<d; i++,j++)
    2580           0 :         gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
    2581             :     }
    2582             :     else
    2583             :     {
    2584           0 :       for (j=1; j<d; j++)
    2585           0 :         gcoeff(t,j,j) = gcopy(gcoeff(D,j,j));
    2586             :     }
    2587             :   }
    2588          21 :   return y;
    2589             : }
    2590             : 
    2591             : /* does b = x[i,i] divide all entries in x[1..i-1,1..i-1] ? If so, return 0;
    2592             :  * else return the index of a problematic row */
    2593             : static long
    2594         196 : gsnf_no_divide(GEN x, long i, long vx)
    2595             : {
    2596         196 :   GEN b = gcoeff(x,i,i);
    2597             :   long j, k;
    2598             : 
    2599         196 :   if (gequal0(b))
    2600             :   {
    2601          14 :     for (k = 1; k < i; k++)
    2602          14 :       for (j = 1; j < i; j++)
    2603          14 :         if (!gequal0(gcoeff(x,k,j))) return k;
    2604           0 :     return 0;
    2605             :   }
    2606             : 
    2607         182 :   if (!is_RgX(b,vx) || degpol(b)<=0) return 0;
    2608         217 :   for (k = 1; k < i; k++)
    2609         378 :     for (j = 1; j < i; j++)
    2610             :     {
    2611         266 :       GEN z = gcoeff(x,k,j), r;
    2612         266 :       if (!is_RgX(z,vx)) z = scalarpol(z, vx);
    2613         266 :       r = RgX_rem(z, b);
    2614         266 :       if (signe(r) && (! isinexactreal(r) ||
    2615           0 :              gexpo(r) > 16 + gexpo(b) - gprecision(r))
    2616          35 :          ) return k;
    2617             :     }
    2618          70 :   return 0;
    2619             : }
    2620             : 
    2621             : /* Hermite Normal Form, with base change matrix if ptB != NULL.
    2622             :  * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
    2623             :  * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
    2624             : GEN
    2625          42 : RgM_hnfall(GEN A, GEN *pB, long remove)
    2626             : {
    2627             :   pari_sp av;
    2628             :   long li, j, k, m, n, def, ldef;
    2629             :   GEN B;
    2630          42 :   long vx = gvar(A);
    2631             : 
    2632          42 :   n = lg(A)-1;
    2633          42 :   if (vx==NO_VARIABLE || !n)
    2634             :   {
    2635           0 :     RgM_check_ZM(A, "mathnf0");
    2636           0 :     return ZM_hnfall(A, pB, remove);
    2637             :   }
    2638          42 :   m = nbrows(A);
    2639          42 :   av = avma;
    2640          42 :   A = RgM_shallowcopy(A);
    2641          42 :   B = pB? matid(n): NULL;
    2642          42 :   def = n; ldef = (m>n)? m-n: 0;
    2643         126 :   for (li=m; li>ldef; li--)
    2644             :   {
    2645             :     GEN d, T;
    2646         714 :     for (j=def-1; j; j--)
    2647             :     {
    2648         630 :       GEN a = gcoeff(A,li,j);
    2649         630 :       if (gequal0(a)) continue;
    2650             : 
    2651         511 :       k = (j==1)? def: j-1;
    2652         511 :       RgC_elem(a,gcoeff(A,li,k), A,B, j,k, li, vx);
    2653             :     }
    2654          84 :     T = normalize_as_RgX(gcoeff(A,li,def), vx, &d);
    2655          84 :     if (gequal0(T))
    2656           0 :     { if (ldef) ldef--; }
    2657             :     else
    2658             :     {
    2659          84 :       if (!gequal1(d))
    2660             :       {
    2661          49 :         gel(A,def) = RgC_Rg_div(gel(A,def), d);
    2662          49 :         if (B) gel(B, def) = RgC_Rg_div(gel(B, def), d);
    2663             :       }
    2664          84 :       RgM_reduce(A, B, li, def, vx);
    2665          84 :       def--;
    2666             :     }
    2667          84 :     if (gc_needed(av,1))
    2668             :     {
    2669           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ghnfall");
    2670           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2671             :     }
    2672             :   }
    2673             :   /* rank A = n - def */
    2674          42 :   if (remove) remove_0cols(def, &A, &B, remove);
    2675          42 :   gerepileall(av, B? 2: 1, &A, &B);
    2676          42 :   if (B) *pB = B;
    2677          42 :   return A;
    2678             : }
    2679             : 
    2680             : static GEN
    2681          49 : RgXM_snf(GEN x,long all)
    2682             : {
    2683             :   pari_sp av;
    2684             :   long i, j, k, n;
    2685             :   GEN z, u, v, U, V;
    2686          49 :   long vx = gvar(x);
    2687          49 :   n = lg(x)-1; if (!n) return trivsmith(all);
    2688          49 :   if (vx==NO_VARIABLE) pari_err_TYPE("RgXM_snf",x);
    2689          49 :   if (lgcols(x) != n+1) pari_err_DIM("gsmithall");
    2690          49 :   av = avma;
    2691          49 :   x = RgM_shallowcopy(x);
    2692          49 :   if (all) { U = matid(n); V = matid(n); }
    2693         196 :   for (i=n; i>=2; i--)
    2694             :   {
    2695             :     for(;;)
    2696         168 :     {
    2697             :       GEN a, b, d;
    2698         315 :       int c = 0;
    2699        1092 :       for (j=i-1; j>=1; j--)
    2700             :       {
    2701         777 :         b = gcoeff(x,i,j); if (gequal0(b)) continue;
    2702         196 :         a = gcoeff(x,i,i);
    2703         196 :         d = gbezout_step(&b, &a, &v, &u, vx);
    2704         700 :         for (k = 1; k < i; k++)
    2705             :         {
    2706         504 :           GEN t = gadd(gmul(u,gcoeff(x,k,i)),gmul(v,gcoeff(x,k,j)));
    2707         504 :           gcoeff(x,k,j) = gsub(gmul(a,gcoeff(x,k,j)),gmul(b,gcoeff(x,k,i)));
    2708         504 :           gcoeff(x,k,i) = t;
    2709             :         }
    2710         196 :         gcoeff(x,i,j) = gen_0;
    2711         196 :         gcoeff(x,i,i) = d;
    2712         196 :         if (all) update(u,v,a,b,(GEN*)(V+i),(GEN*)(V+j));
    2713             :       }
    2714        1092 :       for (j=i-1; j>=1; j--)
    2715             :       {
    2716         777 :         b = gcoeff(x,j,i); if (gequal0(b)) continue;
    2717         175 :         a = gcoeff(x,i,i);
    2718         175 :         d = gbezout_step(&b, &a, &v, &u, vx);
    2719         651 :         for (k = 1; k < i; k++)
    2720             :         {
    2721         476 :           GEN t = gadd(gmul(u,gcoeff(x,i,k)),gmul(v,gcoeff(x,j,k)));
    2722         476 :           gcoeff(x,j,k) = gsub(gmul(a,gcoeff(x,j,k)),gmul(b,gcoeff(x,i,k)));
    2723         476 :           gcoeff(x,i,k) = t;
    2724             :         }
    2725         175 :         gcoeff(x,j,i) = gen_0;
    2726         175 :         gcoeff(x,i,i) = d;
    2727         175 :         if (all) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
    2728         175 :         c = 1;
    2729             :       }
    2730         315 :       if (!c)
    2731             :       {
    2732         196 :         k = gsnf_no_divide(x, i, vx);
    2733         196 :         if (!k) break;
    2734             : 
    2735         245 :         for (j=1; j<=i; j++)
    2736         196 :           gcoeff(x,i,j) = gadd(gcoeff(x,i,j),gcoeff(x,k,j));
    2737          49 :         if (all) gel(U,i) = gadd(gel(U,i),gel(U,k));
    2738             :       }
    2739         168 :       if (gc_needed(av,1))
    2740             :       {
    2741           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"gsmithall");
    2742           0 :         gerepileall(av, all? 3: 1, &x, &U, &V);
    2743             :       }
    2744             :     }
    2745             :   }
    2746         245 :   for (k=1; k<=n; k++)
    2747             :   {
    2748         196 :     GEN d, T = normalize_as_RgX(gcoeff(x,k,k), vx, &d);
    2749         196 :     if (gequal0(T)) continue;
    2750         182 :     if (all && !gequal1(d)) gel(V,k) = RgC_Rg_div(gel(V,k), d);
    2751         182 :     gcoeff(x,k,k) = T;
    2752             :   }
    2753          49 :   z = all? mkvec3(shallowtrans(U), V, x): RgM_diagonal_shallow(x);
    2754          49 :   return gerepilecopy(av, z);
    2755             : }
    2756             : 
    2757             : GEN
    2758         469 : matsnf0(GEN x,long flag)
    2759             : {
    2760         469 :   pari_sp av = avma;
    2761         469 :   if (flag > 7) pari_err_FLAG("matsnf");
    2762         469 :   if (typ(x) == t_VEC && flag & 4) return smithclean(x);
    2763         469 :   if (typ(x)!=t_MAT) pari_err_TYPE("matsnf",x);
    2764         469 :   if (RgM_is_ZM(x)) x = flag&1 ? smithall(x): smith(x);
    2765          49 :   else              x = RgXM_snf(x, flag&1);
    2766         469 :   if (flag & 4) x = gerepileupto(av, smithclean(x));
    2767         469 :   return x;
    2768             : }
    2769             : GEN
    2770           0 : gsmith(GEN x) { return RgXM_snf(x,0); }
    2771             : GEN
    2772           0 : gsmithall(GEN x) { return RgXM_snf(x,1); }
    2773             : 
    2774             : /* H is a relation matrix, either in HNF or a t_VEC (diagonal HNF) */
    2775             : static GEN
    2776      944260 : snf_group(GEN H, GEN D, GEN *newU, GEN *newUi)
    2777             : {
    2778             :   long i, j, l;
    2779             : 
    2780      944260 :   ZM_snfclean(D, newU? *newU: NULL, newUi? *newUi: NULL);
    2781      944188 :   l = lg(D);
    2782      944188 :   if (newU) {
    2783      822464 :     GEN U = *newU;
    2784     2122265 :     for (i = 1; i < l; i++)
    2785             :     {
    2786     1300375 :       GEN d = gel(D,i), d2 = shifti(d, 1);
    2787     5083982 :       for (j = 1; j < lg(U); j++)
    2788     3784181 :         gcoeff(U,i,j) = centermodii(gcoeff(U,i,j), d, d2);
    2789             :     }
    2790      821890 :     *newU = U;
    2791             :   }
    2792      943614 :   if (newUi && l > 1)
    2793             :   { /* UHV=D -> U^-1 = (HV)D^-1 -> U^-1 = H(VD^-1 mod 1) mod H */
    2794             :     /* Ui = ZM_inv(U, NULL); setlg(Ui, l); */
    2795      856626 :     GEN V = *newUi, Ui;
    2796      856626 :     int Hvec = (typ(H) == t_VEC);
    2797     2335828 :     for (i = 1; i < l; i++) gel(V,i) = FpC_red(gel(V,i), gel(D,i));
    2798      856641 :     if (!Hvec)
    2799             :     {
    2800      555703 :       if (ZM_isdiagonal(H)) { H = RgM_diagonal_shallow(H); Hvec = 1; }
    2801             :     }
    2802      856823 :     Ui = Hvec? ZM_diag_mul(H, V): ZM_mul(H, V);
    2803     2335681 :     for (i = 1; i < l; i++) gel(Ui,i) = ZC_Z_divexact(gel(Ui,i), gel(D,i));
    2804      856548 :     *newUi = Hvec? ZM_ZV_mod(Ui, H): ZM_hnfrem(Ui, H);
    2805             :   }
    2806      943743 :   return D;
    2807             : }
    2808             : /* H relation matrix among row of generators g in HNF.  Let URV = D its SNF,
    2809             :  * newU R newV = newD its clean SNF (no 1 in Dnew). Return the diagonal of
    2810             :  * newD, newU and newUi such that  1/U = (newUi, ?).
    2811             :  * Rationale: let (G,0) = g Ui be the new generators then
    2812             :  * 0 = G U R --> G D = 0,  g = G newU  and  G = g newUi */
    2813             : GEN
    2814      642821 : ZM_snf_group(GEN H, GEN *newU, GEN *newUi)
    2815             : {
    2816      642821 :   GEN D = ZM_snfall_i(H, newU, newUi, 1 + 2);
    2817      642914 :   return snf_group(H, D, newU, newUi);
    2818             : }
    2819             : 
    2820             : /* D a ZV: SNF for matdiagonal(D). Faster because we only ensure elementary
    2821             :  * divisors condition: d[n] | ... | d[1] and need not convert D to matrix form*/
    2822             : GEN
    2823      301370 : ZV_snfall(GEN D, GEN *pU, GEN *pV)
    2824             : {
    2825      301370 :   pari_sp av = avma;
    2826      301370 :   long j, n = lg(D)-1;
    2827      301370 :   GEN U = pU? matid(n): NULL;
    2828      301369 :   GEN V = pV? matid(n): NULL;
    2829             :   GEN p;
    2830             : 
    2831      301366 :   D = leafcopy(D);
    2832      961418 :   for (j = n; j > 0; j--)
    2833             :   {
    2834      660052 :     GEN b = gel(D,j);
    2835      660052 :     if (signe(b) < 0)
    2836             :     {
    2837           0 :       gel(D,j) = negi(b);
    2838           0 :       if (V) ZV_togglesign(gel(V,j));
    2839             :     }
    2840             :   }
    2841             :   /* entries are nonnegative integers */
    2842      301366 :   p = gen_indexsort(D, NULL, &negcmpii);
    2843      301360 :   D = vecpermute(D, p);
    2844      301360 :   if (U) U = vecpermute(U, p);
    2845      301360 :   if (V) V = vecpermute(V, p);
    2846             :   /* entries are sorted by decreasing value */
    2847      961369 :   for (j = n; j > 0; j--)
    2848             :   {
    2849      660001 :     GEN b = gel(D,j);
    2850             :     long i;
    2851     1181238 :     for (i = j-1; i > 0; i--)
    2852             :     { /* invariant: a >= b. If au+bv = d is a Bezout relation, A=a/d and B=b/d
    2853             :        * we have [B,-A;u,v]*diag(a,b)*[1-u*A,1; -u*A,1]] = diag(Ab, d) */
    2854      533410 :       GEN a = gel(D,i), u,v, d = bezout(a,b, &u,&v), A, Wi, Wj;
    2855      533403 :       if (equalii(d,b)) continue;
    2856       70326 :       A = diviiexact(a,d);
    2857       70325 :       if (V)
    2858             :       {
    2859       70269 :         GEN t = mulii(u,A);
    2860       70268 :         Wi = ZC_lincomb(subui(1,t), negi(t), gel(V,i), gel(V,j));
    2861       70269 :         Wj = ZC_add(gel(V,i), gel(V,j));
    2862       70270 :         gel(V,i) = Wi;
    2863       70270 :         gel(V,j) = Wj;
    2864             :       }
    2865       70326 :       if (U)
    2866             :       {
    2867       70326 :         GEN B = diviiexact(b,d);
    2868       70325 :         Wi = ZC_lincomb(B, negi(A), gel(U,i), gel(U,j));
    2869       70324 :         Wj = ZC_lincomb(u, v, gel(U,i), gel(U,j));
    2870       70325 :         gel(U,i) = Wi;
    2871       70325 :         gel(U,j) = Wj;
    2872             :       }
    2873       70325 :       gel(D,i) = mulii(A,b); /* lcm(a,b) */
    2874       70323 :       gel(D,j) = d; /* gcd(a,b) */
    2875       70323 :       b = gel(D,j); if (equali1(b)) break;
    2876             :     }
    2877             :   }
    2878      301368 :   snf_pile(av, &D,&U,&V);
    2879      301376 :   if (U) *pU = shallowtrans(U);
    2880      301371 :   if (V) *pV = V;
    2881      301371 :   return D;
    2882             : }
    2883             : GEN
    2884      301370 : ZV_snf_group(GEN d, GEN *newU, GEN *newUi)
    2885             : {
    2886      301370 :   GEN D = ZV_snfall(d, newU, newUi);
    2887      301371 :   return snf_group(d, D, newU, newUi);
    2888             : }
    2889             : 
    2890             : /* D a vector of elementary divisors. Truncate (setlg) to leave out trivial
    2891             :  * entries (= 1) */
    2892             : void
    2893           0 : ZV_snf_trunc(GEN D)
    2894             : {
    2895           0 :   long i, l = lg(D);
    2896           0 :   for (i = 1; i < l; i++)
    2897           0 :     if (is_pm1(gel(D,i))) { setlg(D,i); break; }
    2898           0 : }
    2899             : 
    2900             : long
    2901           0 : zv_snf_rank(GEN D, ulong p)
    2902             : {
    2903           0 :   long i, l = lg(D);
    2904           0 :   if (!p) return l - 1;
    2905           0 :   for (i = 1; i < l; i++)
    2906           0 :     if (D[i] % p) break;
    2907           0 :   return i - 1;
    2908             : }
    2909             : long
    2910          49 : ZV_snf_rank_u(GEN D, ulong p)
    2911             : {
    2912          49 :   long i, l = lg(D);
    2913          49 :   while (l > 1 && D[l-1] == 1) l--;
    2914          49 :   if (!p) return l - 1;
    2915          49 :   if (p == 2)
    2916             :   {
    2917          49 :     for (i = 1; i < l; i++)
    2918          42 :       if (mpodd(gel(D,i))) break;
    2919             :   }
    2920          35 :   else if (!(p & (p-1)))
    2921             :   { /* power of 2 */
    2922          14 :     long n = vals(p);
    2923          28 :     for (i = 1; i < l; i++)
    2924          28 :       if (umodi2n(gel(D,i), n)) break;
    2925             :   }
    2926             :   else
    2927             :   {
    2928          49 :     for (i = 1; i < l; i++)
    2929          42 :       if (umodiu(gel(D,i), p)) break;
    2930             :   }
    2931          49 :   return i - 1;
    2932             : }
    2933             : long
    2934          91 : ZV_snf_rank(GEN D, GEN p)
    2935             : {
    2936          91 :   long i, l = lg(D);
    2937          91 :   if (lgefint(p) == 3) return ZV_snf_rank_u(D, p[2]);
    2938          77 :   while (l > 1 && equali1(gel(D, l-1))) l--;
    2939          42 :   if (!signe(p)) return l - 1;
    2940          77 :   for (i = 1; i < l; i++)
    2941          70 :     if (!dvdii(gel(D,i), p)) break;
    2942          21 :   return i - 1;
    2943             : }
    2944             : long
    2945         154 : snfrank(GEN D, GEN p)
    2946             : {
    2947             :   long i, l;
    2948         154 :   if (typ(D) != t_VEC) pari_err_TYPE("snfrank", D);
    2949         154 :   if (!p) p = gen_0;
    2950         154 :   l = lg(D);
    2951         154 :   if (l == 4 && typ(gel(D,3)) == t_MAT)
    2952             :   { /* from matsnf(,1) */
    2953          14 :     pari_sp av = avma;
    2954             :     long z;
    2955             :     GEN v;
    2956          14 :     D = gel(D,3); l = lg(D);
    2957          14 :     if (l == 1) return 0;
    2958          14 :     z = lgcols(D) - l; /* missing columns of 0s */
    2959          14 :     if (z < 0) pari_err_TYPE("snfrank", D);
    2960          14 :     v = cgetg(l, t_VEC);
    2961          35 :     for (i = 1; i < l; i++) gel(v, i) = gcoeff(D, i + z, i);
    2962          14 :     return gc_long(av, z + snfrank(v, p));
    2963             :   }
    2964         140 :   switch(typ(p))
    2965             :   {
    2966          98 :     case t_INT:
    2967          98 :       if (RgV_is_ZV(D)) return ZV_snf_rank(D, p);
    2968           7 :       if (!signe(p)) break; /* allow p = 0 */
    2969           0 :       pari_err_TYPE("snfrank", D);
    2970          42 :     case t_POL: break;
    2971           0 :     default: pari_err_TYPE("snfrank", p);
    2972             :   }
    2973         175 :   while (l > 1 && gequal1(gel(D, l-1))) l--;
    2974          49 :   if (gequal0(p)) return l - 1;
    2975         112 :   for (i = 1; i < l; i++)
    2976          91 :     if (!gdvd(gel(D,i), p)) break;
    2977          42 :   return i - 1;
    2978             : }

Generated by: LCOV version 1.14