*     E04NDA Example Program Text
*     Mark 20 Release. NAG Copyright 2001.
*     .. Parameters ..
      INTEGER          NIN, NOUT
      PARAMETER        (NIN=5,NOUT=6)
      INTEGER          MMAX, NMAX, NCMAX
      PARAMETER        (MMAX=10,NMAX=10,NCMAX=10)
      INTEGER          LDC, LDA
      PARAMETER        (LDC=NCMAX,LDA=MMAX)
      INTEGER          LIWORK, LWORK
      PARAMETER        (LIWORK=100,LWORK=1000)
      INTEGER          LCWSAV, LIWSAV, LLWSAV, LRWSAV
      PARAMETER        (LCWSAV=1,LIWSAV=610,LLWSAV=120,LRWSAV=475)
*     .. Local Scalars ..
      DOUBLE PRECISION OBJ
      INTEGER          I, IFAIL, INFORM, ITER, J, M, N, NCLIN
*     .. Local Arrays ..
      DOUBLE PRECISION A(LDA,NMAX), B(MMAX), BL(NMAX+NCMAX),
     +                 BU(NMAX+NCMAX), C(LDC,NMAX), CLAMDA(NMAX+NCMAX),
     +                 CVEC(NMAX), RWSAV(LRWSAV), WORK(LWORK), X(NMAX)
      INTEGER          ISTATE(NMAX+NCMAX), IWORK(LIWORK), IWSAV(LIWSAV),
     +                 KX(NMAX)
      LOGICAL          LWSAV(LLWSAV)
      CHARACTER*80     CWSAV(LCWSAV)
*     .. External Subroutines ..
      EXTERNAL         DGEMV, E04NCA, E04NDA, E04NEA, E04WBF, X04ABF
*     .. Executable Statements ..
      WRITE (NOUT,*) 'E04NDA Example Program Results'
*     Skip heading in data file
      READ (NIN,*)
      READ (NIN,*) M, N, NCLIN
      IF (M.LE.MMAX .AND. N.LE.NMAX .AND. NCLIN.LE.NCMAX) THEN
*
*        Read CVEC, A, C, BL, BU and X from data file
*
         READ (NIN,*) (CVEC(I),I=1,N)
         READ (NIN,*) ((A(I,J),J=1,N),I=1,M)
         READ (NIN,*) ((C(I,J),J=1,N),I=1,NCLIN)
         READ (NIN,*) (BL(I),I=1,N+NCLIN)
         READ (NIN,*)
         READ (NIN,*) (BU(I),I=1,N+NCLIN)
         READ (NIN,*)
         READ (NIN,*) (X(I),I=1,N)
*
*        Set the unit number for advisory messages to NOUT
*
         CALL X04ABF(1,NOUT)
*
*        Initialise using E04WBF and check for error exits
*
         IFAIL = 1
         CALL E04WBF('E04NCA',CWSAV,LCWSAV,LWSAV,LLWSAV,IWSAV,LIWSAV,
     +               RWSAV,LRWSAV,IFAIL)
         IF (IFAIL.NE.0) THEN
            WRITE (NOUT,99999) 'E04WBF terminated with IFAIL = ', IFAIL
         ELSE
*
*           Set one option using E04NEA
*
            CALL E04NEA(' Problem Type = QP2 ',LWSAV,IWSAV,RWSAV,INFORM)
            IF (INFORM.NE.0) THEN
               WRITE (NOUT,99999) 'E04NEA terminated with INFORM = ',
     +           INFORM
            ELSE
*
*              Read the options file for the remaining options
*
               CALL E04NDA(NIN,LWSAV,IWSAV,RWSAV,INFORM)
*
               IF (INFORM.NE.0) THEN
                  WRITE (NOUT,99999) 'E04NDA terminated with INFORM =',
     +              INFORM
               END IF
            END IF
            IF (INFORM.EQ.0) THEN
*
*              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)
*
*              Check for error exits
*
               WRITE (NOUT,*)
               IF (IFAIL.EQ.6) THEN
                  WRITE (NOUT,99998)
               ELSE
                  WRITE (NOUT,99997) IFAIL
                  WRITE (NOUT,*)
                  WRITE (NOUT,99996)
                  WRITE (NOUT,*)
                  DO 20 I = 1, N
                     WRITE (NOUT,99995) I, ISTATE(I), X(I), CLAMDA(I)
   20             CONTINUE
                  IF (NCLIN.GT.0) THEN
*
*                 Below is a call to the level 2 BLAS routine DGEMV.
*                 This performs the matrix vector multiplication C*X
*                 (linear constraint values) and puts the result in
*                 the first NCLIN locations of WORK.
*
                     CALL DGEMV('N',NCLIN,N,1.0D0,C,LDC,X,1,0.0D0,WORK,
     +                          1)
                     WRITE (NOUT,*)
                     WRITE (NOUT,*)
                     WRITE (NOUT,99994)
                     WRITE (NOUT,*)
                     DO 40 I = N + 1, N + NCLIN
                        J = I - N
                        WRITE (NOUT,99993) J, ISTATE(I), WORK(J),
     +                    CLAMDA(I)
   40                CONTINUE
                  END IF
                  WRITE (NOUT,*)
                  WRITE (NOUT,*)
                  WRITE (NOUT,99992) OBJ
               END IF
            END IF
         END IF
      END IF
      STOP
*
99999 FORMAT (1X,A,I3)
99998 FORMAT (1X,'An input parameter is invalid')
99997 FORMAT (1X,'E04NCA returned with IFAIL = ',I4)
99996 FORMAT (1X,'Varbl',2X,'Istate',3X,'Value',9X,'Lagr Mult')
99995 FORMAT (1X,'V',2(1X,I3),4X,1P,G14.6,2X,1P,G12.4)
99994 FORMAT (1X,'L Con',2X,'Istate',3X,'Value',9X,'Lagr Mult')
99993 FORMAT (1X,'L',2(1X,I3),4X,1P,G14.6,2X,1P,G12.4)
99992 FORMAT (1X,'Final objective value = ',G15.7)
      END
