naginterfaces.library.sort.permute_decompose(iperm, m1)[source]

permute_decompose decomposes a permutation into cycles, as an aid to reordering ranked data.

For full information please refer to the NAG Library document for m01zc

ipermint, array-like, shape

Elements to of must contain a permutation of the integers to .


Note: this argument represents an array index; the value you supply must be base-1 for compatibility with the NAG Engine.

and must specify the range of elements used in the array and the range of values in the permutation, as specified under .

ipermint, ndarray, shape

Is used as internal workpsace prior to being restored and hence is unchanged.

icyclint, ndarray, shape

Elements to of contain a representation of the permutation as a list of cycles, with the first integer in each cycle negated. (See Notes.)

(errno )

On entry, and .

Constraint: .

(errno )

On entry, .

Constraint: .

(errno )

On entry, .

Constraint: .

(errno )

does not contain a permutation of the integers to . contains an out-of-range value: , .

(errno )

does not contain a permutation of the integers to . contains a repeated value: .


No equivalent traditional C interface for this routine exists in the NAG Library.

permute_decompose is provided as an aid to reordering arbitrary data structures without using additional storage. However, you should consider carefully whether it is necessary to rearrange yourr data, or whether it would be simpler and more efficient to refer to the data in sorted order using an index vector, or to create a copy of the data in sorted order.

To rearrange data into a different order without using additional storage, the simplest method is to decompose the permutation which specifies the new order into cycles and then to do a cyclic permutation of the data items in each cycle. (This is the method used by the reordering functions realvec_rank_rearrange(), intvec_rank_rearrange(), charvec_rank_rearrange() and cmplxvec_rank_rearrange().) Given a vector IRANK which specifies the ranks of the data (as generated by the functions realvec_rank(), intvec_rank(), charvec_rank(), realmat_rank_rows(), intmat_rank_rows(), realmat_rank_columns(), intmat_rank_columns() and arbitrary_rank()), permute_decompose generates a new vector , in which the permutation is represented in its component cycles, with the first element of each cycle negated. For example, the permutation

is composed of the cycles

and the vector generated by permute_decompose contains

In order to rearrange the data according to the specified ranks:

item must be left in place;

items and must be interchanged;

items , , and must be moved right one place round the cycle.

The complete rearrangement can be achieved by the following logic:

loop for to


if then



swap items and