    PROGRAM f08ngfe

!      F08NGF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dgehrd, dhsein, dhseqr, dormhr, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       COMPLEX (KIND=nag_wp)           :: eig, eig1
       REAL (KIND=nag_wp)              :: thresh
       INTEGER                         :: i, ifail, info, j, k, lda, ldc, ldh, &
                                          ldvl, ldz, lwork, m, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), c(:,:), h(:,:), tau(:),      &
                                          vl(:,:), wi(:), work(:), wr(:), z(:,:)
       INTEGER, ALLOCATABLE            :: ifaill(:), ifailr(:)
       LOGICAL, ALLOCATABLE            :: select(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          aimag, cmplx, real
!      .. Executable Statements ..
       WRITE (nout,*) 'F08NGF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       ldz = 1
       lda = n
       ldc = n
       ldh = n
       ldvl = n
       lwork = 64*n
       ALLOCATE (a(lda,n),c(ldc,n),h(ldh,n),tau(n),vl(ldvl,n),wi(n), &
          work(lwork),wr(n),z(ldz,1),ifaill(n),ifailr(n),select(n))

!      Read A from data file

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

       READ (nin,*) thresh

!      Reduce A to upper Hessenberg form H = (Q**T)*A*Q
!      The NAG name equivalent of dgehrd is f08nef
       CALL dgehrd(n,1,n,a,lda,tau,work,lwork,info)

!      Copy A to H
       h(1:n,1:n) = a(1:n,1:n)

!      Calculate the eigenvalues of H (same as A)
!      The NAG name equivalent of dhseqr is f08pef
       CALL dhseqr('Eigenvalues','No vectors',n,1,n,h,ldh,wr,wi,z,ldz,work, &
          lwork,info)

       WRITE (nout,*)
       IF (info>0) THEN
          WRITE (nout,*) 'Failure to converge.'
       ELSE
          WRITE (nout,*) 'Eigenvalues'
          WRITE (nout,99999) (' (',wr(i),',',wi(i),')',i=1,n)

          DO i = 1, n
             select(i) = wr(i) < thresh
          END DO

!         Calculate the eigenvectors of H (as specified by SELECT),
!         storing the result in C
!         The NAG name equivalent of dhsein is f08pkf
          CALL dhsein('Right','QR','No initial vectors',select,n,a,lda,wr,wi, &
             vl,ldvl,c,ldc,n,m,work,ifaill,ifailr,info)

!         Calculate the eigenvectors of A = Q * (eigenvectors of H)
!         The NAG name equivalent of dormhr is f08ngf
          CALL dormhr('Left','No transpose',n,m,1,n,a,lda,tau,c,ldc,work, &
             lwork,info)

!         Print eigenvectors

          WRITE (nout,*)
          FLUSH (nout)

!         Normalize selected eigenvectors
          j = 0
          k = 1
          DO WHILE (k<=n)
             IF (select(k)) THEN
                j = j + 1
                IF (wi(k)==0.0_nag_wp) THEN
                   DO i = 2, n
                      c(i,j) = c(i,j)/c(1,j)
                   END DO
                   c(1,j) = 1.0_nag_wp
                ELSE
                   eig1 = cmplx(c(1,j),c(1,j+1),kind=nag_wp)
                   c(1,j) = 1.0_nag_wp
                   c(1,j+1) = 0.0_nag_wp
                   DO i = 2, n
                      eig = cmplx(c(i,j),c(i,j+1),kind=nag_wp)
                      eig = eig/eig1
                      c(i,j) = real(eig)
                      c(i,j+1) = aimag(eig)
                   END DO
                   j = j + 1
                   k = k + 1
                END IF
             END IF
             k = k + 1
          END DO

!         ifail: behaviour on error exit
!                =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
          ifail = 0
          CALL x04caf('General',' ',n,m,c,ldc,'Contents of array C',ifail)

       END IF

99999  FORMAT (1X,A,F8.4,A,F8.4,A)
    END PROGRAM f08ngfe
