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 20459-9710128) Lines: 465 538 86.4 %
Date: 2017-04-28 05:33:48 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     3163035 : RgM_is_ZM(GEN x)
      19             : {
      20     3163035 :   long i, j, h, l = lg(x);
      21     3163035 :   if (l == 1) return 1;
      22     3162801 :   h = lgcols(x);
      23     3162801 :   if (h == 1) return 1;
      24    10014483 :   for (j = l-1; j > 0; j--)
      25    44412483 :     for (i = h-1; i > 0; i--)
      26    37560285 :       if (typ(gcoeff(x,i,j)) != t_INT) return 0;
      27     2789850 :   return 1;
      28             : }
      29             : 
      30             : int
      31          96 : RgM_is_QM(GEN x)
      32             : {
      33          96 :   long i, j, h, l = lg(x);
      34          96 :   if (l == 1) return 1;
      35          96 :   h = lgcols(x);
      36          96 :   if (h == 1) return 1;
      37         816 :   for (j = l-1; j > 0; j--)
      38       10344 :     for (i = h-1; i > 0; i--)
      39        9624 :       if (!is_rational_t(typ(gcoeff(x,i,j)))) return 0;
      40          84 :   return 1;
      41             : }
      42             : 
      43             : int
      44          18 : RgV_is_ZMV(GEN V)
      45             : {
      46          18 :   long i, l = lg(V);
      47         198 :   for (i=1; i<l; i++)
      48         180 :     if (typ(gel(V,i))!=t_MAT || !RgM_is_ZM(gel(V,i)))
      49           0 :       return 0;
      50          18 :   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      357348 : RgMrow_zc_mul_i(GEN x, GEN y, long c, long i)
      63             : {
      64      357348 :   pari_sp av = avma;
      65      357348 :   GEN s = NULL;
      66             :   long j;
      67    13922286 :   for (j=1; j<c; j++)
      68             :   {
      69    13564938 :     long t = y[j];
      70    13564938 :     if (!t) continue;
      71     1529052 :     if (!s) { s = gmulgs(gcoeff(x,i,j),t); continue; }
      72     1174152 :     switch(t)
      73             :     {
      74      544662 :       case  1: s = gadd(s, gcoeff(x,i,j)); break;
      75      204750 :       case -1: s = gsub(s, gcoeff(x,i,j)); break;
      76      424740 :       default: s = gadd(s, gmulgs(gcoeff(x,i,j), t)); break;
      77             :     }
      78             :   }
      79      357348 :   if (!s) { avma = av; return gen_0; }
      80      354900 :   return gerepileupto(av, s);
      81             : }
      82             : GEN
      83       59946 : 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       89772 : RgM_zc_mul_i(GEN x, GEN y, long c, long l)
      87             : {
      88       89772 :   GEN z = cgetg(l,t_COL);
      89             :   long i;
      90       89772 :   for (i = 1; i < l; i++) gel(z,i) = RgMrow_zc_mul_i(x,y,c,i);
      91       89772 :   return z;
      92             : }
      93             : GEN
      94       61380 : 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        7098 : RgM_zm_mul(GEN x, GEN y)
      98             : {
      99        7098 :   long j, c, l = lg(x), ly = lg(y);
     100        7098 :   GEN z = cgetg(ly, t_MAT);
     101        7098 :   if (l == 1) return z;
     102        7098 :   c = lgcols(x);
     103        7098 :   for (j = 1; j < ly; j++) gel(z,j) = RgM_zc_mul_i(x, gel(y,j), l,c);
     104        7098 :   return z;
     105             : }
     106             : 
     107             : static GEN
     108       31512 : RgV_zc_mul_i(GEN x, GEN y, long l)
     109             : {
     110             :   long i;
     111       31512 :   GEN z = gen_0;
     112       31512 :   pari_sp av = avma;
     113       31512 :   for (i = 1; i < l; i++) z = gadd(z, gmulgs(gel(x,i), y[i]));
     114       31512 :   return gerepileupto(av, z);
     115             : }
     116             : GEN
     117          24 : RgV_zc_mul(GEN x, GEN y) { return RgV_zc_mul_i(x, y, lg(x)); }
     118             : 
     119             : GEN
     120        7927 : RgV_zm_mul(GEN x, GEN y)
     121             : {
     122        7927 :   long j, l = lg(x), ly = lg(y);
     123        7927 :   GEN z = cgetg(ly, t_VEC);
     124        7927 :   for (j = 1; j < ly; j++) gel(z,j) = RgV_zc_mul_i(x, gel(y,j), l);
     125        7927 :   return z;
     126             : }
     127             : 
     128             : /* scalar product x.x */
     129             : GEN
     130        1026 : RgV_dotsquare(GEN x)
     131             : {
     132        1026 :   long i, lx = lg(x);
     133        1026 :   pari_sp av = avma;
     134             :   GEN z;
     135        1026 :   if (lx == 1) return gen_0;
     136        1026 :   z = gsqr(gel(x,1));
     137        4092 :   for (i=2; i<lx; i++)
     138             :   {
     139        3066 :     z = gadd(z, gsqr(gel(x,i)));
     140        3066 :     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        1026 :   return gerepileupto(av,z);
     147             : }
     148             : 
     149             : /* scalar product x.y, lx = lg(x) = lg(y) */
     150             : static GEN
     151     1273943 : RgV_dotproduct_i(GEN x, GEN y, long lx)
     152             : {
     153     1273943 :   pari_sp av = avma;
     154             :   long i;
     155             :   GEN z;
     156     1273943 :   if (lx == 1) return gen_0;
     157     1273481 :   z = gmul(gel(x,1),gel(y,1));
     158    38233903 :   for (i=2; i<lx; i++)
     159             :   {
     160    36960422 :     z = gadd(z, gmul(gel(x,i), gel(y,i)));
     161    36960422 :     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     1273481 :   return gerepileupto(av,z);
     168             : }
     169             : GEN
     170      123648 : RgV_dotproduct(GEN x,GEN y)
     171             : {
     172      123648 :   if (x == y) return RgV_dotsquare(x);
     173      123648 :   return RgV_dotproduct_i(x, y, lg(x));
     174             : }
     175             : /* v[1] + ... + v[lg(v)-1] */
     176             : GEN
     177      218124 : RgV_sum(GEN v)
     178             : {
     179             :   GEN p;
     180      218124 :   long i, l = lg(v);
     181      218124 :   if (l == 1) return gen_0;
     182      218124 :   p = gel(v,1); for (i=2; i<l; i++) p = gadd(p, gel(v,i));
     183      218124 :   return p;
     184             : }
     185             : /* v[1] + ... + v[n]. Assume lg(v) > n. */
     186             : GEN
     187         444 : RgV_sumpart(GEN v, long n)
     188             : {
     189             :   GEN p;
     190             :   long i;
     191         444 :   if (!n) return gen_0;
     192         444 :   p = gel(v,1); for (i=2; i<=n; i++) p = gadd(p, gel(v,i));
     193         444 :   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         312 : RgM_sumcol(GEN A)
     207             : {
     208         312 :   long i,j,m,l = lg(A);
     209             :   GEN v;
     210             : 
     211         312 :   if (l == 1) return cgetg(1,t_MAT);
     212         312 :   if (l == 2) return gcopy(gel(A,1));
     213         180 :   m = lgcols(A);
     214         180 :   v = cgetg(m, t_COL);
     215         588 :   for (i = 1; i < m; i++)
     216             :   {
     217         408 :     pari_sp av = avma;
     218         408 :     GEN s = gcoeff(A,i,1);
     219         408 :     for (j = 2; j < l; j++) s = gadd(s, gcoeff(A,i,j));
     220         408 :     gel(v, i) = gerepileupto(av, s);
     221             :   }
     222         180 :   return v;
     223             : }
     224             : 
     225             : static GEN
     226      605724 : _gmul(void *data, GEN x, GEN y)
     227      605724 : { (void)data; return gmul(x,y); }
     228             : 
     229             : GEN
     230       29904 : RgV_prod(GEN x)
     231             : {
     232       29904 :   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        4260 : RgM_Rg_add(GEN x, GEN y)
     239             : {
     240        4260 :   long l = lg(x), i, j;
     241        4260 :   GEN z = cgetg(l,t_MAT);
     242             : 
     243        4260 :   if (l==1) return z;
     244        4260 :   if (l != lgcols(x)) pari_err_OP( "+", x, y);
     245        4260 :   z = cgetg(l,t_MAT);
     246       34818 :   for (i=1; i<l; i++)
     247             :   {
     248       30558 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     249       30558 :     gel(z,i) = zi;
     250     1651848 :     for (j=1; j<l; j++)
     251     1621290 :       gel(zi,j) = i==j? gadd(y,gel(xi,j)): gcopy(gel(xi,j));
     252             :   }
     253        4260 :   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         462 : RgM_Rg_add_shallow(GEN x, GEN y)
     275             : {
     276         462 :   long l = lg(x), i, j;
     277         462 :   GEN z = cgetg(l,t_MAT);
     278             : 
     279         462 :   if (l==1) return z;
     280         372 :   if (l != lgcols(x)) pari_err_OP( "+", x, y);
     281        1536 :   for (i=1; i<l; i++)
     282             :   {
     283        1164 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     284        1164 :     gel(z,i) = zi;
     285        1164 :     for (j=1; j<l; j++) gel(zi,j) = gel(xi,j);
     286        1164 :     gel(zi,i) = gadd(gel(zi,i), y);
     287             :   }
     288         372 :   return z;
     289             : }
     290             : GEN
     291       34428 : RgM_Rg_sub_shallow(GEN x, GEN y)
     292             : {
     293       34428 :   long l = lg(x), i, j;
     294       34428 :   GEN z = cgetg(l,t_MAT);
     295             : 
     296       34428 :   if (l==1) return z;
     297       34428 :   if (l != lgcols(x)) pari_err_OP( "-", x, y);
     298      393198 :   for (i=1; i<l; i++)
     299             :   {
     300      358770 :     GEN zi = cgetg(l,t_COL), xi = gel(x,i);
     301      358770 :     gel(z,i) = zi;
     302      358770 :     for (j=1; j<l; j++) gel(zi,j) = gel(xi,j);
     303      358770 :     gel(zi,i) = gsub(gel(zi,i), y);
     304             :   }
     305       34428 :   return z;
     306             : }
     307             : 
     308             : GEN
     309      431310 : RgC_Rg_add(GEN x, GEN y)
     310             : {
     311      431310 :   long k, lx = lg(x);
     312      431310 :   GEN z = cgetg(lx, t_COL);
     313      431310 :   if (lx == 1)
     314             :   {
     315           6 :     if (isintzero(y)) return z;
     316           0 :     pari_err_TYPE2("+",x,y);
     317             :   }
     318      431304 :   gel(z,1) = gadd(y,gel(x,1));
     319      431304 :   for (k = 2; k < lx; k++) gel(z,k) = gcopy(gel(x,k));
     320      431304 :   return z;
     321             : }
     322             : GEN
     323        3180 : RgC_Rg_sub(GEN x, GEN y)
     324             : {
     325        3180 :   long k, lx = lg(x);
     326        3180 :   GEN z = cgetg(lx, t_COL);
     327        3180 :   if (lx == 1)
     328             :   {
     329           0 :     if (isintzero(y)) return z;
     330           0 :     pari_err_TYPE2("-",x,y);
     331             :   }
     332        3180 :   gel(z,1) = gsub(gel(x,1), y);
     333        3180 :   for (k = 2; k < lx; k++) gel(z,k) = gcopy(gel(x,k));
     334        3180 :   return z;
     335             : }
     336             : /* a - x */
     337             : GEN
     338       11286 : Rg_RgC_sub(GEN a, GEN x)
     339             : {
     340       11286 :   long k, lx = lg(x);
     341       11286 :   GEN z = cgetg(lx,t_COL);
     342       11286 :   if (lx == 1)
     343             :   {
     344           0 :     if (isintzero(a)) return z;
     345           0 :     pari_err_TYPE2("-",a,x);
     346             :   }
     347       11286 :   gel(z,1) = gsub(a, gel(x,1));
     348       11286 :   for (k = 2; k < lx; k++) gel(z,k) = gneg(gel(x,k));
     349       11286 :   return z;
     350             : }
     351             : 
     352             : 
     353             : static GEN
     354     9380862 : RgC_add_i(GEN x, GEN y, long lx)
     355             : {
     356     9380862 :   GEN A = cgetg(lx, t_COL);
     357             :   long i;
     358     9380862 :   for (i=1; i<lx; i++) gel(A,i) = gadd(gel(x,i), gel(y,i));
     359     9380862 :   return A;
     360             : }
     361             : GEN
     362     7351254 : RgC_add(GEN x, GEN y) { return RgC_add_i(x, y, lg(x)); }
     363             : GEN
     364      466908 : RgV_add(GEN x, GEN y)
     365             : {
     366      466908 :   long i, lx = lg(x);
     367      466908 :   GEN A = cgetg(lx, t_VEC);
     368      466908 :   for (i=1; i<lx; i++) gel(A,i) = gadd(gel(x,i), gel(y,i));
     369      466908 :   return A;
     370             : }
     371             : 
     372             : static GEN
     373     1306458 : RgC_sub_i(GEN x, GEN y, long lx)
     374             : {
     375             :   long i;
     376     1306458 :   GEN A = cgetg(lx, t_COL);
     377     1306458 :   for (i=1; i<lx; i++) gel(A,i) = gsub(gel(x,i), gel(y,i));
     378     1306458 :   return A;
     379             : }
     380             : GEN
     381     1277394 : RgC_sub(GEN x, GEN y) { return RgC_sub_i(x, y, lg(x)); }
     382             : GEN
     383       64615 : RgV_sub(GEN x, GEN y)
     384             : {
     385       64615 :   long i, lx = lg(x);
     386       64615 :   GEN A = cgetg(lx, t_VEC);
     387       64615 :   for (i=1; i<lx; i++) gel(A,i) = gsub(gel(x,i), gel(y,i));
     388       64615 :   return A;
     389             : }
     390             : 
     391             : GEN
     392      425838 : RgM_add(GEN x, GEN y)
     393             : {
     394      425838 :   long lx = lg(x), l, j;
     395             :   GEN z;
     396      425838 :   if (lx == 1) return cgetg(1, t_MAT);
     397      425838 :   z = cgetg(lx, t_MAT); l = lgcols(x);
     398      425838 :   for (j = 1; j < lx; j++) gel(z,j) = RgC_add_i(gel(x,j), gel(y,j), l);
     399      425838 :   return z;
     400             : }
     401             : GEN
     402        7182 : RgM_sub(GEN x, GEN y)
     403             : {
     404        7182 :   long lx = lg(x), l, j;
     405             :   GEN z;
     406        7182 :   if (lx == 1) return cgetg(1, t_MAT);
     407        7182 :   z = cgetg(lx, t_MAT); l = lgcols(x);
     408        7182 :   for (j = 1; j < lx; j++) gel(z,j) = RgC_sub_i(gel(x,j), gel(y,j), l);
     409        7182 :   return z;
     410             : }
     411             : 
     412             : static GEN
     413     2283444 : RgC_neg_i(GEN x, long lx)
     414             : {
     415             :   long i;
     416     2283444 :   GEN y = cgetg(lx, t_COL);
     417     2283444 :   for (i=1; i<lx; i++) gel(y,i) = gneg(gel(x,i));
     418     2283444 :   return y;
     419             : }
     420             : GEN
     421       46362 : RgC_neg(GEN x) { return RgC_neg_i(x, lg(x)); }
     422             : GEN
     423        7422 : RgV_neg(GEN x)
     424             : {
     425        7422 :   long i, lx = lg(x);
     426        7422 :   GEN y = cgetg(lx, t_VEC);
     427        7422 :   for (i=1; i<lx; i++) gel(y,i) = gneg(gel(x,i));
     428        7422 :   return y;
     429             : }
     430             : GEN
     431      402624 : RgM_neg(GEN x)
     432             : {
     433      402624 :   long i, hx, lx = lg(x);
     434      402624 :   GEN y = cgetg(lx, t_MAT);
     435      402624 :   if (lx == 1) return y;
     436      402618 :   hx = lgcols(x);
     437      402618 :   for (i=1; i<lx; i++) gel(y,i) = RgC_neg_i(gel(x,i), hx);
     438      402618 :   return y;
     439             : }
     440             : 
     441             : GEN
     442      100398 : RgV_RgC_mul(GEN x, GEN y)
     443             : {
     444      100398 :   long lx = lg(x);
     445      100398 :   if (lx != lg(y)) pari_err_OP("operation 'RgV_RgC_mul'", x, y);
     446      100344 :   return RgV_dotproduct_i(x, y, lx);
     447             : }
     448             : GEN
     449           6 : RgC_RgV_mul(GEN x, GEN y)
     450             : {
     451           6 :   long i, ly = lg(y);
     452           6 :   GEN z = cgetg(ly,t_MAT);
     453           6 :   for (i=1; i<ly; i++) gel(z,i) = RgC_Rg_mul(x, gel(y,i));
     454           6 :   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    64592472 : RgMrow_RgC_mul_i(GEN x, GEN y, long i, long l)
     475             : {
     476    64592472 :   pari_sp av = avma;
     477    64592472 :   GEN t = gmul(gcoeff(x,i,1), gel(y,1)); /* l > 1 ! */
     478             :   long j;
     479    64592472 :   for (j=2; j<l; j++) t = gadd(t, gmul(gcoeff(x,i,j), gel(y,j)));
     480    64592472 :   return gerepileupto(av,t);
     481             : }
     482             : GEN
     483         390 : RgMrow_RgC_mul(GEN x, GEN y, long i)
     484         390 : { 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     8946034 : RgM_RgC_mul_i(GEN x, GEN y, long lx, long l)
     489             : {
     490     8946034 :   GEN z = cgetg(l,t_COL);
     491             :   long i;
     492     8946034 :   for (i=1; i<l; i++) gel(z,i) = RgMrow_RgC_mul_i(x,y,i,lx);
     493     8946034 :   return z;
     494             : }
     495             : 
     496             : GEN
     497     7497442 : RgM_RgC_mul(GEN x, GEN y)
     498             : {
     499     7497442 :   long lx = lg(x);
     500     7497442 :   GEN ffx = NULL, ffy = NULL;
     501     7497442 :   if (lx != lg(y)) pari_err_OP("operation 'RgM_RgC_mul'", x,y);
     502     7497442 :   if (lx == 1) return cgetg(1,t_COL);
     503     7497442 :   if (RgM_is_FFM(x, &ffx) && RgC_is_FFC(y, &ffy)) {
     504          66 :     if (!FF_samefield(ffx, ffy))
     505           0 :       pari_err_OP("*", ffx, ffy);
     506          66 :     return FFM_FFC_mul(x, y, ffx);
     507             :   }
     508     7497376 :   return RgM_RgC_mul_i(x, y, lx, lgcols(x));
     509             : }
     510             : 
     511             : GEN
     512       85939 : RgV_RgM_mul(GEN x, GEN y)
     513             : {
     514       85939 :   long i, lx, ly = lg(y);
     515             :   GEN z;
     516       85939 :   if (ly == 1) return cgetg(1,t_VEC);
     517       85933 :   lx = lg(x);
     518       85933 :   if (lx != lgcols(y)) pari_err_OP("operation 'RgV_RgM_mul'", x,y);
     519       85927 :   z = cgetg(ly, t_VEC);
     520       85927 :   for (i=1; i<ly; i++) gel(z,i) = RgV_dotproduct_i(x, gel(y,i), lx);
     521       85927 :   return z;
     522             : }
     523             : 
     524             : static int
     525      358671 : is_modular_mul(GEN a, GEN b, GEN *z)
     526             : {
     527      358671 :   GEN p1 = NULL, p2 = NULL, p;
     528             :   ulong pp;
     529      358671 :   if (!RgM_is_FpM(a, &p1) || !p1) return 0;
     530          96 :   if (!RgM_is_FpM(b, &p2) || !p2) return 0;
     531          96 :   p = gcdii(p1, p2);
     532          96 :   a = RgM_Fp_init(a, p, &pp);
     533          96 :   switch(pp)
     534             :   {
     535             :   case 0:
     536          12 :     b = RgM_to_FpM(b,p);
     537          12 :     b = FpM_mul(a,b,p);
     538          12 :     *z = FpM_to_mod(b,p);
     539          12 :     break;
     540             :   case 2:
     541          24 :     b = RgM_to_F2m(b);
     542          24 :     b = F2m_mul(a,b);
     543          24 :     *z = F2m_to_mod(b);
     544          24 :     break;
     545             :   default:
     546          60 :     b = RgM_to_Flm(b,pp);
     547          60 :     b = Flm_mul(a,b,pp);
     548          60 :     *z = Flm_to_mod(b,pp);
     549             :   }
     550          96 :   return 1;
     551             : }
     552             : static int
     553         696 : is_modular_sqr(GEN a, GEN *z)
     554             : {
     555         696 :   GEN p = NULL;
     556             :   ulong pp;
     557         696 :   if (!RgM_is_FpM(a, &p) || !p) return 0;
     558          54 :   a = RgM_Fp_init(a, p, &pp);
     559          54 :   switch(pp)
     560             :   {
     561          12 :     case 0: *z = FpM_to_mod(FpM_mul(a,a, p), p); break;
     562          12 :     case 2: *z = F2m_to_mod(F2m_mul(a,a)); break;
     563          30 :     default:*z = Flm_to_mod(Flm_mul(a,a, pp), pp); break;
     564             :   }
     565          54 :   return 1;
     566             : }
     567             : 
     568             : GEN
     569     1739175 : RgM_mul(GEN x, GEN y)
     570             : {
     571     1739175 :   pari_sp av = avma;
     572     1739175 :   long j, l, lx, ly = lg(y);
     573     1739175 :   GEN z, ffx = NULL, ffy = NULL;
     574     1739175 :   if (ly == 1) return cgetg(1,t_MAT);
     575     1738605 :   lx = lg(x);
     576     1738605 :   if (lx != lgcols(y)) pari_err_OP("operation 'RgM_mul'", x,y);
     577     1738605 :   if (lx == 1) return zeromat(0,ly-1);
     578     1738587 :   if (RgM_is_ZM(x) && RgM_is_ZM(y))
     579     1379916 :     return ZM_mul(x, y);
     580      358671 :   if (is_modular_mul(x,y,&z)) return gerepileupto(av, z);
     581      358575 :   if (RgM_is_FFM(x, &ffx) && RgM_is_FFM(y, &ffy)) {
     582         144 :     if (!FF_samefield(ffx, ffy))
     583           0 :       pari_err_OP("*", ffx, ffy);
     584         144 :     return FFM_mul(x, y, ffx);
     585             :   }
     586      358431 :   z = cgetg(ly, t_MAT);
     587      358431 :   l = lgcols(x);
     588      358431 :   for (j=1; j<ly; j++) gel(z,j) = RgM_RgC_mul_i(x, gel(y,j), lx, l);
     589      358431 :   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         384 : RgM_transmultosym(GEN x, GEN y)
     616             : {
     617         384 :   long i, j, l, ly = lg(y);
     618             :   GEN M;
     619         384 :   if (ly == 1) return cgetg(1,t_MAT);
     620         384 :   if (lg(x) != ly) pari_err_OP("operation 'RgM_transmultosym'", x,y);
     621         384 :   l = lgcols(y);
     622         384 :   if (lgcols(x) != l) pari_err_OP("operation 'RgM_transmultosym'", x,y);
     623         384 :   M = cgetg(ly, t_MAT);
     624        1734 :   for (i=1; i<ly; i++)
     625             :   {
     626        1350 :     GEN xi = gel(x,i), c = cgetg(ly,t_COL);
     627        1350 :     gel(M,i) = c;
     628        3792 :     for (j=1; j<i; j++)
     629        2442 :       gcoeff(M,i,j) = gel(c,j) = RgV_dotproduct_i(xi,gel(y,j),l);
     630        1350 :     gel(c,i) = RgV_dotproduct_i(xi,gel(y,i),l);
     631             :   }
     632         384 :   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         102 : gram_matrix(GEN x)
     656             : {
     657         102 :   long i,j, l, lx = lg(x);
     658             :   GEN M;
     659         102 :   if (!is_matvec_t(typ(x))) pari_err_TYPE("gram",x);
     660         102 :   if (lx == 1) return cgetg(1,t_MAT);
     661          90 :   l = lgcols(x);
     662          90 :   M = cgetg(lx,t_MAT);
     663         252 :   for (i=1; i<lx; i++)
     664             :   {
     665         162 :     GEN xi = gel(x,i), c = cgetg(lx,t_COL);
     666         162 :     gel(M,i) = c;
     667         240 :     for (j=1; j<i; j++)
     668          78 :       gcoeff(M,i,j) = gel(c,j) = RgV_dotproduct_i(xi,gel(x,j),l);
     669         162 :     gel(c,i) = RgV_dotsquare(xi);
     670             :   }
     671          90 :   return M;
     672             : }
     673             : 
     674             : GEN
     675         780 : RgM_sqr(GEN x)
     676             : {
     677         780 :   pari_sp av = avma;
     678         780 :   long j, lx = lg(x);
     679         780 :   GEN z, ffx = NULL;
     680         780 :   if (lx == 1) return cgetg(1, t_MAT);
     681         750 :   if (lx != lgcols(x)) pari_err_OP("operation 'RgM_mul'", x,x);
     682         750 :   if (RgM_is_ZM(x))         return ZM_sqr(x);
     683         696 :   if (is_modular_sqr(x,&z)) return gerepileupto(av, z);
     684         642 :   if (RgM_is_FFM(x, &ffx))  return FFM_mul(x, x, ffx);
     685         618 :   z = cgetg(lx, t_MAT);
     686         618 :   for (j=1; j<lx; j++) gel(z,j) = RgM_RgC_mul_i(x, gel(x,j), lx, lx);
     687         618 :   return z;
     688             : }
     689             : 
     690             : static GEN
     691        1128 : _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        1842 : _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          90 : _RgM_sqr(void *E, GEN x) { (void) E; return RgM_sqr(x); }
     701             : 
     702             : static GEN
     703         192 : _RgM_mul(void *E, GEN x, GEN y) { (void) E; return RgM_mul(x, y); }
     704             : 
     705             : static GEN
     706        1308 : _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         942 : _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         132 : RgM_powers(GEN x, long l)
     720             : {
     721         132 :   long n = lg(x)-1;
     722         132 :   return gen_powers(x,l,1,(void *) &n, &_RgM_sqr, &_RgM_mul, &_RgM_one);
     723             : }
     724             : 
     725             : GEN
     726         396 : RgX_RgMV_eval(GEN Q, GEN x)
     727             : {
     728         396 :   long n = lg(x)>1 ? lg(gel(x,1))-1:0;
     729         396 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&n,&RgM_algebra,&_RgM_cmul);
     730             : }
     731             : 
     732             : GEN
     733         318 : RgX_RgM_eval(GEN Q, GEN x)
     734             : {
     735         318 :   long n = lg(x)-1;
     736         318 :   return gen_bkeval(Q,degpol(Q),x,1,(void*)&n,&RgM_algebra,&_RgM_cmul);
     737             : }
     738             : 
     739             : GEN
     740      979518 : RgC_Rg_div(GEN x, GEN y) {
     741      979518 :   long i, lx = lg(x);
     742      979518 :   GEN z = cgetg(lx, t_COL);
     743      979518 :   for (i=1; i<lx; i++) gel(z,i) = gdiv(gel(x,i),y);
     744      979518 :   return z;
     745             : }
     746             : GEN
     747     3400974 : RgC_Rg_mul(GEN x, GEN y) {
     748     3400974 :   long i, lx = lg(x);
     749     3400974 :   GEN z = cgetg(lx, t_COL);
     750     3400974 :   for (i=1; i<lx; i++) gel(z,i) = gmul(gel(x,i),y);
     751     3400974 :   return z;
     752             : }
     753             : GEN
     754        6318 : RgV_Rg_mul(GEN x, GEN y) {
     755        6318 :   long i, lx = lg(x);
     756        6318 :   GEN z = cgetg(lx, t_VEC);
     757        6318 :   for (i=1; i<lx; i++) gel(z,i) = gmul(gel(x,i),y);
     758        6318 :   return z;
     759             : }
     760             : GEN
     761       89574 : RgM_Rg_div(GEN X, GEN c) {
     762       89574 :   long i, j, h, l = lg(X);
     763       89574 :   GEN A = cgetg(l, t_MAT);
     764       89574 :   if (l == 1) return A;
     765       89532 :   h = lgcols(X);
     766      658038 :   for (j=1; j<l; j++)
     767             :   {
     768      568506 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     769      568506 :     for (i = 1; i < h; i++) gel(a,i) = gdiv(gel(x,i), c);
     770      568506 :     gel(A,j) = a;
     771             :   }
     772       89532 :   return A;
     773             : }
     774             : GEN
     775       64308 : RgM_Rg_mul(GEN X, GEN c) {
     776       64308 :   long i, j, h, l = lg(X);
     777       64308 :   GEN A = cgetg(l, t_MAT);
     778       64308 :   if (l == 1) return A;
     779       64278 :   h = lgcols(X);
     780      292770 :   for (j=1; j<l; j++)
     781             :   {
     782      228492 :     GEN a = cgetg(h, t_COL), x = gel(X, j);
     783      228492 :     for (i = 1; i < h; i++) gel(a,i) = gmul(gel(x,i), c);
     784      228492 :     gel(A,j) = a;
     785             :   }
     786       64278 :   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     2677238 : fill_scalmat(GEN y, GEN t, long n)
     797             : {
     798             :   long i;
     799    11751240 :   for (i = 1; i <= n; i++)
     800             :   {
     801     9074002 :     gel(y,i) = zerocol(n);
     802     9074002 :     gcoeff(y,i,i) = t;
     803             :   }
     804     2677238 : }
     805             : 
     806             : GEN
     807      312492 : scalarmat(GEN x, long n) {
     808      312492 :   GEN y = cgetg(n+1, t_MAT);
     809      312492 :   if (!n) return y;
     810      312492 :   fill_scalmat(y, gcopy(x), n); return y;
     811             : }
     812             : GEN
     813     1107081 : scalarmat_shallow(GEN x, long n) {
     814     1107081 :   GEN y = cgetg(n+1, t_MAT);
     815     1107081 :   fill_scalmat(y, x, n); return y;
     816             : }
     817             : GEN
     818         120 : scalarmat_s(long x, long n) {
     819         120 :   GEN y = cgetg(n+1, t_MAT);
     820         120 :   if (!n) return y;
     821         120 :   fill_scalmat(y, stoi(x), n); return y;
     822             : }
     823             : GEN
     824     1257551 : matid(long n) {
     825             :   GEN y;
     826     1257551 :   if (n < 0) pari_err_DOMAIN("matid", "size", "<", gen_0, stoi(n));
     827     1257545 :   y = cgetg(n+1, t_MAT);
     828     1257545 :   fill_scalmat(y, gen_1, n); return y;
     829             : }
     830             : 
     831             : INLINE GEN
     832      200838 : scalarcol_i(GEN x, long n, long c)
     833             : {
     834             :   long i;
     835      200838 :   GEN y = cgetg(n+1,t_COL);
     836      200838 :   if (!n) return y;
     837      200838 :   gel(y,1) = c? gcopy(x): x;
     838      200838 :   for (i=2; i<=n; i++) gel(y,i) = gen_0;
     839      200838 :   return y;
     840             : }
     841             : 
     842             : GEN
     843       41064 : scalarcol(GEN x, long n) { return scalarcol_i(x,n,1); }
     844             : 
     845             : GEN
     846      159774 : scalarcol_shallow(GEN x, long n) { return scalarcol_i(x,n,0); }
     847             : 
     848             : int
     849        6942 : RgM_isscalar(GEN x, GEN s)
     850             : {
     851        6942 :   long i, j, lx = lg(x);
     852             : 
     853        6942 :   if (lx == 1) return 1;
     854        6942 :   if (lx != lgcols(x)) return 0;
     855        6942 :   if (!s) s = gcoeff(x,1,1);
     856             : 
     857       16770 :   for (j=1; j<lx; j++)
     858             :   {
     859       14328 :     GEN c = gel(x,j);
     860       33798 :     for (i=1; i<j; )
     861        8592 :       if (!gequal0(gel(c,i++))) return 0;
     862             :     /* i = j */
     863       10878 :       if (!gequal(gel(c,i++),s)) return 0;
     864       29514 :     for (   ; i<lx; )
     865        9858 :       if (!gequal0(gel(c,i++))) return 0;
     866             :   }
     867        2442 :   return 1;
     868             : }
     869             : 
     870             : int
     871        1674 : RgM_isidentity(GEN x)
     872             : {
     873        1674 :   long i,j, lx = lg(x);
     874             : 
     875        1674 :   if (lx == 1) return 1;
     876        1674 :   if (lx != lgcols(x)) return 0;
     877        3240 :   for (j=1; j<lx; j++)
     878             :   {
     879        2688 :     GEN c = gel(x,j);
     880        5778 :     for (i=1; i<j; )
     881        1026 :       if (!gequal0(gel(c,i++))) return 0;
     882             :     /* i = j */
     883        2064 :       if (!gequal1(gel(c,i++))) return 0;
     884        4260 :     for (   ; i<lx; )
     885        1128 :       if (!gequal0(gel(c,i++))) return 0;
     886             :   }
     887         552 :   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          24 : RgM_isdiagonal(GEN x)
     906             : {
     907          24 :   long i,j, lx = lg(x);
     908          24 :   if (lx == 1) return 1;
     909          24 :   if (lx != lgcols(x)) return 0;
     910             : 
     911          66 :   for (j=1; j<lx; j++)
     912             :   {
     913          48 :     GEN c = gel(x,j);
     914          78 :     for (i=1; i<j; i++)
     915          30 :       if (!gequal0(gel(c,i))) return 0;
     916          78 :     for (i++; i<lx; i++)
     917          36 :       if (!gequal0(gel(c,i))) return 0;
     918             :   }
     919          18 :   return 1;
     920             : }
     921             : int
     922           6 : isdiagonal(GEN x)
     923             : {
     924           6 :   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       18630 : RgV_isin(GEN v, GEN x)
     930             : {
     931       18630 :   long i, l = lg(v);
     932      347742 :   for (i = 1; i < l; i++)
     933      347580 :     if (gequal(gel(v,i), x)) return i;
     934         162 :   return 0;
     935             : }
     936             : 
     937             : GEN
     938        3288 : RgM_det_triangular(GEN mat)
     939             : {
     940        3288 :   long i,l = lg(mat);
     941             :   pari_sp av;
     942             :   GEN s;
     943             : 
     944        3288 :   if (l<3) return l<2? gen_1: gcopy(gcoeff(mat,1,1));
     945        1956 :   av = avma; s = gcoeff(mat,1,1);
     946        1956 :   for (i=2; i<l; i++) s = gmul(s,gcoeff(mat,i,i));
     947        1956 :   return av==avma? gcopy(s): gerepileupto(av,s);
     948             : }
     949             : 
     950             : GEN
     951        2748 : RgV_kill0(GEN v)
     952             : {
     953             :   long i, l;
     954        2748 :   GEN w = cgetg_copy(v, &l);
     955     1464240 :   for (i = 1; i < l; i++)
     956             :   {
     957     1461492 :     GEN a = gel(v,i);
     958     1461492 :     gel(w,i) = gequal0(a) ? NULL: a;
     959             :   }
     960        2748 :   return w;
     961             : }

Generated by: LCOV version 1.11