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.0 lcov report (development 23690-5d6e28857) Lines: 2551 2958 86.2 %
Date: 2019-03-18 05:43:21 Functions: 279 300 93.0 %
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      147483 : _gen_addmul(GEN b, long k, long i, GEN m, void *E, const struct bb_field *ff)
     289             : {
     290      147483 :   gel(b,i) = ff->red(E,gel(b,i));
     291      147483 :   gel(b,k) = ff->add(E,gel(b,k), ff->mul(E,m, gel(b,i)));
     292      147483 : }
     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       63185 :     for (k = i; k <= li; k++)
     326             :     {
     327       63129 :       GEN piv = ff->red(E,gcoeff(a,k,i));
     328       63129 :       if (!ff->equal0(piv)) { gcoeff(a,k,i) = ff->inv(E,piv); break; }
     329       10325 :       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        8337 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
     336        8337 :       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       18463 :       m = ff->red(E,ff->neg(E,ff->mul(E,m, invpiv)));
     347       18463 :       for (j=i+1; j<=aco; j++) _gen_addmul(gel(a,j),k,i,m,E,ff);
     348       18463 :       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      557535 : gen_matcolmul_i(GEN A, GEN B, ulong lgA, ulong l,
     366             :                 void *E, const struct bb_field *ff)
     367             : {
     368      557535 :   GEN C = cgetg(l, t_COL);
     369             :   ulong i;
     370     3681736 :   for (i = 1; i < l; i++) {
     371     3124201 :     pari_sp av = avma;
     372     3124201 :     GEN e = ff->mul(E, gcoeff(A, i, 1), gel(B, 1));
     373             :     ulong k;
     374    12782241 :     for(k = 2; k < lgA; k++)
     375     9658040 :       e = ff->add(E, e, ff->mul(E, gcoeff(A, i, k), gel(B, k)));
     376     3124201 :     gel(C, i) = gerepileupto(av, ff->red(E, e));
     377             :   }
     378      557535 :   return C;
     379             : }
     380             : 
     381             : GEN
     382      170296 : gen_matcolmul(GEN A, GEN B, void *E, const struct bb_field *ff)
     383             : {
     384      170296 :   ulong lgA = lg(A);
     385      170296 :   if (lgA != (ulong)lg(B))
     386           0 :     pari_err_OP("operation 'gen_matcolmul'", A, B);
     387      170296 :   if (lgA == 1)
     388           0 :     return cgetg(1, t_COL);
     389      170296 :   return gen_matcolmul_i(A, B, lgA, lgcols(A), E, ff);
     390             : }
     391             : 
     392             : static GEN
     393       75402 : 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       75402 :   GEN C = cgetg(lb, t_MAT);
     398      462641 :   for(j = 1; j < lb; j++)
     399      387239 :     gel(C, j) = gen_matcolmul_i(A, gel(B, j), la, l, E, ff);
     400       75402 :   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       75402 : gen_matmul_i(GEN A, GEN B, long l, long la, long lb,
     584             :              void *E, const struct bb_field *ff)
     585             : {
     586       75402 :   if (l <= gen_matmul_sw_bound
     587           7 :       || la <= gen_matmul_sw_bound
     588           0 :       || lb <= gen_matmul_sw_bound)
     589       75402 :     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       75402 : gen_matmul(GEN A, GEN B, void *E, const struct bb_field *ff)
     596             : {
     597       75402 :   ulong lgA, lgB = lg(B);
     598       75402 :   if (lgB == 1)
     599           0 :     return cgetg(1, t_MAT);
     600       75402 :   lgA = lg(A);
     601       75402 :   if (lgA != (ulong)lgcols(B))
     602           0 :     pari_err_OP("operation 'gen_matmul'", A, B);
     603       75402 :   if (lgA == 1)
     604           0 :     return zeromat(0, lgB - 1);
     605       75402 :   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       88702 : image_from_pivot(GEN x, GEN d, long r)
     782             : {
     783             :   GEN y;
     784             :   long j, k;
     785             : 
     786       88702 :   if (!d) return gcopy(x);
     787             :   /* d left on stack for efficiency */
     788       86679 :   r = lg(x)-1 - r; /* = dim Im(x) */
     789       86679 :   y = cgetg(r+1,t_MAT);
     790      770515 :   for (j=k=1; j<=r; k++)
     791      683836 :     if (d[k]) gel(y,j++) = gcopy(gel(x,k));
     792       86679 :   return y;
     793             : }
     794             : 
     795             : /* r = dim Ker x, n = nbrows(x) */
     796             : static GEN
     797       41706 : get_suppl(GEN x, GEN d, long n, long r, GEN(*ei)(long,long))
     798             : {
     799             :   pari_sp av;
     800             :   GEN y, c;
     801       41706 :   long j, k, rx = lg(x)-1; /* != 0 due to init_suppl() */
     802             : 
     803       41706 :   if (rx == n && r == 0) return gcopy(x);
     804       38362 :   y = cgetg(n+1, t_MAT);
     805       38362 :   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      290791 :   for (k = j = 1; j<=rx; j++)
     809      252429 :     if (d[j]) { c[ d[j] ] = 1; gel(y,k++) = gel(x,j); }
     810      388036 :   for (j=1; j<=n; j++)
     811      349674 :     if (!c[j]) gel(y,k++) = (GEN)j; /* HACK */
     812       38362 :   set_avma(av);
     813             : 
     814       38362 :   rx -= r;
     815       38362 :   for (j=1; j<=rx; j++) gel(y,j) = gcopy(gel(y,j));
     816       38362 :   for (   ; j<=n; j++)  gel(y,j) = ei(n, y[j]);
     817       38362 :   return y;
     818             : }
     819             : 
     820             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
     821             : static GEN
     822       96797 : indexrank0(long n, long r, GEN d)
     823             : {
     824       96797 :   GEN p1, p2, res = cgetg(3,t_VEC);
     825             :   long i, j;
     826             : 
     827       96797 :   r = n - r; /* now r = dim Im(x) */
     828       96797 :   p1 = cgetg(r+1,t_VECSMALL); gel(res,1) = p1;
     829       96797 :   p2 = cgetg(r+1,t_VECSMALL); gel(res,2) = p2;
     830       96797 :   if (d)
     831             :   {
     832      540918 :     for (i=0,j=1; j<=n; j++)
     833      445031 :       if (d[j]) { i++; p1[i] = d[j]; p2[i] = j; }
     834       95887 :     vecsmall_sort(p1);
     835             :   }
     836       96797 :   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          28 : FlxqM_FlxqC_mul(GEN A, GEN B, GEN T, ulong p) {
    1431             :   void *E;
    1432          28 :   const struct bb_field *ff = get_Flxq_field(&E, T, p);
    1433          28 :   return gen_matcolmul(A, B, E, ff);
    1434             : }
    1435             : 
    1436             : GEN
    1437          28 : FqM_FqC_mul(GEN A, GEN B, GEN T, GEN p) {
    1438             :   void *E;
    1439          28 :   const struct bb_field *ff = get_Fq_field(&E, T, p);
    1440          28 :   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      138288 : FlxqM_mul(GEN A, GEN B, GEN T, ulong p) {
    1452             :   void *E;
    1453             :   const struct bb_field *ff;
    1454      138288 :   long n = lg(A) - 1;
    1455             : 
    1456      138288 :   if (n == 0)
    1457           0 :     return cgetg(1, t_MAT);
    1458      138288 :   if (n > 1)
    1459       76158 :     return FlxqM_mul_Kronecker(A, B, T, p);
    1460       62130 :   ff = get_Flxq_field(&E, T, p);
    1461       62130 :   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      594120 : FpM_init(GEN a, GEN p, ulong *pp)
    1519             : {
    1520      594120 :   if (lgefint(p) == 3)
    1521             :   {
    1522      586854 :     *pp = uel(p,2);
    1523      586854 :     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      157057 : FpM_gauss_pivot(GEN x, GEN p, long *rr)
    1593             : {
    1594             :   ulong pp;
    1595      157057 :   if (lg(x)==1) { *rr = 0; return NULL; }
    1596      155146 :   x = FpM_init(x, p, &pp);
    1597      155146 :   switch(pp)
    1598             :   {
    1599         815 :   case 0: return FpM_gauss_pivot_gen(x, p, rr);
    1600       43707 :   case 2: return F2m_gauss_pivot(x, rr);
    1601      110624 :   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       87561 : FpM_image(GEN x, GEN p)
    1645             : {
    1646             :   long r;
    1647       87561 :   GEN d = FpM_gauss_pivot(x,p,&r); /* d left on stack for efficiency */
    1648       87561 :   return image_from_pivot(x,d,r);
    1649             : }
    1650             : 
    1651             : GEN
    1652        1043 : Flm_image(GEN x, ulong p)
    1653             : {
    1654             :   long r;
    1655        1043 :   GEN d = Flm_pivots(x, p, &r, 0); /* d left on stack for efficiency */
    1656        1043 :   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       50539 : FpM_FpC_invimage(GEN A, GEN x, GEN p)
    1789             : {
    1790       50539 :   pari_sp av = avma;
    1791             :   ulong pp;
    1792             :   GEN y;
    1793             : 
    1794       50539 :   A = FpM_init(A, p, &pp);
    1795       50539 :   switch(pp)
    1796             :   {
    1797           7 :   case 0: return FpM_FpC_invimage_gen(A, x, p);
    1798             :   case 2:
    1799       18665 :     y = F2m_F2c_invimage(A, ZV_to_F2v(x));
    1800       18665 :     if (!y) return y;
    1801       18665 :     y = F2c_to_ZC(y);
    1802       18665 :     return gerepileupto(av, y);
    1803             :   default:
    1804       31867 :     y = Flm_Flc_invimage(A, ZV_to_Flv(x, pp), pp);
    1805       31867 :     if (!y) return y;
    1806       31867 :     y = Flc_to_ZC(y);
    1807       31867 :     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      312641 : FpM_ker_i(GEN x, GEN p, long deplin)
    1841             : {
    1842      312641 :   pari_sp av = avma;
    1843             :   ulong pp;
    1844             :   GEN y;
    1845             : 
    1846      312641 :   if (lg(x)==1) return cgetg(1,t_MAT);
    1847      312641 :   x = FpM_init(x, p, &pp);
    1848      312641 :   switch(pp)
    1849             :   {
    1850        2147 :   case 0: return FpM_ker_gen(x,p,deplin);
    1851             :   case 2:
    1852       86158 :     y = F2m_ker_sp(x, deplin);
    1853       86158 :     if (!y) return gc_NULL(av);
    1854       86158 :     y = deplin? F2c_to_ZC(y): F2m_to_ZM(y);
    1855       86158 :     return gerepileupto(av, y);
    1856             :   default:
    1857      224336 :     y = Flm_ker_sp(x, pp, deplin);
    1858      224336 :     if (!y) return gc_NULL(av);
    1859      224336 :     y = deplin? Flc_to_ZC(y): Flm_to_ZM(y);
    1860      224336 :     return gerepileupto(av, y);
    1861             :   }
    1862             : }
    1863             : 
    1864             : GEN
    1865      226935 : 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       79770 : 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       71846 : FpM_gauss_i(GEN a, GEN b, GEN p, ulong *pp)
    1953             : {
    1954       71846 :   long n = nbrows(a);
    1955       71846 :   a = FpM_init(a,p,pp);
    1956       71846 :   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       22363 :     if (b) b = ZM_to_F2m(b); else b = matid_F2m(n);
    1963       22363 :     return F2m_gauss_sp(a,b);
    1964             :   default:
    1965       45501 :     if (b) b = ZM_to_Flm(b, *pp); else b = matid_Flm(n);
    1966       45501 :     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       71797 : FpM_inv(GEN a, GEN p)
    2100             : {
    2101       71797 :   pari_sp av = avma;
    2102             :   ulong pp;
    2103             :   GEN u;
    2104       71797 :   if (lg(a) == 1) return cgetg(1, t_MAT);
    2105       71797 :   u = FpM_gauss_i(a, NULL, p, &pp);
    2106       71797 :   if (!u) return gc_NULL(av);
    2107       71783 :   switch(pp)
    2108             :   {
    2109        3926 :   case 0: return gerepilecopy(av, u);
    2110       22356 :   case 2:  u = F2m_to_ZM(u); break;
    2111       45501 :   default: u = Flm_to_ZM(u); break;
    2112             :   }
    2113       67857 :   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       93882 : FpM_intersect(GEN x, GEN y, GEN p)
    2152             : {
    2153       93882 :   pari_sp av = avma;
    2154       93882 :   long j, lx = lg(x);
    2155             :   GEN z;
    2156             : 
    2157       93882 :   if (lx==1 || lg(y)==1) return cgetg(1,t_MAT);
    2158       93882 :   z = FpM_ker(shallowconcat(x,y), p);
    2159       93882 :   for (j=lg(z)-1; j; j--) setlg(gel(z,j),lx);
    2160       93882 :   return gerepileupto(av, FpM_mul(x,z,p));
    2161             : }
    2162             : 
    2163             : static void
    2164       41706 : init_suppl(GEN x)
    2165             : {
    2166       41706 :   if (lg(x) == 1) pari_err_IMPL("suppl [empty matrix]");
    2167             :   /* HACK: avoid overwriting d from gauss_pivot() after set_avma(av) */
    2168       41706 :   (void)new_chunk(lgcols(x) * 2);
    2169       41706 : }
    2170             : 
    2171             : GEN
    2172       40334 : FpM_suppl(GEN x, GEN p)
    2173             : {
    2174             :   GEN d;
    2175             :   long r;
    2176       40334 :   init_suppl(x); d = FpM_gauss_pivot(x,p, &r);
    2177       40334 :   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       91326 : init_indexrank(GEN x) {
    2229       91326 :   (void)new_chunk(3 + 2*lg(x)); /* HACK */
    2230       91326 : }
    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       19317 : Flm_indexrank(GEN x, ulong p) {
    2244       19317 :   pari_sp av = avma;
    2245             :   long r;
    2246             :   GEN d;
    2247       19317 :   init_indexrank(x);
    2248       19317 :   d = Flm_pivots(x, p, &r, 0);
    2249       19317 :   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      617761 : approx_0(GEN x, GEN y)
    2300             : {
    2301      617761 :   long tx = typ(x);
    2302      617761 :   if (tx == t_COMPLEX)
    2303         140 :     return approx_0(gel(x,1), y) && approx_0(gel(x,2), y);
    2304      617880 :   return gequal0(x) ||
    2305      429668 :          (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      638880 : gauss_get_pivot_max(GEN X, GEN X0, long ix, GEN c)
    2311             : {
    2312      638880 :   GEN p, r, x = gel(X,ix), x0 = gel(X0,ix);
    2313      638880 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2314      638880 :   if (c)
    2315             :   {
    2316      119308 :     for (i=1; i<lx; i++)
    2317       73836 :       if (!c[i])
    2318             :       {
    2319       37758 :         long e = gexpo(gel(x,i));
    2320       37758 :         if (e > ex) { ex = e; k = i; }
    2321             :       }
    2322             :   }
    2323             :   else
    2324             :   {
    2325     2070634 :     for (i=ix; i<lx; i++)
    2326             :     {
    2327     1477226 :       long e = gexpo(gel(x,i));
    2328     1477226 :       if (e > ex) { ex = e; k = i; }
    2329             :     }
    2330             :   }
    2331      638880 :   if (!k) return lx;
    2332      617586 :   p = gel(x,k);
    2333      617586 :   r = gel(x0,k); if (isrationalzero(r)) r = x0;
    2334      617586 :   return approx_0(p, r)? lx: k;
    2335             : }
    2336             : static long
    2337       63938 : gauss_get_pivot_padic(GEN X, GEN p, long ix, GEN c)
    2338             : {
    2339       63938 :   GEN x = gel(X, ix);
    2340       63938 :   long i, k = 0, ex = (long)HIGHVALPBIT, lx = lg(x);
    2341       63938 :   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      450723 :     for (i=ix; i<lx; i++)
    2353      386911 :       if (!gequal0(gel(x,i)))
    2354             :       {
    2355      183847 :         long e = gvaluation(gel(x,i), p);
    2356      183847 :         if (e < ex) { ex = e; k = i; }
    2357             :       }
    2358             :   }
    2359       63938 :   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      210316 : get_pivot_fun(GEN x, GEN x0, GEN *data)
    2387             : {
    2388      210316 :   long i, j, hx, lx = lg(x);
    2389      210316 :   int res = t_INT;
    2390      210316 :   GEN p = NULL;
    2391             : 
    2392      210316 :   *data = NULL;
    2393      210316 :   if (lx == 1) return &gauss_get_pivot_NZ;
    2394      210281 :   hx = lgcols(x);
    2395      928861 :   for (j=1; j<lx; j++)
    2396             :   {
    2397      718622 :     GEN xj = gel(x,j);
    2398     3908121 :     for (i=1; i<hx; i++)
    2399             :     {
    2400     3189541 :       GEN c = gel(xj,i);
    2401     3189541 :       switch(typ(c))
    2402             :       {
    2403             :         case t_REAL:
    2404     1748156 :           res = t_REAL;
    2405     1748156 :           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     1279776 :           break;
    2412             :         case t_PADIC:
    2413      161203 :           p = gel(c,2);
    2414      161203 :           res = t_PADIC;
    2415      161203 :           break;
    2416          42 :         default: return &gauss_get_pivot_NZ;
    2417             :       }
    2418             :     }
    2419             :   }
    2420      210239 :   switch(res)
    2421             :   {
    2422      200110 :     case t_REAL: *data = x0; return &gauss_get_pivot_max;
    2423        8260 :     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      201297 : get_col(GEN a, GEN b, GEN p, long li)
    2430             : {
    2431      201297 :   GEN u = cgetg(li+1,t_COL);
    2432             :   long i, j;
    2433             : 
    2434      201297 :   gel(u,li) = gdiv(gel(b,li), p);
    2435      834923 :   for (i=li-1; i>0; i--)
    2436             :   {
    2437      633626 :     pari_sp av = avma;
    2438      633626 :     GEN m = gel(b,i);
    2439      633626 :     for (j=i+1; j<=li; j++) m = gsub(m, gmul(gcoeff(a,i,j), gel(u,j)));
    2440      633626 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(a,i,i)));
    2441             :   }
    2442      201297 :   return u;
    2443             : }
    2444             : 
    2445             : /* bk -= m * bi */
    2446             : static void
    2447     3336118 : _submul(GEN b, long k, long i, GEN m)
    2448             : {
    2449     3336118 :   gel(b,k) = gsub(gel(b,k), gmul(m, gel(b,i)));
    2450     3336118 : }
    2451             : static int
    2452      833461 : init_gauss(GEN a, GEN *b, long *aco, long *li, int *iscol)
    2453             : {
    2454      833461 :   *iscol = *b ? (typ(*b) == t_COL): 0;
    2455      833461 :   *aco = lg(a) - 1;
    2456      833461 :   if (!*aco) /* a empty */
    2457             :   {
    2458          70 :     if (*b && lg(*b) != 1) pari_err_DIM("gauss");
    2459          70 :     *li = 0; return 0;
    2460             :   }
    2461      833391 :   *li = nbrows(a);
    2462      833391 :   if (*li < *aco) pari_err_INV("gauss [no left inverse]", a);
    2463      833391 :   if (*b)
    2464             :   {
    2465      819514 :     switch(typ(*b))
    2466             :     {
    2467             :       case t_MAT:
    2468      117465 :         if (lg(*b) == 1) return 0;
    2469      117465 :         *b = RgM_shallowcopy(*b);
    2470      117465 :         break;
    2471             :       case t_COL:
    2472      702049 :         *b = mkmat( leafcopy(*b) );
    2473      702049 :         break;
    2474           0 :       default: pari_err_TYPE("gauss",*b);
    2475             :     }
    2476      819514 :     if (nbrows(*b) != *li) pari_err_DIM("gauss");
    2477             :   }
    2478             :   else
    2479       13877 :     *b = matid(*li);
    2480      833391 :   return 1;
    2481             : }
    2482             : 
    2483             : static GEN
    2484       32407 : RgM_inv_QM(GEN M)
    2485             : {
    2486       32407 :   pari_sp av = avma;
    2487       32407 :   GEN den, cM, pM = Q_primitive_part(M, &cM);
    2488       32407 :   GEN b = ZM_inv(pM, &den);
    2489       32393 :   if (!b) return gc_NULL(av);
    2490       32386 :   if (cM) den = gmul(den, cM);
    2491       32386 :   if (!gequal1(den)) b = ZM_Q_mul(b, ginv(den));
    2492       32386 :   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       56434 : RgM_inv_fast(GEN x)
    2531             : {
    2532             :   GEN p, pol;
    2533             :   long pa;
    2534       56434 :   long t = RgM_type(x, &p,&pol,&pa);
    2535       56434 :   switch(t)
    2536             :   {
    2537             :     case t_INT:    /* Fall back */
    2538       32407 :     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       23726 :     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      283446 : RgM_solve_basecase(GEN a, GEN b)
    2607             : {
    2608      283446 :   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      283446 :   set_avma(av);
    2615             : 
    2616      283446 :   if (lg(a)-1 == 2 && nbrows(a) == 2) {
    2617             :     /* 2x2 matrix, start by inverting a */
    2618      105559 :     GEN u = gcoeff(a,1,1), v = gcoeff(a,1,2);
    2619      105559 :     GEN w = gcoeff(a,2,1), x = gcoeff(a,2,2);
    2620      105559 :     GEN D = gsub(gmul(u,x), gmul(v,w)), ainv;
    2621      105559 :     if (gequal0(D)) return NULL;
    2622      105559 :     ainv = mkmat2(mkcol2(x, gneg(w)), mkcol2(gneg(v), u));
    2623      105559 :     ainv = gmul(ainv, ginv(D));
    2624      105559 :     if (b) ainv = gmul(ainv, b);
    2625      105559 :     return gerepileupto(av, ainv);
    2626             :   }
    2627             : 
    2628      177887 :   if (!init_gauss(a, &b, &aco, &li, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    2629      177887 :   pivot = get_pivot_fun(a, a, &data);
    2630      177887 :   a = RgM_shallowcopy(a);
    2631      177887 :   bco = lg(b)-1;
    2632      177887 :   if(DEBUGLEVEL>4) err_printf("Entering gauss\n");
    2633             : 
    2634      177887 :   p = NULL; /* gcc -Wall */
    2635      587763 :   for (i=1; i<=aco; i++)
    2636             :   {
    2637             :     /* k is the line where we find the pivot */
    2638      587763 :     k = pivot(a, data, i, NULL);
    2639      587763 :     if (k > li) return NULL;
    2640      587749 :     if (k != i)
    2641             :     { /* exchange the lines s.t. k = i */
    2642      130807 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    2643      130807 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    2644             :     }
    2645      587749 :     p = gcoeff(a,i,i);
    2646      587749 :     if (i == aco) break;
    2647             : 
    2648     1280404 :     for (k=i+1; k<=li; k++)
    2649             :     {
    2650      870528 :       GEN m = gcoeff(a,k,i);
    2651      870528 :       if (!gequal0(m))
    2652             :       {
    2653      691036 :         m = gdiv(m,p);
    2654      691036 :         for (j=i+1; j<=aco; j++) _submul(gel(a,j),k,i,m);
    2655      691036 :         for (j=1;   j<=bco; j++) _submul(gel(b,j),k,i,m);
    2656             :       }
    2657             :     }
    2658      409876 :     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      177873 :   if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
    2666      177873 :   u = cgetg(bco+1,t_MAT);
    2667      177873 :   for (j=1; j<=bco; j++) gel(u,j) = get_col(a,gel(b,j),p,aco);
    2668      177873 :   return gerepilecopy(av, iscol? gel(u,1): u);
    2669             : }
    2670             : 
    2671             : static GEN
    2672      270263 : RgM_RgC_solve_fast(GEN x, GEN y)
    2673             : {
    2674             :   GEN p, pol;
    2675             :   long pa;
    2676      270263 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    2677      270263 :   switch(t)
    2678             :   {
    2679       14518 :     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      255598 :     default:       return gen_0;
    2684             :   }
    2685             : }
    2686             : 
    2687             : static GEN
    2688      112246 : RgM_solve_fast(GEN x, GEN y)
    2689             : {
    2690             :   GEN p, pol;
    2691             :   long pa;
    2692      112246 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    2693      112246 :   switch(t)
    2694             :   {
    2695      107956 :     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        4122 :     default:       return gen_0;
    2700             :   }
    2701             : }
    2702             : 
    2703             : GEN
    2704      382509 : RgM_solve(GEN a, GEN b)
    2705             : {
    2706      382509 :   pari_sp av = avma;
    2707             :   GEN u;
    2708      382509 :   if (!b) return RgM_inv(a);
    2709      382509 :   u = typ(b)==t_MAT ? RgM_solve_fast(a, b): RgM_RgC_solve_fast(a, b);
    2710      382509 :   if (!u) { set_avma(av); return u; }
    2711      382411 :   if (u != gen_0) return u;
    2712      259720 :   return RgM_solve_basecase(a, b);
    2713             : }
    2714             : 
    2715             : GEN
    2716       56434 : RgM_inv(GEN a)
    2717             : {
    2718       56434 :   GEN b = RgM_inv_fast(a);
    2719       56420 :   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      362321 : RgM_inv_upper_ind(GEN A, long index)
    2725             : {
    2726      362321 :   long n = lg(A)-1, i = index, j;
    2727      362321 :   GEN u = zerocol(n);
    2728      362321 :   gel(u,i) = ginv(gcoeff(A,i,i));
    2729     1426737 :   for (i--; i>0; i--)
    2730             :   {
    2731     1064416 :     pari_sp av = avma;
    2732     1064416 :     GEN m = gneg(gmul(gcoeff(A,i,i+1),gel(u,i+1))); /* j = i+1 */
    2733     1064416 :     for (j=i+2; j<=n; j++) m = gsub(m, gmul(gcoeff(A,i,j),gel(u,j)));
    2734     1064416 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(A,i,i)));
    2735             :   }
    2736      362321 :   return u;
    2737             : }
    2738             : GEN
    2739       75103 : RgM_inv_upper(GEN A)
    2740             : {
    2741             :   long i, l;
    2742       75103 :   GEN B = cgetg_copy(A, &l);
    2743       75103 :   for (i = 1; i < l; i++) gel(B,i) = RgM_inv_upper_ind(A, i);
    2744       75103 :   return B;
    2745             : }
    2746             : 
    2747             : static GEN
    2748     1015744 : split_realimag_col(GEN z, long r1, long r2)
    2749             : {
    2750     1015744 :   long i, ru = r1+r2;
    2751     1015744 :   GEN x = cgetg(ru+r2+1,t_COL), y = x + r2;
    2752     3031923 :   for (i=1; i<=r1; i++) {
    2753     2016179 :     GEN a = gel(z,i);
    2754     2016179 :     if (typ(a) == t_COMPLEX) a = gel(a,1); /* paranoia: a should be real */
    2755     2016179 :     gel(x,i) = a;
    2756             :   }
    2757     1729543 :   for (   ; i<=ru; i++) {
    2758      713799 :     GEN b, a = gel(z,i);
    2759      713799 :     if (typ(a) == t_COMPLEX) { b = gel(a,2); a = gel(a,1); } else b = gen_0;
    2760      713799 :     gel(x,i) = a;
    2761      713799 :     gel(y,i) = b;
    2762             :   }
    2763     1015744 :   return x;
    2764             : }
    2765             : GEN
    2766      531601 : split_realimag(GEN x, long r1, long r2)
    2767             : {
    2768             :   long i,l; GEN y;
    2769      531601 :   if (typ(x) == t_COL) return split_realimag_col(x,r1,r2);
    2770      261623 :   y = cgetg_copy(x, &l);
    2771      261623 :   for (i=1; i<l; i++) gel(y,i) = split_realimag_col(gel(x,i), r1, r2);
    2772      261623 :   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      256598 : RgM_solve_realimag(GEN M, GEN y)
    2780             : {
    2781      256598 :   long l = lg(M), r2 = l - lgcols(M), r1 = l-1 - 2*r2;
    2782      256598 :   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      655476 : ZlM_gauss_ratlift(GEN a, GEN b, ulong p, long e, GEN C)
    2800             : {
    2801      655476 :   pari_sp av = avma, av2;
    2802             :   GEN bb, xi, xb, pi, P, B, r;
    2803      655476 :   long i, k = 2;
    2804      655476 :   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      655476 :   pi = P = utoipos(p);
    2809      655476 :   av2 = avma;
    2810      655476 :   xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    2811      655476 :   xb = Flm_to_ZM(xi);
    2812      655476 :   bb = b;
    2813     1287959 :   for (i = 2; i <= e; i++)
    2814             :   {
    2815      693589 :     bb = ZM_Z_divexact(ZM_sub(bb, ZM_nm_mul(a, xi)), P);
    2816      693589 :     if (gc_needed(av,2))
    2817             :     {
    2818          68 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZlM_gauss. i=%ld/%ld",i,e);
    2819          68 :       gerepileall(av2,3, &pi,&bb,&xb);
    2820             :     }
    2821      693589 :     xi = Flm_mul(C, ZM_to_Flm(bb, p), p);
    2822      693589 :     xb = ZM_add(xb, nm_Z_mul(xi, pi));
    2823      693589 :     pi = muliu(pi, p); /* = p^(i-1) */
    2824      693589 :     if (i==k && i < e)
    2825             :     {
    2826      205398 :       k *= 2;
    2827      205398 :       B = sqrti(shifti(pi,-1));
    2828      205398 :       r = FpM_ratlift(xb, pi, B, B, NULL);
    2829      205398 :       if (r)
    2830             :       {
    2831      102970 :         GEN dr, nr = Q_remove_denom(r,&dr);
    2832      102970 :         if (ZM_equal(ZM_mul(a,nr), dr? ZM_Z_mul(b,dr): b))
    2833             :         {
    2834       61106 :           if (DEBUGLEVEL>=4)
    2835           0 :             err_printf("ZlM_gauss: early solution: %ld/%ld\n",i,e);
    2836       61106 :           return gerepilecopy(av, r);
    2837             :         }
    2838             :       }
    2839             :     }
    2840             :   }
    2841      594370 :   B = sqrti(shifti(pi,-1));
    2842      594370 :   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      655574 : ZM_gauss(GEN a, GEN b0)
    2849             : {
    2850      655574 :   pari_sp av = avma, av2;
    2851             :   int iscol;
    2852             :   long n, ncol, i, m, elim;
    2853             :   ulong p;
    2854      655574 :   GEN C, delta, nb, nmin, res, b = b0;
    2855             :   forprime_t S;
    2856             : 
    2857      655574 :   if (!init_gauss(a, &b, &n, &ncol, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    2858      655504 :   nb = gen_0; ncol = lg(b);
    2859     1566344 :   for (i = 1; i < ncol; i++)
    2860             :   {
    2861      910840 :     GEN ni = gnorml2(gel(b, i));
    2862      910840 :     if (cmpii(nb, ni) < 0) nb = ni;
    2863             :   }
    2864      655504 :   if (!signe(nb)) { set_avma(av); return gcopy(b0); }
    2865      655504 :   delta = gen_1; nmin = nb;
    2866     2621468 :   for (i = 1; i <= n; i++)
    2867             :   {
    2868     1965964 :     GEN ni = gnorml2(gel(a, i));
    2869     1965964 :     if (cmpii(ni, nmin) < 0)
    2870             :     {
    2871      102346 :       delta = mulii(delta, nmin); nmin = ni;
    2872             :     }
    2873             :     else
    2874     1863618 :       delta = mulii(delta, ni);
    2875             :   }
    2876      655504 :   if (!signe(nmin)) return NULL;
    2877      655483 :   elim = expi(delta)+1;
    2878      655483 :   av2 = avma;
    2879      655483 :   init_modular_big(&S);
    2880             :   for(;;)
    2881             :   {
    2882      655483 :     p = u_forprime_next(&S);
    2883      655483 :     C = Flm_inv_sp(ZM_to_Flm(a, p), NULL, p);
    2884      655483 :     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     1310952 :   m = (long)ceil((rtodbl(logr_abs(itor(delta,LOWDEFAULTPREC))) + 1)
    2893      655476 :                  / log((double)p));
    2894      655476 :   res = ZlM_gauss_ratlift(a, b, p, m, C);
    2895      655476 :   if (iscol) return gerepilecopy(av, gel(res, 1));
    2896      115821 :   return gerepileupto(av, res);
    2897             : }
    2898             : 
    2899             : /* same as above, M rational */
    2900             : GEN
    2901        1449 : QM_gauss(GEN M, GEN B)
    2902             : {
    2903        1449 :   pari_sp av = avma;
    2904             :   GEN K, MB;
    2905        1449 :   MB = Q_primitive_part(mkvec2(M,B), NULL);
    2906        1449 :   K = ZM_gauss(gel(MB,1), gel(MB,2));
    2907        1449 :   return gerepileupto(av, K);
    2908             : }
    2909             : 
    2910             : static GEN
    2911      135375 : ZM_inv_slice(GEN A, GEN P, GEN *mod)
    2912             : {
    2913      135375 :   pari_sp av = avma;
    2914      135375 :   long i, n = lg(P)-1;
    2915             :   GEN H, T;
    2916      135375 :   if (n == 1)
    2917             :   {
    2918      134252 :     ulong p = uel(P,1);
    2919      134252 :     GEN Hp, a = ZM_to_Flm(A, p);
    2920      134252 :     Hp = Flm_adjoint(a, p);
    2921      134251 :     Hp = gerepileupto(av, Flm_to_ZM(Hp));
    2922      134242 :     *mod = utoi(p); return Hp;
    2923             :   }
    2924        1123 :   T = ZV_producttree(P);
    2925        1123 :   A = ZM_nv_mod_tree(A, P, T);
    2926        1123 :   H = cgetg(n+1, t_VEC);
    2927        4025 :   for(i=1; i <= n; i++)
    2928        2902 :     gel(H,i) = Flm_adjoint(gel(A, i), uel(P,i));
    2929        1123 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    2930        1123 :   *mod = gmael(T, lg(T)-1, 1);
    2931        1123 :   gerepileall(av, 2, &H, mod);
    2932        1123 :   return H;
    2933             : }
    2934             : 
    2935             : static GEN
    2936      114466 : RgM_true_Hadamard(GEN a)
    2937             : {
    2938      114466 :   pari_sp av = avma;
    2939      114466 :   long n = lg(a)-1, i;
    2940             :   GEN B;
    2941      114466 :   if (n == 0) return gen_1;
    2942      114466 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    2943      114466 :   B = gnorml2(gel(a,1));
    2944      114466 :   for (i = 2; i <= n; i++) B = gmul(B, gnorml2(gel(a,i)));
    2945      114466 :   return gerepileuptoint(av, ceil_safe(sqrtr(B)));
    2946             : }
    2947             : 
    2948             : GEN
    2949      135375 : ZM_inv_worker(GEN P, GEN A)
    2950             : {
    2951      135375 :   GEN V = cgetg(3, t_VEC);
    2952      135375 :   gel(V,1) = ZM_inv_slice(A, P, &gel(V,2));
    2953      135364 :   return V;
    2954             : }
    2955             : 
    2956             : static GEN
    2957        5117 : ZM_inv0(GEN A, GEN *pden)
    2958             : {
    2959        5117 :   if (pden) *pden = gen_1;
    2960        5117 :   (void)A; return cgetg(1, t_MAT);
    2961             : }
    2962             : static GEN
    2963       23279 : ZM_inv1(GEN A, GEN *pden)
    2964             : {
    2965       23279 :   GEN a = gcoeff(A,1,1);
    2966       23279 :   long s = signe(a);
    2967       23279 :   if (!s) return NULL;
    2968       23279 :   if (pden) *pden = absi(a);
    2969       23279 :   retmkmat(mkcol(s == 1? gen_1: gen_m1));
    2970             : }
    2971             : static GEN
    2972       46172 : ZM_inv2(GEN A, GEN *pden)
    2973             : {
    2974             :   GEN a, b, c, d, D, cA;
    2975             :   long s;
    2976       46172 :   A = Q_primitive_part(A, &cA);
    2977       46172 :   a = gcoeff(A,1,1); b = gcoeff(A,1,2);
    2978       46172 :   c = gcoeff(A,2,1); d = gcoeff(A,2,2);
    2979       46172 :   D = subii(mulii(a,d), mulii(b,c)); /* left on stack */
    2980       46172 :   s = signe(D);
    2981       46172 :   if (!s) return NULL;
    2982       46172 :   if (s < 0) D = negi(D);
    2983       46172 :   if (pden) *pden = mul_denom(D, cA);
    2984       46172 :   if (s > 0)
    2985       24405 :     retmkmat2(mkcol2(icopy(d), negi(c)), mkcol2(negi(b), icopy(a)));
    2986             :   else
    2987       21767 :     retmkmat2(mkcol2(negi(d), icopy(c)), mkcol2(icopy(b), negi(a)));
    2988             : }
    2989             : 
    2990             : /* to be used when denom(M^(-1)) << det(M) and a sharp multiple is
    2991             :  * not available. Return H primitive such that M*H = den*Id */
    2992             : GEN
    2993           0 : ZM_inv_ratlift(GEN M, GEN *pden)
    2994             : {
    2995           0 :   pari_sp av2, av = avma;
    2996             :   GEN Hp, q, H;
    2997             :   ulong p;
    2998           0 :   long m = lg(M)-1;
    2999             :   forprime_t S;
    3000             :   pari_timer ti;
    3001             : 
    3002           0 :   if (m == 0) return ZM_inv0(M,pden);
    3003           0 :   if (m == 1 && nbrows(M)==1) return ZM_inv1(M,pden);
    3004           0 :   if (m == 2 && nbrows(M)==2) return ZM_inv2(M,pden);
    3005             : 
    3006           0 :   if (DEBUGLEVEL>5) timer_start(&ti);
    3007           0 :   init_modular_big(&S);
    3008           0 :   av2 = avma;
    3009           0 :   H = NULL;
    3010           0 :   while ((p = u_forprime_next(&S)))
    3011             :   {
    3012             :     GEN Mp, B, Hr;
    3013           0 :     Mp = ZM_to_Flm(M,p);
    3014           0 :     Hp = Flm_inv_sp(Mp, NULL, p);
    3015           0 :     if (!Hp) continue;
    3016           0 :     if (!H)
    3017             :     {
    3018           0 :       H = ZM_init_CRT(Hp, p);
    3019           0 :       q = utoipos(p);
    3020             :     }
    3021             :     else
    3022           0 :       ZM_incremental_CRT(&H, Hp, &q, p);
    3023           0 :     B = sqrti(shifti(q,-1));
    3024           0 :     Hr = FpM_ratlift(H,q,B,B,NULL);
    3025           0 :     if (DEBUGLEVEL>5)
    3026           0 :       timer_printf(&ti,"ZM_inv mod %lu (ratlift=%ld)", p,!!Hr);
    3027           0 :     if (Hr) {/* DONE ? */
    3028           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    3029           0 :       if (ZM_isscalar(ZM_mul(Hl, M), *pden)) { H = Hl; break; }
    3030             :     }
    3031             : 
    3032           0 :     if (gc_needed(av,2))
    3033             :     {
    3034           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_inv_ratlift");
    3035           0 :       gerepileall(av2, 2, &H, &q);
    3036             :     }
    3037             :   }
    3038           0 :   if (!*pden) *pden = gen_1;
    3039           0 :   gerepileall(av, 2, &H, pden);
    3040           0 :   return H;
    3041             : }
    3042             : 
    3043             : static GEN
    3044      122198 : ZM_adj_ratlift(GEN A, GEN H, GEN mod)
    3045             : {
    3046             :   GEN B;
    3047      122198 :   GEN D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3048      122198 :   GEN g = gcdii(D, mod);
    3049      122198 :   if (!equali1(g))
    3050             :   {
    3051          14 :     mod = diviiexact(mod, g);
    3052          14 :     H = FpM_red(H, mod);
    3053             :   }
    3054      122198 :   D = Fp_inv(Fp_red(D, mod), mod);
    3055      122198 :   H = FpM_Fp_mul(H, D, mod);
    3056      122198 :   B = sqrti(shifti(mod,-1));
    3057      122198 :   return FpM_ratlift(H, mod, B, B, NULL);
    3058             : }
    3059             : 
    3060             : GEN
    3061      189041 : ZM_inv(GEN A, GEN *pden)
    3062             : {
    3063      189041 :   pari_sp av = avma;
    3064      189041 :   long m = lg(A)-1, n, k1 = 1, k2;
    3065      189041 :   GEN H = NULL, D, H1 = NULL, mod1 = NULL, worker;
    3066      189041 :   ulong bnd, mask, p = 0;
    3067             :   pari_timer ti;
    3068             : 
    3069      189041 :   if (m == 0) return ZM_inv0(A,pden);
    3070      183924 :   if (pden) *pden = gen_1;
    3071      183924 :   if (nbrows(A) < m) return NULL;
    3072      183917 :   if (m == 1 && nbrows(A)==1) return ZM_inv1(A,pden);
    3073      160638 :   if (m == 2 && nbrows(A)==2) return ZM_inv2(A,pden);
    3074             : 
    3075      114466 :   if (DEBUGLEVEL>=5) timer_start(&ti);
    3076      114466 :   bnd = expi(RgM_true_Hadamard(A));
    3077      114466 :   worker = strtoclosure("_ZM_inv_worker", 1, A);
    3078      114466 :   gen_inccrt("ZM_inv_r", worker, NULL, k1, m, &p, &H1, &mod1, nmV_chinese_center, FpM_center);
    3079      114466 :   n = (bnd+1)/expu(p)+1;
    3080      114466 :   if (DEBUGLEVEL>=5) timer_printf(&ti,"inv (%ld/%ld primes)", k1, n);
    3081      114466 :   mask = quadratic_prec_mask(n);
    3082      114466 :   for (k2 = 0;;)
    3083       16961 :   {
    3084             :     GEN Hr;
    3085      131427 :     if (k2 > 0)
    3086             :     {
    3087       14430 :       gen_inccrt("ZM_inv_r", worker, NULL, k2, m, &p, &H1, &mod1,nmV_chinese_center,FpM_center);
    3088       14430 :       k1 += k2;
    3089       14430 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"CRT (%ld/%ld primes)", k1, n);
    3090             :     }
    3091      131427 :     if (mask == 1) break;
    3092      122198 :     k2 = (mask&1UL) ? k1-1: k1;
    3093      122198 :     mask >>= 1;
    3094             : 
    3095      122198 :     Hr = ZM_adj_ratlift(A, H1, mod1);
    3096      122198 :     if (DEBUGLEVEL>=5) timer_printf(&ti,"ratlift (%ld/%ld primes)", k1, n);
    3097      122198 :     if (Hr) {/* DONE ? */
    3098             :       GEN den;
    3099      106966 :       GEN Hl = Q_remove_denom(Hr, &den);
    3100      106966 :       GEN R = ZM_mul(Hl, A);
    3101      106966 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"mult (%ld/%ld primes)", k1, n);
    3102      106966 :       den = den ? den: gen_1;
    3103      106966 :       if (den)
    3104             :       {
    3105      106966 :         if (ZM_isscalar(R, den))
    3106             :         {
    3107      105237 :           H = Hl;
    3108      105237 :           if (pden) *pden = den;
    3109      210474 :           break;
    3110             :         }
    3111             :       }
    3112             :       else
    3113           0 :         if (ZM_isidentity(R)) { H=Hl; break; }
    3114             :     }
    3115             :   }
    3116      114466 :   if (!H)
    3117             :   {
    3118             :     GEN d;
    3119        9229 :     H = H1;
    3120        9229 :     D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3121        9229 :     if (signe(D)==0) pari_err_INV("ZM_inv", A);
    3122        9215 :     d = gcdii(Q_content_safe(H), D);
    3123        9215 :     if (signe(D) < 0) d = negi(d);
    3124        9215 :     if (!equali1(d))
    3125             :     {
    3126        5318 :       H = ZM_Z_divexact(H, d);
    3127        5318 :       D = diviiexact(D, d);
    3128             :     }
    3129        9215 :     if (pden) *pden = D;
    3130             :   }
    3131      114452 :   gerepileall(av, pden? 2: 1, &H, pden);
    3132      114452 :   return H;
    3133             : }
    3134             : 
    3135             : /* same as above, M rational */
    3136             : GEN
    3137        1694 : QM_inv(GEN M)
    3138             : {
    3139        1694 :   pari_sp av = avma;
    3140             :   GEN den, cM, K;
    3141        1694 :   M = Q_primitive_part(M, &cM);
    3142        1694 :   K = ZM_inv(M, &den);
    3143        1694 :   if (!K) return gc_NULL(av);
    3144        1694 :   cM = inv_content(mul_content(cM, den));
    3145        1694 :   if (cM) K = RgM_Rg_div(K, cM);
    3146        1694 :   return gerepileupto(av, K);
    3147             : }
    3148             : 
    3149             : static GEN
    3150       55673 : ZM_ker_i(GEN M, long fl)
    3151             : {
    3152       55673 :   pari_sp av2, av = avma;
    3153             :   GEN q, H, D;
    3154             :   forprime_t S;
    3155       55673 :   av2 = avma;
    3156       55673 :   H = NULL; D = NULL;
    3157       55673 :   if (lg(M)==1) return cgetg(1, t_MAT);
    3158       55666 :   init_modular_big(&S);
    3159             :   for(;;)
    3160       62074 :   {
    3161             :     GEN Kp, Hp, Dp, Mp, Hr, B;
    3162      117740 :     ulong p = u_forprime_next(&S);
    3163      117740 :     Mp = ZM_to_Flm(M, p);
    3164      117740 :     Kp = Flm_ker_sp(Mp, p, 2);
    3165      117740 :     Hp = gel(Kp,1); Dp = gel(Kp,2);
    3166      117740 :     if (H && (lg(Hp)>lg(H) || (lg(Hp)==lg(H) && vecsmall_lexcmp(Dp,D)>0))) continue;
    3167      109832 :     if (!H || (lg(Hp)<lg(H) || vecsmall_lexcmp(Dp,D)<0))
    3168             :     {
    3169       93346 :       H = ZM_init_CRT(Hp, p); D = Dp;
    3170       93346 :       q = utoipos(p);
    3171             :     }
    3172             :     else
    3173       16486 :       ZM_incremental_CRT(&H, Hp, &q, p);
    3174      109832 :     B = sqrti(shifti(q,-1));
    3175      109832 :     Hr = FpM_ratlift(H, q, B, B, NULL);
    3176      109832 :     if (DEBUGLEVEL>5) err_printf("ZM_ker mod %lu (ratlift=%ld)\n", p,!!Hr);
    3177      109832 :     if (Hr) {/* DONE ? */
    3178      104154 :       GEN MH = QM_mul(M, Hr);
    3179      104154 :       if (gequal0(MH)) { H = fl ? vec_Q_primpart(Hr): Hr;  break; }
    3180             :     }
    3181       54166 :     if (gc_needed(av,2))
    3182             :     {
    3183           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_ker");
    3184           0 :       gerepileall(av2, 3, &H, &D, &q);
    3185             :     }
    3186             :   }
    3187       55666 :   return gerepilecopy(av, H);
    3188             : }
    3189             : 
    3190             : GEN
    3191       48861 : ZM_ker(GEN M)
    3192       48861 : { return ZM_ker_i(M, 1); }
    3193             : 
    3194             : GEN
    3195        7645 : QM_ker(GEN M)
    3196             : {
    3197        7645 :   pari_sp av = avma;
    3198        7645 :   long l = lg(M)-1;
    3199        7645 :   if (l==0) return cgetg(1, t_MAT);
    3200        7610 :   if (lgcols(M)==1) return matid(l);
    3201        6749 :   M = shallowtrans(vec_Q_primpart(shallowtrans(M)));
    3202        6749 :   return gerepileupto(av, ZM_ker_i(M, 0));
    3203             : }
    3204             : 
    3205             : /* x a ZM. Return a multiple of the determinant of the lattice generated by
    3206             :  * the columns of x. From Algorithm 2.2.6 in GTM138 */
    3207             : GEN
    3208       47549 : detint(GEN A)
    3209             : {
    3210       47549 :   if (typ(A) != t_MAT) pari_err_TYPE("detint",A);
    3211       47549 :   RgM_check_ZM(A, "detint");
    3212       47549 :   return ZM_detmult(A);
    3213             : }
    3214             : GEN
    3215       97613 : ZM_detmult(GEN A)
    3216             : {
    3217       97613 :   pari_sp av1, av = avma;
    3218             :   GEN B, c, v, piv;
    3219       97613 :   long rg, i, j, k, m, n = lg(A) - 1;
    3220             : 
    3221       97613 :   if (!n) return gen_1;
    3222       97613 :   m = nbrows(A);
    3223       97613 :   if (n < m) return gen_0;
    3224       97592 :   c = zero_zv(m);
    3225       97592 :   av1 = avma;
    3226       97592 :   B = zeromatcopy(m,m);
    3227       97592 :   v = cgetg(m+1, t_COL);
    3228       97592 :   piv = gen_1; rg = 0;
    3229      520889 :   for (k=1; k<=n; k++)
    3230             :   {
    3231      520875 :     GEN pivprec = piv;
    3232      520875 :     long t = 0;
    3233     4167215 :     for (i=1; i<=m; i++)
    3234             :     {
    3235     3646340 :       pari_sp av2 = avma;
    3236             :       GEN vi;
    3237     3646340 :       if (c[i]) continue;
    3238             : 
    3239     2083856 :       vi = mulii(piv, gcoeff(A,i,k));
    3240    18067010 :       for (j=1; j<=m; j++)
    3241    15983154 :         if (c[j]) vi = addii(vi, mulii(gcoeff(B,j,i),gcoeff(A,j,k)));
    3242     2083856 :       if (!t && signe(vi)) t = i;
    3243     2083856 :       gel(v,i) = gerepileuptoint(av2, vi);
    3244             :     }
    3245      520875 :     if (!t) continue;
    3246             :     /* at this point c[t] = 0 */
    3247             : 
    3248      520791 :     if (++rg >= m) { /* full rank; mostly done */
    3249       97578 :       GEN det = gel(v,t); /* last on stack */
    3250       97578 :       if (++k > n)
    3251       97495 :         det = absi(det);
    3252             :       else
    3253             :       {
    3254             :         /* improve further; at this point c[i] is set for all i != t */
    3255          83 :         gcoeff(B,t,t) = piv; v = centermod(gel(B,t), det);
    3256         334 :         for ( ; k<=n; k++)
    3257         251 :           det = gcdii(det, ZV_dotproduct(v, gel(A,k)));
    3258             :       }
    3259       97578 :       return gerepileuptoint(av, det);
    3260             :     }
    3261             : 
    3262      423213 :     piv = gel(v,t);
    3263     3548279 :     for (i=1; i<=m; i++)
    3264             :     {
    3265             :       GEN mvi;
    3266     3125066 :       if (c[i] || i == t) continue;
    3267             : 
    3268     1562533 :       gcoeff(B,t,i) = mvi = negi(gel(v,i));
    3269    13895805 :       for (j=1; j<=m; j++)
    3270    12333272 :         if (c[j]) /* implies j != t */
    3271             :         {
    3272     3069402 :           pari_sp av2 = avma;
    3273     3069402 :           GEN z = addii(mulii(gcoeff(B,j,i), piv), mulii(gcoeff(B,j,t), mvi));
    3274     3069402 :           if (rg > 1) z = diviiexact(z, pivprec);
    3275     3069402 :           gcoeff(B,j,i) = gerepileuptoint(av2, z);
    3276             :         }
    3277             :     }
    3278      423213 :     c[t] = k;
    3279      423213 :     if (gc_needed(av,1))
    3280             :     {
    3281           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"detint. k=%ld",k);
    3282           0 :       gerepileall(av1, 2, &piv,&B); v = zerovec(m);
    3283             :     }
    3284             :   }
    3285          14 :   set_avma(av); return gen_0;
    3286             : }
    3287             : 
    3288             : /* Reduce x modulo (invertible) y */
    3289             : GEN
    3290       14413 : closemodinvertible(GEN x, GEN y)
    3291             : {
    3292       14413 :   return gmul(y, ground(RgM_solve(y,x)));
    3293             : }
    3294             : GEN
    3295           7 : reducemodinvertible(GEN x, GEN y)
    3296             : {
    3297           7 :   return gsub(x, closemodinvertible(x,y));
    3298             : }
    3299             : GEN
    3300           0 : reducemodlll(GEN x,GEN y)
    3301             : {
    3302           0 :   return reducemodinvertible(x, ZM_lll(y, 0.75, LLL_INPLACE));
    3303             : }
    3304             : 
    3305             : /*******************************************************************/
    3306             : /*                                                                 */
    3307             : /*                    KERNEL of an m x n matrix                    */
    3308             : /*          return n - rk(x) linearly independent vectors          */
    3309             : /*                                                                 */
    3310             : /*******************************************************************/
    3311             : static GEN
    3312          28 : RgM_deplin_i(GEN x0)
    3313             : {
    3314          28 :   pari_sp av = avma, av2;
    3315          28 :   long i, j, k, nl, nc = lg(x0)-1;
    3316             :   GEN D, x, y, c, l, d, ck;
    3317             : 
    3318          28 :   if (!nc) return NULL;
    3319          28 :   nl = nbrows(x0);
    3320          28 :   c = zero_zv(nl);
    3321          28 :   l = cgetg(nc+1, t_VECSMALL); /* not initialized */
    3322          28 :   av2 = avma;
    3323          28 :   x = RgM_shallowcopy(x0);
    3324          28 :   d = const_vec(nl, gen_1); /* pivot list */
    3325          28 :   ck = NULL; /* gcc -Wall */
    3326          98 :   for (k=1; k<=nc; k++)
    3327             :   {
    3328          91 :     ck = gel(x,k);
    3329         196 :     for (j=1; j<k; j++)
    3330             :     {
    3331         105 :       GEN cj = gel(x,j), piv = gel(d,j), q = gel(ck,l[j]);
    3332         420 :       for (i=1; i<=nl; i++)
    3333         315 :         if (i!=l[j]) gel(ck,i) = gsub(gmul(piv, gel(ck,i)), gmul(q, gel(cj,i)));
    3334             :     }
    3335             : 
    3336          91 :     i = gauss_get_pivot_NZ(x, NULL, k, c);
    3337          91 :     if (i > nl) break;
    3338          70 :     if (gc_needed(av,1))
    3339             :     {
    3340           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"deplin k = %ld/%ld",k,nc);
    3341           0 :       gerepileall(av2, 2, &x, &d);
    3342           0 :       ck = gel(x,k);
    3343             :     }
    3344          70 :     gel(d,k) = gel(ck,i);
    3345          70 :     c[i] = k; l[k] = i; /* pivot d[k] in x[i,k] */
    3346             :   }
    3347          28 :   if (k > nc) return gc_NULL(av);
    3348          21 :   if (k == 1) { set_avma(av); return scalarcol_shallow(gen_1,nc); }
    3349          21 :   y = cgetg(nc+1,t_COL);
    3350          21 :   gel(y,1) = gcopy(gel(ck, l[1]));
    3351          49 :   for (D=gel(d,1),j=2; j<k; j++)
    3352             :   {
    3353          28 :     gel(y,j) = gmul(gel(ck, l[j]), D);
    3354          28 :     D = gmul(D, gel(d,j));
    3355             :   }
    3356          21 :   gel(y,j) = gneg(D);
    3357          21 :   for (j++; j<=nc; j++) gel(y,j) = gen_0;
    3358          21 :   y = primitive_part(y, &c);
    3359          21 :   return c? gerepileupto(av, y): gerepilecopy(av, y);
    3360             : }
    3361             : static GEN
    3362           0 : RgV_deplin(GEN v)
    3363             : {
    3364           0 :   pari_sp av = avma;
    3365           0 :   long n = lg(v)-1;
    3366           0 :   GEN y, p = NULL;
    3367           0 :   if (n <= 1)
    3368             :   {
    3369           0 :     if (n == 1 && gequal0(gel(v,1))) return mkcol(gen_1);
    3370           0 :     return cgetg(1, t_COL);
    3371             :   }
    3372           0 :   if (gequal0(gel(v,1))) return scalarcol_shallow(gen_1, n);
    3373           0 :   v = primpart(mkvec2(gel(v,1),gel(v,2)));
    3374           0 :   if (RgV_is_FpV(v, &p) && p) v = centerlift(v);
    3375           0 :   y = zerocol(n);
    3376           0 :   gel(y,1) = gneg(gel(v,2));
    3377           0 :   gel(y,2) = gcopy(gel(v,1));
    3378           0 :   return gerepileupto(av, y);
    3379             : 
    3380             : }
    3381             : 
    3382             : static GEN
    3383         105 : RgM_deplin_FpM(GEN x, GEN p)
    3384             : {
    3385         105 :   pari_sp av = avma;
    3386             :   ulong pp;
    3387         105 :   x = RgM_Fp_init(x, p, &pp);
    3388         105 :   switch(pp)
    3389             :   {
    3390             :   case 0:
    3391          35 :     x = FpM_ker_gen(x,p,1);
    3392          35 :     if (!x) return gc_NULL(av);
    3393          21 :     x = FpC_center(x,p,shifti(p,-1));
    3394          21 :     break;
    3395             :   case 2:
    3396          14 :     x = F2m_ker_sp(x,1);
    3397          14 :     if (!x) return gc_NULL(av);
    3398           7 :     x = F2c_to_ZC(x); break;
    3399             :   default:
    3400          56 :     x = Flm_ker_sp(x,pp,1);
    3401          56 :     if (!x) return gc_NULL(av);
    3402          35 :     x = Flv_center(x, pp, pp>>1);
    3403          35 :     x = zc_to_ZC(x);
    3404          35 :     break;
    3405             :   }
    3406          63 :   return gerepileupto(av, x);
    3407             : }
    3408             : 
    3409             : /* FIXME: implement direct modular ZM_deplin ? */
    3410             : static GEN
    3411          98 : QM_deplin(GEN M)
    3412             : {
    3413          98 :   pari_sp av = avma;
    3414          98 :   long l = lg(M)-1;
    3415             :   GEN k;
    3416          98 :   if (l==0) return NULL;
    3417          63 :   if (lgcols(M)==1) return col_ei(l, 1);
    3418          63 :   M = shallowtrans(vec_Q_primpart(shallowtrans(M)));
    3419          63 :   k = ZM_ker_i(M, 1);
    3420          63 :   if (lg(k)== 1) return gc_NULL(av);
    3421          49 :   return gerepilecopy(av, gel(k,1));
    3422             : }
    3423             : 
    3424             : static GEN
    3425          42 : RgM_deplin_FqM(GEN x, GEN pol, GEN p)
    3426             : {
    3427          42 :   pari_sp av = avma;
    3428          42 :   GEN b, T = RgX_to_FpX(pol, p);
    3429          42 :   if (signe(T) == 0) pari_err_OP("deplin",x,pol);
    3430          42 :   b = FqM_deplin(RgM_to_FqM(x, T, p), T, p);
    3431          42 :   return gerepileupto(av, b);
    3432             : }
    3433             : 
    3434             : #define code(t1,t2) ((t1 << 6) | t2)
    3435             : static GEN
    3436         357 : RgM_deplin_fast(GEN x)
    3437             : {
    3438             :   GEN p, pol;
    3439             :   long pa;
    3440         357 :   long t = RgM_type(x, &p,&pol,&pa);
    3441         357 :   switch(t)
    3442             :   {
    3443             :     case t_INT:    /* fall through */
    3444          98 :     case t_FRAC:   return QM_deplin(x);
    3445          84 :     case t_FFELT:  return FFM_deplin(x, pol);
    3446         105 :     case t_INTMOD: return RgM_deplin_FpM(x, p);
    3447             :     case code(t_POLMOD, t_INTMOD):
    3448          42 :                    return RgM_deplin_FqM(x, pol, p);
    3449          28 :     default:       return gen_0;
    3450             :   }
    3451             : }
    3452             : #undef code
    3453             : 
    3454             : static GEN
    3455         357 : RgM_deplin(GEN x)
    3456             : {
    3457         357 :   GEN z = RgM_deplin_fast(x);
    3458         357 :   if (z!= gen_0) return z;
    3459          28 :   return RgM_deplin_i(x);
    3460             : }
    3461             : 
    3462             : GEN
    3463         357 : deplin(GEN x)
    3464             : {
    3465         357 :   switch(typ(x))
    3466             :   {
    3467             :     case t_MAT:
    3468             :     {
    3469         357 :       GEN z = RgM_deplin(x);
    3470         357 :       if (z) return z;
    3471         140 :       return cgetg(1, t_COL);
    3472             :     }
    3473           0 :     case t_VEC: return RgV_deplin(x);
    3474           0 :     default: pari_err_TYPE("deplin",x);
    3475             :   }
    3476             :   return NULL;/*LCOV_EXCL_LINE*/
    3477             : }
    3478             : 
    3479             : /*******************************************************************/
    3480             : /*                                                                 */
    3481             : /*         GAUSS REDUCTION OF MATRICES  (m lines x n cols)         */
    3482             : /*           (kernel, image, complementary image, rank)            */
    3483             : /*                                                                 */
    3484             : /*******************************************************************/
    3485             : /* return the transform of x under a standard Gauss pivot.
    3486             :  * x0 is a reference point when guessing whether x[i,j] ~ 0
    3487             :  * (iff x[i,j] << x0[i,j])
    3488             :  * Set r = dim ker(x). d[k] contains the index of the first non-zero pivot
    3489             :  * in column k */
    3490             : static GEN
    3491         952 : gauss_pivot_ker(GEN x, GEN x0, GEN *dd, long *rr)
    3492             : {
    3493             :   GEN c, d, p, data;
    3494             :   pari_sp av;
    3495             :   long i, j, k, r, t, n, m;
    3496             :   pivot_fun pivot;
    3497             : 
    3498         952 :   n=lg(x)-1; if (!n) { *dd=NULL; *rr=0; return cgetg(1,t_MAT); }
    3499         952 :   m=nbrows(x); r=0;
    3500         952 :   pivot = get_pivot_fun(x, x0, &data);
    3501         952 :   x = RgM_shallowcopy(x);
    3502         952 :   c = zero_zv(m);
    3503         952 :   d = cgetg(n+1,t_VECSMALL);
    3504         952 :   av=avma;
    3505        5558 :   for (k=1; k<=n; k++)
    3506             :   {
    3507        4606 :     j = pivot(x, data, k, c);
    3508        4606 :     if (j > m)
    3509             :     {
    3510        1029 :       r++; d[k]=0;
    3511        4578 :       for(j=1; j<k; j++)
    3512        3549 :         if (d[j]) gcoeff(x,d[j],k) = gclone(gcoeff(x,d[j],k));
    3513             :     }
    3514             :     else
    3515             :     { /* pivot for column k on row j */
    3516        3577 :       c[j]=k; d[k]=j; p = gdiv(gen_m1,gcoeff(x,j,k));
    3517        3577 :       gcoeff(x,j,k) = gen_m1;
    3518             :       /* x[j,] /= - x[j,k] */
    3519        3577 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    3520       35000 :       for (t=1; t<=m; t++)
    3521       31423 :         if (t!=j)
    3522             :         { /* x[t,] -= 1 / x[j,k] x[j,] */
    3523       27846 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    3524       27846 :           if (gequal0(p)) continue;
    3525       74613 :           for (i=k+1; i<=n; i++)
    3526       60648 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i),gmul(p,gcoeff(x,j,i)));
    3527       13965 :           if (gc_needed(av,1)) gerepile_gauss_ker(x,k,t,av);
    3528             :         }
    3529             :     }
    3530             :   }
    3531         952 :   *dd=d; *rr=r; return x;
    3532             : }
    3533             : 
    3534             : /* r = dim ker(x).
    3535             :  * Returns d:
    3536             :  *   d[k] != 0 contains the index of a non-zero pivot in column k
    3537             :  *   d[k] == 0 if column k is a linear combination of the (k-1) first ones */
    3538             : GEN
    3539       34451 : RgM_pivots(GEN x0, GEN data, long *rr, pivot_fun pivot)
    3540             : {
    3541             :   GEN x, c, d, p;
    3542       34451 :   long i, j, k, r, t, m, n = lg(x0)-1;
    3543             :   pari_sp av;
    3544             : 
    3545       34451 :   if (RgM_is_ZM(x0)) return ZM_pivots(x0, rr);
    3546       34353 :   if (!n) { *rr = 0; return NULL; }
    3547             : 
    3548       34353 :   d = cgetg(n+1, t_VECSMALL);
    3549       34353 :   x = RgM_shallowcopy(x0);
    3550       34353 :   m = nbrows(x); r = 0;
    3551       34353 :   c = zero_zv(m);
    3552       34353 :   av = avma;
    3553      959456 :   for (k=1; k<=n; k++)
    3554             :   {
    3555      925103 :     j = pivot(x, data, k, c);
    3556      925103 :     if (j > m) { r++; d[k] = 0; }
    3557             :     else
    3558             :     {
    3559       54841 :       c[j] = k; d[k] = j; p = gdiv(gen_m1, gcoeff(x,j,k));
    3560       54841 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    3561             : 
    3562      213538 :       for (t=1; t<=m; t++)
    3563      158697 :         if (!c[t]) /* no pivot on that line yet */
    3564             :         {
    3565       61452 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    3566     5497505 :           for (i=k+1; i<=n; i++)
    3567     5436053 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i), gmul(p, gcoeff(x,j,i)));
    3568       61452 :           if (gc_needed(av,1)) gerepile_gauss(x,k,t,av,j,c);
    3569             :         }
    3570       54841 :       for (i=k; i<=n; i++) gcoeff(x,j,i) = gen_0; /* dummy */
    3571             :     }
    3572             :   }
    3573       34353 :   *rr = r; set_avma((pari_sp)d); return d;
    3574             : }
    3575             : 
    3576             : static long
    3577      120895 : ZM_count_0_cols(GEN M)
    3578             : {
    3579      120895 :   long i, l = lg(M), n = 0;
    3580      677228 :   for (i = 1; i < l; i++)
    3581      556333 :     if (ZV_equal0(gel(M,i))) n++;
    3582      120895 :   return n;
    3583             : }
    3584             : 
    3585             : static void indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol);
    3586             : /* As RgM_pivots, integer entries. Set *rr = dim Ker M0 */
    3587             : GEN
    3588      125102 : ZM_pivots(GEN M0, long *rr)
    3589             : {
    3590      125102 :   GEN d, dbest = NULL;
    3591             :   long m, n, i, imax, rmin, rbest, zc;
    3592      125102 :   int beenthere = 0;
    3593      125102 :   pari_sp av, av0 = avma;
    3594             :   forprime_t S;
    3595             : 
    3596      125102 :   rbest = n = lg(M0)-1;
    3597      125102 :   if (n == 0) { *rr = 0; return NULL; }
    3598      120895 :   zc = ZM_count_0_cols(M0);
    3599      120895 :   if (n == zc) { *rr = zc; return zero_zv(n); }
    3600             : 
    3601      120748 :   m = nbrows(M0);
    3602      120748 :   rmin = maxss(zc, n-m);
    3603      120748 :   init_modular_small(&S);
    3604      120748 :   imax = (n < (1<<4))? 1: (n>>3); /* heuristic */
    3605             : 
    3606             :   for(;;)
    3607           0 :   {
    3608             :     GEN row, col, M, KM, IM, RHS, X, cX;
    3609             :     long rk;
    3610      130303 :     for (av = avma, i = 0;; set_avma(av), i++)
    3611        9555 :     {
    3612      130303 :       ulong p = u_forprime_next(&S);
    3613             :       long rp;
    3614      130303 :       if (!p) pari_err_OVERFLOW("ZM_pivots [ran out of primes]");
    3615      130303 :       d = Flm_pivots(ZM_to_Flm(M0, p), p, &rp, 1);
    3616      130303 :       if (rp == rmin) { rbest = rp; goto END; } /* maximal rank, return */
    3617       17448 :       if (rp < rbest) { /* save best r so far */
    3618        7895 :         rbest = rp;
    3619        7895 :         guncloneNULL(dbest);
    3620        7895 :         dbest = gclone(d);
    3621       15788 :         if (beenthere) break;
    3622             :       }
    3623       17448 :       if (!beenthere && i >= imax) break;
    3624             :     }
    3625        7893 :     beenthere = 1;
    3626             :     /* Dubious case: there is (probably) a non trivial kernel */
    3627        7893 :     indexrank_all(m,n, rbest, dbest, &row, &col);
    3628        7893 :     M = rowpermute(vecpermute(M0, col), row);
    3629        7893 :     rk = n - rbest; /* (probable) dimension of image */
    3630        7893 :     IM = vecslice(M,1,rk);
    3631        7893 :     KM = vecslice(M,rk+1, n);
    3632        7893 :     M = rowslice(IM, 1,rk); /* square maximal rank */
    3633        7893 :     X = ZM_gauss(M, rowslice(KM, 1,rk));
    3634        7893 :     X = Q_remove_denom(X, &cX);
    3635        7893 :     RHS = rowslice(KM,rk+1,m);
    3636        7893 :     if (cX) RHS = ZM_Z_mul(RHS, cX);
    3637        7893 :     if (ZM_equal(ZM_mul(rowslice(IM,rk+1,m), X), RHS))
    3638             :     {
    3639        7893 :       d = vecsmall_copy(dbest);
    3640        7893 :       goto END;
    3641             :     }
    3642           0 :     set_avma(av);
    3643             :   }
    3644             : END:
    3645      120748 :   *rr = rbest; guncloneNULL(dbest);
    3646      120748 :   return gerepileuptoleaf(av0, d);
    3647             : }
    3648             : 
    3649             : /* set *pr = dim Ker x */
    3650             : static GEN
    3651       21014 : gauss_pivot(GEN x, long *pr) {
    3652             :   GEN data;
    3653       21014 :   pivot_fun pivot = get_pivot_fun(x, x, &data);
    3654       21014 :   return RgM_pivots(x, data, pr, pivot);
    3655             : }
    3656             : 
    3657             : /* compute ker(x), x0 is a reference point when guessing whether x[i,j] ~ 0
    3658             :  * (iff x[i,j] << x0[i,j]) */
    3659             : static GEN
    3660         952 : ker_aux(GEN x, GEN x0)
    3661             : {
    3662         952 :   pari_sp av = avma;
    3663             :   GEN d,y;
    3664             :   long i,j,k,r,n;
    3665             : 
    3666         952 :   x = gauss_pivot_ker(x,x0,&d,&r);
    3667         952 :   if (!r) { set_avma(av); return cgetg(1,t_MAT); }
    3668         931 :   n = lg(x)-1; y=cgetg(r+1,t_MAT);
    3669        1960 :   for (j=k=1; j<=r; j++,k++)
    3670             :   {
    3671        1029 :     GEN p = cgetg(n+1,t_COL);
    3672             : 
    3673        1029 :     gel(y,j) = p; while (d[k]) k++;
    3674        4578 :     for (i=1; i<k; i++)
    3675        3549 :       if (d[i])
    3676             :       {
    3677        3367 :         GEN p1=gcoeff(x,d[i],k);
    3678        3367 :         gel(p,i) = gcopy(p1); gunclone(p1);
    3679             :       }
    3680             :       else
    3681         182 :         gel(p,i) = gen_0;
    3682        1029 :     gel(p,k) = gen_1; for (i=k+1; i<=n; i++) gel(p,i) = gen_0;
    3683             :   }
    3684         931 :   return gerepileupto(av,y);
    3685             : }
    3686             : 
    3687             : static GEN
    3688          77 : RgM_ker_FpM(GEN x, GEN p)
    3689             : {
    3690          77 :   pari_sp av = avma;
    3691             :   ulong pp;
    3692          77 :   x = RgM_Fp_init(x, p, &pp);
    3693          77 :   switch(pp)
    3694             :   {
    3695          35 :     case 0: x = FpM_to_mod(FpM_ker_gen(x,p,0),p); break;
    3696           7 :     case 2: x = F2m_to_mod(F2m_ker_sp(x,0)); break;
    3697          35 :     default:x = Flm_to_mod(Flm_ker_sp(x,pp,0), pp); break;
    3698             :   }
    3699          77 :   return gerepileupto(av, x);
    3700             : }
    3701             : 
    3702             : static GEN
    3703          91 : RgM_ker_FqM(GEN x, GEN pol, GEN p)
    3704             : {
    3705          91 :   pari_sp av = avma;
    3706          91 :   GEN b, T = RgX_to_FpX(pol, p);
    3707          91 :   if (signe(T) == 0) pari_err_OP("ker",x,pol);
    3708          84 :   b = FqM_ker(RgM_to_FqM(x, T, p), T, p);
    3709          84 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    3710             : }
    3711             : 
    3712             : #define code(t1,t2) ((t1 << 6) | t2)
    3713             : static GEN
    3714        8646 : RgM_ker_fast(GEN x)
    3715             : {
    3716             :   GEN p, pol;
    3717             :   long pa;
    3718        8646 :   long t = RgM_type(x, &p,&pol,&pa);
    3719        8646 :   switch(t)
    3720             :   {
    3721             :     case t_INT:    /* fall through */
    3722        7645 :     case t_FRAC:   return QM_ker(x);
    3723          77 :     case t_FFELT:  return FFM_ker(x, pol);
    3724          77 :     case t_INTMOD: return RgM_ker_FpM(x, p);
    3725             :     case code(t_POLMOD, t_INTMOD):
    3726          91 :                    return RgM_ker_FqM(x, pol, p);
    3727         756 :     default:       return NULL;
    3728             :   }
    3729             : }
    3730             : #undef code
    3731             : 
    3732             : GEN
    3733        8646 : ker(GEN x)
    3734             : {
    3735        8646 :   GEN b = RgM_ker_fast(x);
    3736        8639 :   if (b) return b;
    3737         756 :   return ker_aux(x,x);
    3738             : }
    3739             : 
    3740             : GEN
    3741       46214 : matker0(GEN x,long flag)
    3742             : {
    3743       46214 :   if (typ(x)!=t_MAT) pari_err_TYPE("matker",x);
    3744       46214 :   if (!flag) return ker(x);
    3745       45934 :   RgM_check_ZM(x, "matker");
    3746       45934 :   return ZM_ker(x);
    3747             : }
    3748             : 
    3749             : static GEN
    3750          63 : RgM_image_FpM(GEN x, GEN p)
    3751             : {
    3752          63 :   pari_sp av = avma;
    3753             :   ulong pp;
    3754          63 :   x = RgM_Fp_init(x, p, &pp);
    3755          63 :   switch(pp)
    3756             :   {
    3757          28 :     case 0: x = FpM_to_mod(FpM_image(x,p),p); break;
    3758           7 :     case 2: x = F2m_to_mod(F2m_image(x)); break;
    3759          28 :     default:x = Flm_to_mod(Flm_image(x,pp), pp); break;
    3760             :   }
    3761          63 :   return gerepileupto(av, x);
    3762             : }
    3763             : 
    3764             : static GEN
    3765          35 : RgM_image_FqM(GEN x, GEN pol, GEN p)
    3766             : {
    3767          35 :   pari_sp av = avma;
    3768          35 :   GEN b, T = RgX_to_FpX(pol, p);
    3769          35 :   if (signe(T) == 0) pari_err_OP("image",x,pol);
    3770          28 :   b = FqM_image(RgM_to_FqM(x, T, p), T, p);
    3771          28 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    3772             : }
    3773             : 
    3774             : static GEN
    3775        1463 : QM_image(GEN A)
    3776             : {
    3777        1463 :   pari_sp av = avma;
    3778        1463 :   GEN M = vecpermute(A, ZM_indeximage(vec_Q_primpart(A)));
    3779        1463 :   return gerepilecopy(av, M);
    3780             : }
    3781             : 
    3782             : #define code(t1,t2) ((t1 << 6) | t2)
    3783             : static GEN
    3784        1624 : RgM_image_fast(GEN x)
    3785             : {
    3786             :   GEN p, pol;
    3787             :   long pa;
    3788        1624 :   long t = RgM_type(x, &p,&pol,&pa);
    3789        1624 :   switch(t)
    3790             :   {
    3791             :     case t_INT:    /* fall through */
    3792        1463 :     case t_FRAC:   return QM_image(x);
    3793          49 :     case t_FFELT:  return FFM_image(x, pol);
    3794          63 :     case t_INTMOD: return RgM_image_FpM(x, p);
    3795             :     case code(t_POLMOD, t_INTMOD):
    3796          35 :                    return RgM_image_FqM(x, pol, p);
    3797          14 :     default:       return NULL;
    3798             :   }
    3799             : }
    3800             : #undef code
    3801             : 
    3802             : GEN
    3803        1624 : image(GEN x)
    3804             : {
    3805             :   GEN d, M;
    3806             :   long r;
    3807             : 
    3808        1624 :   if (typ(x)!=t_MAT) pari_err_TYPE("matimage",x);
    3809        1624 :   M = RgM_image_fast(x);
    3810        1617 :   if (M) return M;
    3811          14 :   d = gauss_pivot(x,&r); /* d left on stack for efficiency */
    3812          14 :   return image_from_pivot(x,d,r);
    3813             : }
    3814             : 
    3815             : static GEN
    3816          84 : imagecompl_aux(GEN x, GEN(*PIVOT)(GEN,long*))
    3817             : {
    3818          84 :   pari_sp av = avma;
    3819             :   GEN d,y;
    3820             :   long j,i,r;
    3821             : 
    3822          84 :   if (typ(x)!=t_MAT) pari_err_TYPE("imagecompl",x);
    3823          84 :   (void)new_chunk(lg(x) * 4 + 1); /* HACK */
    3824          84 :   d = PIVOT(x,&r); /* if (!d) then r = 0 */
    3825          84 :   set_avma(av); y = cgetg(r+1,t_VECSMALL);
    3826         126 :   for (i=j=1; j<=r; i++)
    3827          42 :     if (!d[i]) y[j++] = i;
    3828          84 :   return y;
    3829             : }
    3830             : GEN
    3831          84 : imagecompl(GEN x) { return imagecompl_aux(x, &gauss_pivot); }
    3832             : GEN
    3833           0 : ZM_imagecompl(GEN x) { return imagecompl_aux(x, &ZM_pivots); }
    3834             : 
    3835             : static GEN
    3836          28 : RgM_RgC_invimage_FpC(GEN A, GEN y, GEN p)
    3837             : {
    3838          28 :   pari_sp av = avma;
    3839             :   ulong pp;
    3840             :   GEN x;
    3841          28 :   A = RgM_Fp_init(A,p,&pp);
    3842          28 :   switch(pp)
    3843             :   {
    3844             :   case 0:
    3845           7 :     y = RgC_to_FpC(y,p);
    3846           7 :     x = FpM_FpC_invimage(A, y, p);
    3847           7 :     return x ? gerepileupto(av, FpC_to_mod(x,p)): NULL;
    3848             :   case 2:
    3849           7 :     y = RgV_to_F2v(y);
    3850           7 :     x = F2m_F2c_invimage(A, y);
    3851           7 :     return x ? gerepileupto(av, F2c_to_mod(x)): NULL;
    3852             :   default:
    3853          14 :     y = RgV_to_Flv(y,pp);
    3854          14 :     x = Flm_Flc_invimage(A, y, pp);
    3855          14 :     return x ? gerepileupto(av, Flc_to_mod(x,pp)): NULL;
    3856             :   }
    3857             : }
    3858             : 
    3859             : static GEN
    3860        2051 : RgM_RgC_invimage_fast(GEN x, GEN y)
    3861             : {
    3862             :   GEN p, pol;
    3863             :   long pa;
    3864        2051 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    3865        2051 :   switch(t)
    3866             :   {
    3867          28 :     case t_INTMOD: return RgM_RgC_invimage_FpC(x, y, p);
    3868          63 :     case t_FFELT:  return FFM_FFC_invimage(x, y, pol);
    3869        1960 :     default:       return gen_0;
    3870             :   }
    3871             : }
    3872             : 
    3873             : GEN
    3874        2156 : RgM_RgC_invimage(GEN A, GEN y)
    3875             : {
    3876        2156 :   pari_sp av = avma;
    3877        2156 :   long i, l = lg(A);
    3878             :   GEN M, x, t;
    3879        2156 :   if (l==1) return NULL;
    3880        2051 :   if (lg(y) != lgcols(A)) pari_err_DIM("inverseimage");
    3881        2051 :   M = RgM_RgC_invimage_fast(A, y);
    3882        2051 :   if (!M) return gc_NULL(av);
    3883        2030 :   if (M != gen_0) return M;
    3884        1960 :   M = ker(shallowconcat(A, y));
    3885        1960 :   i = lg(M)-1;
    3886        1960 :   if (!i) return gc_NULL(av);
    3887             : 
    3888        1701 :   x = gel(M,i); t = gel(x,l);
    3889        1701 :   if (gequal0(t)) return gc_NULL(av);
    3890             : 
    3891        1666 :   t = gneg_i(t); setlg(x,l);
    3892        1666 :   return gerepileupto(av, RgC_Rg_div(x, t));
    3893             : }
    3894             : 
    3895             : /* Return X such that m X = v (t_COL or t_MAT), resp. an empty t_COL / t_MAT
    3896             :  * if no solution exist */
    3897             : GEN
    3898        2366 : inverseimage(GEN m, GEN v)
    3899             : {
    3900             :   GEN y;
    3901        2366 :   if (typ(m)!=t_MAT) pari_err_TYPE("inverseimage",m);
    3902        2366 :   switch(typ(v))
    3903             :   {
    3904             :     case t_COL:
    3905        2128 :       y = RgM_RgC_invimage(m,v);
    3906        2128 :       return y? y: cgetg(1,t_COL);
    3907             :     case t_MAT:
    3908         238 :       y = RgM_invimage(m, v);
    3909         238 :       return y? y: cgetg(1,t_MAT);
    3910             :   }
    3911           0 :   pari_err_TYPE("inverseimage",v);
    3912             :   return NULL;/*LCOV_EXCL_LINE*/
    3913             : }
    3914             : 
    3915             : static GEN
    3916          84 : RgM_invimage_FpM(GEN A, GEN B, GEN p)
    3917             : {
    3918          84 :   pari_sp av = avma;
    3919             :   ulong pp;
    3920             :   GEN x;
    3921          84 :   A = RgM_Fp_init(A,p,&pp);
    3922          84 :   switch(pp)
    3923             :   {
    3924             :   case 0:
    3925          35 :     B = RgM_to_FpM(B,p);
    3926          35 :     x = FpM_invimage_gen(A, B, p);
    3927          35 :     return x ? gerepileupto(av, FpM_to_mod(x, p)): x;
    3928             :   case 2:
    3929           7 :     B = RgM_to_F2m(B);
    3930           7 :     x = F2m_invimage_i(A, B);
    3931           7 :     return x ? gerepileupto(av, F2m_to_mod(x)): x;
    3932             :   default:
    3933          42 :     B = RgM_to_Flm(B,pp);
    3934          42 :     x = Flm_invimage_i(A, B, pp);
    3935          42 :     return x ? gerepileupto(av, Flm_to_mod(x, pp)): x;
    3936             :   }
    3937             : }
    3938             : 
    3939             : static GEN
    3940         252 : RgM_invimage_fast(GEN x, GEN y)
    3941             : {
    3942             :   GEN p, pol;
    3943             :   long pa;
    3944         252 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    3945         252 :   switch(t)
    3946             :   {
    3947          84 :     case t_INTMOD: return RgM_invimage_FpM(x, y, p);
    3948         105 :     case t_FFELT:  return FFM_invimage(x, y, pol);
    3949          63 :     default:       return gen_0;
    3950             :   }
    3951             : }
    3952             : 
    3953             : /* find Z such that A Z = B. Return NULL if no solution */
    3954             : GEN
    3955         252 : RgM_invimage(GEN A, GEN B)
    3956             : {
    3957         252 :   pari_sp av = avma;
    3958             :   GEN d, x, X, Y;
    3959         252 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    3960         252 :   X = RgM_invimage_fast(A, B);
    3961         252 :   if (!X) return gc_NULL(av);
    3962         140 :   if (X != gen_0) return X;
    3963          63 :   x = ker(shallowconcat(RgM_neg(A), B));
    3964             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    3965             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    3966             :    * Y has at least nB columns and full rank */
    3967          63 :   nY = lg(x)-1;
    3968          63 :   if (nY < nB) return gc_NULL(av);
    3969          49 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    3970          49 :   d = cgetg(nB+1, t_VECSMALL);
    3971         441 :   for (i = nB, j = nY; i >= 1; i--, j--)
    3972             :   {
    3973         546 :     for (; j>=1; j--)
    3974         532 :       if (!gequal0(gcoeff(Y,i,j))) { d[i] = j; break; }
    3975         406 :     if (!j) return gc_NULL(av);
    3976             :   }
    3977             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    3978          35 :   Y = vecpermute(Y, d);
    3979          35 :   x = vecpermute(x, d);
    3980          35 :   X = rowslice(x, 1, nA);
    3981          35 :   return gerepileupto(av, RgM_mul(X, RgM_inv_upper(Y)));
    3982             : }
    3983             : 
    3984             : static GEN
    3985          70 : RgM_suppl_FpM(GEN x, GEN p)
    3986             : {
    3987          70 :   pari_sp av = avma;
    3988             :   ulong pp;
    3989          70 :   x = RgM_Fp_init(x, p, &pp);
    3990          70 :   switch(pp)
    3991             :   {
    3992          21 :   case 0: x = FpM_to_mod(FpM_suppl(x,p), p); break;
    3993          14 :   case 2: x = F2m_to_mod(F2m_suppl(x)); break;
    3994          35 :   default:x = Flm_to_mod(Flm_suppl(x,pp), pp); break;
    3995             :   }
    3996          70 :   return gerepileupto(av, x);
    3997             : }
    3998             : 
    3999             : static GEN
    4000         175 : RgM_suppl_fast(GEN x)
    4001             : {
    4002             :   GEN p, pol;
    4003             :   long pa;
    4004         175 :   long t = RgM_type(x,&p,&pol,&pa);
    4005         175 :   switch(t)
    4006             :   {
    4007          70 :     case t_INTMOD: return RgM_suppl_FpM(x, p);
    4008          35 :     case t_FFELT:  return FFM_suppl(x, pol);
    4009          70 :     default:       return NULL;
    4010             :   }
    4011             : }
    4012             : 
    4013             : /* x is an n x k matrix, rank(x) = k <= n. Return an invertible n x n matrix
    4014             :  * whose first k columns are given by x. If rank(x) < k, undefined result. */
    4015             : GEN
    4016         175 : suppl(GEN x)
    4017             : {
    4018         175 :   pari_sp av = avma;
    4019             :   GEN d, M;
    4020             :   long r;
    4021         175 :   if (typ(x)!=t_MAT) pari_err_TYPE("suppl",x);
    4022         175 :   M = RgM_suppl_fast(x);
    4023         175 :   if (M) return M;
    4024          70 :   init_suppl(x);
    4025          70 :   d = gauss_pivot(x,&r);
    4026          70 :   set_avma(av); return get_suppl(x,d,nbrows(x),r,&col_ei);
    4027             : }
    4028             : 
    4029             : GEN
    4030           7 : image2(GEN x)
    4031             : {
    4032           7 :   pari_sp av = avma;
    4033             :   long k, n, i;
    4034             :   GEN A, B;
    4035             : 
    4036           7 :   if (typ(x)!=t_MAT) pari_err_TYPE("image2",x);
    4037           7 :   if (lg(x) == 1) return cgetg(1,t_MAT);
    4038           7 :   A = ker(x); k = lg(A)-1;
    4039           7 :   if (!k) { set_avma(av); return gcopy(x); }
    4040           7 :   A = suppl(A); n = lg(A)-1;
    4041           7 :   B = cgetg(n-k+1, t_MAT);
    4042           7 :   for (i = k+1; i <= n; i++) gel(B,i-k) = RgM_RgC_mul(x, gel(A,i));
    4043           7 :   return gerepileupto(av, B);
    4044             : }
    4045             : 
    4046             : GEN
    4047         210 : matimage0(GEN x,long flag)
    4048             : {
    4049         210 :   switch(flag)
    4050             :   {
    4051         203 :     case 0: return image(x);
    4052           7 :     case 1: return image2(x);
    4053           0 :     default: pari_err_FLAG("matimage");
    4054             :   }
    4055             :   return NULL; /* LCOV_EXCL_LINE */
    4056             : }
    4057             : 
    4058             : static long
    4059         126 : RgM_rank_FpM(GEN x, GEN p)
    4060             : {
    4061         126 :   pari_sp av = avma;
    4062             :   ulong pp;
    4063             :   long r;
    4064         126 :   x = RgM_Fp_init(x,p,&pp);
    4065         126 :   switch(pp)
    4066             :   {
    4067          28 :   case 0: r = FpM_rank(x,p); break;
    4068          63 :   case 2: r = F2m_rank(x); break;
    4069          35 :   default:r = Flm_rank(x,pp); break;
    4070             :   }
    4071         126 :   return gc_long(av, r);
    4072             : }
    4073             : 
    4074             : static long
    4075          49 : RgM_rank_FqM(GEN x, GEN pol, GEN p)
    4076             : {
    4077          49 :   pari_sp av = avma;
    4078             :   long r;
    4079          49 :   GEN T = RgX_to_FpX(pol, p);
    4080          49 :   if (signe(T) == 0) pari_err_OP("rank",x,pol);
    4081          42 :   r = FqM_rank(RgM_to_FqM(x, T, p), T, p);
    4082          42 :   return gc_long(av,r);
    4083             : }
    4084             : 
    4085             : #define code(t1,t2) ((t1 << 6) | t2)
    4086             : static long
    4087         287 : RgM_rank_fast(GEN x)
    4088             : {
    4089             :   GEN p, pol;
    4090             :   long pa;
    4091         287 :   long t = RgM_type(x,&p,&pol,&pa);
    4092         287 :   switch(t)
    4093             :   {
    4094          42 :     case t_INT:    return ZM_rank(x);
    4095           0 :     case t_FRAC:   return QM_rank(x);
    4096         126 :     case t_INTMOD: return RgM_rank_FpM(x, p);
    4097          63 :     case t_FFELT:  return FFM_rank(x, pol);
    4098             :     case code(t_POLMOD, t_INTMOD):
    4099          49 :                    return RgM_rank_FqM(x, pol, p);
    4100           7 :     default:       return -1;
    4101             :   }
    4102             : }
    4103             : #undef code
    4104             : 
    4105             : long
    4106         287 : rank(GEN x)
    4107             : {
    4108         287 :   pari_sp av = avma;
    4109             :   long r;
    4110             : 
    4111         287 :   if (typ(x)!=t_MAT) pari_err_TYPE("rank",x);
    4112         287 :   r = RgM_rank_fast(x);
    4113         280 :   if (r >= 0) return r;
    4114           7 :   (void)gauss_pivot(x, &r);
    4115           7 :   return gc_long(av, lg(x)-1 - r);
    4116             : }
    4117             : 
    4118             : /* d a t_VECSMALL of integers in 1..n. Return the vector of the d[i]
    4119             :  * followed by the missing indices */
    4120             : static GEN
    4121       15786 : perm_complete(GEN d, long n)
    4122             : {
    4123       15786 :   GEN y = cgetg(n+1, t_VECSMALL);
    4124       15786 :   long i, j = 1, k = n, l = lg(d);
    4125       15786 :   pari_sp av = avma;
    4126       15786 :   char *T = stack_calloc(n+1);
    4127       15786 :   for (i = 1; i < l; i++) T[d[i]] = 1;
    4128      149727 :   for (i = 1; i <= n; i++)
    4129      133941 :     if (T[i]) y[j++] = i; else y[k--] = i;
    4130       15786 :   set_avma(av); return y;
    4131             : }
    4132             : 
    4133             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
    4134             : static GEN
    4135        2422 : indeximage0(long n, long r, GEN d)
    4136             : {
    4137             :   long i, j;
    4138             :   GEN v;
    4139             : 
    4140        2422 :   r = n - r; /* now r = dim Im(x) */
    4141        2422 :   v = cgetg(r+1,t_VECSMALL);
    4142       17108 :   if (d) for (i=j=1; j<=n; j++)
    4143       14686 :     if (d[j]) v[i++] = j;
    4144        2422 :   return v;
    4145             : }
    4146             : /* x an m x n t_MAT, n > 0, r = dim Ker(x), d from gauss_pivot */
    4147             : static void
    4148        7893 : indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol)
    4149             : {
    4150        7893 :   GEN IR = indexrank0(n, r, d);
    4151        7893 :   *prow = perm_complete(gel(IR,1), m);
    4152        7893 :   *pcol = perm_complete(gel(IR,2), n);
    4153        7893 : }
    4154             : 
    4155             : static GEN
    4156          28 : RgM_indexrank_FpM(GEN x, GEN p)
    4157             : {
    4158          28 :   pari_sp av = avma;
    4159             :   ulong pp;
    4160             :   GEN r;
    4161          28 :   x = RgM_Fp_init(x,p,&pp);
    4162          28 :   switch(pp)
    4163             :   {
    4164           7 :   case 0:  r = FpM_indexrank(x,p); break;
    4165           7 :   case 2:  r = F2m_indexrank(x); break;
    4166          14 :   default: r = Flm_indexrank(x,pp); break;
    4167             :   }
    4168          28 :   return gerepileupto(av, r);
    4169             : }
    4170             : 
    4171             : static GEN
    4172           0 : RgM_indexrank_FqM(GEN x, GEN pol, GEN p)
    4173             : {
    4174           0 :   pari_sp av = avma;
    4175           0 :   GEN r, T = RgX_to_FpX(pol, p);
    4176           0 :   if (signe(T) == 0) pari_err_OP("indexrank",x,pol);
    4177           0 :   r = FqM_indexrank(RgM_to_FqM(x, T, p), T, p);
    4178           0 :   return gerepileupto(av, r);
    4179             : }
    4180             : 
    4181             : #define code(t1,t2) ((t1 << 6) | t2)
    4182             : static GEN
    4183       22428 : RgM_indexrank_fast(GEN x)
    4184             : {
    4185             :   GEN p, pol;
    4186             :   long pa;
    4187       22428 :   long t = RgM_type(x,&p,&pol,&pa);
    4188       22428 :   switch(t)
    4189             :   {
    4190         392 :     case t_INT:    return ZM_indexrank(x);
    4191        1148 :     case t_FRAC:   return QM_indexrank(x);
    4192          28 :     case t_INTMOD: return RgM_indexrank_FpM(x, p);
    4193          21 :     case t_FFELT:  return FFM_indexrank(x, pol);
    4194             :     case code(t_POLMOD, t_INTMOD):
    4195           0 :                    return RgM_indexrank_FqM(x, pol, p);
    4196       20839 :     default:       return NULL;
    4197             :   }
    4198             : }
    4199             : #undef code
    4200             : 
    4201             : GEN
    4202       22428 : indexrank(GEN x)
    4203             : {
    4204             :   pari_sp av;
    4205             :   long r;
    4206             :   GEN d;
    4207       22428 :   if (typ(x)!=t_MAT) pari_err_TYPE("indexrank",x);
    4208       22428 :   d = RgM_indexrank_fast(x);
    4209       22428 :   if (d) return d;
    4210       20839 :   av = avma;
    4211       20839 :   init_indexrank(x);
    4212       20839 :   d = gauss_pivot(x, &r);
    4213       20839 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    4214             : }
    4215             : 
    4216             : GEN
    4217        2422 : ZM_indeximage(GEN x) {
    4218        2422 :   pari_sp av = avma;
    4219             :   long r;
    4220             :   GEN d;
    4221        2422 :   init_indexrank(x);
    4222        2422 :   d = ZM_pivots(x,&r);
    4223        2422 :   set_avma(av); return indeximage0(lg(x)-1, r, d);
    4224             : }
    4225             : long
    4226       51110 : ZM_rank(GEN x) {
    4227       51110 :   pari_sp av = avma;
    4228             :   long r;
    4229       51110 :   (void)ZM_pivots(x,&r);
    4230       51110 :   return gc_long(av, lg(x)-1-r);
    4231             : }
    4232             : GEN
    4233       19586 : ZM_indexrank(GEN x) {
    4234       19586 :   pari_sp av = avma;
    4235             :   long r;
    4236             :   GEN d;
    4237       19586 :   init_indexrank(x);
    4238       19586 :   d = ZM_pivots(x,&r);
    4239       19586 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    4240             : }
    4241             : 
    4242             : long
    4243           0 : QM_rank(GEN x)
    4244             : {
    4245           0 :   pari_sp av = avma;
    4246           0 :   long r = ZM_rank(Q_primpart(x));
    4247           0 :   set_avma(av);
    4248           0 :   return r;
    4249             : }
    4250             : 
    4251             : GEN
    4252        1148 : QM_indexrank(GEN x)
    4253             : {
    4254        1148 :   pari_sp av = avma;
    4255        1148 :   GEN r = ZM_indexrank(Q_primpart(x));
    4256        1148 :   return gerepileupto(av, r);
    4257             : }
    4258             : 
    4259             : /*******************************************************************/
    4260             : /*                                                                 */
    4261             : /*                             ZabM                                */
    4262             : /*                                                                 */
    4263             : /*******************************************************************/
    4264             : 
    4265             : static GEN
    4266        1866 : FpXM_ratlift(GEN a, GEN q)
    4267             : {
    4268             :   GEN B, y;
    4269        1866 :   long i, j, l = lg(a), n;
    4270        1866 :   B = sqrti(shifti(q,-1));
    4271        1866 :   y = cgetg(l, t_MAT);
    4272        1866 :   if (l==1) return y;
    4273        1866 :   n = lgcols(a);
    4274        5351 :   for (i=1; i<l; i++)
    4275             :   {
    4276        4351 :     GEN yi = cgetg(n, t_COL);
    4277       56840 :     for (j=1; j<n; j++)
    4278             :     {
    4279       53355 :       GEN v = FpX_ratlift(gmael(a,i,j), q, B, B, NULL);
    4280       53355 :       if (!v) return NULL;
    4281       52489 :       gel(yi, j) = RgX_renormalize(v);
    4282             :     }
    4283        3485 :     gel(y,i) = yi;
    4284             :   }
    4285        1000 :   return y;
    4286             : }
    4287             : 
    4288             : static GEN
    4289        4599 : FlmV_recover_pre(GEN a, GEN M, ulong p, ulong pi, long sv)
    4290             : {
    4291        4599 :   GEN a1 = gel(a,1);
    4292        4599 :   long i, j, k, l = lg(a1), n, lM = lg(M);
    4293        4599 :   GEN v = cgetg(lM, t_VECSMALL);
    4294        4599 :   GEN y = cgetg(l, t_MAT);
    4295        4599 :   if (l==1) return y;
    4296        4599 :   n = lgcols(a1);
    4297       34853 :   for (i=1; i<l; i++)
    4298             :   {
    4299       30254 :     GEN yi = cgetg(n, t_COL);
    4300      670661 :     for (j=1; j<n; j++)
    4301             :     {
    4302      640408 :       for (k=1; k<lM; k++) uel(v,k) = umael(gel(a,k),i,j);
    4303      640408 :       gel(yi, j) = Flm_Flc_mul_pre_Flx(M, v, p, pi, sv);
    4304             :     }
    4305       30253 :     gel(y,i) = yi;
    4306             :   }
    4307        4599 :   return y;
    4308             : }
    4309             : 
    4310             : static GEN
    4311           0 : FlkM_inv(GEN M, GEN P, ulong p)
    4312             : {
    4313           0 :   ulong pi = get_Fl_red(p);
    4314           0 :   GEN R = Flx_roots(P, p);
    4315           0 :   long l = lg(R), i;
    4316           0 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    4317           0 :   GEN V = cgetg(l, t_VEC);
    4318           0 :   for(i=1; i<l; i++)
    4319             :   {
    4320           0 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, pi);
    4321           0 :     GEN H = Flm_inv_sp(FlxM_eval_powers_pre(M, pows, p, pi), NULL, p);
    4322           0 :     if (!H) return NULL;
    4323           0 :     gel(V, i) = H;
    4324             :   }
    4325           0 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    4326             : }
    4327             : 
    4328             : static GEN
    4329        2732 : FlkM_adjoint(GEN M, GEN P, ulong p)
    4330             : {
    4331        2732 :   ulong pi = get_Fl_red(p);
    4332        2732 :   GEN R = Flx_roots(P, p);
    4333        2733 :   long l = lg(R), i;
    4334        2733 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    4335        2733 :   GEN V = cgetg(l, t_VEC);
    4336       13378 :   for(i=1; i<l; i++)
    4337             :   {
    4338       10645 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, pi);
    4339       10646 :     gel(V, i) = Flm_adjoint(FlxM_eval_powers_pre(M, pows, p, pi), p);
    4340             :   }
    4341        2733 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    4342             : }
    4343             : 
    4344             : 
    4345             : static GEN
    4346        2524 : ZabM_inv_slice(GEN A, GEN Q, GEN P, GEN *mod)
    4347             : {
    4348        2524 :   pari_sp av = avma;
    4349        2524 :   long i, n = lg(P)-1, w = varn(Q);
    4350             :   GEN H, T;
    4351        2524 :   if (n == 1)
    4352             :   {
    4353        2394 :     ulong p = uel(P,1);
    4354        2394 :     GEN Ap = FqM_to_FlxM(A, Q, utoi(p));
    4355        2393 :     GEN Qp = ZX_to_Flx(Q, p);
    4356        2393 :     GEN Hp = FlkM_adjoint(Ap, Qp, p);
    4357        2394 :     Hp = gerepileupto(av, FlxM_to_ZXM(Hp));
    4358        2394 :     *mod = utoi(p); return Hp;
    4359             :   }
    4360         130 :   T = ZV_producttree(P);
    4361         130 :   A = ZXM_nv_mod_tree(A, P, T, w);
    4362         130 :   Q = ZX_nv_mod_tree(Q, P, T);
    4363         130 :   H = cgetg(n+1, t_VEC);
    4364         469 :   for(i=1; i <= n; i++)
    4365             :   {
    4366         339 :     ulong p = P[i];
    4367         339 :     GEN a = gel(A,i), q = gel(Q, i);
    4368         339 :     gel(H,i) = FlkM_adjoint(a, q, p);
    4369             :   }
    4370         130 :   H = nxMV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    4371         130 :   *mod = gmael(T, lg(T)-1, 1);
    4372         130 :   gerepileall(av, 2, &H, mod);
    4373         130 :   return H;
    4374             : }
    4375             : 
    4376             : GEN
    4377        2524 : ZabM_inv_worker(GEN P, GEN A, GEN Q)
    4378             : {
    4379        2524 :   GEN V = cgetg(3, t_VEC);
    4380        2524 :   gel(V,1) = ZabM_inv_slice(A, Q, P, &gel(V,2));
    4381        2524 :   return V;
    4382             : }
    4383             : 
    4384             : static GEN
    4385        7924 : vecnorml1(GEN a)
    4386             : {
    4387             :   long i, l;
    4388        7924 :   GEN g = cgetg_copy(a, &l);
    4389      128828 :   for (i=1; i<l; i++)
    4390      120904 :     gel(g, i) = gnorml1_fake(gel(a,i));
    4391        7924 :   return g;
    4392             : }
    4393             : 
    4394             : static GEN
    4395        1764 : ZabM_true_Hadamard(GEN a)
    4396             : {
    4397        1764 :   pari_sp av = avma;
    4398        1764 :   long n = lg(a)-1, i;
    4399             :   GEN B;
    4400        1764 :   if (n == 0) return gen_1;
    4401        1764 :   if (n == 1) return gnorml1_fake(gcoeff(a,1,1));
    4402        1190 :   B = gen_1;
    4403        1190 :   for (i = 1; i <= n; i++) B = gmul(B, gnorml2(RgC_gtofp(vecnorml1(gel(a,i)),DEFAULTPREC)));
    4404        1190 :   return gerepileuptoint(av, ceil_safe(sqrtr_abs(B)));
    4405             : }
    4406             : 
    4407             : GEN
    4408        1764 : ZabM_inv(GEN A, GEN Q, long n, GEN *pt_den)
    4409             : {
    4410        1764 :   pari_sp av = avma;
    4411        1764 :   long m = lg(A)-1;
    4412             :   GEN bnd, H, D, d, mod, worker;
    4413        1764 :   if (m == 0)
    4414             :   {
    4415           0 :     if (pt_den) *pt_den = gen_1;
    4416           0 :     return cgetg(1, t_MAT);
    4417             :   }
    4418        1764 :   bnd = ZabM_true_Hadamard(A);
    4419        1764 :   worker = strtoclosure("_ZabM_inv_worker", 2, A, Q);
    4420        1764 :   H = gen_crt("ZabM_inv", worker, mkvecsmall(n), expi(bnd), m, &mod,
    4421             :               nxMV_chinese_center, FpXM_center);
    4422        1764 :   D = RgMrow_RgC_mul(H, gel(A,1), 1);
    4423        1764 :   D = ZX_rem(D, Q);
    4424        1764 :   d = Z_content(mkvec2(H, D));
    4425        1764 :   if (d)
    4426             :   {
    4427         630 :     D = ZX_Z_divexact(D, d);
    4428         630 :     H = Q_div_to_int(H, d);
    4429             :   }
    4430        1764 :   if (pt_den)
    4431             :   {
    4432        1764 :     gerepileall(av, 2, &H, &D);
    4433        1764 :     *pt_den = D; return H;
    4434             :   }
    4435           0 :   return gerepileupto(av, H);
    4436             : }
    4437             : 
    4438             : GEN
    4439           0 : ZabM_inv_ratlift(GEN M, GEN P, long n, GEN *pden)
    4440             : {
    4441           0 :   pari_sp av2, av = avma;
    4442             :   GEN q, H;
    4443           0 :   ulong m = LONG_MAX>>1;
    4444           0 :   ulong p= 1 + m - (m % n);
    4445           0 :   long lM = lg(M);
    4446           0 :   if (lM == 1) { *pden = gen_1; return cgetg(1,t_MAT); }
    4447             : 
    4448           0 :   av2 = avma;
    4449           0 :   H = NULL;
    4450             :   for(;;)
    4451           0 :   {
    4452             :     GEN Hp, Pp, Mp, Hr;
    4453           0 :     do p += n; while(!uisprime(p));
    4454           0 :     Pp = ZX_to_Flx(P, p);
    4455           0 :     Mp = FqM_to_FlxM(M, P, utoi(p));
    4456           0 :     Hp = FlkM_inv(Mp, Pp, p);
    4457           0 :     if (!Hp) continue;
    4458           0 :     if (!H)
    4459             :     {
    4460           0 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p);
    4461           0 :       q = utoipos(p);
    4462             :     }
    4463             :     else
    4464           0 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    4465           0 :     Hr = FpXM_ratlift(H, q);
    4466           0 :     if (DEBUGLEVEL>5) err_printf("ZabM_inv mod %ld (ratlift=%ld)\n", p,!!Hr);
    4467           0 :     if (Hr) {/* DONE ? */
    4468           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    4469           0 :       GEN MH = ZXQM_mul(Hl, M, P);
    4470           0 :       if (*pden)
    4471           0 :       { if (RgM_isscalar(MH, *pden)) { H = Hl; break; }}
    4472             :       else
    4473           0 :       { if (RgM_isidentity(MH)) { H = Hl; *pden = gen_1; break; } }
    4474             :     }
    4475             : 
    4476           0 :     if (gc_needed(av,2))
    4477             :     {
    4478           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_inv");
    4479           0 :       gerepileall(av2, 2, &H, &q);
    4480             :     }
    4481             :   }
    4482           0 :   gerepileall(av, 2, &H, pden);
    4483           0 :   return H;
    4484             : }
    4485             : 
    4486             : static GEN
    4487        1866 : FlkM_ker(GEN M, GEN P, ulong p)
    4488             : {
    4489        1866 :   ulong pi = get_Fl_red(p);
    4490        1866 :   GEN R = Flx_roots(P, p);
    4491        1866 :   long l = lg(R), i, dP = degpol(P), r;
    4492             :   GEN M1, K, D;
    4493        1866 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    4494        1866 :   GEN V = cgetg(l, t_VEC);
    4495        1866 :   M1 = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,1), dP, p, pi), p, pi);
    4496        1866 :   K = Flm_ker_sp(M1, p, 2);
    4497        1866 :   r = lg(gel(K,1)); D = gel(K,2);
    4498        1866 :   gel(V, 1) = gel(K,1);
    4499        3818 :   for(i=2; i<l; i++)
    4500             :   {
    4501        1952 :     GEN Mi = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,i), dP, p, pi), p, pi);
    4502        1952 :     GEN K = Flm_ker_sp(Mi, p, 2);
    4503        1952 :     if (lg(gel(K,1)) != r || !zv_equal(D, gel(K,2))) return NULL;
    4504        1952 :     gel(V, i) = gel(K,1);
    4505             :   }
    4506        1866 :   return mkvec2(FlmV_recover_pre(V, W, p, pi, P[1]), D);
    4507             : }
    4508             : 
    4509             : GEN
    4510         966 : ZabM_ker(GEN M, GEN P, long n)
    4511             : {
    4512         966 :   pari_sp av2, av = avma;
    4513             :   GEN q, H, D;
    4514         966 :   ulong m = LONG_MAX>>1;
    4515         966 :   ulong p= 1 + m - (m % n);
    4516         966 :   av2 = avma;
    4517         966 :   H = NULL; D = NULL;
    4518             :   for(;;)
    4519         900 :   {
    4520             :     GEN Kp, Hp, Dp, Pp, Mp, Hr;
    4521       30269 :     do p += n; while(!uisprime(p));
    4522        1866 :     Pp = ZX_to_Flx(P, p);
    4523        1866 :     Mp = FqM_to_FlxM(M, P, utoi(p));
    4524        1866 :     Kp = FlkM_ker(Mp, Pp, p);
    4525        1866 :     if (!Kp) continue;
    4526        1866 :     Hp = gel(Kp,1); Dp = gel(Kp,2);
    4527        1866 :     if (H && (lg(Hp)>lg(H) || (lg(Hp)==lg(H) && vecsmall_lexcmp(Dp,D)>0))) continue;
    4528        1866 :     if (!H || (lg(Hp)<lg(H) || vecsmall_lexcmp(Dp,D)<0))
    4529             :     {
    4530         966 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p); D = Dp;
    4531         966 :       q = utoipos(p);
    4532             :     }
    4533             :     else
    4534         900 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    4535        1866 :     Hr = FpXM_ratlift(H, q);
    4536        1866 :     if (DEBUGLEVEL>5) err_printf("ZabM_ker mod %ld (ratlift=%ld)\n", p,!!Hr);
    4537        1866 :     if (Hr) {/* DONE ? */
    4538        1000 :       GEN Hl = vec_Q_primpart(Hr);
    4539        1000 :       GEN MH = ZXQM_mul(M, Hl,P);
    4540        1000 :       if (gequal0(MH)) { H = Hl;  break; }
    4541             :     }
    4542             : 
    4543         900 :     if (gc_needed(av,2))
    4544             :     {
    4545           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_ker");
    4546           0 :       gerepileall(av2, 3, &H, &D, &q);
    4547             :     }
    4548             :   }
    4549         966 :   return gerepilecopy(av, H);
    4550             : }
    4551             : 
    4552             : GEN
    4553        2352 : ZabM_indexrank(GEN M, GEN P, long n)
    4554             : {
    4555        2352 :   pari_sp av = avma;
    4556        2352 :   ulong m = LONG_MAX>>1;
    4557        2352 :   ulong p = 1+m-(m%n), D = degpol(P);
    4558        2352 :   long lM = lg(M), lmax = 0, c = 0;
    4559             :   GEN v;
    4560             :   for(;;)
    4561         672 :   {
    4562             :     GEN R, Mp, K;
    4563             :     ulong pi;
    4564             :     long l;
    4565       60911 :     do p += n; while (!uisprime(p));
    4566        3024 :     pi = get_Fl_red(p);
    4567        3024 :     R = Flx_roots(ZX_to_Flx(P, p), p);
    4568        3024 :     Mp = FqM_to_FlxM(M, P, utoipos(p));
    4569        3024 :     K = FlxM_eval_powers_pre(Mp, Fl_powers_pre(uel(R,1), D,p,pi), p,pi);
    4570        3024 :     v = Flm_indexrank(K, p);
    4571        3024 :     l = lg(gel(v,2));
    4572        3024 :     if (l == lM) break;
    4573         896 :     if (lmax >= 0 && l > lmax) { lmax = l; c = 0; } else c++;
    4574         896 :     if (c > 2)
    4575             :     { /* probably not maximal rank, expensive check */
    4576         224 :       lM -= lg(ZabM_ker(M, P, n))-1; /* actual rank (+1) */
    4577         224 :       if (lmax == lM) break;
    4578           0 :       lmax = -1; /* disable check */
    4579             :     }
    4580             :   }
    4581        2352 :   return gerepileupto(av, v);
    4582             : }
    4583             : 
    4584             : #if 0
    4585             : GEN
    4586             : ZabM_gauss(GEN M, GEN P, long n, GEN *den)
    4587             : {
    4588             :   pari_sp av = avma;
    4589             :   GEN v, S, W;
    4590             :   v = ZabM_indexrank(M, P, n);
    4591             :   S = shallowmatextract(M,gel(v,1),gel(v,2));
    4592             :   W = ZabM_inv(S, P, n, den);
    4593             :   gerepileall(av,2,&W,den);
    4594             :   return W;
    4595             : }
    4596             : #endif
    4597             : 
    4598             : GEN
    4599         287 : ZabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *den)
    4600             : {
    4601         287 :   GEN v = ZabM_indexrank(M, P, n);
    4602         287 :   if (pv) *pv = v;
    4603         287 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    4604         287 :   return ZabM_inv(M, P, n, den);
    4605             : }
    4606             : GEN
    4607        4459 : ZM_pseudoinv(GEN M, GEN *pv, GEN *den)
    4608             : {
    4609        4459 :   GEN v = ZM_indexrank(M);
    4610        4459 :   if (pv) *pv = v;
    4611        4459 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    4612        4459 :   return ZM_inv(M, den);
    4613             : }
    4614             : 
    4615             : /*******************************************************************/
    4616             : /*                                                                 */
    4617             : /*                   Structured Elimination                        */
    4618             : /*                                                                 */
    4619             : /*******************************************************************/
    4620             : 
    4621             : static void
    4622      100903 : rem_col(GEN c, long i, GEN iscol, GEN Wrow, long *rcol, long *rrow)
    4623             : {
    4624      100903 :   long lc = lg(c), k;
    4625      100903 :   iscol[i] = 0; (*rcol)--;
    4626      892556 :   for (k = 1; k < lc; ++k)
    4627             :   {
    4628      791653 :     Wrow[c[k]]--;
    4629      791653 :     if (Wrow[c[k]]==0) (*rrow)--;
    4630             :   }
    4631      100903 : }
    4632             : 
    4633             : static void
    4634        6056 : rem_singleton(GEN M, GEN iscol, GEN Wrow, long *rcol, long *rrow)
    4635             : {
    4636             :   long i, j;
    4637        6056 :   long nbcol = lg(iscol)-1, last;
    4638             :   do
    4639             :   {
    4640        8001 :     last = 0;
    4641    18978154 :     for (i = 1; i <= nbcol; ++i)
    4642    18970153 :       if (iscol[i])
    4643             :       {
    4644     9754714 :         GEN c = gmael(M, i, 1);
    4645     9754714 :         long lc = lg(c);
    4646    91075660 :         for (j = 1; j < lc; ++j)
    4647    81333723 :           if (Wrow[c[j]] == 1)
    4648             :           {
    4649       12777 :             rem_col(c, i, iscol, Wrow, rcol, rrow);
    4650       12777 :             last=1; break;
    4651             :           }
    4652             :       }
    4653        8001 :   } while (last);
    4654        6056 : }
    4655             : 
    4656             : static GEN
    4657        5930 : fill_wcol(GEN M, GEN iscol, GEN Wrow, long *w, GEN wcol)
    4658             : {
    4659        5930 :   long nbcol = lg(iscol)-1;
    4660             :   long i, j, m, last;
    4661             :   GEN per;
    4662       14724 :   for (m = 2, last=0; !last ; m++)
    4663             :   {
    4664    22266981 :     for (i = 1; i <= nbcol; ++i)
    4665             :     {
    4666    22258187 :       wcol[i] = 0;
    4667    22258187 :       if (iscol[i])
    4668             :       {
    4669    11426297 :         GEN c = gmael(M, i, 1);
    4670    11426297 :         long lc = lg(c);
    4671   106721532 :         for (j = 1; j < lc; ++j)
    4672    95295235 :           if (Wrow[c[j]] == m) {  wcol[i]++; last = 1; }
    4673             :       }
    4674             :     }
    4675             :   }
    4676        5930 :   per = vecsmall_indexsort(wcol);
    4677        5930 :   *w = wcol[per[nbcol]];
    4678        5930 :   return per;
    4679             : }
    4680             : 
    4681             : /* M is a RgMs with nbrow rows, A a list of row indices.
    4682             :    Eliminate rows of M with a single entry that do not belong to A,
    4683             :    and the corresponding columns. Also eliminate columns until #colums=#rows.
    4684             :    Return pcol and prow:
    4685             :    pcol is a map from the new columns indices to the old one.
    4686             :    prow is a map from the old rows indices to the new one (0 if removed).
    4687             : */
    4688             : 
    4689             : void
    4690         126 : RgMs_structelim_col(GEN M, long nbcol, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    4691             : {
    4692             :   long i,j,k;
    4693         126 :   long lA = lg(A);
    4694         126 :   GEN prow = cgetg(nbrow+1, t_VECSMALL);
    4695         126 :   GEN pcol = zero_zv(nbcol);
    4696         126 :   pari_sp av = avma;
    4697         126 :   long rcol = nbcol, rrow = 0, imin = nbcol - usqrt(nbcol);
    4698         126 :   GEN iscol = const_vecsmall(nbcol, 1);
    4699         126 :   GEN Wrow  = zero_zv(nbrow);
    4700         126 :   GEN wcol = cgetg(nbcol+1, t_VECSMALL);
    4701         126 :   pari_sp av2=avma;
    4702      126721 :   for (i = 1; i <= nbcol; ++i)
    4703             :   {
    4704      126595 :     GEN F = gmael(M, i, 1);
    4705      126595 :     long l = lg(F)-1;
    4706     1118177 :     for (j = 1; j <= l; ++j)
    4707      991582 :       Wrow[F[j]]++;
    4708             :   }
    4709         126 :   for (j = 1; j < lA; ++j)
    4710             :   {
    4711           0 :     if (Wrow[A[j]] == 0) { *p_col=NULL; return; }
    4712           0 :     Wrow[A[j]] = -1;
    4713             :   }
    4714      237272 :   for (i = 1; i <= nbrow; ++i)
    4715      237146 :     if (Wrow[i])
    4716       67633 :       rrow++;
    4717         126 :   rem_singleton(M, iscol, Wrow, &rcol, &rrow);
    4718         126 :   if (rcol<rrow) pari_err_BUG("RgMs_structelim, rcol<rrow");
    4719        6182 :   for (; rcol>rrow;)
    4720             :   {
    4721             :     long w;
    4722        5930 :     GEN per = fill_wcol(M, iscol, Wrow, &w, wcol);
    4723       94056 :     for (i = nbcol; i>=imin && wcol[per[i]]>=w && rcol>rrow; i--)
    4724       88126 :       rem_col(gmael(M, per[i], 1), per[i], iscol, Wrow, &rcol, &rrow);
    4725        5930 :     rem_singleton(M, iscol, Wrow, &rcol, &rrow);
    4726        5930 :     set_avma(av2);
    4727             :   }
    4728      126721 :   for (j = 1, i = 1; i <= nbcol; ++i)
    4729      126595 :     if (iscol[i])
    4730       25692 :       pcol[j++] = i;
    4731         126 :   setlg(pcol,j);
    4732      237272 :   for (k = 1, i = 1; i <= nbrow; ++i)
    4733      237146 :     prow[i] = Wrow[i] ? k++: 0;
    4734         126 :   set_avma(av);
    4735         126 :   *p_col = pcol; *p_row = prow;
    4736             : }
    4737             : 
    4738             : void
    4739           0 : RgMs_structelim(GEN M, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    4740             : {
    4741           0 :   RgMs_structelim_col(M, lg(M)-1, nbrow, A, p_col, p_row);
    4742           0 : }
    4743             : 
    4744             : /*******************************************************************/
    4745             : /*                                                                 */
    4746             : /*                        EIGENVECTORS                             */
    4747             : /*   (independent eigenvectors, sorted by increasing eigenvalue)   */
    4748             : /*                                                                 */
    4749             : /*******************************************************************/
    4750             : /* assume x is square of dimension > 0 */
    4751             : static int
    4752          14 : RgM_is_symmetric_cx(GEN x, long bit)
    4753             : {
    4754          14 :   pari_sp av = avma;
    4755          14 :   long i, j, l = lg(x);
    4756         112 :   for (i = 1; i < l; i++)
    4757         392 :     for (j = 1; j < i; j++)
    4758             :     {
    4759         294 :       GEN a = gcoeff(x,i,j), b = gcoeff(x,j,i), c = gsub(a,b);
    4760         294 :       if (!gequal0(c) && gexpo(c) - gexpo(a) > -bit) return gc_long(av,0);
    4761             :     }
    4762          14 :   return gc_long(av,1);
    4763             : }
    4764             : static GEN
    4765          14 : eigen_err(GEN x, long prec, long flag)
    4766             : {
    4767          14 :   pari_sp av = avma;
    4768          14 :   if (!RgM_is_symmetric_cx(x, 10-prec2nbits(prec))) pari_err_PREC("mateigen");
    4769             :   /* approximately symmetric: recover */
    4770          14 :   x = jacobi(x, prec); if (flag) return x;
    4771           7 :   return gerepilecopy(av, gel(x,1));
    4772             : }
    4773             : GEN
    4774          77 : mateigen(GEN x, long flag, long prec)
    4775             : {
    4776             :   GEN y, R, T;
    4777          77 :   long k, l, ex, n = lg(x);
    4778          77 :   pari_sp av = avma;
    4779             : 
    4780          77 :   if (typ(x)!=t_MAT) pari_err_TYPE("eigen",x);
    4781          77 :   if (n != 1 && n != lgcols(x)) pari_err_DIM("eigen");
    4782          77 :   if (flag < 0 || flag > 1) pari_err_FLAG("mateigen");
    4783          77 :   if (n == 1)
    4784             :   {
    4785          14 :     if (flag) retmkvec2(cgetg(1,t_VEC), cgetg(1,t_MAT));
    4786           7 :     return cgetg(1,t_VEC);
    4787             :   }
    4788          63 :   if (n == 2)
    4789             :   {
    4790          14 :     if (flag) retmkvec2(mkveccopy(gcoeff(x,1,1)), matid(1));
    4791           7 :     return matid(1);
    4792             :   }
    4793             : 
    4794          49 :   ex = 16 - prec2nbits(prec);
    4795          49 :   T = charpoly(x,0);
    4796          49 :   if (RgX_is_QX(T))
    4797             :   {
    4798          28 :     T = ZX_radical( Q_primpart(T) );
    4799          28 :     R = nfrootsQ(T);
    4800          28 :     if (lg(R)-1 < degpol(T))
    4801             :     { /* add missing complex roots */
    4802          14 :       GEN r = cleanroots(RgX_div(T, roots_to_pol(R, 0)), prec);
    4803          14 :       settyp(r, t_VEC);
    4804          14 :       R = shallowconcat(R, r);
    4805             :     }
    4806             :   }
    4807             :   else
    4808             :   {
    4809          21 :     GEN r1, v = vectrunc_init(lg(T));
    4810             :     long e;
    4811          21 :     R = cleanroots(T,prec);
    4812          21 :     r1 = NULL;
    4813         133 :     for (k = 1; k < lg(R); k++)
    4814             :     {
    4815         112 :       GEN r2 = gel(R,k), r = grndtoi(r2, &e);
    4816         112 :       if (e < ex) r2 = r;
    4817         112 :       if (r1)
    4818             :       {
    4819          91 :         r = gsub(r1,r2);
    4820          91 :         if (gequal0(r) || gexpo(r) < ex) continue;
    4821             :       }
    4822          70 :       vectrunc_append(v, r2);
    4823          70 :       r1 = r2;
    4824             :     }
    4825          21 :     R = v;
    4826             :   }
    4827             :   /* R = distinct complex roots of charpoly(x) */
    4828          49 :   l = lg(R); y = cgetg(l, t_VEC);
    4829         231 :   for (k = 1; k < l; k++)
    4830             :   {
    4831         196 :     GEN F = ker_aux(RgM_Rg_sub_shallow(x, gel(R,k)), x);
    4832         196 :     long d = lg(F)-1;
    4833         196 :     if (!d) { set_avma(av); return eigen_err(x, prec, flag); }
    4834         182 :     gel(y,k) = F;
    4835         182 :     if (flag) gel(R,k) = const_vec(d, gel(R,k));
    4836             :   }
    4837          35 :   y = shallowconcat1(y);
    4838          35 :   if (lg(y) > n) { set_avma(av); return eigen_err(x, prec, flag); }
    4839             :   /* lg(y) < n if x is not diagonalizable */
    4840          35 :   if (flag) y = mkvec2(shallowconcat1(R), y);
    4841          35 :   return gerepilecopy(av,y);
    4842             : }
    4843             : GEN
    4844           0 : eigen(GEN x, long prec) { return mateigen(x, 0, prec); }
    4845             : 
    4846             : /*******************************************************************/
    4847             : /*                                                                 */
    4848             : /*                           DETERMINANT                           */
    4849             : /*                                                                 */
    4850             : /*******************************************************************/
    4851             : 
    4852             : GEN
    4853        4060 : det0(GEN a,long flag)
    4854             : {
    4855        4060 :   switch(flag)
    4856             :   {
    4857        4046 :     case 0: return det(a);
    4858          14 :     case 1: return det2(a);
    4859           0 :     default: pari_err_FLAG("matdet");
    4860             :   }
    4861             :   return NULL; /* LCOV_EXCL_LINE */
    4862             : }
    4863             : 
    4864             : /* M a 2x2 matrix, returns det(M) */
    4865             : static GEN
    4866        7830 : RgM_det2(GEN M)
    4867             : {
    4868        7830 :   pari_sp av = avma;
    4869        7830 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    4870        7830 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    4871        7830 :   return gerepileupto(av, gsub(gmul(a,d), gmul(b,c)));
    4872             : }
    4873             : /* M a 2x2 ZM, returns det(M) */
    4874             : static GEN
    4875        8958 : ZM_det2(GEN M)
    4876             : {
    4877        8958 :   pari_sp av = avma;
    4878        8958 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    4879        8958 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    4880        8958 :   return gerepileuptoint(av, subii(mulii(a,d), mulii(b, c)));
    4881             : }
    4882             : /* M a 3x3 ZM, return det(M) */
    4883             : static GEN
    4884        2961 : ZM_det3(GEN M)
    4885             : {
    4886        2961 :   pari_sp av = avma;
    4887        2961 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2), c = gcoeff(M,1,3);
    4888        2961 :   GEN d = gcoeff(M,2,1), e = gcoeff(M,2,2), f = gcoeff(M,2,3);
    4889        2961 :   GEN g = gcoeff(M,3,1), h = gcoeff(M,3,2), i = gcoeff(M,3,3);
    4890        2961 :   GEN t, D = signe(i)? mulii(subii(mulii(a,e), mulii(b,d)), i): gen_0;
    4891        2961 :   if (signe(g))
    4892             :   {
    4893        2737 :     t = mulii(subii(mulii(b,f), mulii(c,e)), g);
    4894        2737 :     D = addii(D, t);
    4895             :   }
    4896        2961 :   if (signe(h))
    4897             :   {
    4898        2758 :     t = mulii(subii(mulii(c,d), mulii(a,f)), h);
    4899        2758 :     D = addii(D, t);
    4900             :   }
    4901        2961 :   return gerepileuptoint(av, D);
    4902             : }
    4903             : 
    4904             : static GEN
    4905       10225 : det_simple_gauss(GEN a, GEN data, pivot_fun pivot)
    4906             : {
    4907       10225 :   pari_sp av = avma;
    4908       10225 :   long i,j,k, s = 1, nbco = lg(a)-1;
    4909       10225 :   GEN p, x = gen_1;
    4910             : 
    4911       10225 :   a = RgM_shallowcopy(a);
    4912       81005 :   for (i=1; i<nbco; i++)
    4913             :   {
    4914       70787 :     k = pivot(a, data, i, NULL);
    4915       70787 :     if (k > nbco) return gerepilecopy(av, gcoeff(a,i,i));
    4916       70780 :     if (k != i)
    4917             :     { /* exchange the lines s.t. k = i */
    4918        4772 :       for (j=i; j<=nbco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    4919        4772 :       s = -s;
    4920             :     }
    4921       70780 :     p = gcoeff(a,i,i);
    4922             : 
    4923       70780 :     x = gmul(x,p);
    4924      413259 :     for (k=i+1; k<=nbco; k++)
    4925             :     {
    4926      342479 :       GEN m = gcoeff(a,i,k);
    4927      342479 :       if (gequal0(m)) continue;
    4928             : 
    4929      122148 :       m = gdiv(m,p);
    4930      810556 :       for (j=i+1; j<=nbco; j++)
    4931      688408 :         gcoeff(a,j,k) = gsub(gcoeff(a,j,k), gmul(m,gcoeff(a,j,i)));
    4932             :     }
    4933       70780 :     if (gc_needed(av,2))
    4934             :     {
    4935           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    4936           0 :       gerepileall(av,2, &a,&x);
    4937             :     }
    4938             :   }
    4939       10218 :   if (s < 0) x = gneg_i(x);
    4940       10218 :   return gerepileupto(av, gmul(x, gcoeff(a,nbco,nbco)));
    4941             : }
    4942             : 
    4943             : GEN
    4944        6430 : det2(GEN a)
    4945             : {
    4946             :   GEN data;
    4947             :   pivot_fun pivot;
    4948        6430 :   long n = lg(a)-1;
    4949        6430 :   if (typ(a)!=t_MAT) pari_err_TYPE("det2",a);
    4950        6430 :   if (!n) return gen_1;
    4951        6430 :   if (n != nbrows(a)) pari_err_DIM("det2");
    4952        6430 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    4953        6430 :   if (n == 2) return RgM_det2(a);
    4954        2016 :   pivot = get_pivot_fun(a, a, &data);
    4955        2016 :   return det_simple_gauss(a, data, pivot);
    4956             : }
    4957             : 
    4958             : /* Assumes a a square t_MAT of dimension n > 0. Returns det(a) using
    4959             :  * Gauss-Bareiss. */
    4960             : static GEN
    4961         336 : det_bareiss(GEN a)
    4962             : {
    4963         336 :   pari_sp av = avma;
    4964         336 :   long nbco = lg(a)-1,i,j,k,s = 1;
    4965             :   GEN p, pprec;
    4966             : 
    4967         336 :   a = RgM_shallowcopy(a);
    4968        1078 :   for (pprec=gen_1,i=1; i<nbco; i++,pprec=p)
    4969             :   {
    4970         742 :     int diveuc = (gequal1(pprec)==0);
    4971             :     GEN ci;
    4972             : 
    4973         742 :     p = gcoeff(a,i,i);
    4974         742 :     if (gequal0(p))
    4975             :     {
    4976           0 :       k=i+1; while (k<=nbco && gequal0(gcoeff(a,i,k))) k++;
    4977           0 :       if (k>nbco) return gerepilecopy(av, p);
    4978           0 :       swap(gel(a,k), gel(a,i)); s = -s;
    4979           0 :       p = gcoeff(a,i,i);
    4980             :     }
    4981         742 :     ci = gel(a,i);
    4982        2072 :     for (k=i+1; k<=nbco; k++)
    4983             :     {
    4984        1330 :       GEN ck = gel(a,k), m = gel(ck,i);
    4985        1330 :       if (gequal0(m))
    4986             :       {
    4987           0 :         if (gequal1(p))
    4988             :         {
    4989           0 :           if (diveuc)
    4990           0 :             gel(a,k) = gdiv(gel(a,k), pprec);
    4991             :         }
    4992             :         else
    4993           0 :           for (j=i+1; j<=nbco; j++)
    4994             :           {
    4995           0 :             GEN p1 = gmul(p, gel(ck,j));
    4996           0 :             if (diveuc) p1 = gdiv(p1,pprec);
    4997           0 :             gel(ck,j) = p1;
    4998             :           }
    4999             :       }
    5000             :       else
    5001        4200 :         for (j=i+1; j<=nbco; j++)
    5002             :         {
    5003        2870 :           pari_sp av2 = avma;
    5004        2870 :           GEN p1 = gsub(gmul(p,gel(ck,j)), gmul(m,gel(ci,j)));
    5005        2870 :           if (diveuc) p1 = gdiv(p1,pprec);
    5006        2870 :           gel(ck,j) = gerepileupto(av2, p1);
    5007             :         }
    5008        1330 :       if (gc_needed(av,2))
    5009             :       {
    5010           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    5011           0 :         gerepileall(av,2, &a,&pprec);
    5012           0 :         ci = gel(a,i);
    5013           0 :         p = gcoeff(a,i,i);
    5014             :       }
    5015             :     }
    5016             :   }
    5017         336 :   p = gcoeff(a,nbco,nbco);
    5018         336 :   p = (s < 0)? gneg(p): gcopy(p);
    5019         336 :   return gerepileupto(av, p);
    5020             : }
    5021             : 
    5022             : /* count non-zero entries in col j, at most 'max' of them.
    5023             :  * Return their indices */
    5024             : static GEN
    5025        1148 : col_count_non_zero(GEN a, long j, long max)
    5026             : {
    5027        1148 :   GEN v = cgetg(max+1, t_VECSMALL);
    5028        1148 :   GEN c = gel(a,j);
    5029        1148 :   long i, l = lg(a), k = 1;
    5030        4410 :   for (i = 1; i < l; i++)
    5031        4284 :     if (!gequal0(gel(c,i)))
    5032             :     {
    5033        4144 :       if (k > max) return NULL; /* fail */
    5034        3122 :       v[k++] = i;
    5035             :     }
    5036         126 :   setlg(v, k); return v;
    5037             : }
    5038             : /* count non-zero entries in row i, at most 'max' of them.
    5039             :  * Return their indices */
    5040             : static GEN
    5041        1134 : row_count_non_zero(GEN a, long i, long max)
    5042             : {
    5043        1134 :   GEN v = cgetg(max+1, t_VECSMALL);
    5044        1134 :   long j, l = lg(a), k = 1;
    5045        4354 :   for (j = 1; j < l; j++)
    5046        4242 :     if (!gequal0(gcoeff(a,i,j)))
    5047             :     {
    5048        4130 :       if (k > max) return NULL; /* fail */
    5049        3108 :       v[k++] = j;
    5050             :     }
    5051         112 :   setlg(v, k); return v;
    5052             : }
    5053             : 
    5054             : static GEN det_develop(GEN a, long max, double bound);
    5055             : /* (-1)^(i+j) a[i,j] * det RgM_minor(a,i,j) */
    5056             : static GEN
    5057         210 : coeff_det(GEN a, long i, long j, long max, double bound)
    5058             : {
    5059         210 :   GEN c = gcoeff(a, i, j);
    5060         210 :   c = gmul(c, det_develop(RgM_minor(a, i,j), max, bound));
    5061         210 :   if (odd(i+j)) c = gneg(c);
    5062         210 :   return c;
    5063             : }
    5064             : /* a square t_MAT, 'bound' a rough upper bound for the number of
    5065             :  * multiplications we are willing to pay while developing rows/columns before
    5066             :  * switching to Gaussian elimination */
    5067             : static GEN
    5068         448 : det_develop(GEN M, long max, double bound)
    5069             : {
    5070         448 :   pari_sp av = avma;
    5071         448 :   long i,j, n = lg(M)-1, lbest = max+2, best_col = 0, best_row = 0;
    5072         448 :   GEN best = NULL;
    5073             : 
    5074         448 :   if (bound < 1.) return det_bareiss(M); /* too costly now */
    5075             : 
    5076         336 :   switch(n)
    5077             :   {
    5078           0 :     case 0: return gen_1;
    5079           0 :     case 1: return gcopy(gcoeff(M,1,1));
    5080          14 :     case 2: return RgM_det2(M);
    5081             :   }
    5082         322 :   if (max > ((n+2)>>1)) max = (n+2)>>1;
    5083        1456 :   for (j = 1; j <= n; j++)
    5084             :   {
    5085        1148 :     pari_sp av2 = avma;
    5086        1148 :     GEN v = col_count_non_zero(M, j, max);
    5087             :     long lv;
    5088        1148 :     if (!v || (lv = lg(v)) >= lbest) { set_avma(av2); continue; }
    5089          98 :     if (lv == 1) { set_avma(av); return gen_0; }
    5090          98 :     if (lv == 2) {
    5091          14 :       set_avma(av);
    5092          14 :       return gerepileupto(av, coeff_det(M,v[1],j,max,bound));
    5093             :     }
    5094          84 :     best = v; lbest = lv; best_col = j;
    5095             :   }
    5096        1442 :   for (i = 1; i <= n; i++)
    5097             :   {
    5098        1134 :     pari_sp av2 = avma;
    5099        1134 :     GEN v = row_count_non_zero(M, i, max);
    5100             :     long lv;
    5101        1134 :     if (!v || (lv = lg(v)) >= lbest) { set_avma(av2); continue; }
    5102           0 :     if (lv == 1) { set_avma(av); return gen_0; }
    5103           0 :     if (lv == 2) {
    5104           0 :       set_avma(av);
    5105           0 :       return gerepileupto(av, coeff_det(M,i,v[1],max,bound));
    5106             :     }
    5107           0 :     best = v; lbest = lv; best_row = i;
    5108             :   }
    5109         308 :   if (best_row)
    5110             :   {
    5111           0 :     double d = lbest-1;
    5112           0 :     GEN s = NULL;
    5113             :     long k;
    5114           0 :     bound /= d*d*d;
    5115           0 :     for (k = 1; k < lbest; k++)
    5116             :     {
    5117           0 :       GEN c = coeff_det(M, best_row, best[k], max, bound);
    5118           0 :       s = s? gadd(s, c): c;
    5119             :     }
    5120           0 :     return gerepileupto(av, s);
    5121             :   }
    5122         308 :   if (best_col)
    5123             :   {
    5124          84 :     double d = lbest-1;
    5125          84 :     GEN s = NULL;
    5126             :     long k;
    5127          84 :     bound /= d*d*d;
    5128         280 :     for (k = 1; k < lbest; k++)
    5129             :     {
    5130         196 :       GEN c = coeff_det(M, best[k], best_col, max, bound);
    5131         196 :       s = s? gadd(s, c): c;
    5132             :     }
    5133          84 :     return gerepileupto(av, s);
    5134             :   }
    5135         224 :   return det_bareiss(M);
    5136             : }
    5137             : 
    5138             : /* area of parallelogram bounded by (v1,v2) */
    5139             : static GEN
    5140       53995 : parallelogramarea(GEN v1, GEN v2)
    5141       53995 : { return gsub(gmul(gnorml2(v1), gnorml2(v2)), gsqr(RgV_dotproduct(v1, v2))); }
    5142             : 
    5143             : /* Square of Hadamard bound for det(a), a square matrix.
    5144             :  * Slightly improvement: instead of using the column norms, use the area of
    5145             :  * the parallelogram formed by pairs of consecutive vectors */
    5146             : GEN
    5147       17039 : RgM_Hadamard(GEN a)
    5148             : {
    5149       17039 :   pari_sp av = avma;
    5150       17039 :   long n = lg(a)-1, i;
    5151             :   GEN B;
    5152       17039 :   if (n == 0) return gen_1;
    5153       17039 :   if (n == 1) return gsqr(gcoeff(a,1,1));
    5154       17039 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    5155       17039 :   B = gen_1;
    5156       71034 :   for (i = 1; i <= n/2; i++)
    5157       53995 :     B = gmul(B, parallelogramarea(gel(a,2*i-1), gel(a,2*i)));
    5158       17039 :   if (odd(n)) B = gmul(B, gnorml2(gel(a, n)));
    5159       17039 :   return gerepileuptoint(av, ceil_safe(B));
    5160             : }
    5161             : 
    5162             : /* If B=NULL, assume B=A' */
    5163             : static GEN
    5164       53564 : ZM_det_slice(GEN A, GEN P, GEN *mod)
    5165             : {
    5166       53564 :   pari_sp av = avma;
    5167       53564 :   long i, n = lg(P)-1;
    5168             :   GEN H, T;
    5169       53564 :   if (n == 1)
    5170             :   {
    5171       42323 :     ulong Hp, p = uel(P,1);
    5172       42323 :     GEN a = ZM_to_Flm(A, p);
    5173       42322 :     Hp = Flm_det_sp(a, p);
    5174       42312 :     set_avma(av);
    5175       42312 :     *mod = utoi(p); return utoi(Hp);
    5176             :   }
    5177       11241 :   T = ZV_producttree(P);
    5178       11241 :   A = ZM_nv_mod_tree(A, P, T);
    5179       11241 :   H = cgetg(n+1, t_VECSMALL);
    5180       39809 :   for(i=1; i <= n; i++)
    5181             :   {
    5182       28570 :     ulong p = P[i];
    5183       28570 :     GEN a = gel(A,i);
    5184       28570 :     H[i] = Flm_det_sp(a, p);
    5185             :   }
    5186       11239 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    5187       11239 :   *mod = gmael(T, lg(T)-1, 1);
    5188       11239 :   gerepileall(av, 2, &H, mod);
    5189       11241 :   return H;
    5190             : }
    5191             : 
    5192             : GEN
    5193       53564 : ZM_det_worker(GEN P, GEN A)
    5194             : {
    5195       53564 :   GEN V = cgetg(3, t_VEC);
    5196       53564 :   gel(V,1) = ZM_det_slice(A, P, &gel(V,2));
    5197       53553 :   return V;
    5198             : }
    5199             : 
    5200             : /* assume dim(a) = n > 0 */
    5201             : static GEN
    5202       29798 : ZM_det_i(GEN M, long n)
    5203             : {
    5204       29798 :   const long DIXON_THRESHOLD = 40;
    5205       29798 :   pari_sp av = avma, av2;
    5206             :   long i;
    5207       29798 :   ulong p, Dp = 1;
    5208             :   forprime_t S;
    5209             :   pari_timer ti;
    5210             :   GEN H, D, mod, h, q, v, worker;
    5211       29798 :   if (n == 1) return icopy(gcoeff(M,1,1));
    5212       28958 :   if (n == 2) return ZM_det2(M);
    5213       20000 :   if (n == 3) return ZM_det3(M);
    5214       17039 :   if (DEBUGLEVEL >=4) timer_start(&ti);
    5215       17039 :   h = RgM_Hadamard(M);
    5216       17039 :   if (!signe(h)) { set_avma(av); return gen_0; }
    5217       17039 :   h = sqrti(h); q = gen_1;
    5218       17039 :   init_modular_big(&S);
    5219       17039 :   p = 0; /* -Wall */
    5220       34078 :   while( cmpii(q, h) <= 0 && (p = u_forprime_next(&S)) )
    5221             :   {
    5222       17039 :     av2 = avma; Dp = Flm_det_sp(ZM_to_Flm(M, p), p);
    5223       17039 :     set_avma(av2);
    5224       17039 :     if (Dp) break;
    5225           0 :     q = muliu(q, p);
    5226             :   }
    5227       17039 :   if (!p) pari_err_OVERFLOW("ZM_det [ran out of primes]");
    5228       17039 :   if (!Dp) { set_avma(av); return gen_0; }
    5229       17039 :   if (n <= DIXON_THRESHOLD)
    5230       17039 :     D = q;
    5231             :   else
    5232             :   {
    5233           0 :     av2 = avma;
    5234           0 :     v = cgetg(n+1, t_COL);
    5235           0 :     gel(v, 1) = gen_1; /* ensure content(v) = 1 */
    5236           0 :     for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    5237           0 :     D = Q_denom(ZM_gauss(M, v));
    5238           0 :     if (expi(D) < expi(h) >> 1)
    5239             :     { /* First try unlucky, try once more */
    5240           0 :       for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    5241           0 :       D = lcmii(D, Q_denom(ZM_gauss(M, v)));
    5242             :     }
    5243           0 :     D = gerepileuptoint(av2, D);
    5244           0 :     if (q != gen_1) D = lcmii(D, q);
    5245             :   }
    5246             :   /* determinant is a multiple of D */
    5247       17039 :   if (DEBUGLEVEL >=4)
    5248           0 :     timer_printf(&ti,"ZM_det: Dixon %ld/%ld bits",expi(D),expi(h));
    5249       17039 :   h = divii(h, D);
    5250       17039 :   worker = strtoclosure("_ZM_det_worker", 1, M);
    5251       17039 :   H = gen_crt("ZM_det", worker, D, expi(h)+1, lg(M)-1, &mod, ZV_chinese, NULL);
    5252       17039 :   if (D) H = Fp_div(H, D, mod);
    5253       17039 :   H = Fp_center(H, mod, shifti(mod,-1));
    5254       17039 :   if (D) H = mulii(H, D);
    5255       17039 :   return gerepileuptoint(av, H);
    5256             : }
    5257             : 
    5258             : static GEN
    5259        1519 : RgM_det_FpM(GEN a, GEN p)
    5260             : {
    5261        1519 :   pari_sp av = avma;
    5262             :   ulong pp, d;
    5263        1519 :   a = RgM_Fp_init(a,p,&pp);
    5264        1519 :   switch(pp)
    5265             :   {
    5266          70 :   case 0: return gerepileupto(av, Fp_to_mod(FpM_det(a,p),p)); break;
    5267          14 :   case 2: d = F2m_det_sp(a); break;
    5268        1435 :   default:d = Flm_det_sp(a, pp); break;
    5269             :   }
    5270        1449 :   set_avma(av); return mkintmodu(d, pp);
    5271             : }
    5272             : 
    5273             : static GEN
    5274          42 : RgM_det_FqM(GEN x, GEN pol, GEN p)
    5275             : {
    5276          42 :   pari_sp av = avma;
    5277          42 :   GEN b, T = RgX_to_FpX(pol, p);
    5278          42 :   if (signe(T) == 0) pari_err_OP("%",x,pol);
    5279          42 :   b = FqM_det(RgM_to_FqM(x, T, p), T, p);
    5280          42 :   if (!b) return gc_NULL(av);
    5281          42 :   return gerepilecopy(av, mkpolmod(FpX_to_mod(b, p), FpX_to_mod(T, p)));
    5282             : }
    5283             : 
    5284             : #define code(t1,t2) ((t1 << 6) | t2)
    5285             : static GEN
    5286       10603 : RgM_det_fast(GEN x)
    5287             : {
    5288             :   GEN p, pol;
    5289             :   long pa;
    5290       10603 :   long t = RgM_type(x, &p,&pol,&pa);
    5291       10603 :   switch(t)
    5292             :   {
    5293         336 :     case t_INT:    return ZM_det(x);
    5294         196 :     case t_FRAC:   return QM_det(x);
    5295          63 :     case t_FFELT:  return FFM_det(x, pol);
    5296        1519 :     case t_INTMOD: return RgM_det_FpM(x, p);
    5297             :     case code(t_POLMOD, t_INTMOD):
    5298          42 :                    return RgM_det_FqM(x, pol, p);
    5299        8447 :     default:       return NULL;
    5300             :   }
    5301             : }
    5302             : #undef code
    5303             : 
    5304             : static long
    5305         238 : det_init_max(long n)
    5306             : {
    5307         238 :   if (n > 100) return 0;
    5308         238 :   if (n > 50) return 1;
    5309         238 :   if (n > 30) return 4;
    5310         238 :   return 7;
    5311             : }
    5312             : 
    5313             : GEN
    5314       14605 : det(GEN a)
    5315             : {
    5316       14605 :   long n = lg(a)-1;
    5317             :   double B;
    5318             :   GEN data, b;
    5319             :   pivot_fun pivot;
    5320             : 
    5321       14605 :   if (typ(a)!=t_MAT) pari_err_TYPE("det",a);
    5322       14605 :   if (!n) return gen_1;
    5323       14563 :   if (n != nbrows(a)) pari_err_DIM("det");
    5324       14556 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    5325       14005 :   if (n == 2) return RgM_det2(a);
    5326       10603 :   b = RgM_det_fast(a);
    5327       10603 :   if (b) return b;
    5328        8447 :   pivot = get_pivot_fun(a, a, &data);
    5329        8447 :   if (pivot != gauss_get_pivot_NZ) return det_simple_gauss(a, data, pivot);
    5330         238 :   B = (double)n;
    5331         238 :   return det_develop(a, det_init_max(n), B*B*B);
    5332             : }
    5333             : 
    5334             : GEN
    5335       29805 : ZM_det(GEN a)
    5336             : {
    5337       29805 :   long n = lg(a)-1;
    5338       29805 :   if (!n) return gen_1;
    5339       29798 :   return ZM_det_i(a, n);
    5340             : }
    5341             : 
    5342             : GEN
    5343         196 : QM_det(GEN M)
    5344             : {
    5345         196 :   pari_sp av = avma;
    5346         196 :   GEN cM, pM = Q_primitive_part(M, &cM);
    5347         196 :   GEN b = ZM_det(pM);
    5348         196 :   if (cM) b = gmul(b, gpowgs(cM, lg(M)-1));
    5349         196 :   return gerepileupto(av, b);
    5350             : }

Generated by: LCOV version 1.13