#ifndef CENET_MATH_EXPR_HPP #define CENET_MATH_EXPR_HPP #include "ops.hpp" #include "tensor_traits.hpp" #include #include namespace cenet { namespace math { struct expression_tag {}; template struct unary_expression : expression_tag { E expr; constexpr auto shape() const { return expr.shape(); } auto coeff(std::size_t index) const { return Op{}(expr.coeff(index)); } }; template struct binary_expression : expression_tag { L lhs; R rhs; template binary_expression(LFwd&& lhs, RFwd&& rhs) : lhs(std::forward(lhs)), rhs(std::forward(rhs)) {} constexpr auto shape() const { return Op::shape(lhs, rhs); } auto coeff(std::size_t index) const { return Op{}(index, lhs.coeff(index), as_operand(rhs)); } private: template static decltype(auto) as_operand(T&& value) { if constexpr (is_tensor_v) { return std::as_const(value).view(); } else { return std::forward(value); } } }; namespace detail { template using remove_cvref_t = std::remove_cv_t>; template struct operand { using type = T; }; template struct operand::view_type>> { using type = typename remove_cvref_t::view_type; }; template 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>> binary_expression operator+(L&& lhs, R&& rhs) { return { std::forward(lhs), std::forward(rhs) }; } template >::type>> binary_expression operator-(L&& lhs, R&& rhs) { return { std::forward(lhs), std::forward(rhs) }; } } // namespace math } // namespace cenet #endif // CENET_MATH_EXPR_HPP