## A. y (Anagram Index)

Assigns a unique number to any permutation on N points (say, the letters 'ABCD').

This number is called the Anagram Index (AI).

```   N=: 4        NB. Confine attention to permutations on 4 points
A=: 'ABCD'   NB. N points provided for a given permutation to act on
p=: 2 0 1 3  NB. a given permutation on N points
] i=: i.N    NB. the identity permutation on N points
0 1 2 3
p { A        NB. Permute 'ABCD' by permutation: p
CABD

A. p         NB. The AI of permutation: p
12
A. i         NB. The AI of permutation: i (identity)
0```

For a given AI, the permutation on N points can be found by using dyadic A. (see below)

```   12 A. i
2 0 1 3
p
2 0 1 3```

### Common uses

1. Work conveniently with permutations, using the AI instead of the direct permutation (rearranged i.N)

2. Explore the subgroups of a given permutation group.

Example: the symmetric group of all permutations on 4 points, known as Sym(4) or S4.

Representing each permutation by its AI, we can represent the set S4 in J as follows:

```   N=: 4
] S4=: i. !N   NB. a list of AIs
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23```

plus a group product operation: mul, between any two AIs (the elements of S4)

`   mul=: 4 : 'A. (a2p x) { (a2p y)'"0   NB. (AI) mul (AI) --> (AI)`

mul always yields another AI, viz an element in S4.

The set of all permutations on N points forms a mathematical group under the operation: mul. That is:

• the set is closed under mul

```   z=: S4 mul/ S4
~. ,z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23```
• The operation is associative, i.e. expressions in mul can be bracketed in any order

```   ] 'a b c'=: 3 ? #S4
3 2 17
assert (a mul (b mul c)) -: ((a mul b) mul c)```
• There exists a unique identity element. In S4 this is 0.

• Every AI a has a unique right inverse r and a unique left inverse 's', such that

``` assert i -: (a mul r)
assert i -: (s mul a)```

3. Build the Cayley Table of the subgroup of Sym4 generated by the AI: 9 under mul

```   9 mul 9
16
9 mul 9 mul 9
18
9 mul 9 mul 9 mul 9
0

S=: 0 9 16 18   NB. Define candidate set S```

Does S form a group under mul? If so then (S mul/ S) will contain only items of S

```   S mul/ S   NB. the Cayley Table of subgroup S
0  9 16 18
9 16 18  0
16 18  0  9
18  0  9 16```

## x A. y (Anagram)

Returns the direct permutation p corresponding to its anagram index (AI) x

• See monadic A. above for all definitions, including i and p.

```   p              NB. a sample direct permutation on 4 points
2 0 1 3
i              NB. the identity permutation on 4 points
0 1 2 3
A. p           NB. the AI of permutation: p
12
12 A. i        NB. recover original p from its AI
2 0 1 3```

Given the value of N (the number of points), viz. 4, the AI: 12 contains full information about p and can be used instead of p to permute the 4 points directly, or indeed any desired list of 4 items

```   p { 'ABCD'     NB. use p to permute a list of 4 letters (the "4 points")
CABD
12 A. 'ABCD'   NB. use the AI instead
CABD```

### Common uses

1. Generate all the distinct permutations ("anagrams") of 4 items

```   n=: !N=: 4   NB. number of distinct permutations on N=4 points
(i.n) A. i   NB. the permutations corresponding to the list of AI's: (i.n)
0 1 2 3
0 1 3 2
0 2 1 3
0 2 3 1
0 3 1 2
0 3 2 1
1 0 2 3
1 0 3 2
1 2 0 3
1 2 3 0
1 3 0 2
1 3 2 0
2 0 1 3
2 0 3 1
2 1 0 3
2 1 3 0
2 3 0 1
2 3 1 0
3 0 1 2
3 0 2 1
3 1 0 2
3 1 2 0
3 2 0 1
3 2 1 0```

2. Build a table of the AI's of S4 (the symmetric group on 4 points) with their corresponding direct permutations

```   S4              NB. The AI's contained in S4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
\$ t=: S4 A. i   NB. list of corresponding permutations
24 4
(,.S4) ; t
+--+-------+
| 0|0 1 2 3|
| 1|0 1 3 2|
| 2|0 2 1 3|
| 3|0 2 3 1|
| 4|0 3 1 2|
| 5|0 3 2 1|
| 6|1 0 2 3|
| 7|1 0 3 2|
| 8|1 2 0 3|
| 9|1 2 3 0|
|10|1 3 0 2|
|11|1 3 2 0|
|12|2 0 1 3|
|13|2 0 3 1|
|14|2 1 0 3|
|15|2 1 3 0|
|16|2 3 0 1|
|17|2 3 1 0|
|18|3 0 1 2|
|19|3 0 2 1|
|20|3 1 0 2|
|21|3 1 2 0|
|22|3 2 0 1|
|23|3 2 1 0|
+--+-------+```