From 3fca91e1eac1465c3236413b88cbd936dc2a53d7 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sun, 1 Nov 2026 05:59:58 +0100 Subject: [PATCH] refactor(math): generalize tensor operations --- include/cenet/math/expr.hpp | 42 ++++------- include/cenet/math/ops.hpp | 109 +++------------------------- include/cenet/math/tensor.hpp | 11 +-- include/cenet/math/tensor_shape.hpp | 32 +------- 4 files changed, 29 insertions(+), 165 deletions(-) diff --git a/include/cenet/math/expr.hpp b/include/cenet/math/expr.hpp index b1a2798..f84b334 100644 --- a/include/cenet/math/expr.hpp +++ b/include/cenet/math/expr.hpp @@ -4,6 +4,7 @@ #include "ops.hpp" #include "tensor_traits.hpp" +#include #include #include @@ -71,45 +72,30 @@ using operand_t = typename operand::type; template static constexpr bool is_expression_v = std::is_base_of_v>; -template -struct expression_result { - using type = remove_cvref_t; -}; - -template -struct expression_result> { - using type = typename Op::template result_type>::type>>; -}; - -template -struct expression_result> { - using type = typename Op::template result_type>::type>, - operand_t>::type>>; -}; - } // namespace detail -template >::type>> +template binary_expression operator+(L&& lhs, R&& rhs) { + // TODO: introduce a better exception class + if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch"); return { std::forward(lhs), std::forward(rhs) }; } -template >::type>> +template binary_expression operator-(L&& lhs, R&& rhs) { + if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch"); return { std::forward(lhs), std::forward(rhs) }; } -template >::type>> +template binary_expression operator*(L&& lhs, R&& rhs) { + if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch"); + return { std::forward(lhs), std::forward(rhs) }; +} + +template +binary_expression operator/(L&& lhs, R&& rhs) { + if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch"); return { std::forward(lhs), std::forward(rhs) }; } diff --git a/include/cenet/math/ops.hpp b/include/cenet/math/ops.hpp index ba393ce..4593c44 100644 --- a/include/cenet/math/ops.hpp +++ b/include/cenet/math/ops.hpp @@ -2,42 +2,26 @@ #define CENET_MATH_OPS_HPP #include "tensor_shape.hpp" -#include "tensor_traits.hpp" #include +#include #include -#include namespace cenet { namespace math { namespace ops { -class plus { -private: +template +struct generic_binary_op { template - struct result_type_impl {}; - - template - struct result_type_impl, tensor_view> { - using lhs_view = tensor_view; - using rhs_view = tensor_view; - - using type = std::enable_if_t< - detail::is_shape_equivalent::value, - tensor>() + std::declval>()), LDims...>>; - }; -public: - template - using result_type = typename result_type_impl::type; - - template && is_tensor_v)>> static bool compatible(const L& lhs, const R& rhs) { return lhs.shape() == rhs.shape(); } template static constexpr auto shape(const L& lhs, const R& rhs) { - if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape"); + if (lhs.shape() != rhs.shape()) throw std::runtime_error("tensor shape mismatch"); + // TODO: introduce "best shape" function/trait if constexpr (detail::is_static_shape_v) { return lhs.shape(); } else { @@ -47,87 +31,14 @@ public: template constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const { - return lhs + rhs.coeff(index); + return Op{}(lhs, rhs.coeff(index)); } }; -class minus { -private: - template - struct result_type_impl {}; - - template - struct result_type_impl, tensor_view> { - using lhs_view = tensor_view; - using rhs_view = tensor_view; - - using type = std::enable_if_t< - detail::is_shape_equivalent::value, - tensor>() - std::declval>()), LDims...>>; - }; -public: - template - using result_type = typename result_type_impl::type; - - template && is_tensor_v)>> - static bool compatible(const L& lhs, const R& rhs) { - return lhs.shape() == rhs.shape(); - } - - template - static constexpr auto shape(const L& lhs, const R& rhs) { - if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape"); - if constexpr (detail::is_static_shape_v) { - return lhs.shape(); - } else { - return rhs.shape(); - } - } - - template - constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const { - return lhs - rhs.coeff(index); - } -}; - -class multiply { -private: - template - struct result_type_impl {}; - - template - struct result_type_impl, tensor_view> { - using lhs_view = tensor_view; - using rhs_view = tensor_view; - - using type = std::enable_if_t< - detail::is_shape_equivalent::value, - tensor>() * std::declval>()), LDims...>>; - }; -public: - template - using result_type = typename result_type_impl::type; - - template && is_tensor_v)>> - static bool compatible(const L& lhs, const R& rhs) { - return lhs.shape() == rhs.shape(); - } - - template - static constexpr auto shape(const L& lhs, const R& rhs) { - if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape"); - if constexpr (detail::is_static_shape_v) { - return lhs.shape(); - } else { - return rhs.shape(); - } - } - - template - constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const { - return lhs * rhs.coeff(index); - } -}; +struct plus : generic_binary_op> {}; +struct minus : generic_binary_op> {}; +struct multiply : generic_binary_op> {}; +struct divide : generic_binary_op> {}; } // namespace ops } // namespace math diff --git a/include/cenet/math/tensor.hpp b/include/cenet/math/tensor.hpp index d4e061e..98f5fb0 100644 --- a/include/cenet/math/tensor.hpp +++ b/include/cenet/math/tensor.hpp @@ -86,11 +86,7 @@ public: public: template >> tensor(E&& exprFwd) { // NOLINT - // If static shape check failed, check dynamically (requried for static = dynamic op dynamic). - if constexpr (!shape_type::template compatible::type>()) { - // TODO: introduce better exceptions - if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch"); - } + if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch"); if constexpr (!traits::inline_storage) { if constexpr (traits::dynamic) { @@ -108,10 +104,7 @@ public: template >> tensor& operator=(E&& exprFwd) { - // If static shape check failed, check dynamically (requried for static = dynamic op dynamic). - if constexpr (!shape_type::template compatible::type>()) { - if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch"); - } + if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch"); if constexpr (!traits::inline_storage) { if constexpr (traits::dynamic) { diff --git a/include/cenet/math/tensor_shape.hpp b/include/cenet/math/tensor_shape.hpp index 105404f..1e65b80 100644 --- a/include/cenet/math/tensor_shape.hpp +++ b/include/cenet/math/tensor_shape.hpp @@ -36,16 +36,6 @@ struct static_shape : shape { return compatible_impl::value; } - template - bool operator==(const static_shape& rhs) const { - return sizeof...(Dims) == sizeof...(RDims) && ((Dims == RDims) && ...); - } - - template - bool operator!=(const static_shape& rhs) const { - return sizeof...(Dims) != sizeof...(RDims) || ((Dims != RDims) || ...); - } - template constexpr auto get() const { return get_impl::value; @@ -89,18 +79,6 @@ struct dynamic_shape : shape { return compatible_impl::value; } - template - bool operator==(const static_shape& rhs) const { - return sizeof...(Dims) == sizeof...(RDims) && - compare_impl(std::make_index_sequence{}, rhs); - } - - template - bool operator==(const dynamic_shape& rhs) const { - return sizeof...(Dims) == sizeof...(RDims) && - compare_impl(std::make_index_sequence{}, rhs); - } - template constexpr auto get() const { return get_impl::value == dynamic_extent ? dimensions[I] : get_impl::value; @@ -115,13 +93,9 @@ private: struct compatible_impl> : std::conditional_t<((Dims == dynamic_extent || Dims == OthDims) && ...), std::true_type, std::false_type> {}; - template