blob: 3bc3055dc89fcf035d2c359c29efab2e4abef8ce [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien Georgedbdfee12014-04-17 17:11:03 +010027#include <math.h>
28
29#include "misc.h"
30#include "mpconfig.h"
31#include "qstr.h"
32#include "obj.h"
33#include "builtin.h"
34
Damien Georgefb510b32014-06-01 13:32:54 +010035#if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_CMATH
Damien Georgedbdfee12014-04-17 17:11:03 +010036
37// These are defined in modmath.c
38extern const mp_obj_float_t mp_math_e_obj;
39extern const mp_obj_float_t mp_math_pi_obj;
40
41mp_obj_t mp_cmath_phase(mp_obj_t z_obj) {
42 mp_float_t real, imag;
43 mp_obj_get_complex(z_obj, &real, &imag);
44 return mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real));
45}
46STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_phase_obj, mp_cmath_phase);
47
48mp_obj_t mp_cmath_polar(mp_obj_t z_obj) {
49 mp_float_t real, imag;
50 mp_obj_get_complex(z_obj, &real, &imag);
51 mp_obj_t tuple[2] = {
52 mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)),
53 mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real)),
54 };
55 return mp_obj_new_tuple(2, tuple);
56}
57STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_polar_obj, mp_cmath_polar);
58
59mp_obj_t mp_cmath_rect(mp_obj_t r_obj, mp_obj_t phi_obj) {
60 mp_float_t r = mp_obj_get_float(r_obj);
61 mp_float_t phi = mp_obj_get_float(phi_obj);
62 return mp_obj_new_complex(r * MICROPY_FLOAT_C_FUN(cos)(phi), r * MICROPY_FLOAT_C_FUN(sin)(phi));
63}
64STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_cmath_rect_obj, mp_cmath_rect);
65
66mp_obj_t mp_cmath_exp(mp_obj_t z_obj) {
67 mp_float_t real, imag;
68 mp_obj_get_complex(z_obj, &real, &imag);
69 mp_float_t exp_real = MICROPY_FLOAT_C_FUN(exp)(real);
70 return mp_obj_new_complex(exp_real * MICROPY_FLOAT_C_FUN(cos)(imag), exp_real * MICROPY_FLOAT_C_FUN(sin)(imag));
71}
72STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_exp_obj, mp_cmath_exp);
73
74// TODO can take second argument, being the base
75mp_obj_t mp_cmath_log(mp_obj_t z_obj) {
76 mp_float_t real, imag;
77 mp_obj_get_complex(z_obj, &real, &imag);
78 return mp_obj_new_complex(0.5 * MICROPY_FLOAT_C_FUN(log)(real*real + imag*imag), MICROPY_FLOAT_C_FUN(atan2)(imag, real));
79}
80STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log_obj, mp_cmath_log);
81
82mp_obj_t mp_cmath_log10(mp_obj_t z_obj) {
83 mp_float_t real, imag;
84 mp_obj_get_complex(z_obj, &real, &imag);
85 return mp_obj_new_complex(0.5 * MICROPY_FLOAT_C_FUN(log10)(real*real + imag*imag), MICROPY_FLOAT_C_FUN(atan2)(imag, real));
86}
87STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log10_obj, mp_cmath_log10);
88
89mp_obj_t mp_cmath_sqrt(mp_obj_t z_obj) {
90 mp_float_t real, imag;
91 mp_obj_get_complex(z_obj, &real, &imag);
92 mp_float_t sqrt_abs = MICROPY_FLOAT_C_FUN(pow)(real*real + imag*imag, 0.25);
93 mp_float_t theta = 0.5 * MICROPY_FLOAT_C_FUN(atan2)(imag, real);
Damien George93e51b52014-05-03 18:40:50 +010094 return mp_obj_new_complex(sqrt_abs * MICROPY_FLOAT_C_FUN(cos)(theta), sqrt_abs * MICROPY_FLOAT_C_FUN(sin)(theta));
Damien Georgedbdfee12014-04-17 17:11:03 +010095}
96STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sqrt_obj, mp_cmath_sqrt);
97
98mp_obj_t mp_cmath_cos(mp_obj_t z_obj) {
99 mp_float_t real, imag;
100 mp_obj_get_complex(z_obj, &real, &imag);
101 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));
102}
103STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_cos_obj, mp_cmath_cos);
104
105mp_obj_t mp_cmath_sin(mp_obj_t z_obj) {
106 mp_float_t real, imag;
107 mp_obj_get_complex(z_obj, &real, &imag);
108 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));
109}
110STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sin_obj, mp_cmath_sin);
111
112STATIC const mp_map_elem_t mp_module_cmath_globals_table[] = {
113 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_cmath) },
114 { MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj },
115 { MP_OBJ_NEW_QSTR(MP_QSTR_pi), (mp_obj_t)&mp_math_pi_obj },
116 { MP_OBJ_NEW_QSTR(MP_QSTR_phase), (mp_obj_t)&mp_cmath_phase_obj },
117 { MP_OBJ_NEW_QSTR(MP_QSTR_polar), (mp_obj_t)&mp_cmath_polar_obj },
118 { MP_OBJ_NEW_QSTR(MP_QSTR_rect), (mp_obj_t)&mp_cmath_rect_obj },
119 { MP_OBJ_NEW_QSTR(MP_QSTR_exp), (mp_obj_t)&mp_cmath_exp_obj },
120 { MP_OBJ_NEW_QSTR(MP_QSTR_log), (mp_obj_t)&mp_cmath_log_obj },
121 { MP_OBJ_NEW_QSTR(MP_QSTR_log10), (mp_obj_t)&mp_cmath_log10_obj },
122 { MP_OBJ_NEW_QSTR(MP_QSTR_sqrt), (mp_obj_t)&mp_cmath_sqrt_obj },
123 //{ MP_OBJ_NEW_QSTR(MP_QSTR_acos), (mp_obj_t)&mp_cmath_acos_obj },
124 //{ MP_OBJ_NEW_QSTR(MP_QSTR_asin), (mp_obj_t)&mp_cmath_asin_obj },
125 //{ MP_OBJ_NEW_QSTR(MP_QSTR_atan), (mp_obj_t)&mp_cmath_atan_obj },
126 { MP_OBJ_NEW_QSTR(MP_QSTR_cos), (mp_obj_t)&mp_cmath_cos_obj },
127 { MP_OBJ_NEW_QSTR(MP_QSTR_sin), (mp_obj_t)&mp_cmath_sin_obj },
128 //{ MP_OBJ_NEW_QSTR(MP_QSTR_tan), (mp_obj_t)&mp_cmath_tan_obj },
129 //{ MP_OBJ_NEW_QSTR(MP_QSTR_acosh), (mp_obj_t)&mp_cmath_acosh_obj },
130 //{ MP_OBJ_NEW_QSTR(MP_QSTR_asinh), (mp_obj_t)&mp_cmath_asinh_obj },
131 //{ MP_OBJ_NEW_QSTR(MP_QSTR_atanh), (mp_obj_t)&mp_cmath_atanh_obj },
132 //{ MP_OBJ_NEW_QSTR(MP_QSTR_cosh), (mp_obj_t)&mp_cmath_cosh_obj },
133 //{ MP_OBJ_NEW_QSTR(MP_QSTR_sinh), (mp_obj_t)&mp_cmath_sinh_obj },
134 //{ MP_OBJ_NEW_QSTR(MP_QSTR_tanh), (mp_obj_t)&mp_cmath_tanh_obj },
135 //{ MP_OBJ_NEW_QSTR(MP_QSTR_isfinite), (mp_obj_t)&mp_cmath_isfinite_obj },
136 //{ MP_OBJ_NEW_QSTR(MP_QSTR_isinf), (mp_obj_t)&mp_cmath_isinf_obj },
137 //{ MP_OBJ_NEW_QSTR(MP_QSTR_isnan), (mp_obj_t)&mp_cmath_isnan_obj },
138};
139
140STATIC const mp_obj_dict_t mp_module_cmath_globals = {
141 .base = {&mp_type_dict},
142 .map = {
143 .all_keys_are_qstrs = 1,
144 .table_is_fixed_array = 1,
Damien George6d3c5e42014-04-26 10:47:29 +0100145 .used = ARRAY_SIZE(mp_module_cmath_globals_table),
146 .alloc = ARRAY_SIZE(mp_module_cmath_globals_table),
Damien Georgedbdfee12014-04-17 17:11:03 +0100147 .table = (mp_map_elem_t*)mp_module_cmath_globals_table,
148 },
149};
150
151const mp_obj_module_t mp_module_cmath = {
152 .base = { &mp_type_module },
153 .name = MP_QSTR_cmath,
154 .globals = (mp_obj_dict_t*)&mp_module_cmath_globals,
155};
156
Damien Georgefb510b32014-06-01 13:32:54 +0100157#endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_CMATH