Skip to content

Commit a3fc235

Browse files
committed
fux keepdims code
1 parent f013bad commit a3fc235

File tree

7 files changed

+213
-33
lines changed

7 files changed

+213
-33
lines changed

code/numpy/numerical.c

+70-7
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ static mp_obj_t numerical_sum_mean_std_ndarray(ndarray_obj_t *ndarray, mp_obj_t
372372
mp_float_t norm = (mp_float_t)_shape_strides.shape[0];
373373
// re-wind the array here
374374
farray = (mp_float_t *)results->array;
375-
for(size_t i=0; i < results->len; i++) {
375+
for(size_t i = 0; i < results->len; i++) {
376376
*farray++ *= norm;
377377
}
378378
}
@@ -397,9 +397,9 @@ static mp_obj_t numerical_sum_mean_std_ndarray(ndarray_obj_t *ndarray, mp_obj_t
397397
RUN_MEAN_STD(mp_float_t, array, farray, _shape_strides, div, isStd);
398398
}
399399
}
400-
// return(ulab_tools_restore_dims(results, keepdims, axis));
401-
return MP_OBJ_FROM_PTR(results);
400+
return ulab_tools_restore_dims(ndarray, results, keepdims, _shape_strides);
402401
}
402+
// we should never get to this point
403403
return mp_const_none;
404404
}
405405
#endif
@@ -439,7 +439,7 @@ static mp_obj_t numerical_argmin_argmax_iterable(mp_obj_t oin, uint8_t optype) {
439439
}
440440
}
441441

442-
static mp_obj_t numerical_argmin_argmax_ndarray(ndarray_obj_t *ndarray, mp_obj_t axis, uint8_t optype) {
442+
static mp_obj_t numerical_argmin_argmax_ndarray(ndarray_obj_t *ndarray, mp_obj_t keepdims, mp_obj_t axis, uint8_t optype) {
443443
// TODO: treat the flattened array
444444
if(ndarray->len == 0) {
445445
mp_raise_ValueError(MP_ERROR_TEXT("attempt to get (arg)min/(arg)max of empty sequence"));
@@ -519,7 +519,9 @@ static mp_obj_t numerical_argmin_argmax_ndarray(ndarray_obj_t *ndarray, mp_obj_t
519519
int32_t *strides = m_new0(int32_t, ULAB_MAX_DIMS);
520520

521521
numerical_reduce_axes(ndarray, ax, shape, strides);
522-
uint8_t index = ULAB_MAX_DIMS - ndarray->ndim + ax;
522+
shape_strides _shape_strides = tools_reduce_axes(ndarray, axis);
523+
524+
uint8_t index = _shape_strides.axis;
523525

524526
ndarray_obj_t *results = NULL;
525527

@@ -548,8 +550,9 @@ static mp_obj_t numerical_argmin_argmax_ndarray(ndarray_obj_t *ndarray, mp_obj_t
548550
if(results->len == 1) {
549551
return mp_binary_get_val_array(results->dtype, results->array, 0);
550552
}
551-
return MP_OBJ_FROM_PTR(results);
553+
return ulab_tools_restore_dims(ndarray, results, keepdims, _shape_strides);
552554
}
555+
// we should never get to this point
553556
return mp_const_none;
554557
}
555558
#endif
@@ -599,7 +602,7 @@ static mp_obj_t numerical_function(size_t n_args, const mp_obj_t *pos_args, mp_m
599602
case NUMERICAL_ARGMIN:
600603
case NUMERICAL_ARGMAX:
601604
COMPLEX_DTYPE_NOT_IMPLEMENTED(ndarray->dtype)
602-
return numerical_argmin_argmax_ndarray(ndarray, axis, optype);
605+
return numerical_argmin_argmax_ndarray(ndarray, keepdims, axis, optype);
603606
case NUMERICAL_SUM:
604607
case NUMERICAL_MEAN:
605608
COMPLEX_DTYPE_NOT_IMPLEMENTED(ndarray->dtype)
@@ -1423,6 +1426,66 @@ MP_DEFINE_CONST_FUN_OBJ_KW(numerical_std_obj, 1, numerical_std);
14231426

14241427
mp_obj_t numerical_sum(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
14251428
return numerical_function(n_args, pos_args, kw_args, NUMERICAL_SUM);
1429+
// static const mp_arg_t allowed_args[] = {
1430+
// { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_rom_obj = MP_ROM_NONE} } ,
1431+
// { MP_QSTR_axis, MP_ARG_OBJ, { .u_rom_obj = MP_ROM_NONE } },
1432+
// { MP_QSTR_keepdims, MP_ARG_OBJ, { .u_rom_obj = MP_ROM_FALSE } },
1433+
// };
1434+
1435+
// mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
1436+
// mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
1437+
1438+
// mp_obj_t oin = args[0].u_obj;
1439+
// mp_obj_t axis = args[1].u_obj;
1440+
// mp_obj_t keepdims = args[2].u_obj;
1441+
1442+
// if((axis != mp_const_none) && (!mp_obj_is_int(axis))) {
1443+
// mp_raise_TypeError(MP_ERROR_TEXT("axis must be None, or an integer"));
1444+
// }
1445+
1446+
// ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(oin);
1447+
// if(!mp_obj_is_int(axis) & (axis != mp_const_none)) {
1448+
// mp_raise_TypeError(MP_ERROR_TEXT("axis must be None, or an integer"));
1449+
// }
1450+
1451+
// shape_strides _shape_strides;
1452+
1453+
// _shape_strides.increment = 0;
1454+
// // this is the contracted dimension (won't be overwritten for axis == None)
1455+
// _shape_strides.ndim = 0;
1456+
1457+
// size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
1458+
// _shape_strides.shape = shape;
1459+
// int32_t *strides = m_new(int32_t, ULAB_MAX_DIMS);
1460+
// _shape_strides.strides = strides;
1461+
1462+
// memcpy(_shape_strides.shape, ndarray->shape, sizeof(size_t) * ULAB_MAX_DIMS);
1463+
// memcpy(_shape_strides.strides, ndarray->strides, sizeof(int32_t) * ULAB_MAX_DIMS);
1464+
1465+
// uint8_t index = ULAB_MAX_DIMS - 1; // value of index for axis == mp_const_none (won't be overwritten)
1466+
1467+
// if(axis != mp_const_none) { // i.e., axis is an integer
1468+
// int8_t ax = tools_get_axis(axis, ndarray->ndim);
1469+
// index = ULAB_MAX_DIMS - ndarray->ndim + ax;
1470+
// _shape_strides.ndim = ndarray->ndim - 1;
1471+
// }
1472+
1473+
// // move the value stored at index to the leftmost position, and align everything else to the right
1474+
// _shape_strides.shape[0] = ndarray->shape[index];
1475+
// _shape_strides.strides[0] = ndarray->strides[index];
1476+
// for(uint8_t i = 0; i < index; i++) {
1477+
// // entries to the right of index must be shifted by one position to the left
1478+
// _shape_strides.shape[i + 1] = ndarray->shape[i];
1479+
// _shape_strides.strides[i + 1] = ndarray->strides[i];
1480+
// }
1481+
1482+
// if(_shape_strides.ndim != 0) {
1483+
// _shape_strides.increment = 1;
1484+
// }
1485+
1486+
1487+
// return mp_const_none;
1488+
14261489
}
14271490

14281491
MP_DEFINE_CONST_FUN_OBJ_KW(numerical_sum_obj, 1, numerical_sum);

code/ulab.c

+1-1
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.6.1
36+
#define ULAB_VERSION 6.7.1
3737
#define xstr(s) str(s)
3838
#define str(s) #s
3939

code/ulab_tools.c

+25-24
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,18 @@ shape_strides tools_reduce_axes(ndarray_obj_t *ndarray, mp_obj_t axis) {
199199
memcpy(_shape_strides.shape, ndarray->shape, sizeof(size_t) * ULAB_MAX_DIMS);
200200
memcpy(_shape_strides.strides, ndarray->strides, sizeof(int32_t) * ULAB_MAX_DIMS);
201201

202-
uint8_t index = ULAB_MAX_DIMS - 1; // value of index for axis == mp_const_none (won't be overwritten)
202+
_shape_strides.axis = ULAB_MAX_DIMS - 1; // value of index for axis == mp_const_none (won't be overwritten)
203203

204204
if(axis != mp_const_none) { // i.e., axis is an integer
205205
int8_t ax = tools_get_axis(axis, ndarray->ndim);
206-
index = ULAB_MAX_DIMS - ndarray->ndim + ax;
206+
_shape_strides.axis = ULAB_MAX_DIMS - ndarray->ndim + ax;
207207
_shape_strides.ndim = ndarray->ndim - 1;
208208
}
209209

210210
// move the value stored at index to the leftmost position, and align everything else to the right
211-
_shape_strides.shape[0] = ndarray->shape[index];
212-
_shape_strides.strides[0] = ndarray->strides[index];
213-
for(uint8_t i = 0; i < index; i++) {
211+
_shape_strides.shape[0] = ndarray->shape[_shape_strides.axis];
212+
_shape_strides.strides[0] = ndarray->strides[_shape_strides.axis];
213+
for(uint8_t i = 0; i < _shape_strides.axis; i++) {
214214
// entries to the right of index must be shifted by one position to the left
215215
_shape_strides.shape[i + 1] = ndarray->shape[i];
216216
_shape_strides.strides[i + 1] = ndarray->strides[i];
@@ -220,35 +220,36 @@ shape_strides tools_reduce_axes(ndarray_obj_t *ndarray, mp_obj_t axis) {
220220
_shape_strides.increment = 1;
221221
}
222222

223+
if(_shape_strides.ndim == 0) {
224+
_shape_strides.ndim = 1;
225+
_shape_strides.shape[ULAB_MAX_DIMS - 1] = 1;
226+
_shape_strides.strides[ULAB_MAX_DIMS - 1] = ndarray->itemsize;
227+
}
228+
223229
return _shape_strides;
224230
}
225231

226-
227-
mp_obj_t ulab_tools_restore_dims(ndarray_obj_t *results, mp_obj_t keepdims, mp_obj_t axis) {
232+
mp_obj_t ulab_tools_restore_dims(ndarray_obj_t *ndarray, ndarray_obj_t *results, mp_obj_t keepdims, shape_strides _shape_strides) {
228233
// restores the contracted dimension, if keepdims is True
229-
return MP_OBJ_FROM_PTR(results);
234+
if((ndarray->ndim == 1) && (keepdims != mp_const_true)) {
235+
// since the original array has already been contracted and
236+
// we don't want to keep the dimensions here, we have to return a scalar
237+
return mp_binary_get_val_array(results->dtype, results->array, 0);
238+
}
239+
230240
if(keepdims == mp_const_true) {
231-
mp_printf(MP_PYTHON_PRINTER, "keepdims");
232241
results->ndim += 1;
233-
int8_t ax = tools_get_axis(axis, results->ndim + 1);
234-
printf("%d\n", ax);
235-
for(int8_t i = ULAB_MAX_DIMS - 1; i >= 0; i--) {
236-
printf("(%ld)\n", results->shape[i]);
242+
for(int8_t i = 0; i < ULAB_MAX_DIMS; i++) {
243+
results->shape[i] = ndarray->shape[i];
237244
}
238-
mp_float_t *a = (mp_float_t *)results->array;
239-
printf("%f\n", *a);
240-
// shift values from the right to the left in the strides and shape arrays
241-
for(uint8_t i = 0; i < ULAB_MAX_DIMS - 1 - results->ndim + ax; i++) {
242-
results->shape[i] = results->shape[i + 1];
243-
results->strides[i] = results->strides[i + 1];
245+
results->shape[_shape_strides.axis] = 1;
246+
247+
results->strides[ULAB_MAX_DIMS - 1] = ndarray->itemsize;
248+
for(uint8_t i = ULAB_MAX_DIMS; i > 1; i--) {
249+
results->strides[i - 2] = results->strides[i - 1] * results->shape[i - 1];
244250
}
245-
results->shape[ULAB_MAX_DIMS - 1 - results->ndim + ax] = 1;
246-
results->strides[ULAB_MAX_DIMS - 1 - results->ndim + ax + 1] = results->strides[ULAB_MAX_DIMS - 1 - results->ndim + ax];
247251
}
248252

249-
if((keepdims == mp_const_false) && (results->ndim == 0)) { // return a scalar here
250-
return mp_binary_get_val_array(results->dtype, results->array, 0);
251-
}
252253
return MP_OBJ_FROM_PTR(results);
253254
}
254255

code/ulab_tools.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
typedef struct _shape_strides_t {
1919
uint8_t increment;
20+
uint8_t axis;
2021
uint8_t ndim;
2122
size_t *shape;
2223
int32_t *strides;
@@ -34,7 +35,7 @@ void *ndarray_set_float_function(uint8_t );
3435

3536
shape_strides tools_reduce_axes(ndarray_obj_t *, mp_obj_t );
3637
int8_t tools_get_axis(mp_obj_t , uint8_t );
37-
mp_obj_t ulab_tools_restore_dims(ndarray_obj_t * , mp_obj_t , mp_obj_t );
38+
mp_obj_t ulab_tools_restore_dims(ndarray_obj_t * , ndarray_obj_t * , mp_obj_t , shape_strides );
3839
ndarray_obj_t *tools_object_is_square(mp_obj_t );
3940

4041
uint8_t ulab_binary_get_size(uint8_t );

docs/ulab-change-log.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
Mon, 30 Dec 2024
2+
3+
version 6.7.1
4+
5+
add keepdims keyword argument to numerical functions
6+
7+
Sun, 15 Dec 2024
8+
9+
version 6.7.0
10+
11+
add scipy.integrate module
12+
113
Sun, 24 Nov 2024
214

315
version 6.6.1

tests/2d/numpy/sum.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
try:
2+
from ulab import numpy as np
3+
except ImportError:
4+
import numpy as np
5+
6+
for dtype in (np.uint8, np.int8, np.uint16, np.int8, np.float):
7+
a = np.array(range(12), dtype=dtype)
8+
b = a.reshape((3, 4))
9+
10+
print(a)
11+
print(b)
12+
print()
13+
14+
print(np.sum(a))
15+
print(np.sum(a, axis=0))
16+
print(np.sum(a, axis=0, keepdims=True))
17+
18+
print()
19+
print(np.sum(b))
20+
print(np.sum(b, axis=0))
21+
print(np.sum(b, axis=1))
22+
print(np.sum(b, axis=0, keepdims=True))
23+
print(np.sum(b, axis=1, keepdims=True))

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

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
array([0, 1, 2, ..., 9, 10, 11], dtype=uint8)
2+
array([[0, 1, 2, 3],
3+
[4, 5, 6, 7],
4+
[8, 9, 10, 11]], dtype=uint8)
5+
6+
66
7+
66
8+
array([66], dtype=uint8)
9+
10+
66
11+
array([12, 15, 18, 21], dtype=uint8)
12+
array([6, 22, 38], dtype=uint8)
13+
array([[12, 15, 18, 21]], dtype=uint8)
14+
array([[6],
15+
[22],
16+
[38]], dtype=uint8)
17+
array([0, 1, 2, ..., 9, 10, 11], dtype=int8)
18+
array([[0, 1, 2, 3],
19+
[4, 5, 6, 7],
20+
[8, 9, 10, 11]], dtype=int8)
21+
22+
66
23+
66
24+
array([66], dtype=int8)
25+
26+
66
27+
array([12, 15, 18, 21], dtype=int8)
28+
array([6, 22, 38], dtype=int8)
29+
array([[12, 15, 18, 21]], dtype=int8)
30+
array([[6],
31+
[22],
32+
[38]], dtype=int8)
33+
array([0, 1, 2, ..., 9, 10, 11], dtype=uint16)
34+
array([[0, 1, 2, 3],
35+
[4, 5, 6, 7],
36+
[8, 9, 10, 11]], dtype=uint16)
37+
38+
66
39+
66
40+
array([66], dtype=uint16)
41+
42+
66
43+
array([12, 15, 18, 21], dtype=uint16)
44+
array([6, 22, 38], dtype=uint16)
45+
array([[12, 15, 18, 21]], dtype=uint16)
46+
array([[6],
47+
[22],
48+
[38]], dtype=uint16)
49+
array([0, 1, 2, ..., 9, 10, 11], dtype=int8)
50+
array([[0, 1, 2, 3],
51+
[4, 5, 6, 7],
52+
[8, 9, 10, 11]], dtype=int8)
53+
54+
66
55+
66
56+
array([66], dtype=int8)
57+
58+
66
59+
array([12, 15, 18, 21], dtype=int8)
60+
array([6, 22, 38], dtype=int8)
61+
array([[12, 15, 18, 21]], dtype=int8)
62+
array([[6],
63+
[22],
64+
[38]], dtype=int8)
65+
array([0.0, 1.0, 2.0, ..., 9.0, 10.0, 11.0], dtype=float64)
66+
array([[0.0, 1.0, 2.0, 3.0],
67+
[4.0, 5.0, 6.0, 7.0],
68+
[8.0, 9.0, 10.0, 11.0]], dtype=float64)
69+
70+
66.0
71+
66.0
72+
array([66.0], dtype=float64)
73+
74+
66.0
75+
array([12.0, 15.0, 18.0, 21.0], dtype=float64)
76+
array([6.0, 22.0, 38.0], dtype=float64)
77+
array([[12.0, 15.0, 18.0, 21.0]], dtype=float64)
78+
array([[6.0],
79+
[22.0],
80+
[38.0]], dtype=float64)

0 commit comments

Comments
 (0)