@@ -2784,17 +2784,9 @@ array sort(const array& a, StreamOrDevice s /* = {} */) {
27842784
27852785/* * Returns a sorted copy of the array along a given axis. */
27862786array sort (const array& a, int axis, StreamOrDevice s /* = {} */ ) {
2787- // Check for valid axis
2788- if (axis + static_cast <int >(a.ndim ()) < 0 ||
2789- axis >= static_cast <int >(a.ndim ())) {
2790- std::ostringstream msg;
2791- msg << " [sort] Received invalid axis " << axis << " for array with "
2792- << a.ndim () << " dimensions." ;
2793- throw std::invalid_argument (msg.str ());
2794- }
2795-
2787+ auto ax = normalize_axis_index (axis, a.ndim (), " [sort] " );
27962788 return array (
2797- a.shape (), a.dtype (), std::make_shared<Sort>(to_stream (s), axis ), {a});
2789+ a.shape (), a.dtype (), std::make_shared<Sort>(to_stream (s), ax ), {a});
27982790}
27992791
28002792/* * Returns indices that sort the flattened array. */
@@ -2805,17 +2797,9 @@ array argsort(const array& a, StreamOrDevice s /* = {} */) {
28052797
28062798/* * Returns indices that sort the array along a given axis. */
28072799array argsort (const array& a, int axis, StreamOrDevice s /* = {} */ ) {
2808- // Check for valid axis
2809- if (axis + static_cast <int >(a.ndim ()) < 0 ||
2810- axis >= static_cast <int >(a.ndim ())) {
2811- std::ostringstream msg;
2812- msg << " [argsort] Received invalid axis " << axis << " for array with "
2813- << a.ndim () << " dimensions." ;
2814- throw std::invalid_argument (msg.str ());
2815- }
2816-
2800+ auto ax = normalize_axis_index (axis, a.ndim (), " [argsort] " );
28172801 return array (
2818- a.shape (), uint32, std::make_shared<ArgSort>(to_stream (s), axis ), {a});
2802+ a.shape (), uint32, std::make_shared<ArgSort>(to_stream (s), ax ), {a});
28192803}
28202804
28212805/* *
0 commit comments