summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-04-17 17:11:03 +0100
committerDamien George <damien.p.george@gmail.com>2014-04-17 17:11:03 +0100
commitdbdfee15a1839858b4d12f352b2e34597e6e76e9 (patch)
treedb3d1cae2a4b3c3bdc825a226b818d728100fe8d /py
parentfb06bfc11ccc2506d27e3f53750771d5c737df2e (diff)
downloadmicropython-dbdfee15a1839858b4d12f352b2e34597e6e76e9.tar.gz
micropython-dbdfee15a1839858b4d12f352b2e34597e6e76e9.zip
py: Add cmath module, for complex math. Disabled by default.
Not all functions implemented. Not enabled on pyboard.
Diffstat (limited to 'py')
-rw-r--r--py/builtin.h1
-rw-r--r--py/builtintables.c3
-rw-r--r--py/modcmath.c131
-rw-r--r--py/modmath.c9
-rw-r--r--py/mpconfig.h10
-rw-r--r--py/py.mk1
-rw-r--r--py/qstrdefs.h9
7 files changed, 160 insertions, 4 deletions
diff --git a/py/builtin.h b/py/builtin.h
index 36b9f38814..9b2b9c9424 100644
--- a/py/builtin.h
+++ b/py/builtin.h
@@ -48,6 +48,7 @@ extern const mp_obj_module_t mp_module_array;
extern const mp_obj_module_t mp_module_collections;
extern const mp_obj_module_t mp_module_io;
extern const mp_obj_module_t mp_module_math;
+extern const mp_obj_module_t mp_module_cmath;
extern const mp_obj_module_t mp_module_micropython;
extern const mp_obj_module_t mp_module_struct;
extern const mp_obj_module_t mp_module_sys;
diff --git a/py/builtintables.c b/py/builtintables.c
index 6bdae24002..c4c96ed35a 100644
--- a/py/builtintables.c
+++ b/py/builtintables.c
@@ -139,6 +139,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
#if MICROPY_ENABLE_FLOAT
{ MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&mp_module_math },
+#if MICROPY_ENABLE_MOD_CMATH
+ { MP_OBJ_NEW_QSTR(MP_QSTR_cmath), (mp_obj_t)&mp_module_cmath },
+#endif
#endif
#if MICROPY_ENABLE_MOD_SYS
{ MP_OBJ_NEW_QSTR(MP_QSTR_sys), (mp_obj_t)&mp_module_sys },
diff --git a/py/modcmath.c b/py/modcmath.c
new file mode 100644
index 0000000000..3f561e7f30
--- /dev/null
+++ b/py/modcmath.c
@@ -0,0 +1,131 @@
+#include <math.h>
+
+#include "misc.h"
+#include "mpconfig.h"
+#include "qstr.h"
+#include "obj.h"
+#include "builtin.h"
+
+#if MICROPY_ENABLE_FLOAT && MICROPY_ENABLE_MOD_CMATH
+
+// These are defined in modmath.c
+extern const mp_obj_float_t mp_math_e_obj;
+extern const mp_obj_float_t mp_math_pi_obj;
+
+mp_obj_t mp_cmath_phase(mp_obj_t z_obj) {
+ mp_float_t real, imag;
+ mp_obj_get_complex(z_obj, &real, &imag);
+ return mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_phase_obj, mp_cmath_phase);
+
+mp_obj_t mp_cmath_polar(mp_obj_t z_obj) {
+ mp_float_t real, imag;
+ mp_obj_get_complex(z_obj, &real, &imag);
+ mp_obj_t tuple[2] = {
+ mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)),
+ mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real)),
+ };
+ return mp_obj_new_tuple(2, tuple);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_polar_obj, mp_cmath_polar);
+
+mp_obj_t mp_cmath_rect(mp_obj_t r_obj, mp_obj_t phi_obj) {
+ mp_float_t r = mp_obj_get_float(r_obj);
+ mp_float_t phi = mp_obj_get_float(phi_obj);
+ return mp_obj_new_complex(r * MICROPY_FLOAT_C_FUN(cos)(phi), r * MICROPY_FLOAT_C_FUN(sin)(phi));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_cmath_rect_obj, mp_cmath_rect);
+
+mp_obj_t mp_cmath_exp(mp_obj_t z_obj) {
+ mp_float_t real, imag;
+ mp_obj_get_complex(z_obj, &real, &imag);
+ mp_float_t exp_real = MICROPY_FLOAT_C_FUN(exp)(real);
+ return mp_obj_new_complex(exp_real * MICROPY_FLOAT_C_FUN(cos)(imag), exp_real * MICROPY_FLOAT_C_FUN(sin)(imag));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_exp_obj, mp_cmath_exp);
+
+// TODO can take second argument, being the base
+mp_obj_t mp_cmath_log(mp_obj_t z_obj) {
+ mp_float_t real, imag;
+ mp_obj_get_complex(z_obj, &real, &imag);
+ return mp_obj_new_complex(0.5 * MICROPY_FLOAT_C_FUN(log)(real*real + imag*imag), MICROPY_FLOAT_C_FUN(atan2)(imag, real));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log_obj, mp_cmath_log);
+
+mp_obj_t mp_cmath_log10(mp_obj_t z_obj) {
+ mp_float_t real, imag;
+ mp_obj_get_complex(z_obj, &real, &imag);
+ return mp_obj_new_complex(0.5 * MICROPY_FLOAT_C_FUN(log10)(real*real + imag*imag), MICROPY_FLOAT_C_FUN(atan2)(imag, real));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log10_obj, mp_cmath_log10);
+
+mp_obj_t mp_cmath_sqrt(mp_obj_t z_obj) {
+ mp_float_t real, imag;
+ mp_obj_get_complex(z_obj, &real, &imag);
+ mp_float_t sqrt_abs = MICROPY_FLOAT_C_FUN(pow)(real*real + imag*imag, 0.25);
+ mp_float_t theta = 0.5 * MICROPY_FLOAT_C_FUN(atan2)(imag, real);
+ return mp_obj_new_complex(sqrt_abs * cos(theta), sqrt_abs * sin(theta));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sqrt_obj, mp_cmath_sqrt);
+
+mp_obj_t mp_cmath_cos(mp_obj_t z_obj) {
+ mp_float_t real, imag;
+ mp_obj_get_complex(z_obj, &real, &imag);
+ return mp_obj_new_complex(MICROPY_FLOAT_C_FUN(cos)(real) * MICROPY_FLOAT_C_FUN(cosh)(imag), -MICROPY_FLOAT_C_FUN(sin)(real) * MICROPY_FLOAT_C_FUN(sinh)(imag));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_cos_obj, mp_cmath_cos);
+
+mp_obj_t mp_cmath_sin(mp_obj_t z_obj) {
+ mp_float_t real, imag;
+ mp_obj_get_complex(z_obj, &real, &imag);
+ return mp_obj_new_complex(MICROPY_FLOAT_C_FUN(sin)(real) * MICROPY_FLOAT_C_FUN(cosh)(imag), MICROPY_FLOAT_C_FUN(cos)(real) * MICROPY_FLOAT_C_FUN(sinh)(imag));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sin_obj, mp_cmath_sin);
+
+STATIC const mp_map_elem_t mp_module_cmath_globals_table[] = {
+ { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_cmath) },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_pi), (mp_obj_t)&mp_math_pi_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_phase), (mp_obj_t)&mp_cmath_phase_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_polar), (mp_obj_t)&mp_cmath_polar_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_rect), (mp_obj_t)&mp_cmath_rect_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_exp), (mp_obj_t)&mp_cmath_exp_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_log), (mp_obj_t)&mp_cmath_log_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_log10), (mp_obj_t)&mp_cmath_log10_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_sqrt), (mp_obj_t)&mp_cmath_sqrt_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_acos), (mp_obj_t)&mp_cmath_acos_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_asin), (mp_obj_t)&mp_cmath_asin_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_atan), (mp_obj_t)&mp_cmath_atan_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_cos), (mp_obj_t)&mp_cmath_cos_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_sin), (mp_obj_t)&mp_cmath_sin_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_tan), (mp_obj_t)&mp_cmath_tan_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_acosh), (mp_obj_t)&mp_cmath_acosh_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_asinh), (mp_obj_t)&mp_cmath_asinh_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_atanh), (mp_obj_t)&mp_cmath_atanh_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_cosh), (mp_obj_t)&mp_cmath_cosh_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_sinh), (mp_obj_t)&mp_cmath_sinh_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_tanh), (mp_obj_t)&mp_cmath_tanh_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_isfinite), (mp_obj_t)&mp_cmath_isfinite_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_isinf), (mp_obj_t)&mp_cmath_isinf_obj },
+ //{ MP_OBJ_NEW_QSTR(MP_QSTR_isnan), (mp_obj_t)&mp_cmath_isnan_obj },
+};
+
+STATIC const mp_obj_dict_t mp_module_cmath_globals = {
+ .base = {&mp_type_dict},
+ .map = {
+ .all_keys_are_qstrs = 1,
+ .table_is_fixed_array = 1,
+ .used = sizeof(mp_module_cmath_globals_table) / sizeof(mp_map_elem_t),
+ .alloc = sizeof(mp_module_cmath_globals_table) / sizeof(mp_map_elem_t),
+ .table = (mp_map_elem_t*)mp_module_cmath_globals_table,
+ },
+};
+
+const mp_obj_module_t mp_module_cmath = {
+ .base = { &mp_type_module },
+ .name = MP_QSTR_cmath,
+ .globals = (mp_obj_dict_t*)&mp_module_cmath_globals,
+};
+
+#endif // MICROPY_ENABLE_FLOAT && MICROPY_ENABLE_MOD_CMATH
diff --git a/py/modmath.c b/py/modmath.c
index c26ba16189..09bb4ce3aa 100644
--- a/py/modmath.c
+++ b/py/modmath.c
@@ -6,7 +6,7 @@
#include "obj.h"
#include "builtin.h"
-#if MICROPY_ENABLE_FLOAT
+#if MICROPY_ENABLE_FLOAT && MICROPY_ENABLE_MOD_MATH
//TODO: Change macros to check for overflow and raise OverflowError or RangeError
#define MATH_FUN_1(py_name, c_name) \
@@ -25,8 +25,9 @@
mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return mp_obj_new_int((machine_int_t)MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj))); } \
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
-STATIC const mp_obj_float_t mp_math_e_obj = {{&mp_type_float}, M_E};
-STATIC const mp_obj_float_t mp_math_pi_obj = {{&mp_type_float}, M_PI};
+// These are also used by cmath.c
+const mp_obj_float_t mp_math_e_obj = {{&mp_type_float}, M_E};
+const mp_obj_float_t mp_math_pi_obj = {{&mp_type_float}, M_PI};
MATH_FUN_1(sqrt, sqrt)
MATH_FUN_2(pow, pow)
@@ -156,4 +157,4 @@ const mp_obj_module_t mp_module_math = {
.globals = (mp_obj_dict_t*)&mp_module_math_globals,
};
-#endif // MICROPY_ENABLE_FLOAT
+#endif // MICROPY_ENABLE_FLOAT && MICROPY_ENABLE_MOD_MATH
diff --git a/py/mpconfig.h b/py/mpconfig.h
index f7ebdabbda..43c931cd66 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -110,6 +110,16 @@ typedef double mp_float_t;
#define MICROPY_ENABLE_FLOAT (0)
#endif
+// Whether to provide "math" module
+#ifndef MICROPY_ENABLE_MOD_MATH
+#define MICROPY_ENABLE_MOD_MATH (1)
+#endif
+
+// Whether to provide "cmath" module
+#ifndef MICROPY_ENABLE_MOD_CMATH
+#define MICROPY_ENABLE_MOD_CMATH (0)
+#endif
+
// Whether to provide "io" module
#ifndef MICROPY_ENABLE_MOD_IO
#define MICROPY_ENABLE_MOD_IO (1)
diff --git a/py/py.mk b/py/py.mk
index 5d02c95faf..996cbee4e3 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -82,6 +82,7 @@ PY_O_BASENAME = \
modcollections.o \
modio.o \
modmath.o \
+ modcmath.o \
modmicropython.o \
modstruct.o \
modsys.o \
diff --git a/py/qstrdefs.h b/py/qstrdefs.h
index 20c4db75f0..a4df86fda7 100644
--- a/py/qstrdefs.h
+++ b/py/qstrdefs.h
@@ -217,6 +217,7 @@ Q(iterator)
Q(module)
Q(slice)
+#if MICROPY_ENABLE_MOD_MATH || MICROPY_ENABLE_MOD_CMATH
Q(math)
Q(e)
Q(pi)
@@ -258,6 +259,14 @@ Q(erf)
Q(erfc)
Q(gamma)
Q(lgamma)
+#endif
+
+#if MICROPY_ENABLE_MOD_CMATH
+Q(cmath)
+Q(phase)
+Q(polar)
+Q(rect)
+#endif
Q(mem_total)
Q(mem_current)