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.14.0 lcov report (development 27775-aca467eab2) Lines: 752 962 78.2 %
Date: 2022-07-03 07:33:15 Functions: 112 134 83.6 %
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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : #include "pari.h"
      16             : #include "paripriv.h"
      17             : 
      18             : #define DEBUGLEVEL DEBUGLEVEL_mat
      19             : 
      20             : /********************************************************************/
      21             : /**                                                                **/
      22             : /**                           REDUCTION                            **/
      23             : /**                                                                **/
      24             : /********************************************************************/
      25             : /* z in Z^n, return lift(Col(z) * Mod(1,p)) */
      26             : GEN
      27    37615414 : FpC_red(GEN x, GEN p)
      28   314582170 : { pari_APPLY_type(t_COL, modii(gel(x,i), p)) }
      29             : 
      30             : /* z in Z^n, return lift(Vec(z) * Mod(1,p)) */
      31             : GEN
      32         189 : FpV_red(GEN x, GEN p)
      33       25388 : { pari_APPLY_type(t_VEC, modii(gel(x,i), p)) }
      34             : 
      35             : GEN
      36     4024266 : FpC_center(GEN x, GEN p, GEN pov2)
      37    29304561 : { pari_APPLY_type(t_COL, Fp_center(gel(x,i), p, pov2)) }
      38             : 
      39             : GEN
      40      164675 : Flv_center(GEN x, ulong p, ulong ps2)
      41     2621598 : { pari_APPLY_ulong(Fl_center(uel(x,i),p,ps2)) }
      42             : 
      43             : /* z in Mat m,n(Z), return lift(z * Mod(1,p)) */
      44             : GEN
      45     8309307 : FpM_red(GEN x, GEN p)
      46    36269794 : { pari_APPLY_same(FpC_red(gel(x,i), p)) }
      47             : 
      48             : GEN
      49      840394 : FpM_center(GEN x, GEN p, GEN pov2)
      50     3770091 : { pari_APPLY_same(FpC_center(gel(x,i), p,pov2)) }
      51             : 
      52             : /* p != 3; assume entries in [0,p[ and ps2 = p>>1. */
      53             : static void
      54          77 : _FpC_center_inplace(GEN z, GEN p, GEN ps2)
      55             : {
      56          77 :   long i, l = lg(z);
      57         357 :   for (i = 1; i < l; i++)
      58             :   { /* HACK: assume p != 3, which ensures u = gen_[0-2] is never written to */
      59         280 :     GEN u = gel(z,i);
      60         280 :     if (abscmpii(u, ps2) > 0) subiiz(u, p, u);
      61             :   }
      62          77 : }
      63             : static void
      64           7 : _F3C_center_inplace(GEN z)
      65             : {
      66           7 :   long i, l = lg(z);
      67          35 :   for (i = 1; i < l; i++) /* z[i] = 0, 1 : no-op */
      68          28 :     if (equaliu(gel(z,i), 2)) gel(z,i) = gen_m1;
      69           7 : }
      70             : void
      71           0 : FpC_center_inplace(GEN z, GEN p, GEN ps2)
      72             : {
      73           0 :   if (equaliu(p, 3))
      74           0 :     _F3C_center_inplace(z);
      75             :   else
      76           0 :     _FpC_center_inplace(z, p, ps2);
      77           0 : }
      78             : 
      79             : void
      80          42 : FpM_center_inplace(GEN z, GEN p, GEN pov2)
      81             : {
      82          42 :   long i, l = lg(z);
      83          42 :   if (equaliu(p, 3))
      84          14 :     for (i = 1; i < l; i++) _F3C_center_inplace(gel(z,i));
      85             :   else
      86         112 :     for (i = 1; i < l; i++) _FpC_center_inplace(gel(z,i), p, pov2);
      87          42 : }
      88             : GEN
      89       11347 : Flm_center(GEN x, ulong p, ulong ps2)
      90      175875 : { pari_APPLY_same(Flv_center(gel(x,i), p,ps2)) }
      91             : 
      92             : GEN
      93         119 : random_FpV(long d, GEN p)
      94             : {
      95             :   long i;
      96         119 :   GEN y = cgetg(d+1,t_VEC);
      97       25045 :   for (i=1; i<=d; i++) gel(y,i) = randomi(p);
      98         119 :   return y;
      99             : }
     100             : 
     101             : GEN
     102         413 : random_FpC(long d, GEN p)
     103             : {
     104             :   long i;
     105         413 :   GEN y = cgetg(d+1,t_COL);
     106        3409 :   for (i=1; i<=d; i++) gel(y,i) = randomi(p);
     107         413 :   return y;
     108             : }
     109             : 
     110             : GEN
     111       41810 : random_Flv(long n, ulong p)
     112             : {
     113       41810 :   GEN y = cgetg(n+1, t_VECSMALL);
     114             :   long i;
     115      263968 :   for (i=1; i<=n; i++) uel(y,i) = random_Fl(p);
     116       41811 :   return y;
     117             : }
     118             : 
     119             : /********************************************************************/
     120             : /**                                                                **/
     121             : /**                           ADD, SUB                             **/
     122             : /**                                                                **/
     123             : /********************************************************************/
     124             : GEN
     125      185912 : FpC_add(GEN x, GEN y, GEN p)
     126     2671675 : { pari_APPLY_type(t_COL, Fp_add(gel(x,i), gel(y,i), p)) }
     127             : 
     128             : GEN
     129           0 : FpV_add(GEN x, GEN y, GEN p)
     130           0 : { pari_APPLY_type(t_VEC, Fp_add(gel(x,i), gel(y,i), p)) }
     131             : 
     132             : GEN
     133           0 : FpM_add(GEN x, GEN y, GEN p)
     134           0 : { pari_APPLY_same(FpC_add(gel(x,i), gel(y,i), p)) }
     135             : 
     136             : GEN
     137      755505 : Flv_add(GEN x, GEN y, ulong p)
     138             : {
     139      755505 :   if (p==2)
     140     1364526 :     pari_APPLY_ulong(x[i]^y[i])
     141             :   else
     142     7657545 :     pari_APPLY_ulong(Fl_add(x[i], y[i], p))
     143             : }
     144             : 
     145             : void
     146      398573 : Flv_add_inplace(GEN x, GEN y, ulong p)
     147             : {
     148      398573 :   long i, l = lg(x);
     149      398573 :   if (p==2)
     150     1218265 :     for (i = 1; i < l; i++) x[i] ^= y[i];
     151             :   else
     152      164878 :     for (i = 1; i < l; i++) x[i] = Fl_add(x[i], y[i], p);
     153      398573 : }
     154             : 
     155             : ulong
     156        3934 : Flv_sum(GEN x, ulong p)
     157             : {
     158        3934 :   long i, l = lg(x);
     159        3934 :   ulong s = 0;
     160        3934 :   if (p==2)
     161       17752 :     for (i = 1; i < l; i++) s ^= x[i];
     162             :   else
     163           0 :     for (i = 1; i < l; i++) s = Fl_add(s, x[i], p);
     164        3934 :   return s;
     165             : }
     166             : 
     167             : GEN
     168     3746506 : FpC_sub(GEN x, GEN y, GEN p)
     169    26283719 : { pari_APPLY_type(t_COL, Fp_sub(gel(x,i), gel(y,i), p)) }
     170             : 
     171             : GEN
     172           0 : FpV_sub(GEN x, GEN y, GEN p)
     173           0 : { pari_APPLY_type(t_VEC, Fp_sub(gel(x,i), gel(y,i), p)) }
     174             : 
     175             : GEN
     176      975073 : FpM_sub(GEN x, GEN y, GEN p)
     177     4235326 : { pari_APPLY_same(FpC_sub(gel(x,i), gel(y,i), p)) }
     178             : 
     179             : GEN
     180   219819455 : Flv_sub(GEN x, GEN y, ulong p)
     181   986154696 : { pari_APPLY_ulong(Fl_sub(uel(x,i), uel(y,i), p)) }
     182             : 
     183             : void
     184           0 : Flv_sub_inplace(GEN x, GEN y, ulong p)
     185             : {
     186           0 :   long i, l = lg(x);
     187           0 :   for (i = 1; i < l; i++) x[i] = Fl_sub(x[i], y[i], p);
     188           0 : }
     189             : 
     190             : GEN
     191       49743 : Flm_Fl_add(GEN x, ulong y, ulong p)
     192             : {
     193       49743 :   long l = lg(x), i, j;
     194       49743 :   GEN z = cgetg(l,t_MAT);
     195             : 
     196       49743 :   if (l==1) return z;
     197       49743 :   if (l != lgcols(x)) pari_err_OP( "+", x, utoi(y));
     198      235832 :   for (i=1; i<l; i++)
     199             :   {
     200      186089 :     GEN zi = cgetg(l,t_VECSMALL), xi = gel(x,i);
     201      186086 :     gel(z,i) = zi;
     202     1302963 :     for (j=1; j<l; j++) zi[j] = xi[j];
     203      186086 :     zi[i] = Fl_add(zi[i], y, p);
     204             :   }
     205       49743 :   return z;
     206             : }
     207             : GEN
     208        3689 : Flm_Fl_sub(GEN x, ulong y, ulong p) { return Flm_Fl_add(x, Fl_neg(y,p), p); }
     209             : 
     210             : GEN
     211        8889 : Flm_add(GEN x, GEN y, ulong p)
     212      148713 : { pari_APPLY_same(Flv_add(gel(x,i), gel(y,i), p)) }
     213             : 
     214             : GEN
     215    23496055 : Flm_sub(GEN x, GEN y, ulong p)
     216   243301157 : { pari_APPLY_same(Flv_sub(gel(x,i), gel(y,i), p)) }
     217             : 
     218             : /********************************************************************/
     219             : /**                                                                **/
     220             : /**                           MULTIPLICATION                       **/
     221             : /**                                                                **/
     222             : /********************************************************************/
     223             : GEN
     224      994235 : FpC_Fp_mul(GEN x, GEN y, GEN p)
     225    11303850 : { pari_APPLY_type(t_COL, Fp_mul(gel(x,i), y, p)) }
     226             : 
     227             : GEN
     228      859112 : Flv_Fl_mul(GEN x, ulong y, ulong p)
     229    11106475 : { pari_APPLY_ulong(Fl_mul(x[i], y, p)) }
     230             : 
     231             : GEN
     232         905 : Flv_Fl_div(GEN x, ulong y, ulong p)
     233             : {
     234         905 :   return Flv_Fl_mul(x, Fl_inv(y, p), p);
     235             : }
     236             : void
     237           0 : Flv_Fl_div_inplace(GEN x, ulong y, ulong p)
     238             : {
     239           0 :   Flv_Fl_mul_inplace(x, Fl_inv(y, p), p);
     240           0 : }
     241             : GEN
     242      579593 : FpM_Fp_mul(GEN X, GEN c, GEN p) {
     243      579593 :   long i, j, h, l = lg(X);
     244      579593 :   GEN A = cgetg(l, t_MAT);
     245      579597 :   if (l == 1) return A;
     246      579597 :   h = lgcols(X);
     247     3103045 :   for (j=1; j<l; j++)
     248             :   {
     249     2523690 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     250    19437451 :     for (i = 1; i < h; i++) gel(a,i) = Fp_mul(gel(x,i), c, p);
     251     2523448 :     gel(A,j) = a;
     252             :   }
     253      579355 :   return A;
     254             : }
     255             : 
     256             : /* x *= y */
     257             : void
     258     6980278 : Flv_Fl_mul_part_inplace(GEN x, ulong y, ulong p, long l)
     259             : {
     260             :   long i;
     261     6980278 :   if (HIGHWORD(y | p))
     262     7888049 :     for(i=1; i<=l; i++) x[i] = Fl_mul(x[i], y, p);
     263             :   else
     264    31679963 :     for(i=1; i<=l; i++) x[i] = (x[i] * y) % p;
     265     6980275 : }
     266             : void
     267           0 : Flv_Fl_mul_inplace(GEN x, ulong y, ulong p)
     268           0 : { Flv_Fl_mul_part_inplace(x, y, p, lg(x)-1); }
     269             : 
     270             : /* set y *= x */
     271             : void
     272           0 : Flm_Fl_mul_inplace(GEN y, ulong x, ulong p)
     273             : {
     274           0 :   long i, j, m, l = lg(y);
     275           0 :   if (l == 1) return;
     276           0 :   m = lgcols(y);
     277           0 :   if (HIGHWORD(x | p))
     278           0 :     for(j=1; j<l; j++)
     279           0 :       for(i=1; i<m; i++) ucoeff(y,i,j) = Fl_mul(ucoeff(y,i,j), x, p);
     280             :   else
     281           0 :     for(j=1; j<l; j++)
     282           0 :       for(i=1; i<m; i++) ucoeff(y,i,j) = (ucoeff(y,i,j) * x) % p;
     283             : }
     284             : 
     285             : /* return x * y */
     286             : static GEN
     287    12195735 : Flm_Fl_mul_pre_i(GEN y, ulong x, ulong p, ulong pi)
     288             : {
     289    12195735 :   long i, j, m, l = lg(y);
     290    12195735 :   GEN z = cgetg(l, t_MAT);
     291    12193833 :   if (l == 1) return z;
     292    12193833 :   m = lgcols(y);
     293   141572047 :   for(j=1; j<l; j++) {
     294   129310858 :     GEN c = cgetg(m, t_VECSMALL); gel(z,j) = c;
     295   339498069 :     for(i=1; i<m; i++) uel(c,i) = Fl_mul_pre(ucoeff(y,i,j), x, p, pi);
     296             :   }
     297    12261189 :   return z;
     298             : }
     299             : 
     300             : /* return x * y */
     301             : static GEN
     302     8797322 : Flm_Fl_mul_OK(GEN y, ulong x, ulong p)
     303             : {
     304     8797322 :   long i, j, m, l = lg(y);
     305     8797322 :   GEN z = cgetg(l, t_MAT);
     306     8797324 :   if (l == 1) return z;
     307     8797324 :   m = lgcols(y);
     308    51030447 :   for(j=1; j<l; j++) {
     309    42233135 :     GEN c = cgetg(m, t_VECSMALL); gel(z,j) = c;
     310   124605204 :     for(i=1; i<m; i++) uel(c,i) = (ucoeff(y,i,j) * x) % p;
     311             :   }
     312     8797312 :   return z;
     313             : }
     314             : 
     315             : /* return x * y */
     316             : GEN
     317    20958323 : Flm_Fl_mul_pre(GEN y, ulong x, ulong p, ulong pi)
     318             : {
     319    20958323 :   if (HIGHWORD(p))
     320    12195414 :     return Flm_Fl_mul_pre_i(y, x, p, pi);
     321             :   else
     322     8762909 :     return Flm_Fl_mul_OK(y, x, p);
     323             : }
     324             : 
     325             : /* return x * y */
     326             : GEN
     327       34418 : Flm_Fl_mul(GEN y, ulong x, ulong p)
     328             : {
     329       34418 :   if (HIGHWORD(p))
     330          74 :     return Flm_Fl_mul_pre_i(y, x, p, get_Fl_red(p));
     331             :   else
     332       34344 :     return Flm_Fl_mul_OK(y, x, p);
     333             : }
     334             : 
     335             : GEN
     336     5312056 : Flv_neg(GEN x, ulong p)
     337    36819461 : { pari_APPLY_ulong(Fl_neg(uel(x,i), p)) }
     338             : 
     339             : void
     340        6171 : Flv_neg_inplace(GEN v, ulong p)
     341             : {
     342             :   long i;
     343      176370 :   for (i = 1; i < lg(v); ++i)
     344      170199 :     v[i] = Fl_neg(v[i], p);
     345        6171 : }
     346             : 
     347             : GEN
     348      421376 : Flm_neg(GEN x, ulong p)
     349     5686361 : { pari_APPLY_same(Flv_neg(gel(x,i), p)) }
     350             : 
     351             : /* x[i,]*y. Assume lx > 1 and 0 < i < lgcols(x) */
     352             : static GEN
     353    17123066 : ZMrow_ZC_mul_i(GEN x, GEN y, long lx, long i)
     354             : {
     355    17123066 :   GEN c = mulii(gcoeff(x,i,1), gel(y,1));
     356             :   long k;
     357   215556510 :   for (k = 2; k < lx; k++)
     358             :   {
     359   198435726 :     GEN t = mulii(gcoeff(x,i,k), gel(y,k));
     360   198430269 :     if (signe(t)) c = addii(c, t);
     361             :   }
     362    17120784 :   return c;
     363             : }
     364             : 
     365             : static long
     366    97688819 : zmrow_zc_mul(GEN x, GEN y, long lx, long i)
     367             : {
     368             :   long k;
     369    97688819 :   long c = coeff(x,i,1) * y[1];
     370  1498282366 :   for (k = 2; k < lx; k++)
     371  1400593547 :     c += coeff(x,i,k) * y[k];
     372    97688819 :   return c;
     373             : }
     374             : 
     375             : GEN
     376     6450544 : zm_zc_mul(GEN x, GEN y)
     377             : {
     378     6450544 :   long lx = lg(x), l, i;
     379             :   GEN z;
     380     6450544 :   if (lx == 1) return cgetg(1, t_VECSMALL);
     381     6450544 :   l = lg(gel(x,1));
     382     6450544 :   z = cgetg(l,t_VECSMALL);
     383   104139363 :   for (i=1; i<l; i++) z[i] = zmrow_zc_mul(x, y, lx, i);
     384     6450544 :   return z;
     385             : }
     386             : 
     387             : GEN
     388        2114 : zm_mul(GEN x, GEN y)
     389             : {
     390        2114 :   long i,j,lx=lg(x), ly=lg(y);
     391             :   GEN z;
     392        2114 :   if (ly==1) return cgetg(1,t_MAT);
     393        2114 :   z = cgetg(ly,t_MAT);
     394        2114 :   if (lx==1)
     395             :   {
     396           0 :     for (i=1; i<ly; i++) gel(z,i) = cgetg(1,t_VECSMALL);
     397           0 :     return z;
     398             :   }
     399       30849 :   for (j=1; j<ly; j++)
     400       28735 :     gel(z,j) = zm_zc_mul(x, gel(y,j));
     401        2114 :   return z;
     402             : }
     403             : 
     404             : static ulong
     405   692427825 : Flmrow_Flc_mul_SMALL(GEN x, GEN y, ulong p, long lx, long i)
     406             : {
     407   692427825 :   ulong c = ucoeff(x,i,1) * uel(y,1);
     408             :   long k;
     409 10525958709 :   for (k = 2; k < lx; k++) {
     410  9833530884 :     c += ucoeff(x,i,k) * uel(y,k);
     411  9833530884 :     if (c & HIGHBIT) c %= p;
     412             :   }
     413   692427825 :   return c % p;
     414             : }
     415             : 
     416             : static ulong
     417   547777567 : Flmrow_Flc_mul_i(GEN x, GEN y, ulong p, ulong pi, long lx, long i)
     418             : {
     419             :   ulong l0, l1, v1, h0, h1;
     420   547777567 :   long k = 1;
     421             :   LOCAL_OVERFLOW;
     422             :   LOCAL_HIREMAINDER;
     423   547777567 :   l1 = mulll(ucoeff(x,i,k), uel(y,k)); h1 = hiremainder; v1 = 0;
     424 10122821827 :   while (++k < lx) {
     425  9575044260 :     l0 = mulll(ucoeff(x,i,k), uel(y,k)); h0 = hiremainder;
     426  9575044260 :     l1 = addll(l0, l1); h1 = addllx(h0, h1); v1 += overflow;
     427             :   }
     428   547777567 :   if (v1 == 0) return remll_pre(h1, l1, p, pi);
     429    63963291 :   else return remlll_pre(v1, h1, l1, p, pi);
     430             : }
     431             : 
     432             : static GEN
     433      173657 : Flm_Flc_mul_i_2(GEN x, GEN y, long lx, long l)
     434             : {
     435             :   long i,j;
     436      173657 :   GEN z = NULL;
     437             : 
     438     1577642 :   for (j=1; j<lx; j++)
     439             :   {
     440     1403985 :     if (!y[j]) continue;
     441      439629 :     if (!z) z = Flv_copy(gel(x,j));
     442    12449688 :     else for (i = 1; i < l; i++) z[i] ^= coeff(x,i,j);
     443             :   }
     444      173657 :   if (!z) z = zero_zv(l-1);
     445      173657 :   return z;
     446             : }
     447             : 
     448             : static GEN
     449     1591616 : FpM_FpC_mul_i(GEN x, GEN y, long lx, long l, GEN p)
     450             : {
     451     1591616 :   GEN z = cgetg(l,t_COL);
     452             :   long i;
     453    15693193 :   for (i = 1; i < l; i++)
     454             :   {
     455    14101606 :     pari_sp av = avma;
     456    14101606 :     GEN c = ZMrow_ZC_mul_i(x, y, lx, i);
     457    14101338 :     gel(z,i) = gerepileuptoint(av, modii(c,p));
     458             :   }
     459     1591587 :   return z;
     460             : }
     461             : 
     462             : static void
     463    80046002 : __Flm_Flc_mul_i_SMALL(GEN z, GEN x, GEN y, long lx, long l, ulong p)
     464             : {
     465             :   long i;
     466   772486984 :   for (i = 1; i < l; i++) z[i] = Flmrow_Flc_mul_SMALL(x, y, p, lx, i);
     467    80052201 : }
     468             : static GEN
     469    76922713 : Flm_Flc_mul_i_SMALL(GEN x, GEN y, long lx, long l, ulong p)
     470             : {
     471    76922713 :   GEN z = cgetg(l,t_VECSMALL);
     472    76921720 :   __Flm_Flc_mul_i_SMALL(z, x, y, lx, l, p);
     473    76923403 :   return z;
     474             : }
     475             : 
     476             : static void
     477    94426197 : __Flm_Flc_mul_i(GEN z, GEN x, GEN y, long lx, long l, ulong p, ulong pi)
     478             : {
     479             :   long i;
     480   644925330 :   for (i = 1; i < l; i++) z[i] = Flmrow_Flc_mul_i(x, y, p, pi, lx, i);
     481    94893830 : }
     482             : static GEN
     483    90310521 : Flm_Flc_mul_i(GEN x, GEN y, long lx, long l, ulong p, ulong pi)
     484             : {
     485    90310521 :   GEN z = cgetg(l,t_VECSMALL);
     486    90216765 :   __Flm_Flc_mul_i(z, x, y, lx, l, p, pi);
     487    90349994 :   return z;
     488             : }
     489             : 
     490             : GEN
     491     4338441 : FpM_mul(GEN x, GEN y, GEN p)
     492             : {
     493     4338441 :   long lx=lg(x), ly=lg(y);
     494             :   GEN z;
     495     4338441 :   pari_sp av = avma;
     496     4338441 :   if (ly==1) return cgetg(1,t_MAT);
     497     4338441 :   if (lx==1) return zeromat(0, ly-1);
     498     4338441 :   if (lgefint(p) == 3)
     499             :   {
     500     2322303 :     ulong pp = uel(p,2);
     501     2322303 :     if (pp == 2)
     502             :     {
     503      688935 :       x = ZM_to_F2m(x);
     504      688967 :       y = ZM_to_F2m(y);
     505      688974 :       z = F2m_to_ZM(F2m_mul(x,y));
     506             :     }
     507             :     else
     508             :     {
     509     1633368 :       x = ZM_to_Flm(x, pp);
     510     1633466 :       y = ZM_to_Flm(y, pp);
     511     1633468 :       z = Flm_to_ZM(Flm_mul(x,y, pp));
     512             :     }
     513             :   } else
     514     2016138 :     z = FpM_red(ZM_mul(x, y), p);
     515     4338765 :   return gerepileupto(av, z);
     516             : }
     517             : 
     518             : static GEN
     519    27431636 : Flm_mul_classical(GEN x, GEN y, long l, long lx, long ly, ulong p, ulong pi)
     520             : {
     521             :   long j;
     522    27431636 :   GEN z = cgetg(ly, t_MAT);
     523    27430063 :   if (SMALL_ULONG(p))
     524    96988078 :     for (j=1; j<ly; j++)
     525    76639360 :       gel(z,j) = Flm_Flc_mul_i_SMALL(x, gel(y,j), lx, l, p);
     526             :   else
     527    97416892 :     for (j=1; j<ly; j++)
     528    90335671 :       gel(z,j) = Flm_Flc_mul_i(x, gel(y,j), lx, l, p, pi);
     529    27429939 :   return z;
     530             : }
     531             : 
     532             : /*
     533             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     534             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     535             : */
     536             : static void
     537       65016 : add_slices_ip(long m, long n,
     538             :            GEN A, long ma, long da, long na, long ea,
     539             :            GEN B, long mb, long db, long nb, long eb,
     540             :            GEN M, long dy, long dx, ulong p)
     541             : {
     542       65016 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     543             :   GEN C;
     544             : 
     545     2678706 :   for (j = 1; j <= min_e; j++) {
     546     2613690 :     C = gel(M, j + dx) + dy;
     547   116575408 :     for (i = 1; i <= min_d; i++)
     548   113961718 :       uel(C, i) = Fl_add(ucoeff(A, ma + i, na + j),
     549   113961435 :                         ucoeff(B, mb + i, nb + j), p);
     550     2675300 :     for (; i <= da; i++)
     551       61327 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     552     2613973 :     for (; i <= db; i++)
     553           0 :       uel(C, i) = ucoeff(B, mb + i, nb + j);
     554     2613973 :     for (; i <= m; i++)
     555           0 :       uel(C, i) = 0;
     556             :   }
     557       68175 :   for (; j <= ea; j++) {
     558        3159 :     C = gel(M, j + dx) + dy;
     559      158717 :     for (i = 1; i <= da; i++)
     560      155558 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     561        3159 :     for (; i <= m; i++)
     562           0 :       uel(C, i) = 0;
     563             :   }
     564       65016 :   for (; j <= eb; j++) {
     565           0 :     C = gel(M, j + dx) + dy;
     566           0 :     for (i = 1; i <= db; i++)
     567           0 :       uel(C, i) = ucoeff(B, mb + i, nb + j);
     568           0 :     for (; i <= m; i++)
     569           0 :       uel(C, i) = 0;
     570             :   }
     571       65016 :   for (; j <= n; j++) {
     572           0 :     C = gel(M, j + dx) + dy;
     573           0 :     for (i = 1; i <= m; i++)
     574           0 :       uel(C, i) = 0;
     575             :   }
     576       65016 : }
     577             : 
     578             : static GEN
     579       32507 : add_slices(long m, long n,
     580             :            GEN A, long ma, long da, long na, long ea,
     581             :            GEN B, long mb, long db, long nb, long eb, ulong p)
     582             : {
     583             :   GEN M;
     584             :   long j;
     585       32507 :   M = cgetg(n + 1, t_MAT);
     586     1364171 :   for (j = 1; j <= n; j++)
     587     1331668 :     gel(M, j) = cgetg(m + 1, t_VECSMALL);
     588       32503 :   add_slices_ip(m, n, A, ma, da, na, ea, B, mb, db, nb, eb, M, 0, 0, p);
     589       32508 :   return M;
     590             : }
     591             : 
     592             : /*
     593             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     594             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     595             : */
     596             : static GEN
     597       56888 : subtract_slices(long m, long n,
     598             :                 GEN A, long ma, long da, long na, long ea,
     599             :                 GEN B, long mb, long db, long nb, long eb, ulong p)
     600             : {
     601       56888 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     602       56888 :   GEN M = cgetg(n + 1, t_MAT), C;
     603             : 
     604     2439708 :   for (j = 1; j <= min_e; j++) {
     605     2382819 :     gel(M, j) = C = cgetg(m + 1, t_VECSMALL);
     606   109804210 :     for (i = 1; i <= min_d; i++)
     607   107421410 :       uel(C, i) = Fl_sub(ucoeff(A, ma + i, na + j),
     608   107421846 :                         ucoeff(B, mb + i, nb + j), p);
     609     2469095 :     for (; i <= da; i++)
     610       86731 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     611     2453985 :     for (; i <= db; i++)
     612       71621 :       uel(C, i) = Fl_neg(ucoeff(B, mb + i, nb + j), p);
     613     2382364 :     for (; i <= m; i++)
     614           0 :       uel(C, i) = 0;
     615             :   }
     616       56889 :   for (; j <= ea; j++) {
     617           0 :     gel(M, j) = C = cgetg(m + 1, t_VECSMALL);
     618           0 :     for (i = 1; i <= da; i++)
     619           0 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     620           0 :     for (; i <= m; i++)
     621           0 :       uel(C, i) = 0;
     622             :   }
     623       58057 :   for (; j <= eb; j++) {
     624        1168 :     gel(M, j) = C = cgetg(m + 1, t_VECSMALL);
     625       66642 :     for (i = 1; i <= db; i++)
     626       65474 :       uel(C, i) = Fl_neg(ucoeff(B, mb + i, nb + j), p);
     627        1168 :     for (; i <= m; i++)
     628           0 :       uel(C, i) = 0;
     629             :   }
     630       58057 :   for (; j <= n; j++)
     631        1168 :     gel(M, j) = zero_Flv(m);
     632       56889 :   return M;
     633             : }
     634             : 
     635             : static GEN Flm_mul_i(GEN x, GEN y, long l, long lx, long ly, ulong p, ulong pi);
     636             : 
     637             : /* Strassen-Winograd matrix product A (m x n) * B (n x p) */
     638             : static GEN
     639        8127 : Flm_mul_sw(GEN A, GEN B, long m, long n, long p, ulong l, ulong li)
     640             : {
     641             :   pari_sp av;
     642        8127 :   long m1 = (m + 1)/2, m2 = m/2,
     643        8127 :     n1 = (n + 1)/2, n2 = n/2,
     644        8127 :     p1 = (p + 1)/2, p2 = p/2;
     645             :   GEN A11, A12, A22, B11, B21, B22,
     646             :     S1, S2, S3, S4, T1, T2, T3, T4,
     647             :     M1, M2, M3, M4, M5, M6, M7,
     648             :     V1, V2, V3, C;
     649             :   long j;
     650        8127 :   C = cgetg(p + 1, t_MAT);
     651      650939 :   for (j = 1; j <= p; j++)
     652      642811 :     gel(C, j) = cgetg(m + 1, t_VECSMALL);
     653        8128 :   av = avma;
     654        8128 :   T2 = subtract_slices(n1, p2, B, 0, n1, p1, p2, B, n1, n2, p1, p2, l);
     655        8127 :   S1 = subtract_slices(m2, n1, A, m1, m2, 0, n1, A, 0, m2, 0, n1, l);
     656        8127 :   M2 = Flm_mul_i(S1, T2, m2 + 1, n1 + 1, p2 + 1, l, li);
     657        8127 :   if (gc_needed(av, 1))
     658           0 :     gerepileall(av, 2, &T2, &M2);  /* destroy S1 */
     659        8127 :   T3 = subtract_slices(n1, p1, T2, 0, n1, 0, p2, B, 0, n1, 0, p1, l);
     660        8127 :   if (gc_needed(av, 1))
     661           0 :     gerepileall(av, 2, &M2, &T3);  /* destroy T2 */
     662        8127 :   S2 = add_slices(m2, n1, A, m1, m2, 0, n1, A, m1, m2, n1, n2, l);
     663        8127 :   T1 = subtract_slices(n1, p1, B, 0, n1, p1, p2, B, 0, n1, 0, p2, l);
     664        8127 :   M3 = Flm_mul_i(S2, T1, m2 + 1, n1 + 1, p2 + 1, l, li);
     665        8127 :   if (gc_needed(av, 1))
     666           0 :     gerepileall(av, 4, &M2, &T3, &S2, &M3);  /* destroy T1 */
     667        8127 :   S3 = subtract_slices(m1, n1, S2, 0, m2, 0, n1, A, 0, m1, 0, n1, l);
     668        8127 :   if (gc_needed(av, 1))
     669           0 :     gerepileall(av, 4, &M2, &T3, &M3, &S3);  /* destroy S2 */
     670        8127 :   A11 = matslice(A, 1, m1, 1, n1);
     671        8127 :   B11 = matslice(B, 1, n1, 1, p1);
     672        8127 :   M1 = Flm_mul_i(A11, B11, m1 + 1, n1 + 1, p1 + 1, l, li);
     673        8127 :   if (gc_needed(av, 1))
     674           0 :     gerepileall(av, 5, &M2, &T3, &M3, &S3, &M1);  /* destroy A11, B11 */
     675        8127 :   A12 = matslice(A, 1, m1, n1 + 1, n);
     676        8127 :   B21 = matslice(B, n1 + 1, n, 1, p1);
     677        8127 :   M4 = Flm_mul_i(A12, B21, m1 + 1, n2 + 1, p1 + 1, l, li);
     678        8127 :   if (gc_needed(av, 1))
     679           0 :     gerepileall(av, 6, &M2, &T3, &M3, &S3, &M1, &M4);  /* destroy A12, B21 */
     680        8127 :   add_slices_ip(m1, p1, M1, 0, m1, 0, p1, M4, 0, m1, 0, p1, C, 0, 0, l);
     681        8127 :   if (gc_needed(av, 1))
     682           0 :     gerepileall(av, 5, &M2, &T3, &M3, &S3, &M1);  /* destroy M4 */
     683        8127 :   M5 = Flm_mul_i(S3, T3, m1 + 1, n1 + 1, p1 + 1, l, li);
     684        8127 :   S4 = subtract_slices(m1, n2, A, 0, m1, n1, n2, S3, 0, m1, 0, n2, l);
     685        8127 :   if (gc_needed(av, 1))
     686           0 :     gerepileall(av, 6, &M2, &T3, &M3, &M1, &M5, &S4);  /* destroy S3 */
     687        8127 :   T4 = add_slices(n2, p1, B, n1, n2, 0, p1, T3, 0, n2, 0, p1, l);
     688        8127 :   if (gc_needed(av, 1))
     689           0 :     gerepileall(av, 6, &M2, &M3, &M1, &M5, &S4, &T4);  /* destroy T3 */
     690        8127 :   V1 = subtract_slices(m1, p1, M1, 0, m1, 0, p1, M5, 0, m1, 0, p1, l);
     691        8127 :   if (gc_needed(av, 1))
     692           0 :     gerepileall(av, 5, &M2, &M3, &S4, &T4, &V1);  /* destroy M1, M5 */
     693        8127 :   B22 = matslice(B, n1 + 1, n, p1 + 1, p);
     694        8127 :   M6 = Flm_mul_i(S4, B22, m1 + 1, n2 + 1, p2 + 1, l, li);
     695        8127 :   if (gc_needed(av, 1))
     696           0 :     gerepileall(av, 5, &M2, &M3, &T4, &V1, &M6);  /* destroy S4, B22 */
     697        8127 :   A22 = matslice(A, m1 + 1, m, n1 + 1, n);
     698        8127 :   M7 = Flm_mul_i(A22, T4, m2 + 1, n2 + 1, p1 + 1, l, li);
     699        8127 :   if (gc_needed(av, 1))
     700           0 :     gerepileall(av, 5, &M2, &M3, &V1, &M6, &M7);  /* destroy A22, T4 */
     701        8127 :   V3 = add_slices(m1, p2, V1, 0, m1, 0, p2, M3, 0, m2, 0, p2, l);
     702        8127 :   add_slices_ip(m1, p2, V3, 0, m1, 0, p2, M6, 0, m1, 0, p2, C, 0, p1, l);
     703        8127 :   if (gc_needed(av, 1))
     704           0 :     gerepileall(av, 4, &M2, &M3, &V1, &M7);  /* destroy V3, M6 */
     705        8127 :   V2 = add_slices(m2, p1, V1, 0, m2, 0, p1, M2, 0, m2, 0, p2, l);
     706        8127 :   if (gc_needed(av, 1))
     707           0 :     gerepileall(av, 3, &M3, &M7, &V2);  /* destroy V1, M2 */
     708        8127 :   add_slices_ip(m2, p1, V2, 0, m2, 0, p1, M7, 0, m2, 0, p1, C, m1, 0, l);
     709        8127 :   add_slices_ip(m2, p2, V2, 0, m2, 0, p2, M3, 0, m2, 0, p2, C, m1, p1, l);
     710        8127 :   set_avma(av); return C;
     711             : }
     712             : 
     713             : /* Strassen-Winograd used for dim >= ZM_sw_bound */
     714             : static GEN
     715    27438827 : Flm_mul_i(GEN x, GEN y, long l, long lx, long ly, ulong p, ulong pi)
     716             : {
     717    27438827 :   ulong e = expu(p);
     718             : #ifdef LONG_IS_64BIT
     719    23193258 :   long ZM_sw_bound = e <= 29 ? 140: e <=62 ? 40: 70;
     720             : #else
     721     4245768 :   long ZM_sw_bound = e <= 12 ? 230: e <=14 ? 170 : e <=17 ? 110: 120;
     722             : #endif
     723    27439026 :   if (l <= ZM_sw_bound || lx <= ZM_sw_bound || ly <= ZM_sw_bound)
     724    27430899 :     return Flm_mul_classical(x, y, l, lx, ly, p, pi);
     725             :   else
     726        8127 :     return Flm_mul_sw(x, y, l - 1, lx - 1, ly - 1, p, pi);
     727             : }
     728             : 
     729             : GEN
     730    12521337 : Flm_mul_pre(GEN x, GEN y, ulong p, ulong pi)
     731             : {
     732    12521337 :   long lx=lg(x), ly=lg(y);
     733    12521337 :   if (ly==1) return cgetg(1,t_MAT);
     734    12521337 :   if (lx==1) return zero_Flm(0, ly-1);
     735    12521337 :   return Flm_mul_i(x, y, lgcols(x), lx, ly, p, pi);
     736             : }
     737             : 
     738             : GEN
     739    14857366 : Flm_mul(GEN x, GEN y, ulong p)
     740             : {
     741    14857366 :   long lx=lg(x), ly=lg(y);
     742    14857366 :   if (ly==1) return cgetg(1,t_MAT);
     743    14857079 :   if (lx==1) return zero_Flm(0, ly-1);
     744    14857079 :   return Flm_mul_i(x, y, lgcols(x), lx, ly, p, get_Fl_red(p));
     745             : }
     746             : 
     747             : struct _Flm
     748             : {
     749             :   ulong p;
     750             :   long n;
     751             : };
     752             : 
     753             : static GEN
     754        8512 : _Flm_mul(void *E , GEN x, GEN y)
     755        8512 : { return Flm_mul(x,y,((struct _Flm*)E)->p); }
     756             : static GEN
     757       42014 : _Flm_sqr(void *E, GEN x)
     758       42014 : { return Flm_mul(x,x,((struct _Flm*)E)->p); }
     759             : static GEN
     760         861 : _Flm_one(void *E)
     761         861 : { return matid_Flm(((struct _Flm*)E)->n); }
     762             : GEN
     763       23548 : Flm_powu(GEN x, ulong n, ulong p)
     764             : {
     765             :   struct _Flm d;
     766       23548 :   if (!n) return matid(lg(x)-1);
     767       23548 :   d.p = p;
     768       23548 :   return gen_powu(x, n, (void*)&d, &_Flm_sqr, &_Flm_mul);
     769             : }
     770             : GEN
     771         861 : Flm_powers(GEN x, ulong n, ulong p)
     772             : {
     773             :   struct _Flm d;
     774         861 :   d.p = p;
     775         861 :   d.n = lg(x)-1;
     776         861 :   return gen_powers(x, n, 1, (void*)&d,
     777             :                     &_Flm_sqr, &_Flm_mul, &_Flm_one);
     778             : }
     779             : 
     780             : static GEN
     781           0 : _FpM_mul(void *p , GEN x, GEN y)
     782           0 : { return FpM_mul(x,y,(GEN)p); }
     783             : static GEN
     784           0 : _FpM_sqr(void *p, GEN x)
     785           0 : { return FpM_mul(x,x,(GEN)p); }
     786             : GEN
     787           0 : FpM_powu(GEN x, ulong n, GEN p)
     788             : {
     789           0 :   if (!n) return matid(lg(x)-1);
     790           0 :   if (lgefint(p) == 3)
     791             :   {
     792           0 :     pari_sp av = avma;
     793           0 :     ulong pp = uel(p,2);
     794             :     GEN z;
     795           0 :     if (pp == 2)
     796           0 :       z = F2m_to_ZM(F2m_powu(ZM_to_F2m(x),n));
     797             :     else
     798           0 :       z = Flm_to_ZM(Flm_powu(ZM_to_Flm(x, pp), n, pp));
     799           0 :     return gerepileupto(av, z);
     800             :   }
     801           0 :   return gen_powu(x, n, (void*)p, &_FpM_sqr, &_FpM_mul);
     802             : }
     803             : 
     804             : /*Multiple a column vector by a line vector to make a matrix*/
     805             : GEN
     806          14 : Flc_Flv_mul(GEN x, GEN y, ulong p)
     807             : {
     808          14 :   long i,j, lx=lg(x), ly=lg(y);
     809             :   GEN z;
     810          14 :   if (ly==1) return cgetg(1,t_MAT);
     811          14 :   z = cgetg(ly, t_MAT);
     812          49 :   for (j=1; j < ly; j++)
     813             :   {
     814          35 :     GEN zj = cgetg(lx,t_VECSMALL);
     815         112 :     for (i=1; i<lx; i++)
     816          77 :       uel(zj,i) = Fl_mul(uel(x,i), uel(y,j), p);
     817          35 :     gel(z,j) = zj;
     818             :   }
     819          14 :   return z;
     820             : }
     821             : 
     822             : /*Multiple a column vector by a line vector to make a matrix*/
     823             : GEN
     824        5705 : FpC_FpV_mul(GEN x, GEN y, GEN p)
     825             : {
     826        5705 :   long i,j, lx=lg(x), ly=lg(y);
     827             :   GEN z;
     828        5705 :   if (ly==1) return cgetg(1,t_MAT);
     829        5705 :   z = cgetg(ly,t_MAT);
     830       64023 :   for (j=1; j < ly; j++)
     831             :   {
     832       58317 :     GEN zj = cgetg(lx,t_COL);
     833     1379662 :     for (i=1; i<lx; i++) gel(zj,i) = Fp_mul(gel(x,i),gel(y,j), p);
     834       58318 :     gel(z, j) = zj;
     835             :   }
     836        5706 :   return z;
     837             : }
     838             : 
     839             : /* Multiply a line vector by a column and return a scalar (t_INT) */
     840             : GEN
     841     4674154 : FpV_dotproduct(GEN x, GEN y, GEN p)
     842             : {
     843     4674154 :   long i, lx = lg(x);
     844             :   pari_sp av;
     845             :   GEN c;
     846     4674154 :   if (lx == 1) return gen_0;
     847     4673636 :   av = avma; c = mulii(gel(x,1),gel(y,1));
     848    16010073 :   for (i=2; i<lx; i++) c = addii(c, mulii(gel(x,i),gel(y,i)));
     849     4673542 :   return gerepileuptoint(av, modii(c,p));
     850             : }
     851             : GEN
     852           0 : FpV_dotsquare(GEN x, GEN p)
     853             : {
     854           0 :   long i, lx = lg(x);
     855             :   pari_sp av;
     856             :   GEN c;
     857           0 :   if (lx == 1) return gen_0;
     858           0 :   av = avma; c = sqri(gel(x,1));
     859           0 :   for (i=2; i<lx; i++) c = addii(c, sqri(gel(x,i)));
     860           0 :   return gerepileuptoint(av, modii(c,p));
     861             : }
     862             : 
     863             : INLINE ulong
     864     8046975 : Flv_dotproductspec_SMALL(GEN x, GEN y, ulong p, long lx)
     865             : {
     866     8046975 :   ulong c = uel(x,0) * uel(y,0);
     867             :   long k;
     868    62174389 :   for (k = 1; k < lx; k++) {
     869    54127414 :     c += uel(x,k) * uel(y,k);
     870    54127414 :     if (c & HIGHBIT) c %= p;
     871             :   }
     872     8046975 :   return c % p;
     873             : }
     874             : 
     875             : INLINE ulong
     876      740390 : Flv_dotproductspec_i(GEN x, GEN y, ulong p, ulong pi, long lx)
     877             : {
     878             :   ulong l0, l1, v1, h0, h1;
     879      740390 :   long i = 0;
     880             :   LOCAL_OVERFLOW;
     881             :   LOCAL_HIREMAINDER;
     882      740390 :   l1 = mulll(uel(x,i), uel(y,i)); h1 = hiremainder; v1 = 0;
     883    15414081 :   while (++i < lx) {
     884    14673691 :     l0 = mulll(uel(x,i), uel(y,i)); h0 = hiremainder;
     885    14673691 :     l1 = addll(l0, l1); h1 = addllx(h0, h1); v1 += overflow;
     886             :   }
     887      740390 :   if (v1 == 0) return remll_pre(h1, l1, p, pi);
     888      464441 :   else return remlll_pre(v1, h1, l1, p, pi);
     889             : }
     890             : 
     891             : ulong
     892      349972 : Flv_dotproduct(GEN x, GEN y, ulong p)
     893             : {
     894      349972 :   long lx = lg(x)-1;
     895      349972 :   if (lx == 0) return 0;
     896      349972 :   if (SMALL_ULONG(p))
     897      349972 :     return Flv_dotproductspec_SMALL(x+1, y+1, p, lx);
     898             :   else
     899           0 :     return Flv_dotproductspec_i(x+1, y+1, p, get_Fl_red(p), lx);
     900             : }
     901             : 
     902             : ulong
     903       59018 : Flv_dotproduct_pre(GEN x, GEN y, ulong p, ulong pi)
     904             : {
     905       59018 :   long lx = lg(x)-1;
     906       59018 :   if (lx == 0) return 0;
     907       59018 :   if (SMALL_ULONG(p))
     908       53436 :     return Flv_dotproductspec_SMALL(x+1, y+1, p, lx);
     909             :   else
     910        5582 :     return Flv_dotproductspec_i(x+1, y+1, p, pi, lx);
     911             : }
     912             : 
     913             : ulong
     914     8438464 : Flx_dotproduct(GEN x, GEN y, ulong p)
     915             : {
     916     8438464 :   long lx = minss(lgpol(x), lgpol(y));
     917     8438479 :   if (lx == 0) return 0;
     918     8378337 :   if (SMALL_ULONG(p))
     919     7643529 :     return Flv_dotproductspec_SMALL(x+2, y+2, p, lx);
     920             :   else
     921      734808 :     return Flv_dotproductspec_i(x+2, y+2, p, get_Fl_red(p), lx);
     922             : }
     923             : 
     924             : GEN
     925     1591622 : FpM_FpC_mul(GEN x, GEN y, GEN p)
     926             : {
     927     1591622 :   long lx = lg(x);
     928     1591622 :   return lx==1? cgetg(1,t_COL): FpM_FpC_mul_i(x, y, lx, lgcols(x), p);
     929             : }
     930             : GEN
     931      459843 : Flm_Flc_mul(GEN x, GEN y, ulong p)
     932             : {
     933      459843 :   long l, lx = lg(x);
     934      459843 :   if (lx==1) return cgetg(1,t_VECSMALL);
     935      459843 :   l = lgcols(x);
     936      459843 :   if (p==2)
     937      173657 :     return Flm_Flc_mul_i_2(x, y, lx, l);
     938      286186 :   else if (SMALL_ULONG(p))
     939      283466 :     return Flm_Flc_mul_i_SMALL(x, y, lx, l, p);
     940             :   else
     941        2720 :     return Flm_Flc_mul_i(x, y, lx, l, p, get_Fl_red(p));
     942             : }
     943             : 
     944             : GEN
     945        6693 : Flm_Flc_mul_pre(GEN x, GEN y, ulong p, ulong pi)
     946             : {
     947        6693 :   long l, lx = lg(x);
     948             :   GEN z;
     949        6693 :   if (lx==1) return cgetg(1,t_VECSMALL);
     950        6693 :   l = lgcols(x);
     951        6693 :   z = cgetg(l, t_VECSMALL);
     952        6693 :   if (SMALL_ULONG(p))
     953        4407 :     __Flm_Flc_mul_i_SMALL(z, x, y, lx, l, p);
     954             :   else
     955        2286 :     __Flm_Flc_mul_i(z, x, y, lx, l, p, pi);
     956        6693 :   return z;
     957             : }
     958             : 
     959             : GEN
     960     7321285 : Flm_Flc_mul_pre_Flx(GEN x, GEN y, ulong p, ulong pi, long sv)
     961             : {
     962     7321285 :   long l, lx = lg(x);
     963             :   GEN z;
     964     7321285 :   if (lx==1) return pol0_Flx(sv);
     965     7321285 :   l = lgcols(x);
     966     7318258 :   z = cgetg(l + 1, t_VECSMALL); z[1] = sv;
     967     7321475 :   if (SMALL_ULONG(p))
     968     3120029 :     __Flm_Flc_mul_i_SMALL(z + 1, x, y, lx, l, p);
     969             :   else
     970     4201446 :     __Flm_Flc_mul_i(z + 1, x, y, lx, l, p, pi);
     971     7335884 :   return Flx_renormalize(z, l + 1);
     972             : }
     973             : 
     974             : /* RgV_to_RgX(FpM_FpC_mul(x,y,p), v), p != NULL, memory clean */
     975             : GEN
     976      982853 : FpM_FpC_mul_FpX(GEN x, GEN y, GEN p, long v)
     977             : {
     978      982853 :   long i, l, lx = lg(x);
     979             :   GEN z;
     980      982853 :   if (lx==1) return pol_0(v);
     981      982853 :   l = lgcols(x);
     982      982853 :   z = new_chunk(l+1);
     983     1711574 :   for (i=l-1; i; i--)
     984             :   {
     985     1527985 :     pari_sp av = avma;
     986     1527985 :     GEN p1 = ZMrow_ZC_mul_i(x,y,lx,i);
     987     1527983 :     p1 = modii(p1, p);
     988     1527980 :     if (signe(p1))
     989             :     {
     990      799259 :       if (i != l-1) stackdummy((pari_sp)(z + l+1), (pari_sp)(z + i+2));
     991      799259 :       gel(z,i+1) = gerepileuptoint(av, p1);
     992      799264 :       break;
     993             :     }
     994      728721 :     set_avma(av);
     995             :   }
     996      982853 :   if (!i) { set_avma((pari_sp)(z + l+1)); return pol_0(v); }
     997      799264 :   z[0] = evaltyp(t_POL) | evallg(i+2);
     998      799264 :   z[1] = evalsigne(1) | evalvarn(v);
     999     2292739 :   for (; i; i--)
    1000             :   {
    1001     1493475 :     pari_sp av = avma;
    1002     1493475 :     GEN p1 = ZMrow_ZC_mul_i(x,y,lx,i);
    1003     1493472 :     gel(z,i+1) = gerepileuptoint(av, modii(p1,p));
    1004             :   }
    1005      799264 :   return z;
    1006             : }
    1007             : 
    1008             : /********************************************************************/
    1009             : /**                                                                **/
    1010             : /**                           TRANSPOSITION                        **/
    1011             : /**                                                                **/
    1012             : /********************************************************************/
    1013             : 
    1014             : /* == zm_transpose */
    1015             : GEN
    1016      944109 : Flm_transpose(GEN x)
    1017             : {
    1018      944109 :   long i, dx, lx = lg(x);
    1019             :   GEN y;
    1020      944109 :   if (lx == 1) return cgetg(1,t_MAT);
    1021      944109 :   dx = lgcols(x); y = cgetg(dx,t_MAT);
    1022    10773888 :   for (i=1; i<dx; i++) gel(y,i) = Flm_row(x,i);
    1023      944109 :   return y;
    1024             : }
    1025             : 
    1026             : /********************************************************************/
    1027             : /**                                                                **/
    1028             : /**                           SCALAR MATRICES                      **/
    1029             : /**                                                                **/
    1030             : /********************************************************************/
    1031             : 
    1032             : GEN
    1033        3972 : gen_matid(long n, void *E, const struct bb_field *S)
    1034             : {
    1035        3972 :   GEN y = cgetg(n+1,t_MAT), _0, _1;
    1036             :   long i;
    1037        3972 :   if (n < 0) pari_err_DOMAIN("gen_matid", "dimension","<",gen_0,stoi(n));
    1038        3972 :   _0 = S->s(E,0);
    1039        3972 :   _1 = S->s(E,1);
    1040       17757 :   for (i=1; i<=n; i++)
    1041             :   {
    1042       13785 :     GEN z = const_col(n, _0); gel(z,i) = _1;
    1043       13785 :     gel(y, i) = z;
    1044             :   }
    1045        3972 :   return y;
    1046             : }
    1047             : 
    1048             : GEN
    1049          35 : matid_F2xqM(long n, GEN T)
    1050             : {
    1051             :   void *E;
    1052          35 :   const struct bb_field *S = get_F2xq_field(&E, T);
    1053          35 :   return gen_matid(n, E, S);
    1054             : }
    1055             : GEN
    1056          56 : matid_FlxqM(long n, GEN T, ulong p)
    1057             : {
    1058             :   void *E;
    1059          56 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    1060          56 :   return gen_matid(n, E, S);
    1061             : }
    1062             : 
    1063             : GEN
    1064     2186543 : matid_Flm(long n)
    1065             : {
    1066     2186543 :   GEN y = cgetg(n+1,t_MAT);
    1067             :   long i;
    1068     2186535 :   if (n < 0) pari_err_DOMAIN("matid_Flm", "dimension","<",gen_0,stoi(n));
    1069    12053301 :   for (i=1; i<=n; i++) { gel(y,i) = zero_zv(n); ucoeff(y, i,i) = 1; }
    1070     2186540 :   return y;
    1071             : }
    1072             : 
    1073             : GEN
    1074          42 : scalar_Flm(long s, long n)
    1075             : {
    1076             :   long i;
    1077          42 :   GEN y = cgetg(n+1,t_MAT);
    1078         560 :   for (i=1; i<=n; i++) { gel(y,i) = zero_Flv(n); coeff(y, i,i) = s; }
    1079          42 :   return y;
    1080             : }
    1081             : 
    1082             : /********************************************************************/
    1083             : /**                                                                **/
    1084             : /**                           CONVERSIONS                          **/
    1085             : /**                                                                **/
    1086             : /********************************************************************/
    1087             : GEN
    1088    41031573 : ZV_to_Flv(GEN x, ulong p)
    1089   532151190 : { pari_APPLY_ulong(umodiu(gel(x,i), p)) }
    1090             : 
    1091             : GEN
    1092     7471505 : ZM_to_Flm(GEN x, ulong p)
    1093    47589620 : { pari_APPLY_same(ZV_to_Flv(gel(x,i), p)) }
    1094             : 
    1095             : GEN
    1096        2233 : ZMV_to_FlmV(GEN x, ulong m)
    1097       17927 : { pari_APPLY_type(t_VEC,ZM_to_Flm(gel(x,i), m)) }
    1098             : 
    1099             : /*                          TO INTMOD                        */
    1100             : static GEN
    1101    11715358 : to_intmod(GEN x, GEN p) { retmkintmod(modii(x, p), p); }
    1102             : static GEN
    1103      383844 : Fl_to_intmod(ulong x, GEN p) { retmkintmod(utoi(x), p); }
    1104             : 
    1105             : GEN
    1106        3394 : Fp_to_mod(GEN z, GEN p)
    1107             : {
    1108        3394 :   retmkintmod(modii(z, p), icopy(p));
    1109             : }
    1110             : 
    1111             : GEN
    1112      617575 : FpX_to_mod_raw(GEN z, GEN p)
    1113             : {
    1114      617575 :   long i,l = lg(z);
    1115             :   GEN x;
    1116      617575 :   if (l == 2)
    1117             :   {
    1118      524566 :     x = cgetg(3,t_POL); x[1] = z[1];
    1119      524566 :     gel(x,2) = mkintmod(gen_0,p); return x;
    1120             :   }
    1121       93009 :   x = cgetg(l,t_POL);
    1122      435498 :   for (i=2; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1123       93009 :   x[1] = z[1]; return normalizepol_lg(x,l);
    1124             : }
    1125             : 
    1126             : /* z in Z[X], return z * Mod(1,p), normalized*/
    1127             : GEN
    1128     1245039 : FpX_to_mod(GEN z, GEN p)
    1129             : {
    1130     1245039 :   long i,l = lg(z);
    1131             :   GEN x;
    1132     1245039 :   if (l == 2)
    1133             :   {
    1134        1463 :     x = cgetg(3,t_POL); x[1] = z[1];
    1135        1463 :     gel(x,2) = mkintmod(gen_0,icopy(p)); return x;
    1136             :   }
    1137     1243576 :   x = cgetg(l,t_POL);
    1138     1243576 :   if (l >2) p = icopy(p);
    1139    12541306 :   for (i=2; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1140     1243576 :   x[1] = z[1]; return normalizepol_lg(x,l);
    1141             : }
    1142             : 
    1143             : GEN
    1144         511 : FpXC_to_mod(GEN z, GEN p)
    1145             : {
    1146         511 :   long i,l = lg(z);
    1147         511 :   GEN x = cgetg(l, t_COL);
    1148         511 :   p = icopy(p);
    1149        8799 :   for (i=1; i<l; i++)
    1150        8288 :     gel(x,i) = FpX_to_mod_raw(gel(z,i), p);
    1151         511 :   return x;
    1152             : }
    1153             : 
    1154             : static GEN
    1155          28 : FpXC_to_mod_raw(GEN x, GEN p)
    1156          84 : { pari_APPLY_type(t_COL, FpX_to_mod_raw(gel(x,i), p)) }
    1157             : 
    1158             : GEN
    1159          14 : FpXM_to_mod(GEN z, GEN p)
    1160             : {
    1161          14 :   long i,l = lg(z);
    1162          14 :   GEN x = cgetg(l, t_MAT);
    1163          14 :   p = icopy(p);
    1164          42 :   for (i=1; i<l; i++)
    1165          28 :     gel(x,i) = FpXC_to_mod_raw(gel(z,i), p);
    1166          14 :   return x;
    1167             : }
    1168             : 
    1169             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1170             : GEN
    1171        8134 : FpV_to_mod(GEN z, GEN p)
    1172             : {
    1173        8134 :   long i,l = lg(z);
    1174        8134 :   GEN x = cgetg(l, t_VEC);
    1175        8134 :   if (l == 1) return x;
    1176        8134 :   p = icopy(p);
    1177       16667 :   for (i=1; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1178        8134 :   return x;
    1179             : }
    1180             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1181             : GEN
    1182         105 : FpC_to_mod(GEN z, GEN p)
    1183             : {
    1184         105 :   long i, l = lg(z);
    1185         105 :   GEN x = cgetg(l, t_COL);
    1186         105 :   if (l == 1) return x;
    1187          91 :   p = icopy(p);
    1188         805 :   for (i=1; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1189          91 :   return x;
    1190             : }
    1191             : /* z in Mat m,n(Z), return z * Mod(1,p), normalized*/
    1192             : GEN
    1193         226 : FpM_to_mod(GEN z, GEN p)
    1194             : {
    1195         226 :   long i, j, m, l = lg(z);
    1196         226 :   GEN  x = cgetg(l,t_MAT), y, zi;
    1197         226 :   if (l == 1) return x;
    1198         205 :   m = lgcols(z);
    1199         205 :   p = icopy(p);
    1200        2456 :   for (i=1; i<l; i++)
    1201             :   {
    1202        2251 :     gel(x,i) = cgetg(m,t_COL);
    1203        2251 :     y = gel(x,i); zi= gel(z,i);
    1204       66799 :     for (j=1; j<m; j++) gel(y,j) = to_intmod(gel(zi,j), p);
    1205             :   }
    1206         205 :   return x;
    1207             : }
    1208             : GEN
    1209          28 : Flc_to_mod(GEN z, ulong pp)
    1210             : {
    1211          28 :   long i, l = lg(z);
    1212          28 :   GEN p, x = cgetg(l, t_COL);
    1213          28 :   if (l == 1) return x;
    1214          28 :   p = utoipos(pp);
    1215         112 :   for (i=1; i<l; i++) gel(x,i) = Fl_to_intmod(z[i], p);
    1216          28 :   return x;
    1217             : }
    1218             : GEN
    1219       56292 : Flm_to_mod(GEN z, ulong pp)
    1220             : {
    1221       56292 :   long i, j, m, l = lg(z);
    1222       56292 :   GEN p, x = cgetg(l,t_MAT), y, zi;
    1223       56292 :   if (l == 1) return x;
    1224       56264 :   m = lgcols(z);
    1225       56264 :   p = utoipos(pp);
    1226      179348 :   for (i=1; i<l; i++)
    1227             :   {
    1228      123084 :     gel(x,i) = cgetg(m,t_COL);
    1229      123084 :     y = gel(x,i); zi= gel(z,i);
    1230      506844 :     for (j=1; j<m; j++) gel(y,j) = Fl_to_intmod(zi[j], p);
    1231             :   }
    1232       56264 :   return x;
    1233             : }
    1234             : 
    1235             : GEN
    1236         574 : FpVV_to_mod(GEN z, GEN p)
    1237             : {
    1238         574 :   long i, j, m, l = lg(z);
    1239         574 :   GEN  x = cgetg(l,t_VEC), y, zi;
    1240         574 :   if (l == 1) return x;
    1241         574 :   m = lgcols(z);
    1242         574 :   p = icopy(p);
    1243        1246 :   for (i=1; i<l; i++)
    1244             :   {
    1245         672 :     gel(x,i) = cgetg(m,t_VEC);
    1246         672 :     y = gel(x,i); zi= gel(z,i);
    1247        2016 :     for (j=1; j<m; j++) gel(y,j) = to_intmod(gel(zi,j), p);
    1248             :   }
    1249         574 :   return x;
    1250             : }
    1251             : 
    1252             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1253             : GEN
    1254           7 : FpXQC_to_mod(GEN z, GEN T, GEN p)
    1255             : {
    1256           7 :   long i,l = lg(z);
    1257           7 :   GEN x = cgetg(l, t_COL);
    1258           7 :   if (l == 1) return x;
    1259           7 :   p = icopy(p);
    1260           7 :   T = FpX_to_mod_raw(T, p);
    1261          21 :   for (i=1; i<l; i++)
    1262          14 :     gel(x,i) = mkpolmod(FpX_to_mod_raw(gel(z,i), p), T);
    1263           7 :   return x;
    1264             : }
    1265             : 
    1266             : static GEN
    1267      584675 : Fq_to_mod_raw(GEN x, GEN T, GEN p)
    1268             : {
    1269      584675 :   GEN z = typ(x)==t_INT ? mkintmod(modii(x, p), p): FpX_to_mod_raw(x, p);
    1270      584675 :   return mkpolmod(z, T);
    1271             : }
    1272             : 
    1273             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1274             : GEN
    1275          14 : FqC_to_mod(GEN z, GEN T, GEN p)
    1276             : {
    1277             :   GEN x;
    1278          14 :   long i,l = lg(z);
    1279          14 :   if (!T) return FpC_to_mod(z, p);
    1280          14 :   x = cgetg(l, t_COL);
    1281          14 :   if (l == 1) return x;
    1282          14 :   p = icopy(p);
    1283          14 :   T = FpX_to_mod_raw(T, p);
    1284         252 :   for (i=1; i<l; i++)
    1285         238 :     gel(x,i) = Fq_to_mod_raw(gel(z, i), T, p);
    1286          14 :   return x;
    1287             : }
    1288             : 
    1289             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1290             : static GEN
    1291      108115 : FqC_to_mod_raw(GEN x, GEN T, GEN p)
    1292      692552 : { pari_APPLY_type(t_COL, Fq_to_mod_raw(gel(x,i), T, p)) }
    1293             : 
    1294             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1295             : GEN
    1296       26159 : FqM_to_mod(GEN z, GEN T, GEN p)
    1297             : {
    1298             :   GEN x;
    1299       26159 :   long i,l = lg(z);
    1300       26159 :   if (!T) return FpM_to_mod(z, p);
    1301       26159 :   x = cgetg(l, t_MAT);
    1302       26159 :   if (l == 1) return x;
    1303       26131 :   p = icopy(p);
    1304       26131 :   T = FpX_to_mod_raw(T, p);
    1305      134246 :   for (i=1; i<l; i++)
    1306      108115 :     gel(x,i) = FqC_to_mod_raw(gel(z, i), T, p);
    1307       26131 :   return x;
    1308             : }
    1309             : 
    1310             : /********************************************************************/
    1311             : /*                                                                  */
    1312             : /*                     Blackbox linear algebra                      */
    1313             : /*                                                                  */
    1314             : /********************************************************************/
    1315             : 
    1316             : /* A sparse column (zCs) v is a t_COL with two components C and E which are
    1317             :  * t_VECSMALL of the same length, representing sum_i E[i]*e_{C[i]}, where
    1318             :  * (e_j) is the canonical basis.
    1319             :  * A sparse matrix (zMs) is a t_VEC of zCs */
    1320             : 
    1321             : /* FpCs and FpMs are identical but E[i] is interpreted as a _signed_ C long
    1322             :  * integer representing an element of Fp. This is important since p can be
    1323             :  * large and p+E[i] would not fit in a C long.  */
    1324             : 
    1325             : /* RgCs and RgMs are similar, except that the type of the component is
    1326             :  * unspecified. Functions handling RgCs/RgMs must be independent of the type
    1327             :  * of E. */
    1328             : 
    1329             : /* Most functions take an argument nbrow which is the number of lines of the
    1330             :  * column/matrix, which cannot be derived from the data. */
    1331             : 
    1332             : GEN
    1333           0 : zCs_to_ZC(GEN R, long nbrow)
    1334             : {
    1335           0 :   GEN C = gel(R,1), E = gel(R,2), c = zerocol(nbrow);
    1336           0 :   long j, l = lg(C);
    1337           0 :   for (j = 1; j < l; ++j) gel(c, C[j]) = stoi(E[j]);
    1338           0 :   return c;
    1339             : }
    1340             : 
    1341             : GEN
    1342           0 : zMs_to_ZM(GEN x, long nbrow)
    1343           0 : { pari_APPLY_type(t_MAT, zCs_to_ZC(gel(x, i), nbrow)) }
    1344             : 
    1345             : /* Solve equation f(X) = B (mod p) where B is a FpV, and f is an endomorphism.
    1346             :  * Return either a solution as a t_COL, or a kernel vector as a t_VEC. */
    1347             : GEN
    1348         119 : gen_FpM_Wiedemann(void *E, GEN (*f)(void*, GEN), GEN B, GEN p)
    1349             : {
    1350         119 :   pari_sp ltop = avma;
    1351         119 :   long col = 0, n = lg(B)-1, m = 2*n+1;
    1352         119 :   if (ZV_equal0(B)) return zerocol(n);
    1353         119 :   while (++col <= n)
    1354             :   {
    1355         119 :     pari_sp btop = avma, av;
    1356             :     long i, lQ;
    1357         119 :     GEN V, Q, M, W = B;
    1358         119 :     GEN b = cgetg(m+2, t_POL);
    1359         119 :     b[1] = evalsigne(1)|evalvarn(0);
    1360         119 :     gel(b, 2) = gel(W, col);
    1361       49971 :     for (i = 3; i<m+2; i++)
    1362       49852 :       gel(b, i) = cgeti(lgefint(p));
    1363         119 :     av = avma;
    1364       49971 :     for (i = 3; i<m+2; i++)
    1365             :     {
    1366       49852 :       W = f(E, W);
    1367       49852 :       affii(gel(W, col),gel(b, i));
    1368       49852 :       if (gc_needed(av,1))
    1369             :       {
    1370         969 :         if (DEBUGMEM>1) pari_warn(warnmem,"Wiedemann: first loop, %ld",i);
    1371         969 :         W = gerepileupto(av, W);
    1372             :       }
    1373             :     }
    1374         119 :     b = FpX_renormalize(b, m+2);
    1375         119 :     if (lgpol(b)==0) {set_avma(btop); continue; }
    1376         119 :     M = FpX_halfgcd(b, pol_xn(m, 0), p);
    1377         119 :     Q = FpX_neg(FpX_normalize(gcoeff(M, 2, 1),p),p);
    1378         119 :     W = B; lQ =lg(Q);
    1379         119 :     if (DEBUGLEVEL) err_printf("Wiedemann: deg. minpoly: %ld\n",lQ-3);
    1380         119 :     V = FpC_Fp_mul(W, gel(Q, lQ-2), p);
    1381         119 :     av = avma;
    1382       24597 :     for (i = lQ-3; i > 1; i--)
    1383             :     {
    1384       24478 :       W = f(E, W);
    1385       24478 :       V = ZC_lincomb(gen_1, gel(Q,i), V, W);
    1386       24478 :       if (gc_needed(av,1))
    1387             :       {
    1388         696 :         if (DEBUGMEM>1) pari_warn(warnmem,"Wiedemann: second loop, %ld",i);
    1389         696 :         gerepileall(av, 2, &V, &W);
    1390             :       }
    1391             :     }
    1392         119 :     V = FpC_red(V, p);
    1393         119 :     W = FpC_sub(f(E,V), B, p);
    1394         119 :     if (ZV_equal0(W)) return gerepilecopy(ltop, V);
    1395           0 :     av = avma;
    1396           0 :     for (i = 1; i <= n; ++i)
    1397             :     {
    1398           0 :       V = W;
    1399           0 :       W = f(E, V);
    1400           0 :       if (ZV_equal0(W))
    1401           0 :         return gerepilecopy(ltop, shallowtrans(V));
    1402           0 :       gerepileall(av, 2, &V, &W);
    1403             :     }
    1404           0 :     set_avma(btop);
    1405             :   }
    1406           0 :   return NULL;
    1407             : }
    1408             : 
    1409             : GEN
    1410           0 : zMs_ZC_mul(GEN M, GEN B)
    1411             : {
    1412             :   long i, j;
    1413           0 :   long n = lg(B)-1;
    1414           0 :   GEN V = zerocol(n);
    1415           0 :   for (i = 1; i <= n; ++i)
    1416           0 :     if (signe(gel(B, i)))
    1417             :     {
    1418           0 :       GEN R = gel(M, i), C = gel(R, 1), E = gel(R, 2);
    1419           0 :       long l = lg(C);
    1420           0 :       for (j = 1; j < l; ++j)
    1421             :       {
    1422           0 :         long k = C[j];
    1423           0 :         switch(E[j])
    1424             :         {
    1425           0 :         case 1:
    1426           0 :           gel(V, k) = gel(V,k)==gen_0 ? gel(B,i) : addii(gel(V, k), gel(B,i));
    1427           0 :           break;
    1428           0 :         case -1:
    1429           0 :           gel(V, k) = gel(V,k)==gen_0 ? negi(gel(B,i)) : subii(gel(V, k), gel(B,i));
    1430           0 :           break;
    1431           0 :         default:
    1432           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]));
    1433           0 :           break;
    1434             :         }
    1435             :       }
    1436             :     }
    1437           0 :   return V;
    1438             : }
    1439             : 
    1440             : GEN
    1441           0 : FpMs_FpC_mul(GEN M, GEN B, GEN p) { return FpC_red(zMs_ZC_mul(M, B), p); }
    1442             : 
    1443             : GEN
    1444       74568 : ZV_zMs_mul(GEN B, GEN M)
    1445             : {
    1446             :   long i, j;
    1447       74568 :   long m = lg(M)-1;
    1448       74568 :   GEN V = cgetg(m+1,t_VEC);
    1449    38972792 :   for (i = 1; i <= m; ++i)
    1450             :   {
    1451    38898224 :     GEN R = gel(M, i), C = gel(R, 1), E = gel(R, 2);
    1452    38898224 :     long l = lg(C);
    1453             :     GEN z;
    1454    38898224 :     if (l == 1)
    1455             :     {
    1456           0 :       gel(V,i) = gen_0;
    1457           0 :       continue;
    1458             :     }
    1459    38898224 :     z = mulis(gel(B, C[1]), E[1]);
    1460   327090273 :     for (j = 2; j < l; ++j)
    1461             :     {
    1462   288192049 :       long k = C[j];
    1463   288192049 :       switch(E[j])
    1464             :       {
    1465   194218066 :       case 1:
    1466   194218066 :         z = addii(z, gel(B,k));
    1467   194218066 :         break;
    1468    78585272 :       case -1:
    1469    78585272 :         z = subii(z, gel(B,k));
    1470    78585272 :         break;
    1471    15388711 :       default:
    1472    15388711 :         z = addii(z, mulis(gel(B,k), E[j]));
    1473    15388711 :         break;
    1474             :       }
    1475             :     }
    1476    38898224 :     gel(V,i) = z;
    1477             :   }
    1478       74568 :   return V;
    1479             : }
    1480             : 
    1481             : GEN
    1482         119 : FpV_FpMs_mul(GEN B, GEN M, GEN p) { return FpV_red(ZV_zMs_mul(B, M), p); }
    1483             : 
    1484             : GEN
    1485           0 : ZlM_gauss(GEN a, GEN b, ulong p, long e, GEN C)
    1486             : {
    1487           0 :   pari_sp av = avma, av2;
    1488           0 :   GEN xi, xb, pi = gen_1, P;
    1489             :   long i;
    1490           0 :   if (!C) {
    1491           0 :     C = Flm_inv(ZM_to_Flm(a, p), p);
    1492           0 :     if (!C) pari_err_INV("ZlM_gauss", a);
    1493             :   }
    1494           0 :   P = utoipos(p);
    1495           0 :   av2 = avma;
    1496           0 :   xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    1497           0 :   xb = Flm_to_ZM(xi);
    1498           0 :   for (i = 2; i <= e; i++)
    1499             :   {
    1500           0 :     pi = muliu(pi, p); /* = p^(i-1) */
    1501           0 :     b = ZM_Z_divexact(ZM_sub(b, ZM_nm_mul(a, xi)), P);
    1502           0 :     if (gc_needed(av,2))
    1503             :     {
    1504           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZlM_gauss. i=%ld/%ld",i,e);
    1505           0 :       gerepileall(av2,3, &pi,&b,&xb);
    1506             :     }
    1507           0 :     xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    1508           0 :     xb = ZM_add(xb, nm_Z_mul(xi, pi));
    1509             :   }
    1510           0 :   return gerepileupto(av, xb);
    1511             : }
    1512             : 
    1513             : struct wrapper_modp_s {
    1514             :   GEN (*f)(void*, GEN);
    1515             :   void *E;
    1516             :   GEN p;
    1517             : };
    1518             : 
    1519             : /* compute f(x) mod p */
    1520             : static GEN
    1521       74449 : wrap_relcomb_modp(void *E, GEN x)
    1522             : {
    1523       74449 :   struct wrapper_modp_s *W = (struct wrapper_modp_s*)E;
    1524       74449 :   return FpC_red(W->f(W->E, x), W->p);
    1525             : }
    1526             : static GEN
    1527           0 : wrap_relcomb(void*E, GEN x) { return zMs_ZC_mul((GEN)E, x); }
    1528             : 
    1529             : static GEN
    1530       74449 : wrap_relker(void*E, GEN x) { return ZV_zMs_mul(x, (GEN)E); }
    1531             : 
    1532             : /* Solve f(X) = B (mod p^e); blackbox version of ZlM_gauss */
    1533             : GEN
    1534           0 : gen_ZpM_Dixon_Wiedemann(void *E, GEN (*f)(void*, GEN), GEN B, GEN p, long e)
    1535             : {
    1536             :   struct wrapper_modp_s W;
    1537           0 :   pari_sp av = avma;
    1538           0 :   GEN xb, xi, pi = gen_1;
    1539             :   long i;
    1540           0 :   W.E = E;
    1541           0 :   W.f = f;
    1542           0 :   W.p = p;
    1543           0 :   xi = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, FpC_red(B, p), p); /* f^(-1) B */
    1544           0 :   if (!xi || e == 1 || typ(xi) == t_VEC) return xi;
    1545           0 :   xb = xi;
    1546           0 :   for (i = 2; i <= e; i++)
    1547             :   {
    1548           0 :     pi = mulii(pi, p); /* = p^(i-1) */
    1549           0 :     B = ZC_Z_divexact(ZC_sub(B, f(E, xi)), p);
    1550           0 :     if (gc_needed(av,2))
    1551             :     {
    1552           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_ZpM_Dixon_Wiedemann. i=%ld",i);
    1553           0 :       gerepileall(av,3, &pi,&B,&xb);
    1554             :     }
    1555           0 :     xi = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, FpC_red(B, p), p);
    1556           0 :     if (!xi) return NULL;
    1557           0 :     if (typ(xi) == t_VEC) return gerepileupto(av, xi);
    1558           0 :     xb = ZC_add(xb, ZC_Z_mul(xi, pi));
    1559             :   }
    1560           0 :   return gerepileupto(av, xb);
    1561             : }
    1562             : 
    1563             : static GEN
    1564       24926 : vecprow(GEN A, GEN prow)
    1565             : {
    1566       24926 :   return mkvec2(vecsmallpermute(prow,gel(A,1)), gel(A,2));
    1567             : }
    1568             : 
    1569             : /* Solve the equation MX = A. Return either a solution as a t_COL,
    1570             :  * or the index of a column which is linearly dependent from the others as a
    1571             :  * t_VECSMALL with a single component. */
    1572             : GEN
    1573           0 : ZpMs_ZpCs_solve(GEN M, GEN A, long nbrow, GEN p, long e)
    1574             : {
    1575           0 :   pari_sp av = avma;
    1576             :   GEN pcol, prow;
    1577           0 :   long nbi=lg(M)-1, lR;
    1578             :   long i, n;
    1579             :   GEN Mp, Ap, Rp;
    1580             :   pari_timer ti;
    1581           0 :   if (DEBUGLEVEL) timer_start(&ti);
    1582           0 :   RgMs_structelim(M, nbrow, gel(A, 1), &pcol, &prow);
    1583           0 :   if (!pcol) return gc_NULL(av);
    1584           0 :   if (DEBUGLEVEL)
    1585           0 :     timer_printf(&ti,"structured elimination (%ld -> %ld)",nbi,lg(pcol)-1);
    1586           0 :   n = lg(pcol)-1;
    1587           0 :   Mp = cgetg(n+1, t_MAT);
    1588           0 :   for(i=1; i<=n; i++)
    1589           0 :     gel(Mp, i) = vecprow(gel(M,pcol[i]), prow);
    1590           0 :   Ap = zCs_to_ZC(vecprow(A, prow), n);
    1591           0 :   if (DEBUGLEVEL) timer_start(&ti);
    1592           0 :   Rp = gen_ZpM_Dixon_Wiedemann((void*)Mp,wrap_relcomb, Ap, p, e);
    1593           0 :   if (DEBUGLEVEL) timer_printf(&ti,"linear algebra");
    1594           0 :   if (!Rp) return gc_NULL(av);
    1595           0 :   lR = lg(Rp)-1;
    1596           0 :   if (typ(Rp) == t_COL)
    1597             :   {
    1598           0 :     GEN R = zerocol(nbi+1);
    1599           0 :     for (i=1; i<=lR; i++)
    1600           0 :        gel(R,pcol[i]) = gel(Rp,i);
    1601           0 :     return gerepilecopy(av, R);
    1602             :   }
    1603           0 :   for (i = 1; i <= lR; ++i)
    1604           0 :     if (signe(gel(Rp, i)))
    1605           0 :       return gerepileuptoleaf(av, mkvecsmall(pcol[i]));
    1606             :   return NULL; /* LCOV_EXCL_LINE */
    1607             : }
    1608             : 
    1609             : GEN
    1610           0 : FpMs_FpCs_solve(GEN M, GEN A, long nbrow, GEN p)
    1611             : {
    1612           0 :   return ZpMs_ZpCs_solve(M, A, nbrow, p, 1);
    1613             : }
    1614             : 
    1615             : GEN
    1616           0 : FpMs_FpCs_solve_safe(GEN M, GEN A, long nbrow, GEN p)
    1617             : {
    1618             :   GEN res;
    1619           0 :   pari_CATCH(e_INV)
    1620             :   {
    1621           0 :     GEN E = pari_err_last();
    1622           0 :     GEN x = err_get_compo(E,2);
    1623           0 :     if (typ(x) != t_INTMOD) pari_err(0,E);
    1624           0 :     if (DEBUGLEVEL)
    1625           0 :       pari_warn(warner,"FpMs_FpCs_solve_safe, impossible inverse %Ps", x);
    1626           0 :     res = NULL;
    1627             :   } pari_TRY {
    1628           0 :     res = ZpMs_ZpCs_solve(M, A, nbrow, p, 1);
    1629           0 :   } pari_ENDCATCH
    1630           0 :   return res;
    1631             : }
    1632             : 
    1633             : static GEN
    1634         119 : FpMs_structelim_back(GEN M, GEN V, GEN prow, GEN p)
    1635             : {
    1636         119 :   long i, j, oldf = 0, f = 0;
    1637         119 :   long lrow = lg(prow), lM = lg(M);
    1638         119 :   GEN W = const_vecsmall(lM-1,1);
    1639         119 :   GEN R = cgetg(lrow, t_VEC);
    1640      235298 :   for (i=1; i<lrow; i++)
    1641      235179 :     gel(R,i) = prow[i]==0 ? NULL: gel(V,prow[i]);
    1642             :   do
    1643             :   {
    1644         393 :     oldf = f;
    1645      436630 :     for(i=1; i<lM; i++)
    1646      436237 :       if (W[i])
    1647             :       {
    1648      147891 :         GEN C = gel(M,i), F = gel(C,1), E = gel(C,2);
    1649      147891 :         long c=0, cj=0, lF = lg(F);
    1650     1295120 :         for(j=1; j<lF; j++)
    1651     1147229 :           if (!gel(R,F[j])) { c++; cj=j; }
    1652      147891 :         if (c>=2) continue;
    1653      127154 :         if (c==1)
    1654             :         {
    1655       41418 :           pari_sp av = avma;
    1656       41418 :           GEN s = gen_0;
    1657      359180 :           for(j=1; j<lF; j++)
    1658      317762 :             if (j!=cj) s = Fp_add(s, mulis(gel(R,F[j]), E[j]), p);
    1659             :           /* s /= -E[cj] mod p */
    1660       41418 :           s = E[cj] < 0? Fp_divu(s, -E[cj], p): Fp_divu(Fp_neg(s,p), E[cj], p);
    1661       41418 :           gel(R,F[cj]) = gerepileuptoint(av, s);
    1662       41418 :           f++;
    1663             :         }
    1664      127154 :         W[i]=0;
    1665             :       }
    1666         393 :   } while(oldf!=f);
    1667      235298 :   for (i=1; i<lrow; i++)
    1668      235179 :     if (!gel(R,i)) gel(R,i) = gen_0;
    1669         119 :   return R;
    1670             : }
    1671             : 
    1672             : /* Return a linear form Y such that YM is essentially 0 */
    1673             : GEN
    1674         119 : FpMs_leftkernel_elt_col(GEN M, long nbcol, long nbrow, GEN p)
    1675             : {
    1676         119 :   pari_sp av = avma, av2;
    1677             :   GEN pcol, prow;
    1678             :   long i, n;
    1679             :   GEN Mp, B, MB, R, Rp;
    1680             :   pari_timer ti;
    1681             :   struct wrapper_modp_s W;
    1682         119 :   if (DEBUGLEVEL) timer_start(&ti);
    1683         119 :   RgMs_structelim_col(M, nbcol, nbrow, cgetg(1,t_VECSMALL), &pcol, &prow);
    1684         119 :   if (!pcol) return gc_NULL(av);
    1685         119 :   if (DEBUGLEVEL)
    1686           0 :     timer_printf(&ti,"structured elimination (%ld -> %ld)",nbcol,lg(pcol)-1);
    1687         119 :   n = lg(pcol)-1;
    1688         119 :   Mp = cgetg(n+1, t_MAT);
    1689       25045 :   for(i=1; i<=n; i++)
    1690       24926 :     gel(Mp, i) = vecprow(gel(M,pcol[i]), prow);
    1691         119 :   W.E = (void*) Mp;
    1692         119 :   W.f = wrap_relker;
    1693         119 :   W.p = p;
    1694         119 :   av2 = avma;
    1695             :   for(;;)
    1696             :   {
    1697         119 :     set_avma(av2);
    1698         119 :     B = random_FpV(n, p);
    1699         119 :     MB = FpV_FpMs_mul(B, Mp, p);
    1700         119 :     if (DEBUGLEVEL) timer_start(&ti);
    1701         119 :     pari_CATCH(e_INV)
    1702             :     {
    1703           0 :       GEN E = pari_err_last();
    1704           0 :       GEN x = err_get_compo(E,2);
    1705           0 :       if (typ(x) != t_INTMOD) pari_err(0,E);
    1706           0 :       if (DEBUGLEVEL)
    1707           0 :         pari_warn(warner,"FpMs_leftkernel_elt, impossible inverse %Ps", x);
    1708           0 :       Rp = NULL;
    1709             :     } pari_TRY {
    1710         119 :       Rp = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, MB, p);
    1711         119 :     } pari_ENDCATCH
    1712         119 :     if (!Rp) continue;
    1713         119 :     if (typ(Rp)==t_COL)
    1714             :     {
    1715         119 :       Rp = FpC_sub(Rp,B,p);
    1716         119 :       if (ZV_equal0(Rp)) continue;
    1717             :     }
    1718         119 :     R = FpMs_structelim_back(M, Rp, prow, p);
    1719         119 :     if (DEBUGLEVEL) timer_printf(&ti,"Wiedemann left kernel");
    1720         119 :     return gerepilecopy(av, R);
    1721             :   }
    1722             : }
    1723             : 
    1724             : GEN
    1725          42 : FpMs_leftkernel_elt(GEN M, long nbrow, GEN p)
    1726             : {
    1727          42 :   return FpMs_leftkernel_elt_col(M, lg(M)-1, nbrow, p);
    1728             : }

Generated by: LCOV version 1.13