    PROGRAM f08nafe

!      F08NAF Example Program Text
!      Mark 23 Release. NAG Copyright 2011.

!      .. 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 Functions ..
       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
