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

NAG FL Interface Introduction
Example description
    Program g02asfe

!     G02ASF Example Program Text

!     Mark 30.3 Release. nAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: dsyev, g02asf, nag_wp, x04caf, x04eaf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: alpha, errtol, fnorm
      Integer                          :: i, ifail, its, ldg, ldh, ldx, lwork, &
                                          m, maxit, n
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: eig(:), g(:,:), work(:), x(:,:)
      Integer, Allocatable             :: h(:,:)
!     .. Executable Statements ..

      Write (nout,*) 'G02ASF Example Program Results'
      Write (nout,*)
      Flush (nout)

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

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

      ldg = n
      ldh = n
      ldx = n
      lwork = 66*n
      Allocate (g(ldg,n),h(ldh,n),x(ldx,n),eig(n),work(lwork))

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

!     Read in the matrix H, only the strictly lower triangle need be set
      Read (nin,*)(h(i,1:i-1),i=2,n)

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

!     Calculate nearest correlation matrix
      ifail = 0
      Call g02asf(g,ldg,n,alpha,h,ldh,errtol,maxit,m,x,ldx,its,fnorm,ifail)

      ifail = 0
!     Display the symmetrised input matrix
      Call x04caf('General',' ',n,n,g,ldg,'Symmetrised Input Matrix, G',ifail)
      Write (nout,*)

      ifail = 0
!     Display the lower triangle of H
      Call x04eaf('Lower','Unit',n,n,h,ldh,'Lower Triangle of H',ifail)
      Write (nout,*)

!     Display results
      ifail = 0
      Call x04caf('General',' ',n,n,x,ldx,'Nearest Correlation Matrix, X',     &
        ifail)
      Write (nout,*)
      Write (nout,99999) 'Number of iterations taken:', its
      Write (nout,*)
      Write (nout,99998) 'Norm value:', fnorm
      Write (nout,*)
      Write (nout,99998) 'ALPHA:     ', alpha

      ifail = 0
!     Compute the eigenvalues of X
      Call dsyev('N','U',n,x,ldx,eig,work,lwork,ifail)
      Write (nout,*)
      Flush (nout)
      Call x04caf('General',' ',1,n,eig,1,'Eigenvalues of X',ifail)

99999 Format (1X,A,I5)
99998 Format (1X,A,F26.4)

    End Program g02asfe