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

    MODULE g05ymfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: ifun
       FUNCTION ifun(x,lx)
!         Function being integrated, in this example
!         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 g05ymfe_mod
    PROGRAM g05ymfe

!      G05YMF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : g05ylf, g05ymf, nag_wp, x04caf
       USE g05ymfe_mod, ONLY : ifun, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: sum, vsbl
       INTEGER                             :: dn, genid, i, idim, ifail,       &
                                              iskip, ldquas, liref, n, rcord
       CHARACTER (80)                      :: title
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: quas(:,:)
       INTEGER, ALLOCATABLE                :: iref(:)
!      .. Intrinsic Functions ..
       INTRINSIC                              real
!      .. Executable Statements ..
       WRITE (nout,*) 'G05YMF Example Program Results'
       WRITE (nout,*)

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

!      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 the generator to use
       READ (nin,*) genid

!      Read in the 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))

!      Initialize the generator
       ifail = 0
       CALL g05ylf(genid,idim,iref,liref,iskip,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,*)
       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 g05ymfe
