Program f08pnfe

!     F08PNF Example Program Text

!     Mark 26.1 Release. NAG Copyright 2016.

!     .. Use Statements ..
      Use nag_library, Only: f08pnz, nag_wp, x02ajf, x04dbf, zgees, zgemm,     &
                             zlange => f06uaf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Complex (Kind=nag_wp)            :: alpha, beta
      Real (Kind=nag_wp)               :: norm
      Integer                          :: i, ifail, info, lda, ldc, ldd, ldvs, &
                                          lwork, n, sdim
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), c(:,:), d(:,:), vs(:,:),   &
                                          w(:), work(:)
      Complex (Kind=nag_wp)            :: wdum(1)
      Real (Kind=nag_wp), Allocatable  :: rwork(:)
      Logical                          :: dummy(1)
      Character (1)                    :: clabs(1), rlabs(1)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: cmplx, max, nint, real
!     .. Executable Statements ..
      Write (nout,*) 'F08PNF 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),vs(ldvs,n),c(ldc,n),d(ldd,n),w(n),rwork(n))

!     Use routine workspace query to get optimal workspace.
      lwork = -1
!     The NAG name equivalent of zgees is f08pnf
      Call zgees('Vectors (Schur)','No sort',f08pnz,n,a,lda,sdim,w,vs,ldvs,    &
        wdum,lwork,rwork,dummy,info)

!     Make sure that there is enough workspace for block size nb.
      lwork = max((nb+1)*n,nint(real(wdum(1))))
      Allocate (work(lwork))

!     Read in the matrix A
      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 x04dbf('General',' ',n,n,a,lda,'Bracketed','F7.4','Matrix A',       &
        'Integer',rlabs,'Integer',clabs,80,0,ifail)

      Write (nout,*)
      Flush (nout)

!     Find the Schur factorization of A
!     The NAG name equivalent of zgees is f08pnf
      Call zgees('Vectors (Schur)','No sort',f08pnz,n,a,lda,sdim,w,vs,ldvs,    &
        work,lwork,rwork,dummy,info)

      If (info>0) Then
        Write (nout,99999) 'Failure in ZGEES. INFO =', info
      Else

!       Compute A - Z*T*Z^H from the factorization of A and store in matrix D
!       The NAG name equivalent of zgemm is f06zaf
        alpha = cmplx(1,kind=nag_wp)
        beta = cmplx(0,kind=nag_wp)
        Call zgemm('N','N',n,n,n,alpha,vs,ldvs,a,lda,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,vs,ldvs,beta,d,ldd)

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

99999 Format (1X,A,I4)
99998 Format (1X,I4,2X,' (',F7.4,',',F7.4,')',:)
    End Program f08pnfe