From f2dd2230c4fdf1aa5c7a160782efdde18e8204bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20V=C3=B6r=C3=B6s?= Date: Mon, 23 Jan 2023 21:53:41 +0100 Subject: [PATCH] fix sorting of empty arrays in sort_complex (#583) --- code/numpy/carray/carray.c | 8 ++++++-- code/ulab.c | 2 +- docs/ulab-change-log.md | 6 ++++++ tests/2d/complex/sort_complex.py | 2 ++ tests/2d/complex/sort_complex.py.exp | 6 ++++++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/code/numpy/carray/carray.c b/code/numpy/carray/carray.c index a5f8a2b1..44e9ab12 100644 --- a/code/numpy/carray/carray.c +++ b/code/numpy/carray/carray.c @@ -191,8 +191,12 @@ mp_obj_t carray_sort_complex(mp_obj_t _source) { } ndarray_obj_t *ndarray = ndarray_copy_view_convert_type(source, NDARRAY_COMPLEX); - mp_float_t *array = (mp_float_t *)ndarray->array; - carray_sort_complex_(array, ndarray->len); + + if(ndarray->len != 0) { + mp_float_t *array = (mp_float_t *)ndarray->array; + carray_sort_complex_(array, ndarray->len); + } + return MP_OBJ_FROM_PTR(ndarray); } diff --git a/code/ulab.c b/code/ulab.c index 466920df..1bd83ce1 100644 --- a/code/ulab.c +++ b/code/ulab.c @@ -33,7 +33,7 @@ #include "user/user.h" #include "utils/utils.h" -#define ULAB_VERSION 6.0.6 +#define ULAB_VERSION 6.0.7 #define xstr(s) str(s) #define str(s) #s diff --git a/docs/ulab-change-log.md b/docs/ulab-change-log.md index ae3f161e..dc829940 100644 --- a/docs/ulab-change-log.md +++ b/docs/ulab-change-log.md @@ -6,6 +6,12 @@ version 6.0.6 Sun, 21 Jan 2023 +version 6.0.7 + + treat empty arrays in sort_complex correctly + +Sun, 21 Jan 2023 + version 6.0.5 fix ones()/zeros() method when the amount of memory to allocate overflows diff --git a/tests/2d/complex/sort_complex.py b/tests/2d/complex/sort_complex.py index 1ac1edc1..a4154730 100644 --- a/tests/2d/complex/sort_complex.py +++ b/tests/2d/complex/sort_complex.py @@ -7,6 +7,8 @@ for dtype in dtypes: print(np.sort_complex(np.array(range(5, 0, -1), dtype=dtype))) + # these should all return an empty complex array + print(np.sort_complex(np.array(range(5, 0, 1), dtype=dtype))) print() n = 6 diff --git a/tests/2d/complex/sort_complex.py.exp b/tests/2d/complex/sort_complex.py.exp index 9026e4ae..fd2f56d0 100644 --- a/tests/2d/complex/sort_complex.py.exp +++ b/tests/2d/complex/sort_complex.py.exp @@ -1,9 +1,15 @@ array([1.0+0.0j, 2.0+0.0j, 3.0+0.0j, 4.0+0.0j, 5.0+0.0j], dtype=complex) +array([], dtype=complex) array([1.0+0.0j, 2.0+0.0j, 3.0+0.0j, 4.0+0.0j, 5.0+0.0j], dtype=complex) +array([], dtype=complex) array([1.0+0.0j, 2.0+0.0j, 3.0+0.0j, 4.0+0.0j, 5.0+0.0j], dtype=complex) +array([], dtype=complex) array([1.0+0.0j, 2.0+0.0j, 3.0+0.0j, 4.0+0.0j, 5.0+0.0j], dtype=complex) +array([], dtype=complex) array([1.0+0.0j, 2.0+0.0j, 3.0+0.0j, 4.0+0.0j, 5.0+0.0j], dtype=complex) +array([], dtype=complex) array([1.0+0.0j, 2.0+0.0j, 3.0+0.0j, 4.0+0.0j, 5.0+0.0j], dtype=complex) +array([], dtype=complex) array([1.0+1.0j, 1.0+2.0j, 1.0+3.0j, 1.0+4.0j, 1.0+5.0j, 1.0+6.0j], dtype=complex) array([1.0+0.0j, 1.0+1.0j, 1.0+2.0j, 1.0+3.0j, 1.0+4.0j, 1.0+5.0j], dtype=complex)