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

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

    Module f01ef_t1w_fe_mod

!     F01EF_T1W_F Example Program Module:
!            Parameters and User-defined Routines

!     nin:       the input channel number
!     nout:      the output channel number

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: cos, nagad_t1w_w_rtype
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Integer, Parameter, Public       :: nin = 5, nout = 6
    Contains
      Subroutine f(ad_handle,iflag,n,x,fx,iuser,ruser)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Integer, Intent (Inout)        :: iflag
        Integer, Intent (In)           :: n
!       .. Array Arguments ..
        Type (nagad_t1w_w_rtype), Intent (Out) :: fx(n)
        Type (nagad_t1w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_t1w_w_rtype), Intent (In) :: x(n)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Executable Statements ..

        fx(1:n) = cos(x(1:n))

        Return
      End Subroutine f
    End Module f01ef_t1w_fe_mod
    Program f01ef_t1w_fe

!     F01EF_T1W_F Example Main Program

!     .. Use Statements ..
      Use f01ef_t1w_fe_mod, Only: f, nin, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f01ef_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
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Integer                          :: i, ierr, ifail, iflag, j, lda, n
      Character (1)                    :: uplo
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: a(:,:), a_in(:,:)
      Type (nagad_t1w_w_rtype)         :: ruser(1)
      Real (Kind=nag_wp), Allocatable  :: a_r(:,:)
      Integer                          :: iuser(1)
!     .. Executable Statements ..
      Write (nout,*) 'F01EF_T1W_F Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      Read (nin,*) uplo

      lda = n
      Allocate (a(lda,n),a_in(lda,n),a_r(lda,n))

!     Read A from data file
      a_r = 0.0_nag_wp
      If (uplo=='U' .Or. uplo=='u') Then
        Read (nin,*)(a_r(i,i:n),i=1,n)
      Else
        Read (nin,*)(a_r(i,1:i),i=1,n)
      End If
      a_in = a_r

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

!     Register variables to differentiate w.r.t.
      Do i = 1, n

        Call nagad_t1w_set_derivative(a_in(i,i),1.0_nag_wp)
        a = a_in

!       Find f( A )
        ifail = 0
        Call f01ef_t1w_f(ad_handle,uplo,n,a,lda,f,iuser,ruser,iflag,ifail)

!       Print solution
        If (i==1) Then
          a_r = a
          ierr = 0
          Call x04caf(uplo,'N',n,n,a_r,lda,'Symmetric f(A)',ierr)
        End If
        Do j = 1, n
          a_r(j,i) = a(j,j)%tangent
        End Do
        a_in(i,i)%tangent = 0.0_nag_wp
      End Do
      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: First order tangents'
      Write (nout,*) ' Computational mode    : algorithmic'

      Write (nout,*)
      Write (nout,*) ' Derivatives of diagonal of f(A) w.r.t. diagonal of A'
      Write (nout,*)

      Call x04caf('General',' ',n,n,a_r,n,'       d(fA(i,i))/da(j,j)',ifail)

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

    End Program f01ef_t1w_fe