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 21343-6216058) Lines: 463 536 86.4 %
Date: 2017-11-19 06:21:17 Functions: 87 96 90.6 %
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      348677 : RgM_is_ZM(GEN x)
      19             : {
      20      348677 :   long i, j, h, l = lg(x);
      21      348677 :   if (l == 1) return 1;
      22      348369 :   h = lgcols(x);
      23      348369 :   if (h == 1) return 1;
      24     1527377 :   for (j = l-1; j > 0; j--)
      25     8522530 :     for (i = h-1; i > 0; i--)
      26     7343263 :       if (typ(gcoeff(x,i,j)) != t_INT) return 0;
      27      318268 :   return 1;
      28             : }
      29             : 
      30             : int
      31        1603 : RgM_is_QM(GEN x)
      32             : {
      33        1603 :   long i, j, h, l = lg(x);
      34        1603 :   if (l == 1) return 1;
      35        1603 :   h = lgcols(x);
      36        1603 :   if (h == 1) return 1;
      37        5425 :   for (j = l-1; j > 0; j--)
      38       21014 :     for (i = h-1; i > 0; i--)
      39       17192 :       if (!is_rational_t(typ(gcoeff(x,i,j)))) return 0;
      40        1589 :   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      416110 : RgMrow_zc_mul_i(GEN x, GEN y, long c, long i)
      63             : {
      64      416110 :   pari_sp av = avma;
      65      416110 :   GEN s = NULL;
      66             :   long j;
      67    15937603 :   for (j=1; j<c; j++)
      68             :   {
      69    15521493 :     long t = y[j];
      70    15521493 :     if (!t) continue;
      71     1766199 :     if (!s) { s = gmulgs(gcoeff(x,i,j),t); continue; }
      72     1353218 :     switch(t)
      73             :     {
      74      630337 :       case  1: s = gadd(s, gcoeff(x,i,j)); break;
      75      232449 :       case -1: s = gsub(s, gcoeff(x,i,j)); break;
      76      490432 :       default: s = gadd(s, gmulgs(gcoeff(x,i,j), t)); break;
      77             :     }
      78             :   }
      79      416110 :   if (!s) { avma = av; return gen_0; }
      80      412981 :   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      107317 : RgM_zc_mul_i(GEN x, GEN y, long c, long l)
      87             : {
      88      107317 :   GEN z = cgetg(l,t_COL);
      89             :   long i;
      90      107317 :   for (i = 1; i < l; i++) gel(z,i) = RgMrow_zc_mul_i(x,y,c,i);
      91      107317 :   return z;
      92             : }
      93             : GEN
      94       71631 : 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        9768 : RgM_zm_mul(GEN x, GEN y)
      98             : {
      99        9768 :   long j, c, l = lg(x), ly = lg(y);
     100        9768 :   GEN z = cgetg(ly, t_MAT);
     101        9768 :   if (l == 1) return z;
     102        9768 :   c = lgcols(x);
     103        9768 :   for (j = 1; j < ly; j++) gel(z,j) = RgM_zc_mul_i(x, gel(y,j), l,c);
     104        9768 :   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     1090531 : RgV_dotproduct_i(GEN x, GEN y, long lx)
     152             : {
     153     1090531 :   pari_sp av = avma;
     154             :   long i;
     155             :   GEN z;
     156     1090531 :   if (lx == 1) return gen_0;
     157     1089992 :   z = gmul(gel(x,1),gel(y,1));
     158    40549022 :   for (i=2; i<lx; i++)
     159             :   {
     160    39459030 :     z = gadd(z, gmul(gel(x,i), gel(y,i)));
     161    39459030 :     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     1089992 :   return gerepileupto(av,z);
     168             : }
     169             : GEN
     170      125151 : RgV_dotproduct(GEN x,GEN y)
     171             : {
     172      125151 :   if (x == y) return RgV_dotsquare(x);
     173      125151 :   return RgV_dotproduct_i(x, y, lg(x));
     174             : }
     175             : /* v[1] + ... + v[lg(v)-1] */
     176             : GEN
     177      315318 : RgV_sum(GEN v)
     178             : {
     179             :   GEN p;
     180      315318 :   long i, l = lg(v);
     181      315318 :   if (l == 1) return gen_0;
     182      315318 :   p = gel(v,1); for (i=2; i<l; i++) p = gadd(p, gel(v,i));
     183      315318 :   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      727155 : _gmul(void *data, GEN x, GEN y)
     227      727155 : { (void)data; return gmul(x,y); }
     228             : 
     229             : GEN
     230       39553 : RgV_prod(GEN x)
     231             : {
     232       39553 :   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        5298 : RgM_Rg_add(GEN x, GEN y)
     239             : {
     240        5298 :   long l = lg(x), i, j;
     241        5298 :   GEN z = cgetg(l,t_MAT);
     242             : 
     243        5298 :   if (l==1) return z;
     244        5298 :   if (l != lgcols(x)) pari_err_OP( "+", x, y);
     245        5298 :   z = cgetg(l,t_MAT);
     246       40772 :   for (i=1; i<l; i++)
     247             :   {
     248       35474 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     249       35474 :     gel(z,i) = zi;
     250     1887222 :     for (j=1; j<l; j++)
     251     1851748 :       gel(zi,j) = i==j? gadd(y,gel(xi,j)): gcopy(gel(xi,j));
     252             :   }
     253        5298 :   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       50193 : RgM_Rg_sub_shallow(GEN x, GEN y)
     292             : {
     293       50193 :   long l = lg(x), i, j;
     294       50193 :   GEN z = cgetg(l,t_MAT);
     295             : 
     296       50193 :   if (l==1) return z;
     297       50193 :   if (l != lgcols(x)) pari_err_OP( "-", x, y);
     298      581124 :   for (i=1; i<l; i++)
     299             :   {
     300      530931 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     301      530931 :     gel(z,i) = zi;
     302      530931 :     for (j=1; j<l; j++) gel(zi,j) = gel(xi,j);
     303      530931 :     gel(zi,i) = gsub(gel(zi,i), y);
     304             :   }
     305       50193 :   return z;
     306             : }
     307             : 
     308             : GEN
     309     3168932 : RgC_Rg_add(GEN x, GEN y)
     310             : {
     311     3168932 :   long k, lx = lg(x);
     312     3168932 :   GEN z = cgetg(lx, t_COL);
     313     3168932 :   if (lx == 1)
     314             :   {
     315           7 :     if (isintzero(y)) return z;
     316           0 :     pari_err_TYPE2("+",x,y);
     317             :   }
     318     3168925 :   gel(z,1) = gadd(y,gel(x,1));
     319     3168925 :   for (k = 2; k < lx; k++) gel(z,k) = gcopy(gel(x,k));
     320     3168925 :   return z;
     321             : }
     322             : GEN
     323       21105 : RgC_Rg_sub(GEN x, GEN y)
     324             : {
     325       21105 :   long k, lx = lg(x);
     326       21105 :   GEN z = cgetg(lx, t_COL);
     327       21105 :   if (lx == 1)
     328             :   {
     329           0 :     if (isintzero(y)) return z;
     330           0 :     pari_err_TYPE2("-",x,y);
     331             :   }
     332       21105 :   gel(z,1) = gsub(gel(x,1), y);
     333       21105 :   for (k = 2; k < lx; k++) gel(z,k) = gcopy(gel(x,k));
     334       21105 :   return z;
     335             : }
     336             : /* a - x */
     337             : GEN
     338      101579 : Rg_RgC_sub(GEN a, GEN x)
     339             : {
     340      101579 :   long k, lx = lg(x);
     341      101579 :   GEN z = cgetg(lx,t_COL);
     342      101579 :   if (lx == 1)
     343             :   {
     344           0 :     if (isintzero(a)) return z;
     345           0 :     pari_err_TYPE2("-",a,x);
     346             :   }
     347      101579 :   gel(z,1) = gsub(a, gel(x,1));
     348      101579 :   for (k = 2; k < lx; k++) gel(z,k) = gneg(gel(x,k));
     349      101579 :   return z;
     350             : }
     351             : 
     352             : 
     353             : static GEN
     354    11938370 : RgC_add_i(GEN x, GEN y, long lx)
     355             : {
     356    11938370 :   GEN A = cgetg(lx, t_COL);
     357             :   long i;
     358    11938370 :   for (i=1; i<lx; i++) gel(A,i) = gadd(gel(x,i), gel(y,i));
     359    11938370 :   return A;
     360             : }
     361             : GEN
     362     9559952 : RgC_add(GEN x, GEN y) { return RgC_add_i(x, y, lg(x)); }
     363             : GEN
     364      603777 : RgV_add(GEN x, GEN y)
     365      603777 : { pari_APPLY_type(t_VEC, gadd(gel(x,i), gel(y,i))) }
     366             : 
     367             : static GEN
     368     1761247 : RgC_sub_i(GEN x, GEN y, long lx)
     369             : {
     370             :   long i;
     371     1761247 :   GEN A = cgetg(lx, t_COL);
     372     1761247 :   for (i=1; i<lx; i++) gel(A,i) = gsub(gel(x,i), gel(y,i));
     373     1761247 :   return A;
     374             : }
     375             : GEN
     376     1721935 : RgC_sub(GEN x, GEN y) { return RgC_sub_i(x, y, lg(x)); }
     377             : GEN
     378       74326 : RgV_sub(GEN x, GEN y)
     379       74326 : { pari_APPLY_type(t_VEC, gsub(gel(x,i), gel(y,i))) }
     380             : 
     381             : GEN
     382      499443 : RgM_add(GEN x, GEN y)
     383             : {
     384      499443 :   long lx = lg(x), l, j;
     385             :   GEN z;
     386      499443 :   if (lx == 1) return cgetg(1, t_MAT);
     387      499443 :   z = cgetg(lx, t_MAT); l = lgcols(x);
     388      499443 :   for (j = 1; j < lx; j++) gel(z,j) = RgC_add_i(gel(x,j), gel(y,j), l);
     389      499443 :   return z;
     390             : }
     391             : GEN
     392       10167 : RgM_sub(GEN x, GEN y)
     393             : {
     394       10167 :   long lx = lg(x), l, j;
     395             :   GEN z;
     396       10167 :   if (lx == 1) return cgetg(1, t_MAT);
     397       10167 :   z = cgetg(lx, t_MAT); l = lgcols(x);
     398       10167 :   for (j = 1; j < lx; j++) gel(z,j) = RgC_sub_i(gel(x,j), gel(y,j), l);
     399       10167 :   return z;
     400             : }
     401             : 
     402             : static GEN
     403     2734822 : RgC_neg_i(GEN x, long lx)
     404             : {
     405             :   long i;
     406     2734822 :   GEN y = cgetg(lx, t_COL);
     407     2734822 :   for (i=1; i<lx; i++) gel(y,i) = gneg(gel(x,i));
     408     2734822 :   return y;
     409             : }
     410             : GEN
     411      123885 : RgC_neg(GEN x) { return RgC_neg_i(x, lg(x)); }
     412             : GEN
     413       24277 : RgV_neg(GEN x)
     414       24277 : { pari_APPLY_type(t_VEC, gneg(gel(x,i))) }
     415             : GEN
     416      469812 : RgM_neg(GEN x)
     417             : {
     418      469812 :   long i, hx, lx = lg(x);
     419      469812 :   GEN y = cgetg(lx, t_MAT);
     420      469812 :   if (lx == 1) return y;
     421      469805 :   hx = lgcols(x);
     422      469805 :   for (i=1; i<lx; i++) gel(y,i) = RgC_neg_i(gel(x,i), hx);
     423      469805 :   return y;
     424             : }
     425             : 
     426             : GEN
     427      141495 : RgV_RgC_mul(GEN x, GEN y)
     428             : {
     429      141495 :   long lx = lg(x);
     430      141495 :   if (lx != lg(y)) pari_err_OP("operation 'RgV_RgC_mul'", x, y);
     431      141432 :   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    69635022 : RgMrow_RgC_mul_i(GEN x, GEN y, long i, long l)
     460             : {
     461    69635022 :   pari_sp av = avma;
     462    69635022 :   GEN t = gmul(gcoeff(x,i,1), gel(y,1)); /* l > 1 ! */
     463             :   long j;
     464    69635009 :   for (j=2; j<l; j++) t = gadd(t, gmul(gcoeff(x,i,j), gel(y,j)));
     465    69635019 :   return gerepileupto(av,t);
     466             : }
     467             : GEN
     468        3409 : RgMrow_RgC_mul(GEN x, GEN y, long i)
     469        3409 : { return RgMrow_RgC_mul_i(x, y, i, lg(x)); }
     470             : 
     471             : /* compatible t_MAT * t_COL, lx = lg(x) = lg(y) > 1, l = lgcols(x) */
     472             : static GEN
     473    10358890 : RgM_RgC_mul_i(GEN x, GEN y, long lx, long l)
     474             : {
     475    10358890 :   GEN z = cgetg(l,t_COL);
     476             :   long i;
     477    10358889 :   for (i=1; i<l; i++) gel(z,i) = RgMrow_RgC_mul_i(x,y,i,lx);
     478    10358887 :   return z;
     479             : }
     480             : 
     481             : GEN
     482     8998749 : RgM_RgC_mul(GEN x, GEN y)
     483             : {
     484     8998749 :   long lx = lg(x);
     485     8998749 :   GEN ffx = NULL, ffy = NULL;
     486     8998749 :   if (lx != lg(y)) pari_err_OP("operation 'RgM_RgC_mul'", x,y);
     487     8998749 :   if (lx == 1) return cgetg(1,t_COL);
     488     8998749 :   if (RgM_is_FFM(x, &ffx) && RgC_is_FFC(y, &ffy)) {
     489          77 :     if (!FF_samefield(ffx, ffy))
     490           0 :       pari_err_OP("*", ffx, ffy);
     491          77 :     return FFM_FFC_mul(x, y, ffx);
     492             :   }
     493     8998671 :   return RgM_RgC_mul_i(x, y, lx, lgcols(x));
     494             : }
     495             : 
     496             : GEN
     497       56513 : RgV_RgM_mul(GEN x, GEN y)
     498             : {
     499       56513 :   long i, lx, ly = lg(y);
     500             :   GEN z;
     501       56513 :   if (ly == 1) return cgetg(1,t_VEC);
     502       56506 :   lx = lg(x);
     503       56506 :   if (lx != lgcols(y)) pari_err_OP("operation 'RgV_RgM_mul'", x,y);
     504       56499 :   z = cgetg(ly, t_VEC);
     505       56499 :   for (i=1; i<ly; i++) gel(z,i) = RgV_dotproduct_i(x, gel(y,i), lx);
     506       56499 :   return z;
     507             : }
     508             : 
     509             : static GEN
     510       56238 : RgM_mul_FpM(GEN x, GEN y, GEN p)
     511             : {
     512       56238 :   pari_sp av = avma;
     513             :   GEN r;
     514       56238 :   if (lgefint(p) == 3)
     515             :   {
     516       56166 :     ulong pp = uel(p, 2);
     517       56166 :     r = Flm_to_ZM_inplace(Flm_mul(RgM_to_Flm(x, pp),
     518             :                                   RgM_to_Flm(y, pp), pp));
     519             :   }
     520             :   else
     521          72 :     r = FpM_mul(RgM_to_FpM(x, p), RgM_to_FpM(y, p), p);
     522       56238 :   return gerepileupto(av, FpM_to_mod(r, p));
     523             : }
     524             : 
     525             : static GEN
     526          84 : RgM_mul_FqM(GEN x, GEN y, GEN pol, GEN p)
     527             : {
     528          84 :   pari_sp av = avma;
     529          84 :   GEN T = RgX_to_FpX(pol, p);
     530          84 :   GEN b = FqM_mul(RgM_to_FqM(x, T, p), RgM_to_FqM(y, T, p), T, p);
     531          84 :   return gerepileupto(av, FqM_to_mod(b, T, p));
     532             : }
     533             : 
     534             : static GEN
     535       11788 : RgM_liftred(GEN x, GEN T)
     536       11788 : { return RgXQM_red(liftpol_shallow(x), T); }
     537             : 
     538             : static GEN
     539        2030 : RgM_mul_ZXQM(GEN x, GEN y, GEN T)
     540             : {
     541        2030 :   pari_sp av = avma;
     542        2030 :   GEN b = ZXQM_mul(RgM_liftred(x,T), RgM_liftred(y, T), T);
     543        2030 :   return gerepilecopy(av, QXQM_to_mod_shallow(b,T));
     544             : }
     545             : 
     546             : static GEN
     547         133 : RgM_sqr_ZXQM(GEN x, GEN T)
     548             : {
     549         133 :   pari_sp av = avma;
     550         133 :   GEN b = ZXQM_sqr(RgM_liftred(x, T), T);
     551         133 :   return gerepilecopy(av, QXQM_to_mod_shallow(b,T));
     552             : }
     553             : 
     554             : static GEN
     555        3794 : RgM_mul_QXQM(GEN x, GEN y, GEN T)
     556             : {
     557        3794 :   pari_sp av = avma;
     558        3794 :   GEN b = QXQM_mul(RgM_liftred(x, T), RgM_liftred(y, T), T);
     559        3794 :   return gerepilecopy(av, QXQM_to_mod_shallow(b,T));
     560             : }
     561             : 
     562             : static GEN
     563           7 : RgM_sqr_QXQM(GEN x, GEN T)
     564             : {
     565           7 :   pari_sp av = avma;
     566           7 :   GEN b = QXQM_sqr(RgM_liftred(x, T), T);
     567           7 :   return gerepilecopy(av, QXQM_to_mod_shallow(b,T));
     568             : }
     569             : 
     570             : INLINE int
     571        3843 : RgX_is_monic_ZX(GEN pol)
     572        3843 : { return RgX_is_ZX(pol) && ZX_is_monic(pol); }
     573             : 
     574             : #define code(t1,t2) ((t1 << 6) | t2)
     575             : static GEN
     576     2308053 : RgM_mul_fast(GEN x, GEN y)
     577             : {
     578             :   GEN p, pol;
     579             :   long pa;
     580     2308053 :   long t = RgM_type2(x,y, &p,&pol,&pa);
     581     2308053 :   switch(t)
     582             :   {
     583     1680234 :     case t_INT:    return ZM_mul(x,y);
     584      167835 :     case t_FRAC:   return QM_mul(x,y);
     585         217 :     case t_FFELT:  return FFM_mul(x, y, pol);
     586       56175 :     case t_INTMOD: return RgM_mul_FpM(x, y, p);
     587             :     case code(t_POLMOD, t_INT):
     588        2037 :                    return ZX_is_monic(pol)? RgM_mul_ZXQM(x, y, pol): NULL;
     589             :     case code(t_POLMOD, t_FRAC):
     590        3815 :                    return RgX_is_monic_ZX(pol)? RgM_mul_QXQM(x, y, pol): NULL;
     591             :     case code(t_POLMOD, t_INTMOD):
     592          84 :                    return RgM_mul_FqM(x, y, pol, p);
     593      397656 :     default:       return NULL;
     594             :   }
     595             : }
     596             : 
     597             : static GEN
     598        1078 : RgM_sqr_fast(GEN x)
     599             : {
     600             :   GEN p, pol;
     601             :   long pa;
     602        1078 :   long t = RgM_type(x, &p,&pol,&pa);
     603        1078 :   switch(t)
     604             :   {
     605          98 :     case t_INT:    return ZM_sqr(x);
     606         700 :     case t_FRAC:   return QM_mul(x, x);
     607          28 :     case t_FFELT:  return FFM_mul(x, x, pol);
     608          63 :     case t_INTMOD: return RgM_mul_FpM(x, x, p);
     609             :     case code(t_POLMOD, t_INT):
     610         140 :                    return ZX_is_monic(pol)? RgM_sqr_ZXQM(x, pol): NULL;
     611             :     case code(t_POLMOD, t_FRAC):
     612          28 :                    return RgX_is_monic_ZX(pol)? RgM_sqr_QXQM(x, pol): NULL;
     613             :     case code(t_POLMOD, t_INTMOD):
     614           0 :                    return RgM_mul_FqM(x, x, pol, p);
     615          21 :     default:       return NULL;
     616             :   }
     617             : }
     618             : 
     619             : #undef code
     620             : 
     621             : GEN
     622     2327737 : RgM_mul(GEN x, GEN y)
     623             : {
     624     2327737 :   long j, l, lx, ly = lg(y);
     625             :   GEN z;
     626     2327737 :   if (ly == 1) return cgetg(1,t_MAT);
     627     2308074 :   lx = lg(x);
     628     2308074 :   if (lx != lgcols(y)) pari_err_OP("operation 'RgM_mul'", x,y);
     629     2308074 :   if (lx == 1) return zeromat(0,ly-1);
     630     2308053 :   z = RgM_mul_fast(x, y);
     631     2308053 :   if (z) return z;
     632      397684 :   z = cgetg(ly, t_MAT);
     633      397684 :   l = lgcols(x);
     634      397684 :   for (j=1; j<ly; j++) gel(z,j) = RgM_RgC_mul_i(x, gel(y,j), lx, l);
     635      397684 :   return z;
     636             : }
     637             : 
     638             : GEN
     639        1113 : RgM_sqr(GEN x)
     640             : {
     641        1113 :   long j, lx = lg(x);
     642             :   GEN z;
     643        1113 :   if (lx == 1) return cgetg(1, t_MAT);
     644        1078 :   if (lx != lgcols(x)) pari_err_OP("operation 'RgM_mul'", x,x);
     645        1078 :   z = RgM_sqr_fast(x);
     646        1078 :   if (z) return z;
     647          49 :   z = cgetg(lx, t_MAT);
     648          49 :   for (j=1; j<lx; j++) gel(z,j) = RgM_RgC_mul_i(x, gel(x,j), lx, lx);
     649          49 :   return z;
     650             : }
     651             : 
     652             : /* assume result is symmetric */
     653             : GEN
     654           0 : RgM_multosym(GEN x, GEN y)
     655             : {
     656           0 :   long j, lx, ly = lg(y);
     657             :   GEN M;
     658           0 :   if (ly == 1) return cgetg(1,t_MAT);
     659           0 :   lx = lg(x);
     660           0 :   if (lx != lgcols(y)) pari_err_OP("operation 'RgM_multosym'", x,y);
     661           0 :   if (lx == 1) return cgetg(1,t_MAT);
     662           0 :   if (ly != lgcols(x)) pari_err_OP("operation 'RgM_multosym'", x,y);
     663           0 :   M = cgetg(ly, t_MAT);
     664           0 :   for (j=1; j<ly; j++)
     665             :   {
     666           0 :     GEN z = cgetg(ly,t_COL), yj = gel(y,j);
     667             :     long i;
     668           0 :     for (i=1; i<j; i++) gel(z,i) = gcoeff(M,j,i);
     669           0 :     for (i=j; i<ly; i++)gel(z,i) = RgMrow_RgC_mul_i(x,yj,i,lx);
     670           0 :     gel(M,j) = z;
     671             :   }
     672           0 :   return M;
     673             : }
     674             : /* x~ * y, assuming result is symmetric */
     675             : GEN
     676         447 : RgM_transmultosym(GEN x, GEN y)
     677             : {
     678         447 :   long i, j, l, ly = lg(y);
     679             :   GEN M;
     680         447 :   if (ly == 1) return cgetg(1,t_MAT);
     681         447 :   if (lg(x) != ly) pari_err_OP("operation 'RgM_transmultosym'", x,y);
     682         447 :   l = lgcols(y);
     683         447 :   if (lgcols(x) != l) pari_err_OP("operation 'RgM_transmultosym'", x,y);
     684         447 :   M = cgetg(ly, t_MAT);
     685        2019 :   for (i=1; i<ly; i++)
     686             :   {
     687        1572 :     GEN xi = gel(x,i), c = cgetg(ly,t_COL);
     688        1572 :     gel(M,i) = c;
     689        4418 :     for (j=1; j<i; j++)
     690        2846 :       gcoeff(M,i,j) = gel(c,j) = RgV_dotproduct_i(xi,gel(y,j),l);
     691        1572 :     gel(c,i) = RgV_dotproduct_i(xi,gel(y,i),l);
     692             :   }
     693         447 :   return M;
     694             : }
     695             : /* x~ * y */
     696             : GEN
     697           0 : RgM_transmul(GEN x, GEN y)
     698             : {
     699           0 :   long i, j, l, lx, ly = lg(y);
     700             :   GEN M;
     701           0 :   if (ly == 1) return cgetg(1,t_MAT);
     702           0 :   lx = lg(x);
     703           0 :   l = lgcols(y);
     704           0 :   if (lgcols(x) != l) pari_err_OP("operation 'RgM_transmul'", x,y);
     705           0 :   M = cgetg(ly, t_MAT);
     706           0 :   for (i=1; i<ly; i++)
     707             :   {
     708           0 :     GEN yi = gel(y,i), c = cgetg(lx,t_COL);
     709           0 :     gel(M,i) = c;
     710           0 :     for (j=1; j<lx; j++) gel(c,j) = RgV_dotproduct_i(yi,gel(x,j),l);
     711             :   }
     712           0 :   return M;
     713             : }
     714             : 
     715             : GEN
     716         119 : gram_matrix(GEN x)
     717             : {
     718         119 :   long i,j, l, lx = lg(x);
     719             :   GEN M;
     720         119 :   if (!is_matvec_t(typ(x))) pari_err_TYPE("gram",x);
     721         119 :   if (lx == 1) return cgetg(1,t_MAT);
     722         105 :   l = lgcols(x);
     723         105 :   M = cgetg(lx,t_MAT);
     724         294 :   for (i=1; i<lx; i++)
     725             :   {
     726         189 :     GEN xi = gel(x,i), c = cgetg(lx,t_COL);
     727         189 :     gel(M,i) = c;
     728         280 :     for (j=1; j<i; j++)
     729          91 :       gcoeff(M,i,j) = gel(c,j) = RgV_dotproduct_i(xi,gel(x,j),l);
     730         189 :     gel(c,i) = RgV_dotsquare(xi);
     731             :   }
     732         105 :   return M;
     733             : }
     734             : 
     735             : static GEN
     736        2093 : _RgM_add(void *E, GEN x, GEN y) { (void)E; return RgM_add(x, y); }
     737             : 
     738             : static GEN
     739           0 : _RgM_sub(void *E, GEN x, GEN y) { (void)E; return RgM_sub(x, y); }
     740             : 
     741             : static GEN
     742        3549 : _RgM_cmul(void *E, GEN P, long a, GEN x) { (void)E; return RgM_Rg_mul(x,gel(P,a+2)); }
     743             : 
     744             : static GEN
     745         133 : _RgM_sqr(void *E, GEN x) { (void) E; return RgM_sqr(x); }
     746             : 
     747             : static GEN
     748         322 : _RgM_mul(void *E, GEN x, GEN y) { (void) E; return RgM_mul(x, y); }
     749             : 
     750             : static GEN
     751        2870 : _RgM_one(void *E) { long *n = (long*) E; return matid(*n); }
     752             : 
     753             : static GEN
     754           0 : _RgM_zero(void *E) { long *n = (long*) E; return zeromat(*n,*n); }
     755             : 
     756             : static GEN
     757        1911 : _RgM_red(void *E, GEN x) { (void)E; return x; }
     758             : 
     759             : static struct bb_algebra RgM_algebra = { _RgM_red, _RgM_add, _RgM_sub,
     760             :        _RgM_mul, _RgM_sqr, _RgM_one, _RgM_zero };
     761             : 
     762             : /* generates the list of powers of x of degree 0,1,2,...,l*/
     763             : GEN
     764         154 : RgM_powers(GEN x, long l)
     765             : {
     766         154 :   long n = lg(x)-1;
     767         154 :   return gen_powers(x,l,1,(void *) &n, &_RgM_sqr, &_RgM_mul, &_RgM_one);
     768             : }
     769             : 
     770             : GEN
     771         462 : RgX_RgMV_eval(GEN Q, GEN x)
     772             : {
     773         462 :   long n = lg(x)>1 ? lg(gel(x,1))-1:0;
     774         462 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&n,&RgM_algebra,&_RgM_cmul);
     775             : }
     776             : 
     777             : GEN
     778         994 : RgX_RgM_eval(GEN Q, GEN x)
     779             : {
     780         994 :   long n = lg(x)-1;
     781         994 :   return gen_bkeval(Q,degpol(Q),x,1,(void*)&n,&RgM_algebra,&_RgM_cmul);
     782             : }
     783             : 
     784             : GEN
     785     2088761 : RgC_Rg_div(GEN x, GEN y)
     786     2088761 : { pari_APPLY_type(t_COL, gdiv(gel(x,i),y)) }
     787             : 
     788             : GEN
     789     5347068 : RgC_Rg_mul(GEN x, GEN y)
     790     5347068 : { pari_APPLY_type(t_COL, gmul(gel(x,i),y)) }
     791             : 
     792             : GEN
     793       17902 : RgV_Rg_mul(GEN x, GEN y)
     794       17902 : { pari_APPLY_type(t_VEC, gmul(gel(x,i),y)) }
     795             : 
     796             : GEN
     797      138752 : RgM_Rg_div(GEN X, GEN c) {
     798      138752 :   long i, j, h, l = lg(X);
     799      138752 :   GEN A = cgetg(l, t_MAT);
     800      138752 :   if (l == 1) return A;
     801      138703 :   h = lgcols(X);
     802      930966 :   for (j=1; j<l; j++)
     803             :   {
     804      792263 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     805      792263 :     for (i = 1; i < h; i++) gel(a,i) = gdiv(gel(x,i), c);
     806      792263 :     gel(A,j) = a;
     807             :   }
     808      138703 :   return A;
     809             : }
     810             : GEN
     811      163050 : RgM_Rg_mul(GEN X, GEN c) {
     812      163050 :   long i, j, h, l = lg(X);
     813      163050 :   GEN A = cgetg(l, t_MAT);
     814      163050 :   if (l == 1) return A;
     815      163015 :   h = lgcols(X);
     816      833656 :   for (j=1; j<l; j++)
     817             :   {
     818      670641 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     819      670641 :     for (i = 1; i < h; i++) gel(a,i) = gmul(gel(x,i), c);
     820      670641 :     gel(A,j) = a;
     821             :   }
     822      163015 :   return A;
     823             : }
     824             : 
     825             : /********************************************************************/
     826             : /*                                                                  */
     827             : /*                    SCALAR TO MATRIX/VECTOR                       */
     828             : /*                                                                  */
     829             : /********************************************************************/
     830             : /* fill the square nxn matrix equal to t*Id */
     831             : static void
     832     3691519 : fill_scalmat(GEN y, GEN t, long n)
     833             : {
     834             :   long i;
     835    15464846 :   for (i = 1; i <= n; i++)
     836             :   {
     837    11773327 :     gel(y,i) = zerocol(n);
     838    11773327 :     gcoeff(y,i,i) = t;
     839             :   }
     840     3691519 : }
     841             : 
     842             : GEN
     843      436187 : scalarmat(GEN x, long n) {
     844      436187 :   GEN y = cgetg(n+1, t_MAT);
     845      436187 :   if (!n) return y;
     846      436187 :   fill_scalmat(y, gcopy(x), n); return y;
     847             : }
     848             : GEN
     849     1419161 : scalarmat_shallow(GEN x, long n) {
     850     1419161 :   GEN y = cgetg(n+1, t_MAT);
     851     1419161 :   fill_scalmat(y, x, n); return y;
     852             : }
     853             : GEN
     854         140 : scalarmat_s(long x, long n) {
     855         140 :   GEN y = cgetg(n+1, t_MAT);
     856         140 :   if (!n) return y;
     857         140 :   fill_scalmat(y, stoi(x), n); return y;
     858             : }
     859             : GEN
     860     1836038 : matid(long n) {
     861             :   GEN y;
     862     1836038 :   if (n < 0) pari_err_DOMAIN("matid", "size", "<", gen_0, stoi(n));
     863     1836031 :   y = cgetg(n+1, t_MAT);
     864     1836031 :   fill_scalmat(y, gen_1, n); return y;
     865             : }
     866             : 
     867             : INLINE GEN
     868      362383 : scalarcol_i(GEN x, long n, long c)
     869             : {
     870             :   long i;
     871      362383 :   GEN y = cgetg(n+1,t_COL);
     872      362383 :   if (!n) return y;
     873      362383 :   gel(y,1) = c? gcopy(x): x;
     874      362383 :   for (i=2; i<=n; i++) gel(y,i) = gen_0;
     875      362383 :   return y;
     876             : }
     877             : 
     878             : GEN
     879       77925 : scalarcol(GEN x, long n) { return scalarcol_i(x,n,1); }
     880             : 
     881             : GEN
     882      284458 : scalarcol_shallow(GEN x, long n) { return scalarcol_i(x,n,0); }
     883             : 
     884             : int
     885       23563 : RgM_isscalar(GEN x, GEN s)
     886             : {
     887       23563 :   long i, j, lx = lg(x);
     888             : 
     889       23563 :   if (lx == 1) return 1;
     890       23563 :   if (lx != lgcols(x)) return 0;
     891       23563 :   if (!s) s = gcoeff(x,1,1);
     892             : 
     893       61504 :   for (j=1; j<lx; j++)
     894             :   {
     895       49646 :     GEN c = gel(x,j);
     896      118003 :     for (i=1; i<j; )
     897       28701 :       if (!gequal0(gel(c,i++))) return 0;
     898             :     /* i = j */
     899       39656 :       if (!gequal(gel(c,i++),s)) return 0;
     900      106928 :     for (   ; i<lx; )
     901       31046 :       if (!gequal0(gel(c,i++))) return 0;
     902             :   }
     903       11858 :   return 1;
     904             : }
     905             : 
     906             : int
     907       12201 : RgM_isidentity(GEN x)
     908             : {
     909       12201 :   long i,j, lx = lg(x);
     910             : 
     911       12201 :   if (lx == 1) return 1;
     912       12201 :   if (lx != lgcols(x)) return 0;
     913       16527 :   for (j=1; j<lx; j++)
     914             :   {
     915       15883 :     GEN c = gel(x,j);
     916       32473 :     for (i=1; i<j; )
     917        3696 :       if (!gequal0(gel(c,i++))) return 0;
     918             :     /* i = j */
     919       12894 :       if (!gequal1(gel(c,i++))) return 0;
     920       12922 :     for (   ; i<lx; )
     921        4270 :       if (!gequal0(gel(c,i++))) return 0;
     922             :   }
     923         644 :   return 1;
     924             : }
     925             : 
     926             : long
     927           0 : RgC_is_ei(GEN x)
     928             : {
     929           0 :   long i, j = 0, l = lg(x);
     930           0 :   for (i = 1; i < l; i++)
     931             :   {
     932           0 :     GEN c = gel(x,i);
     933           0 :     if (gequal0(c)) continue;
     934           0 :     if (!gequal1(c) || j) return 0;
     935           0 :     j = i;
     936             :   }
     937           0 :   return j;
     938             : }
     939             : 
     940             : int
     941         329 : RgM_isdiagonal(GEN x)
     942             : {
     943         329 :   long i,j, lx = lg(x);
     944         329 :   if (lx == 1) return 1;
     945         329 :   if (lx != lgcols(x)) return 0;
     946             : 
     947        3066 :   for (j=1; j<lx; j++)
     948             :   {
     949        2744 :     GEN c = gel(x,j);
     950       16835 :     for (i=1; i<j; i++)
     951       14091 :       if (!gequal0(gel(c,i))) return 0;
     952       16835 :     for (i++; i<lx; i++)
     953       14098 :       if (!gequal0(gel(c,i))) return 0;
     954             :   }
     955         322 :   return 1;
     956             : }
     957             : int
     958         308 : isdiagonal(GEN x)
     959             : {
     960         308 :   return (typ(x)==t_MAT) && RgM_isdiagonal(x);
     961             : }
     962             : 
     963             : /* returns the first index i<=n such that x=v[i] if it exists, 0 otherwise */
     964             : long
     965       21735 : RgV_isin(GEN v, GEN x)
     966             : {
     967       21735 :   long i, l = lg(v);
     968      405699 :   for (i = 1; i < l; i++)
     969      405510 :     if (gequal(gel(v,i), x)) return i;
     970         189 :   return 0;
     971             : }
     972             : 
     973             : GEN
     974       15169 : RgM_det_triangular(GEN mat)
     975             : {
     976       15169 :   long i,l = lg(mat);
     977             :   pari_sp av;
     978             :   GEN s;
     979             : 
     980       15169 :   if (l<3) return l<2? gen_1: gcopy(gcoeff(mat,1,1));
     981       13587 :   av = avma; s = gcoeff(mat,1,1);
     982       13587 :   for (i=2; i<l; i++) s = gmul(s,gcoeff(mat,i,i));
     983       13587 :   return av==avma? gcopy(s): gerepileupto(av,s);
     984             : }
     985             : 
     986             : GEN
     987        3605 : RgV_kill0(GEN v)
     988             : {
     989             :   long i, l;
     990        3605 :   GEN w = cgetg_copy(v, &l);
     991     1410052 :   for (i = 1; i < l; i++)
     992             :   {
     993     1406447 :     GEN a = gel(v,i);
     994     1406447 :     gel(w,i) = gequal0(a) ? NULL: a;
     995             :   }
     996        3605 :   return w;
     997             : }

Generated by: LCOV version 1.11