# NAG CL Interfaceg03eac (distance_​mat)

Settings help

CL Name Style:

## 1Purpose

g03eac computes a distance (dissimilarity) matrix.

## 2Specification

 #include
 void g03eac (Nag_MatUpdate update, Nag_DistanceType dist, Nag_VarScaleType scale, Integer n, Integer m, const double x[], Integer tdx, const Integer isx[], double s[], double d[], NagError *fail)
The function may be called by the names: g03eac or nag_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:
 $d jk = { ∑ i=1 p D( x ji / s i , x ki / s i )} α ,$
where ${x}_{ji}$ and ${x}_{ki}$ are the $\left(j,i\right)$th and $\left(k,i\right)$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 g03eac:
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. 1.Standard deviation: ${s}_{i}=\sqrt{{\sum }_{j=1}^{n}{\left({x}_{ji}-\overline{x}\right)}^{2}/\left(n-1\right)}$
2. 2.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. 3.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 g03eac 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 the G02 Chapter Introduction and the G11 Chapter Introduction respectively.
g03eac returns the strictly lower triangle of the distance matrix.
Everitt B S (1974) Cluster Analysis Heinemann
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press

## 5Arguments

1: $\mathbf{update}$Nag_MatUpdate Input
On entry: indicates whether or not an existing matrix is to be updated.
${\mathbf{update}}=\mathrm{Nag_MatUp}$
The matrix $D$ is updated and distances are added to $D$.
${\mathbf{update}}=\mathrm{Nag_NoMatUp}$
The matrix $D$ is initialized to zero before the distances are added to $D$.
Constraint: ${\mathbf{update}}=\mathrm{Nag_MatUp}$ or $\mathrm{Nag_NoMatUp}$.
2: $\mathbf{dist}$Nag_DistanceType Input
On entry: indicates which type of distances are computed.
${\mathbf{dist}}=\mathrm{Nag_DistAbs}$
Absolute distances.
${\mathbf{dist}}=\mathrm{Nag_DistEuclid}$
Euclidean distances.
${\mathbf{dist}}=\mathrm{Nag_DistSquared}$
Euclidean squared distances.
Constraint: ${\mathbf{dist}}=\mathrm{Nag_DistAbs}$, $\mathrm{Nag_DistEuclid}$ or $\mathrm{Nag_DistSquared}$.
3: $\mathbf{scale}$Nag_VarScaleType Input
On entry: indicates the standardization of the variables to be used.
${\mathbf{scale}}=\mathrm{Nag_VarScaleStd}$
Standard deviation.
${\mathbf{scale}}=\mathrm{Nag_VarScaleRange}$
Range.
${\mathbf{scale}}=\mathrm{Nag_VarScaleUser}$
Standardizations given in array $S$.
${\mathbf{scale}}=\mathrm{Nag_NoVarScale}$
Unscaled.
Constraint: ${\mathbf{scale}}=\mathrm{Nag_VarScaleStd}$, $\mathrm{Nag_VarScaleRange}$, $\mathrm{Nag_VarScaleUser}$ or $\mathrm{Nag_NoVarScale}$.
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{n}}×{\mathbf{tdx}}\right]$const double Input
On entry: ${\mathbf{x}}\left[\left(\mathit{i}-1\right)×{\mathbf{tdx}}+\mathit{j}-1\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{tdx}$Integer Input
On entry: the stride separating matrix column elements in the array x.
Constraint: ${\mathbf{tdx}}\ge {\mathbf{m}}$.
8: $\mathbf{isx}\left[{\mathbf{m}}\right]$const Integer Input
On entry: ${\mathbf{isx}}\left[j-1\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}-1\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}-1\right]>0$ for at least one $\mathit{j}$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
9: $\mathbf{s}\left[{\mathbf{m}}\right]$double Input/Output
On entry: if ${\mathbf{scale}}=\mathrm{Nag_VarScaleUser}$ and ${\mathbf{isx}}\left[\mathit{j}-1\right]>0$ then ${\mathbf{s}}\left[\mathit{j}-1\right]$ must contain the scaling for variable $\mathit{j}$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
Constraint: if ${\mathbf{scale}}=\mathrm{Nag_VarScaleUser}$ and ${\mathbf{isx}}\left[j-1\right]>0$, ${\mathbf{s}}\left[\mathit{j}-1\right]>0.0$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
On exit: if ${\mathbf{scale}}=\mathrm{Nag_VarScaleStd}$ and ${\mathbf{isx}}\left[j-1\right]>0$ then ${\mathbf{s}}\left[j-1\right]$ contains the standard deviation of the variable in the $j$th column of x.
If ${\mathbf{scale}}=\mathrm{Nag_VarScaleRange}$ and ${\mathbf{isx}}\left[j-1\right]>0$ then ${\mathbf{s}}\left[j-1\right]$ contains the range of the variable in the $j$th column of x.
If ${\mathbf{scale}}=\mathrm{Nag_NoVarScale}$ and ${\mathbf{isx}}\left[j-1\right]>0$ then ${\mathbf{s}}\left[j-1\right]=1.0$ and if ${\mathbf{scale}}=\mathrm{Nag_VarScaleUser}$ then s is unchanged.
10: $\mathbf{d}\left[{\mathbf{n}}×\left({\mathbf{n}}-1\right)/2\right]$double Input/Output
On entry: if ${\mathbf{update}}=\mathrm{Nag_MatUp}$ then 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-1\right]$, $i>j$ must contain ${d}_{ij}$.
Constraint: if ${\mathbf{update}}=\mathrm{Nag_MatUp}$, ${\mathbf{d}}\left[\mathit{j}-1\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-1\right]$, $i>j$.
11: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_2_INT_ARG_LT
On entry, ${\mathbf{tdx}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{tdx}}\ge {\mathbf{m}}$.
NE_BAD_PARAM
On entry, argument dist had an illegal value.
On entry, argument scale had an illegal value.
On entry, argument update had an illegal value.
NE_IDEN_ELEM_COND
On entry, ${\mathbf{scale}}=\mathrm{Nag_VarScaleRange}$ or ${\mathbf{scale}}=\mathrm{Nag_VarScaleStd}$, and ${\mathbf{x}}\left[\left(\mathit{i}-1\right)×{\mathbf{tdx}}+j-1\right]={\mathbf{x}}\left[\left(\mathit{i}\right)×{\mathbf{tdx}}+j-1\right]$, for $\mathit{i}=1,2,\dots ,n-1$, for some $j$ with ${\mathbf{isx}}\left[i-1\right]>0$.
NE_INT_ARG_LE
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}>0$.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 2$.
NE_INTARR
On entry, ${\mathbf{isx}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{isx}}\left[i-1\right]>0$, for at least one $i,i=1,2,\dots ,{\mathbf{m}}$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_REALARR
On entry, ${\mathbf{d}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{d}}\left[\mathit{i}-1\right]\ge 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}×\left({\mathbf{n}}-1\right)/2$, when ${\mathbf{update}}=\mathrm{Nag_MatUp}$.
On entry, ${\mathbf{s}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{s}}\left[\mathit{j}-1\right]>0$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$, when ${\mathbf{scale}}=\mathrm{Nag_VarScaleUser}$ and ${\mathbf{isx}}\left[j-1\right]>0$.

## 7Accuracy

The computations are believed to be stable.

## 8Parallelism and Performance

g03eac 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 function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

## 9Further Comments

g03ecc 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 (g03eace.c)

### 10.2Program Data

Program Data (g03eace.d)

### 10.3Program Results

Program Results (g03eace.r)