From 22bf8db5e8243defdfc494dec6982d22747f152d Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Thu, 29 Oct 2026 10:55:29 +0100 Subject: [PATCH] refactor(math): improve tensor shapes --- include/cenet/math/tensor.hpp | 65 ++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/include/cenet/math/tensor.hpp b/include/cenet/math/tensor.hpp index 204a579..94772b8 100644 --- a/include/cenet/math/tensor.hpp +++ b/include/cenet/math/tensor.hpp @@ -16,21 +16,61 @@ namespace math { namespace detail { -struct empty_shape {}; +template +struct static_shape { + static_shape() = default; -template + template = sizeof...(Dims))>> + explicit static_shape(const static_shape& oth) {} + + template + static constexpr bool compatible() { + return ((Dims == OthDims) && ...); + } + + template + static constexpr bool compatible() { + return compatible_impl::value; + } +private: + template + struct compatible_impl : std::false_type {}; + + template + struct compatible_impl> + : std::conditional_t<((Dims == OthDims) && ...), std::true_type, std::false_type> {}; +}; + +template struct dynamic_shape { dynamic_shape() = default; - template = Rank)>> + template = sizeof...(Dims))>> explicit dynamic_shape(const dynamic_shape& oth) { - constexpr std::size_t ext_offset = ExtRank - Rank; - for (std::size_t i = 0; i < Rank; ++i) { + constexpr std::size_t ext_offset = ExtRank - sizeof...(Dims); + for (std::size_t i = 0; i < sizeof...(Dims); ++i) { dimensions[i] = oth.dimensions[ext_offset + i]; } } - std::size_t dimensions[Rank]{}; + template + static constexpr bool compatible() { + return ((Dims == dynamic_extent || Dims == OthDims) && ...); + } + + template + static constexpr bool compatible() { + return compatible_impl::value; + } + + std::size_t dimensions[sizeof...(Dims)]{}; +private: + template + struct compatible_impl : std::false_type {}; + + template + struct compatible_impl> + : std::conditional_t<((Dims == dynamic_extent || Dims == OthDims) && ...), std::true_type, std::false_type> {}; }; } // namespace detail @@ -126,7 +166,8 @@ private: } } private: - using shape_type = std::conditional_t, detail::empty_shape>; + using shape_type = + std::conditional_t, detail::static_shape>; template tensor_view(value_type* view, Fwd&& shape) @@ -145,6 +186,9 @@ public: using constant_value_type = const value_type; using view_type = tensor_view; using constant_view_type = tensor_view; +private: + using shape_type = + std::conditional_t, detail::static_shape>; public: tensor() { if constexpr (traits::dynamic) { @@ -161,7 +205,7 @@ public: typename = std::enable_if_t<( traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v && ...))>> explicit tensor(RtDims... dimensions) - : m_shape(detail::dynamic_shape{}) { + : m_shape(detail::dynamic_shape{}) { set_dimensions(dimensions...); } @@ -198,10 +242,9 @@ public: return *this; } public: - // TODO: Introduce another type trait for tensor compability template && !is_tensor_v && - std::is_same_v::type>>> + shape_type::template compatible::type>()>> tensor(E&& expr) { // NOLINT if constexpr (!traits::inline_storage) { static_assert(!traits::dynamic, "Dynamic tensors are not supported yet!"); @@ -301,8 +344,6 @@ private: m_items = nullptr; } } -private: - using shape_type = std::conditional_t, detail::empty_shape>; private: typename traits::storage_type m_items; shape_type m_shape;