    PROGRAM f08usfe

!      F08USF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dsterf, nag_wp, zhbgst, zhbtrd, zpbstf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, info, j, ka, kb, ldab, ldbb, ldx, n
       CHARACTER (1)                   :: uplo
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: ab(:,:), bb(:,:), work(:), x(:,:)
       REAL (KIND=nag_wp), ALLOCATABLE :: d(:), e(:), rwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max, min
!      .. Executable Statements ..
       WRITE (nout,*) 'F08USF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n, ka, kb
       ldab = ka + 1
       ldbb = kb + 1
       ldx = n
       ALLOCATE (ab(ldab,n),bb(ldbb,n),work(n),x(ldx,n),d(n),e(n-1),rwork(n))

!      Read A and B from data file

       READ (nin,*) uplo
       IF (uplo=='U') THEN
          DO i = 1, n
             READ (nin,*) (ab(ka+1+i-j,j),j=i,min(n,i+ka))
          END DO
          DO i = 1, n
             READ (nin,*) (bb(kb+1+i-j,j),j=i,min(n,i+kb))
          END DO
       ELSE IF (uplo=='L') THEN
          DO i = 1, n
             READ (nin,*) (ab(1+i-j,j),j=max(1,i-ka),i)
          END DO
          DO i = 1, n
             READ (nin,*) (bb(1+i-j,j),j=max(1,i-kb),i)
          END DO
       END IF

!      Compute the split Cholesky factorization of B
!      The NAG name equivalent of zpbstf is f08utf
       CALL zpbstf(uplo,n,kb,bb,ldbb,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 zhbgst is f08usf
          CALL zhbgst('N',uplo,n,ka,kb,ab,ldab,bb,ldbb,x,ldx,work,rwork,info)

!         Reduce C to tridiagonal form T = (Q**H)*C*Q
!         The NAG name equivalent of zhbtrd is f08hsf
          CALL zhbtrd('N',uplo,n,ka,ab,ldab,d,e,x,ldx,work,info)

!         Calclate 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,(8F8.4))
    END PROGRAM f08usfe
