    PROGRAM g02abfe

!      G02ABF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dsyev, g02abf, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: alpha, errtol, nrmgrd
       INTEGER                         :: feval, i, ifail, iter, ldg, ldx,     &
                                          lwork, maxit, maxits, n
       CHARACTER (1)                   :: opt
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: eig(:), g(:,:), w(:), work(:), x(:,:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02ABF 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

       ldg = n
       ldx = n
       lwork = 66*n
       ALLOCATE (g(ldg,n),w(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 vector W
       READ (nin,*) w(1:n)

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

!      Calculate nearest correlation matrix
       ifail = 0

       CALL g02abf(g,ldg,n,opt,alpha,w,errtol,maxits,maxit,x,ldx,iter,feval, &
          nrmgrd,ifail)

!      Display results
       ifail = 0
       CALL x04caf('General',' ',n,n,x,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

       ifail = 0
!      The NAG name equivalent of dsyev is f08faf
       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,I11)
99998  FORMAT (1X,A,I9)
99997  FORMAT (1X,A,F37.3)

    END PROGRAM g02abfe
