!   G05YNF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE g05ynfe_mod

!      G05YNF Example Program Module:
!             Parameters and User-defined Routines

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: lseed = 1, nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: ifun
       FUNCTION ifun(x,lx)
!         Function being integrated, in this case
!         ABS(4.0 X - 2)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: ifun
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: lx
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x(lx)
!         .. Local Scalars ..
          INTEGER                             :: d
!         .. Intrinsic Functions ..
          INTRINSIC                              abs
!         .. Executable Statements ..
          ifun = 1.0E0_nag_wp
          DO d = 1, lx
             ifun = ifun*abs(4.0E0_nag_wp*x(d)-2.0E0_nag_wp)
          END DO
!dec$ attributes offload : mic :: ifun
       END FUNCTION ifun
    END MODULE g05ynfe_mod
    PROGRAM g05ynfe

!      G05YNF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : g05kff, g05ymf, g05ynf, nag_wp, x04caf
       USE g05ynfe_mod, ONLY : ifun, lseed, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: sum, vsbl
       INTEGER                             :: dn, genid, i, idim, ifail,       &
                                              iskip, ldquas, liref, lstate, n, &
                                              nsdigi, pgenid, psubid, rcord,   &
                                              stype
       CHARACTER (80)                      :: title
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: quas(:,:)
       INTEGER, ALLOCATABLE                :: iref(:), state(:)
       INTEGER                             :: seed(lseed)
!      .. Intrinsic Functions ..
       INTRINSIC                              real
!      .. Executable Statements ..
       WRITE (nout,*) 'G05YNF Example Program Results'
       WRITE (nout,*)

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

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

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

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

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

!      Fix the RCORD = 1, so QUAS(IDIM,N). As we 
!      are accessing each dimension in turn for a given variate
!      when evaluating the function, this is more efficient
       rcord = 1

!      Read in quasi-random generator and scrambling to use
       READ (nin,*) genid, stype, nsdigi

!      Read in problem size
       READ (nin,*) n, idim, iskip

       IF (genid==4) THEN
          liref = 407
       ELSE
          liref = 32*idim + 7
       END IF
       ldquas = idim
       ALLOCATE (quas(ldquas,n),iref(liref))

!      Call the initializer for the quasi-random sequence
       ifail = 0
       CALL g05ynf(genid,stype,idim,iref,liref,iskip,nsdigi,state,ifail)

!      Generate N quasi-random variates
       ifail = 0
       CALL g05ymf(n,rcord,quas,ldquas,iref,ifail)

!      Evaluate the function, and sum
       sum = 0.0E0_nag_wp
       DO i = 1, n
          sum = sum + ifun(quas(1:idim,i),idim)
       END DO

!      Convert sum to mean value
       vsbl = sum/real(n,kind=nag_wp)
       WRITE (nout,99999)
       WRITE (nout,99999) 'Value of integral = ', vsbl

!      Read in number of variates to display
       READ (nin,*) dn

!      Display the first DN variates
       WRITE (nout,*)
       WRITE (title,99998) 'First ', dn, ' variates for all ', idim, &
          ' dimensions'
       FLUSH (nout)
       ifail = 0
       CALL x04caf('General',' ',idim,dn,quas,ldquas,title,ifail)

99999  FORMAT (1X,A,F8.4)
99998  FORMAT (A,I0,A,I0,A)
    END PROGRAM g05ynfe
