Added generic numerical differentiation algorithm
-
Feature description was added to the changelog -
Tests covering your feature were added? -
Any new feature or behavior change was documented?
- Other people write an entire FEM solver in less than 180 lines of code. This MR is different. It writes d/dx in more than 280 lines of code. Big is beautiful.
🐘 ❤ 🐳 - Supports forward and central differences.
How to use
NumLib::NumericalDerivative<NumLib::CentralDifferencesStrategy> deriv{
NumLib::RelativeEpsilon{rel_eps},
NumLib::MinimumPerturbation{min_pert}};
auto const [dfdt, dfdxy, dfdz] =
deriv(&fNumDiffVectorial, t, Eigen::Vector2d{x, y}, z);
Should work with all functions (plain functions, function objects, lambdas) that take double values or Eigen (column) vectors as input and return values with basic arithmetic capabilites (subtraction and division), e.g.:
struct NumDiffXY
{
NumDiffXY(double x, double y) : x{x}, y{y} {}
explicit NumDiffXY(Eigen::Vector2d const& xy) : x{xy[0]}, y{xy[1]} {}
NumDiffXY operator-(NumDiffXY const& other) const
{
return {x - other.x, y - other.y};
}
NumDiffXY operator/(double a) const { return {x / a, y / a}; }
double x;
double y;
};
Edited by Christoph Lehmann