    PROGRAM f08fufe

!      F08FUF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dstebz, nag_wp, x04dbf, zhetrd, zstein, zunmtr
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER   :: zero = 0.0E0_nag_wp
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: vl, vu
       INTEGER                         :: i, ifail, info, lda, ldc, lwork, m,  &
                                          n, nsplit
       CHARACTER (1)                   :: uplo
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), c(:,:), tau(:), work(:)
       REAL (KIND=nag_wp), ALLOCATABLE :: d(:), e(:), rwork(:), w(:)
       INTEGER, ALLOCATABLE            :: iblock(:), ifailv(:), isplit(:),     &
                                          iwork(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08FUF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       lda = n
       ldc = n
       lwork = 64*n
       ALLOCATE (a(lda,n),c(ldc,n),tau(n),work(lwork),d(n),e(n),rwork(5*n), &
          w(n),iblock(n),ifailv(n),isplit(n),iwork(3*n))

!      Read A from data file

       READ (nin,*) uplo
       IF (uplo=='U') THEN
          READ (nin,*) (a(i,i:n),i=1,n)
       ELSE IF (uplo=='L') THEN
          READ (nin,*) (a(i,1:i),i=1,n)
       END IF

!      Reduce A to tridiagonal form T = (Q**H)*A*Q
!      The NAG name equivalent of zhetrd is f08fsf
       CALL zhetrd(uplo,n,a,lda,d,e,tau,work,lwork,info)

!      Calculate the two smallest eigenvalues of T (same as A)

!      The NAG name equivalent of dstebz is f08jjf
       CALL dstebz('I','B',n,vl,vu,1,2,zero,d,e,m,nsplit,w,iblock,isplit, &
          rwork,iwork,info)

       WRITE (nout,*)
       IF (info>0) THEN
          WRITE (nout,*) 'Failure to converge.'
       ELSE
          WRITE (nout,*) 'Eigenvalues'
          WRITE (nout,99999) w(1:m)

!         Calculate the eigenvectors of T, storing the result in C
!         The NAG name equivalent of zstein is f08jxf
          CALL zstein(n,d,e,m,w,iblock,isplit,c,ldc,rwork,iwork,ifailv,info)

          IF (info>0) THEN
             WRITE (nout,*) 'Failure to converge.'
          ELSE

!            Calculate the eigenvectors of A = Q * (eigenvectors of T)
!            The NAG name equivalent of zunmtr is f08fuf
             CALL zunmtr('Left',uplo,'No transpose',n,m,a,lda,tau,c,ldc,work, &
                lwork,info)

!            Print eigenvectors
             WRITE (nout,*)
             FLUSH (nout)

!            Normalize the eigenvectors
             DO i = 1, m
                c(1:n,i) = c(1:n,i)/c(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,m,c,ldc,'Bracketed','F7.4', &
                'Eigenvectors','Integer',rlabs,'Integer',clabs,80,0,ifail)

          END IF
       END IF

99999  FORMAT (8X,4(F7.4,11X:))
    END PROGRAM f08fufe
