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
51template <typename Functor_, NumericalDiffMode mode = Forward>
52class NumericalDiff : public Functor_ {
53 public:
54 using Functor = Functor_;
55 using Scalar = typename Functor::Scalar;
56 using InputType = typename Functor::InputType;
57 using ValueType = typename Functor::ValueType;
58 using JacobianType = typename Functor::JacobianType;
59
60 private:
61 // Fallback/Forward mode helper
62 static Scalar defaultRelTol() {
63 return mode == Central ? numext::cbrt(NumTraits<Scalar>::epsilon()) : numext::sqrt(NumTraits<Scalar>::epsilon());
64 }
65
66 public:
69 NumericalDiff(Scalar epsfcn = 0.)
70 : Functor(),
71 rel_tol(mode == Central ? numext::cbrt(numext::maxi(epsfcn, NumTraits<Scalar>::epsilon()))
72 : numext::sqrt(numext::maxi(epsfcn, NumTraits<Scalar>::epsilon()))) {
73 setAbsTol(rel_tol);
74 }
75
76 NumericalDiff(const Functor& f, Scalar epsfcn = 0.)
77 : Functor(f),
78 rel_tol(mode == Central ? numext::cbrt(numext::maxi(epsfcn, NumTraits<Scalar>::epsilon()))
79 : numext::sqrt(numext::maxi(epsfcn, NumTraits<Scalar>::epsilon()))) {
80 setAbsTol(rel_tol);
81 }
82
83 // forward constructors
84 template <typename T0, std::enable_if_t<std::is_constructible<Functor, const T0&>::value, int> = 0>
85 NumericalDiff(const T0& a0) : Functor(a0), rel_tol(defaultRelTol()), abs_tol(defaultRelTol()) {}
86
87 template <typename T0, typename T1,
88 std::enable_if_t<std::is_constructible<Functor, const T0&, const T1&>::value, int> = 0>
89 NumericalDiff(const T0& a0, const T1& a1) : Functor(a0, a1), rel_tol(defaultRelTol()), abs_tol(defaultRelTol()) {}
90
91 template <typename T0, typename T1, typename T2,
92 std::enable_if_t<std::is_constructible<Functor, const T0&, const T1&, const T2&>::value, int> = 0>
93 NumericalDiff(const T0& a0, const T1& a1, const T2& a2)
94 : Functor(a0, a1, a2), rel_tol(defaultRelTol()), abs_tol(defaultRelTol()) {}
95
97 void setRelTol(Scalar _rel_tol) {
98 eigen_assert(_rel_tol >= NumTraits<Scalar>::epsilon());
99 rel_tol = _rel_tol;
100 }
101
105 void setAbsTol(Scalar _abs_tol) {
106 eigen_assert(_abs_tol >= 0);
107 abs_tol = _abs_tol;
108 }
109
110 Scalar relTol() const { return rel_tol; }
112 Scalar absTol() const { return abs_tol; }
113
114 enum { InputsAtCompileTime = Functor::InputsAtCompileTime, ValuesAtCompileTime = Functor::ValuesAtCompileTime };
115
129 int df(const InputType& _x, JacobianType& jac) const {
130 using std::abs;
131 /* Local variables */
132 Scalar h;
133 int nfev = 0;
134 const typename InputType::Index n = _x.size();
135 ValueType val1, val2;
136 InputType x = _x;
137 // TODO: We should do this only if the size is not already known.
138 val1.resize(Functor::values());
139 val2.resize(Functor::values());
140
141 // initialization
142 switch (mode) {
143 case Forward:
144 // compute f(x)
145 Functor::operator()(x, val1);
146 nfev++;
147 break;
148 case Central:
149 // do nothing
150 break;
151 default:
152 eigen_assert(false);
153 }
154
155 // Function Body
156 for (int j = 0; j < n; ++j) {
157 const Scalar x_abs = abs(x[j]);
158 h = numext::maxi(x_abs * rel_tol, abs_tol);
159 if (h == Scalar(0)) h = rel_tol;
160 // The functor is evaluated at fl(x[j] + h), so divide by that representable step: the rounding
161 // of x[j] + h perturbs h by up to ulp(x[j]) <= epsilon |x[j]| <= (epsilon/rel_tol) * h <= h, comparable
162 // to the error of the difference quotient itself.
163 Scalar x_plus = _x[j] + h;
164 // Keeps -ffast-math from folding (x[j] + h) - x[j] back into h.
165 internal::pbarrier(x_plus);
166 h = x_plus - _x[j];
167 switch (mode) {
168 case Forward:
169 x[j] = x_plus;
170 Functor::operator()(x, val2);
171 nfev++;
172 x[j] = _x[j];
173 jac.col(j) = (val2 - val1) / h;
174 break;
175 case Central: {
176 x[j] = x_plus;
177 Functor::operator()(x, val2);
178 nfev++;
179 // x[j] - h can round (a tie when x[j] < 0 has |x[j]| within h above a power of two), so
180 // divide by the separation of the two evaluation points rather than by 2*h.
181 Scalar x_minus = _x[j] - h;
182 internal::pbarrier(x_minus);
183 x[j] = x_minus;
184 Functor::operator()(x, val1);
185 nfev++;
186 x[j] = _x[j];
187 jac.col(j) = (val2 - val1) / (x_plus - x_minus);
188 break;
189 }
190 default:
191 eigen_assert(false);
192 }
193 }
194 return nfev;
195 }
196
197 private:
198 Scalar rel_tol;
199 Scalar abs_tol;
200
201 NumericalDiff& operator=(const NumericalDiff&) = delete;
202};
203
204} // end namespace Eigen
205
206// vim: ai ts=4 sts=4 et sw=4
207#endif // EIGEN_NUMERICAL_DIFF_H
Definition NumericalDiff.h:52
NumericalDiff(Scalar epsfcn=0.)
Definition NumericalDiff.h:69
int df(const InputType &_x, JacobianType &jac) const
Definition NumericalDiff.h:129
NumericalDiff(const Functor &f, Scalar epsfcn=0.)
Definition NumericalDiff.h:76
Scalar absTol() const
Definition NumericalDiff.h:112
Scalar relTol() const
Definition NumericalDiff.h:110
void setAbsTol(Scalar _abs_tol)
Definition NumericalDiff.h:105
void setRelTol(Scalar _rel_tol)
Definition NumericalDiff.h:97
Namespace containing all symbols from the Eigen library.