feat(math): introduce apply operation
This commit is contained in:
@@ -13,6 +13,9 @@ namespace math {
|
||||
|
||||
struct expression_tag {};
|
||||
|
||||
template <typename E, typename Op>
|
||||
struct unary_expression;
|
||||
|
||||
template <typename L, typename R, typename Op>
|
||||
struct binary_expression;
|
||||
|
||||
@@ -23,6 +26,18 @@ struct ops_provider {
|
||||
if (!ops::matmul::compatible(*crtp_this(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { *crtp_this(), std::forward<R>(rhs) };
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
unary_expression<CRTP, ops::apply<Func>> apply(Func&& func) {
|
||||
if (!ops::apply<Func>::compatible(*crtp_this())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { *crtp_this(), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
template <typename R, typename Func>
|
||||
binary_expression<CRTP, R, ops::apply<Func>> apply(R&& rhs, Func&& func) {
|
||||
if (!ops::apply<Func>::compatible(*crtp_this(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { *crtp_this(), std::forward<R>(rhs), std::forward<Func>(func) };
|
||||
}
|
||||
private:
|
||||
decltype(auto) crtp_this() { return reinterpret_cast<CRTP*>(this); }
|
||||
};
|
||||
@@ -38,7 +53,7 @@ struct unary_expression : expression_tag, ops_provider<unary_expression<E, Op>>
|
||||
|
||||
constexpr auto shape() const { return expr.shape(); }
|
||||
|
||||
auto coeff(std::size_t index) const { return Op{}(index, std::as_const(expr)); }
|
||||
auto coeff(std::size_t index) const { return op(index, std::as_const(expr)); }
|
||||
};
|
||||
|
||||
template <typename L, typename R, typename Op>
|
||||
@@ -53,7 +68,7 @@ struct binary_expression : expression_tag, ops_provider<binary_expression<L, R,
|
||||
|
||||
constexpr auto shape() const { return Op::shape(lhs, rhs); }
|
||||
|
||||
auto coeff(std::size_t index) const { return Op{}(index, std::as_const(lhs), std::as_const(rhs)); }
|
||||
auto coeff(std::size_t index) const { return op(index, std::as_const(lhs), std::as_const(rhs)); }
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
@@ -119,6 +119,58 @@ struct matmul {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Func>
|
||||
struct apply {
|
||||
Func func;
|
||||
|
||||
template <typename Fwd = Func, typename = std::enable_if_t<std::is_constructible_v<Func, Fwd>>>
|
||||
apply(Fwd&& fwd) // NOLINT
|
||||
: func(std::forward<Fwd>(fwd)) {}
|
||||
|
||||
template <typename E>
|
||||
static constexpr bool compatible(const E& expr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
static constexpr bool compatible(const L& lhs, const R& rhs) {
|
||||
return lhs.shape() == rhs.shape();
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static constexpr auto shape(const E& expr) {
|
||||
if (!compatible(expr)) throw std::runtime_error("uncompatible operands");
|
||||
return expr.shape();
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
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<decltype(lhs.shape())>;
|
||||
using rhs_shape = remove_cvref_t<decltype(rhs.shape())>;
|
||||
|
||||
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<lhs_rows, rhs_cols>{};
|
||||
} else {
|
||||
return static_shape<lhs_rows, rhs_cols>{};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
constexpr auto operator()(std::size_t index, const E& expr) const {
|
||||
return func(expr.coeff(index));
|
||||
}
|
||||
|
||||
template <typename L, typename R>
|
||||
constexpr auto operator()(std::size_t index, const L& lhs, const R& rhs) const {
|
||||
return func(lhs.coeff(index), rhs.coeff(index));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ops
|
||||
} // namespace math
|
||||
} // namespace cenet
|
||||
|
||||
Reference in New Issue
Block a user