feat(math): add transpose operation and fill_random
This commit is contained in:
@@ -39,6 +39,10 @@ struct ops_provider {
|
||||
if (!ops::apply<Func>::compatible(self(), rhs)) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self(), std::forward<R>(rhs), std::forward<Func>(func) };
|
||||
}
|
||||
|
||||
unary_expression<CRTP, ops::transpose> transpose() {
|
||||
if (!ops::transpose::compatible(self())) throw std::runtime_error("tensor shape mismatch");
|
||||
return { self() };
|
||||
}
|
||||
private:
|
||||
decltype(auto) self() & { return reinterpret_cast<CRTP&>(*this); }
|
||||
|
||||
@@ -171,6 +171,35 @@ struct apply {
|
||||
}
|
||||
};
|
||||
|
||||
struct transpose {
|
||||
template <typename E>
|
||||
static constexpr bool compatible(const E& expr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static constexpr auto shape(const E& expr) {
|
||||
if (!compatible(expr)) throw std::runtime_error("uncompatible operands");
|
||||
|
||||
using shape_type = remove_cvref_t<decltype(expr.shape())>;
|
||||
|
||||
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<cols, rows>{ shape_type::template get<1>(), shape_type::template get<0>() };
|
||||
} else {
|
||||
return static_shape<cols, rows>{};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
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
|
||||
|
||||
@@ -151,6 +151,13 @@ struct is_static_shape : std::false_type {};
|
||||
template <std::size_t... Dims>
|
||||
struct is_static_shape<static_shape<Dims...>> : std::true_type {};
|
||||
|
||||
template <std::size_t... Dims>
|
||||
struct is_static_shape_var : std::true_type {};
|
||||
|
||||
template <std::size_t First, std::size_t... Rest>
|
||||
struct is_static_shape_var<First, Rest...>
|
||||
: std::conditional_t<(First == dynamic_extent), std::false_type, is_static_shape_var<Rest...>> {};
|
||||
|
||||
template <typename T>
|
||||
inline constexpr auto is_static_shape_v = is_static_shape<T>::value;
|
||||
|
||||
@@ -209,8 +216,16 @@ struct best_shape<L<LDims...>, R<RDims...>> {
|
||||
using type = std::enable_if_t<valid, std::conditional_t<left, L<LDims...>, R<RDims...>>>;
|
||||
};
|
||||
|
||||
template <std::size_t... Dims>
|
||||
struct shape_of_impl {
|
||||
using type = std::conditional_t<is_static_shape_var<Dims...>::value, static_shape<Dims...>, dynamic_shape<Dims...>>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <std::size_t... Dims>
|
||||
using shape_of = typename detail::shape_of_impl<Dims...>::type;
|
||||
|
||||
template <template <std::size_t...> class L,
|
||||
std::size_t... LDims,
|
||||
template <std::size_t...> class R,
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
@@ -99,6 +100,16 @@ public:
|
||||
}
|
||||
|
||||
void fill(const tensor_view& view) { std::memcpy(m_view, view.m_view, size() * sizeof(*m_view)); }
|
||||
|
||||
template <typename Dist>
|
||||
void fill_random(Dist dist) {
|
||||
static std::random_device s_rd;
|
||||
static std::mt19937 s_mt(s_rd());
|
||||
|
||||
for (std::size_t i = 0; i < size(); ++i) {
|
||||
m_view[i] = dist(s_mt);
|
||||
}
|
||||
}
|
||||
public:
|
||||
value_type& coeff(std::size_t index) { return m_view[index]; }
|
||||
const value_type& coeff(std::size_t index) const { return m_view[index]; }
|
||||
|
||||
Reference in New Issue
Block a user