*     F12FAF Example Program Text
*     Mark 21 Release. NAG Copyright 2004.
*     .. Parameters ..
      INTEGER          IMON, IPOINT, LICOMM, NIN, NOUT
      PARAMETER        (IMON=0,IPOINT=0,LICOMM=134,NIN=5,NOUT=6)
      INTEGER          MAXN, MAXNCV, LDV
      PARAMETER        (MAXN=256,MAXNCV=30,LDV=MAXN)
      INTEGER          LCOMM
      PARAMETER        (LCOMM=3*MAXN+MAXNCV*MAXNCV+8*MAXNCV+60)
*     .. Local Scalars ..
      DOUBLE PRECISION SIGMA
      INTEGER          IFAIL, IREVCM, J, N, NCONV, NCV, NEV, NITER,
     +                 NSHIFT, NX
*     .. Local Arrays ..
      DOUBLE PRECISION AX(MAXN), COMM(LCOMM), D(MAXNCV,2), MX(MAXN),
     +                 RESID(MAXN), V(LDV,MAXNCV), X(MAXN)
      INTEGER          ICOMM(LICOMM)
*     .. External Functions ..
      DOUBLE PRECISION DNRM2
      EXTERNAL         DNRM2
*     .. External Subroutines ..
      EXTERNAL         AV, DCOPY, F12FAF, F12FBF, F12FCF, F12FDF, F12FEF
*     .. Executable Statements ..
      WRITE (NOUT,*) 'F12FAF Example Program Results'
      WRITE (NOUT,*)
*     Skip heading in data file
      READ (NIN,*)
      READ (NIN,*) NX, NEV, NCV
      N = NX*NX
      IF (N.LT.1 .OR. N.GT.MAXN) THEN
         WRITE (NOUT,99999) 'N is out of range: N = ', N
      ELSE IF (NCV.GT.MAXNCV) THEN
         WRITE (NOUT,99999) 'NCV is out of range: NCV = ', NCV
      ELSE
         IFAIL = 0
         CALL F12FAF(N,NEV,NCV,ICOMM,LICOMM,COMM,LCOMM,IFAIL)
*     Set the region of the spectrum that is required.
         CALL F12FDF('SMALLEST MAGNITUDE',ICOMM,COMM,IFAIL)
*     Increase the iteration limit if required.
         CALL F12FDF('ITERATION LIMIT=500',ICOMM,COMM,IFAIL)
         IF (IPOINT.EQ.1) THEN
*        Use pointers to Workspace in calculating matrix vector products
*        rather than interfacing through the array X
            CALL F12FDF('POINTERS=YES',ICOMM,COMM,IFAIL)
         END IF
         IREVCM = 0
         IFAIL = -1
   20    CONTINUE
         CALL F12FBF(IREVCM,RESID,V,LDV,X,MX,NSHIFT,COMM,ICOMM,IFAIL)
         IF (IREVCM.NE.5) THEN
            IF (IREVCM.EQ.-1 .OR. IREVCM.EQ.1) THEN
*           Perform matrix vector multiplication y <--- Op*x
               IF (IPOINT.EQ.0) THEN
                  CALL AV(NX,X,AX)
                  CALL DCOPY(N,AX,1,X,1)
               ELSE
                  CALL AV(NX,COMM(ICOMM(1)),COMM(ICOMM(2)))
               END IF
            ELSE IF (IREVCM.EQ.4 .AND. IMON.NE.0) THEN
*           Output monitoring information
               CALL F12FEF(NITER,NCONV,D,D(1,2),ICOMM,COMM)
               WRITE (6,99998) NITER, NCONV, DNRM2(NEV,D(1,2),1)
            END IF
            GO TO 20
         END IF
         IF (IFAIL.EQ.0) THEN
*        Post-Process using F12FCF to compute eigenvalue/vectors.
            CALL F12FCF(NCONV,D,V,LDV,SIGMA,RESID,V,LDV,COMM,ICOMM,
     +                  IFAIL)
            WRITE (NOUT,99996) NCONV
            DO 40 J = 1, NCONV
               WRITE (NOUT,99995) J, D(J,1)
   40       CONTINUE
         ELSE
            WRITE (NOUT,99997) IFAIL
         END IF
      END IF
      STOP
*
99999 FORMAT (1X,A,I5)
99998 FORMAT (1X,'Iteration',1X,I3,', No. converged =',1X,I3,', norm o',
     +       'f estimates =',E16.8)
99997 FORMAT (1X,' NAG Routine F12FBF Returned with IFAIL = ',I6)
99996 FORMAT (1X,/' The ',I4,' Ritz values of smallest magnitude are:',
     +       /)
99995 FORMAT (1X,I8,5X,F12.4)
      END
*
      SUBROUTINE AV(NX,V,W)
*     .. Parameters ..
      DOUBLE PRECISION ONE
      PARAMETER     (ONE=1.0D+0)
*     .. Scalar Arguments ..
      INTEGER       NX
*     .. Array Arguments ..
      DOUBLE PRECISION V(NX*NX), W(NX*NX)
*     .. Local Scalars ..
      DOUBLE PRECISION H2
      INTEGER       J, LO
*     .. External Subroutines ..
      EXTERNAL      DAXPY, DSCAL, TV
*     .. Intrinsic Functions ..
      INTRINSIC     DBLE
*     .. Executable Statements ..
      H2 = ONE/DBLE((NX+1)*(NX+1))
      CALL TV(NX,V(1),W(1))
      CALL DAXPY(NX,-ONE,V(NX+1),1,W(1),1)
      DO 20 J = 2, NX - 1
         LO = (J-1)*NX
         CALL TV(NX,V(LO+1),W(LO+1))
         CALL DAXPY(NX,-ONE,V(LO-NX+1),1,W(LO+1),1)
         CALL DAXPY(NX,-ONE,V(LO+NX+1),1,W(LO+1),1)
   20 CONTINUE
      LO = (NX-1)*NX
      CALL TV(NX,V(LO+1),W(LO+1))
      CALL DAXPY(NX,-ONE,V(LO-NX+1),1,W(LO+1),1)
      CALL DSCAL(NX*NX,ONE/H2,W,1)
      RETURN
      END
*
      SUBROUTINE TV(NX,X,Y)
*     Compute the matrix vector multiplication y<---T*x where T is a nx
*     by nx tridiagonal matrix.
*     .. Parameters ..
      DOUBLE PRECISION ONE
      PARAMETER     (ONE=1.0D+0)
*     .. Scalar Arguments ..
      INTEGER       NX
*     .. Array Arguments ..
      DOUBLE PRECISION X(NX), Y(NX)
*     .. Local Scalars ..
      DOUBLE PRECISION DD, DL, DU
      INTEGER       J
*     .. Executable Statements ..
      DD = 4.0D+0
      DL = -ONE
      DU = -ONE
      Y(1) = DD*X(1) + DU*X(2)
      DO 20 J = 2, NX - 1
         Y(J) = DL*X(J-1) + DD*X(J) + DU*X(J+1)
   20 CONTINUE
      Y(NX) = DL*X(NX-1) + DD*X(NX)
      RETURN
      END
