Permutations: Difference between revisions

m
→‎{{header|GAP}}: slightly simpler
m (→‎{{header|GAP}}: slightly simpler)
Line 1,304:
=={{header|GAP}}==
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,
which would probably be more manageable in later computations.
<lang gap>gap>perms := n -> List(SymmetricGroup(n4), p -> ListPermuted([1 ..n 4], x -> x^p));
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 ],
Line 1,313:
[ 4, 3, 2, 1 ], [ 2, 3, 4, 1 ], [ 3, 4, 2, 1 ] ]</lang>
GAP has also built-in functions to get permutations
<lang gap># All arrangements of 4 elements in 1 .. 4
Arrangements([1 .. 4], 4);
# All permutations of 1 .. 4
PermutationsList([1 .. 4]);</lang>
Here is an implementation using a function to compute next permutation in lexicographic order:
<lang gap>NextPermutation := function(a)
Anonymous user