# NAG Library Routine Document

## 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 nagmk26.h
 void m01zaf_ (Integer iperm[], const Integer *m1, const Integer *m2, Integer *ifail)

## 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 M01D routines generate ranks, and the M01E routines 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 (see the example in Section 10).
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 arrayInput/Output
On entry: elements m1 to 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}$ – IntegerInput
3:     $\mathbf{m2}$ – IntegerInput
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}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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{m2}}<1$, or ${\mathbf{m1}}<1$, or ${\mathbf{m1}}>{\mathbf{m2}}$.
${\mathbf{ifail}}=2$
Elements m1 to m2 of iperm contain a value outside the range m1 to m2.
${\mathbf{ifail}}=3$
Elements m1 to m2 of iperm contain a repeated value.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.
If ${\mathbf{ifail}}={\mathbf{2}}$ or ${\mathbf{3}}$, 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.

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)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017