    PROGRAM f08nvfe

!      F08NVF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp, x04dbf, zgebak, zgebal, zgehrd, zhseqr, &
                               ztrevc, zunghr
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       COMPLEX (KIND=nag_wp)           :: firstnz
       INTEGER                         :: i, ifail, ihi, ilo, info, j, lda,    &
                                          ldh, ldvl, ldvr, lwork, m, n
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), h(:,:), tau(:), vl(:,:),  &
                                             vr(:,:), w(:), work(:)
       REAL (KIND=nag_wp), ALLOCATABLE :: rwork(:), scale(:)
       LOGICAL                         :: select(1)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Intrinsic Functions ..
       INTRINSIC                          aimag, real
!      .. Executable Statements ..
       WRITE (nout,*) 'F08NVF Example Program Results'
       FLUSH (nout)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       ldvl = 1
       lda = n
       ldh = n
       ldvr = n
       lwork = 64*n
       ALLOCATE (a(lda,n),h(ldh,n),tau(n),vl(ldvl,1),vr(ldvr,n),w(n), &
          work(lwork),rwork(n),scale(n))

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

!      Balance A
!      The NAG name equivalent of zgebal is f08nvf
       CALL zgebal('Both',n,a,lda,ilo,ihi,scale,info)

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

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

!      Form Q explicitly, storing the result in VR
!      The NAG name equivalent of zunghr is f08ntf
       CALL zunghr(n,1,n,vr,ldvr,tau,work,lwork,info)

!      Calculate the eigenvalues and Schur factorization of A
!      The NAG name equivalent of zhseqr is f08psf
       CALL zhseqr('Schur form','Vectors',n,ilo,ihi,h,ldh,w,vr,ldvr,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)

!         Calculate the eigenvectors of A, storing the result in VR
!         The NAG name equivalent of ztrevc is f08qxf
          CALL ztrevc('Right','Backtransform',select,n,h,ldh,vl,ldvl,vr,ldvr, &
             n,m,work,rwork,info)

!         The NAG name equivalent of zgebak is f08nwf
          CALL zgebak('Both','Right',n,ilo,ihi,scale,m,vr,ldvr,info)

!         Print eigenvectors

          WRITE (nout,*)
          FLUSH (nout)

!         Normalize the eigenvectors
          DO i = 1, m
             DO j = n, 1, -1
                IF (vr(j,i)/=(0._nag_wp,0._nag_wp)) THEN
                   firstnz = vr(j,i)
                END IF
             END DO
             vr(1:n,i) = vr(1:n,i)/firstnz
          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,vr,ldvr,'Bracketed','F7.4', &
             'Contents of array VR','Integer',rlabs,'Integer',clabs,80,0, &
             ifail)

       END IF

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