Skip to content

Commit 9dfdeea

Browse files
Alex-PLACETCopilot
andcommitted
Fix clip function behavior when a_min is greater than a_max
Co-authored-by: Copilot <copilot@github.com>
1 parent 1d158a9 commit 9dfdeea

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

include/xtensor/core/xmath.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,13 @@ namespace xt
606606
template <class A1, class A2, class A3>
607607
constexpr auto operator()(const A1& v, const A2& lo, const A3& hi) const
608608
{
609-
return xtl::select(v < lo, lo, xtl::select(hi < v, hi, v));
609+
return xtl::select(lo < hi, xtl::select(v < lo, lo, xtl::select(hi < v, hi, v)), hi);
610610
}
611611

612612
template <class A1, class A2, class A3>
613613
constexpr auto simd_apply(const A1& v, const A2& lo, const A3& hi) const
614614
{
615-
return xt_simd::select(v < lo, lo, xt_simd::select(hi < v, hi, v));
615+
return xt_simd::select(lo < hi, xt_simd::select(v < lo, lo, xt_simd::select(hi < v, hi, v)), hi);
616616
}
617617
};
618618

test/test_xmath.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ namespace xt
222222
EXPECT_EQ(res1, clip(opt_a, 2.0, 4.0));
223223
}
224224

225+
TEST(xmath, clip_amin_greater_than_amax)
226+
{
227+
// NumPy-compatible behavior: when a_min > a_max, all values
228+
// are set to a_max (the hi bound).
229+
const xarray<int> arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
230+
const xarray<int> expected = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
231+
EXPECT_EQ(expected, clip(arr, 8, 1));
232+
}
233+
225234
TEST(xmath, sign)
226235
{
227236
shape_type shape = {3, 2};

0 commit comments

Comments
 (0)