    PROGRAM g01nafe

!      G01NAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g01naf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: beta, con
       INTEGER                         :: i, ifail, j, l, lda, ldsig, lwk, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), emu(:), rkum(:), rmom(:),    &
                                          sigma(:,:), wk(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G01NAF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size
       READ (nin,*) beta, con
       READ (nin,*) n, l

       ldsig = n
       lda = n
       lwk = 3*n*(n+1)/2 + n
       ALLOCATE (a(lda,n),emu(n),sigma(ldsig,n),rkum(l),rmom(l),wk(lwk))

!      Compute A, EMU, and SIGMA for simple autoregression
       DO i = 1, n
          DO j = i, n
             a(j,i) = 0.0E0_nag_wp
          END DO
       END DO
       DO i = 1, n - 1
          a(i+1,i) = 0.5E0_nag_wp
       END DO
       emu(1) = con*beta
       DO i = 1, n - 1
          emu(i+1) = beta*emu(i)
       END DO
       sigma(1,1) = 1.0E0_nag_wp
       DO i = 2, n
          sigma(i,i) = beta*beta*sigma(i-1,i-1) + 1.0E0_nag_wp
       END DO
       DO i = 1, n
          DO j = i + 1, n
             sigma(j,i) = beta*sigma(j-1,i)
          END DO
       END DO

!      Compute cumulants
       ifail = 0
       CALL g01naf('M','M',n,a,lda,emu,sigma,ldsig,l,rkum,rmom,wk,ifail)

!      Display results
       WRITE (nout,99999) ' N = ', n, ' BETA = ', beta, ' CON = ', con
       WRITE (nout,*)
       WRITE (nout,*) '    Cumulants       Moments'
       WRITE (nout,*)
       WRITE (nout,99998) (i,rkum(i),rmom(i),i=1,l)

99999  FORMAT (A,I3,2(A,F6.3))
99998  FORMAT (I3,E12.4,4X,E12.4)
    END PROGRAM g01nafe
