    PROGRAM f03bhfe

!      F03BHF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dpbtrf, f03bhf, nag_wp, x04cef
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: d
       INTEGER                         :: i, id, ifail, info, j, kd, kl, ku,   &
                                          ldab, n
       CHARACTER (1)                   :: uplo
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: ab(:,:)
!      .. Intrinsic Functions ..
       INTRINSIC                          index, max, min
!      .. Executable Statements ..
       WRITE (nout,*) 'F03BHF Example Program Results'

!      Skip heading in data file
       READ (nin,*)

       READ (nin,*) uplo

       READ (nin,*) n, kd
       ldab = kd + 1
       ALLOCATE (ab(ldab,n))

       IF (index('Ll',uplo)<=0) THEN
!         Read in upper triangular banded matrix
          ku = kd
          kl = 0
          DO i = 1, n
             READ (nin,*) (ab(kd+1+i-j,j),j=i,min(i+kd,n))
          END DO
       ELSE
!         Read in lower triangular banded matrix
          ku = 0
          kl = kd
          DO i = 1, n
             READ (nin,*) (ab(1+i-j,j),j=max(1,i-kd),i)
          END DO
       END IF
!      Factorize A
!      The NAG name equivalent of dpbtrf is f07hdf
       CALL dpbtrf(uplo,n,kd,ab,ldab,info)

       IF (info==0) THEN
          WRITE (nout,*)
          FLUSH (nout)
          ifail = 0
          CALL x04cef(n,n,kl,ku,ab,ldab,'Array AB after factorization',ifail)

          ifail = 0
          CALL f03bhf(uplo,n,kd,ab,ldab,d,id,ifail)

          WRITE (nout,*)
          WRITE (nout,99999) d, id
          WRITE (nout,*)
          WRITE (nout,99998) d*2.0E0_nag_wp**id
       ELSE
          WRITE (nout,99997) info
       END IF

99999  FORMAT (1X,'D = ',F13.5,' ID = ',I0)
99998  FORMAT (1X,'Value of determinant = ',E13.5)
99997  FORMAT (' ** Factorization routine return error flag info = ',I0,'.')
    END PROGRAM f03bhfe
