0

I have written a C++ library that depends on SymEngine (https://github.com/symengine/symengine).
This library API offers functions that have parameter and return variables of types of SymEngine, e.g. Symengine::Symbol.

I would like to make the functionality of this library also available in Python.
I would like to do that with pybind11.

SymEngine offers Python wrappers ("import symengine") and they do this via Cython,
see the corresponding git repo https://github.com/symengine/symengine.py.

For my goal I think two crucial steps are:
How can I access the SymEngine C++ objects from the corresponding Python wrappers?
How can I create a SymEngine Python wrapper object to work with in Python from a C++ object?

2
  • 1
    It is unclear to me where you are stuck at. pybind11 has this example: github.com/pybind/cmake_example/blob/master/src/main.cpp which shows how to export something from c++ to python. Otherwise, can you clarify the issue ? Commented Feb 26 at 11:33
  • @AvinashThakur My problem is rather on the Cython side. I want to have access to the underlying C++ object from the Python wrapper object such that I can use the C++ object in my C++ code. And after that I have to create a Python wrapper object from a SymEngine C++ object that can be used in Python. I tried to clarify my question with an edit. Commented Feb 26 at 11:46

1 Answer 1

1

This is untested code. There might be a few bugs here.

  1. For accessing the C++ value
auto wrapper = py::module::import("symengine.lib.symengine_wrapper");

RCP<const Basic> python_to_cpp(py::object_ python_object) {
  RCP<const Basic> cpp_object;
  py::object_ capsule = PyCapsule_New(&cpp_object, NULL, NULL);
  wrapper.attr("assign_to_capsule")(capsule, python_object);
  return cpp_object
}
  1. For accessing the python value
auto wrapper = py::module::import("symengine.lib.symengine_wrapper");

py::object_ cpp_to_python(RCP<const Basic>& cpp_object) {
  py::object_ capsule = PyCapsule_New(&cpp_object, NULL, NULL);
  return wrapper.attr("capsule_to_basic")(capsule);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.