    PROGRAM f11mhfe

!      F11MHF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f11mdf, f11mef, f11mff, f11mhf, nag_wp, x04caf, &
                               x04cbf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER   :: one = 1.E0_nag_wp
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: flop, thresh
       INTEGER                         :: i, ifail, j, ldb, ldx, n, nnz, nnzl, &
                                          nnzu, nrhs, nzlmx, nzlumx, nzumx
       CHARACTER (1)                   :: spec, trans
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:), b(:,:), berr(:), ferr(:),      &
                                          lval(:), uval(:), x(:,:)
       INTEGER, ALLOCATABLE            :: icolzp(:), il(:), iprm(:),           &
                                          irowix(:), iu(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Executable Statements ..
       WRITE (nout,*) 'F11MHF Example Program Results'
       FLUSH (nout)
!      Skip heading in data file
       READ (nin,*)

!      Read order of matrix and number of right hand sides

       READ (nin,*) n, nrhs
       ldb = n
       ldx = n

       ALLOCATE (b(ldb,nrhs),berr(nrhs),ferr(nrhs),x(ldx,nrhs),icolzp(n+1), &
          iprm(7*n))

!      Read the matrix A

       READ (nin,*) icolzp(1:n+1)
       nnz = icolzp(n+1) - 1

       ALLOCATE (a(nnz),lval(8*nnz),uval(8*nnz),il(7*n+8*nnz+4),irowix(nnz), &
          iu(2*n+8*nnz+1))

       DO i = 1, nnz
          READ (nin,*) a(i), irowix(i)
       END DO

!      Read the right hand sides

       DO j = 1, nrhs
          READ (nin,*) x(1:n,j)
          b(1:n,j) = x(1:n,j)
       END DO

!      Calculate COLAMD permutation

       spec = 'M'

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f11mdf(spec,n,icolzp,irowix,iprm,ifail)

!      Factorise

       thresh = one
       ifail = 0
       nzlmx = 8*nnz
       nzlumx = 8*nnz
       nzumx = 8*nnz

       CALL f11mef(n,irowix,a,iprm,thresh,nzlmx,nzlumx,nzumx,il,lval,iu,uval, &
          nnzl,nnzu,flop,ifail)

!      Compute solution in array X

       trans = 'N'

       ifail = 0
       CALL f11mff(trans,n,iprm,il,lval,iu,uval,nrhs,x,ldx,ifail)

!      Improve solution, and compute backward errors and estimated
!      bounds on the forward errors

       CALL f11mhf(trans,n,icolzp,irowix,a,iprm,il,lval,iu,uval,nrhs,b,ldb,x, &
          ldx,ferr,berr,ifail)

!      Print solution

       WRITE (nout,*)
       FLUSH (nout)

       CALL x04caf('G',' ',n,nrhs,x,ldx,'Solutions',ifail)
       CALL x04cbf('G','X',nrhs,1,ferr,nrhs,'1PE8.1', &
          'Estimated Forward Error','N',rlabs,'N',clabs,80,0,ifail)
       CALL x04cbf('G','X',nrhs,1,berr,nrhs,'1PE8.1','Backward Error','N', &
          rlabs,'N',clabs,80,0,ifail)

    END PROGRAM f11mhfe
