    Program f08nvfe

!     F08NVF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. 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 Procedures ..
      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
