Files
eigen/unsupported/Eigen/src/NonLinearOptimization/dogleg.h

123 lines
3.3 KiB
C
Raw Normal View History

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)
{
/* Local variables */
2009-08-23 21:47:55 +02:00
int i, j, k, l, jj;
Scalar sum, temp, alpha, bnorm;
2009-08-22 07:31:14 +02:00
Scalar gnorm, qnorm;
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-24 16:39:49 +02:00
/* first, calculate the gauss-newton direction. */
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;
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];
++l;
}
2009-08-23 21:47:55 +02:00
temp = r[jj];
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 - 1;
2009-08-23 21:47:55 +02:00
}
temp = epsmch * temp;
2009-08-24 16:39:49 +02:00
if (temp == 0.)
2009-08-23 21:47:55 +02:00
temp = epsmch;
}
x[j] = (qtb[j] - sum) / temp;
}
2009-08-24 16:39:49 +02:00
/* test whether the gauss-newton direction is acceptable. */
wa1.fill(0.);
2010-01-05 15:38:20 +01:00
wa2 = diag.cwiseProduct(x);
2009-08-23 21:39:47 +02:00
qnorm = wa2.stableNorm();
2009-08-23 21:47:55 +02:00
if (qnorm <= delta)
return;
2009-08-24 16:39:49 +02:00
/* the gauss-newton direction is not acceptable. */
/* next, calculate the scaled gradient direction. */
2009-08-23 21:39:47 +02:00
l = 0;
for (j = 0; j < n; ++j) {
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;
++l;
}
wa1[j] /= diag[j];
}
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-23 21:39:47 +02:00
gnorm = wa1.stableNorm();
sgnorm = 0.;
alpha = delta / qnorm;
if (gnorm == 0.)
2009-08-24 16:39:49 +02:00
goto algo_end;
2009-08-24 16:39:49 +02:00
/* calculate the point along the scaled gradient */
/* at which the quadratic is minimized. */
2010-01-05 15:38:20 +01:00
wa1.array() /= (diag*gnorm).array();
2009-08-23 21:39:47 +02:00
l = 0;
for (j = 0; j < n; ++j) {
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];
++l;
/* L100: */
}
wa2[j] = sum;
/* L110: */
}
2009-08-23 21:39:47 +02:00
temp = wa2.stableNorm();
sgnorm = gnorm / temp / temp;
2009-08-24 16:39:49 +02:00
/* test whether the scaled gradient direction is acceptable. */
alpha = 0.;
2009-08-24 16:39:49 +02:00
if (sgnorm >= delta)
goto algo_end;
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-23 21:39:47 +02:00
bnorm = qtb.stableNorm();
temp = bnorm / gnorm * (bnorm / qnorm) * (sgnorm / delta);
/* 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)));
/* 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-24 16:39:49 +02:00
/* form appropriate convex combination of the gauss-newton */
/* direction and the scaled gradient direction. */
temp = (1.-alpha) * std::min(sgnorm,delta);
x = temp * wa1 + alpha * x;
2009-08-23 21:39:47 +02:00
}