    PROGRAM f08ktfe

!      F08KTF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f06tff, nag_wp, x04dbf, zbdsqr, zgebrd, zungbr
!      .. 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 ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), c(:,:), taup(:), tauq(:), &
                                             u(:,:), vt(:,:), work(:)
       REAL (KIND=nag_wp), ALLOCATABLE :: d(:), e(:), rwork(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08KTF 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),taup(n),tauq(n),u(ldu,n),vt(ldvt,n), &
             work(lwork),d(n),e(n-1),rwork(4*n-4))

!         Read A from data file
          READ (nin,*) (a(i,1:n),i=1,m)

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

          IF (m>=n) THEN

!            Copy A to VT and U
             CALL f06tff('Upper',n,n,a,lda,vt,ldvt)
             CALL f06tff('Lower',m,n,a,lda,u,ldu)

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

!            Form Q explicitly, storing the result in U
             CALL zungbr('Q',m,n,n,u,ldu,tauq,work,lwork,info)

!            Compute the SVD of A
!            The NAG name equivalent of zbdsqr is f08msf
             CALL zbdsqr('Upper',n,n,m,0,d,e,vt,ldvt,u,ldu,c,ldc,rwork,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 x04dbf('General',' ',n,n,vt,ldvt,'Bracketed','F7.4', &
                'Example 1: right singular vectors, by row','Integer',rlabs, &
                'Integer',clabs,80,0,ifail)

             WRITE (nout,*)
             FLUSH (nout)

             CALL x04dbf('General',' ',m,n,u,ldu,'Bracketed','F7.4', &
                'Example 1: left singular vectors, by column','Integer',rlabs, &
                'Integer',clabs,80,0,ifail)

          ELSE

!            Copy A to VT and U
             CALL f06tff('Upper',m,n,a,lda,vt,ldvt)
             CALL f06tff('Lower',m,m,a,lda,u,ldu)

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

!            Form Q explicitly, storing the result in U
             CALL zungbr('Q',m,m,n,u,ldu,tauq,work,lwork,info)

!            Compute the SVD of A
!            The NAG name equivalent of zbdsqr is f08msf
             CALL zbdsqr('Lower',m,n,m,0,d,e,vt,ldvt,u,ldu,c,ldc,rwork,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 x04dbf('General',' ',m,n,vt,ldvt,'Bracketed','F7.4', &
                'Example 2: right singular vectors, by row','Integer',rlabs, &
                'Integer',clabs,80,0,ifail)

             WRITE (nout,*)
             FLUSH (nout)

             CALL x04dbf('General',' ',m,m,u,ldu,'Bracketed','F7.4', &
                'Example 2: left singular vectors, by column','Integer',rlabs, &
                'Integer',clabs,80,0,ifail)

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

99999  FORMAT (8X,4(F7.4,11X:))
    END PROGRAM f08ktfe
