# xgenerator

Defined in `xtensor/generators/xgenerator.hpp`

template<class F, class R, class S>
class xgenerator : public xt::xsharable_expression<xgenerator<F, R, S>>, public xt::xconst_iterable<xgenerator<F, R, S>>, public xt::xconst_accessible<xgenerator<F, R, S>>, public extension::xgenerator_base_t<F, R, S>

  Multidimensional function operating on indices.

  The xgenerator class implements a multidimensional function, generating a value from the supplied indices.

  Template Parameters:

    - F – the function type

    - R – the return type of the function

    - S – the shape type of the generator

  Constructor

  template<class Func>
  inline xgenerator(Func &&f, const S &shape) noexcept

    Constructs an xgenerator applying the specified function over the given shape.

    Parameters:

      - f – the function to apply

      - shape – the shape of the xgenerator

  Size and shape

  inline const inner_shape_type &shape() const noexcept

    Returns the shape of the xgenerator.

  Broadcasting

  template<class O>
  inline bool broadcast_shape(O &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 O>
  inline bool has_linear_assign(const O&) const noexcept

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

    Returns:

      a boolean indicating whether a linear assign is possible

  template<class O = xt::dynamic_shape<typename shape_type::value_type>>
  inline auto reshape(O &&shape) const &

    Reshapes the generator and keeps old elements.

    The `shape` argument can have one of its value equal to `-1`, in this case the value is inferred from the number of elements in the generator and the remaining values in the `shape`.

        auto a = xt::arange<double>(50).reshape({-1, 10});
        //a.shape() is {5, 10}

    Parameters:

      shape – the new shape (has to have same number of elements as the original generator)

  Data

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

    Returns the evaluated element at the specified position in the function.

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

  template<class ...Args>
  inline const_reference unchecked(Args... args) const

    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 const_reference element(It first, It last) const

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

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

  Public Functions

  size_type shape(size_type index) const

    Returns the i-th dimension of the expression.