NAG Library Manual, Mark 30.1
Interfaces:  FL   CL   CPP   AD 

NAG AD Library Introduction
Example description
!   D01TC_P0W_F Example Program Text
!     Mark 30.1 Release. NAG Copyright 2024.

    Module d01tc_p0w_fe_mod

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Integer, Parameter, Public       :: ndim = 4, nout = 6
    Contains
      Subroutine f(ad_handle,ndim,x,ret,iuser,ruser)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Real (Kind=nag_wp), Intent (Out) :: ret
        Integer, Intent (In)           :: ndim
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
        Real (Kind=nag_wp), Intent (In) :: x(ndim)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Intrinsic Procedures ..
        Intrinsic                      :: exp
!       .. Executable Statements ..
        ret = (x(1)*x(2)*x(3))**6/(x(4)+2.0E0_nag_wp)**8*                      &
          exp(-2.0_nag_wp*x(2)-0.5_nag_wp*x(3)*x(3))
        Return

      End Subroutine f
    End Module d01tc_p0w_fe_mod

    Program d01tc_p0w_fe
!     D01TC_P0W_F Example Main Program

!     .. Use Statements ..
      Use d01tc_p0w_fe_mod, Only: f, ndim, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d01fb_p0w_f, d01tc_p0w_f
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: a, ans, b, c, d
      Integer                          :: i, ifail, j, lwa, quadtype
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: abscis(:), da(:), dw(:), weight(:)
      Real (Kind=nag_wp)               :: ruser(1)
      Integer                          :: iuser(1), nptvec(ndim)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: sum
!     .. Executable Statements ..

      Write (nout,*) 'D01TC_P0W_F Example Program Results'

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

      Allocate (abscis(lwa),weight(lwa),da(lwa),dw(lwa))
      j = 1

!     Evaluate weights and abscisae in each dimension
      Do i = 1, 4
        ifail = 0
        Select Case (i)
        Case (1)
          a = 1.0E0_nag_wp
          b = 2.0E0_nag_wp
          c = 0.0_nag_wp
          d = 0.0_nag_wp
          quadtype = 0
        Case (2)
          a = 0.0E0_nag_wp
          b = 2.0E0_nag_wp
          c = 0.0_nag_wp
          d = 0.0_nag_wp
          quadtype = -3
        Case (3)
          a = 0.0E0_nag_wp
          b = 0.5E0_nag_wp
          c = 0.0_nag_wp
          d = 0.0_nag_wp
          quadtype = -4
        Case (4)
          a = 1.0E0_nag_wp
          b = 2.0E0_nag_wp
          c = 0.0_nag_wp
          d = 2.0_nag_wp
          quadtype = -5
        End Select

        Call d01tc_p0w_f(ad_handle,quadtype,a,b,c,d,nptvec(i),weight(j),       &
          abscis(j),ifail)

        j = j + nptvec(i)
      End Do

!     Call the passive routine
      ifail = 0
      Call d01fb_p0w_f(ad_handle,ndim,nptvec,lwa,weight,abscis,f,ans,iuser,    &
        ruser,ifail)

      Write (nout,*)
      Write (nout,99999) ' Solution, x =', ans
99999 Format (1X,A,1X,F12.5)

    End Program d01tc_p0w_fe