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

NAG FL Interface Introduction
Example description
    Program g02aefe

!     G02AEF Example Program Text

!     Mark 30.3 Release. nAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: dgemm, g02aef, nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: one = 1.0_nag_wp
      Real (Kind=nag_wp), Parameter    :: zero = 0.0_nag_wp
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: errtol, nrmpgd
      Integer                          :: feval, i, ifail, iter, k, lda, ldg,  &
                                          ldx, maxit, n
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), g(:,:), x(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'G02AEF Example Program Results'
      Write (nout,*)
      Flush (nout)

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

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

      lda = n
      ldg = n
      ldx = n
      Allocate (a(lda,n),g(ldg,n),x(ldx,n))

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

!     Use the defaults for ERRTOL and MAXIT
      errtol = zero
      maxit = 0

!     Set k value
      k = 2

!     Calculate the nearest factor loading matrix
      ifail = 0

      Call g02aef(g,ldg,n,k,errtol,maxit,x,ldx,iter,feval,nrmpgd,ifail)

!     Display results
      ifail = 0
      Call x04caf('General',' ',n,k,x,ldx,'Factor Loading Matrix X',ifail)
      Write (nout,*)
      Write (nout,99999) 'Number of steps taken:', iter
      Write (nout,99998) 'Number of function evaluations:', feval

!     Generate Nearest k factor correlation matrix
!     The NAG name equivalent of dgemm is f06yaf
      Call dgemm('N','T',n,n,k,one,x,n,x,n,zero,a,n)
      Do i = 1, n
        a(i,i) = one
      End Do
      Write (nout,*)
      Flush (nout)
      ifail = 0
      Call x04caf('General',' ',n,n,a,lda,'Nearest Correlation Matrix',ifail)

99999 Format (1X,A,I11)
99998 Format (1X,A,I9)

    End Program g02aefe