Example description
    Program f08aefe

!     F08AEF Example Program Text

!     Mark 26.2 Release. NAG Copyright 2017.

!     .. Use Statements ..
      Use nag_library, Only: dgeqrf, dnrm2, dormqr, dtrtrs, nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Integer                          :: i, ifail, info, j, lda, ldb, lwork,  &
                                          m, n, nrhs
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), b(:,:), rnorm(:), tau(:),    &
                                          work(:)
!     .. Executable Statements ..
      Write (nout,*) 'F08AEF Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n, nrhs
      lda = m
      ldb = m
      lwork = nb*n
      Allocate (a(lda,n),b(ldb,nrhs),rnorm(nrhs),tau(n),work(lwork))

!     Read A and B from data file

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

!     Compute the QR factorization of A
!     The NAG name equivalent of dgeqrf is f08aef
      Call dgeqrf(m,n,a,lda,tau,work,lwork,info)

!     Compute C = (C1) = (Q**T)*B, storing the result in B
!                 (C2)
!     The NAG name equivalent of dormqr is f08agf
      Call dormqr('Left','Transpose',m,nrhs,n,a,lda,tau,b,ldb,work,lwork,info)

!     Compute least squares solutions by back-substitution in
!     R*X = C1
!     The NAG name equivalent of dtrtrs is f07tef
      Call dtrtrs('Upper','No transpose','Non-Unit',n,nrhs,a,lda,b,ldb,info)

      If (info>0) Then
        Write (nout,*) 'The upper triangular factor, R, of A is singular, '
        Write (nout,*) 'the least squares solution could not be computed'
      Else

!       Print least squares solutions

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call x04caf('General',' ',n,nrhs,b,ldb,'Least squares solution(s)',    &
          ifail)

!       Compute and print estimates of the square roots of the residual
!       sums of squares

!       The NAG name equivalent of dnrm2 is f06ejf
        Do j = 1, nrhs
          rnorm(j) = dnrm2(m-n,b(n+1,j),1)
        End Do

        Write (nout,*)
        Write (nout,*) 'Square root(s) of the residual sum(s) of squares'
        Write (nout,99999) rnorm(1:nrhs)
      End If

99999 Format (5X,1P,7E11.2)
    End Program f08aefe