feat(math): introduce ops_provider CRTP class
This commit is contained in:
@@ -13,8 +13,22 @@ namespace math {
|
||||
|
||||
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>
|
||||
struct unary_expression : expression_tag {
|
||||
struct unary_expression : expression_tag, ops_provider<unary_expression<E, Op>> {
|
||||
E expr;
|
||||
Op op;
|
||||
|
||||
@@ -28,7 +42,7 @@ struct unary_expression : expression_tag {
|
||||
};
|
||||
|
||||
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;
|
||||
R rhs;
|
||||
Op op;
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace cenet {
|
||||
namespace math {
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
class tensor : public expression_tag {
|
||||
class tensor : public expression_tag, public ops_provider<tensor<T, Dims...>> {
|
||||
public:
|
||||
using traits = tensor_traits<tensor>;
|
||||
using value_type = typename traits::value_type;
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace cenet {
|
||||
namespace math {
|
||||
|
||||
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>);
|
||||
|
||||
template <typename, std::size_t...>
|
||||
|
||||
Reference in New Issue
Block a user