    PROGRAM e04ncae

!      E04NCA Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dgemv, e04nca, e04wbf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER   :: one = 1.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER   :: zero = 0.0_nag_wp
       INTEGER, PARAMETER              :: inc1 = 1, lcwsav = 1, liwsav = 610,  &
                                          llwsav = 120, lrwsav = 475, nin = 5, &
                                          nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: obj
       INTEGER                         :: i, ifail, iter, lda, ldc, liwork,    &
                                          lwork, m, n, nclin, sdc
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:), bl(:), bu(:), c(:,:),  &
                                          clamda(:), cvec(:), work(:), x(:)
       REAL (KIND=nag_wp)              :: rwsav(lrwsav)
       INTEGER, ALLOCATABLE            :: istate(:), iwork(:), kx(:)
       INTEGER                         :: iwsav(liwsav)
       LOGICAL                         :: lwsav(llwsav)
       CHARACTER (80)                  :: cwsav(lcwsav)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,*) 'E04NCA 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))

       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 E04NCA

       ifail = 0
       CALL e04wbf('E04NCA',cwsav,lcwsav,lwsav,llwsav,iwsav,liwsav,rwsav, &
          lrwsav,ifail)

!      Solve the problem

       ifail = -1
       CALL e04nca(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)

       SELECT CASE (ifail)
       CASE (0:5,7:)
!         Print variable headers
          WRITE (nout,99999)

          DO i = 1, n
             WRITE (nout,99998) i, istate(i), x(i), clamda(i)
          END DO

          IF (nclin>0) THEN

!            C*x --> work
!            The NAG name equivalent of dgemv is f06paf
             CALL dgemv('N',nclin,n,one,c,ldc,x,inc1,zero,work,inc1)

!            Print constraint headers
             WRITE (nout,99997)

             DO i = 1, nclin
                WRITE (nout,99996) i, istate(i+n), work(i), clamda(i+n)
             END DO

          END IF

          WRITE (nout,99995) obj
       END SELECT

99999  FORMAT (/1X,'Varbl',2X,'Istate',3X,'Value',9X,'Lagr Mult'/)
99998  FORMAT (1X,'V',2(1X,I3),4X,1P,G14.6,2X,1P,G12.4)
99997  FORMAT (//1X,'L Con',2X,'Istate',3X,'Value',9X,'Lagr Mult'/)
99996  FORMAT (1X,'L',2(1X,I3),4X,1P,G14.6,2X,1P,G12.4)
99995  FORMAT (//1X,'Final objective value = ',G15.7)
    END PROGRAM e04ncae
