Skip to content

Adding rank member #241

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

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/xtensor-python/pyarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ namespace xt
using inner_shape_type = typename base_type::inner_shape_type;
using inner_strides_type = typename base_type::inner_strides_type;
using inner_backstrides_type = typename base_type::inner_backstrides_type;
constexpr static std::size_t rank = SIZE_MAX;

pyarray();
pyarray(const value_type& t);
Expand Down Expand Up @@ -514,7 +515,7 @@ namespace xt
{
return;
}

m_shape = inner_shape_type(reinterpret_cast<size_type*>(PyArray_SHAPE(this->python_array())),
static_cast<size_type>(PyArray_NDIM(this->python_array())));
m_strides = inner_strides_type(reinterpret_cast<difference_type*>(PyArray_STRIDES(this->python_array())),
Expand Down
3 changes: 2 additions & 1 deletion include/xtensor-python/pytensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ namespace xt
using inner_shape_type = typename base_type::inner_shape_type;
using inner_strides_type = typename base_type::inner_strides_type;
using inner_backstrides_type = typename base_type::inner_backstrides_type;
constexpr static std::size_t rank = N;

pytensor();
pytensor(nested_initializer_list_t<T, N> t);
Expand Down Expand Up @@ -471,7 +472,7 @@ namespace xt
{
return;
}

if (PyArray_NDIM(this->python_array()) != N)
{
throw std::runtime_error("NumPy: ndarray has incorrect number of dimensions");
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ set(XTENSOR_PYTHON_TESTS
test_pyarray.cpp
test_pytensor.cpp
test_pyvectorize.cpp
test_sfinae.cpp
)

add_executable(test_xtensor_python ${XTENSOR_PYTHON_TESTS} ${XTENSOR_PYTHON_HEADERS})
Expand Down
53 changes: 53 additions & 0 deletions test/test_sfinae.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/***************************************************************************
* Copyright (c) Wolf Vollprecht, Johan Mabille and Sylvain Corlay *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include <limits>

#include "gtest/gtest.h"
#include "xtensor-python/pytensor.hpp"
#include "xtensor-python/pyarray.hpp"
#include "xtensor/xarray.hpp"
#include "xtensor/xtensor.hpp"

namespace xt
{
template <class E, std::enable_if_t<!xt::has_fixed_rank_t<E>::value, int> = 0>
inline bool sfinae_has_fixed_rank(E&&)
{
return false;
}

template <class E, std::enable_if_t<xt::has_fixed_rank_t<E>::value, int> = 0>
inline bool sfinae_has_fixed_rank(E&&)
{
return true;
}

TEST(sfinae, fixed_rank)
{
xt::pyarray<size_t> a = {{9, 9, 9}, {9, 9, 9}};
xt::pytensor<size_t, 1> b = {9, 9};
xt::pytensor<size_t, 2> c = {{9, 9}, {9, 9}};

EXPECT_TRUE(sfinae_has_fixed_rank(a) == false);
EXPECT_TRUE(sfinae_has_fixed_rank(b) == true);
EXPECT_TRUE(sfinae_has_fixed_rank(c) == true);
}

TEST(sfinae, get_rank)
{
xt::pytensor<double, 1> A = xt::zeros<double>({2});
xt::pytensor<double, 2> B = xt::zeros<double>({2, 2});
xt::pyarray<double> C = xt::zeros<double>({2, 2});

EXPECT_TRUE(xt::get_rank<decltype(A)>::value == 1ul);
EXPECT_TRUE(xt::get_rank<decltype(B)>::value == 2ul);
EXPECT_TRUE(xt::get_rank<decltype(C)>::value == SIZE_MAX);
}
}