!   E04GDF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.
    MODULE e04gdfe_mod

!      E04GDF 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                  :: inc1 = 1, liw = 1, m = 15,       &
                                              n = 3, nin = 5, nout = 6, nt = 3
       INTEGER, PARAMETER                  :: ldfjac = m
       INTEGER, PARAMETER                  :: ldv = n
       INTEGER, PARAMETER                  :: lw = 7*n + m*n + 2*m + n*n
       CHARACTER (1), PARAMETER            :: trans = 'T'
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: t(m,nt), y(m)
    CONTAINS
!dec$ attributes offload : mic :: lsqgrd
       SUBROUTINE lsqgrd(m,n,fvec,fjac,ldfjac,g)
!         Routine to evaluate gradient of the sum of squares

!         .. Use Statements ..
          USE nag_library, ONLY : dgemv
!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: ldfjac, m, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: fjac(ldfjac,n), fvec(m)
          REAL (KIND=nag_wp), INTENT (OUT)    :: g(n)
!         .. Executable Statements ..
!         The NAG name equivalent of dgemv is f06paf
          CALL dgemv(trans,m,n,one,fjac,ldfjac,fvec,inc1,zero,g,inc1)

          g(1:n) = two*g(1:n)

          RETURN

!dec$ attributes offload : mic :: lsqgrd
       END SUBROUTINE lsqgrd
!dec$ attributes offload : mic :: lsqfun
       SUBROUTINE lsqfun(iflag,m,n,xc,fvec,fjac,ldfjac,iw,liw,w,lw)

!         Routine to evaluate the residuals and their 1st derivatives.
!         A global variable could be updated here to count the
!         number of calls of LSQFUN with IFLAG set to 1 (since NF
!         in LSQMON only counts calls with IFLAG set to 2)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (INOUT)             :: iflag
          INTEGER, INTENT (IN)                :: ldfjac, liw, lw, m, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (INOUT)  :: fjac(ldfjac,n), w(lw)
          REAL (KIND=nag_wp), INTENT (OUT)    :: fvec(m)
          REAL (KIND=nag_wp), INTENT (IN)     :: xc(n)
          INTEGER, INTENT (INOUT)             :: iw(liw)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: denom, dummy
          INTEGER                             :: i
!         .. Executable Statements ..
          DO i = 1, m
             denom = xc(2)*t(i,2) + xc(3)*t(i,3)

             IF (iflag==2) THEN
                fvec(i) = xc(1) + t(i,1)/denom - y(i)
             END IF

             fjac(i,1) = one
             dummy = -one/(denom*denom)
             fjac(i,2) = t(i,1)*t(i,2)*dummy
             fjac(i,3) = t(i,1)*t(i,3)*dummy
          END DO

          RETURN

!dec$ attributes offload : mic :: lsqfun
       END SUBROUTINE lsqfun
!dec$ attributes offload : mic :: lsqmon
       SUBROUTINE lsqmon(m,n,xc,fvec,fjac,ldfjac,s,igrade,niter,nf,iw,liw,w, &
             lw)
!         Monitoring routine

!         .. Use Statements ..
          USE nag_library, ONLY : ddot
!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Parameters ..
          INTEGER, PARAMETER                  :: ndec = 3
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: igrade, ldfjac, liw, lw, m,   &
                                                 n, nf, niter
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: fjac(ldfjac,n), fvec(m),      &
                                                 s(n), xc(n)
          REAL (KIND=nag_wp), INTENT (INOUT)  :: w(lw)
          INTEGER, INTENT (INOUT)             :: iw(liw)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: fsumsq, gtg
          INTEGER                             :: j
!         .. Local Arrays ..
          REAL (KIND=nag_wp)                  :: g(ndec)
!         .. Executable Statements ..
!         The NAG name equivalent of ddot is f06eaf
          fsumsq = ddot(m,fvec,inc1,fvec,inc1)

          CALL lsqgrd(m,n,fvec,fjac,ldfjac,g)

          gtg = ddot(n,g,inc1,g,inc1)

!         A global variable giving the number of calls of
!         LSQFUN with IFLAG set to 1 could be printed here

          WRITE (nout,*)
          WRITE (nout,*) &
             ' Itns    F evals          SUMSQ             GTG        grade'
          WRITE (nout,99999) niter, nf, fsumsq, gtg, igrade
          WRITE (nout,*)
          WRITE (nout,*) &
             '       X                    G           Singular values'

          WRITE (nout,99998) (xc(j),g(j),s(j),j=1,n)

          RETURN

99999     FORMAT (1X,I4,6X,I5,6X,1P,E13.5,6X,1P,E9.1,6X,I3)
99998     FORMAT (1X,1P,E13.5,10X,1P,E9.1,10X,1P,E9.1)
!dec$ attributes offload : mic :: lsqmon
       END SUBROUTINE lsqmon
    END MODULE e04gdfe_mod
    PROGRAM e04gdfe

!      E04GDF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e04gdf, e04yaf, x02ajf
       USE e04gdfe_mod, ONLY : ldfjac, ldv, liw, lsqfun, lsqgrd, lsqmon, lw,   &
                               m, n, nag_wp, nin, nout, nt, t, y
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: eta, fsumsq, stepmx, xtol
       INTEGER                             :: i, ifail, iprint, maxcal, nf,    &
                                              niter
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: fjac(ldfjac,n), fvec(m), g(n),   &
                                              s(n), v(ldv,n), w(lw), x(n)
       INTEGER                             :: iw(liw)
!      .. Intrinsic Functions ..
       INTRINSIC                              sqrt
!      .. Executable Statements ..
       WRITE (nout,*) 'E04GDF Example Program Results'

!      Skip heading in data file
       READ (nin,*)

!      Observations of TJ (J = 1, 2, ..., nt) are held in T(I, J)
!      (I = 1, 2, ... , m)

       DO i = 1, m
          READ (nin,*) y(i), t(i,1:nt)
       END DO

!      Check LSQFUN by calling E04YAF at an arbitrary point. Since
!      E04YAF only checks the derivatives calculated when IFLAG = 2,
!      a separate program should be run before using E04YAF or
!      E04GDF to check that LSQFUN gives the same values for the
!      elements of FJAC when IFLAG is set to 1 as when IFLAG is
!      set to 2.

       x(1:nt) = (/ 0.19_nag_wp, -1.34_nag_wp, 0.88_nag_wp/)

       ifail = 0
       CALL e04yaf(m,n,lsqfun,x,fvec,fjac,ldfjac,iw,liw,w,lw,ifail)

!      Continue setting parameters for E04GDF

!      Set IPRINT to 1 to obtain output from LSQMON at each iteration

       iprint = -1

       maxcal = 50*n
       eta = 0.9_nag_wp
       xtol = 10.0_nag_wp*sqrt(x02ajf())

!      We estimate that the minimum will be within 10 units of the
!      starting point

       stepmx = 10.0_nag_wp

!      Set up the starting point

       x(1:nt) = (/ 0.5_nag_wp, 1.0_nag_wp, 1.5_nag_wp/)

       ifail = -1
       CALL e04gdf(m,n,lsqfun,lsqmon,iprint,maxcal,eta,xtol,stepmx,x,fsumsq, &
          fvec,fjac,ldfjac,s,v,ldv,niter,nf,iw,liw,w,lw,ifail)

       SELECT CASE (ifail)
       CASE (0,2:)
          WRITE (nout,*)
          WRITE (nout,99999) 'On exit, the sum of squares is', fsumsq
          WRITE (nout,99999) 'at the point', x(1:n)

          CALL lsqgrd(m,n,fvec,fjac,ldfjac,g)

          WRITE (nout,99998) 'The corresponding gradient is', g(1:n)
          WRITE (nout,*) '                           (machine dependent)'
          WRITE (nout,*) 'and the residuals are'
          WRITE (nout,99997) fvec(1:m)
       END SELECT

99999  FORMAT (1X,A,3F12.4)
99998  FORMAT (1X,A,1P,3E12.3)
99997  FORMAT (1X,1P,E9.1)
    END PROGRAM e04gdfe
