Code coverage tests

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

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

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

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

Generated by: LCOV version 1.16