    PROGRAM f08yxfe

!      F08YXF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f06tff, f06thf, nag_wp, x04dbf, zgeqrf, zggbak, &
                               zggbal, zgghrd, zhgeqz, ztgevc, zungqr, zunmqr
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       COMPLEX (KIND=nag_wp), PARAMETER :: cone = (1.0E0_nag_wp,0.0E0_nag_wp)
       COMPLEX (KIND=nag_wp), PARAMETER :: czero = (0.0E0_nag_wp,0.0E0_nag_wp)
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       COMPLEX (KIND=nag_wp)           :: e
       INTEGER                         :: i, icols, ifail, ihi, ilo, info,     &
                                          irows, jwork, lda, ldb, ldvl, ldvr,  &
                                          lwork, m, n
       LOGICAL                         :: ileft, iright
       CHARACTER (1)                   :: compq, compz, howmny, job, side
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), alpha(:), b(:,:),         &
                                             beta(:), tau(:), vl(:,:),         &
                                             vr(:,:), work(:)
       REAL (KIND=nag_wp), ALLOCATABLE :: lscale(:), rscale(:), rwork(:)
       LOGICAL, ALLOCATABLE            :: select(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Intrinsic Functions ..
       INTRINSIC                          aimag, nint, real
!      .. Executable Statements ..
       WRITE (nout,*) 'F08YXF Example Program Results'
       FLUSH (nout)

!      ileft  is TRUE if left  eigenvectors are required
!      iright is TRUE if right eigenvectors are required

       ileft = .TRUE.
       iright = .TRUE.

!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       lda = n
       ldb = n
       ldvl = n
       ldvr = n
       lwork = 6*n
       ALLOCATE (a(lda,n),alpha(n),b(ldb,n),beta(n),tau(n),vl(ldvl,ldvl), &
          vr(ldvr,ldvr),work(lwork),lscale(n),rscale(n),rwork(6*n),select(n))

!      READ matrix A from data file
       READ (nin,*) (a(i,1:n),i=1,n)

!      READ matrix B from data file
       READ (nin,*) (b(i,1:n),i=1,n)

!      Balance matrix pair (A,B)
       job = 'B'
!      The NAG name equivalent of zggbal is f08wvf
       CALL zggbal(job,n,a,lda,b,ldb,ilo,ihi,lscale,rscale,rwork,info)

!      Matrix A after balancing
!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL x04dbf('General',' ',n,n,a,lda,'Bracketed','F7.4', &
          'Matrix A after balancing','Integer',rlabs,'Integer',clabs,80,0, &
          ifail)
       WRITE (nout,*)
       FLUSH (nout)

!      Matrix B after balancing
       ifail = 0
       CALL x04dbf('General',' ',n,n,b,ldb,'Bracketed','F7.4', &
          'Matrix B after balancing','Integer',rlabs,'Integer',clabs,80,0, &
          ifail)
       WRITE (nout,*)
       FLUSH (nout)

!      Reduce B to triangular form using QR
       irows = ihi + 1 - ilo
       icols = n + 1 - ilo
!      The NAG name equivalent of zgeqrf is f08asf
       CALL zgeqrf(irows,icols,b(ilo,ilo),ldb,tau,work,lwork,info)

!      Apply the orthogonal transformation to A
!      The NAG name equivalent of zunmqr is f08auf
       CALL zunmqr('L','C',irows,icols,irows,b(ilo,ilo),ldb,tau,a(ilo,ilo), &
          lda,work,lwork,info)

!      Initialize VL (for left eigenvectors)
       IF (ileft) THEN

          CALL f06thf('General',n,n,czero,cone,vl,ldvl)
          CALL f06tff('Lower',irows-1,irows-1,b(ilo+1,ilo),ldb,vl(ilo+1,ilo), &
             ldvl)
!         The NAG name equivalent of zungqr is f08atf
          CALL zungqr(irows,irows,irows,vl(ilo,ilo),ldvl,tau,work,lwork,info)

       END IF

!      Initialize VR for right eigenvectors
       IF (iright) CALL f06thf('General',n,n,czero,cone,vr,ldvr)

!      Compute the generalized Hessenberg form of (A,B)
       compq = 'V'
       compz = 'V'
!      The NAG name equivalent of zgghrd is f08wsf
       CALL zgghrd(compq,compz,n,ilo,ihi,a,lda,b,ldb,vl,ldvl,vr,ldvr,info)

!      Matrix A in generalized Hessenberg form
       ifail = 0
       CALL x04dbf('General',' ',n,n,a,lda,'Bracketed','F7.3', &
          'Matrix A in Hessenberg form','Integer',rlabs,'Integer',clabs,80,0, &
          ifail)
       WRITE (nout,*)
       FLUSH (nout)

!      Matrix B in generalized Hessenberg form
       ifail = 0
       CALL x04dbf('General',' ',n,n,b,ldb,'Bracketed','F7.3', &
          'Matrix B in Hessenberg form','Integer',rlabs,'Integer',clabs,80,0, &
          ifail)

!      Routine ZHGEQZ
!      Workspace query: jwork = -1
       jwork = -1
       job = 'S'
!      The NAG name equivalent of zhgeqz is f08xsf
       CALL zhgeqz(job,compq,compz,n,ilo,ihi,a,lda,b,ldb,alpha,beta,vl,ldvl, &
          vr,ldvr,work,jwork,rwork,info)

       WRITE (nout,*)
       WRITE (nout,99999) nint(real(work(1)))
       WRITE (nout,99998) lwork
       WRITE (nout,*)
       FLUSH (nout)

!      Compute the generalized Schur form
!      if the workspace lwork is adequate

       IF (nint(real(work(1)))<=lwork) THEN

!         The NAG name equivalent of zhgeqz is f08xsf
          CALL zhgeqz(job,compq,compz,n,ilo,ihi,a,lda,b,ldb,alpha,beta,vl, &
             ldvl,vr,ldvr,work,lwork,rwork,info)

!         Print the generalized eigenvalues
!         Note: the actual values of beta are real and non-negative

          WRITE (nout,99997)
          DO i = 1, n
             IF (real(beta(i))/=0.0E0_nag_wp) THEN
                e = alpha(i)/beta(i)
                WRITE (nout,99995) i, '(', real(e), ',', aimag(e), ')'
             ELSE
                WRITE (nout,99996) i
             END IF
          END DO
          WRITE (nout,*)
          FLUSH (nout)

!         Compute left and right generalized eigenvectors
!         of the balanced matrix
          howmny = 'B'
          IF (ileft .AND. iright) THEN
             side = 'B'
          ELSE IF (ileft) THEN
             side = 'L'
          ELSE IF (iright) THEN
             side = 'R'
          END IF

!         The NAG name equivalent of ztgevc is f08yxf
          CALL ztgevc(side,howmny,select,n,a,lda,b,ldb,vl,ldvl,vr,ldvr,n,m, &
             work,rwork,info)

!         Compute right eigenvectors of the original matrix

          IF (iright) THEN
             job = 'B'
             side = 'R'

!            The NAG name equivalent of zggbak is f08wwf
             CALL zggbak(job,side,n,ilo,ihi,lscale,rscale,n,vr,ldvr,info)

!            Normalize the right eigenvectors
             DO i = 1, n
                vr(1:n,i) = vr(1:n,i)/vr(1,i)
             END DO

!            Print the right eigenvectors

             ifail = 0
             CALL x04dbf('General',' ',n,n,vr,ldvr,'Bracketed','F7.4', &
                'Right eigenvectors','Integer',rlabs,'Integer',clabs,80,0, &
                ifail)

             WRITE (nout,*)
             FLUSH (nout)

          END IF

!         Compute left eigenvectors of the original matrix

          IF (iright) THEN
             job = 'B'
             side = 'L'

!            The NAG name equivalent of zggbak is f08wwf
             CALL zggbak(job,side,n,ilo,ihi,lscale,rscale,n,vl,ldvl,info)

!            Normalize the left eigenvectors
             DO i = 1, n
                vl(1:n,i) = vl(1:n,i)/vl(1,i)
             END DO

!            Print the left eigenvectors

             ifail = 0
             CALL x04dbf('General',' ',n,n,vl,ldvl,'Bracketed','F7.4', &
                'Left eigenvectors','Integer',rlabs,'Integer',clabs,80,0, &
                ifail)

          END IF
       ELSE
          WRITE (nout,99994)
       END IF

99999  FORMAT (1X,'Minimal required LWORK = ',I6)
99998  FORMAT (1X,'Actual value of  LWORK = ',I6)
99997  FORMAT (1X,'Generalized eigenvalues')
99996  FORMAT (1X,I4,' Infinite eigenvalue')
99995  FORMAT (1X,I4,5X,A,F7.3,A,F7.3,A)
99994  FORMAT (1X,'Insufficient workspace for array WORK'/' in F08XSF/', &
          'ZHGEQZ')
    END PROGRAM f08yxfe
