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.14.0 lcov report (development 27775-aca467eab2) Lines: 2742 3165 86.6 %
Date: 2022-07-03 07:33:15 Functions: 292 314 93.0 %
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        1571 : gen_ker(GEN x, long deplin, void *E, const struct bb_field *ff)
     124             : {
     125        1571 :   pari_sp av0 = avma, av, tetpil;
     126             :   GEN y, c, d;
     127             :   long i, j, k, r, t, n, m;
     128             : 
     129        1571 :   n=lg(x)-1; if (!n) return cgetg(1,t_MAT);
     130        1571 :   m=nbrows(x); r=0;
     131        1571 :   x = RgM_shallowcopy(x);
     132        1571 :   c = zero_zv(m);
     133        1571 :   d=new_chunk(n+1);
     134        1571 :   av=avma;
     135        5625 :   for (k=1; k<=n; k++)
     136             :   {
     137       11652 :     for (j=1; j<=m; j++)
     138        9906 :       if (!c[j])
     139             :       {
     140        6829 :         gcoeff(x,j,k) = ff->red(E, gcoeff(x,j,k));
     141        6829 :         if (!ff->equal0(gcoeff(x,j,k))) break;
     142             :       }
     143        4082 :     if (j>m)
     144             :     {
     145        1746 :       if (deplin)
     146             :       {
     147          28 :         GEN c = cgetg(n+1, t_COL), g0 = ff->s(E,0), g1=ff->s(E,1);
     148          63 :         for (i=1; i<k; i++) gel(c,i) = ff->red(E, gcoeff(x,d[i],k));
     149          28 :         gel(c,k) = g1; for (i=k+1; i<=n; i++) gel(c,i) = g0;
     150          28 :         return gerepileupto(av0, c);
     151             :       }
     152        1718 :       r++; d[k]=0;
     153        4239 :       for(j=1; j<k; j++)
     154        2521 :         if (d[j]) gcoeff(x,d[j],k) = gclone(gcoeff(x,d[j],k));
     155             :     }
     156             :     else
     157             :     {
     158        2336 :       GEN piv = ff->neg(E,ff->inv(E,gcoeff(x,j,k)));
     159        2336 :       c[j] = k; d[k] = j;
     160        2336 :       gcoeff(x,j,k) = ff->s(E,-1);
     161        5453 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = ff->red(E,ff->mul(E,piv,gcoeff(x,j,i)));
     162       11662 :       for (t=1; t<=m; t++)
     163             :       {
     164        9326 :         if (t==j) continue;
     165             : 
     166        6990 :         piv = ff->red(E,gcoeff(x,t,k));
     167        6990 :         if (ff->equal0(piv)) continue;
     168             : 
     169        2012 :         gcoeff(x,t,k) = ff->s(E,0);
     170        5019 :         for (i=k+1; i<=n; i++)
     171        3007 :            gcoeff(x,t,i) = ff->red(E, ff->add(E, gcoeff(x,t,i),
     172        3007 :                                       ff->mul(E,piv,gcoeff(x,j,i))));
     173        2012 :         if (gc_needed(av,1))
     174           0 :           gen_gerepile_gauss_ker(x,k,t,av,E,ff->red);
     175             :       }
     176             :     }
     177             :   }
     178        1543 :   if (deplin) return gc_NULL(av0);
     179             : 
     180        1515 :   tetpil=avma; y=cgetg(r+1,t_MAT);
     181        3233 :   for (j=k=1; j<=r; j++,k++)
     182             :   {
     183        1718 :     GEN C = cgetg(n+1,t_COL);
     184        1718 :     GEN g0 = ff->s(E,0), g1 = ff->s(E,1);
     185        3391 :     gel(y,j) = C; while (d[k]) k++;
     186        4239 :     for (i=1; i<k; i++)
     187        2521 :       if (d[i])
     188             :       {
     189        2111 :         GEN p1=gcoeff(x,d[i],k);
     190        2111 :         gel(C,i) = ff->red(E,p1); gunclone(p1);
     191             :       }
     192             :       else
     193         410 :         gel(C,i) = g0;
     194        2332 :     gel(C,k) = g1; for (i=k+1; i<=n; i++) gel(C,i) = g0;
     195             :   }
     196        1515 :   return gerepile(av0,tetpil,y);
     197             : }
     198             : 
     199             : GEN
     200        1556 : gen_Gauss_pivot(GEN x, long *rr, void *E, const struct bb_field *ff)
     201             : {
     202             :   pari_sp av;
     203             :   GEN c, d;
     204        1556 :   long i, j, k, r, t, m, n = lg(x)-1;
     205             : 
     206        1556 :   if (!n) { *rr = 0; return NULL; }
     207             : 
     208        1556 :   m=nbrows(x); r=0;
     209        1556 :   d = cgetg(n+1, t_VECSMALL);
     210        1556 :   x = RgM_shallowcopy(x);
     211        1556 :   c = zero_zv(m);
     212        1556 :   av=avma;
     213        5747 :   for (k=1; k<=n; k++)
     214             :   {
     215       10941 :     for (j=1; j<=m; j++)
     216       10648 :       if (!c[j])
     217             :       {
     218        7172 :         gcoeff(x,j,k) = ff->red(E,gcoeff(x,j,k));
     219        7172 :         if (!ff->equal0(gcoeff(x,j,k))) break;
     220             :       }
     221        4191 :     if (j>m) { r++; d[k]=0; }
     222             :     else
     223             :     {
     224        3898 :       GEN piv = ff->neg(E,ff->inv(E,gcoeff(x,j,k)));
     225        3898 :       GEN g0 = ff->s(E,0);
     226        3898 :       c[j] = k; d[k] = j;
     227        8116 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = ff->red(E,ff->mul(E,piv,gcoeff(x,j,i)));
     228       24208 :       for (t=1; t<=m; t++)
     229             :       {
     230       20310 :         if (c[t]) continue; /* already a pivot on that line */
     231             : 
     232       12507 :         piv = ff->red(E,gcoeff(x,t,k));
     233       12507 :         if (ff->equal0(piv)) continue;
     234        4947 :         gcoeff(x,t,k) = g0;
     235        8952 :         for (i=k+1; i<=n; i++)
     236        4005 :           gcoeff(x,t,i) = ff->red(E, ff->add(E,gcoeff(x,t,i), ff->mul(E,piv,gcoeff(x,j,i))));
     237        4947 :         if (gc_needed(av,1))
     238           0 :           gerepile_gauss(x,k,t,av,j,c);
     239             :       }
     240       12014 :       for (i=k; i<=n; i++) gcoeff(x,j,i) = g0; /* dummy */
     241             :     }
     242             :   }
     243        1556 :   *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      143984 : _gen_addmul(GEN b, long k, long i, GEN m, void *E, const struct bb_field *ff)
     292             : {
     293      143984 :   gel(b,i) = ff->red(E,gel(b,i));
     294      143984 :   gel(b,k) = ff->add(E,gel(b,k), ff->mul(E,m, gel(b,i)));
     295      143984 : }
     296             : 
     297             : static GEN
     298       54854 : _gen_get_col(GEN a, GEN b, long li, void *E, const struct bb_field *ff)
     299             : {
     300       54854 :   GEN u = cgetg(li+1,t_COL);
     301       54854 :   pari_sp av = avma;
     302             :   long i, j;
     303             : 
     304       54854 :   gel(u,li) = gerepileupto(av, ff->red(E,ff->mul(E,gel(b,li), gcoeff(a,li,li))));
     305      285179 :   for (i=li-1; i>0; i--)
     306             :   {
     307      230325 :     pari_sp av = avma;
     308      230325 :     GEN m = gel(b,i);
     309      921848 :     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      230325 :     m = ff->red(E, m);
     311      230325 :     gel(u,i) = gerepileupto(av, ff->red(E,ff->mul(E,m, gcoeff(a,i,i))));
     312             :   }
     313       54854 :   return u;
     314             : }
     315             : 
     316             : GEN
     317       12193 : gen_Gauss(GEN a, GEN b, void *E, const struct bb_field *ff)
     318             : {
     319             :   long i, j, k, li, bco, aco;
     320       12193 :   GEN u, g0 = ff->s(E,0);
     321       12193 :   pari_sp av = avma;
     322       12193 :   a = RgM_shallowcopy(a);
     323       12193 :   b = RgM_shallowcopy(b);
     324       12193 :   aco = lg(a)-1; bco = lg(b)-1; li = nbrows(a);
     325       53713 :   for (i=1; i<=aco; i++)
     326             :   {
     327             :     GEN invpiv;
     328       64675 :     for (k = i; k <= li; k++)
     329             :     {
     330       64619 :       GEN piv = ff->red(E,gcoeff(a,k,i));
     331       64619 :       if (!ff->equal0(piv)) { gcoeff(a,k,i) = ff->inv(E,piv); break; }
     332       10962 :       gcoeff(a,k,i) = g0;
     333             :     }
     334             :     /* found a pivot on line k */
     335       53713 :     if (k > li) return NULL;
     336       53657 :     if (k != i)
     337             :     { /* swap lines so that k = i */
     338       44212 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
     339       59304 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
     340             :     }
     341       53657 :     if (i == aco) break;
     342             : 
     343       41520 :     invpiv = gcoeff(a,i,i); /* 1/piv mod p */
     344      157435 :     for (k=i+1; k<=li; k++)
     345             :     {
     346      115915 :       GEN m = ff->red(E,gcoeff(a,k,i)); gcoeff(a,k,i) = g0;
     347      115915 :       if (ff->equal0(m)) continue;
     348             : 
     349       17697 :       m = ff->red(E,ff->neg(E,ff->mul(E,m, invpiv)));
     350       70921 :       for (j=i+1; j<=aco; j++) _gen_addmul(gel(a,j),k,i,m,E,ff);
     351      108457 :       for (j=1  ; j<=bco; j++) _gen_addmul(gel(b,j),k,i,m,E,ff);
     352             :     }
     353       41520 :     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       12137 :   if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
     361       12137 :   u = cgetg(bco+1,t_MAT);
     362       66991 :   for (j=1; j<=bco; j++) gel(u,j) = _gen_get_col(a, gel(b,j), aco, E, ff);
     363       12137 :   return u;
     364             : }
     365             : 
     366             : /* compatible t_MAT * t_COL, lgA = lg(A) = lg(B) > 1, l = lgcols(A) */
     367             : static GEN
     368      576823 : gen_matcolmul_i(GEN A, GEN B, ulong lgA, ulong l,
     369             :                 void *E, const struct bb_field *ff)
     370             : {
     371      576823 :   GEN C = cgetg(l, t_COL);
     372             :   ulong i;
     373     3855180 :   for (i = 1; i < l; i++) {
     374     3278357 :     pari_sp av = avma;
     375     3278357 :     GEN e = ff->mul(E, gcoeff(A, i, 1), gel(B, 1));
     376             :     ulong k;
     377    14093105 :     for(k = 2; k < lgA; k++)
     378    10814748 :       e = ff->add(E, e, ff->mul(E, gcoeff(A, i, k), gel(B, k)));
     379     3278357 :     gel(C, i) = gerepileupto(av, ff->red(E, e));
     380             :   }
     381      576823 :   return C;
     382             : }
     383             : 
     384             : GEN
     385      180173 : gen_matcolmul(GEN A, GEN B, void *E, const struct bb_field *ff)
     386             : {
     387      180173 :   ulong lgA = lg(A);
     388      180173 :   if (lgA != (ulong)lg(B))
     389           0 :     pari_err_OP("operation 'gen_matcolmul'", A, B);
     390      180173 :   if (lgA == 1)
     391           0 :     return cgetg(1, t_COL);
     392      180173 :   return gen_matcolmul_i(A, B, lgA, lgcols(A), E, ff);
     393             : }
     394             : 
     395             : static GEN
     396       76357 : 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       76357 :   GEN C = cgetg(lb, t_MAT);
     401      473007 :   for(j = 1; j < lb; j++)
     402      396650 :     gel(C, j) = gen_matcolmul_i(A, gel(B, j), la, l, E, ff);
     403       76357 :   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       76357 : gen_matmul_i(GEN A, GEN B, long l, long la, long lb,
     587             :              void *E, const struct bb_field *ff)
     588             : {
     589       76357 :   if (l <= gen_matmul_sw_bound
     590           7 :       || la <= gen_matmul_sw_bound
     591           0 :       || lb <= gen_matmul_sw_bound)
     592       76357 :     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       76357 : gen_matmul(GEN A, GEN B, void *E, const struct bb_field *ff)
     599             : {
     600       76357 :   ulong lgA, lgB = lg(B);
     601       76357 :   if (lgB == 1)
     602           0 :     return cgetg(1, t_MAT);
     603       76357 :   lgA = lg(A);
     604       76357 :   if (lgA != (ulong)lgcols(B))
     605           0 :     pari_err_OP("operation 'gen_matmul'", A, B);
     606       76357 :   if (lgA == 1)
     607           0 :     return zeromat(0, lgB - 1);
     608       76357 :   return gen_matmul_i(A, B, lgcols(A), lgA, lgB, E, ff);
     609             : }
     610             : 
     611             : static GEN
     612       18632 : gen_colneg(GEN A, void *E, const struct bb_field *ff)
     613             : {
     614             :   long i, l;
     615       18632 :   GEN B = cgetg_copy(A, &l);
     616       72593 :   for (i = 1; i < l; i++)
     617       53961 :     gel(B, i) = ff->neg(E, gel(A, i));
     618       18632 :   return B;
     619             : }
     620             : 
     621             : static GEN
     622        3958 : gen_matneg(GEN A, void *E, const struct bb_field *ff)
     623             : {
     624             :   long i, l;
     625        3958 :   GEN B = cgetg_copy(A, &l);
     626       22534 :   for (i = 1; i < l; i++)
     627       18576 :     gel(B, i) = gen_colneg(gel(A, i), E, ff);
     628        3958 :   return B;
     629             : }
     630             : 
     631             : static GEN
     632      284291 : gen_colscalmul(GEN A, GEN b, void *E, const struct bb_field *ff)
     633             : {
     634             :   long i, l;
     635      284291 :   GEN B = cgetg_copy(A, &l);
     636      681214 :   for (i = 1; i < l; i++)
     637      396923 :     gel(B, i) = ff->red(E, ff->mul(E, gel(A, i), b));
     638      284291 :   return B;
     639             : }
     640             : 
     641             : static GEN
     642       50970 : gen_matscalmul(GEN A, GEN b, void *E, const struct bb_field *ff)
     643             : {
     644             :   long i, l;
     645       50970 :   GEN B = cgetg_copy(A, &l);
     646      335261 :   for (i = 1; i < l; i++)
     647      284291 :     gel(B, i) = gen_colscalmul(gel(A, i), b, E, ff);
     648       50970 :   return B;
     649             : }
     650             : 
     651             : static GEN
     652      534315 : gen_colsub(GEN A, GEN C, void *E, const struct bb_field *ff)
     653             : {
     654             :   long i, l;
     655      534315 :   GEN B = cgetg_copy(A, &l);
     656     1948132 :   for (i = 1; i < l; i++)
     657     1413817 :     gel(B, i) = ff->add(E, gel(A, i), ff->neg(E, gel(C, i)));
     658      534315 :   return B;
     659             : }
     660             : 
     661             : static GEN
     662       73095 : gen_matsub(GEN A, GEN C, void *E, const struct bb_field *ff)
     663             : {
     664             :   long i, l;
     665       73095 :   GEN B = cgetg_copy(A, &l);
     666      607410 :   for (i = 1; i < l; i++)
     667      534315 :     gel(B, i) = gen_colsub(gel(A, i), gel(C, i), E, ff);
     668       73095 :   return B;
     669             : }
     670             : 
     671             : static GEN
     672       44332 : gen_zerocol(long n, void* data, const struct bb_field *R)
     673             : {
     674       44332 :   GEN C = cgetg(n+1,t_COL), zero = R->s(data, 0);
     675             :   long i;
     676      266059 :   for (i=1; i<=n; i++) gel(C,i) = zero;
     677       44332 :   return C;
     678             : }
     679             : 
     680             : static GEN
     681       14126 : gen_zeromat(long m, long n, void* data, const struct bb_field *R)
     682             : {
     683       14126 :   GEN M = cgetg(n+1,t_MAT);
     684             :   long i;
     685       58458 :   for (i=1; i<=n; i++) gel(M,i) = gen_zerocol(m, data, R);
     686       14126 :   return M;
     687             : }
     688             : 
     689             : static GEN
     690         140 : gen_colei(long n, long i, void *E, const struct bb_field *S)
     691             : {
     692         140 :   GEN y = cgetg(n+1,t_COL), _0, _1;
     693             :   long j;
     694         140 :   if (n < 0) pari_err_DOMAIN("gen_colei", "dimension","<",gen_0,stoi(n));
     695         140 :   _0 = S->s(E,0);
     696         140 :   _1 = S->s(E,1);
     697        2268 :   for (j=1; j<=n; j++)
     698        2128 :     gel(y, j) = i==j ? _1: _0;
     699         140 :   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      380826 : image_from_pivot(GEN x, GEN d, long r)
     785             : {
     786             :   GEN y;
     787             :   long j, k;
     788             : 
     789      380826 :   if (!d) return gcopy(x);
     790             :   /* d left on stack for efficiency */
     791      378054 :   r = lg(x)-1 - r; /* = dim Im(x) */
     792      378054 :   y = cgetg(r+1,t_MAT);
     793     2376748 :   for (j=k=1; j<=r; k++)
     794     1998694 :     if (d[k]) gel(y,j++) = gcopy(gel(x,k));
     795      378054 :   return y;
     796             : }
     797             : 
     798             : /* r = dim Ker x, n = nbrows(x) */
     799             : static GEN
     800      272631 : get_suppl(GEN x, GEN d, long n, long r, GEN(*ei)(long,long))
     801             : {
     802             :   pari_sp av;
     803             :   GEN y, c;
     804      272631 :   long j, k, rx = lg(x)-1; /* != 0 due to init_suppl() */
     805             : 
     806      272631 :   if (rx == n && r == 0) return gcopy(x);
     807      201662 :   y = cgetg(n+1, t_MAT);
     808      201665 :   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      916032 :   for (k = j = 1; j<=rx; j++)
     812      714364 :     if (d[j]) { c[ d[j] ] = 1; gel(y,k++) = gel(x,j); }
     813     1286816 :   for (j=1; j<=n; j++)
     814     1085148 :     if (!c[j]) gel(y,k++) = (GEN)j; /* HACK */
     815      201668 :   set_avma(av);
     816             : 
     817      201667 :   rx -= r;
     818      915959 :   for (j=1; j<=rx; j++) gel(y,j) = gcopy(gel(y,j));
     819      572521 :   for (   ; j<=n; j++)  gel(y,j) = ei(n, y[j]);
     820      201666 :   return y;
     821             : }
     822             : 
     823             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
     824             : static GEN
     825      189503 : indexrank0(long n, long r, GEN d)
     826             : {
     827      189503 :   GEN p1, p2, res = cgetg(3,t_VEC);
     828             :   long i, j;
     829             : 
     830      189503 :   r = n - r; /* now r = dim Im(x) */
     831      189503 :   p1 = cgetg(r+1,t_VECSMALL); gel(res,1) = p1;
     832      189503 :   p2 = cgetg(r+1,t_VECSMALL); gel(res,2) = p2;
     833      189503 :   if (d)
     834             :   {
     835     1058800 :     for (i=0,j=1; j<=n; j++)
     836      870662 :       if (d[j]) { i++; p1[i] = d[j]; p2[i] = j; }
     837      188138 :     vecsmall_sort(p1);
     838             :   }
     839      189503 :   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       17049 : indexcompl(GEN v, long n)
     867             : {
     868       17049 :   long i, j, k, m = lg(v) - 1;
     869       17049 :   GEN w = cgetg(n - m + 1, t_VECSMALL);
     870      157070 :   for (i = j = k = 1; i <= n; i++)
     871      140021 :     if (j <= m && v[j] == i) j++; else w[k++] = i;
     872       17049 :   return w;
     873             : }
     874             : 
     875             : static GEN
     876        3754 : gen_solve_upper_1(GEN U, GEN B, void *E, const struct bb_field *ff)
     877        3754 : { return gen_matscalmul(B, ff->inv(E, gcoeff(U, 1, 1)), E, ff); }
     878             : 
     879             : static GEN
     880        2074 : gen_rsolve_upper_2(GEN U, GEN B, void *E, const struct bb_field *ff)
     881             : {
     882        2074 :   GEN a = gcoeff(U, 1, 1), b = gcoeff(U, 1, 2), d = gcoeff(U, 2, 2);
     883        2074 :   GEN D = ff->red(E, ff->mul(E, a, d)), Dinv = ff->inv(E, D);
     884        2074 :   GEN ainv = ff->red(E, ff->mul(E, d, Dinv));
     885        2074 :   GEN dinv = ff->red(E, ff->mul(E, a, Dinv));
     886        2074 :   GEN B1 = rowslice(B, 1, 1);
     887        2074 :   GEN B2 = rowslice(B, 2, 2);
     888        2074 :   GEN X2 = gen_matscalmul(B2, dinv, E, ff);
     889        2074 :   GEN X1 = gen_matscalmul(gen_matsub(B1, gen_matscalmul(X2, b, E, ff), E, ff),
     890             :                           ainv, E, ff);
     891        2074 :   return vconcat(X1, X2);
     892             : }
     893             : 
     894             : /* solve U*X = B,  U upper triangular and invertible */
     895             : static GEN
     896        5399 : gen_rsolve_upper(GEN U, GEN B, void *E, const struct bb_field *ff,
     897             :                  GEN (*mul)(void *E, GEN a, GEN))
     898             : {
     899        5399 :   long n = lg(U) - 1, n1;
     900             :   GEN U2, U11, U12, U22, B1, B2, X1, X2, X;
     901        5399 :   pari_sp av = avma;
     902             : 
     903        5399 :   if (n == 0) return B;
     904        5399 :   if (n == 1) return gen_solve_upper_1(U, B, E, ff);
     905        4529 :   if (n == 2) return gen_rsolve_upper_2(U, B, E, ff);
     906        2455 :   n1 = (n + 1)/2;
     907        2455 :   U2 = vecslice(U, n1 + 1, n);
     908        2455 :   U11 = matslice(U, 1,n1, 1,n1);
     909        2455 :   U12 = rowslice(U2, 1, n1);
     910        2455 :   U22 = rowslice(U2, n1 + 1, n);
     911        2455 :   B1 = rowslice(B, 1, n1);
     912        2455 :   B2 = rowslice(B, n1 + 1, n);
     913        2455 :   X2 = gen_rsolve_upper(U22, B2, E, ff, mul);
     914        2455 :   B1 = gen_matsub(B1, mul(E, U12, X2), E, ff);
     915        2455 :   if (gc_needed(av, 1)) gerepileall(av, 3, &B1, &U11, &X2);
     916        2455 :   X1 = gen_rsolve_upper(U11, B1, E, ff, mul);
     917        2455 :   X = vconcat(X1, X2);
     918        2455 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     919        2455 :   return X;
     920             : }
     921             : 
     922             : static GEN
     923        5947 : gen_lsolve_upper_2(GEN U, GEN B, void *E, const struct bb_field *ff)
     924             : {
     925        5947 :   GEN a = gcoeff(U, 1, 1), b = gcoeff(U, 1, 2), d = gcoeff(U, 2, 2);
     926        5947 :   GEN D = ff->red(E, ff->mul(E, a, d)), Dinv = ff->inv(E, D);
     927        5947 :   GEN ainv = ff->red(E, ff->mul(E, d, Dinv)), dinv = ff->red(E, ff->mul(E, a, Dinv));
     928        5947 :   GEN B1 = vecslice(B, 1, 1);
     929        5947 :   GEN B2 = vecslice(B, 2, 2);
     930        5947 :   GEN X1 = gen_matscalmul(B1, ainv, E, ff);
     931        5947 :   GEN X2 = gen_matscalmul(gen_matsub(B2, gen_matscalmul(X1, b, E, ff), E, ff), dinv, E, ff);
     932        5947 :   return shallowconcat(X1, X2);
     933             : }
     934             : 
     935             : /* solve X*U = B,  U upper triangular and invertible */
     936             : static GEN
     937       13523 : gen_lsolve_upper(GEN U, GEN B, void *E, const struct bb_field *ff,
     938             :                  GEN (*mul)(void *E, GEN a, GEN))
     939             : {
     940       13523 :   long n = lg(U) - 1, n1;
     941             :   GEN U2, U11, U12, U22, B1, B2, X1, X2, X;
     942       13523 :   pari_sp av = avma;
     943             : 
     944       13523 :   if (n == 0) return B;
     945       13523 :   if (n == 1) return gen_solve_upper_1(U, B, E, ff);
     946       10639 :   if (n == 2) return gen_lsolve_upper_2(U, B, E, ff);
     947        4692 :   n1 = (n + 1)/2;
     948        4692 :   U2 = vecslice(U, n1 + 1, n);
     949        4692 :   U11 = matslice(U, 1,n1, 1,n1);
     950        4692 :   U12 = rowslice(U2, 1, n1);
     951        4692 :   U22 = rowslice(U2, n1 + 1, n);
     952        4692 :   B1 = vecslice(B, 1, n1);
     953        4692 :   B2 = vecslice(B, n1 + 1, n);
     954        4692 :   X1 = gen_lsolve_upper(U11, B1, E, ff, mul);
     955        4692 :   B2 = gen_matsub(B2, mul(E, X1, U12), E, ff);
     956        4692 :   if (gc_needed(av, 1)) gerepileall(av, 3, &B2, &U22, &X1);
     957        4692 :   X2 = gen_lsolve_upper(U22, B2, E, ff, mul);
     958        4692 :   X = shallowconcat(X1, X2);
     959        4692 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     960        4692 :   return X;
     961             : }
     962             : 
     963             : static GEN
     964       15688 : gen_rsolve_lower_unit_2(GEN L, GEN A, void *E, const struct bb_field *ff)
     965             : {
     966       15688 :   GEN X1 = rowslice(A, 1, 1);
     967       15688 :   GEN X2 = gen_matsub(rowslice(A, 2, 2), gen_matscalmul(X1, gcoeff(L, 2, 1), E, ff), E, ff);
     968       15688 :   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       36476 : 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       36476 :   long m = lg(L) - 1, m1, n;
     978             :   GEN L1, L11, L21, L22, A1, A2, X1, X2, X;
     979       36476 :   pari_sp av = avma;
     980             : 
     981       36476 :   if (m == 0) return zeromat(0, lg(A) - 1);
     982       36476 :   if (m == 1) return rowslice(A, 1, 1);
     983       28662 :   if (m == 2) return gen_rsolve_lower_unit_2(L, A, E, ff);
     984       12974 :   m1 = (m + 1)/2;
     985       12974 :   n = nbrows(L);
     986       12974 :   L1 = vecslice(L, 1, m1);
     987       12974 :   L11 = rowslice(L1, 1, m1);
     988       12974 :   L21 = rowslice(L1, m1 + 1, n);
     989       12974 :   A1 = rowslice(A, 1, m1);
     990       12974 :   X1 = gen_rsolve_lower_unit(L11, A1, E, ff, mul);
     991       12974 :   A2 = rowslice(A, m1 + 1, n);
     992       12974 :   A2 = gen_matsub(A2, mul(E, L21, X1), E, ff);
     993       12974 :   if (gc_needed(av, 1)) gerepileall(av, 2, &A2, &X1);
     994       12974 :   L22 = matslice(L, m1+1,n, m1+1,m);
     995       12974 :   X2 = gen_rsolve_lower_unit(L22, A2, E, ff, mul);
     996       12974 :   X = vconcat(X1, X2);
     997       12974 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
     998       12974 :   return X;
     999             : }
    1000             : 
    1001             : static GEN
    1002        7465 : gen_lsolve_lower_unit_2(GEN L, GEN A, void *E, const struct bb_field *ff)
    1003             : {
    1004        7465 :   GEN X2 = vecslice(A, 2, 2);
    1005        7465 :   GEN X1 = gen_matsub(vecslice(A, 1, 1),
    1006        7465 :                     gen_matscalmul(X2, gcoeff(L, 2, 1), E, ff), E, ff);
    1007        7465 :   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       19265 : 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       19265 :   long m = lg(L) - 1, m1;
    1017             :   GEN L1, L2, L11, L21, L22, A1, A2, X1, X2, X;
    1018       19265 :   pari_sp av = avma;
    1019             : 
    1020       19265 :   if (m <= 1) return A;
    1021       15129 :   if (m == 2) return gen_lsolve_lower_unit_2(L, A, E, ff);
    1022        7664 :   m1 = (m + 1)/2;
    1023        7664 :   L2 = vecslice(L, m1 + 1, m);
    1024        7664 :   L22 = rowslice(L2, m1 + 1, m);
    1025        7664 :   A2 = vecslice(A, m1 + 1, m);
    1026        7664 :   X2 = gen_lsolve_lower_unit(L22, A2, E, ff, mul);
    1027        7664 :   if (gc_needed(av, 1)) X2 = gerepilecopy(av, X2);
    1028        7664 :   L1 = vecslice(L, 1, m1);
    1029        7664 :   L21 = rowslice(L1, m1 + 1, m);
    1030        7664 :   A1 = vecslice(A, 1, m1);
    1031        7664 :   A1 = gen_matsub(A1, mul(E, X2, L21), E, ff);
    1032        7664 :   L11 = rowslice(L1, 1, m1);
    1033        7664 :   if (gc_needed(av, 1)) gerepileall(av, 3, &A1, &L11, &X2);
    1034        7664 :   X1 = gen_lsolve_lower_unit(L11, A1, E, ff, mul);
    1035        7664 :   X = shallowconcat(X1, X2);
    1036        7664 :   if (gc_needed(av, 1)) X = gerepilecopy(av, X);
    1037        7664 :   return X;
    1038             : }
    1039             : 
    1040             : /* destroy A */
    1041             : static long
    1042       21339 : gen_CUP_basecase(GEN A, GEN *R, GEN *C, GEN *U, GEN *P, void *E, const struct bb_field *ff)
    1043             : {
    1044       21339 :   long i, j, k, m = nbrows(A), n = lg(A) - 1, pr, pc;
    1045             :   pari_sp av;
    1046             :   GEN u, v;
    1047             : 
    1048       21339 :   if (P) *P = identity_perm(n);
    1049       21339 :   *R = cgetg(m + 1, t_VECSMALL);
    1050       21339 :   av = avma;
    1051       55654 :   for (j = 1, pr = 0; j <= n; j++)
    1052             :   {
    1053      126079 :     for (pr++, pc = 0; pr <= m; pr++)
    1054             :     {
    1055      604655 :       for (k = j; k <= n; k++)
    1056             :       {
    1057      495244 :         v = ff->red(E, gcoeff(A, pr, k));
    1058      495244 :         gcoeff(A, pr, k) = v;
    1059      495244 :         if (!pc && !ff->equal0(v)) pc = k;
    1060             :       }
    1061      109411 :       if (pc) break;
    1062             :     }
    1063       50983 :     if (!pc) break;
    1064       34315 :     (*R)[j] = pr;
    1065       34315 :     if (pc != j)
    1066             :     {
    1067        4559 :       swap(gel(A, j), gel(A, pc));
    1068        4559 :       if (P) lswap((*P)[j], (*P)[pc]);
    1069             :     }
    1070       34315 :     u = ff->inv(E, gcoeff(A, pr, j));
    1071      170309 :     for (i = pr + 1; i <= m; i++)
    1072             :     {
    1073      135994 :       v = ff->red(E, ff->mul(E, gcoeff(A, i, j), u));
    1074      135994 :       gcoeff(A, i, j) = v;
    1075      135994 :       v = ff->neg(E, v);
    1076      434171 :       for (k = j + 1; k <= n; k++)
    1077      298177 :         gcoeff(A, i, k) = ff->add(E, gcoeff(A, i, k),
    1078      298177 :                                   ff->red(E, ff->mul(E, gcoeff(A, pr, k), v)));
    1079             :     }
    1080       34315 :     if (gc_needed(av, 2)) A = gerepilecopy(av, A);
    1081             :   }
    1082       21339 :   setlg(*R, j);
    1083       21339 :   *C = vecslice(A, 1, j - 1);
    1084       21339 :   if (U) *U = rowpermute(A, *R);
    1085       21339 :   return j - 1;
    1086             : }
    1087             : 
    1088             : static const long gen_CUP_LIMIT = 5;
    1089             : 
    1090             : static long
    1091       10979 : 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       10979 :   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       10979 :   pari_sp av = avma;
    1098             : 
    1099       10979 :   if (m < gen_CUP_LIMIT || n < gen_CUP_LIMIT)
    1100             :     /* destroy A; not called at the outermost recursion level */
    1101        6284 :     return gen_CUP_basecase(A, R, C, U, P, E, ff);
    1102        4695 :   m1 = (minss(m, n) + 1)/2;
    1103        4695 :   A1 = rowslice(A, 1, m1);
    1104        4695 :   A2 = rowslice(A, m1 + 1, m);
    1105        4695 :   r1 = gen_CUP(A1, &R1, &C1, &U1, &P1, E, ff, mul);
    1106        4695 :   if (r1 == 0)
    1107             :   {
    1108         556 :     r2 = gen_CUP(A2, &R2, &C2, &U2, &P2, E, ff, mul);
    1109         556 :     *R = cgetg(r2 + 1, t_VECSMALL);
    1110         859 :     for (i = 1; i <= r2; i++) (*R)[i] = R2[i] + m1;
    1111         556 :     *C = vconcat(gen_zeromat(m1, r2, E, ff), C2);
    1112         556 :     *U = U2;
    1113         556 :     *P = P2;
    1114         556 :     r = r2;
    1115             :   }
    1116             :   else
    1117             :   {
    1118        4139 :     U11 = vecslice(U1, 1, r1);
    1119        4139 :     U12 = vecslice(U1, r1 + 1, n);
    1120        4139 :     T21 = vecslicepermute(A2, P1, 1, r1);
    1121        4139 :     T22 = vecslicepermute(A2, P1, r1 + 1, n);
    1122        4139 :     C21 = gen_lsolve_upper(U11, T21, E, ff, mul);
    1123        4139 :     if (gc_needed(av, 1))
    1124           0 :       gerepileall(av, 7, &R1, &C1, &P1, &U11, &U12, &T22, &C21);
    1125        4139 :     B2 = gen_matsub(T22, mul(E, C21, U12), E, ff);
    1126        4139 :     r2 = gen_CUP(B2, &R2, &C2, &U2, &P2, E, ff, mul);
    1127        4139 :     r = r1 + r2;
    1128        4139 :     *R = cgetg(r + 1, t_VECSMALL);
    1129       18917 :     for (i = 1; i <= r1; i++) (*R)[i] = R1[i];
    1130       19981 :     for (     ; i <= r; i++)  (*R)[i] = R2[i - r1] + m1;
    1131        4139 :     *C = shallowconcat(vconcat(C1, C21),
    1132             :                        vconcat(gen_zeromat(m1, r2, E, ff), C2));
    1133        4139 :     *U = shallowconcat(vconcat(U11, gen_zeromat(r2, r1, E, ff)),
    1134             :                        vconcat(vecpermute(U12, P2), U2));
    1135             : 
    1136        4139 :     *P = cgetg(n + 1, t_VECSMALL);
    1137       18917 :     for (i = 1; i <= r1; i++) (*P)[i] = P1[i];
    1138       46740 :     for (     ; i <= n; i++)  (*P)[i] = P1[P2[i - r1] + r1];
    1139             :   }
    1140        4695 :   if (gc_needed(av, 1)) gerepileall(av, 4, R, C, U, P);
    1141        4695 :   return r;
    1142             : }
    1143             : 
    1144             : /* column echelon form */
    1145             : static long
    1146       26328 : gen_echelon(GEN A, GEN *R, GEN *C, void *E, const struct bb_field *ff,
    1147             :             GEN (*mul)(void*, GEN, GEN))
    1148             : {
    1149       26328 :   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       26328 :   pari_sp av = avma;
    1153             : 
    1154       26328 :   if (m < gen_CUP_LIMIT || n < gen_CUP_LIMIT)
    1155       15055 :     return gen_CUP_basecase(shallowcopy(A), R, C, NULL, NULL, E, ff);
    1156             : 
    1157       11273 :   n1 = (n + 1)/2;
    1158       11273 :   A1 = vecslice(A, 1, n1);
    1159       11273 :   A2 = vecslice(A, n1 + 1, n);
    1160       11273 :   r1 = gen_echelon(A1, &R1, &C1, E, ff, mul);
    1161       11273 :   if (!r1) return gen_echelon(A2, R, C, E, ff, mul);
    1162       10208 :   if (r1 == m) { *R = R1; *C = C1; return r1; }
    1163        9997 :   R1c = indexcompl(R1, m);
    1164        9997 :   C11 = rowpermute(C1, R1);
    1165        9997 :   C21 = rowpermute(C1, R1c);
    1166        9997 :   A12 = rowpermute(A2, R1);
    1167        9997 :   A22 = rowpermute(A2, R1c);
    1168        9997 :   M12 = gen_rsolve_lower_unit(C11, A12, E, ff, mul);
    1169        9997 :   B2 = gen_matsub(A22, mul(E, C21, M12), E, ff);
    1170        9997 :   r2 = gen_echelon(B2, &R2, &C2, E, ff, mul);
    1171        9997 :   if (!r2) { *R = R1; *C = C1; r = r1; }
    1172             :   else
    1173             :   {
    1174        5271 :     R2 = perm_mul(R1c, R2);
    1175        5271 :     C2 = rowpermute(vconcat(gen_zeromat(r1, r2, E, ff), C2),
    1176             :                     perm_inv(vecsmall_concat(R1, R1c)));
    1177        5271 :     r = r1 + r2;
    1178        5271 :     *R = cgetg(r + 1, t_VECSMALL);
    1179        5271 :     *C = cgetg(r + 1, t_MAT);
    1180       36921 :     for (j = j1 = j2 = 1; j <= r; j++)
    1181       31650 :       if (j2 > r2 || (j1 <= r1 && R1[j1] < R2[j2]))
    1182             :       {
    1183       18493 :         gel(*C, j) = gel(C1, j1);
    1184       18493 :         (*R)[j] = R1[j1++];
    1185             :       }
    1186             :       else
    1187             :       {
    1188       13157 :         gel(*C, j) = gel(C2, j2);
    1189       13157 :         (*R)[j] = R2[j2++];
    1190             :       }
    1191             :   }
    1192        9997 :   if (gc_needed(av, 1)) gerepileall(av, 2, R, C);
    1193        9997 :   return r;
    1194             : }
    1195             : 
    1196             : static GEN
    1197         862 : 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         862 :   long i, n = lg(x) - 1, r;
    1202         862 :   GEN R, C, U, P, d = zero_zv(n);
    1203         862 :   av = avma;
    1204         862 :   r = gen_CUP(x, &R, &C, &U, &P, E, ff, mul);
    1205        6543 :   for(i = 1; i <= r; i++)
    1206        5681 :     d[P[i]] = R[i];
    1207         862 :   set_avma(av);
    1208         862 :   *rr = n - r;
    1209         862 :   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        3881 : gen_ker_echelon(GEN x, void *E, const struct bb_field *ff,
    1268             :                 GEN (*mul)(void*, GEN, GEN))
    1269             : {
    1270        3881 :   pari_sp av = avma;
    1271             :   GEN R, Rc, C, C1, C2, S, K;
    1272        3881 :   long n = lg(x) - 1, r;
    1273        3881 :   r = gen_echelon(shallowtrans(x), &R, &C, E, ff, mul);
    1274        3881 :   Rc = indexcompl(R, n);
    1275        3881 :   C1 = rowpermute(C, R);
    1276        3881 :   C2 = rowpermute(C, Rc);
    1277        3881 :   S = gen_lsolve_lower_unit(C1, C2, E, ff, mul);
    1278        3881 :   K = vecpermute(shallowconcat(gen_matneg(S, E, ff), gen_matid(n - r, E, ff)),
    1279             :                  perm_inv(vecsmall_concat(R, Rc)));
    1280        3881 :   K = shallowtrans(K);
    1281        3881 :   return gerepilecopy(av, K);
    1282             : }
    1283             : 
    1284             : static GEN
    1285          84 : gen_deplin_echelon(GEN x, void *E, const struct bb_field *ff,
    1286             :                    GEN (*mul)(void*, GEN, GEN))
    1287             : {
    1288          84 :   pari_sp av = avma;
    1289             :   GEN R, Rc, C, C1, C2, s, v;
    1290          84 :   long i, n = lg(x) - 1, r;
    1291          84 :   r = gen_echelon(shallowtrans(x), &R, &C, E, ff, mul);
    1292          84 :   if (r == n) return gc_NULL(av);
    1293          56 :   Rc = indexcompl(R, n);
    1294          56 :   i = Rc[1];
    1295          56 :   C1 = rowpermute(C, R);
    1296          56 :   C2 = rowslice(C, i, i);
    1297          56 :   s = row(gen_lsolve_lower_unit(C1, C2, E, ff, mul), 1);
    1298          56 :   settyp(s, t_COL);
    1299          56 :   v = vecpermute(shallowconcat(gen_colneg(s, E, ff), gen_colei(n - r, 1, E, ff)),
    1300             :                  perm_inv(vecsmall_concat(R, Rc)));
    1301          56 :   return gerepilecopy(av, v);
    1302             : }
    1303             : 
    1304             : static GEN
    1305         524 : 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         524 :   long n = lg(a) - 1, r;
    1310         524 :   if (nbrows(a) < n || (r = gen_CUP(a, &R, &C, &U, &P, E, ff, mul)) < n)
    1311          56 :     return NULL;
    1312         468 :   Y = gen_rsolve_lower_unit(rowpermute(C, R), rowpermute(b, R), E, ff, mul);
    1313         468 :   return rowpermute(gen_rsolve_upper(U, Y, E, ff, mul), perm_inv(P));
    1314             : }
    1315             : 
    1316             : static GEN
    1317        3869 : gen_gauss(GEN a, GEN b, void *E, const struct bb_field *ff,
    1318             :           GEN (*mul)(void*, GEN, GEN))
    1319             : {
    1320        3869 :   if (lg(a) - 1 >= gen_CUP_LIMIT)
    1321         524 :     return gen_gauss_CUP(a, b, E, ff, mul);
    1322        3345 :   return gen_Gauss(a, b, E, ff);
    1323             : }
    1324             : 
    1325             : static GEN
    1326        5459 : gen_ker_i(GEN x, long deplin, void *E, const struct bb_field *ff,
    1327             :           GEN (*mul)(void*, GEN, GEN)) {
    1328        5459 :   if (lg(x) - 1 >= gen_CUP_LIMIT && nbrows(x) >= gen_CUP_LIMIT)
    1329        3965 :     return deplin? gen_deplin_echelon(x, E, ff, mul): gen_ker_echelon(x, E, ff, mul);
    1330        1494 :   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          70 : gen_matcolinvimage_i(GEN A, GEN y, void *E, const struct bb_field *ff,
    1348             :                      GEN (*mul)(void*, GEN, GEN))
    1349             : {
    1350          70 :   pari_sp av = avma;
    1351          70 :   long i, l = lg(A);
    1352             :   GEN M, x, t;
    1353             : 
    1354          70 :   M = gen_ker_i(shallowconcat(A, y), 0, E, ff, mul);
    1355          70 :   i = lg(M) - 1;
    1356          70 :   if (!i) return gc_NULL(av);
    1357             : 
    1358          70 :   x = gel(M, i);
    1359          70 :   t = gel(x, l);
    1360          70 :   if (ff->equal0(t)) return gc_NULL(av);
    1361             : 
    1362          49 :   t = ff->neg(E, ff->inv(E, t));
    1363          49 :   setlg(x, l);
    1364         175 :   for (i = 1; i < l; i++)
    1365         126 :     gel(x, i) = ff->red(E, ff->mul(E, t, gel(x, i)));
    1366          49 :   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        2411 : gen_pivots(GEN x, long *rr, void *E, const struct bb_field *ff,
    1381             :            GEN (*mul)(void*, GEN, GEN))
    1382             : {
    1383        2411 :   if (lg(x) - 1 >= gen_CUP_LIMIT && nbrows(x) >= gen_CUP_LIMIT)
    1384         862 :     return gen_pivots_CUP(x, rr, E, ff, mul);
    1385        1549 :   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          49 : FqM_FqC_mul(GEN A, GEN B, GEN T, GEN p) {
    1441             :   void *E;
    1442          49 :   const struct bb_field *ff = get_Fq_field(&E, T, p);
    1443          49 :   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      146106 : FlxqM_mul(GEN A, GEN B, GEN T, ulong p) {
    1455             :   void *E;
    1456             :   const struct bb_field *ff;
    1457      146106 :   long n = lg(A) - 1;
    1458             : 
    1459      146106 :   if (n == 0)
    1460           0 :     return cgetg(1, t_MAT);
    1461      146106 :   if (n > 1)
    1462       83385 :     return FlxqM_mul_Kronecker(A, B, T, p);
    1463       62721 :   ff = get_Flxq_field(&E, T, p);
    1464       62721 :   return gen_matmul(A, B, E, ff);
    1465             : }
    1466             : 
    1467             : GEN
    1468       66423 : FqM_mul(GEN A, GEN B, GEN T, GEN p) {
    1469             :   void *E;
    1470       66423 :   long n = lg(A) - 1;
    1471             :   const struct bb_field *ff;
    1472       66423 :   if (n == 0)
    1473           0 :     return cgetg(1, t_MAT);
    1474       66423 :   if (n > 1)
    1475       62804 :     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       16422 : _FlxqM_mul(void *E, GEN A, GEN B)
    1498             : {
    1499       16422 :   struct _Flxq *D = (struct _Flxq*)E;
    1500       16422 :   return FlxqM_mul(A, B, D->T, D->p);
    1501             : }
    1502             : 
    1503             : static GEN
    1504       19213 : _FpM_mul(void *E, GEN A, GEN B)
    1505       19213 : { 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     1301758 : FpM_init(GEN a, GEN p, ulong *pp)
    1521             : {
    1522     1301758 :   if (lgefint(p) == 3)
    1523             :   {
    1524     1297019 :     *pp = uel(p,2);
    1525     1297019 :     return (*pp==2)? ZM_to_F2m(a): ZM_to_Flm(a, *pp);
    1526             :   }
    1527        4739 :   *pp = 0; return a;
    1528             : }
    1529             : static GEN
    1530     1288778 : FpM_init3(GEN a, GEN p, ulong *pp)
    1531             : {
    1532     1288778 :   if (lgefint(p) == 3)
    1533             :   {
    1534     1286547 :     *pp = uel(p,2);
    1535     1286547 :     switch(*pp)
    1536             :     {
    1537      711854 :       case 2: return ZM_to_F2m(a);
    1538      161354 :       case 3: return ZM_to_F3m(a);
    1539      413339 :       default:return ZM_to_Flm(a, *pp);
    1540             :     }
    1541             :   }
    1542        2231 :   *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 :   set_avma(av); return utoi(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         857 : FpM_gauss_pivot_gen(GEN x, GEN p, long *rr)
    1617             : {
    1618             :   void *E;
    1619         857 :   const struct bb_field *S = get_Fp_field(&E,p);
    1620         857 :   return gen_pivots(x, rr, E, S, _FpM_mul);
    1621             : }
    1622             : 
    1623             : static GEN
    1624      642269 : FpM_gauss_pivot(GEN x, GEN p, long *rr)
    1625             : {
    1626             :   ulong pp;
    1627      642269 :   if (lg(x)==1) { *rr = 0; return NULL; }
    1628      640148 :   x = FpM_init(x, p, &pp);
    1629      640158 :   switch(pp)
    1630             :   {
    1631         857 :   case 0: return FpM_gauss_pivot_gen(x, p, rr);
    1632      354695 :   case 2: return F2m_gauss_pivot(x, rr);
    1633      284606 :   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        1407 : FlxqM_gauss_pivot(GEN x, GEN T, ulong p, long *rr) {
    1647             :   void *E;
    1648        1407 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    1649        1407 :   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        1484 : FqM_gauss_pivot(GEN x, GEN T, GEN p, long *rr)
    1661             : {
    1662        1484 :   if (lg(x)==1) { *rr = 0; return NULL; }
    1663        1484 :   if (!T) return FpM_gauss_pivot(x, p, rr);
    1664        1484 :   if (lgefint(p) == 3)
    1665             :   {
    1666        1379 :     pari_sp av = avma;
    1667        1379 :     ulong pp = uel(p,2);
    1668        1379 :     GEN Tp = ZXT_to_FlxT(T, pp);
    1669        1379 :     GEN d = FlxqM_gauss_pivot(ZXM_to_FlxM(x, pp, get_Flx_var(Tp)), Tp, pp, rr);
    1670        1379 :     return d ? gerepileuptoleaf(av, d): d;
    1671             :   }
    1672         105 :   return FqM_gauss_pivot_gen(x, T, p, rr);
    1673             : }
    1674             : 
    1675             : GEN
    1676      341342 : FpM_image(GEN x, GEN p)
    1677             : {
    1678             :   long r;
    1679      341342 :   GEN d = FpM_gauss_pivot(x,p,&r); /* d left on stack for efficiency */
    1680      341339 :   return image_from_pivot(x,d,r);
    1681             : }
    1682             : 
    1683             : GEN
    1684       39390 : Flm_image(GEN x, ulong p)
    1685             : {
    1686             :   long r;
    1687       39390 :   GEN d = Flm_pivots(x, p, &r, 0); /* d left on stack for efficiency */
    1688       39390 :   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           7 : FpM_FpC_invimage_gen(GEN A, GEN y, GEN p)
    1813             : {
    1814             :   void *E;
    1815           7 :   const struct bb_field *ff = get_Fp_field(&E, p);
    1816           7 :   return gen_matcolinvimage_i(A, y, E, ff, _FpM_mul);
    1817             : }
    1818             : 
    1819             : GEN
    1820      317554 : FpM_FpC_invimage(GEN A, GEN x, GEN p)
    1821             : {
    1822      317554 :   pari_sp av = avma;
    1823             :   ulong pp;
    1824             :   GEN y;
    1825             : 
    1826      317554 :   A = FpM_init(A, p, &pp);
    1827      317563 :   switch(pp)
    1828             :   {
    1829           7 :   case 0: return FpM_FpC_invimage_gen(A, x, p);
    1830      197048 :   case 2:
    1831      197048 :     y = F2m_F2c_invimage(A, ZV_to_F2v(x));
    1832      197049 :     if (!y) return y;
    1833      197049 :     y = F2c_to_ZC(y);
    1834      197047 :     return gerepileupto(av, y);
    1835      120508 :   default:
    1836      120508 :     y = Flm_Flc_invimage(A, ZV_to_Flv(x, pp), pp);
    1837      120508 :     if (!y) return y;
    1838      120508 :     y = Flc_to_ZC(y);
    1839      120508 :     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        2302 : FpM_ker_gen(GEN x, GEN p, long deplin)
    1866             : {
    1867             :   void *E;
    1868        2302 :   const struct bb_field *S = get_Fp_field(&E,p);
    1869        2302 :   return gen_ker_i(x, deplin, E, S, _FpM_mul);
    1870             : }
    1871             : static GEN
    1872     1288776 : FpM_ker_i(GEN x, GEN p, long deplin)
    1873             : {
    1874     1288776 :   pari_sp av = avma;
    1875             :   ulong pp;
    1876             :   GEN y;
    1877             : 
    1878     1288776 :   if (lg(x)==1) return cgetg(1,t_MAT);
    1879     1288776 :   x = FpM_init3(x, p, &pp);
    1880     1288801 :   switch(pp)
    1881             :   {
    1882        2232 :   case 0: return FpM_ker_gen(x,p,deplin);
    1883      711870 :   case 2:
    1884      711870 :     y = F2m_ker_sp(x, deplin);
    1885      711882 :     if (!y) return gc_NULL(av);
    1886      711891 :     y = deplin? F2c_to_ZC(y): F2m_to_ZM(y);
    1887      711885 :     return gerepileupto(av, y);
    1888      161354 :   case 3:
    1889      161354 :     y = F3m_ker_sp(x, deplin);
    1890      161354 :     if (!y) return gc_NULL(av);
    1891      161354 :     y = deplin? F3c_to_ZC(y): F3m_to_ZM(y);
    1892      161354 :     return gerepileupto(av, y);
    1893      413345 :   default:
    1894      413345 :     y = Flm_ker_sp(x, pp, deplin);
    1895      413345 :     if (!y) return gc_NULL(av);
    1896      413345 :     y = deplin? Flc_to_ZC(y): Flm_to_ZM(y);
    1897      413345 :     return gerepileupto(av, y);
    1898             :   }
    1899             : }
    1900             : 
    1901             : GEN
    1902      797274 : 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        2926 : FlxqM_ker_i(GEN x, GEN T, ulong p, long deplin) {
    1923             :   void *E;
    1924        2926 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    1925        2926 :   return gen_ker_i(x, deplin, E, S, _FlxqM_mul);
    1926             : }
    1927             : 
    1928             : GEN
    1929        2891 : FlxqM_ker(GEN x, GEN T, ulong p)
    1930             : {
    1931        2891 :   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        9037 : FqM_ker_i(GEN x, GEN T, GEN p, long deplin)
    1943             : {
    1944        9037 :   if (!T) return FpM_ker_i(x,p,deplin);
    1945        2989 :   if (lg(x)==1) return cgetg(1,t_MAT);
    1946             : 
    1947        2989 :   if (lgefint(p)==3)
    1948             :   {
    1949        2863 :     pari_sp ltop=avma;
    1950        2863 :     ulong l= p[2];
    1951        2863 :     GEN Tl = ZXT_to_FlxT(T,l);
    1952        2863 :     GEN Ml = ZXM_to_FlxM(x, l, get_Flx_var(Tl));
    1953        2863 :     GEN p1 = FlxM_to_ZXM(FlxqM_ker(Ml,Tl,l));
    1954        2863 :     return gerepileupto(ltop,p1);
    1955             :   }
    1956         126 :   return FqM_ker_gen(x, T, p, deplin);
    1957             : }
    1958             : 
    1959             : GEN
    1960        8960 : FqM_ker(GEN x, GEN T, GEN p) { return FqM_ker_i(x,T,p,0); }
    1961             : 
    1962             : GEN
    1963      485473 : FpM_deplin(GEN x, GEN p) { return FpM_ker_i(x,p,1); }
    1964             : 
    1965             : GEN
    1966          14 : F2xqM_deplin(GEN x, GEN T)
    1967             : {
    1968          14 :   return F2xqM_ker_i(x, T, 1);
    1969             : }
    1970             : 
    1971             : GEN
    1972          35 : FlxqM_deplin(GEN x, GEN T, ulong p)
    1973             : {
    1974          35 :   return FlxqM_ker_i(x, T, p, 1);
    1975             : }
    1976             : 
    1977             : GEN
    1978          77 : FqM_deplin(GEN x, GEN T, GEN p) { return FqM_ker_i(x,T,p,1); }
    1979             : 
    1980             : static GEN
    1981        3561 : FpM_gauss_gen(GEN a, GEN b, GEN p)
    1982             : {
    1983             :   void *E;
    1984        3561 :   const struct bb_field *S = get_Fp_field(&E,p);
    1985        3561 :   return gen_gauss(a,b, E, S, _FpM_mul);
    1986             : }
    1987             : /* a an FpM, lg(a)>1; b an FpM or NULL (replace by identity) */
    1988             : static GEN
    1989      340156 : FpM_gauss_i(GEN a, GEN b, GEN p, ulong *pp)
    1990             : {
    1991      340156 :   long n = nbrows(a);
    1992      340157 :   a = FpM_init(a,p,pp);
    1993      340158 :   switch(*pp)
    1994             :   {
    1995        3561 :   case 0:
    1996        3561 :     if (!b) b = matid(n);
    1997        3561 :     return FpM_gauss_gen(a,b,p);
    1998      227307 :   case 2:
    1999      227307 :     if (b) b = ZM_to_F2m(b); else b = matid_F2m(n);
    2000      227310 :     return F2m_gauss_sp(a,b);
    2001      109290 :   default:
    2002      109290 :     if (b) b = ZM_to_Flm(b, *pp); else b = matid_Flm(n);
    2003      109290 :     return Flm_gauss_sp(a,b, NULL, *pp);
    2004             :   }
    2005             : }
    2006             : GEN
    2007          35 : FpM_gauss(GEN a, GEN b, GEN p)
    2008             : {
    2009          35 :   pari_sp av = avma;
    2010             :   ulong pp;
    2011             :   GEN u;
    2012          35 :   if (lg(a) == 1 || lg(b)==1) return cgetg(1, t_MAT);
    2013          35 :   u = FpM_gauss_i(a, b, p, &pp);
    2014          35 :   if (!u) return gc_NULL(av);
    2015          28 :   switch(pp)
    2016             :   {
    2017          28 :   case 0: return gerepilecopy(av, u);
    2018           0 :   case 2:  u = F2m_to_ZM(u); break;
    2019           0 :   default: u = Flm_to_ZM(u); break;
    2020             :   }
    2021           0 :   return gerepileupto(av, u);
    2022             : }
    2023             : 
    2024             : static GEN
    2025          84 : F2xqM_gauss_gen(GEN a, GEN b, GEN T)
    2026             : {
    2027             :   void *E;
    2028          84 :   const struct bb_field *S = get_F2xq_field(&E, T);
    2029          84 :   return gen_gauss(a, b, E, S, _F2xqM_mul);
    2030             : }
    2031             : 
    2032             : GEN
    2033          21 : F2xqM_gauss(GEN a, GEN b, GEN T)
    2034             : {
    2035          21 :   pari_sp av = avma;
    2036          21 :   long n = lg(a)-1;
    2037             :   GEN u;
    2038          21 :   if (!n || lg(b)==1) { set_avma(av); return cgetg(1, t_MAT); }
    2039          21 :   u = F2xqM_gauss_gen(a, b, T);
    2040          21 :   if (!u) return gc_NULL(av);
    2041          14 :   return gerepilecopy(av, u);
    2042             : }
    2043             : 
    2044             : static GEN
    2045          91 : FlxqM_gauss_i(GEN a, GEN b, GEN T, ulong p) {
    2046             :   void *E;
    2047          91 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    2048          91 :   return gen_gauss(a, b, E, S, _FlxqM_mul);
    2049             : }
    2050             : 
    2051             : GEN
    2052          21 : FlxqM_gauss(GEN a, GEN b, GEN T, ulong p)
    2053             : {
    2054          21 :   pari_sp av = avma;
    2055          21 :   long n = lg(a)-1;
    2056             :   GEN u;
    2057          21 :   if (!n || lg(b)==1) { set_avma(av); return cgetg(1, t_MAT); }
    2058          21 :   u = FlxqM_gauss_i(a, b, T, p);
    2059          21 :   if (!u) return gc_NULL(av);
    2060          14 :   return gerepilecopy(av, u);
    2061             : }
    2062             : 
    2063             : static GEN
    2064         133 : FqM_gauss_gen(GEN a, GEN b, GEN T, GEN p)
    2065             : {
    2066             :   void *E;
    2067         133 :   const struct bb_field *S = get_Fq_field(&E,T,p);
    2068         133 :   return gen_gauss(a,b,E,S,_FqM_mul);
    2069             : }
    2070             : GEN
    2071          21 : FqM_gauss(GEN a, GEN b, GEN T, GEN p)
    2072             : {
    2073          21 :   pari_sp av = avma;
    2074             :   GEN u;
    2075             :   long n;
    2076          21 :   if (!T) return FpM_gauss(a,b,p);
    2077          21 :   n = lg(a)-1; if (!n || lg(b)==1) return cgetg(1, t_MAT);
    2078          21 :   u = FqM_gauss_gen(a,b,T,p);
    2079          21 :   if (!u) return gc_NULL(av);
    2080          14 :   return gerepilecopy(av, u);
    2081             : }
    2082             : 
    2083             : GEN
    2084          14 : FpM_FpC_gauss(GEN a, GEN b, GEN p)
    2085             : {
    2086          14 :   pari_sp av = avma;
    2087             :   ulong pp;
    2088             :   GEN u;
    2089          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    2090          14 :   u = FpM_gauss_i(a, mkmat(b), p, &pp);
    2091          14 :   if (!u) return gc_NULL(av);
    2092          14 :   switch(pp)
    2093             :   {
    2094          14 :   case 0: return gerepilecopy(av, gel(u,1));
    2095           0 :   case 2:  u = F2c_to_ZC(gel(u,1)); break;
    2096           0 :   default: u = Flc_to_ZC(gel(u,1)); break;
    2097             :   }
    2098           0 :   return gerepileupto(av, u);
    2099             : }
    2100             : 
    2101             : GEN
    2102          28 : F2xqM_F2xqC_gauss(GEN a, GEN b, GEN T)
    2103             : {
    2104          28 :   pari_sp av = avma;
    2105             :   GEN u;
    2106          28 :   if (lg(a) == 1) return cgetg(1, t_COL);
    2107          28 :   u = F2xqM_gauss_gen(a, mkmat(b), T);
    2108          28 :   if (!u) return gc_NULL(av);
    2109          14 :   return gerepilecopy(av, gel(u,1));
    2110             : }
    2111             : 
    2112             : GEN
    2113          14 : FlxqM_FlxqC_gauss(GEN a, GEN b, GEN T, ulong p)
    2114             : {
    2115          14 :   pari_sp av = avma;
    2116             :   GEN u;
    2117          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    2118          14 :   u = FlxqM_gauss_i(a, mkmat(b), T, p);
    2119          14 :   if (!u) return gc_NULL(av);
    2120           7 :   return gerepilecopy(av, gel(u,1));
    2121             : }
    2122             : 
    2123             : GEN
    2124          14 : FqM_FqC_gauss(GEN a, GEN b, GEN T, GEN p)
    2125             : {
    2126          14 :   pari_sp av = avma;
    2127             :   GEN u;
    2128          14 :   if (!T) return FpM_FpC_gauss(a,b,p);
    2129          14 :   if (lg(a) == 1) return cgetg(1, t_COL);
    2130          14 :   u = FqM_gauss_gen(a,mkmat(b),T,p);
    2131          14 :   if (!u) return gc_NULL(av);
    2132           7 :   return gerepilecopy(av, gel(u,1));
    2133             : }
    2134             : 
    2135             : GEN
    2136      340107 : FpM_inv(GEN a, GEN p)
    2137             : {
    2138      340107 :   pari_sp av = avma;
    2139             :   ulong pp;
    2140             :   GEN u;
    2141      340107 :   if (lg(a) == 1) return cgetg(1, t_MAT);
    2142      340107 :   u = FpM_gauss_i(a, NULL, p, &pp);
    2143      340109 :   if (!u) return gc_NULL(av);
    2144      340093 :   switch(pp)
    2145             :   {
    2146        3505 :   case 0: return gerepilecopy(av, u);
    2147      227297 :   case 2:  u = F2m_to_ZM(u); break;
    2148      109291 :   default: u = Flm_to_ZM(u); break;
    2149             :   }
    2150      336586 :   return gerepileupto(av, u);
    2151             : }
    2152             : 
    2153             : GEN
    2154          35 : F2xqM_inv(GEN a, GEN T)
    2155             : {
    2156          35 :   pari_sp av = avma;
    2157             :   GEN u;
    2158          35 :   if (lg(a) == 1) { set_avma(av); return cgetg(1, t_MAT); }
    2159          35 :   u = F2xqM_gauss_gen(a, matid_F2xqM(nbrows(a),T), T);
    2160          35 :   if (!u) return gc_NULL(av);
    2161          28 :   return gerepilecopy(av, u);
    2162             : }
    2163             : 
    2164             : GEN
    2165          56 : FlxqM_inv(GEN a, GEN T, ulong p)
    2166             : {
    2167          56 :   pari_sp av = avma;
    2168             :   GEN u;
    2169          56 :   if (lg(a) == 1) { set_avma(av); return cgetg(1, t_MAT); }
    2170          56 :   u = FlxqM_gauss_i(a, matid_FlxqM(nbrows(a),T,p), T,p);
    2171          56 :   if (!u) return gc_NULL(av);
    2172          42 :   return gerepilecopy(av, u);
    2173             : }
    2174             : 
    2175             : GEN
    2176          98 : FqM_inv(GEN a, GEN T, GEN p)
    2177             : {
    2178          98 :   pari_sp av = avma;
    2179             :   GEN u;
    2180          98 :   if (!T) return FpM_inv(a,p);
    2181          98 :   if (lg(a) == 1) return cgetg(1, t_MAT);
    2182          98 :   u = FqM_gauss_gen(a,matid(nbrows(a)),T,p);
    2183          98 :   if (!u) return gc_NULL(av);
    2184          70 :   return gerepilecopy(av, u);
    2185             : }
    2186             : 
    2187             : GEN
    2188      353909 : FpM_intersect_i(GEN x, GEN y, GEN p)
    2189             : {
    2190      353909 :   long j, lx = lg(x);
    2191             :   GEN z;
    2192             : 
    2193      353909 :   if (lx == 1 || lg(y) == 1) return cgetg(1,t_MAT);
    2194      353909 :   if (lgefint(p) == 3)
    2195             :   {
    2196      353909 :     ulong pp = p[2];
    2197      353909 :     return Flm_to_ZM(Flm_intersect_i(ZM_to_Flm(x,pp), ZM_to_Flm(y,pp), pp));
    2198             :   }
    2199           0 :   z = FpM_ker(shallowconcat(x,y), p);
    2200           0 :   for (j=lg(z)-1; j; j--) setlg(gel(z,j),lx);
    2201           0 :   return FpM_mul(x,z,p);
    2202             : }
    2203             : GEN
    2204           0 : FpM_intersect(GEN x, GEN y, GEN p)
    2205             : {
    2206           0 :   pari_sp av = avma;
    2207             :   GEN z;
    2208           0 :   if (lgefint(p) == 3)
    2209             :   {
    2210           0 :     ulong pp = p[2];
    2211           0 :     z = Flm_image(Flm_intersect_i(ZM_to_Flm(x,pp), ZM_to_Flm(y,pp), pp), pp);
    2212             :   }
    2213             :   else
    2214           0 :     z = FpM_image(FpM_intersect_i(x,y,p), p);
    2215           0 :   return gerepileupto(av, z);
    2216             : }
    2217             : 
    2218             : static void
    2219      272632 : init_suppl(GEN x)
    2220             : {
    2221      272632 :   if (lg(x) == 1) pari_err_IMPL("suppl [empty matrix]");
    2222             :   /* HACK: avoid overwriting d from gauss_pivot() after set_avma(av) */
    2223      272632 :   (void)new_chunk(lgcols(x) * 2);
    2224      272632 : }
    2225             : 
    2226             : GEN
    2227      271085 : FpM_suppl(GEN x, GEN p)
    2228             : {
    2229             :   GEN d;
    2230             :   long r;
    2231      271085 :   init_suppl(x); d = FpM_gauss_pivot(x,p, &r);
    2232      271088 :   return get_suppl(x,d,nbrows(x),r,&col_ei);
    2233             : }
    2234             : 
    2235             : GEN
    2236          14 : F2m_suppl(GEN x)
    2237             : {
    2238             :   GEN d;
    2239             :   long r;
    2240          14 :   init_suppl(x); d = F2m_gauss_pivot(F2m_copy(x), &r);
    2241          14 :   return get_suppl(x,d,mael(x,1,1),r,&F2v_ei);
    2242             : }
    2243             : 
    2244             : GEN
    2245         105 : Flm_suppl(GEN x, ulong p)
    2246             : {
    2247             :   GEN d;
    2248             :   long r;
    2249         105 :   init_suppl(x); d = Flm_pivots(x, p, &r, 0);
    2250         105 :   return get_suppl(x,d,nbrows(x),r,&vecsmall_ei);
    2251             : }
    2252             : 
    2253             : GEN
    2254           7 : F2xqM_suppl(GEN x, GEN T)
    2255             : {
    2256             :   void *E;
    2257           7 :   const struct bb_field *S = get_F2xq_field(&E, T);
    2258           7 :   return gen_suppl(x, E, S, _F2xqM_mul);
    2259             : }
    2260             : 
    2261             : GEN
    2262          14 : FlxqM_suppl(GEN x, GEN T, ulong p)
    2263             : {
    2264             :   void *E;
    2265          14 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    2266          14 :   return gen_suppl(x, E, S, _FlxqM_mul);
    2267             : }
    2268             : 
    2269             : GEN
    2270        4305 : FqM_suppl(GEN x, GEN T, GEN p)
    2271             : {
    2272        4305 :   pari_sp av = avma;
    2273             :   GEN d;
    2274             :   long r;
    2275             : 
    2276        4305 :   if (!T) return FpM_suppl(x,p);
    2277        1358 :   init_suppl(x);
    2278        1358 :   d = FqM_gauss_pivot(x,T,p,&r);
    2279        1358 :   set_avma(av); return get_suppl(x,d,nbrows(x),r,&col_ei);
    2280             : }
    2281             : 
    2282             : static void
    2283      173343 : init_indexrank(GEN x) {
    2284      173343 :   (void)new_chunk(3 + 2*lg(x)); /* HACK */
    2285      173342 : }
    2286             : 
    2287             : GEN
    2288       29792 : FpM_indexrank(GEN x, GEN p) {
    2289       29792 :   pari_sp av = avma;
    2290             :   long r;
    2291             :   GEN d;
    2292       29792 :   init_indexrank(x);
    2293       29792 :   d = FpM_gauss_pivot(x,p,&r);
    2294       29792 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    2295             : }
    2296             : 
    2297             : GEN
    2298       52877 : Flm_indexrank(GEN x, ulong p) {
    2299       52877 :   pari_sp av = avma;
    2300             :   long r;
    2301             :   GEN d;
    2302       52877 :   init_indexrank(x);
    2303       52876 :   d = Flm_pivots(x, p, &r, 0);
    2304       52877 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    2305             : }
    2306             : 
    2307             : GEN
    2308          60 : F2m_indexrank(GEN x) {
    2309          60 :   pari_sp av = avma;
    2310             :   long r;
    2311             :   GEN d;
    2312          60 :   init_indexrank(x);
    2313          60 :   d = F2m_gauss_pivot(F2m_copy(x),&r);
    2314          60 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    2315             : }
    2316             : 
    2317             : GEN
    2318           7 : F2xqM_indexrank(GEN x, GEN T) {
    2319           7 :   pari_sp av = avma;
    2320             :   long r;
    2321             :   GEN d;
    2322           7 :   init_indexrank(x);
    2323           7 :   d = F2xqM_gauss_pivot(x, T, &r);
    2324           7 :   set_avma(av); return indexrank0(lg(x) - 1, r, d);
    2325             : }
    2326             : 
    2327             : GEN
    2328           7 : FlxqM_indexrank(GEN x, GEN T, ulong p) {
    2329           7 :   pari_sp av = avma;
    2330             :   long r;
    2331             :   GEN d;
    2332           7 :   init_indexrank(x);
    2333           7 :   d = FlxqM_gauss_pivot(x, T, p, &r);
    2334           7 :   set_avma(av); return indexrank0(lg(x) - 1, r, d);
    2335             : }
    2336             : 
    2337             : GEN
    2338           7 : FqM_indexrank(GEN x, GEN T, GEN p) {
    2339           7 :   pari_sp av = avma;
    2340             :   long r;
    2341             :   GEN d;
    2342           7 :   init_indexrank(x);
    2343           7 :   d = FqM_gauss_pivot(x, T, p, &r);
    2344           7 :   set_avma(av); return indexrank0(lg(x) - 1, r, d);
    2345             : }
    2346             : 
    2347             : /*******************************************************************/
    2348             : /*                                                                 */
    2349             : /*                       Solve A*X=B (Gauss pivot)                 */
    2350             : /*                                                                 */
    2351             : /*******************************************************************/
    2352             : /* x a column, x0 same column in the original input matrix (for reference),
    2353             :  * c list of pivots so far */
    2354             : static long
    2355     1892342 : gauss_get_pivot_max(GEN X, GEN X0, long ix, GEN c)
    2356             : {
    2357     1892342 :   GEN p, r, x = gel(X,ix), x0 = gel(X0,ix);
    2358     1892342 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2359     1892342 :   if (c)
    2360             :   {
    2361      320274 :     for (i=1; i<lx; i++)
    2362      184004 :       if (!c[i])
    2363             :       {
    2364       88238 :         long e = gexpo(gel(x,i));
    2365       88238 :         if (e > ex) { ex = e; k = i; }
    2366             :       }
    2367             :   }
    2368             :   else
    2369             :   {
    2370     6362085 :     for (i=ix; i<lx; i++)
    2371             :     {
    2372     4605998 :       long e = gexpo(gel(x,i));
    2373     4606013 :       if (e > ex) { ex = e; k = i; }
    2374             :     }
    2375             :   }
    2376     1892357 :   if (!k) return lx;
    2377     1823933 :   p = gel(x,k);
    2378     1823933 :   r = gel(x0,k); if (isrationalzero(r)) r = x0;
    2379     1823937 :   return cx_approx0(p, r)? lx: k;
    2380             : }
    2381             : static long
    2382      253239 : gauss_get_pivot_padic(GEN X, GEN p, long ix, GEN c)
    2383             : {
    2384      253239 :   GEN x = gel(X, ix);
    2385      253239 :   long i, k = 0, ex = (long)HIGHVALPBIT, lx = lg(x);
    2386      253239 :   if (c)
    2387             :   {
    2388         504 :     for (i=1; i<lx; i++)
    2389         378 :       if (!c[i] && !gequal0(gel(x,i)))
    2390             :       {
    2391         245 :         long e = gvaluation(gel(x,i), p);
    2392         245 :         if (e < ex) { ex = e; k = i; }
    2393             :       }
    2394             :   }
    2395             :   else
    2396             :   {
    2397     2312744 :     for (i=ix; i<lx; i++)
    2398     2059631 :       if (!gequal0(gel(x,i)))
    2399             :       {
    2400     1557682 :         long e = gvaluation(gel(x,i), p);
    2401     1557682 :         if (e < ex) { ex = e; k = i; }
    2402             :       }
    2403             :   }
    2404      253239 :   return k? k: lx;
    2405             : }
    2406             : static long
    2407        4235 : gauss_get_pivot_NZ(GEN X, GEN x0/*unused*/, long ix, GEN c)
    2408             : {
    2409        4235 :   GEN x = gel(X, ix);
    2410        4235 :   long i, lx = lg(x);
    2411             :   (void)x0;
    2412        4235 :   if (c)
    2413             :   {
    2414       11872 :     for (i=1; i<lx; i++)
    2415       11004 :       if (!c[i] && !gequal0(gel(x,i))) return i;
    2416             :   }
    2417             :   else
    2418             :   {
    2419        2380 :     for (i=ix; i<lx; i++)
    2420        2366 :       if (!gequal0(gel(x,i))) return i;
    2421             :   }
    2422         882 :   return lx;
    2423             : }
    2424             : 
    2425             : /* Return pivot seeking function appropriate for the domain of the RgM x
    2426             :  * (first non zero pivot, maximal pivot...)
    2427             :  * x0 is a reference point used when guessing whether x[i,j] ~ 0
    2428             :  * (iff x[i,j] << x0[i,j]); typical case: mateigen, Gauss pivot on x - vp.Id,
    2429             :  * but use original x when deciding whether a prospective pivot is nonzero */
    2430             : static pivot_fun
    2431      594831 : get_pivot_fun(GEN x, GEN x0, GEN *data)
    2432             : {
    2433      594831 :   long i, j, hx, lx = lg(x);
    2434      594831 :   int res = t_INT;
    2435      594831 :   GEN p = NULL;
    2436             : 
    2437      594831 :   *data = NULL;
    2438      594831 :   if (lx == 1) return &gauss_get_pivot_NZ;
    2439      594796 :   hx = lgcols(x);
    2440     2808466 :   for (j=1; j<lx; j++)
    2441             :   {
    2442     2213711 :     GEN xj = gel(x,j);
    2443    13817790 :     for (i=1; i<hx; i++)
    2444             :     {
    2445    11604121 :       GEN c = gel(xj,i);
    2446    11604121 :       switch(typ(c))
    2447             :       {
    2448     5672908 :         case t_REAL:
    2449     5672908 :           res = t_REAL;
    2450     5672908 :           break;
    2451         364 :         case t_COMPLEX:
    2452         364 :           if (typ(gel(c,1)) == t_REAL || typ(gel(c,2)) == t_REAL) res = t_REAL;
    2453         364 :           break;
    2454     4385158 :         case t_INT: case t_INTMOD: case t_FRAC: case t_FFELT: case t_QUAD:
    2455             :         case t_POLMOD: /* exact types */
    2456     4385158 :           break;
    2457     1545649 :         case t_PADIC:
    2458     1545649 :           p = gel(c,2);
    2459     1545649 :           res = t_PADIC;
    2460     1545649 :           break;
    2461          42 :         default: return &gauss_get_pivot_NZ;
    2462             :       }
    2463             :     }
    2464             :   }
    2465      594755 :   switch(res)
    2466             :   {
    2467      563994 :     case t_REAL: *data = x0; return &gauss_get_pivot_max;
    2468       29477 :     case t_PADIC: *data = p; return &gauss_get_pivot_padic;
    2469        1284 :     default: return &gauss_get_pivot_NZ;
    2470             :   }
    2471             : }
    2472             : 
    2473             : static GEN
    2474      660112 : get_col(GEN a, GEN b, GEN p, long li)
    2475             : {
    2476      660112 :   GEN u = cgetg(li+1,t_COL);
    2477             :   long i, j;
    2478             : 
    2479      660112 :   gel(u,li) = gdiv(gel(b,li), p);
    2480     2918001 :   for (i=li-1; i>0; i--)
    2481             :   {
    2482     2257895 :     pari_sp av = avma;
    2483     2257895 :     GEN m = gel(b,i);
    2484    10659594 :     for (j=i+1; j<=li; j++) m = gsub(m, gmul(gcoeff(a,i,j), gel(u,j)));
    2485     2257880 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(a,i,i)));
    2486             :   }
    2487      660106 :   return u;
    2488             : }
    2489             : 
    2490             : /* bk -= m * bi */
    2491             : static void
    2492    10684424 : _submul(GEN b, long k, long i, GEN m)
    2493             : {
    2494    10684424 :   gel(b,k) = gsub(gel(b,k), gmul(m, gel(b,i)));
    2495    10684391 : }
    2496             : static int
    2497     1422384 : init_gauss(GEN a, GEN *b, long *aco, long *li, int *iscol)
    2498             : {
    2499     1422384 :   *iscol = *b ? (typ(*b) == t_COL): 0;
    2500     1422384 :   *aco = lg(a) - 1;
    2501     1422384 :   if (!*aco) /* a empty */
    2502             :   {
    2503          70 :     if (*b && lg(*b) != 1) pari_err_DIM("gauss");
    2504          70 :     *li = 0; return 0;
    2505             :   }
    2506     1422314 :   *li = nbrows(a);
    2507     1422314 :   if (*li < *aco) pari_err_INV("gauss [no left inverse]", a);
    2508     1422314 :   if (*b)
    2509             :   {
    2510     1343503 :     switch(typ(*b))
    2511             :     {
    2512      121300 :       case t_MAT:
    2513      121300 :         if (lg(*b) == 1) return 0;
    2514      121300 :         *b = RgM_shallowcopy(*b);
    2515      121300 :         break;
    2516     1222205 :       case t_COL:
    2517     1222205 :         *b = mkmat( leafcopy(*b) );
    2518     1222204 :         break;
    2519           0 :       default: pari_err_TYPE("gauss",*b);
    2520             :     }
    2521     1343504 :     if (nbrows(*b) != *li) pari_err_DIM("gauss");
    2522             :   }
    2523             :   else
    2524       78811 :     *b = matid(*li);
    2525     1422311 :   return 1;
    2526             : }
    2527             : 
    2528             : static GEN
    2529         112 : RgM_inv_FpM(GEN a, GEN p)
    2530             : {
    2531             :   ulong pp;
    2532         112 :   a = RgM_Fp_init(a, p, &pp);
    2533         112 :   switch(pp)
    2534             :   {
    2535          35 :   case 0:
    2536          35 :     a = FpM_inv(a,p);
    2537          35 :     if (a) a = FpM_to_mod(a, p);
    2538          35 :     break;
    2539          35 :   case 2:
    2540          35 :     a = F2m_inv(a);
    2541          35 :     if (a) a = F2m_to_mod(a);
    2542          35 :     break;
    2543          42 :   default:
    2544          42 :     a = Flm_inv_sp(a, NULL, pp);
    2545          42 :     if (a) a = Flm_to_mod(a, pp);
    2546             :   }
    2547         112 :   return a;
    2548             : }
    2549             : 
    2550             : static GEN
    2551          42 : RgM_inv_FqM(GEN x, GEN pol, GEN p)
    2552             : {
    2553          42 :   pari_sp av = avma;
    2554          42 :   GEN b, T = RgX_to_FpX(pol, p);
    2555          42 :   if (signe(T) == 0) pari_err_OP("^",x,gen_m1);
    2556          42 :   b = FqM_inv(RgM_to_FqM(x, T, p), T, p);
    2557          42 :   if (!b) return gc_NULL(av);
    2558          28 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    2559             : }
    2560             : 
    2561             : #define code(t1,t2) ((t1 << 6) | t2)
    2562             : static GEN
    2563      201059 : RgM_inv_fast(GEN x)
    2564             : {
    2565             :   GEN p, pol;
    2566             :   long pa;
    2567      201059 :   long t = RgM_type(x, &p,&pol,&pa);
    2568      201060 :   switch(t)
    2569             :   {
    2570       45583 :     case t_INT:    /* Fall back */
    2571       45583 :     case t_FRAC:   return QM_inv(x);
    2572         147 :     case t_FFELT:  return FFM_inv(x, pol);
    2573         112 :     case t_INTMOD: return RgM_inv_FpM(x, p);
    2574          42 :     case code(t_POLMOD, t_INTMOD):
    2575          42 :                    return RgM_inv_FqM(x, pol, p);
    2576      155176 :     default:       return gen_0;
    2577             :   }
    2578             : }
    2579             : #undef code
    2580             : 
    2581             : static GEN
    2582          49 : RgM_RgC_solve_FpC(GEN a, GEN b, GEN p)
    2583             : {
    2584          49 :   pari_sp av = avma;
    2585             :   ulong pp;
    2586          49 :   a = RgM_Fp_init(a, p, &pp);
    2587          49 :   switch(pp)
    2588             :   {
    2589          14 :   case 0:
    2590          14 :     b = RgC_to_FpC(b, p);
    2591          14 :     a = FpM_FpC_gauss(a,b,p);
    2592          14 :     return a ? gerepileupto(av, FpC_to_mod(a, p)): NULL;
    2593          14 :   case 2:
    2594          14 :     b = RgV_to_F2v(b);
    2595          14 :     a = F2m_F2c_gauss(a,b);
    2596          14 :     return a ? gerepileupto(av, F2c_to_mod(a)): NULL;
    2597          21 :   default:
    2598          21 :     b = RgV_to_Flv(b, pp);
    2599          21 :     a = Flm_Flc_gauss(a, b, pp);
    2600          21 :     return a ? gerepileupto(av, Flc_to_mod(a, pp)): NULL;
    2601             :   }
    2602             : }
    2603             : 
    2604             : static GEN
    2605          98 : RgM_solve_FpM(GEN a, GEN b, GEN p)
    2606             : {
    2607          98 :   pari_sp av = avma;
    2608             :   ulong pp;
    2609          98 :   a = RgM_Fp_init(a, p, &pp);
    2610          98 :   switch(pp)
    2611             :   {
    2612          35 :   case 0:
    2613          35 :     b = RgM_to_FpM(b, p);
    2614          35 :     a = FpM_gauss(a,b,p);
    2615          35 :     return a ? gerepileupto(av, FpM_to_mod(a, p)): NULL;
    2616          21 :   case 2:
    2617          21 :     b = RgM_to_F2m(b);
    2618          21 :     a = F2m_gauss(a,b);
    2619          21 :     return a ? gerepileupto(av, F2m_to_mod(a)): NULL;
    2620          42 :   default:
    2621          42 :     b = RgM_to_Flm(b, pp);
    2622          42 :     a = Flm_gauss(a,b,pp);
    2623          42 :     return a ? gerepileupto(av, Flm_to_mod(a, pp)): NULL;
    2624             :   }
    2625             : }
    2626             : 
    2627             : /* Gaussan Elimination. If a is square, return a^(-1)*b;
    2628             :  * if a has more rows than columns and b is NULL, return c such that c a = Id.
    2629             :  * a is a (not necessarily square) matrix
    2630             :  * b is a matrix or column vector, NULL meaning: take the identity matrix,
    2631             :  *   effectively returning the inverse of a
    2632             :  * If a and b are empty, the result is the empty matrix.
    2633             :  *
    2634             :  * li: number of rows of a and b
    2635             :  * aco: number of columns of a
    2636             :  * bco: number of columns of b (if matrix)
    2637             :  */
    2638             : static GEN
    2639      760407 : RgM_solve_basecase(GEN a, GEN b)
    2640             : {
    2641      760407 :   pari_sp av = avma;
    2642             :   long i, j, k, li, bco, aco;
    2643             :   int iscol;
    2644             :   pivot_fun pivot;
    2645             :   GEN p, u, data;
    2646             : 
    2647      760407 :   set_avma(av);
    2648             : 
    2649      760407 :   if (lg(a)-1 == 2 && nbrows(a) == 2) {
    2650             :     /* 2x2 matrix, start by inverting a */
    2651      288670 :     GEN u = gcoeff(a,1,1), v = gcoeff(a,1,2);
    2652      288670 :     GEN w = gcoeff(a,2,1), x = gcoeff(a,2,2);
    2653      288670 :     GEN D = gsub(gmul(u,x), gmul(v,w)), ainv;
    2654      288661 :     if (gequal0(D)) return NULL;
    2655      288660 :     ainv = mkmat2(mkcol2(x, gneg(w)), mkcol2(gneg(v), u));
    2656      288666 :     ainv = gmul(ainv, ginv(D));
    2657      288659 :     if (b) ainv = gmul(ainv, b);
    2658      288663 :     return gerepileupto(av, ainv);
    2659             :   }
    2660             : 
    2661      471737 :   if (!init_gauss(a, &b, &aco, &li, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    2662      471737 :   pivot = get_pivot_fun(a, a, &data);
    2663      471737 :   a = RgM_shallowcopy(a);
    2664      471736 :   bco = lg(b)-1;
    2665      471736 :   if(DEBUGLEVEL>4) err_printf("Entering gauss\n");
    2666             : 
    2667      471736 :   p = NULL; /* gcc -Wall */
    2668     1669149 :   for (i=1; i<=aco; i++)
    2669             :   {
    2670             :     /* k is the line where we find the pivot */
    2671     1669139 :     k = pivot(a, data, i, NULL);
    2672     1669156 :     if (k > li) return NULL;
    2673     1669129 :     if (k != i)
    2674             :     { /* exchange the lines s.t. k = i */
    2675     1349124 :       for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    2676     1010796 :       for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    2677             :     }
    2678     1669129 :     p = gcoeff(a,i,i);
    2679     1669129 :     if (i == aco) break;
    2680             : 
    2681     3859828 :     for (k=i+1; k<=li; k++)
    2682             :     {
    2683     2662423 :       GEN m = gcoeff(a,k,i);
    2684     2662423 :       if (!gequal0(m))
    2685             :       {
    2686     2026829 :         m = gdiv(m,p);
    2687     8384789 :         for (j=i+1; j<=aco; j++) _submul(gel(a,j),k,i,m);
    2688     6353533 :         for (j=1;   j<=bco; j++) _submul(gel(b,j),k,i,m);
    2689             :       }
    2690             :     }
    2691     1197405 :     if (gc_needed(av,1))
    2692             :     {
    2693          12 :       if(DEBUGMEM>1) pari_warn(warnmem,"gauss. i=%ld",i);
    2694          12 :       gerepileall(av,2, &a,&b);
    2695             :     }
    2696             :   }
    2697             : 
    2698      471720 :   if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
    2699      471720 :   u = cgetg(bco+1,t_MAT);
    2700     1131815 :   for (j=1; j<=bco; j++) gel(u,j) = get_col(a,gel(b,j),p,aco);
    2701      471703 :   return gerepilecopy(av, iscol? gel(u,1): u);
    2702             : }
    2703             : 
    2704             : static GEN
    2705      570643 : RgM_RgC_solve_fast(GEN x, GEN y)
    2706             : {
    2707             :   GEN p, pol;
    2708             :   long pa;
    2709      570643 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    2710      570646 :   switch(t)
    2711             :   {
    2712       13552 :     case t_INT:    return ZM_gauss(x, y);
    2713          42 :     case t_FRAC:   return QM_gauss(x, y);
    2714          49 :     case t_INTMOD: return RgM_RgC_solve_FpC(x, y, p);
    2715          56 :     case t_FFELT:  return FFM_FFC_gauss(x, y, pol);
    2716      556947 :     default:       return gen_0;
    2717             :   }
    2718             : }
    2719             : 
    2720             : static GEN
    2721       48509 : RgM_solve_fast(GEN x, GEN y)
    2722             : {
    2723             :   GEN p, pol;
    2724             :   long pa;
    2725       48509 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    2726       48510 :   switch(t)
    2727             :   {
    2728          49 :     case t_INT:    return ZM_gauss(x, y);
    2729          14 :     case t_FRAC:   return QM_gauss(x, y);
    2730          98 :     case t_INTMOD: return RgM_solve_FpM(x, y, p);
    2731          63 :     case t_FFELT:  return FFM_gauss(x, y, pol);
    2732       48286 :     default:       return gen_0;
    2733             :   }
    2734             : }
    2735             : 
    2736             : GEN
    2737      619152 : RgM_solve(GEN a, GEN b)
    2738             : {
    2739      619152 :   pari_sp av = avma;
    2740             :   GEN u;
    2741      619152 :   if (!b) return RgM_inv(a);
    2742      619152 :   u = typ(b)==t_MAT ? RgM_solve_fast(a, b): RgM_RgC_solve_fast(a, b);
    2743      619156 :   if (!u) return gc_NULL(av);
    2744      619058 :   if (u != gen_0) return u;
    2745      605233 :   return RgM_solve_basecase(a, b);
    2746             : }
    2747             : 
    2748             : GEN
    2749      201059 : RgM_inv(GEN a)
    2750             : {
    2751      201059 :   GEN b = RgM_inv_fast(a);
    2752      201046 :   return b==gen_0? RgM_solve_basecase(a, NULL): b;
    2753             : }
    2754             : 
    2755             : /* assume dim A >= 1, A invertible + upper triangular  */
    2756             : static GEN
    2757      832424 : RgM_inv_upper_ind(GEN A, long index)
    2758             : {
    2759      832424 :   long n = lg(A)-1, i = index, j;
    2760      832424 :   GEN u = zerocol(n);
    2761      832430 :   gel(u,i) = ginv(gcoeff(A,i,i));
    2762     2389933 :   for (i--; i>0; i--)
    2763             :   {
    2764     1557492 :     pari_sp av = avma;
    2765     1557492 :     GEN m = gneg(gmul(gcoeff(A,i,i+1),gel(u,i+1))); /* j = i+1 */
    2766     6632405 :     for (j=i+2; j<=n; j++) m = gsub(m, gmul(gcoeff(A,i,j),gel(u,j)));
    2767     1557461 :     gel(u,i) = gerepileupto(av, gdiv(m, gcoeff(A,i,i)));
    2768             :   }
    2769      832441 :   return u;
    2770             : }
    2771             : GEN
    2772      277952 : RgM_inv_upper(GEN A)
    2773             : {
    2774             :   long i, l;
    2775      277952 :   GEN B = cgetg_copy(A, &l);
    2776     1110373 :   for (i = 1; i < l; i++) gel(B,i) = RgM_inv_upper_ind(A, i);
    2777      277963 :   return B;
    2778             : }
    2779             : 
    2780             : static GEN
    2781     2671127 : split_realimag_col(GEN z, long r1, long r2)
    2782             : {
    2783     2671127 :   long i, ru = r1+r2;
    2784     2671127 :   GEN x = cgetg(ru+r2+1,t_COL), y = x + r2;
    2785     7054195 :   for (i=1; i<=r1; i++) {
    2786     4383068 :     GEN a = gel(z,i);
    2787     4383068 :     if (typ(a) == t_COMPLEX) a = gel(a,1); /* paranoia: a should be real */
    2788     4383068 :     gel(x,i) = a;
    2789             :   }
    2790     5354966 :   for (   ; i<=ru; i++) {
    2791     2683839 :     GEN b, a = gel(z,i);
    2792     2683839 :     if (typ(a) == t_COMPLEX) { b = gel(a,2); a = gel(a,1); } else b = gen_0;
    2793     2683839 :     gel(x,i) = a;
    2794     2683839 :     gel(y,i) = b;
    2795             :   }
    2796     2671127 :   return x;
    2797             : }
    2798             : GEN
    2799     1345947 : split_realimag(GEN x, long r1, long r2)
    2800             : {
    2801             :   long i,l; GEN y;
    2802     1345947 :   if (typ(x) == t_COL) return split_realimag_col(x,r1,r2);
    2803      666094 :   y = cgetg_copy(x, &l);
    2804     2657371 :   for (i=1; i<l; i++) gel(y,i) = split_realimag_col(gel(x,i), r1, r2);
    2805      666095 :   return y;
    2806             : }
    2807             : 
    2808             : /* assume M = (r1+r2) x (r1+2r2) matrix and y compatible vector or matrix
    2809             :  * r1 first lines of M,y are real. Solve the system obtained by splitting
    2810             :  * real and imaginary parts. */
    2811             : GEN
    2812      604096 : RgM_solve_realimag(GEN M, GEN y)
    2813             : {
    2814      604096 :   long l = lg(M), r2 = l - lgcols(M), r1 = l-1 - 2*r2;
    2815      604097 :   return RgM_solve(split_realimag(M, r1,r2),
    2816             :                    split_realimag(y, r1,r2));
    2817             : }
    2818             : 
    2819             : GEN
    2820         434 : gauss(GEN a, GEN b)
    2821             : {
    2822             :   GEN z;
    2823         434 :   long t = typ(b);
    2824         434 :   if (typ(a)!=t_MAT) pari_err_TYPE("gauss",a);
    2825         434 :   if (t!=t_COL && t!=t_MAT) pari_err_TYPE("gauss",b);
    2826         434 :   z = RgM_solve(a,b);
    2827         434 :   if (!z) pari_err_INV("gauss",a);
    2828         329 :   return z;
    2829             : }
    2830             : 
    2831             : static GEN
    2832      948431 : ZlM_gauss_ratlift(GEN a, GEN b, ulong p, long e, GEN C)
    2833             : {
    2834      948431 :   pari_sp av = avma, av2;
    2835             :   GEN bb, xi, xb, pi, q, B, r;
    2836             :   long i, f, k;
    2837             :   ulong mask;
    2838      948431 :   if (!C) {
    2839           0 :     C = Flm_inv(ZM_to_Flm(a, p), p);
    2840           0 :     if (!C) pari_err_INV("ZlM_gauss", a);
    2841             :   }
    2842      948431 :   k = f = ZM_max_lg(a)-1;
    2843      948431 :   mask = quadratic_prec_mask((e+f-1)/f);
    2844      948430 :   pi = q = powuu(p, f);
    2845      948403 :   bb = b;
    2846      948403 :   C = ZpM_invlift(FpM_red(a, q), Flm_to_ZM(C), utoipos(p), f);
    2847      948420 :   av2 = avma;
    2848      948420 :   xb = xi = FpM_mul(C, b, q);
    2849      987610 :   for (i = f; i <= e; i+=f)
    2850             :   {
    2851      158706 :     if (i==k)
    2852             :     {
    2853      153664 :       k = (mask&1UL) ? 2*k-f: 2*k;
    2854      153664 :       mask >>= 1;
    2855      153664 :       B = sqrti(shifti(pi,-1));
    2856      153664 :       r = FpM_ratlift(xb, pi, B, B, NULL);
    2857      153664 :       if (r)
    2858             :       {
    2859      126927 :         GEN dr, nr = Q_remove_denom(r,&dr);
    2860      126927 :         if (ZM_equal(ZM_mul(a,nr), dr? ZM_Z_mul(b,dr): b))
    2861             :         {
    2862      119520 :           if (DEBUGLEVEL>=4)
    2863           0 :             err_printf("ZlM_gauss: early solution: %ld/%ld\n",i,e);
    2864      119520 :           return gerepilecopy(av, r);
    2865             :         }
    2866             :       }
    2867             :     }
    2868       39186 :     bb = ZM_Z_divexact(ZM_sub(bb, ZM_mul(a, xi)), q);
    2869       39186 :     if (gc_needed(av,2))
    2870             :     {
    2871           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZlM_gauss. i=%ld/%ld",i,e);
    2872           0 :       gerepileall(av2,3, &pi,&bb,&xb);
    2873             :     }
    2874       39186 :     xi = FpM_mul(C, bb, q);
    2875       39186 :     xb = ZM_add(xb, ZM_Z_mul(xi, pi));
    2876       39186 :     pi = mulii(pi, q);
    2877             :   }
    2878      828904 :   B = sqrti(shifti(pi,-1));
    2879      828918 :   return gerepileupto(av, FpM_ratlift(xb, pi, B, B, NULL));
    2880             : }
    2881             : 
    2882             : /* Dixon p-adic lifting algorithm.
    2883             :  * Numer. Math. 40, 137-141 (1982), DOI: 10.1007/BF01459082 */
    2884             : GEN
    2885      950647 : ZM_gauss(GEN a, GEN b)
    2886             : {
    2887      950647 :   pari_sp av = avma, av2;
    2888             :   int iscol;
    2889             :   long n, ncol, i, m, elim;
    2890             :   ulong p;
    2891             :   GEN C, delta, nb, nmin, res;
    2892             :   forprime_t S;
    2893             : 
    2894      950647 :   if (!init_gauss(a, &b, &n, &ncol, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
    2895      950574 :   if (n < ncol)
    2896             :   {
    2897          28 :     GEN y = ZM_indexrank(a), y1 = gel(y,1), y2 = gel(y,2);
    2898          28 :     if (lg(y2)-1 != n) return NULL;
    2899          28 :     a = rowpermute(a, y1);
    2900          28 :     b = rowpermute(b, y1);
    2901             :   }
    2902             :   /* a is square and invertible */
    2903      950574 :   nb = gen_0; ncol = lg(b);
    2904     2064417 :   for (i = 1; i < ncol; i++)
    2905             :   {
    2906     1113853 :     GEN ni = gnorml2(gel(b, i));
    2907     1113841 :     if (cmpii(nb, ni) < 0) nb = ni;
    2908             :   }
    2909      950564 :   if (!signe(nb)) {set_avma(av); return iscol? zerocol(n): zeromat(n,lg(b)-1);}
    2910      948436 :   delta = gen_1; nmin = nb;
    2911     4083928 :   for (i = 1; i <= n; i++)
    2912             :   {
    2913     3135575 :     GEN ni = gnorml2(gel(a, i));
    2914     3135687 :     if (cmpii(ni, nmin) < 0)
    2915             :     {
    2916       80983 :       delta = mulii(delta, nmin); nmin = ni;
    2917             :     }
    2918             :     else
    2919     3054705 :       delta = mulii(delta, ni);
    2920             :   }
    2921      948353 :   if (!signe(nmin)) return NULL;
    2922      948339 :   elim = expi(delta)+1;
    2923      948407 :   av2 = avma;
    2924      948407 :   init_modular_big(&S);
    2925             :   for(;;)
    2926             :   {
    2927      948424 :     p = u_forprime_next(&S);
    2928      948423 :     C = Flm_inv_sp(ZM_to_Flm(a, p), NULL, p);
    2929      948436 :     if (C) break;
    2930           7 :     elim -= expu(p);
    2931           7 :     if (elim < 0) return NULL;
    2932           0 :     set_avma(av2);
    2933             :   }
    2934             :   /* N.B. Our delta/lambda are SQUARES of those in the paper
    2935             :    * log(delta lambda) / log p, where lambda is 3+sqrt(5) / 2,
    2936             :    * whose log is < 1, hence + 1 (to cater for rounding errors) */
    2937      948429 :   m = (long)ceil((dbllog2(delta)*M_LN2 + 1) / log((double)p));
    2938      948432 :   res = ZlM_gauss_ratlift(a, b, p, m, C);
    2939      948438 :   if (iscol) return gerepilecopy(av, gel(res, 1));
    2940       79664 :   return gerepileupto(av, res);
    2941             : }
    2942             : 
    2943             : /* #C = n, C[z[i]] = K[i], complete by 0s */
    2944             : static GEN
    2945          14 : RgC_inflate(GEN K, GEN z, long n)
    2946             : {
    2947          14 :   GEN c = zerocol(n);
    2948          14 :   long j, l = lg(K);
    2949          28 :   for (j = 1; j < l; j++) gel(c, z[j]) = gel(K, j);
    2950          14 :   return c;
    2951             : }
    2952             : /* in place: C[i] *= cB / v[i] */
    2953             : static void
    2954        6069 : QC_normalize(GEN C, GEN v, GEN cB)
    2955             : {
    2956        6069 :   long l = lg(C), i;
    2957       45521 :   for (i = 1; i < l; i++)
    2958             :   {
    2959       39452 :     GEN c = cB, k = gel(C,i), d = gel(v,i);
    2960       39452 :     if (d)
    2961             :     {
    2962       23814 :       if (isintzero(d)) { gel(C,i) = gen_0; continue; }
    2963       23814 :       c = div_content(c, d);
    2964             :     }
    2965       39452 :     gel(C,i) = c? gmul(k,c): k;
    2966             :   }
    2967        6069 : }
    2968             : 
    2969             : /* same as above, M rational; if flag = 1, call indexrank and return 1 sol */
    2970             : GEN
    2971        6062 : QM_gauss_i(GEN M, GEN B, long flag)
    2972             : {
    2973        6062 :   pari_sp av = avma;
    2974             :   long i, l, n;
    2975        6062 :   int col = typ(B) == t_COL;
    2976        6062 :   GEN K, cB, N = cgetg_copy(M, &l), v = cgetg(l, t_VEC), z2 = NULL;
    2977             : 
    2978       45542 :   for (i = 1; i < l; i++)
    2979       39480 :     gel(N,i) = Q_primitive_part(gel(M,i), &gel(v,i));
    2980        6062 :   if (flag)
    2981             :   {
    2982         301 :     GEN z = ZM_indexrank(N), z1 = gel(z,1);
    2983         301 :     z2 = gel(z,2);
    2984         301 :     N = shallowmatextract(N, z1, z2);
    2985         301 :     B = col? vecpermute(B,z1): rowpermute(B,z1);
    2986         301 :     if (lg(z2) == l) z2 = NULL; else v = vecpermute(v, z2);
    2987             :   }
    2988        6062 :   B = Q_primitive_part(B, &cB);
    2989        6062 :   K = ZM_gauss(N, B); if (!K) return gc_NULL(av);
    2990        6062 :   n = l - 1;
    2991        6062 :   if (col)
    2992             :   {
    2993        6034 :     QC_normalize(K, v, cB);
    2994        6034 :     if (z2) K = RgC_inflate(K, z2, n);
    2995             :   }
    2996             :   else
    2997             :   {
    2998          28 :     long lK = lg(K);
    2999          63 :     for (i = 1; i < lK; i++)
    3000             :     {
    3001          35 :       QC_normalize(gel(K,i), v, cB);
    3002          35 :       if (z2) gel(K,i) = RgC_inflate(gel(K,i), z2, n);
    3003             :     }
    3004             :   }
    3005        6062 :   return gerepilecopy(av, K);
    3006             : }
    3007             : GEN
    3008        5761 : QM_gauss(GEN M, GEN B) { return QM_gauss_i(M, B, 0); }
    3009             : 
    3010             : static GEN
    3011      589781 : ZM_inv_slice(GEN A, GEN P, GEN *mod)
    3012             : {
    3013      589781 :   pari_sp av = avma;
    3014      589781 :   long i, n = lg(P)-1;
    3015             :   GEN H, T;
    3016      589781 :   if (n == 1)
    3017             :   {
    3018      574133 :     ulong p = uel(P,1);
    3019      574133 :     GEN Hp, a = ZM_to_Flm(A, p);
    3020      574135 :     Hp = Flm_adjoint(a, p);
    3021      574131 :     Hp = gerepileupto(av, Flm_to_ZM(Hp));
    3022      574135 :     *mod = utoipos(p); return Hp;
    3023             :   }
    3024       15648 :   T = ZV_producttree(P);
    3025       15648 :   A = ZM_nv_mod_tree(A, P, T);
    3026       15648 :   H = cgetg(n+1, t_VEC);
    3027       77282 :   for(i=1; i <= n; i++)
    3028       61634 :     gel(H,i) = Flm_adjoint(gel(A, i), uel(P,i));
    3029       15648 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    3030       15647 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    3031             : }
    3032             : 
    3033             : static GEN
    3034      545250 : RgM_true_Hadamard(GEN a)
    3035             : {
    3036      545250 :   pari_sp av = avma;
    3037      545250 :   long n = lg(a)-1, i;
    3038             :   GEN B;
    3039      545250 :   if (n == 0) return gen_1;
    3040      545250 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    3041      545249 :   B = gnorml2(gel(a,1));
    3042     2286832 :   for (i = 2; i <= n; i++) B = gmul(B, gnorml2(gel(a,i)));
    3043      545253 :   return gerepileuptoint(av, ceil_safe(sqrtr(B)));
    3044             : }
    3045             : 
    3046             : GEN
    3047      589781 : ZM_inv_worker(GEN P, GEN A)
    3048             : {
    3049      589781 :   GEN V = cgetg(3, t_VEC);
    3050      589781 :   gel(V,1) = ZM_inv_slice(A, P, &gel(V,2));
    3051      589781 :   return V;
    3052             : }
    3053             : 
    3054             : static GEN
    3055       41110 : ZM_inv0(GEN A, GEN *pden)
    3056             : {
    3057       41110 :   if (pden) *pden = gen_1;
    3058       41110 :   (void)A; return cgetg(1, t_MAT);
    3059             : }
    3060             : static GEN
    3061       66683 : ZM_inv1(GEN A, GEN *pden)
    3062             : {
    3063       66683 :   GEN a = gcoeff(A,1,1);
    3064       66683 :   long s = signe(a);
    3065       66683 :   if (!s) return NULL;
    3066       66683 :   if (pden) *pden = absi(a);
    3067       66683 :   retmkmat(mkcol(s == 1? gen_1: gen_m1));
    3068             : }
    3069             : static GEN
    3070      122234 : ZM_inv2(GEN A, GEN *pden)
    3071             : {
    3072             :   GEN a, b, c, d, D, cA;
    3073             :   long s;
    3074      122234 :   A = Q_primitive_part(A, &cA);
    3075      122234 :   a = gcoeff(A,1,1); b = gcoeff(A,1,2);
    3076      122234 :   c = gcoeff(A,2,1); d = gcoeff(A,2,2);
    3077      122234 :   D = subii(mulii(a,d), mulii(b,c)); /* left on stack */
    3078      122227 :   s = signe(D);
    3079      122227 :   if (!s) return NULL;
    3080      122227 :   if (s < 0) D = negi(D);
    3081      122229 :   if (pden) *pden = mul_denom(D, cA);
    3082      122229 :   if (s > 0)
    3083       81558 :     retmkmat2(mkcol2(icopy(d), negi(c)), mkcol2(negi(b), icopy(a)));
    3084             :   else
    3085       40671 :     retmkmat2(mkcol2(negi(d), icopy(c)), mkcol2(icopy(b), negi(a)));
    3086             : }
    3087             : 
    3088             : /* to be used when denom(M^(-1)) << det(M) and a sharp multiple is
    3089             :  * not available. Return H primitive such that M*H = den*Id */
    3090             : GEN
    3091           0 : ZM_inv_ratlift(GEN M, GEN *pden)
    3092             : {
    3093           0 :   pari_sp av2, av = avma;
    3094             :   GEN Hp, q, H;
    3095             :   ulong p;
    3096           0 :   long m = lg(M)-1;
    3097             :   forprime_t S;
    3098             :   pari_timer ti;
    3099             : 
    3100           0 :   if (m == 0) return ZM_inv0(M,pden);
    3101           0 :   if (m == 1 && nbrows(M)==1) return ZM_inv1(M,pden);
    3102           0 :   if (m == 2 && nbrows(M)==2) return ZM_inv2(M,pden);
    3103             : 
    3104           0 :   if (DEBUGLEVEL>5) timer_start(&ti);
    3105           0 :   init_modular_big(&S);
    3106           0 :   av2 = avma;
    3107           0 :   H = NULL;
    3108           0 :   while ((p = u_forprime_next(&S)))
    3109             :   {
    3110             :     GEN Mp, B, Hr;
    3111           0 :     Mp = ZM_to_Flm(M,p);
    3112           0 :     Hp = Flm_inv_sp(Mp, NULL, p);
    3113           0 :     if (!Hp) continue;
    3114           0 :     if (!H)
    3115             :     {
    3116           0 :       H = ZM_init_CRT(Hp, p);
    3117           0 :       q = utoipos(p);
    3118             :     }
    3119             :     else
    3120           0 :       ZM_incremental_CRT(&H, Hp, &q, p);
    3121           0 :     B = sqrti(shifti(q,-1));
    3122           0 :     Hr = FpM_ratlift(H,q,B,B,NULL);
    3123           0 :     if (DEBUGLEVEL>5)
    3124           0 :       timer_printf(&ti,"ZM_inv mod %lu (ratlift=%ld)", p,!!Hr);
    3125           0 :     if (Hr) {/* DONE ? */
    3126           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    3127           0 :       if (ZM_isscalar(ZM_mul(Hl, M), *pden)) { H = Hl; break; }
    3128             :     }
    3129             : 
    3130           0 :     if (gc_needed(av,2))
    3131             :     {
    3132           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZM_inv_ratlift");
    3133           0 :       gerepileall(av2, 2, &H, &q);
    3134             :     }
    3135             :   }
    3136           0 :   if (!*pden) *pden = gen_1;
    3137           0 :   return gc_all(av, 2, &H, pden);
    3138             : }
    3139             : 
    3140             : GEN
    3141       72492 : FpM_ratlift_worker(GEN A, GEN mod, GEN B)
    3142             : {
    3143             :   long l, i;
    3144       72492 :   GEN H = cgetg_copy(A, &l);
    3145      147560 :   for (i = 1; i < l; i++)
    3146             :   {
    3147       75085 :      GEN c = FpC_ratlift(gel(A,i), mod, B, B, NULL);
    3148       75070 :      gel(H,i) = c? c: gen_0;
    3149             :   }
    3150       72475 :   return H;
    3151             : }
    3152             : static int
    3153      575531 : can_ratlift(GEN x, GEN mod, GEN B)
    3154             : {
    3155      575531 :   pari_sp av = avma;
    3156             :   GEN a, b;
    3157      575531 :   return gc_bool(av, Fp_ratlift(x, mod, B, B, &a,&b));
    3158             : }
    3159             : static GEN
    3160      635847 : FpM_ratlift_parallel(GEN A, GEN mod, GEN B)
    3161             : {
    3162      635847 :   pari_sp av = avma;
    3163             :   GEN worker;
    3164      635847 :   long i, l = lg(A), m = mt_nbthreads();
    3165      635853 :   int test = !!B;
    3166             : 
    3167      635853 :   if (l == 1 || lgcols(A) == 1) return gcopy(A);
    3168      635853 :   if (!B) B = sqrti(shifti(mod,-1));
    3169      635854 :   if (m == 1 || l == 2 || lgcols(A) < 10)
    3170             :   {
    3171      629045 :     A = FpM_ratlift(A, mod, B, B, NULL);
    3172      629047 :     return A? A: gc_NULL(av);
    3173             :   }
    3174             :   /* test one coefficient first */
    3175        6809 :   if (test && !can_ratlift(gcoeff(A,1,1), mod, B)) return gc_NULL(av);
    3176        6722 :   worker = snm_closure(is_entry("_FpM_ratlift_worker"), mkvec2(mod,B));
    3177        6722 :   A = gen_parapply_slice(worker, A, m);
    3178       74355 :   for (i = 1; i < l; i++) if (typ(gel(A,i)) != t_COL) return gc_NULL(av);
    3179        5842 :   return A;
    3180             : }
    3181             : 
    3182             : static GEN
    3183      568922 : ZM_adj_ratlift(GEN A, GEN H, GEN mod, GEN T)
    3184             : {
    3185      568922 :   pari_sp av = avma;
    3186             :   GEN B, D, g;
    3187      568922 :   D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3188      568921 :   if (T) D = mulii(T, D);
    3189      568921 :   g = gcdii(D, mod);
    3190      568915 :   if (!equali1(g))
    3191             :   {
    3192          14 :     mod = diviiexact(mod, g);
    3193          14 :     H = FpM_red(H, mod);
    3194             :   }
    3195      568913 :   D = Fp_inv(Fp_red(D, mod), mod);
    3196             :   /* test 1 coeff first */
    3197      568914 :   B = sqrti(shifti(mod,-1));
    3198      568917 :   if (!can_ratlift(Fp_mul(D, gcoeff(A,1,1), mod), mod, B)) return gc_NULL(av);
    3199      554960 :   H = FpM_Fp_mul(H, D, mod);
    3200      554960 :   H = FpM_ratlift_parallel(H, mod, B);
    3201      554971 :   return H? H: gc_NULL(av);
    3202             : }
    3203             : 
    3204             : /* if (T) return T A^(-1) in Mn(Q), else B in Mn(Z) such that A B = den*Id */
    3205             : static GEN
    3206      775284 : ZM_inv_i(GEN A, GEN *pden, GEN T)
    3207             : {
    3208      775284 :   pari_sp av = avma;
    3209      775284 :   long m = lg(A)-1, n, k1 = 1, k2;
    3210      775284 :   GEN H = NULL, D, H1 = NULL, mod1 = NULL, worker;
    3211             :   ulong bnd, mask;
    3212             :   forprime_t S;
    3213             :   pari_timer ti;
    3214             : 
    3215      775284 :   if (m == 0) return ZM_inv0(A,pden);
    3216      734174 :   if (pden) *pden = gen_1;
    3217      734174 :   if (nbrows(A) < m) return NULL;
    3218      734169 :   if (m == 1 && nbrows(A)==1 && !T) return ZM_inv1(A,pden);
    3219      667486 :   if (m == 2 && nbrows(A)==2 && !T) return ZM_inv2(A,pden);
    3220             : 
    3221      545252 :   if (DEBUGLEVEL>=5) timer_start(&ti);
    3222      545252 :   init_modular_big(&S);
    3223      545252 :   bnd = expi(RgM_true_Hadamard(A));
    3224      545254 :   worker = snm_closure(is_entry("_ZM_inv_worker"), mkvec(A));
    3225      545254 :   gen_inccrt("ZM_inv_r", worker, NULL, k1, 0, &S, &H1, &mod1, nmV_chinese_center, FpM_center);
    3226      545254 :   n = (bnd+1)/expu(S.p)+1;
    3227      545254 :   if (DEBUGLEVEL>=5) timer_printf(&ti,"inv (%ld/%ld primes)", k1, n);
    3228      545254 :   mask = quadratic_prec_mask(n);
    3229      545254 :   for (k2 = 0;;)
    3230       44356 :   {
    3231             :     GEN Hr;
    3232      589610 :     if (k2 > 0)
    3233             :     {
    3234       38747 :       gen_inccrt("ZM_inv_r", worker, NULL, k2, 0, &S, &H1, &mod1,nmV_chinese_center,FpM_center);
    3235       38747 :       k1 += k2;
    3236       38747 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"CRT (%ld/%ld primes)", k1, n);
    3237             :     }
    3238      589610 :     if (mask == 1) break;
    3239      568922 :     k2 = (mask&1UL) ? k1-1: k1;
    3240      568922 :     mask >>= 1;
    3241             : 
    3242      568922 :     Hr = ZM_adj_ratlift(A, H1, mod1, T);
    3243      568923 :     if (DEBUGLEVEL>=5) timer_printf(&ti,"ratlift (%ld/%ld primes)", k1, n);
    3244      568923 :     if (Hr) {/* DONE ? */
    3245      527454 :       GEN Hl = Q_primpart(Hr), R = ZM_mul(Hl, A), d = gcoeff(R,1,1);
    3246      527453 :       if (gsigne(d) < 0) { d = gneg(d); Hl = ZM_neg(Hl); }
    3247      527453 :       if (DEBUGLEVEL>=5) timer_printf(&ti,"mult (%ld/%ld primes)", k1, n);
    3248      527453 :       if (equali1(d))
    3249             :       {
    3250      437626 :         if (ZM_isidentity(R)) { H = Hl; break; }
    3251             :       }
    3252       89827 :       else if (ZM_isscalar(R, d))
    3253             :       {
    3254       86940 :         if (T) T = gdiv(T,d);
    3255       83641 :         else if (pden) *pden = d;
    3256       86940 :         H = Hl; break;
    3257             :       }
    3258             :     }
    3259             :   }
    3260      545254 :   if (!H)
    3261             :   {
    3262             :     GEN d;
    3263       20688 :     H = H1;
    3264       20688 :     D = ZMrow_ZC_mul(H, gel(A,1), 1);
    3265       20688 :     if (signe(D)==0) pari_err_INV("ZM_inv", A);
    3266       20688 :     if (T) T = gdiv(T, D);
    3267             :     else
    3268             :     {
    3269       19766 :       d = gcdii(Q_content_safe(H), D);
    3270       19766 :       if (signe(D) < 0) d = negi(d);
    3271       19766 :       if (!equali1(d))
    3272             :       {
    3273       11962 :         H = ZM_Z_divexact(H, d);
    3274       11962 :         D = diviiexact(D, d);
    3275             :       }
    3276       19767 :       if (pden) *pden = D;
    3277             :     }
    3278             :   }
    3279      545255 :   if (T && !isint1(T)) H = ZM_Q_mul(H, T);
    3280      545255 :   return gc_all(av, pden? 2: 1, &H, pden);
    3281             : }
    3282             : GEN
    3283      714748 : ZM_inv(GEN A, GEN *pden) { return ZM_inv_i(A, pden, NULL); }
    3284             : 
    3285             : /* same as above, M rational */
    3286             : GEN
    3287       60535 : QM_inv(GEN M)
    3288             : {
    3289       60535 :   pari_sp av = avma;
    3290             :   GEN den, dM, K;
    3291       60535 :   M = Q_remove_denom(M, &dM);
    3292       60535 :   K = ZM_inv_i(M, &den, dM);
    3293       60535 :   if (!K) return gc_NULL(av);
    3294       60528 :   if (den && !equali1(den)) K = ZM_Q_mul(K, ginv(den));
    3295       60515 :   return gerepileupto(av, K);
    3296             : }
    3297             : 
    3298             : static GEN
    3299      105218 : ZM_ker_filter(GEN A, GEN P)
    3300             : {
    3301      105218 :   long i, j, l = lg(A), n = 1, d = lg(gmael(A,1,1));
    3302      105218 :   GEN B, Q, D = gmael(A,1,2);
    3303      215177 :   for (i=2; i<l; i++)
    3304             :   {
    3305      109959 :     GEN Di = gmael(A,i,2);
    3306      109959 :     long di = lg(gmael(A,i,1));
    3307      109959 :     int c = vecsmall_lexcmp(D, Di);
    3308      109959 :     if (di==d && c==0) n++;
    3309       45588 :     else if (d > di || (di==d && c>0))
    3310       37680 :     { n = 1; d = di; D = Di; }
    3311             :   }
    3312      105218 :   B = cgetg(n+1, t_VEC);
    3313      105219 :   Q = cgetg(n+1, typ(P));
    3314      320394 :   for (i=1, j=1; i<l; i++)
    3315             :   {
    3316      215176 :     if (lg(gmael(A,i,1))==d &&  vecsmall_lexcmp(D, gmael(A,i,2))==0)
    3317             :     {
    3318      169587 :       gel(B,j) = gmael(A,i,1);
    3319      169587 :       Q[j] = P[i];
    3320      169587 :       j++;
    3321             :     }
    3322             :   }
    3323      105218 :   return mkvec3(B,Q,D);
    3324             : }
    3325             : 
    3326             : static GEN
    3327       69555 : ZM_ker_chinese(GEN A, GEN P, GEN *mod)
    3328             : {
    3329       69555 :   GEN BQD = ZM_ker_filter(A, P);
    3330       69555 :   return mkvec2(nmV_chinese_center(gel(BQD,1), gel(BQD,2), mod), gel(BQD,3));
    3331             : }
    3332             : 
    3333             : static GEN
    3334      129629 : ZM_ker_slice(GEN A, GEN P, GEN *mod)
    3335             : {
    3336      129629 :   pari_sp av = avma;
    3337      129629 :   long i, n = lg(P)-1;
    3338             :   GEN BQD, D, H, T, Q;
    3339      129629 :   if (n == 1)
    3340             :   {
    3341       93962 :     ulong p = uel(P,1);
    3342       93962 :     GEN K = Flm_ker_sp(ZM_to_Flm(A, p), p, 2);
    3343       93962 :     *mod = utoipos(p); return mkvec2(Flm_to_ZM(gel(K,1)), gel(K,2));
    3344             :   }
    3345       35667 :   T = ZV_producttree(P);
    3346       35666 :   A = ZM_nv_mod_tree(A, P, T);
    3347       35667 :   H = cgetg(n+1, t_VEC);
    3348      111498 :   for(i=1 ; i <= n; i++)
    3349       75835 :     gel(H,i) = Flm_ker_sp(gel(A, i), P[i], 2);
    3350       35663 :   BQD = ZM_ker_filter(H, P); Q = gel(BQD,2);
    3351       35663 :   if (lg(Q) != lg(P)) T = ZV_producttree(Q);
    3352       35663 :   H = nmV_chinese_center_tree_seq(gel(BQD,1), Q, T, ZV_chinesetree(Q,T));
    3353       35665 :   *mod = gmael(T, lg(T)-1, 1);
    3354       35665 :   D = gel(BQD, 3);
    3355       35665 :   gerepileall(av, 3, &H, &D, mod);
    3356       35664 :   return mkvec2(H,D);
    3357             : }
    3358             : 
    3359             : GEN
    3360      129629 : ZM_ker_worker(GEN P, GEN A)
    3361             : {
    3362      129629 :   GEN V = cgetg(3, t_VEC);
    3363      129629 :   gel(V,1) = ZM_ker_slice(A, P, &gel(V,2));
    3364      129626 :   return V;
    3365             : }
    3366             : 
    3367             : /* assume lg(A) > 1 */
    3368             : static GEN
    3369       62891 : ZM_ker_i(GEN A)
    3370             : {
    3371             :   pari_sp av;
    3372       62891 :   long k, m = lg(A)-1;
    3373       62891 :   GEN HD = NULL, mod = gen_1, worker;
    3374             :   forprime_t S;
    3375             : 
    3376       62891 :   if (m >= 2*nbrows(A))
    3377             :   {
    3378        3052 :     GEN v = ZM_indexrank(A), y = gel(v,2), z = indexcompl(y, m);
    3379             :     GEN B, A1, A1i, d;
    3380        3052 :     A = rowpermute(A, gel(v,1)); /* same kernel */
    3381        3052 :     A1 = vecpermute(A, y); /* maximal rank submatrix */
    3382        3052 :     B = vecpermute(A, z);
    3383        3052 :     A1i = ZM_inv(A1, &d);
    3384        3052 :     if (!d) d = gen_1;
    3385        3052 :     B = vconcat(ZM_mul(ZM_neg(A1i), B), scalarmat_shallow(d, lg(B)-1));
    3386        3052 :     if (!gequal(y, identity_perm(lg(y)-1)))
    3387         657 :       B = rowpermute(B, perm_inv(shallowconcat(y,z)));
    3388        3052 :     return vec_Q_primpart(B);
    3389             :   }
    3390       59839 :   init_modular_big(&S);
    3391       59839 :   worker = snm_closure(is_entry("_ZM_ker_worker"), mkvec(A));
    3392       59839 :   av = avma;
    3393       59839 :   for (k = 1;; k <<= 1)
    3394       65342 :   {
    3395             :     pari_timer ti;
    3396             :     GEN H, Hr;
    3397      125181 :     gen_inccrt_i("ZM_ker", worker, NULL, (k+1)>>1, 0,
    3398             :                  &S, &HD, &mod, ZM_ker_chinese, NULL);
    3399      125181 :     gerepileall(av, 2, &HD, &mod);
    3400      140724 :     H = gel(HD, 1); if (lg(H) == 1) return H;
    3401       80885 :     if (DEBUGLEVEL >= 4) timer_start(&ti);
    3402       80885 :     Hr = FpM_ratlift_parallel(H, mod, NULL);
    3403       80885 :     if (DEBUGLEVEL >= 4) timer_printf(&ti,"ZM_ker: ratlift (%ld)",!!Hr);
    3404       80885 :     if (Hr)
    3405             :     {
    3406             :       GEN MH;
    3407       70141 :       Hr = vec_Q_primpart(Hr);
    3408       70141 :       MH = ZM_mul(A, Hr);
    3409       70141 :       if (DEBUGLEVEL >= 4) timer_printf(&ti,"ZM_ker: QM_mul");
    3410       70141 :       if (ZM_equal0(MH)) return Hr;
    3411             :     }
    3412             :   }
    3413             : }
    3414             : 
    3415             : GEN
    3416       47141 : ZM_ker(GEN M)
    3417             : {
    3418       47141 :   pari_sp av = avma;
    3419       47141 :   long l = lg(M)-1;
    3420       47141 :   if (l==0) return cgetg(1, t_MAT);
    3421       47141 :   if (lgcols(M)==1) return matid(l);
    3422       47141 :   return gerepilecopy(av, ZM_ker_i(M));
    3423             : }
    3424             : 
    3425             : GEN
    3426       16569 : QM_ker(GEN M)
    3427             : {
    3428       16569 :   pari_sp av = avma;
    3429       16569 :   long l = lg(M)-1;
    3430       16569 :   if (l==0) return cgetg(1, t_MAT);
    3431       16527 :   if (lgcols(M)==1) return matid(l);
    3432       15666 :   return gerepilecopy(av, ZM_ker_i(row_Q_primpart(M)));
    3433             : }
    3434             : 
    3435             : /* x a ZM. Return a multiple of the determinant of the lattice generated by
    3436             :  * the columns of x. From Algorithm 2.2.6 in GTM138 */
    3437             : GEN
    3438       47549 : detint(GEN A)
    3439             : {
    3440       47549 :   if (typ(A) != t_MAT) pari_err_TYPE("detint",A);
    3441       47549 :   RgM_check_ZM(A, "detint");
    3442       47549 :   return ZM_detmult(A);
    3443             : }
    3444             : GEN
    3445      155070 : ZM_detmult(GEN A)
    3446             : {
    3447      155070 :   pari_sp av1, av = avma;
    3448             :   GEN B, c, v, piv;
    3449      155070 :   long rg, i, j, k, m, n = lg(A) - 1;
    3450             : 
    3451      155070 :   if (!n) return gen_1;
    3452      155070 :   m = nbrows(A);
    3453      155070 :   if (n < m) return gen_0;
    3454      154993 :   c = zero_zv(m);
    3455      154993 :   av1 = avma;
    3456      154993 :   B = zeromatcopy(m,m);
    3457      154993 :   v = cgetg(m+1, t_COL);
    3458      154993 :   piv = gen_1; rg = 0;
    3459      680643 :   for (k=1; k<=n; k++)
    3460             :   {
    3461      680629 :     GEN pivprec = piv;
    3462      680629 :     long t = 0;
    3463     4908045 :     for (i=1; i<=m; i++)
    3464             :     {
    3465     4227417 :       pari_sp av2 = avma;
    3466             :       GEN vi;
    3467     4227417 :       if (c[i]) continue;
    3468             : 
    3469     2454271 :       vi = mulii(piv, gcoeff(A,i,k));
    3470    22463686 :       for (j=1; j<=m; j++)
    3471    20009375 :         if (c[j]) vi = addii(vi, mulii(gcoeff(B,j,i),gcoeff(A,j,k)));
    3472     2454311 :       if (!t && signe(vi)) t = i;
    3473     2454311 :       gel(v,i) = gerepileuptoint(av2, vi);
    3474             :     }
    3475      680628 :     if (!t) continue;
    3476             :     /* at this point c[t] = 0 */
    3477             : 
    3478      680537 :     if (++rg >= m) { /* full rank; mostly done */
    3479      154978 :       GEN det = gel(v,t); /* last on stack */
    3480      154978 :       if (++k > n)
    3481      154839 :         det = absi(det);
    3482             :       else
    3483             :       {
    3484             :         /* improve further; at this point c[i] is set for all i != t */
    3485         139 :         gcoeff(B,t,t) = piv; v = centermod(gel(B,t), det);
    3486         446 :         for ( ; k<=n; k++)
    3487         307 :           det = gcdii(det, ZV_dotproduct(v, gel(A,k)));
    3488             :       }
    3489      154978 :       return gerepileuptoint(av, det);
    3490             :     }
    3491             : 
    3492      525559 :     piv = gel(v,t);
    3493     4071946 :     for (i=1; i<=m; i++)
    3494             :     {
    3495             :       GEN mvi;
    3496     3546388 :       if (c[i] || i == t) continue;
    3497             : 
    3498     1773195 :       gcoeff(B,t,i) = mvi = negi(gel(v,i));
    3499    17551825 :       for (j=1; j<=m; j++)
    3500    15778631 :         if (c[j]) /* implies j != t */
    3501             :         {
    3502     4077414 :           pari_sp av2 = avma;
    3503     4077414 :           GEN z = addii(mulii(gcoeff(B,j,i), piv), mulii(gcoeff(B,j,t), mvi));
    3504     4077413 :           if (rg > 1) z = diviiexact(z, pivprec);
    3505     4077412 :           gcoeff(B,j,i) = gerepileuptoint(av2, z);
    3506             :         }
    3507             :     }
    3508      525558 :     c[t] = k;
    3509      525558 :     if (gc_needed(av,1))
    3510             :     {
    3511           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"detint. k=%ld",k);
    3512           0 :       gerepileall(av1, 2, &piv,&B); v = zerovec(m);
    3513             :     }
    3514             :   }
    3515          14 :   return gc_const(av, gen_0);
    3516             : }
    3517             : 
    3518             : /* Reduce x modulo (invertible) y */
    3519             : GEN
    3520       13468 : closemodinvertible(GEN x, GEN y)
    3521             : {
    3522       13468 :   return gmul(y, ground(RgM_solve(y,x)));
    3523             : }
    3524             : GEN
    3525           7 : reducemodinvertible(GEN x, GEN y)
    3526             : {
    3527           7 :   return gsub(x, closemodinvertible(x,y));
    3528             : }
    3529             : GEN
    3530           0 : reducemodlll(GEN x,GEN y)
    3531             : {
    3532           0 :   return reducemodinvertible(x, ZM_lll(y, 0.75, LLL_INPLACE));
    3533             : }
    3534             : 
    3535             : /*******************************************************************/
    3536             : /*                                                                 */
    3537             : /*                    KERNEL of an m x n matrix                    */
    3538             : /*          return n - rk(x) linearly independent vectors          */
    3539             : /*                                                                 */
    3540             : /*******************************************************************/
    3541             : static GEN
    3542          28 : RgM_deplin_i(GEN x0)
    3543             : {
    3544          28 :   pari_sp av = avma, av2;
    3545          28 :   long i, j, k, nl, nc = lg(x0)-1;
    3546             :   GEN D, x, y, c, l, d, ck;
    3547             : 
    3548          28 :   if (!nc) return NULL;
    3549          28 :   nl = nbrows(x0);
    3550          28 :   c = zero_zv(nl);
    3551          28 :   l = cgetg(nc+1, t_VECSMALL); /* not initialized */
    3552          28 :   av2 = avma;
    3553          28 :   x = RgM_shallowcopy(x0);
    3554          28 :   d = const_vec(nl, gen_1); /* pivot list */
    3555          28 :   ck = NULL; /* gcc -Wall */
    3556          98 :   for (k=1; k<=nc; k++)
    3557             :   {
    3558          91 :     ck = gel(x,k);
    3559         196 :     for (j=1; j<k; j++)
    3560             :     {
    3561         105 :       GEN cj = gel(x,j), piv = gel(d,j), q = gel(ck,l[j]);
    3562         420 :       for (i=1; i<=nl; i++)
    3563         315 :         if (i!=l[j]) gel(ck,i) = gsub(gmul(piv, gel(ck,i)), gmul(q, gel(cj,i)));
    3564             :     }
    3565             : 
    3566          91 :     i = gauss_get_pivot_NZ(x, NULL, k, c);
    3567          91 :     if (i > nl) break;
    3568          70 :     if (gc_needed(av,1))
    3569             :     {
    3570           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"deplin k = %ld/%ld",k,nc);
    3571           0 :       gerepileall(av2, 2, &x, &d);
    3572           0 :       ck = gel(x,k);
    3573             :     }
    3574          70 :     gel(d,k) = gel(ck,i);
    3575          70 :     c[i] = k; l[k] = i; /* pivot d[k] in x[i,k] */
    3576             :   }
    3577          28 :   if (k > nc) return gc_NULL(av);
    3578          21 :   if (k == 1) { set_avma(av); return scalarcol_shallow(gen_1,nc); }
    3579          21 :   y = cgetg(nc+1,t_COL);
    3580          21 :   gel(y,1) = gcopy(gel(ck, l[1]));
    3581          49 :   for (D=gel(d,1),j=2; j<k; j++)
    3582             :   {
    3583          28 :     gel(y,j) = gmul(gel(ck, l[j]), D);
    3584          28 :     D = gmul(D, gel(d,j));
    3585             :   }
    3586          21 :   gel(y,j) = gneg(D);
    3587          21 :   for (j++; j<=nc; j++) gel(y,j) = gen_0;
    3588          21 :   y = primitive_part(y, &c);
    3589          21 :   return c? gerepileupto(av, y): gerepilecopy(av, y);
    3590             : }
    3591             : static GEN
    3592           0 : RgV_deplin(GEN v)
    3593             : {
    3594           0 :   pari_sp av = avma;
    3595           0 :   long n = lg(v)-1;
    3596           0 :   GEN y, p = NULL;
    3597           0 :   if (n <= 1)
    3598             :   {
    3599           0 :     if (n == 1 && gequal0(gel(v,1))) return mkcol(gen_1);
    3600           0 :     return cgetg(1, t_COL);
    3601             :   }
    3602           0 :   if (gequal0(gel(v,1))) return scalarcol_shallow(gen_1, n);
    3603           0 :   v = primpart(mkvec2(gel(v,1),gel(v,2)));
    3604           0 :   if (RgV_is_FpV(v, &p) && p) v = centerlift(v);
    3605           0 :   y = zerocol(n);
    3606           0 :   gel(y,1) = gneg(gel(v,2));
    3607           0 :   gel(y,2) = gcopy(gel(v,1));
    3608           0 :   return gerepileupto(av, y);
    3609             : 
    3610             : }
    3611             : 
    3612             : static GEN
    3613         105 : RgM_deplin_FpM(GEN x, GEN p)
    3614             : {
    3615         105 :   pari_sp av = avma;
    3616             :   ulong pp;
    3617         105 :   x = RgM_Fp_init3(x, p, &pp);
    3618         105 :   switch(pp)
    3619             :   {
    3620          35 :   case 0:
    3621          35 :     x = FpM_ker_gen(x,p,1);
    3622          35 :     if (!x) return gc_NULL(av);
    3623          21 :     x = FpC_center(x,p,shifti(p,-1));
    3624          21 :     break;
    3625          14 :   case 2:
    3626          14 :     x = F2m_ker_sp(x,1);
    3627          14 :     if (!x) return gc_NULL(av);
    3628           7 :     x = F2c_to_ZC(x); break;
    3629           0 :   case 3:
    3630           0 :     x = F3m_ker_sp(x,1);
    3631           0 :     if (!x) return gc_NULL(av);
    3632           0 :     x = F3c_to_ZC(x); break;
    3633          56 :   default:
    3634          56 :     x = Flm_ker_sp(x,pp,1);
    3635          56 :     if (!x) return gc_NULL(av);
    3636          35 :     x = Flv_center(x, pp, pp>>1);
    3637          35 :     x = zc_to_ZC(x);
    3638          35 :     break;
    3639             :   }
    3640          63 :   return gerepileupto(av, x);
    3641             : }
    3642             : 
    3643             : /* FIXME: implement direct modular ZM_deplin ? */
    3644             : static GEN
    3645         119 : QM_deplin(GEN M)
    3646             : {
    3647         119 :   pari_sp av = avma;
    3648         119 :   long l = lg(M)-1;
    3649             :   GEN k;
    3650         119 :   if (l==0) return NULL;
    3651          84 :   if (lgcols(M)==1) return col_ei(l, 1);
    3652          84 :   k = ZM_ker_i(row_Q_primpart(M));
    3653          84 :   if (lg(k)== 1) return gc_NULL(av);
    3654          70 :   return gerepilecopy(av, gel(k,1));
    3655             : }
    3656             : 
    3657             : static GEN
    3658          42 : RgM_deplin_FqM(GEN x, GEN pol, GEN p)
    3659             : {
    3660          42 :   pari_sp av = avma;
    3661          42 :   GEN b, T = RgX_to_FpX(pol, p);
    3662          42 :   if (signe(T) == 0) pari_err_OP("deplin",x,pol);
    3663          42 :   b = FqM_deplin(RgM_to_FqM(x, T, p), T, p);
    3664          42 :   return gerepileupto(av, b);
    3665             : }
    3666             : 
    3667             : #define code(t1,t2) ((t1 << 6) | t2)
    3668             : static GEN
    3669         378 : RgM_deplin_fast(GEN x)
    3670             : {
    3671             :   GEN p, pol;
    3672             :   long pa;
    3673         378 :   long t = RgM_type(x, &p,&pol,&pa);
    3674         378 :   switch(t)
    3675             :   {
    3676         119 :     case t_INT:    /* fall through */
    3677         119 :     case t_FRAC:   return QM_deplin(x);
    3678          84 :     case t_FFELT:  return FFM_deplin(x, pol);
    3679         105 :     case t_INTMOD: return RgM_deplin_FpM(x, p);
    3680          42 :     case code(t_POLMOD, t_INTMOD):
    3681          42 :                    return RgM_deplin_FqM(x, pol, p);
    3682          28 :     default:       return gen_0;
    3683             :   }
    3684             : }
    3685             : #undef code
    3686             : 
    3687             : static GEN
    3688         378 : RgM_deplin(GEN x)
    3689             : {
    3690         378 :   GEN z = RgM_deplin_fast(x);
    3691         378 :   if (z!= gen_0) return z;
    3692          28 :   return RgM_deplin_i(x);
    3693             : }
    3694             : 
    3695             : GEN
    3696         378 : deplin(GEN x)
    3697             : {
    3698         378 :   switch(typ(x))
    3699             :   {
    3700         378 :     case t_MAT:
    3701             :     {
    3702         378 :       GEN z = RgM_deplin(x);
    3703         378 :       if (z) return z;
    3704         140 :       return cgetg(1, t_COL);
    3705             :     }
    3706           0 :     case t_VEC: return RgV_deplin(x);
    3707           0 :     default: pari_err_TYPE("deplin",x);
    3708             :   }
    3709             :   return NULL;/*LCOV_EXCL_LINE*/
    3710             : }
    3711             : 
    3712             : /*******************************************************************/
    3713             : /*                                                                 */
    3714             : /*         GAUSS REDUCTION OF MATRICES  (m lines x n cols)         */
    3715             : /*           (kernel, image, complementary image, rank)            */
    3716             : /*                                                                 */
    3717             : /*******************************************************************/
    3718             : /* return the transform of x under a standard Gauss pivot.
    3719             :  * x0 is a reference point when guessing whether x[i,j] ~ 0
    3720             :  * (iff x[i,j] << x0[i,j])
    3721             :  * Set r = dim ker(x). d[k] contains the index of the first nonzero pivot
    3722             :  * in column k */
    3723             : static GEN
    3724        1056 : gauss_pivot_ker(GEN x, GEN x0, GEN *dd, long *rr)
    3725             : {
    3726             :   GEN c, d, p, data;
    3727             :   pari_sp av;
    3728             :   long i, j, k, r, t, n, m;
    3729             :   pivot_fun pivot;
    3730             : 
    3731        1056 :   n=lg(x)-1; if (!n) { *dd=NULL; *rr=0; return cgetg(1,t_MAT); }
    3732        1056 :   m=nbrows(x); r=0;
    3733        1056 :   pivot = get_pivot_fun(x, x0, &data);
    3734        1056 :   x = RgM_shallowcopy(x);
    3735        1056 :   c = zero_zv(m);
    3736        1056 :   d = cgetg(n+1,t_VECSMALL);
    3737        1056 :   av=avma;
    3738        6260 :   for (k=1; k<=n; k++)
    3739             :   {
    3740        5204 :     j = pivot(x, data, k, c);
    3741        5204 :     if (j > m)
    3742             :     {
    3743        1113 :       r++; d[k]=0;
    3744        5068 :       for(j=1; j<k; j++)
    3745        3955 :         if (d[j]) gcoeff(x,d[j],k) = gclone(gcoeff(x,d[j],k));
    3746             :     }
    3747             :     else
    3748             :     { /* pivot for column k on row j */
    3749        4091 :       c[j]=k; d[k]=j; p = gdiv(gen_m1,gcoeff(x,j,k));
    3750        4091 :       gcoeff(x,j,k) = gen_m1;
    3751             :       /* x[j,] /= - x[j,k] */
    3752       21907 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    3753       39026 :       for (t=1; t<=m; t++)
    3754       34935 :         if (t!=j)
    3755             :         { /* x[t,] -= 1 / x[j,k] x[j,] */
    3756       30844 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    3757       30844 :           if (gequal0(p)) continue;
    3758       81212 :           for (i=k+1; i<=n; i++)
    3759       65446 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i),gmul(p,gcoeff(x,j,i)));
    3760       15766 :           if (gc_needed(av,1)) gerepile_gauss_ker(x,k,t,av);
    3761             :         }
    3762             :     }
    3763             :   }
    3764        1056 :   *dd=d; *rr=r; return x;
    3765             : }
    3766             : 
    3767             : /* r = dim ker(x).
    3768             :  * Returns d:
    3769             :  *   d[k] != 0 contains the index of a nonzero pivot in column k
    3770             :  *   d[k] == 0 if column k is a linear combination of the (k-1) first ones */
    3771             : GEN
    3772      154120 : RgM_pivots(GEN x0, GEN data, long *rr, pivot_fun pivot)
    3773             : {
    3774             :   GEN x, c, d, p;
    3775      154120 :   long i, j, k, r, t, m, n = lg(x0)-1;
    3776             :   pari_sp av;
    3777             : 
    3778      154120 :   if (RgM_is_ZM(x0)) return ZM_pivots(x0, rr);
    3779      135934 :   if (!n) { *rr = 0; return NULL; }
    3780             : 
    3781      135934 :   d = cgetg(n+1, t_VECSMALL);
    3782      135934 :   x = RgM_shallowcopy(x0);
    3783      135934 :   m = nbrows(x); r = 0;
    3784      135934 :   c = zero_zv(m);
    3785      135960 :   av = avma;
    3786     3523459 :   for (k=1; k<=n; k++)
    3787             :   {
    3788     3387524 :     j = pivot(x, data, k, c);
    3789     3387530 :     if (j > m) { r++; d[k] = 0; }
    3790             :     else
    3791             :     {
    3792      273059 :       c[j] = k; d[k] = j; p = gdiv(gen_m1, gcoeff(x,j,k));
    3793    10062321 :       for (i=k+1; i<=n; i++) gcoeff(x,j,i) = gmul(p,gcoeff(x,j,i));
    3794             : 
    3795     1096388 :       for (t=1; t<=m; t++)
    3796      823360 :         if (!c[t]) /* no pivot on that line yet */
    3797             :         {
    3798      315532 :           p = gcoeff(x,t,k); gcoeff(x,t,k) = gen_0;
    3799    22365244 :           for (i=k+1; i<=n; i++)
    3800    22049712 :             gcoeff(x,t,i) = gadd(gcoeff(x,t,i), gmul(p, gcoeff(x,j,i)));
    3801      315532 :           if (gc_needed(av,1)) gerepile_gauss(x,k,t,av,j,c);
    3802             :         }
    3803    10335532 :       for (i=k; i<=n; i++) gcoeff(x,j,i) = gen_0; /* dummy */
    3804             :     }
    3805             :   }
    3806      135935 :   *rr = r; return gc_const((pari_sp)d, d);
    3807             : }
    3808             : 
    3809             : static long
    3810      356424 : ZM_count_0_cols(GEN M)
    3811             : {
    3812      356424 :   long i, l = lg(M), n = 0;
    3813     1699649 :   for (i = 1; i < l; i++)
    3814     1343224 :     if (ZV_equal0(gel(M,i))) n++;
    3815      356425 :   return n;
    3816             : }
    3817             : 
    3818             : static void indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol);
    3819             : /* As RgM_pivots, integer entries. Set *rr = dim Ker M0 */
    3820             : GEN
    3821      379750 : ZM_pivots(GEN M0, long *rr)
    3822             : {
    3823      379750 :   GEN d, dbest = NULL;
    3824             :   long m, mm, n, nn, i, imax, rmin, rbest, zc;
    3825      379750 :   int beenthere = 0;
    3826      379750 :   pari_sp av, av0 = avma;
    3827             :   forprime_t S;
    3828             : 
    3829      379750 :   rbest = n = lg(M0)-1;
    3830      379750 :   if (n == 0) { *rr = 0; return NULL; }
    3831      356424 :   zc = ZM_count_0_cols(M0);
    3832      356424 :   if (n == zc) { *rr = zc; return zero_zv(n); }
    3833             : 
    3834      356144 :   m = nbrows(M0);
    3835      356141 :   rmin = maxss(zc, n-m);
    3836      356140 :   init_modular_small(&S);
    3837      356141 :   if (n <= m) { nn = n; mm = m; } else { nn = m; mm = n; }
    3838      356141 :   imax = (nn < 16)? 1: (nn < 64)? 2: 3; /* heuristic */
    3839             : 
    3840             :   for(;;)
    3841           0 :   {
    3842             :     GEN row, col, M, KM, IM, RHS, X, cX;
    3843             :     long rk;
    3844      379786 :     for (av = avma, i = 0;; set_avma(av), i++)
    3845       23645 :     {
    3846      379786 :       ulong p = u_forprime_next(&S);
    3847             :       long rp;
    3848      379789 :       if (!p) pari_err_OVERFLOW("ZM_pivots [ran out of primes]");
    3849      379789 :       d = Flm_pivots(ZM_to_Flm(M0, p), p, &rp, 1);
    3850      379790 :       if (rp == rmin) { rbest = rp; goto END; } /* maximal rank, return */
    3851       45776 :       if (rp < rbest) { /* save best r so far */
    3852       22133 :         rbest = rp;
    3853       22133 :         guncloneNULL(dbest);
    3854       22133 :         dbest = gclone(d);
    3855       22133 :         if (beenthere) break;
    3856             :       }
    3857       45776 :       if (!beenthere && i >= imax) break;
    3858             :     }
    3859       22131 :     beenthere = 1;
    3860             :     /* Dubious case: there is (probably) a non trivial kernel */
    3861       22131 :     indexrank_all(m,n, rbest, dbest, &row, &col);
    3862       22131 :     M = rowpermute(vecpermute(M0, col), row);
    3863       22131 :     rk = n - rbest; /* (probable) dimension of image */
    3864       22131 :     if (n > m) M = shallowtrans(M);
    3865       22131 :     IM = vecslice(M,1,rk);
    3866       22131 :     KM = vecslice(M,rk+1, nn);
    3867       22131 :     M = rowslice(IM, 1,rk); /* square maximal rank */
    3868       22131 :     X = ZM_gauss(M, rowslice(KM, 1,rk));
    3869       22131 :     RHS = rowslice(KM,rk+1,mm);
    3870       22131 :     M = rowslice(IM,rk+1,mm);
    3871       22131 :     X = Q_remove_denom(X, &cX);
    3872       22131 :     if (cX) RHS = ZM_Z_mul(RHS, cX);
    3873       22131 :     if (ZM_equal(ZM_mul(M, X), RHS)) { d = vecsmall_copy(dbest); goto END; }
    3874           0 :     set_avma(av);
    3875             :   }
    3876      356145 : END:
    3877      356145 :   *rr = rbest; guncloneNULL(dbest);
    3878      356144 :   return gerepileuptoleaf(av0, d);
    3879             : }
    3880             : 
    3881             : /* set *pr = dim Ker x */
    3882             : static GEN
    3883       59093 : gauss_pivot(GEN x, long *pr) {
    3884             :   GEN data;
    3885       59093 :   pivot_fun pivot = get_pivot_fun(x, x, &data);
    3886       59093 :   return RgM_pivots(x, data, pr, pivot);
    3887             : }
    3888             : 
    3889             : /* compute ker(x), x0 is a reference point when guessing whether x[i,j] ~ 0
    3890             :  * (iff x[i,j] << x0[i,j]) */
    3891             : static GEN
    3892        1056 : ker_aux(GEN x, GEN x0)
    3893             : {
    3894        1056 :   pari_sp av = avma;
    3895             :   GEN d,y;
    3896             :   long i,j,k,r,n;
    3897             : 
    3898        1056 :   x = gauss_pivot_ker(x,x0,&d,&r);
    3899        1056 :   if (!r) { set_avma(av); return cgetg(1,t_MAT); }
    3900        1015 :   n = lg(x)-1; y=cgetg(r+1,t_MAT);
    3901        2128 :   for (j=k=1; j<=r; j++,k++)
    3902             :   {
    3903        1113 :     GEN p = cgetg(n+1,t_COL);
    3904             : 
    3905        4704 :     gel(y,j) = p; while (d[k]) k++;
    3906        5068 :     for (i=1; i<k; i++)
    3907        3955 :       if (d[i])
    3908             :       {
    3909        3773 :         GEN p1=gcoeff(x,d[i],k);
    3910        3773 :         gel(p,i) = gcopy(p1); gunclone(p1);
    3911             :       }
    3912             :       else
    3913         182 :         gel(p,i) = gen_0;
    3914        1883 :     gel(p,k) = gen_1; for (i=k+1; i<=n; i++) gel(p,i) = gen_0;
    3915             :   }
    3916        1015 :   return gerepileupto(av,y);
    3917             : }
    3918             : 
    3919             : static GEN
    3920          84 : RgM_ker_FpM(GEN x, GEN p)
    3921             : {
    3922          84 :   pari_sp av = avma;
    3923             :   ulong pp;
    3924          84 :   x = RgM_Fp_init3(x, p, &pp);
    3925          84 :   switch(pp)
    3926             :   {
    3927          35 :     case 0: x = FpM_to_mod(FpM_ker_gen(x,p,0),p); break;
    3928           7 :     case 2: x = F2m_to_mod(F2m_ker_sp(x,0)); break;
    3929           7 :     case 3: x = F3m_to_mod(F3m_ker_sp(x,0)); break;
    3930          35 :     default:x = Flm_to_mod(Flm_ker_sp(x,pp,0), pp); break;
    3931             :   }
    3932          84 :   return gerepileupto(av, x);
    3933             : }
    3934             : 
    3935             : static GEN
    3936          91 : RgM_ker_FqM(GEN x, GEN pol, GEN p)
    3937             : {
    3938          91 :   pari_sp av = avma;
    3939          91 :   GEN b, T = RgX_to_FpX(pol, p);
    3940          91 :   if (signe(T) == 0) pari_err_OP("ker",x,pol);
    3941          84 :   b = FqM_ker(RgM_to_FqM(x, T, p), T, p);
    3942          84 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    3943             : }
    3944             : 
    3945             : #define code(t1,t2) ((t1 << 6) | t2)
    3946             : static GEN
    3947        8484 : RgM_ker_fast(GEN x)
    3948             : {
    3949             :   GEN p, pol;
    3950             :   long pa;
    3951        8484 :   long t = RgM_type(x, &p,&pol,&pa);
    3952        8484 :   switch(t)
    3953             :   {
    3954        7462 :     case t_INT:    /* fall through */
    3955        7462 :     case t_FRAC:   return QM_ker(x);
    3956          77 :     case t_FFELT:  return FFM_ker(x, pol);
    3957          84 :     case t_INTMOD: return RgM_ker_FpM(x, p);
    3958          91 :     case code(t_POLMOD, t_INTMOD):
    3959          91 :                    return RgM_ker_FqM(x, pol, p);
    3960         770 :     default:       return NULL;
    3961             :   }
    3962             : }
    3963             : #undef code
    3964             : 
    3965             : GEN
    3966        8484 : ker(GEN x)
    3967             : {
    3968        8484 :   GEN b = RgM_ker_fast(x);
    3969        8477 :   if (b) return b;
    3970         770 :   return ker_aux(x,x);
    3971             : }
    3972             : 
    3973             : GEN
    3974       46221 : matker0(GEN x,long flag)
    3975             : {
    3976       46221 :   if (typ(x)!=t_MAT) pari_err_TYPE("matker",x);
    3977       46221 :   if (!flag) return ker(x);
    3978       45934 :   RgM_check_ZM(x, "matker");
    3979       45934 :   return ZM_ker(x);
    3980             : }
    3981             : 
    3982             : static GEN
    3983          63 : RgM_image_FpM(GEN x, GEN p)
    3984             : {
    3985          63 :   pari_sp av = avma;
    3986             :   ulong pp;
    3987          63 :   x = RgM_Fp_init(x, p, &pp);
    3988          63 :   switch(pp)
    3989             :   {
    3990          28 :     case 0: x = FpM_to_mod(FpM_image(x,p),p); break;
    3991           7 :     case 2: x = F2m_to_mod(F2m_image(x)); break;
    3992          28 :     default:x = Flm_to_mod(Flm_image(x,pp), pp); break;
    3993             :   }
    3994          63 :   return gerepileupto(av, x);
    3995             : }
    3996             : 
    3997             : static GEN
    3998          35 : RgM_image_FqM(GEN x, GEN pol, GEN p)
    3999             : {
    4000          35 :   pari_sp av = avma;
    4001          35 :   GEN b, T = RgX_to_FpX(pol, p);
    4002          35 :   if (signe(T) == 0) pari_err_OP("image",x,pol);
    4003          28 :   b = FqM_image(RgM_to_FqM(x, T, p), T, p);
    4004          28 :   return gerepileupto(av, FqM_to_mod(b, T, p));
    4005             : }
    4006             : 
    4007             : GEN
    4008        5971 : QM_image_shallow(GEN A)
    4009             : {
    4010        5971 :   A = vec_Q_primpart(A);
    4011        5971 :   return vecpermute(A, ZM_indeximage(A));
    4012             : }
    4013             : GEN
    4014        5201 : QM_image(GEN A)
    4015             : {
    4016        5201 :   pari_sp av = avma;
    4017        5201 :   return gerepilecopy(av, QM_image_shallow(A));
    4018             : }
    4019             : 
    4020             : #define code(t1,t2) ((t1 << 6) | t2)
    4021             : static GEN
    4022        5362 : RgM_image_fast(GEN x)
    4023             : {
    4024             :   GEN p, pol;
    4025             :   long pa;
    4026        5362 :   long t = RgM_type(x, &p,&pol,&pa);
    4027        5362 :   switch(t)
    4028             :   {
    4029        5201 :     case t_INT:    /* fall through */
    4030        5201 :     case t_FRAC:   return QM_image(x);
    4031          49 :     case t_FFELT:  return FFM_image(x, pol);
    4032          63 :     case t_INTMOD: return RgM_image_FpM(x, p);
    4033          35 :     case code(t_POLMOD, t_INTMOD):
    4034          35 :                    return RgM_image_FqM(x, pol, p);
    4035          14 :     default:       return NULL;
    4036             :   }
    4037             : }
    4038             : #undef code
    4039             : 
    4040             : GEN
    4041        5362 : image(GEN x)
    4042             : {
    4043             :   GEN d, M;
    4044             :   long r;
    4045             : 
    4046        5362 :   if (typ(x)!=t_MAT) pari_err_TYPE("matimage",x);
    4047        5362 :   M = RgM_image_fast(x);
    4048        5355 :   if (M) return M;
    4049          14 :   d = gauss_pivot(x,&r); /* d left on stack for efficiency */
    4050          14 :   return image_from_pivot(x,d,r);
    4051             : }
    4052             : 
    4053             : static GEN
    4054          84 : imagecompl_aux(GEN x, GEN(*PIVOT)(GEN,long*))
    4055             : {
    4056          84 :   pari_sp av = avma;
    4057             :   GEN d,y;
    4058             :   long j,i,r;
    4059             : 
    4060          84 :   if (typ(x)!=t_MAT) pari_err_TYPE("imagecompl",x);
    4061          84 :   (void)new_chunk(lg(x) * 4 + 1); /* HACK */
    4062          84 :   d = PIVOT(x,&r); /* if (!d) then r = 0 */
    4063          84 :   set_avma(av); y = cgetg(r+1,t_VECSMALL);
    4064         126 :   for (i=j=1; j<=r; i++)
    4065          42 :     if (!d[i]) y[j++] = i;
    4066          84 :   return y;
    4067             : }
    4068             : GEN
    4069          84 : imagecompl(GEN x) { return imagecompl_aux(x, &gauss_pivot); }
    4070             : GEN
    4071           0 : ZM_imagecompl(GEN x) { return imagecompl_aux(x, &ZM_pivots); }
    4072             : 
    4073             : static GEN
    4074          28 : RgM_RgC_invimage_FpC(GEN A, GEN y, GEN p)
    4075             : {
    4076          28 :   pari_sp av = avma;
    4077             :   ulong pp;
    4078             :   GEN x;
    4079          28 :   A = RgM_Fp_init(A,p,&pp);
    4080          28 :   switch(pp)
    4081             :   {
    4082           7 :   case 0:
    4083           7 :     y = RgC_to_FpC(y,p);
    4084           7 :     x = FpM_FpC_invimage(A, y, p);
    4085           7 :     return x ? gerepileupto(av, FpC_to_mod(x,p)): NULL;
    4086           7 :   case 2:
    4087           7 :     y = RgV_to_F2v(y);
    4088           7 :     x = F2m_F2c_invimage(A, y);
    4089           7 :     return x ? gerepileupto(av, F2c_to_mod(x)): NULL;
    4090          14 :   default:
    4091          14 :     y = RgV_to_Flv(y,pp);
    4092          14 :     x = Flm_Flc_invimage(A, y, pp);
    4093          14 :     return x ? gerepileupto(av, Flc_to_mod(x,pp)): NULL;
    4094             :   }
    4095             : }
    4096             : 
    4097             : static GEN
    4098        2149 : RgM_RgC_invimage_fast(GEN x, GEN y)
    4099             : {
    4100             :   GEN p, pol;
    4101             :   long pa;
    4102        2149 :   long t = RgM_RgC_type(x, y, &p,&pol,&pa);
    4103        2149 :   switch(t)
    4104             :   {
    4105          28 :     case t_INTMOD: return RgM_RgC_invimage_FpC(x, y, p);
    4106          63 :     case t_FFELT:  return FFM_FFC_invimage(x, y, pol);
    4107        2058 :     default:       return gen_0;
    4108             :   }
    4109             : }
    4110             : 
    4111             : GEN
    4112        2254 : RgM_RgC_invimage(GEN A, GEN y)
    4113             : {
    4114        2254 :   pari_sp av = avma;
    4115        2254 :   long i, l = lg(A);
    4116             :   GEN M, x, t;
    4117        2254 :   if (l==1) return NULL;
    4118        2149 :   if (lg(y) != lgcols(A)) pari_err_DIM("inverseimage");
    4119        2149 :   M = RgM_RgC_invimage_fast(A, y);
    4120        2149 :   if (!M) return gc_NULL(av);
    4121        2128 :   if (M != gen_0) return M;
    4122        2058 :   M = ker(shallowconcat(A, y));
    4123        2058 :   i = lg(M)-1;
    4124        2058 :   if (!i) return gc_NULL(av);
    4125             : 
    4126        1799 :   x = gel(M,i); t = gel(x,l);
    4127        1799 :   if (gequal0(t)) return gc_NULL(av);
    4128             : 
    4129        1764 :   t = gneg_i(t); setlg(x,l);
    4130        1764 :   return gerepileupto(av, RgC_Rg_div(x, t));
    4131             : }
    4132             : 
    4133             : /* Return X such that m X = v (t_COL or t_MAT), resp. an empty t_COL / t_MAT
    4134             :  * if no solution exist */
    4135             : GEN
    4136        2415 : inverseimage(GEN m, GEN v)
    4137             : {
    4138             :   GEN y;
    4139        2415 :   if (typ(m)!=t_MAT) pari_err_TYPE("inverseimage",m);
    4140        2415 :   switch(typ(v))
    4141             :   {
    4142        2177 :     case t_COL:
    4143        2177 :       y = RgM_RgC_invimage(m,v);
    4144        2177 :       return y? y: cgetg(1,t_COL);
    4145         238 :     case t_MAT:
    4146         238 :       y = RgM_invimage(m, v);
    4147         238 :       return y? y: cgetg(1,t_MAT);
    4148             :   }
    4149           0 :   pari_err_TYPE("inverseimage",v);
    4150             :   return NULL;/*LCOV_EXCL_LINE*/
    4151             : }
    4152             : 
    4153             : static GEN
    4154          84 : RgM_invimage_FpM(GEN A, GEN B, GEN p)
    4155             : {
    4156          84 :   pari_sp av = avma;
    4157             :   ulong pp;
    4158             :   GEN x;
    4159          84 :   A = RgM_Fp_init(A,p,&pp);
    4160          84 :   switch(pp)
    4161             :   {
    4162          35 :   case 0:
    4163          35 :     B = RgM_to_FpM(B,p);
    4164          35 :     x = FpM_invimage_gen(A, B, p);
    4165          35 :     return x ? gerepileupto(av, FpM_to_mod(x, p)): x;
    4166           7 :   case 2:
    4167           7 :     B = RgM_to_F2m(B);
    4168           7 :     x = F2m_invimage_i(A, B);
    4169           7 :     return x ? gerepileupto(av, F2m_to_mod(x)): x;
    4170          42 :   default:
    4171          42 :     B = RgM_to_Flm(B,pp);
    4172          42 :     x = Flm_invimage_i(A, B, pp);
    4173          42 :     return x ? gerepileupto(av, Flm_to_mod(x, pp)): x;
    4174             :   }
    4175             : }
    4176             : 
    4177             : static GEN
    4178         252 : RgM_invimage_fast(GEN x, GEN y)
    4179             : {
    4180             :   GEN p, pol;
    4181             :   long pa;
    4182         252 :   long t = RgM_type2(x, y, &p,&pol,&pa);
    4183         252 :   switch(t)
    4184             :   {
    4185          84 :     case t_INTMOD: return RgM_invimage_FpM(x, y, p);
    4186         105 :     case t_FFELT:  return FFM_invimage(x, y, pol);
    4187          63 :     default:       return gen_0;
    4188             :   }
    4189             : }
    4190             : 
    4191             : /* find Z such that A Z = B. Return NULL if no solution */
    4192             : GEN
    4193         252 : RgM_invimage(GEN A, GEN B)
    4194             : {
    4195         252 :   pari_sp av = avma;
    4196             :   GEN d, x, X, Y;
    4197         252 :   long i, j, nY, nA = lg(A)-1, nB = lg(B)-1;
    4198         252 :   X = RgM_invimage_fast(A, B);
    4199         252 :   if (!X) return gc_NULL(av);
    4200         140 :   if (X != gen_0) return X;
    4201          63 :   x = ker(shallowconcat(RgM_neg(A), B));
    4202             :   /* AX = BY, Y in strict upper echelon form with pivots = 1.
    4203             :    * We must find T such that Y T = Id_nB then X T = Z. This exists iff
    4204             :    * Y has at least nB columns and full rank */
    4205          63 :   nY = lg(x)-1;
    4206          63 :   if (nY < nB) return gc_NULL(av);
    4207          49 :   Y = rowslice(x, nA+1, nA+nB); /* nB rows */
    4208          49 :   d = cgetg(nB+1, t_VECSMALL);
    4209         441 :   for (i = nB, j = nY; i >= 1; i--, j--)
    4210             :   {
    4211         546 :     for (; j>=1; j--)
    4212         532 :       if (!gequal0(gcoeff(Y,i,j))) { d[i] = j; break; }
    4213         406 :     if (!j) return gc_NULL(av);
    4214             :   }
    4215             :   /* reduce to the case Y square, upper triangular with 1s on diagonal */
    4216          35 :   Y = vecpermute(Y, d);
    4217          35 :   x = vecpermute(x, d);
    4218          35 :   X = rowslice(x, 1, nA);
    4219          35 :   return gerepileupto(av, RgM_mul(X, RgM_inv_upper(Y)));
    4220             : }
    4221             : 
    4222             : static GEN
    4223          70 : RgM_suppl_FpM(GEN x, GEN p)
    4224             : {
    4225          70 :   pari_sp av = avma;
    4226             :   ulong pp;
    4227          70 :   x = RgM_Fp_init(x, p, &pp);
    4228          70 :   switch(pp)
    4229             :   {
    4230          21 :   case 0: x = FpM_to_mod(FpM_suppl(x,p), p); break;
    4231          14 :   case 2: x = F2m_to_mod(F2m_suppl(x)); break;
    4232          35 :   default:x = Flm_to_mod(Flm_suppl(x,pp), pp); break;
    4233             :   }
    4234          70 :   return gerepileupto(av, x);
    4235             : }
    4236             : 
    4237             : static GEN
    4238         175 : RgM_suppl_fast(GEN x)
    4239             : {
    4240             :   GEN p, pol;
    4241             :   long pa;
    4242         175 :   long t = RgM_type(x,&p,&pol,&pa);
    4243         175 :   switch(t)
    4244             :   {
    4245          70 :     case t_INTMOD: return RgM_suppl_FpM(x, p);
    4246          35 :     case t_FFELT:  return FFM_suppl(x, pol);
    4247          70 :     default:       return NULL;
    4248             :   }
    4249             : }
    4250             : 
    4251             : /* x is an n x k matrix, rank(x) = k <= n. Return an invertible n x n matrix
    4252             :  * whose first k columns are given by x. If rank(x) < k, undefined result. */
    4253             : GEN
    4254         175 : suppl(GEN x)
    4255             : {
    4256         175 :   pari_sp av = avma;
    4257             :   GEN d, M;
    4258             :   long r;
    4259         175 :   if (typ(x)!=t_MAT) pari_err_TYPE("suppl",x);
    4260         175 :   M = RgM_suppl_fast(x);
    4261         175 :   if (M) return M;
    4262          70 :   init_suppl(x);
    4263          70 :   d = gauss_pivot(x,&r);
    4264          70 :   set_avma(av); return get_suppl(x,d,nbrows(x),r,&col_ei);
    4265             : }
    4266             : 
    4267             : GEN
    4268           7 : image2(GEN x)
    4269             : {
    4270           7 :   pari_sp av = avma;
    4271             :   long k, n, i;
    4272             :   GEN A, B;
    4273             : 
    4274           7 :   if (typ(x)!=t_MAT) pari_err_TYPE("image2",x);
    4275           7 :   if (lg(x) == 1) return cgetg(1,t_MAT);
    4276           7 :   A = ker(x); k = lg(A)-1;
    4277           7 :   if (!k) { set_avma(av); return gcopy(x); }
    4278           7 :   A = suppl(A); n = lg(A)-1;
    4279           7 :   B = cgetg(n-k+1, t_MAT);
    4280          21 :   for (i = k+1; i <= n; i++) gel(B,i-k) = RgM_RgC_mul(x, gel(A,i));
    4281           7 :   return gerepileupto(av, B);
    4282             : }
    4283             : 
    4284             : GEN
    4285         210 : matimage0(GEN x,long flag)
    4286             : {
    4287         210 :   switch(flag)
    4288             :   {
    4289         203 :     case 0: return image(x);
    4290           7 :     case 1: return image2(x);
    4291           0 :     default: pari_err_FLAG("matimage");
    4292             :   }
    4293             :   return NULL; /* LCOV_EXCL_LINE */
    4294             : }
    4295             : 
    4296             : static long
    4297         126 : RgM_rank_FpM(GEN x, GEN p)
    4298             : {
    4299         126 :   pari_sp av = avma;
    4300             :   ulong pp;
    4301             :   long r;
    4302         126 :   x = RgM_Fp_init(x,p,&pp);
    4303         126 :   switch(pp)
    4304             :   {
    4305          28 :   case 0: r = FpM_rank(x,p); break;
    4306          63 :   case 2: r = F2m_rank(x); break;
    4307          35 :   default:r = Flm_rank(x,pp); break;
    4308             :   }
    4309         126 :   return gc_long(av, r);
    4310             : }
    4311             : 
    4312             : static long
    4313          49 : RgM_rank_FqM(GEN x, GEN pol, GEN p)
    4314             : {
    4315          49 :   pari_sp av = avma;
    4316             :   long r;
    4317          49 :   GEN T = RgX_to_FpX(pol, p);
    4318          49 :   if (signe(T) == 0) pari_err_OP("rank",x,pol);
    4319          42 :   r = FqM_rank(RgM_to_FqM(x, T, p), T, p);
    4320          42 :   return gc_long(av,r);
    4321             : }
    4322             : 
    4323             : #define code(t1,t2) ((t1 << 6) | t2)
    4324             : static long
    4325         294 : RgM_rank_fast(GEN x)
    4326             : {
    4327             :   GEN p, pol;
    4328             :   long pa;
    4329         294 :   long t = RgM_type(x,&p,&pol,&pa);
    4330         294 :   switch(t)
    4331             :   {
    4332          42 :     case t_INT:    return ZM_rank(x);
    4333           0 :     case t_FRAC:   return QM_rank(x);
    4334         126 :     case t_INTMOD: return RgM_rank_FpM(x, p);
    4335          70 :     case t_FFELT:  return FFM_rank(x, pol);
    4336          49 :     case code(t_POLMOD, t_INTMOD):
    4337          49 :                    return RgM_rank_FqM(x, pol, p);
    4338           7 :     default:       return -1;
    4339             :   }
    4340             : }
    4341             : #undef code
    4342             : 
    4343             : long
    4344         294 : rank(GEN x)
    4345             : {
    4346         294 :   pari_sp av = avma;
    4347             :   long r;
    4348             : 
    4349         294 :   if (typ(x)!=t_MAT) pari_err_TYPE("rank",x);
    4350         294 :   r = RgM_rank_fast(x);
    4351         287 :   if (r >= 0) return r;
    4352           7 :   (void)gauss_pivot(x, &r);
    4353           7 :   return gc_long(av, lg(x)-1 - r);
    4354             : }
    4355             : 
    4356             : /* d a t_VECSMALL of integers in 1..n. Return the vector of the d[i]
    4357             :  * followed by the missing indices */
    4358             : static GEN
    4359       44262 : perm_complete(GEN d, long n)
    4360             : {
    4361       44262 :   GEN y = cgetg(n+1, t_VECSMALL);
    4362       44262 :   long i, j = 1, k = n, l = lg(d);
    4363       44262 :   pari_sp av = avma;
    4364       44262 :   char *T = stack_calloc(n+1);
    4365      221966 :   for (i = 1; i < l; i++) T[d[i]] = 1;
    4366      436651 :   for (i = 1; i <= n; i++)
    4367      392389 :     if (T[i]) y[j++] = i; else y[k--] = i;
    4368       44262 :   return gc_const(av, y);
    4369             : }
    4370             : 
    4371             : /* n = dim x, r = dim Ker(x), d from gauss_pivot */
    4372             : static GEN
    4373        5971 : indeximage0(long n, long r, GEN d)
    4374             : {
    4375             :   long i, j;
    4376             :   GEN v;
    4377             : 
    4378        5971 :   r = n - r; /* now r = dim Im(x) */
    4379        5971 :   v = cgetg(r+1,t_VECSMALL);
    4380       32571 :   if (d) for (i=j=1; j<=n; j++)
    4381       26600 :     if (d[j]) v[i++] = j;
    4382        5971 :   return v;
    4383             : }
    4384             : /* x an m x n t_MAT, n > 0, r = dim Ker(x), d from gauss_pivot */
    4385             : static void
    4386       22131 : indexrank_all(long m, long n, long r, GEN d, GEN *prow, GEN *pcol)
    4387             : {
    4388       22131 :   GEN IR = indexrank0(n, r, d);
    4389       22131 :   *prow = perm_complete(gel(IR,1), m);
    4390       22131 :   *pcol = perm_complete(gel(IR,2), n);
    4391       22131 : }
    4392             : 
    4393             : static GEN
    4394          28 : RgM_indexrank_FpM(GEN x, GEN p)
    4395             : {
    4396          28 :   pari_sp av = avma;
    4397             :   ulong pp;
    4398             :   GEN r;
    4399          28 :   x = RgM_Fp_init(x,p,&pp);
    4400          28 :   switch(pp)
    4401             :   {
    4402           7 :   case 0:  r = FpM_indexrank(x,p); break;
    4403           7 :   case 2:  r = F2m_indexrank(x); break;
    4404          14 :   default: r = Flm_indexrank(x,pp); break;
    4405             :   }
    4406          28 :   return gerepileupto(av, r);
    4407             : }
    4408             : 
    4409             : static GEN
    4410           0 : RgM_indexrank_FqM(GEN x, GEN pol, GEN p)
    4411             : {
    4412           0 :   pari_sp av = avma;
    4413           0 :   GEN r, T = RgX_to_FpX(pol, p);
    4414           0 :   if (signe(T) == 0) pari_err_OP("indexrank",x,pol);
    4415           0 :   r = FqM_indexrank(RgM_to_FqM(x, T, p), T, p);
    4416           0 :   return gerepileupto(av, r);
    4417             : }
    4418             : 
    4419             : #define code(t1,t2) ((t1 << 6) | t2)
    4420             : static GEN
    4421       60507 : RgM_indexrank_fast(GEN x)
    4422             : {
    4423             :   GEN p, pol;
    4424             :   long pa;
    4425       60507 :   long t = RgM_type(x,&p,&pol,&pa);
    4426       60507 :   switch(t)
    4427             :   {
    4428         392 :     case t_INT:    return ZM_indexrank(x);
    4429        1148 :     case t_FRAC:   return QM_indexrank(x);
    4430          28 :     case t_INTMOD: return RgM_indexrank_FpM(x, p);
    4431          21 :     case t_FFELT:  return FFM_indexrank(x, pol);
    4432           0 :     case code(t_POLMOD, t_INTMOD):
    4433           0 :                    return RgM_indexrank_FqM(x, pol, p);
    4434       58918 :     default:       return NULL;
    4435             :   }
    4436             : }
    4437             : #undef code
    4438             : 
    4439             : GEN
    4440       60507 : indexrank(GEN x)
    4441             : {
    4442             :   pari_sp av;
    4443             :   long r;
    4444             :   GEN d;
    4445       60507 :   if (typ(x)!=t_MAT) pari_err_TYPE("indexrank",x);
    4446       60507 :   d = RgM_indexrank_fast(x);
    4447       60507 :   if (d) return d;
    4448       58918 :   av = avma;
    4449       58918 :   init_indexrank(x);
    4450       58918 :   d = gauss_pivot(x, &r);
    4451       58917 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    4452             : }
    4453             : 
    4454             : GEN
    4455        5971 : ZM_indeximage(GEN x) {
    4456        5971 :   pari_sp av = avma;
    4457             :   long r;
    4458             :   GEN d;
    4459        5971 :   init_indexrank(x);
    4460        5971 :   d = ZM_pivots(x,&r);
    4461        5971 :   set_avma(av); return indeximage0(lg(x)-1, r, d);
    4462             : }
    4463             : long
    4464      100646 : ZM_rank(GEN x) {
    4465      100646 :   pari_sp av = avma;
    4466             :   long r;
    4467      100646 :   (void)ZM_pivots(x,&r);
    4468      100648 :   return gc_long(av, lg(x)-1-r);
    4469             : }
    4470             : GEN
    4471       25704 : ZM_indexrank(GEN x) {
    4472       25704 :   pari_sp av = avma;
    4473             :   long r;
    4474             :   GEN d;
    4475       25704 :   init_indexrank(x);
    4476       25704 :   d = ZM_pivots(x,&r);
    4477       25704 :   set_avma(av); return indexrank0(lg(x)-1, r, d);
    4478             : }
    4479             : 
    4480             : long
    4481           0 : QM_rank(GEN x)
    4482             : {
    4483           0 :   pari_sp av = avma;
    4484           0 :   long r = ZM_rank(Q_primpart(x));
    4485           0 :   set_avma(av);
    4486           0 :   return r;
    4487             : }
    4488             : 
    4489             : GEN
    4490        1148 : QM_indexrank(GEN x)
    4491             : {
    4492        1148 :   pari_sp av = avma;
    4493        1148 :   GEN r = ZM_indexrank(Q_primpart(x));
    4494        1148 :   return gerepileupto(av, r);
    4495             : }
    4496             : 
    4497             : /*******************************************************************/
    4498             : /*                                                                 */
    4499             : /*                             ZabM                                */
    4500             : /*                                                                 */
    4501             : /*******************************************************************/
    4502             : 
    4503             : static GEN
    4504        1276 : FpXM_ratlift(GEN a, GEN q)
    4505             : {
    4506             :   GEN B, y;
    4507        1276 :   long i, j, l = lg(a), n;
    4508        1276 :   B = sqrti(shifti(q,-1));
    4509        1276 :   y = cgetg(l, t_MAT);
    4510        1276 :   if (l==1) return y;
    4511        1276 :   n = lgcols(a);
    4512        3059 :   for (i=1; i<l; i++)
    4513             :   {
    4514        2404 :     GEN yi = cgetg(n, t_COL);
    4515       32311 :     for (j=1; j<n; j++)
    4516             :     {
    4517       30528 :       GEN v = FpX_ratlift(gmael(a,i,j), q, B, B, NULL);
    4518       30528 :       if (!v) return NULL;
    4519       29907 :       gel(yi, j) = RgX_renormalize(v);
    4520             :     }
    4521        1783 :     gel(y,i) = yi;
    4522             :   }
    4523         655 :   return y;
    4524             : }
    4525             : 
    4526             : static GEN
    4527        4476 : FlmV_recover_pre(GEN a, GEN M, ulong p, ulong pi, long sv)
    4528             : {
    4529        4476 :   GEN a1 = gel(a,1);
    4530        4476 :   long i, j, k, l = lg(a1), n, lM = lg(M);
    4531        4476 :   GEN v = cgetg(lM, t_VECSMALL);
    4532        4476 :   GEN y = cgetg(l, t_MAT);
    4533        4476 :   if (l==1) return y;
    4534        4476 :   n = lgcols(a1);
    4535       22462 :   for (i=1; i<l; i++)
    4536             :   {
    4537       17985 :     GEN yi = cgetg(n, t_COL);
    4538      347211 :     for (j=1; j<n; j++)
    4539             :     {
    4540     4673669 :       for (k=1; k<lM; k++) uel(v,k) = umael(gel(a,k),i,j);
    4541      329225 :       gel(yi, j) = Flm_Flc_mul_pre_Flx(M, v, p, pi, sv);
    4542             :     }
    4543       17986 :     gel(y,i) = yi;
    4544             :   }
    4545        4477 :   return y;
    4546             : }
    4547             : 
    4548             : static GEN
    4549           0 : FlkM_inv(GEN M, GEN P, ulong p)
    4550             : {
    4551           0 :   ulong pi = get_Fl_red(p);
    4552           0 :   GEN R = Flx_roots(P, p);
    4553           0 :   long l = lg(R), i;
    4554           0 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    4555           0 :   GEN V = cgetg(l, t_VEC);
    4556           0 :   for(i=1; i<l; i++)
    4557             :   {
    4558           0 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, pi);
    4559           0 :     GEN H = Flm_inv_sp(FlxM_eval_powers_pre(M, pows, p, pi), NULL, p);
    4560           0 :     if (!H) return NULL;
    4561           0 :     gel(V, i) = H;
    4562             :   }
    4563           0 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    4564             : }
    4565             : 
    4566             : static GEN
    4567        3200 : FlkM_adjoint(GEN M, GEN P, ulong p)
    4568             : {
    4569        3200 :   ulong pi = get_Fl_red(p);
    4570        3200 :   GEN R = Flx_roots(P, p);
    4571        3201 :   long l = lg(R), i;
    4572        3201 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    4573        3201 :   GEN V = cgetg(l, t_VEC);
    4574       15520 :   for(i=1; i<l; i++)
    4575             :   {
    4576       12320 :     GEN pows = Fl_powers_pre(uel(R,i), degpol(P), p, pi);
    4577       12320 :     gel(V, i) = Flm_adjoint(FlxM_eval_powers_pre(M, pows, p, pi), p);
    4578             :   }
    4579        3200 :   return FlmV_recover_pre(V, W, p, pi, P[1]);
    4580             : }
    4581             : 
    4582             : static GEN
    4583        1978 : ZabM_inv_slice(GEN A, GEN Q, GEN P, GEN *mod)
    4584             : {
    4585        1978 :   pari_sp av = avma;
    4586        1978 :   long i, n = lg(P)-1, w = varn(Q);
    4587             :   GEN H, T;
    4588        1978 :   if (n == 1)
    4589             :   {
    4590        1548 :     ulong p = uel(P,1);
    4591        1548 :     GEN Qp = ZX_to_Flx(Q, p);
    4592        1548 :     GEN Ap = ZXM_to_FlxM(A, p, get_Flx_var(Qp));
    4593        1548 :     GEN Hp = FlkM_adjoint(Ap, Qp, p);
    4594        1548 :     Hp = gerepileupto(av, FlxM_to_ZXM(Hp));
    4595        1548 :     *mod = utoipos(p); return Hp;
    4596             :   }
    4597         430 :   T = ZV_producttree(P);
    4598         430 :   A = ZXM_nv_mod_tree(A, P, T, w);
    4599         430 :   Q = ZX_nv_mod_tree(Q, P, T);
    4600         430 :   H = cgetg(n+1, t_VEC);
    4601        2083 :   for(i=1; i <= n; i++)
    4602             :   {
    4603        1653 :     ulong p = P[i];
    4604        1653 :     GEN a = gel(A,i), q = gel(Q, i);
    4605        1653 :     gel(H,i) = FlkM_adjoint(a, q, p);
    4606             :   }
    4607         430 :   H = nxMV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P,T));
    4608         430 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    4609             : }
    4610             : 
    4611             : GEN
    4612        1978 : ZabM_inv_worker(GEN P, GEN A, GEN Q)
    4613             : {
    4614        1978 :   GEN V = cgetg(3, t_VEC);
    4615        1978 :   gel(V,1) = ZabM_inv_slice(A, Q, P, &gel(V,2));
    4616        1978 :   return V;
    4617             : }
    4618             : 
    4619             : static GEN
    4620        5467 : vecnorml1(GEN a)
    4621             : {
    4622             :   long i, l;
    4623        5467 :   GEN g = cgetg_copy(a, &l);
    4624       60214 :   for (i=1; i<l; i++)
    4625       54747 :     gel(g, i) = gnorml1_fake(gel(a,i));
    4626        5467 :   return g;
    4627             : }
    4628             : 
    4629             : static GEN
    4630        1820 : ZabM_true_Hadamard(GEN a)
    4631             : {
    4632        1820 :   pari_sp av = avma;
    4633        1820 :   long n = lg(a)-1, i;
    4634             :   GEN B;
    4635        1820 :   if (n == 0) return gen_1;
    4636        1820 :   if (n == 1) return gnorml1_fake(gcoeff(a,1,1));
    4637        1176 :   B = gen_1;
    4638        6643 :   for (i = 1; i <= n; i++)
    4639        5467 :     B = gmul(B, gnorml2(RgC_gtofp(vecnorml1(gel(a,i)),DEFAULTPREC)));
    4640        1176 :   return gerepileuptoint(av, ceil_safe(sqrtr_abs(B)));
    4641             : }
    4642             : 
    4643             : GEN
    4644        1820 : ZabM_inv(GEN A, GEN Q, long n, GEN *pt_den)
    4645             : {
    4646        1820 :   pari_sp av = avma;
    4647             :   forprime_t S;
    4648             :   GEN bnd, H, D, d, mod, worker;
    4649        1820 :   if (lg(A) == 1)
    4650             :   {
    4651           0 :     if (pt_den) *pt_den = gen_1;
    4652           0 :     return cgetg(1, t_MAT);
    4653             :   }
    4654        1820 :   bnd = ZabM_true_Hadamard(A);
    4655        1820 :   worker = snm_closure(is_entry("_ZabM_inv_worker"), mkvec2(A, Q));
    4656        1820 :   u_forprime_arith_init(&S, HIGHBIT+1, ULONG_MAX, 1, n);
    4657        1820 :   H = gen_crt("ZabM_inv", worker, &S, NULL, expi(bnd), 0, &mod,
    4658             :               nxMV_chinese_center, FpXM_center);
    4659        1820 :   D = RgMrow_RgC_mul(H, gel(A,1), 1);
    4660        1820 :   D = ZX_rem(D, Q);
    4661        1820 :   d = Z_content(mkvec2(H, D));
    4662        1820 :   if (d)
    4663             :   {
    4664         511 :     D = ZX_Z_divexact(D, d);
    4665         511 :     H = Q_div_to_int(H, d);
    4666             :   }
    4667        1820 :   if (!pt_den) return gerepileupto(av, H);
    4668        1820 :   *pt_den = D; return gc_all(av, 2, &H, pt_den);
    4669             : }
    4670             : 
    4671             : GEN
    4672           0 : ZabM_inv_ratlift(GEN M, GEN P, long n, GEN *pden)
    4673             : {
    4674           0 :   pari_sp av2, av = avma;
    4675             :   GEN q, H;
    4676           0 :   ulong m = LONG_MAX>>1;
    4677           0 :   ulong p= 1 + m - (m % n);
    4678           0 :   long lM = lg(M);
    4679           0 :   if (lM == 1) { *pden = gen_1; return cgetg(1,t_MAT); }
    4680             : 
    4681           0 :   av2 = avma;
    4682           0 :   H = NULL;
    4683             :   for(;;)
    4684           0 :   {
    4685             :     GEN Hp, Pp, Mp, Hr;
    4686           0 :     do p += n; while(!uisprime(p));
    4687           0 :     Pp = ZX_to_Flx(P, p);
    4688           0 :     Mp = ZXM_to_FlxM(M, p, get_Flx_var(Pp));
    4689           0 :     Hp = FlkM_inv(Mp, Pp, p);
    4690           0 :     if (!Hp) continue;
    4691           0 :     if (!H)
    4692             :     {
    4693           0 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p);
    4694           0 :       q = utoipos(p);
    4695             :     }
    4696             :     else
    4697           0 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    4698           0 :     Hr = FpXM_ratlift(H, q);
    4699           0 :     if (DEBUGLEVEL>5) err_printf("ZabM_inv mod %ld (ratlift=%ld)\n", p,!!Hr);
    4700           0 :     if (Hr) {/* DONE ? */
    4701           0 :       GEN Hl = Q_remove_denom(Hr, pden);
    4702           0 :       GEN MH = ZXQM_mul(Hl, M, P);
    4703           0 :       if (*pden)
    4704           0 :       { if (RgM_isscalar(MH, *pden)) { H = Hl; break; }}
    4705             :       else
    4706           0 :       { if (RgM_isidentity(MH)) { H = Hl; *pden = gen_1; break; } }
    4707             :     }
    4708             : 
    4709           0 :     if (gc_needed(av,2))
    4710             :     {
    4711           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_inv");
    4712           0 :       gerepileall(av2, 2, &H, &q);
    4713             :     }
    4714             :   }
    4715           0 :   return gc_all(av, 2, &H, pden);
    4716             : }
    4717             : 
    4718             : static GEN
    4719        1276 : FlkM_ker(GEN M, GEN P, ulong p)
    4720             : {
    4721        1276 :   ulong pi = get_Fl_red(p);
    4722        1276 :   GEN R = Flx_roots(P, p);
    4723        1276 :   long l = lg(R), i, dP = degpol(P), r;
    4724             :   GEN M1, K, D;
    4725        1276 :   GEN W = Flv_invVandermonde(R, 1UL, p);
    4726        1276 :   GEN V = cgetg(l, t_VEC);
    4727        1276 :   M1 = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,1), dP, p, pi), p, pi);
    4728        1276 :   K = Flm_ker_sp(M1, p, 2);
    4729        1276 :   r = lg(gel(K,1)); D = gel(K,2);
    4730        1276 :   gel(V, 1) = gel(K,1);
    4731        2652 :   for(i=2; i<l; i++)
    4732             :   {
    4733        1376 :     GEN Mi = FlxM_eval_powers_pre(M, Fl_powers_pre(uel(R,i), dP, p, pi), p, pi);
    4734        1376 :     GEN K = Flm_ker_sp(Mi, p, 2);
    4735        1376 :     if (lg(gel(K,1)) != r || !zv_equal(D, gel(K,2))) return NULL;
    4736        1376 :     gel(V, i) = gel(K,1);
    4737             :   }
    4738        1276 :   return mkvec2(FlmV_recover_pre(V, W, p, pi, P[1]), D);
    4739             : }
    4740             : 
    4741             : static int
    4742         655 : ZabM_ker_check(GEN M, GEN H, ulong p, GEN P, long n)
    4743             : {
    4744             :   GEN pow;
    4745         655 :   long j, l = lg(H);
    4746             :   ulong pi, r;
    4747        3899 :   do p += n; while(!uisprime(p));
    4748         655 :   pi = get_Fl_red(p);
    4749         655 :   P = ZX_to_Flx(P, p);
    4750         655 :   r = Flx_oneroot(P, p);
    4751         655 :   pow = Fl_powers_pre(r, degpol(P),p,pi);
    4752         655 :   M = ZXM_to_FlxM(M, p, P[1]); M = FlxM_eval_powers_pre(M, pow, p, pi);
    4753         655 :   H = ZXM_to_FlxM(H, p, P[1]); H = FlxM_eval_powers_pre(H, pow, p, pi);
    4754        2178 :   for (j = 1; j < l; j++)
    4755        1555 :     if (!zv_equal0(Flm_Flc_mul_pre(M, gel(H,j), p, pi))) return 0;
    4756         623 :   return 1;
    4757             : }
    4758             : 
    4759             : GEN
    4760         623 : ZabM_ker(GEN M, GEN P, long n)
    4761             : {
    4762         623 :   pari_sp av = avma;
    4763             :   pari_timer ti;
    4764         623 :   GEN q, H = NULL, D = NULL;
    4765         623 :   ulong m = LONG_MAX>>1;
    4766         623 :   ulong p = 1 + m - (m % n);
    4767             : 
    4768         623 :   if (DEBUGLEVEL>5) timer_start(&ti);
    4769             :   for(;;)
    4770         653 :   {
    4771             :     GEN Kp, Hp, Dp, Pp, Mp, Hr;
    4772       22341 :     do p += n; while(!uisprime(p));
    4773        1276 :     Pp = ZX_to_Flx(P, p);
    4774        1276 :     Mp = ZXM_to_FlxM(M, p, get_Flx_var(Pp));
    4775        1276 :     Kp = FlkM_ker(Mp, Pp, p);
    4776        1276 :     if (!Kp) continue;
    4777        1276 :     Hp = gel(Kp,1); Dp = gel(Kp,2);
    4778        1276 :     if (H && (lg(Hp)>lg(H) || (lg(Hp)==lg(H) && vecsmall_lexcmp(Dp,D)>0))) continue;
    4779        1276 :     if (!H || (lg(Hp)<lg(H) || vecsmall_lexcmp(Dp,D)<0))
    4780             :     {
    4781         623 :       H = ZXM_init_CRT(Hp, degpol(P)-1, p); D = Dp;
    4782         623 :       q = utoipos(p);
    4783             :     }
    4784             :     else
    4785         653 :       ZXM_incremental_CRT(&H, Hp, &q, p);
    4786        1276 :     Hr = FpXM_ratlift(H, q);
    4787        1276 :     if (DEBUGLEVEL>5) timer_printf(&ti,"ZabM_ker mod %ld (ratlift=%ld)", p,!!Hr);
    4788        1276 :     if (Hr) {/* DONE ? */
    4789         655 :       GEN Hl = vec_Q_primpart(Hr);
    4790         655 :       if (ZabM_ker_check(M, Hl, p, P, n)) { H = Hl;  break; }
    4791             :     }
    4792             : 
    4793         653 :     if (gc_needed(av,2))
    4794             :     {
    4795           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"ZabM_ker");
    4796           0 :       gerepileall(av, 3, &H, &D, &q);
    4797             :     }
    4798             :   }
    4799         623 :   return gerepilecopy(av, H);
    4800             : }
    4801             : 
    4802             : GEN
    4803        2387 : ZabM_indexrank(GEN M, GEN P, long n)
    4804             : {
    4805        2387 :   pari_sp av = avma;
    4806        2387 :   ulong m = LONG_MAX>>1;
    4807        2387 :   ulong p = 1+m-(m%n), D = degpol(P);
    4808        2387 :   long lM = lg(M), lmax = 0, c = 0;
    4809             :   GEN v;
    4810             :   for(;;)
    4811         735 :   {
    4812             :     GEN R, Pp, Mp, K;
    4813             :     ulong pi;
    4814             :     long l;
    4815       61415 :     do p += n; while (!uisprime(p));
    4816        3122 :     pi = get_Fl_red(p);
    4817        3122 :     Pp = ZX_to_Flx(P, p);
    4818        3122 :     R = Flx_roots(Pp, p);
    4819        3122 :     Mp = ZXM_to_FlxM(M, p, get_Flx_var(Pp));
    4820        3122 :     K = FlxM_eval_powers_pre(Mp, Fl_powers_pre(uel(R,1), D,p,pi), p,pi);
    4821        3122 :     v = Flm_indexrank(K, p);
    4822        3122 :     l = lg(gel(v,2));
    4823        3122 :     if (l == lM) break;
    4824         980 :     if (lmax >= 0 && l > lmax) { lmax = l; c = 0; } else c++;
    4825         980 :     if (c > 2)
    4826             :     { /* probably not maximal rank, expensive check */
    4827         245 :       lM -= lg(ZabM_ker(M, P, n))-1; /* actual rank (+1) */
    4828         245 :       if (lmax == lM) break;
    4829           0 :       lmax = -1; /* disable check */
    4830             :     }
    4831             :   }
    4832        2387 :   return gerepileupto(av, v);
    4833             : }
    4834             : 
    4835             : #if 0
    4836             : GEN
    4837             : ZabM_gauss(GEN M, GEN P, long n, GEN *den)
    4838             : {
    4839             :   pari_sp av = avma;
    4840             :   GEN v, S, W;
    4841             :   v = ZabM_indexrank(M, P, n);
    4842             :   S = shallowmatextract(M,gel(v,1),gel(v,2));
    4843             :   W = ZabM_inv(S, P, n, den);
    4844             :   return gc_all(av,2,&W,den);
    4845             : }
    4846             : #endif
    4847             : 
    4848             : GEN
    4849         140 : ZabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *den)
    4850             : {
    4851         140 :   GEN v = ZabM_indexrank(M, P, n);
    4852         140 :   if (pv) *pv = v;
    4853         140 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    4854         140 :   return ZabM_inv(M, P, n, den);
    4855             : }
    4856             : GEN
    4857        4977 : ZM_pseudoinv(GEN M, GEN *pv, GEN *den)
    4858             : {
    4859        4977 :   GEN v = ZM_indexrank(M);
    4860        4977 :   if (pv) *pv = v;
    4861        4977 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
    4862        4977 :   return ZM_inv(M, den);
    4863             : }
    4864             : 
    4865             : /*******************************************************************/
    4866             : /*                                                                 */
    4867             : /*                   Structured Elimination                        */
    4868             : /*                                                                 */
    4869             : /*******************************************************************/
    4870             : 
    4871             : static void
    4872      111654 : rem_col(GEN c, long i, GEN iscol, GEN Wrow, long *rcol, long *rrow)
    4873             : {
    4874      111654 :   long lc = lg(c), k;
    4875      111654 :   iscol[i] = 0; (*rcol)--;
    4876     1078678 :   for (k = 1; k < lc; ++k)
    4877             :   {
    4878      967024 :     Wrow[c[k]]--;
    4879      967024 :     if (Wrow[c[k]]==0) (*rrow)--;
    4880             :   }
    4881      111654 : }
    4882             : 
    4883             : static void
    4884        6278 : rem_singleton(GEN M, GEN iscol, GEN Wrow, long idx, long *rcol, long *rrow)
    4885             : {
    4886             :   long i, j;
    4887        6278 :   long nbcol = lg(iscol)-1, last;
    4888             :   do
    4889             :   {
    4890        8353 :     last = 0;
    4891    19439662 :     for (i = 1; i <= nbcol; ++i)
    4892    19431309 :       if (iscol[i])
    4893             :       {
    4894    10129482 :         GEN c = idx ? gmael(M, i, idx): gel(M,i);
    4895    10129482 :         long lc = lg(c);
    4896    97291642 :         for (j = 1; j < lc; ++j)
    4897    87184471 :           if (Wrow[c[j]] == 1)
    4898             :           {
    4899       22311 :             rem_col(c, i, iscol, Wrow, rcol, rrow);
    4900       22311 :             last=1; break;
    4901             :           }
    4902             :       }
    4903        8353 :   } while (last);
    4904        6278 : }
    4905             : 
    4906             : static GEN
    4907        6106 : fill_wcol(GEN M, GEN iscol, GEN Wrow, long *w, GEN wcol)
    4908             : {
    4909        6106 :   long nbcol = lg(iscol)-1;
    4910             :   long i, j, m, last;
    4911             :   GEN per;
    4912       15300 :   for (m = 2, last=0; !last ; m++)
    4913             :   {
    4914    22448205 :     for (i = 1; i <= nbcol; ++i)
    4915             :     {
    4916    22439011 :       wcol[i] = 0;
    4917    22439011 :       if (iscol[i])
    4918             :       {
    4919    11521072 :         GEN c = gmael(M, i, 1);
    4920    11521072 :         long lc = lg(c);
    4921   107254564 :         for (j = 1; j < lc; ++j)
    4922    95733492 :           if (Wrow[c[j]] == m) {  wcol[i]++; last = 1; }
    4923             :       }
    4924             :     }
    4925             :   }
    4926        6106 :   per = vecsmall_indexsort(wcol);
    4927        6106 :   *w = wcol[per[nbcol]];
    4928        6106 :   return per;
    4929             : }
    4930             : 
    4931             : /* M is a RgMs with nbrow rows, A a list of row indices.
    4932             :    Eliminate rows of M with a single entry that do not belong to A,
    4933             :    and the corresponding columns. Also eliminate columns until #colums=#rows.
    4934             :    Return pcol and prow:
    4935             :    pcol is a map from the new columns indices to the old one.
    4936             :    prow is a map from the old rows indices to the new one (0 if removed).
    4937             : */
    4938             : 
    4939             : void
    4940         119 : RgMs_structelim_col(GEN M, long nbcol, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    4941             : {
    4942         119 :   long i, j, k, lA = lg(A);
    4943         119 :   GEN prow = cgetg(nbrow+1, t_VECSMALL);
    4944         119 :   GEN pcol = zero_zv(nbcol);
    4945         119 :   pari_sp av = avma;
    4946         119 :   long rcol = nbcol, rrow = 0, imin = nbcol - usqrt(nbcol);
    4947         119 :   GEN iscol = const_vecsmall(nbcol, 1);
    4948         119 :   GEN Wrow  = zero_zv(nbrow);
    4949         119 :   GEN wcol = cgetg(nbcol+1, t_VECSMALL);
    4950         119 :   pari_sp av2 = avma;
    4951      126763 :   for (i = 1; i <= nbcol; ++i)
    4952             :   {
    4953      126644 :     GEN F = gmael(M, i, 1);
    4954      126644 :     long l = lg(F)-1;
    4955     1115936 :     for (j = 1; j <= l; ++j) Wrow[F[j]]++;
    4956             :   }
    4957         119 :   for (j = 1; j < lA; ++j)
    4958             :   {
    4959           0 :     if (Wrow[A[j]] == 0) { *p_col=NULL; return; }
    4960           0 :     Wrow[A[j]] = -1;
    4961             :   }
    4962      235298 :   for (i = 1; i <= nbrow; ++i)
    4963      235179 :     if (Wrow[i]) rrow++;
    4964         119 :   rem_singleton(M, iscol, Wrow, 1, &rcol, &rrow);
    4965         119 :   if (rcol < rrow) pari_err_BUG("RgMs_structelim, rcol<rrow");
    4966        6225 :   while (rcol > rrow)
    4967             :   {
    4968             :     long w;
    4969        6106 :     GEN per = fill_wcol(M, iscol, Wrow, &w, wcol);
    4970       95449 :     for (i = nbcol; i>=imin && wcol[per[i]]>=w && rcol>rrow; i--)
    4971       89343 :       rem_col(gmael(M, per[i], 1), per[i], iscol, Wrow, &rcol, &rrow);
    4972        6106 :     rem_singleton(M, iscol, Wrow, 1, &rcol, &rrow); set_avma(av2);
    4973             :   }
    4974      126763 :   for (j = 1, i = 1; i <= nbcol; ++i)
    4975      126644 :     if (iscol[i]) pcol[j++] = i;
    4976         119 :   setlg(pcol,j);
    4977      235298 :   for (k = 1, i = 1; i <= nbrow; ++i) prow[i] = Wrow[i]? k++: 0;
    4978         119 :   *p_col = pcol; *p_row = prow; set_avma(av);
    4979             : }
    4980             : 
    4981             : void
    4982           0 : RgMs_structelim(GEN M, long nbrow, GEN A, GEN *p_col, GEN *p_row)
    4983           0 : { RgMs_structelim_col(M, lg(M)-1, nbrow, A, p_col, p_row); }
    4984             : 
    4985             : GEN
    4986          53 : F2Ms_colelim(GEN M, long nbrow)
    4987             : {
    4988          53 :   long i,j, nbcol = lg(M)-1, rcol = nbcol, rrow = 0;
    4989          53 :   GEN pcol = zero_zv(nbcol);
    4990          53 :   pari_sp av = avma;
    4991          53 :   GEN iscol = const_vecsmall(nbcol, 1), Wrow  = zero_zv(nbrow);
    4992       85968 :   for (i = 1; i <= nbcol; ++i)
    4993             :   {
    4994       85915 :     GEN F = gel(M, i);
    4995       85915 :     long l = lg(F)-1;
    4996     1587632 :     for (j = 1; j <= l; ++j) Wrow[F[j]]++;
    4997             :   }
    4998          53 :   rem_singleton(M, iscol, Wrow, 0, &rcol, &rrow);
    4999       85968 :   for (j = 1, i = 1; i <= nbcol; ++i)
    5000       85915 :     if (iscol[i]) pcol[j++] = i;
    5001          53 :   fixlg(pcol,j); return gc_const(av, pcol);
    5002             : }
    5003             : 
    5004             : /*******************************************************************/
    5005             : /*                                                                 */
    5006             : /*                        EIGENVECTORS                             */
    5007             : /*   (independent eigenvectors, sorted by increasing eigenvalue)   */
    5008             : /*                                                                 */
    5009             : /*******************************************************************/
    5010             : /* assume x is square of dimension > 0 */
    5011             : static int
    5012          34 : RgM_is_symmetric_cx(GEN x, long bit)
    5013             : {
    5014          34 :   pari_sp av = avma;
    5015          34 :   long i, j, l = lg(x);
    5016         201 :   for (i = 1; i < l; i++)
    5017         670 :     for (j = 1; j < i; j++)
    5018             :     {
    5019         503 :       GEN a = gcoeff(x,i,j), b = gcoeff(x,j,i), c = gsub(a,b);
    5020         503 :       if (!gequal0(c) && gexpo(c) - gexpo(a) > -bit) return gc_long(av,0);
    5021             :     }
    5022          21 :   return gc_long(av,1);
    5023             : }
    5024             : static GEN
    5025          34 : eigen_err(int exact, GEN x, long flag, long prec)
    5026             : {
    5027          34 :   pari_sp av = avma;
    5028          34 :   if (RgM_is_symmetric_cx(x, prec2nbits(prec) - 10))
    5029             :   { /* approximately symmetric: recover */
    5030          21 :     x = jacobi(x, prec); if (flag) return x;
    5031          14 :     return gerepilecopy(av, gel(x,2));
    5032             :   }
    5033          13 :   if (exact)
    5034             :   {
    5035           6 :     GEN y = mateigen(x, flag, precdbl(prec));
    5036           6 :     return gerepilecopy(av, gprec_wtrunc(y, prec));
    5037             :   }
    5038           7 :   pari_err_PREC("mateigen");
    5039             :   return NULL; /* LCOV_EXCL_LINE */
    5040             : }
    5041             : GEN
    5042         104 : mateigen(GEN x, long flag, long prec)
    5043             : {
    5044             :   GEN y, R, T;
    5045         104 :   long k, l, ex, n = lg(x);
    5046             :   int exact;
    5047         104 :   pari_sp av = avma;
    5048             : 
    5049         104 :   if (typ(x)!=t_MAT) pari_err_TYPE("eigen",x);
    5050         104 :   if (n != 1 && n != lgcols(x)) pari_err_DIM("eigen");
    5051         104 :   if (flag < 0 || flag > 1) pari_err_FLAG("mateigen");
    5052         104 :   if (n == 1)
    5053             :   {
    5054          14 :     if (flag) retmkvec2(cgetg(1,t_VEC), cgetg(1,t_MAT));
    5055           7 :     return cgetg(1,t_VEC);
    5056             :   }
    5057          90 :   if (n == 2)
    5058             :   {
    5059          14 :     if (flag) retmkvec2(mkveccopy(gcoeff(x,1,1)), matid(1));
    5060           7 :     return matid(1);
    5061             :   }
    5062             : 
    5063          76 :   ex = 16 - prec2nbits(prec);
    5064          76 :   T = charpoly(x,0);
    5065          76 :   exact = RgX_is_QX(T);
    5066          76 :   if (exact)
    5067             :   {
    5068          41 :     T = ZX_radical( Q_primpart(T) );
    5069          41 :     R = nfrootsQ(T);
    5070          41 :     if (lg(R)-1 < degpol(T))
    5071             :     { /* add missing complex roots */
    5072          27 :       GEN r = cleanroots(RgX_div(T, roots_to_pol(R, 0)), prec);
    5073          27 :       settyp(r, t_VEC);
    5074          27 :       R = shallowconcat(R, r);
    5075             :     }
    5076             :   }
    5077             :   else
    5078             :   {
    5079          35 :     GEN r1, v = vectrunc_init(lg(T));
    5080             :     long e;
    5081          35 :     R = cleanroots(T,prec);
    5082          35 :     r1 = NULL;
    5083         231 :     for (k = 1; k < lg(R); k++)
    5084             :     {
    5085         196 :       GEN r2 = gel(R,k), r = grndtoi(r2, &e);
    5086         196 :       if (e < ex) r2 = r;
    5087         196 :       if (r1)
    5088             :       {
    5089         161 :         r = gsub(r1,r2);
    5090         161 :         if (gequal0(r) || gexpo(r) < ex) continue;
    5091             :       }
    5092         154 :       vectrunc_append(v, r2);
    5093         154 :       r1 = r2;
    5094             :     }
    5095          35 :     R = v;
    5096             :   }
    5097             :   /* R = distinct complex roots of charpoly(x) */
    5098          76 :   l = lg(R); y = cgetg(l, t_VEC);
    5099         328 :   for (k = 1; k < l; k++)
    5100             :   {
    5101         286 :     GEN F = ker_aux(RgM_Rg_sub_shallow(x, gel(R,k)), x);
    5102         286 :     long d = lg(F)-1;
    5103         286 :     if (!d) { set_avma(av); return eigen_err(exact, x, flag, prec); }
    5104         252 :     gel(y,k) = F;
    5105         252 :     if (flag) gel(R,k) = const_vec(d, gel(R,k));
    5106             :   }
    5107          42 :   y = shallowconcat1(y);
    5108          42 :   if (lg(y) > n) { set_avma(av); return eigen_err(exact, x, flag, prec); }
    5109             :   /* lg(y) < n if x is not diagonalizable */
    5110          42 :   if (flag) y = mkvec2(shallowconcat1(R), y);
    5111          42 :   return gerepilecopy(av,y);
    5112             : }
    5113             : GEN
    5114           0 : eigen(GEN x, long prec) { return mateigen(x, 0, prec); }
    5115             : 
    5116             : /*******************************************************************/
    5117             : /*                                                                 */
    5118             : /*                           DETERMINANT                           */
    5119             : /*                                                                 */
    5120             : /*******************************************************************/
    5121             : 
    5122             : GEN
    5123       26390 : det0(GEN a,long flag)
    5124             : {
    5125       26390 :   switch(flag)
    5126             :   {
    5127       26376 :     case 0: return det(a);
    5128          14 :     case 1: return det2(a);
    5129           0 :     default: pari_err_FLAG("matdet");
    5130             :   }
    5131             :   return NULL; /* LCOV_EXCL_LINE */
    5132             : }
    5133             : 
    5134             : /* M a 2x2 matrix, returns det(M) */
    5135             : static GEN
    5136       83375 : RgM_det2(GEN M)
    5137             : {
    5138       83375 :   pari_sp av = avma;
    5139       83375 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    5140       83375 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    5141       83375 :   return gerepileupto(av, gsub(gmul(a,d), gmul(b,c)));
    5142             : }
    5143             : /* M a 2x2 ZM, returns det(M) */
    5144             : static GEN
    5145        6364 : ZM_det2(GEN M)
    5146             : {
    5147        6364 :   pari_sp av = avma;
    5148        6364 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    5149        6364 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    5150        6364 :   return gerepileuptoint(av, subii(mulii(a,d), mulii(b, c)));
    5151             : }
    5152             : /* M a 3x3 ZM, return det(M) */
    5153             : static GEN
    5154       92862 : ZM_det3(GEN M)
    5155             : {
    5156       92862 :   pari_sp av = avma;
    5157       92862 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2), c = gcoeff(M,1,3);
    5158       92862 :   GEN d = gcoeff(M,2,1), e = gcoeff(M,2,2), f = gcoeff(M,2,3);
    5159       92862 :   GEN g = gcoeff(M,3,1), h = gcoeff(M,3,2), i = gcoeff(M,3,3);
    5160       92862 :   GEN t, D = signe(i)? mulii(subii(mulii(a,e), mulii(b,d)), i): gen_0;
    5161       92862 :   if (signe(g))
    5162             :   {
    5163       64876 :     t = mulii(subii(mulii(b,f), mulii(c,e)), g);
    5164       64876 :     D = addii(D, t);
    5165             :   }
    5166       92862 :   if (signe(h))
    5167             :   {
    5168       74816 :     t = mulii(subii(mulii(c,d), mulii(a,f)), h);
    5169       74816 :     D = addii(D, t);
    5170             :   }
    5171       92862 :   return gerepileuptoint(av, D);
    5172             : }
    5173             : 
    5174             : static GEN
    5175       62709 : det_simple_gauss(GEN a, GEN data, pivot_fun pivot)
    5176             : {
    5177       62709 :   pari_sp av = avma;
    5178       62709 :   long i,j,k, s = 1, nbco = lg(a)-1;
    5179       62709 :   GEN p, x = gen_1;
    5180             : 
    5181       62709 :   a = RgM_shallowcopy(a);
    5182      403734 :   for (i=1; i<nbco; i++)
    5183             :   {
    5184      341033 :     k = pivot(a, data, i, NULL);
    5185      341034 :     if (k > nbco) return gerepilecopy(av, gcoeff(a,i,i));
    5186      341027 :     if (k != i)
    5187             :     { /* exchange the lines s.t. k = i */
    5188     1524596 :       for (j=i; j<=nbco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
    5189      149915 :       s = -s;
    5190             :     }
    5191      341027 :     p = gcoeff(a,i,i);
    5192             : 
    5193      341027 :     x = gmul(x,p);
    5194     2340414 :     for (k=i+1; k<=nbco; k++)
    5195             :     {
    5196     1999389 :       GEN m = gcoeff(a,i,k);
    5197     1999389 :       if (gequal0(m)) continue;
    5198             : 
    5199     1397183 :       m = gdiv(m,p);
    5200    13539150 :       for (j=i+1; j<=nbco; j++)
    5201    12141969 :         gcoeff(a,j,k) = gsub(gcoeff(a,j,k), gmul(m,gcoeff(a,j,i)));
    5202             :     }
    5203      341025 :     if (gc_needed(av,2))
    5204             :     {
    5205           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    5206           0 :       gerepileall(av,2, &a,&x);
    5207             :     }
    5208             :   }
    5209       62701 :   if (s < 0) x = gneg_i(x);
    5210       62702 :   return gerepileupto(av, gmul(x, gcoeff(a,nbco,nbco)));
    5211             : }
    5212             : 
    5213             : GEN
    5214      114779 : det2(GEN a)
    5215             : {
    5216             :   GEN data;
    5217             :   pivot_fun pivot;
    5218      114779 :   long n = lg(a)-1;
    5219      114779 :   if (typ(a)!=t_MAT) pari_err_TYPE("det2",a);
    5220      114779 :   if (!n) return gen_1;
    5221      114779 :   if (n != nbrows(a)) pari_err_DIM("det2");
    5222      114779 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    5223       77204 :   if (n == 2) return RgM_det2(a);
    5224       28864 :   pivot = get_pivot_fun(a, a, &data);
    5225       28864 :   return det_simple_gauss(a, data, pivot);
    5226             : }
    5227             : 
    5228             : /* Assumes a a square t_MAT of dimension n > 0. Returns det(a) using
    5229             :  * Gauss-Bareiss. */
    5230             : static GEN
    5231         448 : det_bareiss(GEN a)
    5232             : {
    5233         448 :   pari_sp av = avma;
    5234         448 :   long nbco = lg(a)-1,i,j,k,s = 1;
    5235             :   GEN p, pprec;
    5236             : 
    5237         448 :   a = RgM_shallowcopy(a);
    5238        1274 :   for (pprec=gen_1,i=1; i<nbco; i++,pprec=p)
    5239             :   {
    5240         826 :     int diveuc = (gequal1(pprec)==0);
    5241             :     GEN ci;
    5242             : 
    5243         826 :     p = gcoeff(a,i,i);
    5244         826 :     if (gequal0(p))
    5245             :     {
    5246           0 :       k=i+1; while (k<=nbco && gequal0(gcoeff(a,i,k))) k++;
    5247           0 :       if (k>nbco) return gerepilecopy(av, p);
    5248           0 :       swap(gel(a,k), gel(a,i)); s = -s;
    5249           0 :       p = gcoeff(a,i,i);
    5250             :     }
    5251         826 :     ci = gel(a,i);
    5252        2184 :     for (k=i+1; k<=nbco; k++)
    5253             :     {
    5254        1358 :       GEN ck = gel(a,k), m = gel(ck,i);
    5255        1358 :       if (gequal0(m))
    5256             :       {
    5257           0 :         if (gequal1(p))
    5258             :         {
    5259           0 :           if (diveuc)
    5260           0 :             gel(a,k) = gdiv(gel(a,k), pprec);
    5261             :         }
    5262             :         else
    5263           0 :           for (j=i+1; j<=nbco; j++)
    5264             :           {
    5265           0 :             GEN p1 = gmul(p, gel(ck,j));
    5266           0 :             if (diveuc) p1 = gdiv(p1,pprec);
    5267           0 :             gel(ck,j) = p1;
    5268             :           }
    5269             :       }
    5270             :       else
    5271        4088 :         for (j=i+1; j<=nbco; j++)
    5272             :         {
    5273        2730 :           pari_sp av2 = avma;
    5274        2730 :           GEN p1 = gsub(gmul(p,gel(ck,j)), gmul(m,gel(ci,j)));
    5275        2730 :           if (diveuc) p1 = gdiv(p1,pprec);
    5276        2730 :           gel(ck,j) = gerepileupto(av2, p1);
    5277             :         }
    5278        1358 :       if (gc_needed(av,2))
    5279             :       {
    5280           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"det. col = %ld",i);
    5281           0 :         gerepileall(av,2, &a,&pprec);
    5282           0 :         ci = gel(a,i);
    5283           0 :         p = gcoeff(a,i,i);
    5284             :       }
    5285             :     }
    5286             :   }
    5287         448 :   p = gcoeff(a,nbco,nbco);
    5288         448 :   p = (s < 0)? gneg(p): gcopy(p);
    5289         448 :   return gerepileupto(av, p);
    5290             : }
    5291             : 
    5292             : /* count nonzero entries in col j, at most 'max' of them.
    5293             :  * Return their indices */
    5294             : static GEN
    5295        1400 : col_count_non_zero(GEN a, long j, long max)
    5296             : {
    5297        1400 :   GEN v = cgetg(max+1, t_VECSMALL);
    5298        1400 :   GEN c = gel(a,j);
    5299        1400 :   long i, l = lg(a), k = 1;
    5300        5278 :   for (i = 1; i < l; i++)
    5301        5040 :     if (!gequal0(gel(c,i)))
    5302             :     {
    5303        4788 :       if (k > max) return NULL; /* fail */
    5304        3626 :       v[k++] = i;
    5305             :     }
    5306         238 :   setlg(v, k); return v;
    5307             : }
    5308             : /* count nonzero entries in row i, at most 'max' of them.
    5309             :  * Return their indices */
    5310             : static GEN
    5311        1386 : row_count_non_zero(GEN a, long i, long max)
    5312             : {
    5313        1386 :   GEN v = cgetg(max+1, t_VECSMALL);
    5314        1386 :   long j, l = lg(a), k = 1;
    5315        5222 :   for (j = 1; j < l; j++)
    5316        4998 :     if (!gequal0(gcoeff(a,i,j)))
    5317             :     {
    5318        4774 :       if (k > max) return NULL; /* fail */
    5319        3612 :       v[k++] = j;
    5320             :     }
    5321         224 :   setlg(v, k); return v;
    5322             : }
    5323             : 
    5324             : static GEN det_develop(GEN a, long max, double bound);
    5325             : /* (-1)^(i+j) a[i,j] * det RgM_minor(a,i,j) */
    5326             : static GEN
    5327         406 : coeff_det(GEN a, long i, long j, long max, double bound)
    5328             : {
    5329         406 :   GEN c = gcoeff(a, i, j);
    5330         406 :   c = gmul(c, det_develop(RgM_minor(a, i,j), max, bound));
    5331         406 :   if (odd(i+j)) c = gneg(c);
    5332         406 :   return c;
    5333             : }
    5334             : /* a square t_MAT, 'bound' a rough upper bound for the number of
    5335             :  * multiplications we are willing to pay while developing rows/columns before
    5336             :  * switching to Gaussian elimination */
    5337             : static GEN
    5338         644 : det_develop(GEN M, long max, double bound)
    5339             : {
    5340         644 :   pari_sp av = avma;
    5341         644 :   long i,j, n = lg(M)-1, lbest = max+2, best_col = 0, best_row = 0;
    5342         644 :   GEN best = NULL;
    5343             : 
    5344         644 :   if (bound < 1.) return det_bareiss(M); /* too costly now */
    5345             : 
    5346         420 :   switch(n)
    5347             :   {
    5348           0 :     case 0: return gen_1;
    5349           0 :     case 1: return gcopy(gcoeff(M,1,1));
    5350          14 :     case 2: return RgM_det2(M);
    5351             :   }
    5352         406 :   if (max > ((n+2)>>1)) max = (n+2)>>1;
    5353        1792 :   for (j = 1; j <= n; j++)
    5354             :   {
    5355        1400 :     pari_sp av2 = avma;
    5356        1400 :     GEN v = col_count_non_zero(M, j, max);
    5357             :     long lv;
    5358        1400 :     if (!v || (lv = lg(v)) >= lbest) { set_avma(av2); continue; }
    5359         182 :     if (lv == 1) { set_avma(av); return gen_0; }
    5360         182 :     if (lv == 2) {
    5361          14 :       set_avma(av);
    5362          14 :       return gerepileupto(av, coeff_det(M,v[1],j,max,bound));
    5363             :     }
    5364         168 :     best = v; lbest = lv; best_col = j;
    5365             :   }
    5366        1778 :   for (i = 1; i <= n; i++)
    5367             :   {
    5368        1386 :     pari_sp av2 = avma;
    5369        1386 :     GEN v = row_count_non_zero(M, i, max);
    5370             :     long lv;
    5371        1386 :     if (!v || (lv = lg(v)) >= lbest) { set_avma(av2); continue; }
    5372           0 :     if (lv == 1) { set_avma(av); return gen_0; }
    5373           0 :     if (lv == 2) {
    5374           0 :       set_avma(av);
    5375           0 :       return gerepileupto(av, coeff_det(M,i,v[1],max,bound));
    5376             :     }
    5377           0 :     best = v; lbest = lv; best_row = i;
    5378             :   }
    5379         392 :   if (best_row)
    5380             :   {
    5381           0 :     double d = lbest-1;
    5382           0 :     GEN s = NULL;
    5383             :     long k;
    5384           0 :     bound /= d*d*d;
    5385           0 :     for (k = 1; k < lbest; k++)
    5386             :     {
    5387           0 :       GEN c = coeff_det(M, best_row, best[k], max, bound);
    5388           0 :       s = s? gadd(s, c): c;
    5389             :     }
    5390           0 :     return gerepileupto(av, s);
    5391             :   }
    5392         392 :   if (best_col)
    5393             :   {
    5394         168 :     double d = lbest-1;
    5395         168 :     GEN s = NULL;
    5396             :     long k;
    5397         168 :     bound /= d*d*d;
    5398         560 :     for (k = 1; k < lbest; k++)
    5399             :     {
    5400         392 :       GEN c = coeff_det(M, best[k], best_col, max, bound);
    5401         392 :       s = s? gadd(s, c): c;
    5402             :     }
    5403         168 :     return gerepileupto(av, s);
    5404             :   }
    5405         224 :   return det_bareiss(M);
    5406             : }
    5407             : 
    5408             : /* area of parallelogram bounded by (v1,v2) */
    5409             : static GEN
    5410       61810 : parallelogramarea(GEN v1, GEN v2)
    5411       61810 : { return gsub(gmul(gnorml2(v1), gnorml2(v2)), gsqr(RgV_dotproduct(v1, v2))); }
    5412             : 
    5413             : /* Square of Hadamard bound for det(a), a square matrix.
    5414             :  * Slight improvement: instead of using the column norms, use the area of
    5415             :  * the parallelogram formed by pairs of consecutive vectors */
    5416             : GEN
    5417       19397 : RgM_Hadamard(GEN a)
    5418             : {
    5419       19397 :   pari_sp av = avma;
    5420       19397 :   long n = lg(a)-1, i;
    5421             :   GEN B;
    5422       19397 :   if (n == 0) return gen_1;
    5423       19397 :   if (n == 1) return gsqr(gcoeff(a,1,1));
    5424       19397 :   a = RgM_gtofp(a, LOWDEFAULTPREC);
    5425       19397 :   B = gen_1;
    5426       81207 :   for (i = 1; i <= n/2; i++)
    5427       61810 :     B = gmul(B, parallelogramarea(gel(a,2*i-1), gel(a,2*i)));
    5428       19397 :   if (odd(n)) B = gmul(B, gnorml2(gel(a, n)));
    5429       19397 :   return gerepileuptoint(av, ceil_safe(B));
    5430             : }
    5431             : 
    5432             : /* If B=NULL, assume B=A' */
    5433             : static GEN
    5434       20570 : ZM_det_slice(GEN A, GEN P, GEN *mod)
    5435             : {
    5436       20570 :   pari_sp av = avma;
    5437       20570 :   long i, n = lg(P)-1;
    5438             :   GEN H, T;
    5439       20570 :   if (n == 1)
    5440             :   {
    5441           0 :     ulong Hp, p = uel(P,1);
    5442           0 :     GEN a = ZM_to_Flm(A, p);
    5443           0 :     Hp = Flm_det_sp(a, p);
    5444           0 :     set_avma(av); *mod = utoipos(p); return utoi(Hp);
    5445             :   }
    5446       20570 :   T = ZV_producttree(P);
    5447       20570 :   A = ZM_nv_mod_tree(A, P, T);
    5448       20570 :   H = cgetg(n+1, t_VECSMALL);
    5449       85601 :   for(i=1; i <= n; i++)
    5450             :   {
    5451       65031 :     ulong p = P[i];
    5452       65031 :     GEN a = gel(A,i);
    5453       65031 :     H[i] = Flm_det_sp(a, p);
    5454             :   }
    5455       20570 :   H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
    5456       20570 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    5457             : }
    5458             : 
    5459             : GEN
    5460       20570 : ZM_det_worker(GEN P, GEN A)
    5461             : {
    5462       20570 :   GEN V = cgetg(3, t_VEC);
    5463       20570 :   gel(V,1) = ZM_det_slice(A, P, &gel(V,2));
    5464       20570 :   return V;
    5465             : }
    5466             : 
    5467             : GEN
    5468      120148 : ZM_det(GEN M)
    5469             : {
    5470      120148 :   const long DIXON_THRESHOLD = 40;
    5471             :   pari_sp av, av2;
    5472      120148 :   long i, n = lg(M)-1;
    5473             :   ulong p, Dp;
    5474             :   forprime_t S;
    5475             :   pari_timer ti;
    5476             :   GEN H, D, mod, h, q, v, worker;
    5477             : #ifdef LONG_IS_64BIT
    5478      102984 :   const ulong PMAX = 18446744073709551557UL;
    5479             : #else
    5480       17164 :   const ulong PMAX = 4294967291UL;
    5481             : #endif
    5482             : 
    5483      120148 :   switch(n)
    5484             :   {
    5485           7 :     case 0: return gen_1;
    5486        1518 :     case 1: return icopy(gcoeff(M,1,1));
    5487        6364 :     case 2: return ZM_det2(M);
    5488       92862 :     case 3: return ZM_det3(M);
    5489             :   }
    5490       19397 :   if (DEBUGLEVEL>=4) timer_start(&ti);
    5491       19397 :   av = avma; h = RgM_Hadamard(M); /* |D| <= sqrt(h) */
    5492       19397 :   if (!signe(h)) { set_avma(av); return gen_0; }
    5493       19397 :   h = sqrti(h);
    5494       19397 :   if (lgefint(h) == 3 && (ulong)h[2] <= (PMAX >> 1))
    5495             :   { /* h < p/2 => direct result */
    5496        6747 :     p = PMAX;
    5497        6747 :     Dp = Flm_det_sp(ZM_to_Flm(M, p), p);
    5498        6747 :     set_avma(av);
    5499        6747 :     if (!Dp) return gen_0;
    5500        6747 :     return (Dp <= (p>>1))? utoipos(Dp): utoineg(p - Dp);
    5501             :   }
    5502       12650 :   q = gen_1; Dp = 1;
    5503       12650 :   init_modular_big(&S);
    5504       12650 :   p = 0; /* -Wall */
    5505       12650 :   while (cmpii(q, h) <= 0 && (p = u_forprime_next(&S)))
    5506             :   {
    5507       12650 :     av2 = avma; Dp = Flm_det_sp(ZM_to_Flm(M, p), p);
    5508       12650 :     set_avma(av2);
    5509       12650 :     if (Dp) break;
    5510           0 :     q = muliu(q, p);
    5511             :   }
    5512       12650 :   if (!p) pari_err_OVERFLOW("ZM_det [ran out of primes]");
    5513       12650 :   if (!Dp) { set_avma(av); return gen_0; }
    5514       12650 :   if (mt_nbthreads() > 1 || n <= DIXON_THRESHOLD)
    5515       12650 :     D = q; /* never competitive when bound is sharp even with 2 threads */
    5516             :   else
    5517             :   {
    5518           0 :     av2 = avma;
    5519           0 :     v = cgetg(n+1, t_COL);
    5520           0 :     gel(v, 1) = gen_1; /* ensure content(v) = 1 */
    5521           0 :     for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    5522           0 :     D = Q_denom(ZM_gauss(M, v));
    5523           0 :     if (expi(D) < expi(h) >> 1)
    5524             :     { /* First try unlucky, try once more */
    5525           0 :       for (i = 2; i <= n; i++) gel(v, i) = stoi(random_Fl(15) - 7);
    5526           0 :       D = lcmii(D, Q_denom(ZM_gauss(M, v)));
    5527             :     }
    5528           0 :     D = gerepileuptoint(av2, D);
    5529           0 :     if (q != gen_1) D = lcmii(D, q);
    5530             :   }
    5531       12650 :   if (DEBUGLEVEL >=4)
    5532           0 :     timer_printf(&ti,"ZM_det: Dixon %ld/%ld bits",expi(D),expi(h));
    5533             :   /* determinant is a multiple of D */
    5534       12650 :   if (is_pm1(D)) D = NULL;
    5535       12650 :   if (D) h = diviiexact(h, D);
    5536       12650 :   worker = snm_closure(is_entry("_ZM_det_worker"), mkvec(M));
    5537       12650 :   H = gen_crt("ZM_det", worker, &S, D, expi(h)+1, 0, &mod,
    5538             :               ZV_chinese, NULL);
    5539       12650 :   if (D) H = Fp_div(H, D, mod);
    5540       12650 :   H = Fp_center(H, mod, shifti(mod,-1));
    5541       12650 :   if (D) H = mulii(H, D);
    5542       12650 :   return gerepileuptoint(av, H);
    5543             : }
    5544             : 
    5545             : static GEN
    5546        1519 : RgM_det_FpM(GEN a, GEN p)
    5547             : {
    5548        1519 :   pari_sp av = avma;
    5549             :   ulong pp, d;
    5550        1519 :   a = RgM_Fp_init(a,p,&pp);
    5551        1519 :   switch(pp)
    5552             :   {
    5553          70 :   case 0: return gerepileupto(av, Fp_to_mod(FpM_det(a,p),p)); break;
    5554          14 :   case 2: d = F2m_det_sp(a); break;
    5555        1435 :   default:d = Flm_det_sp(a, pp); break;
    5556             :   }
    5557        1449 :   set_avma(av); return mkintmodu(d, pp);
    5558             : }
    5559             : 
    5560             : static GEN
    5561          42 : RgM_det_FqM(GEN x, GEN pol, GEN p)
    5562             : {
    5563          42 :   pari_sp av = avma;
    5564          42 :   GEN b, T = RgX_to_FpX(pol, p);
    5565          42 :   if (signe(T) == 0) pari_err_OP("%",x,pol);
    5566          42 :   b = FqM_det(RgM_to_FqM(x, T, p), T, p);
    5567          42 :   if (!b) return gc_NULL(av);
    5568          42 :   return gerepilecopy(av, mkpolmod(FpX_to_mod(b, p), FpX_to_mod(T, p)));
    5569             : }
    5570             : 
    5571             : #define code(t1,t2) ((t1 << 6) | t2)
    5572             : static GEN
    5573       36078 : RgM_det_fast(GEN x)
    5574             : {
    5575             :   GEN p, pol;
    5576             :   long pa;
    5577       36078 :   long t = RgM_type(x, &p,&pol,&pa);
    5578       36078 :   switch(t)
    5579             :   {
    5580         175 :     case t_INT:    return ZM_det(x);
    5581         196 :     case t_FRAC:   return QM_det(x);
    5582          63 :     case t_FFELT:  return FFM_det(x, pol);
    5583        1519 :     case t_INTMOD: return RgM_det_FpM(x, p);
    5584          42 :     case code(t_POLMOD, t_INTMOD):
    5585          42 :                    return RgM_det_FqM(x, pol, p);
    5586       34083 :     default:       return NULL;
    5587             :   }
    5588             : }
    5589             : #undef code
    5590             : 
    5591             : static long
    5592         238 : det_init_max(long n)
    5593             : {
    5594         238 :   if (n > 100) return 0;
    5595         238 :   if (n > 50) return 1;
    5596         238 :   if (n > 30) return 4;
    5597         238 :   return 7;
    5598             : }
    5599             : 
    5600             : GEN
    5601      354045 : det(GEN a)
    5602             : {
    5603      354045 :   long n = lg(a)-1;
    5604             :   double B;
    5605             :   GEN data, b;
    5606             :   pivot_fun pivot;
    5607             : 
    5608      354045 :   if (typ(a)!=t_MAT) pari_err_TYPE("det",a);
    5609      354045 :   if (!n) return gen_1;
    5610      354003 :   if (n != nbrows(a)) pari_err_DIM("det");
    5611      353996 :   if (n == 1) return gcopy(gcoeff(a,1,1));
    5612       71098 :   if (n == 2) return RgM_det2(a);
    5613       36078 :   b = RgM_det_fast(a);
    5614       36078 :   if (b) return b;
    5615       34083 :   pivot = get_pivot_fun(a, a, &data);
    5616       34083 :   if (pivot != gauss_get_pivot_NZ) return det_simple_gauss(a, data, pivot);
    5617         238 :   B = (double)n;
    5618         238 :   return det_develop(a, det_init_max(n), B*B*B);
    5619             : }
    5620             : 
    5621             : GEN
    5622         196 : QM_det(GEN M)
    5623             : {
    5624         196 :   pari_sp av = avma;
    5625         196 :   GEN cM, pM = Q_primitive_part(M, &cM);
    5626         196 :   GEN b = ZM_det(pM);
    5627         196 :   if (cM) b = gmul(b, gpowgs(cM, lg(M)-1));
    5628         196 :   return gerepileupto(av, b);
    5629             : }

Generated by: LCOV version 1.13