NAG Library Manual, Mark 28.6
Interfaces:  FL   CL   CPP   AD 

NAG FL Interface Introduction
Example description
    Program f08khfe

!     F08KHF Example Program Text

!     Mark 28.6 Release. NAG Copyright 2022.

!     .. 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 Procedures ..
      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