NAG FL Interface
g05pzf (matrix_​2waytable)

Settings help

FL Name Style:

FL Specification Language:

1 Purpose

g05pzf generates a random two-way table.

2 Specification

Fortran Interface
Subroutine g05pzf ( mode, nrow, ncol, totr, totc, r, lr, state, x, ldx, ifail)
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)
C Header Interface
#include <nag.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)
The routine may be called by the names g05pzf or nagf_rand_matrix_2waytable.

3 Description

Given m row totals Ri and n column totals Cj (with i=1m Ri=j=1n Cj=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.

4 References

Patefield W M (1981) An efficient method of generating R×C tables with given row and column totals Appl. Stats. 30 91–97

5 Arguments

1: mode Integer Input
On entry: a code for selecting the operation to be performed by the routine.
Set up reference vector only.
Generate two-way table using reference vector set up in a prior call to g05pzf.
Set up reference vector and generate two-way table.
Constraint: mode=0, 1 or 2.
2: nrow Integer Input
On entry: m, the number of rows in the table.
Constraint: nrow2.
3: ncol Integer Input
On entry: n, the number of columns in the table.
Constraint: ncol2.
4: totr(nrow) Integer array Input
On entry: the m row totals, Ri, for i=1,2,,m.
  • totr(i)0, for i=1,2,,m;
  • i=1mtotr(i)=j=1ntotc(j);
  • i totr(i)>0, for i=1,2,,m.
5: totc(ncol) Integer array Input
On entry: the n column totals, Cj, for j=1,2,,n.
  • totc(j)0, for j=1,2,,n;
  • j=1ntotc(j)=i=1mtotr(i).
6: r(lr) Real (Kind=nag_wp) array Communication Array
On entry: if mode=1, the reference vector from the previous call to g05pzf.
On exit: the reference vector.
7: lr Integer Input
On entry: the dimension of the array r as declared in the (sub)program from which g05pzf is called.
Constraint: lri=1nrowtotr(i)+5.
8: state(*) Integer array Communication 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: x(ldx,ncol) Integer array Output
On exit: if mode=1 or 2, a pseudorandom two-way m×n table, X, with element x(i,j) containing the (i,j)th entry in the table such that i=1 m x(i,j)=totc(j) and j=1 n x(i,j)=totr(i)
10: ldx Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g05pzf is called.
Constraint: ldxnrow.
11: ifail Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. When the value -1 or 1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6 Error Indicators and Warnings

If on entry 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:
On entry, mode=value.
Constraint: mode=0, 1 or 2.
On entry, nrow=value.
Constraint: nrow2.
On entry, ncol=value.
Constraint: ncol2.
On entry, at least one element of totr is negative or totr sums to zero.
On entry, totc has at least one negative element.
nrow or ncol is not the same as when r was set up in a previous call.
Previous value of nrow=value and nrow=value.
Previous value of ncol=value and ncol=value.
On entry, lr is not large enough, lr=value: minimum length required =value.
On entry, state vector has been corrupted or not initialized.
On entry, ldx=value and nrow=value.
Constraint: ldxnrow.
On entry, the arrays totr and totc do not sum to the same total: totr array total is value, totc array total is value.
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

Not applicable.

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
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.

9 Further Comments


10 Example

Following initialization of the pseudorandom number generator by a call to g05kff, this example generates and prints a 4×3 two-way table, with row totals of 9, 11, 7 and 23 respectively, and column totals of 16, 17 and 17 respectively.

10.1 Program Text

Program Text (g05pzfe.f90)

10.2 Program Data

Program Data (g05pzfe.d)

10.3 Program Results

Program Results (g05pzfe.r)