Program f08nhfe

!     F08NHF Example Program Text

!     Mark 26.1 Release. NAG Copyright 2016.

!     .. Use Statements ..
      Use nag_library, Only: blas_damax_val, dgebak, dgebal, dgehrd, dhseqr,   &
                             dnrm2, dorghr, dtrevc, nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: zero = 0.0_nag_wp
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: r
      Integer                          :: i, ifail, ihi, ilo, info, k, 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, largest positive
        Do i = 1, m
          Call blas_damax_val(n,vr(1,i),1,k,r)
          If (vr(k,i)<zero) Then
            vr(1:n,i) = -vr(1:n,i)
          End If
          r = dnrm2(n,vr(1,i),1)
          vr(1:n,i) = vr(1:n,i)/r
        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