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

    MODULE e01tnfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER       :: one = 1.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: six = 6.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: three = 3.0_nag_wp
       INTEGER, PARAMETER                  :: nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: funct
       FUNCTION funct(x)
!         This function evaluates the 5D function funct.

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: funct
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x(5)
!         .. Intrinsic Functions ..
          INTRINSIC                              cos
!         .. Executable Statements ..
          funct = ((1.25_nag_wp+cos(5.4_nag_wp*x(5)))*cos(six*x(1))*cos(six*x( &
             2))*cos(six*x(3)))/(six+six*(three*x(4)-one)**2)

          RETURN
!dec$ attributes offload : mic :: funct
       END FUNCTION funct
    END MODULE e01tnfe_mod
    PROGRAM e01tnfe

!      E01TNF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e01tmf, e01tnf, g05kff, g05saf, nag_wp
       USE e01tnfe_mod, ONLY : funct, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: lseed = 1
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: fun
       INTEGER                             :: genid, i, ifail, liq, lrq,       &
                                              lstate, m, n, nq, nw, subid
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: f(:), q(:), qx(:,:), rq(:),      &
                                              x(:,:), xe(:,:)
       INTEGER, ALLOCATABLE                :: iq(:), state(:)
       INTEGER                             :: seed(lseed), seed2(lseed)
!      .. Intrinsic Functions ..
       INTRINSIC                              abs
!      .. Executable Statements ..
       WRITE (nout,*) 'E01TNF Example Program Results'

!      Skip heading in data file
       READ (nin,*)

!      Read in the base generator information and seeds
       READ (nin,*) genid, subid, seed(1), seed2(1)

!      Initial call to initialiser to get size of STATE array
       lstate = 0
       ALLOCATE (state(lstate))
       ifail = 0
       CALL g05kff(genid,subid,seed,lseed,state,lstate,ifail)

!      Reallocate STATE
       DEALLOCATE (state)
       ALLOCATE (state(lstate))

!      Initialize the generator to a repeatable sequence
       ifail = 0
       CALL g05kff(genid,subid,seed,lseed,state,lstate,ifail)

!      Input the number of nodes.
       READ (nin,*) m
       liq = 2*m + 1
       lrq = 21*m + 11
       ALLOCATE (x(5,m),f(m),iq(liq),rq(lrq))

!      Generate the data points X
       ifail = 0
       CALL g05saf(5*m,state,x,ifail)

!      Evaluate F
       DO i = 1, m
          f(i) = funct(x(1,i))
       END DO

!      Generate the interpolant using E01TMF.
       nq = 0
       nw = 0

       ifail = 0
       CALL e01tmf(m,x,f,nw,nq,iq,rq,ifail)

!      Input the number of evaluation points.
       READ (nin,*) n
       ALLOCATE (xe(5,n),q(n),qx(5,n))

!      Generate repeatable evaluation points.
       ifail = 0
       CALL g05kff(genid,subid,seed2,lseed,state,lstate,ifail)
       ifail = 0
       CALL g05saf(5*n,state,xe,ifail)

!      Evaluate the interpolant.
       ifail = 0
       CALL e01tnf(m,x,f,iq,rq,n,xe,q,qx,ifail)

       WRITE (nout,99997)
       WRITE (nout,99998)
       DO i = 1, n
          fun = funct(xe(1,i))
          WRITE (nout,99999) i, fun, q(i), abs(fun-q(i))
       END DO

99999  FORMAT (1X,I4,1X,3F10.4)
99998  FORMAT (4X,'---|',20('-'),'+',15('-'))
99997  FORMAT (/4X,'I  |',2X,'F(I)',6X,'Q(I)',4X,'|',1X,'|F(I)-Q(I)|')
    END PROGRAM e01tnfe
