    PROGRAM f04amfe

!      F04AMF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f04amf, nag_wp, x02ajf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: eps
       INTEGER                         :: i, ifail, ir, lda, ldb, ldqr, ldx,   &
                                          m, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), alpha(:), b(:,:), e(:),      &
                                          qr(:,:), r(:), x(:,:), y(:), z(:)
       INTEGER, ALLOCATABLE            :: ipiv(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F04AMF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) m, n
       ir = 1
       lda = m
       ldb = m
       ldqr = m
       ldx = n
       ALLOCATE (a(lda,n),alpha(n),b(ldb,ir),e(n),qr(ldqr,n),r(m),x(ldx,ir), &
          y(n),z(n),ipiv(n))
       READ (nin,*) (a(i,1:n),b(i,1:ir),i=1,m)
       eps = x02ajf()

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f04amf(a,lda,x,ldx,b,ldb,m,n,ir,eps,qr,ldqr,alpha,e,y,z,r,ipiv, &
          ifail)

       WRITE (nout,*) ' Solution'
       DO i = 1, n
          WRITE (nout,99999) x(i,1:ir)
       END DO

99999  FORMAT (1X,8F9.4)
    END PROGRAM f04amfe
