!   E04LBF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.
    MODULE e04lbfe_mod

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

!         .. 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
!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 e04lbfe_mod
    PROGRAM e04lbfe

!      E04LBF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e04hcf, e04hdf, e04lbf
       USE e04lbfe_mod, ONLY : funct, h, lh, liw, lw, monit, n, nag_wp, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: eta, f, stepmx, xtol
       INTEGER                             :: ibound, ifail, iprint, maxcal, nz
!      .. 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)
!      .. Intrinsic Functions ..
       INTRINSIC                              count
!      .. Executable Statements ..
       WRITE (nout,*) 'E04LBF Example Program Results'
       FLUSH (nout)

!      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/)

!      Check the 1st derivatives

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

!      Check the 2nd derivatives

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

!      Continue setting parameters for E04LBF

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

       maxcal = 50*n
       eta = 0.9_nag_wp

!      Set XTOL to zero so that E04LBF will use the default tolerance

       xtol = 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 unconstrained, 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 e04lbf(n,funct,h,monit,iprint,maxcal,eta,xtol,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,99998) 'at the point', x(1:n)
          WRITE (nout,*) 'The corresponding (machine dependent) gradient is'
          WRITE (nout,99997) g(1:n)
          WRITE (nout,99996) 'ISTATE contains', istate(1:n)

          nz = count(istate(1:n)>0)

          WRITE (nout,99995) 'and HESD contains', hesd(1:nz)
       END SELECT

99999  FORMAT (1X,A,F9.4)
99998  FORMAT (1X,A,4F9.4)
99997  FORMAT (23X,1P,4E12.3)
99996  FORMAT (1X,A,4I5)
99995  FORMAT (1X,A,4E12.4)
    END PROGRAM e04lbfe
