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

NAG AD Library Introduction
Example description
    Program g02ab_t1w_fe

!     G02AB_T1W_F Example Program Text

!     Mark 30.1 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: g02ab_t1w_f, 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)         :: alpha, errtol, nrmgrd
      Real (Kind=nag_wp)               :: dx
      Integer                          :: feval, i, ifail, iter, j, k, l, ldg, &
                                          ldx, lwork, maxit, maxits, n
      Character (1)                    :: opt
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: eig(:), g(:,:), g_in(:,:),      &
                                          w(:), work(:), w_in(:), x(:,:)
      Real (Kind=nag_wp), Allocatable  :: dxdg(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'G02AB_T1W_F Example Program Results'
      Write (nout,*)
      Flush (nout)

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

!     Read in the problem size, opt and alpha
      Read (nin,*) n, opt, alpha%value
      alpha%tangent = 0.0_nag_wp
      ldg = n
      ldx = n
      lwork = 66*n
      Allocate (g(n,n),g_in(n,n),w(n),w_in(n),x(n,n),eig(n),work(lwork),       &
        dxdg(n,n))

!     Initialize arrays
      g_in(1:ldg,1:n) = 0.0_nag_wp
      x(1:ldx,1:n) = 0.0_nag_wp
      w_in(1:n) = 0.0_nag_wp

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

!     Read in the vector W
      Read (nin,*) w_in(1:n)%value

      ifail = 0
      Call x10aa_t1w_f(ad_handle,ifail)
!     Use the defaults for ERRTOL, MAXITS and MAXIT
      errtol = 0.0E0_nag_wp
      maxits = 0
      maxit = 0


outer: Do i = 1, n
        Do j = 1, n
          g = g_in
          w = w_in
          g(i,j)%tangent = 1.0_nag_wp

!         Calculate nearest correlation matrix
          ifail = 0
          Call g02ab_t1w_f(ad_handle,g,ldg,n,opt,alpha,w,errtol,maxits,maxit,  &
            x,ldx,iter,feval,nrmgrd,ifail)

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

            Write (nout,*)
            Write (nout,99997) 'ALPHA: ', alpha%value

            Write (nout,*)
          End If
          dx = 0.0_nag_wp
          Do k = 1, n
            Do l = 1, n
              dx = dx + x(k,l)%tangent
            End Do
          End Do
          dxdg(i,j) = dx

        End Do

      End Do outer

      Call x04caf('General',' ',n,n,dxdg,n,' (1,...,1)^T * dx/dg ',ifail)

      Call x10ab_t1w_f(ad_handle,ifail)
99999 Format (1X,A,I11)
99998 Format (1X,A,I9)
99997 Format (1X,A,F37.3)
    End Program g02ab_t1w_fe