    PROGRAM f08bsfe

!      F08BSF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp, x04dbf, zgeqpf, ztrsv, zunmqr
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       COMPLEX (KIND=nag_wp), PARAMETER :: zero = (0.0E0_nag_wp,0.0E0_nag_wp)
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: tol
       INTEGER                         :: i, ifail, info, k, lda, ldb, ldx,    &
                                          lwork, m, n, nrhs
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:,:), tau(:), work(:),  &
                                             x(:,:)
       REAL (KIND=nag_wp), ALLOCATABLE :: rwork(:)
       INTEGER, ALLOCATABLE            :: jpvt(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Intrinsic Functions ..
       INTRINSIC                          abs
!      .. Executable Statements ..
       WRITE (nout,*) 'F08BSF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) m, n, nrhs
       lda = m
       ldb = m
       ldx = m
       lwork = 64*n
       ALLOCATE (a(lda,n),b(ldb,nrhs),tau(n),work(lwork),x(ldx,nrhs), &
          rwork(2*n),jpvt(n))

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

!      Initialize JPVT to be zero so that all columns are free

       jpvt(1:n) = 0

!      Compute the QR factorization of A
!      The NAG name equivalent of zgeqpf is f08bsf
       CALL zgeqpf(m,n,a,lda,jpvt,tau,work,rwork,info)

!      Choose TOL to reflect the relative accuracy of the input data

       tol = 0.01_nag_wp

!      Determine which columns of R to use

LOOP:  DO k = 1, n
          IF (abs(a(k,k))<=tol*abs(a(1,1))) EXIT LOOP
       END DO LOOP

!      Compute C = (Q**H)*B, storing the result in B

       k = k - 1

!      The NAG name equivalent of zunmqr is f08auf
       CALL zunmqr('Left','Conjugate Transpose',m,nrhs,k,a,lda,tau,b,ldb,work, &
          lwork,info)

!      Compute least-squares solution by backsubstitution in R*B = C

       DO i = 1, nrhs

!         The NAG name equivalent of ztrsv is f06sjf
          CALL ztrsv('Upper','No transpose','Non-Unit',k,a,lda,b(1,i),1)

!         Set the unused elements of the I-th solution vector to zero

          b(k+1:n,i) = zero

       END DO

!      Unscramble the least-squares solution stored in B

       DO i = 1, n
          x(jpvt(i),1:nrhs) = b(i,1:nrhs)
       END DO

!      Print least-squares solution

       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,x,ldx,'Bracketed','F7.4', &
          'Least-squares solution','Integer',rlabs,'Integer',clabs,80,0,ifail)

    END PROGRAM f08bsfe
