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

NAG AD Library Introduction
Example description
    Program f11xe_t1w_fe

!     F11XE_T1W_F Example Program Text

!     Mark 30.0 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f11xe_t1w_f, nagad_t1w_set_derivative,          &
                               nagad_t1w_w_rtype, x10aa_t1w_f, x10ab_t1w_f,    &
                               Assignment (=)
      Use nag_library, Only: nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Integer                          :: i, ifail, n, nnz
      Character (1)                    :: check
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: a(:), x(:), y(:)
      Real (Kind=nag_wp), Allocatable  :: ar(:), dydx(:,:), yr(:)
      Integer, Allocatable             :: icol(:), irow(:)
!     .. Executable Statements ..
      Write (nout,*) 'F11XE_T1W_F Example Program Results'
!     Skip heading in data file
      Read (nin,*)

!     Read order of matrix and number of nonzero entries
      Read (nin,*) n
      Read (nin,*) nnz

      Allocate (a(nnz),x(n),y(n),icol(nnz),irow(nnz))
      Allocate (ar(nnz),yr(n),dydx(n,n))

!     Read the matrix A
      Do i = 1, nnz
        Read (nin,*) ar(i), irow(i), icol(i)
      End Do
      a(1:nnz) = ar(1:nnz)

!     Read the vector x
      Read (nin,*) yr(1:n)

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

      Do i = 1, n
        x = yr
        Call nagad_t1w_set_derivative(x(i),1.0_nag_wp)

!       Calculate matrix-vector product
        check = 'C'
        ifail = 0
        Call f11xe_t1w_f(ad_handle,n,nnz,a,irow,icol,check,x,y,ifail)

        x(i)%tangent = 0.0_nag_wp
        dydx(1:n,i) = y(1:n)%tangent
      End Do

!     Output results
      Write (nout,*) ' Matrix-vector product'
      Write (nout,99999)(y(i)%value,i=1,n)

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

      Write (nout,*)
      Write (nout,*) ' Derivatives of product Y w.r.t. X (A)'
      Write (nout,*)
      Call x04caf('General',' ',n,n,dydx,n,'       dy(i)/dx(j)',ifail)

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

99999 Format (1X,1P,E16.4)
    End Program f11xe_t1w_fe