    PROGRAM f07kdfe

!      F07KDF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dpstrf, nag_wp, x04caf, x04ebf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER   :: zero = 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 ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), work(:)
       INTEGER, ALLOCATABLE            :: piv(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Executable Statements ..
       WRITE (nout,*) 'F07KDF 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 dpstrf is f07kdf
       CALL dpstrf(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 x04caf(uplo,'Nonunit',n,n,a,lda,'Factor',ifail)

!      Print pivot indices
       WRITE (nout,*)
       WRITE (nout,*) 'PIV'
       FLUSH (nout)

       ifail = 0
       CALL x04ebf('General','Non-unit',1,n,piv,1,'I11',' ','No',rlabs,'No', &
          clabs,80,1,ifail)

    END PROGRAM f07kdfe
