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

NAG AD Library Introduction
Example description
    Program g02aa_a1t1w_fe

!     G02AA_A1T1W_F Example Program Text
!     Mark 28.3 Release. NAG Copyright 2022.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: g02aa_a1t1w_f, nagad_a1t1w_get_derivative,      &
                               nagad_a1t1w_inc_derivative,                     &
                               nagad_a1t1w_ir_create => x10za_a1t1w_f,         &
                               nagad_a1t1w_ir_interpret_adjoint,               &
                               nagad_a1t1w_ir_register_variable,               &
                               nagad_a1t1w_ir_remove, nagad_a1t1w_w_rtype,     &
                               nagad_t1w_w_rtype, x10aa_a1t1w_f,               &
                               x10ab_a1t1w_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_a1t1w_w_rtype)       :: errtol, nrmgrd
      Type (nagad_t1w_w_rtype)         :: t_t
      Real (Kind=nag_wp), Save         :: da = 0.0_nag_wp
      Real (Kind=nag_wp), Save         :: dxdg
      Integer                          :: feval, i, ifail, iter, j, ldg, ldx,  &
                                          maxit, maxits, n
!     .. Local Arrays ..
      Type (nagad_a1t1w_w_rtype), Allocatable :: g(:,:), g_in(:,:), x(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'G02AA_A1T1W_F Example Program Results'
      Write (nout,*)
      Flush (nout)

      ifail = 0
      Call x10aa_a1t1w_f(ad_handle,ifail)
      Call nagad_a1t1w_ir_create

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

!     Read in the problem size
      Read (nin,*) n

      ldg = n
      ldx = n
      Allocate (g(ldg,n),x(ldx,n),g_in(ldg,n))

!     Read in the matrix G
      g_in(1:n,1:n) = 0.0_nag_wp
      Read (nin,*)(g_in(i,1:n)%value%value,i=1,n)

!     Use the defaults for ERRTOL, MAXITS and MAXIT
      errtol = 0.0E0_nag_wp
      maxits = 0
      maxit = 0

      g_in(1:n,1:n)%value%tangent = 1.0_nag_wp
      Call nagad_a1t1w_ir_register_variable(g_in)
      g = g_in
!     Calculate nearest correlation matrix
      ifail = 0
      Call g02aa_a1t1w_f(ad_handle,g,ldg,n,errtol,maxits,maxit,x,ldx,iter,     &
        feval,nrmgrd,ifail)

      t_t = 1.0_nag_wp
      Call nagad_a1t1w_inc_derivative(x(1:n,1:n),t_t)

      Call nagad_a1t1w_ir_interpret_adjoint(ifail)

!     Display results
      ifail = 0
      Call x04caf('General',' ',n,n,x%value%value,ldx,                         &
        'Nearest Correlation Matrix',ifail)
      Write (nout,*)
      Write (nout,99999) ' Number of Newton steps taken:', iter
      Write (nout,99998) ' Number of function evaluations:', feval

      Write (nout,*)
      Write (nout,*) ' '

      dxdg = 0.0_nag_wp
      Do i = 1, n
        Do j = 1, n
          t_t = nagad_a1t1w_get_derivative(g_in(i,j))
          dxdg = dxdg + t_t%tangent
        End Do
      End Do
      Write (nout,*)
      Write (nout,'(1X,A)') 'Sum of Hessian terms for X w.r.t. G'
      Write (nout,*)
      Write (nout,'(1X,A,E11.2)')                                              &
        'Sum_{i,j,k,l,m,n} d^2 X_{m,n} / dG_{i,j} dG_{k,l}: ', dxdg

      Call nagad_a1t1w_ir_remove
      Call x10ab_a1t1w_f(ad_handle,ifail)
99999 Format (1X,A,I11)
99998 Format (1X,A,I9)
    End Program g02aa_a1t1w_fe