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

NAG FL Interface Introduction
Example description
    Program f08kwfe

!     F08KWF Example Program Text

!     Mark 29.0 Release. NAG Copyright 2023.

!     .. Use Statements ..
      Use nag_library, Only: ddisna, nag_wp, x02ajf, x04daf, zgesvj
!     .. 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, ldv, lrwork, &
                                          lwork, m, n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), cwork(:), v(:,:)
      Real (Kind=nag_wp), Allocatable  :: rcondu(:), rcondv(:), rwork(:),      &
                                          sva(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, max, nint
!     .. Executable Statements ..
      Write (nout,*) 'F08KWF Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n
      lda = m
      ldv = n
      lwork = n + m
      lrwork = max(6,n)
      Allocate (a(lda,n),rcondu(m),rcondv(m),sva(n),v(ldv,n),cwork(lwork),     &
        rwork(lrwork))

!     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, m.ge.n)

!     The NAG name equivalent of zgesvj is f08kwf
      Call zgesvj('G','U','V',m,n,a,lda,sva,0,v,ldv,cwork,lwork,rwork,lrwork,  &
        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
        Write (nout,*) 'Singular values'
        Write (nout,99999)(sva(j),j=1,n)

        If (abs(rwork(1)-1.0_nag_wp)>eps) Then
          Write (nout,99996) 'Values need scaling by factor = ', rwork(1)
        End If
        Write (nout,*)
        Flush (nout)

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call x04daf('General',' ',m,n,a,lda,'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,'(/1X,A)')                                                 &
          'Error estimates (as multiples of machine precision):'
        Write (nout,'(/1X,A)') '  for the singular values'
        Write (nout,99998) nint(serrbd/x02ajf())
        Write (nout,'(/1X,A)') '  for left singular vectors'
        Write (nout,99998)(nint(serrbd/rcondu(i)/x02ajf()),i=1,n)
        Write (nout,'(/1X,A)') '  for right singular vectors'
        Write (nout,99998)(nint(serrbd/rcondv(i)/x02ajf()),i=1,n)
      Else
        Write (nout,99997) 'Failure in ZGESVJ. INFO =', info
      End If

99999 Format (3X,(8F8.4))
99998 Format (4X,6I4)
99997 Format (1X,A,I4)
99996 Format (/,1X,A,1P,E13.5)
    End Program f08kwfe