!   E04HEF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.
    MODULE e04hefe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: liw = 1, m = 15, n = 3, nin = 5, &
                                              nout = 6, nt = 3
       INTEGER, PARAMETER                  :: lb = n*(n+1)/2
       INTEGER, PARAMETER                  :: ldfjac = m
       INTEGER, PARAMETER                  :: ldv = n
       INTEGER, PARAMETER                  :: lw = 7*n + m*n + 2*m + n*n
!      .. 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('T',m,n,1.0_nag_wp,fjac,ldfjac,fvec,1,0.0_nag_wp,g,1)

          g(1:n) = 2.0_nag_wp*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

!         .. 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)
             fvec(i) = xc(1) + t(i,1)/denom - y(i)
             fjac(i,1) = 1.0_nag_wp
             dummy = -1.0_nag_wp/(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 :: lsqhes
       SUBROUTINE lsqhes(iflag,m,n,fvec,xc,b,lb,iw,liw,w,lw)

!         Routine to compute the lower triangle of the matrix B
!         (stored by rows in the array B)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (INOUT)             :: iflag
          INTEGER, INTENT (IN)                :: lb, liw, lw, m, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: b(lb)
          REAL (KIND=nag_wp), INTENT (IN)     :: fvec(m), xc(n)
          REAL (KIND=nag_wp), INTENT (INOUT)  :: w(lw)
          INTEGER, INTENT (INOUT)             :: iw(liw)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: dummy, sum22, sum32, sum33
          INTEGER                             :: i
!         .. Executable Statements ..
          b(1) = 0.0_nag_wp
          b(2) = 0.0_nag_wp
          sum22 = 0.0_nag_wp
          sum32 = 0.0_nag_wp
          sum33 = 0.0_nag_wp

          DO i = 1, m
             dummy = 2.0_nag_wp*t(i,1)/(xc(2)*t(i,2)+xc(3)*t(i,3))**3
             sum22 = sum22 + fvec(i)*dummy*t(i,2)**2
             sum32 = sum32 + fvec(i)*dummy*t(i,2)*t(i,3)
             sum33 = sum33 + fvec(i)*dummy*t(i,3)**2
          END DO

          b(3) = sum22
          b(4) = 0.0_nag_wp
          b(5) = sum32
          b(6) = sum33

          RETURN

!dec$ attributes offload : mic :: lsqhes
       END SUBROUTINE lsqhes
!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 : f06eaf
!         .. 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 ..
          fsumsq = f06eaf(m,fvec,1,fvec,1)

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

          gtg = f06eaf(n,g,1,g,1)

          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'

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

          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 e04hefe_mod
    PROGRAM e04hefe

!      E04HEF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e04hef, e04yaf, e04ybf, x02ajf
       USE e04hefe_mod, ONLY : lb, ldfjac, ldv, liw, lsqfun, lsqgrd, lsqhes,   &
                               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)                  :: b(lb), 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,*) 'E04HEF 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

!      Set up an arbitrary point at which to check the derivatives

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

!      Check the 1st derivatives

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

!      Check the evaluation of B

       ifail = 0
       CALL e04ybf(m,n,lsqfun,lsqhes,x,fvec,fjac,ldfjac,b,lb,iw,liw,w,lw, &
          ifail)

!      Continue setting parameters for E04HEF

!      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 e04hef(m,n,lsqfun,lsqhes,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 e04hefe
