    PROGRAM f08zefe

!      F08ZEF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dgemv, dggqrf, dnrm2, dormqr, dormrq, dtrtrs,   &
                               nag_wp
!      .. 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              :: nb = 64, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: rnorm
       INTEGER                         :: i, info, lda, ldb, lwork, m, n, p
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:,:), d(:), taua(:),       &
                                          taub(:), work(:), y(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max, min
!      .. Executable Statements ..
       WRITE (nout,*) 'F08ZEF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n, m, p
       lda = n
       ldb = n
       lwork = nb*(m+p)
       ALLOCATE (a(lda,m),b(ldb,p),d(n),taua(m),taub(m+p),work(lwork),y(p))

!      Read A, B and D from data file
       READ (nin,*) (a(i,1:m),i=1,n)
       READ (nin,*) (b(i,1:p),i=1,n)
       READ (nin,*) d(1:n)

!      Compute the generalized QR factorization of (A,B) as
!      A = Q*(R),   B = Q*(T11 T12)*Z
!             (0)          ( 0  T22)
!      The NAG name equivalent of dggqrf is f08zef
       CALL dggqrf(n,m,p,a,lda,taua,b,ldb,taub,work,lwork,info)

!      Compute c = (c1) = (Q**T)*d, storing the result in D
!                   (c2)
!      The NAG name equivalent of dormqr is f08agf
       CALL dormqr('Left','Transpose',n,1,m,a,lda,taua,d,n,work,lwork,info)

!      Putting Z*y = w = (w1), set w1 = 0, storing the result in Y1
!                         (w2)
       y(1:m+p-n) = zero

       IF (n>m) THEN

!         Copy c2 into Y2
          y(m+p-n+1:p) = d(m+1:n)

!         Solve T22*w2 = c2 for w2, storing result in Y2
!         The NAG name equivalent of dtrtrs is f07tef
          CALL dtrtrs('Upper','No transpose','Non-unit',n-m,1,b(m+1,m+p-n+1), &
             ldb,y(m+p-n+1),n-m,info)

          IF (info>0) THEN
             WRITE (nout,*) &
                'The upper triangular factor, T22, of B is singular, '
             WRITE (nout,*) 'the least squares solution could not be computed'
             GO TO 20
          END IF

!         Compute estimate of the square root of the residual sum of squares 
!         norm(y) = norm(w2)

!         The NAG name equivalent of dnrm2 is f06ejf
          rnorm = dnrm2(n-m,y(m+p-n+1),1)

!         Form c1 - T12*w2 in D
!         The NAG name equivalent of dgemv is f06paf
          CALL dgemv('No transpose',m,n-m,-one,b(1,m+p-n+1),ldb,y(m+p-n+1),1, &
             one,d,1)
       END IF

!      Solve R*x = c1 - T12*w2 for x
!      The NAG name equivalent of dtrtrs is f07tef
       CALL dtrtrs('Upper','No transpose','Non-unit',m,1,a,lda,d,m,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

!         Compute y = (Z**T)*w
!         The NAG name equivalent of dormrq is f08ckf
          CALL dormrq('Left','Transpose',p,1,min(n,p),b(max(1, &
             n-p+1),1),ldb,taub,y,p,work,lwork,info)

!         Print least squares solution x

          WRITE (nout,*) 'Generalized least squares solution'
          WRITE (nout,99999) d(1:m)

!         Print residual vector y

          WRITE (nout,*)
          WRITE (nout,*) 'Residual vector'
          WRITE (nout,99998) y(1:p)

!         Print estimate of the square root of the residual sum of squares

          WRITE (nout,*)
          WRITE (nout,*) 'Square root of the residual sum of squares'
          WRITE (nout,99998) rnorm
       END IF
20     CONTINUE

99999  FORMAT (1X,7F11.4)
99998  FORMAT (3X,1P,7E11.2)
    END PROGRAM f08zefe
