    PROGRAM f08fgfe

!      F08FGF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dormtr, dstebz, dstein, dsytrd, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER   :: zero = 0.0E0_nag_wp
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: vl, vu
       INTEGER                         :: i, ifail, info, lda, ldc, lwork, m,  &
                                          n, nsplit
       CHARACTER (1)                   :: uplo
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), c(:,:), d(:), e(:), tau(:),  &
                                          w(:), work(:)
       INTEGER, ALLOCATABLE            :: iblock(:), ifailv(:), isplit(:),     &
                                          iwork(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08FGF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       lda = n
       ldc = n
       lwork = 64*n
       ALLOCATE (a(lda,n),c(ldc,n),d(n),e(n),tau(n),w(n),work(lwork), &
          iblock(n),ifailv(n),isplit(n),iwork(3*n))

!      Read A from data file

       READ (nin,*) uplo
       IF (uplo=='U') THEN
          READ (nin,*) (a(i,i:n),i=1,n)
       ELSE IF (uplo=='L') THEN
          READ (nin,*) (a(i,1:i),i=1,n)
       END IF

!      Reduce A to tridiagonal form T = (Q**T)*A*Q
       CALL dsytrd(uplo,n,a,lda,d,e,tau,work,lwork,info)

!      Calculate the two smallest eigenvalues of T (same as A)

!      The NAG name equivalent of dstebz is f08jjf
       CALL dstebz('I','B',n,vl,vu,1,2,zero,d,e,m,nsplit,w,iblock,isplit,work, &
          iwork,info)

       WRITE (nout,*)
       IF (info>0) THEN
          WRITE (nout,*) 'Failure to converge.'
       ELSE
          WRITE (nout,*) 'Eigenvalues'
          WRITE (nout,99999) w(1:m)

!         Calculate the eigenvectors of T, storing the result in C
!         The NAG name equivalent of dstein is f08jkf
          CALL dstein(n,d,e,m,w,iblock,isplit,c,ldc,work,iwork,ifailv,info)

          IF (info>0) THEN
             WRITE (nout,*) 'Failure to converge.'
          ELSE

!            Calculate the eigenvectors of A = Q * (eigenvectors of T)
!            The NAG name equivalent of dormtr is f08fgf
             CALL dormtr('Left',uplo,'No transpose',n,m,a,lda,tau,c,ldc,work, &
                lwork,info)

!            Print eigenvectors

             WRITE (nout,*)
             FLUSH (nout)

!            Normalize the eigenvectors
             DO i = 1, m
                c(1:n,i) = c(1:n,i)/c(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,c,ldc,'Eigenvectors',ifail)

          END IF
       END IF

99999  FORMAT (3X,(9F8.4))
    END PROGRAM f08fgfe
