Intro 25. Permutations

>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help Dictionary

25. Permutations
Anagrams are familiar examples of the important notion of permutations:

       w=: 'STOP'
       3 2 0 1 { w
    POST

       2 3 1 0 { w
    OPTS

       3 0 2 1 { w
    PSOT

The left arguments of`{` above are themselves permutations of the list`i.4` ;`` examples of permutation vectors, used to represent permutation functions in the form`p&{` .
If`p` is a permutation vector, the phrase`p&C.` also represents the permutation`p&{` .`` However, other cases of the cycle function`C.` are distinct from the from function`{` .`` In particular,`C. p` yields the cycle representation of the permutation`p` .`` For example:

       ]c=: C. p=: 2 4 0 1 3
    +---+-----+
    |2 0|4 3 1|
    +---+-----+

       c C. 'ABCDE'                   C. c
    CEABD                          2 4 0 1 3

Each of the boxed elements of a cycle specify a list of positions that cycle among themselves; in the example above, the element from position`3` moves to position`4` ,`` element`1` moves to`3` ,`` and element`4` to`1` .
A permutation can be identified by its index in the table of all`!n` permutations of order`n` listed in increasing order. This index is the anagram index of the permutation; the corresponding permutation is effected by the function`A.` as illustrated below:

       1 A. 'ABCDE'                   A. 0 1 2 4 3
    ABCED                          1

       (i.!3) A. i.3                  (i.!3) A. 'ABC'
    0 1 2                          ABC
    0 2 1                          ACB
    1 0 2                          BAC
    1 2 0                          BCA
    2 0 1                          CAB
    2 1 0                          CBA

Exercises

25.1 | Use the following as exercises in reading and writing (try on`a=:'abcdef'` and`b=: i. 6` and`c=: i. 6 6`):
    f=: 1&A.                       Interchange last two items
    g=: 3&A.                       Rotate last three items
    h=: 5&A.                       Reverse last three items
    i=: <:@!@[ A. ]                k i a reverses last k items
25.2 | Experiment with the following expressions and others like them to determine the rules for using abbreviated arguments to`C.` and compare your conclusions with the dictionary definitions:
    2 1 4 C. b=:i.6
    (<2 1 4) C. b
    (3 1;5 0) C. b
25.3 | Make a program`ac` that produces a table of the cycle representations of all permutations of the order of its argument, as in`ac 3` . Answer:`ac=: C.@(i.@! A. i.)`

>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help Dictionary