2

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 Answers1

4

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
Olexandr Konovalov
  • 7,002
  • 2
  • 34
  • 72
  • P.S. Any suggestions for the syntax to specify n-cycles? – Olexandr Konovalov Sep 09 '13 at 19:32
  • @ Alexander could you please explain what is the meaning of i->(i mod n)+1? – S786 Jan 31 '14 at 04:31
  • @smaz: -> is a GAP shortcut for one-argument function. i->(i mod n)+1? creates a function f(i) such that i from [1..n-1] is mapped to i+1, and n is mapped to 1. On other inputs it will act appropriately (but in the example above it is called for i from [1..n]). P.S. there should be no space between @ and username to ping, I've just occasionally discovered your question. – Olexandr Konovalov Jan 31 '14 at 08:04