Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - FpV.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21342-bb34613) Lines: 712 930 76.6 %
Date: 2017-11-18 06:21:14 Functions: 102 129 79.1 %
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     9215391 : FpC_red(GEN x, GEN p)
      25     9215391 : { 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      202013 : FpV_red(GEN x, GEN p)
      30      202013 : { pari_APPLY_type(t_VEC, modii(gel(x,i), p)) }
      31             : 
      32             : GEN
      33     1029473 : FpC_center(GEN x, GEN p, GEN pov2)
      34     1029473 : { 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      111692 : Flv_center(GEN x, ulong p, ulong ps2)
      51      111692 : { 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     1774173 : FpM_red(GEN x, GEN p)
      56     1774173 : { pari_APPLY_same(FpC_red(gel(x,i), p)) }
      57             : 
      58             : GEN
      59      135311 : FpM_center(GEN x, GEN p, GEN pov2)
      60      135311 : { 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        7896 : Flm_center(GEN x, ulong p, ulong ps2)
      70        7896 : { pari_APPLY_same(Flv_center(gel(x,i), p,ps2)) }
      71             : 
      72             : GEN
      73           0 : random_FpV(long d, GEN p)
      74             : {
      75             :   long i;
      76           0 :   GEN y = cgetg(d+1,t_VEC);
      77           0 :   for (i=1; i<=d; i++) gel(y,i) = randomi(p);
      78           0 :   return y;
      79             : }
      80             : 
      81             : GEN
      82         140 : random_FpC(long d, GEN p)
      83             : {
      84             :   long i;
      85         140 :   GEN y = cgetg(d+1,t_COL);
      86         140 :   for (i=1; i<=d; i++) gel(y,i) = randomi(p);
      87         140 :   return y;
      88             : }
      89             : 
      90             : GEN
      91        3374 : random_Flv(long n, ulong p)
      92             : {
      93        3374 :   GEN y = cgetg(n+1, t_VECSMALL);
      94             :   long i;
      95        3374 :   for (i=1; i<=n; i++) uel(y,i) = random_Fl(p);
      96        3374 :   return y;
      97             : }
      98             : 
      99             : /********************************************************************/
     100             : /**                                                                **/
     101             : /**                           ADD, SUB                             **/
     102             : /**                                                                **/
     103             : /********************************************************************/
     104             : GEN
     105      137060 : FpC_add(GEN x, GEN y, GEN p)
     106      137060 : { 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      628761 : Flv_add(GEN x, GEN y, ulong p)
     118             : {
     119      628761 :   if (p==2)
     120       87703 :     pari_APPLY_ulong(x[i]^y[i])
     121             :   else
     122      541058 :     pari_APPLY_ulong(Fl_add(x[i], y[i], p))
     123             : }
     124             : 
     125             : void
     126      168260 : Flv_add_inplace(GEN x, GEN y, ulong p)
     127             : {
     128      168260 :   long i, l = lg(x);
     129      168260 :   if (p==2)
     130      165894 :     for (i = 1; i < l; i++) x[i] ^= y[i];
     131             :   else
     132        2366 :     for (i = 1; i < l; i++) x[i] = Fl_add(x[i], y[i], p);
     133      168260 : }
     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      199507 : FpC_sub(GEN x, GEN y, GEN p)
     149      199507 : { 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    85952365 : Flv_sub(GEN x, GEN y, ulong p)
     161    85952365 : { 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        7560 : Flm_Fl_add(GEN x, ulong y, ulong p)
     172             : {
     173        7560 :   long l = lg(x), i, j;
     174        7560 :   GEN z = cgetg(l,t_MAT);
     175             : 
     176        7560 :   if (l==1) return z;
     177        7560 :   if (l != lgcols(x)) pari_err_OP( "+", x, utoi(y));
     178       31003 :   for (i=1; i<l; i++)
     179             :   {
     180       23443 :     GEN zi = cgetg(l,t_VECSMALL), xi = gel(x,i);
     181       23443 :     gel(z,i) = zi;
     182       23443 :     for (j=1; j<l; j++) zi[j] = xi[j];
     183       23443 :     zi[i] = Fl_add(zi[i], y, p);
     184             :   }
     185        7560 :   return z;
     186             : }
     187             : 
     188             : GEN
     189        6993 : Flm_add(GEN x, GEN y, ulong p)
     190        6993 : { pari_APPLY_same(Flv_add(gel(x,i), gel(y,i), p)) }
     191             : 
     192             : GEN
     193     8784407 : Flm_sub(GEN x, GEN y, ulong p)
     194     8784407 : { pari_APPLY_same(Flv_sub(gel(x,i), gel(y,i), p)) }
     195             : 
     196             : /********************************************************************/
     197             : /**                                                                **/
     198             : /**                           MULTIPLICATION                       **/
     199             : /**                                                                **/
     200             : /********************************************************************/
     201             : GEN
     202      622216 : FpC_Fp_mul(GEN x, GEN y, GEN p)
     203      622216 : { pari_APPLY_type(t_COL, Fp_mul(gel(x,i), y, p)) }
     204             : 
     205             : GEN
     206      433225 : Flv_Fl_mul(GEN x, ulong y, ulong p)
     207      433225 : { pari_APPLY_ulong(Fl_mul(x[i], y, p)) }
     208             : 
     209             : GEN
     210         329 : Flv_Fl_div(GEN x, ulong y, ulong p)
     211             : {
     212         329 :   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        1351 : FpM_Fp_mul(GEN X, GEN c, GEN p) {
     221        1351 :   long i, j, h, l = lg(X);
     222        1351 :   GEN A = cgetg(l, t_MAT);
     223        1351 :   if (l == 1) return A;
     224        1351 :   h = lgcols(X);
     225       10479 :   for (j=1; j<l; j++)
     226             :   {
     227        9128 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     228        9128 :     for (i = 1; i < h; i++) gel(a,i) = Fp_mul(gel(x,i), c, p);
     229        9128 :     gel(A,j) = a;
     230             :   }
     231        1351 :   return A;
     232             : }
     233             : 
     234             : /* x *= y */
     235             : void
     236     1539737 : Flv_Fl_mul_part_inplace(GEN x, ulong y, ulong p, long l)
     237             : {
     238             :   long i;
     239     1539737 :   for (i=1;i<=l;i++) x[i] = Fl_mul(x[i], y, p);
     240     1539737 : }
     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     7966295 : Flm_Fl_mul(GEN y, ulong x, ulong p)
     263             : {
     264     7966295 :   long i, j, m, l = lg(y);
     265     7966295 :   GEN z = cgetg(l, t_MAT);
     266     7966169 :   if (l == 1) return z;
     267     7966169 :   m = lgcols(y);
     268     7967600 :   if (HIGHWORD(x | p))
     269    63025819 :     for(j=1; j<l; j++) {
     270    57899138 :       GEN c = cgetg(m, t_VECSMALL); gel(z,j) = c;
     271    57727677 :       for(i=1; i<m; i++) c[i] = Fl_mul(ucoeff(y,i,j), x, p);
     272             :     }
     273             :   else
     274    15265580 :     for(j=1; j<l; j++) {
     275    12425046 :       GEN c = cgetg(m, t_VECSMALL); gel(z,j) = c;
     276    12425046 :       for(i=1; i<m; i++) c[i] = (ucoeff(y,i,j) * x) % p;
     277             :     }
     278     7967215 :   return z;
     279             : }
     280             : 
     281             : GEN
     282     1806850 : Flv_neg(GEN x, ulong p)
     283     1806850 : { pari_APPLY_ulong(Fl_neg(uel(x,i), p)) }
     284             : 
     285             : void
     286        5289 : Flv_neg_inplace(GEN v, ulong p)
     287             : {
     288             :   long i;
     289      167660 :   for (i = 1; i < lg(v); ++i)
     290      162371 :     v[i] = Fl_neg(v[i], p);
     291        5289 : }
     292             : 
     293             : GEN
     294      168501 : Flm_neg(GEN x, ulong p)
     295      168501 : { 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    11488454 : ZMrow_ZC_mul_i(GEN x, GEN y, long lx, long i)
     300             : {
     301    11488454 :   GEN c = mulii(gcoeff(x,i,1), gel(y,1));
     302             :   long k;
     303   174010034 :   for (k = 2; k < lx; k++)
     304             :   {
     305   162521580 :     GEN t = mulii(gcoeff(x,i,k), gel(y,k));
     306   162521580 :     if (signe(t)) c = addii(c, t);
     307             :   }
     308    11488454 :   return c;
     309             : }
     310             : 
     311             : static long
     312    21635523 : zmrow_zc_mul(GEN x, GEN y, long lx, long i)
     313             : {
     314             :   long k;
     315    21635523 :   long c = coeff(x,i,1) * y[1];
     316   289227596 :   for (k = 2; k < lx; k++)
     317   267592073 :     c += coeff(x,i,k) * y[k];
     318    21635523 :   return c;
     319             : }
     320             : 
     321             : GEN
     322     1653225 : zm_zc_mul(GEN x, GEN y)
     323             : {
     324     1653225 :   long lx = lg(x), l, i;
     325             :   GEN z;
     326     1653225 :   if (lx == 1) return cgetg(1, t_VECSMALL);
     327     1653225 :   l = lg(gel(x,1));
     328     1653225 :   z = cgetg(l,t_VECSMALL);
     329     1653225 :   for (i=1; i<l; i++) z[i] = zmrow_zc_mul(x, y, lx, i);
     330     1653225 :   return z;
     331             : }
     332             : 
     333             : GEN
     334         252 : zm_mul(GEN x, GEN y)
     335             : {
     336         252 :   long i,j,lx=lg(x), ly=lg(y);
     337             :   GEN z;
     338         252 :   if (ly==1) return cgetg(1,t_MAT);
     339         252 :   z = cgetg(ly,t_MAT);
     340         252 :   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        2128 :   for (j=1; j<ly; j++)
     346        1876 :     gel(z,j) = zm_zc_mul(x, gel(y,j));
     347         252 :   return z;
     348             : }
     349             : 
     350             : static ulong
     351   184643206 : Flmrow_Flc_mul_SMALL(GEN x, GEN y, ulong p, long lx, long i)
     352             : {
     353   184643206 :   ulong c = ucoeff(x,i,1) * uel(y,1);
     354             :   long k;
     355  2921535716 :   for (k = 2; k < lx; k++) {
     356  2736892510 :     c += ucoeff(x,i,k) * uel(y,k);
     357  2736892510 :     if (c & HIGHBIT) c %= p;
     358             :   }
     359   184643206 :   return c % p;
     360             : }
     361             : 
     362             : static ulong
     363   197980953 : 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   197980953 :   long k = 1;
     367             :   LOCAL_OVERFLOW;
     368             :   LOCAL_HIREMAINDER;
     369   197980953 :   l1 = mulll(ucoeff(x,i,k), uel(y,k)); h1 = hiremainder; v1 = 0;
     370  2205382329 :   while (++k < lx) {
     371  1809420423 :     l0 = mulll(ucoeff(x,i,k), uel(y,k)); h0 = hiremainder;
     372  1809420423 :     l1 = addll(l0, l1); h1 = addllx(h0, h1); v1 += overflow;
     373             :   }
     374   197980953 :   if (v1 == 0) return remll_pre(h1, l1, p, pi);
     375     4039701 :   else return remlll_pre(v1, h1, l1, p, pi);
     376             : }
     377             : 
     378             : static GEN
     379       82250 : Flm_Flc_mul_i_2(GEN x, GEN y, long lx, long l)
     380             : {
     381             :   long i,j;
     382       82250 :   GEN z = NULL;
     383             : 
     384     1059751 :   for (j=1; j<lx; j++)
     385             :   {
     386      977501 :     if (!y[j]) continue;
     387      304116 :     if (!z) z = Flv_copy(gel(x,j));
     388      229531 :     else for (i = 1; i < l; i++) z[i] ^= coeff(x,i,j);
     389             :   }
     390       82250 :   if (!z) z = zero_zv(l-1);
     391       82250 :   return z;
     392             : }
     393             : 
     394             : static GEN
     395      941038 : FpM_FpC_mul_i(GEN x, GEN y, long lx, long l, GEN p)
     396             : {
     397      941038 :   GEN z = cgetg(l,t_COL);
     398             :   long i;
     399    11499010 :   for (i = 1; i < l; i++)
     400             :   {
     401    10557972 :     pari_sp av = avma;
     402    10557972 :     GEN c = ZMrow_ZC_mul_i(x, y, lx, i);
     403    10557972 :     gel(z,i) = gerepileuptoint(av, modii(c,p));
     404             :   }
     405      941038 :   return z;
     406             : }
     407             : 
     408             : static void
     409    22693588 : __Flm_Flc_mul_i_SMALL(GEN z, GEN x, GEN y, long lx, long l, ulong p)
     410             : {
     411             :   long i;
     412    22693588 :   for (i = 1; i < l; i++) z[i] = Flmrow_Flc_mul_SMALL(x, y, p, lx, i);
     413    22675424 : }
     414             : static GEN
     415    19553625 : Flm_Flc_mul_i_SMALL(GEN x, GEN y, long lx, long l, ulong p)
     416             : {
     417    19553625 :   GEN z = cgetg(l,t_VECSMALL);
     418    19553625 :   __Flm_Flc_mul_i_SMALL(z, x, y, lx, l, p);
     419    19553625 :   return z;
     420             : }
     421             : 
     422             : static void
     423    39479480 : __Flm_Flc_mul_i(GEN z, GEN x, GEN y, long lx, long l, ulong p, ulong pi)
     424             : {
     425             :   long i;
     426    39479480 :   for (i = 1; i < l; i++) z[i] = Flmrow_Flc_mul_i(x, y, p, pi, lx, i);
     427    39190135 : }
     428             : static GEN
     429    34986217 : Flm_Flc_mul_i(GEN x, GEN y, long lx, long l, ulong p, ulong pi)
     430             : {
     431    34986217 :   GEN z = cgetg(l,t_VECSMALL);
     432    34988055 :   __Flm_Flc_mul_i(z, x, y, lx, l, p, pi);
     433    34985341 :   return z;
     434             : }
     435             : 
     436             : INLINE GEN
     437      292091 : F2m_F2c_mul_i(GEN x, GEN y, long lx, long l)
     438             : {
     439             :   long j;
     440      292091 :   GEN z = NULL;
     441             : 
     442     3445271 :   for (j=1; j<lx; j++)
     443             :   {
     444     3153180 :     if (!F2v_coeff(y,j)) continue;
     445      706630 :     if (!z) z = vecsmall_copy(gel(x,j));
     446      435035 :     else F2v_add_inplace(z,gel(x,j));
     447             :   }
     448      292091 :   if (!z) z = zero_F2v(l);
     449      292091 :   return z;
     450             : }
     451             : 
     452             : GEN
     453      376492 : FpM_mul(GEN x, GEN y, GEN p)
     454             : {
     455      376492 :   long j, l, lx=lg(x), ly=lg(y);
     456             :   GEN z;
     457      376492 :   if (ly==1) return cgetg(1,t_MAT);
     458      376492 :   if (lx==1) return zeromat(0, ly-1);
     459      376492 :   if (lgefint(p) == 3)
     460             :   {
     461      372910 :     pari_sp av = avma;
     462      372910 :     ulong pp = uel(p,2);
     463      372910 :     if (pp == 2)
     464             :     {
     465       61058 :       x = ZM_to_F2m(x);
     466       61058 :       y = ZM_to_F2m(y);
     467       61058 :       z = F2m_to_ZM(F2m_mul(x,y));
     468             :     }
     469             :     else
     470             :     {
     471      311852 :       x = ZM_to_Flm(x, pp);
     472      311852 :       y = ZM_to_Flm(y, pp);
     473      311852 :       z = Flm_to_ZM(Flm_mul(x,y, pp));
     474             :     }
     475      372910 :     return gerepileupto(av, z);
     476             :   }
     477        3582 :   l = lgcols(x); z = cgetg(ly,t_MAT);
     478        3582 :   for (j=1; j<ly; j++) gel(z,j) = FpM_FpC_mul_i(x, gel(y,j), lx, l, p);
     479        3582 :   return z;
     480             : }
     481             : 
     482             : static GEN
     483     6399503 : Flm_mul_classical(GEN x, GEN y, long l, long lx, long ly, ulong p, ulong pi)
     484             : {
     485             :   long j;
     486     6399503 :   GEN z = cgetg(ly, t_MAT);
     487     6399231 :   if (SMALL_ULONG(p))
     488    22265707 :     for (j=1; j<ly; j++)
     489    19427781 :       gel(z,j) = Flm_Flc_mul_i_SMALL(x, gel(y,j), lx, l, p);
     490             :   else
     491    38539475 :     for (j=1; j<ly; j++)
     492    34977989 :       gel(z,j) = Flm_Flc_mul_i(x, gel(y,j), lx, l, p, pi);
     493     6399412 :   return z;
     494             : }
     495             : 
     496             : /*
     497             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     498             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     499             : */
     500             : static void
     501         976 : add_slices_ip(long m, long n,
     502             :            GEN A, long ma, long da, long na, long ea,
     503             :            GEN B, long mb, long db, long nb, long eb,
     504             :            GEN M, long dy, long dx, ulong p)
     505             : {
     506         976 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     507             :   GEN C;
     508             : 
     509       48459 :   for (j = 1; j <= min_e; j++) {
     510       47483 :     C = gel(M, j + dx) + dy;
     511     3126437 :     for (i = 1; i <= min_d; i++)
     512     6157908 :       uel(C, i) = Fl_add(ucoeff(A, ma + i, na + j),
     513     3078954 :                         ucoeff(B, mb + i, nb + j), p);
     514       49960 :     for (; i <= da; i++)
     515        2477 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     516       47483 :     for (; i <= db; i++)
     517           0 :       uel(C, i) = ucoeff(B, mb + i, nb + j);
     518       47483 :     for (; i <= m; i++)
     519           0 :       uel(C, i) = 0;
     520             :   }
     521        1075 :   for (; j <= ea; j++) {
     522          99 :     C = gel(M, j + dx) + dy;
     523        6339 :     for (i = 1; i <= da; i++)
     524        6240 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     525          99 :     for (; i <= m; i++)
     526           0 :       uel(C, i) = 0;
     527             :   }
     528         976 :   for (; j <= eb; j++) {
     529           0 :     C = gel(M, j + dx) + dy;
     530           0 :     for (i = 1; i <= db; i++)
     531           0 :       uel(C, i) = ucoeff(B, mb + i, nb + j);
     532           0 :     for (; i <= m; i++)
     533           0 :       uel(C, i) = 0;
     534             :   }
     535         976 :   for (; j <= n; j++) {
     536           0 :     C = gel(M, j + dx) + dy;
     537           0 :     for (i = 1; i <= m; i++)
     538           0 :       uel(C, i) = 0;
     539             :   }
     540         976 : }
     541             : 
     542             : static GEN
     543         488 : add_slices(long m, long n,
     544             :            GEN A, long ma, long da, long na, long ea,
     545             :            GEN B, long mb, long db, long nb, long eb, ulong p)
     546             : {
     547             :   GEN M;
     548             :   long j;
     549         488 :   M = cgetg(n + 1, t_MAT);
     550       23908 :   for (j = 1; j <= n; j++)
     551       23420 :     gel(M, j) = cgetg(m + 1, t_VECSMALL);
     552         488 :   add_slices_ip(m, n, A, ma, da, na, ea, B, mb, db, nb, eb, M, 0, 0, p);
     553         488 :   return M;
     554             : }
     555             : 
     556             : /*
     557             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     558             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     559             : */
     560             : static GEN
     561         854 : subtract_slices(long m, long n,
     562             :                 GEN A, long ma, long da, long na, long ea,
     563             :                 GEN B, long mb, long db, long nb, long eb, ulong p)
     564             : {
     565         854 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     566         854 :   GEN M = cgetg(n + 1, t_MAT), C;
     567             : 
     568       40784 :   for (j = 1; j <= min_e; j++) {
     569       39930 :     gel(M, j) = C = cgetg(m + 1, t_VECSMALL);
     570     2545507 :     for (i = 1; i <= min_d; i++)
     571     5011154 :       uel(C, i) = Fl_sub(ucoeff(A, ma + i, na + j),
     572     2505577 :                         ucoeff(B, mb + i, nb + j), p);
     573       43969 :     for (; i <= da; i++)
     574        4039 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     575       41856 :     for (; i <= db; i++)
     576        1926 :       uel(C, i) = Fl_neg(ucoeff(B, mb + i, nb + j), p);
     577       39930 :     for (; i <= m; i++)
     578           0 :       uel(C, i) = 0;
     579             :   }
     580         854 :   for (; j <= ea; j++) {
     581           0 :     gel(M, j) = C = cgetg(m + 1, t_VECSMALL);
     582           0 :     for (i = 1; i <= da; i++)
     583           0 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     584           0 :     for (; i <= m; i++)
     585           0 :       uel(C, i) = 0;
     586             :   }
     587         873 :   for (; j <= eb; j++) {
     588          19 :     gel(M, j) = C = cgetg(m + 1, t_VECSMALL);
     589        1351 :     for (i = 1; i <= db; i++)
     590        1332 :       uel(C, i) = Fl_neg(ucoeff(B, mb + i, nb + j), p);
     591          19 :     for (; i <= m; i++)
     592           0 :       uel(C, i) = 0;
     593             :   }
     594         873 :   for (; j <= n; j++)
     595          19 :     gel(M, j) = zero_Flv(m);
     596         854 :   return M;
     597             : }
     598             : 
     599             : static GEN Flm_mul_i(GEN x, GEN y, long l, long lx, long ly, ulong p, ulong pi);
     600             : 
     601             : /* Strassen-Winograd matrix product A (m x n) * B (n x p) */
     602             : static GEN
     603         122 : Flm_mul_sw(GEN A, GEN B, long m, long n, long p, ulong l, ulong li)
     604             : {
     605             :   pari_sp av;
     606         122 :   long m1 = (m + 1)/2, m2 = m/2,
     607         122 :     n1 = (n + 1)/2, n2 = n/2,
     608         122 :     p1 = (p + 1)/2, p2 = p/2;
     609             :   GEN A11, A12, A22, B11, B21, B22,
     610             :     S1, S2, S3, S4, T1, T2, T3, T4,
     611             :     M1, M2, M3, M4, M5, M6, M7,
     612             :     V1, V2, V3, C;
     613             :   long j;
     614         122 :   C = cgetg(p + 1, t_MAT);
     615       12203 :   for (j = 1; j <= p; j++)
     616       12081 :     gel(C, j) = cgetg(m + 1, t_VECSMALL);
     617         122 :   av = avma;
     618         122 :   T2 = subtract_slices(n1, p2, B, 0, n1, p1, p2, B, n1, n2, p1, p2, l);
     619         122 :   S1 = subtract_slices(m2, n1, A, m1, m2, 0, n1, A, 0, m2, 0, n1, l);
     620         122 :   M2 = Flm_mul_i(S1, T2, m2 + 1, n1 + 1, p2 + 1, l, li);
     621         122 :   if (gc_needed(av, 1))
     622           0 :     gerepileall(av, 2, &T2, &M2);  /* destroy S1 */
     623         122 :   T3 = subtract_slices(n1, p1, T2, 0, n1, 0, p2, B, 0, n1, 0, p1, l);
     624         122 :   if (gc_needed(av, 1))
     625           0 :     gerepileall(av, 2, &M2, &T3);  /* destroy T2 */
     626         122 :   S2 = add_slices(m2, n1, A, m1, m2, 0, n1, A, m1, m2, n1, n2, l);
     627         122 :   T1 = subtract_slices(n1, p1, B, 0, n1, p1, p2, B, 0, n1, 0, p2, l);
     628         122 :   M3 = Flm_mul_i(S2, T1, m2 + 1, n1 + 1, p2 + 1, l, li);
     629         122 :   if (gc_needed(av, 1))
     630           0 :     gerepileall(av, 4, &M2, &T3, &S2, &M3);  /* destroy T1 */
     631         122 :   S3 = subtract_slices(m1, n1, S2, 0, m2, 0, n1, A, 0, m1, 0, n1, l);
     632         122 :   if (gc_needed(av, 1))
     633           0 :     gerepileall(av, 4, &M2, &T3, &M3, &S3);  /* destroy S2 */
     634         122 :   A11 = matslice(A, 1, m1, 1, n1);
     635         122 :   B11 = matslice(B, 1, n1, 1, p1);
     636         122 :   M1 = Flm_mul_i(A11, B11, m1 + 1, n1 + 1, p1 + 1, l, li);
     637         122 :   if (gc_needed(av, 1))
     638           0 :     gerepileall(av, 5, &M2, &T3, &M3, &S3, &M1);  /* destroy A11, B11 */
     639         122 :   A12 = matslice(A, 1, m1, n1 + 1, n);
     640         122 :   B21 = matslice(B, n1 + 1, n, 1, p1);
     641         122 :   M4 = Flm_mul_i(A12, B21, m1 + 1, n2 + 1, p1 + 1, l, li);
     642         122 :   if (gc_needed(av, 1))
     643           0 :     gerepileall(av, 6, &M2, &T3, &M3, &S3, &M1, &M4);  /* destroy A12, B21 */
     644         122 :   add_slices_ip(m1, p1, M1, 0, m1, 0, p1, M4, 0, m1, 0, p1, C, 0, 0, l);
     645         122 :   if (gc_needed(av, 1))
     646           0 :     gerepileall(av, 5, &M2, &T3, &M3, &S3, &M1);  /* destroy M4 */
     647         122 :   M5 = Flm_mul_i(S3, T3, m1 + 1, n1 + 1, p1 + 1, l, li);
     648         122 :   S4 = subtract_slices(m1, n2, A, 0, m1, n1, n2, S3, 0, m1, 0, n2, l);
     649         122 :   if (gc_needed(av, 1))
     650           0 :     gerepileall(av, 6, &M2, &T3, &M3, &M1, &M5, &S4);  /* destroy S3 */
     651         122 :   T4 = add_slices(n2, p1, B, n1, n2, 0, p1, T3, 0, n2, 0, p1, l);
     652         122 :   if (gc_needed(av, 1))
     653           0 :     gerepileall(av, 6, &M2, &M3, &M1, &M5, &S4, &T4);  /* destroy T3 */
     654         122 :   V1 = subtract_slices(m1, p1, M1, 0, m1, 0, p1, M5, 0, m1, 0, p1, l);
     655         122 :   if (gc_needed(av, 1))
     656           0 :     gerepileall(av, 5, &M2, &M3, &S4, &T4, &V1);  /* destroy M1, M5 */
     657         122 :   B22 = matslice(B, n1 + 1, n, p1 + 1, p);
     658         122 :   M6 = Flm_mul_i(S4, B22, m1 + 1, n2 + 1, p2 + 1, l, li);
     659         122 :   if (gc_needed(av, 1))
     660           0 :     gerepileall(av, 5, &M2, &M3, &T4, &V1, &M6);  /* destroy S4, B22 */
     661         122 :   A22 = matslice(A, m1 + 1, m, n1 + 1, n);
     662         122 :   M7 = Flm_mul_i(A22, T4, m2 + 1, n2 + 1, p1 + 1, l, li);
     663         122 :   if (gc_needed(av, 1))
     664           0 :     gerepileall(av, 5, &M2, &M3, &V1, &M6, &M7);  /* destroy A22, T4 */
     665         122 :   V3 = add_slices(m1, p2, V1, 0, m1, 0, p2, M3, 0, m2, 0, p2, l);
     666         122 :   add_slices_ip(m1, p2, V3, 0, m1, 0, p2, M6, 0, m1, 0, p2, C, 0, p1, l);
     667         122 :   if (gc_needed(av, 1))
     668           6 :     gerepileall(av, 4, &M2, &M3, &V1, &M7);  /* destroy V3, M6 */
     669         122 :   V2 = add_slices(m2, p1, V1, 0, m2, 0, p1, M2, 0, m2, 0, p2, l);
     670         122 :   if (gc_needed(av, 1))
     671           0 :     gerepileall(av, 3, &M3, &M7, &V2);  /* destroy V1, M2 */
     672         122 :   add_slices_ip(m2, p1, V2, 0, m2, 0, p1, M7, 0, m2, 0, p1, C, m1, 0, l);
     673         122 :   add_slices_ip(m2, p2, V2, 0, m2, 0, p2, M3, 0, m2, 0, p2, C, m1, p1, l);
     674         122 :   avma = av; return C;
     675             : }
     676             : 
     677             : /* Strassen-Winograd used for dim >= ZM_sw_bound */
     678             : static GEN
     679     6399654 : Flm_mul_i(GEN x, GEN y, long l, long lx, long ly, ulong p, ulong pi)
     680             : {
     681     6399654 :   ulong e = expu(p);
     682             : #ifdef LONG_IS_64BIT
     683     5035448 :   long ZM_sw_bound = e <= 29 ? 140: e <=62 ? 40: 70;
     684             : #else
     685     1364239 :   long ZM_sw_bound = e <= 12 ? 230: e <=14 ? 170 : e <=17 ? 110: 120;
     686             : #endif
     687     6399687 :   if (l <= ZM_sw_bound || lx <= ZM_sw_bound || ly <= ZM_sw_bound)
     688     6399565 :     return Flm_mul_classical(x, y, l, lx, ly, p, pi);
     689             :   else
     690         122 :     return Flm_mul_sw(x, y, l - 1, lx - 1, ly - 1, p, pi);
     691             : }
     692             : 
     693             : GEN
     694     6398958 : Flm_mul(GEN x, GEN y, ulong p)
     695             : {
     696     6398958 :   long lx=lg(x), ly=lg(y);
     697     6398958 :   if (ly==1) return cgetg(1,t_MAT);
     698     6398958 :   if (lx==1) return zero_Flm(0, ly-1);
     699     6398958 :   return Flm_mul_i(x, y, lgcols(x), lx, ly, p, get_Fl_red(p));
     700             : }
     701             : 
     702             : GEN
     703       61065 : F2m_mul(GEN x, GEN y)
     704             : {
     705       61065 :   long i,j,l,lx=lg(x), ly=lg(y);
     706             :   GEN z;
     707       61065 :   if (ly==1) return cgetg(1,t_MAT);
     708       61065 :   z = cgetg(ly,t_MAT);
     709       61065 :   if (lx==1)
     710             :   {
     711           0 :     for (i=1; i<ly; i++) gel(z,i) = mkvecsmall(0);
     712           0 :     return z;
     713             :   }
     714       61065 :   l = coeff(x,1,1);
     715       61065 :   for (j=1; j<ly; j++) gel(z,j) = F2m_F2c_mul_i(x, gel(y,j), lx, l);
     716       61065 :   return z;
     717             : }
     718             : 
     719             : static GEN
     720        8099 : _Flm_mul(void *p , GEN x, GEN y)
     721        8099 : { return Flm_mul(x,y,*(ulong*)p); }
     722             : static GEN
     723       27258 : _Flm_sqr(void *p, GEN x)
     724       27258 : { return Flm_mul(x,x,*(ulong*)p); }
     725             : GEN
     726       14658 : Flm_powu(GEN x, ulong n, ulong p)
     727             : {
     728       14658 :   pari_sp av = avma;
     729       14658 :   if (!n) return matid(lg(x)-1);
     730       14658 :   return gerepileupto(av, gen_powu(x, n, (void*)&p, &_Flm_sqr, &_Flm_mul));
     731             : }
     732             : static GEN
     733           0 : _F2m_mul(void *data, GEN x, GEN y)
     734           0 : { (void) data; return F2m_mul(x,y); }
     735             : static GEN
     736           0 : _F2m_sqr(void *data, GEN x)
     737           0 : { (void) data; return F2m_mul(x,x); }
     738             : GEN
     739           0 : F2m_powu(GEN x, ulong n)
     740             : {
     741           0 :   pari_sp av = avma;
     742           0 :   if (!n) return matid(lg(x)-1);
     743           0 :   return gerepileupto(av, gen_powu(x, n,NULL, &_F2m_sqr, &_F2m_mul));
     744             : }
     745             : static GEN
     746           0 : _FpM_mul(void *p , GEN x, GEN y)
     747           0 : { return FpM_mul(x,y,(GEN)p); }
     748             : static GEN
     749           0 : _FpM_sqr(void *p, GEN x)
     750           0 : { return FpM_mul(x,x,(GEN)p); }
     751             : GEN
     752           0 : FpM_powu(GEN x, ulong n, GEN p)
     753             : {
     754           0 :   pari_sp av = avma;
     755           0 :   if (!n) return matid(lg(x)-1);
     756           0 :   if (lgefint(p) == 3)
     757             :   {
     758           0 :     pari_sp av = avma;
     759           0 :     ulong pp = uel(p,2);
     760             :     GEN z;
     761           0 :     if (pp == 2)
     762           0 :       z = F2m_to_ZM(F2m_powu(ZM_to_F2m(x),n));
     763             :     else
     764           0 :       z = Flm_to_ZM(Flm_powu(ZM_to_Flm(x, pp), n, pp));
     765           0 :     return gerepileupto(av, z);
     766             :   }
     767           0 :   return gerepileupto(av, gen_powu(x, n, (void*)p, &_FpM_sqr, &_FpM_mul));
     768             : }
     769             : 
     770             : /*Multiple a column vector by a line vector to make a matrix*/
     771             : GEN
     772           7 : Flc_Flv_mul(GEN x, GEN y, ulong p)
     773             : {
     774           7 :   long i,j, lx=lg(x), ly=lg(y);
     775             :   GEN z;
     776           7 :   if (ly==1) return cgetg(1,t_MAT);
     777           7 :   z = cgetg(ly, t_MAT);
     778          28 :   for (j=1; j < ly; j++)
     779             :   {
     780          21 :     GEN zj = cgetg(lx,t_VECSMALL);
     781          84 :     for (i=1; i<lx; i++)
     782          63 :       uel(zj,i) = Fl_mul(uel(x,i), uel(y,j), p);
     783          21 :     gel(z,j) = zj;
     784             :   }
     785           7 :   return z;
     786             : }
     787             : 
     788             : /*Multiple a column vector by a line vector to make a matrix*/
     789             : GEN
     790        1540 : FpC_FpV_mul(GEN x, GEN y, GEN p)
     791             : {
     792        1540 :   long i,j, lx=lg(x), ly=lg(y);
     793             :   GEN z;
     794        1540 :   if (ly==1) return cgetg(1,t_MAT);
     795        1540 :   z = cgetg(ly,t_MAT);
     796       28252 :   for (j=1; j < ly; j++)
     797             :   {
     798       26712 :     GEN zj = cgetg(lx,t_COL);
     799       26712 :     for (i=1; i<lx; i++) gel(zj,i) = Fp_mul(gel(x,i),gel(y,j), p);
     800       26712 :     gel(z, j) = zj;
     801             :   }
     802        1540 :   return z;
     803             : }
     804             : 
     805             : /* Multiply a line vector by a column and return a scalar (t_INT) */
     806             : GEN
     807     1292545 : FpV_dotproduct(GEN x, GEN y, GEN p)
     808             : {
     809     1292545 :   long i, lx = lg(x);
     810             :   pari_sp av;
     811             :   GEN c;
     812     1292545 :   if (lx == 1) return gen_0;
     813     1290410 :   av = avma; c = mulii(gel(x,1),gel(y,1));
     814     1290410 :   for (i=2; i<lx; i++) c = addii(c, mulii(gel(x,i),gel(y,i)));
     815     1290410 :   return gerepileuptoint(av, modii(c,p));
     816             : }
     817             : GEN
     818           0 : FpV_dotsquare(GEN x, GEN p)
     819             : {
     820           0 :   long i, lx = lg(x);
     821             :   pari_sp av;
     822             :   GEN c;
     823           0 :   if (lx == 1) return gen_0;
     824           0 :   av = avma; c = sqri(gel(x,1));
     825           0 :   for (i=2; i<lx; i++) c = addii(c, sqri(gel(x,i)));
     826           0 :   return gerepileuptoint(av, modii(c,p));
     827             : }
     828             : 
     829             : INLINE ulong
     830      692878 : Flv_dotproductspec_SMALL(GEN x, GEN y, ulong p, long lx)
     831             : {
     832      692878 :   ulong c = uel(x,0) * uel(y,0);
     833             :   long k;
     834     6913505 :   for (k = 1; k < lx; k++) {
     835     6220627 :     c += uel(x,k) * uel(y,k);
     836     6220627 :     if (c & HIGHBIT) c %= p;
     837             :   }
     838      692878 :   return c % p;
     839             : }
     840             : 
     841             : INLINE ulong
     842        5542 : Flv_dotproductspec_i(GEN x, GEN y, ulong p, ulong pi, long lx)
     843             : {
     844             :   ulong l0, l1, v1, h0, h1;
     845        5542 :   long i = 0;
     846             :   LOCAL_OVERFLOW;
     847             :   LOCAL_HIREMAINDER;
     848        5542 :   l1 = mulll(uel(x,i), uel(y,i)); h1 = hiremainder; v1 = 0;
     849      166552 :   while (++i < lx) {
     850      155468 :     l0 = mulll(uel(x,i), uel(y,i)); h0 = hiremainder;
     851      155468 :     l1 = addll(l0, l1); h1 = addllx(h0, h1); v1 += overflow;
     852             :   }
     853        5542 :   if (v1 == 0) return remll_pre(h1, l1, p, pi);
     854           0 :   else return remlll_pre(v1, h1, l1, p, pi);
     855             : }
     856             : 
     857             : ulong
     858      254772 : Flv_dotproduct(GEN x, GEN y, ulong p)
     859             : {
     860      254772 :   long lx = lg(x)-1;
     861      254772 :   if (lx == 0) return 0;
     862      254772 :   if (SMALL_ULONG(p))
     863      254772 :     return Flv_dotproductspec_SMALL(x+1, y+1, p, lx);
     864             :   else
     865           0 :     return Flv_dotproductspec_i(x+1, y+1, p, get_Fl_red(p), lx);
     866             : }
     867             : 
     868             : ulong
     869       53458 : Flv_dotproduct_pre(GEN x, GEN y, ulong p, ulong pi)
     870             : {
     871       53458 :   long lx = lg(x)-1;
     872       53458 :   if (lx == 0) return 0;
     873       53458 :   if (SMALL_ULONG(p))
     874       48000 :     return Flv_dotproductspec_SMALL(x+1, y+1, p, lx);
     875             :   else
     876        5458 :     return Flv_dotproductspec_i(x+1, y+1, p, pi, lx);
     877             : }
     878             : 
     879             : ulong
     880      400218 : Flx_dotproduct(GEN x, GEN y, ulong p)
     881             : {
     882      400218 :   long lx = minss(lgpol(x), lgpol(y));
     883      400218 :   if (lx == 0) return 0;
     884      390188 :   if (SMALL_ULONG(p))
     885      390104 :     return Flv_dotproductspec_SMALL(x+2, y+2, p, lx);
     886             :   else
     887          84 :     return Flv_dotproductspec_i(x+2, y+2, p, get_Fl_red(p), lx);
     888             : }
     889             : 
     890             : ulong
     891           0 : F2v_dotproduct(GEN x, GEN y)
     892             : {
     893           0 :   long i, lx = lg(x);
     894             :   ulong c;
     895           0 :   if (lx == 1) return 0;
     896           0 :   c = uel(x,1) & uel(y,1);
     897           0 :   for (i=2; i<lx; i++) c ^= uel(x,i) & uel(y,i);
     898             : #ifdef LONG_IS_64BIT
     899           0 :   c ^= c >> 32;
     900             : #endif
     901           0 :   c ^= c >> 16;
     902           0 :   c ^= c >>  8;
     903           0 :   c ^= c >>  4;
     904           0 :   c ^= c >>  2;
     905           0 :   c ^= c >>  1;
     906           0 :   return c & 1;
     907             : }
     908             : 
     909             : GEN
     910      895614 : FpM_FpC_mul(GEN x, GEN y, GEN p)
     911             : {
     912      895614 :   long lx = lg(x);
     913      895614 :   return lx==1? cgetg(1,t_COL): FpM_FpC_mul_i(x, y, lx, lgcols(x), p);
     914             : }
     915             : GEN
     916      208208 : Flm_Flc_mul(GEN x, GEN y, ulong p)
     917             : {
     918      208208 :   long l, lx = lg(x);
     919      208208 :   if (lx==1) return cgetg(1,t_VECSMALL);
     920      208208 :   l = lgcols(x);
     921      208208 :   if (p==2)
     922       82250 :     return Flm_Flc_mul_i_2(x, y, lx, l);
     923      125958 :   else if (SMALL_ULONG(p))
     924      125844 :     return Flm_Flc_mul_i_SMALL(x, y, lx, l, p);
     925             :   else
     926         114 :     return Flm_Flc_mul_i(x, y, lx, l, p, get_Fl_red(p));
     927             : }
     928             : 
     929             : GEN
     930        4816 : Flm_Flc_mul_pre(GEN x, GEN y, ulong p, ulong pi)
     931             : {
     932        4816 :   long l, lx = lg(x);
     933             :   GEN z;
     934        4816 :   if (lx==1) return cgetg(1,t_VECSMALL);
     935        4816 :   l = lgcols(x);
     936        4816 :   z = cgetg(l, t_VECSMALL);
     937        4816 :   if (SMALL_ULONG(p))
     938        4128 :     __Flm_Flc_mul_i_SMALL(z, x, y, lx, l, p);
     939             :   else
     940         688 :     __Flm_Flc_mul_i(z, x, y, lx, l, p, pi);
     941        4816 :   return z;
     942             : }
     943             : 
     944             : GEN
     945     7292267 : Flm_Flc_mul_pre_Flx(GEN x, GEN y, ulong p, ulong pi, long sv)
     946             : {
     947     7292267 :   long l, lx = lg(x);
     948             :   GEN z;
     949     7292267 :   if (lx==1) return pol0_Flx(sv);
     950     7292267 :   l = lgcols(x);
     951     7296473 :   z = cgetg(l + 1, t_VECSMALL); z[1] = sv;
     952     7297691 :   if (SMALL_ULONG(p))
     953     3115141 :     __Flm_Flc_mul_i_SMALL(z + 1, x, y, lx, l, p);
     954             :   else
     955     4182550 :     __Flm_Flc_mul_i(z + 1, x, y, lx, l, p, pi);
     956     7302012 :   return Flx_renormalize(z, l + 1);
     957             : }
     958             : 
     959             : GEN
     960           0 : F2m_F2c_mul(GEN x, GEN y)
     961             : {
     962           0 :   long l, lx = lg(x);
     963           0 :   if (lx==1) return cgetg(1,t_VECSMALL);
     964           0 :   l = coeff(x,1,1);
     965           0 :   return F2m_F2c_mul_i(x, y, lx, l);
     966             : }
     967             : /* RgV_to_RgX(FpM_FpC_mul(x,y,p), v), p != NULL, memory clean */
     968             : GEN
     969      325062 : FpM_FpC_mul_FpX(GEN x, GEN y, GEN p, long v)
     970             : {
     971      325062 :   long i, l, lx = lg(x);
     972             :   GEN z;
     973      325062 :   if (lx==1) return pol_0(v);
     974      325062 :   l = lgcols(x);
     975      325062 :   z = new_chunk(l+1);
     976      709675 :   for (i=l-1; i; i--)
     977             :   {
     978      561196 :     pari_sp av = avma;
     979      561196 :     GEN p1 = ZMrow_ZC_mul_i(x,y,lx,i);
     980      561196 :     p1 = modii(p1, p);
     981      561196 :     if (signe(p1))
     982             :     {
     983      176583 :       if (i != l-1) stackdummy((pari_sp)(z + l+1), (pari_sp)(z + i+2));
     984      176583 :       gel(z,i+1) = gerepileuptoint(av, p1);
     985      176583 :       break;
     986             :     }
     987      384613 :     avma = av;
     988             :   }
     989      325062 :   if (!i) { avma = (pari_sp)(z + l+1); return pol_0(v); }
     990      176583 :   z[0] = evaltyp(t_POL) | evallg(i+2);
     991      176583 :   z[1] = evalsigne(1) | evalvarn(v);
     992      545869 :   for (; i; i--)
     993             :   {
     994      369286 :     pari_sp av = avma;
     995      369286 :     GEN p1 = ZMrow_ZC_mul_i(x,y,lx,i);
     996      369286 :     gel(z,i+1) = gerepileuptoint(av, modii(p1,p));
     997             :   }
     998      176583 :   return z;
     999             : }
    1000             : 
    1001             : /********************************************************************/
    1002             : /**                                                                **/
    1003             : /**                           TRANSPOSITION                        **/
    1004             : /**                                                                **/
    1005             : /********************************************************************/
    1006             : 
    1007             : /* == zm_transpose */
    1008             : GEN
    1009      366888 : Flm_transpose(GEN x)
    1010             : {
    1011      366888 :   long i, dx, lx = lg(x);
    1012             :   GEN y;
    1013      366888 :   if (lx == 1) return cgetg(1,t_MAT);
    1014      366888 :   dx = lgcols(x); y = cgetg(dx,t_MAT);
    1015      366889 :   for (i=1; i<dx; i++) gel(y,i) = Flm_row(x,i);
    1016      366886 :   return y;
    1017             : }
    1018             : 
    1019             : /********************************************************************/
    1020             : /**                                                                **/
    1021             : /**                           SCALAR MATRICES                      **/
    1022             : /**                                                                **/
    1023             : /********************************************************************/
    1024             : 
    1025             : GEN
    1026        1897 : gen_matid(long n, void *E, const struct bb_field *S)
    1027             : {
    1028        1897 :   GEN y = cgetg(n+1,t_MAT), _0, _1;
    1029             :   long i;
    1030        1897 :   if (n < 0) pari_err_DOMAIN("gen_matid", "dimension","<",gen_0,stoi(n));
    1031        1897 :   _0 = S->s(E,0);
    1032        1897 :   _1 = S->s(E,1);
    1033        8505 :   for (i=1; i<=n; i++)
    1034             :   {
    1035        6608 :     GEN z = const_col(n, _0); gel(z,i) = _1;
    1036        6608 :     gel(y, i) = z;
    1037             :   }
    1038        1897 :   return y;
    1039             : }
    1040             : 
    1041             : GEN
    1042       12866 : matid_F2m(long n)
    1043             : {
    1044       12866 :   GEN y = cgetg(n+1,t_MAT);
    1045             :   long i;
    1046       12866 :   if (n < 0) pari_err_DOMAIN("matid_F2m", "dimension","<",gen_0,stoi(n));
    1047       12866 :   for (i=1; i<=n; i++) { gel(y,i) = zero_F2v(n); F2v_set(gel(y,i),i); }
    1048       12866 :   return y;
    1049             : }
    1050             : 
    1051             : GEN
    1052          35 : matid_F2xqM(long n, GEN T)
    1053             : {
    1054             :   void *E;
    1055          35 :   const struct bb_field *S = get_F2xq_field(&E, T);
    1056          35 :   return gen_matid(n, E, S);
    1057             : }
    1058             : GEN
    1059        1862 : matid_FlxqM(long n, GEN T, ulong p)
    1060             : {
    1061             :   void *E;
    1062        1862 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    1063        1862 :   return gen_matid(n, E, S);
    1064             : }
    1065             : 
    1066             : GEN
    1067      879897 : matid_Flm(long n)
    1068             : {
    1069      879897 :   GEN y = cgetg(n+1,t_MAT);
    1070             :   long i;
    1071      879889 :   if (n < 0) pari_err_DOMAIN("matid_Flm", "dimension","<",gen_0,stoi(n));
    1072      879883 :   for (i=1; i<=n; i++) { gel(y,i) = zero_zv(n); ucoeff(y, i,i) = 1; }
    1073      879903 :   return y;
    1074             : }
    1075             : 
    1076             : GEN
    1077          42 : scalar_Flm(long s, long n)
    1078             : {
    1079             :   long i;
    1080          42 :   GEN y = cgetg(n+1,t_MAT);
    1081          42 :   for (i=1; i<=n; i++) { gel(y,i) = zero_Flv(n); coeff(y, i,i) = s; }
    1082          42 :   return y;
    1083             : }
    1084             : 
    1085             : /********************************************************************/
    1086             : /**                                                                **/
    1087             : /**                           CONVERSIONS                          **/
    1088             : /**                                                                **/
    1089             : /********************************************************************/
    1090             : GEN
    1091    17050671 : ZV_to_Flv(GEN x, ulong p)
    1092    17050671 : { pari_APPLY_ulong(umodiu(gel(x,i), p)) }
    1093             : 
    1094             : GEN
    1095     3506984 : ZM_to_Flm(GEN x, ulong p)
    1096     3506984 : { pari_APPLY_same(ZV_to_Flv(gel(x,i), p)) }
    1097             : 
    1098             : GEN
    1099        1302 : ZMV_to_FlmV(GEN x, ulong m)
    1100        1302 : { pari_APPLY_type(t_VEC,ZM_to_Flm(gel(x,i), m)) }
    1101             : 
    1102             : /*                          TO INTMOD                        */
    1103             : static GEN
    1104     9295310 : to_intmod(GEN x, GEN p) { retmkintmod(modii(x, p), p); }
    1105             : static GEN
    1106       15610 : Fl_to_intmod(ulong x, GEN p) { retmkintmod(utoi(x), p); }
    1107             : 
    1108             : GEN
    1109      298806 : Fp_to_mod(GEN z, GEN p)
    1110             : {
    1111      298806 :   retmkintmod(modii(z, p), icopy(p));
    1112             : }
    1113             : 
    1114             : /* z in Z[X], return z * Mod(1,p), normalized*/
    1115             : GEN
    1116     1121349 : FpX_to_mod(GEN z, GEN p)
    1117             : {
    1118     1121349 :   long i,l = lg(z);
    1119     1121349 :   GEN x = cgetg(l,t_POL);
    1120     1121349 :   if (l >2) p = icopy(p);
    1121     1121349 :   for (i=2; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1122     1121349 :   x[1] = z[1]; return normalizepol_lg(x,l);
    1123             : }
    1124             : 
    1125             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1126             : GEN
    1127        4578 : FpV_to_mod(GEN z, GEN p)
    1128             : {
    1129        4578 :   long i,l = lg(z);
    1130        4578 :   GEN x = cgetg(l, t_VEC);
    1131        4578 :   if (l == 1) return x;
    1132        4578 :   p = icopy(p);
    1133        4578 :   for (i=1; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1134        4578 :   return x;
    1135             : }
    1136             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1137             : GEN
    1138          63 : FpC_to_mod(GEN z, GEN p)
    1139             : {
    1140          63 :   long i, l = lg(z);
    1141          63 :   GEN x = cgetg(l, t_COL);
    1142          63 :   if (l == 1) return x;
    1143          63 :   p = icopy(p);
    1144          63 :   for (i=1; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1145          63 :   return x;
    1146             : }
    1147             : /* z in Mat m,n(Z), return z * Mod(1,p), normalized*/
    1148             : GEN
    1149       56371 : FpM_to_mod(GEN z, GEN p)
    1150             : {
    1151       56371 :   long i, j, m, l = lg(z);
    1152       56371 :   GEN  x = cgetg(l,t_MAT), y, zi;
    1153       56371 :   if (l == 1) return x;
    1154       56350 :   m = lgcols(z);
    1155       56350 :   p = icopy(p);
    1156      181762 :   for (i=1; i<l; i++)
    1157             :   {
    1158      125412 :     gel(x,i) = cgetg(m,t_COL);
    1159      125412 :     y = gel(x,i); zi= gel(z,i);
    1160      125412 :     for (j=1; j<m; j++) gel(y,j) = to_intmod(gel(zi,j), p);
    1161             :   }
    1162       56350 :   return x;
    1163             : }
    1164             : GEN
    1165          28 : Flc_to_mod(GEN z, ulong pp)
    1166             : {
    1167          28 :   long i, l = lg(z);
    1168          28 :   GEN p, x = cgetg(l, t_COL);
    1169          28 :   if (l == 1) return x;
    1170          28 :   p = utoipos(pp);
    1171          28 :   for (i=1; i<l; i++) gel(x,i) = Fl_to_intmod(z[i], p);
    1172          28 :   return x;
    1173             : }
    1174             : GEN
    1175         161 : Flm_to_mod(GEN z, ulong pp)
    1176             : {
    1177         161 :   long i, j, m, l = lg(z);
    1178         161 :   GEN p, x = cgetg(l,t_MAT), y, zi;
    1179         161 :   if (l == 1) return x;
    1180         133 :   m = lgcols(z);
    1181         133 :   p = utoipos(pp);
    1182        1148 :   for (i=1; i<l; i++)
    1183             :   {
    1184        1015 :     gel(x,i) = cgetg(m,t_COL);
    1185        1015 :     y = gel(x,i); zi= gel(z,i);
    1186        1015 :     for (j=1; j<m; j++) gel(y,j) = Fl_to_intmod(zi[j], p);
    1187             :   }
    1188         133 :   return x;
    1189             : }
    1190             : 
    1191             : GEN
    1192        1820 : FpVV_to_mod(GEN z, GEN p)
    1193             : {
    1194        1820 :   long i, j, m, l = lg(z);
    1195        1820 :   GEN  x = cgetg(l,t_VEC), y, zi;
    1196        1820 :   if (l == 1) return x;
    1197        1743 :   m = lgcols(z);
    1198        1743 :   p = icopy(p);
    1199        3647 :   for (i=1; i<l; i++)
    1200             :   {
    1201        1904 :     gel(x,i) = cgetg(m,t_VEC);
    1202        1904 :     y = gel(x,i); zi= gel(z,i);
    1203        1904 :     for (j=1; j<m; j++) gel(y,j) = to_intmod(gel(zi,j), p);
    1204             :   }
    1205        1743 :   return x;
    1206             : }
    1207             : 
    1208             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1209             : GEN
    1210           7 : FpXQC_to_mod(GEN z, GEN T, GEN p)
    1211             : {
    1212           7 :   long i,l = lg(z);
    1213           7 :   GEN x = cgetg(l, t_COL);
    1214           7 :   if (l == 1) return x;
    1215           7 :   T = FpX_to_mod(T, p);
    1216          21 :   for (i=1; i<l; i++)
    1217          14 :     gel(x,i) = mkpolmod(FpX_to_mod(gel(z,i), p), T);
    1218           7 :   return x;
    1219             : }
    1220             : 
    1221             : static GEN
    1222       61208 : Fq_to_mod_raw(GEN x, GEN T, GEN p)
    1223             : {
    1224       61208 :   GEN z = typ(x)==t_INT ? Fp_to_mod(x, p): FpX_to_mod(x, p);
    1225       61208 :   return mkpolmod(z, T);
    1226             : }
    1227             : 
    1228             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1229             : static GEN
    1230        2660 : FqC_to_mod_raw(GEN x, GEN T, GEN p)
    1231        2660 : { pari_APPLY_type(t_COL, Fq_to_mod_raw(gel(x,i), T, p)) }
    1232             : 
    1233             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1234             : GEN
    1235         196 : FqM_to_mod(GEN z, GEN T, GEN p)
    1236             : {
    1237             :   GEN x;
    1238         196 :   long i,l = lg(z);
    1239         196 :   if (!T) return FpM_to_mod(z, p);
    1240         196 :   x = cgetg(l, t_MAT);
    1241         196 :   if (l == 1) return x;
    1242         168 :   T = FpX_to_mod(T, p);
    1243        2828 :   for (i=1; i<l; i++)
    1244        2660 :     gel(x,i) = FqC_to_mod_raw(gel(z, i), T, p);
    1245         168 :   return x;
    1246             : }
    1247             : 
    1248             : /********************************************************************/
    1249             : /*                                                                  */
    1250             : /*                     Blackbox linear algebra                      */
    1251             : /*                                                                  */
    1252             : /********************************************************************/
    1253             : 
    1254             : /* A sparse column (zCs) v is a t_COL with two components C and E which are
    1255             :  * t_VECSMALL of the same length, representing sum_i E[i]*e_{C[i]}, where
    1256             :  * (e_j) is the canonical basis.
    1257             :  * A sparse matrix (zMs) is a t_VEC of zCs */
    1258             : 
    1259             : /* FpCs and FpMs are identical but E[i] is interpreted as a _signed_ C long
    1260             :  * integer representing an element of Fp. This is important since p can be
    1261             :  * large and p+E[i] would not fit in a C long.  */
    1262             : 
    1263             : /* RgCs and RgMs are similar, except that the type of the component is
    1264             :  * unspecified. Functions handling RgCs/RgMs must be independent of the type
    1265             :  * of E. */
    1266             : 
    1267             : /* Most functions take an argument nbrow which is the number of lines of the
    1268             :  * column/matrix, which cannot be derived from the data. */
    1269             : 
    1270             : GEN
    1271           0 : zCs_to_ZC(GEN R, long nbrow)
    1272             : {
    1273           0 :   GEN C = gel(R,1), E = gel(R,2), c = zerocol(nbrow);
    1274           0 :   long j, l = lg(C);
    1275           0 :   for (j = 1; j < l; ++j) gel(c, C[j]) = stoi(E[j]);
    1276           0 :   return c;
    1277             : }
    1278             : 
    1279             : GEN
    1280           0 : zMs_to_ZM(GEN x, long nbrow)
    1281           0 : { pari_APPLY_same(zCs_to_ZC(gel(x, i), nbrow)) }
    1282             : 
    1283             : /* Solve equation f(X) = B (mod p) where B is a FpV, and f is an endomorphism.
    1284             :  * Return either a solution as a t_COL, or a kernel vector as a t_VEC. */
    1285             : GEN
    1286         119 : gen_FpM_Wiedemann(void *E, GEN (*f)(void*, GEN), GEN B, GEN p)
    1287             : {
    1288         119 :   pari_sp ltop = avma;
    1289         119 :   long col = 0, n = lg(B)-1, m = 2*n+1;
    1290         119 :   if (ZV_equal0(B)) return zerocol(n);
    1291         238 :   while (++col <= n)
    1292             :   {
    1293         119 :     pari_sp btop = avma, av;
    1294             :     long i, lQ;
    1295         119 :     GEN V, Q, M, W = B;
    1296         119 :     GEN b = cgetg(m+2, t_POL);
    1297         119 :     b[1] = evalsigne(1)|evalvarn(0);
    1298         119 :     gel(b, 2) = gel(W, col);
    1299       50565 :     for (i = 3; i<m+2; i++)
    1300       50446 :       gel(b, i) = cgeti(lgefint(p));
    1301         119 :     av = avma;
    1302       50565 :     for (i = 3; i<m+2; i++)
    1303             :     {
    1304       50446 :       W = f(E, W);
    1305       50446 :       affii(gel(W, col),gel(b, i));
    1306       50446 :       if (gc_needed(av,1))
    1307             :       {
    1308        2000 :         if (DEBUGMEM>1) pari_warn(warnmem,"Wiedemann: first loop, %ld",i);
    1309        2000 :         W = gerepileupto(av, W);
    1310             :       }
    1311             :     }
    1312         119 :     b = FpX_renormalize(b, m+2);
    1313         119 :     if (lgpol(b)==0) {avma = btop; continue; }
    1314         119 :     M = FpX_halfgcd(b, pol_xn(m, 0), p);
    1315         119 :     Q = FpX_neg(FpX_normalize(gcoeff(M, 2, 1),p),p);
    1316         119 :     W = B; lQ =lg(Q);
    1317         119 :     if (DEBUGLEVEL) err_printf("Wiedemann: deg. minpoly: %ld\n",lQ-3);
    1318         119 :     V = FpC_Fp_mul(W, gel(Q, lQ-2), p);
    1319         119 :     av = avma;
    1320       24810 :     for (i = lQ-3; i > 1; i--)
    1321             :     {
    1322       24691 :       W = f(E, W);
    1323       24691 :       V = ZC_lincomb(gen_1, gel(Q,i), V, W);
    1324       24691 :       if (gc_needed(av,1))
    1325             :       {
    1326        1582 :         if (DEBUGMEM>1) pari_warn(warnmem,"Wiedemann: second loop, %ld",i);
    1327        1582 :         gerepileall(av, 2, &V, &W);
    1328             :       }
    1329             :     }
    1330         119 :     V = FpC_red(V, p);
    1331         119 :     W = FpC_sub(f(E,V), B, p);
    1332         238 :     if (ZV_equal0(W)) return gerepilecopy(ltop, V);
    1333           0 :     av = avma;
    1334           0 :     for (i = 1; i <= n; ++i)
    1335             :     {
    1336           0 :       V = W;
    1337           0 :       W = f(E, V);
    1338           0 :       if (ZV_equal0(W))
    1339           0 :         return gerepilecopy(ltop, shallowtrans(V));
    1340           0 :       gerepileall(av, 2, &V, &W);
    1341             :     }
    1342           0 :     avma = btop;
    1343             :   }
    1344           0 :   return NULL;
    1345             : }
    1346             : 
    1347             : GEN
    1348           0 : zMs_ZC_mul(GEN M, GEN B)
    1349             : {
    1350             :   long i, j;
    1351           0 :   long n = lg(B)-1;
    1352           0 :   GEN V = zerocol(n);
    1353           0 :   for (i = 1; i <= n; ++i)
    1354           0 :     if (signe(gel(B, i)))
    1355             :     {
    1356           0 :       GEN R = gel(M, i), C = gel(R, 1), E = gel(R, 2);
    1357           0 :       long l = lg(C);
    1358           0 :       for (j = 1; j < l; ++j)
    1359             :       {
    1360           0 :         long k = C[j];
    1361           0 :         switch(E[j])
    1362             :         {
    1363             :         case 1:
    1364           0 :           gel(V, k) = gel(V,k)==gen_0 ? gel(B,i) : addii(gel(V, k), gel(B,i));
    1365           0 :           break;
    1366             :         case -1:
    1367           0 :           gel(V, k) = gel(V,k)==gen_0 ? negi(gel(B,i)) : subii(gel(V, k), gel(B,i));
    1368           0 :           break;
    1369             :         default:
    1370           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]));
    1371           0 :           break;
    1372             :         }
    1373             :       }
    1374             :     }
    1375           0 :   return V;
    1376             : }
    1377             : 
    1378             : GEN
    1379           0 : FpMs_FpC_mul(GEN M, GEN B, GEN p) { return FpC_red(zMs_ZC_mul(M, B), p); }
    1380             : 
    1381             : GEN
    1382       75375 : ZV_zMs_mul(GEN B, GEN M)
    1383             : {
    1384             :   long i, j;
    1385       75375 :   long m = lg(M)-1;
    1386       75375 :   GEN V = cgetg(m+1,t_VEC);
    1387    39261568 :   for (i = 1; i <= m; ++i)
    1388             :   {
    1389    39186193 :     GEN R = gel(M, i), C = gel(R, 1), E = gel(R, 2);
    1390    39186193 :     long l = lg(C);
    1391    39186193 :     GEN z = mulis(gel(B, C[1]), E[1]);
    1392   329587863 :     for (j = 2; j < l; ++j)
    1393             :     {
    1394   290401670 :       long k = C[j];
    1395   290401670 :       switch(E[j])
    1396             :       {
    1397             :       case 1:
    1398   196103761 :         z = addii(z, gel(B,k));
    1399   196103761 :         break;
    1400             :       case -1:
    1401    79103778 :         z = subii(z, gel(B,k));
    1402    79103778 :         break;
    1403             :       default:
    1404    15194131 :         z = addii(z, mulis(gel(B,k), E[j]));
    1405    15194131 :         break;
    1406             :       }
    1407             :     }
    1408    39186193 :     gel(V,i) = z;
    1409             :   }
    1410       75375 :   return V;
    1411             : }
    1412             : 
    1413             : GEN
    1414         119 : FpV_FpMs_mul(GEN B, GEN M, GEN p) { return FpV_red(ZV_zMs_mul(B, M), p); }
    1415             : 
    1416             : GEN
    1417      512653 : ZlM_gauss(GEN a, GEN b, ulong p, long e, GEN C)
    1418             : {
    1419      512653 :   pari_sp av = avma, av2;
    1420      512653 :   GEN xi, xb, pi = gen_1, P;
    1421             :   long i;
    1422      512653 :   if (!C) {
    1423           0 :     C = Flm_inv(ZM_to_Flm(a, p), p);
    1424           0 :     if (!C) pari_err_INV("ZlM_gauss", a);
    1425             :   }
    1426      512653 :   P = utoipos(p);
    1427      512653 :   av2 = avma;
    1428      512653 :   xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    1429      512653 :   xb = Flm_to_ZM(xi);
    1430     1147399 :   for (i = 2; i <= e; i++)
    1431             :   {
    1432      634746 :     pi = muliu(pi, p); /* = p^(i-1) */
    1433      634746 :     b = ZM_Z_divexact(ZM_sub(b, ZM_nm_mul(a, xi)), P);
    1434      634746 :     if (gc_needed(av,2))
    1435             :     {
    1436          35 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZlM_gauss. i=%ld/%ld",i,e);
    1437          35 :       gerepileall(av2,3, &pi,&b,&xb);
    1438             :     }
    1439      634746 :     xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    1440      634746 :     xb = ZM_add(xb, nm_Z_mul(xi, pi));
    1441             :   }
    1442      512653 :   return gerepileupto(av, xb);
    1443             : }
    1444             : 
    1445             : struct wrapper_modp_s {
    1446             :   GEN (*f)(void*, GEN);
    1447             :   void *E;
    1448             :   GEN p;
    1449             : };
    1450             : 
    1451             : /* compute f(x) mod p */
    1452             : static GEN
    1453       75256 : wrap_relcomb_modp(void *E, GEN x)
    1454             : {
    1455       75256 :   struct wrapper_modp_s *W = (struct wrapper_modp_s*)E;
    1456       75256 :   return FpC_red(W->f(W->E, x), W->p);
    1457             : }
    1458             : static GEN
    1459           0 : wrap_relcomb(void*E, GEN x) { return zMs_ZC_mul((GEN)E, x); }
    1460             : 
    1461             : static GEN
    1462       75256 : wrap_relker(void*E, GEN x) { return ZV_zMs_mul(x, (GEN)E); }
    1463             : 
    1464             : /* Solve f(X) = B (mod p^e); blackbox version of ZlM_gauss */
    1465             : GEN
    1466           0 : gen_ZpM_Dixon(void *E, GEN (*f)(void*, GEN), GEN B, GEN p, long e)
    1467             : {
    1468             :   struct wrapper_modp_s W;
    1469           0 :   pari_sp av = avma;
    1470           0 :   GEN xb, xi, pi = gen_1;
    1471             :   long i;
    1472           0 :   W.E = E;
    1473           0 :   W.f = f;
    1474           0 :   W.p = p;
    1475           0 :   xi = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, FpC_red(B, p), p); /* f^(-1) B */
    1476           0 :   if (!xi || e == 1 || typ(xi) == t_VEC) return xi;
    1477           0 :   xb = xi;
    1478           0 :   for (i = 2; i <= e; i++)
    1479             :   {
    1480           0 :     pi = mulii(pi, p); /* = p^(i-1) */
    1481           0 :     B = ZC_Z_divexact(ZC_sub(B, f(E, xi)), p);
    1482           0 :     if (gc_needed(av,2))
    1483             :     {
    1484           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_ZpM_Dixon. i=%ld",i);
    1485           0 :       gerepileall(av,3, &pi,&B,&xb);
    1486             :     }
    1487           0 :     xi = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, FpC_red(B, p), p);
    1488           0 :     if (!xi) return NULL;
    1489           0 :     if (typ(xi) == t_VEC) return gerepileupto(av, xi);
    1490           0 :     xb = ZC_add(xb, ZC_Z_mul(xi, pi));
    1491             :   }
    1492           0 :   return gerepileupto(av, xb);
    1493             : }
    1494             : 
    1495             : static GEN
    1496       25223 : vecprow(GEN A, GEN prow)
    1497             : {
    1498       25223 :   return mkvec2(vecsmallpermute(prow,gel(A,1)), gel(A,2));
    1499             : }
    1500             : 
    1501             : /* Solve the equation MX = A. Return either a solution as a t_COL,
    1502             :  * or the index of a column which is linearly dependent from the others as a
    1503             :  * t_VECSMALL with a single component. */
    1504             : GEN
    1505           0 : ZpMs_ZpCs_solve(GEN M, GEN A, long nbrow, GEN p, long e)
    1506             : {
    1507           0 :   pari_sp av = avma;
    1508             :   GEN pcol, prow;
    1509           0 :   long nbi=lg(M)-1, lR;
    1510             :   long i, n;
    1511             :   GEN Mp, Ap, Rp;
    1512             :   pari_timer ti;
    1513           0 :   if (DEBUGLEVEL) timer_start(&ti);
    1514           0 :   RgMs_structelim(M, nbrow, gel(A, 1), &pcol, &prow);
    1515           0 :   if (!pcol) { avma = av; return NULL; }
    1516           0 :   if (DEBUGLEVEL)
    1517           0 :     timer_printf(&ti,"structured elimination (%ld -> %ld)",nbi,lg(pcol)-1);
    1518           0 :   n = lg(pcol)-1;
    1519           0 :   Mp = cgetg(n+1, t_MAT);
    1520           0 :   for(i=1; i<=n; i++)
    1521           0 :     gel(Mp, i) = vecprow(gel(M,pcol[i]), prow);
    1522           0 :   Ap = zCs_to_ZC(vecprow(A, prow), n);
    1523           0 :   if (DEBUGLEVEL) timer_start(&ti);
    1524           0 :   Rp = gen_ZpM_Dixon((void*)Mp,wrap_relcomb, Ap, p, e);
    1525           0 :   if (DEBUGLEVEL) timer_printf(&ti,"linear algebra");
    1526           0 :   if (!Rp) { avma = av; return NULL; }
    1527           0 :   lR = lg(Rp)-1;
    1528           0 :   if (typ(Rp) == t_COL)
    1529             :   {
    1530           0 :     GEN R = zerocol(nbi+1);
    1531           0 :     for (i=1; i<=lR; i++)
    1532           0 :        gel(R,pcol[i]) = gel(Rp,i);
    1533           0 :     return gerepilecopy(av, R);
    1534             :   }
    1535           0 :   for (i = 1; i <= lR; ++i)
    1536           0 :     if (signe(gel(Rp, i)))
    1537           0 :       return gerepileuptoleaf(av, mkvecsmall(pcol[i]));
    1538             :   return NULL; /* LCOV_EXCL_LINE */
    1539             : }
    1540             : 
    1541             : GEN
    1542           0 : FpMs_FpCs_solve(GEN M, GEN A, long nbrow, GEN p)
    1543             : {
    1544           0 :   return ZpMs_ZpCs_solve(M, A, nbrow, p, 1);
    1545             : }
    1546             : 
    1547             : GEN
    1548           0 : FpMs_FpCs_solve_safe(GEN M, GEN A, long nbrow, GEN p)
    1549             : {
    1550             :   GEN res;
    1551           0 :   pari_CATCH(e_INV)
    1552             :   {
    1553           0 :     GEN E = pari_err_last();
    1554           0 :     GEN x = err_get_compo(E,2);
    1555           0 :     if (typ(x) != t_INTMOD) pari_err(0,E);
    1556           0 :     if (DEBUGLEVEL)
    1557           0 :       pari_warn(warner,"FpMs_FpCs_solve_safe, impossible inverse %Ps", x);
    1558           0 :     res = NULL;
    1559             :   } pari_TRY {
    1560           0 :     res = ZpMs_ZpCs_solve(M, A, nbrow, p, 1);
    1561           0 :   } pari_ENDCATCH
    1562           0 :   return res;
    1563             : }
    1564             : 
    1565             : static GEN
    1566         119 : FpMs_structelim_back(GEN M, GEN V, GEN prow, GEN p)
    1567             : {
    1568         119 :   long i, j, oldf = 0, f = 0;
    1569         119 :   long lrow = lg(prow), lM = lg(M);
    1570         119 :   GEN W = const_vecsmall(lM-1,1);
    1571         119 :   GEN R = cgetg(lrow, t_VEC);
    1572      221956 :   for (i=1; i<lrow; i++)
    1573      221837 :     gel(R,i) = prow[i]==0 ? NULL: gel(V,prow[i]);
    1574             :   do
    1575             :   {
    1576         386 :     oldf = f;
    1577      437057 :     for(i=1; i<lM; i++)
    1578      436671 :       if (W[i])
    1579             :       {
    1580      148580 :         GEN C = gel(M,i), F = gel(C,1), E = gel(C,2);
    1581      148580 :         long c=0, cj=0, lF = lg(F);
    1582     1301459 :         for(j=1; j<lF; j++)
    1583     1152879 :           if (!gel(R,F[j]))
    1584       86602 :           { c++; cj=j; }
    1585      148580 :         if (c>=2) continue;
    1586      127901 :         if (c==1)
    1587             :         {
    1588       40913 :           pari_sp av = avma;
    1589       40913 :           GEN s = gen_0;
    1590      356337 :           for(j=1; j<lF; j++)
    1591      315424 :             if (j!=cj) s = Fp_add(s, mulis(gel(R,F[j]), E[j]), p);
    1592       40913 :           gel(R,F[cj]) = gerepileupto(av, Fp_div(Fp_neg(s, p), stoi(E[cj]), p));
    1593       40913 :           f++;
    1594             :         }
    1595      127901 :         W[i]=0;
    1596             :       }
    1597         386 :   } while(oldf!=f);
    1598      221956 :   for (i=1; i<lrow; i++)
    1599      221837 :     if (!gel(R,i)) gel(R,i) = gen_0;
    1600         119 :   return R;
    1601             : }
    1602             : 
    1603             : /* Return a linear form Y such that YM is essentially 0 */
    1604             : GEN
    1605         119 : FpMs_leftkernel_elt_col(GEN M, long nbcol, long nbrow, GEN p)
    1606             : {
    1607         119 :   pari_sp av = avma, av2;
    1608             :   GEN pcol, prow;
    1609             :   long i, n;
    1610             :   GEN Mp, B, MB, R, Rp;
    1611             :   pari_timer ti;
    1612             :   struct wrapper_modp_s W;
    1613         119 :   if (DEBUGLEVEL) timer_start(&ti);
    1614         119 :   RgMs_structelim_col(M, nbcol, nbrow, cgetg(1,t_VECSMALL), &pcol, &prow);
    1615         119 :   if (!pcol) { avma = av; return NULL; }
    1616         119 :   if (DEBUGLEVEL)
    1617           0 :     timer_printf(&ti,"structured elimination (%ld -> %ld)",nbcol,lg(pcol)-1);
    1618         119 :   n = lg(pcol)-1;
    1619         119 :   Mp = cgetg(n+1, t_MAT);
    1620       25342 :   for(i=1; i<=n; i++)
    1621       25223 :     gel(Mp, i) = vecprow(gel(M,pcol[i]), prow);
    1622         119 :   W.E = (void*) Mp;
    1623         119 :   W.f = wrap_relker;
    1624         119 :   W.p = p;
    1625         119 :   av2 = avma;
    1626             :   for(;;)
    1627             :   {
    1628         119 :     avma = av2;
    1629         119 :     B = cgetg(n+1,t_VEC);
    1630       25342 :     for(i=1; i<=n; i++)
    1631       25223 :       gel(B,i) = randomi(p);
    1632         119 :     MB = FpV_FpMs_mul(B, Mp, p);
    1633         119 :     if (DEBUGLEVEL) timer_start(&ti);
    1634         119 :     pari_CATCH(e_INV)
    1635             :     {
    1636           0 :       GEN E = pari_err_last();
    1637           0 :       GEN x = err_get_compo(E,2);
    1638           0 :       if (typ(x) != t_INTMOD) pari_err(0,E);
    1639           0 :       if (DEBUGLEVEL)
    1640           0 :         pari_warn(warner,"FpMs_leftkernel_elt, impossible inverse %Ps", x);
    1641           0 :       Rp = NULL;
    1642             :     } pari_TRY {
    1643         119 :       Rp = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, MB, p);
    1644         119 :     } pari_ENDCATCH
    1645         119 :     if (!Rp) continue;
    1646         119 :     if (typ(Rp)==t_COL)
    1647             :     {
    1648         119 :       Rp = FpC_sub(Rp,B,p);
    1649         119 :       if (ZV_equal0(Rp)) continue;
    1650             :     }
    1651         119 :     R = FpMs_structelim_back(M, Rp, prow, p);
    1652         119 :     if (DEBUGLEVEL) timer_printf(&ti,"Wiedemann left kernel");
    1653         119 :     return gerepilecopy(av, R);
    1654           0 :   }
    1655             : }
    1656             : 
    1657             : GEN
    1658          35 : FpMs_leftkernel_elt(GEN M, long nbrow, GEN p)
    1659             : {
    1660          35 :   return FpMs_leftkernel_elt_col(M, lg(M)-1, nbrow, p);
    1661             : }

Generated by: LCOV version 1.11