    PROGRAM f08xsfe

!      F08XSF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp, x04dbf, zgeqrf, zggbal, zgghrd, zhgeqz, &
                               zunmqr
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       COMPLEX (KIND=nag_wp)           :: e
       INTEGER                         :: i, ifail, ihi, ilo, info, irows,     &
                                          jwork, lda, ldb, ldq, ldz, lwork, n
       CHARACTER (1)                   :: compq, compz, job
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), alpha(:), b(:,:),         &
                                             beta(:), q(:,:), tau(:), work(:), &
                                             z(:,:)
       REAL (KIND=nag_wp), ALLOCATABLE :: lscale(:), rscale(:), rwork(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Intrinsic Functions ..
       INTRINSIC                          aimag, nint, real
!      .. Executable Statements ..
       WRITE (nout,*) 'F08XSF Example Program Results'
       FLUSH (nout)

!      Skip heading in data file

       READ (nin,*)
       READ (nin,*) n
       ldq = 1
       ldz = 1
       lda = n
       ldb = n
       lwork = 6*n
       ALLOCATE (a(lda,n),alpha(n),b(ldb,n),beta(n),q(ldq,ldq),tau(n), &
          work(lwork),z(ldz,ldz),lscale(n),rscale(n),rwork(6*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'
       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

!      The NAG name equivalent of zgeqrf is f08asf
       CALL zgeqrf(irows,irows,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,irows,irows,b(ilo,ilo),ldb,tau,a(ilo,ilo), &
          lda,work,lwork,info)

!      Compute the generalized Hessenberg form of (A,B) -> (H,T)
       compq = 'N'
       compz = 'N'

!      The NAG name equivalent of zgghrd is f08wsf
       CALL zgghrd(compq,compz,irows,1,irows,a(ilo,ilo),lda,b(ilo,ilo),ldb,q, &
          ldq,z,ldz,info)

!      Matrix A (H) 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 (T) in generalized Hessenberg form
       ifail = 0
       CALL x04dbf('General',' ',n,n,b,ldb,'Bracketed','F7.3', &
          'Matrix B is triangular','Integer',rlabs,'Integer',clabs,80,0,ifail)

!      Routine ZHGEQZ
!      Workspace query: jwork = -1

       jwork = -1
       job = 'E'
!      The NAG name equivalent of zhgeqz is f08xsf
       CALL zhgeqz(job,compq,compz,n,ilo,ihi,a,lda,b,ldb,alpha,beta,q,ldq,z, &
          ldz,work,jwork,rwork,info)
       WRITE (nout,*)
       WRITE (nout,99999) nint(real(work(1)))
       WRITE (nout,99998) lwork
       WRITE (nout,*)
       WRITE (nout,99997)
       WRITE (nout,99996)
       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,q,ldq, &
             z,ldz,work,lwork,rwork,info)

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

          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
       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,5X,'Infinite eigenvalue')
99995  FORMAT (1X,I4,5X,A,F7.3,A,F7.3,A)
99994  FORMAT (1X,'Insufficient workspace allocated for call to F08XSF/ZHGEQZ' &
          )
    END PROGRAM f08xsfe
