feat(math): add reshape operation

This commit is contained in:
2026-11-01 20:44:25 +01:00
parent 961067c28d
commit d23905c259
3 changed files with 35 additions and 14 deletions
+26 -12
View File
@@ -146,18 +146,7 @@ struct apply {
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>{};
}
return lhs.shape();
}
template <typename E>
@@ -201,6 +190,31 @@ struct transpose {
}
};
// TODO: introduce dynamic reshape
template <typename S, typename = std::enable_if_t<::cenet::math::detail::is_static_shape_v<S>>>
struct reshape {
template <typename E>
static constexpr bool compatible(const E& expr) {
using shape_type = remove_cvref_t<decltype(expr.shape())>;
if constexpr (!::cenet::math::detail::is_static_shape<shape_type>::value) {
return false;
} else {
return shape_type::size == S::size;
}
}
template <typename E>
static constexpr auto shape(const E& expr) {
if (!compatible(expr)) throw std::runtime_error("uncompatible operands");
return S{};
}
template <typename E>
constexpr auto operator()(std::size_t index, const E& expr) const {
return expr.coeff(index);
}
};
} // namespace ops
} // namespace math
} // namespace cenet