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

NAG AD Library Introduction
Example description
    Program e01aa_a1w_fe

!     E01AA_A1W_F Example Program Text

!     Mark 30.1 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: e01aa_a1w_f, nagad_a1w_get_derivative,          &
                               nagad_a1w_inc_derivative,                       &
                               nagad_a1w_ir_interpret_adjoint_sparse,          &
                               nagad_a1w_ir_register_variable,                 &
                               nagad_a1w_ir_remove, nagad_a1w_w_rtype,         &
                               x10aa_a1w_f, x10ab_a1w_f, x10za_a1w_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_a1w_w_rtype)         :: x
      Real (Kind=nag_wp)               :: xr
      Integer                          :: i, ifail, j, n, n1, n2
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: a(:), b(:), c(:)
      Real (Kind=nag_wp), Allocatable  :: ar(:), br(:)
!     .. Executable Statements ..
      Write (nout,*) 'E01AA_A1W_F Example Program Results'

!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n, xr
      x = xr

      n1 = n + 1
      n2 = n*(n+1)/2
      Allocate (a(n1),b(n1),c(n2))
      Allocate (ar(n1),br(n1))

      Read (nin,*)(ar(i),i=1,n1)
      Read (nin,*)(br(i),i=1,n1)

      a(1:n1) = ar(1:n1)
      b(1:n1) = br(1:n1)

!     Create AD tape
      Call x10za_a1w_f

!     Create AD configuration data object and set computational mode
      ifail = 0
      Call x10aa_a1w_f(ad_handle,ifail)
      ifail = 0

!     Register variables to differentiate w.r.t.
      Call nagad_a1w_ir_register_variable(a)

!     Call AD routine
      ifail = 0
      Call e01aa_a1w_f(ad_handle,a,b,c,n,x,ifail)

      Write (nout,*)
      Write (nout,99999) 'Interpolation point = ', x%value
      Write (nout,*)
      Write (nout,99999) 'Function value at interpolation point = ',           &
        c(n2)%value

!     Setup evaluation of derivatives via adjoints
      Call nagad_a1w_inc_derivative(c(n2),1.0_nag_wp)

      ifail = 0
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

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

!     Get derivatives
      ar(1:n1) = nagad_a1w_get_derivative(a)

      Write (nout,*)
      Write (nout,*) ' Derivatives of fitted value w.r.t. x values:'
      Write (nout,*) '  j         d/dx(j)'
      Do j = 1, n1
        Write (nout,99998) j, ar(j)
      End Do

!     Remove computational data object and tape
      Call x10ab_a1w_f(ad_handle,ifail)
      Call nagad_a1w_ir_remove

99999 Format (1X,A,F12.5)
99998 Format (1X,I3,5X,E12.5)
    End Program e01aa_a1w_fe