    PROGRAM f08kffe

!      F08KFF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dbdsqr, dgebrd, dorgbr, f06qff, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ic, ifail, info, lda, ldc, ldu,   &
                                          ldvt, lwork, m, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), c(:,:), d(:), e(:), taup(:), &
                                          tauq(:), u(:,:), vt(:,:), work(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08KFF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       DO ic = 1, 2
          READ (nin,*) m, n
          lda = m
          ldc = n
          ldu = m
          ldvt = n
          lwork = 64*(m+n)
          ALLOCATE (a(lda,n),c(ldc,n),d(n),e(n-1),taup(n),tauq(n),u(ldu,n), &
             vt(ldvt,n),work(lwork))

!         Read A from data file

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

!         Reduce A to bidiagonal form
!         The NAG name equivalent of dgebrd is f08kef
          CALL dgebrd(m,n,a,lda,d,e,tauq,taup,work,lwork,info)

          IF (m>=n) THEN

!            Copy A to VT and U

             CALL f06qff('Upper',n,n,a,lda,vt,ldvt)
             CALL f06qff('Lower',m,n,a,lda,u,ldu)

!            Form P**T explicitly, storing the result in VT
!            The NAG name equivalent of dorgbr is f08kff
             CALL dorgbr('P',n,n,m,vt,ldvt,taup,work,lwork,info)

!            Form Q explicitly, storing the result in U
!            The NAG name equivalent of dorgbr is f08kff
             CALL dorgbr('Q',m,n,n,u,ldu,tauq,work,lwork,info)

!            Compute the SVD of A
!            The NAG name equivalent of dbdsqr is f08mef
             CALL dbdsqr('Upper',n,n,m,0,d,e,vt,ldvt,u,ldu,c,ldc,work,info)

!            Print singular values, left & right singular vectors

             WRITE (nout,*)
             WRITE (nout,*) 'Example 1: singular values'
             WRITE (nout,99999) d(1:n)
             WRITE (nout,*)
             FLUSH (nout)

!            ifail: behaviour on error exit
!                   =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
             ifail = 0
             CALL x04caf('General',' ',n,n,vt,ldvt, &
                'Example 1: right singular vectors, by row',ifail)

             WRITE (nout,*)
             FLUSH (nout)

             CALL x04caf('General',' ',m,n,u,ldu, &
                'Example 1: left singular vectors, by column',ifail)

          ELSE

!            Copy A to VT and U

             CALL f06qff('Upper',m,n,a,lda,vt,ldvt)
             CALL f06qff('Lower',m,m,a,lda,u,ldu)

!            Form P**T explicitly, storing the result in VT
!            The NAG name equivalent of dorgbr is f08kff
             CALL dorgbr('P',m,n,m,vt,ldvt,taup,work,lwork,info)

!            Form Q explicitly, storing the result in U
!            The NAG name equivalent of dorgbr is f08kff
             CALL dorgbr('Q',m,m,n,u,ldu,tauq,work,lwork,info)

!            Compute the SVD of A
!            The NAG name equivalent of dbdsqr is f08mef
             CALL dbdsqr('Lower',m,n,m,0,d,e,vt,ldvt,u,ldu,c,ldc,work,info)

!            Print singular values, left & right singular vectors

             WRITE (nout,*)
             WRITE (nout,*) 'Example 2: singular values'
             WRITE (nout,99999) d(1:m)
             WRITE (nout,*)
             FLUSH (nout)

             ifail = 0
             CALL x04caf('General',' ',m,n,vt,ldvt, &
                'Example 2: right singular vectors, by row',ifail)

             WRITE (nout,*)
             FLUSH (nout)

             CALL x04caf('General',' ',m,m,u,ldu, &
                'Example 2: left singular vectors, by column',ifail)

          END IF
          DEALLOCATE (a,c,d,e,taup,tauq,u,vt,work)
       END DO

99999  FORMAT (3X,(8F8.4))
    END PROGRAM f08kffe
