Numerical differentiation

Nonsymmetric input and result

template<typename _Position = void, typename _Function, typename _Point>
auto tmech::num_diff_central(_Function __func, _Point const &__x, double const __h = 1e-7)

Numerical differentiation based on central difference scheme

f'(x) \approx \frac{f(x+h) - f(x-h)}{2h}

.

tmech::tensor<double,3,2> X;
X.randn();
auto func = [&](auto const& F){
            return 1.5*(tmech::trace(tmech::trans(F)*F) - 3);
            };

auto dFunc = tmech::num_diff_central(func, X);
auto dFunc_ = [&](auto const& F){
              return tmech::num_diff_central(func, F);
              };
auto ddFunc = tmech::num_diff_central<tmech::sequence<1,2,3,4>>(func, X);

Parameters
  • func: Input type/function f(x), used for the numerical differentiation. This type/function needs to provide an overloaded access operator(x).

  • x: A given point, at which the function is evaluated.

  • __dx: Is

Symmetric input and result

template<typename _SymDirection, typename _SymResult = std::tuple<>, typename _Function, typename _Point>
auto tmech::num_diff_sym_central(_Function __func, _Point const &__x, double const __h = 5e-6)

Symmetric numerical differentiation based on central difference scheme

f'(x) \approx \frac{f(x+h) - f(x-h)}{2h}

.

//symmetry of the input
using Sym2x2 = std::tuple<tmech::sequence<1,2>,tmech::sequence<2,1>>;
//symmetry of the result
using Sym4x4 = std::tuple<
               tmech::sequence<1,2,3,4>,
               tmech::sequence<2,1,3,4>,
               tmech::sequence<1,2,4,3>,
               tmech::sequence<2,1,4,3>>;
tmech::tensor<double,3,2> X;
X = tmech::sym(tmech::rand<double,3,2>());
//first deriv
auto func = [&](auto const& F){
            return 1.5*(tmech::trace(tmech::trans(F)*F) - 3);
            };
auto dfunc_res = tmech::num_diff_sym_central<Sym2x2>(func, X);

//second deriv
auto dfunc = [&](auto const& F){
              return tmech::num_diff_sym_central<Sym2x2>(func, F);
              };
auto ddfunc_res = tmech::num_diff_sym_central<Sym2x2, Sym4x4>(dfunc, X);

Parameters
  • func: Input type/function f(x), used for the numerical differentiation. This type/function needs to provide an overloaded access operator(x).

  • x: A given point, at which the function is evaluated.

  • __dx: Is