    PROGRAM f08nbfe

!      F08NBF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dgeevx, nag_wp, x02ajf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nb = 64, nin = 5, nout = 6
!      .. Local Scalars ..
       COMPLEX (KIND=nag_wp)           :: eig
       REAL (KIND=nag_wp)              :: abnrm, eps, erbnd, rcnd, tol
       INTEGER                         :: i, ihi, ilo, info, j, lda, ldvl,     &
                                          ldvr, lwork, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), rconde(:), rcondv(:),        &
                                          scale(:), vl(:,:), vr(:,:), wi(:),   &
                                          work(:), wr(:)
       REAL (KIND=nag_wp)              :: dummy(1)
       INTEGER, ALLOCATABLE            :: iwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          cmplx, max, nint
!      .. Executable Statements ..
       WRITE (nout,*) 'F08NBF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       lda = n
       ldvl = n
       ldvr = n
       lwork = (2+nb)*n
       ALLOCATE (a(lda,n),rconde(n),rcondv(n),scale(n),vl(ldvl,n),vr(ldvr,n), &
          wi(n),wr(n),iwork(2*n-2))

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

!      Make sure that there is enough workspace for blocksize nb.
       lwork = max((nb+2)*n,nint(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 dgeevx is f08nbf
       CALL dgeevx('Balance','Vectors (left)','Vectors (right)', &
          'Both reciprocal condition numbers',n,a,lda,wr,wi,vl,ldvl,vr,ldvr, &
          ilo,ihi,scale,abnrm,rconde,rcondv,work,lwork,iwork,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,*)
             IF (wi(j)==0.0E0_nag_wp) THEN
                WRITE (nout,99999) 'Eigenvalue(', j, ') = ', wr(j)
             ELSE
                eig = cmplx(wr(j),wi(j),kind=nag_wp)
                WRITE (nout,99998) 'Eigenvalue(', j, ') = ', eig
             END IF
             rcnd = rconde(j)
             WRITE (nout,*)
             WRITE (nout,99997) 'Reciprocal condition number = ', rcnd
             IF (rcnd>0.0E0_nag_wp) THEN
                erbnd = tol/rcnd
                WRITE (nout,99997) 'Error bound                 = ', erbnd
             ELSE
                WRITE (nout,*) 'Error bound is infinite'
             END IF

!            Print information on jth eigenvector

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

99999  FORMAT (1X,A,I2,A,1P,E11.4)
99998  FORMAT (1X,A,I2,A,'(',1P,E11.4,',',1P,E11.4,')')
99997  FORMAT (1X,A,1P,E8.1)
99996  FORMAT (1X,A,I2,A)
99995  FORMAT (1X,1P,E11.4)
99994  FORMAT (1X,'(',1P,E11.4,',',1P,E11.4,')')
99993  FORMAT (1X,A,I4)
    END PROGRAM f08nbfe
