    PROGRAM f08npfe

!      F08NPF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. 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, erbnd, rcnd, 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 Functions ..
       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

          DO j = 1, n

!            Print information on jth eigenvalue

             WRITE (nout,*)
             WRITE (nout,99999) 'Eigenvalue(', j, ') = ', w(j)
             rcnd = rconde(j)
             WRITE (nout,*)
             WRITE (nout,99998) 'Reciprocal condition number = ', rcnd
             IF (rcnd>0.0E0_nag_wp) THEN
                erbnd = tol/rcnd
                WRITE (nout,99998) 'Error bound                 = ', erbnd
             ELSE
                WRITE (nout,*) 'Error bound is infinite'
             END IF

!            Print information on jth eigenvector

             WRITE (nout,*)
             WRITE (nout,99997) 'Eigenvector(', j, ')', vr(1:n,j)
             rcnd = rcondv(j)
             WRITE (nout,*)
             WRITE (nout,99998) 'Reciprocal condition number = ', rcnd
             IF (rcnd>0.0E0_nag_wp) THEN
                erbnd = tol/rcnd
                WRITE (nout,99998) 'Error bound                 = ', erbnd
             ELSE
                WRITE (nout,*) 'Error bound is infinite'
             END IF
          END DO
       ELSE
          WRITE (nout,*)
          WRITE (nout,99996) 'Failure in ZGEEVX. INFO =', info
       END IF

99999  FORMAT (1X,A,I2,A,'(',1P,E11.4,',',1P,E11.4,')')
99998  FORMAT (1X,A,1P,E8.1)
99997  FORMAT (1X,A,I2,A/3(1X,'(',1P,E11.4,',',1P,E11.4,')':))
99996  FORMAT (1X,A,I4)
    END PROGRAM f08npfe
