| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -158,6 +158,7 @@ namespace xt | |||
| 158 | 158 | using inner_shape_type = typename base_type::inner_shape_type; | |
| 159 | 159 | using inner_strides_type = typename base_type::inner_strides_type; | |
| 160 | 160 | using inner_backstrides_type = typename base_type::inner_backstrides_type; | |
| 161 | + constexpr static std::size_t rank = SIZE_MAX; | ||
| 161 | 162 | ||
| 162 | 163 | pyarray(); | |
| 163 | 164 | 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 | |
|---|---|---|---|
@@ -168,6 +168,7 @@ namespace xt | |||
| 168 | 168 | using inner_shape_type = typename base_type::inner_shape_type; | |
| 169 | 169 | using inner_strides_type = typename base_type::inner_strides_type; | |
| 170 | 170 | using inner_backstrides_type = typename base_type::inner_backstrides_type; | |
| 171 | + constexpr static std::size_t rank = N; | ||
| 171 | 172 | ||
| 172 | 173 | pytensor(); | |
| 173 | 174 | pytensor(nested_initializer_list_t<T, N> t); | |
@@ -471,7 +472,7 @@ namespace xt | |||
| 471 | 472 | { | |
| 472 | 473 | return; | |
| 473 | 474 | } | |
| 474 | - | ||
| 475 | + | ||
| 475 | 476 | if (PyArray_NDIM(this->python_array()) != N) | |
| 476 | 477 | { | |
| 477 | 478 | throw std::runtime_error("NumPy: ndarray has incorrect number of dimensions"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -86,6 +86,7 @@ set(XTENSOR_PYTHON_TESTS | |||
| 86 | 86 | test_pyarray.cpp | |
| 87 | 87 | test_pytensor.cpp | |
| 88 | 88 | test_pyvectorize.cpp | |
| 89 | + test_sfinae.cpp | ||
| 89 | 90 | ) | |
| 90 | 91 | ||
| 91 | 92 | 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