    PROGRAM g03bafe

!      G03BAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g03baf, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: acc, g
       INTEGER                         :: i, ifail, iter, k, ldfl, ldr, lwk,   &
                                          maxit, nvar
       CHARACTER (1)                   :: stand
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: fl(:,:), flr(:,:), r(:,:), wk(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G03BAF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)

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

!      Read in the problem size
       READ (nin,*) nvar, k, g, stand, acc, maxit

       ldfl = nvar
       ldr = k
       lwk = 2*nvar + k*k + 5*(k-1)
       ALLOCATE (fl(ldfl,k),flr(ldfl,k),r(ldr,k),wk(lwk))

!      Read in loadings
       READ (nin,*) (fl(i,1:k),i=1,nvar)

!      Compute rotations
       ifail = 0
       CALL g03baf(stand,g,nvar,k,fl,ldfl,flr,r,ldr,acc,maxit,iter,wk,ifail)

!      Display results
       ifail = 0
       CALL x04caf('General',' ',nvar,k,flr,ldfl,'Rotated factor loadings', &
          ifail)
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04caf('General',' ',k,k,r,ldr,'Rotated matrix',ifail)

    END PROGRAM g03bafe
