Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
NumericalDiff.h
1// -*- coding: utf-8
2// vim: set fileencoding=utf-8
3// SPDX-License-Identifier: MPL-2.0
4
5// This file is part of Eigen, a lightweight C++ template library
6// for linear algebra.
7//
8// Copyright (C) 2009 Thomas Capricelli <orzel@freehackers.org>
9//
10// This Source Code Form is subject to the terms of the Mozilla
11// Public License v. 2.0. If a copy of the MPL was not distributed
12// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
13
14#ifndef EIGEN_NUMERICAL_DIFF_H
15#define EIGEN_NUMERICAL_DIFF_H
16
17// IWYU pragma: private
18#include "./InternalHeaderCheck.h"
19
20namespace Eigen {
21
22enum NumericalDiffMode { Forward, Central };
23
35template <typename Functor_, NumericalDiffMode mode = Forward>
36class NumericalDiff : public Functor_ {
37 public:
38 using Functor = Functor_;
39 using Scalar = typename Functor::Scalar;
40 using InputType = typename Functor::InputType;
41 using ValueType = typename Functor::ValueType;
42 using JacobianType = typename Functor::JacobianType;
43
44 private:
45 // Fallback/Forward mode helper
46 static Scalar defaultRelTol() {
47 return mode == Central ? numext::cbrt(NumTraits<Scalar>::epsilon()) : numext::sqrt(NumTraits<Scalar>::epsilon());
48 }
49
50 public:
51 NumericalDiff(Scalar epsfcn = 0.)
52 : Functor(),
53 rel_tol(mode == Central ? numext::cbrt(numext::maxi(epsfcn, NumTraits<Scalar>::epsilon()))
54 : numext::sqrt(numext::maxi(epsfcn, NumTraits<Scalar>::epsilon()))) {
55 setAbsTol(rel_tol);
56 }
57 NumericalDiff(const Functor& f, Scalar epsfcn = 0.)
58 : Functor(f),
59 rel_tol(mode == Central ? numext::cbrt(numext::maxi(epsfcn, NumTraits<Scalar>::epsilon()))
60 : numext::sqrt(numext::maxi(epsfcn, NumTraits<Scalar>::epsilon()))) {
61 setAbsTol(rel_tol);
62 }
63
64 // forward constructors
65 template <typename T0, std::enable_if_t<std::is_constructible<Functor, const T0&>::value, int> = 0>
66 NumericalDiff(const T0& a0) : Functor(a0), rel_tol(defaultRelTol()), abs_tol(defaultRelTol()) {}
67
68 template <typename T0, typename T1,
69 std::enable_if_t<std::is_constructible<Functor, const T0&, const T1&>::value, int> = 0>
70 NumericalDiff(const T0& a0, const T1& a1) : Functor(a0, a1), rel_tol(defaultRelTol()), abs_tol(defaultRelTol()) {}
71
72 template <typename T0, typename T1, typename T2,
73 std::enable_if_t<std::is_constructible<Functor, const T0&, const T1&, const T2&>::value, int> = 0>
74 NumericalDiff(const T0& a0, const T1& a1, const T2& a2)
75 : Functor(a0, a1, a2), rel_tol(defaultRelTol()), abs_tol(defaultRelTol()) {}
76
77 // Setters/getters for tolerances
78 void setRelTol(Scalar _rel_tol) {
79 eigen_assert(_rel_tol >= NumTraits<Scalar>::epsilon());
80 rel_tol = _rel_tol;
81 }
82 void setAbsTol(Scalar _abs_tol) {
83 eigen_assert(_abs_tol > 0);
84 abs_tol = _abs_tol;
85 }
86 Scalar relTol() const { return rel_tol; }
87 Scalar absTol() const { return abs_tol; }
88
89 enum { InputsAtCompileTime = Functor::InputsAtCompileTime, ValuesAtCompileTime = Functor::ValuesAtCompileTime };
90
103 int df(const InputType& _x, JacobianType& jac) const {
104 using std::abs;
105 /* Local variables */
106 Scalar h;
107 int nfev = 0;
108 const typename InputType::Index n = _x.size();
109 ValueType val1, val2;
110 InputType x = _x;
111 // TODO: We should do this only if the size is not already known.
112 val1.resize(Functor::values());
113 val2.resize(Functor::values());
114
115 // initialization
116 switch (mode) {
117 case Forward:
118 // compute f(x)
119 Functor::operator()(x, val1);
120 nfev++;
121 break;
122 case Central:
123 // do nothing
124 break;
125 default:
126 eigen_assert(false);
127 }
128
129 // Function Body
130 for (int j = 0; j < n; ++j) {
131 const Scalar x_abs = abs(x[j]);
132 h = numext::maxi(x_abs * rel_tol, abs_tol);
133 // The functor is evaluated at fl(x[j] + h), so divide by that representable step: the rounding
134 // of x[j] + h perturbs h by up to ulp(x[j]) <= epsilon |x[j]| <= (epsilon/rel_tol) * h <= h, comparable
135 // to the error of the difference quotient itself.
136 Scalar x_plus = _x[j] + h;
137 // Keeps -ffast-math from folding (x[j] + h) - x[j] back into h.
138 internal::pbarrier(x_plus);
139 h = x_plus - _x[j];
140 switch (mode) {
141 case Forward:
142 x[j] = x_plus;
143 Functor::operator()(x, val2);
144 nfev++;
145 x[j] = _x[j];
146 jac.col(j) = (val2 - val1) / h;
147 break;
148 case Central: {
149 x[j] = x_plus;
150 Functor::operator()(x, val2);
151 nfev++;
152 // x[j] - h can round (a tie when x[j] < 0 has |x[j]| within h above a power of two), so
153 // divide by the separation of the two evaluation points rather than by 2*h.
154 Scalar x_minus = _x[j] - h;
155 internal::pbarrier(x_minus);
156 x[j] = x_minus;
157 Functor::operator()(x, val1);
158 nfev++;
159 x[j] = _x[j];
160 jac.col(j) = (val2 - val1) / (x_plus - x_minus);
161 break;
162 }
163 default:
164 eigen_assert(false);
165 }
166 }
167 return nfev;
168 }
169
170 private:
171 Scalar rel_tol;
172 Scalar abs_tol;
173
174 NumericalDiff& operator=(const NumericalDiff&) = delete;
175};
176
177} // end namespace Eigen
178
179// vim: ai ts=4 sts=4 et sw=4
180#endif // EIGEN_NUMERICAL_DIFF_H
Definition NumericalDiff.h:36
int df(const InputType &_x, JacobianType &jac) const
Definition NumericalDiff.h:103
Namespace containing all symbols from the Eigen library.