    PROGRAM f08jvfe

!      F08JVF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp, x04dbf, zhbtrd, zstedc
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
       CHARACTER (1), PARAMETER        :: uplo = 'U'
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, info, j, kd, ldab, ldz,    &
                                          lgn, liwork, lrwork, lwork, n
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: ab(:,:), work(:), z(:,:)
       COMPLEX (KIND=nag_wp)           :: cdum(1)
       REAL (KIND=nag_wp), ALLOCATABLE :: d(:), e(:), rwork(:)
       REAL (KIND=nag_wp)              :: rdum(1)
       INTEGER                         :: idum(1)
       INTEGER, ALLOCATABLE            :: iwork(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Intrinsic Functions ..
       INTRINSIC                          ceiling, log, max, min, nint, real
!      .. Executable Statements ..
       WRITE (nout,*) 'F08JVF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n, kd
       ldab = kd + 1
       ldz = n
       lgn = ceiling(log(real(n,kind=nag_wp))/log(2.0_nag_wp))
       ALLOCATE (ab(ldab,n),z(ldz,n),d(n),e(n-1))

!      Use routine workspace query to get optimal workspace.
       lwork = -1
       lrwork = -1
       liwork = -1
!      The NAG name equivalent of zstedc is f08jvf
       CALL zstedc('V',n,d,e,z,ldz,cdum,lwork,rdum,lrwork,idum,liwork,info)

!      Make sure that there is enough workspace.
       lwork = max(n*n,nint(real(cdum(1))))
       lrwork = max(1+3*n+2*n*lgn+4*n*n,nint(rdum(1)))
       liwork = max(6+6*n+5*n*lgn,idum(1))
       ALLOCATE (work(lwork),rwork(lrwork),iwork(liwork))

!      Read the upper or lower triangular part of the band matrix A
!      from data file

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

!      Reduce A to tridiagonal form T = (Z**T)*A*Z, and form Z

!      The NAG name equivalent of zhbtrd is f08hsf
       CALL zhbtrd('V',uplo,n,kd,ab,ldab,d,e,z,ldz,work,info)

!      Calculate all the eigenvalues and eigenvectors of A,
!      from T and Z

!      The NAG name equivalent of zstedc is f08jvf
       CALL zstedc('V',n,d,e,z,ldz,work,lwork,rwork,lrwork,iwork,liwork,info)

       IF (info==0) THEN

!         Print eigenvalues and eigenvectors

          WRITE (nout,*) 'Eigenvalues'
          WRITE (nout,99999) d(1:n)

          WRITE (nout,*)
          FLUSH (nout)

!         Normalize the eigenvectors
          DO i = 1, n
             z(1:n,i) = z(1:n,i)/z(1,i)
          END DO

!         ifail: behaviour on error exit
!                =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
          ifail = 0
          CALL x04dbf('General',' ',n,n,z,ldz,'Bracketed','F7.4', &
             'Eigenvectors','Integer',rlabs,'Integer',clabs,80,0,ifail)

       ELSE
          WRITE (nout,99998) 'Failure in ZSTEDC. INFO = ', info
       END IF

99999  FORMAT (4X,F8.4,3(10X,F8.4))
99998  FORMAT (1X,A,I10)
    END PROGRAM f08jvfe
