    PROGRAM f08vsfe

!      F08VSF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f06uaf, nag_wp, x02ajf, x04dbf, zggsvp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: eps, tola, tolb
       INTEGER                         :: i, ifail, info, irank, k, l, lda,    &
                                          ldb, ldq, ldu, ldv, m, n, p
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:,:), q(:,:), tau(:),   &
                                             u(:,:), v(:,:), work(:)
       REAL (KIND=nag_wp), ALLOCATABLE :: rwork(:)
       INTEGER, ALLOCATABLE            :: iwork(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Intrinsic Functions ..
       INTRINSIC                          max, real
!      .. Executable Statements ..
       WRITE (nout,*) 'F08VSF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) m, n, p
       lda = m
       ldb = p
       ldq = n
       ldu = m
       ldv = p
       ALLOCATE (a(lda,n),b(ldb,n),q(ldq,n),tau(n),u(ldu,m),v(ldv,p), &
          work(m+3*n+p),rwork(2*n),iwork(n))

!      Read the m by n matrix A and p by n matrix B from data file

       READ (nin,*) (a(i,1:n),i=1,m)
       READ (nin,*) (b(i,1:n),i=1,p)

!      Compute tola and tolb as
!          tola = max(m,n)*norm(A)*macheps
!          tolb = max(p,n)*norm(B)*macheps

       eps = x02ajf()
       tola = real(max(m,n),kind=nag_wp)*f06uaf('One-norm',m,n,a,lda,rwork)* &
          eps
       tolb = real(max(p,n),kind=nag_wp)*f06uaf('One-norm',p,n,b,ldb,rwork)* &
          eps

!      Compute the factorization of (A, B)
!          (A = U*S*(Q**H), B = V*T*(Q**H))

!      The NAG name equivalent of zggsvp is f08vsf
       CALL zggsvp('U','V','Q',m,p,n,a,lda,b,ldb,tola,tolb,k,l,u,ldu,v,ldv,q, &
          ldq,iwork,rwork,tau,work,info)

!      Print solution

       irank = k + l
       WRITE (nout,*) 'Numerical rank of (A**T B**T)**T (K+L)'
       WRITE (nout,99999) irank

       WRITE (nout,*)
       FLUSH (nout)
       IF (m>=irank) THEN

!         ifail: behaviour on error exit
!                =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
          ifail = 0
          CALL x04dbf('Upper','Non-unit',irank,irank,a(1,n-irank+1),lda, &
             'Bracketed','1P,E12.4','Upper triangular matrix S','Integer', &
             rlabs,'Integer',clabs,80,0,ifail)

       ELSE

          ifail = 0
          CALL x04dbf('Upper','Non-unit',m,irank,a(1,n-irank+1),lda, &
             'Bracketed','1P,E12.4','Upper trapezoidal matrix S','Integer', &
             rlabs,'Integer',clabs,80,0,ifail)

       END IF
       WRITE (nout,*)
       FLUSH (nout)

       ifail = 0
       CALL x04dbf('Upper','Non-unit',l,l,b(1,n-l+1),ldb,'Bracketed', &
          '1P,E12.4','Upper triangular matrix T','Integer',rlabs,'Integer', &
          clabs,80,0,ifail)

       WRITE (nout,*)
       FLUSH (nout)

       ifail = 0
       CALL x04dbf('General',' ',m,m,u,ldu,'Bracketed','1P,E12.4', &
          'Orthogonal matrix U','Integer',rlabs,'Integer',clabs,80,0,ifail)

       WRITE (nout,*)
       FLUSH (nout)

       ifail = 0
       CALL x04dbf('General',' ',p,p,v,ldv,'Bracketed','1P,E12.4', &
          'Orthogonal matrix V','Integer',rlabs,'Integer',clabs,80,0,ifail)

       WRITE (nout,*)
       FLUSH (nout)

       ifail = 0
       CALL x04dbf('General',' ',n,n,q,ldq,'Bracketed','1P,E12.4', &
          'Orthogonal matrix Q','Integer',rlabs,'Integer',clabs,80,0,ifail)

99999  FORMAT (1X,I5)
    END PROGRAM f08vsfe
