NAG FL Interface
m01zaf (permute_​invert)

1 Purpose

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

2 Specification

Fortran Interface
Subroutine m01zaf ( iperm, m1, m2, ifail)
Integer, Intent (In) :: m1, m2
Integer, Intent (Inout) :: iperm(m2), ifail
C Header Interface
#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.

3 Description

There are two common ways of describing a permutation using an integer vector iperm. The first uses ranks: ipermi holds the position to which the ith data element should be moved in order to sort the data; in other words its rank in the sorted order. The second uses indices: ipermi holds the current position of the data element which would occur in ith position in sorted order. For example, given the values
3.55.92.90.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.

4 References

None.

5 Arguments

1: ipermm2 Integer array Input/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: m1 Integer Input
3: 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<m1m2.
4: ifail Integer Input/Output
On entry: ifail must be set to 0, -1 or 1. If you are unfamiliar with this argument you should refer to Section 4 in the Introduction to the NAG Library FL Interface for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1 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 -1 or 1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6 Error Indicators and Warnings

If on entry 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:
ifail=1
On entry, m1=value.
Constraint: m11.
On entry, m1=value and m2=value.
Constraint: m1m2.
On entry, m2=value.
Constraint: m21.
ifail=2
ipermm1:m2 does not contain a permutation of the integers m1 to m2. ipermI contains an out-of-range value: I=value, ipermI=value.
ifail=3
ipermm1:m2 does not contain a permutation of the integers m1 to m2. iperm contains a repeated value: value.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
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.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.
If ifail=2 or 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.

7 Accuracy

Not applicable.

8 Parallelism and Performance

m01zaf is not threaded in any implementation.

9 Further Comments

None.

10 Example

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.1 Program Text

Program Text (m01zafe.f90)

10.2 Program Data

Program Data (m01zafe.d)

10.3 Program Results

Program Results (m01zafe.r)