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

NAG AD Library Introduction
Example description
    Program f08kd_a1w_fe

!     F08KD_A1W_F Example Program Text
!     Mark 30.0 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f08kd_a1w_f, nagad_a1w_get_derivative,          &
                               nagad_a1w_inc_derivative,                       &
                               nagad_a1w_ir_create => x10za_a1w_f,             &
                               nagad_a1w_ir_interpret_adjoint_sparse,          &
                               nagad_a1w_ir_register_variable,                 &
                               nagad_a1w_ir_remove, nagad_a1w_ir_zero_adjoints &
                               , nagad_a1w_w_rtype, nagad_algorithmic,         &
                               x10aa_a1w_f, x10ab_a1w_f, x10ac_a1w_f,          &
                               Assignment (=)
      Use nag_library, Only: nag_wp, x04caf
!     .. 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,  &
                                          mode, n
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: a(:,:), ax(:,:), s(:), u(:,:),  &
                                          vt(:,:), work(:)
      Type (nagad_a1w_w_rtype)         :: dummy(1,1)
      Real (Kind=nag_wp), Allocatable  :: ar(:,:), dsda(:,:), ur(:,:), vr(:,:)
      Integer, Allocatable             :: iwork(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max, min, nint
!     .. Executable Statements ..
      Write (nout,*) 'F08KD_A1W_F Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n
      Read (nin,*) mode
      lda = m
      ldu = m
      ldvt = n
      Allocate (a(lda,n),s(m),u(ldu,m),vt(ldvt,n),iwork(8*min(m,n)))
      Allocate (ar(m,n),ax(lda,n),ur(ldu,m),vr(ldvt,n),dsda(m,m))
!     Create AD tape
      Call nagad_a1w_ir_create

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

!     Set Computational mode
      ifail = 0
      Call x10ac_a1w_f(ad_handle,mode,ifail)

!     Use routine workspace query to get optimal workspace.
      lwork = -1
      ifail = 0
!     The NAG name equivalent of dgesdd_a1w is f08kd_a1w_f
      Call f08kd_a1w_f(ad_handle,'A',m,n,a,lda,s,u,ldu,vt,ldvt,dummy,lwork,    &
        iwork,ifail)

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

!     Read the m by n matrix A from data file

      Read (nin,*)(ar(i,1:n),i=1,m)
      a = ar

!     Register variables to differentiate w.r.t.
      Call nagad_a1w_ir_register_variable(a)
      ax = a
!     Compute the singular values and left and right singular vectors
!     of A (A = U*S*(V**T), m.le.n)

!     The NAG name equivalent of dgesdd_a1w is f08kd_a1w_f
      ifail = 0
      Call f08kd_a1w_f(ad_handle,'A',m,n,ax,lda,s,u,ldu,vt,ldvt,work,lwork,    &
        iwork,ifail)

!     Print solution

      Write (nout,*)
      Write (nout,*) 'Singular values'
      Write (nout,99999) s(1:m)%value
99999 Format (3X,(8F8.4))
      Flush (nout)

      ur(1:m,1:m) = u(1:m,1:m)%value
      vr(1:n,1:n) = vt(1:n,1:n)%value
!     Normalize so that u(1,j)>=0
      Do i = 1, min(m,n)
        If (ur(1,i)<0.0_nag_wp) Then
          ur(1:m,i) = -ur(1:m,i)
          vr(i,1:n) = -vr(i,1:n)
        End If
      End Do
!     ifail: behaviour on error exit
!            =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call x04caf('General',' ',m,m,ur,ldu,'Left singular vectors',ifail)

      Write (nout,*)
      Flush (nout)

      Call x04caf('General',' ',m,n,vr,ldvt,'Right singular vectors by row '// &
        '(first m rows of V**T)',ifail)

      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: First order adjoints'
      If (mode==nagad_algorithmic) Then
        Write (nout,*) ' Computational mode    : algorithmic'
      Else
        Write (nout,*) ' Computational mode    : symbolic'
      End If

      Write (nout,*)
      Write (nout,*) ' Derivatives of S w.r.t. first column of A:'
!     Setup evaluation of derivatives via adjoints
      Do i = 1, m
        Call nagad_a1w_ir_zero_adjoints
        Call nagad_a1w_inc_derivative(s(i),1.0_nag_wp)
        ifail = 0
        Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

!       Get derivatives
        dsda(i,1:m) = nagad_a1w_get_derivative(a(1:m,1))
      End Do
      Call x04caf('General',' ',m,m,dsda,m,'',ifail)

!     Remove computational data object and tape
      Call x10ab_a1w_f(ad_handle,ifail)
      Call nagad_a1w_ir_remove

    End Program f08kd_a1w_fe