    PROGRAM f02wdfe

!      F02WDF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f02wdf, nag_wp, x04cbf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: tol
       INTEGER                         :: i, ifail, irank, lda, ldpt, ldr,     &
                                          lwork, m, n
       LOGICAL                         :: svd, wantb, wantpt, wantr
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), pt(:,:), r(:,:), sv(:),      &
                                          work(:), z(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Executable Statements ..
       WRITE (nout,*) 'F02WDF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) m, n
       WRITE (nout,*)
       lda = m
       ldpt = n
       ldr = n
       lwork = 3*n
       ALLOCATE (a(lda,n),pt(ldpt,n),r(ldr,n),sv(n),work(lwork),z(n))
       svd = .TRUE.
       tol = 5.0E-4_nag_wp
       READ (nin,*) (a(i,1:n),i=1,m)
       wantb = .FALSE.
       wantr = .TRUE.
       wantpt = .TRUE.

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f02wdf(m,n,a,lda,wantb,work,tol,svd,irank,z,sv,wantr,r,ldr,wantpt, &
          pt,ldpt,work,lwork,ifail)

       WRITE (nout,99999) 'Rank of A is', irank
       WRITE (nout,*)
       FLUSH (nout)

       ifail = 0
       CALL x04cbf('General',' ',m,n,a,lda,'F9.3', &
          'Details of QU factorization','N',rlabs,'N',clabs,80,0,ifail)

       WRITE (nout,*)
       WRITE (nout,*) 'Vector Z'
       WRITE (nout,99998) z(1:n)
       WRITE (nout,*)
       FLUSH (nout)

       ifail = 0
       CALL x04cbf('General',' ',n,n,r,ldr,'F9.3','Matrix R','N',rlabs,'N', &
          clabs,80,0,ifail)

       WRITE (nout,*)
       WRITE (nout,*) 'Singular values'
       WRITE (nout,99998) sv(1:n)
       WRITE (nout,*)
       FLUSH (nout)

       ifail = 0
       CALL x04cbf('General',' ',n,n,pt,ldpt,'F9.3','Matrix P**T','N',rlabs, &
          'N',clabs,80,0,ifail)

99999  FORMAT (1X,A,I5,A,I5)
99998  FORMAT (1X,8F9.3)
    END PROGRAM f02wdfe
