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

    MODULE c05qsfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: n = 9, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: fcn
       SUBROUTINE fcn(n,lindf,indf,x,fvec,iuser,ruser,iflag)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Parameters ..
          REAL (KIND=nag_wp), PARAMETER       :: one = 1.0E0_nag_wp
          REAL (KIND=nag_wp), PARAMETER       :: three = 3.0E0_nag_wp
          REAL (KIND=nag_wp), PARAMETER       :: two = 2.0E0_nag_wp
          REAL (KIND=nag_wp), PARAMETER       :: alpha = (one/two)**7
!         .. Scalar Arguments ..
          INTEGER, INTENT (INOUT)             :: iflag
          INTEGER, INTENT (IN)                :: lindf, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: fvec(n)
          REAL (KIND=nag_wp), INTENT (INOUT)  :: ruser(*)
          REAL (KIND=nag_wp), INTENT (IN)     :: x(n)
          INTEGER, INTENT (IN)                :: indf(lindf)
          INTEGER, INTENT (INOUT)             :: iuser(*)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: theta
          INTEGER                             :: i, ind
!         .. Intrinsic Functions ..
          INTRINSIC                              real
!         .. Executable Statements ..
          iflag = 0
          theta = real(iuser(1),kind=nag_wp)*alpha
          DO ind = 1, lindf
             i = indf(ind)
             fvec(i) = (three-(two+theta)*x(i))*x(i) + one
             IF (i>1) THEN
                fvec(i) = fvec(i) - x(i-1)
             END IF
             IF (i<n) THEN
                fvec(i) = fvec(i) - two*x(i+1)
             END IF
          END DO
          RETURN

!dec$ attributes offload : mic :: fcn
       END SUBROUTINE fcn
    END MODULE c05qsfe_mod
    PROGRAM c05qsfe

!      C05QSF Example Program Text

!      .. Use Statements ..
       USE nag_library, ONLY : c05qsf, dnrm2, nag_wp, x02ajf
       USE c05qsfe_mod, ONLY : fcn, n, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: fnorm, xtol
       INTEGER                             :: i, ifail, j, licomm, lrcomm
       LOGICAL                             :: init
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: fvec(:), rcomm(:), x(:)
       REAL (KIND=nag_wp)                  :: ruser(1)
       INTEGER, ALLOCATABLE                :: icomm(:)
       INTEGER                             :: iuser(1)
!      .. Intrinsic Functions ..
       INTRINSIC                              sqrt
!      .. Executable Statements ..
       WRITE (nout,*) 'C05QSF Example Program Results'

       xtol = sqrt(x02ajf())
       lrcomm = 12 + 3*n
       licomm = 8*n + 19 + 3*n

       ALLOCATE (fvec(n),x(n),rcomm(lrcomm),icomm(licomm))

!      The following starting values provide a rough solution.
       x(1:n) = -1.0E0_nag_wp

       DO i = 0, 1
          ifail = -1

!         Perturb the system?
          iuser(1) = i

          init = (i==0)
          CALL c05qsf(fcn,n,x,fvec,xtol,init,rcomm,lrcomm,icomm,licomm,iuser, &
             ruser,ifail)

          SELECT CASE (ifail)
          CASE (0)
!            The NAG name equivalent of dnrm2 is f06ejf
             fnorm = dnrm2(n,fvec,1)
             WRITE (nout,*)
             WRITE (nout,99999) 'Final 2-norm of the residuals =', fnorm
             WRITE (nout,*)
             WRITE (nout,*) 'Final approximate solution'
             WRITE (nout,*)
             WRITE (nout,99998) (x(j),j=1,n)
          CASE (2:4)
             WRITE (nout,*)
             WRITE (nout,*) 'Approximate solution'
             WRITE (nout,*)
             WRITE (nout,99998) (x(j),j=1,n)
          END SELECT
       END DO

99999  FORMAT (1X,A,E12.4)
99998  FORMAT (1X,3F12.4)
    END PROGRAM c05qsfe
