| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -154,6 +154,7 @@ namespace xt | |||
| 154 | 154 | using inner_shape_type = typename base_type::inner_shape_type; | |
| 155 | 155 | using inner_strides_type = typename base_type::inner_strides_type; | |
| 156 | 156 | using inner_backstrides_type = typename base_type::inner_backstrides_type; | |
| 157 | + constexpr static std::size_t rank = SIZE_MAX; | ||
| 157 | 158 | ||
| 158 | 159 | pyarray(); | |
| 159 | 160 | pyarray(const value_type& t); | |
@@ -514,7 +515,7 @@ namespace xt | |||
| 514 | 515 | { | |
| 515 | 516 | return; | |
| 516 | 517 | } | |
| 517 | - | ||
| 518 | + | ||
| 518 | 519 | m_shape = inner_shape_type(reinterpret_cast<size_type*>(PyArray_SHAPE(this->python_array())), | |
| 519 | 520 | static_cast<size_type>(PyArray_NDIM(this->python_array()))); | |
| 520 | 521 | m_strides = inner_strides_type(reinterpret_cast<difference_type*>(PyArray_STRIDES(this->python_array())), | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -164,6 +164,7 @@ namespace xt | |||
| 164 | 164 | using inner_shape_type = typename base_type::inner_shape_type; | |
| 165 | 165 | using inner_strides_type = typename base_type::inner_strides_type; | |
| 166 | 166 | using inner_backstrides_type = typename base_type::inner_backstrides_type; | |
| 167 | + constexpr static std::size_t rank = N; | ||
| 167 | 168 | ||
| 168 | 169 | pytensor(); | |
| 169 | 170 | pytensor(nested_initializer_list_t<T, N> t); | |
@@ -467,7 +468,7 @@ namespace xt | |||
| 467 | 468 | { | |
| 468 | 469 | return; | |
| 469 | 470 | } | |
| 470 | - | ||
| 471 | + | ||
| 471 | 472 | if (PyArray_NDIM(this->python_array()) != N) | |
| 472 | 473 | { | |
| 473 | 474 | throw std::runtime_error("NumPy: ndarray has incorrect number of dimensions"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -89,6 +89,7 @@ set(XTENSOR_PYTHON_TESTS | |||
| 89 | 89 | test_pyarray_traits.cpp | |
| 90 | 90 | test_pytensor.cpp | |
| 91 | 91 | test_pyvectorize.cpp | |
| 92 | + test_sfinae.cpp | ||
| 92 | 93 | ) | |
| 93 | 94 | ||
| 94 | 95 | add_executable(test_xtensor_python ${XTENSOR_PYTHON_TESTS} ${XTENSOR_PYTHON_HEADERS}) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,53 @@ | |||
| 1 | + /*************************************************************************** | ||
| 2 | + * Copyright (c) Wolf Vollprecht, Johan Mabille and Sylvain Corlay * | ||
| 3 | + * Copyright (c) QuantStack * | ||
| 4 | + * * | ||
| 5 | + * Distributed under the terms of the BSD 3-Clause License. * | ||
| 6 | + * * | ||
| 7 | + * The full license is in the file LICENSE, distributed with this software. * | ||
| 8 | + ****************************************************************************/ | ||
| 9 | + | ||
| 10 | + #include <limits> | ||
| 11 | + | ||
| 12 | + #include "gtest/gtest.h" | ||
| 13 | + #include "xtensor-python/pytensor.hpp" | ||
| 14 | + #include "xtensor-python/pyarray.hpp" | ||
| 15 | + #include "xtensor/xarray.hpp" | ||
| 16 | + #include "xtensor/xtensor.hpp" | ||
| 17 | + | ||
| 18 | + namespace xt | ||
| 19 | + { | ||
| 20 | + template <class E, std::enable_if_t<!xt::has_fixed_rank_t<E>::value, int> = 0> | ||
| 21 | + inline bool sfinae_has_fixed_rank(E&&) | ||
| 22 | + { | ||
| 23 | + return false; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + template <class E, std::enable_if_t<xt::has_fixed_rank_t<E>::value, int> = 0> | ||
| 27 | + inline bool sfinae_has_fixed_rank(E&&) | ||
| 28 | + { | ||
| 29 | + return true; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + TEST(sfinae, fixed_rank) | ||
| 33 | + { | ||
| 34 | + xt::pyarray<size_t> a = {{9, 9, 9}, {9, 9, 9}}; | ||
| 35 | + xt::pytensor<size_t, 1> b = {9, 9}; | ||
| 36 | + xt::pytensor<size_t, 2> c = {{9, 9}, {9, 9}}; | ||
| 37 | + | ||
| 38 | + EXPECT_TRUE(sfinae_has_fixed_rank(a) == false); | ||
| 39 | + EXPECT_TRUE(sfinae_has_fixed_rank(b) == true); | ||
| 40 | + EXPECT_TRUE(sfinae_has_fixed_rank(c) == true); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + TEST(sfinae, get_rank) | ||
| 44 | + { | ||
| 45 | + xt::pytensor<double, 1> A = xt::zeros<double>({2}); | ||
| 46 | + xt::pytensor<double, 2> B = xt::zeros<double>({2, 2}); | ||
| 47 | + xt::pyarray<double> C = xt::zeros<double>({2, 2}); | ||
| 48 | + | ||
| 49 | + EXPECT_TRUE(xt::get_rank<decltype(A)>::value == 1ul); | ||
| 50 | + EXPECT_TRUE(xt::get_rank<decltype(B)>::value == 2ul); | ||
| 51 | + EXPECT_TRUE(xt::get_rank<decltype(C)>::value == SIZE_MAX); | ||
| 52 | + } | ||
| 53 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments