From 59c0e716822905c710d0a260cfc4c53022884e72 Mon Sep 17 00:00:00 2001 From: Harald Milz Date: Sun, 8 Dec 2024 13:09:02 +0100 Subject: [PATCH] complex number error handling --- code/scipy/integrate/integrate.c | 68 ++++++++++++++++++++++---------- code/ulab.h | 7 +++- 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/code/scipy/integrate/integrate.c b/code/scipy/integrate/integrate.c index 71864692..db80a7ac 100644 --- a/code/scipy/integrate/integrate.c +++ b/code/scipy/integrate/integrate.c @@ -258,14 +258,21 @@ static mp_obj_t integrate_quad(size_t n_args, const mp_obj_t *pos_args, mp_map_t mp_raise_TypeError(MP_ERROR_TEXT("first argument must be a function")); } + // iterate over args 1, 2, and 4 + // arg 3 will be handled by MP_ARG_INT above. + for (int i=1; i<=4; i*=2) { + type = mp_obj_get_type(args[i].u_obj); + if (type != &mp_type_float && type != &mp_type_int) { + mp_raise_msg_varg(&mp_type_TypeError, + MP_ERROR_TEXT("can't convert arg %d from %s to float"), i, mp_obj_get_type_str(args[i].u_obj)); + } + } mp_float_t a = mp_obj_get_float(args[1].u_obj); mp_float_t b = mp_obj_get_float(args[2].u_obj); uint16_t n = (uint16_t)args[3].u_int; -#if 0 - if(n < 0) { - mp_raise_ValueError(MP_ERROR_TEXT("levels should be > 0")); - } -#endif + if (n < 1) { + mp_raise_ValueError(MP_ERROR_TEXT("levels needs to be a positive integer")); + } mp_float_t eps = mp_obj_get_float(args[4].u_obj); mp_obj_t res[2]; @@ -354,14 +361,21 @@ static mp_obj_t integrate_romberg(size_t n_args, const mp_obj_t *pos_args, mp_ma mp_raise_TypeError(MP_ERROR_TEXT("first argument must be a function")); } + // iterate over args 1, 2, and 4 + // arg 3 will be handled by MP_ARG_INT above. + for (int i=1; i<=4; i*=2) { + type = mp_obj_get_type(args[i].u_obj); + if (type != &mp_type_float && type != &mp_type_int) { + mp_raise_msg_varg(&mp_type_TypeError, + MP_ERROR_TEXT("can't convert arg %d from %s to float"), i, mp_obj_get_type_str(args[i].u_obj)); + } + } mp_float_t a = mp_obj_get_float(args[1].u_obj); mp_float_t b = mp_obj_get_float(args[2].u_obj); uint16_t steps = (uint16_t)args[3].u_int; -# if 0 - if(steps < 0) { - mp_raise_ValueError(MP_ERROR_TEXT("steps should be > 0")); - } -#endif + if (steps < 1) { + mp_raise_ValueError(MP_ERROR_TEXT("steps needs to be a positive integer")); + } mp_float_t eps = mp_obj_get_float(args[4].u_obj); return mp_obj_new_float(qromb(fun, a, b, steps, eps)); @@ -443,14 +457,21 @@ static mp_obj_t integrate_simpson(size_t n_args, const mp_obj_t *pos_args, mp_ma mp_raise_TypeError(MP_ERROR_TEXT("first argument must be a function")); } + // iterate over args 1, 2, and 4 + // arg 3 will be handled by MP_ARG_INT above. + for (int i=1; i<=4; i*=2) { + type = mp_obj_get_type(args[i].u_obj); + if (type != &mp_type_float && type != &mp_type_int) { + mp_raise_msg_varg(&mp_type_TypeError, + MP_ERROR_TEXT("can't convert arg %d from %s to float"), i, mp_obj_get_type_str(args[i].u_obj)); + } + } mp_float_t a = mp_obj_get_float(args[1].u_obj); mp_float_t b = mp_obj_get_float(args[2].u_obj); uint16_t steps = (uint16_t)args[3].u_int; -#if 0 - if(steps < 0) { - mp_raise_ValueError(MP_ERROR_TEXT("steps should be > 0")); - } -#endif + if (steps < 1) { + mp_raise_ValueError(MP_ERROR_TEXT("steps needs to be a positive integer")); + } mp_float_t eps = mp_obj_get_float(args[4].u_obj); return mp_obj_new_float(qasi(fun, a, b, steps, eps)); @@ -609,14 +630,21 @@ static mp_obj_t integrate_quadgk(size_t n_args, const mp_obj_t *pos_args, mp_map mp_raise_TypeError(MP_ERROR_TEXT("first argument must be a function")); } + // iterate over args 1, 2, and 4 + // arg 3 will be handled by MP_ARG_INT above. + for (int i=1; i<=4; i*=2) { + type = mp_obj_get_type(args[i].u_obj); + if (type != &mp_type_float && type != &mp_type_int) { + mp_raise_msg_varg(&mp_type_TypeError, + MP_ERROR_TEXT("can't convert arg %d from %s to float"), i, mp_obj_get_type_str(args[i].u_obj)); + } + } mp_float_t a = mp_obj_get_float(args[1].u_obj); mp_float_t b = mp_obj_get_float(args[2].u_obj); uint16_t order = (uint16_t)args[3].u_int; -#if 0 - if(order < 0) { - mp_raise_ValueError(MP_ERROR_TEXT("levels should be > 0")); - } -#endif + if (order < 1) { + mp_raise_ValueError(MP_ERROR_TEXT("order needs to be a positive integer")); + } mp_float_t eps = mp_obj_get_float(args[4].u_obj); mp_obj_t res[2]; diff --git a/code/ulab.h b/code/ulab.h index 79e015ba..7b5e7f40 100644 --- a/code/ulab.h +++ b/code/ulab.h @@ -398,7 +398,8 @@ #define ULAB_NUMPY_HAS_WHERE (1) #endif -// the integrate module +// the integrate module; functions of the integrate module still have +// to be defined separately #ifndef ULAB_SCIPY_HAS_INTEGRATE_MODULE #define ULAB_SCIPY_HAS_INTEGRATE_MODULE (1) #endif @@ -416,10 +417,12 @@ #define ULAB_INTEGRATE_HAS_SIMPSON (1) #endif - +#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE +// we compile quadgk only when we have _DOUBLE #ifndef ULAB_INTEGRATE_HAS_QUADGK #define ULAB_INTEGRATE_HAS_QUADGK (1) #endif +#endif // the linalg module; functions of the linalg module still have // to be defined separately