xview

Defined in xtensor/xview.hpp

template<class CT, class ...S>
class xview : public xt::xview_semantic<xview<CT, S...>>, public std::conditional_t<detail::is_contiguous_view<std::decay_t<CT>, S...>::value, xcontiguous_iterable<xview<CT, S...>>, xiterable<xview<CT, S...>>>, public xt::xaccessible<xview<CT, S...>>, public extension::xview_base_t<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 also

view, range, all, newaxis, keep, drop

Template Parameters:
  • CT – the closure type of the xexpression to adapt

  • S – the slices type describing the shape adaptation

Constructor

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

Constructs a view on the specified xexpression.

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

See also

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

inline const inner_shape_type &shape() const noexcept

Returns the shape of the view.

inline const slice_type &slices() const noexcept

Returns the slices of the view.

inline layout_type layout() const noexcept

Returns the slices of the view.

Data

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

Fills the view with the given value.

Parameters:

value – the value to fill the view with.

inline xexpression_type &expression() noexcept

Returns a reference to the underlying expression of the view.

inline const xexpression_type &expression() const noexcept

Returns a const reference to the underlying expression of the view.

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

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

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

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

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 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 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 T>
inline auto storage() -> std::enable_if_t<has_data_interface<T>::value, storage_type&>

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>
inline auto strides() const -> std::enable_if_t<has_data_interface<T>::value && is_strided_view, const inner_strides_type&>

Return the strides for the underlying container of the view.

template<class T>
inline auto data() const -> std::enable_if_t<has_data_interface<T>::value && is_strided_view, const_pointer>

Return the pointer to the underlying buffer.

template<class T>
inline auto data_offset() const noexcept -> std::enable_if_t<has_data_interface<T>::value && is_strided_view, std::size_t>

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

Broadcasting

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

Broadcast the shape of the view 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 ST>
inline bool has_linear_assign(const ST &strides) const

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

Returns:

a boolean indicating whether a linear assign is possible

Extended copy semantic

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

The extended assignment operator.

template<class E, class ...S>
inline 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 also

range, all, newaxis

Parameters:
  • e – the xexpression to adapt

  • slices – the slices list describing the view. view accepts negative indices, in that case indexing is done in reverse order.

template<class E>
inline auto xt::row(E &&e, std::ptrdiff_t index)

Constructs and returns a row (sliced view) on the specified expression.

Users should not directly construct the slices but call helper functions instead. This function is only allowed on expressions with two dimensions.

Parameters:
  • e – the xexpression to adapt

  • index – 0-based index of the row, negative indices will return the last rows in reverse order.

Throws:

std::invalid_argument – if the expression has more than 2 dimensions.

template<class E>
inline auto xt::col(E &&e, std::ptrdiff_t index)

Constructs and returns a column (sliced view) on the specified expression.

Users should not directly construct the slices but call helper functions instead. This function is only allowed on expressions with two dimensions.

Parameters:
  • e – the xexpression to adapt

  • index – 0-based index of the column, negative indices will return the last columns in reverse order.

Throws:

std::invalid_argument – if the expression has more than 2 dimensions.

Defined in xtensor/xslice.hpp

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

Select a range from start_val to stop_val (excluded).

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 0 to 5 (excluded)
range(_, _)  // equivalent to `all()`

See also

view, strided_view

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

Select a range from start_val to stop_val (excluded) 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 also

view, strided_view

Warning

doxygenfunction: Unable to resolve function “xt::all” with arguments None in doxygen xml output for project “xtensor” from directory: ../xml. Potential matches:

- auto all() noexcept
- template<class E> bool all(E &&e)
inline auto xt::newaxis() noexcept

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

See also

view, strided_view

inline auto xt::ellipsis() noexcept

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 also

strided_view

template<class T>
inline detail::disable_integral_keep<T> xt::keep(T &&indices)

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

Note: this slice cannot be used in the xstrided_view!

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

indices – The indices container

Returns:

instance of xkeep_slice

template<class T>
inline detail::disable_integral_drop<T> xt::drop(T &&indices)

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

Note: this slice cannot be used in the xstrided_view!

xt::xarray<double> a = xt::arange(9);
a.reshape({3, 3});
xt::view(a, xt::drop(0, 2); // => {{3, 4, 5}}
Parameters:

indices – The container of indices to drop

Returns:

instance of xdrop_slice