    PROGRAM f08sefe

!      F08SEF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dpotrf, dsterf, dsygst, dsytrd, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, info, lda, ldb, lwork, n
       CHARACTER (1)                   :: uplo
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:,:), d(:), e(:), tau(:),  &
                                          work(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08SEF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       lda = n
       ldb = n
       lwork = 64*n
       ALLOCATE (a(lda,n),b(ldb,n),d(n),e(n-1),tau(n),work(lwork))

!      Read A and B from data file

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

!      Compute the Cholesky factorization of B
!      The NAG name equivalent of dpotrf is f07fdf
       CALL dpotrf(uplo,n,b,ldb,info)

       WRITE (nout,*)
       IF (info>0) THEN
          WRITE (nout,*) 'B is not positive definite.'
       ELSE

!         Reduce the problem to standard form C*y = lambda*y, storing
!         the result in A
!         The NAG name equivalent of dsygst is f08sef
          CALL dsygst(1,uplo,n,a,lda,b,ldb,info)

!         Reduce C to tridiagonal form T = (Q**T)*C*Q
!         The NAG name equivalent of dsytrd is f08fef
          CALL dsytrd(uplo,n,a,lda,d,e,tau,work,lwork,info)

!         Calculate the eigenvalues of T (same as C)
!         The NAG name equivalent of dsterf is f08jff
          CALL dsterf(n,d,e,info)

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

!            Print eigenvalues

             WRITE (nout,*) 'Eigenvalues'
             WRITE (nout,99999) d(1:n)
          END IF
       END IF

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