Skip to content

Commit

Permalink
Fixing bug in argmin/argmax called with axis on rank-1 container (
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus authored Dec 5, 2023
1 parent 69eaac5 commit 7008d95
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ on:

jobs:


linux:

strategy:
Expand Down Expand Up @@ -86,7 +85,6 @@ jobs:
working-directory: build
run: ctest -R ^xtest$ --output-on-failure


macos:

strategy:
Expand Down
13 changes: 11 additions & 2 deletions include/xtensor/xsort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1173,8 +1173,17 @@ namespace xt
{
auto begin = e.template begin<L>();
auto end = e.template end<L>();
std::size_t i = static_cast<std::size_t>(std::distance(begin, std::min_element(begin, end)));
return xtensor<size_t, 0>{i};
// todo C++17 : constexpr
if (std::is_same<F, std::less<value_type>>::value)
{
std::size_t i = static_cast<std::size_t>(std::distance(begin, std::min_element(begin, end)));
return xtensor<size_t, 0>{i};
}
else
{
std::size_t i = static_cast<std::size_t>(std::distance(begin, std::max_element(begin, end)));
return xtensor<size_t, 0>{i};
}
}

result_shape_type alt_shape;
Expand Down
8 changes: 8 additions & 0 deletions test/test_xsort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ namespace xt
EXPECT_EQ(ex, argmin(xa));
EXPECT_EQ(ex_2, argmin(xa, 0));
EXPECT_EQ(ex_3, argmin(xa, 1));

xtensor<double, 1> ya = {1, 0, 3, 2, 2};
EXPECT_EQ(1, argmin(ya)());
EXPECT_EQ(1, argmin(ya, 0)());
}

TEST(xsort, argmax)
Expand Down Expand Up @@ -263,6 +267,10 @@ namespace xt
xtensor<std::size_t, 2> ex_6 = {{0, 0, 0, 0}, {0, 0, 0, 0}};
EXPECT_EQ(ex_6, argmax(c, 1));

xtensor<double, 1> ya = {1, 0, 3, 2, 2};
EXPECT_EQ(2, argmax(ya)());
EXPECT_EQ(2, argmax(ya, 0)());

// xtensor#2568
xarray<double> d = {0, 1, 0};
xtensor<size_t, 0> d_ex_1 = {1};
Expand Down

0 comments on commit 7008d95

Please sign in to comment.