diff --git a/include/xtensor/core/xmath.hpp b/include/xtensor/core/xmath.hpp index 315e13a82..827e84219 100644 --- a/include/xtensor/core/xmath.hpp +++ b/include/xtensor/core/xmath.hpp @@ -606,13 +606,13 @@ namespace xt template constexpr auto operator()(const A1& v, const A2& lo, const A3& hi) const { - return xtl::select(v < lo, lo, xtl::select(hi < v, hi, v)); + return xtl::select(lo < hi, xtl::select(v < lo, lo, xtl::select(hi < v, hi, v)), hi); } template constexpr auto simd_apply(const A1& v, const A2& lo, const A3& hi) const { - return xt_simd::select(v < lo, lo, xt_simd::select(hi < v, hi, v)); + return xt_simd::select(lo < hi, xt_simd::select(v < lo, lo, xt_simd::select(hi < v, hi, v)), hi); } }; diff --git a/test/test_xmath.cpp b/test/test_xmath.cpp index 8f56e93b9..57771859b 100644 --- a/test/test_xmath.cpp +++ b/test/test_xmath.cpp @@ -222,6 +222,15 @@ namespace xt EXPECT_EQ(res1, clip(opt_a, 2.0, 4.0)); } + TEST(xmath, clip_amin_greater_than_amax) + { + // NumPy-compatible behavior: when a_min > a_max, all values + // are set to a_max (the hi bound). + const xarray arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + const xarray expected = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + EXPECT_EQ(expected, clip(arr, 8, 1)); + } + TEST(xmath, sign) { shape_type shape = {3, 2};