xfunction

Defined in xtensor/core/xfunction.hpp

template<class F, class ...CT>
class xfunction : private xt::xconst_iterable<xfunction<F, CT...>>, public xt::xsharable_expression<xfunction<F, CT...>>, private xt::xconst_accessible<xfunction<F, CT...>>, public extension::xfunction_base_t<F, CT...>

Multidimensional function operating on xtensor expressions.

The xfunction class implements a multidimensional function operating on xtensor expressions.

Template Parameters:
  • F – the function type

  • CT – the closure types for arguments of the function

Constructor

template<class Func, class ...CTA, class U = std::enable_if_t<!std::is_base_of<std::decay_t<Func>, self_type>::value>>
inline xfunction(Func &&f, CTA&&... e) noexcept

Constructs an xfunction applying the specified function to the given arguments.

Parameters:
  • f – the function to apply

  • e – the xexpression arguments

template<class FA, class ...CTA>
inline xfunction(xfunction<FA, CTA...> xf) noexcept

Constructs an xfunction applying the specified function given by another xfunction with its arguments.

Parameters:

xf – the xfunction to apply

Size and shape

inline size_type dimension() const noexcept

Returns the number of dimensions of the function.

inline const inner_shape_type &shape() const

Returns the shape of the xfunction.

inline layout_type layout() const noexcept

Returns the layout_type of the xfunction.

Data

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

Returns a constant reference to the 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.unchecked(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.

inline const_reference flat(size_type i) const

Returns a constant reference to the element at the specified position of the underlying contiguous storage of the function.

Parameters:

index – index to underlying flat storage.

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 – boolean for reusing a previously computed shape

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 xfunction can be linearly assigned to an expression with the specified strides.

Returns:

a boolean indicating whether a linear assign is possible

Public Functions

size_type size() const noexcept

Returns the size of the expression.

size_type shape(size_type index) const

Returns the i-th dimension of the expression.

const_reference at(Args... args) const

Returns a constant reference to the element at the specified position in the expression, after dimension and bounds checking.

Parameters:

args – a list of indices specifying the position in the expression. Indices must be unsigned integers, the number of indices should be equal to the number of dimensions of the expression.

Throws:

std::out_of_range – if the number of argument is greater than the number of dimensions or if indices are out of bounds.

disable_integral_t<S, const_reference> operator[](const S &index) const

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

Parameters:

index – a sequence of indices specifying the position in the expression. Indices must be unsigned integers, the number of indices in the list should be equal or greater than the number of dimensions of the expression.

const_reference back() const

Returns a constant reference to last the element of the expression.

const_reference front() const

Returns a constant reference to first the element of the expression.

bool in_bounds(Args... args) const

Returns true only if the the specified position is a valid entry in the expression.

Parameters:

args – a list of indices specifying the position in the expression.

Returns:

bool

const_reference periodic(Args... args) const

Returns a constant reference to the element at the specified position in the expression, after applying periodicity to the indices (negative and ‘overflowing’ indices are changed).

Parameters:

args – a list of indices specifying the position in the expression. Indices must be integers, the number of indices should be equal to the number of dimensions of the expression.

const_layout_iterator<L> begin() const noexcept

Returns a constant iterator to the first element of the expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_broadcast_iterator<S, L> begin(const S &shape) const noexcept

Returns a constant iterator to the first element of the expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_layout_iterator<L> cbegin() const noexcept

Returns a constant iterator to the first element of the expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_broadcast_iterator<S, L> cbegin(const S &shape) const noexcept

Returns a constant iterator to the first element of the expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_layout_iterator<L> cend() const noexcept

Returns a constant iterator to the element following the last element of the expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_broadcast_iterator<S, L> cend(const S &shape) const noexcept

Returns a constant iterator to the element following the last element of the expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_reverse_layout_iterator<L> crbegin() const noexcept

Returns a constant iterator to the first element of the reversed expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_reverse_broadcast_iterator<S, L> crbegin(const S &shape) const noexcept

Returns a constant iterator to the first element of the reversed expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_reverse_layout_iterator<L> crend() const noexcept

Returns a constant iterator to the element following the last element of the reversed expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_reverse_broadcast_iterator<S, L> crend(const S &shape) const noexcept

Returns a constant iterator to the element following the last element of the reversed expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_layout_iterator<L> end() const noexcept

Returns a constant iterator to the element following the last element of the expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_broadcast_iterator<S, L> end(const S &shape) const noexcept

Returns a constant iterator to the element following the last element of the expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_reverse_layout_iterator<L> rbegin() const noexcept

Returns a constant iterator to the first element of the reversed expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_reverse_broadcast_iterator<S, L> rbegin(const S &shape) const noexcept

Returns a constant iterator to the first element of the reversed expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_reverse_layout_iterator<L> rend() const noexcept

Returns a constant iterator to the element following the last element of the reversed expression.

Template Parameters:

L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

const_reverse_broadcast_iterator<S, L> rend(const S &shape) const noexcept

Returns a constant iterator to the element following the last element of the reversed expression.

The iteration is broadcasted to the specified shape.

Parameters:

shape – the shape used for broadcasting

Template Parameters:
  • S – type of the shape parameter.

  • L – order used for the traversal. Default value is XTENSOR_DEFAULT_TRAVERSAL.

Defined in xtensor/core/xmath.hpp

template<class F, class ...E>
inline auto xt::make_lambda_xfunction(F &&lambda, E&&... args)

Create a xfunction from a lambda.

This function can be used to easily create performant xfunctions from lambdas:

template <class E1>
inline auto square(E1&& e1) noexcept
{
    auto fnct = [](auto x) -> decltype(x * x) {
        return x * x;
    };
    return make_lambda_xfunction(std::move(fnct), std::forward<E1>(e1));
}

Lambda function allow the reusal of a single arguments in multiple places (otherwise only correctly possible when using xshared_expressions). auto lambda functions are automatically vectorized with xsimd if possible (note that the trailing -> decltype(...) is mandatory for the feature detection to work).

Parameters:
  • lambda – the lambda to be vectorized

  • args – forwarded arguments

Returns:

lazy xfunction