26 template <
int IN_DIM,
int OUT_DIM>
30 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
32 typedef Eigen::Matrix<double, IN_DIM, 1>
IN_TYPE;
33 typedef Eigen::Matrix<double, OUT_DIM, 1>
OUT_TYPE;
34 typedef Eigen::Matrix<double, OUT_DIM, IN_DIM>
JAC_TYPE;
35 typedef Eigen::Matrix<double, IN_DIM, IN_DIM>
HES_TYPE;
37 typedef std::function<OUT_TYPE(const IN_TYPE&)>
Function;
50 : f_(f), doubleSidedDerivative_(doubleSidedDerivative)
52 eps_ = sqrt(Eigen::NumTraits<double>::epsilon());
56 : f_(arg.f_), doubleSidedDerivative_(arg.doubleSidedDerivative_), eps_(arg.eps_)
64 virtual OUT_TYPE
forwardZero(
const Eigen::VectorXd& x)
override {
return f_(x); }
70 virtual JAC_TYPE
jacobian(
const Eigen::VectorXd& x)
override 75 if (!doubleSidedDerivative_)
80 for (
size_t i = 0;
i < x.rows(); ++
i)
83 double h = eps_ * std::max(std::abs(x(
i)), 1.0);
84 volatile double x_ph = x(
i) + h;
85 double dxp = x_ph - x(
i);
87 IN_TYPE x_perturbed = x;
88 x_perturbed(
i) = x_ph;
91 OUT_TYPE y_perturbed = f_(x_perturbed);
93 if (doubleSidedDerivative_)
95 volatile double x_mh = x(
i) - h;
96 double dxm = x(
i) - x_mh;
99 x_perturbed(
i) = x_mh;
100 OUT_TYPE y_perturbed_low = f_(x_perturbed);
102 jac.template col(
i) = (y_perturbed - y_perturbed_low) / (dxp + dxm);
106 jac.template col(
i) = (y_perturbed - y_ref) / dxp;
114 std::function<OUT_TYPE(const IN_TYPE&)> f_;
116 bool doubleSidedDerivative_;
virtual OUT_TYPE forwardZero(const Eigen::VectorXd &x) override
Evaluates the method itself.
Definition: DerivativesNumDiff.h:64
DerivativesNumDiff(Function &f, bool doubleSidedDerivative=false)
default constructor
Definition: DerivativesNumDiff.h:49
virtual JAC_TYPE jacobian(const Eigen::VectorXd &x) override
evaluate Derivatives at a given point
Definition: DerivativesNumDiff.h:70
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef Eigen::Matrix< double, IN_DIM, 1 > IN_TYPE
function input vector type
Definition: DerivativesNumDiff.h:32
Eigen::Matrix< double, OUT_DIM, 1 > OUT_TYPE
function output vector type
Definition: DerivativesNumDiff.h:33
Eigen::Matrix< double, OUT_DIM, IN_DIM > JAC_TYPE
Definition: DerivativesNumDiff.h:34
Derivatives using Num-Diff Codegeneration.
Definition: DerivativesNumDiff.h:27
General interface class for a Derivatives.
Definition: Derivatives.h:24
std::function< OUT_TYPE(const IN_TYPE &)> Function
function tpye
Definition: DerivativesNumDiff.h:37
DerivativesNumDiff * clone() const override
deep cloning
Definition: DerivativesNumDiff.h:63
virtual ~DerivativesNumDiff()
default destructor
Definition: DerivativesNumDiff.h:61
Eigen::Matrix< double, IN_DIM, IN_DIM > HES_TYPE
Definition: DerivativesNumDiff.h:35
DerivativesNumDiff(const DerivativesNumDiff &arg)
Definition: DerivativesNumDiff.h:55