Skip to content

Commit

Permalink
Merge pull request xtensor-stack#2373 from tdegeus/flip
Browse files Browse the repository at this point in the history
flip: adding overload without axis (mimics NumPy)
  • Loading branch information
JohanMabille authored May 20, 2021
2 parents cbc61f3 + 1e958ea commit 6504ecd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
27 changes: 24 additions & 3 deletions include/xtensor/xmanipulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ namespace xt
template <class E>
auto vsplit(E& e, std::size_t n);

template <class E>
auto flip(E&& e);

template <class E>
auto flip(E&& e, std::size_t axis);

Expand Down Expand Up @@ -589,7 +592,7 @@ namespace xt
/**
* @brief Split an xexpression into subexpressions horizontally (column-wise)
*
* This method is equivalent to ``split(e, n, 1)``.
* This method is equivalent to ``split(e, n, 1)``.
*
* @param e input xexpression
* @param n number of elements to return
Expand Down Expand Up @@ -618,6 +621,24 @@ namespace xt
* flip implementation *
***********************/

/**
* @brief Reverse the order of elements in an xexpression along every axis.
*
* @param e the input xexpression
*
* @return returns a view with the result of the flip.
*/
template <class E>
inline auto flip(E&& e)
{
using size_type = typename std::decay_t<E>::size_type;
auto r = flip(e, 0);
for (size_type d = 1; d < e.dimension(); ++d) {
r = flip(r, d);
}
return r;
}

/**
* @brief Reverse the order of elements in an xexpression along the given axis.
* Note: A NumPy/Matlab style `flipud(arr)` is equivalent to `xt::flip(arr, 0)`,
Expand Down Expand Up @@ -902,7 +923,7 @@ namespace xt
* @brief Repeats elements of an expression along a given axis.
*
* @param e the input xexpression
* @param repeats The number of repetition of each elements. The size of \ref repeats
* @param repeats The number of repetition of each elements. The size of \ref repeats
* must match the shape of the given \ref axis.
* @param axis the axis along which to repeat the value
*
Expand All @@ -918,7 +939,7 @@ namespace xt
* @brief Repeats elements of an expression along a given axis.
*
* @param e the input xexpression
* @param repeats The number of repetition of each elements. The size of \ref repeats
* @param repeats The number of repetition of each elements. The size of \ref repeats
* must match the shape of the given \ref axis.
* @param axis the axis along which to repeat the value
*
Expand Down
18 changes: 17 additions & 1 deletion test/test_xmanipulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ namespace xt
auto e = flatten<XTENSOR_DEFAULT_LAYOUT>(a);
xtensor<double, 1> fl = e;
using assign_traits = xassign_traits<decltype(fl), decltype(e)>;

#if XTENSOR_USE_XSIMD
EXPECT_TRUE(assign_traits::simd_linear_assign());
EXPECT_TRUE(assign_traits::simd_linear_assign(fl, e));
Expand Down Expand Up @@ -397,6 +397,22 @@ namespace xt
ASSERT_EQ(expected_2, ft);
}

TEST(xmanipulation, flip_noaxis)
{
{
xtensor<double, 1> e = {1, 2, 3, 4, 5, 6};
xtensor<double, 1> t = xt::flip(e);
xtensor<double, 1> expected = {6, 5, 4, 3, 2, 1};
ASSERT_EQ(expected, t);
}
{
xtensor<double, 2> e = {{1, 2, 3}, {4, 5, 6}};
xtensor<double, 2> t = xt::flip(e);
xtensor<double, 2> expected = {{6, 5, 4}, {3, 2, 1}};
ASSERT_EQ(expected, t);
}
}

TEST(xmanipulation, rot90)
{
xarray<double> e = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Expand Down

0 comments on commit 6504ecd

Please sign in to comment.