Eigen-unsupported  5.0.1-dev+284dcc12
 
Loading...
Searching...
No Matches
dogleg.h
1// IWYU pragma: private
2#include "./InternalHeaderCheck.h"
3
4namespace Eigen {
5
6namespace internal {
7
8template <typename Scalar>
9void dogleg(const Matrix<Scalar, Dynamic, Dynamic> &qrfac, const Matrix<Scalar, Dynamic, 1> &diag,
10 const Matrix<Scalar, Dynamic, 1> &qtb, Scalar delta, Matrix<Scalar, Dynamic, 1> &x) {
11 using std::abs;
12 using std::sqrt;
13
14 typedef DenseIndex Index;
15
16 /* Local variables */
17 Index i, j;
18 Scalar sum, temp, alpha, bnorm;
19 Scalar gnorm, qnorm;
20 Scalar sgnorm;
21
22 /* Function Body */
23 const Scalar epsmch = NumTraits<Scalar>::epsilon();
24 const Index n = qrfac.cols();
25 eigen_assert(n == qtb.size());
26 eigen_assert(n == x.size());
27 eigen_assert(n == diag.size());
28 Matrix<Scalar, Dynamic, 1> wa1(n), wa2(n);
29
30 /* first, calculate the gauss-newton direction. */
31 for (j = n - 1; j >= 0; --j) {
32 temp = qrfac(j, j);
33 if (temp == 0.) {
34 temp = epsmch * qrfac.col(j).head(j + 1).maxCoeff();
35 if (temp == 0.) temp = epsmch;
36 }
37 if (j == n - 1)
38 x[j] = qtb[j] / temp;
39 else
40 x[j] = (qtb[j] - qrfac.row(j).tail(n - j - 1).dot(x.tail(n - j - 1))) / temp;
41 }
42
43 /* test whether the gauss-newton direction is acceptable. */
44 qnorm = diag.cwiseProduct(x).stableNorm();
45 if (qnorm <= delta) return;
46
47 // TODO : this path is not tested by Eigen unit tests
48
49 /* the gauss-newton direction is not acceptable. */
50 /* next, calculate the scaled gradient direction. */
51
52 wa1.fill(0.);
53 for (j = 0; j < n; ++j) {
54 wa1.tail(n - j) += qrfac.row(j).tail(n - j) * qtb[j];
55 wa1[j] /= diag[j];
56 }
57
58 /* calculate the norm of the scaled gradient and test for */
59 /* the special case in which the scaled gradient is zero. */
60 gnorm = wa1.stableNorm();
61 sgnorm = 0.;
62 alpha = delta / qnorm;
63 if (gnorm == 0.) goto algo_end;
64
65 /* calculate the point along the scaled gradient */
66 /* at which the quadratic is minimized. */
67 wa1.array() /= (diag * gnorm).array();
68 // TODO : once unit tests cover this part,:
69 // wa2 = qrfac.template triangularView<Upper>() * wa1;
70 for (j = 0; j < n; ++j) {
71 sum = 0.;
72 for (i = j; i < n; ++i) {
73 sum += qrfac(j, i) * wa1[i];
74 }
75 wa2[j] = sum;
76 }
77 temp = wa2.stableNorm();
78 sgnorm = gnorm / temp / temp;
79
80 /* test whether the scaled gradient direction is acceptable. */
81 alpha = 0.;
82 if (sgnorm >= delta) goto algo_end;
83
84 /* the scaled gradient direction is not acceptable. */
85 /* finally, calculate the point along the dogleg */
86 /* at which the quadratic is minimized. */
87 bnorm = qtb.stableNorm();
88 temp = bnorm / gnorm * (bnorm / qnorm) * (sgnorm / delta);
89 temp = temp - delta / qnorm * numext::abs2(sgnorm / delta) +
90 sqrt(numext::abs2(temp - delta / qnorm) +
91 (1. - numext::abs2(delta / qnorm)) * (1. - numext::abs2(sgnorm / delta)));
92 alpha = delta / qnorm * (1. - numext::abs2(sgnorm / delta)) / temp;
93algo_end:
94
95 /* form appropriate convex combination of the gauss-newton */
96 /* direction and the scaled gradient direction. */
97 temp = (1. - alpha) * (std::min)(sgnorm, delta);
98 x = temp * wa1 + alpha * x;
99}
100
101} // end namespace internal
102
103} // end namespace Eigen
Namespace containing all symbols from the Eigen library.
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index