Is there a better way to input an $n$-cycle in GAP?

217 Views Asked by At

In GAP, we can generate permutations using, for example

alpha:=(1,2,3,4,5);

But, this method is not usable if we want to input the permutation $$(1,2,\ldots,n)$$ for some variable $n$.

It is possible to use

alpha:=PermList(List([1..n],i->(i mod n)+1));

for variable $n$, but this feels clumsy.

Question: Is there a better way to input an $n$-cycle in GAP?

1

There are 1 best solutions below

3
On

Thank you, it's a good question. In brief, your suggestion to use PermList is right, but its argument may be assembled faster:

alpha:=PermList(Concatenation([2..n],[1]));

Now a bit longer version of the reply with timings. First, another alternative is MappingPermListList:

gap> n:=5;;MappingPermListList([1..n],Concatenation([2..n],[1]));
(1,2,3,4,5)

Nevertheless, your suggestion with PermList is already slightly faster:

gap> n:=100000;;for i in [1..100] do alpha:=PermList(List([1..n],i->(i mod n)+1));od;time;
984
gap> n:=100000;;for i in [1..100] do alpha:=MappingPermListList([1..n],Concatenation([2..n],[1]));od;time;
1361

However, one could do even better, avoiding writing mod n)+1 and using Concatenation instead. Then the performance is ~20 times faster:

gap> n:=100000;;for i in [1..100] do alpha:=PermList(Concatenation([2..n],[1]));od;time;
47

Summarising, I'd recommend to use PermList in this particular case when one needs to create a cycle $(1,2,...n)$. In a general case, one should choose between PermList and MappingPermListList dependently on the data available.

P.S. Note that the naive approach to multiply transpositions does not scale well at all:

gap> n:=5;; alpha:=(1,2);;for j in [3..n] do alpha:=alpha*(1,j);od; alpha;
(1,2,3,4,5)
gap> n:=100000;; alpha:=(1,2);;for j in [3..n] do alpha:=alpha*(1,j);od; time;
14429