Re: [C++-sig] Accessing 2D array from python in C++
Hi Jim, Do you mean, "you could wrap multi_array itself using class_", I should do as below: boost::python::class_ > >("PyVec").def(boost::python::vector_indexing_suite > >()); 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 To: Development of Python/C++ integration ; shahmi 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" 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 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 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 > > >(Vp); > > The error message I received is "TypeError: No registered converter was able > to produce a C++ rvalue of type std::vector std::allocator >, std::allocator std::allocator > > > 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___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Accessing 2D array from python in C++
I apologize for double posting this because it was spammed before. Hi Jim, Do you mean, "you could wrap multi_array itself using class_", I should do as below: boost::python::class_ > >("PyVec").def(boost::python::vector_indexing_suite > > >()); 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 To: Development of Python/C++ integration ; shahmi 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" 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 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 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 > > >(Vp); > > The error message I received is "TypeError: No registered converter was able > to produce a C++ rvalue of type std::vector std::allocator >, std::allocator std::allocator > > > 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___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
[C++-sig] Fw: Accessing 2D array from python in C++
- Forwarded Message - From: alfa To: Jim Bosch ; Development of Python/C++ integration Sent: Wednesday, August 15, 2012 10:57 AM Subject: Re: [C++-sig] Accessing 2D array from python in C++ I apologize for double posting this because it was spammed before. Hi Jim, Do you mean, "you could wrap multi_array itself using class_", I should do as below: boost::python::class_ > >("PyVec").def(boost::python::vector_indexing_suite > > >()); 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 To: Development of Python/C++ integration ; shahmi 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" 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 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 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 > > >(Vp); > > The error message I received is "TypeError: No registered converter was able > to produce a C++ rvalue of type std::vector std::allocator >, std::allocator std::allocator > > > 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 ___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
[C++-sig] Problem exposing 2D python array in C++
Hi, Is the following way valid to expose a 2D array variable from python (Vp) to c++(Vp_cpp)? std::vector > Vp_cpp; boost::python::class_ > >("PyVec").def(boost::python::vector_indexing_suite > >()); main_namespace["Vp"] = boost::python::ptr(&Vp_cpp); But, then if I tried to manipulate the 2D vector in C++ like the following, it doesn't work: Vp_cpp[1][1] = 999; std::cout << "Vp_cpp[1][1] = " << Vp_cpp[1][1] << std::endl; // It doesn't print to What could be wrong or am I missing something? Thanks in advance.___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
[C++-sig] Exposing 2D python array in C++
Hi, Is the following way valid to expose a 2D array variable from python (Vp) to c++(Vp_cpp)? std::vector > Vp_cpp; boost::python::class_ > >("PyVec").def(boost::python::vector_indexing_suite > > >()); main_namespace["Vp"] = boost::python::ptr(&Vp_cpp); But, then if I tried to manipulate the 2D vector in C++ like the following, it doesn't work: Vp_cpp[1][1] = 999; std::cout << "Vp_cpp[1][1] = " << Vp_cpp[1][1] << std::endl; // It doesn't print to What could be wrong or am I missing something? Thanks in advance.___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Accessing 2D array from python in C++
On Aug 15, 2012 1:57 AM, "alfa" wrote: > > I apologize for double posting this because it was spammed before. > > Hi Jim, > > Do you mean, "you could wrap multi_array itself using class_", I should do as below: > > boost::python::class_ > >("PyVec").def(boost::python::vector_indexing_suite > >()); > No, I don't think that will work; I meant something like this: boost::python::class_< boost::multi_array<...> >(...) ...; Where the last "..." is where you have to manually wrap the features of multi_array you want to use. I'm afraid there's nothing as easy as vector_indexing_suite. Jim ___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig