feat(math): introduce apply operation
This commit is contained in:
@@ -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