# NAG CL Interfaceg03ebc (distance_​mat_​2)

Settings help

CL Name Style:

## 1Purpose

g03ebc computes a distance (dissimilarity) matrix between two sets of observations.

## 2Specification

 #include
 void g03ebc (Nag_MatUpdate update, Nag_VarScaleType scal, Nag_VarScaleYType stype, double p, Integer m, Integer n, Integer l, const double x[], Integer pdx, const Integer isv[], const double y[], Integer pdy, double sx[], double sy[], double d[], NagError *fail)
The function may be called by the names: g03ebc or nag_mv_distance_mat_2.

## 3Description

Given two sets of observations on $l$ variables, a distance matrix is such that the $ij$th element represents how far apart or how dissimilar the $i$th observation from the first set and the $j$th observation from the second set are.
Let $X$ and $Y$ be $m×l$ and $n×l$ data matrices of $m$ and $n$ observations, respectively, on $l$ variables. The distance between observation $i$ from $X$ and observation $j$ from $Y$, ${d}_{ij}$, is most commonly defined in terms of the scaled Minkowski $p$-norm:
 $dij= { ∑k=1p (|xik/sxi-yjk/syj|) p } 1/p ,$
where ${x}_{ik}$ and ${y}_{jk}$ are the $ik$th and $jk$th elements of $X$ and $Y$ respectively, ${\mathrm{sx}}_{i}$ is a standardization for the $i$th variable in $X$, ${\mathrm{sy}}_{j}$ is a standardization for the $j$th variable in $Y$, and $p$ is the order of the Minkowski norm.
Three standardizations (scalings) for the variables are available.
1. (a)Standard deviation: ${\mathrm{sx}}_{i}=\sqrt{\sum _{k=1}^{n}{\left({x}_{ji}-\overline{x}\right)}^{2}/\left(n-1\right)}$
2. (b)Range: ${\mathrm{sx}}_{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 ${\mathrm{sx}}_{i}$.
In addition to the Minkowski measure 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 g03ebc 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.
g03ebc returns the full rectangular 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}$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{scal}$Nag_VarScaleType Input
On entry: indicates the standardization of the variables to be used.
${\mathbf{scal}}=\mathrm{Nag_VarScaleStd}$
Standard deviation.
${\mathbf{scal}}=\mathrm{Nag_VarScaleRange}$
Range.
${\mathbf{scal}}=\mathrm{Nag_VarScaleUser}$
Standardizations given in array sx (and posibly sy).
${\mathbf{scal}}=\mathrm{Nag_NoVarScale}$
Unscaled.
Constraint: ${\mathbf{scal}}=\mathrm{Nag_VarScaleStd}$, $\mathrm{Nag_VarScaleRange}$, $\mathrm{Nag_VarScaleUser}$ or $\mathrm{Nag_NoVarScale}$.
3: $\mathbf{stype}$Nag_VarScaleYType Input
On entry: indicates how the standardization of the variables treats the two sets of observations.
${\mathbf{stype}}=\mathrm{Nag_VarScaleAmalg}$
Amalgamated.
${\mathbf{stype}}=\mathrm{Nag_VarScaleIndep}$
Independent.
${\mathbf{stype}}=\mathrm{Nag_VarScaleX}$
Standardization is based purley on observations in x.
Constraint: ${\mathbf{stype}}=\mathrm{Nag_VarScaleAmalg}$, $\mathrm{Nag_VarScaleIndep}$ or $\mathrm{Nag_VarScaleX}$.
4: $\mathbf{p}$double Input
On entry: the order $p$ of the Minkowski distance metric.
Constraint: ${\mathbf{p}}\ge 1.0$.
5: $\mathbf{m}$Integer Input
On entry: $m$, the number of observations in the data array x.
Constraint: ${\mathbf{m}}\ge 1$.
6: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations in the data array y.
Constraint: ${\mathbf{n}}\ge 1$.
7: $\mathbf{l}$Integer Input
On entry: l, the total number of variables in arrays x and y.
Constraint: ${\mathbf{l}}>0$.
8: $\mathbf{x}\left[{\mathbf{pdx}}×{\mathbf{l}}\right]$const double Input
Note: the $\left(i,j\right)$th element of the matrix $X$ is stored in ${\mathbf{x}}\left[\left(j-1\right)×{\mathbf{pdx}}+i-1\right]$.
On entry: ${\mathbf{x}}\left[\left(\mathit{k}-1\right)×{\mathbf{pdx}}+\mathit{i}-1\right]$ must contain the value of the $\mathit{k}$th variable for the $\mathit{i}$th observation in the first set of observations, for $\mathit{i}=1,2,\dots ,m$ and $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
9: $\mathbf{pdx}$Integer Input
On entry: the stride separating matrix row elements in the array x.
Constraint: ${\mathbf{pdx}}\ge {\mathbf{m}}$.
10: $\mathbf{isv}\left[{\mathbf{l}}\right]$const Integer Input
On entry: ${\mathbf{isv}}\left[j-1\right]$ indicates whether or not the $j$th variable in x and y is to be included in the distance computations.
If ${\mathbf{isv}}\left[\mathit{k}-1\right]=0$ the $\mathit{j}$th variable is not included., for $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
If ${\mathbf{isv}}\left[\mathit{k}-1\right]\ge 1$ the $\mathit{j}$th variable is included, for $\mathit{k}=1,2,\dots ,{\mathbf{l}}$
Constraint: ${\mathbf{isv}}\left[\mathit{j}-1\right]>0$ for at least one $\mathit{j}$, for $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
11: $\mathbf{y}\left[{\mathbf{pdy}}×{\mathbf{l}}\right]$const double Input
Note: the $\left(i,j\right)$th element of the matrix $Y$ is stored in ${\mathbf{y}}\left[\left(j-1\right)×{\mathbf{pdy}}+i-1\right]$.
On entry: ${\mathbf{y}}\left[\left(\mathit{k}-1\right)×{\mathbf{pdy}}+\mathit{j}-1\right]$ must contain the value of the $\mathit{k}$th variable for the $\mathit{j}$th observation in the second set of observations, for $\mathit{j}=1,2,\dots ,n$ and $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
12: $\mathbf{pdy}$Integer Input
On entry: the stride separating matrix row elements in the array y.
Constraint: ${\mathbf{pdy}}\ge {\mathbf{n}}$.
13: $\mathbf{sx}\left[{\mathbf{l}}\right]$double Input/Output
On entry: if ${\mathbf{scal}}=\mathrm{Nag_VarScaleUser}$ and ${\mathbf{isv}}\left[\mathit{k}-1\right]>0$ then ${\mathbf{sx}}\left[\mathit{k}-1\right]$ must contain the scaling for variable $\mathit{k}$, for $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
Constraint: if ${\mathbf{scal}}=\mathrm{Nag_VarScaleUser}$ and ${\mathbf{isv}}\left[k\right]>0$, ${\mathbf{sx}}\left[\mathit{k}\right]>0.0$, for $\mathit{k}=0,1,\dots ,{\mathbf{l}}-1$.
On exit: if ${\mathbf{scal}}=\mathrm{Nag_VarScaleStd}$ and ${\mathbf{isv}}\left[k-1\right]>0$ then ${\mathbf{sx}}\left[k-1\right]$ contains the standard deviation of the variable in the $k$th column of x.
If ${\mathbf{scal}}=\mathrm{Nag_VarScaleRange}$ and ${\mathbf{isv}}\left[k-1\right]>0$, ${\mathbf{sx}}\left[k-1\right]$ contains the range of the variable in the $j$th column of x.
If ${\mathbf{scal}}=\mathrm{Nag_NoVarScale}$ and ${\mathbf{isv}}\left[k-1\right]>0$, ${\mathbf{sx}}\left[k-1\right]=1.0$.
If ${\mathbf{scal}}=\mathrm{Nag_VarScaleUser}$, sx is unchanged.
14: $\mathbf{sy}\left[\mathit{dim}\right]$double Input/Output
Note: the dimension, dim, of the array sy must be at least
• ${\mathbf{l}}$ when ${\mathbf{stype}}=\mathrm{Nag_VarScaleIndep}$ and ${\mathbf{scal}}\ne \mathrm{Nag_NoVarScale}$;
• otherwise sy may be NULL.
On entry: if ${\mathbf{scal}}=\mathrm{Nag_VarScaleUser}$ and ${\mathbf{stype}}=\mathrm{Nag_VarScaleIndep}$ and ${\mathbf{isv}}\left[\mathit{k}-1\right]>0$ then ${\mathbf{sy}}\left[\mathit{k}-1\right]$ must contain the scaling for variable $\mathit{k}$ , for $\mathit{k}=1,2,\dots ,{\mathbf{l}}$.
If ${\mathbf{stype}}\ne \mathrm{Nag_VarScaleIndep}$, or ${\mathbf{scal}}=\mathrm{Nag_NoVarScale}$ then sy is not referenced and may be NULL.
Constraint: if ${\mathbf{scal}}=\mathrm{Nag_VarScaleUser}$ and ${\mathbf{stype}}=\mathrm{Nag_VarScaleIndep}$ and ${\mathbf{isv}}\left[k\right]>0$, ${\mathbf{sy}}\left[\mathit{k}\right]>0.0$, for $\mathit{k}=0,1,\dots ,{\mathbf{l}}-1$.
On exit: if ${\mathbf{scal}}=\mathrm{Nag_VarScaleStd}$ and ${\mathbf{stype}}=\mathrm{Nag_VarScaleIndep}$ and ${\mathbf{isv}}\left[k-1\right]>0$ then ${\mathbf{sy}}\left[k-1\right]$ contains the standard deviation of the variable in the $k$th column of x.
If ${\mathbf{scal}}=\mathrm{Nag_VarScaleRange}$ and ${\mathbf{stype}}=\mathrm{Nag_VarScaleIndep}$ and ${\mathbf{isv}}\left[k-1\right]>0$, ${\mathbf{sy}}\left[k-1\right]$ contains the range of the variable in the $j$th column of x.
If ${\mathbf{scal}}=\mathrm{Nag_NoVarScale}$, sy is unchanged.
If ${\mathbf{scal}}=\mathrm{Nag_VarScaleUser}$, sy is unchanged.
15: $\mathbf{d}\left[{\mathbf{m}}×{\mathbf{n}}\right]$double Input/Output
Note: the $\left(i,j\right)$th element of the matrix $D$ is stored in ${\mathbf{d}}\left[\left(j-1\right)×{\mathbf{m}}+i-1\right]$.
On entry: the $m×n$ distance matrix $D$.
If ${\mathbf{update}}=\mathrm{Nag_MatUp}$, d need not be set.
On exit: the (possibly updated) distance matrix $D$.
16: $\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_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_ARRAY_CONS
On entry, at least one element of ${\mathbf{sx}}\le 0.0$.
On entry, at least one element of ${\mathbf{sx}}\le 0.0$ or ${\mathbf{sy}}\le 0.0$.
On entry, isv does not contain a positive element.
NE_BAD_PARAM
On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_CONST_COL
Variable $⟨\mathit{\text{value}}⟩$ is constant.
NE_INT
On entry, ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{l}}\ge 1$.
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\ge 1$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 1$.
NE_INT_2
On entry, ${\mathbf{pdx}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{pdx}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{pdy}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{pdy}}\ge {\mathbf{n}}$.
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.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NEG_ELEMENT
On entry, at least one element of ${\mathbf{d}}<0.0$.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_REAL
On entry, ${\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{p}}\ge 1.0$.

## 7Accuracy

The computations are believed to be stable.

## 8Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
g03ebc 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

When there is a large number of observations and these do not require scaling factors to be computed internally, then the distance matrix for all observations can be computed in stages by using g03ebc to systematically calculate the distance matrix for pairs of row blocks of observations. When there are a large number of variables, the distance matrix can be updated in stages using observations on blocks of variables at each update.

## 10Example

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

### 10.1Program Text

Program Text (g03ebce.c)

### 10.2Program Data

Program Data (g03ebce.d)

### 10.3Program Results

Program Results (g03ebce.r)