Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - alglin1.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 22303-eb3e11d) Lines: 3416 3670 93.1 %
Date: 2018-04-21 06:16:28 Functions: 338 357 94.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000, 2012  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /********************************************************************/
      15             : /**                                                                **/
      16             : /**                         LINEAR ALGEBRA                         **/
      17             : /**                          (first part)                          **/
      18             : /**                                                                **/
      19             : /********************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : /*******************************************************************/
      24             : /*                                                                 */
      25             : /*                         GEREPILE                                */
      26             : /*                                                                 */
      27             : /*******************************************************************/
      28             : 
      29             : static void
      30          66 : gerepile_mat(pari_sp av, pari_sp tetpil, GEN x, long k, long m, long n, long t)
      31             : {
      32          66 :   pari_sp A, bot = pari_mainstack->bot;
      33             :   long u, i;
      34             :   size_t dec;
      35             : 
      36          66 :   (void)gerepile(av,tetpil,NULL); dec = av-tetpil;
      37             : 
      38         877 :   for (u=t+1; u<=m; u++)
      39             :   {
      40         811 :     A = (pari_sp)coeff(x,u,k);
      41         811 :     if (A < av && A >= bot) coeff(x,u,k) += dec;
      42             :   }
      43        1917 :   for (i=k+1; i<=n; i++)
      44       75213 :     for (u=1; u<=m; u++)
      45             :     {
      46       73362 :       A = (pari_sp)coeff(x,u,i);
      47       73362 :       if (A < av && A >= bot) coeff(x,u,i) += dec;
      48             :     }
      49          66 : }
      50             : 
      51             : static void
      52          66 : gen_gerepile_gauss_ker(GEN x, long k, long t, pari_sp av, void *E, GEN (*copy)(void*, GEN))
      53             : {
      54          66 :   pari_sp tetpil = avma;
      55          66 :   long u,i, n = lg(x)-1, m = n? nbrows(x): 0;
      56             : 
      57          66 :   if (DEBUGMEM > 1) pari_warn(warnmem,"gauss_pivot_ker. k=%ld, n=%ld",k,n);
      58          66 :   for (u=t+1; u<=m; u++) gcoeff(x,u,k) = copy(E,gcoeff(x,u,k));
      59        1917 :   for (i=k+1; i<=n; i++)
      60        1851 :     for (u=1; u<=m; u++) gcoeff(x,u,i) = copy(E,gcoeff(x,u,i));
      61          66 :   gerepile_mat(av,tetpil,x,k,m,n,t);
      62          66 : }
      63             : 
      64             : /* special gerepile for huge matrices */
      65             : 
      66             : #define COPY(x) {\
      67             :   GEN _t = (x); if (!is_universal_constant(_t)) x = gcopy(_t); \
      68             : }
      69             : 
      70             : INLINE GEN
      71           0 : _copy(void *E, GEN x)
      72             : {
      73           0 :   (void) E; COPY(x);
      74           0 :   return x;
      75             : }
      76             : 
      77             : static void
      78           0 : gerepile_gauss_ker(GEN x, long k, long t, pari_sp av)
      79             : {
      80           0 :   gen_gerepile_gauss_ker(x, k, t, av, NULL, &_copy);
      81           0 : }
      82             : 
      83             : static void
      84          10 : gerepile_gauss(GEN x,long k,long t,pari_sp av, long j, GEN c)
      85             : {
      86          10 :   pari_sp tetpil = avma, A, bot;
      87          10 :   long u,i, n = lg(x)-1, m = n? nbrows(x): 0;
      88             :   size_t dec;
      89             : 
      90          10 :   if (DEBUGMEM > 1) pari_warn(warnmem,"gauss_pivot. k=%ld, n=%ld",k,n);
      91         375 :   for (u=t+1; u<=m; u++)
      92         365 :     if (u==j || !c[u]) COPY(gcoeff(x,u,k));
      93         710 :   for (u=1; u<=m; u++)
      94         700 :     if (u==j || !c[u])
      95         690 :       for (i=k+1; i<=n; i++) COPY(gcoeff(x,u,i));
      96             : 
      97          10 :   (void)gerepile(av,tetpil,NULL); dec = av-tetpil;
      98          10 :   bot = pari_mainstack->bot;
      99         375 :   for (u=t+1; u<=m; u++)
     100         365 :     if (u==j || !c[u])
     101             :     {
     102         365 :       A=(pari_sp)coeff(x,u,k);
     103         365 :       if (A<av && A>=bot) coeff(x,u,k)+=dec;
     104             :     }
     105         710 :   for (u=1; u<=m; u++)
     106         700 :     if (u==j || !c[u])
     107       47610 :       for (i=k+1; i<=n; i++)
     108             :       {
     109       46920 :         A=(pari_sp)coeff(x,u,i);
     110       46920 :         if (A<av && A>=bot) coeff(x,u,i)+=dec;
     111             :       }
     112          10 : }
     113             : 
     114             : /*******************************************************************/
     115             : /*                                                                 */
     116             : /*                         GENERIC                                 */
     117             : /*                                                                 */
     118             : /*******************************************************************/
     119             : GEN
     120        3351 : gen_ker(GEN x, long deplin, void *E, const struct bb_field *ff)
     121             : {
     122        3351 :   pari_sp av0 = avma, av, tetpil;
     123             :   GEN y, c, d;
     124             :   long i, j, k, r, t, n, m;
     125             : 
     126        3351 :   n=lg(x)-1; if (!n) return cgetg(1,t_MAT);
     127        3351 :   m=nbrows(x); r=0;
     128        3351 :   x = RgM_shallowcopy(x);
     129        3351 :   c = zero_zv(m);
     130        3351 :   d=new_chunk(n+1);
     131        3351 :   av=avma;
     132       25740 :   for (k=1; k<=n; k++)
     133             :   {
     134      214573 :     for (j=1; j<=m; j++)
     135      206022 :       if (!c[j])
     136             :       {
     137      129763 :         gcoeff(x,j,k) = ff->red(E, gcoeff(x,j,k));
     138      129763 :         if (!ff->equal0(gcoeff(x,j,k))) break;
     139             :       }
     140       22459 :     if (j>m)
     141             :     {
     142        8551 :       if (deplin)
     143             :       {
     144          70 :         GEN c = cgetg(n+1, t_COL), g0 = ff->s(E,0), g1=ff->s(E,1);
     145          70 :         for (i=1; i<k; i++) gel(c,i) = ff->red(E, gcoeff(x,d[i],k));
     146          70 :         gel(c,k) = g1; for (i=k+1; i<=n; i++) gel(c,i) = g0;
     147          70 :         return gerepileupto(av0, c);
     148             :       }
     149        8481 :       r++; d[k]=0;
     150       79222 :       for(j=1; j<k; j++)
     151       70741 :         if (d[j]) gcoeff(x,d[j],k) = gclone(gcoeff(x,d[j],k));
     152             :     }
     153             :     else
     154             :     {
     155       13908 :       GEN piv = ff->neg(E,ff->inv(E,gcoeff(x,j,k)));
     156       13908 :       c[j] = k; d[k] = j;
     157       13908 :       gcoeff(x,j,k) = ff->s(E,-1);
     158       13908 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = ff->red(E,ff->mul(E,piv,gcoeff(x,j,i)));
     159      225941 :       for (t=1; t<=m; t++)
     160             :       {
     161      212033 :         if (t==j) continue;
     162             : 
     163      198125 :         piv = ff->red(E,gcoeff(x,t,k));
     164      198125 :         if (ff->equal0(piv)) continue;
     165             : 
     166      147320 :         gcoeff(x,t,k) = ff->s(E,0);
     167     1245785 :         for (i=k+1; i<=n; i++)
     168     1098465 :            gcoeff(x,t,i) = ff->add(E, gcoeff(x,t,i), ff->mul(E,piv,gcoeff(x,j,i)));
     169      147320 :         if (gc_needed(av,1))
     170          66 :           gen_gerepile_gauss_ker(x,k,t,av,E,ff->red);
     171             :       }
     172             :     }
     173             :   }
     174        3281 :   if (deplin) { avma = av0; return NULL; }
     175             : 
     176        3232 :   tetpil=avma; y=cgetg(r+1,t_MAT);
     177       11713 :   for (j=k=1; j<=r; j++,k++)
     178             :   {
     179        8481 :     GEN C = cgetg(n+1,t_COL);
     180        8481 :     GEN g0 = ff->s(E,0), g1 = ff->s(E,1);
     181        8481 :     gel(y,j) = C; while (d[k]) k++;
     182       79222 :     for (i=1; i<k; i++)
     183       70741 :       if (d[i])
     184             :       {
     185       36321 :         GEN p1=gcoeff(x,d[i],k);
     186       36321 :         gel(C,i) = ff->red(E,p1); gunclone(p1);
     187             :       }
     188             :       else
     189       34420 :         gel(C,i) = g0;
     190        8481 :     gel(C,k) = g1; for (i=k+1; i<=n; i++) gel(C,i) = g0;
     191             :   }
     192        3232 :   return gerepile(av0,tetpil,y);
     193             : }
     194             : 
     195             : GEN
     196        2425 : gen_Gauss_pivot(GEN x, long *rr, void *E, const struct bb_field *ff)
     197             : {
     198             :   pari_sp av;
     199             :   GEN c, d;
     200        2425 :   long i, j, k, r, t, m, n = lg(x)-1;
     201             : 
     202        2425 :   if (!n) { *rr = 0; return NULL; }
     203             : 
     204        2425 :   m=nbrows(x); r=0;
     205        2425 :   d = cgetg(n+1, t_VECSMALL);
     206        2425 :   x = RgM_shallowcopy(x);
     207        2425 :   c = zero_zv(m);
     208        2425 :   av=avma;
     209       13497 :   for (k=1; k<=n; k++)
     210             :   {
     211      101210 :     for (j=1; j<=m; j++)
     212       99046 :       if (!c[j])
     213             :       {
     214       67134 :         gcoeff(x,j,k) = ff->red(E,gcoeff(x,j,k));
     215       67134 :         if (!ff->equal0(gcoeff(x,j,k))) break;
     216             :       }
     217       11072 :     if (j>m) { r++; d[k]=0; }
     218             :     else
     219             :     {
     220        8908 :       GEN piv = ff->neg(E,ff->inv(E,gcoeff(x,j,k)));
     221        8908 :       GEN g0 = ff->s(E,0);
     222        8908 :       c[j] = k; d[k] = j;
     223        8908 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = ff->red(E,ff->mul(E,piv,gcoeff(x,j,i)));
     224       97431 :       for (t=1; t<=m; t++)
     225             :       {
     226       88523 :         if (c[t]) continue; /* already a pivot on that line */
     227             : 
     228       58135 :         piv = ff->red(E,gcoeff(x,t,k));
     229       58135 :         if (ff->equal0(piv)) continue;
     230       30548 :         gcoeff(x,t,k) = g0;
     231      321167 :         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       30548 :         if (gc_needed(av,1))
     234          10 :           gerepile_gauss(x,k,t,av,j,c);
     235             :       }
     236        8908 :       for (i=k; i<=n; i++) gcoeff(x,j,i) = g0; /* dummy */
     237             :     }
     238             :   }
     239        2425 :   *rr = r; avma = (pari_sp)d; return d;
     240             : }
     241             : 
     242             : GEN
     243         413 : gen_det(GEN a, void *E, const struct bb_field *ff)
     244             : {
     245         413 :   pari_sp av = avma;
     246         413 :   long i,j,k, s = 1, nbco = lg(a)-1;
     247         413 :   GEN q, x = ff->s(E,1);
     248         413 :   if (!nbco) return x;
     249         406 :   a = RgM_shallowcopy(a);
     250        3192 :   for (i=1; i<nbco; i++)
     251             :   {
     252        6034 :     for(k=i; k<=nbco; k++)
     253             :     {
     254        5999 :       gcoeff(a,k,i) = ff->red(E,gcoeff(a,k,i));
     255        5999 :       if (!ff->equal0(gcoeff(a,k,i))) break;
     256             :     }
     257        2821 :     if (k > nbco) return gerepileupto(av, gcoeff(a,i,i));
     258        2786 :     if (k != i)
     259             :     { /* exchange the lines s.t. k = i */
     260         665 :       for (j=i; j<=nbco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
     261         665 :       s = -s;
     262             :     }
     263        2786 :     q = gcoeff(a,i,i);
     264             : 
     265        2786 :     x = ff->red(E,ff->mul(E,x,q));
     266        2786 :     q = ff->inv(E,q);
     267       24717 :     for (k=i+1; k<=nbco; k++)
     268             :     {
     269       21931 :       GEN m = ff->red(E,gcoeff(a,i,k));
     270       21931 :       if (ff->equal0(m)) continue;
     271             : 
     272       14903 :       m = ff->neg(E, ff->mul(E,m, q));
     273      206920 :       for (j=i+1; j<=nbco; j++)
     274             :       {
     275      192017 :         gcoeff(a,j,k) = ff->add(E, gcoeff(a,j,k), ff->mul(E,m,gcoeff(a,j,i)));
     276      192017 :         if (gc_needed(av,1))
     277             :         {
     278         823 :           if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
     279         823 :           gerepileall(av,4, &a,&x,&q,&m);
     280             :         }
     281             :       }
     282             :     }
     283             :   }
     284         371 :   if (s < 0) x = ff->neg(E,x);
     285         371 :   return gerepileupto(av, ff->red(E,ff->mul(E, x, gcoeff(a,nbco,nbco))));
     286             : }
     287             : 
     288             : INLINE void
     289      899356 : _gen_addmul(GEN b, long k, long i, GEN m, void *E, const struct bb_field *ff)
     290             : {
     291      899356 :   gel(b,i) = ff->red(E,gel(b,i));
     292      899356 :   gel(b,k) = ff->add(E,gel(b,k), ff->mul(E,m, gel(b,i)));
     293      899356 : }
     294             : 
     295             : static GEN
     296       57189 : _gen_get_col(GEN a, GEN b, long li, void *E, const struct bb_field *ff)
     297             : {
     298       57189 :   GEN u = cgetg(li+1,t_COL);
     299       57189 :   pari_sp av = avma;
     300             :   long i, j;
     301             : 
     302       57189 :   gel(u,li) = gerepileupto(av, ff->red(E,ff->mul(E,gel(b,li), gcoeff(a,li,li))));
     303      325674 :   for (i=li-1; i>0; i--)
     304             :   {
     305      268485 :     pari_sp av = avma;
     306      268485 :     GEN m = gel(b,i);
     307      268485 :     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      268485 :     m = ff->red(E, m);
     309      268485 :     gel(u,i) = gerepileupto(av, ff->red(E,ff->mul(E,m, gcoeff(a,i,i))));
     310             :   }
     311       57189 :   return u;
     312             : }
     313             : 
     314             : GEN
     315       12595 : gen_Gauss(GEN a, GEN b, void *E, const struct bb_field *ff)
     316             : {
     317             :   long i, j, k, li, bco, aco;
     318       12595 :   GEN u, g0 = ff->s(E,0);
     319       12595 :   pari_sp av = avma;
     320       12595 :   a = RgM_shallowcopy(a);
     321       12595 :   b = RgM_shallowcopy(b);
     322       12595 :   aco = lg(a)-1; bco = lg(b)-1; li = nbrows(a);
     323       57574 :   for (i=1; i<=aco; i++)
     324             :   {
     325             :     GEN invpiv;
     326       74611 :     for (k = i; k <= li; k++)
     327             :     {
     328       74513 :       GEN piv = ff->red(E,gcoeff(a,k,i));
     329       74513 :       if (!ff->equal0(piv)) { gcoeff(a,k,i) = ff->inv(E,piv); break; }
     330       17037 :       gcoeff(a,k,i) = g0;
     331             :     }
     332             :     /* found a pivot on line k */
     333       57574 :     if (k > li) return NULL;
     334       57476 :     if (k != i)
     335             :     { /* swap lines so that k = i */
     336        9182 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
     337        9182 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
     338             :     }
     339       57476 :     if (i == aco) break;
     340             : 
     341       44979 :     invpiv = gcoeff(a,i,i); /* 1/piv mod p */
     342      188696 :     for (k=i+1; k<=li; k++)
     343             :     {
     344      143717 :       GEN m = ff->red(E,gcoeff(a,k,i)); gcoeff(a,k,i) = g0;
     345      143717 :       if (ff->equal0(m)) continue;
     346             : 
     347       44680 :       m = ff->red(E,ff->neg(E,ff->mul(E,m, invpiv)));
     348       44680 :       for (j=i+1; j<=aco; j++) _gen_addmul(gel(a,j),k,i,m,E,ff);
     349       44680 :       for (j=1  ; j<=bco; j++) _gen_addmul(gel(b,j),k,i,m,E,ff);
     350             :     }
     351       44979 :     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       12497 :   if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
     359       12497 :   u = cgetg(bco+1,t_MAT);
     360       12497 :   for (j=1; j<=bco; j++) gel(u,j) = _gen_get_col(a, gel(b,j), aco, E, ff);
     361       12497 :   return u;
     362             : }
     363             : 
     364             : /* compatible t_MAT * t_COL, lgA = lg(A) = lg(B) > 1, l = lgcols(A) */
     365             : static GEN
     366      311892 : gen_matcolmul_i(GEN A, GEN B, ulong lgA, ulong l,
     367             :                 void *E, const struct bb_field *ff)
     368             : {
     369      311892 :   GEN C = cgetg(l, t_COL);
     370             :   ulong i;
     371     2092412 :   for (i = 1; i < l; i++) {
     372     1780520 :     pari_sp av = avma;
     373     1780520 :     GEN e = ff->mul(E, gcoeff(A, i, 1), gel(B, 1));
     374             :     ulong k;
     375    10477236 :     for(k = 2; k < lgA; k++)
     376     8696716 :       e = ff->add(E, e, ff->mul(E, gcoeff(A, i, k), gel(B, k)));
     377     1780520 :     gel(C, i) = gerepileupto(av, ff->red(E, e));
     378             :   }
     379      311892 :   return C;
     380             : }
     381             : 
     382             : GEN
     383      161735 : gen_matcolmul(GEN A, GEN B, void *E, const struct bb_field *ff)
     384             : {
     385      161735 :   ulong lgA = lg(A);
     386      161735 :   if (lgA != (ulong)lg(B))
     387           0 :     pari_err_OP("operation 'gen_matcolmul'", A, B);
     388      161735 :   if (lgA == 1)
     389           0 :     return cgetg(1, t_COL);
     390      161735 :   return gen_matcolmul_i(A, B, lgA, lgcols(A), E, ff);
     391             : }
     392             : 
     393             : GEN
     394       15197 : gen_matmul(GEN A, GEN B, void *E, const struct bb_field *ff)
     395             : {
     396       15197 :   ulong j, l, lgA, lgB = lg(B);
     397             :   GEN C;
     398       15197 :   if (lgB == 1)
     399           0 :     return cgetg(1, t_MAT);
     400       15197 :   lgA = lg(A);
     401       15197 :   if (lgA != (ulong)lgcols(B))
     402           0 :     pari_err_OP("operation 'gen_matmul'", A, B);
     403       15197 :   if (lgA == 1)
     404           0 :     return zeromat(0, lgB - 1);
     405       15197 :   l = lgcols(A);
     406       15197 :   C = cgetg(lgB, t_MAT);
     407      165354 :   for(j = 1; j < lgB; j++)
     408      150157 :     gel(C, j) = gen_matcolmul_i(A, gel(B, j), lgA, l, E, ff);
     409       15197 :   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       81667 : image_from_pivot(GEN x, GEN d, long r)
     515             : {
     516             :   GEN y;
     517             :   long j, k;
     518             : 
     519       81667 :   if (!d) return gcopy(x);
     520             :   /* d left on stack for efficiency */
     521       80302 :   r = lg(x)-1 - r; /* = dim Im(x) */
     522       80302 :   y = cgetg(r+1,t_MAT);
     523      708559 :   for (j=k=1; j<=r; k++)
     524      628257 :     if (d[k]) gel(y,j++) = gcopy(gel(x,k));
     525       80302 :   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      393329 : indexcompl(GEN v, long n)
     553             : {
     554      393329 :   long i, j, k, m = lg(v) - 1;
     555      393329 :   GEN w = cgetg(n - m + 1, t_VECSMALL);
     556     7449353 :   for (i = j = k = 1; i <= n; i++)
     557     7056024 :     if (j <= m && v[j] == i) j++; else w[k++] = i;
     558      393329 :   return w;
     559             : }
     560             : 
     561             : static GEN
     562      235254 : Flm_rsolve_upper_1(GEN U, GEN B, ulong p)
     563      235254 : { return Flm_Fl_mul(B, Fl_inv(ucoeff(U, 1, 1), p), p); }
     564             : 
     565             : static GEN
     566      696340 : Flm_rsolve_upper_2(GEN U, GEN B, ulong p)
     567             : {
     568      696340 :   ulong a = ucoeff(U, 1, 1), b = ucoeff(U, 1, 2), d = ucoeff(U, 2, 2);
     569      696340 :   ulong D = Fl_mul(a, d, p), Dinv = Fl_inv(D, p);
     570      696344 :   ulong ainv = Fl_mul(d, Dinv, p), dinv = Fl_mul(a, Dinv, p);
     571      696343 :   GEN B1 = rowslice(B, 1, 1);
     572      696328 :   GEN B2 = rowslice(B, 2, 2);
     573      696332 :   GEN X2 = Flm_Fl_mul(B2, dinv, p);
     574      696331 :   GEN X1 = Flm_Fl_mul(Flm_sub(B1, Flm_Fl_mul(X2, b, p), p),
     575             :                       ainv, p);
     576      696331 :   return vconcat(X1, X2);
     577             : }
     578             : 
     579             : /* solve U*X = B,  U upper triangular and invertible */
     580             : static GEN
     581     1684111 : Flm_rsolve_upper(GEN U, GEN B, ulong p)
     582             : {
     583     1684111 :   long n = lg(U) - 1, n1;
     584             :   GEN U2, U11, U12, U22, B1, B2, X1, X2, X;
     585     1684111 :   pari_sp av = avma;
     586             : 
     587     1684111 :   if (n == 0) return B;
     588     1684111 :   if (n == 1) return Flm_rsolve_upper_1(U, B, p);
     589     1448857 :   if (n == 2) return Flm_rsolve_upper_2(U, B, p);
     590      752519 :   n1 = (n + 1)/2;
     591      752519 :   U2 = vecslice(U, n1 + 1, n);
     592      752508 :   U22 = rowslice(U2, n1 + 1, n);
     593      752512 :   B2 = rowslice(B, n1 + 1, n);
     594      752515 :   X2 = Flm_rsolve_upper(U22, B2, p);
     595      752517 :   U12 = rowslice(U2, 1, n1);
     596      752498 :   B1 = rowslice(B, 1, n1);
     597      752520 :   B1 = Flm_sub(B1, Flm_mul(U12, X2, p), p);
     598      752502 :   if (gc_needed(av, 1)) gerepileall(av, 2, &B1, &X2);
     599      752502 :   U11 = matslice(U, 1,n1, 1,n1);
     600      752508 :   X1 = Flm_rsolve_upper(U11, B1, p);
     601      752525 :   X = vconcat(X1, X2);
     602      752526 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     603      752526 :   return X;
     604             : }
     605             : 
     606             : static GEN
     607      268960 : Flm_lsolve_upper_1(GEN U, GEN B, ulong p)
     608      268960 : { return Flm_Fl_mul(B, Fl_inv(ucoeff(U, 1, 1), p), p); }
     609             : 
     610             : static GEN
     611      755136 : Flm_lsolve_upper_2(GEN U, GEN B, ulong p)
     612             : {
     613      755136 :   ulong a = ucoeff(U, 1, 1), b = ucoeff(U, 1, 2), d = ucoeff(U, 2, 2);
     614      755136 :   ulong D = Fl_mul(a, d, p), Dinv = Fl_inv(D, p);
     615      755163 :   ulong ainv = Fl_mul(d, Dinv, p), dinv = Fl_mul(a, Dinv, p);
     616      755161 :   GEN B1 = vecslice(B, 1, 1);
     617      755134 :   GEN B2 = vecslice(B, 2, 2);
     618      755134 :   GEN X1 = Flm_Fl_mul(B1, ainv, p);
     619      755129 :   GEN X2 = Flm_Fl_mul(Flm_sub(B2, Flm_Fl_mul(X1, b, p), p),
     620             :                       dinv, p);
     621      755137 :   return shallowconcat(X1, X2);
     622             : }
     623             : 
     624             : /* solve X*U = B,  U upper triangular and invertible */
     625             : static GEN
     626     1739351 : Flm_lsolve_upper(GEN U, GEN B, ulong p)
     627             : {
     628     1739351 :   long n = lg(U) - 1, n1;
     629             :   GEN U2, U11, U12, U22, B1, B2, X1, X2, X;
     630     1739351 :   pari_sp av = avma;
     631             : 
     632     1739351 :   if (n == 0) return B;
     633     1739351 :   if (n == 1) return Flm_lsolve_upper_1(U, B, p);
     634     1470390 :   if (n == 2) return Flm_lsolve_upper_2(U, B, p);
     635      715251 :   n1 = (n + 1)/2;
     636      715251 :   U2 = vecslice(U, n1 + 1, n);
     637      715238 :   U11 = matslice(U, 1,n1, 1,n1);
     638      715237 :   U12 = rowslice(U2, 1, n1);
     639      715231 :   U22 = rowslice(U2, n1 + 1, n);
     640      715238 :   B1 = vecslice(B, 1, n1);
     641      715238 :   B2 = vecslice(B, n1 + 1, n);
     642      715234 :   X1 = Flm_lsolve_upper(U11, B1, p);
     643      715260 :   B2 = Flm_sub(B2, Flm_mul(X1, U12, p), p);
     644      715236 :   if (gc_needed(av, 1)) gerepileall(av, 3, &B2, &U22, &X1);
     645      715236 :   X2 = Flm_lsolve_upper(U22, B2, p);
     646      715259 :   X = shallowconcat(X1, X2);
     647      715257 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     648      715256 :   return X;
     649             : }
     650             : 
     651             : static GEN
     652     1313191 : Flm_rsolve_lower_unit_2(GEN L, GEN A, ulong p)
     653             : {
     654     1313191 :   GEN X1 = rowslice(A, 1, 1);
     655     1313175 :   GEN X2 = Flm_sub(rowslice(A, 2, 2),
     656     1313175 :                    Flm_Fl_mul(X1, ucoeff(L, 2, 1), p), p);
     657     1313180 :   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     3034613 : Flm_rsolve_lower_unit(GEN L, GEN A, ulong p)
     664             : {
     665     3034613 :   long m = lg(L) - 1, m1, n;
     666             :   GEN L1, L11, L21, L22, A1, A2, X1, X2, X;
     667     3034613 :   pari_sp av = avma;
     668             : 
     669     3034613 :   if (m == 0) return zero_Flm(0, lg(A) - 1);
     670     3034613 :   if (m == 1) return rowslice(A, 1, 1);
     671     2618412 :   if (m == 2) return Flm_rsolve_lower_unit_2(L, A, p);
     672     1305225 :   m1 = (m + 1)/2;
     673     1305225 :   n = nbrows(L);
     674     1305227 :   L1 = vecslice(L, 1, m1);
     675     1305211 :   L11 = rowslice(L1, 1, m1);
     676     1305205 :   L21 = rowslice(L1, m1 + 1, n);
     677     1305206 :   A1 = rowslice(A, 1, m1);
     678     1305207 :   X1 = Flm_rsolve_lower_unit(L11, A1, p);
     679     1305231 :   A2 = rowslice(A, m1 + 1, n);
     680     1305214 :   A2 = Flm_sub(A2, Flm_mul(L21, X1, p), p);
     681     1305216 :   if (gc_needed(av, 1)) gerepileall(av, 2, &A2, &X1);
     682     1305216 :   L22 = matslice(L, m1+1,n, m1+1,m);
     683     1305205 :   X2 = Flm_rsolve_lower_unit(L22, A2, p);
     684     1305232 :   X = vconcat(X1, X2);
     685     1305239 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     686     1305239 :   return X;
     687             : }
     688             : 
     689             : static GEN
     690      709601 : Flm_lsolve_lower_unit_2(GEN L, GEN A, ulong p)
     691             : {
     692      709601 :   GEN X2 = vecslice(A, 2, 2);
     693      709601 :   GEN X1 = Flm_sub(vecslice(A, 1, 1),
     694      709601 :                    Flm_Fl_mul(X2, ucoeff(L, 2, 1), p), p);
     695      709601 :   return shallowconcat(X1, X2);
     696             : }
     697             : 
     698             : /* solve L*X = A,  L square lower triangular with ones on the diagonal */
     699             : static GEN
     700     1826961 : Flm_lsolve_lower_unit(GEN L, GEN A, ulong p)
     701             : {
     702     1826961 :   long m = lg(L) - 1, m1;
     703             :   GEN L1, L2, L11, L21, L22, A1, A2, X1, X2, X;
     704     1826961 :   pari_sp av = avma;
     705             : 
     706     1826961 :   if (m <= 1) return A;
     707     1552291 :   if (m == 2) return Flm_lsolve_lower_unit_2(L, A, p);
     708      842690 :   m1 = (m + 1)/2;
     709      842690 :   L2 = vecslice(L, m1 + 1, m);
     710      842690 :   L22 = rowslice(L2, m1 + 1, m);
     711      842690 :   A2 = vecslice(A, m1 + 1, m);
     712      842690 :   X2 = Flm_lsolve_lower_unit(L22, A2, p);
     713      842690 :   if (gc_needed(av, 1)) X2 = gerepilecopy(av, X2);
     714      842690 :   L1 = vecslice(L, 1, m1);
     715      842690 :   L21 = rowslice(L1, m1 + 1, m);
     716      842690 :   A1 = vecslice(A, 1, m1);
     717      842690 :   A1 = Flm_sub(A1, Flm_mul(X2, L21, p), p);
     718      842690 :   L11 = rowslice(L1, 1, m1);
     719      842690 :   if (gc_needed(av, 1)) gerepileall(av, 3, &A1, &L11, &X2);
     720      842690 :   X1 = Flm_lsolve_lower_unit(L11, A1, p);
     721      842690 :   X = shallowconcat(X1, X2);
     722      842690 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     723      842690 :   return X;
     724             : }
     725             : 
     726             : /* destroy A */
     727             : static long
     728     1014377 : Flm_CUP_gauss(GEN A, GEN *R, GEN *C, GEN *U, GEN *P, ulong p)
     729             : {
     730     1014377 :   long i, j, k, m = nbrows(A), n = lg(A) - 1, pr, pc, u, v;
     731             : 
     732     1014378 :   if (P) *P = identity_perm(n);
     733     1014383 :   *R = cgetg(m + 1, t_VECSMALL);
     734     5391380 :   for (j = 1, pr = 0; j <= n; j++)
     735             :   {
     736     6929572 :     for (pr++, pc = 0; pr <= m; pr++)
     737             :     {
     738     6464217 :       for (k = j; k <= n; k++) { v = ucoeff(A, pr, k); if (!pc && v) pc = k; }
     739     6464217 :       if (pc) break;
     740             :     }
     741     4842296 :     if (!pc) break;
     742     4376953 :     (*R)[j] = pr;
     743     4376953 :     if (pc != j)
     744             :     {
     745     1024093 :       swap(gel(A, j), gel(A, pc));
     746     1024093 :       if (P) lswap((*P)[j], (*P)[pc]);
     747             :     }
     748     4376953 :     u = Fl_inv(ucoeff(A, pr, j), p);
     749    29176170 :     for (i = pr + 1; i <= m; i++)
     750             :     {
     751    24799163 :       v = Fl_mul(ucoeff(A, i, j), u, p);
     752    24799762 :       ucoeff(A, i, j) = v;
     753   125373027 :       for (k = j + 1; k <= n; k++)
     754   201148654 :         ucoeff(A, i, k) = Fl_sub(ucoeff(A, i, k),
     755   100574327 :                                  Fl_mul(ucoeff(A, pr, k), v, p), p);
     756             :     }
     757             :   }
     758     1014427 :   setlg(*R, j);
     759     1014386 :   *C = vecslice(A, 1, j - 1);
     760     1014379 :   if (U) *U = rowpermute(A, *R);
     761     1014389 :   return j - 1;
     762             : }
     763             : 
     764             : static const long Flm_CUP_LIMIT = 8;
     765             : 
     766             : static ulong
     767      907067 : Flm_CUP(GEN A, GEN *R, GEN *C, GEN *U, GEN *P, ulong p)
     768             : {
     769      907067 :   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      907064 :   pari_sp av = avma;
     773             : 
     774      907064 :   if (m < Flm_CUP_LIMIT || n < Flm_CUP_LIMIT)
     775             :     /* destroy A; not called at the outermost recursion level */
     776      593390 :     return Flm_CUP_gauss(A, R, C, U, P, p);
     777      313674 :   m1 = (minss(m, n) + 1)/2;
     778      313672 :   A1 = rowslice(A, 1, m1);
     779      313671 :   A2 = rowslice(A, m1 + 1, m);
     780      313673 :   r1 = Flm_CUP(A1, &R1, &C1, &U1, &P1, p);
     781      313672 :   if (r1 == 0)
     782             :   {
     783        4808 :     r2 = Flm_CUP(A2, &R2, &C2, &U2, &P2, p);
     784        4808 :     *R = cgetg(r2 + 1, t_VECSMALL);
     785        4808 :     for (i = 1; i <= r2; i++) (*R)[i] = R2[i] + m1;
     786        4808 :     *C = vconcat(zero_Flm(m1, r2), C2);
     787        4808 :     *U = U2;
     788        4808 :     *P = P2;
     789        4808 :     r = r2;
     790             :   }
     791             :   else
     792             :   {
     793      308864 :     U11 = vecslice(U1, 1, r1);
     794      308865 :     U12 = vecslice(U1, r1 + 1, n);
     795      308863 :     T21 = vecslicepermute(A2, P1, 1, r1);
     796      308861 :     T22 = vecslicepermute(A2, P1, r1 + 1, n);
     797      308863 :     C21 = Flm_lsolve_upper(U11, T21, p);
     798      308864 :     if (gc_needed(av, 1))
     799           0 :       gerepileall(av, 7, &R1, &C1, &P1, &U11, &U12, &T22, &C21);
     800      308864 :     B2 = Flm_sub(T22, Flm_mul(C21, U12, p), p);
     801      308857 :     r2 = Flm_CUP(B2, &R2, &C2, &U2, &P2, p);
     802      308862 :     r = r1 + r2;
     803      308862 :     *R = cgetg(r + 1, t_VECSMALL);
     804      308863 :     for (i = 1; i <= r1; i++) (*R)[i] = R1[i];
     805      308863 :     for (;      i <= r; i++)  (*R)[i] = R2[i - r1] + m1;
     806      308863 :     *C = shallowconcat(vconcat(C1, C21),
     807             :                        vconcat(zero_Flm(m1, r2), C2));
     808      308863 :     *U = shallowconcat(vconcat(U11, zero_Flm(r2, r1)),
     809             :                        vconcat(vecpermute(U12, P2), U2));
     810      308866 :     *P = cgetg(n + 1, t_VECSMALL);
     811      308862 :     for (i = 1; i <= r1; i++) (*P)[i] = P1[i];
     812      308862 :     for (; i <= n; i++)       (*P)[i] = P1[P2[i - r1] + r1];
     813             :   }
     814      313670 :   if (gc_needed(av, 1)) gerepileall(av, 4, R, C, U, P);
     815      313671 :   return r;
     816             : }
     817             : 
     818             : static ulong
     819      420985 : Flm_echelon_gauss(GEN A, GEN *R, GEN *C, ulong p)
     820      420985 : { return Flm_CUP_gauss(A, R, C, NULL, NULL, p); }
     821             : 
     822             : /* column echelon form */
     823             : static ulong
     824      702135 : Flm_echelon(GEN A, GEN *R, GEN *C, ulong p)
     825             : {
     826      702135 :   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      702135 :   pari_sp av = avma;
     830             : 
     831      702135 :   if (m < Flm_CUP_LIMIT || n < Flm_CUP_LIMIT)
     832      420985 :     return Flm_echelon_gauss(Flm_copy(A), R, C, p);
     833             : 
     834      281150 :   n1 = (n + 1)/2;
     835      281150 :   A1 = vecslice(A, 1, n1);
     836      281150 :   A2 = vecslice(A, n1 + 1, n);
     837      281150 :   r1 = Flm_echelon(A1, &R1, &C1, p);
     838      281150 :   if (!r1) return Flm_echelon(A2, R, C, p);
     839      246872 :   if (r1 == m) { *R = R1; *C = C1; return r1; }
     840             : 
     841      245091 :   R1c = indexcompl(R1, m);
     842      245091 :   C11 = rowpermute(C1, R1);
     843      245091 :   C21 = rowpermute(C1, R1c);
     844      245091 :   A12 = rowpermute(A2, R1);
     845      245091 :   A22 = rowpermute(A2, R1c);
     846      245091 :   M12 = Flm_rsolve_lower_unit(C11, A12, p);
     847      245091 :   B2 = Flm_sub(A22, Flm_mul(C21, M12, p), p);
     848      245091 :   r2 = Flm_echelon(B2, &R2, &C2, p);
     849      245091 :   if (!r2) { *R = R1; *C = C1; r = r1; }
     850             :   else
     851             :   {
     852      232152 :     R2 = perm_mul(R1c, R2);
     853      232152 :     C2 = rowpermute(vconcat(zero_Flm(r1, r2), C2),
     854             :                     perm_inv(vecsmall_concat(R1, R1c)));
     855      232152 :     r = r1 + r2;
     856      232152 :     *R = cgetg(r + 1, t_VECSMALL);
     857      232152 :     *C = cgetg(r + 1, t_MAT);
     858     2798586 :     for (j = j1 = j2 = 1; j <= r; j++)
     859     2566434 :       if (j2 > r2 || (j1 <= r1 && R1[j1] < R2[j2]))
     860             :       {
     861     1331770 :         gel(*C, j) = gel(C1, j1);
     862     1331770 :         (*R)[j] = R1[j1++];
     863             :       }
     864             :       else
     865             :       {
     866     1234664 :         gel(*C, j) = gel(C2, j2);
     867     1234664 :         (*R)[j] = R2[j2++];
     868             :       }
     869             :   }
     870      245091 :   if (gc_needed(av, 1)) gerepileall(av, 2, R, C);
     871      245091 :   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     2369204 : F2v_find_nonzero(GEN x0, GEN mask0, long m)
    1206             : {
    1207     2369204 :   ulong *x = (ulong *)x0+2, *mask = (ulong *)mask0+2, e;
    1208     2369204 :   long i, l = lg(x0)-2;
    1209     4823178 :   for (i = 0; i < l; i++)
    1210             :   {
    1211     4285991 :     e = *x++ & *mask++;
    1212     4285991 :     if (e) return i*BITS_IN_LONG+vals(e)+1;
    1213             :   }
    1214      537187 :   return m+1;
    1215             : }
    1216             : 
    1217             : /* in place, destroy x */
    1218             : GEN
    1219      299394 : F2m_ker_sp(GEN x, long deplin)
    1220             : {
    1221             :   GEN y, c, d;
    1222             :   long i, j, k, r, m, n;
    1223             : 
    1224      299394 :   n = lg(x)-1;
    1225      299394 :   m = mael(x,1,1); r=0;
    1226             : 
    1227      299394 :   d = cgetg(n+1, t_VECSMALL);
    1228      299394 :   c = const_F2v(m);
    1229     2394406 :   for (k=1; k<=n; k++)
    1230             :   {
    1231     2120101 :     GEN xk = gel(x,k);
    1232     2120101 :     j = F2v_find_nonzero(xk, c, m);
    1233     2120103 :     if (j>m)
    1234             :     {
    1235      448447 :       if (deplin) {
    1236       25089 :         GEN c = zero_F2v(n);
    1237      108269 :         for (i=1; i<k; i++)
    1238       83180 :           if (F2v_coeff(xk, d[i]))
    1239       41897 :             F2v_set(c, i);
    1240       25089 :         F2v_set(c, k);
    1241       25089 :         return c;
    1242             :       }
    1243      423358 :       r++; d[k] = 0;
    1244             :     }
    1245             :     else
    1246             :     {
    1247     1671656 :       F2v_clear(c,j); d[k] = j;
    1248     1671655 :       F2v_clear(xk, j);
    1249   130502857 :       for (i=k+1; i<=n; i++)
    1250             :       {
    1251   128831203 :         GEN xi = gel(x,i);
    1252   128831203 :         if (F2v_coeff(xi,j)) F2v_add_inplace(xi, xk);
    1253             :       }
    1254     1671654 :       F2v_set(xk, j);
    1255             :     }
    1256             :   }
    1257      274305 :   if (deplin) return NULL;
    1258             : 
    1259      272870 :   y = zero_F2m_copy(n,r);
    1260      696228 :   for (j=k=1; j<=r; j++,k++)
    1261             :   {
    1262      423358 :     GEN C = gel(y,j); while (d[k]) k++;
    1263    18070204 :     for (i=1; i<k; i++)
    1264    17646846 :       if (d[i] && F2m_coeff(x,d[i],k))
    1265     6770661 :         F2v_set(C,i);
    1266      423358 :     F2v_set(C, k);
    1267             :   }
    1268      272870 :   return y;
    1269             : }
    1270             : 
    1271             : static void /* assume m < p */
    1272     6601371 : _Fl_addmul(GEN b, long k, long i, ulong m, ulong p, ulong pi)
    1273             : {
    1274     6601371 :   uel(b,k) = Fl_addmul_pre(uel(b, k), m, uel(b, i), p, pi);
    1275     6601371 : }
    1276             : static void /* same m = 1 */
    1277      183606 : _Fl_add(GEN b, long k, long i, ulong p)
    1278             : {
    1279      183606 :   uel(b,k) = Fl_add(uel(b,k), uel(b,i), p);
    1280      183606 : }
    1281             : static void /* assume m < p && SMALL_ULONG(p) && (! (b[i] & b[k] & HIGHMASK)) */
    1282    13185168 : _Fl_addmul_OK(GEN b, long k, long i, ulong m, ulong p)
    1283             : {
    1284    13185168 :   uel(b,k) += m * uel(b,i);
    1285    13185168 :   if (uel(b,k) & HIGHMASK) uel(b,k) %= p;
    1286    13185168 : }
    1287             : static void /* assume SMALL_ULONG(p) && (! (b[i] & b[k] & HIGHMASK)) */
    1288     2679967 : _Fl_add_OK(GEN b, long k, long i, ulong p)
    1289             : {
    1290     2679967 :   uel(b,k) += uel(b,i);
    1291     2679967 :   if (uel(b,k) & HIGHMASK) uel(b,k) %= p;
    1292     2679967 : }
    1293             : 
    1294             : static GEN
    1295      119653 : 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      119653 :   n = lg(x)-1;
    1302      119653 :   m=nbrows(x); r=0;
    1303             : 
    1304      119653 :   c = zero_zv(m);
    1305      119653 :   d = cgetg(n+1, t_VECSMALL);
    1306      119653 :   a = 0; /* for gcc -Wall */
    1307      531734 :   for (k=1; k<=n; k++)
    1308             :   {
    1309     1725008 :     for (j=1; j<=m; j++)
    1310     1520949 :       if (!c[j])
    1311             :       {
    1312      964008 :         a = ucoeff(x,j,k) % p;
    1313      964008 :         if (a) break;
    1314             :       }
    1315      430883 :     if (j > m)
    1316             :     {
    1317      204059 :       if (deplin==1) {
    1318       18802 :         c = cgetg(n+1, t_VECSMALL);
    1319       18802 :         for (i=1; i<k; i++) c[i] = ucoeff(x,d[i],k) % p;
    1320       18802 :         c[k] = 1; for (i=k+1; i<=n; i++) c[i] = 0;
    1321       18802 :         return c;
    1322             :       }
    1323      185257 :       r++; d[k] = 0;
    1324             :     }
    1325             :     else
    1326             :     {
    1327      226824 :       ulong piv = p - Fl_inv(a, p); /* -1/a */
    1328      226824 :       c[j] = k; d[k] = j;
    1329      226824 :       ucoeff(x,j,k) = p-1;
    1330      226824 :       if (piv != 1)
    1331      152340 :         for (i=k+1; i<=n; i++) ucoeff(x,j,i) = (piv * ucoeff(x,j,i)) % p;
    1332     1429913 :       for (t=1; t<=m; t++)
    1333             :       {
    1334     1203089 :         if (t == j) continue;
    1335             : 
    1336      976265 :         piv = ( ucoeff(x,t,k) %= p );
    1337      976265 :         if (!piv) continue;
    1338      447480 :         if (piv == 1)
    1339      116122 :           for (i=k+1; i<=n; i++) _Fl_add_OK(gel(x,i),t,j, p);
    1340             :         else
    1341      331358 :           for (i=k+1; i<=n; i++) _Fl_addmul_OK(gel(x,i),t,j,piv, p);
    1342             :       }
    1343             :     }
    1344             :   }
    1345      100851 :   if (deplin==1) return NULL;
    1346             : 
    1347      100844 :   y = cgetg(r+1, t_MAT);
    1348      286101 :   for (j=k=1; j<=r; j++,k++)
    1349             :   {
    1350      185257 :     GEN C = cgetg(n+1, t_VECSMALL);
    1351             : 
    1352      185257 :     gel(y,j) = C; while (d[k]) k++;
    1353      617567 :     for (i=1; i<k; i++)
    1354      432310 :       if (d[i])
    1355      263398 :         uel(C,i) = ucoeff(x,d[i],k) % p;
    1356             :       else
    1357      168912 :         uel(C,i) = 0UL;
    1358      185257 :     uel(C,k) = 1UL; for (i=k+1; i<=n; i++) uel(C,i) = 0UL;
    1359             :   }
    1360      100844 :   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      100844 :   return y;
    1367             : }
    1368             : 
    1369             : /* in place, destroy x */
    1370             : static GEN
    1371      239364 : 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      239364 :   n = lg(x)-1;
    1377      239364 :   if (!n) return cgetg(1,t_MAT);
    1378      239259 :   if (SMALL_ULONG(p)) return Flm_ker_gauss_OK(x, p, deplin);
    1379      119606 :   pi = get_Fl_red(p);
    1380             : 
    1381      119606 :   m=nbrows(x); r=0;
    1382             : 
    1383      119606 :   c = zero_zv(m);
    1384      119606 :   d = cgetg(n+1, t_VECSMALL);
    1385      119606 :   a = 0; /* for gcc -Wall */
    1386      390211 :   for (k=1; k<=n; k++)
    1387             :   {
    1388      772397 :     for (j=1; j<=m; j++)
    1389      669023 :       if (!c[j])
    1390             :       {
    1391      519702 :         a = ucoeff(x,j,k);
    1392      519702 :         if (a) break;
    1393             :       }
    1394      270612 :     if (j > m)
    1395             :     {
    1396      103374 :       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      103367 :       r++; d[k] = 0;
    1403             :     }
    1404             :     else
    1405             :     {
    1406      167238 :       ulong piv = p - Fl_inv(a, p); /* -1/a */
    1407      167238 :       c[j] = k; d[k] = j;
    1408      167238 :       ucoeff(x,j,k) = p-1;
    1409      167238 :       if (piv != 1)
    1410      323813 :         for (i=k+1; i<=n; i++)
    1411      158420 :           ucoeff(x,j,i) = Fl_mul_pre(piv, ucoeff(x,j,i), p, pi);
    1412      703295 :       for (t=1; t<=m; t++)
    1413             :       {
    1414      536057 :         if (t == j) continue;
    1415             : 
    1416      368819 :         piv = ucoeff(x,t,k);
    1417      368819 :         if (!piv) continue;
    1418      201317 :         if (piv == 1)
    1419        8772 :           for (i=k+1; i<=n; i++) _Fl_add(gel(x,i),t,j,p);
    1420             :         else
    1421      192545 :           for (i=k+1; i<=n; i++) _Fl_addmul(gel(x,i),t,j,piv,p, pi);
    1422             :       }
    1423             :     }
    1424             :   }
    1425      119599 :   if (deplin==1) return NULL;
    1426             : 
    1427      119592 :   y = cgetg(r+1, t_MAT);
    1428      222959 :   for (j=k=1; j<=r; j++,k++)
    1429             :   {
    1430      103367 :     GEN C = cgetg(n+1, t_VECSMALL);
    1431             : 
    1432      103367 :     gel(y,j) = C; while (d[k]) k++;
    1433      219745 :     for (i=1; i<k; i++)
    1434      116378 :       if (d[i])
    1435       78049 :         uel(C,i) = ucoeff(x,d[i],k);
    1436             :       else
    1437       38329 :         uel(C,i) = 0UL;
    1438      103367 :     uel(C,k) = 1UL; for (i=k+1; i<=n; i++) uel(C,i) = 0UL;
    1439             :   }
    1440      119592 :   if (deplin == 2) {
    1441      116119 :     GEN pc = cgetg(n - r + 1, t_VECSMALL);  /* indices of pivot columns */
    1442      364275 :     for (i = j = 1; j <= n; j++)
    1443      248156 :       if (d[j]) pc[i++] = j;
    1444      116119 :     return mkvec2(y, pc);
    1445             :   }
    1446        3473 :   return y;
    1447             : }
    1448             : 
    1449             : GEN
    1450       72714 : FpM_intersect(GEN x, GEN y, GEN p)
    1451             : {
    1452       72714 :   pari_sp av = avma;
    1453       72714 :   long j, lx = lg(x);
    1454             :   GEN z;
    1455             : 
    1456       72714 :   if (lx==1 || lg(y)==1) return cgetg(1,t_MAT);
    1457       72714 :   z = FpM_ker(shallowconcat(x,y), p);
    1458       72714 :   for (j=lg(z)-1; j; j--) setlg(gel(z,j),lx);
    1459       72714 :   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          78 : 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      117938 : Flm_ker_echelon(GEN x, ulong p, long pivots) {
    1482      117938 :   pari_sp av = avma;
    1483             :   GEN R, Rc, C, C1, C2, S, K;
    1484      117938 :   long n = lg(x) - 1, r;
    1485      117938 :   r = Flm_echelon(Flm_transpose(x), &R, &C, p);
    1486      117938 :   Rc = indexcompl(R, n);
    1487      117938 :   C1 = rowpermute(C, R);
    1488      117938 :   C2 = rowpermute(C, Rc);
    1489      117938 :   S = Flm_lsolve_lower_unit(C1, C2, p);
    1490      117938 :   K = vecpermute(shallowconcat(Flm_neg(S, p), matid_Flm(n - r)),
    1491             :                  perm_inv(vecsmall_concat(R, Rc)));
    1492      117938 :   K = Flm_transpose(K);
    1493      117938 :   if (pivots)
    1494        3363 :     return gerepilecopy(av, mkvec2(K, R));
    1495      114575 :   return gerepileupto(av, K);
    1496             : }
    1497             : 
    1498             : static GEN
    1499       23643 : Flm_deplin_echelon(GEN x, ulong p) {
    1500       23643 :   pari_sp av = avma;
    1501             :   GEN R, Rc, C, C1, C2, s, v;
    1502       23643 :   long i, n = lg(x) - 1, r;
    1503       23643 :   r = Flm_echelon(Flm_transpose(x), &R, &C, p);
    1504       23643 :   if (r == n) { avma = av; return NULL; }
    1505       23636 :   Rc = indexcompl(R, n);
    1506       23636 :   i = Rc[1];
    1507       23636 :   C1 = rowpermute(C, R);
    1508       23636 :   C2 = rowslice(C, i, i);
    1509       23636 :   s = Flm_row(Flm_lsolve_lower_unit(C1, C2, p), 1);
    1510       23636 :   v = vecpermute(vecsmall_concat(Flv_neg(s, p), vecsmall_ei(n - r, 1)),
    1511             :                  perm_inv(vecsmall_concat(R, Rc)));
    1512       23636 :   return gerepileuptoleaf(av, v);
    1513             : }
    1514             : 
    1515             : static GEN
    1516      380945 : Flm_ker_i(GEN x, ulong p, long deplin, long inplace) {
    1517      380945 :   if (lg(x) - 1 >= Flm_CUP_LIMIT && nbrows(x) >= Flm_CUP_LIMIT)
    1518      141581 :     switch(deplin) {
    1519      114575 :     case 0: return Flm_ker_echelon(x, p, 0);
    1520       23643 :     case 1: return Flm_deplin_echelon(x, p);
    1521        3363 :     case 2: return Flm_ker_echelon(x, p, 1);
    1522             :     }
    1523      239364 :   return Flm_ker_gauss(inplace? x: Flm_copy(x), p, deplin);
    1524             : }
    1525             : 
    1526             : GEN
    1527      315804 : Flm_ker_sp(GEN x, ulong p, long deplin) {
    1528      315804 :   return Flm_ker_i(x, p, deplin, 1);
    1529             : }
    1530             : 
    1531             : GEN
    1532       65141 : Flm_ker(GEN x, ulong p) {
    1533       65141 :   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       55109 : Flm_det_gauss(GEN a, ulong p)
    1604             : {
    1605       55109 :   long i,j,k, s = 1, nbco = lg(a)-1;
    1606       55109 :   ulong pi, q, x = 1;
    1607             : 
    1608       55109 :   if (SMALL_ULONG(p)) return Flm_det_gauss_OK(a, nbco, p);
    1609       53366 :   pi = get_Fl_red(p);
    1610      339099 :   for (i=1; i<nbco; i++)
    1611             :   {
    1612      285815 :     for(k=i; k<=nbco; k++)
    1613      285807 :       if (ucoeff(a,k,i)) break;
    1614      285733 :     if (k > nbco) return ucoeff(a,i,i);
    1615      285733 :     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      285733 :     q = ucoeff(a,i,i);
    1621             : 
    1622      285733 :     x = Fl_mul_pre(x, q, p, pi);
    1623      285738 :     q = Fl_inv(q,p);
    1624     1220740 :     for (k=i+1; k<=nbco; k++)
    1625             :     {
    1626      935013 :       ulong m = ucoeff(a,i,k);
    1627      935013 :       if (!m) continue;
    1628             : 
    1629      892636 :       m = Fl_mul_pre(m, q, p, pi);
    1630     4547452 :       for (j=i+1; j<=nbco; j++)
    1631     3655000 :         ucoeff(a,j,k) = Fl_sub(ucoeff(a,j,k), Fl_mul_pre(m,ucoeff(a,j,i), p, pi), p);
    1632             :     }
    1633             :   }
    1634       53366 :   if (s < 0) x = Fl_neg(x, p);
    1635       53366 :   return Fl_mul(x, ucoeff(a,nbco,nbco), p);
    1636             : }
    1637             : 
    1638             : static ulong
    1639       35271 : Flm_det_CUP(GEN a, ulong p) {
    1640             :   GEN R, C, U, P;
    1641       35271 :   long d, i, n = lg(a) - 1, r;
    1642       35271 :   r = Flm_CUP(a, &R, &C, &U, &P, p);
    1643       35267 :   if (r < n)
    1644          39 :     d = 0;
    1645             :   else {
    1646       35228 :     d = perm_sign(P) == 1? 1: p-1;
    1647      354279 :     for (i = 1; i <= n; i++)
    1648      319049 :       d = Fl_mul(d, ucoeff(U, i, i), p);
    1649             :   }
    1650       35269 :   return d;
    1651             : }
    1652             : 
    1653             : static ulong
    1654       90380 : Flm_det_i(GEN x, ulong p, long inplace) {
    1655       90380 :   pari_sp av = avma;
    1656             :   ulong d;
    1657       90380 :   if (lg(x) - 1 >= Flm_CUP_LIMIT)
    1658       35271 :     d = Flm_det_CUP(x, p);
    1659             :   else
    1660       55109 :     d = Flm_det_gauss(inplace? x: Flm_copy(x), p);
    1661       90375 :   avma = av; return d;
    1662             : }
    1663             : 
    1664             : ulong
    1665       90380 : Flm_det_sp(GEN x, ulong p) {
    1666       90380 :   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      478626 : FpM_init(GEN a, GEN p, ulong *pp)
    1676             : {
    1677      478626 :   if (lgefint(p) == 3)
    1678             :   {
    1679      471077 :     *pp = uel(p,2);
    1680      471077 :     return (*pp==2)? ZM_to_F2m(a): ZM_to_Flm(a, *pp);
    1681             :   }
    1682        7549 :   *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       36637 : F2m_gauss_pivot(GEN x, long *rr)
    1720             : {
    1721             :   GEN c, d;
    1722             :   long i, j, k, r, m, n;
    1723             : 
    1724       36637 :   n = lg(x)-1; if (!n) { *rr=0; return NULL; }
    1725       36637 :   m = mael(x,1,1); r=0;
    1726             : 
    1727       36637 :   d = cgetg(n+1, t_VECSMALL);
    1728       36637 :   c = const_F2v(m);
    1729      285740 :   for (k=1; k<=n; k++)
    1730             :   {
    1731      249103 :     GEN xk = gel(x,k);
    1732      249103 :     j = F2v_find_nonzero(xk, c, m);
    1733      249103 :     if (j>m) { r++; d[k] = 0; }
    1734             :     else
    1735             :     {
    1736      160363 :       F2v_clear(c,j); d[k] = j;
    1737     2164496 :       for (i=k+1; i<=n; i++)
    1738             :       {
    1739     2004133 :         GEN xi = gel(x,i);
    1740     2004133 :         if (F2v_coeff(xi,j)) F2v_add_inplace(xi, xk);
    1741             :       }
    1742             :     }
    1743             :   }
    1744             : 
    1745       36637 :   *rr = r; avma = (pari_sp)d; return d;
    1746             : }
    1747             : 
    1748             : /* Destroy x */
    1749             : static GEN
    1750      166798 : 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      166798 :   n=lg(x)-1; if (!n) { *rr=0; return NULL; }
    1756             : 
    1757      166798 :   m=nbrows(x); r=0;
    1758      166798 :   d=cgetg(n+1,t_VECSMALL);
    1759      166798 :   c = zero_zv(m);
    1760      702719 :   for (k=1; k<=n; k++)
    1761             :   {
    1762     1739345 :     for (j=1; j<=m; j++)
    1763     1642521 :       if (!c[j])
    1764             :       {
    1765      905384 :         ucoeff(x,j,k) %= p;
    1766      905384 :         if (ucoeff(x,j,k)) break;
    1767             :       }
    1768      535921 :     if (j>m) { r++; d[k]=0; }
    1769             :     else
    1770             :     {
    1771      439097 :       ulong piv = p - Fl_inv(ucoeff(x,j,k), p);
    1772      439097 :       c[j]=k; d[k]=j;
    1773     1222664 :       for (i=k+1; i<=n; i++)
    1774      783567 :         ucoeff(x,j,i) = Fl_mul(piv, ucoeff(x,j,i), p);
    1775     3319265 :       for (t=1; t<=m; t++)
    1776     2880168 :         if (!c[t]) /* no pivot on that line yet */
    1777             :         {
    1778     1879005 :           piv = ucoeff(x,t,k);
    1779     1879005 :           if (piv)
    1780             :           {
    1781     1025727 :             ucoeff(x,t,k) = 0;
    1782     3227459 :             for (i=k+1; i<=n; i++)
    1783     4403464 :               ucoeff(x,t,i) = Fl_add(ucoeff(x,t,i),
    1784     2201732 :                                      Fl_mul(piv,ucoeff(x,j,i),p),p);
    1785             :           }
    1786             :         }
    1787      439097 :       for (i=k; i<=n; i++) ucoeff(x,j,i) = 0; /* dummy */
    1788             :     }
    1789             :   }
    1790      166798 :   *rr = r; avma = (pari_sp)d; return d;
    1791             : }
    1792             : 
    1793             : static GEN
    1794       65364 : Flm_pivots_CUP(GEN x, ulong p, long *rr) {
    1795             :   pari_sp av;
    1796       65364 :   long i, n = lg(x) - 1, r;
    1797       65364 :   GEN R, C, U, P, d = zero_zv(n);
    1798       65364 :   av = avma;
    1799       65364 :   r = Flm_CUP(x, &R, &C, &U, &P, p);
    1800      803250 :   for(i = 1; i <= r; i++)
    1801      737886 :     d[P[i]] = R[i];
    1802       65364 :   avma = av;
    1803       65364 :   *rr = n - r;
    1804       65364 :   return d;
    1805             : }
    1806             : 
    1807             : static GEN
    1808      232162 : Flm_pivots(GEN x, ulong p, long *rr, long inplace) {
    1809      232162 :   if (lg(x) - 1 >= Flm_CUP_LIMIT && nbrows(x) >= Flm_CUP_LIMIT)
    1810       65364 :     return Flm_pivots_CUP(x, p, rr);
    1811      166798 :   return Flm_gauss_pivot(inplace? x: Flm_copy(x), p, rr);
    1812             : }
    1813             : 
    1814             : static GEN
    1815        1025 : FpM_gauss_pivot_gen(GEN x, GEN p, long *rr)
    1816             : {
    1817             :   void *E;
    1818        1025 :   const struct bb_field *S = get_Fp_field(&E,p);
    1819        1025 :   return gen_Gauss_pivot(x, rr, E, S);
    1820             : }
    1821             : static GEN
    1822      140572 : FpM_gauss_pivot(GEN x, GEN p, long *rr)
    1823             : {
    1824             :   ulong pp;
    1825      140572 :   if (lg(x)==1) { *rr = 0; return NULL; }
    1826      139207 :   x = FpM_init(x, p, &pp);
    1827      139207 :   switch(pp)
    1828             :   {
    1829        1025 :   case 0: return FpM_gauss_pivot_gen(x, p, rr);
    1830       36546 :   case 2: return F2m_gauss_pivot(x, rr);
    1831      101636 :   default:return Flm_pivots(x, pp, rr, 1);
    1832             :   }
    1833             : }
    1834             : 
    1835             : GEN
    1836       81506 : FpM_image(GEN x, GEN p)
    1837             : {
    1838             :   long r;
    1839       81506 :   GEN d = FpM_gauss_pivot(x,p,&r); /* d left on stack for efficiency */
    1840       81506 :   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        2186 : Flm_rank(GEN x, ulong p)
    1870             : {
    1871        2186 :   pari_sp av = avma;
    1872             :   long r;
    1873        2186 :   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        2151 :   (void) Flm_pivots(x, p, &r, 0);
    1879        2151 :   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      272729 : FpM_ker_i(GEN x, GEN p, long deplin)
    2208             : {
    2209      272729 :   pari_sp av = avma;
    2210             :   ulong pp;
    2211             :   GEN y;
    2212             : 
    2213      272729 :   if (lg(x)==1) return cgetg(1,t_MAT);
    2214      272729 :   x = FpM_init(x, p, &pp);
    2215      272729 :   switch(pp)
    2216             :   {
    2217        2189 :   case 0: return FpM_ker_gen(x,p,deplin);
    2218             :   case 2:
    2219       74330 :     y = F2m_ker_sp(x, deplin);
    2220       74330 :     if (!y) return y;
    2221       74330 :     y = deplin? F2c_to_ZC(y): F2m_to_ZM(y);
    2222       74330 :     return gerepileupto(av, y);
    2223             :   default:
    2224      196210 :     y = Flm_ker_sp(x, pp, deplin);
    2225      196210 :     if (!y) return y;
    2226      196210 :     y = deplin? Flc_to_ZC(y): Flm_to_ZM(y);
    2227      196210 :     return gerepileupto(av, y);
    2228             :   }
    2229             : }
    2230             : 
    2231             : GEN
    2232      199630 : FpM_ker(GEN x, GEN p) { return FpM_ker_i(x,p,0); }
    2233             : 
    2234             : GEN
    2235       67289 : 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        8491 : FqM_ker_i(GEN x, GEN T, GEN p, long deplin)
    2246             : {
    2247        8491 :   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        8414 : 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      575019 : approx_0(GEN x, GEN y)
    2392             : {
    2393      575019 :   long tx = typ(x);
    2394      575019 :   if (tx == t_COMPLEX)
    2395          42 :     return approx_0(gel(x,1), y) && approx_0(gel(x,2), y);
    2396      575124 :   return gequal0(x) ||
    2397      397815 :          (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      587724 : gauss_get_pivot_max(GEN X, GEN X0, long ix, GEN c)
    2403             : {
    2404      587724 :   GEN p, r, x = gel(X,ix), x0 = gel(X0,ix);
    2405      587724 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2406      587724 :   if (c)
    2407             :   {
    2408       80990 :     for (i=1; i<lx; i++)
    2409       53025 :       if (!c[i])
    2410             :       {
    2411       27244 :         long e = gexpo(gel(x,i));
    2412       27244 :         if (e > ex) { ex = e; k = i; }
    2413             :       }
    2414             :   }
    2415             :   else
    2416             :   {
    2417     1923403 :     for (i=ix; i<lx; i++)
    2418             :     {
    2419     1363644 :       long e = gexpo(gel(x,i));
    2420     1363644 :       if (e > ex) { ex = e; k = i; }
    2421             :     }
    2422             :   }
    2423      587724 :   if (!k) return lx;
    2424      574970 :   p = gel(x,k);
    2425      574970 :   r = gel(x0,k); if (isrationalzero(r)) r = x0;
    2426      574970 :   return approx_0(p, r)? lx: k;
    2427             : }
    2428             : static long
    2429       63651 : gauss_get_pivot_padic(GEN X, GEN p, long ix, GEN c)
    2430             : {
    2431       63651 :   GEN x = gel(X, ix);
    2432       63651 :   long i, k = 0, ex = (long)HIGHVALPBIT, lx = lg(x);
    2433       63651 :   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      448105 :     for (i=ix; i<lx; i++)
    2445      384580 :       if (!gequal0(gel(x,i)))
    2446             :       {
    2447      185268 :         long e = gvaluation(gel(x,i), p);
    2448      185268 :         if (e < ex) { ex = e; k = i; }
    2449             :       }
    2450             :   }
    2451       63651 :   return k? k: lx;
    2452             : }
    2453             : static long
    2454        4319 : gauss_get_pivot_NZ(GEN X, GEN x0/*unused*/, long ix, GEN c)
    2455             : {
    2456        4319 :   GEN x = gel(X, ix);
    2457        4319 :   long i, lx = lg(x);
    2458             :   (void)x0;
    2459        4319 :   if (c)
    2460             :   {
    2461       11550 :     for (i=1; i<lx; i++)
    2462       10724 :       if (!c[i] && !gequal0(gel(x,i))) return i;
    2463             :   }
    2464             :   else
    2465             :   {
    2466        2639 :     for (i=ix; i<lx; i++)
    2467        2625 :       if (!gequal0(gel(x,i))) return i;
    2468             :   }
    2469         840 :   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      191465 : get_pivot_fun(GEN x, GEN x0, GEN *data)
    2479             : {
    2480      191465 :   long i, j, hx, lx = lg(x);
    2481      191465 :   int res = t_INT;
    2482      191465 :   GEN p = NULL;
    2483             : 
    2484      191465 :   *data = NULL;
    2485      191465 :   if (lx == 1) return &gauss_get_pivot_NZ;
    2486      191430 :   hx = lgcols(x);
    2487      857454 :   for (j=1; j<lx; j++)
    2488             :   {
    2489      666052 :     GEN xj = gel(x,j);
    2490     3634162 :     for (i=1; i<hx; i++)
    2491             :     {
    2492     2968138 :       GEN c = gel(xj,i);
    2493     2968138 :       switch(typ(c))
    2494             :       {
    2495             :         case t_REAL:
    2496     1574598 :           res = t_REAL;
    2497     1574598 :           break;
    2498             :         case t_COMPLEX:
    2499         266 :           if (typ(gel(c,1)) == t_REAL || typ(gel(c,2)) == t_REAL) res = t_REAL;
    2500         266 :           break;
    2501             :         case t_INT: case t_INTMOD: case t_FRAC: case t_FFELT: case t_QUAD:
    2502             :         case t_POLMOD: /* exact types */
    2503     1231847 :           break;
    2504             :         case t_PADIC:
    2505      161399 :           p = gel(c,2);
    2506      161399 :           res = t_PADIC;
    2507      161399 :           break;
    2508          28 :         default: return &gauss_get_pivot_NZ;
    2509             :       }
    2510             :     }
    2511             :   }
    2512      191402 :   switch(res)
    2513             :   {
    2514      181525 :     case t_REAL: *data = x0; return &gauss_get_pivot_max;
    2515        8281 :     case t_PADIC: *data = p; return &gauss_get_pivot_padic;
    2516        1596 :     default: return &gauss_get_pivot_NZ;
    2517             :   }
    2518             : }
    2519             : 
    2520             : static GEN
    2521      186261 : get_col(GEN a, GEN b, GEN p, long li)
    2522             : {
    2523      186261 :   GEN u = cgetg(li+1,t_COL);
    2524             :   long i, j;
    2525             : 
    2526      186261 :   gel(u,li) = gdiv(gel(b,li), p);
    2527      762991 :   for (i=li-1; i>0; i--)
    2528             :   {
    2529      576730 :     pari_sp av = avma;
    2530      576730 :     GEN m = gel(b,i);
    2531      576730 :     for (j=i+1; j<=li; j++) m = gsub(m, gmul(gcoeff(a,i,j), gel(u,j)));
    2532      576730 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(a,i,i)));
    2533             :   }
    2534      186261 :   return u;
    2535             : }
    2536             : /* assume 0 <= a[i,j] < p */
    2537             : static GEN
    2538      348555 : Fl_get_col_OK(GEN a, GEN b, long li, ulong p)
    2539             : {
    2540      348555 :   GEN u = cgetg(li+1,t_VECSMALL);
    2541      348555 :   ulong m = uel(b,li) % p;
    2542             :   long i,j;
    2543             : 
    2544      348555 :   uel(u,li) = (m * ucoeff(a,li,li)) % p;
    2545     3954742 :   for (i = li-1; i > 0; i--)
    2546             :   {
    2547     3606187 :     m = p - uel(b,i)%p;
    2548    31884333 :     for (j = i+1; j <= li; j++) {
    2549    28278146 :       if (m & HIGHBIT) m %= p;
    2550    28278146 :       m += ucoeff(a,i,j) * uel(u,j); /* 0 <= u[j] < p */
    2551             :     }
    2552     3606187 :     m %= p;
    2553     3606187 :     if (m) m = ((p-m) * ucoeff(a,i,i)) % p;
    2554     3606187 :     uel(u,i) = m;
    2555             :   }
    2556      348555 :   return u;
    2557             : }
    2558             : static GEN
    2559     1520700 : Fl_get_col(GEN a, GEN b, long li, ulong p)
    2560             : {
    2561     1520700 :   GEN u = cgetg(li+1,t_VECSMALL);
    2562     1520700 :   ulong m = uel(b,li) % p;
    2563             :   long i,j;
    2564             : 
    2565     1520700 :   uel(u,li) = Fl_mul(m, ucoeff(a,li,li), p);
    2566     4487380 :   for (i=li-1; i>0; i--)
    2567             :   {
    2568     2966680 :     m = b[i]%p;
    2569     8102037 :     for (j = i+1; j <= li; j++)
    2570     5135357 :       m = Fl_sub(m, Fl_mul(ucoeff(a,i,j), uel(u,j), p), p);
    2571     2966680 :     if (m) m = Fl_mul(m, ucoeff(a,i,i), p);
    2572     2966680 :     uel(u,i) = m;
    2573             :   }
    2574     1520700 :   return u;
    2575             : }
    2576             : 
    2577             : /* bk -= m * bi */
    2578             : static void
    2579     2835818 : _submul(GEN b, long k, long i, GEN m)
    2580             : {
    2581     2835818 :   gel(b,k) = gsub(gel(b,k), gmul(m, gel(b,i)));
    2582     2835818 : }
    2583             : static int
    2584      772644 : init_gauss(GEN a, GEN *b, long *aco, long *li, int *iscol)
    2585             : {
    2586      772644 :   *iscol = *b ? (typ(*b) == t_COL): 0;
    2587      772644 :   *aco = lg(a) - 1;
    2588      772644 :   if (!*aco) /* a empty */
    2589             :   {
    2590          70 :     if (*b && lg(*b) != 1) pari_err_DIM("gauss");
    2591          70 :     *li = 0; return 0;
    2592             :   }
    2593      772574 :   *li = nbrows(a);
    2594      772574 :   if (*li < *aco) pari_err_INV("gauss [no left inverse]", a);
    2595      772574 :   if (*b)
    2596             :   {
    2597      763989 :     switch(typ(*b))
    2598             :     {
    2599             :       case t_MAT:
    2600       83902 :         if (lg(*b) == 1) return 0;
    2601       83902 :         *b = RgM_shallowcopy(*b);
    2602       83902 :         break;
    2603             :       case t_COL:
    2604      680087 :         *b = mkmat( leafcopy(*b) );
    2605      680087 :         break;
    2606           0 :       default: pari_err_TYPE("gauss",*b);
    2607             :     }
    2608      763989 :     if (nbrows(*b) != *li) pari_err_DIM("gauss");
    2609             :   }
    2610             :   else
    2611        8585 :     *b = matid(*li);
    2612      772574 :   return 1;
    2613             : }
    2614             : 
    2615             : static GEN Flm_inv_sp(GEN a, ulong *detp, ulong p);
    2616             : static GEN
    2617       28007 : RgM_inv_QM(GEN M)
    2618             : {
    2619       28007 :   pari_sp av = avma;
    2620       28007 :   GEN den, cM, pM = Q_primitive_part(M, &cM);
    2621       28007 :   GEN b = ZM_inv(pM, &den);
    2622       28000 :   if (!b) { avma = av; return NULL; }
    2623       27993 :   if (cM) den = gmul(den, cM);
    2624       27993 :   if (!gequal1(den)) b = ZM_Q_mul(b, ginv(den));
    2625       27993 :   return gerepileupto(av, b);
    2626             : }
    2627             : 
    2628             : static GEN
    2629         112 : RgM_inv_FpM(GEN a, GEN p)
    2630             : {
    2631             :   ulong pp;
    2632         112 :   a = RgM_Fp_init(a, p, &pp);
    2633         112 :   switch(pp)
    2634             :   {
    2635             :   case 0:
    2636          35 :     a = FpM_inv(a,p);
    2637          35 :     if (a) a = FpM_to_mod(a, p);
    2638          35 :     break;
    2639             :   case 2:
    2640          35 :     a = F2m_inv(a);
    2641          35 :     if (a) a = F2m_to_mod(a);
    2642          35 :     break;
    2643             :   default:
    2644          42 :     a = Flm_inv_sp(a, NULL, pp);
    2645          42 :     if (a) a = Flm_to_mod(a, pp);
    2646             :   }
    2647         112 :   return a;
    2648             : }
    2649             : 
    2650             : static GEN
    2651          42 : RgM_inv_FqM(GEN x, GEN pol, GEN p)
    2652             : {
    2653          42 :   pari_sp av = avma;
    2654          42 :   GEN T = RgX_to_FpX(pol, p);
    2655          42 :   GEN b = FqM_inv(RgM_to_FqM(x, T, p), T, p);
    2656          42 :   if (!b) { avma = av; return NULL; }
    2657          28 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    2658             : }
    2659             : 
    2660             : #define code(t1,t2) ((t1 << 6) | t2)
    2661             : static GEN
    2662       44578 : RgM_inv_fast(GEN x)
    2663             : {
    2664             :   GEN p, pol;
    2665             :   long pa;
    2666       44578 :   long t = RgM_type(x, &p,&pol,&pa);
    2667       44578 :   switch(t)
    2668             :   {
    2669             :     case t_INT:    /* Fall back */
    2670       28007 :     case t_FRAC:   return RgM_inv_QM(x);
    2671         147 :     case t_FFELT:  return FFM_inv(x, pol);
    2672         112 :     case t_INTMOD: return RgM_inv_FpM(x, p);
    2673             :     case code(t_POLMOD, t_INTMOD):
    2674          42 :                    return RgM_inv_FqM(x, pol, p);
    2675       16270 :     default:       return gen_0;
    2676             :   }
    2677             : }
    2678             : #undef code
    2679             : 
    2680             : static GEN
    2681          49 : RgM_RgC_solve_FpC(GEN a, GEN b, GEN p)
    2682             : {
    2683          49 :   pari_sp av = avma;
    2684             :   ulong pp;
    2685          49 :   a = RgM_Fp_init(a, p, &pp);
    2686          49 :   switch(pp)
    2687             :   {
    2688             :   case 0:
    2689          14 :     b = RgC_to_FpC(b, p);
    2690          14 :     a = FpM_FpC_gauss(a,b,p);
    2691          14 :     return a ? gerepileupto(av, FpC_to_mod(a, p)): NULL;
    2692             :   case 2:
    2693          14 :     b = RgV_to_F2v(b);
    2694          14 :     a = F2m_F2c_gauss(a,b);
    2695          14 :     return a ? gerepileupto(av, F2c_to_mod(a)): NULL;
    2696             :   default:
    2697          21 :     b = RgV_to_Flv(b, pp);
    2698          21 :     a = Flm_Flc_gauss(a, b, pp);
    2699          21 :     return a ? gerepileupto(av, Flc_to_mod(a, pp)): NULL;
    2700             :   }
    2701             : }
    2702             : 
    2703             : static GEN
    2704          98 : RgM_solve_FpM(GEN a, GEN b, GEN p)
    2705             : {
    2706          98 :   pari_sp av = avma;
    2707             :   ulong pp;
    2708          98 :   a = RgM_Fp_init(a, p, &pp);
    2709          98 :   switch(pp)
    2710             :   {
    2711             :   case 0:
    2712          35 :     b = RgM_to_FpM(b, p);
    2713          35 :     a = FpM_gauss(a,b,p);
    2714          35 :     return a ? gerepileupto(av, FpM_to_mod(a, p)): NULL;
    2715             :   case 2:
    2716          21 :     b = RgM_to_F2m(b);
    2717          21 :     a = F2m_gauss(a,b);
    2718          21 :     return a ? gerepileupto(av, F2m_to_mod(a)): NULL;
    2719             :   default:
    2720          42 :     b = RgM_to_Flm(b, pp);
    2721          42 :     a = Flm_gauss(a,b,pp);
    2722          42 :     return a ? gerepileupto(av, Flm_to_mod(a, pp)): NULL;
    2723             :   }
    2724             : }
    2725             : 
    2726             : /* Gaussan Elimination. If a is square, return a^(-1)*b;
    2727             :  * if a has more rows than columns and b is NULL, return c such that c a = Id.
    2728             :  * a is a (not necessarily square) matrix
    2729             :  * b is a matrix or column vector, NULL meaning: take the identity matrix,
    2730             :  *   effectively returning the inverse of a
    2731             :  * If a and b are empty, the result is the empty matrix.
    2732             :  *
    2733             :  * li: number of rows of a and b
    2734             :  * aco: number of columns of a
    2735             :  * bco: number of columns of b (if matrix)
    2736             :  */
    2737             : static GEN
    2738      267037 : RgM_solve_basecase(GEN a, GEN b)
    2739             : {
    2740      267037 :   pari_sp av = avma;
    2741             :   long i, j, k, li, bco, aco;
    2742             :   int iscol;
    2743             :   pivot_fun pivot;
    2744             :   GEN p, u, data;
    2745             : 
    2746      267037 :   avma = av;
    2747             : 
    2748      267037 :   if (lg(a)-1 == 2 && nbrows(a) == 2) {
    2749             :     /* 2x2 matrix, start by inverting a */
    2750       99139 :     GEN u = gcoeff(a,1,1), v = gcoeff(a,1,2);
    2751       99139 :     GEN w = gcoeff(a,2,1), x = gcoeff(a,2,2);
    2752       99139 :     GEN D = gsub(gmul(u,x), gmul(v,w)), ainv;
    2753       99139 :     if (gequal0(D)) return NULL;
    2754       99139 :     ainv = mkmat2(mkcol2(x, gneg(w)), mkcol2(gneg(v), u));
    2755       99139 :     ainv = gmul(ainv, ginv(D));
    2756       99139 :     if (b) ainv = gmul(ainv, b);
    2757       99139 :     return gerepileupto(av, ainv);
    2758             :   }
    2759             : 
    2760      167898 :   if (!init_gauss(a, &b, &aco, &li, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    2761      167898 :   pivot = get_pivot_fun(a, a, &data);
    2762      167898 :   a = RgM_shallowcopy(a);
    2763      167898 :   bco = lg(b)-1;
    2764      167898 :   if(DEBUGLEVEL>4) err_printf("Entering gauss\n");
    2765             : 
    2766      167898 :   p = NULL; /* gcc -Wall */
    2767      555101 :   for (i=1; i<=aco; i++)
    2768             :   {
    2769             :     /* k is the line where we find the pivot */
    2770      555101 :     k = pivot(a, data, i, NULL);
    2771      555101 :     if (k > li) return NULL;
    2772      555087 :     if (k != i)
    2773             :     { /* exchange the lines s.t. k = i */
    2774      123020 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    2775      123020 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    2776             :     }
    2777      555087 :     p = gcoeff(a,i,i);
    2778      555087 :     if (i == aco) break;
    2779             : 
    2780     1181522 :     for (k=i+1; k<=li; k++)
    2781             :     {
    2782      794319 :       GEN m = gcoeff(a,k,i);
    2783      794319 :       if (!gequal0(m))
    2784             :       {
    2785      629243 :         m = gdiv(m,p);
    2786      629243 :         for (j=i+1; j<=aco; j++) _submul(gel(a,j),k,i,m);
    2787      629243 :         for (j=1;   j<=bco; j++) _submul(gel(b,j),k,i,m);
    2788             :       }
    2789             :     }
    2790      387203 :     if (gc_needed(av,1))
    2791             :     {
    2792          62 :       if(DEBUGMEM>1) pari_warn(warnmem,"gauss. i=%ld",i);
    2793          62 :       gerepileall(av,2, &a,&b);
    2794             :     }
    2795             :   }
    2796             : 
    2797      167884 :   if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
    2798      167884 :   u = cgetg(bco+1,t_MAT);
    2799      167884 :   for (j=1; j<=bco; j++) gel(u,j) = get_col(a,gel(b,j),p,aco);
    2800      167884 :   return gerepilecopy(av, iscol? gel(u,1): u);
    2801             : }
    2802             : 
    2803             : static GEN
    2804      261198 : RgM_RgC_solve_fast(GEN x, GEN y)
    2805             : {
    2806             :   GEN p, pol;
    2807             :   long pa;
    2808      261198 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    2809      261198 :   switch(t)
    2810             :   {
    2811       13160 :     case t_INT:    return ZM_gauss(x, y);
    2812          42 :     case t_FRAC:   return QM_gauss(x, y);
    2813          49 :     case t_INTMOD: return RgM_RgC_solve_FpC(x, y, p);
    2814          56 :     case t_FFELT:  return FFM_FFC_gauss(x, y, pol);
    2815      247891 :     default:       return gen_0;
    2816             :   }
    2817             : }
    2818             : 
    2819             : static GEN
    2820       79353 : RgM_solve_fast(GEN x, GEN y)
    2821             : {
    2822             :   GEN p, pol;
    2823             :   long pa;
    2824       79353 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    2825       79353 :   switch(t)
    2826             :   {
    2827       76309 :     case t_INT:    return ZM_gauss(x, y);
    2828           7 :     case t_FRAC:   return QM_gauss(x, y);
    2829          98 :     case t_INTMOD: return RgM_solve_FpM(x, y, p);
    2830          63 :     case t_FFELT:  return FFM_gauss(x, y, pol);
    2831        2876 :     default:       return gen_0;
    2832             :   }
    2833             : }
    2834             : 
    2835             : GEN
    2836      340551 : RgM_solve(GEN a, GEN b)
    2837             : {
    2838      340551 :   pari_sp av = avma;
    2839             :   GEN u;
    2840      340551 :   if (!b) return RgM_inv(a);
    2841      340551 :   u = typ(b)==t_MAT ? RgM_solve_fast(a, b): RgM_RgC_solve_fast(a, b);
    2842      340551 :   if (!u) { avma = av; return u; }
    2843      340453 :   if (u != gen_0) return u;
    2844      250767 :   return RgM_solve_basecase(a, b);
    2845             : }
    2846             : 
    2847             : GEN
    2848       44578 : RgM_inv(GEN a)
    2849             : {
    2850       44578 :   GEN b = RgM_inv_fast(a);
    2851       44571 :   return b==gen_0? RgM_solve_basecase(a, NULL): b;
    2852             : }
    2853             : 
    2854             : /* assume dim A >= 1, A invertible + upper triangular  */
    2855             : static GEN
    2856      329568 : RgM_inv_upper_ind(GEN A, long index)
    2857             : {
    2858      329568 :   long n = lg(A)-1, i = index, j;
    2859      329568 :   GEN u = zerocol(n);
    2860      329568 :   gel(u,i) = ginv(gcoeff(A,i,i));
    2861     1364941 :   for (i--; i>0; i--)
    2862             :   {
    2863     1035373 :     pari_sp av = avma;
    2864     1035373 :     GEN m = gneg(gmul(gcoeff(A,i,i+1),gel(u,i+1))); /* j = i+1 */
    2865     1035373 :     for (j=i+2; j<=n; j++) m = gsub(m, gmul(gcoeff(A,i,j),gel(u,j)));
    2866     1035373 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(A,i,i)));
    2867             :   }
    2868      329568 :   return u;
    2869             : }
    2870             : GEN
    2871       58618 : RgM_inv_upper(GEN A)
    2872             : {
    2873             :   long i, l;
    2874       58618 :   GEN B = cgetg_copy(A, &l);
    2875       58618 :   for (i = 1; i < l; i++) gel(B,i) = RgM_inv_upper_ind(A, i);
    2876       58618 :   return B;
    2877             : }
    2878             : 
    2879             : /* assume dim A >= 1, A invertible + upper triangular, 1s on diagonal  */
    2880             : static GEN
    2881          98 : FpM_inv_upper_1_ind(GEN A, long index, GEN p)
    2882             : {
    2883          98 :   long n = lg(A)-1, i = index, j;
    2884          98 :   GEN u = zerocol(n);
    2885          98 :   gel(u,i) = gen_1;
    2886         567 :   for (i--; i>0; i--)
    2887             :   {
    2888         469 :     pari_sp av = avma;
    2889         469 :     GEN m = negi(mulii(gcoeff(A,i,i+1),gel(u,i+1))); /* j = i+1 */
    2890         469 :     for (j=i+2; j<=n; j++) m = subii(m, mulii(gcoeff(A,i,j),gel(u,j)));
    2891         469 :     gel(u,i) = gerepileuptoint(av, modii(m,p));
    2892             :   }
    2893          98 :   return u;
    2894             : }
    2895             : static GEN
    2896          14 : FpM_inv_upper_1(GEN A, GEN p)
    2897             : {
    2898             :   long i, l;
    2899          14 :   GEN B = cgetg_copy(A, &l);
    2900          14 :   for (i = 1; i < l; i++) gel(B,i) = FpM_inv_upper_1_ind(A, i, p);
    2901          14 :   return B;
    2902             : }
    2903             : /* assume dim A >= 1, A invertible + upper triangular, 1s on diagonal,
    2904             :  * reduced mod p */
    2905             : static GEN
    2906          28 : Flm_inv_upper_1_ind(GEN A, long index, ulong p)
    2907             : {
    2908          28 :   long n = lg(A)-1, i = index, j;
    2909          28 :   GEN u = const_vecsmall(n, 0);
    2910          28 :   u[i] = 1;
    2911          28 :   if (SMALL_ULONG(p))
    2912          21 :     for (i--; i>0; i--)
    2913             :     {
    2914           7 :       ulong m = ucoeff(A,i,i+1) * uel(u,i+1); /* j = i+1 */
    2915           7 :       for (j=i+2; j<=n; j++)
    2916             :       {
    2917           0 :         if (m & HIGHMASK) m %= p;
    2918           0 :         m += ucoeff(A,i,j) * uel(u,j);
    2919             :       }
    2920           7 :       u[i] = Fl_neg(m % p, p);
    2921             :     }
    2922             :   else
    2923          21 :     for (i--; i>0; i--)
    2924             :     {
    2925           7 :       ulong m = Fl_mul(ucoeff(A,i,i+1),uel(u,i+1), p); /* j = i+1 */
    2926           7 :       for (j=i+2; j<=n; j++) m = Fl_add(m, Fl_mul(ucoeff(A,i,j),uel(u,j),p), p);
    2927           7 :       u[i] = Fl_neg(m, p);
    2928             :     }
    2929          28 :   return u;
    2930             : }
    2931             : static GEN
    2932          14 : F2m_inv_upper_1_ind(GEN A, long index)
    2933             : {
    2934          14 :   pari_sp av = avma;
    2935          14 :   long n = lg(A)-1, i = index, j;
    2936          14 :   GEN u = const_vecsmall(n, 0);
    2937          14 :   u[i] = 1;
    2938          21 :   for (i--; i>0; i--)
    2939             :   {
    2940           7 :     ulong m = F2m_coeff(A,i,i+1) & uel(u,i+1); /* j = i+1 */
    2941           7 :     for (j=i+2; j<=n; j++) m ^= F2m_coeff(A,i,j) & uel(u,j);
    2942           7 :     u[i] = m & 1;
    2943             :   }
    2944          14 :   return gerepileuptoleaf(av, Flv_to_F2v(u));
    2945             : }
    2946             : static GEN
    2947          14 : Flm_inv_upper_1(GEN A, ulong p)
    2948             : {
    2949             :   long i, l;
    2950          14 :   GEN B = cgetg_copy(A, &l);
    2951          14 :   for (i = 1; i < l; i++) gel(B,i) = Flm_inv_upper_1_ind(A, i, p);
    2952          14 :   return B;
    2953             : }
    2954             : static GEN
    2955           7 : F2m_inv_upper_1(GEN A)
    2956             : {
    2957             :   long i, l;
    2958           7 :   GEN B = cgetg_copy(A, &l);
    2959           7 :   for (i = 1; i < l; i++) gel(B,i) = F2m_inv_upper_1_ind(A, i);
    2960           7 :   return B;
    2961             : }
    2962             : 
    2963             : static GEN
    2964      972580 : split_realimag_col(GEN z, long r1, long r2)
    2965             : {
    2966      972580 :   long i, ru = r1+r2;
    2967      972580 :   GEN x = cgetg(ru+r2+1,t_COL), y = x + r2;
    2968     2913159 :   for (i=1; i<=r1; i++) {
    2969     1940579 :     GEN a = gel(z,i);
    2970     1940579 :     if (typ(a) == t_COMPLEX) a = gel(a,1); /* paranoia: a should be real */
    2971     1940579 :     gel(x,i) = a;
    2972             :   }
    2973     1611775 :   for (   ; i<=ru; i++) {
    2974      639195 :     GEN b, a = gel(z,i);
    2975      639195 :     if (typ(a) == t_COMPLEX) { b = gel(a,2); a = gel(a,1); } else b = gen_0;
    2976      639195 :     gel(x,i) = a;
    2977      639195 :     gel(y,i) = b;
    2978             :   }
    2979      972580 :   return x;
    2980             : }
    2981             : GEN
    2982      510942 : split_realimag(GEN x, long r1, long r2)
    2983             : {
    2984             :   long i,l; GEN y;
    2985      510942 :   if (typ(x) == t_COL) return split_realimag_col(x,r1,r2);
    2986      251970 :   y = cgetg_copy(x, &l);
    2987      251970 :   for (i=1; i<l; i++) gel(y,i) = split_realimag_col(gel(x,i), r1, r2);
    2988      251970 :   return y;
    2989             : }
    2990             : 
    2991             : /* assume M = (r1+r2) x (r1+2r2) matrix and y compatible vector or matrix
    2992             :  * r1 first lines of M,y are real. Solve the system obtained by splitting
    2993             :  * real and imaginary parts. */
    2994             : GEN
    2995      248534 : RgM_solve_realimag(GEN M, GEN y)
    2996             : {
    2997      248534 :   long l = lg(M), r2 = l - lgcols(M), r1 = l-1 - 2*r2;
    2998      248534 :   return RgM_solve(split_realimag(M, r1,r2),
    2999             :                    split_realimag(y, r1,r2));
    3000             : }
    3001             : 
    3002             : GEN
    3003         420 : gauss(GEN a, GEN b)
    3004             : {
    3005             :   GEN z;
    3006         420 :   long t = typ(b);
    3007         420 :   if (typ(a)!=t_MAT) pari_err_TYPE("gauss",a);
    3008         420 :   if (t!=t_COL && t!=t_MAT) pari_err_TYPE("gauss",b);
    3009         420 :   z = RgM_solve(a,b);
    3010         420 :   if (!z) pari_err_INV("gauss",a);
    3011         315 :   return z;
    3012             : }
    3013             : 
    3014             : static GEN
    3015      119618 : F2_get_col(GEN b, GEN d, long li, long aco)
    3016             : {
    3017      119618 :   long i, l = nbits2lg(aco);
    3018      119618 :   GEN u = cgetg(l, t_VECSMALL);
    3019      119618 :   u[1] = aco;
    3020     1644528 :   for (i = 1; i <= li; i++)
    3021     1524910 :     if (d[i]) /* d[i] can still be 0 if li > aco */
    3022             :     {
    3023     1524861 :       if (F2v_coeff(b, i))
    3024      510072 :         F2v_set(u, d[i]);
    3025             :       else
    3026     1014789 :         F2v_clear(u, d[i]);
    3027             :     }
    3028      119618 :   return u;
    3029             : }
    3030             : 
    3031             : /* destroy a, b */
    3032             : static GEN
    3033       18863 : F2m_gauss_sp(GEN a, GEN b)
    3034             : {
    3035       18863 :   long i, j, k, l, li, bco, aco = lg(a)-1;
    3036             :   GEN u, d;
    3037             : 
    3038       18863 :   if (!aco) return cgetg(1,t_MAT);
    3039       18863 :   li = gel(a,1)[1];
    3040       18863 :   d = zero_Flv(li);
    3041       18863 :   bco = lg(b)-1;
    3042      138523 :   for (i=1; i<=aco; i++)
    3043             :   {
    3044      119681 :     GEN ai = vecsmall_copy(gel(a,i));
    3045      119681 :     if (!d[i] && F2v_coeff(ai, i))
    3046       62913 :       k = i;
    3047             :     else
    3048       56768 :       for (k = 1; k <= li; k++) if (!d[k] && F2v_coeff(ai,k)) break;
    3049             :     /* found a pivot on row k */
    3050      119681 :     if (k > li) return NULL;
    3051      119660 :     d[k] = i;
    3052             : 
    3053             :     /* Clear k-th row but column-wise instead of line-wise */
    3054             :     /*  a_ij -= a_i1*a1j/a_11
    3055             :        line-wise grouping:  L_j -= a_1j/a_11*L_1
    3056             :        column-wise:         C_i -= a_i1/a_11*C_1
    3057             :     */
    3058      119660 :     F2v_clear(ai,k);
    3059     1644815 :     for (l=1; l<=aco; l++)
    3060             :     {
    3061     1525155 :       GEN al = gel(a,l);
    3062     1525155 :       if (F2v_coeff(al,k)) F2v_add_inplace(al,ai);
    3063             :     }
    3064     1644780 :     for (l=1; l<=bco; l++)
    3065             :     {
    3066     1525120 :       GEN bl = gel(b,l);
    3067     1525120 :       if (F2v_coeff(bl,k)) F2v_add_inplace(bl,ai);
    3068             :     }
    3069             :   }
    3070       18842 :   u = cgetg(bco+1,t_MAT);
    3071       18842 :   for (j = 1; j <= bco; j++) gel(u,j) = F2_get_col(gel(b,j), d, li, aco);
    3072       18842 :   return u;
    3073             : }
    3074             : 
    3075             : GEN
    3076          35 : F2m_gauss(GEN a, GEN b)
    3077             : {
    3078          35 :   pari_sp av = avma;
    3079          35 :   if (lg(a) == 1) return cgetg(1,t_MAT);
    3080          35 :   return gerepileupto(av, F2m_gauss_sp(F2m_copy(a), F2m_copy(b)));
    3081             : }
    3082             : GEN
    3083          14 : F2m_F2c_gauss(GEN a, GEN b)
    3084             : {
    3085          14 :   pari_sp av = avma;
    3086          14 :   GEN z = F2m_gauss(a, mkmat(b));
    3087          14 :   if (!z) { avma = av; return NULL; }
    3088           7 :   if (lg(z) == 1) { avma = av; return cgetg(1,t_VECSMALL); }
    3089           7 :   return gerepileuptoleaf(av, gel(z,1));
    3090             : }
    3091             : 
    3092             : GEN
    3093          35 : F2m_inv(GEN a)
    3094             : {
    3095          35 :   pari_sp av = avma;
    3096          35 :   if (lg(a) == 1) return cgetg(1,t_MAT);
    3097          35 :   return gerepileupto(av, F2m_gauss_sp(F2m_copy(a), matid_F2m(gel(a,1)[1])));
    3098             : }
    3099             : 
    3100             : /* destroy a, b */
    3101             : static GEN
    3102       48579 : Flm_gauss_sp_OK(GEN a, GEN b, ulong *detp, ulong p)
    3103             : {
    3104       48579 :   long i, j, k, li, bco, aco = lg(a)-1, s = 1;
    3105       48579 :   ulong det = 1;
    3106             :   GEN u;
    3107             : 
    3108       48579 :   li = nbrows(a);
    3109       48579 :   bco = lg(b)-1;
    3110      348576 :   for (i=1; i<=aco; i++)
    3111             :   {
    3112             :     ulong invpiv;
    3113             :     /* Fl_get_col wants 0 <= a[i,j] < p for all i,j */
    3114      348576 :     for (k = 1; k < i; k++) ucoeff(a,k,i) %= p;
    3115      588712 :     for (k = i; k <= li; k++)
    3116             :     {
    3117      588705 :       ulong piv = ( ucoeff(a,k,i) %= p );
    3118      588705 :       if (piv)
    3119             :       {
    3120      348569 :         ucoeff(a,k,i) = Fl_inv(piv, p);
    3121      348569 :         if (detp)
    3122             :         {
    3123           0 :           if (det & HIGHMASK) det %= p;
    3124           0 :           det *= piv;
    3125             :         }
    3126      348569 :         break;
    3127             :       }
    3128             :     }
    3129             :     /* found a pivot on line k */
    3130      348576 :     if (k > li) return NULL;
    3131      348569 :     if (k != i)
    3132             :     { /* swap lines so that k = i */
    3133       96141 :       s = -s;
    3134       96141 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    3135       96141 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    3136             :     }
    3137      348569 :     if (i == aco) break;
    3138             : 
    3139      299997 :     invpiv = p - ucoeff(a,i,i); /* -1/piv mod p */
    3140     2103129 :     for (k=i+1; k<=li; k++)
    3141             :     {
    3142     1803132 :       ulong m = ( ucoeff(a,k,i) %= p );
    3143     1803132 :       if (!m) continue;
    3144             : 
    3145      574574 :       m = Fl_mul(m, invpiv, p);
    3146      574574 :       if (m == 1) {
    3147      100221 :         for (j=i+1; j<=aco; j++) _Fl_add_OK(gel(a,j),k,i, p);
    3148      100221 :         for (j=1;   j<=bco; j++) _Fl_add_OK(gel(b,j),k,i, p);
    3149             :       } else {
    3150      474353 :         for (j=i+1; j<=aco; j++) _Fl_addmul_OK(gel(a,j),k,i,m, p);
    3151      474353 :         for (j=1;   j<=bco; j++) _Fl_addmul_OK(gel(b,j),k,i,m, p);
    3152             :       }
    3153             :     }
    3154             :   }
    3155       48572 :   if (detp)
    3156             :   {
    3157           0 :     det %=  p;
    3158           0 :     if (s < 0 && det) det = p - det;
    3159           0 :     *detp = det;
    3160             :   }
    3161       48572 :   u = cgetg(bco+1,t_MAT);
    3162       48572 :   for (j=1; j<=bco; j++) gel(u,j) = Fl_get_col_OK(a,gel(b,j), aco,p);
    3163       48572 :   return u;
    3164             : }
    3165             : 
    3166             : /* destroy a, b */
    3167             : static GEN
    3168      621832 : Flm_gauss_sp(GEN a, GEN b, ulong *detp, ulong p)
    3169             : {
    3170      621832 :   long i, j, k, li, bco, aco = lg(a)-1, s = 1;
    3171      621832 :   ulong det = 1;
    3172             :   GEN u;
    3173             :   ulong pi;
    3174      621832 :   if (!aco) { if (detp) *detp = 1; return cgetg(1,t_MAT); }
    3175      621832 :   if (SMALL_ULONG(p)) return Flm_gauss_sp_OK(a, b, detp, p);
    3176      573253 :   pi = get_Fl_red(p);
    3177      573253 :   li = nbrows(a);
    3178      573253 :   bco = lg(b)-1;
    3179     1520721 :   for (i=1; i<=aco; i++)
    3180             :   {
    3181             :     ulong invpiv;
    3182             :     /* Fl_get_col wants 0 <= a[i,j] < p for all i,j */
    3183     1574018 :     for (k = i; k <= li; k++)
    3184             :     {
    3185     1574011 :       ulong piv = ucoeff(a,k,i);
    3186     1574011 :       if (piv)
    3187             :       {
    3188     1520714 :         ucoeff(a,k,i) = Fl_inv(piv, p);
    3189     1520714 :         if (detp) det = Fl_mul_pre(det, piv, p, pi);
    3190     1520714 :         break;
    3191             :       }
    3192             :     }
    3193             :     /* found a pivot on line k */
    3194     1520721 :     if (k > li) return NULL;
    3195     1520714 :     if (k != i)
    3196             :     { /* swap lines so that k = i */
    3197       46481 :       s = -s;
    3198       46481 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    3199       46481 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    3200             :     }
    3201     1520714 :     if (i == aco) break;
    3202             : 
    3203      947468 :     invpiv = p - ucoeff(a,i,i); /* -1/piv mod p */
    3204     2430864 :     for (k=i+1; k<=li; k++)
    3205             :     {
    3206     1483396 :       ulong m = ucoeff(a,k,i);
    3207     1483396 :       if (!m) continue;
    3208             : 
    3209     1216715 :       m = Fl_mul_pre(m, invpiv, p, pi);
    3210     1216715 :       if (m == 1) {
    3211       27732 :         for (j=i+1; j<=aco; j++) _Fl_add(gel(a,j),k,i, p);
    3212       27732 :         for (j=1;   j<=bco; j++) _Fl_add(gel(b,j),k,i, p);
    3213             :       } else {
    3214     1188983 :         for (j=i+1; j<=aco; j++) _Fl_addmul(gel(a,j),k,i,m, p, pi);
    3215     1188983 :         for (j=1;   j<=bco; j++) _Fl_addmul(gel(b,j),k,i,m, p, pi);
    3216             :       }
    3217             :     }
    3218             :   }
    3219      573246 :   if (detp)
    3220             :   {
    3221           0 :     if (s < 0 && det) det = p - det;
    3222           0 :     *detp = det;
    3223             :   }
    3224      573246 :   u = cgetg(bco+1,t_MAT);
    3225      573246 :   for (j=1; j<=bco; j++) gel(u,j) = Fl_get_col(a,gel(b,j), aco,p);
    3226      573246 :   return u;
    3227             : }
    3228             : 
    3229             : static GEN
    3230      179060 : Flm_gauss_from_CUP(GEN b, GEN R, GEN C, GEN U, GEN P, ulong p, ulong *detp)
    3231             : {
    3232      179060 :   GEN Y = Flm_rsolve_lower_unit(rowpermute(C, R), rowpermute(b, R), p);
    3233      179061 :   GEN X = rowpermute(Flm_rsolve_upper(U, Y, p), perm_inv(P));
    3234      179061 :   if (detp)
    3235             :   {
    3236      136716 :     ulong d = perm_sign(P) == 1? 1: p-1;
    3237      136716 :     long i, r = lg(R);
    3238     1299018 :     for (i = 1; i < r; i++)
    3239     1162302 :       d = Fl_mul(d, ucoeff(U, i, i), p);
    3240      136716 :     *detp = d;
    3241             :   }
    3242      179061 :   return X;
    3243             : }
    3244             : 
    3245             : static GEN
    3246       42359 : Flm_gauss_CUP(GEN a, GEN b, ulong *detp, ulong p) {
    3247             :   GEN R, C, U, P;
    3248       42359 :   long n = lg(a) - 1, r;
    3249       42359 :   if (nbrows(a) < n || (r = Flm_CUP(a, &R, &C, &U, &P, p)) < n)
    3250          14 :     return NULL;
    3251       42345 :   return Flm_gauss_from_CUP(b, R, C, U, P, p, detp);
    3252             : }
    3253             : 
    3254             : GEN
    3255          63 : Flm_gauss(GEN a, GEN b, ulong p) {
    3256          63 :   pari_sp av = avma;
    3257             :   GEN x;
    3258          63 :   if (lg(a) - 1 >= Flm_CUP_LIMIT)
    3259          14 :     x = Flm_gauss_CUP(a, b, NULL, p);
    3260             :   else {
    3261          49 :     a = RgM_shallowcopy(a);
    3262          49 :     b = RgM_shallowcopy(b);
    3263          49 :     x = Flm_gauss_sp(a, b, NULL, p);
    3264             :   }
    3265          63 :   if (!x) { avma = av; return NULL; }
    3266          49 :   return gerepileupto(av, x);
    3267             : }
    3268             : 
    3269             : static GEN
    3270      624199 : Flm_inv_i(GEN a, ulong *detp, ulong p, long inplace) {
    3271      624199 :   pari_sp av = avma;
    3272      624199 :   long n = lg(a) - 1;
    3273             :   GEN b, x;
    3274      624199 :   if (n == 0) return cgetg(1, t_MAT);
    3275      624199 :   b = matid_Flm(nbrows(a));
    3276      624199 :   if (n >= Flm_CUP_LIMIT)
    3277       42345 :     x = Flm_gauss_CUP(a, b, detp, p);
    3278             :   else {
    3279      581854 :     if (!inplace)
    3280        8624 :       a = RgM_shallowcopy(a);
    3281      581854 :     x = Flm_gauss_sp(a, b, detp, p);
    3282             :   }
    3283      624199 :   if (!x) { avma = av; return NULL; }
    3284      624185 :   return gerepileupto(av, x);
    3285             : }
    3286             : 
    3287             : static GEN
    3288      604704 : Flm_inv_sp(GEN a, ulong *detp, ulong p) {
    3289      604704 :   return Flm_inv_i(a, detp, p, 1);
    3290             : }
    3291             : 
    3292             : GEN
    3293       19495 : Flm_inv(GEN a, ulong p) {
    3294       19495 :   return Flm_inv_i(a, NULL, p, 0);
    3295             : }
    3296             : 
    3297             : GEN
    3298          21 : Flm_Flc_gauss(GEN a, GEN b, ulong p) {
    3299          21 :   pari_sp av = avma;
    3300          21 :   GEN z = Flm_gauss(a, mkmat(b), p);
    3301          21 :   if (!z) { avma = av; return NULL; }
    3302          14 :   if (lg(z) == 1) { avma = av; return cgetg(1,t_VECSMALL); }
    3303          14 :   return gerepileuptoleaf(av, gel(z,1));
    3304             : }
    3305             : 
    3306             : GEN
    3307      136723 : Flm_adjoint(GEN A, ulong p)
    3308             : {
    3309      136723 :   pari_sp av = avma;
    3310             :   GEN R, C, U, P, C1, U1, v, c, d;
    3311      136723 :   long r, i, q, n = lg(A)-1, m;
    3312             :   ulong D;
    3313      136723 :   if (n == 0) return cgetg(1, t_MAT);
    3314      136723 :   r = Flm_CUP(A, &R, &C, &U, &P, p);
    3315      136723 :   m = nbrows(A);
    3316      136723 :   if (r == n)
    3317             :   {
    3318      136716 :     GEN X = Flm_gauss_from_CUP(matid_Flm(m), R, C, U, P, p, &D);
    3319      136715 :     return gerepileupto(av, Flm_Fl_mul(X, D, p));
    3320             :   }
    3321           7 :   if (r < n-1) return zero_Flm(m, m);
    3322          21 :   for (q = m, i = 1; i < m; i++)
    3323          14 :    if (R[i] != i) { q = i; break; }
    3324           7 :   C1 = matslice(C, 1, q-1, 1, q-1);
    3325           7 :   c = vecslice(Flm_row(C, q), 1, q-1);
    3326           7 :   c = Flm_lsolve_lower_unit(C1, Flm_transpose(mkmat(c)), p);
    3327           7 :   d = cgetg(n+1, t_VECSMALL);
    3328           7 :   for (i=1; i<q; i++)    uel(d,i) = ucoeff(c,1,i);
    3329           7 :   uel(d,q) = p-1;
    3330           7 :   for (i=q+1; i<=n; i++) uel(d,i) = 0;
    3331           7 :   U1 = vecslice(U, 1, n-1);
    3332           7 :   v = gel(Flm_rsolve_upper(U1, mkmat(gel(U,n)), p),1);
    3333           7 :   v = vecsmall_append(v, p-1);
    3334           7 :   D = perm_sign(P) != (odd(q+n)?-1:1) ? p-1 : 1;
    3335          21 :   for (i = 1; i <= n-1; i++)
    3336          14 :     D = Fl_mul(D, ucoeff(U1, i, i), p);
    3337           7 :   d = Flv_Fl_mul(d, D, p);
    3338           7 :   return rowpermute(Flc_Flv_mul(v, d, p),perm_inv(P));
    3339             : }
    3340             : 
    3341             : static GEN
    3342        4020 : FpM_gauss_gen(GEN a, GEN b, GEN p)
    3343             : {
    3344             :   void *E;
    3345        4020 :   const struct bb_field *S = get_Fp_field(&E,p);
    3346        4020 :   return gen_Gauss(a,b, E, S);
    3347             : }
    3348             : /* a an FpM, lg(a)>1; b an FpM or NULL (replace by identity) */
    3349             : static GEN
    3350       62742 : FpM_gauss_i(GEN a, GEN b, GEN p, ulong *pp)
    3351             : {
    3352       62742 :   long n = nbrows(a);
    3353       62742 :   a = FpM_init(a,p,pp);
    3354       62742 :   switch(*pp)
    3355             :   {
    3356             :   case 0:
    3357        4020 :     if (!b) b = matid(n);
    3358        4020 :     return FpM_gauss_gen(a,b,p);
    3359             :   case 2:
    3360       18793 :     if (b) b = ZM_to_F2m(b); else b = matid_F2m(n);
    3361       18793 :     return F2m_gauss_sp(a,b);
    3362             :   default:
    3363       39929 :     if (b) b = ZM_to_Flm(b, *pp); else b = matid_Flm(n);
    3364       39929 :     return Flm_gauss_sp(a,b, NULL, *pp);
    3365             :   }
    3366             : }
    3367             : GEN
    3368          35 : FpM_gauss(GEN a, GEN b, GEN p)
    3369             : {
    3370          35 :   pari_sp av = avma;
    3371             :   ulong pp;
    3372             :   GEN u;
    3373          35 :   if (lg(a) == 1 || lg(b)==1) return cgetg(1, t_MAT);
    3374          35 :   u = FpM_gauss_i(a, b, p, &pp);
    3375          35 :   if (!u) { avma = av; return NULL; }
    3376          28 :   switch(pp)
    3377             :   {
    3378          28 :   case 0: return gerepilecopy(av, u);
    3379           0 :   case 2:  u = F2m_to_ZM(u); break;
    3380           0 :   default: u = Flm_to_ZM(u); break;
    3381             :   }
    3382           0 :   return gerepileupto(av, u);
    3383             : }
    3384             : GEN
    3385       62693 : FpM_inv(GEN a, GEN p)
    3386             : {
    3387       62693 :   pari_sp av = avma;
    3388             :   ulong pp;
    3389             :   GEN u;
    3390       62693 :   if (lg(a) == 1) return cgetg(1, t_MAT);
    3391       62693 :   u = FpM_gauss_i(a, NULL, p, &pp);
    3392       62693 :   if (!u) { avma = av; return NULL; }
    3393       62679 :   switch(pp)
    3394             :   {
    3395        3964 :   case 0: return gerepilecopy(av, u);
    3396       18786 :   case 2:  u = F2m_to_ZM(u); break;
    3397       39929 :   default: u = Flm_to_ZM(u); break;
    3398             :   }
    3399       58715 :   return gerepileupto(av, u);
    3400             : }
    3401             : 
    3402             : GEN
    3403          14 : FpM_FpC_gauss(GEN a, GEN b, GEN p)
    3404             : {
    3405          14 :   pari_sp av = avma;
    3406             :   ulong pp;
    3407             :   GEN u;
    3408          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    3409          14 :   u = FpM_gauss_i(a, mkmat(b), p, &pp);
    3410          14 :   if (!u) { avma = av; return NULL; }
    3411          14 :   switch(pp)
    3412             :   {
    3413          14 :   case 0: return gerepilecopy(av, gel(u,1));
    3414           0 :   case 2:  u = F2c_to_ZC(gel(u,1)); break;
    3415           0 :   default: u = Flc_to_ZC(gel(u,1)); break;
    3416             :   }
    3417           0 :   return gerepileupto(av, u);
    3418             : }
    3419             : 
    3420             : static GEN
    3421          56 : FlxqM_gauss_gen(GEN a, GEN b, GEN T, ulong p)
    3422             : {
    3423             :   void *E;
    3424          56 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    3425          56 :   return gen_Gauss(a, b, E, S);
    3426             : }
    3427             : 
    3428             : static GEN
    3429          35 : FlxqM_gauss_CUP(GEN a, GEN b, GEN T, ulong p) {
    3430             :   GEN R, C, U, P, Y;
    3431          35 :   long n = lg(a) - 1, r;
    3432          35 :   if (nbrows(a) < n || (r = FlxqM_CUP(a, &R, &C, &U, &P, T, p)) < n)
    3433          14 :     return NULL;
    3434          21 :   Y = FlxqM_rsolve_lower_unit(rowpermute(C, R),
    3435             :                               rowpermute(b, R), T, p);
    3436          21 :   return rowpermute(FlxqM_rsolve_upper(U, Y, T, p),
    3437             :                     perm_inv(P));
    3438             : }
    3439             : 
    3440             : static GEN
    3441          91 : FlxqM_gauss_i(GEN a, GEN b, GEN T, ulong p) {
    3442          91 :   if (lg(a) - 1 >= FlxqM_CUP_LIMIT)
    3443          35 :     return FlxqM_gauss_CUP(a, b, T, p);
    3444          56 :   return FlxqM_gauss_gen(a, b, T, p);
    3445             : }
    3446             : 
    3447             : GEN
    3448          21 : FlxqM_gauss(GEN a, GEN b, GEN T, ulong p)
    3449             : {
    3450          21 :   pari_sp av = avma;
    3451          21 :   long n = lg(a)-1;
    3452             :   GEN u;
    3453          21 :   if (!n || lg(b)==1) { avma = av; return cgetg(1, t_MAT); }
    3454          21 :   u = FlxqM_gauss_i(a, b, T, p);
    3455          21 :   if (!u) { avma = av; return NULL; }
    3456          14 :   return gerepilecopy(av, u);
    3457             : }
    3458             : GEN
    3459          56 : FlxqM_inv(GEN a, GEN T, ulong p)
    3460             : {
    3461          56 :   pari_sp av = avma;
    3462             :   GEN u;
    3463          56 :   if (lg(a) == 1) { avma = av; return cgetg(1, t_MAT); }
    3464          56 :   u = FlxqM_gauss_i(a, matid_FlxqM(nbrows(a),T,p), T,p);
    3465          56 :   if (!u) { avma = av; return NULL; }
    3466          42 :   return gerepilecopy(av, u);
    3467             : }
    3468             : GEN
    3469          14 : FlxqM_FlxqC_gauss(GEN a, GEN b, GEN T, ulong p)
    3470             : {
    3471          14 :   pari_sp av = avma;
    3472             :   GEN u;
    3473          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    3474          14 :   u = FlxqM_gauss_i(a, mkmat(b), T, p);
    3475          14 :   if (!u) { avma = av; return NULL; }
    3476           7 :   return gerepilecopy(av, gel(u,1));
    3477             : }
    3478             : 
    3479             : static GEN
    3480         133 : FqM_gauss_gen(GEN a, GEN b, GEN T, GEN p)
    3481             : {
    3482             :   void *E;
    3483         133 :   const struct bb_field *S = get_Fq_field(&E,T,p);
    3484         133 :   return gen_Gauss(a,b,E,S);
    3485             : }
    3486             : GEN
    3487          21 : FqM_gauss(GEN a, GEN b, GEN T, GEN p)
    3488             : {
    3489          21 :   pari_sp av = avma;
    3490             :   GEN u;
    3491             :   long n;
    3492          21 :   if (!T) return FpM_gauss(a,b,p);
    3493          21 :   n = lg(a)-1; if (!n || lg(b)==1) return cgetg(1, t_MAT);
    3494          21 :   u = FqM_gauss_gen(a,b,T,p);
    3495          21 :   if (!u) { avma = av; return NULL; }
    3496          14 :   return gerepilecopy(av, u);
    3497             : }
    3498             : GEN
    3499          98 : FqM_inv(GEN a, GEN T, GEN p)
    3500             : {
    3501          98 :   pari_sp av = avma;
    3502             :   GEN u;
    3503          98 :   if (!T) return FpM_inv(a,p);
    3504          98 :   if (lg(a) == 1) return cgetg(1, t_MAT);
    3505          98 :   u = FqM_gauss_gen(a,matid(nbrows(a)),T,p);
    3506          98 :   if (!u) { avma = av; return NULL; }
    3507          70 :   return gerepilecopy(av, u);
    3508             : }
    3509             : GEN
    3510          14 : FqM_FqC_gauss(GEN a, GEN b, GEN T, GEN p)
    3511             : {
    3512          14 :   pari_sp av = avma;
    3513             :   GEN u;
    3514          14 :   if (!T) return FpM_FpC_gauss(a,b,p);
    3515          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    3516          14 :   u = FqM_gauss_gen(a,mkmat(b),T,p);
    3517          14 :   if (!u) { avma = av; return NULL; }
    3518           7 :   return gerepilecopy(av, gel(u,1));
    3519             : }
    3520             : 
    3521             : static GEN
    3522      604655 : ZlM_gauss_ratlift(GEN a, GEN b, ulong p, long e, GEN C)
    3523             : {
    3524      604655 :   pari_sp av = avma, av2;
    3525             :   GEN bb, xi, xb, pi, P, B, r;
    3526      604655 :   long i, k = 2;
    3527      604655 :   if (!C) {
    3528           0 :     C = Flm_inv(ZM_to_Flm(a, p), p);
    3529           0 :     if (!C) pari_err_INV("ZlM_gauss", a);
    3530             :   }
    3531      604655 :   pi = P = utoipos(p);
    3532      604655 :   av2 = avma;
    3533      604655 :   xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    3534      604655 :   xb = Flm_to_ZM(xi);
    3535      604655 :   bb = b;
    3536      962937 :   for (i = 2; i <= e; i++)
    3537             :   {
    3538      433889 :     bb = ZM_Z_divexact(ZM_sub(bb, ZM_nm_mul(a, xi)), P);
    3539      433889 :     if (gc_needed(av,2))
    3540             :     {
    3541          68 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZlM_gauss. i=%ld/%ld",i,e);
    3542          68 :       gerepileall(av2,3, &pi,&bb,&xb);
    3543             :     }
    3544      433889 :     xi = Flm_mul(C, ZM_to_Flm(bb, p), p);
    3545      433889 :     xb = ZM_add(xb, nm_Z_mul(xi, pi));
    3546      433889 :     pi = muliu(pi, p); /* = p^(i-1) */
    3547      433889 :     if (i==k && i < e)
    3548             :     {
    3549      144453 :       k *= 2;
    3550      144453 :       B = sqrti(shifti(pi,-1));
    3551      144453 :       r = FpM_ratlift(xb, pi, B, B, NULL);
    3552      144453 :       if (r && gequal(QM_mul(a, r), b))
    3553             :       {
    3554       75607 :         if (DEBUGLEVEL>=4)
    3555           0 :           err_printf("ZlM_gauss: early solution: %ld/%ld\n",i,e);
    3556       75607 :         return gerepilecopy(av, r);
    3557             :       }
    3558             :     }
    3559             :   }
    3560      529048 :   B = sqrti(shifti(pi,-1));
    3561      529048 :   return gerepileupto(av, FpM_ratlift(xb, pi, B, B, NULL));
    3562             : }
    3563             : 
    3564             : /* Dixon p-adic lifting algorithm.
    3565             :  * Numer. Math. 40, 137-141 (1982), DOI: 10.1007/BF01459082 */
    3566             : GEN
    3567      604746 : ZM_gauss(GEN a, GEN b0)
    3568             : {
    3569      604746 :   pari_sp av = avma, av2;
    3570             :   int iscol;
    3571             :   long n, ncol, i, m, elim;
    3572             :   ulong p;
    3573      604746 :   GEN C, delta, nb, nmin, res, b = b0;
    3574             :   forprime_t S;
    3575             : 
    3576      604746 :   if (!init_gauss(a, &b, &n, &ncol, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    3577      604676 :   nb = gen_0; ncol = lg(b);
    3578     1418804 :   for (i = 1; i < ncol; i++)
    3579             :   {
    3580      814128 :     GEN ni = gnorml2(gel(b, i));
    3581      814128 :     if (cmpii(nb, ni) < 0) nb = ni;
    3582             :   }
    3583      604676 :   if (!signe(nb)) { avma = av; return gcopy(b0); }
    3584      604676 :   delta = gen_1; nmin = nb;
    3585     2432328 :   for (i = 1; i <= n; i++)
    3586             :   {
    3587     1827652 :     GEN ni = gnorml2(gel(a, i));
    3588     1827652 :     if (cmpii(ni, nmin) < 0)
    3589             :     {
    3590       75784 :       delta = mulii(delta, nmin); nmin = ni;
    3591             :     }
    3592             :     else
    3593     1751868 :       delta = mulii(delta, ni);
    3594             :   }
    3595      604676 :   if (!signe(nmin)) return NULL;
    3596      604662 :   elim = expi(delta)+1;
    3597      604662 :   av2 = avma;
    3598      604662 :   init_modular_big(&S);
    3599             :   for(;;)
    3600             :   {
    3601      604662 :     p = u_forprime_next(&S);
    3602      604662 :     C = Flm_inv_sp(ZM_to_Flm(a, p), NULL, p);
    3603      604662 :     if (C) break;
    3604           7 :     elim -= expu(p);
    3605           7 :     if (elim < 0) return NULL;
    3606           0 :     avma = av2;
    3607           0 :   }
    3608             :   /* N.B. Our delta/lambda are SQUARES of those in the paper
    3609             :    * log(delta lambda) / log p, where lambda is 3+sqrt(5) / 2,
    3610             :    * whose log is < 1, hence + 1 (to cater for rounding errors) */
    3611     1209310 :   m = (long)ceil((rtodbl(logr_abs(itor(delta,LOWDEFAULTPREC))) + 1)
    3612      604655 :                  / log((double)p));
    3613      604655 :   res = ZlM_gauss_ratlift(a, b, p, m, C);
    3614      604655 :   if (iscol) return gerepilecopy(av, gel(res, 1));
    3615       82755 :   return gerepileupto(av, res);
    3616             : }
    3617             : 
    3618             : /* same as above, M rational */
    3619             : GEN
    3620         887 : QM_gauss(GEN M, GEN B)
    3621             : {
    3622         887 :   pari_sp av = avma;
    3623             :   GEN K, MB;
    3624         887 :   MB = Q_primitive_part(mkvec2(M,B), NULL);
    3625         887 :   K = ZM_gauss(gel(MB,1), gel(MB,2));
    3626         887 :   return gerepileupto(av, K);
    3627             : }
    3628             : 
    3629             : static GEN
    3630      127945 : ZM_inv_slice(GEN A, GEN P, GEN *mod)
    3631             : {
    3632      127945 :   pari_sp av = avma;
    3633      127945 :   long i, n = lg(P)-1;
    3634             :   GEN H, T;
    3635      127945 :   if (n == 1)
    3636             :   {
    3637      127188 :     ulong p = uel(P,1);
    3638      127188 :     GEN Hp, a = ZM_to_Flm(A, p);
    3639      127188 :     Hp = Flm_adjoint(a, p);
    3640      127187 :     Hp = gerepileupto(av, Flm_to_ZM(Hp));
    3641      127188 :     *mod = utoi(p); return Hp;
    3642             :   }
    3643         757 :   T = ZV_producttree(P);
    3644         757 :   A = ZM_nv_mod_tree(A, P, T);
    3645         757 :   H = cgetg(n+1, t_VEC);
    3646        2748 :   for(i=1; i <= n; i++)
    3647        1991 :     gel(H,i) = Flm_adjoint(gel(A, i), uel(P,i));
    3648         757 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    3649         757 :   *mod = gmael(T, lg(T)-1, 1);
    3650         757 :   gerepileall(av, 2, &H, mod);
    3651         757 :   return H;
    3652             : }
    3653             : 
    3654             : static GEN
    3655      108928 : RgM_true_Hadamard(GEN a)
    3656             : {
    3657      108928 :   pari_sp av = avma;
    3658      108928 :   long n = lg(a)-1, i;
    3659             :   GEN B;
    3660      108928 :   if (n == 0) return gen_1;
    3661      108928 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    3662      108928 :   B = gnorml2(gel(a,1));
    3663      108928 :   for (i = 2; i <= n; i++) B = gmul(B, gnorml2(gel(a,i)));
    3664      108928 :   return gerepileuptoint(av, ceil_safe(sqrtr_abs(B)));
    3665             : }
    3666             : 
    3667             : GEN
    3668      127945 : ZM_inv_worker(GEN P, GEN A)
    3669             : {
    3670      127945 :   GEN V = cgetg(3, t_VEC);
    3671      127945 :   gel(V,1) = ZM_inv_slice(A, P, &gel(V,2));
    3672      127945 :   return V;
    3673             : }
    3674             : 
    3675             : static GEN
    3676        4550 : ZM_inv0(GEN A, GEN *pden)
    3677             : {
    3678        4550 :   if (pden) *pden = gen_1;
    3679        4550 :   (void)A; return cgetg(1, t_MAT);
    3680             : }
    3681             : static GEN
    3682       16622 : ZM_inv1(GEN A, GEN *pden)
    3683             : {
    3684       16622 :   GEN a = gcoeff(A,1,1);
    3685       16622 :   long s = signe(a);
    3686       16622 :   if (!s) return NULL;
    3687       16622 :   if (pden) *pden = absi(a);
    3688       16622 :   retmkmat(mkcol(s == 1? gen_1: gen_m1));
    3689             : }
    3690             : static GEN
    3691       37705 : ZM_inv2(GEN A, GEN *pden)
    3692             : {
    3693             :   GEN a, b, c, d, D, cA;
    3694             :   long s;
    3695       37705 :   A = Q_primitive_part(A, &cA);
    3696       37705 :   a = gcoeff(A,1,1); b = gcoeff(A,1,2);
    3697       37705 :   c = gcoeff(A,2,1); d = gcoeff(A,2,2);
    3698       37705 :   D = subii(mulii(a,d), mulii(b,c)); /* left on stack */
    3699       37705 :   s = signe(D);
    3700       37705 :   if (!s) return NULL;
    3701       37705 :   if (s < 0) D = negi(D);
    3702       37705 :   if (pden) *pden = mul_denom(D, cA);
    3703       37705 :   if (s > 0)
    3704       18763 :     retmkmat2(mkcol2(icopy(d), negi(c)), mkcol2(negi(b), icopy(a)));
    3705             :   else
    3706       18942 :     retmkmat2(mkcol2(negi(d), icopy(c)), mkcol2(icopy(b), negi(a)));
    3707             : }
    3708             : 
    3709             : /* to be used when denom(M^(-1)) << det(M) and a sharp multiple is
    3710             :  * not available. Return H primitive such that M*H = den*Id */
    3711             : GEN
    3712           0 : ZM_inv_ratlift(GEN M, GEN *pden)
    3713             : {
    3714           0 :   pari_sp av2, av = avma;
    3715             :   GEN Hp, q, H;
    3716             :   ulong p;
    3717           0 :   long m = lg(M)-1;
    3718             :   forprime_t S;
    3719             :   pari_timer ti;
    3720             : 
    3721           0 :   if (m == 0) return ZM_inv0(M,pden);
    3722           0 :   if (m == 1 && nbrows(M)==1) return ZM_inv1(M,pden);
    3723           0 :   if (m == 2 && nbrows(M)==2) return ZM_inv2(M,pden);
    3724             : 
    3725           0 :   if (DEBUGLEVEL>5) timer_start(&ti);
    3726           0 :   init_modular_big(&S);
    3727           0 :   av2 = avma;
    3728           0 :   H = NULL;
    3729           0 :   while ((p = u_forprime_next(&S)))
    3730             :   {
    3731             :     GEN Mp, B, Hr;
    3732           0 :     Mp = ZM_to_Flm(M,p);
    3733           0 :     Hp = Flm_inv_sp(Mp, NULL, p);
    3734           0 :     if (!Hp) continue;
    3735           0 :     if (!H)
    3736             :     {
    3737           0 :       H = ZM_init_CRT(Hp, p);
    3738           0 :       q = utoipos(p);
    3739             :     }
    3740             :     else
    3741           0 :       ZM_incremental_CRT(&H, Hp, &q, p);
    3742           0 :     B = sqrti(shifti(q,-1));
    3743           0 :     Hr = FpM_ratlift(H,q,B,B,NULL);
    3744           0 :     if (DEBUGLEVEL>5)
    3745           0 :       timer_printf(&ti,"ZM_inv mod %lu (ratlift=%ld)", p,!!Hr);
    3746           0 :     if (Hr) {/* DONE ? */
    3747           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    3748           0 :       if (ZM_isscalar(ZM_mul(Hl, M), *pden)) { H = Hl; break; }
    3749             :     }
    3750             : 
    3751           0 :     if (gc_needed(av,2))
    3752             :     {
    3753           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_inv_ratlift");
    3754           0 :       gerepileall(av2, 2, &H, &q);
    3755             :     }
    3756             :   }
    3757           0 :   if (!*pden) *pden = gen_1;
    3758           0 :   gerepileall(av, 2, &H, pden);
    3759           0 :   return H;
    3760             : }
    3761             : 
    3762             : static GEN
    3763      116188 : ZM_adj_ratlift(GEN A, GEN H, GEN mod)
    3764             : {
    3765             :   GEN B;
    3766      116188 :   GEN D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3767      116188 :   GEN g = gcdii(D, mod);
    3768      116188 :   if (!equali1(g))
    3769             :   {
    3770           7 :     mod = diviiexact(mod, g);
    3771           7 :     H = FpM_red(H, mod);
    3772             :   }
    3773      116188 :   D = Fp_inv(Fp_red(D, mod), mod);
    3774      116188 :   H = FpM_Fp_mul(H, D, mod);
    3775      116188 :   B = sqrti(shifti(mod,-1));
    3776      116188 :   return FpM_ratlift(H, mod, B, B, NULL);
    3777             : }
    3778             : 
    3779             : GEN
    3780      167812 : ZM_inv(GEN A, GEN *pden)
    3781             : {
    3782      167812 :   pari_sp av = avma;
    3783      167812 :   long m = lg(A)-1, n, k1 = 1, k2;
    3784      167812 :   GEN H = NULL, D, H1 = NULL, mod1 = NULL, worker;
    3785      167812 :   ulong bnd, mask, p = 0;
    3786             :   pari_timer ti;
    3787             : 
    3788      167812 :   if (m == 0) return ZM_inv0(A,pden);
    3789      163262 :   if (pden) *pden = gen_1;
    3790      163262 :   if (nbrows(A) < m) return NULL;
    3791      163255 :   if (m == 1 && nbrows(A)==1) return ZM_inv1(A,pden);
    3792      146633 :   if (m == 2 && nbrows(A)==2) return ZM_inv2(A,pden);
    3793             : 
    3794      108928 :   if (DEBUGLEVEL>=5) timer_start(&ti);
    3795      108928 :   bnd = expi(RgM_true_Hadamard(A));
    3796      108928 :   worker = strtoclosure("_ZM_inv_worker", 1, A);
    3797      108928 :   gen_inccrt("ZM_inv_r", worker, NULL, k1, m, &p, &H1, &mod1, nmV_chinese_center, FpM_center);
    3798      108928 :   n = (bnd+1)/expu(p)+1;
    3799      108928 :   if (DEBUGLEVEL>=5) timer_printf(&ti,"inv (%ld/%ld primes)", k1, n);
    3800      108928 :   mask = quadratic_prec_mask(n);
    3801      108928 :   for (k2 = 0;;)
    3802             :   {
    3803             :     GEN Hr;
    3804      125110 :     if (k2 > 0)
    3805             :     {
    3806       13790 :       gen_inccrt("ZM_inv_r", worker, NULL, k2, m, &p, &H1, &mod1,nmV_chinese_center,FpM_center);
    3807       13790 :       k1 += k2;
    3808       13790 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"CRT (%ld/%ld primes)", k1, n);
    3809             :     }
    3810      125110 :     if (mask == 1) break;
    3811      116188 :     k2 = (mask&1UL) ? k1-1: k1;
    3812      116188 :     mask >>= 1;
    3813             : 
    3814      116188 :     Hr = ZM_adj_ratlift(A, H1, mod1);
    3815      116188 :     if (DEBUGLEVEL>=5) timer_printf(&ti,"ratlift (%ld/%ld primes)", k1, n);
    3816      116188 :     if (Hr) {/* DONE ? */
    3817             :       GEN den;
    3818      101741 :       GEN Hl = Q_remove_denom(Hr, &den);
    3819      101741 :       GEN R = ZM_mul(Hl, A);
    3820      101741 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"mult (%ld/%ld primes)", k1, n);
    3821      101741 :       den = den ? den: gen_1;
    3822      101741 :       if (den)
    3823             :       {
    3824      101741 :         if (ZM_isscalar(R, den))
    3825             :         {
    3826      100006 :           H = Hl;
    3827      100006 :           if (pden) *pden = den;
    3828      200012 :           break;
    3829             :         }
    3830             :       }
    3831             :       else
    3832           0 :         if (ZM_isidentity(R)) { H=Hl; break; }
    3833             :     }
    3834       16182 :   }
    3835      108928 :   if (!H)
    3836             :   {
    3837             :     GEN d;
    3838        8922 :     H = H1;
    3839        8922 :     D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3840        8922 :     if (signe(D)==0) pari_err_INV("ZM_inv", A);
    3841        8915 :     d = gcdii(Q_content_safe(H), D);
    3842        8915 :     if (signe(D) < 0) d = negi(d);
    3843        8915 :     if (!equali1(d))
    3844             :     {
    3845        5271 :       H = ZM_Z_divexact(H, d);
    3846        5271 :       D = diviiexact(D, d);
    3847             :     }
    3848        8915 :     if (pden) *pden = D;
    3849             :   }
    3850      108921 :   gerepileall(av, pden? 2: 1, &H, pden);
    3851      108921 :   return H;
    3852             : }
    3853             : 
    3854             : /* same as above, M rational */
    3855             : GEN
    3856         847 : QM_inv(GEN M)
    3857             : {
    3858         847 :   pari_sp av = avma;
    3859             :   GEN den, cM, K;
    3860         847 :   M = Q_primitive_part(M, &cM);
    3861         847 :   K = ZM_inv(M, &den);
    3862         847 :   if (!K) { avma = av; return NULL; }
    3863         847 :   cM = inv_content(mul_content(cM, den));
    3864         847 :   if (cM) K = RgM_Rg_div(K, cM);
    3865         847 :   return gerepileupto(av, K);
    3866             : }
    3867             : 
    3868             : static GEN
    3869       55357 : ZM_ker_i(GEN M, long fl)
    3870             : {
    3871       55357 :   pari_sp av2, av = avma;
    3872             :   GEN q, H, D;
    3873             :   forprime_t S;
    3874       55357 :   av2 = avma;
    3875       55357 :   H = NULL; D = NULL;
    3876       55357 :   if (lg(M)==1) return cgetg(1, t_MAT);
    3877       55350 :   init_modular_big(&S);
    3878             :   for(;;)
    3879             :   {
    3880             :     GEN Kp, Hp, Dp, Mp, Hr, B;
    3881      117216 :     ulong p = u_forprime_next(&S);
    3882      117216 :     Mp = ZM_to_Flm(M, p);
    3883      117216 :     Kp = Flm_ker_sp(Mp, p, 2);
    3884      117216 :     Hp = gel(Kp,1); Dp = gel(Kp,2);
    3885      117216 :     if (H && (lg(Hp)>lg(H) || (lg(Hp)==lg(H) && vecsmall_lexcmp(Dp,D)>0))) continue;
    3886      109308 :     if (!H || (lg(Hp)<lg(H) || vecsmall_lexcmp(Dp,D)<0))
    3887             :     {
    3888       93030 :       H = ZM_init_CRT(Hp, p); D = Dp;
    3889       93030 :       q = utoipos(p);
    3890             :     }
    3891             :     else
    3892       16278 :       ZM_incremental_CRT(&H, Hp, &q, p);
    3893      109308 :     B = sqrti(shifti(q,-1));
    3894      109308 :     Hr = FpM_ratlift(H, q, B, B, NULL);
    3895      109308 :     if (DEBUGLEVEL>5) err_printf("ZM_ker mod %lu (ratlift=%ld)\n", p,!!Hr);
    3896      109308 :     if (Hr) {/* DONE ? */
    3897      103813 :       GEN MH = QM_mul(M, Hr);
    3898      103813 :       if (gequal0(MH)) { H = fl ? vec_Q_primpart(Hr): Hr;  break; }
    3899             :     }
    3900       53958 :     if (gc_needed(av,2))
    3901             :     {
    3902           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_ker");
    3903           0 :       gerepileall(av2, 3, &H, &D, &q);
    3904             :     }
    3905       61866 :   }
    3906       55350 :   return gerepilecopy(av, H);
    3907             : }
    3908             : 
    3909             : GEN
    3910       48763 : ZM_ker(GEN M)
    3911       48763 : { return ZM_ker_i(M, 1); }
    3912             : 
    3913             : GEN
    3914        7756 : QM_ker(GEN M)
    3915             : {
    3916        7756 :   pari_sp av = avma;
    3917        7756 :   long l = lg(M)-1;
    3918        7756 :   if (l==0) return cgetg(1, t_MAT);
    3919        7721 :   if (lgcols(M)==1) return matid(l);
    3920        6531 :   M = shallowtrans(vec_Q_primpart(shallowtrans(M)));
    3921        6531 :   return gerepileupto(av, ZM_ker_i(M, 0));
    3922             : }
    3923             : 
    3924             : /* x a ZM. Return a multiple of the determinant of the lattice generated by
    3925             :  * the columns of x. From Algorithm 2.2.6 in GTM138 */
    3926             : GEN
    3927       47521 : detint(GEN A)
    3928             : {
    3929       47521 :   if (typ(A) != t_MAT) pari_err_TYPE("detint",A);
    3930       47521 :   RgM_check_ZM(A, "detint");
    3931       47521 :   return ZM_detmult(A);
    3932             : }
    3933             : GEN
    3934       77838 : ZM_detmult(GEN A)
    3935             : {
    3936       77838 :   pari_sp av1, av = avma;
    3937             :   GEN B, c, v, piv;
    3938       77838 :   long rg, i, j, k, m, n = lg(A) - 1;
    3939             : 
    3940       77838 :   if (!n) return gen_1;
    3941       77838 :   m = nbrows(A);
    3942       77838 :   if (n < m) return gen_0;
    3943       77817 :   c = zero_zv(m);
    3944       77817 :   av1 = avma;
    3945       77817 :   B = zeromatcopy(m,m);
    3946       77817 :   v = cgetg(m+1, t_COL);
    3947       77817 :   piv = gen_1; rg = 0;
    3948      478812 :   for (k=1; k<=n; k++)
    3949             :   {
    3950      478798 :     GEN pivprec = piv;
    3951      478798 :     long t = 0;
    3952     4020453 :     for (i=1; i<=m; i++)
    3953             :     {
    3954     3541655 :       pari_sp av2 = avma;
    3955             :       GEN vi;
    3956     3541655 :       if (c[i]) continue;
    3957             : 
    3958     2010475 :       vi = mulii(piv, gcoeff(A,i,k));
    3959    17740929 :       for (j=1; j<=m; j++)
    3960    15730454 :         if (c[j]) vi = addii(vi, mulii(gcoeff(B,j,i),gcoeff(A,j,k)));
    3961     2010475 :       if (!t && signe(vi)) t = i;
    3962     2010475 :       gel(v,i) = gerepileuptoint(av2, vi);
    3963             :     }
    3964      478798 :     if (!t) continue;
    3965             :     /* at this point c[t] = 0 */
    3966             : 
    3967      478714 :     if (++rg >= m) { /* full rank; mostly done */
    3968       77803 :       GEN det = gel(v,t); /* last on stack */
    3969       77803 :       if (++k > n)
    3970       77727 :         det = absi(det);
    3971             :       else
    3972             :       {
    3973             :         /* improve further; at this point c[i] is set for all i != t */
    3974          76 :         gcoeff(B,t,t) = piv; v = centermod(gel(B,t), det);
    3975         320 :         for ( ; k<=n; k++)
    3976         244 :           det = gcdii(det, ZV_dotproduct(v, gel(A,k)));
    3977             :       }
    3978       77803 :       return gerepileuptoint(av, det);
    3979             :     }
    3980             : 
    3981      400911 :     piv = gel(v,t);
    3982     3463369 :     for (i=1; i<=m; i++)
    3983             :     {
    3984             :       GEN mvi;
    3985     3062458 :       if (c[i] || i == t) continue;
    3986             : 
    3987     1531229 :       gcoeff(B,t,i) = mvi = negi(gel(v,i));
    3988    13716486 :       for (j=1; j<=m; j++)
    3989    12185257 :         if (c[j]) /* implies j != t */
    3990             :         {
    3991     3040933 :           pari_sp av2 = avma;
    3992     3040933 :           GEN z = addii(mulii(gcoeff(B,j,i), piv), mulii(gcoeff(B,j,t), mvi));
    3993     3040933 :           if (rg > 1) z = diviiexact(z, pivprec);
    3994     3040933 :           gcoeff(B,j,i) = gerepileuptoint(av2, z);
    3995             :         }
    3996             :     }
    3997      400911 :     c[t] = k;
    3998      400911 :     if (gc_needed(av,1))
    3999             :     {
    4000           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"detint. k=%ld",k);
    4001           0 :       gerepileall(av1, 2, &piv,&B); v = zerovec(m);
    4002             :     }
    4003             :   }
    4004          14 :   avma = av; return gen_0;
    4005             : }
    4006             : 
    4007             : /* Reduce x modulo (invertible) y */
    4008             : GEN
    4009       13118 : closemodinvertible(GEN x, GEN y)
    4010             : {
    4011       13118 :   return gmul(y, ground(RgM_solve(y,x)));
    4012             : }
    4013             : GEN
    4014           7 : reducemodinvertible(GEN x, GEN y)
    4015             : {
    4016           7 :   return gsub(x, closemodinvertible(x,y));
    4017             : }
    4018             : GEN
    4019           0 : reducemodlll(GEN x,GEN y)
    4020             : {
    4021           0 :   return reducemodinvertible(x, ZM_lll(y, 0.75, LLL_INPLACE));
    4022             : }
    4023             : 
    4024             : /*******************************************************************/
    4025             : /*                                                                 */
    4026             : /*                    KERNEL of an m x n matrix                    */
    4027             : /*          return n - rk(x) linearly independent vectors          */
    4028             : /*                                                                 */
    4029             : /*******************************************************************/
    4030             : static GEN
    4031          28 : RgM_deplin_i(GEN x0)
    4032             : {
    4033          28 :   pari_sp av = avma, av2;
    4034          28 :   long i, j, k, nl, nc = lg(x0)-1;
    4035             :   GEN D, x, y, c, l, d, ck;
    4036             : 
    4037          28 :   if (!nc) { avma=av; return NULL; }
    4038          28 :   nl = nbrows(x0);
    4039          28 :   c = zero_zv(nl);
    4040          28 :   l = cgetg(nc+1, t_VECSMALL); /* not initialized */
    4041          28 :   av2 = avma;
    4042          28 :   x = RgM_shallowcopy(x0);
    4043          28 :   d = const_vec(nl, gen_1); /* pivot list */
    4044          28 :   ck = NULL; /* gcc -Wall */
    4045          98 :   for (k=1; k<=nc; k++)
    4046             :   {
    4047          91 :     ck = gel(x,k);
    4048         196 :     for (j=1; j<k; j++)
    4049             :     {
    4050         105 :       GEN cj = gel(x,j), piv = gel(d,j), q = gel(ck,l[j]);
    4051         420 :       for (i=1; i<=nl; i++)
    4052         315 :         if (i!=l[j]) gel(ck,i) = gsub(gmul(piv, gel(ck,i)), gmul(q, gel(cj,i)));
    4053             :     }
    4054             : 
    4055          91 :     i = gauss_get_pivot_NZ(x, NULL, k, c);
    4056          91 :     if (i > nl) break;
    4057          70 :     if (gc_needed(av,1))
    4058             :     {
    4059           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"deplin k = %ld/%ld",k,nc);
    4060           0 :       gerepileall(av2, 2, &x, &d);
    4061           0 :       ck = gel(x,k);
    4062             :     }
    4063          70 :     gel(d,k) = gel(ck,i);
    4064          70 :     c[i] = k; l[k] = i; /* pivot d[k] in x[i,k] */
    4065             :   }
    4066          28 :   if (k > nc) { avma = av; return NULL; }
    4067          21 :   if (k == 1) { avma = av; return scalarcol_shallow(gen_1,nc); }
    4068          21 :   y = cgetg(nc+1,t_COL);
    4069          21 :   gel(y,1) = gcopy(gel(ck, l[1]));
    4070          49 :   for (D=gel(d,1),j=2; j<k; j++)
    4071             :   {
    4072          28 :     gel(y,j) = gmul(gel(ck, l[j]), D);
    4073          28 :     D = gmul(D, gel(d,j));
    4074             :   }
    4075          21 :   gel(y,j) = gneg(D);
    4076          21 :   for (j++; j<=nc; j++) gel(y,j) = gen_0;
    4077          21 :   y = primitive_part(y, &c);
    4078          21 :   return c? gerepileupto(av, y): gerepilecopy(av, y);
    4079             : }
    4080             : static GEN
    4081           0 : RgV_deplin(GEN v)
    4082             : {
    4083           0 :   pari_sp av = avma;
    4084           0 :   long n = lg(v)-1;
    4085           0 :   GEN y, p = NULL;
    4086           0 :   if (n <= 1)
    4087             :   {
    4088           0 :     if (n == 1 && gequal0(gel(v,1))) return mkcol(gen_1);
    4089           0 :     return cgetg(1, t_COL);
    4090             :   }
    4091           0 :   if (gequal0(gel(v,1))) return scalarcol_shallow(gen_1, n);
    4092           0 :   v = primpart(mkvec2(gel(v,1),gel(v,2)));
    4093           0 :   if (RgV_is_FpV(v, &p) && p) v = centerlift(v);
    4094           0 :   y = zerocol(n);
    4095           0 :   gel(y,1) = gneg(gel(v,2));
    4096           0 :   gel(y,2) = gcopy(gel(v,1));
    4097           0 :   return gerepileupto(av, y);
    4098             : 
    4099             : }
    4100             : 
    4101             : static GEN
    4102         105 : RgM_deplin_FpM(GEN x, GEN p)
    4103             : {
    4104         105 :   pari_sp av = avma;
    4105             :   ulong pp;
    4106         105 :   x = RgM_Fp_init(x, p, &pp);
    4107         105 :   switch(pp)
    4108             :   {
    4109             :   case 0:
    4110          35 :     x = FpM_ker_gen(x,p,1);
    4111          35 :     if (!x) { avma = av; return NULL; }
    4112          21 :     x = FpC_center(x,p,shifti(p,-1));
    4113          21 :     break;
    4114             :   case 2:
    4115          14 :     x = F2m_ker_sp(x,1);
    4116          14 :     if (!x) { avma = av; return NULL; }
    4117           7 :     x = F2c_to_ZC(x); break;
    4118             :   default:
    4119          56 :     x = Flm_ker_sp(x,pp,1);
    4120          56 :     if (!x) { avma = av; return NULL; }
    4121          35 :     x = Flv_center(x, pp, pp>>1);
    4122          35 :     x = zc_to_ZC(x);
    4123          35 :     break;
    4124             :   }
    4125          63 :   return gerepileupto(av, x);
    4126             : }
    4127             : 
    4128             : /* FIXME: implement direct modular ZM_deplin ? */
    4129             : static GEN
    4130          98 : QM_deplin(GEN M)
    4131             : {
    4132          98 :   pari_sp av = avma;
    4133          98 :   long l = lg(M)-1;
    4134             :   GEN k;
    4135          98 :   if (l==0) return NULL;
    4136          63 :   if (lgcols(M)==1) return col_ei(l, 1);
    4137          63 :   M = shallowtrans(vec_Q_primpart(shallowtrans(M)));
    4138          63 :   k = ZM_ker_i(M, 1);
    4139          63 :   if (lg(k)== 1) { avma = av; return NULL; }
    4140          49 :   return gerepilecopy(av, gel(k,1));
    4141             : }
    4142             : 
    4143             : static GEN
    4144          42 : RgM_deplin_FqM(GEN x, GEN pol, GEN p)
    4145             : {
    4146          42 :   pari_sp av = avma;
    4147          42 :   GEN T = RgX_to_FpX(pol, p);
    4148          42 :   GEN b = FqM_deplin(RgM_to_FqM(x, T, p), T, p);
    4149          42 :   return gerepileupto(av, b);
    4150             : }
    4151             : 
    4152             : #define code(t1,t2) ((t1 << 6) | t2)
    4153             : static GEN
    4154         357 : RgM_deplin_fast(GEN x)
    4155             : {
    4156             :   GEN p, pol;
    4157             :   long pa;
    4158         357 :   long t = RgM_type(x, &p,&pol,&pa);
    4159         357 :   switch(t)
    4160             :   {
    4161             :     case t_INT:    /* fall through */
    4162          98 :     case t_FRAC:   return QM_deplin(x);
    4163          84 :     case t_FFELT:  return FFM_deplin(x, pol);
    4164         105 :     case t_INTMOD: return RgM_deplin_FpM(x, p);
    4165             :     case code(t_POLMOD, t_INTMOD):
    4166          42 :                    return RgM_deplin_FqM(x, pol, p);
    4167          28 :     default:       return gen_0;
    4168             :   }
    4169             : }
    4170             : #undef code
    4171             : 
    4172             : static GEN
    4173         357 : RgM_deplin(GEN x)
    4174             : {
    4175         357 :   GEN z = RgM_deplin_fast(x);
    4176         357 :   if (z!= gen_0) return z;
    4177          28 :   return RgM_deplin_i(x);
    4178             : }
    4179             : 
    4180             : GEN
    4181         357 : deplin(GEN x)
    4182             : {
    4183         357 :   switch(typ(x))
    4184             :   {
    4185             :     case t_MAT:
    4186             :     {
    4187         357 :       GEN z = RgM_deplin(x);
    4188         357 :       if (z) return z;
    4189         140 :       return cgetg(1, t_COL);
    4190             :     }
    4191           0 :     case t_VEC: return RgV_deplin(x);
    4192           0 :     default: pari_err_TYPE("deplin",x);
    4193             :   }
    4194             :   return NULL;/*LCOV_EXCL_LINE*/
    4195             : }
    4196             : 
    4197             : /*******************************************************************/
    4198             : /*                                                                 */
    4199             : /*         GAUSS REDUCTION OF MATRICES  (m lines x n cols)         */
    4200             : /*           (kernel, image, complementary image, rank)            */
    4201             : /*                                                                 */
    4202             : /*******************************************************************/
    4203             : /* return the transform of x under a standard Gauss pivot.
    4204             :  * x0 is a reference point when guessing whether x[i,j] ~ 0
    4205             :  * (iff x[i,j] << x0[i,j])
    4206             :  * Set r = dim ker(x). d[k] contains the index of the first non-zero pivot
    4207             :  * in column k */
    4208             : static GEN
    4209         882 : gauss_pivot_ker(GEN x, GEN x0, GEN *dd, long *rr)
    4210             : {
    4211             :   GEN c, d, p, data;
    4212             :   pari_sp av;
    4213             :   long i, j, k, r, t, n, m;
    4214             :   pivot_fun pivot;
    4215             : 
    4216         882 :   n=lg(x)-1; if (!n) { *dd=NULL; *rr=0; return cgetg(1,t_MAT); }
    4217         882 :   m=nbrows(x); r=0;
    4218         882 :   pivot = get_pivot_fun(x, x0, &data);
    4219         882 :   x = RgM_shallowcopy(x);
    4220         882 :   c = zero_zv(m);
    4221         882 :   d = cgetg(n+1,t_VECSMALL);
    4222         882 :   av=avma;
    4223        5061 :   for (k=1; k<=n; k++)
    4224             :   {
    4225        4179 :     j = pivot(x, data, k, c);
    4226        4179 :     if (j > m)
    4227             :     {
    4228         931 :       r++; d[k]=0;
    4229        4137 :       for(j=1; j<k; j++)
    4230        3206 :         if (d[j]) gcoeff(x,d[j],k) = gclone(gcoeff(x,d[j],k));
    4231             :     }
    4232             :     else
    4233             :     { /* pivot for column k on row j */
    4234        3248 :       c[j]=k; d[k]=j; p = gdiv(gen_m1,gcoeff(x,j,k));
    4235        3248 :       gcoeff(x,j,k) = gen_m1;
    4236             :       /* x[j,] /= - x[j,k] */
    4237        3248 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    4238       33054 :       for (t=1; t<=m; t++)
    4239       29806 :         if (t!=j)
    4240             :         { /* x[t,] -= 1 / x[j,k] x[j,] */
    4241       26558 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    4242      223706 :           for (i=k+1; i<=n; i++)
    4243      197148 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i),gmul(p,gcoeff(x,j,i)));
    4244       26558 :           if (gc_needed(av,1)) gerepile_gauss_ker(x,k,t,av);
    4245             :         }
    4246             :     }
    4247             :   }
    4248         882 :   *dd=d; *rr=r; return x;
    4249             : }
    4250             : 
    4251             : static GEN FpM_gauss_pivot(GEN x, GEN p, long *rr);
    4252             : static GEN FqM_gauss_pivot(GEN x, GEN T, GEN p, long *rr);
    4253             : static GEN F2m_gauss_pivot(GEN x, long *rr);
    4254             : 
    4255             : /* r = dim ker(x).
    4256             :  * Returns d:
    4257             :  *   d[k] != 0 contains the index of a non-zero pivot in column k
    4258             :  *   d[k] == 0 if column k is a linear combination of the (k-1) first ones */
    4259             : GEN
    4260       22948 : RgM_pivots(GEN x0, GEN data, long *rr, pivot_fun pivot)
    4261             : {
    4262             :   GEN x, c, d, p;
    4263       22948 :   long i, j, k, r, t, m, n = lg(x0)-1;
    4264             :   pari_sp av;
    4265             : 
    4266       22948 :   if (RgM_is_ZM(x0)) return ZM_pivots(x0, rr);
    4267       22857 :   if (!n) { *rr = 0; return NULL; }
    4268             : 
    4269       22857 :   d = cgetg(n+1, t_VECSMALL);
    4270       22857 :   x = RgM_shallowcopy(x0);
    4271       22857 :   m = nbrows(x); r = 0;
    4272       22857 :   c = zero_zv(m);
    4273       22857 :   av = avma;
    4274      927436 :   for (k=1; k<=n; k++)
    4275             :   {
    4276      904579 :     j = pivot(x, data, k, c);
    4277      904579 :     if (j > m) { r++; d[k] = 0; }
    4278             :     else
    4279             :     {
    4280       39752 :       c[j] = k; d[k] = j; p = gdiv(gen_m1, gcoeff(x,j,k));
    4281       39752 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    4282             : 
    4283      166241 :       for (t=1; t<=m; t++)
    4284      126489 :         if (!c[t]) /* no pivot on that line yet */
    4285             :         {
    4286       51958 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    4287     5557098 :           for (i=k+1; i<=n; i++)
    4288     5505140 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i), gmul(p, gcoeff(x,j,i)));
    4289       51958 :           if (gc_needed(av,1)) gerepile_gauss(x,k,t,av,j,c);
    4290             :         }
    4291       39752 :       for (i=k; i<=n; i++) gcoeff(x,j,i) = gen_0; /* dummy */
    4292             :     }
    4293             :   }
    4294       22857 :   *rr = r; avma = (pari_sp)d; return d;
    4295             : }
    4296             : 
    4297             : static long
    4298      104188 : ZM_count_0_cols(GEN M)
    4299             : {
    4300      104188 :   long i, l = lg(M), n = 0;
    4301      553514 :   for (i = 1; i < l; i++)
    4302      449326 :     if (ZV_equal0(gel(M,i))) n++;
    4303      104188 :   return n;
    4304             : }
    4305             : 
    4306             : static void indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol);
    4307             : /* As RgM_pivots, integer entries. Set *rr = dim Ker M0 */
    4308             : GEN
    4309      108115 : ZM_pivots(GEN M0, long *rr)
    4310             : {
    4311      108115 :   GEN d, dbest = NULL;
    4312             :   long m, n, i, imax, rmin, rbest, zc;
    4313      108115 :   int beenthere = 0;
    4314      108115 :   pari_sp av, av0 = avma;
    4315             :   forprime_t S;
    4316             : 
    4317      108115 :   rbest = n = lg(M0)-1;
    4318      108115 :   if (n == 0) { *rr = 0; return NULL; }
    4319      104188 :   zc = ZM_count_0_cols(M0);
    4320      104188 :   if (n == zc) { *rr = zc; return zero_zv(n); }
    4321             : 
    4322      103824 :   m = nbrows(M0);
    4323      103824 :   rmin = maxss(zc, n-m);
    4324      103824 :   init_modular_small(&S);
    4325      103824 :   imax = (n < (1<<4))? 1: (n>>3); /* heuristic */
    4326             : 
    4327             :   for(;;)
    4328             :   {
    4329             :     GEN row, col, M, KM, IM, RHS, X, cX;
    4330             :     long rk;
    4331      111715 :     for (av = avma, i = 0;; avma = av, i++)
    4332             :     {
    4333      111715 :       ulong p = u_forprime_next(&S);
    4334             :       long rp;
    4335      111715 :       if (!p) pari_err_OVERFLOW("ZM_pivots [ran out of primes]");
    4336      111715 :       d = Flm_pivots(ZM_to_Flm(M0, p), p, &rp, 1);
    4337      111715 :       if (rp == rmin) { rbest = rp; goto END; } /* maximal rank, return */
    4338       14365 :       if (rp < rbest) { /* save best r so far */
    4339        6476 :         rbest = rp;
    4340        6476 :         if (dbest) gunclone(dbest);
    4341        6476 :         dbest = gclone(d);
    4342       12950 :         if (beenthere) break;
    4343             :       }
    4344       14365 :       if (!beenthere && i >= imax) break;
    4345        7891 :     }
    4346        6474 :     beenthere = 1;
    4347             :     /* Dubious case: there is (probably) a non trivial kernel */
    4348        6474 :     indexrank_all(m,n, rbest, dbest, &row, &col);
    4349        6474 :     M = rowpermute(vecpermute(M0, col), row);
    4350        6474 :     rk = n - rbest; /* (probable) dimension of image */
    4351        6474 :     IM = vecslice(M,1,rk);
    4352        6474 :     KM = vecslice(M,rk+1, n);
    4353        6474 :     M = rowslice(IM, 1,rk); /* square maximal rank */
    4354        6474 :     X = ZM_gauss(M, rowslice(KM, 1,rk));
    4355        6474 :     X = Q_remove_denom(X, &cX);
    4356        6474 :     RHS = rowslice(KM,rk+1,m);
    4357        6474 :     if (cX) RHS = ZM_Z_mul(RHS, cX);
    4358        6474 :     if (ZM_equal(ZM_mul(rowslice(IM,rk+1,m), X), RHS))
    4359             :     {
    4360        6474 :       d = vecsmall_copy(dbest);
    4361        6474 :       goto END;
    4362             :     }
    4363           0 :     avma = av;
    4364           0 :   }
    4365             : END:
    4366      103824 :   *rr = rbest; if (dbest) gunclone(dbest);
    4367      103824 :   return gerepileuptoleaf(av0, d);
    4368             : }
    4369             : 
    4370             : /* set *pr = dim Ker x */
    4371             : static GEN
    4372       12649 : gauss_pivot(GEN x, long *pr) {
    4373             :   GEN data;
    4374       12649 :   pivot_fun pivot = get_pivot_fun(x, x, &data);
    4375       12649 :   return RgM_pivots(x, data, pr, pivot);
    4376             : }
    4377             : 
    4378             : /* compute ker(x), x0 is a reference point when guessing whether x[i,j] ~ 0
    4379             :  * (iff x[i,j] << x0[i,j]) */
    4380             : static GEN
    4381         882 : ker_aux(GEN x, GEN x0)
    4382             : {
    4383         882 :   pari_sp av = avma;
    4384             :   GEN d,y;
    4385             :   long i,j,k,r,n;
    4386             : 
    4387         882 :   x = gauss_pivot_ker(x,x0,&d,&r);
    4388         882 :   if (!r) { avma=av; return cgetg(1,t_MAT); }
    4389         875 :   n = lg(x)-1; y=cgetg(r+1,t_MAT);
    4390        1806 :   for (j=k=1; j<=r; j++,k++)
    4391             :   {
    4392         931 :     GEN p = cgetg(n+1,t_COL);
    4393             : 
    4394         931 :     gel(y,j) = p; while (d[k]) k++;
    4395        4137 :     for (i=1; i<k; i++)
    4396        3206 :       if (d[i])
    4397             :       {
    4398        3108 :         GEN p1=gcoeff(x,d[i],k);
    4399        3108 :         gel(p,i) = gcopy(p1); gunclone(p1);
    4400             :       }
    4401             :       else
    4402          98 :         gel(p,i) = gen_0;
    4403         931 :     gel(p,k) = gen_1; for (i=k+1; i<=n; i++) gel(p,i) = gen_0;
    4404             :   }
    4405         875 :   return gerepileupto(av,y);
    4406             : }
    4407             : 
    4408             : static GEN
    4409          77 : RgM_ker_FpM(GEN x, GEN p)
    4410             : {
    4411          77 :   pari_sp av = avma;
    4412             :   ulong pp;
    4413          77 :   x = RgM_Fp_init(x, p, &pp);
    4414          77 :   switch(pp)
    4415             :   {
    4416          35 :     case 0: x = FpM_to_mod(FpM_ker_gen(x,p,0),p); break;
    4417           7 :     case 2: x = F2m_to_mod(F2m_ker_sp(x,0)); break;
    4418          35 :     default:x = Flm_to_mod(Flm_ker_sp(x,pp,0), pp); break;
    4419             :   }
    4420          77 :   return gerepileupto(av, x);
    4421             : }
    4422             : 
    4423             : static GEN
    4424          84 : RgM_ker_FqM(GEN x, GEN pol, GEN p)
    4425             : {
    4426          84 :   pari_sp av = avma;
    4427          84 :   GEN T = RgX_to_FpX(pol, p);
    4428          84 :   GEN b = FqM_ker(RgM_to_FqM(x, T, p), T, p);
    4429          84 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    4430             : }
    4431             : 
    4432             : #define code(t1,t2) ((t1 << 6) | t2)
    4433             : static GEN
    4434        8722 : RgM_ker_fast(GEN x)
    4435             : {
    4436             :   GEN p, pol;
    4437             :   long pa;
    4438        8722 :   long t = RgM_type(x, &p,&pol,&pa);
    4439        8722 :   switch(t)
    4440             :   {
    4441             :     case t_INT:    /* fall through */
    4442        7756 :     case t_FRAC:   return QM_ker(x);
    4443          77 :     case t_FFELT:  return FFM_ker(x, pol);
    4444          77 :     case t_INTMOD: return RgM_ker_FpM(x, p);
    4445             :     case code(t_POLMOD, t_INTMOD):
    4446          84 :                    return RgM_ker_FqM(x, pol, p);
    4447         728 :     default:       return NULL;
    4448             :   }
    4449             : }
    4450             : #undef code
    4451             : 
    4452             : GEN
    4453        8722 : ker(GEN x)
    4454             : {
    4455        8722 :   GEN b = RgM_ker_fast(x);
    4456        8722 :   if (b) return b;
    4457         728 :   return ker_aux(x,x);
    4458             : }
    4459             : 
    4460             : GEN
    4461       46207 : matker0(GEN x,long flag)
    4462             : {
    4463       46207 :   if (typ(x)!=t_MAT) pari_err_TYPE("matker",x);
    4464       46207 :   if (!flag) return ker(x);
    4465       45934 :   RgM_check_ZM(x, "matker");
    4466       45934 :   return ZM_ker(x);
    4467             : }
    4468             : 
    4469             : static GEN
    4470          63 : RgM_image_FpM(GEN x, GEN p)
    4471             : {
    4472          63 :   pari_sp av = avma;
    4473             :   ulong pp;
    4474          63 :   x = RgM_Fp_init(x, p, &pp);
    4475          63 :   switch(pp)
    4476             :   {
    4477          28 :     case 0: x = FpM_to_mod(FpM_image(x,p),p); break;
    4478           7 :     case 2: x = F2m_to_mod(F2m_image(x)); break;
    4479          28 :     default:x = Flm_to_mod(Flm_image(x,pp), pp); break;
    4480             :   }
    4481          63 :   return gerepileupto(av, x);
    4482             : }
    4483             : 
    4484             : static GEN
    4485          28 : RgM_image_FqM(GEN x, GEN pol, GEN p)
    4486             : {
    4487          28 :   pari_sp av = avma;
    4488          28 :   GEN T = RgX_to_FpX(pol, p);
    4489          28 :   GEN b = FqM_image(RgM_to_FqM(x, T, p), T, p);
    4490          28 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    4491             : }
    4492             : 
    4493             : static GEN
    4494        1407 : QM_image(GEN A)
    4495             : {
    4496        1407 :   pari_sp av = avma;
    4497        1407 :   GEN M = vecpermute(A, ZM_indeximage(vec_Q_primpart(A)));
    4498        1407 :   return gerepilecopy(av, M);
    4499             : }
    4500             : 
    4501             : #define code(t1,t2) ((t1 << 6) | t2)
    4502             : static GEN
    4503        1561 : RgM_image_fast(GEN x)
    4504             : {
    4505             :   GEN p, pol;
    4506             :   long pa;
    4507        1561 :   long t = RgM_type(x, &p,&pol,&pa);
    4508        1561 :   switch(t)
    4509             :   {
    4510             :     case t_INT:    /* fall through */
    4511        1407 :     case t_FRAC:   return QM_image(x);
    4512          49 :     case t_FFELT:  return FFM_image(x, pol);
    4513          63 :     case t_INTMOD: return RgM_image_FpM(x, p);
    4514             :     case code(t_POLMOD, t_INTMOD):
    4515          28 :                    return RgM_image_FqM(x, pol, p);
    4516          14 :     default:       return NULL;
    4517             :   }
    4518             : }
    4519             : #undef code
    4520             : 
    4521             : GEN
    4522        1561 : image(GEN x)
    4523             : {
    4524             :   GEN d, M;
    4525             :   long r;
    4526             : 
    4527        1561 :   if (typ(x)!=t_MAT) pari_err_TYPE("matimage",x);
    4528        1561 :   M = RgM_image_fast(x);
    4529        1561 :   if (M) return M;
    4530          14 :   d = gauss_pivot(x,&r); /* d left on stack for efficiency */
    4531          14 :   return image_from_pivot(x,d,r);
    4532             : }
    4533             : 
    4534             : static GEN
    4535          84 : imagecompl_aux(GEN x, GEN(*PIVOT)(GEN,long*))
    4536             : {
    4537          84 :   pari_sp av = avma;
    4538             :   GEN d,y;
    4539             :   long j,i,r;
    4540             : 
    4541          84 :   if (typ(x)!=t_MAT) pari_err_TYPE("imagecompl",x);
    4542          84 :   (void)new_chunk(lg(x) * 4 + 1); /* HACK */
    4543          84 :   d = PIVOT(x,&r); /* if (!d) then r = 0 */
    4544          84 :   avma = av; y = cgetg(r+1,t_VECSMALL);
    4545         126 :   for (i=j=1; j<=r; i++)
    4546          42 :     if (!d[i]) y[j++] = i;
    4547          84 :   return y;
    4548             : }
    4549             : GEN
    4550          84 : imagecompl(GEN x) { return imagecompl_aux(x, &gauss_pivot); }
    4551             : GEN
    4552           0 : ZM_imagecompl(GEN x) { return imagecompl_aux(x, &ZM_pivots); }
    4553             : 
    4554             : GEN
    4555       42279 : FpM_FpC_invimage(GEN A, GEN y, GEN p)
    4556             : {
    4557       42279 :   pari_sp av = avma;
    4558       42279 :   long i, l = lg(A);
    4559             :   GEN M, x, t;
    4560             : 
    4561       42279 :   if (lgefint(p) == 3)
    4562             :   {
    4563       42272 :     ulong pp = p[2];
    4564       42272 :     A = ZM_to_Flm(A, pp);
    4565       42272 :     y = ZV_to_Flv(y, pp);
    4566       42272 :     x = Flm_Flc_invimage(A,y,pp);
    4567       42272 :     if (!x) { avma = av; return NULL; }
    4568       42272 :     return gerepileupto(av, Flc_to_ZC(x));
    4569             :   }
    4570           7 :   if (l==1) return NULL;
    4571           7 :   if (lg(y) != lgcols(A)) pari_err_DIM("FpM_FpC_invimage");
    4572           7 :   M = FpM_ker(shallowconcat(A,y),p);
    4573           7 :   i = lg(M)-1; if (!i) { avma = av; return NULL; }
    4574             : 
    4575           7 :   x = gel(M,i); t = gel(x,l);
    4576           7 :   if (!signe(t)) { avma = av; return NULL; }
    4577             : 
    4578           7 :   setlg(x,l); t = Fp_inv(negi(t),p);
    4579           7 :   if (is_pm1(t)) return gerepilecopy(av, x);
    4580           7 :   return gerepileupto(av, FpC_Fp_mul(x, t, p));
    4581             : }
    4582             : GEN
    4583       51323 : Flm_Flc_invimage(GEN A, GEN y, ulong p)
    4584             : {
    4585       51323 :   pari_sp av = avma;
    4586       51323 :   long i, l = lg(A);
    4587             :   GEN M, x;
    4588             :   ulong t;
    4589             : 
    4590       51323 :   if (l==1) return NULL;
    4591       51323 :   if (lg(y) != lgcols(A)) pari_err_DIM("Flm_Flc_invimage");
    4592       51323 :   M = cgetg(l+1,t_MAT);
    4593       51323 :   for (i=1; i<l; i++) gel(M,i) = gel(A,i);
    4594       51323 :   gel(M,l) = y; M = Flm_ker(M,p);
    4595       51323 :   i = lg(M)-1; if (!i) { avma = av; return NULL; }
    4596             : 
    4597       51323 :   x = gel(M,i); t = x[l];
    4598       51323 :   if (!t) { avma = av; return NULL; }
    4599             : 
    4600       51323 :   setlg(x,l); t = Fl_inv(Fl_neg(t,p),p);
    4601       51323 :   if (t!=1) x = Flv_Fl_mul(x, t, p);
    4602       51323 :   return gerepileuptoleaf(av, x);
    4603             : }
    4604             : GEN
    4605          21 : F2m_F2c_invimage(GEN A, GEN y)
    4606             : {
    4607          21 :   pari_sp av = avma;
    4608          21 :   long i, l = lg(A);
    4609             :   GEN M, x;
    4610             : 
    4611          21 :   if (l==1) return NULL;
    4612          21 :   if (lg(y) != lgcols(A)) pari_err_DIM("F2m_F2c_invimage");
    4613          21 :   M = cgetg(l+1,t_MAT);
    4614          21 :   for (i=1; i<l; i++) gel(M,i) = gel(A,i);
    4615          21 :   gel(M,l) = y; M = F2m_ker(M);
    4616          21 :   i = lg(M)-1; if (!i) { avma = av; return NULL; }
    4617             : 
    4618          21 :   x = gel(M,i);
    4619          21 :   if (!F2v_coeff(x,l)) { avma = av; return NULL; }
    4620          21 :   F2v_clear(x, x[1]); x[1]--; /* remove last coord */
    4621          21 :   return gerepileuptoleaf(av, x);
    4622             : }
    4623             : 
    4624             : static GEN
    4625          28 : RgM_RgC_invimage_FpC(GEN A, GEN y, GEN p)
    4626             : {
    4627          28 :   pari_sp av = avma;
    4628             :   ulong pp;
    4629             :   GEN x;
    4630          28 :   A = RgM_Fp_init(A,p,&pp);
    4631          28 :   switch(pp)
    4632             :   {
    4633             :   case 0:
    4634           7 :     y = RgC_to_FpC(y,p);
    4635           7 :     x = FpM_FpC_invimage(A, y, p);
    4636           7 :     return x ? gerepileupto(av, FpC_to_mod(x,p)): NULL;
    4637             :   case 2:
    4638           7 :     y = RgV_to_F2v(y);
    4639           7 :     x = F2m_F2c_invimage(A, y);
    4640           7 :     return x ? gerepileupto(av, F2c_to_mod(x)): NULL;
    4641             :   default:
    4642          14 :     y = RgV_to_Flv(y,pp);
    4643          14 :     x = Flm_Flc_invimage(A, y, pp);
    4644          14 :     return x ? gerepileupto(av, Flc_to_mod(x,pp)): NULL;
    4645             :   }
    4646             : }
    4647             : 
    4648             : static GEN
    4649        1834 : RgM_RgC_invimage_fast(GEN x, GEN y)
    4650             : {
    4651             :   GEN p, pol;
    4652             :   long pa;
    4653        1834 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    4654        1834 :   switch(t)
    4655             :   {
    4656          28 :     case t_INTMOD: return RgM_RgC_invimage_FpC(x, y, p);
    4657          63 :     case t_FFELT:  return FFM_FFC_invimage(x, y, pol);
    4658        1743 :     default:       return gen_0;
    4659             :   }
    4660             : }
    4661             : 
    4662             : GEN
    4663        1939 : RgM_RgC_invimage(GEN A, GEN y)
    4664             : {
    4665        1939 :   pari_sp av = avma;
    4666        1939 :   long i, l = lg(A);
    4667             :   GEN M, x, t;
    4668        1939 :   if (l==1) return NULL;
    4669        1834 :   if (lg(y) != lgcols(A)) pari_err_DIM("inverseimage");
    4670        1834 :   M = RgM_RgC_invimage_fast(A, y);
    4671        1834 :   if (!M) {avma = av; return NULL; }
    4672        1813 :   if (M != gen_0) return M;
    4673        1743 :   M = ker(shallowconcat(A, y));
    4674        1743 :   i = lg(M)-1;
    4675        1743 :   if (!i) { avma = av; return NULL; }
    4676             : 
    4677        1484 :   x = gel(M,i); t = gel(x,l);
    4678        1484 :   if (gequal0(t)) { avma = av; return NULL; }
    4679             : 
    4680        1449 :   t = gneg_i(t); setlg(x,l);
    4681        1449 :   return gerepileupto(av, RgC_Rg_div(x, t));
    4682             : }
    4683             : 
    4684             : /* Return X such that m X = v (t_COL or t_MAT), resp. an empty t_COL / t_MAT
    4685             :  * if no solution exist */
    4686             : GEN
    4687        2156 : inverseimage(GEN m, GEN v)
    4688             : {
    4689             :   GEN y;
    4690        2156 :   if (typ(m)!=t_MAT) pari_err_TYPE("inverseimage",m);
    4691        2156 :   switch(typ(v))
    4692             :   {
    4693             :     case t_COL:
    4694        1918 :       y = RgM_RgC_invimage(m,v);
    4695        1918 :       return y? y: cgetg(1,t_COL);
    4696             :     case t_MAT:
    4697         238 :       y = RgM_invimage(m, v);
    4698         238 :       return y? y: cgetg(1,t_MAT);
    4699             :   }
    4700           0 :   pari_err_TYPE("inverseimage",v);
    4701             :   return NULL;/*LCOV_EXCL_LINE*/
    4702             : }
    4703             : 
    4704             : static GEN
    4705          21 : Flm_invimage_CUP(GEN A, GEN B, ulong p) {
    4706          21 :   pari_sp av = avma;
    4707             :   GEN R, Rc, C, U, P, B1, B2, C1, C2, X, Y, Z;
    4708             :   long r;
    4709          21 :   r = Flm_CUP(A, &R, &C, &U, &P, p);
    4710          21 :   Rc = indexcompl(R, nbrows(B));
    4711          21 :   C1 = rowpermute(C, R);
    4712          21 :   C2 = rowpermute(C, Rc);
    4713          21 :   B1 = rowpermute(B, R);
    4714          21 :   B2 = rowpermute(B, Rc);
    4715          21 :   Z = Flm_rsolve_lower_unit(C1, B1, p);
    4716          21 :   if (!gequal(Flm_mul(C2, Z, p), B2))
    4717          14 :     return NULL;
    4718          14 :   Y = vconcat(Flm_rsolve_upper(vecslice(U, 1, r), Z, p),
    4719          14 :               zero_Flm(lg(A) - 1 - r, lg(B) - 1));
    4720           7 :   X = rowpermute(Y, perm_inv(P));
    4721           7 :   return gerepileupto(av, X);
    4722             : }
    4723             : 
    4724             : static GEN
    4725          42 : Flm_invimage_i(GEN A, GEN B, ulong p)
    4726             : {
    4727             :   GEN d, x, X, Y;
    4728          42 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4729             : 
    4730          42 :   if (!nB) return cgetg(1, t_MAT);
    4731          42 :   if (nA + nB >= Flm_CUP_LIMIT && nbrows(B) >= Flm_CUP_LIMIT)
    4732          21 :     return Flm_invimage_CUP(A, B, p);
    4733             : 
    4734          21 :   x = Flm_ker_sp(shallowconcat(Flm_neg(A,p), B), p, 0);
    4735             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4736             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4737             :    * Y has at least nB columns and full rank */
    4738          21 :   nY = lg(x)-1;
    4739          21 :   if (nY < nB) return NULL;
    4740          21 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    4741          21 :   d = cgetg(nB+1, t_VECSMALL);
    4742          56 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4743             :   {
    4744          49 :     for (; j>=1; j--)
    4745          42 :       if (coeff(Y,i,j)) { d[i] = j; break; }
    4746          42 :     if (!j) return NULL;
    4747             :   }
    4748             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    4749          14 :   Y = vecpermute(Y, d);
    4750          14 :   x = vecpermute(x, d);
    4751          14 :   X = rowslice(x, 1, nA);
    4752          14 :   return Flm_mul(X, Flm_inv_upper_1(Y,p), p);
    4753             : }
    4754             : 
    4755             : static GEN
    4756           7 : F2m_invimage_i(GEN A, GEN B)
    4757             : {
    4758             :   GEN d, x, X, Y;
    4759           7 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4760           7 :   x = F2m_ker_sp(shallowconcat(A, B), 0);
    4761             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4762             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4763             :    * Y has at least nB columns and full rank */
    4764           7 :   nY = lg(x)-1;
    4765           7 :   if (nY < nB) return NULL;
    4766             : 
    4767             :   /* implicitly: Y = rowslice(x, nA+1, nA+nB), nB rows */
    4768           7 :   d = cgetg(nB+1, t_VECSMALL);
    4769          21 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4770             :   {
    4771          14 :     for (; j>=1; j--)
    4772          14 :       if (F2m_coeff(x,nA+i,j)) { d[i] = j; break; } /* Y[i,j] */
    4773          14 :     if (!j) return NULL;
    4774             :   }
    4775           7 :   x = vecpermute(x, d);
    4776             : 
    4777           7 :   X = F2m_rowslice(x, 1, nA);
    4778           7 :   Y = F2m_rowslice(x, nA+1, nA+nB);
    4779           7 :   return F2m_mul(X, F2m_inv_upper_1(Y));
    4780             : }
    4781             : GEN
    4782           0 : Flm_invimage(GEN A, GEN B, ulong p)
    4783             : {
    4784           0 :   pari_sp av = avma;
    4785           0 :   GEN X = Flm_invimage_i(A,B,p);
    4786           0 :   if (!X) { avma = av; return NULL; }
    4787           0 :   return gerepileupto(av, X);
    4788             : }
    4789             : GEN
    4790           0 : F2m_invimage(GEN A, GEN B)
    4791             : {
    4792           0 :   pari_sp av = avma;
    4793           0 :   GEN X = F2m_invimage_i(A,B);
    4794           0 :   if (!X) { avma = av; return NULL; }
    4795           0 :   return gerepileupto(av, X);
    4796             : }
    4797             : static GEN
    4798          35 : FpM_invimage_i(GEN A, GEN B, GEN p)
    4799             : {
    4800             :   GEN d, x, X, Y;
    4801          35 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4802          35 :   if (lgefint(p) == 3)
    4803             :   {
    4804           0 :     ulong pp = p[2];
    4805           0 :     A = ZM_to_Flm(A, pp);
    4806           0 :     B = ZM_to_Flm(B, pp);
    4807           0 :     x = Flm_invimage_i(A, B, pp);
    4808           0 :     return x? Flm_to_ZM(x): NULL;
    4809             :   }
    4810          35 :   x = FpM_ker(shallowconcat(ZM_neg(A), B), p);
    4811             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4812             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4813             :    * Y has at least nB columns and full rank */
    4814          35 :   nY = lg(x)-1;
    4815          35 :   if (nY < nB) return NULL;
    4816          28 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    4817          28 :   d = cgetg(nB+1, t_VECSMALL);
    4818         217 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4819             :   {
    4820         280 :     for (; j>=1; j--)
    4821         266 :       if (signe(gcoeff(Y,i,j))) { d[i] = j; break; }
    4822         203 :     if (!j) return NULL;
    4823             :   }
    4824             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    4825          14 :   Y = vecpermute(Y, d);
    4826          14 :   x = vecpermute(x, d);
    4827          14 :   X = rowslice(x, 1, nA);
    4828          14 :   return FpM_mul(X, FpM_inv_upper_1(Y,p), p);
    4829             : }
    4830             : GEN
    4831           0 : FpM_invimage(GEN A, GEN B, GEN p)
    4832             : {
    4833           0 :   pari_sp av = avma;
    4834           0 :   GEN X = FpM_invimage_i(A,B,p);
    4835           0 :   if (!X) { avma = av; return NULL; }
    4836           0 :   return gerepileupto(av, X);
    4837             : }
    4838             : 
    4839             : static GEN
    4840          84 : RgM_invimage_FpM(GEN A, GEN B, GEN p)
    4841             : {
    4842          84 :   pari_sp av = avma;
    4843             :   ulong pp;
    4844             :   GEN x;
    4845          84 :   A = RgM_Fp_init(A,p,&pp);
    4846          84 :   switch(pp)
    4847             :   {
    4848             :   case 0:
    4849          35 :     B = RgM_to_FpM(B,p);
    4850          35 :     x = FpM_invimage_i(A, B, p);
    4851          35 :     return x ? gerepileupto(av, FpM_to_mod(x, p)): x;
    4852             :   case 2:
    4853           7 :     B = RgM_to_F2m(B);
    4854           7 :     x = F2m_invimage_i(A, B);
    4855           7 :     return x ? gerepileupto(av, F2m_to_mod(x)): x;
    4856             :   default:
    4857          42 :     B = RgM_to_Flm(B,pp);
    4858          42 :     x = Flm_invimage_i(A, B, pp);
    4859          42 :     return x ? gerepileupto(av, Flm_to_mod(x, pp)): x;
    4860             :   }
    4861             : }
    4862             : 
    4863             : static GEN
    4864         252 : RgM_invimage_fast(GEN x, GEN y)
    4865             : {
    4866             :   GEN p, pol;
    4867             :   long pa;
    4868         252 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    4869         252 :   switch(t)
    4870             :   {
    4871          84 :     case t_INTMOD: return RgM_invimage_FpM(x, y, p);
    4872         105 :     case t_FFELT:  return FFM_invimage(x, y, pol);
    4873          63 :     default:       return gen_0;
    4874             :   }
    4875             : }
    4876             : 
    4877             : /* find Z such that A Z = B. Return NULL if no solution */
    4878             : GEN
    4879         252 : RgM_invimage(GEN A, GEN B)
    4880             : {
    4881         252 :   pari_sp av = avma;
    4882             :   GEN d, x, X, Y;
    4883         252 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4884         252 :   X = RgM_invimage_fast(A, B);
    4885         252 :   if (!X) {avma = av; return NULL; }
    4886         140 :   if (X != gen_0) return X;
    4887          63 :   x = ker(shallowconcat(RgM_neg(A), B));
    4888             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4889             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4890             :    * Y has at least nB columns and full rank */
    4891          63 :   nY = lg(x)-1;
    4892          63 :   if (nY < nB) { avma = av; return NULL; }
    4893          49 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    4894          49 :   d = cgetg(nB+1, t_VECSMALL);
    4895         441 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4896             :   {
    4897         546 :     for (; j>=1; j--)
    4898         532 :       if (!gequal0(gcoeff(Y,i,j))) { d[i] = j; break; }
    4899         406 :     if (!j) { avma = av; return NULL; }
    4900             :   }
    4901             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    4902          35 :   Y = vecpermute(Y, d);
    4903          35 :   x = vecpermute(x, d);
    4904          35 :   X = rowslice(x, 1, nA);
    4905          35 :   return gerepileupto(av, RgM_mul(X, RgM_inv_upper(Y)));
    4906             : }
    4907             : 
    4908             : /* r = dim Ker x, n = nbrows(x) */
    4909             : static GEN
    4910       43771 : get_suppl(GEN x, GEN d, long n, long r, GEN(*ei)(long,long))
    4911             : {
    4912             :   pari_sp av;
    4913             :   GEN y, c;
    4914       43771 :   long j, k, rx = lg(x)-1; /* != 0 due to init_suppl() */
    4915             : 
    4916       43771 :   if (rx == n && r == 0) return gcopy(x);
    4917       35163 :   y = cgetg(n+1, t_MAT);
    4918       35163 :   av = avma; c = zero_zv(n);
    4919             :   /* c = lines containing pivots (could get it from gauss_pivot, but cheap)
    4920             :    * In theory r = 0 and d[j] > 0 for all j, but why take chances? */
    4921      256729 :   for (k = j = 1; j<=rx; j++)
    4922      221566 :     if (d[j]) { c[ d[j] ] = 1; gel(y,k++) = gel(x,j); }
    4923      360148 :   for (j=1; j<=n; j++)
    4924      324985 :     if (!c[j]) gel(y,k++) = (GEN)j; /* HACK */
    4925       35163 :   avma = av;
    4926             : 
    4927       35163 :   rx -= r;
    4928       35163 :   for (j=1; j<=rx; j++) gel(y,j) = gcopy(gel(y,j));
    4929       35163 :   for (   ; j<=n; j++)  gel(y,j) = ei(n, y[j]);
    4930       35163 :   return y;
    4931             : }
    4932             : 
    4933             : static void
    4934       43771 : init_suppl(GEN x)
    4935             : {
    4936       43771 :   if (lg(x) == 1) pari_err_IMPL("suppl [empty matrix]");
    4937             :   /* HACK: avoid overwriting d from gauss_pivot() after avma=av */
    4938       43771 :   (void)new_chunk(lgcols(x) * 2);
    4939       43771 : }
    4940             : 
    4941             : GEN
    4942       42399 : FpM_suppl(GEN x, GEN p)
    4943             : {
    4944             :   GEN d;
    4945             :   long r;
    4946       42399 :   init_suppl(x); d = FpM_gauss_pivot(x,p, &r);
    4947       42399 :   return get_suppl(x,d,nbrows(x),r,&col_ei);
    4948             : }
    4949             : 
    4950             : GEN
    4951          63 : Flm_suppl(GEN x, ulong p)
    4952             : {
    4953             :   GEN d;
    4954             :   long r;
    4955          63 :   init_suppl(x); d = Flm_pivots(x, p, &r, 0);
    4956          63 :   return get_suppl(x,d,nbrows(x),r,&vecsmall_ei);
    4957             : }
    4958             : 
    4959             : GEN
    4960          14 : F2m_suppl(GEN x)
    4961             : {
    4962             :   GEN d;
    4963             :   long r;
    4964          14 :   init_suppl(x); d = F2m_gauss_pivot(F2m_copy(x), &r);
    4965          14 :   return get_suppl(x,d,mael(x,1,1),r,&F2v_ei);
    4966             : }
    4967             : 
    4968             : static GEN
    4969          70 : RgM_suppl_FpM(GEN x, GEN p)
    4970             : {
    4971          70 :   pari_sp av = avma;
    4972             :   ulong pp;
    4973          70 :   x = RgM_Fp_init(x, p, &pp);
    4974          70 :   switch(pp)
    4975             :   {
    4976          21 :   case 0: x = FpM_to_mod(FpM_suppl(x,p), p); break;
    4977          14 :   case 2: x = F2m_to_mod(F2m_suppl(x)); break;
    4978          35 :   default:x = Flm_to_mod(Flm_suppl(x,pp), pp); break;
    4979             :   }
    4980          70 :   return gerepileupto(av, x);
    4981             : }
    4982             : 
    4983             : static GEN
    4984         168 : RgM_suppl_fast(GEN x)
    4985             : {
    4986             :   GEN p, pol;
    4987             :   long pa;
    4988         168 :   long t = RgM_type(x,&p,&pol,&pa);
    4989         168 :   switch(t)
    4990             :   {
    4991          70 :     case t_INTMOD: return RgM_suppl_FpM(x, p);
    4992          35 :     case t_FFELT:  return FFM_suppl(x, pol);
    4993          63 :     default:       return NULL;
    4994             :   }
    4995             : }
    4996             : 
    4997             : /* x is an n x k matrix, rank(x) = k <= n. Return an invertible n x n matrix
    4998             :  * whose first k columns are given by x. If rank(x) < k, undefined result. */
    4999             : GEN
    5000         168 : suppl(GEN x)
    5001             : {
    5002         168 :   pari_sp av = avma;
    5003             :   GEN d, M;
    5004             :   long r;
    5005         168 :   if (typ(x)!=t_MAT) pari_err_TYPE("suppl",x);
    5006         168 :   M = RgM_suppl_fast(x);
    5007         168 :   if (M) return M;
    5008          63 :   init_suppl(x);
    5009          63 :   d = gauss_pivot(x,&r);
    5010          63 :   avma = av; return get_suppl(x,d,nbrows(x),r,&col_ei);
    5011             : }
    5012             : /* variable number to be filled in later */
    5013             : static GEN
    5014          84 : _FlxC_ei(long n, long i)
    5015             : {
    5016          84 :   GEN x = cgetg(n + 1, t_COL);
    5017             :   long j;
    5018        1652 :   for (j = 1; j <= n; j++)
    5019        1568 :     gel(x, j) = (j == i)? pol1_Flx(0): pol0_Flx(0);
    5020          84 :   return x;
    5021             : }
    5022             : 
    5023             : GEN
    5024           7 : F2xqM_suppl(GEN x, GEN T)
    5025             : {
    5026           7 :   pari_sp av = avma;
    5027             :   GEN d, y;
    5028           7 :   long n = nbrows(x), r, sv = get_Flx_var(T);
    5029             : 
    5030           7 :   init_suppl(x);
    5031           7 :   d = F2xqM_gauss_pivot(x, T, &r);
    5032           7 :   avma = av;
    5033           7 :   y = get_suppl(x, d, n, r, &_FlxC_ei);
    5034           7 :   if (sv) {
    5035             :     long i, j;
    5036          21 :     for (j = r + 1; j <= n; j++) {
    5037          42 :       for (i = 1; i <= n; i++)
    5038          28 :         gcoeff(y, i, j)[1] = sv;
    5039             :     }
    5040             :   }
    5041           7 :   return y;
    5042             : }
    5043             : 
    5044             : GEN
    5045          14 : FlxqM_suppl(GEN x, GEN T, ulong p)
    5046             : {
    5047          14 :   pari_sp av = avma;
    5048             :   GEN d, y;
    5049          14 :   long n = nbrows(x), r, sv = get_Flx_var(T);
    5050             : 
    5051          14 :   init_suppl(x);
    5052          14 :   d = FlxqM_gauss_pivot(x, T, p, &r);
    5053          14 :   avma = av;
    5054          14 :   y = get_suppl(x, d, n, r, &_FlxC_ei);
    5055          14 :   if (sv) {
    5056             :     long i, j;
    5057          21 :     for (j = r + 1; j <= n; j++) {
    5058          42 :       for (i = 1; i <= n; i++)
    5059          28 :         gcoeff(y, i, j)[1] = sv;
    5060             :     }
    5061             :   }
    5062          14 :   return y;
    5063             : }
    5064             : 
    5065             : GEN
    5066        4060 : FqM_suppl(GEN x, GEN T, GEN p)
    5067             : {
    5068        4060 :   pari_sp av = avma;
    5069             :   GEN d;
    5070             :   long r;
    5071             : 
    5072        4060 :   if (!T) return FpM_suppl(x,p);
    5073        1211 :   init_suppl(x);
    5074        1211 :   d = FqM_gauss_pivot(x,T,p,&r);
    5075        1211 :   avma = av; return get_suppl(x,d,nbrows(x),r,&col_ei);
    5076             : }
    5077             : 
    5078             : GEN
    5079           7 : image2(GEN x)
    5080             : {
    5081           7 :   pari_sp av = avma;
    5082             :   long k, n, i;
    5083             :   GEN A, B;
    5084             : 
    5085           7 :   if (typ(x)!=t_MAT) pari_err_TYPE("image2",x);
    5086           7 :   if (lg(x) == 1) return cgetg(1,t_MAT);
    5087           7 :   A = ker(x); k = lg(A)-1;
    5088           7 :   if (!k) { avma = av; return gcopy(x); }
    5089           7 :   A = suppl(A); n = lg(A)-1;
    5090           7 :   B = cgetg(n-k+1, t_MAT);
    5091           7 :   for (i = k+1; i <= n; i++) gel(B,i-k) = RgM_RgC_mul(x, gel(A,i));
    5092           7 :   return gerepileupto(av, B);
    5093             : }
    5094             : 
    5095             : GEN
    5096         203 : matimage0(GEN x,long flag)
    5097             : {
    5098         203 :   switch(flag)
    5099             :   {
    5100         196 :     case 0: return image(x);
    5101           7 :     case 1: return image2(x);
    5102           0 :     default: pari_err_FLAG("matimage");
    5103             :   }
    5104             :   return NULL; /* LCOV_EXCL_LINE */
    5105             : }
    5106             : 
    5107             : static long
    5108         126 : RgM_rank_FpM(GEN x, GEN p)
    5109             : {
    5110         126 :   pari_sp av = avma;
    5111             :   ulong pp;
    5112             :   long r;
    5113         126 :   x = RgM_Fp_init(x,p,&pp);
    5114         126 :   switch(pp)
    5115             :   {
    5116          28 :   case 0: r = FpM_rank(x,p); break;
    5117          63 :   case 2: r = F2m_rank(x); break;
    5118          35 :   default:r = Flm_rank(x,pp); break;
    5119             :   }
    5120         126 :   avma = av; return r;
    5121             : }
    5122             : 
    5123             : static long
    5124          42 : RgM_rank_FqM(GEN x, GEN pol, GEN p)
    5125             : {
    5126          42 :   pari_sp av = avma;
    5127          42 :   GEN T = RgX_to_FpX(pol, p);
    5128          42 :   long r = FqM_rank(RgM_to_FqM(x, T, p), T, p);
    5129          42 :   avma = av;
    5130          42 :   return r;
    5131             : }
    5132             : 
    5133             : #define code(t1,t2) ((t1 << 6) | t2)
    5134             : static long
    5135         280 : RgM_rank_fast(GEN x)
    5136             : {
    5137             :   GEN p, pol;
    5138             :   long pa;
    5139         280 :   long t = RgM_type(x,&p,&pol,&pa);
    5140         280 :   switch(t)
    5141             :   {
    5142          42 :     case t_INT:    return ZM_rank(x);
    5143           0 :     case t_FRAC:   return QM_rank(x);
    5144         126 :     case t_INTMOD: return RgM_rank_FpM(x, p);
    5145          63 :     case t_FFELT:  return FFM_rank(x, pol);
    5146             :     case code(t_POLMOD, t_INTMOD):
    5147          42 :                    return RgM_rank_FqM(x, pol, p);
    5148           7 :     default:       return -1;
    5149             :   }
    5150             : }
    5151             : #undef code
    5152             : 
    5153             : long
    5154         280 : rank(GEN x)
    5155             : {
    5156         280 :   pari_sp av = avma;
    5157             :   long r;
    5158             : 
    5159         280 :   if (typ(x)!=t_MAT) pari_err_TYPE("rank",x);
    5160         280 :   r = RgM_rank_fast(x);
    5161         280 :   if (r >= 0) return r;
    5162           7 :   (void)gauss_pivot(x, &r);
    5163           7 :   avma = av; return lg(x)-1 - r;
    5164             : }
    5165             : 
    5166             : /* d a t_VECSMALL of integers in 1..n. Return the vector of the d[i]
    5167             :  * followed by the missing indices */
    5168             : static GEN
    5169       12948 : perm_complete(GEN d, long n)
    5170             : {
    5171       12948 :   GEN y = cgetg(n+1, t_VECSMALL);
    5172       12948 :   long i, j = 1, k = n, l = lg(d);
    5173       12948 :   pari_sp av = avma;
    5174       12948 :   char *T = stack_calloc(n+1);
    5175       12948 :   for (i = 1; i < l; i++) T[d[i]] = 1;
    5176      130884 :   for (i = 1; i <= n; i++)
    5177      117936 :     if (T[i]) y[j++] = i; else y[k--] = i;
    5178       12948 :   avma = av; return y;
    5179             : }
    5180             : 
    5181             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
    5182             : static GEN
    5183       70888 : indexrank0(long n, long r, GEN d)
    5184             : {
    5185       70888 :   GEN p1, p2, res = cgetg(3,t_VEC);
    5186             :   long i, j;
    5187             : 
    5188       70888 :   r = n - r; /* now r = dim Im(x) */
    5189       70888 :   p1 = cgetg(r+1,t_VECSMALL); gel(res,1) = p1;
    5190       70888 :   p2 = cgetg(r+1,t_VECSMALL); gel(res,2) = p2;
    5191       70888 :   if (d)
    5192             :   {
    5193      389259 :     for (i=0,j=1; j<=n; j++)
    5194      319260 :       if (d[j]) { i++; p1[i] = d[j]; p2[i] = j; }
    5195       69999 :     vecsmall_sort(p1);
    5196             :   }
    5197       70888 :   return res;
    5198             : }
    5199             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
    5200             : static GEN
    5201        2366 : indeximage0(long n, long r, GEN d)
    5202             : {
    5203             :   long i, j;
    5204             :   GEN v;
    5205             : 
    5206        2366 :   r = n - r; /* now r = dim Im(x) */
    5207        2366 :   v = cgetg(r+1,t_VECSMALL);
    5208       16919 :   if (d) for (i=j=1; j<=n; j++)
    5209       14553 :     if (d[j]) v[i++] = j;
    5210        2366 :   return v;
    5211             : }
    5212             : /* x an m x n t_MAT, n > 0, r = dim Ker(x), d from gauss_pivot */
    5213             : static void
    5214        6474 : indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol)
    5215             : {
    5216        6474 :   GEN IR = indexrank0(n, r, d);
    5217        6474 :   *prow = perm_complete(gel(IR,1), m);
    5218        6474 :   *pcol = perm_complete(gel(IR,2), n);
    5219        6474 : }
    5220             : static void
    5221       66780 : init_indexrank(GEN x) {
    5222       66780 :   (void)new_chunk(3 + 2*lg(x)); /* HACK */
    5223       66780 : }
    5224             : 
    5225             : static GEN
    5226          28 : RgM_indexrank_FpM(GEN x, GEN p)
    5227             : {
    5228          28 :   pari_sp av = avma;
    5229             :   ulong pp;
    5230             :   GEN r;
    5231          28 :   x = RgM_Fp_init(x,p,&pp);
    5232          28 :   switch(pp)
    5233             :   {
    5234           7 :   case 0:  r = FpM_indexrank(x,p); break;
    5235           7 :   case 2:  r = F2m_indexrank(x); break;
    5236          14 :   default: r = Flm_indexrank(x,pp); break;
    5237             :   }
    5238          28 :   return gerepileupto(av, r);
    5239             : }
    5240             : 
    5241             : static GEN
    5242           0 : RgM_indexrank_FqM(GEN x, GEN pol, GEN p)
    5243             : {
    5244           0 :   pari_sp av = avma;
    5245           0 :   GEN T = RgX_to_FpX(pol, p);
    5246           0 :   GEN r = FqM_indexrank(RgM_to_FqM(x, T, p), T, p);
    5247           0 :   return gerepileupto(av, r);
    5248             : }
    5249             : 
    5250             : #define code(t1,t2) ((t1 << 6) | t2)
    5251             : static GEN
    5252       14273 : RgM_indexrank_fast(GEN x)
    5253             : {
    5254             :   GEN p, pol;
    5255             :   long pa;
    5256       14273 :   long t = RgM_type(x,&p,&pol,&pa);
    5257       14273 :   switch(t)
    5258             :   {
    5259         595 :     case t_INT:    return ZM_indexrank(x);
    5260        1148 :     case t_FRAC:   return QM_indexrank(x);
    5261          28 :     case t_INTMOD: return RgM_indexrank_FpM(x, p);
    5262          21 :     case t_FFELT:  return FFM_indexrank(x, pol);
    5263             :     case code(t_POLMOD, t_INTMOD):
    5264           0 :                    return RgM_indexrank_FqM(x, pol, p);
    5265       12481 :     default:       return NULL;
    5266             :   }
    5267             : }
    5268             : #undef code
    5269             : 
    5270             : GEN
    5271       14273 : indexrank(GEN x)
    5272             : {
    5273             :   pari_sp av;
    5274             :   long r;
    5275             :   GEN d;
    5276       14273 :   if (typ(x)!=t_MAT) pari_err_TYPE("indexrank",x);
    5277       14273 :   d = RgM_indexrank_fast(x);
    5278       14273 :   if (d) return d;
    5279       12481 :   av = avma;
    5280       12481 :   init_indexrank(x);
    5281       12481 :   d = gauss_pivot(x, &r);
    5282       12481 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5283             : }
    5284             : 
    5285             : GEN
    5286       16639 : FpM_indexrank(GEN x, GEN p) {
    5287       16639 :   pari_sp av = avma;
    5288             :   long r;
    5289             :   GEN d;
    5290       16639 :   init_indexrank(x);
    5291       16639 :   d = FpM_gauss_pivot(x,p,&r);
    5292       16639 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5293             : }
    5294             : 
    5295             : GEN
    5296       16534 : Flm_indexrank(GEN x, ulong p) {
    5297       16534 :   pari_sp av = avma;
    5298             :   long r;
    5299             :   GEN d;
    5300       16534 :   init_indexrank(x);
    5301       16534 :   d = Flm_pivots(x, p, &r, 0);
    5302       16534 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5303             : }
    5304             : 
    5305             : GEN
    5306           7 : F2m_indexrank(GEN x) {
    5307           7 :   pari_sp av = avma;
    5308             :   long r;
    5309             :   GEN d;
    5310           7 :   init_indexrank(x);
    5311           7 :   d = F2m_gauss_pivot(F2m_copy(x),&r);
    5312           7 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5313             : }
    5314             : 
    5315             : GEN
    5316           7 : F2xqM_indexrank(GEN x, GEN T) {
    5317           7 :   pari_sp av = avma;
    5318             :   long r;
    5319             :   GEN d;
    5320           7 :   init_indexrank(x);
    5321           7 :   d = F2xqM_gauss_pivot(x, T, &r);
    5322           7 :   avma = av; return indexrank0(lg(x) - 1, r, d);
    5323             : }
    5324             : 
    5325             : GEN
    5326           7 : FlxqM_indexrank(GEN x, GEN T, ulong p) {
    5327           7 :   pari_sp av = avma;
    5328             :   long r;
    5329             :   GEN d;
    5330           7 :   init_indexrank(x);
    5331           7 :   d = FlxqM_gauss_pivot(x, T, p, &r);
    5332           7 :   avma = av; return indexrank0(lg(x) - 1, r, d);
    5333             : }
    5334             : 
    5335             : GEN
    5336           7 : FqM_indexrank(GEN x, GEN T, GEN p) {
    5337           7 :   pari_sp av = avma;
    5338             :   long r;
    5339             :   GEN d;
    5340           7 :   init_indexrank(x);
    5341           7 :   d = FqM_gauss_pivot(x, T, p, &r);
    5342           7 :   avma = av; return indexrank0(lg(x) - 1, r, d);
    5343             : }
    5344             : 
    5345             : GEN
    5346        2366 : ZM_indeximage(GEN x) {
    5347        2366 :   pari_sp av = avma;
    5348             :   long r;
    5349             :   GEN d;
    5350        2366 :   init_indexrank(x);
    5351        2366 :   d = ZM_pivots(x,&r);
    5352        2366 :   avma = av; return indeximage0(lg(x)-1, r, d);
    5353             : }
    5354             : long
    5355       43025 : ZM_rank(GEN x) {
    5356       43025 :   pari_sp av = avma;
    5357             :   long r;
    5358       43025 :   (void)ZM_pivots(x,&r);
    5359       43025 :   avma = av; return lg(x)-1-r;
    5360             : }
    5361             : GEN
    5362       18732 : ZM_indexrank(GEN x) {
    5363       18732 :   pari_sp av = avma;
    5364             :   long r;
    5365             :   GEN d;
    5366       18732 :   init_indexrank(x);
    5367       18732 :   d = ZM_pivots(x,&r);
    5368       18732 :   avma = av; return indexrank0(lg(x)-1, r, d);
    5369             : }
    5370             : 
    5371             : long
    5372           0 : QM_rank(GEN x)
    5373             : {
    5374           0 :   pari_sp av = avma;
    5375           0 :   long r = ZM_rank(Q_primpart(x));
    5376           0 :   avma = av;
    5377           0 :   return r;
    5378             : }
    5379             : 
    5380             : GEN
    5381        1148 : QM_indexrank(GEN x)
    5382             : {
    5383        1148 :   pari_sp av = avma;
    5384        1148 :   GEN r = ZM_indexrank(Q_primpart(x));
    5385        1148 :   return gerepileupto(av, r);
    5386             : }
    5387             : 
    5388             : /*******************************************************************/
    5389             : /*                                                                 */
    5390             : /*                             ZabM                                */
    5391             : /*                                                                 */
    5392             : /*******************************************************************/
    5393             : 
    5394             : static GEN
    5395        1090 : FpXM_ratlift(GEN a, GEN q)
    5396             : {
    5397             :   GEN B, y;
    5398        1090 :   long i, j, l = lg(a), n;
    5399        1090 :   B = sqrti(shifti(q,-1));
    5400        1090 :   y = cgetg(l, t_MAT);
    5401        1090 :   if (l==1) return y;
    5402        1090 :   n = lgcols(a);
    5403        3562 :   for (i=1; i<l; i++)
    5404             :   {
    5405        2866 :     GEN yi = cgetg(n, t_COL);
    5406       40711 :     for (j=1; j<n; j++)
    5407             :     {
    5408       38239 :       GEN v = FpX_ratlift(gmael(a,i,j), q, B, B, NULL);
    5409       38239 :       if (!v) return NULL;
    5410       37845 :       gel(yi, j) = RgX_renormalize(v);
    5411             :     }
    5412        2472 :     gel(y,i) = yi;
    5413             :   }
    5414         696 :   return y;
    5415             : }
    5416             : 
    5417             : static GEN
    5418        2982 : FlmV_recover_pre(GEN a, GEN M, ulong p, ulong pi, long sv)
    5419             : {
    5420        2982 :   GEN a1 = gel(a,1);
    5421        2982 :   long i, j, k, l = lg(a1), n, lM = lg(M);
    5422        2982 :   GEN v = cgetg(lM, t_VECSMALL);
    5423        2982 :   GEN y = cgetg(l, t_MAT);
    5424        2982 :   if (l==1) return y;
    5425        2982 :   n = lgcols(a1);
    5426       24581 :   for (i=1; i<l; i++)
    5427             :   {
    5428       21599 :     GEN yi = cgetg(n, t_COL);
    5429      529467 :     for (j=1; j<n; j++)
    5430             :     {
    5431      507868 :       for (k=1; k<lM; k++) uel(v,k) = umael(gel(a,k),i,j);
    5432      507868 :       gel(yi, j) = Flm_Flc_mul_pre_Flx(M, v, p, pi, sv);
    5433             :     }
    5434       21599 :     gel(y,i) = yi;
    5435             :   }
    5436        2982 :   return y;
    5437             : }
    5438             : 
    5439             : static GEN
    5440           0 : FlkM_inv(GEN M, GEN P, ulong p)
    5441             : {
    5442           0 :   ulong pi = get_Fl_red(p);
    5443           0 :   GEN R = Flx_roots(P, p);
    5444           0 :   long l = lg(R), i;
    5445           0 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    5446           0 :   GEN V = cgetg(l, t_VEC);
    5447           0 :   for(i=1; i<l; i++)
    5448             :   {
    5449           0 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, pi);
    5450           0 :     GEN H = Flm_inv_sp(FlxM_eval_powers_pre(M, pows, p, pi), NULL, p);
    5451           0 :     if (!H) return NULL;
    5452           0 :     gel(V, i) = H;
    5453             :   }
    5454           0 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    5455             : }
    5456             : 
    5457             : static GEN
    5458        1892 : FlkM_adjoint(GEN M, GEN P, ulong p)
    5459             : {
    5460        1892 :   ulong pi = get_Fl_red(p);
    5461        1892 :   GEN R = Flx_roots(P, p);
    5462        1892 :   long l = lg(R), i;
    5463        1892 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    5464        1892 :   GEN V = cgetg(l, t_VEC);
    5465        9435 :   for(i=1; i<l; i++)
    5466             :   {
    5467        7543 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, pi);
    5468        7543 :     gel(V, i) = Flm_adjoint(FlxM_eval_powers_pre(M, pows, p, pi), p);
    5469             :   }
    5470        1892 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    5471             : }
    5472             : 
    5473             : 
    5474             : static GEN
    5475        1802 : ZabM_inv_slice(GEN A, GEN Q, GEN P, GEN *mod)
    5476             : {
    5477        1802 :   pari_sp av = avma;
    5478        1802 :   long i, n = lg(P)-1, w = varn(Q);
    5479             :   GEN H, T;
    5480        1802 :   if (n == 1)
    5481             :   {
    5482        1736 :     ulong p = uel(P,1);
    5483        1736 :     GEN Ap = FqM_to_FlxM(A, Q, utoi(p));
    5484        1736 :     GEN Qp = ZX_to_Flx(Q, p);
    5485        1736 :     GEN Hp = FlkM_adjoint(Ap, Qp, p);
    5486        1736 :     Hp = gerepileupto(av, FlxM_to_ZXM(Hp));
    5487        1736 :     *mod = utoi(p); return Hp;
    5488             :   }
    5489          66 :   T = ZV_producttree(P);
    5490          66 :   A = ZXM_nv_mod_tree(A, P, T, w);
    5491          66 :   Q = ZX_nv_mod_tree(Q, P, T);
    5492          66 :   H = cgetg(n+1, t_VEC);
    5493         222 :   for(i=1; i <= n; i++)
    5494             :   {
    5495         156 :     ulong p = P[i];
    5496         156 :     GEN a = gel(A,i), q = gel(Q, i);
    5497         156 :     gel(H,i) = FlkM_adjoint(a, q, p);
    5498             :   }
    5499          66 :   H = nxMV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    5500          66 :   *mod = gmael(T, lg(T)-1, 1);
    5501          66 :   gerepileall(av, 2, &H, mod);
    5502          66 :   return H;
    5503             : }
    5504             : 
    5505             : GEN
    5506        1802 : ZabM_inv_worker(GEN P, GEN A, GEN Q)
    5507             : {
    5508        1802 :   GEN V = cgetg(3, t_VEC);
    5509        1801 :   gel(V,1) = ZabM_inv_slice(A, Q, P, &gel(V,2));
    5510        1802 :   return V;
    5511             : }
    5512             : 
    5513             : static GEN
    5514        5292 : vecnorml1(GEN a)
    5515             : {
    5516             :   long i, l;
    5517        5292 :   GEN g = cgetg_copy(a, &l);
    5518       94976 :   for (i=1; i<l; i++)
    5519       89684 :     gel(g, i) = gnorml1_fake(gel(a,i));
    5520        5292 :   return g;
    5521             : }
    5522             : 
    5523             : static GEN
    5524        1232 : ZabM_true_Hadamard(GEN a)
    5525             : {
    5526        1232 :   pari_sp av = avma;
    5527        1232 :   long n = lg(a)-1, i;
    5528             :   GEN B;
    5529        1232 :   if (n == 0) return gen_1;
    5530        1232 :   if (n == 1) return gnorml1_fake(gcoeff(a,1,1));
    5531         728 :   B = gen_1;
    5532         728 :   for (i = 1; i <= n; i++) B = gmul(B, gnorml2(RgC_gtofp(vecnorml1(gel(a,i)),DEFAULTPREC)));
    5533         728 :   return gerepileuptoint(av, ceil_safe(sqrtr_abs(B)));
    5534             : }
    5535             : 
    5536             : GEN
    5537        1232 : ZabM_inv(GEN A, GEN Q, long n, GEN *pt_den)
    5538             : {
    5539        1232 :   pari_sp av = avma;
    5540        1232 :   long m = lg(A)-1;
    5541             :   GEN bnd, H, D, d, mod, worker;
    5542        1232 :   if (m == 0)
    5543             :   {
    5544           0 :     if (pt_den) *pt_den = gen_1;
    5545           0 :     return cgetg(1, t_MAT);
    5546             :   }
    5547        1232 :   bnd = ZabM_true_Hadamard(A);
    5548        1232 :   worker = strtoclosure("_ZabM_inv_worker", 2, A, Q);
    5549        1232 :   H = gen_crt("ZabM_inv", worker, mkvecsmall(n), expi(bnd), m, &mod,
    5550             :               nxMV_chinese_center, FpXM_center);
    5551        1232 :   D = RgMrow_RgC_mul(H, gel(A,1), 1);
    5552        1232 :   D = ZX_rem(D, Q);
    5553        1232 :   d = Z_content(mkvec2(H, D));
    5554        1232 :   if (d)
    5555             :   {
    5556         420 :     D = ZX_Z_divexact(D, d);
    5557         420 :     H = Q_div_to_int(H, d);
    5558             :   }
    5559        1232 :   if (pt_den)
    5560             :   {
    5561        1232 :     gerepileall(av, 2, &H, &D);
    5562        1232 :     *pt_den = D; return H;
    5563             :   }
    5564           0 :   return gerepileupto(av, H);
    5565             : }
    5566             : 
    5567             : GEN
    5568           0 : ZabM_inv_ratlift(GEN M, GEN P, long n, GEN *pden)
    5569             : {
    5570           0 :   pari_sp av2, av = avma;
    5571             :   GEN q, H;
    5572           0 :   ulong m = LONG_MAX>>1;
    5573           0 :   ulong p= 1 + m - (m % n);
    5574           0 :   long lM = lg(M);
    5575           0 :   if (lM == 1) { *pden = gen_1; return cgetg(1,t_MAT); }
    5576             : 
    5577           0 :   av2 = avma;
    5578           0 :   H = NULL;
    5579             :   for(;;)
    5580             :   {
    5581             :     GEN Hp, Pp, Mp, Hr;
    5582           0 :     do p += n; while(!uisprime(p));
    5583           0 :     Pp = ZX_to_Flx(P, p);
    5584           0 :     Mp = FqM_to_FlxM(M, P, utoi(p));
    5585           0 :     Hp = FlkM_inv(Mp, Pp, p);
    5586           0 :     if (!Hp) continue;
    5587           0 :     if (!H)
    5588             :     {
    5589           0 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p);
    5590           0 :       q = utoipos(p);
    5591             :     }
    5592             :     else
    5593           0 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    5594           0 :     Hr = FpXM_ratlift(H, q);
    5595           0 :     if (DEBUGLEVEL>5) err_printf("ZabM_inv mod %ld (ratlift=%ld)\n", p,!!Hr);
    5596           0 :     if (Hr) {/* DONE ? */
    5597           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    5598           0 :       GEN MH = ZXQM_mul(Hl, M, P);
    5599           0 :       if (*pden)
    5600           0 :       { if (RgM_isscalar(MH, *pden)) { H = Hl; break; }}
    5601             :       else
    5602           0 :       { if (RgM_isidentity(MH)) { H = Hl; *pden = gen_1; break; } }
    5603             :     }
    5604             : 
    5605           0 :     if (gc_needed(av,2))
    5606             :     {
    5607           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_inv");
    5608           0 :       gerepileall(av2, 2, &H, &q);
    5609             :     }
    5610           0 :   }
    5611           0 :   gerepileall(av, 2, &H, pden);
    5612           0 :   return H;
    5613             : }
    5614             : 
    5615             : static GEN
    5616        1090 : FlkM_ker(GEN M, GEN P, ulong p)
    5617             : {
    5618        1090 :   ulong pi = get_Fl_red(p);
    5619        1090 :   GEN R = Flx_roots(P, p);
    5620        1090 :   long l = lg(R), i, dP = degpol(P), r;
    5621             :   GEN M1, K, D;
    5622        1090 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    5623        1090 :   GEN V = cgetg(l, t_VEC);
    5624        1090 :   M1 = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,1), dP, p, pi), p, pi);
    5625        1090 :   K = Flm_ker_sp(M1, p, 2);
    5626        1090 :   r = lg(gel(K,1)); D = gel(K,2);
    5627        1090 :   gel(V, 1) = gel(K,1);
    5628        2266 :   for(i=2; i<l; i++)
    5629             :   {
    5630        1176 :     GEN Mi = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,i), dP, p, pi), p, pi);
    5631        1176 :     GEN K = Flm_ker_sp(Mi, p, 2);
    5632        1176 :     if (lg(gel(K,1)) != r || !zv_equal(D, gel(K,2))) return NULL;
    5633        1176 :     gel(V, i) = gel(K,1);
    5634             :   }
    5635        1090 :   return mkvec2(FlmV_recover_pre(V, W, p, pi, P[1]), D);
    5636             : }
    5637             : 
    5638             : GEN
    5639         686 : ZabM_ker(GEN M, GEN P, long n)
    5640             : {
    5641         686 :   pari_sp av2, av = avma;
    5642             :   GEN q, H, D;
    5643         686 :   ulong m = LONG_MAX>>1;
    5644         686 :   ulong p= 1 + m - (m % n);
    5645         686 :   av2 = avma;
    5646         686 :   H = NULL; D = NULL;
    5647             :   for(;;)
    5648             :   {
    5649             :     GEN Kp, Hp, Dp, Pp, Mp, Hr;
    5650       29149 :     do p += n; while(!uisprime(p));
    5651        1090 :     Pp = ZX_to_Flx(P, p);
    5652        1090 :     Mp = FqM_to_FlxM(M, P, utoi(p));
    5653        1090 :     Kp = FlkM_ker(Mp, Pp, p);
    5654        1090 :     if (!Kp) continue;
    5655        1090 :     Hp = gel(Kp,1); Dp = gel(Kp,2);
    5656        1090 :     if (H && (lg(Hp)>lg(H) || (lg(Hp)==lg(H) && vecsmall_lexcmp(Dp,D)>0))) continue;
    5657        1090 :     if (!H || (lg(Hp)<lg(H) || vecsmall_lexcmp(Dp,D)<0))
    5658             :     {
    5659         686 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p); D = Dp;
    5660         686 :       q = utoipos(p);
    5661             :     }
    5662             :     else
    5663         404 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    5664        1090 :     Hr = FpXM_ratlift(H, q);
    5665        1090 :     if (DEBUGLEVEL>5) err_printf("ZabM_ker mod %ld (ratlift=%ld)\n", p,!!Hr);
    5666        1090 :     if (Hr) {/* DONE ? */
    5667         696 :       GEN Hl = vec_Q_primpart(Hr);
    5668         696 :       GEN MH = ZXQM_mul(M, Hl,P);
    5669         696 :       if (gequal0(MH)) { H = Hl;  break; }
    5670             :     }
    5671             : 
    5672         404 :     if (gc_needed(av,2))
    5673             :     {
    5674           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_ker");
    5675           0 :       gerepileall(av2, 3, &H, &D, &q);
    5676             :     }
    5677         404 :   }
    5678         686 :   return gerepilecopy(av, H);
    5679             : }
    5680             : 
    5681             : GEN
    5682        2002 : ZabM_indexrank(GEN M, GEN P, long n)
    5683             : {
    5684        2002 :   pari_sp av = avma;
    5685        2002 :   ulong m = LONG_MAX>>1;
    5686        2002 :   ulong p = 1+m-(m%n), D = degpol(P);
    5687        2002 :   long lM = lg(M), lmax = 0, c = 0;
    5688             :   GEN v;
    5689             :   for(;;)
    5690             :   {
    5691             :     GEN R, Mp, K;
    5692             :     ulong pi;
    5693             :     long l;
    5694       79777 :     do p += n; while (!uisprime(p));
    5695        2674 :     pi = get_Fl_red(p);
    5696        2674 :     R = Flx_roots(ZX_to_Flx(P, p), p);
    5697        2674 :     Mp = FqM_to_FlxM(M, P, utoipos(p));
    5698        2674 :     K = FlxM_eval_powers_pre(Mp, Fl_powers_pre(uel(R,1), D,p,pi), p,pi);
    5699        2674 :     v = Flm_indexrank(K, p);
    5700        2674 :     l = lg(gel(v,2));
    5701        2674 :     if (l == lM) break;
    5702         896 :     if (lmax >= 0 && l > lmax) { lmax = l; c = 0; } else c++;
    5703         896 :     if (c > 2)
    5704             :     { /* probably not maximal rank, expensive check */
    5705         224 :       lM -= lg(ZabM_ker(M, P, n))-1; /* actual rank (+1) */
    5706         224 :       if (lmax == lM) break;
    5707           0 :       lmax = -1; /* disable check */
    5708             :     }
    5709         672 :   }
    5710        2002 :   return gerepileupto(av, v);
    5711             : }
    5712             : 
    5713             : #if 0
    5714             : GEN
    5715             : ZabM_gauss(GEN M, GEN P, long n, GEN *den)
    5716             : {
    5717             :   pari_sp av = avma;
    5718             :   GEN v, S, W;
    5719             :   v = ZabM_indexrank(M, P, n);
    5720             :   S = shallowmatextract(M,gel(v,1),gel(v,2));
    5721             :   W = ZabM_inv(S, P, n, den);
    5722             :   gerepileall(av,2,&W,den);
    5723             :   return W;
    5724             : }
    5725             : #endif
    5726             : 
    5727             : GEN
    5728         203 : ZabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *den)
    5729             : {
    5730         203 :   GEN v = ZabM_indexrank(M, P, n);
    5731         203 :   if (pv) *pv = v;
    5732         203 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    5733         203 :   return ZabM_inv(M, P, n, den);
    5734             : }
    5735             : GEN
    5736        4235 : ZM_pseudoinv(GEN M, GEN *pv, GEN *den)
    5737             : {
    5738        4235 :   GEN v = ZM_indexrank(M);
    5739        4235 :   if (pv) *pv = v;
    5740        4235 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    5741        4235 :   return ZM_inv(M, den);
    5742             : }
    5743             : 
    5744             : /*******************************************************************/
    5745             : /*                                                                 */
    5746             : /*                   Structured Elimination                        */
    5747             : /*                                                                 */
    5748             : /*******************************************************************/
    5749             : 
    5750             : static void
    5751       99653 : rem_col(GEN c, long i, GEN iscol, GEN Wrow, long *rcol, long *rrow)
    5752             : {
    5753       99653 :   long lc = lg(c), k;
    5754       99653 :   iscol[i] = 0; (*rcol)--;
    5755      882952 :   for (k = 1; k < lc; ++k)
    5756             :   {
    5757      783299 :     Wrow[c[k]]--;
    5758      783299 :     if (Wrow[c[k]]==0) (*rrow)--;
    5759             :   }
    5760       99653 : }
    5761             : 
    5762             : static void
    5763        5968 : rem_singleton(GEN M, GEN iscol, GEN Wrow, long *rcol, long *rrow)
    5764             : {
    5765             :   long i, j;
    5766        5968 :   long nbcol = lg(iscol)-1, last;
    5767             :   do
    5768             :   {
    5769        7819 :     last = 0;
    5770    18931887 :     for (i = 1; i <= nbcol; ++i)
    5771    18924068 :       if (iscol[i])
    5772             :       {
    5773     9731240 :         GEN c = gmael(M, i, 1);
    5774     9731240 :         long lc = lg(c);
    5775    90891816 :         for (j = 1; j < lc; ++j)
    5776    81172643 :           if (Wrow[c[j]] == 1)
    5777             :           {
    5778       12067 :             rem_col(c, i, iscol, Wrow, rcol, rrow);
    5779       12067 :             last=1; break;
    5780             :           }
    5781             :       }
    5782        7819 :   } while (last);
    5783        5968 : }
    5784             : 
    5785             : static GEN
    5786        5849 : fill_wcol(GEN M, GEN iscol, GEN Wrow, long *w, GEN wcol)
    5787             : {
    5788        5849 :   long nbcol = lg(iscol)-1;
    5789             :   long i, j, m, last;
    5790             :   GEN per;
    5791       14561 :   for (m = 2, last=0; !last ; m++)
    5792             :   {
    5793    22246144 :     for (i = 1; i <= nbcol; ++i)
    5794             :     {
    5795    22237432 :       wcol[i] = 0;
    5796    22237432 :       if (iscol[i])
    5797             :       {
    5798    11416272 :         GEN c = gmael(M, i, 1);
    5799    11416272 :         long lc = lg(c);
    5800   106642588 :         for (j = 1; j < lc; ++j)
    5801    95226316 :           if (Wrow[c[j]] == m) {  wcol[i]++; last = 1; }
    5802             :       }
    5803             :     }
    5804             :   }
    5805        5849 :   per = vecsmall_indexsort(wcol);
    5806        5849 :   *w = wcol[per[nbcol]];
    5807        5849 :   return per;
    5808             : }
    5809             : 
    5810             : /* M is a RgMs with nbrow rows, A a list of row indices.
    5811             :    Eliminate rows of M with a single entry that do not belong to A,
    5812             :    and the corresponding columns. Also eliminate columns until #colums=#rows.
    5813             :    Return pcol and prow:
    5814             :    pcol is a map from the new columns indices to the old one.
    5815             :    prow is a map from the old rows indices to the new one (0 if removed).
    5816             : */
    5817             : 
    5818             : void
    5819         119 : RgMs_structelim_col(GEN M, long nbcol, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    5820             : {
    5821             :   long i,j,k;
    5822         119 :   long lA = lg(A);
    5823         119 :   GEN prow = cgetg(nbrow+1, t_VECSMALL);
    5824         119 :   GEN pcol = zero_zv(nbcol);
    5825         119 :   pari_sp av = avma;
    5826         119 :   long rcol = nbcol, rrow = 0, imin = nbcol - usqrt(nbcol);
    5827         119 :   GEN iscol = const_vecsmall(nbcol, 1);
    5828         119 :   GEN Wrow  = zero_zv(nbrow);
    5829         119 :   GEN wcol = cgetg(nbcol+1, t_VECSMALL);
    5830         119 :   pari_sp av2=avma;
    5831      124999 :   for (i = 1; i <= nbcol; ++i)
    5832             :   {
    5833      124880 :     GEN F = gmael(M, i, 1);
    5834      124880 :     long l = lg(F)-1;
    5835     1104907 :     for (j = 1; j <= l; ++j)
    5836      980027 :       Wrow[F[j]]++;
    5837             :   }
    5838         119 :   for (j = 1; j < lA; ++j)
    5839             :   {
    5840         119 :     if (Wrow[A[j]] == 0) { *p_col=NULL; return; }
    5841           0 :     Wrow[A[j]] = -1;
    5842             :   }
    5843      221956 :   for (i = 1; i <= nbrow; ++i)
    5844      221837 :     if (Wrow[i])
    5845       66339 :       rrow++;
    5846         119 :   rem_singleton(M, iscol, Wrow, &rcol, &rrow);
    5847         119 :   if (rcol<rrow) pari_err_BUG("RgMs_structelim, rcol<rrow");
    5848        6087 :   for (; rcol>rrow;)
    5849             :   {
    5850             :     long w;
    5851        5849 :     GEN per = fill_wcol(M, iscol, Wrow, &w, wcol);
    5852       93435 :     for (i = nbcol; i>=imin && wcol[per[i]]>=w && rcol>rrow; i--)
    5853       87586 :       rem_col(gmael(M, per[i], 1), per[i], iscol, Wrow, &rcol, &rrow);
    5854        5849 :     rem_singleton(M, iscol, Wrow, &rcol, &rrow);
    5855        5849 :     avma = av2;
    5856             :   }
    5857      124999 :   for (j = 1, i = 1; i <= nbcol; ++i)
    5858      124880 :     if (iscol[i])
    5859       25227 :       pcol[j++] = i;
    5860         119 :   setlg(pcol,j);
    5861      221956 :   for (k = 1, i = 1; i <= nbrow; ++i)
    5862      221837 :     prow[i] = Wrow[i] ? k++: 0;
    5863         119 :   avma = av;
    5864         119 :   *p_col = pcol; *p_row = prow;
    5865             : }
    5866             : 
    5867             : void
    5868           0 : RgMs_structelim(GEN M, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    5869             : {
    5870           0 :   RgMs_structelim_col(M, lg(M)-1, nbrow, A, p_col, p_row);
    5871           0 : }
    5872             : 
    5873             : /*******************************************************************/
    5874             : /*                                                                 */
    5875             : /*                        EIGENVECTORS                             */
    5876             : /*   (independent eigenvectors, sorted by increasing eigenvalue)   */
    5877             : /*                                                                 */
    5878             : /*******************************************************************/
    5879             : 
    5880             : GEN
    5881          63 : mateigen(GEN x, long flag, long prec)
    5882             : {
    5883             :   GEN y, R, T;
    5884          63 :   long k, l, ex, n = lg(x);
    5885          63 :   pari_sp av = avma;
    5886             : 
    5887          63 :   if (typ(x)!=t_MAT) pari_err_TYPE("eigen",x);
    5888          63 :   if (n != 1 && n != lgcols(x)) pari_err_DIM("eigen");
    5889          63 :   if (flag < 0 || flag > 1) pari_err_FLAG("mateigen");
    5890          63 :   if (n == 1)
    5891             :   {
    5892          14 :     if (flag) retmkvec2(cgetg(1,t_VEC), cgetg(1,t_MAT));
    5893           7 :     return cgetg(1,t_VEC);
    5894             :   }
    5895          49 :   if (n == 2)
    5896             :   {
    5897          14 :     if (flag) retmkvec2(mkveccopy(gcoeff(x,1,1)), matid(1));
    5898           7 :     return matid(1);
    5899             :   }
    5900             : 
    5901          35 :   ex = 16 - prec2nbits(prec);
    5902          35 :   T = charpoly(x,0);
    5903          35 :   if (RgX_is_QX(T))
    5904             :   {
    5905          28 :     T = ZX_radical( Q_primpart(T) );
    5906          28 :     R = nfrootsQ(T);
    5907          28 :     if (lg(R)-1 < degpol(T))
    5908             :     { /* add missing complex roots */
    5909          14 :       GEN r = cleanroots(RgX_div(T, roots_to_pol(R, 0)), prec);
    5910          14 :       settyp(r, t_VEC);
    5911          14 :       R = shallowconcat(R, r);
    5912             :     }
    5913             :   }
    5914             :   else
    5915             :   {
    5916           7 :     GEN r1, v = vectrunc_init(lg(T));
    5917             :     long e;
    5918           7 :     R = cleanroots(T,prec);
    5919           7 :     r1 = NULL;
    5920          21 :     for (k = 1; k < lg(R); k++)
    5921             :     {
    5922          14 :       GEN r2 = gel(R,k), r = grndtoi(r2, &e);
    5923          14 :       if (e < ex) r2 = r;
    5924          14 :       if (r1)
    5925             :       {
    5926           7 :         r = gsub(r1,r2);
    5927           7 :         if (gequal0(r) || gexpo(r) < ex) continue;
    5928             :       }
    5929          14 :       vectrunc_append(v, r2);
    5930          14 :       r1 = r2;
    5931             :     }
    5932           7 :     R = v;
    5933             :   }
    5934             :   /* R = distinct complex roots of charpoly(x) */
    5935          35 :   l = lg(R); y = cgetg(l, t_VEC);
    5936         189 :   for (k = 1; k < l; k++)
    5937             :   {
    5938         154 :     GEN F = ker_aux(RgM_Rg_sub_shallow(x, gel(R,k)), x);
    5939         154 :     long d = lg(F)-1;
    5940         154 :     if (!d) pari_err_PREC("mateigen");
    5941         154 :     gel(y,k) = F;
    5942         154 :     if (flag) gel(R,k) = const_vec(d, gel(R,k));
    5943             :   }
    5944          35 :   y = shallowconcat1(y);
    5945          35 :   if (lg(y) > n) pari_err_PREC("mateigen");
    5946             :   /* lg(y) < n if x is not diagonalizable */
    5947          35 :   if (flag) y = mkvec2(shallowconcat1(R), y);
    5948          35 :   return gerepilecopy(av,y);
    5949             : }
    5950             : GEN
    5951           0 : eigen(GEN x, long prec) { return mateigen(x, 0, prec); }
    5952             : 
    5953             : /*******************************************************************/
    5954             : /*                                                                 */
    5955             : /*                           DETERMINANT                           */
    5956             : /*                                                                 */
    5957             : /*******************************************************************/
    5958             : 
    5959             : GEN
    5960        4046 : det0(GEN a,long flag)
    5961             : {
    5962        4046 :   switch(flag)
    5963             :   {
    5964        4032 :     case 0: return det(a);
    5965          14 :     case 1: return det2(a);
    5966           0 :     default: pari_err_FLAG("matdet");
    5967             :   }
    5968             :   return NULL; /* LCOV_EXCL_LINE */
    5969             : }
    5970             : 
    5971             : /* M a 2x2 matrix, returns det(M) */
    5972             : static GEN
    5973        5889 : RgM_det2(GEN M)
    5974             : {
    5975        5889 :   pari_sp av = avma;
    5976        5889 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    5977        5889 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    5978        5889 :   return gerepileupto(av, gsub(gmul(a,d), gmul(b,c)));
    5979             : }
    5980             : /* M a 2x2 ZM, returns det(M) */
    5981             : static GEN
    5982        8673 : ZM_det2(GEN M)
    5983             : {
    5984        8673 :   pari_sp av = avma;
    5985        8673 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    5986        8673 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    5987        8673 :   return gerepileuptoint(av, subii(mulii(a,d), mulii(b, c)));
    5988             : }
    5989             : /* M a 3x3 ZM, return det(M) */
    5990             : static GEN
    5991        2940 : ZM_det3(GEN M)
    5992             : {
    5993        2940 :   pari_sp av = avma;
    5994        2940 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2), c = gcoeff(M,1,3);
    5995        2940 :   GEN d = gcoeff(M,2,1), e = gcoeff(M,2,2), f = gcoeff(M,2,3);
    5996        2940 :   GEN g = gcoeff(M,3,1), h = gcoeff(M,3,2), i = gcoeff(M,3,3);
    5997        2940 :   GEN t, D = signe(i)? mulii(subii(mulii(a,e), mulii(b,d)), i): gen_0;
    5998        2940 :   if (signe(g))
    5999             :   {
    6000        2737 :     t = mulii(subii(mulii(b,f), mulii(c,e)), g);
    6001        2737 :     D = addii(D, t);
    6002             :   }
    6003        2940 :   if (signe(h))
    6004             :   {
    6005        2758 :     t = mulii(subii(mulii(c,d), mulii(a,f)), h);
    6006        2758 :     D = addii(D, t);
    6007             :   }
    6008        2940 :   return gerepileuptoint(av, D);
    6009             : }
    6010             : 
    6011             : static GEN
    6012        9959 : det_simple_gauss(GEN a, GEN data, pivot_fun pivot)
    6013             : {
    6014        9959 :   pari_sp av = avma;
    6015        9959 :   long i,j,k, s = 1, nbco = lg(a)-1;
    6016        9959 :   GEN p, x = gen_1;
    6017             : 
    6018        9959 :   a = RgM_shallowcopy(a);
    6019       79374 :   for (i=1; i<nbco; i++)
    6020             :   {
    6021       69422 :     k = pivot(a, data, i, NULL);
    6022       69422 :     if (k > nbco) return gerepilecopy(av, gcoeff(a,i,i));
    6023       69415 :     if (k != i)
    6024             :     { /* exchange the lines s.t. k = i */
    6025        4471 :       for (j=i; j<=nbco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    6026        4471 :       s = -s;
    6027             :     }
    6028       69415 :     p = gcoeff(a,i,i);
    6029             : 
    6030       69415 :     x = gmul(x,p);
    6031      406126 :     for (k=i+1; k<=nbco; k++)
    6032             :     {
    6033      336711 :       GEN m = gcoeff(a,i,k);
    6034      336711 :       if (gequal0(m)) continue;
    6035             : 
    6036      119215 :       m = gdiv(m,p);
    6037      792020 :       for (j=i+1; j<=nbco; j++)
    6038      672805 :         gcoeff(a,j,k) = gsub(gcoeff(a,j,k), gmul(m,gcoeff(a,j,i)));
    6039             :     }
    6040       69415 :     if (gc_needed(av,2))
    6041             :     {
    6042           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    6043           0 :       gerepileall(av,2, &a,&x);
    6044             :     }
    6045             :   }
    6046        9952 :   if (s < 0) x = gneg_i(x);
    6047        9952 :   return gerepileupto(av, gmul(x, gcoeff(a,nbco,nbco)));
    6048             : }
    6049             : 
    6050             : GEN
    6051        4517 : det2(GEN a)
    6052             : {
    6053             :   GEN data;
    6054             :   pivot_fun pivot;
    6055        4517 :   long n = lg(a)-1;
    6056        4517 :   if (typ(a)!=t_MAT) pari_err_TYPE("det2",a);
    6057        4517 :   if (!n) return gen_1;
    6058        4517 :   if (n != nbrows(a)) pari_err_DIM("det2");
    6059        4517 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    6060        4517 :   if (n == 2) return RgM_det2(a);
    6061        1729 :   pivot = get_pivot_fun(a, a, &data);
    6062        1729 :   return det_simple_gauss(a, data, pivot);
    6063             : }
    6064             : 
    6065             : static GEN
    6066         287 : mydiv(GEN x, GEN y)
    6067             : {
    6068         287 :   long tx = typ(x), ty = typ(y);
    6069         287 :   if (tx == ty && tx == t_POL && varn(x) == varn(y)) return RgX_div(x,y);
    6070         287 :   return gdiv(x,y);
    6071             : }
    6072             : 
    6073             : /* Assumes a a square t_MAT of dimension n > 0. Returns det(a) using
    6074             :  * Gauss-Bareiss. */
    6075             : static GEN
    6076          63 : det_bareiss(GEN a)
    6077             : {
    6078          63 :   pari_sp av = avma;
    6079          63 :   long nbco = lg(a)-1,i,j,k,s = 1;
    6080             :   GEN p, pprec;
    6081             : 
    6082          63 :   a = RgM_shallowcopy(a);
    6083         245 :   for (pprec=gen_1,i=1; i<nbco; i++,pprec=p)
    6084             :   {
    6085             :     GEN ci;
    6086         182 :     int diveuc = (gequal1(pprec)==0);
    6087             : 
    6088         182 :     p = gcoeff(a,i,i);
    6089         182 :     if (gequal0(p))
    6090             :     {
    6091           0 :       k=i+1; while (k<=nbco && gequal0(gcoeff(a,i,k))) k++;
    6092           0 :       if (k>nbco) return gerepilecopy(av, p);
    6093           0 :       swap(gel(a,k), gel(a,i)); s = -s;
    6094           0 :       p = gcoeff(a,i,i);
    6095             :     }
    6096         182 :     ci = gel(a,i);
    6097         539 :     for (k=i+1; k<=nbco; k++)
    6098             :     {
    6099         357 :       GEN ck = gel(a,k), m = gel(ck,i);
    6100         357 :       if (gequal0(m))
    6101             :       {
    6102           0 :         if (gequal1(p))
    6103             :         {
    6104           0 :           if (diveuc)
    6105           0 :             gel(a,k) = mydiv(gel(a,k), pprec);
    6106             :         }
    6107             :         else
    6108           0 :           for (j=i+1; j<=nbco; j++)
    6109             :           {
    6110           0 :             GEN p1 = gmul(p, gel(ck,j));
    6111           0 :             if (diveuc) p1 = mydiv(p1,pprec);
    6112           0 :             gel(ck,j) = p1;
    6113             :           }
    6114             :       }
    6115             :       else
    6116        1176 :         for (j=i+1; j<=nbco; j++)
    6117             :         {
    6118         819 :           pari_sp av2 = avma;
    6119         819 :           GEN p1 = gsub(gmul(p,gel(ck,j)), gmul(m,gel(ci,j)));
    6120         819 :           if (diveuc) p1 = mydiv(p1,pprec);
    6121         819 :           gel(ck,j) = gerepileupto(av2, p1);
    6122             :         }
    6123         357 :       if (gc_needed(av,2))
    6124             :       {
    6125           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    6126           0 :         gerepileall(av,2, &a,&pprec);
    6127           0 :         ci = gel(a,i);
    6128           0 :         p = gcoeff(a,i,i);
    6129             :       }
    6130             :     }
    6131             :   }
    6132          63 :   p = gcoeff(a,nbco,nbco);
    6133          63 :   p = (s < 0)? gneg(p): gcopy(p);
    6134          63 :   return gerepileupto(av, p);
    6135             : }
    6136             : 
    6137             : /* count non-zero entries in col j, at most 'max' of them.
    6138             :  * Return their indices */
    6139             : static GEN
    6140         259 : col_count_non_zero(GEN a, long j, long max)
    6141             : {
    6142         259 :   GEN v = cgetg(max+1, t_VECSMALL);
    6143         259 :   GEN c = gel(a,j);
    6144         259 :   long i, l = lg(a), k = 1;
    6145        1015 :   for (i = 1; i < l; i++)
    6146        1001 :     if (!gequal0(gel(c,i)))
    6147             :     {
    6148         973 :       if (k > max) return NULL; /* fail */
    6149         728 :       v[k++] = i;
    6150             :     }
    6151          14 :   setlg(v, k); return v;
    6152             : }
    6153             : /* count non-zero entries in row i, at most 'max' of them.
    6154             :  * Return their indices */
    6155             : static GEN
    6156         245 : row_count_non_zero(GEN a, long i, long max)
    6157             : {
    6158         245 :   GEN v = cgetg(max+1, t_VECSMALL);
    6159         245 :   long j, l = lg(a), k = 1;
    6160         959 :   for (j = 1; j < l; j++)
    6161         959 :     if (!gequal0(gcoeff(a,i,j)))
    6162             :     {
    6163         959 :       if (k > max) return NULL; /* fail */
    6164         714 :       v[k++] = j;
    6165             :     }
    6166           0 :   setlg(v, k); return v;
    6167             : }
    6168             : 
    6169             : static GEN det_develop(GEN a, long max, double bound);
    6170             : /* (-1)^(i+j) a[i,j] * det RgM_minor(a,i,j) */
    6171             : static GEN
    6172          14 : coeff_det(GEN a, long i, long j, long max, double bound)
    6173             : {
    6174          14 :   GEN c = gcoeff(a, i, j);
    6175          14 :   c = gmul(c, det_develop(RgM_minor(a, i,j), max, bound));
    6176          14 :   if (odd(i+j)) c = gneg(c);
    6177          14 :   return c;
    6178             : }
    6179             : /* a square t_MAT, 'bound' a rough upper bound for the number of
    6180             :  * multiplications we are willing to pay while developing rows/columns before
    6181             :  * switching to Gaussian elimination */
    6182             : static GEN
    6183          91 : det_develop(GEN M, long max, double bound)
    6184             : {
    6185          91 :   pari_sp av = avma;
    6186          91 :   long i,j, n = lg(M)-1, lbest = max+2, best_col = 0, best_row = 0;
    6187          91 :   GEN best = NULL;
    6188             : 
    6189          91 :   if (bound < 1.) return det_bareiss(M); /* too costly now */
    6190             : 
    6191          91 :   switch(n)
    6192             :   {
    6193           0 :     case 0: return gen_1;
    6194           0 :     case 1: return gcopy(gcoeff(M,1,1));
    6195          14 :     case 2: return RgM_det2(M);
    6196             :   }
    6197          77 :   if (max > ((n+2)>>1)) max = (n+2)>>1;
    6198         322 :   for (j = 1; j <= n; j++)
    6199             :   {
    6200         259 :     pari_sp av2 = avma;
    6201         259 :     GEN v = col_count_non_zero(M, j, max);
    6202             :     long lv;
    6203         259 :     if (!v || (lv = lg(v)) >= lbest) { avma = av2; continue; }
    6204          14 :     if (lv == 1) { avma = av; return gen_0; }
    6205          14 :     if (lv == 2) {
    6206          14 :       avma = av;
    6207          14 :       return gerepileupto(av, coeff_det(M,v[1],j,max,bound));
    6208             :     }
    6209           0 :     best = v; lbest = lv; best_col = j;
    6210             :   }
    6211         308 :   for (i = 1; i <= n; i++)
    6212             :   {
    6213         245 :     pari_sp av2 = avma;
    6214         245 :     GEN v = row_count_non_zero(M, i, max);
    6215             :     long lv;
    6216         245 :     if (!v || (lv = lg(v)) >= lbest) { avma = av2; continue; }
    6217           0 :     if (lv == 1) { avma = av; return gen_0; }
    6218           0 :     if (lv == 2) {
    6219           0 :       avma = av;
    6220           0 :       return gerepileupto(av, coeff_det(M,i,v[1],max,bound));
    6221             :     }
    6222           0 :     best = v; lbest = lv; best_row = i;
    6223             :   }
    6224          63 :   if (best_row)
    6225             :   {
    6226           0 :     double d = lbest-1;
    6227           0 :     GEN s = NULL;
    6228             :     long k;
    6229           0 :     bound /= d*d*d;
    6230           0 :     for (k = 1; k < lbest; k++)
    6231             :     {
    6232           0 :       GEN c = coeff_det(M, best_row, best[k], max, bound);
    6233           0 :       s = s? gadd(s, c): c;
    6234             :     }
    6235           0 :     return gerepileupto(av, s);
    6236             :   }
    6237          63 :   if (best_col)
    6238             :   {
    6239           0 :     double d = lbest-1;
    6240           0 :     GEN s = NULL;
    6241             :     long k;
    6242           0 :     bound /= d*d*d;
    6243           0 :     for (k = 1; k < lbest; k++)
    6244             :     {
    6245           0 :       GEN c = coeff_det(M, best[k], best_col, max, bound);
    6246           0 :       s = s? gadd(s, c): c;
    6247             :     }
    6248           0 :     return gerepileupto(av, s);
    6249             :   }
    6250          63 :   return det_bareiss(M);
    6251             : }
    6252             : 
    6253             : /* area of parallelogram bounded by (v1,v2) */
    6254             : static GEN
    6255       51779 : parallelogramarea(GEN v1, GEN v2)
    6256       51779 : { return gsub(gmul(gnorml2(v1), gnorml2(v2)), gsqr(RgV_dotproduct(v1, v2))); }
    6257             : 
    6258             : /* Square of Hadamard bound for det(a), a square matrix.
    6259             :  * Slightly improvement: instead of using the column norms, use the area of
    6260             :  * the parallelogram formed by pairs of consecutive vectors */
    6261             : GEN
    6262       16800 : RgM_Hadamard(GEN a)
    6263             : {
    6264       16800 :   pari_sp av = avma;
    6265       16800 :   long n = lg(a)-1, i;
    6266             :   GEN B;
    6267       16800 :   if (n == 0) return gen_1;
    6268       16800 :   if (n == 1) return gsqr(gcoeff(a,1,1));
    6269       16800 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    6270       16800 :   B = gen_1;
    6271       68579 :   for (i = 1; i <= n/2; i++)
    6272       51779 :     B = gmul(B, parallelogramarea(gel(a,2*i-1), gel(a,2*i)));
    6273       16800 :   if (odd(n)) B = gmul(B, gnorml2(gel(a, n)));
    6274       16800 :   return gerepileuptoint(av, ceil_safe(B));
    6275             : }
    6276             : 
    6277             : /* If B=NULL, assume B=A' */
    6278             : static GEN
    6279       52791 : ZM_det_slice(GEN A, GEN P, GEN *mod)
    6280             : {
    6281       52791 :   pari_sp av = avma;
    6282       52791 :   long i, n = lg(P)-1;
    6283             :   GEN H, T;
    6284       52791 :   if (n == 1)
    6285             :   {
    6286       41531 :     ulong Hp, p = uel(P,1);
    6287       41531 :     GEN a = ZM_to_Flm(A, p);
    6288       41528 :     Hp = Flm_det_sp(a, p);
    6289       41526 :     avma = av;
    6290       41526 :     *mod = utoi(p); return utoi(Hp);
    6291             :   }
    6292       11260 :   T = ZV_producttree(P);
    6293       11260 :   A = ZM_nv_mod_tree(A, P, T);
    6294       11260 :   H = cgetg(n+1, t_VECSMALL);
    6295       39858 :   for(i=1; i <= n; i++)
    6296             :   {
    6297       28599 :     ulong p = P[i];
    6298       28599 :     GEN a = gel(A,i);
    6299       28599 :     H[i] = Flm_det_sp(a, p);
    6300             :   }
    6301       11259 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    6302       11259 :   *mod = gmael(T, lg(T)-1, 1);
    6303       11259 :   gerepileall(av, 2, &H, mod);
    6304       11260 :   return H;
    6305             : }
    6306             : 
    6307             : GEN
    6308       52790 : ZM_det_worker(GEN P, GEN A)
    6309             : {
    6310       52790 :   GEN V = cgetg(3, t_VEC);
    6311       52791 :   gel(V,1) = ZM_det_slice(A, P, &gel(V,2));
    6312       52787 :   return V;
    6313             : }
    6314             : 
    6315             : /* assume dim(a) = n > 0 */
    6316             : static GEN
    6317       29253 : ZM_det_i(GEN M, long n)
    6318             : {
    6319       29253 :   const long DIXON_THRESHOLD = 40;
    6320       29253 :   pari_sp av = avma, av2;
    6321             :   long i;
    6322       29253 :   ulong p, Dp = 1;
    6323             :   forprime_t S;
    6324             :   pari_timer ti;
    6325             :   GEN H, D, mod, h, q, v, worker;
    6326       29253 :   if (n == 1) return icopy(gcoeff(M,1,1));
    6327       28413 :   if (n == 2) return ZM_det2(M);
    6328       19740 :   if (n == 3) return ZM_det3(M);
    6329       16800 :   if (DEBUGLEVEL >=4) timer_start(&ti);
    6330       16800 :   h = RgM_Hadamard(M);
    6331       16800 :   if (!signe(h)) { avma = av; return gen_0; }
    6332       16800 :   h = sqrti(h); q = gen_1;
    6333       16800 :   init_modular_big(&S);
    6334       16800 :   p = 0; /* -Wall */
    6335       33600 :   while( cmpii(q, h) <= 0 && (p = u_forprime_next(&S)) )
    6336             :   {
    6337       16800 :     av2 = avma; Dp = Flm_det_sp(ZM_to_Flm(M, p), p);
    6338       16800 :     avma = av2;
    6339       16800 :     if (Dp) break;
    6340           0 :     q = muliu(q, p);
    6341             :   }
    6342       16800 :   if (!p) pari_err_OVERFLOW("ZM_det [ran out of primes]");
    6343       16800 :   if (!Dp) { avma = av; return gen_0; }
    6344       16800 :   if (n <= DIXON_THRESHOLD)
    6345       16800 :     D = q;
    6346             :   else
    6347             :   {
    6348           0 :     av2 = avma;
    6349           0 :     v = cgetg(n+1, t_COL);
    6350           0 :     gel(v, 1) = gen_1; /* ensure content(v) = 1 */
    6351           0 :     for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    6352           0 :     D = Q_denom(ZM_gauss(M, v));
    6353           0 :     if (expi(D) < expi(h) >> 1)
    6354             :     { /* First try unlucky, try once more */
    6355           0 :       for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    6356           0 :       D = lcmii(D, Q_denom(ZM_gauss(M, v)));
    6357             :     }
    6358           0 :     D = gerepileuptoint(av2, D);
    6359           0 :     if (q != gen_1) D = lcmii(D, q);
    6360             :   }
    6361             :   /* determinant is a multiple of D */
    6362       16800 :   if (DEBUGLEVEL >=4)
    6363           0 :     timer_printf(&ti,"ZM_det: Dixon %ld/%ld bits",expi(D),expi(h));
    6364       16800 :   h = divii(h, D);
    6365       16800 :   worker = strtoclosure("_ZM_det_worker", 1, M);
    6366       16800 :   H = gen_crt("ZM_det", worker, D, expi(h)+1, lg(M)-1, &mod, ZV_chinese, NULL);
    6367       16800 :   if (D) H = Fp_div(H, D, mod);
    6368       16800 :   H = Fp_center(H, mod, shifti(mod,-1));
    6369       16800 :   if (D) H = mulii(H, D);
    6370       16800 :   return gerepileuptoint(av, H);
    6371             : }
    6372             : 
    6373             : static GEN
    6374        1519 : RgM_det_FpM(GEN a, GEN p)
    6375             : {
    6376        1519 :   pari_sp av = avma;
    6377             :   ulong pp, d;
    6378        1519 :   a = RgM_Fp_init(a,p,&pp);
    6379        1519 :   switch(pp)
    6380             :   {
    6381          70 :   case 0: return gerepileupto(av, Fp_to_mod(FpM_det(a,p),p)); break;
    6382          14 :   case 2: d = F2m_det(a); break;
    6383        1435 :   default:d = Flm_det_sp(a, pp); break;
    6384             :   }
    6385        1449 :   avma = av; return mkintmodu(d, pp);
    6386             : }
    6387             : 
    6388             : static GEN
    6389          42 : RgM_det_FqM(GEN x, GEN pol, GEN p)
    6390             : {
    6391          42 :   pari_sp av = avma;
    6392          42 :   GEN T = RgX_to_FpX(pol, p);
    6393          42 :   GEN b = FqM_det(RgM_to_FqM(x, T, p), T, p);
    6394          42 :   if (!b) { avma = av; return NULL; }
    6395          42 :   return gerepilecopy(av, mkpolmod(FpX_to_mod(b, p), FpX_to_mod(T, p)));
    6396             : }
    6397             : 
    6398             : #define code(t1,t2) ((t1 << 6) | t2)
    6399             : static GEN
    6400       10239 : RgM_det_fast(GEN x)
    6401             : {
    6402             :   GEN p, pol;
    6403             :   long pa;
    6404       10239 :   long t = RgM_type(x, &p,&pol,&pa);
    6405       10239 :   switch(t)
    6406             :   {
    6407         287 :     case t_INT:    return ZM_det(x);
    6408          21 :     case t_FRAC:   return QM_det(x);
    6409          63 :     case t_FFELT:  return FFM_det(x, pol);
    6410        1519 :     case t_INTMOD: return RgM_det_FpM(x, p);
    6411             :     case code(t_POLMOD, t_INTMOD):
    6412          42 :                    return RgM_det_FqM(x, pol, p);
    6413        8307 :     default:       return NULL;
    6414             :   }
    6415             : }
    6416             : #undef code
    6417             : 
    6418             : static long
    6419          77 : det_init_max(long n)
    6420             : {
    6421          77 :   if (n > 100) return 0;
    6422          77 :   if (n > 50) return 1;
    6423          77 :   if (n > 30) return 4;
    6424          77 :   return 7;
    6425             : }
    6426             : 
    6427             : GEN
    6428       13604 : det(GEN a)
    6429             : {
    6430       13604 :   long n = lg(a)-1;
    6431             :   double B;
    6432             :   GEN data, b;
    6433             :   pivot_fun pivot;
    6434             : 
    6435       13604 :   if (typ(a)!=t_MAT) pari_err_TYPE("det",a);
    6436       13604 :   if (!n) return gen_1;
    6437       13562 :   if (n != nbrows(a)) pari_err_DIM("det");
    6438       13555 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    6439       13326 :   if (n == 2) return RgM_det2(a);
    6440       10239 :   b = RgM_det_fast(a);
    6441       10239 :   if (b) return b;
    6442        8307 :   pivot = get_pivot_fun(a, a, &data);
    6443        8307 :   if (pivot != gauss_get_pivot_NZ) return det_simple_gauss(a, data, pivot);
    6444          77 :   B = (double)n;
    6445          77 :   return det_develop(a, det_init_max(n), B*B*B);
    6446             : }
    6447             : 
    6448             : GEN
    6449       29260 : ZM_det(GEN a)
    6450             : {
    6451       29260 :   long n = lg(a)-1;
    6452       29260 :   if (!n) return gen_1;
    6453       29253 :   return ZM_det_i(a, n);
    6454             : }
    6455             : 
    6456             : GEN
    6457          21 : QM_det(GEN M)
    6458             : {
    6459          21 :   pari_sp av = avma;
    6460          21 :   GEN cM, pM = Q_primitive_part(M, &cM);
    6461          21 :   GEN b = ZM_det(pM);
    6462          21 :   if (cM) b = gmul(b, gpowgs(cM, lg(M)-1));
    6463          21 :   return gerepileupto(av, b);
    6464             : }

Generated by: LCOV version 1.11