    PROGRAM g02cefe

!      G02CEF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02cef, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, ldr, ldr2, ldssp, ldssp2,  &
                                          m, n
       CHARACTER (80)                  :: fmt
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: r(:,:), r2(:,:), ssp(:,:),           &
                                          ssp2(:,:), std(:), std2(:), xbar(:), &
                                          xbar2(:)
       INTEGER, ALLOCATABLE            :: korder(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02CEF Example Program Results'
       WRITE (nout,*)

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

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

       ldr = n
       ldr2 = m
       ldssp = n
       ldssp2 = m
       ALLOCATE (r(ldr,n),r2(ldr2,m),ssp(ldssp,n),ssp2(ldssp2,m),std(n), &
          std2(m),xbar(n),xbar2(m),korder(m))

!      Read in the data
       READ (nin,*) xbar(1:n)
       READ (nin,*) std(1:n)
       READ (nin,*) (ssp(i,1:n),i=1,n)
       READ (nin,*) (r(i,1:n),i=1,n)

!      Read in the reordering vector
       READ (nin,*) korder(1:m)

!      Format for displaying vectors
       WRITE (fmt,99999) '(1x,a,', n, '(1x,f10.4))'

!      Display data
       WRITE (nout,fmt) 'Original vector XBAR :   ', xbar(1:n)
       WRITE (nout,*)
       WRITE (nout,fmt) 'Original vector STD  :   ', std(1:n)
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04caf('General',' ',n,n,ssp,ldssp,'Original matrix SSP  :',ifail)
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04caf('General',' ',n,n,r,ldr,'Original matrix R    :',ifail)
       WRITE (nout,*)

!      Calculate summaries on reduced number of variables
       ifail = 0
       CALL g02cef(n,xbar,std,ssp,ldssp,r,ldr,m,korder,xbar2,std2,ssp2,ldssp2, &
          r2,ldr2,ifail)

!      Display results
       WRITE (nout,fmt) 'New vector XBAR2 :   ', xbar2(1:m)
       WRITE (nout,*)
       WRITE (nout,fmt) 'New vector STD2  :   ', std2(1:m)
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04caf('General',' ',m,m,ssp2,ldssp2,'New matrix SSP2  :',ifail)
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04caf('General',' ',m,m,r2,ldr2,'New matrix R2    :',ifail)

99999  FORMAT (A,I0,A)
    END PROGRAM g02cefe
