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