    PROGRAM f08hpfe

!      F08HPF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp, x04daf, zhbevx
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER   :: zero = 0.0E+0_nag_wp
       INTEGER, PARAMETER              :: nin = 5, nout = 6
       CHARACTER (1), PARAMETER        :: uplo = 'U'
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: abstol, vl, vu
       INTEGER                         :: i, ifail, il, info, iu, j, kd, ldab, &
                                          ldq, ldz, m, n
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: ab(:,:), q(:,:), work(:), z(:,:)
       REAL (KIND=nag_wp), ALLOCATABLE :: rwork(:), w(:)
       INTEGER, ALLOCATABLE            :: iwork(:), jfail(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max, min
!      .. Executable Statements ..
       WRITE (nout,*) 'F08HPF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n, kd
       ldab = kd + 1
       ldq = n
       ldz = n
       m = n
       ALLOCATE (ab(ldab,n),q(ldq,n),work(n),z(ldz,m),rwork(7*n),w(n), &
          iwork(5*n),jfail(n))

!      Read the lower and upper bounds of the interval to be searched,
!      and read the upper or lower triangular part of the matrix A
!      from data file

       READ (nin,*) vl, vu
       IF (uplo=='U') THEN
          READ (nin,*) ((ab(kd+1+i-j,j),j=i,min(n,i+kd)),i=1,n)
       ELSE IF (uplo=='L') THEN
          READ (nin,*) ((ab(1+i-j,j),j=max(1,i-kd),i),i=1,n)
       END IF

!      Set the absolute error tolerance for eigenvalues.  With ABSTOL
!      set to zero, the default value is used instead

       abstol = zero

!      Solve the band symmetric eigenvalue problem
!      The NAG name equivalent of zhbevx is f08hpf
       CALL zhbevx('Vectors','Values in range',uplo,n,kd,ab,ldab,q,ldq,vl,vu, &
          il,iu,abstol,m,w,z,ldz,work,rwork,iwork,jfail,info)

       IF (info>=0) THEN

!         Print solution

          WRITE (nout,99999) 'Number of eigenvalues found =', m
          WRITE (nout,*)
          WRITE (nout,*) 'Eigenvalues'
          WRITE (nout,99998) w(1:m)
          FLUSH (nout)

!         ifail: behaviour on error exit
!                =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
          ifail = 0
          CALL x04daf('General',' ',n,m,z,ldz,'Selected eigenvectors',ifail)

          IF (info>0) THEN
             WRITE (nout,99999) 'INFO eigenvectors failed to converge, INFO =' &
                , info
             WRITE (nout,*) 'Indices of eigenvectors that did not converge'
             WRITE (nout,99997) jfail(1:m)
          END IF
       ELSE
          WRITE (nout,99999) 'Failure in ZHBEVX. INFO =', info
       END IF

99999  FORMAT (1X,A,I5)
99998  FORMAT (3X,(8F8.4))
99997  FORMAT (3X,(8I8))
    END PROGRAM f08hpfe
