    PROGRAM f08jlfe

!      F08JLF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dstegr, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER   :: vl = 0.0E0_nag_wp
       REAL (KIND=nag_wp), PARAMETER   :: vu = 0.0E0_nag_wp
       INTEGER, PARAMETER              :: il = 0, iu = 0, nin = 5, nout = 6
       CHARACTER (1), PARAMETER        :: range = 'A'
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: abstol
       INTEGER                         :: i, ifail, info, ldz, liwork, lwork,  &
                                          m, n
       CHARACTER (1)                   :: jobz
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: d(:), e(:), w(:), work(:), z(:,:)
       INTEGER, ALLOCATABLE            :: isuppz(:), iwork(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08JLF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       ldz = n
       liwork = 10*n
       lwork = 18*n
       ALLOCATE (d(n),e(n),w(n),work(lwork),z(ldz,n),isuppz(2*n), &
          iwork(liwork))

!      Read the symmetric tridiagonal matrix T from data file, first
!      the diagonal elements, then the off diagonal elements and then
!      JOBV ('N' - eigenvalues only, 'V' - vectors as well)

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

!      Calculate all the eigenvalues of T. Set ABSTOL to zero so that
!      the default value is used.

       abstol = 0.0E0_nag_wp
!      The NAG name equivalent of dstegr is f08jlf
       CALL dstegr(jobz,range,n,d,e,vl,vu,il,iu,abstol,m,w,z,ldz,isuppz,work, &
          lwork,iwork,liwork,info)

       IF (info==0) THEN

!         Print eigenvalues and eigenvectors

          WRITE (nout,*) 'Eigenvalues'
          WRITE (nout,99999) w(1:m)

          WRITE (nout,*)
          FLUSH (nout)

!         Standardize the eigenvectors so that first elements are non-negative.
          DO i = 1, m
            IF (z(1,i)<0.0_nag_wp)  z(1:n,i) = -z(1:n,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,'Eigenvectors',ifail)

       ELSE
          WRITE (nout,99998) 'Failure to compute an eigenvalue, INFO = ', info
       END IF

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