// Copyright Matthew Pulver 2018 - 2019. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) #include #include #include using namespace boost::math::differentiation; template promote f(const W& w, const X& x, const Y& y, const Z& z) { using namespace std; return exp(w * sin(x * log(y) / z) + sqrt(w * z / (x * y))) + w * w / tan(z); } int main() { using float50 = boost::multiprecision::cpp_bin_float_50; constexpr unsigned Nw = 3; // Max order of derivative to calculate for w constexpr unsigned Nx = 2; // Max order of derivative to calculate for x constexpr unsigned Ny = 4; // Max order of derivative to calculate for y constexpr unsigned Nz = 3; // Max order of derivative to calculate for z // Declare 4 independent variables together into a std::tuple. auto const variables = make_ftuple(11, 12, 13, 14); auto const& w = std::get<0>(variables); // Up to Nw derivatives at w=11 auto const& x = std::get<1>(variables); // Up to Nx derivatives at x=12 auto const& y = std::get<2>(variables); // Up to Ny derivatives at y=13 auto const& z = std::get<3>(variables); // Up to Nz derivatives at z=14 auto const v = f(w, x, y, z); // Calculated from Mathematica symbolic differentiation. float50 const answer("1976.319600747797717779881875290418720908121189218755"); std::cout << std::setprecision(std::numeric_limits::digits10) << "mathematica : " << answer << '\n' << "autodiff : " << v.derivative(Nw, Nx, Ny, Nz) << '\n' << std::setprecision(3) << "relative error: " << (v.derivative(Nw, Nx, Ny, Nz) / answer - 1) << '\n'; return 0; } /* Output: mathematica : 1976.3196007477977177798818752904187209081211892188 autodiff : 1976.3196007477977177798818752904187209081211892188 relative error: 2.67e-50 **/