Program g02ab_a1w_fe
! G02AB_A1W_F Example Program Text
! Mark 28.3 Release. NAG Copyright 2022.
! .. Use Statements ..
Use iso_c_binding, Only: c_ptr
Use nagad_library, Only: g02ab_a1w_f, nagad_a1w_get_derivative, &
nagad_a1w_inc_derivative, &
nagad_a1w_ir_create => x10za_a1w_f, &
nagad_a1w_ir_interpret_adjoint_sparse, &
nagad_a1w_ir_register_variable, &
nagad_a1w_ir_remove, nagad_a1w_w_rtype, &
x10aa_a1w_f, x10ab_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
Type (nagad_a1w_w_rtype) :: alpha, errtol, nrmgrd
Real (Kind=nag_wp) :: tmp
Integer :: feval, i, ifail, iter, j, ldg, ldx, &
lwork, maxit, maxits, n
Character (1) :: opt
! .. Local Arrays ..
Type (nagad_a1w_w_rtype), Allocatable :: eig(:), g(:,:), g_in(:,:), &
w(:), work(:), x(:,:)
Real (Kind=nag_wp), Allocatable :: dxdg(:,:)
! .. Executable Statements ..
Write (nout,*) 'G02AB_A1W_F Example Program Results'
Write (nout,*)
Flush (nout)
! Skip heading in data file
Read (nin,*)
alpha = 0.0_nag_wp
! Read in the problem size, opt and alpha
Read (nin,*) n, opt, tmp
alpha = tmp
ldg = n
ldx = n
lwork = 66*n
Allocate (g(n,n),g_in(n,n),w(n),x(n,n),eig(n),work(lwork),dxdg(n,n))
x = 0.0_nag_wp
! Read in the matrix G
Read (nin,*)(dxdg(i,1:n),i=1,n)
g_in(1:n,1:n) = dxdg(1:n,1:n)
! Read in the vector W
Read (nin,*) dxdg(1:n,1)
w(1:n) = dxdg(1:n,1)
! Use the defaults for ERRTOL, MAXITS and MAXIT
errtol = 0.0E0_nag_wp
maxits = 0
maxit = 0
Call nagad_a1w_ir_create
ifail = 0
Call x10aa_a1w_f(ad_handle,ifail)
Do j = 1, n
Do i = 1, n
Call nagad_a1w_ir_register_variable(g_in(i,j))
g(i,j) = g_in(i,j)
End Do
End Do
! Calculate nearest correlation matrix
ifail = 0
Call g02ab_a1w_f(ad_handle,g,ldg,n,opt,alpha,w,errtol,maxits,maxit,x, &
ldx,iter,feval,nrmgrd,ifail)
! Display results
dxdg(1:n,1:n) = x(1:n,1:n)%value
ifail = 0
Call x04caf('General',' ',n,n,dxdg,n,'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,*)
Do i = 1, n
Do j = 1, n
Call nagad_a1w_inc_derivative(x(i,j),1.0_nag_wp)
End Do
End Do
Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)
Do i = 1, n
Do j = 1, n
dxdg(i,j) = nagad_a1w_get_derivative(g_in(i,j))
End Do
End Do
Call x10ab_a1w_f(ad_handle,ifail)
Call nagad_a1w_ir_remove
Call x04caf('General',' ',n,n,dxdg,n,' (1,...,1)^T * dx/dg ',ifail)
99999 Format (1X,A,I11)
99998 Format (1X,A,I9)
99997 Format (1X,A,F37.3)
End Program g02ab_a1w_fe