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

    MODULE d01apfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: lw = 800, nout = 6
       INTEGER, PARAMETER                  :: liw = lw/4
    CONTAINS
!dec$ attributes offload : mic :: g1
       FUNCTION g1(x)

!         .. Use Statements ..
          USE nag_library, ONLY : x01aaf
!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: g1
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: a, pi
!         .. Intrinsic Functions ..
          INTRINSIC                              cos
!         .. Executable Statements ..
          pi = x01aaf(pi)
          a = 10.0E0_nag_wp*pi
          g1 = cos(a*x)

          RETURN

!dec$ attributes offload : mic :: g1
       END FUNCTION g1
!dec$ attributes offload : mic :: g2
       FUNCTION g2(x)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: g2
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: omega
!         .. Intrinsic Functions ..
          INTRINSIC                              sin
!         .. Executable Statements ..
          omega = 10.0E0_nag_wp
          g2 = sin(omega*x)

          RETURN

!dec$ attributes offload : mic :: g2
       END FUNCTION g2
    END MODULE d01apfe_mod
    PROGRAM d01apfe

!      D01APF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d01apf, nag_wp
       USE d01apfe_mod, ONLY : g1, g2, liw, lw, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: a, abserr, alpha, b, beta,       &
                                              epsabs, epsrel, result
       INTEGER                             :: ifail, key, nof
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: alpha_a(2), beta_a(2)
       REAL (KIND=nag_wp), ALLOCATABLE     :: w(:)
       INTEGER, ALLOCATABLE                :: iw(:)
       INTEGER                             :: key_a(2)
!      .. Executable Statements ..
       WRITE (nout,*) 'D01APF Example Program Results'

       ALLOCATE (w(lw),iw(liw))

       alpha_a = (/ 0.0_nag_wp, -0.5_nag_wp/)
       beta_a = (/ 0.0_nag_wp, -0.5_nag_wp/)
       key_a = (/ 2, 1/)

       epsabs = 0.0_nag_wp
       epsrel = 1.0E-04_nag_wp
       a = 0.0_nag_wp
       b = 1.0_nag_wp

FUNS:  DO nof = 1, 2

          alpha = alpha_a(nof)
          beta = beta_a(nof)
          key = key_a(nof)

          ifail = -1
          IF (nof==1) THEN
             CALL d01apf(g1,a,b,alpha,beta,key,epsabs,epsrel,result,abserr,w, &
                lw,iw,liw,ifail)
          ELSE
             CALL d01apf(g2,a,b,alpha,beta,key,epsabs,epsrel,result,abserr,w, &
                lw,iw,liw,ifail)
          END IF

          IF (ifail<0) THEN
             EXIT FUNS
          END IF

          WRITE (nout,*)
          WRITE (nout,99999) 'A     ', 'lower limit of integration', a
          WRITE (nout,99999) 'B     ', 'upper limit of integration', b
          WRITE (nout,99998) 'EPSABS', 'absolute accuracy requested', epsabs
          WRITE (nout,99998) 'EPSREL', 'relative accuracy requested', epsrel
          WRITE (nout,*)
          WRITE (nout,99998) 'ALPHA ', 'parameter in the weight function', &
             alpha
          WRITE (nout,99998) 'BETA  ', 'parameter in the weight function', &
             beta
          WRITE (nout,99997) 'KEY   ', 'which weight function is used', key

          IF (ifail>3) THEN
             CYCLE FUNS
          END IF
          WRITE (nout,*)
          WRITE (nout,99996) 'RESULT', 'approximation to the integral', result
          WRITE (nout,99998) 'ABSERR', 'estimate of the absolute error', &
             abserr
          WRITE (nout,99997) 'IW(1)', 'number of subintervals used ', iw(1)

       END DO FUNS

99999  FORMAT (1X,A6,' - ',A32,' = ',F10.4)
99998  FORMAT (1X,A6,' - ',A32,' = ',E9.2)
99997  FORMAT (1X,A6,' - ',A32,' = ',I4)
99996  FORMAT (1X,A6,' - ',A32,' = ',F9.5)
    END PROGRAM d01apfe
