    PROGRAM f08fdfe

!      F08FDF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. 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 Functions ..
       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)

!         Normalize the eigenvectors
          DO i = 1, m
             z(1:n,i) = z(1:n,i)/z(1,i)
          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,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
