Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
chkder.h
1// SPDX-FileCopyrightText: The Eigen Authors
2// SPDX-License-Identifier: MPL-2.0
3
4#ifndef EIGEN_NONLINEAROPTIMIZATION_CHKDER_H
5#define EIGEN_NONLINEAROPTIMIZATION_CHKDER_H
6
7#define chkder_log10e 0.43429448190325182765
8#define chkder_factor 100.
9
10// IWYU pragma: private
11#include "./InternalHeaderCheck.h"
12
13namespace Eigen {
14
15namespace internal {
16
17template <typename Scalar>
18void chkder(const Matrix<Scalar, Dynamic, 1> &x, const Matrix<Scalar, Dynamic, 1> &fvec,
19 const Matrix<Scalar, Dynamic, Dynamic> &fjac, Matrix<Scalar, Dynamic, 1> &xp,
20 const Matrix<Scalar, Dynamic, 1> &fvecp, int mode, Matrix<Scalar, Dynamic, 1> &err) {
21 using std::abs;
22 using std::log;
23 using std::sqrt;
24
25 typedef DenseIndex Index;
26
27 const Scalar eps = sqrt(NumTraits<Scalar>::epsilon());
28 const Scalar epsf = chkder_factor * NumTraits<Scalar>::epsilon();
29 const Scalar epslog = chkder_log10e * log(eps);
30 Scalar temp;
31
32 const Index m = fvec.size(), n = x.size();
33
34 if (mode != 2) {
35 /* mode = 1. */
36 xp.resize(n);
37 for (Index j = 0; j < n; ++j) {
38 temp = eps * abs(x[j]);
39 if (temp == 0.) temp = eps;
40 xp[j] = x[j] + temp;
41 }
42 } else {
43 /* mode = 2. */
44 err.setZero(m);
45 for (Index j = 0; j < n; ++j) {
46 temp = abs(x[j]);
47 if (temp == 0.) temp = 1.;
48 err += temp * fjac.col(j);
49 }
50 for (Index i = 0; i < m; ++i) {
51 temp = 1.;
52 if (fvec[i] != 0. && fvecp[i] != 0. && abs(fvecp[i] - fvec[i]) >= epsf * abs(fvec[i]))
53 temp = eps * abs((fvecp[i] - fvec[i]) / eps - err[i]) / (abs(fvec[i]) + abs(fvecp[i]));
54 err[i] = 1.;
55 if (temp > NumTraits<Scalar>::epsilon() && temp < eps) err[i] = (chkder_log10e * log(temp) - epslog) / epslog;
56 if (temp >= eps) err[i] = 0.;
57 }
58 }
59}
60
61} // end namespace internal
62
63} // end namespace Eigen
64
65#undef chkder_log10e
66#undef chkder_factor
67
68#endif // EIGEN_NONLINEAROPTIMIZATION_CHKDER_H
Namespace containing all symbols from the Eigen library.