From 2267c8d7f8f5e98dc42976cb542c49269b30d99a Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sun, 1 Nov 2026 07:34:08 +0100 Subject: [PATCH] feat(math): introduce matmul operator --- include/cenet/math/expr.hpp | 4 +-- include/cenet/math/ops.hpp | 50 +++++++++++++++++++++++++++++- include/cenet/math/type_traits.hpp | 15 +++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 include/cenet/math/type_traits.hpp diff --git a/include/cenet/math/expr.hpp b/include/cenet/math/expr.hpp index 7e5da3d..30f44d6 100644 --- a/include/cenet/math/expr.hpp +++ b/include/cenet/math/expr.hpp @@ -2,6 +2,7 @@ #define CENET_MATH_EXPR_HPP #include "ops.hpp" +#include "type_traits.hpp" #include #include @@ -43,9 +44,6 @@ struct binary_expression : expression_tag { namespace detail { -template -using remove_cvref_t = std::remove_cv_t>; - template struct operand { using type = T; diff --git a/include/cenet/math/ops.hpp b/include/cenet/math/ops.hpp index 3021f36..7ed4f6c 100644 --- a/include/cenet/math/ops.hpp +++ b/include/cenet/math/ops.hpp @@ -1,7 +1,9 @@ #ifndef CENET_MATH_OPS_HPP #define CENET_MATH_OPS_HPP +#include "tensor_fwd.hpp" #include "tensor_shape.hpp" +#include "type_traits.hpp" #include #include @@ -25,7 +27,7 @@ struct has_shape().shape())>> : std::tru template struct generic_binary_op { template - static bool compatible(const L& lhs, const R& rhs) { + static constexpr bool compatible(const L& lhs, const R& rhs) { if constexpr (std::is_arithmetic_v || std::is_arithmetic_v) { return true; } else { @@ -71,6 +73,52 @@ struct minus : generic_binary_op> {}; struct multiply : generic_binary_op> {}; struct divide : generic_binary_op> {}; +struct matmul { + template + static constexpr bool compatible(const L& lhs, const R& rhs) { + using lhs_shape = remove_cvref_t; + using rhs_shape = remove_cvref_t; + + return lhs_shape::rank == 2 && rhs_shape::rank == 2 && + lhs.shape().template get<1>() == rhs.shape().template get<0>(); + } + + template + static constexpr auto shape(const L& lhs, const R& rhs) { + if (!compatible(lhs, rhs)) throw std::runtime_error("uncompatible operands"); + + using lhs_shape = remove_cvref_t; + using rhs_shape = remove_cvref_t; + + constexpr std::size_t lhs_rows = lhs_shape::template sget<0>(); + constexpr std::size_t rhs_cols = rhs_shape::template sget<1>(); + + if constexpr (lhs_rows == dynamic_extent || rhs_cols == dynamic_extent) { + return dynamic_shape{}; + } else { + return static_shape{}; + } + } + + template + constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const { + const std::size_t outCols = shape(lhs, rhs).template get<1>(); + const std::size_t row = index / outCols; + const std::size_t col = index % outCols; + + auto lhsRows = lhs.shape().template get<0>(); + auto lhsCols = lhs.shape().template get<1>(); + auto rhsCols = rhs.shape().template get<1>(); + + decltype(lhs.coeff(0) * rhs.coeff(0)) acc{}; + for (std::size_t i = 0; i < lhsCols; ++i) { + acc += lhs.coeff((row * lhsCols) + i) * rhs.coeff((i * rhsCols) + col); + } + + return acc; + } +}; + } // namespace ops } // namespace math } // namespace cenet diff --git a/include/cenet/math/type_traits.hpp b/include/cenet/math/type_traits.hpp new file mode 100644 index 0000000..3b2a630 --- /dev/null +++ b/include/cenet/math/type_traits.hpp @@ -0,0 +1,15 @@ +#ifndef CENET_MATH_TYPE_TRAITS_HPP +#define CENET_MATH_TYPE_TRAITS_HPP + +#include + +namespace cenet { +namespace math { + +template +using remove_cvref_t = std::remove_cv_t>; + +} +} // namespace cenet + +#endif // CENET_MATH_TYPE_TRAITS_HPP