Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - hnf_snf.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20838-243a3c2) Lines: 1490 1637 91.0 %
Date: 2017-07-28 06:22:09 Functions: 84 89 94.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : /**************************************************************/
      17             : /**                                                          **/
      18             : /**                HERMITE NORMAL FORM REDUCTION             **/
      19             : /**                                                          **/
      20             : /**************************************************************/
      21             : static GEN ZV_hnfgcdext(GEN A);
      22             : static GEN
      23           7 : hnfallgen(GEN x)
      24             : {
      25           7 :   GEN z = cgetg(3, t_VEC);
      26           7 :   gel(z,1) = RgM_hnfall(x, (GEN*)(z+2), 1);
      27           7 :   return z;
      28             : }
      29             : GEN
      30         154 : mathnf0(GEN x, long flag)
      31             : {
      32         154 :   switch(typ(x))
      33             :   {
      34             :     case t_VEC:
      35          70 :       if (RgV_is_ZV(x))
      36          42 :         switch (flag)
      37             :         {
      38             :           case 0:
      39          14 :             if (lg(x) == 1) return cgetg(1, t_MAT);
      40           7 :             retmkmat(mkcol(ZV_content(x)));
      41             :           case 1:
      42             :           case 4:
      43          21 :             return ZV_hnfgcdext(x);
      44             :         }
      45          35 :       x = gtomat(x); break;
      46          84 :     case t_MAT: break;
      47           0 :     default: pari_err_TYPE("mathnf0",x);
      48             :   }
      49             : 
      50         119 :   switch(flag)
      51             :   {
      52          77 :     case 0: case 2: return RgM_is_ZM(x)? ZM_hnf(x): RgM_hnfall(x,NULL,1);
      53          21 :     case 1: case 3: return RgM_is_ZM(x)? hnfall(x): hnfallgen(x);
      54           7 :     case 4: RgM_check_ZM(x, "mathnf0"); return hnflll(x);
      55          14 :     case 5: RgM_check_ZM(x, "mathnf0"); return hnfperm(x);
      56           0 :     default: pari_err_FLAG("mathnf");
      57             :   }
      58             :   return NULL; /* LCOV_EXCL_LINE */
      59             : }
      60             : 
      61             : /*******************************************************************/
      62             : /*                                                                 */
      63             : /*                SPECIAL HNF (FOR INTERNAL USE !!!)               */
      64             : /*                                                                 */
      65             : /*******************************************************************/
      66             : static int
      67     3093865 : count(GEN mat, long row, long len, long *firstnonzero)
      68             : {
      69     3093865 :   long j, n = 0;
      70             : 
      71   211783607 :   for (j=1; j<=len; j++)
      72             :   {
      73   209370719 :     long p = mael(mat,j,row);
      74   209370719 :     if (p)
      75             :     {
      76     8303315 :       if (labs(p)!=1) return -1;
      77     7622338 :       n++; *firstnonzero=j;
      78             :     }
      79             :   }
      80     2412888 :   return n;
      81             : }
      82             : 
      83             : static int
      84      103842 : count2(GEN mat, long row, long len)
      85             : {
      86             :   long j;
      87      953548 :   for (j=len; j; j--)
      88      920115 :     if (labs(mael(mat,j,row)) == 1) return j;
      89       33433 :   return 0;
      90             : }
      91             : 
      92             : static GEN
      93       42097 : hnffinal(GEN matgen,GEN perm,GEN* ptdep,GEN* ptB,GEN* ptC)
      94             : {
      95             :   GEN p1,p2,U,H,Hnew,Bnew,Cnew,diagH1;
      96       42097 :   GEN B = *ptB, C = *ptC, dep = *ptdep, depnew;
      97             :   pari_sp av;
      98             :   long i,j,k,s,i1,j1,zc;
      99       42097 :   long co = lg(C);
     100       42097 :   long col = lg(matgen)-1;
     101             :   long lnz, nlze, lig;
     102             : 
     103       42097 :   if (col == 0) return matgen;
     104       41740 :   lnz = nbrows(matgen); /* was called lnz-1 - nr in hnfspec */
     105       41740 :   nlze = nbrows(dep);
     106       41740 :   lig = nlze + lnz;
     107             :   /* H: lnz x lnz [disregarding initial 0 cols], U: col x col */
     108       41740 :   H = ZM_hnflll(matgen, &U, 0);
     109       41740 :   H += (lg(H)-1 - lnz); H[0] = evaltyp(t_MAT) | evallg(lnz+1);
     110             :   /* Only keep the part above the H (above the 0s is 0 since the dep rows
     111             :    * are dependent from the ones in matgen) */
     112       41740 :   zc = col - lnz; /* # of 0 columns, correspond to units */
     113       41740 :   if (nlze) { dep = ZM_mul(dep,U); dep += zc; }
     114             : 
     115       41740 :   diagH1 = new_chunk(lnz+1); /* diagH1[i] = 0 iff H[i,i] != 1 (set later) */
     116             : 
     117       41740 :   av = avma;
     118       41740 :   Cnew = cgetg(co, typ(C));
     119       41740 :   setlg(C, col+1); p1 = gmul(C,U);
     120       41740 :   for (j=1; j<=col; j++) gel(Cnew,j) = gel(p1,j);
     121       41740 :   for (   ; j<co ; j++)  gel(Cnew,j) = gel(C,j);
     122             : 
     123             :   /* Clean up B using new H */
     124      198603 :   for (s=0,i=lnz; i; i--)
     125             :   {
     126      156863 :     GEN Di = gel(dep,i), Hi = gel(H,i);
     127      156863 :     GEN h = gel(Hi,i); /* H[i,i] */
     128      156863 :     if ( (diagH1[i] = is_pm1(h)) ) { h = NULL; s++; }
     129     7546505 :     for (j=col+1; j<co; j++)
     130             :     {
     131     7389642 :       GEN z = gel(B,j-col);
     132     7389642 :       p1 = gel(z,i+nlze);
     133     7389642 :       if (h) p1 = truedivii(p1,h);
     134     7389642 :       if (!signe(p1)) continue;
     135     4390848 :       for (k=1; k<=nlze; k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Di,k)));
     136     4390848 :       for (   ; k<=lig;  k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Hi,k-nlze)));
     137     4390848 :       gel(Cnew,j) = gsub(gel(Cnew,j), gmul(p1, gel(Cnew,i+zc)));
     138             :     }
     139      156863 :     if (gc_needed(av,3))
     140             :     {
     141          47 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnffinal, i = %ld",i);
     142          47 :       gerepileall(av, 2, &Cnew, &B);
     143             :     }
     144             :   }
     145       41740 :   p1 = cgetg(lnz+1,t_VEC); p2 = perm + nlze;
     146      198603 :   for (i1=0, j1=lnz-s, i=1; i<=lnz; i++) /* push the 1 rows down */
     147      156863 :     if (diagH1[i])
     148       74344 :       gel(p1,++j1) = gel(p2,i);
     149             :     else
     150       82519 :       gel(p2,++i1) = gel(p2,i);
     151       41740 :   for (i=i1+1; i<=lnz; i++) gel(p2,i) = gel(p1,i);
     152             : 
     153             :   /* s = # extra redundant generators taken from H
     154             :    *          zc  col-s  co   zc = col - lnz
     155             :    *       [ 0 |dep |     ]    i = nlze + lnz - s = lig - s
     156             :    *  nlze [--------|  B' ]
     157             :    *       [ 0 | H' |     ]    H' = H minus the s rows with a 1 on diagonal
     158             :    *     i [--------|-----] lig-s           (= "1-rows")
     159             :    *       [   0    | Id  ]
     160             :    *       [        |     ] li */
     161       41740 :   lig -= s; col -= s; lnz -= s;
     162       41740 :   Hnew = cgetg(lnz+1,t_MAT);
     163       41740 :   depnew = cgetg(lnz+1,t_MAT); /* only used if nlze > 0 */
     164       41740 :   Bnew = cgetg(co-col,t_MAT);
     165       41740 :   C = shallowcopy(Cnew);
     166      198603 :   for (j=1,i1=j1=0; j<=lnz+s; j++)
     167             :   {
     168      156863 :     GEN z = gel(H,j);
     169      156863 :     if (diagH1[j])
     170             :     { /* hit exactly s times */
     171       74344 :       i1++; C[i1+col] = Cnew[j+zc];
     172       74344 :       p1 = cgetg(lig+1,t_COL); gel(Bnew,i1) = p1;
     173       74344 :       for (i=1; i<=nlze; i++) gel(p1,i) = gcoeff(dep,i,j);
     174       74344 :       p1 += nlze;
     175             :     }
     176             :     else
     177             :     {
     178       82519 :       j1++; C[j1+zc] = Cnew[j+zc];
     179       82519 :       p1 = cgetg(lnz+1,t_COL); gel(Hnew,j1) = p1;
     180       82519 :       depnew[j1] = dep[j];
     181             :     }
     182      785550 :     for (i=k=1; k<=lnz; i++)
     183      628687 :       if (!diagH1[i]) p1[k++] = z[i];
     184             :   }
     185     1266696 :   for (j=s+1; j<co-col; j++)
     186             :   {
     187     1224956 :     GEN z = gel(B,j-s);
     188     1224956 :     p1 = cgetg(lig+1,t_COL); gel(Bnew,j) = p1;
     189     1224956 :     for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
     190     1224956 :     z += nlze; p1 += nlze;
     191     5730945 :     for (i=k=1; k<=lnz; i++)
     192     4505989 :       if (!diagH1[i]) gel(p1,k++) = gel(z,i);
     193             :   }
     194       41740 :   *ptdep = depnew;
     195       41740 :   *ptC = C;
     196       41740 :   *ptB = Bnew; return Hnew;
     197             : }
     198             : 
     199             : /* for debugging */
     200             : static void
     201           0 : p_mat(GEN mat, GEN perm, long k)
     202             : {
     203           0 :   pari_sp av = avma;
     204           0 :   perm = vecslice(perm, k+1, lg(perm)-1);
     205           0 :   err_printf("Permutation: %Ps\n",perm);
     206           0 :   if (DEBUGLEVEL > 6)
     207           0 :     err_printf("matgen = %Ps\n", zm_to_ZM( rowpermute(mat, perm) ));
     208           0 :   avma = av;
     209           0 : }
     210             : 
     211             : static GEN
     212      637961 : col_dup(long l, GEN col)
     213             : {
     214      637961 :   GEN c = new_chunk(l);
     215      637961 :   memcpy(c,col,l * sizeof(long)); return c;
     216             : }
     217             : 
     218             : /* permutation giving imagecompl(x') | image(x'), x' = transpose of x */
     219             : static GEN
     220       41740 : ZM_rowrankprofile(GEN x, long *nlze)
     221             : {
     222       41740 :   pari_sp av = avma;
     223             :   GEN d, y;
     224             :   long i, j, k, l, r;
     225             : 
     226       41740 :   x = shallowtrans(x); l = lg(x);
     227       41740 :   (void)new_chunk(l); /* HACK */
     228       41740 :   d = ZM_pivots(x,&r); avma = av;
     229       41740 :   *nlze = r;
     230       41740 :   if (!d) return identity_perm(l-1);
     231       38870 :   y = cgetg(l,t_VECSMALL);
     232      252967 :   for (i = j = 1, k = r+1; i<l; i++)
     233      214097 :     if (d[i]) y[k++] = i; else y[j++] = i;
     234       38870 :   return y;
     235             : }
     236             : 
     237             : /* HNF reduce a relation matrix (column operations + row permutation)
     238             : ** Input:
     239             : **   mat = (li-1) x (co-1) matrix of long
     240             : **   C   = r x (co-1) matrix of GEN
     241             : **   perm= permutation vector (length li-1), indexing the rows of mat: easier
     242             : **     to maintain perm than to copy rows. For columns we can do it directly
     243             : **     using e.g. swap(mat[i], mat[j])
     244             : **   k0 = integer. The k0 first lines of mat are dense, the others are sparse.
     245             : ** Output: cf ASCII art in the function body
     246             : **
     247             : ** row permutations applied to perm
     248             : ** column operations applied to C. IN PLACE
     249             : **/
     250             : GEN
     251       24861 : hnfspec_i(GEN mat0, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, long k0)
     252             : {
     253             :   pari_sp av;
     254             :   long co, n, s, nlze, lnz, nr, i, j, k, lk0, col, lig, *p;
     255             :   GEN mat;
     256             :   GEN p1, p2, matb, matbnew, vmax, matt, T, extramat, B, C, H, dep, permpro;
     257       24861 :   const long li = lg(perm); /* = lgcols(mat0) */
     258       24861 :   const long CO = lg(mat0);
     259             : 
     260       24861 :   n = 0; /* -Wall */
     261             : 
     262       24861 :   C = *ptC; co = CO;
     263       24861 :   if (co > 300 && co > 1.5 * li)
     264             :   { /* treat the rest at the end */
     265           0 :     co = (long)(1.2 * li);
     266           0 :     setlg(C, co);
     267             :   }
     268             : 
     269       24861 :   if (DEBUGLEVEL>5)
     270             :   {
     271           0 :     err_printf("Entering hnfspec\n");
     272           0 :     p_mat(mat0,perm,0);
     273             :   }
     274       24861 :   matt = cgetg(co, t_MAT); /* dense part of mat (top) */
     275       24861 :   mat = cgetg(co, t_MAT);
     276      662822 :   for (j = 1; j < co; j++)
     277             :   {
     278      637961 :     GEN matj = col_dup(li, gel(mat0,j));
     279      637961 :     p1 = cgetg(k0+1,t_COL); gel(matt,j) = p1; gel(mat,j) = matj;
     280      637961 :     for (i=1; i<=k0; i++) gel(p1,i) = stoi(matj[perm[i]]);
     281             :   }
     282       24861 :   av = avma;
     283             : 
     284       24861 :   i = lig = li-1; col = co-1; lk0 = k0;
     285       24861 :   T = (k0 || (lg(C) > 1 && lgcols(C) > 1))? matid(col): NULL;
     286             :   /* Look for lines with a single non-0 entry, equal to 1 in absolute value */
     287     2414335 :   while (i > lk0 && col)
     288     2364613 :     switch( count(mat,perm[i],col,&n) )
     289             :     {
     290             :       case 0: /* move zero lines between k0+1 and lk0 */
     291       17349 :         lk0++; lswap(perm[i], perm[lk0]);
     292       17349 :         i = lig; continue;
     293             : 
     294             :       case 1: /* move trivial generator between lig+1 and li */
     295      131157 :         lswap(perm[i], perm[lig]);
     296      131157 :         if (T) swap(gel(T,n), gel(T,col));
     297      131157 :         swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     298      131157 :         if (p[perm[lig]] < 0) /* = -1 */
     299             :         { /* convert relation -g = 0 to g = 0 */
     300      113160 :           for (i=lk0+1; i<lig; i++) p[perm[i]] = -p[perm[i]];
     301      113160 :           if (T)
     302             :           {
     303      113160 :             p1 = gel(T,col);
     304     5850676 :             for (i=1; ; i++) /* T is a permuted identity: single non-0 entry */
     305     5850676 :               if (signe(gel(p1,i))) { togglesign_safe(&gel(p1,i)); break; }
     306     5737516 :           }
     307             :         }
     308      131157 :         lig--; col--; i = lig; continue;
     309             : 
     310     2216107 :       default: i--;
     311             :     }
     312       24861 :   if (DEBUGLEVEL>5) { err_printf("    after phase1:\n"); p_mat(mat,perm,0); }
     313             : 
     314             : #define absmax(s,z) {long _z; _z = labs(z); if (_z > s) s = _z;}
     315             :   /* Get rid of all lines containing only 0 and +/- 1, keeping track of column
     316             :    * operations in T. Leave the rows 1..lk0 alone [up to k0, coefficient
     317             :    * explosion, between k0+1 and lk0, row is 0] */
     318       24861 :   s = 0;
     319      268728 :   while (lig > lk0 && col && s < (long)(HIGHBIT>>1))
     320             :   {
     321      750965 :     for (i=lig; i>lk0; i--)
     322      729252 :       if (count(mat,perm[i],col,&n) > 0) break;
     323      240719 :     if (i == lk0) break;
     324             : 
     325             :     /* only 0, +/- 1 entries, at least 2 of them non-zero */
     326      219006 :     lswap(perm[i], perm[lig]);
     327      219006 :     swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     328      219006 :     if (T) swap(gel(T,n), gel(T,col));
     329      219006 :     if (p[perm[lig]] < 0)
     330             :     {
     331      155952 :       for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
     332      155952 :       if (T) ZV_togglesign(gel(T,col));
     333             :     }
     334     9576800 :     for (j=1; j<col; j++)
     335             :     {
     336     9357794 :       GEN matj = gel(mat,j);
     337             :       long t;
     338     9357794 :       if (! (t = matj[perm[lig]]) ) continue;
     339      592553 :       if (t == 1) {
     340      281984 :         for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= p[perm[i]]);
     341             :       }
     342             :       else { /* t = -1 */
     343      310569 :         for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] += p[perm[i]]);
     344             :       }
     345      592553 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
     346             :     }
     347      219006 :     lig--; col--;
     348      219006 :     if (gc_needed(av,3))
     349             :     {
     350           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[1]");
     351           0 :       if (T) T = gerepilecopy(av, T); else avma = av;
     352             :     }
     353             :   }
     354             :   /* As above with lines containing a +/- 1 (no other assumption).
     355             :    * Stop when single precision becomes dangerous */
     356       24861 :   vmax = cgetg(co,t_VECSMALL);
     357      312659 :   for (j=1; j<=col; j++)
     358             :   {
     359      287798 :     GEN matj = gel(mat,j);
     360      287798 :     for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[i]);
     361      287798 :     vmax[j] = s;
     362             :   }
     363      120131 :   while (lig > lk0 && col)
     364             :   {
     365      110382 :     for (i=lig; i>lk0; i--)
     366      103842 :       if ( (n = count2(mat,perm[i],col)) ) break;
     367       76949 :     if (i == lk0) break;
     368             : 
     369       70409 :     lswap(vmax[n], vmax[col]);
     370       70409 :     lswap(perm[i], perm[lig]);
     371       70409 :     swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     372       70409 :     if (T) swap(gel(T,n), gel(T,col));
     373       70409 :     if (p[perm[lig]] < 0)
     374             :     {
     375       31879 :       for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
     376       31879 :       if (T) ZV_togglesign(gel(T,col));
     377             :     }
     378     1177683 :     for (j=1; j<col; j++)
     379             :     {
     380     1107274 :       GEN matj = gel(mat,j);
     381             :       long t;
     382     1107274 :       if (! (t = matj[perm[lig]]) ) continue;
     383      595572 :       if (vmax[col] && (ulong)labs(t) >= (HIGHBIT-vmax[j]) / vmax[col])
     384           0 :         goto END2;
     385             : 
     386      595572 :       for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= t*p[perm[i]]);
     387      595572 :       vmax[j] = s;
     388      595572 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
     389             :     }
     390       70409 :     lig--; col--;
     391       70409 :     if (gc_needed(av,3))
     392             :     {
     393           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[2]");
     394           0 :       gerepileall(av, T? 2: 1, &vmax, &T);
     395             :     }
     396             :   }
     397             : 
     398             : END2: /* clean up mat: remove everything to the right of the 1s on diagonal */
     399             :   /* go multiprecision first */
     400       24861 :   matb = cgetg(co,t_MAT); /* bottom part (complement of matt) */
     401      662822 :   for (j=1; j<co; j++)
     402             :   {
     403      637961 :     GEN matj = gel(mat,j);
     404      637961 :     p1 = cgetg(li-k0,t_COL); gel(matb,j) = p1;
     405      637961 :     p1 -= k0;
     406      637961 :     for (i=k0+1; i<li; i++) gel(p1,i) = stoi(matj[perm[i]]);
     407             :   }
     408       24861 :   if (DEBUGLEVEL>5)
     409             :   {
     410           0 :     err_printf("    after phase2:\n");
     411           0 :     p_mat(mat,perm,lk0);
     412             :   }
     413      420865 :   for (i=li-2; i>lig; i--)
     414             :   {
     415      396004 :     long h, i0 = i - k0, k = i + co-li;
     416      396004 :     GEN Bk = gel(matb,k);
     417    13319415 :     for (j=k+1; j<co; j++)
     418             :     {
     419    12923411 :       GEN Bj = gel(matb,j), v = gel(Bj,i0);
     420    12923411 :       s = signe(v); if (!s) continue;
     421             : 
     422     2623316 :       gel(Bj,i0) = gen_0;
     423     2623316 :       if (is_pm1(v))
     424             :       {
     425     1495561 :         if (s > 0) /* v = 1 */
     426      741703 :         { for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), gel(Bk,h)); }
     427             :         else /* v = -1 */
     428      753858 :         { for (h=1; h<i0; h++) gel(Bj,h) = addii(gel(Bj,h), gel(Bk,h)); }
     429             :       }
     430             :       else {
     431     1127755 :         for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), mulii(v,gel(Bk,h)));
     432             :       }
     433     2623316 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,k), negi(v));
     434     2623316 :       if (gc_needed(av,3))
     435             :       {
     436          19 :         if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[3], (i,j) = %ld,%ld", i,j);
     437          19 :         for (h=1; h<co; h++) setlg(matb[h], i0+1); /* bottom can be forgotten */
     438          19 :         gerepileall(av, T? 2: 1, &matb, &T);
     439          19 :         Bk = gel(matb,k);
     440             :       }
     441             :     }
     442             :   }
     443       24861 :   for (j=1; j<co; j++) setlg(matb[j], lig-k0+1); /* bottom can be forgotten */
     444       24861 :   gerepileall(av, T? 2: 1, &matb, &T);
     445       24861 :   if (DEBUGLEVEL>5) err_printf("    matb cleaned up (using Id block)\n");
     446             : 
     447       24861 :   nlze = lk0 - k0;  /* # of 0 rows */
     448       24861 :   lnz = lig-nlze+1; /* 1 + # of non-0 rows (!= 0...0 1 0 ... 0) */
     449       24861 :   if (T) matt = ZM_mul(matt,T); /* update top rows */
     450       24861 :   extramat = cgetg(col+1,t_MAT); /* = new C minus the 0 rows */
     451      242250 :   for (j=1; j<=col; j++)
     452             :   {
     453      217389 :     GEN z = gel(matt,j);
     454      217389 :     GEN t = (gel(matb,j)) + nlze - k0;
     455      217389 :     p2=cgetg(lnz,t_COL); gel(extramat,j) = p2;
     456      217389 :     for (i=1; i<=k0; i++) gel(p2,i) = gel(z,i); /* top k0 rows */
     457      217389 :     for (   ; i<lnz; i++) gel(p2,i) = gel(t,i); /* other non-0 rows */
     458             :   }
     459       24861 :   if (!col) {
     460         357 :     permpro = identity_perm(lnz);
     461         357 :     nr = lnz;
     462             :   }
     463             :   else
     464       24504 :     permpro = ZM_rowrankprofile(extramat, &nr);
     465             :   /* lnz = lg(permpro) */
     466       24861 :   if (nlze)
     467             :   { /* put the nlze 0 rows (trivial generators) at the top */
     468        3881 :     p1 = new_chunk(lk0+1);
     469        3881 :     for (i=1; i<=nlze; i++) p1[i] = perm[i + k0];
     470        3881 :     for (   ; i<=lk0; i++)  p1[i] = perm[i - nlze];
     471        3881 :     for (i=1; i<=lk0; i++)  perm[i] = p1[i];
     472             :   }
     473             :   /* sort other rows according to permpro (nr redundant generators first) */
     474       24861 :   p1 = new_chunk(lnz); p2 = perm + nlze;
     475       24861 :   for (i=1; i<lnz; i++) p1[i] = p2[permpro[i]];
     476       24861 :   for (i=1; i<lnz; i++) p2[i] = p1[i];
     477             :   /* perm indexes the rows of mat
     478             :    *   |_0__|__redund__|__dense__|__too big__|_____done______|
     479             :    *   0  nlze                              lig             li
     480             :    *         \___nr___/ \___k0__/
     481             :    *         \____________lnz ______________/
     482             :    *
     483             :    *               col   co
     484             :    *       [dep     |     ]
     485             :    *    i0 [--------|  B  ] (i0 = nlze + nr)
     486             :    *       [matbnew |     ] matbnew has maximal rank = lnz-1 - nr
     487             :    * mat = [--------|-----] lig
     488             :    *       [   0    | Id  ]
     489             :    *       [        |     ] li */
     490             : 
     491       24861 :   matbnew = cgetg(col+1,t_MAT); /* dense+toobig, maximal rank. For hnffinal */
     492       24861 :   dep    = cgetg(col+1,t_MAT); /* rows dependent from the ones in matbnew */
     493      242250 :   for (j=1; j<=col; j++)
     494             :   {
     495      217389 :     GEN z = gel(extramat,j);
     496      217389 :     p1 = cgetg(nlze+nr+1,t_COL); gel(dep,j) = p1;
     497      217389 :     p2 = cgetg(lnz-nr,t_COL); gel(matbnew,j) = p2;
     498      217389 :     for (i=1; i<=nlze; i++) gel(p1,i) = gen_0;
     499      217389 :     p1 += nlze; for (i=1; i<=nr; i++) p1[i] = z[permpro[i]];
     500      217389 :     p2 -= nr;   for (   ; i<lnz; i++) p2[i] = z[permpro[i]];
     501             :   }
     502             : 
     503             :   /* redundant generators in terms of the genuine generators
     504             :    * (x_i) = - (g_i) B */
     505       24861 :   B = cgetg(co-col,t_MAT);
     506      445433 :   for (j=col+1; j<co; j++)
     507             :   {
     508      420572 :     GEN y = gel(matt,j);
     509      420572 :     GEN z = gel(matb,j);
     510      420572 :     p1=cgetg(lig+1,t_COL); gel(B,j-col) = p1;
     511      420572 :     for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
     512      420572 :     p1 += nlze; z += nlze-k0;
     513     3627693 :     for (k=1; k<lnz; k++)
     514             :     {
     515     3207121 :       i = permpro[k];
     516     3207121 :       gel(p1,k) = (i <= k0)? gel(y,i): gel(z,i);
     517             :     }
     518             :   }
     519       24861 :   if (T) C = typ(C)==t_MAT? RgM_mul(C,T): RgV_RgM_mul(C,T);
     520       24861 :   gerepileall(av, 4, &matbnew, &B, &dep, &C);
     521       24861 :   *ptdep = dep;
     522       24861 :   *ptB = B;
     523       24861 :   H = hnffinal(matbnew, perm, ptdep, ptB, &C);
     524       24861 :   if (CO > co)
     525             :   { /* treat the rest, N cols at a time (hnflll slow otherwise) */
     526           0 :     const long N = 300;
     527           0 :     long a, L = CO - co, l = minss(L, N); /* L columns to add */
     528           0 :     GEN CC = *ptC, m0 = mat0;
     529           0 :     setlg(CC, CO); /* restore */
     530           0 :     CC += co-1;
     531           0 :     m0 += co-1;
     532           0 :     for (a = l;;)
     533             :     {
     534             :       GEN MAT, emb;
     535           0 :       gerepileall(av, 4, &H,&C,ptB,ptdep);
     536           0 :       MAT = cgetg(l + 1, t_MAT);
     537           0 :       emb = cgetg(l + 1, typ(C));
     538           0 :       for (j = 1 ; j <= l; j++)
     539             :       {
     540           0 :         gel(MAT,j) = gel(m0,j);
     541           0 :         emb[j] = CC[j];
     542             :       }
     543           0 :       H = hnfadd_i(H, perm, ptdep, ptB, &C, MAT, emb);
     544           0 :       if (a == L) break;
     545           0 :       CC += l;
     546           0 :       m0 += l;
     547           0 :       a += l; if (a > L) { l = L - (a - l); a = L; }
     548           0 :     }
     549             :   }
     550       24861 :   *ptC = C; return H;
     551             : }
     552             : 
     553             : GEN
     554         322 : hnfspec(GEN mat, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, long k0)
     555             : {
     556         322 :   pari_sp av = avma;
     557         322 :   GEN H = hnfspec_i(mat, perm, ptdep, ptB, ptC, k0);
     558         322 :   gerepileall(av, 4, ptC, ptdep, ptB, &H); return H;
     559             : }
     560             : 
     561             : /* HNF reduce x, apply same transforms to C */
     562             : GEN
     563         322 : mathnfspec(GEN x, GEN *ptperm, GEN *ptdep, GEN *ptB, GEN *ptC)
     564             : {
     565         322 :   long i,j,k,ly,lx = lg(x);
     566             :   GEN z, perm;
     567         322 :   if (lx == 1) return cgetg(1, t_MAT);
     568         322 :   ly = lgcols(x);
     569         322 :   *ptperm = perm = identity_perm(ly-1);
     570         322 :   z = cgetg(lx,t_MAT);
     571        2604 :   for (i=1; i<lx; i++)
     572             :   {
     573        2282 :     GEN C = cgetg(ly,t_COL), D = gel(x,i);
     574        2282 :     gel(z,i) = C;
     575       88340 :     for (j=1; j<ly; j++)
     576             :     {
     577       86058 :       GEN d = gel(D,j);
     578       86058 :       if (is_bigint(d)) goto TOOLARGE;
     579       86058 :       C[j] = itos(d);
     580             :     }
     581             :   }
     582             :   /*  [ dep |     ]
     583             :    *  [-----|  B  ]
     584             :    *  [  H  |     ]
     585             :    *  [-----|-----]
     586             :    *  [  0  | Id  ] */
     587         322 :   return hnfspec(z,perm, ptdep, ptB, ptC, 0);
     588             : 
     589             : TOOLARGE:
     590           0 :   if (lg(*ptC) > 1 && lgcols(*ptC) > 1)
     591           0 :     pari_err_IMPL("mathnfspec with large entries");
     592           0 :   x = ZM_hnf(x); lx = lg(x); j = ly; k = 0;
     593           0 :   for (i=1; i<ly; i++)
     594             :   {
     595           0 :     if (equali1(gcoeff(x,i,i + lx-ly)))
     596           0 :       perm[--j] = i;
     597             :     else
     598           0 :       perm[++k] = i;
     599             :   }
     600           0 :   setlg(perm,k+1);
     601           0 :   x = rowpermute(x, perm); /* upper part */
     602           0 :   setlg(perm,ly);
     603           0 :   *ptB = vecslice(x, j+lx-ly, lx-1);
     604           0 :   setlg(x, j);
     605           0 :   *ptdep = rowslice(x, 1, lx-ly);
     606           0 :   return rowslice(x, lx-ly+1, k); /* H */
     607             : }
     608             : 
     609             : /* add new relations to a matrix treated by hnfspec (extramat / extraC) */
     610             : GEN
     611       17236 : hnfadd_i(GEN H, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, /* cf hnfspec */
     612             :        GEN extramat,GEN extraC)
     613             : {
     614       17236 :   GEN matb, extratop, Cnew, permpro, B = *ptB, C = *ptC, dep = *ptdep;
     615             :   long i, lH, lB, li, lig, co, col, nlze;
     616             : 
     617       17236 :   if (lg(extramat) == 1) return H;
     618       17236 :   co = lg(C)-1;
     619       17236 :   lH = lg(H)-1;
     620       17236 :   lB = lg(B)-1;
     621       17236 :   li = lg(perm)-1;
     622       17236 :   lig = li - lB;
     623       17236 :   col = co - lB;
     624       17236 :   nlze = lig - lH;
     625             : 
     626             :  /*               col    co
     627             :   *       [ 0 |dep |     ]
     628             :   *  nlze [--------|  B  ]
     629             :   *       [ 0 | H  |     ]
     630             :   *       [--------|-----] lig
     631             :   *       [   0    | Id  ]
     632             :   *       [        |     ] li */
     633       17236 :   extratop = zm_to_ZM( rowslicepermute(extramat, perm, 1, lig) );
     634       17236 :   if (li != lig)
     635             :   { /* zero out bottom part, using the Id block */
     636       17236 :     GEN A = vecslice(C, col+1, co);
     637       17236 :     GEN c = rowslicepermute(extramat, perm, lig+1, li);
     638       17236 :     extraC   = gsub(extraC, typ(A)==t_MAT? RgM_zm_mul(A, c): RgV_zm_mul(A,c));
     639       17236 :     extratop = ZM_sub(extratop, ZM_zm_mul(B, c));
     640             :   }
     641             : 
     642       17236 :   extramat = shallowconcat(extratop, vconcat(dep, H));
     643       17236 :   Cnew     = shallowconcat(extraC, vecslice(C, col-lH+1, co));
     644       17236 :   if (DEBUGLEVEL>5) err_printf("    1st phase done\n");
     645       17236 :   permpro = ZM_rowrankprofile(extramat, &nlze);
     646       17236 :   extramat = rowpermute(extramat, permpro);
     647       17236 :   *ptB     = rowpermute(B,        permpro);
     648       17236 :   permpro = vecsmallpermute(perm, permpro);
     649       17236 :   for (i=1; i<=lig; i++) perm[i] = permpro[i]; /* perm o= permpro */
     650             : 
     651       17236 :   *ptdep  = rowslice(extramat, 1, nlze);
     652       17236 :   matb    = rowslice(extramat, nlze+1, lig);
     653       17236 :   if (DEBUGLEVEL>5) err_printf("    2nd phase done\n");
     654       17236 :   H = hnffinal(matb,perm,ptdep,ptB,&Cnew);
     655       17236 :   *ptC = shallowconcat(vecslice(C, 1, col-lH), Cnew);
     656       17236 :   return H;
     657             : }
     658             : 
     659             : GEN
     660           0 : hnfadd(GEN H, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, /* cf hnfspec */
     661             :        GEN extramat,GEN extraC)
     662             : {
     663           0 :   pari_sp av = avma;
     664           0 :   H = hnfadd_i(H, perm, ptdep, ptB, ptC, ZM_to_zm(extramat), extraC);
     665           0 :   gerepileall(av, 4, ptC, ptdep, ptB, &H); return H;
     666             : }
     667             : 
     668             : /* zero aj = Aij (!= 0)  using  ak = Aik (maybe 0), via linear combination of
     669             :  * A[j] and A[k] of determinant 1. If U != NULL, likewise update its columns */
     670             : static void
     671    14231177 : ZC_elem(GEN aj, GEN ak, GEN A, GEN U, long j, long k)
     672             : {
     673             :   GEN p1,u,v,d;
     674             : 
     675    14231177 :   if (!signe(ak)) {
     676     2553087 :     swap(gel(A,j), gel(A,k));
     677     2553087 :     if (U) swap(gel(U,j), gel(U,k));
     678    14499664 :     return;
     679             :   }
     680    11678090 :   d = bezout(aj,ak,&u,&v);
     681             :   /* frequent special case (u,v) = (1,0) or (0,1) */
     682    11678090 :   if (!signe(u))
     683             :   { /* ak | aj */
     684     5250387 :     p1 = diviiexact(aj,ak); togglesign(p1);
     685     5250387 :     ZC_lincomb1_inplace(gel(A,j), gel(A,k), p1);
     686     5250387 :     if (U)
     687      450421 :       ZC_lincomb1_inplace(gel(U,j), gel(U,k), p1);
     688     5250387 :     return;
     689             :   }
     690     6427703 :   if (!signe(v))
     691             :   { /* aj | ak */
     692     4143103 :     p1 = diviiexact(ak,aj); togglesign(p1);
     693     4143103 :     ZC_lincomb1_inplace(gel(A,k), gel(A,j), p1);
     694     4143103 :     swap(gel(A,j), gel(A,k));
     695     4143103 :     if (U) {
     696       46699 :       ZC_lincomb1_inplace(gel(U,k), gel(U,j), p1);
     697       46699 :       swap(gel(U,j), gel(U,k));
     698             :     }
     699     4143103 :     return;
     700             :   }
     701             : 
     702     2284600 :   if (!is_pm1(d)) { aj = diviiexact(aj, d); ak = diviiexact(ak, d); }
     703     2284600 :   p1 = gel(A,k); aj = negi(aj); /* NOT togglesign */
     704     2284600 :   gel(A,k) = ZC_lincomb(u,v, gel(A,j),p1);
     705     2284600 :   gel(A,j) = ZC_lincomb(aj,ak, p1,gel(A,j));
     706     2284600 :   if (U)
     707             :   {
     708      145134 :     p1 = gel(U,k);
     709      145134 :     gel(U,k) = ZC_lincomb(u,v, gel(U,j),p1);
     710      145134 :     gel(U,j) = ZC_lincomb(aj,ak, p1,gel(U,j));
     711             :   }
     712             : }
     713             : 
     714             : INLINE int
     715         931 : is_RgX(GEN a, long v) { return typ(a) == t_POL && varn(a)==v; }
     716             : /* set u,v such that au + bv = gcd(a,b), divide a,b by the gcd */
     717             : static GEN
     718         266 : gbezout_step(GEN *pa, GEN *pb, GEN *pu, GEN *pv, long vx)
     719             : {
     720         266 :   GEN a = *pa, b = *pb, d;
     721         266 :   if (gequal0(a))
     722             :   {
     723           0 :     *pa = gen_0; *pu = gen_0;
     724           0 :     *pb = gen_1; *pv = gen_1; return b;
     725             :   }
     726         266 :   a = is_RgX(a,vx)? RgX_renormalize(a): scalarpol(a, vx);
     727         266 :   b = is_RgX(b,vx)? RgX_renormalize(b): scalarpol(b, vx);
     728         266 :   d = RgX_extgcd(a,b, pu,pv);
     729         266 :   if (degpol(d)) { a = RgX_div(a, d); b = RgX_div(b, d); }
     730         154 :   else if (typ(gel(d,2)) == t_REAL && lg(gel(d,2)) <= 3)
     731             : #if 1
     732             :   { /* possible accuracy problem */
     733           0 :     GEN D = RgX_gcd_simple(a,b);
     734           0 :     if (degpol(D)) {
     735           0 :       D = RgX_Rg_div(D, leading_coeff(D));
     736           0 :       a = RgX_div(a, D);
     737           0 :       b = RgX_div(b, D);
     738           0 :       d = RgX_extgcd(a,b, pu,pv); /* retry now */
     739           0 :       d = RgX_mul(d, D);
     740             :     }
     741             :   }
     742             : #else
     743             :   { /* less stable */
     744             :     d = RgX_extgcd_simple(a,b, pu,pv);
     745             :     if (degpol(d)) { a = RgX_div(a, d); b = RgX_div(b, d); }
     746             :   }
     747             : #endif
     748         266 :   *pa = a;
     749         266 :   *pb = b; return d;
     750             : }
     751             : static GEN
     752      237000 : col_mul(GEN x, GEN c)
     753             : {
     754      237000 :   if (typ(x) == t_INT)
     755             :   {
     756      237000 :     long s = signe(x);
     757      237000 :     if (!s) return NULL;
     758      178065 :     if (is_pm1(x)) return (s > 0)? c: RgC_neg(c);
     759             :   }
     760       32221 :   return RgC_Rg_mul(c, x);
     761             : }
     762             : static void
     763           0 : do_zero(GEN x)
     764             : {
     765           0 :   long i, lx = lg(x);
     766           0 :   for (i=1; i<lx; i++) gel(x,i) = gen_0;
     767           0 : }
     768             : 
     769             : /* (c1, c2) *= [u,-b; v,a] */
     770             : static void
     771       59250 : update(GEN u, GEN v, GEN a, GEN b, GEN *c1, GEN *c2)
     772             : {
     773             :   GEN p1,p2;
     774             : 
     775       59250 :   u = col_mul(u,*c1);
     776       59250 :   v = col_mul(v,*c2);
     777       59250 :   if (u) p1 = v? gadd(u,v): u;
     778         315 :   else   p1 = v? v: NULL;
     779             : 
     780       59250 :   a = col_mul(a,*c2);
     781       59250 :   b = col_mul(gneg_i(b),*c1);
     782       59250 :   if (a) p2 = b? RgC_add(a,b): a;
     783           0 :   else   p2 = b? b: NULL;
     784             : 
     785       59250 :   if (!p1) do_zero(*c1); else *c1 = p1;
     786       59250 :   if (!p2) do_zero(*c2); else *c2 = p2;
     787       59250 : }
     788             : 
     789             : /* zero aj = Aij (!= 0)  using  ak = Aik (maybe 0), via linear combination of
     790             :  * A[j] and A[k] of determinant 1. If U != NULL, likewise update its columns */
     791             : static void
     792           7 : RgC_elem(GEN aj, GEN ak, GEN A, GEN V, long j, long k, long li, long vx)
     793             : {
     794           7 :   GEN u,v, d = gbezout_step(&aj, &ak, &u, &v, vx);
     795             :   long l;
     796             :   /* (A[,k], A[,j]) *= [v, -aj; u, ak ] */
     797          14 :   for (l = 1; l < li; l++)
     798             :   {
     799           7 :     GEN t = gadd(gmul(u,gcoeff(A,l,j)), gmul(v,gcoeff(A,l,k)));
     800           7 :     gcoeff(A,l,j) = gsub(gmul(ak,gcoeff(A,l,j)), gmul(aj,gcoeff(A,l,k)));
     801           7 :     gcoeff(A,l,k) = t;
     802             :   }
     803           7 :   gcoeff(A,li,j) = gen_0;
     804           7 :   gcoeff(A,li,k) = d;
     805           7 :   if (V) update(v,u,ak,aj,(GEN*)(V+k),(GEN*)(V+j));
     806           7 : }
     807             : 
     808             : /* reduce A[i,j] mod A[i,j0] for j=j0+1... via column operations */
     809             : static void
     810     1346934 : ZM_reduce(GEN A, GEN U, long i, long j0)
     811             : {
     812     1346934 :   long j, lA = lg(A);
     813     1346934 :   GEN d = gcoeff(A,i,j0);
     814     1346934 :   if (signe(d) < 0)
     815             :   {
     816        4078 :     ZV_neg_inplace(gel(A,j0));
     817        4078 :     if (U) ZV_togglesign(gel(U,j0));
     818        4078 :     d = gcoeff(A,i,j0);
     819             :   }
     820     3799155 :   for (j=j0+1; j<lA; j++)
     821             :   {
     822     2452221 :     GEN q = truedivii(gcoeff(A,i,j), d);
     823     2452221 :     if (!signe(q)) continue;
     824             : 
     825      100770 :     togglesign(q);
     826      100770 :     ZC_lincomb1_inplace(gel(A,j), gel(A,j0), q);
     827      100770 :     if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,j0), q);
     828             :   }
     829     1346934 : }
     830             : 
     831             : static GEN
     832         168 : RgX_normalize_all(GEN T, GEN *pd)
     833             : {
     834         168 :   GEN d = leading_coeff(T);
     835         336 :   while (gequal0(d) || ( typ(d) == t_REAL && lg(d) == 3
     836           0 :                        && gexpo(T) - expo(d) > (long)BITS_IN_LONG)) {
     837           0 :      T = normalizepol_lg(T, lg(T)-1);
     838           0 :      if (!signe(T)) { *pd = gen_1; return T; }
     839           0 :      d = leading_coeff(T);
     840             :   }
     841         168 :   *pd = d;
     842         168 :   return RgX_Rg_div(T, d);
     843             : }
     844             : /* reduce A[i,j] mod A[i,j0] for j=j0+1... via column operations */
     845             : static void
     846          28 : RgM_reduce(GEN A, GEN U, long i, long j0, long vx)
     847             : {
     848          28 :   long j, lA = lg(A);
     849          28 :   GEN d, T = gcoeff(A,i,j0);
     850          28 :   if (is_RgX(T,vx)) {
     851          28 :     T = RgX_normalize_all(T, &d);
     852          28 :     if (degpol(T) == 0) { d = gel(T,2); T = gen_1; }
     853             :   } else {
     854           0 :     d = T; T = gen_1;
     855             :   }
     856          28 :   if (U && !gequal1(d)) gel(U,j0) = RgC_Rg_div(gel(U,j0), d);
     857          28 :   gcoeff(A,i,j0) = T;
     858             : 
     859          35 :   for (j=j0+1; j<lA; j++)
     860             :   {
     861           7 :     GEN t = gcoeff(A,i,j), q;
     862           7 :     if (gequal0(t)) continue;
     863           7 :     if (T == gen_1)
     864           0 :       q = t;
     865           7 :     else if (is_RgX(t,vx))
     866           7 :       q = RgX_div(t, T);
     867           0 :     else continue;
     868             : 
     869           7 :     if (gequal0(q)) continue;
     870           0 :     gel(A,j) = RgC_sub(gel(A,j), RgC_Rg_mul(gel(A,j0), q));
     871           0 :     if (U) gel(U,j) = RgC_sub(gel(U,j), RgC_Rg_mul(gel(U,j0), q));
     872             :   }
     873          28 : }
     874             : 
     875             : /* A,B square integral in upper HNF, of the same dimension > 0. Return Au
     876             :  * in Z^n (v in Z^n not computed), such that Au + Bv = [1, 0, ..., 0] */
     877             : GEN
     878      122864 : hnfmerge_get_1(GEN A, GEN B)
     879             : {
     880      122864 :   pari_sp av = avma;
     881      122864 :   long j, k, l = lg(A), lb;
     882      122864 :   GEN b, U = cgetg(l + 1, t_MAT), C = cgetg(l + 1, t_VEC);
     883             : 
     884      122864 :   b = gcoeff(B,1,1); lb = lgefint(b);
     885      252322 :   for (j = 1; j < l; j++)
     886             :   {
     887             :     GEN t;
     888      252322 :     long c = j+1;
     889      252322 :     gel(U,j) = col_ei(l-1, j);
     890      252322 :     gel(U,c) = zerocol(l-1); /* dummy */
     891      252322 :     gel(C,j) = vecslice(gel(A,j), 1,j);
     892      252322 :     gel(C,c) = vecslice(gel(B,j), 1,j);
     893      729225 :     for (k = j; k > 0; k--)
     894             :     {
     895      476903 :       t = gcoeff(C,k,c);
     896      476903 :       if (gequal0(t)) continue;
     897      417018 :       setlg(C[c], k+1);
     898      417018 :       ZC_elem(t, gcoeff(C,k,k), C, U, c, k);
     899      417018 :       if (lgefint(gcoeff(C,k,k)) > lb) gel(C,k) = FpC_red(gel(C,k), b);
     900      417018 :       if (j > 4)
     901             :       {
     902       43820 :         GEN u = gel(U,k);
     903             :         long h;
     904      614173 :         for (h=1; h<l; h++)
     905      570353 :           if (lgefint(gel(u,h)) > lb) gel(u,h) = remii(gel(u,h), b);
     906             :       }
     907             :     }
     908      252322 :     if (j == 1)
     909      122864 :       t = gcoeff(C,1,1);
     910             :     else
     911             :     {
     912             :       GEN u;
     913      129458 :       t = bezout(gcoeff(C,1,1), b, &u, NULL); /* >= 0 */
     914      129458 :       if (signe(u) && !equali1(u)) gel(U,1) = ZC_Z_mul(gel(U,1), u);
     915      129458 :       gcoeff(C,1,1) = t;
     916             :     }
     917      252322 :     if (equali1(t)) break;
     918             :   }
     919      122864 :   if (j >= l) return NULL;
     920      122864 :   b = lcmii(gcoeff(A,1,1),b);
     921      122864 :   A = FpC_red(ZM_ZC_mul(A,gel(U,1)), b);
     922      122864 :   return gerepileupto(av, FpC_center(A, b, shifti(b,-1)));
     923             : }
     924             : 
     925             : /* remove the first r columns */
     926             : static void
     927       53624 : remove_0cols(long r, GEN *pA, GEN *pB, long remove)
     928             : {
     929       53624 :   GEN A = *pA, B = *pB;
     930       53624 :   long l = lg(A);
     931       53624 :   A += r; A[0] = evaltyp(t_MAT) | evallg(l-r);
     932       53624 :   if (B && remove == 2) { B += r; B[0] = A[0]; }
     933       53624 :   *pA = A; *pB = B;
     934       53624 : }
     935             : 
     936             : /* Inefficient compared to hnfall. 'remove' = throw away lin.dep columns */
     937             : static GEN
     938       17791 : hnf_i(GEN A, int remove)
     939             : {
     940       17791 :   pari_sp av0 = avma, av;
     941             :   long s, n, m, j, k, li, def, ldef;
     942             : 
     943       17791 :   RgM_dimensions(A, &m, &n);
     944       17791 :   if (!n) return cgetg(1,t_MAT);
     945       17686 :   av = avma;
     946       17686 :   A = RgM_shallowcopy(A);
     947       17686 :   def = n; ldef = (m>n)? m-n: 0;
     948       48777 :   for (li=m; li>ldef; li--)
     949             :   {
     950       78901 :     for (j=def-1; j; j--)
     951             :     {
     952       47810 :       GEN a = gcoeff(A,li,j);
     953       47810 :       if (!signe(a)) continue;
     954             : 
     955             :       /* zero a = Aij  using  b = Aik */
     956       25036 :       k = (j==1)? def: j-1;
     957       25036 :       ZC_elem(a,gcoeff(A,li,k), A,NULL, j,k);
     958       25036 :       if (gc_needed(av,1))
     959             :       {
     960           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnf[1]. li=%ld",li);
     961           0 :         A = gerepilecopy(av, A);
     962             :       }
     963             :     }
     964       31091 :     s = signe(gcoeff(A,li,def));
     965       31091 :     if (s)
     966             :     {
     967       30433 :       if (s < 0) ZV_neg_inplace(gel(A,def));
     968       30433 :       ZM_reduce(A, NULL, li,def);
     969       30433 :       def--;
     970             :     }
     971             :     else
     972         658 :       if (ldef) ldef--;
     973       31091 :     if (gc_needed(av,1))
     974             :     {
     975           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnf[2]. li=%ld",li);
     976           0 :       A = gerepilecopy(av, A);
     977             :     }
     978             :   }
     979             :   /* rank A = n - def */
     980       17686 :   if (remove) { GEN B = NULL; remove_0cols(def, &A, &B, remove); }
     981       17686 :   return gerepileupto(av0, ZM_copy(A));
     982             : }
     983             : 
     984             : GEN
     985       20230 : ZM_hnf(GEN x) { return lg(x) > 8? ZM_hnfall(x, NULL, 1): hnf_i(x, 1); }
     986             : 
     987             : /* u*z[1..k] mod p, in place */
     988             : static void
     989     2598083 : FpV_Fp_mul_part_ip(GEN z, GEN u, GEN p, long k)
     990             : {
     991             :   long i;
     992     2598083 :   if (is_pm1(u)) {
     993      205674 :     if (signe(u) > 0) {
     994      865374 :       for (i = 1; i <= k; i++)
     995      676626 :         if (signe(gel(z,i))) gel(z,i) = modii(gel(z,i), p);
     996             :     } else {
     997       43610 :       for (i = 1; i <= k; i++)
     998       26684 :         if (signe(gel(z,i))) gel(z,i) = modii(negi(gel(z,i)), p);
     999             :     }
    1000             :   }
    1001             :   else {
    1002     9780975 :     for (i = 1; i <= k; i++)
    1003     7388566 :       if (signe(gel(z,i))) gel(z,i) = Fp_mul(u,gel(z,i), p);
    1004             :   }
    1005     2598083 : }
    1006             : static void
    1007     7916274 : FpV_red_part_ipvec(GEN z, GEN p, long k)
    1008             : {
    1009             :   long i;
    1010     7916274 :   for (i = 1; i <= k; i++) gel(z,i) = modii(gel(z,i), gel(p,i));
    1011     7916274 : }
    1012             : 
    1013             : /* return x * U, in echelon form (mod p^m), where (det(U),p) = 1.
    1014             :  * If early_abort is set, return NULL as soon as one pivot is 0 (mod p^m) */
    1015             : GEN
    1016       46922 : ZpM_echelon(GEN x, long early_abort, GEN p, GEN pm)
    1017             : {
    1018       46922 :   pari_sp av0 = avma, av;
    1019             :   long m, li, co, i, j, k, def, ldef;
    1020             : 
    1021       46922 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1022       46922 :   li = lgcols(x);
    1023       46922 :   av = avma;
    1024       46922 :   x = RgM_shallowcopy(x);
    1025       46922 :   m = Z_pval(pm, p);
    1026             : 
    1027       46922 :   ldef = (li > co)? li - co: 0;
    1028      423864 :   for (def = co-1,i = li-1; i > ldef; i--)
    1029             :   {
    1030      377366 :     long vmin = LONG_MAX, kmin = 0;
    1031      377366 :     GEN umin = gen_0, pvmin, q;
    1032     3450901 :     for (k = 1; k <= def; k++)
    1033             :     {
    1034     3146858 :       GEN u = gcoeff(x,i,k);
    1035             :       long v;
    1036     3146858 :       if (!signe(u)) continue;
    1037     1518005 :       v = Z_pvalrem(u, p, &u);
    1038     1518005 :       if (v >= m) gcoeff(x,i,k) = gen_0;
    1039      982383 :       else if (v < vmin) {
    1040      365706 :         vmin = v; kmin = k; umin = u;
    1041      365706 :         if (!vmin) break;
    1042             :       }
    1043             :     }
    1044      377366 :     if (!kmin)
    1045             :     {
    1046      154246 :       if (early_abort) return NULL;
    1047      153822 :       gcoeff(x,i,def) = gen_0;
    1048      153822 :       ldef--;
    1049      153822 :       if (ldef < 0) ldef = 0;
    1050      153822 :       continue;
    1051             :     }
    1052      223120 :     if (kmin != def) swap(gel(x,def), gel(x,kmin));
    1053      223120 :     q = vmin? powiu(p, m-vmin): pm;
    1054             :     /* pivot has valuation vmin */
    1055      223120 :     umin = modii(umin, q);
    1056      223120 :     if (!equali1(umin))
    1057      174969 :       FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(umin,q), pm, i-1);
    1058      223120 :     gcoeff(x, i, def) = pvmin = powiu(p, vmin);
    1059     2590973 :     for (j = def-1; j; j--)
    1060             :     { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
    1061     2367853 :       GEN t, a = gcoeff(x,i,j) = modii(gcoeff(x,i,j), pm);
    1062     2367853 :       if (!signe(a)) continue;
    1063             : 
    1064     1166398 :       t = diviiexact(a, pvmin); togglesign(t);
    1065     1166398 :       ZC_lincomb1_inplace(gel(x,j), gel(x,def), t);
    1066     1166398 :       if (gc_needed(av,1))
    1067             :       {
    1068          14 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZpM_echelon. i=%ld",i);
    1069          14 :         x = gerepilecopy(av, x); pvmin = gcoeff(x,i,def);
    1070             :       }
    1071             :     }
    1072      223120 :     def--;
    1073             :   }
    1074       46498 :   if (co > li)
    1075             :   {
    1076           0 :     x += co - li;
    1077           0 :     x[0] = evaltyp(t_MAT) | evallg(li);
    1078             :   }
    1079       46498 :   return gerepilecopy(av0, x);
    1080             : }
    1081             : GEN
    1082      773872 : zlm_echelon(GEN x, long early_abort, ulong p, ulong pm)
    1083             : {
    1084      773872 :   pari_sp av0 = avma;
    1085             :   long li, co, i, j, k, def, ldef;
    1086             :   ulong m;
    1087             : 
    1088      773872 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1089      773872 :   li = lgcols(x);
    1090      773872 :   x = Flm_copy(x);
    1091      773872 :   m = u_lval(pm, p);
    1092             : 
    1093      773872 :   ldef = (li > co)? li - co: 0;
    1094     3967279 :   for (def = co-1,i = li-1; i > ldef; i--)
    1095             :   {
    1096     3249695 :     long vmin = LONG_MAX, kmin = 0;
    1097     3249695 :     ulong umin = 0, pvmin, q;
    1098    12587177 :     for (k = 1; k <= def; k++)
    1099             :     {
    1100    11201765 :       ulong u = ucoeff(x,i,k);
    1101             :       long v;
    1102    11201765 :       if (!u) continue;
    1103     5211980 :       v = u_lvalrem(u, p, &u);
    1104     5211980 :       if (v >= (long) m) ucoeff(x,i,k) = 0;
    1105     5211980 :       else if (v < vmin) {
    1106     3256275 :         vmin = v; kmin = k; umin = u;
    1107     3256275 :         if (!vmin) break;
    1108             :       }
    1109             :     }
    1110     3249695 :     if (!kmin)
    1111             :     {
    1112      235641 :       if (early_abort) return NULL;
    1113      179353 :       ucoeff(x,i,def) = 0;
    1114      179353 :       ldef--;
    1115      179353 :       if (ldef < 0) ldef = 0;
    1116      179353 :       continue;
    1117             :     }
    1118     3014054 :     if (kmin != def) swap(gel(x,def), gel(x,kmin));
    1119     3014054 :     q = vmin? upowuu(p, m-vmin): pm;
    1120             :     /* pivot has valuation vmin */
    1121     3014054 :     umin %= q;
    1122     3014054 :     if (umin != 1)
    1123     1557398 :       Flv_Fl_mul_part_inplace(gel(x,def), Fl_inv(umin,q), pm, i-1);
    1124     3014054 :     ucoeff(x, i, def) = pvmin = upowuu(p, vmin);
    1125    18551083 :     for (j = def-1; j; j--)
    1126             :     { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
    1127    15537029 :       ulong t, a = ucoeff(x,i,j);
    1128    15537029 :       if (!a) continue;
    1129             : 
    1130     8662302 :       t = Fl_neg(a / pvmin, q);
    1131     8662302 :       Flc_lincomb1_inplace(gel(x,j), gel(x,def), t, pm);
    1132             :     }
    1133     3014054 :     def--;
    1134             :   }
    1135      717584 :   if (co > li)
    1136             :   {
    1137           0 :     x += co - li;
    1138           0 :     x[0] = evaltyp(t_MAT) | evallg(li);
    1139             :   }
    1140      717584 :   return gerepilecopy(av0, x);
    1141             : }
    1142             : 
    1143             : /* dm = multiple of diag element (usually detint(x))
    1144             :  * flag & hnf_MODID: reduce mod dm * matid [ otherwise as above ].
    1145             :  * flag & hnf_PART: don't reduce once diagonal is known; */
    1146             : 
    1147             : /* x a ZM, dm a t_INT */
    1148             : GEN
    1149     1867756 : ZM_hnfmodall_i(GEN x, GEN dm, long flag)
    1150             : {
    1151             :   pari_sp av;
    1152     1867756 :   const long center = (flag & hnf_CENTER);
    1153     1867756 :   long moddiag = (flag & hnf_MODID);
    1154             :   long li, co, i, j, k, def, ldef;
    1155             :   GEN a, b, p1, p2, u, dm2, LDM;
    1156             : 
    1157     1867756 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1158     1867700 :   li = lgcols(x); if (li == 1) return cgetg(1,t_MAT);
    1159     1867693 :   if (typ(dm) == t_INT)
    1160             :   {
    1161     1853077 :     if (flag == hnf_MODID && BPSW_psp(dm)) return ZM_hnfmodprime(x, dm);
    1162     1366621 :     dm2 = shifti(dm, -1);
    1163     1366621 :     dm = const_vec(li-1,dm);
    1164     1366621 :     dm2= const_vec(li-1,dm2);
    1165     1366621 :     LDM= const_vecsmall(li-1, lgefint(dm));
    1166             :   }
    1167             :   else
    1168             :   {
    1169       14616 :     if (lg(dm) != li) pari_err_DIM("ZM_hnfmod");
    1170       14616 :     moddiag = 1;
    1171       14616 :     dm2 = cgetg(li, t_VEC);
    1172       14616 :     LDM = cgetg(li, t_VECSMALL);
    1173       46319 :     for (i=1; i<li; i++)
    1174             :     {
    1175       31703 :       gel(dm2,i) = shifti(gel(dm,i),-1);
    1176       31703 :       LDM[i] = lgefint(gel(dm,i));
    1177             :     }
    1178             :   }
    1179     1381237 :   av = avma;
    1180     1381237 :   x = RgM_shallowcopy(x);
    1181             : 
    1182     1381237 :   ldef = 0;
    1183     1381237 :   if (li > co)
    1184             :   {
    1185        2681 :     ldef = li - co;
    1186        2681 :     if (!moddiag)
    1187           7 :       pari_err_DOMAIN("ZM_hnfmod","nb lines",">", strtoGENstr("nb columns"), x);
    1188             :   }
    1189     5583420 :   for (def = co-1,i = li-1; i > ldef; i--,def--)
    1190             :   {
    1191     4202190 :     GEN d = gel(dm,i), d2 = gel(dm2,i);
    1192     4202190 :     gcoeff(x,i,def) = centermodii(gcoeff(x,i,def), d,d2);
    1193    21191603 :     for (j = def-1; j; j--)
    1194             :     {
    1195    16989413 :       gcoeff(x,i,j) = centermodii(gcoeff(x,i,j), d,d2);
    1196    16989413 :       a = gcoeff(x,i,j);
    1197    16989413 :       if (!signe(a)) continue;
    1198             : 
    1199     9421674 :       k = (j==1)? def: j-1;
    1200     9421674 :       gcoeff(x,i,k) = centermodii(gcoeff(x,i,k), d,d2);
    1201     9421674 :       ZC_elem(a,gcoeff(x,i,k), x,NULL, j,k);
    1202     9421674 :       p1 = gel(x,j);
    1203     9421674 :       p2 = gel(x,k);
    1204             :       /* prevent coeffs explosion: reduce mod dm when lg() > ldm */
    1205    62181061 :       for (k = 1; k < i; k++)
    1206             :       {
    1207    52759387 :         if (lgefint(gel(p1,k)) > LDM[k])
    1208          13 :           gel(p1,k) = centermodii(gel(p1,k), gel(dm,k),gel(dm2,k));
    1209    52759387 :         if (lgefint(gel(p2,k)) > LDM[k])
    1210           5 :           gel(p2,k) = centermodii(gel(p2,k), gel(dm,k),gel(dm2,k));
    1211             :       }
    1212             :     }
    1213     4202190 :     if (gc_needed(av,1))
    1214             :     {
    1215         157 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[1]. i=%ld",i);
    1216         157 :       x = gerepilecopy(av, x);
    1217             :     }
    1218     4202190 :     if (moddiag && !signe(gcoeff(x,i,def)))
    1219             :     { /* missing pivot on line i, insert column */
    1220     1150363 :       GEN a = cgetg(co + 1, t_MAT);
    1221     1150363 :       for (k = 1; k <= def; k++) gel(a,k) = gel(x,k);
    1222     1150363 :       gel(a,k++) = Rg_col_ei(gel(dm,i), li-1, i);
    1223     1150363 :       for (     ; k <= co;  k++) gel(a,k) = gel(x,k-1);
    1224     1150363 :       ldef--; if (ldef < 0) ldef = 0;
    1225     1150363 :       co++; def++; x = a;
    1226             :     }
    1227             :   }
    1228     1381230 :   if (co < li)
    1229             :   { /* implies moddiag, add missing diag(dm) components */
    1230        1736 :     GEN a = cgetg(li+1, t_MAT);
    1231        1736 :     for (k = 1; k <= li-co; k++) gel(a,k) = Rg_col_ei(gel(dm,k), li-1, k);
    1232        1736 :     for (i = 1; i < co; i++) gel(a,k-1+i) = gel(x,i);
    1233        1736 :     gel(a,li) = zerocol(li-1); x = a;
    1234             :   }
    1235             :   else
    1236             :   {
    1237     1379494 :     x += co - li;
    1238     1379494 :     x[0] = evaltyp(t_MAT) | evallg(li); /* kill 0 columns */
    1239             :   }
    1240     1381230 :   if (moddiag)
    1241             :   { /* one column extra: an accumulator, discarded at the end */
    1242     1269440 :     if (lg(x) == li) x = shallowconcat(x, zerocol(li-1));
    1243             :     /* add up missing diag(dm) components */
    1244     5126061 :     for (i = li-1; i > 0; i--)
    1245             :     {
    1246     3856621 :       gcoeff(x, i, li) = gel(dm,i);
    1247    14551890 :       for (j = i; j > 0; j--)
    1248             :       {
    1249    10695269 :         GEN a = gcoeff(x, j, li);
    1250    10695269 :         if (!signe(a)) continue;
    1251     3958137 :         ZC_elem(a, gcoeff(x,j,j), x, NULL, li,j);
    1252     3958137 :         FpV_red_part_ipvec(gel(x,li), dm, j-1);
    1253     3958137 :         FpV_red_part_ipvec(gel(x,j),  dm, j-1);
    1254             :       }
    1255     3856621 :       if (gc_needed(av,1))
    1256             :       {
    1257           7 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[2]. i=%ld", i);
    1258           7 :         x = gerepilecopy(av, x);
    1259             :       }
    1260             :     }
    1261             :   }
    1262             :   else
    1263             :   {
    1264      111790 :     b = gel(dm,1);
    1265      459669 :     for (i = li-1; i > 0; i--)
    1266             :     {
    1267      347879 :       GEN d = bezout(gcoeff(x,i,i),b, &u,NULL);
    1268      347879 :       gcoeff(x,i,i) = d;
    1269      347879 :       FpV_Fp_mul_part_ip(gel(x,i), u, b, i-1);
    1270      347879 :       if (i > 1) b = diviiexact(b,d);
    1271             :     }
    1272             :   }
    1273     1381230 :   x[0] = evaltyp(t_MAT) | evallg(li); /* kill 0 columns / discard accumulator */
    1274     1381230 :   if (flag & hnf_PART) return x;
    1275             : 
    1276     1381230 :   if (!moddiag)
    1277             :   { /* compute optimal value for dm */
    1278      111790 :     b = cgetg(li, t_VEC); gel(b,1) = gcoeff(x,1,1);
    1279      111790 :     for (i = 2; i < li; i++) gel(b,i) = mulii(gel(b,i-1), gcoeff(x,i,i));
    1280      111790 :     dm = b;
    1281             :   }
    1282             : 
    1283     5585730 :   for (i = li-1; i > 0; i--)
    1284             :   {
    1285     4204500 :     GEN diag = gcoeff(x,i,i);
    1286     4204500 :     if (signe(diag) < 0) { gel(x,i) = ZC_neg(gel(x,i)); diag = gcoeff(x,i,i); }
    1287    11809052 :     for (j = i+1; j < li; j++)
    1288             :     {
    1289     7604552 :       b = gcoeff(x,i,j);
    1290     7604552 :       b = center? diviiround(b,diag): truedivii(b, diag);
    1291     7604552 :       if (!signe(b)) continue;
    1292     4319597 :       togglesign(b);
    1293     4319597 :       ZC_lincomb1_inplace(gel(x,j), gel(x,i),b);
    1294     4319597 :       p1 = gel(x,j);
    1295    17938083 :       for (k=1; k<i; k++)
    1296    13618486 :         if (lgefint(gel(p1,k)) > LDM[k]) gel(p1,k) = remii(gel(p1,k), gel(dm,i));
    1297             :     }
    1298     4204500 :     if (gc_needed(av,1))
    1299             :     {
    1300          36 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[3]. i=%ld", i);
    1301          36 :       gerepileall(av, 2, &x, &dm); diag = gcoeff(x,i,i);
    1302             :     }
    1303             :   }
    1304     1381230 :   return x;
    1305             : }
    1306             : GEN
    1307     1863444 : ZM_hnfmodall(GEN x, GEN dm, long flag)
    1308             : {
    1309     1863444 :   pari_sp av = avma;
    1310     1863444 :   return gerepilecopy(av, ZM_hnfmodall_i(x, dm, flag));
    1311             : }
    1312             : GEN
    1313      111783 : ZM_hnfmod(GEN x, GEN d) { return ZM_hnfmodall(x,d,0); }
    1314             : GEN
    1315     1620251 : ZM_hnfmodid(GEN x, GEN d)
    1316     1620251 : { return ZM_hnfmodall(x,d,hnf_MODID); }
    1317             : /* return the column echelon form of x with 1's as pivots,
    1318             :  * P contains the row indices containing the pivots in increasing order */
    1319             : static GEN
    1320     1395231 : FpM_echelon(GEN x, GEN *pP, GEN p)
    1321             : {
    1322             :   pari_sp av;
    1323             :   long iP, li, co, i, j, k, def, ldef;
    1324             :   GEN P;
    1325             : 
    1326     1395231 :   co = lg(x); if (co == 1) { *pP = cgetg(1,t_VECSMALL); return cgetg(1,t_MAT); }
    1327     1395231 :   li = lgcols(x);
    1328     1395231 :   iP = 1;
    1329     1395231 :   *pP = P = cgetg(li, t_VECSMALL);
    1330     1395231 :   av = avma;
    1331     1395231 :   x = FpM_red(x, p);
    1332             : 
    1333     1395231 :   ldef = (li > co)? li - co: 0;
    1334     5626371 :   for (def = co-1,i = li-1; i > ldef; i--)
    1335             :   {
    1336     4231140 :     GEN u = NULL;
    1337     7614552 :     for (k = def; k; k--)
    1338             :     {
    1339     5960496 :       u = gcoeff(x,i,k);
    1340     5960496 :       if (signe(u)) break;
    1341             :     }
    1342     4231140 :     if (!k)
    1343             :     {
    1344     1654056 :       if (--ldef < 0) ldef = 0;
    1345     1654056 :       continue;
    1346             :     }
    1347     2577084 :     P[iP++] = i;
    1348     2577084 :     if (k != def) swap(gel(x,def), gel(x,k));
    1349     2577084 :     if (!equali1(u))
    1350     2075235 :       FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(u,p), p, i-1);
    1351     2577084 :     gcoeff(x, i, def) = gen_1;
    1352     9512847 :     for (j = def-1; j; j--)
    1353             :     { /* zero x[i, 1..def-1] using x[i,def] = 1*/
    1354     6935763 :       GEN xj = gel(x,j), u = gel(xj,i);
    1355     6935763 :       if (!signe(u)) continue;
    1356             : 
    1357     3995458 :       ZC_lincomb1_inplace(xj, gel(x,def), negi(u));
    1358     3995458 :       for (k = 1; k < i; k++) gel(xj,k) = modii(gel(xj,k), p);
    1359     3995458 :       if (gc_needed(av,1))
    1360             :       {
    1361           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"FpM_echelon. i=%ld",i);
    1362           0 :         x = gerepilecopy(av, x);
    1363             :       }
    1364             :     }
    1365     2577084 :     def--;
    1366             :   }
    1367             :   /* rank = iP - 1 */
    1368     1395231 :   setlg(P, iP); vecsmall_sort(P);
    1369     1395231 :   if (co > iP) x += co - iP;
    1370     1395231 :   x[0] = evaltyp(t_MAT) | evallg(iP);
    1371     1395231 :   return x;
    1372             : }
    1373             : /* given x square of maximal rank with 1 or p on diagonal from hnfmodid
    1374             :  * (=> a column containing p has its other entries at 0 ), return the HNF */
    1375             : static GEN
    1376     1383295 : FpM_hnfend(GEN x, GEN p)
    1377             : {
    1378     1383295 :   long i, l = lgcols(x);
    1379     5604270 :   for (i = l-1; i > 0; i--)
    1380             :   {
    1381     4220975 :     GEN diag = gcoeff(x,i,i);
    1382             :     long j;
    1383     4220975 :     if (is_pm1(diag))
    1384     6274167 :       for (j = i+1; j < l; j++)
    1385             :       {
    1386     3720303 :         GEN xj = gel(x,j), b = gel(xj,i);
    1387             :         long k;
    1388     3720303 :         if (!signe(b)) continue;
    1389     2496800 :         ZC_lincomb1_inplace(xj, gel(x,i), negi(b));
    1390    12338915 :         for (k=1; k<i; k++)
    1391     9842115 :           if (lgefint(gel(xj,k)) > 3) gel(xj,k) = remii(gel(xj,k), p);
    1392             :       }
    1393             :     else
    1394     1667111 :       for (j = i+1; j < l; j++) gcoeff(x,i,j) = modii(gcoeff(x,i,j), p);
    1395             :   }
    1396     1383295 :   return x;
    1397             : }
    1398             : GEN
    1399     1395231 : ZM_hnfmodprime(GEN x, GEN p)
    1400             : {
    1401     1395231 :   pari_sp av = avma;
    1402             :   GEN P, y;
    1403             :   long l, lP, i;
    1404     1395231 :   if (lg(x) == 1) return cgetg(1, t_MAT);
    1405     1395231 :   l = lgcols(x);
    1406     1395231 :   x = FpM_echelon(x, &P, p);
    1407     1395231 :   lP = lg(P); /* rank = lP-1 */
    1408     1395231 :   if (lP == l) { avma = av; return matid(l-1); }
    1409     1383295 :   y = scalarmat_shallow(p, l-1);
    1410     1383295 :   for (i = 1; i < lP; i++) gel(y,P[i]) = gel(x,i);
    1411     1383295 :   return gerepilecopy(av, FpM_hnfend(y,p));
    1412             : }
    1413             : 
    1414             : static GEN
    1415      130759 : allhnfmod(GEN x, GEN dm, int flag)
    1416             : {
    1417      130759 :   if (typ(x)!=t_MAT) pari_err_TYPE("allhnfmod",x);
    1418      130759 :   RgM_check_ZM(x, "allhnfmod");
    1419      130759 :   if (isintzero(dm)) return ZM_hnf(x);
    1420      130759 :   return ZM_hnfmodall(x, dm, flag);
    1421             : }
    1422             : GEN
    1423          14 : hnfmod(GEN x, GEN d)
    1424             : {
    1425          14 :   if (typ(d) != t_INT) pari_err_TYPE("mathnfmod",d);
    1426          14 :   return allhnfmod(x, d, 0);
    1427             : }
    1428             : GEN
    1429      130745 : hnfmodid(GEN x, GEN d)
    1430             : {
    1431      130745 :   switch(typ(d))
    1432             :   {
    1433      130731 :     case t_INT: break;
    1434             :     case t_VEC: case t_COL:
    1435          14 :       if (RgV_is_ZV(d)) break;
    1436           0 :     default: pari_err_TYPE("mathnfmodid",d);
    1437             :   }
    1438      130745 :   return allhnfmod(x, d, hnf_MODID);
    1439             : }
    1440             : 
    1441             : /* M a ZM in HNF. Normalize with *centered* residues */
    1442             : GEN
    1443        1183 : ZM_hnfcenter(GEN M)
    1444             : {
    1445        1183 :   long i, j, k, N = lg(M)-1;
    1446        1183 :   pari_sp av = avma;
    1447             : 
    1448        4893 :   for (j=N-1; j>0; j--) /* skip last line */
    1449             :   {
    1450        3710 :     GEN Mj = gel(M,j), a = gel(Mj,j);
    1451       20783 :     for (k = j+1; k <= N; k++)
    1452             :     {
    1453       17073 :       GEN Mk = gel(M,k), q = diviiround(gel(Mk,j), a);
    1454       17073 :       long s = signe(q);
    1455       17073 :       if (!s) continue;
    1456       12299 :       if (is_pm1(q))
    1457             :       {
    1458        2114 :         if (s < 0)
    1459         364 :           for (i = 1; i <= j; i++) gel(Mk,i) = addii(gel(Mk,i), gel(Mj,i));
    1460             :         else
    1461        1750 :           for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), gel(Mj,i));
    1462             :       }
    1463             :       else
    1464       10185 :         for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), mulii(q,gel(Mj,i)));
    1465       12299 :       if (gc_needed(av,1))
    1466             :       {
    1467           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfcenter, j = %ld",j);
    1468           0 :         M = gerepilecopy(av, M);
    1469             :       }
    1470             :     }
    1471             :   }
    1472        1183 :   return M;
    1473             : }
    1474             : 
    1475             : /***********************************************************************/
    1476             : /*                                                                     */
    1477             : /*                 HNFLLL (Havas, Majewski, Mathews)                   */
    1478             : /*                                                                     */
    1479             : /***********************************************************************/
    1480             : 
    1481             : static void
    1482      502921 : Minus(long j, GEN lambda)
    1483             : {
    1484      502921 :   long k, n = lg(lambda);
    1485             : 
    1486      502921 :   for (k=1  ; k<j; k++) togglesign_safe(&gcoeff(lambda,k,j));
    1487      502921 :   for (k=j+1; k<n; k++) togglesign_safe(&gcoeff(lambda,j,k));
    1488      502921 : }
    1489             : 
    1490             : /* index of first non-zero entry */
    1491             : static long
    1492    33711183 : findi(GEN M)
    1493             : {
    1494    33711183 :   long i, n = lg(M);
    1495   227678896 :   for (i=1; i<n; i++)
    1496   208882765 :     if (signe(gel(M,i))) return i;
    1497    18796131 :   return 0;
    1498             : }
    1499             : 
    1500             : static long
    1501    33711183 : findi_normalize(GEN Aj, GEN B, long j, GEN lambda)
    1502             : {
    1503    33711183 :   long r = findi(Aj);
    1504    33711183 :   if (r && signe(gel(Aj,r)) < 0)
    1505             :   {
    1506      502921 :     ZV_togglesign(Aj); if (B) ZV_togglesign(gel(B,j));
    1507      502921 :     Minus(j,lambda);
    1508             :   }
    1509    33711183 :   return r;
    1510             : }
    1511             : 
    1512             : static void
    1513    16855137 : reduce2(GEN A, GEN B, long k, long j, long *row0, long *row1, GEN lambda, GEN D)
    1514             : {
    1515             :   GEN q;
    1516             :   long i;
    1517             : 
    1518    16855137 :   *row0 = findi_normalize(gel(A,j), B,j,lambda);
    1519    16855137 :   *row1 = findi_normalize(gel(A,k), B,k,lambda);
    1520    16855137 :   if (*row0)
    1521     5305257 :     q = truedivii(gcoeff(A,*row0,k), gcoeff(A,*row0,j));
    1522    11549880 :   else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
    1523     2498140 :     q = diviiround(gcoeff(lambda,j,k), gel(D,j));
    1524             :   else
    1525    25906877 :     return;
    1526             : 
    1527     7803397 :   if (signe(q))
    1528             :   {
    1529     4614999 :     GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
    1530     4614999 :     togglesign_safe(&q);
    1531     4614999 :     if (*row0) ZC_lincomb1_inplace(gel(A,k),gel(A,j),q);
    1532     4614999 :     if (B) ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
    1533     4614999 :     gel(Lk,j) = addii(gel(Lk,j), mulii(q,gel(D,j)));
    1534     4614999 :     if (is_pm1(q))
    1535             :     {
    1536     1452680 :       if (signe(q) > 0)
    1537             :       {
    1538     5251089 :         for (i=1; i<j; i++)
    1539     4699848 :           if (signe(gel(Lj,i))) gel(Lk,i) = addii(gel(Lk,i), gel(Lj,i));
    1540             :       }
    1541             :       else
    1542             :       {
    1543     6077517 :         for (i=1; i<j; i++)
    1544     5176078 :           if (signe(gel(Lj,i))) gel(Lk,i) = subii(gel(Lk,i), gel(Lj,i));
    1545             :       }
    1546             :     }
    1547             :     else
    1548             :     {
    1549    16493914 :       for (i=1; i<j; i++)
    1550    13331595 :         if (signe(gel(Lj,i))) gel(Lk,i) = addii(gel(Lk,i), mulii(q,gel(Lj,i)));
    1551             :     }
    1552             :   }
    1553             : }
    1554             : 
    1555             : static void
    1556     2432572 : hnfswap(GEN A, GEN B, long k, GEN lambda, GEN D)
    1557             : {
    1558     2432572 :   GEN t, p1, p2, Lk = gel(lambda,k);
    1559     2432572 :   long i,j,n = lg(A);
    1560             : 
    1561     2432572 :   swap(gel(A,k), gel(A,k-1));
    1562     2432572 :   if (B) swap(gel(B,k), gel(B,k-1));
    1563     2432572 :   for (j=k-2; j; j--) swap(gcoeff(lambda,j,k-1), gel(Lk,j));
    1564    28896182 :   for (i=k+1; i<n; i++)
    1565             :   {
    1566    26463610 :     GEN Li = gel(lambda,i);
    1567    26463610 :     p1 = mulii(gel(Li,k-1), gel(D,k));
    1568    26463610 :     p2 = mulii(gel(Li,k), gel(Lk,k-1));
    1569    26463610 :     t = subii(p1,p2);
    1570             : 
    1571    26463610 :     p1 = mulii(gel(Li,k), gel(D,k-2));
    1572    26463610 :     p2 = mulii(gel(Li,k-1), gel(Lk,k-1));
    1573    26463610 :     gel(Li,k-1) = diviiexact(addii(p1,p2), gel(D,k-1));
    1574    26463610 :     gel(Li,k) = diviiexact(t, gel(D,k-1));
    1575             :   }
    1576     2432572 :   p1 = mulii(gel(D,k-2), gel(D,k));
    1577     2432572 :   p2 = sqri(gel(Lk,k-1));
    1578     2432572 :   gel(D,k-1) = diviiexact(addii(p1,p2), gel(D,k-1));
    1579     2432572 : }
    1580             : 
    1581             : /* reverse row order in matrix A, IN PLACE */
    1582             : static GEN
    1583      102612 : reverse_rows(GEN A)
    1584             : {
    1585      102612 :   long i, j, h, n = lg(A);
    1586      102612 :   if (n == 1) return A;
    1587      102556 :   h = lgcols(A);
    1588      881818 :   for (j=1; j<n; j++)
    1589             :   {
    1590      779262 :     GEN c = gel(A,j);
    1591             :     /* start at (h-1) >>1 : if h = 2i even, no need to swap c[i] and itself */
    1592      779262 :     for (i=(h-1)>>1; i; i--) swap(gel(c,i), gel(c,h-i));
    1593             :   }
    1594      102556 :   return A;
    1595             : }
    1596             : 
    1597             : GEN
    1598       51306 : ZM_hnflll(GEN A, GEN *ptB, int remove)
    1599             : {
    1600       51306 :   pari_sp av = avma;
    1601             : #ifdef HNFLLL_QUALITY
    1602             :   const long m1 = 1, n1 = 1; /* alpha = m1/n1. Maybe 3/4 here ? */
    1603             : #endif
    1604             :   long n, k, kmax;
    1605             :   GEN B, lambda, D;
    1606             : 
    1607       51306 :   n = lg(A);
    1608       51306 :   A = reverse_rows(ZM_copy(A)); /* ZM_copy for in place findi_normalize() */
    1609       51306 :   B = ptB? matid(n-1): NULL;
    1610       51306 :   D = const_vec(n, gen_1) + 1;
    1611       51306 :   lambda = zeromatcopy(n-1,n-1);
    1612       51306 :   k = kmax = 2;
    1613     4324733 :   while (k < n)
    1614             :   {
    1615             :     long row0, row1;
    1616             :     int do_swap;
    1617     4222121 :     reduce2(A,B,k,k-1,&row0,&row1,lambda,D);
    1618     4222121 :     if (row0)
    1619     2673431 :       do_swap = (!row1 || row0 <= row1);
    1620     1548690 :     else if (!row1)
    1621             :     { /* row0 == row1 == 0 */
    1622     1341925 :       pari_sp av1 = avma;
    1623     1341925 :       GEN z = addii(mulii(gel(D,k-2),gel(D,k)), sqri(gcoeff(lambda,k-1,k)));
    1624             : #ifdef HNFLLL_QUALITY
    1625             :       do_swap = (cmpii(mului(n1,z), mului(m1,sqri(gel(D,k-1)))) < 0);
    1626             : #else /* assume m1 = n1 = 1 */
    1627     1341925 :       do_swap = (cmpii(z, sqri(gel(D,k-1))) < 0);
    1628             : #endif
    1629     1341925 :       avma = av1;
    1630             :     }
    1631             :     else
    1632      206765 :       do_swap = 0;
    1633     4222121 :     if (do_swap)
    1634             :     {
    1635     2371695 :       hnfswap(A,B,k,lambda,D);
    1636     2371695 :       if (k > 2) k--;
    1637             :     }
    1638             :     else
    1639             :     {
    1640             :       long i;
    1641    14483442 :       for (i=k-2; i; i--)
    1642             :       {
    1643             :         long row0, row1;
    1644    12633016 :         reduce2(A,B,k,i,&row0,&row1,lambda,D);
    1645    12633016 :         if (gc_needed(av,3))
    1646             :         {
    1647          10 :           GEN b = D-1;
    1648          10 :           if (DEBUGMEM>1) pari_warn(warnmem,"hnflll (reducing), kmax = %ld",kmax);
    1649          10 :           gerepileall(av, B? 4: 3, &A, &lambda, &b, &B);
    1650          10 :           D = b+1;
    1651             :         }
    1652             :       }
    1653     1850426 :       if (++k > kmax) kmax = k;
    1654             :     }
    1655     4222121 :     if (gc_needed(av,3))
    1656             :     {
    1657          28 :       GEN b = D-1;
    1658          28 :       if (DEBUGMEM>1) pari_warn(warnmem,"hnflll, kmax = %ld / %ld",kmax,n-1);
    1659          28 :       gerepileall(av, B? 4: 3, &A, &lambda, &b, &B);
    1660          28 :       D = b+1;
    1661             :     }
    1662             :   }
    1663             :   /* handle trivial case: return negative diag coefficient otherwise */
    1664       51306 :   if (n == 2) (void)findi_normalize(gel(A,1), B,1,lambda);
    1665       51306 :   A = reverse_rows(A);
    1666       51306 :   if (remove)
    1667             :   {
    1668             :     long i;
    1669       59608 :     for (i = 1; i < n; i++)
    1670       54176 :       if (!ZV_equal0(gel(A,i))) break;
    1671        9566 :     remove_0cols(i-1, &A, &B, remove);
    1672             :   }
    1673       51306 :   gerepileall(av, B? 2: 1, &A, &B);
    1674       51306 :   if (B) *ptB = B;
    1675       51306 :   return A;
    1676             : }
    1677             : 
    1678             : GEN
    1679           7 : hnflll(GEN x)
    1680             : {
    1681           7 :   GEN z = cgetg(3, t_VEC);
    1682           7 :   gel(z,1) = ZM_hnflll(x, &gel(z,2), 1);
    1683           7 :   return z;
    1684             : }
    1685             : 
    1686             : /* Variation on HNFLLL: Extended GCD */
    1687             : 
    1688             : static void
    1689      368151 : reduce1(GEN A, GEN B, long k, long j, GEN lambda, GEN D)
    1690             : {
    1691             :   GEN q;
    1692             :   long i;
    1693             : 
    1694      368151 :   if (signe(gel(A,j)))
    1695       55232 :     q = diviiround(gel(A,k),gel(A,j));
    1696      312919 :   else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
    1697        8346 :     q = diviiround(gcoeff(lambda,j,k), gel(D,j));
    1698             :   else
    1699      672724 :     return;
    1700             : 
    1701       63578 :   if (signe(q))
    1702             :   {
    1703       49489 :     GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
    1704       49489 :     togglesign_safe(&q);
    1705       49489 :     gel(A,k) = addii(gel(A,k), mulii(q,gel(A,j)));
    1706       49489 :     ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
    1707       49489 :     gel(Lk,j) = addii(gel(Lk,j), mulii(q,gel(D,j)));
    1708       73484 :     for (i=1; i<j; i++)
    1709       23995 :       if (signe(gel(Lj,i))) gel(Lk,i) = addii(gel(Lk,i), mulii(q,gel(Lj,i)));
    1710             :   }
    1711             : }
    1712             : 
    1713             : static GEN
    1714       50932 : ZV_gcdext_i(GEN A)
    1715             : {
    1716             : #ifdef HNFLLL_QUALITY
    1717             :   const long m1 = 1, n1 = 1; /* alpha = m1/n1. Maybe 3/4 here ? */
    1718             : #endif
    1719       50932 :   long k, n = lg(A);
    1720             :   GEN B, lambda, D;
    1721             : 
    1722       50932 :   if (n == 1) retmkvec2(gen_1, cgetg(1,t_MAT));
    1723       50932 :   A = leafcopy(A);
    1724       50932 :   B = matid(n-1);
    1725       50932 :   lambda = zeromatcopy(n-1,n-1);
    1726       50932 :   D = const_vec(n, gen_1) + 1;
    1727       50932 :   k = 2;
    1728      291161 :   while (k < n)
    1729             :   {
    1730             :     int do_swap;
    1731             : 
    1732      189297 :     reduce1(A,B,k,k-1,lambda,D);
    1733      189297 :     if (signe(gel(A,k-1))) do_swap = 1;
    1734      134065 :     else if (!signe(gel(A,k)))
    1735             :     {
    1736       60836 :       pari_sp av1 = avma;
    1737       60836 :       GEN z = addii(mulii(gel(D,k-2),gel(D,k)), sqri(gcoeff(lambda,k-1,k)));
    1738             : #ifdef HNFLLL_QUALITY
    1739             :       do_swap = (cmpii(mului(n1,z), mului(m1,sqri(gel(D,k-1)))) < 0);
    1740             : #else /* assume m1 = n1 = 1 */
    1741       60836 :       do_swap = (cmpii(z, sqri(gel(D,k-1))) < 0);
    1742             : #endif
    1743       60836 :       avma = av1;
    1744             :     }
    1745       73229 :     else do_swap = 0;
    1746             : 
    1747      189297 :     if (do_swap)
    1748             :     {
    1749       60877 :       hnfswap(A,B,k,lambda,D);
    1750       60877 :       if (k > 2) k--;
    1751             :     }
    1752             :     else
    1753             :     {
    1754             :       long i;
    1755      128420 :       for (i=k-2; i; i--) reduce1(A,B,k,i,lambda,D);
    1756      128420 :       k++;
    1757             :     }
    1758             :   }
    1759       50932 :   if (signe(gel(A,n-1)) < 0)
    1760             :   {
    1761        6675 :     gel(A,n-1) = negi(gel(A,n-1));
    1762        6675 :     ZV_togglesign(gel(B,n-1));
    1763             :   }
    1764       50932 :   return mkvec2(gel(A,n-1), B);
    1765             : }
    1766             : GEN
    1767       50918 : ZV_extgcd(GEN A)
    1768             : {
    1769       50918 :   pari_sp av = avma;
    1770       50918 :   return gerepilecopy(av, ZV_gcdext_i(A));
    1771             : }
    1772             : /* as ZV_extgcd, transforming the gcd into a t_MAT, for mathnf0 */
    1773             : static GEN
    1774          21 : ZV_hnfgcdext(GEN A)
    1775             : {
    1776          21 :   pari_sp av = avma;
    1777             :   GEN z;
    1778          21 :   if (lg(A) == 1) retmkvec2(cgetg(1,t_MAT),cgetg(1,t_MAT));
    1779          14 :   z = ZV_gcdext_i(A);
    1780          14 :   gel(z,1) = mkmat(mkcol(gel(z,1)));
    1781          14 :   return gerepilecopy(av, z);
    1782             : }
    1783             : 
    1784             : /* HNF with permutation. */
    1785             : GEN
    1786         238 : ZM_hnfperm(GEN A, GEN *ptU, GEN *ptperm)
    1787             : {
    1788             :   GEN U, c, l, perm, d, p, q, b;
    1789         238 :   pari_sp av = avma, av1;
    1790             :   long r, t, i, j, j1, k, m, n;
    1791             : 
    1792         238 :   n = lg(A)-1;
    1793         238 :   if (!n)
    1794             :   {
    1795           7 :     if (ptU) *ptU = cgetg(1,t_MAT);
    1796           7 :     if (ptperm) *ptperm = cgetg(1,t_VEC);
    1797           7 :     return cgetg(1, t_MAT);
    1798             :   }
    1799         231 :   m = nbrows(A);
    1800         231 :   c = zero_zv(m);
    1801         231 :   l = zero_zv(n);
    1802         231 :   perm = cgetg(m+1, t_VECSMALL);
    1803         231 :   av1 = avma;
    1804         231 :   A = RgM_shallowcopy(A);
    1805         231 :   U = ptU? matid(n): NULL;
    1806             :   /* U base change matrix : A0*U = A all along */
    1807        1148 :   for (r=0, k=1; k <= n; k++)
    1808             :   {
    1809        2849 :     for (j=1; j<k; j++)
    1810             :     {
    1811        1932 :       if (!l[j]) continue;
    1812        1799 :       t=l[j]; b=gcoeff(A,t,k);
    1813        1799 :       if (!signe(b)) continue;
    1814             : 
    1815         525 :       ZC_elem(b,gcoeff(A,t,j), A,U,k,j);
    1816         525 :       d = gcoeff(A,t,j);
    1817         525 :       if (signe(d) < 0)
    1818             :       {
    1819           0 :         ZV_neg_inplace(gel(A,j));
    1820           0 :         if (U) ZV_togglesign(gel(U,j));
    1821           0 :         d = gcoeff(A,t,j);
    1822             :       }
    1823        1253 :       for (j1=1; j1<j; j1++)
    1824             :       {
    1825         728 :         if (!l[j1]) continue;
    1826         714 :         q = truedivii(gcoeff(A,t,j1),d);
    1827         714 :         if (!signe(q)) continue;
    1828             : 
    1829         329 :         togglesign(q);
    1830         329 :         ZC_lincomb1_inplace(gel(A,j1), gel(A,j), q);
    1831         329 :         if (U) ZC_lincomb1_inplace(gel(U,j1), gel(U,j), q);
    1832             :       }
    1833             :     }
    1834         917 :     t = m; while (t && (c[t] || !signe(gcoeff(A,t,k)))) t--;
    1835         917 :     if (t)
    1836             :     {
    1837         819 :       p = gcoeff(A,t,k);
    1838       13846 :       for (i=t-1; i; i--)
    1839             :       {
    1840       13027 :         q = gcoeff(A,i,k);
    1841       13027 :         if (signe(q) && abscmpii(p,q) > 0) { p = q; t = i; }
    1842             :       }
    1843         819 :       perm[++r] = l[k] = t; c[t] = k;
    1844         819 :       if (signe(p) < 0)
    1845             :       {
    1846          97 :         ZV_neg_inplace(gel(A,k));
    1847          97 :         if (U) ZV_togglesign(gel(U,k));
    1848          97 :         p = gcoeff(A,t,k);
    1849             :       }
    1850             :       /* p > 0 */
    1851        2408 :       for (j=1; j<k; j++)
    1852             :       {
    1853        1589 :         if (!l[j]) continue;
    1854        1568 :         q = truedivii(gcoeff(A,t,j),p);
    1855        1568 :         if (!signe(q)) continue;
    1856             : 
    1857         244 :         togglesign(q);
    1858         244 :         ZC_lincomb1_inplace(gel(A,j), gel(A,k), q);
    1859         244 :         if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,k), q);
    1860             :       }
    1861             :     }
    1862         917 :     if (gc_needed(av1,1))
    1863             :     {
    1864           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"hnfperm, k=%ld",k);
    1865           0 :       gerepileall(av1, U? 2: 1, &A, &U);
    1866             :     }
    1867             :   }
    1868         231 :   if (r < m)
    1869             :   {
    1870        3227 :     for (i=1,k=r; i<=m; i++)
    1871        3052 :       if (!c[i]) perm[++k] = i;
    1872             :   }
    1873             : 
    1874             : /* We have A0*U=A, U in Gl(n,Z)
    1875             :  * basis for Im(A):  columns of A s.t l[j]>0 (r   cols)
    1876             :  * basis for Ker(A): columns of U s.t l[j]=0 (n-r cols) */
    1877         231 :   p = cgetg(r+1,t_MAT);
    1878         231 :   for (i=1; i<=m/2; i++) lswap(perm[i], perm[m+1-i]);
    1879         231 :   if (U)
    1880             :   {
    1881          70 :     GEN u = cgetg(n+1,t_MAT);
    1882         322 :     for (t=1,k=r,j=1; j<=n; j++)
    1883         252 :       if (l[j])
    1884             :       {
    1885         154 :         u[k + n-r] = U[j];
    1886         154 :         gel(p,k--) = vecpermute(gel(A,j), perm);
    1887             :       }
    1888             :       else
    1889          98 :         u[t++] = U[j];
    1890          70 :     *ptU = u;
    1891          70 :     if (ptperm) *ptperm = perm;
    1892          70 :     gerepileall(av, ptperm? 3: 2, &p, ptU, ptperm);
    1893             :   }
    1894             :   else
    1895             :   {
    1896         826 :     for (k=r,j=1; j<=n; j++)
    1897         665 :       if (l[j]) gel(p,k--) = vecpermute(gel(A,j), perm);
    1898         161 :     if (ptperm) *ptperm = perm;
    1899         161 :     gerepileall(av, ptperm? 2: 1, &p, ptperm);
    1900             :   }
    1901         231 :   return p;
    1902             : }
    1903             : 
    1904             : GEN
    1905         154 : ZM_hnf_knapsack(GEN x)
    1906             : {
    1907         154 :   GEN t, perm, H = ZM_hnfperm(x,NULL,&perm);
    1908         154 :   long i,j, l = lg(H), h = lgcols(H);
    1909        1743 :   for (i=1; i<h; i++)
    1910             :   {
    1911        1673 :     int fl = 0;
    1912       10542 :     for (j=1; j<l; j++)
    1913             :     {
    1914        8953 :       t = gcoeff(H,i,j);
    1915        8953 :       if (signe(t))
    1916             :       {
    1917        1701 :         if (!is_pm1(t) || fl) return NULL;
    1918        1617 :         fl = 1;
    1919             :       }
    1920             :     }
    1921             :   }
    1922          70 :   return rowpermute(H, perm_inv(perm));
    1923             : }
    1924             : 
    1925             : GEN
    1926          14 : hnfperm(GEN A)
    1927             : {
    1928          14 :   GEN y = cgetg(4, t_VEC);
    1929          14 :   gel(y,1) = ZM_hnfperm(A, &gel(y,2), &gel(y,3));
    1930          14 :   return y;
    1931             : }
    1932             : 
    1933             : /* Hermite Normal Form, with base change matrix if ptB != NULL.
    1934             :  * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
    1935             :  * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
    1936             : GEN
    1937      115840 : ZM_hnfall_i(GEN A, GEN *ptB, long remove)
    1938             : {
    1939             :   pari_sp av;
    1940             :   long m, n, r, i, j, k, li;
    1941             :   GEN B, c, h, a;
    1942             : 
    1943      115840 :   RgM_dimensions(A, &m,&n);
    1944      115840 :   if (!n)
    1945             :   {
    1946           7 :     if (ptB) *ptB = cgetg(1,t_MAT);
    1947           7 :     return cgetg(1,t_MAT);
    1948             :   }
    1949      115833 :   c = zero_zv(m);
    1950      115833 :   h = const_vecsmall(n, m);
    1951      115833 :   av = avma;
    1952      115833 :   A = RgM_shallowcopy(A);
    1953      115833 :   B = ptB? matid(n): NULL;
    1954      115833 :   r = n+1;
    1955      391112 :   for (li=m; li; li--)
    1956             :   {
    1957      879237 :     for (j=1; j<r; j++)
    1958             :     {
    1959     1126838 :       for (i=h[j]; i>li; i--)
    1960             :       {
    1961      247804 :         a = gcoeff(A,i,j);
    1962      247804 :         k = c[i];
    1963             :         /* zero a = Aij  using  Aik */
    1964      247804 :         if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B,j,k);
    1965      247804 :         ZM_reduce(A,B, i,k); /* ensure reduced entries */
    1966      247804 :         if (gc_needed(av,1))
    1967             :         {
    1968           0 :           if (DEBUGMEM>1)
    1969           0 :             pari_warn(warnmem,"ZM_hnfall[1], li = %ld, j = %ld", li, j);
    1970           0 :           gerepileall(av, B? 2: 1, &A, &B);
    1971             :         }
    1972             :       }
    1973      879034 :       if (signe( gcoeff(A,li,j) )) break;
    1974      603958 :       h[j] = li-1;
    1975             :     }
    1976      275279 :     if (j == r) continue;
    1977      275076 :     r--;
    1978      275076 :     if (j < r) /* A[j] != 0 */
    1979             :     {
    1980      199321 :       swap(gel(A,j), gel(A,r));
    1981      199321 :       if (B) swap(gel(B,j), gel(B,r));
    1982      199321 :       h[j] = h[r]; h[r] = li; c[li] = r;
    1983             :     }
    1984      275076 :     if (signe(gcoeff(A,li,r)) < 0)
    1985             :     {
    1986       62466 :       ZV_neg_inplace(gel(A,r));
    1987       62466 :       if (B) ZV_togglesign(gel(B,r));
    1988             :     }
    1989      275076 :     ZM_reduce(A,B, li,r);
    1990      275076 :     if (gc_needed(av,1))
    1991             :     {
    1992           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[2], li = %ld", li);
    1993           0 :       gerepileall(av, B? 2: 1, &A, &B);
    1994             :     }
    1995             :   }
    1996             : 
    1997      115833 :   if (DEBUGLEVEL>5) err_printf("\nhnfall, final phase: ");
    1998      115833 :   r--; /* first r cols are in the image the n-r (independent) last ones */
    1999      686837 :   for (j=1; j<=r; j++)
    2000     1364625 :     for (i=h[j]; i; i--)
    2001             :     {
    2002      793621 :       a = gcoeff(A,i,j);
    2003      793621 :       k = c[i];
    2004      793621 :       if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B, j,k);
    2005      793621 :       ZM_reduce(A,B, i,k); /* ensure reduced entries, even if a = 0 */
    2006      793621 :       if (gc_needed(av,1))
    2007             :       {
    2008           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[3], j = %ld", j);
    2009           0 :         gerepileall(av, B? 2: 1, &A, &B);
    2010             :       }
    2011             :     }
    2012      115833 :   if (DEBUGLEVEL>5) err_printf("\n");
    2013      115833 :   if (remove) remove_0cols(r, &A, &B, remove);
    2014      115833 :   if (ptB) *ptB = B;
    2015      115833 :   return A;
    2016             : }
    2017             : GEN
    2018        3083 : ZM_hnfall(GEN A, GEN *ptB, long remove)
    2019             : {
    2020        3083 :   pari_sp av = avma;
    2021        3083 :   A = ZM_hnfall_i(A, ptB, remove);
    2022        3083 :   gerepileall(av, ptB? 2: 1, &A, ptB);
    2023        3083 :   return A;
    2024             : }
    2025             : 
    2026             : GEN
    2027          14 : hnfall(GEN x)
    2028             : {
    2029          14 :   GEN z = cgetg(3, t_VEC);
    2030          14 :   gel(z,1) = ZM_hnfall(x, (GEN*)(z+2), 1);
    2031          14 :   return z;
    2032             : }
    2033             : GEN
    2034          14 : hnf(GEN x) { return mathnf0(x,0); }
    2035             : 
    2036             : /* C = A^(-1)t where A and C are integral, A is upper triangular, t t_INT */
    2037             : GEN
    2038       20783 : hnf_invscale(GEN A, GEN t)
    2039             : {
    2040       20783 :   long n = lg(A)-1, i,j,k;
    2041       20783 :   GEN m, c = cgetg(n+1,t_MAT);
    2042             : 
    2043       20783 :   if (!n) return c;
    2044      114940 :   for (k=1; k<=n; k++)
    2045             :   { /* cf hnf_divscale with B = id, thus b = e_k */
    2046       94157 :     GEN u = cgetg(n+1, t_COL);
    2047       94157 :     pari_sp av = avma;
    2048       94157 :     gel(c,k) = u;
    2049       94157 :     gel(u,n) = k == n? gerepileuptoint(av, diviiexact(t, gcoeff(A,n,n))): gen_0;
    2050      621397 :     for (i=n-1; i>0; i--)
    2051             :     {
    2052      527240 :       av = avma; m = i == k? t: gen_0;
    2053      527240 :       for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2054      527240 :       gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
    2055             :     }
    2056             :   }
    2057       20783 :   return c;
    2058             : }
    2059             : 
    2060             : /* C = A^(-1)(tB) where A, B, C are integral, A is upper triangular, t t_INT */
    2061             : GEN
    2062      181275 : hnf_divscale(GEN A, GEN B, GEN t)
    2063             : {
    2064      181275 :   long n = lg(A)-1, i,j,k;
    2065      181275 :   GEN m, c = cgetg(n+1,t_MAT);
    2066             : 
    2067      181275 :   if (!n) return c;
    2068      710188 :   for (k=1; k<=n; k++)
    2069             :   {
    2070      528913 :     GEN u = cgetg(n+1, t_COL), b = gel(B,k);
    2071      528913 :     pari_sp av = avma;
    2072      528913 :     gel(c,k) = u; m = mulii(gel(b,n),t);
    2073      528913 :     gel(u,n) = gerepileuptoint(av, diviiexact(m, gcoeff(A,n,n)));
    2074     2266947 :     for (i=n-1; i>0; i--)
    2075             :     {
    2076     1738034 :       av = avma; m = mulii(gel(b,i),t);
    2077     1738034 :       for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2078     1738034 :       gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
    2079             :     }
    2080             :   }
    2081      181275 :   return c;
    2082             : }
    2083             : 
    2084             : /* A, B integral upper HNF. A^(-1) B integral ? */
    2085             : int
    2086          91 : hnfdivide(GEN A, GEN B)
    2087             : {
    2088          91 :   pari_sp av = avma;
    2089          91 :   long n = lg(A)-1, i,j,k;
    2090             :   GEN u, b, m, r;
    2091             : 
    2092          91 :   if (!n) return 1;
    2093          91 :   if (lg(B)-1 != n) pari_err_DIM("hnfdivide");
    2094          91 :   u = cgetg(n+1, t_COL);
    2095         413 :   for (k=1; k<=n; k++)
    2096             :   {
    2097         322 :     b = gel(B,k);
    2098         322 :     m = gel(b,k);
    2099         322 :     gel(u,k) = dvmdii(m, gcoeff(A,k,k), &r);
    2100         322 :     if (r != gen_0) { avma = av; return 0; }
    2101         798 :     for (i=k-1; i>0; i--)
    2102             :     {
    2103         476 :       m = gel(b,i);
    2104         476 :       for (j=i+1; j<=k; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2105         476 :       m = dvmdii(m, gcoeff(A,i,i), &r);
    2106         476 :       if (r != gen_0) { avma = av; return 0; }
    2107         476 :       gel(u,i) = m;
    2108             :     }
    2109             :   }
    2110          91 :   avma = av; return 1;
    2111             : }
    2112             : 
    2113             : /* A upper HNF, b integral vector. Return A^(-1) b if integral,
    2114             :  * NULL otherwise. Assume #A[,1] = #b. */
    2115             : GEN
    2116       51940 : hnf_invimage(GEN A, GEN b)
    2117             : {
    2118       51940 :   pari_sp av = avma;
    2119       51940 :   long n = lg(A)-1, m, i, k;
    2120             :   GEN u, r;
    2121             : 
    2122       51940 :   if (!n) return lg(b)==1? cgetg(1,t_COL):NULL;
    2123       51898 :   m = nbrows(A); /* m >= n */
    2124       51898 :   u = cgetg(n+1, t_COL);
    2125       85645 :   for (i = n, k = m; k > 0; k--)
    2126             :   {
    2127       85645 :     pari_sp av2 = avma;
    2128             :     long j;
    2129       85645 :     GEN t = gel(b,k), Aki = gcoeff(A,k,i);
    2130       85645 :     if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
    2131       85645 :     for (j=i+1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
    2132       85645 :     if (!signe(Aki))
    2133             :     {
    2134           0 :       if (signe(t)) { avma = av;return NULL; }
    2135           0 :       avma = av2; gel(u,i) = gen_0; continue;
    2136             :     }
    2137       85645 :     t = dvmdii(t, Aki, &r);
    2138       85645 :     if (r != gen_0) { avma = av; return NULL; }
    2139       61390 :     gel(u,i) = gerepileuptoint(av2, t);
    2140       61390 :     if (--i == 0) break;
    2141             :   }
    2142             :   /* If there is a solution, it must be u. Check remaining equations */
    2143       55286 :   for (; k > 0; k--)
    2144             :   {
    2145       27643 :     pari_sp av2 = avma;
    2146             :     long j;
    2147       27643 :     GEN t = gel(b,k);
    2148       27643 :     if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
    2149       27643 :     for (j=1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
    2150       27643 :     if (signe(t)) { avma = av;return NULL; }
    2151       27643 :     avma = av2;
    2152             :   }
    2153       27643 :   return u;
    2154             : }
    2155             : 
    2156             : /* A upper HNF, B integral matrix or column. Return A^(-1) B if integral,
    2157             :  * NULL otherwise */
    2158             : GEN
    2159       45605 : hnf_solve(GEN A, GEN B)
    2160             : {
    2161             :   pari_sp av;
    2162             :   long i, l;
    2163             :   GEN C;
    2164             : 
    2165       45605 :   if (typ(B) == t_COL) return hnf_invimage(A, B);
    2166       40271 :   av = avma; C = cgetg_copy(B, &l);
    2167       63399 :   for (i = 1; i < l; i++) {
    2168       42189 :     GEN c = hnf_invimage(A, gel(B,i));
    2169       42189 :     if (!c) { avma = av; return NULL; }
    2170       23128 :     gel(C,i) = c;
    2171             :   }
    2172       21210 :   return C;
    2173             : }
    2174             : 
    2175             : /***************************************************************/
    2176             : /**                                                           **/
    2177             : /**               SMITH NORMAL FORM REDUCTION                 **/
    2178             : /**                                                           **/
    2179             : /***************************************************************/
    2180             : 
    2181             : static GEN
    2182           0 : trivsmith(long all)
    2183             : {
    2184             :   GEN z;
    2185           0 :   if (!all) return cgetg(1,t_VEC);
    2186           0 :   z=cgetg(4,t_VEC);
    2187           0 :   gel(z,1) = cgetg(1,t_MAT);
    2188           0 :   gel(z,2) = cgetg(1,t_MAT);
    2189           0 :   gel(z,3) = cgetg(1,t_MAT); return z;
    2190             : }
    2191             : 
    2192             : static void
    2193      136899 : snf_pile(pari_sp av, GEN *x, GEN *U, GEN *V)
    2194             : {
    2195             :   GEN *gptr[3];
    2196      136899 :   int c = 1; gptr[0]=x;
    2197      136899 :   if (*U) gptr[c++] = U;
    2198      136899 :   if (*V) gptr[c++] = V;
    2199      136899 :   gerepilemany(av,gptr,c);
    2200      136899 : }
    2201             : 
    2202             : static GEN
    2203       77303 : bezout_step(GEN *pa, GEN *pb, GEN *pu, GEN *pv)
    2204             : {
    2205       77303 :   GEN a = *pa, b = *pb, d;
    2206       77303 :   if (absequalii(a,b))
    2207             :   {
    2208       29295 :     long sa = signe(a), sb = signe(b);
    2209       29295 :     *pv = gen_0;
    2210       29295 :     if (sb == sa) {
    2211       29099 :       *pa = *pb = gen_1;
    2212       29099 :       if (sa > 0) {*pu=gen_1; return a;} else {*pu=gen_m1; return absi(a);}
    2213             :     }
    2214         196 :     if (sa > 0) { *pa = *pu = gen_1; *pb = gen_m1; return a; }
    2215           7 :     *pa = *pu = gen_m1; *pb = gen_1; return b;
    2216             :   }
    2217       48008 :   d = bezout(a,b, pu,pv);
    2218       48008 :   *pa = diviiexact(a, d);
    2219       48008 :   *pb = diviiexact(b, d); return d;
    2220             : }
    2221             : 
    2222             : static int
    2223      362124 : negcmpii(void *E, GEN x, GEN y) { (void)E; return -cmpii(x,y); }
    2224             : 
    2225             : /* does b = x[i,i] divide all entries in x[1..i-1,1..i-1] ? If so, return 0;
    2226             :  * else return the index of a problematic row */
    2227             : static long
    2228      156849 : ZM_snf_no_divide(GEN x, long i)
    2229             : {
    2230      156849 :   GEN b = gcoeff(x,i,i);
    2231             :   long j, k;
    2232             : 
    2233      156849 :   if (!signe(b))
    2234             :   { /* impossible in the current implementation : x square of maximal rank */
    2235           0 :     for (k = 1; k < i; k++)
    2236           0 :       for (j = 1; j < i; j++)
    2237           0 :         if (signe(gcoeff(x,k,j))) return k;
    2238           0 :     return 0;
    2239             :   }
    2240      156849 :   if (is_pm1(b)) return 0;
    2241      285652 :   for (k=1; k<i; k++)
    2242             :   {
    2243     1051612 :     for (j=1; j<i; j++)
    2244      844312 :       if (signe(remii(gcoeff(x,k,j),b))) return k;
    2245             :   }
    2246       75608 :   return 0;
    2247             : }
    2248             : 
    2249             : /* Return the SNF D of matrix X. If ptU/ptV non-NULL set them to U/V
    2250             :  * to that D = UXV */
    2251             : GEN
    2252       76063 : ZM_snfall_i(GEN x, GEN *ptU, GEN *ptV, int return_vec)
    2253             : {
    2254       76063 :   pari_sp av0 = avma, av;
    2255             :   long i, j, k, m0, m, n0, n;
    2256       76063 :   GEN p1, u, v, U, V, V0, mdet, ys, perm = NULL;
    2257             : 
    2258       76063 :   n0 = n = lg(x)-1;
    2259       76063 :   if (!n) {
    2260        3620 :     if (ptU) *ptU = cgetg(1,t_MAT);
    2261        3620 :     if (ptV) *ptV = cgetg(1,t_MAT);
    2262        3620 :     return cgetg(1, return_vec? t_VEC: t_MAT);
    2263             :   }
    2264       72443 :   av = avma;
    2265       72443 :   m0 = m = nbrows(x);
    2266             : 
    2267       72443 :   U = ptU? gen_1: NULL; /* TRANSPOSE of row transform matrix [act on columns]*/
    2268       72443 :   V = ptV? gen_1: NULL;
    2269       72443 :   V0 = NULL;
    2270       72443 :   x = RgM_shallowcopy(x);
    2271       72443 :   if (m == n && ZM_ishnf(x))
    2272             :   {
    2273       64652 :     mdet = ZM_det_triangular(x);
    2274       64652 :     if (V) *ptV = matid(n);
    2275             :   }
    2276             :   else
    2277             :   {
    2278        7791 :     mdet = ZM_detmult(x);
    2279        7791 :     if (signe(mdet))
    2280             :     {
    2281        7770 :       if (!V)
    2282          77 :         p1 = ZM_hnfmod(x,mdet);
    2283             :       else
    2284             :       {
    2285        7693 :         if (m == n)
    2286             :         {
    2287        7672 :           p1 = ZM_hnfmod(x,mdet);
    2288        7672 :           *ptV = RgM_solve(x,p1);
    2289             :         }
    2290             :         else
    2291          21 :           p1 = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
    2292             :       }
    2293        7770 :       mdet = ZM_det_triangular(p1);
    2294             :     }
    2295             :     else
    2296          21 :       p1 = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
    2297        7791 :     x = p1;
    2298             :   }
    2299       72443 :   n = lg(x)-1;
    2300       72443 :   if (V)
    2301             :   {
    2302       68600 :     V = *ptV;
    2303       68600 :     if (n != n0)
    2304             :     {
    2305          21 :       V0 = vecslice(V, 1, n0 - n); /* kernel */
    2306          21 :       V  = vecslice(V, n0-n+1, n0);
    2307          21 :       av = avma;
    2308             :     }
    2309             :   }
    2310             :   /* independent columns */
    2311       72443 :   if (!signe(mdet))
    2312             :   {
    2313          21 :     if (n)
    2314             :     {
    2315          21 :       x = ZM_snfall_i(shallowtrans(x), ptV, ptU, return_vec); /* swap ptV,ptU */
    2316          21 :       if (typ(x) == t_MAT && n != m) x = shallowtrans(x);
    2317          21 :       if (V) V = ZM_mul(V, shallowtrans(*ptV));
    2318          21 :       if (U) U = *ptU; /* TRANSPOSE */
    2319             :     }
    2320             :     else /* 0 matrix */
    2321             :     {
    2322           0 :       x = cgetg(1,t_MAT);
    2323           0 :       if (V) V = cgetg(1, t_MAT);
    2324           0 :       if (U) U = matid(m);
    2325             :     }
    2326          21 :     goto THEEND;
    2327             :   }
    2328       72422 :   if (U) U = matid(n);
    2329             : 
    2330             :   /* square, maximal rank n */
    2331       72422 :   p1 = gen_indexsort(RgM_diagonal_shallow(x), NULL, &negcmpii);
    2332       72422 :   ys = cgetg(n+1,t_MAT);
    2333       72422 :   for (j=1; j<=n; j++) gel(ys,j) = vecpermute(gel(x, p1[j]), p1);
    2334       72422 :   x = ys;
    2335       72422 :   if (U) U = vecpermute(U, p1);
    2336       72422 :   if (V) V = vecpermute(V, p1);
    2337             : 
    2338       72422 :   p1 = ZM_hnfmod(x, mdet);
    2339       72422 :   if (V) V = ZM_mul(V, RgM_solve(x,p1));
    2340       72422 :   x = p1;
    2341             : 
    2342       72422 :   if (DEBUGLEVEL>7) err_printf("starting SNF loop");
    2343      453054 :   for (i=n; i>1; i--)
    2344             :   {
    2345      154105 :     if (DEBUGLEVEL>7) err_printf("\ni = %ld: ",i);
    2346             :     for(;;)
    2347             :     {
    2348      211360 :       int c = 0;
    2349             :       GEN a, b;
    2350      928103 :       for (j=i-1; j>=1; j--)
    2351             :       {
    2352      716743 :         b = gcoeff(x,i,j); if (!signe(b)) continue;
    2353        8864 :         a = gcoeff(x,i,i);
    2354        8864 :         ZC_elem(b, a, x,V, j,i);
    2355        8864 :         if (gc_needed(av,1))
    2356             :         {
    2357           0 :           if (DEBUGMEM>1) pari_warn(warnmem,"[1]: ZM_snfall i = %ld", i);
    2358           0 :           snf_pile(av, &x,&U,&V);
    2359             :         }
    2360             :       }
    2361      211360 :       if (DEBUGLEVEL>7) err_printf("; ");
    2362      928103 :       for (j=i-1; j>=1; j--)
    2363             :       {
    2364             :         GEN d;
    2365      716743 :         b = gcoeff(x,j,i); if (!signe(b)) continue;
    2366       77303 :         a = gcoeff(x,i,i);
    2367       77303 :         d = bezout_step(&a, &b, &u, &v);
    2368      451369 :         for (k = 1; k < i; k++)
    2369             :         {
    2370      374066 :           GEN t = addii(mulii(u,gcoeff(x,i,k)),mulii(v,gcoeff(x,j,k)));
    2371      748132 :           gcoeff(x,j,k) = subii(mulii(a,gcoeff(x,j,k)),
    2372      374066 :                                 mulii(b,gcoeff(x,i,k)));
    2373      374066 :           gcoeff(x,i,k) = t;
    2374             :         }
    2375       77303 :         gcoeff(x,j,i) = gen_0;
    2376       77303 :         gcoeff(x,i,i) = d;
    2377       77303 :         if (U) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
    2378       77303 :         if (gc_needed(av,1))
    2379             :         {
    2380           0 :           if (DEBUGMEM>1) pari_warn(warnmem,"[2]: ZM_snfall, i = %ld", i);
    2381           0 :           snf_pile(av, &x,&U,&V);
    2382             :         }
    2383       77303 :         c = 1;
    2384             :       }
    2385      211360 :       if (!c)
    2386             :       {
    2387      156849 :         k = ZM_snf_no_divide(x, i);
    2388      156849 :         if (!k) break;
    2389             : 
    2390             :         /* x[k,j] != 0 mod b */
    2391        9261 :         for (j=1; j<=i; j++)
    2392        6517 :           gcoeff(x,i,j) = addii(gcoeff(x,i,j),gcoeff(x,k,j));
    2393        2744 :         if (U) gel(U,i) = gadd(gel(U,i),gel(U,k));
    2394             :       }
    2395       57255 :       if (gc_needed(av,1))
    2396             :       {
    2397           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"[3]: ZM_snfall");
    2398           0 :         snf_pile(av, &x,&U,&V);
    2399             :       }
    2400       57255 :     }
    2401             :   }
    2402       72422 :   if (DEBUGLEVEL>7) err_printf("\n");
    2403      298949 :   for (k=1; k<=n; k++)
    2404      226527 :     if (signe(gcoeff(x,k,k)) < 0)
    2405             :     {
    2406         112 :       if (V) ZV_togglesign(gel(V,k));
    2407         112 :       togglesign(gcoeff(x,k,k));
    2408             :     }
    2409             : THEEND:
    2410       72443 :   if (return_vec)
    2411             :   {
    2412       67226 :     long l = lg(x)-1;
    2413       67226 :     if (typ(x) == t_MAT) x = RgM_diagonal_shallow(x);
    2414       67226 :     if (m0 > l) x = shallowconcat(zerovec(m0-l), x);
    2415             :   }
    2416             : 
    2417       72443 :   if (V0)
    2418             :   {
    2419          21 :     if (!return_vec) x = shallowconcat(zeromat(m,n0-n), x);
    2420          21 :     if (V) V = shallowconcat(V0, V);
    2421             :   }
    2422       72443 :   if (U)
    2423             :   {
    2424       56079 :     U = shallowtrans(U);
    2425       56079 :     if (perm) U = vecpermute(U, perm_inv(perm));
    2426             :   }
    2427       72443 :   snf_pile(av0, &x,&U,&V);
    2428       72443 :   if (ptU) *ptU = U;
    2429       72443 :   if (ptV) *ptV = V;
    2430       72443 :   return x;
    2431             : }
    2432             : GEN
    2433        8374 : ZM_snfall(GEN x, GEN *U, GEN *V) { return ZM_snfall_i(x, U, V, 0); }
    2434             : GEN
    2435         217 : ZM_snf(GEN x) { return ZM_snfall_i(x, NULL,NULL, 1); }
    2436             : 
    2437             : GEN
    2438         322 : smith(GEN x) {
    2439         322 :   if (typ(x)!=t_MAT) pari_err_TYPE("smith",x);
    2440         322 :   RgM_check_ZM(x, "smith");
    2441         322 :   return ZM_snfall_i(x, NULL,NULL, 1);
    2442             : }
    2443             : GEN
    2444          21 : smithall(GEN x)
    2445             : {
    2446          21 :   GEN z = cgetg(4, t_VEC);
    2447          21 :   if (typ(x)!=t_MAT) pari_err_TYPE("smithall",x);
    2448          21 :   RgM_check_ZM(x, "smithall");
    2449          21 :   gel(z,3) = ZM_snfall_i(x, (GEN*)(z+1),(GEN*)(z+2), 0);
    2450          21 :   return z;
    2451             : }
    2452             : 
    2453             : void
    2454      128456 : ZM_snfclean(GEN d, GEN u, GEN v)
    2455             : {
    2456      128456 :   long i, c, l = lg(d);
    2457             : 
    2458      128456 :   if (typ(d) == t_VEC)
    2459      128456 :     for (c=1; c<l; c++) { GEN t = gel(d,c); if (is_pm1(t)) break; }
    2460             :   else
    2461             :   {
    2462           0 :     for (c=1; c<l; c++) { GEN t = gcoeff(d,c,c); if (is_pm1(t)) break; }
    2463           0 :     if (c < l) for (i = 1; i < c; i++) setlg(gel(d,i), c);
    2464             :   }
    2465      128456 :   setlg(d, c);
    2466      128456 :   if (u) for (i=1; i<l; i++) setlg(gel(u,i), c);
    2467      128456 :   if (v) setlg(v, c);
    2468      128456 : }
    2469             : 
    2470             : /* Assume z was computed by [g]smithall(). Remove the 1s on the diagonal */
    2471             : GEN
    2472         539 : smithclean(GEN z)
    2473             : {
    2474             :   long i, j, h, l, c, d;
    2475             :   GEN U, V, y, D, t;
    2476             : 
    2477         539 :   if (typ(z) != t_VEC) pari_err_TYPE("smithclean",z);
    2478         539 :   l = lg(z); if (l == 1) return cgetg(1,t_VEC);
    2479         532 :   U = gel(z,1);
    2480         532 :   if (l != 4 || typ(U) != t_MAT)
    2481             :   { /* assume z = vector of elementary divisors */
    2482        1470 :     for (c=1; c<l; c++)
    2483        1309 :       if (gequal1(gel(z,c))) break;
    2484         511 :     return gcopy_lg(z, c);
    2485             :   }
    2486          21 :   V = gel(z,2);
    2487          21 :   D = gel(z,3);
    2488          21 :   l = lg(D);
    2489          21 :   if (l == 1) return gcopy(z);
    2490          21 :   h = lgcols(D);
    2491          21 :   if (h > l)
    2492             :   { /* D = vconcat(zero matrix, diagonal matrix) */
    2493          21 :     for (c=1+h-l, d=1; c<h; c++,d++)
    2494          21 :       if (gequal1(gcoeff(D,c,d))) break;
    2495             :   }
    2496           7 :   else if (h < l)
    2497             :   { /* D = concat(zero matrix, diagonal matrix) */
    2498           7 :     for (c=1, d=1+l-h; d<l; c++,d++)
    2499           7 :       if (gequal1(gcoeff(D,c,d))) break;
    2500             :   }
    2501             :   else
    2502             :   { /* D diagonal */
    2503           0 :     for (c=1; c<l; c++)
    2504           0 :       if (gequal1(gcoeff(D,c,c))) break;
    2505           0 :     d = c;
    2506             :   }
    2507             :   /* U was (h-1)x(h-1), V was (l-1)x(l-1), D was (h-1)x(l-1) */
    2508          21 :   y = cgetg(4,t_VEC);
    2509             :   /* truncate U to (c-1) x (h-1) */
    2510          21 :   gel(y,1) = t = cgetg(h,t_MAT);
    2511          21 :   for (j=1; j<h; j++) gel(t,j) = gcopy_lg(gel(U,j), c);
    2512             :   /* truncate V to (l-1) x (d-1) */
    2513          21 :   gel(y,2) = gcopy_lg(V, d);
    2514          21 :   gel(y,3) = t = zeromatcopy(c-1, d-1);
    2515             :   /* truncate D to a (c-1) x (d-1) matrix */
    2516          21 :   if (d > 1)
    2517             :   {
    2518          14 :     if (h > l)
    2519             :     {
    2520          14 :       for (i=1+h-l, j=1; i<c; i++,j++)
    2521           7 :         gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
    2522             :     }
    2523           7 :     else if (h < l)
    2524             :     {
    2525           7 :       for (i=1, j=1+l-h; j<d; i++,j++)
    2526           0 :         gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
    2527             :     }
    2528             :     else
    2529             :     {
    2530           0 :       for (j=1; j<d; j++)
    2531           0 :         gcoeff(t,j,j) = gcopy(gcoeff(D,j,j));
    2532             :     }
    2533             :   }
    2534          21 :   return y;
    2535             : }
    2536             : 
    2537             : /* does b = x[i,i] divide all entries in x[1..i-1,1..i-1] ? If so, return 0;
    2538             :  * else return the index of a problematic row */
    2539             : static long
    2540         112 : gsnf_no_divide(GEN x, long i, long vx)
    2541             : {
    2542         112 :   GEN b = gcoeff(x,i,i);
    2543             :   long j, k;
    2544             : 
    2545         112 :   if (gequal0(b))
    2546             :   {
    2547          14 :     for (k = 1; k < i; k++)
    2548          14 :       for (j = 1; j < i; j++)
    2549          14 :         if (!gequal0(gcoeff(x,k,j))) return k;
    2550           0 :     return 0;
    2551             :   }
    2552             : 
    2553          98 :   if (!is_RgX(b,vx) || degpol(b)<=0) return 0;
    2554         133 :   for (k = 1; k < i; k++)
    2555         231 :     for (j = 1; j < i; j++)
    2556             :     {
    2557         154 :       GEN z = gcoeff(x,k,j), r;
    2558         154 :       if (!is_RgX(z,vx)) z = scalarpol(z, vx);
    2559         154 :       r = RgX_rem(z, b);
    2560         154 :       if (signe(r) && (! isinexactreal(r) ||
    2561           0 :              gexpo(r) > 16 + gexpo(b) - prec2nbits(gprecision(r)))
    2562           7 :          ) return k;
    2563             :     }
    2564          49 :   return 0;
    2565             : }
    2566             : 
    2567             : /* Hermite Normal Form, with base change matrix if ptB != NULL.
    2568             :  * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
    2569             :  * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
    2570             : GEN
    2571          21 : RgM_hnfall(GEN A, GEN *pB, long remove)
    2572             : {
    2573             :   pari_sp av;
    2574             :   long li, j, k, m, n, def, ldef;
    2575             :   GEN B;
    2576          21 :   long vx = gvar(A);
    2577             : 
    2578          21 :   n = lg(A)-1;
    2579          21 :   if (vx==NO_VARIABLE || !n)
    2580             :   {
    2581           0 :     RgM_check_ZM(A, "mathnf0");
    2582           0 :     return ZM_hnfall(A, pB, remove);
    2583             :   }
    2584          21 :   m = nbrows(A);
    2585          21 :   av = avma;
    2586          21 :   A = RgM_shallowcopy(A);
    2587          21 :   B = pB? matid(n): NULL;
    2588          21 :   def = n; ldef = (m>n)? m-n: 0;
    2589          49 :   for (li=m; li>ldef; li--)
    2590             :   {
    2591             :     GEN T;
    2592          49 :     for (j=def-1; j; j--)
    2593             :     {
    2594          21 :       GEN a = gcoeff(A,li,j);
    2595          21 :       if (gequal0(a)) continue;
    2596             : 
    2597           7 :       k = (j==1)? def: j-1;
    2598           7 :       RgC_elem(a,gcoeff(A,li,k), A,B, j,k, li, vx);
    2599             :     }
    2600          28 :     T = gcoeff(A,li,def);
    2601          28 :     if (gequal0(T))
    2602           0 :     { if (ldef) ldef--; }
    2603             :     else
    2604             :     {
    2605             :       GEN d;
    2606          28 :       gcoeff(A,li,def) = RgX_normalize_all(T, &d);
    2607          28 :       if (B && !gequal1(d)) gel(B, def) = RgC_Rg_div(gel(B, def), d);
    2608          28 :       RgM_reduce(A, B, li, def, vx);
    2609          28 :       def--;
    2610             :     }
    2611          28 :     if (gc_needed(av,1))
    2612             :     {
    2613           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ghnfall");
    2614           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2615             :     }
    2616             :   }
    2617             :   /* rank A = n - def */
    2618          21 :   if (remove) remove_0cols(def, &A, &B, remove);
    2619          21 :   gerepileall(av, B? 2: 1, &A, &B);
    2620          21 :   if (B) *pB = B;
    2621          21 :   return A;
    2622             : }
    2623             : 
    2624             : static GEN
    2625          42 : gsmithall_i(GEN x,long all)
    2626             : {
    2627             :   pari_sp av;
    2628             :   long i, j, k, n;
    2629             :   GEN z, u, v, U, V;
    2630          42 :   long vx = gvar(x);
    2631          42 :   if (typ(x)!=t_MAT) pari_err_TYPE("gsmithall",x);
    2632          42 :   if (vx==NO_VARIABLE) return all? smithall(x): smith(x);
    2633          35 :   n = lg(x)-1;
    2634          35 :   if (!n) return trivsmith(all);
    2635          35 :   if (lgcols(x) != n+1) pari_err_DIM("gsmithall");
    2636          35 :   av = avma;
    2637          35 :   x = RgM_shallowcopy(x);
    2638          35 :   if (all) { U = matid(n); V = matid(n); }
    2639         252 :   for (i=n; i>=2; i--)
    2640             :   {
    2641             :     for(;;)
    2642             :     {
    2643             :       GEN a, b, d;
    2644         189 :       int c = 0;
    2645         546 :       for (j=i-1; j>=1; j--)
    2646             :       {
    2647         357 :         b = gcoeff(x,i,j); if (gequal0(b)) continue;
    2648         140 :         a = gcoeff(x,i,i);
    2649         140 :         d = gbezout_step(&b, &a, &v, &u, vx);
    2650         469 :         for (k = 1; k < i; k++)
    2651             :         {
    2652         329 :           GEN t = gadd(gmul(u,gcoeff(x,k,i)),gmul(v,gcoeff(x,k,j)));
    2653         329 :           gcoeff(x,k,j) = gsub(gmul(a,gcoeff(x,k,j)),gmul(b,gcoeff(x,k,i)));
    2654         329 :           gcoeff(x,k,i) = t;
    2655             :         }
    2656         140 :         gcoeff(x,i,j) = gen_0;
    2657         140 :         gcoeff(x,i,i) = d;
    2658         140 :         if (all) update(u,v,a,b,(GEN*)(V+i),(GEN*)(V+j));
    2659             :       }
    2660         546 :       for (j=i-1; j>=1; j--)
    2661             :       {
    2662         357 :         b = gcoeff(x,j,i); if (gequal0(b)) continue;
    2663         119 :         a = gcoeff(x,i,i);
    2664         119 :         d = gbezout_step(&b, &a, &v, &u, vx);
    2665         406 :         for (k = 1; k < i; k++)
    2666             :         {
    2667         287 :           GEN t = gadd(gmul(u,gcoeff(x,i,k)),gmul(v,gcoeff(x,j,k)));
    2668         287 :           gcoeff(x,j,k) = gsub(gmul(a,gcoeff(x,j,k)),gmul(b,gcoeff(x,i,k)));
    2669         287 :           gcoeff(x,i,k) = t;
    2670             :         }
    2671         119 :         gcoeff(x,j,i) = gen_0;
    2672         119 :         gcoeff(x,i,i) = d;
    2673         119 :         if (all) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
    2674         119 :         c = 1;
    2675             :       }
    2676         189 :       if (!c)
    2677             :       {
    2678         112 :         k = gsnf_no_divide(x, i, vx);
    2679         112 :         if (!k) break;
    2680             : 
    2681          70 :         for (j=1; j<=i; j++)
    2682          49 :           gcoeff(x,i,j) = gadd(gcoeff(x,i,j),gcoeff(x,k,j));
    2683          21 :         if (all) gel(U,i) = gadd(gel(U,i),gel(U,k));
    2684             :       }
    2685          98 :       if (gc_needed(av,1))
    2686             :       {
    2687           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"gsmithall");
    2688           0 :         gerepileall(av, all? 3: 1, &x, &U, &V);
    2689             :       }
    2690          98 :     }
    2691             :   }
    2692         161 :   for (k=1; k<=n; k++)
    2693             :   {
    2694         126 :     GEN d, T = gcoeff(x,k,k);
    2695         126 :     if (!signe(T)) continue;
    2696         112 :     if (is_RgX(T,vx)) T = RgX_normalize_all(T, &d); else { d = T; T = gen_1; }
    2697         112 :     if (all && !gequal1(d)) gel(V,k) = RgC_Rg_div(gel(V,k), d);
    2698         112 :     gcoeff(x,k,k) = T;
    2699             :   }
    2700          35 :   z = all? mkvec3(shallowtrans(U), V, x): RgM_diagonal_shallow(x);
    2701          35 :   return gerepilecopy(av, z);
    2702             : }
    2703             : 
    2704             : GEN
    2705         378 : matsnf0(GEN x,long flag)
    2706             : {
    2707         378 :   pari_sp av = avma;
    2708         378 :   if (flag > 7) pari_err_FLAG("matsnf");
    2709         378 :   if (typ(x) == t_VEC && flag & 4) return smithclean(x);
    2710         378 :   if (flag & 2) x = flag&1 ? gsmithall(x): gsmith(x);
    2711         336 :   else          x = flag&1 ?  smithall(x):  smith(x);
    2712         378 :   if (flag & 4) x = gerepileupto(av, smithclean(x));
    2713         378 :   return x;
    2714             : }
    2715             : 
    2716             : GEN
    2717          42 : gsmith(GEN x) { return gsmithall_i(x,0); }
    2718             : 
    2719             : GEN
    2720           0 : gsmithall(GEN x) { return gsmithall_i(x,1); }
    2721             : 
    2722             : /* H is a relation matrix, either in HNF or a t_VEC (diagonal HNF) */
    2723             : static GEN
    2724      128456 : snf_group(GEN H, GEN D, GEN *newU, GEN *newUi)
    2725             : {
    2726             :   long i, j, l;
    2727             : 
    2728      128456 :   ZM_snfclean(D, newU? *newU: NULL, newUi? *newUi: NULL);
    2729      128456 :   l = lg(D);
    2730      128456 :   if (newU) {
    2731      112210 :     GEN U = *newU;
    2732      354340 :     for (i = 1; i < l; i++)
    2733             :     {
    2734      242130 :       GEN d = gel(D,i), d2 = shifti(d, 1);
    2735     1289365 :       for (j = 1; j < lg(U); j++)
    2736     1047235 :         gcoeff(U,i,j) = centermodii(gcoeff(U,i,j), d, d2);
    2737             :     }
    2738      112210 :     *newU = U;
    2739             :   }
    2740      128456 :   if (newUi && l > 1)
    2741             :   { /* UHV=D -> U^-1 = (HV)D^-1 -> U^-1 = H(VD^-1 mod 1) mod H */
    2742             :     /* Ui = ZM_inv(U, gen_1); setlg(Ui, l); */
    2743      124388 :     GEN V = *newUi, Ui;
    2744      124388 :     int Hvec = (typ(H) == t_VEC);
    2745      124388 :     for (i = 1; i < l; i++) gel(V,i) = FpC_red(gel(V,i), gel(D,i));
    2746      124388 :     if (!Hvec)
    2747             :     {
    2748       60121 :       if (ZM_isdiagonal(H)) { H = RgM_diagonal_shallow(H); Hvec = 1; }
    2749             :     }
    2750      124388 :     Ui = Hvec? ZM_diag_mul(H, V): ZM_mul(H, V);
    2751      124388 :     for (i = 1; i < l; i++) gel(Ui,i) = ZC_Z_divexact(gel(Ui,i), gel(D,i));
    2752      124388 :     if (Hvec)
    2753       88784 :     { for (i = 1; i < l; i++) gel(Ui,i) = vecmodii(gel(Ui,i), H); }
    2754             :     else
    2755       35604 :       Ui = ZM_hnfrem(Ui, H);
    2756      124388 :     *newUi = Ui;
    2757             :   }
    2758      128456 :   return D;
    2759             : }
    2760             : /* H relation matrix among row of generators g in HNF.  Let URV = D its SNF,
    2761             :  * newU R newV = newD its clean SNF (no 1 in Dnew). Return the diagonal of
    2762             :  * newD, newU and newUi such that  1/U = (newUi, ?).
    2763             :  * Rationale: let (G,0) = g Ui be the new generators then
    2764             :  * 0 = G U R --> G D = 0,  g = G newU  and  G = g newUi */
    2765             : GEN
    2766       64000 : ZM_snf_group(GEN H, GEN *newU, GEN *newUi)
    2767             : {
    2768       64000 :   GEN D = ZM_snfall_i(H, newU, newUi, 1);
    2769       64000 :   return snf_group(H, D, newU, newUi);
    2770             : }
    2771             : 
    2772             : /* D a ZV: SNF for matdiagonal(D). Faster because we only ensure elementary
    2773             :  * divisors condition: d[n] | ... | d[1] and need not convert D to matrix form*/
    2774             : GEN
    2775       64456 : ZV_snfall(GEN D, GEN *pU, GEN *pV)
    2776             : {
    2777       64456 :   pari_sp av = avma;
    2778       64456 :   long j, n = lg(D)-1;
    2779       64456 :   GEN U = pU? matid(n): NULL;
    2780       64456 :   GEN V = pV? matid(n): NULL;
    2781             :   GEN p;
    2782             : 
    2783       64456 :   D = leafcopy(D);
    2784      202251 :   for (j = n; j > 0; j--)
    2785             :   {
    2786      137795 :     GEN b = gel(D,j);
    2787      137795 :     if (signe(b) < 0)
    2788             :     {
    2789           0 :       gel(D,j) = absi(b);
    2790           0 :       if (V) ZV_togglesign(gel(V,j));
    2791             :     }
    2792             :   }
    2793             :   /* entries are non-negative integers */
    2794       64456 :   p = gen_indexsort(D, NULL, &negcmpii);
    2795       64456 :   D = vecpermute(D, p);
    2796       64456 :   if (U) U = vecpermute(U, p);
    2797       64456 :   if (V) V = vecpermute(V, p);
    2798             :   /* entries are sorted by decreasing value */
    2799      202251 :   for (j = n; j > 0; j--)
    2800             :   {
    2801      137795 :     GEN b = gel(D,j);
    2802             :     long i;
    2803      244713 :     for (i = j-1; i > 0; i--)
    2804             :     { /* invariant: a >= b. If au+bv = d is a Bezout relation, A=a/d and B=b/d
    2805             :        * we have [B,-A;u,v]*diag(a,b)*[1-u*A,1; -u*A,1]] = diag(Ab, d) */
    2806      107590 :       GEN a = gel(D,i), u,v, d = bezout(a,b, &u,&v), A, Wi, Wj;
    2807      107590 :       if (equalii(d,b)) continue;
    2808       13517 :       A = diviiexact(a,d);
    2809       13517 :       if (V)
    2810             :       {
    2811       13475 :         GEN t = mulii(u,A);
    2812       13475 :         Wi = ZC_lincomb(subui(1,t), negi(t), gel(V,i), gel(V,j));
    2813       13475 :         Wj = ZC_add(gel(V,i), gel(V,j));
    2814       13475 :         gel(V,i) = Wi;
    2815       13475 :         gel(V,j) = Wj;
    2816             :       }
    2817       13517 :       if (U)
    2818             :       {
    2819       13517 :         GEN B = diviiexact(b,d);
    2820       13517 :         Wi = ZC_lincomb(B, negi(A), gel(U,i), gel(U,j));
    2821       13517 :         Wj = ZC_lincomb(u, v, gel(U,i), gel(U,j));
    2822       13517 :         gel(U,i) = Wi;
    2823       13517 :         gel(U,j) = Wj;
    2824             :       }
    2825       13517 :       gel(D,i) = mulii(A,b); /* lcm(a,b) */
    2826       13517 :       gel(D,j) = d; /* gcd(a,b) */
    2827       13517 :       b = gel(D,j); if (equali1(b)) break;
    2828             :     }
    2829             :   }
    2830       64456 :   snf_pile(av, &D,&U,&V);
    2831       64456 :   if (U) *pU = shallowtrans(U);
    2832       64456 :   if (V) *pV = V;
    2833       64456 :   return D;
    2834             : }
    2835             : GEN
    2836       64456 : ZV_snf_group(GEN d, GEN *newU, GEN *newUi)
    2837             : {
    2838       64456 :   GEN D = ZV_snfall(d, newU, newUi);
    2839       64456 :   return snf_group(d, D, newU, newUi);
    2840             : }
    2841             : 
    2842             : /* D a vector of elementary divisors. Truncate (setlg) to leave out trivial
    2843             :  * entries (= 1) */
    2844             : void
    2845         224 : ZV_snf_trunc(GEN D)
    2846             : {
    2847         224 :   long i, l = lg(D);
    2848         490 :   for (i = 1; i < l; i++)
    2849         371 :     if (is_pm1(gel(D,i))) { setlg(D,i); break; }
    2850         224 : }

Generated by: LCOV version 1.11