    PROGRAM f08nufe

!      F08NUF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp, x04dbf, zgehrd, zhsein, zhseqr, zunmhr
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: thresh
       INTEGER                         :: i, ifail, info, lda, ldc, ldh, ldvl, &
                                          ldz, lwork, m, n
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), c(:,:), h(:,:), tau(:),   &
                                             vl(:,:), w(:), work(:), z(:,:)
       REAL (KIND=nag_wp), ALLOCATABLE :: rwork(:)
       INTEGER, ALLOCATABLE            :: ifaill(:), ifailr(:)
       LOGICAL, ALLOCATABLE            :: select(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Intrinsic Functions ..
       INTRINSIC                          aimag, real
!      .. Executable Statements ..
       WRITE (nout,*) 'F08NUF Example Program Results'
       FLUSH (nout)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       ldz = 1
       lda = n
       ldc = n
       ldh = n
       ldvl = n
       lwork = 64*n
       ALLOCATE (a(lda,n),c(ldc,n),h(ldh,n),tau(n),vl(ldvl,n),w(n), &
          work(lwork),z(ldz,1),rwork(n),ifaill(n),ifailr(n),select(n))

!      Read A from data file
       READ (nin,*) (a(i,1:n),i=1,n)

       READ (nin,*) thresh

!      Reduce A to upper Hessenberg form H = (Q**H)*A*Q
!      The NAG name equivalent of zgehrd is f08nsf
       CALL zgehrd(n,1,n,a,lda,tau,work,lwork,info)

!      Copy A to H
       h(1:n,1:n) = a(1:n,1:n)

!      Calculate the eigenvalues of H (same as A)
!      The NAG name equivalent of zhseqr is f08psf
       CALL zhseqr('Eigenvalues','No vectors',n,1,n,h,ldh,w,z,ldz,work,lwork, &
          info)

       WRITE (nout,*)
       IF (info>0) THEN
          WRITE (nout,*) 'Failure to converge.'
       ELSE
          WRITE (nout,*) 'Eigenvalues'
          WRITE (nout,99999) (' (',real(w(i)),',',aimag(w(i)),')',i=1,n)
          FLUSH (nout)

          DO i = 1, n
             select(i) = real(w(i)) < thresh
          END DO

!         Calculate the eigenvectors of H (as specified by SELECT),
!         storing the result in C
!         The NAG name equivalent of zhsein is f08pxf
          CALL zhsein('Right','QR','No initial vectors',select,n,a,lda,w,vl, &
             ldvl,c,ldc,n,m,work,rwork,ifaill,ifailr,info)

!         Calculate the eigenvectors of A = Q * (eigenvectors of H)
!         The NAG name equivalent of zunmhr is f08nuf
          CALL zunmhr('Left','No transpose',n,m,1,n,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', &
             'Contents of array C','Integer',rlabs,'Integer',clabs,80,0,ifail)

       END IF

99999  FORMAT ((3X,4(A,F7.4,A,F7.4,A:)))
    END PROGRAM f08nufe
