    Program f08fdfe

!     F08FDF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: dsyevr, nag_wp, x04caf
!     .. 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, vl, vu
      Integer                          :: i, ifail, il, info, iu, lda, ldz,    &
                                          liwork, lwork, m, n
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), w(:), work(:), z(:,:)
      Real (Kind=nag_wp)               :: dummy(1)
      Integer                          :: idum(1)
      Integer, Allocatable             :: isuppz(:), iwork(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max, nint
!     .. Executable Statements ..
      Write (nout,*) 'F08FDF 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),w(n),z(ldz,m),isuppz(2*m))

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

!     Make sure that there is enough workspace for blocksize nb.
      lwork = max((nb+6)*n,nint(dummy(1)))
      liwork = max(10*n,idum(1))
      Allocate (work(lwork),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 dsyevr is f08fdf
      Call dsyevr('Vectors','I','Upper',n,a,lda,vl,vu,il,iu,abstol,m,w,z,ldz, &
        isuppz,work,lwork,iwork,liwork,info)

      If (info==0) Then

!       Print solution

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

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call x04caf('General',' ',n,m,z,ldz,'Selected eigenvectors',ifail)

      Else
        Write (nout,99998) 'Failure in DSYEVR. INFO =', info
      End If

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