! *****************************************************************
! *****************************************************************

subroutine evalf(n,x,f,flag)

  implicit none

  ! SCALAR ARGUMENTS
  integer, intent(in) :: n
  integer, intent(out) :: flag
  real(kind=8), intent(out) :: f

  ! ARRAY ARGUMENTS
  real(kind=8), intent(in) :: x(n)

  ! LOCAL SCALARS
  integer :: status

  flag = 0

  call cutest_ufn(status,n,x,f)
  if ( status .ne. 0 ) flag = - 1

end subroutine evalf

! *****************************************************************
! *****************************************************************

subroutine evalg(n,x,g,flag)

  implicit none

  ! SCALAR ARGUMENTS
  integer, intent(in) :: n
  integer, intent(out) :: flag

  ! ARRAY ARGUMENTS
  real(kind=8), intent(in) :: x(n)
  real(kind=8), intent(out) :: g(n)

  ! LOCAL SCALARS
  integer :: status

  flag = 0

  call cutest_ugr(status,n,x,g)
  if ( status .ne. 0 ) flag = - 1

end subroutine evalg

! *****************************************************************
! *****************************************************************

subroutine evalh(n,x,H,flag)

  implicit none

  ! SCALAR ARGUMENTS
  integer, intent(in) :: n
  integer, intent(out) :: flag

  ! ARRAY ARGUMENTS
  real(kind=8), intent(in) :: x(n)
  real(kind=8), intent(out) :: H(n,n)

  ! COMMON BLOCK
  integer :: hnnzmax
  integer, pointer :: hcol(:),hrow(:)
  real(kind=8), pointer :: hval(:)
  common /sparsehess/ hcol,hrow,hval,hnnzmax
  
  ! LOCAL SCALARS
  integer :: hnnz,k,status

  ! Upper triangle only

  flag = 0

  H(1:n,1:n) = 0.0d0

  call cutest_ush(status,n,x,hnnz,hnnzmax,hval,hrow,hcol)
  if ( status .ne. 0 ) then
     flag = - 1
     return
  end if

  do k = 1,hnnz
     H(hrow(k),hcol(k)) = hval(k)
  end do

end subroutine evalh
