    Program f08nafe

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

!     .. Use Statements ..
      Use nag_library, Only: dgeev, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Complex (Kind=nag_wp)            :: eig
      Integer                          :: i, info, j, lda, ldvr, lwork, n
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), vr(:,:), wi(:), work(:), wr(:)
      Real (Kind=nag_wp)               :: dummy(1,1)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: cmplx, max, nint
!     .. Executable Statements ..
      Write (nout,*) 'F08NAF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      ldvr = n
      Allocate (a(lda,n),vr(ldvr,n),wi(n),wr(n))

!     Use routine workspace query to get optimal workspace.
      lwork = -1
!     The NAG name equivalent of dgeev is f08naf
      Call dgeev('No left vectors','Vectors (right)',n,a,lda,wr,wi,dummy,1,vr, &
        ldvr,dummy,lwork,info)

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

!     Read the matrix A from data file

      Read (nin,*)(a(i,1:n),i=1,n)

!     Compute the eigenvalues and right eigenvectors of A

!     The NAG name equivalent of dgeev is f08naf
      Call dgeev('No left vectors','Vectors (right)',n,a,lda,wr,wi,dummy,1,vr, &
        ldvr,work,lwork,info)

      If (info==0) Then

!       Print solution

        Do j = 1, n
          Write (nout,*)
          If (wi(j)==0.0E0_nag_wp) Then
            Write (nout,99999) 'Eigenvalue(', j, ') = ', wr(j)
          Else
            eig = cmplx(wr(j),wi(j),kind=nag_wp)
            Write (nout,99998) 'Eigenvalue(', j, ') = ', eig
          End If
          Write (nout,*)
          Write (nout,99997) 'Eigenvector(', j, ')'
          If (wi(j)==0.0E0_nag_wp) Then
            Write (nout,99996) vr(1:n,j)
          Else If (wi(j)>0.0E0_nag_wp) Then
            Write (nout,99995)(vr(i,j),vr(i,j+1),i=1,n)
          Else
            Write (nout,99995)(vr(i,j-1),-vr(i,j),i=1,n)
          End If
        End Do
      Else
        Write (nout,*)
        Write (nout,99994) 'Failure in DGEEV.  INFO = ', info
      End If

99999 Format (1X,A,I2,A,1P,E11.4)
99998 Format (1X,A,I2,A,'(',1P,E11.4,',',1P,E11.4,')')
99997 Format (1X,A,I2,A)
99996 Format (1X,1P,E11.4)
99995 Format (1X,'(',1P,E11.4,',',1P,E11.4,')')
99994 Format (1X,A,I4)
    End Program f08nafe
