Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - FpV.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 24988-2584e74448) Lines: 715 915 78.1 %
Date: 2020-01-26 05:57:03 Functions: 108 129 83.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    17709159 : FpC_red(GEN x, GEN p)
      25    17709159 : { pari_APPLY_type(t_COL, modii(gel(x,i), p)) }
      26             : 
      27             : /* z in Z^n, return lift(Vec(z) * Mod(1,p)) */
      28             : GEN
      29      207830 : FpV_red(GEN x, GEN p)
      30      207830 : { pari_APPLY_type(t_VEC, modii(gel(x,i), p)) }
      31             : 
      32             : GEN
      33     1467375 : FpC_center(GEN x, GEN p, GEN pov2)
      34     1467375 : { pari_APPLY_type(t_COL, Fp_center(gel(x,i), p, pov2)) }
      35             : 
      36             : /* assume 0 <= u < p and ps2 = p>>1 */
      37             : INLINE void
      38          28 : Fp_center_inplace(GEN u, GEN p, GEN ps2)
      39          28 : { if (abscmpii(u,ps2) > 0) subiiz(u,p,u); }
      40             : 
      41             : void
      42          14 : FpC_center_inplace(GEN z, GEN p, GEN ps2)
      43             : {
      44          14 :   long i,l = lg(z);
      45          42 :   for (i=1; i<l; i++)
      46          28 :     Fp_center_inplace(gel(z,i), p, ps2);
      47          14 : }
      48             : 
      49             : GEN
      50      142275 : Flv_center(GEN x, ulong p, ulong ps2)
      51      142275 : { pari_APPLY_ulong(Fl_center(uel(x,i),p,ps2)) }
      52             : 
      53             : /* z in Mat m,n(Z), return lift(z * Mod(1,p)) */
      54             : GEN
      55     4593483 : FpM_red(GEN x, GEN p)
      56     4593483 : { pari_APPLY_same(FpC_red(gel(x,i), p)) }
      57             : 
      58             : GEN
      59      168539 : FpM_center(GEN x, GEN p, GEN pov2)
      60      168539 : { pari_APPLY_same(FpC_center(gel(x,i), p,pov2)) }
      61             : 
      62             : void
      63          14 : FpM_center_inplace(GEN z, GEN p, GEN pov2)
      64             : {
      65          14 :   long i, l = lg(z);
      66          14 :   for (i=1; i<l; i++) FpC_center_inplace(gel(z,i), p, pov2);
      67          14 : }
      68             : GEN
      69        9947 : Flm_center(GEN x, ulong p, ulong ps2)
      70        9947 : { pari_APPLY_same(Flv_center(gel(x,i), p,ps2)) }
      71             : 
      72             : GEN
      73         126 : random_FpV(long d, GEN p)
      74             : {
      75             :   long i;
      76         126 :   GEN y = cgetg(d+1,t_VEC);
      77         126 :   for (i=1; i<=d; i++) gel(y,i) = randomi(p);
      78         126 :   return y;
      79             : }
      80             : 
      81             : GEN
      82         413 : random_FpC(long d, GEN p)
      83             : {
      84             :   long i;
      85         413 :   GEN y = cgetg(d+1,t_COL);
      86         413 :   for (i=1; i<=d; i++) gel(y,i) = randomi(p);
      87         413 :   return y;
      88             : }
      89             : 
      90             : GEN
      91        4737 : random_Flv(long n, ulong p)
      92             : {
      93        4737 :   GEN y = cgetg(n+1, t_VECSMALL);
      94             :   long i;
      95        4737 :   for (i=1; i<=n; i++) uel(y,i) = random_Fl(p);
      96        4737 :   return y;
      97             : }
      98             : 
      99             : /********************************************************************/
     100             : /**                                                                **/
     101             : /**                           ADD, SUB                             **/
     102             : /**                                                                **/
     103             : /********************************************************************/
     104             : GEN
     105      185381 : FpC_add(GEN x, GEN y, GEN p)
     106      185381 : { pari_APPLY_type(t_COL, Fp_add(gel(x,i), gel(y,i), p)) }
     107             : 
     108             : GEN
     109           0 : FpV_add(GEN x, GEN y, GEN p)
     110           0 : { pari_APPLY_type(t_VEC, Fp_add(gel(x,i), gel(y,i), p)) }
     111             : 
     112             : GEN
     113           0 : FpM_add(GEN x, GEN y, GEN p)
     114           0 : { pari_APPLY_same(FpC_add(gel(x,i), gel(y,i), p)) }
     115             : 
     116             : GEN
     117      596019 : Flv_add(GEN x, GEN y, ulong p)
     118             : {
     119      596019 :   if (p==2)
     120       78902 :     pari_APPLY_ulong(x[i]^y[i])
     121             :   else
     122      517117 :     pari_APPLY_ulong(Fl_add(x[i], y[i], p))
     123             : }
     124             : 
     125             : void
     126      192003 : Flv_add_inplace(GEN x, GEN y, ulong p)
     127             : {
     128      192003 :   long i, l = lg(x);
     129      192003 :   if (p==2)
     130      188461 :     for (i = 1; i < l; i++) x[i] ^= y[i];
     131             :   else
     132        3542 :     for (i = 1; i < l; i++) x[i] = Fl_add(x[i], y[i], p);
     133      192003 : }
     134             : 
     135             : ulong
     136        3907 : Flv_sum(GEN x, ulong p)
     137             : {
     138        3907 :   long i, l = lg(x);
     139        3907 :   ulong s = 0;
     140        3907 :   if (p==2)
     141        3907 :     for (i = 1; i < l; i++) s ^= x[i];
     142             :   else
     143           0 :     for (i = 1; i < l; i++) s = Fl_add(s, x[i], p);
     144        3907 :   return s;
     145             : }
     146             : 
     147             : GEN
     148     2217716 : FpC_sub(GEN x, GEN y, GEN p)
     149     2217716 : { pari_APPLY_type(t_COL, Fp_sub(gel(x,i), gel(y,i), p)) }
     150             : 
     151             : GEN
     152           0 : FpV_sub(GEN x, GEN y, GEN p)
     153           0 : { pari_APPLY_type(t_VEC, Fp_sub(gel(x,i), gel(y,i), p)) }
     154             : 
     155             : GEN
     156      555368 : FpM_sub(GEN x, GEN y, GEN p)
     157      555368 : { pari_APPLY_same(FpC_sub(gel(x,i), gel(y,i), p)) }
     158             : 
     159             : GEN
     160   139669323 : Flv_sub(GEN x, GEN y, ulong p)
     161   139669323 : { pari_APPLY_ulong(Fl_sub(uel(x,i), uel(y,i), p)) }
     162             : 
     163             : void
     164           0 : Flv_sub_inplace(GEN x, GEN y, ulong p)
     165             : {
     166           0 :   long i, l = lg(x);
     167           0 :   for (i = 1; i < l; i++) x[i] = Fl_sub(x[i], y[i], p);
     168           0 : }
     169             : 
     170             : GEN
     171       14896 : Flm_Fl_add(GEN x, ulong y, ulong p)
     172             : {
     173       14896 :   long l = lg(x), i, j;
     174       14896 :   GEN z = cgetg(l,t_MAT);
     175             : 
     176       14896 :   if (l==1) return z;
     177       14679 :   if (l != lgcols(x)) pari_err_OP( "+", x, utoi(y));
     178       57848 :   for (i=1; i<l; i++)
     179             :   {
     180       43169 :     GEN zi = cgetg(l,t_VECSMALL), xi = gel(x,i);
     181       43169 :     gel(z,i) = zi;
     182       43169 :     for (j=1; j<l; j++) zi[j] = xi[j];
     183       43169 :     zi[i] = Fl_add(zi[i], y, p);
     184             :   }
     185       14679 :   return z;
     186             : }
     187             : GEN
     188         994 : Flm_Fl_sub(GEN x, ulong y, ulong p) { return Flm_Fl_add(x, Fl_neg(y,p), p); }
     189             : 
     190             : GEN
     191        8876 : Flm_add(GEN x, GEN y, ulong p)
     192        8876 : { pari_APPLY_same(Flv_add(gel(x,i), gel(y,i), p)) }
     193             : 
     194             : GEN
     195    10716980 : Flm_sub(GEN x, GEN y, ulong p)
     196    10716980 : { pari_APPLY_same(Flv_sub(gel(x,i), gel(y,i), p)) }
     197             : 
     198             : /********************************************************************/
     199             : /**                                                                **/
     200             : /**                           MULTIPLICATION                       **/
     201             : /**                                                                **/
     202             : /********************************************************************/
     203             : GEN
     204      721887 : FpC_Fp_mul(GEN x, GEN y, GEN p)
     205      721887 : { pari_APPLY_type(t_COL, Fp_mul(gel(x,i), y, p)) }
     206             : 
     207             : GEN
     208      553840 : Flv_Fl_mul(GEN x, ulong y, ulong p)
     209      553840 : { pari_APPLY_ulong(Fl_mul(x[i], y, p)) }
     210             : 
     211             : GEN
     212         854 : Flv_Fl_div(GEN x, ulong y, ulong p)
     213             : {
     214         854 :   return Flv_Fl_mul(x, Fl_inv(y, p), p);
     215             : }
     216             : void
     217           0 : Flv_Fl_div_inplace(GEN x, ulong y, ulong p)
     218             : {
     219           0 :   Flv_Fl_mul_inplace(x, Fl_inv(y, p), p);
     220           0 : }
     221             : GEN
     222      123142 : FpM_Fp_mul(GEN X, GEN c, GEN p) {
     223      123142 :   long i, j, h, l = lg(X);
     224      123142 :   GEN A = cgetg(l, t_MAT);
     225      123142 :   if (l == 1) return A;
     226      123142 :   h = lgcols(X);
     227     1135028 :   for (j=1; j<l; j++)
     228             :   {
     229     1011886 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     230     1011886 :     for (i = 1; i < h; i++) gel(a,i) = Fp_mul(gel(x,i), c, p);
     231     1011886 :     gel(A,j) = a;
     232             :   }
     233      123142 :   return A;
     234             : }
     235             : 
     236             : /* x *= y */
     237             : void
     238     2078981 : Flv_Fl_mul_part_inplace(GEN x, ulong y, ulong p, long l)
     239             : {
     240             :   long i;
     241     2078981 :   if (HIGHWORD(y | p))
     242      335585 :     for(i=1; i<=l; i++) x[i] = Fl_mul(x[i], y, p);
     243             :   else
     244     1743396 :     for(i=1; i<=l; i++) x[i] = (x[i] * y) % p;
     245     2078981 : }
     246             : void
     247           0 : Flv_Fl_mul_inplace(GEN x, ulong y, ulong p)
     248           0 : { Flv_Fl_mul_part_inplace(x, y, p, lg(x)-1); }
     249             : 
     250             : /* set y *= x */
     251             : void
     252           0 : Flm_Fl_mul_inplace(GEN y, ulong x, ulong p)
     253             : {
     254           0 :   long i, j, m, l = lg(y);
     255           0 :   if (l == 1) return;
     256           0 :   m = lgcols(y);
     257           0 :   if (HIGHWORD(x | p))
     258           0 :     for(j=1; j<l; j++)
     259           0 :       for(i=1; i<m; i++) ucoeff(y,i,j) = Fl_mul(ucoeff(y,i,j), x, p);
     260             :   else
     261           0 :     for(j=1; j<l; j++)
     262           0 :       for(i=1; i<m; i++) ucoeff(y,i,j) = (ucoeff(y,i,j) * x) % p;
     263             : }
     264             : 
     265             : /* return x * y */
     266             : static GEN
     267     6864229 : Flm_Fl_mul_pre_i(GEN y, ulong x, ulong p, ulong pi)
     268             : {
     269     6864229 :   long i, j, m, l = lg(y);
     270     6864229 :   GEN z = cgetg(l, t_MAT);
     271     6863620 :   if (l == 1) return z;
     272     6863620 :   m = lgcols(y);
     273    98421506 :   for(j=1; j<l; j++) {
     274    91557930 :     GEN c = cgetg(m, t_VECSMALL); gel(z,j) = c;
     275    91475335 :     for(i=1; i<m; i++) uel(c,i) = Fl_mul_pre(ucoeff(y,i,j), x, p, pi);
     276             :   }
     277     6863576 :   return z;
     278             : }
     279             : 
     280             : /* return x * y */
     281             : static GEN
     282     3095518 : Flm_Fl_mul_OK(GEN y, ulong x, ulong p)
     283             : {
     284     3095518 :   long i, j, m, l = lg(y);
     285     3095518 :   GEN z = cgetg(l, t_MAT);
     286     3095518 :   if (l == 1) return z;
     287     3095518 :   m = lgcols(y);
     288    23227033 :   for(j=1; j<l; j++) {
     289    20131515 :     GEN c = cgetg(m, t_VECSMALL); gel(z,j) = c;
     290    20131515 :     for(i=1; i<m; i++) uel(c,i) = (ucoeff(y,i,j) * x) % p;
     291             :   }
     292     3095518 :   return z;
     293             : }
     294             : 
     295             : /* return x * y */
     296             : GEN
     297     9926085 : Flm_Fl_mul_pre(GEN y, ulong x, ulong p, ulong pi)
     298             : {
     299     9926085 :   if (HIGHWORD(p))
     300     6864265 :     return Flm_Fl_mul_pre_i(y, x, p, pi);
     301             :   else
     302     3061820 :     return Flm_Fl_mul_OK(y, x, p);
     303             : }
     304             : 
     305             : /* return x * y */
     306             : GEN
     307       33698 : Flm_Fl_mul(GEN y, ulong x, ulong p)
     308             : {
     309       33698 :   if (HIGHWORD(p))
     310           0 :     return Flm_Fl_mul_pre_i(y, x, p, get_Fl_red(p));
     311             :   else
     312       33698 :     return Flm_Fl_mul_OK(y, x, p);
     313             : }
     314             : 
     315             : GEN
     316     1675956 : Flv_neg(GEN x, ulong p)
     317     1675956 : { pari_APPLY_ulong(Fl_neg(uel(x,i), p)) }
     318             : 
     319             : void
     320        5764 : Flv_neg_inplace(GEN v, ulong p)
     321             : {
     322             :   long i;
     323      184358 :   for (i = 1; i < lg(v); ++i)
     324      178594 :     v[i] = Fl_neg(v[i], p);
     325        5764 : }
     326             : 
     327             : GEN
     328      121866 : Flm_neg(GEN x, ulong p)
     329      121866 : { pari_APPLY_same(Flv_neg(gel(x,i), p)) }
     330             : 
     331             : /* x[i,]*y. Assume lx > 1 and 0 < i < lgcols(x) */
     332             : static GEN
     333    12728395 : ZMrow_ZC_mul_i(GEN x, GEN y, long lx, long i)
     334             : {
     335    12728395 :   GEN c = mulii(gcoeff(x,i,1), gel(y,1));
     336             :   long k;
     337   195275253 :   for (k = 2; k < lx; k++)
     338             :   {
     339   182546745 :     GEN t = mulii(gcoeff(x,i,k), gel(y,k));
     340   182542460 :     if (signe(t)) c = addii(c, t);
     341             :   }
     342    12728508 :   return c;
     343             : }
     344             : 
     345             : static long
     346    68774335 : zmrow_zc_mul(GEN x, GEN y, long lx, long i)
     347             : {
     348             :   long k;
     349    68774335 :   long c = coeff(x,i,1) * y[1];
     350  1042862800 :   for (k = 2; k < lx; k++)
     351   974088465 :     c += coeff(x,i,k) * y[k];
     352    68774335 :   return c;
     353             : }
     354             : 
     355             : GEN
     356     4606903 : zm_zc_mul(GEN x, GEN y)
     357             : {
     358     4606903 :   long lx = lg(x), l, i;
     359             :   GEN z;
     360     4606903 :   if (lx == 1) return cgetg(1, t_VECSMALL);
     361     4606903 :   l = lg(gel(x,1));
     362     4606903 :   z = cgetg(l,t_VECSMALL);
     363     4606903 :   for (i=1; i<l; i++) z[i] = zmrow_zc_mul(x, y, lx, i);
     364     4606903 :   return z;
     365             : }
     366             : 
     367             : GEN
     368        1771 : zm_mul(GEN x, GEN y)
     369             : {
     370        1771 :   long i,j,lx=lg(x), ly=lg(y);
     371             :   GEN z;
     372        1771 :   if (ly==1) return cgetg(1,t_MAT);
     373        1771 :   z = cgetg(ly,t_MAT);
     374        1771 :   if (lx==1)
     375             :   {
     376           0 :     for (i=1; i<ly; i++) gel(z,i) = cgetg(1,t_VECSMALL);
     377           0 :     return z;
     378             :   }
     379       24402 :   for (j=1; j<ly; j++)
     380       22631 :     gel(z,j) = zm_zc_mul(x, gel(y,j));
     381        1771 :   return z;
     382             : }
     383             : 
     384             : static ulong
     385   355154189 : Flmrow_Flc_mul_SMALL(GEN x, GEN y, ulong p, long lx, long i)
     386             : {
     387   355154189 :   ulong c = ucoeff(x,i,1) * uel(y,1);
     388             :   long k;
     389  5367781587 :   for (k = 2; k < lx; k++) {
     390  5012627398 :     c += ucoeff(x,i,k) * uel(y,k);
     391  5012627398 :     if (c & HIGHBIT) c %= p;
     392             :   }
     393   355154189 :   return c % p;
     394             : }
     395             : 
     396             : static ulong
     397   454926276 : Flmrow_Flc_mul_i(GEN x, GEN y, ulong p, ulong pi, long lx, long i)
     398             : {
     399             :   ulong l0, l1, v1, h0, h1;
     400   454926276 :   long k = 1;
     401             :   LOCAL_OVERFLOW;
     402             :   LOCAL_HIREMAINDER;
     403   454926276 :   l1 = mulll(ucoeff(x,i,k), uel(y,k)); h1 = hiremainder; v1 = 0;
     404  8977660677 :   while (++k < lx) {
     405  8067808125 :     l0 = mulll(ucoeff(x,i,k), uel(y,k)); h0 = hiremainder;
     406  8067808125 :     l1 = addll(l0, l1); h1 = addllx(h0, h1); v1 += overflow;
     407             :   }
     408   454926276 :   if (v1 == 0) return remll_pre(h1, l1, p, pi);
     409    57756119 :   else return remlll_pre(v1, h1, l1, p, pi);
     410             : }
     411             : 
     412             : static GEN
     413      143486 : Flm_Flc_mul_i_2(GEN x, GEN y, long lx, long l)
     414             : {
     415             :   long i,j;
     416      143486 :   GEN z = NULL;
     417             : 
     418     1497489 :   for (j=1; j<lx; j++)
     419             :   {
     420     1354003 :     if (!y[j]) continue;
     421      394276 :     if (!z) z = Flv_copy(gel(x,j));
     422      267622 :     else for (i = 1; i < l; i++) z[i] ^= coeff(x,i,j);
     423             :   }
     424      143486 :   if (!z) z = zero_zv(l-1);
     425      143486 :   return z;
     426             : }
     427             : 
     428             : static GEN
     429     1049279 : FpM_FpC_mul_i(GEN x, GEN y, long lx, long l, GEN p)
     430             : {
     431     1049279 :   GEN z = cgetg(l,t_COL);
     432             :   long i;
     433    12756370 :   for (i = 1; i < l; i++)
     434             :   {
     435    11707098 :     pari_sp av = avma;
     436    11707098 :     GEN c = ZMrow_ZC_mul_i(x, y, lx, i);
     437    11707185 :     gel(z,i) = gerepileuptoint(av, modii(c,p));
     438             :   }
     439     1049272 :   return z;
     440             : }
     441             : 
     442             : static void
     443    38612394 : __Flm_Flc_mul_i_SMALL(GEN z, GEN x, GEN y, long lx, long l, ulong p)
     444             : {
     445             :   long i;
     446    38612394 :   for (i = 1; i < l; i++) z[i] = Flmrow_Flc_mul_SMALL(x, y, p, lx, i);
     447    38627101 : }
     448             : static GEN
     449    35512946 : Flm_Flc_mul_i_SMALL(GEN x, GEN y, long lx, long l, ulong p)
     450             : {
     451    35512946 :   GEN z = cgetg(l,t_VECSMALL);
     452    35512679 :   __Flm_Flc_mul_i_SMALL(z, x, y, lx, l, p);
     453    35512832 :   return z;
     454             : }
     455             : 
     456             : static void
     457    71047103 : __Flm_Flc_mul_i(GEN z, GEN x, GEN y, long lx, long l, ulong p, ulong pi)
     458             : {
     459             :   long i;
     460    71047103 :   for (i = 1; i < l; i++) z[i] = Flmrow_Flc_mul_i(x, y, p, pi, lx, i);
     461    70727981 : }
     462             : static GEN
     463    66101343 : Flm_Flc_mul_i(GEN x, GEN y, long lx, long l, ulong p, ulong pi)
     464             : {
     465    66101343 :   GEN z = cgetg(l,t_VECSMALL);
     466    66258458 :   __Flm_Flc_mul_i(z, x, y, lx, l, p, pi);
     467    66156408 :   return z;
     468             : }
     469             : 
     470             : GEN
     471     2234511 : FpM_mul(GEN x, GEN y, GEN p)
     472             : {
     473     2234511 :   long lx=lg(x), ly=lg(y);
     474             :   GEN z;
     475     2234511 :   pari_sp av = avma;
     476     2234511 :   if (ly==1) return cgetg(1,t_MAT);
     477     2234511 :   if (lx==1) return zeromat(0, ly-1);
     478     2234511 :   if (lgefint(p) == 3)
     479             :   {
     480     1035069 :     ulong pp = uel(p,2);
     481     1035069 :     if (pp == 2)
     482             :     {
     483      100718 :       x = ZM_to_F2m(x);
     484      100718 :       y = ZM_to_F2m(y);
     485      100718 :       z = F2m_to_ZM(F2m_mul(x,y));
     486             :     }
     487             :     else
     488             :     {
     489      934351 :       x = ZM_to_Flm(x, pp);
     490      934351 :       y = ZM_to_Flm(y, pp);
     491      934351 :       z = Flm_to_ZM(Flm_mul(x,y, pp));
     492             :     }
     493             :   } else
     494     1199442 :     z = FpM_red(ZM_mul(x, y), p);
     495     2234511 :   return gerepileupto(av, z);
     496             : }
     497             : 
     498             : static GEN
     499    12410404 : Flm_mul_classical(GEN x, GEN y, long l, long lx, long ly, ulong p, ulong pi)
     500             : {
     501             :   long j;
     502    12410404 :   GEN z = cgetg(ly, t_MAT);
     503    12418323 :   if (SMALL_ULONG(p))
     504    43435990 :     for (j=1; j<ly; j++)
     505    35374287 :       gel(z,j) = Flm_Flc_mul_i_SMALL(x, gel(y,j), lx, l, p);
     506             :   else
     507    70489078 :     for (j=1; j<ly; j++)
     508    66140713 :       gel(z,j) = Flm_Flc_mul_i(x, gel(y,j), lx, l, p, pi);
     509    12410068 :   return z;
     510             : }
     511             : 
     512             : /*
     513             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     514             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     515             : */
     516             : static void
     517       68128 : add_slices_ip(long m, long n,
     518             :            GEN A, long ma, long da, long na, long ea,
     519             :            GEN B, long mb, long db, long nb, long eb,
     520             :            GEN M, long dy, long dx, ulong p)
     521             : {
     522       68128 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     523             :   GEN C;
     524             : 
     525     2815288 :   for (j = 1; j <= min_e; j++) {
     526     2747160 :     C = gel(M, j + dx) + dy;
     527   117793163 :     for (i = 1; i <= min_d; i++)
     528   230092144 :       uel(C, i) = Fl_add(ucoeff(A, ma + i, na + j),
     529   115046072 :                         ucoeff(B, mb + i, nb + j), p);
     530     2775588 :     for (; i <= da; i++)
     531       28497 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     532     2747091 :     for (; i <= db; i++)
     533           0 :       uel(C, i) = ucoeff(B, mb + i, nb + j);
     534     2747091 :     for (; i <= m; i++)
     535           0 :       uel(C, i) = 0;
     536             :   }
     537       69479 :   for (; j <= ea; j++) {
     538        1351 :     C = gel(M, j + dx) + dy;
     539       73175 :     for (i = 1; i <= da; i++)
     540       71824 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     541        1351 :     for (; i <= m; i++)
     542           0 :       uel(C, i) = 0;
     543             :   }
     544       68128 :   for (; j <= eb; j++) {
     545           0 :     C = gel(M, j + dx) + dy;
     546           0 :     for (i = 1; i <= db; i++)
     547           0 :       uel(C, i) = ucoeff(B, mb + i, nb + j);
     548           0 :     for (; i <= m; i++)
     549           0 :       uel(C, i) = 0;
     550             :   }
     551       68128 :   for (; j <= n; j++) {
     552           0 :     C = gel(M, j + dx) + dy;
     553           0 :     for (i = 1; i <= m; i++)
     554           0 :       uel(C, i) = 0;
     555             :   }
     556       68128 : }
     557             : 
     558             : static GEN
     559       34064 : add_slices(long m, long n,
     560             :            GEN A, long ma, long da, long na, long ea,
     561             :            GEN B, long mb, long db, long nb, long eb, ulong p)
     562             : {
     563             :   GEN M;
     564             :   long j;
     565       34064 :   M = cgetg(n + 1, t_MAT);
     566     1416634 :   for (j = 1; j <= n; j++)
     567     1382570 :     gel(M, j) = cgetg(m + 1, t_VECSMALL);
     568       34064 :   add_slices_ip(m, n, A, ma, da, na, ea, B, mb, db, nb, eb, M, 0, 0, p);
     569       34064 :   return M;
     570             : }
     571             : 
     572             : /*
     573             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     574             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     575             : */
     576             : static GEN
     577       59611 : subtract_slices(long m, long n,
     578             :                 GEN A, long ma, long da, long na, long ea,
     579             :                 GEN B, long mb, long db, long nb, long eb, ulong p)
     580             : {
     581       59611 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     582       59610 :   GEN M = cgetg(n + 1, t_MAT), C;
     583             : 
     584     2496440 :   for (j = 1; j <= min_e; j++) {
     585     2436829 :     gel(M, j) = C = cgetg(m + 1, t_VECSMALL);
     586   105871894 :     for (i = 1; i <= min_d; i++)
     587   206870200 :       uel(C, i) = Fl_sub(ucoeff(A, ma + i, na + j),
     588   103435100 :                         ucoeff(B, mb + i, nb + j), p);
     589     2476705 :     for (; i <= da; i++)
     590       39911 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     591     2468568 :     for (; i <= db; i++)
     592       31774 :       uel(C, i) = Fl_neg(ucoeff(B, mb + i, nb + j), p);
     593     2436794 :     for (; i <= m; i++)
     594           0 :       uel(C, i) = 0;
     595             :   }
     596       59611 :   for (; j <= ea; j++) {
     597           0 :     gel(M, j) = C = cgetg(m + 1, t_VECSMALL);
     598           0 :     for (i = 1; i <= da; i++)
     599           0 :       uel(C, i) = ucoeff(A, ma + i, na + j);
     600           0 :     for (; i <= m; i++)
     601           0 :       uel(C, i) = 0;
     602             :   }
     603       60144 :   for (; j <= eb; j++) {
     604         533 :     gel(M, j) = C = cgetg(m + 1, t_VECSMALL);
     605       32270 :     for (i = 1; i <= db; i++)
     606       31737 :       uel(C, i) = Fl_neg(ucoeff(B, mb + i, nb + j), p);
     607         533 :     for (; i <= m; i++)
     608           0 :       uel(C, i) = 0;
     609             :   }
     610       60144 :   for (; j <= n; j++)
     611         533 :     gel(M, j) = zero_Flv(m);
     612       59611 :   return M;
     613             : }
     614             : 
     615             : static GEN Flm_mul_i(GEN x, GEN y, long l, long lx, long ly, ulong p, ulong pi);
     616             : 
     617             : /* Strassen-Winograd matrix product A (m x n) * B (n x p) */
     618             : static GEN
     619        8516 : Flm_mul_sw(GEN A, GEN B, long m, long n, long p, ulong l, ulong li)
     620             : {
     621             :   pari_sp av;
     622        8516 :   long m1 = (m + 1)/2, m2 = m/2,
     623        8516 :     n1 = (n + 1)/2, n2 = n/2,
     624        8516 :     p1 = (p + 1)/2, p2 = p/2;
     625             :   GEN A11, A12, A22, B11, B21, B22,
     626             :     S1, S2, S3, S4, T1, T2, T3, T4,
     627             :     M1, M2, M3, M4, M5, M6, M7,
     628             :     V1, V2, V3, C;
     629             :   long j;
     630        8516 :   C = cgetg(p + 1, t_MAT);
     631      691678 :   for (j = 1; j <= p; j++)
     632      683162 :     gel(C, j) = cgetg(m + 1, t_VECSMALL);
     633        8516 :   av = avma;
     634        8516 :   T2 = subtract_slices(n1, p2, B, 0, n1, p1, p2, B, n1, n2, p1, p2, l);
     635        8515 :   S1 = subtract_slices(m2, n1, A, m1, m2, 0, n1, A, 0, m2, 0, n1, l);
     636        8516 :   M2 = Flm_mul_i(S1, T2, m2 + 1, n1 + 1, p2 + 1, l, li);
     637        8516 :   if (gc_needed(av, 1))
     638           0 :     gerepileall(av, 2, &T2, &M2);  /* destroy S1 */
     639        8516 :   T3 = subtract_slices(n1, p1, T2, 0, n1, 0, p2, B, 0, n1, 0, p1, l);
     640        8516 :   if (gc_needed(av, 1))
     641           0 :     gerepileall(av, 2, &M2, &T3);  /* destroy T2 */
     642        8516 :   S2 = add_slices(m2, n1, A, m1, m2, 0, n1, A, m1, m2, n1, n2, l);
     643        8516 :   T1 = subtract_slices(n1, p1, B, 0, n1, p1, p2, B, 0, n1, 0, p2, l);
     644        8516 :   M3 = Flm_mul_i(S2, T1, m2 + 1, n1 + 1, p2 + 1, l, li);
     645        8516 :   if (gc_needed(av, 1))
     646           0 :     gerepileall(av, 4, &M2, &T3, &S2, &M3);  /* destroy T1 */
     647        8516 :   S3 = subtract_slices(m1, n1, S2, 0, m2, 0, n1, A, 0, m1, 0, n1, l);
     648        8516 :   if (gc_needed(av, 1))
     649           0 :     gerepileall(av, 4, &M2, &T3, &M3, &S3);  /* destroy S2 */
     650        8516 :   A11 = matslice(A, 1, m1, 1, n1);
     651        8516 :   B11 = matslice(B, 1, n1, 1, p1);
     652        8516 :   M1 = Flm_mul_i(A11, B11, m1 + 1, n1 + 1, p1 + 1, l, li);
     653        8516 :   if (gc_needed(av, 1))
     654           0 :     gerepileall(av, 5, &M2, &T3, &M3, &S3, &M1);  /* destroy A11, B11 */
     655        8516 :   A12 = matslice(A, 1, m1, n1 + 1, n);
     656        8516 :   B21 = matslice(B, n1 + 1, n, 1, p1);
     657        8516 :   M4 = Flm_mul_i(A12, B21, m1 + 1, n2 + 1, p1 + 1, l, li);
     658        8516 :   if (gc_needed(av, 1))
     659           0 :     gerepileall(av, 6, &M2, &T3, &M3, &S3, &M1, &M4);  /* destroy A12, B21 */
     660        8516 :   add_slices_ip(m1, p1, M1, 0, m1, 0, p1, M4, 0, m1, 0, p1, C, 0, 0, l);
     661        8516 :   if (gc_needed(av, 1))
     662           0 :     gerepileall(av, 5, &M2, &T3, &M3, &S3, &M1);  /* destroy M4 */
     663        8516 :   M5 = Flm_mul_i(S3, T3, m1 + 1, n1 + 1, p1 + 1, l, li);
     664        8516 :   S4 = subtract_slices(m1, n2, A, 0, m1, n1, n2, S3, 0, m1, 0, n2, l);
     665        8516 :   if (gc_needed(av, 1))
     666           0 :     gerepileall(av, 6, &M2, &T3, &M3, &M1, &M5, &S4);  /* destroy S3 */
     667        8516 :   T4 = add_slices(n2, p1, B, n1, n2, 0, p1, T3, 0, n2, 0, p1, l);
     668        8516 :   if (gc_needed(av, 1))
     669           0 :     gerepileall(av, 6, &M2, &M3, &M1, &M5, &S4, &T4);  /* destroy T3 */
     670        8516 :   V1 = subtract_slices(m1, p1, M1, 0, m1, 0, p1, M5, 0, m1, 0, p1, l);
     671        8516 :   if (gc_needed(av, 1))
     672           1 :     gerepileall(av, 5, &M2, &M3, &S4, &T4, &V1);  /* destroy M1, M5 */
     673        8516 :   B22 = matslice(B, n1 + 1, n, p1 + 1, p);
     674        8516 :   M6 = Flm_mul_i(S4, B22, m1 + 1, n2 + 1, p2 + 1, l, li);
     675        8516 :   if (gc_needed(av, 1))
     676           0 :     gerepileall(av, 5, &M2, &M3, &T4, &V1, &M6);  /* destroy S4, B22 */
     677        8516 :   A22 = matslice(A, m1 + 1, m, n1 + 1, n);
     678        8516 :   M7 = Flm_mul_i(A22, T4, m2 + 1, n2 + 1, p1 + 1, l, li);
     679        8516 :   if (gc_needed(av, 1))
     680           0 :     gerepileall(av, 5, &M2, &M3, &V1, &M6, &M7);  /* destroy A22, T4 */
     681        8516 :   V3 = add_slices(m1, p2, V1, 0, m1, 0, p2, M3, 0, m2, 0, p2, l);
     682        8516 :   add_slices_ip(m1, p2, V3, 0, m1, 0, p2, M6, 0, m1, 0, p2, C, 0, p1, l);
     683        8516 :   if (gc_needed(av, 1))
     684           0 :     gerepileall(av, 4, &M2, &M3, &V1, &M7);  /* destroy V3, M6 */
     685        8516 :   V2 = add_slices(m2, p1, V1, 0, m2, 0, p1, M2, 0, m2, 0, p2, l);
     686        8516 :   if (gc_needed(av, 1))
     687           0 :     gerepileall(av, 3, &M3, &M7, &V2);  /* destroy V1, M2 */
     688        8516 :   add_slices_ip(m2, p1, V2, 0, m2, 0, p1, M7, 0, m2, 0, p1, C, m1, 0, l);
     689        8516 :   add_slices_ip(m2, p2, V2, 0, m2, 0, p2, M3, 0, m2, 0, p2, C, m1, p1, l);
     690        8516 :   set_avma(av); return C;
     691             : }
     692             : 
     693             : /* Strassen-Winograd used for dim >= ZM_sw_bound */
     694             : static GEN
     695    12418800 : Flm_mul_i(GEN x, GEN y, long l, long lx, long ly, ulong p, ulong pi)
     696             : {
     697    12418800 :   ulong e = expu(p);
     698             : #ifdef LONG_IS_64BIT
     699    10409986 :   long ZM_sw_bound = e <= 29 ? 140: e <=62 ? 40: 70;
     700             : #else
     701     2008890 :   long ZM_sw_bound = e <= 12 ? 230: e <=14 ? 170 : e <=17 ? 110: 120;
     702             : #endif
     703    12418876 :   if (l <= ZM_sw_bound || lx <= ZM_sw_bound || ly <= ZM_sw_bound)
     704    12410360 :     return Flm_mul_classical(x, y, l, lx, ly, p, pi);
     705             :   else
     706        8516 :     return Flm_mul_sw(x, y, l - 1, lx - 1, ly - 1, p, pi);
     707             : }
     708             : 
     709             : GEN
     710     5890047 : Flm_mul_pre(GEN x, GEN y, ulong p, ulong pi)
     711             : {
     712     5890047 :   long lx=lg(x), ly=lg(y);
     713     5890047 :   if (ly==1) return cgetg(1,t_MAT);
     714     5890047 :   if (lx==1) return zero_Flm(0, ly-1);
     715     5890047 :   return Flm_mul_i(x, y, lgcols(x), lx, ly, p, pi);
     716             : }
     717             : 
     718             : GEN
     719     6468930 : Flm_mul(GEN x, GEN y, ulong p)
     720             : {
     721     6468930 :   long lx=lg(x), ly=lg(y);
     722     6468930 :   if (ly==1) return cgetg(1,t_MAT);
     723     6468769 :   if (lx==1) return zero_Flm(0, ly-1);
     724     6468769 :   return Flm_mul_i(x, y, lgcols(x), lx, ly, p, get_Fl_red(p));
     725             : }
     726             : 
     727             : struct _Flm
     728             : {
     729             :   ulong p;
     730             :   long n;
     731             : };
     732             : 
     733             : static GEN
     734        8505 : _Flm_mul(void *E , GEN x, GEN y)
     735        8505 : { return Flm_mul(x,y,((struct _Flm*)E)->p); }
     736             : static GEN
     737       43267 : _Flm_sqr(void *E, GEN x)
     738       43267 : { return Flm_mul(x,x,((struct _Flm*)E)->p); }
     739             : static GEN
     740         280 : _Flm_one(void *E)
     741         280 : { return matid_Flm(((struct _Flm*)E)->n); }
     742             : GEN
     743       24472 : Flm_powu(GEN x, ulong n, ulong p)
     744             : {
     745             :   struct _Flm d;
     746       24472 :   if (!n) return matid(lg(x)-1);
     747       24472 :   d.p = p;
     748       24472 :   return gen_powu(x, n, (void*)&d, &_Flm_sqr, &_Flm_mul);
     749             : }
     750             : GEN
     751         280 : Flm_powers(GEN x, ulong n, ulong p)
     752             : {
     753             :   struct _Flm d;
     754         280 :   d.p = p;
     755         280 :   d.n = lg(x)-1;
     756         280 :   return gen_powers(x, n, 1, (void*)&d,
     757             :                     &_Flm_sqr, &_Flm_mul, &_Flm_one);
     758             : }
     759             : 
     760             : static GEN
     761           0 : _FpM_mul(void *p , GEN x, GEN y)
     762           0 : { return FpM_mul(x,y,(GEN)p); }
     763             : static GEN
     764           0 : _FpM_sqr(void *p, GEN x)
     765           0 : { return FpM_mul(x,x,(GEN)p); }
     766             : GEN
     767           0 : FpM_powu(GEN x, ulong n, GEN p)
     768             : {
     769           0 :   if (!n) return matid(lg(x)-1);
     770           0 :   if (lgefint(p) == 3)
     771             :   {
     772           0 :     pari_sp av = avma;
     773           0 :     ulong pp = uel(p,2);
     774             :     GEN z;
     775           0 :     if (pp == 2)
     776           0 :       z = F2m_to_ZM(F2m_powu(ZM_to_F2m(x),n));
     777             :     else
     778           0 :       z = Flm_to_ZM(Flm_powu(ZM_to_Flm(x, pp), n, pp));
     779           0 :     return gerepileupto(av, z);
     780             :   }
     781           0 :   return gen_powu(x, n, (void*)p, &_FpM_sqr, &_FpM_mul);
     782             : }
     783             : 
     784             : /*Multiple a column vector by a line vector to make a matrix*/
     785             : GEN
     786          14 : Flc_Flv_mul(GEN x, GEN y, ulong p)
     787             : {
     788          14 :   long i,j, lx=lg(x), ly=lg(y);
     789             :   GEN z;
     790          14 :   if (ly==1) return cgetg(1,t_MAT);
     791          14 :   z = cgetg(ly, t_MAT);
     792          49 :   for (j=1; j < ly; j++)
     793             :   {
     794          35 :     GEN zj = cgetg(lx,t_VECSMALL);
     795         112 :     for (i=1; i<lx; i++)
     796          77 :       uel(zj,i) = Fl_mul(uel(x,i), uel(y,j), p);
     797          35 :     gel(z,j) = zj;
     798             :   }
     799          14 :   return z;
     800             : }
     801             : 
     802             : /*Multiple a column vector by a line vector to make a matrix*/
     803             : GEN
     804        2373 : FpC_FpV_mul(GEN x, GEN y, GEN p)
     805             : {
     806        2373 :   long i,j, lx=lg(x), ly=lg(y);
     807             :   GEN z;
     808        2373 :   if (ly==1) return cgetg(1,t_MAT);
     809        2373 :   z = cgetg(ly,t_MAT);
     810       34783 :   for (j=1; j < ly; j++)
     811             :   {
     812       32410 :     GEN zj = cgetg(lx,t_COL);
     813       32410 :     for (i=1; i<lx; i++) gel(zj,i) = Fp_mul(gel(x,i),gel(y,j), p);
     814       32410 :     gel(z, j) = zj;
     815             :   }
     816        2373 :   return z;
     817             : }
     818             : 
     819             : /* Multiply a line vector by a column and return a scalar (t_INT) */
     820             : GEN
     821     2009348 : FpV_dotproduct(GEN x, GEN y, GEN p)
     822             : {
     823     2009348 :   long i, lx = lg(x);
     824             :   pari_sp av;
     825             :   GEN c;
     826     2009348 :   if (lx == 1) return gen_0;
     827     2008403 :   av = avma; c = mulii(gel(x,1),gel(y,1));
     828     2008348 :   for (i=2; i<lx; i++) c = addii(c, mulii(gel(x,i),gel(y,i)));
     829     2008330 :   return gerepileuptoint(av, modii(c,p));
     830             : }
     831             : GEN
     832           0 : FpV_dotsquare(GEN x, GEN p)
     833             : {
     834           0 :   long i, lx = lg(x);
     835             :   pari_sp av;
     836             :   GEN c;
     837           0 :   if (lx == 1) return gen_0;
     838           0 :   av = avma; c = sqri(gel(x,1));
     839           0 :   for (i=2; i<lx; i++) c = addii(c, sqri(gel(x,i)));
     840           0 :   return gerepileuptoint(av, modii(c,p));
     841             : }
     842             : 
     843             : INLINE ulong
     844     3876824 : Flv_dotproductspec_SMALL(GEN x, GEN y, ulong p, long lx)
     845             : {
     846     3876824 :   ulong c = uel(x,0) * uel(y,0);
     847             :   long k;
     848    39725413 :   for (k = 1; k < lx; k++) {
     849    35848589 :     c += uel(x,k) * uel(y,k);
     850    35848589 :     if (c & HIGHBIT) c %= p;
     851             :   }
     852     3876824 :   return c % p;
     853             : }
     854             : 
     855             : INLINE ulong
     856      697228 : Flv_dotproductspec_i(GEN x, GEN y, ulong p, ulong pi, long lx)
     857             : {
     858             :   ulong l0, l1, v1, h0, h1;
     859      697228 :   long i = 0;
     860             :   LOCAL_OVERFLOW;
     861             :   LOCAL_HIREMAINDER;
     862      697228 :   l1 = mulll(uel(x,i), uel(y,i)); h1 = hiremainder; v1 = 0;
     863    15327042 :   while (++i < lx) {
     864    13932586 :     l0 = mulll(uel(x,i), uel(y,i)); h0 = hiremainder;
     865    13932586 :     l1 = addll(l0, l1); h1 = addllx(h0, h1); v1 += overflow;
     866             :   }
     867      697228 :   if (v1 == 0) return remll_pre(h1, l1, p, pi);
     868      432952 :   else return remlll_pre(v1, h1, l1, p, pi);
     869             : }
     870             : 
     871             : ulong
     872      320971 : Flv_dotproduct(GEN x, GEN y, ulong p)
     873             : {
     874      320971 :   long lx = lg(x)-1;
     875      320971 :   if (lx == 0) return 0;
     876      320971 :   if (SMALL_ULONG(p))
     877      320971 :     return Flv_dotproductspec_SMALL(x+1, y+1, p, lx);
     878             :   else
     879           0 :     return Flv_dotproductspec_i(x+1, y+1, p, get_Fl_red(p), lx);
     880             : }
     881             : 
     882             : ulong
     883       57183 : Flv_dotproduct_pre(GEN x, GEN y, ulong p, ulong pi)
     884             : {
     885       57183 :   long lx = lg(x)-1;
     886       57183 :   if (lx == 0) return 0;
     887       57183 :   if (SMALL_ULONG(p))
     888       51691 :     return Flv_dotproductspec_SMALL(x+1, y+1, p, lx);
     889             :   else
     890        5492 :     return Flv_dotproductspec_i(x+1, y+1, p, pi, lx);
     891             : }
     892             : 
     893             : ulong
     894     4220760 : Flx_dotproduct(GEN x, GEN y, ulong p)
     895             : {
     896     4220760 :   long lx = minss(lgpol(x), lgpol(y));
     897     4220783 :   if (lx == 0) return 0;
     898     4195713 :   if (SMALL_ULONG(p))
     899     3503977 :     return Flv_dotproductspec_SMALL(x+2, y+2, p, lx);
     900             :   else
     901      691736 :     return Flv_dotproductspec_i(x+2, y+2, p, get_Fl_red(p), lx);
     902             : }
     903             : 
     904             : GEN
     905     1049280 : FpM_FpC_mul(GEN x, GEN y, GEN p)
     906             : {
     907     1049280 :   long lx = lg(x);
     908     1049280 :   return lx==1? cgetg(1,t_COL): FpM_FpC_mul_i(x, y, lx, lgcols(x), p);
     909             : }
     910             : GEN
     911      285521 : Flm_Flc_mul(GEN x, GEN y, ulong p)
     912             : {
     913      285521 :   long l, lx = lg(x);
     914      285521 :   if (lx==1) return cgetg(1,t_VECSMALL);
     915      285521 :   l = lgcols(x);
     916      285521 :   if (p==2)
     917      143486 :     return Flm_Flc_mul_i_2(x, y, lx, l);
     918      142035 :   else if (SMALL_ULONG(p))
     919      138647 :     return Flm_Flc_mul_i_SMALL(x, y, lx, l, p);
     920             :   else
     921        3388 :     return Flm_Flc_mul_i(x, y, lx, l, p, get_Fl_red(p));
     922             : }
     923             : 
     924             : GEN
     925        4816 : Flm_Flc_mul_pre(GEN x, GEN y, ulong p, ulong pi)
     926             : {
     927        4816 :   long l, lx = lg(x);
     928             :   GEN z;
     929        4816 :   if (lx==1) return cgetg(1,t_VECSMALL);
     930        4816 :   l = lgcols(x);
     931        4816 :   z = cgetg(l, t_VECSMALL);
     932        4816 :   if (SMALL_ULONG(p))
     933        4128 :     __Flm_Flc_mul_i_SMALL(z, x, y, lx, l, p);
     934             :   else
     935         688 :     __Flm_Flc_mul_i(z, x, y, lx, l, p, pi);
     936        4815 :   return z;
     937             : }
     938             : 
     939             : GEN
     940     7655251 : Flm_Flc_mul_pre_Flx(GEN x, GEN y, ulong p, ulong pi, long sv)
     941             : {
     942     7655251 :   long l, lx = lg(x);
     943             :   GEN z;
     944     7655251 :   if (lx==1) return pol0_Flx(sv);
     945     7655251 :   l = lgcols(x);
     946     7652478 :   z = cgetg(l + 1, t_VECSMALL); z[1] = sv;
     947     7660552 :   if (SMALL_ULONG(p))
     948     3101461 :     __Flm_Flc_mul_i_SMALL(z + 1, x, y, lx, l, p);
     949             :   else
     950     4559091 :     __Flm_Flc_mul_i(z + 1, x, y, lx, l, p, pi);
     951     7686765 :   return Flx_renormalize(z, l + 1);
     952             : }
     953             : 
     954             : /* RgV_to_RgX(FpM_FpC_mul(x,y,p), v), p != NULL, memory clean */
     955             : GEN
     956      355839 : FpM_FpC_mul_FpX(GEN x, GEN y, GEN p, long v)
     957             : {
     958      355839 :   long i, l, lx = lg(x);
     959             :   GEN z;
     960      355839 :   if (lx==1) return pol_0(v);
     961      355839 :   l = lgcols(x);
     962      355839 :   z = new_chunk(l+1);
     963      760524 :   for (i=l-1; i; i--)
     964             :   {
     965      605677 :     pari_sp av = avma;
     966      605677 :     GEN p1 = ZMrow_ZC_mul_i(x,y,lx,i);
     967      605677 :     p1 = modii(p1, p);
     968      605677 :     if (signe(p1))
     969             :     {
     970      200992 :       if (i != l-1) stackdummy((pari_sp)(z + l+1), (pari_sp)(z + i+2));
     971      200992 :       gel(z,i+1) = gerepileuptoint(av, p1);
     972      200992 :       break;
     973             :     }
     974      404685 :     set_avma(av);
     975             :   }
     976      355839 :   if (!i) { set_avma((pari_sp)(z + l+1)); return pol_0(v); }
     977      200992 :   z[0] = evaltyp(t_POL) | evallg(i+2);
     978      200992 :   z[1] = evalsigne(1) | evalvarn(v);
     979      616632 :   for (; i; i--)
     980             :   {
     981      415640 :     pari_sp av = avma;
     982      415640 :     GEN p1 = ZMrow_ZC_mul_i(x,y,lx,i);
     983      415640 :     gel(z,i+1) = gerepileuptoint(av, modii(p1,p));
     984             :   }
     985      200992 :   return z;
     986             : }
     987             : 
     988             : /********************************************************************/
     989             : /**                                                                **/
     990             : /**                           TRANSPOSITION                        **/
     991             : /**                                                                **/
     992             : /********************************************************************/
     993             : 
     994             : /* == zm_transpose */
     995             : GEN
     996      270774 : Flm_transpose(GEN x)
     997             : {
     998      270774 :   long i, dx, lx = lg(x);
     999             :   GEN y;
    1000      270774 :   if (lx == 1) return cgetg(1,t_MAT);
    1001      270557 :   dx = lgcols(x); y = cgetg(dx,t_MAT);
    1002      270557 :   for (i=1; i<dx; i++) gel(y,i) = Flm_row(x,i);
    1003      270558 :   return y;
    1004             : }
    1005             : 
    1006             : /********************************************************************/
    1007             : /**                                                                **/
    1008             : /**                           SCALAR MATRICES                      **/
    1009             : /**                                                                **/
    1010             : /********************************************************************/
    1011             : 
    1012             : GEN
    1013        3774 : gen_matid(long n, void *E, const struct bb_field *S)
    1014             : {
    1015        3774 :   GEN y = cgetg(n+1,t_MAT), _0, _1;
    1016             :   long i;
    1017        3774 :   if (n < 0) pari_err_DOMAIN("gen_matid", "dimension","<",gen_0,stoi(n));
    1018        3774 :   _0 = S->s(E,0);
    1019        3774 :   _1 = S->s(E,1);
    1020       16773 :   for (i=1; i<=n; i++)
    1021             :   {
    1022       12999 :     GEN z = const_col(n, _0); gel(z,i) = _1;
    1023       12999 :     gel(y, i) = z;
    1024             :   }
    1025        3774 :   return y;
    1026             : }
    1027             : 
    1028             : GEN
    1029          35 : matid_F2xqM(long n, GEN T)
    1030             : {
    1031             :   void *E;
    1032          35 :   const struct bb_field *S = get_F2xq_field(&E, T);
    1033          35 :   return gen_matid(n, E, S);
    1034             : }
    1035             : GEN
    1036          56 : matid_FlxqM(long n, GEN T, ulong p)
    1037             : {
    1038             :   void *E;
    1039          56 :   const struct bb_field *S = get_Flxq_field(&E, T, p);
    1040          56 :   return gen_matid(n, E, S);
    1041             : }
    1042             : 
    1043             : GEN
    1044      870511 : matid_Flm(long n)
    1045             : {
    1046      870511 :   GEN y = cgetg(n+1,t_MAT);
    1047             :   long i;
    1048      870511 :   if (n < 0) pari_err_DOMAIN("matid_Flm", "dimension","<",gen_0,stoi(n));
    1049      870499 :   for (i=1; i<=n; i++) { gel(y,i) = zero_zv(n); ucoeff(y, i,i) = 1; }
    1050      870511 :   return y;
    1051             : }
    1052             : 
    1053             : GEN
    1054          42 : scalar_Flm(long s, long n)
    1055             : {
    1056             :   long i;
    1057          42 :   GEN y = cgetg(n+1,t_MAT);
    1058          42 :   for (i=1; i<=n; i++) { gel(y,i) = zero_Flv(n); coeff(y, i,i) = s; }
    1059          42 :   return y;
    1060             : }
    1061             : 
    1062             : /********************************************************************/
    1063             : /**                                                                **/
    1064             : /**                           CONVERSIONS                          **/
    1065             : /**                                                                **/
    1066             : /********************************************************************/
    1067             : GEN
    1068    23180400 : ZV_to_Flv(GEN x, ulong p)
    1069    23180400 : { pari_APPLY_ulong(umodiu(gel(x,i), p)) }
    1070             : 
    1071             : GEN
    1072     3729716 : ZM_to_Flm(GEN x, ulong p)
    1073     3729716 : { pari_APPLY_same(ZV_to_Flv(gel(x,i), p)) }
    1074             : 
    1075             : GEN
    1076        2324 : ZMV_to_FlmV(GEN x, ulong m)
    1077        2324 : { pari_APPLY_type(t_VEC,ZM_to_Flm(gel(x,i), m)) }
    1078             : 
    1079             : /*                          TO INTMOD                        */
    1080             : static GEN
    1081    12014621 : to_intmod(GEN x, GEN p) { retmkintmod(modii(x, p), p); }
    1082             : static GEN
    1083       19376 : Fl_to_intmod(ulong x, GEN p) { retmkintmod(utoi(x), p); }
    1084             : 
    1085             : GEN
    1086      302399 : Fp_to_mod(GEN z, GEN p)
    1087             : {
    1088      302399 :   retmkintmod(modii(z, p), icopy(p));
    1089             : }
    1090             : 
    1091             : /* z in Z[X], return z * Mod(1,p), normalized*/
    1092             : GEN
    1093     1855754 : FpX_to_mod(GEN z, GEN p)
    1094             : {
    1095     1855754 :   long i,l = lg(z);
    1096             :   GEN x;
    1097     1855754 :   if (l == 2)
    1098             :   {
    1099      525875 :     x = cgetg(3,t_POL); x[1] = z[1];
    1100      525875 :     gel(x,2) = mkintmod(gen_0,icopy(p)); return x;
    1101             :   }
    1102     1329879 :   x = cgetg(l,t_POL);
    1103     1329879 :   if (l >2) p = icopy(p);
    1104     1329879 :   for (i=2; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1105     1329879 :   x[1] = z[1]; return normalizepol_lg(x,l);
    1106             : }
    1107             : 
    1108             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1109             : GEN
    1110        6587 : FpV_to_mod(GEN z, GEN p)
    1111             : {
    1112        6587 :   long i,l = lg(z);
    1113        6587 :   GEN x = cgetg(l, t_VEC);
    1114        6587 :   if (l == 1) return x;
    1115        6587 :   p = icopy(p);
    1116        6587 :   for (i=1; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1117        6587 :   return x;
    1118             : }
    1119             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1120             : GEN
    1121         105 : FpC_to_mod(GEN z, GEN p)
    1122             : {
    1123         105 :   long i, l = lg(z);
    1124         105 :   GEN x = cgetg(l, t_COL);
    1125         105 :   if (l == 1) return x;
    1126          91 :   p = icopy(p);
    1127          91 :   for (i=1; i<l; i++) gel(x,i) = to_intmod(gel(z,i), p);
    1128          91 :   return x;
    1129             : }
    1130             : /* z in Mat m,n(Z), return z * Mod(1,p), normalized*/
    1131             : GEN
    1132       56392 : FpM_to_mod(GEN z, GEN p)
    1133             : {
    1134       56392 :   long i, j, m, l = lg(z);
    1135       56392 :   GEN  x = cgetg(l,t_MAT), y, zi;
    1136       56392 :   if (l == 1) return x;
    1137       56371 :   m = lgcols(z);
    1138       56371 :   p = icopy(p);
    1139      181986 :   for (i=1; i<l; i++)
    1140             :   {
    1141      125615 :     gel(x,i) = cgetg(m,t_COL);
    1142      125615 :     y = gel(x,i); zi= gel(z,i);
    1143      125615 :     for (j=1; j<m; j++) gel(y,j) = to_intmod(gel(zi,j), p);
    1144             :   }
    1145       56371 :   return x;
    1146             : }
    1147             : GEN
    1148          28 : Flc_to_mod(GEN z, ulong pp)
    1149             : {
    1150          28 :   long i, l = lg(z);
    1151          28 :   GEN p, x = cgetg(l, t_COL);
    1152          28 :   if (l == 1) return x;
    1153          28 :   p = utoipos(pp);
    1154          28 :   for (i=1; i<l; i++) gel(x,i) = Fl_to_intmod(z[i], p);
    1155          28 :   return x;
    1156             : }
    1157             : GEN
    1158         189 : Flm_to_mod(GEN z, ulong pp)
    1159             : {
    1160         189 :   long i, j, m, l = lg(z);
    1161         189 :   GEN p, x = cgetg(l,t_MAT), y, zi;
    1162         189 :   if (l == 1) return x;
    1163         161 :   m = lgcols(z);
    1164         161 :   p = utoipos(pp);
    1165        1414 :   for (i=1; i<l; i++)
    1166             :   {
    1167        1253 :     gel(x,i) = cgetg(m,t_COL);
    1168        1253 :     y = gel(x,i); zi= gel(z,i);
    1169        1253 :     for (j=1; j<m; j++) gel(y,j) = Fl_to_intmod(zi[j], p);
    1170             :   }
    1171         161 :   return x;
    1172             : }
    1173             : 
    1174             : GEN
    1175         574 : FpVV_to_mod(GEN z, GEN p)
    1176             : {
    1177         574 :   long i, j, m, l = lg(z);
    1178         574 :   GEN  x = cgetg(l,t_VEC), y, zi;
    1179         574 :   if (l == 1) return x;
    1180         574 :   m = lgcols(z);
    1181         574 :   p = icopy(p);
    1182        1246 :   for (i=1; i<l; i++)
    1183             :   {
    1184         672 :     gel(x,i) = cgetg(m,t_VEC);
    1185         672 :     y = gel(x,i); zi= gel(z,i);
    1186         672 :     for (j=1; j<m; j++) gel(y,j) = to_intmod(gel(zi,j), p);
    1187             :   }
    1188         574 :   return x;
    1189             : }
    1190             : 
    1191             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1192             : GEN
    1193           7 : FpXQC_to_mod(GEN z, GEN T, GEN p)
    1194             : {
    1195           7 :   long i,l = lg(z);
    1196           7 :   GEN x = cgetg(l, t_COL);
    1197           7 :   if (l == 1) return x;
    1198           7 :   T = FpX_to_mod(T, p);
    1199          21 :   for (i=1; i<l; i++)
    1200          14 :     gel(x,i) = mkpolmod(FpX_to_mod(gel(z,i), p), T);
    1201           7 :   return x;
    1202             : }
    1203             : 
    1204             : static GEN
    1205      584675 : Fq_to_mod_raw(GEN x, GEN T, GEN p)
    1206             : {
    1207      584675 :   GEN z = typ(x)==t_INT ? Fp_to_mod(x, p): FpX_to_mod(x, p);
    1208      584675 :   return mkpolmod(z, T);
    1209             : }
    1210             : 
    1211             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1212             : GEN
    1213          14 : FqC_to_mod(GEN z, GEN T, GEN p)
    1214             : {
    1215             :   GEN x;
    1216          14 :   long i,l = lg(z);
    1217          14 :   if (!T) return FpC_to_mod(z, p);
    1218          14 :   x = cgetg(l, t_COL);
    1219          14 :   if (l == 1) return x;
    1220          14 :   T = FpX_to_mod(T, p);
    1221         252 :   for (i=1; i<l; i++)
    1222         238 :     gel(x,i) = Fq_to_mod_raw(gel(z, i), T, p);
    1223          14 :   return x;
    1224             : }
    1225             : 
    1226             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1227             : static GEN
    1228      108115 : FqC_to_mod_raw(GEN x, GEN T, GEN p)
    1229      108115 : { pari_APPLY_type(t_COL, Fq_to_mod_raw(gel(x,i), T, p)) }
    1230             : 
    1231             : /* z in Z^n, return z * Mod(1,p), normalized*/
    1232             : GEN
    1233       26159 : FqM_to_mod(GEN z, GEN T, GEN p)
    1234             : {
    1235             :   GEN x;
    1236       26159 :   long i,l = lg(z);
    1237       26159 :   if (!T) return FpM_to_mod(z, p);
    1238       26159 :   x = cgetg(l, t_MAT);
    1239       26159 :   if (l == 1) return x;
    1240       26131 :   T = FpX_to_mod(T, p);
    1241      134246 :   for (i=1; i<l; i++)
    1242      108115 :     gel(x,i) = FqC_to_mod_raw(gel(z, i), T, p);
    1243       26131 :   return x;
    1244             : }
    1245             : 
    1246             : /********************************************************************/
    1247             : /*                                                                  */
    1248             : /*                     Blackbox linear algebra                      */
    1249             : /*                                                                  */
    1250             : /********************************************************************/
    1251             : 
    1252             : /* A sparse column (zCs) v is a t_COL with two components C and E which are
    1253             :  * t_VECSMALL of the same length, representing sum_i E[i]*e_{C[i]}, where
    1254             :  * (e_j) is the canonical basis.
    1255             :  * A sparse matrix (zMs) is a t_VEC of zCs */
    1256             : 
    1257             : /* FpCs and FpMs are identical but E[i] is interpreted as a _signed_ C long
    1258             :  * integer representing an element of Fp. This is important since p can be
    1259             :  * large and p+E[i] would not fit in a C long.  */
    1260             : 
    1261             : /* RgCs and RgMs are similar, except that the type of the component is
    1262             :  * unspecified. Functions handling RgCs/RgMs must be independent of the type
    1263             :  * of E. */
    1264             : 
    1265             : /* Most functions take an argument nbrow which is the number of lines of the
    1266             :  * column/matrix, which cannot be derived from the data. */
    1267             : 
    1268             : GEN
    1269           0 : zCs_to_ZC(GEN R, long nbrow)
    1270             : {
    1271           0 :   GEN C = gel(R,1), E = gel(R,2), c = zerocol(nbrow);
    1272           0 :   long j, l = lg(C);
    1273           0 :   for (j = 1; j < l; ++j) gel(c, C[j]) = stoi(E[j]);
    1274           0 :   return c;
    1275             : }
    1276             : 
    1277             : GEN
    1278           0 : zMs_to_ZM(GEN x, long nbrow)
    1279           0 : { pari_APPLY_same(zCs_to_ZC(gel(x, i), nbrow)) }
    1280             : 
    1281             : /* Solve equation f(X) = B (mod p) where B is a FpV, and f is an endomorphism.
    1282             :  * Return either a solution as a t_COL, or a kernel vector as a t_VEC. */
    1283             : GEN
    1284         126 : gen_FpM_Wiedemann(void *E, GEN (*f)(void*, GEN), GEN B, GEN p)
    1285             : {
    1286         126 :   pari_sp ltop = avma;
    1287         126 :   long col = 0, n = lg(B)-1, m = 2*n+1;
    1288         126 :   if (ZV_equal0(B)) return zerocol(n);
    1289         252 :   while (++col <= n)
    1290             :   {
    1291         126 :     pari_sp btop = avma, av;
    1292             :     long i, lQ;
    1293         126 :     GEN V, Q, M, W = B;
    1294         126 :     GEN b = cgetg(m+2, t_POL);
    1295         126 :     b[1] = evalsigne(1)|evalvarn(0);
    1296         126 :     gel(b, 2) = gel(W, col);
    1297       51522 :     for (i = 3; i<m+2; i++)
    1298       51396 :       gel(b, i) = cgeti(lgefint(p));
    1299         126 :     av = avma;
    1300       51522 :     for (i = 3; i<m+2; i++)
    1301             :     {
    1302       51396 :       W = f(E, W);
    1303       51396 :       affii(gel(W, col),gel(b, i));
    1304       51396 :       if (gc_needed(av,1))
    1305             :       {
    1306        2000 :         if (DEBUGMEM>1) pari_warn(warnmem,"Wiedemann: first loop, %ld",i);
    1307        2000 :         W = gerepileupto(av, W);
    1308             :       }
    1309             :     }
    1310         126 :     b = FpX_renormalize(b, m+2);
    1311         126 :     if (lgpol(b)==0) {set_avma(btop); continue; }
    1312         126 :     M = FpX_halfgcd(b, pol_xn(m, 0), p);
    1313         126 :     Q = FpX_neg(FpX_normalize(gcoeff(M, 2, 1),p),p);
    1314         126 :     W = B; lQ =lg(Q);
    1315         126 :     if (DEBUGLEVEL) err_printf("Wiedemann: deg. minpoly: %ld\n",lQ-3);
    1316         126 :     V = FpC_Fp_mul(W, gel(Q, lQ-2), p);
    1317         126 :     av = avma;
    1318       25278 :     for (i = lQ-3; i > 1; i--)
    1319             :     {
    1320       25152 :       W = f(E, W);
    1321       25152 :       V = ZC_lincomb(gen_1, gel(Q,i), V, W);
    1322       25152 :       if (gc_needed(av,1))
    1323             :       {
    1324        1583 :         if (DEBUGMEM>1) pari_warn(warnmem,"Wiedemann: second loop, %ld",i);
    1325        1583 :         gerepileall(av, 2, &V, &W);
    1326             :       }
    1327             :     }
    1328         126 :     V = FpC_red(V, p);
    1329         126 :     W = FpC_sub(f(E,V), B, p);
    1330         252 :     if (ZV_equal0(W)) return gerepilecopy(ltop, V);
    1331           0 :     av = avma;
    1332           0 :     for (i = 1; i <= n; ++i)
    1333             :     {
    1334           0 :       V = W;
    1335           0 :       W = f(E, V);
    1336           0 :       if (ZV_equal0(W))
    1337           0 :         return gerepilecopy(ltop, shallowtrans(V));
    1338           0 :       gerepileall(av, 2, &V, &W);
    1339             :     }
    1340           0 :     set_avma(btop);
    1341             :   }
    1342           0 :   return NULL;
    1343             : }
    1344             : 
    1345             : GEN
    1346           0 : zMs_ZC_mul(GEN M, GEN B)
    1347             : {
    1348             :   long i, j;
    1349           0 :   long n = lg(B)-1;
    1350           0 :   GEN V = zerocol(n);
    1351           0 :   for (i = 1; i <= n; ++i)
    1352           0 :     if (signe(gel(B, i)))
    1353             :     {
    1354           0 :       GEN R = gel(M, i), C = gel(R, 1), E = gel(R, 2);
    1355           0 :       long l = lg(C);
    1356           0 :       for (j = 1; j < l; ++j)
    1357             :       {
    1358           0 :         long k = C[j];
    1359           0 :         switch(E[j])
    1360             :         {
    1361             :         case 1:
    1362           0 :           gel(V, k) = gel(V,k)==gen_0 ? gel(B,i) : addii(gel(V, k), gel(B,i));
    1363           0 :           break;
    1364             :         case -1:
    1365           0 :           gel(V, k) = gel(V,k)==gen_0 ? negi(gel(B,i)) : subii(gel(V, k), gel(B,i));
    1366           0 :           break;
    1367             :         default:
    1368           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]));
    1369           0 :           break;
    1370             :         }
    1371             :       }
    1372             :     }
    1373           0 :   return V;
    1374             : }
    1375             : 
    1376             : GEN
    1377           0 : FpMs_FpC_mul(GEN M, GEN B, GEN p) { return FpC_red(zMs_ZC_mul(M, B), p); }
    1378             : 
    1379             : GEN
    1380       76800 : ZV_zMs_mul(GEN B, GEN M)
    1381             : {
    1382             :   long i, j;
    1383       76800 :   long m = lg(M)-1;
    1384       76800 :   GEN V = cgetg(m+1,t_VEC);
    1385    39360196 :   for (i = 1; i <= m; ++i)
    1386             :   {
    1387    39283396 :     GEN R = gel(M, i), C = gel(R, 1), E = gel(R, 2);
    1388    39283396 :     long l = lg(C);
    1389    39283396 :     GEN z = mulis(gel(B, C[1]), E[1]);
    1390   330263217 :     for (j = 2; j < l; ++j)
    1391             :     {
    1392   290979821 :       long k = C[j];
    1393   290979821 :       switch(E[j])
    1394             :       {
    1395             :       case 1:
    1396   196103761 :         z = addii(z, gel(B,k));
    1397   196103761 :         break;
    1398             :       case -1:
    1399    79337676 :         z = subii(z, gel(B,k));
    1400    79337676 :         break;
    1401             :       default:
    1402    15538384 :         z = addii(z, mulis(gel(B,k), E[j]));
    1403    15538384 :         break;
    1404             :       }
    1405             :     }
    1406    39283396 :     gel(V,i) = z;
    1407             :   }
    1408       76800 :   return V;
    1409             : }
    1410             : 
    1411             : GEN
    1412         126 : FpV_FpMs_mul(GEN B, GEN M, GEN p) { return FpV_red(ZV_zMs_mul(B, M), p); }
    1413             : 
    1414             : GEN
    1415           0 : ZlM_gauss(GEN a, GEN b, ulong p, long e, GEN C)
    1416             : {
    1417           0 :   pari_sp av = avma, av2;
    1418           0 :   GEN xi, xb, pi = gen_1, P;
    1419             :   long i;
    1420           0 :   if (!C) {
    1421           0 :     C = Flm_inv(ZM_to_Flm(a, p), p);
    1422           0 :     if (!C) pari_err_INV("ZlM_gauss", a);
    1423             :   }
    1424           0 :   P = utoipos(p);
    1425           0 :   av2 = avma;
    1426           0 :   xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    1427           0 :   xb = Flm_to_ZM(xi);
    1428           0 :   for (i = 2; i <= e; i++)
    1429             :   {
    1430           0 :     pi = muliu(pi, p); /* = p^(i-1) */
    1431           0 :     b = ZM_Z_divexact(ZM_sub(b, ZM_nm_mul(a, xi)), P);
    1432           0 :     if (gc_needed(av,2))
    1433             :     {
    1434           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZlM_gauss. i=%ld/%ld",i,e);
    1435           0 :       gerepileall(av2,3, &pi,&b,&xb);
    1436             :     }
    1437           0 :     xi = Flm_mul(C, ZM_to_Flm(b, p), p);
    1438           0 :     xb = ZM_add(xb, nm_Z_mul(xi, pi));
    1439             :   }
    1440           0 :   return gerepileupto(av, xb);
    1441             : }
    1442             : 
    1443             : struct wrapper_modp_s {
    1444             :   GEN (*f)(void*, GEN);
    1445             :   void *E;
    1446             :   GEN p;
    1447             : };
    1448             : 
    1449             : /* compute f(x) mod p */
    1450             : static GEN
    1451       76674 : wrap_relcomb_modp(void *E, GEN x)
    1452             : {
    1453       76674 :   struct wrapper_modp_s *W = (struct wrapper_modp_s*)E;
    1454       76674 :   return FpC_red(W->f(W->E, x), W->p);
    1455             : }
    1456             : static GEN
    1457           0 : wrap_relcomb(void*E, GEN x) { return zMs_ZC_mul((GEN)E, x); }
    1458             : 
    1459             : static GEN
    1460       76674 : wrap_relker(void*E, GEN x) { return ZV_zMs_mul(x, (GEN)E); }
    1461             : 
    1462             : /* Solve f(X) = B (mod p^e); blackbox version of ZlM_gauss */
    1463             : GEN
    1464           0 : gen_ZpM_Dixon_Wiedemann(void *E, GEN (*f)(void*, GEN), GEN B, GEN p, long e)
    1465             : {
    1466             :   struct wrapper_modp_s W;
    1467           0 :   pari_sp av = avma;
    1468           0 :   GEN xb, xi, pi = gen_1;
    1469             :   long i;
    1470           0 :   W.E = E;
    1471           0 :   W.f = f;
    1472           0 :   W.p = p;
    1473           0 :   xi = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, FpC_red(B, p), p); /* f^(-1) B */
    1474           0 :   if (!xi || e == 1 || typ(xi) == t_VEC) return xi;
    1475           0 :   xb = xi;
    1476           0 :   for (i = 2; i <= e; i++)
    1477             :   {
    1478           0 :     pi = mulii(pi, p); /* = p^(i-1) */
    1479           0 :     B = ZC_Z_divexact(ZC_sub(B, f(E, xi)), p);
    1480           0 :     if (gc_needed(av,2))
    1481             :     {
    1482           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_ZpM_Dixon_Wiedemann. i=%ld",i);
    1483           0 :       gerepileall(av,3, &pi,&B,&xb);
    1484             :     }
    1485           0 :     xi = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, FpC_red(B, p), p);
    1486           0 :     if (!xi) return NULL;
    1487           0 :     if (typ(xi) == t_VEC) return gerepileupto(av, xi);
    1488           0 :     xb = ZC_add(xb, ZC_Z_mul(xi, pi));
    1489             :   }
    1490           0 :   return gerepileupto(av, xb);
    1491             : }
    1492             : 
    1493             : static GEN
    1494       25698 : vecprow(GEN A, GEN prow)
    1495             : {
    1496       25698 :   return mkvec2(vecsmallpermute(prow,gel(A,1)), gel(A,2));
    1497             : }
    1498             : 
    1499             : /* Solve the equation MX = A. Return either a solution as a t_COL,
    1500             :  * or the index of a column which is linearly dependent from the others as a
    1501             :  * t_VECSMALL with a single component. */
    1502             : GEN
    1503           0 : ZpMs_ZpCs_solve(GEN M, GEN A, long nbrow, GEN p, long e)
    1504             : {
    1505           0 :   pari_sp av = avma;
    1506             :   GEN pcol, prow;
    1507           0 :   long nbi=lg(M)-1, lR;
    1508             :   long i, n;
    1509             :   GEN Mp, Ap, Rp;
    1510             :   pari_timer ti;
    1511           0 :   if (DEBUGLEVEL) timer_start(&ti);
    1512           0 :   RgMs_structelim(M, nbrow, gel(A, 1), &pcol, &prow);
    1513           0 :   if (!pcol) return gc_NULL(av);
    1514           0 :   if (DEBUGLEVEL)
    1515           0 :     timer_printf(&ti,"structured elimination (%ld -> %ld)",nbi,lg(pcol)-1);
    1516           0 :   n = lg(pcol)-1;
    1517           0 :   Mp = cgetg(n+1, t_MAT);
    1518           0 :   for(i=1; i<=n; i++)
    1519           0 :     gel(Mp, i) = vecprow(gel(M,pcol[i]), prow);
    1520           0 :   Ap = zCs_to_ZC(vecprow(A, prow), n);
    1521           0 :   if (DEBUGLEVEL) timer_start(&ti);
    1522           0 :   Rp = gen_ZpM_Dixon_Wiedemann((void*)Mp,wrap_relcomb, Ap, p, e);
    1523           0 :   if (DEBUGLEVEL) timer_printf(&ti,"linear algebra");
    1524           0 :   if (!Rp) return gc_NULL(av);
    1525           0 :   lR = lg(Rp)-1;
    1526           0 :   if (typ(Rp) == t_COL)
    1527             :   {
    1528           0 :     GEN R = zerocol(nbi+1);
    1529           0 :     for (i=1; i<=lR; i++)
    1530           0 :        gel(R,pcol[i]) = gel(Rp,i);
    1531           0 :     return gerepilecopy(av, R);
    1532             :   }
    1533           0 :   for (i = 1; i <= lR; ++i)
    1534           0 :     if (signe(gel(Rp, i)))
    1535           0 :       return gerepileuptoleaf(av, mkvecsmall(pcol[i]));
    1536             :   return NULL; /* LCOV_EXCL_LINE */
    1537             : }
    1538             : 
    1539             : GEN
    1540           0 : FpMs_FpCs_solve(GEN M, GEN A, long nbrow, GEN p)
    1541             : {
    1542           0 :   return ZpMs_ZpCs_solve(M, A, nbrow, p, 1);
    1543             : }
    1544             : 
    1545             : GEN
    1546           0 : FpMs_FpCs_solve_safe(GEN M, GEN A, long nbrow, GEN p)
    1547             : {
    1548             :   GEN res;
    1549           0 :   pari_CATCH(e_INV)
    1550             :   {
    1551           0 :     GEN E = pari_err_last();
    1552           0 :     GEN x = err_get_compo(E,2);
    1553           0 :     if (typ(x) != t_INTMOD) pari_err(0,E);
    1554           0 :     if (DEBUGLEVEL)
    1555           0 :       pari_warn(warner,"FpMs_FpCs_solve_safe, impossible inverse %Ps", x);
    1556           0 :     res = NULL;
    1557             :   } pari_TRY {
    1558           0 :     res = ZpMs_ZpCs_solve(M, A, nbrow, p, 1);
    1559           0 :   } pari_ENDCATCH
    1560           0 :   return res;
    1561             : }
    1562             : 
    1563             : static GEN
    1564         126 : FpMs_structelim_back(GEN M, GEN V, GEN prow, GEN p)
    1565             : {
    1566         126 :   long i, j, oldf = 0, f = 0;
    1567         126 :   long lrow = lg(prow), lM = lg(M);
    1568         126 :   GEN W = const_vecsmall(lM-1,1);
    1569         126 :   GEN R = cgetg(lrow, t_VEC);
    1570      237272 :   for (i=1; i<lrow; i++)
    1571      237146 :     gel(R,i) = prow[i]==0 ? NULL: gel(V,prow[i]);
    1572             :   do
    1573             :   {
    1574         414 :     oldf = f;
    1575      443945 :     for(i=1; i<lM; i++)
    1576      443531 :       if (W[i])
    1577             :       {
    1578      150768 :         GEN C = gel(M,i), F = gel(C,1), E = gel(C,2);
    1579      150768 :         long c=0, cj=0, lF = lg(F);
    1580     1318019 :         for(j=1; j<lF; j++)
    1581     1167251 :           if (!gel(R,F[j])) { c++; cj=j; }
    1582      150768 :         if (c>=2) continue;
    1583      129590 :         if (c==1)
    1584             :         {
    1585       41695 :           pari_sp av = avma;
    1586       41695 :           GEN s = gen_0;
    1587      362323 :           for(j=1; j<lF; j++)
    1588      320628 :             if (j!=cj) s = Fp_add(s, mulis(gel(R,F[j]), E[j]), p);
    1589             :           /* s /= -E[cj] mod p */
    1590       41695 :           s = E[cj] < 0? Fp_divu(s, -E[cj], p): Fp_divu(Fp_neg(s,p), E[cj], p);
    1591       41695 :           gel(R,F[cj]) = gerepileuptoint(av, s);
    1592       41695 :           f++;
    1593             :         }
    1594      129590 :         W[i]=0;
    1595             :       }
    1596         414 :   } while(oldf!=f);
    1597      237272 :   for (i=1; i<lrow; i++)
    1598      237146 :     if (!gel(R,i)) gel(R,i) = gen_0;
    1599         126 :   return R;
    1600             : }
    1601             : 
    1602             : /* Return a linear form Y such that YM is essentially 0 */
    1603             : GEN
    1604         126 : FpMs_leftkernel_elt_col(GEN M, long nbcol, long nbrow, GEN p)
    1605             : {
    1606         126 :   pari_sp av = avma, av2;
    1607             :   GEN pcol, prow;
    1608             :   long i, n;
    1609             :   GEN Mp, B, MB, R, Rp;
    1610             :   pari_timer ti;
    1611             :   struct wrapper_modp_s W;
    1612         126 :   if (DEBUGLEVEL) timer_start(&ti);
    1613         126 :   RgMs_structelim_col(M, nbcol, nbrow, cgetg(1,t_VECSMALL), &pcol, &prow);
    1614         126 :   if (!pcol) return gc_NULL(av);
    1615         126 :   if (DEBUGLEVEL)
    1616           0 :     timer_printf(&ti,"structured elimination (%ld -> %ld)",nbcol,lg(pcol)-1);
    1617         126 :   n = lg(pcol)-1;
    1618         126 :   Mp = cgetg(n+1, t_MAT);
    1619       25824 :   for(i=1; i<=n; i++)
    1620       25698 :     gel(Mp, i) = vecprow(gel(M,pcol[i]), prow);
    1621         126 :   W.E = (void*) Mp;
    1622         126 :   W.f = wrap_relker;
    1623         126 :   W.p = p;
    1624         126 :   av2 = avma;
    1625             :   for(;;)
    1626             :   {
    1627         126 :     set_avma(av2);
    1628         126 :     B = random_FpV(n, p);
    1629         126 :     MB = FpV_FpMs_mul(B, Mp, p);
    1630         126 :     if (DEBUGLEVEL) timer_start(&ti);
    1631         126 :     pari_CATCH(e_INV)
    1632             :     {
    1633           0 :       GEN E = pari_err_last();
    1634           0 :       GEN x = err_get_compo(E,2);
    1635           0 :       if (typ(x) != t_INTMOD) pari_err(0,E);
    1636           0 :       if (DEBUGLEVEL)
    1637           0 :         pari_warn(warner,"FpMs_leftkernel_elt, impossible inverse %Ps", x);
    1638           0 :       Rp = NULL;
    1639             :     } pari_TRY {
    1640         126 :       Rp = gen_FpM_Wiedemann((void*)&W, wrap_relcomb_modp, MB, p);
    1641         126 :     } pari_ENDCATCH
    1642         126 :     if (!Rp) continue;
    1643         126 :     if (typ(Rp)==t_COL)
    1644             :     {
    1645         126 :       Rp = FpC_sub(Rp,B,p);
    1646         126 :       if (ZV_equal0(Rp)) continue;
    1647             :     }
    1648         126 :     R = FpMs_structelim_back(M, Rp, prow, p);
    1649         126 :     if (DEBUGLEVEL) timer_printf(&ti,"Wiedemann left kernel");
    1650         126 :     return gerepilecopy(av, R);
    1651             :   }
    1652             : }
    1653             : 
    1654             : GEN
    1655          42 : FpMs_leftkernel_elt(GEN M, long nbrow, GEN p)
    1656             : {
    1657          42 :   return FpMs_leftkernel_elt_col(M, lg(M)-1, nbrow, p);
    1658             : }

Generated by: LCOV version 1.13