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

    MODULE g05yffe_mod
!      G05YFF Example Program Module:
!             Parameters and User-defined Routines

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: liref = 2000, nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: fun
       FUNCTION fun(idim,x,n)

!         .. 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 g05yffe_mod
    PROGRAM g05yffe

!      G05YFF Example Main Program

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

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

       ifail = 0
       CALL g05yef(idim,iref,iskip,ifail)

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

             CALL g05yff(n,quasi,iref,ifail)

             sum = sum + fun(idim,quasi,n)
          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 g05yffe
