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

    MODULE g07dcfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER       :: dchi = 1.5_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: h1 = 1.5_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: h2 = 3.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: h3 = 4.5_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: zero = 0.0_nag_wp
       INTEGER, PARAMETER                  :: nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: hampels_psi
       FUNCTION hampels_psi(t)
!         Hampel's Piecewise Linear Function.

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: hampels_psi
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: t
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: abst
!         .. Intrinsic Functions ..
          INTRINSIC                              abs, min
!         .. Executable Statements ..
          abst = abs(t)
          IF (abst<h3) THEN
             IF (abst<=h2) THEN
                hampels_psi = min(h1,abst)
             ELSE
                hampels_psi = h1*(h3-abst)/(h3-h2)
             END IF
             IF (t<zero) hampels_psi = -hampels_psi
          ELSE
             hampels_psi = zero
          END IF
          RETURN
!dec$ attributes offload : mic :: hampels_psi
       END FUNCTION hampels_psi
!dec$ attributes offload : mic :: hubers_chi
       FUNCTION hubers_chi(t)
!         Huber's CHI function.

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: hubers_chi
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: t
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: abst, ps
!         .. Intrinsic Functions ..
          INTRINSIC                              abs, min
!         .. Executable Statements ..
          abst = abs(t)
          ps = min(dchi,abst)
          hubers_chi = ps*ps/2.0E0_nag_wp
          RETURN
!dec$ attributes offload : mic :: hubers_chi
       END FUNCTION hubers_chi
    END MODULE g07dcfe_mod
    PROGRAM g07dcfe

!      G07DCF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : g07dcf, nag_wp
       USE g07dcfe_mod, ONLY : hampels_psi, hubers_chi, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: beta, sigma, sigsav, thesav,     &
                                              theta, tol
       INTEGER                             :: ifail, isigma, maxit, n, nit
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: rs(:), wrk(:), x(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G07DCF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size and control parameters
       READ (nin,*) n, beta, maxit

       ALLOCATE (x(n),wrk(n),rs(n))

!      Read in data
       READ (nin,*) x(1:n)

!      Display titles
       WRITE (nout,*) '          Input parameters     Output parameters'
       WRITE (nout,*) 'ISIGMA   SIGMA   THETA   TOL    SIGMA  THETA'

D_LP:  DO
          READ (nin,*,IOSTAT=ifail) isigma, sigma, theta, tol
          IF (ifail/=0) THEN
             EXIT D_LP
          END IF

!         Save input parameters
          sigsav = sigma
          thesav = theta

!         Compute M-estimates
          ifail = 0
          CALL g07dcf(hubers_chi,hampels_psi,isigma,n,x,beta,theta,sigma, &
             maxit,tol,rs,nit,wrk,ifail)

!         Display results
          WRITE (nout,99999) isigma, sigsav, thesav, tol, sigma, theta
       END DO D_LP

99999  FORMAT (1X,I3,3X,2F8.4,F7.4,1X,2F8.4)
    END PROGRAM g07dcfe
