    PROGRAM f08bvfe

!      F08BVF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dznrm2, nag_wp, x04dbf, zgeqp3, ztrsm, ztzrzf,  &
                               zunmqr, zunmrz
!      .. 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              :: inc1 = 1, nb = 64, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: tol
       INTEGER                         :: i, ifail, info, j, k, lda, ldb,      &
                                          lwork, m, n, nrhs
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:,:), tau(:), work(:)
       REAL (KIND=nag_wp), ALLOCATABLE :: rnorm(:), rwork(:)
       INTEGER, ALLOCATABLE            :: jpvt(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Intrinsic Functions ..
       INTRINSIC                          abs
!      .. Executable Statements ..
       WRITE (nout,*) 'F08BVF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) m, n, nrhs
       lda = m
       ldb = m
       lwork = (n+1)*nb
       ALLOCATE (a(lda,n),b(ldb,nrhs),tau(n),work(lwork),rnorm(n),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 with column pivoting as
!      A = Q*(R11 R12)*(P**T)
!             ( 0  R22)
!      The NAG name equivalent of zgeqp3 is f08btf
       CALL zgeqp3(m,n,a,lda,jpvt,tau,work,lwork,rwork,info)

!      Compute C = (C1) = (Q**H)*B, storing the result in B
!                   (C2)
!      The NAG name equivalent of zunmqr is f08auf
       CALL zunmqr('Left','Conjugate transpose',m,nrhs,n,a,lda,tau,b,ldb,work, &
          lwork,info)

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

       tol = 0.01_nag_wp

!      Determine and print the rank, K, of R relative to TOL

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

       WRITE (nout,*) 'Tolerance used to estimate the rank of A'
       WRITE (nout,99999) tol
       WRITE (nout,*) 'Estimated rank of A'
       WRITE (nout,99998) k
       WRITE (nout,*)
       FLUSH (nout)

!      Compute the RZ factorization of the K by K part of R as
!      (R1 R2) = (T 0)*Z
!      The NAG name equivalent of ztzrzf is f08bvf
       CALL ztzrzf(k,n,a,lda,tau,work,lwork,info)

!      Compute least-squares solutions of triangular problems by
!      back substitution in T*Y1 = C1, storing the result in B
!      The NAG name equivalent of ztrsm is f06zjf
       CALL ztrsm('Left','Upper','No transpose','Non-Unit',k,nrhs,one,a,lda,b, &
          ldb)

!      Compute estimates of the square roots of the residual sums of
!      squares (2-norm of each of the columns of C2)

!      The NAG name equivalent of dznrm2 is f06jjf
       DO j = 1, nrhs
          rnorm(j) = dznrm2(m-k,b(k+1,j),inc1)
       END DO

!      Set the remaining elements of the solutions to zero (to give
!      the minimum-norm solutions), Y2 = 0

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

!      Form W = (Z**H)*Y
!      The NAG name equivalent of zunmrz is f08bxf
       CALL zunmrz('Left','Conjugate transpose',n,nrhs,k,n-k,a,lda,tau,b,ldb, &
          work,lwork,info)

!      Permute the least-squares solutions stored in B to give X = P*W

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

!      Print least-squares solutions

!      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', &
          'Least-squares solution(s)','Integer',rlabs,'Integer',clabs,80,0, &
          ifail)

!      Print the square roots of the residual sums of squares

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

99999  FORMAT (3X,1P,7E11.2)
99998  FORMAT (1X,I6)
    END PROGRAM f08bvfe
