!   E01TLF Example Program Text
!   Mark 24 Release. NAG Copyright 2012.

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

!       .. 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 Procedures ..
        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
      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 Procedures ..
      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
