Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - hnf_snf.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 22303-eb3e11d) Lines: 1520 1670 91.0 %
Date: 2018-04-20 06:16:30 Functions: 86 91 94.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11