    PROGRAM f08jhfe

!      F08JHF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dsbtrd, dstedc, nag_wp, x04caf
!      .. 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, lwork, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: ab(:,:), d(:), e(:), work(:), z(:,:)
       REAL (KIND=nag_wp)              :: rdum(1)
       INTEGER                         :: idum(1)
       INTEGER, ALLOCATABLE            :: iwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          ceiling, log, max, min, nint, real
!      .. Executable Statements ..
       WRITE (nout,*) 'F08JHF 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.0E0_nag_wp))
       ALLOCATE (ab(ldab,n),d(n),e(n-1),z(ldz,n))

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

!      Make sure that there is enough workspace.
       lwork = 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),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 dsbtrd is f08hef
       CALL dsbtrd('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 dstedc is f08jhf
       CALL dstedc('V',n,d,e,z,ldz,work,lwork,iwork,liwork,info)

       IF (info==0) THEN

!         Print eigenvalues and eigenvectors

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

          WRITE (nout,*)
          FLUSH (nout)

!         Standardize the eigenvectors so that first elements are non-negative.
          DO i = 1, n
             IF (z(1,i)<0.0_nag_wp)  z(1:n,i) = -z(1:n,i)
          END DO

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

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

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