    PROGRAM f04yafe

!      F04YAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f04jgf, f04yaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: sigma, tol
       INTEGER                         :: i, ifail, irank, job, lda, lwork, n, p
       LOGICAL                         :: svd
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), cj(:), work(:), y(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F04YAF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n, p
       lda = n
       lwork = 4*p
       ALLOCATE (a(lda,p),cj(p),work(lwork),y(n))
       tol = 5.0E-4_nag_wp
       READ (nin,*) (a(i,1:p),i=1,n)
       READ (nin,*) y(1:n)

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f04jgf(n,p,a,lda,y,tol,svd,sigma,irank,work,lwork,ifail)

       WRITE (nout,*)
       WRITE (nout,99999) 'SIGMA =', sigma, '  Rank =', irank, '  SVD =', svd
       WRITE (nout,*)
       WRITE (nout,*) 'Solution vector'
       WRITE (nout,99998) y(1:p)
       job = 0

       ifail = 0
       CALL f04yaf(job,p,sigma,a,lda,svd,irank,work,cj,work(p+1),ifail)

       WRITE (nout,*)
       WRITE (nout,*) 'Estimated variances of regression coefficients'
       WRITE (nout,99998) cj(1:p)

99999  FORMAT (1X,A,F9.4,A,I3,A,L3)
99998  FORMAT (1X,7F9.4)
    END PROGRAM f04yafe
