Program f08me_a1w_fe
! F08MEF Example Program Text
! Mark 27.1 Release. NAG Copyright 2020.
! .. Use Statements ..
Use iso_c_binding, Only: c_ptr
Use nagad_library, Only: f08me_a1w_f, nagad_a1w_get_derivative, &
nagad_a1w_inc_derivative, &
nagad_a1w_ir_interpret_adjoint_sparse, &
nagad_a1w_ir_register_variable, &
nagad_a1w_ir_remove, nagad_a1w_ir_zero_adjoints &
, nagad_a1w_w_rtype, x10aa_a1w_f, x10ab_a1w_f, &
x10za_a1w_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, ldc, ldu, ldvt, n
Character (1) :: uplo
! .. Local Arrays ..
Type (nagad_a1w_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_A1W_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
! Initialize U and VT to be the unit matrix
u = 0.0_nag_wp
vt = 0.0_nag_wp
Do i = 1, n
u(i,i) = 1.0_nag_wp
vt(i,i) = 1.0_nag_wp
End Do
! Create AD tape
Call x10za_a1w_f
! Create AD configuration data object
ifail = 0
Call x10aa_a1w_f(ad_handle,ifail)
! Register variables to differentiate w.r.t.
Call nagad_a1w_ir_register_variable(d_in)
Call nagad_a1w_ir_register_variable(e_in)
d = d_in
e = e_in
! Calculate the SVD of B
ifail = 0
Call f08me_a1w_f(ad_handle,uplo,n,n,n,0,d,e,u,ldu,vt,ldvt,c,ldc,work, &
ifail)
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)
vtr = vt
ifail = 0
Call x04caf('General',' ',n,n,vtr,ldvt,'Right singular vectors, by row', &
ifail)
Write (nout,*)
Flush (nout)
Write (nout,*)
Write (nout,*) ' Derivatives calculated: First order adjoints'
Write (nout,*) ' Computational mode : algorithmic'
Write (nout,*)
Write (nout,*) ' Derivatives of S.V.s w.r.t. d, e on input:'
! Setup evaluation of derivatives via adjoints
Do i = 1, n
Call nagad_a1w_ir_zero_adjoints
Call nagad_a1w_inc_derivative(d(i),1.0_nag_wp)
ifail = 0
Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)
! Get derivatives
dsdd(i,1:n) = nagad_a1w_get_derivative(d_in(1:n))
dsde(i,1:n-1) = nagad_a1w_get_derivative(e_in(1:n-1))
End Do
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 and tape
Call x10ab_a1w_f(ad_handle,ifail)
Call nagad_a1w_ir_remove
99999 Format (3X,(8F8.4))
End Program f08me_a1w_fe