# xbroadcast

Defined in `xtensor/xbroadcast.hpp`

template<class CT, class X>
class xbroadcast : public xt::xsharable_expression<xbroadcast<CT, X>>, public xt::xconst_iterable<xbroadcast<CT, X>>, public xt::xconst_accessible<xbroadcast<CT, X>>, public extension::xbroadcast_base_t<CT, X>

  Broadcasted xexpression to a specified shape.

  The xbroadcast class implements the broadcasting of an xexpression to a specified shape. xbroadcast is not meant to be used directly, but only with the broadcast helper functions.

  See also

  broadcast

  Template Parameters:

    - CT – the closure type of the xexpression to broadcast

    - X – the type of the specified shape.

  Constructor

  template<class CTA, class S>
  inline xbroadcast(CTA &&e, const S &s)

    Constructs an xbroadcast expression broadcasting the specified xexpression to the given shape.

    Parameters:

      - e – the expression to broadcast

      - s – the shape to apply

  template<class CTA>
  inline xbroadcast(CTA &&e, shape_type &&s)

    Constructs an xbroadcast expression broadcasting the specified xexpression to the given shape.

    Parameters:

      - e – the expression to broadcast

      - s – the shape to apply

  Size and shape

  inline const inner_shape_type &shape() const noexcept

    Returns the shape of the expression.

  inline layout_type layout() const noexcept

    Returns the layout_type of the expression.

  Data

  inline const xexpression_type &expression() const noexcept

    Returns a constant reference to the underlying expression of the broadcast expression.

  template<class ...Args>
  inline auto operator()(Args... args) const -> const_reference

    Returns a constant reference to the element at the specified position in the expression.

    Parameters:

      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<class ...Args>
  inline auto unchecked(Args... args) const -> const_reference

    Returns a constant reference to the element at the specified position in the expression.

    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:

        xt::xarray<double> a = {{0, 1}, {2, 3}};
        xt::xarray<double> b = {0, 1};
        auto fd = a + b;
        double res = fd.uncheked(0, 1);

    Parameters:

      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.

  template<class It>
  inline auto element(It, It last) const -> const_reference

    Returns a constant reference to the element at the specified position in the expression.

    Parameters:

      - first – iterator starting the sequence of indices

      - 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.

  Broadcasting

  template<class S>
  inline bool broadcast_shape(S &shape, bool reuse_cache = false) const

    Broadcast the shape of the function to the specified parameter.

    Parameters:

      - shape – the result shape

      - reuse_cache – parameter for internal optimization

    Returns:

      a boolean indicating whether the broadcasting is trivial

  template<class S>
  inline bool has_linear_assign(const S &strides) const noexcept

    Checks whether the xbroadcast can be linearly assigned to an expression with the specified strides.

    Returns:

      a boolean indicating whether a linear assign is possible

  Public Functions

  inline size_type size() const noexcept

    Returns the size of the expression.

  inline size_type shape(size_type index) const

    Returns the i-th dimension of the expression.

template<class E, class S>
inline auto xt::broadcast(E &&e, const S &s)

  Returns an xexpression broadcasting the given expression to a specified shape.

  The returned expression either hold a const reference to `e` or a copy depending on whether `e` is an lvalue or an rvalue.

  Template Parameters:

    - e – the xexpression to broadcast

    - s – the specified shape to broadcast.