!   D01FBF Example Program Text
!   Mark 24 Release. NAG Copyright 2012.

    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
!     .. Parameters ..
      Integer, Parameter                   :: 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 nag_library, Only: d01baw, d01bax, d01bay, d01baz, d01bbf, d01fbf,   &
                             nag_wp
      Use d01fbfe_mod, Only: fun, ndim, nout
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)                   :: a, ans, b
      Integer                              :: i, ifail, itype, 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))

      itype = 1
      j = 1

      Do i = 1, 4

        ifail = 0
        Select Case (i)
        Case (1)
          a = 1.0E0_nag_wp
          b = 2.0E0_nag_wp
          Call d01bbf(d01baz,a,b,itype,nptvec(i),weight(j),abscis(j),ifail)
        Case (2)
          a = 0.0E0_nag_wp
          b = 2.0E0_nag_wp
          Call d01bbf(d01bax,a,b,itype,nptvec(i),weight(j),abscis(j),ifail)
        Case (3)
          a = 0.0E0_nag_wp
          b = 0.5E0_nag_wp
          Call d01bbf(d01baw,a,b,itype,nptvec(i),weight(j),abscis(j),ifail)
        Case (4)
          a = 1.0E0_nag_wp
          b = 2.0E0_nag_wp
          Call d01bbf(d01bay,a,b,itype,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
