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 19357-d770f77) Lines: 628 832 75.5 %
Date: 2016-08-27 06:11:27 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     3977837 : FpC_red(GEN z, GEN p)
      25             : {
      26     3977837 :   long i,l = lg(z);
      27     3977837 :   GEN x = cgetg(l, t_COL);
      28     3977837 :   for (i=1; i<l; i++) gel(x,i) = modii(gel(z,i),p);
      29     3977837 :   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      751961 : FpM_red(GEN z, GEN p)
      75             : {
      76      751961 :   long i, l = lg(z);
      77      751961 :   GEN x = cgetg(l,t_MAT);
      78      751961 :   for (i=1; i<l; i++) gel(x,i) = FpC_red(gel(z,i), p);
      79      751961 :   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     7348635 : FpC_add(GEN x, GEN y, GEN p)
     112             : {
     113     7348635 :   long i, lx = lg(x);
     114     7348635 :   GEN z = cgetg(lx, t_COL);
     115     7348635 :   for (i = 1; i < lx; i++) gel(z, i) = Fp_add(gel(x, i), gel(y, i), p);
     116     7348635 :   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      267547 : FpM_add(GEN x, GEN y, GEN p)
     128             : {
     129      267547 :   long lx = lg(x), j;
     130             :   GEN z;
     131      267547 :   if (lx == 1) return cgetg(1, t_MAT);
     132      267547 :   z = cgetg(lx, t_MAT);
     133      267547 :   for (j = 1; j < lx; j++) gel(z,j) = FpC_add(gel(x,j), gel(y,j), p);
     134      267547 :   return z;
     135             : }
     136             : 
     137             : GEN
     138   130418190 : Flv_add(GEN x, GEN y, ulong p)
     139             : {
     140   130418190 :   long i, l = lg(x);
     141   130418190 :   GEN z = cgetg(l, t_VECSMALL);
     142   130418190 :   if (p==2)
     143       29554 :     for (i = 1; i < l; i++) z[i] = x[i]^y[i];
     144             :   else
     145   130388636 :     for (i = 1; i < l; i++) z[i] = Fl_add(x[i], y[i], p);
     146   130418190 :   return z;
     147             : }
     148             : 
     149             : void
     150      457071 : Flv_add_inplace(GEN x, GEN y, ulong p)
     151             : {
     152      457071 :   long i, l = lg(x);
     153      457071 :   if (p==2)
     154      454971 :     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      457071 : }
     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      271726 : FpC_sub(GEN x, GEN y, GEN p)
     173             : {
     174      271726 :   long i, lx = lg(x);
     175      271726 :   GEN z = cgetg(lx, t_COL);
     176      271726 :   for (i = 1; i < lx; i++) gel(z, i) = Fp_sub(gel(x, i), gel(y, i), p);
     177      271726 :   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        5950 : Flm_Fl_add(GEN x, ulong y, ulong p)
     215             : {
     216        5950 :   long l = lg(x), i, j;
     217        5950 :   GEN z = cgetg(l,t_MAT);
     218             : 
     219        5950 :   if (l==1) return z;
     220        5950 :   if (l != lgcols(x)) pari_err_OP( "+", x, utoi(y));
     221       24717 :   for (i=1; i<l; i++)
     222             :   {
     223       18767 :     GEN zi = cgetg(l,t_VECSMALL), xi = gel(x,i);
     224       18767 :     gel(z,i) = zi;
     225       18767 :     for (j=1; j<l; j++) zi[j] = xi[j];
     226       18767 :     zi[i] = Fl_add(zi[i], y, p);
     227             :   }
     228        5950 :   return z;
     229             : }
     230             : 
     231             : GEN
     232       13622 : Flm_add(GEN x, GEN y, ulong p)
     233             : {
     234       13622 :   long i, l = lg(x);
     235       13622 :   GEN z = cgetg(l,t_MAT);
     236       13622 :   for (i = 1; i < l; i++) gel(z,i) = Flv_add(gel(x,i),gel(y,i),p);
     237       13622 :   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      352121 : FpC_Fp_mul(GEN x, GEN y, GEN p)
     256             : {
     257      352121 :   long i, l = lg(x);
     258      352121 :   GEN z = cgetg(l, t_COL);
     259      352121 :   for (i=1;i<l;i++) gel(z,i) = Fp_mul(gel(x,i),y,p);
     260      352121 :   return z;
     261             : }
     262             : GEN
     263      727395 : Flv_Fl_mul(GEN x, ulong y, ulong p)
     264             : {
     265      727395 :   long i, l = lg(x);
     266      727395 :   GEN z = cgetg(l, t_VECSMALL);
     267      727395 :   for (i=1;i<l;i++) z[i] = Fl_mul(x[i], y, p);
     268      727395 :   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      267547 : FpM_Fp_mul(GEN X, GEN c, GEN p) {
     282      267547 :   long i, j, h, l = lg(X);
     283      267547 :   GEN A = cgetg(l, t_MAT);
     284      267547 :   if (l == 1) return A;
     285      267547 :   h = lgcols(X);
     286     7488992 :   for (j=1; j<l; j++)
     287             :   {
     288     7221445 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     289     7221445 :     for (i = 1; i < h; i++) gel(a,i) = Fp_mul(gel(x,i), c, p);
     290     7221445 :     gel(A,j) = a;
     291             :   }
     292      267547 :   return A;
     293             : }
     294             : 
     295             : /* x *= y */
     296             : void
     297      993912 : Flv_Fl_mul_part_inplace(GEN x, ulong y, ulong p, long l)
     298             : {
     299             :   long i;
     300      993912 :   for (i=1;i<=l;i++) x[i] = Fl_mul(x[i], y, p);
     301      993912 : }
     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       27925 : Flm_Fl_mul_inplace(GEN y, ulong x, ulong p)
     309             : {
     310       27925 :   long i, j, m = lgcols(y), l = lg(y);
     311       27925 :   if (HIGHWORD(x | p))
     312      293099 :     for(j=1; j<l; j++)
     313      265174 :       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       27925 : }
     318             : /* return x * y */
     319             : GEN
     320      373303 : Flm_Fl_mul(GEN y, ulong x, ulong p)
     321             : {
     322      373303 :   long i, j, m = lgcols(y), l = lg(y);
     323      373303 :   GEN z = cgetg(l, t_MAT);
     324      373303 :   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    10729656 :     for(j=1; j<l; j++) {
     331    10356353 :       GEN c = cgetg(m, t_VECSMALL); gel(z,j) = c;
     332    10356353 :       for(i=1; i<m; i++) c[i] = (ucoeff(y,i,j) * x) % p;
     333             :     }
     334      373303 :   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     9449300 : ZMrow_ZC_mul_i(GEN x, GEN y, long lx, long i)
     367             : {
     368     9449300 :   GEN c = mulii(gcoeff(x,i,1), gel(y,1));
     369             :   long k;
     370   226318061 :   for (k = 2; k < lx; k++)
     371             :   {
     372   216868761 :     GEN t = mulii(gcoeff(x,i,k), gel(y,k));
     373   216868761 :     if (signe(t)) c = addii(c, t);
     374             :   }
     375     9449300 :   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   177929087 : Flmrow_Flc_mul_SMALL(GEN x, GEN y, ulong p, long lx, long i)
     419             : {
     420   177929087 :   ulong c = ucoeff(x,i,1) * uel(y,1);
     421             :   long k;
     422  5816688512 :   for (k = 2; k < lx; k++) {
     423  5638759425 :     c += ucoeff(x,i,k) * uel(y,k);
     424  5638759425 :     if (c & HIGHBIT) c %= p;
     425             :   }
     426   177929087 :   return c % p;
     427             : }
     428             : 
     429             : static ulong
     430    47119205 : 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    47119205 :   long k = 1;
     434             :   LOCAL_OVERFLOW;
     435             :   LOCAL_HIREMAINDER;
     436    47119205 :   l1 = mulll(ucoeff(x,i,k), uel(y,k)); h1 = hiremainder; v1 = 0;
     437   730990963 :   while (++k < lx) {
     438   636752553 :     l0 = mulll(ucoeff(x,i,k), uel(y,k)); h0 = hiremainder;
     439   636752553 :     l1 = addll(l0, l1); h1 = addllx(h0, h1); v1 += overflow;
     440             :   }
     441    47119205 :   if (v1 == 0) return remll_pre(h1, l1, p, pi);
     442     1722645 :   else return remlll_pre(v1, h1, l1, p, pi);
     443             : }
     444             : 
     445             : static GEN
     446      395680 : Flm_Flc_mul_i_2(GEN x, GEN y, long lx, long l)
     447             : {
     448             :   long i,j;
     449      395680 :   GEN z = NULL;
     450             : 
     451     4338974 :   for (j=1; j<lx; j++)
     452             :   {
     453     3943294 :     if (!y[j]) continue;
     454      425636 :     if (!z) z = Flv_copy(gel(x,j));
     455      158053 :     else for (i = 1; i < l; i++) z[i] ^= coeff(x,i,j);
     456             :   }
     457      395680 :   if (!z) z = zero_zv(l-1);
     458      395680 :   return z;
     459             : }
     460             : 
     461             : static GEN
     462      443809 : FpM_FpC_mul_i(GEN x, GEN y, long lx, long l, GEN p)
     463             : {
     464      443809 :   GEN z = cgetg(l,t_COL);
     465             :   long i;
     466     8919364 :   for (i = 1; i < l; i++)
     467             :   {
     468     8475555 :     pari_sp av = avma;
     469     8475555 :     GEN c = ZMrow_ZC_mul_i(x, y, lx, i);
     470     8475555 :     gel(z,i) = gerepileuptoint(av, modii(c,p));
     471             :   }
     472      443809 :   return z;
     473             : }
     474             : 
     475             : static void
     476     7765680 : __Flm_Flc_mul_i_SMALL(GEN z, GEN x, GEN y, long lx, long l, ulong p)
     477             : {
     478             :   long i;
     479     7765680 :   for (i = 1; i < l; i++) z[i] = Flmrow_Flc_mul_SMALL(x, y, p, lx, i);
     480     7756412 : }
     481             : static GEN
     482     4838240 : Flm_Flc_mul_i_SMALL(GEN x, GEN y, long lx, long l, ulong p)
     483             : {
     484     4838240 :   GEN z = cgetg(l,t_VECSMALL);
     485     4838240 :   __Flm_Flc_mul_i_SMALL(z, x, y, lx, l, p);
     486     4838240 :   return z;
     487             : }
     488             : 
     489             : static void
     490     4678900 : __Flm_Flc_mul_i(GEN z, GEN x, GEN y, long lx, long l, ulong p, ulong pi)
     491             : {
     492             :   long i;
     493     4678900 :   for (i = 1; i < l; i++) z[i] = Flmrow_Flc_mul_i(x, y, p, pi, lx, i);
     494     4682440 : }
     495             : static GEN
     496      829135 : Flm_Flc_mul_i(GEN x, GEN y, long lx, long l, ulong p, ulong pi)
     497             : {
     498      829135 :   GEN z = cgetg(l,t_VECSMALL);
     499      829135 :   __Flm_Flc_mul_i(z, x, y, lx, l, p, pi);
     500      829135 :   return z;
     501             : }
     502             : 
     503             : INLINE GEN
     504      810159 : F2m_F2c_mul_i(GEN x, GEN y, long lx, long l)
     505             : {
     506             :   long j;
     507      810159 :   GEN z = NULL;
     508             : 
     509     5907574 :   for (j=1; j<lx; j++)
     510             :   {
     511     5097415 :     if (!F2v_coeff(y,j)) continue;
     512     1401543 :     if (!z) z = vecsmall_copy(gel(x,j));
     513      611907 :     else F2v_add_inplace(z,gel(x,j));
     514             :   }
     515      810159 :   if (!z) z = zero_F2v(l);
     516      810159 :   return z;
     517             : }
     518             : 
     519             : GEN
     520      410601 : FpM_mul(GEN x, GEN y, GEN p)
     521             : {
     522      410601 :   long j, l, lx=lg(x), ly=lg(y);
     523             :   GEN z;
     524      410601 :   if (ly==1) return cgetg(1,t_MAT);
     525      410601 :   if (lx==1) return zeromat(0, ly-1);
     526      410601 :   if (lgefint(p) == 3)
     527             :   {
     528      409876 :     pari_sp av = avma;
     529      409876 :     ulong pp = uel(p,2);
     530      409876 :     if (pp == 2)
     531             :     {
     532      144849 :       x = ZM_to_F2m(x);
     533      144849 :       y = ZM_to_F2m(y);
     534      144849 :       z = F2m_to_ZM(F2m_mul(x,y));
     535             :     }
     536             :     else
     537             :     {
     538      265027 :       x = ZM_to_Flm(x, pp);
     539      265027 :       y = ZM_to_Flm(y, pp);
     540      265027 :       z = Flm_to_ZM(Flm_mul(x,y, pp));
     541             :     }
     542      409876 :     return gerepileupto(av, z);
     543             :   }
     544         725 :   l = lgcols(x); z = cgetg(ly,t_MAT);
     545         725 :   for (j=1; j<ly; j++) gel(z,j) = FpM_FpC_mul_i(x, gel(y,j), lx, l, p);
     546         725 :   return z;
     547             : }
     548             : GEN
     549     1120550 : Flm_mul(GEN x, GEN y, ulong p)
     550             : {
     551     1120550 :   long i,j,l,lx=lg(x), ly=lg(y);
     552             :   GEN z;
     553     1120550 :   if (ly==1) return cgetg(1,t_MAT);
     554     1120550 :   z = cgetg(ly,t_MAT);
     555     1120550 :   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     1120550 :   l = lgcols(x);
     561     1120550 :   if (SMALL_ULONG(p)) {
     562     4941388 :     for (j=1; j<ly; j++)
     563     4595669 :       gel(z,j) = Flm_Flc_mul_i_SMALL(x, gel(y,j), lx, l, p);
     564             :   } else {
     565      774831 :     ulong pi = get_Fl_red(p);
     566     1603854 :     for (j=1; j<ly; j++)
     567      829023 :       gel(z,j) = Flm_Flc_mul_i(x, gel(y,j), lx, l, p, pi);
     568             :   }
     569     1120550 :   return z;
     570             : }
     571             : GEN
     572      144898 : F2m_mul(GEN x, GEN y)
     573             : {
     574      144898 :   long i,j,l,lx=lg(x), ly=lg(y);
     575             :   GEN z;
     576      144898 :   if (ly==1) return cgetg(1,t_MAT);
     577      144898 :   z = cgetg(ly,t_MAT);
     578      144898 :   if (lx==1)
     579             :   {
     580           0 :     for (i=1; i<ly; i++) gel(z,i) = mkvecsmall(0);
     581           0 :     return z;
     582             :   }
     583      144898 :   l = coeff(x,1,1);
     584      144898 :   for (j=1; j<ly; j++) gel(z,j) = F2m_F2c_mul_i(x, gel(y,j), lx, l);
     585      144898 :   return z;
     586             : }
     587             : 
     588             : static GEN
     589       14203 : _Flm_mul(void *p , GEN x, GEN y)
     590       14203 : { return Flm_mul(x,y,*(ulong*)p); }
     591             : static GEN
     592       42336 : _Flm_sqr(void *p, GEN x)
     593       42336 : { return Flm_mul(x,x,*(ulong*)p); }
     594             : GEN
     595       19628 : Flm_powu(GEN x, ulong n, ulong p)
     596             : {
     597       19628 :   pari_sp av = avma;
     598       19628 :   if (!n) return matid(lg(x)-1);
     599       19628 :   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        1050 : FpC_FpV_mul(GEN x, GEN y, GEN p)
     642             : {
     643        1050 :   long i,j, lx=lg(x), ly=lg(y);
     644             :   GEN z;
     645        1050 :   if (ly==1) return cgetg(1,t_MAT);
     646        1050 :   z = cgetg(ly,t_MAT);
     647       23100 :   for (j=1; j < ly; j++)
     648             :   {
     649       22050 :     gel(z,j) = cgetg(lx,t_COL);
     650       22050 :     for (i=1; i<lx; i++) gcoeff(z,i,j) = Fp_mul(gel(x,i),gel(y,j), p);
     651             :   }
     652        1050 :   return z;
     653             : }
     654             : 
     655             : /* Multiply a line vector by a column and return a scalar (t_INT) */
     656             : GEN
     657     2263861 : FpV_dotproduct(GEN x, GEN y, GEN p)
     658             : {
     659     2263861 :   long i, lx = lg(x);
     660             :   pari_sp av;
     661             :   GEN c;
     662     2263861 :   if (lx == 1) return gen_0;
     663     2263826 :   av = avma; c = mulii(gel(x,1),gel(y,1));
     664     2263826 :   for (i=2; i<lx; i++) c = addii(c, mulii(gel(x,i),gel(y,i)));
     665     2263826 :   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      815214 : Flv_dotproductspec_SMALL(GEN x, GEN y, ulong p, long lx)
     681             : {
     682      815214 :   ulong c = uel(x,0) * uel(y,0);
     683             :   long k;
     684    12989417 :   for (k = 1; k < lx; k++) {
     685    12174203 :     c += uel(x,k) * uel(y,k);
     686    12174203 :     if (c & HIGHBIT) c %= p;
     687             :   }
     688      815214 :   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      373303 : Flv_dotproduct(GEN x, GEN y, ulong p)
     709             : {
     710      373303 :   long lx = lg(x)-1;
     711      373303 :   if (lx == 1) return 0;
     712      373303 :   if (SMALL_ULONG(p))
     713      373303 :     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       44457 : Flv_dotproduct_pre(GEN x, GEN y, ulong p, ulong pi)
     720             : {
     721       44457 :   long lx = lg(x)-1;
     722       44457 :   if (lx == 1) return 0;
     723       44457 :   if (SMALL_ULONG(p))
     724       39751 :     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      410504 : Flx_dotproduct(GEN x, GEN y, ulong p)
     731             : {
     732      410504 :   long lx = minss(lgpol(x), lgpol(y));
     733      410504 :   if (lx == 0) return 0;
     734      402244 :   if (SMALL_ULONG(p))
     735      402160 :     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      437117 : FpM_FpC_mul(GEN x, GEN y, GEN p)
     761             : {
     762      437117 :   long lx = lg(x);
     763      437117 :   return lx==1? cgetg(1,t_COL): FpM_FpC_mul_i(x, y, lx, lgcols(x), p);
     764             : }
     765             : GEN
     766      743909 : Flm_Flc_mul(GEN x, GEN y, ulong p)
     767             : {
     768      743909 :   long l, lx = lg(x);
     769      743909 :   if (lx==1) return cgetg(1,t_VECSMALL);
     770      638363 :   l = lgcols(x);
     771      638363 :   if (p==2)
     772      395680 :     return Flm_Flc_mul_i_2(x, y, lx, l);
     773      242683 :   else if (SMALL_ULONG(p))
     774      242571 :     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        4816 :   return z;
     792             : }
     793             : 
     794             : GEN
     795     6754861 : Flm_Flc_mul_pre_Flx(GEN x, GEN y, ulong p, ulong pi, long sv)
     796             : {
     797     6754861 :   long l, lx = lg(x);
     798             :   GEN z;
     799     6754861 :   if (lx==1) return pol0_Flx(sv);
     800     6754861 :   l = lgcols(x);
     801     6760683 :   z = cgetg(l + 1, t_VECSMALL); z[1] = sv;
     802     6762021 :   if (SMALL_ULONG(p))
     803     2911537 :     __Flm_Flc_mul_i_SMALL(z + 1, x, y, lx, l, p);
     804             :   else
     805     3850484 :     __Flm_Flc_mul_i(z + 1, x, y, lx, l, p, pi);
     806     6766014 :   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      247223 : FpM_FpC_mul_FpX(GEN x, GEN y, GEN p, long v)
     820             : {
     821      247223 :   long i, l, lx = lg(x);
     822             :   GEN z;
     823      247223 :   if (lx==1) return pol_0(v);
     824      247223 :   l = lgcols(x);
     825      247223 :   z = new_chunk(l+1);
     826      396525 :   for (i=l-1; i; i--)
     827             :   {
     828      380843 :     pari_sp av = avma;
     829      380843 :     GEN p1 = ZMrow_ZC_mul_i(x,y,lx,i);
     830      380843 :     p1 = modii(p1, p);
     831      380843 :     if (signe(p1))
     832             :     {
     833      231541 :       if (i != l-1) stackdummy((pari_sp)(z + l+1), (pari_sp)(z + i+2));
     834      231541 :       gel(z,i+1) = gerepileuptoint(av, p1);
     835      231541 :       break;
     836             :     }
     837      149302 :     avma = av;
     838             :   }
     839      247223 :   if (!i) { avma = (pari_sp)(z + l+1); return pol_0(v); }
     840      231541 :   z[0] = evaltyp(t_POL) | evallg(i+2);
     841      231541 :   z[1] = evalsigne(1) | evalvarn(v);
     842      824443 :   for (; i; i--)
     843             :   {
     844      592902 :     pari_sp av = avma;
     845      592902 :     GEN p1 = ZMrow_ZC_mul_i(x,y,lx,i);
     846      592902 :     gel(z,i+1) = gerepileuptoint(av, modii(p1,p));
     847             :   }
     848      231541 :   return z;
     849             : }
     850             : 
     851             : /********************************************************************/
     852             : /**                                                                **/
     853             : /**                           TRANSPOSITION                        **/
     854             : /**                                                                **/
     855             : /********************************************************************/
     856             : 
     857             : /* == zm_transpose */
     858             : GEN
     859        3993 : Flm_transpose(GEN x)
     860             : {
     861        3993 :   long i, dx, lx = lg(x);
     862             :   GEN y;
     863        3993 :   if (lx == 1) return cgetg(1,t_MAT);
     864        3993 :   dx = lgcols(x); y = cgetg(dx,t_MAT);
     865        3998 :   for (i=1; i<dx; i++) gel(y,i) = Flm_row(x,i);
     866        3993 :   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       12879 : matid_F2m(long n)
     893             : {
     894       12879 :   GEN y = cgetg(n+1,t_MAT);
     895             :   long i;
     896       12879 :   if (n < 0) pari_err_DOMAIN("matid_F2m", "dimension","<",gen_0,stoi(n));
     897       12879 :   for (i=1; i<=n; i++) { gel(y,i) = zero_F2v(n); F2v_set(gel(y,i),i); }
     898       12879 :   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      589280 : matid_Flm(long n)
     918             : {
     919      589280 :   GEN y = cgetg(n+1,t_MAT);
     920             :   long i;
     921      589280 :   if (n < 0) pari_err_DOMAIN("matid_Flm", "dimension","<",gen_0,stoi(n));
     922      589280 :   for (i=1; i<=n; i++) { gel(y,i) = zero_zv(n); ucoeff(y, i,i) = 1; }
     923      589280 :   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    17037835 : ZV_to_Flv(GEN x, ulong p)
     942             : {
     943    17037835 :   long i, n = lg(x);
     944    17037835 :   GEN y = cgetg(n,t_VECSMALL);
     945    17037937 :   for (i=1; i<n; i++) y[i] = umodiu(gel(x,i), p);
     946    17037838 :   return y;
     947             : }
     948             : GEN
     949     2632744 : ZM_to_Flm(GEN x, ulong p)
     950             : {
     951     2632744 :   long j,n = lg(x);
     952     2632744 :   GEN y = cgetg(n,t_MAT);
     953     2632755 :   if (n == 1) return y;
     954     2632629 :   for (j=1; j<n; j++) gel(y,j) = ZV_to_Flv(gel(x,j), p);
     955     2632623 :   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     1269507 : 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      281518 : Fp_to_mod(GEN z, GEN p)
     974             : {
     975      281518 :   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        4032 : FpV_to_mod(GEN z, GEN p)
     992             : {
     993        4032 :   long i,l = lg(z);
     994        4032 :   GEN x = cgetg(l, t_VEC);
     995        4032 :   if (l == 1) return x;
     996        4032 :   p = icopy(p);
     997        4032 :   for (i=1; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
     998        4032 :   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, monomial(gen_1, 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      341094 : ZlM_gauss(GEN a, GEN b, ulong p, long e, GEN C)
    1258             : {
    1259      341094 :   pari_sp av = avma, av2;
    1260      341094 :   GEN xi, xb, pi = gen_1, P;
    1261             :   long i;
    1262      341094 :   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      341094 :   P = utoipos(p);
    1267      341094 :   av2 = avma;
    1268      341094 :   xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    1269      341094 :   xb = Flm_to_ZM(xi);
    1270      773380 :   for (i = 2; i <= e; i++)
    1271             :   {
    1272      432286 :     pi = muliu(pi, p); /* = p^(i-1) */
    1273      432286 :     b = ZM_Z_divexact(ZM_sub(b, ZM_nm_mul(a, xi)), P);
    1274      432286 :     if (gc_needed(av,2))
    1275             :     {
    1276          33 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZlM_gauss. i=%ld",i);
    1277          33 :       gerepileall(av2,3, &pi,&b,&xb);
    1278             :     }
    1279      432286 :     xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    1280      432286 :     xb = ZM_add(xb, nm_Z_mul(xi, pi));
    1281             :   }
    1282      341094 :   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