Program g02bufe

!     G02BUF Example Program Text

!     Mark 25 Release. NAG Copyright 2014.

!     .. Use Statements ..
      Use nag_library, Only: dscal, g02buf, nag_wp, x04ccf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: one = 1.0_nag_wp
      Integer, Parameter               :: inc1 = 1, nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: alpha, sw
      Integer                          :: i, ifail, lc, ldx, lwt, m, n
      Character (1)                    :: mean, weight
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: c(:), wmean(:), wt(:), x(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'G02BUF Example Program Results'
      Write (nout,*)
      Flush (nout)

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

!     Read in problem size
      Read (nin,*) mean, weight, m, n

      If (weight=='W' .Or. weight=='w') Then
        lwt = n
      Else
        lwt = 0
      End If
      ldx = n
      lc = (m*m+m)/2
      Allocate (wt(lwt),x(ldx,m),wmean(m),c(lc))

!     Read in data
      If (lwt>0) Then
        Read (nin,*) wt(1:n)
      End If
      Read (nin,*)(x(i,1:m),i=1,n)

!     Calculate sums of squares and cross-products matrix
      ifail = 0
      Call g02buf(mean,weight,n,m,x,ldx,wt,sw,wmean,c,ifail)

!     Display results
      Write (nout,*) 'Means'
      Write (nout,99999) wmean(1:m)
      Write (nout,*)
      Write (nout,*) 'Weights'
      Write (nout,99999) wt(1:n)
      Write (nout,*)
      Flush (nout)
      ifail = 0
      Call x04ccf('Upper','Non-unit',m,c,'Sums of squares and cross-products', &
        ifail)

!     Convert the sums of squares and cross-products to a variance matrix
      If (sw>one) Then
        alpha = one/(sw-one)
!       The NAG name equivalent of dscal is f06edf
        Call dscal(lc,alpha,c,inc1)
        Write (nout,*)
        Flush (nout)
        ifail = 0
        Call x04ccf('Upper','Non-unit',m,c,'Variance matrix',ifail)
      End If

99999 Format (1X,6F14.4)
    End Program g02bufe