    PROGRAM f08wafe

!      F08WAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dggev, nag_wp, x02amf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nb = 64, nin = 5, nout = 6
!      .. Local Scalars ..
       COMPLEX (KIND=nag_wp)           :: eig
       REAL (KIND=nag_wp)              :: small
       INTEGER                         :: i, info, j, lda, ldb, ldvr, lwork, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), alphai(:), alphar(:),        &
                                          b(:,:), beta(:), vr(:,:), work(:)
       REAL (KIND=nag_wp)              :: dummy(1,1)
!      .. Intrinsic Functions ..
       INTRINSIC                          abs, cmplx, max, nint
!      .. Executable Statements ..
       WRITE (nout,*) 'F08WAF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       lda = n
       ldb = n
       ldvr = n
       ALLOCATE (a(lda,n),alphai(n),alphar(n),b(ldb,n),beta(n),vr(ldvr,n))

!      Use routine workspace query to get optimal workspace.
       lwork = -1
!      The NAG name equivalent of dggev is f08waf
       CALL dggev('No left vectors','Vectors (right)',n,a,lda,b,ldb,alphar, &
          alphai,beta,dummy,1,vr,ldvr,dummy,lwork,info)

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

!      Read in the matrices A and B

       READ (nin,*) (a(i,1:n),i=1,n)
       READ (nin,*) (b(i,1:n),i=1,n)

!      Solve the generalized eigenvalue problem

!      The NAG name equivalent of dggev is f08waf
       CALL dggev('No left vectors','Vectors (right)',n,a,lda,b,ldb,alphar, &
          alphai,beta,dummy,1,vr,ldvr,work,lwork,info)

       IF (info>0) THEN
          WRITE (nout,*)
          WRITE (nout,99999) 'Failure in DGGEV. INFO =', info
       ELSE
          small = x02amf()
          DO j = 1, n
             WRITE (nout,*)
             IF ((abs(alphar(j))+abs(alphai(j)))*small>=abs(beta(j))) THEN
                WRITE (nout,99998) 'Eigenvalue(', j, ')', &
                   ' is numerically infinite or undetermined', 'ALPHAR(', j, &
                   ') = ', alphar(j), ', ALPHAI(', j, ') = ', alphai(j), &
                   ', BETA(', j, ') = ', beta(j)
             ELSE
                IF (alphai(j)==0.0E0_nag_wp) THEN
                   WRITE (nout,99997) 'Eigenvalue(', j, ') = ', &
                      alphar(j)/beta(j)
                ELSE
                   eig = cmplx(alphar(j),alphai(j),kind=nag_wp)/ &
                      cmplx(beta(j),kind=nag_wp)
                   WRITE (nout,99996) 'Eigenvalue(', j, ') = ', eig
                END IF
             END IF
             WRITE (nout,*)
             WRITE (nout,99995) 'Eigenvector(', j, ')'
             IF (alphai(j)==0.0E0_nag_wp) THEN
                WRITE (nout,99994) (vr(i,j),i=1,n)
             ELSE IF (alphai(j)>0.0E0_nag_wp) THEN
                WRITE (nout,99993) (vr(i,j),vr(i,j+1),i=1,n)
             ELSE
                WRITE (nout,99993) (vr(i,j-1),-vr(i,j),i=1,n)
             END IF
          END DO

       END IF

99999  FORMAT (1X,A,I4)
99998  FORMAT (1X,A,I2,2A/1X,2(A,I2,A,1P,E11.4,3X),A,I2,A,1P,E11.4)
99997  FORMAT (1X,A,I2,A,1P,E11.4)
99996  FORMAT (1X,A,I2,A,'(',1P,E11.4,',',1P,E11.4,')')
99995  FORMAT (1X,A,I2,A)
99994  FORMAT (1X,1P,E11.4)
99993  FORMAT (1X,'(',1P,E11.4,',',1P,E11.4,')')
    END PROGRAM f08wafe
