    Program f08yufe

!     F08YUF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: nag_wp, x04dbf, ztgsen
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: pl, pr
      Integer                          :: i, ifail, ijob, info, lda, ldb, ldq, &
                                          ldz, liwork, lwork, m, n
      Logical                          :: wantq, wantz
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), alpha(:), b(:,:), beta(:), &
                                            q(:,:), work(:), z(:,:)
      Real (Kind=nag_wp)               :: dif(2)
      Integer, Allocatable             :: iwork(:)
      Logical, Allocatable             :: select(:)
      Character (1)                    :: clabs(1), rlabs(1)
!     .. Executable Statements ..
      Write (nout,*) 'F08YUF Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      ldb = n
      ldq = n
      ldz = n
      liwork = (n*n)/2 + 2
      lwork = n*n
      Allocate (a(lda,n),alpha(n),b(ldb,n),beta(n),q(ldq,n),work(lwork), &
        z(ldz,n),iwork(liwork),select(n))

!     Read A, B, Q, Z and the logical array SELECT from data file

      Read (nin,*)(a(i,1:n),i=1,n)
      Read (nin,*)(b(i,1:n),i=1,n)
      Read (nin,*)(q(i,1:n),i=1,n)
      Read (nin,*)(z(i,1:n),i=1,n)

      Read (nin,*) select(1:n)

!     Set ijob, wantq and wantz
      ijob = 4
      wantq = .True.
      wantz = .True.

!     Reorder the Schur factors A and B and update the matrices
!     Q and Z

!     The NAG name equivalent of ztgsen is f08yuf
      Call ztgsen(ijob,wantq,wantz,select,n,a,lda,b,ldb,alpha,beta,q,ldq,z, &
        ldz,m,pl,pr,dif,work,lwork,iwork,liwork,info)

      If (info/=0) Then
        Write (nout,99999) info
        Write (nout,*)
        Flush (nout)
      End If

!     Print reordered generalized Schur form

!     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', &
        'Reordered Schur matrix A','Integer',rlabs,'Integer',clabs,80,0,ifail)

      Write (nout,*)
      Flush (nout)

      ifail = 0
      Call x04dbf('General',' ',n,n,b,ldb,'Bracketed','F7.4', &
        'Reordered Schur matrix B','Integer',rlabs,'Integer',clabs,80,0,ifail)

!     Print deflating subspaces

      Write (nout,*)
      Flush (nout)

      ifail = 0
      Call x04dbf('General',' ',n,m,q,ldq,'Bracketed','F7.4', &
        'Basis of left deflating invariant subspace','Integer',rlabs, &
        'Integer',clabs,80,0,ifail)

      Write (nout,*)
      Flush (nout)

      ifail = 0
      Call x04dbf('General',' ',n,m,z,ldz,'Bracketed','F7.4', &
        'Basis of right deflating invariant subspace','Integer',rlabs, &
        'Integer',clabs,80,0,ifail)

!     Print norm estimates and F-norm upper bounds

      Write (nout,*)
      Write (nout,99998) 'Norm estimate of projection onto', &
        ' left  eigenspace for selected cluster', 1.0E0_nag_wp/pl
      Write (nout,*)
      Write (nout,99998) 'Norm estimate of projection onto', &
        ' right eigenspace for selected cluster', 1.0E0_nag_wp/pr
      Write (nout,*)
      Write (nout,99998) 'F-norm based upper bound on', ' Difu', dif(1)
      Write (nout,*)
      Write (nout,99998) 'F-norm based upper bound on', ' Difl', dif(2)

99999 Format (' Reordering could not be completed. INFO = ',I3)
99998 Format (1X,2A/1X,1P,E10.2)
    End Program f08yufe
