subroutine rcont2 ( nrow, ncol, nrowt, ncolt, matrix, key, ierror ) ! !******************************************************************************* ! !! RCONT2 constructs a random 2 way contingency table with given sums. ! ! ! Reference: ! ! Algorithm AS159, ! Applied Statistics, ! Volume 30, Number 1, 1981. ! ! Modified: ! ! 04 March 2001 ! ! Parameters: ! ! Input, integer NROW, NCOL, the number of rows and columns in the table. ! ! Input, integer NROWT(NROW), NCOLT(NCOL), the row and column sums. ! ! Output, integer MATRIX(NROW,NCOL), the matrix. ! ! Input/output, logical KEY, a flag that indicates whether data has ! been initialized for this problem. Set KEY = .FALSE. before the first ! call. ! ! Output, integer IERROR, an error flag, which is returned as 0 ! if no error occurred. ! implicit none ! integer, parameter :: maxtot = 5000 ! integer ncol integer nrow ! real dummy real, save, dimension ( maxtot+1 ) :: fact integer i integer ia integer iap integer ib integer ic integer id integer idp integer ie integer ierror integer igp integer ihp integer ii integer iip integer j integer jc integer jwork(ncol) logical key integer l logical lsm logical lsp integer m integer matrix(nrow,ncol) integer ncolt(ncol) integer nll integer nlm integer nlmp integer nrowt(nrow) integer nrowtl integer, save :: ntotal = 0 real sumprb real x real y ! ierror = 0 ! ! On user's signal, set up the factorial table. ! if ( .not. key ) then key = .true. if ( nrow <= 1 ) then ierror = 1 return end if if ( ncol <= 1 ) then ierror = 2 return end if do i = 1, nrow if ( nrowt(i) <= 0 ) then ierror = 3 return end if end do ntotal = 0 do j = 1, ncol if ( ncolt(j) <= 0 ) then ierror = 4 return end if ntotal = ntotal + ncolt(j) end do if ( ntotal > maxtot ) then ierror = 5 return end if ! ! Calculate log-factorials. ! x = 0.0E+00 fact(1) = 0.0E+00 do i = 1, ntotal x = x + log ( real ( i ) ) fact(i+1) = x end do end if ! ! Construct random matrix ! do j = 1, ncol-1 jwork(j) = ncolt(j) end do jc = ntotal do l = 1, nrow-1 nrowtl = nrowt(l) ia = nrowtl ic = jc jc = jc - nrowtl do m = 1, ncol-1 id = jwork(m) ie = ic ic = ic - id ib = ie - ia ii = ib - id ! ! Test for zero entries in matrix. ! if ( ie == 0 ) then matrix(l,m:ncol) = 0 go to 190 end if ! ! Generate a pseudo-random number. ! call random_number ( harvest = dummy ) ! ! Compute the conditional expected value of MATRIX(L,M). ! 131 continue nlm = ia * id / real ( ie ) + 0.5E+00 iap = ia + 1 idp = id + 1 igp = idp - nlm ihp = iap - nlm nlmp = nlm + 1 iip = ii + nlmp x = exp ( fact(iap) + fact(ib+1) + fact(ic+1) + fact(idp) - & fact(ie+1) - fact(nlmp) - fact(igp) - fact(ihp) - fact(iip) ) if ( x >= dummy ) then go to 160 end if sumprb = x y = x nll = nlm lsp = .false. lsm = .false. ! ! Increment entry in row l, column m ! 140 continue j = ( id - nlm ) * ( ia - nlm ) if ( j == 0 ) then lsp = .true. else nlm = nlm + 1 x = x * j / real ( nlm * ( ii + nlm ) ) sumprb = sumprb + x if ( sumprb >= dummy ) then go to 160 end if end if do while ( .not. lsm ) ! ! Decrement the entry in row l, column m ! j = nll * ( ii + nll ) if ( j == 0 ) then lsm = .true. exit end if nll = nll - 1 y = y * j / real ( ( id - nll ) * ( ia - nll ) ) sumprb = sumprb + y if ( sumprb >= dummy ) then go to 159 end if if ( .not. lsp ) go to 140 end do if ( .not. lsp ) then go to 140 end if call random_number ( harvest = dummy ) dummy = sumprb * dummy go to 131 159 continue nlm = nll 160 continue matrix(l,m) = nlm ia = ia - nlm jwork(m) = jwork(m) - nlm end do matrix(l,ncol) = ia 190 continue end do ! ! Compute the last row. ! do m = 1, ncol-1 matrix(nrow,m) = jwork(m) end do matrix(nrow,ncol) = ib - matrix(nrow,ncol-1) return end subroutine ivec_print ( n, a, title ) ! !******************************************************************************* ! !! IVEC_PRINT prints an integer vector. ! ! ! Modified: ! ! 28 November 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of components of the vector. ! ! Input, integer A(N), the vector to be printed. ! ! Input, character ( len = * ) TITLE, a title to be printed first. ! TITLE may be blank. ! implicit none ! integer n ! integer a(n) integer big integer i character ( len = * ) title ! if ( title /= ' ' ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) end if big = maxval ( abs ( a(1:n) ) ) write ( *, '(a)' ) ' ' if ( big < 1000 ) then do i = 1, n write ( *, '(i6,1x,i4)' ) i, a(i) end do else if ( big < 1000000 ) then do i = 1, n write ( *, '(i6,1x,i7)' ) i, a(i) end do else do i = 1, n write ( *, '(i6,i11)' ) i, a(i) end do end if return end subroutine imat_print ( lda, m, n, a, title ) ! !******************************************************************************* ! !! IMAT_PRINT prints an integer matrix. ! ! ! Modified: ! ! 08 May 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer LDA, the leading dimension of A. ! ! Input, integer M, the number of rows in A. ! ! Input, integer N, the number of columns in A. ! ! Input, integer A(LDA,N), the matrix to be printed. ! ! Input, character ( len = * ) TITLE, a title to be printed first. ! TITLE may be blank. ! implicit none ! integer lda integer n ! integer a(lda,n) integer i integer j integer jhi integer jlo integer m character ( len = * ) title ! if ( title /= ' ' ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) end if do jlo = 1, n, 10 jhi = min ( jlo + 9, n ) write ( *, '(a)' ) ' ' write ( *, '(6x,10(i7))' ) ( j, j = jlo, jhi ) write ( *, * ) ' ' do i = 1, m write ( *, '(i6,10i7)' ) i, a(i,jlo:jhi) end do end do return end subroutine timestamp ( ) ! !******************************************************************************* ! !! TIMESTAMP prints the current YMDHMS date as a time stamp. ! ! ! Example: ! ! May 31 2001 9:45:54.872 AM ! ! Modified: ! ! 31 May 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! None ! implicit none ! character ( len = 8 ) ampm integer d character ( len = 8 ) date integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s character ( len = 10 ) time integer values(8) integer y character ( len = 5 ) zone ! call date_and_time ( date, time, zone, values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(a,1x,i2,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & trim ( month(m) ), d, y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end