From 68a21c5a5952913d6192be747efd245887bc36cc Mon Sep 17 00:00:00 2001 From: Alexis Placet <2400067+Alex-PLACET@users.noreply.github.com> Date: Thu, 30 Apr 2026 13:24:54 +0200 Subject: [PATCH 1/2] Fix substring calculation in CSV cell parsing and add lexical_cast for signed and unsigned char --- include/xtensor/io/xcsv.hpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/include/xtensor/io/xcsv.hpp b/include/xtensor/io/xcsv.hpp index 080ccaf54..2fd347745 100644 --- a/include/xtensor/io/xcsv.hpp +++ b/include/xtensor/io/xcsv.hpp @@ -66,7 +66,7 @@ namespace xt } size_t last = cell.find_last_not_of(' '); - return cell.substr(first, last == std::string::npos ? cell.size() : last + 1); + return cell.substr(first, last == std::string::npos ? cell.size() : last - first + 1); } template <> @@ -93,6 +93,18 @@ namespace xt return std::stoi(cell); } + template <> + inline signed char lexical_cast(const std::string& cell) + { + return static_cast(std::stoi(cell)); + } + + template <> + inline unsigned char lexical_cast(const std::string& cell) + { + return static_cast(std::stoul(cell)); + } + template <> inline long lexical_cast(const std::string& cell) { From 9dfdeea0c78bb10e3b076cbf0cd24e92d4da1fca Mon Sep 17 00:00:00 2001 From: Alexis Placet <2400067+Alex-PLACET@users.noreply.github.com> Date: Wed, 6 May 2026 09:29:02 +0200 Subject: [PATCH 2/2] Fix clip function behavior when a_min is greater than a_max Co-authored-by: Copilot --- include/xtensor/core/xmath.hpp | 4 ++-- test/test_xmath.cpp | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) 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};