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

    MODULE e01tlfe_mod

!      E01TLF 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
       SUBROUTINE funct(m,x,f)
!         This subroutine evaluates the 4D function funct.

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: m
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: f(m)
          REAL (KIND=nag_wp), INTENT (IN)     :: x(4,m)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: c1, c2, c3, c4
          INTEGER                             :: i
!         .. Intrinsic Functions ..
          INTRINSIC                              cos
!         .. Executable Statements ..
          DO i = 1, m
             c1 = cos(six*x(1,i))
             c2 = cos(six*x(2,i))
             c3 = six + six*(three*x(3,i)-one)**2
             c4 = 1.25_nag_wp + cos(5.4_nag_wp*x(4,i))
             f(i) = c4*c1*c2/c3
          END DO
          RETURN
!dec$ attributes offload : mic :: funct
       END SUBROUTINE funct
    END MODULE e01tlfe_mod
    PROGRAM e01tlfe

!      E01TLF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e01tkf, e01tlf, g05kff, g05saf, nag_wp
       USE e01tlfe_mod, ONLY : funct, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: lseed = 1
!      .. Local Scalars ..
       INTEGER                             :: genid, i, ifail, liq, lrq,       &
                                              lstate, m, n, nq, nw, subid
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: f(:), fun(:), q(:), qx(:,:),     &
                                              rq(:), x(:,:), xe(:,:)
       INTEGER, ALLOCATABLE                :: iq(:), state(:)
       INTEGER                             :: seed(lseed), seed2(lseed)
!      .. Intrinsic Functions ..
       INTRINSIC                              abs
!      .. Executable Statements ..
       WRITE (nout,*) 'E01TLF 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 = 15*m + 9
       ALLOCATE (x(4,m),f(m),iq(liq),rq(lrq))

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

!      Evaluate F
       CALL funct(m,x,f)

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

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

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

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

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

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

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