    Program f08npfe

!     F08NPF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: nag_wp, x02ajf, zgeevx
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: abnrm, eps, tol
      Integer                          :: i, ihi, ilo, info, j, lda, ldvl,     &
                                          ldvr, lwork, n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), vl(:,:), vr(:,:), w(:),    &
                                            work(:)
      Complex (Kind=nag_wp)            :: dummy(1)
      Real (Kind=nag_wp), Allocatable  :: rconde(:), rcondv(:), rwork(:),      &
                                          scale(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max, nint, real
!     .. Executable Statements ..
      Write (nout,*) 'F08NPF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      ldvl = n
      ldvr = n
      Allocate (a(lda,n),vl(ldvl,n),vr(ldvr,n),w(n),rconde(n),rcondv(n), &
        rwork(2*n),scale(n))

!     Use routine workspace query to get optimal workspace.
      lwork = -1
!     The NAG name equivalent of zgeevx is f08npf
      Call zgeevx('Balance','Vectors (left)','Vectors (right)', &
        'Both reciprocal condition numbers',n,a,lda,w,vl,ldvl,vr,ldvr,ilo,ihi, &
        scale,abnrm,rconde,rcondv,dummy,lwork,rwork,info)

!     Make sure that there is enough workspace for blocksize nb.
      lwork = max((nb+1)*n,nint(real(dummy(1))))
      Allocate (work(lwork))

!     Read the matrix A from data file

      Read (nin,*)(a(i,1:n),i=1,n)

!     Solve the eigenvalue problem

!     The NAG name equivalent of zgeevx is f08npf
      Call zgeevx('Balance','Vectors (left)','Vectors (right)', &
        'Both reciprocal condition numbers',n,a,lda,w,vl,ldvl,vr,ldvr,ilo,ihi, &
        scale,abnrm,rconde,rcondv,work,lwork,rwork,info)

      If (info==0) Then

!       Compute the machine precision

        eps = x02ajf()
        tol = eps*abnrm

!       Print the eigenvalues and vectors, and associated condition
!       number and bounds

        Write (nout,*)
        Write (nout,*) 'Eigenvalues'
        Write (nout,*)
        Write (nout,*) '         Eigenvalue           rcond    error'

        Do j = 1, n

!         Print information on jth eigenvalue

          If (rconde(j)>0.0_nag_wp) Then
            If (tol/rconde(j)<10.0_nag_wp*eps) Then
              Write (nout,99999) j, w(j), rconde(j), '-'
            Else
              Write (nout,99998) j, w(j), rconde(j), tol/rconde(j)
            End If
          Else
            Write (nout,99999) j, w(j), rconde(j), 'Inf'
          End If

        End Do

        Write (nout,*)
        Write (nout,*) 'Eigenvectors'
        Write (nout,*)
        Write (nout,*) '         Eigenvector          rcond    error'

        Do j = 1, n

!         Print information on jth eigenvector

          Write (nout,*)

!         Make first real part component be positive
          If (real(vr(1,j))<0.0_nag_wp) Then
            vr(1:n,j) = -vr(1:n,j)
          End If
          If (rcondv(j)>0.0_nag_wp) Then
            If (tol/rcondv(j)<10.0_nag_wp*eps) Then
              Write (nout,99999) j, vr(1,j), rcondv(j), '-'
            Else
              Write (nout,99998) j, vr(1,j), rcondv(j), tol/rcondv(j)
            End If
          Else
            Write (nout,99999) j, vr(1,j), rcondv(j), 'Inf'
          End If

          Write (nout,99997) vr(2:n,j)

        End Do
        Write (nout,*)
        Write (nout,*) 'Errors below 10*machine precision are not displayed'
      Else
        Write (nout,*)
        Write (nout,99996) 'Failure in ZGEEVX. INFO =', info
      End If

99999 Format (1X,I2,1X,'(',1P,E11.4,',',E11.4,')',1X,0P,F7.4,4X,A)
99998 Format (1X,I2,1X,'(',1P,E11.4,',',E11.4,')',1X,0P,F7.4,1X,1P,E8.1)
99997 Format (1X,3X,'(',1P,E11.4,',',E11.4,')')
99996 Format (1X,A,I4)

    End Program f08npfe
