How to return Eigen MatrixXf object to Python in Ctype

Multi tool use
up vote
-1
down vote
favorite
I am trying to call c++ function from python using ctype. I can successfully call it from python now. However, the issue comes from returning Eigen Matrix. For example, I have matrix like :
Eigen::MatrixXf M
I want to
return M
I tried to use PyObject to directly cast it like (PyObject*)M
, which does not work.
Essentially, I want to return a matrix or numpy array in Python. I am wondering how I can do it with PyObject.
python c++ ctypes eigen
|
show 6 more comments
up vote
-1
down vote
favorite
I am trying to call c++ function from python using ctype. I can successfully call it from python now. However, the issue comes from returning Eigen Matrix. For example, I have matrix like :
Eigen::MatrixXf M
I want to
return M
I tried to use PyObject to directly cast it like (PyObject*)M
, which does not work.
Essentially, I want to return a matrix or numpy array in Python. I am wondering how I can do it with PyObject.
python c++ ctypes eigen
PyObject is generic. Please edit your question and add the code that tried to convert it into into one rather than something specific, like a list.
– martineau
Nov 11 at 1:42
@martineauI have edited the question to be more specific.
– Wei
Nov 11 at 1:48
Casting some third-party C++ type into a PyObject is not the same thing as converting it into some data structure that the Python interpreter will understand—so I'm not surprised it doesn't work. If you're usingnumpy
, you could convert it to one of the Python-compatible types defined within that package. You could also define your own (Python-compatible) type and convert it to that.
– martineau
Nov 11 at 1:57
@martineauThanks for the reply. Yes, I am usingnumpy
here, which is the ideal return type for me. But how I should do to do that conversion? Do I need to includenumpy/arrayObject.h
something like that?
– Wei
Nov 11 at 2:01
1
A little searching immediately turned up several useful things: NumPy C-API, Interfacing with C, and Speeding up Python and NumPy: C++ing the Way for example.
– martineau
Nov 11 at 18:53
|
show 6 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am trying to call c++ function from python using ctype. I can successfully call it from python now. However, the issue comes from returning Eigen Matrix. For example, I have matrix like :
Eigen::MatrixXf M
I want to
return M
I tried to use PyObject to directly cast it like (PyObject*)M
, which does not work.
Essentially, I want to return a matrix or numpy array in Python. I am wondering how I can do it with PyObject.
python c++ ctypes eigen
I am trying to call c++ function from python using ctype. I can successfully call it from python now. However, the issue comes from returning Eigen Matrix. For example, I have matrix like :
Eigen::MatrixXf M
I want to
return M
I tried to use PyObject to directly cast it like (PyObject*)M
, which does not work.
Essentially, I want to return a matrix or numpy array in Python. I am wondering how I can do it with PyObject.
python c++ ctypes eigen
python c++ ctypes eigen
edited Nov 11 at 1:48
asked Nov 11 at 1:28
Wei
5619
5619
PyObject is generic. Please edit your question and add the code that tried to convert it into into one rather than something specific, like a list.
– martineau
Nov 11 at 1:42
@martineauI have edited the question to be more specific.
– Wei
Nov 11 at 1:48
Casting some third-party C++ type into a PyObject is not the same thing as converting it into some data structure that the Python interpreter will understand—so I'm not surprised it doesn't work. If you're usingnumpy
, you could convert it to one of the Python-compatible types defined within that package. You could also define your own (Python-compatible) type and convert it to that.
– martineau
Nov 11 at 1:57
@martineauThanks for the reply. Yes, I am usingnumpy
here, which is the ideal return type for me. But how I should do to do that conversion? Do I need to includenumpy/arrayObject.h
something like that?
– Wei
Nov 11 at 2:01
1
A little searching immediately turned up several useful things: NumPy C-API, Interfacing with C, and Speeding up Python and NumPy: C++ing the Way for example.
– martineau
Nov 11 at 18:53
|
show 6 more comments
PyObject is generic. Please edit your question and add the code that tried to convert it into into one rather than something specific, like a list.
– martineau
Nov 11 at 1:42
@martineauI have edited the question to be more specific.
– Wei
Nov 11 at 1:48
Casting some third-party C++ type into a PyObject is not the same thing as converting it into some data structure that the Python interpreter will understand—so I'm not surprised it doesn't work. If you're usingnumpy
, you could convert it to one of the Python-compatible types defined within that package. You could also define your own (Python-compatible) type and convert it to that.
– martineau
Nov 11 at 1:57
@martineauThanks for the reply. Yes, I am usingnumpy
here, which is the ideal return type for me. But how I should do to do that conversion? Do I need to includenumpy/arrayObject.h
something like that?
– Wei
Nov 11 at 2:01
1
A little searching immediately turned up several useful things: NumPy C-API, Interfacing with C, and Speeding up Python and NumPy: C++ing the Way for example.
– martineau
Nov 11 at 18:53
PyObject is generic. Please edit your question and add the code that tried to convert it into into one rather than something specific, like a list.
– martineau
Nov 11 at 1:42
PyObject is generic. Please edit your question and add the code that tried to convert it into into one rather than something specific, like a list.
– martineau
Nov 11 at 1:42
@martineauI have edited the question to be more specific.
– Wei
Nov 11 at 1:48
@martineauI have edited the question to be more specific.
– Wei
Nov 11 at 1:48
Casting some third-party C++ type into a PyObject is not the same thing as converting it into some data structure that the Python interpreter will understand—so I'm not surprised it doesn't work. If you're using
numpy
, you could convert it to one of the Python-compatible types defined within that package. You could also define your own (Python-compatible) type and convert it to that.– martineau
Nov 11 at 1:57
Casting some third-party C++ type into a PyObject is not the same thing as converting it into some data structure that the Python interpreter will understand—so I'm not surprised it doesn't work. If you're using
numpy
, you could convert it to one of the Python-compatible types defined within that package. You could also define your own (Python-compatible) type and convert it to that.– martineau
Nov 11 at 1:57
@martineauThanks for the reply. Yes, I am using
numpy
here, which is the ideal return type for me. But how I should do to do that conversion? Do I need to include numpy/arrayObject.h
something like that?– Wei
Nov 11 at 2:01
@martineauThanks for the reply. Yes, I am using
numpy
here, which is the ideal return type for me. But how I should do to do that conversion? Do I need to include numpy/arrayObject.h
something like that?– Wei
Nov 11 at 2:01
1
1
A little searching immediately turned up several useful things: NumPy C-API, Interfacing with C, and Speeding up Python and NumPy: C++ing the Way for example.
– martineau
Nov 11 at 18:53
A little searching immediately turned up several useful things: NumPy C-API, Interfacing with C, and Speeding up Python and NumPy: C++ing the Way for example.
– martineau
Nov 11 at 18:53
|
show 6 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245067%2fhow-to-return-eigen-matrixxf-object-to-python-in-ctype%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
lllx,tY7yqv3O 0o5t 0 XowHtm4Fafw,ebcn2X,6G m G3ONjCDv,h5R0IcMagqUNxHcm,UNYZ,c8 VW8x Cdq
PyObject is generic. Please edit your question and add the code that tried to convert it into into one rather than something specific, like a list.
– martineau
Nov 11 at 1:42
@martineauI have edited the question to be more specific.
– Wei
Nov 11 at 1:48
Casting some third-party C++ type into a PyObject is not the same thing as converting it into some data structure that the Python interpreter will understand—so I'm not surprised it doesn't work. If you're using
numpy
, you could convert it to one of the Python-compatible types defined within that package. You could also define your own (Python-compatible) type and convert it to that.– martineau
Nov 11 at 1:57
@martineauThanks for the reply. Yes, I am using
numpy
here, which is the ideal return type for me. But how I should do to do that conversion? Do I need to includenumpy/arrayObject.h
something like that?– Wei
Nov 11 at 2:01
1
A little searching immediately turned up several useful things: NumPy C-API, Interfacing with C, and Speeding up Python and NumPy: C++ing the Way for example.
– martineau
Nov 11 at 18:53