implemented any/all for iterables and scalars

This commit is contained in:
Zoltán Vörös 2021-02-01 21:05:43 +01:00
parent e4fa4cb851
commit e485b0c5e4
5 changed files with 49 additions and 1 deletions

View file

@ -61,6 +61,38 @@ static void numerical_reduce_axes(ndarray_obj_t *ndarray, int8_t axis, size_t *s
}
}
#if ULAB_NUMPY_HAS_ALL | ULAB_NUMPY_HAS_ANY
static mp_obj_t numerical_all_any_iterable(mp_obj_t oin, bool anytype) {
if(mp_obj_is_int(oin) || mp_obj_is_float(oin)) {
return mp_obj_is_true(oin) ? mp_const_true : mp_const_false;
}
mp_obj_iter_buf_t iter_buf;
mp_obj_t item, iterable = mp_getiter(oin, &iter_buf);
while((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
if(!mp_obj_is_true(item) & !anytype) {
return mp_const_false;
} else if(mp_obj_is_true(item) & anytype) {
return mp_const_true;
}
}
return anytype ? mp_const_false : mp_const_true;
}
#if ULAB_NUMPY_HAS_ALL
mp_obj_t numerical_all(mp_obj_t oin) {
return numerical_all_any_iterable(oin, false);
}
MP_DEFINE_CONST_FUN_OBJ_1(numerical_all_obj, numerical_all);
#endif
#if ULAB_NUMPY_HAS_ANY
mp_obj_t numerical_any(mp_obj_t oin) {
return numerical_all_any_iterable(oin, true);
}
MP_DEFINE_CONST_FUN_OBJ_1(numerical_any_obj, numerical_any);
#endif
#endif
#if ULAB_NUMPY_HAS_SUM | ULAB_NUMPY_HAS_MEAN | ULAB_NUMPY_HAS_STD
static mp_obj_t numerical_sum_mean_std_iterable(mp_obj_t oin, uint8_t optype, size_t ddof) {
mp_float_t value = 0.0, M = 0.0, m = 0.0, S = 0.0, s = 0.0, sum = 0.0;

View file

@ -568,6 +568,8 @@
#endif
MP_DECLARE_CONST_FUN_OBJ_1(numerical_all_obj);
MP_DECLARE_CONST_FUN_OBJ_1(numerical_any_obj);
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_argmax_obj);
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_argmin_obj);
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_argsort_obj);

View file

@ -149,6 +149,12 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_convolve), (mp_obj_t)&filter_convolve_obj },
#endif
// functions of the numerical sub-module
#if ULAB_NUMPY_HAS_ALL
{ MP_OBJ_NEW_QSTR(MP_QSTR_all), (mp_obj_t)&numerical_all_obj },
#endif
#if ULAB_NUMPY_HAS_ANY
{ MP_OBJ_NEW_QSTR(MP_QSTR_any), (mp_obj_t)&numerical_any_obj },
#endif
#if ULAB_NUMPY_HAS_ARGMINMAX
{ MP_OBJ_NEW_QSTR(MP_QSTR_argmax), (mp_obj_t)&numerical_argmax_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_argmin), (mp_obj_t)&numerical_argmin_obj },

View file

@ -33,7 +33,7 @@
#include "user/user.h"
#define ULAB_VERSION 2.2.0
#define ULAB_VERSION 2.3.0
#define xstr(s) str(s)
#define str(s) #s
#define ULAB_VERSION_STRING xstr(ULAB_VERSION) xstr(-) xstr(ULAB_MAX_DIMS) xstr(D)

View file

@ -375,6 +375,14 @@
#define ULAB_FFT_HAS_IFFT (1)
#endif
#ifndef ULAB_NUMPY_HAS_ALL
#define ULAB_NUMPY_HAS_ALL (1)
#endif
#ifndef ULAB_NUMPY_HAS_ANY
#define ULAB_NUMPY_HAS_ANY (1)
#endif
#ifndef ULAB_NUMPY_HAS_ARGMINMAX
#define ULAB_NUMPY_HAS_ARGMINMAX (1)
#endif