Skip to content

Commit

Permalink
complex number error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
h-milz committed Dec 8, 2024
1 parent 0a6ad6d commit 59c0e71
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
68 changes: 48 additions & 20 deletions code/scipy/integrate/integrate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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];
Expand Down
7 changes: 5 additions & 2 deletions code/ulab.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 59c0e71

Please sign in to comment.