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

NAG AD Library Introduction
Example description
    Program f03bf_t1w_fe

!     F03BF_T1W_F Example Program Text

!     Mark 30.3 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f03bf_t1w_f, f07fd_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
      Character (1), Parameter         :: uplo = 'l'
!     .. 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(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'F03BF_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))

      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, i

          Call nagad_t1w_set_derivative(a_in(i,j),1.0_nag_wp)

          a = a_in
!         Factorize A
!         The NAG name equivalent of dpotrf is f07fdf
          ifail = 0
          Call f07fd_t1w_f(ad_handle,uplo,n,a,lda,ifail)

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

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

          If (i==1 .And. j==1) Then
            Write (nout,*)
            Write (nout,99999) d%value, id
            Write (nout,*)
            Write (nout,99998) d%value*2.0_nag_wp**id
99999       Format (1X,'D = ',F13.5,' ID = ',I12)
99998       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('L','N',n,n,ar,n,'       d(d)/da',ifail)

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

    End Program f03bf_t1w_fe