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 - ZV.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 24988-2584e74448) Lines: 801 901 88.9 %
Date: 2020-01-26 05:57:03 Functions: 120 130 92.3 %
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     1282978 : check_ZV(GEN x, long l)
      19             : {
      20             :   long i;
      21     8239532 :   for (i=1; i<l; i++)
      22     6956645 :     if (typ(gel(x,i)) != t_INT) return 0;
      23     1282887 :   return 1;
      24             : }
      25             : void
      26      520451 : RgV_check_ZV(GEN A, const char *s)
      27             : {
      28      520451 :   if (!RgV_is_ZV(A)) pari_err_TYPE(stack_strcat(s," [integer vector]"), A);
      29      520444 : }
      30             : void
      31      369322 : RgM_check_ZM(GEN A, const char *s)
      32             : {
      33      369322 :   long n = lg(A);
      34      369322 :   if (n != 1)
      35             :   {
      36      369266 :     long j, m = lgcols(A);
      37     1652153 :     for (j=1; j<n; j++)
      38     1282978 :       if (!check_ZV(gel(A,j), m))
      39          91 :         pari_err_TYPE(stack_strcat(s," [integer matrix]"), A);
      40             :   }
      41      369231 : }
      42             : 
      43             : static long
      44    62802062 : ZV_max_lg_i(GEN x, long m)
      45             : {
      46    62802062 :   long i, prec = 2;
      47    62802062 :   for (i=1; i<m; i++) { long l = lgefint(gel(x,i)); if (l > prec) prec = l; }
      48    62802062 :   return prec;
      49             : }
      50             : 
      51             : long
      52       10612 : ZV_max_lg(GEN x)
      53       10612 : { return ZV_max_lg_i(x, lg(x)); }
      54             : 
      55             : static long
      56    21213316 : ZM_max_lg_i(GEN x, long n, long m)
      57             : {
      58    21213316 :   long prec = 2;
      59    21213316 :   if (n != 1)
      60             :   {
      61             :     long j;
      62    84004766 :     for (j=1; j<n; j++)
      63             :     {
      64    62791450 :       long l = ZV_max_lg_i(gel(x,j), m);
      65    62791450 :       if (l > prec) prec = l;
      66             :     }
      67             :   }
      68    21213316 :   return prec;
      69             : }
      70             : 
      71             : long
      72      530162 : ZM_max_lg(GEN x)
      73             : {
      74      530162 :   long n = lg(x);
      75      530162 :   if (n==1) return 2;
      76      530162 :   return ZM_max_lg_i(x, n, lgcols(x));
      77             : }
      78             : 
      79             : GEN
      80        3394 : ZM_supnorm(GEN x)
      81             : {
      82        3394 :   long i, j, h, lx = lg(x);
      83        3394 :   GEN s = gen_0;
      84        3394 :   if (lx == 1) return gen_1;
      85        3394 :   h = lgcols(x);
      86       20533 :   for (j=1; j<lx; j++)
      87             :   {
      88       17139 :     GEN xj = gel(x,j);
      89      240192 :     for (i=1; i<h; i++)
      90             :     {
      91      223053 :       GEN c = gel(xj,i);
      92      223053 :       if (abscmpii(c, s) > 0) s = c;
      93             :     }
      94             :   }
      95        3394 :   return absi(s);
      96             : }
      97             : 
      98             : /********************************************************************/
      99             : /**                                                                **/
     100             : /**                           MULTIPLICATION                       **/
     101             : /**                                                                **/
     102             : /********************************************************************/
     103             : /* x non-empty ZM, y a compatible nc (dimension > 0). */
     104             : static GEN
     105           0 : ZM_nc_mul_i(GEN x, GEN y, long c, long l)
     106             : {
     107             :   long i, j;
     108             :   pari_sp av;
     109           0 :   GEN z = cgetg(l,t_COL), s;
     110             : 
     111           0 :   for (i=1; i<l; i++)
     112             :   {
     113           0 :     av = avma; s = muliu(gcoeff(x,i,1),y[1]);
     114           0 :     for (j=2; j<c; j++)
     115           0 :       if (y[j]) s = addii(s, muliu(gcoeff(x,i,j),y[j]));
     116           0 :     gel(z,i) = gerepileuptoint(av,s);
     117             :   }
     118           0 :   return z;
     119             : }
     120             : 
     121             : /* x ZV, y a compatible zc. */
     122             : GEN
     123     2214660 : ZV_zc_mul(GEN x, GEN y)
     124             : {
     125     2214660 :   long j, l = lg(x);
     126     2214660 :   pari_sp av = avma;
     127     2214660 :   GEN s = mulis(gel(x,1),y[1]);
     128    50028650 :   for (j=2; j<l; j++)
     129    47813990 :     if (y[j]) s = addii(s, mulis(gel(x,j),y[j]));
     130     2214660 :   return gerepileuptoint(av,s);
     131             : }
     132             : 
     133             : /* x non-empty ZM, y a compatible zc (dimension > 0). */
     134             : static GEN
     135     4916262 : ZM_zc_mul_i(GEN x, GEN y, long c, long l)
     136             : {
     137             :   long i, j;
     138     4916262 :   GEN z = cgetg(l,t_COL);
     139             : 
     140    27461636 :   for (i=1; i<l; i++)
     141             :   {
     142    22545374 :     pari_sp av = avma;
     143    22545374 :     GEN s = mulis(gcoeff(x,i,1),y[1]);
     144   400045173 :     for (j=2; j<c; j++)
     145   377499799 :       if (y[j]) s = addii(s, mulis(gcoeff(x,i,j),y[j]));
     146    22545374 :     gel(z,i) = gerepileuptoint(av,s);
     147             :   }
     148     4916262 :   return z;
     149             : }
     150             : GEN
     151     3374000 : ZM_zc_mul(GEN x, GEN y) {
     152     3374000 :   long l = lg(x);
     153     3374000 :   if (l == 1) return cgetg(1, t_COL);
     154     3374000 :   return ZM_zc_mul_i(x,y, l, lgcols(x));
     155             : }
     156             : 
     157             : /* y non-empty ZM, x a compatible zv (dimension > 0). */
     158             : GEN
     159        1260 : zv_ZM_mul(GEN x, GEN y) {
     160        1260 :   long i,j, lx = lg(x), ly = lg(y);
     161             :   GEN z;
     162        1260 :   if (lx == 1) return zerovec(ly-1);
     163        1260 :   z = cgetg(ly,t_VEC);
     164        3080 :   for (j=1; j<ly; j++)
     165             :   {
     166        1820 :     pari_sp av = avma;
     167        1820 :     GEN s = mulsi(x[1], gcoeff(y,1,j));
     168        3472 :     for (i=2; i<lx; i++)
     169        1652 :       if (x[i]) s = addii(s, mulsi(x[i], gcoeff(y,i,j)));
     170        1820 :     gel(z,j) = gerepileuptoint(av,s);
     171             :   }
     172        1260 :   return z;
     173             : }
     174             : 
     175             : /* x ZM, y a compatible zm (dimension > 0). */
     176             : GEN
     177      754066 : ZM_zm_mul(GEN x, GEN y)
     178             : {
     179      754066 :   long j, c, l = lg(x), ly = lg(y);
     180      754066 :   GEN z = cgetg(ly, t_MAT);
     181      754066 :   if (l == 1) return z;
     182      754066 :   c = lgcols(x);
     183      754066 :   for (j = 1; j < ly; j++) gel(z,j) = ZM_zc_mul_i(x, gel(y,j), l,c);
     184      754066 :   return z;
     185             : }
     186             : /* x ZM, y a compatible zn (dimension > 0). */
     187             : GEN
     188           0 : ZM_nm_mul(GEN x, GEN y)
     189             : {
     190           0 :   long j, c, l = lg(x), ly = lg(y);
     191           0 :   GEN z = cgetg(ly, t_MAT);
     192           0 :   if (l == 1) return z;
     193           0 :   c = lgcols(x);
     194           0 :   for (j = 1; j < ly; j++) gel(z,j) = ZM_nc_mul_i(x, gel(y,j), l,c);
     195           0 :   return z;
     196             : }
     197             : 
     198             : /* Strassen-Winograd algorithm */
     199             : 
     200             : /*
     201             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     202             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     203             : */
     204             : static GEN
     205      348176 : add_slices(long m, long n,
     206             :            GEN A, long ma, long da, long na, long ea,
     207             :            GEN B, long mb, long db, long nb, long eb)
     208             : {
     209      348176 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     210      348176 :   GEN M = cgetg(n + 1, t_MAT), C;
     211             : 
     212     2669371 :   for (j = 1; j <= min_e; j++) {
     213     2321195 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     214    35886051 :     for (i = 1; i <= min_d; i++)
     215    67129712 :       gel(C, i) = addii(gcoeff(A, ma + i, na + j),
     216    33564856 :                         gcoeff(B, mb + i, nb + j));
     217     2350359 :     for (; i <= da; i++)
     218       29164 :       gel(C, i) = gcoeff(A, ma + i, na + j);
     219     2321195 :     for (; i <= db; i++)
     220           0 :       gel(C, i) = gcoeff(B, mb + i, nb + j);
     221     2321195 :     for (; i <= m; i++)
     222           0 :       gel(C, i) = gen_0;
     223             :   }
     224      368878 :   for (; j <= ea; j++) {
     225       20702 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     226       76160 :     for (i = 1; i <= da; i++)
     227       55458 :       gel(C, i) = gcoeff(A, ma + i, na + j);
     228       20702 :     for (; i <= m; i++)
     229           0 :       gel(C, i) = gen_0;
     230             :   }
     231      348176 :   for (; j <= eb; j++) {
     232           0 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     233           0 :     for (i = 1; i <= db; i++)
     234           0 :       gel(C, i) = gcoeff(B, mb + i, nb + j);
     235           0 :     for (; i <= m; i++)
     236           0 :       gel(C, i) = gen_0;
     237             :   }
     238      348176 :   for (; j <= n; j++)
     239           0 :     gel(M, j) = zerocol(m);
     240      348176 :   return M;
     241             : }
     242             : 
     243             : /*
     244             :   Return A[ma+1..ma+da, na+1..na+ea] - B[mb+1..mb+db, nb+1..nb+eb]
     245             :   as an (m x n)-matrix, padding the input with zeroes as necessary.
     246             : */
     247             : static GEN
     248      304654 : subtract_slices(long m, long n,
     249             :                 GEN A, long ma, long da, long na, long ea,
     250             :                 GEN B, long mb, long db, long nb, long eb)
     251             : {
     252      304654 :   long min_d = minss(da, db), min_e = minss(ea, eb), i, j;
     253      304654 :   GEN M = cgetg(n + 1, t_MAT), C;
     254             : 
     255     2323547 :   for (j = 1; j <= min_e; j++) {
     256     2018893 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     257    32494743 :     for (i = 1; i <= min_d; i++)
     258    60951700 :       gel(C, i) = subii(gcoeff(A, ma + i, na + j),
     259    30475850 :                         gcoeff(B, mb + i, nb + j));
     260     2039138 :     for (; i <= da; i++)
     261       20245 :       gel(C, i) = gcoeff(A, ma + i, na + j);
     262     2051576 :     for (; i <= db; i++)
     263       32683 :       gel(C, i) = negi(gcoeff(B, mb + i, nb + j));
     264     2018893 :     for (; i <= m; i++)
     265           0 :       gel(C, i) = gen_0;
     266             :   }
     267      304654 :   for (; j <= ea; j++) {
     268           0 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     269           0 :     for (i = 1; i <= da; i++)
     270           0 :       gel(C, i) = gcoeff(A, ma + i, na + j);
     271           0 :     for (; i <= m; i++)
     272           0 :       gel(C, i) = gen_0;
     273             :   }
     274      320382 :   for (; j <= eb; j++) {
     275       15728 :     gel(M, j) = C = cgetg(m + 1, t_COL);
     276       49937 :     for (i = 1; i <= db; i++)
     277       34209 :       gel(C, i) = negi(gcoeff(B, mb + i, nb + j));
     278       15728 :     for (; i <= m; i++)
     279           0 :       gel(C, i) = gen_0;
     280             :   }
     281      320382 :   for (; j <= n; j++)
     282       15728 :     gel(M, j) = zerocol(m);
     283      304654 :   return M;
     284             : }
     285             : 
     286             : static GEN ZM_mul_i(GEN x, GEN y, long l, long lx, long ly);
     287             : 
     288             : /* Strassen-Winograd matrix product A (m x n) * B (n x p) */
     289             : static GEN
     290       43522 : ZM_mul_sw(GEN A, GEN B, long m, long n, long p)
     291             : {
     292       43522 :   pari_sp av = avma;
     293       43522 :   long m1 = (m + 1)/2, m2 = m/2,
     294       43522 :     n1 = (n + 1)/2, n2 = n/2,
     295       43522 :     p1 = (p + 1)/2, p2 = p/2;
     296             :   GEN A11, A12, A22, B11, B21, B22,
     297             :     S1, S2, S3, S4, T1, T2, T3, T4,
     298             :     M1, M2, M3, M4, M5, M6, M7,
     299             :     V1, V2, V3, C11, C12, C21, C22, C;
     300             : 
     301       43522 :   T2 = subtract_slices(n1, p2, B, 0, n1, p1, p2, B, n1, n2, p1, p2);
     302       43522 :   S1 = subtract_slices(m2, n1, A, m1, m2, 0, n1, A, 0, m2, 0, n1);
     303       43522 :   M2 = ZM_mul_i(S1, T2, m2 + 1, n1 + 1, p2 + 1);
     304       43522 :   if (gc_needed(av, 1))
     305           0 :     gerepileall(av, 2, &T2, &M2);  /* destroy S1 */
     306       43522 :   T3 = subtract_slices(n1, p1, T2, 0, n1, 0, p2, B, 0, n1, 0, p1);
     307       43522 :   if (gc_needed(av, 1))
     308           0 :     gerepileall(av, 2, &M2, &T3);  /* destroy T2 */
     309       43522 :   S2 = add_slices(m2, n1, A, m1, m2, 0, n1, A, m1, m2, n1, n2);
     310       43522 :   T1 = subtract_slices(n1, p1, B, 0, n1, p1, p2, B, 0, n1, 0, p2);
     311       43522 :   M3 = ZM_mul_i(S2, T1, m2 + 1, n1 + 1, p2 + 1);
     312       43522 :   if (gc_needed(av, 1))
     313           0 :     gerepileall(av, 4, &M2, &T3, &S2, &M3);  /* destroy T1 */
     314       43522 :   S3 = subtract_slices(m1, n1, S2, 0, m2, 0, n1, A, 0, m1, 0, n1);
     315       43522 :   if (gc_needed(av, 1))
     316           0 :     gerepileall(av, 4, &M2, &T3, &M3, &S3);  /* destroy S2 */
     317       43522 :   A11 = matslice(A, 1, m1, 1, n1);
     318       43522 :   B11 = matslice(B, 1, n1, 1, p1);
     319       43522 :   M1 = ZM_mul_i(A11, B11, m1 + 1, n1 + 1, p1 + 1);
     320       43522 :   if (gc_needed(av, 1))
     321           0 :     gerepileall(av, 5, &M2, &T3, &M3, &S3, &M1);  /* destroy A11, B11 */
     322       43522 :   A12 = matslice(A, 1, m1, n1 + 1, n);
     323       43522 :   B21 = matslice(B, n1 + 1, n, 1, p1);
     324       43522 :   M4 = ZM_mul_i(A12, B21, m1 + 1, n2 + 1, p1 + 1);
     325       43522 :   if (gc_needed(av, 1))
     326           0 :     gerepileall(av, 6, &M2, &T3, &M3, &S3, &M1, &M4);  /* destroy A12, B21 */
     327       43522 :   C11 = add_slices(m1, p1, M1, 0, m1, 0, p1, M4, 0, m1, 0, p1);
     328       43522 :   if (gc_needed(av, 1))
     329           0 :     gerepileall(av, 6, &M2, &T3, &M3, &S3, &M1, &C11);  /* destroy M4 */
     330       43522 :   M5 = ZM_mul_i(S3, T3, m1 + 1, n1 + 1, p1 + 1);
     331       43522 :   S4 = subtract_slices(m1, n2, A, 0, m1, n1, n2, S3, 0, m1, 0, n2);
     332       43522 :   if (gc_needed(av, 1))
     333           5 :     gerepileall(av, 7, &M2, &T3, &M3, &M1, &C11, &M5, &S4);  /* destroy S3 */
     334       43522 :   T4 = add_slices(n2, p1, B, n1, n2, 0, p1, T3, 0, n2, 0, p1);
     335       43522 :   if (gc_needed(av, 1))
     336           0 :     gerepileall(av, 7, &M2, &M3, &M1, &C11, &M5, &S4, &T4);  /* destroy T3 */
     337       43522 :   V1 = subtract_slices(m1, p1, M1, 0, m1, 0, p1, M5, 0, m1, 0, p1);
     338       43522 :   if (gc_needed(av, 1))
     339           1 :     gerepileall(av, 6, &M2, &M3, &S4, &T4, &C11, &V1);  /* destroy M1, M5 */
     340       43522 :   B22 = matslice(B, n1 + 1, n, p1 + 1, p);
     341       43522 :   M6 = ZM_mul_i(S4, B22, m1 + 1, n2 + 1, p2 + 1);
     342       43522 :   if (gc_needed(av, 1))
     343           6 :     gerepileall(av, 6, &M2, &M3, &T4, &C11, &V1, &M6);  /* destroy S4, B22 */
     344       43522 :   A22 = matslice(A, m1 + 1, m, n1 + 1, n);
     345       43522 :   M7 = ZM_mul_i(A22, T4, m2 + 1, n2 + 1, p1 + 1);
     346       43522 :   if (gc_needed(av, 1))
     347           0 :     gerepileall(av, 6, &M2, &M3, &C11, &V1, &M6, &M7);  /* destroy A22, T4 */
     348       43522 :   V3 = add_slices(m1, p2, V1, 0, m1, 0, p2, M3, 0, m2, 0, p2);
     349       43522 :   C12 = add_slices(m1, p2, V3, 0, m1, 0, p2, M6, 0, m1, 0, p2);
     350       43522 :   if (gc_needed(av, 1))
     351           6 :     gerepileall(av, 6, &M2, &M3, &C11, &V1, &M7, &C12);  /* destroy V3, M6 */
     352       43522 :   V2 = add_slices(m2, p1, V1, 0, m2, 0, p1, M2, 0, m2, 0, p2);
     353       43522 :   if (gc_needed(av, 1))
     354           0 :     gerepileall(av, 5, &M3, &C11, &M7, &C12, &V2);  /* destroy V1, M2 */
     355       43522 :   C21 = add_slices(m2, p1, V2, 0, m2, 0, p1, M7, 0, m2, 0, p1);
     356       43522 :   if (gc_needed(av, 1))
     357           7 :     gerepileall(av, 5, &M3, &C11, &C12, &V2, &C21);  /* destroy M7 */
     358       43522 :   C22 = add_slices(m2, p2, V2, 0, m2, 0, p2, M3, 0, m2, 0, p2);
     359       43522 :   if (gc_needed(av, 1))
     360           0 :     gerepileall(av, 4, &C11, &C12, &C21, &C22);  /* destroy V2, M3 */
     361       43522 :   C = shallowconcat(vconcat(C11, C21), vconcat(C12, C22));
     362       43522 :   return gerepilecopy(av, C);
     363             : }
     364             : 
     365             : /* x[i,]*y. Assume lg(x) > 1 and 0 < i < lgcols(x) */
     366             : static GEN
     367   280936200 : ZMrow_ZC_mul_i(GEN x, GEN y, long i, long lx)
     368             : {
     369   280936200 :   pari_sp av = avma;
     370   280936200 :   GEN c = mulii(gcoeff(x,i,1), gel(y,1)), ZERO = gen_0;
     371             :   long k;
     372  3064036163 :   for (k = 2; k < lx; k++)
     373             :   {
     374  2783100313 :     GEN t = mulii(gcoeff(x,i,k), gel(y,k));
     375  2783100058 :     if (t != ZERO) c = addii(c, t);
     376             :   }
     377   280935850 :   return gerepileuptoint(av, c);
     378             : }
     379             : GEN
     380    43000129 : ZMrow_ZC_mul(GEN x, GEN y, long i)
     381    43000129 : { return ZMrow_ZC_mul_i(x, y, i, lg(x)); }
     382             : 
     383             : /* return x * y, 1 < lx = lg(x), l = lgcols(x) */
     384             : static GEN
     385    44588967 : ZM_ZC_mul_i(GEN x, GEN y, long lx, long l)
     386             : {
     387    44588967 :   GEN z = cgetg(l,t_COL);
     388             :   long i;
     389    44589000 :   for (i=1; i<l; i++) gel(z,i) = ZMrow_ZC_mul_i(x,y,i,lx);
     390    44588977 :   return z;
     391             : }
     392             : 
     393             : static GEN
     394    10300652 : ZM_mul_classical(GEN x, GEN y, long l, long lx, long ly)
     395             : {
     396             :   long j;
     397    10300652 :   GEN z = cgetg(ly, t_MAT);
     398    38874914 :   for (j = 1; j < ly; j++)
     399    28574262 :     gel(z, j) = ZM_ZC_mul_i(x, gel(y, j), lx, l);
     400    10300652 :   return z;
     401             : }
     402             : 
     403             : static GEN
     404        3356 : ZM_mul_slice(GEN A, GEN B, GEN P, GEN *mod)
     405             : {
     406        3356 :   pari_sp av = avma;
     407        3356 :   long i, n = lg(P)-1;
     408             :   GEN H, T;
     409        3356 :   if (n == 1)
     410             :   {
     411        1174 :     ulong p = uel(P,1);
     412        1174 :     GEN a = ZM_to_Flm(A, p);
     413        1174 :     GEN b = ZM_to_Flm(B, p);
     414        1174 :     GEN Hp = gerepileupto(av, Flm_to_ZM(Flm_mul(a, b, p)));
     415        1174 :     *mod = utoi(p); return Hp;
     416             :   }
     417        2182 :   T = ZV_producttree(P);
     418        2183 :   A = ZM_nv_mod_tree(A, P, T);
     419        2184 :   B = ZM_nv_mod_tree(B, P, T);
     420        2184 :   H = cgetg(n+1, t_VEC);
     421       11632 :   for(i=1; i <= n; i++)
     422        9448 :     gel(H,i) = Flm_mul(gel(A,i),gel(B,i),P[i]);
     423        2184 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
     424        2184 :   *mod = gmael(T, lg(T)-1, 1);
     425        2184 :   gerepileall(av, 2, &H, mod);
     426        2184 :   return H;
     427             : }
     428             : 
     429             : GEN
     430        3356 : ZM_mul_worker(GEN P, GEN A, GEN B)
     431             : {
     432        3356 :   GEN V = cgetg(3, t_VEC);
     433        3356 :   gel(V,1) = ZM_mul_slice(A, B, P, &gel(V,2));
     434        3358 :   return V;
     435             : }
     436             : 
     437             : static long
     438      184423 : ZC_expi(GEN x)
     439             : {
     440      184423 :   long i, l = lg(x), m=-1;
     441    19012752 :   for(i = 1; i < l; i++)
     442             :   {
     443    18828329 :     GEN xi = gel(x,i);
     444    18828329 :     long e = signe(xi) ? expi(xi): -1;
     445    18828329 :     if (e > m) m = e;
     446             :   }
     447      184423 :   return m;
     448             : }
     449             : 
     450             : static long
     451        1908 : ZM_expi(GEN x)
     452             : {
     453        1908 :   long i, l = lg(x), m=-1;
     454      186331 :   for(i = 1; i < l; i++)
     455             :   {
     456      184423 :     long e = ZC_expi(gel(x,i));
     457      184423 :     if (e > m) m = e;
     458             :   }
     459        1908 :   return m;
     460             : }
     461             : 
     462             : static GEN
     463         954 : ZM_mul_fast(GEN A, GEN B, long lA, long lB)
     464             : {
     465         954 :   pari_sp av = avma;
     466             :   forprime_t S;
     467             :   GEN H, worker;
     468         954 :   long h, mA = ZM_expi(A), mB = ZM_expi(B);
     469         954 :   if (mA==-1 || mB==-1) return zeromat(nbrows(A),lB-1);
     470         940 :   h = 3 + mA + mB + expu(lA-1);
     471         940 :   init_modular_big(&S);
     472         940 :   worker = snm_closure(is_entry("_ZM_mul_worker"), mkvec2(A,B));
     473         940 :   H = gen_crt("ZM_mul", worker, &S, NULL, h, lA-1, NULL,
     474             :               nmV_chinese_center, FpM_center);
     475         940 :   return gerepileupto(av, H);
     476             : }
     477             : 
     478             : /* Strassen-Winograd used for dim >= ZM_sw_bound */
     479             : static GEN
     480    10339934 : ZM_mul_i(GEN x, GEN y, long l, long lx, long ly)
     481             : {
     482    10339934 :   if (lx > 70 && ly > 70 && l > 70)
     483         954 :     return ZM_mul_fast(x, y, lx, ly);
     484             :   else
     485             :   {
     486    10338980 :     long s = maxss(ZM_max_lg_i(x,lx,l), ZM_max_lg_i(y,ly,lx));
     487    10338980 :     long ZM_sw_bound = s > 60 ? 2: s > 25 ? 4: s>15 ? 8 : s > 8 ? 16 : 32;
     488    10338980 :     if (l <= ZM_sw_bound || lx <= ZM_sw_bound || ly <= ZM_sw_bound)
     489    10295467 :       return ZM_mul_classical(x, y, l, lx, ly);
     490             :     else
     491       43513 :       return ZM_mul_sw(x, y, l - 1, lx - 1, ly - 1);
     492             :   }
     493             : }
     494             : 
     495             : GEN
     496    10056231 : ZM_mul(GEN x, GEN y)
     497             : {
     498    10056231 :   long lx=lg(x), ly=lg(y);
     499    10056231 :   if (ly==1) return cgetg(1,t_MAT);
     500    10036309 :   if (lx==1) return zeromat(0, ly-1);
     501    10035280 :   return ZM_mul_i(x, y, lgcols(x), lx, ly);
     502             : }
     503             : 
     504             : GEN
     505      139165 : QM_mul(GEN x, GEN y)
     506             : {
     507      139165 :   GEN dx, nx = Q_primitive_part(x, &dx);
     508      139165 :   GEN dy, ny = Q_primitive_part(y, &dy);
     509      139165 :   GEN z = ZM_mul(nx, ny);
     510      139165 :   if (dx || dy)
     511             :   {
     512      139165 :     GEN d = dx ? dy ? gmul(dx, dy): dx : dy;
     513      139165 :     if (!gequal1(d)) z = ZM_Q_mul(z, d);
     514             :   }
     515      139165 :   return z;
     516             : }
     517             : 
     518             : GEN
     519         700 : QM_sqr(GEN x)
     520             : {
     521         700 :   GEN dx, nx = Q_primitive_part(x, &dx);
     522         700 :   GEN z = ZM_sqr(nx);
     523         700 :   if (dx)
     524         700 :     z = ZM_Q_mul(z, gsqr(dx));
     525         700 :   return z;
     526             : }
     527             : 
     528             : GEN
     529      116451 : QM_QC_mul(GEN x, GEN y)
     530             : {
     531      116451 :   GEN dx, nx = Q_primitive_part(x, &dx);
     532      116451 :   GEN dy, ny = Q_primitive_part(y, &dy);
     533      116451 :   GEN z = ZM_ZC_mul(nx, ny);
     534      116451 :   if (dx || dy)
     535             :   {
     536      116423 :     GEN d = dx ? dy ? gmul(dx, dy): dx : dy;
     537      116423 :     if (!gequal1(d)) z = ZC_Q_mul(z, d);
     538             :   }
     539      116451 :   return z;
     540             : }
     541             : 
     542             : /* assume result is symmetric */
     543             : GEN
     544           0 : ZM_multosym(GEN x, GEN y)
     545             : {
     546           0 :   long j, lx, ly = lg(y);
     547             :   GEN M;
     548           0 :   if (ly == 1) return cgetg(1,t_MAT);
     549           0 :   lx = lg(x); /* = lgcols(y) */
     550           0 :   if (lx == 1) return cgetg(1,t_MAT);
     551             :   /* ly = lgcols(x) */
     552           0 :   M = cgetg(ly, t_MAT);
     553           0 :   for (j=1; j<ly; j++)
     554             :   {
     555           0 :     GEN z = cgetg(ly,t_COL), yj = gel(y,j);
     556             :     long i;
     557           0 :     for (i=1; i<j; i++) gel(z,i) = gcoeff(M,j,i);
     558           0 :     for (i=j; i<ly; i++)gel(z,i) = ZMrow_ZC_mul_i(x,yj,i,lx);
     559           0 :     gel(M,j) = z;
     560             :   }
     561           0 :   return M;
     562             : }
     563             : 
     564             : /* compute m*diagonal(d), assume lg(d) = lg(m). Shallow */
     565             : GEN
     566          14 : ZM_mul_diag(GEN m, GEN d)
     567             : {
     568             :   long j, l;
     569          14 :   GEN y = cgetg_copy(m, &l);
     570          56 :   for (j=1; j<l; j++)
     571             :   {
     572          42 :     GEN c = gel(d,j);
     573          42 :     gel(y,j) = equali1(c)? gel(m,j): ZC_Z_mul(gel(m,j), c);
     574             :   }
     575          14 :   return y;
     576             : }
     577             : /* compute diagonal(d)*m, assume lg(d) = lg(m~). Shallow */
     578             : GEN
     579      136107 : ZM_diag_mul(GEN d, GEN m)
     580             : {
     581      136107 :   long i, j, l = lg(d), lm = lg(m);
     582      136107 :   GEN y = cgetg(lm, t_MAT);
     583      136107 :   for (j=1; j<lm; j++) gel(y,j) = cgetg(l, t_COL);
     584      443120 :   for (i=1; i<l; i++)
     585             :   {
     586      307013 :     GEN c = gel(d,i);
     587      307013 :     if (equali1(c))
     588       16128 :       for (j=1; j<lm; j++) gcoeff(y,i,j) = gcoeff(m,i,j);
     589             :     else
     590      290885 :       for (j=1; j<lm; j++) gcoeff(y,i,j) = mulii(gcoeff(m,i,j), c);
     591             :   }
     592      136107 :   return y;
     593             : }
     594             : 
     595             : /* assume lx > 1 is lg(x) = lg(y) */
     596             : static GEN
     597    14388223 : ZV_dotproduct_i(GEN x, GEN y, long lx)
     598             : {
     599    14388223 :   pari_sp av = avma;
     600    14388223 :   GEN c = mulii(gel(x,1), gel(y,1));
     601             :   long i;
     602   115894264 :   for (i = 2; i < lx; i++)
     603             :   {
     604   101506041 :     GEN t = mulii(gel(x,i), gel(y,i));
     605   101506041 :     if (t != gen_0) c = addii(c, t);
     606             :   }
     607    14388223 :   return gerepileuptoint(av, c);
     608             : }
     609             : 
     610             : /* x~ * y, assuming result is symmetric */
     611             : GEN
     612      161705 : ZM_transmultosym(GEN x, GEN y)
     613             : {
     614      161705 :   long i, j, l, ly = lg(y);
     615             :   GEN M;
     616      161705 :   if (ly == 1) return cgetg(1,t_MAT);
     617             :   /* lg(x) = ly */
     618      161705 :   l = lgcols(y); /* = lgcols(x) */
     619      161705 :   M = cgetg(ly, t_MAT);
     620     1266121 :   for (i=1; i<ly; i++)
     621             :   {
     622     1104416 :     GEN xi = gel(x,i), c = cgetg(ly,t_COL);
     623     1104416 :     gel(M,i) = c;
     624     5066476 :     for (j=1; j<i; j++)
     625     3962060 :       gcoeff(M,i,j) = gel(c,j) = ZV_dotproduct_i(xi,gel(y,j),l);
     626     1104416 :     gel(c,i) = ZV_dotproduct_i(xi,gel(y,i),l);
     627             :   }
     628      161705 :   return M;
     629             : }
     630             : /* x~ * y */
     631             : GEN
     632         497 : ZM_transmul(GEN x, GEN y)
     633             : {
     634         497 :   long i, j, l, lx, ly = lg(y);
     635             :   GEN M;
     636         497 :   if (ly == 1) return cgetg(1,t_MAT);
     637         497 :   lx = lg(x);
     638         497 :   l = lgcols(y);
     639         497 :   if (lgcols(x) != l) pari_err_OP("operation 'ZM_transmul'", x,y);
     640         497 :   M = cgetg(ly, t_MAT);
     641        1617 :   for (i=1; i<ly; i++)
     642             :   {
     643        1120 :     GEN yi = gel(y,i), c = cgetg(lx,t_COL);
     644        1120 :     gel(M,i) = c;
     645        1120 :     for (j=1; j<lx; j++) gel(c,j) = ZV_dotproduct_i(yi,gel(x,j),l);
     646             :   }
     647         497 :   return M;
     648             : }
     649             : 
     650             : static GEN
     651        5194 : ZM_sqr_i(GEN x, long l)
     652             : {
     653        5194 :   if (l > 70)
     654           0 :     return ZM_mul_fast(x, x, l, l);
     655             :   else
     656             :   {
     657        5194 :     long s = ZM_max_lg_i(x,l,l);
     658        5194 :     long ZM_sw_bound = s > 60 ? 2: s > 25 ? 4: s>15 ? 8 : s > 8 ? 16 : 32;
     659        5194 :     if (l <= ZM_sw_bound)
     660        5185 :       return ZM_mul_classical(x, x, l, l, l);
     661             :     else
     662           9 :       return ZM_mul_sw(x, x, l - 1, l - 1, l - 1);
     663             :   }
     664             : }
     665             : 
     666             : GEN
     667        5194 : ZM_sqr(GEN x)
     668             : {
     669        5194 :   long lx=lg(x);
     670        5194 :   if (lx==1) return cgetg(1,t_MAT);
     671        5194 :   return ZM_sqr_i(x, lx);
     672             : }
     673             : GEN
     674    16014709 : ZM_ZC_mul(GEN x, GEN y)
     675             : {
     676    16014709 :   long lx = lg(x);
     677    16014709 :   return lx==1? cgetg(1,t_COL): ZM_ZC_mul_i(x, y, lx, lgcols(x));
     678             : }
     679             : 
     680             : GEN
     681     1294733 : ZC_Z_div(GEN x, GEN c)
     682     1294733 : { pari_APPLY_type(t_COL, Qdivii(gel(x,i), c)) }
     683             : 
     684             : GEN
     685       14546 : ZM_Z_div(GEN x, GEN c)
     686       14546 : { pari_APPLY_same(ZC_Z_div(gel(x, i), c)) }
     687             : 
     688             : GEN
     689     1319555 : ZC_Q_mul(GEN A, GEN z)
     690             : {
     691     1319555 :   pari_sp av = avma;
     692     1319555 :   long i, l = lg(A);
     693             :   GEN d, n, Ad, B, u;
     694     1319555 :   if (typ(z)==t_INT) return ZC_Z_mul(A,z);
     695     1317000 :   n = gel(z, 1); d = gel(z, 2);
     696     1317000 :   Ad = FpC_red(A, d);
     697     1317000 :   u = gcdii(d, FpV_factorback(Ad, NULL, d));
     698     1317000 :   B = cgetg(l, t_COL);
     699     1317000 :   if (equali1(u))
     700             :   {
     701      247596 :     for(i=1; i<l; i++)
     702      204070 :       gel(B, i) = mkfrac(mulii(n, gel(A,i)), d);
     703             :   } else
     704             :   {
     705    12223906 :     for(i=1; i<l; i++)
     706             :     {
     707    10950432 :       GEN di = gcdii(gel(Ad, i), u), ni = mulii(n, diviiexact(gel(A,i), di));
     708    10950432 :       if (equalii(d, di))
     709     7854155 :         gel(B, i) = ni;
     710             :       else
     711     3096277 :         gel(B, i) = mkfrac(ni, diviiexact(d, di));
     712             :     }
     713             :   }
     714     1317000 :   return gerepilecopy(av, B);
     715             : }
     716             : 
     717             : GEN
     718      297251 : ZM_Q_mul(GEN x, GEN z)
     719             : {
     720      297251 :   if (typ(z)==t_INT) return ZM_Z_mul(x,z);
     721      198572 :   pari_APPLY_same(ZC_Q_mul(gel(x, i), z));
     722             : }
     723             : 
     724             : long
     725   182226205 : zv_dotproduct(GEN x, GEN y)
     726             : {
     727   182226205 :   long i, lx = lg(x);
     728             :   ulong c;
     729   182226205 :   if (lx == 1) return 0;
     730   182226205 :   c = uel(x,1)*uel(y,1);
     731  2825755660 :   for (i = 2; i < lx; i++)
     732  2643529455 :     c += uel(x,i)*uel(y,i);
     733   182226205 :   return c;
     734             : }
     735             : 
     736             : GEN
     737      164855 : ZV_ZM_mul(GEN x, GEN y)
     738             : {
     739      164855 :   long i, lx = lg(x), ly = lg(y);
     740             :   GEN z;
     741      164855 :   if (lx == 1) return zerovec(ly-1);
     742      164841 :   z = cgetg(ly, t_VEC);
     743      164841 :   for (i = 1; i < ly; i++) gel(z,i) = ZV_dotproduct_i(x, gel(y,i), lx);
     744      164841 :   return z;
     745             : }
     746             : 
     747             : GEN
     748           0 : ZC_ZV_mul(GEN x, GEN y)
     749             : {
     750           0 :   long i,j, lx=lg(x), ly=lg(y);
     751             :   GEN z;
     752           0 :   if (ly==1) return cgetg(1,t_MAT);
     753           0 :   z = cgetg(ly,t_MAT);
     754           0 :   for (j=1; j < ly; j++)
     755             :   {
     756           0 :     gel(z,j) = cgetg(lx,t_COL);
     757           0 :     for (i=1; i<lx; i++) gcoeff(z,i,j) = mulii(gel(x,i),gel(y,j));
     758             :   }
     759           0 :   return z;
     760             : }
     761             : 
     762             : GEN
     763     6310725 : ZV_dotsquare(GEN x)
     764             : {
     765             :   long i, lx;
     766             :   pari_sp av;
     767             :   GEN z;
     768     6310725 :   lx = lg(x);
     769     6310725 :   if (lx == 1) return gen_0;
     770     6310725 :   av = avma; z = sqri(gel(x,1));
     771     6310725 :   for (i=2; i<lx; i++) z = addii(z, sqri(gel(x,i)));
     772     6310725 :   return gerepileuptoint(av,z);
     773             : }
     774             : 
     775             : GEN
     776    12810999 : ZV_dotproduct(GEN x,GEN y)
     777             : {
     778             :   long lx;
     779    12810999 :   if (x == y) return ZV_dotsquare(x);
     780     8860276 :   lx = lg(x);
     781     8860276 :   if (lx == 1) return gen_0;
     782     8860276 :   return ZV_dotproduct_i(x, y, lx);
     783             : }
     784             : 
     785             : static GEN
     786         294 : _ZM_mul(void *data /*ignored*/, GEN x, GEN y)
     787         294 : { (void)data; return ZM_mul(x,y); }
     788             : static GEN
     789        3255 : _ZM_sqr(void *data /*ignored*/, GEN x)
     790        3255 : { (void)data; return ZM_sqr(x); }
     791             : GEN
     792           0 : ZM_pow(GEN x, GEN n)
     793             : {
     794           0 :   pari_sp av = avma;
     795           0 :   if (!signe(n)) return matid(lg(x)-1);
     796           0 :   return gerepilecopy(av, gen_pow_i(x, n, NULL, &_ZM_sqr, &_ZM_mul));
     797             : }
     798             : GEN
     799        2786 : ZM_powu(GEN x, ulong n)
     800             : {
     801        2786 :   pari_sp av = avma;
     802        2786 :   if (!n) return matid(lg(x)-1);
     803        2786 :   return gerepilecopy(av, gen_powu_i(x, n, NULL, &_ZM_sqr, &_ZM_mul));
     804             : }
     805             : /********************************************************************/
     806             : /**                                                                **/
     807             : /**                           ADD, SUB                             **/
     808             : /**                                                                **/
     809             : /********************************************************************/
     810             : static GEN
     811    17524444 : ZC_add_i(GEN x, GEN y, long lx)
     812             : {
     813    17524444 :   GEN A = cgetg(lx, t_COL);
     814             :   long i;
     815    17524890 :   for (i=1; i<lx; i++) gel(A,i) = addii(gel(x,i), gel(y,i));
     816    17523840 :   return A;
     817             : }
     818             : GEN
     819     9366550 : ZC_add(GEN x, GEN y) { return ZC_add_i(x, y, lg(x)); }
     820             : GEN
     821      144879 : ZC_Z_add(GEN x, GEN y)
     822             : {
     823      144879 :   long k, lx = lg(x);
     824      144879 :   GEN z = cgetg(lx, t_COL);
     825      144879 :   if (lx == 1) pari_err_TYPE2("+",x,y);
     826      144879 :   gel(z,1) = addii(y,gel(x,1));
     827      144879 :   for (k = 2; k < lx; k++) gel(z,k) = icopy(gel(x,k));
     828      144879 :   return z;
     829             : }
     830             : 
     831             : static GEN
     832     2224998 : ZC_sub_i(GEN x, GEN y, long lx)
     833             : {
     834             :   long i;
     835     2224998 :   GEN A = cgetg(lx, t_COL);
     836     2224998 :   for (i=1; i<lx; i++) gel(A,i) = subii(gel(x,i), gel(y,i));
     837     2224998 :   return A;
     838             : }
     839             : GEN
     840     2066294 : ZC_sub(GEN x, GEN y) { return ZC_sub_i(x, y, lg(x)); }
     841             : GEN
     842           0 : ZC_Z_sub(GEN x, GEN y)
     843             : {
     844           0 :   long k, lx = lg(x);
     845           0 :   GEN z = cgetg(lx, t_COL);
     846           0 :   if (lx == 1) pari_err_TYPE2("+",x,y);
     847           0 :   gel(z,1) = subii(gel(x,1), y);
     848           0 :   for (k = 2; k < lx; k++) gel(z,k) = icopy(gel(x,k));
     849           0 :   return z;
     850             : }
     851             : GEN
     852      110645 : Z_ZC_sub(GEN a, GEN x)
     853             : {
     854      110645 :   long k, lx = lg(x);
     855      110645 :   GEN z = cgetg(lx, t_COL);
     856      110659 :   if (lx == 1) pari_err_TYPE2("-",a,x);
     857      110659 :   gel(z,1) = subii(a, gel(x,1));
     858      110607 :   for (k = 2; k < lx; k++) gel(z,k) = negi(gel(x,k));
     859      110657 :   return z;
     860             : }
     861             : 
     862             : GEN
     863      673250 : ZM_add(GEN x, GEN y)
     864             : {
     865      673250 :   long lx = lg(x), l, j;
     866             :   GEN z;
     867      673250 :   if (lx == 1) return cgetg(1, t_MAT);
     868      665144 :   z = cgetg(lx, t_MAT); l = lgcols(x);
     869      665144 :   for (j = 1; j < lx; j++) gel(z,j) = ZC_add_i(gel(x,j), gel(y,j), l);
     870      665144 :   return z;
     871             : }
     872             : GEN
     873       95233 : ZM_sub(GEN x, GEN y)
     874             : {
     875       95233 :   long lx = lg(x), l, j;
     876             :   GEN z;
     877       95233 :   if (lx == 1) return cgetg(1, t_MAT);
     878       95233 :   z = cgetg(lx, t_MAT); l = lgcols(x);
     879       95233 :   for (j = 1; j < lx; j++) gel(z,j) = ZC_sub_i(gel(x,j), gel(y,j), l);
     880       95233 :   return z;
     881             : }
     882             : /********************************************************************/
     883             : /**                                                                **/
     884             : /**                         LINEAR COMBINATION                     **/
     885             : /**                                                                **/
     886             : /********************************************************************/
     887             : /* return X/c assuming division is exact */
     888             : GEN
     889     4188774 : ZC_Z_divexact(GEN x, GEN c)
     890     4188774 : { pari_APPLY_type(t_COL, diviiexact(gel(x,i), c)) }
     891             : GEN
     892           0 : ZC_u_divexact(GEN x, ulong c)
     893           0 : { pari_APPLY_type(t_COL, diviuexact(gel(x,i), c)) }
     894             : 
     895             : GEN
     896      795896 : ZM_Z_divexact(GEN x, GEN c)
     897      795896 : { pari_APPLY_same(ZC_Z_divexact(gel(x,i), c)) }
     898             : 
     899             : GEN
     900    15730216 : ZC_Z_mul(GEN x, GEN c)
     901             : {
     902    15730216 :   if (!signe(c)) return zerocol(lg(x)-1);
     903    15283008 :   if (is_pm1(c)) return (signe(c) > 0)? ZC_copy(x): ZC_neg(x);
     904    12629682 :   pari_APPLY_type(t_COL, mulii(gel(x,i), c))
     905             : }
     906             : 
     907             : GEN
     908       22826 : ZC_z_mul(GEN x, long c)
     909             : {
     910       22826 :   if (!c) return zerocol(lg(x)-1);
     911       15602 :   if (c == 1) return ZC_copy(x);
     912       11080 :   if (c ==-1) return ZC_neg(x);
     913        9288 :   pari_APPLY_type(t_COL, mulsi(c, gel(x,i)))
     914             : }
     915             : 
     916             : GEN
     917       17959 : zv_z_mul(GEN x, long n)
     918       17959 : { pari_APPLY_long(x[i]*n) }
     919             : 
     920             : /* return a ZM */
     921             : GEN
     922           0 : nm_Z_mul(GEN X, GEN c)
     923             : {
     924           0 :   long i, j, h, l = lg(X), s = signe(c);
     925             :   GEN A;
     926           0 :   if (l == 1) return cgetg(1, t_MAT);
     927           0 :   h = lgcols(X);
     928           0 :   if (!s) return zeromat(h-1, l-1);
     929           0 :   if (is_pm1(c)) {
     930           0 :     if (s > 0) return Flm_to_ZM(X);
     931           0 :     X = Flm_to_ZM(X); ZM_togglesign(X); return X;
     932             :   }
     933           0 :   A = cgetg(l, t_MAT);
     934           0 :   for (j = 1; j < l; j++)
     935             :   {
     936           0 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     937           0 :     for (i = 1; i < h; i++) gel(a,i) = muliu(c, x[i]);
     938           0 :     gel(A,j) = a;
     939             :   }
     940           0 :   return A;
     941             : }
     942             : GEN
     943     1681476 : ZM_Z_mul(GEN X, GEN c)
     944             : {
     945     1681476 :   long i, j, h, l = lg(X);
     946             :   GEN A;
     947     1681476 :   if (l == 1) return cgetg(1, t_MAT);
     948     1681476 :   h = lgcols(X);
     949     1681476 :   if (!signe(c)) return zeromat(h-1, l-1);
     950     1680482 :   if (is_pm1(c)) return (signe(c) > 0)? ZM_copy(X): ZM_neg(X);
     951     1444716 :   A = cgetg(l, t_MAT);
     952    11782842 :   for (j = 1; j < l; j++)
     953             :   {
     954    10338126 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     955    10338126 :     for (i = 1; i < h; i++) gel(a,i) = mulii(c, gel(x,i));
     956    10338126 :     gel(A,j) = a;
     957             :   }
     958     1444716 :   return A;
     959             : }
     960             : void
     961    50065830 : ZC_lincomb1_inplace_i(GEN X, GEN Y, GEN v, long n)
     962             : {
     963             :   long i;
     964    50065830 :   for (i = n; i; i--) gel(X,i) = addmulii_inplace(gel(X,i), gel(Y,i), v);
     965    50059476 : }
     966             : /* X <- X + v Y (elementary col operation) */
     967             : void
     968    43902185 : ZC_lincomb1_inplace(GEN X, GEN Y, GEN v)
     969             : {
     970    43902185 :   if (lgefint(v) != 2) return ZC_lincomb1_inplace_i(X, Y, v, lg(X)-1);
     971             : }
     972             : void
     973    11494194 : Flc_lincomb1_inplace(GEN X, GEN Y, ulong v, ulong q)
     974             : {
     975             :   long i;
     976    11494194 :   if (!v) return; /* v = 0 */
     977    11494194 :   for (i = lg(X)-1; i; i--) X[i] = Fl_add(X[i], Fl_mul(Y[i], v, q), q);
     978             : }
     979             : 
     980             : /* X + v Y, wasteful if (v = 0) */
     981             : static GEN
     982     6997626 : ZC_lincomb1(GEN v, GEN x, GEN y)
     983     6997626 : { pari_APPLY_type(t_COL, addmulii(gel(x,i), gel(y,i), v)) }
     984             : 
     985             : /* -X + vY */
     986             : static GEN
     987      226513 : ZC_lincomb_1(GEN v, GEN x, GEN y)
     988      226513 : { pari_APPLY_type(t_COL, mulsubii(gel(y,i), v, gel(x,i))) }
     989             : 
     990             : /* X,Y compatible ZV; u,v in Z. Returns A = u*X + v*Y */
     991             : GEN
     992    15268961 : ZC_lincomb(GEN u, GEN v, GEN X, GEN Y)
     993             : {
     994             :   long su, sv;
     995             :   GEN A;
     996             : 
     997    15268961 :   su = signe(u); if (!su) return ZC_Z_mul(Y, v);
     998    15268639 :   sv = signe(v); if (!sv) return ZC_Z_mul(X, u);
     999    15268289 :   if (is_pm1(v))
    1000             :   {
    1001     2647896 :     if (is_pm1(u))
    1002             :     {
    1003     2214082 :       if (su != sv) A = ZC_sub(X, Y);
    1004      789524 :       else          A = ZC_add(X, Y);
    1005     2214082 :       if (su < 0) ZV_togglesign(A); /* in place but was created above */
    1006             :     }
    1007             :     else
    1008             :     {
    1009      433814 :       if (sv > 0) A = ZC_lincomb1 (u, Y, X);
    1010      177982 :       else        A = ZC_lincomb_1(u, Y, X);
    1011             :     }
    1012             :   }
    1013    12620389 :   else if (is_pm1(u))
    1014             :   {
    1015     6790325 :     if (su > 0) A = ZC_lincomb1 (v, X, Y);
    1016       48531 :     else        A = ZC_lincomb_1(v, X, Y);
    1017             :   }
    1018             :   else
    1019             :   { /* not cgetg_copy: x may be a t_VEC */
    1020     5830126 :     long i, lx = lg(X);
    1021     5830126 :     A = cgetg(lx,t_COL);
    1022     5830148 :     for (i=1; i<lx; i++) gel(A,i) = lincombii(u,v,gel(X,i),gel(Y,i));
    1023             :   }
    1024    15268057 :   return A;
    1025             : }
    1026             : 
    1027             : /********************************************************************/
    1028             : /**                                                                **/
    1029             : /**                           CONVERSIONS                          **/
    1030             : /**                                                                **/
    1031             : /********************************************************************/
    1032             : GEN
    1033       77539 : ZV_to_nv(GEN x)
    1034       77539 : { pari_APPLY_ulong(itou(gel(x,i))) }
    1035             : 
    1036             : GEN
    1037       32016 : zm_to_ZM(GEN x)
    1038       32016 : { pari_APPLY_type(t_MAT, zc_to_ZC(gel(x,i))) }
    1039             : 
    1040             : GEN
    1041         112 : zmV_to_ZMV(GEN x)
    1042         112 : { pari_APPLY_type(t_VEC, zm_to_ZM(gel(x,i))) }
    1043             : 
    1044             : /* same as Flm_to_ZM but do not assume positivity */
    1045             : GEN
    1046        1218 : ZM_to_zm(GEN x)
    1047        1218 : { pari_APPLY_same(ZV_to_zv(gel(x,i))) }
    1048             : 
    1049             : GEN
    1050      284480 : zv_to_Flv(GEN x, ulong p)
    1051      284480 : { pari_APPLY_ulong(umodsu(x[i], p)) }
    1052             : 
    1053             : GEN
    1054       19894 : zm_to_Flm(GEN x, ulong p)
    1055       19894 : { pari_APPLY_same(zv_to_Flv(gel(x,i),p)) }
    1056             : 
    1057             : GEN
    1058          35 : ZMV_to_zmV(GEN x)
    1059          35 : { pari_APPLY_type(t_VEC, ZM_to_zm(gel(x,i))) }
    1060             : 
    1061             : /********************************************************************/
    1062             : /**                                                                **/
    1063             : /**                         COPY, NEGATION                         **/
    1064             : /**                                                                **/
    1065             : /********************************************************************/
    1066             : GEN
    1067     4980557 : ZC_copy(GEN x)
    1068             : {
    1069     4980557 :   long i, lx = lg(x);
    1070     4980557 :   GEN y = cgetg(lx, t_COL);
    1071    44595185 :   for (i=1; i<lx; i++)
    1072             :   {
    1073    39614600 :     GEN c = gel(x,i);
    1074    39614600 :     gel(y,i) = lgefint(c) == 2? gen_0: icopy(c);
    1075             :   }
    1076     4980585 :   return y;
    1077             : }
    1078             : 
    1079             : GEN
    1080      284097 : ZM_copy(GEN x)
    1081      284097 : { pari_APPLY_same(ZC_copy(gel(x,i))) }
    1082             : 
    1083             : void
    1084       87167 : ZV_neg_inplace(GEN M)
    1085             : {
    1086       87167 :   long l = lg(M);
    1087       87167 :   while (--l > 0) gel(M,l) = negi(gel(M,l));
    1088       87167 : }
    1089             : GEN
    1090     1845651 : ZC_neg(GEN x)
    1091     1845651 : { pari_APPLY_type(t_COL, negi(gel(x,i))) }
    1092             : GEN
    1093       31666 : zv_neg(GEN x)
    1094       31666 : { pari_APPLY_long(-x[i]) }
    1095             : GEN
    1096         261 : zv_neg_inplace(GEN M)
    1097             : {
    1098         261 :   long l = lg(M);
    1099         261 :   while (--l > 0) M[l] = -M[l];
    1100         261 :   return M;
    1101             : }
    1102             : GEN
    1103      290776 : ZM_neg(GEN x)
    1104      290776 : { pari_APPLY_same(ZC_neg(gel(x,i))) }
    1105             : 
    1106             : void
    1107     1459490 : ZV_togglesign(GEN M)
    1108             : {
    1109     1459490 :   long l = lg(M);
    1110     1459490 :   while (--l > 0) togglesign_safe(&gel(M,l));
    1111     1459490 : }
    1112             : void
    1113           0 : ZM_togglesign(GEN M)
    1114             : {
    1115           0 :   long l = lg(M);
    1116           0 :   while (--l > 0) ZV_togglesign(gel(M,l));
    1117           0 : }
    1118             : 
    1119             : /********************************************************************/
    1120             : /**                                                                **/
    1121             : /**                        "DIVISION" mod HNF                      **/
    1122             : /**                                                                **/
    1123             : /********************************************************************/
    1124             : /* Reduce ZC x modulo ZM y in HNF, may return x itself (not a copy) */
    1125             : GEN
    1126     2147431 : ZC_hnfremdiv(GEN x, GEN y, GEN *Q)
    1127             : {
    1128     2147431 :   long i, l = lg(x);
    1129             :   GEN q;
    1130             : 
    1131     2147431 :   if (Q) *Q = cgetg(l,t_COL);
    1132     2147431 :   if (l == 1) return cgetg(1,t_COL);
    1133    19286416 :   for (i = l-1; i>0; i--)
    1134             :   {
    1135    17138985 :     q = diviiround(gel(x,i), gcoeff(y,i,i));
    1136    17138985 :     if (signe(q)) {
    1137     8553455 :       togglesign(q);
    1138     8553455 :       x = ZC_lincomb(gen_1, q, x, gel(y,i));
    1139             :     }
    1140    17138985 :     if (Q) gel(*Q, i) = q;
    1141             :   }
    1142     2147431 :   return x;
    1143             : }
    1144             : 
    1145             : /* x = y Q + R, may return some columns of x (not copies) */
    1146             : GEN
    1147       72855 : ZM_hnfdivrem(GEN x, GEN y, GEN *Q)
    1148             : {
    1149       72855 :   long lx = lg(x), i;
    1150       72855 :   GEN R = cgetg(lx, t_MAT);
    1151       72855 :   if (Q)
    1152             :   {
    1153       23632 :     GEN q = cgetg(lx, t_MAT); *Q = q;
    1154       23632 :     for (i=1; i<lx; i++) gel(R,i) = ZC_hnfremdiv(gel(x,i),y,(GEN*)(q+i));
    1155             :   }
    1156             :   else
    1157      153143 :     for (i=1; i<lx; i++)
    1158             :     {
    1159      103920 :       pari_sp av = avma;
    1160      103920 :       GEN z = ZC_hnfrem(gel(x,i),y);
    1161      103920 :       gel(R,i) = (avma == av)? ZC_copy(z): gerepileupto(av, z);
    1162             :     }
    1163       72855 :   return R;
    1164             : }
    1165             : 
    1166             : 
    1167             : /********************************************************************/
    1168             : /**                                                                **/
    1169             : /**                               TESTS                            **/
    1170             : /**                                                                **/
    1171             : /********************************************************************/
    1172             : int
    1173    21183112 : zv_equal0(GEN V)
    1174             : {
    1175    21183112 :   long l = lg(V);
    1176    55554179 :   while (--l > 0)
    1177    28045932 :     if (V[l]) return 0;
    1178     6325135 :   return 1;
    1179             : }
    1180             : 
    1181             : int
    1182     1085204 : ZV_equal0(GEN V)
    1183             : {
    1184     1085204 :   long l = lg(V);
    1185     4800572 :   while (--l > 0)
    1186     3465803 :     if (signe(gel(V,l))) return 0;
    1187      249565 :   return 1;
    1188             : }
    1189             : int
    1190        9037 : ZMrow_equal0(GEN V, long i)
    1191             : {
    1192        9037 :   long l = lg(V);
    1193       25810 :   while (--l > 0)
    1194       14475 :     if (signe(gcoeff(V,i,l))) return 0;
    1195        2298 :   return 1;
    1196             : }
    1197             : 
    1198             : static int
    1199    15941773 : ZV_equal_lg(GEN V, GEN W, long l)
    1200             : {
    1201    40730541 :   while (--l > 0)
    1202    22713484 :     if (!equalii(gel(V,l), gel(W,l))) return 0;
    1203     2075284 :   return 1;
    1204             : }
    1205             : int
    1206    14624860 : ZV_equal(GEN V, GEN W)
    1207             : {
    1208    14624860 :   long l = lg(V);
    1209    14624860 :   if (lg(W) != l) return 0;
    1210    14624860 :   return ZV_equal_lg(V, W, l);
    1211             : }
    1212             : int
    1213      519766 : ZM_equal(GEN A, GEN B)
    1214             : {
    1215      519766 :   long i, m, l = lg(A);
    1216      519766 :   if (lg(B) != l) return 0;
    1217      518275 :   if (l == 1) return 1;
    1218      518275 :   m = lgcols(A);
    1219      518275 :   if (lgcols(B) != m) return 0;
    1220     1758104 :   for (i = 1; i < l; i++)
    1221     1316900 :     if (!ZV_equal_lg(gel(A,i), gel(B,i), m)) return 0;
    1222      441204 :   return 1;
    1223             : }
    1224             : int
    1225       68569 : ZM_equal0(GEN A)
    1226             : {
    1227       68569 :   long i, j, m, l = lg(A);
    1228       68569 :   if (l == 1) return 1;
    1229       68569 :   m = lgcols(A);
    1230      114342 :   for (j = 1; j < l; j++)
    1231     2438720 :     for (i = 1; i < m; i++)
    1232     2392947 :       if (signe(gcoeff(A,i,j))) return 0;
    1233       13901 :   return 1;
    1234             : }
    1235             : int
    1236    30124194 : zv_equal(GEN V, GEN W)
    1237             : {
    1238    30124194 :   long l = lg(V);
    1239    30124194 :   if (lg(W) != l) return 0;
    1240   283854433 :   while (--l > 0)
    1241   224488834 :     if (V[l] != W[l]) return 0;
    1242    29607520 :   return 1;
    1243             : }
    1244             : 
    1245             : int
    1246        1337 : zvV_equal(GEN V, GEN W)
    1247             : {
    1248        1337 :   long l = lg(V);
    1249        1337 :   if (lg(W) != l) return 0;
    1250       79037 :   while (--l > 0)
    1251       77301 :     if (!zv_equal(gel(V,l),gel(W,l))) return 0;
    1252         406 :   return 1;
    1253             : }
    1254             : 
    1255             : int
    1256      169424 : ZM_ishnf(GEN x)
    1257             : {
    1258      169424 :   long i,j, lx = lg(x);
    1259      515630 :   for (i=1; i<lx; i++)
    1260             :   {
    1261      392721 :     GEN xii = gcoeff(x,i,i);
    1262      392721 :     if (signe(xii) <= 0) return 0;
    1263      921342 :     for (j=1; j<i; j++)
    1264      554010 :       if (signe(gcoeff(x,i,j))) return 0;
    1265      976047 :     for (j=i+1; j<lx; j++)
    1266             :     {
    1267      629841 :       GEN xij = gcoeff(x,i,j);
    1268      629841 :       if (signe(xij)<0 || cmpii(xij,xii)>=0) return 0;
    1269             :     }
    1270             :   }
    1271      122909 :   return 1;
    1272             : }
    1273             : int
    1274      122239 : ZM_isidentity(GEN x)
    1275             : {
    1276      122239 :   long i,j, lx = lg(x);
    1277             : 
    1278      122239 :   if (lx == 1) return 1;
    1279      122239 :   if (lx != lgcols(x)) return 0;
    1280      880868 :   for (j=1; j<lx; j++)
    1281             :   {
    1282      758650 :     GEN c = gel(x,j);
    1283     4286071 :     for (i=1; i<j; )
    1284     2768771 :       if (signe(gel(c,i++))) return 0;
    1285             :     /* i = j */
    1286      758650 :       if (!equali1(gel(c,i++))) return 0;
    1287     4286057 :     for (   ; i<lx; )
    1288     2768785 :       if (signe(gel(c,i++))) return 0;
    1289             :   }
    1290      122218 :   return 1;
    1291             : }
    1292             : int
    1293       84453 : ZM_isdiagonal(GEN x)
    1294             : {
    1295       84453 :   long i,j, lx = lg(x);
    1296       84453 :   if (lx == 1) return 1;
    1297       84453 :   if (lx != lgcols(x)) return 0;
    1298             : 
    1299      249568 :   for (j=1; j<lx; j++)
    1300             :   {
    1301      214366 :     GEN c = gel(x,j);
    1302      428139 :     for (i=1; i<j; i++)
    1303      262989 :       if (signe(gel(c,i))) return 0;
    1304      611339 :     for (i++; i<lx; i++)
    1305      446224 :       if (signe(gel(c,i))) return 0;
    1306             :   }
    1307       35202 :   return 1;
    1308             : }
    1309             : int
    1310       42913 : ZM_isscalar(GEN x, GEN s)
    1311             : {
    1312       42913 :   long i, j, lx = lg(x);
    1313             : 
    1314       42913 :   if (lx == 1) return 1;
    1315       42913 :   if (!s) s = gcoeff(x,1,1);
    1316       42913 :   if (equali1(s)) return ZM_isidentity(x);
    1317       42913 :   if (lx != lgcols(x)) return 0;
    1318      232535 :   for (j=1; j<lx; j++)
    1319             :   {
    1320      212685 :     GEN c = gel(x,j);
    1321     1733606 :     for (i=1; i<j; )
    1322     1330517 :       if (signe(gel(c,i++))) return 0;
    1323             :     /* i = j */
    1324      190404 :       if (!equalii(gel(c,i++), s)) return 0;
    1325     1724375 :     for (   ; i<lx; )
    1326     1344797 :       if (signe(gel(c,i++))) return 0;
    1327             :   }
    1328       19850 :   return 1;
    1329             : }
    1330             : 
    1331             : long
    1332      102002 : ZC_is_ei(GEN x)
    1333             : {
    1334      102002 :   long i, j = 0, l = lg(x);
    1335      960317 :   for (i = 1; i < l; i++)
    1336             :   {
    1337      858316 :     GEN c = gel(x,i);
    1338      858316 :     long s = signe(c);
    1339      858316 :     if (!s) continue;
    1340      101996 :     if (s < 0 || !is_pm1(c) || j) return 0;
    1341      101995 :     j = i;
    1342             :   }
    1343      102001 :   return j;
    1344             : }
    1345             : 
    1346             : /********************************************************************/
    1347             : /**                                                                **/
    1348             : /**                       MISCELLANEOUS                            **/
    1349             : /**                                                                **/
    1350             : /********************************************************************/
    1351             : /* assume lg(x) = lg(y), x,y in Z^n */
    1352             : int
    1353      820330 : ZV_cmp(GEN x, GEN y)
    1354             : {
    1355      820330 :   long fl,i, lx = lg(x);
    1356     1510201 :   for (i=1; i<lx; i++)
    1357     1220423 :     if (( fl = cmpii(gel(x,i), gel(y,i)) )) return fl;
    1358      289778 :   return 0;
    1359             : }
    1360             : /* assume lg(x) = lg(y), x,y in Z^n */
    1361             : int
    1362        4313 : ZV_abscmp(GEN x, GEN y)
    1363             : {
    1364        4313 :   long fl,i, lx = lg(x);
    1365       20859 :   for (i=1; i<lx; i++)
    1366       20779 :     if (( fl = abscmpii(gel(x,i), gel(y,i)) )) return fl;
    1367          80 :   return 0;
    1368             : }
    1369             : 
    1370             : long
    1371     3447854 : zv_content(GEN x)
    1372             : {
    1373     3447854 :   long i, s, l = lg(x);
    1374     3447854 :   if (l == 1) return 0;
    1375     3447847 :   s = labs(x[1]);
    1376     3447847 :   for (i = 2; i < l && s != 1; i++) s = ugcd(s, labs(x[i]));
    1377     3447847 :   return s;
    1378             : }
    1379             : GEN
    1380      300635 : ZV_content(GEN x)
    1381             : {
    1382      300635 :   long i, l = lg(x);
    1383      300635 :   pari_sp av = avma;
    1384             :   GEN c;
    1385      300635 :   if (l == 1) return gen_0;
    1386      300635 :   if (l == 2) return absi(gel(x,1));
    1387      233302 :   c = gel(x,1);
    1388      548870 :   for (i = 2; i < l; i++)
    1389             :   {
    1390      402212 :     c = gcdii(c, gel(x,i));
    1391      402212 :     if (is_pm1(c)) { set_avma(av); return gen_1; }
    1392             :   }
    1393      146658 :   return gerepileuptoint(av, c);
    1394             : }
    1395             : 
    1396             : GEN
    1397     1100561 : ZM_det_triangular(GEN mat)
    1398             : {
    1399             :   pari_sp av;
    1400     1100561 :   long i,l = lg(mat);
    1401             :   GEN s;
    1402             : 
    1403     1100561 :   if (l<3) return l<2? gen_1: icopy(gcoeff(mat,1,1));
    1404      993462 :   av = avma; s = gcoeff(mat,1,1);
    1405      993462 :   for (i=2; i<l; i++) s = mulii(s,gcoeff(mat,i,i));
    1406      993462 :   return gerepileuptoint(av,s);
    1407             : }
    1408             : 
    1409             : /* assumes no overflow */
    1410             : long
    1411      392637 : zv_prod(GEN v)
    1412             : {
    1413      392637 :   long n, i, l = lg(v);
    1414      392637 :   if (l == 1) return 1;
    1415      293685 :   n = v[1]; for (i = 2; i < l; i++) n *= v[i];
    1416      293685 :   return n;
    1417             : }
    1418             : 
    1419             : static GEN
    1420   278324317 : _mulii(void *E, GEN a, GEN b)
    1421   278324317 : { (void) E; return mulii(a, b); }
    1422             : 
    1423             : /* product of ulongs */
    1424             : GEN
    1425      375249 : zv_prod_Z(GEN v)
    1426             : {
    1427      375249 :   pari_sp av = avma;
    1428      375249 :   long k, m, n = lg(v)-1;
    1429             :   GEN V;
    1430      375249 :   switch(n) {
    1431       19607 :     case 0: return gen_1;
    1432       59787 :     case 1: return utoi(v[1]);
    1433      130683 :     case 2: return muluu(v[1], v[2]);
    1434             :   }
    1435      165172 :   m = n >> 1;
    1436      165172 :   V = cgetg(m + (odd(n)? 2: 1), t_VEC);
    1437      165172 :   for (k = 1; k <= m; k++) gel(V,k) = muluu(v[k<<1], v[(k<<1)-1]);
    1438      165172 :   if (odd(n)) gel(V,k) = utoipos(v[n]);
    1439      165172 :   return gerepileuptoint(av, gen_product(V, NULL, &_mulii));
    1440             : }
    1441             : GEN
    1442    14694393 : vecsmall_prod(GEN v)
    1443             : {
    1444    14694393 :   pari_sp av = avma;
    1445    14694393 :   long k, m, n = lg(v)-1;
    1446             :   GEN V;
    1447    14694393 :   switch (n) {
    1448           0 :     case 0: return gen_1;
    1449           0 :     case 1: return stoi(v[1]);
    1450          21 :     case 2: return mulss(v[1], v[2]);
    1451             :   }
    1452    14694372 :   m = n >> 1;
    1453    14694372 :   V = cgetg(m + (odd(n)? 2: 1), t_VEC);
    1454    14694372 :   for (k = 1; k <= m; k++) gel(V,k) = mulss(v[k<<1], v[(k<<1)-1]);
    1455    14694372 :   if (odd(n)) gel(V,k) = stoi(v[n]);
    1456    14694372 :   return gerepileuptoint(av, gen_product(V, NULL, &_mulii));
    1457             : }
    1458             : 
    1459             : GEN
    1460     7607718 : ZV_prod(GEN v)
    1461             : {
    1462     7607718 :   pari_sp av = avma;
    1463     7607718 :   long i, l = lg(v);
    1464             :   GEN n;
    1465     7607718 :   if (l == 1) return gen_1;
    1466     7585493 :   if (l > 7) return gerepileuptoint(av, gen_product(v, NULL, _mulii));
    1467      243822 :   n = gel(v,1);
    1468      243822 :   if (l == 2) return icopy(n);
    1469      145891 :   for (i = 2; i < l; i++) n = mulii(n, gel(v,i));
    1470      145891 :   return gerepileuptoint(av, n);
    1471             : }
    1472             : /* assumes no overflow */
    1473             : long
    1474        2030 : zv_sum(GEN v)
    1475             : {
    1476        2030 :   long n, i, l = lg(v);
    1477        2030 :   if (l == 1) return 0;
    1478        2009 :   n = v[1]; for (i = 2; i < l; i++) n += v[i];
    1479        2009 :   return n;
    1480             : }
    1481             : GEN
    1482          63 : ZV_sum(GEN v)
    1483             : {
    1484          63 :   pari_sp av = avma;
    1485          63 :   long i, l = lg(v);
    1486             :   GEN n;
    1487          63 :   if (l == 1) return gen_0;
    1488          63 :   n = gel(v,1);
    1489          63 :   if (l == 2) return icopy(n);
    1490          63 :   for (i = 2; i < l; i++) n = addii(n, gel(v,i));
    1491          63 :   return gerepileuptoint(av, n);
    1492             : }
    1493             : 
    1494             : /********************************************************************/
    1495             : /**                                                                **/
    1496             : /**         GRAM SCHMIDT REDUCTION (integer matrices)              **/
    1497             : /**                                                                **/
    1498             : /********************************************************************/
    1499             : 
    1500             : /* L[k,] += q * L[l,], l < k. Inefficient if q = 0 */
    1501             : static void
    1502       82832 : Zupdate_row(long k, long l, GEN q, GEN L, GEN B)
    1503             : {
    1504       82832 :   long i, qq = itos_or_0(q);
    1505       82832 :   if (!qq)
    1506             :   {
    1507        3155 :     for(i=1;i<l;i++)  gcoeff(L,k,i) = addii(gcoeff(L,k,i),mulii(q,gcoeff(L,l,i)));
    1508        3155 :     gcoeff(L,k,l) = addii(gcoeff(L,k,l), mulii(q,B));
    1509        3155 :     return;
    1510             :   }
    1511       79677 :   if (qq == 1) {
    1512       19771 :     for (i=1;i<l; i++) gcoeff(L,k,i) = addii(gcoeff(L,k,i),gcoeff(L,l,i));
    1513       19771 :     gcoeff(L,k,l) = addii(gcoeff(L,k,l), B);
    1514       59906 :   } else if (qq == -1) {
    1515       21688 :     for (i=1;i<l; i++) gcoeff(L,k,i) = subii(gcoeff(L,k,i),gcoeff(L,l,i));
    1516       21688 :     gcoeff(L,k,l) = addii(gcoeff(L,k,l), negi(B));
    1517             :   } else {
    1518       38218 :     for(i=1;i<l;i++) gcoeff(L,k,i) = addii(gcoeff(L,k,i),mulsi(qq,gcoeff(L,l,i)));
    1519       38218 :     gcoeff(L,k,l) = addii(gcoeff(L,k,l), mulsi(qq,B));
    1520             :   }
    1521             : }
    1522             : 
    1523             : /* update L[k,] */
    1524             : static void
    1525      180789 : ZRED(long k, long l, GEN x, GEN L, GEN B)
    1526             : {
    1527      180789 :   GEN q = truedivii(addii(B,shifti(gcoeff(L,k,l),1)), shifti(B,1));
    1528      180789 :   if (!signe(q)) return;
    1529       82832 :   q = negi(q);
    1530       82832 :   Zupdate_row(k,l,q,L,B);
    1531       82832 :   gel(x,k) = ZC_lincomb(gen_1, q, gel(x,k), gel(x,l));
    1532             : }
    1533             : 
    1534             : /* Gram-Schmidt reduction, x a ZM */
    1535             : static void
    1536      206661 : ZincrementalGS(GEN x, GEN L, GEN B, long k)
    1537             : {
    1538             :   long i, j;
    1539      685741 :   for (j=1; j<=k; j++)
    1540             :   {
    1541      479080 :     pari_sp av = avma;
    1542      479080 :     GEN u = ZV_dotproduct(gel(x,k), gel(x,j));
    1543     1033389 :     for (i=1; i<j; i++)
    1544             :     {
    1545      554309 :       u = subii(mulii(gel(B,i+1), u), mulii(gcoeff(L,k,i), gcoeff(L,j,i)));
    1546      554309 :       u = diviiexact(u, gel(B,i));
    1547             :     }
    1548      479080 :     gcoeff(L,k,j) = gerepileuptoint(av, u);
    1549             :   }
    1550      206661 :   gel(B,k+1) = gcoeff(L,k,k); gcoeff(L,k,k) = gen_1;
    1551      206661 : }
    1552             : 
    1553             : /* Variant reducemodinvertible(ZC v, ZM y), when y singular.
    1554             :  * Very inefficient if y is not LLL-reduced of maximal rank */
    1555             : static GEN
    1556       22274 : ZC_reducemodmatrix_i(GEN v, GEN y)
    1557             : {
    1558       22274 :   GEN B, L, x = shallowconcat(y, v);
    1559       22274 :   long k, lx = lg(x), nx = lx-1;
    1560             : 
    1561       22274 :   B = scalarcol_shallow(gen_1, lx);
    1562       22274 :   L = zeromatcopy(nx, nx);
    1563       22274 :   for (k=1; k <= nx; k++) ZincrementalGS(x, L, B, k);
    1564       22274 :   for (k = nx-1; k >= 1; k--) ZRED(nx,k, x,L,gel(B,k+1));
    1565       22274 :   return gel(x,nx);
    1566             : }
    1567             : GEN
    1568       22274 : ZC_reducemodmatrix(GEN v, GEN y) {
    1569       22274 :   pari_sp av = avma;
    1570       22274 :   return gerepilecopy(av, ZC_reducemodmatrix_i(v,y));
    1571             : }
    1572             : 
    1573             : /* Variant reducemodinvertible(ZM v, ZM y), when y singular.
    1574             :  * Very inefficient if y is not LLL-reduced of maximal rank */
    1575             : static GEN
    1576       37345 : ZM_reducemodmatrix_i(GEN v, GEN y)
    1577             : {
    1578             :   GEN B, L, V;
    1579       37345 :   long j, k, lv = lg(v), nx = lg(y), lx = nx+1;
    1580             : 
    1581       37345 :   V = cgetg(lv, t_MAT);
    1582       37345 :   B = scalarcol_shallow(gen_1, lx);
    1583       37345 :   L = zeromatcopy(nx, nx);
    1584       37345 :   for (k=1; k < nx; k++) ZincrementalGS(y, L, B, k);
    1585      108374 :   for (j = 1; j < lg(v); j++)
    1586             :   {
    1587       71029 :     GEN x = shallowconcat(y, gel(v,j));
    1588       71029 :     ZincrementalGS(x, L, B, nx); /* overwrite last */
    1589       71029 :     for (k = nx-1; k >= 1; k--) ZRED(nx,k, x,L,gel(B,k+1));
    1590       71029 :     gel(V,j) = gel(x,nx);
    1591             :   }
    1592       37345 :   return V;
    1593             : }
    1594             : GEN
    1595       37345 : ZM_reducemodmatrix(GEN v, GEN y) {
    1596       37345 :   pari_sp av = avma;
    1597       37345 :   return gerepilecopy(av, ZM_reducemodmatrix_i(v,y));
    1598             : }
    1599             : 
    1600             : GEN
    1601       12243 : ZC_reducemodlll(GEN x,GEN y)
    1602             : {
    1603       12243 :   pari_sp av = avma;
    1604       12243 :   GEN z = ZC_reducemodmatrix(x, ZM_lll(y, 0.75, LLL_INPLACE));
    1605       12243 :   return gerepilecopy(av, z);
    1606             : }
    1607             : GEN
    1608           0 : ZM_reducemodlll(GEN x,GEN y)
    1609             : {
    1610           0 :   pari_sp av = avma;
    1611           0 :   GEN z = ZM_reducemodmatrix(x, ZM_lll(y, 0.75, LLL_INPLACE));
    1612           0 :   return gerepilecopy(av, z);
    1613             : }

Generated by: LCOV version 1.13