!   D01FBF Example Program Text
!   Mark 26.1 Release. NAG Copyright 2016.

    Module d01fbfe_mod

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

!     .. Use Statements ..
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: fun
!     .. Parameters ..
      Integer, Parameter, Public       :: ndim = 4, nout = 6
    Contains
      Function fun(ndim,x)

!       .. Function Return Value ..
        Real (Kind=nag_wp)             :: fun
!       .. Scalar Arguments ..
        Integer, Intent (In)           :: ndim
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (In) :: x(ndim)
!       .. Intrinsic Procedures ..
        Intrinsic                      :: exp
!       .. Executable Statements ..
        fun = (x(1)*x(2)*x(3))**6/(x(4)+2.0E0_nag_wp)**8*                      &
          exp(-2.0E0_nag_wp*x(2)-0.5E0_nag_wp*x(3)*x(3))

        Return

      End Function fun
    End Module d01fbfe_mod
    Program d01fbfe

!     D01FBF Example Main Program

!     .. Use Statements ..
      Use d01fbfe_mod, Only: fun, ndim, nout
      Use nag_library, Only: d01fbf, d01tbf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: a, ans, b
      Integer                          :: i, ifail, j, lwa
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: abscis(:), weight(:)
      Integer                          :: nptvec(ndim)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: sum
!     .. Executable Statements ..
      Write (nout,*) 'D01FBF Example Program Results'

      nptvec(1:ndim) = (/4,4,4,4/)
      lwa = sum(nptvec(1:ndim))
      Allocate (abscis(lwa),weight(lwa))

      j = 1

      Do i = 1, 4

        ifail = 0
        Select Case (i)
        Case (1)
          a = 1.0E0_nag_wp
          b = 2.0E0_nag_wp
          Call d01tbf(0,a,b,nptvec(i),weight(j),abscis(j),ifail)
        Case (2)
          a = 0.0E0_nag_wp
          b = 2.0E0_nag_wp
          Call d01tbf(-3,a,b,nptvec(i),weight(j),abscis(j),ifail)
        Case (3)
          a = 0.0E0_nag_wp
          b = 0.5E0_nag_wp
          Call d01tbf(-4,a,b,nptvec(i),weight(j),abscis(j),ifail)
        Case (4)
          a = 1.0E0_nag_wp
          b = 2.0E0_nag_wp
          Call d01tbf(-5,a,b,nptvec(i),weight(j),abscis(j),ifail)
        End Select

        j = j + nptvec(i)
      End Do

      ifail = 0
      ans = d01fbf(ndim,nptvec,lwa,weight,abscis,fun,ifail)

      Write (nout,*)
      Write (nout,99999) 'Answer = ', ans

99999 Format (1X,A,F10.5)
    End Program d01fbfe