[C++-sig] Accessing 2D array from python in C++
alfa
alfarobi0161 at yahoo.com
Wed Aug 15 10:47:30 CEST 2012
Hi Jim,
Do you mean, "you could wrap multi_array itself using class_", I should do as below:
boost::python::class_<std::vector<std::vector<double> > >("PyVec").def(boost::python::vector_indexing_suite<std::vector<std::vector<double> > >());
I also did as below to expose the data from python to C++:
main_namespace["Vp"] = boost::python::ptr(&Vp_cpp);
However, when I try to access the data to the std::cout, like the following, it complains:
std::cout << "Vp_cpp = " << Vp_cpp << std::endl;
What could be wrong or am I missing something? Thanks in advance.
________________________________
From: Jim Bosch <talljimbo at gmail.com>
To: Development of Python/C++ integration <cplusplus-sig at python.org>; shahmi <alfarobi0161 at yahoo.com>
Sent: Tuesday, August 14, 2012 5:54 PM
Subject: Re: [C++-sig] Accessing 2D array from python in C++
On Aug 14, 2012 4:25 AM, "shahmi" <alfarobi0161 at yahoo.com> wrote:
>
> Hi,
>
> I have tried to access/expose 2D array from python in C++ using boost::multi_array. Nevertheless, it seems that the boost::multi_array<double, 2> datatype is not supported. Is there any way to resolve this? My way of accessing it as below:
>
> boost::python::object Vp = main_namespace["Vp"];
> Vp_cpp = boost::python::extract<boost::multi_array<double, 2> >(Vp);
>
> I've also tried to try to use 2D vector as below, nonetheless, it still doesn't work:
>
> boost::python::object Vp = main_namespace["Vp"];
> Vp_cpp = boost::python::extract<std::vector<std::vector<double> > >(Vp);
>
> The error message I received is "TypeError: No registered converter was able to produce a C++ rvalue of type std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > from this Python object of type matrix"
>
In general Boost.Python doesn't provide converters for all the other boost libraries.
The first question is what you want to return in Python; you could wrap multi_array itself using class_. You could also return a nested list or tuple.
If you want to use numpy, you'll probably want a helper library like this one:
https://github.com/ndarray/Boost.NumPy
In either of the last two cases, you can either write your own converter and add it to the converter registry (harder, but reusable), or write a custom wrapper for the function that returns a 2-d array (easier, but not good if you have many such functions).
HTH
Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120815/bb42c7be/attachment-0001.html>
More information about the Cplusplus-sig
mailing list