    Program f08frfe

!     F08FRF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: blas_zamax_val, nag_wp, x04daf, zheevr
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: zero = 0.0E+0_nag_wp
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: abstol, r, vl, vu
      Integer                          :: i, ifail, il, info, iu, k, lda, ldz, &
                                          liwork, lrwork, lwork, m, n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), work(:), z(:,:)
      Complex (Kind=nag_wp)            :: dummy(1)
      Real (Kind=nag_wp)               :: rdum(1)
      Real (Kind=nag_wp), Allocatable  :: rwork(:), w(:)
      Integer                          :: idum(1)
      Integer, Allocatable             :: isuppz(:), iwork(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, cmplx, conjg, max, nint, real
!     .. Executable Statements ..
      Write (nout,*) 'F08FRF Example Program Results'
      Write (nout,*)
!     Skip heading in data file and read N and the lower and upper
!     indices of the smallest and largest eigenvalues to be found
      Read (nin,*)
      Read (nin,*) n, il, iu
      lda = n
      ldz = n
      m = n
      Allocate (a(lda,n),z(ldz,m),w(n),isuppz(2*m))

!     Use routine workspace query to get optimal workspace.
      lwork = -1
      liwork = -1
      lrwork = -1
!     The NAG name equivalent of zheevr is f08frf
      Call zheevr('Vectors','I','Upper',n,a,lda,vl,vu,il,iu,abstol,m,w,z,ldz, &
        isuppz,dummy,lwork,rdum,lrwork,idum,liwork,info)

!     Make sure that there is enough workspace for blocksize nb.
      lwork = max((nb+1)*n,nint(real(dummy(1))))
      lrwork = max(24*n,nint(rdum(1)))
      liwork = max(10*n,idum(1))
      Allocate (work(lwork),rwork(lrwork),iwork(liwork))

!     Read the upper triangular part of the matrix A from data file

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

!     Set the absolute error tolerance for eigenvalues.  With ABSTOL
!     set to zero, the default value is used instead

      abstol = zero

!     Solve the symmetric eigenvalue problem

!     The NAG name equivalent of zheevr is f08frf
      Call zheevr('Vectors','I','Upper',n,a,lda,vl,vu,il,iu,abstol,m,w,z,ldz, &
        isuppz,work,lwork,rwork,lrwork,iwork,liwork,info)

      If (info==0) Then

!       Print solution

        Write (nout,*) 'Selected eigenvalues'
        Write (nout,99999) w(1:m)
        Flush (nout)

!       Normalize the eigenvectors so that the element of largest absolute
!       value is real.
        Do i = 1, m
          Call blas_zamax_val(n,z(1,i),1,k,r)
          z(1:n,i) = z(1:n,i)*(conjg(z(k,i))/cmplx(abs(z(k,i)),kind=nag_wp))
        End Do

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call x04daf('General',' ',n,m,z,ldz,'Selected eigenvectors',ifail)
      Else
        Write (nout,99998) 'Failure in ZHEEVR. INFO =', info
      End If

99999 Format (3X,(8F8.4))
99998 Format (1X,A,I5)
    End Program f08frfe
