    PROGRAM f08nhfe

!      F08NHF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dgebak, dgebal, dgehrd, dhseqr, dorghr, dtrevc, &
                               nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: firstnz
       INTEGER                         :: i, ifail, ihi, ilo, info, j, lda,    &
                                          ldh, ldvl, ldvr, lwork, m, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), h(:,:), scale(:), tau(:),    &
                                          vl(:,:), vr(:,:), wi(:), work(:),    &
                                          wr(:)
       LOGICAL                         :: select(1)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08NHF Example Program Results'
!      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),scale(n),tau(n),vl(ldvl,1),vr(ldvr,n), &
          wi(n),work(lwork),wr(n))

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

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

!      Reduce A to upper Hessenberg form H = (Q**T)*A*Q
!      The NAG name equivalent of dgehrd is f08nef
       CALL dgehrd(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 dorghr is f08nff
       CALL dorghr(n,1,n,vr,ldvr,tau,work,lwork,info)

!      Calculate the eigenvalues and Schur factorization of A
!      The NAG name equivalent of dhseqr is f08pef
       CALL dhseqr('Schur form','Vectors',n,ilo,ihi,h,ldh,wr,wi,vr,ldvr,work, &
          lwork,info)

       WRITE (nout,*)
       IF (info>0) THEN
          WRITE (nout,*) 'Failure to converge.'
       ELSE
          WRITE (nout,*) 'Eigenvalues'
          WRITE (nout,99999) (' (',wr(i),',',wi(i),')',i=1,n)

!         Calculate the eigenvectors of A, storing the result in VR

!         The NAG name equivalent of dtrevc is f08qkf
          CALL dtrevc('Right','Backtransform',select,n,h,ldh,vl,ldvl,vr,ldvr, &
             n,m,work,info)

!         The NAG name equivalent of dgebak is f08njf
          CALL dgebak('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 x04caf('General',' ',n,m,vr,ldvr,'Contents of array VR',ifail)

       END IF

99999  FORMAT (1X,A,F8.4,A,F8.4,A)
    END PROGRAM f08nhfe
