!   F08PPF Example Program Text
!   Mark 24 Release. NAG Copyright 2012.

    Module f08ppfe_mod

!     F08PPF Example Program Module:
!            Parameters and User-defined Routines

!     .. Use Statements ..
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter                   :: nb = 64, nin = 5, nout = 6
    Contains
      Function select(w)

!       Logical function select for use with ZGEESX (F08PPF)

!       Returns the value .TRUE. if the real part of the eigenvalue
!       w is positive.

!       .. Function Return Value ..
        Logical                              :: select
!       .. Scalar Arguments ..
        Complex (Kind=nag_wp), Intent (In)   :: w
!       .. Local Scalars ..
        Logical                              :: d
!       .. Intrinsic Procedures ..
        Intrinsic                            :: real
!       .. Executable Statements ..
        If (real(w)>0.0_nag_wp) Then
          d = .True.
        Else
          d = .False.
        End If

        select = d

        Return
      End Function select
    End Module f08ppfe_mod
    Program f08ppfe

!     F08PPF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: f06uaf, nag_wp, x02ajf, zgeesx
      Use f08ppfe_mod, Only: nb, nin, nout, select
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)                   :: anorm, eps, rconde, rcondv, tol
      Integer                              :: i, info, lda, ldvs, lwork, n, sdim
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable   :: a(:,:), vs(:,:), w(:), work(:)
      Complex (Kind=nag_wp)                :: dummy(1)
      Real (Kind=nag_wp), Allocatable      :: rwork(:)
      Logical, Allocatable                 :: bwork(:)
!     .. Intrinsic Procedures ..
      Intrinsic                            :: max, nint, real
!     .. Executable Statements ..
      Write (nout,*) 'F08PPF Example Program Results'
      Write (nout,*)
      Flush (nout)

!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      ldvs = n
      Allocate (a(lda,n),vs(ldvs,n),w(n),rwork(n),bwork(n))

!     Use routine workspace query to get optimal workspace.
      lwork = -1
!     The NAG name equivalent of zgeesx is f08ppf
      Call zgeesx('Vectors (Schur)','Sort',select, &
        'Both reciprocal condition numbers',n,a,lda,sdim,w,vs,ldvs,rconde, &
        rcondv,dummy,lwork,rwork,bwork,info)

!     Make sure that there is enough workspace for blocksize nb.
      lwork = max(n*(nb+1+n/2),nint(real(dummy(1))))
      Allocate (work(lwork))

!     Read in the matrix A
      Read (nin,*)(a(i,1:n),i=1,n)

!     Find the Frobenius norms of A
      anorm = f06uaf('Frobenius',n,n,a,lda,rwork)

!     Find the Schur factorization of A
!     The NAG name equivalent of zgeesx is f08ppf
      Call zgeesx('Vectors (Schur)','Sort',select, &
        'Both reciprocal condition numbers',n,a,lda,sdim,w,vs,ldvs,rconde, &
        rcondv,work,lwork,rwork,bwork,info)

      If (info==0 .Or. info==(n+2)) Then

        Write (nout,*)
        Write (nout,99999) 'Number of eigenvalues for which SELECT is true = ' &
          , sdim, '(dimension of invariant subspace)'

        Write (nout,*)
!       Print eigenvalues.  
        Write (nout,*) 'Selected eigenvalues'
        Write (nout,99998)(i,w(i),i=1,sdim)
        Write (nout,*)

        If (info==(n+2)) Then
          Write (nout,99998) '***Note that rounding errors mean ', &
            'that leading eigenvalues in the Schur form', &
            'no longer satisfy SELECT = .TRUE.'
          Write (nout,*)
        End If

        Flush (nout)

!       Print out the reciprocal condition numbers
        Write (nout,99997) 'Reciprocal of projection norm onto the invariant', &
          'subspace for the selected eigenvalues', 'RCONDE = ', rconde
        Write (nout,*)
        Write (nout,99996) &
          'Reciprocal condition number for the invariant subspace', &
          'RCONDV = ', rcondv

!       Compute the machine precision
        eps = x02ajf()
        tol = eps*anorm

!       Print out the approximate asymptotic error bound on the
!       average absolute error of the selected eigenvalues given by
!       eps*norm(A)/RCONDE
        Write (nout,*)
        Write (nout,99995) 'Approximate asymptotic error bound for selected ', &
          'eigenvalues   = ', tol/rconde

!       Print out an approximate asymptotic bound on the maximum
!       angular error in the computed invariant subspace given by
!       eps*norm(A)/RCONDV
        Write (nout,99995) &
          'Approximate asymptotic error bound for the invariant ', &
          'subspace = ', tol/rcondv
      Else
        Write (nout,99994) 'Failure in ZGEESX. INFO =', info
      End If

99999 Format (1X,A,I4/1X,A)
99998 Format (1X,I4,2X,' (',F7.4,',',F7.4,')':)
99997 Format (1X,A/1X,A/1X,A,1P,E8.1)
99996 Format (1X,A/1X,A,1P,E8.1)
99995 Format (1X,2A,1P,E8.1)
99994 Format (1X,A,I4)
    End Program f08ppfe
