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

NAG AD Library Introduction
Example description
    Program f08kp_p0w_fe

!     F08KP_P0W_F Example Program Text
!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f08kp_p0w_f
      Use nag_library, Only: nag_wp, x04daf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Integer                          :: i, ifail, lda, ldu, ldvt, lwork, m,  &
                                          n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), u(:,:), vt(:,:), work(:)
      Complex (Kind=nag_wp)            :: dummy(1)
      Real (Kind=nag_wp), Allocatable  :: rwork(:), s(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max, nint, real
!     .. Executable Statements ..
      Write (nout,*) 'F08KP_P0W_F Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n
      lda = m
      ldu = m
      ldvt = n
      Allocate (a(lda,n),s(m),u(ldu,m),vt(ldvt,n),rwork(5*n))

!     Use routine workspace query to get optimal workspace.
      lwork = -1
      ifail = 0
      Call f08kp_p0w_f(ad_handle,'A','A',m,n,a,lda,s,u,ldu,vt,ldvt,dummy,      &
        lwork,rwork,ifail)

!     Make sure that there is enough workspace for block size nb.
      lwork = max(m+3*n+nb*(m+n),nint(real(dummy(1))))
      Allocate (work(lwork))

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

!     Compute the singular values and left and right singular vectors
!     of A (A = U*S*(V**T), m.le.n)
      ifail = 0
      Call f08kp_p0w_f(ad_handle,'A','A',m,n,a,lda,s,u,ldu,vt,ldvt,work,lwork, &
        rwork,ifail)

!     Print solution
      Write (nout,*)
      Write (nout,*) 'Singular values'
      Write (nout,99999) s(1:n)
99999 Format (3X,(8F8.4))
      Flush (nout)

      ifail = 0
      Call x04daf('General',' ',m,m,u,ldu,'Left singular vectors',ifail)

      Write (nout,*)
      Flush (nout)
      Call x04daf('General',' ',n,n,vt,ldvt,'Right singular vectors by row',   &
        ifail)

    End Program f08kp_p0w_fe