From 8dee1cbe1f1d3bd5dbc21ec95f121decccd251e3 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sun, 1 Nov 2026 06:10:33 +0100 Subject: [PATCH] feat(math): introduce scalar operations --- include/cenet/math/expr.hpp | 1 - include/cenet/math/ops.hpp | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/cenet/math/expr.hpp b/include/cenet/math/expr.hpp index 340a17a..7e5da3d 100644 --- a/include/cenet/math/expr.hpp +++ b/include/cenet/math/expr.hpp @@ -2,7 +2,6 @@ #define CENET_MATH_EXPR_HPP #include "ops.hpp" -#include "tensor_traits.hpp" #include #include diff --git a/include/cenet/math/ops.hpp b/include/cenet/math/ops.hpp index 498305e..72b637b 100644 --- a/include/cenet/math/ops.hpp +++ b/include/cenet/math/ops.hpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace cenet { namespace math { @@ -15,12 +16,16 @@ template struct generic_binary_op { template static bool compatible(const L& lhs, const R& rhs) { - return lhs.shape() == rhs.shape(); + if constexpr (std::is_arithmetic_v || std::is_arithmetic_v) { + return true; + } else { + 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("tensor shape mismatch"); + if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands"); // TODO: introduce "best shape" function/trait if constexpr (detail::is_static_shape_v) { return lhs.shape(); @@ -31,7 +36,13 @@ struct generic_binary_op { template constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const { - return Op{}(lhs.coeff(index), rhs.coeff(index)); + if constexpr (std::is_arithmetic_v) { + return Op{}(lhs, rhs.coeff(index)); + } else if constexpr (std::is_arithmetic_v) { + return Op{}(lhs.coeff(index), rhs); + } else { + return Op{}(lhs.coeff(index), rhs.coeff(index)); + } } };