    PROGRAM f03bafe

!      F03BAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dgetrf, f03baf, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: d
       INTEGER                         :: i, id, ifail, info, lda, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:)
       INTEGER, ALLOCATABLE            :: ipiv(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F03BAF Example Program Results'

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

       READ (nin,*) n
       lda = n
       ALLOCATE (a(lda,n),ipiv(n))

       READ (nin,*) (a(i,1:n),i=1,n)

!      LU Factorize A
!      The NAG name equivalent of dgetrf is f07adf
       CALL dgetrf(n,n,a,lda,ipiv,info)

       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04caf('G','N',n,n,a,lda,'Array A after factorization',ifail)

       WRITE (nout,*)
       WRITE (nout,*) 'Pivots'
       WRITE (nout,99999) ipiv(1:n)
       WRITE (nout,*)
       FLUSH (nout)

       ifail = 0
       CALL f03baf(n,a,lda,ipiv,d,id,ifail)

       WRITE (nout,99998) d, id
       WRITE (nout,*)
       WRITE (nout,99997) d*2.0_nag_wp**id

99999  FORMAT (1X,8(1X,I13))
99998  FORMAT (1X,'D = ',F13.5,' ID = ',I12)
99997  FORMAT (1X,'Value of determinant = ',E13.5)
    END PROGRAM f03bafe
