blob: 7ad8f5ad60e62569bc3a0e6062422288df4fb126 [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 George51dfcb42015-01-01 20:27:54 +000027#include "py/builtin.h"
Damien Georgedbdfee12014-04-17 17:11:03 +010028
Paul Sokolovsky2b7236d2015-12-07 00:18:44 +020029#if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_BUILTINS_COMPLEX && MICROPY_PY_CMATH
Damien Georgedbdfee12014-04-17 17:11:03 +010030
Damien George51dfcb42015-01-01 20:27:54 +000031#include <math.h>
32
Damien George30dd23a2014-08-10 17:50:28 +010033/// \module cmath - mathematical functions for complex numbers
34///
35/// The `cmath` module provides some basic mathematical funtions for
36/// working with complex numbers.
37
Damien George30dd23a2014-08-10 17:50:28 +010038/// \function phase(z)
39/// Returns the phase of the number `z`, in the range (-pi, +pi].
Damien George969a6b32014-12-10 22:07:04 +000040STATIC mp_obj_t mp_cmath_phase(mp_obj_t z_obj) {
Damien Georgedbdfee12014-04-17 17:11:03 +010041 mp_float_t real, imag;
42 mp_obj_get_complex(z_obj, &real, &imag);
43 return mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real));
44}
45STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_phase_obj, mp_cmath_phase);
46
Damien George30dd23a2014-08-10 17:50:28 +010047/// \function polar(z)
48/// Returns, as a tuple, the polar form of `z`.
Damien George969a6b32014-12-10 22:07:04 +000049STATIC mp_obj_t mp_cmath_polar(mp_obj_t z_obj) {
Damien Georgedbdfee12014-04-17 17:11:03 +010050 mp_float_t real, imag;
51 mp_obj_get_complex(z_obj, &real, &imag);
52 mp_obj_t tuple[2] = {
53 mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)),
54 mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real)),
55 };
56 return mp_obj_new_tuple(2, tuple);
57}
58STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_polar_obj, mp_cmath_polar);
59
Damien George30dd23a2014-08-10 17:50:28 +010060/// \function rect(r, phi)
Damien George7703d712014-08-11 22:19:44 +000061/// Returns the complex number with modulus `r` and phase `phi`.
Damien George969a6b32014-12-10 22:07:04 +000062STATIC mp_obj_t mp_cmath_rect(mp_obj_t r_obj, mp_obj_t phi_obj) {
Damien Georgedbdfee12014-04-17 17:11:03 +010063 mp_float_t r = mp_obj_get_float(r_obj);
64 mp_float_t phi = mp_obj_get_float(phi_obj);
65 return mp_obj_new_complex(r * MICROPY_FLOAT_C_FUN(cos)(phi), r * MICROPY_FLOAT_C_FUN(sin)(phi));
66}
67STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_cmath_rect_obj, mp_cmath_rect);
68
Damien George30dd23a2014-08-10 17:50:28 +010069/// \function exp(z)
Damien George7703d712014-08-11 22:19:44 +000070/// Return the exponential of `z`.
Damien George969a6b32014-12-10 22:07:04 +000071STATIC mp_obj_t mp_cmath_exp(mp_obj_t z_obj) {
Damien Georgedbdfee12014-04-17 17:11:03 +010072 mp_float_t real, imag;
73 mp_obj_get_complex(z_obj, &real, &imag);
74 mp_float_t exp_real = MICROPY_FLOAT_C_FUN(exp)(real);
75 return mp_obj_new_complex(exp_real * MICROPY_FLOAT_C_FUN(cos)(imag), exp_real * MICROPY_FLOAT_C_FUN(sin)(imag));
76}
77STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_exp_obj, mp_cmath_exp);
78
Damien George30dd23a2014-08-10 17:50:28 +010079/// \function log(z)
Damien George7703d712014-08-11 22:19:44 +000080/// Return the natural logarithm of `z`. The branch cut is along the negative real axis.
Damien Georgedbdfee12014-04-17 17:11:03 +010081// TODO can take second argument, being the base
Damien George969a6b32014-12-10 22:07:04 +000082STATIC mp_obj_t mp_cmath_log(mp_obj_t z_obj) {
Damien Georgedbdfee12014-04-17 17:11:03 +010083 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(log)(real*real + imag*imag), MICROPY_FLOAT_C_FUN(atan2)(imag, real));
86}
87STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log_obj, mp_cmath_log);
88
Damien Georgea24eafa2015-11-17 14:10:13 +000089#if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
Damien George30dd23a2014-08-10 17:50:28 +010090/// \function log10(z)
Damien George7703d712014-08-11 22:19:44 +000091/// Return the base-10 logarithm of `z`. The branch cut is along the negative real axis.
Damien George969a6b32014-12-10 22:07:04 +000092STATIC mp_obj_t mp_cmath_log10(mp_obj_t z_obj) {
Damien Georgedbdfee12014-04-17 17:11:03 +010093 mp_float_t real, imag;
94 mp_obj_get_complex(z_obj, &real, &imag);
Damien Georgef49782f2015-02-02 12:52:14 +000095 return mp_obj_new_complex(0.5 * MICROPY_FLOAT_C_FUN(log10)(real*real + imag*imag), 0.4342944819032518 * MICROPY_FLOAT_C_FUN(atan2)(imag, real));
Damien Georgedbdfee12014-04-17 17:11:03 +010096}
97STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log10_obj, mp_cmath_log10);
Damien Georgea24eafa2015-11-17 14:10:13 +000098#endif
Damien Georgedbdfee12014-04-17 17:11:03 +010099
Damien George30dd23a2014-08-10 17:50:28 +0100100/// \function sqrt(z)
Damien George7703d712014-08-11 22:19:44 +0000101/// Return the square-root of `z`.
Damien George969a6b32014-12-10 22:07:04 +0000102STATIC mp_obj_t mp_cmath_sqrt(mp_obj_t z_obj) {
Damien Georgedbdfee12014-04-17 17:11:03 +0100103 mp_float_t real, imag;
104 mp_obj_get_complex(z_obj, &real, &imag);
105 mp_float_t sqrt_abs = MICROPY_FLOAT_C_FUN(pow)(real*real + imag*imag, 0.25);
106 mp_float_t theta = 0.5 * MICROPY_FLOAT_C_FUN(atan2)(imag, real);
Damien George93e51b52014-05-03 18:40:50 +0100107 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 +0100108}
109STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sqrt_obj, mp_cmath_sqrt);
110
Damien George30dd23a2014-08-10 17:50:28 +0100111/// \function cos(z)
Damien George7703d712014-08-11 22:19:44 +0000112/// Return the cosine of `z`.
Damien George969a6b32014-12-10 22:07:04 +0000113STATIC mp_obj_t mp_cmath_cos(mp_obj_t z_obj) {
Damien Georgedbdfee12014-04-17 17:11:03 +0100114 mp_float_t real, imag;
115 mp_obj_get_complex(z_obj, &real, &imag);
116 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));
117}
118STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_cos_obj, mp_cmath_cos);
119
Damien George30dd23a2014-08-10 17:50:28 +0100120/// \function sin(z)
Damien George7703d712014-08-11 22:19:44 +0000121/// Return the sine of `z`.
Damien George969a6b32014-12-10 22:07:04 +0000122STATIC mp_obj_t mp_cmath_sin(mp_obj_t z_obj) {
Damien Georgedbdfee12014-04-17 17:11:03 +0100123 mp_float_t real, imag;
124 mp_obj_get_complex(z_obj, &real, &imag);
125 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));
126}
127STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sin_obj, mp_cmath_sin);
128
Damien Georgecbf76742015-11-27 13:38:15 +0000129STATIC const mp_rom_map_elem_t mp_module_cmath_globals_table[] = {
130 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cmath) },
131 { MP_ROM_QSTR(MP_QSTR_e), mp_const_float_e },
132 { MP_ROM_QSTR(MP_QSTR_pi), mp_const_float_pi },
133 { MP_ROM_QSTR(MP_QSTR_phase), MP_ROM_PTR(&mp_cmath_phase_obj) },
134 { MP_ROM_QSTR(MP_QSTR_polar), MP_ROM_PTR(&mp_cmath_polar_obj) },
135 { MP_ROM_QSTR(MP_QSTR_rect), MP_ROM_PTR(&mp_cmath_rect_obj) },
136 { MP_ROM_QSTR(MP_QSTR_exp), MP_ROM_PTR(&mp_cmath_exp_obj) },
137 { MP_ROM_QSTR(MP_QSTR_log), MP_ROM_PTR(&mp_cmath_log_obj) },
Damien Georgea24eafa2015-11-17 14:10:13 +0000138 #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
Damien Georgecbf76742015-11-27 13:38:15 +0000139 { MP_ROM_QSTR(MP_QSTR_log10), MP_ROM_PTR(&mp_cmath_log10_obj) },
Damien Georgea24eafa2015-11-17 14:10:13 +0000140 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000141 { MP_ROM_QSTR(MP_QSTR_sqrt), MP_ROM_PTR(&mp_cmath_sqrt_obj) },
142 //{ MP_ROM_QSTR(MP_QSTR_acos), MP_ROM_PTR(&mp_cmath_acos_obj) },
143 //{ MP_ROM_QSTR(MP_QSTR_asin), MP_ROM_PTR(&mp_cmath_asin_obj) },
144 //{ MP_ROM_QSTR(MP_QSTR_atan), MP_ROM_PTR(&mp_cmath_atan_obj) },
145 { MP_ROM_QSTR(MP_QSTR_cos), MP_ROM_PTR(&mp_cmath_cos_obj) },
146 { MP_ROM_QSTR(MP_QSTR_sin), MP_ROM_PTR(&mp_cmath_sin_obj) },
147 //{ MP_ROM_QSTR(MP_QSTR_tan), MP_ROM_PTR(&mp_cmath_tan_obj) },
148 //{ MP_ROM_QSTR(MP_QSTR_acosh), MP_ROM_PTR(&mp_cmath_acosh_obj) },
149 //{ MP_ROM_QSTR(MP_QSTR_asinh), MP_ROM_PTR(&mp_cmath_asinh_obj) },
150 //{ MP_ROM_QSTR(MP_QSTR_atanh), MP_ROM_PTR(&mp_cmath_atanh_obj) },
151 //{ MP_ROM_QSTR(MP_QSTR_cosh), MP_ROM_PTR(&mp_cmath_cosh_obj) },
152 //{ MP_ROM_QSTR(MP_QSTR_sinh), MP_ROM_PTR(&mp_cmath_sinh_obj) },
153 //{ MP_ROM_QSTR(MP_QSTR_tanh), MP_ROM_PTR(&mp_cmath_tanh_obj) },
154 //{ MP_ROM_QSTR(MP_QSTR_isfinite), MP_ROM_PTR(&mp_cmath_isfinite_obj) },
155 //{ MP_ROM_QSTR(MP_QSTR_isinf), MP_ROM_PTR(&mp_cmath_isinf_obj) },
156 //{ MP_ROM_QSTR(MP_QSTR_isnan), MP_ROM_PTR(&mp_cmath_isnan_obj) },
Damien Georgedbdfee12014-04-17 17:11:03 +0100157};
158
Damien George3b603f22014-11-29 14:39:27 +0000159STATIC MP_DEFINE_CONST_DICT(mp_module_cmath_globals, mp_module_cmath_globals_table);
Damien Georgedbdfee12014-04-17 17:11:03 +0100160
161const mp_obj_module_t mp_module_cmath = {
162 .base = { &mp_type_module },
Damien Georgedbdfee12014-04-17 17:11:03 +0100163 .globals = (mp_obj_dict_t*)&mp_module_cmath_globals,
164};
165
Damien Georgefb510b32014-06-01 13:32:54 +0100166#endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_CMATH