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

    MODULE d01gbfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: lenwrk = 500, maxcls = 20000,    &
                                              ndim = 4, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: functn
       FUNCTION functn(ndim,x)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: functn
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: ndim
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x(ndim)
!         .. Intrinsic Functions ..
          INTRINSIC                              exp
!         .. Executable Statements ..
          functn = 4.0E0_nag_wp*x(1)*x(3)**2*exp(2.0E0_nag_wp*x(1)*x(3))/ &
             (1.0E0_nag_wp+x(2)+x(4))**2

          RETURN

!dec$ attributes offload : mic :: functn
       END FUNCTION functn
    END MODULE d01gbfe_mod
    PROGRAM d01gbfe

!      D01GBF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d01gbf, nag_wp
       USE d01gbfe_mod, ONLY : functn, lenwrk, maxcls, ndim, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: acc, eps, finest
       INTEGER                             :: ifail, mincls
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: a(:), b(:), wrkstr(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'D01GBF Example Program Results'

       ALLOCATE (a(ndim),b(ndim),wrkstr(lenwrk))

       a(1:ndim) = 0.0E0_nag_wp
       b(1:ndim) = 1.0E0_nag_wp
       eps = 0.01E0_nag_wp
       mincls = 1000

       ifail = -1
       CALL d01gbf(ndim,a,b,mincls,maxcls,functn,eps,acc,lenwrk,wrkstr,finest, &
          ifail)

       SELECT CASE (ifail)
       CASE (0,2)
          WRITE (nout,*)
          WRITE (nout,99998) 'Requested accuracy    = ', eps
          WRITE (nout,99997) 'Estimated value       = ', finest
          WRITE (nout,99998) 'Estimated accuracy    = ', acc
          WRITE (nout,99999) 'Number of evaluations = ', mincls
       END SELECT

99999  FORMAT (1X,A,I5)
99998  FORMAT (1X,A,E13.2)
99997  FORMAT (1X,A,F13.5)
    END PROGRAM d01gbfe
