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

    MODULE c05zdfe_mod

!      C05ZDF 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, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: get_fvec
       SUBROUTINE get_fvec(x,fvec)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: fvec(:)
          REAL (KIND=nag_wp), INTENT (IN)     :: x(:)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: u, v, w
          INTEGER                             :: i
!         .. Local Arrays ..
          REAL (KIND=nag_wp), ALLOCATABLE     :: y(:)
!         .. Intrinsic Functions ..
          INTRINSIC                              min, real
!         .. Executable Statements ..
          ALLOCATE (y(m))

          y(1:m) = real((/14,18,22,25,29,32,35,39,47,58,73,96,134,210,439/), &
             kind=nag_wp)
          y(1:m) = y(1:m)*0.01_nag_wp

          DO i = 1, m
             u = real(i,kind=nag_wp)
             v = real(m+1-i,kind=nag_wp)
             w = min(u,v)
             fvec(i) = y(i) - (x(1)+u/(v*x(2)+w*x(3)))
          END DO

          RETURN
!dec$ attributes offload : mic :: get_fvec
       END SUBROUTINE get_fvec
!dec$ attributes offload : mic :: get_fjac
       SUBROUTINE get_fjac(x,fjac)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (INOUT)  :: fjac(:,:)
          REAL (KIND=nag_wp), INTENT (IN)     :: x(:)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: denom, u, v, w
          INTEGER                             :: i
!         .. Intrinsic Functions ..
          INTRINSIC                              min, real
!         .. Executable Statements ..
          DO i = 1, m
             u = real(i,kind=nag_wp)
             v = real(m+1-i,kind=nag_wp)
             w = min(u,v)
             denom = (v*x(2)+w*x(3))**(-2)
             fjac(i,1:n) = (/ -1.0E0_nag_wp, u*v*denom, u*w*denom/)
          END DO

          RETURN
!dec$ attributes offload : mic :: get_fjac
       END SUBROUTINE get_fjac
    END MODULE c05zdfe_mod
    PROGRAM c05zdfe

!      C05ZDF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : c05zdf, nag_wp
       USE c05zdfe_mod, ONLY : get_fjac, get_fvec, m, n, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       INTEGER                             :: i, ifail, mode
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: err(:), fjac(:,:), fvec(:),      &
                                              fvecp(:), x(:), xp(:)
!      .. Intrinsic Functions ..
       INTRINSIC                              any
!      .. Executable Statements ..
       WRITE (nout,*) 'C05ZDF Example Program Results'

       ALLOCATE (err(m),fjac(m,n),fvec(m),fvecp(m),x(n),xp(n))

!      Point at which to check gradients:

       x(1:n) = (/ 0.92_nag_wp, 0.13_nag_wp, 0.54_nag_wp/)

       mode = 1

       ifail = 0
       CALL c05zdf(mode,m,n,x,fvec,fjac,xp,fvecp,err,ifail)

       CALL get_fvec(x,fvec)

       CALL get_fvec(xp,fvecp)

       CALL get_fjac(x,fjac)

       mode = 2

       ifail = 0
       CALL c05zdf(mode,m,n,x,fvec,fjac,xp,fvecp,err,ifail)

       WRITE (nout,*)
       WRITE (nout,99999) 'At point ', (x(i),i=1,n), ','

       IF (any(err(1:m)<=0.5_nag_wp)) THEN

          DO i = 1, m

             IF (err(i)<=0.5_nag_wp) THEN
                WRITE (nout,99998) 'suspicious gradient number ', i, &
                   ' with error measure ', err(i)
             END IF

          END DO

       ELSE
          WRITE (nout,99997) 'gradients appear correct'
       END IF

99999  FORMAT (1X,A,3F12.4,A)
99998  FORMAT (1X,A,I5,A,F12.4)
99997  FORMAT (1X,A)
    END PROGRAM c05zdfe
