    PROGRAM f08aafe

!      F08AAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dgels, dnrm2, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nb = 64, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: rnorm
       INTEGER                         :: i, info, lda, ldb, lwork, m, n, nrhs
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:), work(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08AAF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) m, n
       lda = m
       lwork = n + nb*m
       ALLOCATE (a(lda,n),b(m),work(lwork))

!      Read A and B from data file

       READ (nin,*) (a(i,1:n),i=1,m)
       READ (nin,*) b(1:m)

!      Solve the least squares problem min( norm2(b - Ax) ) for x

       nrhs = 1
       ldb = m

!      The NAG name equivalent of dgels is f08aaf
       CALL dgels('No transpose',m,n,nrhs,a,lda,b,ldb,work,lwork,info)

!      Print solution

       WRITE (nout,*) 'Least squares solution'
       WRITE (nout,99999) b(1:n)

!      Compute and print estimate of the square root of the residual
!      sum of squares

!      The NAG name equivalent of dnrm2 is f06ejf
       rnorm = dnrm2(m-n,b(n+1),1)
       WRITE (nout,*)
       WRITE (nout,*) 'Square root of the residual sum of squares'
       WRITE (nout,99998) rnorm

99999  FORMAT (1X,7F11.4)
99998  FORMAT (3X,1P,E11.2)
    END PROGRAM f08aafe
