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 - FpV.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23036-b751c0af5) Lines: 706 936 75.4 %
Date: 2018-09-26 05:46:06 Functions: 104 131 79.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /********************************************************************/
      18             : /**                                                                **/
      19             : /**                           REDUCTION                            **/
      20             : /**                                                                **/
      21             : /********************************************************************/
      22             : /* z in Z^n, return lift(Col(z) * Mod(1,p)) */
      23             : GEN
      24    10070255 : FpC_red(GEN x, GEN p)
      25    10070255 : { pari_APPLY_type(t_COL, modii(gel(x,i), p)) }
      26             : 
      27             : /* z in Z^n, return lift(Vec(z) * Mod(1,p)) */
      28             : GEN
      29      207830 : FpV_red(GEN x, GEN p)
      30      207830 : { pari_APPLY_type(t_VEC, modii(gel(x,i), p)) }
      31             : 
      32             : GEN
      33     1314602 : FpC_center(GEN x, GEN p, GEN pov2)
      34     1314602 : { pari_APPLY_type(t_COL, Fp_center(gel(x,i), p, pov2)) }
      35             : 
      36             : /* assume 0 <= u < p and ps2 = p>>1 */
      37             : INLINE void
      38          28 : Fp_center_inplace(GEN u, GEN p, GEN ps2)
      39          28 : { if (abscmpii(u,ps2) > 0) subiiz(u,p,u); }
      40             : 
      41             : void
      42          14 : FpC_center_inplace(GEN z, GEN p, GEN ps2)
      43             : {
      44          14 :   long i,l = lg(z);
      45          42 :   for (i=1; i<l; i++)
      46          28 :     Fp_center_inplace(gel(z,i), p, ps2);
      47          14 : }
      48             : 
      49             : GEN
      50      142275 : Flv_center(GEN x, ulong p, ulong ps2)
      51      142275 : { pari_APPLY_ulong(Fl_center(uel(x,i),p,ps2)) }
      52             : 
      53             : /* z in Mat m,n(Z), return lift(z * Mod(1,p)) */
      54             : GEN
      55     2032728 : FpM_red(GEN x, GEN p)
      56     2032728 : { pari_APPLY_same(FpC_red(gel(x,i), p)) }
      57             : 
      58             : GEN
      59      162135 : FpM_center(GEN x, GEN p, GEN pov2)
      60      162135 : { pari_APPLY_same(FpC_center(gel(x,i), p,pov2)) }
      61             : 
      62             : void
      63          14 : FpM_center_inplace(GEN z, GEN p, GEN pov2)
      64             : {
      65          14 :   long i, l = lg(z);
      66          14 :   for (i=1; i<l; i++) FpC_center_inplace(gel(z,i), p, pov2);
      67          14 : }
      68             : GEN
      69        9947 : Flm_center(GEN x, ulong p, ulong ps2)
      70        9947 : { pari_APPLY_same(Flv_center(gel(x,i), p,ps2)) }
      71             : 
      72             : GEN
      73         126 : random_FpV(long d, GEN p)
      74             : {
      75             :   long i;
      76         126 :   GEN y = cgetg(d+1,t_VEC);
      77         126 :   for (i=1; i<=d; i++) gel(y,i) = randomi(p);
      78         126 :   return y;
      79             : }
      80             : 
      81             : GEN
      82         413 : random_FpC(long d, GEN p)
      83             : {
      84             :   long i;
      85         413 :   GEN y = cgetg(d+1,t_COL);
      86         413 :   for (i=1; i<=d; i++) gel(y,i) = randomi(p);
      87         413 :   return y;
      88             : }
      89             : 
      90             : GEN
      91        4053 : random_Flv(long n, ulong p)
      92             : {
      93        4053 :   GEN y = cgetg(n+1, t_VECSMALL);
      94             :   long i;
      95        4053 :   for (i=1; i<=n; i++) uel(y,i) = random_Fl(p);
      96        4053 :   return y;
      97             : }
      98             : 
      99             : /********************************************************************/
     100             : /**                                                                **/
     101             : /**                           ADD, SUB                             **/
     102             : /**                                                                **/
     103             : /********************************************************************/
     104             : GEN
     105      186102 : FpC_add(GEN x, GEN y, GEN p)
     106      186102 : { pari_APPLY_type(t_COL, Fp_add(gel(x,i), gel(y,i), p)) }
     107             : 
     108             : GEN
     109           0 : FpV_add(GEN x, GEN y, GEN p)
     110           0 : { pari_APPLY_type(t_VEC, Fp_add(gel(x,i), gel(y,i), p)) }
     111             : 
     112             : GEN
     113           0 : FpM_add(GEN x, GEN y, GEN p)
     114           0 : { pari_APPLY_same(FpC_add(gel(x,i), gel(y,i), p)) }
     115             : 
     116             : GEN
     117      658197 : Flv_add(GEN x, GEN y, ulong p)
     118             : {
     119      658197 :   if (p==2)
     120       98994 :     pari_APPLY_ulong(x[i]^y[i])
     121             :   else
     122      559203 :     pari_APPLY_ulong(Fl_add(x[i], y[i], p))
     123             : }
     124             : 
     125             : void
     126      169184 : Flv_add_inplace(GEN x, GEN y, ulong p)
     127             : {
     128      169184 :   long i, l = lg(x);
     129      169184 :   if (p==2)
     130      166048 :     for (i = 1; i < l; i++) x[i] ^= y[i];
     131             :   else
     132        3136 :     for (i = 1; i < l; i++) x[i] = Fl_add(x[i], y[i], p);
     133      169184 : }
     134             : 
     135             : ulong
     136        3822 : Flv_sum(GEN x, ulong p)
     137             : {
     138        3822 :   long i, l = lg(x);
     139        3822 :   ulong s = 0;
     140        3822 :   if (p==2)
     141        3822 :     for (i = 1; i < l; i++) s ^= x[i];
     142             :   else
     143           0 :     for (i = 1; i < l; i++) s = Fl_add(s, x[i], p);
     144        3822 :   return s;
     145             : }
     146             : 
     147             : GEN
     148      481614 : FpC_sub(GEN x, GEN y, GEN p)
     149      481614 : { pari_APPLY_type(t_COL, Fp_sub(gel(x,i), gel(y,i), p)) }
     150             : 
     151             : GEN
     152           0 : FpV_sub(GEN x, GEN y, GEN p)
     153           0 : { pari_APPLY_type(t_VEC, Fp_sub(gel(x,i), gel(y,i), p)) }
     154             : 
     155             : GEN
     156           0 : FpM_sub(GEN x, GEN y, GEN p)
     157           0 : { pari_APPLY_same(FpC_sub(gel(x,i), gel(y,i), p)) }
     158             : 
     159             : GEN
     160    74842832 : Flv_sub(GEN x, GEN y, ulong p)
     161    74842832 : { pari_APPLY_ulong(Fl_sub(uel(x,i), uel(y,i), p)) }
     162             : 
     163             : void
     164           0 : Flv_sub_inplace(GEN x, GEN y, ulong p)
     165             : {
     166           0 :   long i, l = lg(x);
     167           0 :   for (i = 1; i < l; i++) x[i] = Fl_sub(x[i], y[i], p);
     168           0 : }
     169             : 
     170             : GEN
     171       13972 : Flm_Fl_add(GEN x, ulong y, ulong p)
     172             : {
     173       13972 :   long l = lg(x), i, j;
     174       13972 :   GEN z = cgetg(l,t_MAT);
     175             : 
     176       13972 :   if (l==1) return z;
     177       13867 :   if (l != lgcols(x)) pari_err_OP( "+", x, utoi(y));
     178       53480 :   for (i=1; i<l; i++)
     179             :   {
     180       39613 :     GEN zi = cgetg(l,t_VECSMALL), xi = gel(x,i);
     181       39613 :     gel(z,i) = zi;
     182       39613 :     for (j=1; j<l; j++) zi[j] = xi[j];
     183       39613 :     zi[i] = Fl_add(zi[i], y, p);
     184             :   }
     185       13867 :   return z;
     186             : }
     187             : 
     188             : GEN
     189        8715 : Flm_add(GEN x, GEN y, ulong p)
     190        8715 : { pari_APPLY_same(Flv_add(gel(x,i), gel(y,i), p)) }
     191             : 
     192             : GEN
     193     8058100 : Flm_sub(GEN x, GEN y, ulong p)
     194     8058100 : { pari_APPLY_same(Flv_sub(gel(x,i), gel(y,i), p)) }
     195             : 
     196             : /********************************************************************/
     197             : /**                                                                **/
     198             : /**                           MULTIPLICATION                       **/
     199             : /**                                                                **/
     200             : /********************************************************************/
     201             : GEN
     202      719672 : FpC_Fp_mul(GEN x, GEN y, GEN p)
     203      719672 : { pari_APPLY_type(t_COL, Fp_mul(gel(x,i), y, p)) }
     204             : 
     205             : GEN
     206      513154 : Flv_Fl_mul(GEN x, ulong y, ulong p)
     207      513154 : { pari_APPLY_ulong(Fl_mul(x[i], y, p)) }
     208             : 
     209             : GEN
     210         464 : Flv_Fl_div(GEN x, ulong y, ulong p)
     211             : {
     212         464 :   return Flv_Fl_mul(x, Fl_inv(y, p), p);
     213             : }
     214             : void
     215           0 : Flv_Fl_div_inplace(GEN x, ulong y, ulong p)
     216             : {
     217           0 :   Flv_Fl_mul_inplace(x, Fl_inv(y, p), p);
     218           0 : }
     219             : GEN
     220      121514 : FpM_Fp_mul(GEN X, GEN c, GEN p) {
     221      121514 :   long i, j, h, l = lg(X);
     222      121514 :   GEN A = cgetg(l, t_MAT);
     223      121514 :   if (l == 1) return A;
     224      121514 :   h = lgcols(X);
     225     1131626 :   for (j=1; j<l; j++)
     226             :   {
     227     1010112 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     228     1010112 :     for (i = 1; i < h; i++) gel(a,i) = Fp_mul(gel(x,i), c, p);
     229     1010112 :     gel(A,j) = a;
     230             :   }
     231      121514 :   return A;
     232             : }
     233             : 
     234             : /* x *= y */
     235             : void
     236     1581116 : Flv_Fl_mul_part_inplace(GEN x, ulong y, ulong p, long l)
     237             : {
     238             :   long i;
     239     1581116 :   for (i=1;i<=l;i++) x[i] = Fl_mul(x[i], y, p);
     240     1581116 : }
     241             : void
     242           0 : Flv_Fl_mul_inplace(GEN x, ulong y, ulong p)
     243           0 : { Flv_Fl_mul_part_inplace(x, y, p, lg(x)-1); }
     244             : 
     245             : /* set y *= x */
     246             : void
     247           0 : Flm_Fl_mul_inplace(GEN y, ulong x, ulong p)
     248             : {
     249           0 :   long i, j, m, l = lg(y);
     250           0 :   if (l == 1) return;
     251           0 :   m = lgcols(y);
     252           0 :   if (HIGHWORD(x | p))
     253           0 :     for(j=1; j<l; j++)
     254           0 :       for(i=1; i<m; i++) ucoeff(y,i,j) = Fl_mul(ucoeff(y,i,j), x, p);
     255             :   else
     256           0 :     for(j=1; j<l; j++)
     257           0 :       for(i=1; i<m; i++) ucoeff(y,i,j) = (ucoeff(y,i,j) * x) % p;
     258             : }
     259             : 
     260             : /* return x * y */
     261             : GEN
     262     7468754 : Flm_Fl_mul(GEN y, ulong x, ulong p)
     263             : {
     264     7468754 :   long i, j, m, l = lg(y);
     265     7468754 :   GEN z = cgetg(l, t_MAT);
     266     7468208 :   if (l == 1) return z;
     267     7468208 :   m = lgcols(y);
     268     7469701 :   if (HIGHWORD(x | p))
     269    48136857 :     for(j=1; j<l; j++) {
     270    43498094 :       GEN c = cgetg(m, t_VECSMALL); gel(z,j) = c;
     271    43483167 :       for(i=1; i<m; i++) c[i] = Fl_mul(ucoeff(y,i,j), x, p);
     272             :     }
     273             :   else
     274    17725238 :     for(j=1; j<l; j++) {
     275    14895619 :       GEN c = cgetg(m, t_VECSMALL); gel(z,j) = c;
     276    14895619 :       for(i=1; i<m; i++) c[i] = (ucoeff(y,i,j) * x) % p;
     277             :     }
     278     7468382 :   return z;
     279             : }
     280             : 
     281             : GEN
     282     1497265 : Flv_neg(GEN x, ulong p)
     283     1497265 : { pari_APPLY_ulong(Fl_neg(uel(x,i), p)) }
     284             : 
     285             : void
     286        5816 : Flv_neg_inplace(GEN v, ulong p)
     287             : {
     288             :   long i;
     289      184636 :   for (i = 1; i < lg(v); ++i)
     290      178820 :     v[i] = Fl_neg(v[i], p);
     291        5816 : }
     292             : 
     293             : GEN
     294      120679 : Flm_neg(GEN x, ulong p)
     295      120679 : { pari_APPLY_same(Flv_neg(gel(x,i), p)) }
     296             : 
     297             : /* x[i,]*y. Assume lx > 1 and 0 < i < lgcols(x) */
     298             : static GEN
     299    12726281 : ZMrow_ZC_mul_i(GEN x, GEN y, long lx, long i)
     300             : {
     301    12726281 :   GEN c = mulii(gcoeff(x,i,1), gel(y,1));
     302             :   long k;
     303   195659086 :   for (k = 2; k < lx; k++)
     304             :   {
     305   182932805 :     GEN t = mulii(gcoeff(x,i,k), gel(y,k));
     306   182932805 :     if (signe(t)) c = addii(c, t);
     307             :   }
     308    12726281 :   return c;
     309             : }
     310             : 
     311             : static long
     312    68774188 : zmrow_zc_mul(GEN x, GEN y, long lx, long i)
     313             : {
     314             :   long k;
     315    68774188 :   long c = coeff(x,i,1) * y[1];
     316  1042862541 :   for (k = 2; k < lx; k++)
     317   974088353 :     c += coeff(x,i,k) * y[k];
     318    68774188 :   return c;
     319             : }
     320             : 
     321             : GEN
     322     4606826 : zm_zc_mul(GEN x, GEN y)
     323             : {
     324     4606826 :   long lx = lg(x), l, i;
     325             :   GEN z;
     326     4606826 :   if (lx == 1) return cgetg(1, t_VECSMALL);
     327     4606826 :   l = lg(gel(x,1));
     328     4606826 :   z = cgetg(l,t_VECSMALL);
     329     4606826 :   for (i=1; i<l; i++) z[i] = zmrow_zc_mul(x, y, lx, i);
     330     4606826 :   return z;
     331             : }
     332             : 
     333             : GEN
     334        1743 : zm_mul(GEN x, GEN y)
     335             : {
     336        1743 :   long i,j,lx=lg(x), ly=lg(y);
     337             :   GEN z;
     338        1743 :   if (ly==1) return cgetg(1,t_MAT);
     339        1743 :   z = cgetg(ly,t_MAT);
     340        1743 :   if (lx==1)
     341             :   {
     342           0 :     for (i=1; i<ly; i++) gel(z,i) = cgetg(1,t_VECSMALL);
     343           0 :     return z;
     344             :   }
     345       24318 :   for (j=1; j<ly; j++)
     346       22575 :     gel(z,j) = zm_zc_mul(x, gel(y,j));
     347        1743 :   return z;
     348             : }
     349             : 
     350             : static ulong
     351   325639107 : Flmrow_Flc_mul_SMALL(GEN x, GEN y, ulong p, long lx, long i)
     352             : {
     353   325639107 :   ulong c = ucoeff(x,i,1) * uel(y,1);
     354             :   long k;
     355  4723840368 :   for (k = 2; k < lx; k++) {
     356  4398201261 :     c += ucoeff(x,i,k) * uel(y,k);
     357  4398201261 :     if (c & HIGHBIT) c %= p;
     358             :   }
     359   325639107 :   return c % p;
     360             : }
     361             : 
     362             : static ulong
     363   170623254 : Flmrow_Flc_mul_i(GEN x, GEN y, ulong p, ulong pi, long lx, long i)
     364             : {
     365             :   ulong l0, l1, v1, h0, h1;
     366   170623254 :   long k = 1;
     367             :   LOCAL_OVERFLOW;
     368             :   LOCAL_HIREMAINDER;
     369   170623254 :   l1 = mulll(ucoeff(x,i,k), uel(y,k)); h1 = hiremainder; v1 = 0;
     370  2125559787 :   while (++k < lx) {
     371  1784313279 :     l0 = mulll(ucoeff(x,i,k), uel(y,k)); h0 = hiremainder;
     372  1784313279 :     l1 = addll(l0, l1); h1 = addllx(h0, h1); v1 += overflow;
     373             :   }
     374   170623254 :   if (v1 == 0) return remll_pre(h1, l1, p, pi);
     375     1112461 :   else return remlll_pre(v1, h1, l1, p, pi);
     376             : }
     377             : 
     378             : static GEN
     379      141953 : Flm_Flc_mul_i_2(GEN x, GEN y, long lx, long l)
     380             : {
     381             :   long i,j;
     382      141953 :   GEN z = NULL;
     383             : 
     384     1486905 :   for (j=1; j<lx; j++)
     385             :   {
     386     1344952 :     if (!y[j]) continue;
     387      381527 :     if (!z) z = Flv_copy(gel(x,j));
     388      258390 :     else for (i = 1; i < l; i++) z[i] ^= coeff(x,i,j);
     389             :   }
     390      141953 :   if (!z) z = zero_zv(l-1);
     391      141953 :   return z;
     392             : }
     393             : 
     394             : static GEN
     395     1057255 : FpM_FpC_mul_i(GEN x, GEN y, long lx, long l, GEN p)
     396             : {
     397     1057255 :   GEN z = cgetg(l,t_COL);
     398             :   long i;
     399    12852396 :   for (i = 1; i < l; i++)
     400             :   {
     401    11795141 :     pari_sp av = avma;
     402    11795141 :     GEN c = ZMrow_ZC_mul_i(x, y, lx, i);
     403    11795141 :     gel(z,i) = gerepileuptoint(av, modii(c,p));
     404             :   }
     405     1057255 :   return z;
     406             : }
     407             : 
     408             : static void
     409    35207594 : __Flm_Flc_mul_i_SMALL(GEN z, GEN x, GEN y, long lx, long l, ulong p)
     410             : {
     411             :   long i;
     412    35207594 :   for (i = 1; i < l; i++) z[i] = Flmrow_Flc_mul_SMALL(x, y, p, lx, i);
     413    35223360 : }
     414             : static GEN
     415    32103998 : Flm_Flc_mul_i_SMALL(GEN x, GEN y, long lx, long l, ulong p)
     416             : {
     417    32103998 :   GEN z = cgetg(l,t_VECSMALL);
     418    32103998 :   __Flm_Flc_mul_i_SMALL(z, x, y, lx, l, p);
     419    32104014 :   return z;
     420             : }
     421             : 
     422             : static void
     423    31721474 : __Flm_Flc_mul_i(GEN z, GEN x, GEN y, long lx, long l, ulong p, ulong pi)
     424             : {
     425             :   long i;
     426    31721474 :   for (i = 1; i < l; i++) z[i] = Flmrow_Flc_mul_i(x, y, p, pi, lx, i);
     427    31919480 : }
     428             : static GEN
     429    27512810 : Flm_Flc_mul_i(GEN x, GEN y, long lx, long l, ulong p, ulong pi)
     430             : {
     431    27512810 :   GEN z = cgetg(l,t_VECSMALL);
     432    27470853 :   __Flm_Flc_mul_i(z, x, y, lx, l, p, pi);
     433    27530268 :   return z;
     434             : }
     435             : 
     436             : INLINE GEN
     437      393943 : F2m_F2c_mul_i(GEN x, GEN y, long lx, long l)
     438             : {
     439             :   long j;
     440      393943 :   GEN z = NULL;
     441             : 
     442     4551170 :   for (j=1; j<lx; j++)
     443             :   {
     444     4157227 :     if (!F2v_coeff(y,j)) continue;
     445      947351 :     if (!z) z = vecsmall_copy(gel(x,j));
     446      577339 :     else F2v_add_inplace(z,gel(x,j));
     447             :   }
     448      393943 :   if (!z) z = zero_F2v(l);
     449      393943 :   return z;
     450             : }
     451             : 
     452             : GEN
     453      519352 : FpM_mul(GEN x, GEN y, GEN p)
     454             : {
     455      519352 :   long lx=lg(x), ly=lg(y);
     456             :   GEN z;
     457      519352 :   pari_sp av = avma;
     458      519352 :   if (ly==1) return cgetg(1,t_MAT);
     459      519352 :   if (lx==1) return zeromat(0, ly-1);
     460      519352 :   if (lgefint(p) == 3)
     461             :   {
     462      510300 :     ulong pp = uel(p,2);
     463      510300 :     if (pp == 2)
     464             :     {
     465       91069 :       x = ZM_to_F2m(x);
     466       91069 :       y = ZM_to_F2m(y);
     467       91069 :       z = F2m_to_ZM(F2m_mul(x,y));
     468             :     }
     469             :     else
     470             :     {
     471      419231 :       x = ZM_to_Flm(x, pp);
     472      419231 :       y = ZM_to_Flm(y, pp);
     473      419231 :       z = Flm_to_ZM(Flm_mul(x,y, pp));
     474             :     }
     475             :   } else
     476        9052 :     z = FpM_red(ZM_mul(x, y), p);
     477      519352 :   return gerepileupto(av, z);
     478             : }
     479             : 
     480             : static GEN
     481    10606142 : Flm_mul_classical(GEN x, GEN y, long l, long lx, long ly, ulong p, ulong pi)
     482             : {
     483             :   long j;
     484    10606142 :   GEN z = cgetg(ly, t_MAT);
     485    10605689 :   if (SMALL_ULONG(p))
     486    39134402 :     for (j=1; j<ly; j++)
     487    31972197 :       gel(z,j) = Flm_Flc_mul_i_SMALL(x, gel(y,j), lx, l, p);
     488             :   else
     489    30969162 :     for (j=1; j<ly; j++)
     490    27525366 :       gel(z,j) = Flm_Flc_mul_i(x, gel(y,j), lx, l, p, pi);
     491    10606001 :   return z;
     492             : }
     493             : 
     494             : /*
     495             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     496             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     497             : */
     498             : static void
     499        8144 : add_slices_ip(long m, long n,
     500             :            GEN A, long ma, long da, long na, long ea,
     501             :            GEN B, long mb, long db, long nb, long eb,
     502             :            GEN M, long dy, long dx, ulong p)
     503             : {
     504        8144 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     505             :   GEN C;
     506             : 
     507      307612 :   for (j = 1; j <= min_e; j++) {
     508      299468 :     C = gel(M, j + dx) + dy;
     509    12794396 :     for (i = 1; i <= min_d; i++)
     510    24989856 :       uel(C, i) = Fl_add(ucoeff(A, ma + i, na + j),
     511    12494928 :                         ucoeff(B, mb + i, nb + j), p);
     512      307634 :     for (; i <= da; i++)
     513        8166 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     514      299468 :     for (; i <= db; i++)
     515           0 :       uel(C, i) = ucoeff(B, mb + i, nb + j);
     516      299468 :     for (; i <= m; i++)
     517           0 :       uel(C, i) = 0;
     518             :   }
     519        8808 :   for (; j <= ea; j++) {
     520         664 :     C = gel(M, j + dx) + dy;
     521       29672 :     for (i = 1; i <= da; i++)
     522       29008 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     523         664 :     for (; i <= m; i++)
     524           0 :       uel(C, i) = 0;
     525             :   }
     526        8144 :   for (; j <= eb; j++) {
     527           0 :     C = gel(M, j + dx) + dy;
     528           0 :     for (i = 1; i <= db; i++)
     529           0 :       uel(C, i) = ucoeff(B, mb + i, nb + j);
     530           0 :     for (; i <= m; i++)
     531           0 :       uel(C, i) = 0;
     532             :   }
     533        8144 :   for (; j <= n; j++) {
     534           0 :     C = gel(M, j + dx) + dy;
     535           0 :     for (i = 1; i <= m; i++)
     536           0 :       uel(C, i) = 0;
     537             :   }
     538        8144 : }
     539             : 
     540             : static GEN
     541        4072 : add_slices(long m, long n,
     542             :            GEN A, long ma, long da, long na, long ea,
     543             :            GEN B, long mb, long db, long nb, long eb, ulong p)
     544             : {
     545             :   GEN M;
     546             :   long j;
     547        4072 :   M = cgetg(n + 1, t_MAT);
     548      151704 :   for (j = 1; j <= n; j++)
     549      147632 :     gel(M, j) = cgetg(m + 1, t_VECSMALL);
     550        4072 :   add_slices_ip(m, n, A, ma, da, na, ea, B, mb, db, nb, eb, M, 0, 0, p);
     551        4072 :   return M;
     552             : }
     553             : 
     554             : /*
     555             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     556             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     557             : */
     558             : static GEN
     559        7126 : subtract_slices(long m, long n,
     560             :                 GEN A, long ma, long da, long na, long ea,
     561             :                 GEN B, long mb, long db, long nb, long eb, ulong p)
     562             : {
     563        7126 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     564        7126 :   GEN M = cgetg(n + 1, t_MAT), C;
     565             : 
     566      258454 :   for (j = 1; j <= min_e; j++) {
     567      251328 :     gel(M, j) = C = cgetg(m + 1, t_VECSMALL);
     568    10371004 :     for (i = 1; i <= min_d; i++)
     569    20239352 :       uel(C, i) = Fl_sub(ucoeff(A, ma + i, na + j),
     570    10119676 :                         ucoeff(B, mb + i, nb + j), p);
     571      271396 :     for (; i <= da; i++)
     572       20068 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     573      258420 :     for (; i <= db; i++)
     574        7092 :       uel(C, i) = Fl_neg(ucoeff(B, mb + i, nb + j), p);
     575      251328 :     for (; i <= m; i++)
     576           0 :       uel(C, i) = 0;
     577             :   }
     578        7126 :   for (; j <= ea; j++) {
     579           0 :     gel(M, j) = C = cgetg(m + 1, t_VECSMALL);
     580           0 :     for (i = 1; i <= da; i++)
     581           0 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     582           0 :     for (; i <= m; i++)
     583           0 :       uel(C, i) = 0;
     584             :   }
     585        7312 :   for (; j <= eb; j++) {
     586         186 :     gel(M, j) = C = cgetg(m + 1, t_VECSMALL);
     587        6990 :     for (i = 1; i <= db; i++)
     588        6804 :       uel(C, i) = Fl_neg(ucoeff(B, mb + i, nb + j), p);
     589         186 :     for (; i <= m; i++)
     590           0 :       uel(C, i) = 0;
     591             :   }
     592        7312 :   for (; j <= n; j++)
     593         186 :     gel(M, j) = zero_Flv(m);
     594        7126 :   return M;
     595             : }
     596             : 
     597             : static GEN Flm_mul_i(GEN x, GEN y, long l, long lx, long ly, ulong p, ulong pi);
     598             : 
     599             : /* Strassen-Winograd matrix product A (m x n) * B (n x p) */
     600             : static GEN
     601        1018 : Flm_mul_sw(GEN A, GEN B, long m, long n, long p, ulong l, ulong li)
     602             : {
     603             :   pari_sp av;
     604        1018 :   long m1 = (m + 1)/2, m2 = m/2,
     605        1018 :     n1 = (n + 1)/2, n2 = n/2,
     606        1018 :     p1 = (p + 1)/2, p2 = p/2;
     607             :   GEN A11, A12, A22, B11, B21, B22,
     608             :     S1, S2, S3, S4, T1, T2, T3, T4,
     609             :     M1, M2, M3, M4, M5, M6, M7,
     610             :     V1, V2, V3, C;
     611             :   long j;
     612        1018 :   C = cgetg(p + 1, t_MAT);
     613       77268 :   for (j = 1; j <= p; j++)
     614       76250 :     gel(C, j) = cgetg(m + 1, t_VECSMALL);
     615        1018 :   av = avma;
     616        1018 :   T2 = subtract_slices(n1, p2, B, 0, n1, p1, p2, B, n1, n2, p1, p2, l);
     617        1018 :   S1 = subtract_slices(m2, n1, A, m1, m2, 0, n1, A, 0, m2, 0, n1, l);
     618        1018 :   M2 = Flm_mul_i(S1, T2, m2 + 1, n1 + 1, p2 + 1, l, li);
     619        1018 :   if (gc_needed(av, 1))
     620           0 :     gerepileall(av, 2, &T2, &M2);  /* destroy S1 */
     621        1018 :   T3 = subtract_slices(n1, p1, T2, 0, n1, 0, p2, B, 0, n1, 0, p1, l);
     622        1018 :   if (gc_needed(av, 1))
     623           0 :     gerepileall(av, 2, &M2, &T3);  /* destroy T2 */
     624        1018 :   S2 = add_slices(m2, n1, A, m1, m2, 0, n1, A, m1, m2, n1, n2, l);
     625        1018 :   T1 = subtract_slices(n1, p1, B, 0, n1, p1, p2, B, 0, n1, 0, p2, l);
     626        1018 :   M3 = Flm_mul_i(S2, T1, m2 + 1, n1 + 1, p2 + 1, l, li);
     627        1018 :   if (gc_needed(av, 1))
     628           0 :     gerepileall(av, 4, &M2, &T3, &S2, &M3);  /* destroy T1 */
     629        1018 :   S3 = subtract_slices(m1, n1, S2, 0, m2, 0, n1, A, 0, m1, 0, n1, l);
     630        1018 :   if (gc_needed(av, 1))
     631           0 :     gerepileall(av, 4, &M2, &T3, &M3, &S3);  /* destroy S2 */
     632        1018 :   A11 = matslice(A, 1, m1, 1, n1);
     633        1018 :   B11 = matslice(B, 1, n1, 1, p1);
     634        1018 :   M1 = Flm_mul_i(A11, B11, m1 + 1, n1 + 1, p1 + 1, l, li);
     635        1018 :   if (gc_needed(av, 1))
     636           0 :     gerepileall(av, 5, &M2, &T3, &M3, &S3, &M1);  /* destroy A11, B11 */
     637        1018 :   A12 = matslice(A, 1, m1, n1 + 1, n);
     638        1018 :   B21 = matslice(B, n1 + 1, n, 1, p1);
     639        1018 :   M4 = Flm_mul_i(A12, B21, m1 + 1, n2 + 1, p1 + 1, l, li);
     640        1018 :   if (gc_needed(av, 1))
     641           0 :     gerepileall(av, 6, &M2, &T3, &M3, &S3, &M1, &M4);  /* destroy A12, B21 */
     642        1018 :   add_slices_ip(m1, p1, M1, 0, m1, 0, p1, M4, 0, m1, 0, p1, C, 0, 0, l);
     643        1018 :   if (gc_needed(av, 1))
     644           0 :     gerepileall(av, 5, &M2, &T3, &M3, &S3, &M1);  /* destroy M4 */
     645        1018 :   M5 = Flm_mul_i(S3, T3, m1 + 1, n1 + 1, p1 + 1, l, li);
     646        1018 :   S4 = subtract_slices(m1, n2, A, 0, m1, n1, n2, S3, 0, m1, 0, n2, l);
     647        1018 :   if (gc_needed(av, 1))
     648           0 :     gerepileall(av, 6, &M2, &T3, &M3, &M1, &M5, &S4);  /* destroy S3 */
     649        1018 :   T4 = add_slices(n2, p1, B, n1, n2, 0, p1, T3, 0, n2, 0, p1, l);
     650        1018 :   if (gc_needed(av, 1))
     651           0 :     gerepileall(av, 6, &M2, &M3, &M1, &M5, &S4, &T4);  /* destroy T3 */
     652        1018 :   V1 = subtract_slices(m1, p1, M1, 0, m1, 0, p1, M5, 0, m1, 0, p1, l);
     653        1018 :   if (gc_needed(av, 1))
     654           0 :     gerepileall(av, 5, &M2, &M3, &S4, &T4, &V1);  /* destroy M1, M5 */
     655        1018 :   B22 = matslice(B, n1 + 1, n, p1 + 1, p);
     656        1018 :   M6 = Flm_mul_i(S4, B22, m1 + 1, n2 + 1, p2 + 1, l, li);
     657        1018 :   if (gc_needed(av, 1))
     658           0 :     gerepileall(av, 5, &M2, &M3, &T4, &V1, &M6);  /* destroy S4, B22 */
     659        1018 :   A22 = matslice(A, m1 + 1, m, n1 + 1, n);
     660        1018 :   M7 = Flm_mul_i(A22, T4, m2 + 1, n2 + 1, p1 + 1, l, li);
     661        1018 :   if (gc_needed(av, 1))
     662           0 :     gerepileall(av, 5, &M2, &M3, &V1, &M6, &M7);  /* destroy A22, T4 */
     663        1018 :   V3 = add_slices(m1, p2, V1, 0, m1, 0, p2, M3, 0, m2, 0, p2, l);
     664        1018 :   add_slices_ip(m1, p2, V3, 0, m1, 0, p2, M6, 0, m1, 0, p2, C, 0, p1, l);
     665        1018 :   if (gc_needed(av, 1))
     666           0 :     gerepileall(av, 4, &M2, &M3, &V1, &M7);  /* destroy V3, M6 */
     667        1018 :   V2 = add_slices(m2, p1, V1, 0, m2, 0, p1, M2, 0, m2, 0, p2, l);
     668        1018 :   if (gc_needed(av, 1))
     669           0 :     gerepileall(av, 3, &M3, &M7, &V2);  /* destroy V1, M2 */
     670        1018 :   add_slices_ip(m2, p1, V2, 0, m2, 0, p1, M7, 0, m2, 0, p1, C, m1, 0, l);
     671        1018 :   add_slices_ip(m2, p2, V2, 0, m2, 0, p2, M3, 0, m2, 0, p2, C, m1, p1, l);
     672        1018 :   set_avma(av); return C;
     673             : }
     674             : 
     675             : /* Strassen-Winograd used for dim >= ZM_sw_bound */
     676             : static GEN
     677    10607132 : Flm_mul_i(GEN x, GEN y, long l, long lx, long ly, ulong p, ulong pi)
     678             : {
     679    10607132 :   ulong e = expu(p);
     680             : #ifdef LONG_IS_64BIT
     681     8686549 :   long ZM_sw_bound = e <= 29 ? 140: e <=62 ? 40: 70;
     682             : #else
     683     1920585 :   long ZM_sw_bound = e <= 12 ? 230: e <=14 ? 170 : e <=17 ? 110: 120;
     684             : #endif
     685    10607134 :   if (l <= ZM_sw_bound || lx <= ZM_sw_bound || ly <= ZM_sw_bound)
     686    10606116 :     return Flm_mul_classical(x, y, l, lx, ly, p, pi);
     687             :   else
     688        1018 :     return Flm_mul_sw(x, y, l - 1, lx - 1, ly - 1, p, pi);
     689             : }
     690             : 
     691             : GEN
     692    10600068 : Flm_mul(GEN x, GEN y, ulong p)
     693             : {
     694    10600068 :   long lx=lg(x), ly=lg(y);
     695    10600068 :   if (ly==1) return cgetg(1,t_MAT);
     696    10599963 :   if (lx==1) return zero_Flm(0, ly-1);
     697    10599963 :   return Flm_mul_i(x, y, lgcols(x), lx, ly, p, get_Fl_red(p));
     698             : }
     699             : 
     700             : GEN
     701       91076 : F2m_mul(GEN x, GEN y)
     702             : {
     703       91076 :   long i,j,l,lx=lg(x), ly=lg(y);
     704             :   GEN z;
     705       91076 :   if (ly==1) return cgetg(1,t_MAT);
     706       91076 :   z = cgetg(ly,t_MAT);
     707       91076 :   if (lx==1)
     708             :   {
     709           0 :     for (i=1; i<ly; i++) gel(z,i) = mkvecsmall(0);
     710           0 :     return z;
     711             :   }
     712       91076 :   l = coeff(x,1,1);
     713       91076 :   for (j=1; j<ly; j++) gel(z,j) = F2m_F2c_mul_i(x, gel(y,j), lx, l);
     714       91076 :   return z;
     715             : }
     716             : 
     717             : struct _Flm
     718             : {
     719             :   ulong p;
     720             :   long n;
     721             : };
     722             : 
     723             : static GEN
     724        8421 : _Flm_mul(void *E , GEN x, GEN y)
     725        8421 : { return Flm_mul(x,y,((struct _Flm*)E)->p); }
     726             : static GEN
     727       43379 : _Flm_sqr(void *E, GEN x)
     728       43379 : { return Flm_mul(x,x,((struct _Flm*)E)->p); }
     729             : static GEN
     730         126 : _Flm_one(void *E)
     731         126 : { return matid_Flm(((struct _Flm*)E)->n); }
     732             : GEN
     733       24528 : Flm_powu(GEN x, ulong n, ulong p)
     734             : {
     735       24528 :   pari_sp av = avma;
     736             :   struct _Flm d;
     737       24528 :   if (!n) return matid(lg(x)-1);
     738       24528 :   d.p = p;
     739       24528 :   return gerepileupto(av, gen_powu(x, n, (void*)&d, &_Flm_sqr, &_Flm_mul));
     740             : }
     741             : GEN
     742         126 : Flm_powers(GEN x, ulong n, ulong p)
     743             : {
     744         126 :   pari_sp av = avma;
     745             :   struct _Flm d;
     746         126 :   d.p = p;
     747         126 :   d.n = lg(x)-1;
     748         126 :   return gerepileupto(av, gen_powers(x, n, 1, (void*)&d,
     749             :                           &_Flm_sqr, &_Flm_mul, &_Flm_one));
     750             : }
     751             : 
     752             : static GEN
     753           0 : _F2m_mul(void *data, GEN x, GEN y)
     754           0 : { (void) data; return F2m_mul(x,y); }
     755             : static GEN
     756           0 : _F2m_sqr(void *data, GEN x)
     757           0 : { (void) data; return F2m_mul(x,x); }
     758             : GEN
     759           0 : F2m_powu(GEN x, ulong n)
     760             : {
     761           0 :   pari_sp av = avma;
     762           0 :   if (!n) return matid(lg(x)-1);
     763           0 :   return gerepileupto(av, gen_powu(x, n,NULL, &_F2m_sqr, &_F2m_mul));
     764             : }
     765             : static GEN
     766           0 : _FpM_mul(void *p , GEN x, GEN y)
     767           0 : { return FpM_mul(x,y,(GEN)p); }
     768             : static GEN
     769           0 : _FpM_sqr(void *p, GEN x)
     770           0 : { return FpM_mul(x,x,(GEN)p); }
     771             : GEN
     772           0 : FpM_powu(GEN x, ulong n, GEN p)
     773             : {
     774           0 :   pari_sp av = avma;
     775           0 :   if (!n) return matid(lg(x)-1);
     776           0 :   if (lgefint(p) == 3)
     777             :   {
     778           0 :     pari_sp av = avma;
     779           0 :     ulong pp = uel(p,2);
     780             :     GEN z;
     781           0 :     if (pp == 2)
     782           0 :       z = F2m_to_ZM(F2m_powu(ZM_to_F2m(x),n));
     783             :     else
     784           0 :       z = Flm_to_ZM(Flm_powu(ZM_to_Flm(x, pp), n, pp));
     785           0 :     return gerepileupto(av, z);
     786             :   }
     787           0 :   return gerepileupto(av, gen_powu(x, n, (void*)p, &_FpM_sqr, &_FpM_mul));
     788             : }
     789             : 
     790             : /*Multiple a column vector by a line vector to make a matrix*/
     791             : GEN
     792          14 : Flc_Flv_mul(GEN x, GEN y, ulong p)
     793             : {
     794          14 :   long i,j, lx=lg(x), ly=lg(y);
     795             :   GEN z;
     796          14 :   if (ly==1) return cgetg(1,t_MAT);
     797          14 :   z = cgetg(ly, t_MAT);
     798          49 :   for (j=1; j < ly; j++)
     799             :   {
     800          35 :     GEN zj = cgetg(lx,t_VECSMALL);
     801         112 :     for (i=1; i<lx; i++)
     802          77 :       uel(zj,i) = Fl_mul(uel(x,i), uel(y,j), p);
     803          35 :     gel(z,j) = zj;
     804             :   }
     805          14 :   return z;
     806             : }
     807             : 
     808             : /*Multiple a column vector by a line vector to make a matrix*/
     809             : GEN
     810        1548 : FpC_FpV_mul(GEN x, GEN y, GEN p)
     811             : {
     812        1548 :   long i,j, lx=lg(x), ly=lg(y);
     813             :   GEN z;
     814        1548 :   if (ly==1) return cgetg(1,t_MAT);
     815        1548 :   z = cgetg(ly,t_MAT);
     816       28310 :   for (j=1; j < ly; j++)
     817             :   {
     818       26762 :     GEN zj = cgetg(lx,t_COL);
     819       26762 :     for (i=1; i<lx; i++) gel(zj,i) = Fp_mul(gel(x,i),gel(y,j), p);
     820       26762 :     gel(z, j) = zj;
     821             :   }
     822        1548 :   return z;
     823             : }
     824             : 
     825             : /* Multiply a line vector by a column and return a scalar (t_INT) */
     826             : GEN
     827     1576166 : FpV_dotproduct(GEN x, GEN y, GEN p)
     828             : {
     829     1576166 :   long i, lx = lg(x);
     830             :   pari_sp av;
     831             :   GEN c;
     832     1576166 :   if (lx == 1) return gen_0;
     833     1571679 :   av = avma; c = mulii(gel(x,1),gel(y,1));
     834     1571679 :   for (i=2; i<lx; i++) c = addii(c, mulii(gel(x,i),gel(y,i)));
     835     1571679 :   return gerepileuptoint(av, modii(c,p));
     836             : }
     837             : GEN
     838           0 : FpV_dotsquare(GEN x, GEN p)
     839             : {
     840           0 :   long i, lx = lg(x);
     841             :   pari_sp av;
     842             :   GEN c;
     843           0 :   if (lx == 1) return gen_0;
     844           0 :   av = avma; c = sqri(gel(x,1));
     845           0 :   for (i=2; i<lx; i++) c = addii(c, sqri(gel(x,i)));
     846           0 :   return gerepileuptoint(av, modii(c,p));
     847             : }
     848             : 
     849             : INLINE ulong
     850     2616066 : Flv_dotproductspec_SMALL(GEN x, GEN y, ulong p, long lx)
     851             : {
     852     2616066 :   ulong c = uel(x,0) * uel(y,0);
     853             :   long k;
     854    20362924 :   for (k = 1; k < lx; k++) {
     855    17746858 :     c += uel(x,k) * uel(y,k);
     856    17746858 :     if (c & HIGHBIT) c %= p;
     857             :   }
     858     2616066 :   return c % p;
     859             : }
     860             : 
     861             : INLINE ulong
     862        8262 : Flv_dotproductspec_i(GEN x, GEN y, ulong p, ulong pi, long lx)
     863             : {
     864             :   ulong l0, l1, v1, h0, h1;
     865        8262 :   long i = 0;
     866             :   LOCAL_OVERFLOW;
     867             :   LOCAL_HIREMAINDER;
     868        8262 :   l1 = mulll(uel(x,i), uel(y,i)); h1 = hiremainder; v1 = 0;
     869      183381 :   while (++i < lx) {
     870      166857 :     l0 = mulll(uel(x,i), uel(y,i)); h0 = hiremainder;
     871      166857 :     l1 = addll(l0, l1); h1 = addllx(h0, h1); v1 += overflow;
     872             :   }
     873        8262 :   if (v1 == 0) return remll_pre(h1, l1, p, pi);
     874           3 :   else return remlll_pre(v1, h1, l1, p, pi);
     875             : }
     876             : 
     877             : ulong
     878      321538 : Flv_dotproduct(GEN x, GEN y, ulong p)
     879             : {
     880      321538 :   long lx = lg(x)-1;
     881      321538 :   if (lx == 0) return 0;
     882      321538 :   if (SMALL_ULONG(p))
     883      321538 :     return Flv_dotproductspec_SMALL(x+1, y+1, p, lx);
     884             :   else
     885           0 :     return Flv_dotproductspec_i(x+1, y+1, p, get_Fl_red(p), lx);
     886             : }
     887             : 
     888             : ulong
     889       57183 : Flv_dotproduct_pre(GEN x, GEN y, ulong p, ulong pi)
     890             : {
     891       57183 :   long lx = lg(x)-1;
     892       57183 :   if (lx == 0) return 0;
     893       57183 :   if (SMALL_ULONG(p))
     894       51691 :     return Flv_dotproductspec_SMALL(x+1, y+1, p, lx);
     895             :   else
     896        5492 :     return Flv_dotproductspec_i(x+1, y+1, p, pi, lx);
     897             : }
     898             : 
     899             : ulong
     900     2267372 : Flx_dotproduct(GEN x, GEN y, ulong p)
     901             : {
     902     2267372 :   long lx = minss(lgpol(x), lgpol(y));
     903     2267372 :   if (lx == 0) return 0;
     904     2245606 :   if (SMALL_ULONG(p))
     905     2242836 :     return Flv_dotproductspec_SMALL(x+2, y+2, p, lx);
     906             :   else
     907        2770 :     return Flv_dotproductspec_i(x+2, y+2, p, get_Fl_red(p), lx);
     908             : }
     909             : 
     910             : ulong
     911           0 : F2v_dotproduct(GEN x, GEN y)
     912             : {
     913           0 :   long i, lx = lg(x);
     914             :   ulong c;
     915           0 :   if (lx <= 2) return 0;
     916           0 :   c = uel(x,2) & uel(y,2);
     917           0 :   for (i=3; i<lx; i++) c ^= uel(x,i) & uel(y,i);
     918             : #ifdef LONG_IS_64BIT
     919           0 :   c ^= c >> 32;
     920             : #endif
     921           0 :   c ^= c >> 16;
     922           0 :   c ^= c >>  8;
     923           0 :   c ^= c >>  4;
     924           0 :   c ^= c >>  2;
     925           0 :   c ^= c >>  1;
     926           0 :   return c & 1;
     927             : }
     928             : 
     929             : GEN
     930     1057255 : FpM_FpC_mul(GEN x, GEN y, GEN p)
     931             : {
     932     1057255 :   long lx = lg(x);
     933     1057255 :   return lx==1? cgetg(1,t_COL): FpM_FpC_mul_i(x, y, lx, lgcols(x), p);
     934             : }
     935             : GEN
     936      273869 : Flm_Flc_mul(GEN x, GEN y, ulong p)
     937             : {
     938      273869 :   long l, lx = lg(x);
     939      273869 :   if (lx==1) return cgetg(1,t_VECSMALL);
     940      273869 :   l = lgcols(x);
     941      273869 :   if (p==2)
     942      141953 :     return Flm_Flc_mul_i_2(x, y, lx, l);
     943      131916 :   else if (SMALL_ULONG(p))
     944      131802 :     return Flm_Flc_mul_i_SMALL(x, y, lx, l, p);
     945             :   else
     946         114 :     return Flm_Flc_mul_i(x, y, lx, l, p, get_Fl_red(p));
     947             : }
     948             : 
     949             : GEN
     950        4816 : Flm_Flc_mul_pre(GEN x, GEN y, ulong p, ulong pi)
     951             : {
     952        4816 :   long l, lx = lg(x);
     953             :   GEN z;
     954        4816 :   if (lx==1) return cgetg(1,t_VECSMALL);
     955        4816 :   l = lgcols(x);
     956        4816 :   z = cgetg(l, t_VECSMALL);
     957        4816 :   if (SMALL_ULONG(p))
     958        4128 :     __Flm_Flc_mul_i_SMALL(z, x, y, lx, l, p);
     959             :   else
     960         688 :     __Flm_Flc_mul_i(z, x, y, lx, l, p, pi);
     961        4816 :   return z;
     962             : }
     963             : 
     964             : GEN
     965     7455657 : Flm_Flc_mul_pre_Flx(GEN x, GEN y, ulong p, ulong pi, long sv)
     966             : {
     967     7455657 :   long l, lx = lg(x);
     968             :   GEN z;
     969     7455657 :   if (lx==1) return pol0_Flx(sv);
     970     7455657 :   l = lgcols(x);
     971     7452501 :   z = cgetg(l + 1, t_VECSMALL); z[1] = sv;
     972     7469357 :   if (SMALL_ULONG(p))
     973     3105810 :     __Flm_Flc_mul_i_SMALL(z + 1, x, y, lx, l, p);
     974             :   else
     975     4363547 :     __Flm_Flc_mul_i(z + 1, x, y, lx, l, p, pi);
     976     7498808 :   return Flx_renormalize(z, l + 1);
     977             : }
     978             : 
     979             : GEN
     980           0 : F2m_F2c_mul(GEN x, GEN y)
     981             : {
     982           0 :   long l, lx = lg(x);
     983           0 :   if (lx==1) return cgetg(1,t_VECSMALL);
     984           0 :   l = coeff(x,1,1);
     985           0 :   return F2m_F2c_mul_i(x, y, lx, l);
     986             : }
     987             : /* RgV_to_RgX(FpM_FpC_mul(x,y,p), v), p != NULL, memory clean */
     988             : GEN
     989      325419 : FpM_FpC_mul_FpX(GEN x, GEN y, GEN p, long v)
     990             : {
     991      325419 :   long i, l, lx = lg(x);
     992             :   GEN z;
     993      325419 :   if (lx==1) return pol_0(v);
     994      325419 :   l = lgcols(x);
     995      325419 :   z = new_chunk(l+1);
     996      710508 :   for (i=l-1; i; i--)
     997             :   {
     998      561637 :     pari_sp av = avma;
     999      561637 :     GEN p1 = ZMrow_ZC_mul_i(x,y,lx,i);
    1000      561637 :     p1 = modii(p1, p);
    1001      561637 :     if (signe(p1))
    1002             :     {
    1003      176548 :       if (i != l-1) stackdummy((pari_sp)(z + l+1), (pari_sp)(z + i+2));
    1004      176548 :       gel(z,i+1) = gerepileuptoint(av, p1);
    1005      176548 :       break;
    1006             :     }
    1007      385089 :     set_avma(av);
    1008             :   }
    1009      325419 :   if (!i) { avma = (pari_sp)(z + l+1); return pol_0(v); }
    1010      176548 :   z[0] = evaltyp(t_POL) | evallg(i+2);
    1011      176548 :   z[1] = evalsigne(1) | evalvarn(v);
    1012      546051 :   for (; i; i--)
    1013             :   {
    1014      369503 :     pari_sp av = avma;
    1015      369503 :     GEN p1 = ZMrow_ZC_mul_i(x,y,lx,i);
    1016      369503 :     gel(z,i+1) = gerepileuptoint(av, modii(p1,p));
    1017             :   }
    1018      176548 :   return z;
    1019             : }
    1020             : 
    1021             : /********************************************************************/
    1022             : /**                                                                **/
    1023             : /**                           TRANSPOSITION                        **/
    1024             : /**                                                                **/
    1025             : /********************************************************************/
    1026             : 
    1027             : /* == zm_transpose */
    1028             : GEN
    1029      270996 : Flm_transpose(GEN x)
    1030             : {
    1031      270996 :   long i, dx, lx = lg(x);
    1032             :   GEN y;
    1033      270996 :   if (lx == 1) return cgetg(1,t_MAT);
    1034      270891 :   dx = lgcols(x); y = cgetg(dx,t_MAT);
    1035      270891 :   for (i=1; i<dx; i++) gel(y,i) = Flm_row(x,i);
    1036      270892 :   return y;
    1037             : }
    1038             : 
    1039             : /********************************************************************/
    1040             : /**                                                                **/
    1041             : /**                           SCALAR MATRICES                      **/
    1042             : /**                                                                **/
    1043             : /********************************************************************/
    1044             : 
    1045             : GEN
    1046        1904 : gen_matid(long n, void *E, const struct bb_field *S)
    1047             : {
    1048        1904 :   GEN y = cgetg(n+1,t_MAT), _0, _1;
    1049             :   long i;
    1050        1904 :   if (n < 0) pari_err_DOMAIN("gen_matid", "dimension","<",gen_0,stoi(n));
    1051        1904 :   _0 = S->s(E,0);
    1052        1904 :   _1 = S->s(E,1);
    1053        8484 :   for (i=1; i<=n; i++)
    1054             :   {
    1055        6580 :     GEN z = const_col(n, _0); gel(z,i) = _1;
    1056        6580 :     gel(y, i) = z;
    1057             :   }
    1058        1904 :   return y;
    1059             : }
    1060             : 
    1061             : GEN
    1062       20424 : matid_F2m(long n)
    1063             : {
    1064       20424 :   GEN y = cgetg(n+1,t_MAT);
    1065             :   long i;
    1066       20424 :   if (n < 0) pari_err_DOMAIN("matid_F2m", "dimension","<",gen_0,stoi(n));
    1067       20424 :   for (i=1; i<=n; i++) { gel(y,i) = zero_F2v(n); F2v_set(gel(y,i),i); }
    1068       20424 :   return y;
    1069             : }
    1070             : 
    1071             : GEN
    1072          35 : matid_F2xqM(long n, GEN T)
    1073             : {
    1074             :   void *E;
    1075          35 :   const struct bb_field *S = get_F2xq_field(&E, T);
    1076          35 :   return gen_matid(n, E, S);
    1077             : }
    1078             : GEN
    1079        1869 : matid_FlxqM(long n, GEN T, ulong p)
    1080             : {
    1081             :   void *E;
    1082        1869 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    1083        1869 :   return gen_matid(n, E, S);
    1084             : }
    1085             : 
    1086             : GEN
    1087      909676 : matid_Flm(long n)
    1088             : {
    1089      909676 :   GEN y = cgetg(n+1,t_MAT);
    1090             :   long i;
    1091      909674 :   if (n < 0) pari_err_DOMAIN("matid_Flm", "dimension","<",gen_0,stoi(n));
    1092      909663 :   for (i=1; i<=n; i++) { gel(y,i) = zero_zv(n); ucoeff(y, i,i) = 1; }
    1093      909673 :   return y;
    1094             : }
    1095             : 
    1096             : GEN
    1097          42 : scalar_Flm(long s, long n)
    1098             : {
    1099             :   long i;
    1100          42 :   GEN y = cgetg(n+1,t_MAT);
    1101          42 :   for (i=1; i<=n; i++) { gel(y,i) = zero_Flv(n); coeff(y, i,i) = s; }
    1102          42 :   return y;
    1103             : }
    1104             : 
    1105             : /********************************************************************/
    1106             : /**                                                                **/
    1107             : /**                           CONVERSIONS                          **/
    1108             : /**                                                                **/
    1109             : /********************************************************************/
    1110             : GEN
    1111    21669250 : ZV_to_Flv(GEN x, ulong p)
    1112    21669250 : { pari_APPLY_ulong(umodiu(gel(x,i), p)) }
    1113             : 
    1114             : GEN
    1115     3953214 : ZM_to_Flm(GEN x, ulong p)
    1116     3953214 : { pari_APPLY_same(ZV_to_Flv(gel(x,i), p)) }
    1117             : 
    1118             : GEN
    1119        2317 : ZMV_to_FlmV(GEN x, ulong m)
    1120        2317 : { pari_APPLY_type(t_VEC,ZM_to_Flm(gel(x,i), m)) }
    1121             : 
    1122             : /*                          TO INTMOD                        */
    1123             : static GEN
    1124    12900345 : to_intmod(GEN x, GEN p) { retmkintmod(modii(x, p), p); }
    1125             : static GEN
    1126       19376 : Fl_to_intmod(ulong x, GEN p) { retmkintmod(utoi(x), p); }
    1127             : 
    1128             : GEN
    1129      297254 : Fp_to_mod(GEN z, GEN p)
    1130             : {
    1131      297254 :   retmkintmod(modii(z, p), icopy(p));
    1132             : }
    1133             : 
    1134             : /* z in Z[X], return z * Mod(1,p), normalized*/
    1135             : GEN
    1136     2063171 : FpX_to_mod(GEN z, GEN p)
    1137             : {
    1138     2063171 :   long i,l = lg(z);
    1139             :   GEN x;
    1140     2063171 :   if (l == 2)
    1141             :   {
    1142      524937 :     x = cgetg(3,t_POL); x[1] = z[1];
    1143      524937 :     gel(x,2) = mkintmod(gen_0,icopy(p)); return x;
    1144             :   }
    1145     1538234 :   x = cgetg(l,t_POL);
    1146     1538234 :   if (l >2) p = icopy(p);
    1147     1538234 :   for (i=2; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1148     1538234 :   x[1] = z[1]; return normalizepol_lg(x,l);
    1149             : }
    1150             : 
    1151             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1152             : GEN
    1153        6853 : FpV_to_mod(GEN z, GEN p)
    1154             : {
    1155        6853 :   long i,l = lg(z);
    1156        6853 :   GEN x = cgetg(l, t_VEC);
    1157        6853 :   if (l == 1) return x;
    1158        6853 :   p = icopy(p);
    1159        6853 :   for (i=1; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1160        6853 :   return x;
    1161             : }
    1162             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1163             : GEN
    1164          77 : FpC_to_mod(GEN z, GEN p)
    1165             : {
    1166          77 :   long i, l = lg(z);
    1167          77 :   GEN x = cgetg(l, t_COL);
    1168          77 :   if (l == 1) return x;
    1169          63 :   p = icopy(p);
    1170          63 :   for (i=1; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1171          63 :   return x;
    1172             : }
    1173             : /* z in Mat m,n(Z), return z * Mod(1,p), normalized*/
    1174             : GEN
    1175       56392 : FpM_to_mod(GEN z, GEN p)
    1176             : {
    1177       56392 :   long i, j, m, l = lg(z);
    1178       56392 :   GEN  x = cgetg(l,t_MAT), y, zi;
    1179       56392 :   if (l == 1) return x;
    1180       56371 :   m = lgcols(z);
    1181       56371 :   p = icopy(p);
    1182      181986 :   for (i=1; i<l; i++)
    1183             :   {
    1184      125615 :     gel(x,i) = cgetg(m,t_COL);
    1185      125615 :     y = gel(x,i); zi= gel(z,i);
    1186      125615 :     for (j=1; j<m; j++) gel(y,j) = to_intmod(gel(zi,j), p);
    1187             :   }
    1188       56371 :   return x;
    1189             : }
    1190             : GEN
    1191          28 : Flc_to_mod(GEN z, ulong pp)
    1192             : {
    1193          28 :   long i, l = lg(z);
    1194          28 :   GEN p, x = cgetg(l, t_COL);
    1195          28 :   if (l == 1) return x;
    1196          28 :   p = utoipos(pp);
    1197          28 :   for (i=1; i<l; i++) gel(x,i) = Fl_to_intmod(z[i], p);
    1198          28 :   return x;
    1199             : }
    1200             : GEN
    1201         189 : Flm_to_mod(GEN z, ulong pp)
    1202             : {
    1203         189 :   long i, j, m, l = lg(z);
    1204         189 :   GEN p, x = cgetg(l,t_MAT), y, zi;
    1205         189 :   if (l == 1) return x;
    1206         161 :   m = lgcols(z);
    1207         161 :   p = utoipos(pp);
    1208        1414 :   for (i=1; i<l; i++)
    1209             :   {
    1210        1253 :     gel(x,i) = cgetg(m,t_COL);
    1211        1253 :     y = gel(x,i); zi= gel(z,i);
    1212        1253 :     for (j=1; j<m; j++) gel(y,j) = Fl_to_intmod(zi[j], p);
    1213             :   }
    1214         161 :   return x;
    1215             : }
    1216             : 
    1217             : GEN
    1218         574 : FpVV_to_mod(GEN z, GEN p)
    1219             : {
    1220         574 :   long i, j, m, l = lg(z);
    1221         574 :   GEN  x = cgetg(l,t_VEC), y, zi;
    1222         574 :   if (l == 1) return x;
    1223         574 :   m = lgcols(z);
    1224         574 :   p = icopy(p);
    1225        1246 :   for (i=1; i<l; i++)
    1226             :   {
    1227         672 :     gel(x,i) = cgetg(m,t_VEC);
    1228         672 :     y = gel(x,i); zi= gel(z,i);
    1229         672 :     for (j=1; j<m; j++) gel(y,j) = to_intmod(gel(zi,j), p);
    1230             :   }
    1231         574 :   return x;
    1232             : }
    1233             : 
    1234             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1235             : GEN
    1236           7 : FpXQC_to_mod(GEN z, GEN T, GEN p)
    1237             : {
    1238           7 :   long i,l = lg(z);
    1239           7 :   GEN x = cgetg(l, t_COL);
    1240           7 :   if (l == 1) return x;
    1241           7 :   T = FpX_to_mod(T, p);
    1242          21 :   for (i=1; i<l; i++)
    1243          14 :     gel(x,i) = mkpolmod(FpX_to_mod(gel(z,i), p), T);
    1244           7 :   return x;
    1245             : }
    1246             : 
    1247             : static GEN
    1248      584437 : Fq_to_mod_raw(GEN x, GEN T, GEN p)
    1249             : {
    1250      584437 :   GEN z = typ(x)==t_INT ? Fp_to_mod(x, p): FpX_to_mod(x, p);
    1251      584437 :   return mkpolmod(z, T);
    1252             : }
    1253             : 
    1254             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1255             : static GEN
    1256      108115 : FqC_to_mod_raw(GEN x, GEN T, GEN p)
    1257      108115 : { pari_APPLY_type(t_COL, Fq_to_mod_raw(gel(x,i), T, p)) }
    1258             : 
    1259             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1260             : GEN
    1261       26159 : FqM_to_mod(GEN z, GEN T, GEN p)
    1262             : {
    1263             :   GEN x;
    1264       26159 :   long i,l = lg(z);
    1265       26159 :   if (!T) return FpM_to_mod(z, p);
    1266       26159 :   x = cgetg(l, t_MAT);
    1267       26159 :   if (l == 1) return x;
    1268       26131 :   T = FpX_to_mod(T, p);
    1269      134246 :   for (i=1; i<l; i++)
    1270      108115 :     gel(x,i) = FqC_to_mod_raw(gel(z, i), T, p);
    1271       26131 :   return x;
    1272             : }
    1273             : 
    1274             : /********************************************************************/
    1275             : /*                                                                  */
    1276             : /*                     Blackbox linear algebra                      */
    1277             : /*                                                                  */
    1278             : /********************************************************************/
    1279             : 
    1280             : /* A sparse column (zCs) v is a t_COL with two components C and E which are
    1281             :  * t_VECSMALL of the same length, representing sum_i E[i]*e_{C[i]}, where
    1282             :  * (e_j) is the canonical basis.
    1283             :  * A sparse matrix (zMs) is a t_VEC of zCs */
    1284             : 
    1285             : /* FpCs and FpMs are identical but E[i] is interpreted as a _signed_ C long
    1286             :  * integer representing an element of Fp. This is important since p can be
    1287             :  * large and p+E[i] would not fit in a C long.  */
    1288             : 
    1289             : /* RgCs and RgMs are similar, except that the type of the component is
    1290             :  * unspecified. Functions handling RgCs/RgMs must be independent of the type
    1291             :  * of E. */
    1292             : 
    1293             : /* Most functions take an argument nbrow which is the number of lines of the
    1294             :  * column/matrix, which cannot be derived from the data. */
    1295             : 
    1296             : GEN
    1297           0 : zCs_to_ZC(GEN R, long nbrow)
    1298             : {
    1299           0 :   GEN C = gel(R,1), E = gel(R,2), c = zerocol(nbrow);
    1300           0 :   long j, l = lg(C);
    1301           0 :   for (j = 1; j < l; ++j) gel(c, C[j]) = stoi(E[j]);
    1302           0 :   return c;
    1303             : }
    1304             : 
    1305             : GEN
    1306           0 : zMs_to_ZM(GEN x, long nbrow)
    1307           0 : { pari_APPLY_same(zCs_to_ZC(gel(x, i), nbrow)) }
    1308             : 
    1309             : /* Solve equation f(X) = B (mod p) where B is a FpV, and f is an endomorphism.
    1310             :  * Return either a solution as a t_COL, or a kernel vector as a t_VEC. */
    1311             : GEN
    1312         126 : gen_FpM_Wiedemann(void *E, GEN (*f)(void*, GEN), GEN B, GEN p)
    1313             : {
    1314         126 :   pari_sp ltop = avma;
    1315         126 :   long col = 0, n = lg(B)-1, m = 2*n+1;
    1316         126 :   if (ZV_equal0(B)) return zerocol(n);
    1317         252 :   while (++col <= n)
    1318             :   {
    1319         126 :     pari_sp btop = avma, av;
    1320             :     long i, lQ;
    1321         126 :     GEN V, Q, M, W = B;
    1322         126 :     GEN b = cgetg(m+2, t_POL);
    1323         126 :     b[1] = evalsigne(1)|evalvarn(0);
    1324         126 :     gel(b, 2) = gel(W, col);
    1325       51524 :     for (i = 3; i<m+2; i++)
    1326       51398 :       gel(b, i) = cgeti(lgefint(p));
    1327         126 :     av = avma;
    1328       51524 :     for (i = 3; i<m+2; i++)
    1329             :     {
    1330       51398 :       W = f(E, W);
    1331       51398 :       affii(gel(W, col),gel(b, i));
    1332       51398 :       if (gc_needed(av,1))
    1333             :       {
    1334        2000 :         if (DEBUGMEM>1) pari_warn(warnmem,"Wiedemann: first loop, %ld",i);
    1335        2000 :         W = gerepileupto(av, W);
    1336             :       }
    1337             :     }
    1338         126 :     b = FpX_renormalize(b, m+2);
    1339         126 :     if (lgpol(b)==0) {set_avma(btop); continue; }
    1340         126 :     M = FpX_halfgcd(b, pol_xn(m, 0), p);
    1341         126 :     Q = FpX_neg(FpX_normalize(gcoeff(M, 2, 1),p),p);
    1342         126 :     W = B; lQ =lg(Q);
    1343         126 :     if (DEBUGLEVEL) err_printf("Wiedemann: deg. minpoly: %ld\n",lQ-3);
    1344         126 :     V = FpC_Fp_mul(W, gel(Q, lQ-2), p);
    1345         126 :     av = avma;
    1346       25279 :     for (i = lQ-3; i > 1; i--)
    1347             :     {
    1348       25153 :       W = f(E, W);
    1349       25153 :       V = ZC_lincomb(gen_1, gel(Q,i), V, W);
    1350       25153 :       if (gc_needed(av,1))
    1351             :       {
    1352        1583 :         if (DEBUGMEM>1) pari_warn(warnmem,"Wiedemann: second loop, %ld",i);
    1353        1583 :         gerepileall(av, 2, &V, &W);
    1354             :       }
    1355             :     }
    1356         126 :     V = FpC_red(V, p);
    1357         126 :     W = FpC_sub(f(E,V), B, p);
    1358         252 :     if (ZV_equal0(W)) return gerepilecopy(ltop, V);
    1359           0 :     av = avma;
    1360           0 :     for (i = 1; i <= n; ++i)
    1361             :     {
    1362           0 :       V = W;
    1363           0 :       W = f(E, V);
    1364           0 :       if (ZV_equal0(W))
    1365           0 :         return gerepilecopy(ltop, shallowtrans(V));
    1366           0 :       gerepileall(av, 2, &V, &W);
    1367             :     }
    1368           0 :     set_avma(btop);
    1369             :   }
    1370           0 :   return NULL;
    1371             : }
    1372             : 
    1373             : GEN
    1374           0 : zMs_ZC_mul(GEN M, GEN B)
    1375             : {
    1376             :   long i, j;
    1377           0 :   long n = lg(B)-1;
    1378           0 :   GEN V = zerocol(n);
    1379           0 :   for (i = 1; i <= n; ++i)
    1380           0 :     if (signe(gel(B, i)))
    1381             :     {
    1382           0 :       GEN R = gel(M, i), C = gel(R, 1), E = gel(R, 2);
    1383           0 :       long l = lg(C);
    1384           0 :       for (j = 1; j < l; ++j)
    1385             :       {
    1386           0 :         long k = C[j];
    1387           0 :         switch(E[j])
    1388             :         {
    1389             :         case 1:
    1390           0 :           gel(V, k) = gel(V,k)==gen_0 ? gel(B,i) : addii(gel(V, k), gel(B,i));
    1391           0 :           break;
    1392             :         case -1:
    1393           0 :           gel(V, k) = gel(V,k)==gen_0 ? negi(gel(B,i)) : subii(gel(V, k), gel(B,i));
    1394           0 :           break;
    1395             :         default:
    1396           0 :           gel(V, k) = gel(V,k)==gen_0 ? mulis(gel(B, i), E[j]) : addii(gel(V, k), mulis(gel(B, i), E[j]));
    1397           0 :           break;
    1398             :         }
    1399             :       }
    1400             :     }
    1401           0 :   return V;
    1402             : }
    1403             : 
    1404             : GEN
    1405           0 : FpMs_FpC_mul(GEN M, GEN B, GEN p) { return FpC_red(zMs_ZC_mul(M, B), p); }
    1406             : 
    1407             : GEN
    1408       76803 : ZV_zMs_mul(GEN B, GEN M)
    1409             : {
    1410             :   long i, j;
    1411       76803 :   long m = lg(M)-1;
    1412       76803 :   GEN V = cgetg(m+1,t_VEC);
    1413    39360976 :   for (i = 1; i <= m; ++i)
    1414             :   {
    1415    39284173 :     GEN R = gel(M, i), C = gel(R, 1), E = gel(R, 2);
    1416    39284173 :     long l = lg(C);
    1417    39284173 :     GEN z = mulis(gel(B, C[1]), E[1]);
    1418   330272856 :     for (j = 2; j < l; ++j)
    1419             :     {
    1420   290988683 :       long k = C[j];
    1421   290988683 :       switch(E[j])
    1422             :       {
    1423             :       case 1:
    1424   196103761 :         z = addii(z, gel(B,k));
    1425   196103761 :         break;
    1426             :       case -1:
    1427    79342515 :         z = subii(z, gel(B,k));
    1428    79342515 :         break;
    1429             :       default:
    1430    15542407 :         z = addii(z, mulis(gel(B,k), E[j]));
    1431    15542407 :         break;
    1432             :       }
    1433             :     }
    1434    39284173 :     gel(V,i) = z;
    1435             :   }
    1436       76803 :   return V;
    1437             : }
    1438             : 
    1439             : GEN
    1440         126 : FpV_FpMs_mul(GEN B, GEN M, GEN p) { return FpV_red(ZV_zMs_mul(B, M), p); }
    1441             : 
    1442             : GEN
    1443           0 : ZlM_gauss(GEN a, GEN b, ulong p, long e, GEN C)
    1444             : {
    1445           0 :   pari_sp av = avma, av2;
    1446           0 :   GEN xi, xb, pi = gen_1, P;
    1447             :   long i;
    1448           0 :   if (!C) {
    1449           0 :     C = Flm_inv(ZM_to_Flm(a, p), p);
    1450           0 :     if (!C) pari_err_INV("ZlM_gauss", a);
    1451             :   }
    1452           0 :   P = utoipos(p);
    1453           0 :   av2 = avma;
    1454           0 :   xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    1455           0 :   xb = Flm_to_ZM(xi);
    1456           0 :   for (i = 2; i <= e; i++)
    1457             :   {
    1458           0 :     pi = muliu(pi, p); /* = p^(i-1) */
    1459           0 :     b = ZM_Z_divexact(ZM_sub(b, ZM_nm_mul(a, xi)), P);
    1460           0 :     if (gc_needed(av,2))
    1461             :     {
    1462           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZlM_gauss. i=%ld/%ld",i,e);
    1463           0 :       gerepileall(av2,3, &pi,&b,&xb);
    1464             :     }
    1465           0 :     xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    1466           0 :     xb = ZM_add(xb, nm_Z_mul(xi, pi));
    1467             :   }
    1468           0 :   return gerepileupto(av, xb);
    1469             : }
    1470             : 
    1471             : struct wrapper_modp_s {
    1472             :   GEN (*f)(void*, GEN);
    1473             :   void *E;
    1474             :   GEN p;
    1475             : };
    1476             : 
    1477             : /* compute f(x) mod p */
    1478             : static GEN
    1479       76677 : wrap_relcomb_modp(void *E, GEN x)
    1480             : {
    1481       76677 :   struct wrapper_modp_s *W = (struct wrapper_modp_s*)E;
    1482       76677 :   return FpC_red(W->f(W->E, x), W->p);
    1483             : }
    1484             : static GEN
    1485           0 : wrap_relcomb(void*E, GEN x) { return zMs_ZC_mul((GEN)E, x); }
    1486             : 
    1487             : static GEN
    1488       76677 : wrap_relker(void*E, GEN x) { return ZV_zMs_mul(x, (GEN)E); }
    1489             : 
    1490             : /* Solve f(X) = B (mod p^e); blackbox version of ZlM_gauss */
    1491             : GEN
    1492           0 : gen_ZpM_Dixon(void *E, GEN (*f)(void*, GEN), GEN B, GEN p, long e)
    1493             : {
    1494             :   struct wrapper_modp_s W;
    1495           0 :   pari_sp av = avma;
    1496           0 :   GEN xb, xi, pi = gen_1;
    1497             :   long i;
    1498           0 :   W.E = E;
    1499           0 :   W.f = f;
    1500           0 :   W.p = p;
    1501           0 :   xi = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, FpC_red(B, p), p); /* f^(-1) B */
    1502           0 :   if (!xi || e == 1 || typ(xi) == t_VEC) return xi;
    1503           0 :   xb = xi;
    1504           0 :   for (i = 2; i <= e; i++)
    1505             :   {
    1506           0 :     pi = mulii(pi, p); /* = p^(i-1) */
    1507           0 :     B = ZC_Z_divexact(ZC_sub(B, f(E, xi)), p);
    1508           0 :     if (gc_needed(av,2))
    1509             :     {
    1510           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_ZpM_Dixon. i=%ld",i);
    1511           0 :       gerepileall(av,3, &pi,&B,&xb);
    1512             :     }
    1513           0 :     xi = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, FpC_red(B, p), p);
    1514           0 :     if (!xi) return NULL;
    1515           0 :     if (typ(xi) == t_VEC) return gerepileupto(av, xi);
    1516           0 :     xb = ZC_add(xb, ZC_Z_mul(xi, pi));
    1517             :   }
    1518           0 :   return gerepileupto(av, xb);
    1519             : }
    1520             : 
    1521             : static GEN
    1522       25699 : vecprow(GEN A, GEN prow)
    1523             : {
    1524       25699 :   return mkvec2(vecsmallpermute(prow,gel(A,1)), gel(A,2));
    1525             : }
    1526             : 
    1527             : /* Solve the equation MX = A. Return either a solution as a t_COL,
    1528             :  * or the index of a column which is linearly dependent from the others as a
    1529             :  * t_VECSMALL with a single component. */
    1530             : GEN
    1531           0 : ZpMs_ZpCs_solve(GEN M, GEN A, long nbrow, GEN p, long e)
    1532             : {
    1533           0 :   pari_sp av = avma;
    1534             :   GEN pcol, prow;
    1535           0 :   long nbi=lg(M)-1, lR;
    1536             :   long i, n;
    1537             :   GEN Mp, Ap, Rp;
    1538             :   pari_timer ti;
    1539           0 :   if (DEBUGLEVEL) timer_start(&ti);
    1540           0 :   RgMs_structelim(M, nbrow, gel(A, 1), &pcol, &prow);
    1541           0 :   if (!pcol) return gc_NULL(av);
    1542           0 :   if (DEBUGLEVEL)
    1543           0 :     timer_printf(&ti,"structured elimination (%ld -> %ld)",nbi,lg(pcol)-1);
    1544           0 :   n = lg(pcol)-1;
    1545           0 :   Mp = cgetg(n+1, t_MAT);
    1546           0 :   for(i=1; i<=n; i++)
    1547           0 :     gel(Mp, i) = vecprow(gel(M,pcol[i]), prow);
    1548           0 :   Ap = zCs_to_ZC(vecprow(A, prow), n);
    1549           0 :   if (DEBUGLEVEL) timer_start(&ti);
    1550           0 :   Rp = gen_ZpM_Dixon((void*)Mp,wrap_relcomb, Ap, p, e);
    1551           0 :   if (DEBUGLEVEL) timer_printf(&ti,"linear algebra");
    1552           0 :   if (!Rp) return gc_NULL(av);
    1553           0 :   lR = lg(Rp)-1;
    1554           0 :   if (typ(Rp) == t_COL)
    1555             :   {
    1556           0 :     GEN R = zerocol(nbi+1);
    1557           0 :     for (i=1; i<=lR; i++)
    1558           0 :        gel(R,pcol[i]) = gel(Rp,i);
    1559           0 :     return gerepilecopy(av, R);
    1560             :   }
    1561           0 :   for (i = 1; i <= lR; ++i)
    1562           0 :     if (signe(gel(Rp, i)))
    1563           0 :       return gerepileuptoleaf(av, mkvecsmall(pcol[i]));
    1564             :   return NULL; /* LCOV_EXCL_LINE */
    1565             : }
    1566             : 
    1567             : GEN
    1568           0 : FpMs_FpCs_solve(GEN M, GEN A, long nbrow, GEN p)
    1569             : {
    1570           0 :   return ZpMs_ZpCs_solve(M, A, nbrow, p, 1);
    1571             : }
    1572             : 
    1573             : GEN
    1574           0 : FpMs_FpCs_solve_safe(GEN M, GEN A, long nbrow, GEN p)
    1575             : {
    1576             :   GEN res;
    1577           0 :   pari_CATCH(e_INV)
    1578             :   {
    1579           0 :     GEN E = pari_err_last();
    1580           0 :     GEN x = err_get_compo(E,2);
    1581           0 :     if (typ(x) != t_INTMOD) pari_err(0,E);
    1582           0 :     if (DEBUGLEVEL)
    1583           0 :       pari_warn(warner,"FpMs_FpCs_solve_safe, impossible inverse %Ps", x);
    1584           0 :     res = NULL;
    1585             :   } pari_TRY {
    1586           0 :     res = ZpMs_ZpCs_solve(M, A, nbrow, p, 1);
    1587           0 :   } pari_ENDCATCH
    1588           0 :   return res;
    1589             : }
    1590             : 
    1591             : static GEN
    1592         126 : FpMs_structelim_back(GEN M, GEN V, GEN prow, GEN p)
    1593             : {
    1594         126 :   long i, j, oldf = 0, f = 0;
    1595         126 :   long lrow = lg(prow), lM = lg(M);
    1596         126 :   GEN W = const_vecsmall(lM-1,1);
    1597         126 :   GEN R = cgetg(lrow, t_VEC);
    1598      237272 :   for (i=1; i<lrow; i++)
    1599      237146 :     gel(R,i) = prow[i]==0 ? NULL: gel(V,prow[i]);
    1600             :   do
    1601             :   {
    1602         415 :     oldf = f;
    1603      444461 :     for(i=1; i<lM; i++)
    1604      444046 :       if (W[i])
    1605             :       {
    1606      150743 :         GEN C = gel(M,i), F = gel(C,1), E = gel(C,2);
    1607      150743 :         long c=0, cj=0, lF = lg(F);
    1608     1317812 :         for(j=1; j<lF; j++)
    1609     1167069 :           if (!gel(R,F[j]))
    1610       88375 :           { c++; cj=j; }
    1611      150743 :         if (c>=2) continue;
    1612      129593 :         if (c==1)
    1613             :         {
    1614       41696 :           pari_sp av = avma;
    1615       41696 :           GEN s = gen_0;
    1616      362325 :           for(j=1; j<lF; j++)
    1617      320629 :             if (j!=cj) s = Fp_add(s, mulis(gel(R,F[j]), E[j]), p);
    1618       41696 :           gel(R,F[cj]) = gerepileupto(av, Fp_div(Fp_neg(s, p), stoi(E[cj]), p));
    1619       41696 :           f++;
    1620             :         }
    1621      129593 :         W[i]=0;
    1622             :       }
    1623         415 :   } while(oldf!=f);
    1624      237272 :   for (i=1; i<lrow; i++)
    1625      237146 :     if (!gel(R,i)) gel(R,i) = gen_0;
    1626         126 :   return R;
    1627             : }
    1628             : 
    1629             : /* Return a linear form Y such that YM is essentially 0 */
    1630             : GEN
    1631         126 : FpMs_leftkernel_elt_col(GEN M, long nbcol, long nbrow, GEN p)
    1632             : {
    1633         126 :   pari_sp av = avma, av2;
    1634             :   GEN pcol, prow;
    1635             :   long i, n;
    1636             :   GEN Mp, B, MB, R, Rp;
    1637             :   pari_timer ti;
    1638             :   struct wrapper_modp_s W;
    1639         126 :   if (DEBUGLEVEL) timer_start(&ti);
    1640         126 :   RgMs_structelim_col(M, nbcol, nbrow, cgetg(1,t_VECSMALL), &pcol, &prow);
    1641         126 :   if (!pcol) return gc_NULL(av);
    1642         126 :   if (DEBUGLEVEL)
    1643           0 :     timer_printf(&ti,"structured elimination (%ld -> %ld)",nbcol,lg(pcol)-1);
    1644         126 :   n = lg(pcol)-1;
    1645         126 :   Mp = cgetg(n+1, t_MAT);
    1646       25825 :   for(i=1; i<=n; i++)
    1647       25699 :     gel(Mp, i) = vecprow(gel(M,pcol[i]), prow);
    1648         126 :   W.E = (void*) Mp;
    1649         126 :   W.f = wrap_relker;
    1650         126 :   W.p = p;
    1651         126 :   av2 = avma;
    1652             :   for(;;)
    1653             :   {
    1654         126 :     set_avma(av2);
    1655         126 :     B = random_FpV(n, p);
    1656         126 :     MB = FpV_FpMs_mul(B, Mp, p);
    1657         126 :     if (DEBUGLEVEL) timer_start(&ti);
    1658         126 :     pari_CATCH(e_INV)
    1659             :     {
    1660           0 :       GEN E = pari_err_last();
    1661           0 :       GEN x = err_get_compo(E,2);
    1662           0 :       if (typ(x) != t_INTMOD) pari_err(0,E);
    1663           0 :       if (DEBUGLEVEL)
    1664           0 :         pari_warn(warner,"FpMs_leftkernel_elt, impossible inverse %Ps", x);
    1665           0 :       Rp = NULL;
    1666             :     } pari_TRY {
    1667         126 :       Rp = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, MB, p);
    1668         126 :     } pari_ENDCATCH
    1669         126 :     if (!Rp) continue;
    1670         126 :     if (typ(Rp)==t_COL)
    1671             :     {
    1672         126 :       Rp = FpC_sub(Rp,B,p);
    1673         126 :       if (ZV_equal0(Rp)) continue;
    1674             :     }
    1675         126 :     R = FpMs_structelim_back(M, Rp, prow, p);
    1676         126 :     if (DEBUGLEVEL) timer_printf(&ti,"Wiedemann left kernel");
    1677         126 :     return gerepilecopy(av, R);
    1678             :   }
    1679             : }
    1680             : 
    1681             : GEN
    1682          42 : FpMs_leftkernel_elt(GEN M, long nbrow, GEN p)
    1683             : {
    1684          42 :   return FpMs_leftkernel_elt_col(M, lg(M)-1, nbrow, p);
    1685             : }

Generated by: LCOV version 1.13