!   E04KDF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.
    MODULE e04kdfe_mod

!      E04KDF 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 = 2, n = 4, nout = 6
       INTEGER, PARAMETER                  :: lh = n*(n-1)/2
       INTEGER, PARAMETER                  :: lw = 7*n + n*(n-1)/2
    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.
!         A global variable could be updated here to count the number of
!         calls of FUNCT with IFLAG = 1 (since NF in MONIT only counts
!         calls with IFLAG = 2)

!         .. 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 ..
          IF (iflag/=1) THEN
             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
          END IF

          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 :: monit
       SUBROUTINE monit(n,xc,fc,gc,istate,gpjnrm,cond,posdef,niter,nf,iw,liw, &
             w,lw)
!         Monitoring routine

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: cond, fc, gpjnrm
          INTEGER, INTENT (IN)                :: liw, lw, n, nf, niter
          LOGICAL, INTENT (IN)                :: posdef
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: gc(n), xc(n)
          REAL (KIND=nag_wp), INTENT (INOUT)  :: w(lw)
          INTEGER, INTENT (IN)                :: istate(n)
          INTEGER, INTENT (INOUT)             :: iw(liw)
!         .. Local Scalars ..
          INTEGER                             :: isj, j
!         .. Executable Statements ..
          WRITE (nout,*)
          WRITE (nout,*) ' Itn     Fn evals              Fn value' // &
             '            Norm of proj gradient'
          WRITE (nout,99999) niter, nf, fc, gpjnrm
          WRITE (nout,*)
          WRITE (nout,*) &
             ' J           X(J)                G(J)           Status'

          DO j = 1, n
             isj = istate(j)

             SELECT CASE (isj)
             CASE (1:)
                WRITE (nout,99998) j, xc(j), gc(j), '    Free'
             CASE (-1)
                WRITE (nout,99998) j, xc(j), gc(j), '    Upper Bound'
             CASE (-2)
                WRITE (nout,99998) j, xc(j), gc(j), '    Lower Bound'
             CASE (-3)
                WRITE (nout,99998) j, xc(j), gc(j), '    Constant'
             END SELECT

          END DO

          IF (cond/=0.0_nag_wp) THEN

             IF (cond>1.0E6_nag_wp) THEN
                WRITE (nout,*)
                WRITE (nout,*) 'Estimated condition number of projected &
                   &Hessian is more than 1.0E+6'
             ELSE
                WRITE (nout,*)
                WRITE (nout,99997) &
                   'Estimated condition number of projected Hessian = ', cond
             END IF

             IF ( .NOT. posdef) THEN
                WRITE (nout,*)
                WRITE (nout,*) &
                   'Projected Hessian matrix is not positive definite'
             END IF

          END IF

          RETURN

99999     FORMAT (1X,I3,6X,I5,2(6X,1P,E20.4))
99998     FORMAT (1X,I2,1X,1P,2E20.4,A)
99997     FORMAT (1X,A,1P,E10.2)
!dec$ attributes offload : mic :: monit
       END SUBROUTINE monit
    END MODULE e04kdfe_mod
    PROGRAM e04kdfe

!      E04KDF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e04hcf, e04kdf
       USE e04kdfe_mod, ONLY : funct, lh, liw, lw, monit, n, nag_wp, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: delta, eta, f, stepmx, xtol
       INTEGER                             :: ibound, ifail, iprint, maxcal
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: bl(n), bu(n), g(n), hesd(n),     &
                                              hesl(lh), w(lw), x(n)
       INTEGER                             :: istate(n), iw(liw)
!      .. Executable Statements ..
       WRITE (nout,*) 'E04KDF Example Program Results'
       FLUSH (nout)

!      Check FUNCT by calling E04HCF at an arbitrary point. Since E04HCF
!      only checks the derivatives calculated when IFLAG = 2, a separate
!      program should be run before using E04HCF or E04KDF to check that
!      FUNCT gives the same values for the GC(J) when IFLAG is set to 1
!      as when IFLAG is set to 2.

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

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

!      Continue setting parameters for E04KDF

!      Set IPRINT to 1 to obtain output from MONIT at each iteration
       iprint = -1

       maxcal = 50*n
       eta = 0.5_nag_wp

!      Set XTOL and DELTA to zero so that E04KDF will use the default
!      values

       xtol = 0.0_nag_wp
       delta = 0.0_nag_wp

!      We estimate that the minimum will be within 4 units of the
!      starting point

       stepmx = 4.0_nag_wp

       ibound = 0

!      X(3) is not bounded, so we set BL(3) to a large negative
!      number and BU(3) to a large positive number

       bl(1:n) = (/ 1.0_nag_wp, -2.0_nag_wp, -1.0E6_nag_wp, 1.0_nag_wp/)
       bu(1:n) = (/ 3.0_nag_wp, 0.0_nag_wp, 1.0E6_nag_wp, 3.0_nag_wp/)

!      Set up starting point

       x(1:n) = (/ 3.0_nag_wp, -1.0_nag_wp, 0.0_nag_wp, 1.0_nag_wp/)

       ifail = -1
       CALL e04kdf(n,funct,monit,iprint,maxcal,eta,xtol,delta,stepmx,ibound, &
          bl,bu,x,hesl,lh,hesd,istate,f,g,iw,liw,w,lw,ifail)

       SELECT CASE (ifail)
       CASE (0,2:)
          WRITE (nout,*)
          WRITE (nout,99999) 'Function value on exit is ', f
          WRITE (nout,99999) 'at the point', x(1:n)
          WRITE (nout,*) 'The corresponding (machine dependent) gradient is'
          WRITE (nout,99998) g(1:n)
          WRITE (nout,99997) 'ISTATE contains', istate(1:n)
          WRITE (nout,99996) 'and HESD contains', hesd(1:n)
       END SELECT

99999  FORMAT (1X,A,4F12.4)
99998  FORMAT (24X,1P,4E12.3)
99997  FORMAT (1X,A,4I5)
99996  FORMAT (1X,A,4E12.4)
    END PROGRAM e04kdfe
