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

    MODULE g02hbfe_mod

!      G02HBF 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       :: one = 1.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: two = 2.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: zero = 0.0_nag_wp
       INTEGER, PARAMETER                  :: iset = 1, nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: ucv
       FUNCTION ucv(t)
!         UCV function for Krasker-Welsch weights

!         .. Use Statements ..
          USE nag_library, ONLY : s15abf, x01aaf, x02akf
!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: ucv
!         .. Parameters ..
          REAL (KIND=nag_wp), PARAMETER       :: ucvc = 2.5_nag_wp
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: t
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: pc, pd, q, q2
          INTEGER                             :: ifail
!         .. Intrinsic Functions ..
          INTRINSIC                              exp, log, sqrt
!         .. Executable Statements ..
          ucv = one
          IF (t/=zero) THEN
             q = ucvc/t
             q2 = q*q
             ifail = 0
             pc = s15abf(q,ifail)
             IF (q2<-log(x02akf())) THEN
                pd = exp(-q2/two)/sqrt(x01aaf(zero)*two)
             ELSE
                pd = zero
             END IF
             ucv = (two*pc-one)*(one-q2) + q2 - two*q*pd
          END IF
          RETURN
!dec$ attributes offload : mic :: ucv
       END FUNCTION ucv
    END MODULE g02hbfe_mod
    PROGRAM g02hbfe

!      G02HBF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : g02hbf, x04abf, x04ccf
       USE g02hbfe_mod, ONLY : iset, nag_wp, nin, nout, one, ucv
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: bd, bl, tol
       INTEGER                             :: i, ifail, la, ldx, m, maxit, n,  &
                                              nadv, nit, nitmon
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: a(:), wk(:), x(:,:), z(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02HBF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)

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

!      Read in the problem size
       READ (nin,*) n, m

       ldx = n
       la = (m+1)*m/2
       ALLOCATE (x(ldx,m),a(la),wk(la),z(n))

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

!      Read in initial value of A
       READ (nin,*) a(1:la)

!      Read in control parameters
       READ (nin,*) nitmon, bl, bd, maxit, tol

!      Set the advisory channel to NOUT for monitoring information
       IF (nitmon/=0) THEN
          nadv = nout
          CALL x04abf(iset,nadv)
       END IF

!      Calculate A
       ifail = 0
       CALL g02hbf(ucv,n,m,x,ldx,a,z,bl,bd,tol,maxit,nitmon,nit,wk,ifail)

!      Display results
       WRITE (nout,99999) 'G02HBF required ', nit, ' iterations to converge'
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04ccf('Lower','Non-Unit',m,a,'Matrix A',ifail)
       WRITE (nout,*)
       WRITE (nout,*) 'Vector Z'
       WRITE (nout,99998) (z(i),i=1,n)
       WRITE (nout,*)
       WRITE (nout,*) 'Vector of Krasker-Welsch weights'
       WRITE (nout,99998) (one/z(i),i=1,n)

99999  FORMAT (1X,A,I0,A)
99998  FORMAT (1X,F9.4)
    END PROGRAM g02hbfe
