*     F11DKF Example Program Text
*     Mark 20 Release. NAG Copyright 2001.
*     .. Parameters ..
      INTEGER          NIN, NOUT
      PARAMETER        (NIN=5,NOUT=6)
      INTEGER          NMAX, LA, LWORK
      PARAMETER        (NMAX=1000,LA=10000,LWORK=10*NMAX)
*     .. Local Scalars ..
      DOUBLE PRECISION ANORM, SIGMAX, STPLHS, STPRHS, TOL
      INTEGER          I, IFAIL, IFAIL1, IFAILX, IREVCM, ITERM, ITN,
     +                 LWREQ, M, MAXITN, MONIT, N, NITER, NNZ
      LOGICAL          LOOP
      CHARACTER        INIT, NORM, PRECON, WEIGHT
      CHARACTER*8      METHOD
*     .. Local Arrays ..
      DOUBLE PRECISION A(LA), B(NMAX), DIAG(NMAX), WGT(NMAX),
     +                 WORK(LWORK), X(NMAX)
      INTEGER          ICOL(LA), IROW(LA)
*     .. External Subroutines ..
      EXTERNAL         F11BDF, F11BEF, F11BFF, F11DKF, F11XAF
*     .. Executable Statements ..
      WRITE (NOUT,*) 'F11DKF Example Program Results'
*
*     Skip heading in data file
*
      READ (NIN,*)
      READ (NIN,*) N
      IF (N.LE.NMAX) THEN
*
*        Read or initialize the parameters for the iterative solver
*
         READ (NIN,*) METHOD
         READ (NIN,*) PRECON, NORM, WEIGHT, ITERM
         READ (NIN,*) M, TOL, MAXITN
         READ (NIN,*) MONIT
         ANORM = 0.0D0
         SIGMAX = 0.0D0
*
*        Read the parameters for the preconditioner
*
         READ (NIN,*) NITER
*
*        Read the number of non-zero elements of the matrix A, then read
*        the non-zero elements
*
         READ (NIN,*) NNZ
         DO 20 I = 1, NNZ
            READ (NIN,*) A(I), IROW(I), ICOL(I)
   20    CONTINUE
*
*        Read right-hand side vector b and initial approximate solution
*
         READ (NIN,*) (B(I),I=1,N)
         READ (NIN,*) (X(I),I=1,N)
*
*        Call F11BDF to initialize the solver
*
         IFAIL = 0
         CALL F11BDF(METHOD,PRECON,NORM,WEIGHT,ITERM,N,M,TOL,MAXITN,
     +               ANORM,SIGMAX,MONIT,LWREQ,WORK,LWORK,IFAIL)
*
*        Call repeatedly F11BEF to solve the equations
*        Note that the arrays B and X are overwritten
*
*        On final exit, X will contain the solution and B the residual
*        vector
*
         IFAIL = 0
         IREVCM = 0
         LOOP = .TRUE.
         INIT = 'I'
*
   40    CONTINUE
         CALL F11BEF(IREVCM,X,B,WGT,WORK,LWREQ,IFAIL)
         IF (IREVCM.EQ.-1) THEN
            IFAILX = -1
            CALL F11XAF('Transpose',N,NNZ,A,IROW,ICOL,'No checking',X,B,
     +                  IFAILX)
         ELSE IF (IREVCM.EQ.1) THEN
            IFAILX = -1
            CALL F11XAF('No transpose',N,NNZ,A,IROW,ICOL,'No checking',
     +                  X,B,IFAILX)
         ELSE IF (IREVCM.EQ.2) THEN
            IFAIL1 = -1
            CALL F11DKF('Non symmetric','N',INIT,NITER,N,NNZ,A,IROW,
     +                  ICOL,'Check',X,B,DIAG,WORK(LWREQ+1),IFAIL1)
            INIT = 'N'
            IF (IFAIL1.NE.0) IREVCM = 6
         ELSE IF (IREVCM.EQ.3) THEN
            IFAIL1 = 0
            CALL F11BFF(ITN,STPLHS,STPRHS,ANORM,SIGMAX,WORK,LWREQ,
     +                  IFAIL1)
            WRITE (NOUT,99999) ITN, STPLHS
         ELSE IF (IREVCM.EQ.4) THEN
            LOOP = .FALSE.
         END IF
         IF (LOOP) GO TO 40
*
*        Obtain information about the computation
*
         IFAIL1 = 0
         CALL F11BFF(ITN,STPLHS,STPRHS,ANORM,SIGMAX,WORK,LWREQ,IFAIL1)
*
*        Print the output data
*
         WRITE (NOUT,99996)
         WRITE (NOUT,99995) 'Number of iterations for convergence:    ',
     +     ITN
         WRITE (NOUT,99994) 'Residual norm:                           ',
     +     STPLHS
         WRITE (NOUT,99994) 'Right-hand side of termination criterion:',
     +     STPRHS
         WRITE (NOUT,99994) '1-norm of matrix A:                      ',
     +     ANORM
*
*        Output x
*
         WRITE (NOUT,99998)
         WRITE (NOUT,99997) (X(I),B(I),I=1,N)
      END IF
      STOP
*
99999 FORMAT (/1X,'Monitoring at iteration no.',I4,/1X,1P,'residual no',
     +       'rm: ',D14.4)
99998 FORMAT (/2X,'  Solution vector',2X,' Residual vector')
99997 FORMAT (1X,1P,D16.4,1X,D16.4)
99996 FORMAT (/1X,'Final Results')
99995 FORMAT (1X,A,I4)
99994 FORMAT (1X,A,1P,D14.4)
      END
