From de783ad7b7bbeea16a7095ffe83ce33083b4c4ed Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sun, 1 Nov 2026 06:03:04 +0100 Subject: [PATCH] refactor(math): improve expressions Improve expressions by passing index, lhs and rhs. --- include/cenet/math/expr.hpp | 13 ++----------- include/cenet/math/ops.hpp | 6 +++--- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/include/cenet/math/expr.hpp b/include/cenet/math/expr.hpp index f84b334..340a17a 100644 --- a/include/cenet/math/expr.hpp +++ b/include/cenet/math/expr.hpp @@ -24,7 +24,7 @@ struct unary_expression : expression_tag { constexpr auto shape() const { return expr.shape(); } - auto coeff(std::size_t index) const { return Op{}(expr.coeff(index)); } + auto coeff(std::size_t index) const { return Op{}(index, std::as_const(expr)); } }; template @@ -39,16 +39,7 @@ struct binary_expression : expression_tag { 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); - } - } + auto coeff(std::size_t index) const { return Op{}(index, std::as_const(lhs), std::as_const(rhs)); } }; namespace detail { diff --git a/include/cenet/math/ops.hpp b/include/cenet/math/ops.hpp index 4593c44..498305e 100644 --- a/include/cenet/math/ops.hpp +++ b/include/cenet/math/ops.hpp @@ -29,9 +29,9 @@ struct generic_binary_op { } } - template - constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const { - return Op{}(lhs, rhs.coeff(index)); + template + constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const { + return Op{}(lhs.coeff(index), rhs.coeff(index)); } };