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

NAG AD Library Introduction
Example description
    Program f03bf_a1w_fe

!     F03BF_A1W_F Example Program Text

!     Mark 28.3 Release. NAG Copyright 2022.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f03bf_a1w_f, f07fd_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
      Character (1), Parameter         :: uplo = 'l'
!     .. 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(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'F03BF_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))

      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)

      Call nagad_a1w_ir_register_variable(a_in)

      a = a_in
!     Factorize A
      ifail = 0
      Call f07fd_a1w_f(ad_handle,uplo,n,a,lda,ifail)

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

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

      Write (nout,*)
      Write (nout,99999) d%value, id
      Write (nout,*)
      Write (nout,99998) d%value*2.0_nag_wp**id
99999 Format (1X,'D = ',F13.5,' ID = ',I12)
99998 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,*)

      Call nagad_a1w_inc_derivative(d,1.0_nag_wp)
      ifail = 0
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

      Do i = 1, n
        Do j = 1, i
          ar(i,j) = nagad_a1w_get_derivative(a_in(i,j))
        End Do
      End Do
      Call x04caf('L','N',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 f03bf_a1w_fe