2009-08-22 06:40:22 +02:00
|
|
|
|
|
|
|
|
template <typename Scalar>
|
2009-08-23 21:04:55 +02:00
|
|
|
void ei_lmpar(
|
2009-08-23 21:47:55 +02:00
|
|
|
Matrix< Scalar, Dynamic, Dynamic > &r,
|
2009-08-24 08:45:06 +02:00
|
|
|
VectorXi &ipvt, // TODO : const once ipvt mess fixed
|
2009-08-23 21:04:55 +02:00
|
|
|
const Matrix< Scalar, Dynamic, 1 > &diag,
|
|
|
|
|
const Matrix< Scalar, Dynamic, 1 > &qtb,
|
|
|
|
|
Scalar delta,
|
|
|
|
|
Scalar &par,
|
|
|
|
|
Matrix< Scalar, Dynamic, 1 > &x,
|
|
|
|
|
Matrix< Scalar, Dynamic, 1 > &sdiag)
|
2009-08-22 06:40:22 +02:00
|
|
|
{
|
|
|
|
|
/* Local variables */
|
2009-08-22 07:27:17 +02:00
|
|
|
int i, j, k, l;
|
2009-08-22 06:40:22 +02:00
|
|
|
Scalar fp;
|
|
|
|
|
Scalar sum, parc, parl;
|
|
|
|
|
int iter;
|
2009-08-22 07:31:14 +02:00
|
|
|
Scalar temp, paru;
|
2009-08-22 06:40:22 +02:00
|
|
|
int nsing;
|
|
|
|
|
Scalar gnorm;
|
|
|
|
|
Scalar dxnorm;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Function Body */
|
2009-08-22 07:31:14 +02:00
|
|
|
const Scalar dwarf = std::numeric_limits<Scalar>::min();
|
2009-08-23 21:47:55 +02:00
|
|
|
const int n = r.cols();
|
2009-08-23 21:04:55 +02:00
|
|
|
assert(n==diag.size());
|
|
|
|
|
assert(n==qtb.size());
|
|
|
|
|
assert(n==x.size());
|
|
|
|
|
|
|
|
|
|
Matrix< Scalar, Dynamic, 1 > wa1(n), wa2(n);
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* compute and store in x the gauss-newton direction. if the */
|
|
|
|
|
/* jacobian is rank-deficient, obtain a least squares solution. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-23 21:04:55 +02:00
|
|
|
nsing = n-1;
|
|
|
|
|
for (j = 0; j < n; ++j) {
|
2009-08-23 21:06:57 +02:00
|
|
|
wa1[j] = qtb[j];
|
2009-08-23 21:47:55 +02:00
|
|
|
if (r(j,j) == 0. && nsing == n-1)
|
2009-08-23 21:06:57 +02:00
|
|
|
nsing = j - 1;
|
|
|
|
|
if (nsing < n-1)
|
|
|
|
|
wa1[j] = 0.;
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
2009-08-23 21:04:55 +02:00
|
|
|
for (k = 0; k <= nsing; ++k) {
|
2009-08-23 21:06:57 +02:00
|
|
|
j = nsing - k;
|
2009-08-23 21:47:55 +02:00
|
|
|
wa1[j] /= r(j,j);
|
2009-08-23 21:06:57 +02:00
|
|
|
temp = wa1[j];
|
2009-08-23 21:47:55 +02:00
|
|
|
for (i = 0; i < j ; ++i)
|
|
|
|
|
wa1[i] -= r(i,j) * temp;
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
2009-08-23 21:04:55 +02:00
|
|
|
|
|
|
|
|
for (j = 0; j < n; ++j) {
|
2009-08-24 08:45:06 +02:00
|
|
|
l = ipvt[j];
|
2009-08-23 21:06:57 +02:00
|
|
|
x[l] = wa1[j];
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* initialize the iteration counter. */
|
|
|
|
|
/* evaluate the function at the origin, and test */
|
|
|
|
|
/* for acceptance of the gauss-newton direction. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
|
|
|
|
iter = 0;
|
2009-08-23 21:52:39 +02:00
|
|
|
wa2 = diag.cwise() * x;
|
2009-08-23 21:04:55 +02:00
|
|
|
dxnorm = wa2.blueNorm();
|
2009-08-22 06:40:22 +02:00
|
|
|
fp = dxnorm - delta;
|
2009-08-24 16:39:49 +02:00
|
|
|
if (fp <= Scalar(0.1) * delta) {
|
|
|
|
|
par = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* if the jacobian is not rank deficient, the newton */
|
|
|
|
|
/* step provides a lower bound, parl, for the zero of */
|
|
|
|
|
/* the function. otherwise set this bound to zero. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
|
|
|
|
parl = 0.;
|
2009-08-24 16:39:49 +02:00
|
|
|
if (nsing >= n-1) {
|
|
|
|
|
for (j = 0; j < n; ++j) {
|
|
|
|
|
l = ipvt[j];
|
|
|
|
|
wa1[j] = diag[l] * (wa2[l] / dxnorm);
|
|
|
|
|
}
|
|
|
|
|
for (j = 0; j < n; ++j) {
|
|
|
|
|
sum = 0.;
|
|
|
|
|
for (i = 0; i < j; ++i)
|
|
|
|
|
sum += r(i,j) * wa1[i];
|
|
|
|
|
wa1[j] = (wa1[j] - sum) / r(j,j);
|
|
|
|
|
}
|
|
|
|
|
temp = wa1.blueNorm();
|
|
|
|
|
parl = fp / delta / temp / temp;
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* calculate an upper bound, paru, for the zero of the function. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-23 21:04:55 +02:00
|
|
|
for (j = 0; j < n; ++j) {
|
2009-08-23 21:06:57 +02:00
|
|
|
sum = 0.;
|
2009-08-23 21:52:39 +02:00
|
|
|
for (i = 0; i <= j; ++i)
|
2009-08-23 21:47:55 +02:00
|
|
|
sum += r(i,j) * qtb[i];
|
2009-08-24 08:45:06 +02:00
|
|
|
l = ipvt[j];
|
2009-08-23 21:06:57 +02:00
|
|
|
wa1[j] = sum / diag[l];
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
2009-08-23 21:04:55 +02:00
|
|
|
gnorm = wa1.stableNorm();
|
2009-08-22 06:40:22 +02:00
|
|
|
paru = gnorm / delta;
|
2009-08-24 16:39:49 +02:00
|
|
|
if (paru == 0.)
|
2009-08-23 21:06:57 +02:00
|
|
|
paru = dwarf / std::min(delta,Scalar(0.1));
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* if the input par lies outside of the interval (parl,paru), */
|
|
|
|
|
/* set par to the closer endpoint. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-22 07:37:23 +02:00
|
|
|
par = std::max(par,parl);
|
|
|
|
|
par = std::min(par,paru);
|
2009-08-23 21:52:39 +02:00
|
|
|
if (par == 0.)
|
2009-08-23 21:06:57 +02:00
|
|
|
par = gnorm / dxnorm;
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* beginning of an iteration. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
while (true) {
|
|
|
|
|
++iter;
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* evaluate the function at the current value of par. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
if (par == 0.)
|
|
|
|
|
par = std::max(dwarf,Scalar(.001) * paru); /* Computing MAX */
|
2009-08-23 21:52:39 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
temp = ei_sqrt(par);
|
|
|
|
|
wa1 = temp * diag;
|
2009-08-23 21:52:39 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
ipvt.cwise()+=1; // qrsolv() expects the fortran convention (as qrfac provides)
|
2009-11-26 03:26:41 +01:00
|
|
|
ei_qrsolv<Scalar>(n, r.data(), r.rows(), ipvt.data(), wa1.data(), qtb.data(), x.data(), sdiag.data());
|
2009-08-24 16:39:49 +02:00
|
|
|
ipvt.cwise()-=1;
|
2009-08-23 21:52:39 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
wa2 = diag.cwise() * x;
|
|
|
|
|
dxnorm = wa2.blueNorm();
|
|
|
|
|
temp = fp;
|
|
|
|
|
fp = dxnorm - delta;
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* if the function is small enough, accept the current value */
|
|
|
|
|
/* of par. also test for the exceptional cases where parl */
|
|
|
|
|
/* is zero or the number of iterations has reached 10. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
if (ei_abs(fp) <= Scalar(0.1) * delta || (parl == 0. && fp <= temp && temp < 0.) || iter == 10)
|
|
|
|
|
break;
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* compute the newton correction. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
for (j = 0; j < n; ++j) {
|
|
|
|
|
l = ipvt[j];
|
|
|
|
|
wa1[j] = diag[l] * (wa2[l] / dxnorm);
|
|
|
|
|
/* L180: */
|
|
|
|
|
}
|
|
|
|
|
for (j = 0; j < n; ++j) {
|
|
|
|
|
wa1[j] /= sdiag[j];
|
|
|
|
|
temp = wa1[j];
|
|
|
|
|
for (i = j+1; i < n; ++i)
|
|
|
|
|
wa1[i] -= r(i,j) * temp;
|
|
|
|
|
}
|
|
|
|
|
temp = wa1.blueNorm();
|
|
|
|
|
parc = fp / delta / temp / temp;
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* depending on the sign of the function, update parl or paru. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
if (fp > 0.)
|
|
|
|
|
parl = std::max(parl,par);
|
|
|
|
|
if (fp < 0.)
|
|
|
|
|
paru = std::min(paru,par);
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* compute an improved estimate for par. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* Computing MAX */
|
|
|
|
|
par = std::max(parl,par+parc);
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* end of an iteration. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
}
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* termination. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
if (iter == 0)
|
2009-08-23 21:06:57 +02:00
|
|
|
par = 0.;
|
2009-08-22 06:40:22 +02:00
|
|
|
return;
|
2009-08-23 21:47:55 +02:00
|
|
|
}
|
2009-08-22 06:40:22 +02:00
|
|
|
|