How to return Eigen MatrixXf object to Python in Ctype









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.










share|improve this question























  • 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 include numpy/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














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.










share|improve this question























  • 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 include numpy/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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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






  • 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










  • @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 include numpy/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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党