    Program f11defe

!     F11DEF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. 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 Procedures ..
      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
