13#ifndef DUMUX_NUMERIC_DIFFERENTIATION_HH
14#define DUMUX_NUMERIC_DIFFERENTIATION_HH
35 template<
class Scalar>
36 static Scalar
epsilon(
const Scalar value,
const Scalar baseEps = 1e-10)
38 assert(std::numeric_limits<Scalar>::epsilon()*1e4 < baseEps);
42 return baseEps*(abs(value) + 1.0);
49 template<
class Function,
class Scalar,
class FunctionEvalType>
51 FunctionEvalType& derivative,
52 const FunctionEvalType& fx0,
53 const int numericDifferenceMethod = 1)
66 template<
class Function,
class Scalar,
class EpsType,
class FunctionEvalType>
68 FunctionEvalType& derivative,
69 const FunctionEvalType& fx0,
71 const int numericDifferenceMethod = 1)
73 std::is_same_v<Scalar, decltype(std::declval<Scalar>() + std::declval<EpsType>())>
74 && std::is_same_v<Scalar,
decltype(std::declval<Scalar>() - std::declval<EpsType>())>
79 if (numericDifferenceMethod == 5)
81 derivative = function(x0 + eps);
82 derivative -= function(x0 - eps);
84 derivative += function(x0 - 2.0*eps);
85 derivative -= function(x0 + 2.0*eps);
86 derivative /= 12.0*eps;
94 if (numericDifferenceMethod >= 0)
98 derivative = function(x0 + eps);
104 else derivative = fx0;
108 if (numericDifferenceMethod <= 0)
112 derivative -= function(x0 - eps);
118 else derivative -= fx0;
A class for numeric differentiation with respect to a scalar parameter.
Definition: numericdifferentiation.hh:27
static void partialDerivative(const Function &function, Scalar x0, FunctionEvalType &derivative, const FunctionEvalType &fx0, const EpsType eps, const int numericDifferenceMethod=1)
Computes the derivative of a function with respect to a function parameter.
Definition: numericdifferentiation.hh:67
static Scalar epsilon(const Scalar value, const Scalar baseEps=1e-10)
Computes the epsilon used for numeric differentiation.
Definition: numericdifferentiation.hh:36
static void partialDerivative(const Function &function, Scalar x0, FunctionEvalType &derivative, const FunctionEvalType &fx0, const int numericDifferenceMethod=1)
Computes the derivative of a function with respect to a function parameter.
Definition: numericdifferentiation.hh:50