    PROGRAM f11mgfe

!      F11MGF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f11mdf, f11mef, f11mgf, f11mlf, nag_wp
!      .. 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)              :: anorm, flop, rcond, thresh
       INTEGER                         :: i, ifail, n, nnz, nnzl, nnzu, nzlmx, &
                                          nzlumx, nzumx
       CHARACTER (1)                   :: norm, spec
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:), lval(:), uval(:)
       INTEGER, ALLOCATABLE            :: icolzp(:), il(:), iprm(:),           &
                                          irowix(:), iu(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F11MGF Example Program Results'
!      Skip heading in data file
       READ (nin,*)

!      Read order of matrix

       READ (nin,*) n

       ALLOCATE (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

!      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)

!      Calculate norm

       norm = '1'

       ifail = 0
       CALL f11mlf(norm,anorm,n,icolzp,irowix,a,ifail)

!      Calculate condition number

       ifail = 0
       CALL f11mgf(norm,n,il,lval,iu,uval,anorm,rcond,ifail)

!      Output result

       WRITE (nout,*)
       WRITE (nout,*) 'Norm  ,Condition number'
       WRITE (nout,'(F7.3,A1,F7.3)') anorm, ',', 1.0E0_nag_wp/rcond

    END PROGRAM f11mgfe
