xfunction¶
Defined in xtensor/xfunction.hpp
- template <class F, class… CT>
-
class
xt::xfunction¶ Multidimensional function operating on xtensor expressions.
The xfunction class implements a multidimensional function operating on xtensor expressions.
- Template Parameters
F: the function typeCT: the closure types for arguments of the function
Inherits from xt::xconst_iterable< xfunction< F, CT… > >, xt::xsharable_expression< xfunction< F, CT… > >, xt::xconst_accessible< xfunction< F, CT… > >, extension::xfunction_base_t< F, CT… >
Constructor
- template <class Func, class… CTA, class U = std::enable_if_t<!std::is_base_of<std::decay_t<Func>, self_type>::value>>
-
xfunction(Func &&f, CTA&&... e)¶ Constructs an xfunction applying the specified function to the given arguments.
- Parameters
f: the function to applye: the xexpression arguments
Size and shape
-
auto
dimension() const¶ Returns the number of dimensions of the function.
-
auto
shape() const¶ Returns the shape of the xfunction.
-
layout_type
layout() const¶ Returns the layout_type of the xfunction.
Broadcasting
- template <class S>
-
bool
broadcast_shape(S &shape, bool reuse_cache = false) const¶ Broadcast the shape of the function to the specified parameter.
- Return
- a boolean indicating whether the broadcasting is trivial
- Parameters
shape: the result shapereuse_cache: boolean for reusing a previously computed shape
- template <class S>
-
bool
has_linear_assign(const S &strides) const¶ Checks whether the xfunction can be linearly assigned to an expression with the specified strides.
- Return
- a boolean indicating whether a linear assign is possible
Data
- template <class… Args>
-
auto
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>
-
auto
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>
-
auto
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 indiceslast: 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
-
auto
size() const Returns the size of the expression.
-
auto
shape(size_type index) const Returns the i-th dimension of the expression.
- template <class… Args>
-
auto
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.
- Exceptions
std::out_of_range: if the number of argument is greater than the number of dimensions or if indices are out of bounds.
- template <class… Args>
-
auto
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.
- template <class… Args>
-
bool
in_bounds(Args... args) const¶ Returns
trueonly if the the specified position is a valid entry in the expression.- Return
- bool
- Parameters
args: a list of indices specifying the position in the expression.
Defined in xtensor/xmath.hpp
- template <class F, class… E>
-
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).
autolambda functions are automatically vectorized withxsimdif possible (note that the trailing-> decltype(...)is mandatory for the feature detection to work).- Return
- lazy xfunction
- Parameters
lambda: the lambda to be vectorizedargs: forwarded arguments