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

    Module e01znfe_mod

!     E01ZNF 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        :: two = 2.0_nag_wp
      Integer, Parameter                   :: d = 6, nin = 5, nout = 6
    Contains
      Function funct(x)
!       This function evaluates the 6D function funct.

!       .. Function Return Value ..
        Real (Kind=nag_wp)                   :: funct
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (In)      :: x(d)
!       .. Executable Statements ..
        funct = x(1)*x(2)*x(3)/(one+two*x(4)*x(5)*x(6))

        Return
      End Function funct
    End Module e01znfe_mod
    Program e01znfe

!     E01ZNF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: e01zmf, e01znf, g05kff, g05saf, nag_wp
      Use e01znfe_mod, Only: d, 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)
!     .. Intrinsic Procedures ..
      Intrinsic                            :: abs, real
!     .. Executable Statements ..
      Write (nout,*) 'E01ZNF Example Program Results'

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

!     Read in the base generator information and seeds
      Read (nin,*) genid, subid, seed(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 = (d+1)*(d+2)/2*m + 2*d + 1
      Allocate (x(d,m),f(m),iq(liq),rq(lrq))

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

!     Evaluate F
      Do i = 1, m
        f(i) = funct(x(1,i))
      End Do

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

      ifail = 0
      Call e01zmf(d,m,x,f,nw,nq,iq,rq,ifail)

!     Input the number of evaluation points.
      Read (nin,*) n
      Allocate (xe(d,n),q(n),qx(d,n))

!     Generate a set of evaluation points lying on diagonal line 
!     xe(1:d,i) = xe(1,i) = i/(n+1).
      Do i = 1, n
        xe(1:d,i) = real(i,kind=nag_wp)/real(n+1,kind=nag_wp)
      End Do

!     Evaluate the interpolant.
      ifail = 0
      Call e01znf(d,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 e01znfe
