    PROGRAM f08jdfe

!      F08JDF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dstevr, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER   :: zero = 0.0_nag_wp
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: abstol, vl, vu
       INTEGER                         :: ifail, il, info, iu, ldz, liwork,    &
                                          lwork, m, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: d(:), e(:), w(:), work(:), z(:,:)
       REAL (KIND=nag_wp)              :: rdum(1)
       INTEGER                         :: idum(1)
       INTEGER, ALLOCATABLE            :: isuppz(:), iwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max, nint
!      .. Executable Statements ..
       WRITE (nout,*) 'F08JDF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file and read N and the lower and upper
!      indices of the eigenvalues to be found
       READ (nin,*)
       READ (nin,*) n, il, iu
       ldz = n
       m = n
       ALLOCATE (d(n),e(n-1),w(n),z(ldz,m),isuppz(2*n))

!      Use routine workspace query to get optimal workspace.
       lwork = -1
       liwork = -1
!      The NAG name equivalent of dstevr is f08jdf
       CALL dstevr('Vectors','Indices',n,d,e,vl,vu,il,iu,abstol,m,w,z,ldz, &
          isuppz,rdum,lwork,idum,liwork,info)

!      Make sure that there is enough workspace for blocksize nb.
       lwork = max(20*n,nint(rdum(1)))
       liwork = max(10*n,idum(1))
       ALLOCATE (work(lwork),iwork(liwork))

!      Read the diagonal and off-diagonal elements of the matrix A
!      from data file

       READ (nin,*) d(1:n)
       READ (nin,*) e(1:n-1)

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

       abstol = zero

!      Solve the symmetric tridiagonal eigenvalue problem
!      The NAG name equivalent of dstevr is f08jdf
       CALL dstevr('Vectors','Indices',n,d,e,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 DSTEVR. INFO =', info
       END IF

99999  FORMAT (3X,(8F8.4))
99998  FORMAT (1X,A,I5)
    END PROGRAM f08jdfe
