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

    MODULE e04ybfe_mod

!      E04YBF 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, mdec = 15, ndec = 3,    &
                                              nin = 5, nout = 6
       INTEGER, PARAMETER                  :: lb = ndec*(ndec+1)/2
       INTEGER, PARAMETER                  :: ldfjac = mdec
       INTEGER, PARAMETER                  ::                                  &
                               lw = 5*ndec + mdec + mdec*ndec + ndec*(ndec-1)/2
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: t(mdec,ndec), y(mdec)
    CONTAINS
!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.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 :: 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.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 :: lsqhes
       END SUBROUTINE lsqhes
    END MODULE e04ybfe_mod
    PROGRAM e04ybfe

!      E04YBF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e04yaf, e04ybf, nag_wp
       USE e04ybfe_mod, ONLY : lb, ldfjac, liw, lsqfun, lsqhes, lw, mdec,      &
                               ndec, nin, nout, t, y
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       INTEGER                             :: i, ifail, k, m, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: b(lb), fjac(ldfjac,ndec),        &
                                              fvec(mdec), w(lw), x(ndec)
       INTEGER                             :: iw(liw)
!      .. Executable Statements ..
       WRITE (nout,*) 'E04YBF Example Program Results'

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

       m = mdec
       n = ndec

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

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

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

       x(1:n) = (/ 0.19E0_nag_wp, -1.34E0_nag_wp, 0.88E0_nag_wp/)

!      Check the 1st derivatives

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

       WRITE (nout,*)
       WRITE (nout,*) 'The test point is'
       WRITE (nout,99999) x(1:n)

!      Check the evaluation of B

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

       IF (ifail>=0 .AND. ifail/=1) THEN

          SELECT CASE (ifail)
          CASE (0)
             WRITE (nout,*)
             WRITE (nout,*) 'The matrix B is consistent with 1st derivatives'
          CASE (2)
             WRITE (nout,*)
             WRITE (nout,*) 'Probable error in calculation of the matrix B'
          END SELECT

          WRITE (nout,*)
          WRITE (nout,*) 'At the test point, LSQFUN gives'
          WRITE (nout,*)
          WRITE (nout,*) '      Residuals                   1st derivatives'
          WRITE (nout,99998) (fvec(i),fjac(i,1:n),i=1,m)
          WRITE (nout,*)
          WRITE (nout,*) 'and LSQHES gives the lower triangle of the matrix B'
          WRITE (nout,*)

          k = 1

          DO i = 1, n
             WRITE (nout,99998) b(k:(k+i-1))
             k = k + i
          END DO

       END IF

99999  FORMAT (1X,4F10.5)
99998  FORMAT (1X,1P,4E15.3)
    END PROGRAM e04ybfe
