    PROGRAM f08ygfe

!      F08YGF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dtgsen, nag_wp, x04caf
!      .. 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 ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), alphai(:), alphar(:),        &
                                          b(:,:), beta(:), q(:,:), work(:),    &
                                          z(:,:)
       REAL (KIND=nag_wp)              :: dif(2)
       INTEGER, ALLOCATABLE            :: iwork(:)
       LOGICAL, ALLOCATABLE            :: select(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08YGF 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 + 6
       lwork = n*(n+4) + 16
       ALLOCATE (a(lda,n),alphai(n),alphar(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 dtgsen is f08ygf
       CALL dtgsen(ijob,wantq,wantz,select,n,a,lda,b,ldb,alphar,alphai,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 x04caf('General',' ',n,n,a,lda,'Reordered Schur matrix A',ifail)

       WRITE (nout,*)
       FLUSH (nout)

       ifail = 0
       CALL x04caf('General',' ',n,n,b,ldb,'Reordered Schur matrix B',ifail)

!      Print deflating subspaces

       WRITE (nout,*)
       FLUSH (nout)

       ifail = 0
       CALL x04caf('General',' ',n,m,q,ldq, &
          'Basis of left deflating invariant subspace',ifail)

       WRITE (nout,*)
       FLUSH (nout)

       ifail = 0
       CALL x04caf('General',' ',n,m,z,ldz, &
          'Basis of right deflating invariant subspace',ifail)

       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 f08ygfe
