    PROGRAM f08wpfe

!      F08WPF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f06bnf, nag_wp, x02ajf, x02amf, zggevx
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nb = 64, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: abnorm, abnrm, bbnrm, eps, erbnd,    &
                                          rcnd, small, tol
       INTEGER                         :: i, ihi, ilo, info, j, lda, ldb,      &
                                          ldvr, lwork, n
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), alpha(:), b(:,:),         &
                                             beta(:), vr(:,:), work(:)
       COMPLEX (KIND=nag_wp)           :: dummy(1,1)
       REAL (KIND=nag_wp), ALLOCATABLE :: lscale(:), rconde(:), rcondv(:),     &
                                          rscale(:), rwork(:)
       INTEGER, ALLOCATABLE            :: iwork(:)
       LOGICAL, ALLOCATABLE            :: bwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          abs, max, nint, real
!      .. Executable Statements ..
       WRITE (nout,*) 'F08WPF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       lda = n
       ldb = n
       ldvr = n
       ALLOCATE (a(lda,n),alpha(n),b(ldb,n),beta(n),vr(ldvr,n),lscale(n), &
          rconde(n),rcondv(n),rscale(n),rwork(6*n),iwork(n+2),bwork(n))

!      Use routine workspace query to get optimal workspace.
       lwork = -1
!      The NAG name equivalent of zggevx is f08wpf
       CALL zggevx('Balance','No vectors (left)','Vectors (right)', &
          'Both reciprocal condition numbers',n,a,lda,b,ldb,alpha,beta,dummy, &
          1,vr,ldvr,ilo,ihi,lscale,rscale,abnrm,bbnrm,rconde,rcondv,dummy, &
          lwork,rwork,iwork,bwork,info)

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

!      Read in the matrices A and B

       READ (nin,*) (a(i,1:n),i=1,n)
       READ (nin,*) (b(i,1:n),i=1,n)

!      Solve the generalized eigenvalue problem

!      The NAG name equivalent of zggevx is f08wpf
       CALL zggevx('Balance','No vectors (left)','Vectors (right)', &
          'Both reciprocal condition numbers',n,a,lda,b,ldb,alpha,beta,dummy, &
          1,vr,ldvr,ilo,ihi,lscale,rscale,abnrm,bbnrm,rconde,rcondv,work, &
          lwork,rwork,iwork,bwork,info)

       IF (info>0) THEN
          WRITE (nout,*)
          WRITE (nout,99999) 'Failure in ZGGEVX. INFO =', info
       ELSE

!         Compute the machine precision, the safe range parameter
!         SMALL and sqrt(ABNRM**2+BBNRM**2)

          eps = x02ajf()
          small = x02amf()
          abnorm = f06bnf(abnrm,bbnrm)
          tol = eps*abnorm

!         Print out eigenvalues and vectors and associated condition
!         number and bounds

          DO j = 1, n

!            Print out information on the jth eigenvalue

             WRITE (nout,*)
             IF ((abs(alpha(j)))*small>=abs(beta(j))) THEN
                WRITE (nout,99998) 'Eigenvalue(', j, ')', &
                   ' is numerically infinite or undetermined', 'ALPHA(', j, &
                   ') = ', alpha(j), ', BETA(', j, ') = ', beta(j)
             ELSE
                WRITE (nout,99997) 'Eigenvalue(', j, ') = ', alpha(j)/beta(j)
             END IF
             rcnd = rconde(j)
             WRITE (nout,*)
             WRITE (nout,99996) 'Reciprocal condition number = ', rcnd
             IF (rcnd>0.0E0_nag_wp) THEN
                erbnd = tol/rcnd
                WRITE (nout,99996) 'Error bound                 = ', erbnd
             ELSE
                WRITE (nout,*) 'Error bound is infinite'
             END IF

!            Print out information on the jth eigenvector

             WRITE (nout,*)
             WRITE (nout,99995) 'Eigenvector(', j, ')', (vr(i,j),i=1,n)
             rcnd = rcondv(j)
             WRITE (nout,*)
             WRITE (nout,99996) 'Reciprocal condition number = ', rcnd
             IF (rcnd>0.0E0_nag_wp) THEN
                erbnd = tol/rcnd
                WRITE (nout,99996) 'Error bound                 = ', erbnd
             ELSE
                WRITE (nout,*) 'Error bound is infinite'
             END IF
          END DO

       END IF

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