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

NAG AD Library Introduction
Example description
    Program f08ke_t1w_fe

!     F08KE_T1W_F Example Program Text

!     Mark 30.0 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f08ke_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
      Integer                          :: i, ifail, lda, lwork, m, mn, n
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: a(:,:), a_in(:,:), d(:), e(:),  &
                                          taup(:), tauq(:), work(:)
      Real (Kind=nag_wp), Allocatable  :: ar(:,:), dd(:,:), de(:,:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: min
!     .. Executable Statements ..
      Write (nout,*) 'F08KE_T1W_F Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n
      lda = m
      mn = min(m,n)
      lwork = 64*(m+n)
      Allocate (a(lda,n),d(n),e(n-1),taup(n),tauq(n),work(lwork))
      Allocate (ar(m,n),a_in(m,n),dd(n,m),de(n-1,m))

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

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

      Do i = 1, m
        Call nagad_t1w_set_derivative(a_in(i,1),1.0_nag_wp)

        a = a_in

!       Reduce A to bidiagonal form
        ifail = 0
        Call f08ke_t1w_f(ad_handle,m,n,a,lda,d,e,tauq,taup,work,lwork,ifail)
        a_in(i,1)%tangent = 0.0_nag_wp
        dd(1:n,i) = d(1:n)%tangent
        de(1:n-1,i) = e(1:n-1)%tangent
      End Do

!     Print bidiagonal form
      Write (nout,*)
      Write (nout,*) 'Diagonal'
      Write (nout,99999) d(1:min(m,n))%value
      If (m>=n) Then
        Write (nout,*) 'Superdiagonal'
      Else
        Write (nout,*) 'Subdiagonal'
      End If
      Write (nout,99999) e(1:min(m,n)-1)%value

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

      Write (nout,*)
      Write (nout,*) ' Derivatives of d/e w.r.t. first column of A:'
      Write (nout,*)
      ifail = 0
      Call x04caf('General',' ',n,m,dd,n,' dD_i/dA_j1',ifail)
      Write (nout,*)
      ifail = 0
      Call x04caf('General',' ',n-1,m,de,n-1,' dE_i/dA_j1',ifail)

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

99999 Format (1X,8F9.4)
    End Program f08ke_t1w_fe