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

Unexpected value of xt::get_rank<xt::xtensor_adaptor>. #2786

Open
1uc opened this issue May 16, 2024 · 1 comment
Open

Unexpected value of xt::get_rank<xt::xtensor_adaptor>. #2786

1uc opened this issue May 16, 2024 · 1 comment

Comments

@1uc
Copy link

1uc commented May 16, 2024

While using xt::get_rank to detect the rank of various XTensor objects, we noticed that the following test fails:

template <class EC, size_t N, xt::layout_type L, class Tag>
void check_adapt_tensor(xt::xtensor_adaptor<EC, N, L, Tag>& x) {
    REQUIRE(xt::get_rank<decltype(x)>::value == N);
}

TEST_CASE("xt::get_rank", "[xtensor]") {
    std::array<size_t, 3> shape{3, 2, 4};
    xt::xtensor<double, 3> a(shape);
    auto a_view = xt::adapt(a.data(), a.size(), xt::no_ownership(), shape);

    check_adapt_tensor(a_view);
}

Naively, this is unexpected, because xt::xtensor_adaptor to my understanding has a compile-time constant rank and N is one of the template arguments of the class. Which is different from xt::xarray_adaptor which doesn't have a compile-time constant rank.

@spectre-ns
Copy link
Contributor

spectre-ns commented Dec 22, 2024

@1uc Decaying away the reference allows proper SFINAE of get_rank here:

    template <class E, typename = void>
    struct get_rank
    {
        static constexpr std::size_t value = SIZE_MAX;
    };

    template <class E>
    struct get_rank<E, decltype((void) E::rank, void())>
    {
        static constexpr std::size_t value = E::rank;
    };

This passes.

    template <class EC, size_t N, xt::layout_type L, class Tag>
    void check_adapt_tensor(xt::xtensor_adaptor<EC, N, L, Tag>& x) {
        REQUIRE(xt::get_rank<std::decay_t<decltype(x)>>::value == N);
    }

    TEST(xview, test_case) {
        std::array<size_t, 3> shape{3, 2, 4};
        xt::xtensor<double, 3> a(shape);
        auto a_view = xt::adapt(a.data(), a.size(), xt::no_ownership(), shape);

        check_adapt_tensor(a_view);
    }
}

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

No branches or pull requests

2 participants