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 20277-2bd9113) Lines: 573 606 94.6 %
Date: 2017-02-21 05:49:51 Functions: 67 69 97.1 %
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        4739 : vec_isconst(GEN v)
      24             : {
      25        4739 :   long i, l = lg(v);
      26             :   GEN w;
      27        4739 :   if (l==1) return 1;
      28        4739 :   w = gel(v,1);
      29       10052 :   for(i=2;i<l;i++)
      30        8470 :     if (!gequal(gel(v,i), w)) return 0;
      31        1582 :   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        2513 : vec_is1to1(GEN v)
      38             : {
      39        2513 :   long i, j, l = lg(v);
      40       16121 :   for (i=1; i<l; i++)
      41             :   {
      42       13748 :     GEN w = gel(v,i);
      43       90629 :     for(j=i+1; j<l; j++)
      44       77021 :       if (gequal(gel(v,j), w)) return 0;
      45             :   }
      46        2373 :   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     1524240 : vecsmall_sortspec(GEN v, long n, GEN w)
      68             : {
      69     1524240 :   pari_sp ltop=avma;
      70     1524240 :   long nx=n>>1, ny=n-nx;
      71             :   long m, ix, iy;
      72             :   GEN x, y;
      73     1524240 :   if (n<=2)
      74             :   {
      75      934403 :     if (n==1)
      76      331004 :       w[0]=v[0];
      77      603399 :     else if (n==2)
      78             :     {
      79      603400 :       long v0=v[0], v1=v[1];
      80      603400 :       if (v0<=v1) { w[0]=v0; w[1]=v1; }
      81      293628 :       else        { w[0]=v1; w[1]=v0; }
      82             :     }
      83     2458649 :     return;
      84             :   }
      85      589837 :   x=new_chunk(nx); y=new_chunk(ny);
      86      589840 :   vecsmall_sortspec(v,nx,x);
      87      589842 :   vecsmall_sortspec(v+nx,ny,y);
      88     3745124 :   for (m=0, ix=0, iy=0; ix<nx && iy<ny; )
      89     2565438 :     if (x[ix]<=y[iy])
      90     1093141 :       w[m++]=x[ix++];
      91             :     else
      92     1472297 :       w[m++]=y[iy++];
      93      589843 :   for(;ix<nx;) w[m++]=x[ix++];
      94      589843 :   for(;iy<ny;) w[m++]=y[iy++];
      95      589843 :   avma=ltop;
      96             : }
      97             : 
      98             : /*in place sort.*/
      99             : void
     100     4685541 : vecsmall_sort(GEN V)
     101             : {
     102     4685541 :   long l = lg(V)-1;
     103     9371085 :   if (l<=1) return;
     104      344560 :   vecsmall_sortspec(V+1,l,V+1);
     105             : }
     106             : 
     107             : /* cf gen_sortspec */
     108             : static GEN
     109    18115308 : vecsmall_indexsortspec(GEN v, long n)
     110             : {
     111             :   long nx, ny, m, ix, iy;
     112             :   GEN x, y, w;
     113    18115308 :   switch(n)
     114             :   {
     115       42939 :     case 1: return mkvecsmall(1);
     116     5396287 :     case 2: return (v[1] <= v[2])? mkvecsmall2(1,2): mkvecsmall2(2,1);
     117             :     case 3:
     118     4775614 :       if (v[1] <= v[2]) {
     119     4010505 :         if (v[2] <= v[3]) return mkvecsmall3(1,2,3);
     120     1841652 :         return (v[1] <= v[3])? mkvecsmall3(1,3,2)
     121      920826 :                              : mkvecsmall3(3,1,2);
     122             :       } else {
     123      765109 :         if (v[1] <= v[3]) return mkvecsmall3(2,1,3);
     124     1076156 :         return (v[2] <= v[3])? mkvecsmall3(2,3,1)
     125      538078 :                              : mkvecsmall3(3,2,1);
     126             :       }
     127             :   }
     128     7900468 :   nx = n>>1; ny = n-nx;
     129     7900468 :   w = cgetg(n+1,t_VECSMALL);
     130     7900468 :   x = vecsmall_indexsortspec(v,nx);
     131     7900468 :   y = vecsmall_indexsortspec(v+nx,ny);
     132   133610732 :   for (m=1, ix=1, iy=1; ix<=nx && iy<=ny; )
     133   117809796 :     if (v[x[ix]] <= v[y[iy]+nx])
     134    75547461 :       w[m++] = x[ix++];
     135             :     else
     136    42262335 :       w[m++] = y[iy++]+nx;
     137     7900468 :   for(;ix<=nx;) w[m++] = x[ix++];
     138     7900468 :   for(;iy<=ny;) w[m++] = y[iy++]+nx;
     139     7900468 :   avma = (pari_sp)w; return w;
     140             : }
     141             : 
     142             : /*indirect sort.*/
     143             : GEN
     144     2314372 : vecsmall_indexsort(GEN V)
     145             : {
     146     2314372 :   long l=lg(V)-1;
     147     2314372 :   if (l==0) return cgetg(1, t_VECSMALL);
     148     2314372 :   return vecsmall_indexsortspec(V,l);
     149             : }
     150             : 
     151             : /* assume V sorted */
     152             : GEN
     153        1341 : vecsmall_uniq_sorted(GEN V)
     154             : {
     155             :   GEN W;
     156        1341 :   long i,j, l = lg(V);
     157        1341 :   if (l == 1) return vecsmall_copy(V);
     158        1341 :   W = cgetg(l,t_VECSMALL);
     159        1341 :   W[1] = V[1];
     160        2602 :   for(i=j=2; i<l; i++)
     161        1261 :     if (V[i] != W[j-1]) W[j++] = V[i];
     162        1341 :   stackdummy((pari_sp)(W + l), (pari_sp)(W + j));
     163        1341 :   setlg(W, j); return W;
     164             : }
     165             : 
     166             : GEN
     167        1341 : vecsmall_uniq(GEN V)
     168             : {
     169        1341 :   pari_sp av = avma;
     170        1341 :   V = zv_copy(V); vecsmall_sort(V);
     171        1341 :   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       13715 : vecsmall_duplicate(GEN x)
     187             : {
     188       13715 :   pari_sp av=avma;
     189       13715 :   GEN p=vecsmall_indexsort(x);
     190       13715 :   long k,i,r=0,l=lg(x);
     191       13715 :   if (l==1) return 0;
     192       17766 :   for (k=x[p[1]],i=2; i<l; k=x[p[i++]])
     193        4051 :     if (x[p[i]] == k) { r=p[i]; break; }
     194       13715 :   avma=av;
     195       13715 :   return r;
     196             : }
     197             : 
     198             : /*************************************************************************/
     199             : /**                                                                     **/
     200             : /**             Routines for handling vectors of VECSMALL               **/
     201             : /**                                                                     **/
     202             : /*************************************************************************/
     203             : 
     204             : GEN
     205      113302 : vecvecsmall_sort(GEN x)
     206             : {
     207      113302 :   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       90440 : vecperm_orbits_i(GEN v, long n)
     262             : {
     263       90440 :   long mj = 1, k, l, m;
     264       90440 :   GEN cy, cycle = cgetg(n+1, t_VEC), bit = zero_F2v(n);
     265     1063405 :   for (k = 1, l = 1; k <= n;)
     266             :   {
     267      882525 :     for (  ; F2v_coeff(bit,mj); mj++) /*empty*/;
     268      882525 :     cy = cgetg(n+1, t_VECSMALL);
     269      882525 :     m = 1; k++; cy[m++] = mj;
     270      882525 :     F2v_set(bit, mj++);
     271             :     for(;;)
     272             :     {
     273     1609211 :       long o, mold = m;
     274     3221418 :       for (o = 1; o < lg(v); o++)
     275             :       {
     276     1612207 :         GEN vo = gel(v,o);
     277             :         long p;
     278     7177541 :         for (p = 1; p < m; p++) /* m increases! */
     279             :         {
     280     5565334 :           long j = vo[ cy[p] ];
     281     5565334 :           if (!F2v_coeff(bit,j)) cy[m++] = j;
     282     5565334 :           F2v_set(bit,j);
     283             :         }
     284             :       }
     285     1609211 :       if (m == mold) break;
     286      726686 :       k += m - mold;
     287      726686 :     }
     288      882525 :     setlg(cy, m); gel(cycle,l++) = cy;
     289             :   }
     290       90440 :   setlg(cycle, l); return cycle;
     291             : }
     292             : /* memory clean version */
     293             : GEN
     294         798 : vecperm_orbits(GEN v, long n)
     295             : {
     296         798 :   pari_sp av = avma;
     297         798 :   return gerepilecopy(av, vecperm_orbits_i(v, n));
     298             : }
     299             : 
     300             : /* Compute the cyclic decomposition of a permutation */
     301             : GEN
     302       12654 : perm_cycles(GEN v)
     303             : {
     304       12654 :   pari_sp av = avma;
     305       12654 :   return gerepilecopy(av, vecperm_orbits_i(mkvec(v), lg(v)-1));
     306             : }
     307             : 
     308             : /* Output the order of p */
     309             : long
     310       76988 : perm_order(GEN v)
     311             : {
     312       76988 :   pari_sp ltop = avma;
     313       76988 :   GEN c = vecperm_orbits_i(mkvec(v), lg(v)-1);
     314             :   long i, d;
     315       76988 :   for(i=1, d=1; i<lg(c); i++) d = clcm(d, lg(gel(c,i))-1);
     316       76988 :   avma = ltop; return d;
     317             : }
     318             : 
     319             : static long
     320          84 : isperm(GEN v)
     321             : {
     322          84 :   long i, l = lg(v)-1;
     323          84 :   if (typ(v)!=t_VECSMALL) return 0;
     324        1092 :   for (i=1; i<=l; i++)
     325        1008 :     if (v[i]<1 || v[i]>l) return 0;
     326          84 :   return 1;
     327             : }
     328             : 
     329             : long
     330          84 : permorder(GEN v)
     331             : {
     332          84 :   if (!isperm(v)) pari_err_TYPE("permorder",v);
     333          84 :   return perm_order(v);
     334             : }
     335             : 
     336             : GEN
     337        1708 : cyc_pow(GEN cyc, long exp)
     338             : {
     339             :   long i, j, k, l, r;
     340             :   GEN c;
     341        5936 :   for (r = j = 1; j < lg(cyc); j++)
     342             :   {
     343        4228 :     long n = lg(gel(cyc,j)) - 1;
     344        4228 :     r += cgcd(n, exp);
     345             :   }
     346        1708 :   c = cgetg(r, t_VEC);
     347        5936 :   for (r = j = 1; j < lg(cyc); j++)
     348             :   {
     349        4228 :     GEN v = gel(cyc,j);
     350        4228 :     long n = lg(v) - 1, e = smodss(exp,n), g = (long)ugcd(n, e), m = n / g;
     351        9268 :     for (i = 0; i < g; i++)
     352             :     {
     353        5040 :       GEN p = cgetg(m+1, t_VECSMALL);
     354        5040 :       gel(c,r++) = p;
     355       17073 :       for (k = 1, l = i; k <= m; k++)
     356             :       {
     357       12033 :         p[k] = v[l+1];
     358       12033 :         l += e; if (l >= n) l -= n;
     359             :       }
     360             :     }
     361             :   }
     362        1708 :   return c;
     363             : }
     364             : 
     365             : /* Compute the power of a permutation given by product of cycles
     366             :  * Ouput a perm, not a cyc */
     367             : GEN
     368        9063 : cyc_pow_perm(GEN cyc, long exp)
     369             : {
     370             :   long e, j, k, l, n;
     371             :   GEN p;
     372        9063 :   for (n = 0, j = 1; j < lg(cyc); j++) n += lg(gel(cyc,j))-1;
     373        9063 :   p = cgetg(n + 1, t_VECSMALL);
     374       60234 :   for (j = 1; j < lg(cyc); j++)
     375             :   {
     376       51171 :     GEN v = gel(cyc,j);
     377       51171 :     n = lg(v) - 1; e = smodss(exp, n);
     378      196395 :     for (k = 1, l = e; k <= n; k++)
     379             :     {
     380      145224 :       p[v[k]] = v[l+1];
     381      145224 :       if (++l == n) l = 0;
     382             :     }
     383             :   }
     384        9063 :   return p;
     385             : }
     386             : 
     387             : /* Compute the power of a permutation.
     388             :  * TODO: make it more clever for small exp */
     389             : GEN
     390        9063 : perm_pow(GEN perm, long exp)
     391             : {
     392        9063 :   return cyc_pow_perm(perm_cycles(perm), exp);
     393             : }
     394             : 
     395             : GEN
     396          21 : perm_to_GAP(GEN p)
     397             : {
     398          21 :   pari_sp ltop=avma;
     399             :   GEN gap;
     400             :   GEN x;
     401             :   long i;
     402          21 :   long nb, c=0;
     403             :   char *s;
     404             :   long sz;
     405          21 :   long lp=lg(p)-1;
     406          21 :   if (typ(p) != t_VECSMALL)  pari_err_TYPE("perm_to_GAP",p);
     407          21 :   x = perm_cycles(p);
     408          21 :   sz = (long) ((bfffo(lp)+1) * LOG10_2 + 1);
     409             :   /*Dry run*/
     410         133 :   for (i = 1, nb = 1; i < lg(x); ++i)
     411             :   {
     412         112 :     GEN z = gel(x,i);
     413         112 :     long lz = lg(z)-1;
     414         112 :     nb += 1+lz*(sz+2);
     415             :   }
     416          21 :   nb++;
     417             :   /*Real run*/
     418          21 :   gap = cgetg(nchar2nlong(nb) + 1, t_STR);
     419          21 :   s = GSTR(gap);
     420         133 :   for (i = 1; i < lg(x); ++i)
     421             :   {
     422             :     long j;
     423         112 :     GEN z = gel(x,i);
     424         112 :     if (lg(z) > 2)
     425             :     {
     426         112 :       s[c++] = '(';
     427         364 :       for (j = 1; j < lg(z); ++j)
     428             :       {
     429         252 :         if (j > 1)
     430             :         {
     431         140 :           s[c++] = ','; s[c++] = ' ';
     432             :         }
     433         252 :         sprintf(s+c,"%ld",z[j]);
     434         252 :         while(s[c++]) /* empty */;
     435         252 :         c--;
     436             :       }
     437         112 :       s[c++] = ')';
     438             :     }
     439             :   }
     440          21 :   if (!c) { s[c++]='('; s[c++]=')'; }
     441          21 :   s[c] = '\0';
     442          21 :   return gerepileupto(ltop,gap);
     443             : }
     444             : 
     445             : int
     446      509340 : perm_commute(GEN s, GEN t)
     447             : {
     448      509340 :   long i, l = lg(t);
     449    35717452 :   for (i = 1; i < l; i++)
     450    35221888 :     if (t[ s[i] ] != s[ t[i] ]) return 0;
     451      495564 :   return 1;
     452             : }
     453             : 
     454             : /*************************************************************************/
     455             : /**                                                                     **/
     456             : /**                  Routines for handling groups                       **/
     457             : /**                                                                     **/
     458             : /*************************************************************************/
     459             : /* A Group is a t_VEC [gen,orders]
     460             :  * gen (vecvecsmall): list of generators given by permutations
     461             :  * orders (vecsmall): relatives orders of generators. */
     462      384877 : INLINE GEN grp_get_gen(GEN G) { return gel(G,1); }
     463      659219 : INLINE GEN grp_get_ord(GEN G) { return gel(G,2); }
     464             : 
     465             : /* A Quotient Group is a t_VEC [gen,coset]
     466             :  * gen (vecvecsmall): coset generators
     467             :  * coset (vecsmall): gen[coset[p[1]]] generate the p-coset.
     468             :  */
     469       65649 : INLINE GEN quo_get_gen(GEN C) { return gel(C,1); }
     470        9959 : INLINE GEN quo_get_coset(GEN C) { return gel(C,2); }
     471             : 
     472             : static GEN
     473       26723 : trivialsubgroups(void)
     474       26723 : { GEN L = cgetg(2, t_VEC); gel(L,1) = trivialgroup(); return L; }
     475             : 
     476             : /* Compute the order of p modulo the group given by a set */
     477             : long
     478      110138 : perm_relorder(GEN p, GEN set)
     479             : {
     480      110138 :   pari_sp ltop = avma;
     481      110138 :   long n = 1;
     482      110138 :   long q = p[1];
     483      110138 :   while (!F2v_coeff(set,q)) { q = p[q]; n++; }
     484      110138 :   avma = ltop; return n;
     485             : }
     486             : 
     487             : GEN
     488        6815 : perm_generate(GEN S, GEN H, long o)
     489             : {
     490        6815 :   long i, n = lg(H)-1;
     491        6815 :   GEN L = cgetg(n*o + 1, t_VEC);
     492        6815 :   for(i=1; i<=n;     i++) gel(L,i) = vecsmall_copy(gel(H,i));
     493        6815 :   for(   ; i <= n*o; i++) gel(L,i) = perm_mul(gel(L,i-n), S);
     494        6815 :   return L;
     495             : }
     496             : 
     497             : /*Return the order (cardinality) of a group */
     498             : long
     499      284591 : group_order(GEN G)
     500             : {
     501      284591 :   return zv_prod(grp_get_ord(G));
     502             : }
     503             : 
     504             : /* G being a subgroup of S_n, output n */
     505             : long
     506        5407 : group_domain(GEN G)
     507             : {
     508        5407 :   GEN gen = grp_get_gen(G);
     509        5407 :   if (lg(gen) < 2) pari_err_DOMAIN("group_domain", "#G", "=", gen_1,G);
     510        5407 :   return lg(gel(gen,1)) - 1;
     511             : }
     512             : 
     513             : /*Left coset of g mod G: gG*/
     514             : GEN
     515      125996 : group_leftcoset(GEN G, GEN g)
     516             : {
     517      125996 :   GEN gen = grp_get_gen(G), ord = grp_get_ord(G);
     518      125996 :   GEN res = cgetg(group_order(G)+1, t_VEC);
     519             :   long i, j, k;
     520      125996 :   gel(res,1) = vecsmall_copy(g);
     521      125996 :   k = 1;
     522      235482 :   for (i = 1; i < lg(gen); i++)
     523             :   {
     524      109486 :     long c = k * (ord[i] - 1);
     525      109486 :     for (j = 1; j <= c; j++) gel(res,++k) = perm_mul(gel(res,j), gel(gen,i));
     526             :   }
     527      125996 :   return res;
     528             : }
     529             : /*Right coset of g mod G: Gg*/
     530             : GEN
     531       56602 : group_rightcoset(GEN G, GEN g)
     532             : {
     533       56602 :   GEN gen = grp_get_gen(G), ord = grp_get_ord(G);
     534       56602 :   GEN res = cgetg(group_order(G)+1, t_VEC);
     535             :   long i, j, k;
     536       56602 :   gel(res,1) = vecsmall_copy(g);
     537       56602 :   k = 1;
     538       96582 :   for (i = 1; i < lg(gen); i++)
     539             :   {
     540       39980 :     long c = k * (ord[i] - 1);
     541       39980 :     for (j = 1; j <= c; j++) gel(res,++k) = perm_mul(gel(gen,i), gel(res,j));
     542             :   }
     543       56602 :   return res;
     544             : }
     545             : /*Elements of a group from the generators, cf group_leftcoset*/
     546             : GEN
     547       56617 : group_elts(GEN G, long n)
     548             : {
     549       56617 :   GEN gen = grp_get_gen(G), ord = grp_get_ord(G);
     550       56617 :   GEN res = cgetg(group_order(G)+1, t_VEC);
     551             :   long i, j, k;
     552       56617 :   gel(res,1) = identity_perm(n);
     553       56617 :   k = 1;
     554      112318 :   for (i = 1; i < lg(gen); i++)
     555             :   {
     556       55701 :     long c = k * (ord[i] - 1);
     557             :     /* j = 1, use res[1] = identity */
     558       55701 :     gel(res,++k) = vecsmall_copy(gel(gen,i));
     559       55701 :     for (j = 2; j <= c; j++) gel(res,++k) = perm_mul(gel(res,j), gel(gen,i));
     560             :   }
     561       56617 :   return res;
     562             : }
     563             : 
     564             : GEN
     565       10607 : groupelts_set(GEN elts, long n)
     566             : {
     567       10607 :   GEN res = zero_F2v(n);
     568       10607 :   long i, l = lg(elts);
     569       57358 :   for(i=1; i<l; i++)
     570       46751 :     F2v_set(res,mael(elts,i,1));
     571       10607 :   return res;
     572             : }
     573             : 
     574             : /*Elements of a group from the generators, returned as a set (bitmap)*/
     575             : GEN
     576       49558 : group_set(GEN G, long n)
     577             : {
     578       49558 :   GEN res = zero_F2v(n);
     579       49558 :   pari_sp av = avma;
     580       49558 :   GEN elts = group_elts(G, n);
     581       49558 :   long i, l = lg(elts);
     582      155274 :   for(i=1; i<l; i++)
     583      105716 :     F2v_set(res,mael(elts,i,1));
     584       49558 :   avma = av;
     585       49558 :   return res;
     586             : }
     587             : 
     588             : static int
     589        1267 : sgcmp(GEN a, GEN b) { return vecsmall_lexcmp(gel(a,1),gel(b,1)); }
     590             : 
     591             : GEN
     592          14 : subgroups_tableset(GEN S, long n)
     593             : {
     594          14 :   long i, l = lg(S);
     595          14 :   GEN  v = cgetg(l, t_VEC);
     596         322 :   for(i=1; i<l; i++)
     597         308 :     gel(v,i) = mkvec2(group_set(gel(S,i), n), mkvecsmall(i));
     598          14 :   gen_sort_inplace(v,(void*)sgcmp,cmp_nodata, NULL);
     599          14 :   return v;
     600             : }
     601             : 
     602             : long
     603          63 : tableset_find_index(GEN tbl, GEN set)
     604             : {
     605          63 :   long i = tablesearch(tbl,mkvec2(set,mkvecsmall(0)),sgcmp);
     606          63 :   if (!i) return 0;
     607          63 :   return mael3(tbl,i,2,1);
     608             : }
     609             : 
     610             : GEN
     611       26723 : trivialgroup(void) { retmkvec2(cgetg(1,t_VEC), cgetg(1,t_VECSMALL)); }
     612             : /*Cyclic group generated by g of order s*/
     613             : GEN
     614        3939 : cyclicgroup(GEN g, long s)
     615        3939 : { retmkvec2(mkvec( vecsmall_copy(g) ),
     616             :             mkvecsmall(s)); }
     617             : /*Return the group generated by g1,g2 of relative orders s1,s2*/
     618             : GEN
     619          77 : dicyclicgroup(GEN g1, GEN g2, long s1, long s2)
     620          77 : { retmkvec2( mkvec2(vecsmall_copy(g1), vecsmall_copy(g2)),
     621             :              mkvecsmall2(s1, s2) ); }
     622             : 
     623             : /* return the quotient map G --> G/H */
     624             : /*The ouput is [gen,hash]*/
     625             : /* gen (vecvecsmall): coset generators
     626             :  * coset (vecsmall): vecsmall of coset number) */
     627             : GEN
     628        3799 : group_quotient(GEN G, GEN H)
     629             : {
     630        3799 :   pari_sp ltop = avma;
     631             :   GEN  p2, p3;
     632        3799 :   long i, j, a = 1;
     633        3799 :   long n = group_domain(G), o = group_order(H);
     634        3799 :   GEN  elt = group_elts(G,n), el;
     635        3799 :   long le = lg(elt)-1;
     636        3799 :   GEN used = zero_F2v(le+1);
     637        3799 :   long l = le/o;
     638        3799 :   p2 = cgetg(l+1, t_VEC);
     639        3799 :   p3 = zero_zv(n);
     640        3799 :   el = zero_zv(n);
     641       59817 :   for (i = 1; i<=le; i++)
     642       56018 :     el[mael(elt,i,1)]=i;
     643       31225 :   for (i = 1; i <= l; ++i)
     644             :   {
     645             :     GEN V;
     646       27433 :     while(F2v_coeff(used,a)) a++;
     647       27433 :     V = group_leftcoset(H,gel(elt,a));
     648       27433 :     gel(p2,i) = gel(V,1);
     649       83346 :     for(j=1;j<lg(V);j++)
     650             :     {
     651       55920 :       long b = el[mael(V,j,1)];
     652       55920 :       if (b==0) pari_err_IMPL("group_quotient for a non-WSS group");
     653       55913 :       F2v_set(used,b);
     654             :     }
     655       83332 :     for (j = 1; j <= o; j++)
     656       55906 :       p3[mael(V, j, 1)] = i;
     657             :   }
     658        3792 :   return gerepilecopy(ltop,mkvec2(p2,p3));
     659             : }
     660             : 
     661             : /*Compute the image of a permutation by a quotient map.*/
     662             : GEN
     663        9959 : quotient_perm(GEN C, GEN p)
     664             : {
     665        9959 :   GEN gen = quo_get_gen(C);
     666        9959 :   GEN coset = quo_get_coset(C);
     667        9959 :   long j, l = lg(gen);
     668        9959 :   GEN p3 = cgetg(l, t_VECSMALL);
     669      105734 :   for (j = 1; j < l; ++j)
     670             :   {
     671       95775 :     p3[j] = coset[p[mael(gen,j,1)]];
     672       95775 :     if (p3[j]==0) pari_err_IMPL("quotient_perm for a non-WSS group");
     673             :   }
     674        9959 :   return p3;
     675             : }
     676             : 
     677             : /* H is a subgroup of G, C is the quotient map G --> G/H
     678             :  *
     679             :  * Lift a subgroup S of G/H to a subgroup of G containing H */
     680             : GEN
     681       25949 : quotient_subgroup_lift(GEN C, GEN H, GEN S)
     682             : {
     683       25949 :   GEN genH = grp_get_gen(H);
     684       25949 :   GEN genS = grp_get_gen(S);
     685       25949 :   GEN genC = quo_get_gen(C);
     686       25949 :   long l1 = lg(genH)-1;
     687       25949 :   long l2 = lg(genS)-1, j;
     688       25949 :   GEN p1 = cgetg(3, t_VEC), L = cgetg(l1+l2+1, t_VEC);
     689       25949 :   for (j = 1; j <= l1; ++j) gel(L,j) = gel(genH,j);
     690       25949 :   for (j = 1; j <= l2; ++j) gel(L,l1+j) = gel(genC, mael(genS,j,1));
     691       25949 :   gel(p1,1) = L;
     692       25949 :   gel(p1,2) = vecsmall_concat(grp_get_ord(H), grp_get_ord(S));
     693       25949 :   return p1;
     694             : }
     695             : 
     696             : /* Let G a group and C a quotient map G --> G/H
     697             :  * Assume H is normal, return the group G/H */
     698             : GEN
     699        3792 : quotient_group(GEN C, GEN G)
     700             : {
     701        3792 :   pari_sp ltop = avma;
     702             :   GEN Qgen, Qord, Qelt, Qset, Q;
     703        3792 :   GEN Cgen = quo_get_gen(C);
     704        3792 :   GEN Ggen = grp_get_gen(G);
     705        3792 :   long i,j, n = lg(Cgen)-1, l = lg(Ggen);
     706        3792 :   Qord = cgetg(l, t_VECSMALL);
     707        3792 :   Qgen = cgetg(l, t_VEC);
     708        3792 :   Qelt = mkvec(identity_perm(n));
     709        3792 :   Qset = groupelts_set(Qelt, n);
     710       13751 :   for (i = 1, j = 1; i < l; ++i)
     711             :   {
     712        9959 :     GEN  g = quotient_perm(C, gel(Ggen,i));
     713        9959 :     long o = perm_relorder(g, Qset);
     714        9959 :     gel(Qgen,j) = g;
     715        9959 :     Qord[j] = o;
     716        9959 :     if (o != 1)
     717             :     {
     718        6815 :       Qelt = perm_generate(g, Qelt, o);
     719        6815 :       Qset = groupelts_set(Qelt, n);
     720        6815 :       j++;
     721             :     }
     722             :   }
     723        3792 :   setlg(Qgen,j);
     724        3792 :   setlg(Qord,j); Q = mkvec2(Qgen, Qord);
     725        3792 :   return gerepilecopy(ltop,Q);
     726             : }
     727             : 
     728             : /* Return 1 if g normalizes N, 0 otherwise */
     729             : long
     730       56602 : group_perm_normalize(GEN N, GEN g)
     731             : {
     732       56602 :   pari_sp ltop = avma;
     733       56602 :   long r = gequal(vecvecsmall_sort(group_leftcoset(N, g)),
     734             :                   vecvecsmall_sort(group_rightcoset(N, g)));
     735       56602 :   avma = ltop; return r;
     736             : }
     737             : 
     738             : /* L is a list of subgroups, C is a coset and r a relative order.*/
     739             : static GEN
     740       41961 : liftlistsubgroups(GEN L, GEN C, long r)
     741             : {
     742       41961 :   pari_sp ltop = avma;
     743       41961 :   long c = lg(C)-1, l = lg(L)-1, n = lg(gel(C,1))-1, i, k;
     744             :   GEN R;
     745       41961 :   if (!l) return cgetg(1,t_VEC);
     746       34733 :   R = cgetg(l*c+1, t_VEC);
     747       83962 :   for (i = 1, k = 1; i <= l; ++i)
     748             :   {
     749       49229 :     GEN S = gel(L,i), Selt = group_set(S,n);
     750       49229 :     GEN gen = grp_get_gen(S);
     751       49229 :     GEN ord = grp_get_ord(S);
     752             :     long j;
     753      149240 :     for (j = 1; j <= c; ++j)
     754             :     {
     755      100011 :       GEN p = gel(C,j);
     756      100011 :       if (perm_relorder(p, Selt) == r && group_perm_normalize(S, p))
     757       54397 :         gel(R,k++) = mkvec2(vec_append(gen, p),
     758             :                             vecsmall_append(ord, r));
     759             :     }
     760             :   }
     761       34733 :   setlg(R, k);
     762       34733 :   return gerepilecopy(ltop, R);
     763             : }
     764             : 
     765             : /* H is a normal subgroup, C is the quotient map G -->G/H,
     766             :  * S is a subgroup of G/H, and G is embedded in Sym(l)
     767             :  * Return all the subgroups K of G such that
     768             :  * S= K mod H and K inter H={1} */
     769             : static GEN
     770       25949 : liftsubgroup(GEN C, GEN H, GEN S)
     771             : {
     772       25949 :   pari_sp ltop = avma;
     773       25949 :   GEN V = trivialsubgroups();
     774       25949 :   GEN Sgen = grp_get_gen(S);
     775       25949 :   GEN Sord = grp_get_ord(S);
     776       25949 :   GEN Cgen = quo_get_gen(C);
     777       25949 :   long n = lg(Sgen), i;
     778       67910 :   for (i = 1; i < n; ++i)
     779             :   { /*loop over generators of S*/
     780       41961 :     GEN W = group_leftcoset(H, gel(Cgen, mael(Sgen, i, 1)));
     781       41961 :     V = liftlistsubgroups(V, W, Sord[i]);
     782             :   }
     783       25949 :   return gerepilecopy(ltop,V);
     784             : }
     785             : 
     786             : /* 1:A4 2:S4 0: other */
     787             : long
     788        3617 : group_isA4S4(GEN G)
     789             : {
     790        3617 :   GEN elt = grp_get_gen(G);
     791        3617 :   GEN ord = grp_get_ord(G);
     792        3617 :   long n = lg(ord);
     793        3617 :   if (n != 4 && n != 5) return 0;
     794        1260 :   if (ord[1]!=2 || ord[2]!=2 || ord[3]!=3) return 0;
     795          14 :   if (perm_commute(gel(elt,1),gel(elt,3))) return 0;
     796          14 :   if (n==4) return 1;
     797           7 :   if (ord[4]!=2) return 0;
     798           7 :   if (perm_commute(gel(elt,3),gel(elt,4))) return 0;
     799           7 :   return 2;
     800             : }
     801             : /* compute all the subgroups of a group G */
     802             : GEN
     803        4391 : group_subgroups(GEN G)
     804             : {
     805        4391 :   pari_sp ltop = avma;
     806             :   GEN p1, H, C, Q, M, sg1, sg2, sg3;
     807        4391 :   GEN gen = grp_get_gen(G);
     808        4391 :   GEN ord = grp_get_ord(G);
     809        4391 :   long lM, i, j, n = lg(gen);
     810        4391 :   if (n == 1) return trivialsubgroups();
     811        3617 :   if (group_isA4S4(G))
     812             :   {
     813          14 :     GEN s = gel(gen,1);       /*s = (1,2)(3,4) */
     814          14 :     GEN t = gel(gen,2);       /*t = (1,3)(2,4) */
     815          14 :     GEN st = perm_mul(s, t); /*st = (1,4)(2,3) */
     816          14 :     H = dicyclicgroup(s, t, 2, 2);
     817             :     /* sg3 is the list of subgroups intersecting only partially with H*/
     818          14 :     sg3 = cgetg((n==4)?4: 10, t_VEC);
     819          14 :     gel(sg3,1) = cyclicgroup(s, 2);
     820          14 :     gel(sg3,2) = cyclicgroup(t, 2);
     821          14 :     gel(sg3,3) = cyclicgroup(st, 2);
     822          14 :     if (n==5)
     823             :     {
     824           7 :       GEN u = gel(gen,3);
     825           7 :       GEN v = gel(gen,4), w, u2;
     826           7 :       if (zv_equal(perm_conj(u,s), t)) /*u=(2,3,4)*/
     827           7 :         u2 = perm_mul(u,u);
     828             :       else
     829             :       {
     830           0 :         u2 = u;
     831           0 :         u = perm_mul(u,u);
     832             :       }
     833           7 :       if (perm_order(v)==2)
     834             :       {
     835           7 :         if (!perm_commute(s,v)) /*v=(1,2)*/
     836             :         {
     837           0 :           v = perm_conj(u,v);
     838           0 :           if (!perm_commute(s,v)) v = perm_conj(u,v);
     839             :         }
     840           7 :         w = perm_mul(v,t); /*w=(1,4,2,3)*/
     841             :       }
     842             :       else
     843             :       {
     844           0 :         w = v;
     845           0 :         if (!zv_equal(perm_mul(w,w), s)) /*w=(1,4,2,3)*/
     846             :         {
     847           0 :           w = perm_conj(u,w);
     848           0 :           if (!zv_equal(perm_mul(w,w), s)) w = perm_conj(u,w);
     849             :         }
     850           0 :         v = perm_mul(w,t); /*v=(1,2)*/
     851             :       }
     852           7 :       gel(sg3,4) = dicyclicgroup(s,v,2,2);
     853           7 :       gel(sg3,5) = dicyclicgroup(t,perm_conj(u,v),2,2);
     854           7 :       gel(sg3,6) = dicyclicgroup(st,perm_conj(u2,v),2,2);
     855           7 :       gel(sg3,7) = dicyclicgroup(s,w,2,2);
     856           7 :       gel(sg3,8) = dicyclicgroup(t,perm_conj(u,w),2,2);
     857           7 :       gel(sg3,9) = dicyclicgroup(st,perm_conj(u2,w),2,2);
     858             :     }
     859             :   }
     860             :   else
     861             :   {
     862        3603 :     long osig = mael(factoru(ord[1]), 1, 1);
     863        3603 :     GEN sig = perm_pow(gel(gen,1), ord[1]/osig);
     864        3603 :     H = cyclicgroup(sig,osig);
     865        3603 :     sg3 = NULL;
     866             :   }
     867        3617 :   C = group_quotient(G,H);
     868        3610 :   Q = quotient_group(C,G);
     869        3610 :   M = group_subgroups(Q); lM = lg(M);
     870             :   /* sg1 is the list of subgroups containing H*/
     871        3603 :   sg1 = cgetg(lM, t_VEC);
     872        3603 :   for (i = 1; i < lM; ++i) gel(sg1,i) = quotient_subgroup_lift(C,H,gel(M,i));
     873             :   /*sg2 is a list of lists of subgroups not intersecting with H*/
     874        3603 :   sg2 = cgetg(lM, t_VEC);
     875             :   /* Loop over all subgroups of G/H */
     876        3603 :   for (j = 1; j < lM; ++j) gel(sg2,j) = liftsubgroup(C, H, gel(M,j));
     877        3603 :   p1 = gconcat(sg1, shallowconcat1(sg2));
     878        3603 :   if (sg3)
     879             :   {
     880          14 :     p1 = gconcat(p1, sg3);
     881          14 :     if (n==5) /*ensure that the D4 subgroups of S4 are in supersolvable format*/
     882          28 :       for(j = 3; j <= 5; j++)
     883             :       {
     884          21 :         GEN c = gmael(p1,j,1);
     885          21 :         if (!perm_commute(gel(c,1),gel(c,3)))
     886             :         {
     887          14 :           if (perm_commute(gel(c,2),gel(c,3))) { swap(gel(c,1), gel(c,2)); }
     888             :           else
     889           7 :             perm_mul_inplace2(gel(c,2), gel(c,1));
     890             :         }
     891             :       }
     892             :   }
     893        3603 :   return gerepileupto(ltop,p1);
     894             : }
     895             : 
     896             : /*return 1 if G is abelian, else 0*/
     897             : long
     898         910 : group_isabelian(GEN G)
     899             : {
     900         910 :   GEN g = grp_get_gen(G);
     901         910 :   long i, j, n = lg(g);
     902        1463 :   for(i=2; i<n; i++)
     903        1498 :     for(j=1; j<i; j++)
     904         945 :       if (!perm_commute(gel(g,i), gel(g,j))) return 0;
     905         763 :   return 1;
     906             : }
     907             : 
     908             : /*If G is abelian, return its HNF matrix*/
     909             : GEN
     910         329 : group_abelianHNF(GEN G, GEN S)
     911             : {
     912         329 :   GEN M, g = grp_get_gen(G), o = grp_get_ord(G);
     913         329 :   long i, j, k, n = lg(g);
     914         329 :   if (!group_isabelian(G)) return NULL;
     915         259 :   if (n==1) return cgetg(1,t_MAT);
     916         245 :   if (!S) S = group_elts(G, group_domain(G));
     917         245 :   M = cgetg(n,t_MAT);
     918         868 :   for(i=1; i<n; i++)
     919             :   {
     920         623 :     GEN P, C = cgetg(n,t_COL);
     921         623 :     pari_sp av = avma;
     922         623 :     gel(M,i) = C;
     923         623 :     P = perm_pow(gel(g,i), o[i]);
     924         903 :     for(j=1; j<lg(S); j++)
     925         903 :       if (zv_equal(P, gel(S,j))) break;
     926         623 :     avma = av;
     927         623 :     if (j==lg(S)) pari_err_BUG("galoisisabelian [inconsistent group]");
     928         623 :     j--;
     929        1162 :     for(k=1; k<i; k++)
     930             :     {
     931         539 :       long q = j / o[k];
     932         539 :       gel(C,k) = stoi(j - q*o[k]);
     933         539 :       j = q;
     934             :     }
     935         623 :     gel(C,k) = stoi(o[i]);
     936         623 :     for (k++; k<n; k++) gel(C,k) = gen_0;
     937             :   }
     938         245 :   return M;
     939             : }
     940             : 
     941             : /*If G is abelian, return its abstract SNF matrix*/
     942             : GEN
     943         280 : group_abelianSNF(GEN G, GEN L)
     944             : {
     945         280 :   pari_sp ltop = avma;
     946         280 :   GEN H = group_abelianHNF(G,L);
     947         280 :   if (!H) return NULL;
     948         210 :   return gerepileupto(ltop, smithclean( ZM_snf(H) ));
     949             : }
     950             : 
     951             : GEN
     952         168 : abelian_group(GEN v)
     953             : {
     954         168 :   long card = zv_prod(v), i, d = 1, l = lg(v);
     955         168 :   GEN G = cgetg(3,t_VEC), gen = cgetg(l,t_VEC);
     956         168 :   gel(G,1) = gen;
     957         168 :   gel(G,2) = vecsmall_copy(v);
     958         378 :   for(i=1; i<l; i++)
     959             :   {
     960         210 :     GEN p = cgetg(card+1, t_VECSMALL);
     961         210 :     long o = v[i], u = d*(o-1), j, k, l;
     962         210 :     gel(gen, i) = p;
     963             :     /* The following loop is over-optimized. Remember that I wrote it for
     964             :      * testpermutation. Something has survived... BA */
     965         763 :     for(j=1;j<=card;)
     966             :     {
     967        1540 :       for(k=1;k<o;k++)
     968        1197 :         for(l=1;l<=d; l++,j++) p[j] = j+d;
     969         343 :       for (l=1; l<=d; l++,j++) p[j] = j-u;
     970             :     }
     971         210 :     d += u;
     972             :   }
     973         168 :   return G;
     974             : }
     975             : 
     976             : /*return 1 if H is a normal subgroup of G*/
     977             : long
     978          56 : group_subgroup_isnormal(GEN G, GEN H)
     979             : {
     980          56 :   GEN g = grp_get_gen(G);
     981          56 :   long i, n = lg(g);
     982          56 :   if (lg(grp_get_gen(H)) > 1 && group_domain(G) != group_domain(H))
     983           0 :     pari_err_DOMAIN("group_subgroup_isnormal","domain(H)","!=",
     984             :                     strtoGENstr("domain(G)"), H);
     985         126 :   for(i=1; i<n; i++)
     986          91 :     if (!group_perm_normalize(H, gel(g,i))) return 0;
     987          35 :   return 1;
     988             : }
     989             : 
     990             : long
     991         294 : groupelts_exponent(GEN elts)
     992             : {
     993         294 :   long i, n = lg(elts)-1, expo = 1;
     994        3563 :   for(i=1; i<=n; i++)
     995        3269 :     expo = clcm(expo, perm_order(gel(elts,i)));
     996         294 :   return expo;
     997             : }
     998             : 
     999             : GEN
    1000         683 : groupelts_center(GEN S)
    1001             : {
    1002         683 :   pari_sp ltop = avma;
    1003         683 :   long i, j, n = lg(S)-1, l = n;
    1004         683 :   GEN V, elts = zero_F2v(n+1);
    1005       24463 :   for(i=1; i<=n; i++)
    1006             :   {
    1007       23780 :     if (F2v_coeff(elts,i)) { l--;  continue; }
    1008      518007 :     for(j=1; j<=n; j++)
    1009      508332 :       if (!perm_commute(gel(S,i),gel(S,j)))
    1010             :       {
    1011       13587 :         F2v_set(elts,i);
    1012       13587 :         F2v_set(elts,j); l--; break;
    1013             :       }
    1014             :   }
    1015         683 :   V = cgetg(l+1,t_VEC);
    1016       24463 :   for (i=1, j=1; i<=n ;i++)
    1017       23780 :     if (!F2v_coeff(elts,i)) gel(V,j++) = vecsmall_copy(gel(S,i));
    1018         683 :   return gerepileupto(ltop,V);
    1019             : }
    1020             : 
    1021             : GEN
    1022         994 : groupelts_conjclasses(GEN elts, long *ptnbcl)
    1023             : {
    1024         994 :   long i, j, cl = 0, n = lg(elts)-1;
    1025         994 :   GEN c = const_vecsmall(n,0);
    1026       20398 :   for (i=1; i<=n; i++)
    1027             :   {
    1028       19404 :     GEN g = gel(elts,i);
    1029       19404 :     if (c[i]) continue;
    1030        8400 :     c[i] = ++cl;
    1031      213346 :     for(j=1; j<=n; j++)
    1032             :     {
    1033      204946 :       GEN h = perm_conj(gel(elts,j), g);
    1034      204946 :       long i2 = vecsearch(elts,h,NULL);
    1035      204946 :       c[i2] = cl;
    1036             :     }
    1037             :   }
    1038         994 :   if (ptnbcl) *ptnbcl = cl;
    1039         994 :   return c;
    1040             : }
    1041             : 
    1042             : /* S a list of generators */
    1043             : GEN
    1044           0 : groupelts_abelian_group(GEN S)
    1045             : {
    1046           0 :   pari_sp ltop = avma;
    1047             :   GEN Qgen, Qord, Qelt;
    1048           0 :   long i, j, n = lg(gel(S,1))-1, l = lg(S);
    1049           0 :   Qord = cgetg(l, t_VECSMALL);
    1050           0 :   Qgen = cgetg(l, t_VEC);
    1051           0 :   Qelt = mkvec(identity_perm(n));
    1052           0 :   for (i = 1, j = 1; i < l; ++i)
    1053             :   {
    1054           0 :     GEN  g = gel(S,i);
    1055           0 :     long o = perm_relorder(g, groupelts_set(Qelt, n));
    1056           0 :     gel(Qgen,j) = g;
    1057           0 :     Qord[j] = o;
    1058           0 :     if (o != 1) { Qelt = perm_generate(g, Qelt, o); j++; }
    1059             :   }
    1060           0 :   setlg(Qgen,j);
    1061           0 :   setlg(Qord,j);
    1062           0 :   return gerepilecopy(ltop, mkvec2(Qgen, Qord));
    1063             : }
    1064             : 
    1065             : GEN
    1066          14 : group_export_GAP(GEN G)
    1067             : {
    1068          14 :   pari_sp av = avma;
    1069          14 :   GEN s, comma, g = grp_get_gen(G);
    1070          14 :   long i, k, l = lg(g);
    1071          14 :   if (l == 1) return strtoGENstr("Group(())");
    1072           7 :   s = cgetg(2*l, t_VEC);
    1073           7 :   comma = strtoGENstr(", ");
    1074           7 :   gel(s,1) = strtoGENstr("Group(");
    1075          28 :   for (i=1, k=2; i < l; ++i)
    1076             :   {
    1077          21 :     if (i > 1) gel(s,k++) = comma;
    1078          21 :     gel(s,k++) = perm_to_GAP(gel(g,i));
    1079             :   }
    1080           7 :   gel(s,k++) = strtoGENstr(")");
    1081           7 :   return gerepilecopy(av, shallowconcat1(s));
    1082             : }
    1083             : 
    1084             : GEN
    1085          14 : group_export_MAGMA(GEN G)
    1086             : {
    1087          14 :   pari_sp av = avma;
    1088          14 :   GEN s, comma, g = grp_get_gen(G);
    1089          14 :   long i, k, l = lg(g);
    1090          14 :   if (l == 1) return strtoGENstr("PermutationGroup<1|>");
    1091           7 :   s = cgetg(2*l, t_VEC);
    1092           7 :   comma = strtoGENstr(", ");
    1093           7 :   gel(s,1) = gsprintf("PermutationGroup<%ld|",group_domain(G));
    1094          28 :   for (i=1, k=2; i < l; ++i)
    1095             :   {
    1096          21 :     if (i > 1) gel(s,k++) = comma;
    1097          21 :     gel(s,k++) = GENtoGENstr( vecsmall_to_vec(gel(g,i)) );
    1098             :   }
    1099           7 :   gel(s,k++) = strtoGENstr(">");
    1100           7 :   return gerepilecopy(av, shallowconcat1(s));
    1101             : }
    1102             : 
    1103             : GEN
    1104          28 : group_export(GEN G, long format)
    1105             : {
    1106          28 :   switch(format)
    1107             :   {
    1108          14 :   case 0: return group_export_GAP(G);
    1109          14 :   case 1: return group_export_MAGMA(G);
    1110             :   }
    1111           0 :   pari_err_FLAG("galoisexport");
    1112           0 :   return NULL; /*-Wall*/
    1113             : }

Generated by: LCOV version 1.11