Files
cenet/include/cenet/math/expr.hpp
T

123 lines
4.0 KiB
C++

#ifndef CENET_MATH_EXPR_HPP
#define CENET_MATH_EXPR_HPP
#include "ops.hpp"
#include "type_traits.hpp"
#include <stdexcept>
#include <type_traits>
#include <utility>
namespace cenet {
namespace math {
struct expression_tag {};
template <typename E, typename Op>
struct unary_expression;
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) };
}
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); }
};
template <typename E, typename Op>
struct unary_expression : expression_tag, ops_provider<unary_expression<E, Op>> {
E expr;
Op op;
template <typename EFwd = E, typename OpFwd = Op, typename = std::enable_if_t<std::is_constructible_v<E, EFwd>>>
unary_expression(EFwd&& expr, OpFwd&& op = Op{}) // NOLINT
: expr(std::forward<EFwd>(expr)), op(std::forward<OpFwd>(op)) {}
constexpr auto shape() const { return Op::shape(expr); }
auto coeff(std::size_t index) const { return op(index, std::as_const(expr)); }
};
template <typename L, typename R, typename Op>
struct binary_expression : expression_tag, ops_provider<binary_expression<L, R, Op>> {
L lhs;
R rhs;
Op op;
template <typename LFwd = L, typename RFwd = R, typename OpFwd = Op>
binary_expression(LFwd&& lhs, RFwd&& rhs, OpFwd&& op = Op{})
: lhs(std::forward<LFwd>(lhs)), rhs(std::forward<RFwd>(rhs)), op(std::forward<OpFwd>(op)) {}
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)); }
};
namespace detail {
template <typename T, typename = void>
struct operand {
using type = T;
};
template <typename T>
struct operand<T, std::void_t<typename remove_cvref_t<T>::view_type>> {
using type = typename remove_cvref_t<T>::view_type;
};
template <typename T>
using operand_t = typename operand<T>::type;
template <typename T>
static constexpr bool is_expression_v = std::is_base_of_v<expression_tag, remove_cvref_t<T>>;
} // namespace detail
template <typename L, typename R, typename Op = ops::plus>
binary_expression<L, R, Op> operator+(L&& lhs, R&& rhs) {
// TODO: introduce a better exception class
if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch");
return { std::forward<L>(lhs), std::forward<R>(rhs) };
}
template <typename L, typename R, typename Op = ops::minus>
binary_expression<L, R, Op> operator-(L&& lhs, R&& rhs) {
if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch");
return { std::forward<L>(lhs), std::forward<R>(rhs) };
}
template <typename L, typename R, typename Op = ops::multiply>
binary_expression<L, R, Op> operator*(L&& lhs, R&& rhs) {
if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch");
return { std::forward<L>(lhs), std::forward<R>(rhs) };
}
template <typename L, typename R, typename Op = ops::divide>
binary_expression<L, R, Op> operator/(L&& lhs, R&& rhs) {
if (!Op::compatible(lhs, rhs)) throw std::runtime_error("tensor shape mismatch");
return { std::forward<L>(lhs), std::forward<R>(rhs) };
}
} // namespace math
} // namespace cenet
#endif // CENET_MATH_EXPR_HPP