# NAG Library Routine Document

## 1Purpose

g05pzf generates a random two-way table.

## 2Specification

Fortran Interface
 Subroutine g05pzf ( mode, nrow, ncol, totr, totc, r, lr, x, ldx,
 Integer, Intent (In) :: mode, nrow, ncol, totr(nrow), totc(ncol), lr, ldx Integer, Intent (Inout) :: state(*), x(ldx,ncol), ifail Real (Kind=nag_wp), Intent (Inout) :: r(lr)
#include nagmk26.h
 void g05pzf_ (const Integer *mode, const Integer *nrow, const Integer *ncol, const Integer totr[], const Integer totc[], double r[], const Integer *lr, Integer state[], Integer x[], const Integer *ldx, Integer *ifail)

## 3Description

Given $m$ row totals ${R}_{i}$ and $n$ column totals ${C}_{j}$ (with $\sum _{i=1}^{m}{R}_{i}=\sum _{j=1}^{n}{C}_{j}=T$, say), g05pzf will generate a pseudorandom two-way table of integers such that the row and column totals are satisfied.
The method used is based on that described by Patefield (1981) which is most efficient when $T$ is large relative to the number of table entries $m×n$ (i.e., $T>2mn$). Entries are generated one row at a time and one entry at a time within a row. Each entry is generated using the conditional probability distribution for that entry given the entries in the previous rows and the previous entries in the same row.
A reference vector is used to store computed values that can be reused in the generation of new tables with the same row and column totals. g05pzf can be called to simply set up the reference vector, or to generate a two-way table using a reference vector set up in a previous call, or it can combine both functions in a single call.
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 g05pzf.
Patefield W M (1981) An efficient method of generating $R×C$ tables with given row and column totals Appl. Stats. 30 91–97

## 5Arguments

1:     $\mathbf{mode}$ – IntegerInput
On entry: a code for selecting the operation to be performed by the routine.
${\mathbf{mode}}=0$
Set up reference vector only.
${\mathbf{mode}}=1$
Generate two-way table using reference vector set up in a prior call to g05pzf.
${\mathbf{mode}}=2$
Set up reference vector and generate two-way table.
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
2:     $\mathbf{nrow}$ – IntegerInput
On entry: $m$, the number of rows in the table.
Constraint: ${\mathbf{nrow}}\ge 2$.
3:     $\mathbf{ncol}$ – IntegerInput
On entry: $n$, the number of columns in the table.
Constraint: ${\mathbf{ncol}}\ge 2$.
4:     $\mathbf{totr}\left({\mathbf{nrow}}\right)$ – Integer arrayInput
On entry: the $m$ row totals, ${R}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,m$.
Constraints:
• ${\mathbf{totr}}\left(\mathit{i}\right)\ge 0$, for $\mathit{i}=1,2,\dots ,m$;
• $\sum _{i=1}^{m}{\mathbf{totr}}\left(i\right)=\sum _{j=1}^{n}{\mathbf{totc}}\left(j\right)$;
• ${\sum }_{\mathit{i}}{\mathbf{totr}}\left(\mathit{i}\right)>0$, for $\mathit{i}=1,2,\dots ,m$.
5:     $\mathbf{totc}\left({\mathbf{ncol}}\right)$ – Integer arrayInput
On entry: the $n$ column totals, ${C}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,n$.
Constraints:
• ${\mathbf{totc}}\left(\mathit{j}\right)\ge 0$, for $\mathit{j}=1,2,\dots ,n$;
• $\sum _{j=1}^{n}{\mathbf{totc}}\left(j\right)=\sum _{i=1}^{m}{\mathbf{totr}}\left(i\right)$.
6:     $\mathbf{r}\left({\mathbf{lr}}\right)$ – Real (Kind=nag_wp) arrayCommunication Array
On entry: if ${\mathbf{mode}}=1$, the reference vector from the previous call to g05pzf.
On exit: the reference vector.
7:     $\mathbf{lr}$ – IntegerInput
On entry: the dimension of the array r as declared in the (sub)program from which g05pzf is called.
Constraint: ${\mathbf{lr}}\ge \sum _{i=1}^{m}{\mathbf{totr}}\left(i\right)+5$.
8:     $\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.
9:     $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{ncol}}\right)$ – Integer arrayOutput
On exit: if ${\mathbf{mode}}=1$ or $2$, a pseudorandom two-way $m$ by $n$ table, $X$, with element ${\mathbf{x}}\left(i,j\right)$ containing the $\left(i,j\right)$th entry in the table such that $\sum _{\mathit{i}=1}^{m}{\mathbf{x}}\left(i,j\right)={\mathbf{totc}}\left(j\right)$ and $\sum _{\mathit{j}=1}^{n}{\mathbf{x}}\left(i,j\right)={\mathbf{totr}}\left(i\right)$
10:   $\mathbf{ldx}$ – IntegerInput
On entry: the first dimension of the array x as declared in the (sub)program from which g05pzf is called.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{nrow}}$.
11:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. 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 $-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 argument, 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).

## 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, ${\mathbf{mode}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{nrow}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nrow}}\ge 2$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{ncol}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ncol}}\ge 2$.
${\mathbf{ifail}}=4$
On entry, at least one element of totr is negative or totr sums to zero.
${\mathbf{ifail}}=5$
On entry, totc has at least one negative element.
${\mathbf{ifail}}=6$
nrow or ncol is not the same as when r was set up in a previous call.
Previous value of ${\mathbf{nrow}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nrow}}=〈\mathit{\text{value}}〉$.
Previous value of ${\mathbf{ncol}}=〈\mathit{\text{value}}〉$ and ${\mathbf{ncol}}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=7$
On entry, lr is not large enough, ${\mathbf{lr}}=〈\mathit{\text{value}}〉$: minimum length required $\text{}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=8$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=10$
On entry, ${\mathbf{ldx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nrow}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{nrow}}$.
${\mathbf{ifail}}=15$
On entry, the arrays totr and totc do not sum to the same total: totr array total is $〈\mathit{\text{value}}〉$, totc array total is $〈\mathit{\text{value}}〉$.
${\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.

None.

## 8Parallelism and Performance

g05pzf 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 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, this example generates and prints a $4$ by $3$ two-way table, with row totals of $9$, $11$, $7$ and $23$ respectively, and column totals of $16$, $17$ and $17$ respectively.

### 10.1Program Text

Program Text (g05pzfe.f90)

### 10.2Program Data

Program Data (g05pzfe.d)

### 10.3Program Results

Program Results (g05pzfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017