    Program f08aafe

!     F08AAF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. 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
