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.8.0 lcov report (development 19623-dc26710) Lines: 628 832 75.5 %
Date: 2016-09-30 05:54:20 Functions: 89 116 76.7 %
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     4415224 : FpC_red(GEN z, GEN p)
      25             : {
      26     4415224 :   long i,l = lg(z);
      27     4415224 :   GEN x = cgetg(l, t_COL);
      28     4415224 :   for (i=1; i<l; i++) gel(x,i) = modii(gel(z,i),p);
      29     4415224 :   return x;
      30             : }
      31             : 
      32             : /* z in Z^n, return lift(Vec(z) * Mod(1,p)) */
      33             : GEN
      34      202006 : FpV_red(GEN z, GEN p)
      35             : {
      36      202006 :   long i,l = lg(z);
      37      202006 :   GEN x = cgetg(l, t_VEC);
      38      202006 :   for (i=1; i<l; i++) gel(x,i) = modii(gel(z,i),p);
      39      202006 :   return x;
      40             : }
      41             : GEN
      42      181874 : FpC_center(GEN z, GEN p, GEN pov2)
      43             : {
      44      181874 :   long i,l = lg(z);
      45      181874 :   GEN x = cgetg(l, t_COL);
      46      181874 :   for (i=1; i<l; i++) gel(x,i) = Fp_center(gel(z,i),p, pov2);
      47      181874 :   return x;
      48             : }
      49             : 
      50             : /* assume 0 <= u < p and ps2 = p>>1 */
      51             : INLINE void
      52          28 : Fp_center_inplace(GEN u, GEN p, GEN ps2)
      53          28 : { if (abscmpii(u,ps2) > 0) subiiz(u,p,u); }
      54             : 
      55             : void
      56          14 : FpC_center_inplace(GEN z, GEN p, GEN ps2)
      57             : {
      58          14 :   long i,l = lg(z);
      59          42 :   for (i=1; i<l; i++)
      60          28 :     Fp_center_inplace(gel(z,i), p, ps2);
      61          14 : }
      62             : 
      63             : GEN
      64      111678 : Flv_center(GEN z, ulong p, ulong ps2)
      65             : {
      66      111678 :   long i, l = lg(z);
      67      111678 :   GEN x = cgetg(l,t_VECSMALL);
      68      111678 :   for (i=1; i<l; i++) x[i] = Fl_center(z[i],p,ps2);
      69      111678 :   return x;
      70             : }
      71             : 
      72             : /* z in Mat m,n(Z), return lift(z * Mod(1,p)) */
      73             : GEN
      74      766687 : FpM_red(GEN z, GEN p)
      75             : {
      76      766687 :   long i, l = lg(z);
      77      766687 :   GEN x = cgetg(l,t_MAT);
      78      766687 :   for (i=1; i<l; i++) gel(x,i) = FpC_red(gel(z,i), p);
      79      766687 :   return x;
      80             : }
      81             : GEN
      82       49805 : FpM_center(GEN z, GEN p, GEN pov2)
      83             : {
      84       49805 :   long i, l = lg(z);
      85       49805 :   GEN x = cgetg(l,t_MAT);
      86       49805 :   for (i=1; i<l; i++) gel(x,i) = FpC_center(gel(z,i), p, pov2);
      87       49805 :   return x;
      88             : }
      89             : 
      90             : void
      91          14 : FpM_center_inplace(GEN z, GEN p, GEN pov2)
      92             : {
      93          14 :   long i, l = lg(z);
      94          14 :   for (i=1; i<l; i++) FpC_center_inplace(gel(z,i), p, pov2);
      95          14 : }
      96             : GEN
      97        7896 : Flm_center(GEN z, ulong p, ulong ps2)
      98             : {
      99        7896 :   long i, l = lg(z);
     100        7896 :   GEN x = cgetg(l,t_MAT);
     101        7896 :   for (i=1; i<l; i++) gel(x,i) = Flv_center(gel(z,i),p,ps2);
     102        7896 :   return x;
     103             : }
     104             : 
     105             : /********************************************************************/
     106             : /**                                                                **/
     107             : /**                           ADD, SUB                             **/
     108             : /**                                                                **/
     109             : /********************************************************************/
     110             : GEN
     111     8652406 : FpC_add(GEN x, GEN y, GEN p)
     112             : {
     113     8652406 :   long i, lx = lg(x);
     114     8652406 :   GEN z = cgetg(lx, t_COL);
     115     8652406 :   for (i = 1; i < lx; i++) gel(z, i) = Fp_add(gel(x, i), gel(y, i), p);
     116     8652406 :   return z;
     117             : }
     118             : GEN
     119           0 : FpV_add(GEN x, GEN y, GEN p)
     120             : {
     121           0 :   long i, lx = lg(x);
     122           0 :   GEN z = cgetg(lx, t_VEC);
     123           0 :   for (i = 1; i < lx; i++) gel(z, i) = Fp_add(gel(x, i), gel(y, i), p);
     124           0 :   return z;
     125             : }
     126             : GEN
     127      299782 : FpM_add(GEN x, GEN y, GEN p)
     128             : {
     129      299782 :   long lx = lg(x), j;
     130             :   GEN z;
     131      299782 :   if (lx == 1) return cgetg(1, t_MAT);
     132      299782 :   z = cgetg(lx, t_MAT);
     133      299782 :   for (j = 1; j < lx; j++) gel(z,j) = FpC_add(gel(x,j), gel(y,j), p);
     134      299782 :   return z;
     135             : }
     136             : 
     137             : GEN
     138   130458370 : Flv_add(GEN x, GEN y, ulong p)
     139             : {
     140   130458370 :   long i, l = lg(x);
     141   130458370 :   GEN z = cgetg(l, t_VECSMALL);
     142   130458370 :   if (p==2)
     143       41818 :     for (i = 1; i < l; i++) z[i] = x[i]^y[i];
     144             :   else
     145   130416552 :     for (i = 1; i < l; i++) z[i] = Fl_add(x[i], y[i], p);
     146   130458370 :   return z;
     147             : }
     148             : 
     149             : void
     150      457977 : Flv_add_inplace(GEN x, GEN y, ulong p)
     151             : {
     152      457977 :   long i, l = lg(x);
     153      457977 :   if (p==2)
     154      455877 :     for (i = 1; i < l; i++) x[i] ^= y[i];
     155             :   else
     156        2100 :     for (i = 1; i < l; i++) x[i] = Fl_add(x[i], y[i], p);
     157      457977 : }
     158             : 
     159             : ulong
     160        3822 : Flv_sum(GEN x, ulong p)
     161             : {
     162        3822 :   long i, l = lg(x);
     163        3822 :   ulong s = 0;
     164        3822 :   if (p==2)
     165        3822 :     for (i = 1; i < l; i++) s ^= x[i];
     166             :   else
     167           0 :     for (i = 1; i < l; i++) s = Fl_add(s, x[i], p);
     168        3822 :   return s;
     169             : }
     170             : 
     171             : GEN
     172      271796 : FpC_sub(GEN x, GEN y, GEN p)
     173             : {
     174      271796 :   long i, lx = lg(x);
     175      271796 :   GEN z = cgetg(lx, t_COL);
     176      271796 :   for (i = 1; i < lx; i++) gel(z, i) = Fp_sub(gel(x, i), gel(y, i), p);
     177      271796 :   return z;
     178             : }
     179             : GEN
     180           0 : FpV_sub(GEN x, GEN y, GEN p)
     181             : {
     182           0 :   long i, lx = lg(x);
     183           0 :   GEN z = cgetg(lx, t_VEC);
     184           0 :   for (i = 1; i < lx; i++) gel(z, i) = Fp_sub(gel(x, i), gel(y, i), p);
     185           0 :   return z;
     186             : }
     187             : 
     188             : GEN
     189           0 : FpM_sub(GEN x, GEN y, GEN p)
     190             : {
     191           0 :   long i, l = lg(x);
     192           0 :   GEN z = cgetg(l, t_MAT);
     193           0 :   for (i = 1; i < l; i++) gel(z, i) = FpC_sub(gel(x, i), gel(y, i), p);
     194           0 :   return z;
     195             : }
     196             : 
     197             : GEN
     198           0 : Flv_sub(GEN x, GEN y, ulong p)
     199             : {
     200           0 :   long i, l = lg(x);
     201           0 :   GEN z = cgetg(l, t_VECSMALL);
     202           0 :   for (i = 1; i < l; i++) z[i] = Fl_sub(x[i], y[i], p);
     203           0 :   return z;
     204             : }
     205             : 
     206             : void
     207           0 : Flv_sub_inplace(GEN x, GEN y, ulong p)
     208             : {
     209           0 :   long i, l = lg(x);
     210           0 :   for (i = 1; i < l; i++) x[i] = Fl_sub(x[i], y[i], p);
     211           0 : }
     212             : 
     213             : GEN
     214        8162 : Flm_Fl_add(GEN x, ulong y, ulong p)
     215             : {
     216        8162 :   long l = lg(x), i, j;
     217        8162 :   GEN z = cgetg(l,t_MAT);
     218             : 
     219        8162 :   if (l==1) return z;
     220        8162 :   if (l != lgcols(x)) pari_err_OP( "+", x, utoi(y));
     221       33460 :   for (i=1; i<l; i++)
     222             :   {
     223       25298 :     GEN zi = cgetg(l,t_VECSMALL), xi = gel(x,i);
     224       25298 :     gel(z,i) = zi;
     225       25298 :     for (j=1; j<l; j++) zi[j] = xi[j];
     226       25298 :     zi[i] = Fl_add(zi[i], y, p);
     227             :   }
     228        8162 :   return z;
     229             : }
     230             : 
     231             : GEN
     232       14651 : Flm_add(GEN x, GEN y, ulong p)
     233             : {
     234       14651 :   long i, l = lg(x);
     235       14651 :   GEN z = cgetg(l,t_MAT);
     236       14651 :   for (i = 1; i < l; i++) gel(z,i) = Flv_add(gel(x,i),gel(y,i),p);
     237       14651 :   return z;
     238             : }
     239             : 
     240             : GEN
     241           0 : Flm_sub(GEN x, GEN y, ulong p)
     242             : {
     243           0 :   long i, l = lg(x);
     244           0 :   GEN z = cgetg(l, t_MAT);
     245           0 :   for (i = 1; i < l; i++) gel(z, i) = Flv_sub(gel(x, i), gel(y, i), p);
     246           0 :   return z;
     247             : }
     248             : 
     249             : /********************************************************************/
     250             : /**                                                                **/
     251             : /**                           MULTIPLICATION                       **/
     252             : /**                                                                **/
     253             : /********************************************************************/
     254             : GEN
     255      401772 : FpC_Fp_mul(GEN x, GEN y, GEN p)
     256             : {
     257      401772 :   long i, l = lg(x);
     258      401772 :   GEN z = cgetg(l, t_COL);
     259      401772 :   for (i=1;i<l;i++) gel(z,i) = Fp_mul(gel(x,i),y,p);
     260      401772 :   return z;
     261             : }
     262             : GEN
     263      759445 : Flv_Fl_mul(GEN x, ulong y, ulong p)
     264             : {
     265      759445 :   long i, l = lg(x);
     266      759445 :   GEN z = cgetg(l, t_VECSMALL);
     267      759445 :   for (i=1;i<l;i++) z[i] = Fl_mul(x[i], y, p);
     268      759445 :   return z;
     269             : }
     270             : GEN
     271           0 : Flv_Fl_div(GEN x, ulong y, ulong p)
     272             : {
     273           0 :   return Flv_Fl_mul(x, Fl_inv(y, p), p);
     274             : }
     275             : void
     276           0 : Flv_Fl_div_inplace(GEN x, ulong y, ulong p)
     277             : {
     278           0 :   Flv_Fl_mul_inplace(x, Fl_inv(y, p), p);
     279           0 : }
     280             : GEN
     281      299782 : FpM_Fp_mul(GEN X, GEN c, GEN p) {
     282      299782 :   long i, j, h, l = lg(X);
     283      299782 :   GEN A = cgetg(l, t_MAT);
     284      299782 :   if (l == 1) return A;
     285      299782 :   h = lgcols(X);
     286     8825180 :   for (j=1; j<l; j++)
     287             :   {
     288     8525398 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     289     8525398 :     for (i = 1; i < h; i++) gel(a,i) = Fp_mul(gel(x,i), c, p);
     290     8525398 :     gel(A,j) = a;
     291             :   }
     292      299782 :   return A;
     293             : }
     294             : 
     295             : /* x *= y */
     296             : void
     297     1003088 : Flv_Fl_mul_part_inplace(GEN x, ulong y, ulong p, long l)
     298             : {
     299             :   long i;
     300     1003088 :   for (i=1;i<=l;i++) x[i] = Fl_mul(x[i], y, p);
     301     1003088 : }
     302             : void
     303           0 : Flv_Fl_mul_inplace(GEN x, ulong y, ulong p)
     304           0 : { Flv_Fl_mul_part_inplace(x, y, p, lg(x)-1); }
     305             : 
     306             : /* set y *= x */
     307             : void
     308       28873 : Flm_Fl_mul_inplace(GEN y, ulong x, ulong p)
     309             : {
     310       28873 :   long i, j, m = lgcols(y), l = lg(y);
     311       28873 :   if (HIGHWORD(x | p))
     312      296079 :     for(j=1; j<l; j++)
     313      267206 :       for(i=1; i<m; i++) ucoeff(y,i,j) = Fl_mul(ucoeff(y,i,j), x, p);
     314             :   else
     315           0 :     for(j=1; j<l; j++)
     316           0 :       for(i=1; i<m; i++) ucoeff(y,i,j) = (ucoeff(y,i,j) * x) % p;
     317       28873 : }
     318             : /* return x * y */
     319             : GEN
     320      373079 : Flm_Fl_mul(GEN y, ulong x, ulong p)
     321             : {
     322      373079 :   long i, j, m = lgcols(y), l = lg(y);
     323      373079 :   GEN z = cgetg(l, t_MAT);
     324      373079 :   if (HIGHWORD(x | p))
     325           0 :     for(j=1; j<l; j++) {
     326           0 :       GEN c = cgetg(m, t_VECSMALL); gel(z,j) = c;
     327           0 :       for(i=1; i<m; i++) c[i] = Fl_mul(ucoeff(y,i,j), x, p);
     328             :     }
     329             :   else
     330    10725848 :     for(j=1; j<l; j++) {
     331    10352769 :       GEN c = cgetg(m, t_VECSMALL); gel(z,j) = c;
     332    10352769 :       for(i=1; i<m; i++) c[i] = (ucoeff(y,i,j) * x) % p;
     333             :     }
     334      373079 :   return z;
     335             : }
     336             : 
     337             : GEN
     338          42 : Flv_neg(GEN v, ulong p)
     339             : {
     340          42 :   long i, m = lg(v);
     341          42 :   GEN c = cgetg(m, t_VECSMALL);
     342          42 :   for(i=1; i<m; i++) uel(c,i) = Fl_neg(uel(v,i), p);
     343          42 :   return c;
     344             : }
     345             : 
     346             : void
     347        3347 : Flv_neg_inplace(GEN v, ulong p)
     348             : {
     349             :   long i;
     350      148441 :   for (i = 1; i < lg(v); ++i)
     351      145094 :     v[i] = Fl_neg(v[i], p);
     352        3347 : }
     353             : 
     354             : GEN
     355          14 : Flm_neg(GEN y, ulong p)
     356             : {
     357          14 :   long j, l = lg(y);
     358          14 :   GEN z = cgetg(l, t_MAT);
     359          56 :   for(j=1; j<l; j++)
     360          42 :     gel(z,j) = Flv_neg(gel(y,j), p);
     361          14 :   return z;
     362             : }
     363             : 
     364             : /* x[i,]*y. Assume lx > 1 and 0 < i < lgcols(x) */
     365             : static GEN
     366     9764369 : ZMrow_ZC_mul_i(GEN x, GEN y, long lx, long i)
     367             : {
     368     9764369 :   GEN c = mulii(gcoeff(x,i,1), gel(y,1));
     369             :   long k;
     370   228423838 :   for (k = 2; k < lx; k++)
     371             :   {
     372   218659469 :     GEN t = mulii(gcoeff(x,i,k), gel(y,k));
     373   218659469 :     if (signe(t)) c = addii(c, t);
     374             :   }
     375     9764369 :   return c;
     376             : }
     377             : 
     378             : static long
     379    21635677 : zmrow_zc_mul(GEN x, GEN y, long lx, long i)
     380             : {
     381             :   long k;
     382    21635677 :   long c = coeff(x,i,1) * y[1];
     383   289228198 :   for (k = 2; k < lx; k++)
     384   267592521 :     c += coeff(x,i,k) * y[k];
     385    21635677 :   return c;
     386             : }
     387             : 
     388             : GEN
     389     1653239 : zm_zc_mul(GEN x, GEN y)
     390             : {
     391     1653239 :   long lx = lg(x), l, i;
     392             :   GEN z;
     393     1653239 :   if (lx == 1) return cgetg(1, t_VECSMALL);
     394     1653239 :   l = lg(gel(x,1));
     395     1653239 :   z = cgetg(l,t_VECSMALL);
     396     1653239 :   for (i=1; i<l; i++) z[i] = zmrow_zc_mul(x, y, lx, i);
     397     1653239 :   return z;
     398             : }
     399             : 
     400             : GEN
     401         252 : zm_mul(GEN x, GEN y)
     402             : {
     403         252 :   long i,j,lx=lg(x), ly=lg(y);
     404             :   GEN z;
     405         252 :   if (ly==1) return cgetg(1,t_MAT);
     406         252 :   z = cgetg(ly,t_MAT);
     407         252 :   if (lx==1)
     408             :   {
     409           0 :     for (i=1; i<ly; i++) gel(z,i) = cgetg(1,t_VECSMALL);
     410           0 :     return z;
     411             :   }
     412        2128 :   for (j=1; j<ly; j++)
     413        1876 :     gel(z,j) = zm_zc_mul(x, gel(y,j));
     414         252 :   return z;
     415             : }
     416             : 
     417             : static ulong
     418   180847868 : Flmrow_Flc_mul_SMALL(GEN x, GEN y, ulong p, long lx, long i)
     419             : {
     420   180847868 :   ulong c = ucoeff(x,i,1) * uel(y,1);
     421             :   long k;
     422  5845642777 :   for (k = 2; k < lx; k++) {
     423  5664794909 :     c += ucoeff(x,i,k) * uel(y,k);
     424  5664794909 :     if (c & HIGHBIT) c %= p;
     425             :   }
     426   180847868 :   return c % p;
     427             : }
     428             : 
     429             : static ulong
     430    48600580 : Flmrow_Flc_mul_i(GEN x, GEN y, ulong p, ulong pi, long lx, long i)
     431             : {
     432             :   ulong l0, l1, v1, h0, h1;
     433    48600580 :   long k = 1;
     434             :   LOCAL_OVERFLOW;
     435             :   LOCAL_HIREMAINDER;
     436    48600580 :   l1 = mulll(ucoeff(x,i,k), uel(y,k)); h1 = hiremainder; v1 = 0;
     437   746609294 :   while (++k < lx) {
     438   649408134 :     l0 = mulll(ucoeff(x,i,k), uel(y,k)); h0 = hiremainder;
     439   649408134 :     l1 = addll(l0, l1); h1 = addllx(h0, h1); v1 += overflow;
     440             :   }
     441    48600580 :   if (v1 == 0) return remll_pre(h1, l1, p, pi);
     442     2027070 :   else return remlll_pre(v1, h1, l1, p, pi);
     443             : }
     444             : 
     445             : static GEN
     446      397668 : Flm_Flc_mul_i_2(GEN x, GEN y, long lx, long l)
     447             : {
     448             :   long i,j;
     449      397668 :   GEN z = NULL;
     450             : 
     451     4342971 :   for (j=1; j<lx; j++)
     452             :   {
     453     3945303 :     if (!y[j]) continue;
     454      427491 :     if (!z) z = Flv_copy(gel(x,j));
     455      159796 :     else for (i = 1; i < l; i++) z[i] ^= coeff(x,i,j);
     456             :   }
     457      397668 :   if (!z) z = zero_zv(l-1);
     458      397668 :   return z;
     459             : }
     460             : 
     461             : static GEN
     462      488232 : FpM_FpC_mul_i(GEN x, GEN y, long lx, long l, GEN p)
     463             : {
     464      488232 :   GEN z = cgetg(l,t_COL);
     465             :   long i;
     466     9275104 :   for (i = 1; i < l; i++)
     467             :   {
     468     8786872 :     pari_sp av = avma;
     469     8786872 :     GEN c = ZMrow_ZC_mul_i(x, y, lx, i);
     470     8786872 :     gel(z,i) = gerepileuptoint(av, modii(c,p));
     471             :   }
     472      488232 :   return z;
     473             : }
     474             : 
     475             : static void
     476     8061207 : __Flm_Flc_mul_i_SMALL(GEN z, GEN x, GEN y, long lx, long l, ulong p)
     477             : {
     478             :   long i;
     479     8061207 :   for (i = 1; i < l; i++) z[i] = Flmrow_Flc_mul_SMALL(x, y, p, lx, i);
     480     8049867 : }
     481             : static GEN
     482     5132274 : Flm_Flc_mul_i_SMALL(GEN x, GEN y, long lx, long l, ulong p)
     483             : {
     484     5132274 :   GEN z = cgetg(l,t_VECSMALL);
     485     5132274 :   __Flm_Flc_mul_i_SMALL(z, x, y, lx, l, p);
     486     5132274 :   return z;
     487             : }
     488             : 
     489             : static void
     490     4961399 : __Flm_Flc_mul_i(GEN z, GEN x, GEN y, long lx, long l, ulong p, ulong pi)
     491             : {
     492             :   long i;
     493     4961399 :   for (i = 1; i < l; i++) z[i] = Flmrow_Flc_mul_i(x, y, p, pi, lx, i);
     494     4960362 : }
     495             : static GEN
     496     1106857 : Flm_Flc_mul_i(GEN x, GEN y, long lx, long l, ulong p, ulong pi)
     497             : {
     498     1106857 :   GEN z = cgetg(l,t_VECSMALL);
     499     1106857 :   __Flm_Flc_mul_i(z, x, y, lx, l, p, pi);
     500     1106857 :   return z;
     501             : }
     502             : 
     503             : INLINE GEN
     504      828795 : F2m_F2c_mul_i(GEN x, GEN y, long lx, long l)
     505             : {
     506             :   long j;
     507      828795 :   GEN z = NULL;
     508             : 
     509     6090326 :   for (j=1; j<lx; j++)
     510             :   {
     511     5261531 :     if (!F2v_coeff(y,j)) continue;
     512     1440420 :     if (!z) z = vecsmall_copy(gel(x,j));
     513      632653 :     else F2v_add_inplace(z,gel(x,j));
     514             :   }
     515      828795 :   if (!z) z = zero_F2v(l);
     516      828795 :   return z;
     517             : }
     518             : 
     519             : GEN
     520      479793 : FpM_mul(GEN x, GEN y, GEN p)
     521             : {
     522      479793 :   long j, l, lx=lg(x), ly=lg(y);
     523             :   GEN z;
     524      479793 :   if (ly==1) return cgetg(1,t_MAT);
     525      479793 :   if (lx==1) return zeromat(0, ly-1);
     526      479793 :   if (lgefint(p) == 3)
     527             :   {
     528      479048 :     pari_sp av = avma;
     529      479048 :     ulong pp = uel(p,2);
     530      479048 :     if (pp == 2)
     531             :     {
     532      147886 :       x = ZM_to_F2m(x);
     533      147886 :       y = ZM_to_F2m(y);
     534      147886 :       z = F2m_to_ZM(F2m_mul(x,y));
     535             :     }
     536             :     else
     537             :     {
     538      331162 :       x = ZM_to_Flm(x, pp);
     539      331162 :       y = ZM_to_Flm(y, pp);
     540      331162 :       z = Flm_to_ZM(Flm_mul(x,y, pp));
     541             :     }
     542      479048 :     return gerepileupto(av, z);
     543             :   }
     544         745 :   l = lgcols(x); z = cgetg(ly,t_MAT);
     545         745 :   for (j=1; j<ly; j++) gel(z,j) = FpM_FpC_mul_i(x, gel(y,j), lx, l, p);
     546         745 :   return z;
     547             : }
     548             : GEN
     549     1467724 : Flm_mul(GEN x, GEN y, ulong p)
     550             : {
     551     1467724 :   long i,j,l,lx=lg(x), ly=lg(y);
     552             :   GEN z;
     553     1467724 :   if (ly==1) return cgetg(1,t_MAT);
     554     1467724 :   z = cgetg(ly,t_MAT);
     555     1467724 :   if (lx==1)
     556             :   {
     557           0 :     for (i=1; i<ly; i++) gel(z,i) = cgetg(1,t_VECSMALL);
     558           0 :     return z;
     559             :   }
     560     1467724 :   l = lgcols(x);
     561     1467724 :   if (SMALL_ULONG(p)) {
     562     5304600 :     for (j=1; j<ly; j++)
     563     4888653 :       gel(z,j) = Flm_Flc_mul_i_SMALL(x, gel(y,j), lx, l, p);
     564             :   } else {
     565     1051777 :     ulong pi = get_Fl_red(p);
     566     2158522 :     for (j=1; j<ly; j++)
     567     1106745 :       gel(z,j) = Flm_Flc_mul_i(x, gel(y,j), lx, l, p, pi);
     568             :   }
     569     1467724 :   return z;
     570             : }
     571             : GEN
     572      147935 : F2m_mul(GEN x, GEN y)
     573             : {
     574      147935 :   long i,j,l,lx=lg(x), ly=lg(y);
     575             :   GEN z;
     576      147935 :   if (ly==1) return cgetg(1,t_MAT);
     577      147935 :   z = cgetg(ly,t_MAT);
     578      147935 :   if (lx==1)
     579             :   {
     580           0 :     for (i=1; i<ly; i++) gel(z,i) = mkvecsmall(0);
     581           0 :     return z;
     582             :   }
     583      147935 :   l = coeff(x,1,1);
     584      147935 :   for (j=1; j<ly; j++) gel(z,j) = F2m_F2c_mul_i(x, gel(y,j), lx, l);
     585      147935 :   return z;
     586             : }
     587             : 
     588             : static GEN
     589       14189 : _Flm_mul(void *p , GEN x, GEN y)
     590       14189 : { return Flm_mul(x,y,*(ulong*)p); }
     591             : static GEN
     592       42308 : _Flm_sqr(void *p, GEN x)
     593       42308 : { return Flm_mul(x,x,*(ulong*)p); }
     594             : GEN
     595       19614 : Flm_powu(GEN x, ulong n, ulong p)
     596             : {
     597       19614 :   pari_sp av = avma;
     598       19614 :   if (!n) return matid(lg(x)-1);
     599       19614 :   return gerepileupto(av, gen_powu(x, n, (void*)&p, &_Flm_sqr, &_Flm_mul));
     600             : }
     601             : static GEN
     602           0 : _F2m_mul(void *data, GEN x, GEN y)
     603           0 : { (void) data; return F2m_mul(x,y); }
     604             : static GEN
     605           0 : _F2m_sqr(void *data, GEN x)
     606           0 : { (void) data; return F2m_mul(x,x); }
     607             : GEN
     608           0 : F2m_powu(GEN x, ulong n)
     609             : {
     610           0 :   pari_sp av = avma;
     611           0 :   if (!n) return matid(lg(x)-1);
     612           0 :   return gerepileupto(av, gen_powu(x, n,NULL, &_F2m_sqr, &_F2m_mul));
     613             : }
     614             : static GEN
     615           0 : _FpM_mul(void *p , GEN x, GEN y)
     616           0 : { return FpM_mul(x,y,(GEN)p); }
     617             : static GEN
     618           0 : _FpM_sqr(void *p, GEN x)
     619           0 : { return FpM_mul(x,x,(GEN)p); }
     620             : GEN
     621           0 : FpM_powu(GEN x, ulong n, GEN p)
     622             : {
     623           0 :   pari_sp av = avma;
     624           0 :   if (!n) return matid(lg(x)-1);
     625           0 :   if (lgefint(p) == 3)
     626             :   {
     627           0 :     pari_sp av = avma;
     628           0 :     ulong pp = uel(p,2);
     629             :     GEN z;
     630           0 :     if (pp == 2)
     631           0 :       z = F2m_to_ZM(F2m_powu(ZM_to_F2m(x),n));
     632             :     else
     633           0 :       z = Flm_to_ZM(Flm_powu(ZM_to_Flm(x, pp), n, pp));
     634           0 :     return gerepileupto(av, z);
     635             :   }
     636           0 :   return gerepileupto(av, gen_powu(x, n, (void*)p, &_FpM_sqr, &_FpM_mul));
     637             : }
     638             : 
     639             : /*Multiple a column vector by a line vector to make a matrix*/
     640             : GEN
     641        1120 : FpC_FpV_mul(GEN x, GEN y, GEN p)
     642             : {
     643        1120 :   long i,j, lx=lg(x), ly=lg(y);
     644             :   GEN z;
     645        1120 :   if (ly==1) return cgetg(1,t_MAT);
     646        1120 :   z = cgetg(ly,t_MAT);
     647       23492 :   for (j=1; j < ly; j++)
     648             :   {
     649       22372 :     gel(z,j) = cgetg(lx,t_COL);
     650       22372 :     for (i=1; i<lx; i++) gcoeff(z,i,j) = Fp_mul(gel(x,i),gel(y,j), p);
     651             :   }
     652        1120 :   return z;
     653             : }
     654             : 
     655             : /* Multiply a line vector by a column and return a scalar (t_INT) */
     656             : GEN
     657      716889 : FpV_dotproduct(GEN x, GEN y, GEN p)
     658             : {
     659      716889 :   long i, lx = lg(x);
     660             :   pari_sp av;
     661             :   GEN c;
     662      716889 :   if (lx == 1) return gen_0;
     663      716847 :   av = avma; c = mulii(gel(x,1),gel(y,1));
     664      716847 :   for (i=2; i<lx; i++) c = addii(c, mulii(gel(x,i),gel(y,i)));
     665      716847 :   return gerepileuptoint(av, modii(c,p));
     666             : }
     667             : GEN
     668           0 : FpV_dotsquare(GEN x, GEN p)
     669             : {
     670           0 :   long i, lx = lg(x);
     671             :   pari_sp av;
     672             :   GEN c;
     673           0 :   if (lx == 1) return gen_0;
     674           0 :   av = avma; c = sqri(gel(x,1));
     675           0 :   for (i=2; i<lx; i++) c = addii(c, sqri(gel(x,i)));
     676           0 :   return gerepileuptoint(av, modii(c,p));
     677             : }
     678             : 
     679             : INLINE ulong
     680      826036 : Flv_dotproductspec_SMALL(GEN x, GEN y, ulong p, long lx)
     681             : {
     682      826036 :   ulong c = uel(x,0) * uel(y,0);
     683             :   long k;
     684    12559898 :   for (k = 1; k < lx; k++) {
     685    11733862 :     c += uel(x,k) * uel(y,k);
     686    11733862 :     if (c & HIGHBIT) c %= p;
     687             :   }
     688      826036 :   return c % p;
     689             : }
     690             : 
     691             : INLINE ulong
     692        4790 : Flv_dotproductspec_i(GEN x, GEN y, ulong p, ulong pi, long lx)
     693             : {
     694             :   ulong l0, l1, v1, h0, h1;
     695        4790 :   long i = 0;
     696             :   LOCAL_OVERFLOW;
     697             :   LOCAL_HIREMAINDER;
     698        4790 :   l1 = mulll(uel(x,i), uel(y,i)); h1 = hiremainder; v1 = 0;
     699      147856 :   while (++i < lx) {
     700      138276 :     l0 = mulll(uel(x,i), uel(y,i)); h0 = hiremainder;
     701      138276 :     l1 = addll(l0, l1); h1 = addllx(h0, h1); v1 += overflow;
     702             :   }
     703        4790 :   if (v1 == 0) return remll_pre(h1, l1, p, pi);
     704           0 :   else return remlll_pre(v1, h1, l1, p, pi);
     705             : }
     706             : 
     707             : ulong
     708      373079 : Flv_dotproduct(GEN x, GEN y, ulong p)
     709             : {
     710      373079 :   long lx = lg(x)-1;
     711      373079 :   if (lx == 1) return 0;
     712      373079 :   if (SMALL_ULONG(p))
     713      373079 :     return Flv_dotproductspec_SMALL(x+1, y+1, p, lx);
     714             :   else
     715           0 :     return Flv_dotproductspec_i(x+1, y+1, p, get_Fl_red(p), lx);
     716             : }
     717             : 
     718             : ulong
     719       44458 : Flv_dotproduct_pre(GEN x, GEN y, ulong p, ulong pi)
     720             : {
     721       44458 :   long lx = lg(x)-1;
     722       44458 :   if (lx == 1) return 0;
     723       44458 :   if (SMALL_ULONG(p))
     724       39752 :     return Flv_dotproductspec_SMALL(x+1, y+1, p, lx);
     725             :   else
     726        4706 :     return Flv_dotproductspec_i(x+1, y+1, p, pi, lx);
     727             : }
     728             : 
     729             : ulong
     730      420958 : Flx_dotproduct(GEN x, GEN y, ulong p)
     731             : {
     732      420958 :   long lx = minss(lgpol(x), lgpol(y));
     733      420958 :   if (lx == 0) return 0;
     734      413289 :   if (SMALL_ULONG(p))
     735      413205 :     return Flv_dotproductspec_SMALL(x+2, y+2, p, lx);
     736             :   else
     737          84 :     return Flv_dotproductspec_i(x+2, y+2, p, get_Fl_red(p), lx);
     738             : }
     739             : 
     740             : ulong
     741           0 : F2v_dotproduct(GEN x, GEN y)
     742             : {
     743           0 :   long i, lx = lg(x);
     744             :   ulong c;
     745           0 :   if (lx == 2) return 0;
     746           0 :   c = uel(x,2) & uel(y,2);
     747           0 :   for (i=3; i<lx; i++) c ^= uel(x,i) & uel(y,i);
     748             : #ifdef LONG_IS_64BIT
     749           0 :   c ^= c >> 32;
     750             : #endif
     751           0 :   c ^= c >> 16;
     752           0 :   c ^= c >>  8;
     753           0 :   c ^= c >>  4;
     754           0 :   c ^= c >>  2;
     755           0 :   c ^= c >>  1;
     756           0 :   return c & 1;
     757             : }
     758             : 
     759             : GEN
     760      481500 : FpM_FpC_mul(GEN x, GEN y, GEN p)
     761             : {
     762      481500 :   long lx = lg(x);
     763      481500 :   return lx==1? cgetg(1,t_COL): FpM_FpC_mul_i(x, y, lx, lgcols(x), p);
     764             : }
     765             : GEN
     766      747595 : Flm_Flc_mul(GEN x, GEN y, ulong p)
     767             : {
     768      747595 :   long l, lx = lg(x);
     769      747595 :   if (lx==1) return cgetg(1,t_VECSMALL);
     770      641401 :   l = lgcols(x);
     771      641401 :   if (p==2)
     772      397668 :     return Flm_Flc_mul_i_2(x, y, lx, l);
     773      243733 :   else if (SMALL_ULONG(p))
     774      243621 :     return Flm_Flc_mul_i_SMALL(x, y, lx, l, p);
     775             :   else
     776         112 :     return Flm_Flc_mul_i(x, y, lx, l, p, get_Fl_red(p));
     777             : }
     778             : 
     779             : GEN
     780        4816 : Flm_Flc_mul_pre(GEN x, GEN y, ulong p, ulong pi)
     781             : {
     782        4816 :   long l, lx = lg(x);
     783             :   GEN z;
     784        4816 :   if (lx==1) return cgetg(1,t_VECSMALL);
     785        4816 :   l = lgcols(x);
     786        4816 :   z = cgetg(l, t_VECSMALL);
     787        4816 :   if (SMALL_ULONG(p))
     788        4128 :     __Flm_Flc_mul_i_SMALL(z, x, y, lx, l, p);
     789             :   else
     790         688 :     __Flm_Flc_mul_i(z, x, y, lx, l, p, pi);
     791        4815 :   return z;
     792             : }
     793             : 
     794             : GEN
     795     6755586 : Flm_Flc_mul_pre_Flx(GEN x, GEN y, ulong p, ulong pi, long sv)
     796             : {
     797     6755586 :   long l, lx = lg(x);
     798             :   GEN z;
     799     6755586 :   if (lx==1) return pol0_Flx(sv);
     800     6755586 :   l = lgcols(x);
     801     6760259 :   z = cgetg(l + 1, t_VECSMALL); z[1] = sv;
     802     6764916 :   if (SMALL_ULONG(p))
     803     2912283 :     __Flm_Flc_mul_i_SMALL(z + 1, x, y, lx, l, p);
     804             :   else
     805     3852633 :     __Flm_Flc_mul_i(z + 1, x, y, lx, l, p, pi);
     806     6765568 :   return Flx_normalize(z, l + 1);
     807             : }
     808             : 
     809             : GEN
     810           0 : F2m_F2c_mul(GEN x, GEN y)
     811             : {
     812           0 :   long l, lx = lg(x);
     813           0 :   if (lx==1) return cgetg(1,t_VECSMALL);
     814           0 :   l = coeff(x,1,1);
     815           0 :   return F2m_F2c_mul_i(x, y, lx, l);
     816             : }
     817             : /* RgV_to_RgX(FpM_FpC_mul(x,y,p), v), p != NULL, memory clean */
     818             : GEN
     819      248140 : FpM_FpC_mul_FpX(GEN x, GEN y, GEN p, long v)
     820             : {
     821      248140 :   long i, l, lx = lg(x);
     822             :   GEN z;
     823      248140 :   if (lx==1) return pol_0(v);
     824      248140 :   l = lgcols(x);
     825      248140 :   z = new_chunk(l+1);
     826      398982 :   for (i=l-1; i; i--)
     827             :   {
     828      383048 :     pari_sp av = avma;
     829      383048 :     GEN p1 = ZMrow_ZC_mul_i(x,y,lx,i);
     830      383048 :     p1 = modii(p1, p);
     831      383048 :     if (signe(p1))
     832             :     {
     833      232206 :       if (i != l-1) stackdummy((pari_sp)(z + l+1), (pari_sp)(z + i+2));
     834      232206 :       gel(z,i+1) = gerepileuptoint(av, p1);
     835      232206 :       break;
     836             :     }
     837      150842 :     avma = av;
     838             :   }
     839      248140 :   if (!i) { avma = (pari_sp)(z + l+1); return pol_0(v); }
     840      232206 :   z[0] = evaltyp(t_POL) | evallg(i+2);
     841      232206 :   z[1] = evalsigne(1) | evalvarn(v);
     842      826655 :   for (; i; i--)
     843             :   {
     844      594449 :     pari_sp av = avma;
     845      594449 :     GEN p1 = ZMrow_ZC_mul_i(x,y,lx,i);
     846      594449 :     gel(z,i+1) = gerepileuptoint(av, modii(p1,p));
     847             :   }
     848      232206 :   return z;
     849             : }
     850             : 
     851             : /********************************************************************/
     852             : /**                                                                **/
     853             : /**                           TRANSPOSITION                        **/
     854             : /**                                                                **/
     855             : /********************************************************************/
     856             : 
     857             : /* == zm_transpose */
     858             : GEN
     859        4225 : Flm_transpose(GEN x)
     860             : {
     861        4225 :   long i, dx, lx = lg(x);
     862             :   GEN y;
     863        4225 :   if (lx == 1) return cgetg(1,t_MAT);
     864        4225 :   dx = lgcols(x); y = cgetg(dx,t_MAT);
     865        4227 :   for (i=1; i<dx; i++) gel(y,i) = Flm_row(x,i);
     866        4225 :   return y;
     867             : }
     868             : 
     869             : /********************************************************************/
     870             : /**                                                                **/
     871             : /**                           SCALAR MATRICES                      **/
     872             : /**                                                                **/
     873             : /********************************************************************/
     874             : 
     875             : GEN
     876          28 : gen_matid(long n, void *E, const struct bb_field *S)
     877             : {
     878          28 :   GEN y = cgetg(n+1,t_MAT), _0, _1;
     879             :   long i;
     880          28 :   if (n < 0) pari_err_DOMAIN("gen_matid", "dimension","<",gen_0,stoi(n));
     881          28 :   _0 = S->s(E,0);
     882          28 :   _1 = S->s(E,1);
     883         112 :   for (i=1; i<=n; i++)
     884             :   {
     885          84 :     GEN z = const_col(n, _0); gel(z,i) = _1;
     886          84 :     gel(y, i) = z;
     887             :   }
     888          28 :   return y;
     889             : }
     890             : 
     891             : GEN
     892       13391 : matid_F2m(long n)
     893             : {
     894       13391 :   GEN y = cgetg(n+1,t_MAT);
     895             :   long i;
     896       13391 :   if (n < 0) pari_err_DOMAIN("matid_F2m", "dimension","<",gen_0,stoi(n));
     897       13391 :   for (i=1; i<=n; i++) { gel(y,i) = zero_F2v(n); F2v_set(gel(y,i),i); }
     898       13391 :   return y;
     899             : }
     900             : 
     901             : GEN
     902          14 : matid_F2xqM(long n, GEN T)
     903             : {
     904             :   void *E;
     905          14 :   const struct bb_field *S = get_F2xq_field(&E, T);
     906          14 :   return gen_matid(n, E, S);
     907             : }
     908             : GEN
     909          14 : matid_FlxqM(long n, GEN T, ulong p)
     910             : {
     911             :   void *E;
     912          14 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
     913          14 :   return gen_matid(n, E, S);
     914             : }
     915             : 
     916             : GEN
     917      696082 : matid_Flm(long n)
     918             : {
     919      696082 :   GEN y = cgetg(n+1,t_MAT);
     920             :   long i;
     921      696082 :   if (n < 0) pari_err_DOMAIN("matid_Flm", "dimension","<",gen_0,stoi(n));
     922      696082 :   for (i=1; i<=n; i++) { gel(y,i) = zero_zv(n); ucoeff(y, i,i) = 1; }
     923      696082 :   return y;
     924             : }
     925             : 
     926             : GEN
     927          42 : scalar_Flm(long s, long n)
     928             : {
     929             :   long i;
     930          42 :   GEN y = cgetg(n+1,t_MAT);
     931          42 :   for (i=1; i<=n; i++) { gel(y,i) = zero_Flv(n); coeff(y, i,i) = s; }
     932          42 :   return y;
     933             : }
     934             : 
     935             : /********************************************************************/
     936             : /**                                                                **/
     937             : /**                           CONVERSIONS                          **/
     938             : /**                                                                **/
     939             : /********************************************************************/
     940             : GEN
     941    19744235 : ZV_to_Flv(GEN x, ulong p)
     942             : {
     943    19744235 :   long i, n = lg(x);
     944    19744235 :   GEN y = cgetg(n,t_VECSMALL);
     945    19744333 :   for (i=1; i<n; i++) y[i] = umodiu(gel(x,i), p);
     946    19744249 :   return y;
     947             : }
     948             : GEN
     949     3250061 : ZM_to_Flm(GEN x, ulong p)
     950             : {
     951     3250061 :   long j,n = lg(x);
     952     3250061 :   GEN y = cgetg(n,t_MAT);
     953     3250067 :   if (n == 1) return y;
     954     3249941 :   for (j=1; j<n; j++) gel(y,j) = ZV_to_Flv(gel(x,j), p);
     955     3249942 :   return y;
     956             : }
     957             : GEN
     958        1365 : ZMV_to_FlmV(GEN z, ulong m)
     959             : {
     960        1365 :   long i, l = lg(z);
     961        1365 :   GEN x = cgetg(l,t_VEC);
     962        1365 :   for (i=1; i<l; i++) gel(x,i) = ZM_to_Flm(gel(z,i), m);
     963        1365 :   return x;
     964             : }
     965             : 
     966             : /*                          TO INTMOD                        */
     967             : static GEN
     968     1269542 : to_intmod(GEN x, GEN p) { retmkintmod(modii(x, p), p); }
     969             : static GEN
     970        1154 : Fl_to_intmod(ulong x, GEN p) { retmkintmod(utoi(x), p); }
     971             : 
     972             : GEN
     973      281414 : Fp_to_mod(GEN z, GEN p)
     974             : {
     975      281414 :   retmkintmod(modii(z, p), icopy(p));
     976             : }
     977             : 
     978             : /* z in Z[X], return z * Mod(1,p), normalized*/
     979             : GEN
     980       53508 : FpX_to_mod(GEN z, GEN p)
     981             : {
     982       53508 :   long i,l = lg(z);
     983       53508 :   GEN x = cgetg(l,t_POL);
     984       53508 :   if (l >2) p = icopy(p);
     985       53508 :   for (i=2; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
     986       53508 :   x[1] = z[1]; return normalizepol_lg(x,l);
     987             : }
     988             : 
     989             : /* z in Z^n, return z * Mod(1,p), normalized*/
     990             : GEN
     991        4067 : FpV_to_mod(GEN z, GEN p)
     992             : {
     993        4067 :   long i,l = lg(z);
     994        4067 :   GEN x = cgetg(l, t_VEC);
     995        4067 :   if (l == 1) return x;
     996        4067 :   p = icopy(p);
     997        4067 :   for (i=1; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
     998        4067 :   return x;
     999             : }
    1000             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1001             : GEN
    1002          63 : FpC_to_mod(GEN z, GEN p)
    1003             : {
    1004          63 :   long i, l = lg(z);
    1005          63 :   GEN x = cgetg(l, t_COL);
    1006          63 :   if (l == 1) return x;
    1007          63 :   p = icopy(p);
    1008          63 :   for (i=1; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1009          63 :   return x;
    1010             : }
    1011             : /* z in Mat m,n(Z), return z * Mod(1,p), normalized*/
    1012             : GEN
    1013         100 : FpM_to_mod(GEN z, GEN p)
    1014             : {
    1015         100 :   long i, j, m, l = lg(z);
    1016         100 :   GEN  x = cgetg(l,t_MAT), y, zi;
    1017         100 :   if (l == 1) return x;
    1018          93 :   m = lgcols(z);
    1019          93 :   p = icopy(p);
    1020         790 :   for (i=1; i<l; i++)
    1021             :   {
    1022         697 :     gel(x,i) = cgetg(m,t_COL);
    1023         697 :     y = gel(x,i); zi= gel(z,i);
    1024         697 :     for (j=1; j<m; j++) gel(y,j) = to_intmod(gel(zi,j), p);
    1025             :   }
    1026          93 :   return x;
    1027             : }
    1028             : GEN
    1029          28 : Flc_to_mod(GEN z, ulong pp)
    1030             : {
    1031          28 :   long i, l = lg(z);
    1032          28 :   GEN p, x = cgetg(l, t_COL);
    1033          28 :   if (l == 1) return x;
    1034          28 :   p = utoipos(pp);
    1035          28 :   for (i=1; i<l; i++) gel(x,i) = Fl_to_intmod(z[i], p);
    1036          28 :   return x;
    1037             : }
    1038             : GEN
    1039         166 : Flm_to_mod(GEN z, ulong pp)
    1040             : {
    1041         166 :   long i, j, m, l = lg(z);
    1042         166 :   GEN p, x = cgetg(l,t_MAT), y, zi;
    1043         166 :   if (l == 1) return x;
    1044         152 :   m = lgcols(z);
    1045         152 :   p = utoipos(pp);
    1046         547 :   for (i=1; i<l; i++)
    1047             :   {
    1048         395 :     gel(x,i) = cgetg(m,t_COL);
    1049         395 :     y = gel(x,i); zi= gel(z,i);
    1050         395 :     for (j=1; j<m; j++) gel(y,j) = Fl_to_intmod(zi[j], p);
    1051             :   }
    1052         152 :   return x;
    1053             : }
    1054             : 
    1055             : GEN
    1056        1820 : FpVV_to_mod(GEN z, GEN p)
    1057             : {
    1058        1820 :   long i, j, m, l = lg(z);
    1059        1820 :   GEN  x = cgetg(l,t_VEC), y, zi;
    1060        1820 :   if (l == 1) return x;
    1061        1743 :   m = lgcols(z);
    1062        1743 :   p = icopy(p);
    1063        3647 :   for (i=1; i<l; i++)
    1064             :   {
    1065        1904 :     gel(x,i) = cgetg(m,t_VEC);
    1066        1904 :     y = gel(x,i); zi= gel(z,i);
    1067        1904 :     for (j=1; j<m; j++) gel(y,j) = to_intmod(gel(zi,j), p);
    1068             :   }
    1069        1743 :   return x;
    1070             : }
    1071             : 
    1072             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1073             : GEN
    1074           7 : FpXQC_to_mod(GEN z, GEN T, GEN p)
    1075             : {
    1076           7 :   long i,l = lg(z);
    1077           7 :   GEN x = cgetg(l, t_COL); T = FpX_to_mod(T, p);
    1078          21 :   for (i=1; i<l; i++)
    1079          14 :     gel(x,i) = mkpolmod(FpX_to_mod(gel(z,i), p), T);
    1080           7 :   return x;
    1081             : }
    1082             : 
    1083             : /********************************************************************/
    1084             : /*                                                                  */
    1085             : /*                     Blackbox linear algebra                      */
    1086             : /*                                                                  */
    1087             : /********************************************************************/
    1088             : 
    1089             : /* A sparse column (zCs) v is a t_COL with two components C and E which are
    1090             :  * t_VECSMALL of the same length, representing sum_i E[i]*e_{C[i]}, where
    1091             :  * (e_j) is the canonical basis.
    1092             :  * A sparse matrix (zMs) is a t_VEC of zCs */
    1093             : 
    1094             : /* FpCs and FpMs are identical but E[i] is interpreted as a _signed_ C long
    1095             :  * integer representing an element of Fp. This is important since p can be
    1096             :  * large and p+E[i] would not fit in a C long.  */
    1097             : 
    1098             : /* RgCs and RgMs are similar, except that the type of the component is
    1099             :  * unspecified. Functions handling RgCs/RgMs must be independent of the type
    1100             :  * of E. */
    1101             : 
    1102             : /* Most functions take an argument nbrow which is the number of lines of the
    1103             :  * column/matrix, which cannot be derived from the data. */
    1104             : 
    1105             : GEN
    1106           0 : zCs_to_ZC(GEN R, long nbrow)
    1107             : {
    1108           0 :   GEN C = gel(R,1), E = gel(R,2), c = zerocol(nbrow);
    1109           0 :   long j, l = lg(C);
    1110           0 :   for (j = 1; j < l; ++j) gel(c, C[j]) = stoi(E[j]);
    1111           0 :   return c;
    1112             : }
    1113             : 
    1114             : GEN
    1115           0 : zMs_to_ZM(GEN M, long nbrow)
    1116             : {
    1117           0 :   long i, l = lg(M);
    1118           0 :   GEN m = cgetg(l, t_MAT);
    1119           0 :   for (i = 1; i < l; ++i) gel(m,i) = zCs_to_ZC(gel(M,i), nbrow);
    1120           0 :   return m;
    1121             : }
    1122             : 
    1123             : /* Solve equation f(X) = B (mod p) where B is a FpV, and f is an endomorphism.
    1124             :  * Return either a solution as a t_COL, or a kernel vector as a t_VEC. */
    1125             : GEN
    1126         112 : gen_FpM_Wiedemann(void *E, GEN (*f)(void*, GEN), GEN B, GEN p)
    1127             : {
    1128         112 :   pari_sp ltop = avma;
    1129         112 :   long col = 0, n = lg(B)-1, m = 2*n+1;
    1130         112 :   if (ZV_equal0(B)) return zerocol(n);
    1131         224 :   while (++col <= n)
    1132             :   {
    1133         112 :     pari_sp btop = avma, av;
    1134             :     long i, lQ;
    1135         112 :     GEN V, Q, M, W = B;
    1136         112 :     GEN b = cgetg(m+2, t_POL);
    1137         112 :     b[1] = evalsigne(1)|evalvarn(0);
    1138         112 :     gel(b, 2) = gel(W, col);
    1139       49826 :     for (i = 3; i<m+2; i++)
    1140       49714 :       gel(b, i) = cgeti(lgefint(p));
    1141         112 :     av = avma;
    1142       49826 :     for (i = 3; i<m+2; i++)
    1143             :     {
    1144       49714 :       W = f(E, W);
    1145       49714 :       affii(gel(W, col),gel(b, i));
    1146       49714 :       if (gc_needed(av,1))
    1147             :       {
    1148        2378 :         if (DEBUGMEM>1) pari_warn(warnmem,"Wiedemann: first loop, %ld",i);
    1149        2378 :         W = gerepileupto(av, W);
    1150             :       }
    1151             :     }
    1152         112 :     b = FpX_renormalize(b, m+2);
    1153         112 :     if (lgpol(b)==0) {avma = btop; continue; }
    1154         112 :     M = FpX_halfgcd(b, pol_xn(m, 0), p);
    1155         112 :     Q = FpX_neg(FpX_normalize(gcoeff(M, 2, 1),p),p);
    1156         112 :     W = B; lQ =lg(Q);
    1157         112 :     if (DEBUGLEVEL) err_printf("Wiedemann: deg. minpoly: %ld\n",lQ-3);
    1158         112 :     V = FpC_Fp_mul(W, gel(Q, lQ-2), p);
    1159         112 :     av = avma;
    1160       24374 :     for (i = lQ-3; i > 1; i--)
    1161             :     {
    1162       24262 :       W = f(E, W);
    1163       24262 :       V = ZC_lincomb(gen_1, gel(Q,i), V, W);
    1164       24262 :       if (gc_needed(av,1))
    1165             :       {
    1166        1949 :         if (DEBUGMEM>1) pari_warn(warnmem,"Wiedemann: second loop, %ld",i);
    1167        1949 :         gerepileall(av, 2, &V, &W);
    1168             :       }
    1169             :     }
    1170         112 :     V = FpC_red(V, p);
    1171         112 :     W = FpC_sub(f(E,V), B, p);
    1172         224 :     if (ZV_equal0(W)) return gerepilecopy(ltop, V);
    1173           0 :     av = avma;
    1174           0 :     for (i = 1; i <= n; ++i)
    1175             :     {
    1176           0 :       V = W;
    1177           0 :       W = f(E, V);
    1178           0 :       if (ZV_equal0(W))
    1179           0 :         return gerepilecopy(ltop, shallowtrans(V));
    1180           0 :       gerepileall(av, 2, &V, &W);
    1181             :     }
    1182           0 :     avma = btop;
    1183             :   }
    1184           0 :   return NULL;
    1185             : }
    1186             : 
    1187             : GEN
    1188           0 : zMs_ZC_mul(GEN M, GEN B)
    1189             : {
    1190             :   long i, j;
    1191           0 :   long n = lg(B)-1;
    1192           0 :   GEN V = zerocol(n);
    1193           0 :   for (i = 1; i <= n; ++i)
    1194           0 :     if (signe(gel(B, i)))
    1195             :     {
    1196           0 :       GEN R = gel(M, i), C = gel(R, 1), E = gel(R, 2);
    1197           0 :       long l = lg(C);
    1198           0 :       for (j = 1; j < l; ++j)
    1199             :       {
    1200           0 :         long k = C[j];
    1201           0 :         switch(E[j])
    1202             :         {
    1203             :         case 1:
    1204           0 :           gel(V, k) = gel(V,k)==gen_0 ? gel(B,i) : addii(gel(V, k), gel(B,i));
    1205           0 :           break;
    1206             :         case -1:
    1207           0 :           gel(V, k) = gel(V,k)==gen_0 ? negi(gel(B,i)) : subii(gel(V, k), gel(B,i));
    1208           0 :           break;
    1209             :         default:
    1210           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]));
    1211           0 :           break;
    1212             :         }
    1213             :       }
    1214             :     }
    1215           0 :   return V;
    1216             : }
    1217             : 
    1218             : GEN
    1219           0 : FpMs_FpC_mul(GEN M, GEN B, GEN p) { return FpC_red(zMs_ZC_mul(M, B), p); }
    1220             : 
    1221             : GEN
    1222       74200 : ZV_zMs_mul(GEN B, GEN M)
    1223             : {
    1224             :   long i, j;
    1225       74200 :   long m = lg(M)-1;
    1226       74200 :   GEN V = cgetg(m+1,t_VEC);
    1227    40371114 :   for (i = 1; i <= m; ++i)
    1228             :   {
    1229    40296914 :     GEN R = gel(M, i), C = gel(R, 1), E = gel(R, 2);
    1230    40296914 :     long l = lg(C);
    1231    40296914 :     GEN z = mulis(gel(B, C[1]), E[1]);
    1232   338856266 :     for (j = 2; j < l; ++j)
    1233             :     {
    1234   298559352 :       long k = C[j];
    1235   298559352 :       switch(E[j])
    1236             :       {
    1237             :       case 1:
    1238   203977662 :         z = addii(z, gel(B,k));
    1239   203977662 :         break;
    1240             :       case -1:
    1241    81072089 :         z = subii(z, gel(B,k));
    1242    81072089 :         break;
    1243             :       default:
    1244    13509601 :         z = addii(z, mulis(gel(B,k), E[j]));
    1245    13509601 :         break;
    1246             :       }
    1247             :     }
    1248    40296914 :     gel(V,i) = z;
    1249             :   }
    1250       74200 :   return V;
    1251             : }
    1252             : 
    1253             : GEN
    1254         112 : FpV_FpMs_mul(GEN B, GEN M, GEN p) { return FpV_red(ZV_zMs_mul(B, M), p); }
    1255             : 
    1256             : GEN
    1257      437386 : ZlM_gauss(GEN a, GEN b, ulong p, long e, GEN C)
    1258             : {
    1259      437386 :   pari_sp av = avma, av2;
    1260      437386 :   GEN xi, xb, pi = gen_1, P;
    1261             :   long i;
    1262      437386 :   if (!C) {
    1263           0 :     C = Flm_inv(ZM_to_Flm(a, p), p);
    1264           0 :     if (!C) pari_err_INV("ZlM_gauss", a);
    1265             :   }
    1266      437386 :   P = utoipos(p);
    1267      437386 :   av2 = avma;
    1268      437386 :   xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    1269      437386 :   xb = Flm_to_ZM(xi);
    1270     1050289 :   for (i = 2; i <= e; i++)
    1271             :   {
    1272      612903 :     pi = muliu(pi, p); /* = p^(i-1) */
    1273      612903 :     b = ZM_Z_divexact(ZM_sub(b, ZM_nm_mul(a, xi)), P);
    1274      612903 :     if (gc_needed(av,2))
    1275             :     {
    1276          38 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZlM_gauss. i=%ld",i);
    1277          38 :       gerepileall(av2,3, &pi,&b,&xb);
    1278             :     }
    1279      612903 :     xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    1280      612903 :     xb = ZM_add(xb, nm_Z_mul(xi, pi));
    1281             :   }
    1282      437386 :   return gerepileupto(av, xb);
    1283             : }
    1284             : 
    1285             : struct wrapper_modp_s {
    1286             :   GEN (*f)(void*, GEN);
    1287             :   void *E;
    1288             :   GEN p;
    1289             : };
    1290             : 
    1291             : /* compute f(x) mod p */
    1292             : static GEN
    1293       74088 : wrap_relcomb_modp(void *E, GEN x)
    1294             : {
    1295       74088 :   struct wrapper_modp_s *W = (struct wrapper_modp_s*)E;
    1296       74088 :   return FpC_red(W->f(W->E, x), W->p);
    1297             : }
    1298             : static GEN
    1299           0 : wrap_relcomb(void*E, GEN x) { return zMs_ZC_mul((GEN)E, x); }
    1300             : 
    1301             : static GEN
    1302       74088 : wrap_relker(void*E, GEN x) { return ZV_zMs_mul(x, (GEN)E); }
    1303             : 
    1304             : /* Solve f(X) = B (mod p^e); blackbox version of ZlM_gauss */
    1305             : GEN
    1306           0 : gen_ZpM_Dixon(void *E, GEN (*f)(void*, GEN), GEN B, GEN p, long e)
    1307             : {
    1308             :   struct wrapper_modp_s W;
    1309           0 :   pari_sp av = avma;
    1310           0 :   GEN xb, xi, pi = gen_1;
    1311             :   long i;
    1312           0 :   W.E = E;
    1313           0 :   W.f = f;
    1314           0 :   W.p = p;
    1315           0 :   xi = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, FpC_red(B, p), p); /* f^(-1) B */
    1316           0 :   if (!xi || e == 1 || typ(xi) == t_VEC) return xi;
    1317           0 :   xb = xi;
    1318           0 :   for (i = 2; i <= e; i++)
    1319             :   {
    1320           0 :     pi = mulii(pi, p); /* = p^(i-1) */
    1321           0 :     B = ZC_Z_divexact(ZC_sub(B, f(E, xi)), p);
    1322           0 :     if (gc_needed(av,2))
    1323             :     {
    1324           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_ZpM_Dixon. i=%ld",i);
    1325           0 :       gerepileall(av,3, &pi,&B,&xb);
    1326             :     }
    1327           0 :     xi = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, FpC_red(B, p), p);
    1328           0 :     if (!xi) return NULL;
    1329           0 :     if (typ(xi) == t_VEC) return gerepileupto(av, xi);
    1330           0 :     xb = ZC_add(xb, ZC_Z_mul(xi, pi));
    1331             :   }
    1332           0 :   return gerepileupto(av, xb);
    1333             : }
    1334             : 
    1335             : static GEN
    1336       24857 : vecprow(GEN A, GEN prow)
    1337             : {
    1338       24857 :   return mkvec2(vecsmallpermute(prow,gel(A,1)), gel(A,2));
    1339             : }
    1340             : 
    1341             : /* Solve the equation MX = A. Return either a solution as a t_COL,
    1342             :  * or the index of a column which is linearly dependent from the others as a
    1343             :  * t_VECSMALL with a single component. */
    1344             : GEN
    1345           0 : ZpMs_ZpCs_solve(GEN M, GEN A, long nbrow, GEN p, long e)
    1346             : {
    1347           0 :   pari_sp av = avma;
    1348             :   GEN pcol, prow;
    1349           0 :   long nbi=lg(M)-1, lR;
    1350             :   long i, n;
    1351             :   GEN Mp, Ap, Rp;
    1352             :   pari_timer ti;
    1353           0 :   if (DEBUGLEVEL) timer_start(&ti);
    1354           0 :   RgMs_structelim(M, nbrow, gel(A, 1), &pcol, &prow);
    1355           0 :   if (!pcol) { avma = av; return NULL; }
    1356           0 :   if (DEBUGLEVEL)
    1357           0 :     timer_printf(&ti,"structured elimination (%ld -> %ld)",nbi,lg(pcol)-1);
    1358           0 :   n = lg(pcol)-1;
    1359           0 :   Mp = cgetg(n+1, t_MAT);
    1360           0 :   for(i=1; i<=n; i++)
    1361           0 :     gel(Mp, i) = vecprow(gel(M,pcol[i]), prow);
    1362           0 :   Ap = zCs_to_ZC(vecprow(A, prow), n);
    1363           0 :   if (DEBUGLEVEL) timer_start(&ti);
    1364           0 :   Rp = gen_ZpM_Dixon((void*)Mp,wrap_relcomb, Ap, p, e);
    1365           0 :   if (DEBUGLEVEL) timer_printf(&ti,"linear algebra");
    1366           0 :   if (!Rp) { avma = av; return NULL; }
    1367           0 :   lR = lg(Rp)-1;
    1368           0 :   if (typ(Rp) == t_COL)
    1369             :   {
    1370           0 :     GEN R = zerocol(nbi+1);
    1371           0 :     for (i=1; i<=lR; i++)
    1372           0 :        gel(R,pcol[i]) = gel(Rp,i);
    1373           0 :     return gerepilecopy(av, R);
    1374             :   }
    1375           0 :   for (i = 1; i <= lR; ++i)
    1376           0 :     if (signe(gel(Rp, i)))
    1377           0 :       return gerepileuptoleaf(av, mkvecsmall(pcol[i]));
    1378           0 :   return NULL; /* NOT REACHED */
    1379             : }
    1380             : 
    1381             : GEN
    1382           0 : FpMs_FpCs_solve(GEN M, GEN A, long nbrow, GEN p)
    1383             : {
    1384           0 :   return ZpMs_ZpCs_solve(M, A, nbrow, p, 1);
    1385             : }
    1386             : 
    1387             : GEN
    1388           0 : FpMs_FpCs_solve_safe(GEN M, GEN A, long nbrow, GEN p)
    1389             : {
    1390             :   GEN res;
    1391           0 :   pari_CATCH(e_INV)
    1392             :   {
    1393           0 :     GEN E = pari_err_last();
    1394           0 :     GEN x = err_get_compo(E,2);
    1395           0 :     if (typ(x) != t_INTMOD) pari_err(0,E);
    1396           0 :     if (DEBUGLEVEL)
    1397           0 :       pari_warn(warner,"FpMs_FpCs_solve_safe, impossible inverse %Ps", x);
    1398           0 :     res = NULL;
    1399             :   } pari_TRY {
    1400           0 :     res = ZpMs_ZpCs_solve(M, A, nbrow, p, 1);
    1401           0 :   } pari_ENDCATCH
    1402           0 :   return res;
    1403             : }
    1404             : 
    1405             : static GEN
    1406         112 : FpMs_structelim_back(GEN M, GEN V, GEN prow, GEN p)
    1407             : {
    1408         112 :   long i, j, oldf = 0, f = 0;
    1409         112 :   long lrow = lg(prow), lM = lg(M);
    1410         112 :   GEN W = const_vecsmall(lM-1,1);
    1411         112 :   GEN R = cgetg(lrow, t_VEC);
    1412      192031 :   for (i=1; i<lrow; i++)
    1413      191919 :     gel(R,i) = prow[i]==0 ? NULL: gel(V,prow[i]);
    1414             :   do
    1415             :   {
    1416         357 :     oldf = f;
    1417      402493 :     for(i=1; i<lM; i++)
    1418      402136 :       if (W[i])
    1419             :       {
    1420      135905 :         GEN C = gel(M,i), F = gel(C,1), E = gel(C,2);
    1421      135905 :         long c=0, cj=0, lF = lg(F);
    1422     1191477 :         for(j=1; j<lF; j++)
    1423     1055572 :           if (!gel(R,F[j]))
    1424       75866 :           { c++; cj=j; }
    1425      135905 :         if (c>=2) continue;
    1426      118545 :         if (c==1)
    1427             :         {
    1428       37471 :           pari_sp av = avma;
    1429       37471 :           GEN s = gen_0;
    1430      327208 :           for(j=1; j<lF; j++)
    1431      289737 :             if (j!=cj) s = Fp_add(s, mulis(gel(R,F[j]), E[j]), p);
    1432       37471 :           gel(R,F[cj]) = gerepileupto(av, Fp_div(Fp_neg(s, p), stoi(E[cj]), p));
    1433       37471 :           f++;
    1434             :         }
    1435      118545 :         W[i]=0;
    1436             :       }
    1437         357 :   } while(oldf!=f);
    1438      192031 :   for (i=1; i<lrow; i++)
    1439      191919 :     if (!gel(R,i)) gel(R,i) = gen_0;
    1440         112 :   return R;
    1441             : }
    1442             : 
    1443             : /* Return a linear form Y such that YM is essentially 0 */
    1444             : GEN
    1445         112 : FpMs_leftkernel_elt_col(GEN M, long nbcol, long nbrow, GEN p)
    1446             : {
    1447         112 :   pari_sp av = avma, av2;
    1448             :   GEN pcol, prow;
    1449             :   long i, n;
    1450             :   GEN Mp, B, MB, R, Rp;
    1451             :   pari_timer ti;
    1452             :   struct wrapper_modp_s W;
    1453         112 :   if (DEBUGLEVEL) timer_start(&ti);
    1454         112 :   RgMs_structelim_col(M, nbcol, nbrow, cgetg(1,t_VECSMALL), &pcol, &prow);
    1455         112 :   if (!pcol) { avma = av; return NULL; }
    1456         112 :   if (DEBUGLEVEL)
    1457           0 :     timer_printf(&ti,"structured elimination (%ld -> %ld)",nbcol,lg(pcol)-1);
    1458         112 :   n = lg(pcol)-1;
    1459         112 :   Mp = cgetg(n+1, t_MAT);
    1460       24969 :   for(i=1; i<=n; i++)
    1461       24857 :     gel(Mp, i) = vecprow(gel(M,pcol[i]), prow);
    1462         112 :   W.E = (void*) Mp;
    1463         112 :   W.f = wrap_relker;
    1464         112 :   W.p = p;
    1465         112 :   av2 = avma;
    1466             :   for(;;)
    1467             :   {
    1468         112 :     avma = av2;
    1469         112 :     B = cgetg(n+1,t_VEC);
    1470       24969 :     for(i=1; i<=n; i++)
    1471       24857 :       gel(B,i) = randomi(p);
    1472         112 :     MB = FpV_FpMs_mul(B, Mp, p);
    1473         112 :     if (DEBUGLEVEL) timer_start(&ti);
    1474         112 :     pari_CATCH(e_INV)
    1475             :     {
    1476           0 :       GEN E = pari_err_last();
    1477           0 :       GEN x = err_get_compo(E,2);
    1478           0 :       if (typ(x) != t_INTMOD) pari_err(0,E);
    1479           0 :       if (DEBUGLEVEL)
    1480           0 :         pari_warn(warner,"FpMs_leftkernel_elt, impossible inverse %Ps", x);
    1481           0 :       Rp = NULL;
    1482             :     } pari_TRY {
    1483         112 :       Rp = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, MB, p);
    1484         112 :     } pari_ENDCATCH
    1485         112 :     if (!Rp) continue;
    1486         112 :     if (typ(Rp)==t_COL)
    1487             :     {
    1488         112 :       Rp = FpC_sub(Rp,B,p);
    1489         112 :       if (ZV_equal0(Rp)) continue;
    1490             :     }
    1491         112 :     R = FpMs_structelim_back(M, Rp, prow, p);
    1492         112 :     if (DEBUGLEVEL) timer_printf(&ti,"Wiedemann left kernel");
    1493         112 :     return gerepilecopy(av, R);
    1494           0 :   }
    1495             : }
    1496             : 
    1497             : GEN
    1498          28 : FpMs_leftkernel_elt(GEN M, long nbrow, GEN p)
    1499             : {
    1500          28 :   return FpMs_leftkernel_elt_col(M, lg(M)-1, nbrow, p);
    1501             : }

Generated by: LCOV version 1.11