From d23905c259f63b89b3dd9b8ab4ed2f36ad05fb00 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sun, 1 Nov 2026 20:44:25 +0100 Subject: [PATCH] feat(math): add reshape operation --- include/cenet/math/expr.hpp | 6 ++++++ include/cenet/math/ops.hpp | 38 ++++++++++++++++++++++++----------- include/cenet/math/tensor.hpp | 5 +++-- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/include/cenet/math/expr.hpp b/include/cenet/math/expr.hpp index 4697761..06591a1 100644 --- a/include/cenet/math/expr.hpp +++ b/include/cenet/math/expr.hpp @@ -44,6 +44,12 @@ struct ops_provider { if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch"); return { self() }; } + + template + unary_expression> reshape() { + if (!ops::reshape::compatible(self())) throw std::runtime_error("tensor shape mismatch"); + return { self() }; + } private: decltype(auto) self() & { return reinterpret_cast(*this); } decltype(auto) self() && { return reinterpret_cast(*this); } diff --git a/include/cenet/math/ops.hpp b/include/cenet/math/ops.hpp index a61e9a0..907acbb 100644 --- a/include/cenet/math/ops.hpp +++ b/include/cenet/math/ops.hpp @@ -146,18 +146,7 @@ struct apply { template static constexpr auto shape(const L& lhs, const R& rhs) { if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands"); - - using lhs_shape = remove_cvref_t; - using rhs_shape = remove_cvref_t; - - constexpr std::size_t lhs_rows = lhs_shape::template sget<0>(); - constexpr std::size_t rhs_cols = rhs_shape::template sget<1>(); - - if constexpr (lhs_rows == dynamic_extent || rhs_cols == dynamic_extent) { - return dynamic_shape{}; - } else { - return static_shape{}; - } + return lhs.shape(); } template @@ -201,6 +190,31 @@ struct transpose { } }; +// TODO: introduce dynamic reshape +template >> +struct reshape { + template + static constexpr bool compatible(const E& expr) { + using shape_type = remove_cvref_t; + if constexpr (!::cenet::math::detail::is_static_shape::value) { + return false; + } else { + return shape_type::size == S::size; + } + } + + template + static constexpr auto shape(const E& expr) { + if (!compatible(expr)) throw std::runtime_error("uncompatible operands"); + return S{}; + } + + template + constexpr auto operator()(std::size_t index, const E& expr) const { + return expr.coeff(index); + } +}; + } // namespace ops } // namespace math } // namespace cenet diff --git a/include/cenet/math/tensor.hpp b/include/cenet/math/tensor.hpp index ce5d703..0f40b52 100644 --- a/include/cenet/math/tensor.hpp +++ b/include/cenet/math/tensor.hpp @@ -34,9 +34,10 @@ public: m_items = nullptr; m_shape = {}; } else if constexpr (traits::inline_storage) { - std::memset(m_items, 0, sizeof(m_items)); + view().fill(); } else { m_items = new value_type[traits::size]; + view().fill(); } } @@ -261,7 +262,7 @@ private: private: template void dyn_alloc(std::index_sequence) { - if constexpr (traits::dynamic) { + if constexpr (!traits::inline_storage) { m_items = new value_type[size()]; } }