2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-23 21:39:47 +02:00
|
|
|
template <typename Scalar>
|
|
|
|
|
void ei_dogleg(
|
2010-01-26 05:59:58 +01:00
|
|
|
const Matrix< Scalar, Dynamic, Dynamic > &qrfac,
|
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 */
|
2010-01-26 05:59:58 +01:00
|
|
|
int i, j, k;
|
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-23 21:39:47 +02:00
|
|
|
for (k = 0; k < n; ++k) {
|
|
|
|
|
j = n - k - 1;
|
2009-08-23 21:06:57 +02:00
|
|
|
sum = 0.;
|
2009-08-23 21:47:55 +02:00
|
|
|
for (i = j+1; i < n; ++i) {
|
2010-01-26 05:59:58 +01:00
|
|
|
sum += qrfac(j,i) * x[i];
|
2009-08-23 21:06:57 +02:00
|
|
|
}
|
2010-01-26 05:59:58 +01:00
|
|
|
temp = qrfac(j,j);
|
2009-08-23 21:06:57 +02:00
|
|
|
if (temp == 0.) {
|
2010-01-26 05:59:58 +01:00
|
|
|
for (i = 0; i <= j; ++i)
|
|
|
|
|
temp = std::max(temp,ei_abs(qrfac(i,j)));
|
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;
|
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.);
|
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)
|
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
|
|
|
for (j = 0; j < n; ++j) {
|
2009-08-23 21:06:57 +02:00
|
|
|
temp = qtb[j];
|
2010-01-26 05:59:58 +01:00
|
|
|
for (i = j; i < n; ++i)
|
|
|
|
|
wa1[i] += qrfac(j,i) * temp;
|
2009-08-23 21:06:57 +02:00
|
|
|
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
|
|
|
|
2010-01-05 15:38:20 +01:00
|
|
|
wa1.array() /= (diag*gnorm).array();
|
2009-08-23 21:39:47 +02:00
|
|
|
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) {
|
2010-01-26 05:59:58 +01:00
|
|
|
sum += qrfac(j,i) * wa1[i];
|
2009-08-23 21:06:57 +02:00
|
|
|
}
|
|
|
|
|
wa2[j] = sum;
|
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-23 21:39:47 +02:00
|
|
|
}
|
2009-08-22 06:40:22 +02:00
|
|
|
|