!   E04NKF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE e04nkfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: qphx
       SUBROUTINE qphx(nstate,ncolh,x,hx)

!         Routine to compute H*x. (In this version of QPHX, the Hessian
!         matrix H is not referenced explicitly.)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: ncolh, nstate
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: hx(ncolh)
          REAL (KIND=nag_wp), INTENT (IN)     :: x(ncolh)
!         .. Executable Statements ..
          hx(1) = 2.0E0_nag_wp*x(1)
          hx(2) = 2.0E0_nag_wp*x(2)
          hx(3) = 2.0E0_nag_wp*(x(3)+x(4))
          hx(4) = hx(3)
          hx(5) = 2.0E0_nag_wp*x(5)
          hx(6) = 2.0E0_nag_wp*(x(6)+x(7))
          hx(7) = hx(6)

          RETURN

!dec$ attributes offload : mic :: qphx
       END SUBROUTINE qphx
    END MODULE e04nkfe_mod
    PROGRAM e04nkfe

!      E04NKF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e04nkf, nag_wp
       USE e04nkfe_mod, ONLY : nin, nout, qphx
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: obj, sinf
       INTEGER                             :: i, icol, ifail, iobj, jcol,      &
                                              leniz, lenz, m, miniz, minz, n,  &
                                              ncolh, ninf, nname, nnz, ns
       CHARACTER (1)                       :: start
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: a(:), bl(:), bu(:), clamda(:),   &
                                              xs(:), z(:)
       INTEGER, ALLOCATABLE                :: ha(:), istate(:), iz(:), ka(:)
       CHARACTER (8), ALLOCATABLE          :: crname(:)
       CHARACTER (8)                       :: names(5)
!      .. Executable Statements ..
       WRITE (nout,*) 'E04NKF Example Program Results'
       FLUSH (nout)

!      Skip heading in data file.
       READ (nin,*)

       READ (nin,*) n, m
       READ (nin,*) nnz, iobj, ncolh, start, nname
       ALLOCATE (ha(nnz),ka(n+1),istate(n+m),a(nnz),bl(n+m),bu(n+m),xs(n+m), &
          clamda(n+m),crname(nname))

       READ (nin,*) names(1:5)
       READ (nin,*) crname(1:nname)

!      Read the matrix A from data file. Set up KA.

       jcol = 1
       ka(jcol) = 1

       DO i = 1, nnz

!         Element ( HA( I ), ICOL ) is stored in A( I ).

          READ (nin,*) a(i), ha(i), icol

          IF (icol<jcol) THEN

!            Elements not ordered by increasing column index.

             WRITE (nout,99999) 'Element in column', icol, &
                ' found after element in column', jcol, '. Problem', &
                ' abandoned.'
             GO TO 20
          ELSE IF (icol==jcol+1) THEN

!            Index in A of the start of the ICOL-th column equals I.

             ka(icol) = i
             jcol = icol
          ELSE IF (icol>jcol+1) THEN

!            Index in A of the start of the ICOL-th column equals I,
!            but columns JCOL+1,JCOL+2,...,ICOL-1 are empty. Set the
!            corresponding elements of KA to I.

             ka((jcol+1):icol) = i
             jcol = icol
          END IF

       END DO

       ka(n+1) = nnz + 1

!      Columns N,N-1,...,ICOL+1 are empty. Set the corresponding
!      elements of KA accordingly.

       DO i = n, icol + 1, -1
          ka(i) = ka(i+1)
       END DO

       READ (nin,*) bl(1:(n+m))
       READ (nin,*) bu(1:(n+m))

       IF (start=='C') THEN
          READ (nin,*) istate(1:n)
       ELSE IF (start=='W') THEN
          READ (nin,*) istate(1:(n+m))
       END IF

       READ (nin,*) xs(1:n)

!      Solve the QP problem.
!      First call is a workspace query

       leniz = 1
       lenz = 1
       ALLOCATE (iz(leniz),z(lenz))

       ifail = 1
       CALL e04nkf(n,m,nnz,iobj,ncolh,qphx,a,ha,ka,bl,bu,start,names,nname, &
          crname,ns,xs,istate,miniz,minz,ninf,sinf,obj,clamda,iz,leniz,z,lenz, &
          ifail)

       IF (ifail/=0 .AND. ifail/=12 .AND. ifail/=13) THEN
          WRITE (nout,99998) 'Query call to E04NKF failed with IFAIL =', ifail
          GO TO 20
       END IF

       DEALLOCATE (iz,z)

       lenz = minz
       leniz = miniz
       ALLOCATE (iz(leniz),z(lenz))

       ifail = 0
       CALL e04nkf(n,m,nnz,iobj,ncolh,qphx,a,ha,ka,bl,bu,start,names,nname, &
          crname,ns,xs,istate,miniz,minz,ninf,sinf,obj,clamda,iz,leniz,z,lenz, &
          ifail)

20     CONTINUE

99999  FORMAT (/1X,A,I5,A,I5,A,A)
99998  FORMAT (1X,A,I5)
    END PROGRAM e04nkfe
