function r8_uniform_01 ( seed ) !*****************************************************************************80 ! !! R8_UNIFORM_01 returns a unit pseudorandom R8. ! ! Discussion: ! ! An R8 is a real ( kind = 8 ) value. ! ! For now, the input quantity SEED is an integer variable. ! ! This routine implements the recursion ! ! seed = 16807 * seed mod ( 2^31 - 1 ) ! r8_uniform_01 = seed / ( 2^31 - 1 ) ! ! The integer arithmetic never requires more than 32 bits, ! including a sign bit. ! ! If the initial seed is 12345, then the first three computations are ! ! Input Output R8_UNIFORM_01 ! SEED SEED ! ! 12345 207482415 0.096616 ! 207482415 1790989824 0.833995 ! 1790989824 2035175616 0.947702 ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 05 July 2006 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Paul Bratley, Bennett Fox, Linus Schrage, ! A Guide to Simulation, ! Springer Verlag, pages 201-202, 1983. ! ! Pierre L'Ecuyer, ! Random Number Generation, ! in Handbook of Simulation, ! edited by Jerry Banks, ! Wiley Interscience, page 95, 1998. ! ! Bennett Fox, ! Algorithm 647: ! Implementation and Relative Efficiency of Quasirandom ! Sequence Generators, ! ACM Transactions on Mathematical Software, ! Volume 12, Number 4, pages 362-376, 1986. ! ! Peter Lewis, Allen Goodman, James Miller ! A Pseudo-Random Number Generator for the System/360, ! IBM Systems Journal, ! Volume 8, pages 136-143, 1969. ! ! Parameters: ! ! Input/output, integer ( kind = 4 ) SEED, the "seed" value, which should ! NOT be 0. On output, SEED has been updated. ! ! Output, real ( kind = 8 ) R8_UNIFORM_01, a new pseudorandom variate, ! strictly between 0 and 1. ! implicit none integer ( kind = 4 ), parameter :: i4_huge = 2147483647 integer ( kind = 4 ) k real ( kind = 8 ) r8_uniform_01 integer ( kind = 4 ) seed if ( seed == 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'R8_UNIFORM_01 - Fatal error!' write ( *, '(a)' ) ' Input value of SEED = 0.' stop 1 end if k = seed / 127773 seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ) then seed = seed + i4_huge end if r8_uniform_01 = real ( seed, kind = 8 ) * 4.656612875D-10 return end subroutine r8mat_print ( m, n, a, title ) !*****************************************************************************80 ! !! R8MAT_PRINT prints an R8MAT. ! ! Discussion: ! ! An R8MAT is an MxN array of R8's, stored by (I,J) -> [I+J*M]. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 12 September 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) M, the number of rows in A. ! ! Input, integer ( kind = 4 ) N, the number of columns in A. ! ! Input, real ( kind = 8 ) A(M,N), the matrix. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer ( kind = 4 ) m integer ( kind = 4 ) n real ( kind = 8 ) a(m,n) character ( len = * ) title call r8mat_print_some ( m, n, a, 1, 1, m, n, title ) return end subroutine r8mat_print_some ( m, n, a, ilo, jlo, ihi, jhi, title ) !*****************************************************************************80 ! !! R8MAT_PRINT_SOME prints some of an R8MAT. ! ! Discussion: ! ! An R8MAT is an MxN array of R8's, stored by (I,J) -> [I+J*M]. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 10 September 2009 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) M, N, the number of rows and columns. ! ! Input, real ( kind = 8 ) A(M,N), an M by N matrix to be printed. ! ! Input, integer ( kind = 4 ) ILO, JLO, the first row and column to print. ! ! Input, integer ( kind = 4 ) IHI, JHI, the last row and column to print. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer ( kind = 4 ), parameter :: incx = 5 integer ( kind = 4 ) m integer ( kind = 4 ) n real ( kind = 8 ) a(m,n) character ( len = 14 ) ctemp(incx) integer ( kind = 4 ) i integer ( kind = 4 ) i2hi integer ( kind = 4 ) i2lo integer ( kind = 4 ) ihi integer ( kind = 4 ) ilo integer ( kind = 4 ) inc integer ( kind = 4 ) j integer ( kind = 4 ) j2 integer ( kind = 4 ) j2hi integer ( kind = 4 ) j2lo integer ( kind = 4 ) jhi integer ( kind = 4 ) jlo character ( len = * ) title write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) if ( m <= 0 .or. n <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' (None)' return end if do j2lo = max ( jlo, 1 ), min ( jhi, n ), incx j2hi = j2lo + incx - 1 j2hi = min ( j2hi, n ) j2hi = min ( j2hi, jhi ) inc = j2hi + 1 - j2lo write ( *, '(a)' ) ' ' do j = j2lo, j2hi j2 = j + 1 - j2lo write ( ctemp(j2), '(i8,6x)' ) j end do write ( *, '('' Col '',5a14)' ) ctemp(1:inc) write ( *, '(a)' ) ' Row' write ( *, '(a)' ) ' ' i2lo = max ( ilo, 1 ) i2hi = min ( ihi, m ) do i = i2lo, i2hi do j2 = 1, inc j = j2lo - 1 + j2 if ( a(i,j) == real ( int ( a(i,j) ), kind = 8 ) ) then write ( ctemp(j2), '(f8.0,6x)' ) a(i,j) else write ( ctemp(j2), '(g14.6)' ) a(i,j) end if end do write ( *, '(i5,a,5a14)' ) i, ':', ( ctemp(j), j = 1, inc ) end do end do return end subroutine r8pp_print ( n, a, title ) !*****************************************************************************80 ! !! R8PP_PRINT prints an R8PP matrix. ! ! Discussion: ! ! The R8PP storage format is appropriate for a symmetric positive ! definite matrix. Only the upper triangle of the matrix is stored, ! by successive partial columns, in an array of length (N*(N+1))/2, ! which contains (A11,A12,A22,A13,A23,A33,A14,...,ANN) ! ! R8PP storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 07 May 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) N, the order of the matrix. ! N must be positive. ! ! Input, real ( kind = 8 ) A((N*(N+1))/2), the R8PP matrix. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer ( kind = 4 ) n real ( kind = 8 ) a((n*(n+1))/2) character ( len = * ) title call r8pp_print_some ( n, a, 1, 1, n, n, title ) return end subroutine r8pp_print_some ( n, a, ilo, jlo, ihi, jhi, title ) !*****************************************************************************80 ! !! R8PP_PRINT_SOME prints some of an R8PP matrix. ! ! Discussion: ! ! The R8PP storage format is appropriate for a symmetric positive ! definite matrix. Only the upper triangle of the matrix is stored, ! by successive partial columns, in an array of length (N*(N+1))/2, ! which contains (A11,A12,A22,A13,A23,A33,A14,...,ANN) ! ! R8PP storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 21 March 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) N, the order of the matrix. ! N must be positive. ! ! Input, real ( kind = 8 ) A((N*(N+1))/2), the R8PP matrix. ! ! Input, integer ( kind = 4 ) ILO, JLO, IHI, JHI, the first row and ! column, and the last row and column to be printed. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer ( kind = 4 ), parameter :: incx = 5 integer ( kind = 4 ) n real ( kind = 8 ) a((n*(n+1))/2) real ( kind = 8 ) aij character ( len = 14 ) ctemp(incx) integer ( kind = 4 ) i integer ( kind = 4 ) i2hi integer ( kind = 4 ) i2lo integer ( kind = 4 ) ihi integer ( kind = 4 ) ilo integer ( kind = 4 ) inc integer ( kind = 4 ) j integer ( kind = 4 ) j2 integer ( kind = 4 ) j2hi integer ( kind = 4 ) j2lo integer ( kind = 4 ) jhi integer ( kind = 4 ) jlo character ( len = * ) title write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) ! ! Print the columns of the matrix, in strips of 5. ! do j2lo = jlo, jhi, incx j2hi = j2lo + incx - 1 j2hi = min ( j2hi, n ) j2hi = min ( j2hi, jhi ) inc = j2hi + 1 - j2lo write ( *, '(a)' ) ' ' do j = j2lo, j2hi j2 = j + 1 - j2lo write ( ctemp(j2), '(i7,7x)' ) j end do write ( *, '(a,5a14)' ) ' Col: ', ( ctemp(j2), j2 = 1, inc ) write ( *, '(a)' ) ' Row' write ( *, '(a)' ) ' ---' ! ! Determine the range of the rows in this strip. ! i2lo = max ( ilo, 1 ) i2hi = min ( ihi, n ) do i = i2lo, i2hi ! ! Print out (up to) 5 entries in row I, that lie in the current strip. ! do j2 = 1, inc j = j2lo - 1 + j2 if ( i <= j ) then aij = a(i+(j*(j-1))/2) else aij = a(j+(i*(i-1))/2) end if write ( ctemp(j2), '(g14.6)' ) aij end do write ( *, '(i5,1x,5a14)' ) i, ( ctemp(j2), j2 = 1, inc ) end do end do return end subroutine r8utp_print ( n, a, title ) !*****************************************************************************80 ! !! R8UTP_PRINT prints an R8UTP matrix. ! ! Discussion: ! ! The R8UTP storage format is appropriate for an upper triangular ! matrix. Only the upper triangle of the matrix is stored, ! by successive partial columns, in an array of length (N*(N+1))/2, ! which contains (A11,A12,A22,A13,A23,A33,A14,...,ANN) ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 16 April 2014 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) N, the order of the matrix. ! N must be positive. ! ! Input, real ( kind = 8 ) A((N*(N+1))/2), the matrix. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer ( kind = 4 ) n real ( kind = 8 ) a((n*(n+1))/2) character ( len = * ) title call r8utp_print_some ( n, a, 1, 1, n, n, title ) return end subroutine r8utp_print_some ( n, a, ilo, jlo, ihi, jhi, title ) !*****************************************************************************80 ! !! R8UTP_PRINT_SOME prints some of an R8UTP matrix. ! ! Discussion: ! ! The R8UTP storage format is appropriate for an upper triangular ! matrix. Only the upper triangle of the matrix is stored, ! by successive partial columns, in an array of length (N*(N+1))/2, ! which contains (A11,A12,A22,A13,A23,A33,A14,...,ANN) ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 16 April 2014 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) N, the order of the matrix. ! N must be positive. ! ! Input, real ( kind = 8 ) A((N*(N+1))/2), the matrix. ! ! Input, integer ( kind = 4 ) ILO, JLO, IHI, JHI, the first row and ! column, and the last row and column to be printed. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer ( kind = 4 ), parameter :: incx = 5 integer ( kind = 4 ) n real ( kind = 8 ) a((n*(n+1))/2) real ( kind = 8 ) aij character ( len = 14 ) ctemp(incx) integer ( kind = 4 ) i integer ( kind = 4 ) i2hi integer ( kind = 4 ) i2lo integer ( kind = 4 ) ihi integer ( kind = 4 ) ilo integer ( kind = 4 ) inc integer ( kind = 4 ) j integer ( kind = 4 ) j2 integer ( kind = 4 ) j2hi integer ( kind = 4 ) j2lo integer ( kind = 4 ) jhi integer ( kind = 4 ) jlo character ( len = * ) title write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) ! ! Print the columns of the matrix, in strips of 5. ! do j2lo = jlo, jhi, incx j2hi = j2lo + incx - 1 j2hi = min ( j2hi, n ) j2hi = min ( j2hi, jhi ) inc = j2hi + 1 - j2lo write ( *, '(a)' ) ' ' do j = j2lo, j2hi j2 = j + 1 - j2lo write ( ctemp(j2), '(i7,7x)' ) j end do write ( *, '(a,5a14)' ) ' Col: ', ( ctemp(j2), j2 = 1, inc ) write ( *, '(a)' ) ' Row' write ( *, '(a)' ) ' ---' ! ! Determine the range of the rows in this strip. ! i2lo = max ( ilo, 1 ) i2hi = min ( ihi, n ) do i = i2lo, i2hi ! ! Print out (up to) 5 entries in row I, that lie in the current strip. ! do j2 = 1, inc j = j2lo - 1 + j2 if ( i <= j ) then aij = a(i+(j*(j-1))/2) else aij = 0.0D+00 end if write ( ctemp(j2), '(g14.6)' ) aij end do write ( *, '(i5,1x,5a14)' ) i, ( ctemp(j2), j2 = 1, inc ) end do end do return end subroutine rnorm ( seed, u1, u2 ) !*****************************************************************************80 ! !! RNORM returns two independent standard random normal deviates. ! ! Discussion: ! ! This routine sets U1 and U2 to two independent standardized ! random normal deviates. This is a version of the ! method given in Knuth. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 16 April 2014 ! ! Author: ! ! Original FORTRAN77 version by William Smith, Ronald Hocking. ! This FORTRAN90 version by John Burkardt. ! ! Reference: ! ! Donald Knuth, ! The Art of Computer Programming, ! Volume 2, Seminumerical Algorithms, ! Third Edition, ! Addison Wesley, 1997, ! ISBN: 0201896842, ! LC: QA76.6.K64. ! ! Parameters: ! ! Input/output, integer ( kind = 4 ) SEED, a seed for the random ! number generator. ! ! Output, real ( kind = 8 ) U1, U2, two standard random normal deviates. ! implicit none real ( kind = 8 ) r8_uniform_01 real ( kind = 8 ) s integer ( kind = 4 ) seed real ( kind = 8 ) u1 real ( kind = 8 ) u2 real ( kind = 8 ) x real ( kind = 8 ) y do x = r8_uniform_01 ( seed ) y = r8_uniform_01 ( seed ) x = 2.0D+00 * x - 1.0D+00 y = 2.0D+00 * y - 1.0D+00 s = x * x + y * y if ( s <= 1.0D+00 ) then s = sqrt ( - 2.0D+00 * log ( s ) / s ) u1 = x * s u2 = y * s exit end if end do return end subroutine timestamp ( ) !*****************************************************************************80 ! !! TIMESTAMP prints the current YMDHMS date as a time stamp. ! ! Example: ! ! 31 May 2001 9:45:54.872 AM ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 18 May 2013 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! None ! implicit none character ( len = 8 ) ampm integer ( kind = 4 ) d integer ( kind = 4 ) h integer ( kind = 4 ) m integer ( kind = 4 ) mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer ( kind = 4 ) n integer ( kind = 4 ) s integer ( kind = 4 ) values(8) integer ( kind = 4 ) y call date_and_time ( values = 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 ( *, '(i2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end subroutine wshrt ( d, n, np, seed, sa ) !*****************************************************************************80 ! !! WSHRT returns a random Wishart variate. ! ! Discussion: ! ! This routine is a Wishart variate generator. ! ! On output, SA is an upper-triangular matrix of size NP * NP, ! written in linear form, column ordered, whose elements have a ! Wishart(N, SIGMA) distribution. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 16 April 2014 ! ! Author: ! ! Original FORTRAN77 version by William Smith, Ronald Hocking. ! This FORTRAN90 version by John Burkardt. ! ! Reference: ! ! William Smith, Ronald Hocking, ! Algorithm AS 53, Wishart Variate Generator, ! Applied Statistics, ! Volume 21, Number 3, pages 341-345, 1972. ! ! Parameters: ! ! Input, real ( kind = 8 ) D(NP*(NP+1)/2), the upper triangular array that ! represents the Cholesky factor of the correlation matrix SIGMA. ! D is stored in column-major form. ! ! Input, integer ( kind = 4 ) N, the number of degrees of freedom. ! 1 <= N <= NP. ! ! Input, integer ( kind = 4 ) NP, the size of variables. ! ! Input/output, integer ( kind = 4 ) SEED, a seed for the random ! number generator. ! ! Output, real ( kind = 8 ) SA(NP*(NP+1)/2), a sample from the ! Wishart distribution. ! implicit none integer ( kind = 4 ) np real ( kind = 8 ) c real ( kind = 8 ) d((np*(np+1))/2) real ( kind = 8 ) df integer ( kind = 4 ) i integer ( kind = 4 ) ii integer ( kind = 4 ) ip integer ( kind = 4 ) j integer ( kind = 4 ) k integer ( kind = 4 ) n integer ( kind = 4 ) nnp integer ( kind = 4 ) nq integer ( kind = 4 ) nr integer ( kind = 4 ) ns real ( kind = 8 ) rn real ( kind = 8 ) sa((np*(np+1))/2) real ( kind = 8 ) sb((np*(np+1))/2) integer ( kind = 4 ) seed real ( kind = 8 ) u1 real ( kind = 8 ) u2 k = 1 nnp = ( np * ( np + 1 ) ) / 2 ! ! Load SB with independent normal (0, 1) variates. ! do while ( k <= nnp ) call rnorm ( seed, u1, u2 ) sb(k) = u1 k = k + 1 if ( k <= nnp ) then sb(k) = u2 k = k + 1 end if end do ! ! Load diagonal elements with square root of chi-square variates. ! ns = 0 do i = 1, np ! ! The original text read "DF = N - I + 1". ! This should read "DF = NP - I + 1". ! df = real ( np - i + 1, kind = 8 ) ns = ns + i u1 = 2.0D+00 / ( 9.0D+00 * df ) u2 = 1.0D+00 - u1 u1 = sqrt ( u1 ) ! ! Wilson-Hilferty formula for approximating chi-square variates: ! The original code did not take the absolute value! ! sb(ns) = sqrt ( df * abs ( ( u2 + sb(ns) * u1 ) ** 3 ) ) end do rn = real ( n, kind = 8 ) nr = 1 do i = 1, np nr = nr + i - 1 do j = i, np ip = nr nq = ( j * ( j - 1 ) ) / 2 + i - 1 c = 0.0D+00 do k = i, j ip = ip + k - 1 nq = nq + 1 c = c + sb(ip) * d(nq) end do sa(ip) = c end do end do do i = 1, np ii = np - i + 1 nq = nnp - np do j = 1, i ip = ( ii * ( ii - 1 ) ) / 2 c = 0.0D+00 do k = i, np ip = ip + 1 nq = nq + 1 c = c + sa(ip) * sa(nq) end do sa(nq) = c / rn nq = nq - 2 * np + i + j - 1 end do end do return end