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

    MODULE e04ycfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: mdec = 15, ndec = 3, nin = 5,    &
                                              nout = 6
       INTEGER, PARAMETER                  ::                                  &
             lwork = 7*ndec + ndec*ndec + 2*mdec*ndec + 3*mdec+ ndec*(ndec-1)/2
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: t(mdec,ndec), y(mdec)
    CONTAINS
!dec$ attributes offload : mic :: lsfun1
       SUBROUTINE lsfun1(m,n,xc,fvec,iuser,ruser)
!         Routine to evaluate the residuals

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: m, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: fvec(m)
          REAL (KIND=nag_wp), INTENT (INOUT)  :: ruser(*)
          REAL (KIND=nag_wp), INTENT (IN)     :: xc(n)
          INTEGER, INTENT (INOUT)             :: iuser(*)
!         .. Executable Statements ..
          fvec(1:m) = xc(1) + t(1:m,1)/(xc(2)*t(1:m,2)+xc(3)*t(1:m,3)) - &
             y(1:m)

          RETURN

!dec$ attributes offload : mic :: lsfun1
       END SUBROUTINE lsfun1
    END MODULE e04ycfe_mod
    PROGRAM e04ycfe

!      E04YCF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e04fyf, e04ycf, nag_wp
       USE e04ycfe_mod, ONLY : lsfun1, lwork, mdec, ndec, nin, nout, t, y
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: fsumsq
       INTEGER                             :: i, ifail, job, ldv, m, n, ns, nv
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: cj(ndec), ruser(1), work(lwork), &
                                              x(ndec)
       INTEGER                             :: iuser(1)
!      .. Intrinsic Functions ..
       INTRINSIC                              max
!      .. Executable Statements ..
       WRITE (nout,*) 'E04YCF 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

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

       ifail = -1
       CALL e04fyf(m,n,lsfun1,x,fsumsq,work,lwork,iuser,ruser,ifail)

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

!         Compute estimates of the variances of the sample regression
!         coefficients at the final point.
!         Since NS is greater than N we can use the first N elements
!         of the array WORK for the dummy argument WORK.

          ns = 6*n + 2*m + m*n + 1 + max(1,(n*(n-1))/2)
          nv = ns + n
          job = 0
          ldv = n

          ifail = -1
          CALL e04ycf(job,m,n,fsumsq,work(ns),work(nv),ldv,cj,work,ifail)

          SELECT CASE (ifail)
          CASE (0,3:)
             WRITE (nout,*)
             WRITE (nout,*) 'and estimates of the variances of the sample'
             WRITE (nout,*) 'regression coefficients are'
             WRITE (nout,99998) cj(1:n)
          END SELECT

       END SELECT

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