/*************************************************************************** * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht * * Copyright (c) QuantStack * * * * Distributed under the terms of the BSD 3-Clause License. * * * * The full license is in the file LICENSE, distributed with this software. * ****************************************************************************/ #ifndef XTENSOR_BROADCAST_HPP #define XTENSOR_BROADCAST_HPP #include #include #include #include #include #include #include #include #include "xaccessible.hpp" #include "xexpression.hpp" #include "xiterable.hpp" #include "xscalar.hpp" #include "xstrides.hpp" #include "xtensor_config.hpp" #include "xutils.hpp" namespace xt { /************* * broadcast * *************/ template auto broadcast(E&& e, const S& s); template auto broadcast(E&& e, const I (&s)[L]); /************************* * xbroadcast extensions * *************************/ namespace extension { template struct xbroadcast_base_impl; template struct xbroadcast_base_impl { using type = xtensor_empty_base; }; template struct xbroadcast_base : xbroadcast_base_impl, CT, X> { }; template using xbroadcast_base_t = typename xbroadcast_base::type; } /************** * xbroadcast * **************/ template class xbroadcast; template struct xiterable_inner_types> { using xexpression_type = std::decay_t; using inner_shape_type = promote_shape_t; using const_stepper = typename xexpression_type::const_stepper; using stepper = const_stepper; }; template struct xcontainer_inner_types> { using xexpression_type = std::decay_t; using reference = typename xexpression_type::const_reference; using const_reference = typename xexpression_type::const_reference; using size_type = typename xexpression_type::size_type; }; /***************************** * linear_begin / linear_end * *****************************/ template XTENSOR_CONSTEXPR_RETURN auto linear_begin(xbroadcast& c) noexcept { return linear_begin(c.expression()); } template XTENSOR_CONSTEXPR_RETURN auto linear_end(xbroadcast& c) noexcept { return linear_end(c.expression()); } template XTENSOR_CONSTEXPR_RETURN auto linear_begin(const xbroadcast& c) noexcept { return linear_begin(c.expression()); } template XTENSOR_CONSTEXPR_RETURN auto linear_end(const xbroadcast& c) noexcept { return linear_end(c.expression()); } /************************************* * overlapping_memory_checker_traits * *************************************/ template struct overlapping_memory_checker_traits< E, std::enable_if_t::value && is_specialization_of::value>> { static bool check_overlap(const E& expr, const memory_range& dst_range) { if (expr.size() == 0) { return false; } else { using ChildE = std::decay_t; return overlapping_memory_checker_traits::check_overlap(expr.expression(), dst_range); } } }; /** * @class xbroadcast * @brief Broadcasted xexpression to a specified shape. * * The xbroadcast class implements the broadcasting of an \ref xexpression * to a specified shape. xbroadcast is not meant to be used directly, but * only with the \ref broadcast helper functions. * * @tparam CT the closure type of the \ref xexpression to broadcast * @tparam X the type of the specified shape. * * @sa broadcast */ template class xbroadcast : public xsharable_expression>, public xconst_iterable>, public xconst_accessible>, public extension::xbroadcast_base_t { public: using self_type = xbroadcast; using xexpression_type = std::decay_t; using accessible_base = xconst_accessible; using extension_base = extension::xbroadcast_base_t; using expression_tag = typename extension_base::expression_tag; using inner_types = xcontainer_inner_types; using value_type = typename xexpression_type::value_type; using reference = typename inner_types::reference; using const_reference = typename inner_types::const_reference; using pointer = typename xexpression_type::const_pointer; using const_pointer = typename xexpression_type::const_pointer; using size_type = typename inner_types::size_type; using difference_type = typename xexpression_type::difference_type; using iterable_base = xconst_iterable; using inner_shape_type = typename iterable_base::inner_shape_type; using shape_type = inner_shape_type; using stepper = typename iterable_base::stepper; using const_stepper = typename iterable_base::const_stepper; using bool_load_type = typename xexpression_type::bool_load_type; static constexpr layout_type static_layout = layout_type::dynamic; static constexpr bool contiguous_layout = false; template xbroadcast(CTA&& e, const S& s); template xbroadcast(CTA&& e, shape_type&& s); using accessible_base::size; const inner_shape_type& shape() const noexcept; layout_type layout() const noexcept; bool is_contiguous() const noexcept; using accessible_base::shape; template const_reference operator()(Args... args) const; template const_reference unchecked(Args... args) const; template const_reference element(It first, It last) const; const xexpression_type& expression() const noexcept; template bool broadcast_shape(S& shape, bool reuse_cache = false) const; template bool has_linear_assign(const S& strides) const noexcept; template const_stepper stepper_begin(const S& shape) const noexcept; template const_stepper stepper_end(const S& shape, layout_type l) const noexcept; template ::value>> void assign_to(xexpression& e) const; template using rebind_t = xbroadcast; template rebind_t build_broadcast(E&& e) const; private: CT m_e; inner_shape_type m_shape; }; /**************************** * broadcast implementation * ****************************/ /** * @brief Returns an \ref xexpression broadcasting the given expression to * a specified shape. * * @tparam e the \ref xexpression to broadcast * @tparam s the specified shape to broadcast. * * The returned expression either hold a const reference to \p e or a copy * depending on whether \p e is an lvalue or an rvalue. */ template inline auto broadcast(E&& e, const S& s) { using shape_type = filter_fixed_shape_t>; using broadcast_type = xbroadcast, shape_type>; return broadcast_type(std::forward(e), xtl::forward_sequence(s)); } template inline auto broadcast(E&& e, const I (&s)[L]) { using broadcast_type = xbroadcast, std::array>; using shape_type = typename broadcast_type::shape_type; return broadcast_type(std::forward(e), xtl::forward_sequence(s)); } /***************************** * xbroadcast implementation * *****************************/ /** * @name Constructor */ //@{ /** * Constructs an xbroadcast expression broadcasting the specified * \ref xexpression to the given shape * * @param e the expression to broadcast * @param s the shape to apply */ template template inline xbroadcast::xbroadcast(CTA&& e, const S& s) : m_e(std::forward(e)) { if (s.size() < m_e.dimension()) { XTENSOR_THROW(xt::broadcast_error, "Broadcast shape has fewer elements than original expression."); } xt::resize_container(m_shape, s.size()); std::copy(s.begin(), s.end(), m_shape.begin()); xt::broadcast_shape(m_e.shape(), m_shape); } /** * Constructs an xbroadcast expression broadcasting the specified * \ref xexpression to the given shape * * @param e the expression to broadcast * @param s the shape to apply */ template template inline xbroadcast::xbroadcast(CTA&& e, shape_type&& s) : m_e(std::forward(e)) , m_shape(std::move(s)) { xt::broadcast_shape(m_e.shape(), m_shape); } //@} /** * @name Size and shape */ //@{ /** * Returns the shape of the expression. */ template inline auto xbroadcast::shape() const noexcept -> const inner_shape_type& { return m_shape; } /** * Returns the layout_type of the expression. */ template inline layout_type xbroadcast::layout() const noexcept { return m_e.layout(); } template inline bool xbroadcast::is_contiguous() const noexcept { return false; } //@} /** * @name Data */ //@{ /** * Returns a constant reference to the element at the specified position in the expression. * @param args a list of indices specifying the position in the function. Indices * must be unsigned integers, the number of indices should be equal or greater than * the number of dimensions of the expression. */ template template inline auto xbroadcast::operator()(Args... args) const -> const_reference { return m_e(args...); } /** * Returns a constant reference to the element at the specified position in the expression. * @param args a list of indices specifying the position in the expression. Indices * must be unsigned integers, the number of indices must be equal to the number of * dimensions of the expression, else the behavior is undefined. * * @warning This method is meant for performance, for expressions with a dynamic * number of dimensions (i.e. not known at compile time). Since it may have * undefined behavior (see parameters), operator() should be preferred whenever * it is possible. * @warning This method is NOT compatible with broadcasting, meaning the following * code has undefined behavior: * @code{.cpp} * xt::xarray a = {{0, 1}, {2, 3}}; * xt::xarray b = {0, 1}; * auto fd = a + b; * double res = fd.uncheked(0, 1); * @endcode */ template template inline auto xbroadcast::unchecked(Args... args) const -> const_reference { return this->operator()(args...); } /** * Returns a constant reference to the element at the specified position in the expression. * @param first iterator starting the sequence of indices * @param last iterator ending the sequence of indices * The number of indices in the sequence should be equal to or greater * than the number of dimensions of the function. */ template template inline auto xbroadcast::element(It, It last) const -> const_reference { return m_e.element(last - this->dimension(), last); } /** * Returns a constant reference to the underlying expression of the broadcast expression. */ template inline auto xbroadcast::expression() const noexcept -> const xexpression_type& { return m_e; } //@} /** * @name Broadcasting */ //@{ /** * Broadcast the shape of the function to the specified parameter. * @param shape the result shape * @param reuse_cache parameter for internal optimization * @return a boolean indicating whether the broadcasting is trivial */ template template inline bool xbroadcast::broadcast_shape(S& shape, bool) const { return xt::broadcast_shape(m_shape, shape); } /** * Checks whether the xbroadcast can be linearly assigned to an expression * with the specified strides. * @return a boolean indicating whether a linear assign is possible */ template template inline bool xbroadcast::has_linear_assign(const S& strides) const noexcept { return this->dimension() == m_e.dimension() && std::equal(m_shape.cbegin(), m_shape.cend(), m_e.shape().cbegin()) && m_e.has_linear_assign(strides); } //@} template template inline auto xbroadcast::stepper_begin(const S& shape) const noexcept -> const_stepper { // Could check if (broadcastable(shape, m_shape) return m_e.stepper_begin(shape); } template template inline auto xbroadcast::stepper_end(const S& shape, layout_type l) const noexcept -> const_stepper { // Could check if (broadcastable(shape, m_shape) return m_e.stepper_end(shape, l); } template template inline void xbroadcast::assign_to(xexpression& e) const { auto& ed = e.derived_cast(); ed.resize(m_shape); std::fill(ed.begin(), ed.end(), m_e()); } template template inline auto xbroadcast::build_broadcast(E&& e) const -> rebind_t { return rebind_t(std::forward(e), inner_shape_type(m_shape)); } } #endif