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

NAG AD Library Introduction
Example description
    Program e04ne_p0w_fe
!     E04NE_P0W_F Example Program Text

!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: e04nc_p0w_f, e04ne_p0w_f, e04wb_p0w_f
      Use nag_library, Only: dgemv, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: lcwsav = 1, liwsav = 610,            &
                                          llwsav = 120, lrwsav = 475, nin = 5, &
                                          nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: obj
      Integer                          :: i, ifail, inform, iter, lda, ldc,    &
                                          liwork, lwork, m, n, nclin, sdc
      Logical                          :: verbose_output
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), b(:), bl(:), bu(:), c(:,:),  &
                                          clamda(:), cvec(:), rwsav(:),        &
                                          work(:), x(:)
      Integer, Allocatable             :: istate(:), iwork(:), iwsav(:), kx(:)
      Logical, Allocatable             :: lwsav(:)
      Character (80), Allocatable      :: cwsav(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max
!     .. Executable Statements ..
      Write (nout,*) 'E04NE_P0W_F Example Program Results'

!     Skip heading in data file
      Read (nin,*)

      Read (nin,*) m, n, nclin
      liwork = n
      ldc = max(1,nclin)
      lda = max(1,m)

      If (nclin>0) Then
        sdc = n
      Else
        sdc = 1
      End If

!     This particular example problem is of type LS1, so we allocate
!     A(LDA,N), CVEC(1), B(M) and define LWORK as below
      If (nclin>0) Then
        lwork = 2*n**2 + 9*n + 6*nclin
      Else
        lwork = 9*n
      End If

      Allocate (istate(n+nclin),kx(n),iwork(liwork),c(ldc,sdc),bl(n+nclin),    &
        bu(n+nclin),cvec(1),x(n),a(lda,n),b(m),clamda(n+nclin),work(lwork),    &
        iwsav(liwsav),lwsav(llwsav),cwsav(lcwsav),rwsav(lrwsav))

      Read (nin,*)(a(i,1:n),i=1,m)
      Read (nin,*) b(1:m)
      Read (nin,*)(c(i,1:sdc),i=1,nclin)
      Read (nin,*) bl(1:(n+nclin))
      Read (nin,*) bu(1:(n+nclin))
      Read (nin,*) x(1:n)

!     Initialise e04ne
      ifail = 0
      Call e04wb_p0w_f('E04NCA',cwsav,lcwsav,lwsav,llwsav,iwsav,liwsav,rwsav,  &
        lrwsav,ifail)
!     Set option via string: Control the output of the solver
      verbose_output = .False.
      If (.Not. verbose_output) Then
        Call e04ne_p0w_f('Print Level = 0',lwsav,iwsav,rwsav,inform)
      Else
        Call e04ne_p0w_f('Print Level = 1',lwsav,iwsav,rwsav,inform)
      End If

!     Solve the problem
      ifail = -1
      Call e04nc_p0w_f(ad_handle,m,n,nclin,ldc,lda,c,bl,bu,cvec,istate,kx,x,a, &
        b,iter,obj,clamda,iwork,liwork,work,lwork,lwsav,iwsav,rwsav,ifail)

!     Print the solution if the solver converged
      If (ifail==0) Then
!       Print variables
        Write (nout,99999)
        Do i = 1, n
          Write (nout,99998) i, istate(i), x(i), clamda(i)
        End Do

!       Print linear constraints
!       C*x --> work
!       The NAG name equivalent of dgemv is f06paf
        Call dgemv('N',nclin,n,1.0_nag_wp,c,ldc,x,1,0.0_nag_wp,work,1)
        Write (nout,99997)
        Do i = 1, nclin
          Write (nout,99996) i, istate(i+n), work(i), clamda(i+n)
        End Do

!       Print final objective value
        Write (nout,99995) obj
      End If

99999 Format (/,1X,'Varbl',3X,'Istate',4X,'Value',9X,'Lagr Mult')
99998 Format (1X,'V',2(1X,I3),4X,1P,E14.3,2X,1P,E12.3)
99997 Format (/,1X,'L Con',3X,'Istate',4X,'Value',9X,'Lagr Mult')
99996 Format (1X,'L',2(1X,I3),4X,1P,E14.3,2X,1P,E12.3)
99995 Format (/,1X,'Final objective value = ',1P,E15.3)
    End Program e04ne_p0w_fe