    PROGRAM f07krfe

!      F07KRF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp, x04dbf, x04ebf, zpstrf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       COMPLEX (KIND=nag_wp), PARAMETER :: zero = (0.0E0_nag_wp,0.0E0_nag_wp)
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: tol
       INTEGER                         :: i, ifail, info, j, lda, n, rank
       CHARACTER (1)                   :: uplo
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:)
       REAL (KIND=nag_wp), ALLOCATABLE :: work(:)
       INTEGER, ALLOCATABLE            :: piv(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Executable Statements ..
       WRITE (nout,*) 'F07KRF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n, uplo
       lda = n
       ALLOCATE (a(lda,n),piv(n),work(2*n))

!      Read A from data file
       IF (uplo=='U') THEN
          READ (nin,*) (a(i,i:n),i=1,n)
       ELSE IF (uplo=='L') THEN
          READ (nin,*) (a(i,1:i),i=1,n)
       END IF
       tol = -1.0_nag_wp

!      Factorize A
       info = 0
!      The NAG name equivalent of zpstrf is f07krf
       CALL zpstrf(uplo,n,a,lda,piv,rank,tol,work,info)

!      Zero out columns rank+1 to n
       IF (uplo=='U') THEN
          DO j = rank + 1, n
             a(rank+1:j,j) = zero
          END DO
       ELSE IF (uplo=='L') THEN
          DO j = rank + 1, n
             a(j:n,j) = zero
          END DO
       END IF

!      Print rank
       WRITE (nout,*)
       WRITE (nout,'(1X,A15,I3)') 'Computed rank: ', rank

!      Print factor
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04dbf(uplo,'Nonunit',n,n,a,lda,'Bracketed','F5.2','Factor', &
          'Integer',rlabs,'Integer',clabs,80,0,ifail)

!      Print pivot indices
       WRITE (nout,*)
       WRITE (nout,*) 'PIV'
       FLUSH (nout)
       ifail = 0
       CALL x04ebf('General','Non-unit',1,n,piv,1,'I14',' ','No',rlabs,'No', &
          clabs,80,1,ifail)

    END PROGRAM f07krfe
