```    Program f08vefe

!     F08VEF Example Program Text

!     Mark 26.2 Release. NAG Copyright 2017.

!     .. Use Statements ..
Use nag_library, Only: dggsvp, f06raf, nag_wp, x02ajf, x04cbf
!     .. 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 ..
Real (Kind=nag_wp), Allocatable  :: a(:,:), b(:,:), q(:,:), tau(:),      &
u(:,:), v(:,:), work(:)
Integer, Allocatable             :: iwork(:)
Character (1)                    :: clabs(1), rlabs(1)
!     .. Intrinsic Procedures ..
Intrinsic                        :: max, real
!     .. Executable Statements ..
Write (nout,*) 'F08VEF Example Program Results'
Write (nout,*)
Flush (nout)

!     Skip heading in data file
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),iwork(n))

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

!     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)*f06raf('One-norm',m,n,a,lda,work)*eps
tolb = real(max(p,n),kind=nag_wp)*f06raf('One-norm',p,n,b,ldb,work)*eps

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

!     The NAG name equivalent of dggsvp is f08vef
Call dggsvp('U','V','Q',m,p,n,a,lda,b,ldb,tola,tolb,k,l,u,ldu,v,ldv,q,   &
ldq,iwork,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 x04cbf('Upper','Non-unit',irank,irank,a(1,n-irank+1),lda,         &
'1P,E12.4','Upper triangular matrix S','Integer',rlabs,'Integer',    &
clabs,80,0,ifail)

Else

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

End If
Write (nout,*)
Flush (nout)

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

Write (nout,*)
Flush (nout)

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

Write (nout,*)
Flush (nout)

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

Write (nout,*)
Flush (nout)

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

99999 Format (1X,I5)
End Program f08vefe
```