Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to write c++ functions that accept numpy arrays with custom numeric types exposed via EigenPy #378

Closed
ofloveandhate opened this issue Jul 6, 2023 · 4 comments

Comments

@ofloveandhate
Copy link

ofloveandhate commented Jul 6, 2023

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

// in C++
def foo(Eigen::Vector v){
}

foo(Eigen::Vector(blabla))
# in Python

foo(np.array( [N(1), N(2), N(3) ])

where N is my wrapped custom numeric type.

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:

Traceback (most recent call last):
  File "/Users/amethyst/github_repos/b2/python/test/tracking/amptracking_test.py", line 133, in test_tracker_quad
    tracker.track_path(y_end, t_start, t_end, y_start);
Boost.Python.ArgumentError: Python argument types in
    AMPTracker.track_path(AMPTracker, numpy.ndarray, Complex, Complex, numpy.ndarray)
did not match C++ signature:
    track_path(bertini::tracking::AMPTracker {lvalue}, Eigen::Matrix<boost::multiprecision::number<boost::multiprecision::backends::mpc_complex_backend<0u>, (boost::multiprecision::expression_template_option)1>, -1, 1, 0, -1, 1> {lvalue} result, boost::multiprecision::number<boost::multiprecision::backends::mpc_complex_backend<0u>, (boost::multiprecision::expression_template_option)1> start_time, boost::multiprecision::number<boost::multiprecision::backends::mpc_complex_backend<0u>, (boost::multiprecision::expression_template_option)1> end_time, Eigen::Matrix<boost::multiprecision::number<boost::multiprecision::backends::mpc_complex_backend<0u>, (boost::multiprecision::expression_template_option)1>, -1, 1, 0, -1, 1> start_point)
@ofloveandhate ofloveandhate changed the title how to write c++ functions that accept numpy arrays how to write c++ functions that accept numpy arrays with custom numeric types exposed via EigenPy Jul 6, 2023
ofloveandhate added a commit to ofloveandhate/b2 that referenced this issue Jul 6, 2023
because i removed minieigen.  but now function signatures don't match.  i asked for help from the eigenpy people at stack-of-tasks/eigenpy#378, since they've likely solved this problem and there's a model of it in their codebase somewhere
@ofloveandhate
Copy link
Author

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.

@ofloveandhate
Copy link
Author

@ofloveandhate
Copy link
Author

one more closely-related SO question, this time using eigency (but sadly not eigenpy)

@jcarpent
Copy link
Contributor

jcarpent commented Jul 7, 2023

Please look at this test

template <typename Scalar>
void print(const Eigen::Matrix<CustomType<Scalar>, Eigen::Dynamic,
Eigen::Dynamic>& mat) {
std::cout << mat << std::endl;
}

@stack-of-tasks stack-of-tasks locked and limited conversation to collaborators Jul 7, 2023
@jcarpent jcarpent converted this issue into discussion #380 Jul 7, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants