    Program f02wufe

!     F02WUF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: f02wuf, nag_wp, x04cbf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Integer                          :: i, ifail, lda, ldb, ldq, lwork, n,   &
                                          ncolb
      Logical                          :: wantp, wantq
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), b(:), q(:,:), sv(:), work(:)
      Character (1)                    :: clabs(1), rlabs(1)
!     .. Executable Statements ..
      Write (nout,*) 'F02WUF Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n, ncolb
      lda = n
      ldb = n
      ldq = n
      lwork = 5*(n-1)
      Allocate (a(lda,n),b(ldb),q(ldq,n),sv(n),work(lwork))
      Read (nin,*)(a(i,i:n),i=1,n)
      Read (nin,*) b(1:n)
      wantq = .True.
      wantp = .True.

!     ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
!     Find the SVD of A
      Call f02wuf(n,a,lda,ncolb,b,ldb,wantq,q,ldq,sv,wantp,work,ifail)

      Write (nout,*) 'Singular value decomposition of A'
      Write (nout,*)
      Write (nout,*) 'Singular values'
      Write (nout,99999) sv(1:n)
      Write (nout,*)
      Flush (nout)

      ifail = 0
      Call x04cbf('General',' ',n,n,q,ldq,'F8.4', &
        'Left-hand singular vectors, by column','N',rlabs,'N',clabs,80,0, &
        ifail)

      Write (nout,*)
      Write (nout,*) 'Right-hand singular vectors, by column'
      Do i = 1, n
        Write (nout,99999) a(1:n,i)
      End Do
      Write (nout,*)
      Write (nout,*) 'Vector Q''*B'
      Write (nout,99999) b(1:n)

99999 Format (1X,3(1X,F8.4))
    End Program f02wufe
