Skip to content

Commit dbdfee1

Browse files
committed
py: Add cmath module, for complex math. Disabled by default.
Not all functions implemented. Not enabled on pyboard.
1 parent fb06bfc commit dbdfee1

8 files changed

Lines changed: 161 additions & 5 deletions

File tree

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extern const mp_obj_module_t mp_module_array;
4848
extern const mp_obj_module_t mp_module_collections;
4949
extern const mp_obj_module_t mp_module_io;
5050
extern const mp_obj_module_t mp_module_math;
51+
extern const mp_obj_module_t mp_module_cmath;
5152
extern const mp_obj_module_t mp_module_micropython;
5253
extern const mp_obj_module_t mp_module_struct;
5354
extern const mp_obj_module_t mp_module_sys;

py/builtintables.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
139139

140140
#if MICROPY_ENABLE_FLOAT
141141
{ MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&mp_module_math },
142+
#if MICROPY_ENABLE_MOD_CMATH
143+
{ MP_OBJ_NEW_QSTR(MP_QSTR_cmath), (mp_obj_t)&mp_module_cmath },
144+
#endif
142145
#endif
143146
#if MICROPY_ENABLE_MOD_SYS
144147
{ MP_OBJ_NEW_QSTR(MP_QSTR_sys), (mp_obj_t)&mp_module_sys },

py/modcmath.c

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include <math.h>
2+
3+
#include "misc.h"
4+
#include "mpconfig.h"
5+
#include "qstr.h"
6+
#include "obj.h"
7+
#include "builtin.h"
8+
9+
#if MICROPY_ENABLE_FLOAT && MICROPY_ENABLE_MOD_CMATH
10+
11+
// These are defined in modmath.c
12+
extern const mp_obj_float_t mp_math_e_obj;
13+
extern const mp_obj_float_t mp_math_pi_obj;
14+
15+
mp_obj_t mp_cmath_phase(mp_obj_t z_obj) {
16+
mp_float_t real, imag;
17+
mp_obj_get_complex(z_obj, &real, &imag);
18+
return mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real));
19+
}
20+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_phase_obj, mp_cmath_phase);
21+
22+
mp_obj_t mp_cmath_polar(mp_obj_t z_obj) {
23+
mp_float_t real, imag;
24+
mp_obj_get_complex(z_obj, &real, &imag);
25+
mp_obj_t tuple[2] = {
26+
mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)),
27+
mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real)),
28+
};
29+
return mp_obj_new_tuple(2, tuple);
30+
}
31+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_polar_obj, mp_cmath_polar);
32+
33+
mp_obj_t mp_cmath_rect(mp_obj_t r_obj, mp_obj_t phi_obj) {
34+
mp_float_t r = mp_obj_get_float(r_obj);
35+
mp_float_t phi = mp_obj_get_float(phi_obj);
36+
return mp_obj_new_complex(r * MICROPY_FLOAT_C_FUN(cos)(phi), r * MICROPY_FLOAT_C_FUN(sin)(phi));
37+
}
38+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_cmath_rect_obj, mp_cmath_rect);
39+
40+
mp_obj_t mp_cmath_exp(mp_obj_t z_obj) {
41+
mp_float_t real, imag;
42+
mp_obj_get_complex(z_obj, &real, &imag);
43+
mp_float_t exp_real = MICROPY_FLOAT_C_FUN(exp)(real);
44+
return mp_obj_new_complex(exp_real * MICROPY_FLOAT_C_FUN(cos)(imag), exp_real * MICROPY_FLOAT_C_FUN(sin)(imag));
45+
}
46+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_exp_obj, mp_cmath_exp);
47+
48+
// TODO can take second argument, being the base
49+
mp_obj_t mp_cmath_log(mp_obj_t z_obj) {
50+
mp_float_t real, imag;
51+
mp_obj_get_complex(z_obj, &real, &imag);
52+
return mp_obj_new_complex(0.5 * MICROPY_FLOAT_C_FUN(log)(real*real + imag*imag), MICROPY_FLOAT_C_FUN(atan2)(imag, real));
53+
}
54+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log_obj, mp_cmath_log);
55+
56+
mp_obj_t mp_cmath_log10(mp_obj_t z_obj) {
57+
mp_float_t real, imag;
58+
mp_obj_get_complex(z_obj, &real, &imag);
59+
return mp_obj_new_complex(0.5 * MICROPY_FLOAT_C_FUN(log10)(real*real + imag*imag), MICROPY_FLOAT_C_FUN(atan2)(imag, real));
60+
}
61+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log10_obj, mp_cmath_log10);
62+
63+
mp_obj_t mp_cmath_sqrt(mp_obj_t z_obj) {
64+
mp_float_t real, imag;
65+
mp_obj_get_complex(z_obj, &real, &imag);
66+
mp_float_t sqrt_abs = MICROPY_FLOAT_C_FUN(pow)(real*real + imag*imag, 0.25);
67+
mp_float_t theta = 0.5 * MICROPY_FLOAT_C_FUN(atan2)(imag, real);
68+
return mp_obj_new_complex(sqrt_abs * cos(theta), sqrt_abs * sin(theta));
69+
}
70+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sqrt_obj, mp_cmath_sqrt);
71+
72+
mp_obj_t mp_cmath_cos(mp_obj_t z_obj) {
73+
mp_float_t real, imag;
74+
mp_obj_get_complex(z_obj, &real, &imag);
75+
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));
76+
}
77+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_cos_obj, mp_cmath_cos);
78+
79+
mp_obj_t mp_cmath_sin(mp_obj_t z_obj) {
80+
mp_float_t real, imag;
81+
mp_obj_get_complex(z_obj, &real, &imag);
82+
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));
83+
}
84+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sin_obj, mp_cmath_sin);
85+
86+
STATIC const mp_map_elem_t mp_module_cmath_globals_table[] = {
87+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_cmath) },
88+
{ MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj },
89+
{ MP_OBJ_NEW_QSTR(MP_QSTR_pi), (mp_obj_t)&mp_math_pi_obj },
90+
{ MP_OBJ_NEW_QSTR(MP_QSTR_phase), (mp_obj_t)&mp_cmath_phase_obj },
91+
{ MP_OBJ_NEW_QSTR(MP_QSTR_polar), (mp_obj_t)&mp_cmath_polar_obj },
92+
{ MP_OBJ_NEW_QSTR(MP_QSTR_rect), (mp_obj_t)&mp_cmath_rect_obj },
93+
{ MP_OBJ_NEW_QSTR(MP_QSTR_exp), (mp_obj_t)&mp_cmath_exp_obj },
94+
{ MP_OBJ_NEW_QSTR(MP_QSTR_log), (mp_obj_t)&mp_cmath_log_obj },
95+
{ MP_OBJ_NEW_QSTR(MP_QSTR_log10), (mp_obj_t)&mp_cmath_log10_obj },
96+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sqrt), (mp_obj_t)&mp_cmath_sqrt_obj },
97+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_acos), (mp_obj_t)&mp_cmath_acos_obj },
98+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_asin), (mp_obj_t)&mp_cmath_asin_obj },
99+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_atan), (mp_obj_t)&mp_cmath_atan_obj },
100+
{ MP_OBJ_NEW_QSTR(MP_QSTR_cos), (mp_obj_t)&mp_cmath_cos_obj },
101+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sin), (mp_obj_t)&mp_cmath_sin_obj },
102+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_tan), (mp_obj_t)&mp_cmath_tan_obj },
103+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_acosh), (mp_obj_t)&mp_cmath_acosh_obj },
104+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_asinh), (mp_obj_t)&mp_cmath_asinh_obj },
105+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_atanh), (mp_obj_t)&mp_cmath_atanh_obj },
106+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_cosh), (mp_obj_t)&mp_cmath_cosh_obj },
107+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_sinh), (mp_obj_t)&mp_cmath_sinh_obj },
108+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_tanh), (mp_obj_t)&mp_cmath_tanh_obj },
109+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_isfinite), (mp_obj_t)&mp_cmath_isfinite_obj },
110+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_isinf), (mp_obj_t)&mp_cmath_isinf_obj },
111+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_isnan), (mp_obj_t)&mp_cmath_isnan_obj },
112+
};
113+
114+
STATIC const mp_obj_dict_t mp_module_cmath_globals = {
115+
.base = {&mp_type_dict},
116+
.map = {
117+
.all_keys_are_qstrs = 1,
118+
.table_is_fixed_array = 1,
119+
.used = sizeof(mp_module_cmath_globals_table) / sizeof(mp_map_elem_t),
120+
.alloc = sizeof(mp_module_cmath_globals_table) / sizeof(mp_map_elem_t),
121+
.table = (mp_map_elem_t*)mp_module_cmath_globals_table,
122+
},
123+
};
124+
125+
const mp_obj_module_t mp_module_cmath = {
126+
.base = { &mp_type_module },
127+
.name = MP_QSTR_cmath,
128+
.globals = (mp_obj_dict_t*)&mp_module_cmath_globals,
129+
};
130+
131+
#endif // MICROPY_ENABLE_FLOAT && MICROPY_ENABLE_MOD_CMATH

py/modmath.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "obj.h"
77
#include "builtin.h"
88

9-
#if MICROPY_ENABLE_FLOAT
9+
#if MICROPY_ENABLE_FLOAT && MICROPY_ENABLE_MOD_MATH
1010

1111
//TODO: Change macros to check for overflow and raise OverflowError or RangeError
1212
#define MATH_FUN_1(py_name, c_name) \
@@ -25,8 +25,9 @@
2525
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))); } \
2626
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
2727

28-
STATIC const mp_obj_float_t mp_math_e_obj = {{&mp_type_float}, M_E};
29-
STATIC const mp_obj_float_t mp_math_pi_obj = {{&mp_type_float}, M_PI};
28+
// These are also used by cmath.c
29+
const mp_obj_float_t mp_math_e_obj = {{&mp_type_float}, M_E};
30+
const mp_obj_float_t mp_math_pi_obj = {{&mp_type_float}, M_PI};
3031

3132
MATH_FUN_1(sqrt, sqrt)
3233
MATH_FUN_2(pow, pow)
@@ -156,4 +157,4 @@ const mp_obj_module_t mp_module_math = {
156157
.globals = (mp_obj_dict_t*)&mp_module_math_globals,
157158
};
158159

159-
#endif // MICROPY_ENABLE_FLOAT
160+
#endif // MICROPY_ENABLE_FLOAT && MICROPY_ENABLE_MOD_MATH

py/mpconfig.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ typedef double mp_float_t;
110110
#define MICROPY_ENABLE_FLOAT (0)
111111
#endif
112112

113+
// Whether to provide "math" module
114+
#ifndef MICROPY_ENABLE_MOD_MATH
115+
#define MICROPY_ENABLE_MOD_MATH (1)
116+
#endif
117+
118+
// Whether to provide "cmath" module
119+
#ifndef MICROPY_ENABLE_MOD_CMATH
120+
#define MICROPY_ENABLE_MOD_CMATH (0)
121+
#endif
122+
113123
// Whether to provide "io" module
114124
#ifndef MICROPY_ENABLE_MOD_IO
115125
#define MICROPY_ENABLE_MOD_IO (1)

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ PY_O_BASENAME = \
8282
modcollections.o \
8383
modio.o \
8484
modmath.o \
85+
modcmath.o \
8586
modmicropython.o \
8687
modstruct.o \
8788
modsys.o \

py/qstrdefs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ Q(iterator)
217217
Q(module)
218218
Q(slice)
219219

220+
#if MICROPY_ENABLE_MOD_MATH || MICROPY_ENABLE_MOD_CMATH
220221
Q(math)
221222
Q(e)
222223
Q(pi)
@@ -258,6 +259,14 @@ Q(erf)
258259
Q(erfc)
259260
Q(gamma)
260261
Q(lgamma)
262+
#endif
263+
264+
#if MICROPY_ENABLE_MOD_CMATH
265+
Q(cmath)
266+
Q(phase)
267+
Q(polar)
268+
Q(rect)
269+
#endif
261270

262271
Q(mem_total)
263272
Q(mem_current)

unix/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
#define MICROPY_ENABLE_REPL_HELPERS (1)
1111
#define MICROPY_ENABLE_LEXER_UNIX (1)
1212
#define MICROPY_ENABLE_SOURCE_LINE (1)
13-
#define MICROPY_ENABLE_PROPERTY (1)
1413
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
1514
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
1615
#define MICROPY_PATH_MAX (PATH_MAX)
1716
#define MICROPY_USE_COMPUTED_GOTOS (1)
1817
#define MICROPY_MOD_SYS_STDFILES (1)
18+
#define MICROPY_ENABLE_MOD_CMATH (1)
1919

2020
// type definitions for the specific machine
2121

0 commit comments

Comments
 (0)