    PROGRAM f12abfe

!      F12ABF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dgttrf, dgttrs, dnrm2, f12aaf, f12abf, f12acf,  &
                               f12adf, f12aef, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER   :: one = 1.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER   :: two = 2.0_nag_wp
       INTEGER, PARAMETER              :: imon = 0, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: h, rho, s, s1, s2, s3, sigmai, sigmar
       INTEGER                         :: i, ifail, ifail1, info, irevcm,      &
                                          lcomm, ldv, licomm, n, nconv, ncv,   &
                                          nev, niter, nshift, nx
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: comm(:), d(:,:), dd(:), dl(:),       &
                                          du(:), du2(:), mx(:), resid(:),      &
                                          v(:,:), x(:)
       INTEGER, ALLOCATABLE            :: icomm(:), ipiv(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          real
!      .. Executable Statements ..
       WRITE (nout,*) 'F12ABF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) nx, nev, ncv, rho, sigmar, sigmai
       n = nx*nx
       ldv = n
       licomm = 140
       lcomm = 3*n + 3*ncv*ncv + 6*ncv + 60
       ALLOCATE (comm(lcomm),d(ncv,3),dd(n),dl(n),du(n),du2(n),mx(n),resid(n), &
          v(ldv,ncv),x(n),icomm(licomm),ipiv(n))

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f12aaf(n,nev,ncv,icomm,licomm,comm,lcomm,ifail)

!      Set the mode.
       ifail = 0
       CALL f12adf('SHIFTED INVERSE REAL',icomm,comm,ifail)

!      Construct C = A - SIGMA*I, and factorize using DGTTRF/F07CDF.
       h = one/real(n+1,kind=nag_wp)
       s = rho*h/two
       s1 = -one - s
       s2 = two - sigmar
       s3 = -one + s
       dl(1:n-1) = s1
       dd(1:n-1) = s2
       du(1:n-1) = s3
       dd(n) = s2

!      The NAG name equivalent of dgttrf is f07cdf
       CALL dgttrf(n,dl,dd,du,du2,ipiv,info)

       irevcm = 0
       ifail = -1
LOOP:  DO
          CALL f12abf(irevcm,resid,v,ldv,x,mx,nshift,comm,icomm,ifail)

          IF (irevcm/=5) THEN
             IF (irevcm==-1 .OR. irevcm==1) THEN
!               Perform  x <--- OP*x = inv[A-SIGMA*I]*x.
!               The NAG name equivalent of dgttrs is f07cef
                CALL dgttrs('N',n,1,dl,dd,du,du2,ipiv,x,n,info)
             ELSE IF (irevcm==4 .AND. imon/=0) THEN
!               Output monitoring information
                CALL f12aef(niter,nconv,d,d(1,2),d(1,3),icomm,comm)
!               The NAG name equivalent of dnrm2 is f06ejf
                WRITE (6,99999) niter, nconv, dnrm2(nev,d(1,3),1)
             END IF
          ELSE
             EXIT LOOP
          END IF
       END DO LOOP
       IF (ifail==0) THEN
!         Post-Process using F12ACF to compute eigenvalues/vectors.
          ifail1 = 0
          CALL f12acf(nconv,d,d(1,2),v,ldv,sigmar,sigmai,resid,v,ldv,comm, &
             icomm,ifail1)
!         Print computed eigenvalues.
          WRITE (nout,99998) nconv
          DO i = 1, nconv
             WRITE (nout,99997) i, d(i,1), d(i,2)
          END DO
       END IF

99999  FORMAT (1X,'Iteration',1X,I3,', No. converged =',1X,I3,', norm o', &
          'f estimates =',E16.8)
99998  FORMAT (1X/' The ',I4,' Ritz values of closest to unity are:'/)
99997  FORMAT (1X,I8,5X,'( ',F12.4,' , ',F12.4,' )')
    END PROGRAM f12abfe
