2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-23 21:39:47 +02:00
|
|
|
template <typename Scalar>
|
|
|
|
|
void ei_dogleg(
|
2009-08-23 21:47:55 +02:00
|
|
|
Matrix< Scalar, Dynamic, 1 > &r,
|
2009-08-23 21:39:47 +02:00
|
|
|
const Matrix< Scalar, Dynamic, 1 > &diag,
|
|
|
|
|
const Matrix< Scalar, Dynamic, 1 > &qtb,
|
|
|
|
|
Scalar delta,
|
|
|
|
|
Matrix< Scalar, Dynamic, 1 > &x)
|
2009-08-22 06:40:22 +02:00
|
|
|
{
|
|
|
|
|
/* Local variables */
|
2009-08-23 21:47:55 +02:00
|
|
|
int i, j, k, l, jj;
|
2009-08-22 06:40:22 +02:00
|
|
|
Scalar sum, temp, alpha, bnorm;
|
2009-08-22 07:31:14 +02:00
|
|
|
Scalar gnorm, qnorm;
|
2009-08-22 06:40:22 +02:00
|
|
|
Scalar sgnorm;
|
|
|
|
|
|
|
|
|
|
/* Function Body */
|
2009-08-22 07:31:14 +02:00
|
|
|
const Scalar epsmch = epsilon<Scalar>();
|
2009-08-23 21:39:47 +02:00
|
|
|
const int n = diag.size();
|
|
|
|
|
Matrix< Scalar, Dynamic, 1 > wa1(n), wa2(n);
|
|
|
|
|
assert(n==qtb.size());
|
|
|
|
|
assert(n==x.size());
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* first, calculate the gauss-newton direction. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-23 21:39:47 +02:00
|
|
|
jj = n * (n + 1) / 2;
|
|
|
|
|
for (k = 0; k < n; ++k) {
|
|
|
|
|
j = n - k - 1;
|
|
|
|
|
jj -= k+1;
|
2009-08-23 21:06:57 +02:00
|
|
|
l = jj + 1;
|
|
|
|
|
sum = 0.;
|
2009-08-23 21:47:55 +02:00
|
|
|
for (i = j+1; i < n; ++i) {
|
|
|
|
|
sum += r[l] * x[i];
|
2009-08-23 21:06:57 +02:00
|
|
|
++l;
|
|
|
|
|
}
|
2009-08-23 21:47:55 +02:00
|
|
|
temp = r[jj];
|
2009-08-23 21:06:57 +02:00
|
|
|
if (temp == 0.) {
|
2009-08-23 21:47:55 +02:00
|
|
|
l = j;
|
|
|
|
|
for (i = 0; i <= j; ++i) {
|
|
|
|
|
/* Computing MAX */
|
|
|
|
|
temp = std::max(temp,ei_abs(r[l]));
|
|
|
|
|
l = l + n - i;
|
|
|
|
|
}
|
|
|
|
|
temp = epsmch * temp;
|
2009-08-24 16:39:49 +02:00
|
|
|
if (temp == 0.)
|
2009-08-23 21:47:55 +02:00
|
|
|
temp = epsmch;
|
2009-08-23 21:06:57 +02:00
|
|
|
}
|
|
|
|
|
x[j] = (qtb[j] - sum) / temp;
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* test whether the gauss-newton direction is acceptable. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-23 21:52:39 +02:00
|
|
|
wa1.fill(0.);
|
|
|
|
|
wa2 = diag.cwise() * x;
|
2009-08-23 21:39:47 +02:00
|
|
|
qnorm = wa2.stableNorm();
|
2009-08-23 21:47:55 +02:00
|
|
|
if (qnorm <= delta)
|
2009-08-22 06:40:22 +02:00
|
|
|
return;
|
|
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* the gauss-newton direction is not acceptable. */
|
|
|
|
|
/* next, calculate the scaled gradient direction. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-23 21:39:47 +02:00
|
|
|
l = 0;
|
|
|
|
|
for (j = 0; j < n; ++j) {
|
2009-08-23 21:06:57 +02:00
|
|
|
temp = qtb[j];
|
2009-08-23 21:39:47 +02:00
|
|
|
for (i = j; i < n; ++i) {
|
2009-08-23 21:47:55 +02:00
|
|
|
wa1[i] += r[l] * temp;
|
2009-08-23 21:06:57 +02:00
|
|
|
++l;
|
|
|
|
|
}
|
|
|
|
|
wa1[j] /= diag[j];
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* calculate the norm of the scaled gradient and test for */
|
|
|
|
|
/* the special case in which the scaled gradient is zero. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-23 21:39:47 +02:00
|
|
|
gnorm = wa1.stableNorm();
|
2009-08-22 06:40:22 +02:00
|
|
|
sgnorm = 0.;
|
|
|
|
|
alpha = delta / qnorm;
|
2009-08-23 21:52:39 +02:00
|
|
|
if (gnorm == 0.)
|
2009-08-24 16:39:49 +02:00
|
|
|
goto algo_end;
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* calculate the point along the scaled gradient */
|
|
|
|
|
/* at which the quadratic is minimized. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-23 21:52:39 +02:00
|
|
|
wa1.cwise() /= diag*gnorm;
|
2009-08-23 21:39:47 +02:00
|
|
|
l = 0;
|
|
|
|
|
for (j = 0; j < n; ++j) {
|
2009-08-23 21:06:57 +02:00
|
|
|
sum = 0.;
|
2009-08-23 21:39:47 +02:00
|
|
|
for (i = j; i < n; ++i) {
|
2009-08-23 21:47:55 +02:00
|
|
|
sum += r[l] * wa1[i];
|
2009-08-23 21:06:57 +02:00
|
|
|
++l;
|
|
|
|
|
/* L100: */
|
|
|
|
|
}
|
|
|
|
|
wa2[j] = sum;
|
|
|
|
|
/* L110: */
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
2009-08-23 21:39:47 +02:00
|
|
|
temp = wa2.stableNorm();
|
2009-08-22 06:40:22 +02:00
|
|
|
sgnorm = gnorm / temp / temp;
|
|
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* test whether the scaled gradient direction is acceptable. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
|
|
|
|
alpha = 0.;
|
2009-08-24 16:39:49 +02:00
|
|
|
if (sgnorm >= delta)
|
|
|
|
|
goto algo_end;
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* the scaled gradient direction is not acceptable. */
|
|
|
|
|
/* finally, calculate the point along the dogleg */
|
|
|
|
|
/* at which the quadratic is minimized. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-23 21:39:47 +02:00
|
|
|
bnorm = qtb.stableNorm();
|
2009-08-22 06:40:22 +02:00
|
|
|
temp = bnorm / gnorm * (bnorm / qnorm) * (sgnorm / delta);
|
2009-08-23 21:06:57 +02:00
|
|
|
/* Computing 2nd power */
|
2009-08-22 07:27:17 +02:00
|
|
|
temp = temp - delta / qnorm * ei_abs2(sgnorm / delta) + ei_sqrt(ei_abs2(temp - delta / qnorm) + (1.-ei_abs2(delta / qnorm)) * (1.-ei_abs2(sgnorm / delta)));
|
2009-08-23 21:06:57 +02:00
|
|
|
/* Computing 2nd power */
|
2009-08-22 07:27:17 +02:00
|
|
|
alpha = delta / qnorm * (1. - ei_abs2(sgnorm / delta)) / temp;
|
2009-08-24 16:39:49 +02:00
|
|
|
algo_end:
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-24 16:39:49 +02:00
|
|
|
/* form appropriate convex combination of the gauss-newton */
|
|
|
|
|
/* direction and the scaled gradient direction. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-23 21:52:39 +02:00
|
|
|
temp = (1.-alpha) * std::min(sgnorm,delta);
|
|
|
|
|
x = temp * wa1 + alpha * x;
|
2009-08-22 06:40:22 +02:00
|
|
|
return;
|
|
|
|
|
|
2009-08-23 21:39:47 +02:00
|
|
|
}
|
2009-08-22 06:40:22 +02:00
|
|
|
|