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

NAG AD Library Introduction
Example description
    Program f06ya_t1w_fe

!     F06YA_T1W_F Example Program Text

!     Mark 28.6 Release. NAG Copyright 2022.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f06ya_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, k, lda, ldb, ldc, m, n
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: a(:,:), b(:,:), c(:,:),         &
                                          c_in(:,:)
      Real (Kind=nag_wp), Allocatable  :: ar(:,:), br(:,:), cr(:,:), dr(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'F06YA_T1W_F Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n, k
      Read (nin,*) alphar, betar
      alpha = alphar
      beta = betar
      lda = k
      ldb = n
      ldc = m
      Allocate (a(lda,m),b(ldb,k),c(ldc,n),c_in(ldc,n))
      Allocate (ar(lda,m),br(ldb,k),cr(ldc,n),dr(m,n))

!     Read A from data file
      Read (nin,*)(ar(i,1:m),i=1,k)

!     Read B from data file
      Read (nin,*)(br(i,1:k),i=1,n)

      cr(1:m,1:n) = 1.0_nag_wp
      c_in(1:m,1:n) = cr(1:m,1:n)

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

      a = ar
      b = br
      c = c_in

      Call nagad_t1w_set_derivative(alpha,1.0_nag_wp)
      ifail = 0
      Call f06ya_t1w_f(ad_handle,'T','T',m,n,k,alpha,a,lda,b,ldb,beta,c,ldc,   &
        ifail)

      Write (nout,*)
      Write (nout,*) 'Product solution C = beta*C + alpha*A^T*B^T:'
      Write (nout,*)
      cr = c
!     Print solution
      ifail = 0
      Call x04caf('G','N',m,n,cr,ldc,'updated C',ifail)

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

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

      cr(1:m,1:n) = c(1:m,1:n)%tangent

      a = ar
      b = br
      c = c_in
      alpha%tangent = 0.0_nag_wp

      Call nagad_t1w_set_derivative(beta,1.0_nag_wp)
      ifail = 0
      Call f06ya_t1w_f(ad_handle,'T','T',m,n,k,alpha,a,lda,b,ldb,beta,c,ldc,   &
        ifail)

      dr(1:m,1:n) = c(1:m,1:n)%tangent

      Call x04caf('General',' ',m,n,cr,ldc,'       dC/dalpha',ifail)
      Write (nout,*)
      Call x04caf('General',' ',m,n,dr,m,'       dC/dbeta',ifail)

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

    End Program f06ya_t1w_fe