fix(math): fix critical tensor/expression mistakes

This commit is contained in:
2026-11-01 12:48:41 +01:00
parent 377cd5941d
commit 795915d100
3 changed files with 45 additions and 18 deletions
+42 -16
View File
@@ -12,6 +12,7 @@ namespace cenet {
namespace math {
struct expression_tag {};
struct expression_node_tag : expression_tag {};
template <typename E, typename Op>
struct unary_expression;
@@ -23,31 +24,53 @@ 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) };
if (!ops::matmul::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
return { self(), 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) };
if (!ops::apply<Func>::compatible(self())) throw std::runtime_error("tensor shape mismatch");
return { self(), 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) };
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
}
}
private:
decltype(auto) crtp_this() { return reinterpret_cast<CRTP*>(this); }
decltype(auto) self() & { return reinterpret_cast<CRTP&>(*this); }
decltype(auto) self() && { 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;
namespace detail {
template <typename EFwd = E, typename OpFwd = Op, typename = std::enable_if_t<std::is_constructible_v<E, EFwd>>>
template <typename T>
struct operand_storage {
using raw_type = remove_cvref_t<T>;
using type = std::conditional_t<std::is_base_of_v<expression_node_tag, raw_type>,
raw_type,
std::conditional_t<std::is_lvalue_reference_v<T>, T, raw_type>>;
};
template <typename T>
using operand_storage_t = typename operand_storage<T>::type;
} // namespace detail
template <typename E, typename Op>
struct unary_expression : expression_node_tag, ops_provider<unary_expression<E, Op>> {
using expr_type = detail::operand_storage_t<E>;
expr_type expr;
Op op;
template <typename EFwd = E,
typename OpFwd = Op,
typename = std::enable_if_t<std::is_constructible_v<expr_type, EFwd&&> &&
!std::is_same_v<std::decay_t<EFwd>, unary_expression>>>
unary_expression(EFwd&& expr, OpFwd&& op = Op{}) // NOLINT
: expr(std::forward<EFwd>(expr)), op(std::forward<OpFwd>(op)) {}
@@ -57,10 +80,13 @@ struct unary_expression : expression_tag, ops_provider<unary_expression<E, Op>>
};
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;
struct binary_expression : expression_node_tag, ops_provider<binary_expression<L, R, Op>> {
using lhs_type = detail::operand_storage_t<L>;
using rhs_type = detail::operand_storage_t<R>;
lhs_type lhs;
rhs_type rhs;
Op op;
template <typename LFwd = L, typename RFwd = R, typename OpFwd = Op>
binary_expression(LFwd&& lhs, RFwd&& rhs, OpFwd&& op = Op{})
+1 -1
View File
@@ -94,7 +94,7 @@ struct matmul {
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>{};
return dynamic_shape<lhs_rows, rhs_cols>{ lhs_shape::template get<0>(), rhs_shape::template get<1>() };
} else {
return static_shape<lhs_rows, rhs_cols>{};
}
+2 -1
View File
@@ -87,7 +87,8 @@ public:
return *this;
}
public:
template <typename E, typename = std::enable_if_t<detail::is_expression_v<E>>>
template <typename E,
typename = std::enable_if_t<detail::is_expression_v<E> && !std::is_same_v<std::decay_t<E>, tensor>>>
tensor(E&& exprFwd) { // NOLINT
if (m_shape != exprFwd.shape()) throw std::runtime_error("tensor shape mismatch");