Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - alglin1.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 24215-a79de5b25) Lines: 2610 3019 86.5 %
Date: 2019-08-24 05:50:50 Functions: 283 304 93.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000, 2012  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             : /********************************************************************/
      15             : /**                                                                **/
      16             : /**                         LINEAR ALGEBRA                         **/
      17             : /**                          (first part)                          **/
      18             : /**                                                                **/
      19             : /********************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : /*******************************************************************/
      24             : /*                                                                 */
      25             : /*                         GEREPILE                                */
      26             : /*                                                                 */
      27             : /*******************************************************************/
      28             : 
      29             : static void
      30           0 : gerepile_mat(pari_sp av, pari_sp tetpil, GEN x, long k, long m, long n, long t)
      31             : {
      32           0 :   pari_sp A, bot = pari_mainstack->bot;
      33             :   long u, i;
      34             :   size_t dec;
      35             : 
      36           0 :   (void)gerepile(av,tetpil,NULL); dec = av-tetpil;
      37             : 
      38           0 :   for (u=t+1; u<=m; u++)
      39             :   {
      40           0 :     A = (pari_sp)coeff(x,u,k);
      41           0 :     if (A < av && A >= bot) coeff(x,u,k) += dec;
      42             :   }
      43           0 :   for (i=k+1; i<=n; i++)
      44           0 :     for (u=1; u<=m; u++)
      45             :     {
      46           0 :       A = (pari_sp)coeff(x,u,i);
      47           0 :       if (A < av && A >= bot) coeff(x,u,i) += dec;
      48             :     }
      49           0 : }
      50             : 
      51             : static void
      52           0 : gen_gerepile_gauss_ker(GEN x, long k, long t, pari_sp av, void *E, GEN (*copy)(void*, GEN))
      53             : {
      54           0 :   pari_sp tetpil = avma;
      55           0 :   long u,i, n = lg(x)-1, m = n? nbrows(x): 0;
      56             : 
      57           0 :   if (DEBUGMEM > 1) pari_warn(warnmem,"gauss_pivot_ker. k=%ld, n=%ld",k,n);
      58           0 :   for (u=t+1; u<=m; u++) gcoeff(x,u,k) = copy(E,gcoeff(x,u,k));
      59           0 :   for (i=k+1; i<=n; i++)
      60           0 :     for (u=1; u<=m; u++) gcoeff(x,u,i) = copy(E,gcoeff(x,u,i));
      61           0 :   gerepile_mat(av,tetpil,x,k,m,n,t);
      62           0 : }
      63             : 
      64             : /* special gerepile for huge matrices */
      65             : 
      66             : #define COPY(x) {\
      67             :   GEN _t = (x); if (!is_universal_constant(_t)) x = gcopy(_t); \
      68             : }
      69             : 
      70             : INLINE GEN
      71           0 : _copy(void *E, GEN x)
      72             : {
      73           0 :   (void) E; COPY(x);
      74           0 :   return x;
      75             : }
      76             : 
      77             : static void
      78           0 : gerepile_gauss_ker(GEN x, long k, long t, pari_sp av)
      79             : {
      80           0 :   gen_gerepile_gauss_ker(x, k, t, av, NULL, &_copy);
      81           0 : }
      82             : 
      83             : static void
      84           0 : gerepile_gauss(GEN x,long k,long t,pari_sp av, long j, GEN c)
      85             : {
      86           0 :   pari_sp tetpil = avma, A, bot;
      87           0 :   long u,i, n = lg(x)-1, m = n? nbrows(x): 0;
      88             :   size_t dec;
      89             : 
      90           0 :   if (DEBUGMEM > 1) pari_warn(warnmem,"gauss_pivot. k=%ld, n=%ld",k,n);
      91           0 :   for (u=t+1; u<=m; u++)
      92           0 :     if (u==j || !c[u]) COPY(gcoeff(x,u,k));
      93           0 :   for (u=1; u<=m; u++)
      94           0 :     if (u==j || !c[u])
      95           0 :       for (i=k+1; i<=n; i++) COPY(gcoeff(x,u,i));
      96             : 
      97           0 :   (void)gerepile(av,tetpil,NULL); dec = av-tetpil;
      98           0 :   bot = pari_mainstack->bot;
      99           0 :   for (u=t+1; u<=m; u++)
     100           0 :     if (u==j || !c[u])
     101             :     {
     102           0 :       A=(pari_sp)coeff(x,u,k);
     103           0 :       if (A<av && A>=bot) coeff(x,u,k)+=dec;
     104             :     }
     105           0 :   for (u=1; u<=m; u++)
     106           0 :     if (u==j || !c[u])
     107           0 :       for (i=k+1; i<=n; i++)
     108             :       {
     109           0 :         A=(pari_sp)coeff(x,u,i);
     110           0 :         if (A<av && A>=bot) coeff(x,u,i)+=dec;
     111             :       }
     112           0 : }
     113             : 
     114             : /*******************************************************************/
     115             : /*                                                                 */
     116             : /*                         GENERIC                                 */
     117             : /*                                                                 */
     118             : /*******************************************************************/
     119             : GEN
     120        1430 : gen_ker(GEN x, long deplin, void *E, const struct bb_field *ff)
     121             : {
     122        1430 :   pari_sp av0 = avma, av, tetpil;
     123             :   GEN y, c, d;
     124             :   long i, j, k, r, t, n, m;
     125             : 
     126        1430 :   n=lg(x)-1; if (!n) return cgetg(1,t_MAT);
     127        1430 :   m=nbrows(x); r=0;
     128        1430 :   x = RgM_shallowcopy(x);
     129        1430 :   c = zero_zv(m);
     130        1430 :   d=new_chunk(n+1);
     131        1430 :   av=avma;
     132        5106 :   for (k=1; k<=n; k++)
     133             :   {
     134       10549 :     for (j=1; j<=m; j++)
     135        8934 :       if (!c[j])
     136             :       {
     137        6125 :         gcoeff(x,j,k) = ff->red(E, gcoeff(x,j,k));
     138        6125 :         if (!ff->equal0(gcoeff(x,j,k))) break;
     139             :       }
     140        3704 :     if (j>m)
     141             :     {
     142        1615 :       if (deplin)
     143             :       {
     144          28 :         GEN c = cgetg(n+1, t_COL), g0 = ff->s(E,0), g1=ff->s(E,1);
     145          28 :         for (i=1; i<k; i++) gel(c,i) = ff->red(E, gcoeff(x,d[i],k));
     146          28 :         gel(c,k) = g1; for (i=k+1; i<=n; i++) gel(c,i) = g0;
     147          28 :         return gerepileupto(av0, c);
     148             :       }
     149        1587 :       r++; d[k]=0;
     150        3959 :       for(j=1; j<k; j++)
     151        2372 :         if (d[j]) gcoeff(x,d[j],k) = gclone(gcoeff(x,d[j],k));
     152             :     }
     153             :     else
     154             :     {
     155        2089 :       GEN piv = ff->neg(E,ff->inv(E,gcoeff(x,j,k)));
     156        2089 :       c[j] = k; d[k] = j;
     157        2089 :       gcoeff(x,j,k) = ff->s(E,-1);
     158        2089 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = ff->red(E,ff->mul(E,piv,gcoeff(x,j,i)));
     159       10193 :       for (t=1; t<=m; t++)
     160             :       {
     161        8104 :         if (t==j) continue;
     162             : 
     163        6015 :         piv = ff->red(E,gcoeff(x,t,k));
     164        6015 :         if (ff->equal0(piv)) continue;
     165             : 
     166        1518 :         gcoeff(x,t,k) = ff->s(E,0);
     167        3980 :         for (i=k+1; i<=n; i++)
     168        4924 :            gcoeff(x,t,i) = ff->red(E, ff->add(E, gcoeff(x,t,i),
     169        2462 :                                       ff->mul(E,piv,gcoeff(x,j,i))));
     170        1518 :         if (gc_needed(av,1))
     171           0 :           gen_gerepile_gauss_ker(x,k,t,av,E,ff->red);
     172             :       }
     173             :     }
     174             :   }
     175        1402 :   if (deplin) return gc_NULL(av0);
     176             : 
     177        1374 :   tetpil=avma; y=cgetg(r+1,t_MAT);
     178        2961 :   for (j=k=1; j<=r; j++,k++)
     179             :   {
     180        1587 :     GEN C = cgetg(n+1,t_COL);
     181        1587 :     GEN g0 = ff->s(E,0), g1 = ff->s(E,1);
     182        1587 :     gel(y,j) = C; while (d[k]) k++;
     183        3959 :     for (i=1; i<k; i++)
     184        2372 :       if (d[i])
     185             :       {
     186        1981 :         GEN p1=gcoeff(x,d[i],k);
     187        1981 :         gel(C,i) = ff->red(E,p1); gunclone(p1);
     188             :       }
     189             :       else
     190         391 :         gel(C,i) = g0;
     191        1587 :     gel(C,k) = g1; for (i=k+1; i<=n; i++) gel(C,i) = g0;
     192             :   }
     193        1374 :   return gerepile(av0,tetpil,y);
     194             : }
     195             : 
     196             : GEN
     197        1492 : gen_Gauss_pivot(GEN x, long *rr, void *E, const struct bb_field *ff)
     198             : {
     199             :   pari_sp av;
     200             :   GEN c, d;
     201        1492 :   long i, j, k, r, t, m, n = lg(x)-1;
     202             : 
     203        1492 :   if (!n) { *rr = 0; return NULL; }
     204             : 
     205        1492 :   m=nbrows(x); r=0;
     206        1492 :   d = cgetg(n+1, t_VECSMALL);
     207        1492 :   x = RgM_shallowcopy(x);
     208        1492 :   c = zero_zv(m);
     209        1492 :   av=avma;
     210        5546 :   for (k=1; k<=n; k++)
     211             :   {
     212       10559 :     for (j=1; j<=m; j++)
     213       10271 :       if (!c[j])
     214             :       {
     215        6932 :         gcoeff(x,j,k) = ff->red(E,gcoeff(x,j,k));
     216        6932 :         if (!ff->equal0(gcoeff(x,j,k))) break;
     217             :       }
     218        4054 :     if (j>m) { r++; d[k]=0; }
     219             :     else
     220             :     {
     221        3766 :       GEN piv = ff->neg(E,ff->inv(E,gcoeff(x,j,k)));
     222        3766 :       GEN g0 = ff->s(E,0);
     223        3766 :       c[j] = k; d[k] = j;
     224        3766 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = ff->red(E,ff->mul(E,piv,gcoeff(x,j,i)));
     225       23352 :       for (t=1; t<=m; t++)
     226             :       {
     227       19586 :         if (c[t]) continue; /* already a pivot on that line */
     228             : 
     229       12047 :         piv = ff->red(E,gcoeff(x,t,k));
     230       12047 :         if (ff->equal0(piv)) continue;
     231        4767 :         gcoeff(x,t,k) = g0;
     232        8687 :         for (i=k+1; i<=n; i++)
     233        3920 :           gcoeff(x,t,i) = ff->red(E, ff->add(E,gcoeff(x,t,i), ff->mul(E,piv,gcoeff(x,j,i))));
     234        4767 :         if (gc_needed(av,1))
     235           0 :           gerepile_gauss(x,k,t,av,j,c);
     236             :       }
     237        3766 :       for (i=k; i<=n; i++) gcoeff(x,j,i) = g0; /* dummy */
     238             :     }
     239             :   }
     240        1492 :   *rr = r; set_avma((pari_sp)d); return d;
     241             : }
     242             : 
     243             : GEN
     244         294 : gen_det(GEN a, void *E, const struct bb_field *ff)
     245             : {
     246         294 :   pari_sp av = avma;
     247         294 :   long i,j,k, s = 1, nbco = lg(a)-1;
     248         294 :   GEN x = ff->s(E,1);
     249         294 :   if (!nbco) return x;
     250         287 :   a = RgM_shallowcopy(a);
     251        1064 :   for (i=1; i<nbco; i++)
     252             :   {
     253             :     GEN q;
     254        1029 :     for(k=i; k<=nbco; k++)
     255             :     {
     256         994 :       gcoeff(a,k,i) = ff->red(E,gcoeff(a,k,i));
     257         994 :       if (!ff->equal0(gcoeff(a,k,i))) break;
     258             :     }
     259         812 :     if (k > nbco) return gerepileupto(av, gcoeff(a,i,i));
     260         777 :     if (k != i)
     261             :     { /* exchange the lines s.t. k = i */
     262         105 :       for (j=i; j<=nbco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
     263         105 :       s = -s;
     264             :     }
     265         777 :     q = gcoeff(a,i,i);
     266         777 :     x = ff->red(E,ff->mul(E,x,q));
     267         777 :     q = ff->inv(E,q);
     268        2324 :     for (k=i+1; k<=nbco; k++)
     269             :     {
     270        1547 :       GEN m = ff->red(E,gcoeff(a,i,k));
     271        1547 :       if (ff->equal0(m)) continue;
     272        1092 :       m = ff->neg(E, ff->red(E,ff->mul(E,m, q)));
     273        3528 :       for (j=i+1; j<=nbco; j++)
     274        4872 :         gcoeff(a,j,k) = ff->red(E, ff->add(E, gcoeff(a,j,k),
     275        2436 :                                    ff->mul(E, m, gcoeff(a,j,i))));
     276             :     }
     277         777 :     if (gc_needed(av,2))
     278             :     {
     279           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
     280           0 :       gerepileall(av,2, &a,&x);
     281             :     }
     282             :   }
     283         252 :   if (s < 0) x = ff->neg(E,x);
     284         252 :   return gerepileupto(av, ff->red(E,ff->mul(E, x, gcoeff(a,nbco,nbco))));
     285             : }
     286             : 
     287             : INLINE void
     288      146825 : _gen_addmul(GEN b, long k, long i, GEN m, void *E, const struct bb_field *ff)
     289             : {
     290      146825 :   gel(b,i) = ff->red(E,gel(b,i));
     291      146825 :   gel(b,k) = ff->add(E,gel(b,k), ff->mul(E,m, gel(b,i)));
     292      146825 : }
     293             : 
     294             : static GEN
     295       54001 : _gen_get_col(GEN a, GEN b, long li, void *E, const struct bb_field *ff)
     296             : {
     297       54001 :   GEN u = cgetg(li+1,t_COL);
     298       54001 :   pari_sp av = avma;
     299             :   long i, j;
     300             : 
     301       54001 :   gel(u,li) = gerepileupto(av, ff->red(E,ff->mul(E,gel(b,li), gcoeff(a,li,li))));
     302      274418 :   for (i=li-1; i>0; i--)
     303             :   {
     304      220417 :     pari_sp av = avma;
     305      220417 :     GEN m = gel(b,i);
     306      220417 :     for (j=i+1; j<=li; j++) m = ff->add(E,m, ff->neg(E,ff->mul(E,gcoeff(a,i,j), gel(u,j))));
     307      220417 :     m = ff->red(E, m);
     308      220417 :     gel(u,i) = gerepileupto(av, ff->red(E,ff->mul(E,m, gcoeff(a,i,i))));
     309             :   }
     310       54001 :   return u;
     311             : }
     312             : 
     313             : GEN
     314       12243 : gen_Gauss(GEN a, GEN b, void *E, const struct bb_field *ff)
     315             : {
     316             :   long i, j, k, li, bco, aco;
     317       12243 :   GEN u, g0 = ff->s(E,0);
     318       12243 :   pari_sp av = avma;
     319       12243 :   a = RgM_shallowcopy(a);
     320       12243 :   b = RgM_shallowcopy(b);
     321       12243 :   aco = lg(a)-1; bco = lg(b)-1; li = nbrows(a);
     322       52860 :   for (i=1; i<=aco; i++)
     323             :   {
     324             :     GEN invpiv;
     325       63283 :     for (k = i; k <= li; k++)
     326             :     {
     327       63227 :       GEN piv = ff->red(E,gcoeff(a,k,i));
     328       63227 :       if (!ff->equal0(piv)) { gcoeff(a,k,i) = ff->inv(E,piv); break; }
     329       10423 :       gcoeff(a,k,i) = g0;
     330             :     }
     331             :     /* found a pivot on line k */
     332       52860 :     if (k > li) return NULL;
     333       52804 :     if (k != i)
     334             :     { /* swap lines so that k = i */
     335        8344 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
     336        8344 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
     337             :     }
     338       52804 :     if (i == aco) break;
     339             : 
     340       40617 :     invpiv = gcoeff(a,i,i); /* 1/piv mod p */
     341      151578 :     for (k=i+1; k<=li; k++)
     342             :     {
     343      110961 :       GEN m = ff->red(E,gcoeff(a,k,i)); gcoeff(a,k,i) = g0;
     344      110961 :       if (ff->equal0(m)) continue;
     345             : 
     346       18407 :       m = ff->red(E,ff->neg(E,ff->mul(E,m, invpiv)));
     347       18407 :       for (j=i+1; j<=aco; j++) _gen_addmul(gel(a,j),k,i,m,E,ff);
     348       18407 :       for (j=1  ; j<=bco; j++) _gen_addmul(gel(b,j),k,i,m,E,ff);
     349             :     }
     350       40617 :     if (gc_needed(av,1))
     351             :     {
     352           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Gauss. i=%ld",i);
     353           0 :       gerepileall(av,2, &a,&b);
     354             :     }
     355             :   }
     356             : 
     357       12187 :   if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
     358       12187 :   u = cgetg(bco+1,t_MAT);
     359       12187 :   for (j=1; j<=bco; j++) gel(u,j) = _gen_get_col(a, gel(b,j), aco, E, ff);
     360       12187 :   return u;
     361             : }
     362             : 
     363             : /* compatible t_MAT * t_COL, lgA = lg(A) = lg(B) > 1, l = lgcols(A) */
     364             : static GEN
     365      557570 : gen_matcolmul_i(GEN A, GEN B, ulong lgA, ulong l,
     366             :                 void *E, const struct bb_field *ff)
     367             : {
     368      557570 :   GEN C = cgetg(l, t_COL);
     369             :   ulong i;
     370     3682184 :   for (i = 1; i < l; i++) {
     371     3124614 :     pari_sp av = avma;
     372     3124614 :     GEN e = ff->mul(E, gcoeff(A, i, 1), gel(B, 1));
     373             :     ulong k;
     374    12791180 :     for(k = 2; k < lgA; k++)
     375     9666566 :       e = ff->add(E, e, ff->mul(E, gcoeff(A, i, k), gel(B, k)));
     376     3124614 :     gel(C, i) = gerepileupto(av, ff->red(E, e));
     377             :   }
     378      557570 :   return C;
     379             : }
     380             : 
     381             : GEN
     382      170324 : gen_matcolmul(GEN A, GEN B, void *E, const struct bb_field *ff)
     383             : {
     384      170324 :   ulong lgA = lg(A);
     385      170324 :   if (lgA != (ulong)lg(B))
     386           0 :     pari_err_OP("operation 'gen_matcolmul'", A, B);
     387      170324 :   if (lgA == 1)
     388           0 :     return cgetg(1, t_COL);
     389      170324 :   return gen_matcolmul_i(A, B, lgA, lgcols(A), E, ff);
     390             : }
     391             : 
     392             : static GEN
     393       75409 : gen_matmul_classical(GEN A, GEN B, long l, long la, long lb,
     394             :                      void *E, const struct bb_field *ff)
     395             : {
     396             :   long j;
     397       75409 :   GEN C = cgetg(lb, t_MAT);
     398      462655 :   for(j = 1; j < lb; j++)
     399      387246 :     gel(C, j) = gen_matcolmul_i(A, gel(B, j), la, l, E, ff);
     400       75409 :   return C;
     401             : }
     402             : 
     403             : /* Strassen-Winograd algorithm */
     404             : 
     405             : /*
     406             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     407             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     408             : */
     409             : static GEN
     410           0 : add_slices(long m, long n,
     411             :            GEN A, long ma, long da, long na, long ea,
     412             :            GEN B, long mb, long db, long nb, long eb,
     413             :            void *E, const struct bb_field *ff)
     414             : {
     415           0 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     416           0 :   GEN M = cgetg(n + 1, t_MAT), C;
     417             : 
     418           0 :   for (j = 1; j <= min_e; j++) {
     419           0 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     420           0 :     for (i = 1; i <= min_d; i++)
     421           0 :       gel(C, i) = ff->add(E, gcoeff(A, ma + i, na + j),
     422           0 :                           gcoeff(B, mb + i, nb + j));
     423           0 :     for (; i <= da; i++)
     424           0 :       gel(C, i) = gcoeff(A, ma + i, na + j);
     425           0 :     for (; i <= db; i++)
     426           0 :       gel(C, i) = gcoeff(B, mb + i, nb + j);
     427           0 :     for (; i <= m; i++)
     428           0 :       gel(C, i) = ff->s(E, 0);
     429             :   }
     430           0 :   for (; j <= ea; j++) {
     431           0 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     432           0 :     for (i = 1; i <= da; i++)
     433           0 :       gel(C, i) = gcoeff(A, ma + i, na + j);
     434           0 :     for (; i <= m; i++)
     435           0 :       gel(C, i) = ff->s(E, 0);
     436             :   }
     437           0 :   for (; j <= eb; j++) {
     438           0 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     439           0 :     for (i = 1; i <= db; i++)
     440           0 :       gel(C, i) = gcoeff(B, mb + i, nb + j);
     441           0 :     for (; i <= m; i++)
     442           0 :       gel(C, i) = ff->s(E, 0);
     443             :   }
     444           0 :   for (; j <= n; j++) {
     445           0 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     446           0 :     for (i = 1; i <= m; i++)
     447           0 :       gel(C, i) = ff->s(E, 0);
     448             :   }
     449           0 :   return M;
     450             : }
     451             : 
     452             : /*
     453             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     454             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     455             : */
     456             : static GEN
     457           0 : subtract_slices(long m, long n,
     458             :                 GEN A, long ma, long da, long na, long ea,
     459             :                 GEN B, long mb, long db, long nb, long eb,
     460             :                 void *E, const struct bb_field *ff)
     461             : {
     462           0 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     463           0 :   GEN M = cgetg(n + 1, t_MAT), C;
     464             : 
     465           0 :   for (j = 1; j <= min_e; j++) {
     466           0 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     467           0 :     for (i = 1; i <= min_d; i++)
     468           0 :       gel(C, i) = ff->add(E, gcoeff(A, ma + i, na + j),
     469           0 :                           ff->neg(E, gcoeff(B, mb + i, nb + j)));
     470           0 :     for (; i <= da; i++)
     471           0 :       gel(C, i) = gcoeff(A, ma + i, na + j);
     472           0 :     for (; i <= db; i++)
     473           0 :       gel(C, i) = ff->neg(E, gcoeff(B, mb + i, nb + j));
     474           0 :     for (; i <= m; i++)
     475           0 :       gel(C, i) = ff->s(E, 0);
     476             :   }
     477           0 :   for (; j <= ea; j++) {
     478           0 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     479           0 :     for (i = 1; i <= da; i++)
     480           0 :       gel(C, i) = gcoeff(A, ma + i, na + j);
     481           0 :     for (; i <= m; i++)
     482           0 :       gel(C, i) = ff->s(E, 0);
     483             :   }
     484           0 :   for (; j <= eb; j++) {
     485           0 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     486           0 :     for (i = 1; i <= db; i++)
     487           0 :       gel(C, i) = ff->neg(E, gcoeff(B, mb + i, nb + j));
     488           0 :     for (; i <= m; i++)
     489           0 :       gel(C, i) = ff->s(E, 0);
     490             :   }
     491           0 :   for (; j <= n; j++) {
     492           0 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     493           0 :     for (i = 1; i <= m; i++)
     494           0 :       gel(C, i) = ff->s(E, 0);
     495             :   }
     496           0 :   return M;
     497             : }
     498             : 
     499             : static GEN gen_matmul_i(GEN A, GEN B, long l, long la, long lb,
     500             :                         void *E, const struct bb_field *ff);
     501             : 
     502             : static GEN
     503           0 : gen_matmul_sw(GEN A, GEN B, long m, long n, long p,
     504             :               void *E, const struct bb_field *ff)
     505             : {
     506           0 :   pari_sp av = avma;
     507           0 :   long m1 = (m + 1)/2, m2 = m/2,
     508           0 :     n1 = (n + 1)/2, n2 = n/2,
     509           0 :     p1 = (p + 1)/2, p2 = p/2;
     510             :   GEN A11, A12, A22, B11, B21, B22,
     511             :     S1, S2, S3, S4, T1, T2, T3, T4,
     512             :     M1, M2, M3, M4, M5, M6, M7,
     513             :     V1, V2, V3, C11, C12, C21, C22, C;
     514             : 
     515           0 :   T2 = subtract_slices(n1, p2, B, 0, n1, p1, p2, B, n1, n2, p1, p2, E, ff);
     516           0 :   S1 = subtract_slices(m2, n1, A, m1, m2, 0, n1, A, 0, m2, 0, n1, E, ff);
     517           0 :   M2 = gen_matmul_i(S1, T2, m2 + 1, n1 + 1, p2 + 1, E, ff);
     518           0 :   if (gc_needed(av, 1))
     519           0 :     gerepileall(av, 2, &T2, &M2);  /* destroy S1 */
     520           0 :   T3 = subtract_slices(n1, p1, T2, 0, n1, 0, p2, B, 0, n1, 0, p1, E, ff);
     521           0 :   if (gc_needed(av, 1))
     522           0 :     gerepileall(av, 2, &M2, &T3);  /* destroy T2 */
     523           0 :   S2 = add_slices(m2, n1, A, m1, m2, 0, n1, A, m1, m2, n1, n2, E, ff);
     524           0 :   T1 = subtract_slices(n1, p1, B, 0, n1, p1, p2, B, 0, n1, 0, p2, E, ff);
     525           0 :   M3 = gen_matmul_i(S2, T1, m2 + 1, n1 + 1, p2 + 1, E, ff);
     526           0 :   if (gc_needed(av, 1))
     527           0 :     gerepileall(av, 4, &M2, &T3, &S2, &M3);  /* destroy T1 */
     528           0 :   S3 = subtract_slices(m1, n1, S2, 0, m2, 0, n1, A, 0, m1, 0, n1, E, ff);
     529           0 :   if (gc_needed(av, 1))
     530           0 :     gerepileall(av, 4, &M2, &T3, &M3, &S3);  /* destroy S2 */
     531           0 :   A11 = matslice(A, 1, m1, 1, n1);
     532           0 :   B11 = matslice(B, 1, n1, 1, p1);
     533           0 :   M1 = gen_matmul_i(A11, B11, m1 + 1, n1 + 1, p1 + 1, E, ff);
     534           0 :   if (gc_needed(av, 1))
     535           0 :     gerepileall(av, 5, &M2, &T3, &M3, &S3, &M1);  /* destroy A11, B11 */
     536           0 :   A12 = matslice(A, 1, m1, n1 + 1, n);
     537           0 :   B21 = matslice(B, n1 + 1, n, 1, p1);
     538           0 :   M4 = gen_matmul_i(A12, B21, m1 + 1, n2 + 1, p1 + 1, E, ff);
     539           0 :   if (gc_needed(av, 1))
     540           0 :     gerepileall(av, 6, &M2, &T3, &M3, &S3, &M1, &M4);  /* destroy A12, B21 */
     541           0 :   C11 = add_slices(m1, p1, M1, 0, m1, 0, p1, M4, 0, m1, 0, p1, E, ff);
     542           0 :   if (gc_needed(av, 1))
     543           0 :     gerepileall(av, 6, &M2, &T3, &M3, &S3, &M1, &C11);  /* destroy M4 */
     544           0 :   M5 = gen_matmul_i(S3, T3, m1 + 1, n1 + 1, p1 + 1, E, ff);
     545           0 :   S4 = subtract_slices(m1, n2, A, 0, m1, n1, n2, S3, 0, m1, 0, n2, E, ff);
     546           0 :   if (gc_needed(av, 1))
     547           0 :     gerepileall(av, 7, &M2, &T3, &M3, &M1, &C11, &M5, &S4);  /* destroy S3 */
     548           0 :   T4 = add_slices(n2, p1, B, n1, n2, 0, p1, T3, 0, n2, 0, p1, E, ff);
     549           0 :   if (gc_needed(av, 1))
     550           0 :     gerepileall(av, 7, &M2, &M3, &M1, &C11, &M5, &S4, &T4);  /* destroy T3 */
     551           0 :   V1 = subtract_slices(m1, p1, M1, 0, m1, 0, p1, M5, 0, m1, 0, p1, E, ff);
     552           0 :   if (gc_needed(av, 1))
     553           0 :     gerepileall(av, 6, &M2, &M3, &S4, &T4, &C11, &V1);  /* destroy M1, M5 */
     554           0 :   B22 = matslice(B, n1 + 1, n, p1 + 1, p);
     555           0 :   M6 = gen_matmul_i(S4, B22, m1 + 1, n2 + 1, p2 + 1, E, ff);
     556           0 :   if (gc_needed(av, 1))
     557           0 :     gerepileall(av, 6, &M2, &M3, &T4, &C11, &V1, &M6);  /* destroy S4, B22 */
     558           0 :   A22 = matslice(A, m1 + 1, m, n1 + 1, n);
     559           0 :   M7 = gen_matmul_i(A22, T4, m2 + 1, n2 + 1, p1 + 1, E, ff);
     560           0 :   if (gc_needed(av, 1))
     561           0 :     gerepileall(av, 6, &M2, &M3, &C11, &V1, &M6, &M7);  /* destroy A22, T4 */
     562           0 :   V3 = add_slices(m1, p2, V1, 0, m1, 0, p2, M3, 0, m2, 0, p2, E, ff);
     563           0 :   C12 = add_slices(m1, p2, V3, 0, m1, 0, p2, M6, 0, m1, 0, p2, E, ff);
     564           0 :   if (gc_needed(av, 1))
     565           0 :     gerepileall(av, 6, &M2, &M3, &C11, &V1, &M7, &C12);  /* destroy V3, M6 */
     566           0 :   V2 = add_slices(m2, p1, V1, 0, m2, 0, p1, M2, 0, m2, 0, p2, E, ff);
     567           0 :   if (gc_needed(av, 1))
     568           0 :     gerepileall(av, 5, &M3, &C11, &M7, &C12, &V2);  /* destroy V1, M2 */
     569           0 :   C21 = add_slices(m2, p1, V2, 0, m2, 0, p1, M7, 0, m2, 0, p1, E, ff);
     570           0 :   if (gc_needed(av, 1))
     571           0 :     gerepileall(av, 5, &M3, &C11, &C12, &V2, &C21);  /* destroy M7 */
     572           0 :   C22 = add_slices(m2, p2, V2, 0, m2, 0, p2, M3, 0, m2, 0, p2, E, ff);
     573           0 :   if (gc_needed(av, 1))
     574           0 :     gerepileall(av, 4, &C11, &C12, &C21, &C22);  /* destroy V2, M3 */
     575           0 :   C = mkmat2(mkcol2(C11, C21), mkcol2(C12, C22));
     576           0 :   return gerepileupto(av, matconcat(C));
     577             : }
     578             : 
     579             : /* Strassen-Winograd used for dim >= gen_matmul_sw_bound */
     580             : static const long gen_matmul_sw_bound = 24;
     581             : 
     582             : static GEN
     583       75409 : gen_matmul_i(GEN A, GEN B, long l, long la, long lb,
     584             :              void *E, const struct bb_field *ff)
     585             : {
     586       75409 :   if (l <= gen_matmul_sw_bound
     587           7 :       || la <= gen_matmul_sw_bound
     588           0 :       || lb <= gen_matmul_sw_bound)
     589       75409 :     return gen_matmul_classical(A, B, l, la, lb, E, ff);
     590             :   else
     591           0 :     return gen_matmul_sw(A, B, l - 1, la - 1, lb - 1, E, ff);
     592             : }
     593             : 
     594             : GEN
     595       75409 : gen_matmul(GEN A, GEN B, void *E, const struct bb_field *ff)
     596             : {
     597       75409 :   ulong lgA, lgB = lg(B);
     598       75409 :   if (lgB == 1)
     599           0 :     return cgetg(1, t_MAT);
     600       75409 :   lgA = lg(A);
     601       75409 :   if (lgA != (ulong)lgcols(B))
     602           0 :     pari_err_OP("operation 'gen_matmul'", A, B);
     603       75409 :   if (lgA == 1)
     604           0 :     return zeromat(0, lgB - 1);
     605       75409 :   return gen_matmul_i(A, B, lgcols(A), lgA, lgB, E, ff);
     606             : }
     607             : 
     608             : static GEN
     609       17398 : gen_colneg(GEN A, void *E, const struct bb_field *ff)
     610             : {
     611             :   long i, l;
     612       17398 :   GEN B = cgetg_copy(A, &l);
     613       69620 :   for (i = 1; i < l; i++)
     614       52222 :     gel(B, i) = ff->neg(E, gel(A, i));
     615       17398 :   return B;
     616             : }
     617             : 
     618             : static GEN
     619        3748 : gen_matneg(GEN A, void *E, const struct bb_field *ff)
     620             : {
     621             :   long i, l;
     622        3748 :   GEN B = cgetg_copy(A, &l);
     623       21090 :   for (i = 1; i < l; i++)
     624       17342 :     gel(B, i) = gen_colneg(gel(A, i), E, ff);
     625        3748 :   return B;
     626             : }
     627             : 
     628             : static GEN
     629      233775 : gen_colscalmul(GEN A, GEN b, void *E, const struct bb_field *ff)
     630             : {
     631             :   long i, l;
     632      233775 :   GEN B = cgetg_copy(A, &l);
     633      570838 :   for (i = 1; i < l; i++)
     634      337063 :     gel(B, i) = ff->red(E, ff->mul(E, gel(A, i), b));
     635      233775 :   return B;
     636             : }
     637             : 
     638             : static GEN
     639       46927 : gen_matscalmul(GEN A, GEN b, void *E, const struct bb_field *ff)
     640             : {
     641             :   long i, l;
     642       46927 :   GEN B = cgetg_copy(A, &l);
     643      280702 :   for (i = 1; i < l; i++)
     644      233775 :     gel(B, i) = gen_colscalmul(gel(A, i), b, E, ff);
     645       46927 :   return B;
     646             : }
     647             : 
     648             : static GEN
     649      435173 : gen_colsub(GEN A, GEN C, void *E, const struct bb_field *ff)
     650             : {
     651             :   long i, l;
     652      435173 :   GEN B = cgetg_copy(A, &l);
     653     1600614 :   for (i = 1; i < l; i++)
     654     1165441 :     gel(B, i) = ff->add(E, gel(A, i), ff->neg(E, gel(C, i)));
     655      435173 :   return B;
     656             : }
     657             : 
     658             : static GEN
     659       66853 : gen_matsub(GEN A, GEN C, void *E, const struct bb_field *ff)
     660             : {
     661             :   long i, l;
     662       66853 :   GEN B = cgetg_copy(A, &l);
     663      502026 :   for (i = 1; i < l; i++)
     664      435173 :     gel(B, i) = gen_colsub(gel(A, i), gel(C, i), E, ff);
     665       66853 :   return B;
     666             : }
     667             : 
     668             : static GEN
     669       41321 : gen_zerocol(long n, void* data, const struct bb_field *R)
     670             : {
     671       41321 :   GEN C = cgetg(n+1,t_COL), zero = R->s(data, 0);
     672             :   long i;
     673       41321 :   for (i=1; i<=n; i++) gel(C,i) = zero;
     674       41321 :   return C;
     675             : }
     676             : 
     677             : static GEN
     678       13136 : gen_zeromat(long m, long n, void* data, const struct bb_field *R)
     679             : {
     680       13136 :   GEN M = cgetg(n+1,t_MAT);
     681             :   long i;
     682       13136 :   for (i=1; i<=n; i++) gel(M,i) = gen_zerocol(m, data, R);
     683       13136 :   return M;
     684             : }
     685             : 
     686             : static GEN
     687         140 : gen_colei(long n, long i, void *E, const struct bb_field *S)
     688             : {
     689         140 :   GEN y = cgetg(n+1,t_COL), _0, _1;
     690             :   long j;
     691         140 :   if (n < 0) pari_err_DOMAIN("gen_colei", "dimension","<",gen_0,stoi(n));
     692         140 :   _0 = S->s(E,0);
     693         140 :   _1 = S->s(E,1);
     694        2268 :   for (j=1; j<=n; j++)
     695        2128 :     gel(y, j) = i==j ? _1: _0;
     696         140 :   return y;
     697             : }
     698             : 
     699             : /* assume dim A >= 1, A invertible + upper triangular  */
     700             : static GEN
     701          77 : gen_matinv_upper_ind(GEN A, long index, void *E, const struct bb_field *ff)
     702             : {
     703          77 :   long n = lg(A) - 1, i, j;
     704          77 :   GEN u = cgetg(n + 1, t_COL);
     705         147 :   for (i = n; i > index; i--)
     706          70 :     gel(u, i) = ff->s(E, 0);
     707          77 :   gel(u, i) = ff->inv(E, gcoeff(A, i, i));
     708         147 :   for (i--; i > 0; i--) {
     709          70 :     pari_sp av = avma;
     710          70 :     GEN m = ff->neg(E, ff->mul(E, gcoeff(A, i, i + 1), gel(u, i + 1)));
     711         112 :     for (j = i + 2; j <= n; j++)
     712          42 :       m = ff->add(E, m, ff->neg(E, ff->mul(E, gcoeff(A, i, j), gel(u, j))));
     713          70 :     gel(u, i) = gerepileupto(av, ff->red(E, ff->mul(E, m, ff->inv(E, gcoeff(A, i, i)))));
     714             :   }
     715          77 :   return u;
     716             : }
     717             : 
     718             : static GEN
     719          28 : gen_matinv_upper(GEN A, void *E, const struct bb_field *ff)
     720             : {
     721             :   long i, l;
     722          28 :   GEN B = cgetg_copy(A, &l);
     723         105 :   for (i = 1; i < l; i++)
     724          77 :     gel(B,i) = gen_matinv_upper_ind(A, i, E, ff);
     725          28 :   return B;
     726             : }
     727             : 
     728             : /* find z such that A z = y. Return NULL if no solution */
     729             : GEN
     730           0 : gen_matcolinvimage(GEN A, GEN y, void *E, const struct bb_field *ff)
     731             : {
     732           0 :   pari_sp av = avma;
     733           0 :   long i, l = lg(A);
     734             :   GEN M, x, t;
     735             : 
     736           0 :   M = gen_ker(shallowconcat(A, y), 0, E, ff);
     737           0 :   i = lg(M) - 1;
     738           0 :   if (!i) return gc_NULL(av);
     739             : 
     740           0 :   x = gel(M, i);
     741           0 :   t = gel(x, l);
     742           0 :   if (ff->equal0(t)) return gc_NULL(av);
     743             : 
     744           0 :   t = ff->neg(E, ff->inv(E, t));
     745           0 :   setlg(x, l);
     746           0 :   for (i = 1; i < l; i++)
     747           0 :     gel(x, i) = ff->red(E, ff->mul(E, t, gel(x, i)));
     748           0 :   return gerepilecopy(av, x);
     749             : }
     750             : 
     751             : /* find Z such that A Z = B. Return NULL if no solution */
     752             : GEN
     753          77 : gen_matinvimage(GEN A, GEN B, void *E, const struct bb_field *ff)
     754             : {
     755          77 :   pari_sp av = avma;
     756             :   GEN d, x, X, Y;
     757             :   long i, j, nY, nA, nB;
     758          77 :   x = gen_ker(shallowconcat(gen_matneg(A, E, ff), B), 0, E, ff);
     759             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
     760             :    * We must find T such that Y T = Id_nB then X T = Z. This exists
     761             :    * iff Y has at least nB columns and full rank. */
     762          77 :   nY = lg(x) - 1;
     763          77 :   nB = lg(B) - 1;
     764          77 :   if (nY < nB) return gc_NULL(av);
     765          77 :   nA = lg(A) - 1;
     766          77 :   Y = rowslice(x, nA + 1, nA + nB); /* nB rows */
     767          77 :   d = cgetg(nB + 1, t_VECSMALL);
     768         182 :   for (i = nB, j = nY; i >= 1; i--, j--) {
     769         224 :     for (; j >= 1; j--)
     770         175 :       if (!ff->equal0(gcoeff(Y, i, j))) { d[i] = j; break; }
     771         154 :     if (!j) return gc_NULL(av);
     772             :   }
     773             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
     774          28 :   Y = vecpermute(Y, d);
     775          28 :   x = vecpermute(x, d);
     776          28 :   X = rowslice(x, 1, nA);
     777          28 :   return gerepileupto(av, gen_matmul(X, gen_matinv_upper(Y, E, ff), E, ff));
     778             : }
     779             : 
     780             : static GEN
     781       94910 : image_from_pivot(GEN x, GEN d, long r)
     782             : {
     783             :   GEN y;
     784             :   long j, k;
     785             : 
     786       94910 :   if (!d) return gcopy(x);
     787             :   /* d left on stack for efficiency */
     788       92887 :   r = lg(x)-1 - r; /* = dim Im(x) */
     789       92887 :   y = cgetg(r+1,t_MAT);
     790      848687 :   for (j=k=1; j<=r; k++)
     791      755800 :     if (d[k]) gel(y,j++) = gcopy(gel(x,k));
     792       92887 :   return y;
     793             : }
     794             : 
     795             : /* r = dim Ker x, n = nbrows(x) */
     796             : static GEN
     797       44467 : get_suppl(GEN x, GEN d, long n, long r, GEN(*ei)(long,long))
     798             : {
     799             :   pari_sp av;
     800             :   GEN y, c;
     801       44467 :   long j, k, rx = lg(x)-1; /* != 0 due to init_suppl() */
     802             : 
     803       44467 :   if (rx == n && r == 0) return gcopy(x);
     804       41165 :   y = cgetg(n+1, t_MAT);
     805       41165 :   av = avma; c = zero_zv(n);
     806             :   /* c = lines containing pivots (could get it from gauss_pivot, but cheap)
     807             :    * In theory r = 0 and d[j] > 0 for all j, but why take chances? */
     808      319320 :   for (k = j = 1; j<=rx; j++)
     809      278155 :     if (d[j]) { c[ d[j] ] = 1; gel(y,k++) = gel(x,j); }
     810      424761 :   for (j=1; j<=n; j++)
     811      383596 :     if (!c[j]) gel(y,k++) = (GEN)j; /* HACK */
     812       41165 :   set_avma(av);
     813             : 
     814       41165 :   rx -= r;
     815       41165 :   for (j=1; j<=rx; j++) gel(y,j) = gcopy(gel(y,j));
     816       41165 :   for (   ; j<=n; j++)  gel(y,j) = ei(n, y[j]);
     817       41165 :   return y;
     818             : }
     819             : 
     820             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
     821             : static GEN
     822       97081 : indexrank0(long n, long r, GEN d)
     823             : {
     824       97081 :   GEN p1, p2, res = cgetg(3,t_VEC);
     825             :   long i, j;
     826             : 
     827       97081 :   r = n - r; /* now r = dim Im(x) */
     828       97081 :   p1 = cgetg(r+1,t_VECSMALL); gel(res,1) = p1;
     829       97081 :   p2 = cgetg(r+1,t_VECSMALL); gel(res,2) = p2;
     830       97081 :   if (d)
     831             :   {
     832      542368 :     for (i=0,j=1; j<=n; j++)
     833      446197 :       if (d[j]) { i++; p1[i] = d[j]; p2[i] = j; }
     834       96171 :     vecsmall_sort(p1);
     835             :   }
     836       97081 :   return res;
     837             : }
     838             : 
     839             : /*******************************************************************/
     840             : /*                                                                 */
     841             : /*                Echelon form and CUP decomposition               */
     842             : /*                                                                 */
     843             : /*******************************************************************/
     844             : 
     845             : /* By Peter Bruin, based on
     846             :   C.-P. Jeannerod, C. Pernet and A. Storjohann, Rank-profile revealing
     847             :   Gaussian elimination and the CUP matrix decomposition.  J. Symbolic
     848             :   Comput. 56 (2013), 46-68.
     849             : 
     850             :   Decompose an m x n-matrix A of rank r as C*U*P, with
     851             :   - C: m x r-matrix in column echelon form (not necessarily reduced)
     852             :        with all pivots equal to 1
     853             :   - U: upper-triangular r x n-matrix
     854             :   - P: permutation matrix
     855             :   The pivots of C and the known zeroes in C and U are not necessarily
     856             :   filled in; instead, we also return the vector R of pivot rows.
     857             :   Instead of the matrix P, we return the permutation p of [1..n]
     858             :   (t_VECSMALL) such that P[i,j] = 1 if and only if j = p[i].
     859             : */
     860             : 
     861             : /* complement of a strictly increasing subsequence of (1, 2, ..., n) */
     862             : static GEN
     863       13003 : indexcompl(GEN v, long n)
     864             : {
     865       13003 :   long i, j, k, m = lg(v) - 1;
     866       13003 :   GEN w = cgetg(n - m + 1, t_VECSMALL);
     867      130239 :   for (i = j = k = 1; i <= n; i++)
     868      117236 :     if (j <= m && v[j] == i) j++; else w[k++] = i;
     869       13003 :   return w;
     870             : }
     871             : 
     872             : static GEN
     873        3692 : gen_solve_upper_1(GEN U, GEN B, void *E, const struct bb_field *ff)
     874        3692 : { return gen_matscalmul(B, ff->inv(E, gcoeff(U, 1, 1)), E, ff); }
     875             : 
     876             : static GEN
     877        1976 : gen_rsolve_upper_2(GEN U, GEN B, void *E, const struct bb_field *ff)
     878             : {
     879        1976 :   GEN a = gcoeff(U, 1, 1), b = gcoeff(U, 1, 2), d = gcoeff(U, 2, 2);
     880        1976 :   GEN D = ff->red(E, ff->mul(E, a, d)), Dinv = ff->inv(E, D);
     881        1976 :   GEN ainv = ff->red(E, ff->mul(E, d, Dinv));
     882        1976 :   GEN dinv = ff->red(E, ff->mul(E, a, Dinv));
     883        1976 :   GEN B1 = rowslice(B, 1, 1);
     884        1976 :   GEN B2 = rowslice(B, 2, 2);
     885        1976 :   GEN X2 = gen_matscalmul(B2, dinv, E, ff);
     886        1976 :   GEN X1 = gen_matscalmul(gen_matsub(B1, gen_matscalmul(X2, b, E, ff), E, ff),
     887             :                           ainv, E, ff);
     888        1976 :   return vconcat(X1, X2);
     889             : }
     890             : 
     891             : /* solve U*X = B,  U upper triangular and invertible */
     892             : static GEN
     893        5230 : gen_rsolve_upper(GEN U, GEN B, void *E, const struct bb_field *ff,
     894             :                  GEN (*mul)(void *E, GEN a, GEN))
     895             : {
     896        5230 :   long n = lg(U) - 1, n1;
     897             :   GEN U2, U11, U12, U22, B1, B2, X1, X2, X;
     898        5230 :   pari_sp av = avma;
     899             : 
     900        5230 :   if (n == 0) return B;
     901        5230 :   if (n == 1) return gen_solve_upper_1(U, B, E, ff);
     902        4350 :   if (n == 2) return gen_rsolve_upper_2(U, B, E, ff);
     903        2374 :   n1 = (n + 1)/2;
     904        2374 :   U2 = vecslice(U, n1 + 1, n);
     905        2374 :   U11 = matslice(U, 1,n1, 1,n1);
     906        2374 :   U12 = rowslice(U2, 1, n1);
     907        2374 :   U22 = rowslice(U2, n1 + 1, n);
     908        2374 :   B1 = rowslice(B, 1, n1);
     909        2374 :   B2 = rowslice(B, n1 + 1, n);
     910        2374 :   X2 = gen_rsolve_upper(U22, B2, E, ff, mul);
     911        2374 :   B1 = gen_matsub(B1, mul(E, U12, X2), E, ff);
     912        2374 :   if (gc_needed(av, 1)) gerepileall(av, 3, &B1, &U11, &X2);
     913        2374 :   X1 = gen_rsolve_upper(U11, B1, E, ff, mul);
     914        2374 :   X = vconcat(X1, X2);
     915        2374 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     916        2374 :   return X;
     917             : }
     918             : 
     919             : static GEN
     920        5482 : gen_lsolve_upper_2(GEN U, GEN B, void *E, const struct bb_field *ff)
     921             : {
     922        5482 :   GEN a = gcoeff(U, 1, 1), b = gcoeff(U, 1, 2), d = gcoeff(U, 2, 2);
     923        5482 :   GEN D = ff->red(E, ff->mul(E, a, d)), Dinv = ff->inv(E, D);
     924        5482 :   GEN ainv = ff->red(E, ff->mul(E, d, Dinv)), dinv = ff->red(E, ff->mul(E, a, Dinv));
     925        5482 :   GEN B1 = vecslice(B, 1, 1);
     926        5482 :   GEN B2 = vecslice(B, 2, 2);
     927        5482 :   GEN X1 = gen_matscalmul(B1, ainv, E, ff);
     928        5482 :   GEN X2 = gen_matscalmul(gen_matsub(B2, gen_matscalmul(X1, b, E, ff), E, ff), dinv, E, ff);
     929        5482 :   return shallowconcat(X1, X2);
     930             : }
     931             : 
     932             : /* solve X*U = B,  U upper triangular and invertible */
     933             : static GEN
     934       12694 : gen_lsolve_upper(GEN U, GEN B, void *E, const struct bb_field *ff,
     935             :                  GEN (*mul)(void *E, GEN a, GEN))
     936             : {
     937       12694 :   long n = lg(U) - 1, n1;
     938             :   GEN U2, U11, U12, U22, B1, B2, X1, X2, X;
     939       12694 :   pari_sp av = avma;
     940             : 
     941       12694 :   if (n == 0) return B;
     942       12694 :   if (n == 1) return gen_solve_upper_1(U, B, E, ff);
     943        9882 :   if (n == 2) return gen_lsolve_upper_2(U, B, E, ff);
     944        4400 :   n1 = (n + 1)/2;
     945        4400 :   U2 = vecslice(U, n1 + 1, n);
     946        4400 :   U11 = matslice(U, 1,n1, 1,n1);
     947        4400 :   U12 = rowslice(U2, 1, n1);
     948        4400 :   U22 = rowslice(U2, n1 + 1, n);
     949        4400 :   B1 = vecslice(B, 1, n1);
     950        4400 :   B2 = vecslice(B, n1 + 1, n);
     951        4400 :   X1 = gen_lsolve_upper(U11, B1, E, ff, mul);
     952        4400 :   B2 = gen_matsub(B2, mul(E, X1, U12), E, ff);
     953        4400 :   if (gc_needed(av, 1)) gerepileall(av, 3, &B2, &U22, &X1);
     954        4400 :   X2 = gen_lsolve_upper(U22, B2, E, ff, mul);
     955        4400 :   X = shallowconcat(X1, X2);
     956        4400 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     957        4400 :   return X;
     958             : }
     959             : 
     960             : static GEN
     961       13949 : gen_rsolve_lower_unit_2(GEN L, GEN A, void *E, const struct bb_field *ff)
     962             : {
     963       13949 :   GEN X1 = rowslice(A, 1, 1);
     964       13949 :   GEN X2 = gen_matsub(rowslice(A, 2, 2), gen_matscalmul(X1, gcoeff(L, 2, 1), E, ff), E, ff);
     965       13949 :   return vconcat(X1, X2);
     966             : }
     967             : 
     968             : /* solve L*X = A,  L lower triangular with ones on the diagonal
     969             :  * (at least as many rows as columns) */
     970             : static GEN
     971       32657 : gen_rsolve_lower_unit(GEN L, GEN A, void *E, const struct bb_field *ff,
     972             :                       GEN (*mul)(void *E, GEN a, GEN))
     973             : {
     974       32657 :   long m = lg(L) - 1, m1, n;
     975             :   GEN L1, L11, L21, L22, A1, A2, X1, X2, X;
     976       32657 :   pari_sp av = avma;
     977             : 
     978       32657 :   if (m == 0) return zeromat(0, lg(A) - 1);
     979       32657 :   if (m == 1) return rowslice(A, 1, 1);
     980       25409 :   if (m == 2) return gen_rsolve_lower_unit_2(L, A, E, ff);
     981       11460 :   m1 = (m + 1)/2;
     982       11460 :   n = nbrows(L);
     983       11460 :   L1 = vecslice(L, 1, m1);
     984       11460 :   L11 = rowslice(L1, 1, m1);
     985       11460 :   L21 = rowslice(L1, m1 + 1, n);
     986       11460 :   A1 = rowslice(A, 1, m1);
     987       11460 :   X1 = gen_rsolve_lower_unit(L11, A1, E, ff, mul);
     988       11460 :   A2 = rowslice(A, m1 + 1, n);
     989       11460 :   A2 = gen_matsub(A2, mul(E, L21, X1), E, ff);
     990       11460 :   if (gc_needed(av, 1)) gerepileall(av, 2, &A2, &X1);
     991       11460 :   L22 = matslice(L, m1+1,n, m1+1,m);
     992       11460 :   X2 = gen_rsolve_lower_unit(L22, A2, E, ff, mul);
     993       11460 :   X = vconcat(X1, X2);
     994       11460 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     995       11460 :   return X;
     996             : }
     997             : 
     998             : static GEN
     999        6912 : gen_lsolve_lower_unit_2(GEN L, GEN A, void *E, const struct bb_field *ff)
    1000             : {
    1001        6912 :   GEN X2 = vecslice(A, 2, 2);
    1002        6912 :   GEN X1 = gen_matsub(vecslice(A, 1, 1),
    1003        6912 :                     gen_matscalmul(X2, gcoeff(L, 2, 1), E, ff), E, ff);
    1004        6912 :   return shallowconcat(X1, X2);
    1005             : }
    1006             : 
    1007             : /* solve L*X = A,  L lower triangular with ones on the diagonal
    1008             :  * (at least as many rows as columns) */
    1009             : static GEN
    1010       18113 : gen_lsolve_lower_unit(GEN L, GEN A, void *E, const struct bb_field *ff,
    1011             :                       GEN (*mul)(void *E, GEN a, GEN))
    1012             : {
    1013       18113 :   long m = lg(L) - 1, m1;
    1014             :   GEN L1, L2, L11, L21, L22, A1, A2, X1, X2, X;
    1015       18113 :   pari_sp av = avma;
    1016             : 
    1017       18113 :   if (m <= 1) return A;
    1018       14105 :   if (m == 2) return gen_lsolve_lower_unit_2(L, A, E, ff);
    1019        7193 :   m1 = (m + 1)/2;
    1020        7193 :   L2 = vecslice(L, m1 + 1, m);
    1021        7193 :   L22 = rowslice(L2, m1 + 1, m);
    1022        7193 :   A2 = vecslice(A, m1 + 1, m);
    1023        7193 :   X2 = gen_lsolve_lower_unit(L22, A2, E, ff, mul);
    1024        7193 :   if (gc_needed(av, 1)) X2 = gerepilecopy(av, X2);
    1025        7193 :   L1 = vecslice(L, 1, m1);
    1026        7193 :   L21 = rowslice(L1, m1 + 1, m);
    1027        7193 :   A1 = vecslice(A, 1, m1);
    1028        7193 :   A1 = gen_matsub(A1, mul(E, X2, L21), E, ff);
    1029        7193 :   L11 = rowslice(L1, 1, m1);
    1030        7193 :   if (gc_needed(av, 1)) gerepileall(av, 3, &A1, &L11, &X2);
    1031        7193 :   X1 = gen_lsolve_lower_unit(L11, A1, E, ff, mul);
    1032        7193 :   X = shallowconcat(X1, X2);
    1033        7193 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
    1034        7193 :   return X;
    1035             : }
    1036             : 
    1037             : /* destroy A */
    1038             : static long
    1039       19812 : gen_CUP_basecase(GEN A, GEN *R, GEN *C, GEN *U, GEN *P, void *E, const struct bb_field *ff)
    1040             : {
    1041       19812 :   long i, j, k, m = nbrows(A), n = lg(A) - 1, pr, pc;
    1042             :   pari_sp av;
    1043             :   GEN u, v;
    1044             : 
    1045       19812 :   if (P) *P = identity_perm(n);
    1046       19812 :   *R = cgetg(m + 1, t_VECSMALL);
    1047       19812 :   av = avma;
    1048       51790 :   for (j = 1, pr = 0; j <= n; j++)
    1049             :   {
    1050      118331 :     for (pr++, pc = 0; pr <= m; pr++)
    1051             :     {
    1052      543841 :       for (k = j; k <= n; k++)
    1053             :       {
    1054      441027 :         v = ff->red(E, gcoeff(A, pr, k));
    1055      441027 :         gcoeff(A, pr, k) = v;
    1056      441027 :         if (!pc && !ff->equal0(v)) pc = k;
    1057             :       }
    1058      102814 :       if (pc) break;
    1059             :     }
    1060       47495 :     if (!pc) break;
    1061       31978 :     (*R)[j] = pr;
    1062       31978 :     if (pc != j)
    1063             :     {
    1064        4063 :       swap(gel(A, j), gel(A, pc));
    1065        4063 :       if (P) lswap((*P)[j], (*P)[pc]);
    1066             :     }
    1067       31978 :     u = ff->inv(E, gcoeff(A, pr, j));
    1068      151817 :     for (i = pr + 1; i <= m; i++)
    1069             :     {
    1070      119839 :       v = ff->red(E, ff->mul(E, gcoeff(A, i, j), u));
    1071      119839 :       gcoeff(A, i, j) = v;
    1072      119839 :       v = ff->neg(E, v);
    1073      385252 :       for (k = j + 1; k <= n; k++)
    1074      530826 :         gcoeff(A, i, k) = ff->add(E, gcoeff(A, i, k),
    1075      265413 :                                   ff->red(E, ff->mul(E, gcoeff(A, pr, k), v)));
    1076             :     }
    1077       31978 :     if (gc_needed(av, 2)) A = gerepilecopy(av, A);
    1078             :   }
    1079       19812 :   setlg(*R, j);
    1080       19812 :   *C = vecslice(A, 1, j - 1);
    1081       19812 :   if (U) *U = rowpermute(A, *R);
    1082       19812 :   return j - 1;
    1083             : }
    1084             : 
    1085             : static const long gen_CUP_LIMIT = 5;
    1086             : 
    1087             : static long
    1088       10087 : gen_CUP(GEN A, GEN *R, GEN *C, GEN *U, GEN *P, void *E, const struct bb_field *ff,
    1089             :         GEN (*mul)(void *E, GEN a, GEN))
    1090             : {
    1091       10087 :   long m = nbrows(A), m1, n = lg(A) - 1, i, r1, r2, r;
    1092             :   GEN R1, C1, U1, P1, R2, C2, U2, P2;
    1093             :   GEN A1, A2, B2, C21, U11, U12, T21, T22;
    1094       10087 :   pari_sp av = avma;
    1095             : 
    1096       10087 :   if (m < gen_CUP_LIMIT || n < gen_CUP_LIMIT)
    1097             :     /* destroy A; not called at the outermost recursion level */
    1098        5779 :     return gen_CUP_basecase(A, R, C, U, P, E, ff);
    1099        4308 :   m1 = (minss(m, n) + 1)/2;
    1100        4308 :   A1 = rowslice(A, 1, m1);
    1101        4308 :   A2 = rowslice(A, m1 + 1, m);
    1102        4308 :   r1 = gen_CUP(A1, &R1, &C1, &U1, &P1, E, ff, mul);
    1103        4308 :   if (r1 == 0)
    1104             :   {
    1105         414 :     r2 = gen_CUP(A2, &R2, &C2, &U2, &P2, E, ff, mul);
    1106         414 :     *R = cgetg(r2 + 1, t_VECSMALL);
    1107         414 :     for (i = 1; i <= r2; i++) (*R)[i] = R2[i] + m1;
    1108         414 :     *C = vconcat(gen_zeromat(m1, r2, E, ff), C2);
    1109         414 :     *U = U2;
    1110         414 :     *P = P2;
    1111         414 :     r = r2;
    1112             :   }
    1113             :   else
    1114             :   {
    1115        3894 :     U11 = vecslice(U1, 1, r1);
    1116        3894 :     U12 = vecslice(U1, r1 + 1, n);
    1117        3894 :     T21 = vecslicepermute(A2, P1, 1, r1);
    1118        3894 :     T22 = vecslicepermute(A2, P1, r1 + 1, n);
    1119        3894 :     C21 = gen_lsolve_upper(U11, T21, E, ff, mul);
    1120        3894 :     if (gc_needed(av, 1))
    1121           0 :       gerepileall(av, 7, &R1, &C1, &P1, &U11, &U12, &T22, &C21);
    1122        3894 :     B2 = gen_matsub(T22, mul(E, C21, U12), E, ff);
    1123        3894 :     r2 = gen_CUP(B2, &R2, &C2, &U2, &P2, E, ff, mul);
    1124        3894 :     r = r1 + r2;
    1125        3894 :     *R = cgetg(r + 1, t_VECSMALL);
    1126        3894 :     for (i = 1; i <= r1; i++) (*R)[i] = R1[i];
    1127        3894 :     for (     ; i <= r; i++)  (*R)[i] = R2[i - r1] + m1;
    1128        3894 :     *C = shallowconcat(vconcat(C1, C21),
    1129             :                        vconcat(gen_zeromat(m1, r2, E, ff), C2));
    1130        3894 :     *U = shallowconcat(vconcat(U11, gen_zeromat(r2, r1, E, ff)),
    1131             :                        vconcat(vecpermute(U12, P2), U2));
    1132             : 
    1133        3894 :     *P = cgetg(n + 1, t_VECSMALL);
    1134        3894 :     for (i = 1; i <= r1; i++) (*P)[i] = P1[i];
    1135        3894 :     for (     ; i <= n; i++)  (*P)[i] = P1[P2[i - r1] + r1];
    1136             :   }
    1137        4308 :   if (gc_needed(av, 1)) gerepileall(av, 4, R, C, U, P);
    1138        4308 :   return r;
    1139             : }
    1140             : 
    1141             : /* column echelon form */
    1142             : static long
    1143       24413 : gen_echelon(GEN A, GEN *R, GEN *C, void *E, const struct bb_field *ff,
    1144             :             GEN (*mul)(void*, GEN, GEN))
    1145             : {
    1146       24413 :   long j, j1, j2, m = nbrows(A), n = lg(A) - 1, n1, r, r1, r2;
    1147             :   GEN A1, A2, R1, R1c, C1, R2, C2;
    1148             :   GEN A12, A22, B2, C11, C21, M12;
    1149       24413 :   pari_sp av = avma;
    1150             : 
    1151       24413 :   if (m < gen_CUP_LIMIT || n < gen_CUP_LIMIT)
    1152       14033 :     return gen_CUP_basecase(shallowcopy(A), R, C, NULL, NULL, E, ff);
    1153             : 
    1154       10380 :   n1 = (n + 1)/2;
    1155       10380 :   A1 = vecslice(A, 1, n1);
    1156       10380 :   A2 = vecslice(A, n1 + 1, n);
    1157       10380 :   r1 = gen_echelon(A1, &R1, &C1, E, ff, mul);
    1158       10380 :   if (!r1) return gen_echelon(A2, R, C, E, ff, mul);
    1159        9336 :   if (r1 == m) { *R = R1; *C = C1; return r1; }
    1160        9213 :   R1c = indexcompl(R1, m);
    1161        9213 :   C11 = rowpermute(C1, R1);
    1162        9213 :   C21 = rowpermute(C1, R1c);
    1163        9213 :   A12 = rowpermute(A2, R1);
    1164        9213 :   A22 = rowpermute(A2, R1c);
    1165        9213 :   M12 = gen_rsolve_lower_unit(C11, A12, E, ff, mul);
    1166        9213 :   B2 = gen_matsub(A22, mul(E, C21, M12), E, ff);
    1167        9213 :   r2 = gen_echelon(B2, &R2, &C2, E, ff, mul);
    1168        9213 :   if (!r2) { *R = R1; *C = C1; r = r1; }
    1169             :   else
    1170             :   {
    1171        4913 :     R2 = perm_mul(R1c, R2);
    1172        4913 :     C2 = rowpermute(vconcat(gen_zeromat(r1, r2, E, ff), C2),
    1173             :                     perm_inv(vecsmall_concat(R1, R1c)));
    1174        4913 :     r = r1 + r2;
    1175        4913 :     *R = cgetg(r + 1, t_VECSMALL);
    1176        4913 :     *C = cgetg(r + 1, t_MAT);
    1177       33342 :     for (j = j1 = j2 = 1; j <= r; j++)
    1178       28429 :       if (j2 > r2 || (j1 <= r1 && R1[j1] < R2[j2]))
    1179             :       {
    1180       16647 :         gel(*C, j) = gel(C1, j1);
    1181       16647 :         (*R)[j] = R1[j1++];
    1182             :       }
    1183             :       else
    1184             :       {
    1185       11782 :         gel(*C, j) = gel(C2, j2);
    1186       11782 :         (*R)[j] = R2[j2++];
    1187             :       }
    1188             :   }
    1189        9213 :   if (gc_needed(av, 1)) gerepileall(av, 2, R, C);
    1190        9213 :   return r;
    1191             : }
    1192             : 
    1193             : static GEN
    1194         751 : gen_pivots_CUP(GEN x, long *rr, void *E, const struct bb_field *ff,
    1195             :                GEN (*mul)(void*, GEN, GEN))
    1196             : {
    1197             :   pari_sp av;
    1198         751 :   long i, n = lg(x) - 1, r;
    1199         751 :   GEN R, C, U, P, d = zero_zv(n);
    1200         751 :   av = avma;
    1201         751 :   r = gen_CUP(x, &R, &C, &U, &P, E, ff, mul);
    1202        5725 :   for(i = 1; i <= r; i++)
    1203        4974 :     d[P[i]] = R[i];
    1204         751 :   set_avma(av);
    1205         751 :   *rr = n - r;
    1206         751 :   return d;
    1207             : }
    1208             : 
    1209             : static GEN
    1210         140 : gen_det_CUP(GEN a, void *E, const struct bb_field *ff,
    1211             :             GEN (*mul)(void*, GEN, GEN))
    1212             : {
    1213         140 :   pari_sp av = avma;
    1214             :   GEN R, C, U, P, d;
    1215         140 :   long i, n = lg(a) - 1, r;
    1216         140 :   r = gen_CUP(a, &R, &C, &U, &P, E, ff, mul);
    1217         140 :   if (r < n)
    1218           0 :     d = ff->s(E, 0);
    1219             :   else {
    1220         140 :     d = ff->s(E, perm_sign(P) == 1 ? 1: - 1);
    1221        2730 :     for (i = 1; i <= n; i++)
    1222        2590 :       d = ff->red(E, ff->mul(E, d, gcoeff(U, i, i)));
    1223             :   }
    1224         140 :   return gerepileupto(av, d);
    1225             : }
    1226             : 
    1227             : static long
    1228          28 : gen_matrank(GEN x, void *E, const struct bb_field *ff,
    1229             :             GEN (*mul)(void*, GEN, GEN))
    1230             : {
    1231          28 :   pari_sp av = avma;
    1232             :   long r;
    1233          28 :   if (lg(x) - 1 >= gen_CUP_LIMIT && nbrows(x) >= gen_CUP_LIMIT)
    1234             :   {
    1235             :     GEN R, C;
    1236          21 :     return gc_long(av, gen_echelon(x, &R, &C, E, ff, mul));
    1237             :   }
    1238           7 :   (void) gen_Gauss_pivot(x, &r, E, ff);
    1239           7 :   return gc_long(av, lg(x)-1 - r);
    1240             : }
    1241             : 
    1242             : static GEN
    1243          63 : gen_invimage_CUP(GEN A, GEN B, void *E, const struct bb_field *ff,
    1244             :                  GEN (*mul)(void*, GEN, GEN))
    1245             : {
    1246          63 :   pari_sp av = avma;
    1247             :   GEN R, Rc, C, U, P, B1, B2, C1, C2, X, Y, Z;
    1248          63 :   long r = gen_CUP(A, &R, &C, &U, &P, E, ff, mul);
    1249          63 :   Rc = indexcompl(R, nbrows(B));
    1250          63 :   C1 = rowpermute(C, R);
    1251          63 :   C2 = rowpermute(C, Rc);
    1252          63 :   B1 = rowpermute(B, R);
    1253          63 :   B2 = rowpermute(B, Rc);
    1254          63 :   Z = gen_rsolve_lower_unit(C1, B1, E, ff, mul);
    1255          63 :   if (!gequal(mul(E, C2, Z), B2))
    1256          42 :     return NULL;
    1257          42 :   Y = vconcat(gen_rsolve_upper(vecslice(U, 1, r), Z, E, ff, mul),
    1258          42 :               gen_zeromat(lg(A) - 1 - r, lg(B) - 1, E, ff));
    1259          21 :   X = rowpermute(Y, perm_inv(P));
    1260          21 :   return gerepilecopy(av, X);
    1261             : }
    1262             : 
    1263             : static GEN
    1264        3671 : gen_ker_echelon(GEN x, void *E, const struct bb_field *ff,
    1265             :                 GEN (*mul)(void*, GEN, GEN))
    1266             : {
    1267        3671 :   pari_sp av = avma;
    1268             :   GEN R, Rc, C, C1, C2, S, K;
    1269        3671 :   long n = lg(x) - 1, r;
    1270        3671 :   r = gen_echelon(shallowtrans(x), &R, &C, E, ff, mul);
    1271        3671 :   Rc = indexcompl(R, n);
    1272        3671 :   C1 = rowpermute(C, R);
    1273        3671 :   C2 = rowpermute(C, Rc);
    1274        3671 :   S = gen_lsolve_lower_unit(C1, C2, E, ff, mul);
    1275        3671 :   K = vecpermute(shallowconcat(gen_matneg(S, E, ff), gen_matid(n - r, E, ff)),
    1276             :                  perm_inv(vecsmall_concat(R, Rc)));
    1277        3671 :   K = shallowtrans(K);
    1278        3671 :   return gerepilecopy(av, K);
    1279             : }
    1280             : 
    1281             : static GEN
    1282          84 : gen_deplin_echelon(GEN x, void *E, const struct bb_field *ff,
    1283             :                    GEN (*mul)(void*, GEN, GEN))
    1284             : {
    1285          84 :   pari_sp av = avma;
    1286             :   GEN R, Rc, C, C1, C2, s, v;
    1287          84 :   long i, n = lg(x) - 1, r;
    1288          84 :   r = gen_echelon(shallowtrans(x), &R, &C, E, ff, mul);
    1289          84 :   if (r == n) return gc_NULL(av);
    1290          56 :   Rc = indexcompl(R, n);
    1291          56 :   i = Rc[1];
    1292          56 :   C1 = rowpermute(C, R);
    1293          56 :   C2 = rowslice(C, i, i);
    1294          56 :   s = row(gen_lsolve_lower_unit(C1, C2, E, ff, mul), 1);
    1295          56 :   settyp(s, t_COL);
    1296          56 :   v = vecpermute(shallowconcat(gen_colneg(s, E, ff), gen_colei(n - r, 1, E, ff)),
    1297             :                  perm_inv(vecsmall_concat(R, Rc)));
    1298          56 :   return gerepilecopy(av, v);
    1299             : }
    1300             : 
    1301             : static GEN
    1302         517 : gen_gauss_CUP(GEN a, GEN b, void *E, const struct bb_field *ff,
    1303             :               GEN (*mul)(void*, GEN, GEN))
    1304             : {
    1305             :   GEN R, C, U, P, Y;
    1306         517 :   long n = lg(a) - 1, r;
    1307         517 :   if (nbrows(a) < n || (r = gen_CUP(a, &R, &C, &U, &P, E, ff, mul)) < n)
    1308          56 :     return NULL;
    1309         461 :   Y = gen_rsolve_lower_unit(rowpermute(C, R), rowpermute(b, R), E, ff, mul);
    1310         461 :   return rowpermute(gen_rsolve_upper(U, Y, E, ff, mul), perm_inv(P));
    1311             : }
    1312             : 
    1313             : static GEN
    1314        4290 : gen_gauss(GEN a, GEN b, void *E, const struct bb_field *ff,
    1315             :           GEN (*mul)(void*, GEN, GEN))
    1316             : {
    1317        4290 :   if (lg(a) - 1 >= gen_CUP_LIMIT)
    1318         517 :     return gen_gauss_CUP(a, b, E, ff, mul);
    1319        3773 :   return gen_Gauss(a, b, E, ff);
    1320             : }
    1321             : 
    1322             : static GEN
    1323        5108 : gen_ker_i(GEN x, long deplin, void *E, const struct bb_field *ff,
    1324             :           GEN (*mul)(void*, GEN, GEN)) {
    1325        5108 :   if (lg(x) - 1 >= gen_CUP_LIMIT && nbrows(x) >= gen_CUP_LIMIT)
    1326        3755 :     return deplin? gen_deplin_echelon(x, E, ff, mul): gen_ker_echelon(x, E, ff, mul);
    1327        1353 :   return gen_ker(x, deplin, E, ff);
    1328             : }
    1329             : 
    1330             : static GEN
    1331         140 : gen_invimage(GEN A, GEN B, void *E, const struct bb_field *ff,
    1332             :              GEN (*mul)(void*, GEN, GEN))
    1333             : {
    1334         140 :   long nA = lg(A)-1, nB = lg(B)-1;
    1335             : 
    1336         140 :   if (!nB) return cgetg(1, t_MAT);
    1337         140 :   if (nA + nB >= gen_CUP_LIMIT && nbrows(B) >= gen_CUP_LIMIT)
    1338          63 :     return gen_invimage_CUP(A, B, E, ff, mul);
    1339          77 :   return gen_matinvimage(A, B, E, ff);
    1340             : }
    1341             : 
    1342             : /* find z such that A z = y. Return NULL if no solution */
    1343             : static GEN
    1344          70 : gen_matcolinvimage_i(GEN A, GEN y, void *E, const struct bb_field *ff,
    1345             :                      GEN (*mul)(void*, GEN, GEN))
    1346             : {
    1347          70 :   pari_sp av = avma;
    1348          70 :   long i, l = lg(A);
    1349             :   GEN M, x, t;
    1350             : 
    1351          70 :   M = gen_ker_i(shallowconcat(A, y), 0, E, ff, mul);
    1352          70 :   i = lg(M) - 1;
    1353          70 :   if (!i) return gc_NULL(av);
    1354             : 
    1355          70 :   x = gel(M, i);
    1356          70 :   t = gel(x, l);
    1357          70 :   if (ff->equal0(t)) return gc_NULL(av);
    1358             : 
    1359          49 :   t = ff->neg(E, ff->inv(E, t));
    1360          49 :   setlg(x, l);
    1361         175 :   for (i = 1; i < l; i++)
    1362         126 :     gel(x, i) = ff->red(E, ff->mul(E, t, gel(x, i)));
    1363          49 :   return gerepilecopy(av, x);
    1364             : }
    1365             : 
    1366             : static GEN
    1367         420 : gen_det_i(GEN a, void *E, const struct bb_field *ff,
    1368             :           GEN (*mul)(void*, GEN, GEN))
    1369             : {
    1370         420 :   if (lg(a) - 1 >= gen_CUP_LIMIT)
    1371         140 :     return gen_det_CUP(a, E, ff, mul);
    1372             :   else
    1373         280 :     return gen_det(a, E, ff);
    1374             : }
    1375             : 
    1376             : static GEN
    1377        2236 : gen_pivots(GEN x, long *rr, void *E, const struct bb_field *ff,
    1378             :            GEN (*mul)(void*, GEN, GEN))
    1379             : {
    1380        2236 :   if (lg(x) - 1 >= gen_CUP_LIMIT && nbrows(x) >= gen_CUP_LIMIT)
    1381         751 :     return gen_pivots_CUP(x, rr, E, ff, mul);
    1382        1485 :   return gen_Gauss_pivot(x, rr, E, ff);
    1383             : }
    1384             : 
    1385             : /* r = dim Ker x, n = nbrows(x) */
    1386             : static GEN
    1387          21 : gen_get_suppl(GEN x, GEN d, long n, long r, void *E, const struct bb_field *ff)
    1388             : {
    1389             :   GEN y, c;
    1390          21 :   long j, k, rx = lg(x)-1; /* != 0 due to init_suppl() */
    1391             : 
    1392          21 :   if (rx == n && r == 0) return gcopy(x);
    1393          21 :   c = zero_zv(n);
    1394          21 :   y = cgetg(n+1, t_MAT);
    1395             :   /* c = lines containing pivots (could get it from gauss_pivot, but cheap)
    1396             :    * In theory r = 0 and d[j] > 0 for all j, but why take chances? */
    1397         119 :   for (k = j = 1; j<=rx; j++)
    1398          98 :     if (d[j]) { c[ d[j] ] = 1; gel(y,k++) = gcopy(gel(x,j)); }
    1399         203 :   for (j=1; j<=n; j++)
    1400         182 :     if (!c[j]) gel(y,k++) = gen_colei(n, j, E, ff);
    1401          21 :   return y;
    1402             : }
    1403             : 
    1404             : static GEN
    1405          21 : gen_suppl(GEN x, void *E, const struct bb_field *ff,
    1406             :           GEN (*mul)(void*, GEN, GEN))
    1407             : {
    1408             :   GEN d;
    1409          21 :   long n = nbrows(x), r;
    1410             : 
    1411          21 :   if (lg(x) == 1) pari_err_IMPL("suppl [empty matrix]");
    1412          21 :   d = gen_pivots(x, &r, E, ff, mul);
    1413          21 :   return gen_get_suppl(x, d, n, r, E, ff);
    1414             : }
    1415             : 
    1416             : /*******************************************************************/
    1417             : /*                                                                 */
    1418             : /*                MATRIX MULTIPLICATION MODULO P                   */
    1419             : /*                                                                 */
    1420             : /*******************************************************************/
    1421             : 
    1422             : GEN
    1423          21 : F2xqM_F2xqC_mul(GEN A, GEN B, GEN T) {
    1424             :   void *E;
    1425          21 :   const struct bb_field *ff = get_F2xq_field(&E, T);
    1426          21 :   return gen_matcolmul(A, B, E, ff);
    1427             : }
    1428             : 
    1429             : GEN
    1430          35 : FlxqM_FlxqC_mul(GEN A, GEN B, GEN T, ulong p) {
    1431             :   void *E;
    1432          35 :   const struct bb_field *ff = get_Flxq_field(&E, T, p);
    1433          35 :   return gen_matcolmul(A, B, E, ff);
    1434             : }
    1435             : 
    1436             : GEN
    1437          49 : FqM_FqC_mul(GEN A, GEN B, GEN T, GEN p) {
    1438             :   void *E;
    1439          49 :   const struct bb_field *ff = get_Fq_field(&E, T, p);
    1440          49 :   return gen_matcolmul(A, B, E, ff);
    1441             : }
    1442             : 
    1443             : GEN
    1444        1407 : F2xqM_mul(GEN A, GEN B, GEN T) {
    1445             :   void *E;
    1446        1407 :   const struct bb_field *ff = get_F2xq_field(&E, T);
    1447        1407 :   return gen_matmul(A, B, E, ff);
    1448             : }
    1449             : 
    1450             : GEN
    1451      138385 : FlxqM_mul(GEN A, GEN B, GEN T, ulong p) {
    1452             :   void *E;
    1453             :   const struct bb_field *ff;
    1454      138385 :   long n = lg(A) - 1;
    1455             : 
    1456      138385 :   if (n == 0)
    1457           0 :     return cgetg(1, t_MAT);
    1458      138385 :   if (n > 1)
    1459       76248 :     return FlxqM_mul_Kronecker(A, B, T, p);
    1460       62137 :   ff = get_Flxq_field(&E, T, p);
    1461       62137 :   return gen_matmul(A, B, E, ff);
    1462             : }
    1463             : 
    1464             : GEN
    1465       66416 : FqM_mul(GEN A, GEN B, GEN T, GEN p) {
    1466             :   void *E;
    1467       66416 :   long n = lg(A) - 1;
    1468             :   const struct bb_field *ff;
    1469       66416 :   if (n == 0)
    1470           0 :     return cgetg(1, t_MAT);
    1471       66416 :   if (n > 1)
    1472       62797 :     return FqM_mul_Kronecker(A, B, T, p);
    1473        3619 :   ff = get_Fq_field(&E, T, p);
    1474        3619 :   return gen_matmul(A, B, E, ff);
    1475             : }
    1476             : 
    1477             : /*******************************************************************/
    1478             : /*                                                                 */
    1479             : /*                    LINEAR ALGEBRA MODULO P                      */
    1480             : /*                                                                 */
    1481             : /*******************************************************************/
    1482             : 
    1483             : static GEN
    1484           0 : _F2xqM_mul(void *E, GEN A, GEN B)
    1485           0 : { return F2xqM_mul(A, B, (GEN) E); }
    1486             : 
    1487             : struct _Flxq {
    1488             :   GEN aut;
    1489             :   GEN T;
    1490             :   ulong p;
    1491             : };
    1492             : 
    1493             : static GEN
    1494       14077 : _FlxqM_mul(void *E, GEN A, GEN B)
    1495             : {
    1496       14077 :   struct _Flxq *D = (struct _Flxq*)E;
    1497       14077 :   return FlxqM_mul(A, B, D->T, D->p);
    1498             : }
    1499             : 
    1500             : static GEN
    1501       18171 : _FpM_mul(void *E, GEN A, GEN B)
    1502       18171 : { return FpM_mul(A, B, (GEN) E); }
    1503             : 
    1504             : struct _Fq_field
    1505             : {
    1506             :   GEN T, p;
    1507             : };
    1508             : 
    1509             : static GEN
    1510        6349 : _FqM_mul(void *E, GEN A, GEN B)
    1511             : {
    1512        6349 :   struct _Fq_field *D = (struct _Fq_field*) E;
    1513        6349 :   return FqM_mul(A, B, D->T, D->p);
    1514             : }
    1515             : 
    1516             : 
    1517             : static GEN
    1518      629297 : FpM_init(GEN a, GEN p, ulong *pp)
    1519             : {
    1520      629297 :   if (lgefint(p) == 3)
    1521             :   {
    1522      622031 :     *pp = uel(p,2);
    1523      622031 :     return (*pp==2)? ZM_to_F2m(a): ZM_to_Flm(a, *pp);
    1524             :   }
    1525        7266 :   *pp = 0; return a;
    1526             : }
    1527             : GEN
    1528        2359 : RgM_Fp_init(GEN a, GEN p, ulong *pp)
    1529             : {
    1530        2359 :   if (lgefint(p) == 3)
    1531             :   {
    1532        2009 :     *pp = uel(p,2);
    1533        2009 :     return (*pp==2)? RgM_to_F2m(a): RgM_to_Flm(a, *pp);
    1534             :   }
    1535         350 :   *pp = 0; return RgM_to_FpM(a,p);
    1536             : }
    1537             : 
    1538             : static GEN
    1539         315 : FpM_det_gen(GEN a, GEN p)
    1540             : {
    1541             :   void *E;
    1542         315 :   const struct bb_field *S = get_Fp_field(&E,p);
    1543         315 :   return gen_det_i(a, E, S, _FpM_mul);
    1544             : }
    1545             : GEN
    1546        3948 : FpM_det(GEN a, GEN p)
    1547             : {
    1548        3948 :   pari_sp av = avma;
    1549             :   ulong pp, d;
    1550        3948 :   a = FpM_init(a, p, &pp);
    1551        3948 :   switch(pp)
    1552             :   {
    1553         315 :   case 0: return FpM_det_gen(a, p);
    1554        1617 :   case 2: d = F2m_det_sp(a); break;
    1555        2016 :   default:d = Flm_det_sp(a,pp); break;
    1556             :   }
    1557        3633 :   set_avma(av); return utoi(d);
    1558             : }
    1559             : 
    1560             : GEN
    1561           7 : F2xqM_det(GEN a, GEN T)
    1562             : {
    1563             :   void *E;
    1564           7 :   const struct bb_field *S = get_F2xq_field(&E, T);
    1565           7 :   return gen_det_i(a, E, S, _F2xqM_mul);
    1566             : }
    1567             : 
    1568             : GEN
    1569          28 : FlxqM_det(GEN a, GEN T, ulong p) {
    1570             :   void *E;
    1571          28 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    1572          28 :   return gen_det_i(a, E, S, _FlxqM_mul);
    1573             : }
    1574             : 
    1575             : GEN
    1576          70 : FqM_det(GEN x, GEN T, GEN p)
    1577             : {
    1578             :   void *E;
    1579          70 :   const struct bb_field *S = get_Fq_field(&E,T,p);
    1580          70 :   return gen_det_i(x, E, S, _FqM_mul);
    1581             : }
    1582             : 
    1583             : static GEN
    1584         815 : FpM_gauss_pivot_gen(GEN x, GEN p, long *rr)
    1585             : {
    1586             :   void *E;
    1587         815 :   const struct bb_field *S = get_Fp_field(&E,p);
    1588         815 :   return gen_pivots(x, rr, E, S, _FpM_mul);
    1589             : }
    1590             : 
    1591             : static GEN
    1592      166012 : FpM_gauss_pivot(GEN x, GEN p, long *rr)
    1593             : {
    1594             :   ulong pp;
    1595      166012 :   if (lg(x)==1) { *rr = 0; return NULL; }
    1596      164101 :   x = FpM_init(x, p, &pp);
    1597      164101 :   switch(pp)
    1598             :   {
    1599         815 :   case 0: return FpM_gauss_pivot_gen(x, p, rr);
    1600       46275 :   case 2: return F2m_gauss_pivot(x, rr);
    1601      117011 :   default:return Flm_pivots(x, pp, rr, 1);
    1602             :   }
    1603             : }
    1604             : 
    1605             : static GEN
    1606          21 : F2xqM_gauss_pivot(GEN x, GEN T, long *rr)
    1607             : {
    1608             :   void *E;
    1609          21 :   const struct bb_field *S = get_F2xq_field(&E,T);
    1610          21 :   return gen_pivots(x, rr, E, S, _F2xqM_mul);
    1611             : }
    1612             : 
    1613             : static GEN
    1614        1274 : FlxqM_gauss_pivot(GEN x, GEN T, ulong p, long *rr) {
    1615             :   void *E;
    1616        1274 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    1617        1274 :   return gen_pivots(x, rr, E, S, _FlxqM_mul);
    1618             : }
    1619             : 
    1620             : static GEN
    1621         105 : FqM_gauss_pivot_gen(GEN x, GEN T, GEN p, long *rr)
    1622             : {
    1623             :   void *E;
    1624         105 :   const struct bb_field *S = get_Fq_field(&E,T,p);
    1625         105 :   return gen_pivots(x, rr, E, S, _FqM_mul);
    1626             : }
    1627             : static GEN
    1628        1351 : FqM_gauss_pivot(GEN x, GEN T, GEN p, long *rr)
    1629             : {
    1630        1351 :   if (lg(x)==1) { *rr = 0; return NULL; }
    1631        1351 :   if (!T) return FpM_gauss_pivot(x, p, rr);
    1632        1351 :   if (lgefint(p) == 3)
    1633             :   {
    1634        1246 :     pari_sp av = avma;
    1635        1246 :     ulong pp = uel(p,2);
    1636        1246 :     GEN Tp = ZXT_to_FlxT(T, pp);
    1637        1246 :     GEN d = FlxqM_gauss_pivot(FqM_to_FlxM(x, T, p), Tp, pp, rr);
    1638        1246 :     return d ? gerepileuptoleaf(av, d): d;
    1639             :   }
    1640         105 :   return FqM_gauss_pivot_gen(x, T, p, rr);
    1641             : }
    1642             : 
    1643             : GEN
    1644       93755 : FpM_image(GEN x, GEN p)
    1645             : {
    1646             :   long r;
    1647       93755 :   GEN d = FpM_gauss_pivot(x,p,&r); /* d left on stack for efficiency */
    1648       93755 :   return image_from_pivot(x,d,r);
    1649             : }
    1650             : 
    1651             : GEN
    1652        1057 : Flm_image(GEN x, ulong p)
    1653             : {
    1654             :   long r;
    1655        1057 :   GEN d = Flm_pivots(x, p, &r, 0); /* d left on stack for efficiency */
    1656        1057 :   return image_from_pivot(x,d,r);
    1657             : }
    1658             : 
    1659             : GEN
    1660           7 : F2m_image(GEN x)
    1661             : {
    1662             :   long r;
    1663           7 :   GEN d = F2m_gauss_pivot(F2m_copy(x),&r); /* d left on stack for efficiency */
    1664           7 :   return image_from_pivot(x,d,r);
    1665             : }
    1666             : 
    1667             : GEN
    1668           7 : F2xqM_image(GEN x, GEN T)
    1669             : {
    1670             :   long r;
    1671           7 :   GEN d = F2xqM_gauss_pivot(x,T,&r); /* d left on stack for efficiency */
    1672           7 :   return image_from_pivot(x,d,r);
    1673             : }
    1674             : 
    1675             : GEN
    1676          21 : FlxqM_image(GEN x, GEN T, ulong p)
    1677             : {
    1678             :   long r;
    1679          21 :   GEN d = FlxqM_gauss_pivot(x, T, p, &r); /* d left on stack for efficiency */
    1680          21 :   return image_from_pivot(x,d,r);
    1681             : }
    1682             : 
    1683             : GEN
    1684          49 : FqM_image(GEN x, GEN T, GEN p)
    1685             : {
    1686             :   long r;
    1687          49 :   GEN d = FqM_gauss_pivot(x,T,p,&r); /* d left on stack for efficiency */
    1688          49 :   return image_from_pivot(x,d,r);
    1689             : }
    1690             : 
    1691             : long
    1692          28 : FpM_rank(GEN x, GEN p)
    1693             : {
    1694          28 :   pari_sp av = avma;
    1695             :   long r;
    1696          28 :   (void)FpM_gauss_pivot(x,p,&r);
    1697          28 :   return gc_long(av, lg(x)-1 - r);
    1698             : }
    1699             : 
    1700             : long
    1701           7 : F2xqM_rank(GEN x, GEN T)
    1702             : {
    1703           7 :   pari_sp av = avma;
    1704             :   long r;
    1705           7 :   (void)F2xqM_gauss_pivot(x,T,&r);
    1706           7 :   return gc_long(av, lg(x)-1 - r);
    1707             : }
    1708             : 
    1709             : long
    1710          28 : FlxqM_rank(GEN x, GEN T, ulong p)
    1711             : {
    1712             :   void *E;
    1713          28 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    1714          28 :   return gen_matrank(x, E, S, _FlxqM_mul);
    1715             : }
    1716             : 
    1717             : long
    1718          70 : FqM_rank(GEN x, GEN T, GEN p)
    1719             : {
    1720          70 :   pari_sp av = avma;
    1721             :   long r;
    1722          70 :   (void)FqM_gauss_pivot(x,T,p,&r);
    1723          70 :   return gc_long(av, lg(x)-1 - r);
    1724             : }
    1725             : 
    1726             : static GEN
    1727          35 : FpM_invimage_gen(GEN A, GEN B, GEN p)
    1728             : {
    1729             :   void *E;
    1730          35 :   const struct bb_field *ff = get_Fp_field(&E, p);
    1731          35 :   return gen_invimage(A, B, E, ff, _FpM_mul);
    1732             : }
    1733             : 
    1734             : GEN
    1735           0 : FpM_invimage(GEN A, GEN B, GEN p)
    1736             : {
    1737           0 :   pari_sp av = avma;
    1738             :   ulong pp;
    1739             :   GEN y;
    1740             : 
    1741           0 :   A = FpM_init(A, p, &pp);
    1742           0 :   switch(pp)
    1743             :   {
    1744           0 :   case 0: return FpM_invimage_gen(A, B, p);
    1745             :   case 2:
    1746           0 :     y = F2m_invimage(A, ZM_to_F2m(B));
    1747           0 :     if (!y) return gc_NULL(av);
    1748           0 :     y = F2m_to_ZM(y);
    1749           0 :     return gerepileupto(av, y);
    1750             :   default:
    1751           0 :     y = Flm_invimage(A, ZM_to_Flm(B, pp), pp);
    1752           0 :     if (!y) return gc_NULL(av);
    1753           0 :     y = Flm_to_ZM(y);
    1754           0 :     return gerepileupto(av, y);
    1755             :   }
    1756             : }
    1757             : 
    1758             : GEN
    1759          21 : F2xqM_invimage(GEN A, GEN B, GEN T) {
    1760             :   void *E;
    1761          21 :   const struct bb_field *ff = get_F2xq_field(&E, T);
    1762          21 :   return gen_invimage(A, B, E, ff, _F2xqM_mul);
    1763             : }
    1764             : 
    1765             : GEN
    1766          42 : FlxqM_invimage(GEN A, GEN B, GEN T, ulong p) {
    1767             :   void *E;
    1768          42 :   const struct bb_field *ff = get_Flxq_field(&E, T, p);
    1769          42 :   return gen_invimage(A, B, E, ff, _FlxqM_mul);
    1770             : }
    1771             : 
    1772             : GEN
    1773          42 : FqM_invimage(GEN A, GEN B, GEN T, GEN p) {
    1774             :   void *E;
    1775          42 :   const struct bb_field *ff = get_Fq_field(&E, T, p);
    1776          42 :   return gen_invimage(A, B, E, ff, _FqM_mul);
    1777             : }
    1778             : 
    1779             : static GEN
    1780           7 : FpM_FpC_invimage_gen(GEN A, GEN y, GEN p)
    1781             : {
    1782             :   void *E;
    1783           7 :   const struct bb_field *ff = get_Fp_field(&E, p);
    1784           7 :   return gen_matcolinvimage_i(A, y, E, ff, _FpM_mul);
    1785             : }
    1786             : 
    1787             : GEN
    1788       54392 : FpM_FpC_invimage(GEN A, GEN x, GEN p)
    1789             : {
    1790       54392 :   pari_sp av = avma;
    1791             :   ulong pp;
    1792             :   GEN y;
    1793             : 
    1794       54392 :   A = FpM_init(A, p, &pp);
    1795       54392 :   switch(pp)
    1796             :   {
    1797           7 :   case 0: return FpM_FpC_invimage_gen(A, x, p);
    1798             :   case 2:
    1799       19852 :     y = F2m_F2c_invimage(A, ZV_to_F2v(x));
    1800       19852 :     if (!y) return y;
    1801       19852 :     y = F2c_to_ZC(y);
    1802       19852 :     return gerepileupto(av, y);
    1803             :   default:
    1804       34533 :     y = Flm_Flc_invimage(A, ZV_to_Flv(x, pp), pp);
    1805       34533 :     if (!y) return y;
    1806       34533 :     y = Flc_to_ZC(y);
    1807       34533 :     return gerepileupto(av, y);
    1808             :   }
    1809             : }
    1810             : 
    1811             : GEN
    1812          21 : F2xqM_F2xqC_invimage(GEN A, GEN B, GEN T) {
    1813             :   void *E;
    1814          21 :   const struct bb_field *ff = get_F2xq_field(&E, T);
    1815          21 :   return gen_matcolinvimage_i(A, B, E, ff, _F2xqM_mul);
    1816             : }
    1817             : 
    1818             : GEN
    1819          21 : FlxqM_FlxqC_invimage(GEN A, GEN B, GEN T, ulong p) {
    1820             :   void *E;
    1821          21 :   const struct bb_field *ff = get_Flxq_field(&E, T, p);
    1822          21 :   return gen_matcolinvimage_i(A, B, E, ff, _FlxqM_mul);
    1823             : }
    1824             : 
    1825             : GEN
    1826          21 : FqM_FqC_invimage(GEN A, GEN B, GEN T, GEN p) {
    1827             :   void *E;
    1828          21 :   const struct bb_field *ff = get_Fq_field(&E, T, p);
    1829          21 :   return gen_matcolinvimage_i(A, B, E, ff, _FqM_mul);
    1830             : }
    1831             : 
    1832             : static GEN
    1833        2217 : FpM_ker_gen(GEN x, GEN p, long deplin)
    1834             : {
    1835             :   void *E;
    1836        2217 :   const struct bb_field *S = get_Fp_field(&E,p);
    1837        2217 :   return gen_ker_i(x, deplin, E, S, _FpM_mul);
    1838             : }
    1839             : static GEN
    1840      332270 : FpM_ker_i(GEN x, GEN p, long deplin)
    1841             : {
    1842      332270 :   pari_sp av = avma;
    1843             :   ulong pp;
    1844             :   GEN y;
    1845             : 
    1846      332270 :   if (lg(x)==1) return cgetg(1,t_MAT);
    1847      332270 :   x = FpM_init(x, p, &pp);
    1848      332270 :   switch(pp)
    1849             :   {
    1850        2147 :   case 0: return FpM_ker_gen(x,p,deplin);
    1851             :   case 2:
    1852       91124 :     y = F2m_ker_sp(x, deplin);
    1853       91124 :     if (!y) return gc_NULL(av);
    1854       91124 :     y = deplin? F2c_to_ZC(y): F2m_to_ZM(y);
    1855       91124 :     return gerepileupto(av, y);
    1856             :   default:
    1857      238999 :     y = Flm_ker_sp(x, pp, deplin);
    1858      238999 :     if (!y) return gc_NULL(av);
    1859      238999 :     y = deplin? Flc_to_ZC(y): Flm_to_ZM(y);
    1860      238999 :     return gerepileupto(av, y);
    1861             :   }
    1862             : }
    1863             : 
    1864             : GEN
    1865      239971 : FpM_ker(GEN x, GEN p) { return FpM_ker_i(x,p,0); }
    1866             : 
    1867             : static GEN
    1868          35 : F2xqM_ker_i(GEN x, GEN T, long deplin)
    1869             : {
    1870             :   const struct bb_field *ff;
    1871             :   void *E;
    1872             : 
    1873          35 :   if (lg(x)==1) return cgetg(1,t_MAT);
    1874          35 :   ff = get_F2xq_field(&E,T);
    1875          35 :   return gen_ker_i(x,deplin, E, ff, _F2xqM_mul);
    1876             : }
    1877             : 
    1878             : GEN
    1879          21 : F2xqM_ker(GEN x, GEN T)
    1880             : {
    1881          21 :   return F2xqM_ker_i(x, T, 0);
    1882             : }
    1883             : 
    1884             : static GEN
    1885        2660 : FlxqM_ker_i(GEN x, GEN T, ulong p, long deplin) {
    1886             :   void *E;
    1887        2660 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    1888        2660 :   return gen_ker_i(x, deplin, E, S, _FlxqM_mul);
    1889             : }
    1890             : 
    1891             : GEN
    1892        2625 : FlxqM_ker(GEN x, GEN T, ulong p)
    1893             : {
    1894        2625 :   return FlxqM_ker_i(x, T, p, 0);
    1895             : }
    1896             : 
    1897             : static GEN
    1898         126 : FqM_ker_gen(GEN x, GEN T, GEN p, long deplin)
    1899             : {
    1900             :   void *E;
    1901         126 :   const struct bb_field *S = get_Fq_field(&E,T,p);
    1902         126 :   return gen_ker_i(x,deplin,E,S,_FqM_mul);
    1903             : }
    1904             : static GEN
    1905        8659 : FqM_ker_i(GEN x, GEN T, GEN p, long deplin)
    1906             : {
    1907        8659 :   if (!T) return FpM_ker_i(x,p,deplin);
    1908        2723 :   if (lg(x)==1) return cgetg(1,t_MAT);
    1909             : 
    1910        2723 :   if (lgefint(p)==3)
    1911             :   {
    1912        2597 :     pari_sp ltop=avma;
    1913        2597 :     ulong l= p[2];
    1914        2597 :     GEN Ml = FqM_to_FlxM(x, T, p);
    1915        2597 :     GEN Tl = ZXT_to_FlxT(T,l);
    1916        2597 :     GEN p1 = FlxM_to_ZXM(FlxqM_ker(Ml,Tl,l));
    1917        2597 :     return gerepileupto(ltop,p1);
    1918             :   }
    1919         126 :   return FqM_ker_gen(x, T, p, deplin);
    1920             : }
    1921             : 
    1922             : GEN
    1923        8582 : FqM_ker(GEN x, GEN T, GEN p) { return FqM_ker_i(x,T,p,0); }
    1924             : 
    1925             : GEN
    1926       86363 : FpM_deplin(GEN x, GEN p) { return FpM_ker_i(x,p,1); }
    1927             : 
    1928             : GEN
    1929          14 : F2xqM_deplin(GEN x, GEN T)
    1930             : {
    1931          14 :   return F2xqM_ker_i(x, T, 1);
    1932             : }
    1933             : 
    1934             : GEN
    1935          35 : FlxqM_deplin(GEN x, GEN T, ulong p)
    1936             : {
    1937          35 :   return FlxqM_ker_i(x, T, p, 1);
    1938             : }
    1939             : 
    1940             : GEN
    1941          77 : FqM_deplin(GEN x, GEN T, GEN p) { return FqM_ker_i(x,T,p,1); }
    1942             : 
    1943             : static GEN
    1944        3982 : FpM_gauss_gen(GEN a, GEN b, GEN p)
    1945             : {
    1946             :   void *E;
    1947        3982 :   const struct bb_field *S = get_Fp_field(&E,p);
    1948        3982 :   return gen_gauss(a,b, E, S, _FpM_mul);
    1949             : }
    1950             : /* a an FpM, lg(a)>1; b an FpM or NULL (replace by identity) */
    1951             : static GEN
    1952       74586 : FpM_gauss_i(GEN a, GEN b, GEN p, ulong *pp)
    1953             : {
    1954       74586 :   long n = nbrows(a);
    1955       74586 :   a = FpM_init(a,p,pp);
    1956       74586 :   switch(*pp)
    1957             :   {
    1958             :   case 0:
    1959        3982 :     if (!b) b = matid(n);
    1960        3982 :     return FpM_gauss_gen(a,b,p);
    1961             :   case 2:
    1962       23184 :     if (b) b = ZM_to_F2m(b); else b = matid_F2m(n);
    1963       23184 :     return F2m_gauss_sp(a,b);
    1964             :   default:
    1965       47420 :     if (b) b = ZM_to_Flm(b, *pp); else b = matid_Flm(n);
    1966       47420 :     return Flm_gauss_sp(a,b, NULL, *pp);
    1967             :   }
    1968             : }
    1969             : GEN
    1970          35 : FpM_gauss(GEN a, GEN b, GEN p)
    1971             : {
    1972          35 :   pari_sp av = avma;
    1973             :   ulong pp;
    1974             :   GEN u;
    1975          35 :   if (lg(a) == 1 || lg(b)==1) return cgetg(1, t_MAT);
    1976          35 :   u = FpM_gauss_i(a, b, p, &pp);
    1977          35 :   if (!u) return gc_NULL(av);
    1978          28 :   switch(pp)
    1979             :   {
    1980          28 :   case 0: return gerepilecopy(av, u);
    1981           0 :   case 2:  u = F2m_to_ZM(u); break;
    1982           0 :   default: u = Flm_to_ZM(u); break;
    1983             :   }
    1984           0 :   return gerepileupto(av, u);
    1985             : }
    1986             : 
    1987             : static GEN
    1988          84 : F2xqM_gauss_gen(GEN a, GEN b, GEN T)
    1989             : {
    1990             :   void *E;
    1991          84 :   const struct bb_field *S = get_F2xq_field(&E, T);
    1992          84 :   return gen_gauss(a, b, E, S, _F2xqM_mul);
    1993             : }
    1994             : 
    1995             : GEN
    1996          21 : F2xqM_gauss(GEN a, GEN b, GEN T)
    1997             : {
    1998          21 :   pari_sp av = avma;
    1999          21 :   long n = lg(a)-1;
    2000             :   GEN u;
    2001          21 :   if (!n || lg(b)==1) { set_avma(av); return cgetg(1, t_MAT); }
    2002          21 :   u = F2xqM_gauss_gen(a, b, T);
    2003          21 :   if (!u) return gc_NULL(av);
    2004          14 :   return gerepilecopy(av, u);
    2005             : }
    2006             : 
    2007             : static GEN
    2008          91 : FlxqM_gauss_i(GEN a, GEN b, GEN T, ulong p) {
    2009             :   void *E;
    2010          91 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    2011          91 :   return gen_gauss(a, b, E, S, _FlxqM_mul);
    2012             : }
    2013             : 
    2014             : GEN
    2015          21 : FlxqM_gauss(GEN a, GEN b, GEN T, ulong p)
    2016             : {
    2017          21 :   pari_sp av = avma;
    2018          21 :   long n = lg(a)-1;
    2019             :   GEN u;
    2020          21 :   if (!n || lg(b)==1) { set_avma(av); return cgetg(1, t_MAT); }
    2021          21 :   u = FlxqM_gauss_i(a, b, T, p);
    2022          21 :   if (!u) return gc_NULL(av);
    2023          14 :   return gerepilecopy(av, u);
    2024             : }
    2025             : 
    2026             : static GEN
    2027         133 : FqM_gauss_gen(GEN a, GEN b, GEN T, GEN p)
    2028             : {
    2029             :   void *E;
    2030         133 :   const struct bb_field *S = get_Fq_field(&E,T,p);
    2031         133 :   return gen_gauss(a,b,E,S,_FqM_mul);
    2032             : }
    2033             : GEN
    2034          21 : FqM_gauss(GEN a, GEN b, GEN T, GEN p)
    2035             : {
    2036          21 :   pari_sp av = avma;
    2037             :   GEN u;
    2038             :   long n;
    2039          21 :   if (!T) return FpM_gauss(a,b,p);
    2040          21 :   n = lg(a)-1; if (!n || lg(b)==1) return cgetg(1, t_MAT);
    2041          21 :   u = FqM_gauss_gen(a,b,T,p);
    2042          21 :   if (!u) return gc_NULL(av);
    2043          14 :   return gerepilecopy(av, u);
    2044             : }
    2045             : 
    2046             : GEN
    2047          14 : FpM_FpC_gauss(GEN a, GEN b, GEN p)
    2048             : {
    2049          14 :   pari_sp av = avma;
    2050             :   ulong pp;
    2051             :   GEN u;
    2052          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    2053          14 :   u = FpM_gauss_i(a, mkmat(b), p, &pp);
    2054          14 :   if (!u) return gc_NULL(av);
    2055          14 :   switch(pp)
    2056             :   {
    2057          14 :   case 0: return gerepilecopy(av, gel(u,1));
    2058           0 :   case 2:  u = F2c_to_ZC(gel(u,1)); break;
    2059           0 :   default: u = Flc_to_ZC(gel(u,1)); break;
    2060             :   }
    2061           0 :   return gerepileupto(av, u);
    2062             : }
    2063             : 
    2064             : GEN
    2065          28 : F2xqM_F2xqC_gauss(GEN a, GEN b, GEN T)
    2066             : {
    2067          28 :   pari_sp av = avma;
    2068             :   GEN u;
    2069          28 :   if (lg(a) == 1) return cgetg(1, t_COL);
    2070          28 :   u = F2xqM_gauss_gen(a, mkmat(b), T);
    2071          28 :   if (!u) return gc_NULL(av);
    2072          14 :   return gerepilecopy(av, gel(u,1));
    2073             : }
    2074             : 
    2075             : GEN
    2076          14 : FlxqM_FlxqC_gauss(GEN a, GEN b, GEN T, ulong p)
    2077             : {
    2078          14 :   pari_sp av = avma;
    2079             :   GEN u;
    2080          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    2081          14 :   u = FlxqM_gauss_i(a, mkmat(b), T, p);
    2082          14 :   if (!u) return gc_NULL(av);
    2083           7 :   return gerepilecopy(av, gel(u,1));
    2084             : }
    2085             : 
    2086             : GEN
    2087          14 : FqM_FqC_gauss(GEN a, GEN b, GEN T, GEN p)
    2088             : {
    2089          14 :   pari_sp av = avma;
    2090             :   GEN u;
    2091          14 :   if (!T) return FpM_FpC_gauss(a,b,p);
    2092          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    2093          14 :   u = FqM_gauss_gen(a,mkmat(b),T,p);
    2094          14 :   if (!u) return gc_NULL(av);
    2095           7 :   return gerepilecopy(av, gel(u,1));
    2096             : }
    2097             : 
    2098             : GEN
    2099       74537 : FpM_inv(GEN a, GEN p)
    2100             : {
    2101       74537 :   pari_sp av = avma;
    2102             :   ulong pp;
    2103             :   GEN u;
    2104       74537 :   if (lg(a) == 1) return cgetg(1, t_MAT);
    2105       74537 :   u = FpM_gauss_i(a, NULL, p, &pp);
    2106       74537 :   if (!u) return gc_NULL(av);
    2107       74523 :   switch(pp)
    2108             :   {
    2109        3926 :   case 0: return gerepilecopy(av, u);
    2110       23177 :   case 2:  u = F2m_to_ZM(u); break;
    2111       47420 :   default: u = Flm_to_ZM(u); break;
    2112             :   }
    2113       70597 :   return gerepileupto(av, u);
    2114             : }
    2115             : 
    2116             : GEN
    2117          35 : F2xqM_inv(GEN a, GEN T)
    2118             : {
    2119          35 :   pari_sp av = avma;
    2120             :   GEN u;
    2121          35 :   if (lg(a) == 1) { set_avma(av); return cgetg(1, t_MAT); }
    2122          35 :   u = F2xqM_gauss_gen(a, matid_F2xqM(nbrows(a),T), T);
    2123          35 :   if (!u) return gc_NULL(av);
    2124          28 :   return gerepilecopy(av, u);
    2125             : }
    2126             : 
    2127             : GEN
    2128          56 : FlxqM_inv(GEN a, GEN T, ulong p)
    2129             : {
    2130          56 :   pari_sp av = avma;
    2131             :   GEN u;
    2132          56 :   if (lg(a) == 1) { set_avma(av); return cgetg(1, t_MAT); }
    2133          56 :   u = FlxqM_gauss_i(a, matid_FlxqM(nbrows(a),T,p), T,p);
    2134          56 :   if (!u) return gc_NULL(av);
    2135          42 :   return gerepilecopy(av, u);
    2136             : }
    2137             : 
    2138             : GEN
    2139          98 : FqM_inv(GEN a, GEN T, GEN p)
    2140             : {
    2141          98 :   pari_sp av = avma;
    2142             :   GEN u;
    2143          98 :   if (!T) return FpM_inv(a,p);
    2144          98 :   if (lg(a) == 1) return cgetg(1, t_MAT);
    2145          98 :   u = FqM_gauss_gen(a,matid(nbrows(a)),T,p);
    2146          98 :   if (!u) return gc_NULL(av);
    2147          70 :   return gerepilecopy(av, u);
    2148             : }
    2149             : 
    2150             : GEN
    2151      103386 : FpM_intersect(GEN x, GEN y, GEN p)
    2152             : {
    2153      103386 :   pari_sp av = avma;
    2154      103386 :   long j, lx = lg(x);
    2155             :   GEN z;
    2156             : 
    2157      103386 :   if (lx==1 || lg(y)==1) return cgetg(1,t_MAT);
    2158      103386 :   z = FpM_ker(shallowconcat(x,y), p);
    2159      103386 :   for (j=lg(z)-1; j; j--) setlg(gel(z,j),lx);
    2160      103386 :   return gerepileupto(av, FpM_mul(x,z,p));
    2161             : }
    2162             : 
    2163             : static void
    2164       44467 : init_suppl(GEN x)
    2165             : {
    2166       44467 :   if (lg(x) == 1) pari_err_IMPL("suppl [empty matrix]");
    2167             :   /* HACK: avoid overwriting d from gauss_pivot() after set_avma(av) */
    2168       44467 :   (void)new_chunk(lgcols(x) * 2);
    2169       44467 : }
    2170             : 
    2171             : GEN
    2172       43095 : FpM_suppl(GEN x, GEN p)
    2173             : {
    2174             :   GEN d;
    2175             :   long r;
    2176       43095 :   init_suppl(x); d = FpM_gauss_pivot(x,p, &r);
    2177       43095 :   return get_suppl(x,d,nbrows(x),r,&col_ei);
    2178             : }
    2179             : 
    2180             : GEN
    2181          14 : F2m_suppl(GEN x)
    2182             : {
    2183             :   GEN d;
    2184             :   long r;
    2185          14 :   init_suppl(x); d = F2m_gauss_pivot(F2m_copy(x), &r);
    2186          14 :   return get_suppl(x,d,mael(x,1,1),r,&F2v_ei);
    2187             : }
    2188             : 
    2189             : GEN
    2190          63 : Flm_suppl(GEN x, ulong p)
    2191             : {
    2192             :   GEN d;
    2193             :   long r;
    2194          63 :   init_suppl(x); d = Flm_pivots(x, p, &r, 0);
    2195          63 :   return get_suppl(x,d,nbrows(x),r,&vecsmall_ei);
    2196             : }
    2197             : 
    2198             : GEN
    2199           7 : F2xqM_suppl(GEN x, GEN T)
    2200             : {
    2201             :   void *E;
    2202           7 :   const struct bb_field *S = get_F2xq_field(&E, T);
    2203           7 :   return gen_suppl(x, E, S, _F2xqM_mul);
    2204             : }
    2205             : 
    2206             : GEN
    2207          14 : FlxqM_suppl(GEN x, GEN T, ulong p)
    2208             : {
    2209             :   void *E;
    2210          14 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    2211          14 :   return gen_suppl(x, E, S, _FlxqM_mul);
    2212             : }
    2213             : 
    2214             : GEN
    2215        4123 : FqM_suppl(GEN x, GEN T, GEN p)
    2216             : {
    2217        4123 :   pari_sp av = avma;
    2218             :   GEN d;
    2219             :   long r;
    2220             : 
    2221        4123 :   if (!T) return FpM_suppl(x,p);
    2222        1225 :   init_suppl(x);
    2223        1225 :   d = FqM_gauss_pivot(x,T,p,&r);
    2224        1225 :   set_avma(av); return get_suppl(x,d,nbrows(x),r,&col_ei);
    2225             : }
    2226             : 
    2227             : static void
    2228       91529 : init_indexrank(GEN x) {
    2229       91529 :   (void)new_chunk(3 + 2*lg(x)); /* HACK */
    2230       91529 : }
    2231             : 
    2232             : GEN
    2233       29134 : FpM_indexrank(GEN x, GEN p) {
    2234       29134 :   pari_sp av = avma;
    2235             :   long r;
    2236             :   GEN d;
    2237       29134 :   init_indexrank(x);
    2238       29134 :   d = FpM_gauss_pivot(x,p,&r);
    2239       29134 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    2240             : }
    2241             : 
    2242             : GEN
    2243       19422 : Flm_indexrank(GEN x, ulong p) {
    2244       19422 :   pari_sp av = avma;
    2245             :   long r;
    2246             :   GEN d;
    2247       19422 :   init_indexrank(x);
    2248       19422 :   d = Flm_pivots(x, p, &r, 0);
    2249       19422 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    2250             : }
    2251             : 
    2252             : GEN
    2253           7 : F2m_indexrank(GEN x) {
    2254           7 :   pari_sp av = avma;
    2255             :   long r;
    2256             :   GEN d;
    2257           7 :   init_indexrank(x);
    2258           7 :   d = F2m_gauss_pivot(F2m_copy(x),&r);
    2259           7 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    2260             : }
    2261             : 
    2262             : GEN
    2263           7 : F2xqM_indexrank(GEN x, GEN T) {
    2264           7 :   pari_sp av = avma;
    2265             :   long r;
    2266             :   GEN d;
    2267           7 :   init_indexrank(x);
    2268           7 :   d = F2xqM_gauss_pivot(x, T, &r);
    2269           7 :   set_avma(av); return indexrank0(lg(x) - 1, r, d);
    2270             : }
    2271             : 
    2272             : GEN
    2273           7 : FlxqM_indexrank(GEN x, GEN T, ulong p) {
    2274           7 :   pari_sp av = avma;
    2275             :   long r;
    2276             :   GEN d;
    2277           7 :   init_indexrank(x);
    2278           7 :   d = FlxqM_gauss_pivot(x, T, p, &r);
    2279           7 :   set_avma(av); return indexrank0(lg(x) - 1, r, d);
    2280             : }
    2281             : 
    2282             : GEN
    2283           7 : FqM_indexrank(GEN x, GEN T, GEN p) {
    2284           7 :   pari_sp av = avma;
    2285             :   long r;
    2286             :   GEN d;
    2287           7 :   init_indexrank(x);
    2288           7 :   d = FqM_gauss_pivot(x, T, p, &r);
    2289           7 :   set_avma(av); return indexrank0(lg(x) - 1, r, d);
    2290             : }
    2291             : 
    2292             : /*******************************************************************/
    2293             : /*                                                                 */
    2294             : /*                       Solve A*X=B (Gauss pivot)                 */
    2295             : /*                                                                 */
    2296             : /*******************************************************************/
    2297             : /* x ~ 0 compared to reference y */
    2298             : int
    2299      621288 : approx_0(GEN x, GEN y)
    2300             : {
    2301      621288 :   long tx = typ(x);
    2302      621288 :   if (tx == t_COMPLEX)
    2303         140 :     return approx_0(gel(x,1), y) && approx_0(gel(x,2), y);
    2304      621435 :   return gequal0(x) ||
    2305      432185 :          (tx == t_REAL && gexpo(y) - gexpo(x) > bit_prec(x));
    2306             : }
    2307             : /* x a column, x0 same column in the original input matrix (for reference),
    2308             :  * c list of pivots so far */
    2309             : static long
    2310      642407 : gauss_get_pivot_max(GEN X, GEN X0, long ix, GEN c)
    2311             : {
    2312      642407 :   GEN p, r, x = gel(X,ix), x0 = gel(X0,ix);
    2313      642407 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2314      642407 :   if (c)
    2315             :   {
    2316      120128 :     for (i=1; i<lx; i++)
    2317       74492 :       if (!c[i])
    2318             :       {
    2319       38168 :         long e = gexpo(gel(x,i));
    2320       38168 :         if (e > ex) { ex = e; k = i; }
    2321             :       }
    2322             :   }
    2323             :   else
    2324             :   {
    2325     2082264 :     for (i=ix; i<lx; i++)
    2326             :     {
    2327     1485493 :       long e = gexpo(gel(x,i));
    2328     1485493 :       if (e > ex) { ex = e; k = i; }
    2329             :     }
    2330             :   }
    2331      642407 :   if (!k) return lx;
    2332      621113 :   p = gel(x,k);
    2333      621113 :   r = gel(x0,k); if (isrationalzero(r)) r = x0;
    2334      621113 :   return approx_0(p, r)? lx: k;
    2335             : }
    2336             : static long
    2337       66423 : gauss_get_pivot_padic(GEN X, GEN p, long ix, GEN c)
    2338             : {
    2339       66423 :   GEN x = gel(X, ix);
    2340       66423 :   long i, k = 0, ex = (long)HIGHVALPBIT, lx = lg(x);
    2341       66423 :   if (c)
    2342             :   {
    2343         504 :     for (i=1; i<lx; i++)
    2344         378 :       if (!c[i] && !gequal0(gel(x,i)))
    2345             :       {
    2346         245 :         long e = gvaluation(gel(x,i), p);
    2347         245 :         if (e < ex) { ex = e; k = i; }
    2348             :       }
    2349             :   }
    2350             :   else
    2351             :   {
    2352      464464 :     for (i=ix; i<lx; i++)
    2353      398167 :       if (!gequal0(gel(x,i)))
    2354             :       {
    2355      188709 :         long e = gvaluation(gel(x,i), p);
    2356      188709 :         if (e < ex) { ex = e; k = i; }
    2357             :       }
    2358             :   }
    2359       66423 :   return k? k: lx;
    2360             : }
    2361             : static long
    2362        4543 : gauss_get_pivot_NZ(GEN X, GEN x0/*unused*/, long ix, GEN c)
    2363             : {
    2364        4543 :   GEN x = gel(X, ix);
    2365        4543 :   long i, lx = lg(x);
    2366             :   (void)x0;
    2367        4543 :   if (c)
    2368             :   {
    2369       11634 :     for (i=1; i<lx; i++)
    2370       10780 :       if (!c[i] && !gequal0(gel(x,i))) return i;
    2371             :   }
    2372             :   else
    2373             :   {
    2374        2730 :     for (i=ix; i<lx; i++)
    2375        2716 :       if (!gequal0(gel(x,i))) return i;
    2376             :   }
    2377         868 :   return lx;
    2378             : }
    2379             : 
    2380             : /* Return pivot seeking function appropriate for the domain of the RgM x
    2381             :  * (first non zero pivot, maximal pivot...)
    2382             :  * x0 is a reference point used when guessing whether x[i,j] ~ 0
    2383             :  * (iff x[i,j] << x0[i,j]); typical case: mateigen, Gauss pivot on x - vp.Id,
    2384             :  * but use original x when deciding whether a prospective pivot is non-0 */
    2385             : static pivot_fun
    2386      211687 : get_pivot_fun(GEN x, GEN x0, GEN *data)
    2387             : {
    2388      211687 :   long i, j, hx, lx = lg(x);
    2389      211687 :   int res = t_INT;
    2390      211687 :   GEN p = NULL;
    2391             : 
    2392      211687 :   *data = NULL;
    2393      211687 :   if (lx == 1) return &gauss_get_pivot_NZ;
    2394      211652 :   hx = lgcols(x);
    2395      936755 :   for (j=1; j<lx; j++)
    2396             :   {
    2397      725145 :     GEN xj = gel(x,j);
    2398     3949009 :     for (i=1; i<hx; i++)
    2399             :     {
    2400     3223906 :       GEN c = gel(xj,i);
    2401     3223906 :       switch(typ(c))
    2402             :       {
    2403             :         case t_REAL:
    2404     1757814 :           res = t_REAL;
    2405     1757814 :           break;
    2406             :         case t_COMPLEX:
    2407         364 :           if (typ(gel(c,1)) == t_REAL || typ(gel(c,2)) == t_REAL) res = t_REAL;
    2408         364 :           break;
    2409             :         case t_INT: case t_INTMOD: case t_FRAC: case t_FFELT: case t_QUAD:
    2410             :         case t_POLMOD: /* exact types */
    2411     1299317 :           break;
    2412             :         case t_PADIC:
    2413      166369 :           p = gel(c,2);
    2414      166369 :           res = t_PADIC;
    2415      166369 :           break;
    2416          42 :         default: return &gauss_get_pivot_NZ;
    2417             :       }
    2418             :     }
    2419             :   }
    2420      211610 :   switch(res)
    2421             :   {
    2422      201047 :     case t_REAL: *data = x0; return &gauss_get_pivot_max;
    2423        8694 :     case t_PADIC: *data = p; return &gauss_get_pivot_padic;
    2424        1869 :     default: return &gauss_get_pivot_NZ;
    2425             :   }
    2426             : }
    2427             : 
    2428             : static GEN
    2429      203083 : get_col(GEN a, GEN b, GEN p, long li)
    2430             : {
    2431      203083 :   GEN u = cgetg(li+1,t_COL);
    2432             :   long i, j;
    2433             : 
    2434      203083 :   gel(u,li) = gdiv(gel(b,li), p);
    2435      843773 :   for (i=li-1; i>0; i--)
    2436             :   {
    2437      640690 :     pari_sp av = avma;
    2438      640690 :     GEN m = gel(b,i);
    2439      640690 :     for (j=i+1; j<=li; j++) m = gsub(m, gmul(gcoeff(a,i,j), gel(u,j)));
    2440      640690 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(a,i,i)));
    2441             :   }
    2442      203083 :   return u;
    2443             : }
    2444             : 
    2445             : /* bk -= m * bi */
    2446             : static void
    2447     3352553 : _submul(GEN b, long k, long i, GEN m)
    2448             : {
    2449     3352553 :   gel(b,k) = gsub(gel(b,k), gmul(m, gel(b,i)));
    2450     3352553 : }
    2451             : static int
    2452      736799 : init_gauss(GEN a, GEN *b, long *aco, long *li, int *iscol)
    2453             : {
    2454      736799 :   *iscol = *b ? (typ(*b) == t_COL): 0;
    2455      736799 :   *aco = lg(a) - 1;
    2456      736799 :   if (!*aco) /* a empty */
    2457             :   {
    2458          70 :     if (*b && lg(*b) != 1) pari_err_DIM("gauss");
    2459          70 :     *li = 0; return 0;
    2460             :   }
    2461      736729 :   *li = nbrows(a);
    2462      736729 :   if (*li < *aco) pari_err_INV("gauss [no left inverse]", a);
    2463      736729 :   if (*b)
    2464             :   {
    2465      722663 :     switch(typ(*b))
    2466             :     {
    2467             :       case t_MAT:
    2468       18920 :         if (lg(*b) == 1) return 0;
    2469       18920 :         *b = RgM_shallowcopy(*b);
    2470       18920 :         break;
    2471             :       case t_COL:
    2472      703743 :         *b = mkmat( leafcopy(*b) );
    2473      703743 :         break;
    2474           0 :       default: pari_err_TYPE("gauss",*b);
    2475             :     }
    2476      722663 :     if (nbrows(*b) != *li) pari_err_DIM("gauss");
    2477             :   }
    2478             :   else
    2479       14066 :     *b = matid(*li);
    2480      736729 :   return 1;
    2481             : }
    2482             : 
    2483             : static GEN
    2484       32449 : RgM_inv_QM(GEN M)
    2485             : {
    2486       32449 :   pari_sp av = avma;
    2487       32449 :   GEN den, cM, pM = Q_primitive_part(M, &cM);
    2488       32449 :   GEN b = ZM_inv(pM, &den);
    2489       32435 :   if (!b) return gc_NULL(av);
    2490       32428 :   if (cM) den = gmul(den, cM);
    2491       32428 :   if (!gequal1(den)) b = ZM_Q_mul(b, ginv(den));
    2492       32428 :   return gerepileupto(av, b);
    2493             : }
    2494             : 
    2495             : static GEN
    2496         112 : RgM_inv_FpM(GEN a, GEN p)
    2497             : {
    2498             :   ulong pp;
    2499         112 :   a = RgM_Fp_init(a, p, &pp);
    2500         112 :   switch(pp)
    2501             :   {
    2502             :   case 0:
    2503          35 :     a = FpM_inv(a,p);
    2504          35 :     if (a) a = FpM_to_mod(a, p);
    2505          35 :     break;
    2506             :   case 2:
    2507          35 :     a = F2m_inv(a);
    2508          35 :     if (a) a = F2m_to_mod(a);
    2509          35 :     break;
    2510             :   default:
    2511          42 :     a = Flm_inv_sp(a, NULL, pp);
    2512          42 :     if (a) a = Flm_to_mod(a, pp);
    2513             :   }
    2514         112 :   return a;
    2515             : }
    2516             : 
    2517             : static GEN
    2518          42 : RgM_inv_FqM(GEN x, GEN pol, GEN p)
    2519             : {
    2520          42 :   pari_sp av = avma;
    2521          42 :   GEN b, T = RgX_to_FpX(pol, p);
    2522          42 :   if (signe(T) == 0) pari_err_OP("^",x,gen_m1);
    2523          42 :   b = FqM_inv(RgM_to_FqM(x, T, p), T, p);
    2524          42 :   if (!b) return gc_NULL(av);
    2525          28 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    2526             : }
    2527             : 
    2528             : #define code(t1,t2) ((t1 << 6) | t2)
    2529             : static GEN
    2530       56735 : RgM_inv_fast(GEN x)
    2531             : {
    2532             :   GEN p, pol;
    2533             :   long pa;
    2534       56735 :   long t = RgM_type(x, &p,&pol,&pa);
    2535       56735 :   switch(t)
    2536             :   {
    2537             :     case t_INT:    /* Fall back */
    2538       32449 :     case t_FRAC:   return RgM_inv_QM(x);
    2539         147 :     case t_FFELT:  return FFM_inv(x, pol);
    2540         112 :     case t_INTMOD: return RgM_inv_FpM(x, p);
    2541             :     case code(t_POLMOD, t_INTMOD):
    2542          42 :                    return RgM_inv_FqM(x, pol, p);
    2543       23985 :     default:       return gen_0;
    2544             :   }
    2545             : }
    2546             : #undef code
    2547             : 
    2548             : static GEN
    2549          49 : RgM_RgC_solve_FpC(GEN a, GEN b, GEN p)
    2550             : {
    2551          49 :   pari_sp av = avma;
    2552             :   ulong pp;
    2553          49 :   a = RgM_Fp_init(a, p, &pp);
    2554          49 :   switch(pp)
    2555             :   {
    2556             :   case 0:
    2557          14 :     b = RgC_to_FpC(b, p);
    2558          14 :     a = FpM_FpC_gauss(a,b,p);
    2559          14 :     return a ? gerepileupto(av, FpC_to_mod(a, p)): NULL;
    2560             :   case 2:
    2561          14 :     b = RgV_to_F2v(b);
    2562          14 :     a = F2m_F2c_gauss(a,b);
    2563          14 :     return a ? gerepileupto(av, F2c_to_mod(a)): NULL;
    2564             :   default:
    2565          21 :     b = RgV_to_Flv(b, pp);
    2566          21 :     a = Flm_Flc_gauss(a, b, pp);
    2567          21 :     return a ? gerepileupto(av, Flc_to_mod(a, pp)): NULL;
    2568             :   }
    2569             : }
    2570             : 
    2571             : static GEN
    2572          98 : RgM_solve_FpM(GEN a, GEN b, GEN p)
    2573             : {
    2574          98 :   pari_sp av = avma;
    2575             :   ulong pp;
    2576          98 :   a = RgM_Fp_init(a, p, &pp);
    2577          98 :   switch(pp)
    2578             :   {
    2579             :   case 0:
    2580          35 :     b = RgM_to_FpM(b, p);
    2581          35 :     a = FpM_gauss(a,b,p);
    2582          35 :     return a ? gerepileupto(av, FpM_to_mod(a, p)): NULL;
    2583             :   case 2:
    2584          21 :     b = RgM_to_F2m(b);
    2585          21 :     a = F2m_gauss(a,b);
    2586          21 :     return a ? gerepileupto(av, F2m_to_mod(a)): NULL;
    2587             :   default:
    2588          42 :     b = RgM_to_Flm(b, pp);
    2589          42 :     a = Flm_gauss(a,b,pp);
    2590          42 :     return a ? gerepileupto(av, Flm_to_mod(a, pp)): NULL;
    2591             :   }
    2592             : }
    2593             : 
    2594             : /* Gaussan Elimination. If a is square, return a^(-1)*b;
    2595             :  * if a has more rows than columns and b is NULL, return c such that c a = Id.
    2596             :  * a is a (not necessarily square) matrix
    2597             :  * b is a matrix or column vector, NULL meaning: take the identity matrix,
    2598             :  *   effectively returning the inverse of a
    2599             :  * If a and b are empty, the result is the empty matrix.
    2600             :  *
    2601             :  * li: number of rows of a and b
    2602             :  * aco: number of columns of a
    2603             :  * bco: number of columns of b (if matrix)
    2604             :  */
    2605             : static GEN
    2606      284461 : RgM_solve_basecase(GEN a, GEN b)
    2607             : {
    2608      284461 :   pari_sp av = avma;
    2609             :   long i, j, k, li, bco, aco;
    2610             :   int iscol;
    2611             :   pivot_fun pivot;
    2612             :   GEN p, u, data;
    2613             : 
    2614      284461 :   set_avma(av);
    2615             : 
    2616      284461 :   if (lg(a)-1 == 2 && nbrows(a) == 2) {
    2617             :     /* 2x2 matrix, start by inverting a */
    2618      105755 :     GEN u = gcoeff(a,1,1), v = gcoeff(a,1,2);
    2619      105755 :     GEN w = gcoeff(a,2,1), x = gcoeff(a,2,2);
    2620      105755 :     GEN D = gsub(gmul(u,x), gmul(v,w)), ainv;
    2621      105755 :     if (gequal0(D)) return NULL;
    2622      105755 :     ainv = mkmat2(mkcol2(x, gneg(w)), mkcol2(gneg(v), u));
    2623      105755 :     ainv = gmul(ainv, ginv(D));
    2624      105755 :     if (b) ainv = gmul(ainv, b);
    2625      105755 :     return gerepileupto(av, ainv);
    2626             :   }
    2627             : 
    2628      178706 :   if (!init_gauss(a, &b, &aco, &li, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    2629      178706 :   pivot = get_pivot_fun(a, a, &data);
    2630      178706 :   a = RgM_shallowcopy(a);
    2631      178706 :   bco = lg(b)-1;
    2632      178706 :   if(DEBUGLEVEL>4) err_printf("Entering gauss\n");
    2633             : 
    2634      178706 :   p = NULL; /* gcc -Wall */
    2635      590758 :   for (i=1; i<=aco; i++)
    2636             :   {
    2637             :     /* k is the line where we find the pivot */
    2638      590758 :     k = pivot(a, data, i, NULL);
    2639      590758 :     if (k > li) return NULL;
    2640      590744 :     if (k != i)
    2641             :     { /* exchange the lines s.t. k = i */
    2642      131014 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    2643      131014 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    2644             :     }
    2645      590744 :     p = gcoeff(a,i,i);
    2646      590744 :     if (i == aco) break;
    2647             : 
    2648     1286398 :     for (k=i+1; k<=li; k++)
    2649             :     {
    2650      874346 :       GEN m = gcoeff(a,k,i);
    2651      874346 :       if (!gequal0(m))
    2652             :       {
    2653      693631 :         m = gdiv(m,p);
    2654      693631 :         for (j=i+1; j<=aco; j++) _submul(gel(a,j),k,i,m);
    2655      693631 :         for (j=1;   j<=bco; j++) _submul(gel(b,j),k,i,m);
    2656             :       }
    2657             :     }
    2658      412052 :     if (gc_needed(av,1))
    2659             :     {
    2660          12 :       if(DEBUGMEM>1) pari_warn(warnmem,"gauss. i=%ld",i);
    2661          12 :       gerepileall(av,2, &a,&b);
    2662             :     }
    2663             :   }
    2664             : 
    2665      178692 :   if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
    2666      178692 :   u = cgetg(bco+1,t_MAT);
    2667      178692 :   for (j=1; j<=bco; j++) gel(u,j) = get_col(a,gel(b,j),p,aco);
    2668      178692 :   return gerepilecopy(av, iscol? gel(u,1): u);
    2669             : }
    2670             : 
    2671             : static GEN
    2672      271083 : RgM_RgC_solve_fast(GEN x, GEN y)
    2673             : {
    2674             :   GEN p, pol;
    2675             :   long pa;
    2676      271083 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    2677      271083 :   switch(t)
    2678             :   {
    2679       14630 :     case t_INT:    return ZM_gauss(x, y);
    2680          42 :     case t_FRAC:   return QM_gauss(x, y);
    2681          49 :     case t_INTMOD: return RgM_RgC_solve_FpC(x, y, p);
    2682          56 :     case t_FFELT:  return FFM_FFC_gauss(x, y, pol);
    2683      256306 :     default:       return gen_0;
    2684             :   }
    2685             : }
    2686             : 
    2687             : static GEN
    2688        4380 : RgM_solve_fast(GEN x, GEN y)
    2689             : {
    2690             :   GEN p, pol;
    2691             :   long pa;
    2692        4380 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    2693        4380 :   switch(t)
    2694             :   {
    2695          42 :     case t_INT:    return ZM_gauss(x, y);
    2696           7 :     case t_FRAC:   return QM_gauss(x, y);
    2697          98 :     case t_INTMOD: return RgM_solve_FpM(x, y, p);
    2698          63 :     case t_FFELT:  return FFM_gauss(x, y, pol);
    2699        4170 :     default:       return gen_0;
    2700             :   }
    2701             : }
    2702             : 
    2703             : GEN
    2704      275463 : RgM_solve(GEN a, GEN b)
    2705             : {
    2706      275463 :   pari_sp av = avma;
    2707             :   GEN u;
    2708      275463 :   if (!b) return RgM_inv(a);
    2709      275463 :   u = typ(b)==t_MAT ? RgM_solve_fast(a, b): RgM_RgC_solve_fast(a, b);
    2710      275463 :   if (!u) { set_avma(av); return u; }
    2711      275365 :   if (u != gen_0) return u;
    2712      260476 :   return RgM_solve_basecase(a, b);
    2713             : }
    2714             : 
    2715             : GEN
    2716       56735 : RgM_inv(GEN a)
    2717             : {
    2718       56735 :   GEN b = RgM_inv_fast(a);
    2719       56721 :   return b==gen_0? RgM_solve_basecase(a, NULL): b;
    2720             : }
    2721             : 
    2722             : /* assume dim A >= 1, A invertible + upper triangular  */
    2723             : static GEN
    2724      362846 : RgM_inv_upper_ind(GEN A, long index)
    2725             : {
    2726      362846 :   long n = lg(A)-1, i = index, j;
    2727      362846 :   GEN u = zerocol(n);
    2728      362846 :   gel(u,i) = ginv(gcoeff(A,i,i));
    2729     1427934 :   for (i--; i>0; i--)
    2730             :   {
    2731     1065088 :     pari_sp av = avma;
    2732     1065088 :     GEN m = gneg(gmul(gcoeff(A,i,i+1),gel(u,i+1))); /* j = i+1 */
    2733     1065088 :     for (j=i+2; j<=n; j++) m = gsub(m, gmul(gcoeff(A,i,j),gel(u,j)));
    2734     1065088 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(A,i,i)));
    2735             :   }
    2736      362846 :   return u;
    2737             : }
    2738             : GEN
    2739       75285 : RgM_inv_upper(GEN A)
    2740             : {
    2741             :   long i, l;
    2742       75285 :   GEN B = cgetg_copy(A, &l);
    2743       75285 :   for (i = 1; i < l; i++) gel(B,i) = RgM_inv_upper_ind(A, i);
    2744       75285 :   return B;
    2745             : }
    2746             : 
    2747             : static GEN
    2748     1018839 : split_realimag_col(GEN z, long r1, long r2)
    2749             : {
    2750     1018839 :   long i, ru = r1+r2;
    2751     1018839 :   GEN x = cgetg(ru+r2+1,t_COL), y = x + r2;
    2752     3043537 :   for (i=1; i<=r1; i++) {
    2753     2024698 :     GEN a = gel(z,i);
    2754     2024698 :     if (typ(a) == t_COMPLEX) a = gel(a,1); /* paranoia: a should be real */
    2755     2024698 :     gel(x,i) = a;
    2756             :   }
    2757     1732546 :   for (   ; i<=ru; i++) {
    2758      713707 :     GEN b, a = gel(z,i);
    2759      713707 :     if (typ(a) == t_COMPLEX) { b = gel(a,2); a = gel(a,1); } else b = gen_0;
    2760      713707 :     gel(x,i) = a;
    2761      713707 :     gel(y,i) = b;
    2762             :   }
    2763     1018839 :   return x;
    2764             : }
    2765             : GEN
    2766      533225 : split_realimag(GEN x, long r1, long r2)
    2767             : {
    2768             :   long i,l; GEN y;
    2769      533225 :   if (typ(x) == t_COL) return split_realimag_col(x,r1,r2);
    2770      262399 :   y = cgetg_copy(x, &l);
    2771      262399 :   for (i=1; i<l; i++) gel(y,i) = split_realimag_col(gel(x,i), r1, r2);
    2772      262399 :   return y;
    2773             : }
    2774             : 
    2775             : /* assume M = (r1+r2) x (r1+2r2) matrix and y compatible vector or matrix
    2776             :  * r1 first lines of M,y are real. Solve the system obtained by splitting
    2777             :  * real and imaginary parts. */
    2778             : GEN
    2779      257305 : RgM_solve_realimag(GEN M, GEN y)
    2780             : {
    2781      257305 :   long l = lg(M), r2 = l - lgcols(M), r1 = l-1 - 2*r2;
    2782      257305 :   return RgM_solve(split_realimag(M, r1,r2),
    2783             :                    split_realimag(y, r1,r2));
    2784             : }
    2785             : 
    2786             : GEN
    2787         420 : gauss(GEN a, GEN b)
    2788             : {
    2789             :   GEN z;
    2790         420 :   long t = typ(b);
    2791         420 :   if (typ(a)!=t_MAT) pari_err_TYPE("gauss",a);
    2792         420 :   if (t!=t_COL && t!=t_MAT) pari_err_TYPE("gauss",b);
    2793         420 :   z = RgM_solve(a,b);
    2794         420 :   if (!z) pari_err_INV("gauss",a);
    2795         315 :   return z;
    2796             : }
    2797             : 
    2798             : static GEN
    2799      557995 : ZlM_gauss_ratlift(GEN a, GEN b, ulong p, long e, GEN C)
    2800             : {
    2801      557995 :   pari_sp av = avma, av2;
    2802             :   GEN bb, xi, xb, pi, P, B, r;
    2803      557995 :   long i, k = 2;
    2804      557995 :   if (!C) {
    2805           0 :     C = Flm_inv(ZM_to_Flm(a, p), p);
    2806           0 :     if (!C) pari_err_INV("ZlM_gauss", a);
    2807             :   }
    2808      557995 :   pi = P = utoipos(p);
    2809      557995 :   av2 = avma;
    2810      557995 :   xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    2811      557995 :   xb = Flm_to_ZM(xi);
    2812      557995 :   bb = b;
    2813     1183676 :   for (i = 2; i <= e; i++)
    2814             :   {
    2815      685652 :     bb = ZM_Z_divexact(ZM_sub(bb, ZM_nm_mul(a, xi)), P);
    2816      685652 :     if (gc_needed(av,2))
    2817             :     {
    2818          35 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZlM_gauss. i=%ld/%ld",i,e);
    2819          35 :       gerepileall(av2,3, &pi,&bb,&xb);
    2820             :     }
    2821      685652 :     xi = Flm_mul(C, ZM_to_Flm(bb, p), p);
    2822      685652 :     xb = ZM_add(xb, nm_Z_mul(xi, pi));
    2823      685652 :     pi = muliu(pi, p); /* = p^(i-1) */
    2824      685652 :     if (i==k && i < e)
    2825             :     {
    2826      203690 :       k *= 2;
    2827      203690 :       B = sqrti(shifti(pi,-1));
    2828      203690 :       r = FpM_ratlift(xb, pi, B, B, NULL);
    2829      203690 :       if (r)
    2830             :       {
    2831      101923 :         GEN dr, nr = Q_remove_denom(r,&dr);
    2832      101923 :         if (ZM_equal(ZM_mul(a,nr), dr? ZM_Z_mul(b,dr): b))
    2833             :         {
    2834       59971 :           if (DEBUGLEVEL>=4)
    2835           0 :             err_printf("ZlM_gauss: early solution: %ld/%ld\n",i,e);
    2836       59971 :           return gerepilecopy(av, r);
    2837             :         }
    2838             :       }
    2839             :     }
    2840             :   }
    2841      498024 :   B = sqrti(shifti(pi,-1));
    2842      498024 :   return gerepileupto(av, FpM_ratlift(xb, pi, B, B, NULL));
    2843             : }
    2844             : 
    2845             : /* Dixon p-adic lifting algorithm.
    2846             :  * Numer. Math. 40, 137-141 (1982), DOI: 10.1007/BF01459082 */
    2847             : GEN
    2848      558093 : ZM_gauss(GEN a, GEN b0)
    2849             : {
    2850      558093 :   pari_sp av = avma, av2;
    2851             :   int iscol;
    2852             :   long n, ncol, i, m, elim;
    2853             :   ulong p;
    2854      558093 :   GEN C, delta, nb, nmin, res, b = b0;
    2855             :   forprime_t S;
    2856             : 
    2857      558093 :   if (!init_gauss(a, &b, &n, &ncol, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    2858      558023 :   nb = gen_0; ncol = lg(b);
    2859     1182070 :   for (i = 1; i < ncol; i++)
    2860             :   {
    2861      624047 :     GEN ni = gnorml2(gel(b, i));
    2862      624047 :     if (cmpii(nb, ni) < 0) nb = ni;
    2863             :   }
    2864      558023 :   if (!signe(nb)) { set_avma(av); return zerocol(n); }
    2865      558023 :   delta = gen_1; nmin = nb;
    2866     2238604 :   for (i = 1; i <= n; i++)
    2867             :   {
    2868     1680581 :     GEN ni = gnorml2(gel(a, i));
    2869     1680581 :     if (cmpii(ni, nmin) < 0)
    2870             :     {
    2871       28842 :       delta = mulii(delta, nmin); nmin = ni;
    2872             :     }
    2873             :     else
    2874     1651739 :       delta = mulii(delta, ni);
    2875             :   }
    2876      558023 :   if (!signe(nmin)) return NULL;
    2877      558002 :   elim = expi(delta)+1;
    2878      558002 :   av2 = avma;
    2879      558002 :   init_modular_big(&S);
    2880             :   for(;;)
    2881             :   {
    2882      558002 :     p = u_forprime_next(&S);
    2883      558002 :     C = Flm_inv_sp(ZM_to_Flm(a, p), NULL, p);
    2884      558002 :     if (C) break;
    2885           7 :     elim -= expu(p);
    2886           7 :     if (elim < 0) return NULL;
    2887           0 :     set_avma(av2);
    2888             :   }
    2889             :   /* N.B. Our delta/lambda are SQUARES of those in the paper
    2890             :    * log(delta lambda) / log p, where lambda is 3+sqrt(5) / 2,
    2891             :    * whose log is < 1, hence + 1 (to cater for rounding errors) */
    2892     1115990 :   m = (long)ceil((rtodbl(logr_abs(itor(delta,LOWDEFAULTPREC))) + 1)
    2893      557995 :                  / log((double)p));
    2894      557995 :   res = ZlM_gauss_ratlift(a, b, p, m, C);
    2895      557995 :   if (iscol) return gerepilecopy(av, gel(res, 1));
    2896       17228 :   return gerepileupto(av, res);
    2897             : }
    2898             : 
    2899             : /* same as above, M rational */
    2900             : GEN
    2901        1526 : QM_gauss(GEN M, GEN B)
    2902             : {
    2903        1526 :   pari_sp av = avma;
    2904        1526 :   long i, l = lg(M);
    2905        1526 :   GEN K, cB, N = cgetg_copy(M, &l), v = cgetg(l, t_VEC);
    2906        8764 :   for (i = 1; i < l; i++)
    2907        7238 :     gel(N,i) = Q_primitive_part(gel(M,i), &gel(v,i));
    2908        1526 :   B = Q_primitive_part(B, &cB);
    2909        1526 :   K = ZM_gauss(N, B); if (!K) { set_avma(av); return NULL; }
    2910        8743 :   for (i = 1; i < l; i++)
    2911             :   {
    2912        7224 :     GEN c, k = gel(K,i), d = gel(v,i);
    2913        7224 :     if (d)
    2914             :     {
    2915        5111 :       if (isintzero(d))
    2916             :       {
    2917           0 :         if (gequal0(k)) continue;
    2918           0 :         return NULL;
    2919             :       }
    2920        5111 :       d = inv_content(d);
    2921             :     }
    2922        7224 :     c = mul_content(cB, d);
    2923        7224 :     if (c) gel(K,i) = gmul(gel(K,i), c);
    2924             :   }
    2925        1519 :   return gerepilecopy(av, K);
    2926             : }
    2927             : 
    2928             : static GEN
    2929      137272 : ZM_inv_slice(GEN A, GEN P, GEN *mod)
    2930             : {
    2931      137272 :   pari_sp av = avma;
    2932      137272 :   long i, n = lg(P)-1;
    2933             :   GEN H, T;
    2934      137272 :   if (n == 1)
    2935             :   {
    2936      135440 :     ulong p = uel(P,1);
    2937      135440 :     GEN Hp, a = ZM_to_Flm(A, p);
    2938      135421 :     Hp = Flm_adjoint(a, p);
    2939      135436 :     Hp = gerepileupto(av, Flm_to_ZM(Hp));
    2940      135440 :     *mod = utoi(p); return Hp;
    2941             :   }
    2942        1832 :   T = ZV_producttree(P);
    2943        1832 :   A = ZM_nv_mod_tree(A, P, T);
    2944        1832 :   H = cgetg(n+1, t_VEC);
    2945        6483 :   for(i=1; i <= n; i++)
    2946        4651 :     gel(H,i) = Flm_adjoint(gel(A, i), uel(P,i));
    2947        1832 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    2948        1832 :   *mod = gmael(T, lg(T)-1, 1);
    2949        1832 :   gerepileall(av, 2, &H, mod);
    2950        1831 :   return H;
    2951             : }
    2952             : 
    2953             : static GEN
    2954      115113 : RgM_true_Hadamard(GEN a)
    2955             : {
    2956      115113 :   pari_sp av = avma;
    2957      115113 :   long n = lg(a)-1, i;
    2958             :   GEN B;
    2959      115113 :   if (n == 0) return gen_1;
    2960      115113 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    2961      115113 :   B = gnorml2(gel(a,1));
    2962      115113 :   for (i = 2; i <= n; i++) B = gmul(B, gnorml2(gel(a,i)));
    2963      115113 :   return gerepileuptoint(av, ceil_safe(sqrtr(B)));
    2964             : }
    2965             : 
    2966             : GEN
    2967      137271 : ZM_inv_worker(GEN P, GEN A)
    2968             : {
    2969      137271 :   GEN V = cgetg(3, t_VEC);
    2970      137272 :   gel(V,1) = ZM_inv_slice(A, P, &gel(V,2));
    2971      137271 :   return V;
    2972             : }
    2973             : 
    2974             : static GEN
    2975        5131 : ZM_inv0(GEN A, GEN *pden)
    2976             : {
    2977        5131 :   if (pden) *pden = gen_1;
    2978        5131 :   (void)A; return cgetg(1, t_MAT);
    2979             : }
    2980             : static GEN
    2981       29093 : ZM_inv1(GEN A, GEN *pden)
    2982             : {
    2983       29093 :   GEN a = gcoeff(A,1,1);
    2984       29093 :   long s = signe(a);
    2985       29093 :   if (!s) return NULL;
    2986       29093 :   if (pden) *pden = absi(a);
    2987       29093 :   retmkmat(mkcol(s == 1? gen_1: gen_m1));
    2988             : }
    2989             : static GEN
    2990       48122 : ZM_inv2(GEN A, GEN *pden)
    2991             : {
    2992             :   GEN a, b, c, d, D, cA;
    2993             :   long s;
    2994       48122 :   A = Q_primitive_part(A, &cA);
    2995       48122 :   a = gcoeff(A,1,1); b = gcoeff(A,1,2);
    2996       48122 :   c = gcoeff(A,2,1); d = gcoeff(A,2,2);
    2997       48122 :   D = subii(mulii(a,d), mulii(b,c)); /* left on stack */
    2998       48122 :   s = signe(D);
    2999       48122 :   if (!s) return NULL;
    3000       48122 :   if (s < 0) D = negi(D);
    3001       48122 :   if (pden) *pden = mul_denom(D, cA);
    3002       48122 :   if (s > 0)
    3003       26432 :     retmkmat2(mkcol2(icopy(d), negi(c)), mkcol2(negi(b), icopy(a)));
    3004             :   else
    3005       21690 :     retmkmat2(mkcol2(negi(d), icopy(c)), mkcol2(icopy(b), negi(a)));
    3006             : }
    3007             : 
    3008             : /* to be used when denom(M^(-1)) << det(M) and a sharp multiple is
    3009             :  * not available. Return H primitive such that M*H = den*Id */
    3010             : GEN
    3011           0 : ZM_inv_ratlift(GEN M, GEN *pden)
    3012             : {
    3013           0 :   pari_sp av2, av = avma;
    3014             :   GEN Hp, q, H;
    3015             :   ulong p;
    3016           0 :   long m = lg(M)-1;
    3017             :   forprime_t S;
    3018             :   pari_timer ti;
    3019             : 
    3020           0 :   if (m == 0) return ZM_inv0(M,pden);
    3021           0 :   if (m == 1 && nbrows(M)==1) return ZM_inv1(M,pden);
    3022           0 :   if (m == 2 && nbrows(M)==2) return ZM_inv2(M,pden);
    3023             : 
    3024           0 :   if (DEBUGLEVEL>5) timer_start(&ti);
    3025           0 :   init_modular_big(&S);
    3026           0 :   av2 = avma;
    3027           0 :   H = NULL;
    3028           0 :   while ((p = u_forprime_next(&S)))
    3029             :   {
    3030             :     GEN Mp, B, Hr;
    3031           0 :     Mp = ZM_to_Flm(M,p);
    3032           0 :     Hp = Flm_inv_sp(Mp, NULL, p);
    3033           0 :     if (!Hp) continue;
    3034           0 :     if (!H)
    3035             :     {
    3036           0 :       H = ZM_init_CRT(Hp, p);
    3037           0 :       q = utoipos(p);
    3038             :     }
    3039             :     else
    3040           0 :       ZM_incremental_CRT(&H, Hp, &q, p);
    3041           0 :     B = sqrti(shifti(q,-1));
    3042           0 :     Hr = FpM_ratlift(H,q,B,B,NULL);
    3043           0 :     if (DEBUGLEVEL>5)
    3044           0 :       timer_printf(&ti,"ZM_inv mod %lu (ratlift=%ld)", p,!!Hr);
    3045           0 :     if (Hr) {/* DONE ? */
    3046           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    3047           0 :       if (ZM_isscalar(ZM_mul(Hl, M), *pden)) { H = Hl; break; }
    3048             :     }
    3049             : 
    3050           0 :     if (gc_needed(av,2))
    3051             :     {
    3052           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_inv_ratlift");
    3053           0 :       gerepileall(av2, 2, &H, &q);
    3054             :     }
    3055             :   }
    3056           0 :   if (!*pden) *pden = gen_1;
    3057           0 :   gerepileall(av, 2, &H, pden);
    3058           0 :   return H;
    3059             : }
    3060             : 
    3061             : static GEN
    3062      122662 : ZM_adj_ratlift(GEN A, GEN H, GEN mod)
    3063             : {
    3064             :   GEN B;
    3065      122662 :   GEN D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3066      122662 :   GEN g = gcdii(D, mod);
    3067      122662 :   if (!equali1(g))
    3068             :   {
    3069          14 :     mod = diviiexact(mod, g);
    3070          14 :     H = FpM_red(H, mod);
    3071             :   }
    3072      122662 :   D = Fp_inv(Fp_red(D, mod), mod);
    3073      122662 :   H = FpM_Fp_mul(H, D, mod);
    3074      122662 :   B = sqrti(shifti(mod,-1));
    3075      122662 :   return FpM_ratlift(H, mod, B, B, NULL);
    3076             : }
    3077             : 
    3078             : GEN
    3079      197466 : ZM_inv(GEN A, GEN *pden)
    3080             : {
    3081      197466 :   pari_sp av = avma;
    3082      197466 :   long m = lg(A)-1, n, k1 = 1, k2;
    3083      197466 :   GEN H = NULL, D, H1 = NULL, mod1 = NULL, worker;
    3084             :   ulong bnd, mask;
    3085             :   forprime_t S;
    3086             :   pari_timer ti;
    3087             : 
    3088      197466 :   if (m == 0) return ZM_inv0(A,pden);
    3089      192335 :   if (pden) *pden = gen_1;
    3090      192335 :   if (nbrows(A) < m) return NULL;
    3091      192328 :   if (m == 1 && nbrows(A)==1) return ZM_inv1(A,pden);
    3092      163235 :   if (m == 2 && nbrows(A)==2) return ZM_inv2(A,pden);
    3093             : 
    3094      115113 :   if (DEBUGLEVEL>=5) timer_start(&ti);
    3095      115113 :   init_modular_big(&S);
    3096      115113 :   bnd = expi(RgM_true_Hadamard(A));
    3097      115113 :   worker = snm_closure(is_entry("_ZM_inv_worker"), mkvec(A));
    3098      115113 :   gen_inccrt("ZM_inv_r", worker, NULL, k1, m, &S, &H1, &mod1, nmV_chinese_center, FpM_center);
    3099      115113 :   n = (bnd+1)/expu(S.p)+1;
    3100      115113 :   if (DEBUGLEVEL>=5) timer_printf(&ti,"inv (%ld/%ld primes)", k1, n);
    3101      115113 :   mask = quadratic_prec_mask(n);
    3102      115113 :   for (k2 = 0;;)
    3103       16645 :   {
    3104             :     GEN Hr;
    3105      131758 :     if (k2 > 0)
    3106             :     {
    3107       14071 :       gen_inccrt("ZM_inv_r", worker, NULL, k2, m, &S, &H1, &mod1,nmV_chinese_center,FpM_center);
    3108       14071 :       k1 += k2;
    3109       14071 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"CRT (%ld/%ld primes)", k1, n);
    3110             :     }
    3111      131758 :     if (mask == 1) break;
    3112      122662 :     k2 = (mask&1UL) ? k1-1: k1;
    3113      122662 :     mask >>= 1;
    3114             : 
    3115      122662 :     Hr = ZM_adj_ratlift(A, H1, mod1);
    3116      122662 :     if (DEBUGLEVEL>=5) timer_printf(&ti,"ratlift (%ld/%ld primes)", k1, n);
    3117      122662 :     if (Hr) {/* DONE ? */
    3118             :       GEN den;
    3119      107672 :       GEN Hl = Q_remove_denom(Hr, &den);
    3120      107672 :       GEN R = ZM_mul(Hl, A);
    3121      107672 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"mult (%ld/%ld primes)", k1, n);
    3122      107672 :       den = den ? den: gen_1;
    3123      107672 :       if (den)
    3124             :       {
    3125      107672 :         if (ZM_isscalar(R, den))
    3126             :         {
    3127      106017 :           H = Hl;
    3128      106017 :           if (pden) *pden = den;
    3129      212034 :           break;
    3130             :         }
    3131             :       }
    3132             :       else
    3133           0 :         if (ZM_isidentity(R)) { H=Hl; break; }
    3134             :     }
    3135             :   }
    3136      115113 :   if (!H)
    3137             :   {
    3138             :     GEN d;
    3139        9096 :     H = H1;
    3140        9096 :     D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3141        9096 :     if (signe(D)==0) pari_err_INV("ZM_inv", A);
    3142        9082 :     d = gcdii(Q_content_safe(H), D);
    3143        9082 :     if (signe(D) < 0) d = negi(d);
    3144        9082 :     if (!equali1(d))
    3145             :     {
    3146        5218 :       H = ZM_Z_divexact(H, d);
    3147        5218 :       D = diviiexact(D, d);
    3148             :     }
    3149        9082 :     if (pden) *pden = D;
    3150             :   }
    3151      115099 :   gerepileall(av, pden? 2: 1, &H, pden);
    3152      115099 :   return H;
    3153             : }
    3154             : 
    3155             : /* same as above, M rational */
    3156             : GEN
    3157        1694 : QM_inv(GEN M)
    3158             : {
    3159        1694 :   pari_sp av = avma;
    3160             :   GEN den, cM, K;
    3161        1694 :   M = Q_primitive_part(M, &cM);
    3162        1694 :   K = ZM_inv(M, &den);
    3163        1694 :   if (!K) return gc_NULL(av);
    3164        1694 :   cM = inv_content(mul_content(cM, den));
    3165        1694 :   if (cM) K = RgM_Rg_div(K, cM);
    3166        1694 :   return gerepileupto(av, K);
    3167             : }
    3168             : 
    3169             : static GEN
    3170       87931 : ZM_ker_filter(GEN A, GEN P)
    3171             : {
    3172       87931 :   long i, j, l = lg(A), n = 1, d = lg(gmael(A,1,1));
    3173       87931 :   GEN B, Q, D = gmael(A,1,2);
    3174      176626 :   for (i=2; i<l; i++)
    3175             :   {
    3176       88695 :     GEN Di = gmael(A,i,2);
    3177       88695 :     long di = lg(gmael(A,i,1));
    3178       88695 :     int c = vecsmall_lexcmp(D, Di);
    3179       88695 :     if (di==d && c==0) n++;
    3180       45588 :     else if (d > di || (di==d && c>0))
    3181       37680 :     { n = 1; d = di; D = Di; }
    3182             :   }
    3183       87931 :   B = cgetg(n+1, t_VEC);
    3184       87931 :   Q = cgetg(n+1, typ(P));
    3185      264557 :   for (i=1, j=1; i<l; i++)
    3186             :   {
    3187      176626 :     if (lg(gmael(A,i,1))==d &&  vecsmall_lexcmp(D, gmael(A,i,2))==0)
    3188             :     {
    3189      131038 :       gel(B,j) = gmael(A,i,1);
    3190      131038 :       Q[j] = P[i];
    3191      131038 :       j++;
    3192             :     }
    3193             :   }
    3194       87931 :   return mkvec3(B,Q,D);
    3195             : }
    3196             : 
    3197             : static GEN
    3198       85213 : ZM_ker_chinese(GEN A, GEN P, GEN *mod)
    3199             : {
    3200       85213 :   GEN BQD = ZM_ker_filter(A, P);
    3201       85213 :   return mkvec2(nmV_chinese_center(gel(BQD,1), gel(BQD,2), mod), gel(BQD,3));
    3202             : }
    3203             : 
    3204             : static GEN
    3205      140957 : ZM_ker_slice(GEN A, GEN P, GEN *mod)
    3206             : {
    3207      140957 :   pari_sp av = avma;
    3208      140957 :   long i, n = lg(P)-1;
    3209             :   GEN BQD, D, H, T, Q;
    3210      140957 :   if (n == 1)
    3211             :   {
    3212      138241 :     ulong p = uel(P,1);
    3213      138241 :     GEN K = Flm_ker_sp(ZM_to_Flm(A, p), p, 2);
    3214      138203 :     *mod = utoi(p);
    3215      138203 :     return mkvec2(Flm_to_ZM(gel(K,1)), gel(K,2));
    3216             :   }
    3217        2716 :   T = ZV_producttree(P);
    3218        2715 :   A = ZM_nv_mod_tree(A, P, T);
    3219        2718 :   H = cgetg(n+1, t_VEC);
    3220        8834 :   for(i=1 ; i <= n; i++)
    3221        6116 :     gel(H,i) = Flm_ker_sp(gel(A, i), P[i], 2);
    3222        2718 :   BQD = ZM_ker_filter(H, P); Q = gel(BQD,2);
    3223        2718 :   if (lg(Q) != lg(P)) T = ZV_producttree(Q);
    3224        2718 :   H = nmV_chinese_center_tree_seq(gel(BQD,1), Q, T, ZV_chinesetree(Q,T));
    3225        2716 :   *mod = gmael(T, lg(T)-1, 1);
    3226        2716 :   D = gel(BQD, 3);
    3227        2716 :   gerepileall(av, 3, &H, &D, mod);
    3228        2718 :   return mkvec2(H,D);
    3229             : }
    3230             : 
    3231             : GEN
    3232      140959 : ZM_ker_worker(GEN P, GEN A)
    3233             : {
    3234      140959 :   GEN V = cgetg(3, t_VEC);
    3235      140959 :   gel(V,1) = ZM_ker_slice(A, P, &gel(V,2));
    3236      140929 :   return V;
    3237             : }
    3238             : 
    3239             : /* assume lg(A) > 1 */
    3240             : static GEN
    3241       55694 : ZM_ker_i(GEN A)
    3242             : {
    3243             :   pari_sp av;
    3244       55694 :   long k, m = lg(A)-1;
    3245       55694 :   GEN HD = NULL, mod = gen_1, worker;
    3246             :   forprime_t S;
    3247             : 
    3248       55694 :   init_modular_big(&S);
    3249       55694 :   worker = snm_closure(is_entry("_ZM_ker_worker"), mkvec(A));
    3250       55694 :   av = avma;
    3251      114626 :   for (k = 1;; k *= 2)
    3252       58932 :   {
    3253             :     pari_timer ti;
    3254             :     GEN H, B, Hr;
    3255      114626 :     gen_inccrt("ZM_ker", worker, NULL, (k+1)>>1 , m,
    3256             :                &S, &HD, &mod, ZM_ker_chinese, NULL);
    3257      170320 :     H = gel(HD, 1); if (lg(H) == 1) return H;
    3258       70449 :     B = sqrti(shifti(mod,-1));
    3259       70449 :     if (DEBUGLEVEL >= 4) timer_start(&ti);
    3260       70449 :     Hr = FpM_ratlift(H, mod, B, B, NULL);
    3261       70449 :     if (DEBUGLEVEL >= 4) timer_printf(&ti,"ZM_ker: ratlift (%ld)",!!Hr);
    3262       70449 :     if (Hr)
    3263             :     {
    3264             :       GEN MH;
    3265       66408 :       Hr = vec_Q_primpart(Hr);
    3266       66408 :       MH = ZM_mul(A, Hr);
    3267       66408 :       if (DEBUGLEVEL >= 4) timer_printf(&ti,"ZM_ker: QM_mul");
    3268       66408 :       if (ZM_equal0(MH)) return Hr;
    3269             :     }
    3270       58932 :     if (gc_needed(av,2))
    3271             :     {
    3272           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_ker");
    3273           0 :       gerepileall(av, 2, &HD, &mod);
    3274             :     }
    3275             :   }
    3276             : }
    3277             : 
    3278             : GEN
    3279       48889 : ZM_ker(GEN M)
    3280             : {
    3281       48889 :   pari_sp av = avma;
    3282       48889 :   long l = lg(M)-1;
    3283       48889 :   if (l==0) return cgetg(1, t_MAT);
    3284       48882 :   if (lgcols(M)==1) return matid(l);
    3285       48882 :   return gerepilecopy(av, ZM_ker_i(M));
    3286             : }
    3287             : 
    3288             : static GEN
    3289        6812 : row_Q_primpart(GEN M)
    3290        6812 : { return shallowtrans(vec_Q_primpart(shallowtrans(M))); }
    3291             : 
    3292             : GEN
    3293        7645 : QM_ker(GEN M)
    3294             : {
    3295        7645 :   pari_sp av = avma;
    3296        7645 :   long l = lg(M)-1;
    3297        7645 :   if (l==0) return cgetg(1, t_MAT);
    3298        7610 :   if (lgcols(M)==1) return matid(l);
    3299        6749 :   return gerepilecopy(av, ZM_ker_i(row_Q_primpart(M)));
    3300             : }
    3301             : 
    3302             : /* x a ZM. Return a multiple of the determinant of the lattice generated by
    3303             :  * the columns of x. From Algorithm 2.2.6 in GTM138 */
    3304             : GEN
    3305       47549 : detint(GEN A)
    3306             : {
    3307       47549 :   if (typ(A) != t_MAT) pari_err_TYPE("detint",A);
    3308       47549 :   RgM_check_ZM(A, "detint");
    3309       47549 :   return ZM_detmult(A);
    3310             : }
    3311             : GEN
    3312       97844 : ZM_detmult(GEN A)
    3313             : {
    3314       97844 :   pari_sp av1, av = avma;
    3315             :   GEN B, c, v, piv;
    3316       97844 :   long rg, i, j, k, m, n = lg(A) - 1;
    3317             : 
    3318       97844 :   if (!n) return gen_1;
    3319       97844 :   m = nbrows(A);
    3320       97844 :   if (n < m) return gen_0;
    3321       97823 :   c = zero_zv(m);
    3322       97823 :   av1 = avma;
    3323       97823 :   B = zeromatcopy(m,m);
    3324       97823 :   v = cgetg(m+1, t_COL);
    3325       97823 :   piv = gen_1; rg = 0;
    3326      521435 :   for (k=1; k<=n; k++)
    3327             :   {
    3328      521421 :     GEN pivprec = piv;
    3329      521421 :     long t = 0;
    3330     4169581 :     for (i=1; i<=m; i++)
    3331             :     {
    3332     3648160 :       pari_sp av2 = avma;
    3333             :       GEN vi;
    3334     3648160 :       if (c[i]) continue;
    3335             : 
    3336     2085039 :       vi = mulii(piv, gcoeff(A,i,k));
    3337    18074059 :       for (j=1; j<=m; j++)
    3338    15989020 :         if (c[j]) vi = addii(vi, mulii(gcoeff(B,j,i),gcoeff(A,j,k)));
    3339     2085039 :       if (!t && signe(vi)) t = i;
    3340     2085039 :       gel(v,i) = gerepileuptoint(av2, vi);
    3341             :     }
    3342      521421 :     if (!t) continue;
    3343             :     /* at this point c[t] = 0 */
    3344             : 
    3345      521337 :     if (++rg >= m) { /* full rank; mostly done */
    3346       97809 :       GEN det = gel(v,t); /* last on stack */
    3347       97809 :       if (++k > n)
    3348       97726 :         det = absi(det);
    3349             :       else
    3350             :       {
    3351             :         /* improve further; at this point c[i] is set for all i != t */
    3352          83 :         gcoeff(B,t,t) = piv; v = centermod(gel(B,t), det);
    3353         334 :         for ( ; k<=n; k++)
    3354         251 :           det = gcdii(det, ZV_dotproduct(v, gel(A,k)));
    3355             :       }
    3356       97809 :       return gerepileuptoint(av, det);
    3357             :     }
    3358             : 
    3359      423528 :     piv = gel(v,t);
    3360     3549868 :     for (i=1; i<=m; i++)
    3361             :     {
    3362             :       GEN mvi;
    3363     3126340 :       if (c[i] || i == t) continue;
    3364             : 
    3365     1563170 :       gcoeff(B,t,i) = mvi = negi(gel(v,i));
    3366    13900488 :       for (j=1; j<=m; j++)
    3367    12337318 :         if (c[j]) /* implies j != t */
    3368             :         {
    3369     3070326 :           pari_sp av2 = avma;
    3370     3070326 :           GEN z = addii(mulii(gcoeff(B,j,i), piv), mulii(gcoeff(B,j,t), mvi));
    3371     3070326 :           if (rg > 1) z = diviiexact(z, pivprec);
    3372     3070326 :           gcoeff(B,j,i) = gerepileuptoint(av2, z);
    3373             :         }
    3374             :     }
    3375      423528 :     c[t] = k;
    3376      423528 :     if (gc_needed(av,1))
    3377             :     {
    3378           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"detint. k=%ld",k);
    3379           0 :       gerepileall(av1, 2, &piv,&B); v = zerovec(m);
    3380             :     }
    3381             :   }
    3382          14 :   set_avma(av); return gen_0;
    3383             : }
    3384             : 
    3385             : /* Reduce x modulo (invertible) y */
    3386             : GEN
    3387       14525 : closemodinvertible(GEN x, GEN y)
    3388             : {
    3389       14525 :   return gmul(y, ground(RgM_solve(y,x)));
    3390             : }
    3391             : GEN
    3392           7 : reducemodinvertible(GEN x, GEN y)
    3393             : {
    3394           7 :   return gsub(x, closemodinvertible(x,y));
    3395             : }
    3396             : GEN
    3397           0 : reducemodlll(GEN x,GEN y)
    3398             : {
    3399           0 :   return reducemodinvertible(x, ZM_lll(y, 0.75, LLL_INPLACE));
    3400             : }
    3401             : 
    3402             : /*******************************************************************/
    3403             : /*                                                                 */
    3404             : /*                    KERNEL of an m x n matrix                    */
    3405             : /*          return n - rk(x) linearly independent vectors          */
    3406             : /*                                                                 */
    3407             : /*******************************************************************/
    3408             : static GEN
    3409          28 : RgM_deplin_i(GEN x0)
    3410             : {
    3411          28 :   pari_sp av = avma, av2;
    3412          28 :   long i, j, k, nl, nc = lg(x0)-1;
    3413             :   GEN D, x, y, c, l, d, ck;
    3414             : 
    3415          28 :   if (!nc) return NULL;
    3416          28 :   nl = nbrows(x0);
    3417          28 :   c = zero_zv(nl);
    3418          28 :   l = cgetg(nc+1, t_VECSMALL); /* not initialized */
    3419          28 :   av2 = avma;
    3420          28 :   x = RgM_shallowcopy(x0);
    3421          28 :   d = const_vec(nl, gen_1); /* pivot list */
    3422          28 :   ck = NULL; /* gcc -Wall */
    3423          98 :   for (k=1; k<=nc; k++)
    3424             :   {
    3425          91 :     ck = gel(x,k);
    3426         196 :     for (j=1; j<k; j++)
    3427             :     {
    3428         105 :       GEN cj = gel(x,j), piv = gel(d,j), q = gel(ck,l[j]);
    3429         420 :       for (i=1; i<=nl; i++)
    3430         315 :         if (i!=l[j]) gel(ck,i) = gsub(gmul(piv, gel(ck,i)), gmul(q, gel(cj,i)));
    3431             :     }
    3432             : 
    3433          91 :     i = gauss_get_pivot_NZ(x, NULL, k, c);
    3434          91 :     if (i > nl) break;
    3435          70 :     if (gc_needed(av,1))
    3436             :     {
    3437           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"deplin k = %ld/%ld",k,nc);
    3438           0 :       gerepileall(av2, 2, &x, &d);
    3439           0 :       ck = gel(x,k);
    3440             :     }
    3441          70 :     gel(d,k) = gel(ck,i);
    3442          70 :     c[i] = k; l[k] = i; /* pivot d[k] in x[i,k] */
    3443             :   }
    3444          28 :   if (k > nc) return gc_NULL(av);
    3445          21 :   if (k == 1) { set_avma(av); return scalarcol_shallow(gen_1,nc); }
    3446          21 :   y = cgetg(nc+1,t_COL);
    3447          21 :   gel(y,1) = gcopy(gel(ck, l[1]));
    3448          49 :   for (D=gel(d,1),j=2; j<k; j++)
    3449             :   {
    3450          28 :     gel(y,j) = gmul(gel(ck, l[j]), D);
    3451          28 :     D = gmul(D, gel(d,j));
    3452             :   }
    3453          21 :   gel(y,j) = gneg(D);
    3454          21 :   for (j++; j<=nc; j++) gel(y,j) = gen_0;
    3455          21 :   y = primitive_part(y, &c);
    3456          21 :   return c? gerepileupto(av, y): gerepilecopy(av, y);
    3457             : }
    3458             : static GEN
    3459           0 : RgV_deplin(GEN v)
    3460             : {
    3461           0 :   pari_sp av = avma;
    3462           0 :   long n = lg(v)-1;
    3463           0 :   GEN y, p = NULL;
    3464           0 :   if (n <= 1)
    3465             :   {
    3466           0 :     if (n == 1 && gequal0(gel(v,1))) return mkcol(gen_1);
    3467           0 :     return cgetg(1, t_COL);
    3468             :   }
    3469           0 :   if (gequal0(gel(v,1))) return scalarcol_shallow(gen_1, n);
    3470           0 :   v = primpart(mkvec2(gel(v,1),gel(v,2)));
    3471           0 :   if (RgV_is_FpV(v, &p) && p) v = centerlift(v);
    3472           0 :   y = zerocol(n);
    3473           0 :   gel(y,1) = gneg(gel(v,2));
    3474           0 :   gel(y,2) = gcopy(gel(v,1));
    3475           0 :   return gerepileupto(av, y);
    3476             : 
    3477             : }
    3478             : 
    3479             : static GEN
    3480         105 : RgM_deplin_FpM(GEN x, GEN p)
    3481             : {
    3482         105 :   pari_sp av = avma;
    3483             :   ulong pp;
    3484         105 :   x = RgM_Fp_init(x, p, &pp);
    3485         105 :   switch(pp)
    3486             :   {
    3487             :   case 0:
    3488          35 :     x = FpM_ker_gen(x,p,1);
    3489          35 :     if (!x) return gc_NULL(av);
    3490          21 :     x = FpC_center(x,p,shifti(p,-1));
    3491          21 :     break;
    3492             :   case 2:
    3493          14 :     x = F2m_ker_sp(x,1);
    3494          14 :     if (!x) return gc_NULL(av);
    3495           7 :     x = F2c_to_ZC(x); break;
    3496             :   default:
    3497          56 :     x = Flm_ker_sp(x,pp,1);
    3498          56 :     if (!x) return gc_NULL(av);
    3499          35 :     x = Flv_center(x, pp, pp>>1);
    3500          35 :     x = zc_to_ZC(x);
    3501          35 :     break;
    3502             :   }
    3503          63 :   return gerepileupto(av, x);
    3504             : }
    3505             : 
    3506             : /* FIXME: implement direct modular ZM_deplin ? */
    3507             : static GEN
    3508          98 : QM_deplin(GEN M)
    3509             : {
    3510          98 :   pari_sp av = avma;
    3511          98 :   long l = lg(M)-1;
    3512             :   GEN k;
    3513          98 :   if (l==0) return NULL;
    3514          63 :   if (lgcols(M)==1) return col_ei(l, 1);
    3515          63 :   k = ZM_ker_i(row_Q_primpart(M));
    3516          63 :   if (lg(k)== 1) return gc_NULL(av);
    3517          49 :   return gerepilecopy(av, gel(k,1));
    3518             : }
    3519             : 
    3520             : static GEN
    3521          42 : RgM_deplin_FqM(GEN x, GEN pol, GEN p)
    3522             : {
    3523          42 :   pari_sp av = avma;
    3524          42 :   GEN b, T = RgX_to_FpX(pol, p);
    3525          42 :   if (signe(T) == 0) pari_err_OP("deplin",x,pol);
    3526          42 :   b = FqM_deplin(RgM_to_FqM(x, T, p), T, p);
    3527          42 :   return gerepileupto(av, b);
    3528             : }
    3529             : 
    3530             : #define code(t1,t2) ((t1 << 6) | t2)
    3531             : static GEN
    3532         357 : RgM_deplin_fast(GEN x)
    3533             : {
    3534             :   GEN p, pol;
    3535             :   long pa;
    3536         357 :   long t = RgM_type(x, &p,&pol,&pa);
    3537         357 :   switch(t)
    3538             :   {
    3539             :     case t_INT:    /* fall through */
    3540          98 :     case t_FRAC:   return QM_deplin(x);
    3541          84 :     case t_FFELT:  return FFM_deplin(x, pol);
    3542         105 :     case t_INTMOD: return RgM_deplin_FpM(x, p);
    3543             :     case code(t_POLMOD, t_INTMOD):
    3544          42 :                    return RgM_deplin_FqM(x, pol, p);
    3545          28 :     default:       return gen_0;
    3546             :   }
    3547             : }
    3548             : #undef code
    3549             : 
    3550             : static GEN
    3551         357 : RgM_deplin(GEN x)
    3552             : {
    3553         357 :   GEN z = RgM_deplin_fast(x);
    3554         357 :   if (z!= gen_0) return z;
    3555          28 :   return RgM_deplin_i(x);
    3556             : }
    3557             : 
    3558             : GEN
    3559         357 : deplin(GEN x)
    3560             : {
    3561         357 :   switch(typ(x))
    3562             :   {
    3563             :     case t_MAT:
    3564             :     {
    3565         357 :       GEN z = RgM_deplin(x);
    3566         357 :       if (z) return z;
    3567         140 :       return cgetg(1, t_COL);
    3568             :     }
    3569           0 :     case t_VEC: return RgV_deplin(x);
    3570           0 :     default: pari_err_TYPE("deplin",x);
    3571             :   }
    3572             :   return NULL;/*LCOV_EXCL_LINE*/
    3573             : }
    3574             : 
    3575             : /*******************************************************************/
    3576             : /*                                                                 */
    3577             : /*         GAUSS REDUCTION OF MATRICES  (m lines x n cols)         */
    3578             : /*           (kernel, image, complementary image, rank)            */
    3579             : /*                                                                 */
    3580             : /*******************************************************************/
    3581             : /* return the transform of x under a standard Gauss pivot.
    3582             :  * x0 is a reference point when guessing whether x[i,j] ~ 0
    3583             :  * (iff x[i,j] << x0[i,j])
    3584             :  * Set r = dim ker(x). d[k] contains the index of the first non-zero pivot
    3585             :  * in column k */
    3586             : static GEN
    3587         993 : gauss_pivot_ker(GEN x, GEN x0, GEN *dd, long *rr)
    3588             : {
    3589             :   GEN c, d, p, data;
    3590             :   pari_sp av;
    3591             :   long i, j, k, r, t, n, m;
    3592             :   pivot_fun pivot;
    3593             : 
    3594         993 :   n=lg(x)-1; if (!n) { *dd=NULL; *rr=0; return cgetg(1,t_MAT); }
    3595         993 :   m=nbrows(x); r=0;
    3596         993 :   pivot = get_pivot_fun(x, x0, &data);
    3597         993 :   x = RgM_shallowcopy(x);
    3598         993 :   c = zero_zv(m);
    3599         993 :   d = cgetg(n+1,t_VECSMALL);
    3600         993 :   av=avma;
    3601        5763 :   for (k=1; k<=n; k++)
    3602             :   {
    3603        4770 :     j = pivot(x, data, k, c);
    3604        4770 :     if (j > m)
    3605             :     {
    3606        1057 :       r++; d[k]=0;
    3607        4690 :       for(j=1; j<k; j++)
    3608        3633 :         if (d[j]) gcoeff(x,d[j],k) = gclone(gcoeff(x,d[j],k));
    3609             :     }
    3610             :     else
    3611             :     { /* pivot for column k on row j */
    3612        3713 :       c[j]=k; d[k]=j; p = gdiv(gen_m1,gcoeff(x,j,k));
    3613        3713 :       gcoeff(x,j,k) = gen_m1;
    3614             :       /* x[j,] /= - x[j,k] */
    3615        3713 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    3616       35680 :       for (t=1; t<=m; t++)
    3617       31967 :         if (t!=j)
    3618             :         { /* x[t,] -= 1 / x[j,k] x[j,] */
    3619       28254 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    3620       28254 :           if (gequal0(p)) continue;
    3621       75759 :           for (i=k+1; i<=n; i++)
    3622       61386 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i),gmul(p,gcoeff(x,j,i)));
    3623       14373 :           if (gc_needed(av,1)) gerepile_gauss_ker(x,k,t,av);
    3624             :         }
    3625             :     }
    3626             :   }
    3627         993 :   *dd=d; *rr=r; return x;
    3628             : }
    3629             : 
    3630             : /* r = dim ker(x).
    3631             :  * Returns d:
    3632             :  *   d[k] != 0 contains the index of a non-zero pivot in column k
    3633             :  *   d[k] == 0 if column k is a linear combination of the (k-1) first ones */
    3634             : GEN
    3635       34886 : RgM_pivots(GEN x0, GEN data, long *rr, pivot_fun pivot)
    3636             : {
    3637             :   GEN x, c, d, p;
    3638       34886 :   long i, j, k, r, t, m, n = lg(x0)-1;
    3639             :   pari_sp av;
    3640             : 
    3641       34886 :   if (RgM_is_ZM(x0)) return ZM_pivots(x0, rr);
    3642       34781 :   if (!n) { *rr = 0; return NULL; }
    3643             : 
    3644       34781 :   d = cgetg(n+1, t_VECSMALL);
    3645       34781 :   x = RgM_shallowcopy(x0);
    3646       34781 :   m = nbrows(x); r = 0;
    3647       34781 :   c = zero_zv(m);
    3648       34781 :   av = avma;
    3649      979460 :   for (k=1; k<=n; k++)
    3650             :   {
    3651      944679 :     j = pivot(x, data, k, c);
    3652      944679 :     if (j > m) { r++; d[k] = 0; }
    3653             :     else
    3654             :     {
    3655       56053 :       c[j] = k; d[k] = j; p = gdiv(gen_m1, gcoeff(x,j,k));
    3656       56053 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    3657             : 
    3658      220847 :       for (t=1; t<=m; t++)
    3659      164794 :         if (!c[t]) /* no pivot on that line yet */
    3660             :         {
    3661       64569 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    3662     5663861 :           for (i=k+1; i<=n; i++)
    3663     5599292 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i), gmul(p, gcoeff(x,j,i)));
    3664       64569 :           if (gc_needed(av,1)) gerepile_gauss(x,k,t,av,j,c);
    3665             :         }
    3666       56053 :       for (i=k; i<=n; i++) gcoeff(x,j,i) = gen_0; /* dummy */
    3667             :     }
    3668             :   }
    3669       34781 :   *rr = r; set_avma((pari_sp)d); return d;
    3670             : }
    3671             : 
    3672             : static long
    3673      122137 : ZM_count_0_cols(GEN M)
    3674             : {
    3675      122137 :   long i, l = lg(M), n = 0;
    3676      699710 :   for (i = 1; i < l; i++)
    3677      577573 :     if (ZV_equal0(gel(M,i))) n++;
    3678      122137 :   return n;
    3679             : }
    3680             : 
    3681             : static void indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol);
    3682             : /* As RgM_pivots, integer entries. Set *rr = dim Ker M0 */
    3683             : GEN
    3684      126368 : ZM_pivots(GEN M0, long *rr)
    3685             : {
    3686      126368 :   GEN d, dbest = NULL;
    3687             :   long m, n, i, imax, rmin, rbest, zc;
    3688      126368 :   int beenthere = 0;
    3689      126368 :   pari_sp av, av0 = avma;
    3690             :   forprime_t S;
    3691             : 
    3692      126368 :   rbest = n = lg(M0)-1;
    3693      126368 :   if (n == 0) { *rr = 0; return NULL; }
    3694      122137 :   zc = ZM_count_0_cols(M0);
    3695      122137 :   if (n == zc) { *rr = zc; return zero_zv(n); }
    3696             : 
    3697      121983 :   m = nbrows(M0);
    3698      121983 :   rmin = maxss(zc, n-m);
    3699      121983 :   init_modular_small(&S);
    3700      121983 :   imax = (n < (1<<4))? 1: (n>>3); /* heuristic */
    3701             : 
    3702             :   for(;;)
    3703           0 :   {
    3704             :     GEN row, col, M, KM, IM, RHS, X, cX;
    3705             :     long rk;
    3706      131618 :     for (av = avma, i = 0;; set_avma(av), i++)
    3707        9635 :     {
    3708      131618 :       ulong p = u_forprime_next(&S);
    3709             :       long rp;
    3710      131618 :       if (!p) pari_err_OVERFLOW("ZM_pivots [ran out of primes]");
    3711      131618 :       d = Flm_pivots(ZM_to_Flm(M0, p), p, &rp, 1);
    3712      131618 :       if (rp == rmin) { rbest = rp; goto END; } /* maximal rank, return */
    3713       17609 :       if (rp < rbest) { /* save best r so far */
    3714        7976 :         rbest = rp;
    3715        7976 :         guncloneNULL(dbest);
    3716        7976 :         dbest = gclone(d);
    3717       15950 :         if (beenthere) break;
    3718             :       }
    3719       17609 :       if (!beenthere && i >= imax) break;
    3720             :     }
    3721        7974 :     beenthere = 1;
    3722             :     /* Dubious case: there is (probably) a non trivial kernel */
    3723        7974 :     indexrank_all(m,n, rbest, dbest, &row, &col);
    3724        7974 :     M = rowpermute(vecpermute(M0, col), row);
    3725        7974 :     rk = n - rbest; /* (probable) dimension of image */
    3726        7974 :     IM = vecslice(M,1,rk);
    3727        7974 :     KM = vecslice(M,rk+1, n);
    3728        7974 :     M = rowslice(IM, 1,rk); /* square maximal rank */
    3729        7974 :     X = ZM_gauss(M, rowslice(KM, 1,rk));
    3730        7974 :     X = Q_remove_denom(X, &cX);
    3731        7974 :     RHS = rowslice(KM,rk+1,m);
    3732        7974 :     if (cX) RHS = ZM_Z_mul(RHS, cX);
    3733        7974 :     if (ZM_equal(ZM_mul(rowslice(IM,rk+1,m), X), RHS))
    3734             :     {
    3735        7974 :       d = vecsmall_copy(dbest);
    3736        7974 :       goto END;
    3737             :     }
    3738           0 :     set_avma(av);
    3739             :   }
    3740             : END:
    3741      121983 :   *rr = rbest; guncloneNULL(dbest);
    3742      121983 :   return gerepileuptoleaf(av0, d);
    3743             : }
    3744             : 
    3745             : /* set *pr = dim Ker x */
    3746             : static GEN
    3747       21014 : gauss_pivot(GEN x, long *pr) {
    3748             :   GEN data;
    3749       21014 :   pivot_fun pivot = get_pivot_fun(x, x, &data);
    3750       21014 :   return RgM_pivots(x, data, pr, pivot);
    3751             : }
    3752             : 
    3753             : /* compute ker(x), x0 is a reference point when guessing whether x[i,j] ~ 0
    3754             :  * (iff x[i,j] << x0[i,j]) */
    3755             : static GEN
    3756         993 : ker_aux(GEN x, GEN x0)
    3757             : {
    3758         993 :   pari_sp av = avma;
    3759             :   GEN d,y;
    3760             :   long i,j,k,r,n;
    3761             : 
    3762         993 :   x = gauss_pivot_ker(x,x0,&d,&r);
    3763         993 :   if (!r) { set_avma(av); return cgetg(1,t_MAT); }
    3764         959 :   n = lg(x)-1; y=cgetg(r+1,t_MAT);
    3765        2016 :   for (j=k=1; j<=r; j++,k++)
    3766             :   {
    3767        1057 :     GEN p = cgetg(n+1,t_COL);
    3768             : 
    3769        1057 :     gel(y,j) = p; while (d[k]) k++;
    3770        4690 :     for (i=1; i<k; i++)
    3771        3633 :       if (d[i])
    3772             :       {
    3773        3451 :         GEN p1=gcoeff(x,d[i],k);
    3774        3451 :         gel(p,i) = gcopy(p1); gunclone(p1);
    3775             :       }
    3776             :       else
    3777         182 :         gel(p,i) = gen_0;
    3778        1057 :     gel(p,k) = gen_1; for (i=k+1; i<=n; i++) gel(p,i) = gen_0;
    3779             :   }
    3780         959 :   return gerepileupto(av,y);
    3781             : }
    3782             : 
    3783             : static GEN
    3784          77 : RgM_ker_FpM(GEN x, GEN p)
    3785             : {
    3786          77 :   pari_sp av = avma;
    3787             :   ulong pp;
    3788          77 :   x = RgM_Fp_init(x, p, &pp);
    3789          77 :   switch(pp)
    3790             :   {
    3791          35 :     case 0: x = FpM_to_mod(FpM_ker_gen(x,p,0),p); break;
    3792           7 :     case 2: x = F2m_to_mod(F2m_ker_sp(x,0)); break;
    3793          35 :     default:x = Flm_to_mod(Flm_ker_sp(x,pp,0), pp); break;
    3794             :   }
    3795          77 :   return gerepileupto(av, x);
    3796             : }
    3797             : 
    3798             : static GEN
    3799          91 : RgM_ker_FqM(GEN x, GEN pol, GEN p)
    3800             : {
    3801          91 :   pari_sp av = avma;
    3802          91 :   GEN b, T = RgX_to_FpX(pol, p);
    3803          91 :   if (signe(T) == 0) pari_err_OP("ker",x,pol);
    3804          84 :   b = FqM_ker(RgM_to_FqM(x, T, p), T, p);
    3805          84 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    3806             : }
    3807             : 
    3808             : #define code(t1,t2) ((t1 << 6) | t2)
    3809             : static GEN
    3810        8646 : RgM_ker_fast(GEN x)
    3811             : {
    3812             :   GEN p, pol;
    3813             :   long pa;
    3814        8646 :   long t = RgM_type(x, &p,&pol,&pa);
    3815        8646 :   switch(t)
    3816             :   {
    3817             :     case t_INT:    /* fall through */
    3818        7645 :     case t_FRAC:   return QM_ker(x);
    3819          77 :     case t_FFELT:  return FFM_ker(x, pol);
    3820          77 :     case t_INTMOD: return RgM_ker_FpM(x, p);
    3821             :     case code(t_POLMOD, t_INTMOD):
    3822          91 :                    return RgM_ker_FqM(x, pol, p);
    3823         756 :     default:       return NULL;
    3824             :   }
    3825             : }
    3826             : #undef code
    3827             : 
    3828             : GEN
    3829        8646 : ker(GEN x)
    3830             : {
    3831        8646 :   GEN b = RgM_ker_fast(x);
    3832        8639 :   if (b) return b;
    3833         756 :   return ker_aux(x,x);
    3834             : }
    3835             : 
    3836             : GEN
    3837       46214 : matker0(GEN x,long flag)
    3838             : {
    3839       46214 :   if (typ(x)!=t_MAT) pari_err_TYPE("matker",x);
    3840       46214 :   if (!flag) return ker(x);
    3841       45934 :   RgM_check_ZM(x, "matker");
    3842       45934 :   return ZM_ker(x);
    3843             : }
    3844             : 
    3845             : static GEN
    3846          63 : RgM_image_FpM(GEN x, GEN p)
    3847             : {
    3848          63 :   pari_sp av = avma;
    3849             :   ulong pp;
    3850          63 :   x = RgM_Fp_init(x, p, &pp);
    3851          63 :   switch(pp)
    3852             :   {
    3853          28 :     case 0: x = FpM_to_mod(FpM_image(x,p),p); break;
    3854           7 :     case 2: x = F2m_to_mod(F2m_image(x)); break;
    3855          28 :     default:x = Flm_to_mod(Flm_image(x,pp), pp); break;
    3856             :   }
    3857          63 :   return gerepileupto(av, x);
    3858             : }
    3859             : 
    3860             : static GEN
    3861          35 : RgM_image_FqM(GEN x, GEN pol, GEN p)
    3862             : {
    3863          35 :   pari_sp av = avma;
    3864          35 :   GEN b, T = RgX_to_FpX(pol, p);
    3865          35 :   if (signe(T) == 0) pari_err_OP("image",x,pol);
    3866          28 :   b = FqM_image(RgM_to_FqM(x, T, p), T, p);
    3867          28 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    3868             : }
    3869             : 
    3870             : static GEN
    3871        1463 : QM_image(GEN A)
    3872             : {
    3873        1463 :   pari_sp av = avma;
    3874        1463 :   GEN M = vecpermute(A, ZM_indeximage(vec_Q_primpart(A)));
    3875        1463 :   return gerepilecopy(av, M);
    3876             : }
    3877             : 
    3878             : #define code(t1,t2) ((t1 << 6) | t2)
    3879             : static GEN
    3880        1624 : RgM_image_fast(GEN x)
    3881             : {
    3882             :   GEN p, pol;
    3883             :   long pa;
    3884        1624 :   long t = RgM_type(x, &p,&pol,&pa);
    3885        1624 :   switch(t)
    3886             :   {
    3887             :     case t_INT:    /* fall through */
    3888        1463 :     case t_FRAC:   return QM_image(x);
    3889          49 :     case t_FFELT:  return FFM_image(x, pol);
    3890          63 :     case t_INTMOD: return RgM_image_FpM(x, p);
    3891             :     case code(t_POLMOD, t_INTMOD):
    3892          35 :                    return RgM_image_FqM(x, pol, p);
    3893          14 :     default:       return NULL;
    3894             :   }
    3895             : }
    3896             : #undef code
    3897             : 
    3898             : GEN
    3899        1624 : image(GEN x)
    3900             : {
    3901             :   GEN d, M;
    3902             :   long r;
    3903             : 
    3904        1624 :   if (typ(x)!=t_MAT) pari_err_TYPE("matimage",x);
    3905        1624 :   M = RgM_image_fast(x);
    3906        1617 :   if (M) return M;
    3907          14 :   d = gauss_pivot(x,&r); /* d left on stack for efficiency */
    3908          14 :   return image_from_pivot(x,d,r);
    3909             : }
    3910             : 
    3911             : static GEN
    3912          84 : imagecompl_aux(GEN x, GEN(*PIVOT)(GEN,long*))
    3913             : {
    3914          84 :   pari_sp av = avma;
    3915             :   GEN d,y;
    3916             :   long j,i,r;
    3917             : 
    3918          84 :   if (typ(x)!=t_MAT) pari_err_TYPE("imagecompl",x);
    3919          84 :   (void)new_chunk(lg(x) * 4 + 1); /* HACK */
    3920          84 :   d = PIVOT(x,&r); /* if (!d) then r = 0 */
    3921          84 :   set_avma(av); y = cgetg(r+1,t_VECSMALL);
    3922         126 :   for (i=j=1; j<=r; i++)
    3923          42 :     if (!d[i]) y[j++] = i;
    3924          84 :   return y;
    3925             : }
    3926             : GEN
    3927          84 : imagecompl(GEN x) { return imagecompl_aux(x, &gauss_pivot); }
    3928             : GEN
    3929           0 : ZM_imagecompl(GEN x) { return imagecompl_aux(x, &ZM_pivots); }
    3930             : 
    3931             : static GEN
    3932          28 : RgM_RgC_invimage_FpC(GEN A, GEN y, GEN p)
    3933             : {
    3934          28 :   pari_sp av = avma;
    3935             :   ulong pp;
    3936             :   GEN x;
    3937          28 :   A = RgM_Fp_init(A,p,&pp);
    3938          28 :   switch(pp)
    3939             :   {
    3940             :   case 0:
    3941           7 :     y = RgC_to_FpC(y,p);
    3942           7 :     x = FpM_FpC_invimage(A, y, p);
    3943           7 :     return x ? gerepileupto(av, FpC_to_mod(x,p)): NULL;
    3944             :   case 2:
    3945           7 :     y = RgV_to_F2v(y);
    3946           7 :     x = F2m_F2c_invimage(A, y);
    3947           7 :     return x ? gerepileupto(av, F2c_to_mod(x)): NULL;
    3948             :   default:
    3949          14 :     y = RgV_to_Flv(y,pp);
    3950          14 :     x = Flm_Flc_invimage(A, y, pp);
    3951          14 :     return x ? gerepileupto(av, Flc_to_mod(x,pp)): NULL;
    3952             :   }
    3953             : }
    3954             : 
    3955             : static GEN
    3956        2051 : RgM_RgC_invimage_fast(GEN x, GEN y)
    3957             : {
    3958             :   GEN p, pol;
    3959             :   long pa;
    3960        2051 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    3961        2051 :   switch(t)
    3962             :   {
    3963          28 :     case t_INTMOD: return RgM_RgC_invimage_FpC(x, y, p);
    3964          63 :     case t_FFELT:  return FFM_FFC_invimage(x, y, pol);
    3965        1960 :     default:       return gen_0;
    3966             :   }
    3967             : }
    3968             : 
    3969             : GEN
    3970        2156 : RgM_RgC_invimage(GEN A, GEN y)
    3971             : {
    3972        2156 :   pari_sp av = avma;
    3973        2156 :   long i, l = lg(A);
    3974             :   GEN M, x, t;
    3975        2156 :   if (l==1) return NULL;
    3976        2051 :   if (lg(y) != lgcols(A)) pari_err_DIM("inverseimage");
    3977        2051 :   M = RgM_RgC_invimage_fast(A, y);
    3978        2051 :   if (!M) return gc_NULL(av);
    3979        2030 :   if (M != gen_0) return M;
    3980        1960 :   M = ker(shallowconcat(A, y));
    3981        1960 :   i = lg(M)-1;
    3982        1960 :   if (!i) return gc_NULL(av);
    3983             : 
    3984        1701 :   x = gel(M,i); t = gel(x,l);
    3985        1701 :   if (gequal0(t)) return gc_NULL(av);
    3986             : 
    3987        1666 :   t = gneg_i(t); setlg(x,l);
    3988        1666 :   return gerepileupto(av, RgC_Rg_div(x, t));
    3989             : }
    3990             : 
    3991             : /* Return X such that m X = v (t_COL or t_MAT), resp. an empty t_COL / t_MAT
    3992             :  * if no solution exist */
    3993             : GEN
    3994        2366 : inverseimage(GEN m, GEN v)
    3995             : {
    3996             :   GEN y;
    3997        2366 :   if (typ(m)!=t_MAT) pari_err_TYPE("inverseimage",m);
    3998        2366 :   switch(typ(v))
    3999             :   {
    4000             :     case t_COL:
    4001        2128 :       y = RgM_RgC_invimage(m,v);
    4002        2128 :       return y? y: cgetg(1,t_COL);
    4003             :     case t_MAT:
    4004         238 :       y = RgM_invimage(m, v);
    4005         238 :       return y? y: cgetg(1,t_MAT);
    4006             :   }
    4007           0 :   pari_err_TYPE("inverseimage",v);
    4008             :   return NULL;/*LCOV_EXCL_LINE*/
    4009             : }
    4010             : 
    4011             : static GEN
    4012          84 : RgM_invimage_FpM(GEN A, GEN B, GEN p)
    4013             : {
    4014          84 :   pari_sp av = avma;
    4015             :   ulong pp;
    4016             :   GEN x;
    4017          84 :   A = RgM_Fp_init(A,p,&pp);
    4018          84 :   switch(pp)
    4019             :   {
    4020             :   case 0:
    4021          35 :     B = RgM_to_FpM(B,p);
    4022          35 :     x = FpM_invimage_gen(A, B, p);
    4023          35 :     return x ? gerepileupto(av, FpM_to_mod(x, p)): x;
    4024             :   case 2:
    4025           7 :     B = RgM_to_F2m(B);
    4026           7 :     x = F2m_invimage_i(A, B);
    4027           7 :     return x ? gerepileupto(av, F2m_to_mod(x)): x;
    4028             :   default:
    4029          42 :     B = RgM_to_Flm(B,pp);
    4030          42 :     x = Flm_invimage_i(A, B, pp);
    4031          42 :     return x ? gerepileupto(av, Flm_to_mod(x, pp)): x;
    4032             :   }
    4033             : }
    4034             : 
    4035             : static GEN
    4036         252 : RgM_invimage_fast(GEN x, GEN y)
    4037             : {
    4038             :   GEN p, pol;
    4039             :   long pa;
    4040         252 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    4041         252 :   switch(t)
    4042             :   {
    4043          84 :     case t_INTMOD: return RgM_invimage_FpM(x, y, p);
    4044         105 :     case t_FFELT:  return FFM_invimage(x, y, pol);
    4045          63 :     default:       return gen_0;
    4046             :   }
    4047             : }
    4048             : 
    4049             : /* find Z such that A Z = B. Return NULL if no solution */
    4050             : GEN
    4051         252 : RgM_invimage(GEN A, GEN B)
    4052             : {
    4053         252 :   pari_sp av = avma;
    4054             :   GEN d, x, X, Y;
    4055         252 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4056         252 :   X = RgM_invimage_fast(A, B);
    4057         252 :   if (!X) return gc_NULL(av);
    4058         140 :   if (X != gen_0) return X;
    4059          63 :   x = ker(shallowconcat(RgM_neg(A), B));
    4060             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4061             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4062             :    * Y has at least nB columns and full rank */
    4063          63 :   nY = lg(x)-1;
    4064          63 :   if (nY < nB) return gc_NULL(av);
    4065          49 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    4066          49 :   d = cgetg(nB+1, t_VECSMALL);
    4067         441 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4068             :   {
    4069         546 :     for (; j>=1; j--)
    4070         532 :       if (!gequal0(gcoeff(Y,i,j))) { d[i] = j; break; }
    4071         406 :     if (!j) return gc_NULL(av);
    4072             :   }
    4073             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    4074          35 :   Y = vecpermute(Y, d);
    4075          35 :   x = vecpermute(x, d);
    4076          35 :   X = rowslice(x, 1, nA);
    4077          35 :   return gerepileupto(av, RgM_mul(X, RgM_inv_upper(Y)));
    4078             : }
    4079             : 
    4080             : static GEN
    4081          70 : RgM_suppl_FpM(GEN x, GEN p)
    4082             : {
    4083          70 :   pari_sp av = avma;
    4084             :   ulong pp;
    4085          70 :   x = RgM_Fp_init(x, p, &pp);
    4086          70 :   switch(pp)
    4087             :   {
    4088          21 :   case 0: x = FpM_to_mod(FpM_suppl(x,p), p); break;
    4089          14 :   case 2: x = F2m_to_mod(F2m_suppl(x)); break;
    4090          35 :   default:x = Flm_to_mod(Flm_suppl(x,pp), pp); break;
    4091             :   }
    4092          70 :   return gerepileupto(av, x);
    4093             : }
    4094             : 
    4095             : static GEN
    4096         175 : RgM_suppl_fast(GEN x)
    4097             : {
    4098             :   GEN p, pol;
    4099             :   long pa;
    4100         175 :   long t = RgM_type(x,&p,&pol,&pa);
    4101         175 :   switch(t)
    4102             :   {
    4103          70 :     case t_INTMOD: return RgM_suppl_FpM(x, p);
    4104          35 :     case t_FFELT:  return FFM_suppl(x, pol);
    4105          70 :     default:       return NULL;
    4106             :   }
    4107             : }
    4108             : 
    4109             : /* x is an n x k matrix, rank(x) = k <= n. Return an invertible n x n matrix
    4110             :  * whose first k columns are given by x. If rank(x) < k, undefined result. */
    4111             : GEN
    4112         175 : suppl(GEN x)
    4113             : {
    4114         175 :   pari_sp av = avma;
    4115             :   GEN d, M;
    4116             :   long r;
    4117         175 :   if (typ(x)!=t_MAT) pari_err_TYPE("suppl",x);
    4118         175 :   M = RgM_suppl_fast(x);
    4119         175 :   if (M) return M;
    4120          70 :   init_suppl(x);
    4121          70 :   d = gauss_pivot(x,&r);
    4122          70 :   set_avma(av); return get_suppl(x,d,nbrows(x),r,&col_ei);
    4123             : }
    4124             : 
    4125             : GEN
    4126           7 : image2(GEN x)
    4127             : {
    4128           7 :   pari_sp av = avma;
    4129             :   long k, n, i;
    4130             :   GEN A, B;
    4131             : 
    4132           7 :   if (typ(x)!=t_MAT) pari_err_TYPE("image2",x);
    4133           7 :   if (lg(x) == 1) return cgetg(1,t_MAT);
    4134           7 :   A = ker(x); k = lg(A)-1;
    4135           7 :   if (!k) { set_avma(av); return gcopy(x); }
    4136           7 :   A = suppl(A); n = lg(A)-1;
    4137           7 :   B = cgetg(n-k+1, t_MAT);
    4138           7 :   for (i = k+1; i <= n; i++) gel(B,i-k) = RgM_RgC_mul(x, gel(A,i));
    4139           7 :   return gerepileupto(av, B);
    4140             : }
    4141             : 
    4142             : GEN
    4143         210 : matimage0(GEN x,long flag)
    4144             : {
    4145         210 :   switch(flag)
    4146             :   {
    4147         203 :     case 0: return image(x);
    4148           7 :     case 1: return image2(x);
    4149           0 :     default: pari_err_FLAG("matimage");
    4150             :   }
    4151             :   return NULL; /* LCOV_EXCL_LINE */
    4152             : }
    4153             : 
    4154             : static long
    4155         126 : RgM_rank_FpM(GEN x, GEN p)
    4156             : {
    4157         126 :   pari_sp av = avma;
    4158             :   ulong pp;
    4159             :   long r;
    4160         126 :   x = RgM_Fp_init(x,p,&pp);
    4161         126 :   switch(pp)
    4162             :   {
    4163          28 :   case 0: r = FpM_rank(x,p); break;
    4164          63 :   case 2: r = F2m_rank(x); break;
    4165          35 :   default:r = Flm_rank(x,pp); break;
    4166             :   }
    4167         126 :   return gc_long(av, r);
    4168             : }
    4169             : 
    4170             : static long
    4171          49 : RgM_rank_FqM(GEN x, GEN pol, GEN p)
    4172             : {
    4173          49 :   pari_sp av = avma;
    4174             :   long r;
    4175          49 :   GEN T = RgX_to_FpX(pol, p);
    4176          49 :   if (signe(T) == 0) pari_err_OP("rank",x,pol);
    4177          42 :   r = FqM_rank(RgM_to_FqM(x, T, p), T, p);
    4178          42 :   return gc_long(av,r);
    4179             : }
    4180             : 
    4181             : #define code(t1,t2) ((t1 << 6) | t2)
    4182             : static long
    4183         287 : RgM_rank_fast(GEN x)
    4184             : {
    4185             :   GEN p, pol;
    4186             :   long pa;
    4187         287 :   long t = RgM_type(x,&p,&pol,&pa);
    4188         287 :   switch(t)
    4189             :   {
    4190          42 :     case t_INT:    return ZM_rank(x);
    4191           0 :     case t_FRAC:   return QM_rank(x);
    4192         126 :     case t_INTMOD: return RgM_rank_FpM(x, p);
    4193          63 :     case t_FFELT:  return FFM_rank(x, pol);
    4194             :     case code(t_POLMOD, t_INTMOD):
    4195          49 :                    return RgM_rank_FqM(x, pol, p);
    4196           7 :     default:       return -1;
    4197             :   }
    4198             : }
    4199             : #undef code
    4200             : 
    4201             : long
    4202         287 : rank(GEN x)
    4203             : {
    4204         287 :   pari_sp av = avma;
    4205             :   long r;
    4206             : 
    4207         287 :   if (typ(x)!=t_MAT) pari_err_TYPE("rank",x);
    4208         287 :   r = RgM_rank_fast(x);
    4209         280 :   if (r >= 0) return r;
    4210           7 :   (void)gauss_pivot(x, &r);
    4211           7 :   return gc_long(av, lg(x)-1 - r);
    4212             : }
    4213             : 
    4214             : /* d a t_VECSMALL of integers in 1..n. Return the vector of the d[i]
    4215             :  * followed by the missing indices */
    4216             : static GEN
    4217       15948 : perm_complete(GEN d, long n)
    4218             : {
    4219       15948 :   GEN y = cgetg(n+1, t_VECSMALL);
    4220       15948 :   long i, j = 1, k = n, l = lg(d);
    4221       15948 :   pari_sp av = avma;
    4222       15948 :   char *T = stack_calloc(n+1);
    4223       15948 :   for (i = 1; i < l; i++) T[d[i]] = 1;
    4224      150450 :   for (i = 1; i <= n; i++)
    4225      134502 :     if (T[i]) y[j++] = i; else y[k--] = i;
    4226       15948 :   set_avma(av); return y;
    4227             : }
    4228             : 
    4229             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
    4230             : static GEN
    4231        2422 : indeximage0(long n, long r, GEN d)
    4232             : {
    4233             :   long i, j;
    4234             :   GEN v;
    4235             : 
    4236        2422 :   r = n - r; /* now r = dim Im(x) */
    4237        2422 :   v = cgetg(r+1,t_VECSMALL);
    4238       16954 :   if (d) for (i=j=1; j<=n; j++)
    4239       14532 :     if (d[j]) v[i++] = j;
    4240        2422 :   return v;
    4241             : }
    4242             : /* x an m x n t_MAT, n > 0, r = dim Ker(x), d from gauss_pivot */
    4243             : static void
    4244        7974 : indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol)
    4245             : {
    4246        7974 :   GEN IR = indexrank0(n, r, d);
    4247        7974 :   *prow = perm_complete(gel(IR,1), m);
    4248        7974 :   *pcol = perm_complete(gel(IR,2), n);
    4249        7974 : }
    4250             : 
    4251             : static GEN
    4252          28 : RgM_indexrank_FpM(GEN x, GEN p)
    4253             : {
    4254          28 :   pari_sp av = avma;
    4255             :   ulong pp;
    4256             :   GEN r;
    4257          28 :   x = RgM_Fp_init(x,p,&pp);
    4258          28 :   switch(pp)
    4259             :   {
    4260           7 :   case 0:  r = FpM_indexrank(x,p); break;
    4261           7 :   case 2:  r = F2m_indexrank(x); break;
    4262          14 :   default: r = Flm_indexrank(x,pp); break;
    4263             :   }
    4264          28 :   return gerepileupto(av, r);
    4265             : }
    4266             : 
    4267             : static GEN
    4268           0 : RgM_indexrank_FqM(GEN x, GEN pol, GEN p)
    4269             : {
    4270           0 :   pari_sp av = avma;
    4271           0 :   GEN r, T = RgX_to_FpX(pol, p);
    4272           0 :   if (signe(T) == 0) pari_err_OP("indexrank",x,pol);
    4273           0 :   r = FqM_indexrank(RgM_to_FqM(x, T, p), T, p);
    4274           0 :   return gerepileupto(av, r);
    4275             : }
    4276             : 
    4277             : #define code(t1,t2) ((t1 << 6) | t2)
    4278             : static GEN
    4279       22428 : RgM_indexrank_fast(GEN x)
    4280             : {
    4281             :   GEN p, pol;
    4282             :   long pa;
    4283       22428 :   long t = RgM_type(x,&p,&pol,&pa);
    4284       22428 :   switch(t)
    4285             :   {
    4286         392 :     case t_INT:    return ZM_indexrank(x);
    4287        1148 :     case t_FRAC:   return QM_indexrank(x);
    4288          28 :     case t_INTMOD: return RgM_indexrank_FpM(x, p);
    4289          21 :     case t_FFELT:  return FFM_indexrank(x, pol);
    4290             :     case code(t_POLMOD, t_INTMOD):
    4291           0 :                    return RgM_indexrank_FqM(x, pol, p);
    4292       20839 :     default:       return NULL;
    4293             :   }
    4294             : }
    4295             : #undef code
    4296             : 
    4297             : GEN
    4298       22428 : indexrank(GEN x)
    4299             : {
    4300             :   pari_sp av;
    4301             :   long r;
    4302             :   GEN d;
    4303       22428 :   if (typ(x)!=t_MAT) pari_err_TYPE("indexrank",x);
    4304       22428 :   d = RgM_indexrank_fast(x);
    4305       22428 :   if (d) return d;
    4306       20839 :   av = avma;
    4307       20839 :   init_indexrank(x);
    4308       20839 :   d = gauss_pivot(x, &r);
    4309       20839 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    4310             : }
    4311             : 
    4312             : GEN
    4313        2422 : ZM_indeximage(GEN x) {
    4314        2422 :   pari_sp av = avma;
    4315             :   long r;
    4316             :   GEN d;
    4317        2422 :   init_indexrank(x);
    4318        2422 :   d = ZM_pivots(x,&r);
    4319        2422 :   set_avma(av); return indeximage0(lg(x)-1, r, d);
    4320             : }
    4321             : long
    4322       51525 : ZM_rank(GEN x) {
    4323       51525 :   pari_sp av = avma;
    4324             :   long r;
    4325       51525 :   (void)ZM_pivots(x,&r);
    4326       51525 :   return gc_long(av, lg(x)-1-r);
    4327             : }
    4328             : GEN
    4329       19684 : ZM_indexrank(GEN x) {
    4330       19684 :   pari_sp av = avma;
    4331             :   long r;
    4332             :   GEN d;
    4333       19684 :   init_indexrank(x);
    4334       19684 :   d = ZM_pivots(x,&r);
    4335       19684 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    4336             : }
    4337             : 
    4338             : long
    4339           0 : QM_rank(GEN x)
    4340             : {
    4341           0 :   pari_sp av = avma;
    4342           0 :   long r = ZM_rank(Q_primpart(x));
    4343           0 :   set_avma(av);
    4344           0 :   return r;
    4345             : }
    4346             : 
    4347             : GEN
    4348        1148 : QM_indexrank(GEN x)
    4349             : {
    4350        1148 :   pari_sp av = avma;
    4351        1148 :   GEN r = ZM_indexrank(Q_primpart(x));
    4352        1148 :   return gerepileupto(av, r);
    4353             : }
    4354             : 
    4355             : /*******************************************************************/
    4356             : /*                                                                 */
    4357             : /*                             ZabM                                */
    4358             : /*                                                                 */
    4359             : /*******************************************************************/
    4360             : 
    4361             : static GEN
    4362        1866 : FpXM_ratlift(GEN a, GEN q)
    4363             : {
    4364             :   GEN B, y;
    4365        1866 :   long i, j, l = lg(a), n;
    4366        1866 :   B = sqrti(shifti(q,-1));
    4367        1866 :   y = cgetg(l, t_MAT);
    4368        1866 :   if (l==1) return y;
    4369        1866 :   n = lgcols(a);
    4370        5351 :   for (i=1; i<l; i++)
    4371             :   {
    4372        4351 :     GEN yi = cgetg(n, t_COL);
    4373       56840 :     for (j=1; j<n; j++)
    4374             :     {
    4375       53355 :       GEN v = FpX_ratlift(gmael(a,i,j), q, B, B, NULL);
    4376       53355 :       if (!v) return NULL;
    4377       52489 :       gel(yi, j) = RgX_renormalize(v);
    4378             :     }
    4379        3485 :     gel(y,i) = yi;
    4380             :   }
    4381        1000 :   return y;
    4382             : }
    4383             : 
    4384             : static GEN
    4385        4658 : FlmV_recover_pre(GEN a, GEN M, ulong p, ulong pi, long sv)
    4386             : {
    4387        4658 :   GEN a1 = gel(a,1);
    4388        4658 :   long i, j, k, l = lg(a1), n, lM = lg(M);
    4389        4658 :   GEN v = cgetg(lM, t_VECSMALL);
    4390        4658 :   GEN y = cgetg(l, t_MAT);
    4391        4658 :   if (l==1) return y;
    4392        4658 :   n = lgcols(a1);
    4393       35625 :   for (i=1; i<l; i++)
    4394             :   {
    4395       30967 :     GEN yi = cgetg(n, t_COL);
    4396      681802 :     for (j=1; j<n; j++)
    4397             :     {
    4398      650836 :       for (k=1; k<lM; k++) uel(v,k) = umael(gel(a,k),i,j);
    4399      650836 :       gel(yi, j) = Flm_Flc_mul_pre_Flx(M, v, p, pi, sv);
    4400             :     }
    4401       30966 :     gel(y,i) = yi;
    4402             :   }
    4403        4658 :   return y;
    4404             : }
    4405             : 
    4406             : static GEN
    4407           0 : FlkM_inv(GEN M, GEN P, ulong p)
    4408             : {
    4409           0 :   ulong pi = get_Fl_red(p);
    4410           0 :   GEN R = Flx_roots(P, p);
    4411           0 :   long l = lg(R), i;
    4412           0 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    4413           0 :   GEN V = cgetg(l, t_VEC);
    4414           0 :   for(i=1; i<l; i++)
    4415             :   {
    4416           0 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, pi);
    4417           0 :     GEN H = Flm_inv_sp(FlxM_eval_powers_pre(M, pows, p, pi), NULL, p);
    4418           0 :     if (!H) return NULL;
    4419           0 :     gel(V, i) = H;
    4420             :   }
    4421           0 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    4422             : }
    4423             : 
    4424             : static GEN
    4425        2792 : FlkM_adjoint(GEN M, GEN P, ulong p)
    4426             : {
    4427        2792 :   ulong pi = get_Fl_red(p);
    4428        2792 :   GEN R = Flx_roots(P, p);
    4429        2792 :   long l = lg(R), i;
    4430        2792 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    4431        2792 :   GEN V = cgetg(l, t_VEC);
    4432       13956 :   for(i=1; i<l; i++)
    4433             :   {
    4434       11164 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, pi);
    4435       11163 :     gel(V, i) = Flm_adjoint(FlxM_eval_powers_pre(M, pows, p, pi), p);
    4436             :   }
    4437        2792 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    4438             : }
    4439             : 
    4440             : 
    4441             : static GEN
    4442        2583 : ZabM_inv_slice(GEN A, GEN Q, GEN P, GEN *mod)
    4443             : {
    4444        2583 :   pari_sp av = avma;
    4445        2583 :   long i, n = lg(P)-1, w = varn(Q);
    4446             :   GEN H, T;
    4447        2583 :   if (n == 1)
    4448             :   {
    4449        2453 :     ulong p = uel(P,1);
    4450        2453 :     GEN Ap = FqM_to_FlxM(A, Q, utoi(p));
    4451        2453 :     GEN Qp = ZX_to_Flx(Q, p);
    4452        2453 :     GEN Hp = FlkM_adjoint(Ap, Qp, p);
    4453        2453 :     Hp = gerepileupto(av, FlxM_to_ZXM(Hp));
    4454        2453 :     *mod = utoi(p); return Hp;
    4455             :   }
    4456         130 :   T = ZV_producttree(P);
    4457         130 :   A = ZXM_nv_mod_tree(A, P, T, w);
    4458         130 :   Q = ZX_nv_mod_tree(Q, P, T);
    4459         130 :   H = cgetg(n+1, t_VEC);
    4460         469 :   for(i=1; i <= n; i++)
    4461             :   {
    4462         339 :     ulong p = P[i];
    4463         339 :     GEN a = gel(A,i), q = gel(Q, i);
    4464         339 :     gel(H,i) = FlkM_adjoint(a, q, p);
    4465             :   }
    4466         130 :   H = nxMV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    4467         130 :   *mod = gmael(T, lg(T)-1, 1);
    4468         130 :   gerepileall(av, 2, &H, mod);
    4469         130 :   return H;
    4470             : }
    4471             : 
    4472             : GEN
    4473        2583 : ZabM_inv_worker(GEN P, GEN A, GEN Q)
    4474             : {
    4475        2583 :   GEN V = cgetg(3, t_VEC);
    4476        2583 :   gel(V,1) = ZabM_inv_slice(A, Q, P, &gel(V,2));
    4477        2583 :   return V;
    4478             : }
    4479             : 
    4480             : static GEN
    4481        8239 : vecnorml1(GEN a)
    4482             : {
    4483             :   long i, l;
    4484        8239 :   GEN g = cgetg_copy(a, &l);
    4485      132930 :   for (i=1; i<l; i++)
    4486      124691 :     gel(g, i) = gnorml1_fake(gel(a,i));
    4487        8239 :   return g;
    4488             : }
    4489             : 
    4490             : static GEN
    4491        1792 : ZabM_true_Hadamard(GEN a)
    4492             : {
    4493        1792 :   pari_sp av = avma;
    4494        1792 :   long n = lg(a)-1, i;
    4495             :   GEN B;
    4496        1792 :   if (n == 0) return gen_1;
    4497        1792 :   if (n == 1) return gnorml1_fake(gcoeff(a,1,1));
    4498        1218 :   B = gen_1;
    4499        1218 :   for (i = 1; i <= n; i++) B = gmul(B, gnorml2(RgC_gtofp(vecnorml1(gel(a,i)),DEFAULTPREC)));
    4500        1218 :   return gerepileuptoint(av, ceil_safe(sqrtr_abs(B)));
    4501             : }
    4502             : 
    4503             : GEN
    4504        1792 : ZabM_inv(GEN A, GEN Q, long n, GEN *pt_den)
    4505             : {
    4506        1792 :   pari_sp av = avma;
    4507             :   forprime_t S;
    4508        1792 :   long m = lg(A)-1;
    4509             :   GEN bnd, H, D, d, mod, worker;
    4510        1792 :   if (m == 0)
    4511             :   {
    4512           0 :     if (pt_den) *pt_den = gen_1;
    4513           0 :     return cgetg(1, t_MAT);
    4514             :   }
    4515        1792 :   bnd = ZabM_true_Hadamard(A);
    4516        1792 :   worker = snm_closure(is_entry("_ZabM_inv_worker"), mkvec2(A, Q));
    4517        1792 :   u_forprime_arith_init(&S, HIGHBIT+1, ULONG_MAX, 1, n);
    4518        1792 :   H = gen_crt("ZabM_inv", worker, &S, NULL, expi(bnd), m, &mod,
    4519             :               nxMV_chinese_center, FpXM_center);
    4520        1792 :   D = RgMrow_RgC_mul(H, gel(A,1), 1);
    4521        1792 :   D = ZX_rem(D, Q);
    4522        1792 :   d = Z_content(mkvec2(H, D));
    4523        1792 :   if (d)
    4524             :   {
    4525         658 :     D = ZX_Z_divexact(D, d);
    4526         658 :     H = Q_div_to_int(H, d);
    4527             :   }
    4528        1792 :   if (pt_den)
    4529             :   {
    4530        1792 :     gerepileall(av, 2, &H, &D);
    4531        1792 :     *pt_den = D; return H;
    4532             :   }
    4533           0 :   return gerepileupto(av, H);
    4534             : }
    4535             : 
    4536             : GEN
    4537           0 : ZabM_inv_ratlift(GEN M, GEN P, long n, GEN *pden)
    4538             : {
    4539           0 :   pari_sp av2, av = avma;
    4540             :   GEN q, H;
    4541           0 :   ulong m = LONG_MAX>>1;
    4542           0 :   ulong p= 1 + m - (m % n);
    4543           0 :   long lM = lg(M);
    4544           0 :   if (lM == 1) { *pden = gen_1; return cgetg(1,t_MAT); }
    4545             : 
    4546           0 :   av2 = avma;
    4547           0 :   H = NULL;
    4548             :   for(;;)
    4549           0 :   {
    4550             :     GEN Hp, Pp, Mp, Hr;
    4551           0 :     do p += n; while(!uisprime(p));
    4552           0 :     Pp = ZX_to_Flx(P, p);
    4553           0 :     Mp = FqM_to_FlxM(M, P, utoi(p));
    4554           0 :     Hp = FlkM_inv(Mp, Pp, p);
    4555           0 :     if (!Hp) continue;
    4556           0 :     if (!H)
    4557             :     {
    4558           0 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p);
    4559           0 :       q = utoipos(p);
    4560             :     }
    4561             :     else
    4562           0 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    4563           0 :     Hr = FpXM_ratlift(H, q);
    4564           0 :     if (DEBUGLEVEL>5) err_printf("ZabM_inv mod %ld (ratlift=%ld)\n", p,!!Hr);
    4565           0 :     if (Hr) {/* DONE ? */
    4566           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    4567           0 :       GEN MH = ZXQM_mul(Hl, M, P);
    4568           0 :       if (*pden)
    4569           0 :       { if (RgM_isscalar(MH, *pden)) { H = Hl; break; }}
    4570             :       else
    4571           0 :       { if (RgM_isidentity(MH)) { H = Hl; *pden = gen_1; break; } }
    4572             :     }
    4573             : 
    4574           0 :     if (gc_needed(av,2))
    4575             :     {
    4576           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_inv");
    4577           0 :       gerepileall(av2, 2, &H, &q);
    4578             :     }
    4579             :   }
    4580           0 :   gerepileall(av, 2, &H, pden);
    4581           0 :   return H;
    4582             : }
    4583             : 
    4584             : static GEN
    4585        1866 : FlkM_ker(GEN M, GEN P, ulong p)
    4586             : {
    4587        1866 :   ulong pi = get_Fl_red(p);
    4588        1866 :   GEN R = Flx_roots(P, p);
    4589        1866 :   long l = lg(R), i, dP = degpol(P), r;
    4590             :   GEN M1, K, D;
    4591        1866 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    4592        1866 :   GEN V = cgetg(l, t_VEC);
    4593        1866 :   M1 = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,1), dP, p, pi), p, pi);
    4594        1866 :   K = Flm_ker_sp(M1, p, 2);
    4595        1866 :   r = lg(gel(K,1)); D = gel(K,2);
    4596        1866 :   gel(V, 1) = gel(K,1);
    4597        3818 :   for(i=2; i<l; i++)
    4598             :   {
    4599        1952 :     GEN Mi = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,i), dP, p, pi), p, pi);
    4600        1952 :     GEN K = Flm_ker_sp(Mi, p, 2);
    4601        1952 :     if (lg(gel(K,1)) != r || !zv_equal(D, gel(K,2))) return NULL;
    4602        1952 :     gel(V, i) = gel(K,1);
    4603             :   }
    4604        1866 :   return mkvec2(FlmV_recover_pre(V, W, p, pi, P[1]), D);
    4605             : }
    4606             : 
    4607             : GEN
    4608         966 : ZabM_ker(GEN M, GEN P, long n)
    4609             : {
    4610         966 :   pari_sp av2, av = avma;
    4611             :   GEN q, H, D;
    4612         966 :   ulong m = LONG_MAX>>1;
    4613         966 :   ulong p= 1 + m - (m % n);
    4614         966 :   av2 = avma;
    4615         966 :   H = NULL; D = NULL;
    4616             :   for(;;)
    4617         900 :   {
    4618             :     GEN Kp, Hp, Dp, Pp, Mp, Hr;
    4619       30269 :     do p += n; while(!uisprime(p));
    4620        1866 :     Pp = ZX_to_Flx(P, p);
    4621        1866 :     Mp = FqM_to_FlxM(M, P, utoi(p));
    4622        1866 :     Kp = FlkM_ker(Mp, Pp, p);
    4623        1866 :     if (!Kp) continue;
    4624        1866 :     Hp = gel(Kp,1); Dp = gel(Kp,2);
    4625        1866 :     if (H && (lg(Hp)>lg(H) || (lg(Hp)==lg(H) && vecsmall_lexcmp(Dp,D)>0))) continue;
    4626        1866 :     if (!H || (lg(Hp)<lg(H) || vecsmall_lexcmp(Dp,D)<0))
    4627             :     {
    4628         966 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p); D = Dp;
    4629         966 :       q = utoipos(p);
    4630             :     }
    4631             :     else
    4632         900 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    4633        1866 :     Hr = FpXM_ratlift(H, q);
    4634        1866 :     if (DEBUGLEVEL>5) err_printf("ZabM_ker mod %ld (ratlift=%ld)\n", p,!!Hr);
    4635        1866 :     if (Hr) {/* DONE ? */
    4636        1000 :       GEN Hl = vec_Q_primpart(Hr);
    4637        1000 :       GEN MH = ZXQM_mul(M, Hl,P);
    4638        1000 :       if (gequal0(MH)) { H = Hl;  break; }
    4639             :     }
    4640             : 
    4641         900 :     if (gc_needed(av,2))
    4642             :     {
    4643           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_ker");
    4644           0 :       gerepileall(av2, 3, &H, &D, &q);
    4645             :     }
    4646             :   }
    4647         966 :   return gerepilecopy(av, H);
    4648             : }
    4649             : 
    4650             : GEN
    4651        2352 : ZabM_indexrank(GEN M, GEN P, long n)
    4652             : {
    4653        2352 :   pari_sp av = avma;
    4654        2352 :   ulong m = LONG_MAX>>1;
    4655        2352 :   ulong p = 1+m-(m%n), D = degpol(P);
    4656        2352 :   long lM = lg(M), lmax = 0, c = 0;
    4657             :   GEN v;
    4658             :   for(;;)
    4659         672 :   {
    4660             :     GEN R, Mp, K;
    4661             :     ulong pi;
    4662             :     long l;
    4663       60911 :     do p += n; while (!uisprime(p));
    4664        3024 :     pi = get_Fl_red(p);
    4665        3024 :     R = Flx_roots(ZX_to_Flx(P, p), p);
    4666        3024 :     Mp = FqM_to_FlxM(M, P, utoipos(p));
    4667        3024 :     K = FlxM_eval_powers_pre(Mp, Fl_powers_pre(uel(R,1), D,p,pi), p,pi);
    4668        3024 :     v = Flm_indexrank(K, p);
    4669        3024 :     l = lg(gel(v,2));
    4670        3024 :     if (l == lM) break;
    4671         896 :     if (lmax >= 0 && l > lmax) { lmax = l; c = 0; } else c++;
    4672         896 :     if (c > 2)
    4673             :     { /* probably not maximal rank, expensive check */
    4674         224 :       lM -= lg(ZabM_ker(M, P, n))-1; /* actual rank (+1) */
    4675         224 :       if (lmax == lM) break;
    4676           0 :       lmax = -1; /* disable check */
    4677             :     }
    4678             :   }
    4679        2352 :   return gerepileupto(av, v);
    4680             : }
    4681             : 
    4682             : #if 0
    4683             : GEN
    4684             : ZabM_gauss(GEN M, GEN P, long n, GEN *den)
    4685             : {
    4686             :   pari_sp av = avma;
    4687             :   GEN v, S, W;
    4688             :   v = ZabM_indexrank(M, P, n);
    4689             :   S = shallowmatextract(M,gel(v,1),gel(v,2));
    4690             :   W = ZabM_inv(S, P, n, den);
    4691             :   gerepileall(av,2,&W,den);
    4692             :   return W;
    4693             : }
    4694             : #endif
    4695             : 
    4696             : GEN
    4697         287 : ZabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *den)
    4698             : {
    4699         287 :   GEN v = ZabM_indexrank(M, P, n);
    4700         287 :   if (pv) *pv = v;
    4701         287 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    4702         287 :   return ZabM_inv(M, P, n, den);
    4703             : }
    4704             : GEN
    4705        4515 : ZM_pseudoinv(GEN M, GEN *pv, GEN *den)
    4706             : {
    4707        4515 :   GEN v = ZM_indexrank(M);
    4708        4515 :   if (pv) *pv = v;
    4709        4515 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    4710        4515 :   return ZM_inv(M, den);
    4711             : }
    4712             : 
    4713             : /*******************************************************************/
    4714             : /*                                                                 */
    4715             : /*                   Structured Elimination                        */
    4716             : /*                                                                 */
    4717             : /*******************************************************************/
    4718             : 
    4719             : static void
    4720      100901 : rem_col(GEN c, long i, GEN iscol, GEN Wrow, long *rcol, long *rrow)
    4721             : {
    4722      100901 :   long lc = lg(c), k;
    4723      100901 :   iscol[i] = 0; (*rcol)--;
    4724      892525 :   for (k = 1; k < lc; ++k)
    4725             :   {
    4726      791624 :     Wrow[c[k]]--;
    4727      791624 :     if (Wrow[c[k]]==0) (*rrow)--;
    4728             :   }
    4729      100901 : }
    4730             : 
    4731             : static void
    4732        6053 : rem_singleton(GEN M, GEN iscol, GEN Wrow, long *rcol, long *rrow)
    4733             : {
    4734             :   long i, j;
    4735        6053 :   long nbcol = lg(iscol)-1, last;
    4736             :   do
    4737             :   {
    4738        7995 :     last = 0;
    4739    18976678 :     for (i = 1; i <= nbcol; ++i)
    4740    18968683 :       if (iscol[i])
    4741             :       {
    4742     9754154 :         GEN c = gmael(M, i, 1);
    4743     9754154 :         long lc = lg(c);
    4744    91071107 :         for (j = 1; j < lc; ++j)
    4745    81329731 :           if (Wrow[c[j]] == 1)
    4746             :           {
    4747       12778 :             rem_col(c, i, iscol, Wrow, rcol, rrow);
    4748       12778 :             last=1; break;
    4749             :           }
    4750             :       }
    4751        7995 :   } while (last);
    4752        6053 : }
    4753             : 
    4754             : static GEN
    4755        5927 : fill_wcol(GEN M, GEN iscol, GEN Wrow, long *w, GEN wcol)
    4756             : {
    4757        5927 :   long nbcol = lg(iscol)-1;
    4758             :   long i, j, m, last;
    4759             :   GEN per;
    4760       14718 :   for (m = 2, last=0; !last ; m++)
    4761             :   {
    4762    22266243 :     for (i = 1; i <= nbcol; ++i)
    4763             :     {
    4764    22257452 :       wcol[i] = 0;
    4765    22257452 :       if (iscol[i])
    4766             :       {
    4767    11426017 :         GEN c = gmael(M, i, 1);
    4768    11426017 :         long lc = lg(c);
    4769   106719244 :         for (j = 1; j < lc; ++j)
    4770    95293227 :           if (Wrow[c[j]] == m) {  wcol[i]++; last = 1; }
    4771             :       }
    4772             :     }
    4773             :   }
    4774        5927 :   per = vecsmall_indexsort(wcol);
    4775        5927 :   *w = wcol[per[nbcol]];
    4776        5927 :   return per;
    4777             : }
    4778             : 
    4779             : /* M is a RgMs with nbrow rows, A a list of row indices.
    4780             :    Eliminate rows of M with a single entry that do not belong to A,
    4781             :    and the corresponding columns. Also eliminate columns until #colums=#rows.
    4782             :    Return pcol and prow:
    4783             :    pcol is a map from the new columns indices to the old one.
    4784             :    prow is a map from the old rows indices to the new one (0 if removed).
    4785             : */
    4786             : 
    4787             : void
    4788         126 : RgMs_structelim_col(GEN M, long nbcol, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    4789             : {
    4790             :   long i,j,k;
    4791         126 :   long lA = lg(A);
    4792         126 :   GEN prow = cgetg(nbrow+1, t_VECSMALL);
    4793         126 :   GEN pcol = zero_zv(nbcol);
    4794         126 :   pari_sp av = avma;
    4795         126 :   long rcol = nbcol, rrow = 0, imin = nbcol - usqrt(nbcol);
    4796         126 :   GEN iscol = const_vecsmall(nbcol, 1);
    4797         126 :   GEN Wrow  = zero_zv(nbrow);
    4798         126 :   GEN wcol = cgetg(nbcol+1, t_VECSMALL);
    4799         126 :   pari_sp av2=avma;
    4800      126721 :   for (i = 1; i <= nbcol; ++i)
    4801             :   {
    4802      126595 :     GEN F = gmael(M, i, 1);
    4803      126595 :     long l = lg(F)-1;
    4804     1118178 :     for (j = 1; j <= l; ++j)
    4805      991583 :       Wrow[F[j]]++;
    4806             :   }
    4807         126 :   for (j = 1; j < lA; ++j)
    4808             :   {
    4809           0 :     if (Wrow[A[j]] == 0) { *p_col=NULL; return; }
    4810           0 :     Wrow[A[j]] = -1;
    4811             :   }
    4812      237272 :   for (i = 1; i <= nbrow; ++i)
    4813      237146 :     if (Wrow[i])
    4814       67633 :       rrow++;
    4815         126 :   rem_singleton(M, iscol, Wrow, &rcol, &rrow);
    4816         126 :   if (rcol<rrow) pari_err_BUG("RgMs_structelim, rcol<rrow");
    4817        6179 :   for (; rcol>rrow;)
    4818             :   {
    4819             :     long w;
    4820        5927 :     GEN per = fill_wcol(M, iscol, Wrow, &w, wcol);
    4821       94050 :     for (i = nbcol; i>=imin && wcol[per[i]]>=w && rcol>rrow; i--)
    4822       88123 :       rem_col(gmael(M, per[i], 1), per[i], iscol, Wrow, &rcol, &rrow);
    4823        5927 :     rem_singleton(M, iscol, Wrow, &rcol, &rrow);
    4824        5927 :     set_avma(av2);
    4825             :   }
    4826      126721 :   for (j = 1, i = 1; i <= nbcol; ++i)
    4827      126595 :     if (iscol[i])
    4828       25694 :       pcol[j++] = i;
    4829         126 :   setlg(pcol,j);
    4830      237272 :   for (k = 1, i = 1; i <= nbrow; ++i)
    4831      237146 :     prow[i] = Wrow[i] ? k++: 0;
    4832         126 :   set_avma(av);
    4833         126 :   *p_col = pcol; *p_row = prow;
    4834             : }
    4835             : 
    4836             : void
    4837           0 : RgMs_structelim(GEN M, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    4838             : {
    4839           0 :   RgMs_structelim_col(M, lg(M)-1, nbrow, A, p_col, p_row);
    4840           0 : }
    4841             : 
    4842             : /*******************************************************************/
    4843             : /*                                                                 */
    4844             : /*                        EIGENVECTORS                             */
    4845             : /*   (independent eigenvectors, sorted by increasing eigenvalue)   */
    4846             : /*                                                                 */
    4847             : /*******************************************************************/
    4848             : /* assume x is square of dimension > 0 */
    4849             : static int
    4850          27 : RgM_is_symmetric_cx(GEN x, long bit)
    4851             : {
    4852          27 :   pari_sp av = avma;
    4853          27 :   long i, j, l = lg(x);
    4854         138 :   for (i = 1; i < l; i++)
    4855         418 :     for (j = 1; j < i; j++)
    4856             :     {
    4857         307 :       GEN a = gcoeff(x,i,j), b = gcoeff(x,j,i), c = gsub(a,b);
    4858         307 :       if (!gequal0(c) && gexpo(c) - gexpo(a) > -bit) return gc_long(av,0);
    4859             :     }
    4860          14 :   return gc_long(av,1);
    4861             : }
    4862             : static GEN
    4863          27 : eigen_err(int exact, GEN x, long flag, long prec)
    4864             : {
    4865          27 :   pari_sp av = avma;
    4866          27 :   if (RgM_is_symmetric_cx(x, prec2nbits(prec) - 10))
    4867             :   { /* approximately symmetric: recover */
    4868          14 :     x = jacobi(x, prec); if (flag) return x;
    4869           7 :     return gerepilecopy(av, gel(x,1));
    4870             :   }
    4871          13 :   if (exact)
    4872             :   {
    4873           6 :     GEN y = mateigen(x, flag, precdbl(prec));
    4874           6 :     return gerepilecopy(av, gprec_wtrunc(y, prec));
    4875             :   }
    4876           7 :   pari_err_PREC("mateigen");
    4877             :   return NULL; /* LCOV_EXCL_LINE */
    4878             : }
    4879             : GEN
    4880          97 : mateigen(GEN x, long flag, long prec)
    4881             : {
    4882             :   GEN y, R, T;
    4883          97 :   long k, l, ex, n = lg(x);
    4884             :   int exact;
    4885          97 :   pari_sp av = avma;
    4886             : 
    4887          97 :   if (typ(x)!=t_MAT) pari_err_TYPE("eigen",x);
    4888          97 :   if (n != 1 && n != lgcols(x)) pari_err_DIM("eigen");
    4889          97 :   if (flag < 0 || flag > 1) pari_err_FLAG("mateigen");
    4890          97 :   if (n == 1)
    4891             :   {
    4892          14 :     if (flag) retmkvec2(cgetg(1,t_VEC), cgetg(1,t_MAT));
    4893           7 :     return cgetg(1,t_VEC);
    4894             :   }
    4895          83 :   if (n == 2)
    4896             :   {
    4897          14 :     if (flag) retmkvec2(mkveccopy(gcoeff(x,1,1)), matid(1));
    4898           7 :     return matid(1);
    4899             :   }
    4900             : 
    4901          69 :   ex = 16 - prec2nbits(prec);
    4902          69 :   T = charpoly(x,0);
    4903          69 :   exact = RgX_is_QX(T);
    4904          69 :   if (exact)
    4905             :   {
    4906          41 :     T = ZX_radical( Q_primpart(T) );
    4907          41 :     R = nfrootsQ(T);
    4908          41 :     if (lg(R)-1 < degpol(T))
    4909             :     { /* add missing complex roots */
    4910          27 :       GEN r = cleanroots(RgX_div(T, roots_to_pol(R, 0)), prec);
    4911          27 :       settyp(r, t_VEC);
    4912          27 :       R = shallowconcat(R, r);
    4913             :     }
    4914             :   }
    4915             :   else
    4916             :   {
    4917          28 :     GEN r1, v = vectrunc_init(lg(T));
    4918             :     long e;
    4919          28 :     R = cleanroots(T,prec);
    4920          28 :     r1 = NULL;
    4921         168 :     for (k = 1; k < lg(R); k++)
    4922             :     {
    4923         140 :       GEN r2 = gel(R,k), r = grndtoi(r2, &e);
    4924         140 :       if (e < ex) r2 = r;
    4925         140 :       if (r1)
    4926             :       {
    4927         112 :         r = gsub(r1,r2);
    4928         112 :         if (gequal0(r) || gexpo(r) < ex) continue;
    4929             :       }
    4930          98 :       vectrunc_append(v, r2);
    4931          98 :       r1 = r2;
    4932             :     }
    4933          28 :     R = v;
    4934             :   }
    4935             :   /* R = distinct complex roots of charpoly(x) */
    4936          69 :   l = lg(R); y = cgetg(l, t_VEC);
    4937         279 :   for (k = 1; k < l; k++)
    4938             :   {
    4939         237 :     GEN F = ker_aux(RgM_Rg_sub_shallow(x, gel(R,k)), x);
    4940         237 :     long d = lg(F)-1;
    4941         237 :     if (!d) { set_avma(av); return eigen_err(exact, x, flag, prec); }
    4942         210 :     gel(y,k) = F;
    4943         210 :     if (flag) gel(R,k) = const_vec(d, gel(R,k));
    4944             :   }
    4945          42 :   y = shallowconcat1(y);
    4946          42 :   if (lg(y) > n) { set_avma(av); return eigen_err(exact, x, flag, prec); }
    4947             :   /* lg(y) < n if x is not diagonalizable */
    4948          42 :   if (flag) y = mkvec2(shallowconcat1(R), y);
    4949          42 :   return gerepilecopy(av,y);
    4950             : }
    4951             : GEN
    4952           0 : eigen(GEN x, long prec) { return mateigen(x, 0, prec); }
    4953             : 
    4954             : /*******************************************************************/
    4955             : /*                                                                 */
    4956             : /*                           DETERMINANT                           */
    4957             : /*                                                                 */
    4958             : /*******************************************************************/
    4959             : 
    4960             : GEN
    4961        4060 : det0(GEN a,long flag)
    4962             : {
    4963        4060 :   switch(flag)
    4964             :   {
    4965        4046 :     case 0: return det(a);
    4966          14 :     case 1: return det2(a);
    4967           0 :     default: pari_err_FLAG("matdet");
    4968             :   }
    4969             :   return NULL; /* LCOV_EXCL_LINE */
    4970             : }
    4971             : 
    4972             : /* M a 2x2 matrix, returns det(M) */
    4973             : static GEN
    4974        7942 : RgM_det2(GEN M)
    4975             : {
    4976        7942 :   pari_sp av = avma;
    4977        7942 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    4978        7942 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    4979        7942 :   return gerepileupto(av, gsub(gmul(a,d), gmul(b,c)));
    4980             : }
    4981             : /* M a 2x2 ZM, returns det(M) */
    4982             : static GEN
    4983        9161 : ZM_det2(GEN M)
    4984             : {
    4985        9161 :   pari_sp av = avma;
    4986        9161 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    4987        9161 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    4988        9161 :   return gerepileuptoint(av, subii(mulii(a,d), mulii(b, c)));
    4989             : }
    4990             : /* M a 3x3 ZM, return det(M) */
    4991             : static GEN
    4992        2961 : ZM_det3(GEN M)
    4993             : {
    4994        2961 :   pari_sp av = avma;
    4995        2961 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2), c = gcoeff(M,1,3);
    4996        2961 :   GEN d = gcoeff(M,2,1), e = gcoeff(M,2,2), f = gcoeff(M,2,3);
    4997        2961 :   GEN g = gcoeff(M,3,1), h = gcoeff(M,3,2), i = gcoeff(M,3,3);
    4998        2961 :   GEN t, D = signe(i)? mulii(subii(mulii(a,e), mulii(b,d)), i): gen_0;
    4999        2961 :   if (signe(g))
    5000             :   {
    5001        2737 :     t = mulii(subii(mulii(b,f), mulii(c,e)), g);
    5002        2737 :     D = addii(D, t);
    5003             :   }
    5004        2961 :   if (signe(h))
    5005             :   {
    5006        2758 :     t = mulii(subii(mulii(c,d), mulii(a,f)), h);
    5007        2758 :     D = addii(D, t);
    5008             :   }
    5009        2961 :   return gerepileuptoint(av, D);
    5010             : }
    5011             : 
    5012             : static GEN
    5013       10736 : det_simple_gauss(GEN a, GEN data, pivot_fun pivot)
    5014             : {
    5015       10736 :   pari_sp av = avma;
    5016       10736 :   long i,j,k, s = 1, nbco = lg(a)-1;
    5017       10736 :   GEN p, x = gen_1;
    5018             : 
    5019       10736 :   a = RgM_shallowcopy(a);
    5020       84369 :   for (i=1; i<nbco; i++)
    5021             :   {
    5022       73640 :     k = pivot(a, data, i, NULL);
    5023       73640 :     if (k > nbco) return gerepilecopy(av, gcoeff(a,i,i));
    5024       73633 :     if (k != i)
    5025             :     { /* exchange the lines s.t. k = i */
    5026        4694 :       for (j=i; j<=nbco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    5027        4694 :       s = -s;
    5028             :     }
    5029       73633 :     p = gcoeff(a,i,i);
    5030             : 
    5031       73633 :     x = gmul(x,p);
    5032      425969 :     for (k=i+1; k<=nbco; k++)
    5033             :     {
    5034      352336 :       GEN m = gcoeff(a,i,k);
    5035      352336 :       if (gequal0(m)) continue;
    5036             : 
    5037      125088 :       m = gdiv(m,p);
    5038      822348 :       for (j=i+1; j<=nbco; j++)
    5039      697260 :         gcoeff(a,j,k) = gsub(gcoeff(a,j,k), gmul(m,gcoeff(a,j,i)));
    5040             :     }
    5041       73633 :     if (gc_needed(av,2))
    5042             :     {
    5043           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    5044           0 :       gerepileall(av,2, &a,&x);
    5045             :     }
    5046             :   }
    5047       10729 :   if (s < 0) x = gneg_i(x);
    5048       10729 :   return gerepileupto(av, gmul(x, gcoeff(a,nbco,nbco)));
    5049             : }
    5050             : 
    5051             : GEN
    5052        6551 : det2(GEN a)
    5053             : {
    5054             :   GEN data;
    5055             :   pivot_fun pivot;
    5056        6551 :   long n = lg(a)-1;
    5057        6551 :   if (typ(a)!=t_MAT) pari_err_TYPE("det2",a);
    5058        6551 :   if (!n) return gen_1;
    5059        6551 :   if (n != nbrows(a)) pari_err_DIM("det2");
    5060        6551 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    5061        6551 :   if (n == 2) return RgM_det2(a);
    5062        2095 :   pivot = get_pivot_fun(a, a, &data);
    5063        2095 :   return det_simple_gauss(a, data, pivot);
    5064             : }
    5065             : 
    5066             : /* Assumes a a square t_MAT of dimension n > 0. Returns det(a) using
    5067             :  * Gauss-Bareiss. */
    5068             : static GEN
    5069         336 : det_bareiss(GEN a)
    5070             : {
    5071         336 :   pari_sp av = avma;
    5072         336 :   long nbco = lg(a)-1,i,j,k,s = 1;
    5073             :   GEN p, pprec;
    5074             : 
    5075         336 :   a = RgM_shallowcopy(a);
    5076        1078 :   for (pprec=gen_1,i=1; i<nbco; i++,pprec=p)
    5077             :   {
    5078         742 :     int diveuc = (gequal1(pprec)==0);
    5079             :     GEN ci;
    5080             : 
    5081         742 :     p = gcoeff(a,i,i);
    5082         742 :     if (gequal0(p))
    5083             :     {
    5084           0 :       k=i+1; while (k<=nbco && gequal0(gcoeff(a,i,k))) k++;
    5085           0 :       if (k>nbco) return gerepilecopy(av, p);
    5086           0 :       swap(gel(a,k), gel(a,i)); s = -s;
    5087           0 :       p = gcoeff(a,i,i);
    5088             :     }
    5089         742 :     ci = gel(a,i);
    5090        2072 :     for (k=i+1; k<=nbco; k++)
    5091             :     {
    5092        1330 :       GEN ck = gel(a,k), m = gel(ck,i);
    5093        1330 :       if (gequal0(m))
    5094             :       {
    5095           0 :         if (gequal1(p))
    5096             :         {
    5097           0 :           if (diveuc)
    5098           0 :             gel(a,k) = gdiv(gel(a,k), pprec);
    5099             :         }
    5100             :         else
    5101           0 :           for (j=i+1; j<=nbco; j++)
    5102             :           {
    5103           0 :             GEN p1 = gmul(p, gel(ck,j));
    5104           0 :             if (diveuc) p1 = gdiv(p1,pprec);
    5105           0 :             gel(ck,j) = p1;
    5106             :           }
    5107             :       }
    5108             :       else
    5109        4200 :         for (j=i+1; j<=nbco; j++)
    5110             :         {
    5111        2870 :           pari_sp av2 = avma;
    5112        2870 :           GEN p1 = gsub(gmul(p,gel(ck,j)), gmul(m,gel(ci,j)));
    5113        2870 :           if (diveuc) p1 = gdiv(p1,pprec);
    5114        2870 :           gel(ck,j) = gerepileupto(av2, p1);
    5115             :         }
    5116        1330 :       if (gc_needed(av,2))
    5117             :       {
    5118           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    5119           0 :         gerepileall(av,2, &a,&pprec);
    5120           0 :         ci = gel(a,i);
    5121           0 :         p = gcoeff(a,i,i);
    5122             :       }
    5123             :     }
    5124             :   }
    5125         336 :   p = gcoeff(a,nbco,nbco);
    5126         336 :   p = (s < 0)? gneg(p): gcopy(p);
    5127         336 :   return gerepileupto(av, p);
    5128             : }
    5129             : 
    5130             : /* count non-zero entries in col j, at most 'max' of them.
    5131             :  * Return their indices */
    5132             : static GEN
    5133        1148 : col_count_non_zero(GEN a, long j, long max)
    5134             : {
    5135        1148 :   GEN v = cgetg(max+1, t_VECSMALL);
    5136        1148 :   GEN c = gel(a,j);
    5137        1148 :   long i, l = lg(a), k = 1;
    5138        4410 :   for (i = 1; i < l; i++)
    5139        4284 :     if (!gequal0(gel(c,i)))
    5140             :     {
    5141        4144 :       if (k > max) return NULL; /* fail */
    5142        3122 :       v[k++] = i;
    5143             :     }
    5144         126 :   setlg(v, k); return v;
    5145             : }
    5146             : /* count non-zero entries in row i, at most 'max' of them.
    5147             :  * Return their indices */
    5148             : static GEN
    5149        1134 : row_count_non_zero(GEN a, long i, long max)
    5150             : {
    5151        1134 :   GEN v = cgetg(max+1, t_VECSMALL);
    5152        1134 :   long j, l = lg(a), k = 1;
    5153        4354 :   for (j = 1; j < l; j++)
    5154        4242 :     if (!gequal0(gcoeff(a,i,j)))
    5155             :     {
    5156        4130 :       if (k > max) return NULL; /* fail */
    5157        3108 :       v[k++] = j;
    5158             :     }
    5159         112 :   setlg(v, k); return v;
    5160             : }
    5161             : 
    5162             : static GEN det_develop(GEN a, long max, double bound);
    5163             : /* (-1)^(i+j) a[i,j] * det RgM_minor(a,i,j) */
    5164             : static GEN
    5165         210 : coeff_det(GEN a, long i, long j, long max, double bound)
    5166             : {
    5167         210 :   GEN c = gcoeff(a, i, j);
    5168         210 :   c = gmul(c, det_develop(RgM_minor(a, i,j), max, bound));
    5169         210 :   if (odd(i+j)) c = gneg(c);
    5170         210 :   return c;
    5171             : }
    5172             : /* a square t_MAT, 'bound' a rough upper bound for the number of
    5173             :  * multiplications we are willing to pay while developing rows/columns before
    5174             :  * switching to Gaussian elimination */
    5175             : static GEN
    5176         448 : det_develop(GEN M, long max, double bound)
    5177             : {
    5178         448 :   pari_sp av = avma;
    5179         448 :   long i,j, n = lg(M)-1, lbest = max+2, best_col = 0, best_row = 0;
    5180         448 :   GEN best = NULL;
    5181             : 
    5182         448 :   if (bound < 1.) return det_bareiss(M); /* too costly now */
    5183             : 
    5184         336 :   switch(n)
    5185             :   {
    5186           0 :     case 0: return gen_1;
    5187           0 :     case 1: return gcopy(gcoeff(M,1,1));
    5188          14 :     case 2: return RgM_det2(M);
    5189             :   }
    5190         322 :   if (max > ((n+2)>>1)) max = (n+2)>>1;
    5191        1456 :   for (j = 1; j <= n; j++)
    5192             :   {
    5193        1148 :     pari_sp av2 = avma;
    5194        1148 :     GEN v = col_count_non_zero(M, j, max);
    5195             :     long lv;
    5196        1148 :     if (!v || (lv = lg(v)) >= lbest) { set_avma(av2); continue; }
    5197          98 :     if (lv == 1) { set_avma(av); return gen_0; }
    5198          98 :     if (lv == 2) {
    5199          14 :       set_avma(av);
    5200          14 :       return gerepileupto(av, coeff_det(M,v[1],j,max,bound));
    5201             :     }
    5202          84 :     best = v; lbest = lv; best_col = j;
    5203             :   }
    5204        1442 :   for (i = 1; i <= n; i++)
    5205             :   {
    5206        1134 :     pari_sp av2 = avma;
    5207        1134 :     GEN v = row_count_non_zero(M, i, max);
    5208             :     long lv;
    5209        1134 :     if (!v || (lv = lg(v)) >= lbest) { set_avma(av2); continue; }
    5210           0 :     if (lv == 1) { set_avma(av); return gen_0; }
    5211           0 :     if (lv == 2) {
    5212           0 :       set_avma(av);
    5213           0 :       return gerepileupto(av, coeff_det(M,i,v[1],max,bound));
    5214             :     }
    5215           0 :     best = v; lbest = lv; best_row = i;
    5216             :   }
    5217         308 :   if (best_row)
    5218             :   {
    5219           0 :     double d = lbest-1;
    5220           0 :     GEN s = NULL;
    5221             :     long k;
    5222           0 :     bound /= d*d*d;
    5223           0 :     for (k = 1; k < lbest; k++)
    5224             :     {
    5225           0 :       GEN c = coeff_det(M, best_row, best[k], max, bound);
    5226           0 :       s = s? gadd(s, c): c;
    5227             :     }
    5228           0 :     return gerepileupto(av, s);
    5229             :   }
    5230         308 :   if (best_col)
    5231             :   {
    5232          84 :     double d = lbest-1;
    5233          84 :     GEN s = NULL;
    5234             :     long k;
    5235          84 :     bound /= d*d*d;
    5236         280 :     for (k = 1; k < lbest; k++)
    5237             :     {
    5238         196 :       GEN c = coeff_det(M, best[k], best_col, max, bound);
    5239         196 :       s = s? gadd(s, c): c;
    5240             :     }
    5241          84 :     return gerepileupto(av, s);
    5242             :   }
    5243         224 :   return det_bareiss(M);
    5244             : }
    5245             : 
    5246             : /* area of parallelogram bounded by (v1,v2) */
    5247             : static GEN
    5248       54653 : parallelogramarea(GEN v1, GEN v2)
    5249       54653 : { return gsub(gmul(gnorml2(v1), gnorml2(v2)), gsqr(RgV_dotproduct(v1, v2))); }
    5250             : 
    5251             : /* Square of Hadamard bound for det(a), a square matrix.
    5252             :  * Slightly improvement: instead of using the column norms, use the area of
    5253             :  * the parallelogram formed by pairs of consecutive vectors */
    5254             : GEN
    5255       17158 : RgM_Hadamard(GEN a)
    5256             : {
    5257       17158 :   pari_sp av = avma;
    5258       17158 :   long n = lg(a)-1, i;
    5259             :   GEN B;
    5260       17158 :   if (n == 0) return gen_1;
    5261       17158 :   if (n == 1) return gsqr(gcoeff(a,1,1));
    5262       17158 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    5263       17158 :   B = gen_1;
    5264       71811 :   for (i = 1; i <= n/2; i++)
    5265       54653 :     B = gmul(B, parallelogramarea(gel(a,2*i-1), gel(a,2*i)));
    5266       17158 :   if (odd(n)) B = gmul(B, gnorml2(gel(a, n)));
    5267       17158 :   return gerepileuptoint(av, ceil_safe(B));
    5268             : }
    5269             : 
    5270             : /* If B=NULL, assume B=A' */
    5271             : static GEN
    5272       53108 : ZM_det_slice(GEN A, GEN P, GEN *mod)
    5273             : {
    5274       53108 :   pari_sp av = avma;
    5275       53108 :   long i, n = lg(P)-1;
    5276             :   GEN H, T;
    5277       53108 :   if (n == 1)
    5278             :   {
    5279       42215 :     ulong Hp, p = uel(P,1);
    5280       42215 :     GEN a = ZM_to_Flm(A, p);
    5281       42186 :     Hp = Flm_det_sp(a, p);
    5282       42159 :     set_avma(av);
    5283       42159 :     *mod = utoi(p); return utoi(Hp);
    5284             :   }
    5285       10893 :   T = ZV_producttree(P);
    5286       10889 :   A = ZM_nv_mod_tree(A, P, T);
    5287       10885 :   H = cgetg(n+1, t_VECSMALL);
    5288       38327 :   for(i=1; i <= n; i++)
    5289             :   {
    5290       27449 :     ulong p = P[i];
    5291       27449 :     GEN a = gel(A,i);
    5292       27449 :     H[i] = Flm_det_sp(a, p);
    5293             :   }
    5294       10878 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    5295       10881 :   *mod = gmael(T, lg(T)-1, 1);
    5296       10881 :   gerepileall(av, 2, &H, mod);
    5297       10891 :   return H;
    5298             : }
    5299             : 
    5300             : GEN
    5301       53121 : ZM_det_worker(GEN P, GEN A)
    5302             : {
    5303       53121 :   GEN V = cgetg(3, t_VEC);
    5304       53110 :   gel(V,1) = ZM_det_slice(A, P, &gel(V,2));
    5305       53022 :   return V;
    5306             : }
    5307             : 
    5308             : GEN
    5309       30127 : ZM_det(GEN M)
    5310             : {
    5311       30127 :   const long DIXON_THRESHOLD = 40;
    5312             :   pari_sp av, av2;
    5313       30127 :   long i, n = lg(M)-1;
    5314             :   ulong p, Dp;
    5315             :   forprime_t S;
    5316             :   pari_timer ti;
    5317             :   GEN H, D, mod, h, q, v, worker;
    5318             : 
    5319       30127 :   switch(n)
    5320             :   {
    5321           7 :     case 0: return gen_1;
    5322         840 :     case 1: return icopy(gcoeff(M,1,1));
    5323        9161 :     case 2: return ZM_det2(M);
    5324        2961 :     case 3: return ZM_det3(M);
    5325             :   }
    5326       17158 :   if (DEBUGLEVEL >=4) timer_start(&ti);
    5327       17158 :   av = avma; h = RgM_Hadamard(M);
    5328       17158 :   if (!signe(h)) { set_avma(av); return gen_0; }
    5329       17158 :   h = sqrti(h); q = gen_1; Dp = 1;
    5330       17158 :   init_modular_big(&S);
    5331       17158 :   p = 0; /* -Wall */
    5332       34316 :   while (cmpii(q, h) <= 0 && (p = u_forprime_next(&S)))
    5333             :   {
    5334       17158 :     av2 = avma; Dp = Flm_det_sp(ZM_to_Flm(M, p), p);
    5335       17158 :     set_avma(av2);
    5336       17158 :     if (Dp) break;
    5337           0 :     q = muliu(q, p);
    5338             :   }
    5339       17158 :   if (!p) pari_err_OVERFLOW("ZM_det [ran out of primes]");
    5340       17158 :   if (!Dp) { set_avma(av); return gen_0; }
    5341       17158 :   if (n <= DIXON_THRESHOLD)
    5342       17158 :     D = q;
    5343             :   else
    5344             :   {
    5345           0 :     av2 = avma;
    5346           0 :     v = cgetg(n+1, t_COL);
    5347           0 :     gel(v, 1) = gen_1; /* ensure content(v) = 1 */
    5348           0 :     for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    5349           0 :     D = Q_denom(ZM_gauss(M, v));
    5350           0 :     if (expi(D) < expi(h) >> 1)
    5351             :     { /* First try unlucky, try once more */
    5352           0 :       for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    5353           0 :       D = lcmii(D, Q_denom(ZM_gauss(M, v)));
    5354             :     }
    5355           0 :     D = gerepileuptoint(av2, D);
    5356           0 :     if (q != gen_1) D = lcmii(D, q);
    5357             :   }
    5358             :   /* determinant is a multiple of D */
    5359       17158 :   if (DEBUGLEVEL >=4)
    5360           0 :     timer_printf(&ti,"ZM_det: Dixon %ld/%ld bits",expi(D),expi(h));
    5361       17158 :   h = divii(h, D);
    5362       17158 :   worker = snm_closure(is_entry("_ZM_det_worker"), mkvec(M));
    5363       17158 :   H = gen_crt("ZM_det", worker, &S, D, expi(h)+1, lg(M)-1, &mod,
    5364             :               ZV_chinese, NULL);
    5365       17158 :   if (D) H = Fp_div(H, D, mod);
    5366       17158 :   H = Fp_center(H, mod, shifti(mod,-1));
    5367       17158 :   if (D) H = mulii(H, D);
    5368       17158 :   return gerepileuptoint(av, H);
    5369             : }
    5370             : 
    5371             : static GEN
    5372        1519 : RgM_det_FpM(GEN a, GEN p)
    5373             : {
    5374        1519 :   pari_sp av = avma;
    5375             :   ulong pp, d;
    5376        1519 :   a = RgM_Fp_init(a,p,&pp);
    5377        1519 :   switch(pp)
    5378             :   {
    5379          70 :   case 0: return gerepileupto(av, Fp_to_mod(FpM_det(a,p),p)); break;
    5380          14 :   case 2: d = F2m_det_sp(a); break;
    5381        1435 :   default:d = Flm_det_sp(a, pp); break;
    5382             :   }
    5383        1449 :   set_avma(av); return mkintmodu(d, pp);
    5384             : }
    5385             : 
    5386             : static GEN
    5387          42 : RgM_det_FqM(GEN x, GEN pol, GEN p)
    5388             : {
    5389          42 :   pari_sp av = avma;
    5390          42 :   GEN b, T = RgX_to_FpX(pol, p);
    5391          42 :   if (signe(T) == 0) pari_err_OP("%",x,pol);
    5392          42 :   b = FqM_det(RgM_to_FqM(x, T, p), T, p);
    5393          42 :   if (!b) return gc_NULL(av);
    5394          42 :   return gerepilecopy(av, mkpolmod(FpX_to_mod(b, p), FpX_to_mod(T, p)));
    5395             : }
    5396             : 
    5397             : #define code(t1,t2) ((t1 << 6) | t2)
    5398             : static GEN
    5399       11035 : RgM_det_fast(GEN x)
    5400             : {
    5401             :   GEN p, pol;
    5402             :   long pa;
    5403       11035 :   long t = RgM_type(x, &p,&pol,&pa);
    5404       11035 :   switch(t)
    5405             :   {
    5406         336 :     case t_INT:    return ZM_det(x);
    5407         196 :     case t_FRAC:   return QM_det(x);
    5408          63 :     case t_FFELT:  return FFM_det(x, pol);
    5409        1519 :     case t_INTMOD: return RgM_det_FpM(x, p);
    5410             :     case code(t_POLMOD, t_INTMOD):
    5411          42 :                    return RgM_det_FqM(x, pol, p);
    5412        8879 :     default:       return NULL;
    5413             :   }
    5414             : }
    5415             : #undef code
    5416             : 
    5417             : static long
    5418         238 : det_init_max(long n)
    5419             : {
    5420         238 :   if (n > 100) return 0;
    5421         238 :   if (n > 50) return 1;
    5422         238 :   if (n > 30) return 4;
    5423         238 :   return 7;
    5424             : }
    5425             : 
    5426             : GEN
    5427       15121 : det(GEN a)
    5428             : {
    5429       15121 :   long n = lg(a)-1;
    5430             :   double B;
    5431             :   GEN data, b;
    5432             :   pivot_fun pivot;
    5433             : 
    5434       15121 :   if (typ(a)!=t_MAT) pari_err_TYPE("det",a);
    5435       15121 :   if (!n) return gen_1;
    5436       15079 :   if (n != nbrows(a)) pari_err_DIM("det");
    5437       15072 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    5438       14507 :   if (n == 2) return RgM_det2(a);
    5439       11035 :   b = RgM_det_fast(a);
    5440       11035 :   if (b) return b;
    5441        8879 :   pivot = get_pivot_fun(a, a, &data);
    5442        8879 :   if (pivot != gauss_get_pivot_NZ) return det_simple_gauss(a, data, pivot);
    5443         238 :   B = (double)n;
    5444         238 :   return det_develop(a, det_init_max(n), B*B*B);
    5445             : }
    5446             : 
    5447             : GEN
    5448         196 : QM_det(GEN M)
    5449             : {
    5450         196 :   pari_sp av = avma;
    5451         196 :   GEN cM, pM = Q_primitive_part(M, &cM);
    5452         196 :   GEN b = ZM_det(pM);
    5453         196 :   if (cM) b = gmul(b, gpowgs(cM, lg(M)-1));
    5454         196 :   return gerepileupto(av, b);
    5455             : }

Generated by: LCOV version 1.13