NAG C Library Function Document

nag_rand_orthog_matrix (g05pxc)


nag_rand_orthog_matrix (g05pxc) generates a random orthogonal matrix.


#include <nag.h>
#include <nagg05.h>
void  nag_rand_orthog_matrix (Nag_SideType side, Nag_InitializeA init, Integer m, Integer n, Integer state[], double a[], Integer pda, NagError *fail)


nag_rand_orthog_matrix (g05pxc) 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 x1,x2,,xn-1 follow independent multinormal distributions with zero mean and variance Iσ2 and dimensions n,n-1,,2; let Hj=diagIj-1, Hj*, where Ij-1 is the identity matrix and Hj* is the Householder transformation that reduces xj to rjje1, e1 being the vector with first element one and the remaining elements zero and rjj being a scalar, and let D=diagsignr11,signr22,,signrnn. Then the product U=DH1H2Hn-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 functions nag_rand_init_repeatable (g05kfc) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeatable (g05kgc) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_orthog_matrix (g05pxc).


Stewart G W (1980) The efficient generation of random orthogonal matrices with an application to condition estimates SIAM J. Numer. Anal. 17 403–409


1:     side Nag_SideTypeInput
On entry: indicates whether the matrix A is multiplied on the left or right by the random orthogonal matrix U.
The matrix A is multiplied on the left, i.e., premultiplied.
The matrix A is multiplied on the right, i.e., post-multiplied.
Constraint: side=Nag_LeftSide or Nag_RightSide.
2:     init Nag_InitializeAInput
On entry: indicates whether or not a should be initialized to the identity matrix.
a is initialized to the identity matrix.
a is not initialized and the matrix A must be supplied in a.
Constraint: init=Nag_InitializeI or Nag_InputA.
3:     m IntegerInput
On entry: m, the number of rows of the matrix A.
  • if side=Nag_LeftSide, m>1;
  • otherwise m1.
4:     n IntegerInput
On entry: n, the number of columns of the matrix A.
  • if side=Nag_RightSide, n>1;
  • otherwise n1.
5:     state[dim] IntegerCommunication Array
Note: the dimension, dim, of this array is dictated by the requirements of associated functions that must have been previously called. This array MUST be the same array passed as argument state in the previous call to nag_rand_init_repeatable (g05kfc) or nag_rand_init_nonrepeatable (g05kgc).
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[m×pda] doubleInput/Output
On entry: if init=Nag_InputA, a must contain the matrix A, with the i,jth element of A stored in a[i-1×pda+j-1].
On exit: the matrix UA when side=Nag_LeftSide or the matrix A U when side=Nag_RightSide.
7:     pda IntegerInput
On entry: the stride separating matrix column elements in the array a.
Constraint: pdan.
8:     fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

Error Indicators and Warnings

Dynamic memory allocation failed.
See Section in How to Use the NAG Library and its Documentation for further information.
On entry, argument value had an illegal value.
On entry, side=value and m=value.
Constraint: if side=Nag_LeftSide, m>1;
otherwise m1.
On entry, side=value and n=value.
Constraint: if side=Nag_RightSide, n>1;
otherwise n1.
On entry, pda=value.
Constraint: pda>0.
On entry, pda=value and n=value.
Constraint: pdan.
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 2.7.6 in How to Use the NAG Library and its Documentation for further information.
On entry, state vector has been corrupted or not initialized.
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.


The maximum error in UT U should be a modest multiple of machine precision (see Chapter x02).

Parallelism and Performance

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

Further Comments



Following initialization of the pseudorandom number generator by a call to nag_rand_init_repeatable (g05kfc), a 4 by 4 orthogonal matrix is generated using the init=Nag_InitializeI option and the result printed.

Program Text

Program Text (g05pxce.c)

Program Data


Program Results

Program Results (g05pxce.r)