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

    MODULE g05ydfe_mod

!      G05YDF 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 :: fun
       FUNCTION fun(idim,n,x)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: fun
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: idim, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x(n,idim)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: tmp
          INTEGER                             :: i, j
!         .. Intrinsic Functions ..
          INTRINSIC                              abs
!         .. Executable Statements ..
          fun = 0.0_nag_wp
          tmp = 1.0_nag_wp
          DO i = 1, n
             tmp = 1.0_nag_wp
             DO j = 1, idim
                tmp = tmp*abs(4.0_nag_wp*x(i,j)-2.0_nag_wp)
             END DO
             fun = fun + tmp
          END DO
          RETURN
!dec$ attributes offload : mic :: fun
       END FUNCTION fun
    END MODULE g05ydfe_mod
    PROGRAM g05ydfe

!      G05YDF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : g05ycf, g05ydf, nag_wp
       USE g05ydfe_mod, ONLY : fun, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: sum, vsbl
       INTEGER                             :: i, idim, ifail, n, ntimes
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: quasi(:,:)
       INTEGER                             :: iref(406)
!      .. Intrinsic Functions ..
       INTRINSIC                              real
!      .. Executable Statements ..
       WRITE (nout,*) 'G05YDF Example Program Results'

!      Skip heading and read dimension, s, of integral.
       READ (nin,*)
       READ (nin,*) idim
!      n = number of s-dimensional points points per sample;
!      ntimes = number of samples for approximation.
       READ (nin,*) n, ntimes
       ALLOCATE (quasi(n,idim+1))

       ifail = 0
       CALL g05ycf(idim,iref,ifail)

       IF (ifail>=0) THEN
          sum = 0.0_nag_wp
          DO i = 1, ntimes

             CALL g05ydf(n,quasi,iref,ifail)

             sum = sum + fun(idim,n,quasi)
          END DO
          vsbl = sum/real(n*ntimes,kind=nag_wp)
          WRITE (nout,99999)
          WRITE (nout,99999) 'Value of integral = ', vsbl
       END IF

99999  FORMAT (1X,A,F8.4)
    END PROGRAM g05ydfe
