Skip to content

Commit 1e958ea

Browse files
committed
flip: overload without axis (mimics NumPy)
1 parent 78aaac3 commit 1e958ea

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

include/xtensor/xmanipulation.hpp

+24-3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ namespace xt
7676
template <class E>
7777
auto vsplit(E& e, std::size_t n);
7878

79+
template <class E>
80+
auto flip(E&& e);
81+
7982
template <class E>
8083
auto flip(E&& e, std::size_t axis);
8184

@@ -589,7 +592,7 @@ namespace xt
589592
/**
590593
* @brief Split an xexpression into subexpressions horizontally (column-wise)
591594
*
592-
* This method is equivalent to ``split(e, n, 1)``.
595+
* This method is equivalent to ``split(e, n, 1)``.
593596
*
594597
* @param e input xexpression
595598
* @param n number of elements to return
@@ -618,6 +621,24 @@ namespace xt
618621
* flip implementation *
619622
***********************/
620623

624+
/**
625+
* @brief Reverse the order of elements in an xexpression along every axis.
626+
*
627+
* @param e the input xexpression
628+
*
629+
* @return returns a view with the result of the flip.
630+
*/
631+
template <class E>
632+
inline auto flip(E&& e)
633+
{
634+
using size_type = typename std::decay_t<E>::size_type;
635+
auto r = flip(e, 0);
636+
for (size_type d = 1; d < e.dimension(); ++d) {
637+
r = flip(r, d);
638+
}
639+
return r;
640+
}
641+
621642
/**
622643
* @brief Reverse the order of elements in an xexpression along the given axis.
623644
* Note: A NumPy/Matlab style `flipud(arr)` is equivalent to `xt::flip(arr, 0)`,
@@ -902,7 +923,7 @@ namespace xt
902923
* @brief Repeats elements of an expression along a given axis.
903924
*
904925
* @param e the input xexpression
905-
* @param repeats The number of repetition of each elements. The size of \ref repeats
926+
* @param repeats The number of repetition of each elements. The size of \ref repeats
906927
* must match the shape of the given \ref axis.
907928
* @param axis the axis along which to repeat the value
908929
*
@@ -918,7 +939,7 @@ namespace xt
918939
* @brief Repeats elements of an expression along a given axis.
919940
*
920941
* @param e the input xexpression
921-
* @param repeats The number of repetition of each elements. The size of \ref repeats
942+
* @param repeats The number of repetition of each elements. The size of \ref repeats
922943
* must match the shape of the given \ref axis.
923944
* @param axis the axis along which to repeat the value
924945
*

test/test_xmanipulation.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ namespace xt
153153
auto e = flatten<XTENSOR_DEFAULT_LAYOUT>(a);
154154
xtensor<double, 1> fl = e;
155155
using assign_traits = xassign_traits<decltype(fl), decltype(e)>;
156-
156+
157157
#if XTENSOR_USE_XSIMD
158158
EXPECT_TRUE(assign_traits::simd_linear_assign());
159159
EXPECT_TRUE(assign_traits::simd_linear_assign(fl, e));
@@ -397,6 +397,22 @@ namespace xt
397397
ASSERT_EQ(expected_2, ft);
398398
}
399399

400+
TEST(xmanipulation, flip_noaxis)
401+
{
402+
{
403+
xtensor<double, 1> e = {1, 2, 3, 4, 5, 6};
404+
xtensor<double, 1> t = xt::flip(e);
405+
xtensor<double, 1> expected = {6, 5, 4, 3, 2, 1};
406+
ASSERT_EQ(expected, t);
407+
}
408+
{
409+
xtensor<double, 2> e = {{1, 2, 3}, {4, 5, 6}};
410+
xtensor<double, 2> t = xt::flip(e);
411+
xtensor<double, 2> expected = {{6, 5, 4}, {3, 2, 1}};
412+
ASSERT_EQ(expected, t);
413+
}
414+
}
415+
400416
TEST(xmanipulation, rot90)
401417
{
402418
xarray<double> e = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

0 commit comments

Comments
 (0)