Skip to content

Commit 7008d95

Browse files
authored
Fixing bug in argmin/argmax called with axis on rank-1 container (#2753)
1 parent 69eaac5 commit 7008d95

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ on:
88

99
jobs:
1010

11-
1211
linux:
1312

1413
strategy:
@@ -86,7 +85,6 @@ jobs:
8685
working-directory: build
8786
run: ctest -R ^xtest$ --output-on-failure
8887

89-
9088
macos:
9189

9290
strategy:

include/xtensor/xsort.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,8 +1173,17 @@ namespace xt
11731173
{
11741174
auto begin = e.template begin<L>();
11751175
auto end = e.template end<L>();
1176-
std::size_t i = static_cast<std::size_t>(std::distance(begin, std::min_element(begin, end)));
1177-
return xtensor<size_t, 0>{i};
1176+
// todo C++17 : constexpr
1177+
if (std::is_same<F, std::less<value_type>>::value)
1178+
{
1179+
std::size_t i = static_cast<std::size_t>(std::distance(begin, std::min_element(begin, end)));
1180+
return xtensor<size_t, 0>{i};
1181+
}
1182+
else
1183+
{
1184+
std::size_t i = static_cast<std::size_t>(std::distance(begin, std::max_element(begin, end)));
1185+
return xtensor<size_t, 0>{i};
1186+
}
11781187
}
11791188

11801189
result_shape_type alt_shape;

test/test_xsort.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ namespace xt
235235
EXPECT_EQ(ex, argmin(xa));
236236
EXPECT_EQ(ex_2, argmin(xa, 0));
237237
EXPECT_EQ(ex_3, argmin(xa, 1));
238+
239+
xtensor<double, 1> ya = {1, 0, 3, 2, 2};
240+
EXPECT_EQ(1, argmin(ya)());
241+
EXPECT_EQ(1, argmin(ya, 0)());
238242
}
239243

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

270+
xtensor<double, 1> ya = {1, 0, 3, 2, 2};
271+
EXPECT_EQ(2, argmax(ya)());
272+
EXPECT_EQ(2, argmax(ya, 0)());
273+
266274
// xtensor#2568
267275
xarray<double> d = {0, 1, 0};
268276
xtensor<size_t, 0> d_ex_1 = {1};

0 commit comments

Comments
 (0)