2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 21:10:28 +02:00
|
|
|
template<typename Scalar>
|
|
|
|
|
int lmstr_template(minpack_funcderstr_mn fcn, void *p, int m, int n, Scalar *x,
|
2009-08-20 23:36:03 +02:00
|
|
|
Scalar *fvec, Scalar *fjac, int ldfjac, Scalar ftol,
|
|
|
|
|
Scalar xtol, Scalar gtol, int maxfev, Scalar *
|
|
|
|
|
diag, int mode, Scalar factor, int nprint,
|
2009-08-21 00:23:26 +02:00
|
|
|
int &nfev, int &njev, int *ipvt, Scalar *qtf,
|
2009-08-20 23:36:03 +02:00
|
|
|
Scalar *wa1, Scalar *wa2, Scalar *wa3, Scalar *wa4)
|
2009-08-19 18:38:45 +02:00
|
|
|
{
|
|
|
|
|
/* Initialized data */
|
|
|
|
|
|
|
|
|
|
/* System generated locals */
|
2009-08-20 23:56:13 +02:00
|
|
|
int fjac_offset;
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
/* Local variables */
|
2009-08-21 00:26:37 +02:00
|
|
|
int i, j, l;
|
2009-08-20 21:10:28 +02:00
|
|
|
Scalar par, sum;
|
2009-08-19 18:38:45 +02:00
|
|
|
int sing;
|
|
|
|
|
int iter;
|
2009-08-20 21:10:28 +02:00
|
|
|
Scalar temp, temp1, temp2;
|
2009-08-19 18:38:45 +02:00
|
|
|
int iflag;
|
2009-08-20 21:10:28 +02:00
|
|
|
Scalar delta;
|
|
|
|
|
Scalar ratio;
|
|
|
|
|
Scalar fnorm, gnorm, pnorm, xnorm, fnorm1, actred, dirder, prered;
|
2009-08-19 18:38:45 +02:00
|
|
|
int info;
|
|
|
|
|
|
|
|
|
|
/* Parameter adjustments */
|
|
|
|
|
--wa4;
|
|
|
|
|
--fvec;
|
|
|
|
|
--wa3;
|
|
|
|
|
--wa2;
|
|
|
|
|
--wa1;
|
|
|
|
|
--qtf;
|
|
|
|
|
--ipvt;
|
|
|
|
|
--diag;
|
|
|
|
|
--x;
|
2009-08-20 23:56:13 +02:00
|
|
|
fjac_offset = 1 + ldfjac;
|
2009-08-19 18:38:45 +02:00
|
|
|
fjac -= fjac_offset;
|
|
|
|
|
|
|
|
|
|
/* Function Body */
|
|
|
|
|
|
|
|
|
|
info = 0;
|
|
|
|
|
iflag = 0;
|
2009-08-21 00:23:26 +02:00
|
|
|
nfev = 0;
|
|
|
|
|
njev = 0;
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* check the input parameters for errors. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
if (n <= 0 || m < n || ldfjac < n || ftol < 0. || xtol < 0. ||
|
2009-08-20 23:36:03 +02:00
|
|
|
gtol < 0. || maxfev <= 0 || factor <= 0.) {
|
|
|
|
|
goto L340;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
if (mode != 2) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L20;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 23:33:45 +02:00
|
|
|
for (j = 1; j <= n; ++j) {
|
2009-08-20 23:36:03 +02:00
|
|
|
if (diag[j] <= 0.) {
|
|
|
|
|
goto L340;
|
|
|
|
|
}
|
|
|
|
|
/* L10: */
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
L20:
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* evaluate the function at the starting point */
|
|
|
|
|
/* and calculate its norm. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
iflag = (*fcn)(p, m, n, &x[1], &fvec[1], &wa3[1], 1);
|
2009-08-21 00:23:26 +02:00
|
|
|
nfev = 1;
|
2009-08-19 18:38:45 +02:00
|
|
|
if (iflag < 0) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L340;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 21:10:28 +02:00
|
|
|
fnorm = ei_enorm<Scalar>(m, &fvec[1]);
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* initialize levenberg-marquardt parameter and iteration counter. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
par = 0.;
|
|
|
|
|
iter = 1;
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* beginning of the outer loop. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
L30:
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* if requested, call fcn to enable printing of iterates. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
if (nprint <= 0) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L40;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
iflag = 0;
|
|
|
|
|
if ((iter - 1) % nprint == 0) {
|
2009-08-20 23:36:03 +02:00
|
|
|
iflag = (*fcn)(p, m, n, &x[1], &fvec[1], &wa3[1], 0);
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
if (iflag < 0) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L340;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
L40:
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* compute the qr factorization of the jacobian matrix */
|
|
|
|
|
/* calculated one row at a time, while simultaneously */
|
|
|
|
|
/* forming (q transpose)*fvec and storing the first */
|
|
|
|
|
/* n components in qtf. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:33:45 +02:00
|
|
|
for (j = 1; j <= n; ++j) {
|
2009-08-20 23:36:03 +02:00
|
|
|
qtf[j] = 0.;
|
2009-08-21 00:26:37 +02:00
|
|
|
for (i = 1; i <= n; ++i) {
|
|
|
|
|
fjac[i + j * ldfjac] = 0.;
|
2009-08-20 23:36:03 +02:00
|
|
|
/* L50: */
|
|
|
|
|
}
|
|
|
|
|
/* L60: */
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
iflag = 2;
|
2009-08-21 00:26:37 +02:00
|
|
|
for (i = 1; i <= m; ++i) {
|
2009-08-20 23:36:03 +02:00
|
|
|
if ((*fcn)(p, m, n, &x[1], &fvec[1], &wa3[1], iflag) < 0) {
|
|
|
|
|
goto L340;
|
|
|
|
|
}
|
2009-08-21 00:26:37 +02:00
|
|
|
temp = fvec[i];
|
2009-08-20 23:36:03 +02:00
|
|
|
rwupdt(n, &fjac[fjac_offset], ldfjac, &wa3[1], &qtf[1], &temp, &wa1[
|
|
|
|
|
1], &wa2[1]);
|
|
|
|
|
++iflag;
|
|
|
|
|
/* L70: */
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-21 00:23:26 +02:00
|
|
|
++njev;
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* if the jacobian is rank deficient, call qrfac to */
|
|
|
|
|
/* reorder its columns and update the components of qtf. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
sing = FALSE_;
|
2009-08-20 23:33:45 +02:00
|
|
|
for (j = 1; j <= n; ++j) {
|
2009-08-20 23:56:13 +02:00
|
|
|
if (fjac[j + j * ldfjac] == 0.) {
|
2009-08-20 23:36:03 +02:00
|
|
|
sing = TRUE_;
|
|
|
|
|
}
|
|
|
|
|
ipvt[j] = j;
|
2009-08-20 23:56:13 +02:00
|
|
|
wa2[j] = ei_enorm<Scalar>(j, &fjac[j * ldfjac + 1]);
|
2009-08-20 23:36:03 +02:00
|
|
|
/* L80: */
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
if (! sing) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L130;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
qrfac(n, n, &fjac[fjac_offset], ldfjac, TRUE_, &ipvt[1], n, &wa1[1], &
|
2009-08-20 23:36:03 +02:00
|
|
|
wa2[1], &wa3[1]);
|
2009-08-20 23:33:45 +02:00
|
|
|
for (j = 1; j <= n; ++j) {
|
2009-08-20 23:56:13 +02:00
|
|
|
if (fjac[j + j * ldfjac] == 0.) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L110;
|
|
|
|
|
}
|
|
|
|
|
sum = 0.;
|
2009-08-21 00:26:37 +02:00
|
|
|
for (i = j; i <= n; ++i) {
|
|
|
|
|
sum += fjac[i + j * ldfjac] * qtf[i];
|
2009-08-20 23:36:03 +02:00
|
|
|
/* L90: */
|
|
|
|
|
}
|
2009-08-20 23:56:13 +02:00
|
|
|
temp = -sum / fjac[j + j * ldfjac];
|
2009-08-21 00:26:37 +02:00
|
|
|
for (i = j; i <= n; ++i) {
|
|
|
|
|
qtf[i] += fjac[i + j * ldfjac] * temp;
|
2009-08-20 23:36:03 +02:00
|
|
|
/* L100: */
|
|
|
|
|
}
|
2009-08-19 18:38:45 +02:00
|
|
|
L110:
|
2009-08-20 23:56:13 +02:00
|
|
|
fjac[j + j * ldfjac] = wa1[j];
|
2009-08-20 23:36:03 +02:00
|
|
|
/* L120: */
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
L130:
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* on the first iteration and if mode is 1, scale according */
|
|
|
|
|
/* to the norms of the columns of the initial jacobian. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
if (iter != 1) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L170;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
if (mode == 2) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L150;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 23:33:45 +02:00
|
|
|
for (j = 1; j <= n; ++j) {
|
2009-08-20 23:36:03 +02:00
|
|
|
diag[j] = wa2[j];
|
|
|
|
|
if (wa2[j] == 0.) {
|
|
|
|
|
diag[j] = 1.;
|
|
|
|
|
}
|
|
|
|
|
/* L140: */
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
L150:
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* on the first iteration, calculate the norm of the scaled x */
|
|
|
|
|
/* and initialize the step bound delta. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:33:45 +02:00
|
|
|
for (j = 1; j <= n; ++j) {
|
2009-08-20 23:36:03 +02:00
|
|
|
wa3[j] = diag[j] * x[j];
|
|
|
|
|
/* L160: */
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 21:10:28 +02:00
|
|
|
xnorm = ei_enorm<Scalar>(n, &wa3[1]);
|
2009-08-19 18:38:45 +02:00
|
|
|
delta = factor * xnorm;
|
|
|
|
|
if (delta == 0.) {
|
2009-08-20 23:36:03 +02:00
|
|
|
delta = factor;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
L170:
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* compute the norm of the scaled gradient. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
gnorm = 0.;
|
|
|
|
|
if (fnorm == 0.) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L210;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 23:33:45 +02:00
|
|
|
for (j = 1; j <= n; ++j) {
|
2009-08-20 23:36:03 +02:00
|
|
|
l = ipvt[j];
|
|
|
|
|
if (wa2[l] == 0.) {
|
|
|
|
|
goto L190;
|
|
|
|
|
}
|
|
|
|
|
sum = 0.;
|
2009-08-21 00:26:37 +02:00
|
|
|
for (i = 1; i <= j; ++i) {
|
|
|
|
|
sum += fjac[i + j * ldfjac] * (qtf[i] / fnorm);
|
2009-08-20 23:36:03 +02:00
|
|
|
/* L180: */
|
|
|
|
|
}
|
|
|
|
|
/* Computing MAX */
|
2009-08-21 00:04:41 +02:00
|
|
|
gnorm = max(gnorm, ei_abs(sum/wa2[l]));
|
2009-08-19 18:38:45 +02:00
|
|
|
L190:
|
2009-08-20 23:36:03 +02:00
|
|
|
/* L200: */
|
|
|
|
|
;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
L210:
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* test for convergence of the gradient norm. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
if (gnorm <= gtol) {
|
2009-08-20 23:36:03 +02:00
|
|
|
info = 4;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
if (info != 0) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L340;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* rescale if necessary. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
if (mode == 2) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L230;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 23:56:13 +02:00
|
|
|
for (j = 1; j <= n; ++j) /* Computing MAX */
|
|
|
|
|
diag[j] = max(diag[j], wa2[j]);
|
2009-08-19 18:38:45 +02:00
|
|
|
L230:
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* beginning of the inner loop. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
L240:
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* determine the levenberg-marquardt parameter. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
lmpar(n, &fjac[fjac_offset], ldfjac, &ipvt[1], &diag[1], &qtf[1], delta,
|
2009-08-20 23:36:03 +02:00
|
|
|
&par, &wa1[1], &wa2[1], &wa3[1], &wa4[1]);
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* store the direction p and x + p. calculate the norm of p. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:33:45 +02:00
|
|
|
for (j = 1; j <= n; ++j) {
|
2009-08-20 23:36:03 +02:00
|
|
|
wa1[j] = -wa1[j];
|
|
|
|
|
wa2[j] = x[j] + wa1[j];
|
|
|
|
|
wa3[j] = diag[j] * wa1[j];
|
|
|
|
|
/* L250: */
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 21:10:28 +02:00
|
|
|
pnorm = ei_enorm<Scalar>(n, &wa3[1]);
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* on the first iteration, adjust the initial step bound. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
if (iter == 1) {
|
2009-08-20 23:36:03 +02:00
|
|
|
delta = min(delta,pnorm);
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* evaluate the function at x + p and calculate its norm. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
iflag = (*fcn)(p, m, n, &wa2[1], &wa4[1], &wa3[1], 1);
|
2009-08-21 00:23:26 +02:00
|
|
|
++nfev;
|
2009-08-19 18:38:45 +02:00
|
|
|
if (iflag < 0) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L340;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 21:10:28 +02:00
|
|
|
fnorm1 = ei_enorm<Scalar>(m, &wa4[1]);
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* compute the scaled actual reduction. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
actred = -1.;
|
2009-08-20 23:56:13 +02:00
|
|
|
if (p1 * fnorm1 < fnorm) /* Computing 2nd power */
|
|
|
|
|
actred = 1. - ei_abs2(fnorm1 / fnorm);
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* compute the scaled predicted reduction and */
|
|
|
|
|
/* the scaled directional derivative. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:33:45 +02:00
|
|
|
for (j = 1; j <= n; ++j) {
|
2009-08-20 23:36:03 +02:00
|
|
|
wa3[j] = 0.;
|
|
|
|
|
l = ipvt[j];
|
|
|
|
|
temp = wa1[l];
|
2009-08-21 00:26:37 +02:00
|
|
|
for (i = 1; i <= j; ++i) {
|
|
|
|
|
wa3[i] += fjac[i + j * ldfjac] * temp;
|
2009-08-20 23:36:03 +02:00
|
|
|
/* L260: */
|
|
|
|
|
}
|
|
|
|
|
/* L270: */
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 21:10:28 +02:00
|
|
|
temp1 = ei_enorm<Scalar>(n, &wa3[1]) / fnorm;
|
2009-08-21 00:27:11 +02:00
|
|
|
temp2 = ei_sqrt(par) * pnorm / fnorm;
|
2009-08-20 23:36:03 +02:00
|
|
|
/* Computing 2nd power */
|
2009-08-20 23:56:13 +02:00
|
|
|
prered = temp1 * temp1 + temp2 * temp2 / p5;
|
|
|
|
|
dirder = -(temp1 * temp1 + temp2 * temp2);
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* compute the ratio of the actual to the predicted */
|
|
|
|
|
/* reduction. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
ratio = 0.;
|
|
|
|
|
if (prered != 0.) {
|
2009-08-20 23:36:03 +02:00
|
|
|
ratio = actred / prered;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* update the step bound. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
if (ratio > p25) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L280;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
if (actred >= 0.) {
|
2009-08-20 23:36:03 +02:00
|
|
|
temp = p5;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
if (actred < 0.) {
|
2009-08-20 23:36:03 +02:00
|
|
|
temp = p5 * dirder / (dirder + p5 * actred);
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
if (p1 * fnorm1 >= fnorm || temp < p1) {
|
2009-08-20 23:36:03 +02:00
|
|
|
temp = p1;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 23:36:03 +02:00
|
|
|
/* Computing MIN */
|
2009-08-20 23:56:13 +02:00
|
|
|
delta = temp * min(delta, pnorm / p1);
|
|
|
|
|
|
2009-08-19 18:38:45 +02:00
|
|
|
par /= temp;
|
|
|
|
|
goto L300;
|
|
|
|
|
L280:
|
|
|
|
|
if (par != 0. && ratio < p75) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L290;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
delta = pnorm / p5;
|
|
|
|
|
par = p5 * par;
|
|
|
|
|
L290:
|
|
|
|
|
L300:
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* test for successful iteration. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
if (ratio < p0001) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L330;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* successful iteration. update x, fvec, and their norms. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-20 23:33:45 +02:00
|
|
|
for (j = 1; j <= n; ++j) {
|
2009-08-20 23:36:03 +02:00
|
|
|
x[j] = wa2[j];
|
|
|
|
|
wa2[j] = diag[j] * x[j];
|
|
|
|
|
/* L310: */
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-21 00:26:37 +02:00
|
|
|
for (i = 1; i <= m; ++i) {
|
|
|
|
|
fvec[i] = wa4[i];
|
2009-08-20 23:36:03 +02:00
|
|
|
/* L320: */
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 21:10:28 +02:00
|
|
|
xnorm = ei_enorm<Scalar>(n, &wa2[1]);
|
2009-08-19 18:38:45 +02:00
|
|
|
fnorm = fnorm1;
|
|
|
|
|
++iter;
|
|
|
|
|
L330:
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* tests for convergence. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-21 00:04:41 +02:00
|
|
|
if (ei_abs(actred) <= ftol && prered <= ftol && p5 * ratio <= 1.) {
|
2009-08-20 23:36:03 +02:00
|
|
|
info = 1;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
if (delta <= xtol * xnorm) {
|
2009-08-20 23:36:03 +02:00
|
|
|
info = 2;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-21 00:04:41 +02:00
|
|
|
if (ei_abs(actred) <= ftol && prered <= ftol && p5 * ratio <= 1. && info
|
2009-08-20 23:36:03 +02:00
|
|
|
== 2) {
|
|
|
|
|
info = 3;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
if (info != 0) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L340;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* tests for termination and stringent tolerances. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
2009-08-21 00:23:26 +02:00
|
|
|
if (nfev >= maxfev) {
|
2009-08-20 23:36:03 +02:00
|
|
|
info = 5;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-21 00:04:41 +02:00
|
|
|
if (ei_abs(actred) <= epsilon<Scalar>() && prered <= epsilon<Scalar>() && p5 * ratio <= 1.) {
|
2009-08-20 23:36:03 +02:00
|
|
|
info = 6;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 21:10:28 +02:00
|
|
|
if (delta <= epsilon<Scalar>() * xnorm) {
|
2009-08-20 23:36:03 +02:00
|
|
|
info = 7;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
2009-08-20 21:10:28 +02:00
|
|
|
if (gnorm <= epsilon<Scalar>()) {
|
2009-08-20 23:36:03 +02:00
|
|
|
info = 8;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
if (info != 0) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L340;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* end of the inner loop. repeat if iteration unsuccessful. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
if (ratio < p0001) {
|
2009-08-20 23:36:03 +02:00
|
|
|
goto L240;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* end of the outer loop. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
goto L30;
|
|
|
|
|
L340:
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* termination, either normal or user imposed. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
if (iflag < 0) {
|
2009-08-20 23:36:03 +02:00
|
|
|
info = iflag;
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
iflag = 0;
|
|
|
|
|
if (nprint > 0) {
|
2009-08-20 23:36:03 +02:00
|
|
|
iflag = (*fcn)(p, m, n, &x[1], &fvec[1], &wa3[1], 0);
|
2009-08-19 18:38:45 +02:00
|
|
|
}
|
|
|
|
|
return info;
|
|
|
|
|
|
2009-08-20 23:36:03 +02:00
|
|
|
/* last card of subroutine lmstr. */
|
2009-08-19 18:38:45 +02:00
|
|
|
|
|
|
|
|
} /* lmstr_ */
|
|
|
|
|
|