!   E04RXF Example Program Text

!   Mark 26.1 Release. NAG Copyright 2017.

    Module e04rxfe_mod

!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: monit
    Contains
      Subroutine monit(handle,rinfo,stats,iuser,ruser,cpuser,inform)

!       Monitoring function
!       Call to e04rxf to extract values for printing

!       .. Use Statements ..
        Use iso_c_binding, Only: c_ptr
        Use nag_library, Only: e04rxf, nag_wp
!       .. Scalar Arguments ..
        Type (c_ptr), Intent (In)      :: cpuser, handle
        Integer, Intent (Inout)        :: inform
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (In) :: rinfo(100), stats(100)
        Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Local Scalars ..
        Real (Kind=nag_wp)             :: tol
        Integer                        :: i, ifail, n, nout
!       .. Local Arrays ..
        Real (Kind=nag_wp), Allocatable :: x(:)
!       .. Executable Statements ..

        nout = iuser(1)
        n = iuser(2)
        tol = 1.0E-03_nag_wp
!       x is close to the solution, extract the values with e04rxf and print it
        If (rinfo(5)<tol .And. rinfo(6)<tol .And. rinfo(7)<tol) Then
          Allocate (x(n))
          ifail = 0
          Call e04rxf(handle,'Primal Variables',1,n,x,ifail)
          Write (nout,*)
          Write (nout,99999)                                                   &
            'monit() reports good approximate solution (tol =', tol, '):'
          Do i = 1, n
            Write (nout,99997) i, x(i)
          End Do
          Write (nout,99998) 'End of monit()'
        End If

        Return

99999   Format (3X,A,Es9.2,A)
99998   Format (3X,A)
99997   Format (5X,'X',I1,': ',Es9.2)
      End Subroutine monit

    End Module e04rxfe_mod

    Program e04rxfe

!     .. Use Statements ..
      Use e04rxfe_mod, Only: monit
      Use iso_c_binding, Only: c_null_ptr, c_ptr
      Use nag_library, Only: e04mtf, e04raf, e04rff, e04rhf, e04rjf, e04rzf,   &
                             e04zmf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: cpuser, handle
      Integer                          :: idlc, ifail, m, n, nnza, nnzc, nnzu
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:), bla(:), bua(:), c(:), u(:),    &
                                          x(:), xl(:), xu(:)
      Real (Kind=nag_wp)               :: h(1), rinfo(100), ruser(1),          &
                                          stats(100)
      Integer, Allocatable             :: cindex(:), icola(:), irowa(:)
      Integer                          :: icolh(1), irowh(1), iuser(2)
!     .. Executable Statements ..

      Write (nout,*) 'E04RXF Example Program Results'

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

!     read dimensions of the problem
      Read (nin,*) m, n, nnza, nnzc
      nnzu = 2*n + 2*m

!     Allocate memory
      Allocate (cindex(nnzc),icola(nnza),irowa(nnza),a(nnza),bla(m),bua(m),    &
        xl(n),xu(n),c(nnzc),x(n),u(nnzu))

!     Read problem data
      Read (nin,*) cindex(1:nnzc)
      Read (nin,*) c(1:nnzc)
      Read (nin,*) irowa(1:nnza)
      Read (nin,*) icola(1:nnza)
      Read (nin,*) a(1:nnza)
      Read (nin,*) bla(1:m)
      Read (nin,*) bua(1:m)
      Read (nin,*) xl(1:n)
      Read (nin,*) xu(1:n)

!     Create the problem handle
!     Initialize handle
      ifail = 0
      Call e04raf(handle,n,ifail)

!     set objective function
      Call e04rff(handle,nnzc,cindex,c,0,irowh,icolh,h,ifail)

!     Set box constraints
      Call e04rhf(handle,n,xl,xu,ifail)

!     Set linear constraints.
      idlc = 0
      Call e04rjf(handle,m,bla,bua,nnza,irowa,icola,a,idlc,ifail)

!     Turn on monitoring
      Call e04zmf(handle,'LPIPM Monitor Frequency = 1',ifail)

!     Print the solution at the end of the solve
      Call e04zmf(handle,'Print Solution = X',ifail)

!     Call LP interior point solver
!     The monitoring function monit calls e04rxf to extract the primal
!     and dual variables.
      cpuser = c_null_ptr
      iuser(1) = nout
      iuser(2) = n
      ifail = -1
      Call e04mtf(handle,n,x,nnzu,u,rinfo,stats,monit,iuser,ruser,cpuser,      &
        ifail)

!     Free the handle memory
      ifail = -1
      Call e04rzf(handle,ifail)


    End Program e04rxfe