Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - hnf_snf.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.14.0 lcov report (development 27775-aca467eab2) Lines: 1552 1725 90.0 %
Date: 2022-07-03 07:33:15 Functions: 90 100 90.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : #include "pari.h"
      16             : #include "paripriv.h"
      17             : 
      18             : #define DEBUGLEVEL DEBUGLEVEL_mathnf
      19             : 
      20             : /**************************************************************/
      21             : /**                                                          **/
      22             : /**                HERMITE NORMAL FORM REDUCTION             **/
      23             : /**                                                          **/
      24             : /**************************************************************/
      25             : static GEN ZV_hnfgcdext(GEN A);
      26             : static GEN
      27          14 : hnfallgen(GEN x)
      28             : {
      29          14 :   GEN z = cgetg(3, t_VEC);
      30          14 :   gel(z,1) = RgM_hnfall(x, (GEN*)(z+2), 1);
      31          14 :   return z;
      32             : }
      33             : GEN
      34         252 : mathnf0(GEN x, long flag)
      35             : {
      36         252 :   switch(typ(x))
      37             :   {
      38          70 :     case t_VEC:
      39          70 :       if (RgV_is_ZV(x))
      40             :         switch (flag)
      41             :         {
      42          14 :           case 0:
      43          14 :             if (lg(x) == 1) return cgetg(1, t_MAT);
      44           7 :             retmkmat(mkcol(ZV_content(x)));
      45          21 :           case 1:
      46             :           case 4:
      47          21 :             return ZV_hnfgcdext(x);
      48             :         }
      49          35 :       x = gtomat(x); break;
      50         182 :     case t_MAT: break;
      51           0 :     default: pari_err_TYPE("mathnf0",x);
      52             :   }
      53             : 
      54         217 :   switch(flag)
      55             :   {
      56         168 :     case 0: case 2: return RgM_is_ZM(x)? ZM_hnf(x): RgM_hnfall(x,NULL,1);
      57          28 :     case 1: case 3: return RgM_is_ZM(x)? hnfall(x): hnfallgen(x);
      58           7 :     case 4: RgM_check_ZM(x, "mathnf0"); return hnflll(x);
      59          14 :     case 5: RgM_check_ZM(x, "mathnf0"); return hnfperm(x);
      60           0 :     default: pari_err_FLAG("mathnf");
      61             :   }
      62             :   return NULL; /* LCOV_EXCL_LINE */
      63             : }
      64             : 
      65             : /*******************************************************************/
      66             : /*                                                                 */
      67             : /*                SPECIAL HNF (FOR INTERNAL USE !!!)               */
      68             : /*                                                                 */
      69             : /*******************************************************************/
      70             : static int
      71     8253835 : count(GEN mat, long row, long len, long *firstnonzero)
      72             : {
      73     8253835 :   long j, n = 0;
      74             : 
      75   560142435 :   for (j=1; j<=len; j++)
      76             :   {
      77   553720848 :     long p = mael(mat,j,row);
      78   553720848 :     if (p)
      79             :     {
      80    21699533 :       if (labs(p)!=1) return -1;
      81    19867285 :       n++; *firstnonzero=j;
      82             :     }
      83             :   }
      84     6421587 :   return n;
      85             : }
      86             : 
      87             : static int
      88      382934 : count2(GEN mat, long row, long len)
      89             : {
      90             :   long j;
      91     3426998 :   for (j=len; j; j--)
      92     3309422 :     if (labs(mael(mat,j,row)) == 1) return j;
      93      117576 :   return 0;
      94             : }
      95             : 
      96             : static GEN
      97      216797 : hnffinal(GEN matgen,GEN perm,GEN* ptdep,GEN* ptB,GEN* ptC)
      98             : {
      99             :   GEN p1,p2,U,H,Hnew,Bnew,Cnew,diagH1;
     100      216797 :   GEN B = *ptB, C = *ptC, dep = *ptdep, depnew;
     101             :   pari_sp av;
     102             :   long i,j,k,s,i1,j1,zc;
     103      216797 :   long co = lg(C);
     104      216797 :   long col = lg(matgen)-1;
     105             :   long lnz, nlze, lig;
     106             : 
     107      216797 :   if (col == 0) return matgen;
     108      216797 :   lnz = nbrows(matgen); /* was called lnz-1 - nr in hnfspec */
     109      216797 :   nlze = nbrows(dep);
     110      216797 :   lig = nlze + lnz;
     111             :   /* H: lnz x lnz [disregarding initial 0 cols], U: col x col */
     112      216797 :   H = ZM_hnflll(matgen, &U, 0);
     113      216796 :   H += (lg(H)-1 - lnz); H[0] = evaltyp(t_MAT) | evallg(lnz+1);
     114             :   /* Only keep the part above the H (above the 0s is 0 since the dep rows
     115             :    * are dependent from the ones in matgen) */
     116      216795 :   zc = col - lnz; /* # of 0 columns, correspond to units */
     117      216795 :   if (nlze) { dep = ZM_mul(dep,U); dep += zc; }
     118             : 
     119      216795 :   diagH1 = new_chunk(lnz+1); /* diagH1[i] = 0 iff H[i,i] != 1 (set later) */
     120             : 
     121      216796 :   av = avma;
     122      216796 :   Cnew = cgetg(co, typ(C));
     123      216796 :   setlg(C, col+1); p1 = gmul(C,U);
     124     1871343 :   for (j=1; j<=col; j++) gel(Cnew,j) = gel(p1,j);
     125     4130658 :   for (   ; j<co ; j++)  gel(Cnew,j) = gel(C,j);
     126             : 
     127             :   /* Clean up B using new H */
     128      899301 :   for (s=0,i=lnz; i; i--)
     129             :   {
     130      682520 :     GEN Di = gel(dep,i), Hi = gel(H,i);
     131      682520 :     GEN h = gel(Hi,i); /* H[i,i] */
     132      682520 :     if ( (diagH1[i] = is_pm1(h)) ) { h = NULL; s++; }
     133    25265558 :     for (j=col+1; j<co; j++)
     134             :     {
     135    24583246 :       GEN z = gel(B,j-col);
     136    24583246 :       p1 = gel(z,i+nlze);
     137    24583246 :       if (h) p1 = truedivii(p1,h);
     138    24582641 :       if (!signe(p1)) continue;
     139   138787490 :       for (k=1; k<=nlze; k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Di,k)));
     140   171763128 :       for (   ; k<=lig;  k++) gel(z,k) = subii(gel(z,k), mulii(p1, gel(Hi,k-nlze)));
     141    12085048 :       gel(Cnew,j) = gsub(gel(Cnew,j), gmul(p1, gel(Cnew,i+zc)));
     142             :     }
     143      682312 :     if (gc_needed(av,2))
     144             :     {
     145         959 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnffinal, i = %ld",i);
     146         959 :       gerepileall(av, 2, &Cnew, &B);
     147             :     }
     148             :   }
     149      216781 :   p1 = cgetg(lnz+1,t_VEC); p2 = perm + nlze;
     150      899325 :   for (i1=0, j1=lnz-s, i=1; i<=lnz; i++) /* push the 1 rows down */
     151      682532 :     if (diagH1[i])
     152      318695 :       gel(p1,++j1) = gel(p2,i);
     153             :     else
     154      363837 :       gel(p2,++i1) = gel(p2,i);
     155      535487 :   for (i=i1+1; i<=lnz; i++) gel(p2,i) = gel(p1,i);
     156             : 
     157             :   /* s = # extra redundant generators taken from H
     158             :    *          zc  col-s  co   zc = col - lnz
     159             :    *       [ 0 |dep |     ]    i = nlze + lnz - s = lig - s
     160             :    *  nlze [--------|  B' ]
     161             :    *       [ 0 | H' |     ]    H' = H minus the s rows with a 1 on diagonal
     162             :    *     i [--------|-----] lig-s           (= "1-rows")
     163             :    *       [   0    | Id  ]
     164             :    *       [        |     ] li */
     165      216793 :   lig -= s; col -= s; lnz -= s;
     166      216793 :   Hnew = cgetg(lnz+1,t_MAT);
     167      216794 :   depnew = cgetg(lnz+1,t_MAT); /* only used if nlze > 0 */
     168      216793 :   Bnew = cgetg(co-col,t_MAT);
     169      216793 :   C = shallowcopy(Cnew);
     170      899325 :   for (j=1,i1=j1=0; j<=lnz+s; j++)
     171             :   {
     172      682533 :     GEN z = gel(H,j);
     173      682533 :     if (diagH1[j])
     174             :     { /* hit exactly s times */
     175      318694 :       i1++; C[i1+col] = Cnew[j+zc];
     176      318694 :       p1 = cgetg(lig+1,t_COL); gel(Bnew,i1) = p1;
     177     1559600 :       for (i=1; i<=nlze; i++) gel(p1,i) = gcoeff(dep,i,j);
     178      318694 :       p1 += nlze;
     179             :     }
     180             :     else
     181             :     {
     182      363839 :       j1++; C[j1+zc] = Cnew[j+zc];
     183      363839 :       p1 = cgetg(lnz+1,t_COL); gel(Hnew,j1) = p1;
     184      363838 :       depnew[j1] = dep[j];
     185             :     }
     186     3342995 :     for (i=k=1; k<=lnz; i++)
     187     2660463 :       if (!diagH1[i]) p1[k++] = z[i];
     188             :   }
     189     4130587 :   for (j=s+1; j<co-col; j++)
     190             :   {
     191     3913805 :     GEN z = gel(B,j-s);
     192     3913805 :     p1 = cgetg(lig+1,t_COL); gel(Bnew,j) = p1;
     193    22860412 :     for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
     194     3913795 :     z += nlze; p1 += nlze;
     195    22224334 :     for (i=k=1; k<=lnz; i++)
     196    18310539 :       if (!diagH1[i]) gel(p1,k++) = gel(z,i);
     197             :   }
     198      216782 :   *ptdep = depnew;
     199      216782 :   *ptC = C;
     200      216782 :   *ptB = Bnew; return Hnew;
     201             : }
     202             : 
     203             : /* for debugging */
     204             : static void
     205           0 : p_mat(GEN mat, GEN perm, long k)
     206             : {
     207           0 :   pari_sp av = avma;
     208           0 :   perm = vecslice(perm, k+1, lg(perm)-1);
     209           0 :   err_printf("Permutation: %Ps\n",perm);
     210           0 :   if (DEBUGLEVEL > 6)
     211           0 :     err_printf("matgen = %Ps\n", zm_to_ZM( rowpermute(mat, perm) ));
     212           0 :   set_avma(av);
     213           0 : }
     214             : 
     215             : static GEN
     216     2575578 : col_dup(long l, GEN col)
     217             : {
     218     2575578 :   GEN c = new_chunk(l);
     219     2575577 :   memcpy(c,col,l * sizeof(long)); return c;
     220             : }
     221             : 
     222             : /* permutation giving imagecompl(x') | image(x'), x' = transpose of x */
     223             : static GEN
     224      216797 : ZM_rowrankprofile(GEN x, long *nlze)
     225             : {
     226      216797 :   pari_sp av = avma;
     227             :   GEN d, y;
     228             :   long i, j, k, l, r;
     229             : 
     230      216797 :   x = shallowtrans(x); l = lg(x);
     231      216797 :   (void)new_chunk(l); /* HACK */
     232      216796 :   d = ZM_pivots(x,&r); set_avma(av);
     233      216796 :   *nlze = r;
     234      216796 :   if (!d) return identity_perm(l-1);
     235      194807 :   y = cgetg(l,t_VECSMALL);
     236      976523 :   for (i = j = 1, k = r+1; i<l; i++)
     237      781716 :     if (d[i]) y[k++] = i; else y[j++] = i;
     238      194807 :   return y;
     239             : }
     240             : 
     241             : /* HNF reduce a relation matrix (column operations + row permutation)
     242             : ** Input:
     243             : **   mat = (li-1) x (co-1) matrix of long
     244             : **   C   = r x (co-1) matrix of GEN
     245             : **   perm= permutation vector (length li-1), indexing the rows of mat: easier
     246             : **     to maintain perm than to copy rows. For columns we can do it directly
     247             : **     using e.g. swap(mat[i], mat[j])
     248             : **   k0 = integer. The k0 first lines of mat are dense, the others are sparse.
     249             : ** Output: cf ASCII art in the function body
     250             : **
     251             : ** row permutations applied to perm
     252             : ** column operations applied to C. IN PLACE
     253             : **/
     254             : GEN
     255      132970 : hnfspec_i(GEN mat0, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, long k0)
     256             : {
     257             :   pari_sp av;
     258             :   long co, n, s, nlze, lnz, nr, i, j, k, lk0, col, lig, *p;
     259             :   GEN mat;
     260             :   GEN p1, p2, matb, matbnew, vmax, matt, T, extramat, B, C, H, dep, permpro;
     261      132970 :   const long li = lg(perm); /* = lgcols(mat0) */
     262      132970 :   const long CO = lg(mat0);
     263             : 
     264      132970 :   n = 0; /* -Wall */
     265             : 
     266      132970 :   C = *ptC; co = CO;
     267      132970 :   if (co > 300 && co > 1.5 * li)
     268             :   { /* treat the rest at the end */
     269           0 :     co = (long)(1.2 * li);
     270           0 :     setlg(C, co);
     271             :   }
     272             : 
     273      132970 :   if (DEBUGLEVEL>5)
     274             :   {
     275           0 :     err_printf("Entering hnfspec\n");
     276           0 :     p_mat(mat0,perm,0);
     277             :   }
     278      132970 :   matt = cgetg(co, t_MAT); /* dense part of mat (top) */
     279      132970 :   mat = cgetg(co, t_MAT);
     280     2708548 :   for (j = 1; j < co; j++)
     281             :   {
     282     2575579 :     GEN matj = col_dup(li, gel(mat0,j));
     283     2575579 :     p1 = cgetg(k0+1,t_COL); gel(matt,j) = p1; gel(mat,j) = matj;
     284    12055330 :     for (i=1; i<=k0; i++) gel(p1,i) = stoi(matj[perm[i]]);
     285             :   }
     286      132969 :   av = avma;
     287             : 
     288      132969 :   i = lig = li-1; col = co-1; lk0 = k0;
     289      132969 :   T = (k0 || (lg(C) > 1 && lgcols(C) > 1))? matid(col): NULL;
     290             :   /* Look for lines with a single nonzero entry, equal to 1 in absolute value */
     291     6584281 :   while (i > lk0 && col)
     292     6451284 :     switch( count(mat,perm[i],col,&n) )
     293             :     {
     294       40609 :       case 0: /* move zero lines between k0+1 and lk0 */
     295       40609 :         lk0++; lswap(perm[i], perm[lk0]);
     296       40609 :         i = lig; continue;
     297             : 
     298      488335 :       case 1: /* move trivial generator between lig+1 and li */
     299      488335 :         lswap(perm[i], perm[lig]);
     300      488335 :         if (T) swap(gel(T,n), gel(T,col));
     301      488335 :         swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     302      488335 :         if (p[perm[lig]] < 0) /* = -1 */
     303             :         { /* convert relation -g = 0 to g = 0 */
     304    10381024 :           for (i=lk0+1; i<lig; i++) p[perm[i]] = -p[perm[i]];
     305      434943 :           if (T)
     306             :           {
     307      434941 :             p1 = gel(T,col);
     308      434941 :             for (i=1; ; i++) /* T = permuted identity: single nonzero entry */
     309    11109016 :               if (signe(gel(p1,i))) { togglesign_safe(&gel(p1,i)); break; }
     310             :           }
     311             :         }
     312      488353 :         lig--; col--; i = lig; continue;
     313             : 
     314     5922349 :       default: i--;
     315             :     }
     316      132997 :   if (DEBUGLEVEL>5) { err_printf("    after phase1:\n"); p_mat(mat,perm,0); }
     317             : 
     318             : #define absmax(s,z) {long _z; _z = labs(z); if (_z > s) s = _z;}
     319             :   /* Get rid of all lines containing only 0 and +/- 1, keeping track of column
     320             :    * operations in T. Leave the rows 1..lk0 alone [up to k0, coefficient
     321             :    * explosion, between k0+1 and lk0, row is 0] */
     322      132970 :   s = 0;
     323      767314 :   while (lig > lk0 && col && s < (long)(HIGHBIT>>1))
     324             :   {
     325     1925517 :     for (i=lig; i>lk0; i--)
     326     1802633 :       if (count(mat,perm[i],col,&n) > 0) break;
     327      757366 :     if (i == lk0) break;
     328             : 
     329             :     /* only 0, +/- 1 entries, at least 2 of them nonzero */
     330      634482 :     lswap(perm[i], perm[lig]);
     331      634482 :     swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     332      634482 :     if (T) swap(gel(T,n), gel(T,col));
     333      634482 :     if (p[perm[lig]] < 0)
     334             :     {
     335     7100546 :       for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
     336      409652 :       if (T) ZV_togglesign(gel(T,col));
     337             :     }
     338    17632131 :     for (j=1; j<col; j++)
     339             :     {
     340    16997787 :       GEN matj = gel(mat,j);
     341             :       long t;
     342    16997787 :       if (! (t = matj[perm[lig]]) ) continue;
     343     1384988 :       if (t == 1) {
     344    16171396 :         for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= p[perm[i]]);
     345             :       }
     346             :       else { /* t = -1 */
     347    13540518 :         for (i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] += p[perm[i]]);
     348             :       }
     349     1384988 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
     350             :     }
     351      634344 :     lig--; col--;
     352      634344 :     if (gc_needed(av,3))
     353             :     {
     354           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[1]");
     355           0 :       if (T) T = gerepilecopy(av, T); else set_avma(av);
     356             :     }
     357             :   }
     358             :   /* As above with lines containing a +/- 1 (no other assumption).
     359             :    * Stop when single precision becomes dangerous */
     360      132841 :   vmax = cgetg(co,t_VECSMALL);
     361     1585722 :   for (j=1; j<=col; j++)
     362             :   {
     363     1452753 :     GEN matj = gel(mat,j);
     364     6167763 :     for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[i]);
     365     1452753 :     vmax[j] = s;
     366             :   }
     367      398329 :   while (lig > lk0 && col)
     368             :   {
     369      409056 :     for (i=lig; i>lk0; i--)
     370      382935 :       if ( (n = count2(mat,perm[i],col)) ) break;
     371      291479 :     if (i == lk0) break;
     372             : 
     373      265357 :     lswap(vmax[n], vmax[col]);
     374      265357 :     lswap(perm[i], perm[lig]);
     375      265357 :     swap(gel(mat,n), gel(mat,col)); p = gel(mat,col);
     376      265357 :     if (T) swap(gel(T,n), gel(T,col));
     377      265357 :     if (p[perm[lig]] < 0)
     378             :     {
     379      471340 :       for (i=lk0+1; i<=lig; i++) p[perm[i]] = -p[perm[i]];
     380       93619 :       if (T) ZV_togglesign(gel(T,col));
     381             :     }
     382     3395591 :     for (j=1; j<col; j++)
     383             :     {
     384     3130299 :       GEN matj = gel(mat,j);
     385             :       long t;
     386     3130299 :       if (! (t = matj[perm[lig]]) ) continue;
     387     1486070 :       if (vmax[col] && (ulong)labs(t) >= (HIGHBIT-vmax[j]) / vmax[col])
     388           0 :         goto END2;
     389             : 
     390    11987711 :       for (s=0, i=lk0+1; i<=lig; i++) absmax(s, matj[perm[i]] -= t*p[perm[i]]);
     391     1486070 :       vmax[j] = s;
     392     1486070 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,col), stoi(-t));
     393             :     }
     394      265292 :     lig--; col--;
     395      265292 :     if (gc_needed(av,3))
     396             :     {
     397           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[2]");
     398           0 :       gerepileall(av, T? 2: 1, &vmax, &T);
     399             :     }
     400             :   }
     401             : 
     402      106849 : END2: /* clean up mat: remove everything to the right of the 1s on diagonal */
     403             :   /* go multiprecision first */
     404      132971 :   matb = cgetg(co,t_MAT); /* bottom part (complement of matt) */
     405     2708219 :   for (j=1; j<co; j++)
     406             :   {
     407     2575562 :     GEN matj = gel(mat,j);
     408     2575562 :     p1 = cgetg(li-k0,t_COL); gel(matb,j) = p1;
     409     2575568 :     p1 -= k0;
     410    74474487 :     for (i=k0+1; i<li; i++) gel(p1,i) = stoi(matj[perm[i]]);
     411             :   }
     412      132657 :   if (DEBUGLEVEL>5)
     413             :   {
     414           0 :     err_printf("    after phase2:\n");
     415           0 :     p_mat(mat,perm,lk0);
     416             :   }
     417     1388567 :   for (i=li-2; i>lig; i--)
     418             :   {
     419     1255599 :     long h, i0 = i - k0, k = i + co-li;
     420     1255599 :     GEN Bk = gel(matb,k);
     421    25084874 :     for (j=k+1; j<co; j++)
     422             :     {
     423    23829536 :       GEN Bj = gel(matb,j), v = gel(Bj,i0);
     424    23829536 :       s = signe(v); if (!s) continue;
     425             : 
     426     4399429 :       gel(Bj,i0) = gen_0;
     427     4399429 :       if (is_pm1(v))
     428             :       {
     429     2761718 :         if (s > 0) /* v = 1 */
     430    40169376 :         { for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), gel(Bk,h)); }
     431             :         else /* v = -1 */
     432    34885320 :         { for (h=1; h<i0; h++) gel(Bj,h) = addii(gel(Bj,h), gel(Bk,h)); }
     433             :       }
     434             :       else {
     435    37650149 :         for (h=1; h<i0; h++) gel(Bj,h) = subii(gel(Bj,h), mulii(v,gel(Bk,h)));
     436             :       }
     437     4398951 :       if (T) ZC_lincomb1_inplace(gel(T,j), gel(T,k), negi(v));
     438             :     }
     439     1255338 :     if (gc_needed(av,2))
     440             :     {
     441           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"hnfspec[3], i = %ld", i);
     442           0 :       for (h=1; h<co; h++) setlg(matb[h], i0+1); /* bottom can be forgotten */
     443           0 :       gerepileall(av, T? 2: 1, &matb, &T);
     444             :     }
     445             :   }
     446     2708545 :   for (j=1; j<co; j++) setlg(matb[j], lig-k0+1); /* bottom can be forgotten */
     447      132966 :   gerepileall(av, T? 2: 1, &matb, &T);
     448      132970 :   if (DEBUGLEVEL>5) err_printf("    matb cleaned up (using Id block)\n");
     449             : 
     450      132970 :   nlze = lk0 - k0;  /* # of 0 rows */
     451      132970 :   lnz = lig-nlze+1; /* 1 + # of nonzero rows (!= 0...0 1 0 ... 0) */
     452      132970 :   if (T) matt = ZM_mul(matt,T); /* update top rows */
     453      132967 :   extramat = cgetg(col+1,t_MAT); /* = new C minus the 0 rows */
     454     1320362 :   for (j=1; j<=col; j++)
     455             :   {
     456     1187396 :     GEN z = gel(matt,j);
     457     1187396 :     GEN t = (gel(matb,j)) + nlze - k0;
     458     1187396 :     p2=cgetg(lnz,t_COL); gel(extramat,j) = p2;
     459     5042413 :     for (i=1; i<=k0; i++) gel(p2,i) = gel(z,i); /* top k0 rows */
     460     1856687 :     for (   ; i<lnz; i++) gel(p2,i) = gel(t,i); /* other nonzero rows */
     461             :   }
     462      132966 :   if (!col) {
     463           0 :     permpro = identity_perm(lnz);
     464           0 :     nr = lnz;
     465             :   }
     466             :   else
     467      132966 :     permpro = ZM_rowrankprofile(extramat, &nr);
     468             :   /* lnz = lg(permpro) */
     469      132970 :   if (nlze)
     470             :   { /* put the nlze 0 rows (trivial generators) at the top */
     471       12168 :     p1 = new_chunk(lk0+1);
     472       52777 :     for (i=1; i<=nlze; i++) p1[i] = perm[i + k0];
     473       62981 :     for (   ; i<=lk0; i++)  p1[i] = perm[i - nlze];
     474      103590 :     for (i=1; i<=lk0; i++)  perm[i] = p1[i];
     475             :   }
     476             :   /* sort other rows according to permpro (nr redundant generators first) */
     477      132970 :   p1 = new_chunk(lnz); p2 = perm + nlze;
     478      586638 :   for (i=1; i<lnz; i++) p1[i] = p2[permpro[i]];
     479      586640 :   for (i=1; i<lnz; i++) p2[i] = p1[i];
     480             :   /* perm indexes the rows of mat
     481             :    *   |_0__|__redund__|__dense__|__too big__|_____done______|
     482             :    *   0  nlze                              lig             li
     483             :    *         \___nr___/ \___k0__/
     484             :    *         \____________lnz ______________/
     485             :    *
     486             :    *               col   co
     487             :    *       [dep     |     ]
     488             :    *    i0 [--------|  B  ] (i0 = nlze + nr)
     489             :    *       [matbnew |     ] matbnew has maximal rank = lnz-1 - nr
     490             :    * mat = [--------|-----] lig
     491             :    *       [   0    | Id  ]
     492             :    *       [        |     ] li */
     493             : 
     494      132968 :   matbnew = cgetg(col+1,t_MAT); /* dense+toobig, maximal rank. For hnffinal */
     495      132968 :   dep    = cgetg(col+1,t_MAT); /* rows dependent from the ones in matbnew */
     496     1320377 :   for (j=1; j<=col; j++)
     497             :   {
     498     1187407 :     GEN z = gel(extramat,j);
     499     1187407 :     p1 = cgetg(nlze+nr+1,t_COL); gel(dep,j) = p1;
     500     1187415 :     p2 = cgetg(lnz-nr,t_COL); gel(matbnew,j) = p2;
     501     1700009 :     for (i=1; i<=nlze; i++) gel(p1,i) = gen_0;
     502     1334588 :     p1 += nlze; for (i=1; i<=nr; i++) p1[i] = z[permpro[i]];
     503     5564503 :     p2 -= nr;   for (   ; i<lnz; i++) p2[i] = z[permpro[i]];
     504             :   }
     505             : 
     506             :   /* redundant generators in terms of the genuine generators
     507             :    * (x_i) = - (g_i) B */
     508      132970 :   B = cgetg(co-col,t_MAT);
     509     1521163 :   for (j=col+1; j<co; j++)
     510             :   {
     511     1388195 :     GEN y = gel(matt,j);
     512     1388195 :     GEN z = gel(matb,j);
     513     1388195 :     p1=cgetg(lig+1,t_COL); gel(B,j-col) = p1;
     514     4980778 :     for (i=1; i<=nlze; i++) gel(p1,i) = gel(z,i);
     515     1388194 :     p1 += nlze; z += nlze-k0;
     516     9441050 :     for (k=1; k<lnz; k++)
     517             :     {
     518     8052856 :       i = permpro[k];
     519     8052856 :       gel(p1,k) = (i <= k0)? gel(y,i): gel(z,i);
     520             :     }
     521             :   }
     522      132968 :   if (T) C = typ(C)==t_MAT? RgM_ZM_mul(C,T): RgV_RgM_mul(C,T);
     523      132968 :   gerepileall(av, 4, &matbnew, &B, &dep, &C);
     524      132970 :   *ptdep = dep;
     525      132970 :   *ptB = B;
     526      132970 :   H = hnffinal(matbnew, perm, ptdep, ptB, &C);
     527      132970 :   if (CO > co)
     528             :   { /* treat the rest, N cols at a time (hnflll slow otherwise) */
     529           0 :     const long N = 300;
     530           0 :     long a, L = CO - co, l = minss(L, N); /* L columns to add */
     531           0 :     GEN CC = *ptC, m0 = mat0;
     532           0 :     setlg(CC, CO); /* restore */
     533           0 :     CC += co-1;
     534           0 :     m0 += co-1;
     535           0 :     for (a = l;;)
     536           0 :     {
     537             :       GEN MAT, emb;
     538           0 :       gerepileall(av, 4, &H,&C,ptB,ptdep);
     539           0 :       MAT = cgetg(l + 1, t_MAT);
     540           0 :       emb = cgetg(l + 1, typ(C));
     541           0 :       for (j = 1 ; j <= l; j++)
     542             :       {
     543           0 :         gel(MAT,j) = gel(m0,j);
     544           0 :         emb[j] = CC[j];
     545             :       }
     546           0 :       H = hnfadd_i(H, perm, ptdep, ptB, &C, MAT, emb);
     547           0 :       if (a == L) break;
     548           0 :       CC += l;
     549           0 :       m0 += l;
     550           0 :       a += l; if (a > L) { l = L - (a - l); a = L; }
     551             :     }
     552             :   }
     553      132970 :   *ptC = C; return H;
     554             : }
     555             : 
     556             : GEN
     557           0 : hnfspec(GEN mat, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, long k0)
     558             : {
     559           0 :   pari_sp av = avma;
     560           0 :   GEN H = hnfspec_i(mat, perm, ptdep, ptB, ptC, k0);
     561           0 :   return gc_all(av, 4, &H, ptC, ptdep, ptB);
     562             : }
     563             : 
     564             : /* HNF reduce x, apply same transforms to C */
     565             : GEN
     566           0 : mathnfspec(GEN x, GEN *pperm, GEN *pdep, GEN *pB, GEN *pC)
     567             : {
     568           0 :   long i, j, k, l, n, ly, lx = lg(x);
     569             :   GEN z, v1, perm;
     570           0 :   if (lx == 1) return cgetg(1, t_MAT);
     571           0 :   ly = lgcols(x);
     572           0 :   *pperm = perm = identity_perm(ly-1);
     573           0 :   z = cgetg(lx,t_MAT);
     574           0 :   for (i=1; i<lx; i++)
     575             :   {
     576           0 :     GEN C = cgetg(ly,t_COL), D = gel(x,i);
     577           0 :     gel(z,i) = C;
     578           0 :     for (j=1; j<ly; j++)
     579             :     {
     580           0 :       GEN d = gel(D,j);
     581           0 :       if (is_bigint(d)) goto TOOLARGE;
     582           0 :       C[j] = itos(d);
     583             :     }
     584             :   }
     585             :   /*  [ dep |     ]
     586             :    *  [-----|  B  ]
     587             :    *  [  H  |     ]
     588             :    *  [-----|-----]
     589             :    *  [  0  | Id  ] */
     590           0 :   return hnfspec(z,perm, pdep, pB, pC, 0);
     591             : 
     592           0 : TOOLARGE:
     593           0 :   if (lg(*pC) > 1 && lgcols(*pC) > 1)
     594           0 :     pari_err_IMPL("mathnfspec with large entries");
     595           0 :   x = ZM_hnf(x); lx = lg(x);
     596           0 :   v1 = cgetg(ly, t_VECSMALL);
     597           0 :   n = lx - ly;
     598           0 :   for (i = k = l = 1; i < ly; i++)
     599           0 :     if (equali1(gcoeff(x,i,i + n))) v1[l++] = i; else perm[k++] = i;
     600           0 :   setlg(perm, k);
     601           0 :   setlg(v1, l);
     602           0 :   x = rowpermute(x, perm); /* upper part */
     603           0 :   *pperm = vecsmall_concat(perm, v1);
     604           0 :   *pB = vecslice(x, k+n, lx-1);
     605           0 :   setlg(x, k);
     606           0 :   *pdep = rowslice(x, 1, n);
     607           0 :   return n? rowslice(x, n+1, k-1): x; /* H */
     608             : }
     609             : 
     610             : /* add new relations to a matrix treated by hnfspec (extramat / extraC) */
     611             : GEN
     612       83827 : hnfadd_i(GEN H, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, /* cf hnfspec */
     613             :        GEN extramat,GEN extraC)
     614             : {
     615       83827 :   GEN matb, extratop, Cnew, permpro, B = *ptB, C = *ptC, dep = *ptdep;
     616             :   long i, lH, lB, li, lig, co, col, nlze;
     617             : 
     618       83827 :   if (lg(extramat) == 1) return H;
     619       83827 :   co = lg(C)-1;
     620       83827 :   lH = lg(H)-1;
     621       83827 :   lB = lg(B)-1;
     622       83827 :   li = lg(perm)-1;
     623       83827 :   lig = li - lB;
     624       83827 :   col = co - lB;
     625       83827 :   nlze = lig - lH;
     626             : 
     627             :  /*               col    co
     628             :   *       [ 0 |dep |     ]
     629             :   *  nlze [--------|  B  ]
     630             :   *       [ 0 | H  |     ]
     631             :   *       [--------|-----] lig
     632             :   *       [   0    | Id  ]
     633             :   *       [        |     ] li */
     634       83827 :   extratop = zm_to_ZM( rowslicepermute(extramat, perm, 1, lig) );
     635       83827 :   if (li != lig)
     636             :   { /* zero out bottom part, using the Id block */
     637       83799 :     GEN A = vecslice(C, col+1, co);
     638       83799 :     GEN c = rowslicepermute(extramat, perm, lig+1, li);
     639       83799 :     extraC   = gsub(extraC, typ(A)==t_MAT? RgM_zm_mul(A, c): RgV_zm_mul(A,c));
     640       83798 :     extratop = ZM_sub(extratop, ZM_zm_mul(B, c));
     641             :   }
     642             : 
     643       83826 :   extramat = shallowconcat(extratop, vconcat(dep, H));
     644       83827 :   Cnew     = shallowconcat(extraC, vecslice(C, col-lH+1, co));
     645       83827 :   if (DEBUGLEVEL>5) err_printf("    1st phase done\n");
     646       83827 :   permpro = ZM_rowrankprofile(extramat, &nlze);
     647       83827 :   extramat = rowpermute(extramat, permpro);
     648       83827 :   *ptB     = rowpermute(B,        permpro);
     649       83827 :   permpro = vecsmallpermute(perm, permpro);
     650      411868 :   for (i=1; i<=lig; i++) perm[i] = permpro[i]; /* perm o= permpro */
     651             : 
     652       83827 :   *ptdep  = rowslice(extramat, 1, nlze);
     653       83827 :   matb    = rowslice(extramat, nlze+1, lig);
     654       83827 :   if (DEBUGLEVEL>5) err_printf("    2nd phase done\n");
     655       83827 :   H = hnffinal(matb,perm,ptdep,ptB,&Cnew);
     656       83827 :   *ptC = shallowconcat(vecslice(C, 1, col-lH), Cnew);
     657       83827 :   return H;
     658             : }
     659             : 
     660             : GEN
     661           0 : hnfadd(GEN H, GEN perm, GEN* ptdep, GEN* ptB, GEN* ptC, /* cf hnfspec */
     662             :        GEN extramat,GEN extraC)
     663             : {
     664           0 :   pari_sp av = avma;
     665           0 :   H = hnfadd_i(H, perm, ptdep, ptB, ptC, ZM_to_zm(extramat), extraC);
     666           0 :   return gc_all(av, 4, &H, ptC, ptdep, ptB);
     667             : }
     668             : 
     669             : /* zero aj = Aij (!= 0)  using  ak = Aik (maybe 0), via linear combination of
     670             :  * A[j] and A[k] of determinant 1. If U != NULL, likewise update its columns */
     671             : static void
     672    45826202 : ZC_elem(GEN aj, GEN ak, GEN A, GEN U, long j, long k)
     673             : {
     674             :   GEN p1,u,v,d;
     675             : 
     676    45826202 :   if (!signe(ak)) {
     677      107214 :     swap(gel(A,j), gel(A,k));
     678      107214 :     if (U) swap(gel(U,j), gel(U,k));
     679    42501388 :     return;
     680             :   }
     681    45718988 :   d = bezout(aj,ak,&u,&v);
     682             :   /* frequent special case (u,v) = (1,0) or (0,1) */
     683    45709887 :   if (!signe(u))
     684             :   { /* ak | aj */
     685    19835147 :     p1 = diviiexact(aj,ak); togglesign(p1);
     686    19837199 :     ZC_lincomb1_inplace(gel(A,j), gel(A,k), p1);
     687    19846701 :     if (U)
     688     1745770 :       ZC_lincomb1_inplace(gel(U,j), gel(U,k), p1);
     689    19846470 :     return;
     690             :   }
     691    25874740 :   if (!signe(v))
     692             :   { /* aj | ak */
     693    22549405 :     p1 = diviiexact(ak,aj); togglesign(p1);
     694    22547451 :     ZC_lincomb1_inplace(gel(A,k), gel(A,j), p1);
     695    22547701 :     swap(gel(A,j), gel(A,k));
     696    22547701 :     if (U) {
     697      331484 :       ZC_lincomb1_inplace(gel(U,k), gel(U,j), p1);
     698      331487 :       swap(gel(U,j), gel(U,k));
     699             :     }
     700    22547704 :     return;
     701             :   }
     702             : 
     703     3325335 :   if (!is_pm1(d)) { aj = diviiexact(aj, d); ak = diviiexact(ak, d); }
     704     3325312 :   p1 = gel(A,k); aj = negi(aj); /* NOT togglesign */
     705     3325640 :   gel(A,k) = ZC_lincomb(u,v, gel(A,j),p1);
     706     3325191 :   gel(A,j) = ZC_lincomb(aj,ak, p1,gel(A,j));
     707     3325370 :   if (U)
     708             :   {
     709      636140 :     p1 = gel(U,k);
     710      636140 :     gel(U,k) = ZC_lincomb(u,v, gel(U,j),p1);
     711      636161 :     gel(U,j) = ZC_lincomb(aj,ak, p1,gel(U,j));
     712             :   }
     713             : }
     714             : 
     715             : INLINE int
     716        1491 : is_RgX(GEN a, long v) { return typ(a) == t_POL && varn(a)==v; }
     717             : /* set u,v such that au + bv = gcd(a,b), divide a,b by the gcd */
     718             : static GEN
     719         378 : gbezout_step(GEN *pa, GEN *pb, GEN *pu, GEN *pv, long vx)
     720             : {
     721         378 :   GEN a = *pa, b = *pb, d;
     722         378 :   if (gequal0(a))
     723             :   {
     724           0 :     *pa = gen_0; *pu = gen_0;
     725           0 :     *pb = gen_1; *pv = gen_1; return b;
     726             :   }
     727         378 :   a = is_RgX(a,vx)? RgX_renormalize(a): scalarpol(a, vx);
     728         378 :   b = is_RgX(b,vx)? RgX_renormalize(b): scalarpol(b, vx);
     729         378 :   d = RgX_extgcd(a,b, pu,pv);
     730         378 :   if (degpol(d)) { a = RgX_div(a, d); b = RgX_div(b, d); }
     731         238 :   else if (typ(gel(d,2)) == t_REAL && lg(gel(d,2)) <= 3)
     732             : #if 1
     733             :   { /* possible accuracy problem */
     734           0 :     GEN D = RgX_gcd_simple(a,b);
     735           0 :     if (degpol(D)) {
     736           0 :       D = RgX_normalize(D);
     737           0 :       a = RgX_div(a, D);
     738           0 :       b = RgX_div(b, D);
     739           0 :       d = RgX_extgcd(a,b, pu,pv); /* retry now */
     740           0 :       d = RgX_mul(d, D);
     741             :     }
     742             :   }
     743             : #else
     744             :   { /* less stable */
     745             :     d = RgX_extgcd_simple(a,b, pu,pv);
     746             :     if (degpol(d)) { a = RgX_div(a, d); b = RgX_div(b, d); }
     747             :   }
     748             : #endif
     749         378 :   *pa = a;
     750         378 :   *pb = b; return d;
     751             : }
     752             : static GEN
     753     2633290 : col_mul(GEN x, GEN c)
     754             : {
     755     2633290 :   if (typ(x) == t_INT)
     756             :   {
     757     2633329 :     long s = signe(x);
     758     2633329 :     if (!s) return NULL;
     759     1997047 :     if (is_pm1(x)) return (s > 0)? c: RgC_neg(c);
     760             :   }
     761      305574 :   return RgC_Rg_mul(c, x);
     762             : }
     763             : static void
     764           0 : do_zero(GEN x)
     765             : {
     766           0 :   long i, lx = lg(x);
     767           0 :   for (i=1; i<lx; i++) gel(x,i) = gen_0;
     768           0 : }
     769             : 
     770             : /* (c1, c2) *= [u,-b; v,a] */
     771             : static void
     772      658323 : update(GEN u, GEN v, GEN a, GEN b, GEN *c1, GEN *c2)
     773             : {
     774             :   GEN p1,p2;
     775             : 
     776      658323 :   u = col_mul(u,*c1);
     777      658363 :   v = col_mul(v,*c2);
     778      658375 :   if (u) p1 = v? gadd(u,v): u;
     779        5800 :   else   p1 = v? v: NULL;
     780             : 
     781      658373 :   a = col_mul(a,*c2);
     782      658404 :   b = col_mul(gneg_i(b),*c1);
     783      658380 :   if (a) p2 = b? RgC_add(a,b): a;
     784           0 :   else   p2 = b? b: NULL;
     785             : 
     786      658295 :   if (!p1) do_zero(*c1); else *c1 = p1;
     787      658311 :   if (!p2) do_zero(*c2); else *c2 = p2;
     788      658311 : }
     789             : 
     790             : /* zero aj = Aij (!= 0)  using  ak = Aik (maybe 0), via linear combination of
     791             :  * A[j] and A[k] of determinant 1. If U != NULL, likewise update its columns */
     792             : static void
     793           7 : RgC_elem(GEN aj, GEN ak, GEN A, GEN V, long j, long k, long li, long vx)
     794             : {
     795           7 :   GEN u,v, d = gbezout_step(&aj, &ak, &u, &v, vx);
     796             :   long l;
     797             :   /* (A[,k], A[,j]) *= [v, -aj; u, ak ] */
     798          14 :   for (l = 1; l < li; l++)
     799             :   {
     800           7 :     GEN t = gadd(gmul(u,gcoeff(A,l,j)), gmul(v,gcoeff(A,l,k)));
     801           7 :     gcoeff(A,l,j) = gsub(gmul(ak,gcoeff(A,l,j)), gmul(aj,gcoeff(A,l,k)));
     802           7 :     gcoeff(A,l,k) = t;
     803             :   }
     804           7 :   gcoeff(A,li,j) = gen_0;
     805           7 :   gcoeff(A,li,k) = d;
     806           7 :   if (V) update(v,u,ak,aj,(GEN*)(V+k),(GEN*)(V+j));
     807           7 : }
     808             : 
     809             : /* reduce A[i,j] mod A[i,j0] for j=j0+1... via column operations */
     810             : static void
     811     4339928 : ZM_reduce(GEN A, GEN U, long i, long j0)
     812             : {
     813     4339928 :   long j, lA = lg(A);
     814     4339928 :   GEN d = gcoeff(A,i,j0);
     815     4339928 :   if (signe(d) < 0)
     816             :   {
     817       32347 :     ZV_neg_inplace(gel(A,j0));
     818       32347 :     if (U) ZV_togglesign(gel(U,j0));
     819       32347 :     d = gcoeff(A,i,j0);
     820             :   }
     821     9036196 :   for (j=j0+1; j<lA; j++)
     822             :   {
     823     4696245 :     GEN q = truedivii(gcoeff(A,i,j), d);
     824     4696224 :     if (!signe(q)) continue;
     825             : 
     826      261158 :     togglesign(q);
     827      261269 :     ZC_lincomb1_inplace(gel(A,j), gel(A,j0), q);
     828      261265 :     if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,j0), q);
     829             :   }
     830     4339951 : }
     831             : 
     832             : /* normalize T as if it were a t_POL in variable v */
     833             : static GEN
     834         280 : normalize_as_RgX(GEN T, long v, GEN *pd)
     835             : {
     836             :   GEN d;
     837         280 :   if (!is_RgX(T,v)) { *pd = T; return gen_1; }
     838         245 :   d = leading_coeff(T);
     839         245 :   while (gequal0(d) || (typ(d) == t_REAL && lg(d) == 3
     840           0 :                         && gexpo(T) - expo(d) > (long)BITS_IN_LONG)) {
     841          14 :     T = normalizepol_lg(T, lg(T)-1);
     842          14 :     if (!signe(T)) { *pd = gen_1; return T; }
     843           0 :     d = leading_coeff(T);
     844             :   }
     845         231 :   if (degpol(T)) T = RgX_Rg_div(T,d); else { d = gel(T,2); T = gen_1; }
     846         231 :   *pd = d; return T;
     847             : }
     848             : /* reduce A[i,j] mod A[i,j0] for j=j0+1... via column operations */
     849             : static void
     850          42 : RgM_reduce(GEN A, GEN U, long i, long j0, long vx)
     851             : {
     852          42 :   long j, lA = lg(A);
     853          42 :   GEN d, T = normalize_as_RgX(gcoeff(A,i,j0), vx, &d);
     854          42 :   if (U && !gequal1(d)) gel(U,j0) = RgC_Rg_div(gel(U,j0), d);
     855          42 :   gcoeff(A,i,j0) = T;
     856             : 
     857          49 :   for (j=j0+1; j<lA; j++)
     858             :   {
     859           7 :     GEN t = gcoeff(A,i,j), q;
     860           7 :     if (gequal0(t)) continue;
     861           7 :     if (T == gen_1)
     862           0 :       q = t;
     863           7 :     else if (is_RgX(t,vx))
     864           7 :       q = RgX_div(t, T);
     865           0 :     else continue;
     866             : 
     867           7 :     if (gequal0(q)) continue;
     868           0 :     gel(A,j) = RgC_sub(gel(A,j), RgC_Rg_mul(gel(A,j0), q));
     869           0 :     if (U) gel(U,j) = RgC_sub(gel(U,j), RgC_Rg_mul(gel(U,j0), q));
     870             :   }
     871          42 : }
     872             : 
     873             : /* A,B square integral in upper HNF, of the same dimension > 0. Return Au
     874             :  * in Z^n (v in Z^n not computed), such that Au + Bv = [1, 0, ..., 0] */
     875             : GEN
     876      627801 : hnfmerge_get_1(GEN A, GEN B)
     877             : {
     878      627801 :   pari_sp av = avma;
     879      627801 :   long j, k, l = lg(A), lb;
     880      627801 :   GEN b, U = cgetg(l + 1, t_MAT), C = cgetg(l + 1, t_VEC);
     881             : 
     882      627834 :   b = gcoeff(B,1,1); lb = lgefint(b);
     883     1171884 :   for (j = 1; j < l; j++)
     884             :   {
     885             :     GEN t;
     886     1171880 :     long c = j+1;
     887     1171880 :     gel(U,j) = col_ei(l-1, j);
     888     1172027 :     gel(U,c) = zerocol(l-1); /* dummy */
     889     1172030 :     gel(C,j) = vecslice(gel(A,j), 1,j);
     890     1172033 :     gel(C,c) = vecslice(gel(B,j), 1,j);
     891     3159856 :     for (k = j; k > 0; k--)
     892             :     {
     893     1988279 :       t = gcoeff(C,k,c);
     894     1988279 :       if (gequal0(t)) continue;
     895     1810468 :       setlg(C[c], k+1);
     896     1810458 :       ZC_elem(t, gcoeff(C,k,k), C, U, c, k);
     897     1809521 :       if (lgefint(gcoeff(C,k,k)) > lb) gel(C,k) = FpC_red(gel(C,k), b);
     898     1809521 :       if (j > 4)
     899             :       {
     900       80506 :         GEN u = gel(U,k);
     901             :         long h;
     902     1184338 :         for (h=1; h<l; h++)
     903     1103832 :           if (lgefint(gel(u,h)) > lb) gel(u,h) = remii(gel(u,h), b);
     904             :       }
     905             :     }
     906     1171577 :     if (j == 1)
     907      627570 :       t = gcoeff(C,1,1);
     908             :     else
     909             :     {
     910             :       GEN u;
     911      544007 :       t = bezout(gcoeff(C,1,1), b, &u, NULL); /* >= 0 */
     912      544332 :       if (signe(u) && !equali1(u)) gel(U,1) = ZC_Z_mul(gel(U,1), u);
     913      544341 :       gcoeff(C,1,1) = t;
     914             :     }
     915     1171911 :     if (equali1(t)) break;
     916             :   }
     917      627733 :   if (j >= l) return NULL;
     918      627733 :   b = lcmii(gcoeff(A,1,1),b);
     919      627729 :   A = FpC_red(ZM_ZC_mul(A,gel(U,1)), b);
     920      627587 :   return gerepileupto(av, FpC_center(A, b, shifti(b,-1)));
     921             : }
     922             : 
     923             : /* remove the first r columns */
     924             : static void
     925      308062 : remove_0cols(long r, GEN *pA, GEN *pB, long remove)
     926             : {
     927      308062 :   GEN A = *pA, B = *pB;
     928      308062 :   long l = lg(A);
     929      308062 :   A += r; A[0] = evaltyp(t_MAT) | evallg(l-r);
     930      308062 :   if (B && remove == 2) { B += r; B[0] = A[0]; }
     931      308062 :   *pA = A; *pB = B;
     932      308062 : }
     933             : 
     934             : /* Inefficient compared to hnfall. 'remove' = throw away lin.dep columns */
     935             : static GEN
     936       55762 : hnf_i(GEN A, int remove)
     937             : {
     938       55762 :   pari_sp av0 = avma, av;
     939             :   long s, n, m, j, k, li, def, ldef;
     940             : 
     941       55762 :   RgM_dimensions(A, &m, &n);
     942       55762 :   if (!n) return cgetg(1,t_MAT);
     943       55538 :   av = avma;
     944       55538 :   A = RgM_shallowcopy(A);
     945       55538 :   def = n; ldef = (m>n)? m-n: 0;
     946      150508 :   for (li=m; li>ldef; li--)
     947             :   {
     948      387739 :     for (j=def-1; j; j--)
     949             :     {
     950      292769 :       GEN a = gcoeff(A,li,j);
     951      292769 :       if (!signe(a)) continue;
     952             : 
     953             :       /* zero a = Aij  using  b = Aik */
     954      148136 :       k = (j==1)? def: j-1;
     955      148136 :       ZC_elem(a,gcoeff(A,li,k), A,NULL, j,k);
     956      148136 :       if (gc_needed(av,1))
     957             :       {
     958           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnf[1]. li=%ld",li);
     959           0 :         A = gerepilecopy(av, A);
     960             :       }
     961             :     }
     962       94970 :     s = signe(gcoeff(A,li,def));
     963       94970 :     if (s)
     964             :     {
     965       94260 :       if (s < 0) ZV_neg_inplace(gel(A,def));
     966       94260 :       ZM_reduce(A, NULL, li,def);
     967       94260 :       def--;
     968             :     }
     969             :     else
     970         710 :       if (ldef) ldef--;
     971       94970 :     if (gc_needed(av,1))
     972             :     {
     973           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnf[2]. li=%ld",li);
     974           0 :       A = gerepilecopy(av, A);
     975             :     }
     976             :   }
     977             :   /* rank A = n - def */
     978       55538 :   if (remove) { GEN B = NULL; remove_0cols(def, &A, &B, remove); }
     979       55538 :   return gerepileupto(av0, ZM_copy(A));
     980             : }
     981             : 
     982             : GEN
     983       76483 : ZM_hnf(GEN x) { return lg(x) > 8? ZM_hnfall(x, NULL, 1): hnf_i(x, 1); }
     984             : 
     985             : /* u*z[1..k] mod p, in place */
     986             : static void
     987     5468952 : FpV_Fp_mul_part_ip(GEN z, GEN u, GEN p, long k)
     988             : {
     989             :   long i;
     990     5468952 :   if (is_pm1(u)) {
     991      188437 :     if (signe(u) > 0) {
     992      563351 :       for (i = 1; i <= k; i++)
     993      427731 :         if (signe(gel(z,i))) gel(z,i) = modii(gel(z,i), p);
     994             :     } else {
     995      145897 :       for (i = 1; i <= k; i++)
     996       93082 :         if (signe(gel(z,i))) gel(z,i) = modii(negi(gel(z,i)), p);
     997             :     }
     998             :   }
     999             :   else {
    1000    23512686 :     for (i = 1; i <= k; i++)
    1001    18232996 :       if (signe(gel(z,i))) gel(z,i) = Fp_mul(u,gel(z,i), p);
    1002             :   }
    1003     5468125 : }
    1004             : static void
    1005    37208023 : FpV_red_part_ipvec(GEN z, GEN p, long k)
    1006             : {
    1007             :   long i;
    1008   130054672 :   for (i = 1; i <= k; i++) gel(z,i) = modii(gel(z,i), gel(p,i));
    1009    37200884 : }
    1010             : 
    1011             : /* return x * U, in echelon form (mod p^m), where (det(U),p) = 1.
    1012             :  * If early_abort is set, return NULL as soon as one pivot is 0 (mod p^m) */
    1013             : GEN
    1014      479876 : ZpM_echelon(GEN x, long early_abort, GEN p, GEN pm)
    1015             : {
    1016      479876 :   pari_sp av0 = avma, av;
    1017             :   long m, li, co, i, j, k, def, ldef;
    1018             : 
    1019      479876 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1020      479876 :   li = lgcols(x);
    1021      479877 :   av = avma;
    1022      479877 :   x = RgM_shallowcopy(x);
    1023      479880 :   m = Z_pval(pm, p);
    1024             : 
    1025      479878 :   ldef = (li > co)? li - co: 0;
    1026     2353581 :   for (def = co-1,i = li-1; i > ldef; i--)
    1027             :   {
    1028     1874389 :     long vmin = LONG_MAX, kmin = 0;
    1029     1874389 :     GEN umin = gen_0, pvmin, q;
    1030     9514604 :     for (k = 1; k <= def; k++)
    1031             :     {
    1032     8046677 :       GEN u = gcoeff(x,i,k);
    1033             :       long v;
    1034     8046677 :       if (!signe(u)) continue;
    1035     3737431 :       v = Z_pvalrem(u, p, &u);
    1036     3737327 :       if (v >= m) gcoeff(x,i,k) = gen_0;
    1037     2604840 :       else if (v < vmin) {
    1038     1291108 :         vmin = v; kmin = k; umin = u;
    1039     1291108 :         if (!vmin) break;
    1040             :       }
    1041             :     }
    1042     1874285 :     if (!kmin)
    1043             :     {
    1044      922684 :       if (early_abort) return NULL;
    1045      922007 :       gcoeff(x,i,def) = gen_0;
    1046      922007 :       ldef--;
    1047      922007 :       if (ldef < 0) ldef = 0;
    1048      922007 :       continue;
    1049             :     }
    1050      951601 :     if (kmin != def) swap(gel(x,def), gel(x,kmin));
    1051      951601 :     q = vmin? powiu(p, m-vmin): pm;
    1052             :     /* pivot has valuation vmin */
    1053      951596 :     umin = modii(umin, q);
    1054      951639 :     if (!equali1(umin))
    1055      471941 :       FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(umin,q), pm, i-1);
    1056      951643 :     gcoeff(x, i, def) = pvmin = powiu(p, vmin);
    1057     6765266 :     for (j = def-1; j; j--)
    1058             :     { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
    1059     5813570 :       GEN t, a = gcoeff(x,i,j) = modii(gcoeff(x,i,j), pm);
    1060     5813052 :       if (!signe(a)) continue;
    1061             : 
    1062     2667562 :       t = diviiexact(a, pvmin); togglesign(t);
    1063     2668118 :       ZC_lincomb1_inplace(gel(x,j), gel(x,def), t);
    1064     2668090 :       if (gc_needed(av,1))
    1065             :       {
    1066          14 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZpM_echelon. i=%ld",i);
    1067          14 :         x = gerepilecopy(av, x); pvmin = gcoeff(x,i,def);
    1068             :       }
    1069             :     }
    1070      951696 :     def--;
    1071             :   }
    1072      479192 :   if (co > li)
    1073             :   {
    1074           0 :     x += co - li;
    1075           0 :     x[0] = evaltyp(t_MAT) | evallg(li);
    1076             :   }
    1077      479192 :   return gerepilecopy(av0, x);
    1078             : }
    1079             : GEN
    1080     3911025 : zlm_echelon(GEN x, long early_abort, ulong p, ulong pm)
    1081             : {
    1082     3911025 :   pari_sp av0 = avma;
    1083             :   long li, co, i, j, k, def, ldef;
    1084             :   ulong m;
    1085             : 
    1086     3911025 :   co = lg(x); if (co == 1) return cgetg(1,t_MAT);
    1087     3911025 :   li = lgcols(x);
    1088     3911128 :   x = Flm_copy(x);
    1089     3911166 :   m = u_lval(pm, p);
    1090             : 
    1091     3911176 :   ldef = (li > co)? li - co: 0;
    1092    18590603 :   for (def = co-1,i = li-1; i > ldef; i--)
    1093             :   {
    1094    14865248 :     long vmin = LONG_MAX, kmin = 0;
    1095    14865248 :     ulong umin = 0, pvmin, q;
    1096    48359917 :     for (k = 1; k <= def; k++)
    1097             :     {
    1098    41531118 :       ulong u = ucoeff(x,i,k);
    1099             :       long v;
    1100    41531118 :       if (!u) continue;
    1101    21468922 :       v = u_lvalrem(u, p, &u);
    1102    21468902 :       if (v >= (long) m) ucoeff(x,i,k) = 0;
    1103    21468902 :       else if (v < vmin) {
    1104    14947668 :         vmin = v; kmin = k; umin = u;
    1105    14947668 :         if (!vmin) break;
    1106             :       }
    1107             :     }
    1108    14865228 :     if (!kmin)
    1109             :     {
    1110     1009980 :       if (early_abort) return NULL;
    1111      824994 :       ucoeff(x,i,def) = 0;
    1112      824994 :       ldef--;
    1113      824994 :       if (ldef < 0) ldef = 0;
    1114      824994 :       continue;
    1115             :     }
    1116    13855248 :     if (kmin != def) swap(gel(x,def), gel(x,kmin));
    1117    13855248 :     q = vmin? upowuu(p, m-vmin): pm;
    1118             :     /* pivot has valuation vmin */
    1119    13855298 :     umin %= q;
    1120    13855298 :     if (umin != 1)
    1121     6980275 :       Flv_Fl_mul_part_inplace(gel(x,def), Fl_inv(umin,q), pm, i-1);
    1122    13855295 :     ucoeff(x, i, def) = pvmin = upowuu(p, vmin);
    1123    66448270 :     for (j = def-1; j; j--)
    1124             :     { /* zero x[i, 1..def-1] using x[i,def] = pvmin */
    1125    52593837 :       ulong t, a = ucoeff(x,i,j);
    1126    52593837 :       if (!a) continue;
    1127             : 
    1128    28864202 :       t = Fl_neg(a / pvmin, q);
    1129    28863968 :       Flc_lincomb1_inplace(gel(x,j), gel(x,def), t, pm);
    1130             :     }
    1131    13854433 :     def--;
    1132             :   }
    1133     3725355 :   if (co > li)
    1134             :   {
    1135           0 :     x += co - li;
    1136           0 :     x[0] = evaltyp(t_MAT) | evallg(li);
    1137             :   }
    1138     3725355 :   return gerepilecopy(av0, x);
    1139             : }
    1140             : 
    1141             : static int
    1142      202643 : ZV_allequal(GEN v)
    1143             : {
    1144      202643 :   long i, l = lg(v);
    1145      202643 :   if (l > 1)
    1146             :   {
    1147      202643 :     GEN x = gel(v,1);
    1148      370761 :     for (i = 2; i < l; i++) if (!equalii(x,gel(v,i))) return 0;
    1149             :   }
    1150      196790 :   return 1;
    1151             : }
    1152             : /* compute optimal D for hnfmod: x upper triangular */
    1153             : static GEN
    1154     4359895 : optimal_D(GEN x, GEN D)
    1155             : {
    1156     4359895 :   long i, n = nbrows(x);
    1157     4360277 :   GEN C = shallowcopy(D);
    1158     4361382 :   gel(C,1) = gcoeff(x,1,1);
    1159     4824484 :   for (i = 2; i < n; i++)
    1160             :   {
    1161     2812692 :     GEN c = mulii(gel(C,i-1), gcoeff(x,i,i));
    1162     2812408 :     if (signe(c) < 0) togglesign(c);
    1163     2812418 :     if (cmpii(c, gel(D,i)) >= 0) break;
    1164      463102 :     gel(C,i) = c;
    1165             :   }
    1166     4361278 :   return C;
    1167             : }
    1168             : 
    1169             : /* D = multiple of det x (usually detint(x)) or vector of positive moduli
    1170             :  * (compute hnf(x | D))
    1171             :  * flag & hnf_MODID: reduce mod D * matid [ otherwise as above ].
    1172             :  * flag & hnf_PART: don't reduce once diagonal is known
    1173             :  * flag & hnf_CENTER: centermod HNF (2|x[i,j]| <] x[i,i]) */
    1174             : GEN
    1175     4363100 : ZM_hnfmodall_i(GEN x, GEN D, long flag)
    1176             : {
    1177             :   pari_sp av;
    1178     4363100 :   const long center = (flag & hnf_CENTER);
    1179             :   long moddiag, modsame, nli, li, co, i, j, k, def, ldef;
    1180             :   GEN u, LDM;
    1181             : 
    1182     4363100 :   co = lg(x);
    1183     4363100 :   if (co == 1)
    1184             :   {
    1185        1554 :     if (typ(D) == t_INT || lg(D) == 1) return cgetg(1,t_MAT);
    1186         105 :     x = diagonal_shallow(D); /* handle flags properly */
    1187         105 :     co = lg(x);
    1188             :   }
    1189     4361651 :   li = lgcols(x);
    1190     4361854 :   if (li == 1)
    1191             :   {
    1192          36 :     if (typ(D) != t_INT && lg(D) != li) pari_err_DIM("ZM_hnfmod");
    1193          36 :     return cgetg(1,t_MAT);
    1194             :   }
    1195     4361837 :   nli = li - 1;
    1196     4361837 :   modsame = typ(D)==t_INT;
    1197     4361837 :   if (!modsame)
    1198             :   {
    1199      202658 :     if (lg(D) != li) pari_err_DIM("ZM_hnfmod");
    1200      202644 :     if (ZV_allequal(D)) { modsame = 1; D = gel(D,1); }
    1201             :   }
    1202     4361820 :   moddiag = (flag & hnf_MODID) || !modsame;
    1203             :   /* modsame: triangularize mod fixed d*Id;
    1204             :    * moddiag: modulo diagonal matrix, else modulo multiple of determinant */
    1205             : 
    1206     4361820 :   if (modsame)
    1207             :   {
    1208     4355978 :     LDM = const_vecsmall(nli, 2*lgefint(D)-2);
    1209     4356030 :     D = const_vec(nli,D);
    1210             :   }
    1211             :   else
    1212             :   {
    1213        5842 :     LDM = cgetg(li, t_VECSMALL);
    1214       23142 :     for (i=1; i<li; i++) LDM[i] = lgefint(gel(D,i));
    1215             :   }
    1216     4361860 :   av = avma;
    1217     4361860 :   x = RgM_shallowcopy(x);
    1218             : 
    1219     4362041 :   ldef = 0;
    1220     4362041 :   if (li > co)
    1221             :   {
    1222      118901 :     ldef = li - co;
    1223      118901 :     if (!moddiag)
    1224           7 :       pari_err_DOMAIN("ZM_hnfmod","nb lines",">", strtoGENstr("nb columns"), x);
    1225             :   }
    1226    19307355 :   for (def = co-1,i = nli; i > ldef; i--,def--)
    1227             :   {
    1228    14943178 :     GEN d = gel(D,i);
    1229    14943178 :     long add_N = modsame;
    1230    80369679 :     for (j = 1; j < def; j++)
    1231             :     {
    1232    65424347 :       GEN p1, p2, b, a = gcoeff(x,i,j) = remii(gcoeff(x,i,j), d);
    1233    84302320 :       if (!signe(a)) continue;
    1234             : 
    1235    45592973 :       k = j+1;
    1236    45592973 :       b = gcoeff(x,i,k) = remii(gcoeff(x,i,k), d);
    1237    45598714 :       if (!signe(b)) { swap(gel(x,j), gel(x,k)); continue; }
    1238    26710355 :       if (add_N)
    1239             :       { /* ensure the moving pivot on row i divides d from now on */
    1240     8399304 :         add_N = 0;
    1241     8399304 :         if (!equali1(a))
    1242             :         { /* x[j] *= u; after this, a = x[i,j] | d */
    1243     6587092 :           GEN u = Fp_invgen(a, d, &a);
    1244             :           long t;
    1245     6588312 :           p1 = gel(x,j);
    1246    26643230 :           for (t = 1; t < i; t++) gel(p1,t) = mulii(gel(p1,t), u);
    1247     6586519 :           FpV_red_part_ipvec(p1, D, i-1);
    1248     6586483 :           gel(p1,i) = a;
    1249     6586483 :           if (2*lg(a) < lg(b))
    1250             :           { /* reduce x[i,k] mod x[i,j]: helps ZC_elem */
    1251       26481 :             GEN r, q = dvmdii(b, a, &r);
    1252       26481 :             togglesign(q);
    1253       26481 :             ZC_lincomb1_inplace_i(gel(x,k), gel(x,j), q, i-1);
    1254       26481 :             FpV_red_part_ipvec(gel(x,k), D, i-1);
    1255       26481 :             gcoeff(x,i,k) = b = r;
    1256             :           }
    1257             :         }
    1258             :       }
    1259    26709947 :       ZC_elem(a,b, x, NULL, j,k);
    1260    26717155 :       p1 = gel(x,j);
    1261    26717155 :       p2 = gel(x,k);
    1262             :       /* prevent coeffs explosion */
    1263   154490440 :       for (k = 1; k < i; k++)
    1264             :       {
    1265   127773286 :         if (lgefint(gel(p1,k)) > LDM[k]) gel(p1,k) = remii(gel(p1,k),gel(D,k));
    1266   127773285 :         if (lgefint(gel(p2,k)) > LDM[k]) gel(p2,k) = remii(gel(p2,k),gel(D,k));
    1267             :       }
    1268             :     }
    1269    14945332 :     if (gc_needed(av,2))
    1270             :     {
    1271           7 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[1]. i=%ld",i);
    1272           7 :       x = gerepilecopy(av, x);
    1273             :     }
    1274    14945332 :     if (moddiag && !signe(gcoeff(x,i,def)))
    1275             :     { /* missing pivot on line i, insert column */
    1276      847482 :       GEN a = cgetg(co + 1, t_MAT);
    1277     4655708 :       for (k = 1; k <= def; k++) gel(a,k) = gel(x,k);
    1278      847481 :       gel(a,k++) = Rg_col_ei(gel(D,i), nli, i);
    1279     3947022 :       for (     ; k <= co;  k++) gel(a,k) = gel(x,k-1);
    1280      847483 :       ldef--; if (ldef < 0) ldef = 0;
    1281      847483 :       co++; def++; x = a;
    1282             :     }
    1283             :   }
    1284     4364177 :   if (co < li)
    1285             :   { /* implies moddiag, add missing diag(D) components */
    1286       89010 :     GEN a = cgetg(li+1, t_MAT);
    1287      212640 :     for (k = 1; k <= li-co; k++) gel(a,k) = Rg_col_ei(gel(D,k), nli, k);
    1288      253294 :     for (i = 1; i < co; i++) gel(a,k-1+i) = gel(x,i);
    1289       89008 :     gel(a,li) = zerocol(nli); x = a;
    1290             :   }
    1291             :   else
    1292             :   {
    1293     4275167 :     x += co - li;
    1294     4275167 :     x[0] = evaltyp(t_MAT) | evallg(li); /* kill 0 columns */
    1295     4272745 :     if (moddiag) x = shallowconcat(x, zerocol(nli));
    1296             :   }
    1297     4361893 :   if (moddiag)
    1298             :   { /* x[li]: extra column, an accumulator discarded at the end */
    1299             :     GEN D2;
    1300     4208895 :     gcoeff(x,1,1) = gcdii(gcoeff(x,1,1), gel(D,1));
    1301     4206984 :     D2 = optimal_D(x,D);
    1302             :     /* add up missing diag(D) components */
    1303    18850502 :     for (i = nli; i > 0; i--)
    1304             :     {
    1305    14642082 :       gcoeff(x, i, li) = gel(D,i);
    1306    63604939 :       for (j = i; j > 0; j--)
    1307             :       {
    1308    48965998 :         GEN a = gcoeff(x, j, li);
    1309    48965998 :         if (!signe(a)) continue;
    1310    15307771 :         ZC_elem(a, gcoeff(x,j,j), x, NULL, li,j);
    1311    15305338 :         FpV_red_part_ipvec(gel(x,li), D, j-1);
    1312    15306464 :         FpV_red_part_ipvec(gel(x,j),  D, j-1);
    1313             :       }
    1314    14638941 :       if (gc_needed(av,1))
    1315             :       {
    1316          18 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[2]. i=%ld", i);
    1317          18 :         gerepileall(av, 2, &x, &D2);
    1318             :       }
    1319             :     }
    1320     4208420 :     D = D2;
    1321             :   }
    1322             :   else
    1323             :   {
    1324      152998 :     GEN b = gel(D,1);
    1325      574971 :     for (i = nli; i > 0; i--)
    1326             :     {
    1327      421973 :       GEN d = bezout(gcoeff(x,i,i),b, &u,NULL);
    1328      421987 :       gcoeff(x,i,i) = d;
    1329      421987 :       FpV_Fp_mul_part_ip(gel(x,i), u, b, i-1);
    1330      421988 :       if (i > 1) b = diviiexact(b,d);
    1331             :     }
    1332      152998 :     D = optimal_D(x,D);
    1333             :   }
    1334     4361417 :   x[0] = evaltyp(t_MAT) | evallg(li); /* kill 0 columns / discard accumulator */
    1335     4361449 :   if (flag & hnf_PART) return x;
    1336             : 
    1337    19424215 :   for (i = nli; i > 0; i--)
    1338             :   {
    1339    15066740 :     GEN diag = gcoeff(x,i,i);
    1340    15066740 :     if (signe(diag) < 0) { gel(x,i) = ZC_neg(gel(x,i)); diag = gcoeff(x,i,i); }
    1341    15066750 :     if (i != nli)
    1342    34943673 :       for (j = 1; j < i; j++) gcoeff(x,j,i) = remii(gcoeff(x,j,i), gel(D,j));
    1343    50006313 :     for (j = i+1; j < li; j++)
    1344             :     {
    1345    34941713 :       GEN b = gcoeff(x,i,j) = remii(gcoeff(x,i,j), gel(D,i));
    1346    34939512 :       GEN r, q = truedvmdii(b, diag, &r);
    1347             :       /* ensure -diag/2 <= r < diag/2 */
    1348    34939371 :       if (center && signe(r) && abscmpii(shifti(r,1),diag) >= 0)
    1349      631291 :       { r = subii(r,diag); q = addiu(q,1); }
    1350    34941623 :       if (!signe(q)) continue;
    1351    16125817 :       togglesign(q);
    1352    16125865 :       ZC_lincomb1_inplace_i(gel(x,j), gel(x,i), q, i-1);
    1353    16123937 :       gcoeff(x,i,j) = r;
    1354             :     }
    1355    15064600 :     if (gc_needed(av,1))
    1356             :     {
    1357          17 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfmod[3]. i=%ld", i);
    1358          17 :       gerepileall(av, 2, &x, &D);
    1359             :     }
    1360             :   }
    1361     4357475 :   return x;
    1362             : }
    1363             : GEN
    1364     4314563 : ZM_hnfmodall(GEN x, GEN dm, long flag)
    1365             : {
    1366     4314563 :   pari_sp av = avma;
    1367     4314563 :   return gerepilecopy(av, ZM_hnfmodall_i(x, dm, flag));
    1368             : }
    1369             : GEN
    1370      152992 : ZM_hnfmod(GEN x, GEN d) { return ZM_hnfmodall(x,d,0); }
    1371             : GEN
    1372     3778797 : ZM_hnfmodid(GEN x, GEN d)
    1373     3778797 : { return ZM_hnfmodall(x,d,hnf_MODID); }
    1374             : /* return the column echelon form of x with 1's as pivots,
    1375             :  * P contains the row indices containing the pivots in increasing order */
    1376             : static GEN
    1377     2479554 : FpM_echelon(GEN x, GEN *pP, GEN p)
    1378             : {
    1379             :   pari_sp av;
    1380             :   long iP, li, co, i, j, k, def, ldef;
    1381             :   GEN P;
    1382             : 
    1383     2479554 :   co = lg(x); if (co == 1) { *pP = cgetg(1,t_VECSMALL); return cgetg(1,t_MAT); }
    1384     2479554 :   li = lgcols(x);
    1385     2479546 :   iP = 1;
    1386     2479546 :   *pP = P = cgetg(li, t_VECSMALL);
    1387     2479577 :   av = avma;
    1388     2479577 :   x = FpM_red(x, p);
    1389             : 
    1390     2478898 :   ldef = (li > co)? li - co: 0;
    1391    10832304 :   for (def = co-1,i = li-1; i > ldef; i--)
    1392             :   {
    1393     8351294 :     GEN u = NULL;
    1394    12490325 :     for (k = def; k; k--)
    1395             :     {
    1396     9781065 :       u = gcoeff(x,i,k);
    1397     9781065 :       if (signe(u)) break;
    1398             :     }
    1399     8351294 :     if (!k)
    1400             :     {
    1401     2711025 :       if (--ldef < 0) ldef = 0;
    1402     2711025 :       continue;
    1403             :     }
    1404     5640269 :     P[iP++] = i;
    1405     5640269 :     if (k != def) swap(gel(x,def), gel(x,k));
    1406     5640269 :     if (!equali1(u))
    1407     4575345 :       FpV_Fp_mul_part_ip(gel(x,def), Fp_inv(u,p), p, i-1);
    1408     5641791 :     gcoeff(x, i, def) = gen_1;
    1409    21958390 :     for (j = def-1; j; j--)
    1410             :     { /* zero x[i, 1..def-1] using x[i,def] = 1*/
    1411    16316923 :       GEN xj = gel(x,j), u = gel(xj,i);
    1412    16316923 :       if (!signe(u)) continue;
    1413             : 
    1414     8989503 :       ZC_lincomb1_inplace(xj, gel(x,def), negi(u));
    1415    53510131 :       for (k = 1; k < i; k++) gel(xj,k) = modii(gel(xj,k), p);
    1416             :     }
    1417     5641467 :     if (gc_needed(av,2))
    1418             :     {
    1419           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpM_echelon. i=%ld",i);
    1420           0 :       x = gerepilecopy(av, x);
    1421             :     }
    1422     5642381 :     def--;
    1423             :   }
    1424             :   /* rank = iP - 1 */
    1425     2481010 :   setlg(P, iP); vecsmall_sort(P);
    1426     2479631 :   if (co > iP) x += co - iP;
    1427     2479631 :   x[0] = evaltyp(t_MAT) | evallg(iP);
    1428     2479622 :   return x;
    1429             : }
    1430             : /* given x square of maximal rank with 1 or p on diagonal from hnfmodid
    1431             :  * (=> a column containing p has its other entries at 0 ), return the HNF */
    1432             : static GEN
    1433     2479693 : FpM_hnfend(pari_sp av, GEN x, GEN p)
    1434             : {
    1435     2479693 :   long i, l = lgcols(x);
    1436    10831935 :   for (i = l-1; i > 0; i--)
    1437             :   {
    1438     8353756 :     GEN diag = gcoeff(x,i,i);
    1439             :     long j;
    1440     8353756 :     if (is_pm1(diag))
    1441    15586785 :       for (j = i+1; j < l; j++)
    1442             :       {
    1443     9944086 :         GEN xj = gel(x,j), b = gel(xj,i);
    1444             :         long k;
    1445     9944086 :         if (!signe(b)) continue;
    1446     7716304 :         ZC_lincomb1_inplace(xj, gel(x,i), negi(b));
    1447    33799154 :         for (k=1; k<i; k++)
    1448    26083157 :           if (lgefint(gel(xj,k)) > 3) gel(xj,k) = remii(gel(xj,k), p);
    1449             :       }
    1450             :     else
    1451     9406929 :       for (j = i+1; j < l; j++) gcoeff(x,i,j) = modii(gcoeff(x,i,j), p);
    1452     8353003 :     if (gc_needed(av,2))
    1453             :     {
    1454           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpM_hnfend. i=%ld",i);
    1455           0 :       x = gerepilecopy(av, x);
    1456             :     }
    1457             :   }
    1458     2478179 :   return x;
    1459             : }
    1460             : GEN
    1461     2479562 : ZM_hnfmodprime(GEN x, GEN p)
    1462             : {
    1463     2479562 :   pari_sp av = avma;
    1464             :   GEN P, y;
    1465             :   long l, lP, i;
    1466     2479562 :   if (lg(x) == 1) return cgetg(1, t_MAT);
    1467     2479562 :   l = lgcols(x);
    1468     2479556 :   x = FpM_echelon(x, &P, p);
    1469     2479622 :   lP = lg(P); /* rank = lP-1 */
    1470     2479622 :   if (lP == l) { set_avma(av); return matid(l-1); }
    1471     2479622 :   y = scalarmat_shallow(p, l-1);
    1472     8122734 :   for (i = 1; i < lP; i++) gel(y,P[i]) = gel(x,i);
    1473     2479680 :   return gerepilecopy(av, FpM_hnfend(av,y,p));
    1474             : }
    1475             : 
    1476             : static GEN
    1477      381555 : allhnfmod(GEN x, GEN dm, int flag)
    1478             : {
    1479      381555 :   if (typ(x)!=t_MAT) pari_err_TYPE("allhnfmod",x);
    1480      381555 :   RgM_check_ZM(x, "allhnfmod");
    1481      381549 :   if (isintzero(dm)) return ZM_hnf(x);
    1482      381549 :   return ZM_hnfmodall(x, dm, flag);
    1483             : }
    1484             : GEN
    1485          14 : hnfmod(GEN x, GEN d)
    1486             : {
    1487          14 :   if (typ(d) != t_INT) pari_err_TYPE("mathnfmod",d);
    1488          14 :   return allhnfmod(x, d, 0);
    1489             : }
    1490             : GEN
    1491      381541 : hnfmodid(GEN x, GEN d)
    1492             : {
    1493      381541 :   switch(typ(d))
    1494             :   {
    1495      380197 :     case t_INT: break;
    1496        1344 :     case t_VEC: case t_COL:
    1497        1344 :       if (RgV_is_ZV(d)) break;
    1498           0 :     default: pari_err_TYPE("mathnfmodid",d);
    1499             :   }
    1500      381541 :   return allhnfmod(x, d, hnf_MODID);
    1501             : }
    1502             : 
    1503             : /* M a ZM in HNF. Normalize with *centered* residues */
    1504             : GEN
    1505       25052 : ZM_hnfcenter(GEN M)
    1506             : {
    1507       25052 :   long i, j, k, N = lg(M)-1;
    1508       25052 :   pari_sp av = avma;
    1509             : 
    1510       88354 :   for (j=N-1; j>0; j--) /* skip last line */
    1511             :   {
    1512       63300 :     GEN Mj = gel(M,j), a = gel(Mj,j);
    1513      211142 :     for (k = j+1; k <= N; k++)
    1514             :     {
    1515      147840 :       GEN Mk = gel(M,k), q = diviiround(gel(Mk,j), a);
    1516      147843 :       long s = signe(q);
    1517      147843 :       if (!s) continue;
    1518       69361 :       if (is_pm1(q))
    1519             :       {
    1520       31356 :         if (s < 0)
    1521        8008 :           for (i = 1; i <= j; i++) gel(Mk,i) = addii(gel(Mk,i), gel(Mj,i));
    1522             :         else
    1523       89827 :           for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), gel(Mj,i));
    1524             :       }
    1525             :       else
    1526      211774 :         for (i = 1; i <= j; i++) gel(Mk,i) = subii(gel(Mk,i), mulii(q,gel(Mj,i)));
    1527       69362 :       if (gc_needed(av,1))
    1528             :       {
    1529           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfcenter, j = %ld",j);
    1530           0 :         M = gerepilecopy(av, M);
    1531             :       }
    1532             :     }
    1533             :   }
    1534       25054 :   return M;
    1535             : }
    1536             : 
    1537             : /***********************************************************************/
    1538             : /*                                                                     */
    1539             : /*                 HNFLLL (Havas, Majewski, Mathews)                   */
    1540             : /*                                                                     */
    1541             : /***********************************************************************/
    1542             : 
    1543             : static void
    1544     1981029 : Minus(long j, GEN lambda)
    1545             : {
    1546     1981029 :   long k, n = lg(lambda);
    1547             : 
    1548    10622437 :   for (k=1  ; k<j; k++) togglesign_safe(&gcoeff(lambda,k,j));
    1549    15841933 :   for (k=j+1; k<n; k++) togglesign_safe(&gcoeff(lambda,j,k));
    1550     1981015 : }
    1551             : 
    1552             : /* index of first nonzero entry */
    1553             : static long
    1554    82302641 : findi(GEN M)
    1555             : {
    1556    82302641 :   long i, n = lg(M);
    1557   816456439 :   for (i=1; i<n; i++)
    1558   789061282 :     if (signe(gel(M,i))) return i;
    1559    27395157 :   return 0;
    1560             : }
    1561             : 
    1562             : static long
    1563    82295236 : findi_normalize(GEN Aj, GEN B, long j, GEN lambda)
    1564             : {
    1565    82295236 :   long r = findi(Aj);
    1566    82353393 :   if (r && signe(gel(Aj,r)) < 0)
    1567             :   {
    1568     1980982 :     ZV_togglesign(Aj); if (B) ZV_togglesign(gel(B,j));
    1569     1981037 :     Minus(j,lambda);
    1570             :   }
    1571    82356265 :   return r;
    1572             : }
    1573             : 
    1574             : static void
    1575    41126271 : reduce2(GEN A, GEN B, long k, long j, long *row0, long *row1, GEN lambda, GEN D)
    1576             : {
    1577             :   GEN q;
    1578             :   long i;
    1579             : 
    1580    41126271 :   *row0 = findi_normalize(gel(A,j), B,j,lambda);
    1581    41183324 :   *row1 = findi_normalize(gel(A,k), B,k,lambda);
    1582    41199080 :   if (*row0)
    1583    21880645 :     q = truedivii(gcoeff(A,*row0,k), gcoeff(A,*row0,j));
    1584    19318435 :   else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
    1585     6314103 :     q = diviiround(gcoeff(lambda,j,k), gel(D,j));
    1586             :   else
    1587    12997418 :     return;
    1588             : 
    1589    28178953 :   if (signe(q))
    1590             :   {
    1591    13357496 :     GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
    1592    13357496 :     togglesign_safe(&q);
    1593    13372712 :     if (*row0) ZC_lincomb1_inplace(gel(A,k),gel(A,j),q);
    1594    13370444 :     if (B) ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
    1595    13321298 :     gel(Lk,j) = addmulii(gel(Lk,j), q, gel(D,j));
    1596    13302273 :     if (is_pm1(q))
    1597             :     {
    1598     5208743 :       if (signe(q) > 0)
    1599             :       {
    1600     4709334 :         for (i=1; i<j; i++)
    1601     2750160 :           if (signe(gel(Lj,i))) gel(Lk,i) = addii(gel(Lk,i), gel(Lj,i));
    1602             :       }
    1603             :       else
    1604             :       {
    1605     9394159 :         for (i=1; i<j; i++)
    1606     6145881 :           if (signe(gel(Lj,i))) gel(Lk,i) = subii(gel(Lk,i), gel(Lj,i));
    1607             :       }
    1608             :     }
    1609             :     else
    1610             :     {
    1611    25434066 :       for (i=1; i<j; i++)
    1612    17297707 :         if (signe(gel(Lj,i))) gel(Lk,i) = addmulii(gel(Lk,i), q, gel(Lj,i));
    1613             :     }
    1614             :   }
    1615             : }
    1616             : 
    1617             : static void
    1618     8188379 : hnfswap(GEN A, GEN B, long k, GEN lambda, GEN D)
    1619             : {
    1620     8188379 :   GEN t, p1, p2, Lk = gel(lambda,k);
    1621     8188379 :   long i,j,n = lg(A);
    1622             : 
    1623     8188379 :   swap(gel(A,k), gel(A,k-1));
    1624     8188379 :   if (B) swap(gel(B,k), gel(B,k-1));
    1625    34505217 :   for (j=k-2; j; j--) swap(gcoeff(lambda,j,k-1), gel(Lk,j));
    1626    77537065 :   for (i=k+1; i<n; i++)
    1627             :   {
    1628    69348552 :     GEN Li = gel(lambda,i);
    1629    69348552 :     p1 = mulii(gel(Li,k-1), gel(D,k));
    1630    69278802 :     p2 = mulii(gel(Li,k), gel(Lk,k-1));
    1631    69302236 :     t = subii(p1,p2);
    1632             : 
    1633    69313938 :     p1 = mulii(gel(Li,k), gel(D,k-2));
    1634    69296232 :     p2 = mulii(gel(Li,k-1), gel(Lk,k-1));
    1635    69295995 :     gel(Li,k-1) = diviiexact(addii(p1,p2), gel(D,k-1));
    1636    69303226 :     gel(Li,k) = diviiexact(t, gel(D,k-1));
    1637             :   }
    1638     8188513 :   p1 = mulii(gel(D,k-2), gel(D,k));
    1639     8164138 :   p2 = sqri(gel(Lk,k-1));
    1640     8165767 :   gel(D,k-1) = diviiexact(addii(p1,p2), gel(D,k-1));
    1641     8164314 : }
    1642             : 
    1643             : /* reverse row order in matrix A, IN PLACE */
    1644             : static GEN
    1645      439917 : reverse_rows(GEN A)
    1646             : {
    1647      439917 :   long i, j, h, n = lg(A);
    1648      439917 :   if (n == 1) return A;
    1649      439917 :   h = lgcols(A);
    1650     3784391 :   for (j=1; j<n; j++)
    1651             :   {
    1652     3344476 :     GEN c = gel(A,j);
    1653             :     /* start at (h-1) >>1 : if h = 2i even, no need to swap c[i] and itself */
    1654     9735325 :     for (i=(h-1)>>1; i; i--) swap(gel(c,i), gel(c,h-i));
    1655             :   }
    1656      439915 :   return A;
    1657             : }
    1658             : /* decide whether to swap */
    1659             : static int
    1660     4312628 : must_swap(long k, GEN lambda, GEN D)
    1661             : {
    1662     4312628 :   pari_sp av = avma;
    1663     4312628 :   GEN z = addii(mulii(gel(D,k-2),gel(D,k)), sqri(gcoeff(lambda,k-1,k)));
    1664     4306979 :   return gc_bool(av, cmpii(z, sqri(gel(D,k-1))) < 0);
    1665             : }
    1666             : 
    1667             : GEN
    1668      219960 : ZM_hnflll(GEN A, GEN *ptB, int remove)
    1669             : {
    1670      219960 :   pari_sp av = avma;
    1671             :   long n, k, kmax;
    1672             :   GEN B, lambda, D;
    1673             : 
    1674      219960 :   n = lg(A);
    1675      219960 :   A = reverse_rows(ZM_copy(A)); /* ZM_copy for in place findi_normalize() */
    1676      219961 :   B = ptB? matid(n-1): NULL;
    1677      219961 :   D = const_vec(n, gen_1) + 1;
    1678      219961 :   lambda = zeromatcopy(n-1,n-1);
    1679      194936 :   k = kmax = 2;
    1680    15046536 :   while (k < n)
    1681             :   {
    1682             :     long row0, row1;
    1683             :     int do_swap;
    1684    14826579 :     reduce2(A,B,k,k-1,&row0,&row1,lambda,D);
    1685    14839764 :     if      (row0) do_swap = (!row1 || row0 <= row1);
    1686     5106891 :     else if (row1) do_swap = 0;
    1687     4047583 :     else do_swap = must_swap(k,lambda,D);
    1688    14876874 :     if (do_swap)
    1689             :     {
    1690     7942287 :       hnfswap(A,B,k,lambda,D);
    1691     7925184 :       if (k > 2) k--;
    1692             :     }
    1693             :     else
    1694             :     {
    1695             :       long i;
    1696    33213726 :       for (i=k-2; i; i--)
    1697             :       {
    1698             :         long row0, row1;
    1699    26287310 :         reduce2(A,B,k,i,&row0,&row1,lambda,D);
    1700             :       }
    1701     6926416 :       if (++k > kmax) kmax = k;
    1702             :     }
    1703    14851600 :     if (gc_needed(av,3))
    1704             :     {
    1705          69 :       GEN b = D-1;
    1706          69 :       if (DEBUGMEM>1) pari_warn(warnmem,"hnflll, kmax = %ld / %ld",kmax,n-1);
    1707          69 :       gerepileall(av, B? 4: 3, &A, &lambda, &b, &B);
    1708          69 :       if (gc_needed(av,1)) paristack_resize(0); /* avoid desperation GC */
    1709          69 :       D = b+1;
    1710             :     }
    1711             :   }
    1712             :   /* handle trivial case: return negative diag coefficient otherwise */
    1713      219957 :   if (n == 2) (void)findi_normalize(gel(A,1), B,1,lambda);
    1714      219957 :   A = reverse_rows(A);
    1715      219961 :   if (remove)
    1716             :   {
    1717             :     long i;
    1718        7322 :     for (i = 1; i < n; i++)
    1719        7098 :       if (!ZV_equal0(gel(A,i))) break;
    1720        2093 :     remove_0cols(i-1, &A, &B, remove);
    1721             :   }
    1722      219961 :   gerepileall(av, B? 2: 1, &A, &B);
    1723      219960 :   if (B) *ptB = B;
    1724      219960 :   return A;
    1725             : }
    1726             : 
    1727             : GEN
    1728           7 : hnflll(GEN x)
    1729             : {
    1730           7 :   GEN z = cgetg(3, t_VEC);
    1731           7 :   gel(z,1) = ZM_hnflll(x, &gel(z,2), 1);
    1732           7 :   return z;
    1733             : }
    1734             : 
    1735             : /* Variation on HNFLLL: Extended GCD */
    1736             : 
    1737             : static void
    1738      948806 : reduce1(GEN A, GEN B, long k, long j, GEN lambda, GEN D)
    1739             : {
    1740             :   GEN q;
    1741             :   long i;
    1742             : 
    1743      948806 :   if (signe(gel(A,j)))
    1744      171106 :     q = diviiround(gel(A,k),gel(A,j));
    1745      777700 :   else if (abscmpii(shifti(gcoeff(lambda,j,k), 1), gel(D,j)) > 0)
    1746       96075 :     q = diviiround(gcoeff(lambda,j,k), gel(D,j));
    1747             :   else
    1748      681625 :     return;
    1749             : 
    1750      267181 :   if (signe(q))
    1751             :   {
    1752      230192 :     GEN Lk = gel(lambda,k), Lj = gel(lambda,j);
    1753      230192 :     togglesign_safe(&q);
    1754      230192 :     gel(A,k) = addmulii(gel(A,k), q, gel(A,j));
    1755      230192 :     ZC_lincomb1_inplace(gel(B,k),gel(B,j),q);
    1756      230192 :     gel(Lk,j) = addmulii(gel(Lk,j), q, gel(D,j));
    1757      420108 :     for (i=1; i<j; i++)
    1758      189916 :       if (signe(gel(Lj,i))) gel(Lk,i) = addmulii(gel(Lk,i), q, gel(Lj,i));
    1759             :   }
    1760             : }
    1761             : 
    1762             : static GEN
    1763      107422 : ZV_gcdext_i(GEN A)
    1764             : {
    1765      107422 :   long k, n = lg(A);
    1766             :   GEN B, lambda, D;
    1767             : 
    1768      107422 :   if (n == 1) retmkvec2(gen_1, cgetg(1,t_MAT));
    1769      107422 :   A = leafcopy(A);
    1770      107422 :   B = matid(n-1);
    1771      107422 :   lambda = zeromatcopy(n-1,n-1);
    1772      107422 :   D = const_vec(n, gen_1) + 1;
    1773      107422 :   k = 2;
    1774      693938 :   while (k < n)
    1775             :   {
    1776             :     int do_swap;
    1777             : 
    1778      586516 :     reduce1(A,B,k,k-1,lambda,D);
    1779      586516 :     if    (signe(gel(A,k-1))) do_swap = 1;
    1780      415410 :     else if (signe(gel(A,k))) do_swap = 0;
    1781      224136 :     else do_swap = must_swap(k,lambda,D);
    1782      586516 :     if (do_swap)
    1783             :     {
    1784      239703 :       hnfswap(A,B,k,lambda,D);
    1785      239703 :       if (k > 2) k--;
    1786             :     }
    1787             :     else
    1788             :     {
    1789             :       long i;
    1790      709103 :       for (i=k-2; i; i--) reduce1(A,B,k,i,lambda,D);
    1791      346813 :       k++;
    1792             :     }
    1793             :   }
    1794      107422 :   if (signe(gel(A,n-1)) < 0)
    1795             :   {
    1796       13045 :     gel(A,n-1) = negi(gel(A,n-1));
    1797       13045 :     ZV_togglesign(gel(B,n-1));
    1798             :   }
    1799      107422 :   return mkvec2(gel(A,n-1), B);
    1800             : }
    1801             : GEN
    1802      107408 : ZV_extgcd(GEN A)
    1803             : {
    1804      107408 :   pari_sp av = avma;
    1805      107408 :   return gerepilecopy(av, ZV_gcdext_i(A));
    1806             : }
    1807             : /* as ZV_extgcd, transforming the gcd into a t_MAT, for mathnf0 */
    1808             : static GEN
    1809          21 : ZV_hnfgcdext(GEN A)
    1810             : {
    1811          21 :   pari_sp av = avma;
    1812             :   GEN z;
    1813          21 :   if (lg(A) == 1) retmkvec2(cgetg(1,t_MAT),cgetg(1,t_MAT));
    1814          14 :   z = ZV_gcdext_i(A);
    1815          14 :   gel(z,1) = mkmat(mkcol(gel(z,1)));
    1816          14 :   return gerepilecopy(av, z);
    1817             : }
    1818             : 
    1819             : /* HNF with permutation. */
    1820             : GEN
    1821         385 : ZM_hnfperm(GEN A, GEN *ptU, GEN *ptperm)
    1822             : {
    1823             :   GEN U, c, l, perm, d, p, q, b;
    1824         385 :   pari_sp av = avma, av1;
    1825             :   long r, t, i, j, j1, k, m, n;
    1826             : 
    1827         385 :   n = lg(A)-1;
    1828         385 :   if (!n)
    1829             :   {
    1830           7 :     if (ptU) *ptU = cgetg(1,t_MAT);
    1831           7 :     if (ptperm) *ptperm = cgetg(1,t_VEC);
    1832           7 :     return cgetg(1, t_MAT);
    1833             :   }
    1834         378 :   m = nbrows(A);
    1835         378 :   c = zero_zv(m);
    1836         378 :   l = zero_zv(n);
    1837         378 :   perm = cgetg(m+1, t_VECSMALL);
    1838         378 :   av1 = avma;
    1839         378 :   A = RgM_shallowcopy(A);
    1840         378 :   U = ptU? matid(n): NULL;
    1841             :   /* U base change matrix : A0*U = A all along */
    1842        1722 :   for (r=0, k=1; k <= n; k++)
    1843             :   {
    1844        3976 :     for (j=1; j<k; j++)
    1845             :     {
    1846        2632 :       if (!l[j]) continue;
    1847        2478 :       t=l[j]; b=gcoeff(A,t,k);
    1848        2478 :       if (!signe(b)) continue;
    1849             : 
    1850         546 :       ZC_elem(b,gcoeff(A,t,j), A,U,k,j);
    1851         546 :       d = gcoeff(A,t,j);
    1852         546 :       if (signe(d) < 0)
    1853             :       {
    1854           0 :         ZV_neg_inplace(gel(A,j));
    1855           0 :         if (U) ZV_togglesign(gel(U,j));
    1856           0 :         d = gcoeff(A,t,j);
    1857             :       }
    1858        1393 :       for (j1=1; j1<j; j1++)
    1859             :       {
    1860         847 :         if (!l[j1]) continue;
    1861         826 :         q = truedivii(gcoeff(A,t,j1),d);
    1862         826 :         if (!signe(q)) continue;
    1863             : 
    1864         329 :         togglesign(q);
    1865         329 :         ZC_lincomb1_inplace(gel(A,j1), gel(A,j), q);
    1866         329 :         if (U) ZC_lincomb1_inplace(gel(U,j1), gel(U,j), q);
    1867             :       }
    1868             :     }
    1869        4585 :     t = m; while (t && (c[t] || !signe(gcoeff(A,t,k)))) t--;
    1870        1344 :     if (t)
    1871             :     {
    1872        1225 :       p = gcoeff(A,t,k);
    1873       19971 :       for (i=t-1; i; i--)
    1874             :       {
    1875       18746 :         q = gcoeff(A,i,k);
    1876       18746 :         if (signe(q) && abscmpii(p,q) > 0) { p = q; t = i; }
    1877             :       }
    1878        1225 :       perm[++r] = l[k] = t; c[t] = k;
    1879        1225 :       if (signe(p) < 0)
    1880             :       {
    1881         141 :         ZV_neg_inplace(gel(A,k));
    1882         141 :         if (U) ZV_togglesign(gel(U,k));
    1883         141 :         p = gcoeff(A,t,k);
    1884             :       }
    1885             :       /* p > 0 */
    1886        3493 :       for (j=1; j<k; j++)
    1887             :       {
    1888        2268 :         if (!l[j]) continue;
    1889        2233 :         q = truedivii(gcoeff(A,t,j),p);
    1890        2233 :         if (!signe(q)) continue;
    1891             : 
    1892         238 :         togglesign(q);
    1893         238 :         ZC_lincomb1_inplace(gel(A,j), gel(A,k), q);
    1894         238 :         if (U) ZC_lincomb1_inplace(gel(U,j), gel(U,k), q);
    1895             :       }
    1896             :     }
    1897        1344 :     if (gc_needed(av1,1))
    1898             :     {
    1899           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"hnfperm, k=%ld",k);
    1900           0 :       gerepileall(av1, U? 2: 1, &A, &U);
    1901             :     }
    1902             :   }
    1903         378 :   if (r < m)
    1904             :   {
    1905        5131 :     for (i=1,k=r; i<=m; i++)
    1906        4816 :       if (!c[i]) perm[++k] = i;
    1907             :   }
    1908             : 
    1909             : /* We have A0*U=A, U in Gl(n,Z)
    1910             :  * basis for Im(A):  columns of A s.t l[j]>0 (r   cols)
    1911             :  * basis for Ker(A): columns of U s.t l[j]=0 (n-r cols) */
    1912         378 :   p = cgetg(r+1,t_MAT);
    1913        2814 :   for (i=1; i<=m/2; i++) lswap(perm[i], perm[m+1-i]);
    1914         378 :   if (U)
    1915             :   {
    1916          84 :     GEN u = cgetg(n+1,t_MAT);
    1917         378 :     for (t=1,k=r,j=1; j<=n; j++)
    1918         294 :       if (l[j])
    1919             :       {
    1920         182 :         u[k + n-r] = U[j];
    1921         182 :         gel(p,k--) = vecpermute(gel(A,j), perm);
    1922             :       }
    1923             :       else
    1924         112 :         u[t++] = U[j];
    1925          84 :     *ptU = u;
    1926          84 :     if (ptperm) *ptperm = perm;
    1927          84 :     gerepileall(av, ptperm? 3: 2, &p, ptU, ptperm);
    1928             :   }
    1929             :   else
    1930             :   {
    1931        1344 :     for (k=r,j=1; j<=n; j++)
    1932        1050 :       if (l[j]) gel(p,k--) = vecpermute(gel(A,j), perm);
    1933         294 :     if (ptperm) *ptperm = perm;
    1934         294 :     gerepileall(av, ptperm? 2: 1, &p, ptperm);
    1935             :   }
    1936         378 :   return p;
    1937             : }
    1938             : 
    1939             : GEN
    1940         238 : ZM_hnf_knapsack(GEN x)
    1941             : {
    1942         238 :   GEN t, perm, H = ZM_hnfperm(x,NULL,&perm);
    1943         238 :   long i,j, l = lg(H), h = lgcols(H);
    1944        3213 :   for (i=1; i<h; i++)
    1945             :   {
    1946        3066 :     int fl = 0;
    1947       16688 :     for (j=1; j<l; j++)
    1948             :     {
    1949       13713 :       t = gcoeff(H,i,j);
    1950       13713 :       if (signe(t))
    1951             :       {
    1952        3101 :         if (!is_pm1(t) || fl) return NULL;
    1953        3010 :         fl = 1;
    1954             :       }
    1955             :     }
    1956             :   }
    1957         147 :   return rowpermute(H, perm_inv(perm));
    1958             : }
    1959             : 
    1960             : GEN
    1961          14 : hnfperm(GEN A)
    1962             : {
    1963          14 :   GEN y = cgetg(4, t_VEC);
    1964          14 :   gel(y,1) = ZM_hnfperm(A, &gel(y,2), &gel(y,3));
    1965          14 :   return y;
    1966             : }
    1967             : 
    1968             : /* Hermite Normal Form, with base change matrix if ptB != NULL.
    1969             :  * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
    1970             :  * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
    1971             : GEN
    1972      714905 : ZM_hnfall_i(GEN A, GEN *ptB, long remove)
    1973             : {
    1974             :   pari_sp av;
    1975             :   long m, n, r, i, j, k, li;
    1976             :   GEN B, c, h, a;
    1977             : 
    1978      714905 :   RgM_dimensions(A, &m,&n);
    1979      714905 :   if (!n)
    1980             :   {
    1981           7 :     if (ptB) *ptB = cgetg(1,t_MAT);
    1982           7 :     return cgetg(1,t_MAT);
    1983             :   }
    1984      714898 :   c = zero_zv(m);
    1985      714888 :   h = const_vecsmall(n, m);
    1986      714885 :   av = avma;
    1987      714885 :   A = RgM_shallowcopy(A);
    1988      714901 :   B = ptB? matid(n): NULL;
    1989      714902 :   r = n+1;
    1990     2011372 :   for (li=m; li; li--)
    1991             :   {
    1992     2913949 :     for (j=1; j<r; j++)
    1993             :     {
    1994     3546256 :       for (i=h[j]; i>li; i--)
    1995             :       {
    1996      632814 :         a = gcoeff(A,i,j);
    1997      632814 :         k = c[i];
    1998             :         /* zero a = Aij  using  Aik */
    1999      632814 :         if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B,j,k);
    2000      632738 :         ZM_reduce(A,B, i,k); /* ensure reduced entries even if a = 0 */
    2001             :       }
    2002     2913442 :       if (gc_needed(av,1) && (j & 0x7f) == 0)
    2003             :       {
    2004           0 :         if (DEBUGMEM>1)
    2005           0 :           pari_warn(warnmem,"ZM_hnfall[1], li = %ld, j = %ld", li, j);
    2006           0 :         gerepileall(av, B? 2: 1, &A, &B);
    2007             :       }
    2008     2913468 :       if (signe( gcoeff(A,li,j) )) break;
    2009     1617448 :       h[j] = li-1;
    2010             :     }
    2011     1296473 :     if (j == r) continue;
    2012     1296073 :     r--;
    2013     1296073 :     if (j < r) /* A[j] != 0 */
    2014             :     {
    2015      839724 :       swap(gel(A,j), gel(A,r));
    2016      839724 :       if (B) swap(gel(B,j), gel(B,r));
    2017      839724 :       h[j] = h[r]; h[r] = li; c[li] = r;
    2018             :     }
    2019     1296073 :     if (signe(gcoeff(A,li,r)) < 0)
    2020             :     {
    2021      297697 :       ZV_neg_inplace(gel(A,r));
    2022      297712 :       if (B) ZV_togglesign(gel(B,r));
    2023             :     }
    2024     1296088 :     ZM_reduce(A,B, li,r);
    2025     1296069 :     if (gc_needed(av,1))
    2026             :     {
    2027           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[2], li = %ld", li);
    2028           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2029             :     }
    2030             :   }
    2031             : 
    2032      714871 :   if (DEBUGLEVEL>5) err_printf("\nhnfall, final phase: ");
    2033      714887 :   r--; /* first r cols are in the image the n-r (independent) last ones */
    2034     2495419 :   for (j=1; j<=r; j++)
    2035             :   {
    2036     4097901 :     for (i=h[j]; i; i--)
    2037             :     {
    2038     2317375 :       a = gcoeff(A,i,j);
    2039     2317375 :       k = c[i];
    2040     2317375 :       if (signe(a)) ZC_elem(a,gcoeff(A,i,k), A,B, j,k);
    2041     2317349 :       ZM_reduce(A,B, i,k); /* ensure reduced entries, even if a = 0 */
    2042             :     }
    2043     1780526 :     if (gc_needed(av,1) && (j & 0x7f) == 0)
    2044             :     {
    2045           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_hnfall[3], j = %ld", j);
    2046           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2047             :     }
    2048             :   }
    2049      714878 :   if (DEBUGLEVEL>5) err_printf("\n");
    2050      714878 :   if (remove) remove_0cols(r, &A, &B, remove);
    2051      714889 :   if (ptB) *ptB = B;
    2052      714889 :   return A;
    2053             : }
    2054             : GEN
    2055       22037 : ZM_hnfall(GEN A, GEN *ptB, long remove)
    2056             : {
    2057       22037 :   pari_sp av = avma;
    2058       22037 :   A = ZM_hnfall_i(A, ptB, remove);
    2059       22037 :   return gc_all(av, ptB? 2: 1, &A, ptB);
    2060             : }
    2061             : 
    2062             : GEN
    2063          14 : hnfall(GEN x)
    2064             : {
    2065          14 :   GEN z = cgetg(3, t_VEC);
    2066          14 :   gel(z,1) = ZM_hnfall(x, (GEN*)(z+2), 1);
    2067          14 :   return z;
    2068             : }
    2069             : GEN
    2070          14 : hnf(GEN x) { return mathnf0(x,0); }
    2071             : 
    2072             : /* C = A^(-1)t where A and C are integral, A is upper triangular, t t_INT */
    2073             : GEN
    2074      236428 : hnf_invscale(GEN A, GEN t)
    2075             : {
    2076      236428 :   long n = lg(A)-1, i,j,k;
    2077      236428 :   GEN m, c = cgetg(n+1,t_MAT);
    2078             : 
    2079      236429 :   if (!n) return c;
    2080      876965 :   for (k=1; k<=n; k++)
    2081             :   { /* cf hnf_divscale with B = id, thus b = e_k */
    2082      640562 :     GEN u = cgetg(n+1, t_COL);
    2083      640564 :     pari_sp av = avma;
    2084      640564 :     gel(c,k) = u;
    2085      640564 :     gel(u,n) = k == n? gerepileuptoint(av, diviiexact(t, gcoeff(A,n,n))): gen_0;
    2086     2167020 :     for (i=n-1; i>0; i--)
    2087             :     {
    2088     1526484 :       av = avma; m = i == k? t: gen_0;
    2089     5776851 :       for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2090     1526164 :       gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
    2091             :     }
    2092             :   }
    2093      236403 :   return c;
    2094             : }
    2095             : 
    2096             : /* C = A^(-1)(tB) where A, B, C are integral, A is upper triangular, t t_INT */
    2097             : GEN
    2098      195168 : hnf_divscale(GEN A, GEN B, GEN t)
    2099             : {
    2100      195168 :   long n = lg(A)-1, i,j,k;
    2101      195168 :   GEN m, c = cgetg(n+1,t_MAT);
    2102             : 
    2103      195168 :   if (!n) return c;
    2104      874351 :   for (k=1; k<=n; k++)
    2105             :   {
    2106      679185 :     GEN u = cgetg(n+1, t_COL), b = gel(B,k);
    2107      679185 :     pari_sp av = avma;
    2108      679185 :     gel(c,k) = u; m = mulii(gel(b,n),t);
    2109      679168 :     gel(u,n) = gerepileuptoint(av, diviiexact(m, gcoeff(A,n,n)));
    2110     3960737 :     for (i=n-1; i>0; i--)
    2111             :     {
    2112     3281554 :       av = avma; m = mulii(gel(b,i),t);
    2113    30560880 :       for (j=i+1; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2114     3281423 :       gel(u,i) = gerepileuptoint(av, diviiexact(m, gcoeff(A,i,i)));
    2115             :     }
    2116             :   }
    2117      195166 :   return c;
    2118             : }
    2119             : 
    2120             : /* A, B integral upper HNF. A^(-1) B integral ? */
    2121             : int
    2122         133 : hnfdivide(GEN A, GEN B)
    2123             : {
    2124         133 :   pari_sp av = avma;
    2125         133 :   long n = lg(A)-1, i,j,k;
    2126             :   GEN u, b, m, r;
    2127             : 
    2128         133 :   if (!n) return 1;
    2129         133 :   if (lg(B)-1 != n) pari_err_DIM("hnfdivide");
    2130         133 :   u = cgetg(n+1, t_COL);
    2131         483 :   for (k=1; k<=n; k++)
    2132             :   {
    2133         364 :     b = gel(B,k);
    2134         364 :     m = gel(b,k);
    2135         364 :     gel(u,k) = dvmdii(m, gcoeff(A,k,k), &r);
    2136         364 :     if (r != gen_0) return gc_long(av, 0);
    2137         826 :     for (i=k-1; i>0; i--)
    2138             :     {
    2139         476 :       m = gel(b,i);
    2140        1337 :       for (j=i+1; j<=k; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2141         476 :       m = dvmdii(m, gcoeff(A,i,i), &r);
    2142         476 :       if (r != gen_0) return gc_long(av, 0);
    2143         476 :       gel(u,i) = m;
    2144             :     }
    2145             :   }
    2146         119 :   return gc_long(av, 1);
    2147             : }
    2148             : 
    2149             : /* A upper HNF, b integral vector. Return A^(-1) b if integral,
    2150             :  * NULL otherwise. Assume #A[,1] = #b. */
    2151             : GEN
    2152      365176 : hnf_invimage(GEN A, GEN b)
    2153             : {
    2154      365176 :   pari_sp av = avma;
    2155      365176 :   long n = lg(A)-1, m, i, k;
    2156             :   GEN u, r;
    2157             : 
    2158      365176 :   if (!n) return lg(b)==1? cgetg(1,t_COL):NULL;
    2159      365134 :   m = nbrows(A); /* m >= n */
    2160      365137 :   u = cgetg(n+1, t_COL);
    2161      826269 :   for (i = n, k = m; k > 0; k--)
    2162             :   {
    2163      826270 :     pari_sp av2 = avma;
    2164             :     long j;
    2165      826270 :     GEN t = gel(b,k), Aki = gcoeff(A,k,i);
    2166      826270 :     if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
    2167     1982278 :     for (j=i+1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
    2168      826194 :     if (!signe(Aki))
    2169             :     {
    2170           0 :       if (signe(t)) return gc_NULL(av);
    2171           0 :       set_avma(av2); gel(u,i) = gen_0; continue;
    2172             :     }
    2173      826194 :     t = dvmdii(t, Aki, &r);
    2174      826115 :     if (r != gen_0) return gc_NULL(av);
    2175      603723 :     gel(u,i) = gerepileuptoint(av2, t);
    2176      603861 :     if (--i == 0) break;
    2177             :   }
    2178             :   /* If there is a solution, it must be u. Check remaining equations */
    2179      285444 :   for (; k > 0; k--)
    2180             :   {
    2181      142724 :     pari_sp av2 = avma;
    2182             :     long j;
    2183      142724 :     GEN t = gel(b,k);
    2184      142724 :     if (typ(t) != t_INT) pari_err_TYPE("hnf_invimage",t);
    2185      544699 :     for (j=1; j<=n; j++) t = subii(t, mulii(gcoeff(A,k,j),gel(u,j)));
    2186      142721 :     if (signe(t)) return gc_NULL(av);
    2187      142721 :     set_avma(av2);
    2188             :   }
    2189      142720 :   return u;
    2190             : }
    2191             : 
    2192             : /* A upper HNF, B integral matrix or column. Return A^(-1) B if integral,
    2193             :  * NULL otherwise */
    2194             : GEN
    2195      313403 : hnf_solve(GEN A, GEN B)
    2196             : {
    2197             :   pari_sp av;
    2198             :   long i, l;
    2199             :   GEN C;
    2200             : 
    2201      313403 :   if (typ(B) == t_COL) return hnf_invimage(A, B);
    2202      251420 :   av = avma; C = cgetg_copy(B, &l);
    2203      350136 :   for (i = 1; i < l; i++)
    2204             :   {
    2205      265628 :     GEN c = hnf_invimage(A, gel(B,i));
    2206      265601 :     if (!c) return gc_NULL(av);
    2207       98697 :     gel(C,i) = c;
    2208             :   }
    2209       84508 :   return C;
    2210             : }
    2211             : 
    2212             : /***************************************************************/
    2213             : /**                                                           **/
    2214             : /**               SMITH NORMAL FORM REDUCTION                 **/
    2215             : /**                                                           **/
    2216             : /***************************************************************/
    2217             : 
    2218             : static GEN
    2219           0 : trivsmith(long all)
    2220             : {
    2221             :   GEN z;
    2222           0 :   if (!all) return cgetg(1,t_VEC);
    2223           0 :   z=cgetg(4,t_VEC);
    2224           0 :   gel(z,1) = cgetg(1,t_MAT);
    2225           0 :   gel(z,2) = cgetg(1,t_MAT);
    2226           0 :   gel(z,3) = cgetg(1,t_MAT); return z;
    2227             : }
    2228             : 
    2229             : static void
    2230          38 : snf_pile1(pari_sp av, GEN *x, GEN *U)
    2231             : {
    2232             :   GEN *gptr[2];
    2233          38 :   int c = 1; gptr[0]=x;
    2234          38 :   if (*U) gptr[c++] = U;
    2235          38 :   gerepilemany(av,gptr,c);
    2236          38 : }
    2237             : static void
    2238      938301 : snf_pile(pari_sp av, GEN *x, GEN *U, GEN *V)
    2239             : {
    2240             :   GEN *gptr[3];
    2241      938301 :   int c = 1; gptr[0]=x;
    2242      938301 :   if (*U) gptr[c++] = U;
    2243      938301 :   if (*V) gptr[c++] = V;
    2244      938301 :   gerepilemany(av,gptr,c);
    2245      938370 : }
    2246             : 
    2247             : static GEN
    2248      681127 : bezout_step(GEN *pa, GEN *pb, GEN *pu, GEN *pv)
    2249             : {
    2250      681127 :   GEN a = *pa, b = *pb, d;
    2251      681127 :   if (absequalii(a,b))
    2252             :   {
    2253      399554 :     long sa = signe(a), sb = signe(b);
    2254      399554 :     *pv = gen_0;
    2255      399554 :     if (sb == sa) {
    2256      391365 :       *pa = *pb = gen_1;
    2257      391365 :       if (sa > 0) {*pu=gen_1; return a;} else {*pu=gen_m1; return absi(a);}
    2258             :     }
    2259        8189 :     if (sa > 0) { *pa = *pu = gen_1; *pb = gen_m1; return a; }
    2260        1064 :     *pa = *pu = gen_m1; *pb = gen_1; return b;
    2261             :   }
    2262      281577 :   d = bezout(a,b, pu,pv);
    2263      281584 :   *pa = diviiexact(a, d);
    2264      281571 :   *pb = diviiexact(b, d); return d;
    2265             : }
    2266             : 
    2267             : static int
    2268      500282 : negcmpii(void *E, GEN x, GEN y) { (void)E; return -cmpii(x,y); }
    2269             : 
    2270             : /* x square of maximal rank; does b = x[i,i] divide all entries in
    2271             :  * x[1..i-1, 1..i-1] ? If so, return 0; else the index of a problematic row */
    2272             : static long
    2273     1044299 : ZM_snf_no_divide(GEN x, long i)
    2274             : {
    2275     1044299 :   GEN b = gcoeff(x,i,i);
    2276             :   long j, k;
    2277             : 
    2278     1044299 :   if (is_pm1(b)) return 0;
    2279      899554 :   for (k = 1; k < i; k++)
    2280     2247983 :     for (j = 1; j < i; j++)
    2281     1776852 :       if (!dvdii(gcoeff(x,k,j),b)) return k;
    2282      280187 :   return 0;
    2283             : }
    2284             : 
    2285             : static void
    2286     1345676 : ZM_redpart(GEN x, GEN p, long I)
    2287             : {
    2288     1345676 :   long l = lgefint(p), i, j;
    2289     5774789 :   for (i = 1; i <= I; i++)
    2290    26857399 :     for (j = 1; j <= I; j++)
    2291             :     {
    2292    22428286 :       GEN c = gcoeff(x,i,j);
    2293    22428286 :       if (lgefint(c) > l) gcoeff(x,i,j) = remii(c, p);
    2294             :     }
    2295     1345676 : }
    2296             : static void
    2297      841301 : ZMrow_divexact_inplace(GEN M, long i, GEN c)
    2298             : {
    2299      841301 :   long j, l = lg(M);
    2300     3349885 :   for (j = 1; j < l; j++) gcoeff(M,i,j) = diviiexact(gcoeff(M,i,j), c);
    2301      841263 : }
    2302             : 
    2303             : /* Return the SNF D of matrix X. If ptU/ptV non-NULL set them to U/V
    2304             :  * to that D = UXV */
    2305             : GEN
    2306      693524 : ZM_snfall_i(GEN x, GEN *ptU, GEN *ptV, long flag)
    2307             : {
    2308      693524 :   pari_sp av0 = avma, av;
    2309      693524 :   const long return_vec = flag & 1;
    2310             :   long i, j, k, m0, m, n0, n;
    2311      693524 :   GEN u, v, U, V, V0, mdet, A = NULL, perm = NULL;
    2312             : 
    2313      693524 :   n0 = n = lg(x)-1;
    2314      693524 :   if (!n) {
    2315       40398 :     if (ptU) *ptU = cgetg(1,t_MAT);
    2316       40398 :     if (ptV) *ptV = cgetg(1,t_MAT);
    2317       40398 :     return cgetg(1, return_vec? t_VEC: t_MAT);
    2318             :   }
    2319      653126 :   m0 = m = nbrows(x);
    2320             : 
    2321      653132 :   U = V = V0 = NULL; /* U = TRANSPOSE of row transform matrix [act on columns]*/
    2322      653132 :   if (m == n && ZM_ishnf(x))
    2323             :   {
    2324      593089 :     mdet = ZM_det_triangular(x); /* != 0 */
    2325      593026 :     if (ptV) *ptV = matid(n);
    2326             :   }
    2327             :   else
    2328             :   {
    2329       60063 :     mdet = ZM_detmult(x);
    2330       60081 :     if (!signe(mdet))
    2331          77 :       x = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
    2332             :     else
    2333             :     { /* m <= n */
    2334       60004 :       if (!ptV)
    2335         357 :         x = ZM_hnfmod(x,mdet);
    2336       59647 :       else if (m == n)
    2337             :       {
    2338       59619 :         GEN H = ZM_hnfmod(x,mdet);
    2339       59619 :         *ptV = ZM_gauss(x,H);
    2340       59619 :         x = H;
    2341             :       }
    2342             :       else
    2343          28 :         x = ZM_hnfperm(x, ptV, ptU? &perm: NULL);
    2344       60004 :       mdet = ZM_det_triangular(x); /* > 0 */
    2345             :     }
    2346       60081 :     n = lg(x)-1; /* n independent columns */
    2347       60081 :     if (ptV)
    2348             :     {
    2349       59668 :       V = *ptV;
    2350       59668 :       if (n != n0)
    2351             :       {
    2352          28 :         V0 = vecslice(V, 1, n0 - n); /* kernel */
    2353          28 :         V  = vecslice(V, n0-n+1, n0);
    2354             :       }
    2355             :     }
    2356       60081 :     if (!signe(mdet))
    2357             :     {
    2358          77 :       if (n)
    2359             :       {
    2360          70 :         x = ZM_snfall_i(shallowtrans(x), ptV, ptU, return_vec); /* swap V,U */
    2361          70 :         if (!return_vec && n != m) x = shallowtrans(x);
    2362          70 :         if (ptV) V = ZM_mul(V, shallowtrans(*ptV));
    2363          70 :         if (ptU) U = *ptU; /* TRANSPOSE */
    2364             :       }
    2365             :       else /* 0 matrix */
    2366             :       {
    2367           7 :         x = cgetg(1,t_MAT);
    2368           7 :         if (ptV) V = cgetg(1, t_MAT);
    2369           7 :         if (ptU) U = matid(m);
    2370             :       }
    2371          77 :       goto THEEND;
    2372             :     }
    2373             :   }
    2374      653068 :   if (ptV || ptU) U = matid(n); /* we will compute V in terms of U */
    2375      653076 :   if (DEBUGLEVEL>7) err_printf("starting SNF loop");
    2376             : 
    2377             :   /* square, maximal rank n */
    2378      653076 :   A = x; x = shallowcopy(x); av = avma;
    2379     1549169 :   for (i = n; i > 1; i--)
    2380             :   {
    2381      896101 :     if (DEBUGLEVEL>7) err_printf("\ni = %ld: ",i);
    2382             :     for(;;)
    2383      677726 :     {
    2384     1573793 :       int c = 0;
    2385             :       GEN a, b;
    2386     4551481 :       for (j = i-1; j >= 1; j--)
    2387             :       {
    2388     2977801 :         b = gcoeff(x,i,j); if (!signe(b)) continue;
    2389      260643 :         a = gcoeff(x,i,i);
    2390      260643 :         ZC_elem(b, a, x,NULL, j,i);
    2391      260630 :         if (gc_needed(av,1))
    2392             :         {
    2393           1 :           if (DEBUGMEM>1) pari_warn(warnmem,"[1]: ZM_snfall i = %ld", i);
    2394           1 :           snf_pile1(av, &x,&U);
    2395             :         }
    2396             :       }
    2397     1573680 :       if (DEBUGLEVEL>7) err_printf("; ");
    2398     4551412 :       for (j=i-1; j>=1; j--)
    2399             :       {
    2400             :         GEN d;
    2401     2977767 :         b = gcoeff(x,j,i); if (!signe(b)) continue;
    2402      681121 :         a = gcoeff(x,i,i);
    2403      681121 :         d = bezout_step(&a, &b, &u, &v);
    2404     4138553 :         for (k = 1; k < i; k++)
    2405             :         {
    2406     3457531 :           GEN t = addii(mulii(u,gcoeff(x,i,k)),mulii(v,gcoeff(x,j,k)));
    2407     3457637 :           gcoeff(x,j,k) = subii(mulii(a,gcoeff(x,j,k)),
    2408     3457639 :                                 mulii(b,gcoeff(x,i,k)));
    2409     3457461 :           gcoeff(x,i,k) = t;
    2410             :         }
    2411      681022 :         gcoeff(x,j,i) = gen_0;
    2412      681022 :         gcoeff(x,i,i) = d;
    2413      681022 :         if (U) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
    2414      681024 :         if (gc_needed(av,1))
    2415             :         {
    2416          37 :           if (DEBUGMEM>1) pari_warn(warnmem,"[2]: ZM_snfall, i = %ld", i);
    2417          37 :           snf_pile1(av, &x,&U);
    2418             :         }
    2419      681032 :         c = 1;
    2420             :       }
    2421     1573645 :       if (!c)
    2422             :       {
    2423     1044301 :         k = ZM_snf_no_divide(x, i);
    2424     1044242 :         if (!k) break;
    2425             : 
    2426             :         /* x[k,j] != 0 mod b */
    2427      568284 :         for (j = 1; j <= i; j++)
    2428      420149 :           gcoeff(x,i,j) = addii(gcoeff(x,i,j),gcoeff(x,k,j));
    2429      148135 :         if (U) gel(U,i) = gadd(gel(U,i),gel(U,k));
    2430             :       }
    2431      677479 :       ZM_redpart(x, mdet, i);
    2432      677696 :       if (U && (flag & 2)) ZM_redpart(U, mdet, n);
    2433      677724 :       if (gc_needed(av,1))
    2434             :       {
    2435           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"[3]: ZM_snfall");
    2436           0 :         snf_pile1(av, &x,&U);
    2437             :       }
    2438             :     }
    2439             :   }
    2440      653068 :   if (DEBUGLEVEL>7) err_printf("\n");
    2441     2201200 :   for (k = n; k; k--)
    2442             :   {
    2443     1548613 :     GEN d = gcdii(gcoeff(x,k,k), mdet);
    2444     1548261 :     gcoeff(x,k,k) = d;
    2445     1548261 :     if (!is_pm1(d)) mdet = diviiexact(mdet,d);
    2446             :   }
    2447      652587 : THEEND:
    2448      652664 :   if (U) U = shallowtrans(U);
    2449      652855 :   if (ptV && A)
    2450             :   { /* U A V = D => D^(-1) U A = V^(-1) */
    2451      648376 :     long l = lg(x);
    2452      648376 :     GEN W = ZM_mul(U, A);
    2453     1489486 :     for (i = 1; i < l; i++)
    2454             :     {
    2455     1300502 :       GEN c = gcoeff(x,i,i);
    2456     1300502 :       if (is_pm1(c)) break; /* only 1 from now on */
    2457      841083 :       ZMrow_divexact_inplace(W, i, c);
    2458             :     }
    2459      648464 :     if (flag & 2)
    2460             :     {
    2461      624280 :       W = FpM_red(W, gcoeff(x,1,1));
    2462      624271 :       W = matinvmod(W, gcoeff(x,1,1));
    2463             :     }
    2464             :     else
    2465       24184 :       W = ZM_inv(W, NULL);
    2466      648434 :     V = V? ZM_mul(V, W): W;
    2467             :   }
    2468      652907 :   if (return_vec)
    2469             :   {
    2470      629003 :     long l = lg(x)-1;
    2471      629003 :     if (typ(x) == t_MAT) x = RgM_diagonal_shallow(x);
    2472      628989 :     if (m0 > l) x = shallowconcat(zerovec(m0-l), x);
    2473             :   }
    2474             : 
    2475      652893 :   if (V0)
    2476             :   { /* add kernel */
    2477          28 :     if (!return_vec) x = shallowconcat(zeromat(m,n0-n), x);
    2478          28 :     if (ptV) V = shallowconcat(V0, V);
    2479             :   }
    2480      652893 :   if (perm && U) U = vecpermute(U, perm_inv(perm));
    2481      652893 :   snf_pile(av0, &x,&U,&V);
    2482      653195 :   if (ptU) *ptU = U;
    2483      653195 :   if (ptV) *ptV = V;
    2484      653195 :   return x;
    2485             : }
    2486             : GEN
    2487       63651 : ZM_snfall(GEN x, GEN *U, GEN *V) { return ZM_snfall_i(x, U, V, 0); }
    2488             : GEN
    2489        1638 : ZM_snf(GEN x) { return ZM_snfall_i(x, NULL,NULL, 1); }
    2490             : GEN
    2491         385 : smith(GEN x) { return ZM_snfall_i(x, NULL,NULL, 1); }
    2492             : GEN
    2493          35 : smithall(GEN x)
    2494             : {
    2495          35 :   GEN z = cgetg(4, t_VEC);
    2496          35 :   gel(z,3) = ZM_snfall_i(x, (GEN*)(z+1),(GEN*)(z+2), 0);
    2497          35 :   return z;
    2498             : }
    2499             : 
    2500             : GEN
    2501      213595 : ZV_snfclean(GEN d)
    2502             : {
    2503      213595 :   long c, l = lg(d);
    2504      225968 :   for (c = 1; c < l; c++) { GEN t = gel(d,c); if (is_pm1(t)) break; }
    2505      213595 :   return c == l? d: vec_shorten(d, c-1);
    2506             : }
    2507             : void
    2508      910484 : ZM_snfclean(GEN d, GEN u, GEN v)
    2509             : {
    2510      910484 :   long i, c, l = lg(d);
    2511             : 
    2512      910484 :   if (typ(d) == t_VEC)
    2513     2337864 :     for (c=1; c<l; c++) { GEN t = gel(d,c); if (is_pm1(t)) break; }
    2514             :   else
    2515             :   {
    2516           0 :     for (c=1; c<l; c++) { GEN t = gcoeff(d,c,c); if (is_pm1(t)) break; }
    2517           0 :     if (c < l) for (i = 1; i < c; i++) setlg(gel(d,i), c);
    2518             :   }
    2519      910479 :   setlg(d, c);
    2520     2851026 :   if (u) for (i=1; i<l; i++) setlg(gel(u,i), c);
    2521      910456 :   if (v) setlg(v, c);
    2522      910445 : }
    2523             : 
    2524             : /* Assume z was computed by [g]smithall(). Remove the 1s on the diagonal */
    2525             : GEN
    2526         693 : smithclean(GEN z)
    2527             : {
    2528             :   long i, j, h, l, c, d;
    2529             :   GEN U, V, y, D, t;
    2530             : 
    2531         693 :   if (typ(z) != t_VEC) pari_err_TYPE("smithclean",z);
    2532         693 :   l = lg(z); if (l == 1) return cgetg(1,t_VEC);
    2533         686 :   U = gel(z,1);
    2534         686 :   if (l != 4 || typ(U) != t_MAT)
    2535             :   { /* assume z = vector of elementary divisors */
    2536        1827 :     for (c=1; c<l; c++)
    2537        1519 :       if (gequal1(gel(z,c))) break;
    2538         665 :     return gcopy_lg(z, c);
    2539             :   }
    2540          21 :   V = gel(z,2);
    2541          21 :   D = gel(z,3);
    2542          21 :   l = lg(D);
    2543          21 :   if (l == 1) return gcopy(z);
    2544          21 :   h = lgcols(D);
    2545          21 :   if (h > l)
    2546             :   { /* D = vconcat(zero matrix, diagonal matrix) */
    2547          21 :     for (c=1+h-l, d=1; c<h; c++,d++)
    2548          21 :       if (gequal1(gcoeff(D,c,d))) break;
    2549             :   }
    2550           7 :   else if (h < l)
    2551             :   { /* D = concat(zero matrix, diagonal matrix) */
    2552           7 :     for (c=1, d=1+l-h; d<l; c++,d++)
    2553           7 :       if (gequal1(gcoeff(D,c,d))) break;
    2554             :   }
    2555             :   else
    2556             :   { /* D diagonal */
    2557           0 :     for (c=1; c<l; c++)
    2558           0 :       if (gequal1(gcoeff(D,c,c))) break;
    2559           0 :     d = c;
    2560             :   }
    2561             :   /* U was (h-1)x(h-1), V was (l-1)x(l-1), D was (h-1)x(l-1) */
    2562          21 :   y = cgetg(4,t_VEC);
    2563             :   /* truncate U to (c-1) x (h-1) */
    2564          21 :   gel(y,1) = t = cgetg(h,t_MAT);
    2565          77 :   for (j=1; j<h; j++) gel(t,j) = gcopy_lg(gel(U,j), c);
    2566             :   /* truncate V to (l-1) x (d-1) */
    2567          21 :   gel(y,2) = gcopy_lg(V, d);
    2568          21 :   gel(y,3) = t = zeromatcopy(c-1, d-1);
    2569             :   /* truncate D to a (c-1) x (d-1) matrix */
    2570          21 :   if (d > 1)
    2571             :   {
    2572          14 :     if (h > l)
    2573             :     {
    2574          14 :       for (i=1+h-l, j=1; i<c; i++,j++)
    2575           7 :         gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
    2576             :     }
    2577           7 :     else if (h < l)
    2578             :     {
    2579           7 :       for (i=1, j=1+l-h; j<d; i++,j++)
    2580           0 :         gcoeff(t,i,j) = gcopy(gcoeff(D,i,j));
    2581             :     }
    2582             :     else
    2583             :     {
    2584           0 :       for (j=1; j<d; j++)
    2585           0 :         gcoeff(t,j,j) = gcopy(gcoeff(D,j,j));
    2586             :     }
    2587             :   }
    2588          21 :   return y;
    2589             : }
    2590             : 
    2591             : /* does b = x[i,i] divide all entries in x[1..i-1,1..i-1] ? If so, return 0;
    2592             :  * else return the index of a problematic row */
    2593             : static long
    2594         196 : gsnf_no_divide(GEN x, long i, long vx)
    2595             : {
    2596         196 :   GEN b = gcoeff(x,i,i);
    2597             :   long j, k;
    2598             : 
    2599         196 :   if (gequal0(b))
    2600             :   {
    2601          14 :     for (k = 1; k < i; k++)
    2602          14 :       for (j = 1; j < i; j++)
    2603          14 :         if (!gequal0(gcoeff(x,k,j))) return k;
    2604           0 :     return 0;
    2605             :   }
    2606             : 
    2607         182 :   if (!is_RgX(b,vx) || degpol(b)<=0) return 0;
    2608         217 :   for (k = 1; k < i; k++)
    2609         378 :     for (j = 1; j < i; j++)
    2610             :     {
    2611         266 :       GEN z = gcoeff(x,k,j), r;
    2612         266 :       if (!is_RgX(z,vx)) z = scalarpol(z, vx);
    2613         266 :       r = RgX_rem(z, b);
    2614         266 :       if (signe(r) && (! isinexactreal(r) ||
    2615           0 :              gexpo(r) > 16 + gexpo(b) - prec2nbits(gprecision(r)))
    2616          35 :          ) return k;
    2617             :     }
    2618          70 :   return 0;
    2619             : }
    2620             : 
    2621             : /* Hermite Normal Form, with base change matrix if ptB != NULL.
    2622             :  * If 'remove' = 1, remove 0 columns (do NOT update *ptB accordingly)
    2623             :  * If 'remove' = 2, remove 0 columns and update *ptB accordingly */
    2624             : GEN
    2625          35 : RgM_hnfall(GEN A, GEN *pB, long remove)
    2626             : {
    2627             :   pari_sp av;
    2628             :   long li, j, k, m, n, def, ldef;
    2629             :   GEN B;
    2630          35 :   long vx = gvar(A);
    2631             : 
    2632          35 :   n = lg(A)-1;
    2633          35 :   if (vx==NO_VARIABLE || !n)
    2634             :   {
    2635           0 :     RgM_check_ZM(A, "mathnf0");
    2636           0 :     return ZM_hnfall(A, pB, remove);
    2637             :   }
    2638          35 :   m = nbrows(A);
    2639          35 :   av = avma;
    2640          35 :   A = RgM_shallowcopy(A);
    2641          35 :   B = pB? matid(n): NULL;
    2642          35 :   def = n; ldef = (m>n)? m-n: 0;
    2643          77 :   for (li=m; li>ldef; li--)
    2644             :   {
    2645             :     GEN d, T;
    2646          63 :     for (j=def-1; j; j--)
    2647             :     {
    2648          21 :       GEN a = gcoeff(A,li,j);
    2649          21 :       if (gequal0(a)) continue;
    2650             : 
    2651           7 :       k = (j==1)? def: j-1;
    2652           7 :       RgC_elem(a,gcoeff(A,li,k), A,B, j,k, li, vx);
    2653             :     }
    2654          42 :     T = normalize_as_RgX(gcoeff(A,li,def), vx, &d);
    2655          42 :     if (gequal0(T))
    2656           0 :     { if (ldef) ldef--; }
    2657             :     else
    2658             :     {
    2659          42 :       gcoeff(A,li,def) = T;
    2660          42 :       if (B && !gequal1(d)) gel(B, def) = RgC_Rg_div(gel(B, def), d);
    2661          42 :       RgM_reduce(A, B, li, def, vx);
    2662          42 :       def--;
    2663             :     }
    2664          42 :     if (gc_needed(av,1))
    2665             :     {
    2666           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ghnfall");
    2667           0 :       gerepileall(av, B? 2: 1, &A, &B);
    2668             :     }
    2669             :   }
    2670             :   /* rank A = n - def */
    2671          35 :   if (remove) remove_0cols(def, &A, &B, remove);
    2672          35 :   gerepileall(av, B? 2: 1, &A, &B);
    2673          35 :   if (B) *pB = B;
    2674          35 :   return A;
    2675             : }
    2676             : 
    2677             : static GEN
    2678          49 : RgXM_snf(GEN x,long all)
    2679             : {
    2680             :   pari_sp av;
    2681             :   long i, j, k, n;
    2682             :   GEN z, u, v, U, V;
    2683          49 :   long vx = gvar(x);
    2684          49 :   n = lg(x)-1; if (!n) return trivsmith(all);
    2685          49 :   if (vx==NO_VARIABLE) pari_err_TYPE("RgXM_snf",x);
    2686          49 :   if (lgcols(x) != n+1) pari_err_DIM("gsmithall");
    2687          49 :   av = avma;
    2688          49 :   x = RgM_shallowcopy(x);
    2689          49 :   if (all) { U = matid(n); V = matid(n); }
    2690         196 :   for (i=n; i>=2; i--)
    2691             :   {
    2692             :     for(;;)
    2693         168 :     {
    2694             :       GEN a, b, d;
    2695         315 :       int c = 0;
    2696        1092 :       for (j=i-1; j>=1; j--)
    2697             :       {
    2698         777 :         b = gcoeff(x,i,j); if (gequal0(b)) continue;
    2699         196 :         a = gcoeff(x,i,i);
    2700         196 :         d = gbezout_step(&b, &a, &v, &u, vx);
    2701         700 :         for (k = 1; k < i; k++)
    2702             :         {
    2703         504 :           GEN t = gadd(gmul(u,gcoeff(x,k,i)),gmul(v,gcoeff(x,k,j)));
    2704         504 :           gcoeff(x,k,j) = gsub(gmul(a,gcoeff(x,k,j)),gmul(b,gcoeff(x,k,i)));
    2705         504 :           gcoeff(x,k,i) = t;
    2706             :         }
    2707         196 :         gcoeff(x,i,j) = gen_0;
    2708         196 :         gcoeff(x,i,i) = d;
    2709         196 :         if (all) update(u,v,a,b,(GEN*)(V+i),(GEN*)(V+j));
    2710             :       }
    2711        1092 :       for (j=i-1; j>=1; j--)
    2712             :       {
    2713         777 :         b = gcoeff(x,j,i); if (gequal0(b)) continue;
    2714         175 :         a = gcoeff(x,i,i);
    2715         175 :         d = gbezout_step(&b, &a, &v, &u, vx);
    2716         651 :         for (k = 1; k < i; k++)
    2717             :         {
    2718         476 :           GEN t = gadd(gmul(u,gcoeff(x,i,k)),gmul(v,gcoeff(x,j,k)));
    2719         476 :           gcoeff(x,j,k) = gsub(gmul(a,gcoeff(x,j,k)),gmul(b,gcoeff(x,i,k)));
    2720         476 :           gcoeff(x,i,k) = t;
    2721             :         }
    2722         175 :         gcoeff(x,j,i) = gen_0;
    2723         175 :         gcoeff(x,i,i) = d;
    2724         175 :         if (all) update(u,v,a,b,(GEN*)(U+i),(GEN*)(U+j));
    2725         175 :         c = 1;
    2726             :       }
    2727         315 :       if (!c)
    2728             :       {
    2729         196 :         k = gsnf_no_divide(x, i, vx);
    2730         196 :         if (!k) break;
    2731             : 
    2732         245 :         for (j=1; j<=i; j++)
    2733         196 :           gcoeff(x,i,j) = gadd(gcoeff(x,i,j),gcoeff(x,k,j));
    2734          49 :         if (all) gel(U,i) = gadd(gel(U,i),gel(U,k));
    2735             :       }
    2736         168 :       if (gc_needed(av,1))
    2737             :       {
    2738           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"gsmithall");
    2739           0 :         gerepileall(av, all? 3: 1, &x, &U, &V);
    2740             :       }
    2741             :     }
    2742             :   }
    2743         245 :   for (k=1; k<=n; k++)
    2744             :   {
    2745         196 :     GEN d, T = normalize_as_RgX(gcoeff(x,k,k), vx, &d);
    2746         196 :     if (gequal0(T)) continue;
    2747         182 :     if (all && !gequal1(d)) gel(V,k) = RgC_Rg_div(gel(V,k), d);
    2748         182 :     gcoeff(x,k,k) = T;
    2749             :   }
    2750          49 :   z = all? mkvec3(shallowtrans(U), V, x): RgM_diagonal_shallow(x);
    2751          49 :   return gerepilecopy(av, z);
    2752             : }
    2753             : 
    2754             : GEN
    2755         469 : matsnf0(GEN x,long flag)
    2756             : {
    2757         469 :   pari_sp av = avma;
    2758         469 :   if (flag > 7) pari_err_FLAG("matsnf");
    2759         469 :   if (typ(x) == t_VEC && flag & 4) return smithclean(x);
    2760         469 :   if (typ(x)!=t_MAT) pari_err_TYPE("matsnf",x);
    2761         469 :   if (RgM_is_ZM(x)) x = flag&1 ? smithall(x): smith(x);
    2762          49 :   else              x = RgXM_snf(x, flag&1);
    2763         469 :   if (flag & 4) x = gerepileupto(av, smithclean(x));
    2764         469 :   return x;
    2765             : }
    2766             : GEN
    2767           0 : gsmith(GEN x) { return RgXM_snf(x,0); }
    2768             : GEN
    2769           0 : gsmithall(GEN x) { return RgXM_snf(x,1); }
    2770             : 
    2771             : /* H is a relation matrix, either in HNF or a t_VEC (diagonal HNF) */
    2772             : static GEN
    2773      910493 : snf_group(GEN H, GEN D, GEN *newU, GEN *newUi)
    2774             : {
    2775             :   long i, j, l;
    2776             : 
    2777      910493 :   ZM_snfclean(D, newU? *newU: NULL, newUi? *newUi: NULL);
    2778      910463 :   l = lg(D);
    2779      910463 :   if (newU) {
    2780      805387 :     GEN U = *newU;
    2781     2071452 :     for (i = 1; i < l; i++)
    2782             :     {
    2783     1266443 :       GEN d = gel(D,i), d2 = shifti(d, 1);
    2784     4979447 :       for (j = 1; j < lg(U); j++)
    2785     3713382 :         gcoeff(U,i,j) = centermodii(gcoeff(U,i,j), d, d2);
    2786             :     }
    2787      805009 :     *newU = U;
    2788             :   }
    2789      910085 :   if (newUi && l > 1)
    2790             :   { /* UHV=D -> U^-1 = (HV)D^-1 -> U^-1 = H(VD^-1 mod 1) mod H */
    2791             :     /* Ui = ZM_inv(U, NULL); setlg(Ui, l); */
    2792      823452 :     GEN V = *newUi, Ui;
    2793      823452 :     int Hvec = (typ(H) == t_VEC);
    2794     2249677 :     for (i = 1; i < l; i++) gel(V,i) = FpC_red(gel(V,i), gel(D,i));
    2795      823257 :     if (!Hvec)
    2796             :     {
    2797      538629 :       if (ZM_isdiagonal(H)) { H = RgM_diagonal_shallow(H); Hvec = 1; }
    2798             :     }
    2799      823571 :     Ui = Hvec? ZM_diag_mul(H, V): ZM_mul(H, V);
    2800     2249552 :     for (i = 1; i < l; i++) gel(Ui,i) = ZC_Z_divexact(gel(Ui,i), gel(D,i));
    2801      823286 :     if (Hvec)
    2802     1453174 :     { for (i = 1; i < l; i++) gel(Ui,i) = vecmodii(gel(Ui,i), H); }
    2803             :     else
    2804      313553 :       Ui = ZM_hnfrem(Ui, H);
    2805      823483 :     *newUi = Ui;
    2806             :   }
    2807      910116 :   return D;
    2808             : }
    2809             : /* H relation matrix among row of generators g in HNF.  Let URV = D its SNF,
    2810             :  * newU R newV = newD its clean SNF (no 1 in Dnew). Return the diagonal of
    2811             :  * newD, newU and newUi such that  1/U = (newUi, ?).
    2812             :  * Rationale: let (G,0) = g Ui be the new generators then
    2813             :  * 0 = G U R --> G D = 0,  g = G newU  and  G = g newUi */
    2814             : GEN
    2815      625251 : ZM_snf_group(GEN H, GEN *newU, GEN *newUi)
    2816             : {
    2817      625251 :   GEN D = ZM_snfall_i(H, newU, newUi, 1 + 2);
    2818      625335 :   return snf_group(H, D, newU, newUi);
    2819             : }
    2820             : 
    2821             : /* D a ZV: SNF for matdiagonal(D). Faster because we only ensure elementary
    2822             :  * divisors condition: d[n] | ... | d[1] and need not convert D to matrix form*/
    2823             : GEN
    2824      285176 : ZV_snfall(GEN D, GEN *pU, GEN *pV)
    2825             : {
    2826      285176 :   pari_sp av = avma;
    2827      285176 :   long j, n = lg(D)-1;
    2828      285176 :   GEN U = pU? matid(n): NULL;
    2829      285173 :   GEN V = pV? matid(n): NULL;
    2830             :   GEN p;
    2831             : 
    2832      285182 :   D = leafcopy(D);
    2833      912344 :   for (j = n; j > 0; j--)
    2834             :   {
    2835      627163 :     GEN b = gel(D,j);
    2836      627163 :     if (signe(b) < 0)
    2837             :     {
    2838           0 :       gel(D,j) = negi(b);
    2839           0 :       if (V) ZV_togglesign(gel(V,j));
    2840             :     }
    2841             :   }
    2842             :   /* entries are nonnegative integers */
    2843      285181 :   p = gen_indexsort(D, NULL, &negcmpii);
    2844      285176 :   D = vecpermute(D, p);
    2845      285174 :   if (U) U = vecpermute(U, p);
    2846      285169 :   if (V) V = vecpermute(V, p);
    2847             :   /* entries are sorted by decreasing value */
    2848      912287 :   for (j = n; j > 0; j--)
    2849             :   {
    2850      627098 :     GEN b = gel(D,j);
    2851             :     long i;
    2852     1130913 :     for (i = j-1; i > 0; i--)
    2853             :     { /* invariant: a >= b. If au+bv = d is a Bezout relation, A=a/d and B=b/d
    2854             :        * we have [B,-A;u,v]*diag(a,b)*[1-u*A,1; -u*A,1]] = diag(Ab, d) */
    2855      515975 :       GEN a = gel(D,i), u,v, d = bezout(a,b, &u,&v), A, Wi, Wj;
    2856      515968 :       if (equalii(d,b)) continue;
    2857       68107 :       A = diviiexact(a,d);
    2858       68107 :       if (V)
    2859             :       {
    2860       68065 :         GEN t = mulii(u,A);
    2861       68065 :         Wi = ZC_lincomb(subui(1,t), negi(t), gel(V,i), gel(V,j));
    2862       68067 :         Wj = ZC_add(gel(V,i), gel(V,j));
    2863       68067 :         gel(V,i) = Wi;
    2864       68067 :         gel(V,j) = Wj;
    2865             :       }
    2866       68109 :       if (U)
    2867             :       {
    2868       68109 :         GEN B = diviiexact(b,d);
    2869       68109 :         Wi = ZC_lincomb(B, negi(A), gel(U,i), gel(U,j));
    2870       68109 :         Wj = ZC_lincomb(u, v, gel(U,i), gel(U,j));
    2871       68109 :         gel(U,i) = Wi;
    2872       68109 :         gel(U,j) = Wj;
    2873             :       }
    2874       68109 :       gel(D,i) = mulii(A,b); /* lcm(a,b) */
    2875       68109 :       gel(D,j) = d; /* gcd(a,b) */
    2876       68109 :       b = gel(D,j); if (equali1(b)) break;
    2877             :     }
    2878             :   }
    2879      285189 :   snf_pile(av, &D,&U,&V);
    2880      285189 :   if (U) *pU = shallowtrans(U);
    2881      285188 :   if (V) *pV = V;
    2882      285188 :   return D;
    2883             : }
    2884             : GEN
    2885      285176 : ZV_snf_group(GEN d, GEN *newU, GEN *newUi)
    2886             : {
    2887      285176 :   GEN D = ZV_snfall(d, newU, newUi);
    2888      285188 :   return snf_group(d, D, newU, newUi);
    2889             : }
    2890             : 
    2891             : /* D a vector of elementary divisors. Truncate (setlg) to leave out trivial
    2892             :  * entries (= 1) */
    2893             : void
    2894           0 : ZV_snf_trunc(GEN D)
    2895             : {
    2896           0 :   long i, l = lg(D);
    2897           0 :   for (i = 1; i < l; i++)
    2898           0 :     if (is_pm1(gel(D,i))) { setlg(D,i); break; }
    2899           0 : }
    2900             : 
    2901             : long
    2902           0 : zv_snf_rank(GEN D, ulong p)
    2903             : {
    2904           0 :   long i, l = lg(D);
    2905           0 :   if (!p) return l - 1;
    2906           0 :   for (i = 1; i < l; i++)
    2907           0 :     if (D[i] % p) break;
    2908           0 :   return i - 1;
    2909             : }
    2910             : long
    2911          42 : ZV_snf_rank_u(GEN D, ulong p)
    2912             : {
    2913          42 :   long i, l = lg(D);
    2914          42 :   if (!p) return l - 1;
    2915          42 :   if (p == 2)
    2916             :   {
    2917          28 :     for (i = 1; i < l; i++)
    2918          21 :       if (mpodd(gel(D,i))) break;
    2919             :   }
    2920          35 :   else if (!(p & (p-1)))
    2921             :   { /* power of 2 */
    2922          14 :     long n = vals(p);
    2923          28 :     for (i = 1; i < l; i++)
    2924          28 :       if (umodi2n(gel(D,i), n)) break;
    2925             :   }
    2926             :   else
    2927             :   {
    2928          49 :     for (i = 1; i < l; i++)
    2929          42 :       if (umodiu(gel(D,i), p)) break;
    2930             :   }
    2931          42 :   return i - 1;
    2932             : }
    2933             : long
    2934          70 : ZV_snf_rank(GEN D, GEN p)
    2935             : {
    2936             :   long i, l;
    2937          70 :   switch(lgefint(p))
    2938             :   {
    2939           7 :     case 2: return lg(D) - 1;
    2940          42 :     case 3: return ZV_snf_rank_u(D, p[2]);
    2941             :   }
    2942          21 :   l = lg(D);
    2943          77 :   for (i = 1; i < l; i++)
    2944          77 :     if (!dvdii(gel(D,i), p)) break;
    2945          21 :   return i - 1;
    2946             : }
    2947             : long
    2948         119 : snfrank(GEN D, GEN p)
    2949             : {
    2950             :   long i, l;
    2951         119 :   if (typ(D) != t_VEC) pari_err_TYPE("snfrank", D);
    2952         119 :   l = lg(D);
    2953         119 :   if (l == 4 && typ(gel(D,3)) == t_MAT)
    2954             :   { /* from matsnf(,1) */
    2955          14 :     pari_sp av = avma;
    2956             :     long z;
    2957             :     GEN v;
    2958          14 :     D = gel(D,3); l = lg(D);
    2959          14 :     if (l == 1) return 0;
    2960          14 :     z = lgcols(D) - l; /* missing columns of 0s */
    2961          14 :     if (z < 0) pari_err_TYPE("snfrank", D);
    2962          14 :     v = cgetg(l, t_VEC);
    2963          35 :     for (i = 1; i < l; i++) gel(v, i) = gcoeff(D, i + z, i);
    2964          14 :     return gc_long(av, z + snfrank(v, p));
    2965             :   }
    2966         105 :   switch(typ(p))
    2967             :   {
    2968          70 :     case t_INT:
    2969          70 :       if (!RgV_is_ZV(D)) pari_err_TYPE("snfrank", D);
    2970          70 :       return ZV_snf_rank(D, p);
    2971          35 :     case t_POL: break;
    2972           0 :     default: pari_err_TYPE("snfrank", p);
    2973             :   }
    2974          91 :   for (i = 1; i < l; i++)
    2975          91 :     if (!gdvd(gel(D,i), p)) break;
    2976          35 :   return i - 1;
    2977             : }

Generated by: LCOV version 1.13