!   F12FBF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE f12fbfe_mod

!      F12FBF Example Program Module:
!             Parameters and User-defined Routines

!      .. Use Statements ..
       USE nag_library, ONLY : 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
       REAL (KIND=nag_wp), PARAMETER       :: zero = 0.0_nag_wp
       INTEGER, PARAMETER                  :: imon = 0, licomm = 140, nin = 5, &
                                              nout = 6
    CONTAINS
!dec$ attributes offload : mic :: av
       SUBROUTINE av(m,n,x,w)

!         Computes  w <- A*x.

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: m, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: w(m)
          REAL (KIND=nag_wp), INTENT (IN)     :: x(n)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: h, k, s, t
          INTEGER                             :: i, j
!         .. Intrinsic Functions ..
          INTRINSIC                              real
!         .. Executable Statements ..
          h = one/real(m+1,kind=nag_wp)
          k = one/real(n+1,kind=nag_wp)
          w(1:m) = zero
          t = zero

          DO j = 1, n
             t = t + k
             s = zero
             DO i = 1, j
                s = s + h
                w(i) = w(i) + k*s*(t-one)*x(j)
             END DO
             DO i = j + 1, m
                s = s + h
                w(i) = w(i) + k*t*(s-one)*x(j)
             END DO
          END DO

          RETURN
!dec$ attributes offload : mic :: av
       END SUBROUTINE av

!dec$ attributes offload : mic :: atv
       SUBROUTINE atv(m,n,w,y)

!         Computes  y <- A'*w.

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: m, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: w(m)
          REAL (KIND=nag_wp), INTENT (OUT)    :: y(n)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: h, k, s, t
          INTEGER                             :: i, j
!         .. Intrinsic Functions ..
          INTRINSIC                              real
!         .. Executable Statements ..
          h = one/real(m+1,kind=nag_wp)
          k = one/real(n+1,kind=nag_wp)
          y(1:n) = zero
          t = zero

          DO j = 1, n
             t = t + k
             s = zero
             DO i = 1, j
                s = s + h
                y(j) = y(j) + k*s*(t-one)*w(i)
             END DO
             DO i = j + 1, m
                s = s + h
                y(j) = y(j) + k*t*(s-one)*w(i)
             END DO
          END DO

          RETURN
!dec$ attributes offload : mic :: atv
       END SUBROUTINE atv
    END MODULE f12fbfe_mod
    PROGRAM f12fbfe

!      F12FBF Example Main Program

!      .. Use Statements ..
       USE f12fbfe_mod, ONLY : nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Executable Statements ..
       WRITE (nout,*) 'F12FBF Example Program Results'

       CALL ex1

       CALL ex2

    CONTAINS
!dec$ attributes offload : mic :: ex1
       SUBROUTINE ex1

!         .. Use Statements ..
          USE nag_library, ONLY : dgttrf, dgttrs, dnrm2, f12faf, f12fbf,       &
                                  f12fcf, f12fdf, f12fef
          USE f12fbfe_mod, ONLY : imon, licomm, nag_wp, nin, one, two, zero
!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: h2, sigma
          INTEGER                             :: ifail, info, irevcm, j,       &
                                                 lcomm, ldv, n, nconv, ncv,    &
                                                 nev, niter, nshift
!         .. Local Arrays ..
          REAL (KIND=nag_wp), ALLOCATABLE     :: ad(:), adl(:), adu(:),        &
                                                 adu2(:), comm(:), d(:,:),     &
                                                 mx(:), resid(:), v(:,:), x(:)
          INTEGER                             :: icomm(licomm)
          INTEGER, ALLOCATABLE                :: ipiv(:)
!         .. Intrinsic Functions ..
          INTRINSIC                              real
!         .. Executable Statements ..
          WRITE (nout,*)
          WRITE (nout,*) 'F12FBF Example 1'
          WRITE (nout,*)
          FLUSH (nout)
!         Skip heading in data file
          READ (nin,*)
          READ (nin,*)
          READ (nin,*) n, nev, ncv

          ldv = n
          lcomm = 3*n + ncv*ncv + 8*ncv + 60
          ALLOCATE (ad(n),adl(n),adu(n),adu2(n),comm(lcomm),d(ncv,2),mx(n), &
             resid(n),v(ldv,ncv),x(n),ipiv(n))

          ifail = 0
          CALL f12faf(n,nev,ncv,icomm,licomm,comm,lcomm,ifail)

!         Set the region of the spectrum that is required.
          CALL f12fdf('LARGEST MAGNITUDE',icomm,comm,ifail)
!         Use the Shifted Inverse mode.
          CALL f12fdf('SHIFTED INVERSE',icomm,comm,ifail)

          h2 = one/real((n+1)*(n+1),kind=nag_wp)
          sigma = zero
          ad(1:n) = two/h2 - sigma
          adl(1:n) = -one/h2
          adu(1:n) = adl(1:n)
!         The NAG name equivalent of dgttrf is f07cdf
          CALL dgttrf(n,adl,ad,adu,adu2,ipiv,info)

          irevcm = 0
          ifail = -1
REVCM:    DO
             CALL f12fbf(irevcm,resid,v,ldv,x,mx,nshift,comm,icomm,ifail)
             IF (irevcm==5) THEN
                EXIT REVCM
             ELSE IF (irevcm==-1 .OR. irevcm==1) THEN
!               Perform matrix vector multiplication
!               y <--- inv[A-sigma*I]*x
!               The NAG name equivalent of dgttrs is f07cef
                CALL dgttrs('N',n,1,adl,ad,adu,adu2,ipiv,x,n,info)
             ELSE IF (irevcm==4 .AND. imon/=0) THEN
!               Output monitoring information
                CALL f12fef(niter,nconv,d,d(1,2),icomm,comm)
!               The NAG name equivalent of dnrm2 is f06ejf
                WRITE (6,99999) niter, nconv, dnrm2(nev,d(1,2),1)
                FLUSH (nout)
             END IF
          END DO REVCM

          IF (ifail==0) THEN
!            Post-Process using F12FCF to compute eigenvalues/vectors.
             CALL f12fcf(nconv,d,v,ldv,sigma,resid,v,ldv,comm,icomm,ifail)
             WRITE (nout,99998) nconv, sigma
             WRITE (nout,99997) (j,d(j,1),j=1,nconv)
          END IF

          DEALLOCATE (ad,adl,adu,adu2,comm,d,mx,resid,v,x,ipiv)

          RETURN

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 ',F8.4,' are:'/)
99997     FORMAT (1X,I8,5X,F12.4)
!dec$ attributes offload : mic :: ex1
       END SUBROUTINE ex1
!dec$ attributes offload : mic :: ex2
       SUBROUTINE ex2

!         .. Use Statements ..
          USE nag_library, ONLY : daxpy, dnrm2, dscal, f12faf, f12fbf, f12fcf, &
                                  x04caf
          USE f12fbfe_mod, ONLY : atv, av, licomm, nag_wp, nin, one
!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: sigma, temp
          INTEGER                             :: ifail, ifail1, irevcm, j,     &
                                                 lcomm, ldu, ldv, m, n, nconv, &
                                                 ncv, nev, nshift
!         .. Local Arrays ..
          REAL (KIND=nag_wp), ALLOCATABLE     :: ax(:), comm(:), d(:,:),       &
                                                 mx(:), resid(:), u(:,:),      &
                                                 v(:,:), x(:)
          INTEGER                             :: icomm(licomm)
!         .. Intrinsic Functions ..
          INTRINSIC                              sqrt
!         .. Executable Statements ..
          WRITE (nout,*)
          WRITE (nout,*) 'F12FBF Example 2'
          WRITE (nout,*)
          FLUSH (nout)
!         Skip heading in data file
          READ (nin,*)
          READ (nin,*) m, n, nev, ncv

          ldu = m
          ldv = n
          lcomm = 3*n + ncv*ncv + 8*ncv + 60
          ALLOCATE (ax(m),comm(lcomm),d(ncv,2),mx(m),resid(n),u(ldu,nev), &
             v(ldv,ncv),x(m))

!         Initialize for problem.
          ifail = 0
          CALL f12faf(n,nev,ncv,icomm,licomm,comm,lcomm,ifail)

!         Main reverse communication loop.
          irevcm = 0
          ifail = -1
REVCM:    DO
             CALL f12fbf(irevcm,resid,v,ldv,x,mx,nshift,comm,icomm,ifail)
             IF (irevcm==5) THEN
                EXIT REVCM
             ELSE IF (irevcm==-1 .OR. irevcm==1) THEN
!               Perform the operation X <- A'AX
                CALL av(m,n,x,ax)
                CALL atv(m,n,ax,x)
             END IF
          END DO REVCM

          IF (ifail==0) THEN
!            Post-Process using F12FCF.
!            Computed singular values may be extracted.
!            Singular vectors may also be computed now if desired.
             ifail1 = 0
             CALL f12fcf(nconv,d,v,ldv,sigma,resid,v,ldv,comm,icomm,ifail1)

!            Singular values (squared) are returned in the first column
!            of D and the corresponding right singular vectors are
!            returned in the first NEV columns of V.

             DO j = 1, nconv
                d(j,1) = sqrt(d(j,1))

!               Compute the left singular vectors from the formula
!               u = Av/sigma/norm(Av).

                CALL av(m,n,v(1,j),ax)
                u(1:m,j) = ax(1:m)
!               The NAG name equivalent of dnrm2 is f06ejf
                temp = one/dnrm2(m,u(1,j),1)
!               The NAG name equivalent of dscal is f06edf
                CALL dscal(m,temp,u(1,j),1)

!               Compute the residual norm ||A*v - sigma*u|| for the nconv
!               accurately computed singular values and vectors.
!               Store the result in 2nd column of array D.

!               The NAG name equivalent of daxpy is f06ecf
                CALL daxpy(m,-d(j,1),u(1,j),1,ax,1)
                d(j,2) = dnrm2(m,ax,1)

             END DO

!            Print computed residuals

             CALL x04caf('G','N',nconv,2,d,ncv, &
                ' Singular values and direct residuals',ifail1)
          END IF

          DEALLOCATE (ax,comm,d,mx,resid,u,v,x)

          RETURN

!dec$ attributes offload : mic :: ex2
       END SUBROUTINE ex2
    END PROGRAM f12fbfe
