diff --git a/include/cenet/math/expr.hpp b/include/cenet/math/expr.hpp new file mode 100644 index 0000000..fb67777 --- /dev/null +++ b/include/cenet/math/expr.hpp @@ -0,0 +1,101 @@ +#ifndef CENET_MATH_EXPR_HPP +#define CENET_MATH_EXPR_HPP + +#include "ops.hpp" +#include "tensor_traits.hpp" + +#include +#include + +namespace cenet { +namespace math { + +struct expression_tag {}; + +template +struct unary_expression : expression_tag { + E expr; + + auto eval(std::size_t index) { return Op{}(expr.coeff(index)); } +}; + +template +struct binary_expression : expression_tag { + L lhs; + R rhs; + + template + binary_expression(LFwd&& lhs, RFwd&& rhs) + : lhs(std::forward(lhs)), rhs(std::forward(rhs)) {} + + auto eval(std::size_t index) { return Op{}(index, lhs.coeff(index), as_operand(rhs)); } +private: + template + static decltype(auto) as_operand(T&& value) { + if constexpr (is_tensor_v) { + return std::as_const(value).view(); + } else { + return std::forward(value); + } + } +}; + +namespace detail { + +template +using remove_cvref_t = std::remove_cv_t>; + +template +struct operand { + using type = T; +}; + +template +struct operand::view_type>> { + using type = typename remove_cvref_t::view_type; +}; + +template +using operand_t = typename operand::type; + +template +static constexpr bool is_expression_v = std::is_base_of_v>; + +template +struct expression_result { + using type = remove_cvref_t; +}; + +template +struct expression_result> { + using type = typename Op::template result_type>::type>>; +}; + +template +struct expression_result> { + using type = typename Op::template result_type>::type>, + operand_t>::type>>; +}; + +} // namespace detail + +template >::type>> +binary_expression operator+(L&& lhs, R&& rhs) { + return { std::forward(lhs), std::forward(rhs) }; +} + +template >::type>> +binary_expression operator-(L&& lhs, R&& rhs) { + return { std::forward(lhs), std::forward(rhs) }; +} + +} // namespace math +} // namespace cenet + +#endif // CENET_MATH_EXPR_HPP diff --git a/include/cenet/math/ops.hpp b/include/cenet/math/ops.hpp new file mode 100644 index 0000000..59665b2 --- /dev/null +++ b/include/cenet/math/ops.hpp @@ -0,0 +1,56 @@ +#ifndef CENET_MATH_OPS_HPP +#define CENET_MATH_OPS_HPP + +#include "tensor_traits.hpp" + +#include + +namespace cenet { +namespace math { +namespace ops { + +class plus { +private: + template + struct result_type_impl {}; + + template + struct result_type_impl, tensor_view> { + using type = + tensor>() + std::declval>()), Dims...>; + }; +public: + template + using result_type = typename result_type_impl::type; + + template + constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const { + return lhs + rhs.coeff(index); + } +}; + +class minus { +private: + template + struct result_type_impl {}; + + template + struct result_type_impl, tensor_view> { + using type = + tensor>() - std::declval>()), Dims...>; + }; +public: + template + using result_type = typename result_type_impl::type; + + template + constexpr auto operator()(std::size_t index, LT lhs, const R& rhs) const { + return lhs - rhs.coeff(index); + } +}; + +} // namespace ops +} // namespace math +} // namespace cenet + +#endif // CENET_MATH_OPS_HPP diff --git a/include/cenet/math/tensor.hpp b/include/cenet/math/tensor.hpp index 51b75dd..7905d5f 100644 --- a/include/cenet/math/tensor.hpp +++ b/include/cenet/math/tensor.hpp @@ -1,6 +1,7 @@ #ifndef CENET_MATH_TENSOR_HPP #define CENET_MATH_TENSOR_HPP +#include "expr.hpp" #include "tensor_fwd.hpp" #include "tensor_traits.hpp" @@ -159,13 +160,15 @@ public: template && ...))>> - explicit tensor(RtDims... dimensions) { + explicit tensor(RtDims... dimensions) + : m_shape(detail::dynamic_shape{}) { set_dimensions(dimensions...); } ~tensor() { release(); } - tensor(tensor&& other) noexcept { + tensor(tensor&& other) noexcept + : m_shape(std::move(other.m_shape)) { if constexpr (!traits::inline_storage) m_items = nullptr; *this = std::move(other); } @@ -174,13 +177,12 @@ public: if (this == &other) return *this; release(); m_items = other.m_items; - 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)); if constexpr (!traits::inline_storage) other.m_items = nullptr; return *this; } - tensor(const tensor& other) { + tensor(const tensor& other) + : m_shape(other.m_shape) { if constexpr (!traits::inline_storage) m_items = nullptr; *this = other; } @@ -195,9 +197,36 @@ public: return *this; } +public: + // TODO: Introduce another type trait for tensor compability + template && !is_tensor_v && + std::is_same_v::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]; + } + + for (std::size_t i = 0; i < view().size(); ++i) { + m_items[i] = std::forward(expr).eval(i); + } + } + + template && !is_tensor_v && + std::is_same_v::type>>> + tensor& operator=(E&& expr) { + for (std::size_t i = 0; i < view().size(); ++i) { + m_items[i] = std::forward(expr); + } + } public: view_type view() { return { m_items, m_shape }; } constant_view_type view() const { return { m_items, m_shape }; } + + explicit operator view_type() { return view(); } + explicit operator constant_view_type() const { return view(); } public: template {}); } + + auto dimensions() const { return dimensions_impl(std::make_index_sequence{}); } +private: + template + constexpr std::size_t size_impl(std::index_sequence) const { + return (dimension() * ... * 1); + } + + template + constexpr auto dimensions_impl(std::index_sequence) const { + return std::make_tuple(dimension()...); + } +public: + value_type& coeff(std::size_t index) { return m_items[index]; } + const value_type& coeff(std::size_t index) const { return m_items[index]; } + + value_type* data() { return m_items; } + const value_type* data() const { return m_items; } private: template constexpr bool valid_dims(std::index_sequence, RtDims... dimensions) const { @@ -226,6 +275,15 @@ private: constexpr void store_dims(std::index_sequence, RtDims... dimensions) { ((m_shape.dimensions[Is] = static_cast(dimensions)), ...); } +private: + template + constexpr std::size_t dimension() const { + if constexpr (traits::dynamic && traits::dimensions[I] == dynamic_extent) { + return m_shape.dimensions[I]; + } else { + return traits::dimensions[I]; + } + } private: template void dyn_alloc(std::index_sequence) { diff --git a/include/cenet/math/tensor_traits.hpp b/include/cenet/math/tensor_traits.hpp index 40765d8..46997cb 100644 --- a/include/cenet/math/tensor_traits.hpp +++ b/include/cenet/math/tensor_traits.hpp @@ -36,6 +36,19 @@ struct is_storage_inline> : std::true_type {}; template static constexpr auto is_storage_inline_v = is_storage_inline::value; +template +struct detach_storage { + using type = T; +}; + +template +struct detach_storage> { + using type = T; +}; + +template +using detach_storage_t = typename detach_storage::type; + template struct is_tensor : std::false_type {};