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

NAG AD Library Introduction
Example description
    Program f08ah_a1w_fe

!     F08AH_A1W_F Example Program Text
!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f08ah_a1w_f, nagad_a1w_get_derivative,          &
                               nagad_a1w_inc_derivative,                       &
                               nagad_a1w_ir_create => x10za_a1w_f,             &
                               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
      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
      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  :: dlda(:,:), l(:,:)
!     .. Executable Statements ..

      Write (nout,*) 'F08AH_A1W_F Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n
      lda = m
      lwork = 64*n
      Allocate (a(lda,n),a_in(lda,n),l(m,m),dlda(m,n),tau(n),work(lwork))

!     Read A from data file

      Read (nin,*)(a_in(i,1:n)%value,i=1,m)

      ifail = 0
      Call x10aa_a1w_f(ad_handle,ifail)

      Call nagad_a1w_ir_create
      Call nagad_a1w_ir_register_variable(a_in)
      a = a_in

!     Compute the LQ factorization of A
!     The NAG name equivalent of dgelqf_a1w is f08ah_a1w_f

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

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

!     Print L

!     ifail: behaviour on error exit
!            =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call x04caf('Lower','Non',m,m,l,m,'L from LQ factorization of A',ifail)

      Write (nout,*)
      Write (nout,*) 'Derivatives of diagonal of L w.r.t. first row of A'
      ifail = 0
      Call x04caf('General',' ',m,n,dlda,m,'dL(i,i)/dA(1,1:n)',ifail)

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