I want to send a numpy array to a Armadillo (C++) and output a numpy array from the C++ program. I didn't find any tutorials online for this. Can someone give me pointers on how to do this ?
Asked
Active
Viewed 1,862 times
4
-
I'm not including code because, it is not relevant to the question and I didn't find any tutorials to try. – Jan 04 '19 at 15:56
-
read up on ctypes, im guessing that's what you're after. – Paritosh Singh Jan 04 '19 at 15:57
-
1It is very relevant... What wrapper tool are you using for instance? – Matthieu Brucher Jan 04 '19 at 15:58
-
No I have code written python (numpy) and code that does stuff using armadillo in C++. I want to call the C++ function in python. But I don't know how to link them. – Jan 04 '19 at 15:59
-
1What wrapper tool are you using? – Matthieu Brucher Jan 04 '19 at 16:00
-
Boost.python seems to be a good tool for this. – Jan 04 '19 at 16:03
-
OK, so you haven't decided... I would advise against it and suggest pybind11 and then do a similar wrapping as what I do at https://github.com/mbrucher/AudioTK/blob/master/Python/ATK/Adaptive/Adaptive.cpp#L49 with Eigen. Get the sizes, a pointer to the data and wrap in a lambda. – Matthieu Brucher Jan 04 '19 at 16:07
-
pybind11 seems to support only Eigen. I want to use armadillo, its rather long code for me to translate it into Eigen. – Jan 04 '19 at 16:52
-
I too use `pybind11` and `Eigen`, and it works as the pybind11 docs show. If you want to just get something up and running, I'd suggest trying to marshal your Armadillo matrices to Eigen, and then use that to interface. If your Armadillo matrices have fixed strides and dense layout, take a look at `Eigen::Map<>` on how to wrap non-Eigen data without copies. Pybind11 docs: https://pybind11.readthedocs.io/en/stable/advanced/cast/eigen.html#pass-by-reference – eacousineau Jan 05 '19 at 20:05
-
@eacousineau thanks for the info I will try this approach. Can you take a look at this ? https://stackoverflow.com/questions/54055530/error-no-matching-function-for-call-to-pybind11buffer-infobuffer-info – Jan 05 '19 at 20:09
-
FYI This also seems relevant, but for raw C extensions: https://stackoverflow.com/questions/35000565/accessing-view-of-a-numpy-array-using-the-c-api - it may also have refs to other approaches with more high-level wrappers. – eacousineau Jan 05 '19 at 20:10
2 Answers
2
You can rely on cython and the numpy c interface for the data conversion. There are different projects that implement this including armanpy, a library for conversion between numpy and armadillo, or mlpack, a machine learning library that uses armadillo as its data and linear algebra backend. The easiest method that I found was to use the cyarma python library, which comes with simple examples of how to access the c++ armadillo functionality from within cython.
If you want to use purely C++ (without the cython) you could implement a conversion using the boost libraries.

ben26941
- 1,580
- 14
- 20
-2
first convert numpy array to c++ array and enter to armadillo and from outpu
-
1As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 27 '22 at 08:55