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.12.0 lcov report (development 22872-edcf83abb) Lines: 3445 3691 93.3 %
Date: 2018-07-20 05:36:03 Functions: 340 359 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         949 :   for (u=t+1; u<=m; u++)
      39             :   {
      40         883 :     A = (pari_sp)coeff(x,u,k);
      41         883 :     if (A < av && A >= bot) coeff(x,u,k) += dec;
      42             :   }
      43        1920 :   for (i=k+1; i<=n; i++)
      44       75282 :     for (u=1; u<=m; u++)
      45             :     {
      46       73428 :       A = (pari_sp)coeff(x,u,i);
      47       73428 :       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        1920 :   for (i=k+1; i<=n; i++)
      60        1854 :     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         887 :           if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
     279         887 :           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      683184 :   for (j=k=1; j<=r; k++)
     524      608412 :     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      237298 : Flm_rsolve_upper_1(GEN U, GEN B, ulong p)
     563      237298 : { return Flm_Fl_mul(B, Fl_inv(ucoeff(U, 1, 1), p), p); }
     564             : 
     565             : static GEN
     566      722587 : Flm_rsolve_upper_2(GEN U, GEN B, ulong p)
     567             : {
     568      722587 :   ulong a = ucoeff(U, 1, 1), b = ucoeff(U, 1, 2), d = ucoeff(U, 2, 2);
     569      722587 :   ulong D = Fl_mul(a, d, p), Dinv = Fl_inv(D, p);
     570      722616 :   ulong ainv = Fl_mul(d, Dinv, p), dinv = Fl_mul(a, Dinv, p);
     571      722611 :   GEN B1 = rowslice(B, 1, 1);
     572      722585 :   GEN B2 = rowslice(B, 2, 2);
     573      722579 :   GEN X2 = Flm_Fl_mul(B2, dinv, p);
     574      722560 :   GEN X1 = Flm_Fl_mul(Flm_sub(B1, Flm_Fl_mul(X2, b, p), p),
     575             :                       ainv, p);
     576      722572 :   return vconcat(X1, X2);
     577             : }
     578             : 
     579             : /* solve U*X = B,  U upper triangular and invertible */
     580             : static GEN
     581     1735674 : Flm_rsolve_upper(GEN U, GEN B, ulong p)
     582             : {
     583     1735674 :   long n = lg(U) - 1, n1;
     584             :   GEN U2, U11, U12, U22, B1, B2, X1, X2, X;
     585     1735674 :   pari_sp av = avma;
     586             : 
     587     1735674 :   if (n == 0) return B;
     588     1735667 :   if (n == 1) return Flm_rsolve_upper_1(U, B, p);
     589     1498369 :   if (n == 2) return Flm_rsolve_upper_2(U, B, p);
     590      775783 :   n1 = (n + 1)/2;
     591      775783 :   U2 = vecslice(U, n1 + 1, n);
     592      775782 :   U22 = rowslice(U2, n1 + 1, n);
     593      775775 :   B2 = rowslice(B, n1 + 1, n);
     594      775759 :   X2 = Flm_rsolve_upper(U22, B2, p);
     595      775797 :   U12 = rowslice(U2, 1, n1);
     596      775787 :   B1 = rowslice(B, 1, n1);
     597      775771 :   B1 = Flm_sub(B1, Flm_mul(U12, X2, p), p);
     598      775762 :   if (gc_needed(av, 1)) gerepileall(av, 2, &B1, &X2);
     599      775762 :   U11 = matslice(U, 1,n1, 1,n1);
     600      775765 :   X1 = Flm_rsolve_upper(U11, B1, p);
     601      775818 :   X = vconcat(X1, X2);
     602      775824 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     603      775824 :   return X;
     604             : }
     605             : 
     606             : static GEN
     607      273333 : Flm_lsolve_upper_1(GEN U, GEN B, ulong p)
     608      273333 : { return Flm_Fl_mul(B, Fl_inv(ucoeff(U, 1, 1), p), p); }
     609             : 
     610             : static GEN
     611      803887 : Flm_lsolve_upper_2(GEN U, GEN B, ulong p)
     612             : {
     613      803887 :   ulong a = ucoeff(U, 1, 1), b = ucoeff(U, 1, 2), d = ucoeff(U, 2, 2);
     614      803887 :   ulong D = Fl_mul(a, d, p), Dinv = Fl_inv(D, p);
     615      803910 :   ulong ainv = Fl_mul(d, Dinv, p), dinv = Fl_mul(a, Dinv, p);
     616      803894 :   GEN B1 = vecslice(B, 1, 1);
     617      803862 :   GEN B2 = vecslice(B, 2, 2);
     618      803833 :   GEN X1 = Flm_Fl_mul(B1, ainv, p);
     619      803873 :   GEN X2 = Flm_Fl_mul(Flm_sub(B2, Flm_Fl_mul(X1, b, p), p),
     620             :                       dinv, p);
     621      803865 :   return shallowconcat(X1, X2);
     622             : }
     623             : 
     624             : /* solve X*U = B,  U upper triangular and invertible */
     625             : static GEN
     626     1825199 : Flm_lsolve_upper(GEN U, GEN B, ulong p)
     627             : {
     628     1825199 :   long n = lg(U) - 1, n1;
     629             :   GEN U2, U11, U12, U22, B1, B2, X1, X2, X;
     630     1825199 :   pari_sp av = avma;
     631             : 
     632     1825199 :   if (n == 0) return B;
     633     1825199 :   if (n == 1) return Flm_lsolve_upper_1(U, B, p);
     634     1551864 :   if (n == 2) return Flm_lsolve_upper_2(U, B, p);
     635      747979 :   n1 = (n + 1)/2;
     636      747979 :   U2 = vecslice(U, n1 + 1, n);
     637      747976 :   U11 = matslice(U, 1,n1, 1,n1);
     638      747948 :   U12 = rowslice(U2, 1, n1);
     639      747967 :   U22 = rowslice(U2, n1 + 1, n);
     640      747962 :   B1 = vecslice(B, 1, n1);
     641      747947 :   B2 = vecslice(B, n1 + 1, n);
     642      747947 :   X1 = Flm_lsolve_upper(U11, B1, p);
     643      747973 :   B2 = Flm_sub(B2, Flm_mul(X1, U12, p), p);
     644      747974 :   if (gc_needed(av, 1)) gerepileall(av, 3, &B2, &U22, &X1);
     645      747974 :   X2 = Flm_lsolve_upper(U22, B2, p);
     646      747991 :   X = shallowconcat(X1, X2);
     647      747997 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     648      747997 :   return X;
     649             : }
     650             : 
     651             : static GEN
     652     1380166 : Flm_rsolve_lower_unit_2(GEN L, GEN A, ulong p)
     653             : {
     654     1380166 :   GEN X1 = rowslice(A, 1, 1);
     655     1380148 :   GEN X2 = Flm_sub(rowslice(A, 2, 2),
     656     1380148 :                    Flm_Fl_mul(X1, ucoeff(L, 2, 1), p), p);
     657     1380147 :   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     3169342 : Flm_rsolve_lower_unit(GEN L, GEN A, ulong p)
     664             : {
     665     3169342 :   long m = lg(L) - 1, m1, n;
     666             :   GEN L1, L11, L21, L22, A1, A2, X1, X2, X;
     667     3169342 :   pari_sp av = avma;
     668             : 
     669     3169342 :   if (m == 0) return zero_Flm(0, lg(A) - 1);
     670     3169342 :   if (m == 1) return rowslice(A, 1, 1);
     671     2744671 :   if (m == 2) return Flm_rsolve_lower_unit_2(L, A, p);
     672     1364509 :   m1 = (m + 1)/2;
     673     1364509 :   n = nbrows(L);
     674     1364510 :   L1 = vecslice(L, 1, m1);
     675     1364499 :   L11 = rowslice(L1, 1, m1);
     676     1364491 :   L21 = rowslice(L1, m1 + 1, n);
     677     1364494 :   A1 = rowslice(A, 1, m1);
     678     1364487 :   X1 = Flm_rsolve_lower_unit(L11, A1, p);
     679     1364532 :   A2 = rowslice(A, m1 + 1, n);
     680     1364483 :   A2 = Flm_sub(A2, Flm_mul(L21, X1, p), p);
     681     1364480 :   if (gc_needed(av, 1)) gerepileall(av, 2, &A2, &X1);
     682     1364480 :   L22 = matslice(L, m1+1,n, m1+1,m);
     683     1364475 :   X2 = Flm_rsolve_lower_unit(L22, A2, p);
     684     1364509 :   X = vconcat(X1, X2);
     685     1364533 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     686     1364533 :   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     1871613 : Flm_lsolve_lower_unit(GEN L, GEN A, ulong p)
     701             : {
     702     1871613 :   long m = lg(L) - 1, m1;
     703             :   GEN L1, L2, L11, L21, L22, A1, A2, X1, X2, X;
     704     1871613 :   pari_sp av = avma;
     705             : 
     706     1871613 :   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     1060180 : Flm_CUP_gauss(GEN A, GEN *R, GEN *C, GEN *U, GEN *P, ulong p)
     729             : {
     730     1060180 :   long i, j, k, m = nbrows(A), n = lg(A) - 1, pr, pc, u, v;
     731             : 
     732     1060174 :   if (P) *P = identity_perm(n);
     733     1060164 :   *R = cgetg(m + 1, t_VECSMALL);
     734     5572370 :   for (j = 1, pr = 0; j <= n; j++)
     735             :   {
     736     7201830 :     for (pr++, pc = 0; pr <= m; pr++)
     737             :     {
     738     6706236 :       for (k = j; k <= n; k++) { v = ucoeff(A, pr, k); if (!pc && v) pc = k; }
     739     6706236 :       if (pc) break;
     740             :     }
     741     5007790 :     if (!pc) break;
     742     4512129 :     (*R)[j] = pr;
     743     4512129 :     if (pc != j)
     744             :     {
     745     1040025 :       swap(gel(A, j), gel(A, pc));
     746     1040025 :       if (P) lswap((*P)[j], (*P)[pc]);
     747             :     }
     748     4512129 :     u = Fl_inv(ucoeff(A, pr, j), p);
     749    30025823 :     for (i = pr + 1; i <= m; i++)
     750             :     {
     751    25513614 :       v = Fl_mul(ucoeff(A, i, j), u, p);
     752    25513460 :       ucoeff(A, i, j) = v;
     753   129147293 :       for (k = j + 1; k <= n; k++)
     754   207268396 :         ucoeff(A, i, k) = Fl_sub(ucoeff(A, i, k),
     755   103634198 :                                  Fl_mul(ucoeff(A, pr, k), v, p), p);
     756             :     }
     757             :   }
     758     1060241 :   setlg(*R, j);
     759     1060172 :   *C = vecslice(A, 1, j - 1);
     760     1060161 :   if (U) *U = rowpermute(A, *R);
     761     1060176 :   return j - 1;
     762             : }
     763             : 
     764             : static const long Flm_CUP_LIMIT = 8;
     765             : 
     766             : static ulong
     767      959279 : Flm_CUP(GEN A, GEN *R, GEN *C, GEN *U, GEN *P, ulong p)
     768             : {
     769      959279 :   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      959274 :   pari_sp av = avma;
     773             : 
     774      959274 :   if (m < Flm_CUP_LIMIT || n < Flm_CUP_LIMIT)
     775             :     /* destroy A; not called at the outermost recursion level */
     776      623356 :     return Flm_CUP_gauss(A, R, C, U, P, p);
     777      335918 :   m1 = (minss(m, n) + 1)/2;
     778      335919 :   A1 = rowslice(A, 1, m1);
     779      335920 :   A2 = rowslice(A, m1 + 1, m);
     780      335918 :   r1 = Flm_CUP(A1, &R1, &C1, &U1, &P1, p);
     781      335924 :   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      329254 :     U11 = vecslice(U1, 1, r1);
     794      329251 :     U12 = vecslice(U1, r1 + 1, n);
     795      329251 :     T21 = vecslicepermute(A2, P1, 1, r1);
     796      329250 :     T22 = vecslicepermute(A2, P1, r1 + 1, n);
     797      329251 :     C21 = Flm_lsolve_upper(U11, T21, p);
     798      329250 :     if (gc_needed(av, 1))
     799           0 :       gerepileall(av, 7, &R1, &C1, &P1, &U11, &U12, &T22, &C21);
     800      329250 :     B2 = Flm_sub(T22, Flm_mul(C21, U12, p), p);
     801      329250 :     r2 = Flm_CUP(B2, &R2, &C2, &U2, &P2, p);
     802      329248 :     r = r1 + r2;
     803      329248 :     *R = cgetg(r + 1, t_VECSMALL);
     804      329252 :     for (i = 1; i <= r1; i++) (*R)[i] = R1[i];
     805      329252 :     for (;      i <= r; i++)  (*R)[i] = R2[i - r1] + m1;
     806      329252 :     *C = shallowconcat(vconcat(C1, C21),
     807             :                        vconcat(zero_Flm(m1, r2), C2));
     808      329253 :     *U = shallowconcat(vconcat(U11, zero_Flm(r2, r1)),
     809             :                        vconcat(vecpermute(U12, P2), U2));
     810      329259 :     *P = cgetg(n + 1, t_VECSMALL);
     811      329255 :     for (i = 1; i <= r1; i++) (*P)[i] = P1[i];
     812      329255 :     for (; i <= n; i++)       (*P)[i] = P1[P2[i - r1] + r1];
     813             :   }
     814      335925 :   if (gc_needed(av, 1)) gerepileall(av, 4, R, C, U, P);
     815      335927 :   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     2394049 : F2v_find_nonzero(GEN x0, GEN mask0, long m)
    1206             : {
    1207     2394049 :   ulong *x = (ulong *)x0+2, *mask = (ulong *)mask0+2, e;
    1208     2394049 :   long i, l = lg(x0)-2;
    1209     3994264 :   for (i = 0; i < l; i++)
    1210             :   {
    1211     3431107 :     e = *x++ & *mask++;
    1212     3431107 :     if (e) return i*BITS_IN_LONG+vals(e)+1;
    1213             :   }
    1214      563157 :   return m+1;
    1215             : }
    1216             : 
    1217             : /* in place, destroy x */
    1218             : GEN
    1219      303094 : F2m_ker_sp(GEN x, long deplin)
    1220             : {
    1221             :   GEN y, c, d;
    1222             :   long i, j, k, r, m, n;
    1223             : 
    1224      303094 :   n = lg(x)-1;
    1225      303094 :   m = mael(x,1,1); r=0;
    1226             : 
    1227      303094 :   d = cgetg(n+1, t_VECSMALL);
    1228      303094 :   c = const_F2v(m);
    1229     2384838 :   for (k=1; k<=n; k++)
    1230             :   {
    1231     2107113 :     GEN xk = gel(x,k);
    1232     2107113 :     j = F2v_find_nonzero(xk, c, m);
    1233     2107122 :     if (j>m)
    1234             :     {
    1235      455398 :       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      430029 :       r++; d[k] = 0;
    1244             :     }
    1245             :     else
    1246             :     {
    1247     1651724 :       F2v_clear(c,j); d[k] = j;
    1248     1651723 :       F2v_clear(xk, j);
    1249    73592768 :       for (i=k+1; i<=n; i++)
    1250             :       {
    1251    71941042 :         GEN xi = gel(x,i);
    1252    71941042 :         if (F2v_coeff(xi,j)) F2v_add_inplace(xi, xk);
    1253             :       }
    1254     1651726 :       F2v_set(xk, j);
    1255             :     }
    1256             :   }
    1257      277725 :   if (deplin) return NULL;
    1258             : 
    1259      276290 :   y = zero_F2m_copy(n,r);
    1260      706320 :   for (j=k=1; j<=r; j++,k++)
    1261             :   {
    1262      430030 :     GEN C = gel(y,j); while (d[k]) k++;
    1263    12611745 :     for (i=1; i<k; i++)
    1264    12181715 :       if (d[i] && F2m_coeff(x,d[i],k))
    1265     4461644 :         F2v_set(C,i);
    1266      430030 :     F2v_set(C, k);
    1267             :   }
    1268      276290 :   return y;
    1269             : }
    1270             : 
    1271             : static void /* assume m < p */
    1272     6336849 : _Fl_addmul(GEN b, long k, long i, ulong m, ulong p, ulong pi)
    1273             : {
    1274     6336849 :   uel(b,k) = Fl_addmul_pre(uel(b, k), m, uel(b, i), p, pi);
    1275     6336849 : }
    1276             : static void /* same m = 1 */
    1277      185475 : _Fl_add(GEN b, long k, long i, ulong p)
    1278             : {
    1279      185475 :   uel(b,k) = Fl_add(uel(b,k), uel(b,i), p);
    1280      185475 : }
    1281             : static void /* assume m < p && SMALL_ULONG(p) && (! (b[i] & b[k] & HIGHMASK)) */
    1282    14815143 : _Fl_addmul_OK(GEN b, long k, long i, ulong m, ulong p)
    1283             : {
    1284    14815143 :   uel(b,k) += m * uel(b,i);
    1285    14815143 :   if (uel(b,k) & HIGHMASK) uel(b,k) %= p;
    1286    14815143 : }
    1287             : static void /* assume SMALL_ULONG(p) && (! (b[i] & b[k] & HIGHMASK)) */
    1288     2773339 : _Fl_add_OK(GEN b, long k, long i, ulong p)
    1289             : {
    1290     2773339 :   uel(b,k) += uel(b,i);
    1291     2773339 :   if (uel(b,k) & HIGHMASK) uel(b,k) %= p;
    1292     2773339 : }
    1293             : 
    1294             : static GEN
    1295      113224 : 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      113224 :   n = lg(x)-1;
    1302      113224 :   m=nbrows(x); r=0;
    1303             : 
    1304      113224 :   c = zero_zv(m);
    1305      113224 :   d = cgetg(n+1, t_VECSMALL);
    1306      113224 :   a = 0; /* for gcc -Wall */
    1307      531300 :   for (k=1; k<=n; k++)
    1308             :   {
    1309     1789284 :     for (j=1; j<=m; j++)
    1310     1587570 :       if (!c[j])
    1311             :       {
    1312     1016811 :         a = ucoeff(x,j,k) % p;
    1313     1016811 :         if (a) break;
    1314             :       }
    1315      436752 :     if (j > m)
    1316             :     {
    1317      201714 :       if (deplin==1) {
    1318       18676 :         c = cgetg(n+1, t_VECSMALL);
    1319       18676 :         for (i=1; i<k; i++) c[i] = ucoeff(x,d[i],k) % p;
    1320       18676 :         c[k] = 1; for (i=k+1; i<=n; i++) c[i] = 0;
    1321       18676 :         return c;
    1322             :       }
    1323      183038 :       r++; d[k] = 0;
    1324             :     }
    1325             :     else
    1326             :     {
    1327      235038 :       ulong piv = p - Fl_inv(a, p); /* -1/a */
    1328      235038 :       c[j] = k; d[k] = j;
    1329      235038 :       ucoeff(x,j,k) = p-1;
    1330      235038 :       if (piv != 1)
    1331      157203 :         for (i=k+1; i<=n; i++) ucoeff(x,j,i) = (piv * ucoeff(x,j,i)) % p;
    1332     1492881 :       for (t=1; t<=m; t++)
    1333             :       {
    1334     1257843 :         if (t == j) continue;
    1335             : 
    1336     1022805 :         piv = ( ucoeff(x,t,k) %= p );
    1337     1022805 :         if (!piv) continue;
    1338      465461 :         if (piv == 1)
    1339      120859 :           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       94548 :   if (deplin==1) return NULL;
    1346             : 
    1347       94541 :   y = cgetg(r+1, t_MAT);
    1348      277579 :   for (j=k=1; j<=r; j++,k++)
    1349             :   {
    1350      183038 :     GEN C = cgetg(n+1, t_VECSMALL);
    1351             : 
    1352      183038 :     gel(y,j) = C; while (d[k]) k++;
    1353      631128 :     for (i=1; i<k; i++)
    1354      448090 :       if (d[i])
    1355      268948 :         uel(C,i) = ucoeff(x,d[i],k) % p;
    1356             :       else
    1357      179142 :         uel(C,i) = 0UL;
    1358      183038 :     uel(C,k) = 1UL; for (i=k+1; i<=n; i++) uel(C,i) = 0UL;
    1359             :   }
    1360       94541 :   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       94541 :   return y;
    1367             : }
    1368             : 
    1369             : /* in place, destroy x */
    1370             : static GEN
    1371      232847 : 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      232847 :   n = lg(x)-1;
    1377      232847 :   if (!n) return cgetg(1,t_MAT);
    1378      232742 :   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          93 : 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 = vecsmallpermute(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      376935 : Flm_ker_i(GEN x, ulong p, long deplin, long inplace) {
    1517      376935 :   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      232847 :   return Flm_ker_gauss(inplace? x: Flm_copy(x), p, deplin);
    1524             : }
    1525             : 
    1526             : GEN
    1527      309477 : Flm_ker_sp(GEN x, ulong p, long deplin) {
    1528      309477 :   return Flm_ker_i(x, p, deplin, 1);
    1529             : }
    1530             : 
    1531             : GEN
    1532       67458 : Flm_ker(GEN x, ulong p) {
    1533       67458 :   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      339112 :   for (i=1; i<nbco; i++)
    1611             :   {
    1612      285826 :     for(k=i; k<=nbco; k++)
    1613      285822 :       if (ucoeff(a,k,i)) break;
    1614      285744 :     if (k > nbco) return ucoeff(a,i,i);
    1615      285744 :     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      285744 :     q = ucoeff(a,i,i);
    1621             : 
    1622      285744 :     x = Fl_mul_pre(x, q, p, pi);
    1623      285749 :     q = Fl_inv(q,p);
    1624     1220817 :     for (k=i+1; k<=nbco; k++)
    1625             :     {
    1626      935070 :       ulong m = ucoeff(a,i,k);
    1627      935070 :       if (!m) continue;
    1628             : 
    1629      892254 :       m = Fl_mul_pre(m, q, p, pi);
    1630     4551331 :       for (j=i+1; j<=nbco; j++)
    1631     3659150 :         ucoeff(a,j,k) = Fl_sub(ucoeff(a,j,k), Fl_mul_pre(m,ucoeff(a,j,i), p, pi), p);
    1632             :     }
    1633             :   }
    1634       53368 :   if (s < 0) x = Fl_neg(x, p);
    1635       53368 :   return Fl_mul(x, ucoeff(a,nbco,nbco), p);
    1636             : }
    1637             : 
    1638             : static ulong
    1639       36282 : Flm_det_CUP(GEN a, ulong p) {
    1640             :   GEN R, C, U, P;
    1641       36282 :   long d, i, n = lg(a) - 1, r;
    1642       36282 :   r = Flm_CUP(a, &R, &C, &U, &P, p);
    1643       36293 :   if (r < n)
    1644          50 :     d = 0;
    1645             :   else {
    1646       36243 :     d = perm_sign(P) == 1? 1: p-1;
    1647      380367 :     for (i = 1; i <= n; i++)
    1648      344132 :       d = Fl_mul(d, ucoeff(U, i, i), p);
    1649             :   }
    1650       36285 :   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       36282 :     d = Flm_det_CUP(x, p);
    1659             :   else
    1660       55110 :     d = Flm_det_gauss(inplace? x: Flm_copy(x), p);
    1661       91394 :   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      477566 : FpM_init(GEN a, GEN p, ulong *pp)
    1676             : {
    1677      477566 :   if (lgefint(p) == 3)
    1678             :   {
    1679      470265 :     *pp = uel(p,2);
    1680      470265 :     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      167226 : 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      167226 :   n=lg(x)-1; if (!n) { *rr=0; return NULL; }
    1756             : 
    1757      167226 :   m=nbrows(x); r=0;
    1758      167226 :   d=cgetg(n+1,t_VECSMALL);
    1759      167226 :   c = zero_zv(m);
    1760      769356 :   for (k=1; k<=n; k++)
    1761             :   {
    1762     2066644 :     for (j=1; j<=m; j++)
    1763     1905062 :       if (!c[j])
    1764             :       {
    1765     1058907 :         ucoeff(x,j,k) %= p;
    1766     1058907 :         if (ucoeff(x,j,k)) break;
    1767             :       }
    1768      602130 :     if (j>m) { r++; d[k]=0; }
    1769             :     else
    1770             :     {
    1771      440548 :       ulong piv = p - Fl_inv(ucoeff(x,j,k), p);
    1772      440548 :       c[j]=k; d[k]=j;
    1773     1335576 :       for (i=k+1; i<=n; i++)
    1774      895028 :         ucoeff(x,j,i) = Fl_mul(piv, ucoeff(x,j,i), p);
    1775     3342609 :       for (t=1; t<=m; t++)
    1776     2902061 :         if (!c[t]) /* no pivot on that line yet */
    1777             :         {
    1778     1894312 :           piv = ucoeff(x,t,k);
    1779     1894312 :           if (piv)
    1780             :           {
    1781     1048085 :             ucoeff(x,t,k) = 0;
    1782     3379592 :             for (i=k+1; i<=n; i++)
    1783     4663014 :               ucoeff(x,t,i) = Fl_add(ucoeff(x,t,i),
    1784     2331507 :                                      Fl_mul(piv,ucoeff(x,j,i),p),p);
    1785             :           }
    1786             :         }
    1787      440548 :       for (i=k; i<=n; i++) ucoeff(x,j,i) = 0; /* dummy */
    1788             :     }
    1789             :   }
    1790      167226 :   *rr = r; avma = (pari_sp)d; return d;
    1791             : }
    1792             : 
    1793             : static GEN
    1794       67036 : Flm_pivots_CUP(GEN x, ulong p, long *rr) {
    1795             :   pari_sp av;
    1796       67036 :   long i, n = lg(x) - 1, r;
    1797       67036 :   GEN R, C, U, P, d = zero_zv(n);
    1798       67036 :   av = avma;
    1799       67036 :   r = Flm_CUP(x, &R, &C, &U, &P, p);
    1800      815107 :   for(i = 1; i <= r; i++)
    1801      748071 :     d[P[i]] = R[i];
    1802       67036 :   avma = av;
    1803       67036 :   *rr = n - r;
    1804       67036 :   return d;
    1805             : }
    1806             : 
    1807             : static GEN
    1808      234262 : Flm_pivots(GEN x, ulong p, long *rr, long inplace) {
    1809      234262 :   if (lg(x) - 1 >= Flm_CUP_LIMIT && nbrows(x) >= Flm_CUP_LIMIT)
    1810       67036 :     return Flm_pivots_CUP(x, p, rr);
    1811      167226 :   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      140145 : FpM_gauss_pivot(GEN x, GEN p, long *rr)
    1823             : {
    1824             :   ulong pp;
    1825      140145 :   if (lg(x)==1) { *rr = 0; return NULL; }
    1826      138234 :   x = FpM_init(x, p, &pp);
    1827      138234 :   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       98269 :   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        2178 : Flm_rank(GEN x, ulong p)
    1870             : {
    1871        2178 :   pari_sp av = avma;
    1872             :   long r;
    1873        2178 :   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        2143 :   (void) Flm_pivots(x, p, &r, 0);
    1879        2143 :   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      269642 : FpM_ker_i(GEN x, GEN p, long deplin)
    2208             : {
    2209      269642 :   pari_sp av = avma;
    2210             :   ulong pp;
    2211             :   GEN y;
    2212             : 
    2213      269642 :   if (lg(x)==1) return cgetg(1,t_MAT);
    2214      269642 :   x = FpM_init(x, p, &pp);
    2215      269642 :   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      189539 :     y = Flm_ker_sp(x, pp, deplin);
    2225      189539 :     if (!y) return y;
    2226      189539 :     y = deplin? Flc_to_ZC(y): Flm_to_ZM(y);
    2227      189539 :     return gerepileupto(av, y);
    2228             :   }
    2229             : }
    2230             : 
    2231             : GEN
    2232      196522 : FpM_ker(GEN x, GEN p) { return FpM_ker_i(x,p,0); }
    2233             : 
    2234             : GEN
    2235       67352 : 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      575229 : approx_0(GEN x, GEN y)
    2392             : {
    2393      575229 :   long tx = typ(x);
    2394      575229 :   if (tx == t_COMPLEX)
    2395         140 :     return approx_0(gel(x,1), y) && approx_0(gel(x,2), y);
    2396      575306 :   return gequal0(x) ||
    2397      397829 :          (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      588004 : gauss_get_pivot_max(GEN X, GEN X0, long ix, GEN c)
    2403             : {
    2404      588004 :   GEN p, r, x = gel(X,ix), x0 = gel(X0,ix);
    2405      588004 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2406      588004 :   if (c)
    2407             :   {
    2408       84322 :     for (i=1; i<lx; i++)
    2409       55755 :       if (!c[i])
    2410             :       {
    2411       28826 :         long e = gexpo(gel(x,i));
    2412       28826 :         if (e > ex) { ex = e; k = i; }
    2413             :       }
    2414             :   }
    2415             :   else
    2416             :   {
    2417     1921709 :     for (i=ix; i<lx; i++)
    2418             :     {
    2419     1362272 :       long e = gexpo(gel(x,i));
    2420     1362272 :       if (e > ex) { ex = e; k = i; }
    2421             :     }
    2422             :   }
    2423      588004 :   if (!k) return lx;
    2424      575054 :   p = gel(x,k);
    2425      575054 :   r = gel(x0,k); if (isrationalzero(r)) r = x0;
    2426      575054 :   return approx_0(p, r)? lx: k;
    2427             : }
    2428             : static long
    2429       63938 : gauss_get_pivot_padic(GEN X, GEN p, long ix, GEN c)
    2430             : {
    2431       63938 :   GEN x = gel(X, ix);
    2432       63938 :   long i, k = 0, ex = (long)HIGHVALPBIT, lx = lg(x);
    2433       63938 :   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      450723 :     for (i=ix; i<lx; i++)
    2445      386911 :       if (!gequal0(gel(x,i)))
    2446             :       {
    2447      183847 :         long e = gvaluation(gel(x,i), p);
    2448      183847 :         if (e < ex) { ex = e; k = i; }
    2449             :       }
    2450             :   }
    2451       63938 :   return k? k: lx;
    2452             : }
    2453             : static long
    2454        4466 : gauss_get_pivot_NZ(GEN X, GEN x0/*unused*/, long ix, GEN c)
    2455             : {
    2456        4466 :   GEN x = gel(X, ix);
    2457        4466 :   long i, lx = lg(x);
    2458             :   (void)x0;
    2459        4466 :   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        2653 :     for (i=ix; i<lx; i++)
    2467        2639 :       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      191766 : get_pivot_fun(GEN x, GEN x0, GEN *data)
    2479             : {
    2480      191766 :   long i, j, hx, lx = lg(x);
    2481      191766 :   int res = t_INT;
    2482      191766 :   GEN p = NULL;
    2483             : 
    2484      191766 :   *data = NULL;
    2485      191766 :   if (lx == 1) return &gauss_get_pivot_NZ;
    2486      191731 :   hx = lgcols(x);
    2487      859029 :   for (j=1; j<lx; j++)
    2488             :   {
    2489      667333 :     GEN xj = gel(x,j);
    2490     3643248 :     for (i=1; i<hx; i++)
    2491             :     {
    2492     2975950 :       GEN c = gel(xj,i);
    2493     2975950 :       switch(typ(c))
    2494             :       {
    2495             :         case t_REAL:
    2496     1573149 :           res = t_REAL;
    2497     1573149 :           break;
    2498             :         case t_COMPLEX:
    2499         364 :           if (typ(gel(c,1)) == t_REAL || typ(gel(c,2)) == t_REAL) res = t_REAL;
    2500         364 :           break;
    2501             :         case t_INT: case t_INTMOD: case t_FRAC: case t_FFELT: case t_QUAD:
    2502             :         case t_POLMOD: /* exact types */
    2503     1241199 :           break;
    2504             :         case t_PADIC:
    2505      161203 :           p = gel(c,2);
    2506      161203 :           res = t_PADIC;
    2507      161203 :           break;
    2508          35 :         default: return &gauss_get_pivot_NZ;
    2509             :       }
    2510             :     }
    2511             :   }
    2512      191696 :   switch(res)
    2513             :   {
    2514      181644 :     case t_REAL: *data = x0; return &gauss_get_pivot_max;
    2515        8260 :     case t_PADIC: *data = p; return &gauss_get_pivot_padic;
    2516        1792 :     default: return &gauss_get_pivot_NZ;
    2517             :   }
    2518             : }
    2519             : 
    2520             : static GEN
    2521      186247 : get_col(GEN a, GEN b, GEN p, long li)
    2522             : {
    2523      186247 :   GEN u = cgetg(li+1,t_COL);
    2524             :   long i, j;
    2525             : 
    2526      186247 :   gel(u,li) = gdiv(gel(b,li), p);
    2527      763229 :   for (i=li-1; i>0; i--)
    2528             :   {
    2529      576982 :     pari_sp av = avma;
    2530      576982 :     GEN m = gel(b,i);
    2531      576982 :     for (j=i+1; j<=li; j++) m = gsub(m, gmul(gcoeff(a,i,j), gel(u,j)));
    2532      576982 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(a,i,i)));
    2533             :   }
    2534      186247 :   return u;
    2535             : }
    2536             : /* assume 0 <= a[i,j] < p */
    2537             : static GEN
    2538      367980 : Fl_get_col_OK(GEN a, GEN b, long li, ulong p)
    2539             : {
    2540      367980 :   GEN u = cgetg(li+1,t_VECSMALL);
    2541      367980 :   ulong m = uel(b,li) % p;
    2542             :   long i,j;
    2543             : 
    2544      367980 :   uel(u,li) = (m * ucoeff(a,li,li)) % p;
    2545     4062216 :   for (i = li-1; i > 0; i--)
    2546             :   {
    2547     3694236 :     m = p - uel(b,i)%p;
    2548    33580119 :     for (j = i+1; j <= li; j++) {
    2549    29885883 :       if (m & HIGHBIT) m %= p;
    2550    29885883 :       m += ucoeff(a,i,j) * uel(u,j); /* 0 <= u[j] < p */
    2551             :     }
    2552     3694236 :     m %= p;
    2553     3694236 :     if (m) m = ((p-m) * ucoeff(a,i,i)) % p;
    2554     3694236 :     uel(u,i) = m;
    2555             :   }
    2556      367980 :   return u;
    2557             : }
    2558             : static GEN
    2559     1457199 : Fl_get_col(GEN a, GEN b, long li, ulong p)
    2560             : {
    2561     1457199 :   GEN u = cgetg(li+1,t_VECSMALL);
    2562     1457199 :   ulong m = uel(b,li) % p;
    2563             :   long i,j;
    2564             : 
    2565     1457199 :   uel(u,li) = Fl_mul(m, ucoeff(a,li,li), p);
    2566     4300272 :   for (i=li-1; i>0; i--)
    2567             :   {
    2568     2843073 :     m = b[i]%p;
    2569     7903609 :     for (j = i+1; j <= li; j++)
    2570     5060536 :       m = Fl_sub(m, Fl_mul(ucoeff(a,i,j), uel(u,j), p), p);
    2571     2843073 :     if (m) m = Fl_mul(m, ucoeff(a,i,i), p);
    2572     2843073 :     uel(u,i) = m;
    2573             :   }
    2574     1457199 :   return u;
    2575             : }
    2576             : 
    2577             : /* bk -= m * bi */
    2578             : static void
    2579     2823568 : _submul(GEN b, long k, long i, GEN m)
    2580             : {
    2581     2823568 :   gel(b,k) = gsub(gel(b,k), gmul(m, gel(b,i)));
    2582     2823568 : }
    2583             : static int
    2584      749897 : init_gauss(GEN a, GEN *b, long *aco, long *li, int *iscol)
    2585             : {
    2586      749897 :   *iscol = *b ? (typ(*b) == t_COL): 0;
    2587      749897 :   *aco = lg(a) - 1;
    2588      749897 :   if (!*aco) /* a empty */
    2589             :   {
    2590          70 :     if (*b && lg(*b) != 1) pari_err_DIM("gauss");
    2591          70 :     *li = 0; return 0;
    2592             :   }
    2593      749827 :   *li = nbrows(a);
    2594      749827 :   if (*li < *aco) pari_err_INV("gauss [no left inverse]", a);
    2595      749827 :   if (*b)
    2596             :   {
    2597      741200 :     switch(typ(*b))
    2598             :     {
    2599             :       case t_MAT:
    2600       84147 :         if (lg(*b) == 1) return 0;
    2601       84147 :         *b = RgM_shallowcopy(*b);
    2602       84147 :         break;
    2603             :       case t_COL:
    2604      657053 :         *b = mkmat( leafcopy(*b) );
    2605      657053 :         break;
    2606           0 :       default: pari_err_TYPE("gauss",*b);
    2607             :     }
    2608      741200 :     if (nbrows(*b) != *li) pari_err_DIM("gauss");
    2609             :   }
    2610             :   else
    2611        8627 :     *b = matid(*li);
    2612      749827 :   return 1;
    2613             : }
    2614             : 
    2615             : static GEN Flm_inv_sp(GEN a, ulong *detp, ulong p);
    2616             : static GEN
    2617       28098 : RgM_inv_QM(GEN M)
    2618             : {
    2619       28098 :   pari_sp av = avma;
    2620       28098 :   GEN den, cM, pM = Q_primitive_part(M, &cM);
    2621       28098 :   GEN b = ZM_inv(pM, &den);
    2622       28084 :   if (!b) { avma = av; return NULL; }
    2623       28077 :   if (cM) den = gmul(den, cM);
    2624       28077 :   if (!gequal1(den)) b = ZM_Q_mul(b, ginv(den));
    2625       28077 :   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 b, T = RgX_to_FpX(pol, p);
    2655          42 :   if (signe(T) == 0) pari_err_OP("^",x,gen_m1);
    2656          42 :   b = FqM_inv(RgM_to_FqM(x, T, p), T, p);
    2657          42 :   if (!b) { avma = av; return NULL; }
    2658          28 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    2659             : }
    2660             : 
    2661             : #define code(t1,t2) ((t1 << 6) | t2)
    2662             : static GEN
    2663       45475 : RgM_inv_fast(GEN x)
    2664             : {
    2665             :   GEN p, pol;
    2666             :   long pa;
    2667       45475 :   long t = RgM_type(x, &p,&pol,&pa);
    2668       45475 :   switch(t)
    2669             :   {
    2670             :     case t_INT:    /* Fall back */
    2671       28098 :     case t_FRAC:   return RgM_inv_QM(x);
    2672         147 :     case t_FFELT:  return FFM_inv(x, pol);
    2673         112 :     case t_INTMOD: return RgM_inv_FpM(x, p);
    2674             :     case code(t_POLMOD, t_INTMOD):
    2675          42 :                    return RgM_inv_FqM(x, pol, p);
    2676       17076 :     default:       return gen_0;
    2677             :   }
    2678             : }
    2679             : #undef code
    2680             : 
    2681             : static GEN
    2682          49 : RgM_RgC_solve_FpC(GEN a, GEN b, GEN p)
    2683             : {
    2684          49 :   pari_sp av = avma;
    2685             :   ulong pp;
    2686          49 :   a = RgM_Fp_init(a, p, &pp);
    2687          49 :   switch(pp)
    2688             :   {
    2689             :   case 0:
    2690          14 :     b = RgC_to_FpC(b, p);
    2691          14 :     a = FpM_FpC_gauss(a,b,p);
    2692          14 :     return a ? gerepileupto(av, FpC_to_mod(a, p)): NULL;
    2693             :   case 2:
    2694          14 :     b = RgV_to_F2v(b);
    2695          14 :     a = F2m_F2c_gauss(a,b);
    2696          14 :     return a ? gerepileupto(av, F2c_to_mod(a)): NULL;
    2697             :   default:
    2698          21 :     b = RgV_to_Flv(b, pp);
    2699          21 :     a = Flm_Flc_gauss(a, b, pp);
    2700          21 :     return a ? gerepileupto(av, Flc_to_mod(a, pp)): NULL;
    2701             :   }
    2702             : }
    2703             : 
    2704             : static GEN
    2705          98 : RgM_solve_FpM(GEN a, GEN b, GEN p)
    2706             : {
    2707          98 :   pari_sp av = avma;
    2708             :   ulong pp;
    2709          98 :   a = RgM_Fp_init(a, p, &pp);
    2710          98 :   switch(pp)
    2711             :   {
    2712             :   case 0:
    2713          35 :     b = RgM_to_FpM(b, p);
    2714          35 :     a = FpM_gauss(a,b,p);
    2715          35 :     return a ? gerepileupto(av, FpM_to_mod(a, p)): NULL;
    2716             :   case 2:
    2717          21 :     b = RgM_to_F2m(b);
    2718          21 :     a = F2m_gauss(a,b);
    2719          21 :     return a ? gerepileupto(av, F2m_to_mod(a)): NULL;
    2720             :   default:
    2721          42 :     b = RgM_to_Flm(b, pp);
    2722          42 :     a = Flm_gauss(a,b,pp);
    2723          42 :     return a ? gerepileupto(av, Flm_to_mod(a, pp)): NULL;
    2724             :   }
    2725             : }
    2726             : 
    2727             : /* Gaussan Elimination. If a is square, return a^(-1)*b;
    2728             :  * if a has more rows than columns and b is NULL, return c such that c a = Id.
    2729             :  * a is a (not necessarily square) matrix
    2730             :  * b is a matrix or column vector, NULL meaning: take the identity matrix,
    2731             :  *   effectively returning the inverse of a
    2732             :  * If a and b are empty, the result is the empty matrix.
    2733             :  *
    2734             :  * li: number of rows of a and b
    2735             :  * aco: number of columns of a
    2736             :  * bco: number of columns of b (if matrix)
    2737             :  */
    2738             : static GEN
    2739      267647 : RgM_solve_basecase(GEN a, GEN b)
    2740             : {
    2741      267647 :   pari_sp av = avma;
    2742             :   long i, j, k, li, bco, aco;
    2743             :   int iscol;
    2744             :   pivot_fun pivot;
    2745             :   GEN p, u, data;
    2746             : 
    2747      267647 :   avma = av;
    2748             : 
    2749      267647 :   if (lg(a)-1 == 2 && nbrows(a) == 2) {
    2750             :     /* 2x2 matrix, start by inverting a */
    2751       99700 :     GEN u = gcoeff(a,1,1), v = gcoeff(a,1,2);
    2752       99700 :     GEN w = gcoeff(a,2,1), x = gcoeff(a,2,2);
    2753       99700 :     GEN D = gsub(gmul(u,x), gmul(v,w)), ainv;
    2754       99700 :     if (gequal0(D)) return NULL;
    2755       99700 :     ainv = mkmat2(mkcol2(x, gneg(w)), mkcol2(gneg(v), u));
    2756       99700 :     ainv = gmul(ainv, ginv(D));
    2757       99700 :     if (b) ainv = gmul(ainv, b);
    2758       99700 :     return gerepileupto(av, ainv);
    2759             :   }
    2760             : 
    2761      167947 :   if (!init_gauss(a, &b, &aco, &li, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    2762      167947 :   pivot = get_pivot_fun(a, a, &data);
    2763      167947 :   a = RgM_shallowcopy(a);
    2764      167947 :   bco = lg(b)-1;
    2765      167947 :   if(DEBUGLEVEL>4) err_printf("Entering gauss\n");
    2766             : 
    2767      167947 :   p = NULL; /* gcc -Wall */
    2768      555087 :   for (i=1; i<=aco; i++)
    2769             :   {
    2770             :     /* k is the line where we find the pivot */
    2771      555087 :     k = pivot(a, data, i, NULL);
    2772      555087 :     if (k > li) return NULL;
    2773      555073 :     if (k != i)
    2774             :     { /* exchange the lines s.t. k = i */
    2775      122950 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    2776      122950 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    2777             :     }
    2778      555073 :     p = gcoeff(a,i,i);
    2779      555073 :     if (i == aco) break;
    2780             : 
    2781     1181585 :     for (k=i+1; k<=li; k++)
    2782             :     {
    2783      794445 :       GEN m = gcoeff(a,k,i);
    2784      794445 :       if (!gequal0(m))
    2785             :       {
    2786      628375 :         m = gdiv(m,p);
    2787      628375 :         for (j=i+1; j<=aco; j++) _submul(gel(a,j),k,i,m);
    2788      628375 :         for (j=1;   j<=bco; j++) _submul(gel(b,j),k,i,m);
    2789             :       }
    2790             :     }
    2791      387140 :     if (gc_needed(av,1))
    2792             :     {
    2793          62 :       if(DEBUGMEM>1) pari_warn(warnmem,"gauss. i=%ld",i);
    2794          62 :       gerepileall(av,2, &a,&b);
    2795             :     }
    2796             :   }
    2797             : 
    2798      167933 :   if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
    2799      167933 :   u = cgetg(bco+1,t_MAT);
    2800      167933 :   for (j=1; j<=bco; j++) gel(u,j) = get_col(a,gel(b,j),p,aco);
    2801      167933 :   return gerepilecopy(av, iscol? gel(u,1): u);
    2802             : }
    2803             : 
    2804             : static GEN
    2805      261198 : RgM_RgC_solve_fast(GEN x, GEN y)
    2806             : {
    2807             :   GEN p, pol;
    2808             :   long pa;
    2809      261198 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    2810      261198 :   switch(t)
    2811             :   {
    2812       13363 :     case t_INT:    return ZM_gauss(x, y);
    2813          42 :     case t_FRAC:   return QM_gauss(x, y);
    2814          49 :     case t_INTMOD: return RgM_RgC_solve_FpC(x, y, p);
    2815          56 :     case t_FFELT:  return FFM_FFC_gauss(x, y, pol);
    2816      247688 :     default:       return gen_0;
    2817             :   }
    2818             : }
    2819             : 
    2820             : static GEN
    2821       79458 : RgM_solve_fast(GEN x, GEN y)
    2822             : {
    2823             :   GEN p, pol;
    2824             :   long pa;
    2825       79458 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    2826       79458 :   switch(t)
    2827             :   {
    2828       76407 :     case t_INT:    return ZM_gauss(x, y);
    2829           7 :     case t_FRAC:   return QM_gauss(x, y);
    2830          98 :     case t_INTMOD: return RgM_solve_FpM(x, y, p);
    2831          63 :     case t_FFELT:  return FFM_gauss(x, y, pol);
    2832        2883 :     default:       return gen_0;
    2833             :   }
    2834             : }
    2835             : 
    2836             : GEN
    2837      340656 : RgM_solve(GEN a, GEN b)
    2838             : {
    2839      340656 :   pari_sp av = avma;
    2840             :   GEN u;
    2841      340656 :   if (!b) return RgM_inv(a);
    2842      340656 :   u = typ(b)==t_MAT ? RgM_solve_fast(a, b): RgM_RgC_solve_fast(a, b);
    2843      340656 :   if (!u) { avma = av; return u; }
    2844      340558 :   if (u != gen_0) return u;
    2845      250571 :   return RgM_solve_basecase(a, b);
    2846             : }
    2847             : 
    2848             : GEN
    2849       45475 : RgM_inv(GEN a)
    2850             : {
    2851       45475 :   GEN b = RgM_inv_fast(a);
    2852       45461 :   return b==gen_0? RgM_solve_basecase(a, NULL): b;
    2853             : }
    2854             : 
    2855             : /* assume dim A >= 1, A invertible + upper triangular  */
    2856             : static GEN
    2857      330401 : RgM_inv_upper_ind(GEN A, long index)
    2858             : {
    2859      330401 :   long n = lg(A)-1, i = index, j;
    2860      330401 :   GEN u = zerocol(n);
    2861      330401 :   gel(u,i) = ginv(gcoeff(A,i,i));
    2862     1366824 :   for (i--; i>0; i--)
    2863             :   {
    2864     1036423 :     pari_sp av = avma;
    2865     1036423 :     GEN m = gneg(gmul(gcoeff(A,i,i+1),gel(u,i+1))); /* j = i+1 */
    2866     1036423 :     for (j=i+2; j<=n; j++) m = gsub(m, gmul(gcoeff(A,i,j),gel(u,j)));
    2867     1036423 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(A,i,i)));
    2868             :   }
    2869      330401 :   return u;
    2870             : }
    2871             : GEN
    2872       58961 : RgM_inv_upper(GEN A)
    2873             : {
    2874             :   long i, l;
    2875       58961 :   GEN B = cgetg_copy(A, &l);
    2876       58961 :   for (i = 1; i < l; i++) gel(B,i) = RgM_inv_upper_ind(A, i);
    2877       58961 :   return B;
    2878             : }
    2879             : 
    2880             : /* assume dim A >= 1, A invertible + upper triangular, 1s on diagonal  */
    2881             : static GEN
    2882          98 : FpM_inv_upper_1_ind(GEN A, long index, GEN p)
    2883             : {
    2884          98 :   long n = lg(A)-1, i = index, j;
    2885          98 :   GEN u = zerocol(n);
    2886          98 :   gel(u,i) = gen_1;
    2887         567 :   for (i--; i>0; i--)
    2888             :   {
    2889         469 :     pari_sp av = avma;
    2890         469 :     GEN m = negi(mulii(gcoeff(A,i,i+1),gel(u,i+1))); /* j = i+1 */
    2891         469 :     for (j=i+2; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2892         469 :     gel(u,i) = gerepileuptoint(av, modii(m,p));
    2893             :   }
    2894          98 :   return u;
    2895             : }
    2896             : static GEN
    2897          14 : FpM_inv_upper_1(GEN A, GEN p)
    2898             : {
    2899             :   long i, l;
    2900          14 :   GEN B = cgetg_copy(A, &l);
    2901          14 :   for (i = 1; i < l; i++) gel(B,i) = FpM_inv_upper_1_ind(A, i, p);
    2902          14 :   return B;
    2903             : }
    2904             : /* assume dim A >= 1, A invertible + upper triangular, 1s on diagonal,
    2905             :  * reduced mod p */
    2906             : static GEN
    2907          28 : Flm_inv_upper_1_ind(GEN A, long index, ulong p)
    2908             : {
    2909          28 :   long n = lg(A)-1, i = index, j;
    2910          28 :   GEN u = const_vecsmall(n, 0);
    2911          28 :   u[i] = 1;
    2912          28 :   if (SMALL_ULONG(p))
    2913          21 :     for (i--; i>0; i--)
    2914             :     {
    2915           7 :       ulong m = ucoeff(A,i,i+1) * uel(u,i+1); /* j = i+1 */
    2916           7 :       for (j=i+2; j<=n; j++)
    2917             :       {
    2918           0 :         if (m & HIGHMASK) m %= p;
    2919           0 :         m += ucoeff(A,i,j) * uel(u,j);
    2920             :       }
    2921           7 :       u[i] = Fl_neg(m % p, p);
    2922             :     }
    2923             :   else
    2924          21 :     for (i--; i>0; i--)
    2925             :     {
    2926           7 :       ulong m = Fl_mul(ucoeff(A,i,i+1),uel(u,i+1), p); /* j = i+1 */
    2927           7 :       for (j=i+2; j<=n; j++) m = Fl_add(m, Fl_mul(ucoeff(A,i,j),uel(u,j),p), p);
    2928           7 :       u[i] = Fl_neg(m, p);
    2929             :     }
    2930          28 :   return u;
    2931             : }
    2932             : static GEN
    2933          14 : F2m_inv_upper_1_ind(GEN A, long index)
    2934             : {
    2935          14 :   pari_sp av = avma;
    2936          14 :   long n = lg(A)-1, i = index, j;
    2937          14 :   GEN u = const_vecsmall(n, 0);
    2938          14 :   u[i] = 1;
    2939          21 :   for (i--; i>0; i--)
    2940             :   {
    2941           7 :     ulong m = F2m_coeff(A,i,i+1) & uel(u,i+1); /* j = i+1 */
    2942           7 :     for (j=i+2; j<=n; j++) m ^= F2m_coeff(A,i,j) & uel(u,j);
    2943           7 :     u[i] = m & 1;
    2944             :   }
    2945          14 :   return gerepileuptoleaf(av, Flv_to_F2v(u));
    2946             : }
    2947             : static GEN
    2948          14 : Flm_inv_upper_1(GEN A, ulong p)
    2949             : {
    2950             :   long i, l;
    2951          14 :   GEN B = cgetg_copy(A, &l);
    2952          14 :   for (i = 1; i < l; i++) gel(B,i) = Flm_inv_upper_1_ind(A, i, p);
    2953          14 :   return B;
    2954             : }
    2955             : static GEN
    2956           7 : F2m_inv_upper_1(GEN A)
    2957             : {
    2958             :   long i, l;
    2959           7 :   GEN B = cgetg_copy(A, &l);
    2960           7 :   for (i = 1; i < l; i++) gel(B,i) = F2m_inv_upper_1_ind(A, i);
    2961           7 :   return B;
    2962             : }
    2963             : 
    2964             : static GEN
    2965      972118 : split_realimag_col(GEN z, long r1, long r2)
    2966             : {
    2967      972118 :   long i, ru = r1+r2;
    2968      972118 :   GEN x = cgetg(ru+r2+1,t_COL), y = x + r2;
    2969     2912879 :   for (i=1; i<=r1; i++) {
    2970     1940761 :     GEN a = gel(z,i);
    2971     1940761 :     if (typ(a) == t_COMPLEX) a = gel(a,1); /* paranoia: a should be real */
    2972     1940761 :     gel(x,i) = a;
    2973             :   }
    2974     1611488 :   for (   ; i<=ru; i++) {
    2975      639370 :     GEN b, a = gel(z,i);
    2976      639370 :     if (typ(a) == t_COMPLEX) { b = gel(a,2); a = gel(a,1); } else b = gen_0;
    2977      639370 :     gel(x,i) = a;
    2978      639370 :     gel(y,i) = b;
    2979             :   }
    2980      972118 :   return x;
    2981             : }
    2982             : GEN
    2983      510669 : split_realimag(GEN x, long r1, long r2)
    2984             : {
    2985             :   long i,l; GEN y;
    2986      510669 :   if (typ(x) == t_COL) return split_realimag_col(x,r1,r2);
    2987      251746 :   y = cgetg_copy(x, &l);
    2988      251746 :   for (i=1; i<l; i++) gel(y,i) = split_realimag_col(gel(x,i), r1, r2);
    2989      251746 :   return y;
    2990             : }
    2991             : 
    2992             : /* assume M = (r1+r2) x (r1+2r2) matrix and y compatible vector or matrix
    2993             :  * r1 first lines of M,y are real. Solve the system obtained by splitting
    2994             :  * real and imaginary parts. */
    2995             : GEN
    2996      248289 : RgM_solve_realimag(GEN M, GEN y)
    2997             : {
    2998      248289 :   long l = lg(M), r2 = l - lgcols(M), r1 = l-1 - 2*r2;
    2999      248289 :   return RgM_solve(split_realimag(M, r1,r2),
    3000             :                    split_realimag(y, r1,r2));
    3001             : }
    3002             : 
    3003             : GEN
    3004         420 : gauss(GEN a, GEN b)
    3005             : {
    3006             :   GEN z;
    3007         420 :   long t = typ(b);
    3008         420 :   if (typ(a)!=t_MAT) pari_err_TYPE("gauss",a);
    3009         420 :   if (t!=t_COL && t!=t_MAT) pari_err_TYPE("gauss",b);
    3010         420 :   z = RgM_solve(a,b);
    3011         420 :   if (!z) pari_err_INV("gauss",a);
    3012         315 :   return z;
    3013             : }
    3014             : 
    3015             : static GEN
    3016      131994 : F2_get_col(GEN b, GEN d, long li, long aco)
    3017             : {
    3018      131994 :   long i, l = nbits2lg(aco);
    3019      131994 :   GEN u = cgetg(l, t_VECSMALL);
    3020      131994 :   u[1] = aco;
    3021     1829160 :   for (i = 1; i <= li; i++)
    3022     1697166 :     if (d[i]) /* d[i] can still be 0 if li > aco */
    3023             :     {
    3024     1628328 :       if (F2v_coeff(b, i))
    3025      523043 :         F2v_set(u, d[i]);
    3026             :       else
    3027     1105285 :         F2v_clear(u, d[i]);
    3028             :     }
    3029      131994 :   return u;
    3030             : }
    3031             : 
    3032             : /* destroy a, b */
    3033             : static GEN
    3034       20459 : F2m_gauss_sp(GEN a, GEN b)
    3035             : {
    3036       20459 :   long i, j, k, l, li, bco, aco = lg(a)-1;
    3037             :   GEN u, d;
    3038             : 
    3039       20459 :   if (!aco) return cgetg(1,t_MAT);
    3040       20459 :   li = gel(a,1)[1];
    3041       20459 :   d = zero_Flv(li);
    3042       20459 :   bco = lg(b)-1;
    3043      143801 :   for (i=1; i<=aco; i++)
    3044             :   {
    3045      123363 :     GEN ai = vecsmall_copy(gel(a,i));
    3046      123363 :     if (!d[i] && F2v_coeff(ai, i))
    3047       60561 :       k = i;
    3048             :     else
    3049       62802 :       for (k = 1; k <= li; k++) if (!d[k] && F2v_coeff(ai,k)) break;
    3050             :     /* found a pivot on row k */
    3051      123363 :     if (k > li) return NULL;
    3052      123342 :     d[k] = i;
    3053             : 
    3054             :     /* Clear k-th row but column-wise instead of line-wise */
    3055             :     /*  a_ij -= a_i1*a1j/a_11
    3056             :        line-wise grouping:  L_j -= a_1j/a_11*L_1
    3057             :        column-wise:         C_i -= a_i1/a_11*C_1
    3058             :     */
    3059      123342 :     F2v_clear(ai,k);
    3060     1734555 :     for (l=1; l<=aco; l++)
    3061             :     {
    3062     1611213 :       GEN al = gel(a,l);
    3063     1611213 :       if (F2v_coeff(al,k)) F2v_add_inplace(al,ai);
    3064             :     }
    3065     1751929 :     for (l=1; l<=bco; l++)
    3066             :     {
    3067     1628587 :       GEN bl = gel(b,l);
    3068     1628587 :       if (F2v_coeff(bl,k)) F2v_add_inplace(bl,ai);
    3069             :     }
    3070             :   }
    3071       20438 :   u = cgetg(bco+1,t_MAT);
    3072       20438 :   for (j = 1; j <= bco; j++) gel(u,j) = F2_get_col(gel(b,j), d, li, aco);
    3073       20438 :   return u;
    3074             : }
    3075             : 
    3076             : GEN
    3077          35 : F2m_gauss(GEN a, GEN b)
    3078             : {
    3079          35 :   pari_sp av = avma;
    3080          35 :   if (lg(a) == 1) return cgetg(1,t_MAT);
    3081          35 :   return gerepileupto(av, F2m_gauss_sp(F2m_copy(a), F2m_copy(b)));
    3082             : }
    3083             : GEN
    3084          14 : F2m_F2c_gauss(GEN a, GEN b)
    3085             : {
    3086          14 :   pari_sp av = avma;
    3087          14 :   GEN z = F2m_gauss(a, mkmat(b));
    3088          14 :   if (!z) { avma = av; return NULL; }
    3089           7 :   if (lg(z) == 1) { avma = av; return cgetg(1,t_VECSMALL); }
    3090           7 :   return gerepileuptoleaf(av, gel(z,1));
    3091             : }
    3092             : 
    3093             : GEN
    3094          35 : F2m_inv(GEN a)
    3095             : {
    3096          35 :   pari_sp av = avma;
    3097          35 :   if (lg(a) == 1) return cgetg(1,t_MAT);
    3098          35 :   return gerepileupto(av, F2m_gauss_sp(F2m_copy(a), matid_F2m(gel(a,1)[1])));
    3099             : }
    3100             : 
    3101             : /* destroy a, b */
    3102             : static GEN
    3103       51554 : Flm_gauss_sp_OK(GEN a, GEN b, ulong *detp, ulong p)
    3104             : {
    3105       51554 :   long i, j, k, li, bco, aco = lg(a)-1, s = 1;
    3106       51554 :   ulong det = 1;
    3107             :   GEN u;
    3108             : 
    3109       51554 :   li = nbrows(a);
    3110       51554 :   bco = lg(b)-1;
    3111      354069 :   for (i=1; i<=aco; i++)
    3112             :   {
    3113             :     ulong invpiv;
    3114             :     /* Fl_get_col wants 0 <= a[i,j] < p for all i,j */
    3115      354069 :     for (k = 1; k < i; k++) ucoeff(a,k,i) %= p;
    3116      609625 :     for (k = i; k <= li; k++)
    3117             :     {
    3118      609618 :       ulong piv = ( ucoeff(a,k,i) %= p );
    3119      609618 :       if (piv)
    3120             :       {
    3121      354062 :         ucoeff(a,k,i) = Fl_inv(piv, p);
    3122      354062 :         if (detp)
    3123             :         {
    3124           0 :           if (det & HIGHMASK) det %= p;
    3125           0 :           det *= piv;
    3126             :         }
    3127      354062 :         break;
    3128             :       }
    3129             :     }
    3130             :     /* found a pivot on line k */
    3131      354069 :     if (k > li) return NULL;
    3132      354062 :     if (k != i)
    3133             :     { /* swap lines so that k = i */
    3134       99408 :       s = -s;
    3135       99408 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    3136       99408 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    3137             :     }
    3138      354062 :     if (i == aco) break;
    3139             : 
    3140      302515 :     invpiv = p - ucoeff(a,i,i); /* -1/piv mod p */
    3141     2163272 :     for (k=i+1; k<=li; k++)
    3142             :     {
    3143     1860757 :       ulong m = ( ucoeff(a,k,i) %= p );
    3144     1860757 :       if (!m) continue;
    3145             : 
    3146      609463 :       m = Fl_mul(m, invpiv, p);
    3147      609463 :       if (m == 1) {
    3148      102120 :         for (j=i+1; j<=aco; j++) _Fl_add_OK(gel(a,j),k,i, p);
    3149      102120 :         for (j=1;   j<=bco; j++) _Fl_add_OK(gel(b,j),k,i, p);
    3150             :       } else {
    3151      507343 :         for (j=i+1; j<=aco; j++) _Fl_addmul_OK(gel(a,j),k,i,m, p);
    3152      507343 :         for (j=1;   j<=bco; j++) _Fl_addmul_OK(gel(b,j),k,i,m, p);
    3153             :       }
    3154             :     }
    3155             :   }
    3156       51547 :   if (detp)
    3157             :   {
    3158           0 :     det %=  p;
    3159           0 :     if (s < 0 && det) det = p - det;
    3160           0 :     *detp = det;
    3161             :   }
    3162       51547 :   u = cgetg(bco+1,t_MAT);
    3163       51547 :   for (j=1; j<=bco; j++) gel(u,j) = Fl_get_col_OK(a,gel(b,j), aco,p);
    3164       51547 :   return u;
    3165             : }
    3166             : 
    3167             : /* destroy a, b */
    3168             : static GEN
    3169      601990 : Flm_gauss_sp(GEN a, GEN b, ulong *detp, ulong p)
    3170             : {
    3171      601990 :   long i, j, k, li, bco, aco = lg(a)-1, s = 1;
    3172      601990 :   ulong det = 1;
    3173             :   GEN u;
    3174             :   ulong pi;
    3175      601990 :   if (!aco) { if (detp) *detp = 1; return cgetg(1,t_MAT); }
    3176      601990 :   if (SMALL_ULONG(p)) return Flm_gauss_sp_OK(a, b, detp, p);
    3177      550436 :   pi = get_Fl_red(p);
    3178      550436 :   li = nbrows(a);
    3179      550436 :   bco = lg(b)-1;
    3180     1457208 :   for (i=1; i<=aco; i++)
    3181             :   {
    3182             :     ulong invpiv;
    3183             :     /* Fl_get_col wants 0 <= a[i,j] < p for all i,j */
    3184     1511880 :     for (k = i; k <= li; k++)
    3185             :     {
    3186     1511873 :       ulong piv = ucoeff(a,k,i);
    3187     1511873 :       if (piv)
    3188             :       {
    3189     1457201 :         ucoeff(a,k,i) = Fl_inv(piv, p);
    3190     1457201 :         if (detp) det = Fl_mul_pre(det, piv, p, pi);
    3191     1457201 :         break;
    3192             :       }
    3193             :     }
    3194             :     /* found a pivot on line k */
    3195     1457208 :     if (k > li) return NULL;
    3196     1457201 :     if (k != i)
    3197             :     { /* swap lines so that k = i */
    3198       47108 :       s = -s;
    3199       47108 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    3200       47108 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    3201             :     }
    3202     1457201 :     if (i == aco) break;
    3203             : 
    3204      906772 :     invpiv = p - ucoeff(a,i,i); /* -1/piv mod p */
    3205     2328372 :     for (k=i+1; k<=li; k++)
    3206             :     {
    3207     1421600 :       ulong m = ucoeff(a,k,i);
    3208     1421600 :       if (!m) continue;
    3209             : 
    3210     1150437 :       m = Fl_mul_pre(m, invpiv, p, pi);
    3211     1150437 :       if (m == 1) {
    3212       28045 :         for (j=i+1; j<=aco; j++) _Fl_add(gel(a,j),k,i, p);
    3213       28045 :         for (j=1;   j<=bco; j++) _Fl_add(gel(b,j),k,i, p);
    3214             :       } else {
    3215     1122392 :         for (j=i+1; j<=aco; j++) _Fl_addmul(gel(a,j),k,i,m, p, pi);
    3216     1122392 :         for (j=1;   j<=bco; j++) _Fl_addmul(gel(b,j),k,i,m, p, pi);
    3217             :       }
    3218             :     }
    3219             :   }
    3220      550429 :   if (detp)
    3221             :   {
    3222           0 :     if (s < 0 && det) det = p - det;
    3223           0 :     *detp = det;
    3224             :   }
    3225      550429 :   u = cgetg(bco+1,t_MAT);
    3226      550429 :   for (j=1; j<=bco; j++) gel(u,j) = Fl_get_col(a,gel(b,j), aco,p);
    3227      550429 :   return u;
    3228             : }
    3229             : 
    3230             : static GEN
    3231      184094 : Flm_gauss_from_CUP(GEN b, GEN R, GEN C, GEN U, GEN P, ulong p, ulong *detp)
    3232             : {
    3233      184094 :   GEN Y = Flm_rsolve_lower_unit(rowpermute(C, R), rowpermute(b, R), p);
    3234      184095 :   GEN X = rowpermute(Flm_rsolve_upper(U, Y, p), perm_inv(P));
    3235      184094 :   if (detp)
    3236             :   {
    3237      141274 :     ulong d = perm_sign(P) == 1? 1: p-1;
    3238      141276 :     long i, r = lg(R);
    3239     1354373 :     for (i = 1; i < r; i++)
    3240     1213099 :       d = Fl_mul(d, ucoeff(U, i, i), p);
    3241      141274 :     *detp = d;
    3242             :   }
    3243      184094 :   return X;
    3244             : }
    3245             : 
    3246             : static GEN
    3247       42835 : Flm_gauss_CUP(GEN a, GEN b, ulong *detp, ulong p) {
    3248             :   GEN R, C, U, P;
    3249       42835 :   long n = lg(a) - 1, r;
    3250       42835 :   if (nbrows(a) < n || (r = Flm_CUP(a, &R, &C, &U, &P, p)) < n)
    3251          14 :     return NULL;
    3252       42821 :   return Flm_gauss_from_CUP(b, R, C, U, P, p, detp);
    3253             : }
    3254             : 
    3255             : GEN
    3256          63 : Flm_gauss(GEN a, GEN b, ulong p) {
    3257          63 :   pari_sp av = avma;
    3258             :   GEN x;
    3259          63 :   if (lg(a) - 1 >= Flm_CUP_LIMIT)
    3260          14 :     x = Flm_gauss_CUP(a, b, NULL, p);
    3261             :   else {
    3262          49 :     a = RgM_shallowcopy(a);
    3263          49 :     b = RgM_shallowcopy(b);
    3264          49 :     x = Flm_gauss_sp(a, b, NULL, p);
    3265             :   }
    3266          63 :   if (!x) { avma = av; return NULL; }
    3267          49 :   return gerepileupto(av, x);
    3268             : }
    3269             : 
    3270             : static GEN
    3271      603391 : Flm_inv_i(GEN a, ulong *detp, ulong p, long inplace) {
    3272      603391 :   pari_sp av = avma;
    3273      603391 :   long n = lg(a) - 1;
    3274             :   GEN b, x;
    3275      603391 :   if (n == 0) return cgetg(1, t_MAT);
    3276      603391 :   b = matid_Flm(nbrows(a));
    3277      603391 :   if (n >= Flm_CUP_LIMIT)
    3278       42821 :     x = Flm_gauss_CUP(a, b, detp, p);
    3279             :   else {
    3280      560570 :     if (!inplace)
    3281       10178 :       a = RgM_shallowcopy(a);
    3282      560570 :     x = Flm_gauss_sp(a, b, detp, p);
    3283             :   }
    3284      603391 :   if (!x) { avma = av; return NULL; }
    3285      603377 :   return gerepileupto(av, x);
    3286             : }
    3287             : 
    3288             : static GEN
    3289      581901 : Flm_inv_sp(GEN a, ulong *detp, ulong p) {
    3290      581901 :   return Flm_inv_i(a, detp, p, 1);
    3291             : }
    3292             : 
    3293             : GEN
    3294       21490 : Flm_inv(GEN a, ulong p) {
    3295       21490 :   return Flm_inv_i(a, NULL, p, 0);
    3296             : }
    3297             : 
    3298             : GEN
    3299          21 : Flm_Flc_gauss(GEN a, GEN b, ulong p) {
    3300          21 :   pari_sp av = avma;
    3301          21 :   GEN z = Flm_gauss(a, mkmat(b), p);
    3302          21 :   if (!z) { avma = av; return NULL; }
    3303          14 :   if (lg(z) == 1) { avma = av; return cgetg(1,t_VECSMALL); }
    3304          14 :   return gerepileuptoleaf(av, gel(z,1));
    3305             : }
    3306             : 
    3307             : GEN
    3308      141287 : Flm_adjoint(GEN A, ulong p)
    3309             : {
    3310      141287 :   pari_sp av = avma;
    3311             :   GEN R, C, U, P, C1, U1, v, c, d;
    3312      141287 :   long r, i, q, n = lg(A)-1, m;
    3313             :   ulong D;
    3314      141287 :   if (n == 0) return cgetg(1, t_MAT);
    3315      141287 :   r = Flm_CUP(A, &R, &C, &U, &P, p);
    3316      141288 :   m = nbrows(A);
    3317      141287 :   if (r == n)
    3318             :   {
    3319      141273 :     GEN X = Flm_gauss_from_CUP(matid_Flm(m), R, C, U, P, p, &D);
    3320      141273 :     return gerepileupto(av, Flm_Fl_mul(X, D, p));
    3321             :   }
    3322          14 :   if (r < n-1) return zero_Flm(n, m);
    3323          28 :   for (q = n, i = 1; i < n; i++)
    3324          14 :     if (R[i] != i) { q = i; break; }
    3325          14 :   C1 = matslice(C, 1, q-1, 1, q-1);
    3326          14 :   c = vecslice(Flm_row(C, q), 1, q-1);
    3327          14 :   c = Flm_lsolve_lower_unit(C1, Flm_transpose(mkmat(c)), p);
    3328          14 :   d = cgetg(m+1, t_VECSMALL);
    3329          14 :   for (i=1; i<q; i++)    uel(d,i) = ucoeff(c,1,i);
    3330          14 :   uel(d,q) = p-1;
    3331          14 :   for (i=q+1; i<=m; i++) uel(d,i) = 0;
    3332          14 :   U1 = vecslice(U, 1, n-1);
    3333          14 :   v = gel(Flm_rsolve_upper(U1, mkmat(gel(U,n)), p),1);
    3334          14 :   v = vecsmall_append(v, p-1);
    3335          14 :   D = perm_sign(P) != (odd(q+n)?-1:1) ? p-1 : 1;
    3336          28 :   for (i = 1; i <= n-1; i++)
    3337          14 :     D = Fl_mul(D, ucoeff(U1, i, i), p);
    3338          14 :   d = Flv_Fl_mul(d, D, p);
    3339          14 :   return rowpermute(Flc_Flv_mul(v, d, p),perm_inv(P));
    3340             : }
    3341             : 
    3342             : static GEN
    3343        3982 : FpM_gauss_gen(GEN a, GEN b, GEN p)
    3344             : {
    3345             :   void *E;
    3346        3982 :   const struct bb_field *S = get_Fp_field(&E,p);
    3347        3982 :   return gen_Gauss(a,b, E, S);
    3348             : }
    3349             : /* a an FpM, lg(a)>1; b an FpM or NULL (replace by identity) */
    3350             : static GEN
    3351       65742 : FpM_gauss_i(GEN a, GEN b, GEN p, ulong *pp)
    3352             : {
    3353       65742 :   long n = nbrows(a);
    3354       65742 :   a = FpM_init(a,p,pp);
    3355       65742 :   switch(*pp)
    3356             :   {
    3357             :   case 0:
    3358        3982 :     if (!b) b = matid(n);
    3359        3982 :     return FpM_gauss_gen(a,b,p);
    3360             :   case 2:
    3361       20389 :     if (b) b = ZM_to_F2m(b); else b = matid_F2m(n);
    3362       20389 :     return F2m_gauss_sp(a,b);
    3363             :   default:
    3364       41371 :     if (b) b = ZM_to_Flm(b, *pp); else b = matid_Flm(n);
    3365       41371 :     return Flm_gauss_sp(a,b, NULL, *pp);
    3366             :   }
    3367             : }
    3368             : GEN
    3369          35 : FpM_gauss(GEN a, GEN b, GEN p)
    3370             : {
    3371          35 :   pari_sp av = avma;
    3372             :   ulong pp;
    3373             :   GEN u;
    3374          35 :   if (lg(a) == 1 || lg(b)==1) return cgetg(1, t_MAT);
    3375          35 :   u = FpM_gauss_i(a, b, p, &pp);
    3376          35 :   if (!u) { avma = av; return NULL; }
    3377          28 :   switch(pp)
    3378             :   {
    3379          28 :   case 0: return gerepilecopy(av, u);
    3380           0 :   case 2:  u = F2m_to_ZM(u); break;
    3381           0 :   default: u = Flm_to_ZM(u); break;
    3382             :   }
    3383           0 :   return gerepileupto(av, u);
    3384             : }
    3385             : GEN
    3386       65693 : FpM_inv(GEN a, GEN p)
    3387             : {
    3388       65693 :   pari_sp av = avma;
    3389             :   ulong pp;
    3390             :   GEN u;
    3391       65693 :   if (lg(a) == 1) return cgetg(1, t_MAT);
    3392       65693 :   u = FpM_gauss_i(a, NULL, p, &pp);
    3393       65693 :   if (!u) { avma = av; return NULL; }
    3394       65679 :   switch(pp)
    3395             :   {
    3396        3926 :   case 0: return gerepilecopy(av, u);
    3397       20382 :   case 2:  u = F2m_to_ZM(u); break;
    3398       41371 :   default: u = Flm_to_ZM(u); break;
    3399             :   }
    3400       61753 :   return gerepileupto(av, u);
    3401             : }
    3402             : 
    3403             : GEN
    3404          14 : FpM_FpC_gauss(GEN a, GEN b, GEN p)
    3405             : {
    3406          14 :   pari_sp av = avma;
    3407             :   ulong pp;
    3408             :   GEN u;
    3409          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    3410          14 :   u = FpM_gauss_i(a, mkmat(b), p, &pp);
    3411          14 :   if (!u) { avma = av; return NULL; }
    3412          14 :   switch(pp)
    3413             :   {
    3414          14 :   case 0: return gerepilecopy(av, gel(u,1));
    3415           0 :   case 2:  u = F2c_to_ZC(gel(u,1)); break;
    3416           0 :   default: u = Flc_to_ZC(gel(u,1)); break;
    3417             :   }
    3418           0 :   return gerepileupto(av, u);
    3419             : }
    3420             : 
    3421             : static GEN
    3422          56 : FlxqM_gauss_gen(GEN a, GEN b, GEN T, ulong p)
    3423             : {
    3424             :   void *E;
    3425          56 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    3426          56 :   return gen_Gauss(a, b, E, S);
    3427             : }
    3428             : 
    3429             : static GEN
    3430          35 : FlxqM_gauss_CUP(GEN a, GEN b, GEN T, ulong p) {
    3431             :   GEN R, C, U, P, Y;
    3432          35 :   long n = lg(a) - 1, r;
    3433          35 :   if (nbrows(a) < n || (r = FlxqM_CUP(a, &R, &C, &U, &P, T, p)) < n)
    3434          14 :     return NULL;
    3435          21 :   Y = FlxqM_rsolve_lower_unit(rowpermute(C, R),
    3436             :                               rowpermute(b, R), T, p);
    3437          21 :   return rowpermute(FlxqM_rsolve_upper(U, Y, T, p),
    3438             :                     perm_inv(P));
    3439             : }
    3440             : 
    3441             : static GEN
    3442          91 : FlxqM_gauss_i(GEN a, GEN b, GEN T, ulong p) {
    3443          91 :   if (lg(a) - 1 >= FlxqM_CUP_LIMIT)
    3444          35 :     return FlxqM_gauss_CUP(a, b, T, p);
    3445          56 :   return FlxqM_gauss_gen(a, b, T, p);
    3446             : }
    3447             : 
    3448             : GEN
    3449          21 : FlxqM_gauss(GEN a, GEN b, GEN T, ulong p)
    3450             : {
    3451          21 :   pari_sp av = avma;
    3452          21 :   long n = lg(a)-1;
    3453             :   GEN u;
    3454          21 :   if (!n || lg(b)==1) { avma = av; return cgetg(1, t_MAT); }
    3455          21 :   u = FlxqM_gauss_i(a, b, T, p);
    3456          21 :   if (!u) { avma = av; return NULL; }
    3457          14 :   return gerepilecopy(av, u);
    3458             : }
    3459             : GEN
    3460          56 : FlxqM_inv(GEN a, GEN T, ulong p)
    3461             : {
    3462          56 :   pari_sp av = avma;
    3463             :   GEN u;
    3464          56 :   if (lg(a) == 1) { avma = av; return cgetg(1, t_MAT); }
    3465          56 :   u = FlxqM_gauss_i(a, matid_FlxqM(nbrows(a),T,p), T,p);
    3466          56 :   if (!u) { avma = av; return NULL; }
    3467          42 :   return gerepilecopy(av, u);
    3468             : }
    3469             : GEN
    3470          14 : FlxqM_FlxqC_gauss(GEN a, GEN b, GEN T, ulong p)
    3471             : {
    3472          14 :   pari_sp av = avma;
    3473             :   GEN u;
    3474          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    3475          14 :   u = FlxqM_gauss_i(a, mkmat(b), T, p);
    3476          14 :   if (!u) { avma = av; return NULL; }
    3477           7 :   return gerepilecopy(av, gel(u,1));
    3478             : }
    3479             : 
    3480             : static GEN
    3481         133 : FqM_gauss_gen(GEN a, GEN b, GEN T, GEN p)
    3482             : {
    3483             :   void *E;
    3484         133 :   const struct bb_field *S = get_Fq_field(&E,T,p);
    3485         133 :   return gen_Gauss(a,b,E,S);
    3486             : }
    3487             : GEN
    3488          21 : FqM_gauss(GEN a, GEN b, GEN T, GEN p)
    3489             : {
    3490          21 :   pari_sp av = avma;
    3491             :   GEN u;
    3492             :   long n;
    3493          21 :   if (!T) return FpM_gauss(a,b,p);
    3494          21 :   n = lg(a)-1; if (!n || lg(b)==1) return cgetg(1, t_MAT);
    3495          21 :   u = FqM_gauss_gen(a,b,T,p);
    3496          21 :   if (!u) { avma = av; return NULL; }
    3497          14 :   return gerepilecopy(av, u);
    3498             : }
    3499             : GEN
    3500          98 : FqM_inv(GEN a, GEN T, GEN p)
    3501             : {
    3502          98 :   pari_sp av = avma;
    3503             :   GEN u;
    3504          98 :   if (!T) return FpM_inv(a,p);
    3505          98 :   if (lg(a) == 1) return cgetg(1, t_MAT);
    3506          98 :   u = FqM_gauss_gen(a,matid(nbrows(a)),T,p);
    3507          98 :   if (!u) { avma = av; return NULL; }
    3508          70 :   return gerepilecopy(av, u);
    3509             : }
    3510             : GEN
    3511          14 : FqM_FqC_gauss(GEN a, GEN b, GEN T, GEN p)
    3512             : {
    3513          14 :   pari_sp av = avma;
    3514             :   GEN u;
    3515          14 :   if (!T) return FpM_FpC_gauss(a,b,p);
    3516          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    3517          14 :   u = FqM_gauss_gen(a,mkmat(b),T,p);
    3518          14 :   if (!u) { avma = av; return NULL; }
    3519           7 :   return gerepilecopy(av, gel(u,1));
    3520             : }
    3521             : 
    3522             : static GEN
    3523      581852 : ZlM_gauss_ratlift(GEN a, GEN b, ulong p, long e, GEN C)
    3524             : {
    3525      581852 :   pari_sp av = avma, av2;
    3526             :   GEN bb, xi, xb, pi, P, B, r;
    3527      581852 :   long i, k = 2;
    3528      581852 :   if (!C) {
    3529           0 :     C = Flm_inv(ZM_to_Flm(a, p), p);
    3530           0 :     if (!C) pari_err_INV("ZlM_gauss", a);
    3531             :   }
    3532      581852 :   pi = P = utoipos(p);
    3533      581852 :   av2 = avma;
    3534      581852 :   xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    3535      581852 :   xb = Flm_to_ZM(xi);
    3536      581852 :   bb = b;
    3537     1194456 :   for (i = 2; i <= e; i++)
    3538             :   {
    3539      671348 :     bb = ZM_Z_divexact(ZM_sub(bb, ZM_nm_mul(a, xi)), P);
    3540      671348 :     if (gc_needed(av,2))
    3541             :     {
    3542          68 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZlM_gauss. i=%ld/%ld",i,e);
    3543          68 :       gerepileall(av2,3, &pi,&bb,&xb);
    3544             :     }
    3545      671348 :     xi = Flm_mul(C, ZM_to_Flm(bb, p), p);
    3546      671348 :     xb = ZM_add(xb, nm_Z_mul(xi, pi));
    3547      671348 :     pi = muliu(pi, p); /* = p^(i-1) */
    3548      671348 :     if (i==k && i < e)
    3549             :     {
    3550      197614 :       k *= 2;
    3551      197614 :       B = sqrti(shifti(pi,-1));
    3552      197614 :       r = FpM_ratlift(xb, pi, B, B, NULL);
    3553      197614 :       if (r)
    3554             :       {
    3555       98710 :         GEN dr, nr = Q_remove_denom(r,&dr);
    3556       98710 :         if (ZM_equal(ZM_mul(a,nr), dr? ZM_Z_mul(b,dr): b))
    3557             :         {
    3558       58744 :           if (DEBUGLEVEL>=4)
    3559           0 :             err_printf("ZlM_gauss: early solution: %ld/%ld\n",i,e);
    3560       58744 :           return gerepilecopy(av, r);
    3561             :         }
    3562             :       }
    3563             :     }
    3564             :   }
    3565      523108 :   B = sqrti(shifti(pi,-1));
    3566      523108 :   return gerepileupto(av, FpM_ratlift(xb, pi, B, B, NULL));
    3567             : }
    3568             : 
    3569             : /* Dixon p-adic lifting algorithm.
    3570             :  * Numer. Math. 40, 137-141 (1982), DOI: 10.1007/BF01459082 */
    3571             : GEN
    3572      581950 : ZM_gauss(GEN a, GEN b0)
    3573             : {
    3574      581950 :   pari_sp av = avma, av2;
    3575             :   int iscol;
    3576             :   long n, ncol, i, m, elim;
    3577             :   ulong p;
    3578      581950 :   GEN C, delta, nb, nmin, res, b = b0;
    3579             :   forprime_t S;
    3580             : 
    3581      581950 :   if (!init_gauss(a, &b, &n, &ncol, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    3582      581880 :   nb = gen_0; ncol = lg(b);
    3583     1375760 :   for (i = 1; i < ncol; i++)
    3584             :   {
    3585      793880 :     GEN ni = gnorml2(gel(b, i));
    3586      793880 :     if (cmpii(nb, ni) < 0) nb = ni;
    3587             :   }
    3588      581880 :   if (!signe(nb)) { avma = av; return gcopy(b0); }
    3589      581880 :   delta = gen_1; nmin = nb;
    3590     2345919 :   for (i = 1; i <= n; i++)
    3591             :   {
    3592     1764039 :     GEN ni = gnorml2(gel(a, i));
    3593     1764039 :     if (cmpii(ni, nmin) < 0)
    3594             :     {
    3595       76100 :       delta = mulii(delta, nmin); nmin = ni;
    3596             :     }
    3597             :     else
    3598     1687939 :       delta = mulii(delta, ni);
    3599             :   }
    3600      581880 :   if (!signe(nmin)) return NULL;
    3601      581859 :   elim = expi(delta)+1;
    3602      581859 :   av2 = avma;
    3603      581859 :   init_modular_big(&S);
    3604             :   for(;;)
    3605             :   {
    3606      581859 :     p = u_forprime_next(&S);
    3607      581859 :     C = Flm_inv_sp(ZM_to_Flm(a, p), NULL, p);
    3608      581859 :     if (C) break;
    3609           7 :     elim -= expu(p);
    3610           7 :     if (elim < 0) return NULL;
    3611           0 :     avma = av2;
    3612             :   }
    3613             :   /* N.B. Our delta/lambda are SQUARES of those in the paper
    3614             :    * log(delta lambda) / log p, where lambda is 3+sqrt(5) / 2,
    3615             :    * whose log is < 1, hence + 1 (to cater for rounding errors) */
    3616     1163704 :   m = (long)ceil((rtodbl(logr_abs(itor(delta,LOWDEFAULTPREC))) + 1)
    3617      581852 :                  / log((double)p));
    3618      581852 :   res = ZlM_gauss_ratlift(a, b, p, m, C);
    3619      581852 :   if (iscol) return gerepilecopy(av, gel(res, 1));
    3620       83000 :   return gerepileupto(av, res);
    3621             : }
    3622             : 
    3623             : /* same as above, M rational */
    3624             : GEN
    3625         854 : QM_gauss(GEN M, GEN B)
    3626             : {
    3627         854 :   pari_sp av = avma;
    3628             :   GEN K, MB;
    3629         854 :   MB = Q_primitive_part(mkvec2(M,B), NULL);
    3630         854 :   K = ZM_gauss(gel(MB,1), gel(MB,2));
    3631         854 :   return gerepileupto(av, K);
    3632             : }
    3633             : 
    3634             : static GEN
    3635      131322 : ZM_inv_slice(GEN A, GEN P, GEN *mod)
    3636             : {
    3637      131322 :   pari_sp av = avma;
    3638      131322 :   long i, n = lg(P)-1;
    3639             :   GEN H, T;
    3640      131322 :   if (n == 1)
    3641             :   {
    3642      130565 :     ulong p = uel(P,1);
    3643      130565 :     GEN Hp, a = ZM_to_Flm(A, p);
    3644      130564 :     Hp = Flm_adjoint(a, p);
    3645      130564 :     Hp = gerepileupto(av, Flm_to_ZM(Hp));
    3646      130565 :     *mod = utoi(p); return Hp;
    3647             :   }
    3648         757 :   T = ZV_producttree(P);
    3649         757 :   A = ZM_nv_mod_tree(A, P, T);
    3650         757 :   H = cgetg(n+1, t_VEC);
    3651        2748 :   for(i=1; i <= n; i++)
    3652        1991 :     gel(H,i) = Flm_adjoint(gel(A, i), uel(P,i));
    3653         757 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    3654         757 :   *mod = gmael(T, lg(T)-1, 1);
    3655         757 :   gerepileall(av, 2, &H, mod);
    3656         757 :   return H;
    3657             : }
    3658             : 
    3659             : static GEN
    3660      112239 : RgM_true_Hadamard(GEN a)
    3661             : {
    3662      112239 :   pari_sp av = avma;
    3663      112239 :   long n = lg(a)-1, i;
    3664             :   GEN B;
    3665      112239 :   if (n == 0) return gen_1;
    3666      112239 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    3667      112239 :   B = gnorml2(gel(a,1));
    3668      112239 :   for (i = 2; i <= n; i++) B = gmul(B, gnorml2(gel(a,i)));
    3669      112239 :   return gerepileuptoint(av, ceil_safe(sqrtr(B)));
    3670             : }
    3671             : 
    3672             : GEN
    3673      131322 : ZM_inv_worker(GEN P, GEN A)
    3674             : {
    3675      131322 :   GEN V = cgetg(3, t_VEC);
    3676      131322 :   gel(V,1) = ZM_inv_slice(A, P, &gel(V,2));
    3677      131320 :   return V;
    3678             : }
    3679             : 
    3680             : static GEN
    3681        4312 : ZM_inv0(GEN A, GEN *pden)
    3682             : {
    3683        4312 :   if (pden) *pden = gen_1;
    3684        4312 :   (void)A; return cgetg(1, t_MAT);
    3685             : }
    3686             : static GEN
    3687       16972 : ZM_inv1(GEN A, GEN *pden)
    3688             : {
    3689       16972 :   GEN a = gcoeff(A,1,1);
    3690       16972 :   long s = signe(a);
    3691       16972 :   if (!s) return NULL;
    3692       16972 :   if (pden) *pden = absi(a);
    3693       16972 :   retmkmat(mkcol(s == 1? gen_1: gen_m1));
    3694             : }
    3695             : static GEN
    3696       38615 : ZM_inv2(GEN A, GEN *pden)
    3697             : {
    3698             :   GEN a, b, c, d, D, cA;
    3699             :   long s;
    3700       38615 :   A = Q_primitive_part(A, &cA);
    3701       38615 :   a = gcoeff(A,1,1); b = gcoeff(A,1,2);
    3702       38615 :   c = gcoeff(A,2,1); d = gcoeff(A,2,2);
    3703       38615 :   D = subii(mulii(a,d), mulii(b,c)); /* left on stack */
    3704       38615 :   s = signe(D);
    3705       38615 :   if (!s) return NULL;
    3706       38615 :   if (s < 0) D = negi(D);
    3707       38615 :   if (pden) *pden = mul_denom(D, cA);
    3708       38615 :   if (s > 0)
    3709       19435 :     retmkmat2(mkcol2(icopy(d), negi(c)), mkcol2(negi(b), icopy(a)));
    3710             :   else
    3711       19180 :     retmkmat2(mkcol2(negi(d), icopy(c)), mkcol2(icopy(b), negi(a)));
    3712             : }
    3713             : 
    3714             : /* to be used when denom(M^(-1)) << det(M) and a sharp multiple is
    3715             :  * not available. Return H primitive such that M*H = den*Id */
    3716             : GEN
    3717           0 : ZM_inv_ratlift(GEN M, GEN *pden)
    3718             : {
    3719           0 :   pari_sp av2, av = avma;
    3720             :   GEN Hp, q, H;
    3721             :   ulong p;
    3722           0 :   long m = lg(M)-1;
    3723             :   forprime_t S;
    3724             :   pari_timer ti;
    3725             : 
    3726           0 :   if (m == 0) return ZM_inv0(M,pden);
    3727           0 :   if (m == 1 && nbrows(M)==1) return ZM_inv1(M,pden);
    3728           0 :   if (m == 2 && nbrows(M)==2) return ZM_inv2(M,pden);
    3729             : 
    3730           0 :   if (DEBUGLEVEL>5) timer_start(&ti);
    3731           0 :   init_modular_big(&S);
    3732           0 :   av2 = avma;
    3733           0 :   H = NULL;
    3734           0 :   while ((p = u_forprime_next(&S)))
    3735             :   {
    3736             :     GEN Mp, B, Hr;
    3737           0 :     Mp = ZM_to_Flm(M,p);
    3738           0 :     Hp = Flm_inv_sp(Mp, NULL, p);
    3739           0 :     if (!Hp) continue;
    3740           0 :     if (!H)
    3741             :     {
    3742           0 :       H = ZM_init_CRT(Hp, p);
    3743           0 :       q = utoipos(p);
    3744             :     }
    3745             :     else
    3746           0 :       ZM_incremental_CRT(&H, Hp, &q, p);
    3747           0 :     B = sqrti(shifti(q,-1));
    3748           0 :     Hr = FpM_ratlift(H,q,B,B,NULL);
    3749           0 :     if (DEBUGLEVEL>5)
    3750           0 :       timer_printf(&ti,"ZM_inv mod %lu (ratlift=%ld)", p,!!Hr);
    3751           0 :     if (Hr) {/* DONE ? */
    3752           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    3753           0 :       if (ZM_isscalar(ZM_mul(Hl, M), *pden)) { H = Hl; break; }
    3754             :     }
    3755             : 
    3756           0 :     if (gc_needed(av,2))
    3757             :     {
    3758           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_inv_ratlift");
    3759           0 :       gerepileall(av2, 2, &H, &q);
    3760             :     }
    3761             :   }
    3762           0 :   if (!*pden) *pden = gen_1;
    3763           0 :   gerepileall(av, 2, &H, pden);
    3764           0 :   return H;
    3765             : }
    3766             : 
    3767             : static GEN
    3768      119568 : ZM_adj_ratlift(GEN A, GEN H, GEN mod)
    3769             : {
    3770             :   GEN B;
    3771      119568 :   GEN D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3772      119568 :   GEN g = gcdii(D, mod);
    3773      119568 :   if (!equali1(g))
    3774             :   {
    3775          14 :     mod = diviiexact(mod, g);
    3776          14 :     H = FpM_red(H, mod);
    3777             :   }
    3778      119568 :   D = Fp_inv(Fp_red(D, mod), mod);
    3779      119568 :   H = FpM_Fp_mul(H, D, mod);
    3780      119568 :   B = sqrti(shifti(mod,-1));
    3781      119568 :   return FpM_ratlift(H, mod, B, B, NULL);
    3782             : }
    3783             : 
    3784             : GEN
    3785      172145 : ZM_inv(GEN A, GEN *pden)
    3786             : {
    3787      172145 :   pari_sp av = avma;
    3788      172145 :   long m = lg(A)-1, n, k1 = 1, k2;
    3789      172145 :   GEN H = NULL, D, H1 = NULL, mod1 = NULL, worker;
    3790      172145 :   ulong bnd, mask, p = 0;
    3791             :   pari_timer ti;
    3792             : 
    3793      172145 :   if (m == 0) return ZM_inv0(A,pden);
    3794      167833 :   if (pden) *pden = gen_1;
    3795      167833 :   if (nbrows(A) < m) return NULL;
    3796      167826 :   if (m == 1 && nbrows(A)==1) return ZM_inv1(A,pden);
    3797      150854 :   if (m == 2 && nbrows(A)==2) return ZM_inv2(A,pden);
    3798             : 
    3799      112239 :   if (DEBUGLEVEL>=5) timer_start(&ti);
    3800      112239 :   bnd = expi(RgM_true_Hadamard(A));
    3801      112239 :   worker = strtoclosure("_ZM_inv_worker", 1, A);
    3802      112239 :   gen_inccrt("ZM_inv_r", worker, NULL, k1, m, &p, &H1, &mod1, nmV_chinese_center, FpM_center);
    3803      112239 :   n = (bnd+1)/expu(p)+1;
    3804      112239 :   if (DEBUGLEVEL>=5) timer_printf(&ti,"inv (%ld/%ld primes)", k1, n);
    3805      112239 :   mask = quadratic_prec_mask(n);
    3806      112239 :   for (k2 = 0;;)
    3807       16263 :   {
    3808             :     GEN Hr;
    3809      128502 :     if (k2 > 0)
    3810             :     {
    3811       13860 :       gen_inccrt("ZM_inv_r", worker, NULL, k2, m, &p, &H1, &mod1,nmV_chinese_center,FpM_center);
    3812       13860 :       k1 += k2;
    3813       13860 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"CRT (%ld/%ld primes)", k1, n);
    3814             :     }
    3815      128502 :     if (mask == 1) break;
    3816      119568 :     k2 = (mask&1UL) ? k1-1: k1;
    3817      119568 :     mask >>= 1;
    3818             : 
    3819      119568 :     Hr = ZM_adj_ratlift(A, H1, mod1);
    3820      119568 :     if (DEBUGLEVEL>=5) timer_printf(&ti,"ratlift (%ld/%ld primes)", k1, n);
    3821      119568 :     if (Hr) {/* DONE ? */
    3822             :       GEN den;
    3823      105047 :       GEN Hl = Q_remove_denom(Hr, &den);
    3824      105047 :       GEN R = ZM_mul(Hl, A);
    3825      105047 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"mult (%ld/%ld primes)", k1, n);
    3826      105047 :       den = den ? den: gen_1;
    3827      105047 :       if (den)
    3828             :       {
    3829      105047 :         if (ZM_isscalar(R, den))
    3830             :         {
    3831      103305 :           H = Hl;
    3832      103305 :           if (pden) *pden = den;
    3833      206610 :           break;
    3834             :         }
    3835             :       }
    3836             :       else
    3837           0 :         if (ZM_isidentity(R)) { H=Hl; break; }
    3838             :     }
    3839             :   }
    3840      112239 :   if (!H)
    3841             :   {
    3842             :     GEN d;
    3843        8934 :     H = H1;
    3844        8934 :     D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3845        8934 :     if (signe(D)==0) pari_err_INV("ZM_inv", A);
    3846        8920 :     d = gcdii(Q_content_safe(H), D);
    3847        8920 :     if (signe(D) < 0) d = negi(d);
    3848        8920 :     if (!equali1(d))
    3849             :     {
    3850        5299 :       H = ZM_Z_divexact(H, d);
    3851        5299 :       D = diviiexact(D, d);
    3852             :     }
    3853        8920 :     if (pden) *pden = D;
    3854             :   }
    3855      112225 :   gerepileall(av, pden? 2: 1, &H, pden);
    3856      112225 :   return H;
    3857             : }
    3858             : 
    3859             : /* same as above, M rational */
    3860             : GEN
    3861        1694 : QM_inv(GEN M)
    3862             : {
    3863        1694 :   pari_sp av = avma;
    3864             :   GEN den, cM, K;
    3865        1694 :   M = Q_primitive_part(M, &cM);
    3866        1694 :   K = ZM_inv(M, &den);
    3867        1694 :   if (!K) { avma = av; return NULL; }
    3868        1694 :   cM = inv_content(mul_content(cM, den));
    3869        1694 :   if (cM) K = RgM_Rg_div(K, cM);
    3870        1694 :   return gerepileupto(av, K);
    3871             : }
    3872             : 
    3873             : static GEN
    3874       55574 : ZM_ker_i(GEN M, long fl)
    3875             : {
    3876       55574 :   pari_sp av2, av = avma;
    3877             :   GEN q, H, D;
    3878             :   forprime_t S;
    3879       55574 :   av2 = avma;
    3880       55574 :   H = NULL; D = NULL;
    3881       55574 :   if (lg(M)==1) return cgetg(1, t_MAT);
    3882       55567 :   init_modular_big(&S);
    3883             :   for(;;)
    3884       61993 :   {
    3885             :     GEN Kp, Hp, Dp, Mp, Hr, B;
    3886      117560 :     ulong p = u_forprime_next(&S);
    3887      117560 :     Mp = ZM_to_Flm(M, p);
    3888      117560 :     Kp = Flm_ker_sp(Mp, p, 2);
    3889      117560 :     Hp = gel(Kp,1); Dp = gel(Kp,2);
    3890      117560 :     if (H && (lg(Hp)>lg(H) || (lg(Hp)==lg(H) && vecsmall_lexcmp(Dp,D)>0))) continue;
    3891      109652 :     if (!H || (lg(Hp)<lg(H) || vecsmall_lexcmp(Dp,D)<0))
    3892             :     {
    3893       93247 :       H = ZM_init_CRT(Hp, p); D = Dp;
    3894       93247 :       q = utoipos(p);
    3895             :     }
    3896             :     else
    3897       16405 :       ZM_incremental_CRT(&H, Hp, &q, p);
    3898      109652 :     B = sqrti(shifti(q,-1));
    3899      109652 :     Hr = FpM_ratlift(H, q, B, B, NULL);
    3900      109652 :     if (DEBUGLEVEL>5) err_printf("ZM_ker mod %lu (ratlift=%ld)\n", p,!!Hr);
    3901      109652 :     if (Hr) {/* DONE ? */
    3902      104042 :       GEN MH = QM_mul(M, Hr);
    3903      104042 :       if (gequal0(MH)) { H = fl ? vec_Q_primpart(Hr): Hr;  break; }
    3904             :     }
    3905       54085 :     if (gc_needed(av,2))
    3906             :     {
    3907           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_ker");
    3908           0 :       gerepileall(av2, 3, &H, &D, &q);
    3909             :     }
    3910             :   }
    3911       55567 :   return gerepilecopy(av, H);
    3912             : }
    3913             : 
    3914             : GEN
    3915       48770 : ZM_ker(GEN M)
    3916       48770 : { return ZM_ker_i(M, 1); }
    3917             : 
    3918             : GEN
    3919        7637 : QM_ker(GEN M)
    3920             : {
    3921        7637 :   pari_sp av = avma;
    3922        7637 :   long l = lg(M)-1;
    3923        7637 :   if (l==0) return cgetg(1, t_MAT);
    3924        7602 :   if (lgcols(M)==1) return matid(l);
    3925        6741 :   M = shallowtrans(vec_Q_primpart(shallowtrans(M)));
    3926        6741 :   return gerepileupto(av, ZM_ker_i(M, 0));
    3927             : }
    3928             : 
    3929             : /* x a ZM. Return a multiple of the determinant of the lattice generated by
    3930             :  * the columns of x. From Algorithm 2.2.6 in GTM138 */
    3931             : GEN
    3932       47549 : detint(GEN A)
    3933             : {
    3934       47549 :   if (typ(A) != t_MAT) pari_err_TYPE("detint",A);
    3935       47549 :   RgM_check_ZM(A, "detint");
    3936       47549 :   return ZM_detmult(A);
    3937             : }
    3938             : GEN
    3939       77957 : ZM_detmult(GEN A)
    3940             : {
    3941       77957 :   pari_sp av1, av = avma;
    3942             :   GEN B, c, v, piv;
    3943       77957 :   long rg, i, j, k, m, n = lg(A) - 1;
    3944             : 
    3945       77957 :   if (!n) return gen_1;
    3946       77957 :   m = nbrows(A);
    3947       77957 :   if (n < m) return gen_0;
    3948       77936 :   c = zero_zv(m);
    3949       77936 :   av1 = avma;
    3950       77936 :   B = zeromatcopy(m,m);
    3951       77936 :   v = cgetg(m+1, t_COL);
    3952       77936 :   piv = gen_1; rg = 0;
    3953      479218 :   for (k=1; k<=n; k++)
    3954             :   {
    3955      479204 :     GEN pivprec = piv;
    3956      479204 :     long t = 0;
    3957     4023015 :     for (i=1; i<=m; i++)
    3958             :     {
    3959     3543811 :       pari_sp av2 = avma;
    3960             :       GEN vi;
    3961     3543811 :       if (c[i]) continue;
    3962             : 
    3963     2011756 :       vi = mulii(piv, gcoeff(A,i,k));
    3964    17750820 :       for (j=1; j<=m; j++)
    3965    15739064 :         if (c[j]) vi = addii(vi, mulii(gcoeff(B,j,i),gcoeff(A,j,k)));
    3966     2011756 :       if (!t && signe(vi)) t = i;
    3967     2011756 :       gel(v,i) = gerepileuptoint(av2, vi);
    3968             :     }
    3969      479204 :     if (!t) continue;
    3970             :     /* at this point c[t] = 0 */
    3971             : 
    3972      479120 :     if (++rg >= m) { /* full rank; mostly done */
    3973       77922 :       GEN det = gel(v,t); /* last on stack */
    3974       77922 :       if (++k > n)
    3975       77839 :         det = absi(det);
    3976             :       else
    3977             :       {
    3978             :         /* improve further; at this point c[i] is set for all i != t */
    3979          83 :         gcoeff(B,t,t) = piv; v = centermod(gel(B,t), det);
    3980         334 :         for ( ; k<=n; k++)
    3981         251 :           det = gcdii(det, ZV_dotproduct(v, gel(A,k)));
    3982             :       }
    3983       77922 :       return gerepileuptoint(av, det);
    3984             :     }
    3985             : 
    3986      401198 :     piv = gel(v,t);
    3987     3465406 :     for (i=1; i<=m; i++)
    3988             :     {
    3989             :       GEN mvi;
    3990     3064208 :       if (c[i] || i == t) continue;
    3991             : 
    3992     1532104 :       gcoeff(B,t,i) = mvi = negi(gel(v,i));
    3993    13723815 :       for (j=1; j<=m; j++)
    3994    12191711 :         if (c[j]) /* implies j != t */
    3995             :         {
    3996     3042501 :           pari_sp av2 = avma;
    3997     3042501 :           GEN z = addii(mulii(gcoeff(B,j,i), piv), mulii(gcoeff(B,j,t), mvi));
    3998     3042501 :           if (rg > 1) z = diviiexact(z, pivprec);
    3999     3042501 :           gcoeff(B,j,i) = gerepileuptoint(av2, z);
    4000             :         }
    4001             :     }
    4002      401198 :     c[t] = k;
    4003      401198 :     if (gc_needed(av,1))
    4004             :     {
    4005           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"detint. k=%ld",k);
    4006           0 :       gerepileall(av1, 2, &piv,&B); v = zerovec(m);
    4007             :     }
    4008             :   }
    4009          14 :   avma = av; return gen_0;
    4010             : }
    4011             : 
    4012             : /* Reduce x modulo (invertible) y */
    4013             : GEN
    4014       13293 : closemodinvertible(GEN x, GEN y)
    4015             : {
    4016       13293 :   return gmul(y, ground(RgM_solve(y,x)));
    4017             : }
    4018             : GEN
    4019           7 : reducemodinvertible(GEN x, GEN y)
    4020             : {
    4021           7 :   return gsub(x, closemodinvertible(x,y));
    4022             : }
    4023             : GEN
    4024           0 : reducemodlll(GEN x,GEN y)
    4025             : {
    4026           0 :   return reducemodinvertible(x, ZM_lll(y, 0.75, LLL_INPLACE));
    4027             : }
    4028             : 
    4029             : /*******************************************************************/
    4030             : /*                                                                 */
    4031             : /*                    KERNEL of an m x n matrix                    */
    4032             : /*          return n - rk(x) linearly independent vectors          */
    4033             : /*                                                                 */
    4034             : /*******************************************************************/
    4035             : static GEN
    4036          28 : RgM_deplin_i(GEN x0)
    4037             : {
    4038          28 :   pari_sp av = avma, av2;
    4039          28 :   long i, j, k, nl, nc = lg(x0)-1;
    4040             :   GEN D, x, y, c, l, d, ck;
    4041             : 
    4042          28 :   if (!nc) { avma=av; return NULL; }
    4043          28 :   nl = nbrows(x0);
    4044          28 :   c = zero_zv(nl);
    4045          28 :   l = cgetg(nc+1, t_VECSMALL); /* not initialized */
    4046          28 :   av2 = avma;
    4047          28 :   x = RgM_shallowcopy(x0);
    4048          28 :   d = const_vec(nl, gen_1); /* pivot list */
    4049          28 :   ck = NULL; /* gcc -Wall */
    4050          98 :   for (k=1; k<=nc; k++)
    4051             :   {
    4052          91 :     ck = gel(x,k);
    4053         196 :     for (j=1; j<k; j++)
    4054             :     {
    4055         105 :       GEN cj = gel(x,j), piv = gel(d,j), q = gel(ck,l[j]);
    4056         420 :       for (i=1; i<=nl; i++)
    4057         315 :         if (i!=l[j]) gel(ck,i) = gsub(gmul(piv, gel(ck,i)), gmul(q, gel(cj,i)));
    4058             :     }
    4059             : 
    4060          91 :     i = gauss_get_pivot_NZ(x, NULL, k, c);
    4061          91 :     if (i > nl) break;
    4062          70 :     if (gc_needed(av,1))
    4063             :     {
    4064           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"deplin k = %ld/%ld",k,nc);
    4065           0 :       gerepileall(av2, 2, &x, &d);
    4066           0 :       ck = gel(x,k);
    4067             :     }
    4068          70 :     gel(d,k) = gel(ck,i);
    4069          70 :     c[i] = k; l[k] = i; /* pivot d[k] in x[i,k] */
    4070             :   }
    4071          28 :   if (k > nc) { avma = av; return NULL; }
    4072          21 :   if (k == 1) { avma = av; return scalarcol_shallow(gen_1,nc); }
    4073          21 :   y = cgetg(nc+1,t_COL);
    4074          21 :   gel(y,1) = gcopy(gel(ck, l[1]));
    4075          49 :   for (D=gel(d,1),j=2; j<k; j++)
    4076             :   {
    4077          28 :     gel(y,j) = gmul(gel(ck, l[j]), D);
    4078          28 :     D = gmul(D, gel(d,j));
    4079             :   }
    4080          21 :   gel(y,j) = gneg(D);
    4081          21 :   for (j++; j<=nc; j++) gel(y,j) = gen_0;
    4082          21 :   y = primitive_part(y, &c);
    4083          21 :   return c? gerepileupto(av, y): gerepilecopy(av, y);
    4084             : }
    4085             : static GEN
    4086           0 : RgV_deplin(GEN v)
    4087             : {
    4088           0 :   pari_sp av = avma;
    4089           0 :   long n = lg(v)-1;
    4090           0 :   GEN y, p = NULL;
    4091           0 :   if (n <= 1)
    4092             :   {
    4093           0 :     if (n == 1 && gequal0(gel(v,1))) return mkcol(gen_1);
    4094           0 :     return cgetg(1, t_COL);
    4095             :   }
    4096           0 :   if (gequal0(gel(v,1))) return scalarcol_shallow(gen_1, n);
    4097           0 :   v = primpart(mkvec2(gel(v,1),gel(v,2)));
    4098           0 :   if (RgV_is_FpV(v, &p) && p) v = centerlift(v);
    4099           0 :   y = zerocol(n);
    4100           0 :   gel(y,1) = gneg(gel(v,2));
    4101           0 :   gel(y,2) = gcopy(gel(v,1));
    4102           0 :   return gerepileupto(av, y);
    4103             : 
    4104             : }
    4105             : 
    4106             : static GEN
    4107         105 : RgM_deplin_FpM(GEN x, GEN p)
    4108             : {
    4109         105 :   pari_sp av = avma;
    4110             :   ulong pp;
    4111         105 :   x = RgM_Fp_init(x, p, &pp);
    4112         105 :   switch(pp)
    4113             :   {
    4114             :   case 0:
    4115          35 :     x = FpM_ker_gen(x,p,1);
    4116          35 :     if (!x) { avma = av; return NULL; }
    4117          21 :     x = FpC_center(x,p,shifti(p,-1));
    4118          21 :     break;
    4119             :   case 2:
    4120          14 :     x = F2m_ker_sp(x,1);
    4121          14 :     if (!x) { avma = av; return NULL; }
    4122           7 :     x = F2c_to_ZC(x); break;
    4123             :   default:
    4124          56 :     x = Flm_ker_sp(x,pp,1);
    4125          56 :     if (!x) { avma = av; return NULL; }
    4126          35 :     x = Flv_center(x, pp, pp>>1);
    4127          35 :     x = zc_to_ZC(x);
    4128          35 :     break;
    4129             :   }
    4130          63 :   return gerepileupto(av, x);
    4131             : }
    4132             : 
    4133             : /* FIXME: implement direct modular ZM_deplin ? */
    4134             : static GEN
    4135          98 : QM_deplin(GEN M)
    4136             : {
    4137          98 :   pari_sp av = avma;
    4138          98 :   long l = lg(M)-1;
    4139             :   GEN k;
    4140          98 :   if (l==0) return NULL;
    4141          63 :   if (lgcols(M)==1) return col_ei(l, 1);
    4142          63 :   M = shallowtrans(vec_Q_primpart(shallowtrans(M)));
    4143          63 :   k = ZM_ker_i(M, 1);
    4144          63 :   if (lg(k)== 1) { avma = av; return NULL; }
    4145          49 :   return gerepilecopy(av, gel(k,1));
    4146             : }
    4147             : 
    4148             : static GEN
    4149          42 : RgM_deplin_FqM(GEN x, GEN pol, GEN p)
    4150             : {
    4151          42 :   pari_sp av = avma;
    4152          42 :   GEN b, T = RgX_to_FpX(pol, p);
    4153          42 :   if (signe(T) == 0) pari_err_OP("deplin",x,pol);
    4154          42 :   b = FqM_deplin(RgM_to_FqM(x, T, p), T, p);
    4155          42 :   return gerepileupto(av, b);
    4156             : }
    4157             : 
    4158             : #define code(t1,t2) ((t1 << 6) | t2)
    4159             : static GEN
    4160         357 : RgM_deplin_fast(GEN x)
    4161             : {
    4162             :   GEN p, pol;
    4163             :   long pa;
    4164         357 :   long t = RgM_type(x, &p,&pol,&pa);
    4165         357 :   switch(t)
    4166             :   {
    4167             :     case t_INT:    /* fall through */
    4168          98 :     case t_FRAC:   return QM_deplin(x);
    4169          84 :     case t_FFELT:  return FFM_deplin(x, pol);
    4170         105 :     case t_INTMOD: return RgM_deplin_FpM(x, p);
    4171             :     case code(t_POLMOD, t_INTMOD):
    4172          42 :                    return RgM_deplin_FqM(x, pol, p);
    4173          28 :     default:       return gen_0;
    4174             :   }
    4175             : }
    4176             : #undef code
    4177             : 
    4178             : static GEN
    4179         357 : RgM_deplin(GEN x)
    4180             : {
    4181         357 :   GEN z = RgM_deplin_fast(x);
    4182         357 :   if (z!= gen_0) return z;
    4183          28 :   return RgM_deplin_i(x);
    4184             : }
    4185             : 
    4186             : GEN
    4187         357 : deplin(GEN x)
    4188             : {
    4189         357 :   switch(typ(x))
    4190             :   {
    4191             :     case t_MAT:
    4192             :     {
    4193         357 :       GEN z = RgM_deplin(x);
    4194         357 :       if (z) return z;
    4195         140 :       return cgetg(1, t_COL);
    4196             :     }
    4197           0 :     case t_VEC: return RgV_deplin(x);
    4198           0 :     default: pari_err_TYPE("deplin",x);
    4199             :   }
    4200             :   return NULL;/*LCOV_EXCL_LINE*/
    4201             : }
    4202             : 
    4203             : /*******************************************************************/
    4204             : /*                                                                 */
    4205             : /*         GAUSS REDUCTION OF MATRICES  (m lines x n cols)         */
    4206             : /*           (kernel, image, complementary image, rank)            */
    4207             : /*                                                                 */
    4208             : /*******************************************************************/
    4209             : /* return the transform of x under a standard Gauss pivot.
    4210             :  * x0 is a reference point when guessing whether x[i,j] ~ 0
    4211             :  * (iff x[i,j] << x0[i,j])
    4212             :  * Set r = dim ker(x). d[k] contains the index of the first non-zero pivot
    4213             :  * in column k */
    4214             : static GEN
    4215         952 : gauss_pivot_ker(GEN x, GEN x0, GEN *dd, long *rr)
    4216             : {
    4217             :   GEN c, d, p, data;
    4218             :   pari_sp av;
    4219             :   long i, j, k, r, t, n, m;
    4220             :   pivot_fun pivot;
    4221             : 
    4222         952 :   n=lg(x)-1; if (!n) { *dd=NULL; *rr=0; return cgetg(1,t_MAT); }
    4223         952 :   m=nbrows(x); r=0;
    4224         952 :   pivot = get_pivot_fun(x, x0, &data);
    4225         952 :   x = RgM_shallowcopy(x);
    4226         952 :   c = zero_zv(m);
    4227         952 :   d = cgetg(n+1,t_VECSMALL);
    4228         952 :   av=avma;
    4229        5558 :   for (k=1; k<=n; k++)
    4230             :   {
    4231        4606 :     j = pivot(x, data, k, c);
    4232        4606 :     if (j > m)
    4233             :     {
    4234        1029 :       r++; d[k]=0;
    4235        4578 :       for(j=1; j<k; j++)
    4236        3549 :         if (d[j]) gcoeff(x,d[j],k) = gclone(gcoeff(x,d[j],k));
    4237             :     }
    4238             :     else
    4239             :     { /* pivot for column k on row j */
    4240        3577 :       c[j]=k; d[k]=j; p = gdiv(gen_m1,gcoeff(x,j,k));
    4241        3577 :       gcoeff(x,j,k) = gen_m1;
    4242             :       /* x[j,] /= - x[j,k] */
    4243        3577 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    4244       35476 :       for (t=1; t<=m; t++)
    4245       31899 :         if (t!=j)
    4246             :         { /* x[t,] -= 1 / x[j,k] x[j,] */
    4247       28322 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    4248      230636 :           for (i=k+1; i<=n; i++)
    4249      202314 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i),gmul(p,gcoeff(x,j,i)));
    4250       28322 :           if (gc_needed(av,1)) gerepile_gauss_ker(x,k,t,av);
    4251             :         }
    4252             :     }
    4253             :   }
    4254         952 :   *dd=d; *rr=r; return x;
    4255             : }
    4256             : 
    4257             : static GEN FpM_gauss_pivot(GEN x, GEN p, long *rr);
    4258             : static GEN FqM_gauss_pivot(GEN x, GEN T, GEN p, long *rr);
    4259             : static GEN F2m_gauss_pivot(GEN x, long *rr);
    4260             : 
    4261             : /* r = dim ker(x).
    4262             :  * Returns d:
    4263             :  *   d[k] != 0 contains the index of a non-zero pivot in column k
    4264             :  *   d[k] == 0 if column k is a linear combination of the (k-1) first ones */
    4265             : GEN
    4266       23916 : RgM_pivots(GEN x0, GEN data, long *rr, pivot_fun pivot)
    4267             : {
    4268             :   GEN x, c, d, p;
    4269       23916 :   long i, j, k, r, t, m, n = lg(x0)-1;
    4270             :   pari_sp av;
    4271             : 
    4272       23916 :   if (RgM_is_ZM(x0)) return ZM_pivots(x0, rr);
    4273       23818 :   if (!n) { *rr = 0; return NULL; }
    4274             : 
    4275       23818 :   d = cgetg(n+1, t_VECSMALL);
    4276       23818 :   x = RgM_shallowcopy(x0);
    4277       23818 :   m = nbrows(x); r = 0;
    4278       23818 :   c = zero_zv(m);
    4279       23818 :   av = avma;
    4280      908244 :   for (k=1; k<=n; k++)
    4281             :   {
    4282      884426 :     j = pivot(x, data, k, c);
    4283      884426 :     if (j > m) { r++; d[k] = 0; }
    4284             :     else
    4285             :     {
    4286       40953 :       c[j] = k; d[k] = j; p = gdiv(gen_m1, gcoeff(x,j,k));
    4287       40953 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    4288             : 
    4289      167898 :       for (t=1; t<=m; t++)
    4290      126945 :         if (!c[t]) /* no pivot on that line yet */
    4291             :         {
    4292       51820 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    4293     5334048 :           for (i=k+1; i<=n; i++)
    4294     5282228 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i), gmul(p, gcoeff(x,j,i)));
    4295       51820 :           if (gc_needed(av,1)) gerepile_gauss(x,k,t,av,j,c);
    4296             :         }
    4297       40953 :       for (i=k; i<=n; i++) gcoeff(x,j,i) = gen_0; /* dummy */
    4298             :     }
    4299             :   }
    4300       23818 :   *rr = r; avma = (pari_sp)d; return d;
    4301             : }
    4302             : 
    4303             : static long
    4304      106723 : ZM_count_0_cols(GEN M)
    4305             : {
    4306      106723 :   long i, l = lg(M), n = 0;
    4307      616980 :   for (i = 1; i < l; i++)
    4308      510257 :     if (ZV_equal0(gel(M,i))) n++;
    4309      106723 :   return n;
    4310             : }
    4311             : 
    4312             : static void indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol);
    4313             : /* As RgM_pivots, integer entries. Set *rr = dim Ker M0 */
    4314             : GEN
    4315      110636 : ZM_pivots(GEN M0, long *rr)
    4316             : {
    4317      110636 :   GEN d, dbest = NULL;
    4318             :   long m, n, i, imax, rmin, rbest, zc;
    4319      110636 :   int beenthere = 0;
    4320      110636 :   pari_sp av, av0 = avma;
    4321             :   forprime_t S;
    4322             : 
    4323      110636 :   rbest = n = lg(M0)-1;
    4324      110636 :   if (n == 0) { *rr = 0; return NULL; }
    4325      106723 :   zc = ZM_count_0_cols(M0);
    4326      106723 :   if (n == zc) { *rr = zc; return zero_zv(n); }
    4327             : 
    4328      106632 :   m = nbrows(M0);
    4329      106632 :   rmin = maxss(zc, n-m);
    4330      106632 :   init_modular_small(&S);
    4331      106632 :   imax = (n < (1<<4))? 1: (n>>3); /* heuristic */
    4332             : 
    4333             :   for(;;)
    4334           0 :   {
    4335             :     GEN row, col, M, KM, IM, RHS, X, cX;
    4336             :     long rk;
    4337      114915 :     for (av = avma, i = 0;; avma = av, i++)
    4338        8283 :     {
    4339      114915 :       ulong p = u_forprime_next(&S);
    4340             :       long rp;
    4341      114915 :       if (!p) pari_err_OVERFLOW("ZM_pivots [ran out of primes]");
    4342      114915 :       d = Flm_pivots(ZM_to_Flm(M0, p), p, &rp, 1);
    4343      114915 :       if (rp == rmin) { rbest = rp; goto END; } /* maximal rank, return */
    4344       14904 :       if (rp < rbest) { /* save best r so far */
    4345        6623 :         rbest = rp;
    4346        6623 :         if (dbest) gunclone(dbest);
    4347        6623 :         dbest = gclone(d);
    4348       13244 :         if (beenthere) break;
    4349             :       }
    4350       14904 :       if (!beenthere && i >= imax) break;
    4351             :     }
    4352        6621 :     beenthere = 1;
    4353             :     /* Dubious case: there is (probably) a non trivial kernel */
    4354        6621 :     indexrank_all(m,n, rbest, dbest, &row, &col);
    4355        6621 :     M = rowpermute(vecpermute(M0, col), row);
    4356        6621 :     rk = n - rbest; /* (probable) dimension of image */
    4357        6621 :     IM = vecslice(M,1,rk);
    4358        6621 :     KM = vecslice(M,rk+1, n);
    4359        6621 :     M = rowslice(IM, 1,rk); /* square maximal rank */
    4360        6621 :     X = ZM_gauss(M, rowslice(KM, 1,rk));
    4361        6621 :     X = Q_remove_denom(X, &cX);
    4362        6621 :     RHS = rowslice(KM,rk+1,m);
    4363        6621 :     if (cX) RHS = ZM_Z_mul(RHS, cX);
    4364        6621 :     if (ZM_equal(ZM_mul(rowslice(IM,rk+1,m), X), RHS))
    4365             :     {
    4366        6621 :       d = vecsmall_copy(dbest);
    4367        6621 :       goto END;
    4368             :     }
    4369           0 :     avma = av;
    4370             :   }
    4371             : END:
    4372      106632 :   *rr = rbest; if (dbest) gunclone(dbest);
    4373      106632 :   return gerepileuptoleaf(av0, d);
    4374             : }
    4375             : 
    4376             : /* set *pr = dim Ker x */
    4377             : static GEN
    4378       12740 : gauss_pivot(GEN x, long *pr) {
    4379             :   GEN data;
    4380       12740 :   pivot_fun pivot = get_pivot_fun(x, x, &data);
    4381       12740 :   return RgM_pivots(x, data, pr, pivot);
    4382             : }
    4383             : 
    4384             : /* compute ker(x), x0 is a reference point when guessing whether x[i,j] ~ 0
    4385             :  * (iff x[i,j] << x0[i,j]) */
    4386             : static GEN
    4387         952 : ker_aux(GEN x, GEN x0)
    4388             : {
    4389         952 :   pari_sp av = avma;
    4390             :   GEN d,y;
    4391             :   long i,j,k,r,n;
    4392             : 
    4393         952 :   x = gauss_pivot_ker(x,x0,&d,&r);
    4394         952 :   if (!r) { avma=av; return cgetg(1,t_MAT); }
    4395         931 :   n = lg(x)-1; y=cgetg(r+1,t_MAT);
    4396        1960 :   for (j=k=1; j<=r; j++,k++)
    4397             :   {
    4398        1029 :     GEN p = cgetg(n+1,t_COL);
    4399             : 
    4400        1029 :     gel(y,j) = p; while (d[k]) k++;
    4401        4578 :     for (i=1; i<k; i++)
    4402        3549 :       if (d[i])
    4403             :       {
    4404        3367 :         GEN p1=gcoeff(x,d[i],k);
    4405        3367 :         gel(p,i) = gcopy(p1); gunclone(p1);
    4406             :       }
    4407             :       else
    4408         182 :         gel(p,i) = gen_0;
    4409        1029 :     gel(p,k) = gen_1; for (i=k+1; i<=n; i++) gel(p,i) = gen_0;
    4410             :   }
    4411         931 :   return gerepileupto(av,y);
    4412             : }
    4413             : 
    4414             : static GEN
    4415          77 : RgM_ker_FpM(GEN x, GEN p)
    4416             : {
    4417          77 :   pari_sp av = avma;
    4418             :   ulong pp;
    4419          77 :   x = RgM_Fp_init(x, p, &pp);
    4420          77 :   switch(pp)
    4421             :   {
    4422          35 :     case 0: x = FpM_to_mod(FpM_ker_gen(x,p,0),p); break;
    4423           7 :     case 2: x = F2m_to_mod(F2m_ker_sp(x,0)); break;
    4424          35 :     default:x = Flm_to_mod(Flm_ker_sp(x,pp,0), pp); break;
    4425             :   }
    4426          77 :   return gerepileupto(av, x);
    4427             : }
    4428             : 
    4429             : static GEN
    4430          91 : RgM_ker_FqM(GEN x, GEN pol, GEN p)
    4431             : {
    4432          91 :   pari_sp av = avma;
    4433          91 :   GEN b, T = RgX_to_FpX(pol, p);
    4434          91 :   if (signe(T) == 0) pari_err_OP("ker",x,pol);
    4435          84 :   b = FqM_ker(RgM_to_FqM(x, T, p), T, p);
    4436          84 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    4437             : }
    4438             : 
    4439             : #define code(t1,t2) ((t1 << 6) | t2)
    4440             : static GEN
    4441        8638 : RgM_ker_fast(GEN x)
    4442             : {
    4443             :   GEN p, pol;
    4444             :   long pa;
    4445        8638 :   long t = RgM_type(x, &p,&pol,&pa);
    4446        8638 :   switch(t)
    4447             :   {
    4448             :     case t_INT:    /* fall through */
    4449        7637 :     case t_FRAC:   return QM_ker(x);
    4450          77 :     case t_FFELT:  return FFM_ker(x, pol);
    4451          77 :     case t_INTMOD: return RgM_ker_FpM(x, p);
    4452             :     case code(t_POLMOD, t_INTMOD):
    4453          91 :                    return RgM_ker_FqM(x, pol, p);
    4454         756 :     default:       return NULL;
    4455             :   }
    4456             : }
    4457             : #undef code
    4458             : 
    4459             : GEN
    4460        8638 : ker(GEN x)
    4461             : {
    4462        8638 :   GEN b = RgM_ker_fast(x);
    4463        8631 :   if (b) return b;
    4464         756 :   return ker_aux(x,x);
    4465             : }
    4466             : 
    4467             : GEN
    4468       46214 : matker0(GEN x,long flag)
    4469             : {
    4470       46214 :   if (typ(x)!=t_MAT) pari_err_TYPE("matker",x);
    4471       46214 :   if (!flag) return ker(x);
    4472       45934 :   RgM_check_ZM(x, "matker");
    4473       45934 :   return ZM_ker(x);
    4474             : }
    4475             : 
    4476             : static GEN
    4477          63 : RgM_image_FpM(GEN x, GEN p)
    4478             : {
    4479          63 :   pari_sp av = avma;
    4480             :   ulong pp;
    4481          63 :   x = RgM_Fp_init(x, p, &pp);
    4482          63 :   switch(pp)
    4483             :   {
    4484          28 :     case 0: x = FpM_to_mod(FpM_image(x,p),p); break;
    4485           7 :     case 2: x = F2m_to_mod(F2m_image(x)); break;
    4486          28 :     default:x = Flm_to_mod(Flm_image(x,pp), pp); break;
    4487             :   }
    4488          63 :   return gerepileupto(av, x);
    4489             : }
    4490             : 
    4491             : static GEN
    4492          35 : RgM_image_FqM(GEN x, GEN pol, GEN p)
    4493             : {
    4494          35 :   pari_sp av = avma;
    4495          35 :   GEN b, T = RgX_to_FpX(pol, p);
    4496          35 :   if (signe(T) == 0) pari_err_OP("image",x,pol);
    4497          28 :   b = FqM_image(RgM_to_FqM(x, T, p), T, p);
    4498          28 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    4499             : }
    4500             : 
    4501             : static GEN
    4502        1463 : QM_image(GEN A)
    4503             : {
    4504        1463 :   pari_sp av = avma;
    4505        1463 :   GEN M = vecpermute(A, ZM_indeximage(vec_Q_primpart(A)));
    4506        1463 :   return gerepilecopy(av, M);
    4507             : }
    4508             : 
    4509             : #define code(t1,t2) ((t1 << 6) | t2)
    4510             : static GEN
    4511        1624 : RgM_image_fast(GEN x)
    4512             : {
    4513             :   GEN p, pol;
    4514             :   long pa;
    4515        1624 :   long t = RgM_type(x, &p,&pol,&pa);
    4516        1624 :   switch(t)
    4517             :   {
    4518             :     case t_INT:    /* fall through */
    4519        1463 :     case t_FRAC:   return QM_image(x);
    4520          49 :     case t_FFELT:  return FFM_image(x, pol);
    4521          63 :     case t_INTMOD: return RgM_image_FpM(x, p);
    4522             :     case code(t_POLMOD, t_INTMOD):
    4523          35 :                    return RgM_image_FqM(x, pol, p);
    4524          14 :     default:       return NULL;
    4525             :   }
    4526             : }
    4527             : #undef code
    4528             : 
    4529             : GEN
    4530        1624 : image(GEN x)
    4531             : {
    4532             :   GEN d, M;
    4533             :   long r;
    4534             : 
    4535        1624 :   if (typ(x)!=t_MAT) pari_err_TYPE("matimage",x);
    4536        1624 :   M = RgM_image_fast(x);
    4537        1617 :   if (M) return M;
    4538          14 :   d = gauss_pivot(x,&r); /* d left on stack for efficiency */
    4539          14 :   return image_from_pivot(x,d,r);
    4540             : }
    4541             : 
    4542             : static GEN
    4543          84 : imagecompl_aux(GEN x, GEN(*PIVOT)(GEN,long*))
    4544             : {
    4545          84 :   pari_sp av = avma;
    4546             :   GEN d,y;
    4547             :   long j,i,r;
    4548             : 
    4549          84 :   if (typ(x)!=t_MAT) pari_err_TYPE("imagecompl",x);
    4550          84 :   (void)new_chunk(lg(x) * 4 + 1); /* HACK */
    4551          84 :   d = PIVOT(x,&r); /* if (!d) then r = 0 */
    4552          84 :   avma = av; y = cgetg(r+1,t_VECSMALL);
    4553         126 :   for (i=j=1; j<=r; i++)
    4554          42 :     if (!d[i]) y[j++] = i;
    4555          84 :   return y;
    4556             : }
    4557             : GEN
    4558          84 : imagecompl(GEN x) { return imagecompl_aux(x, &gauss_pivot); }
    4559             : GEN
    4560           0 : ZM_imagecompl(GEN x) { return imagecompl_aux(x, &ZM_pivots); }
    4561             : 
    4562             : GEN
    4563       42328 : FpM_FpC_invimage(GEN A, GEN y, GEN p)
    4564             : {
    4565       42328 :   pari_sp av = avma;
    4566       42328 :   long i, l = lg(A);
    4567             :   GEN M, x, t;
    4568             : 
    4569       42328 :   if (lgefint(p) == 3)
    4570             :   {
    4571       42321 :     ulong pp = p[2];
    4572       42321 :     A = ZM_to_Flm(A, pp);
    4573       42321 :     y = ZV_to_Flv(y, pp);
    4574       42321 :     x = Flm_Flc_invimage(A,y,pp);
    4575       42321 :     if (!x) { avma = av; return NULL; }
    4576       42321 :     return gerepileupto(av, Flc_to_ZC(x));
    4577             :   }
    4578           7 :   if (l==1) return NULL;
    4579           7 :   if (lg(y) != lgcols(A)) pari_err_DIM("FpM_FpC_invimage");
    4580           7 :   M = FpM_ker(shallowconcat(A,y),p);
    4581           7 :   i = lg(M)-1; if (!i) { avma = av; return NULL; }
    4582             : 
    4583           7 :   x = gel(M,i); t = gel(x,l);
    4584           7 :   if (!signe(t)) { avma = av; return NULL; }
    4585             : 
    4586           7 :   setlg(x,l); t = Fp_inv(negi(t),p);
    4587           7 :   if (is_pm1(t)) return gerepilecopy(av, x);
    4588           7 :   return gerepileupto(av, FpC_Fp_mul(x, t, p));
    4589             : }
    4590             : GEN
    4591       51463 : Flm_Flc_invimage(GEN A, GEN y, ulong p)
    4592             : {
    4593       51463 :   pari_sp av = avma;
    4594       51463 :   long i, l = lg(A);
    4595             :   GEN M, x;
    4596             :   ulong t;
    4597             : 
    4598       51463 :   if (l==1) return NULL;
    4599       51463 :   if (lg(y) != lgcols(A)) pari_err_DIM("Flm_Flc_invimage");
    4600       51463 :   M = cgetg(l+1,t_MAT);
    4601       51463 :   for (i=1; i<l; i++) gel(M,i) = gel(A,i);
    4602       51463 :   gel(M,l) = y; M = Flm_ker(M,p);
    4603       51463 :   i = lg(M)-1; if (!i) { avma = av; return NULL; }
    4604             : 
    4605       51463 :   x = gel(M,i); t = x[l];
    4606       51463 :   if (!t) { avma = av; return NULL; }
    4607             : 
    4608       51463 :   setlg(x,l); t = Fl_inv(Fl_neg(t,p),p);
    4609       51463 :   if (t!=1) x = Flv_Fl_mul(x, t, p);
    4610       51463 :   return gerepileuptoleaf(av, x);
    4611             : }
    4612             : GEN
    4613          35 : F2m_F2c_invimage(GEN A, GEN y)
    4614             : {
    4615          35 :   pari_sp av = avma;
    4616          35 :   long i, l = lg(A);
    4617             :   GEN M, x;
    4618             : 
    4619          35 :   if (l==1) return NULL;
    4620          35 :   if (lg(y) != lgcols(A)) pari_err_DIM("F2m_F2c_invimage");
    4621          35 :   M = cgetg(l+1,t_MAT);
    4622          35 :   for (i=1; i<l; i++) gel(M,i) = gel(A,i);
    4623          35 :   gel(M,l) = y; M = F2m_ker(M);
    4624          35 :   i = lg(M)-1; if (!i) { avma = av; return NULL; }
    4625             : 
    4626          35 :   x = gel(M,i);
    4627          35 :   if (!F2v_coeff(x,l)) { avma = av; return NULL; }
    4628          35 :   F2v_clear(x, x[1]); x[1]--; /* remove last coord */
    4629          35 :   return gerepileuptoleaf(av, x);
    4630             : }
    4631             : 
    4632             : static GEN
    4633          28 : RgM_RgC_invimage_FpC(GEN A, GEN y, GEN p)
    4634             : {
    4635          28 :   pari_sp av = avma;
    4636             :   ulong pp;
    4637             :   GEN x;
    4638          28 :   A = RgM_Fp_init(A,p,&pp);
    4639          28 :   switch(pp)
    4640             :   {
    4641             :   case 0:
    4642           7 :     y = RgC_to_FpC(y,p);
    4643           7 :     x = FpM_FpC_invimage(A, y, p);
    4644           7 :     return x ? gerepileupto(av, FpC_to_mod(x,p)): NULL;
    4645             :   case 2:
    4646           7 :     y = RgV_to_F2v(y);
    4647           7 :     x = F2m_F2c_invimage(A, y);
    4648           7 :     return x ? gerepileupto(av, F2c_to_mod(x)): NULL;
    4649             :   default:
    4650          14 :     y = RgV_to_Flv(y,pp);
    4651          14 :     x = Flm_Flc_invimage(A, y, pp);
    4652          14 :     return x ? gerepileupto(av, Flc_to_mod(x,pp)): NULL;
    4653             :   }
    4654             : }
    4655             : 
    4656             : static GEN
    4657        2051 : RgM_RgC_invimage_fast(GEN x, GEN y)
    4658             : {
    4659             :   GEN p, pol;
    4660             :   long pa;
    4661        2051 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    4662        2051 :   switch(t)
    4663             :   {
    4664          28 :     case t_INTMOD: return RgM_RgC_invimage_FpC(x, y, p);
    4665          63 :     case t_FFELT:  return FFM_FFC_invimage(x, y, pol);
    4666        1960 :     default:       return gen_0;
    4667             :   }
    4668             : }
    4669             : 
    4670             : GEN
    4671        2156 : RgM_RgC_invimage(GEN A, GEN y)
    4672             : {
    4673        2156 :   pari_sp av = avma;
    4674        2156 :   long i, l = lg(A);
    4675             :   GEN M, x, t;
    4676        2156 :   if (l==1) return NULL;
    4677        2051 :   if (lg(y) != lgcols(A)) pari_err_DIM("inverseimage");
    4678        2051 :   M = RgM_RgC_invimage_fast(A, y);
    4679        2051 :   if (!M) {avma = av; return NULL; }
    4680        2030 :   if (M != gen_0) return M;
    4681        1960 :   M = ker(shallowconcat(A, y));
    4682        1960 :   i = lg(M)-1;
    4683        1960 :   if (!i) { avma = av; return NULL; }
    4684             : 
    4685        1701 :   x = gel(M,i); t = gel(x,l);
    4686        1701 :   if (gequal0(t)) { avma = av; return NULL; }
    4687             : 
    4688        1666 :   t = gneg_i(t); setlg(x,l);
    4689        1666 :   return gerepileupto(av, RgC_Rg_div(x, t));
    4690             : }
    4691             : 
    4692             : /* Return X such that m X = v (t_COL or t_MAT), resp. an empty t_COL / t_MAT
    4693             :  * if no solution exist */
    4694             : GEN
    4695        2366 : inverseimage(GEN m, GEN v)
    4696             : {
    4697             :   GEN y;
    4698        2366 :   if (typ(m)!=t_MAT) pari_err_TYPE("inverseimage",m);
    4699        2366 :   switch(typ(v))
    4700             :   {
    4701             :     case t_COL:
    4702        2128 :       y = RgM_RgC_invimage(m,v);
    4703        2128 :       return y? y: cgetg(1,t_COL);
    4704             :     case t_MAT:
    4705         238 :       y = RgM_invimage(m, v);
    4706         238 :       return y? y: cgetg(1,t_MAT);
    4707             :   }
    4708           0 :   pari_err_TYPE("inverseimage",v);
    4709             :   return NULL;/*LCOV_EXCL_LINE*/
    4710             : }
    4711             : 
    4712             : static GEN
    4713          21 : Flm_invimage_CUP(GEN A, GEN B, ulong p) {
    4714          21 :   pari_sp av = avma;
    4715             :   GEN R, Rc, C, U, P, B1, B2, C1, C2, X, Y, Z;
    4716             :   long r;
    4717          21 :   r = Flm_CUP(A, &R, &C, &U, &P, p);
    4718          21 :   Rc = indexcompl(R, nbrows(B));
    4719          21 :   C1 = rowpermute(C, R);
    4720          21 :   C2 = rowpermute(C, Rc);
    4721          21 :   B1 = rowpermute(B, R);
    4722          21 :   B2 = rowpermute(B, Rc);
    4723          21 :   Z = Flm_rsolve_lower_unit(C1, B1, p);
    4724          21 :   if (!gequal(Flm_mul(C2, Z, p), B2))
    4725          14 :     return NULL;
    4726          14 :   Y = vconcat(Flm_rsolve_upper(vecslice(U, 1, r), Z, p),
    4727          14 :               zero_Flm(lg(A) - 1 - r, lg(B) - 1));
    4728           7 :   X = rowpermute(Y, perm_inv(P));
    4729           7 :   return gerepileupto(av, X);
    4730             : }
    4731             : 
    4732             : static GEN
    4733          42 : Flm_invimage_i(GEN A, GEN B, ulong p)
    4734             : {
    4735             :   GEN d, x, X, Y;
    4736          42 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4737             : 
    4738          42 :   if (!nB) return cgetg(1, t_MAT);
    4739          42 :   if (nA + nB >= Flm_CUP_LIMIT && nbrows(B) >= Flm_CUP_LIMIT)
    4740          21 :     return Flm_invimage_CUP(A, B, p);
    4741             : 
    4742          21 :   x = Flm_ker_sp(shallowconcat(Flm_neg(A,p), B), p, 0);
    4743             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4744             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4745             :    * Y has at least nB columns and full rank */
    4746          21 :   nY = lg(x)-1;
    4747          21 :   if (nY < nB) return NULL;
    4748          21 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    4749          21 :   d = cgetg(nB+1, t_VECSMALL);
    4750          56 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4751             :   {
    4752          49 :     for (; j>=1; j--)
    4753          42 :       if (coeff(Y,i,j)) { d[i] = j; break; }
    4754          42 :     if (!j) return NULL;
    4755             :   }
    4756             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    4757          14 :   Y = vecpermute(Y, d);
    4758          14 :   x = vecpermute(x, d);
    4759          14 :   X = rowslice(x, 1, nA);
    4760          14 :   return Flm_mul(X, Flm_inv_upper_1(Y,p), p);
    4761             : }
    4762             : 
    4763             : static GEN
    4764           7 : F2m_invimage_i(GEN A, GEN B)
    4765             : {
    4766             :   GEN d, x, X, Y;
    4767           7 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4768           7 :   x = F2m_ker_sp(shallowconcat(A, B), 0);
    4769             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4770             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4771             :    * Y has at least nB columns and full rank */
    4772           7 :   nY = lg(x)-1;
    4773           7 :   if (nY < nB) return NULL;
    4774             : 
    4775             :   /* implicitly: Y = rowslice(x, nA+1, nA+nB), nB rows */
    4776           7 :   d = cgetg(nB+1, t_VECSMALL);
    4777          21 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4778             :   {
    4779          14 :     for (; j>=1; j--)
    4780          14 :       if (F2m_coeff(x,nA+i,j)) { d[i] = j; break; } /* Y[i,j] */
    4781          14 :     if (!j) return NULL;
    4782             :   }
    4783           7 :   x = vecpermute(x, d);
    4784             : 
    4785           7 :   X = F2m_rowslice(x, 1, nA);
    4786           7 :   Y = F2m_rowslice(x, nA+1, nA+nB);
    4787           7 :   return F2m_mul(X, F2m_inv_upper_1(Y));
    4788             : }
    4789             : GEN
    4790           0 : Flm_invimage(GEN A, GEN B, ulong p)
    4791             : {
    4792           0 :   pari_sp av = avma;
    4793           0 :   GEN X = Flm_invimage_i(A,B,p);
    4794           0 :   if (!X) { avma = av; return NULL; }
    4795           0 :   return gerepileupto(av, X);
    4796             : }
    4797             : GEN
    4798           0 : F2m_invimage(GEN A, GEN B)
    4799             : {
    4800           0 :   pari_sp av = avma;
    4801           0 :   GEN X = F2m_invimage_i(A,B);
    4802           0 :   if (!X) { avma = av; return NULL; }
    4803           0 :   return gerepileupto(av, X);
    4804             : }
    4805             : static GEN
    4806          35 : FpM_invimage_i(GEN A, GEN B, GEN p)
    4807             : {
    4808             :   GEN d, x, X, Y;
    4809          35 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4810          35 :   if (lgefint(p) == 3)
    4811             :   {
    4812           0 :     ulong pp = p[2];
    4813           0 :     A = ZM_to_Flm(A, pp);
    4814           0 :     B = ZM_to_Flm(B, pp);
    4815           0 :     x = Flm_invimage_i(A, B, pp);
    4816           0 :     return x? Flm_to_ZM(x): NULL;
    4817             :   }
    4818          35 :   x = FpM_ker(shallowconcat(ZM_neg(A), B), p);
    4819             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4820             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4821             :    * Y has at least nB columns and full rank */
    4822          35 :   nY = lg(x)-1;
    4823          35 :   if (nY < nB) return NULL;
    4824          28 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    4825          28 :   d = cgetg(nB+1, t_VECSMALL);
    4826         217 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4827             :   {
    4828         280 :     for (; j>=1; j--)
    4829         266 :       if (signe(gcoeff(Y,i,j))) { d[i] = j; break; }
    4830         203 :     if (!j) return NULL;
    4831             :   }
    4832             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    4833          14 :   Y = vecpermute(Y, d);
    4834          14 :   x = vecpermute(x, d);
    4835          14 :   X = rowslice(x, 1, nA);
    4836          14 :   return FpM_mul(X, FpM_inv_upper_1(Y,p), p);
    4837             : }
    4838             : GEN
    4839           0 : FpM_invimage(GEN A, GEN B, GEN p)
    4840             : {
    4841           0 :   pari_sp av = avma;
    4842           0 :   GEN X = FpM_invimage_i(A,B,p);
    4843           0 :   if (!X) { avma = av; return NULL; }
    4844           0 :   return gerepileupto(av, X);
    4845             : }
    4846             : 
    4847             : static GEN
    4848          84 : RgM_invimage_FpM(GEN A, GEN B, GEN p)
    4849             : {
    4850          84 :   pari_sp av = avma;
    4851             :   ulong pp;
    4852             :   GEN x;
    4853          84 :   A = RgM_Fp_init(A,p,&pp);
    4854          84 :   switch(pp)
    4855             :   {
    4856             :   case 0:
    4857          35 :     B = RgM_to_FpM(B,p);
    4858          35 :     x = FpM_invimage_i(A, B, p);
    4859          35 :     return x ? gerepileupto(av, FpM_to_mod(x, p)): x;
    4860             :   case 2:
    4861           7 :     B = RgM_to_F2m(B);
    4862           7 :     x = F2m_invimage_i(A, B);
    4863           7 :     return x ? gerepileupto(av, F2m_to_mod(x)): x;
    4864             :   default:
    4865          42 :     B = RgM_to_Flm(B,pp);
    4866          42 :     x = Flm_invimage_i(A, B, pp);
    4867          42 :     return x ? gerepileupto(av, Flm_to_mod(x, pp)): x;
    4868             :   }
    4869             : }
    4870             : 
    4871             : static GEN
    4872         252 : RgM_invimage_fast(GEN x, GEN y)
    4873             : {
    4874             :   GEN p, pol;
    4875             :   long pa;
    4876         252 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    4877         252 :   switch(t)
    4878             :   {
    4879          84 :     case t_INTMOD: return RgM_invimage_FpM(x, y, p);
    4880         105 :     case t_FFELT:  return FFM_invimage(x, y, pol);
    4881          63 :     default:       return gen_0;
    4882             :   }
    4883             : }
    4884             : 
    4885             : /* find Z such that A Z = B. Return NULL if no solution */
    4886             : GEN
    4887         252 : RgM_invimage(GEN A, GEN B)
    4888             : {
    4889         252 :   pari_sp av = avma;
    4890             :   GEN d, x, X, Y;
    4891         252 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4892         252 :   X = RgM_invimage_fast(A, B);
    4893         252 :   if (!X) {avma = av; return NULL; }
    4894         140 :   if (X != gen_0) return X;
    4895          63 :   x = ker(shallowconcat(RgM_neg(A), B));
    4896             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4897             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4898             :    * Y has at least nB columns and full rank */
    4899          63 :   nY = lg(x)-1;
    4900          63 :   if (nY < nB) { avma = av; return NULL; }
    4901          49 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    4902          49 :   d = cgetg(nB+1, t_VECSMALL);
    4903         441 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4904             :   {
    4905         546 :     for (; j>=1; j--)
    4906         532 :       if (!gequal0(gcoeff(Y,i,j))) { d[i] = j; break; }
    4907         406 :     if (!j) { avma = av; return NULL; }
    4908             :   }
    4909             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    4910          35 :   Y = vecpermute(Y, d);
    4911          35 :   x = vecpermute(x, d);
    4912          35 :   X = rowslice(x, 1, nA);
    4913          35 :   return gerepileupto(av, RgM_mul(X, RgM_inv_upper(Y)));
    4914             : }
    4915             : 
    4916             : /* r = dim Ker x, n = nbrows(x) */
    4917             : static GEN
    4918       35861 : get_suppl(GEN x, GEN d, long n, long r, GEN(*ei)(long,long))
    4919             : {
    4920             :   pari_sp av;
    4921             :   GEN y, c;
    4922       35861 :   long j, k, rx = lg(x)-1; /* != 0 due to init_suppl() */
    4923             : 
    4924       35861 :   if (rx == n && r == 0) return gcopy(x);
    4925       33175 :   y = cgetg(n+1, t_MAT);
    4926       33175 :   av = avma; c = zero_zv(n);
    4927             :   /* c = lines containing pivots (could get it from gauss_pivot, but cheap)
    4928             :    * In theory r = 0 and d[j] > 0 for all j, but why take chances? */
    4929      257163 :   for (k = j = 1; j<=rx; j++)
    4930      223988 :     if (d[j]) { c[ d[j] ] = 1; gel(y,k++) = gel(x,j); }
    4931      342298 :   for (j=1; j<=n; j++)
    4932      309123 :     if (!c[j]) gel(y,k++) = (GEN)j; /* HACK */
    4933       33175 :   avma = av;
    4934             : 
    4935       33175 :   rx -= r;
    4936       33175 :   for (j=1; j<=rx; j++) gel(y,j) = gcopy(gel(y,j));
    4937       33175 :   for (   ; j<=n; j++)  gel(y,j) = ei(n, y[j]);
    4938       33175 :   return y;
    4939             : }
    4940             : 
    4941             : static void
    4942       35861 : init_suppl(GEN x)
    4943             : {
    4944       35861 :   if (lg(x) == 1) pari_err_IMPL("suppl [empty matrix]");
    4945             :   /* HACK: avoid overwriting d from gauss_pivot() after avma=av */
    4946       35861 :   (void)new_chunk(lgcols(x) * 2);
    4947       35861 : }
    4948             : 
    4949             : GEN
    4950       34482 : FpM_suppl(GEN x, GEN p)
    4951             : {
    4952             :   GEN d;
    4953             :   long r;
    4954       34482 :   init_suppl(x); d = FpM_gauss_pivot(x,p, &r);
    4955       34482 :   return get_suppl(x,d,nbrows(x),r,&col_ei);
    4956             : }
    4957             : 
    4958             : GEN
    4959          63 : Flm_suppl(GEN x, ulong p)
    4960             : {
    4961             :   GEN d;
    4962             :   long r;
    4963          63 :   init_suppl(x); d = Flm_pivots(x, p, &r, 0);
    4964          63 :   return get_suppl(x,d,nbrows(x),r,&vecsmall_ei);
    4965             : }
    4966             : 
    4967             : GEN
    4968          14 : F2m_suppl(GEN x)
    4969             : {
    4970             :   GEN d;
    4971             :   long r;
    4972          14 :   init_suppl(x); d = F2m_gauss_pivot(F2m_copy(x), &r);
    4973          14 :   return get_suppl(x,d,mael(x,1,1),r,&F2v_ei);
    4974             : }
    4975             : 
    4976             : static GEN
    4977          70 : RgM_suppl_FpM(GEN x, GEN p)
    4978             : {
    4979          70 :   pari_sp av = avma;
    4980             :   ulong pp;
    4981          70 :   x = RgM_Fp_init(x, p, &pp);
    4982          70 :   switch(pp)
    4983             :   {
    4984          21 :   case 0: x = FpM_to_mod(FpM_suppl(x,p), p); break;
    4985          14 :   case 2: x = F2m_to_mod(F2m_suppl(x)); break;
    4986          35 :   default:x = Flm_to_mod(Flm_suppl(x,pp), pp); break;
    4987             :   }
    4988          70 :   return gerepileupto(av, x);
    4989             : }
    4990             : 
    4991             : static GEN
    4992         175 : RgM_suppl_fast(GEN x)
    4993             : {
    4994             :   GEN p, pol;
    4995             :   long pa;
    4996         175 :   long t = RgM_type(x,&p,&pol,&pa);
    4997         175 :   switch(t)
    4998             :   {
    4999          70 :     case t_INTMOD: return RgM_suppl_FpM(x, p);
    5000          35 :     case t_FFELT:  return FFM_suppl(x, pol);
    5001          70 :     default:       return NULL;
    5002             :   }
    5003             : }
    5004             : 
    5005             : /* x is an n x k matrix, rank(x) = k <= n. Return an invertible n x n matrix
    5006             :  * whose first k columns are given by x. If rank(x) < k, undefined result. */
    5007             : GEN
    5008         175 : suppl(GEN x)
    5009             : {
    5010         175 :   pari_sp av = avma;
    5011             :   GEN d, M;
    5012             :   long r;
    5013         175 :   if (typ(x)!=t_MAT) pari_err_TYPE("suppl",x);
    5014         175 :   M = RgM_suppl_fast(x);
    5015         175 :   if (M) return M;
    5016          70 :   init_suppl(x);
    5017          70 :   d = gauss_pivot(x,&r);
    5018          70 :   avma = av; return get_suppl(x,d,nbrows(x),r,&col_ei);
    5019             : }
    5020             : /* variable number to be filled in later */
    5021             : static GEN
    5022          84 : _FlxC_ei(long n, long i)
    5023             : {
    5024          84 :   GEN x = cgetg(n + 1, t_COL);
    5025             :   long j;
    5026        1652 :   for (j = 1; j <= n; j++)
    5027        1568 :     gel(x, j) = (j == i)? pol1_Flx(0): pol0_Flx(0);
    5028          84 :   return x;
    5029             : }
    5030             : 
    5031             : GEN
    5032           7 : F2xqM_suppl(GEN x, GEN T)
    5033             : {
    5034           7 :   pari_sp av = avma;
    5035             :   GEN d, y;
    5036           7 :   long n = nbrows(x), r, sv = get_Flx_var(T);
    5037             : 
    5038           7 :   init_suppl(x);
    5039           7 :   d = F2xqM_gauss_pivot(x, T, &r);
    5040           7 :   avma = av;
    5041           7 :   y = get_suppl(x, d, n, r, &_FlxC_ei);
    5042           7 :   if (sv) {
    5043             :     long i, j;
    5044          21 :     for (j = r + 1; j <= n; j++) {
    5045          42 :       for (i = 1; i <= n; i++)
    5046          28 :         gcoeff(y, i, j)[1] = sv;
    5047             :     }
    5048             :   }
    5049           7 :   return y;
    5050             : }
    5051             : 
    5052             : GEN
    5053          14 : FlxqM_suppl(GEN x, GEN T, ulong p)
    5054             : {
    5055          14 :   pari_sp av = avma;
    5056             :   GEN d, y;
    5057          14 :   long n = nbrows(x), r, sv = get_Flx_var(T);
    5058             : 
    5059          14 :   init_suppl(x);
    5060          14 :   d = FlxqM_gauss_pivot(x, T, p, &r);
    5061          14 :   avma = av;
    5062          14 :   y = get_suppl(x, d, n, r, &_FlxC_ei);
    5063          14 :   if (sv) {
    5064             :     long i, j;
    5065          21 :     for (j = r + 1; j <= n; j++) {
    5066          42 :       for (i = 1; i <= n; i++)
    5067          28 :         gcoeff(y, i, j)[1] = sv;
    5068             :     }
    5069             :   }
    5070          14 :   return y;
    5071             : }
    5072             : 
    5073             : GEN
    5074        4039 : FqM_suppl(GEN x, GEN T, GEN p)
    5075             : {
    5076        4039 :   pari_sp av = avma;
    5077             :   GEN d;
    5078             :   long r;
    5079             : 
    5080        4039 :   if (!T) return FpM_suppl(x,p);
    5081        1211 :   init_suppl(x);
    5082        1211 :   d = FqM_gauss_pivot(x,T,p,&r);
    5083        1211 :   avma = av; return get_suppl(x,d,nbrows(x),r,&col_ei);
    5084             : }
    5085             : 
    5086             : GEN
    5087           7 : image2(GEN x)
    5088             : {
    5089           7 :   pari_sp av = avma;
    5090             :   long k, n, i;
    5091             :   GEN A, B;
    5092             : 
    5093           7 :   if (typ(x)!=t_MAT) pari_err_TYPE("image2",x);
    5094           7 :   if (lg(x) == 1) return cgetg(1,t_MAT);
    5095           7 :   A = ker(x); k = lg(A)-1;
    5096           7 :   if (!k) { avma = av; return gcopy(x); }
    5097           7 :   A = suppl(A); n = lg(A)-1;
    5098           7 :   B = cgetg(n-k+1, t_MAT);
    5099           7 :   for (i = k+1; i <= n; i++) gel(B,i-k) = RgM_RgC_mul(x, gel(A,i));
    5100           7 :   return gerepileupto(av, B);
    5101             : }
    5102             : 
    5103             : GEN
    5104         210 : matimage0(GEN x,long flag)
    5105             : {
    5106         210 :   switch(flag)
    5107             :   {
    5108         203 :     case 0: return image(x);
    5109           7 :     case 1: return image2(x);
    5110           0 :     default: pari_err_FLAG("matimage");
    5111             :   }
    5112             :   return NULL; /* LCOV_EXCL_LINE */
    5113             : }
    5114             : 
    5115             : static long
    5116         126 : RgM_rank_FpM(GEN x, GEN p)
    5117             : {
    5118         126 :   pari_sp av = avma;
    5119             :   ulong pp;
    5120             :   long r;
    5121         126 :   x = RgM_Fp_init(x,p,&pp);
    5122         126 :   switch(pp)
    5123             :   {
    5124          28 :   case 0: r = FpM_rank(x,p); break;
    5125          63 :   case 2: r = F2m_rank(x); break;
    5126          35 :   default:r = Flm_rank(x,pp); break;
    5127             :   }
    5128         126 :   avma = av; return r;
    5129             : }
    5130             : 
    5131             : static long
    5132          49 : RgM_rank_FqM(GEN x, GEN pol, GEN p)
    5133             : {
    5134          49 :   pari_sp av = avma;
    5135             :   long r;
    5136          49 :   GEN T = RgX_to_FpX(pol, p);
    5137          49 :   if (signe(T) == 0) pari_err_OP("rank",x,pol);
    5138          42 :   r = FqM_rank(RgM_to_FqM(x, T, p), T, p);
    5139          42 :   avma = av;
    5140          42 :   return r;
    5141             : }
    5142             : 
    5143             : #define code(t1,t2) ((t1 << 6) | t2)
    5144             : static long
    5145         287 : RgM_rank_fast(GEN x)
    5146             : {
    5147             :   GEN p, pol;
    5148             :   long pa;
    5149         287 :   long t = RgM_type(x,&p,&pol,&pa);
    5150         287 :   switch(t)
    5151             :   {
    5152          42 :     case t_INT:    return ZM_rank(x);
    5153           0 :     case t_FRAC:   return QM_rank(x);
    5154         126 :     case t_INTMOD: return RgM_rank_FpM(x, p);
    5155          63 :     case t_FFELT:  return FFM_rank(x, pol);
    5156             :     case code(t_POLMOD, t_INTMOD):
    5157          49 :                    return RgM_rank_FqM(x, pol, p);
    5158           7 :     default:       return -1;
    5159             :   }
    5160             : }
    5161             : #undef code
    5162             : 
    5163             : long
    5164         287 : rank(GEN x)
    5165             : {
    5166         287 :   pari_sp av = avma;
    5167             :   long r;
    5168             : 
    5169         287 :   if (typ(x)!=t_MAT) pari_err_TYPE("rank",x);
    5170         287 :   r = RgM_rank_fast(x);
    5171         280 :   if (r >= 0) return r;
    5172           7 :   (void)gauss_pivot(x, &r);
    5173           7 :   avma = av; return lg(x)-1 - r;
    5174             : }
    5175             : 
    5176             : /* d a t_VECSMALL of integers in 1..n. Return the vector of the d[i]
    5177             :  * followed by the missing indices */
    5178             : static GEN
    5179       13242 : perm_complete(GEN d, long n)
    5180             : {
    5181       13242 :   GEN y = cgetg(n+1, t_VECSMALL);
    5182       13242 :   long i, j = 1, k = n, l = lg(d);
    5183       13242 :   pari_sp av = avma;
    5184       13242 :   char *T = stack_calloc(n+1);
    5185       13242 :   for (i = 1; i < l; i++) T[d[i]] = 1;
    5186      135217 :   for (i = 1; i <= n; i++)
    5187      121975 :     if (T[i]) y[j++] = i; else y[k--] = i;
    5188       13242 :   avma = av; return y;
    5189             : }
    5190             : 
    5191             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
    5192             : static GEN
    5193       86092 : indexrank0(long n, long r, GEN d)
    5194             : {
    5195       86092 :   GEN p1, p2, res = cgetg(3,t_VEC);
    5196             :   long i, j;
    5197             : 
    5198       86092 :   r = n - r; /* now r = dim Im(x) */
    5199       86092 :   p1 = cgetg(r+1,t_VECSMALL); gel(res,1) = p1;
    5200       86092 :   p2 = cgetg(r+1,t_VECSMALL); gel(res,2) = p2;
    5201       86092 :   if (d)
    5202             :   {
    5203      502071 :     for (i=0,j=1; j<=n; j++)
    5204      416889 :       if (d[j]) { i++; p1[i] = d[j]; p2[i] = j; }
    5205       85182 :     vecsmall_sort(p1);
    5206             :   }
    5207       86092 :   return res;
    5208             : }
    5209             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
    5210             : static GEN
    5211        2422 : indeximage0(long n, long r, GEN d)
    5212             : {
    5213             :   long i, j;
    5214             :   GEN v;
    5215             : 
    5216        2422 :   r = n - r; /* now r = dim Im(x) */
    5217        2422 :   v = cgetg(r+1,t_VECSMALL);
    5218       17108 :   if (d) for (i=j=1; j<=n; j++)
    5219       14686 :     if (d[j]) v[i++] = j;
    5220        2422 :   return v;
    5221             : }
    5222             : /* x an m x n t_MAT, n > 0, r = dim Ker(x), d from gauss_pivot */
    5223             : static void
    5224        6621 : indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol)
    5225             : {
    5226        6621 :   GEN IR = indexrank0(n, r, d);
    5227        6621 :   *prow = perm_complete(gel(IR,1), m);
    5228        6621 :   *pcol = perm_complete(gel(IR,2), n);
    5229        6621 : }
    5230             : static void
    5231       81893 : init_indexrank(GEN x) {
    5232       81893 :   (void)new_chunk(3 + 2*lg(x)); /* HACK */
    5233       81893 : }
    5234             : 
    5235             : static GEN
    5236          28 : RgM_indexrank_FpM(GEN x, GEN p)
    5237             : {
    5238          28 :   pari_sp av = avma;
    5239             :   ulong pp;
    5240             :   GEN r;
    5241          28 :   x = RgM_Fp_init(x,p,&pp);
    5242          28 :   switch(pp)
    5243             :   {
    5244           7 :   case 0:  r = FpM_indexrank(x,p); break;
    5245           7 :   case 2:  r = F2m_indexrank(x); break;
    5246          14 :   default: r = Flm_indexrank(x,pp); break;
    5247             :   }
    5248          28 :   return gerepileupto(av, r);
    5249             : }
    5250             : 
    5251             : static GEN
    5252           0 : RgM_indexrank_FqM(GEN x, GEN pol, GEN p)
    5253             : {
    5254           0 :   pari_sp av = avma;
    5255           0 :   GEN r, T = RgX_to_FpX(pol, p);
    5256           0 :   if (signe(T) == 0) pari_err_OP("indexrank",x,pol);
    5257           0 :   r = FqM_indexrank(RgM_to_FqM(x, T, p), T, p);
    5258           0 :   return gerepileupto(av, r);
    5259             : }
    5260             : 
    5261             : #define code(t1,t2) ((t1 << 6) | t2)
    5262             : static GEN
    5263       14154 : RgM_indexrank_fast(GEN x)
    5264             : {
    5265             :   GEN p, pol;
    5266             :   long pa;
    5267       14154 :   long t = RgM_type(x,&p,&pol,&pa);
    5268       14154 :   switch(t)
    5269             :   {
    5270         392 :     case t_INT:    return ZM_indexrank(x);
    5271        1148 :     case t_FRAC:   return QM_indexrank(x);
    5272          28 :     case t_INTMOD: return RgM_indexrank_FpM(x, p);
    5273          21 :     case t_FFELT:  return FFM_indexrank(x, pol);
    5274             :     case code(t_POLMOD, t_INTMOD):
    5275           0 :                    return RgM_indexrank_FqM(x, pol, p);
    5276       12565 :     default:       return NULL;
    5277             :   }
    5278             : }
    5279             : #undef code
    5280             : 
    5281             : GEN
    5282       14154 : indexrank(GEN x)
    5283             : {
    5284             :   pari_sp av;
    5285             :   long r;
    5286             :   GEN d;
    5287       14154 :   if (typ(x)!=t_MAT) pari_err_TYPE("indexrank",x);
    5288       14154 :   d = RgM_indexrank_fast(x);
    5289       14154 :   if (d) return d;
    5290       12565 :   av = avma;
    5291       12565 :   init_indexrank(x);
    5292       12565 :   d = gauss_pivot(x, &r);
    5293       12565 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5294             : }
    5295             : 
    5296             : GEN
    5297       29113 : FpM_indexrank(GEN x, GEN p) {
    5298       29113 :   pari_sp av = avma;
    5299             :   long r;
    5300             :   GEN d;
    5301       29113 :   init_indexrank(x);
    5302       29113 :   d = FpM_gauss_pivot(x,p,&r);
    5303       29113 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5304             : }
    5305             : 
    5306             : GEN
    5307       18809 : Flm_indexrank(GEN x, ulong p) {
    5308       18809 :   pari_sp av = avma;
    5309             :   long r;
    5310             :   GEN d;
    5311       18809 :   init_indexrank(x);
    5312       18809 :   d = Flm_pivots(x, p, &r, 0);
    5313       18809 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5314             : }
    5315             : 
    5316             : GEN
    5317           7 : F2m_indexrank(GEN x) {
    5318           7 :   pari_sp av = avma;
    5319             :   long r;
    5320             :   GEN d;
    5321           7 :   init_indexrank(x);
    5322           7 :   d = F2m_gauss_pivot(F2m_copy(x),&r);
    5323           7 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5324             : }
    5325             : 
    5326             : GEN
    5327           7 : F2xqM_indexrank(GEN x, GEN T) {
    5328           7 :   pari_sp av = avma;
    5329             :   long r;
    5330             :   GEN d;
    5331           7 :   init_indexrank(x);
    5332           7 :   d = F2xqM_gauss_pivot(x, T, &r);
    5333           7 :   avma = av; return indexrank0(lg(x) - 1, r, d);
    5334             : }
    5335             : 
    5336             : GEN
    5337           7 : FlxqM_indexrank(GEN x, GEN T, ulong p) {
    5338           7 :   pari_sp av = avma;
    5339             :   long r;
    5340             :   GEN d;
    5341           7 :   init_indexrank(x);
    5342           7 :   d = FlxqM_gauss_pivot(x, T, p, &r);
    5343           7 :   avma = av; return indexrank0(lg(x) - 1, r, d);
    5344             : }
    5345             : 
    5346             : GEN
    5347           7 : FqM_indexrank(GEN x, GEN T, GEN p) {
    5348           7 :   pari_sp av = avma;
    5349             :   long r;
    5350             :   GEN d;
    5351           7 :   init_indexrank(x);
    5352           7 :   d = FqM_gauss_pivot(x, T, p, &r);
    5353           7 :   avma = av; return indexrank0(lg(x) - 1, r, d);
    5354             : }
    5355             : 
    5356             : GEN
    5357        2422 : ZM_indeximage(GEN x) {
    5358        2422 :   pari_sp av = avma;
    5359             :   long r;
    5360             :   GEN d;
    5361        2422 :   init_indexrank(x);
    5362        2422 :   d = ZM_pivots(x,&r);
    5363        2422 :   avma = av; return indeximage0(lg(x)-1, r, d);
    5364             : }
    5365             : long
    5366       43697 : ZM_rank(GEN x) {
    5367       43697 :   pari_sp av = avma;
    5368             :   long r;
    5369       43697 :   (void)ZM_pivots(x,&r);
    5370       43697 :   avma = av; return lg(x)-1-r;
    5371             : }
    5372             : GEN
    5373       18956 : ZM_indexrank(GEN x) {
    5374       18956 :   pari_sp av = avma;
    5375             :   long r;
    5376             :   GEN d;
    5377       18956 :   init_indexrank(x);
    5378       18956 :   d = ZM_pivots(x,&r);
    5379       18956 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5380             : }
    5381             : 
    5382             : long
    5383           0 : QM_rank(GEN x)
    5384             : {
    5385           0 :   pari_sp av = avma;
    5386           0 :   long r = ZM_rank(Q_primpart(x));
    5387           0 :   avma = av;
    5388           0 :   return r;
    5389             : }
    5390             : 
    5391             : GEN
    5392        1148 : QM_indexrank(GEN x)
    5393             : {
    5394        1148 :   pari_sp av = avma;
    5395        1148 :   GEN r = ZM_indexrank(Q_primpart(x));
    5396        1148 :   return gerepileupto(av, r);
    5397             : }
    5398             : 
    5399             : /*******************************************************************/
    5400             : /*                                                                 */
    5401             : /*                             ZabM                                */
    5402             : /*                                                                 */
    5403             : /*******************************************************************/
    5404             : 
    5405             : static GEN
    5406        1090 : FpXM_ratlift(GEN a, GEN q)
    5407             : {
    5408             :   GEN B, y;
    5409        1090 :   long i, j, l = lg(a), n;
    5410        1090 :   B = sqrti(shifti(q,-1));
    5411        1090 :   y = cgetg(l, t_MAT);
    5412        1090 :   if (l==1) return y;
    5413        1090 :   n = lgcols(a);
    5414        3562 :   for (i=1; i<l; i++)
    5415             :   {
    5416        2866 :     GEN yi = cgetg(n, t_COL);
    5417       40711 :     for (j=1; j<n; j++)
    5418             :     {
    5419       38239 :       GEN v = FpX_ratlift(gmael(a,i,j), q, B, B, NULL);
    5420       38239 :       if (!v) return NULL;
    5421       37845 :       gel(yi, j) = RgX_renormalize(v);
    5422             :     }
    5423        2472 :     gel(y,i) = yi;
    5424             :   }
    5425         696 :   return y;
    5426             : }
    5427             : 
    5428             : static GEN
    5429        3130 : FlmV_recover_pre(GEN a, GEN M, ulong p, ulong pi, long sv)
    5430             : {
    5431        3130 :   GEN a1 = gel(a,1);
    5432        3130 :   long i, j, k, l = lg(a1), n, lM = lg(M);
    5433        3130 :   GEN v = cgetg(lM, t_VECSMALL);
    5434        3130 :   GEN y = cgetg(l, t_MAT);
    5435        3130 :   if (l==1) return y;
    5436        3130 :   n = lgcols(a1);
    5437       25575 :   for (i=1; i<l; i++)
    5438             :   {
    5439       22445 :     GEN yi = cgetg(n, t_COL);
    5440      531209 :     for (j=1; j<n; j++)
    5441             :     {
    5442      508764 :       for (k=1; k<lM; k++) uel(v,k) = umael(gel(a,k),i,j);
    5443      508764 :       gel(yi, j) = Flm_Flc_mul_pre_Flx(M, v, p, pi, sv);
    5444             :     }
    5445       22445 :     gel(y,i) = yi;
    5446             :   }
    5447        3130 :   return y;
    5448             : }
    5449             : 
    5450             : static GEN
    5451           0 : FlkM_inv(GEN M, GEN P, ulong p)
    5452             : {
    5453           0 :   ulong pi = get_Fl_red(p);
    5454           0 :   GEN R = Flx_roots(P, p);
    5455           0 :   long l = lg(R), i;
    5456           0 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    5457           0 :   GEN V = cgetg(l, t_VEC);
    5458           0 :   for(i=1; i<l; i++)
    5459             :   {
    5460           0 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, pi);
    5461           0 :     GEN H = Flm_inv_sp(FlxM_eval_powers_pre(M, pows, p, pi), NULL, p);
    5462           0 :     if (!H) return NULL;
    5463           0 :     gel(V, i) = H;
    5464             :   }
    5465           0 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    5466             : }
    5467             : 
    5468             : static GEN
    5469        2040 : FlkM_adjoint(GEN M, GEN P, ulong p)
    5470             : {
    5471        2040 :   ulong pi = get_Fl_red(p);
    5472        2040 :   GEN R = Flx_roots(P, p);
    5473        2040 :   long l = lg(R), i;
    5474        2040 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    5475        2040 :   GEN V = cgetg(l, t_VEC);
    5476       10772 :   for(i=1; i<l; i++)
    5477             :   {
    5478        8732 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, pi);
    5479        8732 :     gel(V, i) = Flm_adjoint(FlxM_eval_powers_pre(M, pows, p, pi), p);
    5480             :   }
    5481        2040 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    5482             : }
    5483             : 
    5484             : 
    5485             : static GEN
    5486        1950 : ZabM_inv_slice(GEN A, GEN Q, GEN P, GEN *mod)
    5487             : {
    5488        1950 :   pari_sp av = avma;
    5489        1950 :   long i, n = lg(P)-1, w = varn(Q);
    5490             :   GEN H, T;
    5491        1950 :   if (n == 1)
    5492             :   {
    5493        1884 :     ulong p = uel(P,1);
    5494        1884 :     GEN Ap = FqM_to_FlxM(A, Q, utoi(p));
    5495        1884 :     GEN Qp = ZX_to_Flx(Q, p);
    5496        1884 :     GEN Hp = FlkM_adjoint(Ap, Qp, p);
    5497        1884 :     Hp = gerepileupto(av, FlxM_to_ZXM(Hp));
    5498        1884 :     *mod = utoi(p); return Hp;
    5499             :   }
    5500          66 :   T = ZV_producttree(P);
    5501          66 :   A = ZXM_nv_mod_tree(A, P, T, w);
    5502          66 :   Q = ZX_nv_mod_tree(Q, P, T);
    5503          66 :   H = cgetg(n+1, t_VEC);
    5504         222 :   for(i=1; i <= n; i++)
    5505             :   {
    5506         156 :     ulong p = P[i];
    5507         156 :     GEN a = gel(A,i), q = gel(Q, i);
    5508         156 :     gel(H,i) = FlkM_adjoint(a, q, p);
    5509             :   }
    5510          66 :   H = nxMV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    5511          66 :   *mod = gmael(T, lg(T)-1, 1);
    5512          66 :   gerepileall(av, 2, &H, mod);
    5513          66 :   return H;
    5514             : }
    5515             : 
    5516             : GEN
    5517        1950 : ZabM_inv_worker(GEN P, GEN A, GEN Q)
    5518             : {
    5519        1950 :   GEN V = cgetg(3, t_VEC);
    5520        1950 :   gel(V,1) = ZabM_inv_slice(A, Q, P, &gel(V,2));
    5521        1950 :   return V;
    5522             : }
    5523             : 
    5524             : static GEN
    5525        6062 : vecnorml1(GEN a)
    5526             : {
    5527             :   long i, l;
    5528        6062 :   GEN g = cgetg_copy(a, &l);
    5529      100478 :   for (i=1; i<l; i++)
    5530       94416 :     gel(g, i) = gnorml1_fake(gel(a,i));
    5531        6062 :   return g;
    5532             : }
    5533             : 
    5534             : static GEN
    5535        1372 : ZabM_true_Hadamard(GEN a)
    5536             : {
    5537        1372 :   pari_sp av = avma;
    5538        1372 :   long n = lg(a)-1, i;
    5539             :   GEN B;
    5540        1372 :   if (n == 0) return gen_1;
    5541        1372 :   if (n == 1) return gnorml1_fake(gcoeff(a,1,1));
    5542         854 :   B = gen_1;
    5543         854 :   for (i = 1; i <= n; i++) B = gmul(B, gnorml2(RgC_gtofp(vecnorml1(gel(a,i)),DEFAULTPREC)));
    5544         854 :   return gerepileuptoint(av, ceil_safe(sqrtr_abs(B)));
    5545             : }
    5546             : 
    5547             : GEN
    5548        1372 : ZabM_inv(GEN A, GEN Q, long n, GEN *pt_den)
    5549             : {
    5550        1372 :   pari_sp av = avma;
    5551        1372 :   long m = lg(A)-1;
    5552             :   GEN bnd, H, D, d, mod, worker;
    5553        1372 :   if (m == 0)
    5554             :   {
    5555           0 :     if (pt_den) *pt_den = gen_1;
    5556           0 :     return cgetg(1, t_MAT);
    5557             :   }
    5558        1372 :   bnd = ZabM_true_Hadamard(A);
    5559        1372 :   worker = strtoclosure("_ZabM_inv_worker", 2, A, Q);
    5560        1372 :   H = gen_crt("ZabM_inv", worker, mkvecsmall(n), expi(bnd), m, &mod,
    5561             :               nxMV_chinese_center, FpXM_center);
    5562        1372 :   D = RgMrow_RgC_mul(H, gel(A,1), 1);
    5563        1372 :   D = ZX_rem(D, Q);
    5564        1372 :   d = Z_content(mkvec2(H, D));
    5565        1372 :   if (d)
    5566             :   {
    5567         546 :     D = ZX_Z_divexact(D, d);
    5568         546 :     H = Q_div_to_int(H, d);
    5569             :   }
    5570        1372 :   if (pt_den)
    5571             :   {
    5572        1372 :     gerepileall(av, 2, &H, &D);
    5573        1372 :     *pt_den = D; return H;
    5574             :   }
    5575           0 :   return gerepileupto(av, H);
    5576             : }
    5577             : 
    5578             : GEN
    5579           0 : ZabM_inv_ratlift(GEN M, GEN P, long n, GEN *pden)
    5580             : {
    5581           0 :   pari_sp av2, av = avma;
    5582             :   GEN q, H;
    5583           0 :   ulong m = LONG_MAX>>1;
    5584           0 :   ulong p= 1 + m - (m % n);
    5585           0 :   long lM = lg(M);
    5586           0 :   if (lM == 1) { *pden = gen_1; return cgetg(1,t_MAT); }
    5587             : 
    5588           0 :   av2 = avma;
    5589           0 :   H = NULL;
    5590             :   for(;;)
    5591           0 :   {
    5592             :     GEN Hp, Pp, Mp, Hr;
    5593           0 :     do p += n; while(!uisprime(p));
    5594           0 :     Pp = ZX_to_Flx(P, p);
    5595           0 :     Mp = FqM_to_FlxM(M, P, utoi(p));
    5596           0 :     Hp = FlkM_inv(Mp, Pp, p);
    5597           0 :     if (!Hp) continue;
    5598           0 :     if (!H)
    5599             :     {
    5600           0 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p);
    5601           0 :       q = utoipos(p);
    5602             :     }
    5603             :     else
    5604           0 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    5605           0 :     Hr = FpXM_ratlift(H, q);
    5606           0 :     if (DEBUGLEVEL>5) err_printf("ZabM_inv mod %ld (ratlift=%ld)\n", p,!!Hr);
    5607           0 :     if (Hr) {/* DONE ? */
    5608           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    5609           0 :       GEN MH = ZXQM_mul(Hl, M, P);
    5610           0 :       if (*pden)
    5611           0 :       { if (RgM_isscalar(MH, *pden)) { H = Hl; break; }}
    5612             :       else
    5613           0 :       { if (RgM_isidentity(MH)) { H = Hl; *pden = gen_1; break; } }
    5614             :     }
    5615             : 
    5616           0 :     if (gc_needed(av,2))
    5617             :     {
    5618           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_inv");
    5619           0 :       gerepileall(av2, 2, &H, &q);
    5620             :     }
    5621             :   }
    5622           0 :   gerepileall(av, 2, &H, pden);
    5623           0 :   return H;
    5624             : }
    5625             : 
    5626             : static GEN
    5627        1090 : FlkM_ker(GEN M, GEN P, ulong p)
    5628             : {
    5629        1090 :   ulong pi = get_Fl_red(p);
    5630        1090 :   GEN R = Flx_roots(P, p);
    5631        1090 :   long l = lg(R), i, dP = degpol(P), r;
    5632             :   GEN M1, K, D;
    5633        1090 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    5634        1090 :   GEN V = cgetg(l, t_VEC);
    5635        1090 :   M1 = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,1), dP, p, pi), p, pi);
    5636        1090 :   K = Flm_ker_sp(M1, p, 2);
    5637        1090 :   r = lg(gel(K,1)); D = gel(K,2);
    5638        1090 :   gel(V, 1) = gel(K,1);
    5639        2266 :   for(i=2; i<l; i++)
    5640             :   {
    5641        1176 :     GEN Mi = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,i), dP, p, pi), p, pi);
    5642        1176 :     GEN K = Flm_ker_sp(Mi, p, 2);
    5643        1176 :     if (lg(gel(K,1)) != r || !zv_equal(D, gel(K,2))) return NULL;
    5644        1176 :     gel(V, i) = gel(K,1);
    5645             :   }
    5646        1090 :   return mkvec2(FlmV_recover_pre(V, W, p, pi, P[1]), D);
    5647             : }
    5648             : 
    5649             : GEN
    5650         686 : ZabM_ker(GEN M, GEN P, long n)
    5651             : {
    5652         686 :   pari_sp av2, av = avma;
    5653             :   GEN q, H, D;
    5654         686 :   ulong m = LONG_MAX>>1;
    5655         686 :   ulong p= 1 + m - (m % n);
    5656         686 :   av2 = avma;
    5657         686 :   H = NULL; D = NULL;
    5658             :   for(;;)
    5659         404 :   {
    5660             :     GEN Kp, Hp, Dp, Pp, Mp, Hr;
    5661       29149 :     do p += n; while(!uisprime(p));
    5662        1090 :     Pp = ZX_to_Flx(P, p);
    5663        1090 :     Mp = FqM_to_FlxM(M, P, utoi(p));
    5664        1090 :     Kp = FlkM_ker(Mp, Pp, p);
    5665        1090 :     if (!Kp) continue;
    5666        1090 :     Hp = gel(Kp,1); Dp = gel(Kp,2);
    5667        1090 :     if (H && (lg(Hp)>lg(H) || (lg(Hp)==lg(H) && vecsmall_lexcmp(Dp,D)>0))) continue;
    5668        1090 :     if (!H || (lg(Hp)<lg(H) || vecsmall_lexcmp(Dp,D)<0))
    5669             :     {
    5670         686 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p); D = Dp;
    5671         686 :       q = utoipos(p);
    5672             :     }
    5673             :     else
    5674         404 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    5675        1090 :     Hr = FpXM_ratlift(H, q);
    5676        1090 :     if (DEBUGLEVEL>5) err_printf("ZabM_ker mod %ld (ratlift=%ld)\n", p,!!Hr);
    5677        1090 :     if (Hr) {/* DONE ? */
    5678         696 :       GEN Hl = vec_Q_primpart(Hr);
    5679         696 :       GEN MH = ZXQM_mul(M, Hl,P);
    5680         696 :       if (gequal0(MH)) { H = Hl;  break; }
    5681             :     }
    5682             : 
    5683         404 :     if (gc_needed(av,2))
    5684             :     {
    5685           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_ker");
    5686           0 :       gerepileall(av2, 3, &H, &D, &q);
    5687             :     }
    5688             :   }
    5689         686 :   return gerepilecopy(av, H);
    5690             : }
    5691             : 
    5692             : GEN
    5693        2030 : ZabM_indexrank(GEN M, GEN P, long n)
    5694             : {
    5695        2030 :   pari_sp av = avma;
    5696        2030 :   ulong m = LONG_MAX>>1;
    5697        2030 :   ulong p = 1+m-(m%n), D = degpol(P);
    5698        2030 :   long lM = lg(M), lmax = 0, c = 0;
    5699             :   GEN v;
    5700             :   for(;;)
    5701         672 :   {
    5702             :     GEN R, Mp, K;
    5703             :     ulong pi;
    5704             :     long l;
    5705       80649 :     do p += n; while (!uisprime(p));
    5706        2702 :     pi = get_Fl_red(p);
    5707        2702 :     R = Flx_roots(ZX_to_Flx(P, p), p);
    5708        2702 :     Mp = FqM_to_FlxM(M, P, utoipos(p));
    5709        2702 :     K = FlxM_eval_powers_pre(Mp, Fl_powers_pre(uel(R,1), D,p,pi), p,pi);
    5710        2702 :     v = Flm_indexrank(K, p);
    5711        2702 :     l = lg(gel(v,2));
    5712        2702 :     if (l == lM) break;
    5713         896 :     if (lmax >= 0 && l > lmax) { lmax = l; c = 0; } else c++;
    5714         896 :     if (c > 2)
    5715             :     { /* probably not maximal rank, expensive check */
    5716         224 :       lM -= lg(ZabM_ker(M, P, n))-1; /* actual rank (+1) */
    5717         224 :       if (lmax == lM) break;
    5718           0 :       lmax = -1; /* disable check */
    5719             :     }
    5720             :   }
    5721        2030 :   return gerepileupto(av, v);
    5722             : }
    5723             : 
    5724             : #if 0
    5725             : GEN
    5726             : ZabM_gauss(GEN M, GEN P, long n, GEN *den)
    5727             : {
    5728             :   pari_sp av = avma;
    5729             :   GEN v, S, W;
    5730             :   v = ZabM_indexrank(M, P, n);
    5731             :   S = shallowmatextract(M,gel(v,1),gel(v,2));
    5732             :   W = ZabM_inv(S, P, n, den);
    5733             :   gerepileall(av,2,&W,den);
    5734             :   return W;
    5735             : }
    5736             : #endif
    5737             : 
    5738             : GEN
    5739         203 : ZabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *den)
    5740             : {
    5741         203 :   GEN v = ZabM_indexrank(M, P, n);
    5742         203 :   if (pv) *pv = v;
    5743         203 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    5744         203 :   return ZabM_inv(M, P, n, den);
    5745             : }
    5746             : GEN
    5747        4305 : ZM_pseudoinv(GEN M, GEN *pv, GEN *den)
    5748             : {
    5749        4305 :   GEN v = ZM_indexrank(M);
    5750        4305 :   if (pv) *pv = v;
    5751        4305 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    5752        4305 :   return ZM_inv(M, den);
    5753             : }
    5754             : 
    5755             : /*******************************************************************/
    5756             : /*                                                                 */
    5757             : /*                   Structured Elimination                        */
    5758             : /*                                                                 */
    5759             : /*******************************************************************/
    5760             : 
    5761             : static void
    5762      100892 : rem_col(GEN c, long i, GEN iscol, GEN Wrow, long *rcol, long *rrow)
    5763             : {
    5764      100892 :   long lc = lg(c), k;
    5765      100892 :   iscol[i] = 0; (*rcol)--;
    5766      892446 :   for (k = 1; k < lc; ++k)
    5767             :   {
    5768      791554 :     Wrow[c[k]]--;
    5769      791554 :     if (Wrow[c[k]]==0) (*rrow)--;
    5770             :   }
    5771      100892 : }
    5772             : 
    5773             : static void
    5774        6046 : rem_singleton(GEN M, GEN iscol, GEN Wrow, long *rcol, long *rrow)
    5775             : {
    5776             :   long i, j;
    5777        6046 :   long nbcol = lg(iscol)-1, last;
    5778             :   do
    5779             :   {
    5780        7988 :     last = 0;
    5781    18973461 :     for (i = 1; i <= nbcol; ++i)
    5782    18965473 :       if (iscol[i])
    5783             :       {
    5784     9753799 :         GEN c = gmael(M, i, 1);
    5785     9753799 :         long lc = lg(c);
    5786    91067880 :         for (j = 1; j < lc; ++j)
    5787    81326858 :           if (Wrow[c[j]] == 1)
    5788             :           {
    5789       12777 :             rem_col(c, i, iscol, Wrow, rcol, rrow);
    5790       12777 :             last=1; break;
    5791             :           }
    5792             :       }
    5793        7988 :   } while (last);
    5794        6046 : }
    5795             : 
    5796             : static GEN
    5797        5920 : fill_wcol(GEN M, GEN iscol, GEN Wrow, long *w, GEN wcol)
    5798             : {
    5799        5920 :   long nbcol = lg(iscol)-1;
    5800             :   long i, j, m, last;
    5801             :   GEN per;
    5802       14704 :   for (m = 2, last=0; !last ; m++)
    5803             :   {
    5804    22262486 :     for (i = 1; i <= nbcol; ++i)
    5805             :     {
    5806    22253702 :       wcol[i] = 0;
    5807    22253702 :       if (iscol[i])
    5808             :       {
    5809    11424798 :         GEN c = gmael(M, i, 1);
    5810    11424798 :         long lc = lg(c);
    5811   106708915 :         for (j = 1; j < lc; ++j)
    5812    95284117 :           if (Wrow[c[j]] == m) {  wcol[i]++; last = 1; }
    5813             :       }
    5814             :     }
    5815             :   }
    5816        5920 :   per = vecsmall_indexsort(wcol);
    5817        5920 :   *w = wcol[per[nbcol]];
    5818        5920 :   return per;
    5819             : }
    5820             : 
    5821             : /* M is a RgMs with nbrow rows, A a list of row indices.
    5822             :    Eliminate rows of M with a single entry that do not belong to A,
    5823             :    and the corresponding columns. Also eliminate columns until #colums=#rows.
    5824             :    Return pcol and prow:
    5825             :    pcol is a map from the new columns indices to the old one.
    5826             :    prow is a map from the old rows indices to the new one (0 if removed).
    5827             : */
    5828             : 
    5829             : void
    5830         126 : RgMs_structelim_col(GEN M, long nbcol, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    5831             : {
    5832             :   long i,j,k;
    5833         126 :   long lA = lg(A);
    5834         126 :   GEN prow = cgetg(nbrow+1, t_VECSMALL);
    5835         126 :   GEN pcol = zero_zv(nbcol);
    5836         126 :   pari_sp av = avma;
    5837         126 :   long rcol = nbcol, rrow = 0, imin = nbcol - usqrt(nbcol);
    5838         126 :   GEN iscol = const_vecsmall(nbcol, 1);
    5839         126 :   GEN Wrow  = zero_zv(nbrow);
    5840         126 :   GEN wcol = cgetg(nbcol+1, t_VECSMALL);
    5841         126 :   pari_sp av2=avma;
    5842      126721 :   for (i = 1; i <= nbcol; ++i)
    5843             :   {
    5844      126595 :     GEN F = gmael(M, i, 1);
    5845      126595 :     long l = lg(F)-1;
    5846     1118179 :     for (j = 1; j <= l; ++j)
    5847      991584 :       Wrow[F[j]]++;
    5848             :   }
    5849         126 :   for (j = 1; j < lA; ++j)
    5850             :   {
    5851           0 :     if (Wrow[A[j]] == 0) { *p_col=NULL; return; }
    5852           0 :     Wrow[A[j]] = -1;
    5853             :   }
    5854      237272 :   for (i = 1; i <= nbrow; ++i)
    5855      237146 :     if (Wrow[i])
    5856       67636 :       rrow++;
    5857         126 :   rem_singleton(M, iscol, Wrow, &rcol, &rrow);
    5858         126 :   if (rcol<rrow) pari_err_BUG("RgMs_structelim, rcol<rrow");
    5859        6172 :   for (; rcol>rrow;)
    5860             :   {
    5861             :     long w;
    5862        5920 :     GEN per = fill_wcol(M, iscol, Wrow, &w, wcol);
    5863       94035 :     for (i = nbcol; i>=imin && wcol[per[i]]>=w && rcol>rrow; i--)
    5864       88115 :       rem_col(gmael(M, per[i], 1), per[i], iscol, Wrow, &rcol, &rrow);
    5865        5920 :     rem_singleton(M, iscol, Wrow, &rcol, &rrow);
    5866        5920 :     avma = av2;
    5867             :   }
    5868      126721 :   for (j = 1, i = 1; i <= nbcol; ++i)
    5869      126595 :     if (iscol[i])
    5870       25703 :       pcol[j++] = i;
    5871         126 :   setlg(pcol,j);
    5872      237272 :   for (k = 1, i = 1; i <= nbrow; ++i)
    5873      237146 :     prow[i] = Wrow[i] ? k++: 0;
    5874         126 :   avma = av;
    5875         126 :   *p_col = pcol; *p_row = prow;
    5876             : }
    5877             : 
    5878             : void
    5879           0 : RgMs_structelim(GEN M, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    5880             : {
    5881           0 :   RgMs_structelim_col(M, lg(M)-1, nbrow, A, p_col, p_row);
    5882           0 : }
    5883             : 
    5884             : /*******************************************************************/
    5885             : /*                                                                 */
    5886             : /*                        EIGENVECTORS                             */
    5887             : /*   (independent eigenvectors, sorted by increasing eigenvalue)   */
    5888             : /*                                                                 */
    5889             : /*******************************************************************/
    5890             : /* assume x is square of dimension > 0 */
    5891             : static int
    5892          14 : RgM_is_symmetric_cx(GEN x, long bit)
    5893             : {
    5894          14 :   pari_sp av = avma;
    5895          14 :   long i, j, l = lg(x);
    5896         112 :   for (i = 1; i < l; i++)
    5897         392 :     for (j = 1; j < i; j++)
    5898             :     {
    5899         294 :       GEN a = gcoeff(x,i,j), b = gcoeff(x,j,i), c = gsub(a,b);
    5900         294 :       if (!gequal0(c) && gexpo(c) - gexpo(a) > -bit) { avma = av; return 0; }
    5901             :     }
    5902          14 :   avma = av; return 1;
    5903             : }
    5904             : static GEN
    5905          14 : eigen_err(GEN x, long prec, long flag)
    5906             : {
    5907          14 :   pari_sp av = avma;
    5908          14 :   if (!RgM_is_symmetric_cx(x, 10-prec2nbits(prec))) pari_err_PREC("mateigen");
    5909             :   /* approximately symmetric: recover */
    5910          14 :   x = jacobi(x, prec); if (flag) return x;
    5911           7 :   return gerepilecopy(av, gel(x,1));
    5912             : }
    5913             : GEN
    5914          77 : mateigen(GEN x, long flag, long prec)
    5915             : {
    5916             :   GEN y, R, T;
    5917          77 :   long k, l, ex, n = lg(x);
    5918          77 :   pari_sp av = avma;
    5919             : 
    5920          77 :   if (typ(x)!=t_MAT) pari_err_TYPE("eigen",x);
    5921          77 :   if (n != 1 && n != lgcols(x)) pari_err_DIM("eigen");
    5922          77 :   if (flag < 0 || flag > 1) pari_err_FLAG("mateigen");
    5923          77 :   if (n == 1)
    5924             :   {
    5925          14 :     if (flag) retmkvec2(cgetg(1,t_VEC), cgetg(1,t_MAT));
    5926           7 :     return cgetg(1,t_VEC);
    5927             :   }
    5928          63 :   if (n == 2)
    5929             :   {
    5930          14 :     if (flag) retmkvec2(mkveccopy(gcoeff(x,1,1)), matid(1));
    5931           7 :     return matid(1);
    5932             :   }
    5933             : 
    5934          49 :   ex = 16 - prec2nbits(prec);
    5935          49 :   T = charpoly(x,0);
    5936          49 :   if (RgX_is_QX(T))
    5937             :   {
    5938          28 :     T = ZX_radical( Q_primpart(T) );
    5939          28 :     R = nfrootsQ(T);
    5940          28 :     if (lg(R)-1 < degpol(T))
    5941             :     { /* add missing complex roots */
    5942          14 :       GEN r = cleanroots(RgX_div(T, roots_to_pol(R, 0)), prec);
    5943          14 :       settyp(r, t_VEC);
    5944          14 :       R = shallowconcat(R, r);
    5945             :     }
    5946             :   }
    5947             :   else
    5948             :   {
    5949          21 :     GEN r1, v = vectrunc_init(lg(T));
    5950             :     long e;
    5951          21 :     R = cleanroots(T,prec);
    5952          21 :     r1 = NULL;
    5953         133 :     for (k = 1; k < lg(R); k++)
    5954             :     {
    5955         112 :       GEN r2 = gel(R,k), r = grndtoi(r2, &e);
    5956         112 :       if (e < ex) r2 = r;
    5957         112 :       if (r1)
    5958             :       {
    5959          91 :         r = gsub(r1,r2);
    5960          91 :         if (gequal0(r) || gexpo(r) < ex) continue;
    5961             :       }
    5962          70 :       vectrunc_append(v, r2);
    5963          70 :       r1 = r2;
    5964             :     }
    5965          21 :     R = v;
    5966             :   }
    5967             :   /* R = distinct complex roots of charpoly(x) */
    5968          49 :   l = lg(R); y = cgetg(l, t_VEC);
    5969         231 :   for (k = 1; k < l; k++)
    5970             :   {
    5971         196 :     GEN F = ker_aux(RgM_Rg_sub_shallow(x, gel(R,k)), x);
    5972         196 :     long d = lg(F)-1;
    5973         196 :     if (!d) { avma = av; return eigen_err(x, prec, flag); }
    5974         182 :     gel(y,k) = F;
    5975         182 :     if (flag) gel(R,k) = const_vec(d, gel(R,k));
    5976             :   }
    5977          35 :   y = shallowconcat1(y);
    5978          35 :   if (lg(y) > n) { avma = av; return eigen_err(x, prec, flag); }
    5979             :   /* lg(y) < n if x is not diagonalizable */
    5980          35 :   if (flag) y = mkvec2(shallowconcat1(R), y);
    5981          35 :   return gerepilecopy(av,y);
    5982             : }
    5983             : GEN
    5984           0 : eigen(GEN x, long prec) { return mateigen(x, 0, prec); }
    5985             : 
    5986             : /*******************************************************************/
    5987             : /*                                                                 */
    5988             : /*                           DETERMINANT                           */
    5989             : /*                                                                 */
    5990             : /*******************************************************************/
    5991             : 
    5992             : GEN
    5993        4053 : det0(GEN a,long flag)
    5994             : {
    5995        4053 :   switch(flag)
    5996             :   {
    5997        4039 :     case 0: return det(a);
    5998          14 :     case 1: return det2(a);
    5999           0 :     default: pari_err_FLAG("matdet");
    6000             :   }
    6001             :   return NULL; /* LCOV_EXCL_LINE */
    6002             : }
    6003             : 
    6004             : /* M a 2x2 matrix, returns det(M) */
    6005             : static GEN
    6006        6479 : RgM_det2(GEN M)
    6007             : {
    6008        6479 :   pari_sp av = avma;
    6009        6479 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    6010        6479 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    6011        6479 :   return gerepileupto(av, gsub(gmul(a,d), gmul(b,c)));
    6012             : }
    6013             : /* M a 2x2 ZM, returns det(M) */
    6014             : static GEN
    6015        8939 : ZM_det2(GEN M)
    6016             : {
    6017        8939 :   pari_sp av = avma;
    6018        8939 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    6019        8939 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    6020        8939 :   return gerepileuptoint(av, subii(mulii(a,d), mulii(b, c)));
    6021             : }
    6022             : /* M a 3x3 ZM, return det(M) */
    6023             : static GEN
    6024        2940 : ZM_det3(GEN M)
    6025             : {
    6026        2940 :   pari_sp av = avma;
    6027        2940 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2), c = gcoeff(M,1,3);
    6028        2940 :   GEN d = gcoeff(M,2,1), e = gcoeff(M,2,2), f = gcoeff(M,2,3);
    6029        2940 :   GEN g = gcoeff(M,3,1), h = gcoeff(M,3,2), i = gcoeff(M,3,3);
    6030        2940 :   GEN t, D = signe(i)? mulii(subii(mulii(a,e), mulii(b,d)), i): gen_0;
    6031        2940 :   if (signe(g))
    6032             :   {
    6033        2737 :     t = mulii(subii(mulii(b,f), mulii(c,e)), g);
    6034        2737 :     D = addii(D, t);
    6035             :   }
    6036        2940 :   if (signe(h))
    6037             :   {
    6038        2758 :     t = mulii(subii(mulii(c,d), mulii(a,f)), h);
    6039        2758 :     D = addii(D, t);
    6040             :   }
    6041        2940 :   return gerepileuptoint(av, D);
    6042             : }
    6043             : 
    6044             : static GEN
    6045        9896 : det_simple_gauss(GEN a, GEN data, pivot_fun pivot)
    6046             : {
    6047        9896 :   pari_sp av = avma;
    6048        9896 :   long i,j,k, s = 1, nbco = lg(a)-1;
    6049        9896 :   GEN p, x = gen_1;
    6050             : 
    6051        9896 :   a = RgM_shallowcopy(a);
    6052       79304 :   for (i=1; i<nbco; i++)
    6053             :   {
    6054       69415 :     k = pivot(a, data, i, NULL);
    6055       69415 :     if (k > nbco) return gerepilecopy(av, gcoeff(a,i,i));
    6056       69408 :     if (k != i)
    6057             :     { /* exchange the lines s.t. k = i */
    6058        4275 :       for (j=i; j<=nbco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    6059        4275 :       s = -s;
    6060             :     }
    6061       69408 :     p = gcoeff(a,i,i);
    6062             : 
    6063       69408 :     x = gmul(x,p);
    6064      406987 :     for (k=i+1; k<=nbco; k++)
    6065             :     {
    6066      337579 :       GEN m = gcoeff(a,i,k);
    6067      337579 :       if (gequal0(m)) continue;
    6068             : 
    6069      117269 :       m = gdiv(m,p);
    6070      776158 :       for (j=i+1; j<=nbco; j++)
    6071      658889 :         gcoeff(a,j,k) = gsub(gcoeff(a,j,k), gmul(m,gcoeff(a,j,i)));
    6072             :     }
    6073       69408 :     if (gc_needed(av,2))
    6074             :     {
    6075           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    6076           0 :       gerepileall(av,2, &a,&x);
    6077             :     }
    6078             :   }
    6079        9889 :   if (s < 0) x = gneg_i(x);
    6080        9889 :   return gerepileupto(av, gmul(x, gcoeff(a,nbco,nbco)));
    6081             : }
    6082             : 
    6083             : GEN
    6084        4939 : det2(GEN a)
    6085             : {
    6086             :   GEN data;
    6087             :   pivot_fun pivot;
    6088        4939 :   long n = lg(a)-1;
    6089        4939 :   if (typ(a)!=t_MAT) pari_err_TYPE("det2",a);
    6090        4939 :   if (!n) return gen_1;
    6091        4939 :   if (n != nbrows(a)) pari_err_DIM("det2");
    6092        4939 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    6093        4939 :   if (n == 2) return RgM_det2(a);
    6094        1687 :   pivot = get_pivot_fun(a, a, &data);
    6095        1687 :   return det_simple_gauss(a, data, pivot);
    6096             : }
    6097             : 
    6098             : static GEN
    6099         924 : mydiv(GEN x, GEN y)
    6100             : {
    6101         924 :   long tx = typ(x), ty = typ(y);
    6102         924 :   if (tx == ty && tx == t_POL && varn(x) == varn(y)) return RgX_div(x,y);
    6103         924 :   return gdiv(x,y);
    6104             : }
    6105             : 
    6106             : /* Assumes a a square t_MAT of dimension n > 0. Returns det(a) using
    6107             :  * Gauss-Bareiss. */
    6108             : static GEN
    6109         329 : det_bareiss(GEN a)
    6110             : {
    6111         329 :   pari_sp av = avma;
    6112         329 :   long nbco = lg(a)-1,i,j,k,s = 1;
    6113             :   GEN p, pprec;
    6114             : 
    6115         329 :   a = RgM_shallowcopy(a);
    6116        1057 :   for (pprec=gen_1,i=1; i<nbco; i++,pprec=p)
    6117             :   {
    6118             :     GEN ci;
    6119         728 :     int diveuc = (gequal1(pprec)==0);
    6120             : 
    6121         728 :     p = gcoeff(a,i,i);
    6122         728 :     if (gequal0(p))
    6123             :     {
    6124           0 :       k=i+1; while (k<=nbco && gequal0(gcoeff(a,i,k))) k++;
    6125           0 :       if (k>nbco) return gerepilecopy(av, p);
    6126           0 :       swap(gel(a,k), gel(a,i)); s = -s;
    6127           0 :       p = gcoeff(a,i,i);
    6128             :     }
    6129         728 :     ci = gel(a,i);
    6130        2037 :     for (k=i+1; k<=nbco; k++)
    6131             :     {
    6132        1309 :       GEN ck = gel(a,k), m = gel(ck,i);
    6133        1309 :       if (gequal0(m))
    6134             :       {
    6135           0 :         if (gequal1(p))
    6136             :         {
    6137           0 :           if (diveuc)
    6138           0 :             gel(a,k) = mydiv(gel(a,k), pprec);
    6139             :         }
    6140             :         else
    6141           0 :           for (j=i+1; j<=nbco; j++)
    6142             :           {
    6143           0 :             GEN p1 = gmul(p, gel(ck,j));
    6144           0 :             if (diveuc) p1 = mydiv(p1,pprec);
    6145           0 :             gel(ck,j) = p1;
    6146             :           }
    6147             :       }
    6148             :       else
    6149        4144 :         for (j=i+1; j<=nbco; j++)
    6150             :         {
    6151        2835 :           pari_sp av2 = avma;
    6152        2835 :           GEN p1 = gsub(gmul(p,gel(ck,j)), gmul(m,gel(ci,j)));
    6153        2835 :           if (diveuc) p1 = mydiv(p1,pprec);
    6154        2835 :           gel(ck,j) = gerepileupto(av2, p1);
    6155             :         }
    6156        1309 :       if (gc_needed(av,2))
    6157             :       {
    6158           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    6159           0 :         gerepileall(av,2, &a,&pprec);
    6160           0 :         ci = gel(a,i);
    6161           0 :         p = gcoeff(a,i,i);
    6162             :       }
    6163             :     }
    6164             :   }
    6165         329 :   p = gcoeff(a,nbco,nbco);
    6166         329 :   p = (s < 0)? gneg(p): gcopy(p);
    6167         329 :   return gerepileupto(av, p);
    6168             : }
    6169             : 
    6170             : /* count non-zero entries in col j, at most 'max' of them.
    6171             :  * Return their indices */
    6172             : static GEN
    6173        1127 : col_count_non_zero(GEN a, long j, long max)
    6174             : {
    6175        1127 :   GEN v = cgetg(max+1, t_VECSMALL);
    6176        1127 :   GEN c = gel(a,j);
    6177        1127 :   long i, l = lg(a), k = 1;
    6178        4347 :   for (i = 1; i < l; i++)
    6179        4221 :     if (!gequal0(gel(c,i)))
    6180             :     {
    6181        4081 :       if (k > max) return NULL; /* fail */
    6182        3080 :       v[k++] = i;
    6183             :     }
    6184         126 :   setlg(v, k); return v;
    6185             : }
    6186             : /* count non-zero entries in row i, at most 'max' of them.
    6187             :  * Return their indices */
    6188             : static GEN
    6189        1113 : row_count_non_zero(GEN a, long i, long max)
    6190             : {
    6191        1113 :   GEN v = cgetg(max+1, t_VECSMALL);
    6192        1113 :   long j, l = lg(a), k = 1;
    6193        4291 :   for (j = 1; j < l; j++)
    6194        4179 :     if (!gequal0(gcoeff(a,i,j)))
    6195             :     {
    6196        4067 :       if (k > max) return NULL; /* fail */
    6197        3066 :       v[k++] = j;
    6198             :     }
    6199         112 :   setlg(v, k); return v;
    6200             : }
    6201             : 
    6202             : static GEN det_develop(GEN a, long max, double bound);
    6203             : /* (-1)^(i+j) a[i,j] * det RgM_minor(a,i,j) */
    6204             : static GEN
    6205         210 : coeff_det(GEN a, long i, long j, long max, double bound)
    6206             : {
    6207         210 :   GEN c = gcoeff(a, i, j);
    6208         210 :   c = gmul(c, det_develop(RgM_minor(a, i,j), max, bound));
    6209         210 :   if (odd(i+j)) c = gneg(c);
    6210         210 :   return c;
    6211             : }
    6212             : /* a square t_MAT, 'bound' a rough upper bound for the number of
    6213             :  * multiplications we are willing to pay while developing rows/columns before
    6214             :  * switching to Gaussian elimination */
    6215             : static GEN
    6216         441 : det_develop(GEN M, long max, double bound)
    6217             : {
    6218         441 :   pari_sp av = avma;
    6219         441 :   long i,j, n = lg(M)-1, lbest = max+2, best_col = 0, best_row = 0;
    6220         441 :   GEN best = NULL;
    6221             : 
    6222         441 :   if (bound < 1.) return det_bareiss(M); /* too costly now */
    6223             : 
    6224         329 :   switch(n)
    6225             :   {
    6226           0 :     case 0: return gen_1;
    6227           0 :     case 1: return gcopy(gcoeff(M,1,1));
    6228          14 :     case 2: return RgM_det2(M);
    6229             :   }
    6230         315 :   if (max > ((n+2)>>1)) max = (n+2)>>1;
    6231        1428 :   for (j = 1; j <= n; j++)
    6232             :   {
    6233        1127 :     pari_sp av2 = avma;
    6234        1127 :     GEN v = col_count_non_zero(M, j, max);
    6235             :     long lv;
    6236        1127 :     if (!v || (lv = lg(v)) >= lbest) { avma = av2; continue; }
    6237          98 :     if (lv == 1) { avma = av; return gen_0; }
    6238          98 :     if (lv == 2) {
    6239          14 :       avma = av;
    6240          14 :       return gerepileupto(av, coeff_det(M,v[1],j,max,bound));
    6241             :     }
    6242          84 :     best = v; lbest = lv; best_col = j;
    6243             :   }
    6244        1414 :   for (i = 1; i <= n; i++)
    6245             :   {
    6246        1113 :     pari_sp av2 = avma;
    6247        1113 :     GEN v = row_count_non_zero(M, i, max);
    6248             :     long lv;
    6249        1113 :     if (!v || (lv = lg(v)) >= lbest) { avma = av2; continue; }
    6250           0 :     if (lv == 1) { avma = av; return gen_0; }
    6251           0 :     if (lv == 2) {
    6252           0 :       avma = av;
    6253           0 :       return gerepileupto(av, coeff_det(M,i,v[1],max,bound));
    6254             :     }
    6255           0 :     best = v; lbest = lv; best_row = i;
    6256             :   }
    6257         301 :   if (best_row)
    6258             :   {
    6259           0 :     double d = lbest-1;
    6260           0 :     GEN s = NULL;
    6261             :     long k;
    6262           0 :     bound /= d*d*d;
    6263           0 :     for (k = 1; k < lbest; k++)
    6264             :     {
    6265           0 :       GEN c = coeff_det(M, best_row, best[k], max, bound);
    6266           0 :       s = s? gadd(s, c): c;
    6267             :     }
    6268           0 :     return gerepileupto(av, s);
    6269             :   }
    6270         301 :   if (best_col)
    6271             :   {
    6272          84 :     double d = lbest-1;
    6273          84 :     GEN s = NULL;
    6274             :     long k;
    6275          84 :     bound /= d*d*d;
    6276         280 :     for (k = 1; k < lbest; k++)
    6277             :     {
    6278         196 :       GEN c = coeff_det(M, best[k], best_col, max, bound);
    6279         196 :       s = s? gadd(s, c): c;
    6280             :     }
    6281          84 :     return gerepileupto(av, s);
    6282             :   }
    6283         217 :   return det_bareiss(M);
    6284             : }
    6285             : 
    6286             : /* area of parallelogram bounded by (v1,v2) */
    6287             : static GEN
    6288       53991 : parallelogramarea(GEN v1, GEN v2)
    6289       53991 : { return gsub(gmul(gnorml2(v1), gnorml2(v2)), gsqr(RgV_dotproduct(v1, v2))); }
    6290             : 
    6291             : /* Square of Hadamard bound for det(a), a square matrix.
    6292             :  * Slightly improvement: instead of using the column norms, use the area of
    6293             :  * the parallelogram formed by pairs of consecutive vectors */
    6294             : GEN
    6295       17038 : RgM_Hadamard(GEN a)
    6296             : {
    6297       17038 :   pari_sp av = avma;
    6298       17038 :   long n = lg(a)-1, i;
    6299             :   GEN B;
    6300       17038 :   if (n == 0) return gen_1;
    6301       17038 :   if (n == 1) return gsqr(gcoeff(a,1,1));
    6302       17038 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    6303       17038 :   B = gen_1;
    6304       71029 :   for (i = 1; i <= n/2; i++)
    6305       53991 :     B = gmul(B, parallelogramarea(gel(a,2*i-1), gel(a,2*i)));
    6306       17038 :   if (odd(n)) B = gmul(B, gnorml2(gel(a, n)));
    6307       17038 :   return gerepileuptoint(av, ceil_safe(B));
    6308             : }
    6309             : 
    6310             : /* If B=NULL, assume B=A' */
    6311             : static GEN
    6312       53569 : ZM_det_slice(GEN A, GEN P, GEN *mod)
    6313             : {
    6314       53569 :   pari_sp av = avma;
    6315       53569 :   long i, n = lg(P)-1;
    6316             :   GEN H, T;
    6317       53569 :   if (n == 1)
    6318             :   {
    6319       42309 :     ulong Hp, p = uel(P,1);
    6320       42309 :     GEN a = ZM_to_Flm(A, p);
    6321       42310 :     Hp = Flm_det_sp(a, p);
    6322       42309 :     avma = av;
    6323       42309 :     *mod = utoi(p); return utoi(Hp);
    6324             :   }
    6325       11260 :   T = ZV_producttree(P);
    6326       11260 :   A = ZM_nv_mod_tree(A, P, T);
    6327       11260 :   H = cgetg(n+1, t_VECSMALL);
    6328       39854 :   for(i=1; i <= n; i++)
    6329             :   {
    6330       28595 :     ulong p = P[i];
    6331       28595 :     GEN a = gel(A,i);
    6332       28595 :     H[i] = Flm_det_sp(a, p);
    6333             :   }
    6334       11259 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    6335       11259 :   *mod = gmael(T, lg(T)-1, 1);
    6336       11259 :   gerepileall(av, 2, &H, mod);
    6337       11260 :   return H;
    6338             : }
    6339             : 
    6340             : GEN
    6341       53570 : ZM_det_worker(GEN P, GEN A)
    6342             : {
    6343       53570 :   GEN V = cgetg(3, t_VEC);
    6344       53569 :   gel(V,1) = ZM_det_slice(A, P, &gel(V,2));
    6345       53564 :   return V;
    6346             : }
    6347             : 
    6348             : /* assume dim(a) = n > 0 */
    6349             : static GEN
    6350       29757 : ZM_det_i(GEN M, long n)
    6351             : {
    6352       29757 :   const long DIXON_THRESHOLD = 40;
    6353       29757 :   pari_sp av = avma, av2;
    6354             :   long i;
    6355       29757 :   ulong p, Dp = 1;
    6356             :   forprime_t S;
    6357             :   pari_timer ti;
    6358             :   GEN H, D, mod, h, q, v, worker;
    6359       29757 :   if (n == 1) return icopy(gcoeff(M,1,1));
    6360       28917 :   if (n == 2) return ZM_det2(M);
    6361       19978 :   if (n == 3) return ZM_det3(M);
    6362       17038 :   if (DEBUGLEVEL >=4) timer_start(&ti);
    6363       17038 :   h = RgM_Hadamard(M);
    6364       17038 :   if (!signe(h)) { avma = av; return gen_0; }
    6365       17038 :   h = sqrti(h); q = gen_1;
    6366       17038 :   init_modular_big(&S);
    6367       17038 :   p = 0; /* -Wall */
    6368       34076 :   while( cmpii(q, h) <= 0 && (p = u_forprime_next(&S)) )
    6369             :   {
    6370       17038 :     av2 = avma; Dp = Flm_det_sp(ZM_to_Flm(M, p), p);
    6371       17038 :     avma = av2;
    6372       17038 :     if (Dp) break;
    6373           0 :     q = muliu(q, p);
    6374             :   }
    6375       17038 :   if (!p) pari_err_OVERFLOW("ZM_det [ran out of primes]");
    6376       17038 :   if (!Dp) { avma = av; return gen_0; }
    6377       17038 :   if (n <= DIXON_THRESHOLD)
    6378       17038 :     D = q;
    6379             :   else
    6380             :   {
    6381           0 :     av2 = avma;
    6382           0 :     v = cgetg(n+1, t_COL);
    6383           0 :     gel(v, 1) = gen_1; /* ensure content(v) = 1 */
    6384           0 :     for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    6385           0 :     D = Q_denom(ZM_gauss(M, v));
    6386           0 :     if (expi(D) < expi(h) >> 1)
    6387             :     { /* First try unlucky, try once more */
    6388           0 :       for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    6389           0 :       D = lcmii(D, Q_denom(ZM_gauss(M, v)));
    6390             :     }
    6391           0 :     D = gerepileuptoint(av2, D);
    6392           0 :     if (q != gen_1) D = lcmii(D, q);
    6393             :   }
    6394             :   /* determinant is a multiple of D */
    6395       17038 :   if (DEBUGLEVEL >=4)
    6396           0 :     timer_printf(&ti,"ZM_det: Dixon %ld/%ld bits",expi(D),expi(h));
    6397       17038 :   h = divii(h, D);
    6398       17038 :   worker = strtoclosure("_ZM_det_worker", 1, M);
    6399       17038 :   H = gen_crt("ZM_det", worker, D, expi(h)+1, lg(M)-1, &mod, ZV_chinese, NULL);
    6400       17038 :   if (D) H = Fp_div(H, D, mod);
    6401       17038 :   H = Fp_center(H, mod, shifti(mod,-1));
    6402       17038 :   if (D) H = mulii(H, D);
    6403       17038 :   return gerepileuptoint(av, H);
    6404             : }
    6405             : 
    6406             : static GEN
    6407        1519 : RgM_det_FpM(GEN a, GEN p)
    6408             : {
    6409        1519 :   pari_sp av = avma;
    6410             :   ulong pp, d;
    6411        1519 :   a = RgM_Fp_init(a,p,&pp);
    6412        1519 :   switch(pp)
    6413             :   {
    6414          70 :   case 0: return gerepileupto(av, Fp_to_mod(FpM_det(a,p),p)); break;
    6415          14 :   case 2: d = F2m_det(a); break;
    6416        1435 :   default:d = Flm_det_sp(a, pp); break;
    6417             :   }
    6418        1449 :   avma = av; return mkintmodu(d, pp);
    6419             : }
    6420             : 
    6421             : static GEN
    6422          42 : RgM_det_FqM(GEN x, GEN pol, GEN p)
    6423             : {
    6424          42 :   pari_sp av = avma;
    6425          42 :   GEN b, T = RgX_to_FpX(pol, p);
    6426          42 :   if (signe(T) == 0) pari_err_OP("%",x,pol);
    6427          42 :   b = FqM_det(RgM_to_FqM(x, T, p), T, p);
    6428          42 :   if (!b) { avma = av; return NULL; }
    6429          42 :   return gerepilecopy(av, mkpolmod(FpX_to_mod(b, p), FpX_to_mod(T, p)));
    6430             : }
    6431             : 
    6432             : #define code(t1,t2) ((t1 << 6) | t2)
    6433             : static GEN
    6434       10575 : RgM_det_fast(GEN x)
    6435             : {
    6436             :   GEN p, pol;
    6437             :   long pa;
    6438       10575 :   long t = RgM_type(x, &p,&pol,&pa);
    6439       10575 :   switch(t)
    6440             :   {
    6441         315 :     case t_INT:    return ZM_det(x);
    6442         196 :     case t_FRAC:   return QM_det(x);
    6443          63 :     case t_FFELT:  return FFM_det(x, pol);
    6444        1519 :     case t_INTMOD: return RgM_det_FpM(x, p);
    6445             :     case code(t_POLMOD, t_INTMOD):
    6446          42 :                    return RgM_det_FqM(x, pol, p);
    6447        8440 :     default:       return NULL;
    6448             :   }
    6449             : }
    6450             : #undef code
    6451             : 
    6452             : static long
    6453         231 : det_init_max(long n)
    6454             : {
    6455         231 :   if (n > 100) return 0;
    6456         231 :   if (n > 50) return 1;
    6457         231 :   if (n > 30) return 4;
    6458         231 :   return 7;
    6459             : }
    6460             : 
    6461             : GEN
    6462       14136 : det(GEN a)
    6463             : {
    6464       14136 :   long n = lg(a)-1;
    6465             :   double B;
    6466             :   GEN data, b;
    6467             :   pivot_fun pivot;
    6468             : 
    6469       14136 :   if (typ(a)!=t_MAT) pari_err_TYPE("det",a);
    6470       14136 :   if (!n) return gen_1;
    6471       14094 :   if (n != nbrows(a)) pari_err_DIM("det");
    6472       14087 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    6473       13788 :   if (n == 2) return RgM_det2(a);
    6474       10575 :   b = RgM_det_fast(a);
    6475       10575 :   if (b) return b;
    6476        8440 :   pivot = get_pivot_fun(a, a, &data);
    6477        8440 :   if (pivot != gauss_get_pivot_NZ) return det_simple_gauss(a, data, pivot);
    6478         231 :   B = (double)n;
    6479         231 :   return det_develop(a, det_init_max(n), B*B*B);
    6480             : }
    6481             : 
    6482             : GEN
    6483       29764 : ZM_det(GEN a)
    6484             : {
    6485       29764 :   long n = lg(a)-1;
    6486       29764 :   if (!n) return gen_1;
    6487       29757 :   return ZM_det_i(a, n);
    6488             : }
    6489             : 
    6490             : GEN
    6491         196 : QM_det(GEN M)
    6492             : {
    6493         196 :   pari_sp av = avma;
    6494         196 :   GEN cM, pM = Q_primitive_part(M, &cM);
    6495         196 :   GEN b = ZM_det(pM);
    6496         196 :   if (cM) b = gmul(b, gpowgs(cM, lg(M)-1));
    6497         196 :   return gerepileupto(av, b);
    6498             : }

Generated by: LCOV version 1.13