    PROGRAM f08tsfe

!      F08TSF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dsterf, nag_wp, zhpgst, zhptrd, zpptrf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, info, j, n
       CHARACTER (1)                   :: uplo
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: ap(:), bp(:), tau(:)
       REAL (KIND=nag_wp), ALLOCATABLE :: d(:), e(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08TSF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n

       ALLOCATE (ap(n*(n+1)/2),bp(n*(n+1)/2),tau(n),d(n),e(n-1))

!      Read A and B from data file

       READ (nin,*) uplo
       IF (uplo=='U') THEN
          READ (nin,*) ((ap(i+j*(j-1)/2),j=i,n),i=1,n)
          READ (nin,*) ((bp(i+j*(j-1)/2),j=i,n),i=1,n)
       ELSE IF (uplo=='L') THEN
          READ (nin,*) ((ap(i+(2*n-j)*(j-1)/2),j=1,i),i=1,n)
          READ (nin,*) ((bp(i+(2*n-j)*(j-1)/2),j=1,i),i=1,n)
       END IF

!      Compute the Cholesky factorization of B
!      The NAG name equivalent of zpptrf is f07grf
       CALL zpptrf(uplo,n,bp,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 zhpgst is f08tsf
          CALL zhpgst(1,uplo,n,ap,bp,info)

!         Reduce C to tridiagonal form T = (Q**H)*C*Q
!         The NAG name equivalent of zhptrd is f08gsf
          CALL zhptrd(uplo,n,ap,d,e,tau,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 f08tsfe
