    PROGRAM f08xnfe

!      F08XNF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f08xnz, nag_wp, x04dbf, zgges
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nb = 64, nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, info, lda, ldb, ldvsl,     &
                                          ldvsr, lwork, n, sdim
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), alpha(:), b(:,:),         &
                                             beta(:), vsl(:,:), vsr(:,:),      &
                                             work(:)
       COMPLEX (KIND=nag_wp)           :: dummy(1)
       REAL (KIND=nag_wp), ALLOCATABLE :: rwork(:)
       LOGICAL, ALLOCATABLE            :: bwork(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Intrinsic Functions ..
       INTRINSIC                          max, nint, real
!      .. Executable Statements ..
       WRITE (nout,*) 'F08XNF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       lda = n
       ldb = n
       ldvsl = n
       ldvsr = n
       ALLOCATE (a(lda,n),alpha(n),b(ldb,n),beta(n),vsl(ldvsl,n),vsr(ldvsr,n), &
          rwork(8*n),bwork(n))

!      Use routine workspace query to get optimal workspace.
       lwork = -1
!      The NAG name equivalent of zgges is f08xnf
       CALL zgges('Vectors (left)','Vectors (right)','No sort',f08xnz,n,a,lda, &
          b,ldb,sdim,alpha,beta,vsl,ldvsl,vsr,ldvsr,dummy,lwork,rwork,bwork, &
          info)

!      Make sure that there is enough workspace for blocksize nb.
       lwork = max((nb+1)*n,nint(real(dummy(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)

!      Find the generalized Schur form

!      The NAG name equivalent of zgges is f08xnf
       CALL zgges('Vectors (left)','Vectors (right)','No sort',f08xnz,n,a,lda, &
          b,ldb,sdim,alpha,beta,vsl,ldvsl,vsr,ldvsr,work,lwork,rwork,bwork, &
          info)

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

!         Print out the factors of the generalized Schur factorization

!         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.3', &
             'Generalized Schur matrix S','Integer',rlabs,'Integer',clabs,80, &
             0,ifail)

          WRITE (nout,*)
          FLUSH (nout)

          CALL x04dbf('General',' ',n,n,b,ldb,'Bracketed','F7.3', &
             'Generalized Schur matrix T','Integer',rlabs,'Integer',clabs,80, &
             0,ifail)

          WRITE (nout,*)
          FLUSH (nout)

          CALL x04dbf('General',' ',n,n,vsl,ldvsl,'Bracketed','F7.4', &
             'Matrix of left generalized Schur vectors','Integer',rlabs, &
             'Integer',clabs,80,0,ifail)

          WRITE (nout,*)
          FLUSH (nout)

          CALL x04dbf('General',' ',n,n,vsr,ldvsr,'Bracketed','F7.4', &
             'Matrix of right generalized Schur vectors','Integer',rlabs, &
             'Integer',clabs,80,0,ifail)

       END IF

99999  FORMAT (1X,A,I4)
    END PROGRAM f08xnfe
