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

NAG AD Library Introduction
Example description
    Program f08ga_a1w_fe

!     F08GA_A1W_F Example Program Text
!     Mark 29.2 Release. NAG Copyright 2023.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: abs, f08ga_a1w_f, max,                          &
                               nagad_a1w_get_derivative,                       &
                               nagad_a1w_inc_derivative,                       &
                               nagad_a1w_ir_create => x10za_a1w_f,             &
                               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,  &
                               Assignment (=), Operator (*)
      Use nag_library, Only: nag_wp, x02ajf, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
      Character (1), Parameter         :: uplo = 'U'
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_a1w_w_rtype)         :: eerrbd, eps
      Integer                          :: i, ifail, info, j, n
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: ap(:), ap_in(:), w(:), work(:)
      Type (nagad_a1w_w_rtype)         :: dummy(1,1)
      Real (Kind=nag_wp), Allocatable  :: dwda(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'F08GA_A1W_F Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n

      Allocate (ap((n*(n+1))/2),w(n),work(3*n))
      Allocate (ap_in((n*(n+1))/2),dwda(n,n))

      ifail = 0
      Call x10aa_a1w_f(ad_handle,ifail)
      Call nagad_a1w_ir_create
!     Read the upper triangular part of the matrix A from data file

      Read (nin,*)((ap_in(i+(j*(j-1))/2)%value,j=i,n),i=1,n)

      Call nagad_a1w_ir_register_variable(ap_in)
      ap = ap_in

!     Solve the symmetric eigenvalue problem
!     The NAG name equivalent of dspev_a1w is f08ga_a1w_f
      Call f08ga_a1w_f(ad_handle,'No vectors',uplo,n,ap,w,dummy,1,work,info)


      If (info==0) Then

!       Print solution

        Write (nout,*) 'Eigenvalues'
        Write (nout,99999) w(1:n)%value

!       Get the machine precision, EPS and compute the approximate
!       error bound for the computed eigenvalues.  Note that for
!       the 2-norm, max( abs(W(i)) ) = norm(A), and since the
!       eigenvalues are returned in ascending order
!       max( abs(W(i)) ) = max( abs(W(1)), abs(W(n)))

        eps = x02ajf()
        eerrbd = eps*max(abs(w(1)),abs(w(n)))

!       Print the approximate error bound for the eigenvalues

        Write (nout,*)
        Write (nout,*) 'Error estimate for the eigenvalues'
        Write (nout,99998) eerrbd%value
      Else
        Write (nout,99997) 'Failure in DSPEV_A1W. INFO =', info
        Go To 100
      End If

      Do i = 1, n
        Call nagad_a1w_ir_zero_adjoints
        Call nagad_a1w_inc_derivative(w(i),1.0_nag_wp)
        Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

        Do j = 1, n
          dwda(i,j) = nagad_a1w_get_derivative(ap_in(j*(j+1)/2))
        End Do
      End Do

      Write (nout,*)
      Write (nout,*) 'Derivatives of eigenvalues w.r.t. diagonal of A'
      ifail = 0
      Call x04caf('General',' ',n,n,dwda,n,'dW_i/dA_jj',ifail)

100   Continue
      Call nagad_a1w_ir_remove
      Call x10ab_a1w_f(ad_handle,ifail)

99999 Format (3X,(8F8.4))
99998 Format (4X,1P,6E11.1)
99997 Format (1X,A,I4)
    End Program f08ga_a1w_fe