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 - ZV.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.8.0 lcov report (development 19199-979caa2) Lines: 733 814 90.0 %
Date: 2016-07-24 07:10:30 Functions: 104 111 93.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             : static int
      18       36624 : check_ZV(GEN x, long l)
      19             : {
      20             :   long i;
      21      210350 :   for (i=1; i<l; i++)
      22      173810 :     if (typ(gel(x,i)) != t_INT) return 0;
      23       36540 :   return 1;
      24             : }
      25             : void
      26      457071 : RgV_check_ZV(GEN A, const char *s)
      27             : {
      28      457071 :   if (!RgV_is_ZV(A)) pari_err_TYPE(stack_strcat(s," [integer vector]"), A);
      29      457057 : }
      30             : void
      31       12138 : RgM_check_ZM(GEN A, const char *s)
      32             : {
      33       12138 :   long n = lg(A);
      34       12138 :   if (n != 1)
      35             :   {
      36       12103 :     long j, m = lgcols(A);
      37       48643 :     for (j=1; j<n; j++)
      38       36624 :       if (!check_ZV(gel(A,j), m))
      39          84 :         pari_err_TYPE(stack_strcat(s," [integer matrix]"), A);
      40             :   }
      41       12054 : }
      42             : 
      43             : long
      44       10612 : ZV_max_lg(GEN x)
      45             : {
      46       10612 :   long i, prec = 2, m = lg(x);
      47       10612 :   for (i=1; i<m; i++) { long l = lgefint(gel(x,i)); if (l > prec) prec = l; }
      48       10612 :   return prec;
      49             : }
      50             : long
      51        1617 : ZM_max_lg(GEN x)
      52             : {
      53        1617 :   long i, prec = 2, n = lg(x);
      54        1617 :   if (n != 1)
      55             :   {
      56        1617 :     long j, m = lgcols(x);
      57       12285 :     for (j=1; j<n; j++)
      58             :     {
      59       10668 :       GEN c = gel(x,j);
      60       10668 :       for (i=1; i<m; i++) { long l = lgefint(gel(c,i)); if (l > prec) prec = l; }
      61             :     }
      62             :   }
      63        1617 :   return prec;
      64             : }
      65             : 
      66             : GEN
      67        1274 : ZM_supnorm(GEN x)
      68             : {
      69        1274 :   long i, j, h, lx = lg(x);
      70        1274 :   GEN s = gen_0;
      71        1274 :   if (lx == 1) return gen_1;
      72        1274 :   h = lgcols(x);
      73       10717 :   for (j=1; j<lx; j++)
      74             :   {
      75        9443 :     GEN xj = gel(x,j);
      76      176596 :     for (i=1; i<h; i++)
      77             :     {
      78      167153 :       GEN c = gel(xj,i);
      79      167153 :       if (absi_cmp(c, s) > 0) s = c;
      80             :     }
      81             :   }
      82        1274 :   return absi(s);
      83             : }
      84             : 
      85             : /********************************************************************/
      86             : /**                                                                **/
      87             : /**                           MULTIPLICATION                       **/
      88             : /**                                                                **/
      89             : /********************************************************************/
      90             : /* x non-empty ZM, y a compatible nc (dimension > 0). */
      91             : static GEN
      92       32773 : ZM_nc_mul_i(GEN x, GEN y, long c, long l)
      93             : {
      94             :   long i, j;
      95             :   pari_sp av;
      96       32773 :   GEN z = cgetg(l,t_COL), s;
      97             : 
      98     1936290 :   for (i=1; i<l; i++)
      99             :   {
     100     1903517 :     av = avma; s = muliu(gcoeff(x,i,1),y[1]);
     101   146602087 :     for (j=2; j<c; j++)
     102   144698570 :       if (y[j]) s = addii(s, muliu(gcoeff(x,i,j),y[j]));
     103     1903517 :     gel(z,i) = gerepileuptoint(av,s);
     104             :   }
     105       32773 :   return z;
     106             : }
     107             : 
     108             : /* x ZV, y a compatible zc. */
     109             : GEN
     110           0 : ZV_zc_mul(GEN x, GEN y)
     111             : {
     112           0 :   long j, l = lg(x);
     113           0 :   pari_sp av = avma;
     114           0 :   GEN s = mulis(gel(x,1),y[1]);
     115           0 :   for (j=2; j<l; j++)
     116           0 :     if (y[j]) s = addii(s, mulis(gel(x,j),y[j]));
     117           0 :   return gerepileuptoint(av,s);
     118             : }
     119             : 
     120             : /* x non-empty ZM, y a compatible zc (dimension > 0). */
     121             : static GEN
     122     2511354 : ZM_zc_mul_i(GEN x, GEN y, long c, long l)
     123             : {
     124             :   long i, j;
     125     2511354 :   GEN z = cgetg(l,t_COL);
     126             : 
     127    15390503 :   for (i=1; i<l; i++)
     128             :   {
     129    12879149 :     pari_sp av = avma;
     130    12879149 :     GEN s = mulis(gcoeff(x,i,1),y[1]);
     131   241449019 :     for (j=2; j<c; j++)
     132   228569870 :       if (y[j]) s = addii(s, mulis(gcoeff(x,i,j),y[j]));
     133    12879149 :     gel(z,i) = gerepileuptoint(av,s);
     134             :   }
     135     2511354 :   return z;
     136             : }
     137             : GEN
     138     1305641 : ZM_zc_mul(GEN x, GEN y) {
     139     1305641 :   long l = lg(x);
     140     1305641 :   if (l == 1) return cgetg(1, t_COL);
     141     1305641 :   return ZM_zc_mul_i(x,y, l, lgcols(x));
     142             : }
     143             : 
     144             : /* y non-empty ZM, x a compatible zv (dimension > 0). */
     145             : GEN
     146        1204 : zv_ZM_mul(GEN x, GEN y) {
     147        1204 :   long i,j, lx = lg(x), ly = lg(y);
     148             :   GEN z;
     149        1204 :   if (lx == 1) return zerovec(ly-1);
     150        1204 :   z = cgetg(ly,t_VEC);
     151        2968 :   for (j=1; j<ly; j++)
     152             :   {
     153        1764 :     pari_sp av = avma;
     154        1764 :     GEN s = mulsi(x[1], gcoeff(y,1,j));
     155        3416 :     for (i=2; i<lx; i++)
     156        1652 :       if (x[i]) s = addii(s, mulsi(x[i], gcoeff(y,i,j)));
     157        1764 :     gel(z,j) = gerepileuptoint(av,s);
     158             :   }
     159        1204 :   return z;
     160             : }
     161             : 
     162             : /* x ZM, y a compatible zm (dimension > 0). */
     163             : GEN
     164      586604 : ZM_zm_mul(GEN x, GEN y)
     165             : {
     166      586604 :   long j, c, l = lg(x), ly = lg(y);
     167      586604 :   GEN z = cgetg(ly, t_MAT);
     168      586604 :   if (l == 1) return z;
     169      586604 :   c = lgcols(x);
     170      586604 :   for (j = 1; j < ly; j++) gel(z,j) = ZM_zc_mul_i(x, gel(y,j), l,c);
     171      586604 :   return z;
     172             : }
     173             : /* x ZM, y a compatible zn (dimension > 0). */
     174             : GEN
     175        3256 : ZM_nm_mul(GEN x, GEN y)
     176             : {
     177        3256 :   long j, c, l = lg(x), ly = lg(y);
     178        3256 :   GEN z = cgetg(ly, t_MAT);
     179        3256 :   if (l == 1) return z;
     180        3256 :   c = lgcols(x);
     181        3256 :   for (j = 1; j < ly; j++) gel(z,j) = ZM_nc_mul_i(x, gel(y,j), l,c);
     182        3256 :   return z;
     183             : }
     184             : 
     185             : /* Strassen-Winograd algorithm */
     186             : 
     187             : /*
     188             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     189             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     190             : */
     191             : static GEN
     192       85568 : add_slices(long m, long n,
     193             :            GEN A, long ma, long da, long na, long ea,
     194             :            GEN B, long mb, long db, long nb, long eb)
     195             : {
     196       85568 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     197       85568 :   GEN M = cgetg(n + 1, t_MAT), C;
     198             : 
     199     2257570 :   for (j = 1; j <= min_e; j++) {
     200     2172002 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     201    59499566 :     for (i = 1; i <= min_d; i++)
     202   114655128 :       gel(C, i) = addii(gcoeff(A, ma + i, na + j),
     203    57327564 :                         gcoeff(B, mb + i, nb + j));
     204     2209753 :     for (; i <= da; i++)
     205       37751 :       gel(C, i) = gcoeff(A, ma + i, na + j);
     206     2172002 :     for (; i <= db; i++)
     207           0 :       gel(C, i) = gcoeff(B, mb + i, nb + j);
     208     2172002 :     for (; i <= m; i++)
     209           0 :       gel(C, i) = gen_0;
     210             :   }
     211       88711 :   for (; j <= ea; j++) {
     212        3143 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     213       81277 :     for (i = 1; i <= da; i++)
     214       78134 :       gel(C, i) = gcoeff(A, ma + i, na + j);
     215        3143 :     for (; i <= m; i++)
     216           0 :       gel(C, i) = gen_0;
     217             :   }
     218       85568 :   for (; j <= eb; j++) {
     219           0 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     220           0 :     for (i = 1; i <= db; i++)
     221           0 :       gel(C, i) = gcoeff(B, mb + i, nb + j);
     222           0 :     for (; i <= m; i++)
     223           0 :       gel(C, i) = gen_0;
     224             :   }
     225       85568 :   for (; j <= n; j++)
     226           0 :     gel(M, j) = zerocol(m);
     227       85568 :   return M;
     228             : }
     229             : 
     230             : /*
     231             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     232             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     233             : */
     234             : static GEN
     235       74872 : subtract_slices(long m, long n,
     236             :                 GEN A, long ma, long da, long na, long ea,
     237             :                 GEN B, long mb, long db, long nb, long eb)
     238             : {
     239       74872 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     240       74872 :   GEN M = cgetg(n + 1, t_MAT), C;
     241             : 
     242     2028887 :   for (j = 1; j <= min_e; j++) {
     243     1954015 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     244    56370440 :     for (i = 1; i <= min_d; i++)
     245   108832850 :       gel(C, i) = subii(gcoeff(A, ma + i, na + j),
     246    54416425 :                         gcoeff(B, mb + i, nb + j));
     247     2002945 :     for (; i <= da; i++)
     248       48930 :       gel(C, i) = gcoeff(A, ma + i, na + j);
     249     2002525 :     for (; i <= db; i++)
     250       48510 :       gel(C, i) = negi(gcoeff(B, mb + i, nb + j));
     251     1954015 :     for (; i <= m; i++)
     252           0 :       gel(C, i) = gen_0;
     253             :   }
     254       74872 :   for (; j <= ea; j++) {
     255           0 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     256           0 :     for (i = 1; i <= da; i++)
     257           0 :       gel(C, i) = gcoeff(A, ma + i, na + j);
     258           0 :     for (; i <= m; i++)
     259           0 :       gel(C, i) = gen_0;
     260             :   }
     261       76251 :   for (; j <= eb; j++) {
     262        1379 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     263       46676 :     for (i = 1; i <= db; i++)
     264       45297 :       gel(C, i) = negi(gcoeff(B, mb + i, nb + j));
     265        1379 :     for (; i <= m; i++)
     266           0 :       gel(C, i) = gen_0;
     267             :   }
     268       76251 :   for (; j <= n; j++)
     269        1379 :     gel(M, j) = zerocol(m);
     270       74872 :   return M;
     271             : }
     272             : 
     273             : static GEN ZM_mul_i(GEN x, GEN y, long l, long lx, long ly);
     274             : 
     275             : /* Strassen-Winograd matrix product A (m x n) * B (n x p) */
     276             : static GEN
     277       10696 : ZM_mul_sw(GEN A, GEN B, long m, long n, long p)
     278             : {
     279       10696 :   pari_sp av = avma;
     280       10696 :   long m1 = (m + 1)/2, m2 = m/2,
     281       10696 :     n1 = (n + 1)/2, n2 = n/2,
     282       10696 :     p1 = (p + 1)/2, p2 = p/2;
     283             :   GEN A11, A12, A22, B11, B21, B22,
     284             :     S1, S2, S3, S4, T1, T2, T3, T4,
     285             :     M1, M2, M3, M4, M5, M6, M7,
     286             :     V1, V2, V3, C11, C12, C21, C22, C;
     287             : 
     288       10696 :   T2 = subtract_slices(n1, p2, B, 0, n1, p1, p2, B, n1, n2, p1, p2);
     289       10696 :   S1 = subtract_slices(m2, n1, A, m1, m2, 0, n1, A, 0, m2, 0, n1);
     290       10696 :   M2 = ZM_mul_i(S1, T2, m2 + 1, n1 + 1, p2 + 1);
     291       10696 :   if (gc_needed(av, 1))
     292           0 :     gerepileall(av, 2, &T2, &M2);  /* destroy S1 */
     293       10696 :   T3 = subtract_slices(n1, p1, T2, 0, n1, 0, p2, B, 0, n1, 0, p1);
     294       10696 :   if (gc_needed(av, 1))
     295           0 :     gerepileall(av, 2, &M2, &T3);  /* destroy T2 */
     296       10696 :   S2 = add_slices(m2, n1, A, m1, m2, 0, n1, A, m1, m2, n1, n2);
     297       10696 :   T1 = subtract_slices(n1, p1, B, 0, n1, p1, p2, B, 0, n1, 0, p2);
     298       10696 :   M3 = ZM_mul_i(S2, T1, m2 + 1, n1 + 1, p2 + 1);
     299       10696 :   if (gc_needed(av, 1))
     300           0 :     gerepileall(av, 4, &M2, &T3, &S2, &M3);  /* destroy T1 */
     301       10696 :   S3 = subtract_slices(m1, n1, S2, 0, m2, 0, n1, A, 0, m1, 0, n1);
     302       10696 :   if (gc_needed(av, 1))
     303           0 :     gerepileall(av, 4, &M2, &T3, &M3, &S3);  /* destroy S2 */
     304       10696 :   A11 = matslice(A, 1, m1, 1, n1);
     305       10696 :   B11 = matslice(B, 1, n1, 1, p1);
     306       10696 :   M1 = ZM_mul_i(A11, B11, m1 + 1, n1 + 1, p1 + 1);
     307       10696 :   if (gc_needed(av, 1))
     308           0 :     gerepileall(av, 5, &M2, &T3, &M3, &S3, &M1);  /* destroy A11, B11 */
     309       10696 :   A12 = matslice(A, 1, m1, n1 + 1, n);
     310       10696 :   B21 = matslice(B, n1 + 1, n, 1, p1);
     311       10696 :   M4 = ZM_mul_i(A12, B21, m1 + 1, n2 + 1, p1 + 1);
     312       10696 :   if (gc_needed(av, 1))
     313           0 :     gerepileall(av, 6, &M2, &T3, &M3, &S3, &M1, &M4);  /* destroy A12, B21 */
     314       10696 :   C11 = add_slices(m1, p1, M1, 0, m1, 0, p1, M4, 0, m1, 0, p1);
     315       10696 :   if (gc_needed(av, 1))
     316           0 :     gerepileall(av, 6, &M2, &T3, &M3, &S3, &M1, &C11);  /* destroy M4 */
     317       10696 :   M5 = ZM_mul_i(S3, T3, m1 + 1, n1 + 1, p1 + 1);
     318       10696 :   S4 = subtract_slices(m1, n2, A, 0, m1, n1, n2, S3, 0, m1, 0, n2);
     319       10696 :   if (gc_needed(av, 1))
     320           0 :     gerepileall(av, 7, &M2, &T3, &M3, &M1, &C11, &M5, &S4);  /* destroy S3 */
     321       10696 :   T4 = add_slices(n2, p1, B, n1, n2, 0, p1, T3, 0, n2, 0, p1);
     322       10696 :   if (gc_needed(av, 1))
     323           0 :     gerepileall(av, 7, &M2, &M3, &M1, &C11, &M5, &S4, &T4);  /* destroy T3 */
     324       10696 :   V1 = subtract_slices(m1, p1, M1, 0, m1, 0, p1, M5, 0, m1, 0, p1);
     325       10696 :   if (gc_needed(av, 1))
     326           0 :     gerepileall(av, 6, &M2, &M3, &S4, &T4, &C11, &V1);  /* destroy M1, M5 */
     327       10696 :   B22 = matslice(B, n1 + 1, n, p1 + 1, p);
     328       10696 :   M6 = ZM_mul_i(S4, B22, m1 + 1, n2 + 1, p2 + 1);
     329       10696 :   if (gc_needed(av, 1))
     330           0 :     gerepileall(av, 6, &M2, &M3, &T4, &C11, &V1, &M6);  /* destroy S4, B22 */
     331       10696 :   A22 = matslice(A, m1 + 1, m, n1 + 1, n);
     332       10696 :   M7 = ZM_mul_i(A22, T4, m2 + 1, n2 + 1, p1 + 1);
     333       10696 :   if (gc_needed(av, 1))
     334           0 :     gerepileall(av, 6, &M2, &M3, &C11, &V1, &M6, &M7);  /* destroy A22, T4 */
     335       10696 :   V3 = add_slices(m1, p2, V1, 0, m1, 0, p2, M3, 0, m2, 0, p2);
     336       10696 :   C12 = add_slices(m1, p2, V3, 0, m1, 0, p2, M6, 0, m1, 0, p2);
     337       10696 :   if (gc_needed(av, 1))
     338           0 :     gerepileall(av, 6, &M2, &M3, &C11, &V1, &M7, &C12);  /* destroy V3, M6 */
     339       10696 :   V2 = add_slices(m2, p1, V1, 0, m2, 0, p1, M2, 0, m2, 0, p2);
     340       10696 :   if (gc_needed(av, 1))
     341           0 :     gerepileall(av, 5, &M3, &C11, &M7, &C12, &V2);  /* destroy V1, M2 */
     342       10696 :   C21 = add_slices(m2, p1, V2, 0, m2, 0, p1, M7, 0, m2, 0, p1);
     343       10696 :   if (gc_needed(av, 1))
     344           0 :     gerepileall(av, 5, &M3, &C11, &C12, &V2, &C21);  /* destroy M7 */
     345       10696 :   C22 = add_slices(m2, p2, V2, 0, m2, 0, p2, M3, 0, m2, 0, p2);
     346       10696 :   if (gc_needed(av, 1))
     347           0 :     gerepileall(av, 4, &C11, &C12, &C21, &C22);  /* destroy V2, M3 */
     348       10696 :   C = mkmat2(mkcol2(C11, C21), mkcol2(C12, C22));
     349       10696 :   return gerepileupto(av, matconcat(C));
     350             : }
     351             : 
     352             : /* x[i,]*y. Assume lg(x) > 1 and 0 < i < lgcols(x) */
     353             : static GEN
     354   131605404 : ZMrow_ZC_mul_i(GEN x, GEN y, long i, long lx)
     355             : {
     356   131605404 :   pari_sp av = avma;
     357   131605404 :   GEN c = mulii(gcoeff(x,i,1), gel(y,1)), ZERO = gen_0;
     358             :   long k;
     359  2289422761 :   for (k = 2; k < lx; k++)
     360             :   {
     361  2157817357 :     GEN t = mulii(gcoeff(x,i,k), gel(y,k));
     362  2157817357 :     if (t != ZERO) c = addii(c, t);
     363             :   }
     364   131605404 :   return gerepileuptoint(av, c);
     365             : }
     366             : GEN
     367    15085202 : ZMrow_ZC_mul(GEN x, GEN y, long i)
     368    15085202 : { return ZMrow_ZC_mul_i(x, y, i, lg(x)); }
     369             : 
     370             : /* return x * y, 1 < lx = lg(x), l = lgcols(x) */
     371             : static GEN
     372    18663744 : ZM_ZC_mul_i(GEN x, GEN y, long lx, long l)
     373             : {
     374    18663744 :   GEN z = cgetg(l,t_COL);
     375             :   long i;
     376    18663744 :   for (i=1; i<l; i++) gel(z,i) = ZMrow_ZC_mul_i(x,y,i,lx);
     377    18663744 :   return z;
     378             : }
     379             : 
     380             : static GEN
     381     4260754 : ZM_mul_classical(GEN x, GEN y, long l, long lx, long ly)
     382             : {
     383             :   long j;
     384     4260754 :   GEN z = cgetg(ly, t_MAT);
     385    18425453 :   for (j = 1; j < ly; j++)
     386    14164699 :     gel(z, j) = ZM_ZC_mul_i(x, gel(y, j), lx, l);
     387     4260754 :   return z;
     388             : }
     389             : 
     390             : /* Strassen-Winograd used for dim >= ZM_sw_bound */
     391             : static const long ZM_sw_bound = 36;
     392             : 
     393             : static GEN
     394     4238466 : ZM_mul_i(GEN x, GEN y, long l, long lx, long ly)
     395             : {
     396     4238466 :   if (l <= ZM_sw_bound || lx <= ZM_sw_bound || ly <= ZM_sw_bound)
     397     4227770 :     return ZM_mul_classical(x, y, l, lx, ly);
     398             :   else
     399       10696 :     return ZM_mul_sw(x, y, l - 1, lx - 1, ly - 1);
     400             : }
     401             : 
     402             : GEN
     403     4166268 : ZM_mul(GEN x, GEN y)
     404             : {
     405     4166268 :   long lx=lg(x), ly=lg(y);
     406     4166268 :   if (ly==1) return cgetg(1,t_MAT);
     407     4163727 :   if (lx==1) return zeromat(0, ly-1);
     408     4163594 :   return ZM_mul_i(x, y, lgcols(x), lx, ly);
     409             : }
     410             : /* assume result is symmetric */
     411             : GEN
     412           0 : ZM_multosym(GEN x, GEN y)
     413             : {
     414           0 :   long j, lx, ly = lg(y);
     415             :   GEN M;
     416           0 :   if (ly == 1) return cgetg(1,t_MAT);
     417           0 :   lx = lg(x); /* = lgcols(y) */
     418           0 :   if (lx == 1) return cgetg(1,t_MAT);
     419             :   /* ly = lgcols(x) */
     420           0 :   M = cgetg(ly, t_MAT);
     421           0 :   for (j=1; j<ly; j++)
     422             :   {
     423           0 :     GEN z = cgetg(ly,t_COL), yj = gel(y,j);
     424             :     long i;
     425           0 :     for (i=1; i<j; i++) gel(z,i) = gcoeff(M,j,i);
     426           0 :     for (i=j; i<ly; i++)gel(z,i) = ZMrow_ZC_mul_i(x,yj,i,lx);
     427           0 :     gel(M,j) = z;
     428             :   }
     429           0 :   return M;
     430             : }
     431             : 
     432             : /* compute m*diagonal(d), assume lg(d) = lg(m). Shallow */
     433             : GEN
     434          14 : ZM_mul_diag(GEN m, GEN d)
     435             : {
     436             :   long j, l;
     437          14 :   GEN y = cgetg_copy(m, &l);
     438          56 :   for (j=1; j<l; j++)
     439             :   {
     440          42 :     GEN c = gel(d,j);
     441          42 :     gel(y,j) = equali1(c)? gel(m,j): ZC_Z_mul(gel(m,j), c);
     442             :   }
     443          14 :   return y;
     444             : }
     445             : /* compute diagonal(d)*m, assume lg(d) = lg(m~). Shallow */
     446             : GEN
     447         910 : ZM_diag_mul(GEN d, GEN m)
     448             : {
     449         910 :   long i, j, l = lg(d), lm = lg(m);
     450         910 :   GEN y = cgetg(lm, t_MAT);
     451         910 :   for (j=1; j<lm; j++) gel(y,j) = cgetg(l, t_COL);
     452        2457 :   for (i=1; i<l; i++)
     453             :   {
     454        1547 :     GEN c = gel(d,i);
     455        1547 :     if (equali1(c))
     456         364 :       for (j=1; j<lm; j++) gcoeff(y,i,j) = gcoeff(m,i,j);
     457             :     else
     458        1183 :       for (j=1; j<lm; j++) gcoeff(y,i,j) = mulii(gcoeff(m,i,j), c);
     459             :   }
     460         910 :   return y;
     461             : }
     462             : 
     463             : /* assume lx > 1 is lg(x) = lg(y) */
     464             : static GEN
     465    12362812 : ZV_dotproduct_i(GEN x, GEN y, long lx)
     466             : {
     467    12362812 :   pari_sp av = avma;
     468    12362812 :   GEN c = mulii(gel(x,1), gel(y,1));
     469             :   long i;
     470    98437015 :   for (i = 2; i < lx; i++)
     471             :   {
     472    86074203 :     GEN t = mulii(gel(x,i), gel(y,i));
     473    86074203 :     if (t != gen_0) c = addii(c, t);
     474             :   }
     475    12362812 :   return gerepileuptoint(av, c);
     476             : }
     477             : 
     478             : /* x~ * y, assuming result is symmetric */
     479             : GEN
     480      160265 : ZM_transmultosym(GEN x, GEN y)
     481             : {
     482      160265 :   long i, j, l, ly = lg(y);
     483             :   GEN M;
     484      160265 :   if (ly == 1) return cgetg(1,t_MAT);
     485             :   /* lg(x) = ly */
     486      160265 :   l = lgcols(y); /* = lgcols(x) */
     487      160265 :   M = cgetg(ly, t_MAT);
     488     1251022 :   for (i=1; i<ly; i++)
     489             :   {
     490     1090757 :     GEN xi = gel(x,i), c = cgetg(ly,t_COL);
     491     1090757 :     gel(M,i) = c;
     492     4912979 :     for (j=1; j<i; j++)
     493     3822222 :       gcoeff(M,i,j) = gel(c,j) = ZV_dotproduct_i(xi,gel(y,j),l);
     494     1090757 :     gel(c,i) = ZV_dotproduct_i(xi,gel(y,i),l);
     495             :   }
     496      160265 :   return M;
     497             : }
     498             : /* x~ * y */
     499             : GEN
     500         497 : ZM_transmul(GEN x, GEN y)
     501             : {
     502         497 :   long i, j, l, lx, ly = lg(y);
     503             :   GEN M;
     504         497 :   if (ly == 1) return cgetg(1,t_MAT);
     505         497 :   lx = lg(x);
     506         497 :   l = lgcols(y);
     507         497 :   if (lgcols(x) != l) pari_err_OP("operation 'ZM_transmul'", x,y);
     508         497 :   M = cgetg(ly, t_MAT);
     509        1617 :   for (i=1; i<ly; i++)
     510             :   {
     511        1120 :     GEN yi = gel(y,i), c = cgetg(lx,t_COL);
     512        1120 :     gel(M,i) = c;
     513        1120 :     for (j=1; j<lx; j++) gel(c,j) = ZV_dotproduct_i(yi,gel(x,j),l);
     514             :   }
     515         497 :   return M;
     516             : }
     517             : 
     518             : static GEN
     519       32984 : ZM_sqr_i(GEN x, long l)
     520             : {
     521       32984 :   if (l <= ZM_sw_bound)
     522       32984 :     return ZM_mul_classical(x, x, l, l, l);
     523             :   else
     524           0 :     return ZM_mul_sw(x, x, l - 1, l - 1, l - 1);
     525             : }
     526             : 
     527             : GEN
     528       32984 : ZM_sqr(GEN x)
     529             : {
     530       32984 :   long lx=lg(x);
     531       32984 :   if (lx==1) return cgetg(1,t_MAT);
     532       32984 :   return ZM_sqr_i(x, lx);
     533             : }
     534             : GEN
     535     4569186 : ZM_ZC_mul(GEN x, GEN y)
     536             : {
     537     4569186 :   long lx = lg(x);
     538     4569186 :   return lx==1? cgetg(1,t_COL): ZM_ZC_mul_i(x, y, lx, lgcols(x));
     539             : }
     540             : 
     541             : GEN
     542        1904 : ZM_Z_div(GEN X, GEN c)
     543             : {
     544        1904 :   long i, j, h, l = lg(X);
     545        1904 :   GEN A = cgetg(l, t_MAT);
     546        1904 :   if (l == 1) return A;
     547        1904 :   h = lgcols(X);
     548       30156 :   for (j=1; j<l; j++)
     549             :   {
     550       28252 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     551       28252 :     for (i = 1; i < h; i++) gel(a,i) = gred_frac2(gel(x,i), c);
     552       28252 :     gel(A,j) = a;
     553             :   }
     554        1904 :   return A;
     555             : }
     556             : 
     557             : long
     558   151007458 : zv_dotproduct(GEN x, GEN y)
     559             : {
     560   151007458 :   long i, lx = lg(x);
     561             :   ulong c;
     562   151007458 :   if (lx == 1) return 0;
     563   151007458 :   c = uel(x,1)*uel(y,1);
     564  2329149585 :   for (i = 2; i < lx; i++)
     565  2178142127 :     c += uel(x,i)*uel(y,i);
     566   151007458 :   return c;
     567             : }
     568             : 
     569             : GEN
     570        6188 : ZV_ZM_mul(GEN x, GEN y)
     571             : {
     572        6188 :   long i, lx = lg(x), ly = lg(y);
     573             :   GEN z;
     574        6188 :   if (lx == 1) return zerovec(ly-1);
     575        6174 :   z = cgetg(ly, t_VEC);
     576        6174 :   for (i = 1; i < ly; i++) gel(z,i) = ZV_dotproduct_i(x, gel(y,i), lx);
     577        6174 :   return z;
     578             : }
     579             : 
     580             : GEN
     581           0 : ZC_ZV_mul(GEN x, GEN y)
     582             : {
     583           0 :   long i,j, lx=lg(x), ly=lg(y);
     584             :   GEN z;
     585           0 :   if (ly==1) return cgetg(1,t_MAT);
     586           0 :   z = cgetg(ly,t_MAT);
     587           0 :   for (j=1; j < ly; j++)
     588             :   {
     589           0 :     gel(z,j) = cgetg(lx,t_COL);
     590           0 :     for (i=1; i<lx; i++) gcoeff(z,i,j) = mulii(gel(x,i),gel(y,j));
     591             :   }
     592           0 :   return z;
     593             : }
     594             : 
     595             : GEN
     596     4698153 : ZV_dotsquare(GEN x)
     597             : {
     598             :   long i, lx;
     599             :   pari_sp av;
     600             :   GEN z;
     601     4698153 :   lx = lg(x);
     602     4698153 :   if (lx == 1) return gen_0;
     603     4698153 :   av = avma; z = sqri(gel(x,1));
     604     4698153 :   for (i=2; i<lx; i++) z = addii(z, sqri(gel(x,i)));
     605     4698153 :   return gerepileuptoint(av,z);
     606             : }
     607             : 
     608             : GEN
     609    10844921 : ZV_dotproduct(GEN x,GEN y)
     610             : {
     611             :   long lx;
     612    10844921 :   if (x == y) return ZV_dotsquare(x);
     613     7434993 :   lx = lg(x);
     614     7434993 :   if (lx == 1) return gen_0;
     615     7434993 :   return ZV_dotproduct_i(x, y, lx);
     616             : }
     617             : 
     618             : static GEN
     619         217 : _ZM_mul(void *data /*ignored*/, GEN x, GEN y)
     620         217 : { (void)data; return ZM_mul(x,y); }
     621             : static GEN
     622       32522 : _ZM_sqr(void *data /*ignored*/, GEN x)
     623       32522 : { (void)data; return ZM_sqr(x); }
     624             : GEN
     625           0 : ZM_pow(GEN x, GEN n)
     626             : {
     627           0 :   pari_sp av = avma;
     628           0 :   if (!signe(n)) return matid(lg(x)-1);
     629           0 :   return gerepileupto(av, gen_pow(x, n, NULL, &_ZM_sqr, &_ZM_mul));
     630             : }
     631             : GEN
     632       31066 : ZM_powu(GEN x, ulong n)
     633             : {
     634       31066 :   pari_sp av = avma;
     635       31066 :   if (!n) return matid(lg(x)-1);
     636       31066 :   return gerepileupto(av, gen_powu(x, n, NULL, &_ZM_sqr, &_ZM_mul));
     637             : }
     638             : /********************************************************************/
     639             : /**                                                                **/
     640             : /**                           ADD, SUB                             **/
     641             : /**                                                                **/
     642             : /********************************************************************/
     643             : static GEN
     644     1926785 : ZC_add_i(GEN x, GEN y, long lx)
     645             : {
     646     1926785 :   GEN A = cgetg(lx, t_COL);
     647             :   long i;
     648     1926785 :   for (i=1; i<lx; i++) gel(A,i) = addii(gel(x,i), gel(y,i));
     649     1926785 :   return A;
     650             : }
     651             : GEN
     652     1106152 : ZC_add(GEN x, GEN y) { return ZC_add_i(x, y, lg(x)); }
     653             : GEN
     654       85470 : ZC_Z_add(GEN x, GEN y)
     655             : {
     656       85470 :   long k, lx = lg(x);
     657       85470 :   GEN z = cgetg(lx, t_COL);
     658       85470 :   if (lx == 1) pari_err_TYPE2("+",x,y);
     659       85470 :   gel(z,1) = addii(y,gel(x,1));
     660       85470 :   for (k = 2; k < lx; k++) gel(z,k) = icopy(gel(x,k));
     661       85470 :   return z;
     662             : }
     663             : 
     664             : static GEN
     665     1192889 : ZC_sub_i(GEN x, GEN y, long lx)
     666             : {
     667             :   long i;
     668     1192889 :   GEN A = cgetg(lx, t_COL);
     669     1192889 :   for (i=1; i<lx; i++) gel(A,i) = subii(gel(x,i), gel(y,i));
     670     1192889 :   return A;
     671             : }
     672             : GEN
     673     1089537 : ZC_sub(GEN x, GEN y) { return ZC_sub_i(x, y, lg(x)); }
     674             : GEN
     675           0 : ZC_Z_sub(GEN x, GEN y)
     676             : {
     677           0 :   long k, lx = lg(x);
     678           0 :   GEN z = cgetg(lx, t_COL);
     679           0 :   if (lx == 1) pari_err_TYPE2("+",x,y);
     680           0 :   gel(z,1) = subii(gel(x,1), y);
     681           0 :   for (k = 2; k < lx; k++) gel(z,k) = icopy(gel(x,k));
     682           0 :   return z;
     683             : }
     684             : GEN
     685      266010 : Z_ZC_sub(GEN a, GEN x)
     686             : {
     687      266010 :   long k, lx = lg(x);
     688      266010 :   GEN z = cgetg(lx, t_COL);
     689      266010 :   if (lx == 1) pari_err_TYPE2("-",a,x);
     690      266010 :   gel(z,1) = subii(a, gel(x,1));
     691      266010 :   for (k = 2; k < lx; k++) gel(z,k) = negi(gel(x,k));
     692      266010 :   return z;
     693             : }
     694             : 
     695             : GEN
     696       40401 : ZM_add(GEN x, GEN y)
     697             : {
     698       40401 :   long lx = lg(x), l, j;
     699             :   GEN z;
     700       40401 :   if (lx == 1) return cgetg(1, t_MAT);
     701       38973 :   z = cgetg(lx, t_MAT); l = lgcols(x);
     702       38973 :   for (j = 1; j < lx; j++) gel(z,j) = ZC_add_i(gel(x,j), gel(y,j), l);
     703       38973 :   return z;
     704             : }
     705             : GEN
     706       20081 : ZM_sub(GEN x, GEN y)
     707             : {
     708       20081 :   long lx = lg(x), l, j;
     709             :   GEN z;
     710       20081 :   if (lx == 1) return cgetg(1, t_MAT);
     711       20081 :   z = cgetg(lx, t_MAT); l = lgcols(x);
     712       20081 :   for (j = 1; j < lx; j++) gel(z,j) = ZC_sub_i(gel(x,j), gel(y,j), l);
     713       20081 :   return z;
     714             : }
     715             : /********************************************************************/
     716             : /**                                                                **/
     717             : /**                         LINEAR COMBINATION                     **/
     718             : /**                                                                **/
     719             : /********************************************************************/
     720             : /* return X/c assuming division is exact */
     721             : GEN
     722     1487420 : ZC_Z_divexact(GEN X, GEN c)
     723             : {
     724     1487420 :   long i, l = lg(X);
     725     1487420 :   GEN A = cgetg(l, t_COL);
     726     1487420 :   for (i=1; i<l; i++) gel(A,i) = diviiexact(gel(X,i), c);
     727     1487413 :   return A;
     728             : }
     729             : GEN
     730       79808 : ZM_Z_divexact(GEN X, GEN c)
     731             : {
     732       79808 :   long i, l = lg(X);
     733       79808 :   GEN A = cgetg(l, t_MAT);
     734       79808 :   for (i = 1; i < l; i++) gel(A,i) = ZC_Z_divexact(gel(X,i), c);
     735       79801 :   return A;
     736             : }
     737             : /* Return c * X */
     738             : GEN
     739     2606845 : ZC_Z_mul(GEN X, GEN c)
     740             : {
     741             :   long i, l;
     742             :   GEN A;
     743     2606845 :   if (!signe(c)) return zerocol(lg(X)-1);
     744     2582947 :   if (is_pm1(c)) return (signe(c) > 0)? ZC_copy(X): ZC_neg(X);
     745     2507395 :   l = lg(X); A = cgetg(l, t_COL);
     746     2507395 :   for (i=1; i<l; i++) gel(A,i) = mulii(c,gel(X,i));
     747     2507395 :   return A;
     748             : }
     749             : GEN
     750       13209 : ZC_z_mul(GEN X, long c)
     751             : {
     752             :   long i, l;
     753             :   GEN A;
     754       13209 :   if (!c) return zerocol(lg(X)-1);
     755        6692 :   if (c == 1) return ZC_copy(X);
     756        2219 :   if (c ==-1) return ZC_neg(X);
     757         756 :   l = lg(X); A = cgetg(l, t_COL);
     758         756 :   for (i=1; i<l; i++) gel(A,i) = mulsi(c,gel(X,i));
     759         756 :   return A;
     760             : }
     761             : 
     762             : GEN
     763        4361 : zv_z_mul(GEN M, long n)
     764             : {
     765             :   long l;
     766        4361 :   GEN N = cgetg_copy(M, &l);
     767        4361 :   while (--l > 0) N[l] = M[l]*n;
     768        4361 :   return N;
     769             : }
     770             : 
     771             : /* return a ZM */
     772             : GEN
     773        3256 : nm_Z_mul(GEN X, GEN c)
     774             : {
     775        3256 :   long i, j, h, l = lg(X), s = signe(c);
     776             :   GEN A;
     777        3256 :   if (l == 1) return cgetg(1, t_MAT);
     778        3256 :   h = lgcols(X);
     779        3256 :   if (!s) return zeromat(h-1, l-1);
     780        3256 :   if (is_pm1(c)) {
     781           0 :     if (s > 0) return Flm_to_ZM(X);
     782           0 :     X = Flm_to_ZM(X); ZM_togglesign(X); return X;
     783             :   }
     784        3256 :   A = cgetg(l, t_MAT);
     785       36029 :   for (j = 1; j < l; j++)
     786             :   {
     787       32773 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     788       32773 :     for (i = 1; i < h; i++) gel(a,i) = muliu(c, x[i]);
     789       32773 :     gel(A,j) = a;
     790             :   }
     791        3256 :   return A;
     792             : }
     793             : GEN
     794       62860 : ZM_Z_mul(GEN X, GEN c)
     795             : {
     796       62860 :   long i, j, h, l = lg(X);
     797             :   GEN A;
     798       62860 :   if (l == 1) return cgetg(1, t_MAT);
     799       62860 :   h = lgcols(X);
     800       62860 :   if (!signe(c)) return zeromat(h-1, l-1);
     801       62797 :   if (is_pm1(c)) return (signe(c) > 0)? ZM_copy(X): ZM_neg(X);
     802       46725 :   A = cgetg(l, t_MAT);
     803      694642 :   for (j = 1; j < l; j++)
     804             :   {
     805      647917 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     806      647917 :     for (i = 1; i < h; i++) gel(a,i) = mulii(c, gel(x,i));
     807      647917 :     gel(A,j) = a;
     808             :   }
     809       46725 :   return A;
     810             : }
     811             : 
     812             : /* X <- X + v Y (elementary col operation) */
     813             : void
     814    39640745 : ZC_lincomb1_inplace(GEN X, GEN Y, GEN v)
     815             : {
     816    39640745 :   long i, m = lgefint(v);
     817    79281490 :   if (m == 2) return; /* v = 0 */
     818    39640745 :   for (i = lg(X)-1; i; i--) gel(X,i) = addmulii_inplace(gel(X,i), gel(Y,i), v);
     819             : }
     820             : void
     821     7183793 : Flc_lincomb1_inplace(GEN X, GEN Y, ulong v, ulong q)
     822             : {
     823             :   long i;
     824    14367586 :   if (!v) return; /* v = 0 */
     825     7183793 :   for (i = lg(X)-1; i; i--) X[i] = Fl_add(X[i], Fl_mul(Y[i], v, q), q);
     826             : }
     827             : 
     828             : /* X + v Y, wasteful if (v = 0) */
     829             : static GEN
     830     2495445 : ZC_lincomb1(GEN v, GEN X, GEN Y)
     831             : {
     832     2495445 :   long i, lx = lg(X);
     833     2495445 :   GEN A = cgetg(lx,t_COL);
     834     2495445 :   for (i=1; i<lx; i++) gel(A,i) = addmulii(gel(X,i), gel(Y,i), v);
     835     2495445 :   return A;
     836             : }
     837             : /* -X + vY */
     838             : static GEN
     839      379850 : ZC_lincomb_1(GEN v, GEN X, GEN Y)
     840             : {
     841      379850 :   long i, lx = lg(X);
     842      379850 :   GEN A = cgetg(lx,t_COL);
     843      379850 :   for (i=1; i<lx; i++) gel(A,i) = mulsubii(gel(Y,i), v, gel(X,i));
     844      379850 :   return A;
     845             : }
     846             : /* X,Y compatible ZV; u,v in Z. Returns A = u*X + v*Y */
     847             : GEN
     848     8265021 : ZC_lincomb(GEN u, GEN v, GEN X, GEN Y)
     849             : {
     850             :   long su, sv;
     851             :   GEN A;
     852             : 
     853     8265021 :   su = signe(u); if (!su) return ZC_Z_mul(Y, v);
     854     8265014 :   sv = signe(v); if (!sv) return ZC_Z_mul(X, u);
     855     8264993 :   if (is_pm1(v))
     856             :   {
     857     1773061 :     if (is_pm1(u))
     858             :     {
     859     1161245 :       if (su != sv) A = ZC_sub(X, Y);
     860      530409 :       else          A = ZC_add(X, Y);
     861     1161245 :       if (su < 0) ZV_togglesign(A); /* in place but was created above */
     862             :     }
     863             :     else
     864             :     {
     865      611816 :       if (sv > 0) A = ZC_lincomb1 (u, Y, X);
     866      267382 :       else        A = ZC_lincomb_1(u, Y, X);
     867             :     }
     868             :   }
     869     6491932 :   else if (is_pm1(u))
     870             :   {
     871     2263479 :     if (su > 0) A = ZC_lincomb1 (v, X, Y);
     872      112468 :     else        A = ZC_lincomb_1(v, X, Y);
     873             :   }
     874             :   else
     875             :   { /* not cgetg_copy: x may be a t_VEC */
     876     4228453 :     long i, lx = lg(X);
     877     4228453 :     A = cgetg(lx,t_COL);
     878     4228453 :     for (i=1; i<lx; i++) gel(A,i) = lincombii(u,v,gel(X,i),gel(Y,i));
     879             :   }
     880     8264993 :   return A;
     881             : }
     882             : 
     883             : /********************************************************************/
     884             : /**                                                                **/
     885             : /**                           CONVERSIONS                          **/
     886             : /**                                                                **/
     887             : /********************************************************************/
     888             : GEN
     889        6104 : ZV_to_nv(GEN z)
     890             : {
     891        6104 :   long i, l = lg(z);
     892        6104 :   GEN x = cgetg(l, t_VECSMALL);
     893        6104 :   for (i=1; i<l; i++) x[i] = itou(gel(z,i));
     894        6104 :   return x;
     895             : }
     896             : 
     897             : GEN
     898      364662 : zm_to_ZM(GEN z)
     899             : {
     900      364662 :   long i, l = lg(z);
     901      364662 :   GEN x = cgetg(l,t_MAT);
     902      364662 :   for (i=1; i<l; i++) gel(x,i) = zc_to_ZC(gel(z,i));
     903      364662 :   return x;
     904             : }
     905             : 
     906             : GEN
     907          98 : zmV_to_ZMV(GEN z)
     908             : {
     909          98 :   long i, l = lg(z);
     910          98 :   GEN x = cgetg(l,t_VEC);
     911          98 :   for (i=1; i<l; i++) gel(x,i) = zm_to_ZM(gel(z,i));
     912          98 :   return x;
     913             : }
     914             : 
     915             : /* same as Flm_to_ZM but do not assume positivity */
     916             : GEN
     917         777 : ZM_to_zm(GEN z)
     918             : {
     919         777 :   long i, l = lg(z);
     920         777 :   GEN x = cgetg(l,t_MAT);
     921         777 :   for (i=1; i<l; i++) gel(x,i) = ZV_to_zv(gel(z,i));
     922         777 :   return x;
     923             : }
     924             : 
     925             : GEN
     926      223314 : zv_to_Flv(GEN z, ulong p)
     927             : {
     928      223314 :   long i, l = lg(z);
     929      223314 :   GEN x = cgetg(l,t_VECSMALL);
     930      223314 :   for (i=1; i<l; i++) x[i] = umodsu(z[i], p);
     931      223314 :   return x;
     932             : }
     933             : 
     934             : GEN
     935       15792 : zm_to_Flm(GEN z, ulong p)
     936             : {
     937       15792 :   long i, l = lg(z);
     938       15792 :   GEN x = cgetg(l,t_MAT);
     939       15792 :   for (i=1; i<l; i++) gel(x,i) = zv_to_Flv(gel(z,i),p);
     940       15792 :   return x;
     941             : }
     942             : 
     943             : GEN
     944          21 : ZMV_to_zmV(GEN z)
     945             : {
     946          21 :   long i,l = lg(z);
     947          21 :   GEN x = cgetg(l, t_VEC);
     948          21 :   for (i=1; i<l; i++) gel(x,i) = ZM_to_zm(gel(z,i));
     949          21 :   return x;
     950             : }
     951             : 
     952             : /********************************************************************/
     953             : /**                                                                **/
     954             : /**                         COPY, NEGATION                         **/
     955             : /**                                                                **/
     956             : /********************************************************************/
     957             : GEN
     958      959100 : ZC_copy(GEN x)
     959             : {
     960      959100 :   long i, lx = lg(x);
     961      959100 :   GEN y = cgetg(lx, t_COL);
     962     9147275 :   for (i=1; i<lx; i++)
     963             :   {
     964     8188175 :     GEN c = gel(x,i);
     965     8188175 :     gel(y,i) = lgefint(c) == 2? gen_0: icopy(c);
     966             :   }
     967      959100 :   return y;
     968             : }
     969             : 
     970             : GEN
     971       84703 : ZM_copy(GEN x)
     972             : {
     973             :   long l;
     974       84703 :   GEN y = cgetg_copy(x, &l);
     975       84703 :   while (--l > 0) gel(y,l) = ZC_copy(gel(x,l));
     976       84703 :   return y;
     977             : }
     978             : 
     979             : void
     980       58435 : ZV_neg_inplace(GEN M)
     981             : {
     982       58435 :   long l = lg(M);
     983       58435 :   while (--l > 0) gel(M,l) = negi(gel(M,l));
     984       58435 : }
     985             : GEN
     986     1933562 : ZC_neg(GEN M)
     987             : {
     988     1933562 :   long l = lg(M);
     989     1933562 :   GEN N = cgetg(l, t_COL);
     990     1933562 :   while (--l > 0) gel(N,l) = negi(gel(M,l));
     991     1933562 :   return N;
     992             : }
     993             : GEN
     994      213701 : zv_neg(GEN M)
     995             : {
     996             :   long l;
     997      213701 :   GEN N = cgetg_copy(M, &l);
     998      213701 :   while (--l > 0) N[l] = -M[l];
     999      213701 :   return N;
    1000             : }
    1001             : GEN
    1002         231 : zv_neg_inplace(GEN M)
    1003             : {
    1004         231 :   long l = lg(M);
    1005         231 :   while (--l > 0) M[l] = -M[l];
    1006         231 :   return M;
    1007             : }
    1008             : GEN
    1009      228102 : ZM_neg(GEN x)
    1010             : {
    1011             :   long l;
    1012      228102 :   GEN y = cgetg_copy(x, &l);
    1013      228102 :   while (--l > 0) gel(y,l) = ZC_neg(gel(x,l));
    1014      228102 :   return y;
    1015             : }
    1016             : 
    1017             : void
    1018     1798791 : ZV_togglesign(GEN M)
    1019             : {
    1020     1798791 :   long l = lg(M);
    1021     1798791 :   while (--l > 0) togglesign_safe(&gel(M,l));
    1022     1798791 : }
    1023             : void
    1024           0 : ZM_togglesign(GEN M)
    1025             : {
    1026           0 :   long l = lg(M);
    1027           0 :   while (--l > 0) ZV_togglesign(gel(M,l));
    1028           0 : }
    1029             : 
    1030             : /********************************************************************/
    1031             : /**                                                                **/
    1032             : /**                        "DIVISION" mod HNF                      **/
    1033             : /**                                                                **/
    1034             : /********************************************************************/
    1035             : /* Reduce ZC x modulo ZM y in HNF, may return x itself (not a copy) */
    1036             : GEN
    1037      888765 : ZC_hnfremdiv(GEN x, GEN y, GEN *Q)
    1038             : {
    1039      888765 :   long i, l = lg(x);
    1040             :   GEN q;
    1041             : 
    1042      888765 :   if (Q) *Q = cgetg(l,t_COL);
    1043      888765 :   if (l == 1) return cgetg(1,t_COL);
    1044     5230806 :   for (i = l-1; i>0; i--)
    1045             :   {
    1046     4342041 :     q = diviiround(gel(x,i), gcoeff(y,i,i));
    1047     4342041 :     if (signe(q)) {
    1048     2411256 :       togglesign(q);
    1049     2411256 :       x = ZC_lincomb(gen_1, q, x, gel(y,i));
    1050             :     }
    1051     4342041 :     if (Q) gel(*Q, i) = q;
    1052             :   }
    1053      888765 :   return x;
    1054             : }
    1055             : 
    1056             : /* x = y Q + R, may return some columns of x (not copies) */
    1057             : GEN
    1058       60397 : ZM_hnfdivrem(GEN x, GEN y, GEN *Q)
    1059             : {
    1060       60397 :   long lx = lg(x), i;
    1061       60397 :   GEN R = cgetg(lx, t_MAT);
    1062       60397 :   if (Q)
    1063             :   {
    1064        3440 :     GEN q = cgetg(lx, t_MAT); *Q = q;
    1065        3440 :     for (i=1; i<lx; i++) gel(R,i) = ZC_hnfremdiv(gel(x,i),y,(GEN*)(q+i));
    1066             :   }
    1067             :   else
    1068      171531 :     for (i=1; i<lx; i++)
    1069             :     {
    1070      114574 :       pari_sp av = avma;
    1071      114574 :       GEN z = ZC_hnfrem(gel(x,i),y);
    1072      114574 :       gel(R,i) = (avma == av)? ZC_copy(z): gerepileupto(av, z);
    1073             :     }
    1074       60397 :   return R;
    1075             : }
    1076             : 
    1077             : 
    1078             : /********************************************************************/
    1079             : /**                                                                **/
    1080             : /**                               TESTS                            **/
    1081             : /**                                                                **/
    1082             : /********************************************************************/
    1083             : int
    1084    17346924 : zv_equal0(GEN V)
    1085             : {
    1086    17346924 :   long l = lg(V);
    1087    45098669 :   while (--l > 0)
    1088    22449364 :     if (V[l]) return 0;
    1089     5302381 :   return 1;
    1090             : }
    1091             : 
    1092             : int
    1093      462791 : ZV_equal0(GEN V)
    1094             : {
    1095      462791 :   long l = lg(V);
    1096     2187308 :   while (--l > 0)
    1097     1631542 :     if (signe(gel(V,l))) return 0;
    1098       92975 :   return 1;
    1099             : }
    1100             : 
    1101             : static int
    1102     9108509 : ZV_equal_lg(GEN V, GEN W, long l)
    1103             : {
    1104    25823372 :   while (--l > 0)
    1105    14611612 :     if (!equalii(gel(V,l), gel(W,l))) return 0;
    1106     2103251 :   return 1;
    1107             : }
    1108             : int
    1109     7762634 : ZV_equal(GEN V, GEN W)
    1110             : {
    1111     7762634 :   long l = lg(V);
    1112     7762634 :   if (lg(W) != l) return 0;
    1113     7762634 :   return ZV_equal_lg(V, W, l);
    1114             : }
    1115             : int
    1116      426775 : ZM_equal(GEN A, GEN B)
    1117             : {
    1118      426775 :   long i, m, l = lg(A);
    1119      426775 :   if (lg(B) != l) return 0;
    1120      426488 :   if (l == 1) return 1;
    1121      426488 :   m = lgcols(A);
    1122      426488 :   if (lgcols(B) != m) return 0;
    1123     1728567 :   for (i = 1; i < l; i++)
    1124     1345875 :     if (!ZV_equal_lg(gel(A,i), gel(B,i), m)) return 0;
    1125      382692 :   return 1;
    1126             : }
    1127             : int
    1128    21890486 : zv_equal(GEN V, GEN W)
    1129             : {
    1130    21890486 :   long l = lg(V);
    1131    21890486 :   if (lg(W) != l) return 0;
    1132   217127500 :   while (--l > 0)
    1133   174117709 :     if (V[l] != W[l]) return 0;
    1134    21480876 :   return 1;
    1135             : }
    1136             : 
    1137             : int
    1138         343 : zvV_equal(GEN V, GEN W)
    1139             : {
    1140         343 :   long l = lg(V);
    1141         343 :   if (lg(W) != l) return 0;
    1142       58856 :   while (--l > 0)
    1143       58310 :     if (!zv_equal(gel(V,l),gel(W,l))) return 0;
    1144         210 :   return 1;
    1145             : }
    1146             : 
    1147             : int
    1148       81532 : ZM_ishnf(GEN x)
    1149             : {
    1150       81532 :   long i,j, lx = lg(x);
    1151      313981 :   for (i=1; i<lx; i++)
    1152             :   {
    1153      239876 :     GEN xii = gcoeff(x,i,i);
    1154      239876 :     if (signe(xii) <= 0) return 0;
    1155      802184 :     for (j=1; j<i; j++)
    1156      562777 :       if (signe(gcoeff(x,i,j))) return 0;
    1157      831136 :     for (j=i+1; j<lx; j++)
    1158             :     {
    1159      598687 :       GEN xij = gcoeff(x,i,j);
    1160      598687 :       if (signe(xij)<0 || cmpii(xij,xii)>=0) return 0;
    1161             :     }
    1162             :   }
    1163       74105 :   return 1;
    1164             : }
    1165             : int
    1166      253269 : ZM_isidentity(GEN x)
    1167             : {
    1168      253269 :   long i,j, lx = lg(x);
    1169             : 
    1170      253269 :   if (lx == 1) return 1;
    1171      253269 :   if (lx != lgcols(x)) return 0;
    1172     1096656 :   for (j=1; j<lx; j++)
    1173             :   {
    1174      995707 :     GEN c = gel(x,j);
    1175     4748210 :     for (i=1; i<j; )
    1176     2865282 :       if (signe(gel(c,i++))) return 0;
    1177             :     /* i = j */
    1178      887221 :       if (!equali1(gel(c,i++))) return 0;
    1179     4554534 :     for (   ; i<lx; )
    1180     2866521 :       if (signe(gel(c,i++))) return 0;
    1181             :   }
    1182      100949 :   return 1;
    1183             : }
    1184             : int
    1185       14784 : ZM_isscalar(GEN x, GEN s)
    1186             : {
    1187       14784 :   long i, j, lx = lg(x);
    1188             : 
    1189       14784 :   if (lx == 1) return 1;
    1190       14784 :   if (lx != lgcols(x)) return 0;
    1191       14784 :   if (!s) s = gcoeff(x,1,1);
    1192      122727 :   for (j=1; j<lx; j++)
    1193             :   {
    1194      108006 :     GEN c = gel(x,j);
    1195     1264437 :     for (i=1; i<j; )
    1196     1048441 :       if (signe(gel(c,i++))) return 0;
    1197             :     /* i = j */
    1198      107990 :       if (!equalii(gel(c,i++), s)) return 0;
    1199     1264619 :     for (   ; i<lx; )
    1200     1048731 :       if (signe(gel(c,i++))) return 0;
    1201             :   }
    1202       14721 :   return 1;
    1203             : }
    1204             : 
    1205             : 
    1206             : long
    1207       37548 : ZC_is_ei(GEN x)
    1208             : {
    1209       37548 :   long i, j = 0, l = lg(x);
    1210      605416 :   for (i = 1; i < l; i++)
    1211             :   {
    1212      567868 :     GEN c = gel(x,i);
    1213      567868 :     long s = signe(c);
    1214      567868 :     if (!s) continue;
    1215       37541 :     if (s < 0 || !is_pm1(c) || j) return 0;
    1216       37541 :     j = i;
    1217             :   }
    1218       37548 :   return j;
    1219             : }
    1220             : 
    1221             : /********************************************************************/
    1222             : /**                                                                **/
    1223             : /**                       MISCELLANEOUS                            **/
    1224             : /**                                                                **/
    1225             : /********************************************************************/
    1226             : /* assume lg(x) = lg(y), x,y in Z^n */
    1227             : int
    1228      609162 : ZV_cmp(GEN x, GEN y)
    1229             : {
    1230      609162 :   long fl,i, lx = lg(x);
    1231     1337646 :   for (i=1; i<lx; i++)
    1232     1068806 :     if (( fl = cmpii(gel(x,i), gel(y,i)) )) return fl;
    1233      268840 :   return 0;
    1234             : }
    1235             : /* assume lg(x) = lg(y), x,y in Z^n */
    1236             : int
    1237        3480 : ZV_abscmp(GEN x, GEN y)
    1238             : {
    1239        3480 :   long fl,i, lx = lg(x);
    1240       19454 :   for (i=1; i<lx; i++)
    1241       19374 :     if (( fl = absi_cmp(gel(x,i), gel(y,i)) )) return fl;
    1242          80 :   return 0;
    1243             : }
    1244             : 
    1245             : long
    1246      716818 : zv_content(GEN x)
    1247             : {
    1248      716818 :   long i, s, l = lg(x);
    1249      716818 :   if (l == 1) return 0;
    1250      716811 :   s = labs(x[1]);
    1251      716811 :   for (i=2; i<l && s!=1; i++) s = cgcd(x[i],s);
    1252      716811 :   return s;
    1253             : }
    1254             : GEN
    1255        9240 : ZV_content(GEN x)
    1256             : {
    1257        9240 :   long i, l = lg(x);
    1258        9240 :   pari_sp av = avma;
    1259             :   GEN c;
    1260        9240 :   if (l == 1) return gen_0;
    1261        9240 :   if (l == 2) return absi(gel(x,1));
    1262        3374 :   c = gel(x,1);
    1263        6279 :   for (i = 2; i < l; i++)
    1264             :   {
    1265        4053 :     c = gcdii(c, gel(x,i));
    1266        4053 :     if (is_pm1(c)) { avma = av; return gen_1; }
    1267             :   }
    1268        2226 :   return gerepileuptoint(av, c);
    1269             : }
    1270             : 
    1271             : GEN
    1272      740037 : ZM_det_triangular(GEN mat)
    1273             : {
    1274             :   pari_sp av;
    1275      740037 :   long i,l = lg(mat);
    1276             :   GEN s;
    1277             : 
    1278      740037 :   if (l<3) return l<2? gen_1: icopy(gcoeff(mat,1,1));
    1279      674001 :   av = avma; s = gcoeff(mat,1,1);
    1280      674001 :   for (i=2; i<l; i++) s = mulii(s,gcoeff(mat,i,i));
    1281      674001 :   return gerepileuptoint(av,s);
    1282             : }
    1283             : 
    1284             : /* assumes no overflow */
    1285             : long
    1286      291249 : zv_prod(GEN v)
    1287             : {
    1288      291249 :   long n, i, l = lg(v);
    1289      291249 :   if (l == 1) return 1;
    1290      211281 :   n = v[1]; for (i = 2; i < l; i++) n *= v[i];
    1291      211281 :   return n;
    1292             : }
    1293             : 
    1294             : static GEN
    1295      905015 : _mulii(void *E, GEN a, GEN b)
    1296      905015 : { (void) E; return mulii(a, b); }
    1297             : 
    1298             : /* product of ulongs */
    1299             : GEN
    1300      365170 : zv_prod_Z(GEN v)
    1301             : {
    1302      365170 :   pari_sp av = avma;
    1303      365170 :   long k, n = lg(v)-1, m;
    1304             :   GEN x;
    1305      365170 :   if (n == 0) return gen_1;
    1306      345710 :   if (n == 1) return utoi(v[1]);
    1307      288905 :   if (n == 2) return muluu(v[1], v[2]);
    1308      163444 :   m = n >> 1;
    1309      163444 :   x = cgetg(m + (odd(n)? 2: 1), t_VEC);
    1310      163444 :   for (k = 1; k <= m; k++) gel(x,k) = muluu(v[k<<1], v[(k<<1)-1]);
    1311      163444 :   if (odd(n)) gel(x,k) = utoipos(v[n]);
    1312      163444 :   return gerepileuptoint(av, gen_product(x, NULL, _mulii));
    1313             : }
    1314             : 
    1315             : GEN
    1316       35929 : ZV_prod(GEN v)
    1317             : {
    1318       35929 :   pari_sp av = avma;
    1319       35929 :   long i, l = lg(v);
    1320             :   GEN n;
    1321       35929 :   if (l == 1) return gen_1;
    1322       34662 :   if (l > 7) return gerepileuptoint(av, gen_product(v, NULL, _mulii));
    1323       27192 :   n = gel(v,1);
    1324       27192 :   if (l == 2) return icopy(n);
    1325       14739 :   for (i = 2; i < l; i++) n = mulii(n, gel(v,i));
    1326       14739 :   return gerepileuptoint(av, n);
    1327             : }
    1328             : /* assumes no overflow */
    1329             : long
    1330        1582 : zv_sum(GEN v)
    1331             : {
    1332        1582 :   long n, i, l = lg(v);
    1333        1582 :   if (l == 1) return 0;
    1334        1561 :   n = v[1]; for (i = 2; i < l; i++) n += v[i];
    1335        1561 :   return n;
    1336             : }
    1337             : GEN
    1338          56 : ZV_sum(GEN v)
    1339             : {
    1340          56 :   pari_sp av = avma;
    1341          56 :   long i, l = lg(v);
    1342             :   GEN n;
    1343          56 :   if (l == 1) return gen_0;
    1344          56 :   n = gel(v,1);
    1345          56 :   if (l == 2) return icopy(n);
    1346          56 :   for (i = 2; i < l; i++) n = addii(n, gel(v,i));
    1347          56 :   return gerepileuptoint(av, n);
    1348             : }
    1349             : 
    1350             : /********************************************************************/
    1351             : /**                                                                **/
    1352             : /**         GRAM SCHMIDT REDUCTION (integer matrices)              **/
    1353             : /**                                                                **/
    1354             : /********************************************************************/
    1355             : 
    1356             : /* L[k,] += q * L[l,], l < k. Inefficient if q = 0 */
    1357             : static void
    1358      268331 : Zupdate_row(long k, long l, GEN q, GEN L, GEN B)
    1359             : {
    1360      268331 :   long i, qq = itos_or_0(q);
    1361      268331 :   if (!qq)
    1362             :   {
    1363        5053 :     for(i=1;i<l;i++)  gcoeff(L,k,i) = addii(gcoeff(L,k,i),mulii(q,gcoeff(L,l,i)));
    1364        5053 :     gcoeff(L,k,l) = addii(gcoeff(L,k,l), mulii(q,B));
    1365      273384 :     return;
    1366             :   }
    1367      263278 :   if (qq == 1) {
    1368       35094 :     for (i=1;i<l; i++) gcoeff(L,k,i) = addii(gcoeff(L,k,i),gcoeff(L,l,i));
    1369       35094 :     gcoeff(L,k,l) = addii(gcoeff(L,k,l), B);
    1370      228184 :   } else if (qq == -1) {
    1371       83502 :     for (i=1;i<l; i++) gcoeff(L,k,i) = subii(gcoeff(L,k,i),gcoeff(L,l,i));
    1372       83502 :     gcoeff(L,k,l) = addii(gcoeff(L,k,l), negi(B));
    1373             :   } else {
    1374      144682 :     for(i=1;i<l;i++) gcoeff(L,k,i) = addii(gcoeff(L,k,i),mulsi(qq,gcoeff(L,l,i)));
    1375      144682 :     gcoeff(L,k,l) = addii(gcoeff(L,k,l), mulsi(qq,B));
    1376             :   }
    1377             : }
    1378             : 
    1379             : /* update L[k,] */
    1380             : static void
    1381      732176 : ZRED(long k, long l, GEN x, GEN L, GEN B)
    1382             : {
    1383      732176 :   GEN q = truedivii(addii(B,shifti(gcoeff(L,k,l),1)), shifti(B,1));
    1384     1464352 :   if (!signe(q)) return;
    1385      268331 :   q = negi(q);
    1386      268331 :   Zupdate_row(k,l,q,L,B);
    1387      268331 :   gel(x,k) = ZC_lincomb(gen_1, q, gel(x,k), gel(x,l));
    1388             : }
    1389             : 
    1390             : /* Gram-Schmidt reduction, x a ZM */
    1391             : static void
    1392     1009414 : ZincrementalGS(GEN x, GEN L, GEN B, long k)
    1393             : {
    1394             :   long i, j;
    1395     3459479 :   for (j=1; j<=k; j++)
    1396             :   {
    1397     2450065 :     pari_sp av = avma;
    1398     2450065 :     GEN u = ZV_dotproduct(gel(x,k), gel(x,j));
    1399     5378098 :     for (i=1; i<j; i++)
    1400             :     {
    1401     2928033 :       u = subii(mulii(gel(B,i+1), u), mulii(gcoeff(L,k,i), gcoeff(L,j,i)));
    1402     2928033 :       u = diviiexact(u, gel(B,i));
    1403             :     }
    1404     2450065 :     gcoeff(L,k,j) = gerepileuptoint(av, u);
    1405             :   }
    1406     1009414 :   gel(B,k+1) = gcoeff(L,k,k); gcoeff(L,k,k) = gen_1;
    1407     1009414 : }
    1408             : 
    1409             : /* Variant reducemodinvertible(ZC v, ZM y), when y singular.
    1410             :  * Very inefficient if y is not LLL-reduced of maximal rank */
    1411             : static GEN
    1412      280913 : ZC_reducemodmatrix_i(GEN v, GEN y)
    1413             : {
    1414      280913 :   GEN B, L, x = shallowconcat(y, v);
    1415      280913 :   long k, lx = lg(x), nx = lx-1;
    1416             : 
    1417      280913 :   B = scalarcol_shallow(gen_1, lx);
    1418      280913 :   L = zeromatcopy(nx, nx);
    1419      280913 :   for (k=1; k <= nx; k++) ZincrementalGS(x, L, B, k);
    1420      280913 :   for (k = nx-1; k >= 1; k--) ZRED(nx,k, x,L,gel(B,k+1));
    1421      280913 :   return gel(x,nx);
    1422             : }
    1423             : GEN
    1424      280913 : ZC_reducemodmatrix(GEN v, GEN y) {
    1425      280913 :   pari_sp av = avma;
    1426      280913 :   return gerepilecopy(av, ZC_reducemodmatrix_i(v,y));
    1427             : }
    1428             : 
    1429             : /* Variant reducemodinvertible(ZM v, ZM y), when y singular.
    1430             :  * Very inefficient if y is not LLL-reduced of maximal rank */
    1431             : static GEN
    1432        5404 : ZM_reducemodmatrix_i(GEN v, GEN y)
    1433             : {
    1434             :   GEN B, L, V;
    1435        5404 :   long j, k, lv = lg(v), nx = lg(y), lx = nx+1;
    1436             : 
    1437        5404 :   V = cgetg(lv, t_MAT);
    1438        5404 :   B = scalarcol_shallow(gen_1, lx);
    1439        5404 :   L = zeromatcopy(nx, nx);
    1440        5404 :   for (k=1; k < nx; k++) ZincrementalGS(y, L, B, k);
    1441       17220 :   for (j = 1; j < lg(v); j++)
    1442             :   {
    1443       11816 :     GEN x = shallowconcat(y, gel(v,j));
    1444       11816 :     ZincrementalGS(x, L, B, nx); /* overwrite last */
    1445       11816 :     for (k = nx-1; k >= 1; k--) ZRED(nx,k, x,L,gel(B,k+1));
    1446       11816 :     gel(V,j) = gel(x,nx);
    1447             :   }
    1448        5404 :   return V;
    1449             : }
    1450             : GEN
    1451        5404 : ZM_reducemodmatrix(GEN v, GEN y) {
    1452        5404 :   pari_sp av = avma;
    1453        5404 :   return gerepilecopy(av, ZM_reducemodmatrix_i(v,y));
    1454             : }
    1455             : 
    1456             : GEN
    1457      279359 : ZC_reducemodlll(GEN x,GEN y)
    1458             : {
    1459      279359 :   pari_sp av = avma;
    1460      279359 :   GEN z = ZC_reducemodmatrix(x, ZM_lll(y, 0.75, LLL_INPLACE));
    1461      279359 :   return gerepilecopy(av, z);
    1462             : }
    1463             : GEN
    1464           0 : ZM_reducemodlll(GEN x,GEN y)
    1465             : {
    1466           0 :   pari_sp av = avma;
    1467           0 :   GEN z = ZM_reducemodmatrix(x, ZM_lll(y, 0.75, LLL_INPLACE));
    1468           0 :   return gerepilecopy(av, z);
    1469             : }

Generated by: LCOV version 1.11