    PROGRAM f11defe

!      F11DEF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f11def, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: omega, rnorm, tol
       INTEGER                         :: i, ifail, itn, l, lwork, m, maxitn,  &
                                          n, nnz
       CHARACTER (8)                   :: method
       CHARACTER (1)                   :: precon
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:), b(:), work(:), x(:)
       INTEGER, ALLOCATABLE            :: icol(:), irow(:), iwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,*) 'F11DEF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)

!      Read algorithmic parameters
       READ (nin,*) n, m
       READ (nin,*) nnz
       READ (nin,*) method, precon
       l = n
       IF (precon=='N' .OR. precon=='n') l = 0
       lwork = max(4*n+m*(m+n+5)+l+101,8*n+l+100,2*n*(m+3)+m*(m+2)+l+100, &
          11*n+l+100)

       ALLOCATE (a(nnz),b(n),work(lwork),x(n),icol(nnz),irow(nnz), &
          iwork(2*n+1))
       READ (nin,*) omega
       READ (nin,*) tol, maxitn

!      Read the matrix A

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

!      Read right-hand side vector b and initial approximate solution x

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

!      Solve Ax = b using F11DEF

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f11def(method,precon,n,nnz,a,irow,icol,omega,b,m,tol,maxitn,x, &
          rnorm,itn,work,lwork,iwork,ifail)

       WRITE (nout,'(A,I10,A)') ' Converged in', itn, ' iterations'
       WRITE (nout,'(A,1P,E16.3)') ' Final residual norm =', rnorm
       WRITE (nout,*)

!      Output x

       WRITE (nout,*) '           X'
       WRITE (nout,'(1X,1P,E16.4)') x(1:n)

    END PROGRAM f11defe
