    PROGRAM f08gufe

!      F08GUF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dstebz, nag_wp, x04dbf, zhptrd, zstein, zupmtr
!      .. 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, j, ldc, m, n, nsplit
       CHARACTER (1)                   :: uplo
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: ap(:), 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,*) 'F08GUF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       ldc = n
       ALLOCATE (ap(n*(n+1)/2),c(ldc,n),tau(n),work(n),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,*) ((ap(i+j*(j-1)/2),j=i,n),i=1,n)
       ELSE IF (uplo=='L') THEN
          READ (nin,*) ((ap(i+(2*n-j)*(j-1)/2),j=1,i),i=1,n)
       END IF

!      Reduce A to tridiagonal form T = (Q**H)*A*Q
!      The NAG name equivalent of zhptrd is f08gsf
       CALL zhptrd(uplo,n,ap,d,e,tau,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 zupmtr is f08guf
             CALL zupmtr('Left',uplo,'No transpose',n,m,ap,tau,c,ldc,work, &
                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 f08gufe
