dogleg.h
Go to the documentation of this file.
1 // IWYU pragma: private
3 
4 namespace Eigen {
5 
6 namespace internal {
7 
8 template <typename Scalar>
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;
93 algo_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
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
#define eigen_assert(x)
Definition: Macros.h:910
SCALAR Scalar
Definition: bench_gemm.cpp:45
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:192
#define min(a, b)
Definition: datatypes.h:22
RealScalar alpha
Definition: level1_cplx_impl.h:151
const char const char const char * diag
Definition: level2_impl.h:86
void dogleg(const Matrix< Scalar, Dynamic, Dynamic > &qrfac, const Matrix< Scalar, Dynamic, 1 > &diag, const Matrix< Scalar, Dynamic, 1 > &qtb, Scalar delta, Matrix< Scalar, Dynamic, 1 > &x)
Definition: dogleg.h:9
EIGEN_DEVICE_FUNC bool abs2(bool x)
Definition: MathFunctions.h:1102
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
std::array< T, N > array
Definition: EmulateArray.h:231
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: Meta.h:75
int delta
Definition: MultiOpt.py:96
Definition: Eigen_Colamd.h:49
double epsilon
Definition: osc_ring_sarah_asymptotics.h:43
list x
Definition: plotDoE.py:28
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2