    PROGRAM f08ssfe

!      F08SSF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dsterf, nag_wp, zhegst, zhetrd, zpotrf
!      .. 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 ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:,:), tau(:), work(:)
       REAL (KIND=nag_wp), ALLOCATABLE :: d(:), e(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08SSF 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),tau(n),work(lwork),d(n),e(n-1))

!      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 zpotrf is f07frf
       CALL zpotrf(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 zhegst is f08ssf
          CALL zhegst(1,uplo,n,a,lda,b,ldb,info)

!         Reduce C to tridiagonal form T = (Q**H)*C*Q
!         The NAG name equivalent of zhetrd is f08fsf
          CALL zhetrd(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 f08ssfe
