Skip to content

Commit 2b6e0cf

Browse files
Fixed signed number sorting Issue 744 (#750)
* Fixed signed number sorting Issue 744 * Incremented version number
1 parent d161443 commit 2b6e0cf

6 files changed

Lines changed: 175 additions & 37 deletions

File tree

code/numpy/numerical.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,9 +653,13 @@ static mp_obj_t numerical_sort_helper(mp_obj_t oin, mp_obj_t axis, uint8_t inpla
653653

654654
uint8_t *array = (uint8_t *)ndarray->array;
655655
if(ndarray->shape[ax]) {
656-
if((ndarray->dtype == NDARRAY_UINT8) || (ndarray->dtype == NDARRAY_INT8)) {
656+
if(ndarray->dtype == NDARRAY_INT8) {
657+
HEAPSORT(ndarray, int8_t, array, shape, strides, ax, increment, ndarray->shape[ax]);
658+
} else if(ndarray->dtype == NDARRAY_UINT8) {
657659
HEAPSORT(ndarray, uint8_t, array, shape, strides, ax, increment, ndarray->shape[ax]);
658-
} else if((ndarray->dtype == NDARRAY_UINT16) || (ndarray->dtype == NDARRAY_INT16)) {
660+
} else if(ndarray->dtype == NDARRAY_INT16) {
661+
HEAPSORT(ndarray, int16_t, array, shape, strides, ax, increment, ndarray->shape[ax]);
662+
} else if(ndarray->dtype == NDARRAY_UINT16) {
659663
HEAPSORT(ndarray, uint16_t, array, shape, strides, ax, increment, ndarray->shape[ax]);
660664
} else {
661665
HEAPSORT(ndarray, mp_float_t, array, shape, strides, ax, increment, ndarray->shape[ax]);
@@ -803,9 +807,13 @@ mp_obj_t numerical_argsort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
803807
iarray = indices->array;
804808

805809
if(ndarray->shape[ax]) {
806-
if((ndarray->dtype == NDARRAY_UINT8) || (ndarray->dtype == NDARRAY_INT8)) {
810+
if(ndarray->dtype == NDARRAY_INT8) {
811+
HEAP_ARGSORT(ndarray, int8_t, array, shape, strides, ax, increment, ndarray->shape[ax], iarray, istrides, iincrement);
812+
} else if(ndarray->dtype == NDARRAY_UINT8) {
807813
HEAP_ARGSORT(ndarray, uint8_t, array, shape, strides, ax, increment, ndarray->shape[ax], iarray, istrides, iincrement);
808-
} else if((ndarray->dtype == NDARRAY_UINT16) || (ndarray->dtype == NDARRAY_INT16)) {
814+
} else if(ndarray->dtype == NDARRAY_INT16) {
815+
HEAP_ARGSORT(ndarray, int16_t, array, shape, strides, ax, increment, ndarray->shape[ax], iarray, istrides, iincrement);
816+
} else if(ndarray->dtype == NDARRAY_UINT16) {
809817
HEAP_ARGSORT(ndarray, uint16_t, array, shape, strides, ax, increment, ndarray->shape[ax], iarray, istrides, iincrement);
810818
} else {
811819
HEAP_ARGSORT(ndarray, mp_float_t, array, shape, strides, ax, increment, ndarray->shape[ax], iarray, istrides, iincrement);

code/ulab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "user/user.h"
3434
#include "utils/utils.h"
3535

36-
#define ULAB_VERSION 6.12.0
36+
#define ULAB_VERSION 6.12.1
3737
#define xstr(s) str(s)
3838
#define str(s) #s
3939

tests/1d/numpy/sort.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
try:
2+
from ulab import numpy as np
3+
except:
4+
import numpy as np
5+
6+
# sort and argsort over all dtypes, positive values
7+
for dtype in (np.uint8, np.int8, np.uint16, np.int16, np.float):
8+
print()
9+
a = np.array([], dtype=dtype)
10+
print(np.sort(a, axis=0))
11+
print(list(np.argsort(a, axis=0)))
12+
13+
a = np.array([4, 1, 3, 2], dtype=dtype)
14+
print(np.sort(a, axis=0))
15+
print(list(np.argsort(a, axis=0)))
16+
17+
# sort and argsort with negative values for signed and float dtypes
18+
for dtype in (np.int8, np.int16, np.float):
19+
print()
20+
a = np.array([-3, 1, -2, 0], dtype=dtype)
21+
print(np.sort(a, axis=0))
22+
print(list(np.argsort(a, axis=0)))
23+
24+
a = np.array([5, -1, 3, -4, 2], dtype=dtype)
25+
print(np.sort(a, axis=0))
26+
print(list(np.argsort(a, axis=0)))
27+
28+
# median with negative values for signed and float dtypes
29+
for dtype in (np.int8, np.int16, np.float):
30+
print()
31+
a = np.array([-5, -1, -3, -2, -4], dtype=dtype)
32+
print(np.median(a))

tests/1d/numpy/sort.py.exp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
array([], dtype=uint8)
3+
[]
4+
array([1, 2, 3, 4], dtype=uint8)
5+
[1, 3, 2, 0]
6+
7+
array([], dtype=int8)
8+
[]
9+
array([1, 2, 3, 4], dtype=int8)
10+
[1, 3, 2, 0]
11+
12+
array([], dtype=uint16)
13+
[]
14+
array([1, 2, 3, 4], dtype=uint16)
15+
[1, 3, 2, 0]
16+
17+
array([], dtype=int16)
18+
[]
19+
array([1, 2, 3, 4], dtype=int16)
20+
[1, 3, 2, 0]
21+
22+
array([], dtype=float64)
23+
[]
24+
array([1.0, 2.0, 3.0, 4.0], dtype=float64)
25+
[1, 3, 2, 0]
26+
27+
array([-3, -2, 0, 1], dtype=int8)
28+
[0, 2, 3, 1]
29+
array([-4, -1, 2, 3, 5], dtype=int8)
30+
[3, 1, 4, 2, 0]
31+
32+
array([-3, -2, 0, 1], dtype=int16)
33+
[0, 2, 3, 1]
34+
array([-4, -1, 2, 3, 5], dtype=int16)
35+
[3, 1, 4, 2, 0]
36+
37+
array([-3.0, -2.0, 0.0, 1.0], dtype=float64)
38+
[0, 2, 3, 1]
39+
array([-4.0, -1.0, 2.0, 3.0, 5.0], dtype=float64)
40+
[3, 1, 4, 2, 0]
41+
42+
-3.0
43+
44+
-3.0
45+
46+
-3.0

tests/2d/numpy/sort.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,29 @@
33
except:
44
import numpy as np
55

6-
dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float)
7-
8-
for dtype in dtypes:
9-
print()
10-
a = np.array([], dtype=dtype)
6+
# 2D sort and argsort over all dtypes, all axes
7+
for dtype in (np.uint8, np.int8, np.uint16, np.int16, np.float):
8+
a = np.array([[4, 1, 3], [2, 5, 0]], dtype=dtype)
9+
print(np.sort(a, axis=None))
1110
print(np.sort(a, axis=0))
12-
print(list(np.argsort(a, axis=0)))
11+
print(np.sort(a, axis=1))
12+
print(np.argsort(a, axis=0))
13+
print()
1314

14-
a = np.array([4, 1, 3, 2], dtype=dtype)
15+
# 2D sort with negative values for signed and float dtypes, axis=0 and axis=1
16+
for dtype in (np.int8, np.int16, np.float):
17+
a = np.array([[-3, 1], [2, -4], [-1, 3]], dtype=dtype)
1518
print(np.sort(a, axis=0))
16-
print(list(np.argsort(a, axis=0)))
19+
a = np.array([[3, -1, 2], [-4, 5, -2]], dtype=dtype)
20+
print(np.sort(a, axis=1))
21+
print()
22+
23+
# 2D median with negative values for signed and float dtypes, axis=0 and axis=1
24+
for dtype in (np.int16, np.float):
25+
a = np.array([[-3, 1, 2], [0, -4, 1], [2, 3, -1]], dtype=dtype)
26+
print(np.median(a, axis=0))
27+
print(np.median(a, axis=1))
28+
print()
29+
1730

1831

tests/2d/numpy/sort.py.exp

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,64 @@
1+
array([0, 1, 2, 3, 4, 5], dtype=uint8)
2+
array([[2, 1, 0],
3+
[4, 5, 3]], dtype=uint8)
4+
array([[1, 3, 4],
5+
[0, 2, 5]], dtype=uint8)
6+
array([[1, 0, 1],
7+
[0, 1, 0]], dtype=uint16)
8+
9+
array([0, 1, 2, 3, 4, 5], dtype=int8)
10+
array([[2, 1, 0],
11+
[4, 5, 3]], dtype=int8)
12+
array([[1, 3, 4],
13+
[0, 2, 5]], dtype=int8)
14+
array([[1, 0, 1],
15+
[0, 1, 0]], dtype=uint16)
16+
17+
array([0, 1, 2, 3, 4, 5], dtype=uint16)
18+
array([[2, 1, 0],
19+
[4, 5, 3]], dtype=uint16)
20+
array([[1, 3, 4],
21+
[0, 2, 5]], dtype=uint16)
22+
array([[1, 0, 1],
23+
[0, 1, 0]], dtype=uint16)
24+
25+
array([0, 1, 2, 3, 4, 5], dtype=int16)
26+
array([[2, 1, 0],
27+
[4, 5, 3]], dtype=int16)
28+
array([[1, 3, 4],
29+
[0, 2, 5]], dtype=int16)
30+
array([[1, 0, 1],
31+
[0, 1, 0]], dtype=uint16)
32+
33+
array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0], dtype=float64)
34+
array([[2.0, 1.0, 0.0],
35+
[4.0, 5.0, 3.0]], dtype=float64)
36+
array([[1.0, 3.0, 4.0],
37+
[0.0, 2.0, 5.0]], dtype=float64)
38+
array([[1, 0, 1],
39+
[0, 1, 0]], dtype=uint16)
40+
41+
array([[-3, -4],
42+
[-1, 1],
43+
[2, 3]], dtype=int8)
44+
array([[-1, 2, 3],
45+
[-4, -2, 5]], dtype=int8)
46+
47+
array([[-3, -4],
48+
[-1, 1],
49+
[2, 3]], dtype=int16)
50+
array([[-1, 2, 3],
51+
[-4, -2, 5]], dtype=int16)
52+
53+
array([[-3.0, -4.0],
54+
[-1.0, 1.0],
55+
[2.0, 3.0]], dtype=float64)
56+
array([[-1.0, 2.0, 3.0],
57+
[-4.0, -2.0, 5.0]], dtype=float64)
58+
59+
array([0.0, 1.0, 1.0], dtype=float64)
60+
array([1.0, 0.0, 2.0], dtype=float64)
61+
62+
array([0.0, 1.0, 1.0], dtype=float64)
63+
array([1.0, 0.0, 2.0], dtype=float64)
164

2-
array([], dtype=uint8)
3-
[]
4-
array([1, 2, 3, 4], dtype=uint8)
5-
[1, 3, 2, 0]
6-
7-
array([], dtype=int8)
8-
[]
9-
array([1, 2, 3, 4], dtype=int8)
10-
[1, 3, 2, 0]
11-
12-
array([], dtype=uint16)
13-
[]
14-
array([1, 2, 3, 4], dtype=uint16)
15-
[1, 3, 2, 0]
16-
17-
array([], dtype=int16)
18-
[]
19-
array([1, 2, 3, 4], dtype=int16)
20-
[1, 3, 2, 0]
21-
22-
array([], dtype=float64)
23-
[]
24-
array([1.0, 2.0, 3.0, 4.0], dtype=float64)
25-
[1, 3, 2, 0]

0 commit comments

Comments
 (0)