diff --git a/include/cenet/math/expr.hpp b/include/cenet/math/expr.hpp index 4ccfbf0..4697761 100644 --- a/include/cenet/math/expr.hpp +++ b/include/cenet/math/expr.hpp @@ -39,6 +39,10 @@ struct ops_provider { if (!ops::apply::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch"); return { self(), std::forward(rhs), std::forward(func) }; } + + unary_expression transpose() { + if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch"); + return { self() }; } private: decltype(auto) self() & { return reinterpret_cast(*this); } diff --git a/include/cenet/math/ops.hpp b/include/cenet/math/ops.hpp index 02eea67..199ac3e 100644 --- a/include/cenet/math/ops.hpp +++ b/include/cenet/math/ops.hpp @@ -171,6 +171,35 @@ struct apply { } }; +struct transpose { + template + static constexpr bool compatible(const E& expr) { + return true; + } + + template + static constexpr auto shape(const E& expr) { + if (!compatible(expr)) throw std::runtime_error("uncompatible operands"); + + using shape_type = remove_cvref_t; + + constexpr std::size_t rows = shape_type::template sget<0>(); + constexpr std::size_t cols = shape_type::template sget<1>(); + + if constexpr (rows == dynamic_extent || cols == dynamic_extent) { + return dynamic_shape{ shape_type::template get<1>(), shape_type::template get<0>() }; + } else { + return static_shape{}; + } + } + + template + constexpr auto operator()(std::size_t index, const E& expr) const { + const std::size_t cols = expr.shape().template get<1>(); + return expr.coeff(((index % cols) * cols) + (index / cols)); + } +}; + } // namespace ops } // namespace math } // namespace cenet diff --git a/include/cenet/math/tensor_shape.hpp b/include/cenet/math/tensor_shape.hpp index 8c45e76..06fb34d 100644 --- a/include/cenet/math/tensor_shape.hpp +++ b/include/cenet/math/tensor_shape.hpp @@ -151,6 +151,13 @@ struct is_static_shape : std::false_type {}; template struct is_static_shape> : std::true_type {}; +template +struct is_static_shape_var : std::true_type {}; + +template +struct is_static_shape_var + : std::conditional_t<(First == dynamic_extent), std::false_type, is_static_shape_var> {}; + template inline constexpr auto is_static_shape_v = is_static_shape::value; @@ -209,8 +216,16 @@ struct best_shape, R> { using type = std::enable_if_t, R>>; }; +template +struct shape_of_impl { + using type = std::conditional_t::value, static_shape, dynamic_shape>; +}; + } // namespace detail +template +using shape_of = typename detail::shape_of_impl::type; + template