From e8edbe7b7ed7627b70ebf80c40e666d286e55ddf Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Fri, 30 Oct 2026 14:55:22 +0100 Subject: [PATCH] feat(math): add multiply operation --- include/cenet/math/expr.hpp | 8 ++++++++ include/cenet/math/ops.hpp | 41 ++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/include/cenet/math/expr.hpp b/include/cenet/math/expr.hpp index 36bdad1..17898ca 100644 --- a/include/cenet/math/expr.hpp +++ b/include/cenet/math/expr.hpp @@ -99,6 +99,14 @@ 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 diff --git a/include/cenet/math/ops.hpp b/include/cenet/math/ops.hpp index 221a2d2..ba393ce 100644 --- a/include/cenet/math/ops.hpp +++ b/include/cenet/math/ops.hpp @@ -1,7 +1,7 @@ #ifndef CENET_MATH_OPS_HPP #define CENET_MATH_OPS_HPP -#include "cenet/math/tensor_shape.hpp" +#include "tensor_shape.hpp" #include "tensor_traits.hpp" #include @@ -90,6 +90,45 @@ public: } }; +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); + } +}; + } // namespace ops } // namespace math } // namespace cenet