# NAG CL Interfaceg05pzc (matrix_​2waytable)

## 1Purpose

g05pzc generates a random two-way table.

## 2Specification

 #include
 void g05pzc (Nag_ModeRNG mode, Integer nrow, Integer ncol, const Integer totr[], const Integer totc[], double r[], Integer lr, Integer state[], Integer x[], Integer pdx, NagError *fail)
The function may be called by the names: g05pzc, nag_rand_matrix_2waytable or nag_rand_2_way_table.

## 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), g05pzc 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. g05pzc 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 functions g05kfc (for a repeatable sequence if computed sequentially) or g05kgc (for a non-repeatable sequence) must be called prior to the first call to g05pzc.

## 4References

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}$Nag_ModeRNG Input
On entry: a code for selecting the operation to be performed by the function.
${\mathbf{mode}}=\mathrm{Nag_InitializeReference}$
Set up reference vector only.
${\mathbf{mode}}=\mathrm{Nag_GenerateFromReference}$
Generate two-way table using reference vector set up in a prior call to g05pzc.
${\mathbf{mode}}=\mathrm{Nag_InitializeAndGenerate}$
Set up reference vector and generate two-way table.
Constraint: ${\mathbf{mode}}=\mathrm{Nag_InitializeReference}$, $\mathrm{Nag_GenerateFromReference}$ or $\mathrm{Nag_InitializeAndGenerate}$.
2: $\mathbf{nrow}$Integer Input
On entry: $m$, the number of rows in the table.
Constraint: ${\mathbf{nrow}}\ge 2$.
3: $\mathbf{ncol}$Integer Input
On entry: $n$, the number of columns in the table.
Constraint: ${\mathbf{ncol}}\ge 2$.
4: $\mathbf{totr}\left[{\mathbf{nrow}}\right]$const Integer Input
On entry: the $m$ row totals, ${R}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,m$.
Constraints:
• ${\mathbf{totr}}\left[\mathit{i}-1\right]\ge 0$, for $\mathit{i}=1,2,\dots ,m$;
• $\sum _{i=1}^{m}{\mathbf{totr}}\left[i-1\right]=\sum _{j=1}^{n}{\mathbf{totc}}\left[j-1\right]$;
• ${\sum }_{\mathit{i}}{\mathbf{totr}}\left[\mathit{i}-1\right]>0$, for $\mathit{i}=1,2,\dots ,m$.
5: $\mathbf{totc}\left[{\mathbf{ncol}}\right]$const Integer Input
On entry: the $n$ column totals, ${C}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,n$.
Constraints:
• ${\mathbf{totc}}\left[\mathit{j}-1\right]\ge 0$, for $\mathit{j}=1,2,\dots ,n$;
• $\sum _{j=1}^{n}{\mathbf{totc}}\left[j-1\right]=\sum _{i=1}^{m}{\mathbf{totr}}\left[i-1\right]$.
6: $\mathbf{r}\left[{\mathbf{lr}}\right]$double Communication Array
On entry: if ${\mathbf{mode}}=\mathrm{Nag_GenerateFromReference}$, the reference vector from the previous call to g05pzc.
On exit: the reference vector.
7: $\mathbf{lr}$Integer Input
On entry: the dimension of the array r.
Constraint: ${\mathbf{lr}}\ge \sum _{i=1}^{{\mathbf{nrow}}}{\mathbf{totr}}\left[i-1\right]+5$.
8: $\mathbf{state}\left[\mathit{dim}\right]$Integer Communication Array
Note: the dimension, $\mathit{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.
9: $\mathbf{x}\left[{\mathbf{nrow}}×{\mathbf{pdx}}\right]$Integer Output
On exit: if ${\mathbf{mode}}=\mathrm{Nag_GenerateFromReference}$ or $\mathrm{Nag_InitializeAndGenerate}$, a pseudorandom two-way $m$ by $n$ table, $X$, with element ${\mathbf{x}}\left[\left(i-1\right)×{\mathbf{pdx}}+j-1\right]$ containing the $\left(i,j\right)$th entry in the table such that $\sum _{\mathit{i}=1}^{m}{\mathbf{x}}\left[\left(i-1\right)×{\mathbf{pdx}}+j-1\right]={\mathbf{totc}}\left[j-1\right]$ and $\sum _{\mathit{j}=1}^{n}{\mathbf{x}}\left[\left(i-1\right)×{\mathbf{pdx}}+j-1\right]={\mathbf{totr}}\left[i-1\right]$
10: $\mathbf{pdx}$Integer Input
On entry: the stride separating matrix column elements in the array x.
Constraint: ${\mathbf{pdx}}\ge {\mathbf{ncol}}$.
11: $\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_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, lr is not large enough, ${\mathbf{lr}}=〈\mathit{\text{value}}〉$: minimum length required $\text{}=〈\mathit{\text{value}}〉$.
On entry, ${\mathbf{ncol}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ncol}}\ge 2$.
On entry, ${\mathbf{nrow}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nrow}}\ge 2$.
NE_INT_2
On entry, ${\mathbf{pdx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{ncol}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdx}}\ge {\mathbf{ncol}}$.
On entry, ${\mathbf{pdx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nrow}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdx}}\ge {\mathbf{nrow}}$.
NE_INT_ARRAY
On entry, at least one element of totr is negative or totr sums to zero.
On entry, totc has at least one negative element.
NE_INT_ARRAY_2
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}}〉$.
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.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_INVALID_STATE
On entry, state vector has been corrupted or not initialized.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_PREV_CALL
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}}〉$.

None.

## 8Parallelism and Performance

g05pzc 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.

None.

## 10Example

Following initialization of the pseudorandom number generator by a call to g05kfc, 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 (g05pzce.c)

None.

### 10.3Program Results

Program Results (g05pzce.r)