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

NAG AD Library Introduction
Example description
    Program f03ba_a1w_fe

!     F03BA_A1W_F Example Program Text

!     Mark 28.6 Release. NAG Copyright 2022.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f03ba_a1w_f, f07ad_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, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_a1w_w_rtype)         :: d
      Integer                          :: i, id, ifail, j, lda, n
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: a(:,:), a_in(:,:)
      Real (Kind=nag_wp), Allocatable  :: ar(:,:)
      Integer, Allocatable             :: ipiv(:)
!     .. Executable Statements ..
      Write (nout,*) 'F03BA_A1W_F Example Program Results'

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

      Read (nin,*) n
      lda = n
      Allocate (a(lda,n),ar(lda,n),a_in(lda,n),ipiv(n))

      Read (nin,*)(ar(i,1:n),i=1,n)
      a_in = ar

!     Create AD tape
      Call x10za_a1w_f

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

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

      a = a_in
!     LU Factorize A
!     The NAG name equivalent of dgetrf is f07adf
      ifail = 0
      Call f07ad_a1w_f(ad_handle,n,n,a,lda,ipiv,ifail)

      Write (nout,*)
      Flush (nout)
      ar = a
      ifail = 0
      Call x04caf('G','N',n,n,ar,lda,'Array A after factorization',ifail)

      Write (nout,*)
      Write (nout,*) 'Pivots'
      Write (nout,99999) ipiv(1:n)
      Write (nout,*)
      Flush (nout)

      ifail = 0
      Call f03ba_a1w_f(ad_handle,n,a,lda,ipiv,d,id,ifail)

      Write (nout,99998) d%value, id
      Write (nout,*)
      Write (nout,99997) d%value*2.0_nag_wp**id

99999 Format (1X,8(1X,I13))
99998 Format (1X,'D = ',F13.5,' ID = ',I12)
99997 Format (1X,'Value of determinant = ',E13.5)

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

      Write (nout,*)
      Write (nout,*) ' Derivatives of scaled determinant of A w.r.t. A'
      Write (nout,*)
!     Setup evaluation of derivatives via adjoints
      Call nagad_a1w_inc_derivative(d,1.0_nag_wp)
      ifail = 0
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

!     Get derivatives
      Do i = 1, n
        Do j = 1, n
          ar(i,j) = nagad_a1w_get_derivative(a_in(i,j))
        End Do
      End Do
      Call x04caf('General',' ',n,n,ar,n,'       d(d)/da',ifail)

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

    End Program f03ba_a1w_fe