diff --git a/include/cenet/math/expr.hpp b/include/cenet/math/expr.hpp index fdf7dbb..4ccfbf0 100644 --- a/include/cenet/math/expr.hpp +++ b/include/cenet/math/expr.hpp @@ -12,6 +12,7 @@ namespace cenet { namespace math { struct expression_tag {}; +struct expression_node_tag : expression_tag {}; template struct unary_expression; @@ -23,31 +24,53 @@ template struct ops_provider { template binary_expression matmul(R&& rhs) { - if (!ops::matmul::compatible(*crtp_this(), rhs)) throw std::runtime_error("tensor shape mismatch"); - return { *crtp_this(), std::forward(rhs) }; + if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch"); + return { self(), std::forward(rhs) }; } template unary_expression> apply(Func&& func) { - if (!ops::apply::compatible(*crtp_this())) throw std::runtime_error("tensor shape mismatch"); - return { *crtp_this(), std::forward(func) }; + if (!ops::apply::compatible(self())) throw std::runtime_error("tensor shape mismatch"); + return { self(), std::forward(func) }; } template binary_expression> apply(R&& rhs, Func&& func) { - if (!ops::apply::compatible(*crtp_this(), rhs)) throw std::runtime_error("tensor shape mismatch"); - return { *crtp_this(), std::forward(rhs), std::forward(func) }; + if (!ops::apply::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch"); + return { self(), std::forward(rhs), std::forward(func) }; + } } private: - decltype(auto) crtp_this() { return reinterpret_cast(this); } + decltype(auto) self() & { return reinterpret_cast(*this); } + decltype(auto) self() && { return reinterpret_cast(*this); } }; -template -struct unary_expression : expression_tag, ops_provider> { - E expr; - Op op; +namespace detail { - template >> +template +struct operand_storage { + using raw_type = remove_cvref_t; + using type = std::conditional_t, + raw_type, + std::conditional_t, T, raw_type>>; +}; + +template +using operand_storage_t = typename operand_storage::type; + +} // namespace detail + +template +struct unary_expression : expression_node_tag, ops_provider> { + using expr_type = detail::operand_storage_t; + + expr_type expr; + Op op; + + template && + !std::is_same_v, unary_expression>>> unary_expression(EFwd&& expr, OpFwd&& op = Op{}) // NOLINT : expr(std::forward(expr)), op(std::forward(op)) {} @@ -57,10 +80,13 @@ struct unary_expression : expression_tag, ops_provider> }; template -struct binary_expression : expression_tag, ops_provider> { - L lhs; - R rhs; - Op op; +struct binary_expression : expression_node_tag, ops_provider> { + using lhs_type = detail::operand_storage_t; + using rhs_type = detail::operand_storage_t; + + lhs_type lhs; + rhs_type rhs; + Op op; template binary_expression(LFwd&& lhs, RFwd&& rhs, OpFwd&& op = Op{}) diff --git a/include/cenet/math/ops.hpp b/include/cenet/math/ops.hpp index 54faea1..02eea67 100644 --- a/include/cenet/math/ops.hpp +++ b/include/cenet/math/ops.hpp @@ -94,7 +94,7 @@ struct matmul { constexpr std::size_t rhs_cols = rhs_shape::template sget<1>(); if constexpr (lhs_rows == dynamic_extent || rhs_cols == dynamic_extent) { - return dynamic_shape{}; + return dynamic_shape{ lhs_shape::template get<0>(), rhs_shape::template get<1>() }; } else { return static_shape{}; } diff --git a/include/cenet/math/tensor.hpp b/include/cenet/math/tensor.hpp index fb1d9e7..ce5d703 100644 --- a/include/cenet/math/tensor.hpp +++ b/include/cenet/math/tensor.hpp @@ -87,7 +87,8 @@ public: return *this; } public: - template >> + template && !std::is_same_v, tensor>>> tensor(E&& exprFwd) { // NOLINT if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");