    PROGRAM f11dqfe

!      F11DQF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f11dnf, f11dqf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: dtol, rnorm, tol
       INTEGER                         :: i, ifail, itn, la, lfill, liwork,    &
                                          lwork, m, maxitn, n, nnz, nnzc, npivm
       CHARACTER (8)                   :: method
       CHARACTER (1)                   :: milu, pstrat
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:), b(:), work(:), x(:)
       INTEGER, ALLOCATABLE            :: icol(:), idiag(:), ipivp(:),         &
                                          ipivq(:), irow(:), istr(:), iwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,*) 'F11DQF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)

!      Read algorithmic parameters

       READ (nin,*) n, m
       READ (nin,*) nnz
       la = 2*nnz
       liwork = 7*n + 2
       lwork = max(4*n+m*(m+n+5)+121,8*n+120,2*n*(m+3)+m*(m+2)+120,11*n+120)

       ALLOCATE (a(la),b(n),work(lwork),x(n),icol(la),idiag(n),ipivp(n), &
          ipivq(n),irow(la),istr(n+1),iwork(liwork))
       READ (nin,*) method
       READ (nin,*) lfill, dtol
       READ (nin,*) pstrat
       READ (nin,*) milu
       READ (nin,*) tol, maxitn

!      Read the matrix A

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

!      Read rhs vector b and initial approximate solution x

       READ (nin,*) b(1:n)
       READ (nin,*) x(1:n)

!      Calculate incomplete LU factorization

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f11dnf(n,nnz,a,la,irow,icol,lfill,dtol,pstrat,milu,ipivp,ipivq, &
          istr,idiag,nnzc,npivm,iwork,liwork,ifail)

!      Solve Ax = b using F11DQF

       ifail = 0
       CALL f11dqf(method,n,nnz,a,la,irow,icol,ipivp,ipivq,istr,idiag,b,m,tol, &
          maxitn,x,rnorm,itn,work,lwork,ifail)

       WRITE (nout,99999) itn
       WRITE (nout,99998) rnorm
       WRITE (nout,*)

!      Output x

       WRITE (nout,*) '                   X'
       WRITE (nout,99997) x(1:n)

99999  FORMAT (1X,'Converged in',I10,' iterations')
99998  FORMAT (1X,'Final residual norm =',1P,E16.3)
99997  FORMAT (1X,'(',1P,E16.4,',',1P,E16.4,')')
    END PROGRAM f11dqfe
