    PROGRAM f08khfe

!      F08KHF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : ddisna, dgejsv, nag_wp, x02ajf, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nb = 64, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: eps, serrbd
       INTEGER                         :: i, ifail, info, j, lda, ldu, ldv,    &
                                          lwork, m, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), rcondu(:), rcondv(:), s(:),  &
                                          u(:,:), v(:,:), work(:)
       INTEGER, ALLOCATABLE            :: iwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          abs, max
!      .. Executable Statements ..
       WRITE (nout,*) 'F08KHF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) m, n
       lda = m
       ldu = m
       ldv = n
       lwork = max(3*n+n*n+m,3*n+n*n+n*nb,7)
       ALLOCATE (a(lda,n),rcondu(m),rcondv(m),s(n),u(ldu,n),v(ldv,n), &
          work(lwork),iwork(m+3*n))

!      Read the m by n matrix A from data file
       READ (nin,*) ((a(i,j),j=1,n),i=1,m)

!      Compute the singular values and left and right singular vectors
!      of A (A = U*S*V^T, m.ge.n)
!      The NAG name equivalent of dgejsv is f08khf
       CALL dgejsv('E','U','V','R','N','N',m,n,a,lda,s,u,ldu,v,ldv,work,lwork, &
          iwork,info)

       IF (info==0) THEN

!         Compute the approximate error bound for the computed singular values 
!         using the 2-norm, s(1) = norm(A), and machine precision, eps.
          eps = x02ajf()
          serrbd = eps*s(1)

!         Print solution
          IF (abs(work(1)-work(2))<2.0_nag_wp*eps) THEN
!            No scaling required
             WRITE (nout,'(1X,A)') 'Singular values'
             WRITE (nout,99999) (s(j),j=1,n)
          ELSE
             WRITE (nout,'(/1X,A)') 'Scaled singular values'
             WRITE (nout,99999) (s(j),j=1,n)
             WRITE (nout,'(/1X,A)') &
                'For true singular values, multiply by a/b,'
             WRITE (nout,99996) ' where a = ', work(1), ' and b = ', work(2)
          END IF

!         ifail: behaviour on error exit
!                =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
          WRITE (nout,*)
          FLUSH (nout)
          ifail = 0
          CALL x04caf('General',' ',m,n,u,ldu,'Left singular vectors',ifail)

          WRITE (nout,*)
          FLUSH (nout)
          ifail = 0
          CALL x04caf('General',' ',n,n,v,ldv,'Right singular vectors',ifail)

!         Call DDISNA (F08FLF) to estimate reciprocal condition numbers for
!         the singular vectors.

          CALL ddisna('Left',m,n,s,rcondu,info)
          CALL ddisna('Right',m,n,s,rcondv,info)

!         Print the approximate error bounds for the singular values 
!         and vectors.
          WRITE (nout,*)
          WRITE (nout,'(/1X,A)') &
             'Estimate of the condition number of column equilibrated A'
          WRITE (nout,99998) work(3)
          WRITE (nout,'(/1X,A)') 'Error estimate for the singular values'
          WRITE (nout,99998) serrbd
          WRITE (nout,'(/1X,A)') 'Error estimates for left singular vectors'
          WRITE (nout,99998) (serrbd/rcondu(i),i=1,n)
          WRITE (nout,'(/1X,A)') 'Error estimates for right singular vectors'
          WRITE (nout,99998) (serrbd/rcondv(i),i=1,n)
       ELSE
          WRITE (nout,99997) 'Failure in DGEJSV. INFO =', info
       END IF


99999  FORMAT (3X,8F8.4)
99998  FORMAT (4X,1P,6E11.1)
99997  FORMAT (1X,A,I4)
99996  FORMAT (1X,2(A,1P,E13.5))
    END PROGRAM f08khfe
