I'm not a sympy expert, but maybe this can help you:
You can define a Python method, for example:
def f(x): return sin(x)*cos(x)
You can create an evaluation function f1 as a derivative of f using:
from sympy import * x = symbols('x') f1 = lambdify(x, diff(f(x)))
This f1 function can be called from C ++ using boost :: python. You can create an object for the f1 function, call the function using the () operator, and convert the result to double using extract <>.
Here is an example:
namespace py = boost::python; Py_Initialize(); py::object main_module = py::import("__main__"); py::object main_dict = main_module.attr("__dict__"); py::exec( "def f(x):\n" " return sin(x)*cos(x)\n", main_dict ); py::exec( "from sympy import *\n" "x = symbols('x')\n" "f1 = lambdify(x, diff(f(x)))\n", main_dict ); py::object f1 = main_dict["f1"]; std::cout << py::extract<double>(f1(0.0)) << std::endl; std::cout << py::extract<double>(f1(1.0)) << std::endl; return 0;
J. calleja
source share