Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - alglin1.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.1 lcov report (development 22708-0f0e6fe44) Lines: 3426 3671 93.3 %
Date: 2018-06-18 05:36:21 Functions: 338 357 94.7 %
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          66 : gerepile_mat(pari_sp av, pari_sp tetpil, GEN x, long k, long m, long n, long t)
      31             : {
      32          66 :   pari_sp A, bot = pari_mainstack->bot;
      33             :   long u, i;
      34             :   size_t dec;
      35             : 
      36          66 :   (void)gerepile(av,tetpil,NULL); dec = av-tetpil;
      37             : 
      38         877 :   for (u=t+1; u<=m; u++)
      39             :   {
      40         811 :     A = (pari_sp)coeff(x,u,k);
      41         811 :     if (A < av && A >= bot) coeff(x,u,k) += dec;
      42             :   }
      43        1917 :   for (i=k+1; i<=n; i++)
      44       75213 :     for (u=1; u<=m; u++)
      45             :     {
      46       73362 :       A = (pari_sp)coeff(x,u,i);
      47       73362 :       if (A < av && A >= bot) coeff(x,u,i) += dec;
      48             :     }
      49          66 : }
      50             : 
      51             : static void
      52          66 : gen_gerepile_gauss_ker(GEN x, long k, long t, pari_sp av, void *E, GEN (*copy)(void*, GEN))
      53             : {
      54          66 :   pari_sp tetpil = avma;
      55          66 :   long u,i, n = lg(x)-1, m = n? nbrows(x): 0;
      56             : 
      57          66 :   if (DEBUGMEM > 1) pari_warn(warnmem,"gauss_pivot_ker. k=%ld, n=%ld",k,n);
      58          66 :   for (u=t+1; u<=m; u++) gcoeff(x,u,k) = copy(E,gcoeff(x,u,k));
      59        1917 :   for (i=k+1; i<=n; i++)
      60        1851 :     for (u=1; u<=m; u++) gcoeff(x,u,i) = copy(E,gcoeff(x,u,i));
      61          66 :   gerepile_mat(av,tetpil,x,k,m,n,t);
      62          66 : }
      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          10 : gerepile_gauss(GEN x,long k,long t,pari_sp av, long j, GEN c)
      85             : {
      86          10 :   pari_sp tetpil = avma, A, bot;
      87          10 :   long u,i, n = lg(x)-1, m = n? nbrows(x): 0;
      88             :   size_t dec;
      89             : 
      90          10 :   if (DEBUGMEM > 1) pari_warn(warnmem,"gauss_pivot. k=%ld, n=%ld",k,n);
      91         375 :   for (u=t+1; u<=m; u++)
      92         365 :     if (u==j || !c[u]) COPY(gcoeff(x,u,k));
      93         710 :   for (u=1; u<=m; u++)
      94         700 :     if (u==j || !c[u])
      95         690 :       for (i=k+1; i<=n; i++) COPY(gcoeff(x,u,i));
      96             : 
      97          10 :   (void)gerepile(av,tetpil,NULL); dec = av-tetpil;
      98          10 :   bot = pari_mainstack->bot;
      99         375 :   for (u=t+1; u<=m; u++)
     100         365 :     if (u==j || !c[u])
     101             :     {
     102         365 :       A=(pari_sp)coeff(x,u,k);
     103         365 :       if (A<av && A>=bot) coeff(x,u,k)+=dec;
     104             :     }
     105         710 :   for (u=1; u<=m; u++)
     106         700 :     if (u==j || !c[u])
     107       47610 :       for (i=k+1; i<=n; i++)
     108             :       {
     109       46920 :         A=(pari_sp)coeff(x,u,i);
     110       46920 :         if (A<av && A>=bot) coeff(x,u,i)+=dec;
     111             :       }
     112          10 : }
     113             : 
     114             : /*******************************************************************/
     115             : /*                                                                 */
     116             : /*                         GENERIC                                 */
     117             : /*                                                                 */
     118             : /*******************************************************************/
     119             : GEN
     120        3351 : gen_ker(GEN x, long deplin, void *E, const struct bb_field *ff)
     121             : {
     122        3351 :   pari_sp av0 = avma, av, tetpil;
     123             :   GEN y, c, d;
     124             :   long i, j, k, r, t, n, m;
     125             : 
     126        3351 :   n=lg(x)-1; if (!n) return cgetg(1,t_MAT);
     127        3351 :   m=nbrows(x); r=0;
     128        3351 :   x = RgM_shallowcopy(x);
     129        3351 :   c = zero_zv(m);
     130        3351 :   d=new_chunk(n+1);
     131        3351 :   av=avma;
     132       25740 :   for (k=1; k<=n; k++)
     133             :   {
     134      214573 :     for (j=1; j<=m; j++)
     135      206022 :       if (!c[j])
     136             :       {
     137      129763 :         gcoeff(x,j,k) = ff->red(E, gcoeff(x,j,k));
     138      129763 :         if (!ff->equal0(gcoeff(x,j,k))) break;
     139             :       }
     140       22459 :     if (j>m)
     141             :     {
     142        8551 :       if (deplin)
     143             :       {
     144          70 :         GEN c = cgetg(n+1, t_COL), g0 = ff->s(E,0), g1=ff->s(E,1);
     145          70 :         for (i=1; i<k; i++) gel(c,i) = ff->red(E, gcoeff(x,d[i],k));
     146          70 :         gel(c,k) = g1; for (i=k+1; i<=n; i++) gel(c,i) = g0;
     147          70 :         return gerepileupto(av0, c);
     148             :       }
     149        8481 :       r++; d[k]=0;
     150       79222 :       for(j=1; j<k; j++)
     151       70741 :         if (d[j]) gcoeff(x,d[j],k) = gclone(gcoeff(x,d[j],k));
     152             :     }
     153             :     else
     154             :     {
     155       13908 :       GEN piv = ff->neg(E,ff->inv(E,gcoeff(x,j,k)));
     156       13908 :       c[j] = k; d[k] = j;
     157       13908 :       gcoeff(x,j,k) = ff->s(E,-1);
     158       13908 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = ff->red(E,ff->mul(E,piv,gcoeff(x,j,i)));
     159      225941 :       for (t=1; t<=m; t++)
     160             :       {
     161      212033 :         if (t==j) continue;
     162             : 
     163      198125 :         piv = ff->red(E,gcoeff(x,t,k));
     164      198125 :         if (ff->equal0(piv)) continue;
     165             : 
     166      147320 :         gcoeff(x,t,k) = ff->s(E,0);
     167     1245785 :         for (i=k+1; i<=n; i++)
     168     1098465 :            gcoeff(x,t,i) = ff->add(E, gcoeff(x,t,i), ff->mul(E,piv,gcoeff(x,j,i)));
     169      147320 :         if (gc_needed(av,1))
     170          66 :           gen_gerepile_gauss_ker(x,k,t,av,E,ff->red);
     171             :       }
     172             :     }
     173             :   }
     174        3281 :   if (deplin) { avma = av0; return NULL; }
     175             : 
     176        3232 :   tetpil=avma; y=cgetg(r+1,t_MAT);
     177       11713 :   for (j=k=1; j<=r; j++,k++)
     178             :   {
     179        8481 :     GEN C = cgetg(n+1,t_COL);
     180        8481 :     GEN g0 = ff->s(E,0), g1 = ff->s(E,1);
     181        8481 :     gel(y,j) = C; while (d[k]) k++;
     182       79222 :     for (i=1; i<k; i++)
     183       70741 :       if (d[i])
     184             :       {
     185       36321 :         GEN p1=gcoeff(x,d[i],k);
     186       36321 :         gel(C,i) = ff->red(E,p1); gunclone(p1);
     187             :       }
     188             :       else
     189       34420 :         gel(C,i) = g0;
     190        8481 :     gel(C,k) = g1; for (i=k+1; i<=n; i++) gel(C,i) = g0;
     191             :   }
     192        3232 :   return gerepile(av0,tetpil,y);
     193             : }
     194             : 
     195             : GEN
     196        2215 : gen_Gauss_pivot(GEN x, long *rr, void *E, const struct bb_field *ff)
     197             : {
     198             :   pari_sp av;
     199             :   GEN c, d;
     200        2215 :   long i, j, k, r, t, m, n = lg(x)-1;
     201             : 
     202        2215 :   if (!n) { *rr = 0; return NULL; }
     203             : 
     204        2215 :   m=nbrows(x); r=0;
     205        2215 :   d = cgetg(n+1, t_VECSMALL);
     206        2215 :   x = RgM_shallowcopy(x);
     207        2215 :   c = zero_zv(m);
     208        2215 :   av=avma;
     209       12867 :   for (k=1; k<=n; k++)
     210             :   {
     211      100580 :     for (j=1; j<=m; j++)
     212       98416 :       if (!c[j])
     213             :       {
     214       66714 :         gcoeff(x,j,k) = ff->red(E,gcoeff(x,j,k));
     215       66714 :         if (!ff->equal0(gcoeff(x,j,k))) break;
     216             :       }
     217       10652 :     if (j>m) { r++; d[k]=0; }
     218             :     else
     219             :     {
     220        8488 :       GEN piv = ff->neg(E,ff->inv(E,gcoeff(x,j,k)));
     221        8488 :       GEN g0 = ff->s(E,0);
     222        8488 :       c[j] = k; d[k] = j;
     223        8488 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = ff->red(E,ff->mul(E,piv,gcoeff(x,j,i)));
     224       93651 :       for (t=1; t<=m; t++)
     225             :       {
     226       85163 :         if (c[t]) continue; /* already a pivot on that line */
     227             : 
     228       55405 :         piv = ff->red(E,gcoeff(x,t,k));
     229       55405 :         if (ff->equal0(piv)) continue;
     230       29393 :         gcoeff(x,t,k) = g0;
     231      320012 :         for (i=k+1; i<=n; i++)
     232      290619 :           gcoeff(x,t,i) = ff->add(E,gcoeff(x,t,i), ff->mul(E,piv,gcoeff(x,j,i)));
     233       29393 :         if (gc_needed(av,1))
     234          10 :           gerepile_gauss(x,k,t,av,j,c);
     235             :       }
     236        8488 :       for (i=k; i<=n; i++) gcoeff(x,j,i) = g0; /* dummy */
     237             :     }
     238             :   }
     239        2215 :   *rr = r; avma = (pari_sp)d; return d;
     240             : }
     241             : 
     242             : GEN
     243         413 : gen_det(GEN a, void *E, const struct bb_field *ff)
     244             : {
     245         413 :   pari_sp av = avma;
     246         413 :   long i,j,k, s = 1, nbco = lg(a)-1;
     247         413 :   GEN q, x = ff->s(E,1);
     248         413 :   if (!nbco) return x;
     249         406 :   a = RgM_shallowcopy(a);
     250        3192 :   for (i=1; i<nbco; i++)
     251             :   {
     252        6034 :     for(k=i; k<=nbco; k++)
     253             :     {
     254        5999 :       gcoeff(a,k,i) = ff->red(E,gcoeff(a,k,i));
     255        5999 :       if (!ff->equal0(gcoeff(a,k,i))) break;
     256             :     }
     257        2821 :     if (k > nbco) return gerepileupto(av, gcoeff(a,i,i));
     258        2786 :     if (k != i)
     259             :     { /* exchange the lines s.t. k = i */
     260         665 :       for (j=i; j<=nbco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
     261         665 :       s = -s;
     262             :     }
     263        2786 :     q = gcoeff(a,i,i);
     264             : 
     265        2786 :     x = ff->red(E,ff->mul(E,x,q));
     266        2786 :     q = ff->inv(E,q);
     267       24717 :     for (k=i+1; k<=nbco; k++)
     268             :     {
     269       21931 :       GEN m = ff->red(E,gcoeff(a,i,k));
     270       21931 :       if (ff->equal0(m)) continue;
     271             : 
     272       14903 :       m = ff->neg(E, ff->mul(E,m, q));
     273      206920 :       for (j=i+1; j<=nbco; j++)
     274             :       {
     275      192017 :         gcoeff(a,j,k) = ff->add(E, gcoeff(a,j,k), ff->mul(E,m,gcoeff(a,j,i)));
     276      192017 :         if (gc_needed(av,1))
     277             :         {
     278         823 :           if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
     279         823 :           gerepileall(av,4, &a,&x,&q,&m);
     280             :         }
     281             :       }
     282             :     }
     283             :   }
     284         371 :   if (s < 0) x = ff->neg(E,x);
     285         371 :   return gerepileupto(av, ff->red(E,ff->mul(E, x, gcoeff(a,nbco,nbco))));
     286             : }
     287             : 
     288             : INLINE void
     289      878509 : _gen_addmul(GEN b, long k, long i, GEN m, void *E, const struct bb_field *ff)
     290             : {
     291      878509 :   gel(b,i) = ff->red(E,gel(b,i));
     292      878509 :   gel(b,k) = ff->add(E,gel(b,k), ff->mul(E,m, gel(b,i)));
     293      878509 : }
     294             : 
     295             : static GEN
     296       56845 : _gen_get_col(GEN a, GEN b, long li, void *E, const struct bb_field *ff)
     297             : {
     298       56845 :   GEN u = cgetg(li+1,t_COL);
     299       56845 :   pari_sp av = avma;
     300             :   long i, j;
     301             : 
     302       56845 :   gel(u,li) = gerepileupto(av, ff->red(E,ff->mul(E,gel(b,li), gcoeff(a,li,li))));
     303      313998 :   for (i=li-1; i>0; i--)
     304             :   {
     305      257153 :     pari_sp av = avma;
     306      257153 :     GEN m = gel(b,i);
     307      257153 :     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))));
     308      257153 :     m = ff->red(E, m);
     309      257153 :     gel(u,i) = gerepileupto(av, ff->red(E,ff->mul(E,m, gcoeff(a,i,i))));
     310             :   }
     311       56845 :   return u;
     312             : }
     313             : 
     314             : GEN
     315       12515 : gen_Gauss(GEN a, GEN b, void *E, const struct bb_field *ff)
     316             : {
     317             :   long i, j, k, li, bco, aco;
     318       12515 :   GEN u, g0 = ff->s(E,0);
     319       12515 :   pari_sp av = avma;
     320       12515 :   a = RgM_shallowcopy(a);
     321       12515 :   b = RgM_shallowcopy(b);
     322       12515 :   aco = lg(a)-1; bco = lg(b)-1; li = nbrows(a);
     323       55970 :   for (i=1; i<=aco; i++)
     324             :   {
     325             :     GEN invpiv;
     326       72979 :     for (k = i; k <= li; k++)
     327             :     {
     328       72881 :       GEN piv = ff->red(E,gcoeff(a,k,i));
     329       72881 :       if (!ff->equal0(piv)) { gcoeff(a,k,i) = ff->inv(E,piv); break; }
     330       17009 :       gcoeff(a,k,i) = g0;
     331             :     }
     332             :     /* found a pivot on line k */
     333       55970 :     if (k > li) return NULL;
     334       55872 :     if (k != i)
     335             :     { /* swap lines so that k = i */
     336        9154 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
     337        9154 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
     338             :     }
     339       55872 :     if (i == aco) break;
     340             : 
     341       43455 :     invpiv = gcoeff(a,i,i); /* 1/piv mod p */
     342      182136 :     for (k=i+1; k<=li; k++)
     343             :     {
     344      138681 :       GEN m = ff->red(E,gcoeff(a,k,i)); gcoeff(a,k,i) = g0;
     345      138681 :       if (ff->equal0(m)) continue;
     346             : 
     347       43074 :       m = ff->red(E,ff->neg(E,ff->mul(E,m, invpiv)));
     348       43074 :       for (j=i+1; j<=aco; j++) _gen_addmul(gel(a,j),k,i,m,E,ff);
     349       43074 :       for (j=1  ; j<=bco; j++) _gen_addmul(gel(b,j),k,i,m,E,ff);
     350             :     }
     351       43455 :     if (gc_needed(av,1))
     352             :     {
     353          38 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Gauss. i=%ld",i);
     354          38 :       gerepileall(av,2, &a,&b);
     355             :     }
     356             :   }
     357             : 
     358       12417 :   if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
     359       12417 :   u = cgetg(bco+1,t_MAT);
     360       12417 :   for (j=1; j<=bco; j++) gel(u,j) = _gen_get_col(a, gel(b,j), aco, E, ff);
     361       12417 :   return u;
     362             : }
     363             : 
     364             : /* compatible t_MAT * t_COL, lgA = lg(A) = lg(B) > 1, l = lgcols(A) */
     365             : static GEN
     366      311115 : gen_matcolmul_i(GEN A, GEN B, ulong lgA, ulong l,
     367             :                 void *E, const struct bb_field *ff)
     368             : {
     369      311115 :   GEN C = cgetg(l, t_COL);
     370             :   ulong i;
     371     2089073 :   for (i = 1; i < l; i++) {
     372     1777958 :     pari_sp av = avma;
     373     1777958 :     GEN e = ff->mul(E, gcoeff(A, i, 1), gel(B, 1));
     374             :     ulong k;
     375    10469088 :     for(k = 2; k < lgA; k++)
     376     8691130 :       e = ff->add(E, e, ff->mul(E, gcoeff(A, i, k), gel(B, k)));
     377     1777958 :     gel(C, i) = gerepileupto(av, ff->red(E, e));
     378             :   }
     379      311115 :   return C;
     380             : }
     381             : 
     382             : GEN
     383      161336 : gen_matcolmul(GEN A, GEN B, void *E, const struct bb_field *ff)
     384             : {
     385      161336 :   ulong lgA = lg(A);
     386      161336 :   if (lgA != (ulong)lg(B))
     387           0 :     pari_err_OP("operation 'gen_matcolmul'", A, B);
     388      161336 :   if (lgA == 1)
     389           0 :     return cgetg(1, t_COL);
     390      161336 :   return gen_matcolmul_i(A, B, lgA, lgcols(A), E, ff);
     391             : }
     392             : 
     393             : GEN
     394       15155 : gen_matmul(GEN A, GEN B, void *E, const struct bb_field *ff)
     395             : {
     396       15155 :   ulong j, l, lgA, lgB = lg(B);
     397             :   GEN C;
     398       15155 :   if (lgB == 1)
     399           0 :     return cgetg(1, t_MAT);
     400       15155 :   lgA = lg(A);
     401       15155 :   if (lgA != (ulong)lgcols(B))
     402           0 :     pari_err_OP("operation 'gen_matmul'", A, B);
     403       15155 :   if (lgA == 1)
     404           0 :     return zeromat(0, lgB - 1);
     405       15155 :   l = lgcols(A);
     406       15155 :   C = cgetg(lgB, t_MAT);
     407      164934 :   for(j = 1; j < lgB; j++)
     408      149779 :     gel(C, j) = gen_matcolmul_i(A, gel(B, j), lgA, l, E, ff);
     409       15155 :   return C;
     410             : }
     411             : 
     412             : static GEN
     413         469 : gen_colneg(GEN A, void *E, const struct bb_field *ff)
     414             : {
     415             :   long i, l;
     416         469 :   GEN B = cgetg_copy(A, &l);
     417        7910 :   for (i = 1; i < l; i++)
     418        7441 :     gel(B, i) = ff->neg(E, gel(A, i));
     419         469 :   return B;
     420             : }
     421             : 
     422             : static GEN
     423          84 : gen_matneg(GEN A, void *E, const struct bb_field *ff)
     424             : {
     425             :   long i, l;
     426          84 :   GEN B = cgetg_copy(A, &l);
     427         553 :   for (i = 1; i < l; i++)
     428         469 :     gel(B, i) = gen_colneg(gel(A, i), E, ff);
     429          84 :   return B;
     430             : }
     431             : 
     432             : /* assume dim A >= 1, A invertible + upper triangular  */
     433             : static GEN
     434         147 : gen_matinv_upper_ind(GEN A, long index, void *E, const struct bb_field *ff)
     435             : {
     436         147 :   long n = lg(A) - 1, i, j;
     437         147 :   GEN u = cgetg(n + 1, t_COL);
     438         672 :   for (i = n; i > index; i--)
     439         525 :     gel(u, i) = ff->s(E, 0);
     440         147 :   gel(u, i) = ff->inv(E, gcoeff(A, i, i));
     441         672 :   for (i--; i > 0; i--) {
     442         525 :     pari_sp av = avma;
     443         525 :     GEN m = ff->neg(E, ff->mul(E, gcoeff(A, i, i + 1), gel(u, i + 1)));
     444        3647 :     for (j = i + 2; j <= n; j++)
     445        3122 :       m = ff->add(E, m, ff->neg(E, ff->mul(E, gcoeff(A, i, j), gel(u, j))));
     446         525 :     gel(u, i) = gerepileupto(av, ff->red(E, ff->mul(E, m, ff->inv(E, gcoeff(A, i, i)))));
     447             :   }
     448         147 :   return u;
     449             : }
     450             : 
     451             : static GEN
     452          28 : gen_matinv_upper(GEN A, void *E, const struct bb_field *ff)
     453             : {
     454             :   long i, l;
     455          28 :   GEN B = cgetg_copy(A, &l);
     456         175 :   for (i = 1; i < l; i++)
     457         147 :     gel(B,i) = gen_matinv_upper_ind(A, i, E, ff);
     458          28 :   return B;
     459             : }
     460             : 
     461             : /* find z such that A z = y. Return NULL if no solution */
     462             : GEN
     463          63 : gen_matcolinvimage(GEN A, GEN y, void *E, const struct bb_field *ff)
     464             : {
     465          63 :   pari_sp av = avma;
     466          63 :   long i, l = lg(A);
     467             :   GEN M, x, t;
     468             : 
     469          63 :   M = gen_ker(shallowconcat(A, y), 0, E, ff);
     470          63 :   i = lg(M) - 1;
     471          63 :   if (!i) { avma = av; return NULL; }
     472             : 
     473          63 :   x = gel(M, i);
     474          63 :   t = gel(x, l);
     475          63 :   if (ff->equal0(t)) { avma = av; return NULL; }
     476             : 
     477          42 :   t = ff->neg(E, ff->inv(E, t));
     478          42 :   setlg(x, l);
     479         147 :   for (i = 1; i < l; i++)
     480         105 :     gel(x, i) = ff->red(E, ff->mul(E, t, gel(x, i)));
     481          42 :   return gerepilecopy(av, x);
     482             : }
     483             : 
     484             : /* find Z such that A Z = B. Return NULL if no solution */
     485             : GEN
     486          84 : gen_matinvimage(GEN A, GEN B, void *E, const struct bb_field *ff)
     487             : {
     488          84 :   pari_sp av = avma;
     489             :   GEN d, x, X, Y;
     490             :   long i, j, nY, nA, nB;
     491          84 :   x = gen_ker(shallowconcat(gen_matneg(A, E, ff), B), 0, E, ff);
     492             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
     493             :    * We must find T such that Y T = Id_nB then X T = Z. This exists
     494             :    * iff Y has at least nB columns and full rank. */
     495          84 :   nY = lg(x) - 1;
     496          84 :   nB = lg(B) - 1;
     497          84 :   if (nY < nB) { avma = av; return NULL; }
     498          77 :   nA = lg(A) - 1;
     499          77 :   Y = rowslice(x, nA + 1, nA + nB); /* nB rows */
     500          77 :   d = cgetg(nB + 1, t_VECSMALL);
     501         329 :   for (i = nB, j = nY; i >= 1; i--, j--) {
     502         434 :     for (; j >= 1; j--)
     503         385 :       if (!ff->equal0(gcoeff(Y, i, j))) { d[i] = j; break; }
     504         301 :     if (!j) { avma = av; return NULL; }
     505             :   }
     506             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
     507          28 :   Y = vecpermute(Y, d);
     508          28 :   x = vecpermute(x, d);
     509          28 :   X = rowslice(x, 1, nA);
     510          28 :   return gerepileupto(av, gen_matmul(X, gen_matinv_upper(Y, E, ff), E, ff));
     511             : }
     512             : 
     513             : static GEN
     514       76683 : image_from_pivot(GEN x, GEN d, long r)
     515             : {
     516             :   GEN y;
     517             :   long j, k;
     518             : 
     519       76683 :   if (!d) return gcopy(x);
     520             :   /* d left on stack for efficiency */
     521       74772 :   r = lg(x)-1 - r; /* = dim Im(x) */
     522       74772 :   y = cgetg(r+1,t_MAT);
     523      683044 :   for (j=k=1; j<=r; k++)
     524      608272 :     if (d[k]) gel(y,j++) = gcopy(gel(x,k));
     525       74772 :   return y;
     526             : }
     527             : 
     528             : /*******************************************************************/
     529             : /*                                                                 */
     530             : /*                Echelon form and CUP decomposition               */
     531             : /*                                                                 */
     532             : /*******************************************************************/
     533             : 
     534             : /* By Peter Bruin, based on
     535             :   C.-P. Jeannerod, C. Pernet and A. Storjohann, Rank-profile revealing
     536             :   Gaussian elimination and the CUP matrix decomposition.  J. Symbolic
     537             :   Comput. 56 (2013), 46-68.
     538             : 
     539             :   Decompose an m x n-matrix A of rank r as C*U*P, with
     540             :   - C: m x r-matrix in column echelon form (not necessarily reduced)
     541             :        with all pivots equal to 1
     542             :   - U: upper-triangular r x n-matrix
     543             :   - P: permutation matrix
     544             :   The pivots of C and the known zeroes in C and U are not necessarily
     545             :   filled in; instead, we also return the vector R of pivot rows.
     546             :   Instead of the matrix P, we return the permutation p of [1..n]
     547             :   (t_VECSMALL) such that P[i,j] = 1 if and only if j = p[i].
     548             : */
     549             : 
     550             : /* complement of a strictly increasing subsequence of (1, 2, ..., n) */
     551             : static GEN
     552      406972 : indexcompl(GEN v, long n)
     553             : {
     554      406972 :   long i, j, k, m = lg(v) - 1;
     555      406972 :   GEN w = cgetg(n - m + 1, t_VECSMALL);
     556     7745177 :   for (i = j = k = 1; i <= n; i++)
     557     7338205 :     if (j <= m && v[j] == i) j++; else w[k++] = i;
     558      406972 :   return w;
     559             : }
     560             : 
     561             : static GEN
     562      237161 : Flm_rsolve_upper_1(GEN U, GEN B, ulong p)
     563      237161 : { return Flm_Fl_mul(B, Fl_inv(ucoeff(U, 1, 1), p), p); }
     564             : 
     565             : static GEN
     566      721690 : Flm_rsolve_upper_2(GEN U, GEN B, ulong p)
     567             : {
     568      721690 :   ulong a = ucoeff(U, 1, 1), b = ucoeff(U, 1, 2), d = ucoeff(U, 2, 2);
     569      721690 :   ulong D = Fl_mul(a, d, p), Dinv = Fl_inv(D, p);
     570      721724 :   ulong ainv = Fl_mul(d, Dinv, p), dinv = Fl_mul(a, Dinv, p);
     571      721720 :   GEN B1 = rowslice(B, 1, 1);
     572      721678 :   GEN B2 = rowslice(B, 2, 2);
     573      721690 :   GEN X2 = Flm_Fl_mul(B2, dinv, p);
     574      721657 :   GEN X1 = Flm_Fl_mul(Flm_sub(B1, Flm_Fl_mul(X2, b, p), p),
     575             :                       ainv, p);
     576      721654 :   return vconcat(X1, X2);
     577             : }
     578             : 
     579             : /* solve U*X = B,  U upper triangular and invertible */
     580             : static GEN
     581     1733837 : Flm_rsolve_upper(GEN U, GEN B, ulong p)
     582             : {
     583     1733837 :   long n = lg(U) - 1, n1;
     584             :   GEN U2, U11, U12, U22, B1, B2, X1, X2, X;
     585     1733837 :   pari_sp av = avma;
     586             : 
     587     1733837 :   if (n == 0) return B;
     588     1733837 :   if (n == 1) return Flm_rsolve_upper_1(U, B, p);
     589     1496677 :   if (n == 2) return Flm_rsolve_upper_2(U, B, p);
     590      774990 :   n1 = (n + 1)/2;
     591      774990 :   U2 = vecslice(U, n1 + 1, n);
     592      774983 :   U22 = rowslice(U2, n1 + 1, n);
     593      774960 :   B2 = rowslice(B, n1 + 1, n);
     594      774969 :   X2 = Flm_rsolve_upper(U22, B2, p);
     595      774999 :   U12 = rowslice(U2, 1, n1);
     596      774984 :   B1 = rowslice(B, 1, n1);
     597      774968 :   B1 = Flm_sub(B1, Flm_mul(U12, X2, p), p);
     598      774972 :   if (gc_needed(av, 1)) gerepileall(av, 2, &B1, &X2);
     599      774972 :   U11 = matslice(U, 1,n1, 1,n1);
     600      774962 :   X1 = Flm_rsolve_upper(U11, B1, p);
     601      775017 :   X = vconcat(X1, X2);
     602      775027 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     603      775028 :   return X;
     604             : }
     605             : 
     606             : static GEN
     607      273354 : Flm_lsolve_upper_1(GEN U, GEN B, ulong p)
     608      273354 : { return Flm_Fl_mul(B, Fl_inv(ucoeff(U, 1, 1), p), p); }
     609             : 
     610             : static GEN
     611      803334 : Flm_lsolve_upper_2(GEN U, GEN B, ulong p)
     612             : {
     613      803334 :   ulong a = ucoeff(U, 1, 1), b = ucoeff(U, 1, 2), d = ucoeff(U, 2, 2);
     614      803334 :   ulong D = Fl_mul(a, d, p), Dinv = Fl_inv(D, p);
     615      803375 :   ulong ainv = Fl_mul(d, Dinv, p), dinv = Fl_mul(a, Dinv, p);
     616      803363 :   GEN B1 = vecslice(B, 1, 1);
     617      803337 :   GEN B2 = vecslice(B, 2, 2);
     618      803300 :   GEN X1 = Flm_Fl_mul(B1, ainv, p);
     619      803329 :   GEN X2 = Flm_Fl_mul(Flm_sub(B2, Flm_Fl_mul(X1, b, p), p),
     620             :                       dinv, p);
     621      803337 :   return shallowconcat(X1, X2);
     622             : }
     623             : 
     624             : /* solve X*U = B,  U upper triangular and invertible */
     625             : static GEN
     626     1824429 : Flm_lsolve_upper(GEN U, GEN B, ulong p)
     627             : {
     628     1824429 :   long n = lg(U) - 1, n1;
     629             :   GEN U2, U11, U12, U22, B1, B2, X1, X2, X;
     630     1824429 :   pari_sp av = avma;
     631             : 
     632     1824429 :   if (n == 0) return B;
     633     1824429 :   if (n == 1) return Flm_lsolve_upper_1(U, B, p);
     634     1551075 :   if (n == 2) return Flm_lsolve_upper_2(U, B, p);
     635      747737 :   n1 = (n + 1)/2;
     636      747737 :   U2 = vecslice(U, n1 + 1, n);
     637      747731 :   U11 = matslice(U, 1,n1, 1,n1);
     638      747716 :   U12 = rowslice(U2, 1, n1);
     639      747715 :   U22 = rowslice(U2, n1 + 1, n);
     640      747713 :   B1 = vecslice(B, 1, n1);
     641      747714 :   B2 = vecslice(B, n1 + 1, n);
     642      747711 :   X1 = Flm_lsolve_upper(U11, B1, p);
     643      747726 :   B2 = Flm_sub(B2, Flm_mul(X1, U12, p), p);
     644      747723 :   if (gc_needed(av, 1)) gerepileall(av, 3, &B2, &U22, &X1);
     645      747723 :   X2 = Flm_lsolve_upper(U22, B2, p);
     646      747737 :   X = shallowconcat(X1, X2);
     647      747745 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     648      747746 :   return X;
     649             : }
     650             : 
     651             : static GEN
     652     1379276 : Flm_rsolve_lower_unit_2(GEN L, GEN A, ulong p)
     653             : {
     654     1379276 :   GEN X1 = rowslice(A, 1, 1);
     655     1379255 :   GEN X2 = Flm_sub(rowslice(A, 2, 2),
     656     1379255 :                    Flm_Fl_mul(X1, ucoeff(L, 2, 1), p), p);
     657     1379257 :   return vconcat(X1, X2);
     658             : }
     659             : 
     660             : /* solve L*X = A,  L lower triangular with ones on the diagonal
     661             : * (at least as many rows as columns) */
     662             : static GEN
     663     3167520 : Flm_rsolve_lower_unit(GEN L, GEN A, ulong p)
     664             : {
     665     3167520 :   long m = lg(L) - 1, m1, n;
     666             :   GEN L1, L11, L21, L22, A1, A2, X1, X2, X;
     667     3167520 :   pari_sp av = avma;
     668             : 
     669     3167520 :   if (m == 0) return zero_Flm(0, lg(A) - 1);
     670     3167520 :   if (m == 1) return rowslice(A, 1, 1);
     671     2742993 :   if (m == 2) return Flm_rsolve_lower_unit_2(L, A, p);
     672     1363718 :   m1 = (m + 1)/2;
     673     1363718 :   n = nbrows(L);
     674     1363719 :   L1 = vecslice(L, 1, m1);
     675     1363714 :   L11 = rowslice(L1, 1, m1);
     676     1363692 :   L21 = rowslice(L1, m1 + 1, n);
     677     1363697 :   A1 = rowslice(A, 1, m1);
     678     1363694 :   X1 = Flm_rsolve_lower_unit(L11, A1, p);
     679     1363739 :   A2 = rowslice(A, m1 + 1, n);
     680     1363675 :   A2 = Flm_sub(A2, Flm_mul(L21, X1, p), p);
     681     1363689 :   if (gc_needed(av, 1)) gerepileall(av, 2, &A2, &X1);
     682     1363689 :   L22 = matslice(L, m1+1,n, m1+1,m);
     683     1363679 :   X2 = Flm_rsolve_lower_unit(L22, A2, p);
     684     1363725 :   X = vconcat(X1, X2);
     685     1363750 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     686     1363752 :   return X;
     687             : }
     688             : 
     689             : static GEN
     690      731882 : Flm_lsolve_lower_unit_2(GEN L, GEN A, ulong p)
     691             : {
     692      731882 :   GEN X2 = vecslice(A, 2, 2);
     693      731882 :   GEN X1 = Flm_sub(vecslice(A, 1, 1),
     694      731882 :                    Flm_Fl_mul(X2, ucoeff(L, 2, 1), p), p);
     695      731882 :   return shallowconcat(X1, X2);
     696             : }
     697             : 
     698             : /* solve L*X = A,  L square lower triangular with ones on the diagonal */
     699             : static GEN
     700     1871606 : Flm_lsolve_lower_unit(GEN L, GEN A, ulong p)
     701             : {
     702     1871606 :   long m = lg(L) - 1, m1;
     703             :   GEN L1, L2, L11, L21, L22, A1, A2, X1, X2, X;
     704     1871606 :   pari_sp av = avma;
     705             : 
     706     1871606 :   if (m <= 1) return A;
     707     1595641 :   if (m == 2) return Flm_lsolve_lower_unit_2(L, A, p);
     708      863759 :   m1 = (m + 1)/2;
     709      863759 :   L2 = vecslice(L, m1 + 1, m);
     710      863759 :   L22 = rowslice(L2, m1 + 1, m);
     711      863759 :   A2 = vecslice(A, m1 + 1, m);
     712      863759 :   X2 = Flm_lsolve_lower_unit(L22, A2, p);
     713      863759 :   if (gc_needed(av, 1)) X2 = gerepilecopy(av, X2);
     714      863759 :   L1 = vecslice(L, 1, m1);
     715      863759 :   L21 = rowslice(L1, m1 + 1, m);
     716      863759 :   A1 = vecslice(A, 1, m1);
     717      863759 :   A1 = Flm_sub(A1, Flm_mul(X2, L21, p), p);
     718      863759 :   L11 = rowslice(L1, 1, m1);
     719      863759 :   if (gc_needed(av, 1)) gerepileall(av, 3, &A1, &L11, &X2);
     720      863759 :   X1 = Flm_lsolve_lower_unit(L11, A1, p);
     721      863759 :   X = shallowconcat(X1, X2);
     722      863759 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     723      863759 :   return X;
     724             : }
     725             : 
     726             : /* destroy A */
     727             : static long
     728     1059577 : Flm_CUP_gauss(GEN A, GEN *R, GEN *C, GEN *U, GEN *P, ulong p)
     729             : {
     730     1059577 :   long i, j, k, m = nbrows(A), n = lg(A) - 1, pr, pc, u, v;
     731             : 
     732     1059574 :   if (P) *P = identity_perm(n);
     733     1059567 :   *R = cgetg(m + 1, t_VECSMALL);
     734     5569569 :   for (j = 1, pr = 0; j <= n; j++)
     735             :   {
     736     7199105 :     for (pr++, pc = 0; pr <= m; pr++)
     737             :     {
     738     6703835 :       for (k = j; k <= n; k++) { v = ucoeff(A, pr, k); if (!pc && v) pc = k; }
     739     6703835 :       if (pc) break;
     740             :     }
     741     5005197 :     if (!pc) break;
     742     4509887 :     (*R)[j] = pr;
     743     4509887 :     if (pc != j)
     744             :     {
     745     1039498 :       swap(gel(A, j), gel(A, pc));
     746     1039498 :       if (P) lswap((*P)[j], (*P)[pc]);
     747             :     }
     748     4509887 :     u = Fl_inv(ucoeff(A, pr, j), p);
     749    30020054 :     for (i = pr + 1; i <= m; i++)
     750             :     {
     751    25510046 :       v = Fl_mul(ucoeff(A, i, j), u, p);
     752    25509892 :       ucoeff(A, i, j) = v;
     753   129126203 :       for (k = j + 1; k <= n; k++)
     754   207233476 :         ucoeff(A, i, k) = Fl_sub(ucoeff(A, i, k),
     755   103616738 :                                  Fl_mul(ucoeff(A, pr, k), v, p), p);
     756             :     }
     757             :   }
     758     1059682 :   setlg(*R, j);
     759     1059563 :   *C = vecslice(A, 1, j - 1);
     760     1059549 :   if (U) *U = rowpermute(A, *R);
     761     1059561 :   return j - 1;
     762             : }
     763             : 
     764             : static const long Flm_CUP_LIMIT = 8;
     765             : 
     766             : static ulong
     767      958405 : Flm_CUP(GEN A, GEN *R, GEN *C, GEN *U, GEN *P, ulong p)
     768             : {
     769      958405 :   long m = nbrows(A), m1, n = lg(A) - 1, i, r1, r2, r;
     770             :   GEN R1, C1, U1, P1, R2, C2, U2, P2;
     771             :   GEN A1, A2, B2, C21, U11, U12, T21, T22;
     772      958398 :   pari_sp av = avma;
     773             : 
     774      958398 :   if (m < Flm_CUP_LIMIT || n < Flm_CUP_LIMIT)
     775             :     /* destroy A; not called at the outermost recursion level */
     776      622756 :     return Flm_CUP_gauss(A, R, C, U, P, p);
     777      335642 :   m1 = (minss(m, n) + 1)/2;
     778      335640 :   A1 = rowslice(A, 1, m1);
     779      335643 :   A2 = rowslice(A, m1 + 1, m);
     780      335644 :   r1 = Flm_CUP(A1, &R1, &C1, &U1, &P1, p);
     781      335633 :   if (r1 == 0)
     782             :   {
     783        6670 :     r2 = Flm_CUP(A2, &R2, &C2, &U2, &P2, p);
     784        6670 :     *R = cgetg(r2 + 1, t_VECSMALL);
     785        6670 :     for (i = 1; i <= r2; i++) (*R)[i] = R2[i] + m1;
     786        6670 :     *C = vconcat(zero_Flm(m1, r2), C2);
     787        6670 :     *U = U2;
     788        6670 :     *P = P2;
     789        6670 :     r = r2;
     790             :   }
     791             :   else
     792             :   {
     793      328963 :     U11 = vecslice(U1, 1, r1);
     794      328960 :     U12 = vecslice(U1, r1 + 1, n);
     795      328968 :     T21 = vecslicepermute(A2, P1, 1, r1);
     796      328967 :     T22 = vecslicepermute(A2, P1, r1 + 1, n);
     797      328971 :     C21 = Flm_lsolve_upper(U11, T21, p);
     798      328968 :     if (gc_needed(av, 1))
     799           0 :       gerepileall(av, 7, &R1, &C1, &P1, &U11, &U12, &T22, &C21);
     800      328968 :     B2 = Flm_sub(T22, Flm_mul(C21, U12, p), p);
     801      328970 :     r2 = Flm_CUP(B2, &R2, &C2, &U2, &P2, p);
     802      328963 :     r = r1 + r2;
     803      328963 :     *R = cgetg(r + 1, t_VECSMALL);
     804      328967 :     for (i = 1; i <= r1; i++) (*R)[i] = R1[i];
     805      328967 :     for (;      i <= r; i++)  (*R)[i] = R2[i - r1] + m1;
     806      328967 :     *C = shallowconcat(vconcat(C1, C21),
     807             :                        vconcat(zero_Flm(m1, r2), C2));
     808      328976 :     *U = shallowconcat(vconcat(U11, zero_Flm(r2, r1)),
     809             :                        vconcat(vecpermute(U12, P2), U2));
     810      328977 :     *P = cgetg(n + 1, t_VECSMALL);
     811      328976 :     for (i = 1; i <= r1; i++) (*P)[i] = P1[i];
     812      328976 :     for (; i <= n; i++)       (*P)[i] = P1[P2[i - r1] + r1];
     813             :   }
     814      335646 :   if (gc_needed(av, 1)) gerepileall(av, 4, R, C, U, P);
     815      335646 :   return r;
     816             : }
     817             : 
     818             : static ulong
     819      436805 : Flm_echelon_gauss(GEN A, GEN *R, GEN *C, ulong p)
     820      436805 : { return Flm_CUP_gauss(A, R, C, NULL, NULL, p); }
     821             : 
     822             : /* column echelon form */
     823             : static ulong
     824      733676 : Flm_echelon(GEN A, GEN *R, GEN *C, ulong p)
     825             : {
     826      733676 :   long j, j1, j2, m = nbrows(A), n = lg(A) - 1, n1, r, r1, r2;
     827             :   GEN A1, A2, R1, R1c, C1, R2, C2;
     828             :   GEN A12, A22, B2, C11, C21, M12;
     829      733676 :   pari_sp av = avma;
     830             : 
     831      733676 :   if (m < Flm_CUP_LIMIT || n < Flm_CUP_LIMIT)
     832      436805 :     return Flm_echelon_gauss(Flm_copy(A), R, C, p);
     833             : 
     834      296871 :   n1 = (n + 1)/2;
     835      296871 :   A1 = vecslice(A, 1, n1);
     836      296871 :   A2 = vecslice(A, n1 + 1, n);
     837      296871 :   r1 = Flm_echelon(A1, &R1, &C1, p);
     838      296871 :   if (!r1) return Flm_echelon(A2, R, C, p);
     839      260416 :   if (r1 == m) { *R = R1; *C = C1; return r1; }
     840             : 
     841      256227 :   R1c = indexcompl(R1, m);
     842      256227 :   C11 = rowpermute(C1, R1);
     843      256227 :   C21 = rowpermute(C1, R1c);
     844      256227 :   A12 = rowpermute(A2, R1);
     845      256227 :   A22 = rowpermute(A2, R1c);
     846      256227 :   M12 = Flm_rsolve_lower_unit(C11, A12, p);
     847      256227 :   B2 = Flm_sub(A22, Flm_mul(C21, M12, p), p);
     848      256227 :   r2 = Flm_echelon(B2, &R2, &C2, p);
     849      256227 :   if (!r2) { *R = R1; *C = C1; r = r1; }
     850             :   else
     851             :   {
     852      241811 :     R2 = perm_mul(R1c, R2);
     853      241811 :     C2 = rowpermute(vconcat(zero_Flm(r1, r2), C2),
     854             :                     perm_inv(vecsmall_concat(R1, R1c)));
     855      241811 :     r = r1 + r2;
     856      241811 :     *R = cgetg(r + 1, t_VECSMALL);
     857      241811 :     *C = cgetg(r + 1, t_MAT);
     858     2931496 :     for (j = j1 = j2 = 1; j <= r; j++)
     859     2689685 :       if (j2 > r2 || (j1 <= r1 && R1[j1] < R2[j2]))
     860             :       {
     861     1399769 :         gel(*C, j) = gel(C1, j1);
     862     1399769 :         (*R)[j] = R1[j1++];
     863             :       }
     864             :       else
     865             :       {
     866     1289916 :         gel(*C, j) = gel(C2, j2);
     867     1289916 :         (*R)[j] = R2[j2++];
     868             :       }
     869             :   }
     870      256227 :   if (gc_needed(av, 1)) gerepileall(av, 2, R, C);
     871      256227 :   return r;
     872             : }
     873             : 
     874             : static GEN
     875         140 : FlxqM_rsolve_upper_1(GEN U, GEN B, GEN T, ulong p)
     876         140 : { return FlxqM_Flxq_mul(B, Flxq_inv(gcoeff(U, 1, 1), T, p), T, p);
     877             : }
     878             : 
     879             : static GEN
     880         168 : FlxqM_rsolve_upper_2(GEN U, GEN B, GEN T, ulong p)
     881             : {
     882         168 :   GEN a = gcoeff(U, 1, 1), b = gcoeff(U, 1, 2), d = gcoeff(U, 2, 2);
     883         168 :   GEN D = Flxq_mul(a, d, T, p), Dinv = Flxq_inv(D, T, p);
     884         168 :   GEN ainv = Flxq_mul(d, Dinv, T, p), dinv = Flxq_mul(a, Dinv, T, p);
     885         168 :   GEN B1 = rowslice(B, 1, 1);
     886         168 :   GEN B2 = rowslice(B, 2, 2);
     887         168 :   GEN X2 = FlxqM_Flxq_mul(B2, dinv, T, p);
     888         168 :   GEN X1 = FlxqM_Flxq_mul(FlxM_sub(B1, FlxqM_Flxq_mul(X2, b, T, p), p),
     889             :                           ainv, T, p);
     890         168 :   return vconcat(X1, X2);
     891             : }
     892             : 
     893             : /* solve U*X = B,  U upper triangular and invertible */
     894             : static GEN
     895         588 : FlxqM_rsolve_upper(GEN U, GEN B, GEN T, ulong p)
     896             : {
     897         588 :   long n = lg(U) - 1, n1;
     898             :   GEN U2, U11, U12, U22, B1, B2, X1, X2, X;
     899         588 :   pari_sp av = avma;
     900             : 
     901         588 :   if (n == 0) return B;
     902         588 :   if (n == 1) return FlxqM_rsolve_upper_1(U, B, T, p);
     903         448 :   if (n == 2) return FlxqM_rsolve_upper_2(U, B, T, p);
     904         280 :   n1 = (n + 1)/2;
     905         280 :   U2 = vecslice(U, n1 + 1, n);
     906         280 :   U11 = matslice(U, 1,n1, 1,n1);
     907         280 :   U12 = rowslice(U2, 1, n1);
     908         280 :   U22 = rowslice(U2, n1 + 1, n);
     909         280 :   B1 = rowslice(B, 1, n1);
     910         280 :   B2 = rowslice(B, n1 + 1, n);
     911         280 :   X2 = FlxqM_rsolve_upper(U22, B2, T, p);
     912         280 :   B1 = FlxM_sub(B1, FlxqM_mul(U12, X2, T, p), p);
     913         280 :   if (gc_needed(av, 1)) gerepileall(av, 3, &B1, &U11, &X2);
     914         280 :   X1 = FlxqM_rsolve_upper(U11, B1, T, p);
     915         280 :   X = vconcat(X1, X2);
     916         280 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     917         280 :   return X;
     918             : }
     919             : 
     920             : static GEN
     921         469 : FlxqM_lsolve_upper_1(GEN U, GEN B, GEN T, ulong p)
     922         469 : { return FlxqM_Flxq_mul(B, Flxq_inv(gcoeff(U, 1, 1), T, p), T, p); }
     923             : 
     924             : static GEN
     925         728 : FlxqM_lsolve_upper_2(GEN U, GEN B, GEN T, ulong p)
     926             : {
     927         728 :   GEN a = gcoeff(U, 1, 1), b = gcoeff(U, 1, 2), d = gcoeff(U, 2, 2);
     928         728 :   GEN D = Flxq_mul(a, d, T, p), Dinv = Flxq_inv(D, T, p);
     929         728 :   GEN ainv = Flxq_mul(d, Dinv, T, p), dinv = Flxq_mul(a, Dinv, T, p);
     930         728 :   GEN B1 = vecslice(B, 1, 1);
     931         728 :   GEN B2 = vecslice(B, 2, 2);
     932         728 :   GEN X1 = FlxqM_Flxq_mul(B1, ainv, T, p);
     933         728 :   GEN X2 = FlxqM_Flxq_mul(FlxM_sub(B2, FlxqM_Flxq_mul(X1, b, T, p), p),
     934             :                           dinv, T, p);
     935         728 :   return shallowconcat(X1, X2);
     936             : }
     937             : 
     938             : /* solve X*U = B,  U upper triangular and invertible */
     939             : static GEN
     940        1869 : FlxqM_lsolve_upper(GEN U, GEN B, GEN T, ulong p)
     941             : {
     942        1869 :   long n = lg(U) - 1, n1;
     943             :   GEN U2, U11, U12, U22, B1, B2, X1, X2, X;
     944        1869 :   pari_sp av = avma;
     945             : 
     946        1869 :   if (n == 0) return B;
     947        1869 :   if (n == 1) return FlxqM_lsolve_upper_1(U, B, T, p);
     948        1400 :   if (n == 2) return FlxqM_lsolve_upper_2(U, B, T, p);
     949         672 :   n1 = (n + 1)/2;
     950         672 :   U2 = vecslice(U, n1 + 1, n);
     951         672 :   U11 = matslice(U, 1,n1, 1,n1);
     952         672 :   U12 = rowslice(U2, 1, n1);
     953         672 :   U22 = rowslice(U2, n1 + 1, n);
     954         672 :   B1 = vecslice(B, 1, n1);
     955         672 :   B2 = vecslice(B, n1 + 1, n);
     956         672 :   X1 = FlxqM_lsolve_upper(U11, B1, T, p);
     957         672 :   B2 = FlxM_sub(B2, FlxqM_mul(X1, U12, T, p), p);
     958         672 :   if (gc_needed(av, 1)) gerepileall(av, 3, &B2, &U22, &X1);
     959         672 :   X2 = FlxqM_lsolve_upper(U22, B2, T, p);
     960         672 :   X = shallowconcat(X1, X2);
     961         672 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     962         672 :   return X;
     963             : }
     964             : 
     965             : static GEN
     966        6069 : FlxqM_rsolve_lower_unit_2(GEN L, GEN A, GEN T, ulong p)
     967             : {
     968        6069 :   GEN X1 = rowslice(A, 1, 1);
     969        6069 :   GEN X2 = FlxM_sub(rowslice(A, 2, 2),
     970        6069 :                     FlxqM_Flxq_mul(X1, gcoeff(L, 2, 1), T, p), p);
     971        6069 :   return vconcat(X1, X2);
     972             : }
     973             : 
     974             : /* solve L*X = A,  L lower triangular with ones on the diagonal
     975             :  * (at least as many rows as columns) */
     976             : static GEN
     977       13783 : FlxqM_rsolve_lower_unit(GEN L, GEN A, GEN T, ulong p)
     978             : {
     979       13783 :   long m = lg(L) - 1, m1, n;
     980             :   GEN L1, L11, L21, L22, A1, A2, X1, X2, X;
     981       13783 :   pari_sp av = avma;
     982             : 
     983       13783 :   if (m == 0) return zeromat(0, lg(A) - 1);
     984       13783 :   if (m == 1) return rowslice(A, 1, 1);
     985       10542 :   if (m == 2) return FlxqM_rsolve_lower_unit_2(L, A, T, p);
     986        4473 :   m1 = (m + 1)/2;
     987        4473 :   n = nbrows(L);
     988        4473 :   L1 = vecslice(L, 1, m1);
     989        4473 :   L11 = rowslice(L1, 1, m1);
     990        4473 :   L21 = rowslice(L1, m1 + 1, n);
     991        4473 :   A1 = rowslice(A, 1, m1);
     992        4473 :   A2 = rowslice(A, m1 + 1, n);
     993        4473 :   X1 = FlxqM_rsolve_lower_unit(L11, A1, T, p);
     994        4473 :   A2 = FlxM_sub(A2, FlxqM_mul(L21, X1, T, p), p);
     995        4473 :   if (gc_needed(av, 1)) gerepileall(av, 2, &A2, &X1);
     996        4473 :   L22 = matslice(L, m1+1,n, m1+1,m);
     997        4473 :   X2 = FlxqM_rsolve_lower_unit(L22, A2, T, p);
     998        4473 :   X = vconcat(X1, X2);
     999        4473 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
    1000        4473 :   return X;
    1001             : }
    1002             : 
    1003             : static GEN
    1004        2478 : FlxqM_lsolve_lower_unit_2(GEN L, GEN A, GEN T, ulong p)
    1005             : {
    1006        2478 :   GEN X2 = vecslice(A, 2, 2);
    1007        2478 :   GEN X1 = FlxM_sub(vecslice(A, 1, 1),
    1008        2478 :                     FlxqM_Flxq_mul(X2, gcoeff(L, 2, 1), T, p), p);
    1009        2478 :   return shallowconcat(X1, X2);
    1010             : }
    1011             : 
    1012             : /* solve L*X = A,  L square lower triangular with ones on the diagonal */
    1013             : static GEN
    1014        6391 : FlxqM_lsolve_lower_unit(GEN L, GEN A, GEN T, ulong p)
    1015             : {
    1016        6391 :   long m = lg(L) - 1, m1;
    1017             :   GEN L1, L2, L11, L21, L22, A1, A2, X1, X2, X;
    1018        6391 :   pari_sp av = avma;
    1019             : 
    1020        6391 :   if (m <= 1) return A;
    1021        4760 :   if (m == 2) return FlxqM_lsolve_lower_unit_2(L, A, T, p);
    1022        2282 :   m1 = (m + 1)/2;
    1023        2282 :   L1 = vecslice(L, 1, m1);
    1024        2282 :   L2 = vecslice(L, m1 + 1, m);
    1025        2282 :   L11 = rowslice(L1, 1, m1);
    1026        2282 :   L21 = rowslice(L1, m1 + 1, m);
    1027        2282 :   L22 = rowslice(L2, m1 + 1, m);
    1028        2282 :   A1 = vecslice(A, 1, m1);
    1029        2282 :   A2 = vecslice(A, m1 + 1, m);
    1030        2282 :   X2 = FlxqM_lsolve_lower_unit(L22, A2, T, p);
    1031        2282 :   A1 = FlxM_sub(A1, FlxqM_mul(X2, L21, T, p), p);
    1032        2282 :   if (gc_needed(av, 1)) gerepileall(av, 3, &A1, &L11, &X2);
    1033        2282 :   X1 = FlxqM_lsolve_lower_unit(L11, A1, T, p);
    1034        2282 :   X = shallowconcat(X1, X2);
    1035        2282 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
    1036        2282 :   return X;
    1037             : }
    1038             : 
    1039             : /* destroy A */
    1040             : static long
    1041        7819 : FlxqM_CUP_gauss(GEN A, GEN *R, GEN *C, GEN *U, GEN *P, GEN T, ulong p)
    1042             : {
    1043        7819 :   long i, j, k, m = nbrows(A), n = lg(A) - 1, pr, pc;
    1044             :   pari_sp av;
    1045             :   GEN u, v;
    1046             : 
    1047        7819 :   if (P) *P = identity_perm(n);
    1048        7819 :   *R = cgetg(m + 1, t_VECSMALL);
    1049        7819 :   av = avma;
    1050       16170 :   for (j = 1, pr = 0; j <= n; j++)
    1051             :   {
    1052       42091 :     for (pr++, pc = 0; pr <= m; pr++)
    1053             :     {
    1054      167937 :       for (k = j; k <= n; k++)
    1055             :       {
    1056      132454 :         v = Flx_rem(gcoeff(A, pr, k), T, p);
    1057      132454 :         gcoeff(A, pr, k) = v;
    1058      132454 :         if (!pc && lgpol(v) > 0) pc = k;
    1059             :       }
    1060       35483 :       if (pc) break;
    1061             :     }
    1062       14959 :     if (!pc) break;
    1063        8351 :     (*R)[j] = pr;
    1064        8351 :     if (pc != j)
    1065             :     {
    1066        1141 :       swap(gel(A, j), gel(A, pc));
    1067        1141 :       if (P) lswap((*P)[j], (*P)[pc]);
    1068             :     }
    1069        8351 :     u = Flxq_inv(gcoeff(A, pr, j), T, p);
    1070       49490 :     for (i = pr + 1; i <= m; i++)
    1071             :     {
    1072       41139 :       v = Flxq_mul(gcoeff(A, i, j), u, T, p);
    1073       41139 :       gcoeff(A, i, j) = v;
    1074      112910 :       for (k = j + 1; k <= n; k++)
    1075      143542 :         gcoeff(A, i, k) = Flx_sub(gcoeff(A, i, k),
    1076       71771 :                                   Flx_mul(gcoeff(A, pr, k), v, p), p);
    1077             :     }
    1078        8351 :     if (gc_needed(av, 2)) A = gerepilecopy(av, A);
    1079             :   }
    1080        7819 :   setlg(*R, j);
    1081        7819 :   *C = vecslice(A, 1, j - 1);
    1082        7819 :   if (U) *U = rowpermute(A, *R);
    1083        7819 :   return j - 1;
    1084             : }
    1085             : 
    1086             : static const long FlxqM_CUP_LIMIT = 5;
    1087             : 
    1088             : static ulong
    1089        1253 : FlxqM_CUP(GEN A, GEN *R, GEN *C, GEN *U, GEN *P, GEN T, ulong p)
    1090             : {
    1091        1253 :   long m = nbrows(A), m1, n = lg(A) - 1, i, r1, r2, r, sv;
    1092             :   GEN R1, C1, U1, P1, R2, C2, U2, P2;
    1093             :   GEN A1, A2, B2, C21, U11, U12, T21, T22;
    1094        1253 :   pari_sp av = avma;
    1095             : 
    1096        1253 :   if (m < FlxqM_CUP_LIMIT || n < FlxqM_CUP_LIMIT)
    1097             :     /* destroy A; not called at the outermost recursion level */
    1098         672 :     return FlxqM_CUP_gauss(A, R, C, U, P, T, p);
    1099         581 :   sv = get_Flx_var(T);
    1100         581 :   m1 = (minss(m, n) + 1)/2;
    1101         581 :   A1 = rowslice(A, 1, m1);
    1102         581 :   A2 = rowslice(A, m1 + 1, m);
    1103         581 :   r1 = FlxqM_CUP(A1, &R1, &C1, &U1, &P1, T, p);
    1104         581 :   if (r1 == 0)
    1105             :   {
    1106          56 :     r2 = FlxqM_CUP(A2, &R2, &C2, &U2, &P2, T, p);
    1107          56 :     *R = cgetg(r2 + 1, t_VECSMALL);
    1108          56 :     for (i = 1; i <= r2; i++) (*R)[i] = R2[i] + m1;
    1109          56 :     *C = vconcat(zero_FlxM(m1, r2, sv), C2);
    1110          56 :     *U = U2;
    1111          56 :     *P = P2;
    1112          56 :     r = r2;
    1113             :   }
    1114             :   else
    1115             :   {
    1116         525 :     U11 = vecslice(U1, 1, r1);
    1117         525 :     U12 = vecslice(U1, r1 + 1, n);
    1118         525 :     T21 = vecslicepermute(A2, P1, 1, r1);
    1119         525 :     T22 = vecslicepermute(A2, P1, r1 + 1, n);
    1120         525 :     C21 = FlxqM_lsolve_upper(U11, T21, T, p);
    1121         525 :     if (gc_needed(av, 1))
    1122           0 :       gerepileall(av, 7, &R1, &C1, &P1, &U11, &U12, &T22, &C21);
    1123         525 :     B2 = FlxM_sub(T22, FlxqM_mul(C21, U12, T, p), p);
    1124         525 :     r2 = FlxqM_CUP(B2, &R2, &C2, &U2, &P2, T, p);
    1125         525 :     r = r1 + r2;
    1126         525 :     *R = cgetg(r + 1, t_VECSMALL);
    1127         525 :     for (i = 1; i <= r1; i++) (*R)[i] = R1[i];
    1128         525 :     for (     ; i <= r; i++)  (*R)[i] = R2[i - r1] + m1;
    1129         525 :     *C = shallowmatconcat(mkmat2(mkcol2(C1, C21),
    1130             :                                  mkcol2(zero_FlxM(m1, r2, sv), C2)));
    1131         525 :     *U = shallowmatconcat(mkmat2(mkcol2(U11, zero_FlxM(r2, r1, sv)),
    1132             :                                  mkcol2(vecpermute(U12, P2), U2)));
    1133         525 :     *P = cgetg(n + 1, t_VECSMALL);
    1134         525 :     for (i = 1; i <= r1; i++) (*P)[i] = P1[i];
    1135         525 :     for (     ; i <= n; i++)  (*P)[i] = P1[P2[i - r1] + r1];
    1136             :   }
    1137         581 :   if (gc_needed(av, 1)) gerepileall(av, 4, R, C, U, P);
    1138         581 :   return r;
    1139             : }
    1140             : 
    1141             : static ulong
    1142        7147 : FlxqM_echelon_gauss(GEN A, GEN *R, GEN *C, GEN T, ulong p)
    1143        7147 : { return FlxqM_CUP_gauss(A, R, C, NULL, NULL, T, p); }
    1144             : 
    1145             : /* column echelon form */
    1146             : static ulong
    1147       12537 : FlxqM_echelon(GEN A, GEN *R, GEN *C, GEN T, ulong p)
    1148             : {
    1149       12537 :   long j, j1, j2, m = nbrows(A), n = lg(A) - 1, n1, r, r1, r2;
    1150             :   GEN A1, A2, R1, R1c, C1, R2, C2;
    1151             :   GEN A12, A22, B2, C11, C21, M12;
    1152       12537 :   pari_sp av = avma;
    1153             : 
    1154       12537 :   if (m < FlxqM_CUP_LIMIT || n < FlxqM_CUP_LIMIT)
    1155        7147 :     return FlxqM_echelon_gauss(shallowcopy(A), R, C, T, p);
    1156             : 
    1157        5390 :   n1 = (n + 1)/2;
    1158        5390 :   A1 = vecslice(A, 1, n1);
    1159        5390 :   A2 = vecslice(A, n1 + 1, n);
    1160        5390 :   r1 = FlxqM_echelon(A1, &R1, &C1, T, p);
    1161        5390 :   if (!r1) return FlxqM_echelon(A2, R, C, T, p);
    1162        4893 :   if (r1 == m) { *R = R1; *C = C1; return r1; }
    1163        4795 :   R1c = indexcompl(R1, m);
    1164        4795 :   C11 = rowpermute(C1, R1);
    1165        4795 :   C21 = rowpermute(C1, R1c);
    1166        4795 :   A12 = rowpermute(A2, R1);
    1167        4795 :   A22 = rowpermute(A2, R1c);
    1168        4795 :   M12 = FlxqM_rsolve_lower_unit(C11, A12, T, p);
    1169        4795 :   B2 = FlxM_sub(A22, FlxqM_mul(C21, M12, T, p), p);
    1170        4795 :   r2 = FlxqM_echelon(B2, &R2, &C2, T, p);
    1171        4795 :   if (!r2) { *R = R1; *C = C1; r = r1; }
    1172             :   else
    1173             :   {
    1174        1904 :     R2 = perm_mul(R1c, R2);
    1175        1904 :     C2 = rowpermute(vconcat(zero_FlxM(r1, r2, get_Flx_var(T)), C2),
    1176             :                     perm_inv(vecsmall_concat(R1, R1c)));
    1177        1904 :     r = r1 + r2;
    1178        1904 :     *R = cgetg(r + 1, t_VECSMALL);
    1179        1904 :     *C = cgetg(r + 1, t_MAT);
    1180       12040 :     for (j = j1 = j2 = 1; j <= r; j++)
    1181             :     {
    1182       10136 :       if (j2 > r2 || (j1 <= r1 && R1[j1] < R2[j2]))
    1183             :       {
    1184        6181 :         gel(*C, j) = gel(C1, j1);
    1185        6181 :         (*R)[j] = R1[j1++];
    1186             :       }
    1187             :       else
    1188             :       {
    1189        3955 :         gel(*C, j) = gel(C2, j2);
    1190        3955 :         (*R)[j] = R2[j2++];
    1191             :       }
    1192             :     }
    1193             :   }
    1194        4795 :   if (gc_needed(av, 1)) gerepileall(av, 2, R, C);
    1195        4795 :   return r;
    1196             : }
    1197             : 
    1198             : /*******************************************************************/
    1199             : /*                                                                 */
    1200             : /*                    LINEAR ALGEBRA MODULO P                      */
    1201             : /*                                                                 */
    1202             : /*******************************************************************/
    1203             : 
    1204             : static long
    1205     2394370 : F2v_find_nonzero(GEN x0, GEN mask0, long m)
    1206             : {
    1207     2394370 :   ulong *x = (ulong *)x0+2, *mask = (ulong *)mask0+2, e;
    1208     2394370 :   long i, l = lg(x0)-2;
    1209     3994673 :   for (i = 0; i < l; i++)
    1210             :   {
    1211     3431439 :     e = *x++ & *mask++;
    1212     3431439 :     if (e) return i*BITS_IN_LONG+vals(e)+1;
    1213             :   }
    1214      563234 :   return m+1;
    1215             : }
    1216             : 
    1217             : /* in place, destroy x */
    1218             : GEN
    1219      303210 : F2m_ker_sp(GEN x, long deplin)
    1220             : {
    1221             :   GEN y, c, d;
    1222             :   long i, j, k, r, m, n;
    1223             : 
    1224      303210 :   n = lg(x)-1;
    1225      303210 :   m = mael(x,1,1); r=0;
    1226             : 
    1227      303210 :   d = cgetg(n+1, t_VECSMALL);
    1228      303210 :   c = const_F2v(m);
    1229     2385280 :   for (k=1; k<=n; k++)
    1230             :   {
    1231     2107439 :     GEN xk = gel(x,k);
    1232     2107439 :     j = F2v_find_nonzero(xk, c, m);
    1233     2107442 :     if (j>m)
    1234             :     {
    1235      455475 :       if (deplin) {
    1236       25369 :         GEN c = zero_F2v(n);
    1237      109438 :         for (i=1; i<k; i++)
    1238       84069 :           if (F2v_coeff(xk, d[i]))
    1239       42429 :             F2v_set(c, i);
    1240       25369 :         F2v_set(c, k);
    1241       25369 :         return c;
    1242             :       }
    1243      430106 :       r++; d[k] = 0;
    1244             :     }
    1245             :     else
    1246             :     {
    1247     1651967 :       F2v_clear(c,j); d[k] = j;
    1248     1651967 :       F2v_clear(xk, j);
    1249    73589455 :       for (i=k+1; i<=n; i++)
    1250             :       {
    1251    71937487 :         GEN xi = gel(x,i);
    1252    71937487 :         if (F2v_coeff(xi,j)) F2v_add_inplace(xi, xk);
    1253             :       }
    1254     1651968 :       F2v_set(xk, j);
    1255             :     }
    1256             :   }
    1257      277841 :   if (deplin) return NULL;
    1258             : 
    1259      276406 :   y = zero_F2m_copy(n,r);
    1260      706512 :   for (j=k=1; j<=r; j++,k++)
    1261             :   {
    1262      430106 :     GEN C = gel(y,j); while (d[k]) k++;
    1263    12611358 :     for (i=1; i<k; i++)
    1264    12181252 :       if (d[i] && F2m_coeff(x,d[i],k))
    1265     4460746 :         F2v_set(C,i);
    1266      430106 :     F2v_set(C, k);
    1267             :   }
    1268      276406 :   return y;
    1269             : }
    1270             : 
    1271             : static void /* assume m < p */
    1272     6338804 : _Fl_addmul(GEN b, long k, long i, ulong m, ulong p, ulong pi)
    1273             : {
    1274     6338804 :   uel(b,k) = Fl_addmul_pre(uel(b, k), m, uel(b, i), p, pi);
    1275     6338804 : }
    1276             : static void /* same m = 1 */
    1277      185711 : _Fl_add(GEN b, long k, long i, ulong p)
    1278             : {
    1279      185711 :   uel(b,k) = Fl_add(uel(b,k), uel(b,i), p);
    1280      185711 : }
    1281             : static void /* assume m < p && SMALL_ULONG(p) && (! (b[i] & b[k] & HIGHMASK)) */
    1282    14846139 : _Fl_addmul_OK(GEN b, long k, long i, ulong m, ulong p)
    1283             : {
    1284    14846139 :   uel(b,k) += m * uel(b,i);
    1285    14846139 :   if (uel(b,k) & HIGHMASK) uel(b,k) %= p;
    1286    14846139 : }
    1287             : static void /* assume SMALL_ULONG(p) && (! (b[i] & b[k] & HIGHMASK)) */
    1288     2768807 : _Fl_add_OK(GEN b, long k, long i, ulong p)
    1289             : {
    1290     2768807 :   uel(b,k) += uel(b,i);
    1291     2768807 :   if (uel(b,k) & HIGHMASK) uel(b,k) %= p;
    1292     2768807 : }
    1293             : 
    1294             : static GEN
    1295      113134 : Flm_ker_gauss_OK(GEN x, ulong p, long deplin)
    1296             : {
    1297             :   GEN y, c, d;
    1298             :   long i, j, k, r, t, m, n;
    1299             :   ulong a;
    1300             : 
    1301      113134 :   n = lg(x)-1;
    1302      113134 :   m=nbrows(x); r=0;
    1303             : 
    1304      113134 :   c = zero_zv(m);
    1305      113134 :   d = cgetg(n+1, t_VECSMALL);
    1306      113134 :   a = 0; /* for gcc -Wall */
    1307      531010 :   for (k=1; k<=n; k++)
    1308             :   {
    1309     1788809 :     for (j=1; j<=m; j++)
    1310     1587227 :       if (!c[j])
    1311             :       {
    1312     1016613 :         a = ucoeff(x,j,k) % p;
    1313     1016613 :         if (a) break;
    1314             :       }
    1315      436531 :     if (j > m)
    1316             :     {
    1317      201582 :       if (deplin==1) {
    1318       18655 :         c = cgetg(n+1, t_VECSMALL);
    1319       18655 :         for (i=1; i<k; i++) c[i] = ucoeff(x,d[i],k) % p;
    1320       18655 :         c[k] = 1; for (i=k+1; i<=n; i++) c[i] = 0;
    1321       18655 :         return c;
    1322             :       }
    1323      182927 :       r++; d[k] = 0;
    1324             :     }
    1325             :     else
    1326             :     {
    1327      234949 :       ulong piv = p - Fl_inv(a, p); /* -1/a */
    1328      234949 :       c[j] = k; d[k] = j;
    1329      234949 :       ucoeff(x,j,k) = p-1;
    1330      234949 :       if (piv != 1)
    1331      157116 :         for (i=k+1; i<=n; i++) ucoeff(x,j,i) = (piv * ucoeff(x,j,i)) % p;
    1332     1492619 :       for (t=1; t<=m; t++)
    1333             :       {
    1334     1257670 :         if (t == j) continue;
    1335             : 
    1336     1022721 :         piv = ( ucoeff(x,t,k) %= p );
    1337     1022721 :         if (!piv) continue;
    1338      465463 :         if (piv == 1)
    1339      120861 :           for (i=k+1; i<=n; i++) _Fl_add_OK(gel(x,i),t,j, p);
    1340             :         else
    1341      344602 :           for (i=k+1; i<=n; i++) _Fl_addmul_OK(gel(x,i),t,j,piv, p);
    1342             :       }
    1343             :     }
    1344             :   }
    1345       94479 :   if (deplin==1) return NULL;
    1346             : 
    1347       94472 :   y = cgetg(r+1, t_MAT);
    1348      277399 :   for (j=k=1; j<=r; j++,k++)
    1349             :   {
    1350      182927 :     GEN C = cgetg(n+1, t_VECSMALL);
    1351             : 
    1352      182927 :     gel(y,j) = C; while (d[k]) k++;
    1353      630823 :     for (i=1; i<k; i++)
    1354      447896 :       if (d[i])
    1355      268823 :         uel(C,i) = ucoeff(x,d[i],k) % p;
    1356             :       else
    1357      179073 :         uel(C,i) = 0UL;
    1358      182927 :     uel(C,k) = 1UL; for (i=k+1; i<=n; i++) uel(C,i) = 0UL;
    1359             :   }
    1360       94472 :   if (deplin == 2) {
    1361           0 :     GEN pc = cgetg(n - r + 1, t_VECSMALL);  /* indices of pivot columns */
    1362           0 :     for (i = j = 1; j <= n; j++)
    1363           0 :       if (d[j]) pc[i++] = j;
    1364           0 :     return mkvec2(y, pc);
    1365             :   }
    1366       94472 :   return y;
    1367             : }
    1368             : 
    1369             : /* in place, destroy x */
    1370             : static GEN
    1371      232757 : Flm_ker_gauss(GEN x, ulong p, long deplin)
    1372             : {
    1373             :   GEN y, c, d;
    1374             :   long i, j, k, r, t, m, n;
    1375             :   ulong a, pi;
    1376      232757 :   n = lg(x)-1;
    1377      232757 :   if (!n) return cgetg(1,t_MAT);
    1378      232652 :   if (SMALL_ULONG(p)) return Flm_ker_gauss_OK(x, p, deplin);
    1379      119518 :   pi = get_Fl_red(p);
    1380             : 
    1381      119518 :   m=nbrows(x); r=0;
    1382             : 
    1383      119518 :   c = zero_zv(m);
    1384      119518 :   d = cgetg(n+1, t_VECSMALL);
    1385      119518 :   a = 0; /* for gcc -Wall */
    1386      390438 :   for (k=1; k<=n; k++)
    1387             :   {
    1388      776820 :     for (j=1; j<=m; j++)
    1389      673919 :       if (!c[j])
    1390             :       {
    1391      523607 :         a = ucoeff(x,j,k);
    1392      523607 :         if (a) break;
    1393             :       }
    1394      270927 :     if (j > m)
    1395             :     {
    1396      102901 :       if (deplin==1) {
    1397           7 :         c = cgetg(n+1, t_VECSMALL);
    1398           7 :         for (i=1; i<k; i++) c[i] = ucoeff(x,d[i],k);
    1399           7 :         c[k] = 1; for (i=k+1; i<=n; i++) c[i] = 0;
    1400           7 :         return c;
    1401             :       }
    1402      102894 :       r++; d[k] = 0;
    1403             :     }
    1404             :     else
    1405             :     {
    1406      168026 :       ulong piv = p - Fl_inv(a, p); /* -1/a */
    1407      168026 :       c[j] = k; d[k] = j;
    1408      168026 :       ucoeff(x,j,k) = p-1;
    1409      168026 :       if (piv != 1)
    1410      325595 :         for (i=k+1; i<=n; i++)
    1411      159492 :           ucoeff(x,j,i) = Fl_mul_pre(piv, ucoeff(x,j,i), p, pi);
    1412      710320 :       for (t=1; t<=m; t++)
    1413             :       {
    1414      542294 :         if (t == j) continue;
    1415             : 
    1416      374268 :         piv = ucoeff(x,t,k);
    1417      374268 :         if (!piv) continue;
    1418      203887 :         if (piv == 1)
    1419        8976 :           for (i=k+1; i<=n; i++) _Fl_add(gel(x,i),t,j,p);
    1420             :         else
    1421      194911 :           for (i=k+1; i<=n; i++) _Fl_addmul(gel(x,i),t,j,piv,p, pi);
    1422             :       }
    1423             :     }
    1424             :   }
    1425      119511 :   if (deplin==1) return NULL;
    1426             : 
    1427      119504 :   y = cgetg(r+1, t_MAT);
    1428      222398 :   for (j=k=1; j<=r; j++,k++)
    1429             :   {
    1430      102894 :     GEN C = cgetg(n+1, t_VECSMALL);
    1431             : 
    1432      102894 :     gel(y,j) = C; while (d[k]) k++;
    1433      219231 :     for (i=1; i<k; i++)
    1434      116337 :       if (d[i])
    1435       78118 :         uel(C,i) = ucoeff(x,d[i],k);
    1436             :       else
    1437       38219 :         uel(C,i) = 0UL;
    1438      102894 :     uel(C,k) = 1UL; for (i=k+1; i<=n; i++) uel(C,i) = 0UL;
    1439             :   }
    1440      119504 :   if (deplin == 2) {
    1441      115944 :     GEN pc = cgetg(n - r + 1, t_VECSMALL);  /* indices of pivot columns */
    1442      364086 :     for (i = j = 1; j <= n; j++)
    1443      248142 :       if (d[j]) pc[i++] = j;
    1444      115944 :     return mkvec2(y, pc);
    1445             :   }
    1446        3560 :   return y;
    1447             : }
    1448             : 
    1449             : GEN
    1450       73155 : FpM_intersect(GEN x, GEN y, GEN p)
    1451             : {
    1452       73155 :   pari_sp av = avma;
    1453       73155 :   long j, lx = lg(x);
    1454             :   GEN z;
    1455             : 
    1456       73155 :   if (lx==1 || lg(y)==1) return cgetg(1,t_MAT);
    1457       73155 :   z = FpM_ker(shallowconcat(x,y), p);
    1458       73155 :   for (j=lg(z)-1; j; j--) setlg(gel(z,j),lx);
    1459       73155 :   return gerepileupto(av, FpM_mul(x,z,p));
    1460             : }
    1461             : GEN
    1462           0 : Flm_intersect(GEN x, GEN y, ulong p)
    1463             : {
    1464           0 :   pari_sp av = avma;
    1465           0 :   long j, lx = lg(x);
    1466             :   GEN z;
    1467             : 
    1468           0 :   if (lx==1 || lg(y)==1) return cgetg(1,t_MAT);
    1469           0 :   z = Flm_ker(shallowconcat(x,y), p);
    1470           0 :   for (j=lg(z)-1; j; j--) setlg(gel(z,j),lx);
    1471           0 :   return gerepileupto(av, Flm_mul(x,z,p));
    1472             : }
    1473             : 
    1474             : /* not memory clean */
    1475             : GEN
    1476          92 : F2m_ker(GEN x) { return F2m_ker_sp(F2m_copy(x), 0); }
    1477             : GEN
    1478           0 : F2m_deplin(GEN x) { return F2m_ker_sp(F2m_copy(x), 1); }
    1479             : 
    1480             : static GEN
    1481      120536 : Flm_ker_echelon(GEN x, ulong p, long pivots) {
    1482      120536 :   pari_sp av = avma;
    1483             :   GEN R, Rc, C, C1, C2, S, K;
    1484      120536 :   long n = lg(x) - 1, r;
    1485      120536 :   r = Flm_echelon(Flm_transpose(x), &R, &C, p);
    1486      120536 :   Rc = indexcompl(R, n);
    1487      120536 :   C1 = rowpermute(C, R);
    1488      120536 :   C2 = rowpermute(C, Rc);
    1489      120536 :   S = Flm_lsolve_lower_unit(C1, C2, p);
    1490      120536 :   K = vecpermute(shallowconcat(Flm_neg(S, p), matid_Flm(n - r)),
    1491             :                  perm_inv(vecsmall_concat(R, Rc)));
    1492      120536 :   K = Flm_transpose(K);
    1493      120536 :   if (pivots)
    1494        3882 :     return gerepilecopy(av, mkvec2(K, R));
    1495      116654 :   return gerepileupto(av, K);
    1496             : }
    1497             : 
    1498             : static GEN
    1499       23552 : Flm_deplin_echelon(GEN x, ulong p) {
    1500       23552 :   pari_sp av = avma;
    1501             :   GEN R, Rc, C, C1, C2, s, v;
    1502       23552 :   long i, n = lg(x) - 1, r;
    1503       23552 :   r = Flm_echelon(Flm_transpose(x), &R, &C, p);
    1504       23552 :   if (r == n) { avma = av; return NULL; }
    1505       23545 :   Rc = indexcompl(R, n);
    1506       23545 :   i = Rc[1];
    1507       23545 :   C1 = rowpermute(C, R);
    1508       23545 :   C2 = rowslice(C, i, i);
    1509       23545 :   s = Flm_row(Flm_lsolve_lower_unit(C1, C2, p), 1);
    1510       23545 :   v = vecpermute(vecsmall_concat(Flv_neg(s, p), vecsmall_ei(n - r, 1)),
    1511             :                  perm_inv(vecsmall_concat(R, Rc)));
    1512       23545 :   return gerepileuptoleaf(av, v);
    1513             : }
    1514             : 
    1515             : static GEN
    1516      376845 : Flm_ker_i(GEN x, ulong p, long deplin, long inplace) {
    1517      376845 :   if (lg(x) - 1 >= Flm_CUP_LIMIT && nbrows(x) >= Flm_CUP_LIMIT)
    1518      144088 :     switch(deplin) {
    1519      116654 :     case 0: return Flm_ker_echelon(x, p, 0);
    1520       23552 :     case 1: return Flm_deplin_echelon(x, p);
    1521        3882 :     case 2: return Flm_ker_echelon(x, p, 1);
    1522             :     }
    1523      232757 :   return Flm_ker_gauss(inplace? x: Flm_copy(x), p, deplin);
    1524             : }
    1525             : 
    1526             : GEN
    1527      309414 : Flm_ker_sp(GEN x, ulong p, long deplin) {
    1528      309414 :   return Flm_ker_i(x, p, deplin, 1);
    1529             : }
    1530             : 
    1531             : GEN
    1532       67431 : Flm_ker(GEN x, ulong p) {
    1533       67431 :   return Flm_ker_i(x, p, 0, 0);
    1534             : }
    1535             : 
    1536             : GEN
    1537           0 : Flm_deplin(GEN x, ulong p) {
    1538           0 :   return Flm_ker_i(x, p, 1, 0);
    1539             : }
    1540             : 
    1541             : ulong
    1542        1631 : F2m_det_sp(GEN x) { return !F2m_ker_sp(x, 1); }
    1543             : 
    1544             : ulong
    1545          14 : F2m_det(GEN x)
    1546             : {
    1547          14 :   pari_sp av = avma;
    1548          14 :   ulong d = F2m_det_sp(F2m_copy(x));
    1549          14 :   avma = av; return d;
    1550             : }
    1551             : 
    1552             : /* in place, destroy a, SMALL_ULONG(p) is TRUE */
    1553             : static ulong
    1554        1743 : Flm_det_gauss_OK(GEN a, long nbco, ulong p)
    1555             : {
    1556        1743 :   long i,j,k, s = 1;
    1557        1743 :   ulong q, x = 1;
    1558             : 
    1559        7266 :   for (i=1; i<nbco; i++)
    1560             :   {
    1561        7140 :     for(k=i; k<=nbco; k++)
    1562             :     {
    1563        6958 :       ulong c = ucoeff(a,k,i) % p;
    1564        6958 :       ucoeff(a,k,i) = c;
    1565        6958 :       if (c) break;
    1566             :     }
    1567        5705 :     for(j=k+1; j<=nbco; j++) ucoeff(a,j,i) %= p;
    1568        5705 :     if (k > nbco) return ucoeff(a,i,i);
    1569        5523 :     if (k != i)
    1570             :     { /* exchange the lines s.t. k = i */
    1571         784 :       for (j=i; j<=nbco; j++) lswap(ucoeff(a,i,j), ucoeff(a,k,j));
    1572         784 :       s = -s;
    1573             :     }
    1574        5523 :     q = ucoeff(a,i,i);
    1575             : 
    1576        5523 :     if (x & HIGHMASK) x %= p;
    1577        5523 :     x *= q;
    1578        5523 :     q = Fl_inv(q,p);
    1579       18417 :     for (k=i+1; k<=nbco; k++)
    1580             :     {
    1581       12894 :       ulong m = ucoeff(a,i,k) % p;
    1582       12894 :       if (!m) continue;
    1583             : 
    1584        8708 :       m = p - ((m*q)%p);
    1585       34489 :       for (j=i+1; j<=nbco; j++)
    1586             :       {
    1587       25781 :         ulong c = ucoeff(a,j,k);
    1588       25781 :         if (c & HIGHMASK) c %= p;
    1589       25781 :         ucoeff(a,j,k) = c  + m*ucoeff(a,j,i);
    1590             :       }
    1591             :     }
    1592             :   }
    1593        1561 :   if (x & HIGHMASK) x %= p;
    1594        1561 :   q = ucoeff(a,nbco,nbco);
    1595        1561 :   if (q & HIGHMASK) q %= p;
    1596        1561 :   x = (x*q) % p;
    1597        1561 :   if (s < 0 && x) x = p - x;
    1598        1561 :   return x;
    1599             : }
    1600             : 
    1601             : /* in place, destroy a */
    1602             : static ulong
    1603       55110 : Flm_det_gauss(GEN a, ulong p)
    1604             : {
    1605       55110 :   long i,j,k, s = 1, nbco = lg(a)-1;
    1606       55110 :   ulong pi, q, x = 1;
    1607             : 
    1608       55110 :   if (SMALL_ULONG(p)) return Flm_det_gauss_OK(a, nbco, p);
    1609       53367 :   pi = get_Fl_red(p);
    1610      339122 :   for (i=1; i<nbco; i++)
    1611             :   {
    1612      285835 :     for(k=i; k<=nbco; k++)
    1613      285830 :       if (ucoeff(a,k,i)) break;
    1614      285753 :     if (k > nbco) return ucoeff(a,i,i);
    1615      285753 :     if (k != i)
    1616             :     { /* exchange the lines s.t. k = i */
    1617          67 :       for (j=i; j<=nbco; j++) lswap(ucoeff(a,i,j), ucoeff(a,k,j));
    1618          67 :       s = -s;
    1619             :     }
    1620      285753 :     q = ucoeff(a,i,i);
    1621             : 
    1622      285753 :     x = Fl_mul_pre(x, q, p, pi);
    1623      285753 :     q = Fl_inv(q,p);
    1624     1220991 :     for (k=i+1; k<=nbco; k++)
    1625             :     {
    1626      935235 :       ulong m = ucoeff(a,i,k);
    1627      935235 :       if (!m) continue;
    1628             : 
    1629      892529 :       m = Fl_mul_pre(m, q, p, pi);
    1630     4554187 :       for (j=i+1; j<=nbco; j++)
    1631     3661710 :         ucoeff(a,j,k) = Fl_sub(ucoeff(a,j,k), Fl_mul_pre(m,ucoeff(a,j,i), p, pi), p);
    1632             :     }
    1633             :   }
    1634       53369 :   if (s < 0) x = Fl_neg(x, p);
    1635       53369 :   return Fl_mul(x, ucoeff(a,nbco,nbco), p);
    1636             : }
    1637             : 
    1638             : static ulong
    1639       36281 : Flm_det_CUP(GEN a, ulong p) {
    1640             :   GEN R, C, U, P;
    1641       36281 :   long d, i, n = lg(a) - 1, r;
    1642       36281 :   r = Flm_CUP(a, &R, &C, &U, &P, p);
    1643       36294 :   if (r < n)
    1644          54 :     d = 0;
    1645             :   else {
    1646       36240 :     d = perm_sign(P) == 1? 1: p-1;
    1647      380327 :     for (i = 1; i <= n; i++)
    1648      344100 :       d = Fl_mul(d, ucoeff(U, i, i), p);
    1649             :   }
    1650       36281 :   return d;
    1651             : }
    1652             : 
    1653             : static ulong
    1654       91392 : Flm_det_i(GEN x, ulong p, long inplace) {
    1655       91392 :   pari_sp av = avma;
    1656             :   ulong d;
    1657       91392 :   if (lg(x) - 1 >= Flm_CUP_LIMIT)
    1658       36281 :     d = Flm_det_CUP(x, p);
    1659             :   else
    1660       55111 :     d = Flm_det_gauss(inplace? x: Flm_copy(x), p);
    1661       91390 :   avma = av; return d;
    1662             : }
    1663             : 
    1664             : ulong
    1665       91392 : Flm_det_sp(GEN x, ulong p) {
    1666       91392 :   return Flm_det_i(x, p, 1);
    1667             : }
    1668             : 
    1669             : ulong
    1670           0 : Flm_det(GEN x, ulong p) {
    1671           0 :   return Flm_det_i(x, p, 0);
    1672             : }
    1673             : 
    1674             : static GEN
    1675      477461 : FpM_init(GEN a, GEN p, ulong *pp)
    1676             : {
    1677      477461 :   if (lgefint(p) == 3)
    1678             :   {
    1679      470160 :     *pp = uel(p,2);
    1680      470160 :     return (*pp==2)? ZM_to_F2m(a): ZM_to_Flm(a, *pp);
    1681             :   }
    1682        7301 :   *pp = 0; return a;
    1683             : }
    1684             : GEN
    1685        2359 : RgM_Fp_init(GEN a, GEN p, ulong *pp)
    1686             : {
    1687        2359 :   if (lgefint(p) == 3)
    1688             :   {
    1689        2009 :     *pp = uel(p,2);
    1690        2009 :     return (*pp==2)? RgM_to_F2m(a): RgM_to_Flm(a, *pp);
    1691             :   }
    1692         350 :   *pp = 0; return RgM_to_FpM(a,p);
    1693             : }
    1694             : 
    1695             : static GEN
    1696         315 : FpM_det_gen(GEN a, GEN p)
    1697             : {
    1698             :   void *E;
    1699         315 :   const struct bb_field *S = get_Fp_field(&E,p);
    1700         315 :   return gen_det(a, E, S);
    1701             : }
    1702             : GEN
    1703        3948 : FpM_det(GEN a, GEN p)
    1704             : {
    1705        3948 :   pari_sp av = avma;
    1706             :   ulong pp, d;
    1707        3948 :   a = FpM_init(a, p, &pp);
    1708        3948 :   switch(pp)
    1709             :   {
    1710         315 :   case 0: return FpM_det_gen(a, p);
    1711        1617 :   case 2: d = F2m_det_sp(a); break;
    1712        2016 :   default:d = Flm_det_sp(a,pp); break;
    1713             :   }
    1714        3633 :   avma = av; return utoi(d);
    1715             : }
    1716             : 
    1717             : /* Destroy x */
    1718             : static GEN
    1719       39241 : F2m_gauss_pivot(GEN x, long *rr)
    1720             : {
    1721             :   GEN c, d;
    1722             :   long i, j, k, r, m, n;
    1723             : 
    1724       39241 :   n = lg(x)-1; if (!n) { *rr=0; return NULL; }
    1725       39241 :   m = mael(x,1,1); r=0;
    1726             : 
    1727       39241 :   d = cgetg(n+1, t_VECSMALL);
    1728       39241 :   c = const_F2v(m);
    1729      326172 :   for (k=1; k<=n; k++)
    1730             :   {
    1731      286931 :     GEN xk = gel(x,k);
    1732      286931 :     j = F2v_find_nonzero(xk, c, m);
    1733      286931 :     if (j>m) { r++; d[k] = 0; }
    1734             :     else
    1735             :     {
    1736      179172 :       F2v_clear(c,j); d[k] = j;
    1737     2625579 :       for (i=k+1; i<=n; i++)
    1738             :       {
    1739     2446407 :         GEN xi = gel(x,i);
    1740     2446407 :         if (F2v_coeff(xi,j)) F2v_add_inplace(xi, xk);
    1741             :       }
    1742             :     }
    1743             :   }
    1744             : 
    1745       39241 :   *rr = r; avma = (pari_sp)d; return d;
    1746             : }
    1747             : 
    1748             : /* Destroy x */
    1749             : static GEN
    1750      167045 : Flm_gauss_pivot(GEN x, ulong p, long *rr)
    1751             : {
    1752             :   GEN c,d;
    1753             :   long i,j,k,r,t,n,m;
    1754             : 
    1755      167045 :   n=lg(x)-1; if (!n) { *rr=0; return NULL; }
    1756             : 
    1757      167045 :   m=nbrows(x); r=0;
    1758      167045 :   d=cgetg(n+1,t_VECSMALL);
    1759      167045 :   c = zero_zv(m);
    1760      769216 :   for (k=1; k<=n; k++)
    1761             :   {
    1762     2067717 :     for (j=1; j<=m; j++)
    1763     1905945 :       if (!c[j])
    1764             :       {
    1765     1058054 :         ucoeff(x,j,k) %= p;
    1766     1058054 :         if (ucoeff(x,j,k)) break;
    1767             :       }
    1768      602171 :     if (j>m) { r++; d[k]=0; }
    1769             :     else
    1770             :     {
    1771      440399 :       ulong piv = p - Fl_inv(ucoeff(x,j,k), p);
    1772      440399 :       c[j]=k; d[k]=j;
    1773     1337143 :       for (i=k+1; i<=n; i++)
    1774      896744 :         ucoeff(x,j,i) = Fl_mul(piv, ucoeff(x,j,i), p);
    1775     3340796 :       for (t=1; t<=m; t++)
    1776     2900397 :         if (!c[t]) /* no pivot on that line yet */
    1777             :         {
    1778     1892451 :           piv = ucoeff(x,t,k);
    1779     1892451 :           if (piv)
    1780             :           {
    1781     1046653 :             ucoeff(x,t,k) = 0;
    1782     3378214 :             for (i=k+1; i<=n; i++)
    1783     4663122 :               ucoeff(x,t,i) = Fl_add(ucoeff(x,t,i),
    1784     2331561 :                                      Fl_mul(piv,ucoeff(x,j,i),p),p);
    1785             :           }
    1786             :         }
    1787      440399 :       for (i=k; i<=n; i++) ucoeff(x,j,i) = 0; /* dummy */
    1788             :     }
    1789             :   }
    1790      167045 :   *rr = r; avma = (pari_sp)d; return d;
    1791             : }
    1792             : 
    1793             : static GEN
    1794       66966 : Flm_pivots_CUP(GEN x, ulong p, long *rr) {
    1795             :   pari_sp av;
    1796       66966 :   long i, n = lg(x) - 1, r;
    1797       66966 :   GEN R, C, U, P, d = zero_zv(n);
    1798       66966 :   av = avma;
    1799       66966 :   r = Flm_CUP(x, &R, &C, &U, &P, p);
    1800      814673 :   for(i = 1; i <= r; i++)
    1801      747707 :     d[P[i]] = R[i];
    1802       66966 :   avma = av;
    1803       66966 :   *rr = n - r;
    1804       66966 :   return d;
    1805             : }
    1806             : 
    1807             : static GEN
    1808      234011 : Flm_pivots(GEN x, ulong p, long *rr, long inplace) {
    1809      234011 :   if (lg(x) - 1 >= Flm_CUP_LIMIT && nbrows(x) >= Flm_CUP_LIMIT)
    1810       66966 :     return Flm_pivots_CUP(x, p, rr);
    1811      167045 :   return Flm_gauss_pivot(inplace? x: Flm_copy(x), p, rr);
    1812             : }
    1813             : 
    1814             : static GEN
    1815         815 : FpM_gauss_pivot_gen(GEN x, GEN p, long *rr)
    1816             : {
    1817             :   void *E;
    1818         815 :   const struct bb_field *S = get_Fp_field(&E,p);
    1819         815 :   return gen_Gauss_pivot(x, rr, E, S);
    1820             : }
    1821             : static GEN
    1822      140124 : FpM_gauss_pivot(GEN x, GEN p, long *rr)
    1823             : {
    1824             :   ulong pp;
    1825      140124 :   if (lg(x)==1) { *rr = 0; return NULL; }
    1826      138213 :   x = FpM_init(x, p, &pp);
    1827      138213 :   switch(pp)
    1828             :   {
    1829         815 :   case 0: return FpM_gauss_pivot_gen(x, p, rr);
    1830       39150 :   case 2: return F2m_gauss_pivot(x, rr);
    1831       98248 :   default:return Flm_pivots(x, pp, rr, 1);
    1832             :   }
    1833             : }
    1834             : 
    1835             : GEN
    1836       76522 : FpM_image(GEN x, GEN p)
    1837             : {
    1838             :   long r;
    1839       76522 :   GEN d = FpM_gauss_pivot(x,p,&r); /* d left on stack for efficiency */
    1840       76522 :   return image_from_pivot(x,d,r);
    1841             : }
    1842             : 
    1843             : GEN
    1844          63 : Flm_image(GEN x, ulong p)
    1845             : {
    1846             :   long r;
    1847          63 :   GEN d = Flm_pivots(x, p, &r, 0); /* d left on stack for efficiency */
    1848          63 :   return image_from_pivot(x,d,r);
    1849             : }
    1850             : 
    1851             : GEN
    1852           7 : F2m_image(GEN x)
    1853             : {
    1854             :   long r;
    1855           7 :   GEN d = F2m_gauss_pivot(F2m_copy(x),&r); /* d left on stack for efficiency */
    1856           7 :   return image_from_pivot(x,d,r);
    1857             : }
    1858             : 
    1859             : long
    1860          28 : FpM_rank(GEN x, GEN p)
    1861             : {
    1862          28 :   pari_sp av = avma;
    1863             :   long r;
    1864          28 :   (void)FpM_gauss_pivot(x,p,&r);
    1865          28 :   avma = av; return lg(x)-1 - r;
    1866             : }
    1867             : 
    1868             : long
    1869        2179 : Flm_rank(GEN x, ulong p)
    1870             : {
    1871        2179 :   pari_sp av = avma;
    1872             :   long r;
    1873        2179 :   if (lg(x) - 1 >= Flm_CUP_LIMIT && nbrows(x) >= Flm_CUP_LIMIT) {
    1874             :     GEN R, C;
    1875          35 :     r = Flm_echelon(x, &R, &C, p);
    1876          35 :     avma = av; return r;
    1877             :   }
    1878        2144 :   (void) Flm_pivots(x, p, &r, 0);
    1879        2144 :   avma = av; return lg(x)-1 - r;
    1880             : }
    1881             : 
    1882             : long
    1883          63 : F2m_rank(GEN x)
    1884             : {
    1885          63 :   pari_sp av = avma;
    1886             :   long r;
    1887          63 :   (void)F2m_gauss_pivot(F2m_copy(x),&r);
    1888          63 :   avma = av; return lg(x)-1 - r;
    1889             : }
    1890             : 
    1891             : static GEN
    1892        1267 : FlxqM_gauss_pivot(GEN x, GEN T, ulong p, long *rr)
    1893             : {
    1894             :   void *E;
    1895        1267 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    1896        1267 :   return gen_Gauss_pivot(x, rr, E, S);
    1897             : }
    1898             : 
    1899             : static GEN
    1900          14 : FlxqM_pivots_CUP(GEN x, GEN T, ulong p, long *rr) {
    1901             :   pari_sp av;
    1902          14 :   long i, n = lg(x) - 1, r;
    1903          14 :   GEN R, C, U, P, d = zero_zv(n);
    1904          14 :   av = avma;
    1905          14 :   r = FlxqM_CUP(x, &R, &C, &U, &P, T, p);
    1906         182 :   for(i = 1; i <= r; i++)
    1907         168 :     d[P[i]] = R[i];
    1908          14 :   avma = av;
    1909          14 :   *rr = n - r;
    1910          14 :   return d;
    1911             : }
    1912             : 
    1913             : static GEN
    1914          28 : FlxqM_pivots(GEN x, GEN T, ulong p, long *rr) {
    1915          28 :   if (lg(x) - 1 >= FlxqM_CUP_LIMIT && nbrows(x) >= FlxqM_CUP_LIMIT)
    1916          14 :     return FlxqM_pivots_CUP(x, T, p, rr);
    1917          14 :   return FlxqM_gauss_pivot(x, T, p, rr);
    1918             : }
    1919             : 
    1920             : GEN
    1921          21 : FlxqM_image(GEN x, GEN T, ulong p)
    1922             : {
    1923             :   long r;
    1924          21 :   GEN d = FlxqM_pivots(x, T, p, &r); /* d left on stack for efficiency */
    1925          21 :   return image_from_pivot(x,d,r);
    1926             : }
    1927             : 
    1928             : long
    1929          28 : FlxqM_rank(GEN x, GEN T, ulong p)
    1930             : {
    1931          28 :   pari_sp av = avma;
    1932             :   long r;
    1933          28 :   if (lg(x) - 1 >= FlxqM_CUP_LIMIT && nbrows(x) >= FlxqM_CUP_LIMIT) {
    1934             :     GEN R, C;
    1935          21 :     r = FlxqM_echelon(x, &R, &C, T, p);
    1936          21 :     avma = av; return r;
    1937             :   }
    1938           7 :   (void) FlxqM_pivots(x, T, p, &r);
    1939           7 :   avma = av; return lg(x)-1 - r;
    1940             : }
    1941             : 
    1942             : static GEN
    1943           7 : FlxqM_det_gen(GEN a, GEN T, ulong p)
    1944             : {
    1945             :   void *E;
    1946           7 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    1947           7 :   return gen_det(a, E, S);
    1948             : }
    1949             : 
    1950             : static GEN
    1951          21 : FlxqM_det_CUP(GEN a, GEN T, ulong p) {
    1952          21 :   pari_sp av = avma;
    1953             :   GEN R, C, U, P, d;
    1954          21 :   long i, n = lg(a) - 1, r, sv = get_Flx_var(T);
    1955          21 :   r = FlxqM_CUP(a, &R, &C, &U, &P, T, p);
    1956          21 :   if (r < n)
    1957           0 :     d = pol0_Flx(sv);
    1958             :   else {
    1959          21 :     d = mkvecsmall2(sv, perm_sign(P) == 1? 1: p - 1);
    1960         483 :     for (i = 1; i <= n; i++)
    1961         462 :       d = Flxq_mul(d, gcoeff(U, i, i), T, p);
    1962             :   }
    1963          21 :   return gerepileuptoleaf(av, d);
    1964             : }
    1965             : 
    1966             : GEN
    1967          28 : FlxqM_det(GEN a, GEN T, ulong p) {
    1968          28 :   if (lg(a) - 1 >= FlxqM_CUP_LIMIT)
    1969          21 :     return FlxqM_det_CUP(a, T, p);
    1970             :   else
    1971           7 :     return FlxqM_det_gen(a, T, p);
    1972             : }
    1973             : 
    1974             : GEN
    1975          21 : FlxqM_FlxqC_invimage(GEN A, GEN B, GEN T, ulong p) {
    1976             :   void *E;
    1977          21 :   const struct bb_field *ff = get_Flxq_field(&E, T, p);
    1978          21 :   return gen_matcolinvimage(A, B, E, ff);
    1979             : }
    1980             : 
    1981             : GEN
    1982          28 : FlxqM_FlxqC_mul(GEN A, GEN B, GEN T, ulong p) {
    1983             :   void *E;
    1984          28 :   const struct bb_field *ff = get_Flxq_field(&E, T, p);
    1985          28 :   return gen_matcolmul(A, B, E, ff);
    1986             : }
    1987             : 
    1988             : GEN
    1989       15332 : FlxqM_mul(GEN A, GEN B, GEN T, ulong p) {
    1990             :   void *E;
    1991             :   const struct bb_field *ff;
    1992       15332 :   long n = lg(A) - 1;
    1993             : 
    1994       15332 :   if (n == 0)
    1995           0 :     return cgetg(1, t_MAT);
    1996       15332 :   if (n > 1)
    1997       12427 :     return FlxqM_mul_Kronecker(A, B, T, p);
    1998        2905 :   ff = get_Flxq_field(&E, T, p);
    1999        2905 :   return gen_matmul(A, B, E, ff);
    2000             : }
    2001             : 
    2002             : static GEN
    2003          21 : FlxqM_invimage_gen(GEN A, GEN B, GEN T, ulong p) {
    2004             :   void *E;
    2005          21 :   const struct bb_field *ff = get_Flxq_field(&E, T, p);
    2006          21 :   return gen_matinvimage(A, B, E, ff);
    2007             : }
    2008             : 
    2009             : static GEN
    2010          21 : FlxqM_invimage_CUP(GEN A, GEN B, GEN T, ulong p) {
    2011          21 :   pari_sp av = avma;
    2012             :   GEN R, Rc, C, U, P, B1, B2, C1, C2, X, Y, Z;
    2013          21 :   long r, sv = get_Flx_var(T);
    2014          21 :   r = FlxqM_CUP(A, &R, &C, &U, &P, T, p);
    2015          21 :   Rc = indexcompl(R, nbrows(B));
    2016          21 :   C1 = rowpermute(C, R);
    2017          21 :   C2 = rowpermute(C, Rc);
    2018          21 :   B1 = rowpermute(B, R);
    2019          21 :   B2 = rowpermute(B, Rc);
    2020          21 :   Z = FlxqM_rsolve_lower_unit(C1, B1, T, p);
    2021          21 :   if (!gequal(FlxqM_mul(C2, Z, T, p), B2))
    2022          14 :     return NULL;
    2023          14 :   Y = vconcat(FlxqM_rsolve_upper(vecslice(U, 1, r), Z, T, p),
    2024          14 :               zero_FlxM(lg(A) - 1 - r, lg(B) - 1, sv));
    2025           7 :   X = rowpermute(Y, perm_inv(P));
    2026           7 :   return gerepilecopy(av, X);
    2027             : }
    2028             : 
    2029             : GEN
    2030          42 : FlxqM_invimage(GEN A, GEN B, GEN T, ulong p) {
    2031          42 :   long nA = lg(A)-1, nB = lg(B)-1;
    2032             : 
    2033          42 :   if (!nB) return cgetg(1, t_MAT);
    2034          42 :   if (nA + nB >= FlxqM_CUP_LIMIT && nbrows(B) >= FlxqM_CUP_LIMIT)
    2035          21 :     return FlxqM_invimage_CUP(A, B, T, p);
    2036          21 :   return FlxqM_invimage_gen(A, B, T, p);
    2037             : }
    2038             : 
    2039             : GEN
    2040           7 : F2xqM_det(GEN a, GEN T)
    2041             : {
    2042             :   void *E;
    2043           7 :   const struct bb_field *S = get_F2xq_field(&E, T);
    2044           7 :   return gen_det(a, E, S);
    2045             : }
    2046             : 
    2047             : static GEN
    2048          84 : F2xqM_gauss_gen(GEN a, GEN b, GEN T)
    2049             : {
    2050             :   void *E;
    2051          84 :   const struct bb_field *S = get_F2xq_field(&E, T);
    2052          84 :   return gen_Gauss(a, b, E, S);
    2053             : }
    2054             : 
    2055             : GEN
    2056          21 : F2xqM_gauss(GEN a, GEN b, GEN T)
    2057             : {
    2058          21 :   pari_sp av = avma;
    2059          21 :   long n = lg(a)-1;
    2060             :   GEN u;
    2061          21 :   if (!n || lg(b)==1) { avma = av; return cgetg(1, t_MAT); }
    2062          21 :   u = F2xqM_gauss_gen(a, b, T);
    2063          21 :   if (!u) { avma = av; return NULL; }
    2064          14 :   return gerepilecopy(av, u);
    2065             : }
    2066             : 
    2067             : GEN
    2068          35 : F2xqM_inv(GEN a, GEN T)
    2069             : {
    2070          35 :   pari_sp av = avma;
    2071             :   GEN u;
    2072          35 :   if (lg(a) == 1) { avma = av; return cgetg(1, t_MAT); }
    2073          35 :   u = F2xqM_gauss_gen(a, matid_F2xqM(nbrows(a),T), T);
    2074          35 :   if (!u) { avma = av; return NULL; }
    2075          28 :   return gerepilecopy(av, u);
    2076             : }
    2077             : 
    2078             : GEN
    2079          28 : F2xqM_F2xqC_gauss(GEN a, GEN b, GEN T)
    2080             : {
    2081          28 :   pari_sp av = avma;
    2082             :   GEN u;
    2083          28 :   if (lg(a) == 1) return cgetg(1, t_COL);
    2084          28 :   u = F2xqM_gauss_gen(a, mkmat(b), T);
    2085          28 :   if (!u) { avma = av; return NULL; }
    2086          14 :   return gerepilecopy(av, gel(u,1));
    2087             : }
    2088             : 
    2089             : GEN
    2090          21 : F2xqM_F2xqC_invimage(GEN A, GEN B, GEN T) {
    2091             :   void *E;
    2092          21 :   const struct bb_field *ff = get_F2xq_field(&E, T);
    2093          21 :   return gen_matcolinvimage(A, B, E, ff);
    2094             : }
    2095             : 
    2096             : GEN
    2097          21 : F2xqM_F2xqC_mul(GEN A, GEN B, GEN T) {
    2098             :   void *E;
    2099          21 :   const struct bb_field *ff = get_F2xq_field(&E, T);
    2100          21 :   return gen_matcolmul(A, B, E, ff);
    2101             : }
    2102             : 
    2103             : GEN
    2104        1407 : F2xqM_mul(GEN A, GEN B, GEN T) {
    2105             :   void *E;
    2106        1407 :   const struct bb_field *ff = get_F2xq_field(&E, T);
    2107        1407 :   return gen_matmul(A, B, E, ff);
    2108             : }
    2109             : 
    2110             : GEN
    2111          21 : F2xqM_invimage(GEN A, GEN B, GEN T) {
    2112             :   void *E;
    2113          21 :   const struct bb_field *ff = get_F2xq_field(&E, T);
    2114          21 :   return gen_matinvimage(A, B, E, ff);
    2115             : }
    2116             : 
    2117             : static GEN
    2118         105 : FqM_gauss_pivot_gen(GEN x, GEN T, GEN p, long *rr)
    2119             : {
    2120             :   void *E;
    2121         105 :   const struct bb_field *S = get_Fq_field(&E,T,p);
    2122         105 :   return gen_Gauss_pivot(x, rr, E, S);
    2123             : }
    2124             : static GEN
    2125        1337 : FqM_gauss_pivot(GEN x, GEN T, GEN p, long *rr)
    2126             : {
    2127        1337 :   if (lg(x)==1) { *rr = 0; return NULL; }
    2128        1337 :   if (!T) return FpM_gauss_pivot(x, p, rr);
    2129        1337 :   if (lgefint(p) == 3)
    2130             :   {
    2131        1232 :     pari_sp av = avma;
    2132        1232 :     ulong pp = uel(p,2);
    2133        1232 :     GEN Tp = ZXT_to_FlxT(T, pp);
    2134        1232 :     GEN d = FlxqM_gauss_pivot(FqM_to_FlxM(x, T, p), Tp, pp, rr);
    2135        1232 :     return d ? gerepileuptoleaf(av, d): d;
    2136             :   }
    2137         105 :   return FqM_gauss_pivot_gen(x, T, p, rr);
    2138             : }
    2139             : 
    2140             : GEN
    2141          49 : FqM_image(GEN x, GEN T, GEN p)
    2142             : {
    2143             :   long r;
    2144          49 :   GEN d = FqM_gauss_pivot(x,T,p,&r); /* d left on stack for efficiency */
    2145          49 :   return image_from_pivot(x,d,r);
    2146             : }
    2147             : 
    2148             : long
    2149          70 : FqM_rank(GEN x, GEN T, GEN p)
    2150             : {
    2151          70 :   pari_sp av = avma;
    2152             :   long r;
    2153          70 :   (void)FqM_gauss_pivot(x,T,p,&r);
    2154          70 :   avma = av; return lg(x)-1 - r;
    2155             : }
    2156             : 
    2157             : GEN
    2158          70 : FqM_det(GEN x, GEN T, GEN p)
    2159             : {
    2160             :   void *E;
    2161          70 :   const struct bb_field *S = get_Fq_field(&E,T,p);
    2162          70 :   return gen_det(x, E, S);
    2163             : }
    2164             : 
    2165             : GEN
    2166          21 : FqM_FqC_invimage(GEN A, GEN B, GEN T, GEN p) {
    2167             :   void *E;
    2168          21 :   const struct bb_field *ff = get_Fq_field(&E, T, p);
    2169          21 :   return gen_matcolinvimage(A, B, E, ff);
    2170             : }
    2171             : 
    2172             : GEN
    2173          28 : FqM_FqC_mul(GEN A, GEN B, GEN T, GEN p) {
    2174             :   void *E;
    2175          28 :   const struct bb_field *ff = get_Fq_field(&E, T, p);
    2176          28 :   return gen_matcolmul(A, B, E, ff);
    2177             : }
    2178             : 
    2179             : GEN
    2180       59960 : FqM_mul(GEN A, GEN B, GEN T, GEN p) {
    2181             :   void *E;
    2182       59960 :   long n = lg(A) - 1;
    2183             :   const struct bb_field *ff;
    2184       59960 :   if (n == 0)
    2185           0 :     return cgetg(1, t_MAT);
    2186       59960 :   if (n > 1)
    2187       57195 :     return FqM_mul_Kronecker(A, B, T, p);
    2188        2765 :   ff = get_Fq_field(&E, T, p);
    2189        2765 :   return gen_matmul(A, B, E, ff);
    2190             : }
    2191             : 
    2192             : GEN
    2193          42 : FqM_invimage(GEN A, GEN B, GEN T, GEN p) {
    2194             :   void *E;
    2195          42 :   const struct bb_field *ff = get_Fq_field(&E, T, p);
    2196          42 :   return gen_matinvimage(A, B, E, ff);
    2197             : }
    2198             : 
    2199             : static GEN
    2200        2259 : FpM_ker_gen(GEN x, GEN p, long deplin)
    2201             : {
    2202             :   void *E;
    2203        2259 :   const struct bb_field *S = get_Fp_field(&E,p);
    2204        2259 :   return gen_ker(x, deplin, E, S);
    2205             : }
    2206             : static GEN
    2207      269579 : FpM_ker_i(GEN x, GEN p, long deplin)
    2208             : {
    2209      269579 :   pari_sp av = avma;
    2210             :   ulong pp;
    2211             :   GEN y;
    2212             : 
    2213      269579 :   if (lg(x)==1) return cgetg(1,t_MAT);
    2214      269579 :   x = FpM_init(x, p, &pp);
    2215      269579 :   switch(pp)
    2216             :   {
    2217        2189 :   case 0: return FpM_ker_gen(x,p,deplin);
    2218             :   case 2:
    2219       77914 :     y = F2m_ker_sp(x, deplin);
    2220       77914 :     if (!y) return y;
    2221       77914 :     y = deplin? F2c_to_ZC(y): F2m_to_ZM(y);
    2222       77914 :     return gerepileupto(av, y);
    2223             :   default:
    2224      189476 :     y = Flm_ker_sp(x, pp, deplin);
    2225      189476 :     if (!y) return y;
    2226      189476 :     y = deplin? Flc_to_ZC(y): Flm_to_ZM(y);
    2227      189476 :     return gerepileupto(av, y);
    2228             :   }
    2229             : }
    2230             : 
    2231             : GEN
    2232      196480 : FpM_ker(GEN x, GEN p) { return FpM_ker_i(x,p,0); }
    2233             : 
    2234             : GEN
    2235       67331 : FpM_deplin(GEN x, GEN p) { return FpM_ker_i(x,p,1); }
    2236             : 
    2237             : static GEN
    2238         126 : FqM_ker_gen(GEN x, GEN T, GEN p, long deplin)
    2239             : {
    2240             :   void *E;
    2241         126 :   const struct bb_field *S = get_Fq_field(&E,T,p);
    2242         126 :   return gen_ker(x,deplin,E,S);
    2243             : }
    2244             : static GEN
    2245        8449 : FqM_ker_i(GEN x, GEN T, GEN p, long deplin)
    2246             : {
    2247        8449 :   if (!T) return FpM_ker_i(x,p,deplin);
    2248        2681 :   if (lg(x)==1) return cgetg(1,t_MAT);
    2249             : 
    2250        2681 :   if (lgefint(p)==3)
    2251             :   {
    2252        2555 :     pari_sp ltop=avma;
    2253        2555 :     ulong l= p[2];
    2254        2555 :     GEN Ml = FqM_to_FlxM(x, T, p);
    2255        2555 :     GEN Tl = ZXT_to_FlxT(T,l);
    2256        2555 :     GEN p1 = FlxM_to_ZXM(FlxqM_ker(Ml,Tl,l));
    2257        2555 :     return gerepileupto(ltop,p1);
    2258             :   }
    2259         126 :   return FqM_ker_gen(x, T, p, deplin);
    2260             : }
    2261             : 
    2262             : GEN
    2263        8372 : FqM_ker(GEN x, GEN T, GEN p) { return FqM_ker_i(x,T,p,0); }
    2264             : 
    2265             : GEN
    2266          77 : FqM_deplin(GEN x, GEN T, GEN p) { return FqM_ker_i(x,T,p,1); }
    2267             : 
    2268             : static GEN
    2269         784 : FlxqM_ker_gen(GEN x, GEN T, ulong p, long deplin)
    2270             : {
    2271             :   const struct bb_field *ff;
    2272             :   void *E;
    2273             : 
    2274         784 :   if (lg(x)==1) return cgetg(1,t_MAT);
    2275         784 :   ff=get_Flxq_field(&E,T,p);
    2276         784 :   return gen_ker(x,deplin, E, ff);
    2277             : }
    2278             : 
    2279             : static GEN
    2280        1813 : FlxqM_ker_echelon(GEN x, GEN T, ulong p) {
    2281        1813 :   pari_sp av = avma;
    2282             :   GEN R, Rc, C, C1, C2, S, K;
    2283        1813 :   long n = lg(x) - 1, r;
    2284        1813 :   r = FlxqM_echelon(shallowtrans(x), &R, &C, T, p);
    2285        1813 :   Rc = indexcompl(R, n);
    2286        1813 :   C1 = rowpermute(C, R);
    2287        1813 :   C2 = rowpermute(C, Rc);
    2288        1813 :   S = FlxqM_lsolve_lower_unit(C1, C2, T, p);
    2289        1813 :   K = vecpermute(shallowconcat(FlxM_neg(S, p), matid_FlxqM(n - r, T, p)),
    2290             :                  perm_inv(vecsmall_concat(R, Rc)));
    2291        1813 :   K = shallowtrans(K);
    2292        1813 :   return gerepilecopy(av, K);
    2293             : }
    2294             : 
    2295             : static GEN
    2296          14 : col_ei_FlxC(long n, long i, long sv) {
    2297          14 :   GEN v = zero_FlxC(n, sv);
    2298          14 :   gel(v, i) = pol1_Flx(sv);
    2299          14 :   return v;
    2300             : }
    2301             : 
    2302             : static GEN
    2303          21 : FlxqM_deplin_echelon(GEN x, GEN T, ulong p) {
    2304          21 :   pari_sp av = avma;
    2305             :   GEN R, Rc, C, C1, C2, s, v;
    2306          21 :   long i, n = lg(x) - 1, r, sv = get_Flx_var(T);
    2307          21 :   r = FlxqM_echelon(shallowtrans(x), &R, &C, T, p);
    2308          21 :   if (r == n) { avma = av; return NULL; }
    2309          14 :   Rc = indexcompl(R, n);
    2310          14 :   i = Rc[1];
    2311          14 :   C1 = rowpermute(C, R);
    2312          14 :   C2 = rowslice(C, i, i);
    2313          14 :   s = row(FlxqM_lsolve_lower_unit(C1, C2, T, p), 1);
    2314          14 :   settyp(s, t_COL);
    2315          14 :   v = vecpermute(shallowconcat(FlxC_neg(s, p), col_ei_FlxC(n - r, 1, sv)),
    2316             :                  perm_inv(vecsmall_concat(R, Rc)));
    2317          14 :   return gerepilecopy(av, v);
    2318             : }
    2319             : 
    2320             : static GEN
    2321        2618 : FlxqM_ker_i(GEN x, GEN T, ulong p, long deplin) {
    2322        2618 :   if (lg(x) - 1 >= FlxqM_CUP_LIMIT && nbrows(x) >= FlxqM_CUP_LIMIT)
    2323        1834 :     return deplin? FlxqM_deplin_echelon(x, T, p): FlxqM_ker_echelon(x, T, p);
    2324         784 :   return FlxqM_ker_gen(x, T, p, deplin);
    2325             : }
    2326             : 
    2327             : GEN
    2328        2583 : FlxqM_ker(GEN x, GEN T, ulong p)
    2329             : {
    2330        2583 :   return FlxqM_ker_i(x, T, p, 0);
    2331             : }
    2332             : 
    2333             : GEN
    2334          35 : FlxqM_deplin(GEN x, GEN T, ulong p)
    2335             : {
    2336          35 :   return FlxqM_ker_i(x, T, p, 1);
    2337             : }
    2338             : 
    2339             : static GEN
    2340          35 : F2xqM_ker_i(GEN x, GEN T, long deplin)
    2341             : {
    2342             :   const struct bb_field *ff;
    2343             :   void *E;
    2344             : 
    2345          35 :   if (lg(x)==1) return cgetg(1,t_MAT);
    2346          35 :   ff = get_F2xq_field(&E,T);
    2347          35 :   return gen_ker(x,deplin, E, ff);
    2348             : }
    2349             : 
    2350             : GEN
    2351          21 : F2xqM_ker(GEN x, GEN T)
    2352             : {
    2353          21 :   return F2xqM_ker_i(x, T, 0);
    2354             : }
    2355             : 
    2356             : GEN
    2357          14 : F2xqM_deplin(GEN x, GEN T)
    2358             : {
    2359          14 :   return F2xqM_ker_i(x, T, 1);
    2360             : }
    2361             : 
    2362             : static GEN
    2363          28 : F2xqM_gauss_pivot(GEN x, GEN T, long *rr)
    2364             : {
    2365             :   void *E;
    2366          28 :   const struct bb_field *S = get_F2xq_field(&E,T);
    2367          28 :   return gen_Gauss_pivot(x, rr, E, S);
    2368             : }
    2369             : GEN
    2370           7 : F2xqM_image(GEN x, GEN T)
    2371             : {
    2372             :   long r;
    2373           7 :   GEN d = F2xqM_gauss_pivot(x,T,&r); /* d left on stack for efficiency */
    2374           7 :   return image_from_pivot(x,d,r);
    2375             : }
    2376             : long
    2377           7 : F2xqM_rank(GEN x, GEN T)
    2378             : {
    2379           7 :   pari_sp av = avma;
    2380             :   long r;
    2381           7 :   (void)F2xqM_gauss_pivot(x,T,&r);
    2382           7 :   avma = av; return lg(x)-1 - r;
    2383             : }
    2384             : /*******************************************************************/
    2385             : /*                                                                 */
    2386             : /*                       Solve A*X=B (Gauss pivot)                 */
    2387             : /*                                                                 */
    2388             : /*******************************************************************/
    2389             : /* x ~ 0 compared to reference y */
    2390             : int
    2391      574920 : approx_0(GEN x, GEN y)
    2392             : {
    2393      574920 :   long tx = typ(x);
    2394      574920 :   if (tx == t_COMPLEX)
    2395          42 :     return approx_0(gel(x,1), y) && approx_0(gel(x,2), y);
    2396      575025 :   return gequal0(x) ||
    2397      397678 :          (tx == t_REAL && gexpo(y) - gexpo(x) > bit_prec(x));
    2398             : }
    2399             : /* x a column, x0 same column in the original input matrix (for reference),
    2400             :  * c list of pivots so far */
    2401             : static long
    2402      587793 : gauss_get_pivot_max(GEN X, GEN X0, long ix, GEN c)
    2403             : {
    2404      587793 :   GEN p, r, x = gel(X,ix), x0 = gel(X0,ix);
    2405      587793 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2406      587793 :   if (c)
    2407             :   {
    2408       81970 :     for (i=1; i<lx; i++)
    2409       53697 :       if (!c[i])
    2410             :       {
    2411       27468 :         long e = gexpo(gel(x,i));
    2412       27468 :         if (e > ex) { ex = e; k = i; }
    2413             :       }
    2414             :   }
    2415             :   else
    2416             :   {
    2417     1922199 :     for (i=ix; i<lx; i++)
    2418             :     {
    2419     1362679 :       long e = gexpo(gel(x,i));
    2420     1362679 :       if (e > ex) { ex = e; k = i; }
    2421             :     }
    2422             :   }
    2423      587793 :   if (!k) return lx;
    2424      574871 :   p = gel(x,k);
    2425      574871 :   r = gel(x0,k); if (isrationalzero(r)) r = x0;
    2426      574871 :   return approx_0(p, r)? lx: k;
    2427             : }
    2428             : static long
    2429       64575 : gauss_get_pivot_padic(GEN X, GEN p, long ix, GEN c)
    2430             : {
    2431       64575 :   GEN x = gel(X, ix);
    2432       64575 :   long i, k = 0, ex = (long)HIGHVALPBIT, lx = lg(x);
    2433       64575 :   if (c)
    2434             :   {
    2435         504 :     for (i=1; i<lx; i++)
    2436         378 :       if (!c[i] && !gequal0(gel(x,i)))
    2437             :       {
    2438         245 :         long e = gvaluation(gel(x,i), p);
    2439         245 :         if (e < ex) { ex = e; k = i; }
    2440             :       }
    2441             :   }
    2442             :   else
    2443             :   {
    2444      455497 :     for (i=ix; i<lx; i++)
    2445      391048 :       if (!gequal0(gel(x,i)))
    2446             :       {
    2447      187116 :         long e = gvaluation(gel(x,i), p);
    2448      187116 :         if (e < ex) { ex = e; k = i; }
    2449             :       }
    2450             :   }
    2451       64575 :   return k? k: lx;
    2452             : }
    2453             : static long
    2454        4452 : gauss_get_pivot_NZ(GEN X, GEN x0/*unused*/, long ix, GEN c)
    2455             : {
    2456        4452 :   GEN x = gel(X, ix);
    2457        4452 :   long i, lx = lg(x);
    2458             :   (void)x0;
    2459        4452 :   if (c)
    2460             :   {
    2461       11998 :     for (i=1; i<lx; i++)
    2462       11144 :       if (!c[i] && !gequal0(gel(x,i))) return i;
    2463             :   }
    2464             :   else
    2465             :   {
    2466        2639 :     for (i=ix; i<lx; i++)
    2467        2625 :       if (!gequal0(gel(x,i))) return i;
    2468             :   }
    2469         868 :   return lx;
    2470             : }
    2471             : 
    2472             : /* Return pivot seeking function appropriate for the domain of the RgM x
    2473             :  * (first non zero pivot, maximal pivot...)
    2474             :  * x0 is a reference point used when guessing whether x[i,j] ~ 0
    2475             :  * (iff x[i,j] << x0[i,j]); typical case: mateigen, Gauss pivot on x - vp.Id,
    2476             :  * but use original x when deciding whether a prospective pivot is non-0 */
    2477             : static pivot_fun
    2478      191825 : get_pivot_fun(GEN x, GEN x0, GEN *data)
    2479             : {
    2480      191825 :   long i, j, hx, lx = lg(x);
    2481      191825 :   int res = t_INT;
    2482      191825 :   GEN p = NULL;
    2483             : 
    2484      191825 :   *data = NULL;
    2485      191825 :   if (lx == 1) return &gauss_get_pivot_NZ;
    2486      191790 :   hx = lgcols(x);
    2487      859617 :   for (j=1; j<lx; j++)
    2488             :   {
    2489      667855 :     GEN xj = gel(x,j);
    2490     3650183 :     for (i=1; i<hx; i++)
    2491             :     {
    2492     2982356 :       GEN c = gel(xj,i);
    2493     2982356 :       switch(typ(c))
    2494             :       {
    2495             :         case t_REAL:
    2496     1572957 :           res = t_REAL;
    2497     1572957 :           break;
    2498             :         case t_COMPLEX:
    2499         266 :           if (typ(gel(c,1)) == t_REAL || typ(gel(c,2)) == t_REAL) res = t_REAL;
    2500         266 :           break;
    2501             :         case t_INT: case t_INTMOD: case t_FRAC: case t_FFELT: case t_QUAD:
    2502             :         case t_POLMOD: /* exact types */
    2503     1245858 :           break;
    2504             :         case t_PADIC:
    2505      163247 :           p = gel(c,2);
    2506      163247 :           res = t_PADIC;
    2507      163247 :           break;
    2508          28 :         default: return &gauss_get_pivot_NZ;
    2509             :       }
    2510             :     }
    2511             :   }
    2512      191762 :   switch(res)
    2513             :   {
    2514      181612 :     case t_REAL: *data = x0; return &gauss_get_pivot_max;
    2515        8365 :     case t_PADIC: *data = p; return &gauss_get_pivot_padic;
    2516        1785 :     default: return &gauss_get_pivot_NZ;
    2517             :   }
    2518             : }
    2519             : 
    2520             : static GEN
    2521      186277 : get_col(GEN a, GEN b, GEN p, long li)
    2522             : {
    2523      186277 :   GEN u = cgetg(li+1,t_COL);
    2524             :   long i, j;
    2525             : 
    2526      186277 :   gel(u,li) = gdiv(gel(b,li), p);
    2527      763583 :   for (i=li-1; i>0; i--)
    2528             :   {
    2529      577306 :     pari_sp av = avma;
    2530      577306 :     GEN m = gel(b,i);
    2531      577306 :     for (j=i+1; j<=li; j++) m = gsub(m, gmul(gcoeff(a,i,j), gel(u,j)));
    2532      577306 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(a,i,i)));
    2533             :   }
    2534      186277 :   return u;
    2535             : }
    2536             : /* assume 0 <= a[i,j] < p */
    2537             : static GEN
    2538      368008 : Fl_get_col_OK(GEN a, GEN b, long li, ulong p)
    2539             : {
    2540      368008 :   GEN u = cgetg(li+1,t_VECSMALL);
    2541      368008 :   ulong m = uel(b,li) % p;
    2542             :   long i,j;
    2543             : 
    2544      368008 :   uel(u,li) = (m * ucoeff(a,li,li)) % p;
    2545     4062874 :   for (i = li-1; i > 0; i--)
    2546             :   {
    2547     3694866 :     m = p - uel(b,i)%p;
    2548    33584907 :     for (j = i+1; j <= li; j++) {
    2549    29890041 :       if (m & HIGHBIT) m %= p;
    2550    29890041 :       m += ucoeff(a,i,j) * uel(u,j); /* 0 <= u[j] < p */
    2551             :     }
    2552     3694866 :     m %= p;
    2553     3694866 :     if (m) m = ((p-m) * ucoeff(a,i,i)) % p;
    2554     3694866 :     uel(u,i) = m;
    2555             :   }
    2556      368008 :   return u;
    2557             : }
    2558             : static GEN
    2559     1457023 : Fl_get_col(GEN a, GEN b, long li, ulong p)
    2560             : {
    2561     1457023 :   GEN u = cgetg(li+1,t_VECSMALL);
    2562     1457023 :   ulong m = uel(b,li) % p;
    2563             :   long i,j;
    2564             : 
    2565     1457023 :   uel(u,li) = Fl_mul(m, ucoeff(a,li,li), p);
    2566     4299442 :   for (i=li-1; i>0; i--)
    2567             :   {
    2568     2842419 :     m = b[i]%p;
    2569     7900870 :     for (j = i+1; j <= li; j++)
    2570     5058451 :       m = Fl_sub(m, Fl_mul(ucoeff(a,i,j), uel(u,j), p), p);
    2571     2842419 :     if (m) m = Fl_mul(m, ucoeff(a,i,i), p);
    2572     2842419 :     uel(u,i) = m;
    2573             :   }
    2574     1457023 :   return u;
    2575             : }
    2576             : 
    2577             : /* bk -= m * bi */
    2578             : static void
    2579     2825714 : _submul(GEN b, long k, long i, GEN m)
    2580             : {
    2581     2825714 :   gel(b,k) = gsub(gel(b,k), gmul(m, gel(b,i)));
    2582     2825714 : }
    2583             : static int
    2584      749843 : init_gauss(GEN a, GEN *b, long *aco, long *li, int *iscol)
    2585             : {
    2586      749843 :   *iscol = *b ? (typ(*b) == t_COL): 0;
    2587      749843 :   *aco = lg(a) - 1;
    2588      749843 :   if (!*aco) /* a empty */
    2589             :   {
    2590          70 :     if (*b && lg(*b) != 1) pari_err_DIM("gauss");
    2591          70 :     *li = 0; return 0;
    2592             :   }
    2593      749773 :   *li = nbrows(a);
    2594      749773 :   if (*li < *aco) pari_err_INV("gauss [no left inverse]", a);
    2595      749773 :   if (*b)
    2596             :   {
    2597      741148 :     switch(typ(*b))
    2598             :     {
    2599             :       case t_MAT:
    2600       84105 :         if (lg(*b) == 1) return 0;
    2601       84105 :         *b = RgM_shallowcopy(*b);
    2602       84105 :         break;
    2603             :       case t_COL:
    2604      657043 :         *b = mkmat( leafcopy(*b) );
    2605      657043 :         break;
    2606           0 :       default: pari_err_TYPE("gauss",*b);
    2607             :     }
    2608      741148 :     if (nbrows(*b) != *li) pari_err_DIM("gauss");
    2609             :   }
    2610             :   else
    2611        8625 :     *b = matid(*li);
    2612      749773 :   return 1;
    2613             : }
    2614             : 
    2615             : static GEN Flm_inv_sp(GEN a, ulong *detp, ulong p);
    2616             : static GEN
    2617       28070 : RgM_inv_QM(GEN M)
    2618             : {
    2619       28070 :   pari_sp av = avma;
    2620       28070 :   GEN den, cM, pM = Q_primitive_part(M, &cM);
    2621       28070 :   GEN b = ZM_inv(pM, &den);
    2622       28063 :   if (!b) { avma = av; return NULL; }
    2623       28056 :   if (cM) den = gmul(den, cM);
    2624       28056 :   if (!gequal1(den)) b = ZM_Q_mul(b, ginv(den));
    2625       28056 :   return gerepileupto(av, b);
    2626             : }
    2627             : 
    2628             : static GEN
    2629         112 : RgM_inv_FpM(GEN a, GEN p)
    2630             : {
    2631             :   ulong pp;
    2632         112 :   a = RgM_Fp_init(a, p, &pp);
    2633         112 :   switch(pp)
    2634             :   {
    2635             :   case 0:
    2636          35 :     a = FpM_inv(a,p);
    2637          35 :     if (a) a = FpM_to_mod(a, p);
    2638          35 :     break;
    2639             :   case 2:
    2640          35 :     a = F2m_inv(a);
    2641          35 :     if (a) a = F2m_to_mod(a);
    2642          35 :     break;
    2643             :   default:
    2644          42 :     a = Flm_inv_sp(a, NULL, pp);
    2645          42 :     if (a) a = Flm_to_mod(a, pp);
    2646             :   }
    2647         112 :   return a;
    2648             : }
    2649             : 
    2650             : static GEN
    2651          42 : RgM_inv_FqM(GEN x, GEN pol, GEN p)
    2652             : {
    2653          42 :   pari_sp av = avma;
    2654          42 :   GEN T = RgX_to_FpX(pol, p);
    2655          42 :   GEN b = FqM_inv(RgM_to_FqM(x, T, p), T, p);
    2656          42 :   if (!b) { avma = av; return NULL; }
    2657          28 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    2658             : }
    2659             : 
    2660             : #define code(t1,t2) ((t1 << 6) | t2)
    2661             : static GEN
    2662       45424 : RgM_inv_fast(GEN x)
    2663             : {
    2664             :   GEN p, pol;
    2665             :   long pa;
    2666       45424 :   long t = RgM_type(x, &p,&pol,&pa);
    2667       45424 :   switch(t)
    2668             :   {
    2669             :     case t_INT:    /* Fall back */
    2670       28070 :     case t_FRAC:   return RgM_inv_QM(x);
    2671         147 :     case t_FFELT:  return FFM_inv(x, pol);
    2672         112 :     case t_INTMOD: return RgM_inv_FpM(x, p);
    2673             :     case code(t_POLMOD, t_INTMOD):
    2674          42 :                    return RgM_inv_FqM(x, pol, p);
    2675       17053 :     default:       return gen_0;
    2676             :   }
    2677             : }
    2678             : #undef code
    2679             : 
    2680             : static GEN
    2681          49 : RgM_RgC_solve_FpC(GEN a, GEN b, GEN p)
    2682             : {
    2683          49 :   pari_sp av = avma;
    2684             :   ulong pp;
    2685          49 :   a = RgM_Fp_init(a, p, &pp);
    2686          49 :   switch(pp)
    2687             :   {
    2688             :   case 0:
    2689          14 :     b = RgC_to_FpC(b, p);
    2690          14 :     a = FpM_FpC_gauss(a,b,p);
    2691          14 :     return a ? gerepileupto(av, FpC_to_mod(a, p)): NULL;
    2692             :   case 2:
    2693          14 :     b = RgV_to_F2v(b);
    2694          14 :     a = F2m_F2c_gauss(a,b);
    2695          14 :     return a ? gerepileupto(av, F2c_to_mod(a)): NULL;
    2696             :   default:
    2697          21 :     b = RgV_to_Flv(b, pp);
    2698          21 :     a = Flm_Flc_gauss(a, b, pp);
    2699          21 :     return a ? gerepileupto(av, Flc_to_mod(a, pp)): NULL;
    2700             :   }
    2701             : }
    2702             : 
    2703             : static GEN
    2704          98 : RgM_solve_FpM(GEN a, GEN b, GEN p)
    2705             : {
    2706          98 :   pari_sp av = avma;
    2707             :   ulong pp;
    2708          98 :   a = RgM_Fp_init(a, p, &pp);
    2709          98 :   switch(pp)
    2710             :   {
    2711             :   case 0:
    2712          35 :     b = RgM_to_FpM(b, p);
    2713          35 :     a = FpM_gauss(a,b,p);
    2714          35 :     return a ? gerepileupto(av, FpM_to_mod(a, p)): NULL;
    2715             :   case 2:
    2716          21 :     b = RgM_to_F2m(b);
    2717          21 :     a = F2m_gauss(a,b);
    2718          21 :     return a ? gerepileupto(av, F2m_to_mod(a)): NULL;
    2719             :   default:
    2720          42 :     b = RgM_to_Flm(b, pp);
    2721          42 :     a = Flm_gauss(a,b,pp);
    2722          42 :     return a ? gerepileupto(av, Flm_to_mod(a, pp)): NULL;
    2723             :   }
    2724             : }
    2725             : 
    2726             : /* Gaussan Elimination. If a is square, return a^(-1)*b;
    2727             :  * if a has more rows than columns and b is NULL, return c such that c a = Id.
    2728             :  * a is a (not necessarily square) matrix
    2729             :  * b is a matrix or column vector, NULL meaning: take the identity matrix,
    2730             :  *   effectively returning the inverse of a
    2731             :  * If a and b are empty, the result is the empty matrix.
    2732             :  *
    2733             :  * li: number of rows of a and b
    2734             :  * aco: number of columns of a
    2735             :  * bco: number of columns of b (if matrix)
    2736             :  */
    2737             : static GEN
    2738      267610 : RgM_solve_basecase(GEN a, GEN b)
    2739             : {
    2740      267610 :   pari_sp av = avma;
    2741             :   long i, j, k, li, bco, aco;
    2742             :   int iscol;
    2743             :   pivot_fun pivot;
    2744             :   GEN p, u, data;
    2745             : 
    2746      267610 :   avma = av;
    2747             : 
    2748      267610 :   if (lg(a)-1 == 2 && nbrows(a) == 2) {
    2749             :     /* 2x2 matrix, start by inverting a */
    2750       99672 :     GEN u = gcoeff(a,1,1), v = gcoeff(a,1,2);
    2751       99672 :     GEN w = gcoeff(a,2,1), x = gcoeff(a,2,2);
    2752       99672 :     GEN D = gsub(gmul(u,x), gmul(v,w)), ainv;
    2753       99672 :     if (gequal0(D)) return NULL;
    2754       99672 :     ainv = mkmat2(mkcol2(x, gneg(w)), mkcol2(gneg(v), u));
    2755       99672 :     ainv = gmul(ainv, ginv(D));
    2756       99672 :     if (b) ainv = gmul(ainv, b);
    2757       99672 :     return gerepileupto(av, ainv);
    2758             :   }
    2759             : 
    2760      167938 :   if (!init_gauss(a, &b, &aco, &li, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    2761      167938 :   pivot = get_pivot_fun(a, a, &data);
    2762      167938 :   a = RgM_shallowcopy(a);
    2763      167938 :   bco = lg(b)-1;
    2764      167938 :   if(DEBUGLEVEL>4) err_printf("Entering gauss\n");
    2765             : 
    2766      167938 :   p = NULL; /* gcc -Wall */
    2767      555117 :   for (i=1; i<=aco; i++)
    2768             :   {
    2769             :     /* k is the line where we find the pivot */
    2770      555117 :     k = pivot(a, data, i, NULL);
    2771      555117 :     if (k > li) return NULL;
    2772      555103 :     if (k != i)
    2773             :     { /* exchange the lines s.t. k = i */
    2774      122967 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    2775      122967 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    2776             :     }
    2777      555103 :     p = gcoeff(a,i,i);
    2778      555103 :     if (i == aco) break;
    2779             : 
    2780     1181786 :     for (k=i+1; k<=li; k++)
    2781             :     {
    2782      794607 :       GEN m = gcoeff(a,k,i);
    2783      794607 :       if (!gequal0(m))
    2784             :       {
    2785      628537 :         m = gdiv(m,p);
    2786      628537 :         for (j=i+1; j<=aco; j++) _submul(gel(a,j),k,i,m);
    2787      628537 :         for (j=1;   j<=bco; j++) _submul(gel(b,j),k,i,m);
    2788             :       }
    2789             :     }
    2790      387179 :     if (gc_needed(av,1))
    2791             :     {
    2792          62 :       if(DEBUGMEM>1) pari_warn(warnmem,"gauss. i=%ld",i);
    2793          62 :       gerepileall(av,2, &a,&b);
    2794             :     }
    2795             :   }
    2796             : 
    2797      167924 :   if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
    2798      167924 :   u = cgetg(bco+1,t_MAT);
    2799      167924 :   for (j=1; j<=bco; j++) gel(u,j) = get_col(a,gel(b,j),p,aco);
    2800      167924 :   return gerepilecopy(av, iscol? gel(u,1): u);
    2801             : }
    2802             : 
    2803             : static GEN
    2804      261177 : RgM_RgC_solve_fast(GEN x, GEN y)
    2805             : {
    2806             :   GEN p, pol;
    2807             :   long pa;
    2808      261177 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    2809      261177 :   switch(t)
    2810             :   {
    2811       13349 :     case t_INT:    return ZM_gauss(x, y);
    2812          42 :     case t_FRAC:   return QM_gauss(x, y);
    2813          49 :     case t_INTMOD: return RgM_RgC_solve_FpC(x, y, p);
    2814          56 :     case t_FFELT:  return FFM_FFC_gauss(x, y, pol);
    2815      247681 :     default:       return gen_0;
    2816             :   }
    2817             : }
    2818             : 
    2819             : static GEN
    2820       79409 : RgM_solve_fast(GEN x, GEN y)
    2821             : {
    2822             :   GEN p, pol;
    2823             :   long pa;
    2824       79409 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    2825       79409 :   switch(t)
    2826             :   {
    2827       76365 :     case t_INT:    return ZM_gauss(x, y);
    2828           7 :     case t_FRAC:   return QM_gauss(x, y);
    2829          98 :     case t_INTMOD: return RgM_solve_FpM(x, y, p);
    2830          63 :     case t_FFELT:  return FFM_gauss(x, y, pol);
    2831        2876 :     default:       return gen_0;
    2832             :   }
    2833             : }
    2834             : 
    2835             : GEN
    2836      340586 : RgM_solve(GEN a, GEN b)
    2837             : {
    2838      340586 :   pari_sp av = avma;
    2839             :   GEN u;
    2840      340586 :   if (!b) return RgM_inv(a);
    2841      340586 :   u = typ(b)==t_MAT ? RgM_solve_fast(a, b): RgM_RgC_solve_fast(a, b);
    2842      340586 :   if (!u) { avma = av; return u; }
    2843      340488 :   if (u != gen_0) return u;
    2844      250557 :   return RgM_solve_basecase(a, b);
    2845             : }
    2846             : 
    2847             : GEN
    2848       45424 : RgM_inv(GEN a)
    2849             : {
    2850       45424 :   GEN b = RgM_inv_fast(a);
    2851       45417 :   return b==gen_0? RgM_solve_basecase(a, NULL): b;
    2852             : }
    2853             : 
    2854             : /* assume dim A >= 1, A invertible + upper triangular  */
    2855             : static GEN
    2856      330338 : RgM_inv_upper_ind(GEN A, long index)
    2857             : {
    2858      330338 :   long n = lg(A)-1, i = index, j;
    2859      330338 :   GEN u = zerocol(n);
    2860      330338 :   gel(u,i) = ginv(gcoeff(A,i,i));
    2861     1366628 :   for (i--; i>0; i--)
    2862             :   {
    2863     1036290 :     pari_sp av = avma;
    2864     1036290 :     GEN m = gneg(gmul(gcoeff(A,i,i+1),gel(u,i+1))); /* j = i+1 */
    2865     1036290 :     for (j=i+2; j<=n; j++) m = gsub(m, gmul(gcoeff(A,i,j),gel(u,j)));
    2866     1036290 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(A,i,i)));
    2867             :   }
    2868      330338 :   return u;
    2869             : }
    2870             : GEN
    2871       58940 : RgM_inv_upper(GEN A)
    2872             : {
    2873             :   long i, l;
    2874       58940 :   GEN B = cgetg_copy(A, &l);
    2875       58940 :   for (i = 1; i < l; i++) gel(B,i) = RgM_inv_upper_ind(A, i);
    2876       58940 :   return B;
    2877             : }
    2878             : 
    2879             : /* assume dim A >= 1, A invertible + upper triangular, 1s on diagonal  */
    2880             : static GEN
    2881          98 : FpM_inv_upper_1_ind(GEN A, long index, GEN p)
    2882             : {
    2883          98 :   long n = lg(A)-1, i = index, j;
    2884          98 :   GEN u = zerocol(n);
    2885          98 :   gel(u,i) = gen_1;
    2886         567 :   for (i--; i>0; i--)
    2887             :   {
    2888         469 :     pari_sp av = avma;
    2889         469 :     GEN m = negi(mulii(gcoeff(A,i,i+1),gel(u,i+1))); /* j = i+1 */
    2890         469 :     for (j=i+2; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2891         469 :     gel(u,i) = gerepileuptoint(av, modii(m,p));
    2892             :   }
    2893          98 :   return u;
    2894             : }
    2895             : static GEN
    2896          14 : FpM_inv_upper_1(GEN A, GEN p)
    2897             : {
    2898             :   long i, l;
    2899          14 :   GEN B = cgetg_copy(A, &l);
    2900          14 :   for (i = 1; i < l; i++) gel(B,i) = FpM_inv_upper_1_ind(A, i, p);
    2901          14 :   return B;
    2902             : }
    2903             : /* assume dim A >= 1, A invertible + upper triangular, 1s on diagonal,
    2904             :  * reduced mod p */
    2905             : static GEN
    2906          28 : Flm_inv_upper_1_ind(GEN A, long index, ulong p)
    2907             : {
    2908          28 :   long n = lg(A)-1, i = index, j;
    2909          28 :   GEN u = const_vecsmall(n, 0);
    2910          28 :   u[i] = 1;
    2911          28 :   if (SMALL_ULONG(p))
    2912          21 :     for (i--; i>0; i--)
    2913             :     {
    2914           7 :       ulong m = ucoeff(A,i,i+1) * uel(u,i+1); /* j = i+1 */
    2915           7 :       for (j=i+2; j<=n; j++)
    2916             :       {
    2917           0 :         if (m & HIGHMASK) m %= p;
    2918           0 :         m += ucoeff(A,i,j) * uel(u,j);
    2919             :       }
    2920           7 :       u[i] = Fl_neg(m % p, p);
    2921             :     }
    2922             :   else
    2923          21 :     for (i--; i>0; i--)
    2924             :     {
    2925           7 :       ulong m = Fl_mul(ucoeff(A,i,i+1),uel(u,i+1), p); /* j = i+1 */
    2926           7 :       for (j=i+2; j<=n; j++) m = Fl_add(m, Fl_mul(ucoeff(A,i,j),uel(u,j),p), p);
    2927           7 :       u[i] = Fl_neg(m, p);
    2928             :     }
    2929          28 :   return u;
    2930             : }
    2931             : static GEN
    2932          14 : F2m_inv_upper_1_ind(GEN A, long index)
    2933             : {
    2934          14 :   pari_sp av = avma;
    2935          14 :   long n = lg(A)-1, i = index, j;
    2936          14 :   GEN u = const_vecsmall(n, 0);
    2937          14 :   u[i] = 1;
    2938          21 :   for (i--; i>0; i--)
    2939             :   {
    2940           7 :     ulong m = F2m_coeff(A,i,i+1) & uel(u,i+1); /* j = i+1 */
    2941           7 :     for (j=i+2; j<=n; j++) m ^= F2m_coeff(A,i,j) & uel(u,j);
    2942           7 :     u[i] = m & 1;
    2943             :   }
    2944          14 :   return gerepileuptoleaf(av, Flv_to_F2v(u));
    2945             : }
    2946             : static GEN
    2947          14 : Flm_inv_upper_1(GEN A, ulong p)
    2948             : {
    2949             :   long i, l;
    2950          14 :   GEN B = cgetg_copy(A, &l);
    2951          14 :   for (i = 1; i < l; i++) gel(B,i) = Flm_inv_upper_1_ind(A, i, p);
    2952          14 :   return B;
    2953             : }
    2954             : static GEN
    2955           7 : F2m_inv_upper_1(GEN A)
    2956             : {
    2957             :   long i, l;
    2958           7 :   GEN B = cgetg_copy(A, &l);
    2959           7 :   for (i = 1; i < l; i++) gel(B,i) = F2m_inv_upper_1_ind(A, i);
    2960           7 :   return B;
    2961             : }
    2962             : 
    2963             : static GEN
    2964      972083 : split_realimag_col(GEN z, long r1, long r2)
    2965             : {
    2966      972083 :   long i, ru = r1+r2;
    2967      972083 :   GEN x = cgetg(ru+r2+1,t_COL), y = x + r2;
    2968     2912788 :   for (i=1; i<=r1; i++) {
    2969     1940705 :     GEN a = gel(z,i);
    2970     1940705 :     if (typ(a) == t_COMPLEX) a = gel(a,1); /* paranoia: a should be real */
    2971     1940705 :     gel(x,i) = a;
    2972             :   }
    2973     1611453 :   for (   ; i<=ru; i++) {
    2974      639370 :     GEN b, a = gel(z,i);
    2975      639370 :     if (typ(a) == t_COMPLEX) { b = gel(a,2); a = gel(a,1); } else b = gen_0;
    2976      639370 :     gel(x,i) = a;
    2977      639370 :     gel(y,i) = b;
    2978             :   }
    2979      972083 :   return x;
    2980             : }
    2981             : GEN
    2982      510641 : split_realimag(GEN x, long r1, long r2)
    2983             : {
    2984             :   long i,l; GEN y;
    2985      510641 :   if (typ(x) == t_COL) return split_realimag_col(x,r1,r2);
    2986      251725 :   y = cgetg_copy(x, &l);
    2987      251725 :   for (i=1; i<l; i++) gel(y,i) = split_realimag_col(gel(x,i), r1, r2);
    2988      251725 :   return y;
    2989             : }
    2990             : 
    2991             : /* assume M = (r1+r2) x (r1+2r2) matrix and y compatible vector or matrix
    2992             :  * r1 first lines of M,y are real. Solve the system obtained by splitting
    2993             :  * real and imaginary parts. */
    2994             : GEN
    2995      248275 : RgM_solve_realimag(GEN M, GEN y)
    2996             : {
    2997      248275 :   long l = lg(M), r2 = l - lgcols(M), r1 = l-1 - 2*r2;
    2998      248275 :   return RgM_solve(split_realimag(M, r1,r2),
    2999             :                    split_realimag(y, r1,r2));
    3000             : }
    3001             : 
    3002             : GEN
    3003         420 : gauss(GEN a, GEN b)
    3004             : {
    3005             :   GEN z;
    3006         420 :   long t = typ(b);
    3007         420 :   if (typ(a)!=t_MAT) pari_err_TYPE("gauss",a);
    3008         420 :   if (t!=t_COL && t!=t_MAT) pari_err_TYPE("gauss",b);
    3009         420 :   z = RgM_solve(a,b);
    3010         420 :   if (!z) pari_err_INV("gauss",a);
    3011         315 :   return z;
    3012             : }
    3013             : 
    3014             : static GEN
    3015      131994 : F2_get_col(GEN b, GEN d, long li, long aco)
    3016             : {
    3017      131994 :   long i, l = nbits2lg(aco);
    3018      131994 :   GEN u = cgetg(l, t_VECSMALL);
    3019      131994 :   u[1] = aco;
    3020     1829160 :   for (i = 1; i <= li; i++)
    3021     1697166 :     if (d[i]) /* d[i] can still be 0 if li > aco */
    3022             :     {
    3023     1628328 :       if (F2v_coeff(b, i))
    3024      523043 :         F2v_set(u, d[i]);
    3025             :       else
    3026     1105285 :         F2v_clear(u, d[i]);
    3027             :     }
    3028      131994 :   return u;
    3029             : }
    3030             : 
    3031             : /* destroy a, b */
    3032             : static GEN
    3033       20459 : F2m_gauss_sp(GEN a, GEN b)
    3034             : {
    3035       20459 :   long i, j, k, l, li, bco, aco = lg(a)-1;
    3036             :   GEN u, d;
    3037             : 
    3038       20459 :   if (!aco) return cgetg(1,t_MAT);
    3039       20459 :   li = gel(a,1)[1];
    3040       20459 :   d = zero_Flv(li);
    3041       20459 :   bco = lg(b)-1;
    3042      143801 :   for (i=1; i<=aco; i++)
    3043             :   {
    3044      123363 :     GEN ai = vecsmall_copy(gel(a,i));
    3045      123363 :     if (!d[i] && F2v_coeff(ai, i))
    3046       60561 :       k = i;
    3047             :     else
    3048       62802 :       for (k = 1; k <= li; k++) if (!d[k] && F2v_coeff(ai,k)) break;
    3049             :     /* found a pivot on row k */
    3050      123363 :     if (k > li) return NULL;
    3051      123342 :     d[k] = i;
    3052             : 
    3053             :     /* Clear k-th row but column-wise instead of line-wise */
    3054             :     /*  a_ij -= a_i1*a1j/a_11
    3055             :        line-wise grouping:  L_j -= a_1j/a_11*L_1
    3056             :        column-wise:         C_i -= a_i1/a_11*C_1
    3057             :     */
    3058      123342 :     F2v_clear(ai,k);
    3059     1734555 :     for (l=1; l<=aco; l++)
    3060             :     {
    3061     1611213 :       GEN al = gel(a,l);
    3062     1611213 :       if (F2v_coeff(al,k)) F2v_add_inplace(al,ai);
    3063             :     }
    3064     1751929 :     for (l=1; l<=bco; l++)
    3065             :     {
    3066     1628587 :       GEN bl = gel(b,l);
    3067     1628587 :       if (F2v_coeff(bl,k)) F2v_add_inplace(bl,ai);
    3068             :     }
    3069             :   }
    3070       20438 :   u = cgetg(bco+1,t_MAT);
    3071       20438 :   for (j = 1; j <= bco; j++) gel(u,j) = F2_get_col(gel(b,j), d, li, aco);
    3072       20438 :   return u;
    3073             : }
    3074             : 
    3075             : GEN
    3076          35 : F2m_gauss(GEN a, GEN b)
    3077             : {
    3078          35 :   pari_sp av = avma;
    3079          35 :   if (lg(a) == 1) return cgetg(1,t_MAT);
    3080          35 :   return gerepileupto(av, F2m_gauss_sp(F2m_copy(a), F2m_copy(b)));
    3081             : }
    3082             : GEN
    3083          14 : F2m_F2c_gauss(GEN a, GEN b)
    3084             : {
    3085          14 :   pari_sp av = avma;
    3086          14 :   GEN z = F2m_gauss(a, mkmat(b));
    3087          14 :   if (!z) { avma = av; return NULL; }
    3088           7 :   if (lg(z) == 1) { avma = av; return cgetg(1,t_VECSMALL); }
    3089           7 :   return gerepileuptoleaf(av, gel(z,1));
    3090             : }
    3091             : 
    3092             : GEN
    3093          35 : F2m_inv(GEN a)
    3094             : {
    3095          35 :   pari_sp av = avma;
    3096          35 :   if (lg(a) == 1) return cgetg(1,t_MAT);
    3097          35 :   return gerepileupto(av, F2m_gauss_sp(F2m_copy(a), matid_F2m(gel(a,1)[1])));
    3098             : }
    3099             : 
    3100             : /* destroy a, b */
    3101             : static GEN
    3102       51533 : Flm_gauss_sp_OK(GEN a, GEN b, ulong *detp, ulong p)
    3103             : {
    3104       51533 :   long i, j, k, li, bco, aco = lg(a)-1, s = 1;
    3105       51533 :   ulong det = 1;
    3106             :   GEN u;
    3107             : 
    3108       51533 :   li = nbrows(a);
    3109       51533 :   bco = lg(b)-1;
    3110      354097 :   for (i=1; i<=aco; i++)
    3111             :   {
    3112             :     ulong invpiv;
    3113             :     /* Fl_get_col wants 0 <= a[i,j] < p for all i,j */
    3114      354097 :     for (k = 1; k < i; k++) ucoeff(a,k,i) %= p;
    3115      609716 :     for (k = i; k <= li; k++)
    3116             :     {
    3117      609709 :       ulong piv = ( ucoeff(a,k,i) %= p );
    3118      609709 :       if (piv)
    3119             :       {
    3120      354090 :         ucoeff(a,k,i) = Fl_inv(piv, p);
    3121      354090 :         if (detp)
    3122             :         {
    3123           0 :           if (det & HIGHMASK) det %= p;
    3124           0 :           det *= piv;
    3125             :         }
    3126      354090 :         break;
    3127             :       }
    3128             :     }
    3129             :     /* found a pivot on line k */
    3130      354097 :     if (k > li) return NULL;
    3131      354090 :     if (k != i)
    3132             :     { /* swap lines so that k = i */
    3133       99527 :       s = -s;
    3134       99527 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    3135       99527 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    3136             :     }
    3137      354090 :     if (i == aco) break;
    3138             : 
    3139      302564 :     invpiv = p - ucoeff(a,i,i); /* -1/piv mod p */
    3140     2163636 :     for (k=i+1; k<=li; k++)
    3141             :     {
    3142     1861072 :       ulong m = ( ucoeff(a,k,i) %= p );
    3143     1861072 :       if (!m) continue;
    3144             : 
    3145      610744 :       m = Fl_mul(m, invpiv, p);
    3146      610744 :       if (m == 1) {
    3147      101945 :         for (j=i+1; j<=aco; j++) _Fl_add_OK(gel(a,j),k,i, p);
    3148      101945 :         for (j=1;   j<=bco; j++) _Fl_add_OK(gel(b,j),k,i, p);
    3149             :       } else {
    3150      508799 :         for (j=i+1; j<=aco; j++) _Fl_addmul_OK(gel(a,j),k,i,m, p);
    3151      508799 :         for (j=1;   j<=bco; j++) _Fl_addmul_OK(gel(b,j),k,i,m, p);
    3152             :       }
    3153             :     }
    3154             :   }
    3155       51526 :   if (detp)
    3156             :   {
    3157           0 :     det %=  p;
    3158           0 :     if (s < 0 && det) det = p - det;
    3159           0 :     *detp = det;
    3160             :   }
    3161       51526 :   u = cgetg(bco+1,t_MAT);
    3162       51526 :   for (j=1; j<=bco; j++) gel(u,j) = Fl_get_col_OK(a,gel(b,j), aco,p);
    3163       51526 :   return u;
    3164             : }
    3165             : 
    3166             : /* destroy a, b */
    3167             : static GEN
    3168      601918 : Flm_gauss_sp(GEN a, GEN b, ulong *detp, ulong p)
    3169             : {
    3170      601918 :   long i, j, k, li, bco, aco = lg(a)-1, s = 1;
    3171      601918 :   ulong det = 1;
    3172             :   GEN u;
    3173             :   ulong pi;
    3174      601918 :   if (!aco) { if (detp) *detp = 1; return cgetg(1,t_MAT); }
    3175      601918 :   if (SMALL_ULONG(p)) return Flm_gauss_sp_OK(a, b, detp, p);
    3176      550385 :   pi = get_Fl_red(p);
    3177      550385 :   li = nbrows(a);
    3178      550385 :   bco = lg(b)-1;
    3179     1457032 :   for (i=1; i<=aco; i++)
    3180             :   {
    3181             :     ulong invpiv;
    3182             :     /* Fl_get_col wants 0 <= a[i,j] < p for all i,j */
    3183     1511661 :     for (k = i; k <= li; k++)
    3184             :     {
    3185     1511654 :       ulong piv = ucoeff(a,k,i);
    3186     1511654 :       if (piv)
    3187             :       {
    3188     1457025 :         ucoeff(a,k,i) = Fl_inv(piv, p);
    3189     1457025 :         if (detp) det = Fl_mul_pre(det, piv, p, pi);
    3190     1457025 :         break;
    3191             :       }
    3192             :     }
    3193             :     /* found a pivot on line k */
    3194     1457032 :     if (k > li) return NULL;
    3195     1457025 :     if (k != i)
    3196             :     { /* swap lines so that k = i */
    3197       47065 :       s = -s;
    3198       47065 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    3199       47065 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    3200             :     }
    3201     1457025 :     if (i == aco) break;
    3202             : 
    3203      906647 :     invpiv = p - ucoeff(a,i,i); /* -1/piv mod p */
    3204     2327920 :     for (k=i+1; k<=li; k++)
    3205             :     {
    3206     1421273 :       ulong m = ucoeff(a,k,i);
    3207     1421273 :       if (!m) continue;
    3208             : 
    3209     1150782 :       m = Fl_mul_pre(m, invpiv, p, pi);
    3210     1150782 :       if (m == 1) {
    3211       28091 :         for (j=i+1; j<=aco; j++) _Fl_add(gel(a,j),k,i, p);
    3212       28091 :         for (j=1;   j<=bco; j++) _Fl_add(gel(b,j),k,i, p);
    3213             :       } else {
    3214     1122691 :         for (j=i+1; j<=aco; j++) _Fl_addmul(gel(a,j),k,i,m, p, pi);
    3215     1122691 :         for (j=1;   j<=bco; j++) _Fl_addmul(gel(b,j),k,i,m, p, pi);
    3216             :       }
    3217             :     }
    3218             :   }
    3219      550378 :   if (detp)
    3220             :   {
    3221           0 :     if (s < 0 && det) det = p - det;
    3222           0 :     *detp = det;
    3223             :   }
    3224      550378 :   u = cgetg(bco+1,t_MAT);
    3225      550378 :   for (j=1; j<=bco; j++) gel(u,j) = Fl_get_col(a,gel(b,j), aco,p);
    3226      550378 :   return u;
    3227             : }
    3228             : 
    3229             : static GEN
    3230      183855 : Flm_gauss_from_CUP(GEN b, GEN R, GEN C, GEN U, GEN P, ulong p, ulong *detp)
    3231             : {
    3232      183855 :   GEN Y = Flm_rsolve_lower_unit(rowpermute(C, R), rowpermute(b, R), p);
    3233      183855 :   GEN X = rowpermute(Flm_rsolve_upper(U, Y, p), perm_inv(P));
    3234      183854 :   if (detp)
    3235             :   {
    3236      141027 :     ulong d = perm_sign(P) == 1? 1: p-1;
    3237      141029 :     long i, r = lg(R);
    3238     1352135 :     for (i = 1; i < r; i++)
    3239     1211107 :       d = Fl_mul(d, ucoeff(U, i, i), p);
    3240      141028 :     *detp = d;
    3241             :   }
    3242      183855 :   return X;
    3243             : }
    3244             : 
    3245             : static GEN
    3246       42841 : Flm_gauss_CUP(GEN a, GEN b, ulong *detp, ulong p) {
    3247             :   GEN R, C, U, P;
    3248       42841 :   long n = lg(a) - 1, r;
    3249       42841 :   if (nbrows(a) < n || (r = Flm_CUP(a, &R, &C, &U, &P, p)) < n)
    3250          14 :     return NULL;
    3251       42827 :   return Flm_gauss_from_CUP(b, R, C, U, P, p, detp);
    3252             : }
    3253             : 
    3254             : GEN
    3255          63 : Flm_gauss(GEN a, GEN b, ulong p) {
    3256          63 :   pari_sp av = avma;
    3257             :   GEN x;
    3258          63 :   if (lg(a) - 1 >= Flm_CUP_LIMIT)
    3259          14 :     x = Flm_gauss_CUP(a, b, NULL, p);
    3260             :   else {
    3261          49 :     a = RgM_shallowcopy(a);
    3262          49 :     b = RgM_shallowcopy(b);
    3263          49 :     x = Flm_gauss_sp(a, b, NULL, p);
    3264             :   }
    3265          63 :   if (!x) { avma = av; return NULL; }
    3266          49 :   return gerepileupto(av, x);
    3267             : }
    3268             : 
    3269             : static GEN
    3270      603346 : Flm_inv_i(GEN a, ulong *detp, ulong p, long inplace) {
    3271      603346 :   pari_sp av = avma;
    3272      603346 :   long n = lg(a) - 1;
    3273             :   GEN b, x;
    3274      603346 :   if (n == 0) return cgetg(1, t_MAT);
    3275      603346 :   b = matid_Flm(nbrows(a));
    3276      603346 :   if (n >= Flm_CUP_LIMIT)
    3277       42827 :     x = Flm_gauss_CUP(a, b, detp, p);
    3278             :   else {
    3279      560519 :     if (!inplace)
    3280       10178 :       a = RgM_shallowcopy(a);
    3281      560519 :     x = Flm_gauss_sp(a, b, detp, p);
    3282             :   }
    3283      603346 :   if (!x) { avma = av; return NULL; }
    3284      603332 :   return gerepileupto(av, x);
    3285             : }
    3286             : 
    3287             : static GEN
    3288      581856 : Flm_inv_sp(GEN a, ulong *detp, ulong p) {
    3289      581856 :   return Flm_inv_i(a, detp, p, 1);
    3290             : }
    3291             : 
    3292             : GEN
    3293       21490 : Flm_inv(GEN a, ulong p) {
    3294       21490 :   return Flm_inv_i(a, NULL, p, 0);
    3295             : }
    3296             : 
    3297             : GEN
    3298          21 : Flm_Flc_gauss(GEN a, GEN b, ulong p) {
    3299          21 :   pari_sp av = avma;
    3300          21 :   GEN z = Flm_gauss(a, mkmat(b), p);
    3301          21 :   if (!z) { avma = av; return NULL; }
    3302          14 :   if (lg(z) == 1) { avma = av; return cgetg(1,t_VECSMALL); }
    3303          14 :   return gerepileuptoleaf(av, gel(z,1));
    3304             : }
    3305             : 
    3306             : GEN
    3307      141033 : Flm_adjoint(GEN A, ulong p)
    3308             : {
    3309      141033 :   pari_sp av = avma;
    3310             :   GEN R, C, U, P, C1, U1, v, c, d;
    3311      141033 :   long r, i, q, n = lg(A)-1, m;
    3312             :   ulong D;
    3313      141033 :   if (n == 0) return cgetg(1, t_MAT);
    3314      141033 :   r = Flm_CUP(A, &R, &C, &U, &P, p);
    3315      141035 :   m = nbrows(A);
    3316      141035 :   if (r == n)
    3317             :   {
    3318      141028 :     GEN X = Flm_gauss_from_CUP(matid_Flm(m), R, C, U, P, p, &D);
    3319      141027 :     return gerepileupto(av, Flm_Fl_mul(X, D, p));
    3320             :   }
    3321           7 :   if (r < n-1) return zero_Flm(m, m);
    3322          21 :   for (q = m, i = 1; i < m; i++)
    3323          14 :    if (R[i] != i) { q = i; break; }
    3324           7 :   C1 = matslice(C, 1, q-1, 1, q-1);
    3325           7 :   c = vecslice(Flm_row(C, q), 1, q-1);
    3326           7 :   c = Flm_lsolve_lower_unit(C1, Flm_transpose(mkmat(c)), p);
    3327           7 :   d = cgetg(n+1, t_VECSMALL);
    3328           7 :   for (i=1; i<q; i++)    uel(d,i) = ucoeff(c,1,i);
    3329           7 :   uel(d,q) = p-1;
    3330           7 :   for (i=q+1; i<=n; i++) uel(d,i) = 0;
    3331           7 :   U1 = vecslice(U, 1, n-1);
    3332           7 :   v = gel(Flm_rsolve_upper(U1, mkmat(gel(U,n)), p),1);
    3333           7 :   v = vecsmall_append(v, p-1);
    3334           7 :   D = perm_sign(P) != (odd(q+n)?-1:1) ? p-1 : 1;
    3335          21 :   for (i = 1; i <= n-1; i++)
    3336          14 :     D = Fl_mul(D, ucoeff(U1, i, i), p);
    3337           7 :   d = Flv_Fl_mul(d, D, p);
    3338           7 :   return rowpermute(Flc_Flv_mul(v, d, p),perm_inv(P));
    3339             : }
    3340             : 
    3341             : static GEN
    3342        3982 : FpM_gauss_gen(GEN a, GEN b, GEN p)
    3343             : {
    3344             :   void *E;
    3345        3982 :   const struct bb_field *S = get_Fp_field(&E,p);
    3346        3982 :   return gen_Gauss(a,b, E, S);
    3347             : }
    3348             : /* a an FpM, lg(a)>1; b an FpM or NULL (replace by identity) */
    3349             : static GEN
    3350       65721 : FpM_gauss_i(GEN a, GEN b, GEN p, ulong *pp)
    3351             : {
    3352       65721 :   long n = nbrows(a);
    3353       65721 :   a = FpM_init(a,p,pp);
    3354       65721 :   switch(*pp)
    3355             :   {
    3356             :   case 0:
    3357        3982 :     if (!b) b = matid(n);
    3358        3982 :     return FpM_gauss_gen(a,b,p);
    3359             :   case 2:
    3360       20389 :     if (b) b = ZM_to_F2m(b); else b = matid_F2m(n);
    3361       20389 :     return F2m_gauss_sp(a,b);
    3362             :   default:
    3363       41350 :     if (b) b = ZM_to_Flm(b, *pp); else b = matid_Flm(n);
    3364       41350 :     return Flm_gauss_sp(a,b, NULL, *pp);
    3365             :   }
    3366             : }
    3367             : GEN
    3368          35 : FpM_gauss(GEN a, GEN b, GEN p)
    3369             : {
    3370          35 :   pari_sp av = avma;
    3371             :   ulong pp;
    3372             :   GEN u;
    3373          35 :   if (lg(a) == 1 || lg(b)==1) return cgetg(1, t_MAT);
    3374          35 :   u = FpM_gauss_i(a, b, p, &pp);
    3375          35 :   if (!u) { avma = av; return NULL; }
    3376          28 :   switch(pp)
    3377             :   {
    3378          28 :   case 0: return gerepilecopy(av, u);
    3379           0 :   case 2:  u = F2m_to_ZM(u); break;
    3380           0 :   default: u = Flm_to_ZM(u); break;
    3381             :   }
    3382           0 :   return gerepileupto(av, u);
    3383             : }
    3384             : GEN
    3385       65672 : FpM_inv(GEN a, GEN p)
    3386             : {
    3387       65672 :   pari_sp av = avma;
    3388             :   ulong pp;
    3389             :   GEN u;
    3390       65672 :   if (lg(a) == 1) return cgetg(1, t_MAT);
    3391       65672 :   u = FpM_gauss_i(a, NULL, p, &pp);
    3392       65672 :   if (!u) { avma = av; return NULL; }
    3393       65658 :   switch(pp)
    3394             :   {
    3395        3926 :   case 0: return gerepilecopy(av, u);
    3396       20382 :   case 2:  u = F2m_to_ZM(u); break;
    3397       41350 :   default: u = Flm_to_ZM(u); break;
    3398             :   }
    3399       61732 :   return gerepileupto(av, u);
    3400             : }
    3401             : 
    3402             : GEN
    3403          14 : FpM_FpC_gauss(GEN a, GEN b, GEN p)
    3404             : {
    3405          14 :   pari_sp av = avma;
    3406             :   ulong pp;
    3407             :   GEN u;
    3408          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    3409          14 :   u = FpM_gauss_i(a, mkmat(b), p, &pp);
    3410          14 :   if (!u) { avma = av; return NULL; }
    3411          14 :   switch(pp)
    3412             :   {
    3413          14 :   case 0: return gerepilecopy(av, gel(u,1));
    3414           0 :   case 2:  u = F2c_to_ZC(gel(u,1)); break;
    3415           0 :   default: u = Flc_to_ZC(gel(u,1)); break;
    3416             :   }
    3417           0 :   return gerepileupto(av, u);
    3418             : }
    3419             : 
    3420             : static GEN
    3421          56 : FlxqM_gauss_gen(GEN a, GEN b, GEN T, ulong p)
    3422             : {
    3423             :   void *E;
    3424          56 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    3425          56 :   return gen_Gauss(a, b, E, S);
    3426             : }
    3427             : 
    3428             : static GEN
    3429          35 : FlxqM_gauss_CUP(GEN a, GEN b, GEN T, ulong p) {
    3430             :   GEN R, C, U, P, Y;
    3431          35 :   long n = lg(a) - 1, r;
    3432          35 :   if (nbrows(a) < n || (r = FlxqM_CUP(a, &R, &C, &U, &P, T, p)) < n)
    3433          14 :     return NULL;
    3434          21 :   Y = FlxqM_rsolve_lower_unit(rowpermute(C, R),
    3435             :                               rowpermute(b, R), T, p);
    3436          21 :   return rowpermute(FlxqM_rsolve_upper(U, Y, T, p),
    3437             :                     perm_inv(P));
    3438             : }
    3439             : 
    3440             : static GEN
    3441          91 : FlxqM_gauss_i(GEN a, GEN b, GEN T, ulong p) {
    3442          91 :   if (lg(a) - 1 >= FlxqM_CUP_LIMIT)
    3443          35 :     return FlxqM_gauss_CUP(a, b, T, p);
    3444          56 :   return FlxqM_gauss_gen(a, b, T, p);
    3445             : }
    3446             : 
    3447             : GEN
    3448          21 : FlxqM_gauss(GEN a, GEN b, GEN T, ulong p)
    3449             : {
    3450          21 :   pari_sp av = avma;
    3451          21 :   long n = lg(a)-1;
    3452             :   GEN u;
    3453          21 :   if (!n || lg(b)==1) { avma = av; return cgetg(1, t_MAT); }
    3454          21 :   u = FlxqM_gauss_i(a, b, T, p);
    3455          21 :   if (!u) { avma = av; return NULL; }
    3456          14 :   return gerepilecopy(av, u);
    3457             : }
    3458             : GEN
    3459          56 : FlxqM_inv(GEN a, GEN T, ulong p)
    3460             : {
    3461          56 :   pari_sp av = avma;
    3462             :   GEN u;
    3463          56 :   if (lg(a) == 1) { avma = av; return cgetg(1, t_MAT); }
    3464          56 :   u = FlxqM_gauss_i(a, matid_FlxqM(nbrows(a),T,p), T,p);
    3465          56 :   if (!u) { avma = av; return NULL; }
    3466          42 :   return gerepilecopy(av, u);
    3467             : }
    3468             : GEN
    3469          14 : FlxqM_FlxqC_gauss(GEN a, GEN b, GEN T, ulong p)
    3470             : {
    3471          14 :   pari_sp av = avma;
    3472             :   GEN u;
    3473          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    3474          14 :   u = FlxqM_gauss_i(a, mkmat(b), T, p);
    3475          14 :   if (!u) { avma = av; return NULL; }
    3476           7 :   return gerepilecopy(av, gel(u,1));
    3477             : }
    3478             : 
    3479             : static GEN
    3480         133 : FqM_gauss_gen(GEN a, GEN b, GEN T, GEN p)
    3481             : {
    3482             :   void *E;
    3483         133 :   const struct bb_field *S = get_Fq_field(&E,T,p);
    3484         133 :   return gen_Gauss(a,b,E,S);
    3485             : }
    3486             : GEN
    3487          21 : FqM_gauss(GEN a, GEN b, GEN T, GEN p)
    3488             : {
    3489          21 :   pari_sp av = avma;
    3490             :   GEN u;
    3491             :   long n;
    3492          21 :   if (!T) return FpM_gauss(a,b,p);
    3493          21 :   n = lg(a)-1; if (!n || lg(b)==1) return cgetg(1, t_MAT);
    3494          21 :   u = FqM_gauss_gen(a,b,T,p);
    3495          21 :   if (!u) { avma = av; return NULL; }
    3496          14 :   return gerepilecopy(av, u);
    3497             : }
    3498             : GEN
    3499          98 : FqM_inv(GEN a, GEN T, GEN p)
    3500             : {
    3501          98 :   pari_sp av = avma;
    3502             :   GEN u;
    3503          98 :   if (!T) return FpM_inv(a,p);
    3504          98 :   if (lg(a) == 1) return cgetg(1, t_MAT);
    3505          98 :   u = FqM_gauss_gen(a,matid(nbrows(a)),T,p);
    3506          98 :   if (!u) { avma = av; return NULL; }
    3507          70 :   return gerepilecopy(av, u);
    3508             : }
    3509             : GEN
    3510          14 : FqM_FqC_gauss(GEN a, GEN b, GEN T, GEN p)
    3511             : {
    3512          14 :   pari_sp av = avma;
    3513             :   GEN u;
    3514          14 :   if (!T) return FpM_FpC_gauss(a,b,p);
    3515          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    3516          14 :   u = FqM_gauss_gen(a,mkmat(b),T,p);
    3517          14 :   if (!u) { avma = av; return NULL; }
    3518           7 :   return gerepilecopy(av, gel(u,1));
    3519             : }
    3520             : 
    3521             : static GEN
    3522      581807 : ZlM_gauss_ratlift(GEN a, GEN b, ulong p, long e, GEN C)
    3523             : {
    3524      581807 :   pari_sp av = avma, av2;
    3525             :   GEN bb, xi, xb, pi, P, B, r;
    3526      581807 :   long i, k = 2;
    3527      581807 :   if (!C) {
    3528           0 :     C = Flm_inv(ZM_to_Flm(a, p), p);
    3529           0 :     if (!C) pari_err_INV("ZlM_gauss", a);
    3530             :   }
    3531      581807 :   pi = P = utoipos(p);
    3532      581807 :   av2 = avma;
    3533      581807 :   xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    3534      581807 :   xb = Flm_to_ZM(xi);
    3535      581807 :   bb = b;
    3536     1194456 :   for (i = 2; i <= e; i++)
    3537             :   {
    3538      671394 :     bb = ZM_Z_divexact(ZM_sub(bb, ZM_nm_mul(a, xi)), P);
    3539      671394 :     if (gc_needed(av,2))
    3540             :     {
    3541          68 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZlM_gauss. i=%ld/%ld",i,e);
    3542          68 :       gerepileall(av2,3, &pi,&bb,&xb);
    3543             :     }
    3544      671394 :     xi = Flm_mul(C, ZM_to_Flm(bb, p), p);
    3545      671394 :     xb = ZM_add(xb, nm_Z_mul(xi, pi));
    3546      671394 :     pi = muliu(pi, p); /* = p^(i-1) */
    3547      671394 :     if (i==k && i < e)
    3548             :     {
    3549      197609 :       k *= 2;
    3550      197609 :       B = sqrti(shifti(pi,-1));
    3551      197609 :       r = FpM_ratlift(xb, pi, B, B, NULL);
    3552      197609 :       if (r)
    3553             :       {
    3554       98710 :         GEN dr, nr = Q_remove_denom(r,&dr);
    3555       98710 :         if (ZM_equal(ZM_mul(a,nr), dr? ZM_Z_mul(b,dr): b))
    3556             :         {
    3557       58745 :           if (DEBUGLEVEL>=4)
    3558           0 :             err_printf("ZlM_gauss: early solution: %ld/%ld\n",i,e);
    3559       58745 :           return gerepilecopy(av, r);
    3560             :         }
    3561             :       }
    3562             :     }
    3563             :   }
    3564      523062 :   B = sqrti(shifti(pi,-1));
    3565      523062 :   return gerepileupto(av, FpM_ratlift(xb, pi, B, B, NULL));
    3566             : }
    3567             : 
    3568             : /* Dixon p-adic lifting algorithm.
    3569             :  * Numer. Math. 40, 137-141 (1982), DOI: 10.1007/BF01459082 */
    3570             : GEN
    3571      581905 : ZM_gauss(GEN a, GEN b0)
    3572             : {
    3573      581905 :   pari_sp av = avma, av2;
    3574             :   int iscol;
    3575             :   long n, ncol, i, m, elim;
    3576             :   ulong p;
    3577      581905 :   GEN C, delta, nb, nmin, res, b = b0;
    3578             :   forprime_t S;
    3579             : 
    3580      581905 :   if (!init_gauss(a, &b, &n, &ncol, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    3581      581835 :   nb = gen_0; ncol = lg(b);
    3582     1375558 :   for (i = 1; i < ncol; i++)
    3583             :   {
    3584      793723 :     GEN ni = gnorml2(gel(b, i));
    3585      793723 :     if (cmpii(nb, ni) < 0) nb = ni;
    3586             :   }
    3587      581835 :   if (!signe(nb)) { avma = av; return gcopy(b0); }
    3588      581835 :   delta = gen_1; nmin = nb;
    3589     2345746 :   for (i = 1; i <= n; i++)
    3590             :   {
    3591     1763911 :     GEN ni = gnorml2(gel(a, i));
    3592     1763911 :     if (cmpii(ni, nmin) < 0)
    3593             :     {
    3594       76065 :       delta = mulii(delta, nmin); nmin = ni;
    3595             :     }
    3596             :     else
    3597     1687846 :       delta = mulii(delta, ni);
    3598             :   }
    3599      581835 :   if (!signe(nmin)) return NULL;
    3600      581814 :   elim = expi(delta)+1;
    3601      581814 :   av2 = avma;
    3602      581814 :   init_modular_big(&S);
    3603             :   for(;;)
    3604             :   {
    3605      581814 :     p = u_forprime_next(&S);
    3606      581814 :     C = Flm_inv_sp(ZM_to_Flm(a, p), NULL, p);
    3607      581814 :     if (C) break;
    3608           7 :     elim -= expu(p);
    3609           7 :     if (elim < 0) return NULL;
    3610           0 :     avma = av2;
    3611             :   }
    3612             :   /* N.B. Our delta/lambda are SQUARES of those in the paper
    3613             :    * log(delta lambda) / log p, where lambda is 3+sqrt(5) / 2,
    3614             :    * whose log is < 1, hence + 1 (to cater for rounding errors) */
    3615     1163614 :   m = (long)ceil((rtodbl(logr_abs(itor(delta,LOWDEFAULTPREC))) + 1)
    3616      581807 :                  / log((double)p));
    3617      581807 :   res = ZlM_gauss_ratlift(a, b, p, m, C);
    3618      581807 :   if (iscol) return gerepilecopy(av, gel(res, 1));
    3619       82958 :   return gerepileupto(av, res);
    3620             : }
    3621             : 
    3622             : /* same as above, M rational */
    3623             : GEN
    3624         854 : QM_gauss(GEN M, GEN B)
    3625             : {
    3626         854 :   pari_sp av = avma;
    3627             :   GEN K, MB;
    3628         854 :   MB = Q_primitive_part(mkvec2(M,B), NULL);
    3629         854 :   K = ZM_gauss(gel(MB,1), gel(MB,2));
    3630         854 :   return gerepileupto(av, K);
    3631             : }
    3632             : 
    3633             : static GEN
    3634      131249 : ZM_inv_slice(GEN A, GEN P, GEN *mod)
    3635             : {
    3636      131249 :   pari_sp av = avma;
    3637      131249 :   long i, n = lg(P)-1;
    3638             :   GEN H, T;
    3639      131249 :   if (n == 1)
    3640             :   {
    3641      130492 :     ulong p = uel(P,1);
    3642      130492 :     GEN Hp, a = ZM_to_Flm(A, p);
    3643      130491 :     Hp = Flm_adjoint(a, p);
    3644      130492 :     Hp = gerepileupto(av, Flm_to_ZM(Hp));
    3645      130491 :     *mod = utoi(p); return Hp;
    3646             :   }
    3647         757 :   T = ZV_producttree(P);
    3648         757 :   A = ZM_nv_mod_tree(A, P, T);
    3649         757 :   H = cgetg(n+1, t_VEC);
    3650        2747 :   for(i=1; i <= n; i++)
    3651        1990 :     gel(H,i) = Flm_adjoint(gel(A, i), uel(P,i));
    3652         757 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    3653         757 :   *mod = gmael(T, lg(T)-1, 1);
    3654         757 :   gerepileall(av, 2, &H, mod);
    3655         757 :   return H;
    3656             : }
    3657             : 
    3658             : static GEN
    3659      112211 : RgM_true_Hadamard(GEN a)
    3660             : {
    3661      112211 :   pari_sp av = avma;
    3662      112211 :   long n = lg(a)-1, i;
    3663             :   GEN B;
    3664      112211 :   if (n == 0) return gen_1;
    3665      112211 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    3666      112211 :   B = gnorml2(gel(a,1));
    3667      112211 :   for (i = 2; i <= n; i++) B = gmul(B, gnorml2(gel(a,i)));
    3668      112211 :   return gerepileuptoint(av, ceil_safe(sqrtr_abs(B)));
    3669             : }
    3670             : 
    3671             : GEN
    3672      131249 : ZM_inv_worker(GEN P, GEN A)
    3673             : {
    3674      131249 :   GEN V = cgetg(3, t_VEC);
    3675      131249 :   gel(V,1) = ZM_inv_slice(A, P, &gel(V,2));
    3676      131246 :   return V;
    3677             : }
    3678             : 
    3679             : static GEN
    3680        4305 : ZM_inv0(GEN A, GEN *pden)
    3681             : {
    3682        4305 :   if (pden) *pden = gen_1;
    3683        4305 :   (void)A; return cgetg(1, t_MAT);
    3684             : }
    3685             : static GEN
    3686       16937 : ZM_inv1(GEN A, GEN *pden)
    3687             : {
    3688       16937 :   GEN a = gcoeff(A,1,1);
    3689       16937 :   long s = signe(a);
    3690       16937 :   if (!s) return NULL;
    3691       16937 :   if (pden) *pden = absi(a);
    3692       16937 :   retmkmat(mkcol(s == 1? gen_1: gen_m1));
    3693             : }
    3694             : static GEN
    3695       38580 : ZM_inv2(GEN A, GEN *pden)
    3696             : {
    3697             :   GEN a, b, c, d, D, cA;
    3698             :   long s;
    3699       38580 :   A = Q_primitive_part(A, &cA);
    3700       38580 :   a = gcoeff(A,1,1); b = gcoeff(A,1,2);
    3701       38580 :   c = gcoeff(A,2,1); d = gcoeff(A,2,2);
    3702       38580 :   D = subii(mulii(a,d), mulii(b,c)); /* left on stack */
    3703       38580 :   s = signe(D);
    3704       38580 :   if (!s) return NULL;
    3705       38580 :   if (s < 0) D = negi(D);
    3706       38580 :   if (pden) *pden = mul_denom(D, cA);
    3707       38580 :   if (s > 0)
    3708       19407 :     retmkmat2(mkcol2(icopy(d), negi(c)), mkcol2(negi(b), icopy(a)));
    3709             :   else
    3710       19173 :     retmkmat2(mkcol2(negi(d), icopy(c)), mkcol2(icopy(b), negi(a)));
    3711             : }
    3712             : 
    3713             : /* to be used when denom(M^(-1)) << det(M) and a sharp multiple is
    3714             :  * not available. Return H primitive such that M*H = den*Id */
    3715             : GEN
    3716           0 : ZM_inv_ratlift(GEN M, GEN *pden)
    3717             : {
    3718           0 :   pari_sp av2, av = avma;
    3719             :   GEN Hp, q, H;
    3720             :   ulong p;
    3721           0 :   long m = lg(M)-1;
    3722             :   forprime_t S;
    3723             :   pari_timer ti;
    3724             : 
    3725           0 :   if (m == 0) return ZM_inv0(M,pden);
    3726           0 :   if (m == 1 && nbrows(M)==1) return ZM_inv1(M,pden);
    3727           0 :   if (m == 2 && nbrows(M)==2) return ZM_inv2(M,pden);
    3728             : 
    3729           0 :   if (DEBUGLEVEL>5) timer_start(&ti);
    3730           0 :   init_modular_big(&S);
    3731           0 :   av2 = avma;
    3732           0 :   H = NULL;
    3733           0 :   while ((p = u_forprime_next(&S)))
    3734             :   {
    3735             :     GEN Mp, B, Hr;
    3736           0 :     Mp = ZM_to_Flm(M,p);
    3737           0 :     Hp = Flm_inv_sp(Mp, NULL, p);
    3738           0 :     if (!Hp) continue;
    3739           0 :     if (!H)
    3740             :     {
    3741           0 :       H = ZM_init_CRT(Hp, p);
    3742           0 :       q = utoipos(p);
    3743             :     }
    3744             :     else
    3745           0 :       ZM_incremental_CRT(&H, Hp, &q, p);
    3746           0 :     B = sqrti(shifti(q,-1));
    3747           0 :     Hr = FpM_ratlift(H,q,B,B,NULL);
    3748           0 :     if (DEBUGLEVEL>5)
    3749           0 :       timer_printf(&ti,"ZM_inv mod %lu (ratlift=%ld)", p,!!Hr);
    3750           0 :     if (Hr) {/* DONE ? */
    3751           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    3752           0 :       if (ZM_isscalar(ZM_mul(Hl, M), *pden)) { H = Hl; break; }
    3753             :     }
    3754             : 
    3755           0 :     if (gc_needed(av,2))
    3756             :     {
    3757           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_inv_ratlift");
    3758           0 :       gerepileall(av2, 2, &H, &q);
    3759             :     }
    3760             :   }
    3761           0 :   if (!*pden) *pden = gen_1;
    3762           0 :   gerepileall(av, 2, &H, pden);
    3763           0 :   return H;
    3764             : }
    3765             : 
    3766             : static GEN
    3767      119515 : ZM_adj_ratlift(GEN A, GEN H, GEN mod)
    3768             : {
    3769             :   GEN B;
    3770      119515 :   GEN D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3771      119515 :   GEN g = gcdii(D, mod);
    3772      119515 :   if (!equali1(g))
    3773             :   {
    3774           7 :     mod = diviiexact(mod, g);
    3775           7 :     H = FpM_red(H, mod);
    3776             :   }
    3777      119515 :   D = Fp_inv(Fp_red(D, mod), mod);
    3778      119515 :   H = FpM_Fp_mul(H, D, mod);
    3779      119515 :   B = sqrti(shifti(mod,-1));
    3780      119515 :   return FpM_ratlift(H, mod, B, B, NULL);
    3781             : }
    3782             : 
    3783             : GEN
    3784      172040 : ZM_inv(GEN A, GEN *pden)
    3785             : {
    3786      172040 :   pari_sp av = avma;
    3787      172040 :   long m = lg(A)-1, n, k1 = 1, k2;
    3788      172040 :   GEN H = NULL, D, H1 = NULL, mod1 = NULL, worker;
    3789      172040 :   ulong bnd, mask, p = 0;
    3790             :   pari_timer ti;
    3791             : 
    3792      172040 :   if (m == 0) return ZM_inv0(A,pden);
    3793      167735 :   if (pden) *pden = gen_1;
    3794      167735 :   if (nbrows(A) < m) return NULL;
    3795      167728 :   if (m == 1 && nbrows(A)==1) return ZM_inv1(A,pden);
    3796      150791 :   if (m == 2 && nbrows(A)==2) return ZM_inv2(A,pden);
    3797             : 
    3798      112211 :   if (DEBUGLEVEL>=5) timer_start(&ti);
    3799      112211 :   bnd = expi(RgM_true_Hadamard(A));
    3800      112211 :   worker = strtoclosure("_ZM_inv_worker", 1, A);
    3801      112211 :   gen_inccrt("ZM_inv_r", worker, NULL, k1, m, &p, &H1, &mod1, nmV_chinese_center, FpM_center);
    3802      112211 :   n = (bnd+1)/expu(p)+1;
    3803      112211 :   if (DEBUGLEVEL>=5) timer_printf(&ti,"inv (%ld/%ld primes)", k1, n);
    3804      112211 :   mask = quadratic_prec_mask(n);
    3805      112211 :   for (k2 = 0;;)
    3806       16224 :   {
    3807             :     GEN Hr;
    3808      128435 :     if (k2 > 0)
    3809             :     {
    3810       13828 :       gen_inccrt("ZM_inv_r", worker, NULL, k2, m, &p, &H1, &mod1,nmV_chinese_center,FpM_center);
    3811       13828 :       k1 += k2;
    3812       13828 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"CRT (%ld/%ld primes)", k1, n);
    3813             :     }
    3814      128435 :     if (mask == 1) break;
    3815      119515 :     k2 = (mask&1UL) ? k1-1: k1;
    3816      119515 :     mask >>= 1;
    3817             : 
    3818      119515 :     Hr = ZM_adj_ratlift(A, H1, mod1);
    3819      119515 :     if (DEBUGLEVEL>=5) timer_printf(&ti,"ratlift (%ld/%ld primes)", k1, n);
    3820      119515 :     if (Hr) {/* DONE ? */
    3821             :       GEN den;
    3822      105026 :       GEN Hl = Q_remove_denom(Hr, &den);
    3823      105026 :       GEN R = ZM_mul(Hl, A);
    3824      105026 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"mult (%ld/%ld primes)", k1, n);
    3825      105026 :       den = den ? den: gen_1;
    3826      105026 :       if (den)
    3827             :       {
    3828      105026 :         if (ZM_isscalar(R, den))
    3829             :         {
    3830      103291 :           H = Hl;
    3831      103291 :           if (pden) *pden = den;
    3832      206582 :           break;
    3833             :         }
    3834             :       }
    3835             :       else
    3836           0 :         if (ZM_isidentity(R)) { H=Hl; break; }
    3837             :     }
    3838             :   }
    3839      112211 :   if (!H)
    3840             :   {
    3841             :     GEN d;
    3842        8920 :     H = H1;
    3843        8920 :     D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3844        8920 :     if (signe(D)==0) pari_err_INV("ZM_inv", A);
    3845        8913 :     d = gcdii(Q_content_safe(H), D);
    3846        8913 :     if (signe(D) < 0) d = negi(d);
    3847        8913 :     if (!equali1(d))
    3848             :     {
    3849        5292 :       H = ZM_Z_divexact(H, d);
    3850        5292 :       D = diviiexact(D, d);
    3851             :     }
    3852        8913 :     if (pden) *pden = D;
    3853             :   }
    3854      112204 :   gerepileall(av, pden? 2: 1, &H, pden);
    3855      112204 :   return H;
    3856             : }
    3857             : 
    3858             : /* same as above, M rational */
    3859             : GEN
    3860        1694 : QM_inv(GEN M)
    3861             : {
    3862        1694 :   pari_sp av = avma;
    3863             :   GEN den, cM, K;
    3864        1694 :   M = Q_primitive_part(M, &cM);
    3865        1694 :   K = ZM_inv(M, &den);
    3866        1694 :   if (!K) { avma = av; return NULL; }
    3867        1694 :   cM = inv_content(mul_content(cM, den));
    3868        1694 :   if (cM) K = RgM_Rg_div(K, cM);
    3869        1694 :   return gerepileupto(av, K);
    3870             : }
    3871             : 
    3872             : static GEN
    3873       55574 : ZM_ker_i(GEN M, long fl)
    3874             : {
    3875       55574 :   pari_sp av2, av = avma;
    3876             :   GEN q, H, D;
    3877             :   forprime_t S;
    3878       55574 :   av2 = avma;
    3879       55574 :   H = NULL; D = NULL;
    3880       55574 :   if (lg(M)==1) return cgetg(1, t_MAT);
    3881       55567 :   init_modular_big(&S);
    3882             :   for(;;)
    3883       61993 :   {
    3884             :     GEN Kp, Hp, Dp, Mp, Hr, B;
    3885      117560 :     ulong p = u_forprime_next(&S);
    3886      117560 :     Mp = ZM_to_Flm(M, p);
    3887      117560 :     Kp = Flm_ker_sp(Mp, p, 2);
    3888      117560 :     Hp = gel(Kp,1); Dp = gel(Kp,2);
    3889      117560 :     if (H && (lg(Hp)>lg(H) || (lg(Hp)==lg(H) && vecsmall_lexcmp(Dp,D)>0))) continue;
    3890      109652 :     if (!H || (lg(Hp)<lg(H) || vecsmall_lexcmp(Dp,D)<0))
    3891             :     {
    3892       93247 :       H = ZM_init_CRT(Hp, p); D = Dp;
    3893       93247 :       q = utoipos(p);
    3894             :     }
    3895             :     else
    3896       16405 :       ZM_incremental_CRT(&H, Hp, &q, p);
    3897      109652 :     B = sqrti(shifti(q,-1));
    3898      109652 :     Hr = FpM_ratlift(H, q, B, B, NULL);
    3899      109652 :     if (DEBUGLEVEL>5) err_printf("ZM_ker mod %lu (ratlift=%ld)\n", p,!!Hr);
    3900      109652 :     if (Hr) {/* DONE ? */
    3901      104042 :       GEN MH = QM_mul(M, Hr);
    3902      104042 :       if (gequal0(MH)) { H = fl ? vec_Q_primpart(Hr): Hr;  break; }
    3903             :     }
    3904       54085 :     if (gc_needed(av,2))
    3905             :     {
    3906           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_ker");
    3907           0 :       gerepileall(av2, 3, &H, &D, &q);
    3908             :     }
    3909             :   }
    3910       55567 :   return gerepilecopy(av, H);
    3911             : }
    3912             : 
    3913             : GEN
    3914       48770 : ZM_ker(GEN M)
    3915       48770 : { return ZM_ker_i(M, 1); }
    3916             : 
    3917             : GEN
    3918        7637 : QM_ker(GEN M)
    3919             : {
    3920        7637 :   pari_sp av = avma;
    3921        7637 :   long l = lg(M)-1;
    3922        7637 :   if (l==0) return cgetg(1, t_MAT);
    3923        7602 :   if (lgcols(M)==1) return matid(l);
    3924        6741 :   M = shallowtrans(vec_Q_primpart(shallowtrans(M)));
    3925        6741 :   return gerepileupto(av, ZM_ker_i(M, 0));
    3926             : }
    3927             : 
    3928             : /* x a ZM. Return a multiple of the determinant of the lattice generated by
    3929             :  * the columns of x. From Algorithm 2.2.6 in GTM138 */
    3930             : GEN
    3931       47549 : detint(GEN A)
    3932             : {
    3933       47549 :   if (typ(A) != t_MAT) pari_err_TYPE("detint",A);
    3934       47549 :   RgM_check_ZM(A, "detint");
    3935       47549 :   return ZM_detmult(A);
    3936             : }
    3937             : GEN
    3938       77957 : ZM_detmult(GEN A)
    3939             : {
    3940       77957 :   pari_sp av1, av = avma;
    3941             :   GEN B, c, v, piv;
    3942       77957 :   long rg, i, j, k, m, n = lg(A) - 1;
    3943             : 
    3944       77957 :   if (!n) return gen_1;
    3945       77957 :   m = nbrows(A);
    3946       77957 :   if (n < m) return gen_0;
    3947       77936 :   c = zero_zv(m);
    3948       77936 :   av1 = avma;
    3949       77936 :   B = zeromatcopy(m,m);
    3950       77936 :   v = cgetg(m+1, t_COL);
    3951       77936 :   piv = gen_1; rg = 0;
    3952      479218 :   for (k=1; k<=n; k++)
    3953             :   {
    3954      479204 :     GEN pivprec = piv;
    3955      479204 :     long t = 0;
    3956     4023015 :     for (i=1; i<=m; i++)
    3957             :     {
    3958     3543811 :       pari_sp av2 = avma;
    3959             :       GEN vi;
    3960     3543811 :       if (c[i]) continue;
    3961             : 
    3962     2011756 :       vi = mulii(piv, gcoeff(A,i,k));
    3963    17750820 :       for (j=1; j<=m; j++)
    3964    15739064 :         if (c[j]) vi = addii(vi, mulii(gcoeff(B,j,i),gcoeff(A,j,k)));
    3965     2011756 :       if (!t && signe(vi)) t = i;
    3966     2011756 :       gel(v,i) = gerepileuptoint(av2, vi);
    3967             :     }
    3968      479204 :     if (!t) continue;
    3969             :     /* at this point c[t] = 0 */
    3970             : 
    3971      479120 :     if (++rg >= m) { /* full rank; mostly done */
    3972       77922 :       GEN det = gel(v,t); /* last on stack */
    3973       77922 :       if (++k > n)
    3974       77839 :         det = absi(det);
    3975             :       else
    3976             :       {
    3977             :         /* improve further; at this point c[i] is set for all i != t */
    3978          83 :         gcoeff(B,t,t) = piv; v = centermod(gel(B,t), det);
    3979         334 :         for ( ; k<=n; k++)
    3980         251 :           det = gcdii(det, ZV_dotproduct(v, gel(A,k)));
    3981             :       }
    3982       77922 :       return gerepileuptoint(av, det);
    3983             :     }
    3984             : 
    3985      401198 :     piv = gel(v,t);
    3986     3465406 :     for (i=1; i<=m; i++)
    3987             :     {
    3988             :       GEN mvi;
    3989     3064208 :       if (c[i] || i == t) continue;
    3990             : 
    3991     1532104 :       gcoeff(B,t,i) = mvi = negi(gel(v,i));
    3992    13723815 :       for (j=1; j<=m; j++)
    3993    12191711 :         if (c[j]) /* implies j != t */
    3994             :         {
    3995     3042501 :           pari_sp av2 = avma;
    3996     3042501 :           GEN z = addii(mulii(gcoeff(B,j,i), piv), mulii(gcoeff(B,j,t), mvi));
    3997     3042501 :           if (rg > 1) z = diviiexact(z, pivprec);
    3998     3042501 :           gcoeff(B,j,i) = gerepileuptoint(av2, z);
    3999             :         }
    4000             :     }
    4001      401198 :     c[t] = k;
    4002      401198 :     if (gc_needed(av,1))
    4003             :     {
    4004           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"detint. k=%ld",k);
    4005           0 :       gerepileall(av1, 2, &piv,&B); v = zerovec(m);
    4006             :     }
    4007             :   }
    4008          14 :   avma = av; return gen_0;
    4009             : }
    4010             : 
    4011             : /* Reduce x modulo (invertible) y */
    4012             : GEN
    4013       13293 : closemodinvertible(GEN x, GEN y)
    4014             : {
    4015       13293 :   return gmul(y, ground(RgM_solve(y,x)));
    4016             : }
    4017             : GEN
    4018           7 : reducemodinvertible(GEN x, GEN y)
    4019             : {
    4020           7 :   return gsub(x, closemodinvertible(x,y));
    4021             : }
    4022             : GEN
    4023           0 : reducemodlll(GEN x,GEN y)
    4024             : {
    4025           0 :   return reducemodinvertible(x, ZM_lll(y, 0.75, LLL_INPLACE));
    4026             : }
    4027             : 
    4028             : /*******************************************************************/
    4029             : /*                                                                 */
    4030             : /*                    KERNEL of an m x n matrix                    */
    4031             : /*          return n - rk(x) linearly independent vectors          */
    4032             : /*                                                                 */
    4033             : /*******************************************************************/
    4034             : static GEN
    4035          28 : RgM_deplin_i(GEN x0)
    4036             : {
    4037          28 :   pari_sp av = avma, av2;
    4038          28 :   long i, j, k, nl, nc = lg(x0)-1;
    4039             :   GEN D, x, y, c, l, d, ck;
    4040             : 
    4041          28 :   if (!nc) { avma=av; return NULL; }
    4042          28 :   nl = nbrows(x0);
    4043          28 :   c = zero_zv(nl);
    4044          28 :   l = cgetg(nc+1, t_VECSMALL); /* not initialized */
    4045          28 :   av2 = avma;
    4046          28 :   x = RgM_shallowcopy(x0);
    4047          28 :   d = const_vec(nl, gen_1); /* pivot list */
    4048          28 :   ck = NULL; /* gcc -Wall */
    4049          98 :   for (k=1; k<=nc; k++)
    4050             :   {
    4051          91 :     ck = gel(x,k);
    4052         196 :     for (j=1; j<k; j++)
    4053             :     {
    4054         105 :       GEN cj = gel(x,j), piv = gel(d,j), q = gel(ck,l[j]);
    4055         420 :       for (i=1; i<=nl; i++)
    4056         315 :         if (i!=l[j]) gel(ck,i) = gsub(gmul(piv, gel(ck,i)), gmul(q, gel(cj,i)));
    4057             :     }
    4058             : 
    4059          91 :     i = gauss_get_pivot_NZ(x, NULL, k, c);
    4060          91 :     if (i > nl) break;
    4061          70 :     if (gc_needed(av,1))
    4062             :     {
    4063           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"deplin k = %ld/%ld",k,nc);
    4064           0 :       gerepileall(av2, 2, &x, &d);
    4065           0 :       ck = gel(x,k);
    4066             :     }
    4067          70 :     gel(d,k) = gel(ck,i);
    4068          70 :     c[i] = k; l[k] = i; /* pivot d[k] in x[i,k] */
    4069             :   }
    4070          28 :   if (k > nc) { avma = av; return NULL; }
    4071          21 :   if (k == 1) { avma = av; return scalarcol_shallow(gen_1,nc); }
    4072          21 :   y = cgetg(nc+1,t_COL);
    4073          21 :   gel(y,1) = gcopy(gel(ck, l[1]));
    4074          49 :   for (D=gel(d,1),j=2; j<k; j++)
    4075             :   {
    4076          28 :     gel(y,j) = gmul(gel(ck, l[j]), D);
    4077          28 :     D = gmul(D, gel(d,j));
    4078             :   }
    4079          21 :   gel(y,j) = gneg(D);
    4080          21 :   for (j++; j<=nc; j++) gel(y,j) = gen_0;
    4081          21 :   y = primitive_part(y, &c);
    4082          21 :   return c? gerepileupto(av, y): gerepilecopy(av, y);
    4083             : }
    4084             : static GEN
    4085           0 : RgV_deplin(GEN v)
    4086             : {
    4087           0 :   pari_sp av = avma;
    4088           0 :   long n = lg(v)-1;
    4089           0 :   GEN y, p = NULL;
    4090           0 :   if (n <= 1)
    4091             :   {
    4092           0 :     if (n == 1 && gequal0(gel(v,1))) return mkcol(gen_1);
    4093           0 :     return cgetg(1, t_COL);
    4094             :   }
    4095           0 :   if (gequal0(gel(v,1))) return scalarcol_shallow(gen_1, n);
    4096           0 :   v = primpart(mkvec2(gel(v,1),gel(v,2)));
    4097           0 :   if (RgV_is_FpV(v, &p) && p) v = centerlift(v);
    4098           0 :   y = zerocol(n);
    4099           0 :   gel(y,1) = gneg(gel(v,2));
    4100           0 :   gel(y,2) = gcopy(gel(v,1));
    4101           0 :   return gerepileupto(av, y);
    4102             : 
    4103             : }
    4104             : 
    4105             : static GEN
    4106         105 : RgM_deplin_FpM(GEN x, GEN p)
    4107             : {
    4108         105 :   pari_sp av = avma;
    4109             :   ulong pp;
    4110         105 :   x = RgM_Fp_init(x, p, &pp);
    4111         105 :   switch(pp)
    4112             :   {
    4113             :   case 0:
    4114          35 :     x = FpM_ker_gen(x,p,1);
    4115          35 :     if (!x) { avma = av; return NULL; }
    4116          21 :     x = FpC_center(x,p,shifti(p,-1));
    4117          21 :     break;
    4118             :   case 2:
    4119          14 :     x = F2m_ker_sp(x,1);
    4120          14 :     if (!x) { avma = av; return NULL; }
    4121           7 :     x = F2c_to_ZC(x); break;
    4122             :   default:
    4123          56 :     x = Flm_ker_sp(x,pp,1);
    4124          56 :     if (!x) { avma = av; return NULL; }
    4125          35 :     x = Flv_center(x, pp, pp>>1);
    4126          35 :     x = zc_to_ZC(x);
    4127          35 :     break;
    4128             :   }
    4129          63 :   return gerepileupto(av, x);
    4130             : }
    4131             : 
    4132             : /* FIXME: implement direct modular ZM_deplin ? */
    4133             : static GEN
    4134          98 : QM_deplin(GEN M)
    4135             : {
    4136          98 :   pari_sp av = avma;
    4137          98 :   long l = lg(M)-1;
    4138             :   GEN k;
    4139          98 :   if (l==0) return NULL;
    4140          63 :   if (lgcols(M)==1) return col_ei(l, 1);
    4141          63 :   M = shallowtrans(vec_Q_primpart(shallowtrans(M)));
    4142          63 :   k = ZM_ker_i(M, 1);
    4143          63 :   if (lg(k)== 1) { avma = av; return NULL; }
    4144          49 :   return gerepilecopy(av, gel(k,1));
    4145             : }
    4146             : 
    4147             : static GEN
    4148          42 : RgM_deplin_FqM(GEN x, GEN pol, GEN p)
    4149             : {
    4150          42 :   pari_sp av = avma;
    4151          42 :   GEN T = RgX_to_FpX(pol, p);
    4152          42 :   GEN b = FqM_deplin(RgM_to_FqM(x, T, p), T, p);
    4153          42 :   return gerepileupto(av, b);
    4154             : }
    4155             : 
    4156             : #define code(t1,t2) ((t1 << 6) | t2)
    4157             : static GEN
    4158         357 : RgM_deplin_fast(GEN x)
    4159             : {
    4160             :   GEN p, pol;
    4161             :   long pa;
    4162         357 :   long t = RgM_type(x, &p,&pol,&pa);
    4163         357 :   switch(t)
    4164             :   {
    4165             :     case t_INT:    /* fall through */
    4166          98 :     case t_FRAC:   return QM_deplin(x);
    4167          84 :     case t_FFELT:  return FFM_deplin(x, pol);
    4168         105 :     case t_INTMOD: return RgM_deplin_FpM(x, p);
    4169             :     case code(t_POLMOD, t_INTMOD):
    4170          42 :                    return RgM_deplin_FqM(x, pol, p);
    4171          28 :     default:       return gen_0;
    4172             :   }
    4173             : }
    4174             : #undef code
    4175             : 
    4176             : static GEN
    4177         357 : RgM_deplin(GEN x)
    4178             : {
    4179         357 :   GEN z = RgM_deplin_fast(x);
    4180         357 :   if (z!= gen_0) return z;
    4181          28 :   return RgM_deplin_i(x);
    4182             : }
    4183             : 
    4184             : GEN
    4185         357 : deplin(GEN x)
    4186             : {
    4187         357 :   switch(typ(x))
    4188             :   {
    4189             :     case t_MAT:
    4190             :     {
    4191         357 :       GEN z = RgM_deplin(x);
    4192         357 :       if (z) return z;
    4193         140 :       return cgetg(1, t_COL);
    4194             :     }
    4195           0 :     case t_VEC: return RgV_deplin(x);
    4196           0 :     default: pari_err_TYPE("deplin",x);
    4197             :   }
    4198             :   return NULL;/*LCOV_EXCL_LINE*/
    4199             : }
    4200             : 
    4201             : /*******************************************************************/
    4202             : /*                                                                 */
    4203             : /*         GAUSS REDUCTION OF MATRICES  (m lines x n cols)         */
    4204             : /*           (kernel, image, complementary image, rank)            */
    4205             : /*                                                                 */
    4206             : /*******************************************************************/
    4207             : /* return the transform of x under a standard Gauss pivot.
    4208             :  * x0 is a reference point when guessing whether x[i,j] ~ 0
    4209             :  * (iff x[i,j] << x0[i,j])
    4210             :  * Set r = dim ker(x). d[k] contains the index of the first non-zero pivot
    4211             :  * in column k */
    4212             : static GEN
    4213         910 : gauss_pivot_ker(GEN x, GEN x0, GEN *dd, long *rr)
    4214             : {
    4215             :   GEN c, d, p, data;
    4216             :   pari_sp av;
    4217             :   long i, j, k, r, t, n, m;
    4218             :   pivot_fun pivot;
    4219             : 
    4220         910 :   n=lg(x)-1; if (!n) { *dd=NULL; *rr=0; return cgetg(1,t_MAT); }
    4221         910 :   m=nbrows(x); r=0;
    4222         910 :   pivot = get_pivot_fun(x, x0, &data);
    4223         910 :   x = RgM_shallowcopy(x);
    4224         910 :   c = zero_zv(m);
    4225         910 :   d = cgetg(n+1,t_VECSMALL);
    4226         910 :   av=avma;
    4227        5222 :   for (k=1; k<=n; k++)
    4228             :   {
    4229        4312 :     j = pivot(x, data, k, c);
    4230        4312 :     if (j > m)
    4231             :     {
    4232         959 :       r++; d[k]=0;
    4233        4270 :       for(j=1; j<k; j++)
    4234        3311 :         if (d[j]) gcoeff(x,d[j],k) = gclone(gcoeff(x,d[j],k));
    4235             :     }
    4236             :     else
    4237             :     { /* pivot for column k on row j */
    4238        3353 :       c[j]=k; d[k]=j; p = gdiv(gen_m1,gcoeff(x,j,k));
    4239        3353 :       gcoeff(x,j,k) = gen_m1;
    4240             :       /* x[j,] /= - x[j,k] */
    4241        3353 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    4242       33684 :       for (t=1; t<=m; t++)
    4243       30331 :         if (t!=j)
    4244             :         { /* x[t,] -= 1 / x[j,k] x[j,] */
    4245       26978 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    4246      225092 :           for (i=k+1; i<=n; i++)
    4247      198114 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i),gmul(p,gcoeff(x,j,i)));
    4248       26978 :           if (gc_needed(av,1)) gerepile_gauss_ker(x,k,t,av);
    4249             :         }
    4250             :     }
    4251             :   }
    4252         910 :   *dd=d; *rr=r; return x;
    4253             : }
    4254             : 
    4255             : static GEN FpM_gauss_pivot(GEN x, GEN p, long *rr);
    4256             : static GEN FqM_gauss_pivot(GEN x, GEN T, GEN p, long *rr);
    4257             : static GEN F2m_gauss_pivot(GEN x, long *rr);
    4258             : 
    4259             : /* r = dim ker(x).
    4260             :  * Returns d:
    4261             :  *   d[k] != 0 contains the index of a non-zero pivot in column k
    4262             :  *   d[k] == 0 if column k is a linear combination of the (k-1) first ones */
    4263             : GEN
    4264       23908 : RgM_pivots(GEN x0, GEN data, long *rr, pivot_fun pivot)
    4265             : {
    4266             :   GEN x, c, d, p;
    4267       23908 :   long i, j, k, r, t, m, n = lg(x0)-1;
    4268             :   pari_sp av;
    4269             : 
    4270       23908 :   if (RgM_is_ZM(x0)) return ZM_pivots(x0, rr);
    4271       23810 :   if (!n) { *rr = 0; return NULL; }
    4272             : 
    4273       23810 :   d = cgetg(n+1, t_VECSMALL);
    4274       23810 :   x = RgM_shallowcopy(x0);
    4275       23810 :   m = nbrows(x); r = 0;
    4276       23810 :   c = zero_zv(m);
    4277       23810 :   av = avma;
    4278      924703 :   for (k=1; k<=n; k++)
    4279             :   {
    4280      900893 :     j = pivot(x, data, k, c);
    4281      900893 :     if (j > m) { r++; d[k] = 0; }
    4282             :     else
    4283             :     {
    4284       40965 :       c[j] = k; d[k] = j; p = gdiv(gen_m1, gcoeff(x,j,k));
    4285       40965 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    4286             : 
    4287      168178 :       for (t=1; t<=m; t++)
    4288      127213 :         if (!c[t]) /* no pivot on that line yet */
    4289             :         {
    4290       51939 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    4291     5440087 :           for (i=k+1; i<=n; i++)
    4292     5388148 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i), gmul(p, gcoeff(x,j,i)));
    4293       51939 :           if (gc_needed(av,1)) gerepile_gauss(x,k,t,av,j,c);
    4294             :         }
    4295       40965 :       for (i=k; i<=n; i++) gcoeff(x,j,i) = gen_0; /* dummy */
    4296             :     }
    4297             :   }
    4298       23810 :   *rr = r; avma = (pari_sp)d; return d;
    4299             : }
    4300             : 
    4301             : static long
    4302      106625 : ZM_count_0_cols(GEN M)
    4303             : {
    4304      106625 :   long i, l = lg(M), n = 0;
    4305      616609 :   for (i = 1; i < l; i++)
    4306      509984 :     if (ZV_equal0(gel(M,i))) n++;
    4307      106625 :   return n;
    4308             : }
    4309             : 
    4310             : static void indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol);
    4311             : /* As RgM_pivots, integer entries. Set *rr = dim Ker M0 */
    4312             : GEN
    4313      110537 : ZM_pivots(GEN M0, long *rr)
    4314             : {
    4315      110537 :   GEN d, dbest = NULL;
    4316             :   long m, n, i, imax, rmin, rbest, zc;
    4317      110537 :   int beenthere = 0;
    4318      110537 :   pari_sp av, av0 = avma;
    4319             :   forprime_t S;
    4320             : 
    4321      110537 :   rbest = n = lg(M0)-1;
    4322      110537 :   if (n == 0) { *rr = 0; return NULL; }
    4323      106625 :   zc = ZM_count_0_cols(M0);
    4324      106625 :   if (n == zc) { *rr = zc; return zero_zv(n); }
    4325             : 
    4326      106534 :   m = nbrows(M0);
    4327      106534 :   rmin = maxss(zc, n-m);
    4328      106534 :   init_modular_small(&S);
    4329      106534 :   imax = (n < (1<<4))? 1: (n>>3); /* heuristic */
    4330             : 
    4331             :   for(;;)
    4332           0 :   {
    4333             :     GEN row, col, M, KM, IM, RHS, X, cX;
    4334             :     long rk;
    4335      114817 :     for (av = avma, i = 0;; avma = av, i++)
    4336        8283 :     {
    4337      114817 :       ulong p = u_forprime_next(&S);
    4338             :       long rp;
    4339      114817 :       if (!p) pari_err_OVERFLOW("ZM_pivots [ran out of primes]");
    4340      114817 :       d = Flm_pivots(ZM_to_Flm(M0, p), p, &rp, 1);
    4341      114817 :       if (rp == rmin) { rbest = rp; goto END; } /* maximal rank, return */
    4342       14904 :       if (rp < rbest) { /* save best r so far */
    4343        6623 :         rbest = rp;
    4344        6623 :         if (dbest) gunclone(dbest);
    4345        6623 :         dbest = gclone(d);
    4346       13244 :         if (beenthere) break;
    4347             :       }
    4348       14904 :       if (!beenthere && i >= imax) break;
    4349             :     }
    4350        6621 :     beenthere = 1;
    4351             :     /* Dubious case: there is (probably) a non trivial kernel */
    4352        6621 :     indexrank_all(m,n, rbest, dbest, &row, &col);
    4353        6621 :     M = rowpermute(vecpermute(M0, col), row);
    4354        6621 :     rk = n - rbest; /* (probable) dimension of image */
    4355        6621 :     IM = vecslice(M,1,rk);
    4356        6621 :     KM = vecslice(M,rk+1, n);
    4357        6621 :     M = rowslice(IM, 1,rk); /* square maximal rank */
    4358        6621 :     X = ZM_gauss(M, rowslice(KM, 1,rk));
    4359        6621 :     X = Q_remove_denom(X, &cX);
    4360        6621 :     RHS = rowslice(KM,rk+1,m);
    4361        6621 :     if (cX) RHS = ZM_Z_mul(RHS, cX);
    4362        6621 :     if (ZM_equal(ZM_mul(rowslice(IM,rk+1,m), X), RHS))
    4363             :     {
    4364        6621 :       d = vecsmall_copy(dbest);
    4365        6621 :       goto END;
    4366             :     }
    4367           0 :     avma = av;
    4368             :   }
    4369             : END:
    4370      106534 :   *rr = rbest; if (dbest) gunclone(dbest);
    4371      106534 :   return gerepileuptoleaf(av0, d);
    4372             : }
    4373             : 
    4374             : /* set *pr = dim Ker x */
    4375             : static GEN
    4376       12740 : gauss_pivot(GEN x, long *pr) {
    4377             :   GEN data;
    4378       12740 :   pivot_fun pivot = get_pivot_fun(x, x, &data);
    4379       12740 :   return RgM_pivots(x, data, pr, pivot);
    4380             : }
    4381             : 
    4382             : /* compute ker(x), x0 is a reference point when guessing whether x[i,j] ~ 0
    4383             :  * (iff x[i,j] << x0[i,j]) */
    4384             : static GEN
    4385         910 : ker_aux(GEN x, GEN x0)
    4386             : {
    4387         910 :   pari_sp av = avma;
    4388             :   GEN d,y;
    4389             :   long i,j,k,r,n;
    4390             : 
    4391         910 :   x = gauss_pivot_ker(x,x0,&d,&r);
    4392         910 :   if (!r) { avma=av; return cgetg(1,t_MAT); }
    4393         903 :   n = lg(x)-1; y=cgetg(r+1,t_MAT);
    4394        1862 :   for (j=k=1; j<=r; j++,k++)
    4395             :   {
    4396         959 :     GEN p = cgetg(n+1,t_COL);
    4397             : 
    4398         959 :     gel(y,j) = p; while (d[k]) k++;
    4399        4270 :     for (i=1; i<k; i++)
    4400        3311 :       if (d[i])
    4401             :       {
    4402        3213 :         GEN p1=gcoeff(x,d[i],k);
    4403        3213 :         gel(p,i) = gcopy(p1); gunclone(p1);
    4404             :       }
    4405             :       else
    4406          98 :         gel(p,i) = gen_0;
    4407         959 :     gel(p,k) = gen_1; for (i=k+1; i<=n; i++) gel(p,i) = gen_0;
    4408             :   }
    4409         903 :   return gerepileupto(av,y);
    4410             : }
    4411             : 
    4412             : static GEN
    4413          77 : RgM_ker_FpM(GEN x, GEN p)
    4414             : {
    4415          77 :   pari_sp av = avma;
    4416             :   ulong pp;
    4417          77 :   x = RgM_Fp_init(x, p, &pp);
    4418          77 :   switch(pp)
    4419             :   {
    4420          35 :     case 0: x = FpM_to_mod(FpM_ker_gen(x,p,0),p); break;
    4421           7 :     case 2: x = F2m_to_mod(F2m_ker_sp(x,0)); break;
    4422          35 :     default:x = Flm_to_mod(Flm_ker_sp(x,pp,0), pp); break;
    4423             :   }
    4424          77 :   return gerepileupto(av, x);
    4425             : }
    4426             : 
    4427             : static GEN
    4428          84 : RgM_ker_FqM(GEN x, GEN pol, GEN p)
    4429             : {
    4430          84 :   pari_sp av = avma;
    4431          84 :   GEN T = RgX_to_FpX(pol, p);
    4432          84 :   GEN b = FqM_ker(RgM_to_FqM(x, T, p), T, p);
    4433          84 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    4434             : }
    4435             : 
    4436             : #define code(t1,t2) ((t1 << 6) | t2)
    4437             : static GEN
    4438        8631 : RgM_ker_fast(GEN x)
    4439             : {
    4440             :   GEN p, pol;
    4441             :   long pa;
    4442        8631 :   long t = RgM_type(x, &p,&pol,&pa);
    4443        8631 :   switch(t)
    4444             :   {
    4445             :     case t_INT:    /* fall through */
    4446        7637 :     case t_FRAC:   return QM_ker(x);
    4447          77 :     case t_FFELT:  return FFM_ker(x, pol);
    4448          77 :     case t_INTMOD: return RgM_ker_FpM(x, p);
    4449             :     case code(t_POLMOD, t_INTMOD):
    4450          84 :                    return RgM_ker_FqM(x, pol, p);
    4451         756 :     default:       return NULL;
    4452             :   }
    4453             : }
    4454             : #undef code
    4455             : 
    4456             : GEN
    4457        8631 : ker(GEN x)
    4458             : {
    4459        8631 :   GEN b = RgM_ker_fast(x);
    4460        8631 :   if (b) return b;
    4461         756 :   return ker_aux(x,x);
    4462             : }
    4463             : 
    4464             : GEN
    4465       46207 : matker0(GEN x,long flag)
    4466             : {
    4467       46207 :   if (typ(x)!=t_MAT) pari_err_TYPE("matker",x);
    4468       46207 :   if (!flag) return ker(x);
    4469       45934 :   RgM_check_ZM(x, "matker");
    4470       45934 :   return ZM_ker(x);
    4471             : }
    4472             : 
    4473             : static GEN
    4474          63 : RgM_image_FpM(GEN x, GEN p)
    4475             : {
    4476          63 :   pari_sp av = avma;
    4477             :   ulong pp;
    4478          63 :   x = RgM_Fp_init(x, p, &pp);
    4479          63 :   switch(pp)
    4480             :   {
    4481          28 :     case 0: x = FpM_to_mod(FpM_image(x,p),p); break;
    4482           7 :     case 2: x = F2m_to_mod(F2m_image(x)); break;
    4483          28 :     default:x = Flm_to_mod(Flm_image(x,pp), pp); break;
    4484             :   }
    4485          63 :   return gerepileupto(av, x);
    4486             : }
    4487             : 
    4488             : static GEN
    4489          28 : RgM_image_FqM(GEN x, GEN pol, GEN p)
    4490             : {
    4491          28 :   pari_sp av = avma;
    4492          28 :   GEN T = RgX_to_FpX(pol, p);
    4493          28 :   GEN b = FqM_image(RgM_to_FqM(x, T, p), T, p);
    4494          28 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    4495             : }
    4496             : 
    4497             : static GEN
    4498        1463 : QM_image(GEN A)
    4499             : {
    4500        1463 :   pari_sp av = avma;
    4501        1463 :   GEN M = vecpermute(A, ZM_indeximage(vec_Q_primpart(A)));
    4502        1463 :   return gerepilecopy(av, M);
    4503             : }
    4504             : 
    4505             : #define code(t1,t2) ((t1 << 6) | t2)
    4506             : static GEN
    4507        1617 : RgM_image_fast(GEN x)
    4508             : {
    4509             :   GEN p, pol;
    4510             :   long pa;
    4511        1617 :   long t = RgM_type(x, &p,&pol,&pa);
    4512        1617 :   switch(t)
    4513             :   {
    4514             :     case t_INT:    /* fall through */
    4515        1463 :     case t_FRAC:   return QM_image(x);
    4516          49 :     case t_FFELT:  return FFM_image(x, pol);
    4517          63 :     case t_INTMOD: return RgM_image_FpM(x, p);
    4518             :     case code(t_POLMOD, t_INTMOD):
    4519          28 :                    return RgM_image_FqM(x, pol, p);
    4520          14 :     default:       return NULL;
    4521             :   }
    4522             : }
    4523             : #undef code
    4524             : 
    4525             : GEN
    4526        1617 : image(GEN x)
    4527             : {
    4528             :   GEN d, M;
    4529             :   long r;
    4530             : 
    4531        1617 :   if (typ(x)!=t_MAT) pari_err_TYPE("matimage",x);
    4532        1617 :   M = RgM_image_fast(x);
    4533        1617 :   if (M) return M;
    4534          14 :   d = gauss_pivot(x,&r); /* d left on stack for efficiency */
    4535          14 :   return image_from_pivot(x,d,r);
    4536             : }
    4537             : 
    4538             : static GEN
    4539          84 : imagecompl_aux(GEN x, GEN(*PIVOT)(GEN,long*))
    4540             : {
    4541          84 :   pari_sp av = avma;
    4542             :   GEN d,y;
    4543             :   long j,i,r;
    4544             : 
    4545          84 :   if (typ(x)!=t_MAT) pari_err_TYPE("imagecompl",x);
    4546          84 :   (void)new_chunk(lg(x) * 4 + 1); /* HACK */
    4547          84 :   d = PIVOT(x,&r); /* if (!d) then r = 0 */
    4548          84 :   avma = av; y = cgetg(r+1,t_VECSMALL);
    4549         126 :   for (i=j=1; j<=r; i++)
    4550          42 :     if (!d[i]) y[j++] = i;
    4551          84 :   return y;
    4552             : }
    4553             : GEN
    4554          84 : imagecompl(GEN x) { return imagecompl_aux(x, &gauss_pivot); }
    4555             : GEN
    4556           0 : ZM_imagecompl(GEN x) { return imagecompl_aux(x, &ZM_pivots); }
    4557             : 
    4558             : GEN
    4559       42307 : FpM_FpC_invimage(GEN A, GEN y, GEN p)
    4560             : {
    4561       42307 :   pari_sp av = avma;
    4562       42307 :   long i, l = lg(A);
    4563             :   GEN M, x, t;
    4564             : 
    4565       42307 :   if (lgefint(p) == 3)
    4566             :   {
    4567       42300 :     ulong pp = p[2];
    4568       42300 :     A = ZM_to_Flm(A, pp);
    4569       42300 :     y = ZV_to_Flv(y, pp);
    4570       42300 :     x = Flm_Flc_invimage(A,y,pp);
    4571       42300 :     if (!x) { avma = av; return NULL; }
    4572       42300 :     return gerepileupto(av, Flc_to_ZC(x));
    4573             :   }
    4574           7 :   if (l==1) return NULL;
    4575           7 :   if (lg(y) != lgcols(A)) pari_err_DIM("FpM_FpC_invimage");
    4576           7 :   M = FpM_ker(shallowconcat(A,y),p);
    4577           7 :   i = lg(M)-1; if (!i) { avma = av; return NULL; }
    4578             : 
    4579           7 :   x = gel(M,i); t = gel(x,l);
    4580           7 :   if (!signe(t)) { avma = av; return NULL; }
    4581             : 
    4582           7 :   setlg(x,l); t = Fp_inv(negi(t),p);
    4583           7 :   if (is_pm1(t)) return gerepilecopy(av, x);
    4584           7 :   return gerepileupto(av, FpC_Fp_mul(x, t, p));
    4585             : }
    4586             : GEN
    4587       51442 : Flm_Flc_invimage(GEN A, GEN y, ulong p)
    4588             : {
    4589       51442 :   pari_sp av = avma;
    4590       51442 :   long i, l = lg(A);
    4591             :   GEN M, x;
    4592             :   ulong t;
    4593             : 
    4594       51442 :   if (l==1) return NULL;
    4595       51442 :   if (lg(y) != lgcols(A)) pari_err_DIM("Flm_Flc_invimage");
    4596       51442 :   M = cgetg(l+1,t_MAT);
    4597       51442 :   for (i=1; i<l; i++) gel(M,i) = gel(A,i);
    4598       51442 :   gel(M,l) = y; M = Flm_ker(M,p);
    4599       51442 :   i = lg(M)-1; if (!i) { avma = av; return NULL; }
    4600             : 
    4601       51442 :   x = gel(M,i); t = x[l];
    4602       51442 :   if (!t) { avma = av; return NULL; }
    4603             : 
    4604       51442 :   setlg(x,l); t = Fl_inv(Fl_neg(t,p),p);
    4605       51442 :   if (t!=1) x = Flv_Fl_mul(x, t, p);
    4606       51442 :   return gerepileuptoleaf(av, x);
    4607             : }
    4608             : GEN
    4609          35 : F2m_F2c_invimage(GEN A, GEN y)
    4610             : {
    4611          35 :   pari_sp av = avma;
    4612          35 :   long i, l = lg(A);
    4613             :   GEN M, x;
    4614             : 
    4615          35 :   if (l==1) return NULL;
    4616          35 :   if (lg(y) != lgcols(A)) pari_err_DIM("F2m_F2c_invimage");
    4617          35 :   M = cgetg(l+1,t_MAT);
    4618          35 :   for (i=1; i<l; i++) gel(M,i) = gel(A,i);
    4619          35 :   gel(M,l) = y; M = F2m_ker(M);
    4620          35 :   i = lg(M)-1; if (!i) { avma = av; return NULL; }
    4621             : 
    4622          35 :   x = gel(M,i);
    4623          35 :   if (!F2v_coeff(x,l)) { avma = av; return NULL; }
    4624          35 :   F2v_clear(x, x[1]); x[1]--; /* remove last coord */
    4625          35 :   return gerepileuptoleaf(av, x);
    4626             : }
    4627             : 
    4628             : static GEN
    4629          28 : RgM_RgC_invimage_FpC(GEN A, GEN y, GEN p)
    4630             : {
    4631          28 :   pari_sp av = avma;
    4632             :   ulong pp;
    4633             :   GEN x;
    4634          28 :   A = RgM_Fp_init(A,p,&pp);
    4635          28 :   switch(pp)
    4636             :   {
    4637             :   case 0:
    4638           7 :     y = RgC_to_FpC(y,p);
    4639           7 :     x = FpM_FpC_invimage(A, y, p);
    4640           7 :     return x ? gerepileupto(av, FpC_to_mod(x,p)): NULL;
    4641             :   case 2:
    4642           7 :     y = RgV_to_F2v(y);
    4643           7 :     x = F2m_F2c_invimage(A, y);
    4644           7 :     return x ? gerepileupto(av, F2c_to_mod(x)): NULL;
    4645             :   default:
    4646          14 :     y = RgV_to_Flv(y,pp);
    4647          14 :     x = Flm_Flc_invimage(A, y, pp);
    4648          14 :     return x ? gerepileupto(av, Flc_to_mod(x,pp)): NULL;
    4649             :   }
    4650             : }
    4651             : 
    4652             : static GEN
    4653        2051 : RgM_RgC_invimage_fast(GEN x, GEN y)
    4654             : {
    4655             :   GEN p, pol;
    4656             :   long pa;
    4657        2051 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    4658        2051 :   switch(t)
    4659             :   {
    4660          28 :     case t_INTMOD: return RgM_RgC_invimage_FpC(x, y, p);
    4661          63 :     case t_FFELT:  return FFM_FFC_invimage(x, y, pol);
    4662        1960 :     default:       return gen_0;
    4663             :   }
    4664             : }
    4665             : 
    4666             : GEN
    4667        2156 : RgM_RgC_invimage(GEN A, GEN y)
    4668             : {
    4669        2156 :   pari_sp av = avma;
    4670        2156 :   long i, l = lg(A);
    4671             :   GEN M, x, t;
    4672        2156 :   if (l==1) return NULL;
    4673        2051 :   if (lg(y) != lgcols(A)) pari_err_DIM("inverseimage");
    4674        2051 :   M = RgM_RgC_invimage_fast(A, y);
    4675        2051 :   if (!M) {avma = av; return NULL; }
    4676        2030 :   if (M != gen_0) return M;
    4677        1960 :   M = ker(shallowconcat(A, y));
    4678        1960 :   i = lg(M)-1;
    4679        1960 :   if (!i) { avma = av; return NULL; }
    4680             : 
    4681        1701 :   x = gel(M,i); t = gel(x,l);
    4682        1701 :   if (gequal0(t)) { avma = av; return NULL; }
    4683             : 
    4684        1666 :   t = gneg_i(t); setlg(x,l);
    4685        1666 :   return gerepileupto(av, RgC_Rg_div(x, t));
    4686             : }
    4687             : 
    4688             : /* Return X such that m X = v (t_COL or t_MAT), resp. an empty t_COL / t_MAT
    4689             :  * if no solution exist */
    4690             : GEN
    4691        2366 : inverseimage(GEN m, GEN v)
    4692             : {
    4693             :   GEN y;
    4694        2366 :   if (typ(m)!=t_MAT) pari_err_TYPE("inverseimage",m);
    4695        2366 :   switch(typ(v))
    4696             :   {
    4697             :     case t_COL:
    4698        2128 :       y = RgM_RgC_invimage(m,v);
    4699        2128 :       return y? y: cgetg(1,t_COL);
    4700             :     case t_MAT:
    4701         238 :       y = RgM_invimage(m, v);
    4702         238 :       return y? y: cgetg(1,t_MAT);
    4703             :   }
    4704           0 :   pari_err_TYPE("inverseimage",v);
    4705             :   return NULL;/*LCOV_EXCL_LINE*/
    4706             : }
    4707             : 
    4708             : static GEN
    4709          21 : Flm_invimage_CUP(GEN A, GEN B, ulong p) {
    4710          21 :   pari_sp av = avma;
    4711             :   GEN R, Rc, C, U, P, B1, B2, C1, C2, X, Y, Z;
    4712             :   long r;
    4713          21 :   r = Flm_CUP(A, &R, &C, &U, &P, p);
    4714          21 :   Rc = indexcompl(R, nbrows(B));
    4715          21 :   C1 = rowpermute(C, R);
    4716          21 :   C2 = rowpermute(C, Rc);
    4717          21 :   B1 = rowpermute(B, R);
    4718          21 :   B2 = rowpermute(B, Rc);
    4719          21 :   Z = Flm_rsolve_lower_unit(C1, B1, p);
    4720          21 :   if (!gequal(Flm_mul(C2, Z, p), B2))
    4721          14 :     return NULL;
    4722          14 :   Y = vconcat(Flm_rsolve_upper(vecslice(U, 1, r), Z, p),
    4723          14 :               zero_Flm(lg(A) - 1 - r, lg(B) - 1));
    4724           7 :   X = rowpermute(Y, perm_inv(P));
    4725           7 :   return gerepileupto(av, X);
    4726             : }
    4727             : 
    4728             : static GEN
    4729          42 : Flm_invimage_i(GEN A, GEN B, ulong p)
    4730             : {
    4731             :   GEN d, x, X, Y;
    4732          42 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4733             : 
    4734          42 :   if (!nB) return cgetg(1, t_MAT);
    4735          42 :   if (nA + nB >= Flm_CUP_LIMIT && nbrows(B) >= Flm_CUP_LIMIT)
    4736          21 :     return Flm_invimage_CUP(A, B, p);
    4737             : 
    4738          21 :   x = Flm_ker_sp(shallowconcat(Flm_neg(A,p), B), p, 0);
    4739             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4740             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4741             :    * Y has at least nB columns and full rank */
    4742          21 :   nY = lg(x)-1;
    4743          21 :   if (nY < nB) return NULL;
    4744          21 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    4745          21 :   d = cgetg(nB+1, t_VECSMALL);
    4746          56 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4747             :   {
    4748          49 :     for (; j>=1; j--)
    4749          42 :       if (coeff(Y,i,j)) { d[i] = j; break; }
    4750          42 :     if (!j) return NULL;
    4751             :   }
    4752             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    4753          14 :   Y = vecpermute(Y, d);
    4754          14 :   x = vecpermute(x, d);
    4755          14 :   X = rowslice(x, 1, nA);
    4756          14 :   return Flm_mul(X, Flm_inv_upper_1(Y,p), p);
    4757             : }
    4758             : 
    4759             : static GEN
    4760           7 : F2m_invimage_i(GEN A, GEN B)
    4761             : {
    4762             :   GEN d, x, X, Y;
    4763           7 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4764           7 :   x = F2m_ker_sp(shallowconcat(A, B), 0);
    4765             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4766             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4767             :    * Y has at least nB columns and full rank */
    4768           7 :   nY = lg(x)-1;
    4769           7 :   if (nY < nB) return NULL;
    4770             : 
    4771             :   /* implicitly: Y = rowslice(x, nA+1, nA+nB), nB rows */
    4772           7 :   d = cgetg(nB+1, t_VECSMALL);
    4773          21 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4774             :   {
    4775          14 :     for (; j>=1; j--)
    4776          14 :       if (F2m_coeff(x,nA+i,j)) { d[i] = j; break; } /* Y[i,j] */
    4777          14 :     if (!j) return NULL;
    4778             :   }
    4779           7 :   x = vecpermute(x, d);
    4780             : 
    4781           7 :   X = F2m_rowslice(x, 1, nA);
    4782           7 :   Y = F2m_rowslice(x, nA+1, nA+nB);
    4783           7 :   return F2m_mul(X, F2m_inv_upper_1(Y));
    4784             : }
    4785             : GEN
    4786           0 : Flm_invimage(GEN A, GEN B, ulong p)
    4787             : {
    4788           0 :   pari_sp av = avma;
    4789           0 :   GEN X = Flm_invimage_i(A,B,p);
    4790           0 :   if (!X) { avma = av; return NULL; }
    4791           0 :   return gerepileupto(av, X);
    4792             : }
    4793             : GEN
    4794           0 : F2m_invimage(GEN A, GEN B)
    4795             : {
    4796           0 :   pari_sp av = avma;
    4797           0 :   GEN X = F2m_invimage_i(A,B);
    4798           0 :   if (!X) { avma = av; return NULL; }
    4799           0 :   return gerepileupto(av, X);
    4800             : }
    4801             : static GEN
    4802          35 : FpM_invimage_i(GEN A, GEN B, GEN p)
    4803             : {
    4804             :   GEN d, x, X, Y;
    4805          35 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4806          35 :   if (lgefint(p) == 3)
    4807             :   {
    4808           0 :     ulong pp = p[2];
    4809           0 :     A = ZM_to_Flm(A, pp);
    4810           0 :     B = ZM_to_Flm(B, pp);
    4811           0 :     x = Flm_invimage_i(A, B, pp);
    4812           0 :     return x? Flm_to_ZM(x): NULL;
    4813             :   }
    4814          35 :   x = FpM_ker(shallowconcat(ZM_neg(A), B), p);
    4815             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4816             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4817             :    * Y has at least nB columns and full rank */
    4818          35 :   nY = lg(x)-1;
    4819          35 :   if (nY < nB) return NULL;
    4820          28 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    4821          28 :   d = cgetg(nB+1, t_VECSMALL);
    4822         217 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4823             :   {
    4824         280 :     for (; j>=1; j--)
    4825         266 :       if (signe(gcoeff(Y,i,j))) { d[i] = j; break; }
    4826         203 :     if (!j) return NULL;
    4827             :   }
    4828             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    4829          14 :   Y = vecpermute(Y, d);
    4830          14 :   x = vecpermute(x, d);
    4831          14 :   X = rowslice(x, 1, nA);
    4832          14 :   return FpM_mul(X, FpM_inv_upper_1(Y,p), p);
    4833             : }
    4834             : GEN
    4835           0 : FpM_invimage(GEN A, GEN B, GEN p)
    4836             : {
    4837           0 :   pari_sp av = avma;
    4838           0 :   GEN X = FpM_invimage_i(A,B,p);
    4839           0 :   if (!X) { avma = av; return NULL; }
    4840           0 :   return gerepileupto(av, X);
    4841             : }
    4842             : 
    4843             : static GEN
    4844          84 : RgM_invimage_FpM(GEN A, GEN B, GEN p)
    4845             : {
    4846          84 :   pari_sp av = avma;
    4847             :   ulong pp;
    4848             :   GEN x;
    4849          84 :   A = RgM_Fp_init(A,p,&pp);
    4850          84 :   switch(pp)
    4851             :   {
    4852             :   case 0:
    4853          35 :     B = RgM_to_FpM(B,p);
    4854          35 :     x = FpM_invimage_i(A, B, p);
    4855          35 :     return x ? gerepileupto(av, FpM_to_mod(x, p)): x;
    4856             :   case 2:
    4857           7 :     B = RgM_to_F2m(B);
    4858           7 :     x = F2m_invimage_i(A, B);
    4859           7 :     return x ? gerepileupto(av, F2m_to_mod(x)): x;
    4860             :   default:
    4861          42 :     B = RgM_to_Flm(B,pp);
    4862          42 :     x = Flm_invimage_i(A, B, pp);
    4863          42 :     return x ? gerepileupto(av, Flm_to_mod(x, pp)): x;
    4864             :   }
    4865             : }
    4866             : 
    4867             : static GEN
    4868         252 : RgM_invimage_fast(GEN x, GEN y)
    4869             : {
    4870             :   GEN p, pol;
    4871             :   long pa;
    4872         252 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    4873         252 :   switch(t)
    4874             :   {
    4875          84 :     case t_INTMOD: return RgM_invimage_FpM(x, y, p);
    4876         105 :     case t_FFELT:  return FFM_invimage(x, y, pol);
    4877          63 :     default:       return gen_0;
    4878             :   }
    4879             : }
    4880             : 
    4881             : /* find Z such that A Z = B. Return NULL if no solution */
    4882             : GEN
    4883         252 : RgM_invimage(GEN A, GEN B)
    4884             : {
    4885         252 :   pari_sp av = avma;
    4886             :   GEN d, x, X, Y;
    4887         252 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4888         252 :   X = RgM_invimage_fast(A, B);
    4889         252 :   if (!X) {avma = av; return NULL; }
    4890         140 :   if (X != gen_0) return X;
    4891          63 :   x = ker(shallowconcat(RgM_neg(A), B));
    4892             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4893             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4894             :    * Y has at least nB columns and full rank */
    4895          63 :   nY = lg(x)-1;
    4896          63 :   if (nY < nB) { avma = av; return NULL; }
    4897          49 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    4898          49 :   d = cgetg(nB+1, t_VECSMALL);
    4899         441 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4900             :   {
    4901         546 :     for (; j>=1; j--)
    4902         532 :       if (!gequal0(gcoeff(Y,i,j))) { d[i] = j; break; }
    4903         406 :     if (!j) { avma = av; return NULL; }
    4904             :   }
    4905             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    4906          35 :   Y = vecpermute(Y, d);
    4907          35 :   x = vecpermute(x, d);
    4908          35 :   X = rowslice(x, 1, nA);
    4909          35 :   return gerepileupto(av, RgM_mul(X, RgM_inv_upper(Y)));
    4910             : }
    4911             : 
    4912             : /* r = dim Ker x, n = nbrows(x) */
    4913             : static GEN
    4914       35840 : get_suppl(GEN x, GEN d, long n, long r, GEN(*ei)(long,long))
    4915             : {
    4916             :   pari_sp av;
    4917             :   GEN y, c;
    4918       35840 :   long j, k, rx = lg(x)-1; /* != 0 due to init_suppl() */
    4919             : 
    4920       35840 :   if (rx == n && r == 0) return gcopy(x);
    4921       33175 :   y = cgetg(n+1, t_MAT);
    4922       33175 :   av = avma; c = zero_zv(n);
    4923             :   /* c = lines containing pivots (could get it from gauss_pivot, but cheap)
    4924             :    * In theory r = 0 and d[j] > 0 for all j, but why take chances? */
    4925      257163 :   for (k = j = 1; j<=rx; j++)
    4926      223988 :     if (d[j]) { c[ d[j] ] = 1; gel(y,k++) = gel(x,j); }
    4927      342298 :   for (j=1; j<=n; j++)
    4928      309123 :     if (!c[j]) gel(y,k++) = (GEN)j; /* HACK */
    4929       33175 :   avma = av;
    4930             : 
    4931       33175 :   rx -= r;
    4932       33175 :   for (j=1; j<=rx; j++) gel(y,j) = gcopy(gel(y,j));
    4933       33175 :   for (   ; j<=n; j++)  gel(y,j) = ei(n, y[j]);
    4934       33175 :   return y;
    4935             : }
    4936             : 
    4937             : static void
    4938       35840 : init_suppl(GEN x)
    4939             : {
    4940       35840 :   if (lg(x) == 1) pari_err_IMPL("suppl [empty matrix]");
    4941             :   /* HACK: avoid overwriting d from gauss_pivot() after avma=av */
    4942       35840 :   (void)new_chunk(lgcols(x) * 2);
    4943       35840 : }
    4944             : 
    4945             : GEN
    4946       34461 : FpM_suppl(GEN x, GEN p)
    4947             : {
    4948             :   GEN d;
    4949             :   long r;
    4950       34461 :   init_suppl(x); d = FpM_gauss_pivot(x,p, &r);
    4951       34461 :   return get_suppl(x,d,nbrows(x),r,&col_ei);
    4952             : }
    4953             : 
    4954             : GEN
    4955          63 : Flm_suppl(GEN x, ulong p)
    4956             : {
    4957             :   GEN d;
    4958             :   long r;
    4959          63 :   init_suppl(x); d = Flm_pivots(x, p, &r, 0);
    4960          63 :   return get_suppl(x,d,nbrows(x),r,&vecsmall_ei);
    4961             : }
    4962             : 
    4963             : GEN
    4964          14 : F2m_suppl(GEN x)
    4965             : {
    4966             :   GEN d;
    4967             :   long r;
    4968          14 :   init_suppl(x); d = F2m_gauss_pivot(F2m_copy(x), &r);
    4969          14 :   return get_suppl(x,d,mael(x,1,1),r,&F2v_ei);
    4970             : }
    4971             : 
    4972             : static GEN
    4973          70 : RgM_suppl_FpM(GEN x, GEN p)
    4974             : {
    4975          70 :   pari_sp av = avma;
    4976             :   ulong pp;
    4977          70 :   x = RgM_Fp_init(x, p, &pp);
    4978          70 :   switch(pp)
    4979             :   {
    4980          21 :   case 0: x = FpM_to_mod(FpM_suppl(x,p), p); break;
    4981          14 :   case 2: x = F2m_to_mod(F2m_suppl(x)); break;
    4982          35 :   default:x = Flm_to_mod(Flm_suppl(x,pp), pp); break;
    4983             :   }
    4984          70 :   return gerepileupto(av, x);
    4985             : }
    4986             : 
    4987             : static GEN
    4988         175 : RgM_suppl_fast(GEN x)
    4989             : {
    4990             :   GEN p, pol;
    4991             :   long pa;
    4992         175 :   long t = RgM_type(x,&p,&pol,&pa);
    4993         175 :   switch(t)
    4994             :   {
    4995          70 :     case t_INTMOD: return RgM_suppl_FpM(x, p);
    4996          35 :     case t_FFELT:  return FFM_suppl(x, pol);
    4997          70 :     default:       return NULL;
    4998             :   }
    4999             : }
    5000             : 
    5001             : /* x is an n x k matrix, rank(x) = k <= n. Return an invertible n x n matrix
    5002             :  * whose first k columns are given by x. If rank(x) < k, undefined result. */
    5003             : GEN
    5004         175 : suppl(GEN x)
    5005             : {
    5006         175 :   pari_sp av = avma;
    5007             :   GEN d, M;
    5008             :   long r;
    5009         175 :   if (typ(x)!=t_MAT) pari_err_TYPE("suppl",x);
    5010         175 :   M = RgM_suppl_fast(x);
    5011         175 :   if (M) return M;
    5012          70 :   init_suppl(x);
    5013          70 :   d = gauss_pivot(x,&r);
    5014          70 :   avma = av; return get_suppl(x,d,nbrows(x),r,&col_ei);
    5015             : }
    5016             : /* variable number to be filled in later */
    5017             : static GEN
    5018          84 : _FlxC_ei(long n, long i)
    5019             : {
    5020          84 :   GEN x = cgetg(n + 1, t_COL);
    5021             :   long j;
    5022        1652 :   for (j = 1; j <= n; j++)
    5023        1568 :     gel(x, j) = (j == i)? pol1_Flx(0): pol0_Flx(0);
    5024          84 :   return x;
    5025             : }
    5026             : 
    5027             : GEN
    5028           7 : F2xqM_suppl(GEN x, GEN T)
    5029             : {
    5030           7 :   pari_sp av = avma;
    5031             :   GEN d, y;
    5032           7 :   long n = nbrows(x), r, sv = get_Flx_var(T);
    5033             : 
    5034           7 :   init_suppl(x);
    5035           7 :   d = F2xqM_gauss_pivot(x, T, &r);
    5036           7 :   avma = av;
    5037           7 :   y = get_suppl(x, d, n, r, &_FlxC_ei);
    5038           7 :   if (sv) {
    5039             :     long i, j;
    5040          21 :     for (j = r + 1; j <= n; j++) {
    5041          42 :       for (i = 1; i <= n; i++)
    5042          28 :         gcoeff(y, i, j)[1] = sv;
    5043             :     }
    5044             :   }
    5045           7 :   return y;
    5046             : }
    5047             : 
    5048             : GEN
    5049          14 : FlxqM_suppl(GEN x, GEN T, ulong p)
    5050             : {
    5051          14 :   pari_sp av = avma;
    5052             :   GEN d, y;
    5053          14 :   long n = nbrows(x), r, sv = get_Flx_var(T);
    5054             : 
    5055          14 :   init_suppl(x);
    5056          14 :   d = FlxqM_gauss_pivot(x, T, p, &r);
    5057          14 :   avma = av;
    5058          14 :   y = get_suppl(x, d, n, r, &_FlxC_ei);
    5059          14 :   if (sv) {
    5060             :     long i, j;
    5061          21 :     for (j = r + 1; j <= n; j++) {
    5062          42 :       for (i = 1; i <= n; i++)
    5063          28 :         gcoeff(y, i, j)[1] = sv;
    5064             :     }
    5065             :   }
    5066          14 :   return y;
    5067             : }
    5068             : 
    5069             : GEN
    5070        4039 : FqM_suppl(GEN x, GEN T, GEN p)
    5071             : {
    5072        4039 :   pari_sp av = avma;
    5073             :   GEN d;
    5074             :   long r;
    5075             : 
    5076        4039 :   if (!T) return FpM_suppl(x,p);
    5077        1211 :   init_suppl(x);
    5078        1211 :   d = FqM_gauss_pivot(x,T,p,&r);
    5079        1211 :   avma = av; return get_suppl(x,d,nbrows(x),r,&col_ei);
    5080             : }
    5081             : 
    5082             : GEN
    5083           7 : image2(GEN x)
    5084             : {
    5085           7 :   pari_sp av = avma;
    5086             :   long k, n, i;
    5087             :   GEN A, B;
    5088             : 
    5089           7 :   if (typ(x)!=t_MAT) pari_err_TYPE("image2",x);
    5090           7 :   if (lg(x) == 1) return cgetg(1,t_MAT);
    5091           7 :   A = ker(x); k = lg(A)-1;
    5092           7 :   if (!k) { avma = av; return gcopy(x); }
    5093           7 :   A = suppl(A); n = lg(A)-1;
    5094           7 :   B = cgetg(n-k+1, t_MAT);
    5095           7 :   for (i = k+1; i <= n; i++) gel(B,i-k) = RgM_RgC_mul(x, gel(A,i));
    5096           7 :   return gerepileupto(av, B);
    5097             : }
    5098             : 
    5099             : GEN
    5100         203 : matimage0(GEN x,long flag)
    5101             : {
    5102         203 :   switch(flag)
    5103             :   {
    5104         196 :     case 0: return image(x);
    5105           7 :     case 1: return image2(x);
    5106           0 :     default: pari_err_FLAG("matimage");
    5107             :   }
    5108             :   return NULL; /* LCOV_EXCL_LINE */
    5109             : }
    5110             : 
    5111             : static long
    5112         126 : RgM_rank_FpM(GEN x, GEN p)
    5113             : {
    5114         126 :   pari_sp av = avma;
    5115             :   ulong pp;
    5116             :   long r;
    5117         126 :   x = RgM_Fp_init(x,p,&pp);
    5118         126 :   switch(pp)
    5119             :   {
    5120          28 :   case 0: r = FpM_rank(x,p); break;
    5121          63 :   case 2: r = F2m_rank(x); break;
    5122          35 :   default:r = Flm_rank(x,pp); break;
    5123             :   }
    5124         126 :   avma = av; return r;
    5125             : }
    5126             : 
    5127             : static long
    5128          42 : RgM_rank_FqM(GEN x, GEN pol, GEN p)
    5129             : {
    5130          42 :   pari_sp av = avma;
    5131          42 :   GEN T = RgX_to_FpX(pol, p);
    5132          42 :   long r = FqM_rank(RgM_to_FqM(x, T, p), T, p);
    5133          42 :   avma = av;
    5134          42 :   return r;
    5135             : }
    5136             : 
    5137             : #define code(t1,t2) ((t1 << 6) | t2)
    5138             : static long
    5139         280 : RgM_rank_fast(GEN x)
    5140             : {
    5141             :   GEN p, pol;
    5142             :   long pa;
    5143         280 :   long t = RgM_type(x,&p,&pol,&pa);
    5144         280 :   switch(t)
    5145             :   {
    5146          42 :     case t_INT:    return ZM_rank(x);
    5147           0 :     case t_FRAC:   return QM_rank(x);
    5148         126 :     case t_INTMOD: return RgM_rank_FpM(x, p);
    5149          63 :     case t_FFELT:  return FFM_rank(x, pol);
    5150             :     case code(t_POLMOD, t_INTMOD):
    5151          42 :                    return RgM_rank_FqM(x, pol, p);
    5152           7 :     default:       return -1;
    5153             :   }
    5154             : }
    5155             : #undef code
    5156             : 
    5157             : long
    5158         280 : rank(GEN x)
    5159             : {
    5160         280 :   pari_sp av = avma;
    5161             :   long r;
    5162             : 
    5163         280 :   if (typ(x)!=t_MAT) pari_err_TYPE("rank",x);
    5164         280 :   r = RgM_rank_fast(x);
    5165         280 :   if (r >= 0) return r;
    5166           7 :   (void)gauss_pivot(x, &r);
    5167           7 :   avma = av; return lg(x)-1 - r;
    5168             : }
    5169             : 
    5170             : /* d a t_VECSMALL of integers in 1..n. Return the vector of the d[i]
    5171             :  * followed by the missing indices */
    5172             : static GEN
    5173       13242 : perm_complete(GEN d, long n)
    5174             : {
    5175       13242 :   GEN y = cgetg(n+1, t_VECSMALL);
    5176       13242 :   long i, j = 1, k = n, l = lg(d);
    5177       13242 :   pari_sp av = avma;
    5178       13242 :   char *T = stack_calloc(n+1);
    5179       13242 :   for (i = 1; i < l; i++) T[d[i]] = 1;
    5180      135217 :   for (i = 1; i <= n; i++)
    5181      121975 :     if (T[i]) y[j++] = i; else y[k--] = i;
    5182       13242 :   avma = av; return y;
    5183             : }
    5184             : 
    5185             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
    5186             : static GEN
    5187       85896 : indexrank0(long n, long r, GEN d)
    5188             : {
    5189       85896 :   GEN p1, p2, res = cgetg(3,t_VEC);
    5190             :   long i, j;
    5191             : 
    5192       85896 :   r = n - r; /* now r = dim Im(x) */
    5193       85896 :   p1 = cgetg(r+1,t_VECSMALL); gel(res,1) = p1;
    5194       85896 :   p2 = cgetg(r+1,t_VECSMALL); gel(res,2) = p2;
    5195       85896 :   if (d)
    5196             :   {
    5197      501329 :     for (i=0,j=1; j<=n; j++)
    5198      416343 :       if (d[j]) { i++; p1[i] = d[j]; p2[i] = j; }
    5199       84986 :     vecsmall_sort(p1);
    5200             :   }
    5201       85896 :   return res;
    5202             : }
    5203             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
    5204             : static GEN
    5205        2422 : indeximage0(long n, long r, GEN d)
    5206             : {
    5207             :   long i, j;
    5208             :   GEN v;
    5209             : 
    5210        2422 :   r = n - r; /* now r = dim Im(x) */
    5211        2422 :   v = cgetg(r+1,t_VECSMALL);
    5212       17108 :   if (d) for (i=j=1; j<=n; j++)
    5213       14686 :     if (d[j]) v[i++] = j;
    5214        2422 :   return v;
    5215             : }
    5216             : /* x an m x n t_MAT, n > 0, r = dim Ker(x), d from gauss_pivot */
    5217             : static void
    5218        6621 : indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol)
    5219             : {
    5220        6621 :   GEN IR = indexrank0(n, r, d);
    5221        6621 :   *prow = perm_complete(gel(IR,1), m);
    5222        6621 :   *pcol = perm_complete(gel(IR,2), n);
    5223        6621 : }
    5224             : static void
    5225       81697 : init_indexrank(GEN x) {
    5226       81697 :   (void)new_chunk(3 + 2*lg(x)); /* HACK */
    5227       81697 : }
    5228             : 
    5229             : static GEN
    5230          28 : RgM_indexrank_FpM(GEN x, GEN p)
    5231             : {
    5232          28 :   pari_sp av = avma;
    5233             :   ulong pp;
    5234             :   GEN r;
    5235          28 :   x = RgM_Fp_init(x,p,&pp);
    5236          28 :   switch(pp)
    5237             :   {
    5238           7 :   case 0:  r = FpM_indexrank(x,p); break;
    5239           7 :   case 2:  r = F2m_indexrank(x); break;
    5240          14 :   default: r = Flm_indexrank(x,pp); break;
    5241             :   }
    5242          28 :   return gerepileupto(av, r);
    5243             : }
    5244             : 
    5245             : static GEN
    5246           0 : RgM_indexrank_FqM(GEN x, GEN pol, GEN p)
    5247             : {
    5248           0 :   pari_sp av = avma;
    5249           0 :   GEN T = RgX_to_FpX(pol, p);
    5250           0 :   GEN r = FqM_indexrank(RgM_to_FqM(x, T, p), T, p);
    5251           0 :   return gerepileupto(av, r);
    5252             : }
    5253             : 
    5254             : #define code(t1,t2) ((t1 << 6) | t2)
    5255             : static GEN
    5256       14154 : RgM_indexrank_fast(GEN x)
    5257             : {
    5258             :   GEN p, pol;
    5259             :   long pa;
    5260       14154 :   long t = RgM_type(x,&p,&pol,&pa);
    5261       14154 :   switch(t)
    5262             :   {
    5263         392 :     case t_INT:    return ZM_indexrank(x);
    5264        1148 :     case t_FRAC:   return QM_indexrank(x);
    5265          28 :     case t_INTMOD: return RgM_indexrank_FpM(x, p);
    5266          21 :     case t_FFELT:  return FFM_indexrank(x, pol);
    5267             :     case code(t_POLMOD, t_INTMOD):
    5268           0 :                    return RgM_indexrank_FqM(x, pol, p);
    5269       12565 :     default:       return NULL;
    5270             :   }
    5271             : }
    5272             : #undef code
    5273             : 
    5274             : GEN
    5275       14154 : indexrank(GEN x)
    5276             : {
    5277             :   pari_sp av;
    5278             :   long r;
    5279             :   GEN d;
    5280       14154 :   if (typ(x)!=t_MAT) pari_err_TYPE("indexrank",x);
    5281       14154 :   d = RgM_indexrank_fast(x);
    5282       14154 :   if (d) return d;
    5283       12565 :   av = avma;
    5284       12565 :   init_indexrank(x);
    5285       12565 :   d = gauss_pivot(x, &r);
    5286       12565 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5287             : }
    5288             : 
    5289             : GEN
    5290       29113 : FpM_indexrank(GEN x, GEN p) {
    5291       29113 :   pari_sp av = avma;
    5292             :   long r;
    5293             :   GEN d;
    5294       29113 :   init_indexrank(x);
    5295       29113 :   d = FpM_gauss_pivot(x,p,&r);
    5296       29113 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5297             : }
    5298             : 
    5299             : GEN
    5300       18676 : Flm_indexrank(GEN x, ulong p) {
    5301       18676 :   pari_sp av = avma;
    5302             :   long r;
    5303             :   GEN d;
    5304       18676 :   init_indexrank(x);
    5305       18676 :   d = Flm_pivots(x, p, &r, 0);
    5306       18676 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5307             : }
    5308             : 
    5309             : GEN
    5310           7 : F2m_indexrank(GEN x) {
    5311           7 :   pari_sp av = avma;
    5312             :   long r;
    5313             :   GEN d;
    5314           7 :   init_indexrank(x);
    5315           7 :   d = F2m_gauss_pivot(F2m_copy(x),&r);
    5316           7 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5317             : }
    5318             : 
    5319             : GEN
    5320           7 : F2xqM_indexrank(GEN x, GEN T) {
    5321           7 :   pari_sp av = avma;
    5322             :   long r;
    5323             :   GEN d;
    5324           7 :   init_indexrank(x);
    5325           7 :   d = F2xqM_gauss_pivot(x, T, &r);
    5326           7 :   avma = av; return indexrank0(lg(x) - 1, r, d);
    5327             : }
    5328             : 
    5329             : GEN
    5330           7 : FlxqM_indexrank(GEN x, GEN T, ulong p) {
    5331           7 :   pari_sp av = avma;
    5332             :   long r;
    5333             :   GEN d;
    5334           7 :   init_indexrank(x);
    5335           7 :   d = FlxqM_gauss_pivot(x, T, p, &r);
    5336           7 :   avma = av; return indexrank0(lg(x) - 1, r, d);
    5337             : }
    5338             : 
    5339             : GEN
    5340           7 : FqM_indexrank(GEN x, GEN T, GEN p) {
    5341           7 :   pari_sp av = avma;
    5342             :   long r;
    5343             :   GEN d;
    5344           7 :   init_indexrank(x);
    5345           7 :   d = FqM_gauss_pivot(x, T, p, &r);
    5346           7 :   avma = av; return indexrank0(lg(x) - 1, r, d);
    5347             : }
    5348             : 
    5349             : GEN
    5350        2422 : ZM_indeximage(GEN x) {
    5351        2422 :   pari_sp av = avma;
    5352             :   long r;
    5353             :   GEN d;
    5354        2422 :   init_indexrank(x);
    5355        2422 :   d = ZM_pivots(x,&r);
    5356        2422 :   avma = av; return indeximage0(lg(x)-1, r, d);
    5357             : }
    5358             : long
    5359       43669 : ZM_rank(GEN x) {
    5360       43669 :   pari_sp av = avma;
    5361             :   long r;
    5362       43669 :   (void)ZM_pivots(x,&r);
    5363       43669 :   avma = av; return lg(x)-1-r;
    5364             : }
    5365             : GEN
    5366       18893 : ZM_indexrank(GEN x) {
    5367       18893 :   pari_sp av = avma;
    5368             :   long r;
    5369             :   GEN d;
    5370       18893 :   init_indexrank(x);
    5371       18893 :   d = ZM_pivots(x,&r);
    5372       18893 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5373             : }
    5374             : 
    5375             : long
    5376           0 : QM_rank(GEN x)
    5377             : {
    5378           0 :   pari_sp av = avma;
    5379           0 :   long r = ZM_rank(Q_primpart(x));
    5380           0 :   avma = av;
    5381           0 :   return r;
    5382             : }
    5383             : 
    5384             : GEN
    5385        1148 : QM_indexrank(GEN x)
    5386             : {
    5387        1148 :   pari_sp av = avma;
    5388        1148 :   GEN r = ZM_indexrank(Q_primpart(x));
    5389        1148 :   return gerepileupto(av, r);
    5390             : }
    5391             : 
    5392             : /*******************************************************************/
    5393             : /*                                                                 */
    5394             : /*                             ZabM                                */
    5395             : /*                                                                 */
    5396             : /*******************************************************************/
    5397             : 
    5398             : static GEN
    5399        1090 : FpXM_ratlift(GEN a, GEN q)
    5400             : {
    5401             :   GEN B, y;
    5402        1090 :   long i, j, l = lg(a), n;
    5403        1090 :   B = sqrti(shifti(q,-1));
    5404        1090 :   y = cgetg(l, t_MAT);
    5405        1090 :   if (l==1) return y;
    5406        1090 :   n = lgcols(a);
    5407        3562 :   for (i=1; i<l; i++)
    5408             :   {
    5409        2866 :     GEN yi = cgetg(n, t_COL);
    5410       40711 :     for (j=1; j<n; j++)
    5411             :     {
    5412       38239 :       GEN v = FpX_ratlift(gmael(a,i,j), q, B, B, NULL);
    5413       38239 :       if (!v) return NULL;
    5414       37845 :       gel(yi, j) = RgX_renormalize(v);
    5415             :     }
    5416        2472 :     gel(y,i) = yi;
    5417             :   }
    5418         696 :   return y;
    5419             : }
    5420             : 
    5421             : static GEN
    5422        3115 : FlmV_recover_pre(GEN a, GEN M, ulong p, ulong pi, long sv)
    5423             : {
    5424        3115 :   GEN a1 = gel(a,1);
    5425        3115 :   long i, j, k, l = lg(a1), n, lM = lg(M);
    5426        3115 :   GEN v = cgetg(lM, t_VECSMALL);
    5427        3115 :   GEN y = cgetg(l, t_MAT);
    5428        3115 :   if (l==1) return y;
    5429        3115 :   n = lgcols(a1);
    5430       25433 :   for (i=1; i<l; i++)
    5431             :   {
    5432       22318 :     GEN yi = cgetg(n, t_COL);
    5433      528655 :     for (j=1; j<n; j++)
    5434             :     {
    5435      506337 :       for (k=1; k<lM; k++) uel(v,k) = umael(gel(a,k),i,j);
    5436      506337 :       gel(yi, j) = Flm_Flc_mul_pre_Flx(M, v, p, pi, sv);
    5437             :     }
    5438       22318 :     gel(y,i) = yi;
    5439             :   }
    5440        3115 :   return y;
    5441             : }
    5442             : 
    5443             : static GEN
    5444           0 : FlkM_inv(GEN M, GEN P, ulong p)
    5445             : {
    5446           0 :   ulong pi = get_Fl_red(p);
    5447           0 :   GEN R = Flx_roots(P, p);
    5448           0 :   long l = lg(R), i;
    5449           0 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    5450           0 :   GEN V = cgetg(l, t_VEC);
    5451           0 :   for(i=1; i<l; i++)
    5452             :   {
    5453           0 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, pi);
    5454           0 :     GEN H = Flm_inv_sp(FlxM_eval_powers_pre(M, pows, p, pi), NULL, p);
    5455           0 :     if (!H) return NULL;
    5456           0 :     gel(V, i) = H;
    5457             :   }
    5458           0 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    5459             : }
    5460             : 
    5461             : static GEN
    5462        2025 : FlkM_adjoint(GEN M, GEN P, ulong p)
    5463             : {
    5464        2025 :   ulong pi = get_Fl_red(p);
    5465        2025 :   GEN R = Flx_roots(P, p);
    5466        2025 :   long l = lg(R), i;
    5467        2025 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    5468        2025 :   GEN V = cgetg(l, t_VEC);
    5469       10577 :   for(i=1; i<l; i++)
    5470             :   {
    5471        8552 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, pi);
    5472        8552 :     gel(V, i) = Flm_adjoint(FlxM_eval_powers_pre(M, pows, p, pi), p);
    5473             :   }
    5474        2025 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    5475             : }
    5476             : 
    5477             : 
    5478             : static GEN
    5479        1935 : ZabM_inv_slice(GEN A, GEN Q, GEN P, GEN *mod)
    5480             : {
    5481        1935 :   pari_sp av = avma;
    5482        1935 :   long i, n = lg(P)-1, w = varn(Q);
    5483             :   GEN H, T;
    5484        1935 :   if (n == 1)
    5485             :   {
    5486        1869 :     ulong p = uel(P,1);
    5487        1869 :     GEN Ap = FqM_to_FlxM(A, Q, utoi(p));
    5488        1869 :     GEN Qp = ZX_to_Flx(Q, p);
    5489        1869 :     GEN Hp = FlkM_adjoint(Ap, Qp, p);
    5490        1869 :     Hp = gerepileupto(av, FlxM_to_ZXM(Hp));
    5491        1869 :     *mod = utoi(p); return Hp;
    5492             :   }
    5493          66 :   T = ZV_producttree(P);
    5494          66 :   A = ZXM_nv_mod_tree(A, P, T, w);
    5495          66 :   Q = ZX_nv_mod_tree(Q, P, T);
    5496          66 :   H = cgetg(n+1, t_VEC);
    5497         222 :   for(i=1; i <= n; i++)
    5498             :   {
    5499         156 :     ulong p = P[i];
    5500         156 :     GEN a = gel(A,i), q = gel(Q, i);
    5501         156 :     gel(H,i) = FlkM_adjoint(a, q, p);
    5502             :   }
    5503          66 :   H = nxMV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    5504          66 :   *mod = gmael(T, lg(T)-1, 1);
    5505          66 :   gerepileall(av, 2, &H, mod);
    5506          66 :   return H;
    5507             : }
    5508             : 
    5509             : GEN
    5510        1935 : ZabM_inv_worker(GEN P, GEN A, GEN Q)
    5511             : {
    5512        1935 :   GEN V = cgetg(3, t_VEC);
    5513        1935 :   gel(V,1) = ZabM_inv_slice(A, Q, P, &gel(V,2));
    5514        1935 :   return V;
    5515             : }
    5516             : 
    5517             : static GEN
    5518        6006 : vecnorml1(GEN a)
    5519             : {
    5520             :   long i, l;
    5521        6006 :   GEN g = cgetg_copy(a, &l);
    5522       99974 :   for (i=1; i<l; i++)
    5523       93968 :     gel(g, i) = gnorml1_fake(gel(a,i));
    5524        6006 :   return g;
    5525             : }
    5526             : 
    5527             : static GEN
    5528        1365 : ZabM_true_Hadamard(GEN a)
    5529             : {
    5530        1365 :   pari_sp av = avma;
    5531        1365 :   long n = lg(a)-1, i;
    5532             :   GEN B;
    5533        1365 :   if (n == 0) return gen_1;
    5534        1365 :   if (n == 1) return gnorml1_fake(gcoeff(a,1,1));
    5535         847 :   B = gen_1;
    5536         847 :   for (i = 1; i <= n; i++) B = gmul(B, gnorml2(RgC_gtofp(vecnorml1(gel(a,i)),DEFAULTPREC)));
    5537         847 :   return gerepileuptoint(av, ceil_safe(sqrtr_abs(B)));
    5538             : }
    5539             : 
    5540             : GEN
    5541        1365 : ZabM_inv(GEN A, GEN Q, long n, GEN *pt_den)
    5542             : {
    5543        1365 :   pari_sp av = avma;
    5544        1365 :   long m = lg(A)-1;
    5545             :   GEN bnd, H, D, d, mod, worker;
    5546        1365 :   if (m == 0)
    5547             :   {
    5548           0 :     if (pt_den) *pt_den = gen_1;
    5549           0 :     return cgetg(1, t_MAT);
    5550             :   }
    5551        1365 :   bnd = ZabM_true_Hadamard(A);
    5552        1365 :   worker = strtoclosure("_ZabM_inv_worker", 2, A, Q);
    5553        1365 :   H = gen_crt("ZabM_inv", worker, mkvecsmall(n), expi(bnd), m, &mod,
    5554             :               nxMV_chinese_center, FpXM_center);
    5555        1365 :   D = RgMrow_RgC_mul(H, gel(A,1), 1);
    5556        1365 :   D = ZX_rem(D, Q);
    5557        1365 :   d = Z_content(mkvec2(H, D));
    5558        1365 :   if (d)
    5559             :   {
    5560         539 :     D = ZX_Z_divexact(D, d);
    5561         539 :     H = Q_div_to_int(H, d);
    5562             :   }
    5563        1365 :   if (pt_den)
    5564             :   {
    5565        1365 :     gerepileall(av, 2, &H, &D);
    5566        1365 :     *pt_den = D; return H;
    5567             :   }
    5568           0 :   return gerepileupto(av, H);
    5569             : }
    5570             : 
    5571             : GEN
    5572           0 : ZabM_inv_ratlift(GEN M, GEN P, long n, GEN *pden)
    5573             : {
    5574           0 :   pari_sp av2, av = avma;
    5575             :   GEN q, H;
    5576           0 :   ulong m = LONG_MAX>>1;
    5577           0 :   ulong p= 1 + m - (m % n);
    5578           0 :   long lM = lg(M);
    5579           0 :   if (lM == 1) { *pden = gen_1; return cgetg(1,t_MAT); }
    5580             : 
    5581           0 :   av2 = avma;
    5582           0 :   H = NULL;
    5583             :   for(;;)
    5584           0 :   {
    5585             :     GEN Hp, Pp, Mp, Hr;
    5586           0 :     do p += n; while(!uisprime(p));
    5587           0 :     Pp = ZX_to_Flx(P, p);
    5588           0 :     Mp = FqM_to_FlxM(M, P, utoi(p));
    5589           0 :     Hp = FlkM_inv(Mp, Pp, p);
    5590           0 :     if (!Hp) continue;
    5591           0 :     if (!H)
    5592             :     {
    5593           0 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p);
    5594           0 :       q = utoipos(p);
    5595             :     }
    5596             :     else
    5597           0 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    5598           0 :     Hr = FpXM_ratlift(H, q);
    5599           0 :     if (DEBUGLEVEL>5) err_printf("ZabM_inv mod %ld (ratlift=%ld)\n", p,!!Hr);
    5600           0 :     if (Hr) {/* DONE ? */
    5601           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    5602           0 :       GEN MH = ZXQM_mul(Hl, M, P);
    5603           0 :       if (*pden)
    5604           0 :       { if (RgM_isscalar(MH, *pden)) { H = Hl; break; }}
    5605             :       else
    5606           0 :       { if (RgM_isidentity(MH)) { H = Hl; *pden = gen_1; break; } }
    5607             :     }
    5608             : 
    5609           0 :     if (gc_needed(av,2))
    5610             :     {
    5611           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_inv");
    5612           0 :       gerepileall(av2, 2, &H, &q);
    5613             :     }
    5614             :   }
    5615           0 :   gerepileall(av, 2, &H, pden);
    5616           0 :   return H;
    5617             : }
    5618             : 
    5619             : static GEN
    5620        1090 : FlkM_ker(GEN M, GEN P, ulong p)
    5621             : {
    5622        1090 :   ulong pi = get_Fl_red(p);
    5623        1090 :   GEN R = Flx_roots(P, p);
    5624        1090 :   long l = lg(R), i, dP = degpol(P), r;
    5625             :   GEN M1, K, D;
    5626        1090 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    5627        1090 :   GEN V = cgetg(l, t_VEC);
    5628        1090 :   M1 = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,1), dP, p, pi), p, pi);
    5629        1090 :   K = Flm_ker_sp(M1, p, 2);
    5630        1090 :   r = lg(gel(K,1)); D = gel(K,2);
    5631        1090 :   gel(V, 1) = gel(K,1);
    5632        2266 :   for(i=2; i<l; i++)
    5633             :   {
    5634        1176 :     GEN Mi = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,i), dP, p, pi), p, pi);
    5635        1176 :     GEN K = Flm_ker_sp(Mi, p, 2);
    5636        1176 :     if (lg(gel(K,1)) != r || !zv_equal(D, gel(K,2))) return NULL;
    5637        1176 :     gel(V, i) = gel(K,1);
    5638             :   }
    5639        1090 :   return mkvec2(FlmV_recover_pre(V, W, p, pi, P[1]), D);
    5640             : }
    5641             : 
    5642             : GEN
    5643         686 : ZabM_ker(GEN M, GEN P, long n)
    5644             : {
    5645         686 :   pari_sp av2, av = avma;
    5646             :   GEN q, H, D;
    5647         686 :   ulong m = LONG_MAX>>1;
    5648         686 :   ulong p= 1 + m - (m % n);
    5649         686 :   av2 = avma;
    5650         686 :   H = NULL; D = NULL;
    5651             :   for(;;)
    5652         404 :   {
    5653             :     GEN Kp, Hp, Dp, Pp, Mp, Hr;
    5654       29149 :     do p += n; while(!uisprime(p));
    5655        1090 :     Pp = ZX_to_Flx(P, p);
    5656        1090 :     Mp = FqM_to_FlxM(M, P, utoi(p));
    5657        1090 :     Kp = FlkM_ker(Mp, Pp, p);
    5658        1090 :     if (!Kp) continue;
    5659        1090 :     Hp = gel(Kp,1); Dp = gel(Kp,2);
    5660        1090 :     if (H && (lg(Hp)>lg(H) || (lg(Hp)==lg(H) && vecsmall_lexcmp(Dp,D)>0))) continue;
    5661        1090 :     if (!H || (lg(Hp)<lg(H) || vecsmall_lexcmp(Dp,D)<0))
    5662             :     {
    5663         686 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p); D = Dp;
    5664         686 :       q = utoipos(p);
    5665             :     }
    5666             :     else
    5667         404 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    5668        1090 :     Hr = FpXM_ratlift(H, q);
    5669        1090 :     if (DEBUGLEVEL>5) err_printf("ZabM_ker mod %ld (ratlift=%ld)\n", p,!!Hr);
    5670        1090 :     if (Hr) {/* DONE ? */
    5671         696 :       GEN Hl = vec_Q_primpart(Hr);
    5672         696 :       GEN MH = ZXQM_mul(M, Hl,P);
    5673         696 :       if (gequal0(MH)) { H = Hl;  break; }
    5674             :     }
    5675             : 
    5676         404 :     if (gc_needed(av,2))
    5677             :     {
    5678           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_ker");
    5679           0 :       gerepileall(av2, 3, &H, &D, &q);
    5680             :     }
    5681             :   }
    5682         686 :   return gerepilecopy(av, H);
    5683             : }
    5684             : 
    5685             : GEN
    5686        2030 : ZabM_indexrank(GEN M, GEN P, long n)
    5687             : {
    5688        2030 :   pari_sp av = avma;
    5689        2030 :   ulong m = LONG_MAX>>1;
    5690        2030 :   ulong p = 1+m-(m%n), D = degpol(P);
    5691        2030 :   long lM = lg(M), lmax = 0, c = 0;
    5692             :   GEN v;
    5693             :   for(;;)
    5694         672 :   {
    5695             :     GEN R, Mp, K;
    5696             :     ulong pi;
    5697             :     long l;
    5698       80649 :     do p += n; while (!uisprime(p));
    5699        2702 :     pi = get_Fl_red(p);
    5700        2702 :     R = Flx_roots(ZX_to_Flx(P, p), p);
    5701        2702 :     Mp = FqM_to_FlxM(M, P, utoipos(p));
    5702        2702 :     K = FlxM_eval_powers_pre(Mp, Fl_powers_pre(uel(R,1), D,p,pi), p,pi);
    5703        2702 :     v = Flm_indexrank(K, p);
    5704        2702 :     l = lg(gel(v,2));
    5705        2702 :     if (l == lM) break;
    5706         896 :     if (lmax >= 0 && l > lmax) { lmax = l; c = 0; } else c++;
    5707         896 :     if (c > 2)
    5708             :     { /* probably not maximal rank, expensive check */
    5709         224 :       lM -= lg(ZabM_ker(M, P, n))-1; /* actual rank (+1) */
    5710         224 :       if (lmax == lM) break;
    5711           0 :       lmax = -1; /* disable check */
    5712             :     }
    5713             :   }
    5714        2030 :   return gerepileupto(av, v);
    5715             : }
    5716             : 
    5717             : #if 0
    5718             : GEN
    5719             : ZabM_gauss(GEN M, GEN P, long n, GEN *den)
    5720             : {
    5721             :   pari_sp av = avma;
    5722             :   GEN v, S, W;
    5723             :   v = ZabM_indexrank(M, P, n);
    5724             :   S = shallowmatextract(M,gel(v,1),gel(v,2));
    5725             :   W = ZabM_inv(S, P, n, den);
    5726             :   gerepileall(av,2,&W,den);
    5727             :   return W;
    5728             : }
    5729             : #endif
    5730             : 
    5731             : GEN
    5732         203 : ZabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *den)
    5733             : {
    5734         203 :   GEN v = ZabM_indexrank(M, P, n);
    5735         203 :   if (pv) *pv = v;
    5736         203 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    5737         203 :   return ZabM_inv(M, P, n, den);
    5738             : }
    5739             : GEN
    5740        4284 : ZM_pseudoinv(GEN M, GEN *pv, GEN *den)
    5741             : {
    5742        4284 :   GEN v = ZM_indexrank(M);
    5743        4284 :   if (pv) *pv = v;
    5744        4284 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    5745        4284 :   return ZM_inv(M, den);
    5746             : }
    5747             : 
    5748             : /*******************************************************************/
    5749             : /*                                                                 */
    5750             : /*                   Structured Elimination                        */
    5751             : /*                                                                 */
    5752             : /*******************************************************************/
    5753             : 
    5754             : static void
    5755      100896 : rem_col(GEN c, long i, GEN iscol, GEN Wrow, long *rcol, long *rrow)
    5756             : {
    5757      100896 :   long lc = lg(c), k;
    5758      100896 :   iscol[i] = 0; (*rcol)--;
    5759      892492 :   for (k = 1; k < lc; ++k)
    5760             :   {
    5761      791596 :     Wrow[c[k]]--;
    5762      791596 :     if (Wrow[c[k]]==0) (*rrow)--;
    5763             :   }
    5764      100896 : }
    5765             : 
    5766             : static void
    5767        6050 : rem_singleton(GEN M, GEN iscol, GEN Wrow, long *rcol, long *rrow)
    5768             : {
    5769             :   long i, j;
    5770        6050 :   long nbcol = lg(iscol)-1, last;
    5771             :   do
    5772             :   {
    5773        8001 :     last = 0;
    5774    18978549 :     for (i = 1; i <= nbcol; ++i)
    5775    18970548 :       if (iscol[i])
    5776             :       {
    5777     9756240 :         GEN c = gmael(M, i, 1);
    5778     9756240 :         long lc = lg(c);
    5779    91088246 :         for (j = 1; j < lc; ++j)
    5780    81344782 :           if (Wrow[c[j]] == 1)
    5781             :           {
    5782       12776 :             rem_col(c, i, iscol, Wrow, rcol, rrow);
    5783       12776 :             last=1; break;
    5784             :           }
    5785             :       }
    5786        8001 :   } while (last);
    5787        6050 : }
    5788             : 
    5789             : static GEN
    5790        5924 : fill_wcol(GEN M, GEN iscol, GEN Wrow, long *w, GEN wcol)
    5791             : {
    5792        5924 :   long nbcol = lg(iscol)-1;
    5793             :   long i, j, m, last;
    5794             :   GEN per;
    5795       14712 :   for (m = 2, last=0; !last ; m++)
    5796             :   {
    5797    22264695 :     for (i = 1; i <= nbcol; ++i)
    5798             :     {
    5799    22255907 :       wcol[i] = 0;
    5800    22255907 :       if (iscol[i])
    5801             :       {
    5802    11425874 :         GEN c = gmael(M, i, 1);
    5803    11425874 :         long lc = lg(c);
    5804   106718046 :         for (j = 1; j < lc; ++j)
    5805    95292172 :           if (Wrow[c[j]] == m) {  wcol[i]++; last = 1; }
    5806             :       }
    5807             :     }
    5808             :   }
    5809        5924 :   per = vecsmall_indexsort(wcol);
    5810        5924 :   *w = wcol[per[nbcol]];
    5811        5924 :   return per;
    5812             : }
    5813             : 
    5814             : /* M is a RgMs with nbrow rows, A a list of row indices.
    5815             :    Eliminate rows of M with a single entry that do not belong to A,
    5816             :    and the corresponding columns. Also eliminate columns until #colums=#rows.
    5817             :    Return pcol and prow:
    5818             :    pcol is a map from the new columns indices to the old one.
    5819             :    prow is a map from the old rows indices to the new one (0 if removed).
    5820             : */
    5821             : 
    5822             : void
    5823         126 : RgMs_structelim_col(GEN M, long nbcol, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    5824             : {
    5825             :   long i,j,k;
    5826         126 :   long lA = lg(A);
    5827         126 :   GEN prow = cgetg(nbrow+1, t_VECSMALL);
    5828         126 :   GEN pcol = zero_zv(nbcol);
    5829         126 :   pari_sp av = avma;
    5830         126 :   long rcol = nbcol, rrow = 0, imin = nbcol - usqrt(nbcol);
    5831         126 :   GEN iscol = const_vecsmall(nbcol, 1);
    5832         126 :   GEN Wrow  = zero_zv(nbrow);
    5833         126 :   GEN wcol = cgetg(nbcol+1, t_VECSMALL);
    5834         126 :   pari_sp av2=avma;
    5835      126721 :   for (i = 1; i <= nbcol; ++i)
    5836             :   {
    5837      126595 :     GEN F = gmael(M, i, 1);
    5838      126595 :     long l = lg(F)-1;
    5839     1118175 :     for (j = 1; j <= l; ++j)
    5840      991580 :       Wrow[F[j]]++;
    5841             :   }
    5842         126 :   for (j = 1; j < lA; ++j)
    5843             :   {
    5844           0 :     if (Wrow[A[j]] == 0) { *p_col=NULL; return; }
    5845           0 :     Wrow[A[j]] = -1;
    5846             :   }
    5847      237272 :   for (i = 1; i <= nbrow; ++i)
    5848      237146 :     if (Wrow[i])
    5849       67634 :       rrow++;
    5850         126 :   rem_singleton(M, iscol, Wrow, &rcol, &rrow);
    5851         126 :   if (rcol<rrow) pari_err_BUG("RgMs_structelim, rcol<rrow");
    5852        6176 :   for (; rcol>rrow;)
    5853             :   {
    5854             :     long w;
    5855        5924 :     GEN per = fill_wcol(M, iscol, Wrow, &w, wcol);
    5856       94044 :     for (i = nbcol; i>=imin && wcol[per[i]]>=w && rcol>rrow; i--)
    5857       88120 :       rem_col(gmael(M, per[i], 1), per[i], iscol, Wrow, &rcol, &rrow);
    5858        5924 :     rem_singleton(M, iscol, Wrow, &rcol, &rrow);
    5859        5924 :     avma = av2;
    5860             :   }
    5861      126721 :   for (j = 1, i = 1; i <= nbcol; ++i)
    5862      126595 :     if (iscol[i])
    5863       25699 :       pcol[j++] = i;
    5864         126 :   setlg(pcol,j);
    5865      237272 :   for (k = 1, i = 1; i <= nbrow; ++i)
    5866      237146 :     prow[i] = Wrow[i] ? k++: 0;
    5867         126 :   avma = av;
    5868         126 :   *p_col = pcol; *p_row = prow;
    5869             : }
    5870             : 
    5871             : void
    5872           0 : RgMs_structelim(GEN M, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    5873             : {
    5874           0 :   RgMs_structelim_col(M, lg(M)-1, nbrow, A, p_col, p_row);
    5875           0 : }
    5876             : 
    5877             : /*******************************************************************/
    5878             : /*                                                                 */
    5879             : /*                        EIGENVECTORS                             */
    5880             : /*   (independent eigenvectors, sorted by increasing eigenvalue)   */
    5881             : /*                                                                 */
    5882             : /*******************************************************************/
    5883             : 
    5884             : GEN
    5885          63 : mateigen(GEN x, long flag, long prec)
    5886             : {
    5887             :   GEN y, R, T;
    5888          63 :   long k, l, ex, n = lg(x);
    5889          63 :   pari_sp av = avma;
    5890             : 
    5891          63 :   if (typ(x)!=t_MAT) pari_err_TYPE("eigen",x);
    5892          63 :   if (n != 1 && n != lgcols(x)) pari_err_DIM("eigen");
    5893          63 :   if (flag < 0 || flag > 1) pari_err_FLAG("mateigen");
    5894          63 :   if (n == 1)
    5895             :   {
    5896          14 :     if (flag) retmkvec2(cgetg(1,t_VEC), cgetg(1,t_MAT));
    5897           7 :     return cgetg(1,t_VEC);
    5898             :   }
    5899          49 :   if (n == 2)
    5900             :   {
    5901          14 :     if (flag) retmkvec2(mkveccopy(gcoeff(x,1,1)), matid(1));
    5902           7 :     return matid(1);
    5903             :   }
    5904             : 
    5905          35 :   ex = 16 - prec2nbits(prec);
    5906          35 :   T = charpoly(x,0);
    5907          35 :   if (RgX_is_QX(T))
    5908             :   {
    5909          28 :     T = ZX_radical( Q_primpart(T) );
    5910          28 :     R = nfrootsQ(T);
    5911          28 :     if (lg(R)-1 < degpol(T))
    5912             :     { /* add missing complex roots */
    5913          14 :       GEN r = cleanroots(RgX_div(T, roots_to_pol(R, 0)), prec);
    5914          14 :       settyp(r, t_VEC);
    5915          14 :       R = shallowconcat(R, r);
    5916             :     }
    5917             :   }
    5918             :   else
    5919             :   {
    5920           7 :     GEN r1, v = vectrunc_init(lg(T));
    5921             :     long e;
    5922           7 :     R = cleanroots(T,prec);
    5923           7 :     r1 = NULL;
    5924          21 :     for (k = 1; k < lg(R); k++)
    5925             :     {
    5926          14 :       GEN r2 = gel(R,k), r = grndtoi(r2, &e);
    5927          14 :       if (e < ex) r2 = r;
    5928          14 :       if (r1)
    5929             :       {
    5930           7 :         r = gsub(r1,r2);
    5931           7 :         if (gequal0(r) || gexpo(r) < ex) continue;
    5932             :       }
    5933          14 :       vectrunc_append(v, r2);
    5934          14 :       r1 = r2;
    5935             :     }
    5936           7 :     R = v;
    5937             :   }
    5938             :   /* R = distinct complex roots of charpoly(x) */
    5939          35 :   l = lg(R); y = cgetg(l, t_VEC);
    5940         189 :   for (k = 1; k < l; k++)
    5941             :   {
    5942         154 :     GEN F = ker_aux(RgM_Rg_sub_shallow(x, gel(R,k)), x);
    5943         154 :     long d = lg(F)-1;
    5944         154 :     if (!d) pari_err_PREC("mateigen");
    5945         154 :     gel(y,k) = F;
    5946         154 :     if (flag) gel(R,k) = const_vec(d, gel(R,k));
    5947             :   }
    5948          35 :   y = shallowconcat1(y);
    5949          35 :   if (lg(y) > n) pari_err_PREC("mateigen");
    5950             :   /* lg(y) < n if x is not diagonalizable */
    5951          35 :   if (flag) y = mkvec2(shallowconcat1(R), y);
    5952          35 :   return gerepilecopy(av,y);
    5953             : }
    5954             : GEN
    5955           0 : eigen(GEN x, long prec) { return mateigen(x, 0, prec); }
    5956             : 
    5957             : /*******************************************************************/
    5958             : /*                                                                 */
    5959             : /*                           DETERMINANT                           */
    5960             : /*                                                                 */
    5961             : /*******************************************************************/
    5962             : 
    5963             : GEN
    5964        4053 : det0(GEN a,long flag)
    5965             : {
    5966        4053 :   switch(flag)
    5967             :   {
    5968        4039 :     case 0: return det(a);
    5969          14 :     case 1: return det2(a);
    5970           0 :     default: pari_err_FLAG("matdet");
    5971             :   }
    5972             :   return NULL; /* LCOV_EXCL_LINE */
    5973             : }
    5974             : 
    5975             : /* M a 2x2 matrix, returns det(M) */
    5976             : static GEN
    5977        6472 : RgM_det2(GEN M)
    5978             : {
    5979        6472 :   pari_sp av = avma;
    5980        6472 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    5981        6472 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    5982        6472 :   return gerepileupto(av, gsub(gmul(a,d), gmul(b,c)));
    5983             : }
    5984             : /* M a 2x2 ZM, returns det(M) */
    5985             : static GEN
    5986        8925 : ZM_det2(GEN M)
    5987             : {
    5988        8925 :   pari_sp av = avma;
    5989        8925 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    5990        8925 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    5991        8925 :   return gerepileuptoint(av, subii(mulii(a,d), mulii(b, c)));
    5992             : }
    5993             : /* M a 3x3 ZM, return det(M) */
    5994             : static GEN
    5995        2940 : ZM_det3(GEN M)
    5996             : {
    5997        2940 :   pari_sp av = avma;
    5998        2940 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2), c = gcoeff(M,1,3);
    5999        2940 :   GEN d = gcoeff(M,2,1), e = gcoeff(M,2,2), f = gcoeff(M,2,3);
    6000        2940 :   GEN g = gcoeff(M,3,1), h = gcoeff(M,3,2), i = gcoeff(M,3,3);
    6001        2940 :   GEN t, D = signe(i)? mulii(subii(mulii(a,e), mulii(b,d)), i): gen_0;
    6002        2940 :   if (signe(g))
    6003             :   {
    6004        2737 :     t = mulii(subii(mulii(b,f), mulii(c,e)), g);
    6005        2737 :     D = addii(D, t);
    6006             :   }
    6007        2940 :   if (signe(h))
    6008             :   {
    6009        2758 :     t = mulii(subii(mulii(c,d), mulii(a,f)), h);
    6010        2758 :     D = addii(D, t);
    6011             :   }
    6012        2940 :   return gerepileuptoint(av, D);
    6013             : }
    6014             : 
    6015             : static GEN
    6016       10006 : det_simple_gauss(GEN a, GEN data, pivot_fun pivot)
    6017             : {
    6018       10006 :   pari_sp av = avma;
    6019       10006 :   long i,j,k, s = 1, nbco = lg(a)-1;
    6020       10006 :   GEN p, x = gen_1;
    6021             : 
    6022       10006 :   a = RgM_shallowcopy(a);
    6023       80090 :   for (i=1; i<nbco; i++)
    6024             :   {
    6025       70091 :     k = pivot(a, data, i, NULL);
    6026       70091 :     if (k > nbco) return gerepilecopy(av, gcoeff(a,i,i));
    6027       70084 :     if (k != i)
    6028             :     { /* exchange the lines s.t. k = i */
    6029        4362 :       for (j=i; j<=nbco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    6030        4362 :       s = -s;
    6031             :     }
    6032       70084 :     p = gcoeff(a,i,i);
    6033             : 
    6034       70084 :     x = gmul(x,p);
    6035      411325 :     for (k=i+1; k<=nbco; k++)
    6036             :     {
    6037      341241 :       GEN m = gcoeff(a,i,k);
    6038      341241 :       if (gequal0(m)) continue;
    6039             : 
    6040      119125 :       m = gdiv(m,p);
    6041      792420 :       for (j=i+1; j<=nbco; j++)
    6042      673295 :         gcoeff(a,j,k) = gsub(gcoeff(a,j,k), gmul(m,gcoeff(a,j,i)));
    6043             :     }
    6044       70084 :     if (gc_needed(av,2))
    6045             :     {
    6046           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    6047           0 :       gerepileall(av,2, &a,&x);
    6048             :     }
    6049             :   }
    6050        9999 :   if (s < 0) x = gneg_i(x);
    6051        9999 :   return gerepileupto(av, gmul(x, gcoeff(a,nbco,nbco)));
    6052             : }
    6053             : 
    6054             : GEN
    6055        4937 : det2(GEN a)
    6056             : {
    6057             :   GEN data;
    6058             :   pivot_fun pivot;
    6059        4937 :   long n = lg(a)-1;
    6060        4937 :   if (typ(a)!=t_MAT) pari_err_TYPE("det2",a);
    6061        4937 :   if (!n) return gen_1;
    6062        4937 :   if (n != nbrows(a)) pari_err_DIM("det2");
    6063        4937 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    6064        4937 :   if (n == 2) return RgM_det2(a);
    6065        1692 :   pivot = get_pivot_fun(a, a, &data);
    6066        1692 :   return det_simple_gauss(a, data, pivot);
    6067             : }
    6068             : 
    6069             : static GEN
    6070         924 : mydiv(GEN x, GEN y)
    6071             : {
    6072         924 :   long tx = typ(x), ty = typ(y);
    6073         924 :   if (tx == ty && tx == t_POL && varn(x) == varn(y)) return RgX_div(x,y);
    6074         924 :   return gdiv(x,y);
    6075             : }
    6076             : 
    6077             : /* Assumes a a square t_MAT of dimension n > 0. Returns det(a) using
    6078             :  * Gauss-Bareiss. */
    6079             : static GEN
    6080         329 : det_bareiss(GEN a)
    6081             : {
    6082         329 :   pari_sp av = avma;
    6083         329 :   long nbco = lg(a)-1,i,j,k,s = 1;
    6084             :   GEN p, pprec;
    6085             : 
    6086         329 :   a = RgM_shallowcopy(a);
    6087        1057 :   for (pprec=gen_1,i=1; i<nbco; i++,pprec=p)
    6088             :   {
    6089             :     GEN ci;
    6090         728 :     int diveuc = (gequal1(pprec)==0);
    6091             : 
    6092         728 :     p = gcoeff(a,i,i);
    6093         728 :     if (gequal0(p))
    6094             :     {
    6095           0 :       k=i+1; while (k<=nbco && gequal0(gcoeff(a,i,k))) k++;
    6096           0 :       if (k>nbco) return gerepilecopy(av, p);
    6097           0 :       swap(gel(a,k), gel(a,i)); s = -s;
    6098           0 :       p = gcoeff(a,i,i);
    6099             :     }
    6100         728 :     ci = gel(a,i);
    6101        2037 :     for (k=i+1; k<=nbco; k++)
    6102             :     {
    6103        1309 :       GEN ck = gel(a,k), m = gel(ck,i);
    6104        1309 :       if (gequal0(m))
    6105             :       {
    6106           0 :         if (gequal1(p))
    6107             :         {
    6108           0 :           if (diveuc)
    6109           0 :             gel(a,k) = mydiv(gel(a,k), pprec);
    6110             :         }
    6111             :         else
    6112           0 :           for (j=i+1; j<=nbco; j++)
    6113             :           {
    6114           0 :             GEN p1 = gmul(p, gel(ck,j));
    6115           0 :             if (diveuc) p1 = mydiv(p1,pprec);
    6116           0 :             gel(ck,j) = p1;
    6117             :           }
    6118             :       }
    6119             :       else
    6120        4144 :         for (j=i+1; j<=nbco; j++)
    6121             :         {
    6122        2835 :           pari_sp av2 = avma;
    6123        2835 :           GEN p1 = gsub(gmul(p,gel(ck,j)), gmul(m,gel(ci,j)));
    6124        2835 :           if (diveuc) p1 = mydiv(p1,pprec);
    6125        2835 :           gel(ck,j) = gerepileupto(av2, p1);
    6126             :         }
    6127        1309 :       if (gc_needed(av,2))
    6128             :       {
    6129           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    6130           0 :         gerepileall(av,2, &a,&pprec);
    6131           0 :         ci = gel(a,i);
    6132           0 :         p = gcoeff(a,i,i);
    6133             :       }
    6134             :     }
    6135             :   }
    6136         329 :   p = gcoeff(a,nbco,nbco);
    6137         329 :   p = (s < 0)? gneg(p): gcopy(p);
    6138         329 :   return gerepileupto(av, p);
    6139             : }
    6140             : 
    6141             : /* count non-zero entries in col j, at most 'max' of them.
    6142             :  * Return their indices */
    6143             : static GEN
    6144        1127 : col_count_non_zero(GEN a, long j, long max)
    6145             : {
    6146        1127 :   GEN v = cgetg(max+1, t_VECSMALL);
    6147        1127 :   GEN c = gel(a,j);
    6148        1127 :   long i, l = lg(a), k = 1;
    6149        4347 :   for (i = 1; i < l; i++)
    6150        4221 :     if (!gequal0(gel(c,i)))
    6151             :     {
    6152        4081 :       if (k > max) return NULL; /* fail */
    6153        3080 :       v[k++] = i;
    6154             :     }
    6155         126 :   setlg(v, k); return v;
    6156             : }
    6157             : /* count non-zero entries in row i, at most 'max' of them.
    6158             :  * Return their indices */
    6159             : static GEN
    6160        1113 : row_count_non_zero(GEN a, long i, long max)
    6161             : {
    6162        1113 :   GEN v = cgetg(max+1, t_VECSMALL);
    6163        1113 :   long j, l = lg(a), k = 1;
    6164        4291 :   for (j = 1; j < l; j++)
    6165        4179 :     if (!gequal0(gcoeff(a,i,j)))
    6166             :     {
    6167        4067 :       if (k > max) return NULL; /* fail */
    6168        3066 :       v[k++] = j;
    6169             :     }
    6170         112 :   setlg(v, k); return v;
    6171             : }
    6172             : 
    6173             : static GEN det_develop(GEN a, long max, double bound);
    6174             : /* (-1)^(i+j) a[i,j] * det RgM_minor(a,i,j) */
    6175             : static GEN
    6176         210 : coeff_det(GEN a, long i, long j, long max, double bound)
    6177             : {
    6178         210 :   GEN c = gcoeff(a, i, j);
    6179         210 :   c = gmul(c, det_develop(RgM_minor(a, i,j), max, bound));
    6180         210 :   if (odd(i+j)) c = gneg(c);
    6181         210 :   return c;
    6182             : }
    6183             : /* a square t_MAT, 'bound' a rough upper bound for the number of
    6184             :  * multiplications we are willing to pay while developing rows/columns before
    6185             :  * switching to Gaussian elimination */
    6186             : static GEN
    6187         441 : det_develop(GEN M, long max, double bound)
    6188             : {
    6189         441 :   pari_sp av = avma;
    6190         441 :   long i,j, n = lg(M)-1, lbest = max+2, best_col = 0, best_row = 0;
    6191         441 :   GEN best = NULL;
    6192             : 
    6193         441 :   if (bound < 1.) return det_bareiss(M); /* too costly now */
    6194             : 
    6195         329 :   switch(n)
    6196             :   {
    6197           0 :     case 0: return gen_1;
    6198           0 :     case 1: return gcopy(gcoeff(M,1,1));
    6199          14 :     case 2: return RgM_det2(M);
    6200             :   }
    6201         315 :   if (max > ((n+2)>>1)) max = (n+2)>>1;
    6202        1428 :   for (j = 1; j <= n; j++)
    6203             :   {
    6204        1127 :     pari_sp av2 = avma;
    6205        1127 :     GEN v = col_count_non_zero(M, j, max);
    6206             :     long lv;
    6207        1127 :     if (!v || (lv = lg(v)) >= lbest) { avma = av2; continue; }
    6208          98 :     if (lv == 1) { avma = av; return gen_0; }
    6209          98 :     if (lv == 2) {
    6210          14 :       avma = av;
    6211          14 :       return gerepileupto(av, coeff_det(M,v[1],j,max,bound));
    6212             :     }
    6213          84 :     best = v; lbest = lv; best_col = j;
    6214             :   }
    6215        1414 :   for (i = 1; i <= n; i++)
    6216             :   {
    6217        1113 :     pari_sp av2 = avma;
    6218        1113 :     GEN v = row_count_non_zero(M, i, max);
    6219             :     long lv;
    6220        1113 :     if (!v || (lv = lg(v)) >= lbest) { avma = av2; continue; }
    6221           0 :     if (lv == 1) { avma = av; return gen_0; }
    6222           0 :     if (lv == 2) {
    6223           0 :       avma = av;
    6224           0 :       return gerepileupto(av, coeff_det(M,i,v[1],max,bound));
    6225             :     }
    6226           0 :     best = v; lbest = lv; best_row = i;
    6227             :   }
    6228         301 :   if (best_row)
    6229             :   {
    6230           0 :     double d = lbest-1;
    6231           0 :     GEN s = NULL;
    6232             :     long k;
    6233           0 :     bound /= d*d*d;
    6234           0 :     for (k = 1; k < lbest; k++)
    6235             :     {
    6236           0 :       GEN c = coeff_det(M, best_row, best[k], max, bound);
    6237           0 :       s = s? gadd(s, c): c;
    6238             :     }
    6239           0 :     return gerepileupto(av, s);
    6240             :   }
    6241         301 :   if (best_col)
    6242             :   {
    6243          84 :     double d = lbest-1;
    6244          84 :     GEN s = NULL;
    6245             :     long k;
    6246          84 :     bound /= d*d*d;
    6247         280 :     for (k = 1; k < lbest; k++)
    6248             :     {
    6249         196 :       GEN c = coeff_det(M, best[k], best_col, max, bound);
    6250         196 :       s = s? gadd(s, c): c;
    6251             :     }
    6252          84 :     return gerepileupto(av, s);
    6253             :   }
    6254         217 :   return det_bareiss(M);
    6255             : }
    6256             : 
    6257             : /* area of parallelogram bounded by (v1,v2) */
    6258             : static GEN
    6259       53991 : parallelogramarea(GEN v1, GEN v2)
    6260       53991 : { return gsub(gmul(gnorml2(v1), gnorml2(v2)), gsqr(RgV_dotproduct(v1, v2))); }
    6261             : 
    6262             : /* Square of Hadamard bound for det(a), a square matrix.
    6263             :  * Slightly improvement: instead of using the column norms, use the area of
    6264             :  * the parallelogram formed by pairs of consecutive vectors */
    6265             : GEN
    6266       17038 : RgM_Hadamard(GEN a)
    6267             : {
    6268       17038 :   pari_sp av = avma;
    6269       17038 :   long n = lg(a)-1, i;
    6270             :   GEN B;
    6271       17038 :   if (n == 0) return gen_1;
    6272       17038 :   if (n == 1) return gsqr(gcoeff(a,1,1));
    6273       17038 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    6274       17038 :   B = gen_1;
    6275       71029 :   for (i = 1; i <= n/2; i++)
    6276       53991 :     B = gmul(B, parallelogramarea(gel(a,2*i-1), gel(a,2*i)));
    6277       17038 :   if (odd(n)) B = gmul(B, gnorml2(gel(a, n)));
    6278       17038 :   return gerepileuptoint(av, ceil_safe(B));
    6279             : }
    6280             : 
    6281             : /* If B=NULL, assume B=A' */
    6282             : static GEN
    6283       53570 : ZM_det_slice(GEN A, GEN P, GEN *mod)
    6284             : {
    6285       53570 :   pari_sp av = avma;
    6286       53570 :   long i, n = lg(P)-1;
    6287             :   GEN H, T;
    6288       53570 :   if (n == 1)
    6289             :   {
    6290       42310 :     ulong Hp, p = uel(P,1);
    6291       42310 :     GEN a = ZM_to_Flm(A, p);
    6292       42310 :     Hp = Flm_det_sp(a, p);
    6293       42306 :     avma = av;
    6294       42306 :     *mod = utoi(p); return utoi(Hp);
    6295             :   }
    6296       11260 :   T = ZV_producttree(P);
    6297       11259 :   A = ZM_nv_mod_tree(A, P, T);
    6298       11259 :   H = cgetg(n+1, t_VECSMALL);
    6299       39854 :   for(i=1; i <= n; i++)
    6300             :   {
    6301       28595 :     ulong p = P[i];
    6302       28595 :     GEN a = gel(A,i);
    6303       28595 :     H[i] = Flm_det_sp(a, p);
    6304             :   }
    6305       11259 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    6306       11257 :   *mod = gmael(T, lg(T)-1, 1);
    6307       11257 :   gerepileall(av, 2, &H, mod);
    6308       11260 :   return H;
    6309             : }
    6310             : 
    6311             : GEN
    6312       53570 : ZM_det_worker(GEN P, GEN A)
    6313             : {
    6314       53570 :   GEN V = cgetg(3, t_VEC);
    6315       53569 :   gel(V,1) = ZM_det_slice(A, P, &gel(V,2));
    6316       53561 :   return V;
    6317             : }
    6318             : 
    6319             : /* assume dim(a) = n > 0 */
    6320             : static GEN
    6321       29743 : ZM_det_i(GEN M, long n)
    6322             : {
    6323       29743 :   const long DIXON_THRESHOLD = 40;
    6324       29743 :   pari_sp av = avma, av2;
    6325             :   long i;
    6326       29743 :   ulong p, Dp = 1;
    6327             :   forprime_t S;
    6328             :   pari_timer ti;
    6329             :   GEN H, D, mod, h, q, v, worker;
    6330       29743 :   if (n == 1) return icopy(gcoeff(M,1,1));
    6331       28903 :   if (n == 2) return ZM_det2(M);
    6332       19978 :   if (n == 3) return ZM_det3(M);
    6333       17038 :   if (DEBUGLEVEL >=4) timer_start(&ti);
    6334       17038 :   h = RgM_Hadamard(M);
    6335       17038 :   if (!signe(h)) { avma = av; return gen_0; }
    6336       17038 :   h = sqrti(h); q = gen_1;
    6337       17038 :   init_modular_big(&S);
    6338       17038 :   p = 0; /* -Wall */
    6339       34076 :   while( cmpii(q, h) <= 0 && (p = u_forprime_next(&S)) )
    6340             :   {
    6341       17038 :     av2 = avma; Dp = Flm_det_sp(ZM_to_Flm(M, p), p);
    6342       17038 :     avma = av2;
    6343       17038 :     if (Dp) break;
    6344           0 :     q = muliu(q, p);
    6345             :   }
    6346       17038 :   if (!p) pari_err_OVERFLOW("ZM_det [ran out of primes]");
    6347       17038 :   if (!Dp) { avma = av; return gen_0; }
    6348       17038 :   if (n <= DIXON_THRESHOLD)
    6349       17038 :     D = q;
    6350             :   else
    6351             :   {
    6352           0 :     av2 = avma;
    6353           0 :     v = cgetg(n+1, t_COL);
    6354           0 :     gel(v, 1) = gen_1; /* ensure content(v) = 1 */
    6355           0 :     for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    6356           0 :     D = Q_denom(ZM_gauss(M, v));
    6357           0 :     if (expi(D) < expi(h) >> 1)
    6358             :     { /* First try unlucky, try once more */
    6359           0 :       for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    6360           0 :       D = lcmii(D, Q_denom(ZM_gauss(M, v)));
    6361             :     }
    6362           0 :     D = gerepileuptoint(av2, D);
    6363           0 :     if (q != gen_1) D = lcmii(D, q);
    6364             :   }
    6365             :   /* determinant is a multiple of D */
    6366       17038 :   if (DEBUGLEVEL >=4)
    6367           0 :     timer_printf(&ti,"ZM_det: Dixon %ld/%ld bits",expi(D),expi(h));
    6368       17038 :   h = divii(h, D);
    6369       17038 :   worker = strtoclosure("_ZM_det_worker", 1, M);
    6370       17038 :   H = gen_crt("ZM_det", worker, D, expi(h)+1, lg(M)-1, &mod, ZV_chinese, NULL);
    6371       17038 :   if (D) H = Fp_div(H, D, mod);
    6372       17038 :   H = Fp_center(H, mod, shifti(mod,-1));
    6373       17038 :   if (D) H = mulii(H, D);
    6374       17038 :   return gerepileuptoint(av, H);
    6375             : }
    6376             : 
    6377             : static GEN
    6378        1519 : RgM_det_FpM(GEN a, GEN p)
    6379             : {
    6380        1519 :   pari_sp av = avma;
    6381             :   ulong pp, d;
    6382        1519 :   a = RgM_Fp_init(a,p,&pp);
    6383        1519 :   switch(pp)
    6384             :   {
    6385          70 :   case 0: return gerepileupto(av, Fp_to_mod(FpM_det(a,p),p)); break;
    6386          14 :   case 2: d = F2m_det(a); break;
    6387        1435 :   default:d = Flm_det_sp(a, pp); break;
    6388             :   }
    6389        1449 :   avma = av; return mkintmodu(d, pp);
    6390             : }
    6391             : 
    6392             : static GEN
    6393          42 : RgM_det_FqM(GEN x, GEN pol, GEN p)
    6394             : {
    6395          42 :   pari_sp av = avma;
    6396          42 :   GEN T = RgX_to_FpX(pol, p);
    6397          42 :   GEN b = FqM_det(RgM_to_FqM(x, T, p), T, p);
    6398          42 :   if (!b) { avma = av; return NULL; }
    6399          42 :   return gerepilecopy(av, mkpolmod(FpX_to_mod(b, p), FpX_to_mod(T, p)));
    6400             : }
    6401             : 
    6402             : #define code(t1,t2) ((t1 << 6) | t2)
    6403             : static GEN
    6404       10680 : RgM_det_fast(GEN x)
    6405             : {
    6406             :   GEN p, pol;
    6407             :   long pa;
    6408       10680 :   long t = RgM_type(x, &p,&pol,&pa);
    6409       10680 :   switch(t)
    6410             :   {
    6411         315 :     case t_INT:    return ZM_det(x);
    6412         196 :     case t_FRAC:   return QM_det(x);
    6413          63 :     case t_FFELT:  return FFM_det(x, pol);
    6414        1519 :     case t_INTMOD: return RgM_det_FpM(x, p);
    6415             :     case code(t_POLMOD, t_INTMOD):
    6416          42 :                    return RgM_det_FqM(x, pol, p);
    6417        8545 :     default:       return NULL;
    6418             :   }
    6419             : }
    6420             : #undef code
    6421             : 
    6422             : static long
    6423         231 : det_init_max(long n)
    6424             : {
    6425         231 :   if (n > 100) return 0;
    6426         231 :   if (n > 50) return 1;
    6427         231 :   if (n > 30) return 4;
    6428         231 :   return 7;
    6429             : }
    6430             : 
    6431             : GEN
    6432       14241 : det(GEN a)
    6433             : {
    6434       14241 :   long n = lg(a)-1;
    6435             :   double B;
    6436             :   GEN data, b;
    6437             :   pivot_fun pivot;
    6438             : 
    6439       14241 :   if (typ(a)!=t_MAT) pari_err_TYPE("det",a);
    6440       14241 :   if (!n) return gen_1;
    6441       14199 :   if (n != nbrows(a)) pari_err_DIM("det");
    6442       14192 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    6443       13893 :   if (n == 2) return RgM_det2(a);
    6444       10680 :   b = RgM_det_fast(a);
    6445       10680 :   if (b) return b;
    6446        8545 :   pivot = get_pivot_fun(a, a, &data);
    6447        8545 :   if (pivot != gauss_get_pivot_NZ) return det_simple_gauss(a, data, pivot);
    6448         231 :   B = (double)n;
    6449         231 :   return det_develop(a, det_init_max(n), B*B*B);
    6450             : }
    6451             : 
    6452             : GEN
    6453       29750 : ZM_det(GEN a)
    6454             : {
    6455       29750 :   long n = lg(a)-1;
    6456       29750 :   if (!n) return gen_1;
    6457       29743 :   return ZM_det_i(a, n);
    6458             : }
    6459             : 
    6460             : GEN
    6461         196 : QM_det(GEN M)
    6462             : {
    6463         196 :   pari_sp av = avma;
    6464         196 :   GEN cM, pM = Q_primitive_part(M, &cM);
    6465         196 :   GEN b = ZM_det(pM);
    6466         196 :   if (cM) b = gmul(b, gpowgs(cM, lg(M)-1));
    6467         196 :   return gerepileupto(av, b);
    6468             : }

Generated by: LCOV version 1.13