Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 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); |
Damien George | 93e51b5 | 2014-05-03 18:40:50 +0100 | [diff] [blame] | 68 | return mp_obj_new_complex(sqrt_abs * MICROPY_FLOAT_C_FUN(cos)(theta), sqrt_abs * MICROPY_FLOAT_C_FUN(sin)(theta)); |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 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, |
Damien George | 6d3c5e4 | 2014-04-26 10:47:29 +0100 | [diff] [blame] | 119 | .used = ARRAY_SIZE(mp_module_cmath_globals_table), |
| 120 | .alloc = ARRAY_SIZE(mp_module_cmath_globals_table), |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 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 |