From 78864efba9caf17b80677ae49ab7636ec2fcdf2e Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sun, 1 Nov 2026 07:34:22 +0100 Subject: [PATCH] feat(math): introduce ops_provider CRTP class --- include/cenet/math/expr.hpp | 18 ++++++++++++++++-- include/cenet/math/tensor.hpp | 2 +- include/cenet/math/tensor_view.hpp | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/cenet/math/expr.hpp b/include/cenet/math/expr.hpp index 30f44d6..711ff10 100644 --- a/include/cenet/math/expr.hpp +++ b/include/cenet/math/expr.hpp @@ -13,8 +13,22 @@ namespace math { struct expression_tag {}; +template +struct binary_expression; + +template +struct ops_provider { + template + binary_expression matmul(R&& rhs) { + if (!ops::matmul::compatible(*crtp_this(), rhs)) throw std::runtime_error("tensor shape mismatch"); + return { *crtp_this(), std::forward(rhs) }; + } +private: + decltype(auto) crtp_this() { return reinterpret_cast(this); } +}; + template -struct unary_expression : expression_tag { +struct unary_expression : expression_tag, ops_provider> { E expr; Op op; @@ -28,7 +42,7 @@ struct unary_expression : expression_tag { }; template -struct binary_expression : expression_tag { +struct binary_expression : expression_tag, ops_provider> { L lhs; R rhs; Op op; diff --git a/include/cenet/math/tensor.hpp b/include/cenet/math/tensor.hpp index 98f5fb0..dfaa9e4 100644 --- a/include/cenet/math/tensor.hpp +++ b/include/cenet/math/tensor.hpp @@ -17,7 +17,7 @@ namespace cenet { namespace math { template -class tensor : public expression_tag { +class tensor : public expression_tag, public ops_provider> { public: using traits = tensor_traits; using value_type = typename traits::value_type; diff --git a/include/cenet/math/tensor_view.hpp b/include/cenet/math/tensor_view.hpp index dd7ea77..adb7148 100644 --- a/include/cenet/math/tensor_view.hpp +++ b/include/cenet/math/tensor_view.hpp @@ -15,7 +15,7 @@ namespace cenet { namespace math { template -class tensor_view : public expression_tag { +class tensor_view : public expression_tag, public ops_provider> { static_assert(std::is_arithmetic_v); template