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

NAG FL Interface Introduction
Example description
    Program f08kvfe

!     F08KVF Example Program Text

!     Mark 29.1 Release. NAG Copyright 2023.

!     .. Use Statements ..
      Use nag_library, Only: ddisna, nag_wp, x02ajf, x04daf, zgejsv
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: eps, serrbd
      Integer                          :: i, ifail, info, j, lda, ldu, ldv,    &
                                          lrwork, lwork, m, n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), cwork(:), u(:,:), v(:,:)
      Real (Kind=nag_wp), Allocatable  :: rcondu(:), rcondv(:), rwork(:),      &
                                          sva(:)
      Integer, Allocatable             :: iwork(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, nint
!     .. Executable Statements ..
      Write (nout,*) 'F08KVF 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 = 5*n + n*n + 1000
      lrwork = n + 2*m + 7
      Allocate (a(lda,n),rcondu(m),rcondv(m),sva(n),u(ldu,n),v(ldv,n),         &
        cwork(lwork),rwork(lrwork),iwork(m+3*n+3))

!     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 zgejsv is f08kvf
      Call zgejsv('E','U','V','R','N','N',m,n,a,lda,sva,u,ldu,v,ldv,cwork,     &
        lwork,rwork,lrwork,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*sva(1)

!       Print solution
        If (abs(rwork(1)-rwork(2))<2.0_nag_wp*eps) Then
!         No scaling required
          Write (nout,'(1X,A)') 'Singular values'
          Write (nout,99999)(sva(j),j=1,n)
        Else
          Write (nout,'(/1X,A)') 'Scaled singular values'
          Write (nout,99999)(sva(j),j=1,n)
          Write (nout,'(/1X,A)') 'For true singular values, multiply by a/b,'
          Write (nout,99995) ' where a = ', rwork(1), ' and b = ', rwork(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 x04daf('General',' ',m,n,u,ldu,'Left singular vectors',ifail)

        Write (nout,*)
        Flush (nout)
        ifail = 0
        Call x04daf('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,sva,rcondu,info)
        Call ddisna('Right',m,n,sva,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) rwork(3)
        Write (nout,'(/1X,A)')                                                 &
          'Error estimates (as multiples of machine precision):'
        Write (nout,'(/1X,A)') '  for the singular values'
        Write (nout,99997) nint(serrbd/x02ajf())
        Write (nout,'(/1X,A)') '  for left singular vectors'
        Write (nout,99997)(nint(serrbd/rcondu(i)/x02ajf()),i=1,n)
        Write (nout,'(/1X,A)') '  for right singular vectors'
        Write (nout,99997)(nint(serrbd/rcondv(i)/x02ajf()),i=1,n)
      Else
        Write (nout,99996) 'Failure in ZGEJSV. INFO =', info
      End If

99999 Format (3X,8F8.4)
99998 Format (4X,1P,E11.2)
99997 Format (4X,6I4)
99996 Format (1X,A,I4)
99995 Format (1X,2(A,1P,E13.5))
    End Program f08kvfe