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

NAG AD Library Introduction
Example description
    Program f03ba_t1w_fe

!     F03BA_T1W_F Example Program Text

!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f03ba_t1w_f, f07ad_t1w_f,                       &
                               nagad_t1w_set_derivative, nagad_t1w_w_rtype,    &
                               x10aa_t1w_f, x10ab_t1w_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_t1w_w_rtype)         :: d
      Integer                          :: i, id, ifail, j, lda, n
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: a(:,:), a_in(:,:)
      Real (Kind=nag_wp), Allocatable  :: ar(:,:)
      Integer, Allocatable             :: ipiv(:)
!     .. Executable Statements ..
      Write (nout,*) 'F03BA_T1W_F Example Program Results'

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

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

      Read (nin,*)(ar(i,1:n),i=1,n)
      a_in = ar

!     Create AD configuration data object
      ifail = 0
      Call x10aa_t1w_f(ad_handle,ifail)

      Do i = 1, n
        Do j = 1, n
          Call nagad_t1w_set_derivative(a_in(i,j),1.0_nag_wp)
          a = a_in

!         LU Factorize A
!         The NAG name equivalent of dgetrf is f07adf
          ifail = 0
          Call f07ad_t1w_f(ad_handle,n,n,a,lda,ipiv,ifail)

          If (i==1 .And. j==1) Then
            Write (nout,*)
            Flush (nout)
            ar = a
            ifail = 0
            Call x04caf('G','N',n,n,ar,lda,'Array A after factorization',      &
              ifail)

            Write (nout,*)
            Write (nout,*) 'Pivots'
            Write (nout,99999) ipiv(1:n)
            Write (nout,*)
            Flush (nout)
99999       Format (1X,8(1X,I13))
          End If

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

          If (i==1 .And. j==1) Then
            Write (nout,99998) d%value, id
            Write (nout,*)
            Write (nout,99997) d%value*2.0_nag_wp**id

99998       Format (1X,'D = ',F13.5,' ID = ',I12)
99997       Format (1X,'Value of determinant = ',E13.5)
          End If
          ar(i,j) = d%tangent
          a_in(i,j)%tangent = 0.0_nag_wp
        End Do
      End Do

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

      Write (nout,*)
      Write (nout,*) ' Derivatives of scaled determinant of A w.r.t. A'
      Write (nout,*)

      Call x04caf('General',' ',n,n,ar,n,'       d(d)/da',ifail)

!     Remove computational data object
      Call x10ab_t1w_f(ad_handle,ifail)

    End Program f03ba_t1w_fe