G05 Chapter Contents
G05 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG05PXF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G05PXF generates a random orthogonal matrix.

## 2  Specification

 SUBROUTINE G05PXF ( SIDE, INIT, M, N, STATE, A, LDA, IFAIL)
 INTEGER M, N, STATE(*), LDA, IFAIL REAL (KIND=nag_wp) A(LDA,N) CHARACTER(1) SIDE, INIT

## 3  Description

G05PXF pre- or post-multiplies an $m$ by $n$ matrix $A$ by a random orthogonal matrix $U$, overwriting $A$. The matrix $A$ may optionally be initialized to the identity matrix before multiplying by $U$, hence $U$ is returned. $U$ is generated using the method of Stewart (1980). The algorithm can be summarised as follows.
Let ${x}_{1},{x}_{2},\dots ,{x}_{n-1}$ follow independent multinormal distributions with zero mean and variance $I{\sigma }^{2}$ and dimensions $n,n-1,\dots ,2$; let ${H}_{j}=\mathrm{diag}\left({I}_{j-1},{H}_{j}^{*}\right)$, where ${I}_{j-1}$ is the identity matrix and ${H}_{j}^{*}$ is the Householder transformation that reduces ${x}_{j}$ to ${r}_{jj}{e}_{1}$, ${e}_{1}$ being the vector with first element one and the remaining elements zero and ${r}_{jj}$ being a scalar, and let $D=\mathrm{diag}\left(\mathrm{sign}\left({r}_{11}\right),\mathrm{sign}\left({r}_{22}\right),\dots ,\mathrm{sign}\left({r}_{nn}\right)\right)$. Then the product $U=D{H}_{1}{H}_{2}\dots {H}_{n-1}$ is a random orthogonal matrix distributed according to the Haar measure over the set of orthogonal matrices of $n$. See Theorem 3.3 in Stewart (1980).
One of the initialization routines G05KFF (for a repeatable sequence if computed sequentially) or G05KGF (for a non-repeatable sequence) must be called prior to the first call to G05PXF.
Stewart G W (1980) The efficient generation of random orthogonal matrices with an application to condition estimates SIAM J. Numer. Anal. 17 403–409

## 5  Parameters

1:     SIDE – CHARACTER(1)Input
On entry: indicates whether the matrix $A$ is multiplied on the left or right by the random orthogonal matrix $U$.
${\mathbf{SIDE}}=\text{'L'}$
The matrix $A$ is multiplied on the left, i.e., premultiplied.
${\mathbf{SIDE}}=\text{'R'}$
The matrix $A$ is multiplied on the right, i.e., post-multiplied.
Constraint: ${\mathbf{SIDE}}=\text{'L'}$ or $\text{'R'}$.
2:     INIT – CHARACTER(1)Input
On entry: indicates whether or not A should be initialized to the identity matrix.
${\mathbf{INIT}}=\text{'I'}$
A is initialized to the identity matrix.
${\mathbf{INIT}}=\text{'N'}$
A is not initialized and the matrix $A$ must be supplied in A.
Constraint: ${\mathbf{INIT}}=\text{'I'}$ or $\text{'N'}$.
3:     M – INTEGERInput
On entry: $m$, the number of rows of the matrix $A$.
Constraints:
• if ${\mathbf{SIDE}}=\text{'L'}$, ${\mathbf{M}}>1$;
• otherwise ${\mathbf{M}}\ge 1$.
4:     N – INTEGERInput
On entry: $n$, the number of columns of the matrix $A$.
Constraints:
• if ${\mathbf{SIDE}}=\text{'R'}$, ${\mathbf{N}}>1$;
• otherwise ${\mathbf{N}}\ge 1$.
5:     STATE($*$) – INTEGER arrayCommunication Array
Note: the actual argument supplied must be the array STATE supplied to the initialization routines G05KFF or G05KGF.
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
6:     A(LDA,N) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{INIT}}=\text{'N'}$, A must contain the matrix $A$.
On exit: the matrix $UA$ when ${\mathbf{SIDE}}=\text{'L'}$ or the matrix $AU$ when ${\mathbf{SIDE}}=\text{'R'}$.
7:     LDA – INTEGERInput
On entry: the first dimension of the array A as declared in the (sub)program from which G05PXF is called.
Constraint: ${\mathbf{LDA}}\ge {\mathbf{M}}$.
8:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ 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 parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{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{SIDE}}\ne \text{'L'}$ or $\text{'R'}$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{INIT}}\ne \text{'I'}$ or $\text{'N'}$.
${\mathbf{IFAIL}}=3$
 On entry, ${\mathbf{M}}<1$.
${\mathbf{IFAIL}}=4$
 On entry, ${\mathbf{N}}<1$.
${\mathbf{IFAIL}}=5$
 On entry, STATE vector was not initialized or has been corrupted.
${\mathbf{IFAIL}}=7$
 On entry, ${\mathbf{LDA}}<{\mathbf{M}}$.
${\mathbf{IFAIL}}=8$
On entry, an orthogonal matrix of dimension $1$ has been requested.

## 7  Accuracy

The maximum error in ${U}^{\mathrm{T}}U$ should be a modest multiple of machine precision (see Chapter X02).

None.

## 9  Example

Following initialization of the pseudorandom number generator by a call to G05KFF, a $4$ by $4$ orthogonal matrix is generated using the ${\mathbf{INIT}}=\text{'I'}$ option and the result printed.

### 9.1  Program Text

Program Text (g05pxfe.f90)

### 9.2  Program Data

Program Data (g05pxfe.d)

### 9.3  Program Results

Program Results (g05pxfe.r)