36 typedef struct { real r, i; }
complex;
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)
187 #define F2C_proc_par_types 1
189 typedef int (*U_fp)(...);
199 typedef int (*
S_fp)(...);
201 typedef int (*U_fp)();
221 #ifndef Skip_f2c_Undefs
300 static integer iscn, nfev, iycn;
302 static integer nfun, ispt, iypt, i, bound;
619 if (*n <= 0 || *m <= 0) {
622 if (lb3_1.
gtol <= 1e-4) {
635 for (i = 1; i <= i__1; ++i) {
637 if (diag[i] <= zero) {
643 for (i = 1; i <= i__1; ++i) {
664 ispt = *n + (*m << 1);
665 iypt = ispt + *n * *m;
667 for (i = 1; i <= i__1; ++i) {
669 w[ispt + i] = -g[i] * diag[i];
671 gnorm =
sqrt(
ddot_(n, &g[1], &c__1, &g[1], &c__1));
701 ys =
ddot_(n, &w[iypt + npt + 1], &c__1, &w[ispt + npt + 1], &c__1);
705 yy =
ddot_(n, &w[iypt + npt + 1], &c__1, &w[iypt + npt + 1], &c__1);
707 for (i = 1; i <= i__1; ++i) {
718 for (i = 1; i <= i__1; ++i) {
720 if (diag[i] <= zero) {
735 w[*n + cp] = one / ys;
737 for (i = 1; i <= i__1; ++i) {
743 for (i = 1; i <= i__1; ++i) {
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;
753 daxpy_(n, &d__1, &w[iycn + 1], &c__1, &w[1], &c__1);
758 for (i = 1; i <= i__1; ++i) {
760 w[i] = diag[i] * w[i];
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);
782 for (i = 1; i <= i__1; ++i) {
784 w[ispt + point * *n + i] = w[i];
797 for (i = 1; i <= i__1; ++i) {
802 mcsrch_(n, &x[1], f, &g[1], &w[ispt + point * *n + 1], &stp, &ftol, xtol,
803 &maxfev, info, &nfev, &diag[1]);
818 for (i = 1; i <= i__1; ++i) {
819 w[ispt + npt + i] = stp * w[ispt + npt + i];
821 w[iypt + npt + i] = g[i] - w[i];
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) {
1097 static integer i, m, ix, iy, mp1;
1116 if (*incx == 1 && *incy == 1) {
1126 ix = (-(*n) + 1) * *incx + 1;
1129 iy = (-(*n) + 1) * *incy + 1;
1132 for (i = 1; i <= i__1; ++i) {
1133 dy[iy] += *da * dx[ix];
1151 for (i = 1; i <= i__1; ++i) {
1152 dy[i] += *da * dx[i];
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];
1203 if (*incx == 1 && *incy == 1) {
1213 ix = (-(*n) + 1) * *incx + 1;
1216 iy = (-(*n) + 1) * *incy + 1;
1219 for (i = 1; i <= i__1; ++i) {
1220 dtemp += dx[ix] * dy[iy];
1239 for (i = 1; i <= i__1; ++i) {
1240 dtemp += dx[i] * dy[i];
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];
1293 static doublereal finit, width, stmin, stmax;
1295 static doublereal width1, ftest1, dg, fm, fx, fy;
1302 static doublereal dgm, dgx, dgy, fxm, fym, stx, sty;
1344 if (*n <= 0 || *stp <= zero || *ftol < zero || lb3_1.
gtol < zero || *xtol
1355 for (j = 1; j <= i__1; ++j) {
1356 dginit += g[j] * s[j];
1359 if (dginit >= zero) {
1372 dgtest = *ftol * dginit;
1374 width1 = width / p5;
1376 for (j = 1; j <= i__1; ++j) {
1404 stmin =
min(stx,sty);
1405 stmax =
max(stx,sty);
1408 stmax = *stp + xtrapf * (*stp - stx);
1419 if ( ( brackt && (*stp <= stmin || *stp >= stmax) ) || *nfev >= *maxfev - 1 ||
1420 infoc == 0 || (brackt && stmax - stmin <= *xtol * stmax) ) {
1429 for (j = 1; j <= i__1; ++j) {
1430 x[j] = wa[j] + *stp * s[j];
1441 for (j = 1; j <= i__1; ++j) {
1445 ftest1 = finit + *stp * dgtest;
1449 if ( (brackt && (*stp <= stmin || *stp >= stmax)) || infoc == 0) {
1452 if (*stp == lb3_1.
stpmax && *f <= ftest1 && dg <= dgtest) {
1455 if (*stp == lb3_1.
stpmin && (*f > ftest1 || dg >= dgtest)) {
1458 if (*nfev >= *maxfev) {
1461 if (brackt && stmax - stmin <= *xtol * stmax) {
1464 if (*f <= ftest1 &&
abs(dg) <= lb3_1.
gtol * (-dginit)) {
1477 if (stage1 && *f <= ftest1 && dg >=
min(*ftol,lb3_1.
gtol) * dginit) {
1487 if (stage1 && *f <= fx && *f > ftest1) {
1490 fm = *f - *stp * dgtest;
1491 fxm = fx - stx * dgtest;
1492 fym = fy - sty * dgtest;
1494 dgxm = dgx - dgtest;
1495 dgym = dgy - dgtest;
1500 mcstep_(&stx, &fxm, &dgxm, &sty, &fym, &dgym, stp, &fm, &dgm, &brackt,
1501 &stmin, &stmax, &infoc);
1505 fx = fxm + stx * dgtest;
1506 fy = fym + sty * dgtest;
1507 dgx = dgxm + dgtest;
1508 dgy = dgym + dgtest;
1513 mcstep_(&stx, &fx, &dgx, &sty, &fy, &dgy, stp, f, &dg, &brackt, &
1514 stmin, &stmax, &infoc);
1521 if ((d__1 = sty - stx,
abs(d__1)) >= p66 * width1) {
1522 *stp = stx + p5 * (sty - stx);
1525 width = (d__1 = sty - stx,
abs(d__1));
1547 static doublereal sgnd, stpc, stpf, stpq, p, q, gamma, r, s, theta;
1611 if ((*brackt && (*stp <=
min(*stx,*sty) || *stp >=
max(*stx,*sty))) || *dx *
1612 (*stp - *stx) >= (
float)0. || *stpmax < *stpmin) {
1618 sgnd = *dp * (*dx /
abs(*dx));
1628 theta = (*fx - *fp) * 3 / (*stp - *stx) + *dx + *dp;
1630 d__1 =
abs(theta), d__2 =
abs(*dx), d__1 =
max(d__1,d__2), d__2 =
abs(
1635 gamma = s *
sqrt(d__1 * d__1 - *dx / s * (*dp / s));
1639 p = gamma - *dx + theta;
1640 q = gamma - *dx + gamma + *dp;
1642 stpc = *stx + r * (*stp - *stx);
1643 stpq = *stx + *dx / ((*fx - *fp) / (*stp - *stx) + *dx) / 2 * (*stp -
1645 if ((d__1 = stpc - *stx,
abs(d__1)) < (d__2 = stpq - *stx,
abs(d__2)))
1649 stpf = stpc + (stpq - stpc) / 2;
1658 }
else if (sgnd < (
float)0.) {
1661 theta = (*fx - *fp) * 3 / (*stp - *stx) + *dx + *dp;
1663 d__1 =
abs(theta), d__2 =
abs(*dx), d__1 =
max(d__1,d__2), d__2 =
abs(
1668 gamma = s *
sqrt(d__1 * d__1 - *dx / s * (*dp / s));
1672 p = gamma - *dp + theta;
1673 q = gamma - *dp + gamma + *dx;
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)))
1695 }
else if (
abs(*dp) <
abs(*dx)) {
1698 theta = (*fx - *fp) * 3 / (*stp - *stx) + *dx + *dp;
1700 d__1 =
abs(theta), d__2 =
abs(*dx), d__1 =
max(d__1,d__2), d__2 =
abs(
1710 d__1 = 0., d__2 = d__3 * d__3 - *dx / s * (*dp / s);
1711 gamma = s *
sqrt((
max(d__1,d__2)));
1715 p = gamma - *dp + theta;
1716 q = gamma + (*dx - *dp) + gamma;
1718 if (r < (
float)0. && gamma != (float)0.) {
1719 stpc = *stp + r * (*stx - *stp);
1720 }
else if (*stp > *stx) {
1725 stpq = *stp + *dp / (*dp - *dx) * (*stx - *stp);
1727 if ((d__1 = *stp - stpc,
abs(d__1)) < (d__2 = *stp - stpq,
abs(
1734 if ((d__1 = *stp - stpc,
abs(d__1)) > (d__2 = *stp - stpq,
abs(
1751 theta = (*fp - *fy) * 3 / (*sty - *stp) + *dy + *dp;
1753 d__1 =
abs(theta), d__2 =
abs(*dy), d__1 =
max(d__1,d__2), d__2 =
1758 gamma = s *
sqrt(d__1 * d__1 - *dy / s * (*dp / s));
1762 p = gamma - *dp + theta;
1763 q = gamma - *dp + gamma + *dy;
1765 stpc = *stp + r * (*sty - *stp);
1767 }
else if (*stp > *stx) {
1782 if (sgnd < (
float)0.) {
1794 stpf =
min(*stpmax,stpf);
1795 stpf =
max(*stpmin,stpf);
1797 if (*brackt && bound) {
1800 d__1 = *stx + (*sty - *stx) * (
float).66;
1801 *stp =
min(d__1,*stp);
1804 d__1 = *stx + (*sty - *stx) * (
float).66;
1805 *stp =
max(d__1,*stp);
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)
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)
doublereal ddot_(integer *n, doublereal *dx, integer *incx, doublereal *dy, integer *incy)
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)
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
dvariable beta(const prevariable &a, const prevariable &b)
Beta density function.
int daxpy_(integer *n, doublereal *da, doublereal *dx, integer *incx, doublereal *dy, integer *incy)