    MODULE qp_ex04_mod

      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Default Accessibility ..
      PUBLIC
      ! .. Intrinsic Functions ..
      INTRINSIC KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)

    CONTAINS

      SUBROUTINE qp_hx(col,x,hx)

        ! Procedure to evaluate h*x for some matrix h that defines the
        ! hessian of the required qp problem. In this version of qp_hx,
        ! h is implicit.

        ! .. Implicit None Statement ..
        IMPLICIT NONE
        ! .. Scalar Arguments ..
        INTEGER, INTENT (IN) :: col
        ! .. Array Arguments ..
        REAL (wp), INTENT (INOUT) :: hx(:)
        REAL (wp), INTENT (IN) :: x(:)
        ! .. Executable Statements ..
        hx(1) = 2.0_wp*x(1)
        hx(2) = 2.0_wp*x(2)
        hx(3) = 2.0_wp*(x(3)+x(4))
        hx(4) = hx(3)
        hx(5) = 2.0_wp*x(5)
        hx(6) = -2.0_wp*(x(6)+x(7))
        hx(7) = hx(6)

      END SUBROUTINE qp_hx

    END MODULE qp_ex04_mod

    PROGRAM nag_qp_ex04

      ! Example Program Text for nag_qp
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_in, nag_std_out
      USE nag_qp, ONLY : nag_qp_sol
      USE qp_ex04_mod, ONLY : qp_hx, wp
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Local Scalars ..
      INTEGER :: i, m, n
      REAL (wp) :: obj_f
      ! .. Local Arrays ..
      REAL (wp), ALLOCATABLE :: a(:,:), ax_lower(:), ax_upper(:), c(:), x(:), &
       x_lower(:), x_upper(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_qp_ex04'

      READ (nag_std_in,*)          ! Skip heading in data file

      ! Read number of linear constraints (m) and variables (n)
      READ (nag_std_in,*) m, n

      ALLOCATE (a(m,n),ax_lower(m),ax_upper(m),c(n),x(n),x_lower(n), &
       x_upper(n))                 ! Allocate storage

      ! Read in problem data
      READ (nag_std_in,*) c
      READ (nag_std_in,*) (a(i,:),i=1,m)
      READ (nag_std_in,*) x_lower
      READ (nag_std_in,*) ax_lower
      READ (nag_std_in,*) x_upper
      READ (nag_std_in,*) ax_upper
      READ (nag_std_in,*) x

      ! Solve the problem

      CALL nag_qp_sol(x,obj_f,prob_type='S',qp_hx=qp_hx,a=a,ax_lower=ax_lower, &
       ax_upper=ax_upper,c=c,x_lower=x_lower,x_upper=x_upper)

      DEALLOCATE (a,ax_lower,ax_upper,c,x,x_lower, &
       x_upper)                    ! Deallocate storage

    END PROGRAM nag_qp_ex04
