    PROGRAM g03bdfe

!      G03BDF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g03baf, g03bdf, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: acc, g, power
       INTEGER                         :: i, ifail, iter, ldfp, ldfs, ldphi,   &
                                          ldr, ldro, ldx, lwk, m, maxit, n
       CHARACTER (1)                   :: stand
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: fp(:,:), fs(:,:), phi(:,:), r(:,:),  &
                                          ro(:,:), wk(:), x(:,:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G03BDF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)

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

!      Read in problem size
       READ (nin,*) n, m, g, stand, acc, maxit, power

       ldx = n
       ldro = m
       ldfp = n
       ldfs = n
       ldr = m
       ldphi = m
       lwk = 2*n + m*m + 5*(m-1)
       ALLOCATE (fp(ldx,m),x(ldx,m),ro(ldro,m),wk(lwk),phi(ldphi,m), &
          fs(ldfs,m),r(ldr,m))

!      Read loadings matrix
       READ (nin,*) (fp(i,1:m),i=1,n)

!      Calculate orthogonal rotation
       ifail = 0
       CALL g03baf(stand,g,n,m,fp,ldx,x,ro,ldro,acc,maxit,iter,wk,ifail)

!      Calculate ProMax rotation
       ifail = 0
       CALL g03bdf(stand,n,m,x,ldx,ro,ldro,power,fp,ldfp,r,ldr,phi,ldphi,fs, &
          ldfs,ifail)

!      Display results
       ifail = 0
       CALL x04caf('General',' ',n,m,fp,ldfp,'Factor pattern',ifail)
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04caf('General',' ',m,m,r,ldr,'ProMax rotation',ifail)
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04caf('General',' ',m,m,phi,ldphi,'Inter-factor correlations', &
          ifail)
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04caf('General',' ',n,m,fs,ldfs,'Factor structure',ifail)

    END PROGRAM g03bdfe
