Skip to content
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

Binding array of arrays #24

Open
jpomoell opened this issue Jan 31, 2017 · 2 comments
Open

Binding array of arrays #24

jpomoell opened this issue Jan 31, 2017 · 2 comments
Labels

Comments

@jpomoell
Copy link

I'm attempting to bind a xt::pyarray<xt::pyarray<double>> to a Numpy array of arrays, but I am getting compilation errors instantiating the pyarray. The error is a bit long to include here, the relevant bit is

pybind11/include/pybind11/numpy.h:260:81: error: no member named 'dtype' in 'pybind11::detail::npy_format_descriptor<xt::pyarray<double, 16>, void>'
return detail::npy_format_descriptor<typename std::remove_cv::type>::dtype();

coming from the call to pybind11::dtype::of<T> in the return ofpyarray::raw_array_t. Unfortunately, I don't know enough about the inner workings of pybind11 to determine if the problem lies there, or whether this is a xtensor-python issue.

The problem I am trying to solve is to provide Python bindings to a code that uses xtensor xarray. Using xt::xarray<xt::xarray<double>> in a C++-only situation works as expected. Actually, my case is limited in that I always have three inner arrays, so in C++ I would prefer to use std::array<xt::xarray<double>, 3> (or perhaps xt::xtensor<xt::xarray<double>, 3>) but I do not know how to transparently (without copies) expose those as an ndarray of three ndarrays to Python.

Example code that produces the compilation error:

#include <numeric>
#include "pybind11/pybind11.h"
#include "xtensor/xarray.hpp"
#include "xtensor/xmath.hpp"
#include "xtensor-python/pyarray.hpp"

double sum_of_dim(xt::pyarray<xt::pyarray<double>> & m, const int dim)
{
    auto d = m(dim); 
    return std::accumulate(d.begin(), d.end(), 0.0);
}

PYBIND11_PLUGIN(example1_m)
{
    pybind11::module m("example1_m", "Array of array test");
    m.def("sum_of_dim", sum_of_dim, "Array of array function test");
    return m.ptr();
}
@JohanMabille
Copy link
Member

JohanMabille commented Feb 9, 2017

Sorry for the late response; at first sight, it seems the problem is on xtensor-python side.

@wolfv
Copy link
Member

wolfv commented Jun 20, 2018

I think with the recently merged support for pybind11 dtypes we're closer to solving this one. However, I think we do not yet have specializations for dtype<pyarray<T>> which seem necessary.
Needs some experiments to see if we can get this to work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants