diff --git a/include/cenet/math/expr.hpp b/include/cenet/math/expr.hpp index fb67777..0e0da6a 100644 --- a/include/cenet/math/expr.hpp +++ b/include/cenet/math/expr.hpp @@ -16,6 +16,8 @@ template struct unary_expression : expression_tag { E expr; + constexpr auto shape() const { return expr.shape(); } + auto eval(std::size_t index) { return Op{}(expr.coeff(index)); } }; @@ -28,6 +30,8 @@ struct binary_expression : expression_tag { binary_expression(LFwd&& lhs, RFwd&& rhs) : lhs(std::forward(lhs)), rhs(std::forward(rhs)) {} + constexpr auto shape() const { return Op::shape(lhs, rhs); } + auto eval(std::size_t index) { return Op{}(index, lhs.coeff(index), as_operand(rhs)); } private: template diff --git a/include/cenet/math/ops.hpp b/include/cenet/math/ops.hpp index 59665b2..221a2d2 100644 --- a/include/cenet/math/ops.hpp +++ b/include/cenet/math/ops.hpp @@ -1,9 +1,12 @@ #ifndef CENET_MATH_OPS_HPP #define CENET_MATH_OPS_HPP +#include "cenet/math/tensor_shape.hpp" #include "tensor_traits.hpp" #include +#include +#include namespace cenet { namespace math { @@ -14,15 +17,34 @@ private: template struct result_type_impl {}; - template - struct result_type_impl, tensor_view> { - using type = - tensor>() + std::declval>()), Dims...>; + template + struct result_type_impl, tensor_view> { + using lhs_view = tensor_view; + using rhs_view = tensor_view; + + using type = std::enable_if_t< + detail::is_shape_equivalent::value, + tensor>() + std::declval>()), LDims...>>; }; public: template using result_type = typename result_type_impl::type; + template && is_tensor_v)>> + static bool compatible(const L& lhs, const R& rhs) { + return lhs.shape() == rhs.shape(); + } + + template + static constexpr auto shape(const L& lhs, const R& rhs) { + if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape"); + if constexpr (detail::is_static_shape_v) { + return lhs.shape(); + } else { + return rhs.shape(); + } + } + template constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const { return lhs + rhs.coeff(index); @@ -34,15 +56,34 @@ private: template struct result_type_impl {}; - template - struct result_type_impl, tensor_view> { - using type = - tensor>() - std::declval>()), Dims...>; + template + struct result_type_impl, tensor_view> { + using lhs_view = tensor_view; + using rhs_view = tensor_view; + + using type = std::enable_if_t< + detail::is_shape_equivalent::value, + tensor>() - std::declval>()), LDims...>>; }; public: template using result_type = typename result_type_impl::type; + template && is_tensor_v)>> + static bool compatible(const L& lhs, const R& rhs) { + return lhs.shape() == rhs.shape(); + } + + template + static constexpr auto shape(const L& lhs, const R& rhs) { + if (lhs.shape() != rhs.shape()) throw std::runtime_error("unexpected tensor shape"); + if constexpr (detail::is_static_shape_v) { + return lhs.shape(); + } else { + return rhs.shape(); + } + } + template constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const { return lhs - rhs.coeff(index); diff --git a/include/cenet/math/tensor.hpp b/include/cenet/math/tensor.hpp index 94772b8..812e18f 100644 --- a/include/cenet/math/tensor.hpp +++ b/include/cenet/math/tensor.hpp @@ -3,6 +3,7 @@ #include "expr.hpp" #include "tensor_fwd.hpp" +#include "tensor_shape.hpp" #include "tensor_traits.hpp" #include @@ -14,67 +15,6 @@ namespace cenet { namespace math { -namespace detail { - -template -struct static_shape { - static_shape() = default; - - template = sizeof...(Dims))>> - explicit static_shape(const static_shape& oth) {} - - template - static constexpr bool compatible() { - return ((Dims == OthDims) && ...); - } - - template - static constexpr bool compatible() { - return compatible_impl::value; - } -private: - template - struct compatible_impl : std::false_type {}; - - template - struct compatible_impl> - : std::conditional_t<((Dims == OthDims) && ...), std::true_type, std::false_type> {}; -}; - -template -struct dynamic_shape { - dynamic_shape() = default; - - template = sizeof...(Dims))>> - explicit dynamic_shape(const dynamic_shape& oth) { - constexpr std::size_t ext_offset = ExtRank - sizeof...(Dims); - for (std::size_t i = 0; i < sizeof...(Dims); ++i) { - dimensions[i] = oth.dimensions[ext_offset + i]; - } - } - - template - static constexpr bool compatible() { - return ((Dims == dynamic_extent || Dims == OthDims) && ...); - } - - template - static constexpr bool compatible() { - return compatible_impl::value; - } - - std::size_t dimensions[sizeof...(Dims)]{}; -private: - template - struct compatible_impl : std::false_type {}; - - template - struct compatible_impl> - : std::conditional_t<((Dims == dynamic_extent || Dims == OthDims) && ...), std::true_type, std::false_type> {}; -}; - -} // namespace detail - template class tensor_view { static_assert(std::is_arithmetic_v); @@ -86,6 +26,7 @@ class tensor_view { friend class tensor_view; public: using traits = tensor_traits>; + using shape_type = std::conditional_t, static_shape>; using value_type = T; public: template auto inner_view(value_type* view, std::index_sequence) { @@ -166,9 +109,6 @@ private: } } private: - using shape_type = - std::conditional_t, detail::static_shape>; - template tensor_view(value_type* view, Fwd&& shape) : m_view(view), m_shape(std::forward(shape)) {} @@ -186,9 +126,7 @@ public: using constant_value_type = const value_type; using view_type = tensor_view; using constant_view_type = tensor_view; -private: - using shape_type = - std::conditional_t, detail::static_shape>; + using shape_type = std::conditional_t, static_shape>; public: tensor() { if constexpr (traits::dynamic) { @@ -205,7 +143,7 @@ public: typename = std::enable_if_t<( traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v && ...))>> explicit tensor(RtDims... dimensions) - : m_shape(detail::dynamic_shape{}) { + : m_shape(dynamic_shape{}) { set_dimensions(dimensions...); } @@ -213,29 +151,35 @@ public: tensor(tensor&& other) noexcept : m_shape(std::move(other.m_shape)) { - if constexpr (!traits::inline_storage) m_items = nullptr; - *this = std::move(other); + if constexpr (!traits::inline_storage) { + m_items = other.m_items; + other.m_items = nullptr; + } else { + std::memcpy(m_items, other.m_items, sizeof(m_items)); + } } tensor& operator=(tensor&& other) noexcept { if (this == &other) return *this; release(); - m_items = other.m_items; - if constexpr (!traits::inline_storage) other.m_items = nullptr; + if constexpr (!traits::inline_storage) { + m_items = other.m_items; + other.m_items = nullptr; + } else { + std::memcpy(m_items, other.m_items, sizeof(m_items)); + } return *this; } tensor(const tensor& other) : m_shape(other.m_shape) { - if constexpr (!traits::inline_storage) m_items = nullptr; - *this = other; + dyn_alloc(std::make_index_sequence{}); + dyn_copy(std::make_index_sequence{}); } tensor& operator=(const tensor& other) { if (this == &other) return *this; release(); - std::memcpy(m_shape.dimensions, other.m_shape.dimensions, sizeof(m_shape.dimensions)); - std::memcpy(m_shape.strides, other.m_shape.strides, sizeof(m_shape.strides)); dyn_alloc(std::make_index_sequence{}); dyn_copy(std::make_index_sequence{}); @@ -247,8 +191,11 @@ public: shape_type::template compatible::type>()>> tensor(E&& expr) { // NOLINT if constexpr (!traits::inline_storage) { - static_assert(!traits::dynamic, "Dynamic tensors are not supported yet!"); - m_items = new value_type[traits::size]; + if constexpr (traits::dynamic) { + set_dimensions(expr.shape()); + } else { + m_items = new value_type[traits::size]; + } } for (std::size_t i = 0; i < view().size(); ++i) { @@ -260,6 +207,7 @@ public: typename = std::enable_if_t && !is_tensor_v && std::is_same_v::type>>> tensor& operator=(E&& expr) { + if (!expr.valid_for(m_shape)) throw std::runtime_error("unexpected tensor shape"); for (std::size_t i = 0; i < view().size(); ++i) { m_items[i] = std::forward(expr); } @@ -283,6 +231,16 @@ public: m_items = new value_type[(dimensions * ... * 1)]; std::memset(m_items, 0, sizeof(*m_items) * (dimensions * ... * 1)); } + + template