2014-05-03 22:27:38 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/builtin.h"
|
2014-04-17 16:11:03 +00:00
|
|
|
|
2014-06-01 12:32:54 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_CMATH
|
2014-04-17 16:11:03 +00:00
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2014-08-10 16:50:28 +00:00
|
|
|
/// \module cmath - mathematical functions for complex numbers
|
|
|
|
///
|
|
|
|
/// The `cmath` module provides some basic mathematical funtions for
|
|
|
|
/// working with complex numbers.
|
|
|
|
|
|
|
|
/// \function phase(z)
|
|
|
|
/// Returns the phase of the number `z`, in the range (-pi, +pi].
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t mp_cmath_phase(mp_obj_t z_obj) {
|
2014-04-17 16:11:03 +00:00
|
|
|
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);
|
|
|
|
|
2014-08-10 16:50:28 +00:00
|
|
|
/// \function polar(z)
|
|
|
|
/// Returns, as a tuple, the polar form of `z`.
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t mp_cmath_polar(mp_obj_t z_obj) {
|
2014-04-17 16:11:03 +00:00
|
|
|
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);
|
|
|
|
|
2014-08-10 16:50:28 +00:00
|
|
|
/// \function rect(r, phi)
|
2014-08-11 22:19:44 +00:00
|
|
|
/// Returns the complex number with modulus `r` and phase `phi`.
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t mp_cmath_rect(mp_obj_t r_obj, mp_obj_t phi_obj) {
|
2014-04-17 16:11:03 +00:00
|
|
|
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);
|
|
|
|
|
2014-08-10 16:50:28 +00:00
|
|
|
/// \function exp(z)
|
2014-08-11 22:19:44 +00:00
|
|
|
/// Return the exponential of `z`.
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t mp_cmath_exp(mp_obj_t z_obj) {
|
2014-04-17 16:11:03 +00:00
|
|
|
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);
|
|
|
|
|
2014-08-10 16:50:28 +00:00
|
|
|
/// \function log(z)
|
2014-08-11 22:19:44 +00:00
|
|
|
/// Return the natural logarithm of `z`. The branch cut is along the negative real axis.
|
2014-04-17 16:11:03 +00:00
|
|
|
// TODO can take second argument, being the base
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t mp_cmath_log(mp_obj_t z_obj) {
|
2014-04-17 16:11:03 +00:00
|
|
|
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);
|
|
|
|
|
2015-11-17 14:10:13 +00:00
|
|
|
#if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
|
2014-08-10 16:50:28 +00:00
|
|
|
/// \function log10(z)
|
2014-08-11 22:19:44 +00:00
|
|
|
/// Return the base-10 logarithm of `z`. The branch cut is along the negative real axis.
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t mp_cmath_log10(mp_obj_t z_obj) {
|
2014-04-17 16:11:03 +00:00
|
|
|
mp_float_t real, imag;
|
|
|
|
mp_obj_get_complex(z_obj, &real, &imag);
|
2015-02-02 12:52:14 +00:00
|
|
|
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));
|
2014-04-17 16:11:03 +00:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log10_obj, mp_cmath_log10);
|
2015-11-17 14:10:13 +00:00
|
|
|
#endif
|
2014-04-17 16:11:03 +00:00
|
|
|
|
2014-08-10 16:50:28 +00:00
|
|
|
/// \function sqrt(z)
|
2014-08-11 22:19:44 +00:00
|
|
|
/// Return the square-root of `z`.
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t mp_cmath_sqrt(mp_obj_t z_obj) {
|
2014-04-17 16:11:03 +00:00
|
|
|
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);
|
2014-05-03 17:40:50 +00:00
|
|
|
return mp_obj_new_complex(sqrt_abs * MICROPY_FLOAT_C_FUN(cos)(theta), sqrt_abs * MICROPY_FLOAT_C_FUN(sin)(theta));
|
2014-04-17 16:11:03 +00:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sqrt_obj, mp_cmath_sqrt);
|
|
|
|
|
2014-08-10 16:50:28 +00:00
|
|
|
/// \function cos(z)
|
2014-08-11 22:19:44 +00:00
|
|
|
/// Return the cosine of `z`.
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t mp_cmath_cos(mp_obj_t z_obj) {
|
2014-04-17 16:11:03 +00:00
|
|
|
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);
|
|
|
|
|
2014-08-10 16:50:28 +00:00
|
|
|
/// \function sin(z)
|
2014-08-11 22:19:44 +00:00
|
|
|
/// Return the sine of `z`.
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t mp_cmath_sin(mp_obj_t z_obj) {
|
2014-04-17 16:11:03 +00:00
|
|
|
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);
|
|
|
|
|
2015-11-27 13:38:15 +00:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_cmath_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cmath) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_e), mp_const_float_e },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pi), mp_const_float_pi },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_phase), MP_ROM_PTR(&mp_cmath_phase_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_polar), MP_ROM_PTR(&mp_cmath_polar_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_rect), MP_ROM_PTR(&mp_cmath_rect_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_exp), MP_ROM_PTR(&mp_cmath_exp_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_log), MP_ROM_PTR(&mp_cmath_log_obj) },
|
2015-11-17 14:10:13 +00:00
|
|
|
#if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_log10), MP_ROM_PTR(&mp_cmath_log10_obj) },
|
2015-11-17 14:10:13 +00:00
|
|
|
#endif
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sqrt), MP_ROM_PTR(&mp_cmath_sqrt_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_acos), MP_ROM_PTR(&mp_cmath_acos_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_asin), MP_ROM_PTR(&mp_cmath_asin_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_atan), MP_ROM_PTR(&mp_cmath_atan_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_cos), MP_ROM_PTR(&mp_cmath_cos_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sin), MP_ROM_PTR(&mp_cmath_sin_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_tan), MP_ROM_PTR(&mp_cmath_tan_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_acosh), MP_ROM_PTR(&mp_cmath_acosh_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_asinh), MP_ROM_PTR(&mp_cmath_asinh_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_atanh), MP_ROM_PTR(&mp_cmath_atanh_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_cosh), MP_ROM_PTR(&mp_cmath_cosh_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_sinh), MP_ROM_PTR(&mp_cmath_sinh_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_tanh), MP_ROM_PTR(&mp_cmath_tanh_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_isfinite), MP_ROM_PTR(&mp_cmath_isfinite_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_isinf), MP_ROM_PTR(&mp_cmath_isinf_obj) },
|
|
|
|
//{ MP_ROM_QSTR(MP_QSTR_isnan), MP_ROM_PTR(&mp_cmath_isnan_obj) },
|
2014-04-17 16:11:03 +00:00
|
|
|
};
|
|
|
|
|
2014-11-29 14:39:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_cmath_globals, mp_module_cmath_globals_table);
|
2014-04-17 16:11:03 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2014-06-01 12:32:54 +00:00
|
|
|
#endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_CMATH
|