    PROGRAM f08gsfe

!      F08GSF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp, zhptrd
!      .. 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(:), tau(:)
       REAL (KIND=nag_wp), ALLOCATABLE :: d(:), e(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08GSF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n

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

!      Read A from data file

       READ (nin,*) uplo
       IF (uplo=='U') THEN
          READ (nin,*) ((ap(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)
       END IF

!      Reduce A to tridiagonal form

!      The NAG name equivalent of zhptrd is f08gsf
       CALL zhptrd(uplo,n,ap,d,e,tau,info)

!      Print tridiagonal form

       WRITE (nout,*)
       WRITE (nout,*) 'Diagonal'
       WRITE (nout,99999) d(1:n)
       WRITE (nout,*) 'Off-diagonal'
       WRITE (nout,99999) e(1:n-1)

99999  FORMAT (1X,8F9.4)
    END PROGRAM f08gsfe
