# NAG Library Routine Document

## 1Purpose

g05pxf generates a random orthogonal matrix.

## 2Specification

Fortran Interface
 Subroutine g05pxf ( side, init, m, n, a, lda,
 Integer, Intent (In) :: m, n, lda Integer, Intent (Inout) :: state(*), ifail Real (Kind=nag_wp), Intent (Inout) :: a(lda,n) Character (1), Intent (In) :: side, init
#include <nagmk26.h>
 void g05pxf_ (const char *side, const char *init, const Integer *m, const Integer *n, Integer state[], double a[], const Integer *lda, Integer *ifail, const Charlen length_side, const Charlen length_init)

## 3Description

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

## 5Arguments

1:     $\mathbf{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:     $\mathbf{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:     $\mathbf{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:     $\mathbf{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:     $\mathbf{state}\left(*\right)$ – 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:     $\mathbf{a}\left({\mathbf{lda}},{\mathbf{n}}\right)$ – 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:     $\mathbf{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:     $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value  is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value  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, side is not valid: ${\mathbf{side}}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=2$
On entry, init is not valid: ${\mathbf{init}}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{side}}=〈\mathit{\text{value}}〉$, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{side}}=\text{'L'}$, ${\mathbf{m}}>1$; otherwise ${\mathbf{m}}\ge 1$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{side}}=〈\mathit{\text{value}}〉$, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{side}}=\text{'R'}$, ${\mathbf{n}}>1$; otherwise ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=5$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=7$
On entry, ${\mathbf{lda}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lda}}\ge {\mathbf{m}}$.
${\mathbf{ifail}}=8$
On entry, ${\mathbf{side}}=〈\mathit{\text{value}}〉$, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{side}}=\text{'L'}$, ${\mathbf{m}}>1$; otherwise ${\mathbf{m}}\ge 1$.
On entry, ${\mathbf{side}}=〈\mathit{\text{value}}〉$, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{side}}=\text{'R'}$, ${\mathbf{n}}>1$; otherwise ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

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

## 8Parallelism and Performance

g05pxf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g05pxf 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.

None.

## 10Example

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.

### 10.1Program Text

Program Text (g05pxfe.f90)

### 10.2Program Data

Program Data (g05pxfe.d)

### 10.3Program Results

Program Results (g05pxfe.r)