# NAG CL Interfaceg03bcc (rot_​procrustes)

Settings help

CL Name Style:

## 1Purpose

g03bcc computes Procrustes rotations in which an orthogonal rotation is found so that a transformed matrix best matches a target matrix.

## 2Specification

 #include
 void g03bcc (Nag_TransNorm stand, Nag_RotationScale pscale, Integer n, Integer m, double x[], Integer tdx, double y[], Integer tdy, double yhat[], double r[], Integer tdr, double *alpha, double *rss, double res[], NagError *fail)
The function may be called by the names: g03bcc, nag_mv_rot_procrustes or nag_mv_procustes.

## 3Description

Let $X$ and $Y$ be $n×m$ matrices. They can be considered as representing sets of $n$ points in an $m$-dimensional space. The $X$ matrix may be a matrix of loadings from say factor or canonical variate analysis, and the $Y$ matrix may be a postulated pattern matrix or the loadings from a different sample. The problem is to relate the two sets of points without disturbing the relationships between the points in each set. This can be achieved by translating, rotating and scaling the sets of points. The $Y$ matrix is considered as the target matrix and the $X$ matrix is rotated to match that matrix.
First the two sets of points are translated so that their centroids are at the origin to give ${X}_{c}$ and ${Y}_{c}$, i.e., the matrices will have zero column means. Then the rotation of the translated ${X}_{c}$ matrix which minimizes the sum of squared distances between corresponding points in the two sets is found. This is computed from the singular value decomposition of the matrix:
 $XcT Y c = UDVT ,$
where $U$ and $V$ are orthogonal matrices and $D$ is a diagonal matrix. The matrix of rotations, $R$, is computed as:
 $R = UVT .$
After rotation, a scaling or dilation factor, $\alpha$, may be estimated by least squares. Thus, the final set of points that best match ${Y}_{c}$ is given by:
 $Y ^ c = α X c R .$
Before rotation, both sets of points may be normalized to have unit sums of squares or the $X$ matrix may be normalized to have the same sum of squares as the $Y$ matrix. After rotation, the results may be translated to the original $Y$ centroid.
The $i$th residual, ${r}_{i}$, is given by the distance between the point given in the $i$th row of $Y$ and the point given in the $i$th row of $\stackrel{^}{Y}$. The residual sum of squares is also computed.

## 4References

Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press
Lawley D N and Maxwell A E (1971) Factor Analysis as a Statistical Method (2nd Edition) Butterworths

## 5Arguments

1: $\mathbf{stand}$Nag_TransNorm Input
On entry: indicates if translation/normalization is required.
${\mathbf{stand}}=\mathrm{Nag_NoTransNorm}$
No translation or normalization.
${\mathbf{stand}}=\mathrm{Nag_Orig}$
Translation to the origin.
${\mathbf{stand}}=\mathrm{Nag_OrigCentroid}$
Translation to the origin and then to the $Y$ centroid after rotation.
${\mathbf{stand}}=\mathrm{Nag_Norm}$
Unit normalization.
${\mathbf{stand}}=\mathrm{Nag_OrigNorm}$
Translation and normalization.
${\mathbf{stand}}=\mathrm{Nag_OrigNormCentroid}$
Translation and normalization to $Y$ scale, then translation to the $Y$ centroid after rotation.
Constraint: ${\mathbf{stand}}=\mathrm{Nag_NoTransNorm}$, $\mathrm{Nag_Orig}$, $\mathrm{Nag_OrigCentroid}$, $\mathrm{Nag_Norm}$, $\mathrm{Nag_OrigNorm}$ or $\mathrm{Nag_OrigNormCentroid}$.
2: $\mathbf{pscale}$Nag_RotationScale Input
On entry: indicates if least squares scaling is applied after rotation.
${\mathbf{pscale}}=\mathrm{Nag_LsqScale}$
Scaling is to be applied.
${\mathbf{pscale}}=\mathrm{Nag_NotLsqScale}$
No scaling is applied.
Constraint: ${\mathbf{pscale}}=\mathrm{Nag_LsqScale}$ or $\mathrm{Nag_NotLsqScale}$.
3: $\mathbf{n}$Integer Input
On entry: the number of points, $n$.
Constraint: ${\mathbf{n}}\ge 1$.
4: $\mathbf{m}$Integer Input
On entry: the number of dimensions, $m$.
Constraints:
• ${\mathbf{m}}\ge 1$;
• ${\mathbf{m}}\le {\mathbf{n}}$.
5: $\mathbf{x}\left[{\mathbf{n}}×{\mathbf{tdx}}\right]$double Input/Output
Note: the $\left(i,j\right)$th element of the matrix $X$ is stored in ${\mathbf{x}}\left[\left(i-1\right)×{\mathbf{tdx}}+j-1\right]$.
On entry: the matrix to be rotated, $X$.
On exit: if ${\mathbf{stand}}=\mathrm{Nag_NoTransNorm}$, x will be unchanged.
If ${\mathbf{stand}}=\mathrm{Nag_Orig}$, $\mathrm{Nag_OrigCentroid}$, $\mathrm{Nag_OrigNorm}$ or $\mathrm{Nag_OrigNormCentroid}$, x will be translated to have zero column means.
If ${\mathbf{stand}}=\mathrm{Nag_Norm}$ or $\mathrm{Nag_OrigNorm}$, x will be scaled to have unit sum of squares.
If ${\mathbf{stand}}=\mathrm{Nag_OrigNormCentroid}$, x will be scaled to have the same sum of squares as y.
6: $\mathbf{tdx}$Integer Input
On entry: the stride separating matrix column elements in the array x.
Constraint: ${\mathbf{tdx}}\ge {\mathbf{m}}$.
7: $\mathbf{y}\left[{\mathbf{n}}×{\mathbf{tdy}}\right]$double Input/Output
Note: the $\left(i,j\right)$th element of the matrix $Y$ is stored in ${\mathbf{y}}\left[\left(i-1\right)×{\mathbf{tdy}}+j-1\right]$.
On entry: the target matrix, $Y$.
On exit: if ${\mathbf{stand}}=\mathrm{Nag_NoTransNorm}$, then y will be unchanged.
If ${\mathbf{stand}}=\mathrm{Nag_Orig}$ or $\mathrm{Nag_OrigNorm}$, then y will be translated to have zero column means.
If ${\mathbf{stand}}=\mathrm{Nag_Norm}$ or $\mathrm{Nag_OrigNorm}$, then y will be scaled to have unit sum of squares.
If ${\mathbf{stand}}=\mathrm{Nag_OrigCentroid}$ or $\mathrm{Nag_OrigNormCentroid}$, then y will be translated and then after rotation, translated back. The output y should be the same as the input y except for rounding errors.
8: $\mathbf{tdy}$Integer Input
On entry: the stride separating matrix column elements in the arrays y, yhat.
Constraint: ${\mathbf{tdy}}\ge {\mathbf{m}}$.
9: $\mathbf{yhat}\left[{\mathbf{n}}×{\mathbf{tdy}}\right]$double Output
Note: the $\left(i,j\right)$th element of the matrix is stored in ${\mathbf{yhat}}\left[\left(i-1\right)×{\mathbf{tdy}}+j-1\right]$.
On exit: the fitted matrix, $\stackrel{^}{Y}$.
10: $\mathbf{r}\left[{\mathbf{m}}×{\mathbf{tdr}}\right]$double Output
Note: the $\left(i,j\right)$th element of the matrix $R$ is stored in ${\mathbf{r}}\left[\left(i-1\right)×{\mathbf{tdr}}+j-1\right]$.
On exit: the matrix of rotations, $R$, see Section 9.
11: $\mathbf{tdr}$Integer Input
On entry: the stride separating matrix column elements in the array r.
Constraint: ${\mathbf{tdr}}\ge {\mathbf{m}}$.
12: $\mathbf{alpha}$double * Output
On exit: if ${\mathbf{pscale}}=\mathrm{Nag_LsqScale}$ the scaling factor, $\alpha$; otherwise alpha is not set.
13: $\mathbf{rss}$double * Output
On exit: the residual sum of squares.
14: $\mathbf{res}\left[{\mathbf{n}}\right]$double Output
On exit: the residuals, ${r}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
15: $\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_GT
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{m}}\le {\mathbf{n}}$.
NE_2_INT_ARG_LT
On entry, ${\mathbf{tdr}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{tdr}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{tdx}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{tdx}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{tdy}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{tdy}}\ge {\mathbf{m}}$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument pscale had an illegal value.
On entry, argument stand had an illegal value.
NE_INT_ARG_LT
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_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_LSQ_SCAL_ZERO_PTS
The fitted matrix $\stackrel{^}{Y}\text{,}$ contains only zero-points when least squares scaling is applied.
NE_NORM_ZERO_PTS
On entry, either x or y contains only zero-points (possibly after translation) when normalization is to be applied.
NE_SVD_NOT_CONV
The singular value decomposition has failed to converge. This is an unlikely error exit.

## 7Accuracy

The accuracy of the calculation of the rotation matrix largely depends upon the singular value decomposition. See the F08 Chapter Introduction for further details.

## 8Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
g03bcc is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
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.

Note that if the matrix ${X}_{c}^{\mathrm{T}}Y$ is not of full rank, then the matrix of rotations, $R$, may not be unique even if there is a unique solution in terms of the rotated matrix, ${\stackrel{^}{Y}}_{c}$. The matrix $R$ may also include reflections as well as pure rotations, see Krzanowski (1990).
If the column dimensions of the $X$ and $Y$ matrices are not equal, the smaller of the two should be supplemented by columns of zeros. Adding a column of zeros to both $X$ and $Y$ will have the effect of allowing reflections as well as rotations.

## 10Example

Three points representing the vertices of a triangle in two dimensions are input. The points are translated and rotated to match the triangle given by (0,0),(1,0),(0,2) and scaling is applied after rotation. The target matrix and fitted matrix are printed along with additional information.

### 10.1Program Text

Program Text (g03bcce.c)

### 10.2Program Data

Program Data (g03bcce.d)

### 10.3Program Results

Program Results (g03bcce.r)