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

NAG AD Library Introduction
Example description
    Program f06pa_a1w_fe

!     F06PA_A1W_F Example Program Text

!     Mark 28.5 Release. NAG Copyright 2022.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f06pa_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_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)         :: alpha, beta
      Real (Kind=nag_wp)               :: alphar, betar
      Integer                          :: i, ifail, lda, m, n
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: a(:,:), b(:), y(:)
      Real (Kind=nag_wp), Allocatable  :: ar(:,:), br(:), da(:), db(:), yr(:)
!     .. Executable Statements ..
      Write (nout,*) 'F06PA_A1W_F Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n
      Read (nin,*) alphar, betar
      alpha = alphar
      beta = betar
      lda = m
      Allocate (a(lda,n),b(m),y(n))
      Allocate (ar(lda,n),br(m),yr(n),da(n),db(n))

!     Read the m by n matrix A from data file
      Read (nin,*)(ar(i,1:n),i=1,m)

!     Read the vector to be multiplied by A
      Read (nin,*) br(1:m)
      yr = 1.0_nag_wp

!     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(alpha)
      Call nagad_a1w_ir_register_variable(beta)

      a = ar
      b = br
      y = yr

      ifail = 0
      Call f06pa_a1w_f(ad_handle,'T',m,n,alpha,a,lda,b,1,beta,y,1,ifail)

      Write (nout,*)
      Write (nout,*) 'Product solution y = beta*y + alpha*A^T*b:'
      yr = y
      Write (nout,99999) yr(1:n)
99999 Format (4X,F11.4)

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

      Write (nout,*)
      Write (nout,*) ' Derivatives of y w.r.t. alpha and beta'
      Write (nout,*)

!     Setup evaluation of derivatives via adjoints
      Do i = 1, n
        Call nagad_a1w_ir_zero_adjoints
        Call nagad_a1w_inc_derivative(y(i),1.0_nag_wp)
        ifail = 0
        Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

!       Get derivatives
        da(i) = nagad_a1w_get_derivative(alpha)
        db(i) = nagad_a1w_get_derivative(beta)
      End Do
      Call x04caf('General',' ',1,n,da,1,'       dy/dalpha',ifail)
      Write (nout,*)
      Call x04caf('General',' ',1,n,db,1,'       dy/dbeta',ifail)

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

    End Program f06pa_a1w_fe