## Permutations in Cyclic Form

A permutation can be represented in both linear and cyclic notations. The functions described in this section can be used to convert between the two forms.

The linear notation is an index mapping, and has already been described above. The cyclic notation represents a permutation as a series of circular rearrangements of groups of elements, or cycles.

Any permutation can be decomposed into a combination of cycles. For example, under the cycle (1 2 3), 1 is replaced by 2, 2 is replaced by 3 and 3 is replaced by 1 in a circular fashion. Cycles of different sets of elements can be combined independently, for example (1 2 3) (4 5) combines the cycle (1 2 3) with the cycle (4 5), which is an exchange of elements 4 and 5. A cycle of length one represents an element which is unchanged by the permutation and is referred to as a singleton.

The cyclic notation for a permutation is not unique, but can be rearranged into a unique canonical form by a reordering of elements. The library uses the canonical form defined in Knuth's Art of Computer Programming (Vol 1, 3rd Ed, 1997) Section 1.3.3, p.178.

The procedure for obtaining the canonical form given by Knuth is,

1. Write all singleton cycles explicitly
2. Within each cycle, put the smallest number first
3. Order the cycles in decreasing order of the first number in the cycle.

For example, the linear representation (2 4 3 0 1) is represented as (1 4) (0 2 3) in canonical form. The permutation corresponds to an exchange of elements 1 and 4, and rotation of elements 0, 2 and 3.

The important property of the canonical form is that it can be reconstructed from the contents of each cycle without the brackets. In addition, by removing the brackets it can be considered as a linear representation of a different permutation. In the example given above the permutation (2 4 3 0 1) would become (1 4 0 2 3). This mapping between linear permutations defined by the canonical form has many important uses in the theory of permutations.

Function: int gsl_permutation_linear_to_canonical (gsl_permutation * q, const gsl_permutation * p)
  This function computes the canonical form of the permutation p and stores it in the output argument q.

Function: int gsl_permutation_canonical_to_linear (gsl_permutation * p, const gsl_permutation * q)
  This function converts a permutation q in canonical form back into linear form storing it in the output argument p.

Function: size_t gsl_permutation_inversions (const gsl_permutation * p)
  This function counts the number of inversions in the permutation p.

Function: size_t gsl_permutation_linear_cycles (const gsl_permutation * p)
  This function counts the number of cycles in the permutation p.

Function: size_t gsl_permutation_canonical_cycles (const gsl_permutation * q)
  This function counts the number of cycles in the permutation q, where q is given in canonical form.