    PROGRAM g03bcfe

!      G03BCF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g03bcf, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: alpha, rss
       INTEGER                         :: i, ifail, ldr, ldx, ldy, lwk, m, n
       CHARACTER (1)                   :: pscale, stand
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: r(:,:), res(:), wk(:), x(:,:),       &
                                          y(:,:), yhat(:,:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G03BCF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)

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

!      Read in problem size
       READ (nin,*) n, m, stand, pscale

       ldx = n
       ldy = n
       ldr = m
       lwk = m*m + 7*m
       ALLOCATE (x(ldx,m),y(ldy,m),yhat(ldy,m),r(ldr,m),res(n),wk(lwk))

!      Read in data
       READ (nin,*) (x(i,1:m),i=1,n)
       READ (nin,*) (y(i,1:m),i=1,n)

!      Calculate rotations
       ifail = 0
       CALL g03bcf(stand,pscale,n,m,x,ldx,y,ldy,yhat,r,ldr,alpha,rss,res,wk, &
          ifail)

!      Display results
       ifail = 0
       CALL x04caf('General',' ',m,m,r,ldr,'Rotation Matrix',ifail)
       IF (pscale=='S' .OR. pscale=='s') THEN
          WRITE (nout,*)
          WRITE (nout,99999) ' Scale factor = ', alpha
       END IF
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04caf('General',' ',n,m,y,ldy,'Target Matrix',ifail)
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04caf('General',' ',n,m,yhat,ldy,'Fitted Matrix',ifail)
       WRITE (nout,*)
       WRITE (nout,99999) 'RSS = ', rss

99999  FORMAT (1X,A,F10.3)
    END PROGRAM g03bcfe
