feat(math): introduce ops_provider CRTP class

This commit is contained in:
2026-11-01 07:34:22 +01:00
parent 2267c8d7f8
commit 78864efba9
3 changed files with 18 additions and 4 deletions
+16 -2
View File
@@ -13,8 +13,22 @@ namespace math {
struct expression_tag {}; struct expression_tag {};
template <typename L, typename R, typename Op>
struct binary_expression;
template <typename CRTP>
struct ops_provider {
template <typename R>
binary_expression<CRTP, R, ops::matmul> matmul(R&& rhs) {
if (!ops::matmul::compatible(*crtp_this(), rhs)) throw std::runtime_error("tensor shape mismatch");
return { *crtp_this(), std::forward<R>(rhs) };
}
private:
decltype(auto) crtp_this() { return reinterpret_cast<CRTP*>(this); }
};
template <typename E, typename Op> template <typename E, typename Op>
struct unary_expression : expression_tag { struct unary_expression : expression_tag, ops_provider<unary_expression<E, Op>> {
E expr; E expr;
Op op; Op op;
@@ -28,7 +42,7 @@ struct unary_expression : expression_tag {
}; };
template <typename L, typename R, typename Op> template <typename L, typename R, typename Op>
struct binary_expression : expression_tag { struct binary_expression : expression_tag, ops_provider<binary_expression<L, R, Op>> {
L lhs; L lhs;
R rhs; R rhs;
Op op; Op op;
+1 -1
View File
@@ -17,7 +17,7 @@ namespace cenet {
namespace math { namespace math {
template <typename T, std::size_t... Dims> template <typename T, std::size_t... Dims>
class tensor : public expression_tag { class tensor : public expression_tag, public ops_provider<tensor<T, Dims...>> {
public: public:
using traits = tensor_traits<tensor>; using traits = tensor_traits<tensor>;
using value_type = typename traits::value_type; using value_type = typename traits::value_type;
+1 -1
View File
@@ -15,7 +15,7 @@ namespace cenet {
namespace math { namespace math {
template <typename T, std::size_t... Dims> template <typename T, std::size_t... Dims>
class tensor_view : public expression_tag { class tensor_view : public expression_tag, public ops_provider<tensor_view<T, Dims...>> {
static_assert(std::is_arithmetic_v<T>); static_assert(std::is_arithmetic_v<T>);
template <typename, std::size_t...> template <typename, std::size_t...>