From 1f95dd51db2b1cf6440c7f931f727e7a4862c02a Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Thu, 29 Oct 2026 06:44:50 +0100 Subject: [PATCH] feat(math): add size() and data() to tensor view --- include/cenet/math/tensor.hpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/include/cenet/math/tensor.hpp b/include/cenet/math/tensor.hpp index 11c458d..51b75dd 100644 --- a/include/cenet/math/tensor.hpp +++ b/include/cenet/math/tensor.hpp @@ -87,6 +87,26 @@ private: else return (calculate_offset_impl(offs) + std::get(offs)) * dimension(); } +public: + constexpr std::size_t size() const { return size_impl(std::make_index_sequence{}); } + + auto dimensions() const { return dimensions_impl(std::make_index_sequence{}); } +private: + template + constexpr std::size_t size_impl(std::index_sequence) const { + return (dimension() * ... * 1); + } + + template + constexpr auto dimensions_impl(std::index_sequence) const { + return std::make_tuple(dimension()...); + } +public: + value_type& coeff(std::size_t index) { return m_view[index]; } + const value_type& coeff(std::size_t index) const { return m_view[index]; } + + value_type* data() { return m_view; } + const value_type* data() const { return m_view; } private: template auto inner_view(value_type* view, std::index_sequence) { @@ -97,7 +117,7 @@ private: } private: template - auto dimension() const { + constexpr std::size_t dimension() const { if constexpr (traits::dynamic && traits::dimensions[I] == dynamic_extent) { return m_shape.dimensions[I]; } else { @@ -230,9 +250,6 @@ private: shape_type m_shape; }; -template -using matrix = tensor; - } // namespace math } // namespace cenet