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

    MODULE d01fdfe_mod

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

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

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: f
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: ndim
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x(ndim)
!         .. Intrinsic Functions ..
          INTRINSIC                              abs, sqrt, sum
!         .. Executable Statements ..
          f = 1.0E0_nag_wp/sqrt(abs(2.25E0_nag_wp-sum(x(1:ndim)**2)))

          RETURN

!dec$ attributes offload : mic :: f
       END FUNCTION f
!dec$ attributes offload : mic :: region
       SUBROUTINE region(ndim,x,j,c,d)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: c, d
          INTEGER, INTENT (IN)                :: j, ndim
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x(ndim)
!         .. Intrinsic Functions ..
          INTRINSIC                              abs, sqrt, sum
!         .. Executable Statements ..
          IF (j>1) THEN
             d = sqrt(abs(2.25E0_nag_wp-sum(x(1:(j-1))**2)))
             c = -d
          ELSE
             c = -1.5E0_nag_wp
             d = 1.5E0_nag_wp
          END IF

          RETURN

!dec$ attributes offload : mic :: region
       END SUBROUTINE region
    END MODULE d01fdfe_mod
    PROGRAM d01fdfe

!      D01FDF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d01fdf, d01fdv, nag_wp
       USE d01fdfe_mod, ONLY : f, nout, region
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: r0, result, sigma, u
       INTEGER                             :: ifail, limit, ncalls, ndim
!      .. Executable Statements ..
       WRITE (nout,*) 'D01FDF Example Program Results'

       ndim = 3
       limit = 8000
       u = 1.5E0_nag_wp
       sigma = 1.5E0_nag_wp
       r0 = 0.9E0_nag_wp

       ifail = 0
       CALL d01fdf(ndim,f,sigma,d01fdv,limit,r0,u,result,ncalls,ifail)

       WRITE (nout,*)
       WRITE (nout,*) 'Sphere-to-sphere transformation'
       WRITE (nout,*)
       WRITE (nout,99999) 'Estimated value of the integral =', result
       WRITE (nout,99998) 'Number of integrand evaluations =', ncalls
       WRITE (nout,*)
       WRITE (nout,*) 'Product region transformation'

       sigma = -1.0E0_nag_wp
       r0 = 0.8E0_nag_wp

       ifail = 0
       CALL d01fdf(ndim,f,sigma,region,limit,r0,u,result,ncalls,ifail)

       WRITE (nout,*)
       WRITE (nout,99999) 'Estimated value of the integral =', result
       WRITE (nout,99998) 'Number of integrand evaluations =', ncalls

99999  FORMAT (1X,A,F9.3)
99998  FORMAT (1X,A,I4)
    END PROGRAM d01fdfe
