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

    Module f08pafe_mod

!     F08PAF 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(wr,wi)

!       Logical function select for use with DGEES (F08PAF)
!       Returns the value .TRUE. if the eigenvalue is real

!       .. Function Return Value ..
        Logical                              :: select
!       .. Scalar Arguments ..
        Real (Kind=nag_wp), Intent (In)      :: wi, wr
!       .. Local Scalars ..
        Logical                              :: d
!       .. Executable Statements ..
        If (wi==0.0_nag_wp) Then
          d = .True.
        Else
          d = .False.
        End If

        select = d

        Return
      End Function select
    End Module f08pafe_mod
    Program f08pafe

!     F08PAF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: dgees, dgemm, dlange => f06raf, nag_wp, x02ajf,   &
                             x04caf
      Use f08pafe_mod, Only: nb, nin, nout, select
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)                   :: alpha, beta, norm
      Integer                              :: i, ifail, info, lda, ldc, ldd,   &
                                              ldvs, lwork, n, sdim
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable      :: a(:,:), c(:,:), d(:,:), vs(:,:), &
                                              wi(:), work(:), wr(:)
      Real (Kind=nag_wp)                   :: dummy(1), rwork(1)
      Logical, Allocatable                 :: bwork(:)
!     .. Intrinsic Procedures ..
      Intrinsic                            :: max, nint
!     .. Executable Statements ..
      Write (nout,*) 'F08PAF Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      ldc = n
      ldd = n
      ldvs = n
      Allocate (a(lda,n),c(ldc,n),d(ldd,n),vs(ldvs,n),wi(n),wr(n),bwork(n))

!     Use routine workspace query to get optimal workspace.
      lwork = -1
!     The NAG name equivalent of dgees is f08paf
      Call dgees('Vectors (Schur)','Sort',select,n,a,lda,sdim,wr,wi,vs,ldvs, &
        dummy,lwork,bwork,info)

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

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

!     Copy A into D
      d(1:n,1:n) = a(1:n,1:n)

!     Print Matrix A
!     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,'Matrix A',ifail)

      Write (nout,*)
      Flush (nout)

!     Find the Schur factorization
!     The NAG name equivalent of dgees is f08paf
      Call dgees('Vectors (Schur)','Sort',select,n,a,lda,sdim,wr,wi,vs,ldvs, &
        work,lwork,bwork,info)

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

!       Compute A - Z*T*Z^H from the factorization of A and store in matrix D
!       The NAG name equivelent of dgemm is f06yaf
        alpha = 1.0_nag_wp
        beta = 0.0_nag_wp
        Call dgemm('N','N',n,n,n,alpha,vs,ldvs,a,lda,beta,c,ldc)
        alpha = -1.0_nag_wp
        beta = 1.0_nag_wp
        Call dgemm('N','T',n,n,n,alpha,c,ldc,vs,ldvs,beta,d,ldd)

!       Find norm of matrix D and print warning if it is too large
!       f06raf is the NAG name equivalent of the LAPACK auxiliary dlange
        norm = dlange('O',ldd,n,d,ldd,rwork)
        If (norm>x02ajf()**0.8_nag_wp) Then
          Write (nout,*) 'Norm of A-(Z*T*Z^T) is much greater than 0.'
          Write (nout,*) 'Schur factorization has failed.'
        Else
!         Print eigenvalues.
          Write (nout,*) 'Eigenvalues'
          Write (nout,99998)(' (',wr(i),',',wi(i),')',i=1,n)
        End If

      Else
        Write (nout,99999) 'Failure in DGEES.  INFO = ', info
      End If

99999 Format (1X,A,I4)
99998 Format (1X,A,F8.4,A,F8.4,A)

    End Program f08pafe
