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

NAG AD Library Introduction
Example description
!   D01UA_T1W_F Example Program Text
!   Mark 28.7 Release. NAG Copyright 2022.

    Module d01ua_t1w_fe_mod

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: exp, log, nagad_t1w_w_rtype, Operator (/),      &
                               Operator (-), Operator (*), Operator (+)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Integer, Parameter, Public       :: nout = 6
    Contains
      Subroutine f(ad_handle,x,nx,fv,iflag,iuser,ruser)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Integer, Intent (Inout)        :: iflag
        Integer, Intent (In)           :: nx
!       .. Array Arguments ..
        Type (nagad_t1w_w_rtype), Intent (Out) :: fv(nx)
        Type (nagad_t1w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_t1w_w_rtype), Intent (In) :: x(nx)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Executable Statements ..
        Select Case (iuser(1))
        Case (1)
          fv = 4.0E0_nag_wp/(1.0E0_nag_wp+ruser(1)*x*x)
        Case (2)
          fv = 1.0E0_nag_wp/(x*x*log(ruser(1)*x))
        Case (3)
          fv = exp(-ruser(1)*x)/x
        Case (4)
          fv = ruser(1)/x
        Case (5)
          fv = exp(-3.0E0_nag_wp*ruser(1)*x*x-4.0E0_nag_wp*x-1.0E0_nag_wp)
        Case (6)
          fv = exp(2.0E0_nag_wp*ruser(1)*x+2.0E0_nag_wp)
        Case Default
          iflag = -1
        End Select
        Return
      End Subroutine f
    End Module d01ua_t1w_fe_mod

    Program d01ua_t1w_fe
!     D01UA_T1W_F Example Main Program

!     .. Use Statements ..
      Use d01ua_t1w_fe_mod, Only: f, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d01ua_t1w_f, nagad_t1w_w_rtype, x10aa_t1w_f,    &
                               x10ab_t1w_f, Assignment (=)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (nagad_t1w_w_rtype)         :: a, b, dinest
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: dr
      Integer                          :: funid, ifail, key, n
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype)         :: ruser(1)
      Integer                          :: iuser(1)
!     .. Executable Statements ..

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

      n = 64
      ruser(1) = 1.0_nag_wp

!     Create AD configuration data object
      ifail = 0
      Call x10aa_t1w_f(ad_handle,ifail)

      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: First order tangents'
      Write (nout,*) ' Computational mode    : algorithmic'

cases: Do funid = 1, 6
        Write (nout,*)
        Select Case (funid)
        Case (1)
          Write (nout,*) 'Gauss-Legendre example'
          a = 0.0_nag_wp
          b = 1.0_nag_wp
          key = 0
        Case (2)
          Write (nout,*) 'Rational Gauss example'
          a = 2.0_nag_wp
          b = 0.0_nag_wp
          key = -5
        Case (3)
          Write (nout,*) 'Gauss-Laguerre example (adjusted weights)'
          a = 2.0_nag_wp
          b = 1.0_nag_wp
          key = -3
        Case (4)
          Write (nout,*) 'Gauss-Laguerre example (normal weights)'
          a = 2.0_nag_wp
          b = 1.0_nag_wp
          key = 3
        Case (5)
          Write (nout,*) 'Gauss-Hermite example (adjusted weights)'
          a = -1.0_nag_wp
          b = 3.0_nag_wp
          key = -4
        Case (6)
          Write (nout,*) 'Gauss-Hermite example (normal weights)'
          a = -1.0_nag_wp
          b = 3.0_nag_wp
          key = 4
        End Select
        iuser(1) = funid

!       Call the AD routine
        ifail = 0
        ruser(1)%tangent = 1.0_nag_wp
        Call d01ua_t1w_f(ad_handle,key,a,b,n,f,dinest,iuser,ruser,ifail)
        ruser(1)%tangent = 0.0_nag_wp

!       Get derivatives
        dr = dinest%tangent

        Write (nout,*)
        Write (nout,99999) 'dinest   =', dinest%value, 'd/druser = ', dr
99999   Format (1X,A,1X,F10.5,3X,A,1X,F10.5)

      End Do cases

!     Remove computational data object
      ifail = 0
      Call x10ab_t1w_f(ad_handle,ifail)

    End Program d01ua_t1w_fe