C
C     ***********************
C     SIMPLE DRIVER FOR LBFGS
C     ***********************
C
C     Example of driver for LBFGS routine, using a
C     simple test problem. The solution point is at 
C     X=(1,...,1) and the optimal function value of 0.
C
C                          JORGE NOCEDAL
C                        *** July 1990 ***
C
      PROGRAM SDRIVE
c      PARAMETER(NDIM=2000,MSAVE=7,NWORK=NDIM*(2*MSAVE +1)+2*MSAVE)
      PARAMETER(NDIM=100,MSAVE=5,NWORK=NDIM*(2*MSAVE +1)+2*MSAVE)

      DOUBLE PRECISION T1,T2
c
      INTEGER N,M,IPRINT(2),IFLAG
      DOUBLE PRECISION X(NDIM),G(NDIM),DIAG(NDIM),W(NWORK)
      DOUBLE PRECISION F,EPS,XTOL
      LOGICAL DIAGCO
      INTEGER MP,LP
      DOUBLE PRECISION GTOL,STPMIN,STPMAX
C
      DOUBLE PRECISION GNORM,STP1,FTOL,
     *        STP,YS,YY,SQ,YR,BETA,XNORM
      INTEGER ITER,NFUN,POINT,ISPT,IYPT,MAXFEV,INFO,
     *        BOUND,NPT,CP,I,NFEV,INMC,IYCN,ISCN
      LOGICAL FINISH
c
c Variables for linear earch alg - noit initialized
c
      INTEGER INFOC
      LOGICAL BRACKT,STAGE1
      DOUBLE PRECISION DG,DGM,DGINIT,DGTEST,DGX,DGXM,DGY,DGYM,
     *       FINIT,FTEST1,FM,FX,FXM,FY,FYM,STX,STY,
     *       STMIN,STMAX,WIDTH,WIDTH1
c
      N=20
      M=5
      IPRINT(1)= 1
      IPRINT(2)= 0
C
C     We do not wish to provide the diagonal matrices Hk0, and 
C     therefore set DIAGCO to FALSE.
C
      DIAGCO= .FALSE.
      EPS= 1.0D-5
      XTOL= 1.0D-16
      GTOL=0.
      MP=6
      LP=6
      GTOL=9.0D-01
      STPMIN=1.0D-20
      STPMAX=1.0D+20
      ICALL=0
      IFLAG=0
      DO 10 J=1,N,2
         X(J)=-1.2D0
         X(J+1)=1.D0
 10   CONTINUE
C
 20   CONTINUE

c      write(0,*)'**********Doing iteration iter=',ICALL+1

      F= 0.D0
      DO 30 J=1,N,2
        T1= 1.D0-X(J)
        T2= 1.D1*(X(J+1)-X(J)**2)
        G(J+1)= 2.D1*T2
        G(J)= -2.D0*(X(J)*G(J+1)+T1)
        F= F+T1**2+T2**2
 30   CONTINUE
c
      call LBFGS(N,M,IPRINT,
     *                  X,G,DIAG,W,
     *                  F,EPS,XTOL,DIAGCO,
     *                  MP,LP,GTOL,STPMIN,STPMAX,
     *                  GNORM,STP1,FTOL,
     *                  STP,YS,YY,SQ,YR,BETA,XNORM,
     *                  IFLAG,ITER,NFUN,POINT,ISPT,IYPT,MAXFEV,INFO,
     *                  BOUND,NPT,CP,I,NFEV,INMC,IYCN,ISCN,FINISH,
     *                  INFOC,BRACKT,STAGE1,
     *                  DG,DGM,DGINIT,DGTEST,DGX,DGXM,DGY,DGYM,
     *                  FINIT,FTEST1,FM,FX,FXM,FY,FYM,STX,STY,
     *                  STMIN,STMAX,WIDTH,WIDTH1)
c
      IF(IFLAG.LE.0) GO TO 50
      ICALL=ICALL + 1
C     We allow at most 2000 evaluations of F and G
      IF(ICALL.GT.2000) GO TO 50
      GO TO 20
  50  CONTINUE
      END
C
C     ** LAST LINE OF SIMPLE DRIVER (SDRIVE) **

