pybind11

PyBind11: boost::multiprecision::cpp_int to Python

早过忘川 提交于 2019-12-11 13:18:19
问题 I am interested in using pybind11 to optimize some Python computation using C++. The casting documentation doesn't make much sense to me and was wondering if anyone knew how to cast boost datatypes, specifically cpp_int, to a Python datatype so I can return computations. A simple example of what I'm trying to do would be factorials: #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <boost/multiprecision/cpp_int.hpp> using boost::multiprecision::cpp_int; namespace py = pybind11

Pybind11: passing a string* argument to a constructor

眉间皱痕 提交于 2019-12-11 00:24:32
问题 In a C++ library that I'm not allowed to change I have a constructor that looks like this: Dfa(const int n_state, const int dim_alf, const string *alf); If I simply bind with .def(py::init<const int, const int, const std::string*>()) it compiles succesfully. The problem is that I can't pass a string* by python, because for example if I try to execute on python alph=['x','y'] z=Dfa(3,2,alph) It returns the following error: TypeError: __init__(): incompatible constructor arguments. The

passing pointer to C++ from python using pybind11

瘦欲@ 提交于 2019-12-08 22:05:27
问题 I have created the following class using pybind11: py::class_<Raster>(m, "Raster") .def(py::init<double*, std::size_t, std::size_t, std::size_t, double, double, double>()); However I have no idea how I would call this constructor in Python.. I see that Python expects a float in the place of the double*, but I cannot seem to call it. I have tried, ctypes.data_as(ctypes.POINTER(ctypes.c_double)) but this does not work... Edit: I have distilled the answer from @Sergei answer. py::class_<Raster>

Pybind11 or Boost.Python or neither-

早过忘川 提交于 2019-12-08 15:56:13
问题 I'm curious what the most flexible, most efficient, and most seamless method is for getting C++ and Python to talk to each other. The contenders seem to be Pybind11, Boost.Python, and neither (simply writing functions and wrappers as below). using namespace boost::algorithm; static PyObject* strtest(PyObject* self, PyObject* args) { std::string s = "Boost C++ Libraries"; to_upper(s); PyObject * python_val = Py_BuildValue("s", s.c_str()); return python_val; } PyMODINIT_FUNC initmath_demo(void)

PyBind11 Template Class of Many Types

依然范特西╮ 提交于 2019-12-07 06:11:13
问题 I'd like to use PyBind11 to wrap a specialized array class. However, the array is available in many flavours (one per each plain-old-datatype). The code looks like this: py::class_<Array2D<float>>(m, "Array2Dfloat", py::buffer_protocol(), py::dynamic_attr()) .def(py::init<>()) .def(py::init<Array2D<float>::xy_t,Array2D<float>::xy_t,float>()) .def("size", &Array2D<float>::size) .def("width", &Array2D<float>::width) .def("height", &Array2D<float>::height) //... //... The only way I've thought

PyBind11 Template Class of Many Types

泄露秘密 提交于 2019-12-05 10:34:55
I'd like to use PyBind11 to wrap a specialized array class. However, the array is available in many flavours (one per each plain-old-datatype). The code looks like this: py::class_<Array2D<float>>(m, "Array2Dfloat", py::buffer_protocol(), py::dynamic_attr()) .def(py::init<>()) .def(py::init<Array2D<float>::xy_t,Array2D<float>::xy_t,float>()) .def("size", &Array2D<float>::size) .def("width", &Array2D<float>::width) .def("height", &Array2D<float>::height) //... //... The only way I've thought of to tell PyBind11 about these classes is by duplicating the above for each POD through the use of a

pybind11: how to package c++ and python code into a single package?

大憨熊 提交于 2019-12-03 04:03:59
I am trying to package together an existing Python code and a new C++ 11 code using CMake and pybind 11. I think I am missing something simple to add into CMake scripts, but can't find it anywhere: pybind11 examples have only C++ code and none of Python, other online resources are rather convoluted and not up-to-date -- so I just can't figure out how to package functions in both languages together and make them available via Python's import my_package down the line... as an example, I have cloned the cmake_example from pybind11 and added a mult function into cmake_example/mult.py def mult(a, b

Pybind11 doesn't work or C++ doesn't compile after upgrading to Mojave: -lstdc++ not found

两盒软妹~` 提交于 2019-11-30 09:47:31
问题 I was able to install the pybind11 example here before I upgraded my MacOS. However, after I upgraded my MacOS to Mojave, when I compile the same example on that link, I see the following error: clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated] ld: library not found for -lstdc++ clang: error: linker command failed with exit code 1 (use -v to see invocation) error: command 'g++' failed with exit status 1 Am I missing a linker

Pybind11 doesn't work or C++ doesn't compile after upgrading to Mojave: -lstdc++ not found

旧巷老猫 提交于 2019-11-29 18:10:48
I was able to install the pybind11 example here before I upgraded my MacOS. However, after I upgraded my MacOS to Mojave, when I compile the same example on that link, I see the following error: clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated] ld: library not found for -lstdc++ clang: error: linker command failed with exit code 1 (use -v to see invocation) error: command 'g++' failed with exit status 1 Am I missing a linker command? I've searched online but couldn't find a solution. I actually found the answer. I only needed

Pybind Numpy access 2D / ND arrays

蹲街弑〆低调 提交于 2019-11-27 14:15:54
问题 New to pybind - read the documentation but I do not grasp how to apply it to 2D arrays. I have two arrays storing 3d coordinates shape = (10,3) a = np.zeros(shape=(10,3)) b = np.ones(shape=(10,3)) * 3 c = a + b Now, using pybind, how do I perform this operation in C++ working on the numpy arrays? In some documentations I read to access the elements with the [] operator, in others with () . How do assign the 3D vector? How would I get the pointer to the array element to use strides for