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 29115-f22e516b23) Lines: 2767 3159 87.6 %
Date: 2024-05-18 08:06:58 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       13759 :     for (j=1; j<=m; j++)
     216       13151 :       if (!c[j])
     217             :       {
     218        9256 :         gcoeff(x,j,k) = ff->red(E,gcoeff(x,j,k));
     219        9256 :         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        5272 :         gcoeff(x,t,k) = g0;
     235        9673 :         for (i=k+1; i<=n; i++)
     236        4401 :           gcoeff(x,t,i) = ff->red(E, ff->add(E,gcoeff(x,t,i), ff->mul(E,piv,gcoeff(x,j,i))));
     237        5272 :         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      152219 : _gen_addmul(GEN b, long k, long i, GEN m, void *E, const struct bb_field *ff)
     292             : {
     293      152219 :   gel(b,i) = ff->red(E,gel(b,i));
     294      152219 :   gel(b,k) = ff->add(E,gel(b,k), ff->mul(E,m, gel(b,i)));
     295      152219 : }
     296             : 
     297             : static GEN
     298       61172 : _gen_get_col(GEN a, GEN b, long li, void *E, const struct bb_field *ff)
     299             : {
     300       61172 :   GEN u = cgetg(li+1,t_COL);
     301       61172 :   pari_sp av = avma;
     302             :   long i, j;
     303             : 
     304       61172 :   gel(u,li) = gerepileupto(av, ff->red(E,ff->mul(E,gel(b,li), gcoeff(a,li,li))));
     305      314257 :   for (i=li-1; i>0; i--)
     306             :   {
     307      253085 :     pari_sp av = avma;
     308      253085 :     GEN m = gel(b,i);
     309     1005030 :     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      253085 :     m = ff->red(E, m);
     311      253085 :     gel(u,i) = gerepileupto(av, ff->red(E,ff->mul(E,m, gcoeff(a,i,i))));
     312             :   }
     313       61172 :   return u;
     314             : }
     315             : 
     316             : GEN
     317       13743 : gen_Gauss(GEN a, GEN b, void *E, const struct bb_field *ff)
     318             : {
     319             :   long i, j, k, li, bco, aco;
     320       13743 :   GEN u, g0 = ff->s(E,0);
     321       13743 :   pari_sp av = avma;
     322       13743 :   a = RgM_shallowcopy(a);
     323       13743 :   b = RgM_shallowcopy(b);
     324       13743 :   aco = lg(a)-1; bco = lg(b)-1; li = nbrows(a);
     325       59926 :   for (i=1; i<=aco; i++)
     326             :   {
     327             :     GEN invpiv;
     328       72132 :     for (k = i; k <= li; k++)
     329             :     {
     330       72076 :       GEN piv = ff->red(E,gcoeff(a,k,i));
     331       72076 :       if (!ff->equal0(piv)) { gcoeff(a,k,i) = ff->inv(E,piv); break; }
     332       12206 :       gcoeff(a,k,i) = g0;
     333             :     }
     334             :     /* found a pivot on line k */
     335       59926 :     if (k > li) return NULL;
     336       59870 :     if (k != i)
     337             :     { /* swap lines so that k = i */
     338       50677 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
     339       67853 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
     340             :     }
     341       59870 :     if (i == aco) break;
     342             : 
     343       46183 :     invpiv = gcoeff(a,i,i); /* 1/piv mod p */
     344      173541 :     for (k=i+1; k<=li; k++)
     345             :     {
     346      127358 :       GEN m = ff->red(E,gcoeff(a,k,i)); gcoeff(a,k,i) = g0;
     347      127358 :       if (ff->equal0(m)) continue;
     348             : 
     349       18712 :       m = ff->red(E,ff->neg(E,ff->mul(E,m, invpiv)));
     350       74992 :       for (j=i+1; j<=aco; j++) _gen_addmul(gel(a,j),k,i,m,E,ff);
     351      114651 :       for (j=1  ; j<=bco; j++) _gen_addmul(gel(b,j),k,i,m,E,ff);
     352             :     }
     353       46183 :     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       13687 :   if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
     361       13687 :   u = cgetg(bco+1,t_MAT);
     362       74859 :   for (j=1; j<=bco; j++) gel(u,j) = _gen_get_col(a, gel(b,j), aco, E, ff);
     363       13687 :   return u;
     364             : }
     365             : 
     366             : /* compatible t_MAT * t_COL, lgA = lg(A) = lg(B) > 1, l = lgcols(A) */
     367             : static GEN
     368      631002 : gen_matcolmul_i(GEN A, GEN B, ulong lgA, ulong l,
     369             :                 void *E, const struct bb_field *ff)
     370             : {
     371      631002 :   GEN C = cgetg(l, t_COL);
     372             :   ulong i;
     373     4152844 :   for (i = 1; i < l; i++) {
     374     3521842 :     pari_sp av = avma;
     375     3521842 :     GEN e = ff->mul(E, gcoeff(A, i, 1), gel(B, 1));
     376             :     ulong k;
     377    15208558 :     for(k = 2; k < lgA; k++)
     378    11686716 :       e = ff->add(E, e, ff->mul(E, gcoeff(A, i, k), gel(B, k)));
     379     3521842 :     gel(C, i) = gerepileupto(av, ff->red(E, e));
     380             :   }
     381      631002 :   return C;
     382             : }
     383             : 
     384             : GEN
     385      199558 : gen_matcolmul(GEN A, GEN B, void *E, const struct bb_field *ff)
     386             : {
     387      199558 :   ulong lgA = lg(A);
     388      199558 :   if (lgA != (ulong)lg(B))
     389           0 :     pari_err_OP("operation 'gen_matcolmul'", A, B);
     390      199558 :   if (lgA == 1)
     391           0 :     return cgetg(1, t_COL);
     392      199558 :   return gen_matcolmul_i(A, B, lgA, lgcols(A), E, ff);
     393             : }
     394             : 
     395             : static GEN
     396       83789 : 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       83789 :   GEN C = cgetg(lb, t_MAT);
     401      515233 :   for(j = 1; j < lb; j++)
     402      431444 :     gel(C, j) = gen_matcolmul_i(A, gel(B, j), la, l, E, ff);
     403       83789 :   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       83789 : gen_matmul_i(GEN A, GEN B, long l, long la, long lb,
     587             :              void *E, const struct bb_field *ff)
     588             : {
     589       83789 :   if (l <= gen_matmul_sw_bound
     590           7 :       || la <= gen_matmul_sw_bound
     591           0 :       || lb <= gen_matmul_sw_bound)
     592       83789 :     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       83789 : gen_matmul(GEN A, GEN B, void *E, const struct bb_field *ff)
     599             : {
     600       83789 :   ulong lgA, lgB = lg(B);
     601       83789 :   if (lgB == 1)
     602           0 :     return cgetg(1, t_MAT);
     603       83789 :   lgA = lg(A);
     604       83789 :   if (lgA != (ulong)lgcols(B))
     605           0 :     pari_err_OP("operation 'gen_matmul'", A, B);
     606       83789 :   if (lgA == 1)
     607           0 :     return zeromat(0, lgB - 1);
     608       83789 :   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      352266 : image_from_pivot(GEN x, GEN d, long r)
     785             : {
     786             :   GEN y;
     787             :   long j, k;
     788             : 
     789      352266 :   if (!d) return gcopy(x);
     790             :   /* d left on stack for efficiency */
     791      348587 :   r = lg(x)-1 - r; /* = dim Im(x) */
     792      348587 :   y = cgetg(r+1,t_MAT);
     793     2020127 :   for (j=k=1; j<=r; k++)
     794     1671540 :     if (d[k]) gel(y,j++) = gcopy(gel(x,k));
     795      348587 :   return y;
     796             : }
     797             : 
     798             : /* r = dim Ker x, n = nbrows(x) */
     799             : static GEN
     800      261106 : get_suppl(GEN x, GEN d, long n, long r, GEN(*ei)(long,long))
     801             : {
     802             :   pari_sp av;
     803             :   GEN y, c;
     804      261106 :   long j, k, rx = lg(x)-1; /* != 0 due to init_suppl() */
     805             : 
     806      261106 :   if (rx == n && r == 0) return gcopy(x);
     807      191481 :   y = cgetg(n+1, t_MAT);
     808      191483 :   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      810968 :   for (k = j = 1; j<=rx; j++)
     812      619485 :     if (d[j]) { c[ d[j] ] = 1; gel(y,k++) = gel(x,j); }
     813     1139260 :   for (j=1; j<=n; j++)
     814      947777 :     if (!c[j]) gel(y,k++) = (GEN)j; /* HACK */
     815      191483 :   set_avma(av);
     816             : 
     817      191483 :   rx -= r;
     818      810897 :   for (j=1; j<=rx; j++) gel(y,j) = gcopy(gel(y,j));
     819      519844 :   for (   ; j<=n; j++)  gel(y,j) = ei(n, y[j]);
     820      191482 :   return y;
     821             : }
     822             : 
     823             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
     824             : static GEN
     825     1929867 : indexrank0(long n, long r, GEN d)
     826             : {
     827     1929867 :   GEN p1, p2, res = cgetg(3,t_VEC);
     828             :   long i, j;
     829             : 
     830     1929867 :   r = n - r; /* now r = dim Im(x) */
     831     1929867 :   p1 = cgetg(r+1,t_VECSMALL); gel(res,1) = p1;
     832     1929867 :   p2 = cgetg(r+1,t_VECSMALL); gel(res,2) = p2;
     833     1929866 :   if (d)
     834             :   {
     835     7719997 :     for (i=0,j=1; j<=n; j++)
     836     5791495 :       if (d[j]) { i++; p1[i] = d[j]; p2[i] = j; }
     837     1928502 :     vecsmall_sort(p1);
     838             :   }
     839     1929874 :   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        5087 :       swap(gel(A, j), gel(A, pc));
    1068        5087 :       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        3958 : gen_gauss(GEN a, GEN b, void *E, const struct bb_field *ff,
    1318             :           GEN (*mul)(void*, GEN, GEN))
    1319             : {
    1320        3958 :   if (lg(a) - 1 >= gen_CUP_LIMIT)
    1321         559 :     return gen_gauss_CUP(a, b, E, ff, mul);
    1322        3399 :   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      157824 : FlxqM_mul(GEN A, GEN B, GEN T, ulong p) {
    1455             :   void *E;
    1456             :   const struct bb_field *ff;
    1457      157824 :   long n = lg(A) - 1;
    1458             : 
    1459      157824 :   if (n == 0)
    1460           0 :     return cgetg(1, t_MAT);
    1461      157824 :   if (n > 1)
    1462       89013 :     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     1228336 : FpM_init(GEN a, GEN p, ulong *pp)
    1521             : {
    1522     1228336 :   if (lgefint(p) == 3)
    1523             :   {
    1524     1223149 :     *pp = uel(p,2);
    1525     1223149 :     return (*pp==2)? ZM_to_F2m(a): ZM_to_Flm(a, *pp);
    1526             :   }
    1527        5187 :   *pp = 0; return a;
    1528             : }
    1529             : static GEN
    1530     1278564 : FpM_init3(GEN a, GEN p, ulong *pp)
    1531             : {
    1532     1278564 :   if (lgefint(p) == 3)
    1533             :   {
    1534     1275988 :     *pp = uel(p,2);
    1535     1275988 :     switch(*pp)
    1536             :     {
    1537      701568 :       case 2: return ZM_to_F2m(a);
    1538      154225 :       case 3: return ZM_to_F3m(a);
    1539      420195 :       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      602054 : FpM_gauss_pivot(GEN x, GEN p, long *rr)
    1625             : {
    1626             :   ulong pp;
    1627      602054 :   if (lg(x)==1) { *rr = 0; return NULL; }
    1628      599026 :   x = FpM_init(x, p, &pp);
    1629      599033 :   switch(pp)
    1630             :   {
    1631        1214 :   case 0: return FpM_gauss_pivot_gen(x, p, rr);
    1632      345310 :   case 2: return F2m_gauss_pivot(x, rr);
    1633      252509 :   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      312428 : FpM_image(GEN x, GEN p)
    1677             : {
    1678             :   long r;
    1679      312428 :   GEN d = FpM_gauss_pivot(x,p,&r); /* d left on stack for efficiency */
    1680      312429 :   return image_from_pivot(x,d,r);
    1681             : }
    1682             : 
    1683             : GEN
    1684       39739 : Flm_image(GEN x, ulong p)
    1685             : {
    1686             :   long r;
    1687       39739 :   GEN d = Flm_pivots(x, p, &r, 0); /* d left on stack for efficiency */
    1688       39739 :   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      296299 : FpM_FpC_invimage(GEN A, GEN x, GEN p)
    1821             : {
    1822      296299 :   pari_sp av = avma;
    1823             :   ulong pp;
    1824             :   GEN y;
    1825             : 
    1826      296299 :   A = FpM_init(A, p, &pp);
    1827      296316 :   switch(pp)
    1828             :   {
    1829           8 :   case 0: return FpM_FpC_invimage_gen(A, x, p);
    1830      193059 :   case 2:
    1831      193059 :     y = F2m_F2c_invimage(A, ZV_to_F2v(x));
    1832      193054 :     if (!y) return y;
    1833      193054 :     y = F2c_to_ZC(y);
    1834      193053 :     return gerepileupto(av, y);
    1835      103249 :   default:
    1836      103249 :     y = Flm_Flc_invimage(A, ZV_to_Flv(x, pp), pp);
    1837      103249 :     if (!y) return y;
    1838      103249 :     y = Flc_to_ZC(y);
    1839      103249 :     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     1278566 : FpM_ker_i(GEN x, GEN p, long deplin)
    1873             : {
    1874     1278566 :   pari_sp av = avma;
    1875             :   ulong pp;
    1876             :   GEN y;
    1877             : 
    1878     1278566 :   if (lg(x)==1) return cgetg(1,t_MAT);
    1879     1278566 :   x = FpM_init3(x, p, &pp);
    1880     1278598 :   switch(pp)
    1881             :   {
    1882        2576 :   case 0: return FpM_ker_gen(x,p,deplin);
    1883      701598 :   case 2:
    1884      701598 :     y = F2m_ker_sp(x, deplin);
    1885      701589 :     if (!y) return gc_NULL(av);
    1886      701603 :     y = deplin? F2c_to_ZC(y): F2m_to_ZM(y);
    1887      701597 :     return gerepileupto(av, y);
    1888      154224 :   case 3:
    1889      154224 :     y = F3m_ker_sp(x, deplin);
    1890      154224 :     if (!y) return gc_NULL(av);
    1891      154225 :     y = deplin? F3c_to_ZC(y): F3m_to_ZM(y);
    1892      154225 :     return gerepileupto(av, y);
    1893      420200 :   default:
    1894      420200 :     y = Flm_ker_sp(x, pp, deplin);
    1895      420200 :     if (!y) return gc_NULL(av);
    1896      420200 :     y = deplin? Flc_to_ZC(y): Flm_to_ZM(y);
    1897      420200 :     return gerepileupto(av, y);
    1898             :   }
    1899             : }
    1900             : 
    1901             : GEN
    1902      817206 : 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       10540 : FqM_ker_i(GEN x, GEN T, GEN p, long deplin)
    1943             : {
    1944       10540 :   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       10456 : FqM_ker(GEN x, GEN T, GEN p) { return FqM_ker_i(x,T,p,0); }
    1964             : 
    1965             : GEN
    1966      453892 : 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        3650 : FpM_gauss_gen(GEN a, GEN b, GEN p)
    1985             : {
    1986             :   void *E;
    1987        3650 :   const struct bb_field *S = get_Fp_field(&E,p);
    1988        3650 :   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      329098 : FpM_gauss_i(GEN a, GEN b, GEN p, ulong *pp)
    1993             : {
    1994      329098 :   long n = nbrows(a);
    1995      329096 :   a = FpM_init(a,p,pp);
    1996      329092 :   switch(*pp)
    1997             :   {
    1998        3650 :   case 0:
    1999        3650 :     if (!b) b = matid(n);
    2000        3650 :     return FpM_gauss_gen(a,b,p);
    2001      224729 :   case 2:
    2002      224729 :     if (b) b = ZM_to_F2m(b); else b = matid_F2m(n);
    2003      224730 :     return F2m_gauss_sp(a,b);
    2004      100713 :   default:
    2005      100713 :     if (b) b = ZM_to_Flm(b, *pp); else b = matid_Flm(n);
    2006      100713 :     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      329049 : FpM_inv(GEN a, GEN p)
    2140             : {
    2141      329049 :   pari_sp av = avma;
    2142             :   ulong pp;
    2143             :   GEN u;
    2144      329049 :   if (lg(a) == 1) return cgetg(1, t_MAT);
    2145      329049 :   u = FpM_gauss_i(a, NULL, p, &pp);
    2146      329046 :   if (!u) return gc_NULL(av);
    2147      329032 :   switch(pp)
    2148             :   {
    2149        3594 :   case 0: return gerepilecopy(av, u);
    2150      224725 :   case 2:  u = F2m_to_ZM(u); break;
    2151      100713 :   default: u = Flm_to_ZM(u); break;
    2152             :   }
    2153      325441 :   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      263832 : FpM_intersect_i(GEN x, GEN y, GEN p)
    2192             : {
    2193      263832 :   long j, lx = lg(x);
    2194             :   GEN z;
    2195             : 
    2196      263832 :   if (lx == 1 || lg(y) == 1) return cgetg(1,t_MAT);
    2197      263832 :   if (lgefint(p) == 3)
    2198             :   {
    2199      263832 :     ulong pp = p[2];
    2200      263832 :     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      261109 : init_suppl(GEN x)
    2223             : {
    2224      261109 :   if (lg(x) == 1) pari_err_IMPL("suppl [empty matrix]");
    2225             :   /* HACK: avoid overwriting d from gauss_pivot() after set_avma(av) */
    2226      261109 :   (void)new_chunk(lgcols(x) * 2);
    2227      261108 : }
    2228             : 
    2229             : GEN
    2230      259542 : FpM_suppl(GEN x, GEN p)
    2231             : {
    2232             :   GEN d;
    2233             :   long r;
    2234      259542 :   init_suppl(x); d = FpM_gauss_pivot(x,p, &r);
    2235      259540 :   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        4976 : FqM_suppl(GEN x, GEN T, GEN p)
    2274             : {
    2275        4976 :   pari_sp av = avma;
    2276             :   GEN d;
    2277             :   long r;
    2278             : 
    2279        4976 :   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     1910335 : init_indexrank(GEN x) {
    2287     1910335 :   (void)new_chunk(3 + 2*lg(x)); /* HACK */
    2288     1910334 : }
    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       53023 : Flm_indexrank(GEN x, ulong p) {
    2302       53023 :   pari_sp av = avma;
    2303             :   long r;
    2304             :   GEN d;
    2305       53023 :   init_indexrank(x);
    2306       53022 :   d = Flm_pivots(x, p, &r, 0);
    2307       53025 :   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     1919984 : gauss_get_pivot_max(GEN X, GEN X0, long ix, GEN c)
    2359             : {
    2360     1919984 :   GEN p, r, x = gel(X,ix), x0 = gel(X0,ix);
    2361     1919984 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2362     1919984 :   if (c)
    2363             :   {
    2364      324342 :     for (i=1; i<lx; i++)
    2365      187262 :       if (!c[i])
    2366             :       {
    2367       89947 :         long e = gexpo(gel(x,i));
    2368       89946 :         if (e > ex) { ex = e; k = i; }
    2369             :       }
    2370             :   }
    2371             :   else
    2372             :   {
    2373     6438416 :     for (i=ix; i<lx; i++)
    2374             :     {
    2375     4655508 :       long e = gexpo(gel(x,i));
    2376     4655513 :       if (e > ex) { ex = e; k = i; }
    2377             :     }
    2378             :   }
    2379     1919988 :   if (!k) return lx;
    2380     1851389 :   p = gel(x,k);
    2381     1851389 :   r = gel(x0,k); if (isrationalzero(r)) r = x0;
    2382     1851392 :   return cx_approx0(p, r)? lx: k;
    2383             : }
    2384             : static long
    2385      200735 : gauss_get_pivot_padic(GEN X, GEN p, long ix, GEN c)
    2386             : {
    2387      200735 :   GEN x = gel(X, ix);
    2388      200735 :   long i, k = 0, ex = (long)HIGHVALPBIT, lx = lg(x);
    2389      200735 :   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     1715332 :     for (i=ix; i<lx; i++)
    2401     1514723 :       if (!gequal0(gel(x,i)))
    2402             :       {
    2403     1143526 :         long e = gvaluation(gel(x,i), p);
    2404     1143526 :         if (e < ex) { ex = e; k = i; }
    2405             :       }
    2406             :   }
    2407      200735 :   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      600943 : get_pivot_fun(GEN x, GEN x0, GEN *data)
    2435             : {
    2436      600943 :   long i, j, hx, lx = lg(x);
    2437      600943 :   int res = t_INT;
    2438      600943 :   GEN p = NULL;
    2439             : 
    2440      600943 :   *data = NULL;
    2441      600943 :   if (lx == 1) return &gauss_get_pivot_NZ;
    2442      600908 :   hx = lgcols(x);
    2443     2785614 :   for (j=1; j<lx; j++)
    2444             :   {
    2445     2184748 :     GEN xj = gel(x,j);
    2446    12821886 :     for (i=1; i<hx; i++)
    2447             :     {
    2448    10637180 :       GEN c = gel(xj,i);
    2449    10637180 :       switch(typ(c))
    2450             :       {
    2451     5690442 :         case t_REAL:
    2452     5690442 :           res = t_REAL;
    2453     5690442 :           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     3766630 :         case t_INT: case t_INTMOD: case t_FRAC: case t_FFELT: case t_QUAD:
    2458             :         case t_POLMOD: /* exact types */
    2459     3766630 :           break;
    2460     1176426 :         case t_PADIC:
    2461     1176426 :           p = gel(c,2);
    2462     1176426 :           res = t_PADIC;
    2463     1176426 :           break;
    2464          42 :         default: return &gauss_get_pivot_NZ;
    2465             :       }
    2466             :     }
    2467             :   }
    2468      600866 :   switch(res)
    2469             :   {
    2470      572694 :     case t_REAL: *data = x0; return &gauss_get_pivot_max;
    2471       26830 :     case t_PADIC: *data = p; return &gauss_get_pivot_padic;
    2472        1342 :     default: return &gauss_get_pivot_NZ;
    2473             :   }
    2474             : }
    2475             : 
    2476             : static GEN
    2477      675190 : get_col(GEN a, GEN b, GEN p, long li)
    2478             : {
    2479      675190 :   GEN u = cgetg(li+1,t_COL);
    2480             :   long i, j;
    2481             : 
    2482      675190 :   gel(u,li) = gdiv(gel(b,li), p);
    2483     2960911 :   for (i=li-1; i>0; i--)
    2484             :   {
    2485     2285725 :     pari_sp av = avma;
    2486     2285725 :     GEN m = gel(b,i);
    2487    10613196 :     for (j=i+1; j<=li; j++) m = gsub(m, gmul(gcoeff(a,i,j), gel(u,j)));
    2488     2285706 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(a,i,i)));
    2489             :   }
    2490      675186 :   return u;
    2491             : }
    2492             : 
    2493             : /* bk -= m * bi */
    2494             : static void
    2495    10541679 : _submul(GEN b, long k, long i, GEN m)
    2496             : {
    2497    10541679 :   gel(b,k) = gsub(gel(b,k), gmul(m, gel(b,i)));
    2498    10541599 : }
    2499             : static int
    2500     2218347 : init_gauss(GEN a, GEN *b, long *aco, long *li, int *iscol)
    2501             : {
    2502     2218347 :   *iscol = *b ? (typ(*b) == t_COL): 0;
    2503     2218347 :   *aco = lg(a) - 1;
    2504     2218347 :   if (!*aco) /* a empty */
    2505             :   {
    2506          70 :     if (*b && lg(*b) != 1) pari_err_DIM("gauss");
    2507          70 :     *li = 0; return 0;
    2508             :   }
    2509     2218277 :   *li = nbrows(a);
    2510     2218271 :   if (*li < *aco) pari_err_INV("gauss [no left inverse]", a);
    2511     2218273 :   if (*b)
    2512             :   {
    2513     2137851 :     switch(typ(*b))
    2514             :     {
    2515      125135 :       case t_MAT:
    2516      125135 :         if (lg(*b) == 1) return 0;
    2517      125135 :         *b = RgM_shallowcopy(*b);
    2518      125135 :         break;
    2519     2012725 :       case t_COL:
    2520     2012725 :         *b = mkmat( leafcopy(*b) );
    2521     2012724 :         break;
    2522           0 :       default: pari_err_TYPE("gauss",*b);
    2523             :     }
    2524     2137859 :     if (nbrows(*b) != *li) pari_err_DIM("gauss");
    2525             :   }
    2526             :   else
    2527       80422 :     *b = matid(*li);
    2528     2218272 :   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      204628 : RgM_inv_fast(GEN x)
    2567             : {
    2568             :   GEN p, pol;
    2569             :   long pa;
    2570      204628 :   long t = RgM_type(x, &p,&pol,&pa);
    2571      204629 :   switch(t)
    2572             :   {
    2573       45689 :     case t_INT:    /* Fall back */
    2574       45689 :     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          42 :     case code(t_POLMOD, t_INTMOD):
    2578          42 :                    return RgM_inv_FqM(x, pol, p);
    2579      158639 :     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     1374415 : RgM_solve_basecase(GEN a, GEN b)
    2643             : {
    2644     1374415 :   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     1374415 :   set_avma(av);
    2651             : 
    2652     1374415 :   if (lg(a)-1 == 2 && nbrows(a) == 2) {
    2653             :     /* 2x2 matrix, start by inverting a */
    2654      892471 :     GEN u = gcoeff(a,1,1), v = gcoeff(a,1,2);
    2655      892471 :     GEN w = gcoeff(a,2,1), x = gcoeff(a,2,2);
    2656      892471 :     GEN D = gsub(gmul(u,x), gmul(v,w)), ainv;
    2657      892470 :     if (gequal0(D)) return NULL;
    2658      892470 :     ainv = mkmat2(mkcol2(x, gneg(w)), mkcol2(gneg(v), u));
    2659      892470 :     ainv = gmul(ainv, ginv(D));
    2660      892465 :     if (b) ainv = gmul(ainv, b);
    2661      892464 :     return gerepileupto(av, ainv);
    2662             :   }
    2663             : 
    2664      481943 :   if (!init_gauss(a, &b, &aco, &li, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    2665      481943 :   pivot = get_pivot_fun(a, a, &data);
    2666      481944 :   a = RgM_shallowcopy(a);
    2667      481944 :   bco = lg(b)-1;
    2668      481944 :   if(DEBUGLEVEL>4) err_printf("Entering gauss\n");
    2669             : 
    2670      481944 :   p = NULL; /* gcc -Wall */
    2671     1700760 :   for (i=1; i<=aco; i++)
    2672             :   {
    2673             :     /* k is the line where we find the pivot */
    2674     1700758 :     k = pivot(a, data, i, NULL);
    2675     1700760 :     if (k > li) return NULL;
    2676     1700745 :     if (k != i)
    2677             :     { /* exchange the lines s.t. k = i */
    2678     1239552 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    2679      938568 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    2680             :     }
    2681     1700745 :     p = gcoeff(a,i,i);
    2682     1700745 :     if (i == aco) break;
    2683             : 
    2684     3909638 :     for (k=i+1; k<=li; k++)
    2685             :     {
    2686     2690839 :       GEN m = gcoeff(a,k,i);
    2687     2690839 :       if (!gequal0(m))
    2688             :       {
    2689     2042685 :         m = gdiv(m,p);
    2690     8371835 :         for (j=i+1; j<=aco; j++) _submul(gel(a,j),k,i,m);
    2691     6255361 :         for (j=1;   j<=bco; j++) _submul(gel(b,j),k,i,m);
    2692             :       }
    2693             :     }
    2694     1218799 :     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      481930 :   if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
    2702      481930 :   u = cgetg(bco+1,t_MAT);
    2703     1157115 :   for (j=1; j<=bco; j++) gel(u,j) = get_col(a,gel(b,j),p,aco);
    2704      481925 :   return gerepilecopy(av, iscol? gel(u,1): u);
    2705             : }
    2706             : 
    2707             : static GEN
    2708     1182276 : RgM_RgC_solve_fast(GEN x, GEN y)
    2709             : {
    2710             :   GEN p, pol;
    2711             :   long pa;
    2712     1182276 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    2713     1182276 :   switch(t)
    2714             :   {
    2715       14629 :     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     1167367 :     default:       return gen_0;
    2720             :   }
    2721             : }
    2722             : 
    2723             : static GEN
    2724       48657 : RgM_solve_fast(GEN x, GEN y)
    2725             : {
    2726             :   GEN p, pol;
    2727             :   long pa;
    2728       48657 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    2729       48657 :   switch(t)
    2730             :   {
    2731          70 :     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       48412 :     default:       return gen_0;
    2736             :   }
    2737             : }
    2738             : 
    2739             : GEN
    2740     1230933 : RgM_solve(GEN a, GEN b)
    2741             : {
    2742     1230933 :   pari_sp av = avma;
    2743             :   GEN u;
    2744     1230933 :   if (!b) return RgM_inv(a);
    2745     1230933 :   u = typ(b)==t_MAT ? RgM_solve_fast(a, b): RgM_RgC_solve_fast(a, b);
    2746     1230933 :   if (!u) return gc_NULL(av);
    2747     1230835 :   if (u != gen_0) return u;
    2748     1215779 :   return RgM_solve_basecase(a, b);
    2749             : }
    2750             : 
    2751             : GEN
    2752          21 : RgM_div(GEN a, GEN b)
    2753             : {
    2754          21 :   pari_sp av = avma;
    2755          21 :   return gerepilecopy(av, shallowtrans(RgM_solve(shallowtrans(b),shallowtrans(a))));
    2756             : }
    2757             : 
    2758             : GEN
    2759      204628 : RgM_inv(GEN a)
    2760             : {
    2761      204628 :   GEN b = RgM_inv_fast(a);
    2762      204615 :   return b==gen_0? RgM_solve_basecase(a, NULL): b;
    2763             : }
    2764             : 
    2765             : /* assume dim A >= 1, A invertible + upper triangular  */
    2766             : static GEN
    2767     3256687 : RgM_inv_upper_ind(GEN A, long index)
    2768             : {
    2769     3256687 :   long n = lg(A)-1, i = index, j;
    2770     3256687 :   GEN u = zerocol(n);
    2771     3256681 :   gel(u,i) = ginv(gcoeff(A,i,i));
    2772     6522023 :   for (i--; i>0; i--)
    2773             :   {
    2774     3265337 :     pari_sp av = avma;
    2775     3265337 :     GEN m = gneg(gmul(gcoeff(A,i,i+1),gel(u,i+1))); /* j = i+1 */
    2776    13955661 :     for (j=i+2; j<=n; j++) m = gsub(m, gmul(gcoeff(A,i,j),gel(u,j)));
    2777     3265307 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(A,i,i)));
    2778             :   }
    2779     3256686 :   return u;
    2780             : }
    2781             : GEN
    2782     1633559 : RgM_inv_upper(GEN A)
    2783             : {
    2784             :   long i, l;
    2785     1633559 :   GEN B = cgetg_copy(A, &l);
    2786     4890242 :   for (i = 1; i < l; i++) gel(B,i) = RgM_inv_upper_ind(A, i);
    2787     1633555 :   return B;
    2788             : }
    2789             : 
    2790             : static GEN
    2791     4509175 : split_realimag_col(GEN z, long r1, long r2)
    2792             : {
    2793     4509175 :   long i, ru = r1+r2;
    2794     4509175 :   GEN x = cgetg(ru+r2+1,t_COL), y = x + r2;
    2795    12506439 :   for (i=1; i<=r1; i++) {
    2796     7997262 :     GEN a = gel(z,i);
    2797     7997262 :     if (typ(a) == t_COMPLEX) a = gel(a,1); /* paranoia: a should be real */
    2798     7997262 :     gel(x,i) = a;
    2799             :   }
    2800     7198013 :   for (   ; i<=ru; i++) {
    2801     2688836 :     GEN b, a = gel(z,i);
    2802     2688836 :     if (typ(a) == t_COMPLEX) { b = gel(a,2); a = gel(a,1); } else b = gen_0;
    2803     2688836 :     gel(x,i) = a;
    2804     2688836 :     gel(y,i) = b;
    2805             :   }
    2806     4509177 :   return x;
    2807             : }
    2808             : GEN
    2809     2565250 : split_realimag(GEN x, long r1, long r2)
    2810             : {
    2811             :   long i,l; GEN y;
    2812     2565250 :   if (typ(x) == t_COL) return split_realimag_col(x,r1,r2);
    2813     1276775 :   y = cgetg_copy(x, &l);
    2814     4497475 :   for (i=1; i<l; i++) gel(y,i) = split_realimag_col(gel(x,i), r1, r2);
    2815     1276778 :   return y;
    2816             : }
    2817             : 
    2818             : /* assume M = (r1+r2) x (r1+2r2) matrix and y compatible vector or matrix
    2819             :  * r1 first lines of M,y are real. Solve the system obtained by splitting
    2820             :  * real and imaginary parts. */
    2821             : GEN
    2822     1214643 : RgM_solve_realimag(GEN M, GEN y)
    2823             : {
    2824     1214643 :   long l = lg(M), r2 = l - lgcols(M), r1 = l-1 - 2*r2;
    2825     1214643 :   return RgM_solve(split_realimag(M, r1,r2),
    2826             :                    split_realimag(y, r1,r2));
    2827             : }
    2828             : 
    2829             : GEN
    2830         434 : gauss(GEN a, GEN b)
    2831             : {
    2832             :   GEN z;
    2833         434 :   long t = typ(b);
    2834         434 :   if (typ(a)!=t_MAT) pari_err_TYPE("gauss",a);
    2835         434 :   if (t!=t_COL && t!=t_MAT) pari_err_TYPE("gauss",b);
    2836         434 :   z = RgM_solve(a,b);
    2837         434 :   if (!z) pari_err_INV("gauss",a);
    2838         329 :   return z;
    2839             : }
    2840             : 
    2841             : /* #C = n, C[z[i]] = K[i], complete by 0s */
    2842             : static GEN
    2843          14 : RgC_inflate(GEN K, GEN z, long n)
    2844             : {
    2845          14 :   GEN c = zerocol(n);
    2846          14 :   long j, l = lg(K);
    2847          28 :   for (j = 1; j < l; j++) gel(c, z[j]) = gel(K, j);
    2848          14 :   return c;
    2849             : }
    2850             : /* in place: C[i] *= cB / v[i] */
    2851             : static void
    2852        6384 : QC_normalize(GEN C, GEN v, GEN cB)
    2853             : {
    2854        6384 :   long l = lg(C), i;
    2855       47929 :   for (i = 1; i < l; i++)
    2856             :   {
    2857       41545 :     GEN c = cB, k = gel(C,i), d = gel(v,i);
    2858       41545 :     if (d)
    2859             :     {
    2860       24691 :       if (isintzero(d)) { gel(C,i) = gen_0; continue; }
    2861       24691 :       c = div_content(c, d);
    2862             :     }
    2863       41545 :     gel(C,i) = c? gmul(k,c): k;
    2864             :   }
    2865        6384 : }
    2866             : 
    2867             : /* same as above, M rational; if flag = 1, call indexrank and return 1 sol */
    2868             : GEN
    2869        6377 : QM_gauss_i(GEN M, GEN B, long flag)
    2870             : {
    2871        6377 :   pari_sp av = avma;
    2872             :   long i, l, n;
    2873        6377 :   int col = typ(B) == t_COL;
    2874        6377 :   GEN K, cB, N = cgetg_copy(M, &l), v = cgetg(l, t_VEC), z2 = NULL;
    2875             : 
    2876       47950 :   for (i = 1; i < l; i++)
    2877       41573 :     gel(N,i) = Q_primitive_part(gel(M,i), &gel(v,i));
    2878        6377 :   if (flag)
    2879             :   {
    2880         329 :     GEN z = ZM_indexrank(N), z1 = gel(z,1);
    2881         329 :     z2 = gel(z,2);
    2882         329 :     N = shallowmatextract(N, z1, z2);
    2883         329 :     B = col? vecpermute(B,z1): rowpermute(B,z1);
    2884         329 :     if (lg(z2) == l) z2 = NULL; else v = vecpermute(v, z2);
    2885             :   }
    2886        6377 :   B = Q_primitive_part(B, &cB);
    2887        6377 :   K = ZM_gauss(N, B); if (!K) return gc_NULL(av);
    2888        6377 :   n = l - 1;
    2889        6377 :   if (col)
    2890             :   {
    2891        6349 :     QC_normalize(K, v, cB);
    2892        6349 :     if (z2) K = RgC_inflate(K, z2, n);
    2893             :   }
    2894             :   else
    2895             :   {
    2896          28 :     long lK = lg(K);
    2897          63 :     for (i = 1; i < lK; i++)
    2898             :     {
    2899          35 :       QC_normalize(gel(K,i), v, cB);
    2900          35 :       if (z2) gel(K,i) = RgC_inflate(gel(K,i), z2, n);
    2901             :     }
    2902             :   }
    2903        6377 :   return gerepilecopy(av, K);
    2904             : }
    2905             : GEN
    2906        6048 : QM_gauss(GEN M, GEN B) { return QM_gauss_i(M, B, 0); }
    2907             : 
    2908             : static GEN
    2909      785192 : ZM_inv_slice(GEN A, GEN P, GEN *mod)
    2910             : {
    2911      785192 :   pari_sp av = avma;
    2912      785192 :   long i, n = lg(P)-1;
    2913             :   GEN H, T;
    2914      785192 :   if (n == 1)
    2915             :   {
    2916      749836 :     ulong p = uel(P,1);
    2917      749836 :     GEN Hp, a = ZM_to_Flm(A, p);
    2918      749838 :     Hp = Flm_adjoint(a, p);
    2919      749838 :     Hp = gerepileupto(av, Flm_to_ZM(Hp));
    2920      749838 :     *mod = utoipos(p); return Hp;
    2921             :   }
    2922       35356 :   T = ZV_producttree(P);
    2923       35357 :   A = ZM_nv_mod_tree(A, P, T);
    2924       35357 :   H = cgetg(n+1, t_VEC);
    2925      205397 :   for(i=1; i <= n; i++)
    2926      170040 :     gel(H,i) = Flm_adjoint(gel(A, i), uel(P,i));
    2927       35357 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    2928       35357 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    2929             : }
    2930             : 
    2931             : static GEN
    2932      709269 : RgM_true_Hadamard(GEN a)
    2933             : {
    2934      709269 :   pari_sp av = avma;
    2935      709269 :   long n = lg(a)-1, i;
    2936             :   GEN B;
    2937      709269 :   if (n == 0) return gen_1;
    2938      709269 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    2939      709269 :   B = gnorml2(gel(a,1));
    2940     2908928 :   for (i = 2; i <= n; i++) B = gmul(B, gnorml2(gel(a,i)));
    2941      709261 :   return gerepileuptoint(av, ceil_safe(sqrtr(B)));
    2942             : }
    2943             : 
    2944             : GEN
    2945      785192 : ZM_inv_worker(GEN P, GEN A)
    2946             : {
    2947      785192 :   GEN V = cgetg(3, t_VEC);
    2948      785192 :   gel(V,1) = ZM_inv_slice(A, P, &gel(V,2));
    2949      785195 :   return V;
    2950             : }
    2951             : 
    2952             : static GEN
    2953       41244 : ZM_inv0(GEN A, GEN *pden)
    2954             : {
    2955       41244 :   if (pden) *pden = gen_1;
    2956       41244 :   (void)A; return cgetg(1, t_MAT);
    2957             : }
    2958             : static GEN
    2959      643247 : ZM_inv1(GEN A, GEN *pden)
    2960             : {
    2961      643247 :   GEN a = gcoeff(A,1,1);
    2962      643247 :   long s = signe(a);
    2963      643247 :   if (!s) return NULL;
    2964      643247 :   if (pden) *pden = absi(a);
    2965      643246 :   retmkmat(mkcol(s == 1? gen_1: gen_m1));
    2966             : }
    2967             : static GEN
    2968      741634 : ZM_inv2(GEN A, GEN *pden)
    2969             : {
    2970             :   GEN a, b, c, d, D, cA;
    2971             :   long s;
    2972      741634 :   A = Q_primitive_part(A, &cA);
    2973      741634 :   a = gcoeff(A,1,1); b = gcoeff(A,1,2);
    2974      741634 :   c = gcoeff(A,2,1); d = gcoeff(A,2,2);
    2975      741634 :   D = subii(mulii(a,d), mulii(b,c)); /* left on stack */
    2976      741620 :   s = signe(D);
    2977      741620 :   if (!s) return NULL;
    2978      741606 :   if (s < 0) D = negi(D);
    2979      741610 :   if (pden) *pden = mul_denom(D, cA);
    2980      741609 :   if (s > 0)
    2981      700608 :     retmkmat2(mkcol2(icopy(d), negi(c)), mkcol2(negi(b), icopy(a)));
    2982             :   else
    2983       41001 :     retmkmat2(mkcol2(negi(d), icopy(c)), mkcol2(icopy(b), negi(a)));
    2984             : }
    2985             : 
    2986             : /* to be used when denom(M^(-1)) << det(M) and a sharp multiple is
    2987             :  * not available. Return H primitive such that M*H = den*Id */
    2988             : GEN
    2989           0 : ZM_inv_ratlift(GEN M, GEN *pden)
    2990             : {
    2991           0 :   pari_sp av2, av = avma;
    2992             :   GEN Hp, q, H;
    2993             :   ulong p;
    2994           0 :   long m = lg(M)-1;
    2995             :   forprime_t S;
    2996             :   pari_timer ti;
    2997             : 
    2998           0 :   if (m == 0) return ZM_inv0(M,pden);
    2999           0 :   if (m == 1 && nbrows(M)==1) return ZM_inv1(M,pden);
    3000           0 :   if (m == 2 && nbrows(M)==2) return ZM_inv2(M,pden);
    3001             : 
    3002           0 :   if (DEBUGLEVEL>5) timer_start(&ti);
    3003           0 :   init_modular_big(&S);
    3004           0 :   av2 = avma;
    3005           0 :   H = NULL;
    3006           0 :   while ((p = u_forprime_next(&S)))
    3007             :   {
    3008             :     GEN Mp, B, Hr;
    3009           0 :     Mp = ZM_to_Flm(M,p);
    3010           0 :     Hp = Flm_inv_sp(Mp, NULL, p);
    3011           0 :     if (!Hp) continue;
    3012           0 :     if (!H)
    3013             :     {
    3014           0 :       H = ZM_init_CRT(Hp, p);
    3015           0 :       q = utoipos(p);
    3016             :     }
    3017             :     else
    3018           0 :       ZM_incremental_CRT(&H, Hp, &q, p);
    3019           0 :     B = sqrti(shifti(q,-1));
    3020           0 :     Hr = FpM_ratlift(H,q,B,B,NULL);
    3021           0 :     if (DEBUGLEVEL>5)
    3022           0 :       timer_printf(&ti,"ZM_inv mod %lu (ratlift=%ld)", p,!!Hr);
    3023           0 :     if (Hr) {/* DONE ? */
    3024           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    3025           0 :       if (ZM_isscalar(ZM_mul(Hl, M), *pden)) { H = Hl; break; }
    3026             :     }
    3027             : 
    3028           0 :     if (gc_needed(av,2))
    3029             :     {
    3030           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_inv_ratlift");
    3031           0 :       gerepileall(av2, 2, &H, &q);
    3032             :     }
    3033             :   }
    3034           0 :   if (!*pden) *pden = gen_1;
    3035           0 :   return gc_all(av, 2, &H, pden);
    3036             : }
    3037             : 
    3038             : GEN
    3039       73638 : FpM_ratlift_worker(GEN A, GEN mod, GEN B)
    3040             : {
    3041             :   long l, i;
    3042       73638 :   GEN H = cgetg_copy(A, &l);
    3043      154238 :   for (i = 1; i < l; i++)
    3044             :   {
    3045       80614 :      GEN c = FpC_ratlift(gel(A,i), mod, B, B, NULL);
    3046       80600 :      gel(H,i) = c? c: gen_0;
    3047             :   }
    3048       73624 :   return H;
    3049             : }
    3050             : static int
    3051      755015 : can_ratlift(GEN x, GEN mod, GEN B)
    3052             : {
    3053      755015 :   pari_sp av = avma;
    3054             :   GEN a, b;
    3055      755015 :   return gc_bool(av, Fp_ratlift(x, mod, B, B, &a,&b));
    3056             : }
    3057             : static GEN
    3058     2720696 : FpM_ratlift_parallel(GEN A, GEN mod, GEN B)
    3059             : {
    3060     2720696 :   pari_sp av = avma;
    3061             :   GEN worker;
    3062     2720696 :   long i, l = lg(A), m = mt_nbthreads();
    3063     2720694 :   int test = !!B;
    3064             : 
    3065     2720694 :   if (l == 1 || lgcols(A) == 1) return gcopy(A);
    3066     2720693 :   if (!B) B = sqrti(shifti(mod,-1));
    3067     2720651 :   if (m == 1 || l == 2 || lgcols(A) < 10)
    3068             :   {
    3069     2713239 :     A = FpM_ratlift(A, mod, B, B, NULL);
    3070     2713279 :     return A? A: gc_NULL(av);
    3071             :   }
    3072             :   /* test one coefficient first */
    3073        7412 :   if (test && !can_ratlift(gcoeff(A,1,1), mod, B)) return gc_NULL(av);
    3074        7293 :   worker = snm_closure(is_entry("_FpM_ratlift_worker"), mkvec2(mod,B));
    3075        7293 :   A = gen_parapply_slice(worker, A, m);
    3076       79434 :   for (i = 1; i < l; i++) if (typ(gel(A,i)) != t_COL) return gc_NULL(av);
    3077        6242 :   return A;
    3078             : }
    3079             : 
    3080             : static GEN
    3081      747934 : ZM_adj_ratlift(GEN A, GEN H, GEN mod, GEN T)
    3082             : {
    3083      747934 :   pari_sp av = avma;
    3084             :   GEN B, D, g;
    3085      747934 :   D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3086      747930 :   if (T) D = mulii(T, D);
    3087      747930 :   g = gcdii(D, mod);
    3088      747926 :   if (!equali1(g))
    3089             :   {
    3090          14 :     mod = diviiexact(mod, g);
    3091          14 :     H = FpM_red(H, mod);
    3092             :   }
    3093      747926 :   D = Fp_inv(Fp_red(D, mod), mod);
    3094             :   /* test 1 coeff first */
    3095      747923 :   B = sqrti(shifti(mod,-1));
    3096      747916 :   if (!can_ratlift(Fp_mul(D, gcoeff(A,1,1), mod), mod, B)) return gc_NULL(av);
    3097      726909 :   H = FpM_Fp_mul(H, D, mod);
    3098      726900 :   H = FpM_ratlift_parallel(H, mod, B);
    3099      726907 :   return H? H: gc_NULL(av);
    3100             : }
    3101             : 
    3102             : /* if (T) return T A^(-1) in Mn(Q), else B in Mn(Z) such that A B = den*Id */
    3103             : static GEN
    3104     2135398 : ZM_inv_i(GEN A, GEN *pden, GEN T)
    3105             : {
    3106     2135398 :   pari_sp av = avma;
    3107     2135398 :   long m = lg(A)-1, n, k1 = 1, k2;
    3108     2135398 :   GEN H = NULL, D, H1 = NULL, mod1 = NULL, worker;
    3109             :   ulong bnd, mask;
    3110             :   forprime_t S;
    3111             :   pari_timer ti;
    3112             : 
    3113     2135398 :   if (m == 0) return ZM_inv0(A,pden);
    3114     2094154 :   if (pden) *pden = gen_1;
    3115     2094154 :   if (nbrows(A) < m) return NULL;
    3116     2094147 :   if (m == 1 && nbrows(A)==1 && !T) return ZM_inv1(A,pden);
    3117     1450900 :   if (m == 2 && nbrows(A)==2 && !T) return ZM_inv2(A,pden);
    3118             : 
    3119      709266 :   if (DEBUGLEVEL>=5) timer_start(&ti);
    3120      709266 :   init_modular_big(&S);
    3121      709268 :   bnd = expi(RgM_true_Hadamard(A));
    3122      709268 :   worker = snm_closure(is_entry("_ZM_inv_worker"), mkvec(A));
    3123      709270 :   gen_inccrt("ZM_inv_r", worker, NULL, k1, 0, &S, &H1, &mod1, nmV_chinese_center, FpM_center);
    3124      709270 :   n = (bnd+1)/expu(S.p)+1;
    3125      709270 :   if (DEBUGLEVEL>=5) timer_printf(&ti,"inv (%ld/%ld primes)", k1, n);
    3126      709270 :   mask = quadratic_prec_mask(n);
    3127      709270 :   for (k2 = 0;;)
    3128       66668 :   {
    3129             :     GEN Hr;
    3130      775938 :     if (k2 > 0)
    3131             :     {
    3132       59465 :       gen_inccrt("ZM_inv_r", worker, NULL, k2, 0, &S, &H1, &mod1,nmV_chinese_center,FpM_center);
    3133       59465 :       k1 += k2;
    3134       59465 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"CRT (%ld/%ld primes)", k1, n);
    3135             :     }
    3136      775938 :     if (mask == 1) break;
    3137      747934 :     k2 = (mask&1UL) ? k1-1: k1;
    3138      747934 :     mask >>= 1;
    3139             : 
    3140      747934 :     Hr = ZM_adj_ratlift(A, H1, mod1, T);
    3141      747931 :     if (DEBUGLEVEL>=5) timer_printf(&ti,"ratlift (%ld/%ld primes)", k1, n);
    3142      747931 :     if (Hr) {/* DONE ? */
    3143      685120 :       GEN Hl = Q_primpart(Hr), R = ZM_mul(Hl, A), d = gcoeff(R,1,1);
    3144      685119 :       if (gsigne(d) < 0) { d = gneg(d); Hl = ZM_neg(Hl); }
    3145      685120 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"mult (%ld/%ld primes)", k1, n);
    3146      685120 :       if (equali1(d))
    3147             :       {
    3148      592980 :         if (ZM_isidentity(R)) { H = Hl; break; }
    3149             :       }
    3150       92140 :       else if (ZM_isscalar(R, d))
    3151             :       {
    3152       88287 :         if (T) T = gdiv(T,d);
    3153       84863 :         else if (pden) *pden = d;
    3154       88287 :         H = Hl; break;
    3155             :       }
    3156             :     }
    3157             :   }
    3158      709270 :   if (!H)
    3159             :   {
    3160             :     GEN d;
    3161       28004 :     H = H1;
    3162       28004 :     D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3163       28004 :     if (signe(D)==0) pari_err_INV("ZM_inv", A);
    3164       28004 :     if (T) T = gdiv(T, D);
    3165             :     else
    3166             :     {
    3167       27051 :       d = gcdii(Q_content_safe(H), D);
    3168       27051 :       if (signe(D) < 0) d = negi(d);
    3169       27051 :       if (!equali1(d))
    3170             :       {
    3171       15262 :         H = ZM_Z_divexact(H, d);
    3172       15262 :         D = diviiexact(D, d);
    3173             :       }
    3174       27051 :       if (pden) *pden = D;
    3175             :     }
    3176             :   }
    3177      709270 :   if (T && !isint1(T)) H = ZM_Q_mul(H, T);
    3178      709270 :   return gc_all(av, pden? 2: 1, &H, pden);
    3179             : }
    3180             : GEN
    3181     2074643 : ZM_inv(GEN A, GEN *pden) { return ZM_inv_i(A, pden, NULL); }
    3182             : 
    3183             : /* same as above, M rational */
    3184             : GEN
    3185       60755 : QM_inv(GEN M)
    3186             : {
    3187       60755 :   pari_sp av = avma;
    3188             :   GEN den, dM, K;
    3189       60755 :   M = Q_remove_denom(M, &dM);
    3190       60755 :   K = ZM_inv_i(M, &den, dM);
    3191       60755 :   if (!K) return gc_NULL(av);
    3192       60734 :   if (den && !equali1(den)) K = ZM_Q_mul(K, ginv(den));
    3193       60720 :   return gerepileupto(av, K);
    3194             : }
    3195             : 
    3196             : static GEN
    3197      105225 : ZM_ker_filter(GEN A, GEN P)
    3198             : {
    3199      105225 :   long i, j, l = lg(A), n = 1, d = lg(gmael(A,1,1));
    3200      105225 :   GEN B, Q, D = gmael(A,1,2);
    3201      215191 :   for (i=2; i<l; i++)
    3202             :   {
    3203      109966 :     GEN Di = gmael(A,i,2);
    3204      109966 :     long di = lg(gmael(A,i,1));
    3205      109966 :     int c = vecsmall_lexcmp(D, Di);
    3206      109966 :     if (di==d && c==0) n++;
    3207       45588 :     else if (d > di || (di==d && c>0))
    3208       37680 :     { n = 1; d = di; D = Di; }
    3209             :   }
    3210      105225 :   B = cgetg(n+1, t_VEC);
    3211      105225 :   Q = cgetg(n+1, typ(P));
    3212      320416 :   for (i=1, j=1; i<l; i++)
    3213             :   {
    3214      215191 :     if (lg(gmael(A,i,1))==d &&  vecsmall_lexcmp(D, gmael(A,i,2))==0)
    3215             :     {
    3216      169603 :       gel(B,j) = gmael(A,i,1);
    3217      169603 :       Q[j] = P[i];
    3218      169603 :       j++;
    3219             :     }
    3220             :   }
    3221      105225 :   return mkvec3(B,Q,D);
    3222             : }
    3223             : 
    3224             : static GEN
    3225       69559 : ZM_ker_chinese(GEN A, GEN P, GEN *mod)
    3226             : {
    3227       69559 :   GEN BQD = ZM_ker_filter(A, P);
    3228       69559 :   return mkvec2(nmV_chinese_center(gel(BQD,1), gel(BQD,2), mod), gel(BQD,3));
    3229             : }
    3230             : 
    3231             : static GEN
    3232      129591 : ZM_ker_slice(GEN A, GEN P, GEN *mod)
    3233             : {
    3234      129591 :   pari_sp av = avma;
    3235      129591 :   long i, n = lg(P)-1;
    3236             :   GEN BQD, D, H, T, Q;
    3237      129591 :   if (n == 1)
    3238             :   {
    3239       93924 :     ulong p = uel(P,1);
    3240       93924 :     GEN K = Flm_ker_sp(ZM_to_Flm(A, p), p, 2);
    3241       93924 :     *mod = utoipos(p); return mkvec2(Flm_to_ZM(gel(K,1)), gel(K,2));
    3242             :   }
    3243       35667 :   T = ZV_producttree(P);
    3244       35667 :   A = ZM_nv_mod_tree(A, P, T);
    3245       35667 :   H = cgetg(n+1, t_VEC);
    3246      111505 :   for(i=1 ; i <= n; i++)
    3247       75839 :     gel(H,i) = Flm_ker_sp(gel(A, i), P[i], 2);
    3248       35666 :   BQD = ZM_ker_filter(H, P); Q = gel(BQD,2);
    3249       35666 :   if (lg(Q) != lg(P)) T = ZV_producttree(Q);
    3250       35666 :   H = nmV_chinese_center_tree_seq(gel(BQD,1), Q, T, ZV_chinesetree(Q,T));
    3251       35667 :   *mod = gmael(T, lg(T)-1, 1);
    3252       35667 :   D = gel(BQD, 3);
    3253       35667 :   gerepileall(av, 3, &H, &D, mod);
    3254       35667 :   return mkvec2(H,D);
    3255             : }
    3256             : 
    3257             : GEN
    3258      129591 : ZM_ker_worker(GEN P, GEN A)
    3259             : {
    3260      129591 :   GEN V = cgetg(3, t_VEC);
    3261      129591 :   gel(V,1) = ZM_ker_slice(A, P, &gel(V,2));
    3262      129591 :   return V;
    3263             : }
    3264             : 
    3265             : /* assume lg(A) > 1 */
    3266             : static GEN
    3267       62856 : ZM_ker_i(GEN A)
    3268             : {
    3269             :   pari_sp av;
    3270       62856 :   long k, m = lg(A)-1;
    3271       62856 :   GEN HD = NULL, mod = gen_1, worker;
    3272             :   forprime_t S;
    3273             : 
    3274       62856 :   if (m >= 2*nbrows(A))
    3275             :   {
    3276        3059 :     GEN v = ZM_indexrank(A), y = gel(v,2), z = indexcompl(y, m);
    3277             :     GEN B, A1, A1i, d;
    3278        3059 :     A = rowpermute(A, gel(v,1)); /* same kernel */
    3279        3059 :     A1 = vecpermute(A, y); /* maximal rank submatrix */
    3280        3059 :     B = vecpermute(A, z);
    3281        3059 :     A1i = ZM_inv(A1, &d);
    3282        3059 :     if (!d) d = gen_1;
    3283        3059 :     B = vconcat(ZM_mul(ZM_neg(A1i), B), scalarmat_shallow(d, lg(B)-1));
    3284        3059 :     if (!gequal(y, identity_perm(lg(y)-1)))
    3285         692 :       B = rowpermute(B, perm_inv(shallowconcat(y,z)));
    3286        3059 :     return vec_Q_primpart(B);
    3287             :   }
    3288       59797 :   init_modular_big(&S);
    3289       59797 :   worker = snm_closure(is_entry("_ZM_ker_worker"), mkvec(A));
    3290       59797 :   av = avma;
    3291       59797 :   for (k = 1;; k <<= 1)
    3292       65346 :   {
    3293             :     pari_timer ti;
    3294             :     GEN H, Hr;
    3295      125143 :     gen_inccrt_i("ZM_ker", worker, NULL, (k+1)>>1, 0,
    3296             :                  &S, &HD, &mod, ZM_ker_chinese, NULL);
    3297      125143 :     gerepileall(av, 2, &HD, &mod);
    3298      140693 :     H = gel(HD, 1); if (lg(H) == 1) return H;
    3299       80896 :     if (DEBUGLEVEL >= 4) timer_start(&ti);
    3300       80896 :     Hr = FpM_ratlift_parallel(H, mod, NULL);
    3301       80896 :     if (DEBUGLEVEL >= 4) timer_printf(&ti,"ZM_ker: ratlift (%ld)",!!Hr);
    3302       80896 :     if (Hr)
    3303             :     {
    3304             :       GEN MH;
    3305       70146 :       Hr = vec_Q_primpart(Hr);
    3306       70146 :       MH = ZM_mul(A, Hr);
    3307       70146 :       if (DEBUGLEVEL >= 4) timer_printf(&ti,"ZM_ker: QM_mul");
    3308       70146 :       if (ZM_equal0(MH)) return Hr;
    3309             :     }
    3310             :   }
    3311             : }
    3312             : 
    3313             : GEN
    3314       47106 : ZM_ker(GEN M)
    3315             : {
    3316       47106 :   pari_sp av = avma;
    3317       47106 :   long l = lg(M)-1;
    3318       47106 :   if (l==0) return cgetg(1, t_MAT);
    3319       47106 :   if (lgcols(M)==1) return matid(l);
    3320       47106 :   return gerepilecopy(av, ZM_ker_i(M));
    3321             : }
    3322             : 
    3323             : static GEN
    3324     1986203 : ZM_gauss_slice(GEN A, GEN B, GEN P, GEN *mod)
    3325             : {
    3326     1986203 :   pari_sp av = avma;
    3327     1986203 :   long i, n = lg(P)-1;
    3328             :   GEN H, T;
    3329     1986203 :   if (n == 1)
    3330             :   {
    3331     1947180 :     ulong p = uel(P,1);
    3332     1947180 :     GEN Hp = Flm_gauss(ZM_to_Flm(A, p) , ZM_to_Flm(B, p) ,p);
    3333     1947175 :     if (!Hp)  { *mod=gen_1; return zeromat(lg(A)-1,lg(B)-1); }
    3334     1947175 :     Hp = gerepileupto(av, Flm_to_ZM(Hp));
    3335     1947174 :     *mod = utoipos(p); return Hp;
    3336             :   }
    3337       39023 :   T = ZV_producttree(P);
    3338       39023 :   A = ZM_nv_mod_tree(A, P, T);
    3339       39023 :   B = ZM_nv_mod_tree(B, P, T);
    3340       39023 :   H = cgetg(n+1, t_VEC);
    3341      224397 :   for(i=1; i <= n; i++)
    3342             :   {
    3343      185374 :     GEN Hi = Flm_gauss(gel(A, i), gel(B,i), uel(P,i));
    3344      185374 :     gel(H,i) = Hi ? Hi: zero_Flm(lg(A)-1,lg(B)-1);
    3345      185374 :     if (!Hi) uel(P,i)=1;
    3346             :   }
    3347       39023 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    3348       39023 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    3349             : }
    3350             : 
    3351             : GEN
    3352     1986203 : ZM_gauss_worker(GEN P, GEN A, GEN B)
    3353             : {
    3354     1986203 :   GEN V = cgetg(3, t_VEC);
    3355     1986203 :   gel(V,1) = ZM_gauss_slice(A, B, P, &gel(V,2));
    3356     1986197 :   return V;
    3357             : }
    3358             : 
    3359             : /* assume lg(A) > 1 */
    3360             : static GEN
    3361     1736408 : ZM_gauss_i(GEN A, GEN B)
    3362             : {
    3363             :   pari_sp av;
    3364             :   long k, m, ncol;
    3365             :   int iscol;
    3366     1736408 :   GEN y, y1, y2, Hr, H = NULL, mod = gen_1, worker;
    3367             :   forprime_t S;
    3368     1736408 :   if (!init_gauss(A, &B, &m, &ncol, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    3369     1736332 :   init_modular_big(&S);
    3370     1736334 :   y = ZM_indexrank(A); y1 = gel(y,1); y2 = gel(y,2);
    3371     1736341 :   if (lg(y2)-1 != m) return NULL;
    3372     1736320 :   A = rowpermute(A, y1);
    3373     1736316 :   B = rowpermute(B, y1);
    3374             :   /* a is square and invertible */
    3375     1736318 :   ncol = lg(B);
    3376     1736318 :   worker = snm_closure(is_entry("_ZM_gauss_worker"), mkvec2(A,B));
    3377     1736326 :   av = avma;
    3378     1736326 :   for (k = 1;; k <<= 1)
    3379      176580 :   {
    3380             :     pari_timer ti;
    3381     1912906 :     gen_inccrt_i("ZM_gauss", worker, NULL, (k+1)>>1 , m,
    3382             :                  &S, &H, &mod, nmV_chinese_center, FpM_center);
    3383     1912888 :     gerepileall(av, 2, &H, &mod);
    3384     1912904 :     if (DEBUGLEVEL >= 4) timer_start(&ti);
    3385     1912904 :     Hr = FpM_ratlift_parallel(H, mod, NULL);
    3386     1912895 :     if (DEBUGLEVEL >= 4) timer_printf(&ti,"ZM_gauss: ratlift (%ld)",!!Hr);
    3387     1912897 :     if (Hr)
    3388             :     {
    3389             :       GEN MH, c;
    3390     1779837 :       MH = ZM_mul(A, Q_remove_denom(Hr, &c));
    3391     1779821 :       if (DEBUGLEVEL >= 4) timer_printf(&ti,"ZM_ker: QM_mul");
    3392     1779827 :       if (ZM_equal(MH, c ? ZM_Z_mul(B, c): B)) break;
    3393             :     }
    3394             :   }
    3395     1736307 :   return iscol ? gel(Hr, 1): Hr;
    3396             : }
    3397             : 
    3398             : GEN
    3399     1736407 : ZM_gauss(GEN A, GEN B)
    3400             : {
    3401     1736407 :   pari_sp av = avma;
    3402     1736407 :   GEN C = ZM_gauss_i(A,B);
    3403     1736395 :   return C ? gerepilecopy(av, C): NULL;
    3404             : }
    3405             : 
    3406             : GEN
    3407       16569 : QM_ker(GEN M)
    3408             : {
    3409       16569 :   pari_sp av = avma;
    3410       16569 :   long l = lg(M)-1;
    3411       16569 :   if (l==0) return cgetg(1, t_MAT);
    3412       16527 :   if (lgcols(M)==1) return matid(l);
    3413       15666 :   return gerepilecopy(av, ZM_ker_i(row_Q_primpart(M)));
    3414             : }
    3415             : 
    3416             : /* x a ZM. Return a multiple of the determinant of the lattice generated by
    3417             :  * the columns of x. From Algorithm 2.2.6 in GTM138 */
    3418             : GEN
    3419       47549 : detint(GEN A)
    3420             : {
    3421       47549 :   if (typ(A) != t_MAT) pari_err_TYPE("detint",A);
    3422       47549 :   RgM_check_ZM(A, "detint");
    3423       47549 :   return ZM_detmult(A);
    3424             : }
    3425             : GEN
    3426      157266 : ZM_detmult(GEN A)
    3427             : {
    3428      157266 :   pari_sp av1, av = avma;
    3429             :   GEN B, c, v, piv;
    3430      157266 :   long rg, i, j, k, m, n = lg(A) - 1;
    3431             : 
    3432      157266 :   if (!n) return gen_1;
    3433      157266 :   m = nbrows(A);
    3434      157266 :   if (n < m) return gen_0;
    3435      157189 :   c = zero_zv(m);
    3436      157189 :   av1 = avma;
    3437      157189 :   B = zeromatcopy(m,m);
    3438      157190 :   v = cgetg(m+1, t_COL);
    3439      157190 :   piv = gen_1; rg = 0;
    3440      687010 :   for (k=1; k<=n; k++)
    3441             :   {
    3442      686996 :     GEN pivprec = piv;
    3443      686996 :     long t = 0;
    3444     4934038 :     for (i=1; i<=m; i++)
    3445             :     {
    3446     4247044 :       pari_sp av2 = avma;
    3447             :       GEN vi;
    3448     4247044 :       if (c[i]) continue;
    3449             : 
    3450     2467269 :       vi = mulii(piv, gcoeff(A,i,k));
    3451    22519078 :       for (j=1; j<=m; j++)
    3452    20051771 :         if (c[j]) vi = addii(vi, mulii(gcoeff(B,j,i),gcoeff(A,j,k)));
    3453     2467307 :       if (!t && signe(vi)) t = i;
    3454     2467307 :       gel(v,i) = gerepileuptoint(av2, vi);
    3455             :     }
    3456      686994 :     if (!t) continue;
    3457             :     /* at this point c[t] = 0 */
    3458             : 
    3459      686903 :     if (++rg >= m) { /* full rank; mostly done */
    3460      157175 :       GEN det = gel(v,t); /* last on stack */
    3461      157175 :       if (++k > n)
    3462      157036 :         det = absi(det);
    3463             :       else
    3464             :       {
    3465             :         /* improve further; at this point c[i] is set for all i != t */
    3466         139 :         gcoeff(B,t,t) = piv; v = centermod(gel(B,t), det);
    3467         446 :         for ( ; k<=n; k++)
    3468         307 :           det = gcdii(det, ZV_dotproduct(v, gel(A,k)));
    3469             :       }
    3470      157175 :       return gerepileuptoint(av, det);
    3471             :     }
    3472             : 
    3473      529728 :     piv = gel(v,t);
    3474     4089377 :     for (i=1; i<=m; i++)
    3475             :     {
    3476             :       GEN mvi;
    3477     3559648 :       if (c[i] || i == t) continue;
    3478             : 
    3479     1779825 :       gcoeff(B,t,i) = mvi = negi(gel(v,i));
    3480    17581271 :       for (j=1; j<=m; j++)
    3481    15801445 :         if (c[j]) /* implies j != t */
    3482             :         {
    3483     4080605 :           pari_sp av2 = avma;
    3484     4080605 :           GEN z = addii(mulii(gcoeff(B,j,i), piv), mulii(gcoeff(B,j,t), mvi));
    3485     4080605 :           if (rg > 1) z = diviiexact(z, pivprec);
    3486     4080605 :           gcoeff(B,j,i) = gerepileuptoint(av2, z);
    3487             :         }
    3488             :     }
    3489      529729 :     c[t] = k;
    3490      529729 :     if (gc_needed(av,1))
    3491             :     {
    3492           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"detint. k=%ld",k);
    3493           0 :       gerepileall(av1, 2, &piv,&B); v = zerovec(m);
    3494             :     }
    3495             :   }
    3496          14 :   return gc_const(av, gen_0);
    3497             : }
    3498             : 
    3499             : /* Reduce x modulo (invertible) y */
    3500             : GEN
    3501       14678 : closemodinvertible(GEN x, GEN y)
    3502             : {
    3503       14678 :   return gmul(y, ground(RgM_solve(y,x)));
    3504             : }
    3505             : GEN
    3506           7 : reducemodinvertible(GEN x, GEN y)
    3507             : {
    3508           7 :   return gsub(x, closemodinvertible(x,y));
    3509             : }
    3510             : GEN
    3511           0 : reducemodlll(GEN x,GEN y)
    3512             : {
    3513           0 :   return reducemodinvertible(x, ZM_lll(y, 0.75, LLL_INPLACE));
    3514             : }
    3515             : 
    3516             : /*******************************************************************/
    3517             : /*                                                                 */
    3518             : /*                    KERNEL of an m x n matrix                    */
    3519             : /*          return n - rk(x) linearly independent vectors          */
    3520             : /*                                                                 */
    3521             : /*******************************************************************/
    3522             : static GEN
    3523          28 : RgM_deplin_i(GEN x0)
    3524             : {
    3525          28 :   pari_sp av = avma, av2;
    3526          28 :   long i, j, k, nl, nc = lg(x0)-1;
    3527             :   GEN D, x, y, c, l, d, ck;
    3528             : 
    3529          28 :   if (!nc) return NULL;
    3530          28 :   nl = nbrows(x0);
    3531          28 :   c = zero_zv(nl);
    3532          28 :   l = cgetg(nc+1, t_VECSMALL); /* not initialized */
    3533          28 :   av2 = avma;
    3534          28 :   x = RgM_shallowcopy(x0);
    3535          28 :   d = const_vec(nl, gen_1); /* pivot list */
    3536          28 :   ck = NULL; /* gcc -Wall */
    3537          98 :   for (k=1; k<=nc; k++)
    3538             :   {
    3539          91 :     ck = gel(x,k);
    3540         196 :     for (j=1; j<k; j++)
    3541             :     {
    3542         105 :       GEN cj = gel(x,j), piv = gel(d,j), q = gel(ck,l[j]);
    3543         420 :       for (i=1; i<=nl; i++)
    3544         315 :         if (i!=l[j]) gel(ck,i) = gsub(gmul(piv, gel(ck,i)), gmul(q, gel(cj,i)));
    3545             :     }
    3546             : 
    3547          91 :     i = gauss_get_pivot_NZ(x, NULL, k, c);
    3548          91 :     if (i > nl) break;
    3549          70 :     if (gc_needed(av,1))
    3550             :     {
    3551           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"deplin k = %ld/%ld",k,nc);
    3552           0 :       gerepileall(av2, 2, &x, &d);
    3553           0 :       ck = gel(x,k);
    3554             :     }
    3555          70 :     gel(d,k) = gel(ck,i);
    3556          70 :     c[i] = k; l[k] = i; /* pivot d[k] in x[i,k] */
    3557             :   }
    3558          28 :   if (k > nc) return gc_NULL(av);
    3559          21 :   if (k == 1) { set_avma(av); return scalarcol_shallow(gen_1,nc); }
    3560          21 :   y = cgetg(nc+1,t_COL);
    3561          21 :   gel(y,1) = gcopy(gel(ck, l[1]));
    3562          49 :   for (D=gel(d,1),j=2; j<k; j++)
    3563             :   {
    3564          28 :     gel(y,j) = gmul(gel(ck, l[j]), D);
    3565          28 :     D = gmul(D, gel(d,j));
    3566             :   }
    3567          21 :   gel(y,j) = gneg(D);
    3568          21 :   for (j++; j<=nc; j++) gel(y,j) = gen_0;
    3569          21 :   y = primitive_part(y, &c);
    3570          21 :   return c? gerepileupto(av, y): gerepilecopy(av, y);
    3571             : }
    3572             : static GEN
    3573           0 : RgV_deplin(GEN v)
    3574             : {
    3575           0 :   pari_sp av = avma;
    3576           0 :   long n = lg(v)-1;
    3577           0 :   GEN y, p = NULL;
    3578           0 :   if (n <= 1)
    3579             :   {
    3580           0 :     if (n == 1 && gequal0(gel(v,1))) return mkcol(gen_1);
    3581           0 :     return cgetg(1, t_COL);
    3582             :   }
    3583           0 :   if (gequal0(gel(v,1))) return scalarcol_shallow(gen_1, n);
    3584           0 :   v = primpart(mkvec2(gel(v,1),gel(v,2)));
    3585           0 :   if (RgV_is_FpV(v, &p) && p) v = centerlift(v);
    3586           0 :   y = zerocol(n);
    3587           0 :   gel(y,1) = gneg(gel(v,2));
    3588           0 :   gel(y,2) = gcopy(gel(v,1));
    3589           0 :   return gerepileupto(av, y);
    3590             : 
    3591             : }
    3592             : 
    3593             : static GEN
    3594         105 : RgM_deplin_FpM(GEN x, GEN p)
    3595             : {
    3596         105 :   pari_sp av = avma;
    3597             :   ulong pp;
    3598         105 :   x = RgM_Fp_init3(x, p, &pp);
    3599         105 :   switch(pp)
    3600             :   {
    3601          35 :   case 0:
    3602          35 :     x = FpM_ker_gen(x,p,1);
    3603          35 :     if (!x) return gc_NULL(av);
    3604          21 :     x = FpC_center(x,p,shifti(p,-1));
    3605          21 :     break;
    3606          14 :   case 2:
    3607          14 :     x = F2m_ker_sp(x,1);
    3608          14 :     if (!x) return gc_NULL(av);
    3609           7 :     x = F2c_to_ZC(x); break;
    3610           0 :   case 3:
    3611           0 :     x = F3m_ker_sp(x,1);
    3612           0 :     if (!x) return gc_NULL(av);
    3613           0 :     x = F3c_to_ZC(x); break;
    3614          56 :   default:
    3615          56 :     x = Flm_ker_sp(x,pp,1);
    3616          56 :     if (!x) return gc_NULL(av);
    3617          35 :     x = Flv_center(x, pp, pp>>1);
    3618          35 :     x = zc_to_ZC(x);
    3619          35 :     break;
    3620             :   }
    3621          63 :   return gerepileupto(av, x);
    3622             : }
    3623             : 
    3624             : /* FIXME: implement direct modular ZM_deplin ? */
    3625             : static GEN
    3626         119 : QM_deplin(GEN M)
    3627             : {
    3628         119 :   pari_sp av = avma;
    3629         119 :   long l = lg(M)-1;
    3630             :   GEN k;
    3631         119 :   if (l==0) return NULL;
    3632          84 :   if (lgcols(M)==1) return col_ei(l, 1);
    3633          84 :   k = ZM_ker_i(row_Q_primpart(M));
    3634          84 :   if (lg(k)== 1) return gc_NULL(av);
    3635          70 :   return gerepilecopy(av, gel(k,1));
    3636             : }
    3637             : 
    3638             : static GEN
    3639          49 : RgM_deplin_FqM(GEN x, GEN pol, GEN p)
    3640             : {
    3641          49 :   pari_sp av = avma;
    3642          49 :   GEN b, T = RgX_to_FpX(pol, p);
    3643          49 :   if (signe(T) == 0) pari_err_OP("deplin",x,pol);
    3644          49 :   b = FqM_deplin(RgM_to_FqM(x, T, p), T, p);
    3645          49 :   if (!b) return gc_NULL(av);
    3646          35 :   return gerepileupto(av, b);
    3647             : }
    3648             : 
    3649             : #define code(t1,t2) ((t1 << 6) | t2)
    3650             : static GEN
    3651         385 : RgM_deplin_fast(GEN x)
    3652             : {
    3653             :   GEN p, pol;
    3654             :   long pa;
    3655         385 :   long t = RgM_type(x, &p,&pol,&pa);
    3656         385 :   switch(t)
    3657             :   {
    3658         119 :     case t_INT:    /* fall through */
    3659         119 :     case t_FRAC:   return QM_deplin(x);
    3660          84 :     case t_FFELT:  return FFM_deplin(x, pol);
    3661         105 :     case t_INTMOD: return RgM_deplin_FpM(x, p);
    3662          49 :     case code(t_POLMOD, t_INTMOD):
    3663          49 :                    return RgM_deplin_FqM(x, pol, p);
    3664          28 :     default:       return gen_0;
    3665             :   }
    3666             : }
    3667             : #undef code
    3668             : 
    3669             : static GEN
    3670         385 : RgM_deplin(GEN x)
    3671             : {
    3672         385 :   GEN z = RgM_deplin_fast(x);
    3673         385 :   if (z!= gen_0) return z;
    3674          28 :   return RgM_deplin_i(x);
    3675             : }
    3676             : 
    3677             : GEN
    3678         385 : deplin(GEN x)
    3679             : {
    3680         385 :   switch(typ(x))
    3681             :   {
    3682         385 :     case t_MAT:
    3683             :     {
    3684         385 :       GEN z = RgM_deplin(x);
    3685         385 :       if (z) return z;
    3686         147 :       return cgetg(1, t_COL);
    3687             :     }
    3688           0 :     case t_VEC: return RgV_deplin(x);
    3689           0 :     default: pari_err_TYPE("deplin",x);
    3690             :   }
    3691             :   return NULL;/*LCOV_EXCL_LINE*/
    3692             : }
    3693             : 
    3694             : /*******************************************************************/
    3695             : /*                                                                 */
    3696             : /*         GAUSS REDUCTION OF MATRICES  (m lines x n cols)         */
    3697             : /*           (kernel, image, complementary image, rank)            */
    3698             : /*                                                                 */
    3699             : /*******************************************************************/
    3700             : /* return the transform of x under a standard Gauss pivot.
    3701             :  * x0 is a reference point when guessing whether x[i,j] ~ 0
    3702             :  * (iff x[i,j] << x0[i,j])
    3703             :  * Set r = dim ker(x). d[k] contains the index of the first nonzero pivot
    3704             :  * in column k */
    3705             : static GEN
    3706        1271 : gauss_pivot_ker(GEN x, GEN x0, GEN *dd, long *rr)
    3707             : {
    3708             :   GEN c, d, p, data;
    3709             :   pari_sp av;
    3710             :   long i, j, k, r, t, n, m;
    3711             :   pivot_fun pivot;
    3712             : 
    3713        1271 :   n=lg(x)-1; if (!n) { *dd=NULL; *rr=0; return cgetg(1,t_MAT); }
    3714        1271 :   m=nbrows(x); r=0;
    3715        1271 :   pivot = get_pivot_fun(x, x0, &data);
    3716        1271 :   x = RgM_shallowcopy(x);
    3717        1271 :   c = zero_zv(m);
    3718        1271 :   d = cgetg(n+1,t_VECSMALL);
    3719        1271 :   av=avma;
    3720        7475 :   for (k=1; k<=n; k++)
    3721             :   {
    3722        6204 :     j = pivot(x, data, k, c);
    3723        6204 :     if (j > m)
    3724             :     {
    3725        1463 :       r++; d[k]=0;
    3726        6496 :       for(j=1; j<k; j++)
    3727        5033 :         if (d[j]) gcoeff(x,d[j],k) = gclone(gcoeff(x,d[j],k));
    3728             :     }
    3729             :     else
    3730             :     { /* pivot for column k on row j */
    3731        4741 :       c[j]=k; d[k]=j; p = gdiv(gen_m1,gcoeff(x,j,k));
    3732        4741 :       gcoeff(x,j,k) = gen_m1;
    3733             :       /* x[j,] /= - x[j,k] */
    3734       24169 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    3735       42136 :       for (t=1; t<=m; t++)
    3736       37395 :         if (t!=j)
    3737             :         { /* x[t,] -= 1 / x[j,k] x[j,] */
    3738       32654 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    3739       32654 :           if (gequal0(p)) continue;
    3740       86920 :           for (i=k+1; i<=n; i++)
    3741       69463 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i),gmul(p,gcoeff(x,j,i)));
    3742       17457 :           if (gc_needed(av,1)) gerepile_gauss_ker(x,k,t,av);
    3743             :         }
    3744             :     }
    3745             :   }
    3746        1271 :   *dd=d; *rr=r; return x;
    3747             : }
    3748             : 
    3749             : /* r = dim ker(x).
    3750             :  * Returns d:
    3751             :  *   d[k] != 0 contains the index of a nonzero pivot in column k
    3752             :  *   d[k] == 0 if column k is a linear combination of the (k-1) first ones */
    3753             : GEN
    3754      149629 : RgM_pivots(GEN x0, GEN data, long *rr, pivot_fun pivot)
    3755             : {
    3756             :   GEN x, c, d, p;
    3757      149629 :   long i, j, k, r, t, m, n = lg(x0)-1;
    3758             :   pari_sp av;
    3759             : 
    3760      149629 :   if (RgM_is_ZM(x0)) return ZM_pivots(x0, rr);
    3761      137159 :   if (!n) { *rr = 0; return NULL; }
    3762             : 
    3763      137159 :   d = cgetg(n+1, t_VECSMALL);
    3764      137158 :   x = RgM_shallowcopy(x0);
    3765      137159 :   m = nbrows(x); r = 0;
    3766      137159 :   c = zero_zv(m);
    3767      137209 :   av = avma;
    3768     3457308 :   for (k=1; k<=n; k++)
    3769             :   {
    3770     3320149 :     j = pivot(x, data, k, c);
    3771     3320156 :     if (j > m) { r++; d[k] = 0; }
    3772             :     else
    3773             :     {
    3774      270554 :       c[j] = k; d[k] = j; p = gdiv(gen_m1, gcoeff(x,j,k));
    3775     9398562 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    3776             : 
    3777     1051498 :       for (t=1; t<=m; t++)
    3778      781001 :         if (!c[t]) /* no pivot on that line yet */
    3779             :         {
    3780      293829 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    3781    19645572 :           for (i=k+1; i<=n; i++)
    3782    19351750 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i), gmul(p, gcoeff(x,j,i)));
    3783      293822 :           if (gc_needed(av,1)) gerepile_gauss(x,k,t,av,j,c);
    3784             :         }
    3785     9672218 :       for (i=k; i<=n; i++) gcoeff(x,j,i) = gen_0; /* dummy */
    3786             :     }
    3787             :   }
    3788      137159 :   *rr = r; return gc_const((pari_sp)d, d);
    3789             : }
    3790             : 
    3791             : static long
    3792     4228913 : ZM_count_0_cols(GEN M)
    3793             : {
    3794     4228913 :   long i, l = lg(M), n = 0;
    3795    18241291 :   for (i = 1; i < l; i++)
    3796    14012381 :     if (ZV_equal0(gel(M,i))) n++;
    3797     4228910 :   return n;
    3798             : }
    3799             : 
    3800             : static void indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol);
    3801             : /* As RgM_pivots, integer entries. Set *rr = dim Ker M0 */
    3802             : GEN
    3803     4246129 : ZM_pivots(GEN M0, long *rr)
    3804             : {
    3805     4246129 :   GEN d, dbest = NULL;
    3806             :   long m, mm, n, nn, i, imax, rmin, rbest, zc;
    3807     4246129 :   int beenthere = 0;
    3808     4246129 :   pari_sp av, av0 = avma;
    3809             :   forprime_t S;
    3810             : 
    3811     4246129 :   rbest = n = lg(M0)-1;
    3812     4246129 :   if (n == 0) { *rr = 0; return NULL; }
    3813     4228911 :   zc = ZM_count_0_cols(M0);
    3814     4228906 :   if (n == zc) { *rr = zc; return zero_zv(n); }
    3815             : 
    3816     4228780 :   m = nbrows(M0);
    3817     4228779 :   rmin = maxss(zc, n-m);
    3818     4228777 :   init_modular_small(&S);
    3819     4228753 :   if (n <= m) { nn = n; mm = m; } else { nn = m; mm = n; }
    3820     4228753 :   imax = (nn < 16)? 1: (nn < 64)? 2: 3; /* heuristic */
    3821             : 
    3822             :   for(;;)
    3823           0 :   {
    3824             :     GEN row, col, M, KM, IM, RHS, X, cX;
    3825             :     long rk;
    3826     4256580 :     for (av = avma, i = 0;; set_avma(av), i++)
    3827       27828 :     {
    3828     4256580 :       ulong p = u_forprime_next(&S);
    3829             :       long rp;
    3830     4256570 :       if (!p) pari_err_OVERFLOW("ZM_pivots [ran out of primes]");
    3831     4256570 :       d = Flm_pivots(ZM_to_Flm(M0, p), p, &rp, 1);
    3832     4256605 :       if (rp == rmin) { rbest = rp; goto END; } /* maximal rank, return */
    3833       53325 :       if (rp < rbest) { /* save best r so far */
    3834       25520 :         rbest = rp;
    3835       25520 :         guncloneNULL(dbest);
    3836       25520 :         dbest = gclone(d);
    3837       25520 :         if (beenthere) break;
    3838             :       }
    3839       53325 :       if (!beenthere && i >= imax) break;
    3840             :     }
    3841       25497 :     beenthere = 1;
    3842             :     /* Dubious case: there is (probably) a non trivial kernel */
    3843       25497 :     indexrank_all(m,n, rbest, dbest, &row, &col);
    3844       25497 :     M = rowpermute(vecpermute(M0, col), row);
    3845       25497 :     rk = n - rbest; /* (probable) dimension of image */
    3846       25497 :     if (n > m) M = shallowtrans(M);
    3847       25497 :     IM = vecslice(M,1,rk);
    3848       25497 :     KM = vecslice(M,rk+1, nn);
    3849       25497 :     M = rowslice(IM, 1,rk); /* square maximal rank */
    3850       25497 :     X = ZM_gauss(M, rowslice(KM, 1,rk));
    3851       25497 :     RHS = rowslice(KM,rk+1,mm);
    3852       25497 :     M = rowslice(IM,rk+1,mm);
    3853       25496 :     X = Q_remove_denom(X, &cX);
    3854       25496 :     if (cX) RHS = ZM_Z_mul(RHS, cX);
    3855       25496 :     if (ZM_equal(ZM_mul(M, X), RHS)) { d = vecsmall_copy(dbest); goto END; }
    3856           0 :     set_avma(av);
    3857             :   }
    3858     4228777 : END:
    3859     4228777 :   *rr = rbest; guncloneNULL(dbest);
    3860     4228777 :   return gerepileuptoleaf(av0, d);
    3861             : }
    3862             : 
    3863             : /* set *pr = dim Ker x */
    3864             : static GEN
    3865       59071 : gauss_pivot(GEN x, long *pr) {
    3866             :   GEN data;
    3867       59071 :   pivot_fun pivot = get_pivot_fun(x, x, &data);
    3868       59072 :   return RgM_pivots(x, data, pr, pivot);
    3869             : }
    3870             : 
    3871             : /* compute ker(x), x0 is a reference point when guessing whether x[i,j] ~ 0
    3872             :  * (iff x[i,j] << x0[i,j]) */
    3873             : static GEN
    3874        1271 : ker_aux(GEN x, GEN x0)
    3875             : {
    3876        1271 :   pari_sp av = avma;
    3877             :   GEN d,y;
    3878             :   long i,j,k,r,n;
    3879             : 
    3880        1271 :   x = gauss_pivot_ker(x,x0,&d,&r);
    3881        1271 :   if (!r) { set_avma(av); return cgetg(1,t_MAT); }
    3882        1211 :   n = lg(x)-1; y=cgetg(r+1,t_MAT);
    3883        2674 :   for (j=k=1; j<=r; j++,k++)
    3884             :   {
    3885        1463 :     GEN p = cgetg(n+1,t_COL);
    3886             : 
    3887        5586 :     gel(y,j) = p; while (d[k]) k++;
    3888        6496 :     for (i=1; i<k; i++)
    3889        5033 :       if (d[i])
    3890             :       {
    3891        4641 :         GEN p1=gcoeff(x,d[i],k);
    3892        4641 :         gel(p,i) = gcopy(p1); gunclone(p1);
    3893             :       }
    3894             :       else
    3895         392 :         gel(p,i) = gen_0;
    3896        2541 :     gel(p,k) = gen_1; for (i=k+1; i<=n; i++) gel(p,i) = gen_0;
    3897             :   }
    3898        1211 :   return gerepileupto(av,y);
    3899             : }
    3900             : 
    3901             : static GEN
    3902          84 : RgM_ker_FpM(GEN x, GEN p)
    3903             : {
    3904          84 :   pari_sp av = avma;
    3905             :   ulong pp;
    3906          84 :   x = RgM_Fp_init3(x, p, &pp);
    3907          84 :   switch(pp)
    3908             :   {
    3909          35 :     case 0: x = FpM_to_mod(FpM_ker_gen(x,p,0),p); break;
    3910           7 :     case 2: x = F2m_to_mod(F2m_ker_sp(x,0)); break;
    3911           7 :     case 3: x = F3m_to_mod(F3m_ker_sp(x,0)); break;
    3912          35 :     default:x = Flm_to_mod(Flm_ker_sp(x,pp,0), pp); break;
    3913             :   }
    3914          84 :   return gerepileupto(av, x);
    3915             : }
    3916             : 
    3917             : static GEN
    3918          91 : RgM_ker_FqM(GEN x, GEN pol, GEN p)
    3919             : {
    3920          91 :   pari_sp av = avma;
    3921          91 :   GEN b, T = RgX_to_FpX(pol, p);
    3922          91 :   if (signe(T) == 0) pari_err_OP("ker",x,pol);
    3923          84 :   b = FqM_ker(RgM_to_FqM(x, T, p), T, p);
    3924          84 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    3925             : }
    3926             : 
    3927             : #define code(t1,t2) ((t1 << 6) | t2)
    3928             : static GEN
    3929        8603 : RgM_ker_fast(GEN x)
    3930             : {
    3931             :   GEN p, pol;
    3932             :   long pa;
    3933        8603 :   long t = RgM_type(x, &p,&pol,&pa);
    3934        8603 :   switch(t)
    3935             :   {
    3936        7469 :     case t_INT:    /* fall through */
    3937        7469 :     case t_FRAC:   return QM_ker(x);
    3938          77 :     case t_FFELT:  return FFM_ker(x, pol);
    3939          84 :     case t_INTMOD: return RgM_ker_FpM(x, p);
    3940          91 :     case code(t_POLMOD, t_INTMOD):
    3941          91 :                    return RgM_ker_FqM(x, pol, p);
    3942         882 :     default:       return NULL;
    3943             :   }
    3944             : }
    3945             : #undef code
    3946             : 
    3947             : GEN
    3948        8603 : ker(GEN x)
    3949             : {
    3950        8603 :   GEN b = RgM_ker_fast(x);
    3951        8596 :   if (b) return b;
    3952         882 :   return ker_aux(x,x);
    3953             : }
    3954             : 
    3955             : GEN
    3956       46221 : matker0(GEN x,long flag)
    3957             : {
    3958       46221 :   if (typ(x)!=t_MAT) pari_err_TYPE("matker",x);
    3959       46221 :   if (!flag) return ker(x);
    3960       45934 :   RgM_check_ZM(x, "matker");
    3961       45934 :   return ZM_ker(x);
    3962             : }
    3963             : 
    3964             : static GEN
    3965          63 : RgM_image_FpM(GEN x, GEN p)
    3966             : {
    3967          63 :   pari_sp av = avma;
    3968             :   ulong pp;
    3969          63 :   x = RgM_Fp_init(x, p, &pp);
    3970          63 :   switch(pp)
    3971             :   {
    3972          28 :     case 0: x = FpM_to_mod(FpM_image(x,p),p); break;
    3973           7 :     case 2: x = F2m_to_mod(F2m_image(x)); break;
    3974          28 :     default:x = Flm_to_mod(Flm_image(x,pp), pp); break;
    3975             :   }
    3976          63 :   return gerepileupto(av, x);
    3977             : }
    3978             : 
    3979             : static GEN
    3980          35 : RgM_image_FqM(GEN x, GEN pol, GEN p)
    3981             : {
    3982          35 :   pari_sp av = avma;
    3983          35 :   GEN b, T = RgX_to_FpX(pol, p);
    3984          35 :   if (signe(T) == 0) pari_err_OP("image",x,pol);
    3985          28 :   b = FqM_image(RgM_to_FqM(x, T, p), T, p);
    3986          28 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    3987             : }
    3988             : 
    3989             : GEN
    3990        5971 : QM_image_shallow(GEN A)
    3991             : {
    3992        5971 :   A = vec_Q_primpart(A);
    3993        5971 :   return vecpermute(A, ZM_indeximage(A));
    3994             : }
    3995             : GEN
    3996        5201 : QM_image(GEN A)
    3997             : {
    3998        5201 :   pari_sp av = avma;
    3999        5201 :   return gerepilecopy(av, QM_image_shallow(A));
    4000             : }
    4001             : 
    4002             : #define code(t1,t2) ((t1 << 6) | t2)
    4003             : static GEN
    4004        5362 : RgM_image_fast(GEN x)
    4005             : {
    4006             :   GEN p, pol;
    4007             :   long pa;
    4008        5362 :   long t = RgM_type(x, &p,&pol,&pa);
    4009        5362 :   switch(t)
    4010             :   {
    4011        5201 :     case t_INT:    /* fall through */
    4012        5201 :     case t_FRAC:   return QM_image(x);
    4013          49 :     case t_FFELT:  return FFM_image(x, pol);
    4014          63 :     case t_INTMOD: return RgM_image_FpM(x, p);
    4015          35 :     case code(t_POLMOD, t_INTMOD):
    4016          35 :                    return RgM_image_FqM(x, pol, p);
    4017          14 :     default:       return NULL;
    4018             :   }
    4019             : }
    4020             : #undef code
    4021             : 
    4022             : GEN
    4023        5362 : image(GEN x)
    4024             : {
    4025             :   GEN d, M;
    4026             :   long r;
    4027             : 
    4028        5362 :   if (typ(x)!=t_MAT) pari_err_TYPE("matimage",x);
    4029        5362 :   M = RgM_image_fast(x);
    4030        5355 :   if (M) return M;
    4031          14 :   d = gauss_pivot(x,&r); /* d left on stack for efficiency */
    4032          14 :   return image_from_pivot(x,d,r);
    4033             : }
    4034             : 
    4035             : static GEN
    4036          84 : imagecompl_aux(GEN x, GEN(*PIVOT)(GEN,long*))
    4037             : {
    4038          84 :   pari_sp av = avma;
    4039             :   GEN d,y;
    4040             :   long j,i,r;
    4041             : 
    4042          84 :   if (typ(x)!=t_MAT) pari_err_TYPE("imagecompl",x);
    4043          84 :   (void)new_chunk(lg(x) * 4 + 1); /* HACK */
    4044          84 :   d = PIVOT(x,&r); /* if (!d) then r = 0 */
    4045          84 :   set_avma(av); y = cgetg(r+1,t_VECSMALL);
    4046         126 :   for (i=j=1; j<=r; i++)
    4047          42 :     if (!d[i]) y[j++] = i;
    4048          84 :   return y;
    4049             : }
    4050             : GEN
    4051          84 : imagecompl(GEN x) { return imagecompl_aux(x, &gauss_pivot); }
    4052             : GEN
    4053           0 : ZM_imagecompl(GEN x) { return imagecompl_aux(x, &ZM_pivots); }
    4054             : 
    4055             : static GEN
    4056          28 : RgM_RgC_invimage_FpC(GEN A, GEN y, GEN p)
    4057             : {
    4058          28 :   pari_sp av = avma;
    4059             :   ulong pp;
    4060             :   GEN x;
    4061          28 :   A = RgM_Fp_init(A,p,&pp);
    4062          28 :   switch(pp)
    4063             :   {
    4064           7 :   case 0:
    4065           7 :     y = RgC_to_FpC(y,p);
    4066           7 :     x = FpM_FpC_invimage(A, y, p);
    4067           7 :     return x ? gerepileupto(av, FpC_to_mod(x,p)): NULL;
    4068           7 :   case 2:
    4069           7 :     y = RgV_to_F2v(y);
    4070           7 :     x = F2m_F2c_invimage(A, y);
    4071           7 :     return x ? gerepileupto(av, F2c_to_mod(x)): NULL;
    4072          14 :   default:
    4073          14 :     y = RgV_to_Flv(y,pp);
    4074          14 :     x = Flm_Flc_invimage(A, y, pp);
    4075          14 :     return x ? gerepileupto(av, Flc_to_mod(x,pp)): NULL;
    4076             :   }
    4077             : }
    4078             : 
    4079             : static GEN
    4080        2170 : RgM_RgC_invimage_fast(GEN x, GEN y)
    4081             : {
    4082             :   GEN p, pol;
    4083             :   long pa;
    4084        2170 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    4085        2170 :   switch(t)
    4086             :   {
    4087          28 :     case t_INTMOD: return RgM_RgC_invimage_FpC(x, y, p);
    4088          63 :     case t_FFELT:  return FFM_FFC_invimage(x, y, pol);
    4089        2079 :     default:       return gen_0;
    4090             :   }
    4091             : }
    4092             : 
    4093             : GEN
    4094        2275 : RgM_RgC_invimage(GEN A, GEN y)
    4095             : {
    4096        2275 :   pari_sp av = avma;
    4097        2275 :   long i, l = lg(A);
    4098             :   GEN M, x, t;
    4099        2275 :   if (l==1) return NULL;
    4100        2170 :   if (lg(y) != lgcols(A)) pari_err_DIM("inverseimage");
    4101        2170 :   M = RgM_RgC_invimage_fast(A, y);
    4102        2170 :   if (!M) return gc_NULL(av);
    4103        2149 :   if (M != gen_0) return M;
    4104        2079 :   M = ker(shallowconcat(A, y));
    4105        2079 :   i = lg(M)-1;
    4106        2079 :   if (!i) return gc_NULL(av);
    4107             : 
    4108        1820 :   x = gel(M,i); t = gel(x,l);
    4109        1820 :   if (gequal0(t)) return gc_NULL(av);
    4110             : 
    4111        1785 :   t = gneg_i(t); setlg(x,l);
    4112        1785 :   return gerepileupto(av, RgC_Rg_div(x, t));
    4113             : }
    4114             : 
    4115             : /* Return X such that m X = v (t_COL or t_MAT), resp. an empty t_COL / t_MAT
    4116             :  * if no solution exist */
    4117             : GEN
    4118        2436 : inverseimage(GEN m, GEN v)
    4119             : {
    4120             :   GEN y;
    4121        2436 :   if (typ(m)!=t_MAT) pari_err_TYPE("inverseimage",m);
    4122        2436 :   switch(typ(v))
    4123             :   {
    4124        2198 :     case t_COL:
    4125        2198 :       y = RgM_RgC_invimage(m,v);
    4126        2198 :       return y? y: cgetg(1,t_COL);
    4127         238 :     case t_MAT:
    4128         238 :       y = RgM_invimage(m, v);
    4129         238 :       return y? y: cgetg(1,t_MAT);
    4130             :   }
    4131           0 :   pari_err_TYPE("inverseimage",v);
    4132             :   return NULL;/*LCOV_EXCL_LINE*/
    4133             : }
    4134             : 
    4135             : static GEN
    4136          84 : RgM_invimage_FpM(GEN A, GEN B, GEN p)
    4137             : {
    4138          84 :   pari_sp av = avma;
    4139             :   ulong pp;
    4140             :   GEN x;
    4141          84 :   A = RgM_Fp_init(A,p,&pp);
    4142          84 :   switch(pp)
    4143             :   {
    4144          35 :   case 0:
    4145          35 :     B = RgM_to_FpM(B,p);
    4146          35 :     x = FpM_invimage_gen(A, B, p);
    4147          35 :     return x ? gerepileupto(av, FpM_to_mod(x, p)): x;
    4148           7 :   case 2:
    4149           7 :     B = RgM_to_F2m(B);
    4150           7 :     x = F2m_invimage_i(A, B);
    4151           7 :     return x ? gerepileupto(av, F2m_to_mod(x)): x;
    4152          42 :   default:
    4153          42 :     B = RgM_to_Flm(B,pp);
    4154          42 :     x = Flm_invimage_i(A, B, pp);
    4155          42 :     return x ? gerepileupto(av, Flm_to_mod(x, pp)): x;
    4156             :   }
    4157             : }
    4158             : 
    4159             : static GEN
    4160         364 : RgM_invimage_fast(GEN x, GEN y)
    4161             : {
    4162             :   GEN p, pol;
    4163             :   long pa;
    4164         364 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    4165         364 :   switch(t)
    4166             :   {
    4167          84 :     case t_INTMOD: return RgM_invimage_FpM(x, y, p);
    4168         105 :     case t_FFELT:  return FFM_invimage(x, y, pol);
    4169         175 :     default:       return gen_0;
    4170             :   }
    4171             : }
    4172             : 
    4173             : /* find Z such that A Z = B. Return NULL if no solution */
    4174             : GEN
    4175         364 : RgM_invimage(GEN A, GEN B)
    4176             : {
    4177         364 :   pari_sp av = avma;
    4178             :   GEN d, x, X, Y;
    4179         364 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4180         364 :   X = RgM_invimage_fast(A, B);
    4181         364 :   if (!X) return gc_NULL(av);
    4182         252 :   if (X != gen_0) return X;
    4183         175 :   x = ker(shallowconcat(RgM_neg(A), B));
    4184             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4185             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4186             :    * Y has at least nB columns and full rank */
    4187         175 :   nY = lg(x)-1;
    4188         175 :   if (nY < nB) return gc_NULL(av);
    4189         161 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    4190         161 :   d = cgetg(nB+1, t_VECSMALL);
    4191         721 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4192             :   {
    4193         805 :     for (; j>=1; j--)
    4194         756 :       if (!gequal0(gcoeff(Y,i,j))) { d[i] = j; break; }
    4195         609 :     if (!j) return gc_NULL(av);
    4196             :   }
    4197             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    4198         112 :   Y = vecpermute(Y, d);
    4199         112 :   x = vecpermute(x, d);
    4200         112 :   X = rowslice(x, 1, nA);
    4201         112 :   return gerepileupto(av, RgM_mul(X, RgM_inv_upper(Y)));
    4202             : }
    4203             : 
    4204             : static GEN
    4205          70 : RgM_suppl_FpM(GEN x, GEN p)
    4206             : {
    4207          70 :   pari_sp av = avma;
    4208             :   ulong pp;
    4209          70 :   x = RgM_Fp_init(x, p, &pp);
    4210          70 :   switch(pp)
    4211             :   {
    4212          21 :   case 0: x = FpM_to_mod(FpM_suppl(x,p), p); break;
    4213          14 :   case 2: x = F2m_to_mod(F2m_suppl(x)); break;
    4214          35 :   default:x = Flm_to_mod(Flm_suppl(x,pp), pp); break;
    4215             :   }
    4216          70 :   return gerepileupto(av, x);
    4217             : }
    4218             : 
    4219             : static GEN
    4220         175 : RgM_suppl_fast(GEN x)
    4221             : {
    4222             :   GEN p, pol;
    4223             :   long pa;
    4224         175 :   long t = RgM_type(x,&p,&pol,&pa);
    4225         175 :   switch(t)
    4226             :   {
    4227          70 :     case t_INTMOD: return RgM_suppl_FpM(x, p);
    4228          35 :     case t_FFELT:  return FFM_suppl(x, pol);
    4229          70 :     default:       return NULL;
    4230             :   }
    4231             : }
    4232             : 
    4233             : /* x is an n x k matrix, rank(x) = k <= n. Return an invertible n x n matrix
    4234             :  * whose first k columns are given by x. If rank(x) < k, undefined result. */
    4235             : GEN
    4236         175 : suppl(GEN x)
    4237             : {
    4238         175 :   pari_sp av = avma;
    4239             :   GEN d, M;
    4240             :   long r;
    4241         175 :   if (typ(x)!=t_MAT) pari_err_TYPE("suppl",x);
    4242         175 :   M = RgM_suppl_fast(x);
    4243         175 :   if (M) return M;
    4244          70 :   init_suppl(x);
    4245          70 :   d = gauss_pivot(x,&r);
    4246          70 :   set_avma(av); return get_suppl(x,d,nbrows(x),r,&col_ei);
    4247             : }
    4248             : 
    4249             : GEN
    4250           7 : image2(GEN x)
    4251             : {
    4252           7 :   pari_sp av = avma;
    4253             :   long k, n, i;
    4254             :   GEN A, B;
    4255             : 
    4256           7 :   if (typ(x)!=t_MAT) pari_err_TYPE("image2",x);
    4257           7 :   if (lg(x) == 1) return cgetg(1,t_MAT);
    4258           7 :   A = ker(x); k = lg(A)-1;
    4259           7 :   if (!k) { set_avma(av); return gcopy(x); }
    4260           7 :   A = suppl(A); n = lg(A)-1;
    4261           7 :   B = cgetg(n-k+1, t_MAT);
    4262          21 :   for (i = k+1; i <= n; i++) gel(B,i-k) = RgM_RgC_mul(x, gel(A,i));
    4263           7 :   return gerepileupto(av, B);
    4264             : }
    4265             : 
    4266             : GEN
    4267         210 : matimage0(GEN x,long flag)
    4268             : {
    4269         210 :   switch(flag)
    4270             :   {
    4271         203 :     case 0: return image(x);
    4272           7 :     case 1: return image2(x);
    4273           0 :     default: pari_err_FLAG("matimage");
    4274             :   }
    4275             :   return NULL; /* LCOV_EXCL_LINE */
    4276             : }
    4277             : 
    4278             : static long
    4279         126 : RgM_rank_FpM(GEN x, GEN p)
    4280             : {
    4281         126 :   pari_sp av = avma;
    4282             :   ulong pp;
    4283             :   long r;
    4284         126 :   x = RgM_Fp_init(x,p,&pp);
    4285         126 :   switch(pp)
    4286             :   {
    4287          28 :   case 0: r = FpM_rank(x,p); break;
    4288          63 :   case 2: r = F2m_rank(x); break;
    4289          35 :   default:r = Flm_rank(x,pp); break;
    4290             :   }
    4291         126 :   return gc_long(av, r);
    4292             : }
    4293             : 
    4294             : static long
    4295          49 : RgM_rank_FqM(GEN x, GEN pol, GEN p)
    4296             : {
    4297          49 :   pari_sp av = avma;
    4298             :   long r;
    4299          49 :   GEN T = RgX_to_FpX(pol, p);
    4300          49 :   if (signe(T) == 0) pari_err_OP("rank",x,pol);
    4301          42 :   r = FqM_rank(RgM_to_FqM(x, T, p), T, p);
    4302          42 :   return gc_long(av,r);
    4303             : }
    4304             : 
    4305             : #define code(t1,t2) ((t1 << 6) | t2)
    4306             : static long
    4307         315 : RgM_rank_fast(GEN x)
    4308             : {
    4309             :   GEN p, pol;
    4310             :   long pa;
    4311         315 :   long t = RgM_type(x,&p,&pol,&pa);
    4312         315 :   switch(t)
    4313             :   {
    4314          42 :     case t_INT:    return ZM_rank(x);
    4315          21 :     case t_FRAC:   return QM_rank(x);
    4316         126 :     case t_INTMOD: return RgM_rank_FpM(x, p);
    4317          70 :     case t_FFELT:  return FFM_rank(x, pol);
    4318          49 :     case code(t_POLMOD, t_INTMOD):
    4319          49 :                    return RgM_rank_FqM(x, pol, p);
    4320           7 :     default:       return -1;
    4321             :   }
    4322             : }
    4323             : #undef code
    4324             : 
    4325             : long
    4326         315 : rank(GEN x)
    4327             : {
    4328         315 :   pari_sp av = avma;
    4329             :   long r;
    4330             : 
    4331         315 :   if (typ(x)!=t_MAT) pari_err_TYPE("rank",x);
    4332         315 :   r = RgM_rank_fast(x);
    4333         308 :   if (r >= 0) return r;
    4334           7 :   (void)gauss_pivot(x, &r);
    4335           7 :   return gc_long(av, lg(x)-1 - r);
    4336             : }
    4337             : 
    4338             : /* d a t_VECSMALL of integers in 1..n. Return the vector of the d[i]
    4339             :  * followed by the missing indices */
    4340             : static GEN
    4341       50994 : perm_complete(GEN d, long n)
    4342             : {
    4343       50994 :   GEN y = cgetg(n+1, t_VECSMALL);
    4344       50994 :   long i, j = 1, k = n, l = lg(d);
    4345       50994 :   pari_sp av = avma;
    4346       50994 :   char *T = stack_calloc(n+1);
    4347      242202 :   for (i = 1; i < l; i++) T[d[i]] = 1;
    4348      518098 :   for (i = 1; i <= n; i++)
    4349      467104 :     if (T[i]) y[j++] = i; else y[k--] = i;
    4350       50994 :   return gc_const(av, y);
    4351             : }
    4352             : 
    4353             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
    4354             : static GEN
    4355        5971 : indeximage0(long n, long r, GEN d)
    4356             : {
    4357             :   long i, j;
    4358             :   GEN v;
    4359             : 
    4360        5971 :   r = n - r; /* now r = dim Im(x) */
    4361        5971 :   v = cgetg(r+1,t_VECSMALL);
    4362       32837 :   if (d) for (i=j=1; j<=n; j++)
    4363       26866 :     if (d[j]) v[i++] = j;
    4364        5971 :   return v;
    4365             : }
    4366             : /* x an m x n t_MAT, n > 0, r = dim Ker(x), d from gauss_pivot */
    4367             : static void
    4368       25497 : indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol)
    4369             : {
    4370       25497 :   GEN IR = indexrank0(n, r, d);
    4371       25497 :   *prow = perm_complete(gel(IR,1), m);
    4372       25497 :   *pcol = perm_complete(gel(IR,2), n);
    4373       25497 : }
    4374             : 
    4375             : static GEN
    4376          28 : RgM_indexrank_FpM(GEN x, GEN p)
    4377             : {
    4378          28 :   pari_sp av = avma;
    4379             :   ulong pp;
    4380             :   GEN r;
    4381          28 :   x = RgM_Fp_init(x,p,&pp);
    4382          28 :   switch(pp)
    4383             :   {
    4384           7 :   case 0:  r = FpM_indexrank(x,p); break;
    4385           7 :   case 2:  r = F2m_indexrank(x); break;
    4386          14 :   default: r = Flm_indexrank(x,pp); break;
    4387             :   }
    4388          28 :   return gerepileupto(av, r);
    4389             : }
    4390             : 
    4391             : static GEN
    4392           0 : RgM_indexrank_FqM(GEN x, GEN pol, GEN p)
    4393             : {
    4394           0 :   pari_sp av = avma;
    4395           0 :   GEN r, T = RgX_to_FpX(pol, p);
    4396           0 :   if (signe(T) == 0) pari_err_OP("indexrank",x,pol);
    4397           0 :   r = FqM_indexrank(RgM_to_FqM(x, T, p), T, p);
    4398           0 :   return gerepileupto(av, r);
    4399             : }
    4400             : 
    4401             : #define code(t1,t2) ((t1 << 6) | t2)
    4402             : static GEN
    4403       60486 : RgM_indexrank_fast(GEN x)
    4404             : {
    4405             :   GEN p, pol;
    4406             :   long pa;
    4407       60486 :   long t = RgM_type(x,&p,&pol,&pa);
    4408       60485 :   switch(t)
    4409             :   {
    4410         392 :     case t_INT:    return ZM_indexrank(x);
    4411        1148 :     case t_FRAC:   return QM_indexrank(x);
    4412          28 :     case t_INTMOD: return RgM_indexrank_FpM(x, p);
    4413          21 :     case t_FFELT:  return FFM_indexrank(x, pol);
    4414           0 :     case code(t_POLMOD, t_INTMOD):
    4415           0 :                    return RgM_indexrank_FqM(x, pol, p);
    4416       58896 :     default:       return NULL;
    4417             :   }
    4418             : }
    4419             : #undef code
    4420             : 
    4421             : GEN
    4422       60486 : indexrank(GEN x)
    4423             : {
    4424             :   pari_sp av;
    4425             :   long r;
    4426             :   GEN d;
    4427       60486 :   if (typ(x)!=t_MAT) pari_err_TYPE("indexrank",x);
    4428       60486 :   d = RgM_indexrank_fast(x);
    4429       60485 :   if (d) return d;
    4430       58896 :   av = avma;
    4431       58896 :   init_indexrank(x);
    4432       58896 :   d = gauss_pivot(x, &r);
    4433       58897 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    4434             : }
    4435             : 
    4436             : GEN
    4437        5971 : ZM_indeximage(GEN x) {
    4438        5971 :   pari_sp av = avma;
    4439             :   long r;
    4440             :   GEN d;
    4441        5971 :   init_indexrank(x);
    4442        5971 :   d = ZM_pivots(x,&r);
    4443        5971 :   set_avma(av); return indeximage0(lg(x)-1, r, d);
    4444             : }
    4445             : long
    4446     2241944 : ZM_rank(GEN x) {
    4447     2241944 :   pari_sp av = avma;
    4448             :   long r;
    4449     2241944 :   (void)ZM_pivots(x,&r);
    4450     2241930 :   return gc_long(av, lg(x)-1-r);
    4451             : }
    4452             : GEN
    4453     1762329 : ZM_indexrank(GEN x) {
    4454     1762329 :   pari_sp av = avma;
    4455             :   long r;
    4456             :   GEN d;
    4457     1762329 :   init_indexrank(x);
    4458     1762329 :   d = ZM_pivots(x,&r);
    4459     1762334 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    4460             : }
    4461             : 
    4462             : long
    4463          21 : QM_rank(GEN x)
    4464             : {
    4465          21 :   pari_sp av = avma;
    4466          21 :   long r = ZM_rank(Q_primpart(x));
    4467          21 :   set_avma(av);
    4468          21 :   return r;
    4469             : }
    4470             : 
    4471             : GEN
    4472        1148 : QM_indexrank(GEN x)
    4473             : {
    4474        1148 :   pari_sp av = avma;
    4475        1148 :   GEN r = ZM_indexrank(Q_primpart(x));
    4476        1148 :   return gerepileupto(av, r);
    4477             : }
    4478             : 
    4479             : /*******************************************************************/
    4480             : /*                                                                 */
    4481             : /*                             ZabM                                */
    4482             : /*                                                                 */
    4483             : /*******************************************************************/
    4484             : 
    4485             : static GEN
    4486        1276 : FpXM_ratlift(GEN a, GEN q)
    4487             : {
    4488             :   GEN B, y;
    4489        1276 :   long i, j, l = lg(a), n;
    4490        1276 :   B = sqrti(shifti(q,-1));
    4491        1276 :   y = cgetg(l, t_MAT);
    4492        1276 :   if (l==1) return y;
    4493        1276 :   n = lgcols(a);
    4494        3059 :   for (i=1; i<l; i++)
    4495             :   {
    4496        2404 :     GEN yi = cgetg(n, t_COL);
    4497       32311 :     for (j=1; j<n; j++)
    4498             :     {
    4499       30528 :       GEN v = FpX_ratlift(gmael(a,i,j), q, B, B, NULL);
    4500       30528 :       if (!v) return NULL;
    4501       29907 :       gel(yi, j) = RgX_renormalize(v);
    4502             :     }
    4503        1783 :     gel(y,i) = yi;
    4504             :   }
    4505         655 :   return y;
    4506             : }
    4507             : 
    4508             : static GEN
    4509        4477 : FlmV_recover_pre(GEN a, GEN M, ulong p, ulong pi, long sv)
    4510             : {
    4511        4477 :   GEN a1 = gel(a,1);
    4512        4477 :   long i, j, k, l = lg(a1), n, lM = lg(M);
    4513        4477 :   GEN v = cgetg(lM, t_VECSMALL);
    4514        4477 :   GEN y = cgetg(l, t_MAT);
    4515        4477 :   if (l==1) return y;
    4516        4477 :   n = lgcols(a1);
    4517       22466 :   for (i=1; i<l; i++)
    4518             :   {
    4519       17988 :     GEN yi = cgetg(n, t_COL);
    4520      347257 :     for (j=1; j<n; j++)
    4521             :     {
    4522     4673569 :       for (k=1; k<lM; k++) uel(v,k) = umael(gel(a,k),i,j);
    4523      329268 :       gel(yi, j) = Flm_Flc_mul_pre_Flx(M, v, p, pi, sv);
    4524             :     }
    4525       17989 :     gel(y,i) = yi;
    4526             :   }
    4527        4478 :   return y;
    4528             : }
    4529             : 
    4530             : static GEN
    4531           0 : FlkM_inv(GEN M, GEN P, ulong p)
    4532             : {
    4533           0 :   ulong PI = get_Fl_red(p), pi = SMALL_ULONG(p)? 0: PI;
    4534           0 :   GEN R = Flx_roots_pre(P, p, pi);
    4535           0 :   long l = lg(R), i;
    4536           0 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    4537           0 :   GEN V = cgetg(l, t_VEC);
    4538           0 :   for(i=1; i<l; i++)
    4539             :   {
    4540           0 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, PI);
    4541           0 :     GEN H = Flm_inv_sp(FlxM_eval_powers_pre(M, pows, p, pi), NULL, p);
    4542           0 :     if (!H) return NULL;
    4543           0 :     gel(V, i) = H;
    4544             :   }
    4545           0 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    4546             : }
    4547             : 
    4548             : static GEN
    4549        3201 : FlkM_adjoint(GEN M, GEN P, ulong p)
    4550             : {
    4551        3201 :   ulong PI = get_Fl_red(p), pi = SMALL_ULONG(p)? 0: PI;
    4552        3201 :   GEN R = Flx_roots_pre(P, p, pi);
    4553        3201 :   long l = lg(R), i;
    4554        3201 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    4555        3201 :   GEN V = cgetg(l, t_VEC);
    4556       15521 :   for(i=1; i<l; i++)
    4557             :   {
    4558       12320 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, PI);
    4559       12320 :     gel(V, i) = Flm_adjoint(FlxM_eval_powers_pre(M, pows, p, pi), p);
    4560             :   }
    4561        3201 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    4562             : }
    4563             : 
    4564             : static GEN
    4565        1978 : ZabM_inv_slice(GEN A, GEN Q, GEN P, GEN *mod)
    4566             : {
    4567        1978 :   pari_sp av = avma;
    4568        1978 :   long i, n = lg(P)-1, w = varn(Q);
    4569             :   GEN H, T;
    4570        1978 :   if (n == 1)
    4571             :   {
    4572        1548 :     ulong p = uel(P,1);
    4573        1548 :     GEN Qp = ZX_to_Flx(Q, p);
    4574        1548 :     GEN Ap = ZXM_to_FlxM(A, p, get_Flx_var(Qp));
    4575        1548 :     GEN Hp = FlkM_adjoint(Ap, Qp, p);
    4576        1548 :     Hp = gerepileupto(av, FlxM_to_ZXM(Hp));
    4577        1548 :     *mod = utoipos(p); return Hp;
    4578             :   }
    4579         430 :   T = ZV_producttree(P);
    4580         430 :   A = ZXM_nv_mod_tree(A, P, T, w);
    4581         430 :   Q = ZX_nv_mod_tree(Q, P, T);
    4582         430 :   H = cgetg(n+1, t_VEC);
    4583        2083 :   for(i=1; i <= n; i++)
    4584             :   {
    4585        1653 :     ulong p = P[i];
    4586        1653 :     GEN a = gel(A,i), q = gel(Q, i);
    4587        1653 :     gel(H,i) = FlkM_adjoint(a, q, p);
    4588             :   }
    4589         430 :   H = nxMV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    4590         430 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    4591             : }
    4592             : 
    4593             : GEN
    4594        1978 : ZabM_inv_worker(GEN P, GEN A, GEN Q)
    4595             : {
    4596        1978 :   GEN V = cgetg(3, t_VEC);
    4597        1978 :   gel(V,1) = ZabM_inv_slice(A, Q, P, &gel(V,2));
    4598        1978 :   return V;
    4599             : }
    4600             : 
    4601             : static GEN
    4602        5467 : vecnorml1(GEN a)
    4603             : {
    4604             :   long i, l;
    4605        5467 :   GEN g = cgetg_copy(a, &l);
    4606       60214 :   for (i=1; i<l; i++)
    4607       54747 :     gel(g, i) = gnorml1_fake(gel(a,i));
    4608        5467 :   return g;
    4609             : }
    4610             : 
    4611             : static GEN
    4612        1820 : ZabM_true_Hadamard(GEN a)
    4613             : {
    4614        1820 :   pari_sp av = avma;
    4615        1820 :   long n = lg(a)-1, i;
    4616             :   GEN B;
    4617        1820 :   if (n == 0) return gen_1;
    4618        1820 :   if (n == 1) return gnorml1_fake(gcoeff(a,1,1));
    4619        1176 :   B = gen_1;
    4620        6643 :   for (i = 1; i <= n; i++)
    4621        5467 :     B = gmul(B, gnorml2(RgC_gtofp(vecnorml1(gel(a,i)),DEFAULTPREC)));
    4622        1176 :   return gerepileuptoint(av, ceil_safe(sqrtr_abs(B)));
    4623             : }
    4624             : 
    4625             : GEN
    4626        1820 : ZabM_inv(GEN A, GEN Q, long n, GEN *pt_den)
    4627             : {
    4628        1820 :   pari_sp av = avma;
    4629             :   forprime_t S;
    4630             :   GEN bnd, H, D, d, mod, worker;
    4631        1820 :   if (lg(A) == 1)
    4632             :   {
    4633           0 :     if (pt_den) *pt_den = gen_1;
    4634           0 :     return cgetg(1, t_MAT);
    4635             :   }
    4636        1820 :   bnd = ZabM_true_Hadamard(A);
    4637        1820 :   worker = snm_closure(is_entry("_ZabM_inv_worker"), mkvec2(A, Q));
    4638        1820 :   u_forprime_arith_init(&S, HIGHBIT+1, ULONG_MAX, 1, n);
    4639        1820 :   H = gen_crt("ZabM_inv", worker, &S, NULL, expi(bnd), 0, &mod,
    4640             :               nxMV_chinese_center, FpXM_center);
    4641        1820 :   D = RgMrow_RgC_mul(H, gel(A,1), 1);
    4642        1820 :   D = ZX_rem(D, Q);
    4643        1820 :   d = Z_content(mkvec2(H, D));
    4644        1820 :   if (d)
    4645             :   {
    4646         511 :     D = ZX_Z_divexact(D, d);
    4647         511 :     H = Q_div_to_int(H, d);
    4648             :   }
    4649        1820 :   if (!pt_den) return gerepileupto(av, H);
    4650        1820 :   *pt_den = D; return gc_all(av, 2, &H, pt_den);
    4651             : }
    4652             : 
    4653             : GEN
    4654           0 : ZabM_inv_ratlift(GEN M, GEN P, long n, GEN *pden)
    4655             : {
    4656           0 :   pari_sp av2, av = avma;
    4657             :   GEN q, H;
    4658           0 :   ulong m = LONG_MAX>>1;
    4659           0 :   ulong p= 1 + m - (m % n);
    4660           0 :   long lM = lg(M);
    4661           0 :   if (lM == 1) { *pden = gen_1; return cgetg(1,t_MAT); }
    4662             : 
    4663           0 :   av2 = avma;
    4664           0 :   H = NULL;
    4665             :   for(;;)
    4666           0 :   {
    4667             :     GEN Hp, Pp, Mp, Hr;
    4668           0 :     do p += n; while(!uisprime(p));
    4669           0 :     Pp = ZX_to_Flx(P, p);
    4670           0 :     Mp = ZXM_to_FlxM(M, p, get_Flx_var(Pp));
    4671           0 :     Hp = FlkM_inv(Mp, Pp, p);
    4672           0 :     if (!Hp) continue;
    4673           0 :     if (!H)
    4674             :     {
    4675           0 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p);
    4676           0 :       q = utoipos(p);
    4677             :     }
    4678             :     else
    4679           0 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    4680           0 :     Hr = FpXM_ratlift(H, q);
    4681           0 :     if (DEBUGLEVEL>5) err_printf("ZabM_inv mod %ld (ratlift=%ld)\n", p,!!Hr);
    4682           0 :     if (Hr) {/* DONE ? */
    4683           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    4684           0 :       GEN MH = ZXQM_mul(Hl, M, P);
    4685           0 :       if (*pden)
    4686           0 :       { if (RgM_isscalar(MH, *pden)) { H = Hl; break; }}
    4687             :       else
    4688           0 :       { if (RgM_isidentity(MH)) { H = Hl; *pden = gen_1; break; } }
    4689             :     }
    4690             : 
    4691           0 :     if (gc_needed(av,2))
    4692             :     {
    4693           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_inv");
    4694           0 :       gerepileall(av2, 2, &H, &q);
    4695             :     }
    4696             :   }
    4697           0 :   return gc_all(av, 2, &H, pden);
    4698             : }
    4699             : 
    4700             : static GEN
    4701        1276 : FlkM_ker(GEN M, GEN P, ulong p)
    4702             : {
    4703        1276 :   ulong PI = get_Fl_red(p), pi = SMALL_ULONG(p)? 0: PI;
    4704        1276 :   GEN R = Flx_roots_pre(P, p, pi);
    4705        1276 :   long l = lg(R), i, dP = degpol(P), r;
    4706             :   GEN M1, K, D;
    4707        1276 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    4708        1276 :   GEN V = cgetg(l, t_VEC);
    4709        1276 :   M1 = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,1), dP, p, PI), p, pi);
    4710        1276 :   K = Flm_ker_sp(M1, p, 2);
    4711        1276 :   r = lg(gel(K,1)); D = gel(K,2);
    4712        1276 :   gel(V, 1) = gel(K,1);
    4713        2652 :   for(i=2; i<l; i++)
    4714             :   {
    4715        1376 :     GEN Mi = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,i), dP, p, PI), p, pi);
    4716        1376 :     GEN K = Flm_ker_sp(Mi, p, 2);
    4717        1376 :     if (lg(gel(K,1)) != r || !zv_equal(D, gel(K,2))) return NULL;
    4718        1376 :     gel(V, i) = gel(K,1);
    4719             :   }
    4720        1276 :   return mkvec2(FlmV_recover_pre(V, W, p, pi, P[1]), D);
    4721             : }
    4722             : 
    4723             : static int
    4724         655 : ZabM_ker_check(GEN M, GEN H, ulong p, GEN P, long n)
    4725             : {
    4726             :   GEN pow;
    4727         655 :   long j, l = lg(H);
    4728             :   ulong pi, r;
    4729        3899 :   do p += n; while(!uisprime(p));
    4730         655 :   pi = get_Fl_red(p);
    4731         655 :   P = ZX_to_Flx(P, p);
    4732         655 :   r = Flx_oneroot_pre(P, p, pi);
    4733         655 :   pow = Fl_powers_pre(r, degpol(P),p, (p & HIGHMASK)? pi: 0);
    4734         655 :   M = ZXM_to_FlxM(M, p, P[1]); M = FlxM_eval_powers_pre(M, pow, p, pi);
    4735         655 :   H = ZXM_to_FlxM(H, p, P[1]); H = FlxM_eval_powers_pre(H, pow, p, pi);
    4736        2178 :   for (j = 1; j < l; j++)
    4737        1555 :     if (!zv_equal0(Flm_Flc_mul_pre(M, gel(H,j), p, pi))) return 0;
    4738         623 :   return 1;
    4739             : }
    4740             : 
    4741             : GEN
    4742         623 : ZabM_ker(GEN M, GEN P, long n)
    4743             : {
    4744         623 :   pari_sp av = avma;
    4745             :   pari_timer ti;
    4746         623 :   GEN q, H = NULL, D = NULL;
    4747         623 :   ulong m = LONG_MAX>>1;
    4748         623 :   ulong p = 1 + m - (m % n);
    4749             : 
    4750         623 :   if (DEBUGLEVEL>5) timer_start(&ti);
    4751             :   for(;;)
    4752         653 :   {
    4753             :     GEN Kp, Hp, Dp, Pp, Mp, Hr;
    4754       22341 :     do p += n; while(!uisprime(p));
    4755        1276 :     Pp = ZX_to_Flx(P, p);
    4756        1276 :     Mp = ZXM_to_FlxM(M, p, get_Flx_var(Pp));
    4757        1276 :     Kp = FlkM_ker(Mp, Pp, p);
    4758        1276 :     if (!Kp) continue;
    4759        1276 :     Hp = gel(Kp,1); Dp = gel(Kp,2);
    4760        1276 :     if (H && (lg(Hp)>lg(H) || (lg(Hp)==lg(H) && vecsmall_lexcmp(Dp,D)>0))) continue;
    4761        1276 :     if (!H || (lg(Hp)<lg(H) || vecsmall_lexcmp(Dp,D)<0))
    4762             :     {
    4763         623 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p); D = Dp;
    4764         623 :       q = utoipos(p);
    4765             :     }
    4766             :     else
    4767         653 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    4768        1276 :     Hr = FpXM_ratlift(H, q);
    4769        1276 :     if (DEBUGLEVEL>5) timer_printf(&ti,"ZabM_ker mod %ld (ratlift=%ld)", p,!!Hr);
    4770        1276 :     if (Hr) {/* DONE ? */
    4771         655 :       GEN Hl = vec_Q_primpart(Hr);
    4772         655 :       if (ZabM_ker_check(M, Hl, p, P, n)) { H = Hl;  break; }
    4773             :     }
    4774             : 
    4775         653 :     if (gc_needed(av,2))
    4776             :     {
    4777           4 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_ker");
    4778           4 :       gerepileall(av, 3, &H, &D, &q);
    4779             :     }
    4780             :   }
    4781         623 :   return gerepilecopy(av, H);
    4782             : }
    4783             : 
    4784             : GEN
    4785        2387 : ZabM_indexrank(GEN M, GEN P, long n)
    4786             : {
    4787        2387 :   pari_sp av = avma;
    4788        2387 :   ulong m = LONG_MAX>>1;
    4789        2387 :   ulong p = 1+m-(m%n), D = degpol(P);
    4790        2387 :   long lM = lg(M), lmax = 0, c = 0;
    4791             :   GEN v;
    4792             :   for(;;)
    4793         735 :   {
    4794             :     GEN R, Pp, Mp, K;
    4795             :     ulong pi;
    4796             :     long l;
    4797       61415 :     do p += n; while (!uisprime(p));
    4798        3122 :     pi = (p & HIGHMASK)? get_Fl_red(p): 0;
    4799        3122 :     Pp = ZX_to_Flx(P, p);
    4800        3122 :     R = Flx_roots_pre(Pp, p, pi);
    4801        3122 :     Mp = ZXM_to_FlxM(M, p, get_Flx_var(Pp));
    4802        3122 :     K = FlxM_eval_powers_pre(Mp, Fl_powers_pre(uel(R,1), D,p,pi), p,pi);
    4803        3122 :     v = Flm_indexrank(K, p);
    4804        3122 :     l = lg(gel(v,2));
    4805        3122 :     if (l == lM) break;
    4806         980 :     if (lmax >= 0 && l > lmax) { lmax = l; c = 0; } else c++;
    4807         980 :     if (c > 2)
    4808             :     { /* probably not maximal rank, expensive check */
    4809         245 :       lM -= lg(ZabM_ker(M, P, n))-1; /* actual rank (+1) */
    4810         245 :       if (lmax == lM) break;
    4811           0 :       lmax = -1; /* disable check */
    4812             :     }
    4813             :   }
    4814        2387 :   return gerepileupto(av, v);
    4815             : }
    4816             : 
    4817             : #if 0
    4818             : GEN
    4819             : ZabM_gauss(GEN M, GEN P, long n, GEN *den)
    4820             : {
    4821             :   pari_sp av = avma;
    4822             :   GEN v, S, W;
    4823             :   v = ZabM_indexrank(M, P, n);
    4824             :   S = shallowmatextract(M,gel(v,1),gel(v,2));
    4825             :   W = ZabM_inv(S, P, n, den);
    4826             :   return gc_all(av,2,&W,den);
    4827             : }
    4828             : #endif
    4829             : 
    4830             : GEN
    4831         140 : ZabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *den)
    4832             : {
    4833         140 :   GEN v = ZabM_indexrank(M, P, n);
    4834         140 :   if (pv) *pv = v;
    4835         140 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    4836         140 :   return ZabM_inv(M, P, n, den);
    4837             : }
    4838             : GEN
    4839        5012 : ZM_pseudoinv(GEN M, GEN *pv, GEN *den)
    4840             : {
    4841        5012 :   GEN v = ZM_indexrank(M);
    4842        5012 :   if (pv) *pv = v;
    4843        5012 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    4844        5012 :   return ZM_inv(M, den);
    4845             : }
    4846             : 
    4847             : /*******************************************************************/
    4848             : /*                                                                 */
    4849             : /*                   Structured Elimination                        */
    4850             : /*                                                                 */
    4851             : /*******************************************************************/
    4852             : 
    4853             : static void
    4854      111666 : rem_col(GEN c, long i, GEN iscol, GEN Wrow, long *rcol, long *rrow)
    4855             : {
    4856      111666 :   long lc = lg(c), k;
    4857      111666 :   iscol[i] = 0; (*rcol)--;
    4858     1078748 :   for (k = 1; k < lc; ++k)
    4859             :   {
    4860      967082 :     Wrow[c[k]]--;
    4861      967082 :     if (Wrow[c[k]]==0) (*rrow)--;
    4862             :   }
    4863      111666 : }
    4864             : 
    4865             : static void
    4866        6276 : rem_singleton(GEN M, GEN iscol, GEN Wrow, long idx, long *rcol, long *rrow)
    4867             : {
    4868             :   long i, j;
    4869        6276 :   long nbcol = lg(iscol)-1, last;
    4870             :   do
    4871             :   {
    4872        8352 :     last = 0;
    4873    19438191 :     for (i = 1; i <= nbcol; ++i)
    4874    19429839 :       if (iscol[i])
    4875             :       {
    4876    10129080 :         GEN c = idx ? gmael(M, i, idx): gel(M,i);
    4877    10129080 :         long lc = lg(c);
    4878    97287178 :         for (j = 1; j < lc; ++j)
    4879    87180407 :           if (Wrow[c[j]] == 1)
    4880             :           {
    4881       22309 :             rem_col(c, i, iscol, Wrow, rcol, rrow);
    4882       22309 :             last=1; break;
    4883             :           }
    4884             :       }
    4885        8352 :   } while (last);
    4886        6276 : }
    4887             : 
    4888             : static GEN
    4889        6104 : fill_wcol(GEN M, GEN iscol, GEN Wrow, long *w, GEN wcol)
    4890             : {
    4891        6104 :   long nbcol = lg(iscol)-1;
    4892             :   long i, j, m, last;
    4893             :   GEN per;
    4894       15295 :   for (m = 2, last=0; !last ; m++)
    4895             :   {
    4896    22446802 :     for (i = 1; i <= nbcol; ++i)
    4897             :     {
    4898    22437611 :       wcol[i] = 0;
    4899    22437611 :       if (iscol[i])
    4900             :       {
    4901    11520654 :         GEN c = gmael(M, i, 1);
    4902    11520654 :         long lc = lg(c);
    4903   107250627 :         for (j = 1; j < lc; ++j)
    4904    95729973 :           if (Wrow[c[j]] == m) {  wcol[i]++; last = 1; }
    4905             :       }
    4906             :     }
    4907             :   }
    4908        6104 :   per = vecsmall_indexsort(wcol);
    4909        6104 :   *w = wcol[per[nbcol]];
    4910        6104 :   return per;
    4911             : }
    4912             : 
    4913             : /* M is a RgMs with nbrow rows, A a list of row indices.
    4914             :    Eliminate rows of M with a single entry that do not belong to A,
    4915             :    and the corresponding columns. Also eliminate columns until #colums=#rows.
    4916             :    Return pcol and prow:
    4917             :    pcol is a map from the new columns indices to the old one.
    4918             :    prow is a map from the old rows indices to the new one (0 if removed).
    4919             : */
    4920             : 
    4921             : void
    4922         119 : RgMs_structelim_col(GEN M, long nbcol, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    4923             : {
    4924         119 :   long i, j, k, lA = lg(A);
    4925         119 :   GEN prow = cgetg(nbrow+1, t_VECSMALL);
    4926         119 :   GEN pcol = zero_zv(nbcol);
    4927         119 :   pari_sp av = avma;
    4928         119 :   long rcol = nbcol, rrow = 0, imin = nbcol - usqrt(nbcol);
    4929         119 :   GEN iscol = const_vecsmall(nbcol, 1);
    4930         119 :   GEN Wrow  = zero_zv(nbrow);
    4931         119 :   GEN wcol = cgetg(nbcol+1, t_VECSMALL);
    4932         119 :   pari_sp av2 = avma;
    4933      126763 :   for (i = 1; i <= nbcol; ++i)
    4934             :   {
    4935      126644 :     GEN F = gmael(M, i, 1);
    4936      126644 :     long l = lg(F)-1;
    4937     1115876 :     for (j = 1; j <= l; ++j) Wrow[F[j]]++;
    4938             :   }
    4939         119 :   for (j = 1; j < lA; ++j)
    4940             :   {
    4941           0 :     if (Wrow[A[j]] == 0) { *p_col=NULL; return; }
    4942           0 :     Wrow[A[j]] = -1;
    4943             :   }
    4944      235298 :   for (i = 1; i <= nbrow; ++i)
    4945      235179 :     if (Wrow[i]) rrow++;
    4946         119 :   rem_singleton(M, iscol, Wrow, 1, &rcol, &rrow);
    4947         119 :   if (rcol < rrow) pari_err_BUG("RgMs_structelim, rcol<rrow");
    4948        6223 :   while (rcol > rrow)
    4949             :   {
    4950             :     long w;
    4951        6104 :     GEN per = fill_wcol(M, iscol, Wrow, &w, wcol);
    4952       95461 :     for (i = nbcol; i>=imin && wcol[per[i]]>=w && rcol>rrow; i--)
    4953       89357 :       rem_col(gmael(M, per[i], 1), per[i], iscol, Wrow, &rcol, &rrow);
    4954        6104 :     rem_singleton(M, iscol, Wrow, 1, &rcol, &rrow); set_avma(av2);
    4955             :   }
    4956      126763 :   for (j = 1, i = 1; i <= nbcol; ++i)
    4957      126644 :     if (iscol[i]) pcol[j++] = i;
    4958         119 :   setlg(pcol,j);
    4959      235298 :   for (k = 1, i = 1; i <= nbrow; ++i) prow[i] = Wrow[i]? k++: 0;
    4960         119 :   *p_col = pcol; *p_row = prow; set_avma(av);
    4961             : }
    4962             : 
    4963             : void
    4964           0 : RgMs_structelim(GEN M, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    4965           0 : { RgMs_structelim_col(M, lg(M)-1, nbrow, A, p_col, p_row); }
    4966             : 
    4967             : GEN
    4968          53 : F2Ms_colelim(GEN M, long nbrow)
    4969             : {
    4970          53 :   long i,j, nbcol = lg(M)-1, rcol = nbcol, rrow = 0;
    4971          53 :   GEN pcol = zero_zv(nbcol);
    4972          53 :   pari_sp av = avma;
    4973          53 :   GEN iscol = const_vecsmall(nbcol, 1), Wrow  = zero_zv(nbrow);
    4974       85968 :   for (i = 1; i <= nbcol; ++i)
    4975             :   {
    4976       85915 :     GEN F = gel(M, i);
    4977       85915 :     long l = lg(F)-1;
    4978     1587632 :     for (j = 1; j <= l; ++j) Wrow[F[j]]++;
    4979             :   }
    4980          53 :   rem_singleton(M, iscol, Wrow, 0, &rcol, &rrow);
    4981       85968 :   for (j = 1, i = 1; i <= nbcol; ++i)
    4982       85915 :     if (iscol[i]) pcol[j++] = i;
    4983          53 :   fixlg(pcol,j); return gc_const(av, pcol);
    4984             : }
    4985             : 
    4986             : /*******************************************************************/
    4987             : /*                                                                 */
    4988             : /*                        EIGENVECTORS                             */
    4989             : /*   (independent eigenvectors, sorted by increasing eigenvalue)   */
    4990             : /*                                                                 */
    4991             : /*******************************************************************/
    4992             : /* assume x is square of dimension > 0 */
    4993             : static int
    4994          53 : RgM_is_symmetric_cx(GEN x, long bit)
    4995             : {
    4996          53 :   pari_sp av = avma;
    4997          53 :   long i, j, l = lg(x);
    4998         239 :   for (i = 1; i < l; i++)
    4999         708 :     for (j = 1; j < i; j++)
    5000             :     {
    5001         522 :       GEN a = gcoeff(x,i,j), b = gcoeff(x,j,i), c = gsub(a,b);
    5002         522 :       if (!gequal0(c) && gexpo(c) - gexpo(a) > -bit) return gc_long(av,0);
    5003             :     }
    5004          21 :   return gc_long(av,1);
    5005             : }
    5006             : static GEN
    5007          53 : eigen_err(int exact, GEN x, long flag, long prec)
    5008             : {
    5009          53 :   pari_sp av = avma;
    5010             :   GEN y;
    5011          53 :   if (RgM_is_symmetric_cx(x, prec - 10))
    5012             :   { /* approximately symmetric: recover */
    5013          21 :     x = jacobi(x, prec); if (flag) return x;
    5014          14 :     return gerepilecopy(av, gel(x,2));
    5015             :   }
    5016          32 :   if (!exact) x = bestappr(x, NULL);
    5017          32 :   y = mateigen(x, flag, precdbl(prec));
    5018          32 :   if (exact)
    5019          18 :     y = gprec_wtrunc(y, prec);
    5020          14 :   else if (flag)
    5021           7 :     y = mkvec2(RgV_gtofp(gel(y,1), prec), RgM_gtofp(gel(y,2), prec));
    5022             :   else
    5023           7 :     y = RgM_gtofp(y, prec);
    5024          32 :   return gerepilecopy(av, y);
    5025             : }
    5026             : GEN
    5027         144 : mateigen(GEN x, long flag, long prec)
    5028             : {
    5029             :   GEN y, R, T;
    5030         144 :   long k, l, ex, n = lg(x);
    5031             :   int exact;
    5032         144 :   pari_sp av = avma;
    5033             : 
    5034         144 :   if (typ(x)!=t_MAT) pari_err_TYPE("eigen",x);
    5035         144 :   if (n != 1 && n != lgcols(x)) pari_err_DIM("eigen");
    5036         144 :   if (flag < 0 || flag > 1) pari_err_FLAG("mateigen");
    5037         144 :   if (n == 1)
    5038             :   {
    5039          14 :     if (flag) retmkvec2(cgetg(1,t_COL), cgetg(1,t_MAT));
    5040           7 :     return cgetg(1,t_MAT);
    5041             :   }
    5042         130 :   if (n == 2)
    5043             :   {
    5044          14 :     if (flag) retmkvec2(mkcolcopy(gcoeff(x,1,1)), matid(1));
    5045           7 :     return matid(1);
    5046             :   }
    5047             : 
    5048         116 :   ex = 16 - prec;
    5049         116 :   T = charpoly(x,0);
    5050         116 :   exact = RgX_is_QX(T);
    5051         116 :   if (exact)
    5052             :   {
    5053          74 :     T = ZX_radical( Q_primpart(T) );
    5054          74 :     R = nfrootsQ(T); settyp(R, t_COL);
    5055          74 :     if (lg(R)-1 < degpol(T))
    5056             :     { /* add missing complex roots */
    5057          60 :       GEN r = cleanroots(RgX_div(T, roots_to_pol(R, 0)), prec);
    5058          60 :       R = shallowconcat(R, r);
    5059             :     }
    5060             :   }
    5061             :   else
    5062             :   {
    5063          42 :     GEN r1, v = vectrunc_init(lg(T));
    5064             :     long e;
    5065          42 :     R = cleanroots(T,prec);
    5066          42 :     r1 = NULL;
    5067         266 :     for (k = 1; k < lg(R); k++)
    5068             :     {
    5069         224 :       GEN r2 = gel(R,k), r = grndtoi(r2, &e);
    5070         224 :       if (e < ex) r2 = r;
    5071         224 :       if (r1)
    5072             :       {
    5073         182 :         r = gsub(r1,r2);
    5074         182 :         if (gequal0(r) || gexpo(r) < ex) continue;
    5075             :       }
    5076         182 :       vectrunc_append(v, r2);
    5077         182 :       r1 = r2;
    5078             :     }
    5079          42 :     R = v;
    5080             :   }
    5081             :   /* R = distinct complex roots of charpoly(x) */
    5082         116 :   l = lg(R); y = cgetg(l, t_VEC);
    5083         452 :   for (k = 1; k < l; k++)
    5084             :   {
    5085         389 :     GEN F = ker_aux(RgM_Rg_sub_shallow(x, gel(R,k)), x);
    5086         389 :     long d = lg(F)-1;
    5087         389 :     if (!d) { set_avma(av); return eigen_err(exact, x, flag, prec); }
    5088         336 :     gel(y,k) = F;
    5089         336 :     if (flag) gel(R,k) = const_col(d, gel(R,k));
    5090             :   }
    5091          63 :   y = shallowconcat1(y);
    5092          63 :   if (lg(y) > n) { set_avma(av); return eigen_err(exact, x, flag, prec); }
    5093             :   /* lg(y) < n if x is not diagonalizable */
    5094          63 :   if (flag) y = mkvec2(shallowconcat1(R), y);
    5095          63 :   return gerepilecopy(av,y);
    5096             : }
    5097             : GEN
    5098           0 : eigen(GEN x, long prec) { return mateigen(x, 0, prec); }
    5099             : 
    5100             : /*******************************************************************/
    5101             : /*                                                                 */
    5102             : /*                           DETERMINANT                           */
    5103             : /*                                                                 */
    5104             : /*******************************************************************/
    5105             : 
    5106             : GEN
    5107       26474 : det0(GEN a,long flag)
    5108             : {
    5109       26474 :   switch(flag)
    5110             :   {
    5111       26460 :     case 0: return det(a);
    5112          14 :     case 1: return det2(a);
    5113           0 :     default: pari_err_FLAG("matdet");
    5114             :   }
    5115             :   return NULL; /* LCOV_EXCL_LINE */
    5116             : }
    5117             : 
    5118             : /* M a 2x2 matrix, returns det(M) */
    5119             : static GEN
    5120       84640 : RgM_det2(GEN M)
    5121             : {
    5122       84640 :   pari_sp av = avma;
    5123       84640 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    5124       84640 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    5125       84640 :   return gerepileupto(av, gsub(gmul(a,d), gmul(b,c)));
    5126             : }
    5127             : /* M a 2x2 ZM, returns det(M) */
    5128             : static GEN
    5129        6377 : ZM_det2(GEN M)
    5130             : {
    5131        6377 :   pari_sp av = avma;
    5132        6377 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    5133        6377 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    5134        6377 :   return gerepileuptoint(av, subii(mulii(a,d), mulii(b, c)));
    5135             : }
    5136             : /* M a 3x3 ZM, return det(M) */
    5137             : static GEN
    5138       99954 : ZM_det3(GEN M)
    5139             : {
    5140       99954 :   pari_sp av = avma;
    5141       99954 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2), c = gcoeff(M,1,3);
    5142       99954 :   GEN d = gcoeff(M,2,1), e = gcoeff(M,2,2), f = gcoeff(M,2,3);
    5143       99954 :   GEN g = gcoeff(M,3,1), h = gcoeff(M,3,2), i = gcoeff(M,3,3);
    5144       99954 :   GEN t, D = signe(i)? mulii(subii(mulii(a,e), mulii(b,d)), i): gen_0;
    5145       99954 :   if (signe(g))
    5146             :   {
    5147       66097 :     t = mulii(subii(mulii(b,f), mulii(c,e)), g);
    5148       66097 :     D = addii(D, t);
    5149             :   }
    5150       99954 :   if (signe(h))
    5151             :   {
    5152       77366 :     t = mulii(subii(mulii(c,d), mulii(a,f)), h);
    5153       77366 :     D = addii(D, t);
    5154             :   }
    5155       99954 :   return gerepileuptoint(av, D);
    5156             : }
    5157             : 
    5158             : static GEN
    5159       58407 : det_simple_gauss(GEN a, GEN data, pivot_fun pivot)
    5160             : {
    5161       58407 :   pari_sp av = avma;
    5162       58407 :   long i,j,k, s = 1, nbco = lg(a)-1;
    5163       58407 :   GEN p, x = gen_1;
    5164             : 
    5165       58407 :   a = RgM_shallowcopy(a);
    5166      342141 :   for (i=1; i<nbco; i++)
    5167             :   {
    5168      283742 :     k = pivot(a, data, i, NULL);
    5169      283742 :     if (k > nbco) return gerepilecopy(av, gcoeff(a,i,i));
    5170      283735 :     if (k != i)
    5171             :     { /* exchange the lines s.t. k = i */
    5172     1153767 :       for (j=i; j<=nbco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    5173      118561 :       s = -s;
    5174             :     }
    5175      283735 :     p = gcoeff(a,i,i);
    5176             : 
    5177      283735 :     x = gmul(x,p);
    5178     1785093 :     for (k=i+1; k<=nbco; k++)
    5179             :     {
    5180     1501360 :       GEN m = gcoeff(a,i,k);
    5181     1501360 :       if (gequal0(m)) continue;
    5182             : 
    5183     1066996 :       m = gdiv(m,p);
    5184     9938143 :       for (j=i+1; j<=nbco; j++)
    5185     8871149 :         gcoeff(a,j,k) = gsub(gcoeff(a,j,k), gmul(m,gcoeff(a,j,i)));
    5186             :     }
    5187      283733 :     if (gc_needed(av,2))
    5188             :     {
    5189           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    5190           0 :       gerepileall(av,2, &a,&x);
    5191             :     }
    5192             :   }
    5193       58399 :   if (s < 0) x = gneg_i(x);
    5194       58399 :   return gerepileupto(av, gmul(x, gcoeff(a,nbco,nbco)));
    5195             : }
    5196             : 
    5197             : GEN
    5198      115662 : det2(GEN a)
    5199             : {
    5200             :   GEN data;
    5201             :   pivot_fun pivot;
    5202      115662 :   long n = lg(a)-1;
    5203      115662 :   if (typ(a)!=t_MAT) pari_err_TYPE("det2",a);
    5204      115662 :   if (!n) return gen_1;
    5205      115662 :   if (n != nbrows(a)) pari_err_DIM("det2");
    5206      115662 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    5207       76598 :   if (n == 2) return RgM_det2(a);
    5208       27090 :   pivot = get_pivot_fun(a, a, &data);
    5209       27090 :   return det_simple_gauss(a, data, pivot);
    5210             : }
    5211             : 
    5212             : /* Assumes a a square t_MAT of dimension n > 0. Returns det(a) using
    5213             :  * Gauss-Bareiss. */
    5214             : static GEN
    5215         462 : det_bareiss(GEN a)
    5216             : {
    5217         462 :   pari_sp av = avma;
    5218         462 :   long nbco = lg(a)-1,i,j,k,s = 1;
    5219             :   GEN p, pprec;
    5220             : 
    5221         462 :   a = RgM_shallowcopy(a);
    5222        1337 :   for (pprec=gen_1,i=1; i<nbco; i++,pprec=p)
    5223             :   {
    5224         882 :     int diveuc = (gequal1(pprec)==0);
    5225             :     GEN ci;
    5226             : 
    5227         882 :     p = gcoeff(a,i,i);
    5228         882 :     if (gequal0(p))
    5229             :     {
    5230          14 :       k=i+1; while (k<=nbco && gequal0(gcoeff(a,i,k))) k++;
    5231           7 :       if (k>nbco) return gerepilecopy(av, p);
    5232           0 :       swap(gel(a,k), gel(a,i)); s = -s;
    5233           0 :       p = gcoeff(a,i,i);
    5234             :     }
    5235         875 :     ci = gel(a,i);
    5236        2373 :     for (k=i+1; k<=nbco; k++)
    5237             :     {
    5238        1498 :       GEN ck = gel(a,k), m = gel(ck,i);
    5239        1498 :       if (gequal0(m))
    5240             :       {
    5241           7 :         if (gequal1(p))
    5242             :         {
    5243           0 :           if (diveuc)
    5244           0 :             gel(a,k) = gdiv(gel(a,k), pprec);
    5245             :         }
    5246             :         else
    5247          42 :           for (j=i+1; j<=nbco; j++)
    5248             :           {
    5249          35 :             GEN p1 = gmul(p, gel(ck,j));
    5250          35 :             if (diveuc) p1 = gdiv(p1,pprec);
    5251          35 :             gel(ck,j) = p1;
    5252             :           }
    5253             :       }
    5254             :       else
    5255        4662 :         for (j=i+1; j<=nbco; j++)
    5256             :         {
    5257        3171 :           pari_sp av2 = avma;
    5258        3171 :           GEN p1 = gsub(gmul(p,gel(ck,j)), gmul(m,gel(ci,j)));
    5259        3171 :           if (diveuc) p1 = gdiv(p1,pprec);
    5260        3171 :           gel(ck,j) = gerepileupto(av2, p1);
    5261             :         }
    5262        1498 :       if (gc_needed(av,2))
    5263             :       {
    5264           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    5265           0 :         gerepileall(av,2, &a,&pprec);
    5266           0 :         ci = gel(a,i);
    5267           0 :         p = gcoeff(a,i,i);
    5268             :       }
    5269             :     }
    5270             :   }
    5271         455 :   p = gcoeff(a,nbco,nbco);
    5272         455 :   p = (s < 0)? gneg(p): gcopy(p);
    5273         455 :   return gerepileupto(av, p);
    5274             : }
    5275             : 
    5276             : /* count nonzero entries in col j, at most 'max' of them.
    5277             :  * Return their indices */
    5278             : static GEN
    5279        1470 : col_count_non_zero(GEN a, long j, long max)
    5280             : {
    5281        1470 :   GEN v = cgetg(max+1, t_VECSMALL);
    5282        1470 :   GEN c = gel(a,j);
    5283        1470 :   long i, l = lg(a), k = 1;
    5284        5614 :   for (i = 1; i < l; i++)
    5285        5376 :     if (!gequal0(gel(c,i)))
    5286             :     {
    5287        5110 :       if (k > max) return NULL; /* fail */
    5288        3878 :       v[k++] = i;
    5289             :     }
    5290         238 :   setlg(v, k); return v;
    5291             : }
    5292             : /* count nonzero entries in row i, at most 'max' of them.
    5293             :  * Return their indices */
    5294             : static GEN
    5295        1456 : row_count_non_zero(GEN a, long i, long max)
    5296             : {
    5297        1456 :   GEN v = cgetg(max+1, t_VECSMALL);
    5298        1456 :   long j, l = lg(a), k = 1;
    5299        5558 :   for (j = 1; j < l; j++)
    5300        5334 :     if (!gequal0(gcoeff(a,i,j)))
    5301             :     {
    5302        5096 :       if (k > max) return NULL; /* fail */
    5303        3864 :       v[k++] = j;
    5304             :     }
    5305         224 :   setlg(v, k); return v;
    5306             : }
    5307             : 
    5308             : static GEN det_develop(GEN a, long max, double bound);
    5309             : /* (-1)^(i+j) a[i,j] * det RgM_minor(a,i,j) */
    5310             : static GEN
    5311         406 : coeff_det(GEN a, long i, long j, long max, double bound)
    5312             : {
    5313         406 :   GEN c = gcoeff(a, i, j);
    5314         406 :   c = gmul(c, det_develop(RgM_minor(a, i,j), max, bound));
    5315         406 :   if (odd(i+j)) c = gneg(c);
    5316         406 :   return c;
    5317             : }
    5318             : /* a square t_MAT, 'bound' a rough upper bound for the number of
    5319             :  * multiplications we are willing to pay while developing rows/columns before
    5320             :  * switching to Gaussian elimination */
    5321             : static GEN
    5322         658 : det_develop(GEN M, long max, double bound)
    5323             : {
    5324         658 :   pari_sp av = avma;
    5325         658 :   long i,j, n = lg(M)-1, lbest = max+2, best_col = 0, best_row = 0;
    5326         658 :   GEN best = NULL;
    5327             : 
    5328         658 :   if (bound < 1.) return det_bareiss(M); /* too costly now */
    5329             : 
    5330         434 :   switch(n)
    5331             :   {
    5332           0 :     case 0: return gen_1;
    5333           0 :     case 1: return gcopy(gcoeff(M,1,1));
    5334          14 :     case 2: return RgM_det2(M);
    5335             :   }
    5336         420 :   if (max > ((n+2)>>1)) max = (n+2)>>1;
    5337        1876 :   for (j = 1; j <= n; j++)
    5338             :   {
    5339        1470 :     pari_sp av2 = avma;
    5340        1470 :     GEN v = col_count_non_zero(M, j, max);
    5341             :     long lv;
    5342        1470 :     if (!v || (lv = lg(v)) >= lbest) { set_avma(av2); continue; }
    5343         182 :     if (lv == 1) { set_avma(av); return gen_0; }
    5344         182 :     if (lv == 2) {
    5345          14 :       set_avma(av);
    5346          14 :       return gerepileupto(av, coeff_det(M,v[1],j,max,bound));
    5347             :     }
    5348         168 :     best = v; lbest = lv; best_col = j;
    5349             :   }
    5350        1862 :   for (i = 1; i <= n; i++)
    5351             :   {
    5352        1456 :     pari_sp av2 = avma;
    5353        1456 :     GEN v = row_count_non_zero(M, i, max);
    5354             :     long lv;
    5355        1456 :     if (!v || (lv = lg(v)) >= lbest) { set_avma(av2); continue; }
    5356           0 :     if (lv == 1) { set_avma(av); return gen_0; }
    5357           0 :     if (lv == 2) {
    5358           0 :       set_avma(av);
    5359           0 :       return gerepileupto(av, coeff_det(M,i,v[1],max,bound));
    5360             :     }
    5361           0 :     best = v; lbest = lv; best_row = i;
    5362             :   }
    5363         406 :   if (best_row)
    5364             :   {
    5365           0 :     double d = lbest-1;
    5366           0 :     GEN s = NULL;
    5367             :     long k;
    5368           0 :     bound /= d*d*d;
    5369           0 :     for (k = 1; k < lbest; k++)
    5370             :     {
    5371           0 :       GEN c = coeff_det(M, best_row, best[k], max, bound);
    5372           0 :       s = s? gadd(s, c): c;
    5373             :     }
    5374           0 :     return gerepileupto(av, s);
    5375             :   }
    5376         406 :   if (best_col)
    5377             :   {
    5378         168 :     double d = lbest-1;
    5379         168 :     GEN s = NULL;
    5380             :     long k;
    5381         168 :     bound /= d*d*d;
    5382         560 :     for (k = 1; k < lbest; k++)
    5383             :     {
    5384         392 :       GEN c = coeff_det(M, best[k], best_col, max, bound);
    5385         392 :       s = s? gadd(s, c): c;
    5386             :     }
    5387         168 :     return gerepileupto(av, s);
    5388             :   }
    5389         238 :   return det_bareiss(M);
    5390             : }
    5391             : 
    5392             : /* area of parallelogram bounded by (v1,v2) */
    5393             : static GEN
    5394       62301 : parallelogramarea(GEN v1, GEN v2)
    5395       62301 : { return gsub(gmul(gnorml2(v1), gnorml2(v2)), gsqr(RgV_dotproduct(v1, v2))); }
    5396             : 
    5397             : /* Square of Hadamard bound for det(a), a square matrix.
    5398             :  * Slight improvement: instead of using the column norms, use the area of
    5399             :  * the parallelogram formed by pairs of consecutive vectors */
    5400             : GEN
    5401       19450 : RgM_Hadamard(GEN a)
    5402             : {
    5403       19450 :   pari_sp av = avma;
    5404       19450 :   long n = lg(a)-1, i;
    5405             :   GEN B;
    5406       19450 :   if (n == 0) return gen_1;
    5407       19450 :   if (n == 1) return gsqr(gcoeff(a,1,1));
    5408       19450 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    5409       19450 :   B = gen_1;
    5410       81751 :   for (i = 1; i <= n/2; i++)
    5411       62301 :     B = gmul(B, parallelogramarea(gel(a,2*i-1), gel(a,2*i)));
    5412       19450 :   if (odd(n)) B = gmul(B, gnorml2(gel(a, n)));
    5413       19450 :   return gerepileuptoint(av, ceil_safe(B));
    5414             : }
    5415             : 
    5416             : /* If B=NULL, assume B=A' */
    5417             : static GEN
    5418       20692 : ZM_det_slice(GEN A, GEN P, GEN *mod)
    5419             : {
    5420       20692 :   pari_sp av = avma;
    5421       20692 :   long i, n = lg(P)-1;
    5422             :   GEN H, T;
    5423       20692 :   if (n == 1)
    5424             :   {
    5425           0 :     ulong Hp, p = uel(P,1);
    5426           0 :     GEN a = ZM_to_Flm(A, p);
    5427           0 :     Hp = Flm_det_sp(a, p);
    5428           0 :     set_avma(av); *mod = utoipos(p); return utoi(Hp);
    5429             :   }
    5430       20692 :   T = ZV_producttree(P);
    5431       20692 :   A = ZM_nv_mod_tree(A, P, T);
    5432       20692 :   H = cgetg(n+1, t_VECSMALL);
    5433       86628 :   for(i=1; i <= n; i++)
    5434             :   {
    5435       65936 :     ulong p = P[i];
    5436       65936 :     GEN a = gel(A,i);
    5437       65936 :     H[i] = Flm_det_sp(a, p);
    5438             :   }
    5439       20692 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    5440       20692 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    5441             : }
    5442             : 
    5443             : GEN
    5444       20692 : ZM_det_worker(GEN P, GEN A)
    5445             : {
    5446       20692 :   GEN V = cgetg(3, t_VEC);
    5447       20692 :   gel(V,1) = ZM_det_slice(A, P, &gel(V,2));
    5448       20692 :   return V;
    5449             : }
    5450             : 
    5451             : GEN
    5452      127321 : ZM_det(GEN M)
    5453             : {
    5454      127321 :   const long DIXON_THRESHOLD = 40;
    5455             :   pari_sp av, av2;
    5456      127321 :   long i, n = lg(M)-1;
    5457             :   ulong p, Dp;
    5458             :   forprime_t S;
    5459             :   pari_timer ti;
    5460             :   GEN H, D, mod, h, q, v, worker;
    5461             : #ifdef LONG_IS_64BIT
    5462      109140 :   const ulong PMAX = 18446744073709551557UL;
    5463             : #else
    5464       18181 :   const ulong PMAX = 4294967291UL;
    5465             : #endif
    5466             : 
    5467      127321 :   switch(n)
    5468             :   {
    5469           7 :     case 0: return gen_1;
    5470        1533 :     case 1: return icopy(gcoeff(M,1,1));
    5471        6377 :     case 2: return ZM_det2(M);
    5472       99954 :     case 3: return ZM_det3(M);
    5473             :   }
    5474       19450 :   if (DEBUGLEVEL>=4) timer_start(&ti);
    5475       19450 :   av = avma; h = RgM_Hadamard(M); /* |D| <= sqrt(h) */
    5476       19450 :   if (!signe(h)) { set_avma(av); return gen_0; }
    5477       19450 :   h = sqrti(h);
    5478       19450 :   if (lgefint(h) == 3 && (ulong)h[2] <= (PMAX >> 1))
    5479             :   { /* h < p/2 => direct result */
    5480        6766 :     p = PMAX;
    5481        6766 :     Dp = Flm_det_sp(ZM_to_Flm(M, p), p);
    5482        6766 :     set_avma(av);
    5483        6766 :     if (!Dp) return gen_0;
    5484        6766 :     return (Dp <= (p>>1))? utoipos(Dp): utoineg(p - Dp);
    5485             :   }
    5486       12684 :   q = gen_1; Dp = 1;
    5487       12684 :   init_modular_big(&S);
    5488       12684 :   p = 0; /* -Wall */
    5489       12684 :   while (cmpii(q, h) <= 0 && (p = u_forprime_next(&S)))
    5490             :   {
    5491       12684 :     av2 = avma; Dp = Flm_det_sp(ZM_to_Flm(M, p), p);
    5492       12684 :     set_avma(av2);
    5493       12684 :     if (Dp) break;
    5494           0 :     q = muliu(q, p);
    5495             :   }
    5496       12684 :   if (!p) pari_err_OVERFLOW("ZM_det [ran out of primes]");
    5497       12684 :   if (!Dp) { set_avma(av); return gen_0; }
    5498       12684 :   if (mt_nbthreads() > 1 || n <= DIXON_THRESHOLD)
    5499       12678 :     D = q; /* never competitive when bound is sharp even with 2 threads */
    5500             :   else
    5501             :   {
    5502           6 :     av2 = avma;
    5503           6 :     v = cgetg(n+1, t_COL);
    5504           6 :     gel(v, 1) = gen_1; /* ensure content(v) = 1 */
    5505         414 :     for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    5506           6 :     D = Q_denom(ZM_gauss(M, v));
    5507           6 :     if (expi(D) < expi(h) >> 1)
    5508             :     { /* First try unlucky, try once more */
    5509         414 :       for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    5510           6 :       D = lcmii(D, Q_denom(ZM_gauss(M, v)));
    5511             :     }
    5512           6 :     D = gerepileuptoint(av2, D);
    5513           6 :     if (q != gen_1) D = lcmii(D, q);
    5514             :   }
    5515       12684 :   if (DEBUGLEVEL >=4)
    5516           0 :     timer_printf(&ti,"ZM_det: Dixon %ld/%ld bits",expi(D),expi(h));
    5517             :   /* determinant is a multiple of D */
    5518       12684 :   if (is_pm1(D)) D = NULL;
    5519       12684 :   if (D) h = divii(h, D); /* not an exact division, just a bound */
    5520       12684 :   worker = snm_closure(is_entry("_ZM_det_worker"), mkvec(M));
    5521       12684 :   H = gen_crt("ZM_det", worker, &S, D, expi(h)+1, 0, &mod,
    5522             :               ZV_chinese, NULL);
    5523             :   /* H = det(M) modulo mod, (mod,D) = 1; |det(M) / D| <= h */
    5524       12684 :   if (D) H = Fp_div(H, D, mod);
    5525       12684 :   H = Fp_center(H, mod, shifti(mod,-1));
    5526       12684 :   if (D) H = mulii(H, D);
    5527       12684 :   return gerepileuptoint(av, H);
    5528             : }
    5529             : 
    5530             : static GEN
    5531        1519 : RgM_det_FpM(GEN a, GEN p)
    5532             : {
    5533        1519 :   pari_sp av = avma;
    5534             :   ulong pp, d;
    5535        1519 :   a = RgM_Fp_init(a,p,&pp);
    5536        1519 :   switch(pp)
    5537             :   {
    5538          70 :   case 0: return gerepileupto(av, Fp_to_mod(FpM_det(a,p),p)); break;
    5539          14 :   case 2: d = F2m_det_sp(a); break;
    5540        1435 :   default:d = Flm_det_sp(a, pp); break;
    5541             :   }
    5542        1449 :   set_avma(av); return mkintmodu(d, pp);
    5543             : }
    5544             : 
    5545             : static GEN
    5546          42 : RgM_det_FqM(GEN x, GEN pol, GEN p)
    5547             : {
    5548          42 :   pari_sp av = avma;
    5549          42 :   GEN b, T = RgX_to_FpX(pol, p);
    5550          42 :   if (signe(T) == 0) pari_err_OP("%",x,pol);
    5551          42 :   b = FqM_det(RgM_to_FqM(x, T, p), T, p);
    5552          42 :   if (!b) return gc_NULL(av);
    5553          42 :   return gerepilecopy(av, mkpolmod(FpX_to_mod(b, p), FpX_to_mod(T, p)));
    5554             : }
    5555             : 
    5556             : #define code(t1,t2) ((t1 << 6) | t2)
    5557             : static GEN
    5558       33592 : RgM_det_fast(GEN x)
    5559             : {
    5560             :   GEN p, pol;
    5561             :   long pa;
    5562       33592 :   long t = RgM_type(x, &p,&pol,&pa);
    5563       33592 :   switch(t)
    5564             :   {
    5565         196 :     case t_INT:    return ZM_det(x);
    5566         203 :     case t_FRAC:   return QM_det(x);
    5567          63 :     case t_FFELT:  return FFM_det(x, pol);
    5568        1519 :     case t_INTMOD: return RgM_det_FpM(x, p);
    5569          42 :     case code(t_POLMOD, t_INTMOD):
    5570          42 :                    return RgM_det_FqM(x, pol, p);
    5571       31569 :     default:       return NULL;
    5572             :   }
    5573             : }
    5574             : #undef code
    5575             : 
    5576             : static long
    5577         252 : det_init_max(long n)
    5578             : {
    5579         252 :   if (n > 100) return 0;
    5580         252 :   if (n > 50) return 1;
    5581         252 :   if (n > 30) return 4;
    5582         252 :   return 7;
    5583             : }
    5584             : 
    5585             : GEN
    5586      246654 : det(GEN a)
    5587             : {
    5588      246654 :   long n = lg(a)-1;
    5589             :   double B;
    5590             :   GEN data, b;
    5591             :   pivot_fun pivot;
    5592             : 
    5593      246654 :   if (typ(a)!=t_MAT) pari_err_TYPE("det",a);
    5594      246654 :   if (!n) return gen_1;
    5595      246612 :   if (n != nbrows(a)) pari_err_DIM("det");
    5596      246605 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    5597       68710 :   if (n == 2) return RgM_det2(a);
    5598       33592 :   b = RgM_det_fast(a);
    5599       33592 :   if (b) return b;
    5600       31569 :   pivot = get_pivot_fun(a, a, &data);
    5601       31569 :   if (pivot != gauss_get_pivot_NZ) return det_simple_gauss(a, data, pivot);
    5602         252 :   B = (double)n;
    5603         252 :   return det_develop(a, det_init_max(n), B*B*B);
    5604             : }
    5605             : 
    5606             : GEN
    5607         203 : QM_det(GEN M)
    5608             : {
    5609         203 :   pari_sp av = avma;
    5610         203 :   GEN cM, pM = Q_primitive_part(M, &cM);
    5611         203 :   GEN b = ZM_det(pM);
    5612         203 :   if (cM) b = gmul(b, gpowgs(cM, lg(M)-1));
    5613         203 :   return gerepileupto(av, b);
    5614             : }

Generated by: LCOV version 1.14