# NAG FL Interfacem01zaf (permute_​invert)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

m01zaf inverts a permutation, and hence converts a rank vector to an index vector, or vice versa.

## 2Specification

Fortran Interface
 Subroutine m01zaf ( m1, m2,
 Integer, Intent (In) :: m1, m2 Integer, Intent (Inout) :: iperm(m2), ifail
#include <nag.h>
 void m01zaf_ (Integer iperm[], const Integer *m1, const Integer *m2, Integer *ifail)
The routine may be called by the names m01zaf or nagf_sort_permute_invert.

## 3Description

There are two common ways of describing a permutation using an integer vector iperm. The first uses ranks: ${\mathbf{iperm}}\left(i\right)$ holds the position to which the $i$th data element should be moved in order to sort the data; in other words its rank in the sorted order. The second uses indices: ${\mathbf{iperm}}\left(i\right)$ holds the current position of the data element which would occur in $i$th position in sorted order. For example, given the values
 $3.5 5.9 2.9 0.5$
to be sorted in ascending order, the ranks would be
 $3.0 4.0 2.0 1.0$
and the indices would be
 $4.0 3.0 1.0 2.0$
The routines m01daf, m01dbf, m01dcf, m01def, m01dff, m01djf, m01dkf and m01dzf generate ranks, and the routines m01eaf, m01ebf, m01ecf and m01edf require ranks to be supplied to specify the reordering. However if it is desired simply to refer to the data in sorted order without actually reordering them, indices are more convenient than ranks.
m01zaf can be used to convert ranks to indices, or indices to ranks, as the two permutations are inverses of each another.

None.

## 5Arguments

1: $\mathbf{iperm}\left({\mathbf{m2}}\right)$Integer array Input/Output
On entry: elements ${\mathbf{m1}}$ to ${\mathbf{m2}}$ of iperm must contain a permutation of the integers m1 to m2.
On exit: these elements contain the inverse permutation of the integers m1 to m2.
2: $\mathbf{m1}$Integer Input
3: $\mathbf{m2}$Integer Input
On entry: m1 and m2 must specify the range of elements used in the array iperm and the range of values in the permutation, as specified under iperm.
Constraint: $0<{\mathbf{m1}}\le {\mathbf{m2}}$.
4: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{m1}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m1}}\ge 1$.
On entry, ${\mathbf{m1}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{m2}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m1}}\le {\mathbf{m2}}$.
On entry, ${\mathbf{m2}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m2}}\ge 1$.
${\mathbf{ifail}}=2$
${\mathbf{iperm}}\left({\mathbf{m1}}:{\mathbf{m2}}\right)$ does not contain a permutation of the integers m1 to m2. ${\mathbf{iperm}}\left(I\right)$ contains an out-of-range value: $I=⟨\mathit{\text{value}}⟩$, ${\mathbf{iperm}}\left(I\right)=⟨\mathit{\text{value}}⟩$.
Elements m1 to m2 of iperm do not contain a permutation of the integers m1 to m2; on exit these elements are usually corrupted. To check the validity of a permutation without the risk of corrupting it, use m01zbf.
${\mathbf{ifail}}=3$
${\mathbf{iperm}}\left({\mathbf{m1}}:{\mathbf{m2}}\right)$ does not contain a permutation of the integers m1 to m2. iperm contains a repeated value: $⟨\mathit{\text{value}}⟩$.
Elements m1 to m2 of iperm do not contain a permutation of the integers m1 to m2; on exit these elements are usually corrupted. To check the validity of a permutation without the risk of corrupting it, use m01zbf.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

Not applicable.

## 8Parallelism and Performance

m01zaf is not threaded in any implementation.

None.

## 10Example

This example reads a matrix of real numbers and prints its rows in ascending order as ranked by m01def. The program first calls m01def to rank the rows, and then calls m01zaf to convert the rank vector to an index vector, which is used to refer to the rows in sorted order.

### 10.1Program Text

Program Text (m01zafe.f90)

### 10.2Program Data

Program Data (m01zafe.d)

### 10.3Program Results

Program Results (m01zafe.r)