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

NAG AD Library Introduction
Example description
    Program f06pa_t1w_fe

!     F06PA_T1W_F Example Program Text

!     Mark 29.3 Release. NAG Copyright 2023.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f06pa_t1w_f, nagad_t1w_set_derivative,          &
                               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
      Type (nagad_t1w_w_rtype)         :: alpha, beta
      Real (Kind=nag_wp)               :: alphar, betar
      Integer                          :: i, ifail, lda, m, n
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: a(:,:), b(:), y(:), y_in(:)
      Real (Kind=nag_wp), Allocatable  :: ar(:,:), br(:), da(:), db(:), yr(:)
!     .. Executable Statements ..
      Write (nout,*) 'F06PA_T1W_F Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n
      Read (nin,*) alphar, betar
      alpha = alphar
      beta = betar
      lda = m
      Allocate (a(lda,n),b(m),y(n),y_in(n))
      Allocate (ar(lda,n),br(m),yr(n),da(n),db(n))

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

!     Read the vector to be multiplied by A
      Read (nin,*) br(1:m)
      yr = 1.0_nag_wp
      y_in = yr

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

      a = ar
      b = br
      Call nagad_t1w_set_derivative(alpha,1.0_nag_wp)

      y(1:n) = y_in(1:n)
      ifail = 0
      Call f06pa_t1w_f(ad_handle,'T',m,n,alpha,a,lda,b,1,beta,y,1,ifail)

      Write (nout,*)
      Write (nout,*) 'Product solution y = beta*y + alpha*A^T*b:'
      yr = y
      Write (nout,99999) yr(1:n)
99999 Format (4X,F11.4)

!     Get derivatives
      da(1:n) = y(1:n)%tangent

      alpha = alphar
      beta = betar
      Call nagad_t1w_set_derivative(beta,1.0_nag_wp)

      y(1:n) = y_in(1:n)
      ifail = 0
      Call f06pa_t1w_f(ad_handle,'T',m,n,alpha,a,lda,b,1,beta,y,1,ifail)
      db(1:n) = y(1:n)%tangent

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

      Write (nout,*)
      Write (nout,*) ' Derivatives of y w.r.t. alpha and beta'
      Write (nout,*)

!     Setup evaluation of derivatives via adjoints
      Call x04caf('General',' ',1,n,da,1,'       dy/dalpha',ifail)
      Write (nout,*)
      Call x04caf('General',' ',1,n,db,1,'       dy/dbeta',ifail)

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

    End Program f06pa_t1w_fe