how to write c++ functions that accept numpy arrays with custom numeric types exposed via EigenPy #380
-
I'm looking for a bit of help. I have C++ functions that accept Eigen vectors and matrices. And those functions are exposed to Python via Boost.Python, as are my numeric types, and I have exposed Eigen containers of my numeric types via EigenPy. So I think this is an eigenpy question. But I'm struggling to provide inter-operability between the Eigen vectors for C++ calls and numpy containers with dtype my custom numeric type. Do you have examples in eigenpy or pinnochio of a C++ function accepting Eigen data, but which when exposed to Python accepts numpy? super cut-down example code
where But the call in Python fails because the C++ function expects an Eigen container, but it's getting a numpy container. My specific error message:
|
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 2 replies
-
this SO post suggests copying in the data for the python --> c++ direction, but i dislike this. there should be a way to do it directly, to provide an overload / interoperability layer. |
Beta Was this translation helpful? Give feedback.
-
related open question, i think: https://stackoverflow.com/questions/76065711/converting-pyarray-to-eigenref-from-c |
Beta Was this translation helpful? Give feedback.
-
one more closely-related SO question, this time using eigency (but sadly not eigenpy) |
Beta Was this translation helpful? Give feedback.
-
Please look at this test eigenpy/unittest/user_type.cpp Lines 134 to 138 in e945a06 |
Beta Was this translation helpful? Give feedback.
-
Ooh, discussions! Thanks for moving this thread. I'll take a look at the example you indicated, thank you very much. |
Beta Was this translation helpful? Give feedback.
-
I don't think the selected answer solves the issue. It did lead me to add the following line of code, which helped some of the problems:
BUT, I am still unable to pass by non-const reference. |
Beta Was this translation helpful? Give feedback.
-
I need (would strongly like) to be able to pass by non-const reference, in such a function
I have edit: These functions worked in my Python side when I was using Now with eigenpy I no longer have the eigen types directly in Python, and this causes all of my pass-by-mutable-reference functions to break. and I pass by mutable reference a LOT in my library. so getting it back without having to re-wrap all these functions would be awesome. |
Beta Was this translation helpful? Give feedback.
-
I am starting to suspect that I should be using Is that what this line of code is about? |
Beta Was this translation helpful? Give feedback.
-
I played with it in the meantime. Switching my codebase from
to
is causing all sorts of headache for me... Ugh. |
Beta Was this translation helpful? Give feedback.
-
I was able to come up with a workaround. It's not optimal. But it works. I wrap the functions i want to expose to Python that pass by non-const-reference from the C++ side in the code that produces my bindings, like this:
It lets me call my core code without changing to The cost:
So definitely not good. But it will at least keep me from crying for now. Bottom line: use |
Beta Was this translation helpful? Give feedback.
Please look at this test
eigenpy/unittest/user_type.cpp
Lines 134 to 138 in e945a06