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

NAG AD Library Introduction
Example description
    Program f08me_t1w_fe

!     F08ME_T1W_F Example Program Text

!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f08me_t1w_f, nagad_t1w_w_rtype, x10aa_t1w_f,    &
                               x10ab_t1w_f, Assignment (=)
      Use nag_library, Only: nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Integer                          :: i, ifail, j, ldc, ldu, ldvt, n
      Character (1)                    :: uplo
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: c(:,:), d(:), d_in(:), e(:),    &
                                          e_in(:), u(:,:), vt(:,:), work(:)
      Real (Kind=nag_wp), Allocatable  :: dr(:), dsdd(:,:), dsde(:,:), er(:),  &
                                          ur(:,:), vtr(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'F08ME_T1W_F Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      ldc = 1
      ldu = n
      ldvt = n
      Allocate (c(ldc,1),d(n),e(n-1),u(ldu,n),vt(ldvt,n),work(4*n))
      Allocate (d_in(n),e_in(n-1),dr(n),er(n-1),ur(ldu,n),vtr(ldvt,n))
      Allocate (dsdd(n,n),dsde(n,n-1))

!     Read B from data file

      Read (nin,*) dr(1:n)
      Read (nin,*) er(1:n-1)

      Read (nin,*) uplo

      d_in = dr
      e_in = er

!     Create AD configuration data object
      ifail = 0
      Call x10aa_t1w_f(ad_handle,ifail)

      Do i = 1, 2*n - 1
        If (i<=n) Then
          d_in(i)%tangent = 1.0_nag_wp
        Else
          e_in(i-n)%tangent = 1.0_nag_wp
        End If
        d = d_in
        e = e_in

!       Initialize U and VT to be the unit matrix
        u = 0.0_nag_wp
        vt = 0.0_nag_wp
        Do j = 1, n
          u(j,j) = 1.0_nag_wp
          vt(j,j) = 1.0_nag_wp
        End Do

!       Calculate the SVD of B
        ifail = 0
        Call f08me_t1w_f(ad_handle,uplo,n,n,n,0,d,e,u,ldu,vt,ldvt,c,ldc,work,  &
          ifail)

        If (i<=n) Then
          d_in(i)%tangent = 0.0_nag_wp
          dsdd(1:n,i) = d(1:n)%tangent
        Else
          e_in(i-n)%tangent = 0.0_nag_wp
          dsde(1:n,i-n) = d(1:n)%tangent
        End If
      End Do

      Write (nout,*)
!     Print singular values, left & right singular vectors

      Write (nout,*) 'Singular values'
      Write (nout,99999) d(1:n)%value
      Write (nout,*)
      Flush (nout)

      ur = u
      ifail = 0
      Call x04caf('General',' ',n,n,ur,ldu,'Left singular vectors, by column', &
        ifail)
      Write (nout,*)
      Flush (nout)

      vtr = vt
      ifail = 0
      Call x04caf('General',' ',n,n,vtr,ldvt,'Right singular vectors, by row', &
        ifail)


      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: First order tangents'
      Write (nout,*) ' Computational mode    : algorithmic'

      Write (nout,*)
      Write (nout,*) ' Derivatives of S.V.s w.r.t. d, e on input:'
      ifail = 0
      Call x04caf('General',' ',n,n,dsdd,n,' dS_i/dD_j',ifail)
      Write (nout,*)
      ifail = 0
      Call x04caf('General',' ',n,n-1,dsde,n,' dS_i/dE_j',ifail)

!     Remove computational data object
      Call x10ab_t1w_f(ad_handle,ifail)

99999 Format (3X,(8F8.4))
    End Program f08me_t1w_fe