xsort

Defined in xtensor/xsort.hpp

template<class E>
auto xt::sort(const xexpression<E> &e, placeholders::xtuph)
template<class E>
auto xt::sort(const xexpression<E> &e, std::ptrdiff_t axis = -1)

Sort xexpression (optionally along axis) The sort is performed using the std::sort functions.

A copy of the xexpression is created and returned.

Return

sorted array (copy)

Parameters
  • e: xexpression to sort

  • axis: axis along which sort is performed

template<class E>
auto xt::argsort(const xexpression<E> &e, placeholders::xtuph)
template<class E>
auto xt::argsort(const xexpression<E> &e, std::ptrdiff_t axis = -1)

Argsort xexpression (optionally along axis) Performs an indirect sort along the given axis.

Returns an xarray of indices of the same shape as e that index data along the given axis in sorted order.

Return

argsorted index array

Parameters
  • e: xexpression to argsort

  • axis: axis along which argsort is performed

template<layout_type L = xt::layout_type::row_major, class E>
auto xt::argmin(const xexpression<E> &e)
template<layout_type L = xt::layout_type::row_major, class E>
auto xt::argmin(const xexpression<E> &e, std::ptrdiff_t axis)

Find position of minimal value in xexpression.

Return

returns xarray with positions of minimal value

Parameters
  • e: input xexpression

  • axis: select axis (or none)

template<layout_type L = xt::layout_type::row_major, class E>
auto xt::argmax(const xexpression<E> &e)
template<layout_type L = xt::layout_type::row_major, class E>
auto xt::argmax(const xexpression<E> &e, std::ptrdiff_t axis)

Find position of maximal value in xexpression.

Return

returns xarray with positions of maximal value

Parameters
  • e: input xexpression

  • axis: select axis (or none)

template<class E>
auto xt::unique(const xexpression<E> &e)

Find unique elements of a xexpression.

This returns a flattened xtensor with sorted, unique elements from the original expression.

Parameters
  • e: input xexpression (will be flattened)

template<class E, class C, class R = detail::flatten_sort_result_type_t<E>, class = std::enable_if_t<!xtl::is_integral<C>::value, int>>
R xt::partition(const xexpression<E> &e, const C &kth_container, placeholders::xtuph)

Partially sort xexpression.

Partition shuffles the xexpression in a way so that the kth element in the returned xexpression is in the place it would appear in a sorted array and all elements smaller than this entry are placed (unsorted) before.

The optional third parameter can either be an axis or xnone() in which case the xexpression will be flattened.

This function uses std::nth_element internally.

xt::xarray<float> a = {1, 10, -10, 123};
std::cout << xt::partition(a, 0) << std::endl; // {-10, 1, 123, 10} the correct entry at index 0
std::cout << xt::partition(a, 3) << std::endl; // {1, 10, -10, 123} the correct entry at index 3
std::cout << xt::partition(a, {0, 3}) << std::endl; // {-10, 1, 10, 123} the correct entries at index 0 and 3

Return

partially sorted xcontainer

Parameters
  • e: input xexpression

  • kth_container: a container of indices that should contain the correctly sorted value

  • axis: either integer (default = -1) to sort along last axis or xnone() to flatten before sorting

template<class E, class C, class R = typename detail::linear_argsort_result_type<typename detail::sort_eval_type<E>::type>::type, class = std::enable_if_t<!xtl::is_integral<C>::value, int>>
R xt::argpartition(const xexpression<E> &e, const C &kth_container, placeholders::xtuph)

Partially sort arguments.

Argpartition shuffles the indices to a xexpression in a way so that the index for the kth element in the returned xexpression is in the place it would appear in a sorted array and all elements smaller than this entry are placed (unsorted) before.

The optional third parameter can either be an axis or xnone() in which case the xexpression will be flattened.

This function uses std::nth_element internally.

xt::xarray<float> a = {1, 10, -10, 123};
std::cout << xt::argpartition(a, 0) << std::endl; // {2, 0, 3, 1} the correct entry at index 0
std::cout << xt::argpartition(a, 3) << std::endl; // {0, 1, 2, 3} the correct entry at index 3
std::cout << xt::argpartition(a, {0, 3}) << std::endl; // {2, 0, 1, 3} the correct entries at index 0 and 3

Return

xcontainer with indices of partial sort of input

Parameters
  • e: input xexpression

  • kth_container: a container of indices that should contain the correctly sorted value

  • axis: either integer (default = -1) to sort along last axis or xnone() to flatten before sorting

template<class E>
auto xt::median(E &&e, std::ptrdiff_t axis)

Find the median along the specified axis.

Given a vector V of length N, the median of V is the middle value of a sorted copy of V, V_sorted - i e., V_sorted[(N-1)/2], when N is odd, and the average of the two middle values of V_sorted when N is even.

Return

median value

Parameters
  • axis: axis along which the medians are computed. If not set, computes the median along a flattened version of the input.

  • e: input xexpression