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

NAG AD Library Introduction
Example description
    Program e01aa_t1w_fe

!     E01AA_T1W_F Example Program Text

!     Mark 30.3 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: e01aa_t1w_f, nagad_t1w_set_derivative,          &
                               nagad_t1w_w_rtype, x10aa_t1w_f, x10ab_t1w_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)         :: x
      Real (Kind=nag_wp)               :: fx, xr
      Integer                          :: i, ifail, j, n, n1, n2
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: a(:), b(:), c(:)
      Real (Kind=nag_wp), Allocatable  :: ar(:), br(:), dx(:)
!     .. Executable Statements ..
      Write (nout,*) 'E01AA_T1W_F Example Program Results'

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

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

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

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

      Do i = 1, n1

        x = xr
        a(1:n1) = ar(1:n1)
        b(1:n1) = br(1:n1)
        c(1:n2) = 0.0_nag_wp

        Call nagad_t1w_set_derivative(a(i),1.0_nag_wp)

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

        If (i==1) Then
          fx = c(n2)%value
        End If
        dx(i) = c(n2)%tangent

      End Do

      Write (nout,*)
      Write (nout,99999) 'Interpolation point = ', xr
      Write (nout,*)
      Write (nout,99999) 'Function value at interpolation point = ', fx

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

      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, dx(j)
      End Do

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

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