Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - RgV.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21059-cbe0d6a) Lines: 465 538 86.4 %
Date: 2017-09-22 06:24:58 Functions: 79 88 89.8 %
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     4170900 : RgM_is_ZM(GEN x)
      19             : {
      20     4170900 :   long i, j, h, l = lg(x);
      21     4170900 :   if (l == 1) return 1;
      22     4170620 :   h = lgcols(x);
      23     4170620 :   if (h == 1) return 1;
      24    13140055 :   for (j = l-1; j > 0; j--)
      25    61178509 :     for (i = h-1; i > 0; i--)
      26    52208388 :       if (typ(gcoeff(x,i,j)) != t_INT) return 0;
      27     3436345 :   return 1;
      28             : }
      29             : 
      30             : int
      31         112 : RgM_is_QM(GEN x)
      32             : {
      33         112 :   long i, j, h, l = lg(x);
      34         112 :   if (l == 1) return 1;
      35         112 :   h = lgcols(x);
      36         112 :   if (h == 1) return 1;
      37         952 :   for (j = l-1; j > 0; j--)
      38       12068 :     for (i = h-1; i > 0; i--)
      39       11228 :       if (!is_rational_t(typ(gcoeff(x,i,j)))) return 0;
      40          98 :   return 1;
      41             : }
      42             : 
      43             : int
      44          21 : RgV_is_ZMV(GEN V)
      45             : {
      46          21 :   long i, l = lg(V);
      47         231 :   for (i=1; i<l; i++)
      48         210 :     if (typ(gel(V,i))!=t_MAT || !RgM_is_ZM(gel(V,i)))
      49           0 :       return 0;
      50          21 :   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      420500 : RgMrow_zc_mul_i(GEN x, GEN y, long c, long i)
      63             : {
      64      420500 :   pari_sp av = avma;
      65      420500 :   GEN s = NULL;
      66             :   long j;
      67    15925197 :   for (j=1; j<c; j++)
      68             :   {
      69    15504697 :     long t = y[j];
      70    15504697 :     if (!t) continue;
      71     1789806 :     if (!s) { s = gmulgs(gcoeff(x,i,j),t); continue; }
      72     1372547 :     switch(t)
      73             :     {
      74      636442 :       case  1: s = gadd(s, gcoeff(x,i,j)); break;
      75      244972 :       case -1: s = gsub(s, gcoeff(x,i,j)); break;
      76      491133 :       default: s = gadd(s, gmulgs(gcoeff(x,i,j), t)); break;
      77             :     }
      78             :   }
      79      420500 :   if (!s) { avma = av; return gen_0; }
      80      417259 :   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      107540 : RgM_zc_mul_i(GEN x, GEN y, long c, long l)
      87             : {
      88      107540 :   GEN z = cgetg(l,t_COL);
      89             :   long i;
      90      107540 :   for (i = 1; i < l; i++) gel(z,i) = RgMrow_zc_mul_i(x,y,c,i);
      91      107540 :   return z;
      92             : }
      93             : GEN
      94       71925 : 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        9719 : RgM_zm_mul(GEN x, GEN y)
      98             : {
      99        9719 :   long j, c, l = lg(x), ly = lg(y);
     100        9719 :   GEN z = cgetg(ly, t_MAT);
     101        9719 :   if (l == 1) return z;
     102        9719 :   c = lgcols(x);
     103        9719 :   for (j = 1; j < ly; j++) gel(z,j) = RgM_zc_mul_i(x, gel(y,j), l,c);
     104        9719 :   return z;
     105             : }
     106             : 
     107             : static GEN
     108       30917 : RgV_zc_mul_i(GEN x, GEN y, long l)
     109             : {
     110             :   long i;
     111       30917 :   GEN z = gen_0;
     112       30917 :   pari_sp av = avma;
     113       30917 :   for (i = 1; i < l; i++) z = gadd(z, gmulgs(gel(x,i), y[i]));
     114       30917 :   return gerepileupto(av, z);
     115             : }
     116             : GEN
     117          28 : RgV_zc_mul(GEN x, GEN y) { return RgV_zc_mul_i(x, y, lg(x)); }
     118             : 
     119             : GEN
     120        7452 : RgV_zm_mul(GEN x, GEN y)
     121             : {
     122        7452 :   long j, l = lg(x), ly = lg(y);
     123        7452 :   GEN z = cgetg(ly, t_VEC);
     124        7452 :   for (j = 1; j < ly; j++) gel(z,j) = RgV_zc_mul_i(x, gel(y,j), l);
     125        7452 :   return z;
     126             : }
     127             : 
     128             : /* scalar product x.x */
     129             : GEN
     130        1197 : RgV_dotsquare(GEN x)
     131             : {
     132        1197 :   long i, lx = lg(x);
     133        1197 :   pari_sp av = avma;
     134             :   GEN z;
     135        1197 :   if (lx == 1) return gen_0;
     136        1197 :   z = gsqr(gel(x,1));
     137        4774 :   for (i=2; i<lx; i++)
     138             :   {
     139        3577 :     z = gadd(z, gsqr(gel(x,i)));
     140        3577 :     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        1197 :   return gerepileupto(av,z);
     147             : }
     148             : 
     149             : /* scalar product x.y, lx = lg(x) = lg(y) */
     150             : static GEN
     151     1113218 : RgV_dotproduct_i(GEN x, GEN y, long lx)
     152             : {
     153     1113218 :   pari_sp av = avma;
     154             :   long i;
     155             :   GEN z;
     156     1113218 :   if (lx == 1) return gen_0;
     157     1112679 :   z = gmul(gel(x,1),gel(y,1));
     158    41219776 :   for (i=2; i<lx; i++)
     159             :   {
     160    40107097 :     z = gadd(z, gmul(gel(x,i), gel(y,i)));
     161    40107097 :     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     1112679 :   return gerepileupto(av,z);
     168             : }
     169             : GEN
     170      146830 : RgV_dotproduct(GEN x,GEN y)
     171             : {
     172      146830 :   if (x == y) return RgV_dotsquare(x);
     173      146830 :   return RgV_dotproduct_i(x, y, lg(x));
     174             : }
     175             : /* v[1] + ... + v[lg(v)-1] */
     176             : GEN
     177      318061 : RgV_sum(GEN v)
     178             : {
     179             :   GEN p;
     180      318061 :   long i, l = lg(v);
     181      318061 :   if (l == 1) return gen_0;
     182      318061 :   p = gel(v,1); for (i=2; i<l; i++) p = gadd(p, gel(v,i));
     183      318061 :   return p;
     184             : }
     185             : /* v[1] + ... + v[n]. Assume lg(v) > n. */
     186             : GEN
     187         518 : RgV_sumpart(GEN v, long n)
     188             : {
     189             :   GEN p;
     190             :   long i;
     191         518 :   if (!n) return gen_0;
     192         518 :   p = gel(v,1); for (i=2; i<=n; i++) p = gadd(p, gel(v,i));
     193         518 :   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         362 : RgM_sumcol(GEN A)
     207             : {
     208         362 :   long i,j,m,l = lg(A);
     209             :   GEN v;
     210             : 
     211         362 :   if (l == 1) return cgetg(1,t_MAT);
     212         362 :   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      727211 : _gmul(void *data, GEN x, GEN y)
     227      727211 : { (void)data; return gmul(x,y); }
     228             : 
     229             : GEN
     230       39511 : RgV_prod(GEN x)
     231             : {
     232       39511 :   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        5256 : RgM_Rg_add(GEN x, GEN y)
     239             : {
     240        5256 :   long l = lg(x), i, j;
     241        5256 :   GEN z = cgetg(l,t_MAT);
     242             : 
     243        5256 :   if (l==1) return z;
     244        5256 :   if (l != lgcols(x)) pari_err_OP( "+", x, y);
     245        5256 :   z = cgetg(l,t_MAT);
     246       40520 :   for (i=1; i<l; i++)
     247             :   {
     248       35264 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     249       35264 :     gel(z,i) = zi;
     250     1885962 :     for (j=1; j<l; j++)
     251     1850698 :       gel(zi,j) = i==j? gadd(y,gel(xi,j)): gcopy(gel(xi,j));
     252             :   }
     253        5256 :   return z;
     254             : }
     255             : GEN
     256           0 : RgM_Rg_sub(GEN x, GEN y)
     257             : {
     258           0 :   long l = lg(x), i, j;
     259           0 :   GEN z = cgetg(l,t_MAT);
     260             : 
     261           0 :   if (l==1) return z;
     262           0 :   if (l != lgcols(x)) pari_err_OP( "-", x, y);
     263           0 :   z = cgetg(l,t_MAT);
     264           0 :   for (i=1; i<l; i++)
     265             :   {
     266           0 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     267           0 :     gel(z,i) = zi;
     268           0 :     for (j=1; j<l; j++)
     269           0 :       gel(zi,j) = i==j? gsub(y,gel(xi,j)): gcopy(gel(xi,j));
     270             :   }
     271           0 :   return z;
     272             : }
     273             : GEN
     274         539 : RgM_Rg_add_shallow(GEN x, GEN y)
     275             : {
     276         539 :   long l = lg(x), i, j;
     277         539 :   GEN z = cgetg(l,t_MAT);
     278             : 
     279         539 :   if (l==1) return z;
     280         434 :   if (l != lgcols(x)) pari_err_OP( "+", x, y);
     281        1792 :   for (i=1; i<l; i++)
     282             :   {
     283        1358 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     284        1358 :     gel(z,i) = zi;
     285        1358 :     for (j=1; j<l; j++) gel(zi,j) = gel(xi,j);
     286        1358 :     gel(zi,i) = gadd(gel(zi,i), y);
     287             :   }
     288         434 :   return z;
     289             : }
     290             : GEN
     291       50161 : RgM_Rg_sub_shallow(GEN x, GEN y)
     292             : {
     293       50161 :   long l = lg(x), i, j;
     294       50161 :   GEN z = cgetg(l,t_MAT);
     295             : 
     296       50161 :   if (l==1) return z;
     297       50161 :   if (l != lgcols(x)) pari_err_OP( "-", x, y);
     298      580820 :   for (i=1; i<l; i++)
     299             :   {
     300      530659 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     301      530659 :     gel(z,i) = zi;
     302      530659 :     for (j=1; j<l; j++) gel(zi,j) = gel(xi,j);
     303      530659 :     gel(zi,i) = gsub(gel(zi,i), y);
     304             :   }
     305       50161 :   return z;
     306             : }
     307             : 
     308             : GEN
     309     3028330 : RgC_Rg_add(GEN x, GEN y)
     310             : {
     311     3028330 :   long k, lx = lg(x);
     312     3028330 :   GEN z = cgetg(lx, t_COL);
     313     3028330 :   if (lx == 1)
     314             :   {
     315           7 :     if (isintzero(y)) return z;
     316           0 :     pari_err_TYPE2("+",x,y);
     317             :   }
     318     3028323 :   gel(z,1) = gadd(y,gel(x,1));
     319     3028323 :   for (k = 2; k < lx; k++) gel(z,k) = gcopy(gel(x,k));
     320     3028323 :   return z;
     321             : }
     322             : GEN
     323       21133 : RgC_Rg_sub(GEN x, GEN y)
     324             : {
     325       21133 :   long k, lx = lg(x);
     326       21133 :   GEN z = cgetg(lx, t_COL);
     327       21133 :   if (lx == 1)
     328             :   {
     329           0 :     if (isintzero(y)) return z;
     330           0 :     pari_err_TYPE2("-",x,y);
     331             :   }
     332       21133 :   gel(z,1) = gsub(gel(x,1), y);
     333       21133 :   for (k = 2; k < lx; k++) gel(z,k) = gcopy(gel(x,k));
     334       21133 :   return z;
     335             : }
     336             : /* a - x */
     337             : GEN
     338      101586 : Rg_RgC_sub(GEN a, GEN x)
     339             : {
     340      101586 :   long k, lx = lg(x);
     341      101586 :   GEN z = cgetg(lx,t_COL);
     342      101586 :   if (lx == 1)
     343             :   {
     344           0 :     if (isintzero(a)) return z;
     345           0 :     pari_err_TYPE2("-",a,x);
     346             :   }
     347      101586 :   gel(z,1) = gsub(a, gel(x,1));
     348      101586 :   for (k = 2; k < lx; k++) gel(z,k) = gneg(gel(x,k));
     349      101586 :   return z;
     350             : }
     351             : 
     352             : 
     353             : static GEN
     354    11813694 : RgC_add_i(GEN x, GEN y, long lx)
     355             : {
     356    11813694 :   GEN A = cgetg(lx, t_COL);
     357             :   long i;
     358    11813694 :   for (i=1; i<lx; i++) gel(A,i) = gadd(gel(x,i), gel(y,i));
     359    11813694 :   return A;
     360             : }
     361             : GEN
     362     9440400 : RgC_add(GEN x, GEN y) { return RgC_add_i(x, y, lg(x)); }
     363             : GEN
     364      609923 : RgV_add(GEN x, GEN y)
     365             : {
     366      609923 :   long i, lx = lg(x);
     367      609923 :   GEN A = cgetg(lx, t_VEC);
     368      609923 :   for (i=1; i<lx; i++) gel(A,i) = gadd(gel(x,i), gel(y,i));
     369      609923 :   return A;
     370             : }
     371             : 
     372             : static GEN
     373     2148462 : RgC_sub_i(GEN x, GEN y, long lx)
     374             : {
     375             :   long i;
     376     2148462 :   GEN A = cgetg(lx, t_COL);
     377     2148462 :   for (i=1; i<lx; i++) gel(A,i) = gsub(gel(x,i), gel(y,i));
     378     2148462 :   return A;
     379             : }
     380             : GEN
     381     2109739 : RgC_sub(GEN x, GEN y) { return RgC_sub_i(x, y, lg(x)); }
     382             : GEN
     383       73612 : RgV_sub(GEN x, GEN y)
     384             : {
     385       73612 :   long i, lx = lg(x);
     386       73612 :   GEN A = cgetg(lx, t_VEC);
     387       73612 :   for (i=1; i<lx; i++) gel(A,i) = gsub(gel(x,i), gel(y,i));
     388       73612 :   return A;
     389             : }
     390             : 
     391             : GEN
     392      497490 : RgM_add(GEN x, GEN y)
     393             : {
     394      497490 :   long lx = lg(x), l, j;
     395             :   GEN z;
     396      497490 :   if (lx == 1) return cgetg(1, t_MAT);
     397      497490 :   z = cgetg(lx, t_MAT); l = lgcols(x);
     398      497490 :   for (j = 1; j < lx; j++) gel(z,j) = RgC_add_i(gel(x,j), gel(y,j), l);
     399      497490 :   return z;
     400             : }
     401             : GEN
     402        9999 : RgM_sub(GEN x, GEN y)
     403             : {
     404        9999 :   long lx = lg(x), l, j;
     405             :   GEN z;
     406        9999 :   if (lx == 1) return cgetg(1, t_MAT);
     407        9999 :   z = cgetg(lx, t_MAT); l = lgcols(x);
     408        9999 :   for (j = 1; j < lx; j++) gel(z,j) = RgC_sub_i(gel(x,j), gel(y,j), l);
     409        9999 :   return z;
     410             : }
     411             : 
     412             : static GEN
     413     2789919 : RgC_neg_i(GEN x, long lx)
     414             : {
     415             :   long i;
     416     2789919 :   GEN y = cgetg(lx, t_COL);
     417     2789919 :   for (i=1; i<lx; i++) gel(y,i) = gneg(gel(x,i));
     418     2789919 :   return y;
     419             : }
     420             : GEN
     421      123766 : RgC_neg(GEN x) { return RgC_neg_i(x, lg(x)); }
     422             : GEN
     423       25026 : RgV_neg(GEN x)
     424             : {
     425       25026 :   long i, lx = lg(x);
     426       25026 :   GEN y = cgetg(lx, t_VEC);
     427       25026 :   for (i=1; i<lx; i++) gel(y,i) = gneg(gel(x,i));
     428       25026 :   return y;
     429             : }
     430             : GEN
     431      476756 : RgM_neg(GEN x)
     432             : {
     433      476756 :   long i, hx, lx = lg(x);
     434      476756 :   GEN y = cgetg(lx, t_MAT);
     435      476756 :   if (lx == 1) return y;
     436      476749 :   hx = lgcols(x);
     437      476749 :   for (i=1; i<lx; i++) gel(y,i) = RgC_neg_i(gel(x,i), hx);
     438      476749 :   return y;
     439             : }
     440             : 
     441             : GEN
     442      142447 : RgV_RgC_mul(GEN x, GEN y)
     443             : {
     444      142447 :   long lx = lg(x);
     445      142447 :   if (lx != lg(y)) pari_err_OP("operation 'RgV_RgC_mul'", x, y);
     446      142384 :   return RgV_dotproduct_i(x, y, lx);
     447             : }
     448             : GEN
     449           7 : RgC_RgV_mul(GEN x, GEN y)
     450             : {
     451           7 :   long i, ly = lg(y);
     452           7 :   GEN z = cgetg(ly,t_MAT);
     453           7 :   for (i=1; i<ly; i++) gel(z,i) = RgC_Rg_mul(x, gel(y,i));
     454           7 :   return z;
     455             : }
     456             : GEN
     457           0 : RgC_RgM_mul(GEN x, GEN y)
     458             : {
     459           0 :   long i, ly = lg(y);
     460           0 :   GEN z = cgetg(ly,t_MAT);
     461           0 :   if (ly != 1 && lgcols(y) != 2) pari_err_OP("operation 'RgC_RgM_mul'",x,y);
     462           0 :   for (i=1; i<ly; i++) gel(z,i) = RgC_Rg_mul(x, gcoeff(y,1,i));
     463           0 :   return z;
     464             : }
     465             : GEN
     466           0 : RgM_RgV_mul(GEN x, GEN y)
     467             : {
     468           0 :   if (lg(x) != 2) pari_err_OP("operation 'RgM_RgV_mul'", x,y);
     469           0 :   return RgC_RgV_mul(gel(x,1), y);
     470             : }
     471             : 
     472             : /* x[i,]*y, l = lg(y) > 1 */
     473             : static GEN
     474    91588203 : RgMrow_RgC_mul_i(GEN x, GEN y, long i, long l)
     475             : {
     476    91588203 :   pari_sp av = avma;
     477    91588203 :   GEN t = gmul(gcoeff(x,i,1), gel(y,1)); /* l > 1 ! */
     478             :   long j;
     479    91588199 :   for (j=2; j<l; j++) t = gadd(t, gmul(gcoeff(x,i,j), gel(y,j)));
     480    91588201 :   return gerepileupto(av,t);
     481             : }
     482             : GEN
     483        1050 : RgMrow_RgC_mul(GEN x, GEN y, long i)
     484        1050 : { return RgMrow_RgC_mul_i(x, y, i, lg(x)); }
     485             : 
     486             : /* compatible t_MAT * t_COL, lx = lg(x) = lg(y) > 1, l = lgcols(x) */
     487             : static GEN
     488    12661634 : RgM_RgC_mul_i(GEN x, GEN y, long lx, long l)
     489             : {
     490    12661634 :   GEN z = cgetg(l,t_COL);
     491             :   long i;
     492    12661633 :   for (i=1; i<l; i++) gel(z,i) = RgMrow_RgC_mul_i(x,y,i,lx);
     493    12661635 :   return z;
     494             : }
     495             : 
     496             : GEN
     497     9073058 : RgM_RgC_mul(GEN x, GEN y)
     498             : {
     499     9073058 :   long lx = lg(x);
     500     9073058 :   GEN ffx = NULL, ffy = NULL;
     501     9073058 :   if (lx != lg(y)) pari_err_OP("operation 'RgM_RgC_mul'", x,y);
     502     9073058 :   if (lx == 1) return cgetg(1,t_COL);
     503     9073058 :   if (RgM_is_FFM(x, &ffx) && RgC_is_FFC(y, &ffy)) {
     504          77 :     if (!FF_samefield(ffx, ffy))
     505           0 :       pari_err_OP("*", ffx, ffy);
     506          77 :     return FFM_FFC_mul(x, y, ffx);
     507             :   }
     508     9072980 :   return RgM_RgC_mul_i(x, y, lx, lgcols(x));
     509             : }
     510             : 
     511             : GEN
     512       56674 : RgV_RgM_mul(GEN x, GEN y)
     513             : {
     514       56674 :   long i, lx, ly = lg(y);
     515             :   GEN z;
     516       56674 :   if (ly == 1) return cgetg(1,t_VEC);
     517       56667 :   lx = lg(x);
     518       56667 :   if (lx != lgcols(y)) pari_err_OP("operation 'RgV_RgM_mul'", x,y);
     519       56660 :   z = cgetg(ly, t_VEC);
     520       56660 :   for (i=1; i<ly; i++) gel(z,i) = RgV_dotproduct_i(x, gel(y,i), lx);
     521       56660 :   return z;
     522             : }
     523             : 
     524             : static int
     525      700891 : is_modular_mul(GEN a, GEN b, GEN *z)
     526             : {
     527      700891 :   GEN p1 = NULL, p2 = NULL, p;
     528             :   ulong pp;
     529      700891 :   if (!RgM_is_FpM(a, &p1) || !p1) return 0;
     530         112 :   if (!RgM_is_FpM(b, &p2) || !p2) return 0;
     531         112 :   p = gcdii(p1, p2);
     532         112 :   a = RgM_Fp_init(a, p, &pp);
     533         112 :   switch(pp)
     534             :   {
     535             :   case 0:
     536          15 :     b = RgM_to_FpM(b,p);
     537          15 :     b = FpM_mul(a,b,p);
     538          15 :     *z = FpM_to_mod(b,p);
     539          15 :     break;
     540             :   case 2:
     541          28 :     b = RgM_to_F2m(b);
     542          28 :     b = F2m_mul(a,b);
     543          28 :     *z = F2m_to_mod(b);
     544          28 :     break;
     545             :   default:
     546          69 :     b = RgM_to_Flm(b,pp);
     547          69 :     b = Flm_mul(a,b,pp);
     548          69 :     *z = Flm_to_mod(b,pp);
     549             :   }
     550         112 :   return 1;
     551             : }
     552             : static int
     553         931 : is_modular_sqr(GEN a, GEN *z)
     554             : {
     555         931 :   GEN p = NULL;
     556             :   ulong pp;
     557         931 :   if (!RgM_is_FpM(a, &p) || !p) return 0;
     558          63 :   a = RgM_Fp_init(a, p, &pp);
     559          63 :   switch(pp)
     560             :   {
     561          15 :     case 0: *z = FpM_to_mod(FpM_mul(a,a, p), p); break;
     562          14 :     case 2: *z = F2m_to_mod(F2m_mul(a,a)); break;
     563          34 :     default:*z = Flm_to_mod(Flm_mul(a,a, pp), pp); break;
     564             :   }
     565          63 :   return 1;
     566             : }
     567             : 
     568             : GEN
     569     2352607 : RgM_mul(GEN x, GEN y)
     570             : {
     571     2352607 :   pari_sp av = avma;
     572     2352607 :   long j, l, lx, ly = lg(y);
     573     2352607 :   GEN z, ffx = NULL, ffy = NULL;
     574     2352607 :   if (ly == 1) return cgetg(1,t_MAT);
     575     2346909 :   lx = lg(x);
     576     2346909 :   if (lx != lgcols(y)) pari_err_OP("operation 'RgM_mul'", x,y);
     577     2346909 :   if (lx == 1) return zeromat(0,ly-1);
     578     2346888 :   if (RgM_is_ZM(x) && RgM_is_ZM(y))
     579     1645997 :     return ZM_mul(x, y);
     580      700891 :   if (is_modular_mul(x,y,&z)) return gerepileupto(av, z);
     581      700779 :   if (RgM_is_FFM(x, &ffx) && RgM_is_FFM(y, &ffy)) {
     582         168 :     if (!FF_samefield(ffx, ffy))
     583           0 :       pari_err_OP("*", ffx, ffy);
     584         168 :     return FFM_mul(x, y, ffx);
     585             :   }
     586      700611 :   z = cgetg(ly, t_MAT);
     587      700611 :   l = lgcols(x);
     588      700611 :   for (j=1; j<ly; j++) gel(z,j) = RgM_RgC_mul_i(x, gel(y,j), lx, l);
     589      700611 :   return z;
     590             : }
     591             : /* assume result is symmetric */
     592             : GEN
     593           0 : RgM_multosym(GEN x, GEN y)
     594             : {
     595           0 :   long j, lx, ly = lg(y);
     596             :   GEN M;
     597           0 :   if (ly == 1) return cgetg(1,t_MAT);
     598           0 :   lx = lg(x);
     599           0 :   if (lx != lgcols(y)) pari_err_OP("operation 'RgM_multosym'", x,y);
     600           0 :   if (lx == 1) return cgetg(1,t_MAT);
     601           0 :   if (ly != lgcols(x)) pari_err_OP("operation 'RgM_multosym'", x,y);
     602           0 :   M = cgetg(ly, t_MAT);
     603           0 :   for (j=1; j<ly; j++)
     604             :   {
     605           0 :     GEN z = cgetg(ly,t_COL), yj = gel(y,j);
     606             :     long i;
     607           0 :     for (i=1; i<j; i++) gel(z,i) = gcoeff(M,j,i);
     608           0 :     for (i=j; i<ly; i++)gel(z,i) = RgMrow_RgC_mul_i(x,yj,i,lx);
     609           0 :     gel(M,j) = z;
     610             :   }
     611           0 :   return M;
     612             : }
     613             : /* x~ * y, assuming result is symmetric */
     614             : GEN
     615         447 : RgM_transmultosym(GEN x, GEN y)
     616             : {
     617         447 :   long i, j, l, ly = lg(y);
     618             :   GEN M;
     619         447 :   if (ly == 1) return cgetg(1,t_MAT);
     620         447 :   if (lg(x) != ly) pari_err_OP("operation 'RgM_transmultosym'", x,y);
     621         447 :   l = lgcols(y);
     622         447 :   if (lgcols(x) != l) pari_err_OP("operation 'RgM_transmultosym'", x,y);
     623         447 :   M = cgetg(ly, t_MAT);
     624        2019 :   for (i=1; i<ly; i++)
     625             :   {
     626        1572 :     GEN xi = gel(x,i), c = cgetg(ly,t_COL);
     627        1572 :     gel(M,i) = c;
     628        4418 :     for (j=1; j<i; j++)
     629        2846 :       gcoeff(M,i,j) = gel(c,j) = RgV_dotproduct_i(xi,gel(y,j),l);
     630        1572 :     gel(c,i) = RgV_dotproduct_i(xi,gel(y,i),l);
     631             :   }
     632         447 :   return M;
     633             : }
     634             : /* x~ * y */
     635             : GEN
     636           0 : RgM_transmul(GEN x, GEN y)
     637             : {
     638           0 :   long i, j, l, lx, ly = lg(y);
     639             :   GEN M;
     640           0 :   if (ly == 1) return cgetg(1,t_MAT);
     641           0 :   lx = lg(x);
     642           0 :   l = lgcols(y);
     643           0 :   if (lgcols(x) != l) pari_err_OP("operation 'RgM_transmul'", x,y);
     644           0 :   M = cgetg(ly, t_MAT);
     645           0 :   for (i=1; i<ly; i++)
     646             :   {
     647           0 :     GEN yi = gel(y,i), c = cgetg(lx,t_COL);
     648           0 :     gel(M,i) = c;
     649           0 :     for (j=1; j<lx; j++) gel(c,j) = RgV_dotproduct_i(yi,gel(x,j),l);
     650             :   }
     651           0 :   return M;
     652             : }
     653             : 
     654             : GEN
     655         119 : gram_matrix(GEN x)
     656             : {
     657         119 :   long i,j, l, lx = lg(x);
     658             :   GEN M;
     659         119 :   if (!is_matvec_t(typ(x))) pari_err_TYPE("gram",x);
     660         119 :   if (lx == 1) return cgetg(1,t_MAT);
     661         105 :   l = lgcols(x);
     662         105 :   M = cgetg(lx,t_MAT);
     663         294 :   for (i=1; i<lx; i++)
     664             :   {
     665         189 :     GEN xi = gel(x,i), c = cgetg(lx,t_COL);
     666         189 :     gel(M,i) = c;
     667         280 :     for (j=1; j<i; j++)
     668          91 :       gcoeff(M,i,j) = gel(c,j) = RgV_dotproduct_i(xi,gel(x,j),l);
     669         189 :     gel(c,i) = RgV_dotsquare(xi);
     670             :   }
     671         105 :   return M;
     672             : }
     673             : 
     674             : GEN
     675        1043 : RgM_sqr(GEN x)
     676             : {
     677        1043 :   pari_sp av = avma;
     678        1043 :   long j, lx = lg(x);
     679        1043 :   GEN z, ffx = NULL;
     680        1043 :   if (lx == 1) return cgetg(1, t_MAT);
     681        1008 :   if (lx != lgcols(x)) pari_err_OP("operation 'RgM_mul'", x,x);
     682        1008 :   if (RgM_is_ZM(x))         return ZM_sqr(x);
     683         931 :   if (is_modular_sqr(x,&z)) return gerepileupto(av, z);
     684         868 :   if (RgM_is_FFM(x, &ffx))  return FFM_mul(x, x, ffx);
     685         840 :   z = cgetg(lx, t_MAT);
     686         840 :   for (j=1; j<lx; j++) gel(z,j) = RgM_RgC_mul_i(x, gel(x,j), lx, lx);
     687         840 :   return z;
     688             : }
     689             : 
     690             : static GEN
     691        1841 : _RgM_add(void *E, GEN x, GEN y) { (void)E; return RgM_add(x, y); }
     692             : 
     693             : static GEN
     694           0 : _RgM_sub(void *E, GEN x, GEN y) { (void)E; return RgM_sub(x, y); }
     695             : 
     696             : static GEN
     697        3122 : _RgM_cmul(void *E, GEN P, long a, GEN x) { (void)E; return RgM_Rg_mul(x,gel(P,a+2)); }
     698             : 
     699             : static GEN
     700         119 : _RgM_sqr(void *E, GEN x) { (void) E; return RgM_sqr(x); }
     701             : 
     702             : static GEN
     703         273 : _RgM_mul(void *E, GEN x, GEN y) { (void) E; return RgM_mul(x, y); }
     704             : 
     705             : static GEN
     706        2471 : _RgM_one(void *E) { long *n = (long*) E; return matid(*n); }
     707             : 
     708             : static GEN
     709           0 : _RgM_zero(void *E) { long *n = (long*) E; return zeromat(*n,*n); }
     710             : 
     711             : static GEN
     712        1645 : _RgM_red(void *E, GEN x) { (void)E; return x; }
     713             : 
     714             : static struct bb_algebra RgM_algebra = { _RgM_red, _RgM_add, _RgM_sub,
     715             :        _RgM_mul, _RgM_sqr, _RgM_one, _RgM_zero };
     716             : 
     717             : /* generates the list of powers of x of degree 0,1,2,...,l*/
     718             : GEN
     719         154 : RgM_powers(GEN x, long l)
     720             : {
     721         154 :   long n = lg(x)-1;
     722         154 :   return gen_powers(x,l,1,(void *) &n, &_RgM_sqr, &_RgM_mul, &_RgM_one);
     723             : }
     724             : 
     725             : GEN
     726         462 : RgX_RgMV_eval(GEN Q, GEN x)
     727             : {
     728         462 :   long n = lg(x)>1 ? lg(gel(x,1))-1:0;
     729         462 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&n,&RgM_algebra,&_RgM_cmul);
     730             : }
     731             : 
     732             : GEN
     733         819 : RgX_RgM_eval(GEN Q, GEN x)
     734             : {
     735         819 :   long n = lg(x)-1;
     736         819 :   return gen_bkeval(Q,degpol(Q),x,1,(void*)&n,&RgM_algebra,&_RgM_cmul);
     737             : }
     738             : 
     739             : GEN
     740     2158565 : RgC_Rg_div(GEN x, GEN y) {
     741     2158565 :   long i, lx = lg(x);
     742     2158565 :   GEN z = cgetg(lx, t_COL);
     743     2158565 :   for (i=1; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
     744     2158565 :   return z;
     745             : }
     746             : GEN
     747     5731416 : RgC_Rg_mul(GEN x, GEN y) {
     748     5731416 :   long i, lx = lg(x);
     749     5731416 :   GEN z = cgetg(lx, t_COL);
     750     5731416 :   for (i=1; i<lx; i++) gel(z,i) = gmul(gel(x,i),y);
     751     5731416 :   return z;
     752             : }
     753             : GEN
     754        8639 : RgV_Rg_mul(GEN x, GEN y) {
     755        8639 :   long i, lx = lg(x);
     756        8639 :   GEN z = cgetg(lx, t_VEC);
     757        8639 :   for (i=1; i<lx; i++) gel(z,i) = gmul(gel(x,i),y);
     758        8639 :   return z;
     759             : }
     760             : GEN
     761      138381 : RgM_Rg_div(GEN X, GEN c) {
     762      138381 :   long i, j, h, l = lg(X);
     763      138381 :   GEN A = cgetg(l, t_MAT);
     764      138381 :   if (l == 1) return A;
     765      138332 :   h = lgcols(X);
     766      924932 :   for (j=1; j<l; j++)
     767             :   {
     768      786600 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     769      786600 :     for (i = 1; i < h; i++) gel(a,i) = gdiv(gel(x,i), c);
     770      786600 :     gel(A,j) = a;
     771             :   }
     772      138332 :   return A;
     773             : }
     774             : GEN
     775      220670 : RgM_Rg_mul(GEN X, GEN c) {
     776      220670 :   long i, j, h, l = lg(X);
     777      220670 :   GEN A = cgetg(l, t_MAT);
     778      220670 :   if (l == 1) return A;
     779      220635 :   h = lgcols(X);
     780     1220541 :   for (j=1; j<l; j++)
     781             :   {
     782      999906 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     783      999906 :     for (i = 1; i < h; i++) gel(a,i) = gmul(gel(x,i), c);
     784      999906 :     gel(A,j) = a;
     785             :   }
     786      220635 :   return A;
     787             : }
     788             : 
     789             : /********************************************************************/
     790             : /*                                                                  */
     791             : /*                    SCALAR TO MATRIX/VECTOR                       */
     792             : /*                                                                  */
     793             : /********************************************************************/
     794             : /* fill the square nxn matrix equal to t*Id */
     795             : static void
     796     3691063 : fill_scalmat(GEN y, GEN t, long n)
     797             : {
     798             :   long i;
     799    16578698 :   for (i = 1; i <= n; i++)
     800             :   {
     801    12887635 :     gel(y,i) = zerocol(n);
     802    12887635 :     gcoeff(y,i,i) = t;
     803             :   }
     804     3691063 : }
     805             : 
     806             : GEN
     807      436012 : scalarmat(GEN x, long n) {
     808      436012 :   GEN y = cgetg(n+1, t_MAT);
     809      436012 :   if (!n) return y;
     810      436012 :   fill_scalmat(y, gcopy(x), n); return y;
     811             : }
     812             : GEN
     813     1389525 : scalarmat_shallow(GEN x, long n) {
     814     1389525 :   GEN y = cgetg(n+1, t_MAT);
     815     1389525 :   fill_scalmat(y, x, n); return y;
     816             : }
     817             : GEN
     818         140 : scalarmat_s(long x, long n) {
     819         140 :   GEN y = cgetg(n+1, t_MAT);
     820         140 :   if (!n) return y;
     821         140 :   fill_scalmat(y, stoi(x), n); return y;
     822             : }
     823             : GEN
     824     1865393 : matid(long n) {
     825             :   GEN y;
     826     1865393 :   if (n < 0) pari_err_DOMAIN("matid", "size", "<", gen_0, stoi(n));
     827     1865386 :   y = cgetg(n+1, t_MAT);
     828     1865386 :   fill_scalmat(y, gen_1, n); return y;
     829             : }
     830             : 
     831             : INLINE GEN
     832      319926 : scalarcol_i(GEN x, long n, long c)
     833             : {
     834             :   long i;
     835      319926 :   GEN y = cgetg(n+1,t_COL);
     836      319926 :   if (!n) return y;
     837      319926 :   gel(y,1) = c? gcopy(x): x;
     838      319926 :   for (i=2; i<=n; i++) gel(y,i) = gen_0;
     839      319926 :   return y;
     840             : }
     841             : 
     842             : GEN
     843       78625 : scalarcol(GEN x, long n) { return scalarcol_i(x,n,1); }
     844             : 
     845             : GEN
     846      241301 : scalarcol_shallow(GEN x, long n) { return scalarcol_i(x,n,0); }
     847             : 
     848             : int
     849       23795 : RgM_isscalar(GEN x, GEN s)
     850             : {
     851       23795 :   long i, j, lx = lg(x);
     852             : 
     853       23795 :   if (lx == 1) return 1;
     854       23795 :   if (lx != lgcols(x)) return 0;
     855       23795 :   if (!s) s = gcoeff(x,1,1);
     856             : 
     857       63956 :   for (j=1; j<lx; j++)
     858             :   {
     859       51867 :     GEN c = gel(x,j);
     860      142892 :     for (i=1; i<j; )
     861       49177 :       if (!gequal0(gel(c,i++))) return 0;
     862             :     /* i = j */
     863       41848 :       if (!gequal(gel(c,i++),s)) return 0;
     864      131845 :     for (   ; i<lx; )
     865       51523 :       if (!gequal0(gel(c,i++))) return 0;
     866             :   }
     867       12089 :   return 1;
     868             : }
     869             : 
     870             : int
     871       12824 : RgM_isidentity(GEN x)
     872             : {
     873       12824 :   long i,j, lx = lg(x);
     874             : 
     875       12824 :   if (lx == 1) return 1;
     876       12824 :   if (lx != lgcols(x)) return 0;
     877       18004 :   for (j=1; j<lx; j++)
     878             :   {
     879       16737 :     GEN c = gel(x,j);
     880       34517 :     for (i=1; i<j; )
     881        4032 :       if (!gequal0(gel(c,i++))) return 0;
     882             :     /* i = j */
     883       13748 :       if (!gequal1(gel(c,i++))) return 0;
     884       14966 :     for (   ; i<lx; )
     885        4606 :       if (!gequal0(gel(c,i++))) return 0;
     886             :   }
     887        1267 :   return 1;
     888             : }
     889             : 
     890             : long
     891           0 : RgC_is_ei(GEN x)
     892             : {
     893           0 :   long i, j = 0, l = lg(x);
     894           0 :   for (i = 1; i < l; i++)
     895             :   {
     896           0 :     GEN c = gel(x,i);
     897           0 :     if (gequal0(c)) continue;
     898           0 :     if (!gequal1(c) || j) return 0;
     899           0 :     j = i;
     900             :   }
     901           0 :   return j;
     902             : }
     903             : 
     904             : int
     905          28 : RgM_isdiagonal(GEN x)
     906             : {
     907          28 :   long i,j, lx = lg(x);
     908          28 :   if (lx == 1) return 1;
     909          28 :   if (lx != lgcols(x)) return 0;
     910             : 
     911          77 :   for (j=1; j<lx; j++)
     912             :   {
     913          56 :     GEN c = gel(x,j);
     914          91 :     for (i=1; i<j; i++)
     915          35 :       if (!gequal0(gel(c,i))) return 0;
     916          91 :     for (i++; i<lx; i++)
     917          42 :       if (!gequal0(gel(c,i))) return 0;
     918             :   }
     919          21 :   return 1;
     920             : }
     921             : int
     922           7 : isdiagonal(GEN x)
     923             : {
     924           7 :   return (typ(x)==t_MAT) && RgM_isdiagonal(x);
     925             : }
     926             : 
     927             : /* returns the first index i<=n such that x=v[i] if it exists, 0 otherwise */
     928             : long
     929       21735 : RgV_isin(GEN v, GEN x)
     930             : {
     931       21735 :   long i, l = lg(v);
     932      405699 :   for (i = 1; i < l; i++)
     933      405510 :     if (gequal(gel(v,i), x)) return i;
     934         189 :   return 0;
     935             : }
     936             : 
     937             : GEN
     938       25662 : RgM_det_triangular(GEN mat)
     939             : {
     940       25662 :   long i,l = lg(mat);
     941             :   pari_sp av;
     942             :   GEN s;
     943             : 
     944       25662 :   if (l<3) return l<2? gen_1: gcopy(gcoeff(mat,1,1));
     945       24073 :   av = avma; s = gcoeff(mat,1,1);
     946       24073 :   for (i=2; i<l; i++) s = gmul(s,gcoeff(mat,i,i));
     947       24073 :   return av==avma? gcopy(s): gerepileupto(av,s);
     948             : }
     949             : 
     950             : GEN
     951        3521 : RgV_kill0(GEN v)
     952             : {
     953             :   long i, l;
     954        3521 :   GEN w = cgetg_copy(v, &l);
     955     1371559 :   for (i = 1; i < l; i++)
     956             :   {
     957     1368038 :     GEN a = gel(v,i);
     958     1368038 :     gel(w,i) = gequal0(a) ? NULL: a;
     959             :   }
     960        3521 :   return w;
     961             : }

Generated by: LCOV version 1.11