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

NAG AD Library Introduction
Example description
    Program f08ke_a1w_fe

!     F08KE_A1W_F Example Program Text

!     Mark 30.1 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f08ke_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
      Integer                          :: i, ifail, lda, lwork, m, n
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: a(:,:), a_in(:,:), d(:), e(:),  &
                                          taup(:), tauq(:), work(:)
      Real (Kind=nag_wp), Allocatable  :: ar(:,:), dd(:,:), de(:,:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: min
!     .. Executable Statements ..
      Write (nout,*) 'F08KE_A1W_F Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n
      lda = m
      lwork = 64*(m+n)
      Allocate (a(lda,n),d(n),e(n-1),taup(n),tauq(n),work(lwork))
      Allocate (ar(m,n),a_in(m,n),dd(n,m),de(n-1,m))

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

!     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)
      a = a_in

!     Reduce A to bidiagonal form
      ifail = 0
      Call f08ke_a1w_f(ad_handle,m,n,a,lda,d,e,tauq,taup,work,lwork,ifail)

!     Print bidiagonal form
      Write (nout,*)
      Write (nout,*) 'Diagonal'
      Write (nout,99999) d(1:min(m,n))%value
      If (m>=n) Then
        Write (nout,*) 'Superdiagonal'
      Else
        Write (nout,*) 'Subdiagonal'
      End If
      Write (nout,99999) e(1:min(m,n)-1)%value

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

      Write (nout,*)
      Write (nout,*) ' Derivatives of d/e w.r.t. first column of A:'
!     Setup evaluation of derivatives via adjoints
      Do i = 1, n
        Call nagad_a1w_ir_zero_adjoints
        Call nagad_a1w_inc_derivative(d(i),1.0_nag_wp)
        ifail = 0
        Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

!       Get derivatives
        dd(i,1:m) = nagad_a1w_get_derivative(a_in(1:m,1))
      End Do
      Do i = 1, n - 1
        Call nagad_a1w_ir_zero_adjoints
        Call nagad_a1w_inc_derivative(e(i),1.0_nag_wp)
        ifail = 0
        Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

!       Get derivatives
        de(i,1:m) = nagad_a1w_get_derivative(a_in(1:m,1))
      End Do
      ifail = 0
      Call x04caf('General',' ',n,m,dd,n,' dD_i/dA_j1',ifail)
      Write (nout,*)
      ifail = 0
      Call x04caf('General',' ',n-1,m,de,n-1,' dE_i/dA_j1',ifail)

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

99999 Format (1X,8F9.4)
    End Program f08ke_a1w_fe