# NAG FL Interfaceg03eaf (distance_​mat)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g03eaf computes a distance (dissimilarity) matrix.

## 2Specification

Fortran Interface
 Subroutine g03eaf ( dist, scal, n, m, x, ldx, isx, s, d,
 Integer, Intent (In) :: n, m, ldx, isx(m) Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(ldx,m) Real (Kind=nag_wp), Intent (Inout) :: s(m), d(n*(n-1)/2) Character (1), Intent (In) :: update, dist, scal
#include <nag.h>
 void g03eaf_ (const char *update, const char *dist, const char *scal, const Integer *n, const Integer *m, const double x[], const Integer *ldx, const Integer isx[], double s[], double d[], Integer *ifail, const Charlen length_update, const Charlen length_dist, const Charlen length_scal)
The routine may be called by the names g03eaf or nagf_mv_distance_mat.

## 3Description

Given $n$ objects, a distance or dissimilarity matrix is a symmetric matrix with zero diagonal elements such that the $ij$th element represents how far apart or how dissimilar the $i$th and $j$th objects are.
Let $X$ be an $n×p$ data matrix of observations of $p$ variables on $n$ objects, then the distance between object $j$ and object $k$, ${d}_{jk}$, can be defined as:
 $djk= {∑i=1pD(xji/si,xki/si)} α ,$
where ${x}_{ji}$ and ${x}_{ki}$ are the $ji$th and $ki$th elements of $X$, ${s}_{i}$ is a standardization for the $i$th variable and $D\left(u,v\right)$ is a suitable function. Three functions are provided in g03eaf.
1. (a)Euclidean distance: $D\left(u,v\right)={\left(u-v\right)}^{2}$ and $\alpha =\frac{1}{2}$.
2. (b)Euclidean squared distance: $D\left(u,v\right)={\left(u-v\right)}^{2}$ and $\alpha =1$.
3. (c)Absolute distance (city block metric): $D\left(u,v\right)=|u-v|$ and $\alpha =1$.
Three standardizations are available.
1. (a)Standard deviation: ${s}_{i}=\sqrt{\sum _{j=1}^{n}{\left({x}_{ji}-\overline{x}\right)}^{2}/\left(n-1\right)}$
2. (b)Range: ${s}_{i}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({x}_{1i},{x}_{2i},\dots ,{x}_{ni}\right)-\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({x}_{1i},{x}_{2i},\dots ,{x}_{ni}\right)$
3. (c)User-supplied values of ${s}_{i}$.
In addition to the above distances there are a large number of other dissimilarity measures, particularly for dichotomous variables (see Krzanowski (1990) and Everitt (1974)). For the dichotomous case these measures are simple to compute and can, if suitable scaling is used, be combined with the distances computed by g03eaf using the updating option.
Dissimilarity measures for variables can be based on the correlation coefficient for continuous variables and contingency table statistics for dichotomous data, see Chapters G02 and G11 respectively.
g03eaf returns the strictly lower triangle of the distance matrix.

## 4References

Everitt B S (1974) Cluster Analysis Heinemann
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press

## 5Arguments

1: $\mathbf{update}$Character(1) Input
On entry: indicates whether or not an existing matrix is to be updated.
${\mathbf{update}}=\text{'U'}$
The matrix $D$ is updated and distances are added to $D$.
${\mathbf{update}}=\text{'I'}$
The matrix $D$ is initialized to zero before the distances are added to $D$.
Constraint: ${\mathbf{update}}=\text{'U'}$ or $\text{'I'}$.
2: $\mathbf{dist}$Character(1) Input
On entry: indicates which type of distances are computed.
${\mathbf{dist}}=\text{'A'}$
Absolute distances.
${\mathbf{dist}}=\text{'E'}$
Euclidean distances.
${\mathbf{dist}}=\text{'S'}$
Euclidean squared distances.
Constraint: ${\mathbf{dist}}=\text{'A'}$, $\text{'E'}$ or $\text{'S'}$.
3: $\mathbf{scal}$Character(1) Input
On entry: indicates the standardization of the variables to be used.
${\mathbf{scal}}=\text{'S'}$
Standard deviation.
${\mathbf{scal}}=\text{'R'}$
Range.
${\mathbf{scal}}=\text{'G'}$
Standardizations given in array s.
${\mathbf{scal}}=\text{'U'}$
Unscaled.
Constraint: ${\mathbf{scal}}=\text{'S'}$, $\text{'R'}$, $\text{'G'}$ or $\text{'U'}$.
4: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 2$.
5: $\mathbf{m}$Integer Input
On entry: the total number of variables in array x.
Constraint: ${\mathbf{m}}>0$.
6: $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{m}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\mathbf{x}}\left(\mathit{i},\mathit{j}\right)$ must contain the value of the $\mathit{j}$th variable for the $\mathit{i}$th object, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
7: $\mathbf{ldx}$Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g03eaf is called.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
8: $\mathbf{isx}\left({\mathbf{m}}\right)$Integer array Input
On entry: ${\mathbf{isx}}\left(j\right)$ indicates whether or not the $j$th variable in x is to be included in the distance computations.
If ${\mathbf{isx}}\left(\mathit{j}\right)>0$ the $\mathit{j}$th variable is included, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$; otherwise it is not referenced.
Constraint: ${\mathbf{isx}}\left(\mathit{j}\right)>0$ for at least one $\mathit{j}$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
9: $\mathbf{s}\left({\mathbf{m}}\right)$Real (Kind=nag_wp) array Input/Output
On entry: if ${\mathbf{scal}}=\text{'G'}$ and ${\mathbf{isx}}\left(\mathit{j}\right)>0$ then ${\mathbf{s}}\left(\mathit{j}\right)$ must contain the scaling for variable $\mathit{j}$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
Constraint: if ${\mathbf{scal}}=\text{'G'}$ and ${\mathbf{isx}}\left(j\right)>0$, ${\mathbf{s}}\left(\mathit{j}\right)>0.0$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
On exit: if ${\mathbf{scal}}=\text{'S'}$ and ${\mathbf{isx}}\left(j\right)>0$ then ${\mathbf{s}}\left(j\right)$ contains the standard deviation of the variable in the $j$th column of x.
If ${\mathbf{scal}}=\text{'R'}$ and ${\mathbf{isx}}\left(j\right)>0$, ${\mathbf{s}}\left(j\right)$ contains the range of the variable in the $j$th column of x.
If ${\mathbf{scal}}=\text{'U'}$ and ${\mathbf{isx}}\left(j\right)>0$, ${\mathbf{s}}\left(j\right)=1.0$.
If ${\mathbf{scal}}=\text{'G'}$, s is unchanged.
10: $\mathbf{d}\left({\mathbf{n}}×\left({\mathbf{n}}-1\right)/2\right)$Real (Kind=nag_wp) array Input/Output
On entry: if ${\mathbf{update}}=\text{'U'}$, d must contain the strictly lower triangle of the distance matrix $D$ to be updated. $D$ must be stored packed by rows, i.e., ${\mathbf{d}}\left(\left(i-1\right)\left(i-2\right)/2+j\right)$, $i>j$ must contain ${d}_{ij}$.
If ${\mathbf{update}}=\text{'I'}$, d need not be set.
Constraint: if ${\mathbf{update}}=\text{'U'}$, ${\mathbf{d}}\left(\mathit{j}\right)\ge 0.0$, for $\mathit{j}=1,2,\dots ,n\left(n-1\right)/2$.
On exit: the strictly lower triangle of the distance matrix $D$ stored packed by rows, i.e., ${d}_{ij}$ is contained in ${\mathbf{d}}\left(\left(i-1\right)\left(i-2\right)/2+j\right)$, $i>j$.
11: $\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{dist}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{dist}}=\text{'A'}$, $\text{'E'}$ or $\text{'S'}$
On entry, ${\mathbf{ldx}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}>0$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 2$.
On entry, ${\mathbf{scal}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{scal}}=\text{'S'}$, $\text{'R'}$, $\text{'G'}$ or $\text{'U'}$.
On entry, ${\mathbf{update}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{update}}=\text{'U'}$ or $\text{'I'}$.
${\mathbf{ifail}}=2$
On entry, at least one element of ${\mathbf{d}}<0.0$.
On entry, at least one element of ${\mathbf{s}}\le 0.0$.
On entry, isx does not contain a positive element.
Variable $⟨\mathit{\text{value}}⟩$ is constant.
${\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.

## 7Accuracy

The computations are believed to be stable.

## 8Parallelism and Performance

g03eaf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g03eaf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

g03ecf can be used to perform cluster analysis on the computed distance matrix.

## 10Example

A data matrix of five observations and three variables is read in and a distance matrix is calculated from variables $2$ and $3$ using squared Euclidean distance with no scaling. This matrix is then printed.

### 10.1Program Text

Program Text (g03eafe.f90)

### 10.2Program Data

Program Data (g03eafe.d)

### 10.3Program Results

Program Results (g03eafe.r)