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

NAG AD Library Introduction
Example description
    Program f08ae_a1w_fe

!     F08AE_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: f08ae_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               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Integer                          :: i, ifail, info, j, lda, lwork, m, n
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: a(:,:), a_in(:,:), tau(:),      &
                                          work(:)
      Real (Kind=nag_wp), Allocatable  :: ar(:,:), drda(:,:), r(:,:)
!     .. Executable Statements ..

      Write (nout,*) 'F08AE_A1W_F Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n
      lda = m
      lwork = nb*n
      Allocate (a(lda,n),tau(n),work(lwork))
      Allocate (a_in(lda,n),ar(lda,n),drda(n,m),r(n,n))

      ifail = 0
      Call x10aa_a1w_f(ad_handle,ifail)
!     Read A from data file

      Read (nin,*)(ar(i,1:n),i=1,m)
      a_in = ar
      r = 0.0_nag_wp

!     Compute the QR factorization of A
!     The NAG name equivalent of dgeqrf_a1w is f08ae_a1w_f

      Call x10za_a1w_f
      Call nagad_a1w_ir_register_variable(a_in)
      a = a_in

      Call f08ae_a1w_f(ad_handle,m,n,a,lda,tau,work,lwork,info)

!     Compute sensitivities of diagonals of R w.r.t. first column of A
      Do i = 1, n
        Call nagad_a1w_ir_zero_adjoints
        Call nagad_a1w_inc_derivative(a(i,i),1.0_nag_wp)
        ifail = 0
        Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)
        Do j = 1, m
          drda(i,j) = nagad_a1w_get_derivative(a_in(j,1))
        End Do
        Do j = i, n
          r(i,j) = a(i,j)%value
        End Do
      End Do

!     Print R
!     ifail: behaviour on error exit
!            =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call x04caf('Upper','Non',n,n,r,n,'R from QR factorization of A',ifail)

      Write (nout,*)
      Write (nout,*) 'Derivatives of diagonal of R w.r.t. first col of A'
      ifail = 0
      Call x04caf('General',' ',n,m,drda,n,'dR(i,i)/dA(1:m,1)',ifail)

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