1

I want to implement np.tile(a[:,0], (len(b), 1)) in cpp xtensor and I got to the following line, with a xarray<float>(N,21) and b xarray<float>(M,21).

xt::xarray<float> res = xt::tile(
    xt::reshape_view(
        xt::view(a, xt::all(), 0), 
        {1, static_cast<int>(a.shape(0))}
    ), 
    {static_cast<int>(b.shape(0)),1}
);

However whenever I get to M>1 my process dies with std::bad_array_new_length. I verified the shapes of a and b and they are correct. Any idea what might be the problem?

As I didn't understand the problem I tried to extract it in the following helper file:

#include <xtensor/xarray.hpp>
#include <xtensor/xpad.hpp>
#include <xtensor/xio.hpp>
#include <xtensor/xrandom.hpp>
#include <xtensor/xview.hpp>

int main(){
    xt::xarray<float> a = xt::random::rand<float>({2,21});
    xt::xarray<float> b = xt::random::rand<float>({2,21});
    xt::xarray<float> res = xt::tile(
        xt::reshape_view(
            xt::view(a, xt::all(), 0), 
            {1, static_cast<int>(a.shape(0))}
        ), 
        {static_cast<int>(b.shape(0)),1}
    );
    std::cout << res << std::endl;
    return 0;
}

which gave me the expected output:

{{ 0.814724,  0.725839},
 { 0.814724,  0.725839}}

So I'm kinda lost now on how to fix this. Would appreciate some hints!

2
  • 1
    Can you create an minimal reproducible example of the failing code? Commented Oct 31, 2024 at 12:00
  • Hard as I'm depending a lot on other classes and external Inputs, but I'll try Commented Nov 1, 2024 at 13:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.