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

    MODULE d01dafe_mod

!      D01DAF 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 :: phi1
       FUNCTION phi1(y)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: phi1
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: y
!         .. Executable Statements ..
          phi1 = 0.0E0_nag_wp

          RETURN

!dec$ attributes offload : mic :: phi1
       END FUNCTION phi1
!dec$ attributes offload : mic :: phi2a
       FUNCTION phi2a(y)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: phi2a
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: y
!         .. Intrinsic Functions ..
          INTRINSIC                              sqrt
!         .. Executable Statements ..
          phi2a = sqrt(1.0E0_nag_wp-y*y)

          RETURN

!dec$ attributes offload : mic :: phi2a
       END FUNCTION phi2a
!dec$ attributes offload : mic :: fa
       FUNCTION fa(x,y)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: fa
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x, y
!         .. Executable Statements ..
          fa = x + y

          RETURN

!dec$ attributes offload : mic :: fa
       END FUNCTION fa
!dec$ attributes offload : mic :: phi2b
       FUNCTION phi2b(y)

!         .. Use Statements ..
          USE nag_library, ONLY : x01aaf
!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: phi2b
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: y
!         .. Executable Statements ..
          phi2b = 0.5E0_nag_wp*x01aaf(y)

          RETURN

!dec$ attributes offload : mic :: phi2b
       END FUNCTION phi2b
!dec$ attributes offload : mic :: fb
       FUNCTION fb(x,y)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: fb
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x, y
!         .. Intrinsic Functions ..
          INTRINSIC                              cos, sin
!         .. Executable Statements ..
          fb = y*y*(cos(x)+sin(x))

          RETURN

!dec$ attributes offload : mic :: fb
       END FUNCTION fb
    END MODULE d01dafe_mod
    PROGRAM d01dafe

!      D01DAF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d01daf, nag_wp
       USE d01dafe_mod, ONLY : fa, fb, nout, phi1, phi2a, phi2b
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: absacc, ans, ya, yb
       INTEGER                             :: ifail, npts
!      .. Executable Statements ..
       WRITE (nout,*) 'D01DAF Example Program Results'

       ya = 0.0E0_nag_wp
       yb = 1.0E0_nag_wp
       absacc = 1.0E-6_nag_wp

       ifail = 0
       CALL d01daf(ya,yb,phi1,phi2a,fa,absacc,ans,npts,ifail)

       WRITE (nout,*)
       WRITE (nout,*) 'First formulation'
       WRITE (nout,99999) 'Integral =', ans
       WRITE (nout,99998) 'Number of function evaluations =', npts

       ifail = 0
       CALL d01daf(ya,yb,phi1,phi2b,fb,absacc,ans,npts,ifail)

       WRITE (nout,*)
       WRITE (nout,*) 'Second formulation'
       WRITE (nout,99999) 'Integral =', ans
       WRITE (nout,99998) 'Number of function evaluations =', npts

99999  FORMAT (1X,A,F9.4)
99998  FORMAT (1X,A,I5)
    END PROGRAM d01dafe
