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

NAG AD Library Introduction
Example description
    Program f08ae_t1w_fe

!     F08AE_T1W_F Example Program Text
!     Mark 30.1 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f08ae_t1w_f, nagad_t1w_get, nagad_t1w_set,      &
                               nagad_t1w_w_rtype, x10aa_t1w_f, x10ab_t1w_f
      Use nag_library, Only: nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Integer                          :: i, ifail, info, j, lda, lwork, m, n
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: a(:,:), a_in(:,:), tau(:),      &
                                          work(:)
      Real (Kind=nag_wp), Allocatable  :: drda(:,:), r(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'F08AE_T1W_F Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n
      lda = m
      lwork = nb*n
      Allocate (a(lda,n),a_in(lda,n),r(n,n),drda(n,m),tau(n),work(lwork))

      ifail = 0
      Call x10aa_t1w_f(ad_handle,ifail)

!     Read A from data file

      Do i = 1, m
        Do j = 1, n
          Call nagad_t1w_set(a_in(i,j),0.0_nag_wp,1)
        End Do
      End Do

      Read (nin,*)(a_in(i,1:n)%value,i=1,m)

      a = a_in
      Do i = 1, m
        Call nagad_t1w_set(a(i,1),1.0_nag_wp,1)
!       Compute the QR factorization of A
!       The NAG name equivalent of dgeqrf_t1w is f08ae_t1w_f
        Call f08ae_t1w_f(ad_handle,m,n,a,lda,tau,work,lwork,info)
        Do j = 1, n
          Call nagad_t1w_get(a(j,j),drda(j,i),1)
        End Do
        If (i==1) Then
          r(1:n,1:n) = a(1:n,1:n)%value
!         Print R

!         ifail: behaviour on error exit
!                =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
          ifail = 0
          Call x04caf('Upper','Non',n,n,r,n,'R from QR factorization of A',    &
            ifail)
        End If
        a = a_in
      End Do

      Write (nout,*)
      Write (nout,*) 'Derivatives of diagonal of R w.r.t. first col of A'
      ifail = 0
      Call x04caf('General',' ',n,m,drda,n,'dR(i,i)/dA(1:m,1)',ifail)

      Call x10ab_t1w_f(ad_handle,ifail)
    End Program f08ae_t1w_fe