    PROGRAM f08qufe

!      F08QUF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp, x02ajf, x04dbf, zgemm, & 
                               zlange => f06uaf, ztrsen
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       COMPLEX (KIND=nag_wp)           :: alpha, beta
       REAL (KIND=nag_wp)              :: norm, s, sep
       INTEGER                         :: i, ifail, info, lda, ldc, ldq, ldt,  &
                                          lwork, m, n
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), c(:,:), q(:,:), t(:,:),   &
                                             w(:), work(:)
       REAL (KIND=nag_wp)              :: rwork(1)
       LOGICAL, ALLOCATABLE            :: select(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Intrinsic Functions ..
       INTRINSIC                          cmplx
!      .. Executable Statements ..
       WRITE (nout,*) 'F08QUF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       ldc = n
       lda = n
       ldq = n
       ldt = n
       lwork = (n*n)/2
       ALLOCATE (a(lda,n),c(ldc,n),q(ldq,n),t(ldt,n),w(n),work(lwork), &
          select(n))

!      Read T, Q and the logical array SELECT from data file

       READ (nin,*) (t(i,1:n),i=1,n)
       READ (nin,*)
       READ (nin,*) (q(i,1:n),i=1,n)
       READ (nin,*)
       READ (nin,*) select(1:n)

!      Compute Q * T * Q**T to find  A
       alpha = cmplx(1,kind=nag_wp)
       beta = cmplx(0,kind=nag_wp)
       CALL zgemm('N','N',n,n,n,alpha,q,ldq,t,ldt,beta,c,ldc)
       CALL zgemm('N','C',n,n,n,alpha,c,ldc,q,ldq,beta,a,lda)

!      Print Matrix A, as computed from Q * T * Q**T
!      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 created from Q*T*Q^T','Integer',rlabs,'Integer',clabs,80, &
          0,ifail)

       WRITE (nout,*)
       FLUSH (nout)

!      Reorder the Schur factor T and update the matrix Q to obtain TT and QT

!      The NAG name equivalent of ztrsen is f08quf
       CALL ztrsen('Both','Vectors',select,n,t,ldt,q,ldq,w,m,s,sep,work,lwork, &
          info)

!      Compute (Q * T * Q^H) - (QT * TT * QT^H) and store in A,
!      i.e. the difference between reconstructed A using Schur and reordered
!           Schur decompositions.
       alpha = cmplx(1,kind=nag_wp)
       beta = cmplx(0,kind=nag_wp)
       CALL zgemm('N','N',n,n,n,alpha,q,ldq,t,ldt,beta,c,ldc)
       alpha = cmplx(-1,kind=nag_wp)
       beta = cmplx(1,kind=nag_wp)
       CALL zgemm('N','C',n,n,n,alpha,c,ldc,q,ldq,beta,a,lda)

!      Find norm of difference matrix and print warning if it is too large
!      f06uaf is the NAG name equivalent of the LAPACK auxiliary zlange
       norm = zlange('O',lda,n,a,lda,rwork)
       IF (norm>x02ajf()**0.8_nag_wp) THEN
          WRITE (nout,*) 'Norm of A - (QT * TT * QT^H) is much greater than 0.'
          WRITE (nout,*) 'Schur factorization has failed.'
       ELSE
!         Print condition estimates
          WRITE (nout,99999) 'Condition number estimate', &
              ' of the selected cluster of eigenvalues = ', 1.0_nag_wp/s
          WRITE (nout,*)
          WRITE (nout,99999) 'Condition number estimate of the specified ', &
              'invariant subspace    = ', 1.0_nag_wp/sep
       END IF

99999  FORMAT (1X,A,A,1P,E10.2)

    END PROGRAM f08qufe
