    PROGRAM f08avfe

!      F08AVF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp, x04dbf, zgelqf, ztrsm, zunmlq
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       COMPLEX (KIND=nag_wp), PARAMETER :: one = (1.0_nag_wp,0.0_nag_wp)
       COMPLEX (KIND=nag_wp), PARAMETER :: zero = (0.0_nag_wp,0.0_nag_wp)
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, info, lda, ldb, lwork, m,  &
                                          n, nrhs
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:,:), tau(:), work(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08AVF 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 zgelqf is f08avf
       CALL zgelqf(m,n,a,lda,tau,work,lwork,info)

!      Solve L*Y = B, storing the result in B
!      The NAG name equivalent of ztrsm is f06zjf
       CALL ztrsm('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**H)*B in B
!      The NAG name equivalent of zunmlq is f08axf
       CALL zunmlq('Left','Conjugate 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 x04dbf('General',' ',n,nrhs,b,ldb,'Bracketed','F7.4', &
          'Minimum-norm solution(s)','Integer',rlabs,'Integer',clabs,80,0, &
          ifail)

    END PROGRAM f08avfe
