    PROGRAM g05ryfe

!      G05RYF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g05kff, g05ryf, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: lseed = 1, nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: df, genid, i, ifail, ldc, ldx, lr,   &
                                          lstate, m, mode, n, subid
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: c(:,:), r(:), x(:,:), xmu(:)
       INTEGER                         :: seed(lseed)
       INTEGER, ALLOCATABLE            :: state(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G05RYF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)

!      Skip heading in data file
       READ (nin,*)

!      Read in the base generator information and seed
       READ (nin,*) genid, subid, seed(1)

!      Initial call to initialiser to get size of STATE array
       lstate = 0
       ALLOCATE (state(lstate))
       ifail = 0
       CALL g05kff(genid,subid,seed,lseed,state,lstate,ifail)

!      Reallocate STATE
       DEALLOCATE (state)
       ALLOCATE (state(lstate))

!      Initialize the generator to a repeatable sequence
       ifail = 0
       CALL g05kff(genid,subid,seed,lseed,state,lstate,ifail)

!      Read in sample size and number of dimensions
       READ (nin,*) n, m

       ldc = m
       ldx = n
       lr = m*(m+1) + 2
       ALLOCATE (x(ldx,m),c(ldc,m),r(lr),xmu(m))

!      Read in degrees of freedom
       READ (nin,*) df

!      Read in vector of means
       READ (nin,*) xmu(1:m)

!      Read in upper triangle portion of the covariance matrix
       DO i = 1, m
          READ (nin,*) c(i,i:m)
       END DO

!      Using a single call to G05RYF, so set up reference vector
!      and generate values in one go
       mode = 2

!      Generate variates
       ifail = 0
       CALL g05ryf(mode,n,df,m,xmu,c,ldc,r,lr,state,x,ldx,ifail)

!      Display the variates
       ifail = 0
       CALL x04caf('General',' ',n,m,x,ldx,'Variates',ifail)

    END PROGRAM g05ryfe
