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 - RgV.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 24988-2584e74448) Lines: 497 550 90.4 %
Date: 2020-01-26 05:57:03 Functions: 91 98 92.9 %
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             : int
      18      520515 : RgM_is_ZM(GEN x)
      19             : {
      20      520515 :   long i, j, h, l = lg(x);
      21      520515 :   if (l == 1) return 1;
      22      520277 :   h = lgcols(x);
      23      520277 :   if (h == 1) return 1;
      24     2392020 :   for (j = l-1; j > 0; j--)
      25    13025383 :     for (i = h-1; i > 0; i--)
      26    11153640 :       if (typ(gcoeff(x,i,j)) != t_INT) return 0;
      27      484291 :   return 1;
      28             : }
      29             : 
      30             : int
      31         203 : RgM_is_QM(GEN x)
      32             : {
      33         203 :   long i, j, h, l = lg(x);
      34         203 :   if (l == 1) return 1;
      35         189 :   h = lgcols(x);
      36         189 :   if (h == 1) return 1;
      37        1218 :   for (j = l-1; j > 0; j--)
      38       15057 :     for (i = h-1; i > 0; i--)
      39       14028 :       if (!is_rational_t(typ(gcoeff(x,i,j)))) return 0;
      40         175 :   return 1;
      41             : }
      42             : 
      43             : int
      44          35 : RgV_is_ZMV(GEN V)
      45             : {
      46          35 :   long i, l = lg(V);
      47         357 :   for (i=1; i<l; i++)
      48         322 :     if (typ(gel(V,i))!=t_MAT || !RgM_is_ZM(gel(V,i)))
      49           0 :       return 0;
      50          35 :   return 1;
      51             : }
      52             : 
      53             : /********************************************************************/
      54             : /**                                                                **/
      55             : /**                   GENERIC LINEAR ALGEBRA                       **/
      56             : /**                                                                **/
      57             : /********************************************************************/
      58             : /*           GENERIC  MULTIPLICATION involving zc/zm                */
      59             : 
      60             : /* x[i,] * y */
      61             : static GEN
      62     3542071 : RgMrow_zc_mul_i(GEN x, GEN y, long c, long i)
      63             : {
      64     3542071 :   pari_sp av = avma;
      65     3542071 :   GEN s = NULL;
      66             :   long j;
      67   501146208 :   for (j=1; j<c; j++)
      68             :   {
      69   497604137 :     long t = y[j];
      70   497604137 :     if (!t) continue;
      71    12531442 :     if (!s) { s = gmulgs(gcoeff(x,i,j),t); continue; }
      72     9035743 :     switch(t)
      73             :     {
      74     7092462 :       case  1: s = gadd(s, gcoeff(x,i,j)); break;
      75      233254 :       case -1: s = gsub(s, gcoeff(x,i,j)); break;
      76     1710027 :       default: s = gadd(s, gmulgs(gcoeff(x,i,j), t)); break;
      77             :     }
      78             :   }
      79     3542071 :   if (!s) { set_avma(av); return gen_0; }
      80     3495699 :   return gerepileupto(av, s);
      81             : }
      82             : GEN
      83       69937 : RgMrow_zc_mul(GEN x, GEN y, long i) { return RgMrow_zc_mul_i(x,y,lg(y),i); }
      84             : /* x non-empty t_MAT, y a compatible zc (dimension > 0). */
      85             : static GEN
      86      116881 : RgM_zc_mul_i(GEN x, GEN y, long c, long l)
      87             : {
      88      116881 :   GEN z = cgetg(l,t_COL);
      89             :   long i;
      90      116881 :   for (i = 1; i < l; i++) gel(z,i) = RgMrow_zc_mul_i(x,y,c,i);
      91      116881 :   return z;
      92             : }
      93             : GEN
      94       71736 : RgM_zc_mul(GEN x, GEN y) { return RgM_zc_mul_i(x,y, lg(x), lgcols(x)); }
      95             : /* x t_MAT, y a compatible zm (dimension > 0). */
      96             : GEN
      97       13566 : RgM_zm_mul(GEN x, GEN y)
      98             : {
      99       13566 :   long j, c, l = lg(x), ly = lg(y);
     100       13566 :   GEN z = cgetg(ly, t_MAT);
     101       13566 :   if (l == 1) return z;
     102       13566 :   c = lgcols(x);
     103       13566 :   for (j = 1; j < ly; j++) gel(z,j) = RgM_zc_mul_i(x, gel(y,j), l,c);
     104       13566 :   return z;
     105             : }
     106             : 
     107             : static GEN
     108       38023 : RgV_zc_mul_i(GEN x, GEN y, long l)
     109             : {
     110             :   long i;
     111       38023 :   GEN z = gen_0;
     112       38023 :   pari_sp av = avma;
     113       38023 :   for (i = 1; i < l; i++) z = gadd(z, gmulgs(gel(x,i), y[i]));
     114       38023 :   return gerepileupto(av, z);
     115             : }
     116             : GEN
     117        6804 : RgV_zc_mul(GEN x, GEN y) { return RgV_zc_mul_i(x, y, lg(x)); }
     118             : 
     119             : GEN
     120        7551 : RgV_zm_mul(GEN x, GEN y)
     121             : {
     122        7551 :   long j, l = lg(x), ly = lg(y);
     123        7551 :   GEN z = cgetg(ly, t_VEC);
     124        7551 :   for (j = 1; j < ly; j++) gel(z,j) = RgV_zc_mul_i(x, gel(y,j), l);
     125        7551 :   return z;
     126             : }
     127             : 
     128             : /* scalar product x.x */
     129             : GEN
     130        3459 : RgV_dotsquare(GEN x)
     131             : {
     132        3459 :   long i, lx = lg(x);
     133        3459 :   pari_sp av = avma;
     134             :   GEN z;
     135        3459 :   if (lx == 1) return gen_0;
     136        3459 :   z = gsqr(gel(x,1));
     137      193073 :   for (i=2; i<lx; i++)
     138             :   {
     139      189614 :     z = gadd(z, gsqr(gel(x,i)));
     140      189614 :     if (gc_needed(av,3))
     141             :     {
     142           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgV_dotsquare, i = %ld",i);
     143           0 :       z = gerepileupto(av, z);
     144             :     }
     145             :   }
     146        3459 :   return gerepileupto(av,z);
     147             : }
     148             : 
     149             : /* scalar product x.y, lx = lg(x) = lg(y) */
     150             : static GEN
     151     2102435 : RgV_dotproduct_i(GEN x, GEN y, long lx)
     152             : {
     153     2102435 :   pari_sp av = avma;
     154             :   long i;
     155             :   GEN z;
     156     2102435 :   if (lx == 1) return gen_0;
     157     2102379 :   z = gmul(gel(x,1),gel(y,1));
     158    46686148 :   for (i=2; i<lx; i++)
     159             :   {
     160    44583765 :     z = gadd(z, gmul(gel(x,i), gel(y,i)));
     161    44583770 :     if (gc_needed(av,3))
     162             :     {
     163           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgV_dotproduct, i = %ld",i);
     164           0 :       z = gerepileupto(av, z);
     165             :     }
     166             :   }
     167     2102383 :   return gerepileupto(av,z);
     168             : }
     169             : GEN
     170      151673 : RgV_dotproduct(GEN x,GEN y)
     171             : {
     172      151673 :   if (x == y) return RgV_dotsquare(x);
     173      151673 :   return RgV_dotproduct_i(x, y, lg(x));
     174             : }
     175             : /* v[1] + ... + v[lg(v)-1] */
     176             : GEN
     177      583943 : RgV_sum(GEN v)
     178             : {
     179             :   GEN p;
     180      583943 :   long i, l = lg(v);
     181      583943 :   if (l == 1) return gen_0;
     182      583943 :   p = gel(v,1); for (i=2; i<l; i++) p = gadd(p, gel(v,i));
     183      583943 :   return p;
     184             : }
     185             : /* v[1] + ... + v[n]. Assume lg(v) > n. */
     186             : GEN
     187        1022 : RgV_sumpart(GEN v, long n)
     188             : {
     189             :   GEN p;
     190             :   long i;
     191        1022 :   if (!n) return gen_0;
     192        1022 :   p = gel(v,1); for (i=2; i<=n; i++) p = gadd(p, gel(v,i));
     193        1022 :   return p;
     194             : }
     195             : /* v[m] + ... + v[n]. Assume lg(v) > n, m > 0. */
     196             : GEN
     197           0 : RgV_sumpart2(GEN v, long m, long n)
     198             : {
     199             :   GEN p;
     200             :   long i;
     201           0 :   if (n < m) return gen_0;
     202           0 :   p = gel(v,m); for (i=m+1; i<=n; i++) p = gadd(p, gel(v,i));
     203           0 :   return p;
     204             : }
     205             : GEN
     206         432 : RgM_sumcol(GEN A)
     207             : {
     208         432 :   long i,j,m,l = lg(A);
     209             :   GEN v;
     210             : 
     211         432 :   if (l == 1) return cgetg(1,t_MAT);
     212         432 :   if (l == 2) return gcopy(gel(A,1));
     213         208 :   m = lgcols(A);
     214         208 :   v = cgetg(m, t_COL);
     215         680 :   for (i = 1; i < m; i++)
     216             :   {
     217         472 :     pari_sp av = avma;
     218         472 :     GEN s = gcoeff(A,i,1);
     219         472 :     for (j = 2; j < l; j++) s = gadd(s, gcoeff(A,i,j));
     220         472 :     gel(v, i) = gerepileupto(av, s);
     221             :   }
     222         208 :   return v;
     223             : }
     224             : 
     225             : static GEN
     226      921291 : _gmul(void *data, GEN x, GEN y)
     227      921291 : { (void)data; return gmul(x,y); }
     228             : 
     229             : GEN
     230       59122 : RgV_prod(GEN x)
     231             : {
     232       59122 :   return gen_product(x, NULL, _gmul);
     233             : }
     234             : 
     235             : /*                    ADDITION SCALAR + MATRIX                     */
     236             : /* x square matrix, y scalar; create the square matrix x + y*Id */
     237             : GEN
     238        5760 : RgM_Rg_add(GEN x, GEN y)
     239             : {
     240        5760 :   long l = lg(x), i, j;
     241        5760 :   GEN z = cgetg(l,t_MAT);
     242             : 
     243        5760 :   if (l==1) return z;
     244        5760 :   if (l != lgcols(x)) pari_err_OP( "+", x, y);
     245        5760 :   z = cgetg(l,t_MAT);
     246       42634 :   for (i=1; i<l; i++)
     247             :   {
     248       36874 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     249       36874 :     gel(z,i) = zi;
     250     1893914 :     for (j=1; j<l; j++)
     251     1857040 :       gel(zi,j) = i==j? gadd(y,gel(xi,j)): gcopy(gel(xi,j));
     252             :   }
     253        5760 :   return z;
     254             : }
     255             : GEN
     256        6968 : RgM_Rg_sub(GEN x, GEN y)
     257             : {
     258        6968 :   long l = lg(x), i, j;
     259        6968 :   GEN z = cgetg(l,t_MAT);
     260             : 
     261        6968 :   if (l==1) return z;
     262        6968 :   if (l != lgcols(x)) pari_err_OP( "-", x, y);
     263        6968 :   z = cgetg(l,t_MAT);
     264       26284 :   for (i=1; i<l; i++)
     265             :   {
     266       19316 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     267       19316 :     gel(z,i) = zi;
     268       92154 :     for (j=1; j<l; j++)
     269       72838 :       gel(zi,j) = i==j? gsub(gel(xi,j), y): gcopy(gel(xi,j));
     270             :   }
     271        6968 :   return z;
     272             : }
     273             : GEN
     274         497 : RgM_Rg_add_shallow(GEN x, GEN y)
     275             : {
     276         497 :   long l = lg(x), i, j;
     277         497 :   GEN z = cgetg(l,t_MAT);
     278             : 
     279         497 :   if (l==1) return z;
     280         497 :   if (l != lgcols(x)) pari_err_OP( "+", x, y);
     281        1729 :   for (i=1; i<l; i++)
     282             :   {
     283        1232 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     284        1232 :     gel(z,i) = zi;
     285        1232 :     for (j=1; j<l; j++) gel(zi,j) = gel(xi,j);
     286        1232 :     gel(zi,i) = gadd(gel(zi,i), y);
     287             :   }
     288         497 :   return z;
     289             : }
     290             : GEN
     291      600379 : RgM_Rg_sub_shallow(GEN x, GEN y)
     292             : {
     293      600379 :   long l = lg(x), i, j;
     294      600379 :   GEN z = cgetg(l,t_MAT);
     295             : 
     296      600379 :   if (l==1) return z;
     297      600379 :   if (l != lgcols(x)) pari_err_OP( "-", x, y);
     298     2801849 :   for (i=1; i<l; i++)
     299             :   {
     300     2201470 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     301     2201470 :     gel(z,i) = zi;
     302     2201470 :     for (j=1; j<l; j++) gel(zi,j) = gel(xi,j);
     303     2201470 :     gel(zi,i) = gsub(gel(zi,i), y);
     304             :   }
     305      600379 :   return z;
     306             : }
     307             : 
     308             : GEN
     309     3548799 : RgC_Rg_add(GEN x, GEN y)
     310             : {
     311     3548799 :   long k, lx = lg(x);
     312     3548799 :   GEN z = cgetg(lx, t_COL);
     313     3548799 :   if (lx == 1)
     314             :   {
     315           0 :     if (isintzero(y)) return z;
     316           0 :     pari_err_TYPE2("+",x,y);
     317             :   }
     318     3548799 :   gel(z,1) = gadd(y,gel(x,1));
     319     3548799 :   for (k = 2; k < lx; k++) gel(z,k) = gcopy(gel(x,k));
     320     3548799 :   return z;
     321             : }
     322             : GEN
     323       25228 : RgC_Rg_sub(GEN x, GEN y)
     324             : {
     325       25228 :   long k, lx = lg(x);
     326       25228 :   GEN z = cgetg(lx, t_COL);
     327       25228 :   if (lx == 1)
     328             :   {
     329           0 :     if (isintzero(y)) return z;
     330           0 :     pari_err_TYPE2("-",x,y);
     331             :   }
     332       25228 :   gel(z,1) = gsub(gel(x,1), y);
     333       25228 :   for (k = 2; k < lx; k++) gel(z,k) = gcopy(gel(x,k));
     334       25228 :   return z;
     335             : }
     336             : /* a - x */
     337             : GEN
     338      109942 : Rg_RgC_sub(GEN a, GEN x)
     339             : {
     340      109942 :   long k, lx = lg(x);
     341      109942 :   GEN z = cgetg(lx,t_COL);
     342      109942 :   if (lx == 1)
     343             :   {
     344           0 :     if (isintzero(a)) return z;
     345           0 :     pari_err_TYPE2("-",a,x);
     346             :   }
     347      109942 :   gel(z,1) = gsub(a, gel(x,1));
     348      109942 :   for (k = 2; k < lx; k++) gel(z,k) = gneg(gel(x,k));
     349      109942 :   return z;
     350             : }
     351             : 
     352             : 
     353             : static GEN
     354    14671682 : RgC_add_i(GEN x, GEN y, long lx)
     355             : {
     356    14671682 :   GEN A = cgetg(lx, t_COL);
     357             :   long i;
     358    14671682 :   for (i=1; i<lx; i++) gel(A,i) = gadd(gel(x,i), gel(y,i));
     359    14671682 :   return A;
     360             : }
     361             : GEN
     362    11443702 : RgC_add(GEN x, GEN y) { return RgC_add_i(x, y, lg(x)); }
     363             : GEN
     364      154276 : RgV_add(GEN x, GEN y)
     365      154276 : { pari_APPLY_type(t_VEC, gadd(gel(x,i), gel(y,i))) }
     366             : 
     367             : static GEN
     368     4485010 : RgC_sub_i(GEN x, GEN y, long lx)
     369             : {
     370             :   long i;
     371     4485010 :   GEN A = cgetg(lx, t_COL);
     372     4485010 :   for (i=1; i<lx; i++) gel(A,i) = gsub(gel(x,i), gel(y,i));
     373     4485010 :   return A;
     374             : }
     375             : GEN
     376     4413594 : RgC_sub(GEN x, GEN y) { return RgC_sub_i(x, y, lg(x)); }
     377             : GEN
     378      286210 : RgV_sub(GEN x, GEN y)
     379      286210 : { pari_APPLY_type(t_VEC, gsub(gel(x,i), gel(y,i))) }
     380             : 
     381             : GEN
     382      675885 : RgM_add(GEN x, GEN y)
     383             : {
     384      675885 :   long lx = lg(x), l, j;
     385             :   GEN z;
     386      675885 :   if (lx == 1) return cgetg(1, t_MAT);
     387      675885 :   z = cgetg(lx, t_MAT); l = lgcols(x);
     388      675885 :   for (j = 1; j < lx; j++) gel(z,j) = RgC_add_i(gel(x,j), gel(y,j), l);
     389      675885 :   return z;
     390             : }
     391             : GEN
     392       29855 : RgM_sub(GEN x, GEN y)
     393             : {
     394       29855 :   long lx = lg(x), l, j;
     395             :   GEN z;
     396       29855 :   if (lx == 1) return cgetg(1, t_MAT);
     397       29855 :   z = cgetg(lx, t_MAT); l = lgcols(x);
     398       29855 :   for (j = 1; j < lx; j++) gel(z,j) = RgC_sub_i(gel(x,j), gel(y,j), l);
     399       29855 :   return z;
     400             : }
     401             : 
     402             : static GEN
     403     3167920 : RgC_neg_i(GEN x, long lx)
     404             : {
     405             :   long i;
     406     3167920 :   GEN y = cgetg(lx, t_COL);
     407     3167920 :   for (i=1; i<lx; i++) gel(y,i) = gneg(gel(x,i));
     408     3167920 :   return y;
     409             : }
     410             : GEN
     411      199406 : RgC_neg(GEN x) { return RgC_neg_i(x, lg(x)); }
     412             : GEN
     413        2919 : RgV_neg(GEN x)
     414        2919 : { pari_APPLY_type(t_VEC, gneg(gel(x,i))) }
     415             : GEN
     416      536062 : RgM_neg(GEN x)
     417             : {
     418      536062 :   long i, hx, lx = lg(x);
     419      536062 :   GEN y = cgetg(lx, t_MAT);
     420      536062 :   if (lx == 1) return y;
     421      536055 :   hx = lgcols(x);
     422      536055 :   for (i=1; i<lx; i++) gel(y,i) = RgC_neg_i(gel(x,i), hx);
     423      536055 :   return y;
     424             : }
     425             : 
     426             : GEN
     427     1099222 : RgV_RgC_mul(GEN x, GEN y)
     428             : {
     429     1099222 :   long lx = lg(x);
     430     1099222 :   if (lx != lg(y)) pari_err_OP("operation 'RgV_RgC_mul'", x, y);
     431     1099159 :   return RgV_dotproduct_i(x, y, lx);
     432             : }
     433             : GEN
     434        1687 : RgC_RgV_mul(GEN x, GEN y)
     435             : {
     436        1687 :   long i, ly = lg(y);
     437        1687 :   GEN z = cgetg(ly,t_MAT);
     438        1687 :   for (i=1; i<ly; i++) gel(z,i) = RgC_Rg_mul(x, gel(y,i));
     439        1687 :   return z;
     440             : }
     441             : GEN
     442           0 : RgC_RgM_mul(GEN x, GEN y)
     443             : {
     444           0 :   long i, ly = lg(y);
     445           0 :   GEN z = cgetg(ly,t_MAT);
     446           0 :   if (ly != 1 && lgcols(y) != 2) pari_err_OP("operation 'RgC_RgM_mul'",x,y);
     447           0 :   for (i=1; i<ly; i++) gel(z,i) = RgC_Rg_mul(x, gcoeff(y,1,i));
     448           0 :   return z;
     449             : }
     450             : GEN
     451           0 : RgM_RgV_mul(GEN x, GEN y)
     452             : {
     453           0 :   if (lg(x) != 2) pari_err_OP("operation 'RgM_RgV_mul'", x,y);
     454           0 :   return RgC_RgV_mul(gel(x,1), y);
     455             : }
     456             : 
     457             : /* x[i,]*y, l = lg(y) > 1 */
     458             : static GEN
     459    28983593 : RgMrow_RgC_mul_i(GEN x, GEN y, long i, long l)
     460             : {
     461    28983593 :   pari_sp av = avma;
     462    28983593 :   GEN t = gmul(gcoeff(x,i,1), gel(y,1)); /* l > 1 ! */
     463             :   long j;
     464    28983593 :   for (j=2; j<l; j++) t = gadd(t, gmul(gcoeff(x,i,j), gel(y,j)));
     465    28983593 :   return gerepileupto(av,t);
     466             : }
     467             : GEN
     468        4487 : RgMrow_RgC_mul(GEN x, GEN y, long i)
     469        4487 : { return RgMrow_RgC_mul_i(x, y, i, lg(x)); }
     470             : 
     471             : static GEN
     472          28 : RgM_RgC_mul_FpM(GEN x, GEN y, GEN p)
     473             : {
     474          28 :   pari_sp av = avma;
     475             :   GEN r;
     476          28 :   if (lgefint(p) == 3)
     477             :   {
     478          14 :     ulong pp = uel(p, 2);
     479          14 :     r = Flc_to_ZC_inplace(Flm_Flc_mul(RgM_to_Flm(x, pp),
     480             :                                   RgV_to_Flv(y, pp), pp));
     481             :   }
     482             :   else
     483          14 :     r = FpM_FpC_mul(RgM_to_FpM(x, p), RgC_to_FpC(y, p), p);
     484          28 :   return gerepileupto(av, FpC_to_mod(r, p));
     485             : }
     486             : 
     487             : static GEN
     488          14 : RgM_RgC_mul_FqM(GEN x, GEN y, GEN pol, GEN p)
     489             : {
     490          14 :   pari_sp av = avma;
     491          14 :   GEN b, T = RgX_to_FpX(pol, p);
     492          14 :   if (signe(T) == 0) pari_err_OP("*", x, y);
     493          14 :   b = FqM_FqC_mul(RgM_to_FqM(x, T, p), RgC_to_FqC(y, T, p), T, p);
     494          14 :   return gerepileupto(av, FqC_to_mod(b, T, p));
     495             : }
     496             : 
     497             : #define code(t1,t2) ((t1 << 6) | t2)
     498             : static GEN
     499    11617924 : RgM_RgC_mul_fast(GEN x, GEN y)
     500             : {
     501             :   GEN p, pol;
     502             :   long pa;
     503    11617924 :   long t = RgM_RgC_type(x,y, &p,&pol,&pa);
     504    11617924 :   switch(t)
     505             :   {
     506     7812837 :     case t_INT:    return ZM_ZC_mul(x,y);
     507      116423 :     case t_FRAC:   return QM_QC_mul(x,y);
     508          91 :     case t_FFELT:  return FFM_FFC_mul(x, y, pol);
     509          28 :     case t_INTMOD: return RgM_RgC_mul_FpM(x, y, p);
     510             :     case code(t_POLMOD, t_INTMOD):
     511          14 :                    return RgM_RgC_mul_FqM(x, y, pol, p);
     512     3688531 :     default:       return NULL;
     513             :   }
     514             : }
     515             : #undef code
     516             : 
     517             : /* compatible t_MAT * t_COL, lx = lg(x) = lg(y) > 1, l = lgcols(x) */
     518             : static GEN
     519     7868687 : RgM_RgC_mul_i(GEN x, GEN y, long lx, long l)
     520             : {
     521     7868687 :   GEN z = cgetg(l,t_COL);
     522             :   long i;
     523     7868687 :   for (i=1; i<l; i++) gel(z,i) = RgMrow_RgC_mul_i(x,y,i,lx);
     524     7868687 :   return z;
     525             : }
     526             : 
     527             : GEN
     528    11617924 : RgM_RgC_mul(GEN x, GEN y)
     529             : {
     530    11617924 :   long lx = lg(x);
     531             :   GEN z;
     532    11617924 :   if (lx != lg(y)) pari_err_OP("operation 'RgM_RgC_mul'", x,y);
     533    11617924 :   if (lx == 1) return cgetg(1,t_COL);
     534    11617924 :   z = RgM_RgC_mul_fast(x, y);
     535    11617924 :   if (z) return z;
     536     3688531 :   return RgM_RgC_mul_i(x, y, lx, lgcols(x));
     537             : }
     538             : 
     539             : GEN
     540       63555 : RgV_RgM_mul(GEN x, GEN y)
     541             : {
     542       63555 :   long i, lx, ly = lg(y);
     543             :   GEN z;
     544       63555 :   if (ly == 1) return cgetg(1,t_VEC);
     545       63548 :   lx = lg(x);
     546       63548 :   if (lx != lgcols(y)) pari_err_OP("operation 'RgV_RgM_mul'", x,y);
     547       63541 :   z = cgetg(ly, t_VEC);
     548       63541 :   for (i=1; i<ly; i++) gel(z,i) = RgV_dotproduct_i(x, gel(y,i), lx);
     549       63541 :   return z;
     550             : }
     551             : 
     552             : static GEN
     553       56238 : RgM_mul_FpM(GEN x, GEN y, GEN p)
     554             : {
     555       56238 :   pari_sp av = avma;
     556             :   GEN r;
     557       56238 :   if (lgefint(p) == 3)
     558             :   {
     559       56166 :     ulong pp = uel(p, 2);
     560       56166 :     r = Flm_to_ZM_inplace(Flm_mul(RgM_to_Flm(x, pp),
     561             :                                   RgM_to_Flm(y, pp), pp));
     562             :   }
     563             :   else
     564          72 :     r = FpM_mul(RgM_to_FpM(x, p), RgM_to_FpM(y, p), p);
     565       56238 :   return gerepileupto(av, FpM_to_mod(r, p));
     566             : }
     567             : 
     568             : static GEN
     569       26019 : RgM_mul_FqM(GEN x, GEN y, GEN pol, GEN p)
     570             : {
     571       26019 :   pari_sp av = avma;
     572       26019 :   GEN b, T = RgX_to_FpX(pol, p);
     573       26019 :   if (signe(T) == 0) pari_err_OP("*", x, y);
     574       26019 :   b = FqM_mul(RgM_to_FqM(x, T, p), RgM_to_FqM(y, T, p), T, p);
     575       26019 :   return gerepileupto(av, FqM_to_mod(b, T, p));
     576             : }
     577             : 
     578             : static GEN
     579       14812 : RgM_liftred(GEN x, GEN T)
     580       14812 : { return RgXQM_red(liftpol_shallow(x), T); }
     581             : 
     582             : static GEN
     583        2765 : RgM_mul_ZXQM(GEN x, GEN y, GEN T)
     584             : {
     585        2765 :   pari_sp av = avma;
     586        2765 :   GEN b = ZXQM_mul(RgM_liftred(x,T), RgM_liftred(y, T), T);
     587        2765 :   return gerepilecopy(av, QXQM_to_mod_shallow(b,T));
     588             : }
     589             : 
     590             : static GEN
     591         133 : RgM_sqr_ZXQM(GEN x, GEN T)
     592             : {
     593         133 :   pari_sp av = avma;
     594         133 :   GEN b = ZXQM_sqr(RgM_liftred(x, T), T);
     595         133 :   return gerepilecopy(av, QXQM_to_mod_shallow(b,T));
     596             : }
     597             : 
     598             : static GEN
     599        4571 : RgM_mul_QXQM(GEN x, GEN y, GEN T)
     600             : {
     601        4571 :   pari_sp av = avma;
     602        4571 :   GEN b = QXQM_mul(RgM_liftred(x, T), RgM_liftred(y, T), T);
     603        4571 :   return gerepilecopy(av, QXQM_to_mod_shallow(b,T));
     604             : }
     605             : 
     606             : static GEN
     607           7 : RgM_sqr_QXQM(GEN x, GEN T)
     608             : {
     609           7 :   pari_sp av = avma;
     610           7 :   GEN b = QXQM_sqr(RgM_liftred(x, T), T);
     611           7 :   return gerepilecopy(av, QXQM_to_mod_shallow(b,T));
     612             : }
     613             : 
     614             : INLINE int
     615        4620 : RgX_is_monic_ZX(GEN pol)
     616        4620 : { return RgX_is_ZX(pol) && ZX_is_monic(pol); }
     617             : 
     618             : #define code(t1,t2) ((t1 << 6) | t2)
     619             : static GEN
     620     4540127 : RgM_mul_fast(GEN x, GEN y)
     621             : {
     622             :   GEN p, pol;
     623             :   long pa;
     624     4540127 :   long t = RgM_type2(x,y, &p,&pol,&pa);
     625     4540127 :   switch(t)
     626             :   {
     627     2654539 :     case t_INT:    return ZM_mul(x,y);
     628      139165 :     case t_FRAC:   return QM_mul(x,y);
     629        4200 :     case t_FFELT:  return FFM_mul(x, y, pol);
     630       56175 :     case t_INTMOD: return RgM_mul_FpM(x, y, p);
     631             :     case code(t_POLMOD, t_INT):
     632        2772 :                    return ZX_is_monic(pol)? RgM_mul_ZXQM(x, y, pol): NULL;
     633             :     case code(t_POLMOD, t_FRAC):
     634        4592 :                    return RgX_is_monic_ZX(pol)? RgM_mul_QXQM(x, y, pol): NULL;
     635             :     case code(t_POLMOD, t_INTMOD):
     636       26019 :                    return RgM_mul_FqM(x, y, pol, p);
     637     1652665 :     default:       return NULL;
     638             :   }
     639             : }
     640             : 
     641             : static GEN
     642        1183 : RgM_sqr_fast(GEN x)
     643             : {
     644             :   GEN p, pol;
     645             :   long pa;
     646        1183 :   long t = RgM_type(x, &p,&pol,&pa);
     647        1183 :   switch(t)
     648             :   {
     649         126 :     case t_INT:    return ZM_sqr(x);
     650         700 :     case t_FRAC:   return QM_sqr(x);
     651         105 :     case t_FFELT:  return FFM_mul(x, x, pol);
     652          63 :     case t_INTMOD: return RgM_mul_FpM(x, x, p);
     653             :     case code(t_POLMOD, t_INT):
     654         140 :                    return ZX_is_monic(pol)? RgM_sqr_ZXQM(x, pol): NULL;
     655             :     case code(t_POLMOD, t_FRAC):
     656          28 :                    return RgX_is_monic_ZX(pol)? RgM_sqr_QXQM(x, pol): NULL;
     657             :     case code(t_POLMOD, t_INTMOD):
     658           0 :                    return RgM_mul_FqM(x, x, pol, p);
     659          21 :     default:       return NULL;
     660             :   }
     661             : }
     662             : 
     663             : #undef code
     664             : 
     665             : GEN
     666     4562121 : RgM_mul(GEN x, GEN y)
     667             : {
     668     4562121 :   long j, l, lx, ly = lg(y);
     669             :   GEN z;
     670     4562121 :   if (ly == 1) return cgetg(1,t_MAT);
     671     4540148 :   lx = lg(x);
     672     4540148 :   if (lx != lgcols(y)) pari_err_OP("operation 'RgM_mul'", x,y);
     673     4540148 :   if (lx == 1) return zeromat(0,ly-1);
     674     4540127 :   z = RgM_mul_fast(x, y);
     675     4540127 :   if (z) return z;
     676     1652693 :   z = cgetg(ly, t_MAT);
     677     1652693 :   l = lgcols(x);
     678     1652693 :   for (j=1; j<ly; j++) gel(z,j) = RgM_RgC_mul_i(x, gel(y,j), lx, l);
     679     1652693 :   return z;
     680             : }
     681             : 
     682             : GEN
     683        1218 : RgM_sqr(GEN x)
     684             : {
     685        1218 :   long j, lx = lg(x);
     686             :   GEN z;
     687        1218 :   if (lx == 1) return cgetg(1, t_MAT);
     688        1183 :   if (lx != lgcols(x)) pari_err_OP("operation 'RgM_mul'", x,x);
     689        1183 :   z = RgM_sqr_fast(x);
     690        1183 :   if (z) return z;
     691          49 :   z = cgetg(lx, t_MAT);
     692          49 :   for (j=1; j<lx; j++) gel(z,j) = RgM_RgC_mul_i(x, gel(x,j), lx, lx);
     693          49 :   return z;
     694             : }
     695             : 
     696             : /* assume result is symmetric */
     697             : GEN
     698           0 : RgM_multosym(GEN x, GEN y)
     699             : {
     700           0 :   long j, lx, ly = lg(y);
     701             :   GEN M;
     702           0 :   if (ly == 1) return cgetg(1,t_MAT);
     703           0 :   lx = lg(x);
     704           0 :   if (lx != lgcols(y)) pari_err_OP("operation 'RgM_multosym'", x,y);
     705           0 :   if (lx == 1) return cgetg(1,t_MAT);
     706           0 :   if (ly != lgcols(x)) pari_err_OP("operation 'RgM_multosym'", x,y);
     707           0 :   M = cgetg(ly, t_MAT);
     708           0 :   for (j=1; j<ly; j++)
     709             :   {
     710           0 :     GEN z = cgetg(ly,t_COL), yj = gel(y,j);
     711             :     long i;
     712           0 :     for (i=1; i<j; i++) gel(z,i) = gcoeff(M,j,i);
     713           0 :     for (i=j; i<ly; i++)gel(z,i) = RgMrow_RgC_mul_i(x,yj,i,lx);
     714           0 :     gel(M,j) = z;
     715             :   }
     716           0 :   return M;
     717             : }
     718             : /* x~ * y, assuming result is symmetric */
     719             : GEN
     720         496 : RgM_transmultosym(GEN x, GEN y)
     721             : {
     722         496 :   long i, j, l, ly = lg(y);
     723             :   GEN M;
     724         496 :   if (ly == 1) return cgetg(1,t_MAT);
     725         496 :   if (lg(x) != ly) pari_err_OP("operation 'RgM_transmultosym'", x,y);
     726         496 :   l = lgcols(y);
     727         496 :   if (lgcols(x) != l) pari_err_OP("operation 'RgM_transmultosym'", x,y);
     728         496 :   M = cgetg(ly, t_MAT);
     729        2215 :   for (i=1; i<ly; i++)
     730             :   {
     731        1719 :     GEN xi = gel(x,i), c = cgetg(ly,t_COL);
     732        1719 :     gel(M,i) = c;
     733        4712 :     for (j=1; j<i; j++)
     734        2993 :       gcoeff(M,i,j) = gel(c,j) = RgV_dotproduct_i(xi,gel(y,j),l);
     735        1719 :     gel(c,i) = RgV_dotproduct_i(xi,gel(y,i),l);
     736             :   }
     737         496 :   return M;
     738             : }
     739             : /* x~ * y */
     740             : GEN
     741           0 : RgM_transmul(GEN x, GEN y)
     742             : {
     743           0 :   long i, j, l, lx, ly = lg(y);
     744             :   GEN M;
     745           0 :   if (ly == 1) return cgetg(1,t_MAT);
     746           0 :   lx = lg(x);
     747           0 :   l = lgcols(y);
     748           0 :   if (lgcols(x) != l) pari_err_OP("operation 'RgM_transmul'", x,y);
     749           0 :   M = cgetg(ly, t_MAT);
     750           0 :   for (i=1; i<ly; i++)
     751             :   {
     752           0 :     GEN yi = gel(y,i), c = cgetg(lx,t_COL);
     753           0 :     gel(M,i) = c;
     754           0 :     for (j=1; j<lx; j++) gel(c,j) = RgV_dotproduct_i(yi,gel(x,j),l);
     755             :   }
     756           0 :   return M;
     757             : }
     758             : 
     759             : GEN
     760         119 : gram_matrix(GEN x)
     761             : {
     762         119 :   long i,j, l, lx = lg(x);
     763             :   GEN M;
     764         119 :   if (!is_matvec_t(typ(x))) pari_err_TYPE("gram",x);
     765         119 :   if (lx == 1) return cgetg(1,t_MAT);
     766         105 :   l = lgcols(x);
     767         105 :   M = cgetg(lx,t_MAT);
     768         294 :   for (i=1; i<lx; i++)
     769             :   {
     770         189 :     GEN xi = gel(x,i), c = cgetg(lx,t_COL);
     771         189 :     gel(M,i) = c;
     772         280 :     for (j=1; j<i; j++)
     773          91 :       gcoeff(M,i,j) = gel(c,j) = RgV_dotproduct_i(xi,gel(x,j),l);
     774         189 :     gel(c,i) = RgV_dotsquare(xi);
     775             :   }
     776         105 :   return M;
     777             : }
     778             : 
     779             : static GEN
     780        3542 : _RgM_add(void *E, GEN x, GEN y) { (void)E; return RgM_add(x, y); }
     781             : 
     782             : static GEN
     783           0 : _RgM_sub(void *E, GEN x, GEN y) { (void)E; return RgM_sub(x, y); }
     784             : 
     785             : static GEN
     786        5229 : _RgM_cmul(void *E, GEN P, long a, GEN x) { (void)E; return RgM_Rg_mul(x,gel(P,a+2)); }
     787             : 
     788             : static GEN
     789         238 : _RgM_sqr(void *E, GEN x) { (void) E; return RgM_sqr(x); }
     790             : 
     791             : static GEN
     792         686 : _RgM_mul(void *E, GEN x, GEN y) { (void) E; return RgM_mul(x, y); }
     793             : 
     794             : static GEN
     795        3591 : _RgM_one(void *E) { long *n = (long*) E; return matid(*n); }
     796             : 
     797             : static GEN
     798           0 : _RgM_zero(void *E) { long *n = (long*) E; return zeromat(*n,*n); }
     799             : 
     800             : static GEN
     801        2506 : _RgM_red(void *E, GEN x) { (void)E; return x; }
     802             : 
     803             : static struct bb_algebra RgM_algebra = { _RgM_red, _RgM_add, _RgM_sub,
     804             :        _RgM_mul, _RgM_sqr, _RgM_one, _RgM_zero };
     805             : 
     806             : /* generates the list of powers of x of degree 0,1,2,...,l*/
     807             : GEN
     808         168 : RgM_powers(GEN x, long l)
     809             : {
     810         168 :   long n = lg(x)-1;
     811         168 :   return gen_powers(x,l,1,(void *) &n, &_RgM_sqr, &_RgM_mul, &_RgM_one);
     812             : }
     813             : 
     814             : GEN
     815         490 : RgX_RgMV_eval(GEN Q, GEN x)
     816             : {
     817         490 :   long n = lg(x)>1 ? lg(gel(x,1))-1:0;
     818         490 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&n,&RgM_algebra,&_RgM_cmul);
     819             : }
     820             : 
     821             : GEN
     822        1197 : RgX_RgM_eval(GEN Q, GEN x)
     823             : {
     824        1197 :   long n = lg(x)-1;
     825        1197 :   return gen_bkeval(Q,degpol(Q),x,1,(void*)&n,&RgM_algebra,&_RgM_cmul);
     826             : }
     827             : 
     828             : GEN
     829     1201195 : RgC_Rg_div(GEN x, GEN y)
     830     1201195 : { pari_APPLY_type(t_COL, gdiv(gel(x,i),y)) }
     831             : 
     832             : GEN
     833     6157861 : RgC_Rg_mul(GEN x, GEN y)
     834     6157861 : { pari_APPLY_type(t_COL, gmul(gel(x,i),y)) }
     835             : 
     836             : GEN
     837        6867 : RgV_Rg_mul(GEN x, GEN y)
     838        6867 : { pari_APPLY_type(t_VEC, gmul(gel(x,i),y)) }
     839             : 
     840             : GEN
     841      156657 : RgM_Rg_div(GEN X, GEN c) {
     842      156657 :   long i, j, h, l = lg(X);
     843      156657 :   GEN A = cgetg(l, t_MAT);
     844      156657 :   if (l == 1) return A;
     845      156657 :   h = lgcols(X);
     846     1014540 :   for (j=1; j<l; j++)
     847             :   {
     848      857883 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     849      857883 :     for (i = 1; i < h; i++) gel(a,i) = gdiv(gel(x,i), c);
     850      857883 :     gel(A,j) = a;
     851             :   }
     852      156657 :   return A;
     853             : }
     854             : GEN
     855      169365 : RgM_Rg_mul(GEN X, GEN c) {
     856      169365 :   long i, j, h, l = lg(X);
     857      169365 :   GEN A = cgetg(l, t_MAT);
     858      169365 :   if (l == 1) return A;
     859      169008 :   h = lgcols(X);
     860      837123 :   for (j=1; j<l; j++)
     861             :   {
     862      668115 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     863      668115 :     for (i = 1; i < h; i++) gel(a,i) = gmul(gel(x,i), c);
     864      668115 :     gel(A,j) = a;
     865             :   }
     866      169008 :   return A;
     867             : }
     868             : 
     869             : /********************************************************************/
     870             : /*                                                                  */
     871             : /*                    SCALAR TO MATRIX/VECTOR                       */
     872             : /*                                                                  */
     873             : /********************************************************************/
     874             : /* fill the square nxn matrix equal to t*Id */
     875             : static void
     876     5538843 : fill_scalmat(GEN y, GEN t, long n)
     877             : {
     878             :   long i;
     879    21098834 :   for (i = 1; i <= n; i++)
     880             :   {
     881    15559941 :     gel(y,i) = zerocol(n);
     882    15559991 :     gcoeff(y,i,i) = t;
     883             :   }
     884     5538893 : }
     885             : 
     886             : GEN
     887      462196 : scalarmat(GEN x, long n) {
     888      462196 :   GEN y = cgetg(n+1, t_MAT);
     889      462196 :   if (!n) return y;
     890      462196 :   fill_scalmat(y, gcopy(x), n); return y;
     891             : }
     892             : GEN
     893     1780211 : scalarmat_shallow(GEN x, long n) {
     894     1780211 :   GEN y = cgetg(n+1, t_MAT);
     895     1780218 :   fill_scalmat(y, x, n); return y;
     896             : }
     897             : GEN
     898         175 : scalarmat_s(long x, long n) {
     899         175 :   GEN y = cgetg(n+1, t_MAT);
     900         175 :   if (!n) return y;
     901         175 :   fill_scalmat(y, stoi(x), n); return y;
     902             : }
     903             : GEN
     904     3296274 : matid(long n) {
     905             :   GEN y;
     906     3296274 :   if (n < 0) pari_err_DOMAIN("matid", "size", "<", gen_0, stoi(n));
     907     3296267 :   y = cgetg(n+1, t_MAT);
     908     3296267 :   fill_scalmat(y, gen_1, n); return y;
     909             : }
     910             : 
     911             : INLINE GEN
     912      423573 : scalarcol_i(GEN x, long n, long c)
     913             : {
     914             :   long i;
     915      423573 :   GEN y = cgetg(n+1,t_COL);
     916      423573 :   if (!n) return y;
     917      423573 :   gel(y,1) = c? gcopy(x): x;
     918      423573 :   for (i=2; i<=n; i++) gel(y,i) = gen_0;
     919      423573 :   return y;
     920             : }
     921             : 
     922             : GEN
     923      142470 : scalarcol(GEN x, long n) { return scalarcol_i(x,n,1); }
     924             : 
     925             : GEN
     926      281103 : scalarcol_shallow(GEN x, long n) { return scalarcol_i(x,n,0); }
     927             : 
     928             : int
     929       28413 : RgM_isscalar(GEN x, GEN s)
     930             : {
     931       28413 :   long i, j, lx = lg(x);
     932             : 
     933       28413 :   if (lx == 1) return 1;
     934       28413 :   if (lx != lgcols(x)) return 0;
     935       28413 :   if (!s) s = gcoeff(x,1,1);
     936             : 
     937       74774 :   for (j=1; j<lx; j++)
     938             :   {
     939       60025 :     GEN c = gel(x,j);
     940      143999 :     for (i=1; i<j; )
     941       35749 :       if (!gequal0(gel(c,i++))) return 0;
     942             :     /* i = j */
     943       48225 :       if (!gequal(gel(c,i++),s)) return 0;
     944      132181 :     for (   ; i<lx; )
     945       39459 :       if (!gequal0(gel(c,i++))) return 0;
     946             :   }
     947       14749 :   return 1;
     948             : }
     949             : 
     950             : int
     951       12859 : RgM_isidentity(GEN x)
     952             : {
     953       12859 :   long i,j, lx = lg(x);
     954             : 
     955       12859 :   if (lx == 1) return 1;
     956       12859 :   if (lx != lgcols(x)) return 0;
     957       18501 :   for (j=1; j<lx; j++)
     958             :   {
     959       17710 :     GEN c = gel(x,j);
     960       37499 :     for (i=1; i<j; )
     961        5516 :       if (!gequal0(gel(c,i++))) return 0;
     962             :     /* i = j */
     963       14273 :       if (!gequal1(gel(c,i++))) return 0;
     964       17836 :     for (   ; i<lx; )
     965        6552 :       if (!gequal0(gel(c,i++))) return 0;
     966             :   }
     967         791 :   return 1;
     968             : }
     969             : 
     970             : long
     971         308 : RgC_is_ei(GEN x)
     972             : {
     973         308 :   long i, j = 0, l = lg(x);
     974        1792 :   for (i = 1; i < l; i++)
     975             :   {
     976        1484 :     GEN c = gel(x,i);
     977        1484 :     if (gequal0(c)) continue;
     978         308 :     if (!gequal1(c) || j) return 0;
     979         308 :     j = i;
     980             :   }
     981         308 :   return j;
     982             : }
     983             : 
     984             : int
     985         336 : RgM_isdiagonal(GEN x)
     986             : {
     987         336 :   long i,j, lx = lg(x);
     988         336 :   if (lx == 1) return 1;
     989         336 :   if (lx != lgcols(x)) return 0;
     990             : 
     991        3220 :   for (j=1; j<lx; j++)
     992             :   {
     993        2891 :     GEN c = gel(x,j);
     994       18452 :     for (i=1; i<j; i++)
     995       15561 :       if (!gequal0(gel(c,i))) return 0;
     996       18452 :     for (i++; i<lx; i++)
     997       15568 :       if (!gequal0(gel(c,i))) return 0;
     998             :   }
     999         329 :   return 1;
    1000             : }
    1001             : int
    1002         315 : isdiagonal(GEN x) { return (typ(x)==t_MAT) && RgM_isdiagonal(x); }
    1003             : 
    1004             : GEN
    1005       16034 : RgM_det_triangular(GEN mat)
    1006             : {
    1007       16034 :   long i,l = lg(mat);
    1008             :   pari_sp av;
    1009             :   GEN s;
    1010             : 
    1011       16034 :   if (l<3) return l<2? gen_1: gcopy(gcoeff(mat,1,1));
    1012       14858 :   av = avma; s = gcoeff(mat,1,1);
    1013       14858 :   for (i=2; i<l; i++) s = gmul(s,gcoeff(mat,i,i));
    1014       14858 :   return av==avma? gcopy(s): gerepileupto(av,s);
    1015             : }
    1016             : 
    1017             : GEN
    1018       29849 : RgV_kill0(GEN v)
    1019             : {
    1020             :   long i, l;
    1021       29849 :   GEN w = cgetg_copy(v, &l);
    1022    11503131 :   for (i = 1; i < l; i++)
    1023             :   {
    1024    11473278 :     GEN a = gel(v,i);
    1025    11473278 :     gel(w,i) = gequal0(a) ? NULL: a;
    1026             :   }
    1027       29853 :   return w;
    1028             : }

Generated by: LCOV version 1.13