    PROGRAM f08jbfe

!      F08JBF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dstevx, nag_wp, x02amf, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: abstol, vl, vu
       INTEGER                         :: ifail, il, info, iu, ldz, m, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: d(:), e(:), w(:), work(:), z(:,:)
       INTEGER, ALLOCATABLE            :: iwork(:), jfail(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08JBF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       ldz = n
       m = n
       ALLOCATE (d(n),e(n),w(n),work(5*n),z(ldz,m),iwork(5*n),jfail(n))

!      Read the lower and upper bounds of the interval to be searched,
!      and read the diagonal and off-diagonal elements of the matrix
!      A from data file

       READ (nin,*) vl, vu
       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 would be used instead

       abstol = 2.0E0_nag_wp*x02amf()

!      Solve the symmetric eigenvalue problem
!      The NAG name equivalent of dstevx is f08jbf
       CALL dstevx('Vectors','Values in range',n,d,e,vl,vu,il,iu,abstol,m,w,z, &
          ldz,work,iwork,jfail,info)

       IF (info>=0) THEN

!         Print solution

          WRITE (nout,99999) 'Number of eigenvalues found =', m
          WRITE (nout,*)
          WRITE (nout,*) 'Eigenvalues'
          WRITE (nout,99998) 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)

          IF (info>0) THEN
             WRITE (nout,99999) 'INFO eigenvectors failed to converge, INFO =' &
                , info
             WRITE (nout,*) 'Indices of eigenvectors that did not converge'
             WRITE (nout,99997) jfail(1:m)
          END IF
       ELSE
          WRITE (nout,99999) 'Failure in DSTEVX. INFO =', info
       END IF

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