Permutations: Difference between revisions

Content added Content deleted
m (→‎{{header|GAP}}: slightly simpler)
Line 1,304: Line 1,304:
=={{header|GAP}}==
=={{header|GAP}}==
GAP can handle permutations and groups. Here is a straightforward implementation : for each permutation p in S(n) (symmetric group),
GAP can handle permutations and groups. Here is a straightforward implementation : for each permutation p in S(n) (symmetric group),
compute the images of 1...n by p. As an alternative, List(SymmetricGroup(n)) would yield the permutations as GAP ''Permutation'' objects,
compute the images of 1 .. n by p. As an alternative, List(SymmetricGroup(n)) would yield the permutations as GAP ''Permutation'' objects,
which would probably be more manageable in later computations.
which would probably be more manageable in later computations.
<lang gap>gap>perms := n -> List(SymmetricGroup(n), p -> List([1..n], x -> x^p));
<lang gap>gap>List(SymmetricGroup(4), p -> Permuted([1 .. 4], p));
perms(4);
perms(4);
[ [ 1, 2, 3, 4 ], [ 4, 2, 3, 1 ], [ 2, 4, 3, 1 ], [ 3, 2, 4, 1 ], [ 1, 4, 3, 2 ], [ 4, 1, 3, 2 ], [ 2, 1, 3, 4 ],
[ [ 1, 2, 3, 4 ], [ 4, 2, 3, 1 ], [ 2, 4, 3, 1 ], [ 3, 2, 4, 1 ], [ 1, 4, 3, 2 ], [ 4, 1, 3, 2 ], [ 2, 1, 3, 4 ],
Line 1,313: Line 1,313:
[ 4, 3, 2, 1 ], [ 2, 3, 4, 1 ], [ 3, 4, 2, 1 ] ]</lang>
[ 4, 3, 2, 1 ], [ 2, 3, 4, 1 ], [ 3, 4, 2, 1 ] ]</lang>
GAP has also built-in functions to get permutations
GAP has also built-in functions to get permutations
<lang gap># All arrangements of 4 elements in 1..4
<lang gap># All arrangements of 4 elements in 1 .. 4
Arrangements([1..4], 4);
Arrangements([1 .. 4], 4);
# All permutations of 1..4
# All permutations of 1 .. 4
PermutationsList([1..4]);</lang>
PermutationsList([1 .. 4]);</lang>
Here is an implementation using a function to compute next permutation in lexicographic order:
Here is an implementation using a function to compute next permutation in lexicographic order:
<lang gap>NextPermutation := function(a)
<lang gap>NextPermutation := function(a)