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 - perm.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21059-cbe0d6a) Lines: 594 637 93.2 %
Date: 2017-09-22 06:24:58 Functions: 69 72 95.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2003  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             : /*************************************************************************/
      18             : /**                                                                     **/
      19             : /**                   Routines for handling VEC/COL                     **/
      20             : /**                                                                     **/
      21             : /*************************************************************************/
      22             : int
      23        5558 : vec_isconst(GEN v)
      24             : {
      25        5558 :   long i, l = lg(v);
      26             :   GEN w;
      27        5558 :   if (l==1) return 1;
      28        5558 :   w = gel(v,1);
      29       12537 :   for(i=2;i<l;i++)
      30       10654 :     if (!gequal(gel(v,i), w)) return 0;
      31        1883 :   return 1;
      32             : }
      33             : 
      34             : /* Check if all the elements of v are different.
      35             :  * Use a quadratic algorithm. Could be done in n*log(n) by sorting. */
      36             : int
      37        2660 : vec_is1to1(GEN v)
      38             : {
      39        2660 :   long i, j, l = lg(v);
      40       16625 :   for (i=1; i<l; i++)
      41             :   {
      42       14105 :     GEN w = gel(v,i);
      43       91287 :     for(j=i+1; j<l; j++)
      44       77322 :       if (gequal(gel(v,j), w)) return 0;
      45             :   }
      46        2520 :   return 1;
      47             : }
      48             : 
      49             : GEN
      50       75642 : vec_insert(GEN v, long n, GEN x)
      51             : {
      52       75642 :   long i, l=lg(v);
      53       75642 :   GEN V = cgetg(l+1,t_VEC);
      54       75642 :   for(i=1; i<n; i++) gel(V,i) = gel(v,i);
      55       75642 :   gel(V,n) = x;
      56       75642 :   for(i=n+1; i<=l; i++) gel(V,i) = gel(v,i-1);
      57       75642 :   return V;
      58             : }
      59             : /*************************************************************************/
      60             : /**                                                                     **/
      61             : /**                   Routines for handling VECSMALL                    **/
      62             : /**                                                                     **/
      63             : /*************************************************************************/
      64             : /* Sort v[0]...v[n-1] and put result in w[0]...w[n-1].
      65             :  * We accept v==w. w must be allocated. */
      66             : static void
      67   191997103 : vecsmall_sortspec(GEN v, long n, GEN w)
      68             : {
      69   191997103 :   pari_sp ltop=avma;
      70   191997103 :   long nx=n>>1, ny=n-nx;
      71             :   long m, ix, iy;
      72             :   GEN x, y;
      73   191997103 :   if (n<=2)
      74             :   {
      75   108691348 :     if (n==1)
      76    18973176 :       w[0]=v[0];
      77    89718172 :     else if (n==2)
      78             :     {
      79    89718174 :       long v0=v[0], v1=v[1];
      80    89718174 :       if (v0<=v1) { w[0]=v0; w[1]=v1; }
      81     2139112 :       else        { w[0]=v1; w[1]=v0; }
      82             :     }
      83   300688451 :     return;
      84             :   }
      85    83305755 :   x=new_chunk(nx); y=new_chunk(ny);
      86    83305755 :   vecsmall_sortspec(v,nx,x);
      87    83305757 :   vecsmall_sortspec(v+nx,ny,y);
      88   441075110 :   for (m=0, ix=0, iy=0; ix<nx && iy<ny; )
      89   274463600 :     if (x[ix]<=y[iy])
      90   237630987 :       w[m++]=x[ix++];
      91             :     else
      92    36832613 :       w[m++]=y[iy++];
      93    83305755 :   for(;ix<nx;) w[m++]=x[ix++];
      94    83305755 :   for(;iy<ny;) w[m++]=y[iy++];
      95    83305755 :   avma=ltop;
      96             : }
      97             : 
      98             : /*in place sort.*/
      99             : void
     100    30996054 : vecsmall_sort(GEN V)
     101             : {
     102    30996054 :   long l = lg(V)-1;
     103    61992109 :   if (l<=1) return;
     104    25385592 :   vecsmall_sortspec(V+1,l,V+1);
     105             : }
     106             : 
     107             : /* cf gen_sortspec */
     108             : static GEN
     109    20733914 : vecsmall_indexsortspec(GEN v, long n)
     110             : {
     111             :   long nx, ny, m, ix, iy;
     112             :   GEN x, y, w;
     113    20733914 :   switch(n)
     114             :   {
     115       48676 :     case 1: return mkvecsmall(1);
     116     5466830 :     case 2: return (v[1] <= v[2])? mkvecsmall2(1,2): mkvecsmall2(2,1);
     117             :     case 3:
     118     6315494 :       if (v[1] <= v[2]) {
     119     5424668 :         if (v[2] <= v[3]) return mkvecsmall3(1,2,3);
     120     2157530 :         return (v[1] <= v[3])? mkvecsmall3(1,3,2)
     121     1078765 :                              : mkvecsmall3(3,1,2);
     122             :       } else {
     123      890826 :         if (v[1] <= v[3]) return mkvecsmall3(2,1,3);
     124     1265952 :         return (v[2] <= v[3])? mkvecsmall3(2,3,1)
     125      632976 :                              : mkvecsmall3(3,2,1);
     126             :       }
     127             :   }
     128     8902914 :   nx = n>>1; ny = n-nx;
     129     8902914 :   w = cgetg(n+1,t_VECSMALL);
     130     8902913 :   x = vecsmall_indexsortspec(v,nx);
     131     8902914 :   y = vecsmall_indexsortspec(v+nx,ny);
     132   155458159 :   for (m=1, ix=1, iy=1; ix<=nx && iy<=ny; )
     133   137652331 :     if (v[x[ix]] <= v[y[iy]+nx])
     134    89514837 :       w[m++] = x[ix++];
     135             :     else
     136    48137494 :       w[m++] = y[iy++]+nx;
     137     8902914 :   for(;ix<=nx;) w[m++] = x[ix++];
     138     8902914 :   for(;iy<=ny;) w[m++] = y[iy++]+nx;
     139     8902914 :   avma = (pari_sp)w; return w;
     140             : }
     141             : 
     142             : /*indirect sort.*/
     143             : GEN
     144     2928149 : vecsmall_indexsort(GEN V)
     145             : {
     146     2928149 :   long l=lg(V)-1;
     147     2928149 :   if (l==0) return cgetg(1, t_VECSMALL);
     148     2928086 :   return vecsmall_indexsortspec(V,l);
     149             : }
     150             : 
     151             : /* assume V sorted */
     152             : GEN
     153        1387 : vecsmall_uniq_sorted(GEN V)
     154             : {
     155             :   GEN W;
     156        1387 :   long i,j, l = lg(V);
     157        1387 :   if (l == 1) return vecsmall_copy(V);
     158        1379 :   W = cgetg(l,t_VECSMALL);
     159        1379 :   W[1] = V[1];
     160        2681 :   for(i=j=2; i<l; i++)
     161        1302 :     if (V[i] != W[j-1]) W[j++] = V[i];
     162        1379 :   stackdummy((pari_sp)(W + l), (pari_sp)(W + j));
     163        1379 :   setlg(W, j); return W;
     164             : }
     165             : 
     166             : GEN
     167        1380 : vecsmall_uniq(GEN V)
     168             : {
     169        1380 :   pari_sp av = avma;
     170        1380 :   V = zv_copy(V); vecsmall_sort(V);
     171        1380 :   return gerepileuptoleaf(av, vecsmall_uniq_sorted(V));
     172             : }
     173             : 
     174             : /* assume x sorted */
     175             : long
     176           0 : vecsmall_duplicate_sorted(GEN x)
     177             : {
     178           0 :   long i,k,l=lg(x);
     179           0 :   if (l==1) return 0;
     180           0 :   for (k=x[1],i=2; i<l; k=x[i++])
     181           0 :     if (x[i] == k) return i;
     182           0 :   return 0;
     183             : }
     184             : 
     185             : long
     186       12724 : vecsmall_duplicate(GEN x)
     187             : {
     188       12724 :   pari_sp av=avma;
     189       12724 :   GEN p=vecsmall_indexsort(x);
     190       12724 :   long k,i,r=0,l=lg(x);
     191       12724 :   if (l==1) return 0;
     192       17177 :   for (k=x[p[1]],i=2; i<l; k=x[p[i++]])
     193        4453 :     if (x[p[i]] == k) { r=p[i]; break; }
     194       12724 :   avma=av;
     195       12724 :   return r;
     196             : }
     197             : 
     198             : /*************************************************************************/
     199             : /**                                                                     **/
     200             : /**             Routines for handling vectors of VECSMALL               **/
     201             : /**                                                                     **/
     202             : /*************************************************************************/
     203             : 
     204             : GEN
     205      120960 : vecvecsmall_sort(GEN x)
     206             : {
     207      120960 :   return gen_sort(x, (void*)&vecsmall_lexcmp, cmp_nodata);
     208             : }
     209             : 
     210             : GEN
     211         322 : vecvecsmall_sort_uniq(GEN x)
     212             : {
     213         322 :   return gen_sort_uniq(x, (void*)&vecsmall_lexcmp, cmp_nodata);
     214             : }
     215             : 
     216             : GEN
     217          21 : vecvecsmall_indexsort(GEN x)
     218             : {
     219          21 :   return gen_indexsort(x, (void*)&vecsmall_lexcmp, cmp_nodata);
     220             : }
     221             : 
     222             : long
     223    13935173 : vecvecsmall_search(GEN x, GEN y, long flag)
     224             : {
     225    13935173 :   return gen_search(x,y,flag,(void*)vecsmall_prefixcmp, cmp_nodata);
     226             : }
     227             : 
     228             : /* assume x non empty */
     229             : long
     230         133 : vecvecsmall_max(GEN x)
     231             : {
     232         133 :   long i, l = lg(x), m = vecsmall_max(gel(x,1));
     233        1099 :   for (i = 2; i < l; i++)
     234             :   {
     235         966 :     long t = vecsmall_max(gel(x,i));
     236         966 :     if (t > m) m = t;
     237             :   }
     238         133 :   return m;
     239             : }
     240             : 
     241             : /*************************************************************************/
     242             : /**                                                                     **/
     243             : /**                  Routines for handling permutations                 **/
     244             : /**                                                                     **/
     245             : /*************************************************************************/
     246             : 
     247             : /* Permutations may be given by
     248             :  * perm (VECSMALL): a bijection from 1...n to 1...n i-->perm[i]
     249             :  * cyc (VEC of VECSMALL): a product of disjoint cycles. */
     250             : 
     251             : /* Multiply (compose) two permutations, putting the result in the second one. */
     252             : static void
     253           7 : perm_mul_inplace2(GEN s, GEN t)
     254             : {
     255           7 :   long i, l = lg(s);
     256           7 :   for (i = 1; i < l; i++) t[i] = s[t[i]];
     257           7 : }
     258             : 
     259             : /* Orbits of the subgroup generated by v on {1,..,n} */
     260             : static GEN
     261      419251 : vecperm_orbits_i(GEN v, long n)
     262             : {
     263      419251 :   long mj = 1, lv = lg(v), k, l;
     264      419251 :   GEN cycle = cgetg(n+1, t_VEC), bit = const_vecsmall(n, 0);
     265     3268694 :   for (k = 1, l = 1; k <= n;)
     266             :   {
     267     2430192 :     long m = 1;
     268     2430192 :     GEN cy = cgetg(n+1, t_VECSMALL);
     269     2430192 :     for (  ; bit[mj]; mj++) /*empty*/;
     270     2430192 :     k++; cy[m++] = mj;
     271     2430192 :     bit[mj++] = 1;
     272             :     for(;;)
     273             :     {
     274     3931385 :       long o, mold = m;
     275     7865808 :       for (o = 1; o < lv; o++)
     276             :       {
     277     3934423 :         GEN vo = gel(v,o);
     278             :         long p;
     279    16161279 :         for (p = 1; p < m; p++) /* m increases! */
     280             :         {
     281    12226856 :           long j = vo[ cy[p] ];
     282    12226856 :           if (!bit[j]) cy[m++] = j;
     283    12226856 :           bit[j] = 1;
     284             :         }
     285             :       }
     286     3931385 :       if (m == mold) break;
     287     1501193 :       k += m - mold;
     288     1501193 :     }
     289     2430192 :     setlg(cy, m); gel(cycle,l++) = cy;
     290             :   }
     291      419251 :   setlg(cycle, l); return cycle;
     292             : }
     293             : /* memory clean version */
     294             : GEN
     295         805 : vecperm_orbits(GEN v, long n)
     296             : {
     297         805 :   pari_sp av = avma;
     298         805 :   return gerepilecopy(av, vecperm_orbits_i(v, n));
     299             : }
     300             : 
     301             : /* Compute the cyclic decomposition of a permutation */
     302             : GEN
     303        4018 : perm_cycles(GEN v)
     304             : {
     305        4018 :   pari_sp av = avma;
     306        4018 :   return gerepilecopy(av, vecperm_orbits_i(mkvec(v), lg(v)-1));
     307             : }
     308             : 
     309             : static long
     310         168 : isperm(GEN v)
     311             : {
     312         168 :   long i, l = lg(v)-1;
     313         168 :   if (typ(v)!=t_VECSMALL) return 0;
     314        2184 :   for (i=1; i<=l; i++)
     315        2016 :     if (v[i]<1 || v[i]>l) return 0;
     316         168 :   return 1;
     317             : }
     318             : 
     319             : /* Output the order of p */
     320             : long
     321      384986 : perm_order(GEN v)
     322             : {
     323      384986 :   pari_sp ltop = avma;
     324      384986 :   GEN c = vecperm_orbits_i(mkvec(v), lg(v)-1);
     325             :   long i, d;
     326      384986 :   for(i=1, d=1; i<lg(c); i++) d = clcm(d, lg(gel(c,i))-1);
     327      384986 :   avma = ltop; return d;
     328             : }
     329             : 
     330             : long
     331          84 : permorder(GEN v)
     332             : {
     333          84 :   if (!isperm(v)) pari_err_TYPE("permorder",v);
     334          84 :   return perm_order(v);
     335             : }
     336             : 
     337             : /* sign of a permutation */
     338             : long
     339       29442 : perm_sign(GEN v)
     340             : {
     341       29442 :   pari_sp av = avma;
     342       29442 :   GEN c = vecperm_orbits_i(mkvec(v), lg(v)-1);
     343       29442 :   long i, l = lg(c), s = 1;
     344      294842 :   for (i = 1; i < l; i++)
     345      265400 :     if (odd(lg(gel(c, i)))) s = -s;
     346       29442 :   avma = av; return s;
     347             : }
     348             : 
     349             : long
     350          84 : permsign(GEN v)
     351             : {
     352          84 :   if (!isperm(v)) pari_err_TYPE("permorder",v);
     353          84 :   return perm_sign(v);
     354             : }
     355             : 
     356             : GEN
     357        1883 : cyc_pow(GEN cyc, long exp)
     358             : {
     359             :   long i, j, k, l, r;
     360             :   GEN c;
     361        6328 :   for (r = j = 1; j < lg(cyc); j++)
     362             :   {
     363        4445 :     long n = lg(gel(cyc,j)) - 1;
     364        4445 :     r += cgcd(n, exp);
     365             :   }
     366        1883 :   c = cgetg(r, t_VEC);
     367        6328 :   for (r = j = 1; j < lg(cyc); j++)
     368             :   {
     369        4445 :     GEN v = gel(cyc,j);
     370        4445 :     long n = lg(v) - 1, e = smodss(exp,n), g = (long)ugcd(n, e), m = n / g;
     371        9709 :     for (i = 0; i < g; i++)
     372             :     {
     373        5264 :       GEN p = cgetg(m+1, t_VECSMALL);
     374        5264 :       gel(c,r++) = p;
     375       17766 :       for (k = 1, l = i; k <= m; k++)
     376             :       {
     377       12502 :         p[k] = v[l+1];
     378       12502 :         l += e; if (l >= n) l -= n;
     379             :       }
     380             :     }
     381             :   }
     382        1883 :   return c;
     383             : }
     384             : 
     385             : /* Compute the power of a permutation given by product of cycles
     386             :  * Ouput a perm, not a cyc */
     387             : GEN
     388           0 : cyc_pow_perm(GEN cyc, long exp)
     389             : {
     390             :   long e, j, k, l, n;
     391             :   GEN p;
     392           0 :   for (n = 0, j = 1; j < lg(cyc); j++) n += lg(gel(cyc,j))-1;
     393           0 :   p = cgetg(n + 1, t_VECSMALL);
     394           0 :   for (j = 1; j < lg(cyc); j++)
     395             :   {
     396           0 :     GEN v = gel(cyc,j);
     397           0 :     n = lg(v) - 1; e = smodss(exp, n);
     398           0 :     for (k = 1, l = e; k <= n; k++)
     399             :     {
     400           0 :       p[v[k]] = v[l+1];
     401           0 :       if (++l == n) l = 0;
     402             :     }
     403             :   }
     404           0 :   return p;
     405             : }
     406             : 
     407             : GEN
     408        8302 : perm_pow(GEN perm, long exp)
     409             : {
     410        8302 :   long i, r = lg(perm)-1;
     411        8302 :   GEN p = zero_zv(r);
     412        8302 :   pari_sp av = avma;
     413        8302 :   GEN v = cgetg(r+1, t_VECSMALL);
     414      141673 :   for (i=1; i<=r; i++)
     415             :   {
     416             :     long e, n, k, l;
     417      133371 :     if (p[i]) continue;
     418       50771 :     v[1] = i;
     419      133371 :     for (n=1, k=perm[i]; k!=i; k=perm[k], n++)
     420       82600 :       v[n+1] = k;
     421       50771 :     e = smodss(exp, n);
     422      184142 :     for (k = 1, l = e; k <= n; k++)
     423             :     {
     424      133371 :       p[v[k]] = v[l+1];
     425      133371 :       if (++l == n) l = 0;
     426             :     }
     427             :   }
     428        8302 :   avma = av; return p;
     429             : }
     430             : 
     431             : static GEN
     432          21 : perm_to_GAP(GEN p)
     433             : {
     434          21 :   pari_sp ltop=avma;
     435             :   GEN gap;
     436             :   GEN x;
     437             :   long i;
     438          21 :   long nb, c=0;
     439             :   char *s;
     440             :   long sz;
     441          21 :   long lp=lg(p)-1;
     442          21 :   if (typ(p) != t_VECSMALL)  pari_err_TYPE("perm_to_GAP",p);
     443          21 :   x = perm_cycles(p);
     444          21 :   sz = (long) ((bfffo(lp)+1) * LOG10_2 + 1);
     445             :   /*Dry run*/
     446         133 :   for (i = 1, nb = 1; i < lg(x); ++i)
     447             :   {
     448         112 :     GEN z = gel(x,i);
     449         112 :     long lz = lg(z)-1;
     450         112 :     nb += 1+lz*(sz+2);
     451             :   }
     452          21 :   nb++;
     453             :   /*Real run*/
     454          21 :   gap = cgetg(nchar2nlong(nb) + 1, t_STR);
     455          21 :   s = GSTR(gap);
     456         133 :   for (i = 1; i < lg(x); ++i)
     457             :   {
     458             :     long j;
     459         112 :     GEN z = gel(x,i);
     460         112 :     if (lg(z) > 2)
     461             :     {
     462         112 :       s[c++] = '(';
     463         364 :       for (j = 1; j < lg(z); ++j)
     464             :       {
     465         252 :         if (j > 1)
     466             :         {
     467         140 :           s[c++] = ','; s[c++] = ' ';
     468             :         }
     469         252 :         sprintf(s+c,"%ld",z[j]);
     470         252 :         while(s[c++]) /* empty */;
     471         252 :         c--;
     472             :       }
     473         112 :       s[c++] = ')';
     474             :     }
     475             :   }
     476          21 :   if (!c) { s[c++]='('; s[c++]=')'; }
     477          21 :   s[c] = '\0';
     478          21 :   return gerepileupto(ltop,gap);
     479             : }
     480             : 
     481             : int
     482      536732 : perm_commute(GEN s, GEN t)
     483             : {
     484      536732 :   long i, l = lg(t);
     485    38374476 :   for (i = 1; i < l; i++)
     486    37851520 :     if (t[ s[i] ] != s[ t[i] ]) return 0;
     487      522956 :   return 1;
     488             : }
     489             : 
     490             : /*************************************************************************/
     491             : /**                                                                     **/
     492             : /**                  Routines for handling groups                       **/
     493             : /**                                                                     **/
     494             : /*************************************************************************/
     495             : /* A Group is a t_VEC [gen,orders]
     496             :  * gen (vecvecsmall): list of generators given by permutations
     497             :  * orders (vecsmall): relatives orders of generators. */
     498      432292 : INLINE GEN grp_get_gen(GEN G) { return gel(G,1); }
     499      737625 : INLINE GEN grp_get_ord(GEN G) { return gel(G,2); }
     500             : 
     501             : /* A Quotient Group is a t_VEC [gen,coset]
     502             :  * gen (vecvecsmall): coset generators
     503             :  * coset (vecsmall): gen[coset[p[1]]] generate the p-coset.
     504             :  */
     505       76174 : INLINE GEN quo_get_gen(GEN C) { return gel(C,1); }
     506       12173 : INLINE GEN quo_get_coset(GEN C) { return gel(C,2); }
     507             : 
     508             : static GEN
     509       30688 : trivialsubgroups(void)
     510       30688 : { GEN L = cgetg(2, t_VEC); gel(L,1) = trivialgroup(); return L; }
     511             : 
     512             : /* Compute the order of p modulo the group given by a set */
     513             : long
     514      122654 : perm_relorder(GEN p, GEN set)
     515             : {
     516      122654 :   pari_sp ltop = avma;
     517      122654 :   long n = 1;
     518      122654 :   long q = p[1];
     519      122654 :   while (!F2v_coeff(set,q)) { q = p[q]; n++; }
     520      122654 :   avma = ltop; return n;
     521             : }
     522             : 
     523             : GEN
     524        8057 : perm_generate(GEN S, GEN H, long o)
     525             : {
     526        8057 :   long i, n = lg(H)-1;
     527        8057 :   GEN L = cgetg(n*o + 1, t_VEC);
     528        8057 :   for(i=1; i<=n;     i++) gel(L,i) = vecsmall_copy(gel(H,i));
     529        8057 :   for(   ; i <= n*o; i++) gel(L,i) = perm_mul(gel(L,i-n), S);
     530        8057 :   return L;
     531             : }
     532             : 
     533             : /*Return the order (cardinality) of a group */
     534             : long
     535      318192 : group_order(GEN G)
     536             : {
     537      318192 :   return zv_prod(grp_get_ord(G));
     538             : }
     539             : 
     540             : /* G being a subgroup of S_n, output n */
     541             : long
     542        6713 : group_domain(GEN G)
     543             : {
     544        6713 :   GEN gen = grp_get_gen(G);
     545        6713 :   if (lg(gen) < 2) pari_err_DOMAIN("group_domain", "#G", "=", gen_1,G);
     546        6713 :   return lg(gel(gen,1)) - 1;
     547             : }
     548             : 
     549             : /*Left coset of g mod G: gG*/
     550             : GEN
     551      136353 : group_leftcoset(GEN G, GEN g)
     552             : {
     553      136353 :   GEN gen = grp_get_gen(G), ord = grp_get_ord(G);
     554      136353 :   GEN res = cgetg(group_order(G)+1, t_VEC);
     555             :   long i, j, k;
     556      136353 :   gel(res,1) = vecsmall_copy(g);
     557      136353 :   k = 1;
     558      253498 :   for (i = 1; i < lg(gen); i++)
     559             :   {
     560      117145 :     long c = k * (ord[i] - 1);
     561      117145 :     for (j = 1; j <= c; j++) gel(res,++k) = perm_mul(gel(res,j), gel(gen,i));
     562             :   }
     563      136353 :   return res;
     564             : }
     565             : /*Right coset of g mod G: Gg*/
     566             : GEN
     567       60438 : group_rightcoset(GEN G, GEN g)
     568             : {
     569       60438 :   GEN gen = grp_get_gen(G), ord = grp_get_ord(G);
     570       60438 :   GEN res = cgetg(group_order(G)+1, t_VEC);
     571             :   long i, j, k;
     572       60438 :   gel(res,1) = vecsmall_copy(g);
     573       60438 :   k = 1;
     574      101556 :   for (i = 1; i < lg(gen); i++)
     575             :   {
     576       41118 :     long c = k * (ord[i] - 1);
     577       41118 :     for (j = 1; j <= c; j++) gel(res,++k) = perm_mul(gel(gen,i), gel(res,j));
     578             :   }
     579       60438 :   return res;
     580             : }
     581             : /*Elements of a group from the generators, cf group_leftcoset*/
     582             : GEN
     583       70777 : group_elts(GEN G, long n)
     584             : {
     585       70777 :   GEN gen = grp_get_gen(G), ord = grp_get_ord(G);
     586       70777 :   GEN res = cgetg(group_order(G)+1, t_VEC);
     587             :   long i, j, k;
     588       70777 :   gel(res,1) = identity_perm(n);
     589       70777 :   k = 1;
     590      142660 :   for (i = 1; i < lg(gen); i++)
     591             :   {
     592       71883 :     long c = k * (ord[i] - 1);
     593             :     /* j = 1, use res[1] = identity */
     594       71883 :     gel(res,++k) = vecsmall_copy(gel(gen,i));
     595       71883 :     for (j = 2; j <= c; j++) gel(res,++k) = perm_mul(gel(res,j), gel(gen,i));
     596             :   }
     597       70777 :   return res;
     598             : }
     599             : 
     600             : GEN
     601       13146 : groupelts_set(GEN elts, long n)
     602             : {
     603       13146 :   GEN res = zero_F2v(n);
     604       13146 :   long i, l = lg(elts);
     605       65527 :   for(i=1; i<l; i++)
     606       52381 :     F2v_set(res,mael(elts,i,1));
     607       13146 :   return res;
     608             : }
     609             : 
     610             : /*Elements of a group from the generators, returned as a set (bitmap)*/
     611             : GEN
     612       58114 : group_set(GEN G, long n)
     613             : {
     614       58114 :   GEN res = zero_F2v(n);
     615       58114 :   pari_sp av = avma;
     616       58114 :   GEN elts = group_elts(G, n);
     617       58114 :   long i, l = lg(elts);
     618      188391 :   for(i=1; i<l; i++)
     619      130277 :     F2v_set(res,mael(elts,i,1));
     620       58114 :   avma = av;
     621       58114 :   return res;
     622             : }
     623             : 
     624             : static int
     625       16646 : sgcmp(GEN a, GEN b) { return vecsmall_lexcmp(gel(a,1),gel(b,1)); }
     626             : 
     627             : GEN
     628         462 : subgroups_tableset(GEN S, long n)
     629             : {
     630         462 :   long i, l = lg(S);
     631         462 :   GEN  v = cgetg(l, t_VEC);
     632        5194 :   for(i=1; i<l; i++)
     633        4732 :     gel(v,i) = mkvec2(group_set(gel(S,i), n), mkvecsmall(i));
     634         462 :   gen_sort_inplace(v,(void*)sgcmp,cmp_nodata, NULL);
     635         462 :   return v;
     636             : }
     637             : 
     638             : long
     639        1897 : tableset_find_index(GEN tbl, GEN set)
     640             : {
     641        1897 :   long i = tablesearch(tbl,mkvec2(set,mkvecsmall(0)),sgcmp);
     642        1897 :   if (!i) return 0;
     643        1897 :   return mael3(tbl,i,2,1);
     644             : }
     645             : 
     646             : GEN
     647       30688 : trivialgroup(void) { retmkvec2(cgetg(1,t_VEC), cgetg(1,t_VECSMALL)); }
     648             : /*Cyclic group generated by g of order s*/
     649             : GEN
     650        6979 : cyclicgroup(GEN g, long s)
     651        6979 : { retmkvec2(mkvec( vecsmall_copy(g) ),
     652             :             mkvecsmall(s)); }
     653             : /*Return the group generated by g1,g2 of relative orders s1,s2*/
     654             : GEN
     655         945 : dicyclicgroup(GEN g1, GEN g2, long s1, long s2)
     656         945 : { retmkvec2( mkvec2(vecsmall_copy(g1), vecsmall_copy(g2)),
     657             :              mkvecsmall2(s1, s2) ); }
     658             : 
     659             : /* return the quotient map G --> G/H */
     660             : /*The ouput is [gen,hash]*/
     661             : /* gen (vecvecsmall): coset generators
     662             :  * coset (vecsmall): vecsmall of coset number) */
     663             : GEN
     664        5096 : group_quotient(GEN G, GEN H)
     665             : {
     666        5096 :   pari_sp ltop = avma;
     667             :   GEN  p2, p3;
     668        5096 :   long i, j, a = 1;
     669        5096 :   long n = group_domain(G), o = group_order(H);
     670        5096 :   GEN  elt = group_elts(G,n), el;
     671        5096 :   long le = lg(elt)-1;
     672        5096 :   GEN used = zero_F2v(le+1);
     673        5096 :   long l = le/o;
     674        5096 :   p2 = cgetg(l+1, t_VEC);
     675        5096 :   p3 = zero_zv(n);
     676        5096 :   el = zero_zv(n);
     677       69020 :   for (i = 1; i<=le; i++)
     678       63924 :     el[mael(elt,i,1)]=i;
     679       36134 :   for (i = 1; i <= l; ++i)
     680             :   {
     681             :     GEN V;
     682       31045 :     while(F2v_coeff(used,a)) a++;
     683       31045 :     V = group_leftcoset(H,gel(elt,a));
     684       31045 :     gel(p2,i) = gel(V,1);
     685       94864 :     for(j=1;j<lg(V);j++)
     686             :     {
     687       63826 :       long b = el[mael(V,j,1)];
     688       63826 :       if (b==0) pari_err_IMPL("group_quotient for a non-WSS group");
     689       63819 :       F2v_set(used,b);
     690             :     }
     691       94850 :     for (j = 1; j <= o; j++)
     692       63812 :       p3[mael(V, j, 1)] = i;
     693             :   }
     694        5089 :   return gerepilecopy(ltop,mkvec2(p2,p3));
     695             : }
     696             : 
     697             : /*Compute the image of a permutation by a quotient map.*/
     698             : GEN
     699       12173 : quotient_perm(GEN C, GEN p)
     700             : {
     701       12173 :   GEN gen = quo_get_gen(C);
     702       12173 :   GEN coset = quo_get_coset(C);
     703       12173 :   long j, l = lg(gen);
     704       12173 :   GEN p3 = cgetg(l, t_VECSMALL);
     705      115738 :   for (j = 1; j < l; ++j)
     706             :   {
     707      103565 :     p3[j] = coset[p[mael(gen,j,1)]];
     708      103565 :     if (p3[j]==0) pari_err_IMPL("quotient_perm for a non-WSS group");
     709             :   }
     710       12173 :   return p3;
     711             : }
     712             : 
     713             : /* H is a subgroup of G, C is the quotient map G --> G/H
     714             :  *
     715             :  * Lift a subgroup S of G/H to a subgroup of G containing H */
     716             : GEN
     717       29456 : quotient_subgroup_lift(GEN C, GEN H, GEN S)
     718             : {
     719       29456 :   GEN genH = grp_get_gen(H);
     720       29456 :   GEN genS = grp_get_gen(S);
     721       29456 :   GEN genC = quo_get_gen(C);
     722       29456 :   long l1 = lg(genH)-1;
     723       29456 :   long l2 = lg(genS)-1, j;
     724       29456 :   GEN p1 = cgetg(3, t_VEC), L = cgetg(l1+l2+1, t_VEC);
     725       29456 :   for (j = 1; j <= l1; ++j) gel(L,j) = gel(genH,j);
     726       29456 :   for (j = 1; j <= l2; ++j) gel(L,l1+j) = gel(genC, mael(genS,j,1));
     727       29456 :   gel(p1,1) = L;
     728       29456 :   gel(p1,2) = vecsmall_concat(grp_get_ord(H), grp_get_ord(S));
     729       29456 :   return p1;
     730             : }
     731             : 
     732             : /* Let G a group and C a quotient map G --> G/H
     733             :  * Assume H is normal, return the group G/H */
     734             : GEN
     735        5089 : quotient_group(GEN C, GEN G)
     736             : {
     737        5089 :   pari_sp ltop = avma;
     738             :   GEN Qgen, Qord, Qelt, Qset, Q;
     739        5089 :   GEN Cgen = quo_get_gen(C);
     740        5089 :   GEN Ggen = grp_get_gen(G);
     741        5089 :   long i,j, n = lg(Cgen)-1, l = lg(Ggen);
     742        5089 :   Qord = cgetg(l, t_VECSMALL);
     743        5089 :   Qgen = cgetg(l, t_VEC);
     744        5089 :   Qelt = mkvec(identity_perm(n));
     745        5089 :   Qset = groupelts_set(Qelt, n);
     746       17262 :   for (i = 1, j = 1; i < l; ++i)
     747             :   {
     748       12173 :     GEN  g = quotient_perm(C, gel(Ggen,i));
     749       12173 :     long o = perm_relorder(g, Qset);
     750       12173 :     gel(Qgen,j) = g;
     751       12173 :     Qord[j] = o;
     752       12173 :     if (o != 1)
     753             :     {
     754        8057 :       Qelt = perm_generate(g, Qelt, o);
     755        8057 :       Qset = groupelts_set(Qelt, n);
     756        8057 :       j++;
     757             :     }
     758             :   }
     759        5089 :   setlg(Qgen,j);
     760        5089 :   setlg(Qord,j); Q = mkvec2(Qgen, Qord);
     761        5089 :   return gerepilecopy(ltop,Q);
     762             : }
     763             : 
     764             : /* Return 1 if g normalizes N, 0 otherwise */
     765             : long
     766       60438 : group_perm_normalize(GEN N, GEN g)
     767             : {
     768       60438 :   pari_sp ltop = avma;
     769       60438 :   long r = gequal(vecvecsmall_sort(group_leftcoset(N, g)),
     770             :                   vecvecsmall_sort(group_rightcoset(N, g)));
     771       60438 :   avma = ltop; return r;
     772             : }
     773             : 
     774             : /* L is a list of subgroups, C is a coset and r a relative order.*/
     775             : static GEN
     776       44870 : liftlistsubgroups(GEN L, GEN C, long r)
     777             : {
     778       44870 :   pari_sp ltop = avma;
     779       44870 :   long c = lg(C)-1, l = lg(L)-1, n = lg(gel(C,1))-1, i, k;
     780             :   GEN R;
     781       44870 :   if (!l) return cgetg(1,t_VEC);
     782       37429 :   R = cgetg(l*c+1, t_VEC);
     783       89537 :   for (i = 1, k = 1; i <= l; ++i)
     784             :   {
     785       52108 :     GEN S = gel(L,i), Selt = group_set(S,n);
     786       52108 :     GEN gen = grp_get_gen(S);
     787       52108 :     GEN ord = grp_get_ord(S);
     788             :     long j;
     789      158214 :     for (j = 1; j <= c; ++j)
     790             :     {
     791      106106 :       GEN p = gel(C,j);
     792      106106 :       if (perm_relorder(p, Selt) == r && group_perm_normalize(S, p))
     793       58233 :         gel(R,k++) = mkvec2(vec_append(gen, p),
     794             :                             vecsmall_append(ord, r));
     795             :     }
     796             :   }
     797       37429 :   setlg(R, k);
     798       37429 :   return gerepilecopy(ltop, R);
     799             : }
     800             : 
     801             : /* H is a normal subgroup, C is the quotient map G -->G/H,
     802             :  * S is a subgroup of G/H, and G is embedded in Sym(l)
     803             :  * Return all the subgroups K of G such that
     804             :  * S= K mod H and K inter H={1} */
     805             : static GEN
     806       29456 : liftsubgroup(GEN C, GEN H, GEN S)
     807             : {
     808       29456 :   pari_sp ltop = avma;
     809       29456 :   GEN V = trivialsubgroups();
     810       29456 :   GEN Sgen = grp_get_gen(S);
     811       29456 :   GEN Sord = grp_get_ord(S);
     812       29456 :   GEN Cgen = quo_get_gen(C);
     813       29456 :   long n = lg(Sgen), i;
     814       74326 :   for (i = 1; i < n; ++i)
     815             :   { /*loop over generators of S*/
     816       44870 :     GEN W = group_leftcoset(H, gel(Cgen, mael(Sgen, i, 1)));
     817       44870 :     V = liftlistsubgroups(V, W, Sord[i]);
     818             :   }
     819       29456 :   return gerepilecopy(ltop,V);
     820             : }
     821             : 
     822             : /* 1:A4 2:S4 0: other */
     823             : long
     824        4914 : group_isA4S4(GEN G)
     825             : {
     826        4914 :   GEN elt = grp_get_gen(G);
     827        4914 :   GEN ord = grp_get_ord(G);
     828        4914 :   long n = lg(ord);
     829        4914 :   if (n != 4 && n != 5) return 0;
     830        1330 :   if (ord[1]!=2 || ord[2]!=2 || ord[3]!=3) return 0;
     831          14 :   if (perm_commute(gel(elt,1),gel(elt,3))) return 0;
     832          14 :   if (n==4) return 1;
     833           7 :   if (ord[4]!=2) return 0;
     834           7 :   if (perm_commute(gel(elt,3),gel(elt,4))) return 0;
     835           7 :   return 2;
     836             : }
     837             : /* compute all the subgroups of a group G */
     838             : GEN
     839        6146 : group_subgroups(GEN G)
     840             : {
     841        6146 :   pari_sp ltop = avma;
     842             :   GEN p1, H, C, Q, M, sg1, sg2, sg3;
     843        6146 :   GEN gen = grp_get_gen(G);
     844        6146 :   GEN ord = grp_get_ord(G);
     845        6146 :   long lM, i, j, n = lg(gen);
     846        6146 :   if (n == 1) return trivialsubgroups();
     847        4914 :   if (group_isA4S4(G))
     848             :   {
     849          14 :     GEN s = gel(gen,1);       /*s = (1,2)(3,4) */
     850          14 :     GEN t = gel(gen,2);       /*t = (1,3)(2,4) */
     851          14 :     GEN st = perm_mul(s, t); /*st = (1,4)(2,3) */
     852          14 :     H = dicyclicgroup(s, t, 2, 2);
     853             :     /* sg3 is the list of subgroups intersecting only partially with H*/
     854          14 :     sg3 = cgetg((n==4)?4: 10, t_VEC);
     855          14 :     gel(sg3,1) = cyclicgroup(s, 2);
     856          14 :     gel(sg3,2) = cyclicgroup(t, 2);
     857          14 :     gel(sg3,3) = cyclicgroup(st, 2);
     858          14 :     if (n==5)
     859             :     {
     860           7 :       GEN u = gel(gen,3);
     861           7 :       GEN v = gel(gen,4), w, u2;
     862           7 :       if (zv_equal(perm_conj(u,s), t)) /*u=(2,3,4)*/
     863           7 :         u2 = perm_mul(u,u);
     864             :       else
     865             :       {
     866           0 :         u2 = u;
     867           0 :         u = perm_mul(u,u);
     868             :       }
     869           7 :       if (perm_order(v)==2)
     870             :       {
     871           7 :         if (!perm_commute(s,v)) /*v=(1,2)*/
     872             :         {
     873           0 :           v = perm_conj(u,v);
     874           0 :           if (!perm_commute(s,v)) v = perm_conj(u,v);
     875             :         }
     876           7 :         w = perm_mul(v,t); /*w=(1,4,2,3)*/
     877             :       }
     878             :       else
     879             :       {
     880           0 :         w = v;
     881           0 :         if (!zv_equal(perm_mul(w,w), s)) /*w=(1,4,2,3)*/
     882             :         {
     883           0 :           w = perm_conj(u,w);
     884           0 :           if (!zv_equal(perm_mul(w,w), s)) w = perm_conj(u,w);
     885             :         }
     886           0 :         v = perm_mul(w,t); /*v=(1,2)*/
     887             :       }
     888           7 :       gel(sg3,4) = dicyclicgroup(s,v,2,2);
     889           7 :       gel(sg3,5) = dicyclicgroup(t,perm_conj(u,v),2,2);
     890           7 :       gel(sg3,6) = dicyclicgroup(st,perm_conj(u2,v),2,2);
     891           7 :       gel(sg3,7) = dicyclicgroup(s,w,2,2);
     892           7 :       gel(sg3,8) = dicyclicgroup(t,perm_conj(u,w),2,2);
     893           7 :       gel(sg3,9) = dicyclicgroup(st,perm_conj(u2,w),2,2);
     894             :     }
     895             :   }
     896             :   else
     897             :   {
     898        4900 :     long osig = mael(factoru(ord[1]), 1, 1);
     899        4900 :     GEN sig = perm_pow(gel(gen,1), ord[1]/osig);
     900        4900 :     H = cyclicgroup(sig,osig);
     901        4900 :     sg3 = NULL;
     902             :   }
     903        4914 :   C = group_quotient(G,H);
     904        4907 :   Q = quotient_group(C,G);
     905        4907 :   M = group_subgroups(Q); lM = lg(M);
     906             :   /* sg1 is the list of subgroups containing H*/
     907        4900 :   sg1 = cgetg(lM, t_VEC);
     908        4900 :   for (i = 1; i < lM; ++i) gel(sg1,i) = quotient_subgroup_lift(C,H,gel(M,i));
     909             :   /*sg2 is a list of lists of subgroups not intersecting with H*/
     910        4900 :   sg2 = cgetg(lM, t_VEC);
     911             :   /* Loop over all subgroups of G/H */
     912        4900 :   for (j = 1; j < lM; ++j) gel(sg2,j) = liftsubgroup(C, H, gel(M,j));
     913        4900 :   p1 = gconcat(sg1, shallowconcat1(sg2));
     914        4900 :   if (sg3)
     915             :   {
     916          14 :     p1 = gconcat(p1, sg3);
     917          14 :     if (n==5) /*ensure that the D4 subgroups of S4 are in supersolvable format*/
     918          28 :       for(j = 3; j <= 5; j++)
     919             :       {
     920          21 :         GEN c = gmael(p1,j,1);
     921          21 :         if (!perm_commute(gel(c,1),gel(c,3)))
     922             :         {
     923          14 :           if (perm_commute(gel(c,2),gel(c,3))) { swap(gel(c,1), gel(c,2)); }
     924             :           else
     925           7 :             perm_mul_inplace2(gel(c,2), gel(c,1));
     926             :         }
     927             :       }
     928             :   }
     929        4900 :   return gerepileupto(ltop,p1);
     930             : }
     931             : 
     932             : /*return 1 if G is abelian, else 0*/
     933             : long
     934         917 : group_isabelian(GEN G)
     935             : {
     936         917 :   GEN g = grp_get_gen(G);
     937         917 :   long i, j, n = lg(g);
     938        1470 :   for(i=2; i<n; i++)
     939        1498 :     for(j=1; j<i; j++)
     940         945 :       if (!perm_commute(gel(g,i), gel(g,j))) return 0;
     941         770 :   return 1;
     942             : }
     943             : 
     944             : /*If G is abelian, return its HNF matrix*/
     945             : GEN
     946         329 : group_abelianHNF(GEN G, GEN S)
     947             : {
     948         329 :   GEN M, g = grp_get_gen(G), o = grp_get_ord(G);
     949         329 :   long i, j, k, n = lg(g);
     950         329 :   if (!group_isabelian(G)) return NULL;
     951         259 :   if (n==1) return cgetg(1,t_MAT);
     952         245 :   if (!S) S = group_elts(G, group_domain(G));
     953         245 :   M = cgetg(n,t_MAT);
     954         868 :   for(i=1; i<n; i++)
     955             :   {
     956         623 :     GEN P, C = cgetg(n,t_COL);
     957         623 :     pari_sp av = avma;
     958         623 :     gel(M,i) = C;
     959         623 :     P = perm_pow(gel(g,i), o[i]);
     960         903 :     for(j=1; j<lg(S); j++)
     961         903 :       if (zv_equal(P, gel(S,j))) break;
     962         623 :     avma = av;
     963         623 :     if (j==lg(S)) pari_err_BUG("galoisisabelian [inconsistent group]");
     964         623 :     j--;
     965        1162 :     for(k=1; k<i; k++)
     966             :     {
     967         539 :       long q = j / o[k];
     968         539 :       gel(C,k) = stoi(j - q*o[k]);
     969         539 :       j = q;
     970             :     }
     971         623 :     gel(C,k) = stoi(o[i]);
     972         623 :     for (k++; k<n; k++) gel(C,k) = gen_0;
     973             :   }
     974         245 :   return M;
     975             : }
     976             : 
     977             : /*If G is abelian, return its abstract SNF matrix*/
     978             : GEN
     979         280 : group_abelianSNF(GEN G, GEN L)
     980             : {
     981         280 :   pari_sp ltop = avma;
     982         280 :   GEN H = group_abelianHNF(G,L);
     983         280 :   if (!H) return NULL;
     984         210 :   return gerepileupto(ltop, smithclean( ZM_snf(H) ));
     985             : }
     986             : 
     987             : GEN
     988         189 : abelian_group(GEN v)
     989             : {
     990         189 :   long card = zv_prod(v), i, d = 1, l = lg(v);
     991         189 :   GEN G = cgetg(3,t_VEC), gen = cgetg(l,t_VEC);
     992         189 :   gel(G,1) = gen;
     993         189 :   gel(G,2) = vecsmall_copy(v);
     994         420 :   for(i=1; i<l; i++)
     995             :   {
     996         231 :     GEN p = cgetg(card+1, t_VECSMALL);
     997         231 :     long o = v[i], u = d*(o-1), j, k, l;
     998         231 :     gel(gen, i) = p;
     999             :     /* The following loop is over-optimized. Remember that I wrote it for
    1000             :      * testpermutation. Something has survived... BA */
    1001         826 :     for(j=1;j<=card;)
    1002             :     {
    1003        1582 :       for(k=1;k<o;k++)
    1004        1218 :         for(l=1;l<=d; l++,j++) p[j] = j+d;
    1005         364 :       for (l=1; l<=d; l++,j++) p[j] = j-u;
    1006             :     }
    1007         231 :     d += u;
    1008             :   }
    1009         189 :   return G;
    1010             : }
    1011             : 
    1012             : /*return 1 if H is a normal subgroup of G*/
    1013             : long
    1014          56 : group_subgroup_isnormal(GEN G, GEN H)
    1015             : {
    1016          56 :   GEN g = grp_get_gen(G);
    1017          56 :   long i, n = lg(g);
    1018          56 :   if (lg(grp_get_gen(H)) > 1 && group_domain(G) != group_domain(H))
    1019           0 :     pari_err_DOMAIN("group_subgroup_isnormal","domain(H)","!=",
    1020             :                     strtoGENstr("domain(G)"), H);
    1021         126 :   for(i=1; i<n; i++)
    1022          91 :     if (!group_perm_normalize(H, gel(g,i))) return 0;
    1023          35 :   return 1;
    1024             : }
    1025             : 
    1026             : long
    1027         504 : groupelts_exponent(GEN elts)
    1028             : {
    1029         504 :   long i, n = lg(elts)-1, expo = 1;
    1030        5712 :   for(i=1; i<=n; i++)
    1031        5208 :     expo = clcm(expo, perm_order(gel(elts,i)));
    1032         504 :   return expo;
    1033             : }
    1034             : 
    1035             : GEN
    1036         693 : groupelts_center(GEN S)
    1037             : {
    1038         693 :   pari_sp ltop = avma;
    1039         693 :   long i, j, n = lg(S)-1, l = n;
    1040         693 :   GEN V, elts = zero_F2v(n+1);
    1041       24969 :   for(i=1; i<=n; i++)
    1042             :   {
    1043       24276 :     if (F2v_coeff(elts,i)) { l--;  continue; }
    1044      545895 :     for(j=1; j<=n; j++)
    1045      535724 :       if (!perm_commute(gel(S,i),gel(S,j)))
    1046             :       {
    1047       13587 :         F2v_set(elts,i);
    1048       13587 :         F2v_set(elts,j); l--; break;
    1049             :       }
    1050             :   }
    1051         693 :   V = cgetg(l+1,t_VEC);
    1052       24969 :   for (i=1, j=1; i<=n ;i++)
    1053       24276 :     if (!F2v_coeff(elts,i)) gel(V,j++) = vecsmall_copy(gel(S,i));
    1054         693 :   return gerepileupto(ltop,V);
    1055             : }
    1056             : 
    1057             : GEN
    1058        4130 : groupelts_conjclasses(GEN elts, long *ptnbcl)
    1059             : {
    1060        4130 :   long i, j, cl = 0, n = lg(elts)-1;
    1061        4130 :   GEN c = const_vecsmall(n,0);
    1062       55216 :   for (i=1; i<=n; i++)
    1063             :   {
    1064       51086 :     GEN g = gel(elts,i);
    1065       51086 :     if (c[i]) continue;
    1066       35308 :     c[i] = ++cl;
    1067      541912 :     for(j=1; j<=n; j++)
    1068             :     {
    1069      506604 :       GEN h = perm_conj(gel(elts,j), g);
    1070      506604 :       long i2 = vecsearch(elts,h,NULL);
    1071      506604 :       c[i2] = cl;
    1072             :     }
    1073             :   }
    1074        4130 :   if (ptnbcl) *ptnbcl = cl;
    1075        4130 :   return c;
    1076             : }
    1077             : 
    1078             : GEN
    1079        1505 : conjclasses_repr(GEN conj, long nb)
    1080             : {
    1081        1505 :   long i, l = lg(conj);
    1082        1505 :   GEN e = const_vecsmall(nb, 0);
    1083       16905 :   for(i=1; i<l; i++)
    1084             :   {
    1085       15400 :     long ci = conj[i];
    1086       15400 :     if (!e[ci]) e[ci] = i;
    1087             :   }
    1088        1505 :   return e;
    1089             : }
    1090             : 
    1091             : /* S a list of generators */
    1092             : GEN
    1093           0 : groupelts_abelian_group(GEN S)
    1094             : {
    1095           0 :   pari_sp ltop = avma;
    1096             :   GEN Qgen, Qord, Qelt;
    1097           0 :   long i, j, n = lg(gel(S,1))-1, l = lg(S);
    1098           0 :   Qord = cgetg(l, t_VECSMALL);
    1099           0 :   Qgen = cgetg(l, t_VEC);
    1100           0 :   Qelt = mkvec(identity_perm(n));
    1101           0 :   for (i = 1, j = 1; i < l; ++i)
    1102             :   {
    1103           0 :     GEN  g = gel(S,i);
    1104           0 :     long o = perm_relorder(g, groupelts_set(Qelt, n));
    1105           0 :     gel(Qgen,j) = g;
    1106           0 :     Qord[j] = o;
    1107           0 :     if (o != 1) { Qelt = perm_generate(g, Qelt, o); j++; }
    1108             :   }
    1109           0 :   setlg(Qgen,j);
    1110           0 :   setlg(Qord,j);
    1111           0 :   return gerepilecopy(ltop, mkvec2(Qgen, Qord));
    1112             : }
    1113             : 
    1114             : GEN
    1115          14 : group_export_GAP(GEN G)
    1116             : {
    1117          14 :   pari_sp av = avma;
    1118          14 :   GEN s, comma, g = grp_get_gen(G);
    1119          14 :   long i, k, l = lg(g);
    1120          14 :   if (l == 1) return strtoGENstr("Group(())");
    1121           7 :   s = cgetg(2*l, t_VEC);
    1122           7 :   comma = strtoGENstr(", ");
    1123           7 :   gel(s,1) = strtoGENstr("Group(");
    1124          28 :   for (i=1, k=2; i < l; ++i)
    1125             :   {
    1126          21 :     if (i > 1) gel(s,k++) = comma;
    1127          21 :     gel(s,k++) = perm_to_GAP(gel(g,i));
    1128             :   }
    1129           7 :   gel(s,k++) = strtoGENstr(")");
    1130           7 :   return gerepilecopy(av, shallowconcat1(s));
    1131             : }
    1132             : 
    1133             : GEN
    1134          14 : group_export_MAGMA(GEN G)
    1135             : {
    1136          14 :   pari_sp av = avma;
    1137          14 :   GEN s, comma, g = grp_get_gen(G);
    1138          14 :   long i, k, l = lg(g);
    1139          14 :   if (l == 1) return strtoGENstr("PermutationGroup<1|>");
    1140           7 :   s = cgetg(2*l, t_VEC);
    1141           7 :   comma = strtoGENstr(", ");
    1142           7 :   gel(s,1) = gsprintf("PermutationGroup<%ld|",group_domain(G));
    1143          28 :   for (i=1, k=2; i < l; ++i)
    1144             :   {
    1145          21 :     if (i > 1) gel(s,k++) = comma;
    1146          21 :     gel(s,k++) = GENtoGENstr( vecsmall_to_vec(gel(g,i)) );
    1147             :   }
    1148           7 :   gel(s,k++) = strtoGENstr(">");
    1149           7 :   return gerepilecopy(av, shallowconcat1(s));
    1150             : }
    1151             : 
    1152             : GEN
    1153          28 : group_export(GEN G, long format)
    1154             : {
    1155          28 :   switch(format)
    1156             :   {
    1157          14 :   case 0: return group_export_GAP(G);
    1158          14 :   case 1: return group_export_MAGMA(G);
    1159             :   }
    1160           0 :   pari_err_FLAG("galoisexport");
    1161           0 :   return NULL; /*-Wall*/
    1162             : }

Generated by: LCOV version 1.11