!   E04HDF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.
    MODULE e04hdfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: liw = 1, n = 4, nout = 6
       INTEGER, PARAMETER                  :: lh = n*(n-1)/2
       INTEGER, PARAMETER                  :: lw = 5*n
    CONTAINS
!dec$ attributes offload : mic :: funct
       SUBROUTINE funct(iflag,n,xc,fc,gc,iw,liw,w,lw)
!         Routine to evaluate objective function and its 1st derivatives.

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: fc
          INTEGER, INTENT (INOUT)             :: iflag
          INTEGER, INTENT (IN)                :: liw, lw, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: gc(n)
          REAL (KIND=nag_wp), INTENT (INOUT)  :: w(lw)
          REAL (KIND=nag_wp), INTENT (IN)     :: xc(n)
          INTEGER, INTENT (INOUT)             :: iw(liw)
!         .. Executable Statements ..
          fc = (xc(1)+10.0_nag_wp*xc(2))**2 + 5.0_nag_wp*(xc(3)-xc(4))**2 + &
             (xc(2)-2.0_nag_wp*xc(3))**4 + 10.0_nag_wp*(xc(1)-xc(4))**4
          gc(1) = 2.0_nag_wp*(xc(1)+10.0_nag_wp*xc(2)) + &
             40.0_nag_wp*(xc(1)-xc(4))**3
          gc(2) = 20.0_nag_wp*(xc(1)+10.0_nag_wp*xc(2)) + &
             4.0_nag_wp*(xc(2)-2.0_nag_wp*xc(3))**3
          gc(3) = 10.0_nag_wp*(xc(3)-xc(4)) - 8.0_nag_wp*(xc(2)-2.0_nag_wp*xc( &
             3))**3
          gc(4) = 10.0_nag_wp*(xc(4)-xc(3)) - 40.0_nag_wp*(xc(1)-xc(4))**3

          RETURN

!dec$ attributes offload : mic :: funct
       END SUBROUTINE funct
!dec$ attributes offload : mic :: h
       SUBROUTINE h(iflag,n,xc,fhesl,lh,fhesd,iw,liw,w,lw)
!         Routine to evaluate 2nd derivatives

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (INOUT)             :: iflag
          INTEGER, INTENT (IN)                :: lh, liw, lw, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (INOUT)  :: fhesd(n), w(lw)
          REAL (KIND=nag_wp), INTENT (OUT)    :: fhesl(lh)
          REAL (KIND=nag_wp), INTENT (IN)     :: xc(n)
          INTEGER, INTENT (INOUT)             :: iw(liw)
!         .. Executable Statements ..
          fhesd(1) = 2.0_nag_wp + 120.0_nag_wp*(xc(1)-xc(4))**2
          fhesd(2) = 200.0_nag_wp + 12.0_nag_wp*(xc(2)-2.0_nag_wp*xc(3))**2
          fhesd(3) = 10.0_nag_wp + 48.0_nag_wp*(xc(2)-2.0_nag_wp*xc(3))**2
          fhesd(4) = 10.0_nag_wp + 120.0_nag_wp*(xc(1)-xc(4))**2

          fhesl(1) = 20.0_nag_wp
          fhesl(2) = 0.0_nag_wp
          fhesl(3) = -24.0_nag_wp*(xc(2)-2.0_nag_wp*xc(3))**2
          fhesl(4) = -120.0_nag_wp*(xc(1)-xc(4))**2
          fhesl(5) = 0.0_nag_wp
          fhesl(6) = -10.0_nag_wp

          RETURN

!dec$ attributes offload : mic :: h
       END SUBROUTINE h
    END MODULE e04hdfe_mod
    PROGRAM e04hdfe

!      E04HDF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e04hcf, e04hdf
       USE e04hdfe_mod, ONLY : funct, h, lh, liw, lw, n, nag_wp, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: f
       INTEGER                             :: i, ifail, k
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: g(n), hesd(n), hesl(lh), w(lw),  &
                                              x(n)
       INTEGER                             :: iw(liw)
!      .. Executable Statements ..
       WRITE (nout,*) 'E04HDF Example Program Results'

!      Set up an arbitrary point at which to check the derivatives

       x(1:n) = (/ 1.46_nag_wp, -0.82_nag_wp, 0.57_nag_wp, 1.21_nag_wp/)

       WRITE (nout,*)
       WRITE (nout,*) 'The test point is'
       WRITE (nout,99999) x(1:n)

!      Check the 1st derivatives

       ifail = 0
       CALL e04hcf(n,funct,x,f,g,iw,liw,w,lw,ifail)

!      Check the 2nd derivatives

       ifail = -1
       CALL e04hdf(n,funct,h,x,g,hesl,lh,hesd,iw,liw,w,lw,ifail)

       IF (ifail>=0) THEN
          WRITE (nout,*)

          IF (ifail==0) THEN
             WRITE (nout,*) &
                '2nd derivatives are consistent with 1st derivatives'
          ELSE IF (ifail==2) THEN
             WRITE (nout,*) 'Probable error in calculation of 2nd derivatives'
          END IF

          WRITE (nout,*)
          WRITE (nout,99998) &
             'At the test point, FUNCT gives the function value', f
          WRITE (nout,*) 'and the 1st derivatives'
          WRITE (nout,99997) g(1:n)
          WRITE (nout,*)
          WRITE (nout,*) 'H gives the lower triangle of the Hessian matrix'
          WRITE (nout,99996) hesd(1)

          k = 1

          DO i = 2, n
             WRITE (nout,99996) hesl(k:(k+i-2)), hesd(i)
             k = k + i - 1
          END DO

       END IF

99999  FORMAT (1X,4F9.4)
99998  FORMAT (1X,A,1P,E12.4)
99997  FORMAT (1X,1P,4E12.3)
99996  FORMAT (1X,1P,4E12.3)
    END PROGRAM e04hdfe
