Can I use pybind1
to pass a three-dimensional numpy array to a c++ function accepting an Eigen::Tensor
as argument. For example, consider the following c++ function:
Eigen::Tensor<double, 3> addition_tensor(Eigen::Tensor<double, 3> a,
Eigen::Tensor<double, 3> b) {
return a + b;
}
after compiling the function, importing it to python and passing a the numpy array np.ones((1, 2, 2))
to it, I receive the following error message:
TypeError: addition_tensor(): incompatible function arguments. The following argument types are supported:
1. (arg0: Eigen::Tensor<double, 3, 0, long>, arg1: Eigen::Tensor<double, 3, 0, long>) -> Eigen::Tensor<double, 3, 0, long>
I am in particular surprised about not being able to pass a three dimensional numpy array as I can pass a two dimensional numpy array
to a function accepting an Eigen::MatrixXd
, as:
Eigen::MatrixXd addition(Eigen::MatrixXd a, Eigen::MatrixXd b) { return a + b; }
The entire code I used for this example is:
#include <eigen-git-mirror/Eigen/Dense>
#include <eigen-git-mirror/unsupported/Eigen/CXX11/Tensor>
#include "pybind11/include/pybind11/eigen.h"
#include "pybind11/include/pybind11/pybind11.h"
Eigen::MatrixXd addition(Eigen::MatrixXd a, Eigen::MatrixXd b) { return a + b; }
Eigen::Tensor<double, 3> addition_tensor(Eigen::Tensor<double, 3> a,
Eigen::Tensor<double, 3> b) {
return a + b;
}
PYBIND11_MODULE(example, m) {
m.def("addition", &addition, "A function which adds two numbers");
m.def("addition_tensor", &addition_tensor,
"A function which adds two numbers");
}
I compiled the code above with g++ -shared -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`
. Does somebody have an idea how I can a three-dimensional numpy
array to a function accepting a three-dimensional Eigen::Tensor
?