xview

Defined in xtensor/xview.hpp

template <class CT, class... S>
class xview : public xt::xview_semantic<xview<CT, S...>>, public xt::xiterable<xview<CT, S...>>

Multidimensional view with tensor semantic.

The xview class implements a multidimensional view with tensor semantic. It is used to adapt the shape of an xexpression without changing it. xview is not meant to be used directly, but only with the view helper functions.

See
view, range, all, newaxis
Template Parameters
  • CT: the closure type of the xexpression to adapt
  • S: the slices type describing the shape adaptation

Extended copy semantic

template <class E>
auto operator=(const xexpression<E> &e)

The extended assignment operator.

Constructor

template <class CTA, class FSL, class... SL>
xview(CTA &&e, FSL &&first_slice, SL&&... slices)

Constructs a view on the specified xexpression.

Users should not call directly this constructor but use the view function instead.

See
view
Parameters
  • e: the xexpression to adapt
  • first_slice: the first slice describing the view
  • slices: the slices list describing the view

Size and shape

auto dimension() const

Returns the number of dimensions of the view.

auto size() const

Returns the size of the expression.

auto shape() const

Returns the shape of the view.

auto slices() const

Returns the slices of the view.

layout_type layout() const

Returns the slices of the view.

Data

template <class T>
void fill(const T &value)

Fills the view with the given value.

Parameters
  • value: the value to fill the view with.

template <class... Args>
auto operator()(Args... args)

Returns a reference to the element at the specified position in the view.

Parameters
  • args: a list of indices specifying the position in the view. Indices must be unsigned integers, the number of indices should be equal or greater than the number of dimensions of the view.

template <class... Args>
auto at(Args... args)

Returns a 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 function. 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 unchecked(Args... args)

Returns a reference to the element at the specified position in the view.

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 prefered 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 view. Indices must be unsigned integers, the number of indices must be equal to the number of dimensions of the view, else the behavior is undefined.

template <class OS>
auto operator[](const OS &index)

Returns a reference to the element at the specified position in the view.

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

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

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

Parameters
  • args: a list of indices specifying the position in the view. Indices must be unsigned integers, the number of indices should be equal or greater than the number of dimensions of the view.

template <class... Args>
auto at(Args... args) const

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

Parameters
  • args: a list of indices specifying the position in the function. 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 unchecked(Args... args) const

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

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 prefered 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 view. Indices must be unsigned integers, the number of indices must be equal to the number of dimensions of the view, else the behavior is undefined.

template <class OS>
auto operator[](const OS &index) const

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

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

template <class T>
auto storage()

Returns the data holder of the underlying container (only if the view is on a realized container).

xt::eval will make sure that the underlying xexpression is on a realized container.

template <class T>
auto strides() const

Return the strides for the underlying container of the view.

template <class T>
auto data() const

Return the pointer to the underlying buffer.

template <class T>
auto data_offset() const

Return the offset to the first element of the view in the underlying container.

Broadcasting

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

Broadcast the shape of the view to the specified parameter.

Return
a boolean indicating whether the broadcasting is trivial
Parameters
  • shape: the result shape
  • reuse_cache: parameter for internal optimization

template <class ST>
bool is_trivial_broadcast(const ST &strides) const

Compares the specified strides with those of the view to see whether the broadcasting is trivial.

Return
a boolean indicating whether the broadcasting is trivial

template <class E, class... S>
auto xt::view(E &&e, S&&... slices)

Constructs and returns a view on the specified xexpression.

Users should not directly construct the slices but call helper functions instead.

See
range, all, newaxis
Parameters
  • e: the xexpression to adapt
  • slices: the slices list describing the view

Defined in xtensor/xslice.hpp

template <class A, class B>
auto xt::range(A start_val, B stop_val)

Select a range from start_val to stop_val.

You can use the shorthand _ syntax to select from the start or until the end.

using namespace xt::placeholders;  // to enable _ syntax

range(3, _)  // select from index 3 to the end
range(_, 5)  // select from index o to 5
range(_, _)  // equivalent to `all()`

See
view, strided_view

template <class A, class B, class C>
auto xt::range(A start_val, B stop_val, C step)

Select a range from start_val to stop_val with step You can use the shorthand _ syntax to select from the start or until the end.

using namespace xt::placeholders;  // to enable _ syntax
range(3, _, 5)  // select from index 3 to the end with stepsize 5

See
view, strided_view

auto xt::all()

Returns a slice representing a full dimension, to be used as an argument of view function.

See
view, strided_view

auto xt::newaxis()

Returns a slice representing a new axis of length one, to be used as an argument of view function.

See
view, strided_view

auto xt::ellipsis()

Returns a slice representing all remaining dimensions, and selecting all in these dimensions.

Ellipsis will expand to a series of all() slices, until the number of slices is equal to the number of dimensions of the source array.

Note: ellipsis can only be used in strided_view!

xarray<double> a = xarray<double>::from_shape({5, 5, 1, 1, 5});
auto v = xt::strided_view(a, {2, xt::ellipsis(), 2});
// equivalent to using {2, xt::all(), xt::all(), xt::all(), 2};

See
strided_view

template <class T>
auto xt::islice(T &&indices)

Create a non-contigous slice from a container of indices.

Note: this slice can only be used in the xview!

xt::xarray<double> a = xt::arange(9);
a.reshape({3, 3});
xt::view(a, xt::islice({0, 2}); // => {{0, 1, 2}, {6, 7, 8}}
xt::view(a, xt::islice({1, 1, 1}); // => {{3, 4, 5}, {3, 4, 5}, {3, 4, 5}}

Return
instance of xislice
Parameters
  • indices: The indices container