ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lbfgs.cpp
Go to the documentation of this file.
1 
13 /* lbfgs.f -- translated by f2c (version 19950110).
14  You must link the resulting object file with the libraries:
15  -lf2c -lm (in that order)
16 */
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 /* f2c.h -- Standard Fortran to C header file */
23 
28 #ifndef F2C_INCLUDE
29 #define F2C_INCLUDE
30 
31 typedef long int integer;
32 typedef char *address;
33 typedef short int shortint;
34 typedef float real;
35 typedef double doublereal;
36 typedef struct { real r, i; } complex;
37 typedef struct { doublereal r, i; } doublecomplex;
38 typedef long int logical;
39 typedef short int shortlogical;
40 typedef char logical1;
41 typedef char integer1;
42 /* typedef long long longint; */ /* system-dependent */
43 
44 #define TRUE_ (1)
45 #define FALSE_ (0)
46 
47 /* Extern is for use with -E */
48 #ifndef Extern
49 #define Extern extern
50 #endif
51 
52 /* I/O stuff */
53 
54 #ifdef f2c_i2
55 /* for -i2 */
56 typedef short flag;
57 typedef short ftnlen;
58 typedef short ftnint;
59 #else
60 typedef long int flag;
61 typedef long int ftnlen;
62 typedef long int ftnint;
63 #endif
64 
65 /*external read, write*/
66 typedef struct
67 {
68  flag cierr;
69  ftnint ciunit;
70  flag ciend;
71  char *cifmt;
72  ftnint cirec;
73 } cilist;
74 
75 /*internal read, write*/
76 typedef struct
77 {
78  flag icierr;
79  char *iciunit;
80  flag iciend;
81  char *icifmt;
82  ftnint icirlen;
83  ftnint icirnum;
84 } icilist;
85 
86 /*open*/
87 typedef struct
88 {
89  flag oerr;
90  ftnint ounit;
91  char *ofnm;
92  ftnlen ofnmlen;
93  char *osta;
94  char *oacc;
95  char *ofm;
96  ftnint orl;
97  char *oblnk;
98 } olist;
99 
100 /*close*/
101 typedef struct
102 {
103  flag cerr;
104  ftnint cunit;
105  char *csta;
106 } cllist;
107 
108 /*rewind, backspace, endfile*/
109 typedef struct
110 {
111  flag aerr;
112  ftnint aunit;
113 } alist;
114 
115 /* inquire */
116 typedef struct
117 {
118  flag inerr;
119  ftnint inunit;
120  char *infile;
121  ftnlen infilen;
122  ftnint *inex;/*parameters in standard's order*/
123  ftnint *inopen;
124  ftnint *innum;
125  ftnint *innamed;
126  char *inname;
127  ftnlen innamlen;
128  char *inacc;
129  ftnlen inacclen;
130  char *inseq;
131  ftnlen inseqlen;
132  char *indir;
133  ftnlen indirlen;
134  char *infmt;
135  ftnlen infmtlen;
136  char *inform;
137  ftnint informlen;
138  char *inunf;
139  ftnlen inunflen;
140  ftnint *inrecl;
141  ftnint *innrec;
142  char *inblank;
143  ftnlen inblanklen;
144 } inlist;
145 
146 #define VOID void
147 
148 union Multitype {/* for multiple entry points */
149  integer1 g;
150  shortint h;
151  integer i;
152  /* longint j; */
153  real r;
154  doublereal d;
157 };
158 
159 typedef union Multitype Multitype;
160 
161 /*typedef long int Long;*//* No longer used; formerly in Namelist */
162 
163 struct Vardesc {/* for Namelist */
164  char *name;
165  char *addr;
166  ftnlen *dims;
167  int type;
168 };
169 typedef struct Vardesc Vardesc;
170 
171 struct Namelist {
172  char *name;
173  Vardesc **vars;
174  int nvars;
175 };
176 typedef struct Namelist Namelist;
177 
178 #define abs(x) ((x) >= 0 ? (x) : -(x))
179 #define dabs(x) (doublereal)abs(x)
180 #define min(a,b) ((a) <= (b) ? (a) : (b))
181 #define max(a,b) ((a) >= (b) ? (a) : (b))
182 #define dmin(a,b) (doublereal)min(a,b)
183 #define dmax(a,b) (doublereal)max(a,b)
184 
185 /* procedure parameter types for -A and -C++ */
186 
187 #define F2C_proc_par_types 1
188 #ifdef __cplusplus
189 typedef int /* Unknown procedure type */ (*U_fp)(...);
190 typedef shortint (*J_fp)(...);
191 typedef integer (*I_fp)(...);
192 typedef real (*R_fp)(...);
193 typedef doublereal (*D_fp)(...), (*E_fp)(...);
194 typedef /* Complex */ VOID (*C_fp)(...);
195 typedef /* Double Complex */ VOID (*Z_fp)(...);
196 typedef logical (*L_fp)(...);
197 typedef shortlogical (*K_fp)(...);
198 typedef /* Character */ VOID (*H_fp)(...);
199 typedef /* Subroutine */ int (*S_fp)(...);
200 #else
201 typedef int /* Unknown procedure type */ (*U_fp)();
202 typedef shortint (*J_fp)();
203 typedef integer (*I_fp)();
204 typedef real (*R_fp)();
205 typedef doublereal (*D_fp)(), (*E_fp)();
206 typedef /* Complex */ VOID (*C_fp)();
207 typedef /* Double Complex */ VOID (*Z_fp)();
208 typedef logical (*L_fp)();
209 typedef shortlogical (*K_fp)();
210 typedef /* Character */ VOID (*H_fp)();
211 typedef /* Subroutine */ int (*S_fp)();
212 #endif
213 /* E_fp is for real functions when -R is not specified */
214 typedef VOID C_f;/* complex function */
215 typedef VOID H_f;/* character function */
216 typedef VOID Z_f;/* double complex function */
217 typedef doublereal E_f;/* real function with -R not specified */
218 
219 /* undef any lower-case symbols that your C compiler predefines, e.g.: */
220 
221 #ifndef Skip_f2c_Undefs
222 #undef cray
223 #undef gcos
224 #undef mc68010
225 #undef mc68020
226 #undef mips
227 #undef pdp11
228 #undef sgi
229 #undef sparc
230 #undef sun
231 #undef sun2
232 #undef sun3
233 #undef sun4
234 #undef u370
235 #undef u3b
236 #undef u3b2
237 #undef u3b5
238 #undef unix
239 #undef vax
240 #endif
241 #endif
242 /* Common Block Declarations */
243 
244 struct _lb3_1 {
247 };
248 struct _lb3_1 lb3_1 = { 6, 6, .9, 1e-20, 1e20};
249 
250 /* Table of constant values */
251 
252 static integer c__1 = 1;
253 
254 /* ----------------------------------------------------------------------*/
255 /* This file contains the LBFGS algorithm and supporting routines */
256 
257 /* **************** */
258 /* LBFGS SUBROUTINE */
259 /* **************** */
260 
261 /* Subroutine */ int lbfgs_(integer *n, integer *m, doublereal *x, doublereal
262  *f, doublereal *g, logical *diagco, doublereal *diag, integer *iprint,
263  doublereal *eps, doublereal *xtol, doublereal *w, integer *iflag,
264  integer* iter, integer * info)
265 {
266  /* Initialized data */
267 
268  static doublereal one = 1.;
269  static doublereal zero = 0.;
270 
271  /* Format strings */
272 /*
273  static char fmt_245[] = "(/\002 GTOL IS LESS THAN OR EQUAL TO 1.D-04\
274 \002,/\002 IT HAS BEEN RESET TO 9.D-01\002)";
275  static char fmt_200[] = "(/\002 IFLAG= -1 \002,/\002 LINE SEARCH FAILED.\
276  SEE DOCUMENTATION OF ROUTINE XXXXXX\002,/\002 ERROR RETURN OF LINE SEARCH: \
277 INFO= \002,i2,/\002 POSSIBLE CAUSES: FUNCTION OR GRADIENT ARE INCORRECT\002,/\
278 \002 OR INCORRECT TOLERANCES\002)";
279  static char fmt_235[] = "(/\002 IFLAG= -2\002,/\002 THE\002,i5,\002-TH D\
280 IAGONAL ELEMENT OF THE\002,/,\002 INVERSE HESSIAN APPROXIMATION IS NOT POSIT\
281 IVE\002)";
282  static char fmt_240[] = "(/\002 IFLAG= -3\002,/\002 IMPROPER INPUT PARAM\
283 ETERS (N OR M\002,\002 ARE NOT POSITIVE)\002)";
284 */
285 
286  /* System generated locals */
287  integer i__1;
288  doublereal d__1;
289 
290  /* Builtin functions */
291  //integer //s_wsfe(cilist *), e_wsfe();
292  double sqrt(doublereal);
293  //integer // do_fio(integer *, char *, ftnlen);
294 
295  /* Local variables */
296  static doublereal beta;
297  static integer inmc;
299  integer *);
300  static integer iscn, nfev, iycn;
301  static doublereal ftol;
302  static integer nfun, ispt, iypt, i, bound;
303  static doublereal gnorm;
304  extern /* Subroutine */ int daxpy_(integer *, doublereal *, doublereal *,
305  integer *, doublereal *, integer *);
306  static integer point;
307  static doublereal xnorm;
308  static integer cp;
309  static doublereal sq, yr, ys;
310  extern /* Subroutine */ int mcsrch_(integer *, doublereal *, doublereal *,
313  static logical finish;
314  static doublereal yy;
315  static integer maxfev;
316 /*
317 //Subroutine
318  extern int lb1_(integer *, integer *, integer *,
319  doublereal *, integer *, integer *, doublereal *, doublereal *,
320  doublereal *, doublereal *, logical *);
321 */
322  static integer npt;
323  static doublereal stp, stp1;
324 
325  /* Fortran I/O blocks */
326 /*
327  static cilist io___4 = { 0, 0, 0, fmt_245, 0 };
328  static cilist io___30 = { 0, 0, 0, fmt_200, 0 };
329  static cilist io___31 = { 0, 0, 0, fmt_235, 0 };
330  static cilist io___32 = { 0, 0, 0, fmt_240, 0 };
331 */
332 
333 
334 /* LIMITED MEMORY BFGS METHOD FOR LARGE SCALE OPTIMIZATION */
335 /* JORGE NOCEDAL */
336 /* *** July 1990 *** */
337 
338 
339 /* This subroutine solves the unconstrained minimization problem */
340 
341 /* min F(x), x= (x1,x2,...,xN), */
342 
343 /* using the limited memory BFGS method. The routine is especially */
344 /* effective on problems involving a large number of variables. In */
345 /* a typical iteration of this method an approximation Hk to the */
346 /* inverse of the Hessian is obtained by applying M BFGS updates to
347 */
348 /* a diagonal matrix Hk0, using information from the previous M steps.
349 */
350 /* The user specifies the number M, which determines the amount of */
351 /* storage required by the routine. The user may also provide the */
352 /* diagonal matrices Hk0 if not satisfied with the default choice. */
353 /* The algorithm is described in "On the limited memory BFGS method
354 */
355 /* for large scale optimization", by D. Liu and J. Nocedal, */
356 /* Mathematical Programming B 45 (1989) 503-528. */
357 
358 /* The user is required to calculate the function value F and its */
359 /* gradient G. In order to allow the user complete control over */
360 /* these computations, reverse communication is used. The routine */
361 /* must be called repeatedly under the control of the parameter */
362 /* IFLAG. */
363 
364 /* The steplength is determined at each iteration by means of the */
365 /* line search routine MCVSRCH, which is a slight modification of */
366 /* the routine CSRCH written by More' and Thuente. */
367 
368 /* The calling statement is */
369 
370 /* CALL LBFGS(N,M,X,F,G,DIAGCO,DIAG,IPRINT,EPS,XTOL,W,IFLAG) */
371 
372 /* where */
373 
374 /* N is an INTEGER variable that must be set by the user to the
375 */
376 /* number of variables. It is not altered by the routine. */
377 /* Restriction: N>0. */
378 
379 /* M is an INTEGER variable that must be set by the user to */
380 /* the number of corrections used in the BFGS update. It */
381 /* is not altered by the routine. Values of M less than 3 are
382 */
383 /* not recommended; large values of M will result in excessive
384  */
385 /* computing time. 3<= M <=7 is recommended. Restriction: M>0.
386  */
387 
388 /* X is a DOUBLE PRECISION array of length N. On initial entry
389 */
390 /* it must be set by the user to the values of the initial */
391 /* estimate of the solution vector. On exit with IFLAG=0, it
392 */
393 /* contains the values of the variables at the best point */
394 /* found (usually a solution). */
395 
396 /* F is a DOUBLE PRECISION variable. Before initial entry and on
397  */
398 /* a re-entry with IFLAG=1, it must be set by the user to */
399 /* contain the value of the function F at the point X. */
400 
401 /* G is a DOUBLE PRECISION array of length N. Before initial */
402 /* entry and on a re-entry with IFLAG=1, it must be set by */
403 /* the user to contain the components of the gradient G at */
404 /* the point X. */
405 
406 /* DIAGCO is a LOGICAL variable that must be set to .TRUE. if the */
407 /* user wishes to provide the diagonal matrix Hk0 at each */
408 /* iteration. Otherwise it should be set to .FALSE., in which
409 */
410 /* case LBFGS will use a default value described below. If */
411 /* DIAGCO is set to .TRUE. the routine will return at each */
412 /* iteration of the algorithm with IFLAG=2, and the diagonal
413 */
414 /* matrix Hk0 must be provided in the array DIAG. */
415 
416 
417 /* DIAG is a DOUBLE PRECISION array of length N. If DIAGCO=.TRUE.,
418 */
419 /* then on initial entry or on re-entry with IFLAG=2, DIAG */
420 /* it must be set by the user to contain the values of the */
421 /* diagonal matrix Hk0. Restriction: all elements of DIAG */
422 /* must be positive. */
423 
424 /* IPRINT is an INTEGER array of length two which must be set by the
425 */
426 /* user. */
427 
428 /* IPRINT(1) specifies the frequency of the output: */
429 /* IPRINT(1) < 0 : no output is generated, */
430 /* IPRINT(1) = 0 : output only at first and last iteration,
431  */
432 /* IPRINT(1) > 0 : output every IPRINT(1) iterations. */
433 
434 /* IPRINT(2) specifies the type of output generated: */
435 /* IPRINT(2) = 0 : iteration count, number of function */
436 /* evaluations, function value, norm of the
437  */
438 /* gradient, and steplength, */
439 /* IPRINT(2) = 1 : same as IPRINT(2)=0, plus vector of */
440 /* variables and gradient vector at the */
441 /* initial point, */
442 /* IPRINT(2) = 2 : same as IPRINT(2)=1, plus vector of */
443 /* variables, */
444 /* IPRINT(2) = 3 : same as IPRINT(2)=2, plus gradient vector
445 .*/
446 
447 
448 /* EPS is a positive DOUBLE PRECISION variable that must be set by
449  */
450 /* the user, and determines the accuracy with which the solutio
451 n*/
452 /* is to be found. The subroutine terminates when */
453 
454 /* ||G|| < EPS max(1,||X||), */
455 
456 /* where ||.|| denotes the Euclidean norm. */
457 
458 /* XTOL is a positive DOUBLE PRECISION variable that must be set by
459 */
460 /* the user to an estimate of the machine precision (e.g. */
461 /* 10**(-16) on a SUN station 3/60). The line search routine wi
462 ll*/
463 /* terminate if the relative width of the interval of uncertain
464 ty*/
465 /* is less than XTOL. */
466 
467 /* W is a DOUBLE PRECISION array of length N(2M+1)+2M used as */
468 /* workspace for LBFGS. This array must not be altered by the
469 */
470 /* user. */
471 
472 /* IFLAG is an INTEGER variable that must be set to 0 on initial entr
473 y*/
474 /* to the subroutine. A return with IFLAG<0 indicates an error,
475 */
476 /* and IFLAG=0 indicates that the routine has terminated withou
477 t*/
478 /* detecting errors. On a return with IFLAG=1, the user must
479 */
480 /* evaluate the function F and gradient G. On a return with */
481 /* IFLAG=2, the user must provide the diagonal matrix Hk0. */
482 
483 /* The following negative values of IFLAG, detecting an error,
484  */
485 /* are possible: */
486 
487 /* IFLAG=-1 The line search routine MCSRCH failed. The */
488 /* parameter INFO provides more detailed information
489 */
490 /* (see also the documentation of MCSRCH): */
491 
492 /* INFO = 0 IMPROPER INPUT PARAMETERS. */
493 
494 /* INFO = 2 RELATIVE WIDTH OF THE INTERVAL OF */
495 /* UNCERTAINTY IS AT MOST XTOL. */
496 
497 /* INFO = 3 MORE THAN 20 FUNCTION EVALUATIONS WERE
498 */
499 /* REQUIRED AT THE PRESENT ITERATION. */
500 
501 /* INFO = 4 THE STEP IS TOO SMALL. */
502 
503 /* INFO = 5 THE STEP IS TOO LARGE. */
504 
505 /* INFO = 6 ROUNDING ERRORS PREVENT FURTHER PROGRESS
506 .*/
507 /* THERE MAY NOT BE A STEP WHICH SATISFIES
508  */
509 /* THE SUFFICIENT DECREASE AND CURVATURE
510 */
511 /* CONDITIONS. TOLERANCES MAY BE TOO SMALL.
512 */
513 
514 
515 /* IFLAG=-2 The i-th diagonal element of the diagonal inverse
516 */
517 /* Hessian approximation, given in DIAG, is not */
518 /* positive. */
519 
520 /* IFLAG=-3 Improper input parameters for LBFGS (N or M are
521 */
522 /* not positive). */
523 
524 
525 
526 /* ON THE DRIVER: */
527 
528 /* The program that calls LBFGS must contain the declaration: */
529 
530 /* EXTERNAL LB2 */
531 
532 /* LB2 is a BLOCK DATA that defines the default values of several */
533 /* parameters described in the COMMON section. */
534 
535 
536 
537 /* COMMON: */
538 
539 /* The subroutine contains one common area, which the user may wish to
540  */
541 /* reference: */
542 
543 
544 /* MP is an INTEGER variable with default value 6. It is used as the
545 */
546 /* unit number for the printing of the monitoring information */
547 /* controlled by IPRINT. */
548 
549 /* LP is an INTEGER variable with default value 6. It is used as the
550 */
551 /* unit number for the printing of error messages. This printing */
552 /* may be suppressed by setting LP to a non-positive value. */
553 
554 /* GTOL is a DOUBLE PRECISION variable with default value 0.9, which */
555 /* controls the accuracy of the line search routine MCSRCH. If the
556 */
557 /* function and gradient evaluations are inexpensive with respect
558 */
559 /* to the cost of the iteration (which is sometimes the case when
560 */
561 /* solving very large problems) it may be advantageous to set GTOL
562 */
563 /* to a small value. A typical small value is 0.1. Restriction: */
564 /* GTOL should be greater than 1.D-04. */
565 
566 /* STPMIN and STPMAX are non-negative DOUBLE PRECISION variables which
567 */
568 /* specify lower and uper bounds for the step in the line search.
569 */
570 /* Their default values are 1.D-20 and 1.D+20, respectively. These
571 */
572 /* values need not be modified unless the exponents are too large
573 */
574 /* for the machine being used, or unless the problem is extremely
575 */
576 /* badly scaled (in which case the exponents should be increased).
577 */
578 
579 
580 /* MACHINE DEPENDENCIES */
581 
582 /* The only variables that are machine-dependent are XTOL, */
583 /* STPMIN and STPMAX. */
584 
585 
586 /* GENERAL INFORMATION */
587 
588 /* Other routines called directly: DAXPY, DDOT, LB1, MCSRCH */
589 
590 /* Input/Output : No input; diagnostic messages on unit MP and */
591 /* error messages on unit LP. */
592 
593 
594 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
595 -*/
596 
597 
598  /* Parameter adjustments */
599  --diag;
600  --g;
601  --x;
602  --w;
603  --iprint;
604 
605  /* Function Body */
606 
607 /* INITIALIZE */
608 /* ---------- */
609 
610  if (*iflag == 0) {
611  goto L10;
612  }
613  switch (*iflag) {
614  case 1: goto L172;
615  case 2: goto L100;
616  }
617 L10:
618  //iter = 0;
619  if (*n <= 0 || *m <= 0) {
620  goto L196;
621  }
622  if (lb3_1.gtol <= 1e-4) {
623  if (lb3_1.lp > 0) {
624  //io___4.ciunit = lb3_1.lp;
625  //s_wsfe(&io___4);
626  //e_wsfe();
627  }
628  lb3_1.gtol = .9;
629  }
630  nfun = 1;
631  point = 0;
632  finish = FALSE_;
633  if (*diagco) {
634  i__1 = *n;
635  for (i = 1; i <= i__1; ++i) {
636 /* L30: */
637  if (diag[i] <= zero) {
638  goto L195;
639  }
640  }
641  } else {
642  i__1 = *n;
643  for (i = 1; i <= i__1; ++i) {
644 /* L40: */
645  diag[i] = 1.;
646  }
647  }
648 
649 /* THE WORK VECTOR W IS DIVIDED AS FOLLOWS: */
650 /* --------------------------------------- */
651 /* THE FIRST N LOCATIONS ARE USED TO STORE THE GRADIENT AND */
652 /* OTHER TEMPORARY INFORMATION. */
653 /* LOCATIONS (N+1)...(N+M) STORE THE SCALARS RHO. */
654 /* LOCATIONS (N+M+1)...(N+2M) STORE THE NUMBERS ALPHA USED */
655 /* IN THE FORMULA THAT COMPUTES H*G. */
656 /* LOCATIONS (N+2M+1)...(N+2M+NM) STORE THE LAST M SEARCH */
657 /* STEPS. */
658 /* LOCATIONS (N+2M+NM+1)...(N+2M+2NM) STORE THE LAST M */
659 /* GRADIENT DIFFERENCES. */
660 
661 /* THE SEARCH STEPS AND GRADIENT DIFFERENCES ARE STORED IN A */
662 /* CIRCULAR ORDER CONTROLLED BY THE PARAMETER POINT. */
663 
664  ispt = *n + (*m << 1);
665  iypt = ispt + *n * *m;
666  i__1 = *n;
667  for (i = 1; i <= i__1; ++i) {
668 /* L50: */
669  w[ispt + i] = -g[i] * diag[i];
670  }
671  gnorm = sqrt(ddot_(n, &g[1], &c__1, &g[1], &c__1));
672  stp1 = one / gnorm;
673 
674 /* PARAMETERS FOR LINE SEARCH ROUTINE */
675 
676  ftol = 1e-4;
677  maxfev = 20;
678 
679  /*
680  if (iprint[1] >= 0) {
681  lb1_(&iprint[1], &iter, &nfun, &gnorm, n, m, &x[1], f, &g[1], &stp, &
682  finish);
683  }
684  */
685 
686 /* -------------------- */
687 /* MAIN ITERATION LOOP */
688 /* -------------------- */
689 
690 L80:
691  ++(*iter);
692  *info = 0;
693  bound = *iter - 1;
694  if (*iter == 1) {
695  goto L165;
696  }
697  if (*iter > *m) {
698  bound = *m;
699  }
700 
701  ys = ddot_(n, &w[iypt + npt + 1], &c__1, &w[ispt + npt + 1], &c__1);
702  if (ys==0.0)
703  ys=1.e-10;
704  if (! (*diagco)) {
705  yy = ddot_(n, &w[iypt + npt + 1], &c__1, &w[iypt + npt + 1], &c__1);
706  i__1 = *n;
707  for (i = 1; i <= i__1; ++i) {
708 /* L90: */
709  diag[i] = ys / yy;
710  }
711  } else {
712  *iflag = 2;
713  return 0;
714  }
715 L100:
716  if (*diagco) {
717  i__1 = *n;
718  for (i = 1; i <= i__1; ++i) {
719 /* L110: */
720  if (diag[i] <= zero) {
721  goto L195;
722  }
723  }
724  }
725 
726 /* COMPUTE -H*G USING THE FORMULA GIVEN IN: Nocedal, J. 1980, */
727 /* "Updating quasi-Newton matrices with limited storage", */
728 /* Mathematics of Computation, Vol.24, No.151, pp. 773-782. */
729 /* --------------------------------------------------------- */
730 
731  cp = point;
732  if (point == 0) {
733  cp = *m;
734  }
735  w[*n + cp] = one / ys;
736  i__1 = *n;
737  for (i = 1; i <= i__1; ++i) {
738 /* L112: */
739  w[i] = -g[i];
740  }
741  cp = point;
742  i__1 = bound;
743  for (i = 1; i <= i__1; ++i) {
744  --cp;
745  if (cp == -1) {
746  cp = *m - 1;
747  }
748  sq = ddot_(n, &w[ispt + cp * *n + 1], &c__1, &w[1], &c__1);
749  inmc = *n + *m + cp + 1;
750  iycn = iypt + cp * *n;
751  w[inmc] = w[*n + cp + 1] * sq;
752  d__1 = -w[inmc];
753  daxpy_(n, &d__1, &w[iycn + 1], &c__1, &w[1], &c__1);
754 /* L125: */
755  }
756 
757  i__1 = *n;
758  for (i = 1; i <= i__1; ++i) {
759 /* L130: */
760  w[i] = diag[i] * w[i];
761  }
762 
763  i__1 = bound;
764  for (i = 1; i <= i__1; ++i) {
765  yr = ddot_(n, &w[iypt + cp * *n + 1], &c__1, &w[1], &c__1);
766  beta = w[*n + cp + 1] * yr;
767  inmc = *n + *m + cp + 1;
768  beta = w[inmc] - beta;
769  iscn = ispt + cp * *n;
770  daxpy_(n, &beta, &w[iscn + 1], &c__1, &w[1], &c__1);
771  ++cp;
772  if (cp == *m) {
773  cp = 0;
774  }
775 /* L145: */
776  }
777 
778 /* STORE THE NEW SEARCH DIRECTION */
779 /* ------------------------------ */
780 
781  i__1 = *n;
782  for (i = 1; i <= i__1; ++i) {
783 /* L160: */
784  w[ispt + point * *n + i] = w[i];
785  }
786 
787 /* OBTAIN THE ONE-DIMENSIONAL MINIMIZER OF THE FUNCTION */
788 /* BY USING THE LINE SEARCH ROUTINE MCSRCH */
789 /* ---------------------------------------------------- */
790 L165:
791  nfev = 0;
792  stp = one;
793  if (*iter == 1) {
794  stp = stp1;
795  }
796  i__1 = *n;
797  for (i = 1; i <= i__1; ++i) {
798 /* L170: */
799  w[i] = g[i];
800  }
801 L172:
802  mcsrch_(n, &x[1], f, &g[1], &w[ispt + point * *n + 1], &stp, &ftol, xtol,
803  &maxfev, info, &nfev, &diag[1]);
804  if (*info == -1) {
805  *iflag = 1;
806  return 0;
807  }
808  if (*info != 1) {
809  goto L190;
810  }
811  nfun += nfev;
812 
813 /* COMPUTE THE NEW STEP AND GRADIENT CHANGE */
814 /* ----------------------------------------- */
815 
816  npt = point * *n;
817  i__1 = *n;
818  for (i = 1; i <= i__1; ++i) {
819  w[ispt + npt + i] = stp * w[ispt + npt + i];
820 /* L175: */
821  w[iypt + npt + i] = g[i] - w[i];
822  }
823  ++point;
824  if (point == *m) {
825  point = 0;
826  }
827 
828 /* TERMINATION TEST */
829 /* ---------------- */
830 
831  gnorm = sqrt(ddot_(n, &g[1], &c__1, &g[1], &c__1));
832  xnorm = sqrt(ddot_(n, &x[1], &c__1, &x[1], &c__1));
833  xnorm = max(1.,xnorm);
834  if (gnorm / xnorm <= *eps) {
835  finish = TRUE_;
836  }
837 
838  /*
839  if (iprint[1] >= 0) {
840  lb1_(&iprint[1], &iter, &nfun, &gnorm, n, m, &x[1], f, &g[1], &stp, &
841  finish);
842  }
843  */
844  if (finish) {
845  *iflag = 0;
846  return 0;
847  }
848  goto L80;
849 
850 /* ------------------------------------------------------------ */
851 /* END OF MAIN ITERATION LOOP. ERROR EXITS. */
852 /* ------------------------------------------------------------ */
853 
854 L190:
855  *iflag = -1;
856  if (lb3_1.lp > 0) {
857  //io___30.ciunit = lb3_1.lp;
858  //s_wsfe(&io___30);
860  //e_wsfe();
861  }
862  return 0;
863 L195:
864  *iflag = -2;
865  if (lb3_1.lp > 0) {
866  //io___31.ciunit = lb3_1.lp;
867  //s_wsfe(&io___31);
869  //e_wsfe();
870  }
871  return 0;
872 L196:
873  *iflag = -3;
874  if (lb3_1.lp > 0) {
875  //io___32.ciunit = lb3_1.lp;
876  //s_wsfe(&io___32);
877  //e_wsfe();
878  }
879 
880 /* FORMATS */
881 /* ------- */
882 
883  return 0;
884 } /* lbfgs_ */
885 
886 
887 /* LAST LINE OF SUBROUTINE LBFGS */
888 
889 
890 /*
891 //Subroutine
892 int lb1_(integer *iprint, integer *iter, integer *nfun,
893  doublereal *gnorm, integer *n, integer *m, doublereal *x, doublereal *
894  f, doublereal *g, doublereal *stp, logical *finish)
895 {
896  // Format strings
897  static char fmt_10[] = "(\002*******************************************\
898 ******\002)";
899  static char fmt_20[] = "(\002 N=\002,i5,\002 NUMBER OF CORRECTIONS\
900 =\002,i2,/,\002 INITIAL VALUES\002)";
901  static char fmt_30[] = "(\002 F= \002,1pd10.3,\002 GNORM= \002,1pd10.3)"
902  ;
903  static char fmt_40[] = "(\002 VECTOR X= \002)";
904  static char fmt_50[] = "(6(2x,1pd10.3))";
905  static char fmt_60[] = "(\002 GRADIENT VECTOR G= \002)";
906  static char fmt_70[] = "(/\002 I NFN\002,4x,\002FUNC\002,8x,\002GN\
907 ORM\002,7x,\002STEPLENGTH\002/)";
908  static char fmt_80[] = "(2(i4,1x),3x,3(1pd10.3,2x))";
909  static char fmt_90[] = "(\002 FINAL POINT X= \002)";
910  static char fmt_100[] = "(/\002 THE MINIMIZATION TERMINATED WITHOUT DETE\
911 CTING ERRORS.\002,/\002 IFLAG = 0\002)";
912 
913  // System generated locals
914  integer i__1;
915 
916  // Builtin functions
917  //integer //s_wsfe(cilist *), e_wsfe(), // do_fio(integer *, char *, ftnlen);
918 
919  // Local variables
920  static integer i;
921 
922  // Fortran I/O blocks
923  static cilist io___33 = { 0, 0, 0, fmt_10, 0 };
924  static cilist io___34 = { 0, 0, 0, fmt_20, 0 };
925  static cilist io___35 = { 0, 0, 0, fmt_30, 0 };
926  static cilist io___36 = { 0, 0, 0, fmt_40, 0 };
927  static cilist io___37 = { 0, 0, 0, fmt_50, 0 };
928  static cilist io___39 = { 0, 0, 0, fmt_60, 0 };
929  static cilist io___40 = { 0, 0, 0, fmt_50, 0 };
930  static cilist io___41 = { 0, 0, 0, fmt_10, 0 };
931  static cilist io___42 = { 0, 0, 0, fmt_70, 0 };
932  static cilist io___43 = { 0, 0, 0, fmt_70, 0 };
933  static cilist io___44 = { 0, 0, 0, fmt_80, 0 };
934  static cilist io___45 = { 0, 0, 0, fmt_70, 0 };
935  static cilist io___46 = { 0, 0, 0, fmt_80, 0 };
936  static cilist io___47 = { 0, 0, 0, fmt_90, 0 };
937  static cilist io___48 = { 0, 0, 0, fmt_40, 0 };
938  static cilist io___49 = { 0, 0, 0, fmt_50, 0 };
939  static cilist io___50 = { 0, 0, 0, fmt_60, 0 };
940  static cilist io___51 = { 0, 0, 0, fmt_50, 0 };
941  static cilist io___52 = { 0, 0, 0, fmt_100, 0 };
942 
943 
944 
945 // -------------------------------------------------------------
946 // THIS ROUTINE PRINTS MONITORING INFORMATION. THE FREQUENCY AND
947 // AMOUNT OF OUTPUT ARE CONTROLLED BY IPRINT.
948 // -------------------------------------------------------------
949 
950 
951  // Parameter adjustments
952  --iprint;
953  --g;
954  --x;
955 
956  // Function Body
957  if (*iter == 0) {
958  io___33.ciunit = lb3_1.mp;
959  // s_wsfe(&io___33);
960  // e_wsfe();
961  io___34.ciunit = lb3_1.mp;
962  // s_wsfe(&io___34);
963  // do_fio(&c__1, (char *)&(*n), (ftnlen)sizeof(integer));
964  // do_fio(&c__1, (char *)&(*m), (ftnlen)sizeof(integer));
965  // e_wsfe();
966  io___35.ciunit = lb3_1.mp;
967  // s_wsfe(&io___35);
968  // do_fio(&c__1, (char *)&(*f), (ftnlen)sizeof(doublereal));
969  // do_fio(&c__1, (char *)&(*gnorm), (ftnlen)sizeof(doublereal));
970  // e_wsfe();
971  if (iprint[2] >= 1) {
972  io___36.ciunit = lb3_1.mp;
973  //s_wsfe(&io___36);
974  // e_wsfe();
975  io___37.ciunit = lb3_1.mp;
976  //s_wsfe(&io___37);
977  i__1 = *n;
978  for (i = 1; i <= i__1; ++i) {
979  // do_fio(&c__1, (char *)&x[i], (ftnlen)sizeof(doublereal));
980  }
981  // e_wsfe();
982  io___39.ciunit = lb3_1.mp;
983  //s_wsfe(&io___39);
984  // e_wsfe();
985  io___40.ciunit = lb3_1.mp;
986  //s_wsfe(&io___40);
987  i__1 = *n;
988  for (i = 1; i <= i__1; ++i) {
989  // do_fio(&c__1, (char *)&g[i], (ftnlen)sizeof(doublereal));
990  }
991  // e_wsfe();
992  }
993  io___41.ciunit = lb3_1.mp;
994  // s_wsfe(&io___41);
995  // e_wsfe();
996  io___42.ciunit = lb3_1.mp;
997  // s_wsfe(&io___42);
998  // e_wsfe();
999  } else {
1000  if (iprint[1] == 0 && (*iter != 1 && ! (*finish))) {
1001  return 0;
1002  }
1003  if (iprint[1] != 0) {
1004  if ((*iter - 1) % iprint[1] == 0 || *finish) {
1005  if (iprint[2] > 1 && *iter > 1) {
1006  io___43.ciunit = lb3_1.mp;
1007  //s_wsfe(&io___43);
1008  // e_wsfe();
1009  }
1010  io___44.ciunit = lb3_1.mp;
1011  // s_wsfe(&io___44);
1012  // do_fio(&c__1, (char *)&(*iter), (ftnlen)sizeof(integer));
1013  // do_fio(&c__1, (char *)&(*nfun), (ftnlen)sizeof(integer));
1014  // do_fio(&c__1, (char *)&(*f), (ftnlen)sizeof(doublereal));
1015  // do_fio(&c__1, (char *)&(*gnorm), (ftnlen)sizeof(doublereal));
1016  // do_fio(&c__1, (char *)&(*stp), (ftnlen)sizeof(doublereal));
1017  // e_wsfe();
1018  } else {
1019  return 0;
1020  }
1021  } else {
1022  if (iprint[2] > 1 && *finish) {
1023  io___45.ciunit = lb3_1.mp;
1024  // s_wsfe(&io___45);
1025  // e_wsfe();
1026  }
1027  io___46.ciunit = lb3_1.mp;
1028  //s_wsfe(&io___46);
1029  // do_fio(&c__1, (char *)&(*iter), (ftnlen)sizeof(integer));
1030  // do_fio(&c__1, (char *)&(*nfun), (ftnlen)sizeof(integer));
1031  // do_fio(&c__1, (char *)&(*f), (ftnlen)sizeof(doublereal));
1032  // do_fio(&c__1, (char *)&(*gnorm), (ftnlen)sizeof(doublereal));
1033  // do_fio(&c__1, (char *)&(*stp), (ftnlen)sizeof(doublereal));
1034  // e_wsfe();
1035  }
1036  if (iprint[2] == 2 || iprint[2] == 3) {
1037  if (*finish) {
1038  io___47.ciunit = lb3_1.mp;
1039  // s_wsfe(&io___47);
1040  // e_wsfe();
1041  } else {
1042  io___48.ciunit = lb3_1.mp;
1043  // s_wsfe(&io___48);
1044  // e_wsfe();
1045  }
1046  io___49.ciunit = lb3_1.mp;
1047  //s_wsfe(&io___49);
1048  i__1 = *n;
1049  for (i = 1; i <= i__1; ++i) {
1050  // do_fio(&c__1, (char *)&x[i], (ftnlen)sizeof(doublereal));
1051  }
1052  // e_wsfe();
1053  if (iprint[2] == 3) {
1054  io___50.ciunit = lb3_1.mp;
1055  // s_wsfe(&io___50);
1056  // e_wsfe();
1057  io___51.ciunit = lb3_1.mp;
1058  // s_wsfe(&io___51);
1059  i__1 = *n;
1060  for (i = 1; i <= i__1; ++i) {
1061  // do_fio(&c__1, (char *)&g[i], (ftnlen)sizeof(doublereal));
1062  }
1063  // e_wsfe();
1064  }
1065  }
1066  if (*finish) {
1067  io___52.ciunit = lb3_1.mp;
1068  //s_wsfe(&io___52);
1069  // e_wsfe();
1070  }
1071  }
1072 
1073 
1074  return 0;
1075 }
1076 */
1077 
1078 /* ****** */
1079 
1080 
1081 /* ---------------------------------------------------------- */
1082 /* DATA */
1083 /* ---------------------------------------------------------- */
1084 
1085 
1086 
1087 
1088 /* ---------------------------------------------------------- */
1089 
1090 /* Subroutine */ int daxpy_(integer *n, doublereal *da, doublereal *dx,
1091  integer *incx, doublereal *dy, integer *incy)
1092 {
1093  /* System generated locals */
1094  integer i__1;
1095 
1096  /* Local variables */
1097  static integer i, m, ix, iy, mp1;
1098 
1099 
1100 /* constant times a vector plus a vector. */
1101 /* uses unrolled loops for increments equal to one. */
1102 /* jack dongarra, linpack, 3/11/78. */
1103 
1104 
1105  /* Parameter adjustments */
1106  --dy;
1107  --dx;
1108 
1109  /* Function Body */
1110  if (*n <= 0) {
1111  return 0;
1112  }
1113  if (*da == 0.) {
1114  return 0;
1115  }
1116  if (*incx == 1 && *incy == 1) {
1117  goto L20;
1118  }
1119 
1120 /* code for unequal increments or equal increments */
1121 /* not equal to 1 */
1122 
1123  ix = 1;
1124  iy = 1;
1125  if (*incx < 0) {
1126  ix = (-(*n) + 1) * *incx + 1;
1127  }
1128  if (*incy < 0) {
1129  iy = (-(*n) + 1) * *incy + 1;
1130  }
1131  i__1 = *n;
1132  for (i = 1; i <= i__1; ++i) {
1133  dy[iy] += *da * dx[ix];
1134  ix += *incx;
1135  iy += *incy;
1136 /* L10: */
1137  }
1138  return 0;
1139 
1140 /* code for both increments equal to 1 */
1141 
1142 
1143 /* clean-up loop */
1144 
1145 L20:
1146  m = *n % 4;
1147  if (m == 0) {
1148  goto L40;
1149  }
1150  i__1 = m;
1151  for (i = 1; i <= i__1; ++i) {
1152  dy[i] += *da * dx[i];
1153 /* L30: */
1154  }
1155  if (*n < 4) {
1156  return 0;
1157  }
1158 L40:
1159  mp1 = m + 1;
1160  i__1 = *n;
1161  for (i = mp1; i <= i__1; i += 4) {
1162  dy[i] += *da * dx[i];
1163  dy[i + 1] += *da * dx[i + 1];
1164  dy[i + 2] += *da * dx[i + 2];
1165  dy[i + 3] += *da * dx[i + 3];
1166 /* L50: */
1167  }
1168  return 0;
1169 } /* daxpy_ */
1170 
1171 
1172 
1173 /* ---------------------------------------------------------- */
1174 
1176  integer *incy)
1177 {
1178  /* System generated locals */
1179  integer i__1;
1180  doublereal ret_val;
1181 
1182  /* Local variables */
1183  static integer i, m;
1184  static doublereal dtemp;
1185  static integer ix, iy, mp1;
1186 
1187 
1188 /* forms the dot product of two vectors. */
1189 /* uses unrolled loops for increments equal to one. */
1190 /* jack dongarra, linpack, 3/11/78. */
1191 
1192 
1193  /* Parameter adjustments */
1194  --dy;
1195  --dx;
1196 
1197  /* Function Body */
1198  ret_val = 0.;
1199  dtemp = 0.;
1200  if (*n <= 0) {
1201  return ret_val;
1202  }
1203  if (*incx == 1 && *incy == 1) {
1204  goto L20;
1205  }
1206 
1207 /* code for unequal increments or equal increments */
1208 /* not equal to 1 */
1209 
1210  ix = 1;
1211  iy = 1;
1212  if (*incx < 0) {
1213  ix = (-(*n) + 1) * *incx + 1;
1214  }
1215  if (*incy < 0) {
1216  iy = (-(*n) + 1) * *incy + 1;
1217  }
1218  i__1 = *n;
1219  for (i = 1; i <= i__1; ++i) {
1220  dtemp += dx[ix] * dy[iy];
1221  ix += *incx;
1222  iy += *incy;
1223 /* L10: */
1224  }
1225  ret_val = dtemp;
1226  return ret_val;
1227 
1228 /* code for both increments equal to 1 */
1229 
1230 
1231 /* clean-up loop */
1232 
1233 L20:
1234  m = *n % 5;
1235  if (m == 0) {
1236  goto L40;
1237  }
1238  i__1 = m;
1239  for (i = 1; i <= i__1; ++i) {
1240  dtemp += dx[i] * dy[i];
1241 /* L30: */
1242  }
1243  if (*n < 5) {
1244  goto L60;
1245  }
1246 L40:
1247  mp1 = m + 1;
1248  i__1 = *n;
1249  for (i = mp1; i <= i__1; i += 5) {
1250  dtemp = dtemp + dx[i] * dy[i] + dx[i + 1] * dy[i + 1] + dx[i + 2] *
1251  dy[i + 2] + dx[i + 3] * dy[i + 3] + dx[i + 4] * dy[i + 4];
1252 /* L50: */
1253  }
1254 L60:
1255  ret_val = dtemp;
1256  return ret_val;
1257 } /* ddot_ */
1258 
1259 /* ------------------------------------------------------------------ */
1260 
1261 /* ************************** */
1262 /* LINE SEARCH ROUTINE MCSRCH */
1263 /* ************************** */
1264 
1265 /* Subroutine */ int mcsrch_(integer *n, doublereal *x, doublereal *f,
1266  doublereal *g, doublereal *s, doublereal *stp, doublereal *ftol,
1267  doublereal *xtol, integer *maxfev, integer *info, integer *nfev,
1268  doublereal *wa)
1269 {
1270  /* Initialized data */
1271 
1272  static doublereal p5 = .5;
1273  static doublereal p66 = .66;
1274  static doublereal xtrapf = 4.;
1275  static doublereal zero = 0.;
1276 
1277  /* Format strings */
1278 /*
1279  static char fmt_15[] = "(/\002 THE SEARCH DIRECTION IS NOT A DESCENT DI\
1280 RECTION\002)";
1281 */
1282 
1283  /* System generated locals */
1284  integer i__1;
1285  doublereal d__1;
1286 
1287  /* Builtin functions */
1288  //integer //s_wsfe(cilist *), e_wsfe();
1289 
1290  /* Local variables */
1291  static doublereal dgxm, dgym;
1292  static integer j, infoc;
1293  static doublereal finit, width, stmin, stmax;
1294  static logical stage1;
1295  static doublereal width1, ftest1, dg, fm, fx, fy;
1296  static logical brackt;
1297  static doublereal dginit, dgtest;
1298  extern /* Subroutine */ int mcstep_(doublereal *, doublereal *,
1301  doublereal *, integer *);
1302  static doublereal dgm, dgx, dgy, fxm, fym, stx, sty;
1303 
1304  /* Fortran I/O blocks */
1305  //static cilist io___71 = { 0, 0, 0, fmt_15, 0 };
1306 
1307 
1308 
1309 /* SUBROUTINE MCSRCH */
1310 
1311 /* A slight modification of the subroutine CSRCH of More' and Thuente.
1312  */
1313 /* The changes are to allow reverse communication, and do not affect
1314 */
1315 /* NFEV IS AN INTEGER OUTPUT VARIABLE SET TO THE NUMBER OF */
1316 /* CALLS TO FCN. */
1317 
1318 /* WA IS A WORK ARRAY OF LENGTH N. */
1319 
1320 /* SUBPROGRAMS CALLED */
1321 
1322 /* MCSTEP */
1323 
1324 /* FORTRAN-SUPPLIED...ABS,MAX,MIN */
1325 
1326 /* ARGONNE NATIONAL LABORATORY. MINPACK PROJECT. JUNE 1983 */
1327 /* JORGE J. MORE', DAVID J. THUENTE */
1328 
1329 /* ********** */
1330  /* Parameter adjustments */
1331  --wa;
1332  --s;
1333  --g;
1334  --x;
1335 
1336  /* Function Body */
1337  if (*info == -1) {
1338  goto L45;
1339  }
1340  infoc = 1;
1341 
1342 /* CHECK THE INPUT PARAMETERS FOR ERRORS. */
1343 
1344  if (*n <= 0 || *stp <= zero || *ftol < zero || lb3_1.gtol < zero || *xtol
1345  < zero || lb3_1.stpmin < zero || lb3_1.stpmax < lb3_1.stpmin || *
1346  maxfev <= 0) {
1347  return 0;
1348  }
1349 
1350 /* COMPUTE THE INITIAL GRADIENT IN THE SEARCH DIRECTION */
1351 /* AND CHECK THAT S IS A DESCENT DIRECTION. */
1352 
1353  dginit = zero;
1354  i__1 = *n;
1355  for (j = 1; j <= i__1; ++j) {
1356  dginit += g[j] * s[j];
1357 /* L10: */
1358  }
1359  if (dginit >= zero) {
1360  //io___71.ciunit = lb3_1.lp;
1361  // s_wsfe(&io___71);
1362  // e_wsfe();
1363  return 0;
1364  }
1365 
1366 /* INITIALIZE LOCAL VARIABLES. */
1367 
1368  brackt = FALSE_;
1369  stage1 = TRUE_;
1370  *nfev = 0;
1371  finit = *f;
1372  dgtest = *ftol * dginit;
1373  width = lb3_1.stpmax - lb3_1.stpmin;
1374  width1 = width / p5;
1375  i__1 = *n;
1376  for (j = 1; j <= i__1; ++j) {
1377  wa[j] = x[j];
1378 /* L20: */
1379  }
1380 
1381 /* THE VARIABLES STX, FX, DGX CONTAIN THE VALUES OF THE STEP, */
1382 /* FUNCTION, AND DIRECTIONAL DERIVATIVE AT THE BEST STEP. */
1383 /* THE VARIABLES STY, FY, DGY CONTAIN THE VALUE OF THE STEP, */
1384 /* FUNCTION, AND DERIVATIVE AT THE OTHER ENDPOINT OF */
1385 /* THE INTERVAL OF UNCERTAINTY. */
1386 /* THE VARIABLES STP, F, DG CONTAIN THE VALUES OF THE STEP, */
1387 /* FUNCTION, AND DERIVATIVE AT THE CURRENT STEP. */
1388 
1389  stx = zero;
1390  fx = finit;
1391  dgx = dginit;
1392  sty = zero;
1393  fy = finit;
1394  dgy = dginit;
1395 
1396 /* START OF ITERATION. */
1397 
1398 L30:
1399 
1400 /* SET THE MINIMUM AND MAXIMUM STEPS TO CORRESPOND */
1401 /* TO THE PRESENT INTERVAL OF UNCERTAINTY. */
1402 
1403  if (brackt) {
1404  stmin = min(stx,sty);
1405  stmax = max(stx,sty);
1406  } else {
1407  stmin = stx;
1408  stmax = *stp + xtrapf * (*stp - stx);
1409  }
1410 
1411 /* FORCE THE STEP TO BE WITHIN THE BOUNDS STPMAX AND STPMIN. */
1412 
1413  *stp = max(*stp,lb3_1.stpmin);
1414  *stp = min(*stp,lb3_1.stpmax);
1415 
1416 /* IF AN UNUSUAL TERMINATION IS TO OCCUR THEN LET */
1417 /* STP BE THE LOWEST POINT OBTAINED SO FAR. */
1418 
1419  if ( ( brackt && (*stp <= stmin || *stp >= stmax) ) || *nfev >= *maxfev - 1 ||
1420  infoc == 0 || (brackt && stmax - stmin <= *xtol * stmax) ) {
1421  *stp = stx;
1422  }
1423 
1424 /* EVALUATE THE FUNCTION AND GRADIENT AT STP */
1425 /* AND COMPUTE THE DIRECTIONAL DERIVATIVE. */
1426 /* We return to main program to obtain F and G. */
1427 
1428  i__1 = *n;
1429  for (j = 1; j <= i__1; ++j) {
1430  x[j] = wa[j] + *stp * s[j];
1431 /* L40: */
1432  }
1433  *info = -1;
1434  return 0;
1435 
1436 L45:
1437  *info = 0;
1438  ++(*nfev);
1439  dg = zero;
1440  i__1 = *n;
1441  for (j = 1; j <= i__1; ++j) {
1442  dg += g[j] * s[j];
1443 /* L50: */
1444  }
1445  ftest1 = finit + *stp * dgtest;
1446 
1447 /* TEST FOR CONVERGENCE. */
1448 
1449  if ( (brackt && (*stp <= stmin || *stp >= stmax)) || infoc == 0) {
1450  *info = 6;
1451  }
1452  if (*stp == lb3_1.stpmax && *f <= ftest1 && dg <= dgtest) {
1453  *info = 5;
1454  }
1455  if (*stp == lb3_1.stpmin && (*f > ftest1 || dg >= dgtest)) {
1456  *info = 4;
1457  }
1458  if (*nfev >= *maxfev) {
1459  *info = 3;
1460  }
1461  if (brackt && stmax - stmin <= *xtol * stmax) {
1462  *info = 2;
1463  }
1464  if (*f <= ftest1 && abs(dg) <= lb3_1.gtol * (-dginit)) {
1465  *info = 1;
1466  }
1467 
1468 /* CHECK FOR TERMINATION. */
1469 
1470  if (*info != 0) {
1471  return 0;
1472  }
1473 
1474 /* IN THE FIRST STAGE WE SEEK A STEP FOR WHICH THE MODIFIED */
1475 /* FUNCTION HAS A NONPOSITIVE VALUE AND NONNEGATIVE DERIVATIVE. */
1476 
1477  if (stage1 && *f <= ftest1 && dg >= min(*ftol,lb3_1.gtol) * dginit) {
1478  stage1 = FALSE_;
1479  }
1480 
1481 /* A MODIFIED FUNCTION IS USED TO PREDICT THE STEP ONLY IF */
1482 /* WE HAVE NOT OBTAINED A STEP FOR WHICH THE MODIFIED */
1483 /* FUNCTION HAS A NONPOSITIVE FUNCTION VALUE AND NONNEGATIVE */
1484 /* DERIVATIVE, AND IF A LOWER FUNCTION VALUE HAS BEEN */
1485 /* OBTAINED BUT THE DECREASE IS NOT SUFFICIENT. */
1486 
1487  if (stage1 && *f <= fx && *f > ftest1) {
1488 /* DEFINE THE MODIFIED FUNCTION AND DERIVATIVE VALUES. */
1489 
1490  fm = *f - *stp * dgtest;
1491  fxm = fx - stx * dgtest;
1492  fym = fy - sty * dgtest;
1493  dgm = dg - dgtest;
1494  dgxm = dgx - dgtest;
1495  dgym = dgy - dgtest;
1496 
1497 /* CALL CSTEP TO UPDATE THE INTERVAL OF UNCERTAINTY */
1498 /* AND TO COMPUTE THE NEW STEP. */
1499 
1500  mcstep_(&stx, &fxm, &dgxm, &sty, &fym, &dgym, stp, &fm, &dgm, &brackt,
1501  &stmin, &stmax, &infoc);
1502 
1503 /* RESET THE FUNCTION AND GRADIENT VALUES FOR F. */
1504 
1505  fx = fxm + stx * dgtest;
1506  fy = fym + sty * dgtest;
1507  dgx = dgxm + dgtest;
1508  dgy = dgym + dgtest;
1509  } else {
1510 /* CALL MCSTEP TO UPDATE THE INTERVAL OF UNCERTAINTY */
1511 /* AND TO COMPUTE THE NEW STEP. */
1512 
1513  mcstep_(&stx, &fx, &dgx, &sty, &fy, &dgy, stp, f, &dg, &brackt, &
1514  stmin, &stmax, &infoc);
1515  }
1516 
1517 /* FORCE A SUFFICIENT DECREASE IN THE SIZE OF THE */
1518 /* INTERVAL OF UNCERTAINTY. */
1519 
1520  if (brackt) {
1521  if ((d__1 = sty - stx, abs(d__1)) >= p66 * width1) {
1522  *stp = stx + p5 * (sty - stx);
1523  }
1524  width1 = width;
1525  width = (d__1 = sty - stx, abs(d__1));
1526  }
1527 
1528 /* END OF ITERATION. */
1529 
1530  goto L30;
1531 
1532 /* LAST LINE OF SUBROUTINE MCSRCH. */
1533 } /* mcsrch_ */
1534 
1535 /* Subroutine */ int mcstep_(doublereal *stx, doublereal *fx, doublereal *dx,
1536  doublereal *sty, doublereal *fy, doublereal *dy, doublereal *stp,
1537  doublereal *fp, doublereal *dp, logical *brackt, doublereal *stpmin,
1538  doublereal *stpmax, integer *info)
1539 {
1540  /* System generated locals */
1541  doublereal d__1, d__2, d__3;
1542 
1543  /* Builtin functions */
1544  double sqrt(doublereal);
1545 
1546  /* Local variables */
1547  static doublereal sgnd, stpc, stpf, stpq, p, q, gamma, r, s, theta;
1548  static logical bound;
1549 
1550 
1551 /* SUBROUTINE MCSTEP */
1552 
1553 /* THE PURPOSE OF MCSTEP IS TO COMPUTE A SAFEGUARDED STEP FOR */
1554 /* A LINESEARCH AND TO UPDATE AN INTERVAL OF UNCERTAINTY FOR */
1555 /* A MINIMIZER OF THE FUNCTION. */
1556 
1557 /* THE PARAMETER STX CONTAINS THE STEP WITH THE LEAST FUNCTION */
1558 /* VALUE. THE PARAMETER STP CONTAINS THE CURRENT STEP. IT IS */
1559 /* ASSUMED THAT THE DERIVATIVE AT STX IS NEGATIVE IN THE */
1560 /* DIRECTION OF THE STEP. IF BRACKT IS SET TRUE THEN A */
1561 /* MINIMIZER HAS BEEN BRACKETED IN AN INTERVAL OF UNCERTAINTY */
1562 /* WITH ENDPOINTS STX AND STY. */
1563 
1564 /* THE SUBROUTINE STATEMENT IS */
1565 
1566 /* SUBROUTINE MCSTEP(STX,FX,DX,STY,FY,DY,STP,FP,DP,BRACKT, */
1567 /* STPMIN,STPMAX,INFO) */
1568 
1569 /* WHERE */
1570 
1571 /* STX, FX, AND DX ARE VARIABLES WHICH SPECIFY THE STEP, */
1572 /* THE FUNCTION, AND THE DERIVATIVE AT THE BEST STEP OBTAINED */
1573 /* SO FAR. THE DERIVATIVE MUST BE NEGATIVE IN THE DIRECTION */
1574 /* OF THE STEP, THAT IS, DX AND STP-STX MUST HAVE OPPOSITE */
1575 /* SIGNS. ON OUTPUT THESE PARAMETERS ARE UPDATED APPROPRIATELY. */
1576 
1577 /* STY, FY, AND DY ARE VARIABLES WHICH SPECIFY THE STEP, */
1578 /* THE FUNCTION, AND THE DERIVATIVE AT THE OTHER ENDPOINT OF */
1579 /* THE INTERVAL OF UNCERTAINTY. ON OUTPUT THESE PARAMETERS ARE */
1580 /* UPDATED APPROPRIATELY. */
1581 
1582 /* STP, FP, AND DP ARE VARIABLES WHICH SPECIFY THE STEP, */
1583 /* THE FUNCTION, AND THE DERIVATIVE AT THE CURRENT STEP. */
1584 /* IF BRACKT IS SET TRUE THEN ON INPUT STP MUST BE */
1585 /* BETWEEN STX AND STY. ON OUTPUT STP IS SET TO THE NEW STEP. */
1586 
1587 /* BRACKT IS A LOGICAL VARIABLE WHICH SPECIFIES IF A MINIMIZER */
1588 /* HAS BEEN BRACKETED. IF THE MINIMIZER HAS NOT BEEN BRACKETED */
1589 /* THEN ON INPUT BRACKT MUST BE SET FALSE. IF THE MINIMIZER */
1590 /* IS BRACKETED THEN ON OUTPUT BRACKT IS SET TRUE. */
1591 
1592 /* STPMIN AND STPMAX ARE INPUT VARIABLES WHICH SPECIFY LOWER */
1593 /* AND UPPER BOUNDS FOR THE STEP. */
1594 
1595 /* INFO IS AN INTEGER OUTPUT VARIABLE SET AS FOLLOWS: */
1596 /* IF INFO = 1,2,3,4,5, THEN THE STEP HAS BEEN COMPUTED */
1597 /* ACCORDING TO ONE OF THE FIVE CASES BELOW. OTHERWISE */
1598 /* INFO = 0, AND THIS INDICATES IMPROPER INPUT PARAMETERS. */
1599 
1600 /* SUBPROGRAMS CALLED */
1601 
1602 /* FORTRAN-SUPPLIED ... ABS,MAX,MIN,SQRT */
1603 
1604 /* ARGONNE NATIONAL LABORATORY. MINPACK PROJECT. JUNE 1983 */
1605 /* JORGE J. MORE', DAVID J. THUENTE */
1606 
1607  *info = 0;
1608 
1609 /* CHECK THE INPUT PARAMETERS FOR ERRORS. */
1610 
1611  if ((*brackt && (*stp <= min(*stx,*sty) || *stp >= max(*stx,*sty))) || *dx *
1612  (*stp - *stx) >= (float)0. || *stpmax < *stpmin) {
1613  return 0;
1614  }
1615 
1616 /* DETERMINE IF THE DERIVATIVES HAVE OPPOSITE SIGN. */
1617 
1618  sgnd = *dp * (*dx / abs(*dx));
1619 
1620 /* FIRST CASE. A HIGHER FUNCTION VALUE. */
1621 /* THE MINIMUM IS BRACKETED. IF THE CUBIC STEP IS CLOSER */
1622 /* TO STX THAN THE QUADRATIC STEP, THE CUBIC STEP IS TAKEN, */
1623 /* ELSE THE AVERAGE OF THE CUBIC AND QUADRATIC STEPS IS TAKEN. */
1624 
1625  if (*fp > *fx) {
1626  *info = 1;
1627  bound = TRUE_;
1628  theta = (*fx - *fp) * 3 / (*stp - *stx) + *dx + *dp;
1629 /* Computing MAX */
1630  d__1 = abs(theta), d__2 = abs(*dx), d__1 = max(d__1,d__2), d__2 = abs(
1631  *dp);
1632  s = max(d__1,d__2);
1633 /* Computing 2nd power */
1634  d__1 = theta / s;
1635  gamma = s * sqrt(d__1 * d__1 - *dx / s * (*dp / s));
1636  if (*stp < *stx) {
1637  gamma = -gamma;
1638  }
1639  p = gamma - *dx + theta;
1640  q = gamma - *dx + gamma + *dp;
1641  r = p / q;
1642  stpc = *stx + r * (*stp - *stx);
1643  stpq = *stx + *dx / ((*fx - *fp) / (*stp - *stx) + *dx) / 2 * (*stp -
1644  *stx);
1645  if ((d__1 = stpc - *stx, abs(d__1)) < (d__2 = stpq - *stx, abs(d__2)))
1646  {
1647  stpf = stpc;
1648  } else {
1649  stpf = stpc + (stpq - stpc) / 2;
1650  }
1651  *brackt = TRUE_;
1652 
1653 /* SECOND CASE. A LOWER FUNCTION VALUE AND DERIVATIVES OF */
1654 /* OPPOSITE SIGN. THE MINIMUM IS BRACKETED. IF THE CUBIC */
1655 /* STEP IS CLOSER TO STX THAN THE QUADRATIC (SECANT) STEP, */
1656 /* THE CUBIC STEP IS TAKEN, ELSE THE QUADRATIC STEP IS TAKEN. */
1657 
1658  } else if (sgnd < (float)0.) {
1659  *info = 2;
1660  bound = FALSE_;
1661  theta = (*fx - *fp) * 3 / (*stp - *stx) + *dx + *dp;
1662 /* Computing MAX */
1663  d__1 = abs(theta), d__2 = abs(*dx), d__1 = max(d__1,d__2), d__2 = abs(
1664  *dp);
1665  s = max(d__1,d__2);
1666 /* Computing 2nd power */
1667  d__1 = theta / s;
1668  gamma = s * sqrt(d__1 * d__1 - *dx / s * (*dp / s));
1669  if (*stp > *stx) {
1670  gamma = -gamma;
1671  }
1672  p = gamma - *dp + theta;
1673  q = gamma - *dp + gamma + *dx;
1674  r = p / q;
1675  stpc = *stp + r * (*stx - *stp);
1676  stpq = *stp + *dp / (*dp - *dx) * (*stx - *stp);
1677  if ((d__1 = stpc - *stp, abs(d__1)) > (d__2 = stpq - *stp, abs(d__2)))
1678  {
1679  stpf = stpc;
1680  } else {
1681  stpf = stpq;
1682  }
1683  *brackt = TRUE_;
1684 
1685 /* THIRD CASE. A LOWER FUNCTION VALUE, DERIVATIVES OF THE */
1686 /* SAME SIGN, AND THE MAGNITUDE OF THE DERIVATIVE DECREASES. */
1687 /* THE CUBIC STEP IS ONLY USED IF THE CUBIC TENDS TO INFINITY */
1688 /* IN THE DIRECTION OF THE STEP OR IF THE MINIMUM OF THE CUBIC */
1689 /* IS BEYOND STP. OTHERWISE THE CUBIC STEP IS DEFINED TO BE */
1690 /* EITHER STPMIN OR STPMAX. THE QUADRATIC (SECANT) STEP IS ALSO */
1691 /* COMPUTED AND IF THE MINIMUM IS BRACKETED THEN THE THE STEP */
1692 /* CLOSEST TO STX IS TAKEN, ELSE THE STEP FARTHEST AWAY IS TAKEN.
1693 */
1694 
1695  } else if (abs(*dp) < abs(*dx)) {
1696  *info = 3;
1697  bound = TRUE_;
1698  theta = (*fx - *fp) * 3 / (*stp - *stx) + *dx + *dp;
1699 /* Computing MAX */
1700  d__1 = abs(theta), d__2 = abs(*dx), d__1 = max(d__1,d__2), d__2 = abs(
1701  *dp);
1702  s = max(d__1,d__2);
1703 
1704 /* THE CASE GAMMA = 0 ONLY ARISES IF THE CUBIC DOES NOT TEND */
1705 /* TO INFINITY IN THE DIRECTION OF THE STEP. */
1706 
1707 /* Computing MAX */
1708 /* Computing 2nd power */
1709  d__3 = theta / s;
1710  d__1 = 0., d__2 = d__3 * d__3 - *dx / s * (*dp / s);
1711  gamma = s * sqrt((max(d__1,d__2)));
1712  if (*stp > *stx) {
1713  gamma = -gamma;
1714  }
1715  p = gamma - *dp + theta;
1716  q = gamma + (*dx - *dp) + gamma;
1717  r = p / q;
1718  if (r < (float)0. && gamma != (float)0.) {
1719  stpc = *stp + r * (*stx - *stp);
1720  } else if (*stp > *stx) {
1721  stpc = *stpmax;
1722  } else {
1723  stpc = *stpmin;
1724  }
1725  stpq = *stp + *dp / (*dp - *dx) * (*stx - *stp);
1726  if (*brackt) {
1727  if ((d__1 = *stp - stpc, abs(d__1)) < (d__2 = *stp - stpq, abs(
1728  d__2))) {
1729  stpf = stpc;
1730  } else {
1731  stpf = stpq;
1732  }
1733  } else {
1734  if ((d__1 = *stp - stpc, abs(d__1)) > (d__2 = *stp - stpq, abs(
1735  d__2))) {
1736  stpf = stpc;
1737  } else {
1738  stpf = stpq;
1739  }
1740  }
1741 
1742 /* FOURTH CASE. A LOWER FUNCTION VALUE, DERIVATIVES OF THE */
1743 /* SAME SIGN, AND THE MAGNITUDE OF THE DERIVATIVE DOES */
1744 /* NOT DECREASE. IF THE MINIMUM IS NOT BRACKETED, THE STEP */
1745 /* IS EITHER STPMIN OR STPMAX, ELSE THE CUBIC STEP IS TAKEN. */
1746 
1747  } else {
1748  *info = 4;
1749  bound = FALSE_;
1750  if (*brackt) {
1751  theta = (*fp - *fy) * 3 / (*sty - *stp) + *dy + *dp;
1752 /* Computing MAX */
1753  d__1 = abs(theta), d__2 = abs(*dy), d__1 = max(d__1,d__2), d__2 =
1754  abs(*dp);
1755  s = max(d__1,d__2);
1756 /* Computing 2nd power */
1757  d__1 = theta / s;
1758  gamma = s * sqrt(d__1 * d__1 - *dy / s * (*dp / s));
1759  if (*stp > *sty) {
1760  gamma = -gamma;
1761  }
1762  p = gamma - *dp + theta;
1763  q = gamma - *dp + gamma + *dy;
1764  r = p / q;
1765  stpc = *stp + r * (*sty - *stp);
1766  stpf = stpc;
1767  } else if (*stp > *stx) {
1768  stpf = *stpmax;
1769  } else {
1770  stpf = *stpmin;
1771  }
1772  }
1773 
1774 /* UPDATE THE INTERVAL OF UNCERTAINTY. THIS UPDATE DOES NOT */
1775 /* DEPEND ON THE NEW STEP OR THE CASE ANALYSIS ABOVE. */
1776 
1777  if (*fp > *fx) {
1778  *sty = *stp;
1779  *fy = *fp;
1780  *dy = *dp;
1781  } else {
1782  if (sgnd < (float)0.) {
1783  *sty = *stx;
1784  *fy = *fx;
1785  *dy = *dx;
1786  }
1787  *stx = *stp;
1788  *fx = *fp;
1789  *dx = *dp;
1790  }
1791 
1792 /* COMPUTE THE NEW STEP AND SAFEGUARD IT. */
1793 
1794  stpf = min(*stpmax,stpf);
1795  stpf = max(*stpmin,stpf);
1796  *stp = stpf;
1797  if (*brackt && bound) {
1798  if (*sty > *stx) {
1799 /* Computing MIN */
1800  d__1 = *stx + (*sty - *stx) * (float).66;
1801  *stp = min(d__1,*stp);
1802  } else {
1803 /* Computing MAX */
1804  d__1 = *stx + (*sty - *stx) * (float).66;
1805  *stp = max(d__1,*stp);
1806  }
1807  }
1808  return 0;
1809 
1810 /* LAST LINE OF SUBROUTINE MCSTEP. */
1811 } /* mcstep_ */
1812 
1813 #ifdef __cplusplus
1814  }
1815 #endif
doublereal d
Definition: lbfgs.cpp:154
int mcsrch_(integer *n, doublereal *x, doublereal *f, doublereal *g, doublereal *s, doublereal *stp, doublereal *ftol, doublereal *xtol, integer *maxfev, integer *info, integer *nfev, doublereal *wa)
Definition: lbfgs.cpp:1265
long int ftnlen
Definition: cbivnorm.cpp:67
integer lp
Definition: lbfgs.cpp:245
shortint h
Definition: lbfgs.cpp:150
int type
Definition: cbivnorm.cpp:175
int mcstep_(doublereal *stx, doublereal *fx, doublereal *dx, doublereal *sty, doublereal *fy, doublereal *dy, doublereal *stp, doublereal *fp, doublereal *dp, logical *brackt, doublereal *stpmin, doublereal *stpmax, integer *info)
Definition: lbfgs.cpp:1535
complex c
Definition: lbfgs.cpp:155
char * name
Definition: cbivnorm.cpp:180
float real
Definition: cbivnorm.cpp:35
char logical1
Definition: cbivnorm.cpp:41
char * name
Definition: cbivnorm.cpp:172
integer mp
Definition: lbfgs.cpp:245
int(* S_fp)()
Definition: cbivnorm.cpp:222
doublereal ddot_(integer *n, doublereal *dx, integer *incx, doublereal *dy, integer *incy)
Definition: lbfgs.cpp:1175
shortint(* J_fp)()
Definition: cbivnorm.cpp:213
short int shortlogical
Definition: cbivnorm.cpp:40
char * addr
Definition: cbivnorm.cpp:173
#define x
int lbfgs_(integer *n, integer *m, doublereal *x, doublereal *f, doublereal *g, logical *diagco, doublereal *diag, integer *iprint, doublereal *eps, doublereal *xtol, doublereal *w, integer *iflag, integer *iter, integer *info)
Definition: lbfgs.cpp:261
doublereal stpmax
Definition: lbfgs.cpp:246
const int iprint
Definition: fvar.hpp:9504
VOID(* Z_fp)()
Definition: cbivnorm.cpp:218
integer(* I_fp)()
Definition: cbivnorm.cpp:214
doublereal E_f
Definition: cbivnorm.cpp:228
char integer1
Definition: cbivnorm.cpp:42
long int ftnint
Definition: cbivnorm.cpp:68
real(* R_fp)()
Definition: cbivnorm.cpp:215
Vardesc ** vars
Definition: cbivnorm.cpp:181
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
long int flag
Definition: cbivnorm.cpp:66
real r
Definition: lbfgs.cpp:153
#define abs(x)
Definition: lbfgs.cpp:178
#define min(a, b)
Definition: cbivnorm.cpp:188
shortlogical(* K_fp)()
Definition: cbivnorm.cpp:220
doublereal(* D_fp)()
Definition: cbivnorm.cpp:216
dvariable beta(const prevariable &a, const prevariable &b)
Beta density function.
Definition: vbeta.cpp:18
VOID C_f
Definition: cbivnorm.cpp:225
VOID(* C_fp)()
Definition: cbivnorm.cpp:217
doublereal(*)(* E_fp)()
Definition: cbivnorm.cpp:216
struct _lb3_1 lb3_1
Definition: lbfgs.cpp:248
#define VOID
Definition: lbfgs.cpp:146
long int integer
Definition: cbivnorm.cpp:31
#define TRUE_
Definition: lbfgs.cpp:44
long int logical
Definition: cbivnorm.cpp:39
VOID H_f
Definition: cbivnorm.cpp:226
#define FALSE_
Definition: lbfgs.cpp:45
VOID(* H_fp)()
Definition: cbivnorm.cpp:221
#define w
doublecomplex z
Definition: lbfgs.cpp:156
double eps
Definition: ftweak.cpp:13
integer1 g
Definition: lbfgs.cpp:149
char * address
Definition: lbfgs.cpp:32
integer i
Definition: lbfgs.cpp:151
doublereal stpmin
Definition: lbfgs.cpp:246
static integer c__1
Definition: lbfgs.cpp:252
#define max(a, b)
Definition: cbivnorm.cpp:189
int daxpy_(integer *n, doublereal *da, doublereal *dx, integer *incx, doublereal *dy, integer *incy)
Definition: lbfgs.cpp:1090
doublereal gtol
Definition: lbfgs.cpp:246
double doublereal
Definition: cbivnorm.cpp:36
int nvars
Definition: cbivnorm.cpp:182
logical(* L_fp)()
Definition: cbivnorm.cpp:219
VOID Z_f
Definition: cbivnorm.cpp:227
short int shortint
Definition: cbivnorm.cpp:34
ftnlen * dims
Definition: cbivnorm.cpp:174