    PROGRAM f11dsfe

!      F11DSF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f11dsf, 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 ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:), b(:), work(:), x(:)
       INTEGER, ALLOCATABLE            :: icol(:), irow(:), iwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,*) 'F11DSF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)

!      Read algorithmic parameters

       READ (nin,*) n
       READ (nin,*) nnz
       READ (nin,*) method, precon
       READ (nin,*) omega
       READ (nin,*) m, tol, maxitn
       l = n
       IF (precon=='N' .OR. precon=='n') l = 0
       lwork = max(4*n+m*(m+n+5)+l+121,8*n+l+120,2*n*(m+3)+m*(m+2)+l+120, &
          11*n+l+120)

       ALLOCATE (a(nnz),b(n),work(lwork),x(n),icol(nnz),irow(nnz), &
          iwork(2*n+1))

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

!      Solve Ax = b using F11DSF

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f11dsf(method,precon,n,nnz,a,irow,icol,omega,b,m,tol,maxitn,x, &
          rnorm,itn,work,lwork,iwork,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 f11dsfe
