Program f08ahfe

!     F08AHF Example Program Text

!     Mark 25 Release. NAG Copyright 2014.

!     .. Use Statements ..
      Use nag_library, Only: dgelqf, dormlq, dtrsm, nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: one = 1.0E0_nag_wp
      Real (Kind=nag_wp), Parameter    :: zero = 0.0E0_nag_wp
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Integer                          :: i, ifail, info, lda, ldb, lwork, m,  &
                                          n, nrhs
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), b(:,:), tau(:), work(:)
!     .. Executable Statements ..
      Write (nout,*) 'F08AHF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n, nrhs
      lda = m
      ldb = n
      lwork = 64*n
      Allocate (a(lda,n),b(ldb,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 LQ factorization of A
!     The NAG name equivalent of dgelqf is f08ahf
      Call dgelqf(m,n,a,lda,tau,work,lwork,info)

!     Solve L*Y = B, storing the result in B
!     The NAG name equivalent of dtrsm is f06yjf
      Call dtrsm('Left','Lower','No transpose','Non-Unit',m,nrhs,one,a,lda,b, &
        ldb)

!     Set rows (M+1) to N of B to zero

      If (m<n) b(m+1:n,1:nrhs) = zero

!     Compute minimum-norm solution X = (Q**T)*B in B
!     The NAG name equivalent of dormlq is f08akf
      Call dormlq('Left','Transpose',n,nrhs,m,a,lda,tau,b,ldb,work,lwork,info)

!     Print minimum-norm solution(s)

      Write (nout,*)
      Flush (nout)

!     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,'Minimum-norm solution(s)',ifail)

    End Program f08ahfe