    Program f08wnfe

!     F08WNF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: nag_wp, x02amf, zggev
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: small
      Integer                          :: i, info, j, lda, ldb, ldvr, lwork, n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), alpha(:), b(:,:), beta(:), &
                                            vr(:,:), work(:)
      Complex (Kind=nag_wp)            :: dummy(1,1)
      Real (Kind=nag_wp), Allocatable  :: rwork(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, max, nint, real
!     .. Executable Statements ..
      Write (nout,*) 'F08WNF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      ldb = n
      ldvr = n
      Allocate (a(lda,n),alpha(n),b(ldb,n),beta(n),vr(ldvr,n),rwork(8*n))

!     Use routine workspace query to get optimal workspace.
      lwork = -1
!     The NAG name equivalent of zggev is f08wnf
      Call zggev('No left vectors','Vectors (right)',n,a,lda,b,ldb,alpha,beta, &
        dummy,1,vr,ldvr,dummy,lwork,rwork,info)

!     Make sure that there is enough workspace for blocksize nb.
      lwork = max((nb+1)*n,nint(real(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 zggev is f08wnf
      Call zggev('No left vectors','Vectors (right)',n,a,lda,b,ldb,alpha,beta, &
        dummy,1,vr,ldvr,work,lwork,rwork,info)

!     Normalize the eigenvectors
      Do i = 1, n
        vr(1:n,i) = vr(1:n,i)/vr(1,i)
      End Do

      If (info>0) Then
        Write (nout,*)
        Write (nout,99999) 'Failure in ZGGEV. INFO =', info
      Else
        small = x02amf()
        Do j = 1, n
          Write (nout,*)
          If ((abs(alpha(j)))*small>=abs(beta(j))) Then
            Write (nout,99998) 'Eigenvalue(', j, ')', &
              ' is numerically infinite or undetermined', 'ALPHA(', j, ') = ', &
              alpha(j), ', BETA(', j, ') = ', beta(j)
          Else
            Write (nout,99997) 'Eigenvalue(', j, ') = ', alpha(j)/beta(j)
          End If
          Write (nout,*)
          Write (nout,99996) 'Eigenvector(', j, ')', (vr(i,j),i=1,n)
        End Do

      End If

99999 Format (1X,A,I4)
99998 Format (1X,A,I2,2A/1X,2(A,I2,A,'(',1P,E11.4,',',1P,E11.4,')'))
99997 Format (1X,A,I2,A,'(',1P,E11.4,',',1P,E11.4,')')
99996 Format (1X,A,I2,A/3(1X,'(',1P,E11.4,',',1P,E11.4,')':))
    End Program f08wnfe
