    PROGRAM f11jsfe

!      F11JSF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f11jsf, 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, lwork, maxitn, n, nnz
       CHARACTER (6)                   :: method
       CHARACTER (1)                   :: precon
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:), b(:), work(:), x(:)
       REAL (KIND=nag_wp), ALLOCATABLE :: rdiag(:)
       INTEGER, ALLOCATABLE            :: icol(:), irow(:), iwork(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F11JSF Example Program Results'
!      Skip heading in data file
       READ (nin,*)

!      Read algorithmic parameters

       READ (nin,*) n
       READ (nin,*) nnz
       lwork = 7*n + 120
       ALLOCATE (a(nnz),b(n),work(lwork),x(n),rdiag(n),icol(nnz),irow(nnz), &
          iwork(n+1))
       READ (nin,*) method, precon
       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 rhs vector b and initial approximate solution x

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

!      Solve Ax = b using F11JSF

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

       WRITE (nout,99999) 'Converged in', itn, ' iterations'
       WRITE (nout,99998) 'Final residual norm =', rnorm

!      Output x

       WRITE (nout,99997) x(1:n)

99999  FORMAT (1X,A,I10,A)
99998  FORMAT (1X,A,1P,E16.3)
99997  FORMAT (1X,'(',E16.4,',',E16.4,')')
    END PROGRAM f11jsfe
