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

NAG AD Library Introduction
Example description
    Program e01bg_a1t1w_fe

!     E01BG_A1T1W_F Example Program Text
!     Mark 29.2 Release. NAG Copyright 2023.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: e01be_a1t1w_f, e01bg_a1t1w_f,                   &
                               nagad_a1t1w_get_derivative,                     &
                               nagad_a1t1w_inc_derivative,                     &
                               nagad_a1t1w_ir_create => x10za_a1t1w_f,         &
                               nagad_a1t1w_ir_interpret_adjoint,               &
                               nagad_a1t1w_ir_register_variable,               &
                               nagad_a1t1w_ir_remove, nagad_a1t1w_w_rtype,     &
                               nagad_t1w_w_rtype, x10aa_a1t1w_f,               &
                               x10ab_a1t1w_f, Assignment (=)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_t1w_w_rtype)         :: t_t
      Real (Kind=nag_wp)               :: dx, fr, xr
      Integer                          :: ifail, m, n, r
!     .. Local Arrays ..
      Type (nagad_a1t1w_w_rtype), Allocatable :: d(:), f(:), pd(:), pf(:),     &
                                          px(:), x(:)
!     .. Executable Statements ..

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

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

      Read (nin,*) n
      m = 1
      Allocate (d(n),f(n),x(n),px(m),pf(m),pd(m))

!     Create AD tape
      Call nagad_a1t1w_ir_create

!     Initialize x and f arrays using data
      Do r = 1, n
        Read (nin,*) xr, fr
        x(r) = xr
        f(r) = fr
      End Do

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

!     Register variables to differentiate w.r.t.

!     Call the AD routine
      ifail = 0
      Call e01be_a1t1w_f(ad_handle,n,x,f,d,ifail)

!     Evaluate interpolant and derivatives at a mid-point
      px(1) = 8.4_nag_wp
      px(1)%value%tangent = 1.0_nag_wp
      Call nagad_a1t1w_ir_register_variable(px)
      Call e01bg_a1t1w_f(ad_handle,n,x,f,d,m,px,pf,pd,ifail)

      Write (nout,*)
      Write (nout,*)                                                           &
        ' Derivatives calculated: Second order, adjoints of tangents'
      Write (nout,*) ' Computational mode    : algorithmic'


!     Get derivative of interpolated values w.r.t interpolation point
      t_t = 1.0_nag_wp
      Call nagad_a1t1w_inc_derivative(pf(1),t_t)
      Call nagad_a1t1w_inc_derivative(pd(1),t_t)
      Call nagad_a1t1w_ir_interpret_adjoint(ifail)

      t_t = nagad_a1t1w_get_derivative(px(1))
      dx = t_t%tangent

      Write (nout,*)
      Write (nout,99999) 'Interpolated value at x = ', px(1)%value%value,      &
        ' is: ', pf(1)%value%value
99999 Format (1X,A,F7.3,A,E12.5)
      Write (nout,*)
      Write (nout,*) ' Summed Hessian of interpolated values w.r.t. x:'
      Write (nout,*)
      Write (nout,99999) '   d^2(px + pd)/dpx = ', dx

!     Remove computational data object and tape
      ifail = 0
      Call x10ab_a1t1w_f(ad_handle,ifail)
      Call nagad_a1t1w_ir_remove

    End Program e01bg_a1t1w_fe