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

NAG AD Library Introduction
Example description
    Program f08kp_a1w_fe

!     F08KP_A1W_F Example Program Text
!     Mark 30.3 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f08kp_a1w_f, nagad_a1w_get_derivative,          &
                               nagad_a1w_ir_interpret_adjoint_sparse,          &
                               nagad_a1w_ir_register_variable,                 &
                               nagad_a1w_ir_remove, nagad_a1w_ir_zero_adjoints &
                               , nagad_a1w_set_derivative, nagad_a1w_w_ctype,  &
                               nagad_a1w_w_rtype, x10aa_a1w_f, x10ab_a1w_f,    &
                               x10za_a1w_f, Assignment (=)
      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
      Real (Kind=nag_wp)               :: dsi, dsr
      Integer                          :: i, ifail, j, lda, ldu, ldvt, lwork,  &
                                          m, n
!     .. Local Arrays ..
      Type (nagad_a1w_w_ctype), Allocatable :: a(:,:), ax(:,:), u(:,:),        &
                                          vt(:,:), work(:)
      Type (nagad_a1w_w_ctype)         :: dummy(1)
      Type (nagad_a1w_w_rtype), Allocatable :: rwork(:), s(:)
      Complex (Kind=nag_wp), Allocatable :: ac(:,:), dsda(:,:), uc(:,:),       &
                                          vc(:,:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: cmplx, max, nint
!     .. Executable Statements ..
      Write (nout,*) 'F08KP_A1W_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))
      Allocate (ac(m,n),ax(lda,n),uc(ldu,m),vc(ldvt,n),dsda(n,m))
!     Create AD tape
      Call x10za_a1w_f

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

!     Use routine workspace query to get optimal workspace.

      lwork = -1
      ifail = 0
      Call f08kp_a1w_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(dummy(1)%vreal%value))
      Allocate (work(lwork))

!     Read the m by n matrix A from data file

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

!     Register variables to differentiate w.r.t.
      Do i = 1, m
        Call nagad_a1w_ir_register_variable(a(i,1)%vreal)
        Call nagad_a1w_ir_register_variable(a(i,1)%vimag)
      End Do
      ax = a
!     Compute the singular values and left and right singular vectors
!     of A (A = U*S*(V**T), m.le.n)
      ifail = 0
      Call f08kp_a1w_f(ad_handle,'A','A',m,n,ax,lda,s,u,ldu,vt,ldvt,work,      &
        lwork,rwork,ifail)

!     Print solution

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

      Do i = 1, m
        Do j = 1, m
          uc(i,j) = cmplx(u(i,j)%vreal%value,u(i,j)%vimag%value,kind=nag_wp)
        End Do
      End Do
      Do i = 1, n
        Do j = 1, n
          vc(i,j) = cmplx(vt(i,j)%vreal%value,vt(i,j)%vimag%value,kind=nag_wp)
        End Do
      End Do
!     ifail: behaviour on error exit
!            =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call x04daf('General',' ',m,m,uc,m,'Left singular vectors',ifail)

      Write (nout,*)
      Flush (nout)

      Call x04daf('General',' ',n,n,vc,n,'Right singular vectors by row',      &
        ifail)

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

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

!       Get derivatives
        Do j = 1, m
          dsr = nagad_a1w_get_derivative(a(j,1)%vreal)
          dsi = nagad_a1w_get_derivative(a(j,1)%vimag)
          dsda(i,j) = cmplx(dsr,dsi,kind=nag_wp)
        End Do
      End Do
      Call x04daf('General',' ',n,m,dsda,n,'',ifail)

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

    End Program f08kp_a1w_fe