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

NAG AD Library Introduction
Example description
    Program f03bn_a1w_fe

!     F03BN_A1W_F Example Program Text

!     Mark 30.0 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f03bn_a1w_f, f07ar_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_ir_zero_adjoints &
                               , nagad_a1w_set_derivative, nagad_a1w_w_ctype,  &
                               x10aa_a1w_f, x10ab_a1w_f, x10za_a1w_f,          &
                               Assignment (=)
      Use nag_library, Only: nag_wp, x04daf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: two = 2.0_nag_wp
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_a1w_w_ctype)         :: d
      Integer                          :: i, ifail, j, lda, n
!     .. Local Arrays ..
      Type (nagad_a1w_w_ctype), Allocatable :: a(:,:), a_in(:,:)
      Complex (Kind=nag_wp), Allocatable :: ac(:,:)
      Integer                          :: id(2)
      Integer, Allocatable             :: ipiv(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: cmplx
!     .. Executable Statements ..
      Write (nout,*) 'F03BN_A1W_F Example Program Results'

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

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

      Read (nin,*)(ac(i,1:n),i=1,n)

      a_in = ac

!     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%vreal)
      Call nagad_a1w_ir_register_variable(a_in%vimag)

!     LU Factorize A
!     The NAG name equivalent of zgetrf is f07arf
      a = a_in
      ifail = 0
      Call f07ar_a1w_f(ad_handle,n,n,a,lda,ipiv,ifail)
      Write (nout,*)
      Flush (nout)

      ac = a
      ifail = 0
      Call x04daf('g','n',n,n,ac,lda,'Array A after factorization',ifail)

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

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

      Write (nout,99998) d%vreal%value, d%vimag%value, id
      Write (nout,*)
      Write (nout,99997) two**id(1)*d%vreal%value, two**id(2)*d%vimag%value

99999 Format (1X,8(1X,I13))
99998 Format (1X,'D = (',F13.5,',',F13.5,'), ID = (',I0,', ',I0,')')
99997 Format (1X,'Value of determinant = (',E13.5,',',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%vreal,1.0_nag_wp)
      ifail = 0
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)
      Call nagad_a1w_set_derivative(d%vreal,0.0_nag_wp)

!     Get derivatives
      Do i = 1, n
        Do j = 1, n
          ac(i,j) = cmplx(nagad_a1w_get_derivative(a_in(i,                     &
            j)%vreal),nagad_a1w_get_derivative(a_in(i,j)%vimag),kind=nag_wp)
        End Do
      End Do
      Call x04daf('General',' ',n,n,ac,n,'   d(dreal)/da',ifail)

      Call nagad_a1w_ir_zero_adjoints
      Call nagad_a1w_inc_derivative(d%vimag,1.0_nag_wp)
      ifail = 0
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)
      Do i = 1, n
        Do j = 1, n
          ac(i,j) = cmplx(nagad_a1w_get_derivative(a_in(i,                     &
            j)%vreal),nagad_a1w_get_derivative(a_in(i,j)%vimag),kind=nag_wp)
        End Do
      End Do
      Write (nout,*)
      Call x04daf('General',' ',n,n,ac,n,'   d(dimag)/da',ifail)

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

    End Program f03bn_a1w_fe