!   E04HYF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.
    MODULE e04hyfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: m = 15, n = 3, nin = 5,          &
                                              nout = 6, nt = 3
       INTEGER, PARAMETER                  :: lw = 8*n + 2*n*n + 2*m*n + 3*m
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: t(m,nt), y(m)
    CONTAINS
!dec$ attributes offload : mic :: lsfun2
       SUBROUTINE lsfun2(m,n,xc,fvec,fjac,ldfjac,iuser,ruser)
!         Routine to evaluate the residuals and their 1st derivatives.

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: ldfjac, m, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (INOUT)  :: fjac(ldfjac,n), ruser(*)
          REAL (KIND=nag_wp), INTENT (OUT)    :: fvec(m)
          REAL (KIND=nag_wp), INTENT (IN)     :: xc(n)
          INTEGER, INTENT (INOUT)             :: iuser(*)
!         .. 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.0E0_nag_wp
             dummy = -1.0E0_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 :: lsfun2
       END SUBROUTINE lsfun2
!dec$ attributes offload : mic :: lshes2
       SUBROUTINE lshes2(m,n,fvec,xc,b,lb,iuser,ruser)
!         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 (IN)                :: lb, 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)  :: ruser(*)
          INTEGER, INTENT (INOUT)             :: iuser(*)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: dummy, sum22, sum32, sum33
          INTEGER                             :: i
!         .. Executable Statements ..
          b(1) = 0.0E0_nag_wp
          b(2) = 0.0E0_nag_wp
          sum22 = 0.0E0_nag_wp
          sum32 = 0.0E0_nag_wp
          sum33 = 0.0E0_nag_wp

          DO i = 1, m
             dummy = 2.0E0_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.0E0_nag_wp
          b(5) = sum32
          b(6) = sum33

          RETURN

!dec$ attributes offload : mic :: lshes2
       END SUBROUTINE lshes2
    END MODULE e04hyfe_mod
    PROGRAM e04hyfe

!      E04HYF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e04hyf
       USE e04hyfe_mod, ONLY : lsfun2, lshes2, lw, m, n, nag_wp, nin, nout,    &
                               nt, t, y
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: fsumsq
       INTEGER                             :: i, ifail
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: ruser(1), w(lw), x(n)
       INTEGER                             :: iuser(1)
!      .. Executable Statements ..
       WRITE (nout,*) 'E04HYF 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

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

       ifail = -1
       CALL e04hyf(m,n,lsfun2,lshes2,x,fsumsq,w,lw,iuser,ruser,ifail)

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

99999  FORMAT (1X,A,3F12.4)
    END PROGRAM e04hyfe
